mdio_bus.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <linux/kernel.h>
  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>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/phy.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.h>
  36. /**
  37. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  38. * @bus: target mii_bus
  39. *
  40. * Description: Called by a bus driver to bring up all the PHYs
  41. * on a given bus, and attach them to the bus.
  42. *
  43. * Returns 0 on success or < 0 on error.
  44. */
  45. int mdiobus_register(struct mii_bus *bus)
  46. {
  47. int i;
  48. int err = 0;
  49. if (NULL == bus || NULL == bus->name ||
  50. NULL == bus->read ||
  51. NULL == bus->write)
  52. return -EINVAL;
  53. mutex_init(&bus->mdio_lock);
  54. if (bus->reset)
  55. bus->reset(bus);
  56. for (i = 0; i < PHY_MAX_ADDR; i++) {
  57. struct phy_device *phydev;
  58. if (bus->phy_mask & (1 << i)) {
  59. bus->phy_map[i] = NULL;
  60. continue;
  61. }
  62. phydev = get_phy_device(bus, i);
  63. if (IS_ERR(phydev))
  64. return PTR_ERR(phydev);
  65. /* There's a PHY at this address
  66. * We need to set:
  67. * 1) IRQ
  68. * 2) bus_id
  69. * 3) parent
  70. * 4) bus
  71. * 5) mii_bus
  72. * And, we need to register it */
  73. if (phydev) {
  74. phydev->irq = bus->irq[i];
  75. phydev->dev.parent = bus->dev;
  76. phydev->dev.bus = &mdio_bus_type;
  77. snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
  78. phydev->bus = bus;
  79. /* Run all of the fixups for this PHY */
  80. phy_scan_fixups(phydev);
  81. err = device_register(&phydev->dev);
  82. if (err) {
  83. printk(KERN_ERR "phy %d failed to register\n",
  84. i);
  85. phy_device_free(phydev);
  86. phydev = NULL;
  87. }
  88. }
  89. bus->phy_map[i] = phydev;
  90. }
  91. pr_info("%s: probed\n", bus->name);
  92. return err;
  93. }
  94. EXPORT_SYMBOL(mdiobus_register);
  95. void mdiobus_unregister(struct mii_bus *bus)
  96. {
  97. int i;
  98. for (i = 0; i < PHY_MAX_ADDR; i++) {
  99. if (bus->phy_map[i])
  100. device_unregister(&bus->phy_map[i]->dev);
  101. }
  102. }
  103. EXPORT_SYMBOL(mdiobus_unregister);
  104. /**
  105. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  106. * @dev: target PHY device
  107. * @drv: given PHY driver
  108. *
  109. * Description: Given a PHY device, and a PHY driver, return 1 if
  110. * the driver supports the device. Otherwise, return 0.
  111. */
  112. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  113. {
  114. struct phy_device *phydev = to_phy_device(dev);
  115. struct phy_driver *phydrv = to_phy_driver(drv);
  116. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  117. (phydev->phy_id & phydrv->phy_id_mask));
  118. }
  119. /* Suspend and resume. Copied from platform_suspend and
  120. * platform_resume
  121. */
  122. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  123. {
  124. int ret = 0;
  125. struct device_driver *drv = dev->driver;
  126. if (drv && drv->suspend)
  127. ret = drv->suspend(dev, state);
  128. return ret;
  129. }
  130. static int mdio_bus_resume(struct device * dev)
  131. {
  132. int ret = 0;
  133. struct device_driver *drv = dev->driver;
  134. if (drv && drv->resume)
  135. ret = drv->resume(dev);
  136. return ret;
  137. }
  138. struct bus_type mdio_bus_type = {
  139. .name = "mdio_bus",
  140. .match = mdio_bus_match,
  141. .suspend = mdio_bus_suspend,
  142. .resume = mdio_bus_resume,
  143. };
  144. EXPORT_SYMBOL(mdio_bus_type);
  145. int __init mdio_bus_init(void)
  146. {
  147. return bus_register(&mdio_bus_type);
  148. }
  149. void mdio_bus_exit(void)
  150. {
  151. bus_unregister(&mdio_bus_type);
  152. }