Full support for Ginger Console
[linux-ginger.git] / arch / mips / ar7 / platform.c
blobe2278c04459d184625f58a9f939a205c08913d81
1 /*
2 * Copyright (C) 2006,2007 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2006,2007 Eugene Konev <ejka@openwrt.org>
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.
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.
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
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/vlynq.h>
32 #include <linux/leds.h>
33 #include <linux/string.h>
34 #include <linux/etherdevice.h>
35 #include <linux/phy.h>
36 #include <linux/phy_fixed.h>
38 #include <asm/addrspace.h>
39 #include <asm/mach-ar7/ar7.h>
40 #include <asm/mach-ar7/gpio.h>
41 #include <asm/mach-ar7/prom.h>
43 struct plat_vlynq_data {
44 struct plat_vlynq_ops ops;
45 int gpio_bit;
46 int reset_bit;
50 static int vlynq_on(struct vlynq_device *dev)
52 int result;
53 struct plat_vlynq_data *pdata = dev->dev.platform_data;
55 result = gpio_request(pdata->gpio_bit, "vlynq");
56 if (result)
57 goto out;
59 ar7_device_reset(pdata->reset_bit);
61 result = ar7_gpio_disable(pdata->gpio_bit);
62 if (result)
63 goto out_enabled;
65 result = ar7_gpio_enable(pdata->gpio_bit);
66 if (result)
67 goto out_enabled;
69 result = gpio_direction_output(pdata->gpio_bit, 0);
70 if (result)
71 goto out_gpio_enabled;
73 msleep(50);
75 gpio_set_value(pdata->gpio_bit, 1);
76 msleep(50);
78 return 0;
80 out_gpio_enabled:
81 ar7_gpio_disable(pdata->gpio_bit);
82 out_enabled:
83 ar7_device_disable(pdata->reset_bit);
84 gpio_free(pdata->gpio_bit);
85 out:
86 return result;
89 static void vlynq_off(struct vlynq_device *dev)
91 struct plat_vlynq_data *pdata = dev->dev.platform_data;
92 ar7_gpio_disable(pdata->gpio_bit);
93 gpio_free(pdata->gpio_bit);
94 ar7_device_disable(pdata->reset_bit);
97 static struct resource physmap_flash_resource = {
98 .name = "mem",
99 .flags = IORESOURCE_MEM,
100 .start = 0x10000000,
101 .end = 0x107fffff,
104 static struct resource cpmac_low_res[] = {
106 .name = "regs",
107 .flags = IORESOURCE_MEM,
108 .start = AR7_REGS_MAC0,
109 .end = AR7_REGS_MAC0 + 0x7ff,
112 .name = "irq",
113 .flags = IORESOURCE_IRQ,
114 .start = 27,
115 .end = 27,
119 static struct resource cpmac_high_res[] = {
121 .name = "regs",
122 .flags = IORESOURCE_MEM,
123 .start = AR7_REGS_MAC1,
124 .end = AR7_REGS_MAC1 + 0x7ff,
127 .name = "irq",
128 .flags = IORESOURCE_IRQ,
129 .start = 41,
130 .end = 41,
134 static struct resource vlynq_low_res[] = {
136 .name = "regs",
137 .flags = IORESOURCE_MEM,
138 .start = AR7_REGS_VLYNQ0,
139 .end = AR7_REGS_VLYNQ0 + 0xff,
142 .name = "irq",
143 .flags = IORESOURCE_IRQ,
144 .start = 29,
145 .end = 29,
148 .name = "mem",
149 .flags = IORESOURCE_MEM,
150 .start = 0x04000000,
151 .end = 0x04ffffff,
154 .name = "devirq",
155 .flags = IORESOURCE_IRQ,
156 .start = 80,
157 .end = 111,
161 static struct resource vlynq_high_res[] = {
163 .name = "regs",
164 .flags = IORESOURCE_MEM,
165 .start = AR7_REGS_VLYNQ1,
166 .end = AR7_REGS_VLYNQ1 + 0xff,
169 .name = "irq",
170 .flags = IORESOURCE_IRQ,
171 .start = 33,
172 .end = 33,
175 .name = "mem",
176 .flags = IORESOURCE_MEM,
177 .start = 0x0c000000,
178 .end = 0x0cffffff,
181 .name = "devirq",
182 .flags = IORESOURCE_IRQ,
183 .start = 112,
184 .end = 143,
188 static struct resource usb_res[] = {
190 .name = "regs",
191 .flags = IORESOURCE_MEM,
192 .start = AR7_REGS_USB,
193 .end = AR7_REGS_USB + 0xff,
196 .name = "irq",
197 .flags = IORESOURCE_IRQ,
198 .start = 32,
199 .end = 32,
202 .name = "mem",
203 .flags = IORESOURCE_MEM,
204 .start = 0x03400000,
205 .end = 0x034001fff,
209 static struct physmap_flash_data physmap_flash_data = {
210 .width = 2,
213 static struct fixed_phy_status fixed_phy_status __initdata = {
214 .link = 1,
215 .speed = 100,
216 .duplex = 1,
219 static struct plat_cpmac_data cpmac_low_data = {
220 .reset_bit = 17,
221 .power_bit = 20,
222 .phy_mask = 0x80000000,
225 static struct plat_cpmac_data cpmac_high_data = {
226 .reset_bit = 21,
227 .power_bit = 22,
228 .phy_mask = 0x7fffffff,
231 static struct plat_vlynq_data vlynq_low_data = {
232 .ops.on = vlynq_on,
233 .ops.off = vlynq_off,
234 .reset_bit = 20,
235 .gpio_bit = 18,
238 static struct plat_vlynq_data vlynq_high_data = {
239 .ops.on = vlynq_on,
240 .ops.off = vlynq_off,
241 .reset_bit = 16,
242 .gpio_bit = 19,
245 static struct platform_device physmap_flash = {
246 .id = 0,
247 .name = "physmap-flash",
248 .dev.platform_data = &physmap_flash_data,
249 .resource = &physmap_flash_resource,
250 .num_resources = 1,
253 static u64 cpmac_dma_mask = DMA_BIT_MASK(32);
254 static struct platform_device cpmac_low = {
255 .id = 0,
256 .name = "cpmac",
257 .dev = {
258 .dma_mask = &cpmac_dma_mask,
259 .coherent_dma_mask = DMA_BIT_MASK(32),
260 .platform_data = &cpmac_low_data,
262 .resource = cpmac_low_res,
263 .num_resources = ARRAY_SIZE(cpmac_low_res),
266 static struct platform_device cpmac_high = {
267 .id = 1,
268 .name = "cpmac",
269 .dev = {
270 .dma_mask = &cpmac_dma_mask,
271 .coherent_dma_mask = DMA_BIT_MASK(32),
272 .platform_data = &cpmac_high_data,
274 .resource = cpmac_high_res,
275 .num_resources = ARRAY_SIZE(cpmac_high_res),
278 static struct platform_device vlynq_low = {
279 .id = 0,
280 .name = "vlynq",
281 .dev.platform_data = &vlynq_low_data,
282 .resource = vlynq_low_res,
283 .num_resources = ARRAY_SIZE(vlynq_low_res),
286 static struct platform_device vlynq_high = {
287 .id = 1,
288 .name = "vlynq",
289 .dev.platform_data = &vlynq_high_data,
290 .resource = vlynq_high_res,
291 .num_resources = ARRAY_SIZE(vlynq_high_res),
295 static struct gpio_led default_leds[] = {
297 .name = "status",
298 .gpio = 8,
299 .active_low = 1,
303 static struct gpio_led dsl502t_leds[] = {
305 .name = "status",
306 .gpio = 9,
307 .active_low = 1,
310 .name = "ethernet",
311 .gpio = 7,
312 .active_low = 1,
315 .name = "usb",
316 .gpio = 12,
317 .active_low = 1,
321 static struct gpio_led dg834g_leds[] = {
323 .name = "ppp",
324 .gpio = 6,
325 .active_low = 1,
328 .name = "status",
329 .gpio = 7,
330 .active_low = 1,
333 .name = "adsl",
334 .gpio = 8,
335 .active_low = 1,
338 .name = "wifi",
339 .gpio = 12,
340 .active_low = 1,
343 .name = "power",
344 .gpio = 14,
345 .active_low = 1,
346 .default_trigger = "default-on",
350 static struct gpio_led fb_sl_leds[] = {
352 .name = "1",
353 .gpio = 7,
356 .name = "2",
357 .gpio = 13,
358 .active_low = 1,
361 .name = "3",
362 .gpio = 10,
363 .active_low = 1,
366 .name = "4",
367 .gpio = 12,
368 .active_low = 1,
371 .name = "5",
372 .gpio = 9,
373 .active_low = 1,
377 static struct gpio_led fb_fon_leds[] = {
379 .name = "1",
380 .gpio = 8,
383 .name = "2",
384 .gpio = 3,
385 .active_low = 1,
388 .name = "3",
389 .gpio = 5,
392 .name = "4",
393 .gpio = 4,
394 .active_low = 1,
397 .name = "5",
398 .gpio = 11,
399 .active_low = 1,
403 static struct gpio_led_platform_data ar7_led_data;
405 static struct platform_device ar7_gpio_leds = {
406 .name = "leds-gpio",
407 .id = -1,
408 .dev = {
409 .platform_data = &ar7_led_data,
413 static struct platform_device ar7_udc = {
414 .id = -1,
415 .name = "ar7_udc",
416 .resource = usb_res,
417 .num_resources = ARRAY_SIZE(usb_res),
420 static struct resource ar7_wdt_res = {
421 .name = "regs",
422 .start = -1, /* Filled at runtime */
423 .end = -1, /* Filled at runtime */
424 .flags = IORESOURCE_MEM,
427 static struct platform_device ar7_wdt = {
428 .id = -1,
429 .name = "ar7_wdt",
430 .resource = &ar7_wdt_res,
431 .num_resources = 1,
434 static inline unsigned char char2hex(char h)
436 switch (h) {
437 case '0': case '1': case '2': case '3': case '4':
438 case '5': case '6': case '7': case '8': case '9':
439 return h - '0';
440 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
441 return h - 'A' + 10;
442 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
443 return h - 'a' + 10;
444 default:
445 return 0;
449 static void cpmac_get_mac(int instance, unsigned char *dev_addr)
451 int i;
452 char name[5], default_mac[ETH_ALEN], *mac;
454 mac = NULL;
455 sprintf(name, "mac%c", 'a' + instance);
456 mac = prom_getenv(name);
457 if (!mac) {
458 sprintf(name, "mac%c", 'a');
459 mac = prom_getenv(name);
461 if (!mac) {
462 random_ether_addr(default_mac);
463 mac = default_mac;
465 for (i = 0; i < 6; i++)
466 dev_addr[i] = (char2hex(mac[i * 3]) << 4) +
467 char2hex(mac[i * 3 + 1]);
470 static void __init detect_leds(void)
472 char *prid, *usb_prod;
474 /* Default LEDs */
475 ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
476 ar7_led_data.leds = default_leds;
478 /* FIXME: the whole thing is unreliable */
479 prid = prom_getenv("ProductID");
480 usb_prod = prom_getenv("usb_prod");
482 /* If we can't get the product id from PROM, use the default LEDs */
483 if (!prid)
484 return;
486 if (strstr(prid, "Fritz_Box_FON")) {
487 ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
488 ar7_led_data.leds = fb_fon_leds;
489 } else if (strstr(prid, "Fritz_Box_")) {
490 ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
491 ar7_led_data.leds = fb_sl_leds;
492 } else if ((!strcmp(prid, "AR7RD") || !strcmp(prid, "AR7DB"))
493 && usb_prod != NULL && strstr(usb_prod, "DSL-502T")) {
494 ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds);
495 ar7_led_data.leds = dsl502t_leds;
496 } else if (strstr(prid, "DG834")) {
497 ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds);
498 ar7_led_data.leds = dg834g_leds;
502 static int __init ar7_register_devices(void)
504 u16 chip_id;
505 int res;
506 #ifdef CONFIG_SERIAL_8250
507 static struct uart_port uart_port[2];
509 memset(uart_port, 0, sizeof(struct uart_port) * 2);
511 uart_port[0].type = PORT_16550A;
512 uart_port[0].line = 0;
513 uart_port[0].irq = AR7_IRQ_UART0;
514 uart_port[0].uartclk = ar7_bus_freq() / 2;
515 uart_port[0].iotype = UPIO_MEM32;
516 uart_port[0].mapbase = AR7_REGS_UART0;
517 uart_port[0].membase = ioremap(uart_port[0].mapbase, 256);
518 uart_port[0].regshift = 2;
519 res = early_serial_setup(&uart_port[0]);
520 if (res)
521 return res;
524 /* Only TNETD73xx have a second serial port */
525 if (ar7_has_second_uart()) {
526 uart_port[1].type = PORT_16550A;
527 uart_port[1].line = 1;
528 uart_port[1].irq = AR7_IRQ_UART1;
529 uart_port[1].uartclk = ar7_bus_freq() / 2;
530 uart_port[1].iotype = UPIO_MEM32;
531 uart_port[1].mapbase = UR8_REGS_UART1;
532 uart_port[1].membase = ioremap(uart_port[1].mapbase, 256);
533 uart_port[1].regshift = 2;
534 res = early_serial_setup(&uart_port[1]);
535 if (res)
536 return res;
538 #endif /* CONFIG_SERIAL_8250 */
539 res = platform_device_register(&physmap_flash);
540 if (res)
541 return res;
543 ar7_device_disable(vlynq_low_data.reset_bit);
544 res = platform_device_register(&vlynq_low);
545 if (res)
546 return res;
548 if (ar7_has_high_vlynq()) {
549 ar7_device_disable(vlynq_high_data.reset_bit);
550 res = platform_device_register(&vlynq_high);
551 if (res)
552 return res;
555 if (ar7_has_high_cpmac()) {
556 res = fixed_phy_add(PHY_POLL, cpmac_high.id, &fixed_phy_status);
557 if (res && res != -ENODEV)
558 return res;
559 cpmac_get_mac(1, cpmac_high_data.dev_addr);
560 res = platform_device_register(&cpmac_high);
561 if (res)
562 return res;
563 } else {
564 cpmac_low_data.phy_mask = 0xffffffff;
567 res = fixed_phy_add(PHY_POLL, cpmac_low.id, &fixed_phy_status);
568 if (res && res != -ENODEV)
569 return res;
571 cpmac_get_mac(0, cpmac_low_data.dev_addr);
572 res = platform_device_register(&cpmac_low);
573 if (res)
574 return res;
576 detect_leds();
577 res = platform_device_register(&ar7_gpio_leds);
578 if (res)
579 return res;
581 res = platform_device_register(&ar7_udc);
583 chip_id = ar7_chip_id();
584 switch (chip_id) {
585 case AR7_CHIP_7100:
586 case AR7_CHIP_7200:
587 ar7_wdt_res.start = AR7_REGS_WDT;
588 break;
589 case AR7_CHIP_7300:
590 ar7_wdt_res.start = UR8_REGS_WDT;
591 break;
592 default:
593 break;
596 ar7_wdt_res.end = ar7_wdt_res.start + 0x20;
598 res = platform_device_register(&ar7_wdt);
600 return res;
602 arch_initcall(ar7_register_devices);