realtek.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * RealTek PHY drivers
  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
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU 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., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. *
  19. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  20. * author Andy Fleming
  21. *
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <phy.h>
  26. #define PHY_AUTONEGOTIATE_TIMEOUT 5000
  27. /* RTL8211B PHY Status Register */
  28. #define MIIM_RTL8211B_PHY_STATUS 0x11
  29. #define MIIM_RTL8211B_PHYSTAT_SPEED 0xc000
  30. #define MIIM_RTL8211B_PHYSTAT_GBIT 0x8000
  31. #define MIIM_RTL8211B_PHYSTAT_100 0x4000
  32. #define MIIM_RTL8211B_PHYSTAT_DUPLEX 0x2000
  33. #define MIIM_RTL8211B_PHYSTAT_SPDDONE 0x0800
  34. #define MIIM_RTL8211B_PHYSTAT_LINK 0x0400
  35. /* RealTek RTL8211B */
  36. static int rtl8211b_config(struct phy_device *phydev)
  37. {
  38. phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
  39. genphy_config_aneg(phydev);
  40. return 0;
  41. }
  42. static int rtl8211b_parse_status(struct phy_device *phydev)
  43. {
  44. unsigned int speed;
  45. unsigned int mii_reg;
  46. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211B_PHY_STATUS);
  47. if (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
  48. int i = 0;
  49. /* in case of timeout ->link is cleared */
  50. phydev->link = 1;
  51. puts("Waiting for PHY realtime link");
  52. while (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
  53. /* Timeout reached ? */
  54. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  55. puts(" TIMEOUT !\n");
  56. phydev->link = 0;
  57. break;
  58. }
  59. if ((i++ % 1000) == 0)
  60. putc('.');
  61. udelay(1000); /* 1 ms */
  62. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
  63. MIIM_RTL8211B_PHY_STATUS);
  64. }
  65. puts(" done\n");
  66. udelay(500000); /* another 500 ms (results in faster booting) */
  67. } else {
  68. if (mii_reg & MIIM_RTL8211B_PHYSTAT_LINK)
  69. phydev->link = 1;
  70. else
  71. phydev->link = 0;
  72. }
  73. if (mii_reg & MIIM_RTL8211B_PHYSTAT_DUPLEX)
  74. phydev->duplex = DUPLEX_FULL;
  75. else
  76. phydev->duplex = DUPLEX_HALF;
  77. speed = (mii_reg & MIIM_RTL8211B_PHYSTAT_SPEED);
  78. switch (speed) {
  79. case MIIM_RTL8211B_PHYSTAT_GBIT:
  80. phydev->speed = SPEED_1000;
  81. break;
  82. case MIIM_RTL8211B_PHYSTAT_100:
  83. phydev->speed = SPEED_100;
  84. break;
  85. default:
  86. phydev->speed = SPEED_10;
  87. }
  88. return 0;
  89. }
  90. static int rtl8211b_startup(struct phy_device *phydev)
  91. {
  92. /* Read the Status (2x to make sure link is right) */
  93. genphy_update_link(phydev);
  94. rtl8211b_parse_status(phydev);
  95. return 0;
  96. }
  97. static struct phy_driver RTL8211B_driver = {
  98. .name = "RealTek RTL8211B",
  99. .uid = 0x1cc910,
  100. .mask = 0xfffff0,
  101. .features = PHY_GBIT_FEATURES,
  102. .config = &rtl8211b_config,
  103. .startup = &rtl8211b_startup,
  104. .shutdown = &genphy_shutdown,
  105. };
  106. int phy_realtek_init(void)
  107. {
  108. phy_register(&RTL8211B_driver);
  109. return 0;
  110. }