ramips: rename rt3352 usb platform devices
[oweals/openwrt.git] / target / linux / ramips / files / arch / mips / ralink / rt305x / devices.c
1 /*
2  *  Ralink RT305x SoC platform device registration
3  *
4  *  Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/err.h>
14 #include <linux/clk.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/physmap.h>
17 #include <linux/spi/spi.h>
18 #include <linux/rt2x00_platform.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21
22 #include <asm/addrspace.h>
23
24 #include <asm/mach-ralink/rt305x.h>
25 #include <asm/mach-ralink/rt305x_regs.h>
26 #include "devices.h"
27
28 #include <ramips_eth_platform.h>
29 #include <rt305x_esw_platform.h>
30 #include <rt3883_ehci_platform.h>
31 #include <rt3883_ohci_platform.h>
32
33 static struct resource rt305x_flash0_resources[] = {
34         {
35                 .flags  = IORESOURCE_MEM,
36                 .start  = KSEG1ADDR(RT305X_FLASH0_BASE),
37                 .end    = KSEG1ADDR(RT305X_FLASH0_BASE) +
38                           RT305X_FLASH0_SIZE - 1,
39         },
40 };
41
42 struct physmap_flash_data rt305x_flash0_data;
43 static struct platform_device rt305x_flash0_device = {
44         .name           = "physmap-flash",
45         .resource       = rt305x_flash0_resources,
46         .num_resources  = ARRAY_SIZE(rt305x_flash0_resources),
47         .dev = {
48                 .platform_data = &rt305x_flash0_data,
49         },
50 };
51
52 static struct resource rt305x_flash1_resources[] = {
53         {
54                 .flags  = IORESOURCE_MEM,
55                 .start  = KSEG1ADDR(RT305X_FLASH1_BASE),
56                 .end    = KSEG1ADDR(RT305X_FLASH1_BASE) +
57                           RT305X_FLASH1_SIZE - 1,
58         },
59 };
60
61 struct physmap_flash_data rt305x_flash1_data;
62 static struct platform_device rt305x_flash1_device = {
63         .name           = "physmap-flash",
64         .resource       = rt305x_flash1_resources,
65         .num_resources  = ARRAY_SIZE(rt305x_flash1_resources),
66         .dev = {
67                 .platform_data = &rt305x_flash1_data,
68         },
69 };
70
71 static int rt305x_flash_instance __initdata;
72 void __init rt305x_register_flash(unsigned int id)
73 {
74         struct platform_device *pdev;
75         struct physmap_flash_data *pdata;
76         u32 t;
77         int reg;
78
79         switch (id) {
80         case 0:
81                 pdev = &rt305x_flash0_device;
82                 reg = MEMC_REG_FLASH_CFG0;
83                 break;
84         case 1:
85                 pdev = &rt305x_flash1_device;
86                 reg = MEMC_REG_FLASH_CFG1;
87                 break;
88         default:
89                 return;
90         }
91
92         t = rt305x_memc_rr(reg);
93         t = (t >> FLASH_CFG_WIDTH_SHIFT) & FLASH_CFG_WIDTH_MASK;
94
95         pdata = pdev->dev.platform_data;
96         switch (t) {
97         case FLASH_CFG_WIDTH_8BIT:
98                 pdata->width = 1;
99                 break;
100         case FLASH_CFG_WIDTH_16BIT:
101                 pdata->width = 2;
102                 break;
103         case FLASH_CFG_WIDTH_32BIT:
104                 pdata->width = 4;
105                 break;
106         default:
107                 printk(KERN_ERR "RT305x: flash bank%u witdh is invalid\n", id);
108                 return;
109         }
110
111         pdev->id = rt305x_flash_instance;
112
113         platform_device_register(pdev);
114         rt305x_flash_instance++;
115 }
116
117 static void rt305x_fe_reset(void)
118 {
119         rt305x_sysc_wr(RT305X_RESET_FE, SYSC_REG_RESET_CTRL);
120         rt305x_sysc_wr(0, SYSC_REG_RESET_CTRL);
121 }
122
123 static struct resource rt305x_eth_resources[] = {
124         {
125                 .start  = RT305X_FE_BASE,
126                 .end    = RT305X_FE_BASE + PAGE_SIZE - 1,
127                 .flags  = IORESOURCE_MEM,
128         }, {
129                 .start  = RT305X_CPU_IRQ_FE,
130                 .end    = RT305X_CPU_IRQ_FE,
131                 .flags  = IORESOURCE_IRQ,
132         },
133 };
134
135 static struct ramips_eth_platform_data ramips_eth_data = {
136         .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
137         .reset_fe = rt305x_fe_reset,
138         .min_pkt_len = 64,
139 };
140
141 static struct platform_device rt305x_eth_device = {
142         .name           = "ramips_eth",
143         .resource       = rt305x_eth_resources,
144         .num_resources  = ARRAY_SIZE(rt305x_eth_resources),
145         .dev = {
146                 .platform_data = &ramips_eth_data,
147         }
148 };
149
150 static struct resource rt305x_esw_resources[] = {
151         {
152                 .start  = RT305X_SWITCH_BASE,
153                 .end    = RT305X_SWITCH_BASE + PAGE_SIZE - 1,
154                 .flags  = IORESOURCE_MEM,
155         },
156 };
157
158 struct rt305x_esw_platform_data rt305x_esw_data = {
159         .vlan_config            = RT305X_ESW_VLAN_CONFIG_NONE,
160         .reg_initval_fct2       = 0x00d6500c,
161         .reg_initval_fpa2       = 0x3f502b28,
162 };
163
164 static struct platform_device rt305x_esw_device = {
165         .name           = "rt305x-esw",
166         .resource       = rt305x_esw_resources,
167         .num_resources  = ARRAY_SIZE(rt305x_esw_resources),
168         .dev = {
169                 .platform_data = &rt305x_esw_data,
170         }
171 };
172
173 void __init rt305x_register_ethernet(void)
174 {
175         struct clk *clk;
176
177         clk = clk_get(NULL, "sys");
178         if (IS_ERR(clk))
179                 panic("unable to get SYS clock, err=%ld", PTR_ERR(clk));
180
181         ramips_eth_data.sys_freq = clk_get_rate(clk);
182
183         platform_device_register(&rt305x_esw_device);
184         platform_device_register(&rt305x_eth_device);
185 }
186
187 static struct resource rt305x_wifi_resources[] = {
188         {
189                 .start  = RT305X_WMAC_BASE,
190                 .end    = RT305X_WMAC_BASE + 0x3FFFF,
191                 .flags  = IORESOURCE_MEM,
192         }, {
193                 .start  = RT305X_CPU_IRQ_WNIC,
194                 .end    = RT305X_CPU_IRQ_WNIC,
195                 .flags  = IORESOURCE_IRQ,
196         },
197 };
198
199 static struct rt2x00_platform_data rt305x_wifi_data;
200 static struct platform_device rt305x_wifi_device = {
201         .name                   = "rt2800_wmac",
202         .resource               = rt305x_wifi_resources,
203         .num_resources  = ARRAY_SIZE(rt305x_wifi_resources),
204         .dev = {
205                 .platform_data = &rt305x_wifi_data,
206         }
207 };
208
209 void __init rt305x_register_wifi(void)
210 {
211         rt305x_wifi_data.eeprom_file_name = "RT305X.eeprom";
212         platform_device_register(&rt305x_wifi_device);
213 }
214
215 static struct resource rt305x_wdt_resources[] = {
216         {
217                 .start  = RT305X_TIMER_BASE,
218                 .end    = RT305X_TIMER_BASE + RT305X_TIMER_SIZE - 1,
219                 .flags  = IORESOURCE_MEM,
220         },
221 };
222
223 static struct platform_device rt305x_wdt_device = {
224         .name           = "ramips-wdt",
225         .id             = -1,
226         .resource       = rt305x_wdt_resources,
227         .num_resources  = ARRAY_SIZE(rt305x_wdt_resources),
228 };
229
230 void __init rt305x_register_wdt(void)
231 {
232         u32 t;
233
234         /* enable WDT reset output on pin SRAM_CS_N */
235         t = rt305x_sysc_rr(SYSC_REG_SYSTEM_CONFIG);
236         t |= RT305X_SYSCFG_SRAM_CS0_MODE_WDT <<
237              RT305X_SYSCFG_SRAM_CS0_MODE_SHIFT;
238         rt305x_sysc_wr(t, SYSC_REG_SYSTEM_CONFIG);
239
240         platform_device_register(&rt305x_wdt_device);
241 }
242
243 static struct resource rt305x_spi_resources[] = {
244         {
245                 .flags  = IORESOURCE_MEM,
246                 .start  = RT305X_SPI_BASE,
247                 .end    = RT305X_SPI_BASE + RT305X_SPI_SIZE - 1,
248         },
249 };
250
251 static struct platform_device rt305x_spi_device = {
252         .name           = "ramips-spi",
253         .id             = 0,
254         .resource       = rt305x_spi_resources,
255         .num_resources  = ARRAY_SIZE(rt305x_spi_resources),
256 };
257
258 void __init rt305x_register_spi(struct spi_board_info *info, int n)
259 {
260         spi_register_board_info(info, n);
261         platform_device_register(&rt305x_spi_device);
262 }
263
264 static struct resource rt305x_dwc_otg_resources[] = {
265         {
266                 .start  = RT305X_OTG_BASE,
267                 .end    = RT305X_OTG_BASE + 0x3FFFF,
268                 .flags  = IORESOURCE_MEM,
269         }, {
270                 .start  = RT305X_INTC_IRQ_OTG,
271                 .end    = RT305X_INTC_IRQ_OTG,
272                 .flags  = IORESOURCE_IRQ,
273         },
274 };
275
276 static struct platform_device rt305x_dwc_otg_device = {
277         .name                   = "dwc_otg",
278         .resource               = rt305x_dwc_otg_resources,
279         .num_resources  = ARRAY_SIZE(rt305x_dwc_otg_resources),
280         .dev = {
281                 .platform_data = NULL,
282         }
283 };
284
285 static atomic_t rt3352_usb_use_count = ATOMIC_INIT(0);
286
287 static void rt3352_usb_host_start(void)
288 {
289         u32 t;
290
291         if (atomic_inc_return(&rt3352_usb_use_count) != 1)
292                 return;
293
294         t = rt305x_sysc_rr(RT3352_SYSC_REG_USB_PS);
295
296         /* enable clock for port0's and port1's phys */
297         t = rt305x_sysc_rr(RT3352_SYSC_REG_CLKCFG1);
298         t = t | RT3352_CLKCFG1_UPHY0_CLK_EN | RT3352_CLKCFG1_UPHY1_CLK_EN;
299         rt305x_sysc_wr(t, RT3352_SYSC_REG_CLKCFG1);
300         mdelay(500);
301
302         /* pull USBHOST and USBDEV out from reset */
303         t = rt305x_sysc_rr(RT3352_SYSC_REG_RSTCTRL);
304         t &= ~(RT3352_RSTCTRL_UHST | RT3352_RSTCTRL_UDEV);
305         rt305x_sysc_wr(t, RT3352_SYSC_REG_RSTCTRL);
306         mdelay(500);
307
308         /* enable host mode */
309         t = rt305x_sysc_rr(RT3352_SYSC_REG_SYSCFG1);
310         t |= RT3352_SYSCFG1_USB0_HOST_MODE;
311         rt305x_sysc_wr(t, RT3352_SYSC_REG_SYSCFG1);
312
313         t = rt305x_sysc_rr(RT3352_SYSC_REG_USB_PS);
314 }
315
316 static void rt3352_usb_host_stop(void)
317 {
318         u32 t;
319
320         if (atomic_dec_return(&rt3352_usb_use_count) != 0)
321                 return;
322
323         /* put USBHOST and USBDEV into reset */
324         t = rt305x_sysc_rr(RT3352_SYSC_REG_RSTCTRL);
325         t |= RT3352_RSTCTRL_UHST | RT3352_RSTCTRL_UDEV;
326         rt305x_sysc_wr(t, RT3352_SYSC_REG_RSTCTRL);
327         udelay(10000);
328
329         /* disable clock for port0's and port1's phys*/
330         t = rt305x_sysc_rr(RT3352_SYSC_REG_CLKCFG1);
331         t &= ~(RT3352_CLKCFG1_UPHY0_CLK_EN | RT3352_CLKCFG1_UPHY1_CLK_EN);
332         rt305x_sysc_wr(t, RT3352_SYSC_REG_CLKCFG1);
333         udelay(10000);
334 }
335
336 static struct rt3883_ehci_platform_data rt3352_ehci_data = {
337         .start_hw       = rt3352_usb_host_start,
338         .stop_hw        = rt3352_usb_host_stop,
339 };
340
341 static struct resource rt3352_ehci_resources[] = {
342         {
343                 .start  = RT3352_EHCI_BASE,
344                 .end    = RT3352_EHCI_BASE + RT3352_EHCI_SIZE - 1,
345                 .flags  = IORESOURCE_MEM,
346         }, {
347                 .start  = RT305X_INTC_IRQ_OTG,
348                 .end    = RT305X_INTC_IRQ_OTG,
349                 .flags  = IORESOURCE_IRQ,
350         },
351 };
352
353 static u64 rt3352_ehci_dmamask = DMA_BIT_MASK(32);
354 static struct platform_device rt3352_ehci_device = {
355         .name           = "rt3883-ehci",
356         .id             = -1,
357         .resource       = rt3352_ehci_resources,
358         .num_resources  = ARRAY_SIZE(rt3352_ehci_resources),
359         .dev            = {
360                 .dma_mask               = &rt3352_ehci_dmamask,
361                 .coherent_dma_mask      = DMA_BIT_MASK(32),
362                 .platform_data          = &rt3352_ehci_data,
363         },
364 };
365
366 static struct resource rt3352_ohci_resources[] = {
367         {
368                 .start  = RT3352_OHCI_BASE,
369                 .end    = RT3352_OHCI_BASE + RT3352_OHCI_SIZE - 1,
370                 .flags  = IORESOURCE_MEM,
371         }, {
372                 .start  = RT305X_INTC_IRQ_OTG,
373                 .end    = RT305X_INTC_IRQ_OTG,
374                 .flags  = IORESOURCE_IRQ,
375         },
376 };
377
378 static struct rt3883_ohci_platform_data rt3352_ohci_data = {
379         .start_hw       = rt3352_usb_host_start,
380         .stop_hw        = rt3352_usb_host_stop,
381 };
382
383 static u64 rt3352_ohci_dmamask = DMA_BIT_MASK(32);
384 static struct platform_device rt3352_ohci_device = {
385         .name           = "rt3883-ohci",
386         .id             = -1,
387         .resource       = rt3352_ohci_resources,
388         .num_resources  = ARRAY_SIZE(rt3352_ohci_resources),
389         .dev            = {
390                 .dma_mask               = &rt3352_ohci_dmamask,
391                 .coherent_dma_mask      = DMA_BIT_MASK(32),
392                 .platform_data          = &rt3352_ohci_data,
393         },
394 };
395
396 void __init rt305x_register_usb(void)
397 {
398         if (soc_is_rt305x() || soc_is_rt3350()) {
399                 platform_device_register(&rt305x_dwc_otg_device);
400         } else if (soc_is_rt3352()) {
401                 platform_device_register(&rt3352_ehci_device);
402                 platform_device_register(&rt3352_ohci_device);
403         } else {
404                 BUG();
405         }
406 }