of_mdio.c 4.9 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_mdio.h>
  18. #include <linux/module.h>
  19. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  20. MODULE_LICENSE("GPL");
  21. /**
  22. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  23. * @mdio: pointer to mii_bus structure
  24. * @np: pointer to device_node of MDIO bus.
  25. *
  26. * This function registers the mii_bus structure and registers a phy_device
  27. * for each child node of @np.
  28. */
  29. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  30. {
  31. struct phy_device *phy;
  32. struct device_node *child;
  33. int rc, i;
  34. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  35. * the device tree are populated after the bus has been registered */
  36. mdio->phy_mask = ~0;
  37. /* Clear all the IRQ properties */
  38. if (mdio->irq)
  39. for (i=0; i<PHY_MAX_ADDR; i++)
  40. mdio->irq[i] = PHY_POLL;
  41. /* Register the MDIO bus */
  42. rc = mdiobus_register(mdio);
  43. if (rc)
  44. return rc;
  45. /* Loop over the child nodes and register a phy_device for each one */
  46. for_each_child_of_node(np, child) {
  47. const __be32 *addr;
  48. int len;
  49. /* A PHY must have a reg property in the range [0-31] */
  50. addr = of_get_property(child, "reg", &len);
  51. if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
  52. dev_err(&mdio->dev, "%s has invalid PHY address\n",
  53. child->full_name);
  54. continue;
  55. }
  56. if (mdio->irq) {
  57. mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
  58. if (!mdio->irq[*addr])
  59. mdio->irq[*addr] = PHY_POLL;
  60. }
  61. phy = get_phy_device(mdio, be32_to_cpup(addr));
  62. if (!phy) {
  63. dev_err(&mdio->dev, "error probing PHY at address %i\n",
  64. *addr);
  65. continue;
  66. }
  67. phy_scan_fixups(phy);
  68. /* Associate the OF node with the device structure so it
  69. * can be looked up later */
  70. of_node_get(child);
  71. dev_archdata_set_node(&phy->dev.archdata, 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_archdata_get_node(&dev->archdata) == 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_archdata_get_node(&dev->dev.parent->archdata);
  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);