xilinx_axienet_mdio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. u32 *property_p;
  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. property_p = (u32 *) of_get_property(np1, "clock-frequency", NULL);
  153. if (!property_p) {
  154. netdev_warn(lp->ndev, "clock-frequency property not found.\n");
  155. netdev_warn(lp->ndev,
  156. "Setting MDIO clock divisor to default %d\n",
  157. DEFAULT_CLOCK_DIVISOR);
  158. clk_div = DEFAULT_CLOCK_DIVISOR;
  159. of_node_put(np1);
  160. goto issue;
  161. }
  162. host_clock = be32_to_cpup(property_p);
  163. clk_div = (host_clock / (MAX_MDIO_FREQ * 2)) - 1;
  164. /* If there is any remainder from the division of
  165. * fHOST / (MAX_MDIO_FREQ * 2), then we need to add
  166. * 1 to the clock divisor or we will surely be above 2.5 MHz
  167. */
  168. if (host_clock % (MAX_MDIO_FREQ * 2))
  169. clk_div++;
  170. netdev_dbg(lp->ndev,
  171. "Setting MDIO clock divisor to %u/%u Hz host clock.\n",
  172. clk_div, host_clock);
  173. of_node_put(np1);
  174. issue:
  175. axienet_iow(lp, XAE_MDIO_MC_OFFSET,
  176. (((u32) clk_div) | XAE_MDIO_MC_MDIOEN_MASK));
  177. ret = axienet_mdio_wait_until_ready(lp);
  178. if (ret < 0)
  179. return ret;
  180. bus = mdiobus_alloc();
  181. if (!bus)
  182. return -ENOMEM;
  183. np1 = of_get_parent(lp->phy_node);
  184. of_address_to_resource(np1, 0, &res);
  185. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  186. (unsigned long long) res.start);
  187. bus->priv = lp;
  188. bus->name = "Xilinx Axi Ethernet MDIO";
  189. bus->read = axienet_mdio_read;
  190. bus->write = axienet_mdio_write;
  191. bus->parent = lp->dev;
  192. bus->irq = lp->mdio_irqs; /* preallocated IRQ table */
  193. lp->mii_bus = bus;
  194. ret = of_mdiobus_register(bus, np1);
  195. if (ret) {
  196. mdiobus_free(bus);
  197. return ret;
  198. }
  199. return 0;
  200. }
  201. /**
  202. * axienet_mdio_teardown - MDIO remove function
  203. * @lp: Pointer to axienet local data structure.
  204. *
  205. * Unregisters the MDIO and frees any associate memory for mii bus.
  206. */
  207. void axienet_mdio_teardown(struct axienet_local *lp)
  208. {
  209. mdiobus_unregister(lp->mii_bus);
  210. kfree(lp->mii_bus->irq);
  211. mdiobus_free(lp->mii_bus);
  212. lp->mii_bus = NULL;
  213. }