blob: 359ec5048e1c94fc21fd7375cb1e86c61b444273 [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
Michael Haas525d1872016-03-25 18:22:50 +010015/* RTL8211x 1000BASE-T Control Register */
16#define MIIM_RTL8211x_CTRL1000T_MSCE (1 << 12);
17#define MIIM_RTL8211X_CTRL1000T_MASTER (1 << 11);
18
Bhupesh Sharmac624d162013-07-18 13:58:20 +053019/* RTL8211x PHY Status Register */
20#define MIIM_RTL8211x_PHY_STATUS 0x11
21#define MIIM_RTL8211x_PHYSTAT_SPEED 0xc000
22#define MIIM_RTL8211x_PHYSTAT_GBIT 0x8000
23#define MIIM_RTL8211x_PHYSTAT_100 0x4000
24#define MIIM_RTL8211x_PHYSTAT_DUPLEX 0x2000
25#define MIIM_RTL8211x_PHYSTAT_SPDDONE 0x0800
26#define MIIM_RTL8211x_PHYSTAT_LINK 0x0400
Andy Fleming9082eea2011-04-07 21:56:05 -050027
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020028/* RTL8211x PHY Interrupt Enable Register */
29#define MIIM_RTL8211x_PHY_INER 0x12
30#define MIIM_RTL8211x_PHY_INTR_ENA 0x9f01
31#define MIIM_RTL8211x_PHY_INTR_DIS 0x0000
32
33/* RTL8211x PHY Interrupt Status Register */
34#define MIIM_RTL8211x_PHY_INSR 0x13
Andy Fleming9082eea2011-04-07 21:56:05 -050035
Shengzhou Liu3d6af742015-03-12 18:54:59 +080036/* RTL8211F PHY Status Register */
37#define MIIM_RTL8211F_PHY_STATUS 0x1a
38#define MIIM_RTL8211F_AUTONEG_ENABLE 0x1000
39#define MIIM_RTL8211F_PHYSTAT_SPEED 0x0030
40#define MIIM_RTL8211F_PHYSTAT_GBIT 0x0020
41#define MIIM_RTL8211F_PHYSTAT_100 0x0010
42#define MIIM_RTL8211F_PHYSTAT_DUPLEX 0x0008
43#define MIIM_RTL8211F_PHYSTAT_SPDDONE 0x0800
44#define MIIM_RTL8211F_PHYSTAT_LINK 0x0004
45
46#define MIIM_RTL8211F_PAGE_SELECT 0x1f
Shengzhou Liu793ea942015-04-24 16:57:17 +080047#define MIIM_RTL8211F_TX_DELAY 0x100
Shengzhou Liu90712742015-05-21 18:07:35 +080048#define MIIM_RTL8211F_LCR 0x10
Shengzhou Liu3d6af742015-03-12 18:54:59 +080049
Bhupesh Sharmac624d162013-07-18 13:58:20 +053050/* RealTek RTL8211x */
51static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050052{
53 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
54
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020055 /* mask interrupt at init; if the interrupt is
56 * needed indeed, it should be explicitly enabled
57 */
58 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER,
59 MIIM_RTL8211x_PHY_INTR_DIS);
Michael Haas525d1872016-03-25 18:22:50 +010060#ifdef CONFIG_RTL8211X_PHY_FORCE_MASTER
61 unsigned int reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
62 /* force manual master/slave configuration */
63 reg |= MIIM_RTL8211x_CTRL1000T_MSCE;
64 /* force master mode */
65 reg |= MIIM_RTL8211X_CTRL1000T_MASTER;
66 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, reg);
67#endif
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020068 /* read interrupt status just to clear it */
69 phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER);
70
Andy Fleming9082eea2011-04-07 21:56:05 -050071 genphy_config_aneg(phydev);
72
73 return 0;
74}
75
Shengzhou Liu793ea942015-04-24 16:57:17 +080076static int rtl8211f_config(struct phy_device *phydev)
77{
78 u16 reg;
79
80 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
81
82 if (phydev->interface == PHY_INTERFACE_MODE_RGMII) {
83 /* enable TXDLY */
84 phy_write(phydev, MDIO_DEVAD_NONE,
85 MIIM_RTL8211F_PAGE_SELECT, 0xd08);
86 reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11);
87 reg |= MIIM_RTL8211F_TX_DELAY;
88 phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg);
89 /* restore to default page 0 */
90 phy_write(phydev, MDIO_DEVAD_NONE,
91 MIIM_RTL8211F_PAGE_SELECT, 0x0);
92 }
93
Shengzhou Liu90712742015-05-21 18:07:35 +080094 /* Set green LED for Link, yellow LED for Active */
95 phy_write(phydev, MDIO_DEVAD_NONE,
96 MIIM_RTL8211F_PAGE_SELECT, 0xd04);
97 phy_write(phydev, MDIO_DEVAD_NONE, 0x10, 0x617f);
98 phy_write(phydev, MDIO_DEVAD_NONE,
99 MIIM_RTL8211F_PAGE_SELECT, 0x0);
100
Shengzhou Liu793ea942015-04-24 16:57:17 +0800101 genphy_config_aneg(phydev);
102
103 return 0;
104}
105
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530106static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500107{
108 unsigned int speed;
109 unsigned int mii_reg;
110
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530111 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500112
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530113 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500114 int i = 0;
115
116 /* in case of timeout ->link is cleared */
117 phydev->link = 1;
118 puts("Waiting for PHY realtime link");
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530119 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500120 /* Timeout reached ? */
121 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
122 puts(" TIMEOUT !\n");
123 phydev->link = 0;
124 break;
125 }
126
127 if ((i++ % 1000) == 0)
128 putc('.');
129 udelay(1000); /* 1 ms */
130 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530131 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500132 }
133 puts(" done\n");
134 udelay(500000); /* another 500 ms (results in faster booting) */
135 } else {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530136 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming9082eea2011-04-07 21:56:05 -0500137 phydev->link = 1;
138 else
139 phydev->link = 0;
140 }
141
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530142 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming9082eea2011-04-07 21:56:05 -0500143 phydev->duplex = DUPLEX_FULL;
144 else
145 phydev->duplex = DUPLEX_HALF;
146
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530147 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming9082eea2011-04-07 21:56:05 -0500148
149 switch (speed) {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530150 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming9082eea2011-04-07 21:56:05 -0500151 phydev->speed = SPEED_1000;
152 break;
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530153 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming9082eea2011-04-07 21:56:05 -0500154 phydev->speed = SPEED_100;
155 break;
156 default:
157 phydev->speed = SPEED_10;
158 }
159
160 return 0;
161}
162
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800163static int rtl8211f_parse_status(struct phy_device *phydev)
164{
165 unsigned int speed;
166 unsigned int mii_reg;
167 int i = 0;
168
169 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43);
170 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS);
171
172 phydev->link = 1;
173 while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) {
174 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
175 puts(" TIMEOUT !\n");
176 phydev->link = 0;
177 break;
178 }
179
180 if ((i++ % 1000) == 0)
181 putc('.');
182 udelay(1000);
183 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
184 MIIM_RTL8211F_PHY_STATUS);
185 }
186
187 if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX)
188 phydev->duplex = DUPLEX_FULL;
189 else
190 phydev->duplex = DUPLEX_HALF;
191
192 speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED);
193
194 switch (speed) {
195 case MIIM_RTL8211F_PHYSTAT_GBIT:
196 phydev->speed = SPEED_1000;
197 break;
198 case MIIM_RTL8211F_PHYSTAT_100:
199 phydev->speed = SPEED_100;
200 break;
201 default:
202 phydev->speed = SPEED_10;
203 }
204
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800205 return 0;
206}
207
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530208static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500209{
210 /* Read the Status (2x to make sure link is right) */
211 genphy_update_link(phydev);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530212 rtl8211x_parse_status(phydev);
Andy Fleming9082eea2011-04-07 21:56:05 -0500213
214 return 0;
215}
216
Michal Simek6a10bc52016-02-13 10:31:32 +0100217static int rtl8211e_startup(struct phy_device *phydev)
218{
219 genphy_update_link(phydev);
220 genphy_parse_link(phydev);
221
222 return 0;
223}
224
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800225static int rtl8211f_startup(struct phy_device *phydev)
226{
227 /* Read the Status (2x to make sure link is right) */
228 genphy_update_link(phydev);
229 rtl8211f_parse_status(phydev);
230
231 return 0;
232}
233
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530234/* Support for RTL8211B PHY */
Andy Fleming9082eea2011-04-07 21:56:05 -0500235static struct phy_driver RTL8211B_driver = {
236 .name = "RealTek RTL8211B",
237 .uid = 0x1cc910,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530238 .mask = 0xffffff,
Andy Fleming9082eea2011-04-07 21:56:05 -0500239 .features = PHY_GBIT_FEATURES,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530240 .config = &rtl8211x_config,
241 .startup = &rtl8211x_startup,
242 .shutdown = &genphy_shutdown,
243};
244
245/* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */
246static struct phy_driver RTL8211E_driver = {
247 .name = "RealTek RTL8211E",
248 .uid = 0x1cc915,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530249 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530250 .features = PHY_GBIT_FEATURES,
251 .config = &rtl8211x_config,
Michal Simek6a10bc52016-02-13 10:31:32 +0100252 .startup = &rtl8211e_startup,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530253 .shutdown = &genphy_shutdown,
254};
255
256/* Support for RTL8211DN PHY */
257static struct phy_driver RTL8211DN_driver = {
258 .name = "RealTek RTL8211DN",
259 .uid = 0x1cc914,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530260 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530261 .features = PHY_GBIT_FEATURES,
262 .config = &rtl8211x_config,
263 .startup = &rtl8211x_startup,
Andy Fleming9082eea2011-04-07 21:56:05 -0500264 .shutdown = &genphy_shutdown,
265};
266
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800267/* Support for RTL8211F PHY */
268static struct phy_driver RTL8211F_driver = {
269 .name = "RealTek RTL8211F",
270 .uid = 0x1cc916,
271 .mask = 0xffffff,
272 .features = PHY_GBIT_FEATURES,
Shengzhou Liu793ea942015-04-24 16:57:17 +0800273 .config = &rtl8211f_config,
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800274 .startup = &rtl8211f_startup,
275 .shutdown = &genphy_shutdown,
276};
277
Andy Fleming9082eea2011-04-07 21:56:05 -0500278int phy_realtek_init(void)
279{
280 phy_register(&RTL8211B_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530281 phy_register(&RTL8211E_driver);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800282 phy_register(&RTL8211F_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530283 phy_register(&RTL8211DN_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500284
285 return 0;
286}