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.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  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 (at your option) 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 <string.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <ipxe/mii.h>
  28. /** @file
  29. *
  30. * Media Independent Interface
  31. *
  32. */
  33. /**
  34. * Restart autonegotiation
  35. *
  36. * @v mii MII device
  37. * @ret rc Return status code
  38. */
  39. int mii_restart ( struct mii_device *mii ) {
  40. int bmcr;
  41. int rc;
  42. /* Read BMCR */
  43. bmcr = mii_read ( mii, MII_BMCR );
  44. if ( bmcr < 0 ) {
  45. rc = bmcr;
  46. DBGC ( mii, "MII %p could not read BMCR: %s\n",
  47. mii, strerror ( rc ) );
  48. return rc;
  49. }
  50. /* Enable and restart autonegotiation */
  51. bmcr |= ( BMCR_ANENABLE | BMCR_ANRESTART );
  52. if ( ( rc = mii_write ( mii, MII_BMCR, bmcr ) ) != 0 ) {
  53. DBGC ( mii, "MII %p could not write BMCR: %s\n",
  54. mii, strerror ( rc ) );
  55. return rc;
  56. }
  57. DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
  58. return 0;
  59. }
  60. /**
  61. * Reset MII device
  62. *
  63. * @v mii MII device
  64. * @ret rc Return status code
  65. */
  66. int mii_reset ( struct mii_device *mii ) {
  67. unsigned int i;
  68. int bmcr;
  69. int rc;
  70. /* Power-up, enable autonegotiation and initiate reset */
  71. if ( ( rc = mii_write ( mii, MII_BMCR,
  72. ( BMCR_RESET | BMCR_ANENABLE ) ) ) != 0 ) {
  73. DBGC ( mii, "MII %p could not write BMCR: %s\n",
  74. mii, strerror ( rc ) );
  75. return rc;
  76. }
  77. /* Wait for reset to complete */
  78. for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {
  79. /* Check if reset has completed */
  80. bmcr = mii_read ( mii, MII_BMCR );
  81. if ( bmcr < 0 ) {
  82. rc = bmcr;
  83. DBGC ( mii, "MII %p could not read BMCR: %s\n",
  84. mii, strerror ( rc ) );
  85. return rc;
  86. }
  87. /* If reset is not complete, delay 1ms and retry */
  88. if ( bmcr & BMCR_RESET ) {
  89. mdelay ( 1 );
  90. continue;
  91. }
  92. /* Force autonegotation on again, in case it was
  93. * cleared by the reset.
  94. */
  95. if ( ( rc = mii_restart ( mii ) ) != 0 )
  96. return rc;
  97. DBGC ( mii, "MII %p reset after %dms\n", mii, i );
  98. return 0;
  99. }
  100. DBGC ( mii, "MII %p timed out waiting for reset\n", mii );
  101. return -ETIMEDOUT;
  102. }
  103. /**
  104. * Update link status via MII
  105. *
  106. * @v mii MII device
  107. * @v netdev Network device
  108. * @ret rc Return status code
  109. */
  110. int mii_check_link ( struct mii_device *mii, struct net_device *netdev ) {
  111. int bmsr;
  112. int link;
  113. int rc;
  114. /* Read BMSR */
  115. bmsr = mii_read ( mii, MII_BMSR );
  116. if ( bmsr < 0 ) {
  117. rc = bmsr;
  118. return rc;
  119. }
  120. /* Report link status */
  121. link = ( bmsr & BMSR_LSTATUS );
  122. DBGC ( mii, "MII %p link %s (BMSR %#04x)\n",
  123. mii, ( link ? "up" : "down" ), bmsr );
  124. if ( link ) {
  125. netdev_link_up ( netdev );
  126. } else {
  127. netdev_link_down ( netdev );
  128. }
  129. return 0;
  130. }
  131. /**
  132. * Find PHY address
  133. *
  134. * @v mii MII device
  135. * @ret rc Return status code
  136. */
  137. int mii_find ( struct mii_device *mii ) {
  138. unsigned int address;
  139. int id;
  140. /* Try all possible PHY addresses */
  141. for ( address = 0 ; address <= MII_MAX_PHY_ADDRESS ; address++ ) {
  142. mii->address = address;
  143. id = mii_read ( mii, MII_PHYSID1 );
  144. if ( ( id > 0x0000 ) && ( id < 0xffff ) ) {
  145. DBGC ( mii, "MII %p found PHY at address %d\n",
  146. mii, address );
  147. return 0;
  148. }
  149. }
  150. DBGC ( mii, "MII %p failed to find an address\n", mii );
  151. return -ENOENT;
  152. }