xilinx_axienet_mdio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MDIO bus driver for the Xilinx Axi Ethernet device
  4. *
  5. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  6. * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
  7. * Copyright (c) 2010 - 2011 PetaLogix
  8. * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
  9. */
  10. #include <linux/of_address.h>
  11. #include <linux/of_mdio.h>
  12. #include <linux/jiffies.h>
  13. #include "xilinx_axienet.h"
  14. #define MAX_MDIO_FREQ 2500000 /* 2.5 MHz */
  15. #define DEFAULT_CLOCK_DIVISOR XAE_MDIO_DIV_DFT
  16. /* Wait till MDIO interface is ready to accept a new transaction.*/
  17. int axienet_mdio_wait_until_ready(struct axienet_local *lp)
  18. {
  19. unsigned long end = jiffies + 2;
  20. while (!(axienet_ior(lp, XAE_MDIO_MCR_OFFSET) &
  21. XAE_MDIO_MCR_READY_MASK)) {
  22. if (time_before_eq(end, jiffies)) {
  23. WARN_ON(1);
  24. return -ETIMEDOUT;
  25. }
  26. udelay(1);
  27. }
  28. return 0;
  29. }
  30. /**
  31. * axienet_mdio_read - MDIO interface read function
  32. * @bus: Pointer to mii bus structure
  33. * @phy_id: Address of the PHY device
  34. * @reg: PHY register to read
  35. *
  36. * Return: The register contents on success, -ETIMEDOUT on a timeout
  37. *
  38. * Reads the contents of the requested register from the requested PHY
  39. * address by first writing the details into MCR register. After a while
  40. * the register MRD is read to obtain the PHY register content.
  41. */
  42. static int axienet_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  43. {
  44. u32 rc;
  45. int ret;
  46. struct axienet_local *lp = bus->priv;
  47. ret = axienet_mdio_wait_until_ready(lp);
  48. if (ret < 0)
  49. return ret;
  50. axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
  51. (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
  52. XAE_MDIO_MCR_PHYAD_MASK) |
  53. ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
  54. XAE_MDIO_MCR_REGAD_MASK) |
  55. XAE_MDIO_MCR_INITIATE_MASK |
  56. XAE_MDIO_MCR_OP_READ_MASK));
  57. ret = axienet_mdio_wait_until_ready(lp);
  58. if (ret < 0)
  59. return ret;
  60. rc = axienet_ior(lp, XAE_MDIO_MRD_OFFSET) & 0x0000FFFF;
  61. dev_dbg(lp->dev, "axienet_mdio_read(phy_id=%i, reg=%x) == %x\n",
  62. phy_id, reg, rc);
  63. return rc;
  64. }
  65. /**
  66. * axienet_mdio_write - MDIO interface write function
  67. * @bus: Pointer to mii bus structure
  68. * @phy_id: Address of the PHY device
  69. * @reg: PHY register to write to
  70. * @val: Value to be written into the register
  71. *
  72. * Return: 0 on success, -ETIMEDOUT on a timeout
  73. *
  74. * Writes the value to the requested register by first writing the value
  75. * into MWD register. The the MCR register is then appropriately setup
  76. * to finish the write operation.
  77. */
  78. static int axienet_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  79. u16 val)
  80. {
  81. int ret;
  82. struct axienet_local *lp = bus->priv;
  83. dev_dbg(lp->dev, "axienet_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
  84. phy_id, reg, val);
  85. ret = axienet_mdio_wait_until_ready(lp);
  86. if (ret < 0)
  87. return ret;
  88. axienet_iow(lp, XAE_MDIO_MWD_OFFSET, (u32) val);
  89. axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
  90. (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
  91. XAE_MDIO_MCR_PHYAD_MASK) |
  92. ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
  93. XAE_MDIO_MCR_REGAD_MASK) |
  94. XAE_MDIO_MCR_INITIATE_MASK |
  95. XAE_MDIO_MCR_OP_WRITE_MASK));
  96. ret = axienet_mdio_wait_until_ready(lp);
  97. if (ret < 0)
  98. return ret;
  99. return 0;
  100. }
  101. /**
  102. * axienet_mdio_setup - MDIO setup function
  103. * @lp: Pointer to axienet local data structure.
  104. * @np: Pointer to device node
  105. *
  106. * Return: 0 on success, -ETIMEDOUT on a timeout, -ENOMEM when
  107. * mdiobus_alloc (to allocate memory for mii bus structure) fails.
  108. *
  109. * Sets up the MDIO interface by initializing the MDIO clock and enabling the
  110. * MDIO interface in hardware. Register the MDIO interface.
  111. **/
  112. int axienet_mdio_setup(struct axienet_local *lp, struct device_node *np)
  113. {
  114. int ret;
  115. u32 clk_div, host_clock;
  116. struct mii_bus *bus;
  117. struct resource res;
  118. struct device_node *np1;
  119. /* clk_div can be calculated by deriving it from the equation:
  120. * fMDIO = fHOST / ((1 + clk_div) * 2)
  121. *
  122. * Where fMDIO <= 2500000, so we get:
  123. * fHOST / ((1 + clk_div) * 2) <= 2500000
  124. *
  125. * Then we get:
  126. * 1 / ((1 + clk_div) * 2) <= (2500000 / fHOST)
  127. *
  128. * Then we get:
  129. * 1 / (1 + clk_div) <= ((2500000 * 2) / fHOST)
  130. *
  131. * Then we get:
  132. * 1 / (1 + clk_div) <= (5000000 / fHOST)
  133. *
  134. * So:
  135. * (1 + clk_div) >= (fHOST / 5000000)
  136. *
  137. * And finally:
  138. * clk_div >= (fHOST / 5000000) - 1
  139. *
  140. * fHOST can be read from the flattened device tree as property
  141. * "clock-frequency" from the CPU
  142. */
  143. np1 = of_find_node_by_name(NULL, "cpu");
  144. if (!np1) {
  145. netdev_warn(lp->ndev, "Could not find CPU device node.\n");
  146. netdev_warn(lp->ndev,
  147. "Setting MDIO clock divisor to default %d\n",
  148. DEFAULT_CLOCK_DIVISOR);
  149. clk_div = DEFAULT_CLOCK_DIVISOR;
  150. goto issue;
  151. }
  152. if (of_property_read_u32(np1, "clock-frequency", &host_clock)) {
  153. netdev_warn(lp->ndev, "clock-frequency property not found.\n");
  154. netdev_warn(lp->ndev,
  155. "Setting MDIO clock divisor to default %d\n",
  156. DEFAULT_CLOCK_DIVISOR);
  157. clk_div = DEFAULT_CLOCK_DIVISOR;
  158. of_node_put(np1);
  159. goto issue;
  160. }
  161. clk_div = (host_clock / (MAX_MDIO_FREQ * 2)) - 1;
  162. /* If there is any remainder from the division of
  163. * fHOST / (MAX_MDIO_FREQ * 2), then we need to add
  164. * 1 to the clock divisor or we will surely be above 2.5 MHz
  165. */
  166. if (host_clock % (MAX_MDIO_FREQ * 2))
  167. clk_div++;
  168. netdev_dbg(lp->ndev,
  169. "Setting MDIO clock divisor to %u/%u Hz host clock.\n",
  170. clk_div, host_clock);
  171. of_node_put(np1);
  172. issue:
  173. axienet_iow(lp, XAE_MDIO_MC_OFFSET,
  174. (((u32) clk_div) | XAE_MDIO_MC_MDIOEN_MASK));
  175. ret = axienet_mdio_wait_until_ready(lp);
  176. if (ret < 0)
  177. return ret;
  178. bus = mdiobus_alloc();
  179. if (!bus)
  180. return -ENOMEM;
  181. np1 = of_get_parent(lp->phy_node);
  182. of_address_to_resource(np1, 0, &res);
  183. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  184. (unsigned long long) res.start);
  185. bus->priv = lp;
  186. bus->name = "Xilinx Axi Ethernet MDIO";
  187. bus->read = axienet_mdio_read;
  188. bus->write = axienet_mdio_write;
  189. bus->parent = lp->dev;
  190. lp->mii_bus = bus;
  191. ret = of_mdiobus_register(bus, np1);
  192. if (ret) {
  193. mdiobus_free(bus);
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. /**
  199. * axienet_mdio_teardown - MDIO remove function
  200. * @lp: Pointer to axienet local data structure.
  201. *
  202. * Unregisters the MDIO and frees any associate memory for mii bus.
  203. */
  204. void axienet_mdio_teardown(struct axienet_local *lp)
  205. {
  206. mdiobus_unregister(lp->mii_bus);
  207. mdiobus_free(lp->mii_bus);
  208. lp->mii_bus = NULL;
  209. }