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>
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>
37 #include <linux/gpio.h>
39 #include <asm/addrspace.h>
40 #include <asm/mach-ar7/ar7.h>
41 #include <asm/mach-ar7/gpio.h>
42 #include <asm/mach-ar7/prom.h>
44 struct plat_vlynq_data {
45 struct plat_vlynq_ops ops;
51 static int vlynq_on(struct vlynq_device *dev)
54 struct plat_vlynq_data *pdata = dev->dev.platform_data;
56 result = gpio_request(pdata->gpio_bit, "vlynq");
60 ar7_device_reset(pdata->reset_bit);
62 result = ar7_gpio_disable(pdata->gpio_bit);
66 result = ar7_gpio_enable(pdata->gpio_bit);
70 result = gpio_direction_output(pdata->gpio_bit, 0);
72 goto out_gpio_enabled;
76 gpio_set_value(pdata->gpio_bit, 1);
82 ar7_gpio_disable(pdata->gpio_bit);
84 ar7_device_disable(pdata->reset_bit);
85 gpio_free(pdata->gpio_bit);
90 static void vlynq_off(struct vlynq_device *dev)
92 struct plat_vlynq_data *pdata = dev->dev.platform_data;
93 ar7_gpio_disable(pdata->gpio_bit);
94 gpio_free(pdata->gpio_bit);
95 ar7_device_disable(pdata->reset_bit);
98 static struct resource physmap_flash_resource = {
100 .flags = IORESOURCE_MEM,
105 static struct resource cpmac_low_res[] = {
108 .flags = IORESOURCE_MEM,
109 .start = AR7_REGS_MAC0,
110 .end = AR7_REGS_MAC0 + 0x7ff,
114 .flags = IORESOURCE_IRQ,
120 static struct resource cpmac_high_res[] = {
123 .flags = IORESOURCE_MEM,
124 .start = AR7_REGS_MAC1,
125 .end = AR7_REGS_MAC1 + 0x7ff,
129 .flags = IORESOURCE_IRQ,
135 static struct resource vlynq_low_res[] = {
138 .flags = IORESOURCE_MEM,
139 .start = AR7_REGS_VLYNQ0,
140 .end = AR7_REGS_VLYNQ0 + 0xff,
144 .flags = IORESOURCE_IRQ,
150 .flags = IORESOURCE_MEM,
156 .flags = IORESOURCE_IRQ,
162 static struct resource vlynq_high_res[] = {
165 .flags = IORESOURCE_MEM,
166 .start = AR7_REGS_VLYNQ1,
167 .end = AR7_REGS_VLYNQ1 + 0xff,
171 .flags = IORESOURCE_IRQ,
177 .flags = IORESOURCE_MEM,
183 .flags = IORESOURCE_IRQ,
189 static struct resource usb_res[] = {
192 .flags = IORESOURCE_MEM,
193 .start = AR7_REGS_USB,
194 .end = AR7_REGS_USB + 0xff,
198 .flags = IORESOURCE_IRQ,
204 .flags = IORESOURCE_MEM,
210 static struct physmap_flash_data physmap_flash_data = {
214 static struct fixed_phy_status fixed_phy_status __initdata = {
220 static struct plat_cpmac_data cpmac_low_data = {
223 .phy_mask = 0x80000000,
226 static struct plat_cpmac_data cpmac_high_data = {
229 .phy_mask = 0x7fffffff,
232 static struct plat_vlynq_data vlynq_low_data = {
234 .ops.off = vlynq_off,
239 static struct plat_vlynq_data vlynq_high_data = {
241 .ops.off = vlynq_off,
246 static struct platform_device physmap_flash = {
248 .name = "physmap-flash",
249 .dev.platform_data = &physmap_flash_data,
250 .resource = &physmap_flash_resource,
254 static u64 cpmac_dma_mask = DMA_BIT_MASK(32);
255 static struct platform_device cpmac_low = {
259 .dma_mask = &cpmac_dma_mask,
260 .coherent_dma_mask = DMA_BIT_MASK(32),
261 .platform_data = &cpmac_low_data,
263 .resource = cpmac_low_res,
264 .num_resources = ARRAY_SIZE(cpmac_low_res),
267 static struct platform_device cpmac_high = {
271 .dma_mask = &cpmac_dma_mask,
272 .coherent_dma_mask = DMA_BIT_MASK(32),
273 .platform_data = &cpmac_high_data,
275 .resource = cpmac_high_res,
276 .num_resources = ARRAY_SIZE(cpmac_high_res),
279 static struct platform_device vlynq_low = {
282 .dev.platform_data = &vlynq_low_data,
283 .resource = vlynq_low_res,
284 .num_resources = ARRAY_SIZE(vlynq_low_res),
287 static struct platform_device vlynq_high = {
290 .dev.platform_data = &vlynq_high_data,
291 .resource = vlynq_high_res,
292 .num_resources = ARRAY_SIZE(vlynq_high_res),
296 static struct gpio_led default_leds[] = {
304 static struct gpio_led dsl502t_leds[] = {
322 static struct gpio_led dg834g_leds[] = {
347 .default_trigger = "default-on",
351 static struct gpio_led fb_sl_leds[] = {
378 static struct gpio_led fb_fon_leds[] = {
404 static struct gpio_led_platform_data ar7_led_data;
406 static struct platform_device ar7_gpio_leds = {
410 .platform_data = &ar7_led_data,
414 static struct platform_device ar7_udc = {
418 .num_resources = ARRAY_SIZE(usb_res),
421 static struct resource ar7_wdt_res = {
423 .start = -1, /* Filled at runtime */
424 .end = -1, /* Filled at runtime */
425 .flags = IORESOURCE_MEM,
428 static struct platform_device ar7_wdt = {
431 .resource = &ar7_wdt_res,
435 static inline unsigned char char2hex(char h)
438 case '0': case '1': case '2': case '3': case '4':
439 case '5': case '6': case '7': case '8': case '9':
441 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
443 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
450 static void cpmac_get_mac(int instance, unsigned char *dev_addr)
453 char name[5], default_mac[ETH_ALEN], *mac;
456 sprintf(name, "mac%c", 'a' + instance);
457 mac = prom_getenv(name);
459 sprintf(name, "mac%c", 'a');
460 mac = prom_getenv(name);
463 random_ether_addr(default_mac);
466 for (i = 0; i < 6; i++)
467 dev_addr[i] = (char2hex(mac[i * 3]) << 4) +
468 char2hex(mac[i * 3 + 1]);
471 static void __init detect_leds(void)
473 char *prid, *usb_prod;
476 ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
477 ar7_led_data.leds = default_leds;
479 /* FIXME: the whole thing is unreliable */
480 prid = prom_getenv("ProductID");
481 usb_prod = prom_getenv("usb_prod");
483 /* If we can't get the product id from PROM, use the default LEDs */
487 if (strstr(prid, "Fritz_Box_FON")) {
488 ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
489 ar7_led_data.leds = fb_fon_leds;
490 } else if (strstr(prid, "Fritz_Box_")) {
491 ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
492 ar7_led_data.leds = fb_sl_leds;
493 } else if ((!strcmp(prid, "AR7RD") || !strcmp(prid, "AR7DB"))
494 && usb_prod != NULL && strstr(usb_prod, "DSL-502T")) {
495 ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds);
496 ar7_led_data.leds = dsl502t_leds;
497 } else if (strstr(prid, "DG834")) {
498 ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds);
499 ar7_led_data.leds = dg834g_leds;
503 static int __init ar7_register_devices(void)
508 #ifdef CONFIG_SERIAL_8250
509 static struct uart_port uart_port[2] __initdata;
511 memset(uart_port, 0, sizeof(struct uart_port) * 2);
513 uart_port[0].type = PORT_16550A;
514 uart_port[0].line = 0;
515 uart_port[0].irq = AR7_IRQ_UART0;
516 uart_port[0].uartclk = ar7_bus_freq() / 2;
517 uart_port[0].iotype = UPIO_MEM32;
518 uart_port[0].mapbase = AR7_REGS_UART0;
519 uart_port[0].membase = ioremap(uart_port[0].mapbase, 256);
520 uart_port[0].regshift = 2;
521 res = early_serial_setup(&uart_port[0]);
526 /* Only TNETD73xx have a second serial port */
527 if (ar7_has_second_uart()) {
528 uart_port[1].type = PORT_16550A;
529 uart_port[1].line = 1;
530 uart_port[1].irq = AR7_IRQ_UART1;
531 uart_port[1].uartclk = ar7_bus_freq() / 2;
532 uart_port[1].iotype = UPIO_MEM32;
533 uart_port[1].mapbase = UR8_REGS_UART1;
534 uart_port[1].membase = ioremap(uart_port[1].mapbase, 256);
535 uart_port[1].regshift = 2;
536 res = early_serial_setup(&uart_port[1]);
540 #endif /* CONFIG_SERIAL_8250 */
541 res = platform_device_register(&physmap_flash);
545 ar7_device_disable(vlynq_low_data.reset_bit);
546 res = platform_device_register(&vlynq_low);
550 if (ar7_has_high_vlynq()) {
551 ar7_device_disable(vlynq_high_data.reset_bit);
552 res = platform_device_register(&vlynq_high);
557 if (ar7_has_high_cpmac()) {
558 res = fixed_phy_add(PHY_POLL, cpmac_high.id, &fixed_phy_status);
559 if (res && res != -ENODEV)
561 cpmac_get_mac(1, cpmac_high_data.dev_addr);
562 res = platform_device_register(&cpmac_high);
566 cpmac_low_data.phy_mask = 0xffffffff;
569 res = fixed_phy_add(PHY_POLL, cpmac_low.id, &fixed_phy_status);
570 if (res && res != -ENODEV)
573 cpmac_get_mac(0, cpmac_low_data.dev_addr);
574 res = platform_device_register(&cpmac_low);
579 res = platform_device_register(&ar7_gpio_leds);
583 res = platform_device_register(&ar7_udc);
585 chip_id = ar7_chip_id();
589 ar7_wdt_res.start = AR7_REGS_WDT;
592 ar7_wdt_res.start = UR8_REGS_WDT;
598 ar7_wdt_res.end = ar7_wdt_res.start + 0x20;
600 bootcr = (u32 *)ioremap_nocache(AR7_REGS_DCL, 4);
604 /* Register watchdog only if enabled in hardware */
605 if (val & AR7_WDT_HW_ENA)
606 res = platform_device_register(&ar7_wdt);
610 arch_initcall(ar7_register_devices);