• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO Bus 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/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/micrel_phy.h>
22 #include <linux/mii.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/of_device.h>
27 #include <linux/of_mdio.h>
28 #include <linux/phy.h>
29 #include <linux/reset.h>
30 #include <linux/skbuff.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
34 #include <linux/uaccess.h>
35 #include <linux/unistd.h>
36 
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/mdio.h>
39 
40 #include "mdio-boardinfo.h"
41 
mdiobus_register_gpiod(struct mdio_device * mdiodev)42 static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
43 {
44 	/* Deassert the optional reset signal */
45 	mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
46 						 "reset", GPIOD_OUT_LOW);
47 	if (IS_ERR(mdiodev->reset_gpio))
48 		return PTR_ERR(mdiodev->reset_gpio);
49 
50 	if (mdiodev->reset_gpio)
51 		gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
52 
53 	return 0;
54 }
55 
mdiobus_register_reset(struct mdio_device * mdiodev)56 static int mdiobus_register_reset(struct mdio_device *mdiodev)
57 {
58 	struct reset_control *reset;
59 
60 	reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
61 	if (IS_ERR(reset))
62 		return PTR_ERR(reset);
63 
64 	mdiodev->reset_ctrl = reset;
65 
66 	return 0;
67 }
68 
mdiobus_register_device(struct mdio_device * mdiodev)69 int mdiobus_register_device(struct mdio_device *mdiodev)
70 {
71 	int err;
72 
73 	if (mdiodev->bus->mdio_map[mdiodev->addr])
74 		return -EBUSY;
75 
76 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
77 		err = mdiobus_register_gpiod(mdiodev);
78 		if (err)
79 			return err;
80 
81 		err = mdiobus_register_reset(mdiodev);
82 		if (err)
83 			return err;
84 
85 		/* Assert the reset signal */
86 		mdio_device_reset(mdiodev, 1);
87 	}
88 
89 	mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
90 
91 	return 0;
92 }
93 EXPORT_SYMBOL(mdiobus_register_device);
94 
mdiobus_unregister_device(struct mdio_device * mdiodev)95 int mdiobus_unregister_device(struct mdio_device *mdiodev)
96 {
97 	if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
98 		return -EINVAL;
99 
100 	gpiod_put(mdiodev->reset_gpio);
101 	reset_control_put(mdiodev->reset_ctrl);
102 
103 	mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
104 
105 	return 0;
106 }
107 EXPORT_SYMBOL(mdiobus_unregister_device);
108 
mdiobus_find_device(struct mii_bus * bus,int addr)109 static struct mdio_device *mdiobus_find_device(struct mii_bus *bus, int addr)
110 {
111 	bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map);
112 
113 	if (WARN_ONCE(!addr_valid, "addr %d out of range\n", addr))
114 		return NULL;
115 
116 	return bus->mdio_map[addr];
117 }
118 
mdiobus_get_phy(struct mii_bus * bus,int addr)119 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
120 {
121 	struct mdio_device *mdiodev;
122 
123 	mdiodev = mdiobus_find_device(bus, addr);
124 	if (!mdiodev)
125 		return NULL;
126 
127 	if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
128 		return NULL;
129 
130 	return container_of(mdiodev, struct phy_device, mdio);
131 }
132 EXPORT_SYMBOL(mdiobus_get_phy);
133 
mdiobus_is_registered_device(struct mii_bus * bus,int addr)134 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
135 {
136 	return mdiobus_find_device(bus, addr) != NULL;
137 }
138 EXPORT_SYMBOL(mdiobus_is_registered_device);
139 
140 /**
141  * mdiobus_alloc_size - allocate a mii_bus structure
142  * @size: extra amount of memory to allocate for private storage.
143  * If non-zero, then bus->priv is points to that memory.
144  *
145  * Description: called by a bus driver to allocate an mii_bus
146  * structure to fill in.
147  */
mdiobus_alloc_size(size_t size)148 struct mii_bus *mdiobus_alloc_size(size_t size)
149 {
150 	struct mii_bus *bus;
151 	size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
152 	size_t alloc_size;
153 	int i;
154 
155 	/* If we alloc extra space, it should be aligned */
156 	if (size)
157 		alloc_size = aligned_size + size;
158 	else
159 		alloc_size = sizeof(*bus);
160 
161 	bus = kzalloc(alloc_size, GFP_KERNEL);
162 	if (!bus)
163 		return NULL;
164 
165 	bus->state = MDIOBUS_ALLOCATED;
166 	if (size)
167 		bus->priv = (void *)bus + aligned_size;
168 
169 	/* Initialise the interrupts to polling and 64-bit seqcounts */
170 	for (i = 0; i < PHY_MAX_ADDR; i++) {
171 		bus->irq[i] = PHY_POLL;
172 		u64_stats_init(&bus->stats[i].syncp);
173 	}
174 
175 	return bus;
176 }
177 EXPORT_SYMBOL(mdiobus_alloc_size);
178 
179 /**
180  * mdiobus_release - mii_bus device release callback
181  * @d: the target struct device that contains the mii_bus
182  *
183  * Description: called when the last reference to an mii_bus is
184  * dropped, to free the underlying memory.
185  */
mdiobus_release(struct device * d)186 static void mdiobus_release(struct device *d)
187 {
188 	struct mii_bus *bus = to_mii_bus(d);
189 
190 	WARN(bus->state != MDIOBUS_RELEASED &&
191 	     /* for compatibility with error handling in drivers */
192 	     bus->state != MDIOBUS_ALLOCATED,
193 	     "%s: not in RELEASED or ALLOCATED state\n",
194 	     bus->id);
195 
196 	if (bus->state == MDIOBUS_RELEASED)
197 		fwnode_handle_put(dev_fwnode(d));
198 
199 	kfree(bus);
200 }
201 
202 struct mdio_bus_stat_attr {
203 	int addr;
204 	unsigned int field_offset;
205 };
206 
mdio_bus_get_stat(struct mdio_bus_stats * s,unsigned int offset)207 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
208 {
209 	const char *p = (const char *)s + offset;
210 	unsigned int start;
211 	u64 val = 0;
212 
213 	do {
214 		start = u64_stats_fetch_begin(&s->syncp);
215 		val = u64_stats_read((const u64_stats_t *)p);
216 	} while (u64_stats_fetch_retry(&s->syncp, start));
217 
218 	return val;
219 }
220 
mdio_bus_get_global_stat(struct mii_bus * bus,unsigned int offset)221 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
222 {
223 	unsigned int i;
224 	u64 val = 0;
225 
226 	for (i = 0; i < PHY_MAX_ADDR; i++)
227 		val += mdio_bus_get_stat(&bus->stats[i], offset);
228 
229 	return val;
230 }
231 
mdio_bus_stat_field_show(struct device * dev,struct device_attribute * attr,char * buf)232 static ssize_t mdio_bus_stat_field_show(struct device *dev,
233 					struct device_attribute *attr,
234 					char *buf)
235 {
236 	struct mii_bus *bus = to_mii_bus(dev);
237 	struct mdio_bus_stat_attr *sattr;
238 	struct dev_ext_attribute *eattr;
239 	u64 val;
240 
241 	eattr = container_of(attr, struct dev_ext_attribute, attr);
242 	sattr = eattr->var;
243 
244 	if (sattr->addr < 0)
245 		val = mdio_bus_get_global_stat(bus, sattr->field_offset);
246 	else
247 		val = mdio_bus_get_stat(&bus->stats[sattr->addr],
248 					sattr->field_offset);
249 
250 	return sysfs_emit(buf, "%llu\n", val);
251 }
252 
mdio_bus_device_stat_field_show(struct device * dev,struct device_attribute * attr,char * buf)253 static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
254 					       struct device_attribute *attr,
255 					       char *buf)
256 {
257 	struct mdio_device *mdiodev = to_mdio_device(dev);
258 	struct mii_bus *bus = mdiodev->bus;
259 	struct mdio_bus_stat_attr *sattr;
260 	struct dev_ext_attribute *eattr;
261 	int addr = mdiodev->addr;
262 	u64 val;
263 
264 	eattr = container_of(attr, struct dev_ext_attribute, attr);
265 	sattr = eattr->var;
266 
267 	val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
268 
269 	return sysfs_emit(buf, "%llu\n", val);
270 }
271 
272 #define MDIO_BUS_STATS_ATTR_DECL(field, file)				\
273 static struct dev_ext_attribute dev_attr_mdio_bus_##field = {		\
274 	.attr = { .attr = { .name = file, .mode = 0444 },		\
275 		     .show = mdio_bus_stat_field_show,			\
276 	},								\
277 	.var = &((struct mdio_bus_stat_attr) {				\
278 		-1, offsetof(struct mdio_bus_stats, field)		\
279 	}),								\
280 };									\
281 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = {	\
282 	.attr = { .attr = { .name = file, .mode = 0444 },		\
283 		     .show = mdio_bus_device_stat_field_show,		\
284 	},								\
285 	.var = &((struct mdio_bus_stat_attr) {				\
286 		-1, offsetof(struct mdio_bus_stats, field)		\
287 	}),								\
288 };
289 
290 #define MDIO_BUS_STATS_ATTR(field)					\
291 	MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
292 
293 MDIO_BUS_STATS_ATTR(transfers);
294 MDIO_BUS_STATS_ATTR(errors);
295 MDIO_BUS_STATS_ATTR(writes);
296 MDIO_BUS_STATS_ATTR(reads);
297 
298 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file)		\
299 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
300 	.attr = { .attr = { .name = file, .mode = 0444 },		\
301 		     .show = mdio_bus_stat_field_show,			\
302 	},								\
303 	.var = &((struct mdio_bus_stat_attr) {				\
304 		addr, offsetof(struct mdio_bus_stats, field)		\
305 	}),								\
306 }
307 
308 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr)				\
309 	MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr,			\
310 				 __stringify(field) "_" __stringify(addr))
311 
312 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr)			\
313 	MDIO_BUS_STATS_ADDR_ATTR(transfers, addr);			\
314 	MDIO_BUS_STATS_ADDR_ATTR(errors, addr);				\
315 	MDIO_BUS_STATS_ADDR_ATTR(writes, addr);				\
316 	MDIO_BUS_STATS_ADDR_ATTR(reads, addr)				\
317 
318 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
319 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
320 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
321 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
322 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
323 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
324 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
325 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
326 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
327 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
328 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
329 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
330 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
331 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
332 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
333 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
334 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
335 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
336 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
337 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
338 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
339 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
340 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
341 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
342 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
343 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
344 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
345 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
346 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
347 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
348 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
349 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
350 
351 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr)				\
352 	&dev_attr_mdio_bus_addr_transfers_##addr.attr.attr,		\
353 	&dev_attr_mdio_bus_addr_errors_##addr.attr.attr,		\
354 	&dev_attr_mdio_bus_addr_writes_##addr.attr.attr,		\
355 	&dev_attr_mdio_bus_addr_reads_##addr.attr.attr			\
356 
357 static struct attribute *mdio_bus_statistics_attrs[] = {
358 	&dev_attr_mdio_bus_transfers.attr.attr,
359 	&dev_attr_mdio_bus_errors.attr.attr,
360 	&dev_attr_mdio_bus_writes.attr.attr,
361 	&dev_attr_mdio_bus_reads.attr.attr,
362 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
363 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
364 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
365 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
366 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
367 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
368 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
369 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
370 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
371 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
372 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
373 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
374 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
375 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
376 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
377 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
378 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
379 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
380 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
381 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
382 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
383 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
384 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
385 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
386 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
387 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
388 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
389 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
390 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
391 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
392 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
393 	MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
394 	NULL,
395 };
396 
397 static const struct attribute_group mdio_bus_statistics_group = {
398 	.name	= "statistics",
399 	.attrs	= mdio_bus_statistics_attrs,
400 };
401 
402 static const struct attribute_group *mdio_bus_groups[] = {
403 	&mdio_bus_statistics_group,
404 	NULL,
405 };
406 
407 static struct class mdio_bus_class = {
408 	.name		= "mdio_bus",
409 	.dev_release	= mdiobus_release,
410 	.dev_groups	= mdio_bus_groups,
411 };
412 
413 /**
414  * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
415  * @mdio_name: The name of a mdiobus.
416  *
417  * Returns a reference to the mii_bus, or NULL if none found.  The
418  * embedded struct device will have its reference count incremented,
419  * and this must be put_deviced'ed once the bus is finished with.
420  */
mdio_find_bus(const char * mdio_name)421 struct mii_bus *mdio_find_bus(const char *mdio_name)
422 {
423 	struct device *d;
424 
425 	d = class_find_device_by_name(&mdio_bus_class, mdio_name);
426 	return d ? to_mii_bus(d) : NULL;
427 }
428 EXPORT_SYMBOL(mdio_find_bus);
429 
430 #if IS_ENABLED(CONFIG_OF_MDIO)
431 /**
432  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
433  * @mdio_bus_np: Pointer to the mii_bus.
434  *
435  * Returns a reference to the mii_bus, or NULL if none found.  The
436  * embedded struct device will have its reference count incremented,
437  * and this must be put once the bus is finished with.
438  *
439  * Because the association of a device_node and mii_bus is made via
440  * of_mdiobus_register(), the mii_bus cannot be found before it is
441  * registered with of_mdiobus_register().
442  *
443  */
of_mdio_find_bus(struct device_node * mdio_bus_np)444 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
445 {
446 	struct device *d;
447 
448 	if (!mdio_bus_np)
449 		return NULL;
450 
451 	d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
452 	return d ? to_mii_bus(d) : NULL;
453 }
454 EXPORT_SYMBOL(of_mdio_find_bus);
455 
456 /* Walk the list of subnodes of a mdio bus and look for a node that
457  * matches the mdio device's address with its 'reg' property. If
458  * found, set the of_node pointer for the mdio device. This allows
459  * auto-probed phy devices to be supplied with information passed in
460  * via DT.
461  * If a PHY package is found, PHY is searched also there.
462  */
of_mdiobus_find_phy(struct device * dev,struct mdio_device * mdiodev,struct device_node * np)463 static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev,
464 			       struct device_node *np)
465 {
466 	struct device_node *child;
467 
468 	for_each_available_child_of_node(np, child) {
469 		int addr;
470 
471 		if (of_node_name_eq(child, "ethernet-phy-package")) {
472 			/* Validate PHY package reg presence */
473 			if (!of_property_present(child, "reg")) {
474 				of_node_put(child);
475 				return -EINVAL;
476 			}
477 
478 			if (!of_mdiobus_find_phy(dev, mdiodev, child)) {
479 				/* The refcount for the PHY package will be
480 				 * incremented later when PHY join the Package.
481 				 */
482 				of_node_put(child);
483 				return 0;
484 			}
485 
486 			continue;
487 		}
488 
489 		addr = of_mdio_parse_addr(dev, child);
490 		if (addr < 0)
491 			continue;
492 
493 		if (addr == mdiodev->addr) {
494 			device_set_node(dev, of_fwnode_handle(child));
495 			/* The refcount on "child" is passed to the mdio
496 			 * device. Do _not_ use of_node_put(child) here.
497 			 */
498 			return 0;
499 		}
500 	}
501 
502 	return -ENODEV;
503 }
504 
of_mdiobus_link_mdiodev(struct mii_bus * bus,struct mdio_device * mdiodev)505 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
506 				    struct mdio_device *mdiodev)
507 {
508 	struct device *dev = &mdiodev->dev;
509 
510 	if (dev->of_node || !bus->dev.of_node)
511 		return;
512 
513 	of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node);
514 }
515 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
of_mdiobus_link_mdiodev(struct mii_bus * mdio,struct mdio_device * mdiodev)516 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
517 					   struct mdio_device *mdiodev)
518 {
519 }
520 #endif
521 
522 /**
523  * mdiobus_create_device - create a full MDIO device given
524  * a mdio_board_info structure
525  * @bus: MDIO bus to create the devices on
526  * @bi: mdio_board_info structure describing the devices
527  *
528  * Returns 0 on success or < 0 on error.
529  */
mdiobus_create_device(struct mii_bus * bus,struct mdio_board_info * bi)530 static int mdiobus_create_device(struct mii_bus *bus,
531 				 struct mdio_board_info *bi)
532 {
533 	struct mdio_device *mdiodev;
534 	int ret = 0;
535 
536 	mdiodev = mdio_device_create(bus, bi->mdio_addr);
537 	if (IS_ERR(mdiodev))
538 		return -ENODEV;
539 
540 	strscpy(mdiodev->modalias, bi->modalias,
541 		sizeof(mdiodev->modalias));
542 	mdiodev->bus_match = mdio_device_bus_match;
543 	mdiodev->dev.platform_data = (void *)bi->platform_data;
544 
545 	ret = mdio_device_register(mdiodev);
546 	if (ret)
547 		mdio_device_free(mdiodev);
548 
549 	return ret;
550 }
551 
mdiobus_scan(struct mii_bus * bus,int addr,bool c45)552 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
553 {
554 	struct phy_device *phydev = ERR_PTR(-ENODEV);
555 	int err;
556 
557 	phydev = get_phy_device(bus, addr, c45);
558 	if (IS_ERR(phydev))
559 		return phydev;
560 
561 	/* For DT, see if the auto-probed phy has a corresponding child
562 	 * in the bus node, and set the of_node pointer in this case.
563 	 */
564 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
565 
566 	err = phy_device_register(phydev);
567 	if (err) {
568 		phy_device_free(phydev);
569 		return ERR_PTR(-ENODEV);
570 	}
571 
572 	return phydev;
573 }
574 
575 /**
576  * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
577  * @bus: mii_bus to scan
578  * @addr: address on bus to scan
579  *
580  * This function scans one address on the MDIO bus, looking for
581  * devices which can be identified using a vendor/product ID in
582  * registers 2 and 3. Not all MDIO devices have such registers, but
583  * PHY devices typically do. Hence this function assumes anything
584  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
585  * such as switches, will probably not be found during the scan.
586  */
mdiobus_scan_c22(struct mii_bus * bus,int addr)587 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
588 {
589 	return mdiobus_scan(bus, addr, false);
590 }
591 EXPORT_SYMBOL(mdiobus_scan_c22);
592 
593 /**
594  * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
595  * @bus: mii_bus to scan
596  * @addr: address on bus to scan
597  *
598  * This function scans one address on the MDIO bus, looking for
599  * devices which can be identified using a vendor/product ID in
600  * registers 2 and 3. Not all MDIO devices have such registers, but
601  * PHY devices typically do. Hence this function assumes anything
602  * found is a PHY, or can be treated as a PHY. Other MDIO devices,
603  * such as switches, will probably not be found during the scan.
604  */
mdiobus_scan_c45(struct mii_bus * bus,int addr)605 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
606 {
607 	return mdiobus_scan(bus, addr, true);
608 }
609 
mdiobus_scan_bus_c22(struct mii_bus * bus)610 static int mdiobus_scan_bus_c22(struct mii_bus *bus)
611 {
612 	int i;
613 
614 	for (i = 0; i < PHY_MAX_ADDR; i++) {
615 		if ((bus->phy_mask & BIT(i)) == 0) {
616 			struct phy_device *phydev;
617 
618 			phydev = mdiobus_scan_c22(bus, i);
619 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
620 				return PTR_ERR(phydev);
621 		}
622 	}
623 	return 0;
624 }
625 
mdiobus_scan_bus_c45(struct mii_bus * bus)626 static int mdiobus_scan_bus_c45(struct mii_bus *bus)
627 {
628 	int i;
629 
630 	for (i = 0; i < PHY_MAX_ADDR; i++) {
631 		if ((bus->phy_mask & BIT(i)) == 0) {
632 			struct phy_device *phydev;
633 
634 			/* Don't scan C45 if we already have a C22 device */
635 			if (bus->mdio_map[i])
636 				continue;
637 
638 			phydev = mdiobus_scan_c45(bus, i);
639 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
640 				return PTR_ERR(phydev);
641 		}
642 	}
643 	return 0;
644 }
645 
646 /* There are some C22 PHYs which do bad things when where is a C45
647  * transaction on the bus, like accepting a read themselves, and
648  * stomping over the true devices reply, to performing a write to
649  * themselves which was intended for another device. Now that C22
650  * devices have been found, see if any of them are bad for C45, and if we
651  * should skip the C45 scan.
652  */
mdiobus_prevent_c45_scan(struct mii_bus * bus)653 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
654 {
655 	int i;
656 
657 	for (i = 0; i < PHY_MAX_ADDR; i++) {
658 		struct phy_device *phydev;
659 		u32 oui;
660 
661 		phydev = mdiobus_get_phy(bus, i);
662 		if (!phydev)
663 			continue;
664 		oui = phydev->phy_id >> 10;
665 
666 		if (oui == MICREL_OUI)
667 			return true;
668 	}
669 	return false;
670 }
671 
672 /**
673  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
674  * @bus: target mii_bus
675  * @owner: module containing bus accessor functions
676  *
677  * Description: Called by a bus driver to bring up all the PHYs
678  *   on a given bus, and attach them to the bus. Drivers should use
679  *   mdiobus_register() rather than __mdiobus_register() unless they
680  *   need to pass a specific owner module. MDIO devices which are not
681  *   PHYs will not be brought up by this function. They are expected
682  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
683  *
684  * Returns 0 on success or < 0 on error.
685  */
__mdiobus_register(struct mii_bus * bus,struct module * owner)686 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
687 {
688 	struct mdio_device *mdiodev;
689 	struct gpio_desc *gpiod;
690 	bool prevent_c45_scan;
691 	int i, err;
692 
693 	if (!bus || !bus->name)
694 		return -EINVAL;
695 
696 	/* An access method always needs both read and write operations */
697 	if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
698 		return -EINVAL;
699 
700 	/* At least one method is mandatory */
701 	if (!bus->read && !bus->read_c45)
702 		return -EINVAL;
703 
704 	if (bus->parent && bus->parent->of_node)
705 		bus->parent->of_node->fwnode.flags |=
706 					FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
707 
708 	WARN(bus->state != MDIOBUS_ALLOCATED &&
709 	     bus->state != MDIOBUS_UNREGISTERED,
710 	     "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
711 
712 	bus->owner = owner;
713 	bus->dev.parent = bus->parent;
714 	bus->dev.class = &mdio_bus_class;
715 	bus->dev.groups = NULL;
716 	dev_set_name(&bus->dev, "%s", bus->id);
717 
718 	/* If the bus state is allocated, we're registering a fresh bus
719 	 * that may have a fwnode associated with it. Grab a reference
720 	 * to the fwnode. This will be dropped when the bus is released.
721 	 * If the bus was set to unregistered, it means that the bus was
722 	 * previously registered, and we've already grabbed a reference.
723 	 */
724 	if (bus->state == MDIOBUS_ALLOCATED)
725 		fwnode_handle_get(dev_fwnode(&bus->dev));
726 
727 	/* We need to set state to MDIOBUS_UNREGISTERED to correctly release
728 	 * the device in mdiobus_free()
729 	 *
730 	 * State will be updated later in this function in case of success
731 	 */
732 	bus->state = MDIOBUS_UNREGISTERED;
733 
734 	err = device_register(&bus->dev);
735 	if (err) {
736 		pr_err("mii_bus %s failed to register\n", bus->id);
737 		return -EINVAL;
738 	}
739 
740 	mutex_init(&bus->mdio_lock);
741 	mutex_init(&bus->shared_lock);
742 
743 	/* assert bus level PHY GPIO reset */
744 	gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
745 	if (IS_ERR(gpiod)) {
746 		err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
747 				    "mii_bus %s couldn't get reset GPIO\n",
748 				    bus->id);
749 		device_del(&bus->dev);
750 		return err;
751 	} else	if (gpiod) {
752 		bus->reset_gpiod = gpiod;
753 		fsleep(bus->reset_delay_us);
754 		gpiod_set_value_cansleep(gpiod, 0);
755 		if (bus->reset_post_delay_us > 0)
756 			fsleep(bus->reset_post_delay_us);
757 	}
758 
759 	if (bus->reset) {
760 		err = bus->reset(bus);
761 		if (err)
762 			goto error_reset_gpiod;
763 	}
764 
765 	if (bus->read) {
766 		err = mdiobus_scan_bus_c22(bus);
767 		if (err)
768 			goto error;
769 	}
770 
771 	prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
772 
773 	if (!prevent_c45_scan && bus->read_c45) {
774 		err = mdiobus_scan_bus_c45(bus);
775 		if (err)
776 			goto error;
777 	}
778 
779 	mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
780 
781 	bus->state = MDIOBUS_REGISTERED;
782 	dev_dbg(&bus->dev, "probed\n");
783 	return 0;
784 
785 error:
786 	for (i = 0; i < PHY_MAX_ADDR; i++) {
787 		mdiodev = bus->mdio_map[i];
788 		if (!mdiodev)
789 			continue;
790 
791 		mdiodev->device_remove(mdiodev);
792 		mdiodev->device_free(mdiodev);
793 	}
794 error_reset_gpiod:
795 	/* Put PHYs in RESET to save power */
796 	if (bus->reset_gpiod)
797 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
798 
799 	device_del(&bus->dev);
800 	return err;
801 }
802 EXPORT_SYMBOL(__mdiobus_register);
803 
mdiobus_unregister(struct mii_bus * bus)804 void mdiobus_unregister(struct mii_bus *bus)
805 {
806 	struct mdio_device *mdiodev;
807 	int i;
808 
809 	if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
810 		return;
811 	bus->state = MDIOBUS_UNREGISTERED;
812 
813 	for (i = 0; i < PHY_MAX_ADDR; i++) {
814 		mdiodev = bus->mdio_map[i];
815 		if (!mdiodev)
816 			continue;
817 
818 		mdiodev->device_remove(mdiodev);
819 		mdiodev->device_free(mdiodev);
820 	}
821 
822 	/* Put PHYs in RESET to save power */
823 	if (bus->reset_gpiod)
824 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
825 
826 	device_del(&bus->dev);
827 }
828 EXPORT_SYMBOL(mdiobus_unregister);
829 
830 /**
831  * mdiobus_free - free a struct mii_bus
832  * @bus: mii_bus to free
833  *
834  * This function releases the reference to the underlying device
835  * object in the mii_bus.  If this is the last reference, the mii_bus
836  * will be freed.
837  */
mdiobus_free(struct mii_bus * bus)838 void mdiobus_free(struct mii_bus *bus)
839 {
840 	/* For compatibility with error handling in drivers. */
841 	if (bus->state == MDIOBUS_ALLOCATED) {
842 		kfree(bus);
843 		return;
844 	}
845 
846 	WARN(bus->state != MDIOBUS_UNREGISTERED,
847 	     "%s: not in UNREGISTERED state\n", bus->id);
848 	bus->state = MDIOBUS_RELEASED;
849 
850 	put_device(&bus->dev);
851 }
852 EXPORT_SYMBOL(mdiobus_free);
853 
mdiobus_stats_acct(struct mdio_bus_stats * stats,bool op,int ret)854 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
855 {
856 	preempt_disable();
857 	u64_stats_update_begin(&stats->syncp);
858 
859 	u64_stats_inc(&stats->transfers);
860 	if (ret < 0) {
861 		u64_stats_inc(&stats->errors);
862 		goto out;
863 	}
864 
865 	if (op)
866 		u64_stats_inc(&stats->reads);
867 	else
868 		u64_stats_inc(&stats->writes);
869 out:
870 	u64_stats_update_end(&stats->syncp);
871 	preempt_enable();
872 }
873 
874 /**
875  * __mdiobus_read - Unlocked version of the mdiobus_read function
876  * @bus: the mii_bus struct
877  * @addr: the phy address
878  * @regnum: register number to read
879  *
880  * Read a MDIO bus register. Caller must hold the mdio bus lock.
881  *
882  * NOTE: MUST NOT be called from interrupt context.
883  */
__mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)884 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
885 {
886 	int retval;
887 
888 	lockdep_assert_held_once(&bus->mdio_lock);
889 
890 	if (addr >= PHY_MAX_ADDR)
891 		return -ENXIO;
892 
893 	if (bus->read)
894 		retval = bus->read(bus, addr, regnum);
895 	else
896 		retval = -EOPNOTSUPP;
897 
898 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
899 	mdiobus_stats_acct(&bus->stats[addr], true, retval);
900 
901 	return retval;
902 }
903 EXPORT_SYMBOL(__mdiobus_read);
904 
905 /**
906  * __mdiobus_write - Unlocked version of the mdiobus_write function
907  * @bus: the mii_bus struct
908  * @addr: the phy address
909  * @regnum: register number to write
910  * @val: value to write to @regnum
911  *
912  * Write a MDIO bus register. Caller must hold the mdio bus lock.
913  *
914  * NOTE: MUST NOT be called from interrupt context.
915  */
__mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)916 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
917 {
918 	int err;
919 
920 	lockdep_assert_held_once(&bus->mdio_lock);
921 
922 	if (addr >= PHY_MAX_ADDR)
923 		return -ENXIO;
924 
925 	if (bus->write)
926 		err = bus->write(bus, addr, regnum, val);
927 	else
928 		err = -EOPNOTSUPP;
929 
930 	trace_mdio_access(bus, 0, addr, regnum, val, err);
931 	mdiobus_stats_acct(&bus->stats[addr], false, err);
932 
933 	return err;
934 }
935 EXPORT_SYMBOL(__mdiobus_write);
936 
937 /**
938  * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
939  * @bus: the mii_bus struct
940  * @addr: the phy address
941  * @regnum: register number to modify
942  * @mask: bit mask of bits to clear
943  * @set: bit mask of bits to set
944  *
945  * Read, modify, and if any change, write the register value back to the
946  * device. Any error returns a negative number.
947  *
948  * NOTE: MUST NOT be called from interrupt context.
949  */
__mdiobus_modify_changed(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)950 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
951 			     u16 mask, u16 set)
952 {
953 	int new, ret;
954 
955 	ret = __mdiobus_read(bus, addr, regnum);
956 	if (ret < 0)
957 		return ret;
958 
959 	new = (ret & ~mask) | set;
960 	if (new == ret)
961 		return 0;
962 
963 	ret = __mdiobus_write(bus, addr, regnum, new);
964 
965 	return ret < 0 ? ret : 1;
966 }
967 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
968 
969 /**
970  * __mdiobus_c45_read - Unlocked version of the mdiobus_c45_read function
971  * @bus: the mii_bus struct
972  * @addr: the phy address
973  * @devad: device address to read
974  * @regnum: register number to read
975  *
976  * Read a MDIO bus register. Caller must hold the mdio bus lock.
977  *
978  * NOTE: MUST NOT be called from interrupt context.
979  */
__mdiobus_c45_read(struct mii_bus * bus,int addr,int devad,u32 regnum)980 int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum)
981 {
982 	int retval;
983 
984 	lockdep_assert_held_once(&bus->mdio_lock);
985 
986 	if (addr >= PHY_MAX_ADDR)
987 		return -ENXIO;
988 
989 	if (bus->read_c45)
990 		retval = bus->read_c45(bus, addr, devad, regnum);
991 	else
992 		retval = -EOPNOTSUPP;
993 
994 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
995 	mdiobus_stats_acct(&bus->stats[addr], true, retval);
996 
997 	return retval;
998 }
999 EXPORT_SYMBOL(__mdiobus_c45_read);
1000 
1001 /**
1002  * __mdiobus_c45_write - Unlocked version of the mdiobus_write function
1003  * @bus: the mii_bus struct
1004  * @addr: the phy address
1005  * @devad: device address to read
1006  * @regnum: register number to write
1007  * @val: value to write to @regnum
1008  *
1009  * Write a MDIO bus register. Caller must hold the mdio bus lock.
1010  *
1011  * NOTE: MUST NOT be called from interrupt context.
1012  */
__mdiobus_c45_write(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 val)1013 int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
1014 			u16 val)
1015 {
1016 	int err;
1017 
1018 	lockdep_assert_held_once(&bus->mdio_lock);
1019 
1020 	if (addr >= PHY_MAX_ADDR)
1021 		return -ENXIO;
1022 
1023 	if (bus->write_c45)
1024 		err = bus->write_c45(bus, addr, devad, regnum, val);
1025 	else
1026 		err = -EOPNOTSUPP;
1027 
1028 	trace_mdio_access(bus, 0, addr, regnum, val, err);
1029 	mdiobus_stats_acct(&bus->stats[addr], false, err);
1030 
1031 	return err;
1032 }
1033 EXPORT_SYMBOL(__mdiobus_c45_write);
1034 
1035 /**
1036  * __mdiobus_c45_modify_changed - Unlocked version of the mdiobus_modify function
1037  * @bus: the mii_bus struct
1038  * @addr: the phy address
1039  * @devad: device address to read
1040  * @regnum: register number to modify
1041  * @mask: bit mask of bits to clear
1042  * @set: bit mask of bits to set
1043  *
1044  * Read, modify, and if any change, write the register value back to the
1045  * device. Any error returns a negative number.
1046  *
1047  * NOTE: MUST NOT be called from interrupt context.
1048  */
__mdiobus_c45_modify_changed(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 mask,u16 set)1049 static int __mdiobus_c45_modify_changed(struct mii_bus *bus, int addr,
1050 					int devad, u32 regnum, u16 mask,
1051 					u16 set)
1052 {
1053 	int new, ret;
1054 
1055 	ret = __mdiobus_c45_read(bus, addr, devad, regnum);
1056 	if (ret < 0)
1057 		return ret;
1058 
1059 	new = (ret & ~mask) | set;
1060 	if (new == ret)
1061 		return 0;
1062 
1063 	ret = __mdiobus_c45_write(bus, addr, devad, regnum, new);
1064 
1065 	return ret < 0 ? ret : 1;
1066 }
1067 
1068 /**
1069  * mdiobus_read_nested - Nested version of the mdiobus_read function
1070  * @bus: the mii_bus struct
1071  * @addr: the phy address
1072  * @regnum: register number to read
1073  *
1074  * In case of nested MDIO bus access avoid lockdep false positives by
1075  * using mutex_lock_nested().
1076  *
1077  * NOTE: MUST NOT be called from interrupt context,
1078  * because the bus read/write functions may wait for an interrupt
1079  * to conclude the operation.
1080  */
mdiobus_read_nested(struct mii_bus * bus,int addr,u32 regnum)1081 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
1082 {
1083 	int retval;
1084 
1085 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1086 	retval = __mdiobus_read(bus, addr, regnum);
1087 	mutex_unlock(&bus->mdio_lock);
1088 
1089 	return retval;
1090 }
1091 EXPORT_SYMBOL(mdiobus_read_nested);
1092 
1093 /**
1094  * mdiobus_read - Convenience function for reading a given MII mgmt register
1095  * @bus: the mii_bus struct
1096  * @addr: the phy address
1097  * @regnum: register number to read
1098  *
1099  * NOTE: MUST NOT be called from interrupt context,
1100  * because the bus read/write functions may wait for an interrupt
1101  * to conclude the operation.
1102  */
mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)1103 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
1104 {
1105 	int retval;
1106 
1107 	mutex_lock(&bus->mdio_lock);
1108 	retval = __mdiobus_read(bus, addr, regnum);
1109 	mutex_unlock(&bus->mdio_lock);
1110 
1111 	return retval;
1112 }
1113 EXPORT_SYMBOL(mdiobus_read);
1114 
1115 /**
1116  * mdiobus_c45_read - Convenience function for reading a given MII mgmt register
1117  * @bus: the mii_bus struct
1118  * @addr: the phy address
1119  * @devad: device address to read
1120  * @regnum: register number to read
1121  *
1122  * NOTE: MUST NOT be called from interrupt context,
1123  * because the bus read/write functions may wait for an interrupt
1124  * to conclude the operation.
1125  */
mdiobus_c45_read(struct mii_bus * bus,int addr,int devad,u32 regnum)1126 int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum)
1127 {
1128 	int retval;
1129 
1130 	mutex_lock(&bus->mdio_lock);
1131 	retval = __mdiobus_c45_read(bus, addr, devad, regnum);
1132 	mutex_unlock(&bus->mdio_lock);
1133 
1134 	return retval;
1135 }
1136 EXPORT_SYMBOL(mdiobus_c45_read);
1137 
1138 /**
1139  * mdiobus_c45_read_nested - Nested version of the mdiobus_c45_read function
1140  * @bus: the mii_bus struct
1141  * @addr: the phy address
1142  * @devad: device address to read
1143  * @regnum: register number to read
1144  *
1145  * In case of nested MDIO bus access avoid lockdep false positives by
1146  * using mutex_lock_nested().
1147  *
1148  * NOTE: MUST NOT be called from interrupt context,
1149  * because the bus read/write functions may wait for an interrupt
1150  * to conclude the operation.
1151  */
mdiobus_c45_read_nested(struct mii_bus * bus,int addr,int devad,u32 regnum)1152 int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad,
1153 			    u32 regnum)
1154 {
1155 	int retval;
1156 
1157 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1158 	retval = __mdiobus_c45_read(bus, addr, devad, regnum);
1159 	mutex_unlock(&bus->mdio_lock);
1160 
1161 	return retval;
1162 }
1163 EXPORT_SYMBOL(mdiobus_c45_read_nested);
1164 
1165 /**
1166  * mdiobus_write_nested - Nested version of the mdiobus_write function
1167  * @bus: the mii_bus struct
1168  * @addr: the phy address
1169  * @regnum: register number to write
1170  * @val: value to write to @regnum
1171  *
1172  * In case of nested MDIO bus access avoid lockdep false positives by
1173  * using mutex_lock_nested().
1174  *
1175  * NOTE: MUST NOT be called from interrupt context,
1176  * because the bus read/write functions may wait for an interrupt
1177  * to conclude the operation.
1178  */
mdiobus_write_nested(struct mii_bus * bus,int addr,u32 regnum,u16 val)1179 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
1180 {
1181 	int err;
1182 
1183 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1184 	err = __mdiobus_write(bus, addr, regnum, val);
1185 	mutex_unlock(&bus->mdio_lock);
1186 
1187 	return err;
1188 }
1189 EXPORT_SYMBOL(mdiobus_write_nested);
1190 
1191 /**
1192  * mdiobus_write - Convenience function for writing a given MII mgmt register
1193  * @bus: the mii_bus struct
1194  * @addr: the phy address
1195  * @regnum: register number to write
1196  * @val: value to write to @regnum
1197  *
1198  * NOTE: MUST NOT be called from interrupt context,
1199  * because the bus read/write functions may wait for an interrupt
1200  * to conclude the operation.
1201  */
mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)1202 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
1203 {
1204 	int err;
1205 
1206 	mutex_lock(&bus->mdio_lock);
1207 	err = __mdiobus_write(bus, addr, regnum, val);
1208 	mutex_unlock(&bus->mdio_lock);
1209 
1210 	return err;
1211 }
1212 EXPORT_SYMBOL(mdiobus_write);
1213 
1214 /**
1215  * mdiobus_c45_write - Convenience function for writing a given MII mgmt register
1216  * @bus: the mii_bus struct
1217  * @addr: the phy address
1218  * @devad: device address to read
1219  * @regnum: register number to write
1220  * @val: value to write to @regnum
1221  *
1222  * NOTE: MUST NOT be called from interrupt context,
1223  * because the bus read/write functions may wait for an interrupt
1224  * to conclude the operation.
1225  */
mdiobus_c45_write(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 val)1226 int mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
1227 		      u16 val)
1228 {
1229 	int err;
1230 
1231 	mutex_lock(&bus->mdio_lock);
1232 	err = __mdiobus_c45_write(bus, addr, devad, regnum, val);
1233 	mutex_unlock(&bus->mdio_lock);
1234 
1235 	return err;
1236 }
1237 EXPORT_SYMBOL(mdiobus_c45_write);
1238 
1239 /**
1240  * mdiobus_c45_write_nested - Nested version of the mdiobus_c45_write function
1241  * @bus: the mii_bus struct
1242  * @addr: the phy address
1243  * @devad: device address to read
1244  * @regnum: register number to write
1245  * @val: value to write to @regnum
1246  *
1247  * In case of nested MDIO bus access avoid lockdep false positives by
1248  * using mutex_lock_nested().
1249  *
1250  * NOTE: MUST NOT be called from interrupt context,
1251  * because the bus read/write functions may wait for an interrupt
1252  * to conclude the operation.
1253  */
mdiobus_c45_write_nested(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 val)1254 int mdiobus_c45_write_nested(struct mii_bus *bus, int addr, int devad,
1255 			     u32 regnum, u16 val)
1256 {
1257 	int err;
1258 
1259 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
1260 	err = __mdiobus_c45_write(bus, addr, devad, regnum, val);
1261 	mutex_unlock(&bus->mdio_lock);
1262 
1263 	return err;
1264 }
1265 EXPORT_SYMBOL(mdiobus_c45_write_nested);
1266 
1267 /*
1268  * __mdiobus_modify - Convenience function for modifying a given mdio device
1269  *	register
1270  * @bus: the mii_bus struct
1271  * @addr: the phy address
1272  * @regnum: register number to write
1273  * @mask: bit mask of bits to clear
1274  * @set: bit mask of bits to set
1275  */
__mdiobus_modify(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)1276 int __mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
1277 		     u16 set)
1278 {
1279 	int err;
1280 
1281 	err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
1282 
1283 	return err < 0 ? err : 0;
1284 }
1285 EXPORT_SYMBOL_GPL(__mdiobus_modify);
1286 
1287 /**
1288  * mdiobus_modify - Convenience function for modifying a given mdio device
1289  *	register
1290  * @bus: the mii_bus struct
1291  * @addr: the phy address
1292  * @regnum: register number to write
1293  * @mask: bit mask of bits to clear
1294  * @set: bit mask of bits to set
1295  */
mdiobus_modify(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)1296 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
1297 {
1298 	int err;
1299 
1300 	mutex_lock(&bus->mdio_lock);
1301 	err = __mdiobus_modify(bus, addr, regnum, mask, set);
1302 	mutex_unlock(&bus->mdio_lock);
1303 
1304 	return err;
1305 }
1306 EXPORT_SYMBOL_GPL(mdiobus_modify);
1307 
1308 /**
1309  * mdiobus_c45_modify - Convenience function for modifying a given mdio device
1310  *	register
1311  * @bus: the mii_bus struct
1312  * @addr: the phy address
1313  * @devad: device address to read
1314  * @regnum: register number to write
1315  * @mask: bit mask of bits to clear
1316  * @set: bit mask of bits to set
1317  */
mdiobus_c45_modify(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 mask,u16 set)1318 int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum,
1319 		       u16 mask, u16 set)
1320 {
1321 	int err;
1322 
1323 	mutex_lock(&bus->mdio_lock);
1324 	err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum,
1325 					   mask, set);
1326 	mutex_unlock(&bus->mdio_lock);
1327 
1328 	return err < 0 ? err : 0;
1329 }
1330 EXPORT_SYMBOL_GPL(mdiobus_c45_modify);
1331 
1332 /**
1333  * mdiobus_modify_changed - Convenience function for modifying a given mdio
1334  *	device register and returning if it changed
1335  * @bus: the mii_bus struct
1336  * @addr: the phy address
1337  * @regnum: register number to write
1338  * @mask: bit mask of bits to clear
1339  * @set: bit mask of bits to set
1340  */
mdiobus_modify_changed(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)1341 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
1342 			   u16 mask, u16 set)
1343 {
1344 	int err;
1345 
1346 	mutex_lock(&bus->mdio_lock);
1347 	err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
1348 	mutex_unlock(&bus->mdio_lock);
1349 
1350 	return err;
1351 }
1352 EXPORT_SYMBOL_GPL(mdiobus_modify_changed);
1353 
1354 /**
1355  * mdiobus_c45_modify_changed - Convenience function for modifying a given mdio
1356  *	device register and returning if it changed
1357  * @bus: the mii_bus struct
1358  * @addr: the phy address
1359  * @devad: device address to read
1360  * @regnum: register number to write
1361  * @mask: bit mask of bits to clear
1362  * @set: bit mask of bits to set
1363  */
mdiobus_c45_modify_changed(struct mii_bus * bus,int addr,int devad,u32 regnum,u16 mask,u16 set)1364 int mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, int devad,
1365 			       u32 regnum, u16 mask, u16 set)
1366 {
1367 	int err;
1368 
1369 	mutex_lock(&bus->mdio_lock);
1370 	err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum, mask, set);
1371 	mutex_unlock(&bus->mdio_lock);
1372 
1373 	return err;
1374 }
1375 EXPORT_SYMBOL_GPL(mdiobus_c45_modify_changed);
1376 
1377 /**
1378  * mdio_bus_match - determine if given MDIO driver supports the given
1379  *		    MDIO device
1380  * @dev: target MDIO device
1381  * @drv: given MDIO driver
1382  *
1383  * Description: Given a MDIO device, and a MDIO driver, return 1 if
1384  *   the driver supports the device.  Otherwise, return 0. This may
1385  *   require calling the devices own match function, since different classes
1386  *   of MDIO devices have different match criteria.
1387  */
mdio_bus_match(struct device * dev,const struct device_driver * drv)1388 static int mdio_bus_match(struct device *dev, const struct device_driver *drv)
1389 {
1390 	const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
1391 	struct mdio_device *mdio = to_mdio_device(dev);
1392 
1393 	/* Both the driver and device must type-match */
1394 	if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
1395 	    !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
1396 		return 0;
1397 
1398 	if (of_driver_match_device(dev, drv))
1399 		return 1;
1400 
1401 	if (mdio->bus_match)
1402 		return mdio->bus_match(dev, drv);
1403 
1404 	return 0;
1405 }
1406 
mdio_uevent(const struct device * dev,struct kobj_uevent_env * env)1407 static int mdio_uevent(const struct device *dev, struct kobj_uevent_env *env)
1408 {
1409 	int rc;
1410 
1411 	/* Some devices have extra OF data and an OF-style MODALIAS */
1412 	rc = of_device_uevent_modalias(dev, env);
1413 	if (rc != -ENODEV)
1414 		return rc;
1415 
1416 	return 0;
1417 }
1418 
1419 static struct attribute *mdio_bus_device_statistics_attrs[] = {
1420 	&dev_attr_mdio_bus_device_transfers.attr.attr,
1421 	&dev_attr_mdio_bus_device_errors.attr.attr,
1422 	&dev_attr_mdio_bus_device_writes.attr.attr,
1423 	&dev_attr_mdio_bus_device_reads.attr.attr,
1424 	NULL,
1425 };
1426 
1427 static const struct attribute_group mdio_bus_device_statistics_group = {
1428 	.name	= "statistics",
1429 	.attrs	= mdio_bus_device_statistics_attrs,
1430 };
1431 
1432 static const struct attribute_group *mdio_bus_dev_groups[] = {
1433 	&mdio_bus_device_statistics_group,
1434 	NULL,
1435 };
1436 
1437 const struct bus_type mdio_bus_type = {
1438 	.name		= "mdio_bus",
1439 	.dev_groups	= mdio_bus_dev_groups,
1440 	.match		= mdio_bus_match,
1441 	.uevent		= mdio_uevent,
1442 };
1443 EXPORT_SYMBOL(mdio_bus_type);
1444 
mdio_bus_init(void)1445 int __init mdio_bus_init(void)
1446 {
1447 	int ret;
1448 
1449 	ret = class_register(&mdio_bus_class);
1450 	if (!ret) {
1451 		ret = bus_register(&mdio_bus_type);
1452 		if (ret)
1453 			class_unregister(&mdio_bus_class);
1454 	}
1455 
1456 	return ret;
1457 }
1458 
1459 #if IS_ENABLED(CONFIG_PHYLIB)
mdio_bus_exit(void)1460 void mdio_bus_exit(void)
1461 {
1462 	class_unregister(&mdio_bus_class);
1463 	bus_unregister(&mdio_bus_type);
1464 }
1465 EXPORT_SYMBOL_GPL(mdio_bus_exit);
1466 #else
1467 module_init(mdio_bus_init);
1468 /* no module_exit, intentional */
1469 MODULE_LICENSE("GPL");
1470 MODULE_DESCRIPTION("MDIO bus/device layer");
1471 #endif
1472