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