4 * SPDX-License-Identifier: GPL-2.0+
6 * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc.
13 #define PHY_AUTONEGOTIATE_TIMEOUT 5000
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
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
29 /* RTL8211x PHY Interrupt Status Register */
30 #define MIIM_RTL8211x_PHY_INSR 0x13
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
42 #define MIIM_RTL8211F_PAGE_SELECT 0x1f
43 #define MIIM_RTL8211F_TX_DELAY 0x100
45 /* RealTek RTL8211x */
46 static int rtl8211x_config(struct phy_device *phydev)
48 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
50 /* mask interrupt at init; if the interrupt is
51 * needed indeed, it should be explicitly enabled
53 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER,
54 MIIM_RTL8211x_PHY_INTR_DIS);
56 /* read interrupt status just to clear it */
57 phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER);
59 genphy_config_aneg(phydev);
64 static int rtl8211f_config(struct phy_device *phydev)
68 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
70 if (phydev->interface == PHY_INTERFACE_MODE_RGMII) {
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);
82 genphy_config_aneg(phydev);
87 static int rtl8211x_parse_status(struct phy_device *phydev)
92 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
94 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
97 /* in case of timeout ->link is cleared */
99 puts("Waiting for PHY realtime link");
100 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
101 /* Timeout reached ? */
102 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
103 puts(" TIMEOUT !\n");
108 if ((i++ % 1000) == 0)
110 udelay(1000); /* 1 ms */
111 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
112 MIIM_RTL8211x_PHY_STATUS);
115 udelay(500000); /* another 500 ms (results in faster booting) */
117 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
123 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
124 phydev->duplex = DUPLEX_FULL;
126 phydev->duplex = DUPLEX_HALF;
128 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
131 case MIIM_RTL8211x_PHYSTAT_GBIT:
132 phydev->speed = SPEED_1000;
134 case MIIM_RTL8211x_PHYSTAT_100:
135 phydev->speed = SPEED_100;
138 phydev->speed = SPEED_10;
144 static int rtl8211f_parse_status(struct phy_device *phydev)
147 unsigned int mii_reg;
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);
154 while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) {
155 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
156 puts(" TIMEOUT !\n");
161 if ((i++ % 1000) == 0)
164 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
165 MIIM_RTL8211F_PHY_STATUS);
168 if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX)
169 phydev->duplex = DUPLEX_FULL;
171 phydev->duplex = DUPLEX_HALF;
173 speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED);
176 case MIIM_RTL8211F_PHYSTAT_GBIT:
177 phydev->speed = SPEED_1000;
179 case MIIM_RTL8211F_PHYSTAT_100:
180 phydev->speed = SPEED_100;
183 phydev->speed = SPEED_10;
189 static int rtl8211x_startup(struct phy_device *phydev)
191 /* Read the Status (2x to make sure link is right) */
192 genphy_update_link(phydev);
193 rtl8211x_parse_status(phydev);
198 static int rtl8211f_startup(struct phy_device *phydev)
200 /* Read the Status (2x to make sure link is right) */
201 genphy_update_link(phydev);
202 rtl8211f_parse_status(phydev);
207 /* Support for RTL8211B PHY */
208 static struct phy_driver RTL8211B_driver = {
209 .name = "RealTek RTL8211B",
212 .features = PHY_GBIT_FEATURES,
213 .config = &rtl8211x_config,
214 .startup = &rtl8211x_startup,
215 .shutdown = &genphy_shutdown,
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",
223 .features = PHY_GBIT_FEATURES,
224 .config = &rtl8211x_config,
225 .startup = &rtl8211x_startup,
226 .shutdown = &genphy_shutdown,
229 /* Support for RTL8211DN PHY */
230 static struct phy_driver RTL8211DN_driver = {
231 .name = "RealTek RTL8211DN",
234 .features = PHY_GBIT_FEATURES,
235 .config = &rtl8211x_config,
236 .startup = &rtl8211x_startup,
237 .shutdown = &genphy_shutdown,
240 /* Support for RTL8211F PHY */
241 static struct phy_driver RTL8211F_driver = {
242 .name = "RealTek RTL8211F",
245 .features = PHY_GBIT_FEATURES,
246 .config = &rtl8211f_config,
247 .startup = &rtl8211f_startup,
248 .shutdown = &genphy_shutdown,
251 int phy_realtek_init(void)
253 phy_register(&RTL8211B_driver);
254 phy_register(&RTL8211E_driver);
255 phy_register(&RTL8211F_driver);
256 phy_register(&RTL8211DN_driver);