mdio_bus.c 3.7 KB

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