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 | * |
Codrin Ciubotariu | 3cee138 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 6 | * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc. |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 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 | |
Codrin Ciubotariu | 3cee138 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 24 | /* RTL8211x PHY Interrupt Enable Register */ |
| 25 | #define MIIM_RTL8211x_PHY_INER 0x12 |
| 26 | #define MIIM_RTL8211x_PHY_INTR_ENA 0x9f01 |
| 27 | #define MIIM_RTL8211x_PHY_INTR_DIS 0x0000 |
| 28 | |
| 29 | /* RTL8211x PHY Interrupt Status Register */ |
| 30 | #define MIIM_RTL8211x_PHY_INSR 0x13 |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 31 | |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 32 | /* RTL8211F PHY Status Register */ |
| 33 | #define MIIM_RTL8211F_PHY_STATUS 0x1a |
| 34 | #define MIIM_RTL8211F_AUTONEG_ENABLE 0x1000 |
| 35 | #define MIIM_RTL8211F_PHYSTAT_SPEED 0x0030 |
| 36 | #define MIIM_RTL8211F_PHYSTAT_GBIT 0x0020 |
| 37 | #define MIIM_RTL8211F_PHYSTAT_100 0x0010 |
| 38 | #define MIIM_RTL8211F_PHYSTAT_DUPLEX 0x0008 |
| 39 | #define MIIM_RTL8211F_PHYSTAT_SPDDONE 0x0800 |
| 40 | #define MIIM_RTL8211F_PHYSTAT_LINK 0x0004 |
| 41 | |
| 42 | #define MIIM_RTL8211F_PAGE_SELECT 0x1f |
Shengzhou Liu | 793ea94 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 43 | #define MIIM_RTL8211F_TX_DELAY 0x100 |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 44 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 45 | /* RealTek RTL8211x */ |
| 46 | static int rtl8211x_config(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 47 | { |
| 48 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); |
| 49 | |
Codrin Ciubotariu | 3cee138 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 50 | /* mask interrupt at init; if the interrupt is |
| 51 | * needed indeed, it should be explicitly enabled |
| 52 | */ |
| 53 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER, |
| 54 | MIIM_RTL8211x_PHY_INTR_DIS); |
| 55 | |
| 56 | /* read interrupt status just to clear it */ |
| 57 | phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER); |
| 58 | |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 59 | genphy_config_aneg(phydev); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Shengzhou Liu | 793ea94 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 64 | static int rtl8211f_config(struct phy_device *phydev) |
| 65 | { |
| 66 | u16 reg; |
| 67 | |
| 68 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); |
| 69 | |
| 70 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII) { |
| 71 | /* enable TXDLY */ |
| 72 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 73 | MIIM_RTL8211F_PAGE_SELECT, 0xd08); |
| 74 | reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11); |
| 75 | reg |= MIIM_RTL8211F_TX_DELAY; |
| 76 | phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg); |
| 77 | /* restore to default page 0 */ |
| 78 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 79 | MIIM_RTL8211F_PAGE_SELECT, 0x0); |
| 80 | } |
| 81 | |
| 82 | genphy_config_aneg(phydev); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 87 | static int rtl8211x_parse_status(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 88 | { |
| 89 | unsigned int speed; |
| 90 | unsigned int mii_reg; |
| 91 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 92 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 93 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 94 | if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 95 | int i = 0; |
| 96 | |
| 97 | /* in case of timeout ->link is cleared */ |
| 98 | phydev->link = 1; |
| 99 | puts("Waiting for PHY realtime link"); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 100 | while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 101 | /* Timeout reached ? */ |
| 102 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 103 | puts(" TIMEOUT !\n"); |
| 104 | phydev->link = 0; |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | if ((i++ % 1000) == 0) |
| 109 | putc('.'); |
| 110 | udelay(1000); /* 1 ms */ |
| 111 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 112 | MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 113 | } |
| 114 | puts(" done\n"); |
| 115 | udelay(500000); /* another 500 ms (results in faster booting) */ |
| 116 | } else { |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 117 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 118 | phydev->link = 1; |
| 119 | else |
| 120 | phydev->link = 0; |
| 121 | } |
| 122 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 123 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 124 | phydev->duplex = DUPLEX_FULL; |
| 125 | else |
| 126 | phydev->duplex = DUPLEX_HALF; |
| 127 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 128 | speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 129 | |
| 130 | switch (speed) { |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 131 | case MIIM_RTL8211x_PHYSTAT_GBIT: |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 132 | phydev->speed = SPEED_1000; |
| 133 | break; |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 134 | case MIIM_RTL8211x_PHYSTAT_100: |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 135 | phydev->speed = SPEED_100; |
| 136 | break; |
| 137 | default: |
| 138 | phydev->speed = SPEED_10; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 144 | static int rtl8211f_parse_status(struct phy_device *phydev) |
| 145 | { |
| 146 | unsigned int speed; |
| 147 | unsigned int mii_reg; |
| 148 | int i = 0; |
| 149 | |
| 150 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43); |
| 151 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS); |
| 152 | |
| 153 | phydev->link = 1; |
| 154 | while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) { |
| 155 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 156 | puts(" TIMEOUT !\n"); |
| 157 | phydev->link = 0; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | if ((i++ % 1000) == 0) |
| 162 | putc('.'); |
| 163 | udelay(1000); |
| 164 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
| 165 | MIIM_RTL8211F_PHY_STATUS); |
| 166 | } |
| 167 | |
| 168 | if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX) |
| 169 | phydev->duplex = DUPLEX_FULL; |
| 170 | else |
| 171 | phydev->duplex = DUPLEX_HALF; |
| 172 | |
| 173 | speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED); |
| 174 | |
| 175 | switch (speed) { |
| 176 | case MIIM_RTL8211F_PHYSTAT_GBIT: |
| 177 | phydev->speed = SPEED_1000; |
| 178 | break; |
| 179 | case MIIM_RTL8211F_PHYSTAT_100: |
| 180 | phydev->speed = SPEED_100; |
| 181 | break; |
| 182 | default: |
| 183 | phydev->speed = SPEED_10; |
| 184 | } |
| 185 | |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 189 | static int rtl8211x_startup(struct phy_device *phydev) |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 190 | { |
| 191 | /* Read the Status (2x to make sure link is right) */ |
| 192 | genphy_update_link(phydev); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 193 | rtl8211x_parse_status(phydev); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 198 | static int rtl8211f_startup(struct phy_device *phydev) |
| 199 | { |
| 200 | /* Read the Status (2x to make sure link is right) */ |
| 201 | genphy_update_link(phydev); |
| 202 | rtl8211f_parse_status(phydev); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 207 | /* Support for RTL8211B PHY */ |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 208 | static struct phy_driver RTL8211B_driver = { |
| 209 | .name = "RealTek RTL8211B", |
| 210 | .uid = 0x1cc910, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 211 | .mask = 0xffffff, |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 212 | .features = PHY_GBIT_FEATURES, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 213 | .config = &rtl8211x_config, |
| 214 | .startup = &rtl8211x_startup, |
| 215 | .shutdown = &genphy_shutdown, |
| 216 | }; |
| 217 | |
| 218 | /* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */ |
| 219 | static struct phy_driver RTL8211E_driver = { |
| 220 | .name = "RealTek RTL8211E", |
| 221 | .uid = 0x1cc915, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 222 | .mask = 0xffffff, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 223 | .features = PHY_GBIT_FEATURES, |
| 224 | .config = &rtl8211x_config, |
| 225 | .startup = &rtl8211x_startup, |
| 226 | .shutdown = &genphy_shutdown, |
| 227 | }; |
| 228 | |
| 229 | /* Support for RTL8211DN PHY */ |
| 230 | static struct phy_driver RTL8211DN_driver = { |
| 231 | .name = "RealTek RTL8211DN", |
| 232 | .uid = 0x1cc914, |
Bhupesh Sharma | 4220504 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 233 | .mask = 0xffffff, |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 234 | .features = PHY_GBIT_FEATURES, |
| 235 | .config = &rtl8211x_config, |
| 236 | .startup = &rtl8211x_startup, |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 237 | .shutdown = &genphy_shutdown, |
| 238 | }; |
| 239 | |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 240 | /* Support for RTL8211F PHY */ |
| 241 | static struct phy_driver RTL8211F_driver = { |
| 242 | .name = "RealTek RTL8211F", |
| 243 | .uid = 0x1cc916, |
| 244 | .mask = 0xffffff, |
| 245 | .features = PHY_GBIT_FEATURES, |
Shengzhou Liu | 793ea94 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 246 | .config = &rtl8211f_config, |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 247 | .startup = &rtl8211f_startup, |
| 248 | .shutdown = &genphy_shutdown, |
| 249 | }; |
| 250 | |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 251 | int phy_realtek_init(void) |
| 252 | { |
| 253 | phy_register(&RTL8211B_driver); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 254 | phy_register(&RTL8211E_driver); |
Shengzhou Liu | 3d6af74 | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 255 | phy_register(&RTL8211F_driver); |
Bhupesh Sharma | c624d16 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 256 | phy_register(&RTL8211DN_driver); |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |