Commit | Line | Data |
---|---|---|
11b0bacd | 1 | /* |
a79d8e93 | 2 | * Fixed MDIO bus (MDIO bus emulation with fixed PHYs) |
11b0bacd | 3 | * |
a79d8e93 VB |
4 | * Author: Vitaly Bordug <vbordug@ru.mvista.com> |
5 | * Anton Vorontsov <avorontsov@ru.mvista.com> | |
11b0bacd | 6 | * |
a79d8e93 | 7 | * Copyright (c) 2006-2007 MontaVista Software, Inc. |
11b0bacd VB |
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 as published by the | |
11 | * Free Software Foundation; either version 2 of the License, or (at your | |
12 | * option) any later version. | |
11b0bacd | 13 | */ |
a79d8e93 | 14 | |
11b0bacd | 15 | #include <linux/kernel.h> |
11b0bacd | 16 | #include <linux/module.h> |
a79d8e93 VB |
17 | #include <linux/platform_device.h> |
18 | #include <linux/list.h> | |
11b0bacd | 19 | #include <linux/mii.h> |
11b0bacd | 20 | #include <linux/phy.h> |
7c32f470 | 21 | #include <linux/phy_fixed.h> |
57401d5e | 22 | #include <linux/err.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
a7595121 | 24 | #include <linux/of.h> |
a5597008 | 25 | #include <linux/gpio.h> |
11b0bacd | 26 | |
a79d8e93 | 27 | #define MII_REGS_NUM 29 |
11b0bacd | 28 | |
a79d8e93 VB |
29 | struct fixed_mdio_bus { |
30 | int irqs[PHY_MAX_ADDR]; | |
298cf9be | 31 | struct mii_bus *mii_bus; |
a79d8e93 VB |
32 | struct list_head phys; |
33 | }; | |
11b0bacd | 34 | |
a79d8e93 | 35 | struct fixed_phy { |
9b744942 | 36 | int addr; |
a79d8e93 VB |
37 | u16 regs[MII_REGS_NUM]; |
38 | struct phy_device *phydev; | |
39 | struct fixed_phy_status status; | |
40 | int (*link_update)(struct net_device *, struct fixed_phy_status *); | |
41 | struct list_head node; | |
a5597008 | 42 | int link_gpio; |
a79d8e93 | 43 | }; |
7c32f470 | 44 | |
a79d8e93 VB |
45 | static struct platform_device *pdev; |
46 | static struct fixed_mdio_bus platform_fmb = { | |
47 | .phys = LIST_HEAD_INIT(platform_fmb.phys), | |
48 | }; | |
7c32f470 | 49 | |
a79d8e93 | 50 | static int fixed_phy_update_regs(struct fixed_phy *fp) |
11b0bacd | 51 | { |
a79d8e93 | 52 | u16 bmsr = BMSR_ANEGCAPABLE; |
11b0bacd | 53 | u16 bmcr = 0; |
a79d8e93 VB |
54 | u16 lpagb = 0; |
55 | u16 lpa = 0; | |
11b0bacd | 56 | |
a5597008 AL |
57 | if (gpio_is_valid(fp->link_gpio)) |
58 | fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio); | |
59 | ||
868a4215 SS |
60 | if (!fp->status.link) |
61 | goto done; | |
62 | bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE; | |
63 | ||
a79d8e93 | 64 | if (fp->status.duplex) { |
11b0bacd VB |
65 | bmcr |= BMCR_FULLDPLX; |
66 | ||
a79d8e93 VB |
67 | switch (fp->status.speed) { |
68 | case 1000: | |
69 | bmsr |= BMSR_ESTATEN; | |
70 | bmcr |= BMCR_SPEED1000; | |
71 | lpagb |= LPA_1000FULL; | |
72 | break; | |
11b0bacd VB |
73 | case 100: |
74 | bmsr |= BMSR_100FULL; | |
75 | bmcr |= BMCR_SPEED100; | |
a79d8e93 | 76 | lpa |= LPA_100FULL; |
7c32f470 | 77 | break; |
11b0bacd VB |
78 | case 10: |
79 | bmsr |= BMSR_10FULL; | |
a79d8e93 | 80 | lpa |= LPA_10FULL; |
7c32f470 | 81 | break; |
a79d8e93 | 82 | default: |
8d242488 | 83 | pr_warn("fixed phy: unknown speed\n"); |
a79d8e93 | 84 | return -EINVAL; |
11b0bacd VB |
85 | } |
86 | } else { | |
a79d8e93 VB |
87 | switch (fp->status.speed) { |
88 | case 1000: | |
89 | bmsr |= BMSR_ESTATEN; | |
90 | bmcr |= BMCR_SPEED1000; | |
91 | lpagb |= LPA_1000HALF; | |
92 | break; | |
11b0bacd VB |
93 | case 100: |
94 | bmsr |= BMSR_100HALF; | |
95 | bmcr |= BMCR_SPEED100; | |
a79d8e93 | 96 | lpa |= LPA_100HALF; |
7c32f470 | 97 | break; |
11b0bacd | 98 | case 10: |
a79d8e93 VB |
99 | bmsr |= BMSR_10HALF; |
100 | lpa |= LPA_10HALF; | |
7c32f470 | 101 | break; |
a79d8e93 | 102 | default: |
8d242488 | 103 | pr_warn("fixed phy: unknown speed\n"); |
a79d8e93 | 104 | return -EINVAL; |
11b0bacd VB |
105 | } |
106 | } | |
107 | ||
a79d8e93 VB |
108 | if (fp->status.pause) |
109 | lpa |= LPA_PAUSE_CAP; | |
110 | ||
111 | if (fp->status.asym_pause) | |
112 | lpa |= LPA_PAUSE_ASYM; | |
113 | ||
868a4215 | 114 | done: |
9b744942 TP |
115 | fp->regs[MII_PHYSID1] = 0; |
116 | fp->regs[MII_PHYSID2] = 0; | |
a79d8e93 VB |
117 | |
118 | fp->regs[MII_BMSR] = bmsr; | |
119 | fp->regs[MII_BMCR] = bmcr; | |
120 | fp->regs[MII_LPA] = lpa; | |
121 | fp->regs[MII_STAT1000] = lpagb; | |
11b0bacd VB |
122 | |
123 | return 0; | |
124 | } | |
125 | ||
9b744942 | 126 | static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) |
11b0bacd | 127 | { |
ec2a5652 | 128 | struct fixed_mdio_bus *fmb = bus->priv; |
a79d8e93 VB |
129 | struct fixed_phy *fp; |
130 | ||
131 | if (reg_num >= MII_REGS_NUM) | |
132 | return -1; | |
133 | ||
a2dbba76 FF |
134 | /* We do not support emulating Clause 45 over Clause 22 register reads |
135 | * return an error instead of bogus data. | |
136 | */ | |
137 | switch (reg_num) { | |
138 | case MII_MMD_CTRL: | |
139 | case MII_MMD_DATA: | |
140 | return -1; | |
141 | default: | |
142 | break; | |
143 | } | |
144 | ||
a79d8e93 | 145 | list_for_each_entry(fp, &fmb->phys, node) { |
9b744942 | 146 | if (fp->addr == phy_addr) { |
a79d8e93 VB |
147 | /* Issue callback if user registered it. */ |
148 | if (fp->link_update) { | |
149 | fp->link_update(fp->phydev->attached_dev, | |
150 | &fp->status); | |
151 | fixed_phy_update_regs(fp); | |
11b0bacd | 152 | } |
a79d8e93 | 153 | return fp->regs[reg_num]; |
7c32f470 | 154 | } |
a79d8e93 | 155 | } |
11b0bacd | 156 | |
a79d8e93 | 157 | return 0xFFFF; |
11b0bacd VB |
158 | } |
159 | ||
9b744942 | 160 | static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num, |
a79d8e93 | 161 | u16 val) |
11b0bacd | 162 | { |
11b0bacd VB |
163 | return 0; |
164 | } | |
165 | ||
a79d8e93 VB |
166 | /* |
167 | * If something weird is required to be done with link/speed, | |
168 | * network driver is able to assign a function to implement this. | |
169 | * May be useful for PHY's that need to be software-driven. | |
170 | */ | |
171 | int fixed_phy_set_link_update(struct phy_device *phydev, | |
172 | int (*link_update)(struct net_device *, | |
173 | struct fixed_phy_status *)) | |
11b0bacd | 174 | { |
a79d8e93 VB |
175 | struct fixed_mdio_bus *fmb = &platform_fmb; |
176 | struct fixed_phy *fp; | |
11b0bacd | 177 | |
799d4444 | 178 | if (!phydev || !phydev->bus) |
a79d8e93 | 179 | return -EINVAL; |
11b0bacd | 180 | |
a79d8e93 | 181 | list_for_each_entry(fp, &fmb->phys, node) { |
9b744942 | 182 | if (fp->addr == phydev->addr) { |
a79d8e93 VB |
183 | fp->link_update = link_update; |
184 | fp->phydev = phydev; | |
185 | return 0; | |
186 | } | |
187 | } | |
11b0bacd | 188 | |
a79d8e93 | 189 | return -ENOENT; |
7c32f470 | 190 | } |
a79d8e93 | 191 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
7c32f470 | 192 | |
a3bebdce SS |
193 | int fixed_phy_update_state(struct phy_device *phydev, |
194 | const struct fixed_phy_status *status, | |
195 | const struct fixed_phy_status *changed) | |
196 | { | |
197 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
198 | struct fixed_phy *fp; | |
199 | ||
200 | if (!phydev || !phydev->bus) | |
201 | return -EINVAL; | |
202 | ||
203 | list_for_each_entry(fp, &fmb->phys, node) { | |
204 | if (fp->addr == phydev->addr) { | |
205 | #define _UPD(x) if (changed->x) \ | |
206 | fp->status.x = status->x | |
207 | _UPD(link); | |
208 | _UPD(speed); | |
209 | _UPD(duplex); | |
210 | _UPD(pause); | |
211 | _UPD(asym_pause); | |
212 | #undef _UPD | |
213 | fixed_phy_update_regs(fp); | |
214 | return 0; | |
215 | } | |
216 | } | |
217 | ||
218 | return -ENOENT; | |
219 | } | |
220 | EXPORT_SYMBOL(fixed_phy_update_state); | |
221 | ||
9b744942 | 222 | int fixed_phy_add(unsigned int irq, int phy_addr, |
a5597008 AL |
223 | struct fixed_phy_status *status, |
224 | int link_gpio) | |
11b0bacd | 225 | { |
a79d8e93 VB |
226 | int ret; |
227 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
228 | struct fixed_phy *fp; | |
11b0bacd | 229 | |
a79d8e93 VB |
230 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
231 | if (!fp) | |
232 | return -ENOMEM; | |
11b0bacd | 233 | |
a79d8e93 | 234 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
11b0bacd | 235 | |
9b744942 | 236 | fmb->irqs[phy_addr] = irq; |
11b0bacd | 237 | |
9b744942 | 238 | fp->addr = phy_addr; |
a79d8e93 | 239 | fp->status = *status; |
a5597008 AL |
240 | fp->link_gpio = link_gpio; |
241 | ||
242 | if (gpio_is_valid(fp->link_gpio)) { | |
243 | ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN, | |
244 | "fixed-link-gpio-link"); | |
245 | if (ret) | |
246 | goto err_regs; | |
247 | } | |
7c32f470 | 248 | |
a79d8e93 VB |
249 | ret = fixed_phy_update_regs(fp); |
250 | if (ret) | |
a5597008 | 251 | goto err_gpio; |
11b0bacd | 252 | |
a79d8e93 | 253 | list_add_tail(&fp->node, &fmb->phys); |
7c32f470 | 254 | |
a79d8e93 | 255 | return 0; |
11b0bacd | 256 | |
a5597008 AL |
257 | err_gpio: |
258 | if (gpio_is_valid(fp->link_gpio)) | |
259 | gpio_free(fp->link_gpio); | |
a79d8e93 VB |
260 | err_regs: |
261 | kfree(fp); | |
262 | return ret; | |
263 | } | |
264 | EXPORT_SYMBOL_GPL(fixed_phy_add); | |
11b0bacd | 265 | |
a7595121 TP |
266 | void fixed_phy_del(int phy_addr) |
267 | { | |
268 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
269 | struct fixed_phy *fp, *tmp; | |
270 | ||
271 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { | |
272 | if (fp->addr == phy_addr) { | |
273 | list_del(&fp->node); | |
a5597008 AL |
274 | if (gpio_is_valid(fp->link_gpio)) |
275 | gpio_free(fp->link_gpio); | |
a7595121 TP |
276 | kfree(fp); |
277 | return; | |
278 | } | |
279 | } | |
280 | } | |
281 | EXPORT_SYMBOL_GPL(fixed_phy_del); | |
282 | ||
283 | static int phy_fixed_addr; | |
284 | static DEFINE_SPINLOCK(phy_fixed_addr_lock); | |
285 | ||
fd2ef0ba PG |
286 | struct phy_device *fixed_phy_register(unsigned int irq, |
287 | struct fixed_phy_status *status, | |
a5597008 | 288 | int link_gpio, |
fd2ef0ba | 289 | struct device_node *np) |
a7595121 TP |
290 | { |
291 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
292 | struct phy_device *phy; | |
293 | int phy_addr; | |
294 | int ret; | |
295 | ||
296 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ | |
297 | spin_lock(&phy_fixed_addr_lock); | |
298 | if (phy_fixed_addr == PHY_MAX_ADDR) { | |
299 | spin_unlock(&phy_fixed_addr_lock); | |
fd2ef0ba | 300 | return ERR_PTR(-ENOSPC); |
a7595121 TP |
301 | } |
302 | phy_addr = phy_fixed_addr++; | |
303 | spin_unlock(&phy_fixed_addr_lock); | |
304 | ||
a5597008 | 305 | ret = fixed_phy_add(PHY_POLL, phy_addr, status, link_gpio); |
a7595121 | 306 | if (ret < 0) |
fd2ef0ba | 307 | return ERR_PTR(ret); |
a7595121 TP |
308 | |
309 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); | |
310 | if (!phy || IS_ERR(phy)) { | |
311 | fixed_phy_del(phy_addr); | |
fd2ef0ba | 312 | return ERR_PTR(-EINVAL); |
a7595121 TP |
313 | } |
314 | ||
4b195360 MB |
315 | /* propagate the fixed link values to struct phy_device */ |
316 | phy->link = status->link; | |
317 | if (status->link) { | |
318 | phy->speed = status->speed; | |
319 | phy->duplex = status->duplex; | |
320 | phy->pause = status->pause; | |
321 | phy->asym_pause = status->asym_pause; | |
322 | } | |
323 | ||
a7595121 TP |
324 | of_node_get(np); |
325 | phy->dev.of_node = np; | |
5a11dd7d | 326 | phy->is_pseudo_fixed_link = true; |
a7595121 | 327 | |
34b31da4 AL |
328 | switch (status->speed) { |
329 | case SPEED_1000: | |
330 | phy->supported = PHY_1000BT_FEATURES; | |
331 | break; | |
332 | case SPEED_100: | |
333 | phy->supported = PHY_100BT_FEATURES; | |
334 | break; | |
335 | case SPEED_10: | |
336 | default: | |
337 | phy->supported = PHY_10BT_FEATURES; | |
338 | } | |
339 | ||
a7595121 TP |
340 | ret = phy_device_register(phy); |
341 | if (ret) { | |
342 | phy_device_free(phy); | |
343 | of_node_put(np); | |
344 | fixed_phy_del(phy_addr); | |
fd2ef0ba | 345 | return ERR_PTR(ret); |
a7595121 TP |
346 | } |
347 | ||
fd2ef0ba | 348 | return phy; |
a7595121 | 349 | } |
37e9a690 | 350 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
a7595121 | 351 | |
a79d8e93 VB |
352 | static int __init fixed_mdio_bus_init(void) |
353 | { | |
354 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
355 | int ret; | |
11b0bacd | 356 | |
a79d8e93 | 357 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
57401d5e DC |
358 | if (IS_ERR(pdev)) { |
359 | ret = PTR_ERR(pdev); | |
a79d8e93 VB |
360 | goto err_pdev; |
361 | } | |
11b0bacd | 362 | |
298cf9be LB |
363 | fmb->mii_bus = mdiobus_alloc(); |
364 | if (fmb->mii_bus == NULL) { | |
365 | ret = -ENOMEM; | |
366 | goto err_mdiobus_reg; | |
367 | } | |
11b0bacd | 368 | |
9e6c643b | 369 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
298cf9be | 370 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
ec2a5652 | 371 | fmb->mii_bus->priv = fmb; |
298cf9be LB |
372 | fmb->mii_bus->parent = &pdev->dev; |
373 | fmb->mii_bus->read = &fixed_mdio_read; | |
374 | fmb->mii_bus->write = &fixed_mdio_write; | |
375 | fmb->mii_bus->irq = fmb->irqs; | |
376 | ||
377 | ret = mdiobus_register(fmb->mii_bus); | |
a79d8e93 | 378 | if (ret) |
298cf9be | 379 | goto err_mdiobus_alloc; |
11b0bacd | 380 | |
a79d8e93 | 381 | return 0; |
11b0bacd | 382 | |
298cf9be LB |
383 | err_mdiobus_alloc: |
384 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 VB |
385 | err_mdiobus_reg: |
386 | platform_device_unregister(pdev); | |
387 | err_pdev: | |
388 | return ret; | |
389 | } | |
390 | module_init(fixed_mdio_bus_init); | |
11b0bacd | 391 | |
a79d8e93 VB |
392 | static void __exit fixed_mdio_bus_exit(void) |
393 | { | |
394 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
651be3a2 | 395 | struct fixed_phy *fp, *tmp; |
11b0bacd | 396 | |
298cf9be LB |
397 | mdiobus_unregister(fmb->mii_bus); |
398 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 | 399 | platform_device_unregister(pdev); |
11b0bacd | 400 | |
651be3a2 | 401 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
a79d8e93 VB |
402 | list_del(&fp->node); |
403 | kfree(fp); | |
7c32f470 | 404 | } |
11b0bacd | 405 | } |
a79d8e93 | 406 | module_exit(fixed_mdio_bus_exit); |
11b0bacd | 407 | |
a79d8e93 | 408 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
11b0bacd VB |
409 | MODULE_AUTHOR("Vitaly Bordug"); |
410 | MODULE_LICENSE("GPL"); |