1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Microchip Technology
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/delay.h>
10 /* External Register Control Register */
11 #define LAN87XX_EXT_REG_CTL (0x14)
12 #define LAN87XX_EXT_REG_CTL_RD_CTL (0x1000)
13 #define LAN87XX_EXT_REG_CTL_WR_CTL (0x0800)
15 /* External Register Read Data Register */
16 #define LAN87XX_EXT_REG_RD_DATA (0x15)
18 /* External Register Write Data Register */
19 #define LAN87XX_EXT_REG_WR_DATA (0x16)
21 /* Interrupt Source Register */
22 #define LAN87XX_INTERRUPT_SOURCE (0x18)
24 /* Interrupt Mask Register */
25 #define LAN87XX_INTERRUPT_MASK (0x19)
26 #define LAN87XX_MASK_LINK_UP (0x0004)
27 #define LAN87XX_MASK_LINK_DOWN (0x0002)
29 /* phyaccess nested types */
30 #define PHYACC_ATTR_MODE_READ 0
31 #define PHYACC_ATTR_MODE_WRITE 1
32 #define PHYACC_ATTR_MODE_MODIFY 2
34 #define PHYACC_ATTR_BANK_SMI 0
35 #define PHYACC_ATTR_BANK_MISC 1
36 #define PHYACC_ATTR_BANK_PCS 2
37 #define PHYACC_ATTR_BANK_AFE 3
38 #define PHYACC_ATTR_BANK_MAX 7
41 #define DRIVER_DESC "Microchip LAN87XX T1 PHY driver"
43 struct access_ereg_val {
51 static int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
57 if (mode > PHYACC_ATTR_MODE_WRITE || bank > PHYACC_ATTR_BANK_MAX)
60 if (bank == PHYACC_ATTR_BANK_SMI) {
61 if (mode == PHYACC_ATTR_MODE_WRITE)
62 rc = phy_write(phydev, offset, val);
64 rc = phy_read(phydev, offset);
68 if (mode == PHYACC_ATTR_MODE_WRITE) {
69 ereg = LAN87XX_EXT_REG_CTL_WR_CTL;
70 rc = phy_write(phydev, LAN87XX_EXT_REG_WR_DATA, val);
74 ereg = LAN87XX_EXT_REG_CTL_RD_CTL;
77 ereg |= (bank << 8) | offset;
79 rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, ereg);
83 if (mode == PHYACC_ATTR_MODE_READ)
84 rc = phy_read(phydev, LAN87XX_EXT_REG_RD_DATA);
89 static int access_ereg_modify_changed(struct phy_device *phydev,
90 u8 bank, u8 offset, u16 val, u16 mask)
94 if (bank > PHYACC_ATTR_BANK_MAX)
97 rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, bank, offset, val);
101 new = val | (rc & (mask ^ 0xFFFF));
102 rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE, bank, offset, new);
107 static int lan87xx_phy_init(struct phy_device *phydev)
109 static const struct access_ereg_val init[] = {
110 /* TX Amplitude = 5 */
111 {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_AFE, 0x0B,
113 /* Clear SMI interrupts */
114 {PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI, 0x18,
116 /* Clear MISC interrupts */
117 {PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC, 0x08,
119 /* Turn on TC10 Ring Oscillator (ROSC) */
120 {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_MISC, 0x20,
122 /* WUR Detect Length to 1.2uS, LPC Detect Length to 1.09uS */
123 {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_PCS, 0x20,
125 /* Wake_In Debounce Length to 39uS, Wake_Out Length to 79uS */
126 {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x21,
128 /* Enable Auto Wake Forward to Wake_Out, ROSC on, Sleep,
129 * and Wake_In to wake PHY
131 {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x20,
133 /* Enable WUP Auto Fwd, Enable Wake on MDI, Wakeup Debouncer
136 {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x24,
139 {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_SMI, 0x1A,
144 /* Start manual initialization procedures in Managed Mode */
145 rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
146 0x1a, 0x0000, 0x0100);
150 /* Soft Reset the SMI block */
151 rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
152 0x00, 0x8000, 0x8000);
156 /* Check to see if the self-clearing bit is cleared */
157 usleep_range(1000, 2000);
158 rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
159 PHYACC_ATTR_BANK_SMI, 0x00, 0);
162 if ((rc & 0x8000) != 0)
165 /* PHY Initialization */
166 for (i = 0; i < ARRAY_SIZE(init); i++) {
167 if (init[i].mode == PHYACC_ATTR_MODE_MODIFY) {
168 rc = access_ereg_modify_changed(phydev, init[i].bank,
173 rc = access_ereg(phydev, init[i].mode, init[i].bank,
174 init[i].offset, init[i].val);
183 static int lan87xx_phy_config_intr(struct phy_device *phydev)
187 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
188 /* unmask all source and clear them before enable */
189 rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, 0x7FFF);
190 rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
191 val = LAN87XX_MASK_LINK_UP | LAN87XX_MASK_LINK_DOWN;
192 rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
194 rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
198 rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
201 return rc < 0 ? rc : 0;
204 static irqreturn_t lan87xx_handle_interrupt(struct phy_device *phydev)
208 irq_status = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
209 if (irq_status < 0) {
217 phy_trigger_machine(phydev);
222 static int lan87xx_config_init(struct phy_device *phydev)
224 int rc = lan87xx_phy_init(phydev);
226 return rc < 0 ? rc : 0;
229 static struct phy_driver microchip_t1_phy_driver[] = {
231 .phy_id = 0x0007c150,
232 .phy_id_mask = 0xfffffff0,
233 .name = "Microchip LAN87xx T1",
235 .features = PHY_BASIC_T1_FEATURES,
237 .config_init = lan87xx_config_init,
239 .config_intr = lan87xx_phy_config_intr,
240 .handle_interrupt = lan87xx_handle_interrupt,
242 .suspend = genphy_suspend,
243 .resume = genphy_resume,
247 module_phy_driver(microchip_t1_phy_driver);
249 static struct mdio_device_id __maybe_unused microchip_t1_tbl[] = {
250 { 0x0007c150, 0xfffffff0 },
254 MODULE_DEVICE_TABLE(mdio, microchip_t1_tbl);
256 MODULE_AUTHOR(DRIVER_AUTHOR);
257 MODULE_DESCRIPTION(DRIVER_DESC);
258 MODULE_LICENSE("GPL");