xref: /openbmc/linux/drivers/net/pcs/pcs-mtk-lynxi.c (revision c4f461a1)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018-2019 MediaTek Inc.
3 /* A library for MediaTek SGMII circuit
4  *
5  * Author: Sean Wang <sean.wang@mediatek.com>
6  * Author: Alexander Couzens <lynxis@fe80.eu>
7  * Author: Daniel Golle <daniel@makrotopia.org>
8  *
9  */
10 
11 #include <linux/mdio.h>
12 #include <linux/of.h>
13 #include <linux/pcs/pcs-mtk-lynxi.h>
14 #include <linux/phylink.h>
15 #include <linux/regmap.h>
16 
17 /* SGMII subsystem config registers */
18 /* BMCR (low 16) BMSR (high 16) */
19 #define SGMSYS_PCS_CONTROL_1		0x0
20 #define SGMII_BMCR			GENMASK(15, 0)
21 #define SGMII_BMSR			GENMASK(31, 16)
22 
23 #define SGMSYS_PCS_DEVICE_ID		0x4
24 #define SGMII_LYNXI_DEV_ID		0x4d544950
25 
26 #define SGMSYS_PCS_ADVERTISE		0x8
27 #define SGMII_ADVERTISE			GENMASK(15, 0)
28 #define SGMII_LPA			GENMASK(31, 16)
29 
30 #define SGMSYS_PCS_SCRATCH		0x14
31 #define SGMII_DEV_VERSION		GENMASK(31, 16)
32 
33 /* Register to programmable link timer, the unit in 2 * 8ns */
34 #define SGMSYS_PCS_LINK_TIMER		0x18
35 #define SGMII_LINK_TIMER_MASK		GENMASK(19, 0)
36 #define SGMII_LINK_TIMER_VAL(ns)	FIELD_PREP(SGMII_LINK_TIMER_MASK, \
37 						   ((ns) / 2 / 8))
38 
39 /* Register to control remote fault */
40 #define SGMSYS_SGMII_MODE		0x20
41 #define SGMII_IF_MODE_SGMII		BIT(0)
42 #define SGMII_SPEED_DUPLEX_AN		BIT(1)
43 #define SGMII_SPEED_MASK		GENMASK(3, 2)
44 #define SGMII_SPEED_10			FIELD_PREP(SGMII_SPEED_MASK, 0)
45 #define SGMII_SPEED_100			FIELD_PREP(SGMII_SPEED_MASK, 1)
46 #define SGMII_SPEED_1000		FIELD_PREP(SGMII_SPEED_MASK, 2)
47 #define SGMII_DUPLEX_HALF		BIT(4)
48 #define SGMII_REMOTE_FAULT_DIS		BIT(8)
49 
50 /* Register to reset SGMII design */
51 #define SGMSYS_RESERVED_0		0x34
52 #define SGMII_SW_RESET			BIT(0)
53 
54 /* Register to set SGMII speed, ANA RG_ Control Signals III */
55 #define SGMII_PHY_SPEED_MASK		GENMASK(3, 2)
56 #define SGMII_PHY_SPEED_1_25G		FIELD_PREP(SGMII_PHY_SPEED_MASK, 0)
57 #define SGMII_PHY_SPEED_3_125G		FIELD_PREP(SGMII_PHY_SPEED_MASK, 1)
58 
59 /* Register to power up QPHY */
60 #define SGMSYS_QPHY_PWR_STATE_CTRL	0xe8
61 #define	SGMII_PHYA_PWD			BIT(4)
62 
63 /* Register to QPHY wrapper control */
64 #define SGMSYS_QPHY_WRAP_CTRL		0xec
65 #define SGMII_PN_SWAP_MASK		GENMASK(1, 0)
66 #define SGMII_PN_SWAP_TX_RX		(BIT(0) | BIT(1))
67 
68 /* struct mtk_pcs_lynxi -  This structure holds each sgmii regmap andassociated
69  *                         data
70  * @regmap:                The register map pointing at the range used to setup
71  *                         SGMII modes
72  * @dev:                   Pointer to device owning the PCS
73  * @ana_rgc3:              The offset of register ANA_RGC3 relative to regmap
74  * @interface:             Currently configured interface mode
75  * @pcs:                   Phylink PCS structure
76  * @flags:                 Flags indicating hardware properties
77  */
78 struct mtk_pcs_lynxi {
79 	struct regmap		*regmap;
80 	u32			ana_rgc3;
81 	phy_interface_t		interface;
82 	struct			phylink_pcs pcs;
83 	u32			flags;
84 };
85 
86 static struct mtk_pcs_lynxi *pcs_to_mtk_pcs_lynxi(struct phylink_pcs *pcs)
87 {
88 	return container_of(pcs, struct mtk_pcs_lynxi, pcs);
89 }
90 
91 static void mtk_pcs_lynxi_get_state(struct phylink_pcs *pcs,
92 				    struct phylink_link_state *state)
93 {
94 	struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
95 	unsigned int bm, adv;
96 
97 	/* Read the BMSR and LPA */
98 	regmap_read(mpcs->regmap, SGMSYS_PCS_CONTROL_1, &bm);
99 	regmap_read(mpcs->regmap, SGMSYS_PCS_ADVERTISE, &adv);
100 
101 	phylink_mii_c22_pcs_decode_state(state, FIELD_GET(SGMII_BMSR, bm),
102 					 FIELD_GET(SGMII_LPA, adv));
103 }
104 
105 static int mtk_pcs_lynxi_config(struct phylink_pcs *pcs, unsigned int mode,
106 				phy_interface_t interface,
107 				const unsigned long *advertising,
108 				bool permit_pause_to_mac)
109 {
110 	struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
111 	bool mode_changed = false, changed, use_an;
112 	unsigned int rgc3, sgm_mode, bmcr;
113 	int advertise, link_timer;
114 
115 	advertise = phylink_mii_c22_pcs_encode_advertisement(interface,
116 							     advertising);
117 	if (advertise < 0)
118 		return advertise;
119 
120 	/* Clearing IF_MODE_BIT0 switches the PCS to BASE-X mode, and
121 	 * we assume that fixes it's speed at bitrate = line rate (in
122 	 * other words, 1000Mbps or 2500Mbps).
123 	 */
124 	if (interface == PHY_INTERFACE_MODE_SGMII) {
125 		sgm_mode = SGMII_IF_MODE_SGMII;
126 		if (phylink_autoneg_inband(mode)) {
127 			sgm_mode |= SGMII_REMOTE_FAULT_DIS |
128 				    SGMII_SPEED_DUPLEX_AN;
129 			use_an = true;
130 		} else {
131 			use_an = false;
132 		}
133 	} else if (phylink_autoneg_inband(mode)) {
134 		/* 1000base-X or 2500base-X autoneg */
135 		sgm_mode = SGMII_REMOTE_FAULT_DIS;
136 		use_an = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
137 					   advertising);
138 	} else {
139 		/* 1000base-X or 2500base-X without autoneg */
140 		sgm_mode = 0;
141 		use_an = false;
142 	}
143 
144 	if (use_an)
145 		bmcr = BMCR_ANENABLE;
146 	else
147 		bmcr = 0;
148 
149 	if (mpcs->interface != interface) {
150 		link_timer = phylink_get_link_timer_ns(interface);
151 		if (link_timer < 0)
152 			return link_timer;
153 
154 		/* PHYA power down */
155 		regmap_set_bits(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL,
156 				SGMII_PHYA_PWD);
157 
158 		/* Reset SGMII PCS state */
159 		regmap_set_bits(mpcs->regmap, SGMSYS_RESERVED_0,
160 				SGMII_SW_RESET);
161 
162 		if (mpcs->flags & MTK_SGMII_FLAG_PN_SWAP)
163 			regmap_update_bits(mpcs->regmap, SGMSYS_QPHY_WRAP_CTRL,
164 					   SGMII_PN_SWAP_MASK,
165 					   SGMII_PN_SWAP_TX_RX);
166 
167 		if (interface == PHY_INTERFACE_MODE_2500BASEX)
168 			rgc3 = SGMII_PHY_SPEED_3_125G;
169 		else
170 			rgc3 = SGMII_PHY_SPEED_1_25G;
171 
172 		/* Configure the underlying interface speed */
173 		regmap_update_bits(mpcs->regmap, mpcs->ana_rgc3,
174 				   SGMII_PHY_SPEED_MASK, rgc3);
175 
176 		/* Setup the link timer */
177 		regmap_write(mpcs->regmap, SGMSYS_PCS_LINK_TIMER,
178 			     SGMII_LINK_TIMER_VAL(link_timer));
179 
180 		mpcs->interface = interface;
181 		mode_changed = true;
182 	}
183 
184 	/* Update the advertisement, noting whether it has changed */
185 	regmap_update_bits_check(mpcs->regmap, SGMSYS_PCS_ADVERTISE,
186 				 SGMII_ADVERTISE, advertise, &changed);
187 
188 	/* Update the sgmsys mode register */
189 	regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE,
190 			   SGMII_REMOTE_FAULT_DIS | SGMII_SPEED_DUPLEX_AN |
191 			   SGMII_IF_MODE_SGMII, sgm_mode);
192 
193 	/* Update the BMCR */
194 	regmap_update_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1,
195 			   BMCR_ANENABLE, bmcr);
196 
197 	/* Release PHYA power down state
198 	 * Only removing bit SGMII_PHYA_PWD isn't enough.
199 	 * There are cases when the SGMII_PHYA_PWD register contains 0x9 which
200 	 * prevents SGMII from working. The SGMII still shows link but no traffic
201 	 * can flow. Writing 0x0 to the PHYA_PWD register fix the issue. 0x0 was
202 	 * taken from a good working state of the SGMII interface.
203 	 * Unknown how much the QPHY needs but it is racy without a sleep.
204 	 * Tested on mt7622 & mt7986.
205 	 */
206 	usleep_range(50, 100);
207 	regmap_write(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL, 0);
208 
209 	return changed || mode_changed;
210 }
211 
212 static void mtk_pcs_lynxi_restart_an(struct phylink_pcs *pcs)
213 {
214 	struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
215 
216 	regmap_set_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1, BMCR_ANRESTART);
217 }
218 
219 static void mtk_pcs_lynxi_link_up(struct phylink_pcs *pcs, unsigned int mode,
220 				  phy_interface_t interface, int speed,
221 				  int duplex)
222 {
223 	struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
224 	unsigned int sgm_mode;
225 
226 	if (!phylink_autoneg_inband(mode)) {
227 		/* Force the speed and duplex setting */
228 		if (speed == SPEED_10)
229 			sgm_mode = SGMII_SPEED_10;
230 		else if (speed == SPEED_100)
231 			sgm_mode = SGMII_SPEED_100;
232 		else
233 			sgm_mode = SGMII_SPEED_1000;
234 
235 		if (duplex != DUPLEX_FULL)
236 			sgm_mode |= SGMII_DUPLEX_HALF;
237 
238 		regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE,
239 				   SGMII_DUPLEX_HALF | SGMII_SPEED_MASK,
240 				   sgm_mode);
241 	}
242 }
243 
244 static const struct phylink_pcs_ops mtk_pcs_lynxi_ops = {
245 	.pcs_get_state = mtk_pcs_lynxi_get_state,
246 	.pcs_config = mtk_pcs_lynxi_config,
247 	.pcs_an_restart = mtk_pcs_lynxi_restart_an,
248 	.pcs_link_up = mtk_pcs_lynxi_link_up,
249 };
250 
251 struct phylink_pcs *mtk_pcs_lynxi_create(struct device *dev,
252 					 struct regmap *regmap, u32 ana_rgc3,
253 					 u32 flags)
254 {
255 	struct mtk_pcs_lynxi *mpcs;
256 	u32 id, ver;
257 	int ret;
258 
259 	ret = regmap_read(regmap, SGMSYS_PCS_DEVICE_ID, &id);
260 	if (ret < 0)
261 		return NULL;
262 
263 	if (id != SGMII_LYNXI_DEV_ID) {
264 		dev_err(dev, "unknown PCS device id %08x\n", id);
265 		return NULL;
266 	}
267 
268 	ret = regmap_read(regmap, SGMSYS_PCS_SCRATCH, &ver);
269 	if (ret < 0)
270 		return NULL;
271 
272 	ver = FIELD_GET(SGMII_DEV_VERSION, ver);
273 	if (ver != 0x1) {
274 		dev_err(dev, "unknown PCS device version %04x\n", ver);
275 		return NULL;
276 	}
277 
278 	dev_dbg(dev, "MediaTek LynxI SGMII PCS (id 0x%08x, ver 0x%04x)\n", id,
279 		ver);
280 
281 	mpcs = kzalloc(sizeof(*mpcs), GFP_KERNEL);
282 	if (!mpcs)
283 		return NULL;
284 
285 	mpcs->ana_rgc3 = ana_rgc3;
286 	mpcs->regmap = regmap;
287 	mpcs->flags = flags;
288 	mpcs->pcs.ops = &mtk_pcs_lynxi_ops;
289 	mpcs->pcs.poll = true;
290 	mpcs->interface = PHY_INTERFACE_MODE_NA;
291 
292 	return &mpcs->pcs;
293 }
294 EXPORT_SYMBOL(mtk_pcs_lynxi_create);
295 
296 void mtk_pcs_lynxi_destroy(struct phylink_pcs *pcs)
297 {
298 	if (!pcs)
299 		return;
300 
301 	kfree(pcs_to_mtk_pcs_lynxi(pcs));
302 }
303 EXPORT_SYMBOL(mtk_pcs_lynxi_destroy);
304 
305 MODULE_LICENSE("GPL");
306