blob: 35b934b8f0d0257cc0a947deb6aeb016dfa0eb39 [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
Karsten Merker563d8d92016-03-21 20:29:07 +01008 * Copyright 2016 Karsten Merker <merker@debian.org>
Andy Fleming9082eea2011-04-07 21:56:05 -05009 */
10#include <config.h>
11#include <common.h>
oliver@schinagl.nl020f6762016-11-08 17:38:57 +010012#include <linux/bitops.h>
Andy Fleming9082eea2011-04-07 21:56:05 -050013#include <phy.h>
14
15#define PHY_AUTONEGOTIATE_TIMEOUT 5000
16
Michael Haas525d1872016-03-25 18:22:50 +010017/* RTL8211x 1000BASE-T Control Register */
oliver@schinagl.nl020f6762016-11-08 17:38:57 +010018#define MIIM_RTL8211x_CTRL1000T_MSCE BIT(12);
19#define MIIM_RTL8211X_CTRL1000T_MASTER BIT(11);
Michael Haas525d1872016-03-25 18:22:50 +010020
Bhupesh Sharmac624d162013-07-18 13:58:20 +053021/* RTL8211x PHY Status Register */
22#define MIIM_RTL8211x_PHY_STATUS 0x11
23#define MIIM_RTL8211x_PHYSTAT_SPEED 0xc000
24#define MIIM_RTL8211x_PHYSTAT_GBIT 0x8000
25#define MIIM_RTL8211x_PHYSTAT_100 0x4000
26#define MIIM_RTL8211x_PHYSTAT_DUPLEX 0x2000
27#define MIIM_RTL8211x_PHYSTAT_SPDDONE 0x0800
28#define MIIM_RTL8211x_PHYSTAT_LINK 0x0400
Andy Fleming9082eea2011-04-07 21:56:05 -050029
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020030/* RTL8211x PHY Interrupt Enable Register */
31#define MIIM_RTL8211x_PHY_INER 0x12
32#define MIIM_RTL8211x_PHY_INTR_ENA 0x9f01
33#define MIIM_RTL8211x_PHY_INTR_DIS 0x0000
34
35/* RTL8211x PHY Interrupt Status Register */
36#define MIIM_RTL8211x_PHY_INSR 0x13
Andy Fleming9082eea2011-04-07 21:56:05 -050037
Shengzhou Liu3d6af742015-03-12 18:54:59 +080038/* RTL8211F PHY Status Register */
39#define MIIM_RTL8211F_PHY_STATUS 0x1a
40#define MIIM_RTL8211F_AUTONEG_ENABLE 0x1000
41#define MIIM_RTL8211F_PHYSTAT_SPEED 0x0030
42#define MIIM_RTL8211F_PHYSTAT_GBIT 0x0020
43#define MIIM_RTL8211F_PHYSTAT_100 0x0010
44#define MIIM_RTL8211F_PHYSTAT_DUPLEX 0x0008
45#define MIIM_RTL8211F_PHYSTAT_SPDDONE 0x0800
46#define MIIM_RTL8211F_PHYSTAT_LINK 0x0004
47
48#define MIIM_RTL8211F_PAGE_SELECT 0x1f
Shengzhou Liu793ea942015-04-24 16:57:17 +080049#define MIIM_RTL8211F_TX_DELAY 0x100
Shengzhou Liu90712742015-05-21 18:07:35 +080050#define MIIM_RTL8211F_LCR 0x10
Shengzhou Liu3d6af742015-03-12 18:54:59 +080051
Bhupesh Sharmac624d162013-07-18 13:58:20 +053052/* RealTek RTL8211x */
53static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050054{
55 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
56
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020057 /* mask interrupt at init; if the interrupt is
58 * needed indeed, it should be explicitly enabled
59 */
60 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER,
61 MIIM_RTL8211x_PHY_INTR_DIS);
Michael Haas525d1872016-03-25 18:22:50 +010062#ifdef CONFIG_RTL8211X_PHY_FORCE_MASTER
63 unsigned int reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
64 /* force manual master/slave configuration */
65 reg |= MIIM_RTL8211x_CTRL1000T_MSCE;
66 /* force master mode */
67 reg |= MIIM_RTL8211X_CTRL1000T_MASTER;
68 phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, reg);
69#endif
Codrin Ciubotariu3cee1382015-02-13 14:47:58 +020070 /* read interrupt status just to clear it */
71 phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_INER);
72
Andy Fleming9082eea2011-04-07 21:56:05 -050073 genphy_config_aneg(phydev);
74
75 return 0;
76}
77
Shengzhou Liu793ea942015-04-24 16:57:17 +080078static int rtl8211f_config(struct phy_device *phydev)
79{
80 u16 reg;
81
82 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
83
84 if (phydev->interface == PHY_INTERFACE_MODE_RGMII) {
85 /* enable TXDLY */
86 phy_write(phydev, MDIO_DEVAD_NONE,
87 MIIM_RTL8211F_PAGE_SELECT, 0xd08);
88 reg = phy_read(phydev, MDIO_DEVAD_NONE, 0x11);
89 reg |= MIIM_RTL8211F_TX_DELAY;
90 phy_write(phydev, MDIO_DEVAD_NONE, 0x11, reg);
91 /* restore to default page 0 */
92 phy_write(phydev, MDIO_DEVAD_NONE,
93 MIIM_RTL8211F_PAGE_SELECT, 0x0);
94 }
95
Shengzhou Liu90712742015-05-21 18:07:35 +080096 /* Set green LED for Link, yellow LED for Active */
97 phy_write(phydev, MDIO_DEVAD_NONE,
98 MIIM_RTL8211F_PAGE_SELECT, 0xd04);
99 phy_write(phydev, MDIO_DEVAD_NONE, 0x10, 0x617f);
100 phy_write(phydev, MDIO_DEVAD_NONE,
101 MIIM_RTL8211F_PAGE_SELECT, 0x0);
102
Shengzhou Liu793ea942015-04-24 16:57:17 +0800103 genphy_config_aneg(phydev);
104
105 return 0;
106}
107
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530108static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500109{
110 unsigned int speed;
111 unsigned int mii_reg;
112
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530113 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500114
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530115 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500116 int i = 0;
117
118 /* in case of timeout ->link is cleared */
119 phydev->link = 1;
120 puts("Waiting for PHY realtime link");
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530121 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -0500122 /* Timeout reached ? */
123 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
124 puts(" TIMEOUT !\n");
125 phydev->link = 0;
126 break;
127 }
128
129 if ((i++ % 1000) == 0)
130 putc('.');
131 udelay(1000); /* 1 ms */
132 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530133 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -0500134 }
135 puts(" done\n");
136 udelay(500000); /* another 500 ms (results in faster booting) */
137 } else {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530138 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming9082eea2011-04-07 21:56:05 -0500139 phydev->link = 1;
140 else
141 phydev->link = 0;
142 }
143
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530144 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming9082eea2011-04-07 21:56:05 -0500145 phydev->duplex = DUPLEX_FULL;
146 else
147 phydev->duplex = DUPLEX_HALF;
148
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530149 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming9082eea2011-04-07 21:56:05 -0500150
151 switch (speed) {
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530152 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming9082eea2011-04-07 21:56:05 -0500153 phydev->speed = SPEED_1000;
154 break;
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530155 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming9082eea2011-04-07 21:56:05 -0500156 phydev->speed = SPEED_100;
157 break;
158 default:
159 phydev->speed = SPEED_10;
160 }
161
162 return 0;
163}
164
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800165static int rtl8211f_parse_status(struct phy_device *phydev)
166{
167 unsigned int speed;
168 unsigned int mii_reg;
169 int i = 0;
170
171 phy_write(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PAGE_SELECT, 0xa43);
172 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211F_PHY_STATUS);
173
174 phydev->link = 1;
175 while (!(mii_reg & MIIM_RTL8211F_PHYSTAT_LINK)) {
176 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
177 puts(" TIMEOUT !\n");
178 phydev->link = 0;
179 break;
180 }
181
182 if ((i++ % 1000) == 0)
183 putc('.');
184 udelay(1000);
185 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
186 MIIM_RTL8211F_PHY_STATUS);
187 }
188
189 if (mii_reg & MIIM_RTL8211F_PHYSTAT_DUPLEX)
190 phydev->duplex = DUPLEX_FULL;
191 else
192 phydev->duplex = DUPLEX_HALF;
193
194 speed = (mii_reg & MIIM_RTL8211F_PHYSTAT_SPEED);
195
196 switch (speed) {
197 case MIIM_RTL8211F_PHYSTAT_GBIT:
198 phydev->speed = SPEED_1000;
199 break;
200 case MIIM_RTL8211F_PHYSTAT_100:
201 phydev->speed = SPEED_100;
202 break;
203 default:
204 phydev->speed = SPEED_10;
205 }
206
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800207 return 0;
208}
209
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530210static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -0500211{
Michal Simekb733c272016-05-18 12:46:12 +0200212 int ret;
Andy Fleming9082eea2011-04-07 21:56:05 -0500213
Michal Simekb733c272016-05-18 12:46:12 +0200214 /* Read the Status (2x to make sure link is right) */
215 ret = genphy_update_link(phydev);
216 if (ret)
217 return ret;
218
219 return rtl8211x_parse_status(phydev);
Andy Fleming9082eea2011-04-07 21:56:05 -0500220}
221
Michal Simek6a10bc52016-02-13 10:31:32 +0100222static int rtl8211e_startup(struct phy_device *phydev)
223{
Michal Simekb733c272016-05-18 12:46:12 +0200224 int ret;
Michal Simek6a10bc52016-02-13 10:31:32 +0100225
Michal Simekb733c272016-05-18 12:46:12 +0200226 ret = genphy_update_link(phydev);
227 if (ret)
228 return ret;
229
230 return genphy_parse_link(phydev);
Michal Simek6a10bc52016-02-13 10:31:32 +0100231}
232
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800233static int rtl8211f_startup(struct phy_device *phydev)
234{
Michal Simekb733c272016-05-18 12:46:12 +0200235 int ret;
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800236
Michal Simekb733c272016-05-18 12:46:12 +0200237 /* Read the Status (2x to make sure link is right) */
238 ret = genphy_update_link(phydev);
239 if (ret)
240 return ret;
241 /* Read the Status (2x to make sure link is right) */
242
243 return rtl8211f_parse_status(phydev);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800244}
245
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530246/* Support for RTL8211B PHY */
Andy Fleming9082eea2011-04-07 21:56:05 -0500247static struct phy_driver RTL8211B_driver = {
248 .name = "RealTek RTL8211B",
Karsten Merker563d8d92016-03-21 20:29:07 +0100249 .uid = 0x1cc912,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530250 .mask = 0xffffff,
Andy Fleming9082eea2011-04-07 21:56:05 -0500251 .features = PHY_GBIT_FEATURES,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530252 .config = &rtl8211x_config,
253 .startup = &rtl8211x_startup,
254 .shutdown = &genphy_shutdown,
255};
256
257/* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */
258static struct phy_driver RTL8211E_driver = {
259 .name = "RealTek RTL8211E",
260 .uid = 0x1cc915,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530261 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530262 .features = PHY_GBIT_FEATURES,
263 .config = &rtl8211x_config,
Michal Simek6a10bc52016-02-13 10:31:32 +0100264 .startup = &rtl8211e_startup,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530265 .shutdown = &genphy_shutdown,
266};
267
268/* Support for RTL8211DN PHY */
269static struct phy_driver RTL8211DN_driver = {
270 .name = "RealTek RTL8211DN",
271 .uid = 0x1cc914,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530272 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530273 .features = PHY_GBIT_FEATURES,
274 .config = &rtl8211x_config,
275 .startup = &rtl8211x_startup,
Andy Fleming9082eea2011-04-07 21:56:05 -0500276 .shutdown = &genphy_shutdown,
277};
278
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800279/* Support for RTL8211F PHY */
280static struct phy_driver RTL8211F_driver = {
281 .name = "RealTek RTL8211F",
282 .uid = 0x1cc916,
283 .mask = 0xffffff,
284 .features = PHY_GBIT_FEATURES,
Shengzhou Liu793ea942015-04-24 16:57:17 +0800285 .config = &rtl8211f_config,
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800286 .startup = &rtl8211f_startup,
287 .shutdown = &genphy_shutdown,
288};
289
Andy Fleming9082eea2011-04-07 21:56:05 -0500290int phy_realtek_init(void)
291{
292 phy_register(&RTL8211B_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530293 phy_register(&RTL8211E_driver);
Shengzhou Liu3d6af742015-03-12 18:54:59 +0800294 phy_register(&RTL8211F_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530295 phy_register(&RTL8211DN_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500296
297 return 0;
298}