2 * drivers/net/phy/realtek.c
4 * Driver for Realtek PHYs
6 * Author: Johnson Leung <r58129@freescale.com>
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/bitops.h>
17 #include <linux/phy.h>
18 #include <linux/module.h>
20 #define RTL821x_PHYSR 0x11
21 #define RTL821x_PHYSR_DUPLEX BIT(13)
22 #define RTL821x_PHYSR_SPEED GENMASK(15, 14)
24 #define RTL821x_INER 0x12
25 #define RTL8211B_INER_INIT 0x6400
26 #define RTL8211E_INER_LINK_STATUS BIT(10)
27 #define RTL8211F_INER_LINK_STATUS BIT(4)
29 #define RTL821x_INSR 0x13
31 #define RTL821x_PAGE_SELECT 0x1f
33 #define RTL8211F_INSR 0x1d
35 #define RTL8211F_TX_DELAY BIT(8)
37 #define RTL8201F_ISR 0x1e
38 #define RTL8201F_IER 0x13
40 #define RTL8366RB_POWER_SAVE 0x15
41 #define RTL8366RB_POWER_SAVE_ON BIT(12)
43 MODULE_DESCRIPTION("Realtek PHY driver");
44 MODULE_AUTHOR("Johnson Leung");
45 MODULE_LICENSE("GPL");
47 static int rtl821x_read_page(struct phy_device *phydev)
49 return __phy_read(phydev, RTL821x_PAGE_SELECT);
52 static int rtl821x_write_page(struct phy_device *phydev, int page)
54 return __phy_write(phydev, RTL821x_PAGE_SELECT, page);
57 static int rtl8201_ack_interrupt(struct phy_device *phydev)
61 err = phy_read(phydev, RTL8201F_ISR);
63 return (err < 0) ? err : 0;
66 static int rtl821x_ack_interrupt(struct phy_device *phydev)
70 err = phy_read(phydev, RTL821x_INSR);
72 return (err < 0) ? err : 0;
75 static int rtl8211f_ack_interrupt(struct phy_device *phydev)
79 err = phy_read_paged(phydev, 0xa43, RTL8211F_INSR);
81 return (err < 0) ? err : 0;
84 static int rtl8201_config_intr(struct phy_device *phydev)
88 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
89 val = BIT(13) | BIT(12) | BIT(11);
93 return phy_write_paged(phydev, 0x7, RTL8201F_IER, val);
96 static int rtl8211b_config_intr(struct phy_device *phydev)
100 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
101 err = phy_write(phydev, RTL821x_INER,
104 err = phy_write(phydev, RTL821x_INER, 0);
109 static int rtl8211e_config_intr(struct phy_device *phydev)
113 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
114 err = phy_write(phydev, RTL821x_INER,
115 RTL8211E_INER_LINK_STATUS);
117 err = phy_write(phydev, RTL821x_INER, 0);
122 static int rtl8211f_config_intr(struct phy_device *phydev)
126 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
127 val = RTL8211F_INER_LINK_STATUS;
131 return phy_write_paged(phydev, 0xa42, RTL821x_INER, val);
134 static int rtl8211_config_aneg(struct phy_device *phydev)
138 ret = genphy_config_aneg(phydev);
142 /* Quirk was copied from vendor driver. Unfortunately it includes no
143 * description of the magic numbers.
145 if (phydev->speed == SPEED_100 && phydev->autoneg == AUTONEG_DISABLE) {
146 phy_write(phydev, 0x17, 0x2138);
147 phy_write(phydev, 0x0e, 0x0260);
149 phy_write(phydev, 0x17, 0x2108);
150 phy_write(phydev, 0x0e, 0x0000);
156 static int rtl8211c_config_init(struct phy_device *phydev)
158 /* RTL8211C has an issue when operating in Gigabit slave mode */
159 phy_set_bits(phydev, MII_CTRL1000,
160 CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
162 return genphy_config_init(phydev);
165 static int rtl8211f_config_init(struct phy_device *phydev)
170 ret = genphy_config_init(phydev);
174 /* enable TX-delay for rgmii-id and rgmii-txid, otherwise disable it */
175 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
176 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
177 val = RTL8211F_TX_DELAY;
179 return phy_modify_paged(phydev, 0xd08, 0x11, RTL8211F_TX_DELAY, val);
182 static int rtl8211b_suspend(struct phy_device *phydev)
184 phy_write(phydev, MII_MMD_DATA, BIT(9));
186 return genphy_suspend(phydev);
189 static int rtl8211b_resume(struct phy_device *phydev)
191 phy_write(phydev, MII_MMD_DATA, 0);
193 return genphy_resume(phydev);
196 static int rtl8366rb_config_init(struct phy_device *phydev)
200 ret = genphy_config_init(phydev);
204 ret = phy_set_bits(phydev, RTL8366RB_POWER_SAVE,
205 RTL8366RB_POWER_SAVE_ON);
207 dev_err(&phydev->mdio.dev,
208 "error enabling power management\n");
214 static struct phy_driver realtek_drvs[] = {
216 PHY_ID_MATCH_EXACT(0x00008201),
217 .name = "RTL8201CP Ethernet",
218 .features = PHY_BASIC_FEATURES,
220 PHY_ID_MATCH_EXACT(0x001cc816),
221 .name = "RTL8201F Fast Ethernet",
222 .features = PHY_BASIC_FEATURES,
223 .ack_interrupt = &rtl8201_ack_interrupt,
224 .config_intr = &rtl8201_config_intr,
225 .suspend = genphy_suspend,
226 .resume = genphy_resume,
227 .read_page = rtl821x_read_page,
228 .write_page = rtl821x_write_page,
230 PHY_ID_MATCH_EXACT(0x001cc910),
231 .name = "RTL8211 Gigabit Ethernet",
232 .features = PHY_GBIT_FEATURES,
233 .config_aneg = rtl8211_config_aneg,
234 .read_mmd = &genphy_read_mmd_unsupported,
235 .write_mmd = &genphy_write_mmd_unsupported,
237 PHY_ID_MATCH_EXACT(0x001cc912),
238 .name = "RTL8211B Gigabit Ethernet",
239 .features = PHY_GBIT_FEATURES,
240 .ack_interrupt = &rtl821x_ack_interrupt,
241 .config_intr = &rtl8211b_config_intr,
242 .read_mmd = &genphy_read_mmd_unsupported,
243 .write_mmd = &genphy_write_mmd_unsupported,
244 .suspend = rtl8211b_suspend,
245 .resume = rtl8211b_resume,
247 PHY_ID_MATCH_EXACT(0x001cc913),
248 .name = "RTL8211C Gigabit Ethernet",
249 .features = PHY_GBIT_FEATURES,
250 .config_init = rtl8211c_config_init,
251 .read_mmd = &genphy_read_mmd_unsupported,
252 .write_mmd = &genphy_write_mmd_unsupported,
254 PHY_ID_MATCH_EXACT(0x001cc914),
255 .name = "RTL8211DN Gigabit Ethernet",
256 .features = PHY_GBIT_FEATURES,
257 .ack_interrupt = rtl821x_ack_interrupt,
258 .config_intr = rtl8211e_config_intr,
259 .suspend = genphy_suspend,
260 .resume = genphy_resume,
262 PHY_ID_MATCH_EXACT(0x001cc915),
263 .name = "RTL8211E Gigabit Ethernet",
264 .features = PHY_GBIT_FEATURES,
265 .ack_interrupt = &rtl821x_ack_interrupt,
266 .config_intr = &rtl8211e_config_intr,
267 .suspend = genphy_suspend,
268 .resume = genphy_resume,
270 PHY_ID_MATCH_EXACT(0x001cc916),
271 .name = "RTL8211F Gigabit Ethernet",
272 .features = PHY_GBIT_FEATURES,
273 .config_init = &rtl8211f_config_init,
274 .ack_interrupt = &rtl8211f_ack_interrupt,
275 .config_intr = &rtl8211f_config_intr,
276 .suspend = genphy_suspend,
277 .resume = genphy_resume,
278 .read_page = rtl821x_read_page,
279 .write_page = rtl821x_write_page,
281 PHY_ID_MATCH_EXACT(0x001cc961),
282 .name = "RTL8366RB Gigabit Ethernet",
283 .features = PHY_GBIT_FEATURES,
284 .config_init = &rtl8366rb_config_init,
285 .suspend = genphy_suspend,
286 .resume = genphy_resume,
290 module_phy_driver(realtek_drvs);
292 static const struct mdio_device_id __maybe_unused realtek_tbl[] = {
293 { PHY_ID_MATCH_VENDOR(0x001cc800) },
297 MODULE_DEVICE_TABLE(mdio, realtek_tbl);