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