emac_mdio.c 4.4 KB

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