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/etherdevice.h>
18 #include <linux/platform_device.h>
19 #include <linux/serial_8250.h>
21 #include <asm/mach-ar71xx/ar71xx.h>
25 static u8 ar71xx_mac_base[ETH_ALEN] __initdata;
27 static struct resource ar71xx_uart_resources[] = {
29 .start = AR71XX_UART_BASE,
30 .end = AR71XX_UART_BASE + AR71XX_UART_SIZE - 1,
31 .flags = IORESOURCE_MEM,
35 #define AR71XX_UART_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_IOREMAP)
36 static struct plat_serial8250_port ar71xx_uart_data[] = {
38 .mapbase = AR71XX_UART_BASE,
39 .irq = AR71XX_MISC_IRQ_UART,
40 .flags = AR71XX_UART_FLAGS,
44 /* terminating entry */
48 static struct platform_device ar71xx_uart_device = {
50 .id = PLAT8250_DEV_PLATFORM,
51 .resource = ar71xx_uart_resources,
52 .num_resources = ARRAY_SIZE(ar71xx_uart_resources),
54 .platform_data = ar71xx_uart_data
58 void __init ar71xx_add_device_uart(void)
60 ar71xx_uart_data[0].uartclk = ar71xx_ahb_freq;
61 platform_device_register(&ar71xx_uart_device);
64 static struct resource ar71xx_mdio_resources[] = {
67 .flags = IORESOURCE_MEM,
68 .start = AR71XX_GE0_BASE,
69 .end = AR71XX_GE0_BASE + 0x200 - 1,
73 static struct ag71xx_mdio_platform_data ar71xx_mdio_data;
75 struct platform_device ar71xx_mdio_device = {
76 .name = "ag71xx-mdio",
78 .resource = ar71xx_mdio_resources,
79 .num_resources = ARRAY_SIZE(ar71xx_mdio_resources),
81 .platform_data = &ar71xx_mdio_data,
85 static void ar71xx_set_pll(u32 cfg_reg, u32 pll_reg, u32 pll_val, u32 shift)
90 base = ioremap_nocache(AR71XX_PLL_BASE, AR71XX_PLL_SIZE);
92 t = __raw_readl(base + cfg_reg);
95 __raw_writel(t, base + cfg_reg);
98 __raw_writel(pll_val, base + pll_reg);
101 __raw_writel(t, base + cfg_reg);
105 __raw_writel(t, base + cfg_reg);
108 printk(KERN_DEBUG "ar71xx: pll_reg %#x: %#x\n",
109 (unsigned int)(base + pll_reg), __raw_readl(base + pll_reg));
114 void __init ar71xx_add_device_mdio(u32 phy_mask)
116 switch (ar71xx_soc) {
117 case AR71XX_SOC_AR7240:
118 ar71xx_mdio_data.is_ar7240 = 1;
120 case AR71XX_SOC_AR7241:
121 ar71xx_mdio_data.is_ar7240 = 1;
122 ar71xx_mdio_resources[0].start = AR71XX_GE1_BASE;
123 ar71xx_mdio_resources[0].end = AR71XX_GE1_BASE + 0x200 - 1;
125 case AR71XX_SOC_AR7242:
126 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG,
127 AR7242_PLL_REG_ETH0_INT_CLOCK, 0x62000000,
128 AR71XX_ETH0_PLL_SHIFT);
134 ar71xx_mdio_data.phy_mask = phy_mask;
136 platform_device_register(&ar71xx_mdio_device);
139 struct ar71xx_eth_pll_data ar71xx_eth0_pll_data;
140 struct ar71xx_eth_pll_data ar71xx_eth1_pll_data;
142 static u32 ar71xx_get_eth_pll(unsigned int mac, int speed)
144 struct ar71xx_eth_pll_data *pll_data;
149 pll_data = &ar71xx_eth0_pll_data;
152 pll_data = &ar71xx_eth1_pll_data;
160 pll_val = pll_data->pll_10;
163 pll_val = pll_data->pll_100;
166 pll_val = pll_data->pll_1000;
175 static void ar71xx_set_pll_ge0(int speed)
177 u32 val = ar71xx_get_eth_pll(0, speed);
179 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG, AR71XX_PLL_REG_ETH0_INT_CLOCK,
180 val, AR71XX_ETH0_PLL_SHIFT);
183 static void ar71xx_set_pll_ge1(int speed)
185 u32 val = ar71xx_get_eth_pll(1, speed);
187 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG, AR71XX_PLL_REG_ETH1_INT_CLOCK,
188 val, AR71XX_ETH1_PLL_SHIFT);
191 static void ar724x_set_pll_ge0(int speed)
196 static void ar724x_set_pll_ge1(int speed)
201 static void ar7242_set_pll_ge0(int speed)
203 u32 val = ar71xx_get_eth_pll(0, speed);
205 ar71xx_set_pll(AR71XX_PLL_REG_SEC_CONFIG, AR7242_PLL_REG_ETH0_INT_CLOCK,
206 val, AR71XX_ETH0_PLL_SHIFT);
209 static void ar91xx_set_pll_ge0(int speed)
211 u32 val = ar71xx_get_eth_pll(0, speed);
213 ar71xx_set_pll(AR91XX_PLL_REG_ETH_CONFIG, AR91XX_PLL_REG_ETH0_INT_CLOCK,
214 val, AR91XX_ETH0_PLL_SHIFT);
217 static void ar91xx_set_pll_ge1(int speed)
219 u32 val = ar71xx_get_eth_pll(1, speed);
221 ar71xx_set_pll(AR91XX_PLL_REG_ETH_CONFIG, AR91XX_PLL_REG_ETH1_INT_CLOCK,
222 val, AR91XX_ETH1_PLL_SHIFT);
225 static void ar71xx_ddr_flush_ge0(void)
227 ar71xx_ddr_flush(AR71XX_DDR_REG_FLUSH_GE0);
230 static void ar71xx_ddr_flush_ge1(void)
232 ar71xx_ddr_flush(AR71XX_DDR_REG_FLUSH_GE1);
235 static void ar724x_ddr_flush_ge0(void)
237 ar71xx_ddr_flush(AR724X_DDR_REG_FLUSH_GE0);
240 static void ar724x_ddr_flush_ge1(void)
242 ar71xx_ddr_flush(AR724X_DDR_REG_FLUSH_GE1);
245 static void ar91xx_ddr_flush_ge0(void)
247 ar71xx_ddr_flush(AR91XX_DDR_REG_FLUSH_GE0);
250 static void ar91xx_ddr_flush_ge1(void)
252 ar71xx_ddr_flush(AR91XX_DDR_REG_FLUSH_GE1);
255 static struct resource ar71xx_eth0_resources[] = {
258 .flags = IORESOURCE_MEM,
259 .start = AR71XX_GE0_BASE,
260 .end = AR71XX_GE0_BASE + 0x200 - 1,
263 .flags = IORESOURCE_MEM,
264 .start = AR71XX_MII_BASE + MII_REG_MII0_CTRL,
265 .end = AR71XX_MII_BASE + MII_REG_MII0_CTRL + 3,
268 .flags = IORESOURCE_IRQ,
269 .start = AR71XX_CPU_IRQ_GE0,
270 .end = AR71XX_CPU_IRQ_GE0,
274 struct ag71xx_platform_data ar71xx_eth0_data = {
275 .reset_bit = RESET_MODULE_GE0_MAC,
278 struct platform_device ar71xx_eth0_device = {
281 .resource = ar71xx_eth0_resources,
282 .num_resources = ARRAY_SIZE(ar71xx_eth0_resources),
284 .platform_data = &ar71xx_eth0_data,
288 static struct resource ar71xx_eth1_resources[] = {
291 .flags = IORESOURCE_MEM,
292 .start = AR71XX_GE1_BASE,
293 .end = AR71XX_GE1_BASE + 0x200 - 1,
296 .flags = IORESOURCE_MEM,
297 .start = AR71XX_MII_BASE + MII_REG_MII1_CTRL,
298 .end = AR71XX_MII_BASE + MII_REG_MII1_CTRL + 3,
301 .flags = IORESOURCE_IRQ,
302 .start = AR71XX_CPU_IRQ_GE1,
303 .end = AR71XX_CPU_IRQ_GE1,
307 struct ag71xx_platform_data ar71xx_eth1_data = {
308 .reset_bit = RESET_MODULE_GE1_MAC,
311 struct platform_device ar71xx_eth1_device = {
314 .resource = ar71xx_eth1_resources,
315 .num_resources = ARRAY_SIZE(ar71xx_eth1_resources),
317 .platform_data = &ar71xx_eth1_data,
321 #define AR71XX_PLL_VAL_1000 0x00110000
322 #define AR71XX_PLL_VAL_100 0x00001099
323 #define AR71XX_PLL_VAL_10 0x00991099
325 #define AR724X_PLL_VAL_1000 0x00110000
326 #define AR724X_PLL_VAL_100 0x00001099
327 #define AR724X_PLL_VAL_10 0x00991099
329 #define AR7242_PLL_VAL_1000 0x1c000000
330 #define AR7242_PLL_VAL_100 0x00000101
331 #define AR7242_PLL_VAL_10 0x00001616
333 #define AR91XX_PLL_VAL_1000 0x1a000000
334 #define AR91XX_PLL_VAL_100 0x13000a44
335 #define AR91XX_PLL_VAL_10 0x00441099
337 static void __init ar71xx_init_eth_pll_data(unsigned int id)
339 struct ar71xx_eth_pll_data *pll_data;
340 u32 pll_10, pll_100, pll_1000;
344 pll_data = &ar71xx_eth0_pll_data;
347 pll_data = &ar71xx_eth1_pll_data;
353 switch (ar71xx_soc) {
354 case AR71XX_SOC_AR7130:
355 case AR71XX_SOC_AR7141:
356 case AR71XX_SOC_AR7161:
357 pll_10 = AR71XX_PLL_VAL_10;
358 pll_100 = AR71XX_PLL_VAL_100;
359 pll_1000 = AR71XX_PLL_VAL_1000;
362 case AR71XX_SOC_AR7240:
363 case AR71XX_SOC_AR7241:
364 pll_10 = AR724X_PLL_VAL_10;
365 pll_100 = AR724X_PLL_VAL_100;
366 pll_1000 = AR724X_PLL_VAL_1000;
369 case AR71XX_SOC_AR7242:
370 pll_10 = AR7242_PLL_VAL_10;
371 pll_100 = AR7242_PLL_VAL_100;
372 pll_1000 = AR7242_PLL_VAL_1000;
375 case AR71XX_SOC_AR9130:
376 case AR71XX_SOC_AR9132:
377 pll_10 = AR91XX_PLL_VAL_10;
378 pll_100 = AR91XX_PLL_VAL_100;
379 pll_1000 = AR91XX_PLL_VAL_1000;
385 if (!pll_data->pll_10)
386 pll_data->pll_10 = pll_10;
388 if (!pll_data->pll_100)
389 pll_data->pll_100 = pll_100;
391 if (!pll_data->pll_1000)
392 pll_data->pll_1000 = pll_1000;
395 static int ar71xx_eth_instance __initdata;
396 void __init ar71xx_add_device_eth(unsigned int id)
398 struct platform_device *pdev;
399 struct ag71xx_platform_data *pdata;
401 ar71xx_init_eth_pll_data(id);
405 switch (ar71xx_eth0_data.phy_if_mode) {
406 case PHY_INTERFACE_MODE_MII:
407 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_MII;
409 case PHY_INTERFACE_MODE_GMII:
410 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_GMII;
412 case PHY_INTERFACE_MODE_RGMII:
413 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_RGMII;
415 case PHY_INTERFACE_MODE_RMII:
416 ar71xx_eth0_data.mii_if = MII0_CTRL_IF_RMII;
419 printk(KERN_ERR "ar71xx: invalid PHY interface mode "
423 pdev = &ar71xx_eth0_device;
426 switch (ar71xx_eth1_data.phy_if_mode) {
427 case PHY_INTERFACE_MODE_RMII:
428 ar71xx_eth1_data.mii_if = MII1_CTRL_IF_RMII;
430 case PHY_INTERFACE_MODE_RGMII:
431 ar71xx_eth1_data.mii_if = MII1_CTRL_IF_RGMII;
434 printk(KERN_ERR "ar71xx: invalid PHY interface mode "
438 pdev = &ar71xx_eth1_device;
441 printk(KERN_ERR "ar71xx: invalid ethernet id %d\n", id);
445 pdata = pdev->dev.platform_data;
447 switch (ar71xx_soc) {
448 case AR71XX_SOC_AR7130:
449 pdata->ddr_flush = id ? ar71xx_ddr_flush_ge1
450 : ar71xx_ddr_flush_ge0;
451 pdata->set_pll = id ? ar71xx_set_pll_ge1
452 : ar71xx_set_pll_ge0;
455 case AR71XX_SOC_AR7141:
456 case AR71XX_SOC_AR7161:
457 pdata->ddr_flush = id ? ar71xx_ddr_flush_ge1
458 : ar71xx_ddr_flush_ge0;
459 pdata->set_pll = id ? ar71xx_set_pll_ge1
460 : ar71xx_set_pll_ge0;
464 case AR71XX_SOC_AR7242:
465 ar71xx_eth0_data.reset_bit |= AR724X_RESET_GE0_MDIO |
466 RESET_MODULE_GE0_PHY;
467 ar71xx_eth1_data.reset_bit |= AR724X_RESET_GE1_MDIO |
468 RESET_MODULE_GE1_PHY;
469 pdata->ddr_flush = id ? ar724x_ddr_flush_ge1
470 : ar724x_ddr_flush_ge0;
471 pdata->set_pll = id ? ar724x_set_pll_ge1
472 : ar7242_set_pll_ge0;
474 pdata->is_ar724x = 1;
476 if (!pdata->fifo_cfg1)
477 pdata->fifo_cfg1 = 0x0010ffff;
478 if (!pdata->fifo_cfg2)
479 pdata->fifo_cfg2 = 0x015500aa;
480 if (!pdata->fifo_cfg3)
481 pdata->fifo_cfg3 = 0x01f00140;
484 case AR71XX_SOC_AR7241:
485 ar71xx_eth0_data.reset_bit |= AR724X_RESET_GE0_MDIO;
486 ar71xx_eth1_data.reset_bit |= AR724X_RESET_GE1_MDIO;
488 case AR71XX_SOC_AR7240:
489 ar71xx_eth0_data.reset_bit |= RESET_MODULE_GE0_PHY;
490 ar71xx_eth1_data.reset_bit |= RESET_MODULE_GE1_PHY;
491 pdata->ddr_flush = id ? ar724x_ddr_flush_ge1
492 : ar724x_ddr_flush_ge0;
493 pdata->set_pll = id ? ar724x_set_pll_ge1
494 : ar724x_set_pll_ge0;
495 pdata->is_ar724x = 1;
496 if (ar71xx_soc == AR71XX_SOC_AR7240)
497 pdata->is_ar7240 = 1;
499 if (!pdata->fifo_cfg1)
500 pdata->fifo_cfg1 = 0x0010ffff;
501 if (!pdata->fifo_cfg2)
502 pdata->fifo_cfg2 = 0x015500aa;
503 if (!pdata->fifo_cfg3)
504 pdata->fifo_cfg3 = 0x01f00140;
507 case AR71XX_SOC_AR9130:
508 pdata->ddr_flush = id ? ar91xx_ddr_flush_ge1
509 : ar91xx_ddr_flush_ge0;
510 pdata->set_pll = id ? ar91xx_set_pll_ge1
511 : ar91xx_set_pll_ge0;
512 pdata->is_ar91xx = 1;
515 case AR71XX_SOC_AR9132:
516 pdata->ddr_flush = id ? ar91xx_ddr_flush_ge1
517 : ar91xx_ddr_flush_ge0;
518 pdata->set_pll = id ? ar91xx_set_pll_ge1
519 : ar91xx_set_pll_ge0;
520 pdata->is_ar91xx = 1;
528 switch (pdata->phy_if_mode) {
529 case PHY_INTERFACE_MODE_GMII:
530 case PHY_INTERFACE_MODE_RGMII:
531 if (!pdata->has_gbit) {
532 printk(KERN_ERR "ar71xx: no gbit available on eth%d\n",
541 if (is_valid_ether_addr(ar71xx_mac_base)) {
542 memcpy(pdata->mac_addr, ar71xx_mac_base, ETH_ALEN);
543 pdata->mac_addr[5] += ar71xx_eth_instance;
545 random_ether_addr(pdata->mac_addr);
547 "ar71xx: using random MAC address for eth%d\n",
548 ar71xx_eth_instance);
551 if (pdata->mii_bus_dev == NULL)
552 pdata->mii_bus_dev = &ar71xx_mdio_device.dev;
554 /* Reset the device */
555 ar71xx_device_stop(pdata->reset_bit);
558 ar71xx_device_start(pdata->reset_bit);
561 platform_device_register(pdev);
562 ar71xx_eth_instance++;
565 static struct resource ar71xx_spi_resources[] = {
567 .start = AR71XX_SPI_BASE,
568 .end = AR71XX_SPI_BASE + AR71XX_SPI_SIZE - 1,
569 .flags = IORESOURCE_MEM,
573 static struct platform_device ar71xx_spi_device = {
574 .name = "ar71xx-spi",
576 .resource = ar71xx_spi_resources,
577 .num_resources = ARRAY_SIZE(ar71xx_spi_resources),
580 void __init ar71xx_add_device_spi(struct ar71xx_spi_platform_data *pdata,
581 struct spi_board_info const *info,
584 spi_register_board_info(info, n);
585 ar71xx_spi_device.dev.platform_data = pdata;
586 platform_device_register(&ar71xx_spi_device);
589 void __init ar71xx_add_device_wdt(void)
591 platform_device_register_simple("ar71xx-wdt", -1, NULL, 0);
594 void __init ar71xx_set_mac_base(unsigned char *mac)
596 memcpy(ar71xx_mac_base, mac, ETH_ALEN);
599 void __init ar71xx_parse_mac_addr(char *mac_str)
604 t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
605 &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
608 t = sscanf(mac_str, "%02hhx.%02hhx.%02hhx.%02hhx.%02hhx.%02hhx",
609 &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
612 ar71xx_set_mac_base(tmp);
614 printk(KERN_DEBUG "ar71xx: failed to parse mac address "
615 "\"%s\"\n", mac_str);
618 static int __init ar71xx_ethaddr_setup(char *str)
620 ar71xx_parse_mac_addr(str);
623 __setup("ethaddr=", ar71xx_ethaddr_setup);
625 static int __init ar71xx_kmac_setup(char *str)
627 ar71xx_parse_mac_addr(str);
630 __setup("kmac=", ar71xx_kmac_setup);