of_mdio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. mdio->dev.of_node = np;
  43. /* Register the MDIO bus */
  44. rc = mdiobus_register(mdio);
  45. if (rc)
  46. return rc;
  47. /* Loop over the child nodes and register a phy_device for each one */
  48. for_each_available_child_of_node(np, child) {
  49. const __be32 *paddr;
  50. u32 addr;
  51. int len;
  52. bool is_c45;
  53. /* A PHY must have a reg property in the range [0-31] */
  54. paddr = of_get_property(child, "reg", &len);
  55. if (!paddr || len < sizeof(*paddr)) {
  56. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  57. child->full_name);
  58. continue;
  59. }
  60. addr = be32_to_cpup(paddr);
  61. if (addr >= 32) {
  62. dev_err(&mdio->dev, "%s PHY address %i is too large\n",
  63. child->full_name, addr);
  64. continue;
  65. }
  66. if (mdio->irq) {
  67. mdio->irq[addr] = irq_of_parse_and_map(child, 0);
  68. if (!mdio->irq[addr])
  69. mdio->irq[addr] = PHY_POLL;
  70. }
  71. is_c45 = of_device_is_compatible(child,
  72. "ethernet-phy-ieee802.3-c45");
  73. phy = get_phy_device(mdio, addr, is_c45);
  74. if (!phy || IS_ERR(phy)) {
  75. phy = phy_device_create(mdio, addr, 0, false, NULL);
  76. if (!phy || IS_ERR(phy)) {
  77. dev_err(&mdio->dev,
  78. "error creating PHY at address %i\n",
  79. addr);
  80. continue;
  81. }
  82. }
  83. /* Associate the OF node with the device structure so it
  84. * can be looked up later */
  85. of_node_get(child);
  86. phy->dev.of_node = child;
  87. /* All data is now stored in the phy struct; register it */
  88. rc = phy_device_register(phy);
  89. if (rc) {
  90. phy_device_free(phy);
  91. of_node_put(child);
  92. continue;
  93. }
  94. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  95. child->name, addr);
  96. }
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(of_mdiobus_register);
  100. /* Helper function for of_phy_find_device */
  101. static int of_phy_match(struct device *dev, void *phy_np)
  102. {
  103. return dev->of_node == phy_np;
  104. }
  105. /**
  106. * of_phy_find_device - Give a PHY node, find the phy_device
  107. * @phy_np: Pointer to the phy's device tree node
  108. *
  109. * Returns a pointer to the phy_device.
  110. */
  111. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  112. {
  113. struct device *d;
  114. if (!phy_np)
  115. return NULL;
  116. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  117. return d ? to_phy_device(d) : NULL;
  118. }
  119. EXPORT_SYMBOL(of_phy_find_device);
  120. /**
  121. * of_phy_connect - Connect to the phy described in the device tree
  122. * @dev: pointer to net_device claiming the phy
  123. * @phy_np: Pointer to device tree node for the PHY
  124. * @hndlr: Link state callback for the network device
  125. * @iface: PHY data interface type
  126. *
  127. * Returns a pointer to the phy_device if successful. NULL otherwise
  128. */
  129. struct phy_device *of_phy_connect(struct net_device *dev,
  130. struct device_node *phy_np,
  131. void (*hndlr)(struct net_device *), u32 flags,
  132. phy_interface_t iface)
  133. {
  134. struct phy_device *phy = of_phy_find_device(phy_np);
  135. if (!phy)
  136. return NULL;
  137. return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
  138. }
  139. EXPORT_SYMBOL(of_phy_connect);
  140. /**
  141. * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
  142. * @dev: pointer to net_device claiming the phy
  143. * @hndlr: Link state callback for the network device
  144. * @iface: PHY data interface type
  145. *
  146. * This function is a temporary stop-gap and will be removed soon. It is
  147. * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
  148. * not call this function from new drivers.
  149. */
  150. struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
  151. void (*hndlr)(struct net_device *),
  152. phy_interface_t iface)
  153. {
  154. struct device_node *net_np;
  155. char bus_id[MII_BUS_ID_SIZE + 3];
  156. struct phy_device *phy;
  157. const __be32 *phy_id;
  158. int sz;
  159. if (!dev->dev.parent)
  160. return NULL;
  161. net_np = dev->dev.parent->of_node;
  162. if (!net_np)
  163. return NULL;
  164. phy_id = of_get_property(net_np, "fixed-link", &sz);
  165. if (!phy_id || sz < sizeof(*phy_id))
  166. return NULL;
  167. sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
  168. phy = phy_connect(dev, bus_id, hndlr, iface);
  169. return IS_ERR(phy) ? NULL : phy;
  170. }
  171. EXPORT_SYMBOL(of_phy_connect_fixed_link);