Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* MDIO Bus interface
2 *
3 * Author: Andy Fleming
4 *
5 * Copyright (c) 2004 Freescale Semiconductor, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/unistd.h>
20#include <linux/slab.h>
21#include <linux/interrupt.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/of_device.h>
26#include <linux/of_mdio.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/skbuff.h>
30#include <linux/spinlock.h>
31#include <linux/mm.h>
32#include <linux/module.h>
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/phy.h>
36#include <linux/io.h>
37#include <linux/uaccess.h>
38
39#include <asm/irq.h>
40
41#define CREATE_TRACE_POINTS
42#include <trace/events/mdio.h>
43
44#include "mdio-boardinfo.h"
45
46int mdiobus_register_device(struct mdio_device *mdiodev)
47{
48 if (mdiodev->bus->mdio_map[mdiodev->addr])
49 return -EBUSY;
50
51 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
52
53 return 0;
54}
55EXPORT_SYMBOL(mdiobus_register_device);
56
57int mdiobus_unregister_device(struct mdio_device *mdiodev)
58{
59 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
60 return -EINVAL;
61
62 mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
63
64 return 0;
65}
66EXPORT_SYMBOL(mdiobus_unregister_device);
67
68struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
69{
70 struct mdio_device *mdiodev = bus->mdio_map[addr];
71
72 if (!mdiodev)
73 return NULL;
74
75 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
76 return NULL;
77
78 return container_of(mdiodev, struct phy_device, mdio);
79}
80EXPORT_SYMBOL(mdiobus_get_phy);
81
82bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
83{
84 return bus->mdio_map[addr];
85}
86EXPORT_SYMBOL(mdiobus_is_registered_device);
87
88/**
89 * mdiobus_alloc_size - allocate a mii_bus structure
90 * @size: extra amount of memory to allocate for private storage.
91 * If non-zero, then bus->priv is points to that memory.
92 *
93 * Description: called by a bus driver to allocate an mii_bus
94 * structure to fill in.
95 */
96struct mii_bus *mdiobus_alloc_size(size_t size)
97{
98 struct mii_bus *bus;
99 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
100 size_t alloc_size;
101 int i;
102
103 /* If we alloc extra space, it should be aligned */
104 if (size)
105 alloc_size = aligned_size + size;
106 else
107 alloc_size = sizeof(*bus);
108
109 bus = kzalloc(alloc_size, GFP_KERNEL);
110 if (!bus)
111 return NULL;
112
113 bus->state = MDIOBUS_ALLOCATED;
114 if (size)
115 bus->priv = (void *)bus + aligned_size;
116
117 /* Initialise the interrupts to polling */
118 for (i = 0; i < PHY_MAX_ADDR; i++)
119 bus->irq[i] = PHY_POLL;
120
121 return bus;
122}
123EXPORT_SYMBOL(mdiobus_alloc_size);
124
125static void _devm_mdiobus_free(struct device *dev, void *res)
126{
127 mdiobus_free(*(struct mii_bus **)res);
128}
129
130static int devm_mdiobus_match(struct device *dev, void *res, void *data)
131{
132 struct mii_bus **r = res;
133
134 if (WARN_ON(!r || !*r))
135 return 0;
136
137 return *r == data;
138}
139
140/**
141 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
142 * @dev: Device to allocate mii_bus for
143 * @sizeof_priv: Space to allocate for private structure.
144 *
145 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
146 * automatically freed on driver detach.
147 *
148 * If an mii_bus allocated with this function needs to be freed separately,
149 * devm_mdiobus_free() must be used.
150 *
151 * RETURNS:
152 * Pointer to allocated mii_bus on success, NULL on failure.
153 */
154struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
155{
156 struct mii_bus **ptr, *bus;
157
158 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
159 if (!ptr)
160 return NULL;
161
162 /* use raw alloc_dr for kmalloc caller tracing */
163 bus = mdiobus_alloc_size(sizeof_priv);
164 if (bus) {
165 *ptr = bus;
166 devres_add(dev, ptr);
167 } else {
168 devres_free(ptr);
169 }
170
171 return bus;
172}
173EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
174
175/**
176 * devm_mdiobus_free - Resource-managed mdiobus_free()
177 * @dev: Device this mii_bus belongs to
178 * @bus: the mii_bus associated with the device
179 *
180 * Free mii_bus allocated with devm_mdiobus_alloc_size().
181 */
182void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
183{
184 int rc;
185
186 rc = devres_release(dev, _devm_mdiobus_free,
187 devm_mdiobus_match, bus);
188 WARN_ON(rc);
189}
190EXPORT_SYMBOL_GPL(devm_mdiobus_free);
191
192/**
193 * mdiobus_release - mii_bus device release callback
194 * @d: the target struct device that contains the mii_bus
195 *
196 * Description: called when the last reference to an mii_bus is
197 * dropped, to free the underlying memory.
198 */
199static void mdiobus_release(struct device *d)
200{
201 struct mii_bus *bus = to_mii_bus(d);
202 BUG_ON(bus->state != MDIOBUS_RELEASED &&
203 /* for compatibility with error handling in drivers */
204 bus->state != MDIOBUS_ALLOCATED);
205 kfree(bus);
206}
207
208static struct class mdio_bus_class = {
209 .name = "mdio_bus",
210 .dev_release = mdiobus_release,
211};
212
213#if IS_ENABLED(CONFIG_OF_MDIO)
214/* Helper function for of_mdio_find_bus */
215static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
216{
217 return dev->of_node == mdio_bus_np;
218}
219/**
220 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
221 * @mdio_bus_np: Pointer to the mii_bus.
222 *
223 * Returns a reference to the mii_bus, or NULL if none found. The
224 * embedded struct device will have its reference count incremented,
225 * and this must be put once the bus is finished with.
226 *
227 * Because the association of a device_node and mii_bus is made via
228 * of_mdiobus_register(), the mii_bus cannot be found before it is
229 * registered with of_mdiobus_register().
230 *
231 */
232struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
233{
234 struct device *d;
235
236 if (!mdio_bus_np)
237 return NULL;
238
239 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
240 of_mdio_bus_match);
241
242 return d ? to_mii_bus(d) : NULL;
243}
244EXPORT_SYMBOL(of_mdio_find_bus);
245
246/* Walk the list of subnodes of a mdio bus and look for a node that
247 * matches the mdio device's address with its 'reg' property. If
248 * found, set the of_node pointer for the mdio device. This allows
249 * auto-probed phy devices to be supplied with information passed in
250 * via DT.
251 */
252static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
253 struct mdio_device *mdiodev)
254{
255 struct device *dev = &mdiodev->dev;
256 struct device_node *child;
257
258 if (dev->of_node || !bus->dev.of_node)
259 return;
260
261 for_each_available_child_of_node(bus->dev.of_node, child) {
262 int addr;
263 int ret;
264
265 ret = of_property_read_u32(child, "reg", &addr);
266 if (ret < 0) {
267 dev_err(dev, "%s has invalid MDIO address\n",
268 child->full_name);
269 continue;
270 }
271
272 /* A MDIO device must have a reg property in the range [0-31] */
273 if (addr >= PHY_MAX_ADDR) {
274 dev_err(dev, "%s MDIO address %i is too large\n",
275 child->full_name, addr);
276 continue;
277 }
278
279 if (addr == mdiodev->addr) {
280 dev->of_node = child;
281 return;
282 }
283 }
284}
285#else /* !IS_ENABLED(CONFIG_OF_MDIO) */
286static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
287 struct mdio_device *mdiodev)
288{
289}
290#endif
291
292/**
293 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
294 * @bus: target mii_bus
295 * @owner: module containing bus accessor functions
296 *
297 * Description: Called by a bus driver to bring up all the PHYs
298 * on a given bus, and attach them to the bus. Drivers should use
299 * mdiobus_register() rather than __mdiobus_register() unless they
300 * need to pass a specific owner module. MDIO devices which are not
301 * PHYs will not be brought up by this function. They are expected to
302 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
303 *
304 * Returns 0 on success or < 0 on error.
305 */
306int __mdiobus_register(struct mii_bus *bus, struct module *owner)
307{
308 struct mdio_device *mdiodev;
309 int i, err;
310
311 if (NULL == bus || NULL == bus->name ||
312 NULL == bus->read || NULL == bus->write)
313 return -EINVAL;
314
315 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
316 bus->state != MDIOBUS_UNREGISTERED);
317
318 bus->owner = owner;
319 bus->dev.parent = bus->parent;
320 bus->dev.class = &mdio_bus_class;
321 bus->dev.groups = NULL;
322 dev_set_name(&bus->dev, "%s", bus->id);
323
324 err = device_register(&bus->dev);
325 if (err) {
326 pr_err("mii_bus %s failed to register\n", bus->id);
327 put_device(&bus->dev);
328 return -EINVAL;
329 }
330
331 mutex_init(&bus->mdio_lock);
332
333 if (bus->reset)
334 bus->reset(bus);
335
336 for (i = 0; i < PHY_MAX_ADDR; i++) {
337 if ((bus->phy_mask & (1 << i)) == 0) {
338 struct phy_device *phydev;
339
340 phydev = mdiobus_scan(bus, i);
341 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
342 err = PTR_ERR(phydev);
343 goto error;
344 }
345 }
346 }
347
348 mdiobus_setup_mdiodev_from_board_info(bus);
349
350 bus->state = MDIOBUS_REGISTERED;
351 pr_info("%s: probed\n", bus->name);
352 return 0;
353
354error:
355 while (--i >= 0) {
356 mdiodev = bus->mdio_map[i];
357 if (!mdiodev)
358 continue;
359
360 mdiodev->device_remove(mdiodev);
361 mdiodev->device_free(mdiodev);
362 }
363 device_del(&bus->dev);
364 return err;
365}
366EXPORT_SYMBOL(__mdiobus_register);
367
368void mdiobus_unregister(struct mii_bus *bus)
369{
370 struct mdio_device *mdiodev;
371 int i;
372
373 BUG_ON(bus->state != MDIOBUS_REGISTERED);
374 bus->state = MDIOBUS_UNREGISTERED;
375
376 for (i = 0; i < PHY_MAX_ADDR; i++) {
377 mdiodev = bus->mdio_map[i];
378 if (!mdiodev)
379 continue;
380
381 mdiodev->device_remove(mdiodev);
382 mdiodev->device_free(mdiodev);
383 }
384 device_del(&bus->dev);
385}
386EXPORT_SYMBOL(mdiobus_unregister);
387
388/**
389 * mdiobus_free - free a struct mii_bus
390 * @bus: mii_bus to free
391 *
392 * This function releases the reference to the underlying device
393 * object in the mii_bus. If this is the last reference, the mii_bus
394 * will be freed.
395 */
396void mdiobus_free(struct mii_bus *bus)
397{
398 /* For compatibility with error handling in drivers. */
399 if (bus->state == MDIOBUS_ALLOCATED) {
400 kfree(bus);
401 return;
402 }
403
404 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
405 bus->state = MDIOBUS_RELEASED;
406
407 put_device(&bus->dev);
408}
409EXPORT_SYMBOL(mdiobus_free);
410
411/**
412 * mdiobus_scan - scan a bus for MDIO devices.
413 * @bus: mii_bus to scan
414 * @addr: address on bus to scan
415 *
416 * This function scans the MDIO bus, looking for devices which can be
417 * identified using a vendor/product ID in registers 2 and 3. Not all
418 * MDIO devices have such registers, but PHY devices typically
419 * do. Hence this function assumes anything found is a PHY, or can be
420 * treated as a PHY. Other MDIO devices, such as switches, will
421 * probably not be found during the scan.
422 */
423struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
424{
425 struct phy_device *phydev;
426 int err;
427
428 phydev = get_phy_device(bus, addr, false);
429 if (IS_ERR(phydev))
430 return phydev;
431
432 /*
433 * For DT, see if the auto-probed phy has a correspoding child
434 * in the bus node, and set the of_node pointer in this case.
435 */
436 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
437
438 err = phy_device_register(phydev);
439 if (err) {
440 phy_device_free(phydev);
441 return ERR_PTR(-ENODEV);
442 }
443
444 return phydev;
445}
446EXPORT_SYMBOL(mdiobus_scan);
447
448/**
449 * mdiobus_read_nested - Nested version of the mdiobus_read function
450 * @bus: the mii_bus struct
451 * @addr: the phy address
452 * @regnum: register number to read
453 *
454 * In case of nested MDIO bus access avoid lockdep false positives by
455 * using mutex_lock_nested().
456 *
457 * NOTE: MUST NOT be called from interrupt context,
458 * because the bus read/write functions may wait for an interrupt
459 * to conclude the operation.
460 */
461int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
462{
463 int retval;
464
465 BUG_ON(in_interrupt());
466
467 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
468 retval = bus->read(bus, addr, regnum);
469 mutex_unlock(&bus->mdio_lock);
470
471 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
472
473 return retval;
474}
475EXPORT_SYMBOL(mdiobus_read_nested);
476
477/**
478 * mdiobus_read - Convenience function for reading a given MII mgmt register
479 * @bus: the mii_bus struct
480 * @addr: the phy address
481 * @regnum: register number to read
482 *
483 * NOTE: MUST NOT be called from interrupt context,
484 * because the bus read/write functions may wait for an interrupt
485 * to conclude the operation.
486 */
487int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
488{
489 int retval;
490
491 BUG_ON(in_interrupt());
492
493 mutex_lock(&bus->mdio_lock);
494 retval = bus->read(bus, addr, regnum);
495 mutex_unlock(&bus->mdio_lock);
496
497 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
498
499 return retval;
500}
501EXPORT_SYMBOL(mdiobus_read);
502
503/**
504 * mdiobus_write_nested - Nested version of the mdiobus_write function
505 * @bus: the mii_bus struct
506 * @addr: the phy address
507 * @regnum: register number to write
508 * @val: value to write to @regnum
509 *
510 * In case of nested MDIO bus access avoid lockdep false positives by
511 * using mutex_lock_nested().
512 *
513 * NOTE: MUST NOT be called from interrupt context,
514 * because the bus read/write functions may wait for an interrupt
515 * to conclude the operation.
516 */
517int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
518{
519 int err;
520
521 BUG_ON(in_interrupt());
522
523 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
524 err = bus->write(bus, addr, regnum, val);
525 mutex_unlock(&bus->mdio_lock);
526
527 trace_mdio_access(bus, 0, addr, regnum, val, err);
528
529 return err;
530}
531EXPORT_SYMBOL(mdiobus_write_nested);
532
533/**
534 * mdiobus_write - Convenience function for writing a given MII mgmt register
535 * @bus: the mii_bus struct
536 * @addr: the phy address
537 * @regnum: register number to write
538 * @val: value to write to @regnum
539 *
540 * NOTE: MUST NOT be called from interrupt context,
541 * because the bus read/write functions may wait for an interrupt
542 * to conclude the operation.
543 */
544int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
545{
546 int err;
547
548 BUG_ON(in_interrupt());
549
550 mutex_lock(&bus->mdio_lock);
551 err = bus->write(bus, addr, regnum, val);
552 mutex_unlock(&bus->mdio_lock);
553
554 trace_mdio_access(bus, 0, addr, regnum, val, err);
555
556 return err;
557}
558EXPORT_SYMBOL(mdiobus_write);
559
560/**
561 * mdio_bus_match - determine if given MDIO driver supports the given
562 * MDIO device
563 * @dev: target MDIO device
564 * @drv: given MDIO driver
565 *
566 * Description: Given a MDIO device, and a MDIO driver, return 1 if
567 * the driver supports the device. Otherwise, return 0. This may
568 * require calling the devices own match function, since different classes
569 * of MDIO devices have different match criteria.
570 */
571static int mdio_bus_match(struct device *dev, struct device_driver *drv)
572{
573 struct mdio_device *mdio = to_mdio_device(dev);
574
575 if (of_driver_match_device(dev, drv))
576 return 1;
577
578 if (mdio->bus_match)
579 return mdio->bus_match(dev, drv);
580
581 return 0;
582}
583
584#ifdef CONFIG_PM
585static int mdio_bus_suspend(struct device *dev)
586{
587 struct mdio_device *mdio = to_mdio_device(dev);
588
589 if (mdio->pm_ops && mdio->pm_ops->suspend)
590 return mdio->pm_ops->suspend(dev);
591
592 return 0;
593}
594
595static int mdio_bus_resume(struct device *dev)
596{
597 struct mdio_device *mdio = to_mdio_device(dev);
598
599 if (mdio->pm_ops && mdio->pm_ops->resume)
600 return mdio->pm_ops->resume(dev);
601
602 return 0;
603}
604
605static int mdio_bus_restore(struct device *dev)
606{
607 struct mdio_device *mdio = to_mdio_device(dev);
608
609 if (mdio->pm_ops && mdio->pm_ops->restore)
610 return mdio->pm_ops->restore(dev);
611
612 return 0;
613}
614
615static const struct dev_pm_ops mdio_bus_pm_ops = {
616 .suspend = mdio_bus_suspend,
617 .resume = mdio_bus_resume,
618 .freeze = mdio_bus_suspend,
619 .thaw = mdio_bus_resume,
620 .restore = mdio_bus_restore,
621};
622
623#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
624
625#else
626
627#define MDIO_BUS_PM_OPS NULL
628
629#endif /* CONFIG_PM */
630
631struct bus_type mdio_bus_type = {
632 .name = "mdio_bus",
633 .match = mdio_bus_match,
634 .pm = MDIO_BUS_PM_OPS,
635};
636EXPORT_SYMBOL(mdio_bus_type);
637
638int __init mdio_bus_init(void)
639{
640 int ret;
641
642 ret = class_register(&mdio_bus_class);
643 if (!ret) {
644 ret = bus_register(&mdio_bus_type);
645 if (ret)
646 class_unregister(&mdio_bus_class);
647 }
648
649 return ret;
650}
651
652void mdio_bus_exit(void)
653{
654 class_unregister(&mdio_bus_class);
655 bus_unregister(&mdio_bus_type);
656}