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