2 * Atheros AR71xx SoC platform devices
4 * Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Parts of this file are based on Atheros' 2.6.15 BSP
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/etherdevice.h>
19 #include <linux/platform_device.h>
20 #include <linux/serial_8250.h>
21 #include <linux/ath9k_platform.h>
23 #include <asm/mach-ar71xx/ar71xx.h>
27 static u8 ar71xx_mac_base[ETH_ALEN] __initdata;
30 * OHCI (USB full speed host controller)
32 static struct resource ar71xx_ohci_resources[] = {
34 .start = AR71XX_OHCI_BASE,
35 .end = AR71XX_OHCI_BASE + AR71XX_OHCI_SIZE - 1,
36 .flags = IORESOURCE_MEM,
39 .start = AR71XX_MISC_IRQ_OHCI,
40 .end = AR71XX_MISC_IRQ_OHCI,
41 .flags = IORESOURCE_IRQ,
45 static u64 ar71xx_ohci_dmamask = DMA_BIT_MASK(32);
46 static struct platform_device ar71xx_ohci_device = {
47 .name = "ar71xx-ohci",
49 .resource = ar71xx_ohci_resources,
50 .num_resources = ARRAY_SIZE(ar71xx_ohci_resources),
52 .dma_mask = &ar71xx_ohci_dmamask,
53 .coherent_dma_mask = DMA_BIT_MASK(32),
58 * EHCI (USB full speed host controller)
60 static struct resource ar71xx_ehci_resources[] = {
62 .start = AR71XX_EHCI_BASE,
63 .end = AR71XX_EHCI_BASE + AR71XX_EHCI_SIZE - 1,
64 .flags = IORESOURCE_MEM,
67 .start = AR71XX_CPU_IRQ_USB,
68 .end = AR71XX_CPU_IRQ_USB,
69 .flags = IORESOURCE_IRQ,
74 static u64 ar71xx_ehci_dmamask = DMA_BIT_MASK(32);
75 static struct ar71xx_ehci_platform_data ar71xx_ehci_data;
77 static struct platform_device ar71xx_ehci_device = {
78 .name = "ar71xx-ehci",
80 .resource = ar71xx_ehci_resources,
81 .num_resources = ARRAY_SIZE(ar71xx_ehci_resources),
83 .dma_mask = &ar71xx_ehci_dmamask,
84 .coherent_dma_mask = DMA_BIT_MASK(32),
85 .platform_data = &ar71xx_ehci_data,
89 #define AR71XX_USB_RESET_MASK \
90 (RESET_MODULE_USB_HOST | RESET_MODULE_USB_PHY \
91 | RESET_MODULE_USB_OHCI_DLL)
93 static void ar71xx_usb_setup(void)
95 ar71xx_device_stop(AR71XX_USB_RESET_MASK);
97 ar71xx_device_start(AR71XX_USB_RESET_MASK);
99 /* Turning on the Buff and Desc swap bits */
100 ar71xx_usb_ctrl_wr(USB_CTRL_REG_CONFIG, 0xf0000);
102 /* WAR for HW bug. Here it adjusts the duration between two SOFS */
103 ar71xx_usb_ctrl_wr(USB_CTRL_REG_FLADJ, 0x20c00);
108 static void ar91xx_usb_setup(void)
110 ar71xx_device_stop(RESET_MODULE_USBSUS_OVERRIDE);
113 ar71xx_device_start(RESET_MODULE_USB_HOST);
116 ar71xx_device_start(RESET_MODULE_USB_PHY);
120 void __init ar71xx_add_device_usb(void)
122 switch (ar71xx_soc) {
123 case AR71XX_SOC_AR7130:
124 case AR71XX_SOC_AR7141:
125 case AR71XX_SOC_AR7161:
127 platform_device_register(&ar71xx_ohci_device);
128 platform_device_register(&ar71xx_ehci_device);
131 case AR71XX_SOC_AR9130:
132 case AR71XX_SOC_AR9132:
134 ar71xx_ehci_data.is_ar91xx = 1;
135 platform_device_register(&ar71xx_ehci_device);
143 static struct resource ar71xx_uart_resources[] = {
145 .start = AR71XX_UART_BASE,
146 .end = AR71XX_UART_BASE + AR71XX_UART_SIZE - 1,
147 .flags = IORESOURCE_MEM,
151 #define AR71XX_UART_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_IOREMAP)
152 static struct plat_serial8250_port ar71xx_uart_data[] = {
154 .mapbase = AR71XX_UART_BASE,
155 .irq = AR71XX_MISC_IRQ_UART,
156 .flags = AR71XX_UART_FLAGS,
157 .iotype = UPIO_MEM32,
160 /* terminating entry */
164 static struct platform_device ar71xx_uart_device = {
165 .name = "serial8250",
166 .id = PLAT8250_DEV_PLATFORM,
167 .resource = ar71xx_uart_resources,
168 .num_resources = ARRAY_SIZE(ar71xx_uart_resources),
170 .platform_data = ar71xx_uart_data
174 void __init ar71xx_add_device_uart(void)
176 ar71xx_uart_data[0].uartclk = ar71xx_ahb_freq;
177 platform_device_register(&ar71xx_uart_device);
180 static struct resource ar71xx_mdio_resources[] = {
183 .flags = IORESOURCE_MEM,
184 .start = AR71XX_GE0_BASE,
185 .end = AR71XX_GE0_BASE + 0x200 - 1,
189 static struct ag71xx_mdio_platform_data ar71xx_mdio_data = {
190 .phy_mask = 0xffffffff,
193 static struct platform_device ar71xx_mdio_device = {
194 .name = "ag71xx-mdio",
196 .resource = ar71xx_mdio_resources,
197 .num_resources = ARRAY_SIZE(ar71xx_mdio_resources),
199 .platform_data = &ar71xx_mdio_data,
203 void __init ar71xx_add_device_mdio(u32 phy_mask)
205 ar71xx_mdio_data.phy_mask = phy_mask;
206 platform_device_register(&ar71xx_mdio_device);
209 static void ar71xx_set_pll(u32 cfg_reg, u32 pll_reg, u32 pll_val, u32 shift)
214 base = ioremap_nocache(AR71XX_PLL_BASE, AR71XX_PLL_SIZE);
216 t = __raw_readl(base + cfg_reg);
219 __raw_writel(t, base + cfg_reg);
222 __raw_writel(pll_val, base + pll_reg);
225 __raw_writel(t, base + cfg_reg);
229 __raw_writel(t, base + cfg_reg);
232 printk(KERN_DEBUG "ar71xx: pll_reg %#x: %#x\n",
233 (unsigned int)(base + pll_reg), __raw_readl(base + pll_reg));
238 struct ar71xx_eth_pll_data ar71xx_eth0_pll_data;
239 struct ar71xx_eth_pll_data ar71xx_eth1_pll_data;
241 static u32 ar71xx_get_eth_pll(unsigned int mac, int speed)
243 struct ar71xx_eth_pll_data *pll_data;
248 pll_data = &ar71xx_eth0_pll_data;
251 pll_data = &ar71xx_eth1_pll_data;
259 pll_val = pll_data->pll_10;
262 pll_val = pll_data->pll_100;
265 pll_val = pll_data->pll_1000;
274 static void ar71xx_set_pll_ge0(int speed)
276 u32 val = ar71xx_get_eth_pll(0, speed);
278 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG, AR71XX_PLL_REG_ETH0_INT_CLOCK,
279 val, AR71XX_ETH0_PLL_SHIFT);
282 static void ar71xx_set_pll_ge1(int speed)
284 u32 val = ar71xx_get_eth_pll(1, speed);
286 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG, AR71XX_PLL_REG_ETH1_INT_CLOCK,
287 val, AR71XX_ETH1_PLL_SHIFT);
290 static void ar724x_set_pll_ge0(int speed)
295 static void ar724x_set_pll_ge1(int speed)
300 static void ar91xx_set_pll_ge0(int speed)
302 u32 val = ar71xx_get_eth_pll(0, speed);
304 ar71xx_set_pll(AR91XX_PLL_REG_ETH_CONFIG, AR91XX_PLL_REG_ETH0_INT_CLOCK,
305 val, AR91XX_ETH0_PLL_SHIFT);
308 static void ar91xx_set_pll_ge1(int speed)
310 u32 val = ar71xx_get_eth_pll(1, speed);
312 ar71xx_set_pll(AR91XX_PLL_REG_ETH_CONFIG, AR91XX_PLL_REG_ETH1_INT_CLOCK,
313 val, AR91XX_ETH1_PLL_SHIFT);
316 static void ar71xx_ddr_flush_ge0(void)
318 ar71xx_ddr_flush(AR71XX_DDR_REG_FLUSH_GE0);
321 static void ar71xx_ddr_flush_ge1(void)
323 ar71xx_ddr_flush(AR71XX_DDR_REG_FLUSH_GE1);
326 static void ar724x_ddr_flush_ge0(void)
328 ar71xx_ddr_flush(AR724X_DDR_REG_FLUSH_GE0);
331 static void ar724x_ddr_flush_ge1(void)
333 ar71xx_ddr_flush(AR724X_DDR_REG_FLUSH_GE1);
336 static void ar91xx_ddr_flush_ge0(void)
338 ar71xx_ddr_flush(AR91XX_DDR_REG_FLUSH_GE0);
341 static void ar91xx_ddr_flush_ge1(void)
343 ar71xx_ddr_flush(AR91XX_DDR_REG_FLUSH_GE1);
346 static struct resource ar71xx_eth0_resources[] = {
349 .flags = IORESOURCE_MEM,
350 .start = AR71XX_GE0_BASE,
351 .end = AR71XX_GE0_BASE + 0x200 - 1,
354 .flags = IORESOURCE_MEM,
355 .start = AR71XX_MII_BASE + MII_REG_MII0_CTRL,
356 .end = AR71XX_MII_BASE + MII_REG_MII0_CTRL + 3,
359 .flags = IORESOURCE_IRQ,
360 .start = AR71XX_CPU_IRQ_GE0,
361 .end = AR71XX_CPU_IRQ_GE0,
365 struct ag71xx_platform_data ar71xx_eth0_data = {
366 .reset_bit = RESET_MODULE_GE0_MAC,
369 static struct platform_device ar71xx_eth0_device = {
372 .resource = ar71xx_eth0_resources,
373 .num_resources = ARRAY_SIZE(ar71xx_eth0_resources),
375 .platform_data = &ar71xx_eth0_data,
379 static struct resource ar71xx_eth1_resources[] = {
382 .flags = IORESOURCE_MEM,
383 .start = AR71XX_GE1_BASE,
384 .end = AR71XX_GE1_BASE + 0x200 - 1,
387 .flags = IORESOURCE_MEM,
388 .start = AR71XX_MII_BASE + MII_REG_MII1_CTRL,
389 .end = AR71XX_MII_BASE + MII_REG_MII1_CTRL + 3,
392 .flags = IORESOURCE_IRQ,
393 .start = AR71XX_CPU_IRQ_GE1,
394 .end = AR71XX_CPU_IRQ_GE1,
398 struct ag71xx_platform_data ar71xx_eth1_data = {
399 .reset_bit = RESET_MODULE_GE1_MAC,
402 static struct platform_device ar71xx_eth1_device = {
405 .resource = ar71xx_eth1_resources,
406 .num_resources = ARRAY_SIZE(ar71xx_eth1_resources),
408 .platform_data = &ar71xx_eth1_data,
412 #define AR71XX_PLL_VAL_1000 0x00110000
413 #define AR71XX_PLL_VAL_100 0x00001099
414 #define AR71XX_PLL_VAL_10 0x00991099
416 #define AR724X_PLL_VAL_1000 0x00110000
417 #define AR724X_PLL_VAL_100 0x00001099
418 #define AR724X_PLL_VAL_10 0x00991099
420 #define AR91XX_PLL_VAL_1000 0x1a000000
421 #define AR91XX_PLL_VAL_100 0x13000a44
422 #define AR91XX_PLL_VAL_10 0x00441099
424 static void __init ar71xx_init_eth_pll_data(unsigned int id)
426 struct ar71xx_eth_pll_data *pll_data;
427 u32 pll_10, pll_100, pll_1000;
431 pll_data = &ar71xx_eth0_pll_data;
434 pll_data = &ar71xx_eth1_pll_data;
440 switch (ar71xx_soc) {
441 case AR71XX_SOC_AR7130:
442 case AR71XX_SOC_AR7141:
443 case AR71XX_SOC_AR7161:
444 pll_10 = AR71XX_PLL_VAL_10;
445 pll_100 = AR71XX_PLL_VAL_100;
446 pll_1000 = AR71XX_PLL_VAL_1000;
449 case AR71XX_SOC_AR7240:
450 pll_10 = AR724X_PLL_VAL_10;
451 pll_100 = AR724X_PLL_VAL_100;
452 pll_1000 = AR724X_PLL_VAL_1000;
455 case AR71XX_SOC_AR9130:
456 case AR71XX_SOC_AR9132:
457 pll_10 = AR91XX_PLL_VAL_10;
458 pll_100 = AR91XX_PLL_VAL_100;
459 pll_1000 = AR91XX_PLL_VAL_1000;
465 if (!pll_data->pll_10)
466 pll_data->pll_10 = pll_10;
468 if (!pll_data->pll_100)
469 pll_data->pll_100 = pll_100;
471 if (!pll_data->pll_1000)
472 pll_data->pll_1000 = pll_1000;
475 static int ar71xx_eth_instance __initdata;
476 void __init ar71xx_add_device_eth(unsigned int id)
478 struct platform_device *pdev;
479 struct ag71xx_platform_data *pdata;
481 ar71xx_init_eth_pll_data(id);
485 switch (ar71xx_eth0_data.phy_if_mode) {
486 case PHY_INTERFACE_MODE_MII:
487 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_MII;
489 case PHY_INTERFACE_MODE_GMII:
490 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_GMII;
492 case PHY_INTERFACE_MODE_RGMII:
493 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_RGMII;
495 case PHY_INTERFACE_MODE_RMII:
496 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_RMII;
499 printk(KERN_ERR "ar71xx: invalid PHY interface mode "
503 pdev = &ar71xx_eth0_device;
506 switch (ar71xx_eth1_data.phy_if_mode) {
507 case PHY_INTERFACE_MODE_RMII:
508 ar71xx_eth1_data.mii_if = MII1_CTRL_IF_RMII;
510 case PHY_INTERFACE_MODE_RGMII:
511 ar71xx_eth1_data.mii_if = MII1_CTRL_IF_RGMII;
514 printk(KERN_ERR "ar71xx: invalid PHY interface mode "
518 pdev = &ar71xx_eth1_device;
521 printk(KERN_ERR "ar71xx: invalid ethernet id %d\n", id);
525 pdata = pdev->dev.platform_data;
527 switch (ar71xx_soc) {
528 case AR71XX_SOC_AR7130:
529 pdata->ddr_flush = id ? ar71xx_ddr_flush_ge1
530 : ar71xx_ddr_flush_ge0;
531 pdata->set_pll = id ? ar71xx_set_pll_ge1
532 : ar71xx_set_pll_ge0;
535 case AR71XX_SOC_AR7141:
536 case AR71XX_SOC_AR7161:
537 pdata->ddr_flush = id ? ar71xx_ddr_flush_ge1
538 : ar71xx_ddr_flush_ge0;
539 pdata->set_pll = id ? ar71xx_set_pll_ge1
540 : ar71xx_set_pll_ge0;
544 case AR71XX_SOC_AR7240:
545 pdata->ddr_flush = id ? ar724x_ddr_flush_ge1
546 : ar724x_ddr_flush_ge0;
547 pdata->set_pll = id ? ar724x_set_pll_ge1
548 : ar724x_set_pll_ge0;
549 pdata->is_ar724x = 1;
552 case AR71XX_SOC_AR9130:
553 pdata->ddr_flush = id ? ar91xx_ddr_flush_ge1
554 : ar91xx_ddr_flush_ge0;
555 pdata->set_pll = id ? ar91xx_set_pll_ge1
556 : ar91xx_set_pll_ge0;
557 pdata->is_ar91xx = 1;
560 case AR71XX_SOC_AR9132:
561 pdata->ddr_flush = id ? ar91xx_ddr_flush_ge1
562 : ar91xx_ddr_flush_ge0;
563 pdata->set_pll = id ? ar91xx_set_pll_ge1
564 : ar91xx_set_pll_ge0;
565 pdata->is_ar91xx = 1;
573 switch (pdata->phy_if_mode) {
574 case PHY_INTERFACE_MODE_GMII:
575 case PHY_INTERFACE_MODE_RGMII:
576 if (!pdata->has_gbit) {
577 printk(KERN_ERR "ar71xx: no gbit available on eth%d\n",
586 if (is_valid_ether_addr(ar71xx_mac_base)) {
587 memcpy(pdata->mac_addr, ar71xx_mac_base, ETH_ALEN);
588 pdata->mac_addr[5] += ar71xx_eth_instance;
590 random_ether_addr(pdata->mac_addr);
592 "ar71xx: using random MAC address for eth%d\n",
593 ar71xx_eth_instance);
596 platform_device_register(pdev);
597 ar71xx_eth_instance++;
600 static struct resource ar71xx_spi_resources[] = {
602 .start = AR71XX_SPI_BASE,
603 .end = AR71XX_SPI_BASE + AR71XX_SPI_SIZE - 1,
604 .flags = IORESOURCE_MEM,
608 static struct platform_device ar71xx_spi_device = {
609 .name = "ar71xx-spi",
611 .resource = ar71xx_spi_resources,
612 .num_resources = ARRAY_SIZE(ar71xx_spi_resources),
615 void __init ar71xx_add_device_spi(struct ar71xx_spi_platform_data *pdata,
616 struct spi_board_info const *info,
619 spi_register_board_info(info, n);
620 ar71xx_spi_device.dev.platform_data = pdata;
621 platform_device_register(&ar71xx_spi_device);
624 void __init ar71xx_add_device_leds_gpio(int id, unsigned num_leds,
625 struct gpio_led *leds)
627 struct platform_device *pdev;
628 struct gpio_led_platform_data pdata;
632 p = kmalloc(num_leds * sizeof(*p), GFP_KERNEL);
636 memcpy(p, leds, num_leds * sizeof(*p));
638 pdev = platform_device_alloc("leds-gpio", id);
642 memset(&pdata, 0, sizeof(pdata));
643 pdata.num_leds = num_leds;
646 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
650 err = platform_device_add(pdev);
657 platform_device_put(pdev);
663 void __init ar71xx_add_device_gpio_buttons(int id,
664 unsigned poll_interval,
666 struct gpio_button *buttons)
668 struct platform_device *pdev;
669 struct gpio_buttons_platform_data pdata;
670 struct gpio_button *p;
673 p = kmalloc(nbuttons * sizeof(*p), GFP_KERNEL);
677 memcpy(p, buttons, nbuttons * sizeof(*p));
679 pdev = platform_device_alloc("gpio-buttons", id);
681 goto err_free_buttons;
683 memset(&pdata, 0, sizeof(pdata));
684 pdata.poll_interval = poll_interval;
685 pdata.nbuttons = nbuttons;
688 err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
693 err = platform_device_add(pdev);
700 platform_device_put(pdev);
706 void __init ar71xx_add_device_wdt(void)
708 platform_device_register_simple("ar71xx-wdt", -1, NULL, 0);
711 void __init ar71xx_set_mac_base(unsigned char *mac)
713 memcpy(ar71xx_mac_base, mac, ETH_ALEN);
716 void __init ar71xx_parse_mac_addr(char *mac_str)
721 t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
722 &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
725 t = sscanf(mac_str, "%02hhx.%02hhx.%02hhx.%02hhx.%02hhx.%02hhx",
726 &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
729 ar71xx_set_mac_base(tmp);
731 printk(KERN_DEBUG "ar71xx: failed to parse mac address "
732 "\"%s\"\n", mac_str);
735 static struct resource ar91xx_wmac_resources[] = {
737 .start = AR91XX_WMAC_BASE,
738 .end = AR91XX_WMAC_BASE + AR91XX_WMAC_SIZE - 1,
739 .flags = IORESOURCE_MEM,
741 .start = AR71XX_CPU_IRQ_WMAC,
742 .end = AR71XX_CPU_IRQ_WMAC,
743 .flags = IORESOURCE_IRQ,
747 static struct ath9k_platform_data ar91xx_wmac_data;
749 static struct platform_device ar91xx_wmac_device = {
752 .resource = ar91xx_wmac_resources,
753 .num_resources = ARRAY_SIZE(ar91xx_wmac_resources),
755 .platform_data = &ar91xx_wmac_data,
759 void __init ar91xx_add_device_wmac(void)
761 u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);
763 memcpy(ar91xx_wmac_data.eeprom_data, ee,
764 sizeof(ar91xx_wmac_data.eeprom_data));
766 ar71xx_device_stop(RESET_MODULE_AMBA2WMAC);
769 ar71xx_device_start(RESET_MODULE_AMBA2WMAC);
772 platform_device_register(&ar91xx_wmac_device);
775 static struct platform_device ar71xx_dsa_switch_device = {
780 void __init ar71xx_add_device_dsa(unsigned int id,
781 struct dsa_platform_data *d)
785 d->netdev = &ar71xx_eth0_device.dev;
788 d->netdev = &ar71xx_eth1_device.dev;
792 "ar71xx: invalid ethernet id %d for DSA switch\n",
796 d->mii_bus = &ar71xx_mdio_device.dev;
797 ar71xx_dsa_switch_device.dev.platform_data = d;
799 platform_device_register(&ar71xx_dsa_switch_device);