xref: /linux/drivers/net/phy/mdio_bus_provider.c (revision 1b98f357dadd6ea613a435fbaef1a5dd7b35fd21)
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 
32 #include "mdio-boardinfo.h"
33 
34 /**
35  * mdiobus_alloc_size - allocate a mii_bus structure
36  * @size: extra amount of memory to allocate for private storage.
37  * If non-zero, then bus->priv is points to that memory.
38  *
39  * Description: called by a bus driver to allocate an mii_bus
40  * structure to fill in.
41  */
42 struct mii_bus *mdiobus_alloc_size(size_t size)
43 {
44 	struct mii_bus *bus;
45 	size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
46 	size_t alloc_size;
47 	int i;
48 
49 	/* If we alloc extra space, it should be aligned */
50 	if (size)
51 		alloc_size = aligned_size + size;
52 	else
53 		alloc_size = sizeof(*bus);
54 
55 	bus = kzalloc(alloc_size, GFP_KERNEL);
56 	if (!bus)
57 		return NULL;
58 
59 	bus->state = MDIOBUS_ALLOCATED;
60 	if (size)
61 		bus->priv = (void *)bus + aligned_size;
62 
63 	/* Initialise the interrupts to polling and 64-bit seqcounts */
64 	for (i = 0; i < PHY_MAX_ADDR; i++) {
65 		bus->irq[i] = PHY_POLL;
66 		u64_stats_init(&bus->stats[i].syncp);
67 	}
68 
69 	return bus;
70 }
71 EXPORT_SYMBOL(mdiobus_alloc_size);
72 
73 #if IS_ENABLED(CONFIG_OF_MDIO)
74 /* Walk the list of subnodes of a mdio bus and look for a node that
75  * matches the mdio device's address with its 'reg' property. If
76  * found, set the of_node pointer for the mdio device. This allows
77  * auto-probed phy devices to be supplied with information passed in
78  * via DT.
79  * If a PHY package is found, PHY is searched also there.
80  */
81 static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev,
82 			       struct device_node *np)
83 {
84 	struct device_node *child;
85 
86 	for_each_available_child_of_node(np, child) {
87 		int addr;
88 
89 		if (of_node_name_eq(child, "ethernet-phy-package")) {
90 			/* Validate PHY package reg presence */
91 			if (!of_property_present(child, "reg")) {
92 				of_node_put(child);
93 				return -EINVAL;
94 			}
95 
96 			if (!of_mdiobus_find_phy(dev, mdiodev, child)) {
97 				/* The refcount for the PHY package will be
98 				 * incremented later when PHY join the Package.
99 				 */
100 				of_node_put(child);
101 				return 0;
102 			}
103 
104 			continue;
105 		}
106 
107 		addr = of_mdio_parse_addr(dev, child);
108 		if (addr < 0)
109 			continue;
110 
111 		if (addr == mdiodev->addr) {
112 			device_set_node(dev, of_fwnode_handle(child));
113 			/* The refcount on "child" is passed to the mdio
114 			 * device. Do _not_ use of_node_put(child) here.
115 			 */
116 			return 0;
117 		}
118 	}
119 
120 	return -ENODEV;
121 }
122 
123 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
124 				    struct mdio_device *mdiodev)
125 {
126 	struct device *dev = &mdiodev->dev;
127 
128 	if (dev->of_node || !bus->dev.of_node)
129 		return;
130 
131 	of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node);
132 }
133 #endif
134 
135 /**
136  * mdiobus_create_device - create a full MDIO device given
137  * a mdio_board_info structure
138  * @bus: MDIO bus to create the devices on
139  * @bi: mdio_board_info structure describing the devices
140  *
141  * Returns 0 on success or < 0 on error.
142  */
143 static int mdiobus_create_device(struct mii_bus *bus,
144 				 struct mdio_board_info *bi)
145 {
146 	struct mdio_device *mdiodev;
147 	int ret = 0;
148 
149 	mdiodev = mdio_device_create(bus, bi->mdio_addr);
150 	if (IS_ERR(mdiodev))
151 		return -ENODEV;
152 
153 	strscpy(mdiodev->modalias, bi->modalias,
154 		sizeof(mdiodev->modalias));
155 	mdiodev->bus_match = mdio_device_bus_match;
156 	mdiodev->dev.platform_data = (void *)bi->platform_data;
157 
158 	ret = mdio_device_register(mdiodev);
159 	if (ret)
160 		mdio_device_free(mdiodev);
161 
162 	return ret;
163 }
164 
165 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
166 {
167 	struct phy_device *phydev = ERR_PTR(-ENODEV);
168 	struct fwnode_handle *fwnode;
169 	char node_name[16];
170 	int err;
171 
172 	phydev = get_phy_device(bus, addr, c45);
173 	if (IS_ERR(phydev))
174 		return phydev;
175 
176 #if IS_ENABLED(CONFIG_OF_MDIO)
177 	/* For DT, see if the auto-probed phy has a corresponding child
178 	 * in the bus node, and set the of_node pointer in this case.
179 	 */
180 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
181 #endif
182 
183 	/* Search for a swnode for the phy in the swnode hierarchy of the bus.
184 	 * If there is no swnode for the phy provided, just ignore it.
185 	 */
186 	if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) {
187 		snprintf(node_name, sizeof(node_name), "ethernet-phy@%d",
188 			 addr);
189 		fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev),
190 						     node_name);
191 		if (fwnode)
192 			device_set_node(&phydev->mdio.dev, fwnode);
193 	}
194 
195 	err = phy_device_register(phydev);
196 	if (err) {
197 		phy_device_free(phydev);
198 		return ERR_PTR(-ENODEV);
199 	}
200 
201 	return phydev;
202 }
203 
204 /**
205  * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
206  * @bus: mii_bus to scan
207  * @addr: address on bus to scan
208  *
209  * This function scans one address on the MDIO bus, looking for
210  * devices which can be identified using a vendor/product ID in
211  * registers 2 and 3. Not all MDIO devices have such registers, but
212  * PHY devices typically do. Hence this function assumes anything
213  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
214  * such as switches, will probably not be found during the scan.
215  */
216 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
217 {
218 	return mdiobus_scan(bus, addr, false);
219 }
220 EXPORT_SYMBOL(mdiobus_scan_c22);
221 
222 /**
223  * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
224  * @bus: mii_bus to scan
225  * @addr: address on bus to scan
226  *
227  * This function scans one address on the MDIO bus, looking for
228  * devices which can be identified using a vendor/product ID in
229  * registers 2 and 3. Not all MDIO devices have such registers, but
230  * PHY devices typically do. Hence this function assumes anything
231  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
232  * such as switches, will probably not be found during the scan.
233  */
234 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
235 {
236 	return mdiobus_scan(bus, addr, true);
237 }
238 
239 static int mdiobus_scan_bus_c22(struct mii_bus *bus)
240 {
241 	int i;
242 
243 	for (i = 0; i < PHY_MAX_ADDR; i++) {
244 		if ((bus->phy_mask & BIT(i)) == 0) {
245 			struct phy_device *phydev;
246 
247 			phydev = mdiobus_scan_c22(bus, i);
248 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
249 				return PTR_ERR(phydev);
250 		}
251 	}
252 	return 0;
253 }
254 
255 static int mdiobus_scan_bus_c45(struct mii_bus *bus)
256 {
257 	int i;
258 
259 	for (i = 0; i < PHY_MAX_ADDR; i++) {
260 		if ((bus->phy_mask & BIT(i)) == 0) {
261 			struct phy_device *phydev;
262 
263 			/* Don't scan C45 if we already have a C22 device */
264 			if (bus->mdio_map[i])
265 				continue;
266 
267 			phydev = mdiobus_scan_c45(bus, i);
268 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
269 				return PTR_ERR(phydev);
270 		}
271 	}
272 	return 0;
273 }
274 
275 /* There are some C22 PHYs which do bad things when where is a C45
276  * transaction on the bus, like accepting a read themselves, and
277  * stomping over the true devices reply, to performing a write to
278  * themselves which was intended for another device. Now that C22
279  * devices have been found, see if any of them are bad for C45, and if we
280  * should skip the C45 scan.
281  */
282 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
283 {
284 	int i;
285 
286 	for (i = 0; i < PHY_MAX_ADDR; i++) {
287 		struct phy_device *phydev;
288 		u32 oui;
289 
290 		phydev = mdiobus_get_phy(bus, i);
291 		if (!phydev)
292 			continue;
293 		oui = phydev->phy_id >> 10;
294 
295 		if (oui == MICREL_OUI)
296 			return true;
297 	}
298 	return false;
299 }
300 
301 /**
302  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
303  * @bus: target mii_bus
304  * @owner: module containing bus accessor functions
305  *
306  * Description: Called by a bus driver to bring up all the PHYs
307  *   on a given bus, and attach them to the bus. Drivers should use
308  *   mdiobus_register() rather than __mdiobus_register() unless they
309  *   need to pass a specific owner module. MDIO devices which are not
310  *   PHYs will not be brought up by this function. They are expected
311  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
312  *
313  * Returns 0 on success or < 0 on error.
314  */
315 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
316 {
317 	struct mdio_device *mdiodev;
318 	struct gpio_desc *gpiod;
319 	bool prevent_c45_scan;
320 	int i, err;
321 
322 	if (!bus || !bus->name)
323 		return -EINVAL;
324 
325 	/* An access method always needs both read and write operations */
326 	if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
327 		return -EINVAL;
328 
329 	/* At least one method is mandatory */
330 	if (!bus->read && !bus->read_c45)
331 		return -EINVAL;
332 
333 	if (bus->parent && bus->parent->of_node)
334 		bus->parent->of_node->fwnode.flags |=
335 					FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
336 
337 	WARN(bus->state != MDIOBUS_ALLOCATED &&
338 	     bus->state != MDIOBUS_UNREGISTERED,
339 	     "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
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 	/* If the bus state is allocated, we're registering a fresh bus
348 	 * that may have a fwnode associated with it. Grab a reference
349 	 * to the fwnode. This will be dropped when the bus is released.
350 	 * If the bus was set to unregistered, it means that the bus was
351 	 * previously registered, and we've already grabbed a reference.
352 	 */
353 	if (bus->state == MDIOBUS_ALLOCATED)
354 		fwnode_handle_get(dev_fwnode(&bus->dev));
355 
356 	/* We need to set state to MDIOBUS_UNREGISTERED to correctly release
357 	 * the device in mdiobus_free()
358 	 *
359 	 * State will be updated later in this function in case of success
360 	 */
361 	bus->state = MDIOBUS_UNREGISTERED;
362 
363 	err = device_register(&bus->dev);
364 	if (err) {
365 		pr_err("mii_bus %s failed to register\n", bus->id);
366 		return -EINVAL;
367 	}
368 
369 	mutex_init(&bus->mdio_lock);
370 	mutex_init(&bus->shared_lock);
371 
372 	/* assert bus level PHY GPIO reset */
373 	gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
374 	if (IS_ERR(gpiod)) {
375 		err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
376 				    "mii_bus %s couldn't get reset GPIO\n",
377 				    bus->id);
378 		device_del(&bus->dev);
379 		return err;
380 	} else	if (gpiod) {
381 		bus->reset_gpiod = gpiod;
382 		fsleep(bus->reset_delay_us);
383 		gpiod_set_value_cansleep(gpiod, 0);
384 		if (bus->reset_post_delay_us > 0)
385 			fsleep(bus->reset_post_delay_us);
386 	}
387 
388 	if (bus->reset) {
389 		err = bus->reset(bus);
390 		if (err)
391 			goto error_reset_gpiod;
392 	}
393 
394 	if (bus->read) {
395 		err = mdiobus_scan_bus_c22(bus);
396 		if (err)
397 			goto error;
398 	}
399 
400 	prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
401 
402 	if (!prevent_c45_scan && bus->read_c45) {
403 		err = mdiobus_scan_bus_c45(bus);
404 		if (err)
405 			goto error;
406 	}
407 
408 	mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
409 
410 	bus->state = MDIOBUS_REGISTERED;
411 	dev_dbg(&bus->dev, "probed\n");
412 	return 0;
413 
414 error:
415 	for (i = 0; i < PHY_MAX_ADDR; i++) {
416 		mdiodev = bus->mdio_map[i];
417 		if (!mdiodev)
418 			continue;
419 
420 		mdiodev->device_remove(mdiodev);
421 		mdiodev->device_free(mdiodev);
422 	}
423 error_reset_gpiod:
424 	/* Put PHYs in RESET to save power */
425 	if (bus->reset_gpiod)
426 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
427 
428 	device_del(&bus->dev);
429 	return err;
430 }
431 EXPORT_SYMBOL(__mdiobus_register);
432 
433 void mdiobus_unregister(struct mii_bus *bus)
434 {
435 	struct mdio_device *mdiodev;
436 	int i;
437 
438 	if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
439 		return;
440 	bus->state = MDIOBUS_UNREGISTERED;
441 
442 	for (i = 0; i < PHY_MAX_ADDR; i++) {
443 		mdiodev = bus->mdio_map[i];
444 		if (!mdiodev)
445 			continue;
446 
447 		if (mdiodev->reset_gpio)
448 			gpiod_put(mdiodev->reset_gpio);
449 
450 		mdiodev->device_remove(mdiodev);
451 		mdiodev->device_free(mdiodev);
452 	}
453 
454 	/* Put PHYs in RESET to save power */
455 	if (bus->reset_gpiod)
456 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
457 
458 	device_del(&bus->dev);
459 }
460 EXPORT_SYMBOL(mdiobus_unregister);
461 
462 /**
463  * mdiobus_free - free a struct mii_bus
464  * @bus: mii_bus to free
465  *
466  * This function releases the reference to the underlying device
467  * object in the mii_bus.  If this is the last reference, the mii_bus
468  * will be freed.
469  */
470 void mdiobus_free(struct mii_bus *bus)
471 {
472 	/* For compatibility with error handling in drivers. */
473 	if (bus->state == MDIOBUS_ALLOCATED) {
474 		kfree(bus);
475 		return;
476 	}
477 
478 	WARN(bus->state != MDIOBUS_UNREGISTERED,
479 	     "%s: not in UNREGISTERED state\n", bus->id);
480 	bus->state = MDIOBUS_RELEASED;
481 
482 	put_device(&bus->dev);
483 }
484 EXPORT_SYMBOL(mdiobus_free);
485