1*f3b766d9SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
2*f3b766d9SVladimir Oltean /* Driver for 100BASE-TX PHY embedded into NXP SJA1110 switch
3*f3b766d9SVladimir Oltean *
4*f3b766d9SVladimir Oltean * Copyright 2022-2023 NXP
5*f3b766d9SVladimir Oltean */
6*f3b766d9SVladimir Oltean
7*f3b766d9SVladimir Oltean #include <linux/kernel.h>
8*f3b766d9SVladimir Oltean #include <linux/mii.h>
9*f3b766d9SVladimir Oltean #include <linux/module.h>
10*f3b766d9SVladimir Oltean #include <linux/phy.h>
11*f3b766d9SVladimir Oltean
12*f3b766d9SVladimir Oltean #define PHY_ID_CBTX_SJA1110 0x001bb020
13*f3b766d9SVladimir Oltean
14*f3b766d9SVladimir Oltean /* Registers */
15*f3b766d9SVladimir Oltean #define CBTX_MODE_CTRL_STAT 0x11
16*f3b766d9SVladimir Oltean #define CBTX_PDOWN_CTRL 0x18
17*f3b766d9SVladimir Oltean #define CBTX_RX_ERR_COUNTER 0x1a
18*f3b766d9SVladimir Oltean #define CBTX_IRQ_STAT 0x1d
19*f3b766d9SVladimir Oltean #define CBTX_IRQ_ENABLE 0x1e
20*f3b766d9SVladimir Oltean
21*f3b766d9SVladimir Oltean /* Fields */
22*f3b766d9SVladimir Oltean #define CBTX_MODE_CTRL_STAT_AUTO_MDIX_EN BIT(7)
23*f3b766d9SVladimir Oltean #define CBTX_MODE_CTRL_STAT_MDIX_MODE BIT(6)
24*f3b766d9SVladimir Oltean
25*f3b766d9SVladimir Oltean #define CBTX_PDOWN_CTL_TRUE_PDOWN BIT(0)
26*f3b766d9SVladimir Oltean
27*f3b766d9SVladimir Oltean #define CBTX_IRQ_ENERGYON BIT(7)
28*f3b766d9SVladimir Oltean #define CBTX_IRQ_AN_COMPLETE BIT(6)
29*f3b766d9SVladimir Oltean #define CBTX_IRQ_REM_FAULT BIT(5)
30*f3b766d9SVladimir Oltean #define CBTX_IRQ_LINK_DOWN BIT(4)
31*f3b766d9SVladimir Oltean #define CBTX_IRQ_AN_LP_ACK BIT(3)
32*f3b766d9SVladimir Oltean #define CBTX_IRQ_PARALLEL_DETECT_FAULT BIT(2)
33*f3b766d9SVladimir Oltean #define CBTX_IRQ_AN_PAGE_RECV BIT(1)
34*f3b766d9SVladimir Oltean
cbtx_soft_reset(struct phy_device * phydev)35*f3b766d9SVladimir Oltean static int cbtx_soft_reset(struct phy_device *phydev)
36*f3b766d9SVladimir Oltean {
37*f3b766d9SVladimir Oltean int ret;
38*f3b766d9SVladimir Oltean
39*f3b766d9SVladimir Oltean /* Can't soft reset unless we remove PHY from true power down mode */
40*f3b766d9SVladimir Oltean ret = phy_clear_bits(phydev, CBTX_PDOWN_CTRL,
41*f3b766d9SVladimir Oltean CBTX_PDOWN_CTL_TRUE_PDOWN);
42*f3b766d9SVladimir Oltean if (ret)
43*f3b766d9SVladimir Oltean return ret;
44*f3b766d9SVladimir Oltean
45*f3b766d9SVladimir Oltean return genphy_soft_reset(phydev);
46*f3b766d9SVladimir Oltean }
47*f3b766d9SVladimir Oltean
cbtx_config_init(struct phy_device * phydev)48*f3b766d9SVladimir Oltean static int cbtx_config_init(struct phy_device *phydev)
49*f3b766d9SVladimir Oltean {
50*f3b766d9SVladimir Oltean /* Wait for cbtx_config_aneg() to kick in and apply this */
51*f3b766d9SVladimir Oltean phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
52*f3b766d9SVladimir Oltean
53*f3b766d9SVladimir Oltean return 0;
54*f3b766d9SVladimir Oltean }
55*f3b766d9SVladimir Oltean
cbtx_mdix_status(struct phy_device * phydev)56*f3b766d9SVladimir Oltean static int cbtx_mdix_status(struct phy_device *phydev)
57*f3b766d9SVladimir Oltean {
58*f3b766d9SVladimir Oltean int ret;
59*f3b766d9SVladimir Oltean
60*f3b766d9SVladimir Oltean ret = phy_read(phydev, CBTX_MODE_CTRL_STAT);
61*f3b766d9SVladimir Oltean if (ret < 0)
62*f3b766d9SVladimir Oltean return ret;
63*f3b766d9SVladimir Oltean
64*f3b766d9SVladimir Oltean if (ret & CBTX_MODE_CTRL_STAT_MDIX_MODE)
65*f3b766d9SVladimir Oltean phydev->mdix = ETH_TP_MDI_X;
66*f3b766d9SVladimir Oltean else
67*f3b766d9SVladimir Oltean phydev->mdix = ETH_TP_MDI;
68*f3b766d9SVladimir Oltean
69*f3b766d9SVladimir Oltean return 0;
70*f3b766d9SVladimir Oltean }
71*f3b766d9SVladimir Oltean
cbtx_read_status(struct phy_device * phydev)72*f3b766d9SVladimir Oltean static int cbtx_read_status(struct phy_device *phydev)
73*f3b766d9SVladimir Oltean {
74*f3b766d9SVladimir Oltean int ret;
75*f3b766d9SVladimir Oltean
76*f3b766d9SVladimir Oltean ret = cbtx_mdix_status(phydev);
77*f3b766d9SVladimir Oltean if (ret)
78*f3b766d9SVladimir Oltean return ret;
79*f3b766d9SVladimir Oltean
80*f3b766d9SVladimir Oltean return genphy_read_status(phydev);
81*f3b766d9SVladimir Oltean }
82*f3b766d9SVladimir Oltean
cbtx_mdix_config(struct phy_device * phydev)83*f3b766d9SVladimir Oltean static int cbtx_mdix_config(struct phy_device *phydev)
84*f3b766d9SVladimir Oltean {
85*f3b766d9SVladimir Oltean int ret;
86*f3b766d9SVladimir Oltean
87*f3b766d9SVladimir Oltean switch (phydev->mdix_ctrl) {
88*f3b766d9SVladimir Oltean case ETH_TP_MDI_AUTO:
89*f3b766d9SVladimir Oltean return phy_set_bits(phydev, CBTX_MODE_CTRL_STAT,
90*f3b766d9SVladimir Oltean CBTX_MODE_CTRL_STAT_AUTO_MDIX_EN);
91*f3b766d9SVladimir Oltean case ETH_TP_MDI:
92*f3b766d9SVladimir Oltean ret = phy_clear_bits(phydev, CBTX_MODE_CTRL_STAT,
93*f3b766d9SVladimir Oltean CBTX_MODE_CTRL_STAT_AUTO_MDIX_EN);
94*f3b766d9SVladimir Oltean if (ret)
95*f3b766d9SVladimir Oltean return ret;
96*f3b766d9SVladimir Oltean
97*f3b766d9SVladimir Oltean return phy_clear_bits(phydev, CBTX_MODE_CTRL_STAT,
98*f3b766d9SVladimir Oltean CBTX_MODE_CTRL_STAT_MDIX_MODE);
99*f3b766d9SVladimir Oltean case ETH_TP_MDI_X:
100*f3b766d9SVladimir Oltean ret = phy_clear_bits(phydev, CBTX_MODE_CTRL_STAT,
101*f3b766d9SVladimir Oltean CBTX_MODE_CTRL_STAT_AUTO_MDIX_EN);
102*f3b766d9SVladimir Oltean if (ret)
103*f3b766d9SVladimir Oltean return ret;
104*f3b766d9SVladimir Oltean
105*f3b766d9SVladimir Oltean return phy_set_bits(phydev, CBTX_MODE_CTRL_STAT,
106*f3b766d9SVladimir Oltean CBTX_MODE_CTRL_STAT_MDIX_MODE);
107*f3b766d9SVladimir Oltean }
108*f3b766d9SVladimir Oltean
109*f3b766d9SVladimir Oltean return 0;
110*f3b766d9SVladimir Oltean }
111*f3b766d9SVladimir Oltean
cbtx_config_aneg(struct phy_device * phydev)112*f3b766d9SVladimir Oltean static int cbtx_config_aneg(struct phy_device *phydev)
113*f3b766d9SVladimir Oltean {
114*f3b766d9SVladimir Oltean int ret;
115*f3b766d9SVladimir Oltean
116*f3b766d9SVladimir Oltean ret = cbtx_mdix_config(phydev);
117*f3b766d9SVladimir Oltean if (ret)
118*f3b766d9SVladimir Oltean return ret;
119*f3b766d9SVladimir Oltean
120*f3b766d9SVladimir Oltean return genphy_config_aneg(phydev);
121*f3b766d9SVladimir Oltean }
122*f3b766d9SVladimir Oltean
cbtx_ack_interrupts(struct phy_device * phydev)123*f3b766d9SVladimir Oltean static int cbtx_ack_interrupts(struct phy_device *phydev)
124*f3b766d9SVladimir Oltean {
125*f3b766d9SVladimir Oltean return phy_read(phydev, CBTX_IRQ_STAT);
126*f3b766d9SVladimir Oltean }
127*f3b766d9SVladimir Oltean
cbtx_config_intr(struct phy_device * phydev)128*f3b766d9SVladimir Oltean static int cbtx_config_intr(struct phy_device *phydev)
129*f3b766d9SVladimir Oltean {
130*f3b766d9SVladimir Oltean int ret;
131*f3b766d9SVladimir Oltean
132*f3b766d9SVladimir Oltean if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
133*f3b766d9SVladimir Oltean ret = cbtx_ack_interrupts(phydev);
134*f3b766d9SVladimir Oltean if (ret < 0)
135*f3b766d9SVladimir Oltean return ret;
136*f3b766d9SVladimir Oltean
137*f3b766d9SVladimir Oltean ret = phy_write(phydev, CBTX_IRQ_ENABLE, CBTX_IRQ_LINK_DOWN |
138*f3b766d9SVladimir Oltean CBTX_IRQ_AN_COMPLETE | CBTX_IRQ_ENERGYON);
139*f3b766d9SVladimir Oltean if (ret)
140*f3b766d9SVladimir Oltean return ret;
141*f3b766d9SVladimir Oltean } else {
142*f3b766d9SVladimir Oltean ret = phy_write(phydev, CBTX_IRQ_ENABLE, 0);
143*f3b766d9SVladimir Oltean if (ret)
144*f3b766d9SVladimir Oltean return ret;
145*f3b766d9SVladimir Oltean
146*f3b766d9SVladimir Oltean ret = cbtx_ack_interrupts(phydev);
147*f3b766d9SVladimir Oltean if (ret < 0)
148*f3b766d9SVladimir Oltean return ret;
149*f3b766d9SVladimir Oltean }
150*f3b766d9SVladimir Oltean
151*f3b766d9SVladimir Oltean return 0;
152*f3b766d9SVladimir Oltean }
153*f3b766d9SVladimir Oltean
cbtx_handle_interrupt(struct phy_device * phydev)154*f3b766d9SVladimir Oltean static irqreturn_t cbtx_handle_interrupt(struct phy_device *phydev)
155*f3b766d9SVladimir Oltean {
156*f3b766d9SVladimir Oltean int irq_stat, irq_enabled;
157*f3b766d9SVladimir Oltean
158*f3b766d9SVladimir Oltean irq_stat = cbtx_ack_interrupts(phydev);
159*f3b766d9SVladimir Oltean if (irq_stat < 0) {
160*f3b766d9SVladimir Oltean phy_error(phydev);
161*f3b766d9SVladimir Oltean return IRQ_NONE;
162*f3b766d9SVladimir Oltean }
163*f3b766d9SVladimir Oltean
164*f3b766d9SVladimir Oltean irq_enabled = phy_read(phydev, CBTX_IRQ_ENABLE);
165*f3b766d9SVladimir Oltean if (irq_enabled < 0) {
166*f3b766d9SVladimir Oltean phy_error(phydev);
167*f3b766d9SVladimir Oltean return IRQ_NONE;
168*f3b766d9SVladimir Oltean }
169*f3b766d9SVladimir Oltean
170*f3b766d9SVladimir Oltean if (!(irq_enabled & irq_stat))
171*f3b766d9SVladimir Oltean return IRQ_NONE;
172*f3b766d9SVladimir Oltean
173*f3b766d9SVladimir Oltean phy_trigger_machine(phydev);
174*f3b766d9SVladimir Oltean
175*f3b766d9SVladimir Oltean return IRQ_HANDLED;
176*f3b766d9SVladimir Oltean }
177*f3b766d9SVladimir Oltean
cbtx_get_sset_count(struct phy_device * phydev)178*f3b766d9SVladimir Oltean static int cbtx_get_sset_count(struct phy_device *phydev)
179*f3b766d9SVladimir Oltean {
180*f3b766d9SVladimir Oltean return 1;
181*f3b766d9SVladimir Oltean }
182*f3b766d9SVladimir Oltean
cbtx_get_strings(struct phy_device * phydev,u8 * data)183*f3b766d9SVladimir Oltean static void cbtx_get_strings(struct phy_device *phydev, u8 *data)
184*f3b766d9SVladimir Oltean {
185*f3b766d9SVladimir Oltean strncpy(data, "100btx_rx_err", ETH_GSTRING_LEN);
186*f3b766d9SVladimir Oltean }
187*f3b766d9SVladimir Oltean
cbtx_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)188*f3b766d9SVladimir Oltean static void cbtx_get_stats(struct phy_device *phydev,
189*f3b766d9SVladimir Oltean struct ethtool_stats *stats, u64 *data)
190*f3b766d9SVladimir Oltean {
191*f3b766d9SVladimir Oltean int ret;
192*f3b766d9SVladimir Oltean
193*f3b766d9SVladimir Oltean ret = phy_read(phydev, CBTX_RX_ERR_COUNTER);
194*f3b766d9SVladimir Oltean data[0] = (ret < 0) ? U64_MAX : ret;
195*f3b766d9SVladimir Oltean }
196*f3b766d9SVladimir Oltean
197*f3b766d9SVladimir Oltean static struct phy_driver cbtx_driver[] = {
198*f3b766d9SVladimir Oltean {
199*f3b766d9SVladimir Oltean PHY_ID_MATCH_MODEL(PHY_ID_CBTX_SJA1110),
200*f3b766d9SVladimir Oltean .name = "NXP CBTX (SJA1110)",
201*f3b766d9SVladimir Oltean /* PHY_BASIC_FEATURES */
202*f3b766d9SVladimir Oltean .soft_reset = cbtx_soft_reset,
203*f3b766d9SVladimir Oltean .config_init = cbtx_config_init,
204*f3b766d9SVladimir Oltean .suspend = genphy_suspend,
205*f3b766d9SVladimir Oltean .resume = genphy_resume,
206*f3b766d9SVladimir Oltean .config_intr = cbtx_config_intr,
207*f3b766d9SVladimir Oltean .handle_interrupt = cbtx_handle_interrupt,
208*f3b766d9SVladimir Oltean .read_status = cbtx_read_status,
209*f3b766d9SVladimir Oltean .config_aneg = cbtx_config_aneg,
210*f3b766d9SVladimir Oltean .get_sset_count = cbtx_get_sset_count,
211*f3b766d9SVladimir Oltean .get_strings = cbtx_get_strings,
212*f3b766d9SVladimir Oltean .get_stats = cbtx_get_stats,
213*f3b766d9SVladimir Oltean },
214*f3b766d9SVladimir Oltean };
215*f3b766d9SVladimir Oltean
216*f3b766d9SVladimir Oltean module_phy_driver(cbtx_driver);
217*f3b766d9SVladimir Oltean
218*f3b766d9SVladimir Oltean static struct mdio_device_id __maybe_unused cbtx_tbl[] = {
219*f3b766d9SVladimir Oltean { PHY_ID_MATCH_MODEL(PHY_ID_CBTX_SJA1110) },
220*f3b766d9SVladimir Oltean { },
221*f3b766d9SVladimir Oltean };
222*f3b766d9SVladimir Oltean
223*f3b766d9SVladimir Oltean MODULE_DEVICE_TABLE(mdio, cbtx_tbl);
224*f3b766d9SVladimir Oltean
225*f3b766d9SVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <vladimir.oltean@nxp.com>");
226*f3b766d9SVladimir Oltean MODULE_DESCRIPTION("NXP CBTX PHY driver");
227*f3b766d9SVladimir Oltean MODULE_LICENSE("GPL");
228