Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mdio_bus.c
Go to the documentation of this file.
1 /*
2  * drivers/net/phy/mdio_bus.c
3  *
4  * MDIO Bus interface
5  *
6  * Author: Andy Fleming
7  *
8  * Copyright (c) 2004 Freescale Semiconductor, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2 of the License, or (at your
13  * option) any later version.
14  *
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/device.h>
28 #include <linux/of_device.h>
29 #include <linux/of_mdio.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
38 #include <linux/phy.h>
39 
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/uaccess.h>
43 
53 {
54  struct mii_bus *bus;
55  size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
56  size_t alloc_size;
57 
58  /* If we alloc extra space, it should be aligned */
59  if (size)
60  alloc_size = aligned_size + size;
61  else
62  alloc_size = sizeof(*bus);
63 
64  bus = kzalloc(alloc_size, GFP_KERNEL);
65  if (bus) {
66  bus->state = MDIOBUS_ALLOCATED;
67  if (size)
68  bus->priv = (void *)bus + aligned_size;
69  }
70 
71  return bus;
72 }
74 
82 static void mdiobus_release(struct device *d)
83 {
84  struct mii_bus *bus = to_mii_bus(d);
85  BUG_ON(bus->state != MDIOBUS_RELEASED &&
86  /* for compatibility with error handling in drivers */
87  bus->state != MDIOBUS_ALLOCATED);
88  kfree(bus);
89 }
90 
91 static struct class mdio_bus_class = {
92  .name = "mdio_bus",
93  .dev_release = mdiobus_release,
94 };
95 
96 #if IS_ENABLED(CONFIG_OF_MDIO)
97 /* Helper function for of_mdio_find_bus */
98 static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np)
99 {
100  return dev->of_node == mdio_bus_np;
101 }
113 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
114 {
115  struct device *d;
116 
117  if (!mdio_bus_np)
118  return NULL;
119 
120  d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
121  of_mdio_bus_match);
122 
123  return d ? to_mii_bus(d) : NULL;
124 }
125 EXPORT_SYMBOL(of_mdio_find_bus);
126 #endif
127 
138 {
139  int i, err;
140 
141  if (NULL == bus || NULL == bus->name ||
142  NULL == bus->read ||
143  NULL == bus->write)
144  return -EINVAL;
145 
146  BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
147  bus->state != MDIOBUS_UNREGISTERED);
148 
149  bus->dev.parent = bus->parent;
150  bus->dev.class = &mdio_bus_class;
151  bus->dev.groups = NULL;
152  dev_set_name(&bus->dev, "%s", bus->id);
153 
154  err = device_register(&bus->dev);
155  if (err) {
156  pr_err("mii_bus %s failed to register\n", bus->id);
157  return -EINVAL;
158  }
159 
160  mutex_init(&bus->mdio_lock);
161 
162  if (bus->reset)
163  bus->reset(bus);
164 
165  for (i = 0; i < PHY_MAX_ADDR; i++) {
166  if ((bus->phy_mask & (1 << i)) == 0) {
167  struct phy_device *phydev;
168 
169  phydev = mdiobus_scan(bus, i);
170  if (IS_ERR(phydev)) {
171  err = PTR_ERR(phydev);
172  goto error;
173  }
174  }
175  }
176 
177  bus->state = MDIOBUS_REGISTERED;
178  pr_info("%s: probed\n", bus->name);
179  return 0;
180 
181 error:
182  while (--i >= 0) {
183  if (bus->phy_map[i])
184  device_unregister(&bus->phy_map[i]->dev);
185  }
186  device_del(&bus->dev);
187  return err;
188 }
190 
192 {
193  int i;
194 
195  BUG_ON(bus->state != MDIOBUS_REGISTERED);
196  bus->state = MDIOBUS_UNREGISTERED;
197 
198  device_del(&bus->dev);
199  for (i = 0; i < PHY_MAX_ADDR; i++) {
200  if (bus->phy_map[i])
201  device_unregister(&bus->phy_map[i]->dev);
202  bus->phy_map[i] = NULL;
203  }
204 }
206 
215 void mdiobus_free(struct mii_bus *bus)
216 {
217  /*
218  * For compatibility with error handling in drivers.
219  */
220  if (bus->state == MDIOBUS_ALLOCATED) {
221  kfree(bus);
222  return;
223  }
224 
225  BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
226  bus->state = MDIOBUS_RELEASED;
227 
228  put_device(&bus->dev);
229 }
231 
232 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
233 {
234  struct phy_device *phydev;
235  int err;
236 
237  phydev = get_phy_device(bus, addr, false);
238  if (IS_ERR(phydev) || phydev == NULL)
239  return phydev;
240 
241  err = phy_device_register(phydev);
242  if (err) {
243  phy_device_free(phydev);
244  return NULL;
245  }
246 
247  return phydev;
248 }
250 
261 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
262 {
263  int retval;
264 
265  BUG_ON(in_interrupt());
266 
267  mutex_lock(&bus->mdio_lock);
268  retval = bus->read(bus, addr, regnum);
269  mutex_unlock(&bus->mdio_lock);
270 
271  return retval;
272 }
274 
286 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
287 {
288  int err;
289 
290  BUG_ON(in_interrupt());
291 
292  mutex_lock(&bus->mdio_lock);
293  err = bus->write(bus, addr, regnum, val);
294  mutex_unlock(&bus->mdio_lock);
295 
296  return err;
297 }
299 
308 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
309 {
310  struct phy_device *phydev = to_phy_device(dev);
311  struct phy_driver *phydrv = to_phy_driver(drv);
312 
313  if (of_driver_match_device(dev, drv))
314  return 1;
315 
316  if (phydrv->match_phy_device)
317  return phydrv->match_phy_device(phydev);
318 
319  return ((phydrv->phy_id & phydrv->phy_id_mask) ==
320  (phydev->phy_id & phydrv->phy_id_mask));
321 }
322 
323 #ifdef CONFIG_PM
324 
325 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
326 {
327  struct device_driver *drv = phydev->dev.driver;
328  struct phy_driver *phydrv = to_phy_driver(drv);
329  struct net_device *netdev = phydev->attached_dev;
330 
331  if (!drv || !phydrv->suspend)
332  return false;
333 
334  /* PHY not attached? May suspend. */
335  if (!netdev)
336  return true;
337 
338  /*
339  * Don't suspend PHY if the attched netdev parent may wakeup.
340  * The parent may point to a PCI device, as in tg3 driver.
341  */
342  if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
343  return false;
344 
345  /*
346  * Also don't suspend PHY if the netdev itself may wakeup. This
347  * is the case for devices w/o underlaying pwr. mgmt. aware bus,
348  * e.g. SoC devices.
349  */
350  if (device_may_wakeup(&netdev->dev))
351  return false;
352 
353  return true;
354 }
355 
356 static int mdio_bus_suspend(struct device *dev)
357 {
358  struct phy_driver *phydrv = to_phy_driver(dev->driver);
359  struct phy_device *phydev = to_phy_device(dev);
360 
361  /*
362  * We must stop the state machine manually, otherwise it stops out of
363  * control, possibly with the phydev->lock held. Upon resume, netdev
364  * may call phy routines that try to grab the same lock, and that may
365  * lead to a deadlock.
366  */
367  if (phydev->attached_dev && phydev->adjust_link)
368  phy_stop_machine(phydev);
369 
370  if (!mdio_bus_phy_may_suspend(phydev))
371  return 0;
372 
373  return phydrv->suspend(phydev);
374 }
375 
376 static int mdio_bus_resume(struct device *dev)
377 {
378  struct phy_driver *phydrv = to_phy_driver(dev->driver);
379  struct phy_device *phydev = to_phy_device(dev);
380  int ret;
381 
382  if (!mdio_bus_phy_may_suspend(phydev))
383  goto no_resume;
384 
385  ret = phydrv->resume(phydev);
386  if (ret < 0)
387  return ret;
388 
389 no_resume:
390  if (phydev->attached_dev && phydev->adjust_link)
391  phy_start_machine(phydev, NULL);
392 
393  return 0;
394 }
395 
396 static int mdio_bus_restore(struct device *dev)
397 {
398  struct phy_device *phydev = to_phy_device(dev);
399  struct net_device *netdev = phydev->attached_dev;
400  int ret;
401 
402  if (!netdev)
403  return 0;
404 
405  ret = phy_init_hw(phydev);
406  if (ret < 0)
407  return ret;
408 
409  /* The PHY needs to renegotiate. */
410  phydev->link = 0;
411  phydev->state = PHY_UP;
412 
413  phy_start_machine(phydev, NULL);
414 
415  return 0;
416 }
417 
418 static struct dev_pm_ops mdio_bus_pm_ops = {
419  .suspend = mdio_bus_suspend,
420  .resume = mdio_bus_resume,
421  .freeze = mdio_bus_suspend,
422  .thaw = mdio_bus_resume,
423  .restore = mdio_bus_restore,
424 };
425 
426 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
427 
428 #else
429 
430 #define MDIO_BUS_PM_OPS NULL
431 
432 #endif /* CONFIG_PM */
433 
435  .name = "mdio_bus",
436  .match = mdio_bus_match,
437  .pm = MDIO_BUS_PM_OPS,
438 };
439 EXPORT_SYMBOL(mdio_bus_type);
440 
442 {
443  int ret;
444 
445  ret = class_register(&mdio_bus_class);
446  if (!ret) {
447  ret = bus_register(&mdio_bus_type);
448  if (ret)
449  class_unregister(&mdio_bus_class);
450  }
451 
452  return ret;
453 }
454 
455 void mdio_bus_exit(void)
456 {
457  class_unregister(&mdio_bus_class);
458  bus_unregister(&mdio_bus_type);
459 }