blob: e5b1ccde835bc9656c0571c8afde606aa3471567 [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>
David Daneya30e2c12012-06-27 07:33:37 +000025#include <linux/of_device.h>
Mark Brown4085a7f2012-10-08 22:55:43 +000026#include <linux/of_mdio.h>
Andy Fleming00db8182005-07-30 19:31:23 -040027#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/skbuff.h>
30#include <linux/spinlock.h>
31#include <linux/mm.h>
32#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040033#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/phy.h>
Sergei Shtylyov02d320c2014-01-05 03:18:27 +030036#include <linux/io.h>
37#include <linux/uaccess.h>
Andy Fleming00db8182005-07-30 19:31:23 -040038
Andy Fleming00db8182005-07-30 19:31:23 -040039#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040040
Randy Dunlapb3df0da2007-03-06 02:41:48 -080041/**
Timur Tabieb8a54a72012-01-12 15:23:04 -080042 * mdiobus_alloc_size - allocate a mii_bus structure
Randy Dunlapaf58f1d2012-01-21 09:03:04 +000043 * @size: extra amount of memory to allocate for private storage.
44 * If non-zero, then bus->priv is points to that memory.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070045 *
46 * Description: called by a bus driver to allocate an mii_bus
47 * structure to fill in.
48 */
Timur Tabieb8a54a72012-01-12 15:23:04 -080049struct mii_bus *mdiobus_alloc_size(size_t size)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070050{
Lennert Buytenhek46abc022008-10-08 16:33:40 -070051 struct mii_bus *bus;
Timur Tabieb8a54a72012-01-12 15:23:04 -080052 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
53 size_t alloc_size;
Andrew Lunne7f4dc32016-01-06 20:11:15 +010054 int i;
Lennert Buytenhek46abc022008-10-08 16:33:40 -070055
Timur Tabieb8a54a72012-01-12 15:23:04 -080056 /* If we alloc extra space, it should be aligned */
57 if (size)
58 alloc_size = aligned_size + size;
59 else
60 alloc_size = sizeof(*bus);
61
62 bus = kzalloc(alloc_size, GFP_KERNEL);
63 if (bus) {
Lennert Buytenhek46abc022008-10-08 16:33:40 -070064 bus->state = MDIOBUS_ALLOCATED;
Timur Tabieb8a54a72012-01-12 15:23:04 -080065 if (size)
66 bus->priv = (void *)bus + aligned_size;
67 }
Lennert Buytenhek46abc022008-10-08 16:33:40 -070068
Andrew Lunne7f4dc32016-01-06 20:11:15 +010069 /* Initialise the interrupts to polling */
70 for (i = 0; i < PHY_MAX_ADDR; i++)
71 bus->irq[i] = PHY_POLL;
72
Lennert Buytenhek46abc022008-10-08 16:33:40 -070073 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070074}
Timur Tabieb8a54a72012-01-12 15:23:04 -080075EXPORT_SYMBOL(mdiobus_alloc_size);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070076
Grygorii Strashko6d48f442014-04-30 15:23:33 +030077static void _devm_mdiobus_free(struct device *dev, void *res)
78{
79 mdiobus_free(*(struct mii_bus **)res);
80}
81
82static int devm_mdiobus_match(struct device *dev, void *res, void *data)
83{
84 struct mii_bus **r = res;
85
86 if (WARN_ON(!r || !*r))
87 return 0;
88
89 return *r == data;
90}
91
92/**
93 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
94 * @dev: Device to allocate mii_bus for
95 * @sizeof_priv: Space to allocate for private structure.
96 *
97 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
98 * automatically freed on driver detach.
99 *
100 * If an mii_bus allocated with this function needs to be freed separately,
101 * devm_mdiobus_free() must be used.
102 *
103 * RETURNS:
104 * Pointer to allocated mii_bus on success, NULL on failure.
105 */
106struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
107{
108 struct mii_bus **ptr, *bus;
109
110 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
111 if (!ptr)
112 return NULL;
113
114 /* use raw alloc_dr for kmalloc caller tracing */
115 bus = mdiobus_alloc_size(sizeof_priv);
116 if (bus) {
117 *ptr = bus;
118 devres_add(dev, ptr);
119 } else {
120 devres_free(ptr);
121 }
122
123 return bus;
124}
Arnd Bergmann93dccc52014-05-08 16:46:52 +0200125EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
Grygorii Strashko6d48f442014-04-30 15:23:33 +0300126
127/**
128 * devm_mdiobus_free - Resource-managed mdiobus_free()
129 * @dev: Device this mii_bus belongs to
130 * @bus: the mii_bus associated with the device
131 *
132 * Free mii_bus allocated with devm_mdiobus_alloc_size().
133 */
134void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
135{
136 int rc;
137
138 rc = devres_release(dev, _devm_mdiobus_free,
139 devm_mdiobus_match, bus);
140 WARN_ON(rc);
141}
142EXPORT_SYMBOL_GPL(devm_mdiobus_free);
143
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700144/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700145 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -0700146 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700147 *
148 * Description: called when the last reference to an mii_bus is
149 * dropped, to free the underlying memory.
150 */
151static void mdiobus_release(struct device *d)
152{
153 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800154 BUG_ON(bus->state != MDIOBUS_RELEASED &&
155 /* for compatibility with error handling in drivers */
156 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700157 kfree(bus);
158}
159
160static struct class mdio_bus_class = {
161 .name = "mdio_bus",
162 .dev_release = mdiobus_release,
163};
164
Bjørn Morkb943fbb2012-05-11 05:47:01 +0000165#if IS_ENABLED(CONFIG_OF_MDIO)
David Daney25106022012-05-02 15:16:37 +0000166/* Helper function for of_mdio_find_bus */
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100167static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
David Daney25106022012-05-02 15:16:37 +0000168{
169 return dev->of_node == mdio_bus_np;
170}
171/**
172 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
Randy Dunlapf41ef2e2012-06-08 14:07:19 +0000173 * @mdio_bus_np: Pointer to the mii_bus.
David Daney25106022012-05-02 15:16:37 +0000174 *
Russell Kinga1364422015-09-24 20:35:52 +0100175 * Returns a reference to the mii_bus, or NULL if none found. The
176 * embedded struct device will have its reference count incremented,
177 * and this must be put once the bus is finished with.
David Daney25106022012-05-02 15:16:37 +0000178 *
179 * Because the association of a device_node and mii_bus is made via
180 * of_mdiobus_register(), the mii_bus cannot be found before it is
181 * registered with of_mdiobus_register().
182 *
183 */
184struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
185{
186 struct device *d;
187
188 if (!mdio_bus_np)
189 return NULL;
190
191 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
192 of_mdio_bus_match);
193
194 return d ? to_mii_bus(d) : NULL;
195}
196EXPORT_SYMBOL(of_mdio_find_bus);
Daniel Mackd9daa242014-06-28 01:23:35 +0200197
198/* Walk the list of subnodes of a mdio bus and look for a node that matches the
199 * phy's address with its 'reg' property. If found, set the of_node pointer for
200 * the phy. This allows auto-probed pyh devices to be supplied with information
201 * passed in via DT.
202 */
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100203static void of_mdiobus_link_phydev(struct mii_bus *bus,
Daniel Mackd9daa242014-06-28 01:23:35 +0200204 struct phy_device *phydev)
205{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100206 struct device *dev = &phydev->mdio.dev;
Daniel Mackd9daa242014-06-28 01:23:35 +0200207 struct device_node *child;
208
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100209 if (dev->of_node || !bus->dev.of_node)
Daniel Mackd9daa242014-06-28 01:23:35 +0200210 return;
211
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100212 for_each_available_child_of_node(bus->dev.of_node, child) {
Daniel Mackd9daa242014-06-28 01:23:35 +0200213 int addr;
214 int ret;
215
216 ret = of_property_read_u32(child, "reg", &addr);
217 if (ret < 0) {
218 dev_err(dev, "%s has invalid PHY address\n",
219 child->full_name);
220 continue;
221 }
222
223 /* A PHY must have a reg property in the range [0-31] */
224 if (addr >= PHY_MAX_ADDR) {
225 dev_err(dev, "%s PHY address %i is too large\n",
226 child->full_name, addr);
227 continue;
228 }
229
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100230 if (addr == phydev->mdio.addr) {
Daniel Mackd9daa242014-06-28 01:23:35 +0200231 dev->of_node = child;
232 return;
233 }
234 }
235}
236#else /* !IS_ENABLED(CONFIG_OF_MDIO) */
237static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
238 struct phy_device *phydev)
239{
240}
David Daney25106022012-05-02 15:16:37 +0000241#endif
242
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700243/**
Russell King59f06972015-09-25 11:56:56 +0100244 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800245 * @bus: target mii_bus
Russell King59f06972015-09-25 11:56:56 +0100246 * @owner: module containing bus accessor functions
Andy Fleminge1393452005-08-24 18:46:21 -0500247 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800248 * Description: Called by a bus driver to bring up all the PHYs
Russell King59f06972015-09-25 11:56:56 +0100249 * on a given bus, and attach them to the bus. Drivers should use
250 * mdiobus_register() rather than __mdiobus_register() unless they
251 * need to pass a specific owner module.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800252 *
253 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500254 */
Russell King3e3aaf62015-09-24 20:36:02 +0100255int __mdiobus_register(struct mii_bus *bus, struct module *owner)
Andy Fleminge1393452005-08-24 18:46:21 -0500256{
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800257 int i, err;
Andy Fleminge1393452005-08-24 18:46:21 -0500258
Andy Fleminge1393452005-08-24 18:46:21 -0500259 if (NULL == bus || NULL == bus->name ||
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300260 NULL == bus->read || NULL == bus->write)
Andy Fleminge1393452005-08-24 18:46:21 -0500261 return -EINVAL;
262
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700263 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
264 bus->state != MDIOBUS_UNREGISTERED);
265
Russell King3e3aaf62015-09-24 20:36:02 +0100266 bus->owner = owner;
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700267 bus->dev.parent = bus->parent;
268 bus->dev.class = &mdio_bus_class;
269 bus->dev.groups = NULL;
Stephen Hemminger036b6682009-02-26 10:19:36 +0000270 dev_set_name(&bus->dev, "%s", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700271
272 err = device_register(&bus->dev);
273 if (err) {
Joe Perches8d242482012-06-09 07:49:07 +0000274 pr_err("mii_bus %s failed to register\n", bus->id);
Levente Kurusa0c692d02014-01-30 15:45:46 -0800275 put_device(&bus->dev);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700276 return -EINVAL;
277 }
278
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200279 mutex_init(&bus->mdio_lock);
280
Andy Fleminge1393452005-08-24 18:46:21 -0500281 if (bus->reset)
282 bus->reset(bus);
283
284 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200285 if ((bus->phy_mask & (1 << i)) == 0) {
286 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500287
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200288 phydev = mdiobus_scan(bus, i);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800289 if (IS_ERR(phydev)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200290 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800291 goto error;
292 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400293 }
Andy Fleminge1393452005-08-24 18:46:21 -0500294 }
295
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800296 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500297 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800298 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500299
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800300error:
301 while (--i >= 0) {
Russell King38737e42015-09-24 20:36:28 +0100302 struct phy_device *phydev = bus->phy_map[i];
303 if (phydev) {
304 phy_device_remove(phydev);
305 phy_device_free(phydev);
306 }
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800307 }
308 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500309 return err;
310}
Russell King3e3aaf62015-09-24 20:36:02 +0100311EXPORT_SYMBOL(__mdiobus_register);
Andy Fleminge1393452005-08-24 18:46:21 -0500312
313void mdiobus_unregister(struct mii_bus *bus)
314{
315 int i;
316
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700317 BUG_ON(bus->state != MDIOBUS_REGISTERED);
318 bus->state = MDIOBUS_UNREGISTERED;
319
Andy Fleminge1393452005-08-24 18:46:21 -0500320 for (i = 0; i < PHY_MAX_ADDR; i++) {
Russell King38737e42015-09-24 20:36:28 +0100321 struct phy_device *phydev = bus->phy_map[i];
322 if (phydev) {
323 phy_device_remove(phydev);
324 phy_device_free(phydev);
325 }
Andy Fleminge1393452005-08-24 18:46:21 -0500326 }
Mark Salterb6c6aed2015-09-01 09:36:05 -0400327 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500328}
329EXPORT_SYMBOL(mdiobus_unregister);
330
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700331/**
332 * mdiobus_free - free a struct mii_bus
333 * @bus: mii_bus to free
334 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700335 * This function releases the reference to the underlying device
336 * object in the mii_bus. If this is the last reference, the mii_bus
337 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700338 */
339void mdiobus_free(struct mii_bus *bus)
340{
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300341 /* For compatibility with error handling in drivers. */
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700342 if (bus->state == MDIOBUS_ALLOCATED) {
343 kfree(bus);
344 return;
345 }
346
347 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
348 bus->state = MDIOBUS_RELEASED;
349
350 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700351}
352EXPORT_SYMBOL(mdiobus_free);
353
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200354struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
355{
356 struct phy_device *phydev;
357 int err;
358
David Daneyac28b9f2012-06-27 07:33:35 +0000359 phydev = get_phy_device(bus, addr, false);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200360 if (IS_ERR(phydev) || phydev == NULL)
361 return phydev;
362
Daniel Mack86f6cf42014-05-24 09:34:26 +0200363 /*
364 * For DT, see if the auto-probed phy has a correspoding child
365 * in the bus node, and set the of_node pointer in this case.
366 */
367 of_mdiobus_link_phydev(bus, phydev);
368
Grant Likely4dea5472009-04-25 12:52:46 +0000369 err = phy_device_register(phydev);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200370 if (err) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200371 phy_device_free(phydev);
Grant Likely4dea5472009-04-25 12:52:46 +0000372 return NULL;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200373 }
374
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200375 return phydev;
376}
377EXPORT_SYMBOL(mdiobus_scan);
378
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800379/**
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200380 * mdiobus_read_nested - Nested version of the mdiobus_read function
381 * @bus: the mii_bus struct
382 * @addr: the phy address
383 * @regnum: register number to read
384 *
385 * In case of nested MDIO bus access avoid lockdep false positives by
386 * using mutex_lock_nested().
387 *
388 * NOTE: MUST NOT be called from interrupt context,
389 * because the bus read/write functions may wait for an interrupt
390 * to conclude the operation.
391 */
392int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
393{
394 int retval;
395
396 BUG_ON(in_interrupt());
397
398 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
399 retval = bus->read(bus, addr, regnum);
400 mutex_unlock(&bus->mdio_lock);
401
402 return retval;
403}
404EXPORT_SYMBOL(mdiobus_read_nested);
405
406/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000407 * mdiobus_read - Convenience function for reading a given MII mgmt register
408 * @bus: the mii_bus struct
409 * @addr: the phy address
410 * @regnum: register number to read
411 *
412 * NOTE: MUST NOT be called from interrupt context,
413 * because the bus read/write functions may wait for an interrupt
414 * to conclude the operation.
415 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000416int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000417{
418 int retval;
419
420 BUG_ON(in_interrupt());
421
422 mutex_lock(&bus->mdio_lock);
423 retval = bus->read(bus, addr, regnum);
424 mutex_unlock(&bus->mdio_lock);
425
426 return retval;
427}
428EXPORT_SYMBOL(mdiobus_read);
429
430/**
Neil Armstrong21dd19f2015-10-22 10:37:49 +0200431 * mdiobus_write_nested - Nested version of the mdiobus_write function
432 * @bus: the mii_bus struct
433 * @addr: the phy address
434 * @regnum: register number to write
435 * @val: value to write to @regnum
436 *
437 * In case of nested MDIO bus access avoid lockdep false positives by
438 * using mutex_lock_nested().
439 *
440 * NOTE: MUST NOT be called from interrupt context,
441 * because the bus read/write functions may wait for an interrupt
442 * to conclude the operation.
443 */
444int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
445{
446 int err;
447
448 BUG_ON(in_interrupt());
449
450 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
451 err = bus->write(bus, addr, regnum, val);
452 mutex_unlock(&bus->mdio_lock);
453
454 return err;
455}
456EXPORT_SYMBOL(mdiobus_write_nested);
457
458/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000459 * mdiobus_write - Convenience function for writing a given MII mgmt register
460 * @bus: the mii_bus struct
461 * @addr: the phy address
462 * @regnum: register number to write
463 * @val: value to write to @regnum
464 *
465 * NOTE: MUST NOT be called from interrupt context,
466 * because the bus read/write functions may wait for an interrupt
467 * to conclude the operation.
468 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000469int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000470{
471 int err;
472
473 BUG_ON(in_interrupt());
474
475 mutex_lock(&bus->mdio_lock);
476 err = bus->write(bus, addr, regnum, val);
477 mutex_unlock(&bus->mdio_lock);
478
479 return err;
480}
481EXPORT_SYMBOL(mdiobus_write);
482
483/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800484 * mdio_bus_match - determine if given PHY driver supports the given PHY device
485 * @dev: target PHY device
486 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400487 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800488 * Description: Given a PHY device, and a PHY driver, return 1 if
489 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400490 */
491static int mdio_bus_match(struct device *dev, struct device_driver *drv)
492{
493 struct phy_device *phydev = to_phy_device(dev);
494 struct phy_driver *phydrv = to_phy_driver(drv);
Shaohui Xiee0536cd2015-07-17 18:07:19 +0800495 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
496 int i;
Andy Fleming00db8182005-07-30 19:31:23 -0400497
David Daneya30e2c12012-06-27 07:33:37 +0000498 if (of_driver_match_device(dev, drv))
499 return 1;
500
501 if (phydrv->match_phy_device)
502 return phydrv->match_phy_device(phydev);
503
Shaohui Xiee0536cd2015-07-17 18:07:19 +0800504 if (phydev->is_c45) {
505 for (i = 1; i < num_ids; i++) {
506 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
507 continue;
508
509 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
510 (phydev->c45_ids.device_ids[i] &
511 phydrv->phy_id_mask))
512 return 1;
513 }
514 return 0;
515 } else {
516 return (phydrv->phy_id & phydrv->phy_id_mask) ==
517 (phydev->phy_id & phydrv->phy_id_mask);
518 }
Andy Fleming00db8182005-07-30 19:31:23 -0400519}
520
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000521#ifdef CONFIG_PM
522
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800523static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
524{
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100525 struct device_driver *drv = phydev->mdio.dev.driver;
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800526 struct phy_driver *phydrv = to_phy_driver(drv);
527 struct net_device *netdev = phydev->attached_dev;
528
529 if (!drv || !phydrv->suspend)
530 return false;
531
Florian Fainelli803dd9c2015-01-26 22:05:40 -0800532 /* PHY not attached? May suspend if the PHY has not already been
533 * suspended as part of a prior call to phy_disconnect() ->
534 * phy_detach() -> phy_suspend() because the parent netdev might be the
535 * MDIO bus driver and clock gated at this point.
536 */
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800537 if (!netdev)
Florian Fainelli803dd9c2015-01-26 22:05:40 -0800538 return !phydev->suspended;
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800539
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300540 /* Don't suspend PHY if the attched netdev parent may wakeup.
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800541 * The parent may point to a PCI device, as in tg3 driver.
542 */
543 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
544 return false;
545
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300546 /* Also don't suspend PHY if the netdev itself may wakeup. This
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800547 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
548 * e.g. SoC devices.
549 */
550 if (device_may_wakeup(&netdev->dev))
551 return false;
552
553 return true;
554}
555
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000556static int mdio_bus_suspend(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400557{
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800558 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400559
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300560 /* We must stop the state machine manually, otherwise it stops out of
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000561 * control, possibly with the phydev->lock held. Upon resume, netdev
562 * may call phy routines that try to grab the same lock, and that may
563 * lead to a deadlock.
564 */
Simon Guinotfddd9102010-09-13 22:12:01 +0000565 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000566 phy_stop_machine(phydev);
567
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800568 if (!mdio_bus_phy_may_suspend(phydev))
569 return 0;
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000570
Florian Fainelli9272efa2015-01-26 22:05:37 -0800571 return phy_suspend(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400572}
573
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000574static int mdio_bus_resume(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400575{
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800576 struct phy_device *phydev = to_phy_device(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000577 int ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400578
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800579 if (!mdio_bus_phy_may_suspend(phydev))
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000580 goto no_resume;
581
Florian Fainelli9272efa2015-01-26 22:05:37 -0800582 ret = phy_resume(phydev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000583 if (ret < 0)
584 return ret;
585
586no_resume:
Simon Guinotfddd9102010-09-13 22:12:01 +0000587 if (phydev->attached_dev && phydev->adjust_link)
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300588 phy_start_machine(phydev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000589
590 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400591}
592
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000593static int mdio_bus_restore(struct device *dev)
594{
595 struct phy_device *phydev = to_phy_device(dev);
596 struct net_device *netdev = phydev->attached_dev;
597 int ret;
598
599 if (!netdev)
600 return 0;
601
602 ret = phy_init_hw(phydev);
603 if (ret < 0)
604 return ret;
605
606 /* The PHY needs to renegotiate. */
607 phydev->link = 0;
608 phydev->state = PHY_UP;
609
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300610 phy_start_machine(phydev);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000611
612 return 0;
613}
614
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300615static const struct dev_pm_ops mdio_bus_pm_ops = {
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000616 .suspend = mdio_bus_suspend,
617 .resume = mdio_bus_resume,
618 .freeze = mdio_bus_suspend,
619 .thaw = mdio_bus_resume,
620 .restore = mdio_bus_restore,
621};
622
623#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
624
625#else
626
627#define MDIO_BUS_PM_OPS NULL
628
629#endif /* CONFIG_PM */
630
Nick Bowler56277f42012-11-07 06:20:34 +0000631static ssize_t
632phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
633{
634 struct phy_device *phydev = to_phy_device(dev);
635
636 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
637}
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700638static DEVICE_ATTR_RO(phy_id);
Nick Bowler56277f42012-11-07 06:20:34 +0000639
Florian Fainelli3d055d82014-02-11 17:27:40 -0800640static ssize_t
641phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
642{
643 struct phy_device *phydev = to_phy_device(dev);
Florian Fainelli574746d2014-08-27 11:44:33 -0700644 const char *mode = NULL;
Florian Fainelli3d055d82014-02-11 17:27:40 -0800645
Florian Fainelli574746d2014-08-27 11:44:33 -0700646 if (phy_is_internal(phydev))
647 mode = "internal";
648 else
649 mode = phy_modes(phydev->interface);
650
651 return sprintf(buf, "%s\n", mode);
Florian Fainelli3d055d82014-02-11 17:27:40 -0800652}
653static DEVICE_ATTR_RO(phy_interface);
654
Florian Fainelli8bed12852014-02-11 17:27:42 -0800655static ssize_t
656phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
657{
658 struct phy_device *phydev = to_phy_device(dev);
659
660 return sprintf(buf, "%d\n", phydev->has_fixups);
661}
662static DEVICE_ATTR_RO(phy_has_fixups);
663
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700664static struct attribute *mdio_dev_attrs[] = {
665 &dev_attr_phy_id.attr,
Florian Fainelli3d055d82014-02-11 17:27:40 -0800666 &dev_attr_phy_interface.attr,
Florian Fainelli8bed12852014-02-11 17:27:42 -0800667 &dev_attr_phy_has_fixups.attr,
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700668 NULL,
Nick Bowler56277f42012-11-07 06:20:34 +0000669};
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700670ATTRIBUTE_GROUPS(mdio_dev);
Nick Bowler56277f42012-11-07 06:20:34 +0000671
Andy Fleming00db8182005-07-30 19:31:23 -0400672struct bus_type mdio_bus_type = {
673 .name = "mdio_bus",
674 .match = mdio_bus_match,
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000675 .pm = MDIO_BUS_PM_OPS,
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700676 .dev_groups = mdio_dev_groups,
Andy Fleming00db8182005-07-30 19:31:23 -0400677};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700678EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400679
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400680int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400681{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700682 int ret;
683
684 ret = class_register(&mdio_bus_class);
685 if (!ret) {
686 ret = bus_register(&mdio_bus_type);
687 if (ret)
688 class_unregister(&mdio_bus_class);
689 }
690
691 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400692}
693
Peter Chubbdc85dec2005-09-03 14:05:06 -0700694void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500695{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700696 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500697 bus_unregister(&mdio_bus_type);
698}