blob: a3ace685262441219c400b69fa5db765643041c0 [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 *
6 * Copyright 2010-2011 Freescale Semiconductor, Inc.
7 * 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
24
Bhupesh Sharmac624d162013-07-18 13:58:20 +053025/* RealTek RTL8211x */
26static int rtl8211x_config(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050027{
28 phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, BMCR_RESET);
29
30 genphy_config_aneg(phydev);
31
32 return 0;
33}
34
Bhupesh Sharmac624d162013-07-18 13:58:20 +053035static int rtl8211x_parse_status(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050036{
37 unsigned int speed;
38 unsigned int mii_reg;
39
Bhupesh Sharmac624d162013-07-18 13:58:20 +053040 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -050041
Bhupesh Sharmac624d162013-07-18 13:58:20 +053042 if (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -050043 int i = 0;
44
45 /* in case of timeout ->link is cleared */
46 phydev->link = 1;
47 puts("Waiting for PHY realtime link");
Bhupesh Sharmac624d162013-07-18 13:58:20 +053048 while (!(mii_reg & MIIM_RTL8211x_PHYSTAT_SPDDONE)) {
Andy Fleming9082eea2011-04-07 21:56:05 -050049 /* Timeout reached ? */
50 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
51 puts(" TIMEOUT !\n");
52 phydev->link = 0;
53 break;
54 }
55
56 if ((i++ % 1000) == 0)
57 putc('.');
58 udelay(1000); /* 1 ms */
59 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
Bhupesh Sharmac624d162013-07-18 13:58:20 +053060 MIIM_RTL8211x_PHY_STATUS);
Andy Fleming9082eea2011-04-07 21:56:05 -050061 }
62 puts(" done\n");
63 udelay(500000); /* another 500 ms (results in faster booting) */
64 } else {
Bhupesh Sharmac624d162013-07-18 13:58:20 +053065 if (mii_reg & MIIM_RTL8211x_PHYSTAT_LINK)
Andy Fleming9082eea2011-04-07 21:56:05 -050066 phydev->link = 1;
67 else
68 phydev->link = 0;
69 }
70
Bhupesh Sharmac624d162013-07-18 13:58:20 +053071 if (mii_reg & MIIM_RTL8211x_PHYSTAT_DUPLEX)
Andy Fleming9082eea2011-04-07 21:56:05 -050072 phydev->duplex = DUPLEX_FULL;
73 else
74 phydev->duplex = DUPLEX_HALF;
75
Bhupesh Sharmac624d162013-07-18 13:58:20 +053076 speed = (mii_reg & MIIM_RTL8211x_PHYSTAT_SPEED);
Andy Fleming9082eea2011-04-07 21:56:05 -050077
78 switch (speed) {
Bhupesh Sharmac624d162013-07-18 13:58:20 +053079 case MIIM_RTL8211x_PHYSTAT_GBIT:
Andy Fleming9082eea2011-04-07 21:56:05 -050080 phydev->speed = SPEED_1000;
81 break;
Bhupesh Sharmac624d162013-07-18 13:58:20 +053082 case MIIM_RTL8211x_PHYSTAT_100:
Andy Fleming9082eea2011-04-07 21:56:05 -050083 phydev->speed = SPEED_100;
84 break;
85 default:
86 phydev->speed = SPEED_10;
87 }
88
89 return 0;
90}
91
Bhupesh Sharmac624d162013-07-18 13:58:20 +053092static int rtl8211x_startup(struct phy_device *phydev)
Andy Fleming9082eea2011-04-07 21:56:05 -050093{
94 /* Read the Status (2x to make sure link is right) */
95 genphy_update_link(phydev);
Bhupesh Sharmac624d162013-07-18 13:58:20 +053096 rtl8211x_parse_status(phydev);
Andy Fleming9082eea2011-04-07 21:56:05 -050097
98 return 0;
99}
100
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530101/* Support for RTL8211B PHY */
Andy Fleming9082eea2011-04-07 21:56:05 -0500102static struct phy_driver RTL8211B_driver = {
103 .name = "RealTek RTL8211B",
104 .uid = 0x1cc910,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530105 .mask = 0xffffff,
Andy Fleming9082eea2011-04-07 21:56:05 -0500106 .features = PHY_GBIT_FEATURES,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530107 .config = &rtl8211x_config,
108 .startup = &rtl8211x_startup,
109 .shutdown = &genphy_shutdown,
110};
111
112/* Support for RTL8211E-VB-CG, RTL8211E-VL-CG and RTL8211EG-VB-CG PHYs */
113static struct phy_driver RTL8211E_driver = {
114 .name = "RealTek RTL8211E",
115 .uid = 0x1cc915,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530116 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530117 .features = PHY_GBIT_FEATURES,
118 .config = &rtl8211x_config,
119 .startup = &rtl8211x_startup,
120 .shutdown = &genphy_shutdown,
121};
122
123/* Support for RTL8211DN PHY */
124static struct phy_driver RTL8211DN_driver = {
125 .name = "RealTek RTL8211DN",
126 .uid = 0x1cc914,
Bhupesh Sharma42205042013-09-01 04:40:52 +0530127 .mask = 0xffffff,
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530128 .features = PHY_GBIT_FEATURES,
129 .config = &rtl8211x_config,
130 .startup = &rtl8211x_startup,
Andy Fleming9082eea2011-04-07 21:56:05 -0500131 .shutdown = &genphy_shutdown,
132};
133
134int phy_realtek_init(void)
135{
136 phy_register(&RTL8211B_driver);
Bhupesh Sharmac624d162013-07-18 13:58:20 +0530137 phy_register(&RTL8211E_driver);
138 phy_register(&RTL8211DN_driver);
Andy Fleming9082eea2011-04-07 21:56:05 -0500139
140 return 0;
141}