mdio-bitbang.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/delay.h>
  17. #include <linux/mdio-bitbang.h>
  18. #include <linux/module.h>
  19. #include <linux/types.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. Theoretically clause 45 and normal
  108. devices can exist on the same bus. Normal devices should ignore the MDIO_ADDR
  109. phase. */
  110. static void mdiobb_cmd_addr(struct mdiobb_ctrl *ctrl, int phy, int dev_addr,
  111. int reg)
  112. {
  113. mdiobb_cmd(ctrl, MDIO_C45_ADDR, phy, dev_addr);
  114. /* send the turnaround (10) */
  115. mdiobb_send_bit(ctrl, 1);
  116. mdiobb_send_bit(ctrl, 0);
  117. mdiobb_send_num(ctrl, reg, 16);
  118. ctrl->ops->set_mdio_dir(ctrl, 0);
  119. mdiobb_get_bit(ctrl);
  120. }
  121. static int mdiobb_read_common(struct mii_bus *bus, int phy)
  122. {
  123. struct mdiobb_ctrl *ctrl = bus->priv;
  124. int ret, i;
  125. ctrl->ops->set_mdio_dir(ctrl, 0);
  126. /* check the turnaround bit: the PHY should be driving it to zero, if this
  127. * PHY is listed in phy_ignore_ta_mask as having broken TA, skip that
  128. */
  129. if (mdiobb_get_bit(ctrl) != 0 &&
  130. !(bus->phy_ignore_ta_mask & (1 << phy))) {
  131. /* PHY didn't drive TA low -- flush any bits it
  132. * may be trying to send.
  133. */
  134. for (i = 0; i < 32; i++)
  135. mdiobb_get_bit(ctrl);
  136. return 0xffff;
  137. }
  138. ret = mdiobb_get_num(ctrl, 16);
  139. mdiobb_get_bit(ctrl);
  140. return ret;
  141. }
  142. int mdiobb_read_c22(struct mii_bus *bus, int phy, int reg)
  143. {
  144. struct mdiobb_ctrl *ctrl = bus->priv;
  145. mdiobb_cmd(ctrl, ctrl->op_c22_read, phy, reg);
  146. return mdiobb_read_common(bus, phy);
  147. }
  148. EXPORT_SYMBOL(mdiobb_read_c22);
  149. int mdiobb_read_c45(struct mii_bus *bus, int phy, int devad, int reg)
  150. {
  151. struct mdiobb_ctrl *ctrl = bus->priv;
  152. mdiobb_cmd_addr(ctrl, phy, devad, reg);
  153. mdiobb_cmd(ctrl, MDIO_C45_READ, phy, devad);
  154. return mdiobb_read_common(bus, phy);
  155. }
  156. EXPORT_SYMBOL(mdiobb_read_c45);
  157. static int mdiobb_write_common(struct mii_bus *bus, u16 val)
  158. {
  159. struct mdiobb_ctrl *ctrl = bus->priv;
  160. /* send the turnaround (10) */
  161. mdiobb_send_bit(ctrl, 1);
  162. mdiobb_send_bit(ctrl, 0);
  163. mdiobb_send_num(ctrl, val, 16);
  164. ctrl->ops->set_mdio_dir(ctrl, 0);
  165. mdiobb_get_bit(ctrl);
  166. return 0;
  167. }
  168. int mdiobb_write_c22(struct mii_bus *bus, int phy, int reg, u16 val)
  169. {
  170. struct mdiobb_ctrl *ctrl = bus->priv;
  171. mdiobb_cmd(ctrl, ctrl->op_c22_write, phy, reg);
  172. return mdiobb_write_common(bus, val);
  173. }
  174. EXPORT_SYMBOL(mdiobb_write_c22);
  175. int mdiobb_write_c45(struct mii_bus *bus, int phy, int devad, int reg, u16 val)
  176. {
  177. struct mdiobb_ctrl *ctrl = bus->priv;
  178. mdiobb_cmd_addr(ctrl, phy, devad, reg);
  179. mdiobb_cmd(ctrl, MDIO_C45_WRITE, phy, devad);
  180. return mdiobb_write_common(bus, val);
  181. }
  182. EXPORT_SYMBOL(mdiobb_write_c45);
  183. struct mii_bus *alloc_mdio_bitbang(struct mdiobb_ctrl *ctrl)
  184. {
  185. struct mii_bus *bus;
  186. bus = mdiobus_alloc();
  187. if (!bus)
  188. return NULL;
  189. __module_get(ctrl->ops->owner);
  190. bus->read = mdiobb_read_c22;
  191. bus->write = mdiobb_write_c22;
  192. bus->read_c45 = mdiobb_read_c45;
  193. bus->write_c45 = mdiobb_write_c45;
  194. bus->priv = ctrl;
  195. if (!ctrl->override_op_c22) {
  196. ctrl->op_c22_read = MDIO_READ;
  197. ctrl->op_c22_write = MDIO_WRITE;
  198. }
  199. return bus;
  200. }
  201. EXPORT_SYMBOL(alloc_mdio_bitbang);
  202. void free_mdio_bitbang(struct mii_bus *bus)
  203. {
  204. struct mdiobb_ctrl *ctrl = bus->priv;
  205. module_put(ctrl->ops->owner);
  206. mdiobus_free(bus);
  207. }
  208. EXPORT_SYMBOL(free_mdio_bitbang);
  209. MODULE_LICENSE("GPL v2");
  210. MODULE_DESCRIPTION("Bitbanged MDIO buses");