emac_mdio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "emac.h"
  10. /* Number of seconds we wait for "MDIO complete" flag to appear */
  11. #define ARC_MDIO_COMPLETE_POLL_COUNT 1
  12. /**
  13. * arc_mdio_complete_wait - Waits until MDIO transaction is completed.
  14. * @priv: Pointer to ARC EMAC private data structure.
  15. *
  16. * returns: 0 on success, -ETIMEDOUT on a timeout.
  17. */
  18. static int arc_mdio_complete_wait(struct arc_emac_priv *priv)
  19. {
  20. unsigned int i;
  21. for (i = 0; i < ARC_MDIO_COMPLETE_POLL_COUNT * 40; i++) {
  22. unsigned int status = arc_reg_get(priv, R_STATUS);
  23. status &= MDIO_MASK;
  24. if (status) {
  25. /* Reset "MDIO complete" flag */
  26. arc_reg_set(priv, R_STATUS, status);
  27. return 0;
  28. }
  29. msleep(25);
  30. }
  31. return -ETIMEDOUT;
  32. }
  33. /**
  34. * arc_mdio_read - MDIO interface read function.
  35. * @bus: Pointer to MII bus structure.
  36. * @phy_addr: Address of the PHY device.
  37. * @reg_num: PHY register to read.
  38. *
  39. * returns: The register contents on success, -ETIMEDOUT on a timeout.
  40. *
  41. * Reads the contents of the requested register from the requested PHY
  42. * address.
  43. */
  44. static int arc_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  45. {
  46. struct arc_emac_priv *priv = bus->priv;
  47. unsigned int value;
  48. int error;
  49. arc_reg_set(priv, R_MDIO,
  50. 0x60020000 | (phy_addr << 23) | (reg_num << 18));
  51. error = arc_mdio_complete_wait(priv);
  52. if (error < 0)
  53. return error;
  54. value = arc_reg_get(priv, R_MDIO) & 0xffff;
  55. dev_dbg(priv->dev, "arc_mdio_read(phy_addr=%i, reg_num=%x) = %x\n",
  56. phy_addr, reg_num, value);
  57. return value;
  58. }
  59. /**
  60. * arc_mdio_write - MDIO interface write function.
  61. * @bus: Pointer to MII bus structure.
  62. * @phy_addr: Address of the PHY device.
  63. * @reg_num: PHY register to write to.
  64. * @value: Value to be written into the register.
  65. *
  66. * returns: 0 on success, -ETIMEDOUT on a timeout.
  67. *
  68. * Writes the value to the requested register.
  69. */
  70. static int arc_mdio_write(struct mii_bus *bus, int phy_addr,
  71. int reg_num, u16 value)
  72. {
  73. struct arc_emac_priv *priv = bus->priv;
  74. dev_dbg(priv->dev,
  75. "arc_mdio_write(phy_addr=%i, reg_num=%x, value=%x)\n",
  76. phy_addr, reg_num, value);
  77. arc_reg_set(priv, R_MDIO,
  78. 0x50020000 | (phy_addr << 23) | (reg_num << 18) | value);
  79. return arc_mdio_complete_wait(priv);
  80. }
  81. /**
  82. * arc_mdio_probe - MDIO probe function.
  83. * @priv: Pointer to ARC EMAC private data structure.
  84. *
  85. * returns: 0 on success, -ENOMEM when mdiobus_alloc
  86. * (to allocate memory for MII bus structure) fails.
  87. *
  88. * Sets up and registers the MDIO interface.
  89. */
  90. int arc_mdio_probe(struct arc_emac_priv *priv)
  91. {
  92. struct mii_bus *bus;
  93. int error;
  94. bus = mdiobus_alloc();
  95. if (!bus)
  96. return -ENOMEM;
  97. priv->bus = bus;
  98. bus->priv = priv;
  99. bus->parent = priv->dev;
  100. bus->name = "Synopsys MII Bus",
  101. bus->read = &arc_mdio_read;
  102. bus->write = &arc_mdio_write;
  103. snprintf(bus->id, MII_BUS_ID_SIZE, "%s", bus->name);
  104. error = of_mdiobus_register(bus, priv->dev->of_node);
  105. if (error) {
  106. dev_err(priv->dev, "cannot register MDIO bus %s\n", bus->name);
  107. mdiobus_free(bus);
  108. return error;
  109. }
  110. return 0;
  111. }
  112. /**
  113. * arc_mdio_remove - MDIO remove function.
  114. * @priv: Pointer to ARC EMAC private data structure.
  115. *
  116. * Unregisters the MDIO and frees any associate memory for MII bus.
  117. */
  118. int arc_mdio_remove(struct arc_emac_priv *priv)
  119. {
  120. mdiobus_unregister(priv->bus);
  121. mdiobus_free(priv->bus);
  122. priv->bus = NULL;
  123. return 0;
  124. }