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