xref: /linux/drivers/net/phy/mdio_bus_provider.c (revision e3cdf6cf5fc6db0643723083e2c70fffe098e249)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO Bus provider interface
3  *
4  * Author: Andy Fleming
5  *
6  * Copyright (c) 2004 Freescale Semiconductor, Inc.
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/init.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/micrel_phy.h>
21 #include <linux/mii.h>
22 #include <linux/mm.h>
23 #include <linux/netdevice.h>
24 #include <linux/of_device.h>
25 #include <linux/of_mdio.h>
26 #include <linux/phy.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/uaccess.h>
30 #include <linux/unistd.h>
31 #include "phylib-internal.h"
32 
33 /**
34  * mdiobus_release - mii_bus device release callback
35  * @d: the target struct device that contains the mii_bus
36  *
37  * Description: called when the last reference to an mii_bus is
38  * dropped, to free the underlying memory.
39  */
40 static void mdiobus_release(struct device *d)
41 {
42 	struct mii_bus *bus = to_mii_bus(d);
43 
44 	WARN(bus->state != MDIOBUS_RELEASED &&
45 	     /* for compatibility with error handling in drivers */
46 	     bus->state != MDIOBUS_ALLOCATED,
47 	     "%s: not in RELEASED or ALLOCATED state\n",
48 	     bus->id);
49 
50 	if (bus->state == MDIOBUS_RELEASED)
51 		fwnode_handle_put(dev_fwnode(d));
52 
53 	kfree(bus);
54 }
55 
56 struct mdio_bus_stat_attr {
57 	struct device_attribute attr;
58 	int address;
59 	unsigned int field_offset;
60 };
61 
62 static struct mdio_bus_stat_attr *to_sattr(struct device_attribute *attr)
63 {
64 	return container_of(attr, struct mdio_bus_stat_attr, attr);
65 }
66 
67 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
68 {
69 	const u64_stats_t *stats = (const void *)s + offset;
70 	unsigned int start;
71 	u64 val = 0;
72 
73 	do {
74 		start = u64_stats_fetch_begin(&s->syncp);
75 		val = u64_stats_read(stats);
76 	} while (u64_stats_fetch_retry(&s->syncp, start));
77 
78 	return val;
79 }
80 
81 static ssize_t mdio_bus_stat_field_show(struct device *dev,
82 					struct device_attribute *attr,
83 					char *buf)
84 {
85 	struct mdio_bus_stat_attr *sattr = to_sattr(attr);
86 	struct mii_bus *bus = to_mii_bus(dev);
87 	u64 val = 0;
88 
89 	if (sattr->address < 0) {
90 		/* get global stats */
91 		for (int i = 0; i < PHY_MAX_ADDR; i++)
92 			val += mdio_bus_get_stat(&bus->stats[i],
93 						 sattr->field_offset);
94 	} else {
95 		val = mdio_bus_get_stat(&bus->stats[sattr->address],
96 					sattr->field_offset);
97 	}
98 
99 	return sysfs_emit(buf, "%llu\n", val);
100 }
101 
102 static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
103 					       struct device_attribute *attr,
104 					       char *buf)
105 {
106 	struct mdio_bus_stat_attr *sattr = to_sattr(attr);
107 	struct mdio_device *mdiodev = to_mdio_device(dev);
108 	struct mii_bus *bus = mdiodev->bus;
109 	int addr = mdiodev->addr;
110 	u64 val;
111 
112 	val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
113 
114 	return sysfs_emit(buf, "%llu\n", val);
115 }
116 
117 #define MDIO_BUS_STATS_ATTR(field)					\
118 static const struct mdio_bus_stat_attr dev_attr_mdio_bus_##field = {	\
119 	.attr = __ATTR(field, 0444, mdio_bus_stat_field_show, NULL),	\
120 	.address = -1,							\
121 	.field_offset = offsetof(struct mdio_bus_stats, field),		\
122 };									\
123 static const struct mdio_bus_stat_attr dev_attr_mdio_bus_device_##field = { \
124 	.attr = __ATTR(field, 0444, mdio_bus_device_stat_field_show, NULL), \
125 	.field_offset = offsetof(struct mdio_bus_stats, field),		\
126 }
127 
128 MDIO_BUS_STATS_ATTR(transfers);
129 MDIO_BUS_STATS_ATTR(errors);
130 MDIO_BUS_STATS_ATTR(writes);
131 MDIO_BUS_STATS_ATTR(reads);
132 
133 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file)		\
134 static const struct mdio_bus_stat_attr					\
135 dev_attr_mdio_bus_addr_##field##_##addr = {				\
136 	.attr = { .attr = { .name = file, .mode = 0444 },		\
137 		     .show = mdio_bus_stat_field_show,			\
138 	},								\
139 	.address = addr,						\
140 	.field_offset = offsetof(struct mdio_bus_stats, field),		\
141 }
142 
143 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr)				\
144 	MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr,			\
145 				 __stringify(field) "_" __stringify(addr))
146 
147 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr)			\
148 	MDIO_BUS_STATS_ADDR_ATTR(transfers, addr);			\
149 	MDIO_BUS_STATS_ADDR_ATTR(errors, addr);				\
150 	MDIO_BUS_STATS_ADDR_ATTR(writes, addr);				\
151 	MDIO_BUS_STATS_ADDR_ATTR(reads, addr)				\
152 
153 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
154 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
155 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
156 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
157 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
158 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
159 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
160 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
161 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
162 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
163 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
164 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
165 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
166 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
167 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
168 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
169 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
170 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
171 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
172 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
173 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
174 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
175 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
176 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
177 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
178 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
179 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
180 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
181 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
182 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
183 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
184 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
185 
186 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr)				\
187 	&(dev_attr_mdio_bus_addr_transfers_##addr).attr.attr,		\
188 	&(dev_attr_mdio_bus_addr_errors_##addr).attr.attr,		\
189 	&(dev_attr_mdio_bus_addr_writes_##addr).attr.attr,		\
190 	&(dev_attr_mdio_bus_addr_reads_##addr).attr.attr			\
191 
192 static const struct attribute *const mdio_bus_statistics_attrs[] = {
193 	&dev_attr_mdio_bus_transfers.attr.attr,
194 	&dev_attr_mdio_bus_errors.attr.attr,
195 	&dev_attr_mdio_bus_writes.attr.attr,
196 	&dev_attr_mdio_bus_reads.attr.attr,
197 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
198 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
199 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
200 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
201 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
202 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
203 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
204 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
205 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
206 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
207 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
208 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
209 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
210 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
211 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
212 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
213 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
214 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
215 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
216 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
217 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
218 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
219 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
220 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
221 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
222 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
223 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
224 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
225 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
226 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
227 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
228 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
229 	NULL,
230 };
231 
232 static const struct attribute_group mdio_bus_statistics_group = {
233 	.name		= "statistics",
234 	.attrs_const	= mdio_bus_statistics_attrs,
235 };
236 __ATTRIBUTE_GROUPS(mdio_bus_statistics);
237 
238 const struct class mdio_bus_class = {
239 	.name		= "mdio_bus",
240 	.dev_release	= mdiobus_release,
241 	.dev_groups	= mdio_bus_statistics_groups,
242 };
243 
244 /**
245  * mdio_bus_match - determine if given MDIO driver supports the given
246  *		    MDIO device
247  * @dev: target MDIO device
248  * @drv: given MDIO driver
249  *
250  * Return: 1 if the driver supports the device, 0 otherwise
251  *
252  * Description: This may require calling the devices own match function,
253  *   since different classes of MDIO devices have different match criteria.
254  */
255 static int mdio_bus_match(struct device *dev, const struct device_driver *drv)
256 {
257 	const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
258 	struct mdio_device *mdio = to_mdio_device(dev);
259 
260 	/* Both the driver and device must type-match */
261 	if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
262 	    !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
263 		return 0;
264 
265 	if (of_driver_match_device(dev, drv))
266 		return 1;
267 
268 	if (mdio->bus_match)
269 		return mdio->bus_match(dev, drv);
270 
271 	return 0;
272 }
273 
274 static int mdio_uevent(const struct device *dev, struct kobj_uevent_env *env)
275 {
276 	int rc;
277 
278 	/* Some devices have extra OF data and an OF-style MODALIAS */
279 	rc = of_device_uevent_modalias(dev, env);
280 	if (rc != -ENODEV)
281 		return rc;
282 
283 	return 0;
284 }
285 
286 static const struct attribute *const mdio_bus_device_statistics_attrs[] = {
287 	&dev_attr_mdio_bus_device_transfers.attr.attr,
288 	&dev_attr_mdio_bus_device_errors.attr.attr,
289 	&dev_attr_mdio_bus_device_writes.attr.attr,
290 	&dev_attr_mdio_bus_device_reads.attr.attr,
291 	NULL,
292 };
293 
294 static const struct attribute_group mdio_bus_device_statistics_group = {
295 	.name		= "statistics",
296 	.attrs_const	= mdio_bus_device_statistics_attrs,
297 };
298 __ATTRIBUTE_GROUPS(mdio_bus_device_statistics);
299 
300 const struct bus_type mdio_bus_type = {
301 	.name		= "mdio_bus",
302 	.dev_groups	= mdio_bus_device_statistics_groups,
303 	.match		= mdio_bus_match,
304 	.uevent		= mdio_uevent,
305 };
306 
307 /**
308  * mdiobus_alloc_size - allocate a mii_bus structure
309  * @size: extra amount of memory to allocate for private storage.
310  * If non-zero, then bus->priv is points to that memory.
311  *
312  * Description: called by a bus driver to allocate an mii_bus
313  * structure to fill in.
314  */
315 struct mii_bus *mdiobus_alloc_size(size_t size)
316 {
317 	struct mii_bus *bus;
318 	size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
319 	size_t alloc_size;
320 	int i;
321 
322 	/* If we alloc extra space, it should be aligned */
323 	if (size)
324 		alloc_size = aligned_size + size;
325 	else
326 		alloc_size = sizeof(*bus);
327 
328 	bus = kzalloc(alloc_size, GFP_KERNEL);
329 	if (!bus)
330 		return NULL;
331 
332 	bus->state = MDIOBUS_ALLOCATED;
333 	if (size)
334 		bus->priv = (void *)bus + aligned_size;
335 
336 	/* Initialise the interrupts to polling and 64-bit seqcounts */
337 	for (i = 0; i < PHY_MAX_ADDR; i++) {
338 		bus->irq[i] = PHY_POLL;
339 		u64_stats_init(&bus->stats[i].syncp);
340 	}
341 
342 	return bus;
343 }
344 EXPORT_SYMBOL(mdiobus_alloc_size);
345 
346 #if IS_ENABLED(CONFIG_OF_MDIO)
347 /* Walk the list of subnodes of a mdio bus and look for a node that
348  * matches the mdio device's address with its 'reg' property. If
349  * found, set the of_node pointer for the mdio device. This allows
350  * auto-probed phy devices to be supplied with information passed in
351  * via DT.
352  * If a PHY package is found, PHY is searched also there.
353  */
354 static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev,
355 			       struct device_node *np)
356 {
357 	struct device_node *child;
358 
359 	for_each_available_child_of_node(np, child) {
360 		int addr;
361 
362 		if (of_node_name_eq(child, "ethernet-phy-package")) {
363 			/* Validate PHY package reg presence */
364 			if (!of_property_present(child, "reg")) {
365 				of_node_put(child);
366 				return -EINVAL;
367 			}
368 
369 			if (!of_mdiobus_find_phy(dev, mdiodev, child)) {
370 				/* The refcount for the PHY package will be
371 				 * incremented later when PHY join the Package.
372 				 */
373 				of_node_put(child);
374 				return 0;
375 			}
376 
377 			continue;
378 		}
379 
380 		addr = of_mdio_parse_addr(dev, child);
381 		if (addr < 0)
382 			continue;
383 
384 		if (addr == mdiodev->addr) {
385 			device_set_node(dev, of_fwnode_handle(child));
386 			/* The refcount on "child" is passed to the mdio
387 			 * device. Do _not_ use of_node_put(child) here.
388 			 */
389 			return 0;
390 		}
391 	}
392 
393 	return -ENODEV;
394 }
395 
396 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
397 				    struct mdio_device *mdiodev)
398 {
399 	struct device *dev = &mdiodev->dev;
400 
401 	if (dev->of_node || !bus->dev.of_node)
402 		return;
403 
404 	of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node);
405 }
406 #endif
407 
408 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
409 {
410 	struct phy_device *phydev = ERR_PTR(-ENODEV);
411 	struct fwnode_handle *fwnode;
412 	char node_name[16];
413 	int err;
414 
415 	phydev = get_phy_device(bus, addr, c45);
416 	if (IS_ERR(phydev))
417 		return phydev;
418 
419 #if IS_ENABLED(CONFIG_OF_MDIO)
420 	/* For DT, see if the auto-probed phy has a corresponding child
421 	 * in the bus node, and set the of_node pointer in this case.
422 	 */
423 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
424 #endif
425 
426 	/* Search for a swnode for the phy in the swnode hierarchy of the bus.
427 	 * If there is no swnode for the phy provided, just ignore it.
428 	 */
429 	if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) {
430 		snprintf(node_name, sizeof(node_name), "ethernet-phy@%d",
431 			 addr);
432 		fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev),
433 						     node_name);
434 		if (fwnode)
435 			device_set_node(&phydev->mdio.dev, fwnode);
436 	}
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 }
446 
447 /**
448  * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
449  * @bus: mii_bus to scan
450  * @addr: address on bus to scan
451  *
452  * This function scans one address on the MDIO bus, looking for
453  * devices which can be identified using a vendor/product ID in
454  * registers 2 and 3. Not all MDIO devices have such registers, but
455  * PHY devices typically do. Hence this function assumes anything
456  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
457  * such as switches, will probably not be found during the scan.
458  */
459 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
460 {
461 	return mdiobus_scan(bus, addr, false);
462 }
463 EXPORT_SYMBOL(mdiobus_scan_c22);
464 
465 /**
466  * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
467  * @bus: mii_bus to scan
468  * @addr: address on bus to scan
469  *
470  * This function scans one address on the MDIO bus, looking for
471  * devices which can be identified using a vendor/product ID in
472  * registers 2 and 3. Not all MDIO devices have such registers, but
473  * PHY devices typically do. Hence this function assumes anything
474  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
475  * such as switches, will probably not be found during the scan.
476  */
477 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
478 {
479 	return mdiobus_scan(bus, addr, true);
480 }
481 
482 static int mdiobus_scan_bus_c22(struct mii_bus *bus)
483 {
484 	int i;
485 
486 	for (i = 0; i < PHY_MAX_ADDR; i++) {
487 		if ((bus->phy_mask & BIT(i)) == 0) {
488 			struct phy_device *phydev;
489 
490 			phydev = mdiobus_scan_c22(bus, i);
491 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
492 				return PTR_ERR(phydev);
493 		}
494 	}
495 	return 0;
496 }
497 
498 static int mdiobus_scan_bus_c45(struct mii_bus *bus)
499 {
500 	int i;
501 
502 	for (i = 0; i < PHY_MAX_ADDR; i++) {
503 		if ((bus->phy_mask & BIT(i)) == 0) {
504 			struct phy_device *phydev;
505 
506 			/* Don't scan C45 if we already have a C22 device */
507 			if (bus->mdio_map[i])
508 				continue;
509 
510 			phydev = mdiobus_scan_c45(bus, i);
511 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
512 				return PTR_ERR(phydev);
513 		}
514 	}
515 	return 0;
516 }
517 
518 /* There are some C22 PHYs which do bad things when where is a C45
519  * transaction on the bus, like accepting a read themselves, and
520  * stomping over the true devices reply, to performing a write to
521  * themselves which was intended for another device. Now that C22
522  * devices have been found, see if any of them are bad for C45, and if we
523  * should skip the C45 scan.
524  */
525 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
526 {
527 	struct phy_device *phydev;
528 
529 	mdiobus_for_each_phy(bus, phydev) {
530 		u32 oui = phydev->phy_id >> 10;
531 
532 		if (oui == MICREL_OUI)
533 			return true;
534 	}
535 
536 	return false;
537 }
538 
539 /**
540  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
541  * @bus: target mii_bus
542  * @owner: module containing bus accessor functions
543  *
544  * Description: Called by a bus driver to bring up all the PHYs
545  *   on a given bus, and attach them to the bus. Drivers should use
546  *   mdiobus_register() rather than __mdiobus_register() unless they
547  *   need to pass a specific owner module. MDIO devices which are not
548  *   PHYs will not be brought up by this function. They are expected
549  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
550  *
551  * Returns 0 on success or < 0 on error.
552  */
553 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
554 {
555 	struct mdio_device *mdiodev;
556 	struct gpio_desc *gpiod;
557 	bool prevent_c45_scan;
558 	int i, err;
559 
560 	if (!bus || !bus->name)
561 		return -EINVAL;
562 
563 	/* An access method always needs both read and write operations */
564 	if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
565 		return -EINVAL;
566 
567 	/* At least one method is mandatory */
568 	if (!bus->read && !bus->read_c45)
569 		return -EINVAL;
570 
571 	if (bus->parent && bus->parent->of_node)
572 		bus->parent->of_node->fwnode.flags |=
573 					FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
574 
575 	WARN(bus->state != MDIOBUS_ALLOCATED &&
576 	     bus->state != MDIOBUS_UNREGISTERED,
577 	     "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
578 
579 	bus->owner = owner;
580 	bus->dev.parent = bus->parent;
581 	bus->dev.class = &mdio_bus_class;
582 	bus->dev.groups = NULL;
583 	dev_set_name(&bus->dev, "%s", bus->id);
584 
585 	/* If the bus state is allocated, we're registering a fresh bus
586 	 * that may have a fwnode associated with it. Grab a reference
587 	 * to the fwnode. This will be dropped when the bus is released.
588 	 * If the bus was set to unregistered, it means that the bus was
589 	 * previously registered, and we've already grabbed a reference.
590 	 */
591 	if (bus->state == MDIOBUS_ALLOCATED)
592 		fwnode_handle_get(dev_fwnode(&bus->dev));
593 
594 	/* We need to set state to MDIOBUS_UNREGISTERED to correctly release
595 	 * the device in mdiobus_free()
596 	 *
597 	 * State will be updated later in this function in case of success
598 	 */
599 	bus->state = MDIOBUS_UNREGISTERED;
600 
601 	err = device_register(&bus->dev);
602 	if (err) {
603 		pr_err("mii_bus %s failed to register\n", bus->id);
604 		return -EINVAL;
605 	}
606 
607 	mutex_init(&bus->mdio_lock);
608 	mutex_init(&bus->shared_lock);
609 
610 	/* assert bus level PHY GPIO reset */
611 	gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
612 	if (IS_ERR(gpiod)) {
613 		err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
614 				    "mii_bus %s couldn't get reset GPIO\n",
615 				    bus->id);
616 		device_del(&bus->dev);
617 		return err;
618 	} else	if (gpiod) {
619 		bus->reset_gpiod = gpiod;
620 		fsleep(bus->reset_delay_us);
621 		gpiod_set_value_cansleep(gpiod, 0);
622 		if (bus->reset_post_delay_us > 0)
623 			fsleep(bus->reset_post_delay_us);
624 	}
625 
626 	if (bus->reset) {
627 		err = bus->reset(bus);
628 		if (err)
629 			goto error_reset_gpiod;
630 	}
631 
632 	if (bus->read) {
633 		err = mdiobus_scan_bus_c22(bus);
634 		if (err)
635 			goto error;
636 	}
637 
638 	prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
639 
640 	if (!prevent_c45_scan && bus->read_c45) {
641 		err = mdiobus_scan_bus_c45(bus);
642 		if (err)
643 			goto error;
644 	}
645 
646 	bus->state = MDIOBUS_REGISTERED;
647 	dev_dbg(&bus->dev, "probed\n");
648 	return 0;
649 
650 error:
651 	for (i = 0; i < PHY_MAX_ADDR; i++) {
652 		mdiodev = bus->mdio_map[i];
653 		if (!mdiodev)
654 			continue;
655 
656 		mdiodev->device_remove(mdiodev);
657 		mdiodev->device_free(mdiodev);
658 	}
659 error_reset_gpiod:
660 	/* Put PHYs in RESET to save power */
661 	if (bus->reset_gpiod)
662 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
663 
664 	device_del(&bus->dev);
665 	return err;
666 }
667 EXPORT_SYMBOL(__mdiobus_register);
668 
669 void mdiobus_unregister(struct mii_bus *bus)
670 {
671 	struct mdio_device *mdiodev;
672 	int i;
673 
674 	if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
675 		return;
676 	bus->state = MDIOBUS_UNREGISTERED;
677 
678 	for (i = 0; i < PHY_MAX_ADDR; i++) {
679 		mdiodev = bus->mdio_map[i];
680 		if (!mdiodev)
681 			continue;
682 
683 		mdiodev->device_remove(mdiodev);
684 		mdiodev->device_free(mdiodev);
685 	}
686 
687 	/* Put PHYs in RESET to save power */
688 	if (bus->reset_gpiod)
689 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
690 
691 	device_del(&bus->dev);
692 }
693 EXPORT_SYMBOL(mdiobus_unregister);
694 
695 /**
696  * mdiobus_free - free a struct mii_bus
697  * @bus: mii_bus to free
698  *
699  * This function releases the reference to the underlying device
700  * object in the mii_bus.  If this is the last reference, the mii_bus
701  * will be freed.
702  */
703 void mdiobus_free(struct mii_bus *bus)
704 {
705 	/* For compatibility with error handling in drivers. */
706 	if (bus->state == MDIOBUS_ALLOCATED) {
707 		kfree(bus);
708 		return;
709 	}
710 
711 	WARN(bus->state != MDIOBUS_UNREGISTERED,
712 	     "%s: not in UNREGISTERED state\n", bus->id);
713 	bus->state = MDIOBUS_RELEASED;
714 
715 	put_device(&bus->dev);
716 }
717 EXPORT_SYMBOL(mdiobus_free);
718 
719 /**
720  * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
721  * @mdio_name: The name of a mdiobus.
722  *
723  * Return: a reference to the mii_bus, or NULL if none found. The
724  * embedded struct device will have its reference count incremented,
725  * and this must be put_deviced'ed once the bus is finished with.
726  */
727 struct mii_bus *mdio_find_bus(const char *mdio_name)
728 {
729 	struct device *d;
730 
731 	d = class_find_device_by_name(&mdio_bus_class, mdio_name);
732 	return d ? to_mii_bus(d) : NULL;
733 }
734 EXPORT_SYMBOL(mdio_find_bus);
735 
736 #if IS_ENABLED(CONFIG_OF_MDIO)
737 /**
738  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
739  * @mdio_bus_np: Pointer to the mii_bus.
740  *
741  * Return: a reference to the mii_bus, or NULL if none found. The
742  * embedded struct device will have its reference count incremented,
743  * and this must be put once the bus is finished with.
744  *
745  * Because the association of a device_node and mii_bus is made via
746  * of_mdiobus_register(), the mii_bus cannot be found before it is
747  * registered with of_mdiobus_register().
748  *
749  */
750 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
751 {
752 	struct device *d;
753 
754 	if (!mdio_bus_np)
755 		return NULL;
756 
757 	d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
758 	return d ? to_mii_bus(d) : NULL;
759 }
760 EXPORT_SYMBOL(of_mdio_find_bus);
761 #endif
762