mdio_bus.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_alloc - allocate a mii_bus structure
  38. *
  39. * Description: called by a bus driver to allocate an mii_bus
  40. * structure to fill in.
  41. */
  42. struct mii_bus *mdiobus_alloc(void)
  43. {
  44. return kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  45. }
  46. EXPORT_SYMBOL(mdiobus_alloc);
  47. /**
  48. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  49. * @bus: target mii_bus
  50. *
  51. * Description: Called by a bus driver to bring up all the PHYs
  52. * on a given bus, and attach them to the bus.
  53. *
  54. * Returns 0 on success or < 0 on error.
  55. */
  56. int mdiobus_register(struct mii_bus *bus)
  57. {
  58. int i;
  59. int err = 0;
  60. if (NULL == bus || NULL == bus->name ||
  61. NULL == bus->read ||
  62. NULL == bus->write)
  63. return -EINVAL;
  64. mutex_init(&bus->mdio_lock);
  65. if (bus->reset)
  66. bus->reset(bus);
  67. for (i = 0; i < PHY_MAX_ADDR; i++) {
  68. bus->phy_map[i] = NULL;
  69. if ((bus->phy_mask & (1 << i)) == 0) {
  70. struct phy_device *phydev;
  71. phydev = mdiobus_scan(bus, i);
  72. if (IS_ERR(phydev))
  73. err = PTR_ERR(phydev);
  74. }
  75. }
  76. pr_info("%s: probed\n", bus->name);
  77. return err;
  78. }
  79. EXPORT_SYMBOL(mdiobus_register);
  80. void mdiobus_unregister(struct mii_bus *bus)
  81. {
  82. int i;
  83. for (i = 0; i < PHY_MAX_ADDR; i++) {
  84. if (bus->phy_map[i])
  85. device_unregister(&bus->phy_map[i]->dev);
  86. }
  87. }
  88. EXPORT_SYMBOL(mdiobus_unregister);
  89. /**
  90. * mdiobus_free - free a struct mii_bus
  91. * @bus: mii_bus to free
  92. *
  93. * This function frees the mii_bus.
  94. */
  95. void mdiobus_free(struct mii_bus *bus)
  96. {
  97. kfree(bus);
  98. }
  99. EXPORT_SYMBOL(mdiobus_free);
  100. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  101. {
  102. struct phy_device *phydev;
  103. int err;
  104. phydev = get_phy_device(bus, addr);
  105. if (IS_ERR(phydev) || phydev == NULL)
  106. return phydev;
  107. /* There's a PHY at this address
  108. * We need to set:
  109. * 1) IRQ
  110. * 2) bus_id
  111. * 3) parent
  112. * 4) bus
  113. * 5) mii_bus
  114. * And, we need to register it */
  115. phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  116. phydev->dev.parent = bus->parent;
  117. phydev->dev.bus = &mdio_bus_type;
  118. snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
  119. phydev->bus = bus;
  120. /* Run all of the fixups for this PHY */
  121. phy_scan_fixups(phydev);
  122. err = device_register(&phydev->dev);
  123. if (err) {
  124. printk(KERN_ERR "phy %d failed to register\n", addr);
  125. phy_device_free(phydev);
  126. phydev = NULL;
  127. }
  128. bus->phy_map[addr] = phydev;
  129. return phydev;
  130. }
  131. EXPORT_SYMBOL(mdiobus_scan);
  132. /**
  133. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  134. * @dev: target PHY device
  135. * @drv: given PHY driver
  136. *
  137. * Description: Given a PHY device, and a PHY driver, return 1 if
  138. * the driver supports the device. Otherwise, return 0.
  139. */
  140. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  141. {
  142. struct phy_device *phydev = to_phy_device(dev);
  143. struct phy_driver *phydrv = to_phy_driver(drv);
  144. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  145. (phydev->phy_id & phydrv->phy_id_mask));
  146. }
  147. /* Suspend and resume. Copied from platform_suspend and
  148. * platform_resume
  149. */
  150. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  151. {
  152. int ret = 0;
  153. struct device_driver *drv = dev->driver;
  154. if (drv && drv->suspend)
  155. ret = drv->suspend(dev, state);
  156. return ret;
  157. }
  158. static int mdio_bus_resume(struct device * dev)
  159. {
  160. int ret = 0;
  161. struct device_driver *drv = dev->driver;
  162. if (drv && drv->resume)
  163. ret = drv->resume(dev);
  164. return ret;
  165. }
  166. struct bus_type mdio_bus_type = {
  167. .name = "mdio_bus",
  168. .match = mdio_bus_match,
  169. .suspend = mdio_bus_suspend,
  170. .resume = mdio_bus_resume,
  171. };
  172. EXPORT_SYMBOL(mdio_bus_type);
  173. int __init mdio_bus_init(void)
  174. {
  175. return bus_register(&mdio_bus_type);
  176. }
  177. void mdio_bus_exit(void)
  178. {
  179. bus_unregister(&mdio_bus_type);
  180. }