mdio_bus.c 3.5 KB

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