42429b791e91eb9d58724f0fd8cde645171ec8cb
[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 #include <linux/usb/ehci_pdriver.h>
22 #include <linux/usb/ohci_pdriver.h>
23
24 #include <asm/addrspace.h>
25
26 #include <asm/mach-ralink/rt305x.h>
27 #include <asm/mach-ralink/rt305x_regs.h>
28 #include "devices.h"
29
30 #include <ramips_eth_platform.h>
31 #include <rt305x_esw_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         u32 reset_bits = RT305X_RESET_FE;
120
121         if (soc_is_rt5350())
122                 reset_bits |= RT305X_RESET_ESW;
123         rt305x_sysc_wr(reset_bits, SYSC_REG_RESET_CTRL);
124         rt305x_sysc_wr(0, SYSC_REG_RESET_CTRL);
125 }
126
127 static struct resource rt305x_eth_resources[] = {
128         {
129                 .start  = RT305X_FE_BASE,
130                 .end    = RT305X_FE_BASE + PAGE_SIZE - 1,
131                 .flags  = IORESOURCE_MEM,
132         }, {
133                 .start  = RT305X_CPU_IRQ_FE,
134                 .end    = RT305X_CPU_IRQ_FE,
135                 .flags  = IORESOURCE_IRQ,
136         },
137 };
138
139 static struct ramips_eth_platform_data ramips_eth_data = {
140         .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
141         .reset_fe = rt305x_fe_reset,
142         .min_pkt_len = 64,
143 };
144
145 static struct platform_device rt305x_eth_device = {
146         .name           = "ramips_eth",
147         .resource       = rt305x_eth_resources,
148         .num_resources  = ARRAY_SIZE(rt305x_eth_resources),
149         .dev = {
150                 .platform_data = &ramips_eth_data,
151         }
152 };
153
154 static struct resource rt305x_esw_resources[] = {
155         {
156                 .start  = RT305X_SWITCH_BASE,
157                 .end    = RT305X_SWITCH_BASE + PAGE_SIZE - 1,
158                 .flags  = IORESOURCE_MEM,
159         },
160 };
161
162 struct rt305x_esw_platform_data rt305x_esw_data = {
163         /* All ports are LAN ports. */
164         .vlan_config            = RT305X_ESW_VLAN_CONFIG_NONE,
165         .reg_initval_fct2       = 0x00d6500c,
166         /*
167          * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
168          * turbo mii off, rgmi 3.3v off
169          * port5: disabled
170          * port6: enabled, gige, full-duplex, rx/tx-flow-control
171          */
172         .reg_initval_fpa2       = 0x3f502b28,
173 };
174
175 static struct platform_device rt305x_esw_device = {
176         .name           = "rt305x-esw",
177         .resource       = rt305x_esw_resources,
178         .num_resources  = ARRAY_SIZE(rt305x_esw_resources),
179         .dev = {
180                 .platform_data = &rt305x_esw_data,
181         }
182 };
183
184 void __init rt305x_register_ethernet(void)
185 {
186         struct clk *clk;
187
188         clk = clk_get(NULL, "sys");
189         if (IS_ERR(clk))
190                 panic("unable to get SYS clock, err=%ld", PTR_ERR(clk));
191
192         ramips_eth_data.sys_freq = clk_get_rate(clk);
193
194         platform_device_register(&rt305x_esw_device);
195         platform_device_register(&rt305x_eth_device);
196 }
197
198 static struct resource rt305x_wifi_resources[] = {
199         {
200                 .start  = RT305X_WMAC_BASE,
201                 .end    = RT305X_WMAC_BASE + 0x3FFFF,
202                 .flags  = IORESOURCE_MEM,
203         }, {
204                 .start  = RT305X_CPU_IRQ_WNIC,
205                 .end    = RT305X_CPU_IRQ_WNIC,
206                 .flags  = IORESOURCE_IRQ,
207         },
208 };
209
210 static struct rt2x00_platform_data rt305x_wifi_data;
211 static struct platform_device rt305x_wifi_device = {
212         .name                   = "rt2800_wmac",
213         .resource               = rt305x_wifi_resources,
214         .num_resources  = ARRAY_SIZE(rt305x_wifi_resources),
215         .dev = {
216                 .platform_data = &rt305x_wifi_data,
217         }
218 };
219
220 void __init rt305x_register_wifi(void)
221 {
222         u32 t;
223
224         rt305x_wifi_data.eeprom_file_name = "soc_wmac.eeprom";
225
226         if (soc_is_rt3352() || soc_is_rt5350()) {
227                 t = rt305x_sysc_rr(SYSC_REG_SYSTEM_CONFIG);
228                 t &= RT3352_SYSCFG0_XTAL_SEL;
229                 if (!t)
230                         rt305x_wifi_data.clk_is_20mhz = 1;
231         }
232         platform_device_register(&rt305x_wifi_device);
233 }
234
235 static struct resource rt305x_wdt_resources[] = {
236         {
237                 .start  = RT305X_TIMER_BASE,
238                 .end    = RT305X_TIMER_BASE + RT305X_TIMER_SIZE - 1,
239                 .flags  = IORESOURCE_MEM,
240         },
241 };
242
243 static struct platform_device rt305x_wdt_device = {
244         .name           = "ramips-wdt",
245         .id             = -1,
246         .resource       = rt305x_wdt_resources,
247         .num_resources  = ARRAY_SIZE(rt305x_wdt_resources),
248 };
249
250 void __init rt305x_register_wdt(void)
251 {
252         u32 t;
253
254         /* enable WDT reset output on pin SRAM_CS_N */
255         t = rt305x_sysc_rr(SYSC_REG_SYSTEM_CONFIG);
256         t |= RT305X_SYSCFG_SRAM_CS0_MODE_WDT <<
257              RT305X_SYSCFG_SRAM_CS0_MODE_SHIFT;
258         rt305x_sysc_wr(t, SYSC_REG_SYSTEM_CONFIG);
259
260         platform_device_register(&rt305x_wdt_device);
261 }
262
263 static struct resource rt305x_spi_resources[] = {
264         {
265                 .flags  = IORESOURCE_MEM,
266                 .start  = RT305X_SPI_BASE,
267                 .end    = RT305X_SPI_BASE + RT305X_SPI_SIZE - 1,
268         },
269 };
270
271 static struct platform_device rt305x_spi_device = {
272         .name           = "ramips-spi",
273         .id             = 0,
274         .resource       = rt305x_spi_resources,
275         .num_resources  = ARRAY_SIZE(rt305x_spi_resources),
276 };
277
278 void __init rt305x_register_spi(struct spi_board_info *info, int n)
279 {
280         spi_register_board_info(info, n);
281         platform_device_register(&rt305x_spi_device);
282 }
283
284 static struct resource rt305x_dwc_otg_resources[] = {
285         {
286                 .start  = RT305X_OTG_BASE,
287                 .end    = RT305X_OTG_BASE + 0x3FFFF,
288                 .flags  = IORESOURCE_MEM,
289         }, {
290                 .start  = RT305X_INTC_IRQ_OTG,
291                 .end    = RT305X_INTC_IRQ_OTG,
292                 .flags  = IORESOURCE_IRQ,
293         },
294 };
295
296 static struct platform_device rt305x_dwc_otg_device = {
297         .name                   = "dwc_otg",
298         .resource               = rt305x_dwc_otg_resources,
299         .num_resources  = ARRAY_SIZE(rt305x_dwc_otg_resources),
300         .dev = {
301                 .platform_data = NULL,
302         }
303 };
304
305 static atomic_t rt3352_usb_pwr_ref = ATOMIC_INIT(0);
306
307 static int rt3352_usb_power_on(struct platform_device *pdev)
308 {
309
310         if (atomic_inc_return(&rt3352_usb_pwr_ref) == 1) {
311                 u32 t;
312
313                 t = rt305x_sysc_rr(RT3352_SYSC_REG_USB_PS);
314
315                 /* enable clock for port0's and port1's phys */
316                 t = rt305x_sysc_rr(RT3352_SYSC_REG_CLKCFG1);
317                 t |= RT3352_CLKCFG1_UPHY0_CLK_EN | RT3352_CLKCFG1_UPHY1_CLK_EN;
318                 rt305x_sysc_wr(t, RT3352_SYSC_REG_CLKCFG1);
319                 mdelay(500);
320
321                 /* pull USBHOST and USBDEV out from reset */
322                 t = rt305x_sysc_rr(RT3352_SYSC_REG_RSTCTRL);
323                 t &= ~(RT3352_RSTCTRL_UHST | RT3352_RSTCTRL_UDEV);
324                 rt305x_sysc_wr(t, RT3352_SYSC_REG_RSTCTRL);
325                 mdelay(500);
326
327                 /* enable host mode */
328                 t = rt305x_sysc_rr(RT3352_SYSC_REG_SYSCFG1);
329                 t |= RT3352_SYSCFG1_USB0_HOST_MODE;
330                 rt305x_sysc_wr(t, RT3352_SYSC_REG_SYSCFG1);
331
332                 t = rt305x_sysc_rr(RT3352_SYSC_REG_USB_PS);
333         }
334
335         return 0;
336 }
337
338 static void rt3352_usb_power_off(struct platform_device *pdev)
339 {
340
341         if (atomic_dec_return(&rt3352_usb_pwr_ref) == 0) {
342                 u32 t;
343
344                 /* put USBHOST and USBDEV into reset */
345                 t = rt305x_sysc_rr(RT3352_SYSC_REG_RSTCTRL);
346                 t |= RT3352_RSTCTRL_UHST | RT3352_RSTCTRL_UDEV;
347                 rt305x_sysc_wr(t, RT3352_SYSC_REG_RSTCTRL);
348                 udelay(10000);
349
350                 /* disable clock for port0's and port1's phys*/
351                 t = rt305x_sysc_rr(RT3352_SYSC_REG_CLKCFG1);
352                 t &= ~(RT3352_CLKCFG1_UPHY0_CLK_EN | RT3352_CLKCFG1_UPHY1_CLK_EN);
353                 rt305x_sysc_wr(t, RT3352_SYSC_REG_CLKCFG1);
354                 udelay(10000);
355         }
356 }
357
358 static struct usb_ehci_pdata rt3352_ehci_data = {
359         .port_power_off = 1,
360         .power_on       = rt3352_usb_power_on,
361         .power_off      = rt3352_usb_power_off,
362 };
363
364 static struct resource rt3352_ehci_resources[] = {
365         {
366                 .start  = RT3352_EHCI_BASE,
367                 .end    = RT3352_EHCI_BASE + RT3352_EHCI_SIZE - 1,
368                 .flags  = IORESOURCE_MEM,
369         }, {
370                 .start  = RT305X_INTC_IRQ_OTG,
371                 .end    = RT305X_INTC_IRQ_OTG,
372                 .flags  = IORESOURCE_IRQ,
373         },
374 };
375
376 static u64 rt3352_ehci_dmamask = DMA_BIT_MASK(32);
377 static struct platform_device rt3352_ehci_device = {
378         .name           = "ehci-platform",
379         .id             = -1,
380         .resource       = rt3352_ehci_resources,
381         .num_resources  = ARRAY_SIZE(rt3352_ehci_resources),
382         .dev            = {
383                 .dma_mask               = &rt3352_ehci_dmamask,
384                 .coherent_dma_mask      = DMA_BIT_MASK(32),
385                 .platform_data          = &rt3352_ehci_data,
386         },
387 };
388
389 static struct resource rt3352_ohci_resources[] = {
390         {
391                 .start  = RT3352_OHCI_BASE,
392                 .end    = RT3352_OHCI_BASE + RT3352_OHCI_SIZE - 1,
393                 .flags  = IORESOURCE_MEM,
394         }, {
395                 .start  = RT305X_INTC_IRQ_OTG,
396                 .end    = RT305X_INTC_IRQ_OTG,
397                 .flags  = IORESOURCE_IRQ,
398         },
399 };
400
401 static struct usb_ohci_pdata rt3352_ohci_data = {
402         .power_on       = rt3352_usb_power_on,
403         .power_off      = rt3352_usb_power_off,
404 };
405
406 static u64 rt3352_ohci_dmamask = DMA_BIT_MASK(32);
407 static struct platform_device rt3352_ohci_device = {
408         .name           = "ohci-platform",
409         .id             = -1,
410         .resource       = rt3352_ohci_resources,
411         .num_resources  = ARRAY_SIZE(rt3352_ohci_resources),
412         .dev            = {
413                 .dma_mask               = &rt3352_ohci_dmamask,
414                 .coherent_dma_mask      = DMA_BIT_MASK(32),
415                 .platform_data          = &rt3352_ohci_data,
416         },
417 };
418
419 void __init rt305x_register_usb(void)
420 {
421         if (soc_is_rt305x() || soc_is_rt3350()) {
422                 platform_device_register(&rt305x_dwc_otg_device);
423         } else if (soc_is_rt3352() || soc_is_rt5350()) {
424                 platform_device_register(&rt3352_ehci_device);
425                 platform_device_register(&rt3352_ohci_device);
426         } else {
427                 BUG();
428         }
429 }