mdio_bus.c 3.5 KB

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