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