emac_mdio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2004-2013 Synopsys, Inc. (www.synopsys.com)
  4. *
  5. * MDIO implementation for ARC EMAC
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/of_mdio.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/gpio/consumer.h>
  11. #include "emac.h"
  12. /* Number of seconds we wait for "MDIO complete" flag to appear */
  13. #define ARC_MDIO_COMPLETE_POLL_COUNT 1
  14. /**
  15. * arc_mdio_complete_wait - Waits until MDIO transaction is completed.
  16. * @priv: Pointer to ARC EMAC private data structure.
  17. *
  18. * returns: 0 on success, -ETIMEDOUT on a timeout.
  19. */
  20. static int arc_mdio_complete_wait(struct arc_emac_priv *priv)
  21. {
  22. unsigned int i;
  23. for (i = 0; i < ARC_MDIO_COMPLETE_POLL_COUNT * 40; i++) {
  24. unsigned int status = arc_reg_get(priv, R_STATUS);
  25. status &= MDIO_MASK;
  26. if (status) {
  27. /* Reset "MDIO complete" flag */
  28. arc_reg_set(priv, R_STATUS, status);
  29. return 0;
  30. }
  31. msleep(25);
  32. }
  33. return -ETIMEDOUT;
  34. }
  35. /**
  36. * arc_mdio_read - MDIO interface read function.
  37. * @bus: Pointer to MII bus structure.
  38. * @phy_addr: Address of the PHY device.
  39. * @reg_num: PHY register to read.
  40. *
  41. * returns: The register contents on success, -ETIMEDOUT on a timeout.
  42. *
  43. * Reads the contents of the requested register from the requested PHY
  44. * address.
  45. */
  46. static int arc_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  47. {
  48. struct arc_emac_priv *priv = bus->priv;
  49. unsigned int value;
  50. int error;
  51. arc_reg_set(priv, R_MDIO,
  52. 0x60020000 | (phy_addr << 23) | (reg_num << 18));
  53. error = arc_mdio_complete_wait(priv);
  54. if (error < 0)
  55. return error;
  56. value = arc_reg_get(priv, R_MDIO) & 0xffff;
  57. dev_dbg(priv->dev, "arc_mdio_read(phy_addr=%i, reg_num=%x) = %x\n",
  58. phy_addr, reg_num, value);
  59. return value;
  60. }
  61. /**
  62. * arc_mdio_write - MDIO interface write function.
  63. * @bus: Pointer to MII bus structure.
  64. * @phy_addr: Address of the PHY device.
  65. * @reg_num: PHY register to write to.
  66. * @value: Value to be written into the register.
  67. *
  68. * returns: 0 on success, -ETIMEDOUT on a timeout.
  69. *
  70. * Writes the value to the requested register.
  71. */
  72. static int arc_mdio_write(struct mii_bus *bus, int phy_addr,
  73. int reg_num, u16 value)
  74. {
  75. struct arc_emac_priv *priv = bus->priv;
  76. dev_dbg(priv->dev,
  77. "arc_mdio_write(phy_addr=%i, reg_num=%x, value=%x)\n",
  78. phy_addr, reg_num, value);
  79. arc_reg_set(priv, R_MDIO,
  80. 0x50020000 | (phy_addr << 23) | (reg_num << 18) | value);
  81. return arc_mdio_complete_wait(priv);
  82. }
  83. /**
  84. * arc_mdio_reset
  85. * @bus: points to the mii_bus structure
  86. * Description: reset the MII bus
  87. */
  88. static int arc_mdio_reset(struct mii_bus *bus)
  89. {
  90. struct arc_emac_priv *priv = bus->priv;
  91. struct arc_emac_mdio_bus_data *data = &priv->bus_data;
  92. if (data->reset_gpio) {
  93. gpiod_set_value_cansleep(data->reset_gpio, 1);
  94. msleep(data->msec);
  95. gpiod_set_value_cansleep(data->reset_gpio, 0);
  96. }
  97. return 0;
  98. }
  99. /**
  100. * arc_mdio_probe - MDIO probe function.
  101. * @priv: Pointer to ARC EMAC private data structure.
  102. *
  103. * returns: 0 on success, -ENOMEM when mdiobus_alloc
  104. * (to allocate memory for MII bus structure) fails.
  105. *
  106. * Sets up and registers the MDIO interface.
  107. */
  108. int arc_mdio_probe(struct arc_emac_priv *priv)
  109. {
  110. struct arc_emac_mdio_bus_data *data = &priv->bus_data;
  111. struct device_node *np = priv->dev->of_node;
  112. struct mii_bus *bus;
  113. int error;
  114. bus = mdiobus_alloc();
  115. if (!bus)
  116. return -ENOMEM;
  117. priv->bus = bus;
  118. bus->priv = priv;
  119. bus->parent = priv->dev;
  120. bus->name = "Synopsys MII Bus";
  121. bus->read = &arc_mdio_read;
  122. bus->write = &arc_mdio_write;
  123. bus->reset = &arc_mdio_reset;
  124. /* optional reset-related properties */
  125. data->reset_gpio = devm_gpiod_get_optional(priv->dev, "phy-reset",
  126. GPIOD_OUT_LOW);
  127. if (IS_ERR(data->reset_gpio)) {
  128. error = PTR_ERR(data->reset_gpio);
  129. dev_err(priv->dev, "Failed to request gpio: %d\n", error);
  130. return error;
  131. }
  132. of_property_read_u32(np, "phy-reset-duration", &data->msec);
  133. /* A sane reset duration should not be longer than 1s */
  134. if (data->msec > 1000)
  135. data->msec = 1;
  136. snprintf(bus->id, MII_BUS_ID_SIZE, "%s", bus->name);
  137. error = of_mdiobus_register(bus, priv->dev->of_node);
  138. if (error) {
  139. dev_err(priv->dev, "cannot register MDIO bus %s\n", bus->name);
  140. mdiobus_free(bus);
  141. return error;
  142. }
  143. return 0;
  144. }
  145. /**
  146. * arc_mdio_remove - MDIO remove function.
  147. * @priv: Pointer to ARC EMAC private data structure.
  148. *
  149. * Unregisters the MDIO and frees any associate memory for MII bus.
  150. */
  151. int arc_mdio_remove(struct arc_emac_priv *priv)
  152. {
  153. mdiobus_unregister(priv->bus);
  154. mdiobus_free(priv->bus);
  155. priv->bus = NULL;
  156. return 0;
  157. }