[brcm63xx] drop support for 2.6.30 kernel
[openwrt.git] / target / linux / ar7 / files-2.6.30 / arch / mips / ar7 / platform.c
1 /*
2  * Copyright (C) 2006,2007 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2006,2007 Eugene Konev <ejka@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include <linux/autoconf.h>
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_device.h>
27 #include <linux/mtd/physmap.h>
28 #include <linux/serial.h>
29 #include <linux/serial_8250.h>
30 #include <linux/ioport.h>
31 #include <linux/io.h>
32 #include <linux/version.h>
33 #include <linux/vlynq.h>
34 #include <linux/leds.h>
35 #include <linux/string.h>
36
37 #include <asm/addrspace.h>
38 #include <asm/ar7/ar7.h>
39 #include <asm/ar7/gpio.h>
40 #include <asm/ar7/prom.h>
41
42 struct plat_vlynq_data {
43         struct plat_vlynq_ops ops;
44         int gpio_bit;
45         int reset_bit;
46 };
47
48
49 static int vlynq_on(struct vlynq_device *dev)
50 {
51         int result;
52         struct plat_vlynq_data *pdata = dev->dev.platform_data;
53
54         if ((result = gpio_request(pdata->gpio_bit, "vlynq")))
55                 goto out;
56
57         ar7_device_reset(pdata->reset_bit);
58
59         if ((result = ar7_gpio_disable(pdata->gpio_bit)))
60                 goto out_enabled;
61
62         if ((result = ar7_gpio_enable(pdata->gpio_bit)))
63                 goto out_enabled;
64
65         if ((result = gpio_direction_output(pdata->gpio_bit, 0)))
66                 goto out_gpio_enabled;
67
68         mdelay(50);
69
70         gpio_set_value(pdata->gpio_bit, 1);
71         mdelay(50);
72
73         return 0;
74
75 out_gpio_enabled:
76         ar7_gpio_disable(pdata->gpio_bit);
77 out_enabled:
78         ar7_device_disable(pdata->reset_bit);
79         gpio_free(pdata->gpio_bit);
80 out:
81         return result;
82 }
83
84 static void vlynq_off(struct vlynq_device *dev)
85 {
86         struct plat_vlynq_data *pdata = dev->dev.platform_data;
87         ar7_gpio_disable(pdata->gpio_bit);
88         gpio_free(pdata->gpio_bit);
89         ar7_device_disable(pdata->reset_bit);
90 }
91
92 static struct resource physmap_flash_resource = {
93         .name = "mem",
94         .flags = IORESOURCE_MEM,
95         .start = 0x10000000,
96         .end = 0x107fffff,
97 };
98
99 static struct resource cpmac_low_res[] = {
100         {
101                 .name = "regs",
102                 .flags = IORESOURCE_MEM,
103                 .start = AR7_REGS_MAC0,
104                 .end = AR7_REGS_MAC0 + 0x7ff,
105         },
106         {
107                 .name = "irq",
108                 .flags = IORESOURCE_IRQ,
109                 .start = 27,
110                 .end = 27,
111         },
112 };
113
114 static struct resource cpmac_high_res[] = {
115         {
116                 .name = "regs",
117                 .flags = IORESOURCE_MEM,
118                 .start = AR7_REGS_MAC1,
119                 .end = AR7_REGS_MAC1 + 0x7ff,
120         },
121         {
122                 .name = "irq",
123                 .flags = IORESOURCE_IRQ,
124                 .start = 41,
125                 .end = 41,
126         },
127 };
128
129 static struct resource vlynq_low_res[] = {
130         {
131                 .name = "regs",
132                 .flags = IORESOURCE_MEM,
133                 .start = AR7_REGS_VLYNQ0,
134                 .end = AR7_REGS_VLYNQ0 + 0xff,
135         },
136         {
137                 .name = "irq",
138                 .flags = IORESOURCE_IRQ,
139                 .start = 29,
140                 .end = 29,
141         },
142         {
143                 .name = "mem",
144                 .flags = IORESOURCE_MEM,
145                 .start = 0x04000000,
146                 .end = 0x04ffffff,
147         },
148         {
149                 .name = "devirq",
150                 .flags = IORESOURCE_IRQ,
151                 .start = 80,
152                 .end = 111,
153         },
154 };
155
156 static struct resource vlynq_high_res[] = {
157         {
158                 .name = "regs",
159                 .flags = IORESOURCE_MEM,
160                 .start = AR7_REGS_VLYNQ1,
161                 .end = AR7_REGS_VLYNQ1 + 0xff,
162         },
163         {
164                 .name = "irq",
165                 .flags = IORESOURCE_IRQ,
166                 .start = 33,
167                 .end = 33,
168         },
169         {
170                 .name = "mem",
171                 .flags = IORESOURCE_MEM,
172                 .start = 0x0c000000,
173                 .end = 0x0cffffff,
174         },
175         {
176                 .name = "devirq",
177                 .flags = IORESOURCE_IRQ,
178                 .start = 112,
179                 .end = 143,
180         },
181 };
182
183 static struct resource usb_res[] = {
184         {
185                 .name = "regs",
186                 .flags = IORESOURCE_MEM,
187                 .start = AR7_REGS_USB,
188                 .end = AR7_REGS_USB + 0xff,
189         },
190         {
191                 .name = "irq",
192                 .flags = IORESOURCE_IRQ,
193                 .start = 32,
194                 .end = 32,
195         },
196         {
197                 .name = "mem",
198                 .flags = IORESOURCE_MEM,
199                 .start = 0x03400000,
200                 .end = 0x034001fff,
201         },
202 };
203
204 static struct physmap_flash_data physmap_flash_data = {
205         .width = 2,
206 };
207
208 static struct plat_cpmac_data cpmac_low_data = {
209         .reset_bit = 17,
210         .power_bit = 20,
211         .phy_mask = 0x80000000,
212 };
213
214 static struct plat_cpmac_data cpmac_high_data = {
215         .reset_bit = 21,
216         .power_bit = 22,
217         .phy_mask = 0x7fffffff,
218 };
219
220 static struct plat_vlynq_data vlynq_low_data = {
221         .ops.on = vlynq_on,
222         .ops.off = vlynq_off,
223         .reset_bit = 20,
224         .gpio_bit = 18,
225 };
226
227 static struct plat_vlynq_data vlynq_high_data = {
228         .ops.on = vlynq_on,
229         .ops.off = vlynq_off,
230         .reset_bit = 16,
231         .gpio_bit = 19,
232 };
233
234 static struct platform_device physmap_flash = {
235         .id = 0,
236         .name = "physmap-flash",
237         .dev.platform_data = &physmap_flash_data,
238         .resource = &physmap_flash_resource,
239         .num_resources = 1,
240 };
241
242 static u64 cpmac_dma_mask = DMA_32BIT_MASK;
243 static struct platform_device cpmac_low = {
244         .id = 0,
245         .name = "cpmac",
246         .dev = {
247                 .dma_mask = &cpmac_dma_mask,
248                 .coherent_dma_mask = DMA_32BIT_MASK,
249                 .platform_data = &cpmac_low_data,
250         },
251         .resource = cpmac_low_res,
252         .num_resources = ARRAY_SIZE(cpmac_low_res),
253 };
254
255 static struct platform_device cpmac_high = {
256         .id = 1,
257         .name = "cpmac",
258         .dev = {
259                 .dma_mask = &cpmac_dma_mask,
260                 .coherent_dma_mask = DMA_32BIT_MASK,
261                 .platform_data = &cpmac_high_data,
262         },
263         .resource = cpmac_high_res,
264         .num_resources = ARRAY_SIZE(cpmac_high_res),
265 };
266
267 static struct platform_device vlynq_low = {
268         .id = 0,
269         .name = "vlynq",
270         .dev.platform_data = &vlynq_low_data,
271         .resource = vlynq_low_res,
272         .num_resources = ARRAY_SIZE(vlynq_low_res),
273 };
274
275 static struct platform_device vlynq_high = {
276         .id = 1,
277         .name = "vlynq",
278         .dev.platform_data = &vlynq_high_data,
279         .resource = vlynq_high_res,
280         .num_resources = ARRAY_SIZE(vlynq_high_res),
281 };
282
283
284 /* This is proper way to define uart ports, but they are then detected
285  * as xscale and, obviously, don't work...
286  */
287 #if !defined(CONFIG_SERIAL_8250)
288
289 static struct plat_serial8250_port uart0_data = {
290         .mapbase = AR7_REGS_UART0,
291         .irq = AR7_IRQ_UART0,
292         .regshift = 2,
293         .iotype = UPIO_MEM,
294         .flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP,
295 };
296
297 static struct plat_serial8250_port uart1_data = {
298         .mapbase = UR8_REGS_UART1,
299         .irq = AR7_IRQ_UART1,
300         .regshift = 2,
301         .iotype = UPIO_MEM,
302         .flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP,
303 };
304
305 static struct plat_serial8250_port uart_data[] = {
306         uart0_data,
307         uart1_data,
308         { .flags = 0 }
309 };
310
311 static struct plat_serial8250_port uart_data_single[] = {
312         uart0_data,
313         { .flags = 0 }
314 };
315
316 static struct platform_device uart = {
317         .id = 0,
318         .name = "serial8250",
319         .dev.platform_data = uart_data_single
320 };
321 #endif
322
323 static struct gpio_led default_leds[] = {
324         { .name = "status", .gpio = 8, .active_low = 1, },
325 };
326
327 static struct gpio_led dsl502t_leds[] = {
328         { .name = "status", .gpio = 9, .active_low = 1, },
329         { .name = "ethernet", .gpio = 7, .active_low = 1, },
330         { .name = "usb", .gpio = 12, .active_low = 1, },
331 };
332
333 static struct gpio_led dg834g_leds[] = {
334         { .name = "ppp", .gpio = 6, .active_low = 1, },
335         { .name = "status", .gpio = 7, .active_low = 1, },
336         { .name = "adsl", .gpio = 8, .active_low = 1, },
337         { .name = "wifi", .gpio = 12, .active_low = 1, },
338         { .name = "power", .gpio = 14, .active_low = 1, .default_trigger = "default-on", },
339 };
340
341 static struct gpio_led fb_sl_leds[] = {
342         { .name = "1", .gpio = 7, },
343         { .name = "2", .gpio = 13, .active_low = 1, },
344         { .name = "3", .gpio = 10, .active_low = 1, },
345         { .name = "4", .gpio = 12, .active_low = 1, },
346         { .name = "5", .gpio = 9, .active_low = 1, },
347 };
348
349 static struct gpio_led fb_fon_leds[] = {
350         { .name = "1", .gpio = 8, },
351         { .name = "2", .gpio = 3, .active_low = 1, },
352         { .name = "3", .gpio = 5, },
353         { .name = "4", .gpio = 4, .active_low = 1, },
354         { .name = "5", .gpio = 11, .active_low = 1, },
355 };
356
357 static struct gpio_led_platform_data ar7_led_data;
358
359 static struct platform_device ar7_gpio_leds = {
360         .name = "leds-gpio",
361         .id = -1,
362         .dev = {
363                 .platform_data = &ar7_led_data,
364         }
365 };
366
367 static struct platform_device ar7_udc = {
368         .id = -1,
369         .name = "ar7_udc",
370         .resource = usb_res,
371         .num_resources = ARRAY_SIZE(usb_res),
372 };
373
374 static inline unsigned char char2hex(char h)
375 {
376         switch (h) {
377         case '0': case '1': case '2': case '3': case '4':
378         case '5': case '6': case '7': case '8': case '9':
379                 return h - '0';
380         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
381                 return h - 'A' + 10;
382         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
383                 return h - 'a' + 10;
384         default:
385                 return 0;
386         }
387 }
388
389 static void cpmac_get_mac(int instance, unsigned char *dev_addr)
390 {
391         int i;
392         char name[5], default_mac[] = "00:00:00:12:34:56", *mac;
393
394         mac = NULL;
395         sprintf(name, "mac%c", 'a' + instance);
396         mac = prom_getenv(name);
397         if (!mac) {
398                 sprintf(name, "mac%c", 'a');
399                 mac = prom_getenv(name);
400         }
401         if (!mac)
402                 mac = default_mac;
403         for (i = 0; i < 6; i++)
404                 dev_addr[i] = (char2hex(mac[i * 3]) << 4) +
405                         char2hex(mac[i * 3 + 1]);
406 }
407
408 static void __init detect_leds(void)
409 {
410         char *prId, *usb_prod;
411
412         /* Default LEDs */
413         ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
414         ar7_led_data.leds = default_leds;
415
416         /* FIXME: the whole thing is unreliable */
417         prId = prom_getenv("ProductID");
418         usb_prod = prom_getenv("usb_prod");
419         
420         /* If we can't get the product id from PROM, use the default LEDs */
421         if (!prId)
422                 return;
423
424         if (strstr(prId, "Fritz_Box_FON")) {
425                 ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
426                 ar7_led_data.leds = fb_fon_leds;
427         } else if (strstr(prId, "Fritz_Box_")) {
428                 ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
429                 ar7_led_data.leds = fb_sl_leds;
430         } else if ((!strcmp(prId, "AR7RD") || !strcmp(prId, "AR7DB")) && usb_prod != NULL && strstr(usb_prod, "DSL-502T")) {
431                 ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds);
432                 ar7_led_data.leds = dsl502t_leds;
433         } else if (strstr(prId, "DG834")) {
434                 ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds);
435                 ar7_led_data.leds = dg834g_leds;
436         }
437 }
438
439 static int __init ar7_register_devices(void)
440 {
441         int res;
442
443 #ifdef CONFIG_SERIAL_8250
444
445         static struct uart_port uart_port[2];
446
447         memset(uart_port, 0, sizeof(struct uart_port) * 2);
448
449         uart_port[0].type = PORT_AR7;
450         uart_port[0].line = 0;
451         uart_port[0].irq = AR7_IRQ_UART0;
452         uart_port[0].uartclk = ar7_bus_freq() / 2;
453         uart_port[0].iotype = UPIO_MEM;
454         uart_port[0].mapbase = AR7_REGS_UART0;
455         uart_port[0].membase = ioremap(uart_port[0].mapbase, 256);
456         uart_port[0].regshift = 2;
457         uart_port[0].flags = UPF_IOREMAP;
458         res = early_serial_setup(&uart_port[0]);
459         if (res)
460                 return res;
461
462
463         /* Only TNETD73xx have a second serial port */
464         if (ar7_has_second_uart()) {
465                 uart_port[1].type = PORT_AR7;
466                 uart_port[1].line = 1;
467                 uart_port[1].irq = AR7_IRQ_UART1;
468                 uart_port[1].uartclk = ar7_bus_freq() / 2;
469                 uart_port[1].iotype = UPIO_MEM;
470                 uart_port[1].mapbase = UR8_REGS_UART1;
471                 uart_port[1].membase = ioremap(uart_port[1].mapbase, 256);
472                 uart_port[1].regshift = 2;
473                 uart_port[1].flags = UPF_IOREMAP;
474                 res = early_serial_setup(&uart_port[1]);
475                 if (res)
476                         return res;
477         }
478
479 #else /* !CONFIG_SERIAL_8250 */
480
481         uart_data[0].uartclk = ar7_bus_freq() / 2;
482         uart_data[1].uartclk = uart_data[0].uartclk;
483
484         /* Only TNETD73xx have a second serial port */
485         if (ar7_has_second_uart())
486                 uart.dev.platform_data = uart_data;
487
488         res = platform_device_register(&uart);
489         if (res)
490                 return res;
491
492 #endif /* CONFIG_SERIAL_8250 */
493
494         res = platform_device_register(&physmap_flash);
495         if (res)
496                 return res;
497
498         ar7_device_disable(vlynq_low_data.reset_bit);
499         res = platform_device_register(&vlynq_low);
500         if (res)
501                 return res;
502
503         if (ar7_has_high_vlynq()) {
504                 ar7_device_disable(vlynq_high_data.reset_bit);
505                 res = platform_device_register(&vlynq_high);
506                 if (res)
507                         return res;
508         }
509
510         if (ar7_has_high_cpmac()) {
511                 cpmac_get_mac(1, cpmac_high_data.dev_addr);
512                 res = platform_device_register(&cpmac_high);
513                 if (res)
514                         return res;
515         } else {
516                 cpmac_low_data.phy_mask = 0xffffffff;
517         }
518
519         cpmac_get_mac(0, cpmac_low_data.dev_addr);
520         res = platform_device_register(&cpmac_low);
521         if (res)
522                 return res;
523
524         detect_leds();
525         res = platform_device_register(&ar7_gpio_leds);
526         if (res)
527                 return res;
528
529         res = platform_device_register(&ar7_udc);
530
531         return res;
532 }
533
534
535 arch_initcall(ar7_register_devices);