mdio-bitbang.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/slab.h>
  22. #include <linux/types.h>
  23. #include <linux/delay.h>
  24. #define MDIO_READ 1
  25. #define MDIO_WRITE 0
  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 read, 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 (10) */
  93. mdiobb_send_bit(ctrl, 0);
  94. mdiobb_send_bit(ctrl, 1);
  95. mdiobb_send_bit(ctrl, read);
  96. mdiobb_send_bit(ctrl, !read);
  97. mdiobb_send_num(ctrl, phy, 5);
  98. mdiobb_send_num(ctrl, reg, 5);
  99. }
  100. static int mdiobb_read(struct mii_bus *bus, int phy, int reg)
  101. {
  102. struct mdiobb_ctrl *ctrl = bus->priv;
  103. int ret, i;
  104. mdiobb_cmd(ctrl, MDIO_READ, phy, reg);
  105. ctrl->ops->set_mdio_dir(ctrl, 0);
  106. /* check the turnaround bit: the PHY should be driving it to zero */
  107. if (mdiobb_get_bit(ctrl) != 0) {
  108. /* PHY didn't drive TA low -- flush any bits it
  109. * may be trying to send.
  110. */
  111. for (i = 0; i < 32; i++)
  112. mdiobb_get_bit(ctrl);
  113. return 0xffff;
  114. }
  115. ret = mdiobb_get_num(ctrl, 16);
  116. mdiobb_get_bit(ctrl);
  117. return ret;
  118. }
  119. static int mdiobb_write(struct mii_bus *bus, int phy, int reg, u16 val)
  120. {
  121. struct mdiobb_ctrl *ctrl = bus->priv;
  122. mdiobb_cmd(ctrl, MDIO_WRITE, phy, reg);
  123. /* send the turnaround (10) */
  124. mdiobb_send_bit(ctrl, 1);
  125. mdiobb_send_bit(ctrl, 0);
  126. mdiobb_send_num(ctrl, val, 16);
  127. ctrl->ops->set_mdio_dir(ctrl, 0);
  128. mdiobb_get_bit(ctrl);
  129. return 0;
  130. }
  131. struct mii_bus *alloc_mdio_bitbang(struct mdiobb_ctrl *ctrl)
  132. {
  133. struct mii_bus *bus;
  134. bus = mdiobus_alloc();
  135. if (!bus)
  136. return NULL;
  137. __module_get(ctrl->ops->owner);
  138. bus->read = mdiobb_read;
  139. bus->write = mdiobb_write;
  140. bus->priv = ctrl;
  141. return bus;
  142. }
  143. EXPORT_SYMBOL(alloc_mdio_bitbang);
  144. void free_mdio_bitbang(struct mii_bus *bus)
  145. {
  146. struct mdiobb_ctrl *ctrl = bus->priv;
  147. module_put(ctrl->ops->owner);
  148. mdiobus_free(bus);
  149. }
  150. EXPORT_SYMBOL(free_mdio_bitbang);
  151. MODULE_LICENSE("GPL");