001-4.12-07-mdio_bus-Issue-GPIO-RESET-to-PHYs.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. From 69226896ad636b94f6d2e55d75ff21a29c4de83b Mon Sep 17 00:00:00 2001
  2. From: Roger Quadros <[email protected]>
  3. Date: Fri, 21 Apr 2017 16:15:38 +0300
  4. Subject: [PATCH] mdio_bus: Issue GPIO RESET to PHYs.
  5. Some boards [1] leave the PHYs at an invalid state
  6. during system power-up or reset thus causing unreliability
  7. issues with the PHY which manifests as PHY not being detected
  8. or link not functional. To fix this, these PHYs need to be RESET
  9. via a GPIO connected to the PHY's RESET pin.
  10. Some boards have a single GPIO controlling the PHY RESET pin of all
  11. PHYs on the bus whereas some others have separate GPIOs controlling
  12. individual PHY RESETs.
  13. In both cases, the RESET de-assertion cannot be done in the PHY driver
  14. as the PHY will not probe till its reset is de-asserted.
  15. So do the RESET de-assertion in the MDIO bus driver.
  16. [1] - am572x-idk, am571x-idk, a437x-idk
  17. Signed-off-by: Roger Quadros <[email protected]>
  18. Signed-off-by: David S. Miller <[email protected]>
  19. ---
  20. Documentation/devicetree/bindings/net/mdio.txt | 33 ++++++++++++++++++
  21. drivers/net/phy/mdio_bus.c | 47 ++++++++++++++++++++++++++
  22. drivers/of/of_mdio.c | 7 ++++
  23. include/linux/phy.h | 7 ++++
  24. 4 files changed, 94 insertions(+)
  25. create mode 100644 Documentation/devicetree/bindings/net/mdio.txt
  26. --- /dev/null
  27. +++ b/Documentation/devicetree/bindings/net/mdio.txt
  28. @@ -0,0 +1,33 @@
  29. +Common MDIO bus properties.
  30. +
  31. +These are generic properties that can apply to any MDIO bus.
  32. +
  33. +Optional properties:
  34. +- reset-gpios: List of one or more GPIOs that control the RESET lines
  35. + of the PHYs on that MDIO bus.
  36. +- reset-delay-us: RESET pulse width in microseconds as per PHY datasheet.
  37. +
  38. +A list of child nodes, one per device on the bus is expected. These
  39. +should follow the generic phy.txt, or a device specific binding document.
  40. +
  41. +Example :
  42. +This example shows these optional properties, plus other properties
  43. +required for the TI Davinci MDIO driver.
  44. +
  45. + davinci_mdio: ethernet@0x5c030000 {
  46. + compatible = "ti,davinci_mdio";
  47. + reg = <0x5c030000 0x1000>;
  48. + #address-cells = <1>;
  49. + #size-cells = <0>;
  50. +
  51. + reset-gpios = <&gpio2 5 GPIO_ACTIVE_LOW>;
  52. + reset-delay-us = <2>; /* PHY datasheet states 1us min */
  53. +
  54. + ethphy0: ethernet-phy@1 {
  55. + reg = <1>;
  56. + };
  57. +
  58. + ethphy1: ethernet-phy@3 {
  59. + reg = <3>;
  60. + };
  61. + };
  62. --- a/drivers/net/phy/mdio_bus.c
  63. +++ b/drivers/net/phy/mdio_bus.c
  64. @@ -22,8 +22,11 @@
  65. #include <linux/init.h>
  66. #include <linux/delay.h>
  67. #include <linux/device.h>
  68. +#include <linux/gpio.h>
  69. +#include <linux/gpio/consumer.h>
  70. #include <linux/of_device.h>
  71. #include <linux/of_mdio.h>
  72. +#include <linux/of_gpio.h>
  73. #include <linux/netdevice.h>
  74. #include <linux/etherdevice.h>
  75. #include <linux/skbuff.h>
  76. @@ -304,6 +307,7 @@ int __mdiobus_register(struct mii_bus *b
  77. {
  78. struct mdio_device *mdiodev;
  79. int i, err;
  80. + struct gpio_desc *gpiod;
  81. if (NULL == bus || NULL == bus->name ||
  82. NULL == bus->read || NULL == bus->write)
  83. @@ -330,6 +334,35 @@ int __mdiobus_register(struct mii_bus *b
  84. if (bus->reset)
  85. bus->reset(bus);
  86. + /* de-assert bus level PHY GPIO resets */
  87. + if (bus->num_reset_gpios > 0) {
  88. + bus->reset_gpiod = devm_kcalloc(&bus->dev,
  89. + bus->num_reset_gpios,
  90. + sizeof(struct gpio_desc *),
  91. + GFP_KERNEL);
  92. + if (!bus->reset_gpiod)
  93. + return -ENOMEM;
  94. + }
  95. +
  96. + for (i = 0; i < bus->num_reset_gpios; i++) {
  97. + gpiod = devm_gpiod_get_index(&bus->dev, "reset", i,
  98. + GPIOD_OUT_LOW);
  99. + if (IS_ERR(gpiod)) {
  100. + err = PTR_ERR(gpiod);
  101. + if (err != -ENOENT) {
  102. + dev_err(&bus->dev,
  103. + "mii_bus %s couldn't get reset GPIO\n",
  104. + bus->id);
  105. + return err;
  106. + }
  107. + } else {
  108. + bus->reset_gpiod[i] = gpiod;
  109. + gpiod_set_value_cansleep(gpiod, 1);
  110. + udelay(bus->reset_delay_us);
  111. + gpiod_set_value_cansleep(gpiod, 0);
  112. + }
  113. + }
  114. +
  115. for (i = 0; i < PHY_MAX_ADDR; i++) {
  116. if ((bus->phy_mask & (1 << i)) == 0) {
  117. struct phy_device *phydev;
  118. @@ -355,6 +388,13 @@ error:
  119. mdiodev->device_remove(mdiodev);
  120. mdiodev->device_free(mdiodev);
  121. }
  122. +
  123. + /* Put PHYs in RESET to save power */
  124. + for (i = 0; i < bus->num_reset_gpios; i++) {
  125. + if (bus->reset_gpiod[i])
  126. + gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  127. + }
  128. +
  129. device_del(&bus->dev);
  130. return err;
  131. }
  132. @@ -376,6 +416,13 @@ void mdiobus_unregister(struct mii_bus *
  133. mdiodev->device_remove(mdiodev);
  134. mdiodev->device_free(mdiodev);
  135. }
  136. +
  137. + /* Put PHYs in RESET to save power */
  138. + for (i = 0; i < bus->num_reset_gpios; i++) {
  139. + if (bus->reset_gpiod[i])
  140. + gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  141. + }
  142. +
  143. device_del(&bus->dev);
  144. }
  145. EXPORT_SYMBOL(mdiobus_unregister);
  146. --- a/drivers/of/of_mdio.c
  147. +++ b/drivers/of/of_mdio.c
  148. @@ -22,6 +22,8 @@
  149. #include <linux/of_net.h>
  150. #include <linux/module.h>
  151. +#define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
  152. +
  153. MODULE_AUTHOR("Grant Likely <[email protected]>");
  154. MODULE_LICENSE("GPL");
  155. @@ -220,6 +222,11 @@ int of_mdiobus_register(struct mii_bus *
  156. mdio->dev.of_node = np;
  157. + /* Get bus level PHY reset GPIO details */
  158. + mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
  159. + of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
  160. + mdio->num_reset_gpios = of_gpio_named_count(np, "reset-gpios");
  161. +
  162. /* Register the MDIO bus */
  163. rc = mdiobus_register(mdio);
  164. if (rc)
  165. --- a/include/linux/phy.h
  166. +++ b/include/linux/phy.h
  167. @@ -193,6 +193,13 @@ struct mii_bus {
  168. * matching its address
  169. */
  170. int irq[PHY_MAX_ADDR];
  171. +
  172. + /* GPIO reset pulse width in microseconds */
  173. + int reset_delay_us;
  174. + /* Number of reset GPIOs */
  175. + int num_reset_gpios;
  176. + /* Array of RESET GPIO descriptors */
  177. + struct gpio_desc **reset_gpiod;
  178. };
  179. #define to_mii_bus(d) container_of(d, struct mii_bus, dev)