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 */
mdiobus_alloc_size(size_t size)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 */
of_mdiobus_find_phy(struct device * dev,struct mdio_device * mdiodev,struct device_node * np)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
of_mdiobus_link_mdiodev(struct mii_bus * bus,struct mdio_device * mdiodev)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 */
mdiobus_create_device(struct mii_bus * bus,struct mdio_board_info * bi)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->dev.platform_data = (void *)bi->platform_data;
156
157 ret = mdio_device_register(mdiodev);
158 if (ret)
159 mdio_device_free(mdiodev);
160
161 return ret;
162 }
163
mdiobus_scan(struct mii_bus * bus,int addr,bool c45)164 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
165 {
166 struct phy_device *phydev = ERR_PTR(-ENODEV);
167 struct fwnode_handle *fwnode;
168 char node_name[16];
169 int err;
170
171 phydev = get_phy_device(bus, addr, c45);
172 if (IS_ERR(phydev))
173 return phydev;
174
175 #if IS_ENABLED(CONFIG_OF_MDIO)
176 /* For DT, see if the auto-probed phy has a corresponding child
177 * in the bus node, and set the of_node pointer in this case.
178 */
179 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
180 #endif
181
182 /* Search for a swnode for the phy in the swnode hierarchy of the bus.
183 * If there is no swnode for the phy provided, just ignore it.
184 */
185 if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) {
186 snprintf(node_name, sizeof(node_name), "ethernet-phy@%d",
187 addr);
188 fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev),
189 node_name);
190 if (fwnode)
191 device_set_node(&phydev->mdio.dev, fwnode);
192 }
193
194 err = phy_device_register(phydev);
195 if (err) {
196 phy_device_free(phydev);
197 return ERR_PTR(-ENODEV);
198 }
199
200 return phydev;
201 }
202
203 /**
204 * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
205 * @bus: mii_bus to scan
206 * @addr: address on bus to scan
207 *
208 * This function scans one address on the MDIO bus, looking for
209 * devices which can be identified using a vendor/product ID in
210 * registers 2 and 3. Not all MDIO devices have such registers, but
211 * PHY devices typically do. Hence this function assumes anything
212 * found is a PHY, or can be treated as a PHY. Other MDIO devices,
213 * such as switches, will probably not be found during the scan.
214 */
mdiobus_scan_c22(struct mii_bus * bus,int addr)215 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
216 {
217 return mdiobus_scan(bus, addr, false);
218 }
219 EXPORT_SYMBOL(mdiobus_scan_c22);
220
221 /**
222 * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
223 * @bus: mii_bus to scan
224 * @addr: address on bus to scan
225 *
226 * This function scans one address on the MDIO bus, looking for
227 * devices which can be identified using a vendor/product ID in
228 * registers 2 and 3. Not all MDIO devices have such registers, but
229 * PHY devices typically do. Hence this function assumes anything
230 * found is a PHY, or can be treated as a PHY. Other MDIO devices,
231 * such as switches, will probably not be found during the scan.
232 */
mdiobus_scan_c45(struct mii_bus * bus,int addr)233 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
234 {
235 return mdiobus_scan(bus, addr, true);
236 }
237
mdiobus_scan_bus_c22(struct mii_bus * bus)238 static int mdiobus_scan_bus_c22(struct mii_bus *bus)
239 {
240 int i;
241
242 for (i = 0; i < PHY_MAX_ADDR; i++) {
243 if ((bus->phy_mask & BIT(i)) == 0) {
244 struct phy_device *phydev;
245
246 phydev = mdiobus_scan_c22(bus, i);
247 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
248 return PTR_ERR(phydev);
249 }
250 }
251 return 0;
252 }
253
mdiobus_scan_bus_c45(struct mii_bus * bus)254 static int mdiobus_scan_bus_c45(struct mii_bus *bus)
255 {
256 int i;
257
258 for (i = 0; i < PHY_MAX_ADDR; i++) {
259 if ((bus->phy_mask & BIT(i)) == 0) {
260 struct phy_device *phydev;
261
262 /* Don't scan C45 if we already have a C22 device */
263 if (bus->mdio_map[i])
264 continue;
265
266 phydev = mdiobus_scan_c45(bus, i);
267 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
268 return PTR_ERR(phydev);
269 }
270 }
271 return 0;
272 }
273
274 /* There are some C22 PHYs which do bad things when where is a C45
275 * transaction on the bus, like accepting a read themselves, and
276 * stomping over the true devices reply, to performing a write to
277 * themselves which was intended for another device. Now that C22
278 * devices have been found, see if any of them are bad for C45, and if we
279 * should skip the C45 scan.
280 */
mdiobus_prevent_c45_scan(struct mii_bus * bus)281 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
282 {
283 int i;
284
285 for (i = 0; i < PHY_MAX_ADDR; i++) {
286 struct phy_device *phydev;
287 u32 oui;
288
289 phydev = mdiobus_get_phy(bus, i);
290 if (!phydev)
291 continue;
292 oui = phydev->phy_id >> 10;
293
294 if (oui == MICREL_OUI)
295 return true;
296 }
297 return false;
298 }
299
300 /**
301 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
302 * @bus: target mii_bus
303 * @owner: module containing bus accessor functions
304 *
305 * Description: Called by a bus driver to bring up all the PHYs
306 * on a given bus, and attach them to the bus. Drivers should use
307 * mdiobus_register() rather than __mdiobus_register() unless they
308 * need to pass a specific owner module. MDIO devices which are not
309 * PHYs will not be brought up by this function. They are expected
310 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
311 *
312 * Returns 0 on success or < 0 on error.
313 */
__mdiobus_register(struct mii_bus * bus,struct module * owner)314 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
315 {
316 struct mdio_device *mdiodev;
317 struct gpio_desc *gpiod;
318 bool prevent_c45_scan;
319 int i, err;
320
321 if (!bus || !bus->name)
322 return -EINVAL;
323
324 /* An access method always needs both read and write operations */
325 if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
326 return -EINVAL;
327
328 /* At least one method is mandatory */
329 if (!bus->read && !bus->read_c45)
330 return -EINVAL;
331
332 if (bus->parent && bus->parent->of_node)
333 bus->parent->of_node->fwnode.flags |=
334 FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
335
336 WARN(bus->state != MDIOBUS_ALLOCATED &&
337 bus->state != MDIOBUS_UNREGISTERED,
338 "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
339
340 bus->owner = owner;
341 bus->dev.parent = bus->parent;
342 bus->dev.class = &mdio_bus_class;
343 bus->dev.groups = NULL;
344 dev_set_name(&bus->dev, "%s", bus->id);
345
346 /* If the bus state is allocated, we're registering a fresh bus
347 * that may have a fwnode associated with it. Grab a reference
348 * to the fwnode. This will be dropped when the bus is released.
349 * If the bus was set to unregistered, it means that the bus was
350 * previously registered, and we've already grabbed a reference.
351 */
352 if (bus->state == MDIOBUS_ALLOCATED)
353 fwnode_handle_get(dev_fwnode(&bus->dev));
354
355 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
356 * the device in mdiobus_free()
357 *
358 * State will be updated later in this function in case of success
359 */
360 bus->state = MDIOBUS_UNREGISTERED;
361
362 err = device_register(&bus->dev);
363 if (err) {
364 pr_err("mii_bus %s failed to register\n", bus->id);
365 return -EINVAL;
366 }
367
368 mutex_init(&bus->mdio_lock);
369 mutex_init(&bus->shared_lock);
370
371 /* assert bus level PHY GPIO reset */
372 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
373 if (IS_ERR(gpiod)) {
374 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
375 "mii_bus %s couldn't get reset GPIO\n",
376 bus->id);
377 device_del(&bus->dev);
378 return err;
379 } else if (gpiod) {
380 bus->reset_gpiod = gpiod;
381 fsleep(bus->reset_delay_us);
382 gpiod_set_value_cansleep(gpiod, 0);
383 if (bus->reset_post_delay_us > 0)
384 fsleep(bus->reset_post_delay_us);
385 }
386
387 if (bus->reset) {
388 err = bus->reset(bus);
389 if (err)
390 goto error_reset_gpiod;
391 }
392
393 if (bus->read) {
394 err = mdiobus_scan_bus_c22(bus);
395 if (err)
396 goto error;
397 }
398
399 prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
400
401 if (!prevent_c45_scan && bus->read_c45) {
402 err = mdiobus_scan_bus_c45(bus);
403 if (err)
404 goto error;
405 }
406
407 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
408
409 bus->state = MDIOBUS_REGISTERED;
410 dev_dbg(&bus->dev, "probed\n");
411 return 0;
412
413 error:
414 for (i = 0; i < PHY_MAX_ADDR; i++) {
415 mdiodev = bus->mdio_map[i];
416 if (!mdiodev)
417 continue;
418
419 mdiodev->device_remove(mdiodev);
420 mdiodev->device_free(mdiodev);
421 }
422 error_reset_gpiod:
423 /* Put PHYs in RESET to save power */
424 if (bus->reset_gpiod)
425 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
426
427 device_del(&bus->dev);
428 return err;
429 }
430 EXPORT_SYMBOL(__mdiobus_register);
431
mdiobus_unregister(struct mii_bus * bus)432 void mdiobus_unregister(struct mii_bus *bus)
433 {
434 struct mdio_device *mdiodev;
435 int i;
436
437 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
438 return;
439 bus->state = MDIOBUS_UNREGISTERED;
440
441 for (i = 0; i < PHY_MAX_ADDR; i++) {
442 mdiodev = bus->mdio_map[i];
443 if (!mdiodev)
444 continue;
445
446 if (mdiodev->reset_gpio)
447 gpiod_put(mdiodev->reset_gpio);
448
449 mdiodev->device_remove(mdiodev);
450 mdiodev->device_free(mdiodev);
451 }
452
453 /* Put PHYs in RESET to save power */
454 if (bus->reset_gpiod)
455 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
456
457 device_del(&bus->dev);
458 }
459 EXPORT_SYMBOL(mdiobus_unregister);
460
461 /**
462 * mdiobus_free - free a struct mii_bus
463 * @bus: mii_bus to free
464 *
465 * This function releases the reference to the underlying device
466 * object in the mii_bus. If this is the last reference, the mii_bus
467 * will be freed.
468 */
mdiobus_free(struct mii_bus * bus)469 void mdiobus_free(struct mii_bus *bus)
470 {
471 /* For compatibility with error handling in drivers. */
472 if (bus->state == MDIOBUS_ALLOCATED) {
473 kfree(bus);
474 return;
475 }
476
477 WARN(bus->state != MDIOBUS_UNREGISTERED,
478 "%s: not in UNREGISTERED state\n", bus->id);
479 bus->state = MDIOBUS_RELEASED;
480
481 put_device(&bus->dev);
482 }
483 EXPORT_SYMBOL(mdiobus_free);
484