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; | |
e7f4dc35 | 54 | int i; |
eb8a54a7 TT |
55 | |
56 | /* 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); | |
46abc021 | 61 | |
eb8a54a7 TT |
62 | bus = kzalloc(alloc_size, GFP_KERNEL); |
63 | if (bus) { | |
46abc021 | 64 | bus->state = MDIOBUS_ALLOCATED; |
eb8a54a7 TT |
65 | if (size) |
66 | bus->priv = (void *)bus + aligned_size; | |
67 | } | |
46abc021 | 68 | |
e7f4dc35 AL |
69 | /* Initialise the interrupts to polling */ |
70 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
71 | bus->irq[i] = PHY_POLL; | |
72 | ||
46abc021 | 73 | return bus; |
298cf9be | 74 | } |
eb8a54a7 | 75 | EXPORT_SYMBOL(mdiobus_alloc_size); |
298cf9be | 76 | |
6d48f44b GS |
77 | static void _devm_mdiobus_free(struct device *dev, void *res) |
78 | { | |
79 | mdiobus_free(*(struct mii_bus **)res); | |
80 | } | |
81 | ||
82 | static 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 | */ | |
106 | struct 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 | } | |
93dccc59 | 125 | EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size); |
6d48f44b GS |
126 | |
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 | */ | |
134 | void 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 | } | |
142 | EXPORT_SYMBOL_GPL(devm_mdiobus_free); | |
143 | ||
46abc021 LB |
144 | /** |
145 | * mdiobus_release - mii_bus device release callback | |
78c36b15 | 146 | * @d: the target struct device that contains the mii_bus |
46abc021 LB |
147 | * |
148 | * Description: called when the last reference to an mii_bus is | |
149 | * dropped, to free the underlying memory. | |
150 | */ | |
151 | static void mdiobus_release(struct device *d) | |
152 | { | |
153 | struct mii_bus *bus = to_mii_bus(d); | |
161c8d2f KH |
154 | BUG_ON(bus->state != MDIOBUS_RELEASED && |
155 | /* for compatibility with error handling in drivers */ | |
156 | bus->state != MDIOBUS_ALLOCATED); | |
46abc021 LB |
157 | kfree(bus); |
158 | } | |
159 | ||
160 | static struct class mdio_bus_class = { | |
161 | .name = "mdio_bus", | |
162 | .dev_release = mdiobus_release, | |
163 | }; | |
164 | ||
b943fbb0 | 165 | #if IS_ENABLED(CONFIG_OF_MDIO) |
25106022 | 166 | /* Helper function for of_mdio_find_bus */ |
9f3b795a | 167 | static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np) |
25106022 DD |
168 | { |
169 | return dev->of_node == mdio_bus_np; | |
170 | } | |
171 | /** | |
172 | * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. | |
f41ef2e7 | 173 | * @mdio_bus_np: Pointer to the mii_bus. |
25106022 | 174 | * |
a1364421 RK |
175 | * 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. | |
25106022 DD |
178 | * |
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 | */ | |
184 | struct 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 | } | |
196 | EXPORT_SYMBOL(of_mdio_find_bus); | |
d9daa247 DM |
197 | |
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 | */ | |
203 | static void of_mdiobus_link_phydev(struct mii_bus *mdio, | |
204 | struct phy_device *phydev) | |
205 | { | |
206 | struct device *dev = &phydev->dev; | |
207 | struct device_node *child; | |
208 | ||
209 | if (dev->of_node || !mdio->dev.of_node) | |
210 | return; | |
211 | ||
212 | for_each_available_child_of_node(mdio->dev.of_node, child) { | |
213 | 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 | ||
230 | if (addr == phydev->addr) { | |
231 | dev->of_node = child; | |
232 | return; | |
233 | } | |
234 | } | |
235 | } | |
236 | #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ | |
237 | static inline void of_mdiobus_link_phydev(struct mii_bus *mdio, | |
238 | struct phy_device *phydev) | |
239 | { | |
240 | } | |
25106022 DD |
241 | #endif |
242 | ||
b3df0da8 | 243 | /** |
59f06978 | 244 | * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
b3df0da8 | 245 | * @bus: target mii_bus |
59f06978 | 246 | * @owner: module containing bus accessor functions |
e1393456 | 247 | * |
b3df0da8 | 248 | * Description: Called by a bus driver to bring up all the PHYs |
59f06978 RK |
249 | * 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. | |
b3df0da8 RD |
252 | * |
253 | * Returns 0 on success or < 0 on error. | |
e1393456 | 254 | */ |
3e3aaf64 | 255 | int __mdiobus_register(struct mii_bus *bus, struct module *owner) |
e1393456 | 256 | { |
161c8d2f | 257 | int i, err; |
e1393456 | 258 | |
e1393456 | 259 | if (NULL == bus || NULL == bus->name || |
02d320c3 | 260 | NULL == bus->read || NULL == bus->write) |
e1393456 AF |
261 | return -EINVAL; |
262 | ||
46abc021 LB |
263 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
264 | bus->state != MDIOBUS_UNREGISTERED); | |
265 | ||
3e3aaf64 | 266 | bus->owner = owner; |
46abc021 LB |
267 | bus->dev.parent = bus->parent; |
268 | bus->dev.class = &mdio_bus_class; | |
269 | bus->dev.groups = NULL; | |
036b6687 | 270 | dev_set_name(&bus->dev, "%s", bus->id); |
46abc021 LB |
271 | |
272 | err = device_register(&bus->dev); | |
273 | if (err) { | |
8d242488 | 274 | pr_err("mii_bus %s failed to register\n", bus->id); |
0c692d07 | 275 | put_device(&bus->dev); |
46abc021 LB |
276 | return -EINVAL; |
277 | } | |
278 | ||
d1e7fe4d AB |
279 | mutex_init(&bus->mdio_lock); |
280 | ||
e1393456 AF |
281 | if (bus->reset) |
282 | bus->reset(bus); | |
283 | ||
284 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
4fd5f812 LB |
285 | if ((bus->phy_mask & (1 << i)) == 0) { |
286 | struct phy_device *phydev; | |
e1393456 | 287 | |
4fd5f812 | 288 | phydev = mdiobus_scan(bus, i); |
161c8d2f | 289 | if (IS_ERR(phydev)) { |
4fd5f812 | 290 | err = PTR_ERR(phydev); |
161c8d2f KH |
291 | goto error; |
292 | } | |
64b1c2b4 | 293 | } |
e1393456 AF |
294 | } |
295 | ||
161c8d2f | 296 | bus->state = MDIOBUS_REGISTERED; |
e1393456 | 297 | pr_info("%s: probed\n", bus->name); |
161c8d2f | 298 | return 0; |
e1393456 | 299 | |
161c8d2f KH |
300 | error: |
301 | while (--i >= 0) { | |
38737e49 RK |
302 | struct phy_device *phydev = bus->phy_map[i]; |
303 | if (phydev) { | |
304 | phy_device_remove(phydev); | |
305 | phy_device_free(phydev); | |
306 | } | |
161c8d2f KH |
307 | } |
308 | device_del(&bus->dev); | |
e1393456 AF |
309 | return err; |
310 | } | |
3e3aaf64 | 311 | EXPORT_SYMBOL(__mdiobus_register); |
e1393456 AF |
312 | |
313 | void mdiobus_unregister(struct mii_bus *bus) | |
314 | { | |
315 | int i; | |
316 | ||
46abc021 LB |
317 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
318 | bus->state = MDIOBUS_UNREGISTERED; | |
319 | ||
e1393456 | 320 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
38737e49 RK |
321 | struct phy_device *phydev = bus->phy_map[i]; |
322 | if (phydev) { | |
323 | phy_device_remove(phydev); | |
324 | phy_device_free(phydev); | |
325 | } | |
e1393456 | 326 | } |
b6c6aedc | 327 | device_del(&bus->dev); |
e1393456 AF |
328 | } |
329 | EXPORT_SYMBOL(mdiobus_unregister); | |
330 | ||
298cf9be LB |
331 | /** |
332 | * mdiobus_free - free a struct mii_bus | |
333 | * @bus: mii_bus to free | |
334 | * | |
46abc021 LB |
335 | * 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. | |
298cf9be LB |
338 | */ |
339 | void mdiobus_free(struct mii_bus *bus) | |
340 | { | |
02d320c3 | 341 | /* For compatibility with error handling in drivers. */ |
46abc021 LB |
342 | 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); | |
298cf9be LB |
351 | } |
352 | EXPORT_SYMBOL(mdiobus_free); | |
353 | ||
4fd5f812 LB |
354 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
355 | { | |
356 | struct phy_device *phydev; | |
357 | int err; | |
358 | ||
ac28b9f8 | 359 | phydev = get_phy_device(bus, addr, false); |
4fd5f812 LB |
360 | if (IS_ERR(phydev) || phydev == NULL) |
361 | return phydev; | |
362 | ||
86f6cf41 DM |
363 | /* |
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 | ||
4dea547f | 369 | err = phy_device_register(phydev); |
4fd5f812 | 370 | if (err) { |
4fd5f812 | 371 | phy_device_free(phydev); |
4dea547f | 372 | return NULL; |
4fd5f812 LB |
373 | } |
374 | ||
4fd5f812 LB |
375 | return phydev; |
376 | } | |
377 | EXPORT_SYMBOL(mdiobus_scan); | |
378 | ||
21dd19fe NA |
379 | /** |
380 | * 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 | */ | |
392 | int 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 | } | |
404 | EXPORT_SYMBOL(mdiobus_read_nested); | |
405 | ||
2e888103 LB |
406 | /** |
407 | * 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 | */ | |
abf35df2 | 416 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
2e888103 LB |
417 | { |
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 | } | |
428 | EXPORT_SYMBOL(mdiobus_read); | |
429 | ||
21dd19fe NA |
430 | /** |
431 | * 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 | */ | |
444 | int 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 | } | |
456 | EXPORT_SYMBOL(mdiobus_write_nested); | |
457 | ||
2e888103 LB |
458 | /** |
459 | * 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 | */ | |
abf35df2 | 469 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) |
2e888103 LB |
470 | { |
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 | } | |
481 | EXPORT_SYMBOL(mdiobus_write); | |
482 | ||
b3df0da8 RD |
483 | /** |
484 | * mdio_bus_match - determine if given PHY driver supports the given PHY device | |
485 | * @dev: target PHY device | |
486 | * @drv: given PHY driver | |
00db8189 | 487 | * |
b3df0da8 RD |
488 | * Description: Given a PHY device, and a PHY driver, return 1 if |
489 | * the driver supports the device. Otherwise, return 0. | |
00db8189 AF |
490 | */ |
491 | static 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); | |
e0536cd9 SX |
495 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
496 | int i; | |
00db8189 | 497 | |
a30e2c18 DD |
498 | 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 | ||
e0536cd9 SX |
504 | 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 | } | |
00db8189 AF |
519 | } |
520 | ||
2f5cb434 AV |
521 | #ifdef CONFIG_PM |
522 | ||
3d1e4db2 AV |
523 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
524 | { | |
525 | struct device_driver *drv = phydev->dev.driver; | |
526 | 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 | ||
803dd9c7 FF |
532 | /* 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 | */ | |
3d1e4db2 | 537 | if (!netdev) |
803dd9c7 | 538 | return !phydev->suspended; |
3d1e4db2 | 539 | |
02d320c3 | 540 | /* Don't suspend PHY if the attched netdev parent may wakeup. |
3d1e4db2 AV |
541 | * 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 | ||
02d320c3 | 546 | /* Also don't suspend PHY if the netdev itself may wakeup. This |
3d1e4db2 AV |
547 | * 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 | ||
2f5cb434 | 556 | static int mdio_bus_suspend(struct device *dev) |
00db8189 | 557 | { |
0f0ca340 | 558 | struct phy_device *phydev = to_phy_device(dev); |
00db8189 | 559 | |
02d320c3 | 560 | /* We must stop the state machine manually, otherwise it stops out of |
541cd3ee AV |
561 | * 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 | */ | |
fddd9101 | 565 | if (phydev->attached_dev && phydev->adjust_link) |
541cd3ee AV |
566 | phy_stop_machine(phydev); |
567 | ||
3d1e4db2 AV |
568 | if (!mdio_bus_phy_may_suspend(phydev)) |
569 | return 0; | |
541cd3ee | 570 | |
9272efa2 | 571 | return phy_suspend(phydev); |
00db8189 AF |
572 | } |
573 | ||
2f5cb434 | 574 | static int mdio_bus_resume(struct device *dev) |
00db8189 | 575 | { |
0f0ca340 | 576 | struct phy_device *phydev = to_phy_device(dev); |
541cd3ee | 577 | int ret; |
00db8189 | 578 | |
3d1e4db2 | 579 | if (!mdio_bus_phy_may_suspend(phydev)) |
541cd3ee AV |
580 | goto no_resume; |
581 | ||
9272efa2 | 582 | ret = phy_resume(phydev); |
541cd3ee AV |
583 | if (ret < 0) |
584 | return ret; | |
585 | ||
586 | no_resume: | |
fddd9101 | 587 | if (phydev->attached_dev && phydev->adjust_link) |
29935aeb | 588 | phy_start_machine(phydev); |
541cd3ee AV |
589 | |
590 | return 0; | |
00db8189 AF |
591 | } |
592 | ||
2f5cb434 AV |
593 | static 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 | ||
29935aeb | 610 | phy_start_machine(phydev); |
2f5cb434 AV |
611 | |
612 | return 0; | |
613 | } | |
614 | ||
02d320c3 | 615 | static const struct dev_pm_ops mdio_bus_pm_ops = { |
2f5cb434 AV |
616 | .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 | ||
56277f40 NB |
631 | static ssize_t |
632 | phy_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 | } | |
4192c749 | 638 | static DEVICE_ATTR_RO(phy_id); |
56277f40 | 639 | |
3d055d8d FF |
640 | static ssize_t |
641 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) | |
642 | { | |
643 | struct phy_device *phydev = to_phy_device(dev); | |
574746dd | 644 | const char *mode = NULL; |
3d055d8d | 645 | |
574746dd FF |
646 | if (phy_is_internal(phydev)) |
647 | mode = "internal"; | |
648 | else | |
649 | mode = phy_modes(phydev->interface); | |
650 | ||
651 | return sprintf(buf, "%s\n", mode); | |
3d055d8d FF |
652 | } |
653 | static DEVICE_ATTR_RO(phy_interface); | |
654 | ||
8bed1285 FF |
655 | static ssize_t |
656 | phy_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 | } | |
662 | static DEVICE_ATTR_RO(phy_has_fixups); | |
663 | ||
4192c749 GKH |
664 | static struct attribute *mdio_dev_attrs[] = { |
665 | &dev_attr_phy_id.attr, | |
3d055d8d | 666 | &dev_attr_phy_interface.attr, |
8bed1285 | 667 | &dev_attr_phy_has_fixups.attr, |
4192c749 | 668 | NULL, |
56277f40 | 669 | }; |
4192c749 | 670 | ATTRIBUTE_GROUPS(mdio_dev); |
56277f40 | 671 | |
00db8189 AF |
672 | struct bus_type mdio_bus_type = { |
673 | .name = "mdio_bus", | |
674 | .match = mdio_bus_match, | |
2f5cb434 | 675 | .pm = MDIO_BUS_PM_OPS, |
4192c749 | 676 | .dev_groups = mdio_dev_groups, |
00db8189 | 677 | }; |
11b0bacd | 678 | EXPORT_SYMBOL(mdio_bus_type); |
00db8189 | 679 | |
67c4f3fa | 680 | int __init mdio_bus_init(void) |
00db8189 | 681 | { |
46abc021 LB |
682 | 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; | |
00db8189 AF |
692 | } |
693 | ||
dc85dec6 | 694 | void mdio_bus_exit(void) |
e1393456 | 695 | { |
46abc021 | 696 | class_unregister(&mdio_bus_class); |
e1393456 AF |
697 | bus_unregister(&mdio_bus_type); |
698 | } |