mdio_bus.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. spin_lock_init(&bus->mdio_lock);
  50. if (NULL == bus || NULL == bus->name ||
  51. NULL == bus->read ||
  52. NULL == bus->write)
  53. return -EINVAL;
  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. err = device_register(&phydev->dev);
  80. if (err)
  81. printk(KERN_ERR "phy %d failed to register\n",
  82. i);
  83. }
  84. bus->phy_map[i] = phydev;
  85. }
  86. pr_info("%s: probed\n", bus->name);
  87. return err;
  88. }
  89. EXPORT_SYMBOL(mdiobus_register);
  90. void mdiobus_unregister(struct mii_bus *bus)
  91. {
  92. int i;
  93. for (i = 0; i < PHY_MAX_ADDR; i++) {
  94. if (bus->phy_map[i]) {
  95. device_unregister(&bus->phy_map[i]->dev);
  96. kfree(bus->phy_map[i]);
  97. }
  98. }
  99. }
  100. EXPORT_SYMBOL(mdiobus_unregister);
  101. /**
  102. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  103. * @dev: target PHY device
  104. * @drv: given PHY driver
  105. *
  106. * Description: Given a PHY device, and a PHY driver, return 1 if
  107. * the driver supports the device. Otherwise, return 0.
  108. */
  109. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  110. {
  111. struct phy_device *phydev = to_phy_device(dev);
  112. struct phy_driver *phydrv = to_phy_driver(drv);
  113. return ((phydrv->phy_id & phydrv->phy_id_mask) ==
  114. (phydev->phy_id & phydrv->phy_id_mask));
  115. }
  116. /* Suspend and resume. Copied from platform_suspend and
  117. * platform_resume
  118. */
  119. static int mdio_bus_suspend(struct device * dev, pm_message_t state)
  120. {
  121. int ret = 0;
  122. struct device_driver *drv = dev->driver;
  123. if (drv && drv->suspend)
  124. ret = drv->suspend(dev, state);
  125. return ret;
  126. }
  127. static int mdio_bus_resume(struct device * dev)
  128. {
  129. int ret = 0;
  130. struct device_driver *drv = dev->driver;
  131. if (drv && drv->resume)
  132. ret = drv->resume(dev);
  133. return ret;
  134. }
  135. struct bus_type mdio_bus_type = {
  136. .name = "mdio_bus",
  137. .match = mdio_bus_match,
  138. .suspend = mdio_bus_suspend,
  139. .resume = mdio_bus_resume,
  140. };
  141. EXPORT_SYMBOL(mdio_bus_type);
  142. int __init mdio_bus_init(void)
  143. {
  144. return bus_register(&mdio_bus_type);
  145. }
  146. void mdio_bus_exit(void)
  147. {
  148. bus_unregister(&mdio_bus_type);
  149. }