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