netdev/phy/of: Handle IEEE802.3 clause 45 Ethernet PHYs in of_mdiobus_register()
[deliverable/linux.git] / drivers / net / phy / mdio_bus.c
CommitLineData
00db8189
AF
1/*
2 * drivers/net/phy/mdio_bus.c
3 *
4 * MDIO Bus interface
5 *
6 * Author: Andy Fleming
7 *
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
8d242488
JP
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
00db8189 19#include <linux/kernel.h>
00db8189
AF
20#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/slab.h>
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
3d1e4db2 27#include <linux/device.h>
00db8189
AF
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
31#include <linux/spinlock.h>
32#include <linux/mm.h>
33#include <linux/module.h>
00db8189
AF
34#include <linux/mii.h>
35#include <linux/ethtool.h>
36#include <linux/phy.h>
37
38#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41
298cf9be 42/**
eb8a54a7 43 * mdiobus_alloc_size - allocate a mii_bus structure
af58f1d6
RD
44 * @size: extra amount of memory to allocate for private storage.
45 * If non-zero, then bus->priv is points to that memory.
298cf9be
LB
46 *
47 * Description: called by a bus driver to allocate an mii_bus
48 * structure to fill in.
49 */
eb8a54a7 50struct mii_bus *mdiobus_alloc_size(size_t size)
298cf9be 51{
46abc021 52 struct mii_bus *bus;
eb8a54a7
TT
53 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
54 size_t alloc_size;
55
56 /* If we alloc extra space, it should be aligned */
57 if (size)
58 alloc_size = aligned_size + size;
59 else
60 alloc_size = sizeof(*bus);
46abc021 61
eb8a54a7
TT
62 bus = kzalloc(alloc_size, GFP_KERNEL);
63 if (bus) {
46abc021 64 bus->state = MDIOBUS_ALLOCATED;
eb8a54a7
TT
65 if (size)
66 bus->priv = (void *)bus + aligned_size;
67 }
46abc021
LB
68
69 return bus;
298cf9be 70}
eb8a54a7 71EXPORT_SYMBOL(mdiobus_alloc_size);
298cf9be 72
46abc021
LB
73/**
74 * mdiobus_release - mii_bus device release callback
78c36b15 75 * @d: the target struct device that contains the mii_bus
46abc021
LB
76 *
77 * Description: called when the last reference to an mii_bus is
78 * dropped, to free the underlying memory.
79 */
80static void mdiobus_release(struct device *d)
81{
82 struct mii_bus *bus = to_mii_bus(d);
161c8d2f
KH
83 BUG_ON(bus->state != MDIOBUS_RELEASED &&
84 /* for compatibility with error handling in drivers */
85 bus->state != MDIOBUS_ALLOCATED);
46abc021
LB
86 kfree(bus);
87}
88
89static struct class mdio_bus_class = {
90 .name = "mdio_bus",
91 .dev_release = mdiobus_release,
92};
93
b943fbb0 94#if IS_ENABLED(CONFIG_OF_MDIO)
25106022
DD
95/* Helper function for of_mdio_find_bus */
96static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np)
97{
98 return dev->of_node == mdio_bus_np;
99}
100/**
101 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
f41ef2e7 102 * @mdio_bus_np: Pointer to the mii_bus.
25106022
DD
103 *
104 * Returns a pointer to the mii_bus, or NULL if none found.
105 *
106 * Because the association of a device_node and mii_bus is made via
107 * of_mdiobus_register(), the mii_bus cannot be found before it is
108 * registered with of_mdiobus_register().
109 *
110 */
111struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
112{
113 struct device *d;
114
115 if (!mdio_bus_np)
116 return NULL;
117
118 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
119 of_mdio_bus_match);
120
121 return d ? to_mii_bus(d) : NULL;
122}
123EXPORT_SYMBOL(of_mdio_find_bus);
124#endif
125
b3df0da8
RD
126/**
127 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
128 * @bus: target mii_bus
e1393456 129 *
b3df0da8
RD
130 * Description: Called by a bus driver to bring up all the PHYs
131 * on a given bus, and attach them to the bus.
132 *
133 * Returns 0 on success or < 0 on error.
e1393456
AF
134 */
135int mdiobus_register(struct mii_bus *bus)
136{
161c8d2f 137 int i, err;
e1393456 138
e1393456
AF
139 if (NULL == bus || NULL == bus->name ||
140 NULL == bus->read ||
141 NULL == bus->write)
142 return -EINVAL;
143
46abc021
LB
144 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
145 bus->state != MDIOBUS_UNREGISTERED);
146
147 bus->dev.parent = bus->parent;
148 bus->dev.class = &mdio_bus_class;
149 bus->dev.groups = NULL;
036b6687 150 dev_set_name(&bus->dev, "%s", bus->id);
46abc021
LB
151
152 err = device_register(&bus->dev);
153 if (err) {
8d242488 154 pr_err("mii_bus %s failed to register\n", bus->id);
46abc021
LB
155 return -EINVAL;
156 }
157
d1e7fe4d
AB
158 mutex_init(&bus->mdio_lock);
159
e1393456
AF
160 if (bus->reset)
161 bus->reset(bus);
162
163 for (i = 0; i < PHY_MAX_ADDR; i++) {
4fd5f812
LB
164 if ((bus->phy_mask & (1 << i)) == 0) {
165 struct phy_device *phydev;
e1393456 166
4fd5f812 167 phydev = mdiobus_scan(bus, i);
161c8d2f 168 if (IS_ERR(phydev)) {
4fd5f812 169 err = PTR_ERR(phydev);
161c8d2f
KH
170 goto error;
171 }
64b1c2b4 172 }
e1393456
AF
173 }
174
161c8d2f 175 bus->state = MDIOBUS_REGISTERED;
e1393456 176 pr_info("%s: probed\n", bus->name);
161c8d2f 177 return 0;
e1393456 178
161c8d2f
KH
179error:
180 while (--i >= 0) {
181 if (bus->phy_map[i])
182 device_unregister(&bus->phy_map[i]->dev);
183 }
184 device_del(&bus->dev);
e1393456
AF
185 return err;
186}
187EXPORT_SYMBOL(mdiobus_register);
188
189void mdiobus_unregister(struct mii_bus *bus)
190{
191 int i;
192
46abc021
LB
193 BUG_ON(bus->state != MDIOBUS_REGISTERED);
194 bus->state = MDIOBUS_UNREGISTERED;
195
3e44017b 196 device_del(&bus->dev);
e1393456 197 for (i = 0; i < PHY_MAX_ADDR; i++) {
6f4a7f41 198 if (bus->phy_map[i])
e1393456 199 device_unregister(&bus->phy_map[i]->dev);
4dea547f 200 bus->phy_map[i] = NULL;
e1393456
AF
201 }
202}
203EXPORT_SYMBOL(mdiobus_unregister);
204
298cf9be
LB
205/**
206 * mdiobus_free - free a struct mii_bus
207 * @bus: mii_bus to free
208 *
46abc021
LB
209 * This function releases the reference to the underlying device
210 * object in the mii_bus. If this is the last reference, the mii_bus
211 * will be freed.
298cf9be
LB
212 */
213void mdiobus_free(struct mii_bus *bus)
214{
46abc021
LB
215 /*
216 * For compatibility with error handling in drivers.
217 */
218 if (bus->state == MDIOBUS_ALLOCATED) {
219 kfree(bus);
220 return;
221 }
222
223 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
224 bus->state = MDIOBUS_RELEASED;
225
226 put_device(&bus->dev);
298cf9be
LB
227}
228EXPORT_SYMBOL(mdiobus_free);
229
4fd5f812
LB
230struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
231{
232 struct phy_device *phydev;
233 int err;
234
ac28b9f8 235 phydev = get_phy_device(bus, addr, false);
4fd5f812
LB
236 if (IS_ERR(phydev) || phydev == NULL)
237 return phydev;
238
4dea547f 239 err = phy_device_register(phydev);
4fd5f812 240 if (err) {
4fd5f812 241 phy_device_free(phydev);
4dea547f 242 return NULL;
4fd5f812
LB
243 }
244
4fd5f812
LB
245 return phydev;
246}
247EXPORT_SYMBOL(mdiobus_scan);
248
2e888103
LB
249/**
250 * mdiobus_read - Convenience function for reading a given MII mgmt register
251 * @bus: the mii_bus struct
252 * @addr: the phy address
253 * @regnum: register number to read
254 *
255 * NOTE: MUST NOT be called from interrupt context,
256 * because the bus read/write functions may wait for an interrupt
257 * to conclude the operation.
258 */
abf35df2 259int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
2e888103
LB
260{
261 int retval;
262
263 BUG_ON(in_interrupt());
264
265 mutex_lock(&bus->mdio_lock);
266 retval = bus->read(bus, addr, regnum);
267 mutex_unlock(&bus->mdio_lock);
268
269 return retval;
270}
271EXPORT_SYMBOL(mdiobus_read);
272
273/**
274 * mdiobus_write - Convenience function for writing a given MII mgmt register
275 * @bus: the mii_bus struct
276 * @addr: the phy address
277 * @regnum: register number to write
278 * @val: value to write to @regnum
279 *
280 * NOTE: MUST NOT be called from interrupt context,
281 * because the bus read/write functions may wait for an interrupt
282 * to conclude the operation.
283 */
abf35df2 284int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
2e888103
LB
285{
286 int err;
287
288 BUG_ON(in_interrupt());
289
290 mutex_lock(&bus->mdio_lock);
291 err = bus->write(bus, addr, regnum, val);
292 mutex_unlock(&bus->mdio_lock);
293
294 return err;
295}
296EXPORT_SYMBOL(mdiobus_write);
297
b3df0da8
RD
298/**
299 * mdio_bus_match - determine if given PHY driver supports the given PHY device
300 * @dev: target PHY device
301 * @drv: given PHY driver
00db8189 302 *
b3df0da8
RD
303 * Description: Given a PHY device, and a PHY driver, return 1 if
304 * the driver supports the device. Otherwise, return 0.
00db8189
AF
305 */
306static int mdio_bus_match(struct device *dev, struct device_driver *drv)
307{
308 struct phy_device *phydev = to_phy_device(dev);
309 struct phy_driver *phydrv = to_phy_driver(drv);
310
5f708dd9
KG
311 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
312 (phydev->phy_id & phydrv->phy_id_mask));
00db8189
AF
313}
314
2f5cb434
AV
315#ifdef CONFIG_PM
316
3d1e4db2
AV
317static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
318{
319 struct device_driver *drv = phydev->dev.driver;
320 struct phy_driver *phydrv = to_phy_driver(drv);
321 struct net_device *netdev = phydev->attached_dev;
322
323 if (!drv || !phydrv->suspend)
324 return false;
325
326 /* PHY not attached? May suspend. */
327 if (!netdev)
328 return true;
329
330 /*
331 * Don't suspend PHY if the attched netdev parent may wakeup.
332 * The parent may point to a PCI device, as in tg3 driver.
333 */
334 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
335 return false;
336
337 /*
338 * Also don't suspend PHY if the netdev itself may wakeup. This
339 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
340 * e.g. SoC devices.
341 */
342 if (device_may_wakeup(&netdev->dev))
343 return false;
344
345 return true;
346}
347
2f5cb434 348static int mdio_bus_suspend(struct device *dev)
00db8189 349{
3d1e4db2 350 struct phy_driver *phydrv = to_phy_driver(dev->driver);
0f0ca340 351 struct phy_device *phydev = to_phy_device(dev);
00db8189 352
541cd3ee
AV
353 /*
354 * We must stop the state machine manually, otherwise it stops out of
355 * control, possibly with the phydev->lock held. Upon resume, netdev
356 * may call phy routines that try to grab the same lock, and that may
357 * lead to a deadlock.
358 */
fddd9101 359 if (phydev->attached_dev && phydev->adjust_link)
541cd3ee
AV
360 phy_stop_machine(phydev);
361
3d1e4db2
AV
362 if (!mdio_bus_phy_may_suspend(phydev))
363 return 0;
541cd3ee 364
3d1e4db2 365 return phydrv->suspend(phydev);
00db8189
AF
366}
367
2f5cb434 368static int mdio_bus_resume(struct device *dev)
00db8189 369{
3d1e4db2 370 struct phy_driver *phydrv = to_phy_driver(dev->driver);
0f0ca340 371 struct phy_device *phydev = to_phy_device(dev);
541cd3ee 372 int ret;
00db8189 373
3d1e4db2 374 if (!mdio_bus_phy_may_suspend(phydev))
541cd3ee
AV
375 goto no_resume;
376
377 ret = phydrv->resume(phydev);
378 if (ret < 0)
379 return ret;
380
381no_resume:
fddd9101 382 if (phydev->attached_dev && phydev->adjust_link)
541cd3ee
AV
383 phy_start_machine(phydev, NULL);
384
385 return 0;
00db8189
AF
386}
387
2f5cb434
AV
388static int mdio_bus_restore(struct device *dev)
389{
390 struct phy_device *phydev = to_phy_device(dev);
391 struct net_device *netdev = phydev->attached_dev;
392 int ret;
393
394 if (!netdev)
395 return 0;
396
397 ret = phy_init_hw(phydev);
398 if (ret < 0)
399 return ret;
400
401 /* The PHY needs to renegotiate. */
402 phydev->link = 0;
403 phydev->state = PHY_UP;
404
405 phy_start_machine(phydev, NULL);
406
407 return 0;
408}
409
410static struct dev_pm_ops mdio_bus_pm_ops = {
411 .suspend = mdio_bus_suspend,
412 .resume = mdio_bus_resume,
413 .freeze = mdio_bus_suspend,
414 .thaw = mdio_bus_resume,
415 .restore = mdio_bus_restore,
416};
417
418#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
419
420#else
421
422#define MDIO_BUS_PM_OPS NULL
423
424#endif /* CONFIG_PM */
425
00db8189
AF
426struct bus_type mdio_bus_type = {
427 .name = "mdio_bus",
428 .match = mdio_bus_match,
2f5cb434 429 .pm = MDIO_BUS_PM_OPS,
00db8189 430};
11b0bacd 431EXPORT_SYMBOL(mdio_bus_type);
00db8189 432
67c4f3fa 433int __init mdio_bus_init(void)
00db8189 434{
46abc021
LB
435 int ret;
436
437 ret = class_register(&mdio_bus_class);
438 if (!ret) {
439 ret = bus_register(&mdio_bus_type);
440 if (ret)
441 class_unregister(&mdio_bus_class);
442 }
443
444 return ret;
00db8189
AF
445}
446
dc85dec6 447void mdio_bus_exit(void)
e1393456 448{
46abc021 449 class_unregister(&mdio_bus_class);
e1393456
AF
450 bus_unregister(&mdio_bus_type);
451}
This page took 0.748244 seconds and 5 git commands to generate.