Merge tag 'linux-can-fixes-for-3.16-20140725' of git://gitorious.org/linux-can/linux-can
[deliverable/linux.git] / drivers / net / phy / mdio_bus.c
CommitLineData
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 49struct 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 70EXPORT_SYMBOL(mdiobus_alloc_size);
298cf9be 71
6d48f44b
GS
72static 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}
93dccc59 120EXPORT_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 */
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
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 */
146static 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
155static 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 162static 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
DD
169 *
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);
d9daa247
DM
190
191/* Walk the list of subnodes of a mdio bus and look for a node that matches the
192 * phy's address with its 'reg' property. If found, set the of_node pointer for
193 * the phy. This allows auto-probed pyh devices to be supplied with information
194 * passed in via DT.
195 */
196static void of_mdiobus_link_phydev(struct mii_bus *mdio,
197 struct phy_device *phydev)
198{
199 struct device *dev = &phydev->dev;
200 struct device_node *child;
201
202 if (dev->of_node || !mdio->dev.of_node)
203 return;
204
205 for_each_available_child_of_node(mdio->dev.of_node, child) {
206 int addr;
207 int ret;
208
209 ret = of_property_read_u32(child, "reg", &addr);
210 if (ret < 0) {
211 dev_err(dev, "%s has invalid PHY address\n",
212 child->full_name);
213 continue;
214 }
215
216 /* A PHY must have a reg property in the range [0-31] */
217 if (addr >= PHY_MAX_ADDR) {
218 dev_err(dev, "%s PHY address %i is too large\n",
219 child->full_name, addr);
220 continue;
221 }
222
223 if (addr == phydev->addr) {
224 dev->of_node = child;
225 return;
226 }
227 }
228}
229#else /* !IS_ENABLED(CONFIG_OF_MDIO) */
230static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
231 struct phy_device *phydev)
232{
233}
25106022
DD
234#endif
235
b3df0da8
RD
236/**
237 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
238 * @bus: target mii_bus
e1393456 239 *
b3df0da8
RD
240 * Description: Called by a bus driver to bring up all the PHYs
241 * on a given bus, and attach them to the bus.
242 *
243 * Returns 0 on success or < 0 on error.
e1393456
AF
244 */
245int mdiobus_register(struct mii_bus *bus)
246{
161c8d2f 247 int i, err;
e1393456 248
e1393456 249 if (NULL == bus || NULL == bus->name ||
02d320c3 250 NULL == bus->read || NULL == bus->write)
e1393456
AF
251 return -EINVAL;
252
46abc021
LB
253 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
254 bus->state != MDIOBUS_UNREGISTERED);
255
256 bus->dev.parent = bus->parent;
257 bus->dev.class = &mdio_bus_class;
a71e3c37 258 bus->dev.driver = bus->parent->driver;
46abc021 259 bus->dev.groups = NULL;
036b6687 260 dev_set_name(&bus->dev, "%s", bus->id);
46abc021
LB
261
262 err = device_register(&bus->dev);
263 if (err) {
8d242488 264 pr_err("mii_bus %s failed to register\n", bus->id);
0c692d07 265 put_device(&bus->dev);
46abc021
LB
266 return -EINVAL;
267 }
268
d1e7fe4d
AB
269 mutex_init(&bus->mdio_lock);
270
e1393456
AF
271 if (bus->reset)
272 bus->reset(bus);
273
274 for (i = 0; i < PHY_MAX_ADDR; i++) {
4fd5f812
LB
275 if ((bus->phy_mask & (1 << i)) == 0) {
276 struct phy_device *phydev;
e1393456 277
4fd5f812 278 phydev = mdiobus_scan(bus, i);
161c8d2f 279 if (IS_ERR(phydev)) {
4fd5f812 280 err = PTR_ERR(phydev);
161c8d2f
KH
281 goto error;
282 }
64b1c2b4 283 }
e1393456
AF
284 }
285
161c8d2f 286 bus->state = MDIOBUS_REGISTERED;
e1393456 287 pr_info("%s: probed\n", bus->name);
161c8d2f 288 return 0;
e1393456 289
161c8d2f
KH
290error:
291 while (--i >= 0) {
292 if (bus->phy_map[i])
293 device_unregister(&bus->phy_map[i]->dev);
294 }
295 device_del(&bus->dev);
e1393456
AF
296 return err;
297}
298EXPORT_SYMBOL(mdiobus_register);
299
300void mdiobus_unregister(struct mii_bus *bus)
301{
302 int i;
303
46abc021
LB
304 BUG_ON(bus->state != MDIOBUS_REGISTERED);
305 bus->state = MDIOBUS_UNREGISTERED;
306
3e44017b 307 device_del(&bus->dev);
e1393456 308 for (i = 0; i < PHY_MAX_ADDR; i++) {
6f4a7f41 309 if (bus->phy_map[i])
e1393456 310 device_unregister(&bus->phy_map[i]->dev);
4dea547f 311 bus->phy_map[i] = NULL;
e1393456
AF
312 }
313}
314EXPORT_SYMBOL(mdiobus_unregister);
315
298cf9be
LB
316/**
317 * mdiobus_free - free a struct mii_bus
318 * @bus: mii_bus to free
319 *
46abc021
LB
320 * This function releases the reference to the underlying device
321 * object in the mii_bus. If this is the last reference, the mii_bus
322 * will be freed.
298cf9be
LB
323 */
324void mdiobus_free(struct mii_bus *bus)
325{
02d320c3 326 /* For compatibility with error handling in drivers. */
46abc021
LB
327 if (bus->state == MDIOBUS_ALLOCATED) {
328 kfree(bus);
329 return;
330 }
331
332 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
333 bus->state = MDIOBUS_RELEASED;
334
335 put_device(&bus->dev);
298cf9be
LB
336}
337EXPORT_SYMBOL(mdiobus_free);
338
4fd5f812
LB
339struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
340{
341 struct phy_device *phydev;
342 int err;
343
ac28b9f8 344 phydev = get_phy_device(bus, addr, false);
4fd5f812
LB
345 if (IS_ERR(phydev) || phydev == NULL)
346 return phydev;
347
86f6cf41
DM
348 /*
349 * For DT, see if the auto-probed phy has a correspoding child
350 * in the bus node, and set the of_node pointer in this case.
351 */
352 of_mdiobus_link_phydev(bus, phydev);
353
4dea547f 354 err = phy_device_register(phydev);
4fd5f812 355 if (err) {
4fd5f812 356 phy_device_free(phydev);
4dea547f 357 return NULL;
4fd5f812
LB
358 }
359
4fd5f812
LB
360 return phydev;
361}
362EXPORT_SYMBOL(mdiobus_scan);
363
2e888103
LB
364/**
365 * mdiobus_read - Convenience function for reading a given MII mgmt register
366 * @bus: the mii_bus struct
367 * @addr: the phy address
368 * @regnum: register number to read
369 *
370 * NOTE: MUST NOT be called from interrupt context,
371 * because the bus read/write functions may wait for an interrupt
372 * to conclude the operation.
373 */
abf35df2 374int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
2e888103
LB
375{
376 int retval;
377
378 BUG_ON(in_interrupt());
379
380 mutex_lock(&bus->mdio_lock);
381 retval = bus->read(bus, addr, regnum);
382 mutex_unlock(&bus->mdio_lock);
383
384 return retval;
385}
386EXPORT_SYMBOL(mdiobus_read);
387
388/**
389 * mdiobus_write - Convenience function for writing a given MII mgmt register
390 * @bus: the mii_bus struct
391 * @addr: the phy address
392 * @regnum: register number to write
393 * @val: value to write to @regnum
394 *
395 * NOTE: MUST NOT be called from interrupt context,
396 * because the bus read/write functions may wait for an interrupt
397 * to conclude the operation.
398 */
abf35df2 399int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
2e888103
LB
400{
401 int err;
402
403 BUG_ON(in_interrupt());
404
405 mutex_lock(&bus->mdio_lock);
406 err = bus->write(bus, addr, regnum, val);
407 mutex_unlock(&bus->mdio_lock);
408
409 return err;
410}
411EXPORT_SYMBOL(mdiobus_write);
412
b3df0da8
RD
413/**
414 * mdio_bus_match - determine if given PHY driver supports the given PHY device
415 * @dev: target PHY device
416 * @drv: given PHY driver
00db8189 417 *
b3df0da8
RD
418 * Description: Given a PHY device, and a PHY driver, return 1 if
419 * the driver supports the device. Otherwise, return 0.
00db8189
AF
420 */
421static int mdio_bus_match(struct device *dev, struct device_driver *drv)
422{
423 struct phy_device *phydev = to_phy_device(dev);
424 struct phy_driver *phydrv = to_phy_driver(drv);
425
a30e2c18
DD
426 if (of_driver_match_device(dev, drv))
427 return 1;
428
429 if (phydrv->match_phy_device)
430 return phydrv->match_phy_device(phydev);
431
6f901b73
FF
432 return (phydrv->phy_id & phydrv->phy_id_mask) ==
433 (phydev->phy_id & phydrv->phy_id_mask);
00db8189
AF
434}
435
2f5cb434
AV
436#ifdef CONFIG_PM
437
3d1e4db2
AV
438static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
439{
440 struct device_driver *drv = phydev->dev.driver;
441 struct phy_driver *phydrv = to_phy_driver(drv);
442 struct net_device *netdev = phydev->attached_dev;
443
444 if (!drv || !phydrv->suspend)
445 return false;
446
447 /* PHY not attached? May suspend. */
448 if (!netdev)
449 return true;
450
02d320c3 451 /* Don't suspend PHY if the attched netdev parent may wakeup.
3d1e4db2
AV
452 * The parent may point to a PCI device, as in tg3 driver.
453 */
454 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
455 return false;
456
02d320c3 457 /* Also don't suspend PHY if the netdev itself may wakeup. This
3d1e4db2
AV
458 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
459 * e.g. SoC devices.
460 */
461 if (device_may_wakeup(&netdev->dev))
462 return false;
463
464 return true;
465}
466
2f5cb434 467static int mdio_bus_suspend(struct device *dev)
00db8189 468{
3d1e4db2 469 struct phy_driver *phydrv = to_phy_driver(dev->driver);
0f0ca340 470 struct phy_device *phydev = to_phy_device(dev);
00db8189 471
02d320c3 472 /* We must stop the state machine manually, otherwise it stops out of
541cd3ee
AV
473 * control, possibly with the phydev->lock held. Upon resume, netdev
474 * may call phy routines that try to grab the same lock, and that may
475 * lead to a deadlock.
476 */
fddd9101 477 if (phydev->attached_dev && phydev->adjust_link)
541cd3ee
AV
478 phy_stop_machine(phydev);
479
3d1e4db2
AV
480 if (!mdio_bus_phy_may_suspend(phydev))
481 return 0;
541cd3ee 482
3d1e4db2 483 return phydrv->suspend(phydev);
00db8189
AF
484}
485
2f5cb434 486static int mdio_bus_resume(struct device *dev)
00db8189 487{
3d1e4db2 488 struct phy_driver *phydrv = to_phy_driver(dev->driver);
0f0ca340 489 struct phy_device *phydev = to_phy_device(dev);
541cd3ee 490 int ret;
00db8189 491
3d1e4db2 492 if (!mdio_bus_phy_may_suspend(phydev))
541cd3ee
AV
493 goto no_resume;
494
495 ret = phydrv->resume(phydev);
496 if (ret < 0)
497 return ret;
498
499no_resume:
fddd9101 500 if (phydev->attached_dev && phydev->adjust_link)
29935aeb 501 phy_start_machine(phydev);
541cd3ee
AV
502
503 return 0;
00db8189
AF
504}
505
2f5cb434
AV
506static int mdio_bus_restore(struct device *dev)
507{
508 struct phy_device *phydev = to_phy_device(dev);
509 struct net_device *netdev = phydev->attached_dev;
510 int ret;
511
512 if (!netdev)
513 return 0;
514
515 ret = phy_init_hw(phydev);
516 if (ret < 0)
517 return ret;
518
519 /* The PHY needs to renegotiate. */
520 phydev->link = 0;
521 phydev->state = PHY_UP;
522
29935aeb 523 phy_start_machine(phydev);
2f5cb434
AV
524
525 return 0;
526}
527
02d320c3 528static const struct dev_pm_ops mdio_bus_pm_ops = {
2f5cb434
AV
529 .suspend = mdio_bus_suspend,
530 .resume = mdio_bus_resume,
531 .freeze = mdio_bus_suspend,
532 .thaw = mdio_bus_resume,
533 .restore = mdio_bus_restore,
534};
535
536#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
537
538#else
539
540#define MDIO_BUS_PM_OPS NULL
541
542#endif /* CONFIG_PM */
543
56277f40
NB
544static ssize_t
545phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
546{
547 struct phy_device *phydev = to_phy_device(dev);
548
549 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
550}
4192c749 551static DEVICE_ATTR_RO(phy_id);
56277f40 552
3d055d8d
FF
553static ssize_t
554phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
555{
556 struct phy_device *phydev = to_phy_device(dev);
557
558 return sprintf(buf, "%s\n", phy_modes(phydev->interface));
559}
560static DEVICE_ATTR_RO(phy_interface);
561
8bed1285
FF
562static ssize_t
563phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
564{
565 struct phy_device *phydev = to_phy_device(dev);
566
567 return sprintf(buf, "%d\n", phydev->has_fixups);
568}
569static DEVICE_ATTR_RO(phy_has_fixups);
570
4192c749
GKH
571static struct attribute *mdio_dev_attrs[] = {
572 &dev_attr_phy_id.attr,
3d055d8d 573 &dev_attr_phy_interface.attr,
8bed1285 574 &dev_attr_phy_has_fixups.attr,
4192c749 575 NULL,
56277f40 576};
4192c749 577ATTRIBUTE_GROUPS(mdio_dev);
56277f40 578
00db8189
AF
579struct bus_type mdio_bus_type = {
580 .name = "mdio_bus",
581 .match = mdio_bus_match,
2f5cb434 582 .pm = MDIO_BUS_PM_OPS,
4192c749 583 .dev_groups = mdio_dev_groups,
00db8189 584};
11b0bacd 585EXPORT_SYMBOL(mdio_bus_type);
00db8189 586
67c4f3fa 587int __init mdio_bus_init(void)
00db8189 588{
46abc021
LB
589 int ret;
590
591 ret = class_register(&mdio_bus_class);
592 if (!ret) {
593 ret = bus_register(&mdio_bus_type);
594 if (ret)
595 class_unregister(&mdio_bus_class);
596 }
597
598 return ret;
00db8189
AF
599}
600
dc85dec6 601void mdio_bus_exit(void)
e1393456 602{
46abc021 603 class_unregister(&mdio_bus_class);
e1393456
AF
604 bus_unregister(&mdio_bus_type);
605}
This page took 1.07937 seconds and 5 git commands to generate.