mdio-bitbang.c 5.7 KB

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