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 | |
a79d8e93 | 51 | static int 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 | ||
5ae68b0c | 56 | return 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 | |
a79d8e93 VB |
163 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
164 | if (!fp) | |
165 | return -ENOMEM; | |
11b0bacd | 166 | |
a79d8e93 | 167 | memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM); |
11b0bacd | 168 | |
185be5ae RV |
169 | if (irq != PHY_POLL) |
170 | fmb->mii_bus->irq[phy_addr] = irq; | |
11b0bacd | 171 | |
9b744942 | 172 | fp->addr = phy_addr; |
a79d8e93 | 173 | fp->status = *status; |
a5597008 AL |
174 | fp->link_gpio = link_gpio; |
175 | ||
176 | if (gpio_is_valid(fp->link_gpio)) { | |
177 | ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN, | |
178 | "fixed-link-gpio-link"); | |
179 | if (ret) | |
180 | goto err_regs; | |
181 | } | |
7c32f470 | 182 | |
a79d8e93 VB |
183 | ret = fixed_phy_update_regs(fp); |
184 | if (ret) | |
a5597008 | 185 | goto err_gpio; |
11b0bacd | 186 | |
a79d8e93 | 187 | list_add_tail(&fp->node, &fmb->phys); |
7c32f470 | 188 | |
a79d8e93 | 189 | return 0; |
11b0bacd | 190 | |
a5597008 AL |
191 | err_gpio: |
192 | if (gpio_is_valid(fp->link_gpio)) | |
193 | gpio_free(fp->link_gpio); | |
a79d8e93 VB |
194 | err_regs: |
195 | kfree(fp); | |
196 | return ret; | |
197 | } | |
198 | EXPORT_SYMBOL_GPL(fixed_phy_add); | |
11b0bacd | 199 | |
5bcbe0f3 | 200 | static void fixed_phy_del(int phy_addr) |
a7595121 TP |
201 | { |
202 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
203 | struct fixed_phy *fp, *tmp; | |
204 | ||
205 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { | |
206 | if (fp->addr == phy_addr) { | |
207 | list_del(&fp->node); | |
a5597008 AL |
208 | if (gpio_is_valid(fp->link_gpio)) |
209 | gpio_free(fp->link_gpio); | |
a7595121 TP |
210 | kfree(fp); |
211 | return; | |
212 | } | |
213 | } | |
214 | } | |
a7595121 TP |
215 | |
216 | static int phy_fixed_addr; | |
217 | static DEFINE_SPINLOCK(phy_fixed_addr_lock); | |
218 | ||
fd2ef0ba PG |
219 | struct phy_device *fixed_phy_register(unsigned int irq, |
220 | struct fixed_phy_status *status, | |
a5597008 | 221 | int link_gpio, |
fd2ef0ba | 222 | struct device_node *np) |
a7595121 TP |
223 | { |
224 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
225 | struct phy_device *phy; | |
226 | int phy_addr; | |
227 | int ret; | |
228 | ||
185be5ae RV |
229 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
230 | return ERR_PTR(-EPROBE_DEFER); | |
231 | ||
a7595121 TP |
232 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
233 | spin_lock(&phy_fixed_addr_lock); | |
234 | if (phy_fixed_addr == PHY_MAX_ADDR) { | |
235 | spin_unlock(&phy_fixed_addr_lock); | |
fd2ef0ba | 236 | return ERR_PTR(-ENOSPC); |
a7595121 TP |
237 | } |
238 | phy_addr = phy_fixed_addr++; | |
239 | spin_unlock(&phy_fixed_addr_lock); | |
240 | ||
bd1a05ee | 241 | ret = fixed_phy_add(irq, phy_addr, status, link_gpio); |
a7595121 | 242 | if (ret < 0) |
fd2ef0ba | 243 | return ERR_PTR(ret); |
a7595121 TP |
244 | |
245 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); | |
4914a584 | 246 | if (IS_ERR(phy)) { |
a7595121 | 247 | fixed_phy_del(phy_addr); |
fd2ef0ba | 248 | return ERR_PTR(-EINVAL); |
a7595121 TP |
249 | } |
250 | ||
4b195360 MB |
251 | /* propagate the fixed link values to struct phy_device */ |
252 | phy->link = status->link; | |
253 | if (status->link) { | |
254 | phy->speed = status->speed; | |
255 | phy->duplex = status->duplex; | |
256 | phy->pause = status->pause; | |
257 | phy->asym_pause = status->asym_pause; | |
258 | } | |
259 | ||
a7595121 | 260 | of_node_get(np); |
e5a03bfd | 261 | phy->mdio.dev.of_node = np; |
5a11dd7d | 262 | phy->is_pseudo_fixed_link = true; |
a7595121 | 263 | |
34b31da4 AL |
264 | switch (status->speed) { |
265 | case SPEED_1000: | |
266 | phy->supported = PHY_1000BT_FEATURES; | |
267 | break; | |
268 | case SPEED_100: | |
269 | phy->supported = PHY_100BT_FEATURES; | |
270 | break; | |
271 | case SPEED_10: | |
272 | default: | |
273 | phy->supported = PHY_10BT_FEATURES; | |
274 | } | |
275 | ||
a7595121 TP |
276 | ret = phy_device_register(phy); |
277 | if (ret) { | |
278 | phy_device_free(phy); | |
279 | of_node_put(np); | |
280 | fixed_phy_del(phy_addr); | |
fd2ef0ba | 281 | return ERR_PTR(ret); |
a7595121 TP |
282 | } |
283 | ||
fd2ef0ba | 284 | return phy; |
a7595121 | 285 | } |
37e9a690 | 286 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
a7595121 | 287 | |
5bcbe0f3 AL |
288 | void fixed_phy_unregister(struct phy_device *phy) |
289 | { | |
290 | phy_device_remove(phy); | |
291 | ||
292 | fixed_phy_del(phy->mdio.addr); | |
293 | } | |
294 | EXPORT_SYMBOL_GPL(fixed_phy_unregister); | |
295 | ||
a79d8e93 VB |
296 | static int __init fixed_mdio_bus_init(void) |
297 | { | |
298 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
299 | int ret; | |
11b0bacd | 300 | |
a79d8e93 | 301 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
57401d5e DC |
302 | if (IS_ERR(pdev)) { |
303 | ret = PTR_ERR(pdev); | |
a79d8e93 VB |
304 | goto err_pdev; |
305 | } | |
11b0bacd | 306 | |
298cf9be LB |
307 | fmb->mii_bus = mdiobus_alloc(); |
308 | if (fmb->mii_bus == NULL) { | |
309 | ret = -ENOMEM; | |
310 | goto err_mdiobus_reg; | |
311 | } | |
11b0bacd | 312 | |
9e6c643b | 313 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
298cf9be | 314 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
ec2a5652 | 315 | fmb->mii_bus->priv = fmb; |
298cf9be LB |
316 | fmb->mii_bus->parent = &pdev->dev; |
317 | fmb->mii_bus->read = &fixed_mdio_read; | |
318 | fmb->mii_bus->write = &fixed_mdio_write; | |
298cf9be LB |
319 | |
320 | ret = mdiobus_register(fmb->mii_bus); | |
a79d8e93 | 321 | if (ret) |
298cf9be | 322 | goto err_mdiobus_alloc; |
11b0bacd | 323 | |
a79d8e93 | 324 | return 0; |
11b0bacd | 325 | |
298cf9be LB |
326 | err_mdiobus_alloc: |
327 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 VB |
328 | err_mdiobus_reg: |
329 | platform_device_unregister(pdev); | |
330 | err_pdev: | |
331 | return ret; | |
332 | } | |
333 | module_init(fixed_mdio_bus_init); | |
11b0bacd | 334 | |
a79d8e93 VB |
335 | static void __exit fixed_mdio_bus_exit(void) |
336 | { | |
337 | struct fixed_mdio_bus *fmb = &platform_fmb; | |
651be3a2 | 338 | struct fixed_phy *fp, *tmp; |
11b0bacd | 339 | |
298cf9be LB |
340 | mdiobus_unregister(fmb->mii_bus); |
341 | mdiobus_free(fmb->mii_bus); | |
a79d8e93 | 342 | platform_device_unregister(pdev); |
11b0bacd | 343 | |
651be3a2 | 344 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
a79d8e93 VB |
345 | list_del(&fp->node); |
346 | kfree(fp); | |
7c32f470 | 347 | } |
11b0bacd | 348 | } |
a79d8e93 | 349 | module_exit(fixed_mdio_bus_exit); |
11b0bacd | 350 | |
a79d8e93 | 351 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
11b0bacd VB |
352 | MODULE_AUTHOR("Vitaly Bordug"); |
353 | MODULE_LICENSE("GPL"); |