blob: d5c2a46c67e2b272644419cb96cfab8209a612c7 [file] [log] [blame]
Andy Fleming60ca78b2011-04-07 21:56:05 -05001/*
2 * RealTek PHY drivers
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming60ca78b2011-04-07 21:56:05 -05005 *
Codrin Ciubotariude947a12015-02-13 14:47:58 +02006 * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc.
Andy Fleming60ca78b2011-04-07 21:56:05 -05007 * author Andy Fleming
Karsten Merker10800de2016-03-21 20:29:07 +01008 * Copyright 2016 Karsten Merker <merker@debian.org>
Andy Fleming60ca78b2011-04-07 21:56:05 -05009 */
10#include <config.h>
11#include <common.h>
oliver@schinagl.nlec1b26f2016-11-08 17:38:57 +010012#include <linux/bitops.h>
Andy Fleming60ca78b2011-04-07 21:56:05 -050013#include <phy.h>
14
oliver@schinagl.nl6a250902016-11-08 17:38:59 +010015#define PHY_RTL8211x_FORCE_MASTER BIT(1)
kevans@FreeBSD.org7c824012018-02-14 17:02:15 -060016#define PHY_RTL8211E_PINE64_GIGABIT_FIX BIT(2)
oliver@schinagl.nl6a250902016-11-08 17:38:59 +010017
Andy Fleming60ca78b2011-04-07 21:56:05 -050018#define PHY_AUTONEGOTIATE_TIMEOUT 5000
19
Michael Haas0cf2b4e2016-03-25 18:22:50 +010020/* RTL8211x 1000BASE-T Control Register */
oliver@schinagl.nlec1b26f2016-11-08 17:38:57 +010021#define MIIM_RTL8211x_CTRL1000T_MSCE BIT(12);
oliver@schinagl.nl1beb7352016-11-08 17:38:58 +010022#define MIIM_RTL8211x_CTRL1000T_MASTER BIT(11);
Michael Haas0cf2b4e2016-03-25 18:22:50 +010023
Bhupesh Sharma85eb7802013-07-18 13:58:20 +053024/* 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 Fleming60ca78b2011-04-07 21:56:05 -050032
Codrin Ciubotariude947a12015-02-13 14:47:58 +020033/* 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 Fleming60ca78b2011-04-07 21:56:05 -050040
Shengzhou Liu210f17f2015-03-12 18:54:59 +080041/* 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
kevans@FreeBSD.org7c824012018-02-14 17:02:15 -060051#define MIIM_RTL8211E_CONFREG 0x1c
52#define MIIM_RTL8211E_CONFREG_TXD 0x0002
53#define MIIM_RTL8211E_CONFREG_RXD 0x0004
54#define MIIM_RTL8211E_CONFREG_MAGIC 0xb400 /* Undocumented */
55
56#define MIIM_RTL8211E_EXT_PAGE_SELECT 0x1e
57
Shengzhou Liu210f17f2015-03-12 18:54:59 +080058#define MIIM_RTL8211F_PAGE_SELECT 0x1f
Shengzhou Liu67500892015-04-24 16:57:17 +080059#define MIIM_RTL8211F_TX_DELAY 0x100
Shengzhou Liu5487fc62015-05-21 18:07:35 +080060#define MIIM_RTL8211F_LCR 0x10
Shengzhou Liu210f17f2015-03-12 18:54:59 +080061
oliver@schinagl.nl6a250902016-11-08 17:38:59 +010062static int rtl8211b_probe(struct phy_device *phydev)
63{
64#ifdef CONFIG_RTL8211X_PHY_FORCE_MASTER
65 phydev->flags |= PHY_RTL8211x_FORCE_MASTER;
66#endif
67
68 return 0;
69}
70
kevans@FreeBSD.org7c824012018-02-14 17:02:15 -060071static int rtl8211e_probe(struct phy_device *phydev)
72{
73#ifdef CONFIG_RTL8211E_PINE64_GIGABIT_FIX
74 phydev->flags |= PHY_RTL8211E_PINE64_GIGABIT_FIX;
75#endif
76
77 return 0;
78}
79
Bhupesh Sharma85eb7802013-07-18 13:58:20 +053080/* RealTek RTL8211x */
81static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming60ca78b2011-04-07 21:56:05 -050082{
83 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
84
Codrin Ciubotariude947a12015-02-13 14:47:58 +020085 /* mask interrupt at init; if the interrupt is
86 * needed indeed, it should be explicitly enabled
87 */
88 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER,
89 MIIM_RTL8211x_PHY_INTR_DIS);
oliver@schinagl.nl6a250902016-11-08 17:38:59 +010090
91 if (phydev->flags & PHY_RTL8211x_FORCE_MASTER) {
92 unsigned int reg;
93
94 reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
95 /* force manual master/slave configuration */
96 reg |= MIIM_RTL8211x_CTRL1000T_MSCE;
97 /* force master mode */
98 reg |= MIIM_RTL8211x_CTRL1000T_MASTER;
99 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, reg);
100 }
kevans@FreeBSD.org7c824012018-02-14 17:02:15 -0600101 if (phydev->flags & PHY_RTL8211E_PINE64_GIGABIT_FIX) {
102 unsigned int reg;
103
104 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT,
105 7);
106 phy_write(phydev, MDIO_DEVAD_NONE,
107 MIIM_RTL8211E_EXT_PAGE_SELECT, 0xa4);
108 reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG);
109 /* Ensure both internal delays are turned off */
110 reg &= ~(MIIM_RTL8211E_CONFREG_TXD | MIIM_RTL8211E_CONFREG_RXD);
111 /* Flip the magic undocumented bits */
112 reg |= MIIM_RTL8211E_CONFREG_MAGIC;
113 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG, reg);
114 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT,
115 0);
116 }
Codrin Ciubotariude947a12015-02-13 14:47:58 +0200117 /* read interrupt status just to clear it */
118 phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER);
119
Andy Fleming60ca78b2011-04-07 21:56:05 -0500120 genphy_config_aneg(phydev);
121
122 return 0;
123}
124
Shengzhou Liu67500892015-04-24 16:57:17 +0800125static int rtl8211f_config(struct phy_device *phydev)
126{
127 u16 reg;
128
129 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
130
Madalin Bucur83ba7aa2017-08-18 11:35:24 +0300131 phy_write(phydev, MDIO_DEVAD_NONE,
132 MIIM_RTL8211F_PAGE_SELECT, 0xd08);
133 reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11);
134
135 /* enable TX-delay for rgmii-id and rgmii-txid, otherwise disable it */
136 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
137 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shengzhou Liu67500892015-04-24 16:57:17 +0800138 reg |= MIIM_RTL8211F_TX_DELAY;
Madalin Bucur83ba7aa2017-08-18 11:35:24 +0300139 else
140 reg &= ~MIIM_RTL8211F_TX_DELAY;
141
142 phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg);
143 /* restore to default page 0 */
144 phy_write(phydev, MDIO_DEVAD_NONE,
145 MIIM_RTL8211F_PAGE_SELECT, 0x0);
Shengzhou Liu67500892015-04-24 16:57:17 +0800146
Shengzhou Liu5487fc62015-05-21 18:07:35 +0800147 /* Set green LED for Link, yellow LED for Active */
148 phy_write(phydev, MDIO_DEVAD_NONE,
149 MIIM_RTL8211F_PAGE_SELECT, 0xd04);
150 phy_write(phydev, MDIO_DEVAD_NONE, 0x10, 0x617f);
151 phy_write(phydev, MDIO_DEVAD_NONE,
152 MIIM_RTL8211F_PAGE_SELECT, 0x0);
153
Shengzhou Liu67500892015-04-24 16:57:17 +0800154 genphy_config_aneg(phydev);
155
156 return 0;
157}
158
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530159static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming60ca78b2011-04-07 21:56:05 -0500160{
161 unsigned int speed;
162 unsigned int mii_reg;
163
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530164 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500165
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530166 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming60ca78b2011-04-07 21:56:05 -0500167 int i = 0;
168
169 /* in case of timeout ->link is cleared */
170 phydev->link = 1;
171 puts("Waiting for PHY realtime link");
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530172 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming60ca78b2011-04-07 21:56:05 -0500173 /* Timeout reached ? */
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); /* 1 ms */
183 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530184 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500185 }
186 puts(" done\n");
187 udelay(500000); /* another 500 ms (results in faster booting) */
188 } else {
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530189 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming60ca78b2011-04-07 21:56:05 -0500190 phydev->link = 1;
191 else
192 phydev->link = 0;
193 }
194
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530195 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming60ca78b2011-04-07 21:56:05 -0500196 phydev->duplex = DUPLEX_FULL;
197 else
198 phydev->duplex = DUPLEX_HALF;
199
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530200 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500201
202 switch (speed) {
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530203 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming60ca78b2011-04-07 21:56:05 -0500204 phydev->speed = SPEED_1000;
205 break;
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530206 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming60ca78b2011-04-07 21:56:05 -0500207 phydev->speed = SPEED_100;
208 break;
209 default:
210 phydev->speed = SPEED_10;
211 }
212
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800213 return 0;
214}
215
216static int rtl8211f_parse_status(struct phy_device *phydev)
217{
218 unsigned int speed;
219 unsigned int mii_reg;
220 int i = 0;
221
222 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43);
223 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS);
224
225 phydev->link = 1;
226 while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) {
227 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
228 puts(" TIMEOUT !\n");
229 phydev->link = 0;
230 break;
231 }
232
233 if ((i++ % 1000) == 0)
234 putc('.');
235 udelay(1000);
236 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
237 MIIM_RTL8211F_PHY_STATUS);
238 }
239
240 if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX)
241 phydev->duplex = DUPLEX_FULL;
242 else
243 phydev->duplex = DUPLEX_HALF;
244
245 speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED);
246
247 switch (speed) {
248 case MIIM_RTL8211F_PHYSTAT_GBIT:
249 phydev->speed = SPEED_1000;
250 break;
251 case MIIM_RTL8211F_PHYSTAT_100:
252 phydev->speed = SPEED_100;
253 break;
254 default:
255 phydev->speed = SPEED_10;
256 }
257
Andy Fleming60ca78b2011-04-07 21:56:05 -0500258 return 0;
259}
260
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530261static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming60ca78b2011-04-07 21:56:05 -0500262{
Michal Simek5ff89662016-05-18 12:46:12 +0200263 int ret;
264
Andy Fleming60ca78b2011-04-07 21:56:05 -0500265 /* Read the Status (2x to make sure link is right) */
Michal Simek5ff89662016-05-18 12:46:12 +0200266 ret = genphy_update_link(phydev);
267 if (ret)
268 return ret;
Andy Fleming60ca78b2011-04-07 21:56:05 -0500269
Michal Simek5ff89662016-05-18 12:46:12 +0200270 return rtl8211x_parse_status(phydev);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500271}
272
Michal Simekbdf0b722016-02-13 10:31:32 +0100273static int rtl8211e_startup(struct phy_device *phydev)
274{
Michal Simek5ff89662016-05-18 12:46:12 +0200275 int ret;
276
277 ret = genphy_update_link(phydev);
278 if (ret)
279 return ret;
Michal Simekbdf0b722016-02-13 10:31:32 +0100280
Michal Simek5ff89662016-05-18 12:46:12 +0200281 return genphy_parse_link(phydev);
Michal Simekbdf0b722016-02-13 10:31:32 +0100282}
283
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800284static int rtl8211f_startup(struct phy_device *phydev)
285{
Michal Simek5ff89662016-05-18 12:46:12 +0200286 int ret;
287
288 /* Read the Status (2x to make sure link is right) */
289 ret = genphy_update_link(phydev);
290 if (ret)
291 return ret;
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800292 /* Read the Status (2x to make sure link is right) */
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800293
Michal Simek5ff89662016-05-18 12:46:12 +0200294 return rtl8211f_parse_status(phydev);
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800295}
296
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530297/* Support for RTL8211B PHY */
Andy Fleming60ca78b2011-04-07 21:56:05 -0500298static struct phy_driver RTL8211B_driver = {
299 .name = "RealTek RTL8211B",
Karsten Merker10800de2016-03-21 20:29:07 +0100300 .uid = 0x1cc912,
Bhupesh Sharmaad4cd952013-09-01 04:40:52 +0530301 .mask = 0xffffff,
Andy Fleming60ca78b2011-04-07 21:56:05 -0500302 .features = PHY_GBIT_FEATURES,
oliver@schinagl.nl6a250902016-11-08 17:38:59 +0100303 .probe = &rtl8211b_probe,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530304 .config = &rtl8211x_config,
305 .startup = &rtl8211x_startup,
Andy Fleming60ca78b2011-04-07 21:56:05 -0500306 .shutdown = &genphy_shutdown,
307};
308
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530309/* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */
310static struct phy_driver RTL8211E_driver = {
311 .name = "RealTek RTL8211E",
312 .uid = 0x1cc915,
Bhupesh Sharmaad4cd952013-09-01 04:40:52 +0530313 .mask = 0xffffff,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530314 .features = PHY_GBIT_FEATURES,
kevans@FreeBSD.org7c824012018-02-14 17:02:15 -0600315 .probe = &rtl8211e_probe,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530316 .config = &rtl8211x_config,
Michal Simekbdf0b722016-02-13 10:31:32 +0100317 .startup = &rtl8211e_startup,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530318 .shutdown = &genphy_shutdown,
319};
320
321/* Support for RTL8211DN PHY */
322static struct phy_driver RTL8211DN_driver = {
323 .name = "RealTek RTL8211DN",
324 .uid = 0x1cc914,
Bhupesh Sharmaad4cd952013-09-01 04:40:52 +0530325 .mask = 0xffffff,
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530326 .features = PHY_GBIT_FEATURES,
327 .config = &rtl8211x_config,
328 .startup = &rtl8211x_startup,
329 .shutdown = &genphy_shutdown,
330};
331
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800332/* Support for RTL8211F PHY */
333static struct phy_driver RTL8211F_driver = {
334 .name = "RealTek RTL8211F",
335 .uid = 0x1cc916,
336 .mask = 0xffffff,
337 .features = PHY_GBIT_FEATURES,
Shengzhou Liu67500892015-04-24 16:57:17 +0800338 .config = &rtl8211f_config,
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800339 .startup = &rtl8211f_startup,
340 .shutdown = &genphy_shutdown,
341};
342
Andy Fleming60ca78b2011-04-07 21:56:05 -0500343int phy_realtek_init(void)
344{
345 phy_register(&RTL8211B_driver);
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530346 phy_register(&RTL8211E_driver);
Shengzhou Liu210f17f2015-03-12 18:54:59 +0800347 phy_register(&RTL8211F_driver);
Bhupesh Sharma85eb7802013-07-18 13:58:20 +0530348 phy_register(&RTL8211DN_driver);
Andy Fleming60ca78b2011-04-07 21:56:05 -0500349
350 return 0;
351}