blob: 79452a8df325d17f89f378b99946214b5ec3e46a [file] [log] [blame]
Andy Fleming9082eea2011-04-07 21:56:05 -05001/*
2 * RealTek PHY drivers
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming9082eea2011-04-07 21:56:05 -05005 *
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +02006 * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc.
Andy Fleming9082eea2011-04-07 21:56:05 -05007 * author Andy Fleming
Andy Fleming9082eea2011-04-07 21:56:05 -05008 */
9#include <config.h>
10#include <common.h>
11#include <phy.h>
12
13#define PHY_AUTONEGOTIATE_TIMEOUT 5000
14
Bhupesh Sharmac624d162013-07-18 13:58:20 +053015/* 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 Fleming9082eea2011-04-07 21:56:05 -050023
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020024/* 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 Fleming9082eea2011-04-07 21:56:05 -050031
Shengzhou Liu3d6af742015-03-12 18:54:59 +080032/* 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 Liu793ea942015-04-24 16:57:17 +080043#define MIIM_RTL8211F_TX_DELAY 0x100
Shengzhou Liu3d6af742015-03-12 18:54:59 +080044
Bhupesh Sharmac624d162013-07-18 13:58:20 +053045/* RealTek RTL8211x */
46static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050047{
48 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
49
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020050 /* 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 Fleming9082eea2011-04-07 21:56:05 -050059 genphy_config_aneg(phydev);
60
61 return 0;
62}
63
Shengzhou Liu793ea942015-04-24 16:57:17 +080064static 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 Sharmac624d162013-07-18 13:58:20 +053087static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050088{
89 unsigned int speed;
90 unsigned int mii_reg;
91
Bhupesh Sharmac624d162013-07-18 13:58:20 +053092 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -050093
Bhupesh Sharmac624d162013-07-18 13:58:20 +053094 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -050095 int i = 0;
96
97 /* in case of timeout ->link is cleared */
98 phydev->link = 1;
99 puts("Waiting for PHY realtime link");
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530100 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500101 /* 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 Sharmac624d162013-07-18 13:58:20 +0530112 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500113 }
114 puts(" done\n");
115 udelay(500000); /* another 500 ms (results in faster booting) */
116 } else {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530117 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming9082eea2011-04-07 21:56:05 -0500118 phydev->link = 1;
119 else
120 phydev->link = 0;
121 }
122
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530123 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming9082eea2011-04-07 21:56:05 -0500124 phydev->duplex = DUPLEX_FULL;
125 else
126 phydev->duplex = DUPLEX_HALF;
127
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530128 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming9082eea2011-04-07 21:56:05 -0500129
130 switch (speed) {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530131 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming9082eea2011-04-07 21:56:05 -0500132 phydev->speed = SPEED_1000;
133 break;
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530134 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming9082eea2011-04-07 21:56:05 -0500135 phydev->speed = SPEED_100;
136 break;
137 default:
138 phydev->speed = SPEED_10;
139 }
140
141 return 0;
142}
143
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800144static 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 Liu3d6af742015-03-12 18:54:59 +0800186 return 0;
187}
188
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530189static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500190{
191 /* Read the Status (2x to make sure link is right) */
192 genphy_update_link(phydev);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530193 rtl8211x_parse_status(phydev);
Andy Fleming9082eea2011-04-07 21:56:05 -0500194
195 return 0;
196}
197
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800198static 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 Sharmac624d162013-07-18 13:58:20 +0530207/* Support for RTL8211B PHY */
Andy Fleming9082eea2011-04-07 21:56:05 -0500208static struct phy_driver RTL8211B_driver = {
209 .name = "RealTek RTL8211B",
210 .uid = 0x1cc910,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530211 .mask = 0xffffff,
Andy Fleming9082eea2011-04-07 21:56:05 -0500212 .features = PHY_GBIT_FEATURES,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530213 .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 */
219static struct phy_driver RTL8211E_driver = {
220 .name = "RealTek RTL8211E",
221 .uid = 0x1cc915,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530222 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530223 .features = PHY_GBIT_FEATURES,
224 .config = &rtl8211x_config,
225 .startup = &rtl8211x_startup,
226 .shutdown = &genphy_shutdown,
227};
228
229/* Support for RTL8211DN PHY */
230static struct phy_driver RTL8211DN_driver = {
231 .name = "RealTek RTL8211DN",
232 .uid = 0x1cc914,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530233 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530234 .features = PHY_GBIT_FEATURES,
235 .config = &rtl8211x_config,
236 .startup = &rtl8211x_startup,
Andy Fleming9082eea2011-04-07 21:56:05 -0500237 .shutdown = &genphy_shutdown,
238};
239
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800240/* Support for RTL8211F PHY */
241static struct phy_driver RTL8211F_driver = {
242 .name = "RealTek RTL8211F",
243 .uid = 0x1cc916,
244 .mask = 0xffffff,
245 .features = PHY_GBIT_FEATURES,
Shengzhou Liu793ea942015-04-24 16:57:17 +0800246 .config = &rtl8211f_config,
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800247 .startup = &rtl8211f_startup,
248 .shutdown = &genphy_shutdown,
249};
250
Andy Fleming9082eea2011-04-07 21:56:05 -0500251int phy_realtek_init(void)
252{
253 phy_register(&RTL8211B_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530254 phy_register(&RTL8211E_driver);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800255 phy_register(&RTL8211F_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530256 phy_register(&RTL8211DN_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500257
258 return 0;
259}