xref: /linux/drivers/net/phy/micrel.c (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/net/phy/micrel.c
4  *
5  * Driver for Micrel PHYs
6  *
7  * Author: David J. Choi
8  *
9  * Copyright (c) 2010-2013 Micrel, Inc.
10  * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
11  *
12  * Support : Micrel Phys:
13  *		Giga phys: ksz9021, ksz9031, ksz9131, lan8841, lan8814
14  *		100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
15  *			   ksz8021, ksz8031, ksz8051,
16  *			   ksz8081, ksz8091,
17  *			   ksz8061,
18  *		Switch : ksz8873, ksz886x
19  *			 ksz9477, lan8804
20  */
21 
22 #include <linux/bitfield.h>
23 #include <linux/ethtool_netlink.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/phy.h>
27 #include <linux/micrel_phy.h>
28 #include <linux/of.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/ptp_clock_kernel.h>
32 #include <linux/ptp_clock.h>
33 #include <linux/ptp_classify.h>
34 #include <linux/net_tstamp.h>
35 #include <linux/gpio/consumer.h>
36 
37 #include "phylib.h"
38 
39 /* Operation Mode Strap Override */
40 #define MII_KSZPHY_OMSO				0x16
41 #define KSZPHY_OMSO_FACTORY_TEST		BIT(15)
42 #define KSZPHY_OMSO_B_CAST_OFF			BIT(9)
43 #define KSZPHY_OMSO_NAND_TREE_ON		BIT(5)
44 #define KSZPHY_OMSO_RMII_OVERRIDE		BIT(1)
45 #define KSZPHY_OMSO_MII_OVERRIDE		BIT(0)
46 
47 /* general Interrupt control/status reg in vendor specific block. */
48 #define MII_KSZPHY_INTCS			0x1B
49 #define KSZPHY_INTCS_JABBER			BIT(15)
50 #define KSZPHY_INTCS_RECEIVE_ERR		BIT(14)
51 #define KSZPHY_INTCS_PAGE_RECEIVE		BIT(13)
52 #define KSZPHY_INTCS_PARELLEL			BIT(12)
53 #define KSZPHY_INTCS_LINK_PARTNER_ACK		BIT(11)
54 #define KSZPHY_INTCS_LINK_DOWN			BIT(10)
55 #define KSZPHY_INTCS_REMOTE_FAULT		BIT(9)
56 #define KSZPHY_INTCS_LINK_UP			BIT(8)
57 #define KSZPHY_INTCS_ALL			(KSZPHY_INTCS_LINK_UP |\
58 						KSZPHY_INTCS_LINK_DOWN)
59 #define KSZPHY_INTCS_LINK_DOWN_STATUS		BIT(2)
60 #define KSZPHY_INTCS_LINK_UP_STATUS		BIT(0)
61 #define KSZPHY_INTCS_STATUS			(KSZPHY_INTCS_LINK_DOWN_STATUS |\
62 						 KSZPHY_INTCS_LINK_UP_STATUS)
63 
64 /* LinkMD Control/Status */
65 #define KSZ8081_LMD				0x1d
66 #define KSZ8081_LMD_ENABLE_TEST			BIT(15)
67 #define KSZ8081_LMD_STAT_NORMAL			0
68 #define KSZ8081_LMD_STAT_OPEN			1
69 #define KSZ8081_LMD_STAT_SHORT			2
70 #define KSZ8081_LMD_STAT_FAIL			3
71 #define KSZ8081_LMD_STAT_MASK			GENMASK(14, 13)
72 /* Short cable (<10 meter) has been detected by LinkMD */
73 #define KSZ8081_LMD_SHORT_INDICATOR		BIT(12)
74 #define KSZ8081_LMD_DELTA_TIME_MASK		GENMASK(8, 0)
75 
76 #define KSZ9x31_LMD				0x12
77 #define KSZ9x31_LMD_VCT_EN			BIT(15)
78 #define KSZ9x31_LMD_VCT_DIS_TX			BIT(14)
79 #define KSZ9x31_LMD_VCT_PAIR(n)			(((n) & 0x3) << 12)
80 #define KSZ9x31_LMD_VCT_SEL_RESULT		0
81 #define KSZ9x31_LMD_VCT_SEL_THRES_HI		BIT(10)
82 #define KSZ9x31_LMD_VCT_SEL_THRES_LO		BIT(11)
83 #define KSZ9x31_LMD_VCT_SEL_MASK		GENMASK(11, 10)
84 #define KSZ9x31_LMD_VCT_ST_NORMAL		0
85 #define KSZ9x31_LMD_VCT_ST_OPEN			1
86 #define KSZ9x31_LMD_VCT_ST_SHORT		2
87 #define KSZ9x31_LMD_VCT_ST_FAIL			3
88 #define KSZ9x31_LMD_VCT_ST_MASK			GENMASK(9, 8)
89 #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID	BIT(7)
90 #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG	BIT(6)
91 #define KSZ9x31_LMD_VCT_DATA_MASK100		BIT(5)
92 #define KSZ9x31_LMD_VCT_DATA_NLP_FLP		BIT(4)
93 #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK	GENMASK(3, 2)
94 #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK	GENMASK(1, 0)
95 #define KSZ9x31_LMD_VCT_DATA_MASK		GENMASK(7, 0)
96 
97 #define KSZPHY_WIRE_PAIR_MASK			0x3
98 
99 #define LAN8814_CABLE_DIAG			0x12
100 #define LAN8814_CABLE_DIAG_STAT_MASK		GENMASK(9, 8)
101 #define LAN8814_CABLE_DIAG_VCT_DATA_MASK	GENMASK(7, 0)
102 #define LAN8814_PAIR_BIT_SHIFT			12
103 
104 #define LAN8814_SKUS				0xB
105 
106 #define LAN8814_WIRE_PAIR_MASK			0xF
107 
108 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
109 #define LAN8814_INTC				0x18
110 #define LAN8814_INTS				0x1B
111 
112 #define LAN8814_INT_FLF				BIT(15)
113 #define LAN8814_INT_LINK_DOWN			BIT(2)
114 #define LAN8814_INT_LINK_UP			BIT(0)
115 #define LAN8814_INT_LINK			(LAN8814_INT_LINK_UP |\
116 						 LAN8814_INT_LINK_DOWN)
117 
118 #define LAN8814_INTR_CTRL_REG			0x34
119 #define LAN8814_INTR_CTRL_REG_POLARITY		BIT(1)
120 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE	BIT(0)
121 
122 #define LAN8814_EEE_STATE			0x38
123 #define LAN8814_EEE_STATE_MASK2P5P		BIT(10)
124 
125 #define LAN8814_PD_CONTROLS			0x9d
126 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK	GENMASK(3, 0)
127 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL	0xb
128 
129 /* Represents 1ppm adjustment in 2^32 format with
130  * each nsec contains 4 clock cycles.
131  * The value is calculated as following: (1/1000000)/((2^-32)/4)
132  */
133 #define LAN8814_1PPM_FORMAT			17179
134 
135 /* Represents 1ppm adjustment in 2^32 format with
136  * each nsec contains 8 clock cycles.
137  * The value is calculated as following: (1/1000000)/((2^-32)/8)
138  */
139 #define LAN8841_1PPM_FORMAT			34360
140 
141 #define PTP_RX_VERSION				0x0248
142 #define PTP_TX_VERSION				0x0288
143 #define PTP_MAX_VERSION(x)			(((x) & GENMASK(7, 0)) << 8)
144 #define PTP_MIN_VERSION(x)			((x) & GENMASK(7, 0))
145 
146 #define PTP_RX_MOD				0x024F
147 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
148 #define PTP_RX_TIMESTAMP_EN			0x024D
149 #define PTP_TX_TIMESTAMP_EN			0x028D
150 
151 #define PTP_TIMESTAMP_EN_SYNC_			BIT(0)
152 #define PTP_TIMESTAMP_EN_DREQ_			BIT(1)
153 #define PTP_TIMESTAMP_EN_PDREQ_			BIT(2)
154 #define PTP_TIMESTAMP_EN_PDRES_			BIT(3)
155 
156 #define PTP_TX_PARSE_L2_ADDR_EN			0x0284
157 #define PTP_RX_PARSE_L2_ADDR_EN			0x0244
158 
159 #define PTP_TX_PARSE_IP_ADDR_EN			0x0285
160 #define PTP_RX_PARSE_IP_ADDR_EN			0x0245
161 #define LTC_HARD_RESET				0x023F
162 #define LTC_HARD_RESET_				BIT(0)
163 
164 #define TSU_HARD_RESET				0x02C1
165 #define TSU_HARD_RESET_				BIT(0)
166 
167 #define PTP_CMD_CTL				0x0200
168 #define PTP_CMD_CTL_PTP_DISABLE_		BIT(0)
169 #define PTP_CMD_CTL_PTP_ENABLE_			BIT(1)
170 #define PTP_CMD_CTL_PTP_CLOCK_READ_		BIT(3)
171 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_		BIT(4)
172 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_		BIT(5)
173 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_		BIT(6)
174 
175 #define PTP_COMMON_INT_ENA			0x0204
176 #define PTP_COMMON_INT_ENA_GPIO_CAP_EN		BIT(2)
177 
178 #define PTP_CLOCK_SET_SEC_HI			0x0205
179 #define PTP_CLOCK_SET_SEC_MID			0x0206
180 #define PTP_CLOCK_SET_SEC_LO			0x0207
181 #define PTP_CLOCK_SET_NS_HI			0x0208
182 #define PTP_CLOCK_SET_NS_LO			0x0209
183 
184 #define PTP_CLOCK_READ_SEC_HI			0x0229
185 #define PTP_CLOCK_READ_SEC_MID			0x022A
186 #define PTP_CLOCK_READ_SEC_LO			0x022B
187 #define PTP_CLOCK_READ_NS_HI			0x022C
188 #define PTP_CLOCK_READ_NS_LO			0x022D
189 
190 #define PTP_GPIO_SEL				0x0230
191 #define PTP_GPIO_SEL_GPIO_SEL(pin)		((pin) << 8)
192 #define PTP_GPIO_CAP_MAP_LO			0x0232
193 
194 #define PTP_GPIO_CAP_EN				0x0233
195 #define PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	BIT(gpio)
196 #define PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
197 
198 #define PTP_GPIO_RE_LTC_SEC_HI_CAP		0x0235
199 #define PTP_GPIO_RE_LTC_SEC_LO_CAP		0x0236
200 #define PTP_GPIO_RE_LTC_NS_HI_CAP		0x0237
201 #define PTP_GPIO_RE_LTC_NS_LO_CAP		0x0238
202 #define PTP_GPIO_FE_LTC_SEC_HI_CAP		0x0239
203 #define PTP_GPIO_FE_LTC_SEC_LO_CAP		0x023A
204 #define PTP_GPIO_FE_LTC_NS_HI_CAP		0x023B
205 #define PTP_GPIO_FE_LTC_NS_LO_CAP		0x023C
206 
207 #define PTP_GPIO_CAP_STS			0x023D
208 #define PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(gpio)	BIT(gpio)
209 #define PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(gpio)	(BIT(gpio) << 8)
210 
211 #define PTP_OPERATING_MODE			0x0241
212 #define PTP_OPERATING_MODE_STANDALONE_		BIT(0)
213 
214 #define PTP_TX_MOD				0x028F
215 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_	BIT(12)
216 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
217 
218 #define PTP_RX_PARSE_CONFIG			0x0242
219 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
220 #define PTP_RX_PARSE_CONFIG_IPV4_EN_		BIT(1)
221 #define PTP_RX_PARSE_CONFIG_IPV6_EN_		BIT(2)
222 
223 #define PTP_TX_PARSE_CONFIG			0x0282
224 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
225 #define PTP_TX_PARSE_CONFIG_IPV4_EN_		BIT(1)
226 #define PTP_TX_PARSE_CONFIG_IPV6_EN_		BIT(2)
227 
228 #define PTP_CLOCK_RATE_ADJ_HI			0x020C
229 #define PTP_CLOCK_RATE_ADJ_LO			0x020D
230 #define PTP_CLOCK_RATE_ADJ_DIR_			BIT(15)
231 
232 #define PTP_LTC_STEP_ADJ_HI			0x0212
233 #define PTP_LTC_STEP_ADJ_LO			0x0213
234 #define PTP_LTC_STEP_ADJ_DIR_			BIT(15)
235 
236 #define LAN8814_INTR_STS_REG			0x0033
237 #define LAN8814_INTR_STS_REG_1588_TSU0_		BIT(0)
238 #define LAN8814_INTR_STS_REG_1588_TSU1_		BIT(1)
239 #define LAN8814_INTR_STS_REG_1588_TSU2_		BIT(2)
240 #define LAN8814_INTR_STS_REG_1588_TSU3_		BIT(3)
241 
242 #define PTP_CAP_INFO				0x022A
243 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val)	(((reg_val) & 0x0f00) >> 8)
244 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val)	((reg_val) & 0x000f)
245 
246 #define PTP_TX_EGRESS_SEC_HI			0x0296
247 #define PTP_TX_EGRESS_SEC_LO			0x0297
248 #define PTP_TX_EGRESS_NS_HI			0x0294
249 #define PTP_TX_EGRESS_NS_LO			0x0295
250 #define PTP_TX_MSG_HEADER2			0x0299
251 
252 #define PTP_RX_INGRESS_SEC_HI			0x0256
253 #define PTP_RX_INGRESS_SEC_LO			0x0257
254 #define PTP_RX_INGRESS_NS_HI			0x0254
255 #define PTP_RX_INGRESS_NS_LO			0x0255
256 #define PTP_RX_MSG_HEADER2			0x0259
257 
258 #define PTP_TSU_INT_EN				0x0200
259 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_	BIT(3)
260 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_		BIT(2)
261 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_	BIT(1)
262 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_		BIT(0)
263 
264 #define PTP_TSU_INT_STS				0x0201
265 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_	BIT(3)
266 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_		BIT(2)
267 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_	BIT(1)
268 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_		BIT(0)
269 
270 #define LAN8814_LED_CTRL_1			0x0
271 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_	BIT(6)
272 #define LAN8814_LED_CTRL_2			0x1
273 #define LAN8814_LED_CTRL_2_LED1_COM_DIS		BIT(8)
274 
275 /* PHY Control 1 */
276 #define MII_KSZPHY_CTRL_1			0x1e
277 #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
278 
279 /* PHY Control 2 / PHY Control (if no PHY Control 1) */
280 #define MII_KSZPHY_CTRL_2			0x1f
281 #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
282 /* bitmap of PHY register to set interrupt mode */
283 #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
284 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
285 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
286 #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
287 #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
288 #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
289 #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
290 
291 /* Write/read to/from extended registers */
292 #define MII_KSZPHY_EXTREG			0x0b
293 #define KSZPHY_EXTREG_WRITE			0x8000
294 
295 #define MII_KSZPHY_EXTREG_WRITE			0x0c
296 #define MII_KSZPHY_EXTREG_READ			0x0d
297 
298 /* Extended registers */
299 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
300 #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
301 #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
302 
303 #define PS_TO_REG				200
304 #define FIFO_SIZE				8
305 
306 #define LAN8814_PTP_GPIO_NUM			24
307 #define LAN8814_PTP_PEROUT_NUM			2
308 #define LAN8814_PTP_EXTTS_NUM			3
309 
310 #define LAN8814_BUFFER_TIME			2
311 
312 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
313 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
314 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
315 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
316 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
317 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
318 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
319 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
320 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
321 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
322 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
323 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
324 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
325 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
326 
327 #define LAN8814_GPIO_EN1			0x20
328 #define LAN8814_GPIO_EN2			0x21
329 #define LAN8814_GPIO_DIR1			0x22
330 #define LAN8814_GPIO_DIR2			0x23
331 #define LAN8814_GPIO_BUF1			0x24
332 #define LAN8814_GPIO_BUF2			0x25
333 
334 #define LAN8814_GPIO_EN_ADDR(pin) \
335 	((pin) > 15 ? LAN8814_GPIO_EN1 : LAN8814_GPIO_EN2)
336 #define LAN8814_GPIO_EN_BIT(pin)		BIT(pin)
337 #define LAN8814_GPIO_DIR_ADDR(pin) \
338 	((pin) > 15 ? LAN8814_GPIO_DIR1 : LAN8814_GPIO_DIR2)
339 #define LAN8814_GPIO_DIR_BIT(pin)		BIT(pin)
340 #define LAN8814_GPIO_BUF_ADDR(pin) \
341 	((pin) > 15 ? LAN8814_GPIO_BUF1 : LAN8814_GPIO_BUF2)
342 #define LAN8814_GPIO_BUF_BIT(pin)		BIT(pin)
343 
344 #define LAN8814_EVENT_A				0
345 #define LAN8814_EVENT_B				1
346 
347 #define LAN8814_PTP_GENERAL_CONFIG		0x0201
348 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event) \
349 	((event) ? GENMASK(11, 8) : GENMASK(7, 4))
350 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, value) \
351 	(((value) & GENMASK(3, 0)) << (4 + ((event) << 2)))
352 #define LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event) \
353 	((event) ? BIT(2) : BIT(0))
354 #define LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event) \
355 	((event) ? BIT(3) : BIT(1))
356 
357 #define LAN8814_PTP_CLOCK_TARGET_SEC_HI(event)	((event) ? 0x21F : 0x215)
358 #define LAN8814_PTP_CLOCK_TARGET_SEC_LO(event)	((event) ? 0x220 : 0x216)
359 #define LAN8814_PTP_CLOCK_TARGET_NS_HI(event)	((event) ? 0x221 : 0x217)
360 #define LAN8814_PTP_CLOCK_TARGET_NS_LO(event)	((event) ? 0x222 : 0x218)
361 
362 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event)	((event) ? 0x223 : 0x219)
363 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event)	((event) ? 0x224 : 0x21A)
364 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event)	((event) ? 0x225 : 0x21B)
365 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event)	((event) ? 0x226 : 0x21C)
366 
367 /* Delay used to get the second part from the LTC */
368 #define LAN8841_GET_SEC_LTC_DELAY		(500 * NSEC_PER_MSEC)
369 
370 #define LAN8842_REV_8832			0x8832
371 
372 #define LAN8814_REV_LAN8814			0x8814
373 #define LAN8814_REV_LAN8818			0x8818
374 
375 struct kszphy_hw_stat {
376 	const char *string;
377 	u8 reg;
378 	u8 bits;
379 };
380 
381 static struct kszphy_hw_stat kszphy_hw_stats[] = {
382 	{ "phy_receive_errors", 21, 16},
383 	{ "phy_idle_errors", 10, 8 },
384 };
385 
386 struct kszphy_type {
387 	u32 led_mode_reg;
388 	u16 interrupt_level_mask;
389 	u16 cable_diag_reg;
390 	unsigned long pair_mask;
391 	u16 disable_dll_tx_bit;
392 	u16 disable_dll_rx_bit;
393 	u16 disable_dll_mask;
394 	bool has_broadcast_disable;
395 	bool has_nand_tree_disable;
396 	bool has_rmii_ref_clk_sel;
397 };
398 
399 /* Shared structure between the PHYs of the same package. */
400 struct lan8814_shared_priv {
401 	struct phy_device *phydev;
402 	struct ptp_clock *ptp_clock;
403 	struct ptp_clock_info ptp_clock_info;
404 	struct ptp_pin_desc *pin_config;
405 
406 	/* Lock for ptp_clock */
407 	struct mutex shared_lock;
408 };
409 
410 struct lan8814_ptp_rx_ts {
411 	struct list_head list;
412 	u32 seconds;
413 	u32 nsec;
414 	u16 seq_id;
415 };
416 
417 struct kszphy_ptp_priv {
418 	struct mii_timestamper mii_ts;
419 	struct phy_device *phydev;
420 
421 	struct sk_buff_head tx_queue;
422 	struct sk_buff_head rx_queue;
423 
424 	struct list_head rx_ts_list;
425 	/* Lock for Rx ts fifo */
426 	spinlock_t rx_ts_lock;
427 
428 	int hwts_tx_type;
429 	enum hwtstamp_rx_filters rx_filter;
430 	int layer;
431 	int version;
432 
433 	struct ptp_clock *ptp_clock;
434 	struct ptp_clock_info ptp_clock_info;
435 	/* Lock for ptp_clock */
436 	struct mutex ptp_lock;
437 	struct ptp_pin_desc *pin_config;
438 
439 	s64 seconds;
440 	/* Lock for accessing seconds */
441 	spinlock_t seconds_lock;
442 };
443 
444 struct kszphy_phy_stats {
445 	u64 rx_err_pkt_cnt;
446 };
447 
448 struct kszphy_priv {
449 	struct kszphy_ptp_priv ptp_priv;
450 	const struct kszphy_type *type;
451 	struct clk *clk;
452 	int led_mode;
453 	u16 vct_ctrl1000;
454 	bool rmii_ref_clk_sel;
455 	bool rmii_ref_clk_sel_val;
456 	bool clk_enable;
457 	bool is_ptp_available;
458 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
459 	struct kszphy_phy_stats phy_stats;
460 };
461 
462 struct lan8842_phy_stats {
463 	u64 rx_packets;
464 	u64 rx_errors;
465 	u64 tx_packets;
466 	u64 tx_errors;
467 };
468 
469 struct lan8842_priv {
470 	struct lan8842_phy_stats phy_stats;
471 	struct kszphy_ptp_priv ptp_priv;
472 	u16 rev;
473 };
474 
475 struct lanphy_reg_data {
476 	int page;
477 	u16 addr;
478 	u16 val;
479 };
480 
481 static const struct kszphy_type lan8814_type = {
482 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
483 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
484 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
485 };
486 
487 static const struct kszphy_type ksz886x_type = {
488 	.cable_diag_reg		= KSZ8081_LMD,
489 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
490 };
491 
492 static const struct kszphy_type ksz8021_type = {
493 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
494 	.has_broadcast_disable	= true,
495 	.has_nand_tree_disable	= true,
496 	.has_rmii_ref_clk_sel	= true,
497 };
498 
499 static const struct kszphy_type ksz8041_type = {
500 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
501 };
502 
503 static const struct kszphy_type ksz8051_type = {
504 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
505 	.has_nand_tree_disable	= true,
506 };
507 
508 static const struct kszphy_type ksz8081_type = {
509 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
510 	.cable_diag_reg		= KSZ8081_LMD,
511 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
512 	.has_broadcast_disable	= true,
513 	.has_nand_tree_disable	= true,
514 	.has_rmii_ref_clk_sel	= true,
515 };
516 
517 static const struct kszphy_type ks8737_type = {
518 	.interrupt_level_mask	= BIT(14),
519 };
520 
521 static const struct kszphy_type ksz9021_type = {
522 	.interrupt_level_mask	= BIT(14),
523 };
524 
525 static const struct kszphy_type ksz9131_type = {
526 	.interrupt_level_mask	= BIT(14),
527 	.disable_dll_tx_bit	= BIT(12),
528 	.disable_dll_rx_bit	= BIT(12),
529 	.disable_dll_mask	= BIT_MASK(12),
530 };
531 
532 static const struct kszphy_type lan8841_type = {
533 	.disable_dll_tx_bit	= BIT(14),
534 	.disable_dll_rx_bit	= BIT(14),
535 	.disable_dll_mask	= BIT_MASK(14),
536 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
537 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
538 };
539 
kszphy_extended_write(struct phy_device * phydev,u32 regnum,u16 val)540 static int kszphy_extended_write(struct phy_device *phydev,
541 				u32 regnum, u16 val)
542 {
543 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
544 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
545 }
546 
kszphy_extended_read(struct phy_device * phydev,u32 regnum)547 static int kszphy_extended_read(struct phy_device *phydev,
548 				u32 regnum)
549 {
550 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
551 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
552 }
553 
kszphy_ack_interrupt(struct phy_device * phydev)554 static int kszphy_ack_interrupt(struct phy_device *phydev)
555 {
556 	/* bit[7..0] int status, which is a read and clear register. */
557 	int rc;
558 
559 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
560 
561 	return (rc < 0) ? rc : 0;
562 }
563 
kszphy_config_intr(struct phy_device * phydev)564 static int kszphy_config_intr(struct phy_device *phydev)
565 {
566 	const struct kszphy_type *type = phydev->drv->driver_data;
567 	int temp, err;
568 	u16 mask;
569 
570 	if (type && type->interrupt_level_mask)
571 		mask = type->interrupt_level_mask;
572 	else
573 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
574 
575 	/* set the interrupt pin active low */
576 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
577 	if (temp < 0)
578 		return temp;
579 	temp &= ~mask;
580 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
581 
582 	/* enable / disable interrupts */
583 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
584 		err = kszphy_ack_interrupt(phydev);
585 		if (err)
586 			return err;
587 
588 		err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL);
589 	} else {
590 		err = phy_write(phydev, MII_KSZPHY_INTCS, 0);
591 		if (err)
592 			return err;
593 
594 		err = kszphy_ack_interrupt(phydev);
595 	}
596 
597 	return err;
598 }
599 
kszphy_handle_interrupt(struct phy_device * phydev)600 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
601 {
602 	int irq_status;
603 
604 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
605 	if (irq_status < 0) {
606 		phy_error(phydev);
607 		return IRQ_NONE;
608 	}
609 
610 	if (!(irq_status & KSZPHY_INTCS_STATUS))
611 		return IRQ_NONE;
612 
613 	phy_trigger_machine(phydev);
614 
615 	return IRQ_HANDLED;
616 }
617 
kszphy_rmii_clk_sel(struct phy_device * phydev,bool val)618 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
619 {
620 	int ctrl;
621 
622 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
623 	if (ctrl < 0)
624 		return ctrl;
625 
626 	if (val)
627 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
628 	else
629 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
630 
631 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
632 }
633 
kszphy_setup_led(struct phy_device * phydev,u32 reg,int val)634 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
635 {
636 	int rc, temp, shift;
637 
638 	switch (reg) {
639 	case MII_KSZPHY_CTRL_1:
640 		shift = 14;
641 		break;
642 	case MII_KSZPHY_CTRL_2:
643 		shift = 4;
644 		break;
645 	default:
646 		return -EINVAL;
647 	}
648 
649 	temp = phy_read(phydev, reg);
650 	if (temp < 0) {
651 		rc = temp;
652 		goto out;
653 	}
654 
655 	temp &= ~(3 << shift);
656 	temp |= val << shift;
657 	rc = phy_write(phydev, reg, temp);
658 out:
659 	if (rc < 0)
660 		phydev_err(phydev, "failed to set led mode\n");
661 
662 	return rc;
663 }
664 
665 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
666  * unique (non-broadcast) address on a shared bus.
667  */
kszphy_broadcast_disable(struct phy_device * phydev)668 static int kszphy_broadcast_disable(struct phy_device *phydev)
669 {
670 	int ret;
671 
672 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
673 	if (ret < 0)
674 		goto out;
675 
676 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
677 out:
678 	if (ret)
679 		phydev_err(phydev, "failed to disable broadcast address\n");
680 
681 	return ret;
682 }
683 
kszphy_nand_tree_disable(struct phy_device * phydev)684 static int kszphy_nand_tree_disable(struct phy_device *phydev)
685 {
686 	int ret;
687 
688 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
689 	if (ret < 0)
690 		goto out;
691 
692 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
693 		return 0;
694 
695 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
696 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
697 out:
698 	if (ret)
699 		phydev_err(phydev, "failed to disable NAND tree mode\n");
700 
701 	return ret;
702 }
703 
704 /* Some config bits need to be set again on resume, handle them here. */
kszphy_config_reset(struct phy_device * phydev)705 static int kszphy_config_reset(struct phy_device *phydev)
706 {
707 	struct kszphy_priv *priv = phydev->priv;
708 	int ret;
709 
710 	if (priv->rmii_ref_clk_sel) {
711 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
712 		if (ret) {
713 			phydev_err(phydev,
714 				   "failed to set rmii reference clock\n");
715 			return ret;
716 		}
717 	}
718 
719 	if (priv->type && priv->led_mode >= 0)
720 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
721 
722 	return 0;
723 }
724 
kszphy_config_init(struct phy_device * phydev)725 static int kszphy_config_init(struct phy_device *phydev)
726 {
727 	struct kszphy_priv *priv = phydev->priv;
728 	const struct kszphy_type *type;
729 
730 	if (!priv)
731 		return 0;
732 
733 	type = priv->type;
734 
735 	if (type && type->has_broadcast_disable)
736 		kszphy_broadcast_disable(phydev);
737 
738 	if (type && type->has_nand_tree_disable)
739 		kszphy_nand_tree_disable(phydev);
740 
741 	return kszphy_config_reset(phydev);
742 }
743 
ksz8041_fiber_mode(struct phy_device * phydev)744 static int ksz8041_fiber_mode(struct phy_device *phydev)
745 {
746 	struct device_node *of_node = phydev->mdio.dev.of_node;
747 
748 	return of_property_read_bool(of_node, "micrel,fiber-mode");
749 }
750 
ksz8041_config_init(struct phy_device * phydev)751 static int ksz8041_config_init(struct phy_device *phydev)
752 {
753 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
754 
755 	/* Limit supported and advertised modes in fiber mode */
756 	if (ksz8041_fiber_mode(phydev)) {
757 		phydev->dev_flags |= MICREL_PHY_FXEN;
758 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
759 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
760 
761 		linkmode_and(phydev->supported, phydev->supported, mask);
762 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
763 				 phydev->supported);
764 		linkmode_and(phydev->advertising, phydev->advertising, mask);
765 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
766 				 phydev->advertising);
767 		phydev->autoneg = AUTONEG_DISABLE;
768 	}
769 
770 	return kszphy_config_init(phydev);
771 }
772 
ksz8041_config_aneg(struct phy_device * phydev)773 static int ksz8041_config_aneg(struct phy_device *phydev)
774 {
775 	/* Skip auto-negotiation in fiber mode */
776 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
777 		phydev->speed = SPEED_100;
778 		return 0;
779 	}
780 
781 	return genphy_config_aneg(phydev);
782 }
783 
ksz8051_ksz8795_match_phy_device(struct phy_device * phydev,const bool ksz_8051)784 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
785 					    const bool ksz_8051)
786 {
787 	int ret;
788 
789 	if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
790 		return 0;
791 
792 	ret = phy_read(phydev, MII_BMSR);
793 	if (ret < 0)
794 		return ret;
795 
796 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
797 	 * exact PHY ID. However, they can be told apart by the extended
798 	 * capability registers presence. The KSZ8051 PHY has them while
799 	 * the switch does not.
800 	 */
801 	ret &= BMSR_ERCAP;
802 	if (ksz_8051)
803 		return ret;
804 	else
805 		return !ret;
806 }
807 
ksz8051_match_phy_device(struct phy_device * phydev,const struct phy_driver * phydrv)808 static int ksz8051_match_phy_device(struct phy_device *phydev,
809 				    const struct phy_driver *phydrv)
810 {
811 	return ksz8051_ksz8795_match_phy_device(phydev, true);
812 }
813 
ksz8081_config_init(struct phy_device * phydev)814 static int ksz8081_config_init(struct phy_device *phydev)
815 {
816 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
817 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
818 	 * pull-down is missing, the factory test mode should be cleared by
819 	 * manually writing a 0.
820 	 */
821 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
822 
823 	return kszphy_config_init(phydev);
824 }
825 
ksz8081_config_mdix(struct phy_device * phydev,u8 ctrl)826 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
827 {
828 	u16 val;
829 
830 	switch (ctrl) {
831 	case ETH_TP_MDI:
832 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
833 		break;
834 	case ETH_TP_MDI_X:
835 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
836 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
837 		break;
838 	case ETH_TP_MDI_AUTO:
839 		val = 0;
840 		break;
841 	default:
842 		return 0;
843 	}
844 
845 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
846 			  KSZ8081_CTRL2_HP_MDIX |
847 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
848 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
849 			  KSZ8081_CTRL2_HP_MDIX | val);
850 }
851 
ksz8081_config_aneg(struct phy_device * phydev)852 static int ksz8081_config_aneg(struct phy_device *phydev)
853 {
854 	int ret;
855 
856 	ret = genphy_config_aneg(phydev);
857 	if (ret)
858 		return ret;
859 
860 	/* The MDI-X configuration is automatically changed by the PHY after
861 	 * switching from autoneg off to on. So, take MDI-X configuration under
862 	 * own control and set it after autoneg configuration was done.
863 	 */
864 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
865 }
866 
ksz8081_mdix_update(struct phy_device * phydev)867 static int ksz8081_mdix_update(struct phy_device *phydev)
868 {
869 	int ret;
870 
871 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
872 	if (ret < 0)
873 		return ret;
874 
875 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
876 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
877 			phydev->mdix_ctrl = ETH_TP_MDI_X;
878 		else
879 			phydev->mdix_ctrl = ETH_TP_MDI;
880 	} else {
881 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
882 	}
883 
884 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
885 	if (ret < 0)
886 		return ret;
887 
888 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
889 		phydev->mdix = ETH_TP_MDI;
890 	else
891 		phydev->mdix = ETH_TP_MDI_X;
892 
893 	return 0;
894 }
895 
ksz8081_read_status(struct phy_device * phydev)896 static int ksz8081_read_status(struct phy_device *phydev)
897 {
898 	int ret;
899 
900 	ret = ksz8081_mdix_update(phydev);
901 	if (ret < 0)
902 		return ret;
903 
904 	return genphy_read_status(phydev);
905 }
906 
ksz8061_config_init(struct phy_device * phydev)907 static int ksz8061_config_init(struct phy_device *phydev)
908 {
909 	int ret;
910 
911 	/* Chip can be powered down by the bootstrap code. */
912 	ret = phy_read(phydev, MII_BMCR);
913 	if (ret < 0)
914 		return ret;
915 	if (ret & BMCR_PDOWN) {
916 		ret = phy_write(phydev, MII_BMCR, ret & ~BMCR_PDOWN);
917 		if (ret < 0)
918 			return ret;
919 		usleep_range(1000, 2000);
920 	}
921 
922 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
923 	if (ret)
924 		return ret;
925 
926 	return kszphy_config_init(phydev);
927 }
928 
ksz8795_match_phy_device(struct phy_device * phydev,const struct phy_driver * phydrv)929 static int ksz8795_match_phy_device(struct phy_device *phydev,
930 				    const struct phy_driver *phydrv)
931 {
932 	return ksz8051_ksz8795_match_phy_device(phydev, false);
933 }
934 
ksz9021_load_values_from_of(struct phy_device * phydev,const struct device_node * of_node,u16 reg,const char * field1,const char * field2,const char * field3,const char * field4)935 static int ksz9021_load_values_from_of(struct phy_device *phydev,
936 				       const struct device_node *of_node,
937 				       u16 reg,
938 				       const char *field1, const char *field2,
939 				       const char *field3, const char *field4)
940 {
941 	int val1 = -1;
942 	int val2 = -2;
943 	int val3 = -3;
944 	int val4 = -4;
945 	int newval;
946 	int matches = 0;
947 
948 	if (!of_property_read_u32(of_node, field1, &val1))
949 		matches++;
950 
951 	if (!of_property_read_u32(of_node, field2, &val2))
952 		matches++;
953 
954 	if (!of_property_read_u32(of_node, field3, &val3))
955 		matches++;
956 
957 	if (!of_property_read_u32(of_node, field4, &val4))
958 		matches++;
959 
960 	if (!matches)
961 		return 0;
962 
963 	if (matches < 4)
964 		newval = kszphy_extended_read(phydev, reg);
965 	else
966 		newval = 0;
967 
968 	if (val1 != -1)
969 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
970 
971 	if (val2 != -2)
972 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
973 
974 	if (val3 != -3)
975 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
976 
977 	if (val4 != -4)
978 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
979 
980 	return kszphy_extended_write(phydev, reg, newval);
981 }
982 
ksz9021_config_init(struct phy_device * phydev)983 static int ksz9021_config_init(struct phy_device *phydev)
984 {
985 	const struct device_node *of_node;
986 	const struct device *dev_walker;
987 
988 	/* The Micrel driver has a deprecated option to place phy OF
989 	 * properties in the MAC node. Walk up the tree of devices to
990 	 * find a device with an OF node.
991 	 */
992 	dev_walker = &phydev->mdio.dev;
993 	do {
994 		of_node = dev_walker->of_node;
995 		dev_walker = dev_walker->parent;
996 
997 	} while (!of_node && dev_walker);
998 
999 	if (of_node) {
1000 		ksz9021_load_values_from_of(phydev, of_node,
1001 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
1002 				    "txen-skew-ps", "txc-skew-ps",
1003 				    "rxdv-skew-ps", "rxc-skew-ps");
1004 		ksz9021_load_values_from_of(phydev, of_node,
1005 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
1006 				    "rxd0-skew-ps", "rxd1-skew-ps",
1007 				    "rxd2-skew-ps", "rxd3-skew-ps");
1008 		ksz9021_load_values_from_of(phydev, of_node,
1009 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
1010 				    "txd0-skew-ps", "txd1-skew-ps",
1011 				    "txd2-skew-ps", "txd3-skew-ps");
1012 	}
1013 	return 0;
1014 }
1015 
1016 #define KSZ9031_PS_TO_REG		60
1017 
1018 /* Extended registers */
1019 /* MMD Address 0x0 */
1020 #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
1021 #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
1022 
1023 /* MMD Address 0x2 */
1024 #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
1025 #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
1026 #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
1027 
1028 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
1029 #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
1030 #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
1031 #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
1032 #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
1033 
1034 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
1035 #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
1036 #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
1037 #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
1038 #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
1039 
1040 #define MII_KSZ9031RN_CLK_PAD_SKEW	8
1041 #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
1042 #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
1043 
1044 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
1045  * provide different RGMII options we need to configure delay offset
1046  * for each pad relative to build in delay.
1047  */
1048 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
1049  * 1.80ns
1050  */
1051 #define RX_ID				0x7
1052 #define RX_CLK_ID			0x19
1053 
1054 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
1055  * internal 1.2ns delay.
1056  */
1057 #define RX_ND				0xc
1058 #define RX_CLK_ND			0x0
1059 
1060 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
1061 #define TX_ID				0x0
1062 #define TX_CLK_ID			0x1f
1063 
1064 /* set tx and tx_clk to "No delay adjustment" to keep 0ns
1065  * delay
1066  */
1067 #define TX_ND				0x7
1068 #define TX_CLK_ND			0xf
1069 
1070 /* MMD Address 0x1C */
1071 #define MII_KSZ9031RN_EDPD		0x23
1072 #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
1073 
ksz9031_set_loopback(struct phy_device * phydev,bool enable,int speed)1074 static int ksz9031_set_loopback(struct phy_device *phydev, bool enable,
1075 				int speed)
1076 {
1077 	u16 ctl = BMCR_LOOPBACK;
1078 	int val;
1079 
1080 	if (!enable)
1081 		return genphy_loopback(phydev, enable, 0);
1082 
1083 	if (speed == SPEED_10 || speed == SPEED_100 || speed == SPEED_1000)
1084 		phydev->speed = speed;
1085 	else if (speed)
1086 		return -EINVAL;
1087 	phydev->duplex = DUPLEX_FULL;
1088 
1089 	ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
1090 
1091 	phy_write(phydev, MII_BMCR, ctl);
1092 
1093 	return phy_read_poll_timeout(phydev, MII_BMSR, val, val & BMSR_LSTATUS,
1094 				     5000, 500000, true);
1095 }
1096 
ksz9031_of_load_skew_values(struct phy_device * phydev,const struct device_node * of_node,u16 reg,size_t field_sz,const char * field[],u8 numfields,bool * update)1097 static int ksz9031_of_load_skew_values(struct phy_device *phydev,
1098 				       const struct device_node *of_node,
1099 				       u16 reg, size_t field_sz,
1100 				       const char *field[], u8 numfields,
1101 				       bool *update)
1102 {
1103 	int val[4] = {-1, -2, -3, -4};
1104 	int matches = 0;
1105 	u16 mask;
1106 	u16 maxval;
1107 	u16 newval;
1108 	int i;
1109 
1110 	for (i = 0; i < numfields; i++)
1111 		if (!of_property_read_u32(of_node, field[i], val + i))
1112 			matches++;
1113 
1114 	if (!matches)
1115 		return 0;
1116 
1117 	*update |= true;
1118 
1119 	if (matches < numfields)
1120 		newval = phy_read_mmd(phydev, 2, reg);
1121 	else
1122 		newval = 0;
1123 
1124 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1125 	for (i = 0; i < numfields; i++)
1126 		if (val[i] != -(i + 1)) {
1127 			mask = 0xffff;
1128 			mask ^= maxval << (field_sz * i);
1129 			newval = (newval & mask) |
1130 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
1131 					<< (field_sz * i));
1132 		}
1133 
1134 	return phy_write_mmd(phydev, 2, reg, newval);
1135 }
1136 
1137 /* Center KSZ9031RNX FLP timing at 16ms. */
ksz9031_center_flp_timing(struct phy_device * phydev)1138 static int ksz9031_center_flp_timing(struct phy_device *phydev)
1139 {
1140 	int result;
1141 
1142 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
1143 			       0x0006);
1144 	if (result)
1145 		return result;
1146 
1147 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
1148 			       0x1A80);
1149 	if (result)
1150 		return result;
1151 
1152 	return genphy_restart_aneg(phydev);
1153 }
1154 
1155 /* Enable energy-detect power-down mode */
ksz9031_enable_edpd(struct phy_device * phydev)1156 static int ksz9031_enable_edpd(struct phy_device *phydev)
1157 {
1158 	int reg;
1159 
1160 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
1161 	if (reg < 0)
1162 		return reg;
1163 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
1164 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
1165 }
1166 
ksz9031_config_rgmii_delay(struct phy_device * phydev)1167 static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
1168 {
1169 	u16 rx, tx, rx_clk, tx_clk;
1170 	int ret;
1171 
1172 	switch (phydev->interface) {
1173 	case PHY_INTERFACE_MODE_RGMII:
1174 		tx = TX_ND;
1175 		tx_clk = TX_CLK_ND;
1176 		rx = RX_ND;
1177 		rx_clk = RX_CLK_ND;
1178 		break;
1179 	case PHY_INTERFACE_MODE_RGMII_ID:
1180 		tx = TX_ID;
1181 		tx_clk = TX_CLK_ID;
1182 		rx = RX_ID;
1183 		rx_clk = RX_CLK_ID;
1184 		break;
1185 	case PHY_INTERFACE_MODE_RGMII_RXID:
1186 		tx = TX_ND;
1187 		tx_clk = TX_CLK_ND;
1188 		rx = RX_ID;
1189 		rx_clk = RX_CLK_ID;
1190 		break;
1191 	case PHY_INTERFACE_MODE_RGMII_TXID:
1192 		tx = TX_ID;
1193 		tx_clk = TX_CLK_ID;
1194 		rx = RX_ND;
1195 		rx_clk = RX_CLK_ND;
1196 		break;
1197 	default:
1198 		return 0;
1199 	}
1200 
1201 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
1202 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
1203 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
1204 	if (ret < 0)
1205 		return ret;
1206 
1207 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1208 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1209 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1210 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1211 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1212 	if (ret < 0)
1213 		return ret;
1214 
1215 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1216 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1217 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1218 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1219 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1220 	if (ret < 0)
1221 		return ret;
1222 
1223 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1224 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1225 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1226 }
1227 
ksz9031_config_init(struct phy_device * phydev)1228 static int ksz9031_config_init(struct phy_device *phydev)
1229 {
1230 	const struct device_node *of_node;
1231 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
1232 	static const char *rx_data_skews[4] = {
1233 		"rxd0-skew-ps", "rxd1-skew-ps",
1234 		"rxd2-skew-ps", "rxd3-skew-ps"
1235 	};
1236 	static const char *tx_data_skews[4] = {
1237 		"txd0-skew-ps", "txd1-skew-ps",
1238 		"txd2-skew-ps", "txd3-skew-ps"
1239 	};
1240 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1241 	const struct device *dev_walker;
1242 	int result;
1243 
1244 	result = ksz9031_enable_edpd(phydev);
1245 	if (result < 0)
1246 		return result;
1247 
1248 	/* The Micrel driver has a deprecated option to place phy OF
1249 	 * properties in the MAC node. Walk up the tree of devices to
1250 	 * find a device with an OF node.
1251 	 */
1252 	dev_walker = &phydev->mdio.dev;
1253 	do {
1254 		of_node = dev_walker->of_node;
1255 		dev_walker = dev_walker->parent;
1256 	} while (!of_node && dev_walker);
1257 
1258 	if (of_node) {
1259 		bool update = false;
1260 
1261 		if (phy_interface_is_rgmii(phydev)) {
1262 			result = ksz9031_config_rgmii_delay(phydev);
1263 			if (result < 0)
1264 				return result;
1265 		}
1266 
1267 		ksz9031_of_load_skew_values(phydev, of_node,
1268 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1269 				clk_skews, 2, &update);
1270 
1271 		ksz9031_of_load_skew_values(phydev, of_node,
1272 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1273 				control_skews, 2, &update);
1274 
1275 		ksz9031_of_load_skew_values(phydev, of_node,
1276 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1277 				rx_data_skews, 4, &update);
1278 
1279 		ksz9031_of_load_skew_values(phydev, of_node,
1280 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1281 				tx_data_skews, 4, &update);
1282 
1283 		if (update && !phy_interface_is_rgmii(phydev))
1284 			phydev_warn(phydev,
1285 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1286 
1287 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1288 		 * When the device links in the 1000BASE-T slave mode only,
1289 		 * the optional 125MHz reference output clock (CLK125_NDO)
1290 		 * has wide duty cycle variation.
1291 		 *
1292 		 * The optional CLK125_NDO clock does not meet the RGMII
1293 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1294 		 * cannot be used directly by the MAC side for clocking
1295 		 * applications that have setup/hold time requirements on
1296 		 * rising and falling clock edges.
1297 		 *
1298 		 * Workaround:
1299 		 * Force the phy to be the master to receive a stable clock
1300 		 * which meets the duty cycle requirement.
1301 		 */
1302 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1303 			result = phy_read(phydev, MII_CTRL1000);
1304 			if (result < 0)
1305 				goto err_force_master;
1306 
1307 			/* enable master mode, config & prefer master */
1308 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1309 			result = phy_write(phydev, MII_CTRL1000, result);
1310 			if (result < 0)
1311 				goto err_force_master;
1312 		}
1313 	}
1314 
1315 	return ksz9031_center_flp_timing(phydev);
1316 
1317 err_force_master:
1318 	phydev_err(phydev, "failed to force the phy to master mode\n");
1319 	return result;
1320 }
1321 
1322 #define KSZ9131_SKEW_5BIT_MAX	2400
1323 #define KSZ9131_SKEW_4BIT_MAX	800
1324 #define KSZ9131_OFFSET		700
1325 #define KSZ9131_STEP		100
1326 
ksz9131_of_load_skew_values(struct phy_device * phydev,struct device_node * of_node,u16 reg,size_t field_sz,char * field[],u8 numfields)1327 static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1328 				       struct device_node *of_node,
1329 				       u16 reg, size_t field_sz,
1330 				       char *field[], u8 numfields)
1331 {
1332 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1333 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1334 	int skewval, skewmax = 0;
1335 	int matches = 0;
1336 	u16 maxval;
1337 	u16 newval;
1338 	u16 mask;
1339 	int i;
1340 
1341 	/* psec properties in dts should mean x pico seconds */
1342 	if (field_sz == 5)
1343 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1344 	else
1345 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1346 
1347 	for (i = 0; i < numfields; i++)
1348 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1349 			if (skewval < -KSZ9131_OFFSET)
1350 				skewval = -KSZ9131_OFFSET;
1351 			else if (skewval > skewmax)
1352 				skewval = skewmax;
1353 
1354 			val[i] = skewval + KSZ9131_OFFSET;
1355 			matches++;
1356 		}
1357 
1358 	if (!matches)
1359 		return 0;
1360 
1361 	if (matches < numfields)
1362 		newval = phy_read_mmd(phydev, 2, reg);
1363 	else
1364 		newval = 0;
1365 
1366 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1367 	for (i = 0; i < numfields; i++)
1368 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1369 			mask = 0xffff;
1370 			mask ^= maxval << (field_sz * i);
1371 			newval = (newval & mask) |
1372 				(((val[i] / KSZ9131_STEP) & maxval)
1373 					<< (field_sz * i));
1374 		}
1375 
1376 	return phy_write_mmd(phydev, 2, reg, newval);
1377 }
1378 
1379 #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1380 #define KSZ9131RN_RXC_DLL_CTRL		76
1381 #define KSZ9131RN_TXC_DLL_CTRL		77
1382 #define KSZ9131RN_DLL_ENABLE_DELAY	0
1383 
ksz9131_config_rgmii_delay(struct phy_device * phydev)1384 static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1385 {
1386 	const struct kszphy_type *type = phydev->drv->driver_data;
1387 	u16 rxcdll_val, txcdll_val;
1388 	int ret;
1389 
1390 	switch (phydev->interface) {
1391 	case PHY_INTERFACE_MODE_RGMII:
1392 		rxcdll_val = type->disable_dll_rx_bit;
1393 		txcdll_val = type->disable_dll_tx_bit;
1394 		break;
1395 	case PHY_INTERFACE_MODE_RGMII_ID:
1396 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1397 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1398 		break;
1399 	case PHY_INTERFACE_MODE_RGMII_RXID:
1400 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1401 		txcdll_val = type->disable_dll_tx_bit;
1402 		break;
1403 	case PHY_INTERFACE_MODE_RGMII_TXID:
1404 		rxcdll_val = type->disable_dll_rx_bit;
1405 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1406 		break;
1407 	default:
1408 		return 0;
1409 	}
1410 
1411 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1412 			     KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask,
1413 			     rxcdll_val);
1414 	if (ret < 0)
1415 		return ret;
1416 
1417 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1418 			      KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask,
1419 			      txcdll_val);
1420 }
1421 
1422 /* Silicon Errata DS80000693B
1423  *
1424  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
1425  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
1426  * according to the datasheet (off if there is no link).
1427  */
ksz9131_led_errata(struct phy_device * phydev)1428 static int ksz9131_led_errata(struct phy_device *phydev)
1429 {
1430 	int reg;
1431 
1432 	reg = phy_read_mmd(phydev, 2, 0);
1433 	if (reg < 0)
1434 		return reg;
1435 
1436 	if (!(reg & BIT(4)))
1437 		return 0;
1438 
1439 	return phy_set_bits(phydev, 0x1e, BIT(9));
1440 }
1441 
ksz9131_config_init(struct phy_device * phydev)1442 static int ksz9131_config_init(struct phy_device *phydev)
1443 {
1444 	struct device_node *of_node;
1445 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1446 	char *rx_data_skews[4] = {
1447 		"rxd0-skew-psec", "rxd1-skew-psec",
1448 		"rxd2-skew-psec", "rxd3-skew-psec"
1449 	};
1450 	char *tx_data_skews[4] = {
1451 		"txd0-skew-psec", "txd1-skew-psec",
1452 		"txd2-skew-psec", "txd3-skew-psec"
1453 	};
1454 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1455 	const struct device *dev_walker;
1456 	int ret;
1457 
1458 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1459 
1460 	dev_walker = &phydev->mdio.dev;
1461 	do {
1462 		of_node = dev_walker->of_node;
1463 		dev_walker = dev_walker->parent;
1464 	} while (!of_node && dev_walker);
1465 
1466 	if (!of_node)
1467 		return 0;
1468 
1469 	if (phy_interface_is_rgmii(phydev)) {
1470 		ret = ksz9131_config_rgmii_delay(phydev);
1471 		if (ret < 0)
1472 			return ret;
1473 	}
1474 
1475 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1476 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1477 					  clk_skews, 2);
1478 	if (ret < 0)
1479 		return ret;
1480 
1481 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1482 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1483 					  control_skews, 2);
1484 	if (ret < 0)
1485 		return ret;
1486 
1487 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1488 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1489 					  rx_data_skews, 4);
1490 	if (ret < 0)
1491 		return ret;
1492 
1493 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1494 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1495 					  tx_data_skews, 4);
1496 	if (ret < 0)
1497 		return ret;
1498 
1499 	ret = ksz9131_led_errata(phydev);
1500 	if (ret < 0)
1501 		return ret;
1502 
1503 	return 0;
1504 }
1505 
1506 #define MII_KSZ9131_AUTO_MDIX		0x1C
1507 #define MII_KSZ9131_AUTO_MDI_SET	BIT(7)
1508 #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF	BIT(6)
1509 #define MII_KSZ9131_DIG_AXAN_STS	0x14
1510 #define MII_KSZ9131_DIG_AXAN_STS_LINK_DET	BIT(14)
1511 #define MII_KSZ9131_DIG_AXAN_STS_A_SELECT	BIT(12)
1512 
ksz9131_mdix_update(struct phy_device * phydev)1513 static int ksz9131_mdix_update(struct phy_device *phydev)
1514 {
1515 	int ret;
1516 
1517 	if (phydev->mdix_ctrl != ETH_TP_MDI_AUTO) {
1518 		phydev->mdix = phydev->mdix_ctrl;
1519 	} else {
1520 		ret = phy_read(phydev, MII_KSZ9131_DIG_AXAN_STS);
1521 		if (ret < 0)
1522 			return ret;
1523 
1524 		if (ret & MII_KSZ9131_DIG_AXAN_STS_LINK_DET) {
1525 			if (ret & MII_KSZ9131_DIG_AXAN_STS_A_SELECT)
1526 				phydev->mdix = ETH_TP_MDI;
1527 			else
1528 				phydev->mdix = ETH_TP_MDI_X;
1529 		} else {
1530 			phydev->mdix = ETH_TP_MDI_INVALID;
1531 		}
1532 	}
1533 
1534 	return 0;
1535 }
1536 
ksz9131_config_mdix(struct phy_device * phydev,u8 ctrl)1537 static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl)
1538 {
1539 	u16 val;
1540 
1541 	switch (ctrl) {
1542 	case ETH_TP_MDI:
1543 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1544 		      MII_KSZ9131_AUTO_MDI_SET;
1545 		break;
1546 	case ETH_TP_MDI_X:
1547 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF;
1548 		break;
1549 	case ETH_TP_MDI_AUTO:
1550 		val = 0;
1551 		break;
1552 	default:
1553 		return 0;
1554 	}
1555 
1556 	return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX,
1557 			  MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1558 			  MII_KSZ9131_AUTO_MDI_SET, val);
1559 }
1560 
ksz9131_read_status(struct phy_device * phydev)1561 static int ksz9131_read_status(struct phy_device *phydev)
1562 {
1563 	int ret;
1564 
1565 	ret = ksz9131_mdix_update(phydev);
1566 	if (ret < 0)
1567 		return ret;
1568 
1569 	return genphy_read_status(phydev);
1570 }
1571 
ksz9131_config_aneg(struct phy_device * phydev)1572 static int ksz9131_config_aneg(struct phy_device *phydev)
1573 {
1574 	int ret;
1575 
1576 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
1577 	if (ret)
1578 		return ret;
1579 
1580 	return genphy_config_aneg(phydev);
1581 }
1582 
ksz9477_get_features(struct phy_device * phydev)1583 static int ksz9477_get_features(struct phy_device *phydev)
1584 {
1585 	int ret;
1586 
1587 	ret = genphy_read_abilities(phydev);
1588 	if (ret)
1589 		return ret;
1590 
1591 	/* The "EEE control and capability 1" (Register 3.20) seems to be
1592 	 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
1593 	 * on the 7.60 will affect 3.20. So, we need to construct our own list
1594 	 * of caps.
1595 	 * KSZ8563R should have 100BaseTX/Full only.
1596 	 */
1597 	linkmode_and(phydev->supported_eee, phydev->supported,
1598 		     PHY_EEE_CAP1_FEATURES);
1599 
1600 	return 0;
1601 }
1602 
1603 #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
1604 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
1605 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
ksz8873mll_read_status(struct phy_device * phydev)1606 static int ksz8873mll_read_status(struct phy_device *phydev)
1607 {
1608 	int regval;
1609 
1610 	/* dummy read */
1611 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1612 
1613 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1614 
1615 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
1616 		phydev->duplex = DUPLEX_HALF;
1617 	else
1618 		phydev->duplex = DUPLEX_FULL;
1619 
1620 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
1621 		phydev->speed = SPEED_10;
1622 	else
1623 		phydev->speed = SPEED_100;
1624 
1625 	phydev->link = 1;
1626 	phydev->pause = phydev->asym_pause = 0;
1627 
1628 	return 0;
1629 }
1630 
ksz9031_get_features(struct phy_device * phydev)1631 static int ksz9031_get_features(struct phy_device *phydev)
1632 {
1633 	int ret;
1634 
1635 	ret = genphy_read_abilities(phydev);
1636 	if (ret < 0)
1637 		return ret;
1638 
1639 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1640 	 * Whenever the device's Asymmetric Pause capability is set to 1,
1641 	 * link-up may fail after a link-up to link-down transition.
1642 	 *
1643 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1644 	 *
1645 	 * Workaround:
1646 	 * Do not enable the Asymmetric Pause capability bit.
1647 	 */
1648 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1649 
1650 	/* We force setting the Pause capability as the core will force the
1651 	 * Asymmetric Pause capability to 1 otherwise.
1652 	 */
1653 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1654 
1655 	return 0;
1656 }
1657 
ksz9031_read_status(struct phy_device * phydev)1658 static int ksz9031_read_status(struct phy_device *phydev)
1659 {
1660 	int err;
1661 	int regval;
1662 
1663 	err = genphy_read_status(phydev);
1664 	if (err)
1665 		return err;
1666 
1667 	/* Make sure the PHY is not broken. Read idle error count,
1668 	 * and reset the PHY if it is maxed out.
1669 	 */
1670 	regval = phy_read(phydev, MII_STAT1000);
1671 	if ((regval & 0xFF) == 0xFF) {
1672 		phy_init_hw(phydev);
1673 		phydev->link = 0;
1674 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1675 			phydev->drv->config_intr(phydev);
1676 		return genphy_config_aneg(phydev);
1677 	}
1678 
1679 	return 0;
1680 }
1681 
ksz9x31_cable_test_start(struct phy_device * phydev)1682 static int ksz9x31_cable_test_start(struct phy_device *phydev)
1683 {
1684 	struct kszphy_priv *priv = phydev->priv;
1685 	int ret;
1686 
1687 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1688 	 * Prior to running the cable diagnostics, Auto-negotiation should
1689 	 * be disabled, full duplex set and the link speed set to 1000Mbps
1690 	 * via the Basic Control Register.
1691 	 */
1692 	ret = phy_modify(phydev, MII_BMCR,
1693 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
1694 			 BMCR_ANENABLE | BMCR_SPEED100,
1695 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
1696 	if (ret)
1697 		return ret;
1698 
1699 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1700 	 * The Master-Slave configuration should be set to Slave by writing
1701 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
1702 	 * Register.
1703 	 */
1704 	ret = phy_read(phydev, MII_CTRL1000);
1705 	if (ret < 0)
1706 		return ret;
1707 
1708 	/* Cache these bits, they need to be restored once LinkMD finishes. */
1709 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1710 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1711 	ret |= CTL1000_ENABLE_MASTER;
1712 
1713 	return phy_write(phydev, MII_CTRL1000, ret);
1714 }
1715 
ksz9x31_cable_test_result_trans(u16 status)1716 static int ksz9x31_cable_test_result_trans(u16 status)
1717 {
1718 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1719 	case KSZ9x31_LMD_VCT_ST_NORMAL:
1720 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1721 	case KSZ9x31_LMD_VCT_ST_OPEN:
1722 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1723 	case KSZ9x31_LMD_VCT_ST_SHORT:
1724 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1725 	case KSZ9x31_LMD_VCT_ST_FAIL:
1726 		fallthrough;
1727 	default:
1728 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1729 	}
1730 }
1731 
ksz9x31_cable_test_failed(u16 status)1732 static bool ksz9x31_cable_test_failed(u16 status)
1733 {
1734 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
1735 
1736 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
1737 }
1738 
ksz9x31_cable_test_fault_length_valid(u16 status)1739 static bool ksz9x31_cable_test_fault_length_valid(u16 status)
1740 {
1741 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1742 	case KSZ9x31_LMD_VCT_ST_OPEN:
1743 		fallthrough;
1744 	case KSZ9x31_LMD_VCT_ST_SHORT:
1745 		return true;
1746 	}
1747 	return false;
1748 }
1749 
ksz9x31_cable_test_fault_length(struct phy_device * phydev,u16 stat)1750 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
1751 {
1752 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
1753 
1754 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1755 	 *
1756 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
1757 	 */
1758 	if (phydev_id_compare(phydev, PHY_ID_KSZ9131) ||
1759 	    phydev_id_compare(phydev, PHY_ID_KSZ9477))
1760 		dt = clamp(dt - 22, 0, 255);
1761 
1762 	return (dt * 400) / 10;
1763 }
1764 
ksz9x31_cable_test_wait_for_completion(struct phy_device * phydev)1765 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
1766 {
1767 	int val, ret;
1768 
1769 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
1770 				    !(val & KSZ9x31_LMD_VCT_EN),
1771 				    30000, 100000, true);
1772 
1773 	return ret < 0 ? ret : 0;
1774 }
1775 
ksz9x31_cable_test_get_pair(int pair)1776 static int ksz9x31_cable_test_get_pair(int pair)
1777 {
1778 	static const int ethtool_pair[] = {
1779 		ETHTOOL_A_CABLE_PAIR_A,
1780 		ETHTOOL_A_CABLE_PAIR_B,
1781 		ETHTOOL_A_CABLE_PAIR_C,
1782 		ETHTOOL_A_CABLE_PAIR_D,
1783 	};
1784 
1785 	return ethtool_pair[pair];
1786 }
1787 
ksz9x31_cable_test_one_pair(struct phy_device * phydev,int pair)1788 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
1789 {
1790 	int ret, val;
1791 
1792 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1793 	 * To test each individual cable pair, set the cable pair in the Cable
1794 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
1795 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
1796 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
1797 	 * will self clear when the test is concluded.
1798 	 */
1799 	ret = phy_write(phydev, KSZ9x31_LMD,
1800 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
1801 	if (ret)
1802 		return ret;
1803 
1804 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
1805 	if (ret)
1806 		return ret;
1807 
1808 	val = phy_read(phydev, KSZ9x31_LMD);
1809 	if (val < 0)
1810 		return val;
1811 
1812 	if (ksz9x31_cable_test_failed(val))
1813 		return -EAGAIN;
1814 
1815 	ret = ethnl_cable_test_result(phydev,
1816 				      ksz9x31_cable_test_get_pair(pair),
1817 				      ksz9x31_cable_test_result_trans(val));
1818 	if (ret)
1819 		return ret;
1820 
1821 	if (!ksz9x31_cable_test_fault_length_valid(val))
1822 		return 0;
1823 
1824 	return ethnl_cable_test_fault_length(phydev,
1825 					     ksz9x31_cable_test_get_pair(pair),
1826 					     ksz9x31_cable_test_fault_length(phydev, val));
1827 }
1828 
ksz9x31_cable_test_get_status(struct phy_device * phydev,bool * finished)1829 static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
1830 					 bool *finished)
1831 {
1832 	struct kszphy_priv *priv = phydev->priv;
1833 	unsigned long pair_mask;
1834 	int retries = 20;
1835 	int pair, ret, rv;
1836 
1837 	*finished = false;
1838 
1839 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1840 			      phydev->supported) ||
1841 	    linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1842 			      phydev->supported))
1843 		pair_mask = 0xf; /* All pairs */
1844 	else
1845 		pair_mask = 0x3; /* Pairs A and B only */
1846 
1847 	/* Try harder if link partner is active */
1848 	while (pair_mask && retries--) {
1849 		for_each_set_bit(pair, &pair_mask, 4) {
1850 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
1851 			if (ret == -EAGAIN)
1852 				continue;
1853 			if (ret < 0)
1854 				return ret;
1855 			clear_bit(pair, &pair_mask);
1856 		}
1857 		/* If link partner is in autonegotiation mode it will send 2ms
1858 		 * of FLPs with at least 6ms of silence.
1859 		 * Add 2ms sleep to have better chances to hit this silence.
1860 		 */
1861 		if (pair_mask)
1862 			usleep_range(2000, 3000);
1863 	}
1864 
1865 	/* Report remaining unfinished pair result as unknown. */
1866 	for_each_set_bit(pair, &pair_mask, 4) {
1867 		ret = ethnl_cable_test_result(phydev,
1868 					      ksz9x31_cable_test_get_pair(pair),
1869 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
1870 	}
1871 
1872 	*finished = true;
1873 
1874 	/* Restore cached bits from before LinkMD got started. */
1875 	rv = phy_modify(phydev, MII_CTRL1000,
1876 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
1877 			priv->vct_ctrl1000);
1878 	if (rv)
1879 		return rv;
1880 
1881 	return ret;
1882 }
1883 
ksz8873mll_config_aneg(struct phy_device * phydev)1884 static int ksz8873mll_config_aneg(struct phy_device *phydev)
1885 {
1886 	return 0;
1887 }
1888 
ksz886x_config_mdix(struct phy_device * phydev,u8 ctrl)1889 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
1890 {
1891 	u16 val;
1892 
1893 	switch (ctrl) {
1894 	case ETH_TP_MDI:
1895 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
1896 		break;
1897 	case ETH_TP_MDI_X:
1898 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
1899 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
1900 		 * sheet seems to be missing:
1901 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
1902 		 * 0 = Normal operation (transmit on TX+/TX- pins)
1903 		 */
1904 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
1905 		break;
1906 	case ETH_TP_MDI_AUTO:
1907 		val = 0;
1908 		break;
1909 	default:
1910 		return 0;
1911 	}
1912 
1913 	return phy_modify(phydev, MII_BMCR,
1914 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
1915 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
1916 			  KSZ886X_BMCR_HP_MDIX | val);
1917 }
1918 
ksz886x_config_aneg(struct phy_device * phydev)1919 static int ksz886x_config_aneg(struct phy_device *phydev)
1920 {
1921 	int ret;
1922 
1923 	ret = genphy_config_aneg(phydev);
1924 	if (ret)
1925 		return ret;
1926 
1927 	if (phydev->autoneg != AUTONEG_ENABLE) {
1928 		/* When autonegotiation is disabled, we need to manually force
1929 		 * the link state. If we don't do this, the PHY will keep
1930 		 * sending Fast Link Pulses (FLPs) which are part of the
1931 		 * autonegotiation process. This is not desired when
1932 		 * autonegotiation is off.
1933 		 */
1934 		ret = phy_set_bits(phydev, MII_KSZPHY_CTRL,
1935 				   KSZ886X_CTRL_FORCE_LINK);
1936 		if (ret)
1937 			return ret;
1938 	} else {
1939 		/* If we had previously forced the link state, we need to
1940 		 * clear KSZ886X_CTRL_FORCE_LINK bit now. Otherwise, the PHY
1941 		 * will not perform autonegotiation.
1942 		 */
1943 		ret = phy_clear_bits(phydev, MII_KSZPHY_CTRL,
1944 				     KSZ886X_CTRL_FORCE_LINK);
1945 		if (ret)
1946 			return ret;
1947 	}
1948 
1949 	/* The MDI-X configuration is automatically changed by the PHY after
1950 	 * switching from autoneg off to on. So, take MDI-X configuration under
1951 	 * own control and set it after autoneg configuration was done.
1952 	 */
1953 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
1954 }
1955 
ksz886x_mdix_update(struct phy_device * phydev)1956 static int ksz886x_mdix_update(struct phy_device *phydev)
1957 {
1958 	int ret;
1959 
1960 	ret = phy_read(phydev, MII_BMCR);
1961 	if (ret < 0)
1962 		return ret;
1963 
1964 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
1965 		if (ret & KSZ886X_BMCR_FORCE_MDI)
1966 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1967 		else
1968 			phydev->mdix_ctrl = ETH_TP_MDI;
1969 	} else {
1970 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1971 	}
1972 
1973 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
1974 	if (ret < 0)
1975 		return ret;
1976 
1977 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
1978 	if (ret & KSZ886X_CTRL_MDIX_STAT)
1979 		phydev->mdix = ETH_TP_MDI_X;
1980 	else
1981 		phydev->mdix = ETH_TP_MDI;
1982 
1983 	return 0;
1984 }
1985 
ksz886x_read_status(struct phy_device * phydev)1986 static int ksz886x_read_status(struct phy_device *phydev)
1987 {
1988 	int ret;
1989 
1990 	ret = ksz886x_mdix_update(phydev);
1991 	if (ret < 0)
1992 		return ret;
1993 
1994 	return genphy_read_status(phydev);
1995 }
1996 
ksz9477_mdix_update(struct phy_device * phydev)1997 static int ksz9477_mdix_update(struct phy_device *phydev)
1998 {
1999 	if (phydev->mdix_ctrl != ETH_TP_MDI_AUTO)
2000 		phydev->mdix = phydev->mdix_ctrl;
2001 	else
2002 		phydev->mdix = ETH_TP_MDI_INVALID;
2003 
2004 	return 0;
2005 }
2006 
ksz9477_read_mdix_ctrl(struct phy_device * phydev)2007 static int ksz9477_read_mdix_ctrl(struct phy_device *phydev)
2008 {
2009 	int val;
2010 
2011 	val = phy_read(phydev, MII_KSZ9131_AUTO_MDIX);
2012 	if (val < 0)
2013 		return val;
2014 
2015 	if (!(val & MII_KSZ9131_AUTO_MDIX_SWAP_OFF))
2016 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
2017 	else if (val & MII_KSZ9131_AUTO_MDI_SET)
2018 		phydev->mdix_ctrl = ETH_TP_MDI;
2019 	else
2020 		phydev->mdix_ctrl = ETH_TP_MDI_X;
2021 
2022 	return 0;
2023 }
2024 
ksz9477_read_status(struct phy_device * phydev)2025 static int ksz9477_read_status(struct phy_device *phydev)
2026 {
2027 	int ret;
2028 
2029 	ret = ksz9477_mdix_update(phydev);
2030 	if (ret)
2031 		return ret;
2032 
2033 	return genphy_read_status(phydev);
2034 }
2035 
ksz9477_config_aneg(struct phy_device * phydev)2036 static int ksz9477_config_aneg(struct phy_device *phydev)
2037 {
2038 	int ret;
2039 
2040 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
2041 	if (ret)
2042 		return ret;
2043 
2044 	return genphy_config_aneg(phydev);
2045 }
2046 
2047 struct ksz9477_errata_write {
2048 	u8 dev_addr;
2049 	u8 reg_addr;
2050 	u16 val;
2051 };
2052 
2053 static const struct ksz9477_errata_write ksz9477_errata_writes[] = {
2054 	 /* Register settings are needed to improve PHY receive performance */
2055 	{0x01, 0x6f, 0xdd0b},
2056 	{0x01, 0x8f, 0x6032},
2057 	{0x01, 0x9d, 0x248c},
2058 	{0x01, 0x75, 0x0060},
2059 	{0x01, 0xd3, 0x7777},
2060 	{0x1c, 0x06, 0x3008},
2061 	{0x1c, 0x08, 0x2000},
2062 
2063 	/* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
2064 	{0x1c, 0x04, 0x00d0},
2065 
2066 	/* Register settings are required to meet data sheet supply current specifications */
2067 	{0x1c, 0x13, 0x6eff},
2068 	{0x1c, 0x14, 0xe6ff},
2069 	{0x1c, 0x15, 0x6eff},
2070 	{0x1c, 0x16, 0xe6ff},
2071 	{0x1c, 0x17, 0x00ff},
2072 	{0x1c, 0x18, 0x43ff},
2073 	{0x1c, 0x19, 0xc3ff},
2074 	{0x1c, 0x1a, 0x6fff},
2075 	{0x1c, 0x1b, 0x07ff},
2076 	{0x1c, 0x1c, 0x0fff},
2077 	{0x1c, 0x1d, 0xe7ff},
2078 	{0x1c, 0x1e, 0xefff},
2079 	{0x1c, 0x20, 0xeeee},
2080 };
2081 
ksz9477_phy_errata(struct phy_device * phydev)2082 static int ksz9477_phy_errata(struct phy_device *phydev)
2083 {
2084 	int err;
2085 	int i;
2086 
2087 	/* Apply PHY settings to address errata listed in
2088 	 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
2089 	 * Silicon Errata and Data Sheet Clarification documents.
2090 	 *
2091 	 * Document notes: Before configuring the PHY MMD registers, it is
2092 	 * necessary to set the PHY to 100 Mbps speed with auto-negotiation
2093 	 * disabled by writing to register 0xN100-0xN101. After writing the
2094 	 * MMD registers, and after all errata workarounds that involve PHY
2095 	 * register settings, write register 0xN100-0xN101 again to enable
2096 	 * and restart auto-negotiation.
2097 	 */
2098 	err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX);
2099 	if (err)
2100 		return err;
2101 
2102 	for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) {
2103 		const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i];
2104 
2105 		err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val);
2106 		if (err)
2107 			return err;
2108 	}
2109 
2110 	return genphy_restart_aneg(phydev);
2111 }
2112 
ksz9477_config_init(struct phy_device * phydev)2113 static int ksz9477_config_init(struct phy_device *phydev)
2114 {
2115 	int err;
2116 
2117 	/* Only KSZ9897 family of switches needs this fix. */
2118 	if ((phydev->phy_id & 0xf) == 1) {
2119 		err = ksz9477_phy_errata(phydev);
2120 		if (err)
2121 			return err;
2122 	}
2123 
2124 	/* Read initial MDI-X config state. So, we do not need to poll it
2125 	 * later on.
2126 	 */
2127 	err = ksz9477_read_mdix_ctrl(phydev);
2128 	if (err)
2129 		return err;
2130 
2131 	return kszphy_config_init(phydev);
2132 }
2133 
kszphy_get_sset_count(struct phy_device * phydev)2134 static int kszphy_get_sset_count(struct phy_device *phydev)
2135 {
2136 	return ARRAY_SIZE(kszphy_hw_stats);
2137 }
2138 
kszphy_get_strings(struct phy_device * phydev,u8 * data)2139 static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
2140 {
2141 	int i;
2142 
2143 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
2144 		ethtool_puts(&data, kszphy_hw_stats[i].string);
2145 }
2146 
kszphy_get_stat(struct phy_device * phydev,int i)2147 static u64 kszphy_get_stat(struct phy_device *phydev, int i)
2148 {
2149 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
2150 	struct kszphy_priv *priv = phydev->priv;
2151 	int val;
2152 	u64 ret;
2153 
2154 	val = phy_read(phydev, stat.reg);
2155 	if (val < 0) {
2156 		ret = U64_MAX;
2157 	} else {
2158 		val = val & ((1 << stat.bits) - 1);
2159 		priv->stats[i] += val;
2160 		ret = priv->stats[i];
2161 	}
2162 
2163 	return ret;
2164 }
2165 
kszphy_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)2166 static void kszphy_get_stats(struct phy_device *phydev,
2167 			     struct ethtool_stats *stats, u64 *data)
2168 {
2169 	int i;
2170 
2171 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
2172 		data[i] = kszphy_get_stat(phydev, i);
2173 }
2174 
2175 /* KSZ9477 PHY RXER Counter. Probably supported by other PHYs like KSZ9313,
2176  * etc. The counter is incremented when the PHY receives a frame with one or
2177  * more symbol errors. The counter is cleared when the register is read.
2178  */
2179 #define MII_KSZ9477_PHY_RXER_COUNTER	0x15
2180 
kszphy_update_stats(struct phy_device * phydev)2181 static int kszphy_update_stats(struct phy_device *phydev)
2182 {
2183 	struct kszphy_priv *priv = phydev->priv;
2184 	int ret;
2185 
2186 	ret = phy_read(phydev, MII_KSZ9477_PHY_RXER_COUNTER);
2187 	if (ret < 0)
2188 		return ret;
2189 
2190 	priv->phy_stats.rx_err_pkt_cnt += ret;
2191 
2192 	return 0;
2193 }
2194 
kszphy_get_phy_stats(struct phy_device * phydev,struct ethtool_eth_phy_stats * eth_stats,struct ethtool_phy_stats * stats)2195 static void kszphy_get_phy_stats(struct phy_device *phydev,
2196 				 struct ethtool_eth_phy_stats *eth_stats,
2197 				 struct ethtool_phy_stats *stats)
2198 {
2199 	struct kszphy_priv *priv = phydev->priv;
2200 
2201 	stats->rx_errors = priv->phy_stats.rx_err_pkt_cnt;
2202 }
2203 
2204 /* Base register for Signal Quality Indicator (SQI) - Channel A
2205  *
2206  * MMD Address: MDIO_MMD_PMAPMD (0x01)
2207  * Register:    0xAC (Channel A)
2208  * Each channel (pair) has its own register:
2209  *   Channel A: 0xAC
2210  *   Channel B: 0xAD
2211  *   Channel C: 0xAE
2212  *   Channel D: 0xAF
2213  */
2214 #define KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A	0xac
2215 
2216 /* SQI field mask for bits [14:8]
2217  *
2218  * SQI indicates relative quality of the signal.
2219  * A lower value indicates better signal quality.
2220  */
2221 #define KSZ9477_MMD_SQI_MASK			GENMASK(14, 8)
2222 
2223 #define KSZ9477_MAX_CHANNELS			4
2224 #define KSZ9477_SQI_MAX				7
2225 
2226 /* Number of SQI samples to average for a stable result.
2227  *
2228  * Reference: KSZ9477S Datasheet DS00002392C, Section 4.1.11 (page 26)
2229  * For noisy environments, a minimum of 30–50 readings is recommended.
2230  */
2231 #define KSZ9477_SQI_SAMPLE_COUNT		40
2232 
2233 /* The hardware SQI register provides a raw value from 0-127, where a lower
2234  * value indicates better signal quality. However, empirical testing has
2235  * shown that only the 0-7 range is relevant for a functional link. A raw
2236  * value of 8 or higher was measured directly before link drop. This aligns
2237  * with the OPEN Alliance recommendation that SQI=0 should represent the
2238  * pre-failure state.
2239  *
2240  * This table provides a non-linear mapping from the useful raw hardware
2241  * values (0-7) to the standard 0-7 SQI scale, where higher is better.
2242  */
2243 static const u8 ksz_sqi_mapping[] = {
2244 	7, /* raw 0 -> SQI 7 */
2245 	7, /* raw 1 -> SQI 7 */
2246 	6, /* raw 2 -> SQI 6 */
2247 	5, /* raw 3 -> SQI 5 */
2248 	4, /* raw 4 -> SQI 4 */
2249 	3, /* raw 5 -> SQI 3 */
2250 	2, /* raw 6 -> SQI 2 */
2251 	1, /* raw 7 -> SQI 1 */
2252 };
2253 
2254 /**
2255  * kszphy_get_sqi - Read, average, and map Signal Quality Index (SQI)
2256  * @phydev: the PHY device
2257  *
2258  * This function reads and processes the raw Signal Quality Index from the
2259  * PHY. Based on empirical testing, a raw value of 8 or higher indicates a
2260  * pre-failure state and is mapped to SQI 0. Raw values from 0-7 are
2261  * mapped to the standard 0-7 SQI scale via a lookup table.
2262  *
2263  * Return: SQI value (0–7), or a negative errno on failure.
2264  */
kszphy_get_sqi(struct phy_device * phydev)2265 static int kszphy_get_sqi(struct phy_device *phydev)
2266 {
2267 	int sum[KSZ9477_MAX_CHANNELS] = { 0 };
2268 	int worst_sqi = KSZ9477_SQI_MAX;
2269 	int i, val, raw_sqi, ch;
2270 	u8 channels;
2271 
2272 	/* Determine applicable channels based on link speed */
2273 	if (phydev->speed == SPEED_1000)
2274 		channels = 4;
2275 	else if (phydev->speed == SPEED_100)
2276 		channels = 1;
2277 	else
2278 		return -EOPNOTSUPP;
2279 
2280 	/* Sample and accumulate SQI readings for each pair (currently only one).
2281 	 *
2282 	 * Reference: KSZ9477S Datasheet DS00002392C, Section 4.1.11 (page 26)
2283 	 * - The SQI register is updated every 2 µs.
2284 	 * - Values may fluctuate significantly, even in low-noise environments.
2285 	 * - For reliable estimation, average a minimum of 30–50 samples
2286 	 *   (recommended for noisy environments)
2287 	 * - In noisy environments, individual readings are highly unreliable.
2288 	 *
2289 	 * We use 40 samples per pair with a delay of 3 µs between each
2290 	 * read to ensure new values are captured (2 µs update interval).
2291 	 */
2292 	for (i = 0; i < KSZ9477_SQI_SAMPLE_COUNT; i++) {
2293 		for (ch = 0; ch < channels; ch++) {
2294 			val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
2295 					   KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + ch);
2296 			if (val < 0)
2297 				return val;
2298 
2299 			raw_sqi = FIELD_GET(KSZ9477_MMD_SQI_MASK, val);
2300 			sum[ch] += raw_sqi;
2301 
2302 			/* We communicate with the PHY via MDIO via SPI or
2303 			 * I2C, which is relatively slow. At least slower than
2304 			 * the update interval of the SQI register.
2305 			 * So, we can skip the delay between reads.
2306 			 */
2307 		}
2308 	}
2309 
2310 	/* Calculate average for each channel and find the worst SQI */
2311 	for (ch = 0; ch < channels; ch++) {
2312 		int avg_raw_sqi = sum[ch] / KSZ9477_SQI_SAMPLE_COUNT;
2313 		int mapped_sqi;
2314 
2315 		/* Handle the pre-fail/failed state first. */
2316 		if (avg_raw_sqi >= ARRAY_SIZE(ksz_sqi_mapping))
2317 			mapped_sqi = 0;
2318 		else
2319 			/* Use the lookup table for the good signal range. */
2320 			mapped_sqi = ksz_sqi_mapping[avg_raw_sqi];
2321 
2322 		if (mapped_sqi < worst_sqi)
2323 			worst_sqi = mapped_sqi;
2324 	}
2325 
2326 	return worst_sqi;
2327 }
2328 
kszphy_get_sqi_max(struct phy_device * phydev)2329 static int kszphy_get_sqi_max(struct phy_device *phydev)
2330 {
2331 	return KSZ9477_SQI_MAX;
2332 }
2333 
kszphy_get_mse_capability(struct phy_device * phydev,struct phy_mse_capability * cap)2334 static int kszphy_get_mse_capability(struct phy_device *phydev,
2335 				     struct phy_mse_capability *cap)
2336 {
2337 	/* Capabilities depend on link mode:
2338 	 * - 1000BASE-T: per-pair SQI registers exist => expose A..D
2339 	 *   and a WORST selector.
2340 	 * - 100BASE-TX: HW provides a single MSE/SQI reading in the "channel A"
2341 	 *   register, but with auto MDI-X there is no MDI-X resolution bit,
2342 	 *   so we cannot map that register to a specific wire pair reliably.
2343 	 *   To avoid misleading per-channel data, advertise only LINK.
2344 	 * Other speeds: no MSE exposure via this driver.
2345 	 *
2346 	 * Note: WORST is *not* a hardware selector on this family.
2347 	 * We expose it because the driver computes it in software
2348 	 * by scanning per-channel readouts (A..D) and picking the
2349 	 * maximum average MSE.
2350 	 */
2351 	if (phydev->speed == SPEED_1000)
2352 		cap->supported_caps = PHY_MSE_CAP_CHANNEL_A |
2353 				      PHY_MSE_CAP_CHANNEL_B |
2354 				      PHY_MSE_CAP_CHANNEL_C |
2355 				      PHY_MSE_CAP_CHANNEL_D |
2356 				      PHY_MSE_CAP_WORST_CHANNEL;
2357 	else if (phydev->speed == SPEED_100)
2358 		cap->supported_caps = PHY_MSE_CAP_LINK;
2359 	else
2360 		return -EOPNOTSUPP;
2361 
2362 	cap->max_average_mse = FIELD_MAX(KSZ9477_MMD_SQI_MASK);
2363 	cap->refresh_rate_ps = 2000000; /* 2 us */
2364 	/* Estimated from link modulation (125 MBd per channel) and documented
2365 	 * refresh rate of 2 us
2366 	 */
2367 	cap->num_symbols = 250;
2368 
2369 	cap->supported_caps |= PHY_MSE_CAP_AVG;
2370 
2371 	return 0;
2372 }
2373 
kszphy_get_mse_snapshot(struct phy_device * phydev,enum phy_mse_channel channel,struct phy_mse_snapshot * snapshot)2374 static int kszphy_get_mse_snapshot(struct phy_device *phydev,
2375 				   enum phy_mse_channel channel,
2376 				   struct phy_mse_snapshot *snapshot)
2377 {
2378 	u8 num_channels;
2379 	int ret;
2380 
2381 	if (phydev->speed == SPEED_1000)
2382 		num_channels = 4;
2383 	else if (phydev->speed == SPEED_100)
2384 		num_channels = 1;
2385 	else
2386 		return -EOPNOTSUPP;
2387 
2388 	if (channel == PHY_MSE_CHANNEL_WORST) {
2389 		u32 worst_val = 0;
2390 		int i;
2391 
2392 		/* WORST is implemented in software: select the maximum
2393 		 * average MSE across the available per-channel registers.
2394 		 * Only defined when multiple channels exist (1000BASE-T).
2395 		 */
2396 		if (num_channels < 2)
2397 			return -EOPNOTSUPP;
2398 
2399 		for (i = 0; i < num_channels; i++) {
2400 			ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
2401 					KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + i);
2402 			if (ret < 0)
2403 				return ret;
2404 
2405 			ret = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret);
2406 			if (ret > worst_val)
2407 				worst_val = ret;
2408 		}
2409 		snapshot->average_mse = worst_val;
2410 	} else if (channel == PHY_MSE_CHANNEL_LINK && num_channels == 1) {
2411 		ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
2412 				   KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A);
2413 		if (ret < 0)
2414 			return ret;
2415 		snapshot->average_mse = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret);
2416 	} else if (channel >= PHY_MSE_CHANNEL_A &&
2417 		   channel <= PHY_MSE_CHANNEL_D) {
2418 		/* Per-channel readouts are valid only for 1000BASE-T. */
2419 		if (phydev->speed != SPEED_1000)
2420 			return -EOPNOTSUPP;
2421 
2422 		ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
2423 				   KSZ9477_MMD_SIGNAL_QUALITY_CHAN_A + channel);
2424 		if (ret < 0)
2425 			return ret;
2426 		snapshot->average_mse = FIELD_GET(KSZ9477_MMD_SQI_MASK, ret);
2427 	} else {
2428 		return -EOPNOTSUPP;
2429 	}
2430 
2431 	return 0;
2432 }
2433 
kszphy_enable_clk(struct phy_device * phydev)2434 static void kszphy_enable_clk(struct phy_device *phydev)
2435 {
2436 	struct kszphy_priv *priv = phydev->priv;
2437 
2438 	if (!priv->clk_enable && priv->clk) {
2439 		clk_prepare_enable(priv->clk);
2440 		priv->clk_enable = true;
2441 	}
2442 }
2443 
kszphy_disable_clk(struct phy_device * phydev)2444 static void kszphy_disable_clk(struct phy_device *phydev)
2445 {
2446 	struct kszphy_priv *priv = phydev->priv;
2447 
2448 	if (priv->clk_enable && priv->clk) {
2449 		clk_disable_unprepare(priv->clk);
2450 		priv->clk_enable = false;
2451 	}
2452 }
2453 
kszphy_generic_resume(struct phy_device * phydev)2454 static int kszphy_generic_resume(struct phy_device *phydev)
2455 {
2456 	kszphy_enable_clk(phydev);
2457 
2458 	return genphy_resume(phydev);
2459 }
2460 
kszphy_generic_suspend(struct phy_device * phydev)2461 static int kszphy_generic_suspend(struct phy_device *phydev)
2462 {
2463 	int ret;
2464 
2465 	ret = genphy_suspend(phydev);
2466 	if (ret)
2467 		return ret;
2468 
2469 	kszphy_disable_clk(phydev);
2470 
2471 	return 0;
2472 }
2473 
kszphy_suspend(struct phy_device * phydev)2474 static int kszphy_suspend(struct phy_device *phydev)
2475 {
2476 	/* Disable PHY Interrupts */
2477 	if (phy_interrupt_is_valid(phydev)) {
2478 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
2479 		if (phydev->drv->config_intr)
2480 			phydev->drv->config_intr(phydev);
2481 	}
2482 
2483 	return kszphy_generic_suspend(phydev);
2484 }
2485 
kszphy_parse_led_mode(struct phy_device * phydev)2486 static void kszphy_parse_led_mode(struct phy_device *phydev)
2487 {
2488 	const struct kszphy_type *type = phydev->drv->driver_data;
2489 	const struct device_node *np = phydev->mdio.dev.of_node;
2490 	struct kszphy_priv *priv = phydev->priv;
2491 	int ret;
2492 
2493 	if (type && type->led_mode_reg) {
2494 		ret = of_property_read_u32(np, "micrel,led-mode",
2495 					   &priv->led_mode);
2496 
2497 		if (ret)
2498 			priv->led_mode = -1;
2499 
2500 		if (priv->led_mode > 3) {
2501 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
2502 				   priv->led_mode);
2503 			priv->led_mode = -1;
2504 		}
2505 	} else {
2506 		priv->led_mode = -1;
2507 	}
2508 }
2509 
kszphy_resume(struct phy_device * phydev)2510 static int kszphy_resume(struct phy_device *phydev)
2511 {
2512 	int ret;
2513 
2514 	ret = kszphy_generic_resume(phydev);
2515 	if (ret)
2516 		return ret;
2517 
2518 	/* After switching from power-down to normal mode, an internal global
2519 	 * reset is automatically generated. Wait a minimum of 1 ms before
2520 	 * read/write access to the PHY registers.
2521 	 */
2522 	usleep_range(1000, 2000);
2523 
2524 	ret = kszphy_config_reset(phydev);
2525 	if (ret)
2526 		return ret;
2527 
2528 	/* Enable PHY Interrupts */
2529 	if (phy_interrupt_is_valid(phydev)) {
2530 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
2531 		if (phydev->drv->config_intr)
2532 			phydev->drv->config_intr(phydev);
2533 	}
2534 
2535 	return 0;
2536 }
2537 
2538 /* Because of errata DS80000700A, receiver error following software
2539  * power down. Suspend and resume callbacks only disable and enable
2540  * external rmii reference clock.
2541  */
ksz8041_resume(struct phy_device * phydev)2542 static int ksz8041_resume(struct phy_device *phydev)
2543 {
2544 	kszphy_enable_clk(phydev);
2545 
2546 	return 0;
2547 }
2548 
ksz8041_suspend(struct phy_device * phydev)2549 static int ksz8041_suspend(struct phy_device *phydev)
2550 {
2551 	kszphy_disable_clk(phydev);
2552 
2553 	return 0;
2554 }
2555 
ksz9477_resume(struct phy_device * phydev)2556 static int ksz9477_resume(struct phy_device *phydev)
2557 {
2558 	int ret;
2559 
2560 	/* No need to initialize registers if not powered down. */
2561 	ret = phy_read(phydev, MII_BMCR);
2562 	if (ret < 0)
2563 		return ret;
2564 	if (!(ret & BMCR_PDOWN))
2565 		return 0;
2566 
2567 	genphy_resume(phydev);
2568 
2569 	/* After switching from power-down to normal mode, an internal global
2570 	 * reset is automatically generated. Wait a minimum of 1 ms before
2571 	 * read/write access to the PHY registers.
2572 	 */
2573 	usleep_range(1000, 2000);
2574 
2575 	/* Only KSZ9897 family of switches needs this fix. */
2576 	if ((phydev->phy_id & 0xf) == 1) {
2577 		ret = ksz9477_phy_errata(phydev);
2578 		if (ret)
2579 			return ret;
2580 	}
2581 
2582 	/* Enable PHY Interrupts */
2583 	if (phy_interrupt_is_valid(phydev)) {
2584 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
2585 		if (phydev->drv->config_intr)
2586 			phydev->drv->config_intr(phydev);
2587 	}
2588 
2589 	return 0;
2590 }
2591 
ksz8061_resume(struct phy_device * phydev)2592 static int ksz8061_resume(struct phy_device *phydev)
2593 {
2594 	int ret;
2595 
2596 	/* This function can be called twice when the Ethernet device is on. */
2597 	ret = phy_read(phydev, MII_BMCR);
2598 	if (ret < 0)
2599 		return ret;
2600 	if (!(ret & BMCR_PDOWN))
2601 		return 0;
2602 
2603 	ret = kszphy_generic_resume(phydev);
2604 	if (ret)
2605 		return ret;
2606 
2607 	usleep_range(1000, 2000);
2608 
2609 	/* Re-program the value after chip is reset. */
2610 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
2611 	if (ret)
2612 		return ret;
2613 
2614 	/* Enable PHY Interrupts */
2615 	if (phy_interrupt_is_valid(phydev)) {
2616 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
2617 		if (phydev->drv->config_intr)
2618 			phydev->drv->config_intr(phydev);
2619 	}
2620 
2621 	return 0;
2622 }
2623 
ksz8061_suspend(struct phy_device * phydev)2624 static int ksz8061_suspend(struct phy_device *phydev)
2625 {
2626 	return kszphy_suspend(phydev);
2627 }
2628 
kszphy_probe(struct phy_device * phydev)2629 static int kszphy_probe(struct phy_device *phydev)
2630 {
2631 	const struct kszphy_type *type = phydev->drv->driver_data;
2632 	const struct device_node *np = phydev->mdio.dev.of_node;
2633 	struct kszphy_priv *priv;
2634 	struct clk *clk;
2635 
2636 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2637 	if (!priv)
2638 		return -ENOMEM;
2639 
2640 	phydev->priv = priv;
2641 
2642 	priv->type = type;
2643 
2644 	kszphy_parse_led_mode(phydev);
2645 
2646 	clk = devm_clk_get_optional_enabled(&phydev->mdio.dev, "rmii-ref");
2647 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
2648 	if (!IS_ERR_OR_NULL(clk)) {
2649 		unsigned long rate = clk_get_rate(clk);
2650 		bool rmii_ref_clk_sel_25_mhz;
2651 
2652 		if (type)
2653 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
2654 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
2655 				"micrel,rmii-reference-clock-select-25-mhz");
2656 
2657 		if (rate > 24500000 && rate < 25500000) {
2658 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
2659 		} else if (rate > 49500000 && rate < 50500000) {
2660 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
2661 		} else {
2662 			phydev_err(phydev, "Clock rate out of range: %ld\n",
2663 				   rate);
2664 			return -EINVAL;
2665 		}
2666 	} else if (!clk) {
2667 		/* unnamed clock from the generic ethernet-phy binding */
2668 		clk = devm_clk_get_optional_enabled(&phydev->mdio.dev, NULL);
2669 	}
2670 
2671 	if (IS_ERR(clk))
2672 		return PTR_ERR(clk);
2673 
2674 	clk_disable_unprepare(clk);
2675 	priv->clk = clk;
2676 
2677 	if (ksz8041_fiber_mode(phydev))
2678 		phydev->port = PORT_FIBRE;
2679 
2680 	/* Support legacy board-file configuration */
2681 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
2682 		priv->rmii_ref_clk_sel = true;
2683 		priv->rmii_ref_clk_sel_val = true;
2684 	}
2685 
2686 	return 0;
2687 }
2688 
lan8814_cable_test_start(struct phy_device * phydev)2689 static int lan8814_cable_test_start(struct phy_device *phydev)
2690 {
2691 	/* If autoneg is enabled, we won't be able to test cross pair
2692 	 * short. In this case, the PHY will "detect" a link and
2693 	 * confuse the internal state machine - disable auto neg here.
2694 	 * Set the speed to 1000mbit and full duplex.
2695 	 */
2696 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
2697 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
2698 }
2699 
ksz886x_cable_test_start(struct phy_device * phydev)2700 static int ksz886x_cable_test_start(struct phy_device *phydev)
2701 {
2702 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
2703 		return -EOPNOTSUPP;
2704 
2705 	/* If autoneg is enabled, we won't be able to test cross pair
2706 	 * short. In this case, the PHY will "detect" a link and
2707 	 * confuse the internal state machine - disable auto neg here.
2708 	 * If autoneg is disabled, we should set the speed to 10mbit.
2709 	 */
2710 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
2711 }
2712 
ksz886x_cable_test_result_trans(u16 status,u16 mask)2713 static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
2714 {
2715 	switch (FIELD_GET(mask, status)) {
2716 	case KSZ8081_LMD_STAT_NORMAL:
2717 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
2718 	case KSZ8081_LMD_STAT_SHORT:
2719 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
2720 	case KSZ8081_LMD_STAT_OPEN:
2721 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
2722 	case KSZ8081_LMD_STAT_FAIL:
2723 		fallthrough;
2724 	default:
2725 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
2726 	}
2727 }
2728 
ksz886x_cable_test_failed(u16 status,u16 mask)2729 static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
2730 {
2731 	return FIELD_GET(mask, status) ==
2732 		KSZ8081_LMD_STAT_FAIL;
2733 }
2734 
ksz886x_cable_test_fault_length_valid(u16 status,u16 mask)2735 static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
2736 {
2737 	switch (FIELD_GET(mask, status)) {
2738 	case KSZ8081_LMD_STAT_OPEN:
2739 		fallthrough;
2740 	case KSZ8081_LMD_STAT_SHORT:
2741 		return true;
2742 	}
2743 	return false;
2744 }
2745 
ksz886x_cable_test_fault_length(struct phy_device * phydev,u16 status,u16 data_mask)2746 static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
2747 							   u16 status, u16 data_mask)
2748 {
2749 	int dt;
2750 
2751 	/* According to the data sheet the distance to the fault is
2752 	 * DELTA_TIME * 0.4 meters for ksz phys.
2753 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
2754 	 */
2755 	dt = FIELD_GET(data_mask, status);
2756 
2757 	if (phydev_id_compare(phydev, PHY_ID_LAN8814))
2758 		return ((dt - 22) * 800) / 10;
2759 	else
2760 		return (dt * 400) / 10;
2761 }
2762 
ksz886x_cable_test_wait_for_completion(struct phy_device * phydev)2763 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
2764 {
2765 	const struct kszphy_type *type = phydev->drv->driver_data;
2766 	int val, ret;
2767 
2768 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
2769 				    !(val & KSZ8081_LMD_ENABLE_TEST),
2770 				    30000, 100000, true);
2771 
2772 	return ret < 0 ? ret : 0;
2773 }
2774 
lan8814_cable_test_one_pair(struct phy_device * phydev,int pair)2775 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
2776 {
2777 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
2778 					    ETHTOOL_A_CABLE_PAIR_B,
2779 					    ETHTOOL_A_CABLE_PAIR_C,
2780 					    ETHTOOL_A_CABLE_PAIR_D,
2781 					  };
2782 	u32 fault_length;
2783 	int ret;
2784 	int val;
2785 
2786 	val = KSZ8081_LMD_ENABLE_TEST;
2787 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
2788 
2789 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
2790 	if (ret < 0)
2791 		return ret;
2792 
2793 	ret = ksz886x_cable_test_wait_for_completion(phydev);
2794 	if (ret)
2795 		return ret;
2796 
2797 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
2798 	if (val < 0)
2799 		return val;
2800 
2801 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
2802 		return -EAGAIN;
2803 
2804 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
2805 				      ksz886x_cable_test_result_trans(val,
2806 								      LAN8814_CABLE_DIAG_STAT_MASK
2807 								      ));
2808 	if (ret)
2809 		return ret;
2810 
2811 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
2812 		return 0;
2813 
2814 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
2815 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
2816 
2817 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
2818 }
2819 
ksz886x_cable_test_one_pair(struct phy_device * phydev,int pair)2820 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
2821 {
2822 	static const int ethtool_pair[] = {
2823 		ETHTOOL_A_CABLE_PAIR_A,
2824 		ETHTOOL_A_CABLE_PAIR_B,
2825 	};
2826 	int ret, val, mdix;
2827 	u32 fault_length;
2828 
2829 	/* There is no way to choice the pair, like we do one ksz9031.
2830 	 * We can workaround this limitation by using the MDI-X functionality.
2831 	 */
2832 	if (pair == 0)
2833 		mdix = ETH_TP_MDI;
2834 	else
2835 		mdix = ETH_TP_MDI_X;
2836 
2837 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
2838 	case PHY_ID_KSZ8081:
2839 		ret = ksz8081_config_mdix(phydev, mdix);
2840 		break;
2841 	case PHY_ID_KSZ886X:
2842 		ret = ksz886x_config_mdix(phydev, mdix);
2843 		break;
2844 	default:
2845 		ret = -ENODEV;
2846 	}
2847 
2848 	if (ret)
2849 		return ret;
2850 
2851 	/* Now we are ready to fire. This command will send a 100ns pulse
2852 	 * to the pair.
2853 	 */
2854 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
2855 	if (ret)
2856 		return ret;
2857 
2858 	ret = ksz886x_cable_test_wait_for_completion(phydev);
2859 	if (ret)
2860 		return ret;
2861 
2862 	val = phy_read(phydev, KSZ8081_LMD);
2863 	if (val < 0)
2864 		return val;
2865 
2866 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
2867 		return -EAGAIN;
2868 
2869 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
2870 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
2871 	if (ret)
2872 		return ret;
2873 
2874 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
2875 		return 0;
2876 
2877 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
2878 
2879 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
2880 }
2881 
ksz886x_cable_test_get_status(struct phy_device * phydev,bool * finished)2882 static int ksz886x_cable_test_get_status(struct phy_device *phydev,
2883 					 bool *finished)
2884 {
2885 	const struct kszphy_type *type = phydev->drv->driver_data;
2886 	unsigned long pair_mask = type->pair_mask;
2887 	int retries = 20;
2888 	int ret = 0;
2889 	int pair;
2890 
2891 	*finished = false;
2892 
2893 	/* Try harder if link partner is active */
2894 	while (pair_mask && retries--) {
2895 		for_each_set_bit(pair, &pair_mask, 4) {
2896 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
2897 				ret = lan8814_cable_test_one_pair(phydev, pair);
2898 			else
2899 				ret = ksz886x_cable_test_one_pair(phydev, pair);
2900 			if (ret == -EAGAIN)
2901 				continue;
2902 			if (ret < 0)
2903 				return ret;
2904 			clear_bit(pair, &pair_mask);
2905 		}
2906 		/* If link partner is in autonegotiation mode it will send 2ms
2907 		 * of FLPs with at least 6ms of silence.
2908 		 * Add 2ms sleep to have better chances to hit this silence.
2909 		 */
2910 		if (pair_mask)
2911 			msleep(2);
2912 	}
2913 
2914 	*finished = true;
2915 
2916 	return ret;
2917 }
2918 
2919 /**
2920  * LAN8814_PAGE_PCS - Selects Extended Page 0.
2921  *
2922  * This page contains timers used for auto-negotiation, debug registers and
2923  * register to configure fast link failure.
2924  */
2925 #define LAN8814_PAGE_PCS 0
2926 
2927 /**
2928  * LAN8814_PAGE_AFE_PMA - Selects Extended Page 1.
2929  *
2930  * This page appears to control the Analog Front-End (AFE) and Physical
2931  * Medium Attachment (PMA) layers. It is used to access registers like
2932  * LAN8814_PD_CONTROLS and LAN8814_LINK_QUALITY.
2933  */
2934 #define LAN8814_PAGE_AFE_PMA 1
2935 
2936 /**
2937  * LAN8814_PAGE_PCS_DIGITAL - Selects Extended Page 2.
2938  *
2939  * This page seems dedicated to the Physical Coding Sublayer (PCS) and other
2940  * digital logic. It is used for MDI-X alignment (LAN8814_ALIGN_SWAP) and EEE
2941  * state (LAN8814_EEE_STATE) in the LAN8814, and is repurposed for statistics
2942  * and self-test counters in the LAN8842.
2943  */
2944 #define LAN8814_PAGE_PCS_DIGITAL 2
2945 
2946 /**
2947  * LAN8814_PAGE_EEE - Selects Extended Page 3.
2948  *
2949  * This page contains EEE registers
2950  */
2951 #define LAN8814_PAGE_EEE 3
2952 
2953 /**
2954  * LAN8814_PAGE_COMMON_REGS - Selects Extended Page 4.
2955  *
2956  * This page contains device-common registers that affect the entire chip.
2957  * It includes controls for chip-level resets, strap status, GPIO,
2958  * QSGMII, the shared 1588 PTP block, and the PVT monitor.
2959  */
2960 #define LAN8814_PAGE_COMMON_REGS 4
2961 
2962 /**
2963  * LAN8814_PAGE_PORT_REGS - Selects Extended Page 5.
2964  *
2965  * This page contains port-specific registers that must be accessed
2966  * on a per-port basis. It includes controls for port LEDs, QSGMII PCS,
2967  * rate adaptation FIFOs, and the per-port 1588 TSU block.
2968  */
2969 #define LAN8814_PAGE_PORT_REGS 5
2970 
2971 /**
2972  * LAN8814_PAGE_POWER_REGS - Selects Extended Page 28.
2973  *
2974  * This page contains analog control registers and power mode registers.
2975  */
2976 #define LAN8814_PAGE_POWER_REGS 28
2977 
2978 /**
2979  * LAN8814_PAGE_SYSTEM_CTRL - Selects Extended Page 31.
2980  *
2981  * This page appears to hold fundamental system or global controls. In the
2982  * driver, it is used by the related LAN8804 to access the
2983  * LAN8814_CLOCK_MANAGEMENT register.
2984  */
2985 #define LAN8814_PAGE_SYSTEM_CTRL 31
2986 
2987 #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
2988 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
2989 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
2990 
2991 #define LAN8814_QSGMII_TX_CONFIG			0x35
2992 #define LAN8814_QSGMII_TX_CONFIG_QSGMII			BIT(3)
2993 #define LAN8814_QSGMII_SOFT_RESET			0x43
2994 #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
2995 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
2996 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
2997 #define LAN8814_ALIGN_SWAP				0x4a
2998 #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
2999 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
3000 
3001 #define LAN8804_ALIGN_SWAP				0x4a
3002 #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
3003 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
3004 #define LAN8814_CLOCK_MANAGEMENT			0xd
3005 #define LAN8814_LINK_QUALITY				0x8e
3006 
lanphy_read_page_reg(struct phy_device * phydev,int page,u32 addr)3007 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
3008 {
3009 	int data;
3010 
3011 	phy_lock_mdio_bus(phydev);
3012 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
3013 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
3014 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
3015 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
3016 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
3017 	phy_unlock_mdio_bus(phydev);
3018 
3019 	return data;
3020 }
3021 
lanphy_write_page_reg(struct phy_device * phydev,int page,u16 addr,u16 val)3022 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
3023 				 u16 val)
3024 {
3025 	phy_lock_mdio_bus(phydev);
3026 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
3027 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
3028 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
3029 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
3030 
3031 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
3032 	if (val != 0)
3033 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
3034 			   val);
3035 	phy_unlock_mdio_bus(phydev);
3036 	return val;
3037 }
3038 
lanphy_modify_page_reg(struct phy_device * phydev,int page,u16 addr,u16 mask,u16 set)3039 static int lanphy_modify_page_reg(struct phy_device *phydev, int page, u16 addr,
3040 				  u16 mask, u16 set)
3041 {
3042 	int ret;
3043 
3044 	phy_lock_mdio_bus(phydev);
3045 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
3046 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
3047 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
3048 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
3049 	ret = __phy_modify_changed(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA,
3050 				   mask, set);
3051 	phy_unlock_mdio_bus(phydev);
3052 
3053 	if (ret < 0)
3054 		phydev_err(phydev, "__phy_modify_changed() failed: %pe\n",
3055 			   ERR_PTR(ret));
3056 
3057 	return ret;
3058 }
3059 
lan8814_config_ts_intr(struct phy_device * phydev,bool enable)3060 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
3061 {
3062 	u16 val = 0;
3063 
3064 	if (enable)
3065 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
3066 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
3067 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
3068 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
3069 
3070 	return lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3071 				     PTP_TSU_INT_EN, val);
3072 }
3073 
lan8814_ptp_rx_ts_get(struct phy_device * phydev,u32 * seconds,u32 * nano_seconds,u16 * seq_id)3074 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
3075 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
3076 {
3077 	*seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3078 					PTP_RX_INGRESS_SEC_HI);
3079 	*seconds = (*seconds << 16) |
3080 		   lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3081 					PTP_RX_INGRESS_SEC_LO);
3082 
3083 	*nano_seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3084 					     PTP_RX_INGRESS_NS_HI);
3085 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
3086 			lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3087 					     PTP_RX_INGRESS_NS_LO);
3088 
3089 	*seq_id = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3090 				       PTP_RX_MSG_HEADER2);
3091 }
3092 
lan8814_ptp_tx_ts_get(struct phy_device * phydev,u32 * seconds,u32 * nano_seconds,u16 * seq_id)3093 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
3094 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
3095 {
3096 	*seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3097 					PTP_TX_EGRESS_SEC_HI);
3098 	*seconds = *seconds << 16 |
3099 		   lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3100 					PTP_TX_EGRESS_SEC_LO);
3101 
3102 	*nano_seconds = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3103 					     PTP_TX_EGRESS_NS_HI);
3104 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
3105 			lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3106 					     PTP_TX_EGRESS_NS_LO);
3107 
3108 	*seq_id = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3109 				       PTP_TX_MSG_HEADER2);
3110 }
3111 
lan8814_ts_info(struct mii_timestamper * mii_ts,struct kernel_ethtool_ts_info * info)3112 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct kernel_ethtool_ts_info *info)
3113 {
3114 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3115 	struct lan8814_shared_priv *shared = phy_package_get_priv(ptp_priv->phydev);
3116 
3117 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3118 				SOF_TIMESTAMPING_RX_HARDWARE |
3119 				SOF_TIMESTAMPING_RAW_HARDWARE;
3120 
3121 	info->phc_index = ptp_clock_index(shared->ptp_clock);
3122 
3123 	info->tx_types =
3124 		(1 << HWTSTAMP_TX_OFF) |
3125 		(1 << HWTSTAMP_TX_ON) |
3126 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
3127 
3128 	info->rx_filters =
3129 		(1 << HWTSTAMP_FILTER_NONE) |
3130 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
3131 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3132 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3133 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3134 
3135 	return 0;
3136 }
3137 
lan8814_flush_fifo(struct phy_device * phydev,bool egress)3138 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
3139 {
3140 	int i;
3141 
3142 	for (i = 0; i < FIFO_SIZE; ++i)
3143 		lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
3144 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
3145 
3146 	/* Read to clear overflow status bit */
3147 	lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TSU_INT_STS);
3148 }
3149 
lan8814_hwtstamp_set(struct mii_timestamper * mii_ts,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)3150 static int lan8814_hwtstamp_set(struct mii_timestamper *mii_ts,
3151 				struct kernel_hwtstamp_config *config,
3152 				struct netlink_ext_ack *extack)
3153 {
3154 	struct kszphy_ptp_priv *ptp_priv =
3155 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3156 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
3157 	int txcfg = 0, rxcfg = 0;
3158 	int pkt_ts_enable;
3159 
3160 	ptp_priv->hwts_tx_type = config->tx_type;
3161 	ptp_priv->rx_filter = config->rx_filter;
3162 
3163 	switch (config->rx_filter) {
3164 	case HWTSTAMP_FILTER_NONE:
3165 		ptp_priv->layer = 0;
3166 		ptp_priv->version = 0;
3167 		break;
3168 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3169 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3170 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3171 		ptp_priv->layer = PTP_CLASS_L4;
3172 		ptp_priv->version = PTP_CLASS_V2;
3173 		break;
3174 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3175 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3176 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3177 		ptp_priv->layer = PTP_CLASS_L2;
3178 		ptp_priv->version = PTP_CLASS_V2;
3179 		break;
3180 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3181 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3182 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3183 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3184 		ptp_priv->version = PTP_CLASS_V2;
3185 		break;
3186 	default:
3187 		return -ERANGE;
3188 	}
3189 
3190 	if (ptp_priv->layer & PTP_CLASS_L2) {
3191 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3192 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3193 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3194 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3195 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3196 	}
3197 	lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3198 			      PTP_RX_PARSE_CONFIG, rxcfg);
3199 	lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3200 			      PTP_TX_PARSE_CONFIG, txcfg);
3201 
3202 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3203 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3204 	lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3205 			      PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3206 	lanphy_write_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3207 			      PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3208 
3209 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC) {
3210 		lanphy_modify_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3211 				       PTP_TX_MOD,
3212 				       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3213 				       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
3214 	} else if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ON) {
3215 		lanphy_modify_page_reg(ptp_priv->phydev, LAN8814_PAGE_PORT_REGS,
3216 				       PTP_TX_MOD,
3217 				       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3218 				       0);
3219 	}
3220 
3221 	if (config->rx_filter != HWTSTAMP_FILTER_NONE)
3222 		lan8814_config_ts_intr(ptp_priv->phydev, true);
3223 	else
3224 		lan8814_config_ts_intr(ptp_priv->phydev, false);
3225 
3226 	/* In case of multiple starts and stops, these needs to be cleared */
3227 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
3228 		list_del(&rx_ts->list);
3229 		kfree(rx_ts);
3230 	}
3231 	skb_queue_purge(&ptp_priv->rx_queue);
3232 	skb_queue_purge(&ptp_priv->tx_queue);
3233 
3234 	lan8814_flush_fifo(ptp_priv->phydev, false);
3235 	lan8814_flush_fifo(ptp_priv->phydev, true);
3236 
3237 	return 0;
3238 }
3239 
lan8814_txtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)3240 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
3241 			     struct sk_buff *skb, int type)
3242 {
3243 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3244 
3245 	switch (ptp_priv->hwts_tx_type) {
3246 	case HWTSTAMP_TX_ONESTEP_SYNC:
3247 		if (ptp_msg_is_sync(skb, type)) {
3248 			kfree_skb(skb);
3249 			return;
3250 		}
3251 		fallthrough;
3252 	case HWTSTAMP_TX_ON:
3253 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3254 		skb_queue_tail(&ptp_priv->tx_queue, skb);
3255 		break;
3256 	case HWTSTAMP_TX_OFF:
3257 	default:
3258 		kfree_skb(skb);
3259 		break;
3260 	}
3261 }
3262 
lan8814_get_sig_rx(struct sk_buff * skb,u16 * sig)3263 static bool lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
3264 {
3265 	struct ptp_header *ptp_header;
3266 	u32 type;
3267 
3268 	skb_push(skb, ETH_HLEN);
3269 	type = ptp_classify_raw(skb);
3270 	ptp_header = ptp_parse_header(skb, type);
3271 	skb_pull_inline(skb, ETH_HLEN);
3272 
3273 	if (!ptp_header)
3274 		return false;
3275 
3276 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
3277 	return true;
3278 }
3279 
lan8814_match_rx_skb(struct kszphy_ptp_priv * ptp_priv,struct sk_buff * skb)3280 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
3281 				 struct sk_buff *skb)
3282 {
3283 	struct skb_shared_hwtstamps *shhwtstamps;
3284 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
3285 	unsigned long flags;
3286 	bool ret = false;
3287 	u16 skb_sig;
3288 
3289 	if (!lan8814_get_sig_rx(skb, &skb_sig))
3290 		return ret;
3291 
3292 	/* Iterate over all RX timestamps and match it with the received skbs */
3293 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
3294 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
3295 		/* Check if we found the signature we were looking for. */
3296 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
3297 			continue;
3298 
3299 		shhwtstamps = skb_hwtstamps(skb);
3300 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3301 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
3302 						  rx_ts->nsec);
3303 		list_del(&rx_ts->list);
3304 		kfree(rx_ts);
3305 
3306 		ret = true;
3307 		break;
3308 	}
3309 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
3310 
3311 	if (ret)
3312 		netif_rx(skb);
3313 	return ret;
3314 }
3315 
lan8814_rxtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)3316 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
3317 {
3318 	struct kszphy_ptp_priv *ptp_priv =
3319 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3320 
3321 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
3322 	    type == PTP_CLASS_NONE)
3323 		return false;
3324 
3325 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
3326 		return false;
3327 
3328 	/* If we failed to match then add it to the queue for when the timestamp
3329 	 * will come
3330 	 */
3331 	if (!lan8814_match_rx_skb(ptp_priv, skb))
3332 		skb_queue_tail(&ptp_priv->rx_queue, skb);
3333 
3334 	return true;
3335 }
3336 
lan8814_ptp_clock_set(struct phy_device * phydev,time64_t sec,u32 nsec)3337 static void lan8814_ptp_clock_set(struct phy_device *phydev,
3338 				  time64_t sec, u32 nsec)
3339 {
3340 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3341 			      PTP_CLOCK_SET_SEC_LO, lower_16_bits(sec));
3342 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3343 			      PTP_CLOCK_SET_SEC_MID, upper_16_bits(sec));
3344 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3345 			      PTP_CLOCK_SET_SEC_HI, upper_32_bits(sec));
3346 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3347 			      PTP_CLOCK_SET_NS_LO, lower_16_bits(nsec));
3348 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3349 			      PTP_CLOCK_SET_NS_HI, upper_16_bits(nsec));
3350 
3351 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL,
3352 			      PTP_CMD_CTL_PTP_CLOCK_LOAD_);
3353 }
3354 
lan8814_ptp_clock_get(struct phy_device * phydev,time64_t * sec,u32 * nsec)3355 static void lan8814_ptp_clock_get(struct phy_device *phydev,
3356 				  time64_t *sec, u32 *nsec)
3357 {
3358 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL,
3359 			      PTP_CMD_CTL_PTP_CLOCK_READ_);
3360 
3361 	*sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3362 				    PTP_CLOCK_READ_SEC_HI);
3363 	*sec <<= 16;
3364 	*sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3365 				     PTP_CLOCK_READ_SEC_MID);
3366 	*sec <<= 16;
3367 	*sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3368 				     PTP_CLOCK_READ_SEC_LO);
3369 
3370 	*nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3371 				     PTP_CLOCK_READ_NS_HI);
3372 	*nsec <<= 16;
3373 	*nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3374 				      PTP_CLOCK_READ_NS_LO);
3375 }
3376 
lan8814_ptpci_gettime64(struct ptp_clock_info * ptpci,struct timespec64 * ts)3377 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
3378 				   struct timespec64 *ts)
3379 {
3380 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3381 							  ptp_clock_info);
3382 	struct phy_device *phydev = shared->phydev;
3383 	u32 nano_seconds;
3384 	time64_t seconds;
3385 
3386 	mutex_lock(&shared->shared_lock);
3387 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
3388 	mutex_unlock(&shared->shared_lock);
3389 	ts->tv_sec = seconds;
3390 	ts->tv_nsec = nano_seconds;
3391 
3392 	return 0;
3393 }
3394 
lan8814_ptpci_settime64(struct ptp_clock_info * ptpci,const struct timespec64 * ts)3395 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
3396 				   const struct timespec64 *ts)
3397 {
3398 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3399 							  ptp_clock_info);
3400 	struct phy_device *phydev = shared->phydev;
3401 
3402 	mutex_lock(&shared->shared_lock);
3403 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
3404 	mutex_unlock(&shared->shared_lock);
3405 
3406 	return 0;
3407 }
3408 
lan8814_ptp_set_target(struct phy_device * phydev,int event,s64 start_sec,u32 start_nsec)3409 static void lan8814_ptp_set_target(struct phy_device *phydev, int event,
3410 				   s64 start_sec, u32 start_nsec)
3411 {
3412 	/* Set the start time */
3413 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3414 			      LAN8814_PTP_CLOCK_TARGET_SEC_LO(event),
3415 			      lower_16_bits(start_sec));
3416 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3417 			      LAN8814_PTP_CLOCK_TARGET_SEC_HI(event),
3418 			      upper_16_bits(start_sec));
3419 
3420 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3421 			      LAN8814_PTP_CLOCK_TARGET_NS_LO(event),
3422 			      lower_16_bits(start_nsec));
3423 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3424 			      LAN8814_PTP_CLOCK_TARGET_NS_HI(event),
3425 			      upper_16_bits(start_nsec) & 0x3fff);
3426 }
3427 
lan8814_ptp_update_target(struct phy_device * phydev,time64_t sec)3428 static void lan8814_ptp_update_target(struct phy_device *phydev, time64_t sec)
3429 {
3430 	lan8814_ptp_set_target(phydev, LAN8814_EVENT_A,
3431 			       sec + LAN8814_BUFFER_TIME, 0);
3432 	lan8814_ptp_set_target(phydev, LAN8814_EVENT_B,
3433 			       sec + LAN8814_BUFFER_TIME, 0);
3434 }
3435 
lan8814_ptp_clock_step(struct phy_device * phydev,s64 time_step_ns)3436 static void lan8814_ptp_clock_step(struct phy_device *phydev,
3437 				   s64 time_step_ns)
3438 {
3439 	u32 nano_seconds_step;
3440 	u64 abs_time_step_ns;
3441 	time64_t set_seconds;
3442 	u32 nano_seconds;
3443 	u32 remainder;
3444 	s32 seconds;
3445 
3446 	if (time_step_ns >  15000000000LL) {
3447 		/* convert to clock set */
3448 		lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds);
3449 		set_seconds += div_u64_rem(time_step_ns, 1000000000LL,
3450 					   &remainder);
3451 		nano_seconds += remainder;
3452 		if (nano_seconds >= 1000000000) {
3453 			set_seconds++;
3454 			nano_seconds -= 1000000000;
3455 		}
3456 		lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds);
3457 		lan8814_ptp_update_target(phydev, set_seconds);
3458 		return;
3459 	} else if (time_step_ns < -15000000000LL) {
3460 		/* convert to clock set */
3461 		time_step_ns = -time_step_ns;
3462 
3463 		lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds);
3464 		set_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
3465 					   &remainder);
3466 		nano_seconds_step = remainder;
3467 		if (nano_seconds < nano_seconds_step) {
3468 			set_seconds--;
3469 			nano_seconds += 1000000000;
3470 		}
3471 		nano_seconds -= nano_seconds_step;
3472 		lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds);
3473 		lan8814_ptp_update_target(phydev, set_seconds);
3474 		return;
3475 	}
3476 
3477 	/* do clock step */
3478 	if (time_step_ns >= 0) {
3479 		abs_time_step_ns = (u64)time_step_ns;
3480 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
3481 					   &remainder);
3482 		nano_seconds = remainder;
3483 	} else {
3484 		abs_time_step_ns = (u64)(-time_step_ns);
3485 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
3486 			    &remainder));
3487 		nano_seconds = remainder;
3488 		if (nano_seconds > 0) {
3489 			/* subtracting nano seconds is not allowed
3490 			 * convert to subtracting from seconds,
3491 			 * and adding to nanoseconds
3492 			 */
3493 			seconds--;
3494 			nano_seconds = (1000000000 - nano_seconds);
3495 		}
3496 	}
3497 
3498 	if (nano_seconds > 0) {
3499 		/* add 8 ns to cover the likely normal increment */
3500 		nano_seconds += 8;
3501 	}
3502 
3503 	if (nano_seconds >= 1000000000) {
3504 		/* carry into seconds */
3505 		seconds++;
3506 		nano_seconds -= 1000000000;
3507 	}
3508 
3509 	while (seconds) {
3510 		u32 nsec;
3511 
3512 		if (seconds > 0) {
3513 			u32 adjustment_value = (u32)seconds;
3514 			u16 adjustment_value_lo, adjustment_value_hi;
3515 
3516 			if (adjustment_value > 0xF)
3517 				adjustment_value = 0xF;
3518 
3519 			adjustment_value_lo = adjustment_value & 0xffff;
3520 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
3521 
3522 			lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3523 					      PTP_LTC_STEP_ADJ_LO,
3524 					      adjustment_value_lo);
3525 			lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3526 					      PTP_LTC_STEP_ADJ_HI,
3527 					      PTP_LTC_STEP_ADJ_DIR_ |
3528 					      adjustment_value_hi);
3529 			seconds -= ((s32)adjustment_value);
3530 
3531 			lan8814_ptp_clock_get(phydev, &set_seconds, &nsec);
3532 			set_seconds -= adjustment_value;
3533 			lan8814_ptp_update_target(phydev, set_seconds);
3534 		} else {
3535 			u32 adjustment_value = (u32)(-seconds);
3536 			u16 adjustment_value_lo, adjustment_value_hi;
3537 
3538 			if (adjustment_value > 0xF)
3539 				adjustment_value = 0xF;
3540 
3541 			adjustment_value_lo = adjustment_value & 0xffff;
3542 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
3543 
3544 			lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3545 					      PTP_LTC_STEP_ADJ_LO,
3546 					      adjustment_value_lo);
3547 			lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3548 					      PTP_LTC_STEP_ADJ_HI,
3549 					      adjustment_value_hi);
3550 			seconds += ((s32)adjustment_value);
3551 
3552 			lan8814_ptp_clock_get(phydev, &set_seconds, &nsec);
3553 			set_seconds += adjustment_value;
3554 			lan8814_ptp_update_target(phydev, set_seconds);
3555 		}
3556 		lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3557 				      PTP_CMD_CTL, PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
3558 	}
3559 	if (nano_seconds) {
3560 		u16 nano_seconds_lo;
3561 		u16 nano_seconds_hi;
3562 
3563 		nano_seconds_lo = nano_seconds & 0xffff;
3564 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
3565 
3566 		lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3567 				      PTP_LTC_STEP_ADJ_LO,
3568 				      nano_seconds_lo);
3569 		lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3570 				      PTP_LTC_STEP_ADJ_HI,
3571 				      PTP_LTC_STEP_ADJ_DIR_ |
3572 				      nano_seconds_hi);
3573 		lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL,
3574 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
3575 	}
3576 }
3577 
lan8814_ptpci_adjtime(struct ptp_clock_info * ptpci,s64 delta)3578 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
3579 {
3580 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3581 							  ptp_clock_info);
3582 	struct phy_device *phydev = shared->phydev;
3583 
3584 	mutex_lock(&shared->shared_lock);
3585 	lan8814_ptp_clock_step(phydev, delta);
3586 	mutex_unlock(&shared->shared_lock);
3587 
3588 	return 0;
3589 }
3590 
lan8814_ptpci_adjfine(struct ptp_clock_info * ptpci,long scaled_ppm)3591 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
3592 {
3593 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3594 							  ptp_clock_info);
3595 	struct phy_device *phydev = shared->phydev;
3596 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
3597 	bool positive = true;
3598 	u32 kszphy_rate_adj;
3599 
3600 	if (scaled_ppm < 0) {
3601 		scaled_ppm = -scaled_ppm;
3602 		positive = false;
3603 	}
3604 
3605 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
3606 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
3607 
3608 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
3609 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
3610 
3611 	if (positive)
3612 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
3613 
3614 	mutex_lock(&shared->shared_lock);
3615 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CLOCK_RATE_ADJ_HI,
3616 			      kszphy_rate_adj_hi);
3617 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CLOCK_RATE_ADJ_LO,
3618 			      kszphy_rate_adj_lo);
3619 	mutex_unlock(&shared->shared_lock);
3620 
3621 	return 0;
3622 }
3623 
lan8814_ptp_set_reload(struct phy_device * phydev,int event,s64 period_sec,u32 period_nsec)3624 static void lan8814_ptp_set_reload(struct phy_device *phydev, int event,
3625 				   s64 period_sec, u32 period_nsec)
3626 {
3627 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3628 			      LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event),
3629 			      lower_16_bits(period_sec));
3630 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3631 			      LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event),
3632 			      upper_16_bits(period_sec));
3633 
3634 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3635 			      LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event),
3636 			      lower_16_bits(period_nsec));
3637 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3638 			      LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event),
3639 			      upper_16_bits(period_nsec) & 0x3fff);
3640 }
3641 
lan8814_ptp_enable_event(struct phy_device * phydev,int event,int pulse_width)3642 static void lan8814_ptp_enable_event(struct phy_device *phydev, int event,
3643 				     int pulse_width)
3644 {
3645 	/* Set the pulse width of the event,
3646 	 * Make sure that the target clock will be incremented each time when
3647 	 * local time reaches or pass it
3648 	 * Set the polarity high
3649 	 */
3650 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_PTP_GENERAL_CONFIG,
3651 			       LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event) |
3652 			       LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, pulse_width) |
3653 			       LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event) |
3654 			       LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event),
3655 			       LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, pulse_width) |
3656 			       LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event));
3657 }
3658 
lan8814_ptp_disable_event(struct phy_device * phydev,int event)3659 static void lan8814_ptp_disable_event(struct phy_device *phydev, int event)
3660 {
3661 	/* Set target to too far in the future, effectively disabling it */
3662 	lan8814_ptp_set_target(phydev, event, 0xFFFFFFFF, 0);
3663 
3664 	/* And then reload once it reaches the target */
3665 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_PTP_GENERAL_CONFIG,
3666 			       LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event),
3667 			       LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event));
3668 }
3669 
lan8814_ptp_perout_off(struct phy_device * phydev,int pin)3670 static void lan8814_ptp_perout_off(struct phy_device *phydev, int pin)
3671 {
3672 	/* Disable gpio alternate function,
3673 	 * 1: select as gpio,
3674 	 * 0: select alt func
3675 	 */
3676 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3677 			       LAN8814_GPIO_EN_ADDR(pin),
3678 			       LAN8814_GPIO_EN_BIT(pin),
3679 			       LAN8814_GPIO_EN_BIT(pin));
3680 
3681 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3682 			       LAN8814_GPIO_DIR_ADDR(pin),
3683 			       LAN8814_GPIO_DIR_BIT(pin),
3684 			       0);
3685 
3686 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3687 			       LAN8814_GPIO_BUF_ADDR(pin),
3688 			       LAN8814_GPIO_BUF_BIT(pin),
3689 			       0);
3690 }
3691 
lan8814_ptp_perout_on(struct phy_device * phydev,int pin)3692 static void lan8814_ptp_perout_on(struct phy_device *phydev, int pin)
3693 {
3694 	/* Set as gpio output */
3695 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3696 			       LAN8814_GPIO_DIR_ADDR(pin),
3697 			       LAN8814_GPIO_DIR_BIT(pin),
3698 			       LAN8814_GPIO_DIR_BIT(pin));
3699 
3700 	/* Enable gpio 0:for alternate function, 1:gpio */
3701 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3702 			       LAN8814_GPIO_EN_ADDR(pin),
3703 			       LAN8814_GPIO_EN_BIT(pin),
3704 			       0);
3705 
3706 	/* Set buffer type to push pull */
3707 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3708 			       LAN8814_GPIO_BUF_ADDR(pin),
3709 			       LAN8814_GPIO_BUF_BIT(pin),
3710 			       LAN8814_GPIO_BUF_BIT(pin));
3711 }
3712 
lan8814_ptp_perout(struct ptp_clock_info * ptpci,struct ptp_clock_request * rq,int on)3713 static int lan8814_ptp_perout(struct ptp_clock_info *ptpci,
3714 			      struct ptp_clock_request *rq, int on)
3715 {
3716 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3717 							  ptp_clock_info);
3718 	struct phy_device *phydev = shared->phydev;
3719 	struct timespec64 ts_on, ts_period;
3720 	s64 on_nsec, period_nsec;
3721 	int pulse_width;
3722 	int pin, event;
3723 
3724 	mutex_lock(&shared->shared_lock);
3725 	event = rq->perout.index;
3726 	pin = ptp_find_pin(shared->ptp_clock, PTP_PF_PEROUT, event);
3727 	if (pin < 0 || pin >= LAN8814_PTP_PEROUT_NUM) {
3728 		mutex_unlock(&shared->shared_lock);
3729 		return -EBUSY;
3730 	}
3731 
3732 	if (!on) {
3733 		lan8814_ptp_perout_off(phydev, pin);
3734 		lan8814_ptp_disable_event(phydev, event);
3735 		mutex_unlock(&shared->shared_lock);
3736 		return 0;
3737 	}
3738 
3739 	ts_on.tv_sec = rq->perout.on.sec;
3740 	ts_on.tv_nsec = rq->perout.on.nsec;
3741 	on_nsec = timespec64_to_ns(&ts_on);
3742 
3743 	ts_period.tv_sec = rq->perout.period.sec;
3744 	ts_period.tv_nsec = rq->perout.period.nsec;
3745 	period_nsec = timespec64_to_ns(&ts_period);
3746 
3747 	if (period_nsec < 200) {
3748 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
3749 				    phydev_name(phydev));
3750 		mutex_unlock(&shared->shared_lock);
3751 		return -EOPNOTSUPP;
3752 	}
3753 
3754 	if (on_nsec >= period_nsec) {
3755 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
3756 				    phydev_name(phydev));
3757 		mutex_unlock(&shared->shared_lock);
3758 		return -EINVAL;
3759 	}
3760 
3761 	switch (on_nsec) {
3762 	case 200000000:
3763 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
3764 		break;
3765 	case 100000000:
3766 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
3767 		break;
3768 	case 50000000:
3769 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
3770 		break;
3771 	case 10000000:
3772 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
3773 		break;
3774 	case 5000000:
3775 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
3776 		break;
3777 	case 1000000:
3778 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
3779 		break;
3780 	case 500000:
3781 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
3782 		break;
3783 	case 100000:
3784 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
3785 		break;
3786 	case 50000:
3787 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
3788 		break;
3789 	case 10000:
3790 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
3791 		break;
3792 	case 5000:
3793 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
3794 		break;
3795 	case 1000:
3796 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
3797 		break;
3798 	case 500:
3799 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
3800 		break;
3801 	case 100:
3802 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
3803 		break;
3804 	default:
3805 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
3806 				    phydev_name(phydev));
3807 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
3808 		break;
3809 	}
3810 
3811 	/* Configure to pulse every period */
3812 	lan8814_ptp_enable_event(phydev, event, pulse_width);
3813 	lan8814_ptp_set_target(phydev, event, rq->perout.start.sec,
3814 			       rq->perout.start.nsec);
3815 	lan8814_ptp_set_reload(phydev, event, rq->perout.period.sec,
3816 			       rq->perout.period.nsec);
3817 	lan8814_ptp_perout_on(phydev, pin);
3818 	mutex_unlock(&shared->shared_lock);
3819 
3820 	return 0;
3821 }
3822 
lan8814_ptp_extts_on(struct phy_device * phydev,int pin,u32 flags)3823 static void lan8814_ptp_extts_on(struct phy_device *phydev, int pin, u32 flags)
3824 {
3825 	/* Set as gpio input */
3826 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3827 			       LAN8814_GPIO_DIR_ADDR(pin),
3828 			       LAN8814_GPIO_DIR_BIT(pin),
3829 			       0);
3830 
3831 	/* Map the pin to ltc pin 0 of the capture map registers */
3832 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3833 			       PTP_GPIO_CAP_MAP_LO, pin, pin);
3834 
3835 	/* Enable capture on the edges of the ltc pin */
3836 	if (flags & PTP_RISING_EDGE)
3837 		lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3838 				       PTP_GPIO_CAP_EN,
3839 				       PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(0),
3840 				       PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(0));
3841 	if (flags & PTP_FALLING_EDGE)
3842 		lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3843 				       PTP_GPIO_CAP_EN,
3844 				       PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(0),
3845 				       PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(0));
3846 
3847 	/* Enable interrupt top interrupt */
3848 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_COMMON_INT_ENA,
3849 			       PTP_COMMON_INT_ENA_GPIO_CAP_EN,
3850 			       PTP_COMMON_INT_ENA_GPIO_CAP_EN);
3851 }
3852 
lan8814_ptp_extts_off(struct phy_device * phydev,int pin)3853 static void lan8814_ptp_extts_off(struct phy_device *phydev, int pin)
3854 {
3855 	/* Set as gpio out */
3856 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3857 			       LAN8814_GPIO_DIR_ADDR(pin),
3858 			       LAN8814_GPIO_DIR_BIT(pin),
3859 			       LAN8814_GPIO_DIR_BIT(pin));
3860 
3861 	/* Enable alternate, 0:for alternate function, 1:gpio */
3862 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3863 			       LAN8814_GPIO_EN_ADDR(pin),
3864 			       LAN8814_GPIO_EN_BIT(pin),
3865 			       0);
3866 
3867 	/* Clear the mapping of pin to registers 0 of the capture registers */
3868 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
3869 			       PTP_GPIO_CAP_MAP_LO,
3870 			       GENMASK(3, 0),
3871 			       0);
3872 
3873 	/* Disable capture on both of the edges */
3874 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_GPIO_CAP_EN,
3875 			       PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
3876 			       PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
3877 			       0);
3878 
3879 	/* Disable interrupt top interrupt */
3880 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_COMMON_INT_ENA,
3881 			       PTP_COMMON_INT_ENA_GPIO_CAP_EN,
3882 			       0);
3883 }
3884 
lan8814_ptp_extts(struct ptp_clock_info * ptpci,struct ptp_clock_request * rq,int on)3885 static int lan8814_ptp_extts(struct ptp_clock_info *ptpci,
3886 			     struct ptp_clock_request *rq, int on)
3887 {
3888 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
3889 							  ptp_clock_info);
3890 	struct phy_device *phydev = shared->phydev;
3891 	int pin;
3892 
3893 	pin = ptp_find_pin(shared->ptp_clock, PTP_PF_EXTTS,
3894 			   rq->extts.index);
3895 	if (pin == -1 || pin != LAN8814_PTP_EXTTS_NUM)
3896 		return -EINVAL;
3897 
3898 	mutex_lock(&shared->shared_lock);
3899 	if (on)
3900 		lan8814_ptp_extts_on(phydev, pin, rq->extts.flags);
3901 	else
3902 		lan8814_ptp_extts_off(phydev, pin);
3903 
3904 	mutex_unlock(&shared->shared_lock);
3905 
3906 	return 0;
3907 }
3908 
lan8814_ptpci_enable(struct ptp_clock_info * ptpci,struct ptp_clock_request * rq,int on)3909 static int lan8814_ptpci_enable(struct ptp_clock_info *ptpci,
3910 				struct ptp_clock_request *rq, int on)
3911 {
3912 	switch (rq->type) {
3913 	case PTP_CLK_REQ_PEROUT:
3914 		return lan8814_ptp_perout(ptpci, rq, on);
3915 	case PTP_CLK_REQ_EXTTS:
3916 		return lan8814_ptp_extts(ptpci, rq, on);
3917 	default:
3918 		return -EINVAL;
3919 	}
3920 }
3921 
lan8814_ptpci_verify(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)3922 static int lan8814_ptpci_verify(struct ptp_clock_info *ptp, unsigned int pin,
3923 				enum ptp_pin_function func, unsigned int chan)
3924 {
3925 	switch (func) {
3926 	case PTP_PF_NONE:
3927 	case PTP_PF_PEROUT:
3928 		/* Only pins 0 and 1 can generate perout signals. And for pin 0
3929 		 * there is only chan 0 (event A) and for pin 1 there is only
3930 		 * chan 1 (event B)
3931 		 */
3932 		if (pin >= LAN8814_PTP_PEROUT_NUM || pin != chan)
3933 			return -1;
3934 		break;
3935 	case PTP_PF_EXTTS:
3936 		if (pin != LAN8814_PTP_EXTTS_NUM)
3937 			return -1;
3938 		break;
3939 	default:
3940 		return -1;
3941 	}
3942 
3943 	return 0;
3944 }
3945 
lan8814_get_sig_tx(struct sk_buff * skb,u16 * sig)3946 static bool lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
3947 {
3948 	struct ptp_header *ptp_header;
3949 	u32 type;
3950 
3951 	type = ptp_classify_raw(skb);
3952 	ptp_header = ptp_parse_header(skb, type);
3953 
3954 	if (!ptp_header)
3955 		return false;
3956 
3957 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
3958 	return true;
3959 }
3960 
lan8814_match_tx_skb(struct kszphy_ptp_priv * ptp_priv,u32 seconds,u32 nsec,u16 seq_id)3961 static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
3962 				 u32 seconds, u32 nsec, u16 seq_id)
3963 {
3964 	struct skb_shared_hwtstamps shhwtstamps;
3965 	struct sk_buff *skb, *skb_tmp;
3966 	unsigned long flags;
3967 	bool ret = false;
3968 	u16 skb_sig;
3969 
3970 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
3971 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
3972 		if (!lan8814_get_sig_tx(skb, &skb_sig))
3973 			continue;
3974 
3975 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
3976 			continue;
3977 
3978 		__skb_unlink(skb, &ptp_priv->tx_queue);
3979 		ret = true;
3980 		break;
3981 	}
3982 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
3983 
3984 	if (ret) {
3985 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
3986 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
3987 		skb_complete_tx_timestamp(skb, &shhwtstamps);
3988 	}
3989 }
3990 
lan8814_dequeue_tx_skb(struct kszphy_ptp_priv * ptp_priv)3991 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
3992 {
3993 	struct phy_device *phydev = ptp_priv->phydev;
3994 	u32 seconds, nsec;
3995 	u16 seq_id;
3996 
3997 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
3998 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
3999 }
4000 
lan8814_get_tx_ts(struct kszphy_ptp_priv * ptp_priv)4001 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
4002 {
4003 	struct phy_device *phydev = ptp_priv->phydev;
4004 	u32 reg;
4005 
4006 	do {
4007 		lan8814_dequeue_tx_skb(ptp_priv);
4008 
4009 		/* If other timestamps are available in the FIFO,
4010 		 * process them.
4011 		 */
4012 		reg = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4013 					   PTP_CAP_INFO);
4014 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
4015 }
4016 
lan8814_match_skb(struct kszphy_ptp_priv * ptp_priv,struct lan8814_ptp_rx_ts * rx_ts)4017 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
4018 			      struct lan8814_ptp_rx_ts *rx_ts)
4019 {
4020 	struct skb_shared_hwtstamps *shhwtstamps;
4021 	struct sk_buff *skb, *skb_tmp;
4022 	unsigned long flags;
4023 	bool ret = false;
4024 	u16 skb_sig;
4025 
4026 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
4027 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
4028 		if (!lan8814_get_sig_rx(skb, &skb_sig))
4029 			continue;
4030 
4031 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
4032 			continue;
4033 
4034 		__skb_unlink(skb, &ptp_priv->rx_queue);
4035 
4036 		ret = true;
4037 		break;
4038 	}
4039 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
4040 
4041 	if (ret) {
4042 		shhwtstamps = skb_hwtstamps(skb);
4043 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
4044 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
4045 		netif_rx(skb);
4046 	}
4047 
4048 	return ret;
4049 }
4050 
lan8814_match_rx_ts(struct kszphy_ptp_priv * ptp_priv,struct lan8814_ptp_rx_ts * rx_ts)4051 static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
4052 				struct lan8814_ptp_rx_ts *rx_ts)
4053 {
4054 	unsigned long flags;
4055 
4056 	/* If we failed to match the skb add it to the queue for when
4057 	 * the frame will come
4058 	 */
4059 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
4060 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
4061 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
4062 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
4063 	} else {
4064 		kfree(rx_ts);
4065 	}
4066 }
4067 
lan8814_get_rx_ts(struct kszphy_ptp_priv * ptp_priv)4068 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
4069 {
4070 	struct phy_device *phydev = ptp_priv->phydev;
4071 	struct lan8814_ptp_rx_ts *rx_ts;
4072 	u32 reg;
4073 
4074 	do {
4075 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
4076 		if (!rx_ts)
4077 			return;
4078 
4079 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
4080 				      &rx_ts->seq_id);
4081 		lan8814_match_rx_ts(ptp_priv, rx_ts);
4082 
4083 		/* If other timestamps are available in the FIFO,
4084 		 * process them.
4085 		 */
4086 		reg = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4087 					   PTP_CAP_INFO);
4088 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
4089 }
4090 
lan8814_handle_ptp_interrupt(struct phy_device * phydev,u16 status)4091 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
4092 {
4093 	struct kszphy_priv *priv = phydev->priv;
4094 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4095 
4096 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
4097 		lan8814_get_tx_ts(ptp_priv);
4098 
4099 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
4100 		lan8814_get_rx_ts(ptp_priv);
4101 
4102 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
4103 		lan8814_flush_fifo(phydev, true);
4104 		skb_queue_purge(&ptp_priv->tx_queue);
4105 	}
4106 
4107 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
4108 		lan8814_flush_fifo(phydev, false);
4109 		skb_queue_purge(&ptp_priv->rx_queue);
4110 	}
4111 }
4112 
lan8814_gpio_process_cap(struct lan8814_shared_priv * shared)4113 static int lan8814_gpio_process_cap(struct lan8814_shared_priv *shared)
4114 {
4115 	struct phy_device *phydev = shared->phydev;
4116 	struct ptp_clock_event ptp_event = {0};
4117 	unsigned long nsec;
4118 	s64 sec;
4119 	u16 tmp;
4120 
4121 	/* This is 0 because whatever was the input pin it was mapped it to
4122 	 * ltc gpio pin 0
4123 	 */
4124 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_GPIO_SEL,
4125 			       PTP_GPIO_SEL_GPIO_SEL(0),
4126 			       PTP_GPIO_SEL_GPIO_SEL(0));
4127 
4128 	tmp = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4129 				   PTP_GPIO_CAP_STS);
4130 	if (!(tmp & PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(0)) &&
4131 	    !(tmp & PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(0)))
4132 		return -1;
4133 
4134 	if (tmp & BIT(0)) {
4135 		sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4136 					   PTP_GPIO_RE_LTC_SEC_HI_CAP);
4137 		sec <<= 16;
4138 		sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4139 					    PTP_GPIO_RE_LTC_SEC_LO_CAP);
4140 
4141 		nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4142 					    PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
4143 		nsec <<= 16;
4144 		nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4145 					     PTP_GPIO_RE_LTC_NS_LO_CAP);
4146 	} else {
4147 		sec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4148 					   PTP_GPIO_FE_LTC_SEC_HI_CAP);
4149 		sec <<= 16;
4150 		sec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4151 					    PTP_GPIO_FE_LTC_SEC_LO_CAP);
4152 
4153 		nsec = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4154 					    PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
4155 		nsec <<= 16;
4156 		nsec |= lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4157 					     PTP_GPIO_RE_LTC_NS_LO_CAP);
4158 	}
4159 
4160 	ptp_event.index = 0;
4161 	ptp_event.timestamp = ktime_set(sec, nsec);
4162 	ptp_event.type = PTP_CLOCK_EXTTS;
4163 	ptp_clock_event(shared->ptp_clock, &ptp_event);
4164 
4165 	return 0;
4166 }
4167 
lan8814_handle_gpio_interrupt(struct phy_device * phydev,u16 status)4168 static int lan8814_handle_gpio_interrupt(struct phy_device *phydev, u16 status)
4169 {
4170 	struct lan8814_shared_priv *shared = phy_package_get_priv(phydev);
4171 	int ret;
4172 
4173 	mutex_lock(&shared->shared_lock);
4174 	ret = lan8814_gpio_process_cap(shared);
4175 	mutex_unlock(&shared->shared_lock);
4176 
4177 	return ret;
4178 }
4179 
lan8804_config_init(struct phy_device * phydev)4180 static int lan8804_config_init(struct phy_device *phydev)
4181 {
4182 	/* MDI-X setting for swap A,B transmit */
4183 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8804_ALIGN_SWAP,
4184 			       LAN8804_ALIGN_TX_A_B_SWAP_MASK,
4185 			       LAN8804_ALIGN_TX_A_B_SWAP);
4186 
4187 	/* Make sure that the PHY will not stop generating the clock when the
4188 	 * link partner goes down
4189 	 */
4190 	lanphy_write_page_reg(phydev, LAN8814_PAGE_SYSTEM_CTRL,
4191 			      LAN8814_CLOCK_MANAGEMENT, 0x27e);
4192 	lanphy_read_page_reg(phydev, LAN8814_PAGE_AFE_PMA, LAN8814_LINK_QUALITY);
4193 
4194 	return 0;
4195 }
4196 
lan8804_handle_interrupt(struct phy_device * phydev)4197 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
4198 {
4199 	int status;
4200 
4201 	status = phy_read(phydev, LAN8814_INTS);
4202 	if (status < 0) {
4203 		phy_error(phydev);
4204 		return IRQ_NONE;
4205 	}
4206 
4207 	if (status > 0)
4208 		phy_trigger_machine(phydev);
4209 
4210 	return IRQ_HANDLED;
4211 }
4212 
4213 #define LAN8804_OUTPUT_CONTROL			25
4214 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
4215 #define LAN8804_CONTROL				31
4216 #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
4217 
lan8804_config_intr(struct phy_device * phydev)4218 static int lan8804_config_intr(struct phy_device *phydev)
4219 {
4220 	int err;
4221 
4222 	/* This is an internal PHY of lan966x and is not possible to change the
4223 	 * polarity on the GIC found in lan966x, therefore change the polarity
4224 	 * of the interrupt in the PHY from being active low instead of active
4225 	 * high.
4226 	 */
4227 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
4228 
4229 	/* By default interrupt buffer is open-drain in which case the interrupt
4230 	 * can be active only low. Therefore change the interrupt buffer to be
4231 	 * push-pull to be able to change interrupt polarity
4232 	 */
4233 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
4234 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
4235 
4236 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
4237 		err = phy_read(phydev, LAN8814_INTS);
4238 		if (err < 0)
4239 			return err;
4240 
4241 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
4242 		if (err)
4243 			return err;
4244 	} else {
4245 		err = phy_write(phydev, LAN8814_INTC, 0);
4246 		if (err)
4247 			return err;
4248 
4249 		err = phy_read(phydev, LAN8814_INTS);
4250 		if (err < 0)
4251 			return err;
4252 	}
4253 
4254 	return 0;
4255 }
4256 
4257 /* Check if the PHY has 1588 support. There are multiple skus of the PHY and
4258  * some of them support PTP while others don't support it. This function will
4259  * return true is the sku supports it, otherwise will return false.
4260  */
lan8814_has_ptp(struct phy_device * phydev)4261 static bool lan8814_has_ptp(struct phy_device *phydev)
4262 {
4263 	struct kszphy_priv *priv = phydev->priv;
4264 
4265 	return priv->is_ptp_available;
4266 }
4267 
lan8814_handle_interrupt(struct phy_device * phydev)4268 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
4269 {
4270 	int ret = IRQ_NONE;
4271 	int irq_status;
4272 
4273 	irq_status = phy_read(phydev, LAN8814_INTS);
4274 	if (irq_status < 0) {
4275 		phy_error(phydev);
4276 		return IRQ_NONE;
4277 	}
4278 
4279 	if (irq_status & LAN8814_INT_LINK) {
4280 		phy_trigger_machine(phydev);
4281 		ret = IRQ_HANDLED;
4282 	}
4283 
4284 	if (!lan8814_has_ptp(phydev))
4285 		return ret;
4286 
4287 	while (true) {
4288 		irq_status = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4289 						  PTP_TSU_INT_STS);
4290 		if (!irq_status)
4291 			break;
4292 
4293 		lan8814_handle_ptp_interrupt(phydev, irq_status);
4294 		ret = IRQ_HANDLED;
4295 	}
4296 
4297 	if (!lan8814_handle_gpio_interrupt(phydev, irq_status))
4298 		ret = IRQ_HANDLED;
4299 
4300 	return ret;
4301 }
4302 
lan8814_ack_interrupt(struct phy_device * phydev)4303 static int lan8814_ack_interrupt(struct phy_device *phydev)
4304 {
4305 	/* bit[12..0] int status, which is a read and clear register. */
4306 	int rc;
4307 
4308 	rc = phy_read(phydev, LAN8814_INTS);
4309 
4310 	return (rc < 0) ? rc : 0;
4311 }
4312 
lan8814_config_intr(struct phy_device * phydev)4313 static int lan8814_config_intr(struct phy_device *phydev)
4314 {
4315 	int err;
4316 
4317 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, LAN8814_INTR_CTRL_REG,
4318 			      LAN8814_INTR_CTRL_REG_POLARITY |
4319 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
4320 
4321 	/* enable / disable interrupts */
4322 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
4323 		err = lan8814_ack_interrupt(phydev);
4324 		if (err)
4325 			return err;
4326 
4327 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
4328 	} else {
4329 		err = phy_write(phydev, LAN8814_INTC, 0);
4330 		if (err)
4331 			return err;
4332 
4333 		err = lan8814_ack_interrupt(phydev);
4334 	}
4335 
4336 	return err;
4337 }
4338 
lan8814_ptp_init(struct phy_device * phydev)4339 static void lan8814_ptp_init(struct phy_device *phydev)
4340 {
4341 	struct kszphy_priv *priv = phydev->priv;
4342 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4343 
4344 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
4345 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4346 		return;
4347 
4348 	if (!lan8814_has_ptp(phydev))
4349 		return;
4350 
4351 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4352 			      TSU_HARD_RESET, TSU_HARD_RESET_);
4353 
4354 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TX_MOD,
4355 			       PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_,
4356 			       PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_);
4357 
4358 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_RX_MOD,
4359 			       PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_,
4360 			       PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_);
4361 
4362 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4363 			      PTP_RX_PARSE_CONFIG, 0);
4364 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4365 			      PTP_TX_PARSE_CONFIG, 0);
4366 
4367 	/* Removing default registers configs related to L2 and IP */
4368 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4369 			      PTP_TX_PARSE_L2_ADDR_EN, 0);
4370 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4371 			      PTP_RX_PARSE_L2_ADDR_EN, 0);
4372 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4373 			      PTP_TX_PARSE_IP_ADDR_EN, 0);
4374 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4375 			      PTP_RX_PARSE_IP_ADDR_EN, 0);
4376 
4377 	/* Disable checking for minorVersionPTP field */
4378 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_RX_VERSION,
4379 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
4380 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS, PTP_TX_VERSION,
4381 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
4382 
4383 	skb_queue_head_init(&ptp_priv->tx_queue);
4384 	skb_queue_head_init(&ptp_priv->rx_queue);
4385 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
4386 	spin_lock_init(&ptp_priv->rx_ts_lock);
4387 
4388 	ptp_priv->phydev = phydev;
4389 
4390 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
4391 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4392 	ptp_priv->mii_ts.hwtstamp_set = lan8814_hwtstamp_set;
4393 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
4394 
4395 	phydev->mii_ts = &ptp_priv->mii_ts;
4396 
4397 	/* Timestamp selected by default to keep legacy API */
4398 	phydev->default_timestamp = true;
4399 }
4400 
__lan8814_ptp_probe_once(struct phy_device * phydev,char * pin_name,int gpios)4401 static int __lan8814_ptp_probe_once(struct phy_device *phydev, char *pin_name,
4402 				    int gpios)
4403 {
4404 	struct lan8814_shared_priv *shared = phy_package_get_priv(phydev);
4405 
4406 	shared->phydev = phydev;
4407 
4408 	/* Initialise shared lock for clock*/
4409 	mutex_init(&shared->shared_lock);
4410 
4411 	shared->pin_config = devm_kmalloc_array(&phydev->mdio.dev,
4412 						gpios,
4413 						sizeof(*shared->pin_config),
4414 						GFP_KERNEL);
4415 	if (!shared->pin_config)
4416 		return -ENOMEM;
4417 
4418 	for (int i = 0; i < gpios; i++) {
4419 		struct ptp_pin_desc *ptp_pin = &shared->pin_config[i];
4420 
4421 		memset(ptp_pin, 0, sizeof(*ptp_pin));
4422 		snprintf(ptp_pin->name,
4423 			 sizeof(ptp_pin->name), "%s_%02d", pin_name, i);
4424 		ptp_pin->index = i;
4425 		ptp_pin->func =  PTP_PF_NONE;
4426 	}
4427 
4428 	shared->ptp_clock_info.owner = THIS_MODULE;
4429 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
4430 	shared->ptp_clock_info.max_adj = 31249999;
4431 	shared->ptp_clock_info.n_alarm = 0;
4432 	shared->ptp_clock_info.n_ext_ts = LAN8814_PTP_EXTTS_NUM;
4433 	shared->ptp_clock_info.n_pins = gpios;
4434 	shared->ptp_clock_info.pps = 0;
4435 	shared->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
4436 						       PTP_FALLING_EDGE |
4437 						       PTP_STRICT_FLAGS;
4438 	shared->ptp_clock_info.supported_perout_flags = PTP_PEROUT_DUTY_CYCLE;
4439 	shared->ptp_clock_info.pin_config = shared->pin_config;
4440 	shared->ptp_clock_info.n_per_out = LAN8814_PTP_PEROUT_NUM;
4441 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
4442 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
4443 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
4444 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
4445 	shared->ptp_clock_info.getcrosststamp = NULL;
4446 	shared->ptp_clock_info.enable = lan8814_ptpci_enable;
4447 	shared->ptp_clock_info.verify = lan8814_ptpci_verify;
4448 
4449 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
4450 					       &phydev->mdio.dev);
4451 	if (IS_ERR(shared->ptp_clock)) {
4452 		phydev_err(phydev, "ptp_clock_register failed %pe\n",
4453 			   shared->ptp_clock);
4454 		return -EINVAL;
4455 	}
4456 
4457 	/* Check if PHC support is missing at the configuration level */
4458 	if (!shared->ptp_clock)
4459 		return 0;
4460 
4461 	phydev_dbg(phydev, "successfully registered ptp clock\n");
4462 
4463 	/* The EP.4 is shared between all the PHYs in the package and also it
4464 	 * can be accessed by any of the PHYs
4465 	 */
4466 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4467 			      LTC_HARD_RESET, LTC_HARD_RESET_);
4468 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_OPERATING_MODE,
4469 			      PTP_OPERATING_MODE_STANDALONE_);
4470 
4471 	/* Enable ptp to run LTC clock for ptp and gpio 1PPS operation */
4472 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, PTP_CMD_CTL,
4473 			      PTP_CMD_CTL_PTP_ENABLE_);
4474 
4475 	return 0;
4476 }
4477 
lan8814_ptp_probe_once(struct phy_device * phydev)4478 static int lan8814_ptp_probe_once(struct phy_device *phydev)
4479 {
4480 	if (!lan8814_has_ptp(phydev))
4481 		return 0;
4482 
4483 	return __lan8814_ptp_probe_once(phydev, "lan8814_ptp_pin",
4484 					LAN8814_PTP_GPIO_NUM);
4485 }
4486 
lan8814_setup_led(struct phy_device * phydev,int val)4487 static void lan8814_setup_led(struct phy_device *phydev, int val)
4488 {
4489 	int temp;
4490 
4491 	temp = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4492 				    LAN8814_LED_CTRL_1);
4493 
4494 	if (val)
4495 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
4496 	else
4497 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
4498 
4499 	lanphy_write_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
4500 			      LAN8814_LED_CTRL_1, temp);
4501 }
4502 
lan8814_config_init(struct phy_device * phydev)4503 static int lan8814_config_init(struct phy_device *phydev)
4504 {
4505 	struct kszphy_priv *lan8814 = phydev->priv;
4506 	int ret;
4507 
4508 	/* Based on the interface type select how the advertise ability is
4509 	 * encoded, to set as SGMII or as USGMII.
4510 	 */
4511 	if (phydev->interface == PHY_INTERFACE_MODE_QSGMII)
4512 		ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4513 					     LAN8814_QSGMII_TX_CONFIG,
4514 					     LAN8814_QSGMII_TX_CONFIG_QSGMII,
4515 					     LAN8814_QSGMII_TX_CONFIG_QSGMII);
4516 	else
4517 		ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4518 					     LAN8814_QSGMII_TX_CONFIG,
4519 					     LAN8814_QSGMII_TX_CONFIG_QSGMII,
4520 					     0);
4521 
4522 	if (ret < 0)
4523 		return ret;
4524 
4525 	/* MDI-X setting for swap A,B transmit */
4526 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8814_ALIGN_SWAP,
4527 			       LAN8814_ALIGN_TX_A_B_SWAP_MASK,
4528 			       LAN8814_ALIGN_TX_A_B_SWAP);
4529 
4530 	if (lan8814->led_mode >= 0)
4531 		lan8814_setup_led(phydev, lan8814->led_mode);
4532 
4533 	return 0;
4534 }
4535 
4536 /* It is expected that there will not be any 'lan8814_take_coma_mode'
4537  * function called in suspend. Because the GPIO line can be shared, so if one of
4538  * the phys goes back in coma mode, then all the other PHYs will go, which is
4539  * wrong.
4540  */
lan8814_release_coma_mode(struct phy_device * phydev)4541 static int lan8814_release_coma_mode(struct phy_device *phydev)
4542 {
4543 	struct gpio_desc *gpiod;
4544 
4545 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
4546 					GPIOD_OUT_HIGH_OPEN_DRAIN |
4547 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
4548 	if (IS_ERR(gpiod))
4549 		return PTR_ERR(gpiod);
4550 
4551 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
4552 	gpiod_set_value_cansleep(gpiod, 0);
4553 
4554 	return 0;
4555 }
4556 
lan8814_clear_2psp_bit(struct phy_device * phydev)4557 static void lan8814_clear_2psp_bit(struct phy_device *phydev)
4558 {
4559 	/* It was noticed that when traffic is passing through the PHY and the
4560 	 * cable is removed then the LED was still on even though there is no
4561 	 * link
4562 	 */
4563 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL, LAN8814_EEE_STATE,
4564 			       LAN8814_EEE_STATE_MASK2P5P,
4565 			       0);
4566 }
4567 
lan8814_update_meas_time(struct phy_device * phydev)4568 static void lan8814_update_meas_time(struct phy_device *phydev)
4569 {
4570 	/* By setting the measure time to a value of 0xb this will allow cables
4571 	 * longer than 100m to be used. This configuration can be used
4572 	 * regardless of the mode of operation of the PHY
4573 	 */
4574 	lanphy_modify_page_reg(phydev, LAN8814_PAGE_AFE_PMA, LAN8814_PD_CONTROLS,
4575 			       LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK,
4576 			       LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL);
4577 }
4578 
lan8814_probe(struct phy_device * phydev)4579 static int lan8814_probe(struct phy_device *phydev)
4580 {
4581 	const struct kszphy_type *type = phydev->drv->driver_data;
4582 	struct kszphy_priv *priv;
4583 	u16 addr;
4584 	int err;
4585 
4586 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
4587 	if (!priv)
4588 		return -ENOMEM;
4589 
4590 	phydev->priv = priv;
4591 
4592 	priv->type = type;
4593 
4594 	kszphy_parse_led_mode(phydev);
4595 
4596 	/* Strap-in value for PHY address, below register read gives starting
4597 	 * phy address value
4598 	 */
4599 	addr = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS, 0) & 0x1F;
4600 	devm_phy_package_join(&phydev->mdio.dev, phydev,
4601 			      addr, sizeof(struct lan8814_shared_priv));
4602 
4603 	/* There are lan8814 SKUs that don't support PTP. Make sure that for
4604 	 * those skus no PTP device is created. Here we check if the SKU
4605 	 * supports PTP.
4606 	 */
4607 	err = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4608 				   LAN8814_SKUS);
4609 	if (err < 0)
4610 		return err;
4611 
4612 	priv->is_ptp_available = err == LAN8814_REV_LAN8814 ||
4613 				 err == LAN8814_REV_LAN8818;
4614 
4615 	if (phy_package_init_once(phydev)) {
4616 		/* Reset the PHY */
4617 		lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
4618 				       LAN8814_QSGMII_SOFT_RESET,
4619 				       LAN8814_QSGMII_SOFT_RESET_BIT,
4620 				       LAN8814_QSGMII_SOFT_RESET_BIT);
4621 
4622 		err = lan8814_release_coma_mode(phydev);
4623 		if (err)
4624 			return err;
4625 
4626 		err = lan8814_ptp_probe_once(phydev);
4627 		if (err)
4628 			return err;
4629 	}
4630 
4631 	lan8814_ptp_init(phydev);
4632 
4633 	/* Errata workarounds */
4634 	lan8814_clear_2psp_bit(phydev);
4635 	lan8814_update_meas_time(phydev);
4636 
4637 	return 0;
4638 }
4639 
4640 #define LAN8841_MMD_TIMER_REG			0
4641 #define LAN8841_MMD0_REGISTER_17		17
4642 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
4643 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
4644 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
4645 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
4646 #define LAN8841_MMD_ANALOG_REG			28
4647 #define LAN8841_ANALOG_CONTROL_1		1
4648 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
4649 #define LAN8841_ANALOG_CONTROL_10		13
4650 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
4651 #define LAN8841_ANALOG_CONTROL_11		14
4652 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
4653 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
4654 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
4655 #define LAN8841_BTRX_POWER_DOWN			70
4656 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
4657 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
4658 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
4659 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
4660 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
4661 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
4662 #define LAN8841_ADC_CHANNEL_MASK		198
4663 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
4664 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
4665 #define LAN8841_PTP_RX_VERSION			374
4666 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
4667 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
4668 #define LAN8841_PTP_TX_VERSION			438
4669 #define LAN8841_PTP_CMD_CTL			256
4670 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
4671 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
4672 #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
4673 #define LAN8841_PTP_RX_PARSE_CONFIG		368
4674 #define LAN8841_PTP_TX_PARSE_CONFIG		432
4675 #define LAN8841_PTP_RX_MODE			381
4676 #define LAN8841_PTP_INSERT_TS_EN		BIT(0)
4677 #define LAN8841_PTP_INSERT_TS_32BIT		BIT(1)
4678 
lan8841_config_init(struct phy_device * phydev)4679 static int lan8841_config_init(struct phy_device *phydev)
4680 {
4681 	int ret;
4682 
4683 	ret = ksz9131_config_init(phydev);
4684 	if (ret)
4685 		return ret;
4686 
4687 	/* Initialize the HW by resetting everything */
4688 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4689 		       LAN8841_PTP_CMD_CTL,
4690 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
4691 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
4692 
4693 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4694 		       LAN8841_PTP_CMD_CTL,
4695 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
4696 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
4697 
4698 	/* Don't process any frames */
4699 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4700 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
4701 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4702 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
4703 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4704 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
4705 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4706 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
4707 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4708 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
4709 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4710 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
4711 
4712 	/* Disable checking for minorVersionPTP field */
4713 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4714 		      LAN8841_PTP_RX_VERSION, 0xff00);
4715 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4716 		      LAN8841_PTP_TX_VERSION, 0xff00);
4717 
4718 	/* 100BT Clause 40 improvement errata */
4719 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
4720 		      LAN8841_ANALOG_CONTROL_1,
4721 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
4722 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
4723 		      LAN8841_ANALOG_CONTROL_10,
4724 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
4725 
4726 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
4727 	 * Magnetics
4728 	 */
4729 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4730 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
4731 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
4732 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
4733 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
4734 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
4735 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
4736 			      LAN8841_BTRX_POWER_DOWN,
4737 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
4738 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
4739 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
4740 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
4741 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
4742 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
4743 	}
4744 
4745 	/* LDO Adjustment errata */
4746 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
4747 		      LAN8841_ANALOG_CONTROL_11,
4748 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
4749 
4750 	/* 100BT RGMII latency tuning errata */
4751 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
4752 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
4753 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
4754 		      LAN8841_MMD0_REGISTER_17,
4755 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
4756 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
4757 
4758 	return 0;
4759 }
4760 
4761 #define LAN8841_OUTPUT_CTRL			25
4762 #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
4763 #define LAN8841_INT_PTP				BIT(9)
4764 
lan8841_config_intr(struct phy_device * phydev)4765 static int lan8841_config_intr(struct phy_device *phydev)
4766 {
4767 	int err;
4768 
4769 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
4770 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
4771 
4772 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
4773 		err = phy_read(phydev, LAN8814_INTS);
4774 		if (err < 0)
4775 			return err;
4776 
4777 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
4778 		 * even if it PTP is not enabled. Because the underneath blocks
4779 		 * will not enable the PTP so we will never get the PTP
4780 		 * interrupt.
4781 		 */
4782 		err = phy_write(phydev, LAN8814_INTC,
4783 				LAN8814_INT_LINK | LAN8841_INT_PTP);
4784 	} else {
4785 		err = phy_write(phydev, LAN8814_INTC, 0);
4786 		if (err)
4787 			return err;
4788 
4789 		err = phy_read(phydev, LAN8814_INTS);
4790 		if (err < 0)
4791 			return err;
4792 
4793 		/* Getting a positive value doesn't mean that is an error, it
4794 		 * just indicates what was the status. Therefore make sure to
4795 		 * clear the value and say that there is no error.
4796 		 */
4797 		err = 0;
4798 	}
4799 
4800 	return err;
4801 }
4802 
4803 #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
4804 #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
4805 #define LAN8841_PTP_TX_EGRESS_NS_LO			451
4806 #define LAN8841_PTP_TX_EGRESS_NS_HI			450
4807 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
4808 #define LAN8841_PTP_TX_MSG_HEADER2			455
4809 
lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv * ptp_priv,u32 * sec,u32 * nsec,u16 * seq)4810 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
4811 				  u32 *sec, u32 *nsec, u16 *seq)
4812 {
4813 	struct phy_device *phydev = ptp_priv->phydev;
4814 
4815 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
4816 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
4817 		return false;
4818 
4819 	*nsec = ((*nsec & 0x3fff) << 16);
4820 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
4821 
4822 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
4823 	*sec = *sec << 16;
4824 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
4825 
4826 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
4827 
4828 	return true;
4829 }
4830 
lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv * ptp_priv)4831 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
4832 {
4833 	u32 sec, nsec;
4834 	u16 seq;
4835 
4836 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
4837 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
4838 }
4839 
4840 #define LAN8841_PTP_INT_STS			259
4841 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
4842 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
4843 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
4844 
lan8841_ptp_flush_fifo(struct kszphy_ptp_priv * ptp_priv)4845 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv)
4846 {
4847 	struct phy_device *phydev = ptp_priv->phydev;
4848 	int i;
4849 
4850 	for (i = 0; i < FIFO_SIZE; ++i)
4851 		phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
4852 
4853 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
4854 }
4855 
4856 #define LAN8841_PTP_GPIO_CAP_STS			506
4857 #define LAN8841_PTP_GPIO_SEL				327
4858 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
4859 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
4860 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
4861 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
4862 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
4863 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
4864 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
4865 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
4866 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
4867 
lan8841_gpio_process_cap(struct kszphy_ptp_priv * ptp_priv)4868 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
4869 {
4870 	struct phy_device *phydev = ptp_priv->phydev;
4871 	struct ptp_clock_event ptp_event = {0};
4872 	int pin, ret, tmp;
4873 	s32 sec, nsec;
4874 
4875 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
4876 	if (pin == -1)
4877 		return;
4878 
4879 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
4880 	if (tmp < 0)
4881 		return;
4882 
4883 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
4884 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
4885 	if (ret)
4886 		return;
4887 
4888 	mutex_lock(&ptp_priv->ptp_lock);
4889 	if (tmp & BIT(pin)) {
4890 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
4891 		sec <<= 16;
4892 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
4893 
4894 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
4895 		nsec <<= 16;
4896 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
4897 	} else {
4898 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
4899 		sec <<= 16;
4900 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
4901 
4902 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
4903 		nsec <<= 16;
4904 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
4905 	}
4906 	mutex_unlock(&ptp_priv->ptp_lock);
4907 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
4908 	if (ret)
4909 		return;
4910 
4911 	ptp_event.index = 0;
4912 	ptp_event.timestamp = ktime_set(sec, nsec);
4913 	ptp_event.type = PTP_CLOCK_EXTTS;
4914 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
4915 }
4916 
lan8841_handle_ptp_interrupt(struct phy_device * phydev)4917 static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
4918 {
4919 	struct kszphy_priv *priv = phydev->priv;
4920 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4921 	u16 status;
4922 
4923 	do {
4924 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
4925 
4926 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
4927 			lan8841_ptp_process_tx_ts(ptp_priv);
4928 
4929 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
4930 			lan8841_gpio_process_cap(ptp_priv);
4931 
4932 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
4933 			lan8841_ptp_flush_fifo(ptp_priv);
4934 			skb_queue_purge(&ptp_priv->tx_queue);
4935 		}
4936 
4937 	} while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT |
4938 			   LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT |
4939 			   LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT));
4940 }
4941 
4942 #define LAN8841_INTS_PTP		BIT(9)
4943 
lan8841_handle_interrupt(struct phy_device * phydev)4944 static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
4945 {
4946 	irqreturn_t ret = IRQ_NONE;
4947 	int irq_status;
4948 
4949 	irq_status = phy_read(phydev, LAN8814_INTS);
4950 	if (irq_status < 0) {
4951 		phy_error(phydev);
4952 		return IRQ_NONE;
4953 	}
4954 
4955 	if (irq_status & LAN8814_INT_LINK) {
4956 		phy_trigger_machine(phydev);
4957 		ret = IRQ_HANDLED;
4958 	}
4959 
4960 	if (irq_status & LAN8841_INTS_PTP) {
4961 		lan8841_handle_ptp_interrupt(phydev);
4962 		ret = IRQ_HANDLED;
4963 	}
4964 
4965 	return ret;
4966 }
4967 
lan8841_ts_info(struct mii_timestamper * mii_ts,struct kernel_ethtool_ts_info * info)4968 static int lan8841_ts_info(struct mii_timestamper *mii_ts,
4969 			   struct kernel_ethtool_ts_info *info)
4970 {
4971 	struct kszphy_ptp_priv *ptp_priv;
4972 
4973 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
4974 
4975 	info->phc_index = ptp_priv->ptp_clock ?
4976 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
4977 	if (info->phc_index == -1)
4978 		return 0;
4979 
4980 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
4981 				SOF_TIMESTAMPING_RX_HARDWARE |
4982 				SOF_TIMESTAMPING_RAW_HARDWARE;
4983 
4984 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
4985 			 (1 << HWTSTAMP_TX_ON) |
4986 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
4987 
4988 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
4989 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
4990 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
4991 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
4992 
4993 	return 0;
4994 }
4995 
4996 #define LAN8841_PTP_INT_EN			260
4997 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
4998 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
4999 
lan8841_ptp_enable_processing(struct kszphy_ptp_priv * ptp_priv,bool enable)5000 static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv,
5001 					  bool enable)
5002 {
5003 	struct phy_device *phydev = ptp_priv->phydev;
5004 
5005 	if (enable) {
5006 		/* Enable interrupts on the TX side */
5007 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
5008 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
5009 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN,
5010 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
5011 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN);
5012 
5013 		/* Enable the modification of the frame on RX side,
5014 		 * this will add the ns and 2 bits of sec in the reserved field
5015 		 * of the PTP header
5016 		 */
5017 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
5018 			       LAN8841_PTP_RX_MODE,
5019 			       LAN8841_PTP_INSERT_TS_EN |
5020 			       LAN8841_PTP_INSERT_TS_32BIT,
5021 			       LAN8841_PTP_INSERT_TS_EN |
5022 			       LAN8841_PTP_INSERT_TS_32BIT);
5023 
5024 		ptp_schedule_worker(ptp_priv->ptp_clock, 0);
5025 	} else {
5026 		/* Disable interrupts on the TX side */
5027 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
5028 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
5029 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0);
5030 
5031 		/* Disable modification of the RX frames */
5032 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
5033 			       LAN8841_PTP_RX_MODE,
5034 			       LAN8841_PTP_INSERT_TS_EN |
5035 			       LAN8841_PTP_INSERT_TS_32BIT, 0);
5036 
5037 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
5038 	}
5039 }
5040 
5041 #define LAN8841_PTP_RX_TIMESTAMP_EN		379
5042 #define LAN8841_PTP_TX_TIMESTAMP_EN		443
5043 #define LAN8841_PTP_TX_MOD			445
5044 
lan8841_hwtstamp_set(struct mii_timestamper * mii_ts,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)5045 static int lan8841_hwtstamp_set(struct mii_timestamper *mii_ts,
5046 				struct kernel_hwtstamp_config *config,
5047 				struct netlink_ext_ack *extack)
5048 {
5049 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
5050 	struct phy_device *phydev = ptp_priv->phydev;
5051 	int txcfg = 0, rxcfg = 0;
5052 	int pkt_ts_enable;
5053 
5054 	ptp_priv->hwts_tx_type = config->tx_type;
5055 	ptp_priv->rx_filter = config->rx_filter;
5056 
5057 	switch (config->rx_filter) {
5058 	case HWTSTAMP_FILTER_NONE:
5059 		ptp_priv->layer = 0;
5060 		ptp_priv->version = 0;
5061 		break;
5062 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
5063 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
5064 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
5065 		ptp_priv->layer = PTP_CLASS_L4;
5066 		ptp_priv->version = PTP_CLASS_V2;
5067 		break;
5068 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
5069 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
5070 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
5071 		ptp_priv->layer = PTP_CLASS_L2;
5072 		ptp_priv->version = PTP_CLASS_V2;
5073 		break;
5074 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
5075 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
5076 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
5077 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
5078 		ptp_priv->version = PTP_CLASS_V2;
5079 		break;
5080 	default:
5081 		return -ERANGE;
5082 	}
5083 
5084 	/* Setup parsing of the frames and enable the timestamping for ptp
5085 	 * frames
5086 	 */
5087 	if (ptp_priv->layer & PTP_CLASS_L2) {
5088 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
5089 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
5090 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
5091 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
5092 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
5093 	}
5094 
5095 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
5096 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
5097 
5098 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
5099 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
5100 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
5101 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
5102 
5103 	/* Enable / disable of the TX timestamp in the SYNC frames */
5104 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
5105 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
5106 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
5107 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
5108 
5109 	/* Now enable/disable the timestamping */
5110 	lan8841_ptp_enable_processing(ptp_priv,
5111 				      config->rx_filter != HWTSTAMP_FILTER_NONE);
5112 
5113 	skb_queue_purge(&ptp_priv->tx_queue);
5114 
5115 	lan8841_ptp_flush_fifo(ptp_priv);
5116 
5117 	return 0;
5118 }
5119 
lan8841_rxtstamp(struct mii_timestamper * mii_ts,struct sk_buff * skb,int type)5120 static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts,
5121 			     struct sk_buff *skb, int type)
5122 {
5123 	struct kszphy_ptp_priv *ptp_priv =
5124 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
5125 	struct ptp_header *header = ptp_parse_header(skb, type);
5126 	struct skb_shared_hwtstamps *shhwtstamps;
5127 	struct timespec64 ts;
5128 	unsigned long flags;
5129 	u32 ts_header;
5130 
5131 	if (!header)
5132 		return false;
5133 
5134 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
5135 	    type == PTP_CLASS_NONE)
5136 		return false;
5137 
5138 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
5139 		return false;
5140 
5141 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
5142 	ts.tv_sec = ptp_priv->seconds;
5143 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
5144 	ts_header = __be32_to_cpu(header->reserved2);
5145 
5146 	shhwtstamps = skb_hwtstamps(skb);
5147 	memset(shhwtstamps, 0, sizeof(*shhwtstamps));
5148 
5149 	/* Check for any wrap arounds for the second part */
5150 	if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3)
5151 		ts.tv_sec -= GENMASK(1, 0) + 1;
5152 	else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0)
5153 		ts.tv_sec += 1;
5154 
5155 	shhwtstamps->hwtstamp =
5156 		ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30,
5157 			  ts_header & GENMASK(29, 0));
5158 	header->reserved2 = 0;
5159 
5160 	netif_rx(skb);
5161 
5162 	return true;
5163 }
5164 
5165 #define LAN8841_EVENT_A		0
5166 #define LAN8841_EVENT_B		1
5167 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
5168 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
5169 #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
5170 #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
5171 
lan8841_ptp_set_target(struct kszphy_ptp_priv * ptp_priv,u8 event,s64 sec,u32 nsec)5172 static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
5173 				  s64 sec, u32 nsec)
5174 {
5175 	struct phy_device *phydev = ptp_priv->phydev;
5176 	int ret;
5177 
5178 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
5179 			    upper_16_bits(sec));
5180 	if (ret)
5181 		return ret;
5182 
5183 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
5184 			    lower_16_bits(sec));
5185 	if (ret)
5186 		return ret;
5187 
5188 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
5189 			    upper_16_bits(nsec));
5190 	if (ret)
5191 		return ret;
5192 
5193 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
5194 			    lower_16_bits(nsec));
5195 }
5196 
5197 #define LAN8841_BUFFER_TIME	2
5198 
lan8841_ptp_update_target(struct kszphy_ptp_priv * ptp_priv,const struct timespec64 * ts)5199 static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
5200 				     const struct timespec64 *ts)
5201 {
5202 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
5203 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
5204 }
5205 
5206 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
5207 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
5208 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
5209 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
5210 
lan8841_ptp_set_reload(struct kszphy_ptp_priv * ptp_priv,u8 event,s64 sec,u32 nsec)5211 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
5212 				  s64 sec, u32 nsec)
5213 {
5214 	struct phy_device *phydev = ptp_priv->phydev;
5215 	int ret;
5216 
5217 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
5218 			    upper_16_bits(sec));
5219 	if (ret)
5220 		return ret;
5221 
5222 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
5223 			    lower_16_bits(sec));
5224 	if (ret)
5225 		return ret;
5226 
5227 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
5228 			    upper_16_bits(nsec));
5229 	if (ret)
5230 		return ret;
5231 
5232 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
5233 			     lower_16_bits(nsec));
5234 }
5235 
5236 #define LAN8841_PTP_LTC_SET_SEC_HI	262
5237 #define LAN8841_PTP_LTC_SET_SEC_MID	263
5238 #define LAN8841_PTP_LTC_SET_SEC_LO	264
5239 #define LAN8841_PTP_LTC_SET_NS_HI	265
5240 #define LAN8841_PTP_LTC_SET_NS_LO	266
5241 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
5242 
lan8841_ptp_settime64(struct ptp_clock_info * ptp,const struct timespec64 * ts)5243 static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
5244 				 const struct timespec64 *ts)
5245 {
5246 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5247 							ptp_clock_info);
5248 	struct phy_device *phydev = ptp_priv->phydev;
5249 	unsigned long flags;
5250 	int ret;
5251 
5252 	/* Set the value to be stored */
5253 	mutex_lock(&ptp_priv->ptp_lock);
5254 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
5255 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
5256 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
5257 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
5258 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
5259 
5260 	/* Set the command to load the LTC */
5261 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
5262 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
5263 	ret = lan8841_ptp_update_target(ptp_priv, ts);
5264 	mutex_unlock(&ptp_priv->ptp_lock);
5265 
5266 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
5267 	ptp_priv->seconds = ts->tv_sec;
5268 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
5269 
5270 	return ret;
5271 }
5272 
5273 #define LAN8841_PTP_LTC_RD_SEC_HI	358
5274 #define LAN8841_PTP_LTC_RD_SEC_MID	359
5275 #define LAN8841_PTP_LTC_RD_SEC_LO	360
5276 #define LAN8841_PTP_LTC_RD_NS_HI	361
5277 #define LAN8841_PTP_LTC_RD_NS_LO	362
5278 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
5279 
lan8841_ptp_gettime64(struct ptp_clock_info * ptp,struct timespec64 * ts)5280 static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
5281 				 struct timespec64 *ts)
5282 {
5283 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5284 							ptp_clock_info);
5285 	struct phy_device *phydev = ptp_priv->phydev;
5286 	time64_t s;
5287 	s64 ns;
5288 
5289 	mutex_lock(&ptp_priv->ptp_lock);
5290 	/* Issue the command to read the LTC */
5291 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
5292 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
5293 
5294 	/* Read the LTC */
5295 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
5296 	s <<= 16;
5297 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
5298 	s <<= 16;
5299 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
5300 
5301 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
5302 	ns <<= 16;
5303 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
5304 	mutex_unlock(&ptp_priv->ptp_lock);
5305 
5306 	set_normalized_timespec64(ts, s, ns);
5307 	return 0;
5308 }
5309 
lan8841_ptp_getseconds(struct ptp_clock_info * ptp,struct timespec64 * ts)5310 static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp,
5311 				   struct timespec64 *ts)
5312 {
5313 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5314 							ptp_clock_info);
5315 	struct phy_device *phydev = ptp_priv->phydev;
5316 	time64_t s;
5317 
5318 	mutex_lock(&ptp_priv->ptp_lock);
5319 	/* Issue the command to read the LTC */
5320 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
5321 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
5322 
5323 	/* Read the LTC */
5324 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
5325 	s <<= 16;
5326 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
5327 	s <<= 16;
5328 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
5329 	mutex_unlock(&ptp_priv->ptp_lock);
5330 
5331 	set_normalized_timespec64(ts, s, 0);
5332 }
5333 
5334 #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
5335 #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
5336 #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
5337 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
5338 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
5339 
lan8841_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)5340 static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5341 {
5342 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5343 							ptp_clock_info);
5344 	struct phy_device *phydev = ptp_priv->phydev;
5345 	struct timespec64 ts;
5346 	bool add = true;
5347 	u32 nsec;
5348 	s32 sec;
5349 	int ret;
5350 
5351 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
5352 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
5353 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
5354 	 * increment so the value can be bigger than 15 sec. Therefore limit the
5355 	 * possible adjustments so we will not have these corner cases
5356 	 */
5357 	if (delta > 10000000000LL || delta < -10000000000LL) {
5358 		/* The timeadjustment is too big, so fall back using set time */
5359 		u64 now;
5360 
5361 		ptp->gettime64(ptp, &ts);
5362 
5363 		now = ktime_to_ns(timespec64_to_ktime(ts));
5364 		ts = ns_to_timespec64(now + delta);
5365 
5366 		ptp->settime64(ptp, &ts);
5367 		return 0;
5368 	}
5369 
5370 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
5371 	if (delta < 0 && nsec != 0) {
5372 		/* It is not allowed to adjust low the nsec part, therefore
5373 		 * subtract more from second part and add to nanosecond such
5374 		 * that would roll over, so the second part will increase
5375 		 */
5376 		sec--;
5377 		nsec = NSEC_PER_SEC - nsec;
5378 	}
5379 
5380 	/* Calculate the adjustments and the direction */
5381 	if (delta < 0)
5382 		add = false;
5383 
5384 	if (nsec > 0)
5385 		/* add 8 ns to cover the likely normal increment */
5386 		nsec += 8;
5387 
5388 	if (nsec >= NSEC_PER_SEC) {
5389 		/* carry into seconds */
5390 		sec++;
5391 		nsec -= NSEC_PER_SEC;
5392 	}
5393 
5394 	mutex_lock(&ptp_priv->ptp_lock);
5395 	if (sec) {
5396 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
5397 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
5398 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
5399 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
5400 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
5401 	}
5402 
5403 	if (nsec) {
5404 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
5405 			      nsec & 0xffff);
5406 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
5407 			      (nsec >> 16) & 0x3fff);
5408 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
5409 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
5410 	}
5411 	mutex_unlock(&ptp_priv->ptp_lock);
5412 
5413 	/* Update the target clock */
5414 	ptp->gettime64(ptp, &ts);
5415 	mutex_lock(&ptp_priv->ptp_lock);
5416 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
5417 	mutex_unlock(&ptp_priv->ptp_lock);
5418 
5419 	return ret;
5420 }
5421 
5422 #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
5423 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
5424 #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
5425 
lan8841_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)5426 static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
5427 {
5428 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5429 							ptp_clock_info);
5430 	struct phy_device *phydev = ptp_priv->phydev;
5431 	bool faster = true;
5432 	u32 rate;
5433 
5434 	if (!scaled_ppm)
5435 		return 0;
5436 
5437 	if (scaled_ppm < 0) {
5438 		scaled_ppm = -scaled_ppm;
5439 		faster = false;
5440 	}
5441 
5442 	rate = LAN8841_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
5443 	rate += (LAN8841_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
5444 
5445 	mutex_lock(&ptp_priv->ptp_lock);
5446 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
5447 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
5448 			     : upper_16_bits(rate) & 0x3fff);
5449 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
5450 	mutex_unlock(&ptp_priv->ptp_lock);
5451 
5452 	return 0;
5453 }
5454 
lan8841_ptp_verify(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)5455 static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
5456 			      enum ptp_pin_function func, unsigned int chan)
5457 {
5458 	switch (func) {
5459 	case PTP_PF_NONE:
5460 	case PTP_PF_PEROUT:
5461 	case PTP_PF_EXTTS:
5462 		break;
5463 	default:
5464 		return -1;
5465 	}
5466 
5467 	return 0;
5468 }
5469 
5470 #define LAN8841_PTP_GPIO_NUM	10
5471 #define LAN8841_GPIO_EN		128
5472 #define LAN8841_GPIO_DIR	129
5473 #define LAN8841_GPIO_BUF	130
5474 
lan8841_ptp_perout_off(struct kszphy_ptp_priv * ptp_priv,int pin)5475 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
5476 {
5477 	struct phy_device *phydev = ptp_priv->phydev;
5478 	int ret;
5479 
5480 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
5481 	if (ret)
5482 		return ret;
5483 
5484 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
5485 	if (ret)
5486 		return ret;
5487 
5488 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
5489 }
5490 
lan8841_ptp_perout_on(struct kszphy_ptp_priv * ptp_priv,int pin)5491 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
5492 {
5493 	struct phy_device *phydev = ptp_priv->phydev;
5494 	int ret;
5495 
5496 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
5497 	if (ret)
5498 		return ret;
5499 
5500 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
5501 	if (ret)
5502 		return ret;
5503 
5504 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
5505 }
5506 
5507 #define LAN8841_GPIO_DATA_SEL1				131
5508 #define LAN8841_GPIO_DATA_SEL2				132
5509 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
5510 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
5511 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
5512 #define LAN8841_PTP_GENERAL_CONFIG			257
5513 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
5514 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
5515 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
5516 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
5517 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
5518 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
5519 
lan8841_ptp_remove_event(struct kszphy_ptp_priv * ptp_priv,int pin,u8 event)5520 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
5521 				    u8 event)
5522 {
5523 	struct phy_device *phydev = ptp_priv->phydev;
5524 	u16 tmp;
5525 	int ret;
5526 
5527 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
5528 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
5529 	 * depending on the pin, it requires to read a different register
5530 	 */
5531 	if (pin < 5) {
5532 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
5533 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
5534 	} else {
5535 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
5536 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
5537 	}
5538 	if (ret)
5539 		return ret;
5540 
5541 	/* Disable the event */
5542 	if (event == LAN8841_EVENT_A)
5543 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
5544 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
5545 	else
5546 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
5547 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
5548 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
5549 }
5550 
lan8841_ptp_enable_event(struct kszphy_ptp_priv * ptp_priv,int pin,u8 event,int pulse_width)5551 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
5552 				    u8 event, int pulse_width)
5553 {
5554 	struct phy_device *phydev = ptp_priv->phydev;
5555 	u16 tmp;
5556 	int ret;
5557 
5558 	/* Enable the event */
5559 	if (event == LAN8841_EVENT_A)
5560 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
5561 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
5562 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
5563 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
5564 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
5565 	else
5566 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
5567 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
5568 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
5569 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
5570 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
5571 	if (ret)
5572 		return ret;
5573 
5574 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
5575 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
5576 	 * depending on the pin, it requires to read a different register
5577 	 */
5578 	if (event == LAN8841_EVENT_A)
5579 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
5580 	else
5581 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
5582 
5583 	if (pin < 5)
5584 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
5585 				       tmp << (3 * pin));
5586 	else
5587 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
5588 				       tmp << (3 * (pin - 5)));
5589 
5590 	return ret;
5591 }
5592 
5593 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
5594 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
5595 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
5596 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
5597 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
5598 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
5599 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
5600 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
5601 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
5602 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
5603 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
5604 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
5605 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
5606 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
5607 
lan8841_ptp_perout(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)5608 static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
5609 			      struct ptp_clock_request *rq, int on)
5610 {
5611 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5612 							ptp_clock_info);
5613 	struct phy_device *phydev = ptp_priv->phydev;
5614 	struct timespec64 ts_on, ts_period;
5615 	s64 on_nsec, period_nsec;
5616 	int pulse_width;
5617 	int pin;
5618 	int ret;
5619 
5620 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
5621 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
5622 		return -EINVAL;
5623 
5624 	if (!on) {
5625 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
5626 		if (ret)
5627 			return ret;
5628 
5629 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
5630 	}
5631 
5632 	ts_on.tv_sec = rq->perout.on.sec;
5633 	ts_on.tv_nsec = rq->perout.on.nsec;
5634 	on_nsec = timespec64_to_ns(&ts_on);
5635 
5636 	ts_period.tv_sec = rq->perout.period.sec;
5637 	ts_period.tv_nsec = rq->perout.period.nsec;
5638 	period_nsec = timespec64_to_ns(&ts_period);
5639 
5640 	if (period_nsec < 200) {
5641 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
5642 				    phydev_name(phydev));
5643 		return -EOPNOTSUPP;
5644 	}
5645 
5646 	if (on_nsec >= period_nsec) {
5647 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
5648 				    phydev_name(phydev));
5649 		return -EINVAL;
5650 	}
5651 
5652 	switch (on_nsec) {
5653 	case 200000000:
5654 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
5655 		break;
5656 	case 100000000:
5657 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
5658 		break;
5659 	case 50000000:
5660 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
5661 		break;
5662 	case 10000000:
5663 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
5664 		break;
5665 	case 5000000:
5666 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
5667 		break;
5668 	case 1000000:
5669 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
5670 		break;
5671 	case 500000:
5672 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
5673 		break;
5674 	case 100000:
5675 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
5676 		break;
5677 	case 50000:
5678 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
5679 		break;
5680 	case 10000:
5681 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
5682 		break;
5683 	case 5000:
5684 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
5685 		break;
5686 	case 1000:
5687 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
5688 		break;
5689 	case 500:
5690 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
5691 		break;
5692 	case 100:
5693 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
5694 		break;
5695 	default:
5696 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
5697 				    phydev_name(phydev));
5698 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
5699 		break;
5700 	}
5701 
5702 	mutex_lock(&ptp_priv->ptp_lock);
5703 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
5704 				     rq->perout.start.nsec);
5705 	mutex_unlock(&ptp_priv->ptp_lock);
5706 	if (ret)
5707 		return ret;
5708 
5709 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
5710 				     rq->perout.period.nsec);
5711 	if (ret)
5712 		return ret;
5713 
5714 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
5715 				       pulse_width);
5716 	if (ret)
5717 		return ret;
5718 
5719 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
5720 	if (ret)
5721 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
5722 
5723 	return ret;
5724 }
5725 
5726 #define LAN8841_PTP_GPIO_CAP_EN			496
5727 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
5728 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
5729 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
5730 
lan8841_ptp_extts_on(struct kszphy_ptp_priv * ptp_priv,int pin,u32 flags)5731 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
5732 				u32 flags)
5733 {
5734 	struct phy_device *phydev = ptp_priv->phydev;
5735 	u16 tmp = 0;
5736 	int ret;
5737 
5738 	/* Set GPIO to be input */
5739 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
5740 	if (ret)
5741 		return ret;
5742 
5743 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
5744 	if (ret)
5745 		return ret;
5746 
5747 	/* Enable capture on the edges of the pin */
5748 	if (flags & PTP_RISING_EDGE)
5749 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
5750 	if (flags & PTP_FALLING_EDGE)
5751 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
5752 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
5753 	if (ret)
5754 		return ret;
5755 
5756 	/* Enable interrupt */
5757 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
5758 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
5759 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
5760 }
5761 
lan8841_ptp_extts_off(struct kszphy_ptp_priv * ptp_priv,int pin)5762 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
5763 {
5764 	struct phy_device *phydev = ptp_priv->phydev;
5765 	int ret;
5766 
5767 	/* Set GPIO to be output */
5768 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
5769 	if (ret)
5770 		return ret;
5771 
5772 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
5773 	if (ret)
5774 		return ret;
5775 
5776 	/* Disable capture on both of the edges */
5777 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
5778 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
5779 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
5780 			     0);
5781 	if (ret)
5782 		return ret;
5783 
5784 	/* Disable interrupt */
5785 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
5786 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
5787 			      0);
5788 }
5789 
lan8841_ptp_extts(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)5790 static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
5791 			     struct ptp_clock_request *rq, int on)
5792 {
5793 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5794 							ptp_clock_info);
5795 	int pin;
5796 	int ret;
5797 
5798 	/* Reject requests with unsupported flags */
5799 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
5800 				PTP_EXTTS_EDGES |
5801 				PTP_STRICT_FLAGS))
5802 		return -EOPNOTSUPP;
5803 
5804 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
5805 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
5806 		return -EINVAL;
5807 
5808 	mutex_lock(&ptp_priv->ptp_lock);
5809 	if (on)
5810 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
5811 	else
5812 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
5813 	mutex_unlock(&ptp_priv->ptp_lock);
5814 
5815 	return ret;
5816 }
5817 
lan8841_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)5818 static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
5819 			      struct ptp_clock_request *rq, int on)
5820 {
5821 	switch (rq->type) {
5822 	case PTP_CLK_REQ_EXTTS:
5823 		return lan8841_ptp_extts(ptp, rq, on);
5824 	case PTP_CLK_REQ_PEROUT:
5825 		return lan8841_ptp_perout(ptp, rq, on);
5826 	default:
5827 		return -EOPNOTSUPP;
5828 	}
5829 
5830 	return 0;
5831 }
5832 
lan8841_ptp_do_aux_work(struct ptp_clock_info * ptp)5833 static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp)
5834 {
5835 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
5836 							ptp_clock_info);
5837 	struct timespec64 ts;
5838 	unsigned long flags;
5839 
5840 	lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts);
5841 
5842 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
5843 	ptp_priv->seconds = ts.tv_sec;
5844 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
5845 
5846 	return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY);
5847 }
5848 
5849 static struct ptp_clock_info lan8841_ptp_clock_info = {
5850 	.owner		= THIS_MODULE,
5851 	.name		= "lan8841 ptp",
5852 	.max_adj	= 31249999,
5853 	.gettime64	= lan8841_ptp_gettime64,
5854 	.settime64	= lan8841_ptp_settime64,
5855 	.adjtime	= lan8841_ptp_adjtime,
5856 	.adjfine	= lan8841_ptp_adjfine,
5857 	.verify         = lan8841_ptp_verify,
5858 	.enable         = lan8841_ptp_enable,
5859 	.do_aux_work	= lan8841_ptp_do_aux_work,
5860 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
5861 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
5862 	.n_pins         = LAN8841_PTP_GPIO_NUM,
5863 	.supported_perout_flags = PTP_PEROUT_DUTY_CYCLE,
5864 };
5865 
5866 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
5867 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
5868 
lan8841_probe(struct phy_device * phydev)5869 static int lan8841_probe(struct phy_device *phydev)
5870 {
5871 	struct kszphy_ptp_priv *ptp_priv;
5872 	struct kszphy_priv *priv;
5873 	int err;
5874 
5875 	err = kszphy_probe(phydev);
5876 	if (err)
5877 		return err;
5878 
5879 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
5880 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
5881 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
5882 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
5883 
5884 	/* Register the clock */
5885 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
5886 		return 0;
5887 
5888 	priv = phydev->priv;
5889 	ptp_priv = &priv->ptp_priv;
5890 
5891 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
5892 					    LAN8841_PTP_GPIO_NUM,
5893 					    sizeof(*ptp_priv->pin_config),
5894 					    GFP_KERNEL);
5895 	if (!ptp_priv->pin_config)
5896 		return -ENOMEM;
5897 
5898 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
5899 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
5900 
5901 		snprintf(p->name, sizeof(p->name), "pin%d", i);
5902 		p->index = i;
5903 		p->func = PTP_PF_NONE;
5904 	}
5905 
5906 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
5907 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
5908 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
5909 						 &phydev->mdio.dev);
5910 	if (IS_ERR(ptp_priv->ptp_clock)) {
5911 		phydev_err(phydev, "ptp_clock_register failed: %pe\n",
5912 			   ptp_priv->ptp_clock);
5913 		return -EINVAL;
5914 	}
5915 
5916 	if (!ptp_priv->ptp_clock)
5917 		return 0;
5918 
5919 	/* Initialize the SW */
5920 	skb_queue_head_init(&ptp_priv->tx_queue);
5921 	ptp_priv->phydev = phydev;
5922 	mutex_init(&ptp_priv->ptp_lock);
5923 	spin_lock_init(&ptp_priv->seconds_lock);
5924 
5925 	ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp;
5926 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
5927 	ptp_priv->mii_ts.hwtstamp_set = lan8841_hwtstamp_set;
5928 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
5929 
5930 	phydev->mii_ts = &ptp_priv->mii_ts;
5931 
5932 	/* Timestamp selected by default to keep legacy API */
5933 	phydev->default_timestamp = true;
5934 
5935 	return 0;
5936 }
5937 
lan8804_resume(struct phy_device * phydev)5938 static int lan8804_resume(struct phy_device *phydev)
5939 {
5940 	return kszphy_resume(phydev);
5941 }
5942 
lan8804_suspend(struct phy_device * phydev)5943 static int lan8804_suspend(struct phy_device *phydev)
5944 {
5945 	return kszphy_generic_suspend(phydev);
5946 }
5947 
lan8841_resume(struct phy_device * phydev)5948 static int lan8841_resume(struct phy_device *phydev)
5949 {
5950 	return kszphy_generic_resume(phydev);
5951 }
5952 
lan8841_suspend(struct phy_device * phydev)5953 static int lan8841_suspend(struct phy_device *phydev)
5954 {
5955 	struct kszphy_priv *priv = phydev->priv;
5956 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
5957 
5958 	if (ptp_priv->ptp_clock)
5959 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
5960 
5961 	return kszphy_generic_suspend(phydev);
5962 }
5963 
ksz9131_resume(struct phy_device * phydev)5964 static int ksz9131_resume(struct phy_device *phydev)
5965 {
5966 	if (phydev->suspended && phy_interface_is_rgmii(phydev))
5967 		ksz9131_config_rgmii_delay(phydev);
5968 
5969 	return kszphy_resume(phydev);
5970 }
5971 
5972 #define LAN8842_PTP_GPIO_NUM 16
5973 
lan8842_ptp_probe_once(struct phy_device * phydev)5974 static int lan8842_ptp_probe_once(struct phy_device *phydev)
5975 {
5976 	return __lan8814_ptp_probe_once(phydev, "lan8842_ptp_pin",
5977 					LAN8842_PTP_GPIO_NUM);
5978 }
5979 
5980 #define LAN8842_STRAP_REG			0 /* 0x0 */
5981 #define LAN8842_STRAP_REG_PHYADDR_MASK		GENMASK(4, 0)
5982 #define LAN8842_SKU_REG				11 /* 0x0b */
5983 #define LAN8842_SELF_TEST			14 /* 0x0e */
5984 #define LAN8842_SELF_TEST_RX_CNT_ENA		BIT(8)
5985 #define LAN8842_SELF_TEST_TX_CNT_ENA		BIT(4)
5986 
lan8842_probe(struct phy_device * phydev)5987 static int lan8842_probe(struct phy_device *phydev)
5988 {
5989 	struct lan8842_priv *priv;
5990 	int addr;
5991 	int ret;
5992 
5993 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
5994 	if (!priv)
5995 		return -ENOMEM;
5996 
5997 	phydev->priv = priv;
5998 
5999 	/* Similar to lan8814 this PHY has a pin which needs to be pulled down
6000 	 * to enable to pass any traffic through it. Therefore use the same
6001 	 * function as lan8814
6002 	 */
6003 	ret = lan8814_release_coma_mode(phydev);
6004 	if (ret)
6005 		return ret;
6006 
6007 	/* Enable to count the RX and TX packets */
6008 	ret = lanphy_write_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL,
6009 				    LAN8842_SELF_TEST,
6010 				    LAN8842_SELF_TEST_RX_CNT_ENA |
6011 				    LAN8842_SELF_TEST_TX_CNT_ENA);
6012 	if (ret < 0)
6013 		return ret;
6014 
6015 	/* Revision lan8832 doesn't have support for PTP, therefore don't add
6016 	 * any PTP clocks
6017 	 */
6018 	ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
6019 				   LAN8842_SKU_REG);
6020 	if (ret < 0)
6021 		return ret;
6022 
6023 	priv->rev = ret;
6024 	if (priv->rev == LAN8842_REV_8832)
6025 		return 0;
6026 
6027 	/* As the lan8814 and lan8842 has the same IP for the PTP block, the
6028 	 * only difference is the number of the GPIOs, then make sure that the
6029 	 * lan8842 initialized also the shared data pointer as this is used in
6030 	 * all the PTP functions for lan8814. The lan8842 doesn't have multiple
6031 	 * PHYs in the same package.
6032 	 */
6033 	addr = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
6034 				    LAN8842_STRAP_REG);
6035 	if (addr < 0)
6036 		return addr;
6037 	addr &= LAN8842_STRAP_REG_PHYADDR_MASK;
6038 
6039 	ret = devm_phy_package_join(&phydev->mdio.dev, phydev, addr,
6040 				    sizeof(struct lan8814_shared_priv));
6041 	if (ret)
6042 		return ret;
6043 
6044 	if (phy_package_init_once(phydev)) {
6045 		ret = lan8842_ptp_probe_once(phydev);
6046 		if (ret)
6047 			return ret;
6048 	}
6049 
6050 	lan8814_ptp_init(phydev);
6051 
6052 	return 0;
6053 }
6054 
6055 #define LAN8814_POWER_MGMT_MODE_3_ANEG_MDI		0x13
6056 #define LAN8814_POWER_MGMT_MODE_4_ANEG_MDIX		0x14
6057 #define LAN8814_POWER_MGMT_MODE_5_10BT_MDI		0x15
6058 #define LAN8814_POWER_MGMT_MODE_6_10BT_MDIX		0x16
6059 #define LAN8814_POWER_MGMT_MODE_7_100BT_TRAIN		0x17
6060 #define LAN8814_POWER_MGMT_MODE_8_100BT_MDI		0x18
6061 #define LAN8814_POWER_MGMT_MODE_9_100BT_EEE_MDI_TX	0x19
6062 #define LAN8814_POWER_MGMT_MODE_10_100BT_EEE_MDI_RX	0x1a
6063 #define LAN8814_POWER_MGMT_MODE_11_100BT_MDIX		0x1b
6064 #define LAN8814_POWER_MGMT_MODE_12_100BT_EEE_MDIX_TX	0x1c
6065 #define LAN8814_POWER_MGMT_MODE_13_100BT_EEE_MDIX_RX	0x1d
6066 #define LAN8814_POWER_MGMT_MODE_14_100BTX_EEE_TX_RX	0x1e
6067 
6068 #define LAN8814_POWER_MGMT_DLLPD_D			BIT(0)
6069 #define LAN8814_POWER_MGMT_ADCPD_D			BIT(1)
6070 #define LAN8814_POWER_MGMT_PGAPD_D			BIT(2)
6071 #define LAN8814_POWER_MGMT_TXPD_D			BIT(3)
6072 #define LAN8814_POWER_MGMT_DLLPD_C			BIT(4)
6073 #define LAN8814_POWER_MGMT_ADCPD_C			BIT(5)
6074 #define LAN8814_POWER_MGMT_PGAPD_C			BIT(6)
6075 #define LAN8814_POWER_MGMT_TXPD_C			BIT(7)
6076 #define LAN8814_POWER_MGMT_DLLPD_B			BIT(8)
6077 #define LAN8814_POWER_MGMT_ADCPD_B			BIT(9)
6078 #define LAN8814_POWER_MGMT_PGAPD_B			BIT(10)
6079 #define LAN8814_POWER_MGMT_TXPD_B			BIT(11)
6080 #define LAN8814_POWER_MGMT_DLLPD_A			BIT(12)
6081 #define LAN8814_POWER_MGMT_ADCPD_A			BIT(13)
6082 #define LAN8814_POWER_MGMT_PGAPD_A			BIT(14)
6083 #define LAN8814_POWER_MGMT_TXPD_A			BIT(15)
6084 
6085 #define LAN8814_POWER_MGMT_C_D		(LAN8814_POWER_MGMT_DLLPD_D | \
6086 					 LAN8814_POWER_MGMT_ADCPD_D | \
6087 					 LAN8814_POWER_MGMT_PGAPD_D | \
6088 					 LAN8814_POWER_MGMT_DLLPD_C | \
6089 					 LAN8814_POWER_MGMT_ADCPD_C | \
6090 					 LAN8814_POWER_MGMT_PGAPD_C)
6091 
6092 #define LAN8814_POWER_MGMT_B_C_D	(LAN8814_POWER_MGMT_C_D | \
6093 					 LAN8814_POWER_MGMT_DLLPD_B | \
6094 					 LAN8814_POWER_MGMT_ADCPD_B | \
6095 					 LAN8814_POWER_MGMT_PGAPD_B)
6096 
6097 #define LAN8814_POWER_MGMT_VAL1		(LAN8814_POWER_MGMT_C_D | \
6098 					 LAN8814_POWER_MGMT_ADCPD_B | \
6099 					 LAN8814_POWER_MGMT_PGAPD_B | \
6100 					 LAN8814_POWER_MGMT_ADCPD_A | \
6101 					 LAN8814_POWER_MGMT_PGAPD_A)
6102 
6103 #define LAN8814_POWER_MGMT_VAL2		LAN8814_POWER_MGMT_C_D
6104 
6105 #define LAN8814_POWER_MGMT_VAL3		(LAN8814_POWER_MGMT_C_D | \
6106 					 LAN8814_POWER_MGMT_DLLPD_B | \
6107 					 LAN8814_POWER_MGMT_ADCPD_B | \
6108 					 LAN8814_POWER_MGMT_PGAPD_A)
6109 
6110 #define LAN8814_POWER_MGMT_VAL4		(LAN8814_POWER_MGMT_B_C_D | \
6111 					 LAN8814_POWER_MGMT_ADCPD_A | \
6112 					 LAN8814_POWER_MGMT_PGAPD_A)
6113 
6114 #define LAN8814_POWER_MGMT_VAL5		LAN8814_POWER_MGMT_B_C_D
6115 
6116 #define LAN8814_EEE_WAKE_TX_TIMER			0x0e
6117 #define LAN8814_EEE_WAKE_TX_TIMER_MAX_VAL		0x1f
6118 
6119 static const struct lanphy_reg_data short_center_tap_errata[] = {
6120 	{ LAN8814_PAGE_POWER_REGS,
6121 	  LAN8814_POWER_MGMT_MODE_3_ANEG_MDI,
6122 	  LAN8814_POWER_MGMT_VAL1 },
6123 	{ LAN8814_PAGE_POWER_REGS,
6124 	  LAN8814_POWER_MGMT_MODE_4_ANEG_MDIX,
6125 	  LAN8814_POWER_MGMT_VAL1 },
6126 	{ LAN8814_PAGE_POWER_REGS,
6127 	  LAN8814_POWER_MGMT_MODE_5_10BT_MDI,
6128 	  LAN8814_POWER_MGMT_VAL1 },
6129 	{ LAN8814_PAGE_POWER_REGS,
6130 	  LAN8814_POWER_MGMT_MODE_6_10BT_MDIX,
6131 	  LAN8814_POWER_MGMT_VAL1 },
6132 	{ LAN8814_PAGE_POWER_REGS,
6133 	  LAN8814_POWER_MGMT_MODE_7_100BT_TRAIN,
6134 	  LAN8814_POWER_MGMT_VAL2 },
6135 	{ LAN8814_PAGE_POWER_REGS,
6136 	  LAN8814_POWER_MGMT_MODE_8_100BT_MDI,
6137 	  LAN8814_POWER_MGMT_VAL3 },
6138 	{ LAN8814_PAGE_POWER_REGS,
6139 	  LAN8814_POWER_MGMT_MODE_9_100BT_EEE_MDI_TX,
6140 	  LAN8814_POWER_MGMT_VAL3 },
6141 	{ LAN8814_PAGE_POWER_REGS,
6142 	  LAN8814_POWER_MGMT_MODE_10_100BT_EEE_MDI_RX,
6143 	  LAN8814_POWER_MGMT_VAL4 },
6144 	{ LAN8814_PAGE_POWER_REGS,
6145 	  LAN8814_POWER_MGMT_MODE_11_100BT_MDIX,
6146 	  LAN8814_POWER_MGMT_VAL5 },
6147 	{ LAN8814_PAGE_POWER_REGS,
6148 	  LAN8814_POWER_MGMT_MODE_12_100BT_EEE_MDIX_TX,
6149 	  LAN8814_POWER_MGMT_VAL5 },
6150 	{ LAN8814_PAGE_POWER_REGS,
6151 	  LAN8814_POWER_MGMT_MODE_13_100BT_EEE_MDIX_RX,
6152 	  LAN8814_POWER_MGMT_VAL4 },
6153 	{ LAN8814_PAGE_POWER_REGS,
6154 	  LAN8814_POWER_MGMT_MODE_14_100BTX_EEE_TX_RX,
6155 	  LAN8814_POWER_MGMT_VAL4 },
6156 };
6157 
6158 static const struct lanphy_reg_data waketx_timer_errata[] = {
6159 	{ LAN8814_PAGE_EEE,
6160 	  LAN8814_EEE_WAKE_TX_TIMER,
6161 	  LAN8814_EEE_WAKE_TX_TIMER_MAX_VAL },
6162 };
6163 
lanphy_write_reg_data(struct phy_device * phydev,const struct lanphy_reg_data * data,size_t num)6164 static int lanphy_write_reg_data(struct phy_device *phydev,
6165 				 const struct lanphy_reg_data *data,
6166 				 size_t num)
6167 {
6168 	int ret = 0;
6169 
6170 	while (num--) {
6171 		ret = lanphy_write_page_reg(phydev, data->page, data->addr,
6172 					    data->val);
6173 		if (ret)
6174 			break;
6175 	}
6176 
6177 	return ret;
6178 }
6179 
lan8842_erratas(struct phy_device * phydev)6180 static int lan8842_erratas(struct phy_device *phydev)
6181 {
6182 	int ret;
6183 
6184 	ret = lanphy_write_reg_data(phydev, short_center_tap_errata,
6185 				    ARRAY_SIZE(short_center_tap_errata));
6186 	if (ret)
6187 		return ret;
6188 
6189 	return lanphy_write_reg_data(phydev, waketx_timer_errata,
6190 				     ARRAY_SIZE(waketx_timer_errata));
6191 }
6192 
lan8842_config_init(struct phy_device * phydev)6193 static int lan8842_config_init(struct phy_device *phydev)
6194 {
6195 	int ret;
6196 
6197 	/* Reset the PHY */
6198 	ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
6199 				     LAN8814_QSGMII_SOFT_RESET,
6200 				     LAN8814_QSGMII_SOFT_RESET_BIT,
6201 				     LAN8814_QSGMII_SOFT_RESET_BIT);
6202 	if (ret < 0)
6203 		return ret;
6204 
6205 	/* Apply the erratas for this device */
6206 	ret = lan8842_erratas(phydev);
6207 	if (ret < 0)
6208 		return ret;
6209 
6210 	/* Even if the GPIOs are set to control the LEDs the behaviour of the
6211 	 * LEDs is wrong, they are not blinking when there is traffic.
6212 	 * To fix this it is required to set extended LED mode
6213 	 */
6214 	ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
6215 				     LAN8814_LED_CTRL_1,
6216 				     LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_, 0);
6217 	if (ret < 0)
6218 		return ret;
6219 
6220 	ret = lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
6221 				     LAN8814_LED_CTRL_2,
6222 				     LAN8814_LED_CTRL_2_LED1_COM_DIS,
6223 				     LAN8814_LED_CTRL_2_LED1_COM_DIS);
6224 	if (ret < 0)
6225 		return ret;
6226 
6227 	/* To allow the PHY to control the LEDs the GPIOs of the PHY should have
6228 	 * a function mode and not the GPIO. Apparently by default the value is
6229 	 * GPIO and not function even though the datasheet it says that it is
6230 	 * function. Therefore set this value.
6231 	 */
6232 	return lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
6233 				     LAN8814_GPIO_EN2, 0);
6234 }
6235 
6236 #define LAN8842_INTR_CTRL_REG			52 /* 0x34 */
6237 
lan8842_config_intr(struct phy_device * phydev)6238 static int lan8842_config_intr(struct phy_device *phydev)
6239 {
6240 	int err;
6241 
6242 	lanphy_write_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
6243 			      LAN8842_INTR_CTRL_REG,
6244 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
6245 
6246 	/* enable / disable interrupts */
6247 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
6248 		err = lan8814_ack_interrupt(phydev);
6249 		if (err)
6250 			return err;
6251 
6252 		err = phy_write(phydev, LAN8814_INTC,
6253 				LAN8814_INT_LINK | LAN8814_INT_FLF);
6254 	} else {
6255 		err = phy_write(phydev, LAN8814_INTC, 0);
6256 		if (err)
6257 			return err;
6258 
6259 		err = lan8814_ack_interrupt(phydev);
6260 	}
6261 
6262 	return err;
6263 }
6264 
lan8842_inband_caps(struct phy_device * phydev,phy_interface_t interface)6265 static unsigned int lan8842_inband_caps(struct phy_device *phydev,
6266 					phy_interface_t interface)
6267 {
6268 	/* Inband configuration can be enabled or disabled using the registers
6269 	 * PCS1G_ANEG_CONFIG.
6270 	 */
6271 	return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE;
6272 }
6273 
lan8842_config_inband(struct phy_device * phydev,unsigned int modes)6274 static int lan8842_config_inband(struct phy_device *phydev, unsigned int modes)
6275 {
6276 	bool enable;
6277 
6278 	if (modes == LINK_INBAND_DISABLE)
6279 		enable = false;
6280 	else
6281 		enable = true;
6282 
6283 	/* Disable or enable in-band autoneg with PCS Host side
6284 	 * It has the same address as lan8814
6285 	 */
6286 	return lanphy_modify_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
6287 				      LAN8814_QSGMII_PCS1G_ANEG_CONFIG,
6288 				      LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA,
6289 				      enable ? LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA : 0);
6290 }
6291 
lan8842_handle_ptp_interrupt(struct phy_device * phydev,u16 status)6292 static void lan8842_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
6293 {
6294 	struct kszphy_ptp_priv *ptp_priv;
6295 	struct lan8842_priv *priv;
6296 
6297 	priv = phydev->priv;
6298 	ptp_priv = &priv->ptp_priv;
6299 
6300 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
6301 		lan8814_get_tx_ts(ptp_priv);
6302 
6303 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
6304 		lan8814_get_rx_ts(ptp_priv);
6305 
6306 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
6307 		lan8814_flush_fifo(phydev, true);
6308 		skb_queue_purge(&ptp_priv->tx_queue);
6309 	}
6310 
6311 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
6312 		lan8814_flush_fifo(phydev, false);
6313 		skb_queue_purge(&ptp_priv->rx_queue);
6314 	}
6315 }
6316 
lan8842_handle_interrupt(struct phy_device * phydev)6317 static irqreturn_t lan8842_handle_interrupt(struct phy_device *phydev)
6318 {
6319 	struct lan8842_priv *priv = phydev->priv;
6320 	int ret = IRQ_NONE;
6321 	int irq_status;
6322 
6323 	irq_status = phy_read(phydev, LAN8814_INTS);
6324 	if (irq_status < 0) {
6325 		phy_error(phydev);
6326 		return IRQ_NONE;
6327 	}
6328 
6329 	if (irq_status & (LAN8814_INT_LINK | LAN8814_INT_FLF)) {
6330 		phy_trigger_machine(phydev);
6331 		ret = IRQ_HANDLED;
6332 	}
6333 
6334 	/* Phy revision lan8832 doesn't have support for PTP therefore there is
6335 	 * not need to check the PTP and GPIO interrupts
6336 	 */
6337 	if (priv->rev == LAN8842_REV_8832)
6338 		goto out;
6339 
6340 	while (true) {
6341 		irq_status = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
6342 						  PTP_TSU_INT_STS);
6343 		if (!irq_status)
6344 			break;
6345 
6346 		lan8842_handle_ptp_interrupt(phydev, irq_status);
6347 		ret = IRQ_HANDLED;
6348 	}
6349 
6350 	if (!lan8814_handle_gpio_interrupt(phydev, irq_status))
6351 		ret = IRQ_HANDLED;
6352 
6353 out:
6354 	return ret;
6355 }
6356 
lan8842_get_stat(struct phy_device * phydev,int count,int * regs)6357 static u64 lan8842_get_stat(struct phy_device *phydev, int count, int *regs)
6358 {
6359 	u64 ret = 0;
6360 	int val;
6361 
6362 	for (int j = 0; j < count; ++j) {
6363 		val = lanphy_read_page_reg(phydev, LAN8814_PAGE_PCS_DIGITAL,
6364 					   regs[j]);
6365 		if (val < 0)
6366 			return U64_MAX;
6367 
6368 		ret <<= 16;
6369 		ret += val;
6370 	}
6371 	return ret;
6372 }
6373 
lan8842_update_stats(struct phy_device * phydev)6374 static int lan8842_update_stats(struct phy_device *phydev)
6375 {
6376 	struct lan8842_priv *priv = phydev->priv;
6377 	int rx_packets_regs[] = {88, 61, 60};
6378 	int rx_errors_regs[] = {63, 62};
6379 	int tx_packets_regs[] = {89, 85, 84};
6380 	int tx_errors_regs[] = {87, 86};
6381 
6382 	priv->phy_stats.rx_packets = lan8842_get_stat(phydev,
6383 						      ARRAY_SIZE(rx_packets_regs),
6384 						      rx_packets_regs);
6385 	priv->phy_stats.rx_errors = lan8842_get_stat(phydev,
6386 						     ARRAY_SIZE(rx_errors_regs),
6387 						     rx_errors_regs);
6388 	priv->phy_stats.tx_packets = lan8842_get_stat(phydev,
6389 						      ARRAY_SIZE(tx_packets_regs),
6390 						      tx_packets_regs);
6391 	priv->phy_stats.tx_errors = lan8842_get_stat(phydev,
6392 						     ARRAY_SIZE(tx_errors_regs),
6393 						     tx_errors_regs);
6394 
6395 	return 0;
6396 }
6397 
6398 #define LAN8842_FLF				15 /* 0x0e */
6399 #define LAN8842_FLF_ENA				BIT(1)
6400 #define LAN8842_FLF_ENA_LINK_DOWN		BIT(0)
6401 
lan8842_get_fast_down(struct phy_device * phydev,u8 * msecs)6402 static int lan8842_get_fast_down(struct phy_device *phydev, u8 *msecs)
6403 {
6404 	int ret;
6405 
6406 	ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_PCS, LAN8842_FLF);
6407 	if (ret < 0)
6408 		return ret;
6409 
6410 	if (ret & LAN8842_FLF_ENA)
6411 		*msecs = ETHTOOL_PHY_FAST_LINK_DOWN_ON;
6412 	else
6413 		*msecs = ETHTOOL_PHY_FAST_LINK_DOWN_OFF;
6414 
6415 	return 0;
6416 }
6417 
lan8842_set_fast_down(struct phy_device * phydev,const u8 * msecs)6418 static int lan8842_set_fast_down(struct phy_device *phydev, const u8 *msecs)
6419 {
6420 	u16 flf;
6421 
6422 	switch (*msecs) {
6423 	case ETHTOOL_PHY_FAST_LINK_DOWN_OFF:
6424 		flf = 0;
6425 		break;
6426 	case ETHTOOL_PHY_FAST_LINK_DOWN_ON:
6427 		flf = LAN8842_FLF_ENA | LAN8842_FLF_ENA_LINK_DOWN;
6428 		break;
6429 	default:
6430 		return -EINVAL;
6431 	}
6432 
6433 	return lanphy_modify_page_reg(phydev, LAN8814_PAGE_PCS,
6434 				      LAN8842_FLF,
6435 				      LAN8842_FLF_ENA |
6436 				      LAN8842_FLF_ENA_LINK_DOWN, flf);
6437 }
6438 
lan8842_get_tunable(struct phy_device * phydev,struct ethtool_tunable * tuna,void * data)6439 static int lan8842_get_tunable(struct phy_device *phydev,
6440 			       struct ethtool_tunable *tuna, void *data)
6441 {
6442 	switch (tuna->id) {
6443 	case ETHTOOL_PHY_FAST_LINK_DOWN:
6444 		return lan8842_get_fast_down(phydev, data);
6445 	default:
6446 		return -EOPNOTSUPP;
6447 	}
6448 }
6449 
lan8842_set_tunable(struct phy_device * phydev,struct ethtool_tunable * tuna,const void * data)6450 static int lan8842_set_tunable(struct phy_device *phydev,
6451 			       struct ethtool_tunable *tuna, const void *data)
6452 {
6453 	switch (tuna->id) {
6454 	case ETHTOOL_PHY_FAST_LINK_DOWN:
6455 		return lan8842_set_fast_down(phydev, data);
6456 	default:
6457 		return -EOPNOTSUPP;
6458 	}
6459 }
6460 
lan8842_get_phy_stats(struct phy_device * phydev,struct ethtool_eth_phy_stats * eth_stats,struct ethtool_phy_stats * stats)6461 static void lan8842_get_phy_stats(struct phy_device *phydev,
6462 				  struct ethtool_eth_phy_stats *eth_stats,
6463 				  struct ethtool_phy_stats *stats)
6464 {
6465 	struct lan8842_priv *priv = phydev->priv;
6466 
6467 	stats->rx_packets = priv->phy_stats.rx_packets;
6468 	stats->rx_errors = priv->phy_stats.rx_errors;
6469 	stats->tx_packets = priv->phy_stats.tx_packets;
6470 	stats->tx_errors = priv->phy_stats.tx_errors;
6471 }
6472 
6473 static struct phy_driver ksphy_driver[] = {
6474 {
6475 	PHY_ID_MATCH_MODEL(PHY_ID_KS8737),
6476 	.name		= "Micrel KS8737",
6477 	/* PHY_BASIC_FEATURES */
6478 	.driver_data	= &ks8737_type,
6479 	.probe		= kszphy_probe,
6480 	.config_init	= kszphy_config_init,
6481 	.config_intr	= kszphy_config_intr,
6482 	.handle_interrupt = kszphy_handle_interrupt,
6483 	.suspend	= kszphy_suspend,
6484 	.resume		= kszphy_resume,
6485 }, {
6486 	.phy_id		= PHY_ID_KSZ8021,
6487 	.phy_id_mask	= 0x00ffffff,
6488 	.name		= "Micrel KSZ8021 or KSZ8031",
6489 	/* PHY_BASIC_FEATURES */
6490 	.driver_data	= &ksz8021_type,
6491 	.probe		= kszphy_probe,
6492 	.config_init	= kszphy_config_init,
6493 	.config_intr	= kszphy_config_intr,
6494 	.handle_interrupt = kszphy_handle_interrupt,
6495 	.get_sset_count = kszphy_get_sset_count,
6496 	.get_strings	= kszphy_get_strings,
6497 	.get_stats	= kszphy_get_stats,
6498 	.suspend	= kszphy_suspend,
6499 	.resume		= kszphy_resume,
6500 }, {
6501 	.phy_id		= PHY_ID_KSZ8031,
6502 	.phy_id_mask	= 0x00ffffff,
6503 	.name		= "Micrel KSZ8031",
6504 	/* PHY_BASIC_FEATURES */
6505 	.driver_data	= &ksz8021_type,
6506 	.probe		= kszphy_probe,
6507 	.config_init	= kszphy_config_init,
6508 	.config_intr	= kszphy_config_intr,
6509 	.handle_interrupt = kszphy_handle_interrupt,
6510 	.get_sset_count = kszphy_get_sset_count,
6511 	.get_strings	= kszphy_get_strings,
6512 	.get_stats	= kszphy_get_stats,
6513 	.suspend	= kszphy_suspend,
6514 	.resume		= kszphy_resume,
6515 }, {
6516 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041),
6517 	.name		= "Micrel KSZ8041",
6518 	/* PHY_BASIC_FEATURES */
6519 	.driver_data	= &ksz8041_type,
6520 	.probe		= kszphy_probe,
6521 	.config_init	= ksz8041_config_init,
6522 	.config_aneg	= ksz8041_config_aneg,
6523 	.config_intr	= kszphy_config_intr,
6524 	.handle_interrupt = kszphy_handle_interrupt,
6525 	.get_sset_count = kszphy_get_sset_count,
6526 	.get_strings	= kszphy_get_strings,
6527 	.get_stats	= kszphy_get_stats,
6528 	.suspend	= ksz8041_suspend,
6529 	.resume		= ksz8041_resume,
6530 }, {
6531 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041RNLI),
6532 	.name		= "Micrel KSZ8041RNLI",
6533 	/* PHY_BASIC_FEATURES */
6534 	.driver_data	= &ksz8041_type,
6535 	.probe		= kszphy_probe,
6536 	.config_init	= kszphy_config_init,
6537 	.config_intr	= kszphy_config_intr,
6538 	.handle_interrupt = kszphy_handle_interrupt,
6539 	.get_sset_count = kszphy_get_sset_count,
6540 	.get_strings	= kszphy_get_strings,
6541 	.get_stats	= kszphy_get_stats,
6542 	.suspend	= kszphy_suspend,
6543 	.resume		= kszphy_resume,
6544 }, {
6545 	.name		= "Micrel KSZ8051",
6546 	/* PHY_BASIC_FEATURES */
6547 	.driver_data	= &ksz8051_type,
6548 	.probe		= kszphy_probe,
6549 	.config_init	= kszphy_config_init,
6550 	.config_intr	= kszphy_config_intr,
6551 	.handle_interrupt = kszphy_handle_interrupt,
6552 	.get_sset_count = kszphy_get_sset_count,
6553 	.get_strings	= kszphy_get_strings,
6554 	.get_stats	= kszphy_get_stats,
6555 	.match_phy_device = ksz8051_match_phy_device,
6556 	.suspend	= kszphy_suspend,
6557 	.resume		= kszphy_resume,
6558 }, {
6559 	.phy_id		= PHY_ID_KSZ8001,
6560 	.name		= "Micrel KSZ8001 or KS8721",
6561 	.phy_id_mask	= 0x00fffffc,
6562 	/* PHY_BASIC_FEATURES */
6563 	.driver_data	= &ksz8041_type,
6564 	.probe		= kszphy_probe,
6565 	.config_init	= kszphy_config_init,
6566 	.config_intr	= kszphy_config_intr,
6567 	.handle_interrupt = kszphy_handle_interrupt,
6568 	.get_sset_count = kszphy_get_sset_count,
6569 	.get_strings	= kszphy_get_strings,
6570 	.get_stats	= kszphy_get_stats,
6571 	.suspend	= kszphy_suspend,
6572 	.resume		= kszphy_resume,
6573 }, {
6574 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ8081),
6575 	.name		= "Micrel KSZ8081 or KSZ8091",
6576 	.flags		= PHY_POLL_CABLE_TEST,
6577 	/* PHY_BASIC_FEATURES */
6578 	.driver_data	= &ksz8081_type,
6579 	.probe		= kszphy_probe,
6580 	.config_init	= ksz8081_config_init,
6581 	.soft_reset	= genphy_soft_reset,
6582 	.config_aneg	= ksz8081_config_aneg,
6583 	.read_status	= ksz8081_read_status,
6584 	.config_intr	= kszphy_config_intr,
6585 	.handle_interrupt = kszphy_handle_interrupt,
6586 	.get_sset_count = kszphy_get_sset_count,
6587 	.get_strings	= kszphy_get_strings,
6588 	.get_stats	= kszphy_get_stats,
6589 	.suspend	= kszphy_suspend,
6590 	.resume		= kszphy_resume,
6591 	.cable_test_start	= ksz886x_cable_test_start,
6592 	.cable_test_get_status	= ksz886x_cable_test_get_status,
6593 }, {
6594 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ8061),
6595 	.name		= "Micrel KSZ8061",
6596 	/* PHY_BASIC_FEATURES */
6597 	.probe		= kszphy_probe,
6598 	.config_init	= ksz8061_config_init,
6599 	.soft_reset	= genphy_soft_reset,
6600 	.config_intr	= kszphy_config_intr,
6601 	.handle_interrupt = kszphy_handle_interrupt,
6602 	.suspend	= ksz8061_suspend,
6603 	.resume		= ksz8061_resume,
6604 }, {
6605 	.phy_id		= PHY_ID_KSZ9021,
6606 	.phy_id_mask	= 0x000ffffe,
6607 	.name		= "Micrel KSZ9021 Gigabit PHY",
6608 	/* PHY_GBIT_FEATURES */
6609 	.driver_data	= &ksz9021_type,
6610 	.probe		= kszphy_probe,
6611 	.get_features	= ksz9031_get_features,
6612 	.config_init	= ksz9021_config_init,
6613 	.config_intr	= kszphy_config_intr,
6614 	.handle_interrupt = kszphy_handle_interrupt,
6615 	.get_sset_count = kszphy_get_sset_count,
6616 	.get_strings	= kszphy_get_strings,
6617 	.get_stats	= kszphy_get_stats,
6618 	.suspend	= kszphy_suspend,
6619 	.resume		= kszphy_resume,
6620 	.read_mmd	= genphy_read_mmd_unsupported,
6621 	.write_mmd	= genphy_write_mmd_unsupported,
6622 }, {
6623 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ9031),
6624 	.name		= "Micrel KSZ9031 Gigabit PHY",
6625 	.flags		= PHY_POLL_CABLE_TEST,
6626 	.driver_data	= &ksz9021_type,
6627 	.probe		= kszphy_probe,
6628 	.get_features	= ksz9031_get_features,
6629 	.config_init	= ksz9031_config_init,
6630 	.soft_reset	= genphy_soft_reset,
6631 	.read_status	= ksz9031_read_status,
6632 	.config_intr	= kszphy_config_intr,
6633 	.handle_interrupt = kszphy_handle_interrupt,
6634 	.get_sset_count = kszphy_get_sset_count,
6635 	.get_strings	= kszphy_get_strings,
6636 	.get_stats	= kszphy_get_stats,
6637 	.suspend	= kszphy_suspend,
6638 	.resume		= kszphy_resume,
6639 	.cable_test_start	= ksz9x31_cable_test_start,
6640 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
6641 	.set_loopback	= ksz9031_set_loopback,
6642 }, {
6643 	PHY_ID_MATCH_MODEL(PHY_ID_LAN8814),
6644 	.name		= "Microchip INDY Gigabit Quad PHY",
6645 	.flags          = PHY_POLL_CABLE_TEST,
6646 	.config_init	= lan8814_config_init,
6647 	.driver_data	= &lan8814_type,
6648 	.probe		= lan8814_probe,
6649 	.soft_reset	= genphy_soft_reset,
6650 	.read_status	= ksz9031_read_status,
6651 	.get_sset_count	= kszphy_get_sset_count,
6652 	.get_strings	= kszphy_get_strings,
6653 	.get_stats	= kszphy_get_stats,
6654 	.suspend	= genphy_suspend,
6655 	.resume		= kszphy_resume,
6656 	.config_intr	= lan8814_config_intr,
6657 	.inband_caps	= lan8842_inband_caps,
6658 	.config_inband	= lan8842_config_inband,
6659 	.handle_interrupt = lan8814_handle_interrupt,
6660 	.cable_test_start	= lan8814_cable_test_start,
6661 	.cable_test_get_status	= ksz886x_cable_test_get_status,
6662 }, {
6663 	PHY_ID_MATCH_MODEL(PHY_ID_LAN8804),
6664 	.name		= "Microchip LAN966X Gigabit PHY",
6665 	.config_init	= lan8804_config_init,
6666 	.driver_data	= &ksz9021_type,
6667 	.probe		= kszphy_probe,
6668 	.soft_reset	= genphy_soft_reset,
6669 	.read_status	= ksz9031_read_status,
6670 	.get_sset_count	= kszphy_get_sset_count,
6671 	.get_strings	= kszphy_get_strings,
6672 	.get_stats	= kszphy_get_stats,
6673 	.suspend	= lan8804_suspend,
6674 	.resume		= lan8804_resume,
6675 	.config_intr	= lan8804_config_intr,
6676 	.handle_interrupt = lan8804_handle_interrupt,
6677 }, {
6678 	PHY_ID_MATCH_MODEL(PHY_ID_LAN8841),
6679 	.name		= "Microchip LAN8841 Gigabit PHY",
6680 	.flags		= PHY_POLL_CABLE_TEST,
6681 	.driver_data	= &lan8841_type,
6682 	.config_init	= lan8841_config_init,
6683 	.probe		= lan8841_probe,
6684 	.soft_reset	= genphy_soft_reset,
6685 	.config_intr	= lan8841_config_intr,
6686 	.handle_interrupt = lan8841_handle_interrupt,
6687 	.get_sset_count = kszphy_get_sset_count,
6688 	.get_strings	= kszphy_get_strings,
6689 	.get_stats	= kszphy_get_stats,
6690 	.suspend	= lan8841_suspend,
6691 	.resume		= lan8841_resume,
6692 	.cable_test_start	= lan8814_cable_test_start,
6693 	.cable_test_get_status	= ksz886x_cable_test_get_status,
6694 }, {
6695 	PHY_ID_MATCH_MODEL(PHY_ID_LAN8842),
6696 	.name		= "Microchip LAN8842 Gigabit PHY",
6697 	.flags		= PHY_POLL_CABLE_TEST,
6698 	.driver_data	= &lan8814_type,
6699 	.probe		= lan8842_probe,
6700 	.config_init	= lan8842_config_init,
6701 	.config_intr	= lan8842_config_intr,
6702 	.inband_caps	= lan8842_inband_caps,
6703 	.config_inband	= lan8842_config_inband,
6704 	.handle_interrupt = lan8842_handle_interrupt,
6705 	.get_phy_stats	= lan8842_get_phy_stats,
6706 	.update_stats	= lan8842_update_stats,
6707 	.get_tunable	= lan8842_get_tunable,
6708 	.set_tunable	= lan8842_set_tunable,
6709 	.cable_test_start	= lan8814_cable_test_start,
6710 	.cable_test_get_status	= ksz886x_cable_test_get_status,
6711 }, {
6712 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ9131),
6713 	.name		= "Microchip KSZ9131 Gigabit PHY",
6714 	/* PHY_GBIT_FEATURES */
6715 	.flags		= PHY_POLL_CABLE_TEST,
6716 	.driver_data	= &ksz9131_type,
6717 	.probe		= kszphy_probe,
6718 	.soft_reset	= genphy_soft_reset,
6719 	.config_init	= ksz9131_config_init,
6720 	.config_intr	= kszphy_config_intr,
6721 	.config_aneg	= ksz9131_config_aneg,
6722 	.read_status	= ksz9131_read_status,
6723 	.handle_interrupt = kszphy_handle_interrupt,
6724 	.get_sset_count = kszphy_get_sset_count,
6725 	.get_strings	= kszphy_get_strings,
6726 	.get_stats	= kszphy_get_stats,
6727 	.suspend	= kszphy_suspend,
6728 	.resume		= ksz9131_resume,
6729 	.cable_test_start	= ksz9x31_cable_test_start,
6730 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
6731 	.get_features	= ksz9477_get_features,
6732 }, {
6733 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ8873MLL),
6734 	.name		= "Micrel KSZ8873MLL Switch",
6735 	/* PHY_BASIC_FEATURES */
6736 	.config_init	= kszphy_config_init,
6737 	.config_aneg	= ksz8873mll_config_aneg,
6738 	.read_status	= ksz8873mll_read_status,
6739 	.suspend	= genphy_suspend,
6740 	.resume		= genphy_resume,
6741 }, {
6742 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ886X),
6743 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
6744 	.driver_data	= &ksz886x_type,
6745 	/* PHY_BASIC_FEATURES */
6746 	.flags		= PHY_POLL_CABLE_TEST,
6747 	.config_init	= kszphy_config_init,
6748 	.config_aneg	= ksz886x_config_aneg,
6749 	.read_status	= ksz886x_read_status,
6750 	.suspend	= genphy_suspend,
6751 	.resume		= genphy_resume,
6752 	.cable_test_start	= ksz886x_cable_test_start,
6753 	.cable_test_get_status	= ksz886x_cable_test_get_status,
6754 }, {
6755 	.name		= "Micrel KSZ87XX Switch",
6756 	/* PHY_BASIC_FEATURES */
6757 	.config_init	= kszphy_config_init,
6758 	.match_phy_device = ksz8795_match_phy_device,
6759 	.suspend	= genphy_suspend,
6760 	.resume		= genphy_resume,
6761 }, {
6762 	PHY_ID_MATCH_MODEL(PHY_ID_KSZ9477),
6763 	.name		= "Microchip KSZ9477",
6764 	.probe		= kszphy_probe,
6765 	/* PHY_GBIT_FEATURES */
6766 	.config_init	= ksz9477_config_init,
6767 	.config_intr	= kszphy_config_intr,
6768 	.config_aneg	= ksz9477_config_aneg,
6769 	.read_status	= ksz9477_read_status,
6770 	.handle_interrupt = kszphy_handle_interrupt,
6771 	.suspend	= genphy_suspend,
6772 	.resume		= ksz9477_resume,
6773 	.get_phy_stats	= kszphy_get_phy_stats,
6774 	.update_stats	= kszphy_update_stats,
6775 	.cable_test_start	= ksz9x31_cable_test_start,
6776 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
6777 	.get_sqi	= kszphy_get_sqi,
6778 	.get_sqi_max	= kszphy_get_sqi_max,
6779 	.get_mse_capability = kszphy_get_mse_capability,
6780 	.get_mse_snapshot = kszphy_get_mse_snapshot,
6781 } };
6782 
6783 module_phy_driver(ksphy_driver);
6784 
6785 MODULE_DESCRIPTION("Micrel PHY driver");
6786 MODULE_AUTHOR("David J. Choi");
6787 MODULE_LICENSE("GPL");
6788 
6789 static const struct mdio_device_id __maybe_unused micrel_tbl[] = {
6790 	{ PHY_ID_KSZ9021, 0x000ffffe },
6791 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ9031) },
6792 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ9131) },
6793 	{ PHY_ID_KSZ8001, 0x00fffffc },
6794 	{ PHY_ID_MATCH_MODEL(PHY_ID_KS8737) },
6795 	{ PHY_ID_KSZ8021, 0x00ffffff },
6796 	{ PHY_ID_KSZ8031, 0x00ffffff },
6797 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041) },
6798 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8041RNLI) },
6799 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8051) },
6800 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8061) },
6801 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8081) },
6802 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ8873MLL) },
6803 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ886X) },
6804 	{ PHY_ID_MATCH_MODEL(PHY_ID_KSZ9477) },
6805 	{ PHY_ID_MATCH_MODEL(PHY_ID_LAN8814) },
6806 	{ PHY_ID_MATCH_MODEL(PHY_ID_LAN8804) },
6807 	{ PHY_ID_MATCH_MODEL(PHY_ID_LAN8841) },
6808 	{ PHY_ID_MATCH_MODEL(PHY_ID_LAN8842) },
6809 	{ }
6810 };
6811 
6812 MODULE_DEVICE_TABLE(mdio, micrel_tbl);
6813