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 | |
5ae68b0c RK |
27 | #include "swphy.h" |
28 | ||
a79d8e93 | 29 | #define MII_REGS_NUM 29 |
11b0bacd | 30 | |
a79d8e93 | 31 | struct fixed_mdio_bus { |
298cf9be | 32 | struct mii_bus *mii_bus; |
a79d8e93 VB |
33 | struct list_head phys; |
34 | }; | |
11b0bacd | 35 | |
a79d8e93 | 36 | struct fixed_phy { |
9b744942 | 37 | int addr; |
a79d8e93 VB |
38 | u16 regs[MII_REGS_NUM]; |
39 | struct phy_device *phydev; | |
40 | struct fixed_phy_status status; | |
41 | int (*link_update)(struct net_device *, struct fixed_phy_status *); | |
42 | struct list_head node; | |
a5597008 | 43 | int link_gpio; |
a79d8e93 | 44 | }; |
7c32f470 | 45 | |
a79d8e93 VB |
46 | static struct platform_device *pdev; |
47 | static struct fixed_mdio_bus platform_fmb = { | |
48 | .phys = LIST_HEAD_INIT(platform_fmb.phys), | |
49 | }; | |
7c32f470 | 50 | |
68888ce0 | 51 | static void fixed_phy_update_regs(struct fixed_phy *fp) |
11b0bacd | 52 | { |
a5597008 AL |
53 | if (gpio_is_valid(fp->link_gpio)) |
54 | fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio); | |
55 | ||
68888ce0 | 56 | swphy_update_regs(fp->regs, &fp->status); |
11b0bacd VB |
57 | } |
58 | ||
9b744942 | 59 | static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) |
11b0bacd | 60 | { |
ec2a5652 | 61 | struct fixed_mdio_bus *fmb = bus->priv; |
a79d8e93 VB |
62 | struct fixed_phy *fp; |
63 | ||
64 | if (reg_num >= MII_REGS_NUM) | |
65 | return -1; | |
66 | ||
a2dbba76 FF |
67 | /* We do not support emulating Clause 45 over Clause 22 register reads |
68 | * return an error instead of bogus data. | |
69 | */ | |
70 | switch (reg_num) { | |
71 | case MII_MMD_CTRL: | |
72 | case MII_MMD_DATA: | |
73 | return -1; | |
74 | default: | |
75 | break; | |
76 | } | |
77 | ||
a79d8e93 | 78 | list_for_each_entry(fp, &fmb->phys, node) { |
9b744942 | 79 | if (fp->addr == phy_addr) { |
a79d8e93 VB |
80 | /* Issue callback if user registered it. */ |
81 | if (fp->link_update) { | |
82 | fp->link_update(fp->phydev->attached_dev, | |
83 | &fp->status); | |
84 | fixed_phy_update_regs(fp); | |
11b0bacd | 85 | } |
a79d8e93 | 86 | return fp->regs[reg_num]; |
7c32f470 | 87 | } |
a79d8e93 | 88 | } |
11b0bacd | 89 | |
a79d8e93 | 90 | return 0xFFFF; |
11b0bacd VB |
91 | } |
92 | ||
9b744942 | 93 | static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num, |
a79d8e93 | 94 | u16 val) |
11b0bacd | 95 | { |
11b0bacd VB |
96 | return 0; |
97 | } | |
98 | ||
a79d8e93 VB |
99 | /* |
100 | * If something weird is required to be done with link/speed, | |
101 | * network driver is able to assign a function to implement this. | |
102 | * May be useful for PHY's that need to be software-driven. | |
103 | */ | |
104 | int fixed_phy_set_link_update(struct phy_device *phydev, | |
105 | int (*link_update)(struct net_device *, | |
106 | struct fixed_phy_status *)) | |
11b0bacd | 107 | { |
a79d8e93 VB |
108 | struct fixed_mdio_bus *fmb = &platform_fmb; |
109 | struct fixed_phy *fp; | |
11b0bacd | 110 | |
e5a03bfd | 111 | if (!phydev || !phydev->mdio.bus) |
a79d8e93 | 112 | return -EINVAL; |
11b0bacd | 113 | |
a79d8e93 | 114 | list_for_each_entry(fp, &fmb->phys, node) { |
e5a03bfd | 115 | if (fp->addr == phydev->mdio.addr) { |
a79d8e93 VB |
116 | fp->link_update = link_update; |
117 | fp->phydev = phydev; | |
118 | return 0; | |
119 | } | |
120 | } | |
11b0bacd | 121 | |
a79d8e93 | 122 | return -ENOENT; |
7c32f470 | 123 | } |
a79d8e93 | 124 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
7c32f470 | 125 | |
a3bebdce SS |
126 | int fixed_phy_update_state(struct phy_device *phydev, |
127 | const struct fixed_phy_status *status, | |
128 | const struct fixed_phy_status *changed) | |
129 | { | |
130 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
131 | struct fixed_phy *fp; | |
132 | ||
e5a03bfd | 133 | if (!phydev || phydev->mdio.bus != fmb->mii_bus) |
a3bebdce SS |
134 | return -EINVAL; |
135 | ||
136 | list_for_each_entry(fp, &fmb->phys, node) { | |
e5a03bfd | 137 | if (fp->addr == phydev->mdio.addr) { |
a3bebdce SS |
138 | #define _UPD(x) if (changed->x) \ |
139 | fp->status.x = status->x | |
140 | _UPD(link); | |
141 | _UPD(speed); | |
142 | _UPD(duplex); | |
143 | _UPD(pause); | |
144 | _UPD(asym_pause); | |
145 | #undef _UPD | |
146 | fixed_phy_update_regs(fp); | |
147 | return 0; | |
148 | } | |
149 | } | |
150 | ||
151 | return -ENOENT; | |
152 | } | |
153 | EXPORT_SYMBOL(fixed_phy_update_state); | |
154 | ||
9b744942 | 155 | int fixed_phy_add(unsigned int irq, int phy_addr, |
a5597008 AL |
156 | struct fixed_phy_status *status, |
157 | int link_gpio) | |
11b0bacd | 158 | { |
a79d8e93 VB |
159 | int ret; |
160 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
161 | struct fixed_phy *fp; | |
11b0bacd | 162 | |
68888ce0 RK |
163 | ret = swphy_validate_state(status); |
164 | if (ret < 0) | |
165 | return ret; | |
166 | ||
a79d8e93 VB |
167 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
168 | if (!fp) | |
169 | return -ENOMEM; | |
11b0bacd | 170 | |
a79d8e93 | 171 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
11b0bacd | 172 | |
185be5ae RV |
173 | if (irq != PHY_POLL) |
174 | fmb->mii_bus->irq[phy_addr] = irq; | |
11b0bacd | 175 | |
9b744942 | 176 | fp->addr = phy_addr; |
a79d8e93 | 177 | fp->status = *status; |
a5597008 AL |
178 | fp->link_gpio = link_gpio; |
179 | ||
180 | if (gpio_is_valid(fp->link_gpio)) { | |
181 | ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN, | |
182 | "fixed-link-gpio-link"); | |
183 | if (ret) | |
184 | goto err_regs; | |
185 | } | |
7c32f470 | 186 | |
68888ce0 | 187 | fixed_phy_update_regs(fp); |
11b0bacd | 188 | |
a79d8e93 | 189 | list_add_tail(&fp->node, &fmb->phys); |
7c32f470 | 190 | |
a79d8e93 | 191 | return 0; |
11b0bacd | 192 | |
a79d8e93 VB |
193 | err_regs: |
194 | kfree(fp); | |
195 | return ret; | |
196 | } | |
197 | EXPORT_SYMBOL_GPL(fixed_phy_add); | |
11b0bacd | 198 | |
5bcbe0f3 | 199 | static void fixed_phy_del(int phy_addr) |
a7595121 TP |
200 | { |
201 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
202 | struct fixed_phy *fp, *tmp; | |
203 | ||
204 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { | |
205 | if (fp->addr == phy_addr) { | |
206 | list_del(&fp->node); | |
a5597008 AL |
207 | if (gpio_is_valid(fp->link_gpio)) |
208 | gpio_free(fp->link_gpio); | |
a7595121 TP |
209 | kfree(fp); |
210 | return; | |
211 | } | |
212 | } | |
213 | } | |
a7595121 TP |
214 | |
215 | static int phy_fixed_addr; | |
216 | static DEFINE_SPINLOCK(phy_fixed_addr_lock); | |
217 | ||
fd2ef0ba PG |
218 | struct phy_device *fixed_phy_register(unsigned int irq, |
219 | struct fixed_phy_status *status, | |
a5597008 | 220 | int link_gpio, |
fd2ef0ba | 221 | struct device_node *np) |
a7595121 TP |
222 | { |
223 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
224 | struct phy_device *phy; | |
225 | int phy_addr; | |
226 | int ret; | |
227 | ||
185be5ae RV |
228 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
229 | return ERR_PTR(-EPROBE_DEFER); | |
230 | ||
a7595121 TP |
231 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
232 | spin_lock(&phy_fixed_addr_lock); | |
233 | if (phy_fixed_addr == PHY_MAX_ADDR) { | |
234 | spin_unlock(&phy_fixed_addr_lock); | |
fd2ef0ba | 235 | return ERR_PTR(-ENOSPC); |
a7595121 TP |
236 | } |
237 | phy_addr = phy_fixed_addr++; | |
238 | spin_unlock(&phy_fixed_addr_lock); | |
239 | ||
bd1a05ee | 240 | ret = fixed_phy_add(irq, phy_addr, status, link_gpio); |
a7595121 | 241 | if (ret < 0) |
fd2ef0ba | 242 | return ERR_PTR(ret); |
a7595121 TP |
243 | |
244 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); | |
4914a584 | 245 | if (IS_ERR(phy)) { |
a7595121 | 246 | fixed_phy_del(phy_addr); |
fd2ef0ba | 247 | return ERR_PTR(-EINVAL); |
a7595121 TP |
248 | } |
249 | ||
4b195360 MB |
250 | /* propagate the fixed link values to struct phy_device */ |
251 | phy->link = status->link; | |
252 | if (status->link) { | |
253 | phy->speed = status->speed; | |
254 | phy->duplex = status->duplex; | |
255 | phy->pause = status->pause; | |
256 | phy->asym_pause = status->asym_pause; | |
257 | } | |
258 | ||
a7595121 | 259 | of_node_get(np); |
e5a03bfd | 260 | phy->mdio.dev.of_node = np; |
5a11dd7d | 261 | phy->is_pseudo_fixed_link = true; |
a7595121 | 262 | |
34b31da4 AL |
263 | switch (status->speed) { |
264 | case SPEED_1000: | |
265 | phy->supported = PHY_1000BT_FEATURES; | |
266 | break; | |
267 | case SPEED_100: | |
268 | phy->supported = PHY_100BT_FEATURES; | |
269 | break; | |
270 | case SPEED_10: | |
271 | default: | |
272 | phy->supported = PHY_10BT_FEATURES; | |
273 | } | |
274 | ||
a7595121 TP |
275 | ret = phy_device_register(phy); |
276 | if (ret) { | |
277 | phy_device_free(phy); | |
278 | of_node_put(np); | |
279 | fixed_phy_del(phy_addr); | |
fd2ef0ba | 280 | return ERR_PTR(ret); |
a7595121 TP |
281 | } |
282 | ||
fd2ef0ba | 283 | return phy; |
a7595121 | 284 | } |
37e9a690 | 285 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
a7595121 | 286 | |
5bcbe0f3 AL |
287 | void fixed_phy_unregister(struct phy_device *phy) |
288 | { | |
289 | phy_device_remove(phy); | |
290 | ||
291 | fixed_phy_del(phy->mdio.addr); | |
292 | } | |
293 | EXPORT_SYMBOL_GPL(fixed_phy_unregister); | |
294 | ||
a79d8e93 VB |
295 | static int __init fixed_mdio_bus_init(void) |
296 | { | |
297 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
298 | int ret; | |
11b0bacd | 299 | |
a79d8e93 | 300 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
57401d5e DC |
301 | if (IS_ERR(pdev)) { |
302 | ret = PTR_ERR(pdev); | |
a79d8e93 VB |
303 | goto err_pdev; |
304 | } | |
11b0bacd | 305 | |
298cf9be LB |
306 | fmb->mii_bus = mdiobus_alloc(); |
307 | if (fmb->mii_bus == NULL) { | |
308 | ret = -ENOMEM; | |
309 | goto err_mdiobus_reg; | |
310 | } | |
11b0bacd | 311 | |
9e6c643b | 312 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
298cf9be | 313 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
ec2a5652 | 314 | fmb->mii_bus->priv = fmb; |
298cf9be LB |
315 | fmb->mii_bus->parent = &pdev->dev; |
316 | fmb->mii_bus->read = &fixed_mdio_read; | |
317 | fmb->mii_bus->write = &fixed_mdio_write; | |
298cf9be LB |
318 | |
319 | ret = mdiobus_register(fmb->mii_bus); | |
a79d8e93 | 320 | if (ret) |
298cf9be | 321 | goto err_mdiobus_alloc; |
11b0bacd | 322 | |
a79d8e93 | 323 | return 0; |
11b0bacd | 324 | |
298cf9be LB |
325 | err_mdiobus_alloc: |
326 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 VB |
327 | err_mdiobus_reg: |
328 | platform_device_unregister(pdev); | |
329 | err_pdev: | |
330 | return ret; | |
331 | } | |
332 | module_init(fixed_mdio_bus_init); | |
11b0bacd | 333 | |
a79d8e93 VB |
334 | static void __exit fixed_mdio_bus_exit(void) |
335 | { | |
336 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
651be3a2 | 337 | struct fixed_phy *fp, *tmp; |
11b0bacd | 338 | |
298cf9be LB |
339 | mdiobus_unregister(fmb->mii_bus); |
340 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 | 341 | platform_device_unregister(pdev); |
11b0bacd | 342 | |
651be3a2 | 343 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
a79d8e93 VB |
344 | list_del(&fp->node); |
345 | kfree(fp); | |
7c32f470 | 346 | } |
11b0bacd | 347 | } |
a79d8e93 | 348 | module_exit(fixed_mdio_bus_exit); |
11b0bacd | 349 | |
a79d8e93 | 350 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
11b0bacd VB |
351 | MODULE_AUTHOR("Vitaly Bordug"); |
352 | MODULE_LICENSE("GPL"); |