Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 2 | /* |
| 3 | * RealTek PHY drivers |
| 4 | * |
Codrin Ciubotariu | de947a1 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 5 | * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc. |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 6 | * author Andy Fleming |
Karsten Merker | 10800de | 2016-03-21 20:29:07 +0100 | [diff] [blame] | 7 | * Copyright 2016 Karsten Merker <merker@debian.org> |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 8 | */ |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 9 | #include <common.h> |
oliver@schinagl.nl | ec1b26f | 2016-11-08 17:38:57 +0100 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 11 | #include <phy.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 12 | #include <linux/delay.h> |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 13 | |
oliver@schinagl.nl | 6a25090 | 2016-11-08 17:38:59 +0100 | [diff] [blame] | 14 | #define PHY_RTL8211x_FORCE_MASTER BIT(1) |
kevans@FreeBSD.org | 7c82401 | 2018-02-14 17:02:15 -0600 | [diff] [blame] | 15 | #define PHY_RTL8211E_PINE64_GIGABIT_FIX BIT(2) |
Carlo Caione | cf93d02 | 2019-01-24 08:54:37 +0000 | [diff] [blame] | 16 | #define PHY_RTL8211F_FORCE_EEE_RXC_ON BIT(3) |
Amit Singh Tomar | 4f21b2a | 2020-05-09 19:55:11 +0530 | [diff] [blame] | 17 | #define PHY_RTL8201F_S700_RMII_TIMINGS BIT(4) |
oliver@schinagl.nl | 6a25090 | 2016-11-08 17:38:59 +0100 | [diff] [blame] | 18 | |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 19 | #define PHY_AUTONEGOTIATE_TIMEOUT 5000 |
| 20 | |
Michael Haas | 0cf2b4e | 2016-03-25 18:22:50 +0100 | [diff] [blame] | 21 | /* RTL8211x 1000BASE-T Control Register */ |
oliver@schinagl.nl | ec1b26f | 2016-11-08 17:38:57 +0100 | [diff] [blame] | 22 | #define MIIM_RTL8211x_CTRL1000T_MSCE BIT(12); |
oliver@schinagl.nl | 1beb735 | 2016-11-08 17:38:58 +0100 | [diff] [blame] | 23 | #define MIIM_RTL8211x_CTRL1000T_MASTER BIT(11); |
Michael Haas | 0cf2b4e | 2016-03-25 18:22:50 +0100 | [diff] [blame] | 24 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 25 | /* RTL8211x PHY Status Register */ |
| 26 | #define MIIM_RTL8211x_PHY_STATUS 0x11 |
| 27 | #define MIIM_RTL8211x_PHYSTAT_SPEED 0xc000 |
| 28 | #define MIIM_RTL8211x_PHYSTAT_GBIT 0x8000 |
| 29 | #define MIIM_RTL8211x_PHYSTAT_100 0x4000 |
| 30 | #define MIIM_RTL8211x_PHYSTAT_DUPLEX 0x2000 |
| 31 | #define MIIM_RTL8211x_PHYSTAT_SPDDONE 0x0800 |
| 32 | #define MIIM_RTL8211x_PHYSTAT_LINK 0x0400 |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 33 | |
Codrin Ciubotariu | de947a1 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 34 | /* RTL8211x PHY Interrupt Enable Register */ |
| 35 | #define MIIM_RTL8211x_PHY_INER 0x12 |
| 36 | #define MIIM_RTL8211x_PHY_INTR_ENA 0x9f01 |
| 37 | #define MIIM_RTL8211x_PHY_INTR_DIS 0x0000 |
| 38 | |
| 39 | /* RTL8211x PHY Interrupt Status Register */ |
| 40 | #define MIIM_RTL8211x_PHY_INSR 0x13 |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 41 | |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 42 | /* RTL8211F PHY Status Register */ |
| 43 | #define MIIM_RTL8211F_PHY_STATUS 0x1a |
| 44 | #define MIIM_RTL8211F_AUTONEG_ENABLE 0x1000 |
| 45 | #define MIIM_RTL8211F_PHYSTAT_SPEED 0x0030 |
| 46 | #define MIIM_RTL8211F_PHYSTAT_GBIT 0x0020 |
| 47 | #define MIIM_RTL8211F_PHYSTAT_100 0x0010 |
| 48 | #define MIIM_RTL8211F_PHYSTAT_DUPLEX 0x0008 |
| 49 | #define MIIM_RTL8211F_PHYSTAT_SPDDONE 0x0800 |
| 50 | #define MIIM_RTL8211F_PHYSTAT_LINK 0x0004 |
| 51 | |
kevans@FreeBSD.org | 7c82401 | 2018-02-14 17:02:15 -0600 | [diff] [blame] | 52 | #define MIIM_RTL8211E_CONFREG 0x1c |
| 53 | #define MIIM_RTL8211E_CONFREG_TXD 0x0002 |
| 54 | #define MIIM_RTL8211E_CONFREG_RXD 0x0004 |
| 55 | #define MIIM_RTL8211E_CONFREG_MAGIC 0xb400 /* Undocumented */ |
| 56 | |
| 57 | #define MIIM_RTL8211E_EXT_PAGE_SELECT 0x1e |
| 58 | |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 59 | #define MIIM_RTL8211F_PAGE_SELECT 0x1f |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 60 | #define MIIM_RTL8211F_TX_DELAY 0x100 |
Fugang Duan | 030ccaa | 2020-05-03 22:41:16 +0800 | [diff] [blame] | 61 | #define MIIM_RTL8211F_RX_DELAY 0x8 |
Shengzhou Liu | 5487fc6 | 2015-05-21 18:07:35 +0800 | [diff] [blame] | 62 | #define MIIM_RTL8211F_LCR 0x10 |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 63 | |
Amit Singh Tomar | 4f21b2a | 2020-05-09 19:55:11 +0530 | [diff] [blame] | 64 | #define RTL8201F_RMSR 0x10 |
| 65 | |
| 66 | #define RMSR_RX_TIMING_SHIFT BIT(2) |
| 67 | #define RMSR_RX_TIMING_MASK GENMASK(7, 4) |
| 68 | #define RMSR_RX_TIMING_VAL 0x4 |
| 69 | #define RMSR_TX_TIMING_SHIFT BIT(3) |
| 70 | #define RMSR_TX_TIMING_MASK GENMASK(11, 8) |
| 71 | #define RMSR_TX_TIMING_VAL 0x5 |
| 72 | |
Carlo Caione | b8d167e | 2019-01-16 11:34:50 +0000 | [diff] [blame] | 73 | static int rtl8211f_phy_extread(struct phy_device *phydev, int addr, |
| 74 | int devaddr, int regnum) |
| 75 | { |
| 76 | int oldpage = phy_read(phydev, MDIO_DEVAD_NONE, |
| 77 | MIIM_RTL8211F_PAGE_SELECT); |
| 78 | int val; |
| 79 | |
| 80 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, devaddr); |
| 81 | val = phy_read(phydev, MDIO_DEVAD_NONE, regnum); |
| 82 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, oldpage); |
| 83 | |
| 84 | return val; |
| 85 | } |
| 86 | |
| 87 | static int rtl8211f_phy_extwrite(struct phy_device *phydev, int addr, |
| 88 | int devaddr, int regnum, u16 val) |
| 89 | { |
| 90 | int oldpage = phy_read(phydev, MDIO_DEVAD_NONE, |
| 91 | MIIM_RTL8211F_PAGE_SELECT); |
| 92 | |
| 93 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, devaddr); |
| 94 | phy_write(phydev, MDIO_DEVAD_NONE, regnum, val); |
| 95 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, oldpage); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
oliver@schinagl.nl | 6a25090 | 2016-11-08 17:38:59 +0100 | [diff] [blame] | 100 | static int rtl8211b_probe(struct phy_device *phydev) |
| 101 | { |
| 102 | #ifdef CONFIG_RTL8211X_PHY_FORCE_MASTER |
| 103 | phydev->flags |= PHY_RTL8211x_FORCE_MASTER; |
| 104 | #endif |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
kevans@FreeBSD.org | 7c82401 | 2018-02-14 17:02:15 -0600 | [diff] [blame] | 109 | static int rtl8211e_probe(struct phy_device *phydev) |
| 110 | { |
| 111 | #ifdef CONFIG_RTL8211E_PINE64_GIGABIT_FIX |
| 112 | phydev->flags |= PHY_RTL8211E_PINE64_GIGABIT_FIX; |
| 113 | #endif |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Carlo Caione | cf93d02 | 2019-01-24 08:54:37 +0000 | [diff] [blame] | 118 | static int rtl8211f_probe(struct phy_device *phydev) |
| 119 | { |
| 120 | #ifdef CONFIG_RTL8211F_PHY_FORCE_EEE_RXC_ON |
| 121 | phydev->flags |= PHY_RTL8211F_FORCE_EEE_RXC_ON; |
| 122 | #endif |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Amit Singh Tomar | 4f21b2a | 2020-05-09 19:55:11 +0530 | [diff] [blame] | 127 | static int rtl8210f_probe(struct phy_device *phydev) |
| 128 | { |
| 129 | #ifdef CONFIG_RTL8201F_PHY_S700_RMII_TIMINGS |
| 130 | phydev->flags |= PHY_RTL8201F_S700_RMII_TIMINGS; |
| 131 | #endif |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 136 | /* RealTek RTL8211x */ |
| 137 | static int rtl8211x_config(struct phy_device *phydev) |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 138 | { |
| 139 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); |
| 140 | |
Codrin Ciubotariu | de947a1 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 141 | /* mask interrupt at init; if the interrupt is |
| 142 | * needed indeed, it should be explicitly enabled |
| 143 | */ |
| 144 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER, |
| 145 | MIIM_RTL8211x_PHY_INTR_DIS); |
oliver@schinagl.nl | 6a25090 | 2016-11-08 17:38:59 +0100 | [diff] [blame] | 146 | |
| 147 | if (phydev->flags & PHY_RTL8211x_FORCE_MASTER) { |
| 148 | unsigned int reg; |
| 149 | |
| 150 | reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); |
| 151 | /* force manual master/slave configuration */ |
| 152 | reg |= MIIM_RTL8211x_CTRL1000T_MSCE; |
| 153 | /* force master mode */ |
| 154 | reg |= MIIM_RTL8211x_CTRL1000T_MASTER; |
| 155 | phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, reg); |
| 156 | } |
kevans@FreeBSD.org | 7c82401 | 2018-02-14 17:02:15 -0600 | [diff] [blame] | 157 | if (phydev->flags & PHY_RTL8211E_PINE64_GIGABIT_FIX) { |
| 158 | unsigned int reg; |
| 159 | |
| 160 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, |
| 161 | 7); |
| 162 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 163 | MIIM_RTL8211E_EXT_PAGE_SELECT, 0xa4); |
| 164 | reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG); |
| 165 | /* Ensure both internal delays are turned off */ |
| 166 | reg &= ~(MIIM_RTL8211E_CONFREG_TXD | MIIM_RTL8211E_CONFREG_RXD); |
| 167 | /* Flip the magic undocumented bits */ |
| 168 | reg |= MIIM_RTL8211E_CONFREG_MAGIC; |
| 169 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG, reg); |
| 170 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, |
| 171 | 0); |
| 172 | } |
Codrin Ciubotariu | de947a1 | 2015-02-13 14:47:58 +0200 | [diff] [blame] | 173 | /* read interrupt status just to clear it */ |
| 174 | phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER); |
| 175 | |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 176 | genphy_config_aneg(phydev); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Amit Singh Tomar | 7e7c105 | 2020-05-09 19:55:10 +0530 | [diff] [blame] | 181 | /* RealTek RTL8201F */ |
| 182 | static int rtl8201f_config(struct phy_device *phydev) |
| 183 | { |
Amit Singh Tomar | 4f21b2a | 2020-05-09 19:55:11 +0530 | [diff] [blame] | 184 | unsigned int reg; |
| 185 | |
| 186 | if (phydev->flags & PHY_RTL8201F_S700_RMII_TIMINGS) { |
| 187 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, |
| 188 | 7); |
| 189 | reg = phy_read(phydev, MDIO_DEVAD_NONE, RTL8201F_RMSR); |
| 190 | reg &= ~(RMSR_RX_TIMING_MASK | RMSR_TX_TIMING_MASK); |
| 191 | /* Set the needed Rx/Tx Timings for proper PHY operation */ |
| 192 | reg |= (RMSR_RX_TIMING_VAL << RMSR_RX_TIMING_SHIFT) |
| 193 | | (RMSR_TX_TIMING_VAL << RMSR_TX_TIMING_SHIFT); |
| 194 | phy_write(phydev, MDIO_DEVAD_NONE, RTL8201F_RMSR, reg); |
| 195 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, |
| 196 | 0); |
| 197 | } |
| 198 | |
Amit Singh Tomar | 7e7c105 | 2020-05-09 19:55:10 +0530 | [diff] [blame] | 199 | genphy_config_aneg(phydev); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 204 | static int rtl8211f_config(struct phy_device *phydev) |
| 205 | { |
| 206 | u16 reg; |
| 207 | |
Carlo Caione | cf93d02 | 2019-01-24 08:54:37 +0000 | [diff] [blame] | 208 | if (phydev->flags & PHY_RTL8211F_FORCE_EEE_RXC_ON) { |
| 209 | unsigned int reg; |
| 210 | |
| 211 | reg = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 212 | reg &= ~MDIO_PCS_CTRL1_CLKSTOP_EN; |
| 213 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, reg); |
| 214 | } |
| 215 | |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 216 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET); |
| 217 | |
Madalin Bucur | 83ba7aa | 2017-08-18 11:35:24 +0300 | [diff] [blame] | 218 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 219 | MIIM_RTL8211F_PAGE_SELECT, 0xd08); |
| 220 | reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11); |
| 221 | |
| 222 | /* enable TX-delay for rgmii-id and rgmii-txid, otherwise disable it */ |
| 223 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 224 | phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 225 | reg |= MIIM_RTL8211F_TX_DELAY; |
Madalin Bucur | 83ba7aa | 2017-08-18 11:35:24 +0300 | [diff] [blame] | 226 | else |
| 227 | reg &= ~MIIM_RTL8211F_TX_DELAY; |
| 228 | |
| 229 | phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg); |
Fugang Duan | 030ccaa | 2020-05-03 22:41:16 +0800 | [diff] [blame] | 230 | |
| 231 | /* enable RX-delay for rgmii-id and rgmii-rxid, otherwise disable it */ |
| 232 | reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x15); |
| 233 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 234 | phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 235 | reg |= MIIM_RTL8211F_RX_DELAY; |
| 236 | else |
| 237 | reg &= ~MIIM_RTL8211F_RX_DELAY; |
| 238 | phy_write(phydev, MDIO_DEVAD_NONE, 0x15, reg); |
| 239 | |
Madalin Bucur | 83ba7aa | 2017-08-18 11:35:24 +0300 | [diff] [blame] | 240 | /* restore to default page 0 */ |
| 241 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 242 | MIIM_RTL8211F_PAGE_SELECT, 0x0); |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 243 | |
Shengzhou Liu | 5487fc6 | 2015-05-21 18:07:35 +0800 | [diff] [blame] | 244 | /* Set green LED for Link, yellow LED for Active */ |
| 245 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 246 | MIIM_RTL8211F_PAGE_SELECT, 0xd04); |
| 247 | phy_write(phydev, MDIO_DEVAD_NONE, 0x10, 0x617f); |
| 248 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 249 | MIIM_RTL8211F_PAGE_SELECT, 0x0); |
| 250 | |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 251 | genphy_config_aneg(phydev); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 256 | static int rtl8211x_parse_status(struct phy_device *phydev) |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 257 | { |
| 258 | unsigned int speed; |
| 259 | unsigned int mii_reg; |
| 260 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 261 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 262 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 263 | if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 264 | int i = 0; |
| 265 | |
| 266 | /* in case of timeout ->link is cleared */ |
| 267 | phydev->link = 1; |
| 268 | puts("Waiting for PHY realtime link"); |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 269 | while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) { |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 270 | /* Timeout reached ? */ |
| 271 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 272 | puts(" TIMEOUT !\n"); |
| 273 | phydev->link = 0; |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if ((i++ % 1000) == 0) |
| 278 | putc('.'); |
| 279 | udelay(1000); /* 1 ms */ |
| 280 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 281 | MIIM_RTL8211x_PHY_STATUS); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 282 | } |
| 283 | puts(" done\n"); |
| 284 | udelay(500000); /* another 500 ms (results in faster booting) */ |
| 285 | } else { |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 286 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK) |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 287 | phydev->link = 1; |
| 288 | else |
| 289 | phydev->link = 0; |
| 290 | } |
| 291 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 292 | if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX) |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 293 | phydev->duplex = DUPLEX_FULL; |
| 294 | else |
| 295 | phydev->duplex = DUPLEX_HALF; |
| 296 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 297 | speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 298 | |
| 299 | switch (speed) { |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 300 | case MIIM_RTL8211x_PHYSTAT_GBIT: |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 301 | phydev->speed = SPEED_1000; |
| 302 | break; |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 303 | case MIIM_RTL8211x_PHYSTAT_100: |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 304 | phydev->speed = SPEED_100; |
| 305 | break; |
| 306 | default: |
| 307 | phydev->speed = SPEED_10; |
| 308 | } |
| 309 | |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int rtl8211f_parse_status(struct phy_device *phydev) |
| 314 | { |
| 315 | unsigned int speed; |
| 316 | unsigned int mii_reg; |
| 317 | int i = 0; |
| 318 | |
| 319 | phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43); |
| 320 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS); |
| 321 | |
| 322 | phydev->link = 1; |
| 323 | while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) { |
| 324 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 325 | puts(" TIMEOUT !\n"); |
| 326 | phydev->link = 0; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | if ((i++ % 1000) == 0) |
| 331 | putc('.'); |
| 332 | udelay(1000); |
| 333 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
| 334 | MIIM_RTL8211F_PHY_STATUS); |
| 335 | } |
| 336 | |
| 337 | if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX) |
| 338 | phydev->duplex = DUPLEX_FULL; |
| 339 | else |
| 340 | phydev->duplex = DUPLEX_HALF; |
| 341 | |
| 342 | speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED); |
| 343 | |
| 344 | switch (speed) { |
| 345 | case MIIM_RTL8211F_PHYSTAT_GBIT: |
| 346 | phydev->speed = SPEED_1000; |
| 347 | break; |
| 348 | case MIIM_RTL8211F_PHYSTAT_100: |
| 349 | phydev->speed = SPEED_100; |
| 350 | break; |
| 351 | default: |
| 352 | phydev->speed = SPEED_10; |
| 353 | } |
| 354 | |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 358 | static int rtl8211x_startup(struct phy_device *phydev) |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 359 | { |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 360 | int ret; |
| 361 | |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 362 | /* Read the Status (2x to make sure link is right) */ |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 363 | ret = genphy_update_link(phydev); |
| 364 | if (ret) |
| 365 | return ret; |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 366 | |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 367 | return rtl8211x_parse_status(phydev); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Michal Simek | bdf0b72 | 2016-02-13 10:31:32 +0100 | [diff] [blame] | 370 | static int rtl8211e_startup(struct phy_device *phydev) |
| 371 | { |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 372 | int ret; |
| 373 | |
| 374 | ret = genphy_update_link(phydev); |
| 375 | if (ret) |
| 376 | return ret; |
Michal Simek | bdf0b72 | 2016-02-13 10:31:32 +0100 | [diff] [blame] | 377 | |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 378 | return genphy_parse_link(phydev); |
Michal Simek | bdf0b72 | 2016-02-13 10:31:32 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 381 | static int rtl8211f_startup(struct phy_device *phydev) |
| 382 | { |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 383 | int ret; |
| 384 | |
| 385 | /* Read the Status (2x to make sure link is right) */ |
| 386 | ret = genphy_update_link(phydev); |
| 387 | if (ret) |
| 388 | return ret; |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 389 | /* Read the Status (2x to make sure link is right) */ |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 390 | |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 391 | return rtl8211f_parse_status(phydev); |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 394 | /* Support for RTL8211B PHY */ |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 395 | static struct phy_driver RTL8211B_driver = { |
| 396 | .name = "RealTek RTL8211B", |
Karsten Merker | 10800de | 2016-03-21 20:29:07 +0100 | [diff] [blame] | 397 | .uid = 0x1cc912, |
Bhupesh Sharma | ad4cd95 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 398 | .mask = 0xffffff, |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 399 | .features = PHY_GBIT_FEATURES, |
oliver@schinagl.nl | 6a25090 | 2016-11-08 17:38:59 +0100 | [diff] [blame] | 400 | .probe = &rtl8211b_probe, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 401 | .config = &rtl8211x_config, |
| 402 | .startup = &rtl8211x_startup, |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 403 | .shutdown = &genphy_shutdown, |
| 404 | }; |
| 405 | |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 406 | /* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */ |
| 407 | static struct phy_driver RTL8211E_driver = { |
| 408 | .name = "RealTek RTL8211E", |
| 409 | .uid = 0x1cc915, |
Bhupesh Sharma | ad4cd95 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 410 | .mask = 0xffffff, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 411 | .features = PHY_GBIT_FEATURES, |
kevans@FreeBSD.org | 7c82401 | 2018-02-14 17:02:15 -0600 | [diff] [blame] | 412 | .probe = &rtl8211e_probe, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 413 | .config = &rtl8211x_config, |
Michal Simek | bdf0b72 | 2016-02-13 10:31:32 +0100 | [diff] [blame] | 414 | .startup = &rtl8211e_startup, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 415 | .shutdown = &genphy_shutdown, |
| 416 | }; |
| 417 | |
| 418 | /* Support for RTL8211DN PHY */ |
| 419 | static struct phy_driver RTL8211DN_driver = { |
| 420 | .name = "RealTek RTL8211DN", |
| 421 | .uid = 0x1cc914, |
Bhupesh Sharma | ad4cd95 | 2013-09-01 04:40:52 +0530 | [diff] [blame] | 422 | .mask = 0xffffff, |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 423 | .features = PHY_GBIT_FEATURES, |
| 424 | .config = &rtl8211x_config, |
| 425 | .startup = &rtl8211x_startup, |
| 426 | .shutdown = &genphy_shutdown, |
| 427 | }; |
| 428 | |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 429 | /* Support for RTL8211F PHY */ |
| 430 | static struct phy_driver RTL8211F_driver = { |
| 431 | .name = "RealTek RTL8211F", |
| 432 | .uid = 0x1cc916, |
| 433 | .mask = 0xffffff, |
| 434 | .features = PHY_GBIT_FEATURES, |
Carlo Caione | cf93d02 | 2019-01-24 08:54:37 +0000 | [diff] [blame] | 435 | .probe = &rtl8211f_probe, |
Shengzhou Liu | 6750089 | 2015-04-24 16:57:17 +0800 | [diff] [blame] | 436 | .config = &rtl8211f_config, |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 437 | .startup = &rtl8211f_startup, |
| 438 | .shutdown = &genphy_shutdown, |
Carlo Caione | b8d167e | 2019-01-16 11:34:50 +0000 | [diff] [blame] | 439 | .readext = &rtl8211f_phy_extread, |
| 440 | .writeext = &rtl8211f_phy_extwrite, |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 441 | }; |
| 442 | |
Amit Singh Tomar | 7e7c105 | 2020-05-09 19:55:10 +0530 | [diff] [blame] | 443 | /* Support for RTL8201F PHY */ |
| 444 | static struct phy_driver RTL8201F_driver = { |
| 445 | .name = "RealTek RTL8201F 10/100Mbps Ethernet", |
| 446 | .uid = 0x1cc816, |
| 447 | .mask = 0xffffff, |
| 448 | .features = PHY_BASIC_FEATURES, |
Amit Singh Tomar | 4f21b2a | 2020-05-09 19:55:11 +0530 | [diff] [blame] | 449 | .probe = &rtl8210f_probe, |
Amit Singh Tomar | 7e7c105 | 2020-05-09 19:55:10 +0530 | [diff] [blame] | 450 | .config = &rtl8201f_config, |
| 451 | .startup = &rtl8211e_startup, |
| 452 | .shutdown = &genphy_shutdown, |
| 453 | }; |
| 454 | |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 455 | int phy_realtek_init(void) |
| 456 | { |
| 457 | phy_register(&RTL8211B_driver); |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 458 | phy_register(&RTL8211E_driver); |
Shengzhou Liu | 210f17f | 2015-03-12 18:54:59 +0800 | [diff] [blame] | 459 | phy_register(&RTL8211F_driver); |
Bhupesh Sharma | 85eb780 | 2013-07-18 13:58:20 +0530 | [diff] [blame] | 460 | phy_register(&RTL8211DN_driver); |
Amit Singh Tomar | 7e7c105 | 2020-05-09 19:55:10 +0530 | [diff] [blame] | 461 | phy_register(&RTL8201F_driver); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |