You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mii_bit.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2018 Sylvie Barlow <sylvie.c.barlow@gmail.com>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <unistd.h>
  26. #include <ipxe/bitbash.h>
  27. #include <ipxe/mii_bit.h>
  28. /**
  29. * Transfer bits over MII bit-bashing interface
  30. *
  31. * @v basher Bit basher
  32. * @v mask Mask
  33. * @v write Data to write
  34. * @ret read Data read
  35. */
  36. static uint32_t mii_bit_xfer ( struct bit_basher *basher,
  37. uint32_t mask, uint32_t write ) {
  38. uint32_t read = 0;
  39. int bit;
  40. for ( ; mask ; mask >>= 1 ) {
  41. /* Delay */
  42. udelay ( 1 );
  43. /* Write bit to basher */
  44. write_bit ( basher, MII_BIT_MDIO, ( write & mask ) );
  45. /* Read bit from basher */
  46. bit = read_bit ( basher, MII_BIT_MDIO );
  47. read <<= 1;
  48. read |= ( bit & 1 );
  49. /* Set clock high */
  50. write_bit ( basher, MII_BIT_MDC, 1 );
  51. /* Delay */
  52. udelay ( 1 );
  53. /* Set clock low */
  54. write_bit ( basher, MII_BIT_MDC, 0 );
  55. }
  56. return read;
  57. }
  58. /**
  59. * Read or write via MII bit-bashing interface
  60. *
  61. * @v basher Bit basher
  62. * @v phy PHY address
  63. * @v reg Register address
  64. * @v data Data to write
  65. * @v cmd Command
  66. * @ret data Data read
  67. */
  68. static unsigned int mii_bit_rw ( struct bit_basher *basher,
  69. unsigned int phy, unsigned int reg,
  70. unsigned int data, unsigned int cmd ) {
  71. /* Initiate drive for write */
  72. write_bit ( basher, MII_BIT_DRIVE, 1 );
  73. /* Write start */
  74. mii_bit_xfer ( basher, MII_BIT_START_MASK, MII_BIT_START );
  75. /* Write command */
  76. mii_bit_xfer ( basher, MII_BIT_CMD_MASK, cmd );
  77. /* Write PHY address */
  78. mii_bit_xfer ( basher, MII_BIT_PHY_MASK, phy );
  79. /* Write register address */
  80. mii_bit_xfer ( basher, MII_BIT_REG_MASK, reg );
  81. /* Switch drive to read if applicable */
  82. write_bit ( basher, MII_BIT_DRIVE, ( cmd & MII_BIT_CMD_RW ) );
  83. /* Allow space for turnaround */
  84. mii_bit_xfer ( basher, MII_BIT_SWITCH_MASK, MII_BIT_SWITCH );
  85. /* Read or write data */
  86. data = mii_bit_xfer (basher, MII_BIT_DATA_MASK, data );
  87. /* Initiate drive for read */
  88. write_bit ( basher, MII_BIT_DRIVE, 0 );
  89. return data;
  90. }
  91. /**
  92. * Read from MII register
  93. *
  94. * @v mdio MII interface
  95. * @v phy PHY address
  96. * @v reg Register address
  97. * @ret data Data read, or negative error
  98. */
  99. static int mii_bit_read ( struct mii_interface *mdio, unsigned int phy,
  100. unsigned int reg ) {
  101. struct mii_bit_basher *miibit =
  102. container_of ( mdio, struct mii_bit_basher, mdio );
  103. struct bit_basher *basher = &miibit->basher;
  104. return mii_bit_rw ( basher, phy, reg, 0, MII_BIT_CMD_READ );
  105. }
  106. /**
  107. * Write to MII register
  108. *
  109. * @v mdio MII interface
  110. * @v phy PHY address
  111. * @v reg Register address
  112. * @v data Data to write
  113. * @ret rc Return status code
  114. */
  115. static int mii_bit_write ( struct mii_interface *mdio, unsigned int phy,
  116. unsigned int reg, unsigned int data ) {
  117. struct mii_bit_basher *miibit =
  118. container_of ( mdio, struct mii_bit_basher, mdio );
  119. struct bit_basher *basher = &miibit->basher;
  120. mii_bit_rw ( basher, phy, reg, data, MII_BIT_CMD_WRITE );
  121. return 0;
  122. }
  123. /** MII bit basher operations */
  124. static struct mii_operations mii_bit_op = {
  125. .read = mii_bit_read,
  126. .write = mii_bit_write,
  127. };
  128. /**
  129. * Initialise bit-bashing interface
  130. *
  131. * @v miibit MII bit basher
  132. */
  133. void init_mii_bit_basher ( struct mii_bit_basher *miibit ) {
  134. mdio_init ( &miibit->mdio, &mii_bit_op );
  135. };