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>
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
;
50 static int vlynq_on(struct vlynq_device
*dev
)
53 struct plat_vlynq_data
*pdata
= dev
->dev
.platform_data
;
55 result
= gpio_request(pdata
->gpio_bit
, "vlynq");
59 ar7_device_reset(pdata
->reset_bit
);
61 result
= ar7_gpio_disable(pdata
->gpio_bit
);
65 result
= ar7_gpio_enable(pdata
->gpio_bit
);
69 result
= gpio_direction_output(pdata
->gpio_bit
, 0);
71 goto out_gpio_enabled
;
75 gpio_set_value(pdata
->gpio_bit
, 1);
81 ar7_gpio_disable(pdata
->gpio_bit
);
83 ar7_device_disable(pdata
->reset_bit
);
84 gpio_free(pdata
->gpio_bit
);
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
= {
99 .flags
= IORESOURCE_MEM
,
104 static struct resource cpmac_low_res
[] = {
107 .flags
= IORESOURCE_MEM
,
108 .start
= AR7_REGS_MAC0
,
109 .end
= AR7_REGS_MAC0
+ 0x7ff,
113 .flags
= IORESOURCE_IRQ
,
119 static struct resource cpmac_high_res
[] = {
122 .flags
= IORESOURCE_MEM
,
123 .start
= AR7_REGS_MAC1
,
124 .end
= AR7_REGS_MAC1
+ 0x7ff,
128 .flags
= IORESOURCE_IRQ
,
134 static struct resource vlynq_low_res
[] = {
137 .flags
= IORESOURCE_MEM
,
138 .start
= AR7_REGS_VLYNQ0
,
139 .end
= AR7_REGS_VLYNQ0
+ 0xff,
143 .flags
= IORESOURCE_IRQ
,
149 .flags
= IORESOURCE_MEM
,
155 .flags
= IORESOURCE_IRQ
,
161 static struct resource vlynq_high_res
[] = {
164 .flags
= IORESOURCE_MEM
,
165 .start
= AR7_REGS_VLYNQ1
,
166 .end
= AR7_REGS_VLYNQ1
+ 0xff,
170 .flags
= IORESOURCE_IRQ
,
176 .flags
= IORESOURCE_MEM
,
182 .flags
= IORESOURCE_IRQ
,
188 static struct resource usb_res
[] = {
191 .flags
= IORESOURCE_MEM
,
192 .start
= AR7_REGS_USB
,
193 .end
= AR7_REGS_USB
+ 0xff,
197 .flags
= IORESOURCE_IRQ
,
203 .flags
= IORESOURCE_MEM
,
209 static struct physmap_flash_data physmap_flash_data
= {
213 static struct fixed_phy_status fixed_phy_status __initdata
= {
219 static struct plat_cpmac_data cpmac_low_data
= {
222 .phy_mask
= 0x80000000,
225 static struct plat_cpmac_data cpmac_high_data
= {
228 .phy_mask
= 0x7fffffff,
231 static struct plat_vlynq_data vlynq_low_data
= {
233 .ops
.off
= vlynq_off
,
238 static struct plat_vlynq_data vlynq_high_data
= {
240 .ops
.off
= vlynq_off
,
245 static struct platform_device physmap_flash
= {
247 .name
= "physmap-flash",
248 .dev
.platform_data
= &physmap_flash_data
,
249 .resource
= &physmap_flash_resource
,
253 static u64 cpmac_dma_mask
= DMA_BIT_MASK(32);
254 static struct platform_device cpmac_low
= {
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
= {
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
= {
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
= {
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
[] = {
303 static struct gpio_led dsl502t_leds
[] = {
321 static struct gpio_led dg834g_leds
[] = {
346 .default_trigger
= "default-on",
350 static struct gpio_led fb_sl_leds
[] = {
377 static struct gpio_led fb_fon_leds
[] = {
403 static struct gpio_led_platform_data ar7_led_data
;
405 static struct platform_device ar7_gpio_leds
= {
409 .platform_data
= &ar7_led_data
,
413 static struct platform_device ar7_udc
= {
417 .num_resources
= ARRAY_SIZE(usb_res
),
420 static struct resource ar7_wdt_res
= {
422 .start
= -1, /* Filled at runtime */
423 .end
= -1, /* Filled at runtime */
424 .flags
= IORESOURCE_MEM
,
427 static struct platform_device ar7_wdt
= {
430 .resource
= &ar7_wdt_res
,
434 static inline unsigned char char2hex(char h
)
437 case '0': case '1': case '2': case '3': case '4':
438 case '5': case '6': case '7': case '8': case '9':
440 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
442 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
449 static void cpmac_get_mac(int instance
, unsigned char *dev_addr
)
452 char name
[5], default_mac
[ETH_ALEN
], *mac
;
455 sprintf(name
, "mac%c", 'a' + instance
);
456 mac
= prom_getenv(name
);
458 sprintf(name
, "mac%c", 'a');
459 mac
= prom_getenv(name
);
462 random_ether_addr(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
;
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 */
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)
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]);
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]);
538 #endif /* CONFIG_SERIAL_8250 */
539 res
= platform_device_register(&physmap_flash
);
543 ar7_device_disable(vlynq_low_data
.reset_bit
);
544 res
= platform_device_register(&vlynq_low
);
548 if (ar7_has_high_vlynq()) {
549 ar7_device_disable(vlynq_high_data
.reset_bit
);
550 res
= platform_device_register(&vlynq_high
);
555 if (ar7_has_high_cpmac()) {
556 res
= fixed_phy_add(PHY_POLL
, cpmac_high
.id
, &fixed_phy_status
);
557 if (res
&& res
!= -ENODEV
)
559 cpmac_get_mac(1, cpmac_high_data
.dev_addr
);
560 res
= platform_device_register(&cpmac_high
);
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
)
571 cpmac_get_mac(0, cpmac_low_data
.dev_addr
);
572 res
= platform_device_register(&cpmac_low
);
577 res
= platform_device_register(&ar7_gpio_leds
);
581 res
= platform_device_register(&ar7_udc
);
583 chip_id
= ar7_chip_id();
587 ar7_wdt_res
.start
= AR7_REGS_WDT
;
590 ar7_wdt_res
.start
= UR8_REGS_WDT
;
596 ar7_wdt_res
.end
= ar7_wdt_res
.start
+ 0x20;
598 res
= platform_device_register(&ar7_wdt
);
602 arch_initcall(ar7_register_devices
);