blob: 2e58aa54484c9ca4e3154e231a3af2766bc84933 [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;
Lennert Buytenhek46abc022008-10-08 16:33:40 -070054
Timur Tabieb8a54a72012-01-12 15:23:04 -080055 /* If we alloc extra space, it should be aligned */
56 if (size)
57 alloc_size = aligned_size + size;
58 else
59 alloc_size = sizeof(*bus);
60
61 bus = kzalloc(alloc_size, GFP_KERNEL);
62 if (bus) {
Lennert Buytenhek46abc022008-10-08 16:33:40 -070063 bus->state = MDIOBUS_ALLOCATED;
Timur Tabieb8a54a72012-01-12 15:23:04 -080064 if (size)
65 bus->priv = (void *)bus + aligned_size;
66 }
Lennert Buytenhek46abc022008-10-08 16:33:40 -070067
68 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070069}
Timur Tabieb8a54a72012-01-12 15:23:04 -080070EXPORT_SYMBOL(mdiobus_alloc_size);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070071
Grygorii Strashko6d48f442014-04-30 15:23:33 +030072static void _devm_mdiobus_free(struct device *dev, void *res)
73{
74 mdiobus_free(*(struct mii_bus **)res);
75}
76
77static int devm_mdiobus_match(struct device *dev, void *res, void *data)
78{
79 struct mii_bus **r = res;
80
81 if (WARN_ON(!r || !*r))
82 return 0;
83
84 return *r == data;
85}
86
87/**
88 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
89 * @dev: Device to allocate mii_bus for
90 * @sizeof_priv: Space to allocate for private structure.
91 *
92 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
93 * automatically freed on driver detach.
94 *
95 * If an mii_bus allocated with this function needs to be freed separately,
96 * devm_mdiobus_free() must be used.
97 *
98 * RETURNS:
99 * Pointer to allocated mii_bus on success, NULL on failure.
100 */
101struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
102{
103 struct mii_bus **ptr, *bus;
104
105 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
106 if (!ptr)
107 return NULL;
108
109 /* use raw alloc_dr for kmalloc caller tracing */
110 bus = mdiobus_alloc_size(sizeof_priv);
111 if (bus) {
112 *ptr = bus;
113 devres_add(dev, ptr);
114 } else {
115 devres_free(ptr);
116 }
117
118 return bus;
119}
Arnd Bergmann93dccc52014-05-08 16:46:52 +0200120EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
Grygorii Strashko6d48f442014-04-30 15:23:33 +0300121
122/**
123 * devm_mdiobus_free - Resource-managed mdiobus_free()
124 * @dev: Device this mii_bus belongs to
125 * @bus: the mii_bus associated with the device
126 *
127 * Free mii_bus allocated with devm_mdiobus_alloc_size().
128 */
129void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
130{
131 int rc;
132
133 rc = devres_release(dev, _devm_mdiobus_free,
134 devm_mdiobus_match, bus);
135 WARN_ON(rc);
136}
137EXPORT_SYMBOL_GPL(devm_mdiobus_free);
138
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700139/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700140 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -0700141 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700142 *
143 * Description: called when the last reference to an mii_bus is
144 * dropped, to free the underlying memory.
145 */
146static void mdiobus_release(struct device *d)
147{
148 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800149 BUG_ON(bus->state != MDIOBUS_RELEASED &&
150 /* for compatibility with error handling in drivers */
151 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700152 kfree(bus);
153}
154
155static struct class mdio_bus_class = {
156 .name = "mdio_bus",
157 .dev_release = mdiobus_release,
158};
159
Bjørn Morkb943fbb2012-05-11 05:47:01 +0000160#if IS_ENABLED(CONFIG_OF_MDIO)
David Daney25106022012-05-02 15:16:37 +0000161/* Helper function for of_mdio_find_bus */
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100162static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
David Daney25106022012-05-02 15:16:37 +0000163{
164 return dev->of_node == mdio_bus_np;
165}
166/**
167 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
Randy Dunlapf41ef2e2012-06-08 14:07:19 +0000168 * @mdio_bus_np: Pointer to the mii_bus.
David Daney25106022012-05-02 15:16:37 +0000169 *
170 * Returns a pointer to the mii_bus, or NULL if none found.
171 *
172 * Because the association of a device_node and mii_bus is made via
173 * of_mdiobus_register(), the mii_bus cannot be found before it is
174 * registered with of_mdiobus_register().
175 *
176 */
177struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
178{
179 struct device *d;
180
181 if (!mdio_bus_np)
182 return NULL;
183
184 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
185 of_mdio_bus_match);
186
187 return d ? to_mii_bus(d) : NULL;
188}
189EXPORT_SYMBOL(of_mdio_find_bus);
190#endif
191
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700192/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800193 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
194 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -0500195 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800196 * Description: Called by a bus driver to bring up all the PHYs
197 * on a given bus, and attach them to the bus.
198 *
199 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500200 */
201int mdiobus_register(struct mii_bus *bus)
202{
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800203 int i, err;
Andy Fleminge1393452005-08-24 18:46:21 -0500204
Andy Fleminge1393452005-08-24 18:46:21 -0500205 if (NULL == bus || NULL == bus->name ||
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300206 NULL == bus->read || NULL == bus->write)
Andy Fleminge1393452005-08-24 18:46:21 -0500207 return -EINVAL;
208
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700209 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
210 bus->state != MDIOBUS_UNREGISTERED);
211
212 bus->dev.parent = bus->parent;
213 bus->dev.class = &mdio_bus_class;
214 bus->dev.groups = NULL;
Stephen Hemminger036b6682009-02-26 10:19:36 +0000215 dev_set_name(&bus->dev, "%s", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700216
217 err = device_register(&bus->dev);
218 if (err) {
Joe Perches8d242482012-06-09 07:49:07 +0000219 pr_err("mii_bus %s failed to register\n", bus->id);
Levente Kurusa0c692d02014-01-30 15:45:46 -0800220 put_device(&bus->dev);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700221 return -EINVAL;
222 }
223
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200224 mutex_init(&bus->mdio_lock);
225
Andy Fleminge1393452005-08-24 18:46:21 -0500226 if (bus->reset)
227 bus->reset(bus);
228
229 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200230 if ((bus->phy_mask & (1 << i)) == 0) {
231 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500232
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200233 phydev = mdiobus_scan(bus, i);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800234 if (IS_ERR(phydev)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200235 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800236 goto error;
237 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400238 }
Andy Fleminge1393452005-08-24 18:46:21 -0500239 }
240
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800241 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500242 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800243 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500244
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800245error:
246 while (--i >= 0) {
247 if (bus->phy_map[i])
248 device_unregister(&bus->phy_map[i]->dev);
249 }
250 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500251 return err;
252}
253EXPORT_SYMBOL(mdiobus_register);
254
255void mdiobus_unregister(struct mii_bus *bus)
256{
257 int i;
258
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700259 BUG_ON(bus->state != MDIOBUS_REGISTERED);
260 bus->state = MDIOBUS_UNREGISTERED;
261
Lennert Buytenhek3e440172008-11-09 05:34:47 +0100262 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500263 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300264 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500265 device_unregister(&bus->phy_map[i]->dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000266 bus->phy_map[i] = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500267 }
268}
269EXPORT_SYMBOL(mdiobus_unregister);
270
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700271/**
272 * mdiobus_free - free a struct mii_bus
273 * @bus: mii_bus to free
274 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700275 * This function releases the reference to the underlying device
276 * object in the mii_bus. If this is the last reference, the mii_bus
277 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700278 */
279void mdiobus_free(struct mii_bus *bus)
280{
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300281 /* For compatibility with error handling in drivers. */
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700282 if (bus->state == MDIOBUS_ALLOCATED) {
283 kfree(bus);
284 return;
285 }
286
287 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
288 bus->state = MDIOBUS_RELEASED;
289
290 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700291}
292EXPORT_SYMBOL(mdiobus_free);
293
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200294struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
295{
296 struct phy_device *phydev;
297 int err;
298
David Daneyac28b9f2012-06-27 07:33:35 +0000299 phydev = get_phy_device(bus, addr, false);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200300 if (IS_ERR(phydev) || phydev == NULL)
301 return phydev;
302
Daniel Mack86f6cf42014-05-24 09:34:26 +0200303 /*
304 * For DT, see if the auto-probed phy has a correspoding child
305 * in the bus node, and set the of_node pointer in this case.
306 */
307 of_mdiobus_link_phydev(bus, phydev);
308
Grant Likely4dea5472009-04-25 12:52:46 +0000309 err = phy_device_register(phydev);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200310 if (err) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200311 phy_device_free(phydev);
Grant Likely4dea5472009-04-25 12:52:46 +0000312 return NULL;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200313 }
314
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200315 return phydev;
316}
317EXPORT_SYMBOL(mdiobus_scan);
318
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800319/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000320 * mdiobus_read - Convenience function for reading a given MII mgmt register
321 * @bus: the mii_bus struct
322 * @addr: the phy address
323 * @regnum: register number to read
324 *
325 * NOTE: MUST NOT be called from interrupt context,
326 * because the bus read/write functions may wait for an interrupt
327 * to conclude the operation.
328 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000329int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000330{
331 int retval;
332
333 BUG_ON(in_interrupt());
334
335 mutex_lock(&bus->mdio_lock);
336 retval = bus->read(bus, addr, regnum);
337 mutex_unlock(&bus->mdio_lock);
338
339 return retval;
340}
341EXPORT_SYMBOL(mdiobus_read);
342
343/**
344 * mdiobus_write - Convenience function for writing a given MII mgmt register
345 * @bus: the mii_bus struct
346 * @addr: the phy address
347 * @regnum: register number to write
348 * @val: value to write to @regnum
349 *
350 * NOTE: MUST NOT be called from interrupt context,
351 * because the bus read/write functions may wait for an interrupt
352 * to conclude the operation.
353 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000354int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000355{
356 int err;
357
358 BUG_ON(in_interrupt());
359
360 mutex_lock(&bus->mdio_lock);
361 err = bus->write(bus, addr, regnum, val);
362 mutex_unlock(&bus->mdio_lock);
363
364 return err;
365}
366EXPORT_SYMBOL(mdiobus_write);
367
368/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800369 * mdio_bus_match - determine if given PHY driver supports the given PHY device
370 * @dev: target PHY device
371 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400372 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800373 * Description: Given a PHY device, and a PHY driver, return 1 if
374 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400375 */
376static int mdio_bus_match(struct device *dev, struct device_driver *drv)
377{
378 struct phy_device *phydev = to_phy_device(dev);
379 struct phy_driver *phydrv = to_phy_driver(drv);
380
David Daneya30e2c12012-06-27 07:33:37 +0000381 if (of_driver_match_device(dev, drv))
382 return 1;
383
384 if (phydrv->match_phy_device)
385 return phydrv->match_phy_device(phydev);
386
Florian Fainelli6f901b72013-12-17 21:38:10 -0800387 return (phydrv->phy_id & phydrv->phy_id_mask) ==
388 (phydev->phy_id & phydrv->phy_id_mask);
Andy Fleming00db8182005-07-30 19:31:23 -0400389}
390
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000391#ifdef CONFIG_PM
392
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800393static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
394{
395 struct device_driver *drv = phydev->dev.driver;
396 struct phy_driver *phydrv = to_phy_driver(drv);
397 struct net_device *netdev = phydev->attached_dev;
398
399 if (!drv || !phydrv->suspend)
400 return false;
401
402 /* PHY not attached? May suspend. */
403 if (!netdev)
404 return true;
405
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300406 /* Don't suspend PHY if the attched netdev parent may wakeup.
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800407 * The parent may point to a PCI device, as in tg3 driver.
408 */
409 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
410 return false;
411
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300412 /* Also don't suspend PHY if the netdev itself may wakeup. This
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800413 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
414 * e.g. SoC devices.
415 */
416 if (device_may_wakeup(&netdev->dev))
417 return false;
418
419 return true;
420}
421
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000422static int mdio_bus_suspend(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400423{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800424 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800425 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400426
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300427 /* We must stop the state machine manually, otherwise it stops out of
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000428 * control, possibly with the phydev->lock held. Upon resume, netdev
429 * may call phy routines that try to grab the same lock, and that may
430 * lead to a deadlock.
431 */
Simon Guinotfddd9102010-09-13 22:12:01 +0000432 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000433 phy_stop_machine(phydev);
434
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800435 if (!mdio_bus_phy_may_suspend(phydev))
436 return 0;
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000437
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800438 return phydrv->suspend(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400439}
440
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000441static int mdio_bus_resume(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400442{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800443 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800444 struct phy_device *phydev = to_phy_device(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000445 int ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400446
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800447 if (!mdio_bus_phy_may_suspend(phydev))
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000448 goto no_resume;
449
450 ret = phydrv->resume(phydev);
451 if (ret < 0)
452 return ret;
453
454no_resume:
Simon Guinotfddd9102010-09-13 22:12:01 +0000455 if (phydev->attached_dev && phydev->adjust_link)
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300456 phy_start_machine(phydev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000457
458 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400459}
460
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000461static int mdio_bus_restore(struct device *dev)
462{
463 struct phy_device *phydev = to_phy_device(dev);
464 struct net_device *netdev = phydev->attached_dev;
465 int ret;
466
467 if (!netdev)
468 return 0;
469
470 ret = phy_init_hw(phydev);
471 if (ret < 0)
472 return ret;
473
474 /* The PHY needs to renegotiate. */
475 phydev->link = 0;
476 phydev->state = PHY_UP;
477
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300478 phy_start_machine(phydev);
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000479
480 return 0;
481}
482
Sergei Shtylyov02d320c2014-01-05 03:18:27 +0300483static const struct dev_pm_ops mdio_bus_pm_ops = {
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000484 .suspend = mdio_bus_suspend,
485 .resume = mdio_bus_resume,
486 .freeze = mdio_bus_suspend,
487 .thaw = mdio_bus_resume,
488 .restore = mdio_bus_restore,
489};
490
491#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
492
493#else
494
495#define MDIO_BUS_PM_OPS NULL
496
497#endif /* CONFIG_PM */
498
Nick Bowler56277f42012-11-07 06:20:34 +0000499static ssize_t
500phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
501{
502 struct phy_device *phydev = to_phy_device(dev);
503
504 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
505}
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700506static DEVICE_ATTR_RO(phy_id);
Nick Bowler56277f42012-11-07 06:20:34 +0000507
Florian Fainelli3d055d82014-02-11 17:27:40 -0800508static ssize_t
509phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
510{
511 struct phy_device *phydev = to_phy_device(dev);
512
513 return sprintf(buf, "%s\n", phy_modes(phydev->interface));
514}
515static DEVICE_ATTR_RO(phy_interface);
516
Florian Fainelli8bed12852014-02-11 17:27:42 -0800517static ssize_t
518phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
519{
520 struct phy_device *phydev = to_phy_device(dev);
521
522 return sprintf(buf, "%d\n", phydev->has_fixups);
523}
524static DEVICE_ATTR_RO(phy_has_fixups);
525
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700526static struct attribute *mdio_dev_attrs[] = {
527 &dev_attr_phy_id.attr,
Florian Fainelli3d055d82014-02-11 17:27:40 -0800528 &dev_attr_phy_interface.attr,
Florian Fainelli8bed12852014-02-11 17:27:42 -0800529 &dev_attr_phy_has_fixups.attr,
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700530 NULL,
Nick Bowler56277f42012-11-07 06:20:34 +0000531};
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700532ATTRIBUTE_GROUPS(mdio_dev);
Nick Bowler56277f42012-11-07 06:20:34 +0000533
Andy Fleming00db8182005-07-30 19:31:23 -0400534struct bus_type mdio_bus_type = {
535 .name = "mdio_bus",
536 .match = mdio_bus_match,
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000537 .pm = MDIO_BUS_PM_OPS,
Greg Kroah-Hartman4192c742013-10-06 23:55:41 -0700538 .dev_groups = mdio_dev_groups,
Andy Fleming00db8182005-07-30 19:31:23 -0400539};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700540EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400541
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400542int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400543{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700544 int ret;
545
546 ret = class_register(&mdio_bus_class);
547 if (!ret) {
548 ret = bus_register(&mdio_bus_type);
549 if (ret)
550 class_unregister(&mdio_bus_class);
551 }
552
553 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400554}
555
Peter Chubbdc85dec2005-09-03 14:05:06 -0700556void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500557{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700558 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500559 bus_unregister(&mdio_bus_type);
560}