Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 1 | /* |
| 2 | * RealTek PHY drivers |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 5 | * |
| 6 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
| 7 | * author Andy Fleming |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 8 | */ |
| 9 | #include <config.h> |
| 10 | #include <common.h> |
| 11 | #include <phy.h> |
| 12 | |
| 13 | #define PHY_AUTONEGOTIATE_TIMEOUT 5000 |
| 14 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 15 | /* RTL8211x PHY Status Register */ |
| 16 | #define MIIM_RTL8211x_PHY_STATUS 0x11 |
| 17 | #define MIIM_RTL8211x_PHYSTAT_SPEED 0xc000 |
| 18 | #define MIIM_RTL8211x_PHYSTAT_GBIT 0x8000 |
| 19 | #define MIIM_RTL8211x_PHYSTAT_100 0x4000 |
| 20 | #define MIIM_RTL8211x_PHYSTAT_DUPLEX 0x2000 |
| 21 | #define MIIM_RTL8211x_PHYSTAT_SPDDONE 0x0800 |
| 22 | #define MIIM_RTL8211x_PHYSTAT_LINK 0x0400 |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 23 | |
| 24 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 25 | /* RealTek RTL8211x */ |
| 26 | static int rtl8211x_config(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 27 | { |
| 28 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); |
| 29 | |
| 30 | genphy_config_aneg(phydev); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 35 | static int rtl8211x_parse_status(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 36 | { |
| 37 | unsigned int speed; |
| 38 | unsigned int mii_reg; |
| 39 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 40 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 41 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 42 | if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 43 | int i = 0; |
| 44 | |
| 45 | /* in case of timeout ->link is cleared */ |
| 46 | phydev->link = 1; |
| 47 | puts("Waiting for PHY realtime link"); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 48 | while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 49 | /* Timeout reached ? */ |
| 50 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 51 | puts(" TIMEOUT !\n"); |
| 52 | phydev->link = 0; |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | if ((i++ % 1000) == 0) |
| 57 | putc('.'); |
| 58 | udelay(1000); /* 1 ms */ |
| 59 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 60 | MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 61 | } |
| 62 | puts(" done\n"); |
| 63 | udelay(500000); /* another 500 ms (results in faster booting) */ |
| 64 | } else { |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 65 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 66 | phydev->link = 1; |
| 67 | else |
| 68 | phydev->link = 0; |
| 69 | } |
| 70 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 71 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 72 | phydev->duplex = DUPLEX_FULL; |
| 73 | else |
| 74 | phydev->duplex = DUPLEX_HALF; |
| 75 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 76 | speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 77 | |
| 78 | switch (speed) { |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 79 | case MIIM_RTL8211x_PHYSTAT_GBIT: |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 80 | phydev->speed = SPEED_1000; |
| 81 | break; |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 82 | case MIIM_RTL8211x_PHYSTAT_100: |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 83 | phydev->speed = SPEED_100; |
| 84 | break; |
| 85 | default: |
| 86 | phydev->speed = SPEED_10; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 92 | static int rtl8211x_startup(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 93 | { |
| 94 | /* Read the Status (2x to make sure link is right) */ |
| 95 | genphy_update_link(phydev); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 96 | rtl8211x_parse_status(phydev); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 101 | /* Support for RTL8211B PHY */ |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 102 | static struct phy_driver RTL8211B_driver = { |
| 103 | .name = "RealTek RTL8211B", |
| 104 | .uid = 0x1cc910, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 105 | .mask = 0xffffff, |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 106 | .features = PHY_GBIT_FEATURES, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 107 | .config = &rtl8211x_config, |
| 108 | .startup = &rtl8211x_startup, |
| 109 | .shutdown = &genphy_shutdown, |
| 110 | }; |
| 111 | |
| 112 | /* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */ |
| 113 | static struct phy_driver RTL8211E_driver = { |
| 114 | .name = "RealTek RTL8211E", |
| 115 | .uid = 0x1cc915, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 116 | .mask = 0xffffff, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 117 | .features = PHY_GBIT_FEATURES, |
| 118 | .config = &rtl8211x_config, |
| 119 | .startup = &rtl8211x_startup, |
| 120 | .shutdown = &genphy_shutdown, |
| 121 | }; |
| 122 | |
| 123 | /* Support for RTL8211DN PHY */ |
| 124 | static struct phy_driver RTL8211DN_driver = { |
| 125 | .name = "RealTek RTL8211DN", |
| 126 | .uid = 0x1cc914, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 127 | .mask = 0xffffff, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 128 | .features = PHY_GBIT_FEATURES, |
| 129 | .config = &rtl8211x_config, |
| 130 | .startup = &rtl8211x_startup, |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 131 | .shutdown = &genphy_shutdown, |
| 132 | }; |
| 133 | |
| 134 | int phy_realtek_init(void) |
| 135 | { |
| 136 | phy_register(&RTL8211B_driver); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 137 | phy_register(&RTL8211E_driver); |
| 138 | phy_register(&RTL8211DN_driver); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |