mdio-bitbang.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Bitbanged MDIO support.
  4. *
  5. * Author: Scott Wood <scottwood@freescale.com>
  6. * Copyright (c) 2007 Freescale Semiconductor
  7. *
  8. * Based on CPM2 MDIO code which is:
  9. *
  10. * Copyright (c) 2003 Intracom S.A.
  11. * by Pantelis Antoniou <panto@intracom.gr>
  12. *
  13. * 2005 (c) MontaVista Software, Inc.
  14. * Vitaly Bordug <vbordug@ru.mvista.com>
  15. */
  16. #include <linux/module.h>
  17. #include <linux/mdio-bitbang.h>
  18. #include <linux/types.h>
  19. #include <linux/delay.h>
  20. #define MDIO_READ 2
  21. #define MDIO_WRITE 1
  22. #define MDIO_C45 (1<<15)
  23. #define MDIO_C45_ADDR (MDIO_C45 | 0)
  24. #define MDIO_C45_READ (MDIO_C45 | 3)
  25. #define MDIO_C45_WRITE (MDIO_C45 | 1)
  26. #define MDIO_SETUP_TIME 10
  27. #define MDIO_HOLD_TIME 10
  28. /* Minimum MDC period is 400 ns, plus some margin for error. MDIO_DELAY
  29. * is done twice per period.
  30. */
  31. #define MDIO_DELAY 250
  32. /* The PHY may take up to 300 ns to produce data, plus some margin
  33. * for error.
  34. */
  35. #define MDIO_READ_DELAY 350
  36. /* MDIO must already be configured as output. */
  37. static void mdiobb_send_bit(struct mdiobb_ctrl *ctrl, int val)
  38. {
  39. const struct mdiobb_ops *ops = ctrl->ops;
  40. ops->set_mdio_data(ctrl, val);
  41. ndelay(MDIO_DELAY);
  42. ops->set_mdc(ctrl, 1);
  43. ndelay(MDIO_DELAY);
  44. ops->set_mdc(ctrl, 0);
  45. }
  46. /* MDIO must already be configured as input. */
  47. static int mdiobb_get_bit(struct mdiobb_ctrl *ctrl)
  48. {
  49. const struct mdiobb_ops *ops = ctrl->ops;
  50. ndelay(MDIO_DELAY);
  51. ops->set_mdc(ctrl, 1);
  52. ndelay(MDIO_READ_DELAY);
  53. ops->set_mdc(ctrl, 0);
  54. return ops->get_mdio_data(ctrl);
  55. }
  56. /* MDIO must already be configured as output. */
  57. static void mdiobb_send_num(struct mdiobb_ctrl *ctrl, u16 val, int bits)
  58. {
  59. int i;
  60. for (i = bits - 1; i >= 0; i--)
  61. mdiobb_send_bit(ctrl, (val >> i) & 1);
  62. }
  63. /* MDIO must already be configured as input. */
  64. static u16 mdiobb_get_num(struct mdiobb_ctrl *ctrl, int bits)
  65. {
  66. int i;
  67. u16 ret = 0;
  68. for (i = bits - 1; i >= 0; i--) {
  69. ret <<= 1;
  70. ret |= mdiobb_get_bit(ctrl);
  71. }
  72. return ret;
  73. }
  74. /* Utility to send the preamble, address, and
  75. * register (common to read and write).
  76. */
  77. static void mdiobb_cmd(struct mdiobb_ctrl *ctrl, int op, u8 phy, u8 reg)
  78. {
  79. const struct mdiobb_ops *ops = ctrl->ops;
  80. int i;
  81. ops->set_mdio_dir(ctrl, 1);
  82. /*
  83. * Send a 32 bit preamble ('1's) with an extra '1' bit for good
  84. * measure. The IEEE spec says this is a PHY optional
  85. * requirement. The AMD 79C874 requires one after power up and
  86. * one after a MII communications error. This means that we are
  87. * doing more preambles than we need, but it is safer and will be
  88. * much more robust.
  89. */
  90. for (i = 0; i < 32; i++)
  91. mdiobb_send_bit(ctrl, 1);
  92. /* send the start bit (01) and the read opcode (10) or write (01).
  93. Clause 45 operation uses 00 for the start and 11, 10 for
  94. read/write */
  95. mdiobb_send_bit(ctrl, 0);
  96. if (op & MDIO_C45)
  97. mdiobb_send_bit(ctrl, 0);
  98. else
  99. mdiobb_send_bit(ctrl, 1);
  100. mdiobb_send_bit(ctrl, (op >> 1) & 1);
  101. mdiobb_send_bit(ctrl, (op >> 0) & 1);
  102. mdiobb_send_num(ctrl, phy, 5);
  103. mdiobb_send_num(ctrl, reg, 5);
  104. }
  105. /* In clause 45 mode all commands are prefixed by MDIO_ADDR to specify the
  106. lower 16 bits of the 21 bit address. This transfer is done identically to a
  107. MDIO_WRITE except for a different code. To enable clause 45 mode or
  108. MII_ADDR_C45 into the address. Theoretically clause 45 and normal devices
  109. can exist on the same bus. Normal devices should ignore the MDIO_ADDR
  110. phase. */
  111. static int mdiobb_cmd_addr(struct mdiobb_ctrl *ctrl, int phy, u32 addr)
  112. {
  113. unsigned int dev_addr = (addr >> 16) & 0x1F;
  114. unsigned int reg = addr & 0xFFFF;
  115. mdiobb_cmd(ctrl, MDIO_C45_ADDR, phy, dev_addr);
  116. /* send the turnaround (10) */
  117. mdiobb_send_bit(ctrl, 1);
  118. mdiobb_send_bit(ctrl, 0);
  119. mdiobb_send_num(ctrl, reg, 16);
  120. ctrl->ops->set_mdio_dir(ctrl, 0);
  121. mdiobb_get_bit(ctrl);
  122. return dev_addr;
  123. }
  124. static int mdiobb_read(struct mii_bus *bus, int phy, int reg)
  125. {
  126. struct mdiobb_ctrl *ctrl = bus->priv;
  127. int ret, i;
  128. if (reg & MII_ADDR_C45) {
  129. reg = mdiobb_cmd_addr(ctrl, phy, reg);
  130. mdiobb_cmd(ctrl, MDIO_C45_READ, phy, reg);
  131. } else
  132. mdiobb_cmd(ctrl, MDIO_READ, phy, reg);
  133. ctrl->ops->set_mdio_dir(ctrl, 0);
  134. /* check the turnaround bit: the PHY should be driving it to zero, if this
  135. * PHY is listed in phy_ignore_ta_mask as having broken TA, skip that
  136. */
  137. if (mdiobb_get_bit(ctrl) != 0 &&
  138. !(bus->phy_ignore_ta_mask & (1 << phy))) {
  139. /* PHY didn't drive TA low -- flush any bits it
  140. * may be trying to send.
  141. */
  142. for (i = 0; i < 32; i++)
  143. mdiobb_get_bit(ctrl);
  144. return 0xffff;
  145. }
  146. ret = mdiobb_get_num(ctrl, 16);
  147. mdiobb_get_bit(ctrl);
  148. return ret;
  149. }
  150. static int mdiobb_write(struct mii_bus *bus, int phy, int reg, u16 val)
  151. {
  152. struct mdiobb_ctrl *ctrl = bus->priv;
  153. if (reg & MII_ADDR_C45) {
  154. reg = mdiobb_cmd_addr(ctrl, phy, reg);
  155. mdiobb_cmd(ctrl, MDIO_C45_WRITE, phy, reg);
  156. } else
  157. mdiobb_cmd(ctrl, MDIO_WRITE, phy, reg);
  158. /* send the turnaround (10) */
  159. mdiobb_send_bit(ctrl, 1);
  160. mdiobb_send_bit(ctrl, 0);
  161. mdiobb_send_num(ctrl, val, 16);
  162. ctrl->ops->set_mdio_dir(ctrl, 0);
  163. mdiobb_get_bit(ctrl);
  164. return 0;
  165. }
  166. struct mii_bus *alloc_mdio_bitbang(struct mdiobb_ctrl *ctrl)
  167. {
  168. struct mii_bus *bus;
  169. bus = mdiobus_alloc();
  170. if (!bus)
  171. return NULL;
  172. __module_get(ctrl->ops->owner);
  173. bus->read = mdiobb_read;
  174. bus->write = mdiobb_write;
  175. bus->priv = ctrl;
  176. return bus;
  177. }
  178. EXPORT_SYMBOL(alloc_mdio_bitbang);
  179. void free_mdio_bitbang(struct mii_bus *bus)
  180. {
  181. struct mdiobb_ctrl *ctrl = bus->priv;
  182. module_put(ctrl->ops->owner);
  183. mdiobus_free(bus);
  184. }
  185. EXPORT_SYMBOL(free_mdio_bitbang);
  186. MODULE_LICENSE("GPL v2");