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