[adm5120] fix compilation failure of adm5120-dbg.c with the removal of bus_id
[openwrt.git] / target / linux / ar71xx / files / drivers / net / ag71xx / ag71xx_mdio.c
1 /*
2 * Atheros AR71xx built-in ethernet mac driver
3 *
4 * Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Based on Atheros' AG7100 driver
8 *
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.
12 */
13
14 #include "ag71xx.h"
15
16 #define AG71XX_MDIO_RETRY 1000
17 #define AG71XX_MDIO_DELAY 5
18
19 struct ag71xx_mdio *ag71xx_mdio_bus;
20
21 static inline void ag71xx_mdio_wr(struct ag71xx_mdio *am, unsigned reg,
22 u32 value)
23 {
24 void __iomem *r;
25
26 r = am->mdio_base + reg;
27 __raw_writel(value, r);
28
29 /* flush write */
30 (void) __raw_readl(r);
31 }
32
33 static inline u32 ag71xx_mdio_rr(struct ag71xx_mdio *am, unsigned reg)
34 {
35 return __raw_readl(am->mdio_base + reg);
36 }
37
38 static void ag71xx_mdio_dump_regs(struct ag71xx_mdio *am)
39 {
40 DBG("%s: mii_cfg=%08x, mii_cmd=%08x, mii_addr=%08x\n",
41 am->mii_bus->name,
42 ag71xx_mdio_rr(am, AG71XX_REG_MII_CFG),
43 ag71xx_mdio_rr(am, AG71XX_REG_MII_CMD),
44 ag71xx_mdio_rr(am, AG71XX_REG_MII_ADDR));
45 DBG("%s: mii_ctrl=%08x, mii_status=%08x, mii_ind=%08x\n",
46 am->mii_bus->name,
47 ag71xx_mdio_rr(am, AG71XX_REG_MII_CTRL),
48 ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS),
49 ag71xx_mdio_rr(am, AG71XX_REG_MII_IND));
50 }
51
52 static int ag71xx_mdio_mii_read(struct ag71xx_mdio *am, int addr, int reg)
53 {
54 int ret;
55 int i;
56
57 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
58 ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
59 ((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
60 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_READ);
61
62 i = AG71XX_MDIO_RETRY;
63 while (ag71xx_mdio_rr(am, AG71XX_REG_MII_IND) & MII_IND_BUSY) {
64 if (i-- == 0) {
65 printk(KERN_ERR "%s: mii_read timed out\n",
66 am->mii_bus->name);
67 ret = 0xffff;
68 goto out;
69 }
70 udelay(AG71XX_MDIO_DELAY);
71 }
72
73 ret = ag71xx_mdio_rr(am, AG71XX_REG_MII_STATUS) & 0xffff;
74 ag71xx_mdio_wr(am, AG71XX_REG_MII_CMD, MII_CMD_WRITE);
75
76 DBG("mii_read: addr=%04x, reg=%04x, value=%04x\n", addr, reg, ret);
77
78 out:
79 return ret;
80 }
81
82 static void ag71xx_mdio_mii_write(struct ag71xx_mdio *am,
83 int addr, int reg, u16 val)
84 {
85 int i;
86
87 DBG("mii_write: addr=%04x, reg=%04x, value=%04x\n", addr, reg, val);
88
89 ag71xx_mdio_wr(am, AG71XX_REG_MII_ADDR,
90 ((addr & 0xff) << MII_ADDR_SHIFT) | (reg & 0xff));
91 ag71xx_mdio_wr(am, AG71XX_REG_MII_CTRL, val);
92
93 i = AG71XX_MDIO_RETRY;
94 while (ag71xx_mdio_rr(am, AG71XX_REG_MII_IND) & MII_IND_BUSY) {
95 if (i-- == 0) {
96 printk(KERN_ERR "%s: mii_write timed out\n",
97 am->mii_bus->name);
98 break;
99 }
100 udelay(AG71XX_MDIO_DELAY);
101 }
102 }
103
104 static int ag71xx_mdio_reset(struct mii_bus *bus)
105 {
106 struct ag71xx_mdio *am = bus->priv;
107 u32 t;
108
109 if (am->pdata->is_ar7240)
110 t = MII_CFG_CLK_DIV_6;
111 else
112 t = MII_CFG_CLK_DIV_28;
113
114 ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t | MII_CFG_RESET);
115 udelay(100);
116
117 ag71xx_mdio_wr(am, AG71XX_REG_MII_CFG, t);
118 udelay(100);
119
120 return 0;
121 }
122
123 static int ag71xx_mdio_read(struct mii_bus *bus, int addr, int reg)
124 {
125 struct ag71xx_mdio *am = bus->priv;
126
127 return ag71xx_mdio_mii_read(am, addr, reg);
128 }
129
130 static int ag71xx_mdio_write(struct mii_bus *bus, int addr, int reg, u16 val)
131 {
132 struct ag71xx_mdio *am = bus->priv;
133
134 ag71xx_mdio_mii_write(am, addr, reg, val);
135 return 0;
136 }
137
138 static int __init ag71xx_mdio_probe(struct platform_device *pdev)
139 {
140 struct ag71xx_mdio_platform_data *pdata;
141 struct ag71xx_mdio *am;
142 struct resource *res;
143 int i;
144 int err;
145
146 if (ag71xx_mdio_bus)
147 return -EBUSY;
148
149 pdata = pdev->dev.platform_data;
150 if (!pdata) {
151 dev_err(&pdev->dev, "no platform data specified\n");
152 return -EINVAL;
153 }
154
155 am = kzalloc(sizeof(*am), GFP_KERNEL);
156 if (!am) {
157 err = -ENOMEM;
158 goto err_out;
159 }
160
161 am->pdata = pdata;
162
163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
164 if (!res) {
165 dev_err(&pdev->dev, "no iomem resource found\n");
166 err = -ENXIO;
167 goto err_out;
168 }
169
170 am->mdio_base = ioremap_nocache(res->start, res->end - res->start + 1);
171 if (!am->mdio_base) {
172 dev_err(&pdev->dev, "unable to ioremap registers\n");
173 err = -ENOMEM;
174 goto err_free_mdio;
175 }
176
177 am->mii_bus = mdiobus_alloc();
178 if (am->mii_bus == NULL) {
179 err = -ENOMEM;
180 goto err_iounmap;
181 }
182
183 am->mii_bus->name = "ag71xx_mdio";
184 am->mii_bus->read = ag71xx_mdio_read;
185 am->mii_bus->write = ag71xx_mdio_write;
186 am->mii_bus->reset = ag71xx_mdio_reset;
187 am->mii_bus->irq = am->mii_irq;
188 am->mii_bus->priv = am;
189 am->mii_bus->parent = &pdev->dev;
190 snprintf(am->mii_bus->id, MII_BUS_ID_SIZE, "%x", 0);
191 am->mii_bus->phy_mask = pdata->phy_mask;
192
193 for (i = 0; i < PHY_MAX_ADDR; i++)
194 am->mii_irq[i] = PHY_POLL;
195
196 ag71xx_mdio_wr(am, AG71XX_REG_MAC_CFG1, 0);
197
198 err = mdiobus_register(am->mii_bus);
199 if (err)
200 goto err_free_bus;
201
202 ag71xx_mdio_dump_regs(am);
203
204 platform_set_drvdata(pdev, am);
205 ag71xx_mdio_bus = am;
206 return 0;
207
208 err_free_bus:
209 mdiobus_free(am->mii_bus);
210 err_iounmap:
211 iounmap(am->mdio_base);
212 err_free_mdio:
213 kfree(am);
214 err_out:
215 return err;
216 }
217
218 static int __exit ag71xx_mdio_remove(struct platform_device *pdev)
219 {
220 struct ag71xx_mdio *am = platform_get_drvdata(pdev);
221
222 if (am) {
223 ag71xx_mdio_bus = NULL;
224 mdiobus_unregister(am->mii_bus);
225 mdiobus_free(am->mii_bus);
226 iounmap(am->mdio_base);
227 kfree(am);
228 platform_set_drvdata(pdev, NULL);
229 }
230
231 return 0;
232 }
233
234 static struct platform_driver ag71xx_mdio_driver = {
235 .probe = ag71xx_mdio_probe,
236 .remove = __exit_p(ag71xx_mdio_remove),
237 .driver = {
238 .name = "ag71xx-mdio",
239 }
240 };
241
242 int ag71xx_mdio_driver_init(void)
243 {
244 return platform_driver_register(&ag71xx_mdio_driver);
245 }
246
247 void ag71xx_mdio_driver_exit(void)
248 {
249 platform_driver_unregister(&ag71xx_mdio_driver);
250 }
This page took 0.075281 seconds and 5 git commands to generate.