PageRenderTime 1071ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/of/of_mdio.c

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