Commit | Line | Data |
---|---|---|
02d320c3 | 1 | /* MDIO Bus interface |
00db8189 AF |
2 | * |
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 | */ | |
8d242488 JP |
13 | |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
15 | ||
00db8189 | 16 | #include <linux/kernel.h> |
00db8189 AF |
17 | #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> | |
3d1e4db2 | 24 | #include <linux/device.h> |
a30e2c18 | 25 | #include <linux/of_device.h> |
4085a7f0 | 26 | #include <linux/of_mdio.h> |
00db8189 AF |
27 | #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> | |
00db8189 AF |
33 | #include <linux/mii.h> |
34 | #include <linux/ethtool.h> | |
35 | #include <linux/phy.h> | |
02d320c3 SS |
36 | #include <linux/io.h> |
37 | #include <linux/uaccess.h> | |
00db8189 | 38 | |
00db8189 | 39 | #include <asm/irq.h> |
00db8189 | 40 | |
298cf9be | 41 | /** |
eb8a54a7 | 42 | * mdiobus_alloc_size - allocate a mii_bus structure |
af58f1d6 RD |
43 | * @size: extra amount of memory to allocate for private storage. |
44 | * If non-zero, then bus->priv is points to that memory. | |
298cf9be LB |
45 | * |
46 | * Description: called by a bus driver to allocate an mii_bus | |
47 | * structure to fill in. | |
48 | */ | |
eb8a54a7 | 49 | struct mii_bus *mdiobus_alloc_size(size_t size) |
298cf9be | 50 | { |
46abc021 | 51 | struct mii_bus *bus; |
eb8a54a7 TT |
52 | size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); |
53 | size_t alloc_size; | |
54 | ||
55 | /* 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); | |
46abc021 | 60 | |
eb8a54a7 TT |
61 | bus = kzalloc(alloc_size, GFP_KERNEL); |
62 | if (bus) { | |
46abc021 | 63 | bus->state = MDIOBUS_ALLOCATED; |
eb8a54a7 TT |
64 | if (size) |
65 | bus->priv = (void *)bus + aligned_size; | |
66 | } | |
46abc021 LB |
67 | |
68 | return bus; | |
298cf9be | 69 | } |
eb8a54a7 | 70 | EXPORT_SYMBOL(mdiobus_alloc_size); |
298cf9be | 71 | |
6d48f44b GS |
72 | static void _devm_mdiobus_free(struct device *dev, void *res) |
73 | { | |
74 | mdiobus_free(*(struct mii_bus **)res); | |
75 | } | |
76 | ||
77 | static 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 | */ | |
101 | struct 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 | } | |
93dccc59 | 120 | EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size); |
6d48f44b GS |
121 | |
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 | */ | |
129 | void 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 | } | |
137 | EXPORT_SYMBOL_GPL(devm_mdiobus_free); | |
138 | ||
46abc021 LB |
139 | /** |
140 | * mdiobus_release - mii_bus device release callback | |
78c36b15 | 141 | * @d: the target struct device that contains the mii_bus |
46abc021 LB |
142 | * |
143 | * Description: called when the last reference to an mii_bus is | |
144 | * dropped, to free the underlying memory. | |
145 | */ | |
146 | static void mdiobus_release(struct device *d) | |
147 | { | |
148 | struct mii_bus *bus = to_mii_bus(d); | |
161c8d2f KH |
149 | BUG_ON(bus->state != MDIOBUS_RELEASED && |
150 | /* for compatibility with error handling in drivers */ | |
151 | bus->state != MDIOBUS_ALLOCATED); | |
46abc021 LB |
152 | kfree(bus); |
153 | } | |
154 | ||
155 | static struct class mdio_bus_class = { | |
156 | .name = "mdio_bus", | |
157 | .dev_release = mdiobus_release, | |
158 | }; | |
159 | ||
b943fbb0 | 160 | #if IS_ENABLED(CONFIG_OF_MDIO) |
25106022 | 161 | /* Helper function for of_mdio_find_bus */ |
9f3b795a | 162 | static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np) |
25106022 DD |
163 | { |
164 | return dev->of_node == mdio_bus_np; | |
165 | } | |
166 | /** | |
167 | * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. | |
f41ef2e7 | 168 | * @mdio_bus_np: Pointer to the mii_bus. |
25106022 | 169 | * |
a1364421 RK |
170 | * Returns a reference to the mii_bus, or NULL if none found. The |
171 | * embedded struct device will have its reference count incremented, | |
172 | * and this must be put once the bus is finished with. | |
25106022 DD |
173 | * |
174 | * Because the association of a device_node and mii_bus is made via | |
175 | * of_mdiobus_register(), the mii_bus cannot be found before it is | |
176 | * registered with of_mdiobus_register(). | |
177 | * | |
178 | */ | |
179 | struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) | |
180 | { | |
181 | struct device *d; | |
182 | ||
183 | if (!mdio_bus_np) | |
184 | return NULL; | |
185 | ||
186 | d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np, | |
187 | of_mdio_bus_match); | |
188 | ||
189 | return d ? to_mii_bus(d) : NULL; | |
190 | } | |
191 | EXPORT_SYMBOL(of_mdio_find_bus); | |
d9daa247 DM |
192 | |
193 | /* Walk the list of subnodes of a mdio bus and look for a node that matches the | |
194 | * phy's address with its 'reg' property. If found, set the of_node pointer for | |
195 | * the phy. This allows auto-probed pyh devices to be supplied with information | |
196 | * passed in via DT. | |
197 | */ | |
198 | static void of_mdiobus_link_phydev(struct mii_bus *mdio, | |
199 | struct phy_device *phydev) | |
200 | { | |
201 | struct device *dev = &phydev->dev; | |
202 | struct device_node *child; | |
203 | ||
204 | if (dev->of_node || !mdio->dev.of_node) | |
205 | return; | |
206 | ||
207 | for_each_available_child_of_node(mdio->dev.of_node, child) { | |
208 | int addr; | |
209 | int ret; | |
210 | ||
211 | ret = of_property_read_u32(child, "reg", &addr); | |
212 | if (ret < 0) { | |
213 | dev_err(dev, "%s has invalid PHY address\n", | |
214 | child->full_name); | |
215 | continue; | |
216 | } | |
217 | ||
218 | /* A PHY must have a reg property in the range [0-31] */ | |
219 | if (addr >= PHY_MAX_ADDR) { | |
220 | dev_err(dev, "%s PHY address %i is too large\n", | |
221 | child->full_name, addr); | |
222 | continue; | |
223 | } | |
224 | ||
225 | if (addr == phydev->addr) { | |
226 | dev->of_node = child; | |
227 | return; | |
228 | } | |
229 | } | |
230 | } | |
231 | #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ | |
232 | static inline void of_mdiobus_link_phydev(struct mii_bus *mdio, | |
233 | struct phy_device *phydev) | |
234 | { | |
235 | } | |
25106022 DD |
236 | #endif |
237 | ||
b3df0da8 | 238 | /** |
59f06978 | 239 | * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
b3df0da8 | 240 | * @bus: target mii_bus |
59f06978 | 241 | * @owner: module containing bus accessor functions |
e1393456 | 242 | * |
b3df0da8 | 243 | * Description: Called by a bus driver to bring up all the PHYs |
59f06978 RK |
244 | * on a given bus, and attach them to the bus. Drivers should use |
245 | * mdiobus_register() rather than __mdiobus_register() unless they | |
246 | * need to pass a specific owner module. | |
b3df0da8 RD |
247 | * |
248 | * Returns 0 on success or < 0 on error. | |
e1393456 | 249 | */ |
3e3aaf64 | 250 | int __mdiobus_register(struct mii_bus *bus, struct module *owner) |
e1393456 | 251 | { |
161c8d2f | 252 | int i, err; |
e1393456 | 253 | |
e1393456 | 254 | if (NULL == bus || NULL == bus->name || |
02d320c3 | 255 | NULL == bus->read || NULL == bus->write) |
e1393456 AF |
256 | return -EINVAL; |
257 | ||
46abc021 LB |
258 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
259 | bus->state != MDIOBUS_UNREGISTERED); | |
260 | ||
3e3aaf64 | 261 | bus->owner = owner; |
46abc021 LB |
262 | bus->dev.parent = bus->parent; |
263 | bus->dev.class = &mdio_bus_class; | |
264 | bus->dev.groups = NULL; | |
036b6687 | 265 | dev_set_name(&bus->dev, "%s", bus->id); |
46abc021 LB |
266 | |
267 | err = device_register(&bus->dev); | |
268 | if (err) { | |
8d242488 | 269 | pr_err("mii_bus %s failed to register\n", bus->id); |
0c692d07 | 270 | put_device(&bus->dev); |
46abc021 LB |
271 | return -EINVAL; |
272 | } | |
273 | ||
d1e7fe4d AB |
274 | mutex_init(&bus->mdio_lock); |
275 | ||
e1393456 AF |
276 | if (bus->reset) |
277 | bus->reset(bus); | |
278 | ||
279 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
4fd5f812 LB |
280 | if ((bus->phy_mask & (1 << i)) == 0) { |
281 | struct phy_device *phydev; | |
e1393456 | 282 | |
4fd5f812 | 283 | phydev = mdiobus_scan(bus, i); |
161c8d2f | 284 | if (IS_ERR(phydev)) { |
4fd5f812 | 285 | err = PTR_ERR(phydev); |
161c8d2f KH |
286 | goto error; |
287 | } | |
64b1c2b4 | 288 | } |
e1393456 AF |
289 | } |
290 | ||
161c8d2f | 291 | bus->state = MDIOBUS_REGISTERED; |
e1393456 | 292 | pr_info("%s: probed\n", bus->name); |
161c8d2f | 293 | return 0; |
e1393456 | 294 | |
161c8d2f KH |
295 | error: |
296 | while (--i >= 0) { | |
38737e49 RK |
297 | struct phy_device *phydev = bus->phy_map[i]; |
298 | if (phydev) { | |
299 | phy_device_remove(phydev); | |
300 | phy_device_free(phydev); | |
301 | } | |
161c8d2f KH |
302 | } |
303 | device_del(&bus->dev); | |
e1393456 AF |
304 | return err; |
305 | } | |
3e3aaf64 | 306 | EXPORT_SYMBOL(__mdiobus_register); |
e1393456 AF |
307 | |
308 | void mdiobus_unregister(struct mii_bus *bus) | |
309 | { | |
310 | int i; | |
311 | ||
46abc021 LB |
312 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
313 | bus->state = MDIOBUS_UNREGISTERED; | |
314 | ||
e1393456 | 315 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
38737e49 RK |
316 | struct phy_device *phydev = bus->phy_map[i]; |
317 | if (phydev) { | |
318 | phy_device_remove(phydev); | |
319 | phy_device_free(phydev); | |
320 | } | |
e1393456 | 321 | } |
b6c6aedc | 322 | device_del(&bus->dev); |
e1393456 AF |
323 | } |
324 | EXPORT_SYMBOL(mdiobus_unregister); | |
325 | ||
298cf9be LB |
326 | /** |
327 | * mdiobus_free - free a struct mii_bus | |
328 | * @bus: mii_bus to free | |
329 | * | |
46abc021 LB |
330 | * This function releases the reference to the underlying device |
331 | * object in the mii_bus. If this is the last reference, the mii_bus | |
332 | * will be freed. | |
298cf9be LB |
333 | */ |
334 | void mdiobus_free(struct mii_bus *bus) | |
335 | { | |
02d320c3 | 336 | /* For compatibility with error handling in drivers. */ |
46abc021 LB |
337 | if (bus->state == MDIOBUS_ALLOCATED) { |
338 | kfree(bus); | |
339 | return; | |
340 | } | |
341 | ||
342 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); | |
343 | bus->state = MDIOBUS_RELEASED; | |
344 | ||
345 | put_device(&bus->dev); | |
298cf9be LB |
346 | } |
347 | EXPORT_SYMBOL(mdiobus_free); | |
348 | ||
4fd5f812 LB |
349 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
350 | { | |
351 | struct phy_device *phydev; | |
352 | int err; | |
353 | ||
ac28b9f8 | 354 | phydev = get_phy_device(bus, addr, false); |
4fd5f812 LB |
355 | if (IS_ERR(phydev) || phydev == NULL) |
356 | return phydev; | |
357 | ||
86f6cf41 DM |
358 | /* |
359 | * For DT, see if the auto-probed phy has a correspoding child | |
360 | * in the bus node, and set the of_node pointer in this case. | |
361 | */ | |
362 | of_mdiobus_link_phydev(bus, phydev); | |
363 | ||
4dea547f | 364 | err = phy_device_register(phydev); |
4fd5f812 | 365 | if (err) { |
4fd5f812 | 366 | phy_device_free(phydev); |
4dea547f | 367 | return NULL; |
4fd5f812 LB |
368 | } |
369 | ||
4fd5f812 LB |
370 | return phydev; |
371 | } | |
372 | EXPORT_SYMBOL(mdiobus_scan); | |
373 | ||
21dd19fe NA |
374 | /** |
375 | * mdiobus_read_nested - Nested version of the mdiobus_read function | |
376 | * @bus: the mii_bus struct | |
377 | * @addr: the phy address | |
378 | * @regnum: register number to read | |
379 | * | |
380 | * In case of nested MDIO bus access avoid lockdep false positives by | |
381 | * using mutex_lock_nested(). | |
382 | * | |
383 | * NOTE: MUST NOT be called from interrupt context, | |
384 | * because the bus read/write functions may wait for an interrupt | |
385 | * to conclude the operation. | |
386 | */ | |
387 | int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum) | |
388 | { | |
389 | int retval; | |
390 | ||
391 | BUG_ON(in_interrupt()); | |
392 | ||
393 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); | |
394 | retval = bus->read(bus, addr, regnum); | |
395 | mutex_unlock(&bus->mdio_lock); | |
396 | ||
397 | return retval; | |
398 | } | |
399 | EXPORT_SYMBOL(mdiobus_read_nested); | |
400 | ||
2e888103 LB |
401 | /** |
402 | * mdiobus_read - Convenience function for reading a given MII mgmt register | |
403 | * @bus: the mii_bus struct | |
404 | * @addr: the phy address | |
405 | * @regnum: register number to read | |
406 | * | |
407 | * NOTE: MUST NOT be called from interrupt context, | |
408 | * because the bus read/write functions may wait for an interrupt | |
409 | * to conclude the operation. | |
410 | */ | |
abf35df2 | 411 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
2e888103 LB |
412 | { |
413 | int retval; | |
414 | ||
415 | BUG_ON(in_interrupt()); | |
416 | ||
417 | mutex_lock(&bus->mdio_lock); | |
418 | retval = bus->read(bus, addr, regnum); | |
419 | mutex_unlock(&bus->mdio_lock); | |
420 | ||
421 | return retval; | |
422 | } | |
423 | EXPORT_SYMBOL(mdiobus_read); | |
424 | ||
21dd19fe NA |
425 | /** |
426 | * mdiobus_write_nested - Nested version of the mdiobus_write function | |
427 | * @bus: the mii_bus struct | |
428 | * @addr: the phy address | |
429 | * @regnum: register number to write | |
430 | * @val: value to write to @regnum | |
431 | * | |
432 | * In case of nested MDIO bus access avoid lockdep false positives by | |
433 | * using mutex_lock_nested(). | |
434 | * | |
435 | * NOTE: MUST NOT be called from interrupt context, | |
436 | * because the bus read/write functions may wait for an interrupt | |
437 | * to conclude the operation. | |
438 | */ | |
439 | int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val) | |
440 | { | |
441 | int err; | |
442 | ||
443 | BUG_ON(in_interrupt()); | |
444 | ||
445 | mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING); | |
446 | err = bus->write(bus, addr, regnum, val); | |
447 | mutex_unlock(&bus->mdio_lock); | |
448 | ||
449 | return err; | |
450 | } | |
451 | EXPORT_SYMBOL(mdiobus_write_nested); | |
452 | ||
2e888103 LB |
453 | /** |
454 | * mdiobus_write - Convenience function for writing a given MII mgmt register | |
455 | * @bus: the mii_bus struct | |
456 | * @addr: the phy address | |
457 | * @regnum: register number to write | |
458 | * @val: value to write to @regnum | |
459 | * | |
460 | * NOTE: MUST NOT be called from interrupt context, | |
461 | * because the bus read/write functions may wait for an interrupt | |
462 | * to conclude the operation. | |
463 | */ | |
abf35df2 | 464 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) |
2e888103 LB |
465 | { |
466 | int err; | |
467 | ||
468 | BUG_ON(in_interrupt()); | |
469 | ||
470 | mutex_lock(&bus->mdio_lock); | |
471 | err = bus->write(bus, addr, regnum, val); | |
472 | mutex_unlock(&bus->mdio_lock); | |
473 | ||
474 | return err; | |
475 | } | |
476 | EXPORT_SYMBOL(mdiobus_write); | |
477 | ||
b3df0da8 RD |
478 | /** |
479 | * mdio_bus_match - determine if given PHY driver supports the given PHY device | |
480 | * @dev: target PHY device | |
481 | * @drv: given PHY driver | |
00db8189 | 482 | * |
b3df0da8 RD |
483 | * Description: Given a PHY device, and a PHY driver, return 1 if |
484 | * the driver supports the device. Otherwise, return 0. | |
00db8189 AF |
485 | */ |
486 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) | |
487 | { | |
488 | struct phy_device *phydev = to_phy_device(dev); | |
489 | struct phy_driver *phydrv = to_phy_driver(drv); | |
e0536cd9 SX |
490 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
491 | int i; | |
00db8189 | 492 | |
a30e2c18 DD |
493 | if (of_driver_match_device(dev, drv)) |
494 | return 1; | |
495 | ||
496 | if (phydrv->match_phy_device) | |
497 | return phydrv->match_phy_device(phydev); | |
498 | ||
e0536cd9 SX |
499 | if (phydev->is_c45) { |
500 | for (i = 1; i < num_ids; i++) { | |
501 | if (!(phydev->c45_ids.devices_in_package & (1 << i))) | |
502 | continue; | |
503 | ||
504 | if ((phydrv->phy_id & phydrv->phy_id_mask) == | |
505 | (phydev->c45_ids.device_ids[i] & | |
506 | phydrv->phy_id_mask)) | |
507 | return 1; | |
508 | } | |
509 | return 0; | |
510 | } else { | |
511 | return (phydrv->phy_id & phydrv->phy_id_mask) == | |
512 | (phydev->phy_id & phydrv->phy_id_mask); | |
513 | } | |
00db8189 AF |
514 | } |
515 | ||
2f5cb434 AV |
516 | #ifdef CONFIG_PM |
517 | ||
3d1e4db2 AV |
518 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
519 | { | |
520 | struct device_driver *drv = phydev->dev.driver; | |
521 | struct phy_driver *phydrv = to_phy_driver(drv); | |
522 | struct net_device *netdev = phydev->attached_dev; | |
523 | ||
524 | if (!drv || !phydrv->suspend) | |
525 | return false; | |
526 | ||
803dd9c7 FF |
527 | /* PHY not attached? May suspend if the PHY has not already been |
528 | * suspended as part of a prior call to phy_disconnect() -> | |
529 | * phy_detach() -> phy_suspend() because the parent netdev might be the | |
530 | * MDIO bus driver and clock gated at this point. | |
531 | */ | |
3d1e4db2 | 532 | if (!netdev) |
803dd9c7 | 533 | return !phydev->suspended; |
3d1e4db2 | 534 | |
02d320c3 | 535 | /* Don't suspend PHY if the attched netdev parent may wakeup. |
3d1e4db2 AV |
536 | * The parent may point to a PCI device, as in tg3 driver. |
537 | */ | |
538 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) | |
539 | return false; | |
540 | ||
02d320c3 | 541 | /* Also don't suspend PHY if the netdev itself may wakeup. This |
3d1e4db2 AV |
542 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, |
543 | * e.g. SoC devices. | |
544 | */ | |
545 | if (device_may_wakeup(&netdev->dev)) | |
546 | return false; | |
547 | ||
548 | return true; | |
549 | } | |
550 | ||
2f5cb434 | 551 | static int mdio_bus_suspend(struct device *dev) |
00db8189 | 552 | { |
0f0ca340 | 553 | struct phy_device *phydev = to_phy_device(dev); |
00db8189 | 554 | |
02d320c3 | 555 | /* We must stop the state machine manually, otherwise it stops out of |
541cd3ee AV |
556 | * control, possibly with the phydev->lock held. Upon resume, netdev |
557 | * may call phy routines that try to grab the same lock, and that may | |
558 | * lead to a deadlock. | |
559 | */ | |
fddd9101 | 560 | if (phydev->attached_dev && phydev->adjust_link) |
541cd3ee AV |
561 | phy_stop_machine(phydev); |
562 | ||
3d1e4db2 AV |
563 | if (!mdio_bus_phy_may_suspend(phydev)) |
564 | return 0; | |
541cd3ee | 565 | |
9272efa2 | 566 | return phy_suspend(phydev); |
00db8189 AF |
567 | } |
568 | ||
2f5cb434 | 569 | static int mdio_bus_resume(struct device *dev) |
00db8189 | 570 | { |
0f0ca340 | 571 | struct phy_device *phydev = to_phy_device(dev); |
541cd3ee | 572 | int ret; |
00db8189 | 573 | |
3d1e4db2 | 574 | if (!mdio_bus_phy_may_suspend(phydev)) |
541cd3ee AV |
575 | goto no_resume; |
576 | ||
9272efa2 | 577 | ret = phy_resume(phydev); |
541cd3ee AV |
578 | if (ret < 0) |
579 | return ret; | |
580 | ||
581 | no_resume: | |
fddd9101 | 582 | if (phydev->attached_dev && phydev->adjust_link) |
29935aeb | 583 | phy_start_machine(phydev); |
541cd3ee AV |
584 | |
585 | return 0; | |
00db8189 AF |
586 | } |
587 | ||
2f5cb434 AV |
588 | static int mdio_bus_restore(struct device *dev) |
589 | { | |
590 | struct phy_device *phydev = to_phy_device(dev); | |
591 | struct net_device *netdev = phydev->attached_dev; | |
592 | int ret; | |
593 | ||
594 | if (!netdev) | |
595 | return 0; | |
596 | ||
597 | ret = phy_init_hw(phydev); | |
598 | if (ret < 0) | |
599 | return ret; | |
600 | ||
601 | /* The PHY needs to renegotiate. */ | |
602 | phydev->link = 0; | |
603 | phydev->state = PHY_UP; | |
604 | ||
29935aeb | 605 | phy_start_machine(phydev); |
2f5cb434 AV |
606 | |
607 | return 0; | |
608 | } | |
609 | ||
02d320c3 | 610 | static const struct dev_pm_ops mdio_bus_pm_ops = { |
2f5cb434 AV |
611 | .suspend = mdio_bus_suspend, |
612 | .resume = mdio_bus_resume, | |
613 | .freeze = mdio_bus_suspend, | |
614 | .thaw = mdio_bus_resume, | |
615 | .restore = mdio_bus_restore, | |
616 | }; | |
617 | ||
618 | #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops) | |
619 | ||
620 | #else | |
621 | ||
622 | #define MDIO_BUS_PM_OPS NULL | |
623 | ||
624 | #endif /* CONFIG_PM */ | |
625 | ||
56277f40 NB |
626 | static ssize_t |
627 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) | |
628 | { | |
629 | struct phy_device *phydev = to_phy_device(dev); | |
630 | ||
631 | return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); | |
632 | } | |
4192c749 | 633 | static DEVICE_ATTR_RO(phy_id); |
56277f40 | 634 | |
3d055d8d FF |
635 | static ssize_t |
636 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) | |
637 | { | |
638 | struct phy_device *phydev = to_phy_device(dev); | |
574746dd | 639 | const char *mode = NULL; |
3d055d8d | 640 | |
574746dd FF |
641 | if (phy_is_internal(phydev)) |
642 | mode = "internal"; | |
643 | else | |
644 | mode = phy_modes(phydev->interface); | |
645 | ||
646 | return sprintf(buf, "%s\n", mode); | |
3d055d8d FF |
647 | } |
648 | static DEVICE_ATTR_RO(phy_interface); | |
649 | ||
8bed1285 FF |
650 | static ssize_t |
651 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf) | |
652 | { | |
653 | struct phy_device *phydev = to_phy_device(dev); | |
654 | ||
655 | return sprintf(buf, "%d\n", phydev->has_fixups); | |
656 | } | |
657 | static DEVICE_ATTR_RO(phy_has_fixups); | |
658 | ||
4192c749 GKH |
659 | static struct attribute *mdio_dev_attrs[] = { |
660 | &dev_attr_phy_id.attr, | |
3d055d8d | 661 | &dev_attr_phy_interface.attr, |
8bed1285 | 662 | &dev_attr_phy_has_fixups.attr, |
4192c749 | 663 | NULL, |
56277f40 | 664 | }; |
4192c749 | 665 | ATTRIBUTE_GROUPS(mdio_dev); |
56277f40 | 666 | |
00db8189 AF |
667 | struct bus_type mdio_bus_type = { |
668 | .name = "mdio_bus", | |
669 | .match = mdio_bus_match, | |
2f5cb434 | 670 | .pm = MDIO_BUS_PM_OPS, |
4192c749 | 671 | .dev_groups = mdio_dev_groups, |
00db8189 | 672 | }; |
11b0bacd | 673 | EXPORT_SYMBOL(mdio_bus_type); |
00db8189 | 674 | |
67c4f3fa | 675 | int __init mdio_bus_init(void) |
00db8189 | 676 | { |
46abc021 LB |
677 | int ret; |
678 | ||
679 | ret = class_register(&mdio_bus_class); | |
680 | if (!ret) { | |
681 | ret = bus_register(&mdio_bus_type); | |
682 | if (ret) | |
683 | class_unregister(&mdio_bus_class); | |
684 | } | |
685 | ||
686 | return ret; | |
00db8189 AF |
687 | } |
688 | ||
dc85dec6 | 689 | void mdio_bus_exit(void) |
e1393456 | 690 | { |
46abc021 | 691 | class_unregister(&mdio_bus_class); |
e1393456 AF |
692 | bus_unregister(&mdio_bus_type); |
693 | } |