blob: b0867af22062baf888899c0eb2f111519bf61aa4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming9082eea2011-04-07 21:56:05 -05002/*
3 * RealTek PHY drivers
4 *
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +02005 * Copyright 2010-2011, 2015 Freescale Semiconductor, Inc.
Andy Fleming9082eea2011-04-07 21:56:05 -05006 * author Andy Fleming
Karsten Merker563d8d92016-03-21 20:29:07 +01007 * Copyright 2016 Karsten Merker <merker@debian.org>
Andy Fleming9082eea2011-04-07 21:56:05 -05008 */
9#include <config.h>
10#include <common.h>
oliver@schinagl.nl020f6762016-11-08 17:38:57 +010011#include <linux/bitops.h>
Andy Fleming9082eea2011-04-07 21:56:05 -050012#include <phy.h>
13
oliver@schinagl.nlcebf3f52016-11-08 17:38:59 +010014#define PHY_RTL8211x_FORCE_MASTER BIT(1)
kevans@FreeBSD.org66526e72018-02-14 17:02:15 -060015#define PHY_RTL8211E_PINE64_GIGABIT_FIX BIT(2)
oliver@schinagl.nlcebf3f52016-11-08 17:38:59 +010016
Andy Fleming9082eea2011-04-07 21:56:05 -050017#define PHY_AUTONEGOTIATE_TIMEOUT 5000
18
Michael Haas525d1872016-03-25 18:22:50 +010019/* RTL8211x 1000BASE-T Control Register */
oliver@schinagl.nl020f6762016-11-08 17:38:57 +010020#define MIIM_RTL8211x_CTRL1000T_MSCE BIT(12);
oliver@schinagl.nlcbe40e12016-11-08 17:38:58 +010021#define MIIM_RTL8211x_CTRL1000T_MASTER BIT(11);
Michael Haas525d1872016-03-25 18:22:50 +010022
Bhupesh Sharmac624d162013-07-18 13:58:20 +053023/* RTL8211x PHY Status Register */
24#define MIIM_RTL8211x_PHY_STATUS 0x11
25#define MIIM_RTL8211x_PHYSTAT_SPEED 0xc000
26#define MIIM_RTL8211x_PHYSTAT_GBIT 0x8000
27#define MIIM_RTL8211x_PHYSTAT_100 0x4000
28#define MIIM_RTL8211x_PHYSTAT_DUPLEX 0x2000
29#define MIIM_RTL8211x_PHYSTAT_SPDDONE 0x0800
30#define MIIM_RTL8211x_PHYSTAT_LINK 0x0400
Andy Fleming9082eea2011-04-07 21:56:05 -050031
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020032/* RTL8211x PHY Interrupt Enable Register */
33#define MIIM_RTL8211x_PHY_INER 0x12
34#define MIIM_RTL8211x_PHY_INTR_ENA 0x9f01
35#define MIIM_RTL8211x_PHY_INTR_DIS 0x0000
36
37/* RTL8211x PHY Interrupt Status Register */
38#define MIIM_RTL8211x_PHY_INSR 0x13
Andy Fleming9082eea2011-04-07 21:56:05 -050039
Shengzhou Liu3d6af742015-03-12 18:54:59 +080040/* RTL8211F PHY Status Register */
41#define MIIM_RTL8211F_PHY_STATUS 0x1a
42#define MIIM_RTL8211F_AUTONEG_ENABLE 0x1000
43#define MIIM_RTL8211F_PHYSTAT_SPEED 0x0030
44#define MIIM_RTL8211F_PHYSTAT_GBIT 0x0020
45#define MIIM_RTL8211F_PHYSTAT_100 0x0010
46#define MIIM_RTL8211F_PHYSTAT_DUPLEX 0x0008
47#define MIIM_RTL8211F_PHYSTAT_SPDDONE 0x0800
48#define MIIM_RTL8211F_PHYSTAT_LINK 0x0004
49
kevans@FreeBSD.org66526e72018-02-14 17:02:15 -060050#define MIIM_RTL8211E_CONFREG 0x1c
51#define MIIM_RTL8211E_CONFREG_TXD 0x0002
52#define MIIM_RTL8211E_CONFREG_RXD 0x0004
53#define MIIM_RTL8211E_CONFREG_MAGIC 0xb400 /* Undocumented */
54
55#define MIIM_RTL8211E_EXT_PAGE_SELECT 0x1e
56
Shengzhou Liu3d6af742015-03-12 18:54:59 +080057#define MIIM_RTL8211F_PAGE_SELECT 0x1f
Shengzhou Liu793ea942015-04-24 16:57:17 +080058#define MIIM_RTL8211F_TX_DELAY 0x100
Shengzhou Liu90712742015-05-21 18:07:35 +080059#define MIIM_RTL8211F_LCR 0x10
Shengzhou Liu3d6af742015-03-12 18:54:59 +080060
oliver@schinagl.nlcebf3f52016-11-08 17:38:59 +010061static int rtl8211b_probe(struct phy_device *phydev)
62{
63#ifdef CONFIG_RTL8211X_PHY_FORCE_MASTER
64 phydev->flags |= PHY_RTL8211x_FORCE_MASTER;
65#endif
66
67 return 0;
68}
69
kevans@FreeBSD.org66526e72018-02-14 17:02:15 -060070static int rtl8211e_probe(struct phy_device *phydev)
71{
72#ifdef CONFIG_RTL8211E_PINE64_GIGABIT_FIX
73 phydev->flags |= PHY_RTL8211E_PINE64_GIGABIT_FIX;
74#endif
75
76 return 0;
77}
78
Bhupesh Sharmac624d162013-07-18 13:58:20 +053079/* RealTek RTL8211x */
80static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050081{
82 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
83
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020084 /* mask interrupt at init; if the interrupt is
85 * needed indeed, it should be explicitly enabled
86 */
87 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER,
88 MIIM_RTL8211x_PHY_INTR_DIS);
oliver@schinagl.nlcebf3f52016-11-08 17:38:59 +010089
90 if (phydev->flags & PHY_RTL8211x_FORCE_MASTER) {
91 unsigned int reg;
92
93 reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
94 /* force manual master/slave configuration */
95 reg |= MIIM_RTL8211x_CTRL1000T_MSCE;
96 /* force master mode */
97 reg |= MIIM_RTL8211x_CTRL1000T_MASTER;
98 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, reg);
99 }
kevans@FreeBSD.org66526e72018-02-14 17:02:15 -0600100 if (phydev->flags & PHY_RTL8211E_PINE64_GIGABIT_FIX) {
101 unsigned int reg;
102
103 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT,
104 7);
105 phy_write(phydev, MDIO_DEVAD_NONE,
106 MIIM_RTL8211E_EXT_PAGE_SELECT, 0xa4);
107 reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG);
108 /* Ensure both internal delays are turned off */
109 reg &= ~(MIIM_RTL8211E_CONFREG_TXD | MIIM_RTL8211E_CONFREG_RXD);
110 /* Flip the magic undocumented bits */
111 reg |= MIIM_RTL8211E_CONFREG_MAGIC;
112 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211E_CONFREG, reg);
113 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT,
114 0);
115 }
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +0200116 /* read interrupt status just to clear it */
117 phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER);
118
Andy Fleming9082eea2011-04-07 21:56:05 -0500119 genphy_config_aneg(phydev);
120
121 return 0;
122}
123
Shengzhou Liu793ea942015-04-24 16:57:17 +0800124static int rtl8211f_config(struct phy_device *phydev)
125{
126 u16 reg;
127
128 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
129
Madalin Bucur05b29aa2017-08-18 11:35:24 +0300130 phy_write(phydev, MDIO_DEVAD_NONE,
131 MIIM_RTL8211F_PAGE_SELECT, 0xd08);
132 reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11);
133
134 /* enable TX-delay for rgmii-id and rgmii-txid, otherwise disable it */
135 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
136 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
Shengzhou Liu793ea942015-04-24 16:57:17 +0800137 reg |= MIIM_RTL8211F_TX_DELAY;
Madalin Bucur05b29aa2017-08-18 11:35:24 +0300138 else
139 reg &= ~MIIM_RTL8211F_TX_DELAY;
140
141 phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg);
142 /* restore to default page 0 */
143 phy_write(phydev, MDIO_DEVAD_NONE,
144 MIIM_RTL8211F_PAGE_SELECT, 0x0);
Shengzhou Liu793ea942015-04-24 16:57:17 +0800145
Shengzhou Liu90712742015-05-21 18:07:35 +0800146 /* Set green LED for Link, yellow LED for Active */
147 phy_write(phydev, MDIO_DEVAD_NONE,
148 MIIM_RTL8211F_PAGE_SELECT, 0xd04);
149 phy_write(phydev, MDIO_DEVAD_NONE, 0x10, 0x617f);
150 phy_write(phydev, MDIO_DEVAD_NONE,
151 MIIM_RTL8211F_PAGE_SELECT, 0x0);
152
Shengzhou Liu793ea942015-04-24 16:57:17 +0800153 genphy_config_aneg(phydev);
154
155 return 0;
156}
157
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530158static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500159{
160 unsigned int speed;
161 unsigned int mii_reg;
162
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530163 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500164
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530165 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500166 int i = 0;
167
168 /* in case of timeout ->link is cleared */
169 phydev->link = 1;
170 puts("Waiting for PHY realtime link");
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530171 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500172 /* Timeout reached ? */
173 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
174 puts(" TIMEOUT !\n");
175 phydev->link = 0;
176 break;
177 }
178
179 if ((i++ % 1000) == 0)
180 putc('.');
181 udelay(1000); /* 1 ms */
182 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530183 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500184 }
185 puts(" done\n");
186 udelay(500000); /* another 500 ms (results in faster booting) */
187 } else {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530188 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming9082eea2011-04-07 21:56:05 -0500189 phydev->link = 1;
190 else
191 phydev->link = 0;
192 }
193
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530194 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming9082eea2011-04-07 21:56:05 -0500195 phydev->duplex = DUPLEX_FULL;
196 else
197 phydev->duplex = DUPLEX_HALF;
198
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530199 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming9082eea2011-04-07 21:56:05 -0500200
201 switch (speed) {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530202 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming9082eea2011-04-07 21:56:05 -0500203 phydev->speed = SPEED_1000;
204 break;
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530205 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming9082eea2011-04-07 21:56:05 -0500206 phydev->speed = SPEED_100;
207 break;
208 default:
209 phydev->speed = SPEED_10;
210 }
211
212 return 0;
213}
214
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800215static int rtl8211f_parse_status(struct phy_device *phydev)
216{
217 unsigned int speed;
218 unsigned int mii_reg;
219 int i = 0;
220
221 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43);
222 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS);
223
224 phydev->link = 1;
225 while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) {
226 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
227 puts(" TIMEOUT !\n");
228 phydev->link = 0;
229 break;
230 }
231
232 if ((i++ % 1000) == 0)
233 putc('.');
234 udelay(1000);
235 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
236 MIIM_RTL8211F_PHY_STATUS);
237 }
238
239 if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX)
240 phydev->duplex = DUPLEX_FULL;
241 else
242 phydev->duplex = DUPLEX_HALF;
243
244 speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED);
245
246 switch (speed) {
247 case MIIM_RTL8211F_PHYSTAT_GBIT:
248 phydev->speed = SPEED_1000;
249 break;
250 case MIIM_RTL8211F_PHYSTAT_100:
251 phydev->speed = SPEED_100;
252 break;
253 default:
254 phydev->speed = SPEED_10;
255 }
256
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800257 return 0;
258}
259
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530260static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500261{
Michal Simekb733c272016-05-18 12:46:12 +0200262 int ret;
Andy Fleming9082eea2011-04-07 21:56:05 -0500263
Michal Simekb733c272016-05-18 12:46:12 +0200264 /* Read the Status (2x to make sure link is right) */
265 ret = genphy_update_link(phydev);
266 if (ret)
267 return ret;
268
269 return rtl8211x_parse_status(phydev);
Andy Fleming9082eea2011-04-07 21:56:05 -0500270}
271
Michal Simek6a10bc52016-02-13 10:31:32 +0100272static int rtl8211e_startup(struct phy_device *phydev)
273{
Michal Simekb733c272016-05-18 12:46:12 +0200274 int ret;
Michal Simek6a10bc52016-02-13 10:31:32 +0100275
Michal Simekb733c272016-05-18 12:46:12 +0200276 ret = genphy_update_link(phydev);
277 if (ret)
278 return ret;
279
280 return genphy_parse_link(phydev);
Michal Simek6a10bc52016-02-13 10:31:32 +0100281}
282
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800283static int rtl8211f_startup(struct phy_device *phydev)
284{
Michal Simekb733c272016-05-18 12:46:12 +0200285 int ret;
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800286
Michal Simekb733c272016-05-18 12:46:12 +0200287 /* Read the Status (2x to make sure link is right) */
288 ret = genphy_update_link(phydev);
289 if (ret)
290 return ret;
291 /* Read the Status (2x to make sure link is right) */
292
293 return rtl8211f_parse_status(phydev);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800294}
295
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530296/* Support for RTL8211B PHY */
Andy Fleming9082eea2011-04-07 21:56:05 -0500297static struct phy_driver RTL8211B_driver = {
298 .name = "RealTek RTL8211B",
Karsten Merker563d8d92016-03-21 20:29:07 +0100299 .uid = 0x1cc912,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530300 .mask = 0xffffff,
Andy Fleming9082eea2011-04-07 21:56:05 -0500301 .features = PHY_GBIT_FEATURES,
oliver@schinagl.nlcebf3f52016-11-08 17:38:59 +0100302 .probe = &rtl8211b_probe,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530303 .config = &rtl8211x_config,
304 .startup = &rtl8211x_startup,
305 .shutdown = &genphy_shutdown,
306};
307
308/* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */
309static struct phy_driver RTL8211E_driver = {
310 .name = "RealTek RTL8211E",
311 .uid = 0x1cc915,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530312 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530313 .features = PHY_GBIT_FEATURES,
kevans@FreeBSD.org66526e72018-02-14 17:02:15 -0600314 .probe = &rtl8211e_probe,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530315 .config = &rtl8211x_config,
Michal Simek6a10bc52016-02-13 10:31:32 +0100316 .startup = &rtl8211e_startup,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530317 .shutdown = &genphy_shutdown,
318};
319
320/* Support for RTL8211DN PHY */
321static struct phy_driver RTL8211DN_driver = {
322 .name = "RealTek RTL8211DN",
323 .uid = 0x1cc914,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530324 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530325 .features = PHY_GBIT_FEATURES,
326 .config = &rtl8211x_config,
327 .startup = &rtl8211x_startup,
Andy Fleming9082eea2011-04-07 21:56:05 -0500328 .shutdown = &genphy_shutdown,
329};
330
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800331/* Support for RTL8211F PHY */
332static struct phy_driver RTL8211F_driver = {
333 .name = "RealTek RTL8211F",
334 .uid = 0x1cc916,
335 .mask = 0xffffff,
336 .features = PHY_GBIT_FEATURES,
Shengzhou Liu793ea942015-04-24 16:57:17 +0800337 .config = &rtl8211f_config,
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800338 .startup = &rtl8211f_startup,
339 .shutdown = &genphy_shutdown,
340};
341
Andy Fleming9082eea2011-04-07 21:56:05 -0500342int phy_realtek_init(void)
343{
344 phy_register(&RTL8211B_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530345 phy_register(&RTL8211E_driver);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800346 phy_register(&RTL8211F_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530347 phy_register(&RTL8211DN_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500348
349 return 0;
350}