of_mdio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * OF helpers for the MDIO (Ethernet PHY) API
  3. *
  4. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * This file provides helper functions for extracting PHY device information
  9. * out of the OpenFirmware device tree and using it to populate an mii_bus.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/err.h>
  15. #include <linux/phy.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/module.h>
  20. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  21. MODULE_LICENSE("GPL");
  22. /**
  23. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  24. * @mdio: pointer to mii_bus structure
  25. * @np: pointer to device_node of MDIO bus.
  26. *
  27. * This function registers the mii_bus structure and registers a phy_device
  28. * for each child node of @np.
  29. */
  30. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  31. {
  32. struct phy_device *phy;
  33. struct device_node *child;
  34. int rc, i;
  35. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  36. * the device tree are populated after the bus has been registered */
  37. mdio->phy_mask = ~0;
  38. /* Clear all the IRQ properties */
  39. if (mdio->irq)
  40. for (i=0; i<PHY_MAX_ADDR; i++)
  41. mdio->irq[i] = PHY_POLL;
  42. /* Register the MDIO bus */
  43. rc = mdiobus_register(mdio);
  44. if (rc)
  45. return rc;
  46. /* Loop over the child nodes and register a phy_device for each one */
  47. for_each_child_of_node(np, child) {
  48. const __be32 *addr;
  49. int len;
  50. /* A PHY must have a reg property in the range [0-31] */
  51. addr = of_get_property(child, "reg", &len);
  52. if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
  53. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  54. child->full_name);
  55. continue;
  56. }
  57. if (mdio->irq) {
  58. mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
  59. if (!mdio->irq[*addr])
  60. mdio->irq[*addr] = PHY_POLL;
  61. }
  62. phy = get_phy_device(mdio, be32_to_cpup(addr));
  63. if (!phy || IS_ERR(phy)) {
  64. dev_err(&mdio->dev, "error probing PHY at address %i\n",
  65. *addr);
  66. continue;
  67. }
  68. phy_scan_fixups(phy);
  69. /* Associate the OF node with the device structure so it
  70. * can be looked up later */
  71. of_node_get(child);
  72. phy->dev.of_node = child;
  73. /* All data is now stored in the phy struct; register it */
  74. rc = phy_device_register(phy);
  75. if (rc) {
  76. phy_device_free(phy);
  77. of_node_put(child);
  78. continue;
  79. }
  80. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  81. child->name, *addr);
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(of_mdiobus_register);
  86. /* Helper function for of_phy_find_device */
  87. static int of_phy_match(struct device *dev, void *phy_np)
  88. {
  89. return dev->of_node == phy_np;
  90. }
  91. /**
  92. * of_phy_find_device - Give a PHY node, find the phy_device
  93. * @phy_np: Pointer to the phy's device tree node
  94. *
  95. * Returns a pointer to the phy_device.
  96. */
  97. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  98. {
  99. struct device *d;
  100. if (!phy_np)
  101. return NULL;
  102. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  103. return d ? to_phy_device(d) : NULL;
  104. }
  105. EXPORT_SYMBOL(of_phy_find_device);
  106. /**
  107. * of_phy_connect - Connect to the phy described in the device tree
  108. * @dev: pointer to net_device claiming the phy
  109. * @phy_np: Pointer to device tree node for the PHY
  110. * @hndlr: Link state callback for the network device
  111. * @iface: PHY data interface type
  112. *
  113. * Returns a pointer to the phy_device if successfull. NULL otherwise
  114. */
  115. struct phy_device *of_phy_connect(struct net_device *dev,
  116. struct device_node *phy_np,
  117. void (*hndlr)(struct net_device *), u32 flags,
  118. phy_interface_t iface)
  119. {
  120. struct phy_device *phy = of_phy_find_device(phy_np);
  121. if (!phy)
  122. return NULL;
  123. return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
  124. }
  125. EXPORT_SYMBOL(of_phy_connect);
  126. /**
  127. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  128. * @dev: pointer to net_device claiming the phy
  129. * @hndlr: Link state callback for the network device
  130. * @iface: PHY data interface type
  131. *
  132. * This function is a temporary stop-gap and will be removed soon. It is
  133. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  134. * not call this function from new drivers.
  135. */
  136. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  137. void (*hndlr)(struct net_device *),
  138. phy_interface_t iface)
  139. {
  140. struct device_node *net_np;
  141. char bus_id[MII_BUS_ID_SIZE + 3];
  142. struct phy_device *phy;
  143. const __be32 *phy_id;
  144. int sz;
  145. if (!dev->dev.parent)
  146. return NULL;
  147. net_np = dev->dev.parent->of_node;
  148. if (!net_np)
  149. return NULL;
  150. phy_id = of_get_property(net_np, "fixed-link", &sz);
  151. if (!phy_id || sz < sizeof(*phy_id))
  152. return NULL;
  153. sprintf(bus_id, PHY_ID_FMT, "0", be32_to_cpu(phy_id[0]));
  154. phy = phy_connect(dev, bus_id, hndlr, 0, iface);
  155. return IS_ERR(phy) ? NULL : phy;
  156. }
  157. EXPORT_SYMBOL(of_phy_connect_fixed_link);