blob: f99c21f78b639fc1e6b984a20383f62021b757eb [file] [log] [blame]
Sergei Shtylyov02d320c2014-01-05 03:18:27 +03001/* MDIO Bus interface
Andy Fleming00db8182005-07-30 19:31:23 -04002 *
3 * Author: Andy Fleming
4 *
5 * Copyright (c) 2004 Freescale Semiconductor, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
Joe Perches8d242482012-06-09 07:49:07 +000013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Andy Fleming00db8182005-07-30 19:31:23 -040016#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040017#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/unistd.h>
20#include <linux/slab.h>
21#include <linux/interrupt.h>
22#include <linux/init.h>
23#include <linux/delay.h>
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -080024#include <linux/device.h>
Roger Quadros69226892017-04-21 16:15:38 +030025#include <linux/gpio.h>
26#include <linux/gpio/consumer.h>
David Daneya30e2c12012-06-27 07:33:37 +000027#include <linux/of_device.h>
Mark Brown4085a7f2012-10-08 22:55:43 +000028#include <linux/of_mdio.h>
Roger Quadros69226892017-04-21 16:15:38 +030029#include <linux/of_gpio.h>
Andy Fleming00db8182005-07-30 19:31:23 -040030#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
33#include <linux/spinlock.h>
34#include <linux/mm.h>
35#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040036#include <linux/mii.h>
37#include <linux/ethtool.h>
38#include <linux/phy.h>
Sergei Shtylyov02d320c2014-01-05 03:18:27 +030039#include <linux/io.h>
40#include <linux/uaccess.h>
Andy Fleming00db8182005-07-30 19:31:23 -040041
Andy Fleming00db8182005-07-30 19:31:23 -040042#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040043
Uwe Kleine-Könige22e9962016-11-22 16:47:11 +010044#define CREATE_TRACE_POINTS
45#include <trace/events/mdio.h>
46
Florian Fainelli648ea012017-02-04 13:02:44 -080047#include "mdio-boardinfo.h"
48
Andrew Lunn7f854422016-01-06 20:11:18 +010049int mdiobus_register_device(struct mdio_device *mdiodev)
50{
51 if (mdiodev->bus->mdio_map[mdiodev->addr])
52 return -EBUSY;
53
54 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
55
56 return 0;
57}
58EXPORT_SYMBOL(mdiobus_register_device);
59
60int mdiobus_unregister_device(struct mdio_device *mdiodev)
61{
62 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
63 return -EINVAL;
64
65 mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
66
67 return 0;
68}
69EXPORT_SYMBOL(mdiobus_unregister_device);
70
71struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
72{
73 struct mdio_device *mdiodev = bus->mdio_map[addr];
74
75 if (!mdiodev)
76 return NULL;
77
78 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
79 return NULL;
80
81 return container_of(mdiodev, struct phy_device, mdio);
82}
83EXPORT_SYMBOL(mdiobus_get_phy);
84
85bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
86{
87 return bus->mdio_map[addr];
88}
89EXPORT_SYMBOL(mdiobus_is_registered_device);
90
Randy Dunlapb3df0da2007-03-06 02:41:48 -080091/**
Timur Tabieb8a54a72012-01-12 15:23:04 -080092 * mdiobus_alloc_size - allocate a mii_bus structure
Randy Dunlapaf58f1d2012-01-21 09:03:04 +000093 * @size: extra amount of memory to allocate for private storage.
94 * If non-zero, then bus->priv is points to that memory.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070095 *
96 * Description: called by a bus driver to allocate an mii_bus
97 * structure to fill in.
98 */
Timur Tabieb8a54a72012-01-12 15:23:04 -080099struct mii_bus *mdiobus_alloc_size(size_t size)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700100{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700101 struct mii_bus *bus;
Timur Tabieb8a54a72012-01-12 15:23:04 -0800102 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
103 size_t alloc_size;
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100104 int i;
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700105
Timur Tabieb8a54a72012-01-12 15:23:04 -0800106 /* If we alloc extra space, it should be aligned */
107 if (size)
108 alloc_size = aligned_size + size;
109 else
110 alloc_size = sizeof(*bus);
111
112 bus = kzalloc(alloc_size, GFP_KERNEL);
Dan Carpenterdb9107b2016-01-12 12:34:36 +0300113 if (!bus)
114 return NULL;
115
116 bus->state = MDIOBUS_ALLOCATED;
117 if (size)
118 bus->priv = (void *)bus + aligned_size;
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700119
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100120 /* Initialise the interrupts to polling */
121 for (i = 0; i < PHY_MAX_ADDR; i++)
122 bus->irq[i] = PHY_POLL;
123
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700124 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700125}
Timur Tabieb8a54a72012-01-12 15:23:04 -0800126EXPORT_SYMBOL(mdiobus_alloc_size);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700127
Grygorii Strashko6d48f442014-04-30 15:23:33 +0300128static void _devm_mdiobus_free(struct device *dev, void *res)
129{
130 mdiobus_free(*(struct mii_bus **)res);
131}
132
133static int devm_mdiobus_match(struct device *dev, void *res, void *data)
134{
135 struct mii_bus **r = res;
136
137 if (WARN_ON(!r || !*r))
138 return 0;
139
140 return *r == data;
141}
142
143/**
144 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
145 * @dev: Device to allocate mii_bus for
146 * @sizeof_priv: Space to allocate for private structure.
147 *
148 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
149 * automatically freed on driver detach.
150 *
151 * If an mii_bus allocated with this function needs to be freed separately,
152 * devm_mdiobus_free() must be used.
153 *
154 * RETURNS:
155 * Pointer to allocated mii_bus on success, NULL on failure.
156 */
157struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
158{
159 struct mii_bus **ptr, *bus;
160
161 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
162 if (!ptr)
163 return NULL;
164
165 /* use raw alloc_dr for kmalloc caller tracing */
166 bus = mdiobus_alloc_size(sizeof_priv);
167 if (bus) {
168 *ptr = bus;
169 devres_add(dev, ptr);
170 } else {
171 devres_free(ptr);
172 }
173
174 return bus;
175}
Arnd Bergmann93dccc52014-05-08 16:46:52 +0200176EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
Grygorii Strashko6d48f442014-04-30 15:23:33 +0300177
178/**
179 * devm_mdiobus_free - Resource-managed mdiobus_free()
180 * @dev: Device this mii_bus belongs to
181 * @bus: the mii_bus associated with the device
182 *
183 * Free mii_bus allocated with devm_mdiobus_alloc_size().
184 */
185void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
186{
187 int rc;
188
189 rc = devres_release(dev, _devm_mdiobus_free,
190 devm_mdiobus_match, bus);
191 WARN_ON(rc);
192}
193EXPORT_SYMBOL_GPL(devm_mdiobus_free);
194
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700195/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700196 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -0700197 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700198 *
199 * Description: called when the last reference to an mii_bus is
200 * dropped, to free the underlying memory.
201 */
202static void mdiobus_release(struct device *d)
203{
204 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800205 BUG_ON(bus->state != MDIOBUS_RELEASED &&
206 /* for compatibility with error handling in drivers */
207 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700208 kfree(bus);
209}
210
211static struct class mdio_bus_class = {
212 .name = "mdio_bus",
213 .dev_release = mdiobus_release,
214};
215
Bjørn Morkb943fbb2012-05-11 05:47:01 +0000216#if IS_ENABLED(CONFIG_OF_MDIO)
David Daney25106022012-05-02 15:16:37 +0000217/* Helper function for of_mdio_find_bus */
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100218static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
David Daney25106022012-05-02 15:16:37 +0000219{
220 return dev->of_node == mdio_bus_np;
221}
222/**
223 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
Randy Dunlapf41ef2e2012-06-08 14:07:19 +0000224 * @mdio_bus_np: Pointer to the mii_bus.
David Daney25106022012-05-02 15:16:37 +0000225 *
Russell Kinga1364422015-09-24 20:35:52 +0100226 * Returns a reference to the mii_bus, or NULL if none found. The
227 * embedded struct device will have its reference count incremented,
228 * and this must be put once the bus is finished with.
David Daney25106022012-05-02 15:16:37 +0000229 *
230 * Because the association of a device_node and mii_bus is made via
231 * of_mdiobus_register(), the mii_bus cannot be found before it is
232 * registered with of_mdiobus_register().
233 *
234 */
235struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
236{
237 struct device *d;
238
239 if (!mdio_bus_np)
240 return NULL;
241
242 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
243 of_mdio_bus_match);
244
245 return d ? to_mii_bus(d) : NULL;
246}
247EXPORT_SYMBOL(of_mdio_find_bus);
Daniel Mackd9daa242014-06-28 01:23:35 +0200248
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100249/* Walk the list of subnodes of a mdio bus and look for a node that
250 * matches the mdio device's address with its 'reg' property. If
251 * found, set the of_node pointer for the mdio device. This allows
252 * auto-probed phy devices to be supplied with information passed in
253 * via DT.
Daniel Mackd9daa242014-06-28 01:23:35 +0200254 */
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100255static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
256 struct mdio_device *mdiodev)
Daniel Mackd9daa242014-06-28 01:23:35 +0200257{
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100258 struct device *dev = &mdiodev->dev;
Daniel Mackd9daa242014-06-28 01:23:35 +0200259 struct device_node *child;
260
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100261 if (dev->of_node || !bus->dev.of_node)
Daniel Mackd9daa242014-06-28 01:23:35 +0200262 return;
263
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100264 for_each_available_child_of_node(bus->dev.of_node, child) {
Daniel Mackd9daa242014-06-28 01:23:35 +0200265 int addr;
266 int ret;
267
268 ret = of_property_read_u32(child, "reg", &addr);
269 if (ret < 0) {
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100270 dev_err(dev, "%s has invalid MDIO address\n",
Daniel Mackd9daa242014-06-28 01:23:35 +0200271 child->full_name);
272 continue;
273 }
274
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100275 /* A MDIO device must have a reg property in the range [0-31] */
Daniel Mackd9daa242014-06-28 01:23:35 +0200276 if (addr >= PHY_MAX_ADDR) {
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100277 dev_err(dev, "%s MDIO address %i is too large\n",
Daniel Mackd9daa242014-06-28 01:23:35 +0200278 child->full_name, addr);
279 continue;
280 }
281
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100282 if (addr == mdiodev->addr) {
Daniel Mackd9daa242014-06-28 01:23:35 +0200283 dev->of_node = child;
284 return;
285 }
286 }
287}
288#else /* !IS_ENABLED(CONFIG_OF_MDIO) */
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100289static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
290 struct mdio_device *mdiodev)
Daniel Mackd9daa242014-06-28 01:23:35 +0200291{
292}
David Daney25106022012-05-02 15:16:37 +0000293#endif
294
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700295/**
Florian Fainellid0281a52017-03-28 12:57:09 -0700296 * mdiobus_create_device_from_board_info - create a full MDIO device given
297 * a mdio_board_info structure
298 * @bus: MDIO bus to create the devices on
299 * @bi: mdio_board_info structure describing the devices
300 *
301 * Returns 0 on success or < 0 on error.
302 */
303static int mdiobus_create_device(struct mii_bus *bus,
304 struct mdio_board_info *bi)
305{
306 struct mdio_device *mdiodev;
307 int ret = 0;
308
309 mdiodev = mdio_device_create(bus, bi->mdio_addr);
310 if (IS_ERR(mdiodev))
311 return -ENODEV;
312
313 strncpy(mdiodev->modalias, bi->modalias,
314 sizeof(mdiodev->modalias));
315 mdiodev->bus_match = mdio_device_bus_match;
316 mdiodev->dev.platform_data = (void *)bi->platform_data;
317
318 ret = mdio_device_register(mdiodev);
319 if (ret)
320 mdio_device_free(mdiodev);
321
322 return ret;
323}
324
325/**
Russell King59f06972015-09-25 11:56:56 +0100326 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800327 * @bus: target mii_bus
Russell King59f06972015-09-25 11:56:56 +0100328 * @owner: module containing bus accessor functions
Andy Fleminge1393452005-08-24 18:46:21 -0500329 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800330 * Description: Called by a bus driver to bring up all the PHYs
Russell King59f06972015-09-25 11:56:56 +0100331 * on a given bus, and attach them to the bus. Drivers should use
332 * mdiobus_register() rather than __mdiobus_register() unless they
Andrew Lunnf89df3f2016-01-06 20:11:25 +0100333 * need to pass a specific owner module. MDIO devices which are not
334 * PHYs will not be brought up by this function. They are expected to
335 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800336 *
337 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500338 */
Russell King3e3aaf62015-09-24 20:36:02 +0100339int __mdiobus_register(struct mii_bus *bus, struct module *owner)
Andy Fleminge1393452005-08-24 18:46:21 -0500340{
Andrew Lunn711fdba2016-01-06 20:11:27 +0100341 struct mdio_device *mdiodev;
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800342 int i, err;
Roger Quadros69226892017-04-21 16:15:38 +0300343 struct gpio_desc *gpiod;
Andy Fleminge1393452005-08-24 18:46:21 -0500344
Andy Fleminge1393452005-08-24 18:46:21 -0500345 if (NULL == bus || NULL == bus->name ||
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300346 NULL == bus->read || NULL == bus->write)
Andy Fleminge1393452005-08-24 18:46:21 -0500347 return -EINVAL;
348
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700349 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
350 bus->state != MDIOBUS_UNREGISTERED);
351
Russell King3e3aaf62015-09-24 20:36:02 +0100352 bus->owner = owner;
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700353 bus->dev.parent = bus->parent;
354 bus->dev.class = &mdio_bus_class;
355 bus->dev.groups = NULL;
Stephen Hemminger036b6682009-02-26 10:19:36 +0000356 dev_set_name(&bus->dev, "%s", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700357
358 err = device_register(&bus->dev);
359 if (err) {
Joe Perches8d242482012-06-09 07:49:07 +0000360 pr_err("mii_bus %s failed to register\n", bus->id);
Levente Kurusa0c692d02014-01-30 15:45:46 -0800361 put_device(&bus->dev);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700362 return -EINVAL;
363 }
364
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200365 mutex_init(&bus->mdio_lock);
366
Roger Quadros69226892017-04-21 16:15:38 +0300367 /* de-assert bus level PHY GPIO resets */
368 if (bus->num_reset_gpios > 0) {
369 bus->reset_gpiod = devm_kcalloc(&bus->dev,
370 bus->num_reset_gpios,
371 sizeof(struct gpio_desc *),
372 GFP_KERNEL);
373 if (!bus->reset_gpiod)
374 return -ENOMEM;
375 }
376
377 for (i = 0; i < bus->num_reset_gpios; i++) {
378 gpiod = devm_gpiod_get_index(&bus->dev, "reset", i,
379 GPIOD_OUT_LOW);
380 if (IS_ERR(gpiod)) {
381 err = PTR_ERR(gpiod);
382 if (err != -ENOENT) {
383 dev_err(&bus->dev,
384 "mii_bus %s couldn't get reset GPIO\n",
385 bus->id);
386 return err;
387 }
388 } else {
389 bus->reset_gpiod[i] = gpiod;
390 gpiod_set_value_cansleep(gpiod, 1);
391 udelay(bus->reset_delay_us);
392 gpiod_set_value_cansleep(gpiod, 0);
393 }
394 }
395
Florian Fainellidf0c8d92017-05-11 11:24:16 -0700396 if (bus->reset)
397 bus->reset(bus);
398
Andy Fleminge1393452005-08-24 18:46:21 -0500399 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200400 if ((bus->phy_mask & (1 << i)) == 0) {
401 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500402
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200403 phydev = mdiobus_scan(bus, i);
Marek Vasut70e927b2016-05-02 02:47:31 +0200404 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200405 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800406 goto error;
407 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400408 }
Andy Fleminge1393452005-08-24 18:46:21 -0500409 }
410
Florian Fainellid0281a52017-03-28 12:57:09 -0700411 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
Florian Fainelli648ea012017-02-04 13:02:44 -0800412
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800413 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500414 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800415 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500416
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800417error:
418 while (--i >= 0) {
Andrew Lunn711fdba2016-01-06 20:11:27 +0100419 mdiodev = bus->mdio_map[i];
420 if (!mdiodev)
421 continue;
422
423 mdiodev->device_remove(mdiodev);
424 mdiodev->device_free(mdiodev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800425 }
Roger Quadros69226892017-04-21 16:15:38 +0300426
427 /* Put PHYs in RESET to save power */
428 for (i = 0; i < bus->num_reset_gpios; i++) {
429 if (bus->reset_gpiod[i])
430 gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
431 }
432
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800433 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500434 return err;
435}
Russell King3e3aaf62015-09-24 20:36:02 +0100436EXPORT_SYMBOL(__mdiobus_register);
Andy Fleminge1393452005-08-24 18:46:21 -0500437
438void mdiobus_unregister(struct mii_bus *bus)
439{
Andrew Lunna9049e02016-01-06 20:11:26 +0100440 struct mdio_device *mdiodev;
Andy Fleminge1393452005-08-24 18:46:21 -0500441 int i;
442
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700443 BUG_ON(bus->state != MDIOBUS_REGISTERED);
444 bus->state = MDIOBUS_UNREGISTERED;
445
Andy Fleminge1393452005-08-24 18:46:21 -0500446 for (i = 0; i < PHY_MAX_ADDR; i++) {
Andrew Lunna9049e02016-01-06 20:11:26 +0100447 mdiodev = bus->mdio_map[i];
448 if (!mdiodev)
449 continue;
450
Andrew Lunn711fdba2016-01-06 20:11:27 +0100451 mdiodev->device_remove(mdiodev);
452 mdiodev->device_free(mdiodev);
Andy Fleminge1393452005-08-24 18:46:21 -0500453 }
Roger Quadros69226892017-04-21 16:15:38 +0300454
455 /* Put PHYs in RESET to save power */
456 for (i = 0; i < bus->num_reset_gpios; i++) {
457 if (bus->reset_gpiod[i])
458 gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
459 }
460
Mark Salterb6c6aed2015-09-01 09:36:05 -0400461 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500462}
463EXPORT_SYMBOL(mdiobus_unregister);
464
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700465/**
466 * mdiobus_free - free a struct mii_bus
467 * @bus: mii_bus to free
468 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700469 * This function releases the reference to the underlying device
470 * object in the mii_bus. If this is the last reference, the mii_bus
471 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700472 */
473void mdiobus_free(struct mii_bus *bus)
474{
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300475 /* For compatibility with error handling in drivers. */
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700476 if (bus->state == MDIOBUS_ALLOCATED) {
477 kfree(bus);
478 return;
479 }
480
481 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
482 bus->state = MDIOBUS_RELEASED;
483
484 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700485}
486EXPORT_SYMBOL(mdiobus_free);
487
Andrew Lunnf89df3f2016-01-06 20:11:25 +0100488/**
489 * mdiobus_scan - scan a bus for MDIO devices.
490 * @bus: mii_bus to scan
491 * @addr: address on bus to scan
492 *
493 * This function scans the MDIO bus, looking for devices which can be
494 * identified using a vendor/product ID in registers 2 and 3. Not all
495 * MDIO devices have such registers, but PHY devices typically
496 * do. Hence this function assumes anything found is a PHY, or can be
497 * treated as a PHY. Other MDIO devices, such as switches, will
498 * probably not be found during the scan.
499 */
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200500struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
501{
502 struct phy_device *phydev;
503 int err;
504
David Daneyac28b9f2012-06-27 07:33:35 +0000505 phydev = get_phy_device(bus, addr, false);
Sergei Shtylyov66c239e2016-04-24 20:30:53 +0300506 if (IS_ERR(phydev))
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200507 return phydev;
508
Daniel Mack86f6cf42014-05-24 09:34:26 +0200509 /*
510 * For DT, see if the auto-probed phy has a correspoding child
511 * in the bus node, and set the of_node pointer in this case.
512 */
Andrew Lunnf03bc4a2016-01-06 20:11:24 +0100513 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
Daniel Mack86f6cf42014-05-24 09:34:26 +0200514
Grant Likely4dea5472009-04-25 12:52:46 +0000515 err = phy_device_register(phydev);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200516 if (err) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200517 phy_device_free(phydev);
Sergei Shtylyove98a3aa2016-05-03 23:14:41 +0300518 return ERR_PTR(-ENODEV);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200519 }
520
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200521 return phydev;
522}
523EXPORT_SYMBOL(mdiobus_scan);
524
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800525/**
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200526 * mdiobus_read_nested - Nested version of the mdiobus_read function
527 * @bus: the mii_bus struct
528 * @addr: the phy address
529 * @regnum: register number to read
530 *
531 * In case of nested MDIO bus access avoid lockdep false positives by
532 * using mutex_lock_nested().
533 *
534 * NOTE: MUST NOT be called from interrupt context,
535 * because the bus read/write functions may wait for an interrupt
536 * to conclude the operation.
537 */
538int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
539{
540 int retval;
541
542 BUG_ON(in_interrupt());
543
Andrew Lunn9a6f2b02016-04-11 21:40:05 +0200544 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200545 retval = bus->read(bus, addr, regnum);
546 mutex_unlock(&bus->mdio_lock);
547
Uwe Kleine-Könige22e9962016-11-22 16:47:11 +0100548 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
549
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200550 return retval;
551}
552EXPORT_SYMBOL(mdiobus_read_nested);
553
554/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000555 * mdiobus_read - Convenience function for reading a given MII mgmt register
556 * @bus: the mii_bus struct
557 * @addr: the phy address
558 * @regnum: register number to read
559 *
560 * NOTE: MUST NOT be called from interrupt context,
561 * because the bus read/write functions may wait for an interrupt
562 * to conclude the operation.
563 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000564int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000565{
566 int retval;
567
568 BUG_ON(in_interrupt());
569
570 mutex_lock(&bus->mdio_lock);
571 retval = bus->read(bus, addr, regnum);
572 mutex_unlock(&bus->mdio_lock);
573
Uwe Kleine-Könige22e9962016-11-22 16:47:11 +0100574 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
575
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000576 return retval;
577}
578EXPORT_SYMBOL(mdiobus_read);
579
580/**
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200581 * mdiobus_write_nested - Nested version of the mdiobus_write function
582 * @bus: the mii_bus struct
583 * @addr: the phy address
584 * @regnum: register number to write
585 * @val: value to write to @regnum
586 *
587 * In case of nested MDIO bus access avoid lockdep false positives by
588 * using mutex_lock_nested().
589 *
590 * NOTE: MUST NOT be called from interrupt context,
591 * because the bus read/write functions may wait for an interrupt
592 * to conclude the operation.
593 */
594int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
595{
596 int err;
597
598 BUG_ON(in_interrupt());
599
Andrew Lunn9a6f2b02016-04-11 21:40:05 +0200600 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200601 err = bus->write(bus, addr, regnum, val);
602 mutex_unlock(&bus->mdio_lock);
603
Uwe Kleine-Könige22e9962016-11-22 16:47:11 +0100604 trace_mdio_access(bus, 0, addr, regnum, val, err);
605
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200606 return err;
607}
608EXPORT_SYMBOL(mdiobus_write_nested);
609
610/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000611 * mdiobus_write - Convenience function for writing a given MII mgmt register
612 * @bus: the mii_bus struct
613 * @addr: the phy address
614 * @regnum: register number to write
615 * @val: value to write to @regnum
616 *
617 * NOTE: MUST NOT be called from interrupt context,
618 * because the bus read/write functions may wait for an interrupt
619 * to conclude the operation.
620 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000621int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000622{
623 int err;
624
625 BUG_ON(in_interrupt());
626
627 mutex_lock(&bus->mdio_lock);
628 err = bus->write(bus, addr, regnum, val);
629 mutex_unlock(&bus->mdio_lock);
630
Uwe Kleine-Könige22e9962016-11-22 16:47:11 +0100631 trace_mdio_access(bus, 0, addr, regnum, val, err);
632
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000633 return err;
634}
635EXPORT_SYMBOL(mdiobus_write);
636
637/**
Andrew Lunne76a4952016-01-06 20:11:23 +0100638 * mdio_bus_match - determine if given MDIO driver supports the given
639 * MDIO device
640 * @dev: target MDIO device
641 * @drv: given MDIO driver
Andy Fleming00db8182005-07-30 19:31:23 -0400642 *
Andrew Lunne76a4952016-01-06 20:11:23 +0100643 * Description: Given a MDIO device, and a MDIO driver, return 1 if
644 * the driver supports the device. Otherwise, return 0. This may
645 * require calling the devices own match function, since different classes
646 * of MDIO devices have different match criteria.
Andy Fleming00db8182005-07-30 19:31:23 -0400647 */
648static int mdio_bus_match(struct device *dev, struct device_driver *drv)
649{
Andrew Lunne76a4952016-01-06 20:11:23 +0100650 struct mdio_device *mdio = to_mdio_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400651
David Daneya30e2c12012-06-27 07:33:37 +0000652 if (of_driver_match_device(dev, drv))
653 return 1;
654
Andrew Lunne76a4952016-01-06 20:11:23 +0100655 if (mdio->bus_match)
656 return mdio->bus_match(dev, drv);
David Daneya30e2c12012-06-27 07:33:37 +0000657
Andrew Lunne76a4952016-01-06 20:11:23 +0100658 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400659}
660
Russell King1b8f8692017-05-30 21:38:18 +0100661static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
662{
663 int rc;
664
665 /* Some devices have extra OF data and an OF-style MODALIAS */
666 rc = of_device_uevent_modalias(dev, env);
667 if (rc != -ENODEV)
668 return rc;
669
670 return 0;
671}
672
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000673#ifdef CONFIG_PM
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000674static int mdio_bus_suspend(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400675{
Andrew Lunnbc879222016-01-06 20:11:21 +0100676 struct mdio_device *mdio = to_mdio_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400677
Andrew Lunnbc879222016-01-06 20:11:21 +0100678 if (mdio->pm_ops && mdio->pm_ops->suspend)
679 return mdio->pm_ops->suspend(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000680
Andrew Lunnbc879222016-01-06 20:11:21 +0100681 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400682}
683
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000684static int mdio_bus_resume(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400685{
Andrew Lunnbc879222016-01-06 20:11:21 +0100686 struct mdio_device *mdio = to_mdio_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400687
Andrew Lunnbc879222016-01-06 20:11:21 +0100688 if (mdio->pm_ops && mdio->pm_ops->resume)
689 return mdio->pm_ops->resume(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000690
691 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400692}
693
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000694static int mdio_bus_restore(struct device *dev)
695{
Andrew Lunnbc879222016-01-06 20:11:21 +0100696 struct mdio_device *mdio = to_mdio_device(dev);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000697
Andrew Lunnbc879222016-01-06 20:11:21 +0100698 if (mdio->pm_ops && mdio->pm_ops->restore)
699 return mdio->pm_ops->restore(dev);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000700
701 return 0;
702}
703
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300704static const struct dev_pm_ops mdio_bus_pm_ops = {
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000705 .suspend = mdio_bus_suspend,
706 .resume = mdio_bus_resume,
707 .freeze = mdio_bus_suspend,
708 .thaw = mdio_bus_resume,
709 .restore = mdio_bus_restore,
710};
711
712#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
713
714#else
715
716#define MDIO_BUS_PM_OPS NULL
717
718#endif /* CONFIG_PM */
719
Andy Fleming00db8182005-07-30 19:31:23 -0400720struct bus_type mdio_bus_type = {
721 .name = "mdio_bus",
722 .match = mdio_bus_match,
Russell King1b8f8692017-05-30 21:38:18 +0100723 .uevent = mdio_uevent,
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000724 .pm = MDIO_BUS_PM_OPS,
Andy Fleming00db8182005-07-30 19:31:23 -0400725};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700726EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400727
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400728int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400729{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700730 int ret;
731
732 ret = class_register(&mdio_bus_class);
733 if (!ret) {
734 ret = bus_register(&mdio_bus_type);
735 if (ret)
736 class_unregister(&mdio_bus_class);
737 }
738
739 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400740}
Florian Fainelli90eff902017-03-23 10:01:19 -0700741EXPORT_SYMBOL_GPL(mdio_bus_init);
Andy Fleming00db8182005-07-30 19:31:23 -0400742
Florian Fainelli90eff902017-03-23 10:01:19 -0700743#if IS_ENABLED(CONFIG_PHYLIB)
Peter Chubbdc85dec2005-09-03 14:05:06 -0700744void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500745{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700746 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500747 bus_unregister(&mdio_bus_type);
748}
Florian Fainelli90eff902017-03-23 10:01:19 -0700749EXPORT_SYMBOL_GPL(mdio_bus_exit);
750#else
751module_init(mdio_bus_init);
752/* no module_exit, intentional */
753MODULE_LICENSE("GPL");
754MODULE_DESCRIPTION("MDIO bus/device layer");
755#endif