xilinx_axienet_mdio.c 6.3 KB

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