2 * Atheros AR71xx built-in ethernet mac driver
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Based on Atheros' AG7100 driver
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.
16 #define AG71XX_MDIO_RETRY 1000
17 #define AG71XX_MDIO_DELAY 5
19 static inline void ag71xx_mdio_wr(struct ag71xx_mdio *am, unsigned reg,
24 r = am->mdio_base + reg;
25 __raw_writel(value, r);
28 (void) __raw_readl(r);
31 static inline u32 ag71xx_mdio_rr(struct ag71xx_mdio *am, unsigned reg)
33 return __raw_readl(am->mdio_base + reg);
36 static void ag71xx_mdio_dump_regs(struct ag71xx_mdio *am)
38 DBG("%s: mii_cfg=%08x, mii_cmd=%08x, mii_addr=%08x\n",
40 ag71xx_mdio_rr(am, AG71XX_REG_MII_CFG),
41 ag71xx_mdio_rr(am, AG71XX_REG_MII_CMD),
42 ag71xx_mdio_rr(am, AG71XX_REG_MII_ADDR));
43 DBG("%s: mii_ctrl=%08x, mii_status=%08x, mii_ind=%08x\n",
45 ag71xx_mdio_rr(am, AG71XX_REG_MII_CTRL),
46 ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS),
47 ag71xx_mdio_rr(am, AG71XX_REG_MII_IND));
50 static int ag71xx_mdio_wait_busy(struct ag71xx_mdio *am)
54 for (i = 0; i < AG71XX_MDIO_RETRY; i++) {
57 udelay(AG71XX_MDIO_DELAY);
59 busy = ag71xx_mdio_rr(am, AG71XX_REG_MII_IND);
63 udelay(AG71XX_MDIO_DELAY);
66 pr_err("%s: MDIO operation timed out\n", am->mii_bus->name);
71 int ag71xx_mdio_mii_read(struct ag71xx_mdio *am, int addr, int reg)
76 err = ag71xx_mdio_wait_busy(am);
80 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
81 ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
82 ((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
83 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_READ);
85 err = ag71xx_mdio_wait_busy(am);
89 ret = ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS) & 0xffff;
90 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
92 DBG("mii_read: addr=%04x, reg=%04x, value=%04x\n", addr, reg, ret);
97 void ag71xx_mdio_mii_write(struct ag71xx_mdio *am, int addr, int reg, u16 val)
99 DBG("mii_write: addr=%04x, reg=%04x, value=%04x\n", addr, reg, val);
101 ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
102 ((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
103 ag71xx_mdio_wr(am, AG71XX_REG_MII_CTRL, val);
105 ag71xx_mdio_wait_busy(am);
108 static const u32 ar71xx_mdio_div_table[] = {
109 4, 4, 6, 8, 10, 14, 20, 28,
112 static const u32 ar7240_mdio_div_table[] = {
113 2, 2, 4, 6, 8, 12, 18, 26, 32, 40, 48, 56, 62, 70, 78, 96,
116 static const u32 ar933x_mdio_div_table[] = {
117 4, 4, 6, 8, 10, 14, 20, 28, 34, 42, 50, 58, 66, 74, 82, 98,
120 static int ag71xx_mdio_get_divider(struct ag71xx_mdio *am, u32 *div)
122 unsigned long ref_clock, mdio_clock;
127 ref_clock = am->pdata->ref_clock;
128 mdio_clock = am->pdata->mdio_clock;
130 if (!ref_clock || !mdio_clock)
133 if (am->pdata->is_ar9330 || am->pdata->is_ar934x) {
134 table = ar933x_mdio_div_table;
135 ndivs = ARRAY_SIZE(ar933x_mdio_div_table);
136 } else if (am->pdata->is_ar7240) {
137 table = ar7240_mdio_div_table;
138 ndivs = ARRAY_SIZE(ar7240_mdio_div_table);
140 table = ar71xx_mdio_div_table;
141 ndivs = ARRAY_SIZE(ar71xx_mdio_div_table);
144 for (i = 0; i < ndivs; i++) {
147 t = ref_clock / table[i];
148 if (t <= mdio_clock) {
154 dev_err(&am->mii_bus->dev, "no divider found for %lu/%lu\n",
155 ref_clock, mdio_clock);
159 static int ag71xx_mdio_reset(struct mii_bus *bus)
161 struct ag71xx_mdio *am = bus->priv;
165 err = ag71xx_mdio_get_divider(am, &t);
168 if (am->pdata->is_ar7240)
169 t = MII_CFG_CLK_DIV_6;
170 else if (am->pdata->builtin_switch && !am->pdata->is_ar934x)
171 t = MII_CFG_CLK_DIV_10;
172 else if (!am->pdata->builtin_switch && am->pdata->is_ar934x)
173 t = MII_CFG_CLK_DIV_58;
175 t = MII_CFG_CLK_DIV_28;
178 ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t | MII_CFG_RESET);
181 ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t);
187 static int ag71xx_mdio_read(struct mii_bus *bus, int addr, int reg)
189 struct ag71xx_mdio *am = bus->priv;
191 if (am->pdata->builtin_switch)
192 return ar7240sw_phy_read(bus, addr, reg);
194 return ag71xx_mdio_mii_read(am, addr, reg);
197 static int ag71xx_mdio_write(struct mii_bus *bus, int addr, int reg, u16 val)
199 struct ag71xx_mdio *am = bus->priv;
201 if (am->pdata->builtin_switch)
202 ar7240sw_phy_write(bus, addr, reg, val);
204 ag71xx_mdio_mii_write(am, addr, reg, val);
208 static int __devinit ag71xx_mdio_probe(struct platform_device *pdev)
210 struct ag71xx_mdio_platform_data *pdata;
211 struct ag71xx_mdio *am;
212 struct resource *res;
216 pdata = pdev->dev.platform_data;
218 dev_err(&pdev->dev, "no platform data specified\n");
222 am = kzalloc(sizeof(*am), GFP_KERNEL);
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
232 dev_err(&pdev->dev, "no iomem resource found\n");
237 am->mdio_base = ioremap_nocache(res->start, res->end - res->start + 1);
238 if (!am->mdio_base) {
239 dev_err(&pdev->dev, "unable to ioremap registers\n");
244 am->mii_bus = mdiobus_alloc();
245 if (am->mii_bus == NULL) {
250 am->mii_bus->name = "ag71xx_mdio";
251 am->mii_bus->read = ag71xx_mdio_read;
252 am->mii_bus->write = ag71xx_mdio_write;
253 am->mii_bus->reset = ag71xx_mdio_reset;
254 am->mii_bus->irq = am->mii_irq;
255 am->mii_bus->priv = am;
256 am->mii_bus->parent = &pdev->dev;
257 snprintf(am->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(&pdev->dev));
258 am->mii_bus->phy_mask = pdata->phy_mask;
260 for (i = 0; i < PHY_MAX_ADDR; i++)
261 am->mii_irq[i] = PHY_POLL;
263 ag71xx_mdio_wr(am, AG71XX_REG_MAC_CFG1, 0);
265 err = mdiobus_register(am->mii_bus);
269 ag71xx_mdio_dump_regs(am);
271 platform_set_drvdata(pdev, am);
275 mdiobus_free(am->mii_bus);
277 iounmap(am->mdio_base);
284 static int __devexit ag71xx_mdio_remove(struct platform_device *pdev)
286 struct ag71xx_mdio *am = platform_get_drvdata(pdev);
289 mdiobus_unregister(am->mii_bus);
290 mdiobus_free(am->mii_bus);
291 iounmap(am->mdio_base);
293 platform_set_drvdata(pdev, NULL);
299 static struct platform_driver ag71xx_mdio_driver = {
300 .probe = ag71xx_mdio_probe,
301 .remove = __exit_p(ag71xx_mdio_remove),
303 .name = "ag71xx-mdio",
307 int __init ag71xx_mdio_driver_init(void)
309 return platform_driver_register(&ag71xx_mdio_driver);
312 void ag71xx_mdio_driver_exit(void)
314 platform_driver_unregister(&ag71xx_mdio_driver);