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