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