xref: /openbmc/linux/drivers/net/phy/micrel.c (revision 55b24334)
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 /* Operation Mode Strap Override */
38 #define MII_KSZPHY_OMSO				0x16
39 #define KSZPHY_OMSO_FACTORY_TEST		BIT(15)
40 #define KSZPHY_OMSO_B_CAST_OFF			BIT(9)
41 #define KSZPHY_OMSO_NAND_TREE_ON		BIT(5)
42 #define KSZPHY_OMSO_RMII_OVERRIDE		BIT(1)
43 #define KSZPHY_OMSO_MII_OVERRIDE		BIT(0)
44 
45 /* general Interrupt control/status reg in vendor specific block. */
46 #define MII_KSZPHY_INTCS			0x1B
47 #define KSZPHY_INTCS_JABBER			BIT(15)
48 #define KSZPHY_INTCS_RECEIVE_ERR		BIT(14)
49 #define KSZPHY_INTCS_PAGE_RECEIVE		BIT(13)
50 #define KSZPHY_INTCS_PARELLEL			BIT(12)
51 #define KSZPHY_INTCS_LINK_PARTNER_ACK		BIT(11)
52 #define KSZPHY_INTCS_LINK_DOWN			BIT(10)
53 #define KSZPHY_INTCS_REMOTE_FAULT		BIT(9)
54 #define KSZPHY_INTCS_LINK_UP			BIT(8)
55 #define KSZPHY_INTCS_ALL			(KSZPHY_INTCS_LINK_UP |\
56 						KSZPHY_INTCS_LINK_DOWN)
57 #define KSZPHY_INTCS_LINK_DOWN_STATUS		BIT(2)
58 #define KSZPHY_INTCS_LINK_UP_STATUS		BIT(0)
59 #define KSZPHY_INTCS_STATUS			(KSZPHY_INTCS_LINK_DOWN_STATUS |\
60 						 KSZPHY_INTCS_LINK_UP_STATUS)
61 
62 /* LinkMD Control/Status */
63 #define KSZ8081_LMD				0x1d
64 #define KSZ8081_LMD_ENABLE_TEST			BIT(15)
65 #define KSZ8081_LMD_STAT_NORMAL			0
66 #define KSZ8081_LMD_STAT_OPEN			1
67 #define KSZ8081_LMD_STAT_SHORT			2
68 #define KSZ8081_LMD_STAT_FAIL			3
69 #define KSZ8081_LMD_STAT_MASK			GENMASK(14, 13)
70 /* Short cable (<10 meter) has been detected by LinkMD */
71 #define KSZ8081_LMD_SHORT_INDICATOR		BIT(12)
72 #define KSZ8081_LMD_DELTA_TIME_MASK		GENMASK(8, 0)
73 
74 #define KSZ9x31_LMD				0x12
75 #define KSZ9x31_LMD_VCT_EN			BIT(15)
76 #define KSZ9x31_LMD_VCT_DIS_TX			BIT(14)
77 #define KSZ9x31_LMD_VCT_PAIR(n)			(((n) & 0x3) << 12)
78 #define KSZ9x31_LMD_VCT_SEL_RESULT		0
79 #define KSZ9x31_LMD_VCT_SEL_THRES_HI		BIT(10)
80 #define KSZ9x31_LMD_VCT_SEL_THRES_LO		BIT(11)
81 #define KSZ9x31_LMD_VCT_SEL_MASK		GENMASK(11, 10)
82 #define KSZ9x31_LMD_VCT_ST_NORMAL		0
83 #define KSZ9x31_LMD_VCT_ST_OPEN			1
84 #define KSZ9x31_LMD_VCT_ST_SHORT		2
85 #define KSZ9x31_LMD_VCT_ST_FAIL			3
86 #define KSZ9x31_LMD_VCT_ST_MASK			GENMASK(9, 8)
87 #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID	BIT(7)
88 #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG	BIT(6)
89 #define KSZ9x31_LMD_VCT_DATA_MASK100		BIT(5)
90 #define KSZ9x31_LMD_VCT_DATA_NLP_FLP		BIT(4)
91 #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK	GENMASK(3, 2)
92 #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK	GENMASK(1, 0)
93 #define KSZ9x31_LMD_VCT_DATA_MASK		GENMASK(7, 0)
94 
95 #define KSZPHY_WIRE_PAIR_MASK			0x3
96 
97 #define LAN8814_CABLE_DIAG			0x12
98 #define LAN8814_CABLE_DIAG_STAT_MASK		GENMASK(9, 8)
99 #define LAN8814_CABLE_DIAG_VCT_DATA_MASK	GENMASK(7, 0)
100 #define LAN8814_PAIR_BIT_SHIFT			12
101 
102 #define LAN8814_WIRE_PAIR_MASK			0xF
103 
104 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
105 #define LAN8814_INTC				0x18
106 #define LAN8814_INTS				0x1B
107 
108 #define LAN8814_INT_LINK_DOWN			BIT(2)
109 #define LAN8814_INT_LINK_UP			BIT(0)
110 #define LAN8814_INT_LINK			(LAN8814_INT_LINK_UP |\
111 						 LAN8814_INT_LINK_DOWN)
112 
113 #define LAN8814_INTR_CTRL_REG			0x34
114 #define LAN8814_INTR_CTRL_REG_POLARITY		BIT(1)
115 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE	BIT(0)
116 
117 /* Represents 1ppm adjustment in 2^32 format with
118  * each nsec contains 4 clock cycles.
119  * The value is calculated as following: (1/1000000)/((2^-32)/4)
120  */
121 #define LAN8814_1PPM_FORMAT			17179
122 
123 #define PTP_RX_MOD				0x024F
124 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
125 #define PTP_RX_TIMESTAMP_EN			0x024D
126 #define PTP_TX_TIMESTAMP_EN			0x028D
127 
128 #define PTP_TIMESTAMP_EN_SYNC_			BIT(0)
129 #define PTP_TIMESTAMP_EN_DREQ_			BIT(1)
130 #define PTP_TIMESTAMP_EN_PDREQ_			BIT(2)
131 #define PTP_TIMESTAMP_EN_PDRES_			BIT(3)
132 
133 #define PTP_TX_PARSE_L2_ADDR_EN			0x0284
134 #define PTP_RX_PARSE_L2_ADDR_EN			0x0244
135 
136 #define PTP_TX_PARSE_IP_ADDR_EN			0x0285
137 #define PTP_RX_PARSE_IP_ADDR_EN			0x0245
138 #define LTC_HARD_RESET				0x023F
139 #define LTC_HARD_RESET_				BIT(0)
140 
141 #define TSU_HARD_RESET				0x02C1
142 #define TSU_HARD_RESET_				BIT(0)
143 
144 #define PTP_CMD_CTL				0x0200
145 #define PTP_CMD_CTL_PTP_DISABLE_		BIT(0)
146 #define PTP_CMD_CTL_PTP_ENABLE_			BIT(1)
147 #define PTP_CMD_CTL_PTP_CLOCK_READ_		BIT(3)
148 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_		BIT(4)
149 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_		BIT(5)
150 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_		BIT(6)
151 
152 #define PTP_CLOCK_SET_SEC_MID			0x0206
153 #define PTP_CLOCK_SET_SEC_LO			0x0207
154 #define PTP_CLOCK_SET_NS_HI			0x0208
155 #define PTP_CLOCK_SET_NS_LO			0x0209
156 
157 #define PTP_CLOCK_READ_SEC_MID			0x022A
158 #define PTP_CLOCK_READ_SEC_LO			0x022B
159 #define PTP_CLOCK_READ_NS_HI			0x022C
160 #define PTP_CLOCK_READ_NS_LO			0x022D
161 
162 #define PTP_OPERATING_MODE			0x0241
163 #define PTP_OPERATING_MODE_STANDALONE_		BIT(0)
164 
165 #define PTP_TX_MOD				0x028F
166 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_	BIT(12)
167 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
168 
169 #define PTP_RX_PARSE_CONFIG			0x0242
170 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
171 #define PTP_RX_PARSE_CONFIG_IPV4_EN_		BIT(1)
172 #define PTP_RX_PARSE_CONFIG_IPV6_EN_		BIT(2)
173 
174 #define PTP_TX_PARSE_CONFIG			0x0282
175 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_		BIT(0)
176 #define PTP_TX_PARSE_CONFIG_IPV4_EN_		BIT(1)
177 #define PTP_TX_PARSE_CONFIG_IPV6_EN_		BIT(2)
178 
179 #define PTP_CLOCK_RATE_ADJ_HI			0x020C
180 #define PTP_CLOCK_RATE_ADJ_LO			0x020D
181 #define PTP_CLOCK_RATE_ADJ_DIR_			BIT(15)
182 
183 #define PTP_LTC_STEP_ADJ_HI			0x0212
184 #define PTP_LTC_STEP_ADJ_LO			0x0213
185 #define PTP_LTC_STEP_ADJ_DIR_			BIT(15)
186 
187 #define LAN8814_INTR_STS_REG			0x0033
188 #define LAN8814_INTR_STS_REG_1588_TSU0_		BIT(0)
189 #define LAN8814_INTR_STS_REG_1588_TSU1_		BIT(1)
190 #define LAN8814_INTR_STS_REG_1588_TSU2_		BIT(2)
191 #define LAN8814_INTR_STS_REG_1588_TSU3_		BIT(3)
192 
193 #define PTP_CAP_INFO				0x022A
194 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val)	(((reg_val) & 0x0f00) >> 8)
195 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val)	((reg_val) & 0x000f)
196 
197 #define PTP_TX_EGRESS_SEC_HI			0x0296
198 #define PTP_TX_EGRESS_SEC_LO			0x0297
199 #define PTP_TX_EGRESS_NS_HI			0x0294
200 #define PTP_TX_EGRESS_NS_LO			0x0295
201 #define PTP_TX_MSG_HEADER2			0x0299
202 
203 #define PTP_RX_INGRESS_SEC_HI			0x0256
204 #define PTP_RX_INGRESS_SEC_LO			0x0257
205 #define PTP_RX_INGRESS_NS_HI			0x0254
206 #define PTP_RX_INGRESS_NS_LO			0x0255
207 #define PTP_RX_MSG_HEADER2			0x0259
208 
209 #define PTP_TSU_INT_EN				0x0200
210 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_	BIT(3)
211 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_		BIT(2)
212 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_	BIT(1)
213 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_		BIT(0)
214 
215 #define PTP_TSU_INT_STS				0x0201
216 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_	BIT(3)
217 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_		BIT(2)
218 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_	BIT(1)
219 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_		BIT(0)
220 
221 #define LAN8814_LED_CTRL_1			0x0
222 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_	BIT(6)
223 
224 /* PHY Control 1 */
225 #define MII_KSZPHY_CTRL_1			0x1e
226 #define KSZ8081_CTRL1_MDIX_STAT			BIT(4)
227 
228 /* PHY Control 2 / PHY Control (if no PHY Control 1) */
229 #define MII_KSZPHY_CTRL_2			0x1f
230 #define MII_KSZPHY_CTRL				MII_KSZPHY_CTRL_2
231 /* bitmap of PHY register to set interrupt mode */
232 #define KSZ8081_CTRL2_HP_MDIX			BIT(15)
233 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT		BIT(14)
234 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX		BIT(13)
235 #define KSZ8081_CTRL2_FORCE_LINK		BIT(11)
236 #define KSZ8081_CTRL2_POWER_SAVING		BIT(10)
237 #define KSZPHY_CTRL_INT_ACTIVE_HIGH		BIT(9)
238 #define KSZPHY_RMII_REF_CLK_SEL			BIT(7)
239 
240 /* Write/read to/from extended registers */
241 #define MII_KSZPHY_EXTREG			0x0b
242 #define KSZPHY_EXTREG_WRITE			0x8000
243 
244 #define MII_KSZPHY_EXTREG_WRITE			0x0c
245 #define MII_KSZPHY_EXTREG_READ			0x0d
246 
247 /* Extended registers */
248 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
249 #define MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
250 #define MII_KSZPHY_TX_DATA_PAD_SKEW		0x106
251 
252 #define PS_TO_REG				200
253 #define FIFO_SIZE				8
254 
255 struct kszphy_hw_stat {
256 	const char *string;
257 	u8 reg;
258 	u8 bits;
259 };
260 
261 static struct kszphy_hw_stat kszphy_hw_stats[] = {
262 	{ "phy_receive_errors", 21, 16},
263 	{ "phy_idle_errors", 10, 8 },
264 };
265 
266 struct kszphy_type {
267 	u32 led_mode_reg;
268 	u16 interrupt_level_mask;
269 	u16 cable_diag_reg;
270 	unsigned long pair_mask;
271 	u16 disable_dll_tx_bit;
272 	u16 disable_dll_rx_bit;
273 	u16 disable_dll_mask;
274 	bool has_broadcast_disable;
275 	bool has_nand_tree_disable;
276 	bool has_rmii_ref_clk_sel;
277 };
278 
279 /* Shared structure between the PHYs of the same package. */
280 struct lan8814_shared_priv {
281 	struct phy_device *phydev;
282 	struct ptp_clock *ptp_clock;
283 	struct ptp_clock_info ptp_clock_info;
284 
285 	/* Reference counter to how many ports in the package are enabling the
286 	 * timestamping
287 	 */
288 	u8 ref;
289 
290 	/* Lock for ptp_clock and ref */
291 	struct mutex shared_lock;
292 };
293 
294 struct lan8814_ptp_rx_ts {
295 	struct list_head list;
296 	u32 seconds;
297 	u32 nsec;
298 	u16 seq_id;
299 };
300 
301 struct kszphy_ptp_priv {
302 	struct mii_timestamper mii_ts;
303 	struct phy_device *phydev;
304 
305 	struct sk_buff_head tx_queue;
306 	struct sk_buff_head rx_queue;
307 
308 	struct list_head rx_ts_list;
309 	/* Lock for Rx ts fifo */
310 	spinlock_t rx_ts_lock;
311 
312 	int hwts_tx_type;
313 	enum hwtstamp_rx_filters rx_filter;
314 	int layer;
315 	int version;
316 
317 	struct ptp_clock *ptp_clock;
318 	struct ptp_clock_info ptp_clock_info;
319 	/* Lock for ptp_clock */
320 	struct mutex ptp_lock;
321 	struct ptp_pin_desc *pin_config;
322 };
323 
324 struct kszphy_priv {
325 	struct kszphy_ptp_priv ptp_priv;
326 	const struct kszphy_type *type;
327 	int led_mode;
328 	u16 vct_ctrl1000;
329 	bool rmii_ref_clk_sel;
330 	bool rmii_ref_clk_sel_val;
331 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
332 };
333 
334 static const struct kszphy_type lan8814_type = {
335 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
336 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
337 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
338 };
339 
340 static const struct kszphy_type ksz886x_type = {
341 	.cable_diag_reg		= KSZ8081_LMD,
342 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
343 };
344 
345 static const struct kszphy_type ksz8021_type = {
346 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
347 	.has_broadcast_disable	= true,
348 	.has_nand_tree_disable	= true,
349 	.has_rmii_ref_clk_sel	= true,
350 };
351 
352 static const struct kszphy_type ksz8041_type = {
353 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
354 };
355 
356 static const struct kszphy_type ksz8051_type = {
357 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
358 	.has_nand_tree_disable	= true,
359 };
360 
361 static const struct kszphy_type ksz8081_type = {
362 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
363 	.has_broadcast_disable	= true,
364 	.has_nand_tree_disable	= true,
365 	.has_rmii_ref_clk_sel	= true,
366 };
367 
368 static const struct kszphy_type ks8737_type = {
369 	.interrupt_level_mask	= BIT(14),
370 };
371 
372 static const struct kszphy_type ksz9021_type = {
373 	.interrupt_level_mask	= BIT(14),
374 };
375 
376 static const struct kszphy_type ksz9131_type = {
377 	.interrupt_level_mask	= BIT(14),
378 	.disable_dll_tx_bit	= BIT(12),
379 	.disable_dll_rx_bit	= BIT(12),
380 	.disable_dll_mask	= BIT_MASK(12),
381 };
382 
383 static const struct kszphy_type lan8841_type = {
384 	.disable_dll_tx_bit	= BIT(14),
385 	.disable_dll_rx_bit	= BIT(14),
386 	.disable_dll_mask	= BIT_MASK(14),
387 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
388 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
389 };
390 
391 static int kszphy_extended_write(struct phy_device *phydev,
392 				u32 regnum, u16 val)
393 {
394 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
395 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
396 }
397 
398 static int kszphy_extended_read(struct phy_device *phydev,
399 				u32 regnum)
400 {
401 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
402 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
403 }
404 
405 static int kszphy_ack_interrupt(struct phy_device *phydev)
406 {
407 	/* bit[7..0] int status, which is a read and clear register. */
408 	int rc;
409 
410 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
411 
412 	return (rc < 0) ? rc : 0;
413 }
414 
415 static int kszphy_config_intr(struct phy_device *phydev)
416 {
417 	const struct kszphy_type *type = phydev->drv->driver_data;
418 	int temp, err;
419 	u16 mask;
420 
421 	if (type && type->interrupt_level_mask)
422 		mask = type->interrupt_level_mask;
423 	else
424 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
425 
426 	/* set the interrupt pin active low */
427 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
428 	if (temp < 0)
429 		return temp;
430 	temp &= ~mask;
431 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
432 
433 	/* enable / disable interrupts */
434 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
435 		err = kszphy_ack_interrupt(phydev);
436 		if (err)
437 			return err;
438 
439 		err = phy_write(phydev, MII_KSZPHY_INTCS, KSZPHY_INTCS_ALL);
440 	} else {
441 		err = phy_write(phydev, MII_KSZPHY_INTCS, 0);
442 		if (err)
443 			return err;
444 
445 		err = kszphy_ack_interrupt(phydev);
446 	}
447 
448 	return err;
449 }
450 
451 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
452 {
453 	int irq_status;
454 
455 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
456 	if (irq_status < 0) {
457 		phy_error(phydev);
458 		return IRQ_NONE;
459 	}
460 
461 	if (!(irq_status & KSZPHY_INTCS_STATUS))
462 		return IRQ_NONE;
463 
464 	phy_trigger_machine(phydev);
465 
466 	return IRQ_HANDLED;
467 }
468 
469 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
470 {
471 	int ctrl;
472 
473 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
474 	if (ctrl < 0)
475 		return ctrl;
476 
477 	if (val)
478 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
479 	else
480 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
481 
482 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
483 }
484 
485 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
486 {
487 	int rc, temp, shift;
488 
489 	switch (reg) {
490 	case MII_KSZPHY_CTRL_1:
491 		shift = 14;
492 		break;
493 	case MII_KSZPHY_CTRL_2:
494 		shift = 4;
495 		break;
496 	default:
497 		return -EINVAL;
498 	}
499 
500 	temp = phy_read(phydev, reg);
501 	if (temp < 0) {
502 		rc = temp;
503 		goto out;
504 	}
505 
506 	temp &= ~(3 << shift);
507 	temp |= val << shift;
508 	rc = phy_write(phydev, reg, temp);
509 out:
510 	if (rc < 0)
511 		phydev_err(phydev, "failed to set led mode\n");
512 
513 	return rc;
514 }
515 
516 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
517  * unique (non-broadcast) address on a shared bus.
518  */
519 static int kszphy_broadcast_disable(struct phy_device *phydev)
520 {
521 	int ret;
522 
523 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
524 	if (ret < 0)
525 		goto out;
526 
527 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
528 out:
529 	if (ret)
530 		phydev_err(phydev, "failed to disable broadcast address\n");
531 
532 	return ret;
533 }
534 
535 static int kszphy_nand_tree_disable(struct phy_device *phydev)
536 {
537 	int ret;
538 
539 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
540 	if (ret < 0)
541 		goto out;
542 
543 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
544 		return 0;
545 
546 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
547 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
548 out:
549 	if (ret)
550 		phydev_err(phydev, "failed to disable NAND tree mode\n");
551 
552 	return ret;
553 }
554 
555 /* Some config bits need to be set again on resume, handle them here. */
556 static int kszphy_config_reset(struct phy_device *phydev)
557 {
558 	struct kszphy_priv *priv = phydev->priv;
559 	int ret;
560 
561 	if (priv->rmii_ref_clk_sel) {
562 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
563 		if (ret) {
564 			phydev_err(phydev,
565 				   "failed to set rmii reference clock\n");
566 			return ret;
567 		}
568 	}
569 
570 	if (priv->type && priv->led_mode >= 0)
571 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
572 
573 	return 0;
574 }
575 
576 static int kszphy_config_init(struct phy_device *phydev)
577 {
578 	struct kszphy_priv *priv = phydev->priv;
579 	const struct kszphy_type *type;
580 
581 	if (!priv)
582 		return 0;
583 
584 	type = priv->type;
585 
586 	if (type && type->has_broadcast_disable)
587 		kszphy_broadcast_disable(phydev);
588 
589 	if (type && type->has_nand_tree_disable)
590 		kszphy_nand_tree_disable(phydev);
591 
592 	return kszphy_config_reset(phydev);
593 }
594 
595 static int ksz8041_fiber_mode(struct phy_device *phydev)
596 {
597 	struct device_node *of_node = phydev->mdio.dev.of_node;
598 
599 	return of_property_read_bool(of_node, "micrel,fiber-mode");
600 }
601 
602 static int ksz8041_config_init(struct phy_device *phydev)
603 {
604 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
605 
606 	/* Limit supported and advertised modes in fiber mode */
607 	if (ksz8041_fiber_mode(phydev)) {
608 		phydev->dev_flags |= MICREL_PHY_FXEN;
609 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
610 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
611 
612 		linkmode_and(phydev->supported, phydev->supported, mask);
613 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
614 				 phydev->supported);
615 		linkmode_and(phydev->advertising, phydev->advertising, mask);
616 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
617 				 phydev->advertising);
618 		phydev->autoneg = AUTONEG_DISABLE;
619 	}
620 
621 	return kszphy_config_init(phydev);
622 }
623 
624 static int ksz8041_config_aneg(struct phy_device *phydev)
625 {
626 	/* Skip auto-negotiation in fiber mode */
627 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
628 		phydev->speed = SPEED_100;
629 		return 0;
630 	}
631 
632 	return genphy_config_aneg(phydev);
633 }
634 
635 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
636 					    const bool ksz_8051)
637 {
638 	int ret;
639 
640 	if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
641 		return 0;
642 
643 	ret = phy_read(phydev, MII_BMSR);
644 	if (ret < 0)
645 		return ret;
646 
647 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
648 	 * exact PHY ID. However, they can be told apart by the extended
649 	 * capability registers presence. The KSZ8051 PHY has them while
650 	 * the switch does not.
651 	 */
652 	ret &= BMSR_ERCAP;
653 	if (ksz_8051)
654 		return ret;
655 	else
656 		return !ret;
657 }
658 
659 static int ksz8051_match_phy_device(struct phy_device *phydev)
660 {
661 	return ksz8051_ksz8795_match_phy_device(phydev, true);
662 }
663 
664 static int ksz8081_config_init(struct phy_device *phydev)
665 {
666 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
667 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
668 	 * pull-down is missing, the factory test mode should be cleared by
669 	 * manually writing a 0.
670 	 */
671 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
672 
673 	return kszphy_config_init(phydev);
674 }
675 
676 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
677 {
678 	u16 val;
679 
680 	switch (ctrl) {
681 	case ETH_TP_MDI:
682 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
683 		break;
684 	case ETH_TP_MDI_X:
685 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
686 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
687 		break;
688 	case ETH_TP_MDI_AUTO:
689 		val = 0;
690 		break;
691 	default:
692 		return 0;
693 	}
694 
695 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
696 			  KSZ8081_CTRL2_HP_MDIX |
697 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
698 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
699 			  KSZ8081_CTRL2_HP_MDIX | val);
700 }
701 
702 static int ksz8081_config_aneg(struct phy_device *phydev)
703 {
704 	int ret;
705 
706 	ret = genphy_config_aneg(phydev);
707 	if (ret)
708 		return ret;
709 
710 	/* The MDI-X configuration is automatically changed by the PHY after
711 	 * switching from autoneg off to on. So, take MDI-X configuration under
712 	 * own control and set it after autoneg configuration was done.
713 	 */
714 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
715 }
716 
717 static int ksz8081_mdix_update(struct phy_device *phydev)
718 {
719 	int ret;
720 
721 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
722 	if (ret < 0)
723 		return ret;
724 
725 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
726 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
727 			phydev->mdix_ctrl = ETH_TP_MDI_X;
728 		else
729 			phydev->mdix_ctrl = ETH_TP_MDI;
730 	} else {
731 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
732 	}
733 
734 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
735 	if (ret < 0)
736 		return ret;
737 
738 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
739 		phydev->mdix = ETH_TP_MDI;
740 	else
741 		phydev->mdix = ETH_TP_MDI_X;
742 
743 	return 0;
744 }
745 
746 static int ksz8081_read_status(struct phy_device *phydev)
747 {
748 	int ret;
749 
750 	ret = ksz8081_mdix_update(phydev);
751 	if (ret < 0)
752 		return ret;
753 
754 	return genphy_read_status(phydev);
755 }
756 
757 static int ksz8061_config_init(struct phy_device *phydev)
758 {
759 	int ret;
760 
761 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
762 	if (ret)
763 		return ret;
764 
765 	return kszphy_config_init(phydev);
766 }
767 
768 static int ksz8795_match_phy_device(struct phy_device *phydev)
769 {
770 	return ksz8051_ksz8795_match_phy_device(phydev, false);
771 }
772 
773 static int ksz9021_load_values_from_of(struct phy_device *phydev,
774 				       const struct device_node *of_node,
775 				       u16 reg,
776 				       const char *field1, const char *field2,
777 				       const char *field3, const char *field4)
778 {
779 	int val1 = -1;
780 	int val2 = -2;
781 	int val3 = -3;
782 	int val4 = -4;
783 	int newval;
784 	int matches = 0;
785 
786 	if (!of_property_read_u32(of_node, field1, &val1))
787 		matches++;
788 
789 	if (!of_property_read_u32(of_node, field2, &val2))
790 		matches++;
791 
792 	if (!of_property_read_u32(of_node, field3, &val3))
793 		matches++;
794 
795 	if (!of_property_read_u32(of_node, field4, &val4))
796 		matches++;
797 
798 	if (!matches)
799 		return 0;
800 
801 	if (matches < 4)
802 		newval = kszphy_extended_read(phydev, reg);
803 	else
804 		newval = 0;
805 
806 	if (val1 != -1)
807 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
808 
809 	if (val2 != -2)
810 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
811 
812 	if (val3 != -3)
813 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
814 
815 	if (val4 != -4)
816 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
817 
818 	return kszphy_extended_write(phydev, reg, newval);
819 }
820 
821 static int ksz9021_config_init(struct phy_device *phydev)
822 {
823 	const struct device_node *of_node;
824 	const struct device *dev_walker;
825 
826 	/* The Micrel driver has a deprecated option to place phy OF
827 	 * properties in the MAC node. Walk up the tree of devices to
828 	 * find a device with an OF node.
829 	 */
830 	dev_walker = &phydev->mdio.dev;
831 	do {
832 		of_node = dev_walker->of_node;
833 		dev_walker = dev_walker->parent;
834 
835 	} while (!of_node && dev_walker);
836 
837 	if (of_node) {
838 		ksz9021_load_values_from_of(phydev, of_node,
839 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
840 				    "txen-skew-ps", "txc-skew-ps",
841 				    "rxdv-skew-ps", "rxc-skew-ps");
842 		ksz9021_load_values_from_of(phydev, of_node,
843 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
844 				    "rxd0-skew-ps", "rxd1-skew-ps",
845 				    "rxd2-skew-ps", "rxd3-skew-ps");
846 		ksz9021_load_values_from_of(phydev, of_node,
847 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
848 				    "txd0-skew-ps", "txd1-skew-ps",
849 				    "txd2-skew-ps", "txd3-skew-ps");
850 	}
851 	return 0;
852 }
853 
854 #define KSZ9031_PS_TO_REG		60
855 
856 /* Extended registers */
857 /* MMD Address 0x0 */
858 #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
859 #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
860 
861 /* MMD Address 0x2 */
862 #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
863 #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
864 #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
865 
866 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
867 #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
868 #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
869 #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
870 #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
871 
872 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
873 #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
874 #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
875 #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
876 #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
877 
878 #define MII_KSZ9031RN_CLK_PAD_SKEW	8
879 #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
880 #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
881 
882 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
883  * provide different RGMII options we need to configure delay offset
884  * for each pad relative to build in delay.
885  */
886 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
887  * 1.80ns
888  */
889 #define RX_ID				0x7
890 #define RX_CLK_ID			0x19
891 
892 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
893  * internal 1.2ns delay.
894  */
895 #define RX_ND				0xc
896 #define RX_CLK_ND			0x0
897 
898 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
899 #define TX_ID				0x0
900 #define TX_CLK_ID			0x1f
901 
902 /* set tx and tx_clk to "No delay adjustment" to keep 0ns
903  * dealy
904  */
905 #define TX_ND				0x7
906 #define TX_CLK_ND			0xf
907 
908 /* MMD Address 0x1C */
909 #define MII_KSZ9031RN_EDPD		0x23
910 #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
911 
912 static int ksz9031_of_load_skew_values(struct phy_device *phydev,
913 				       const struct device_node *of_node,
914 				       u16 reg, size_t field_sz,
915 				       const char *field[], u8 numfields,
916 				       bool *update)
917 {
918 	int val[4] = {-1, -2, -3, -4};
919 	int matches = 0;
920 	u16 mask;
921 	u16 maxval;
922 	u16 newval;
923 	int i;
924 
925 	for (i = 0; i < numfields; i++)
926 		if (!of_property_read_u32(of_node, field[i], val + i))
927 			matches++;
928 
929 	if (!matches)
930 		return 0;
931 
932 	*update |= true;
933 
934 	if (matches < numfields)
935 		newval = phy_read_mmd(phydev, 2, reg);
936 	else
937 		newval = 0;
938 
939 	maxval = (field_sz == 4) ? 0xf : 0x1f;
940 	for (i = 0; i < numfields; i++)
941 		if (val[i] != -(i + 1)) {
942 			mask = 0xffff;
943 			mask ^= maxval << (field_sz * i);
944 			newval = (newval & mask) |
945 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
946 					<< (field_sz * i));
947 		}
948 
949 	return phy_write_mmd(phydev, 2, reg, newval);
950 }
951 
952 /* Center KSZ9031RNX FLP timing at 16ms. */
953 static int ksz9031_center_flp_timing(struct phy_device *phydev)
954 {
955 	int result;
956 
957 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
958 			       0x0006);
959 	if (result)
960 		return result;
961 
962 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
963 			       0x1A80);
964 	if (result)
965 		return result;
966 
967 	return genphy_restart_aneg(phydev);
968 }
969 
970 /* Enable energy-detect power-down mode */
971 static int ksz9031_enable_edpd(struct phy_device *phydev)
972 {
973 	int reg;
974 
975 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
976 	if (reg < 0)
977 		return reg;
978 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
979 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
980 }
981 
982 static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
983 {
984 	u16 rx, tx, rx_clk, tx_clk;
985 	int ret;
986 
987 	switch (phydev->interface) {
988 	case PHY_INTERFACE_MODE_RGMII:
989 		tx = TX_ND;
990 		tx_clk = TX_CLK_ND;
991 		rx = RX_ND;
992 		rx_clk = RX_CLK_ND;
993 		break;
994 	case PHY_INTERFACE_MODE_RGMII_ID:
995 		tx = TX_ID;
996 		tx_clk = TX_CLK_ID;
997 		rx = RX_ID;
998 		rx_clk = RX_CLK_ID;
999 		break;
1000 	case PHY_INTERFACE_MODE_RGMII_RXID:
1001 		tx = TX_ND;
1002 		tx_clk = TX_CLK_ND;
1003 		rx = RX_ID;
1004 		rx_clk = RX_CLK_ID;
1005 		break;
1006 	case PHY_INTERFACE_MODE_RGMII_TXID:
1007 		tx = TX_ID;
1008 		tx_clk = TX_CLK_ID;
1009 		rx = RX_ND;
1010 		rx_clk = RX_CLK_ND;
1011 		break;
1012 	default:
1013 		return 0;
1014 	}
1015 
1016 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
1017 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
1018 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
1019 	if (ret < 0)
1020 		return ret;
1021 
1022 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1023 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1024 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1025 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1026 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1027 	if (ret < 0)
1028 		return ret;
1029 
1030 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1031 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1032 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1033 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1034 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1035 	if (ret < 0)
1036 		return ret;
1037 
1038 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1039 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1040 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1041 }
1042 
1043 static int ksz9031_config_init(struct phy_device *phydev)
1044 {
1045 	const struct device_node *of_node;
1046 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
1047 	static const char *rx_data_skews[4] = {
1048 		"rxd0-skew-ps", "rxd1-skew-ps",
1049 		"rxd2-skew-ps", "rxd3-skew-ps"
1050 	};
1051 	static const char *tx_data_skews[4] = {
1052 		"txd0-skew-ps", "txd1-skew-ps",
1053 		"txd2-skew-ps", "txd3-skew-ps"
1054 	};
1055 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1056 	const struct device *dev_walker;
1057 	int result;
1058 
1059 	result = ksz9031_enable_edpd(phydev);
1060 	if (result < 0)
1061 		return result;
1062 
1063 	/* The Micrel driver has a deprecated option to place phy OF
1064 	 * properties in the MAC node. Walk up the tree of devices to
1065 	 * find a device with an OF node.
1066 	 */
1067 	dev_walker = &phydev->mdio.dev;
1068 	do {
1069 		of_node = dev_walker->of_node;
1070 		dev_walker = dev_walker->parent;
1071 	} while (!of_node && dev_walker);
1072 
1073 	if (of_node) {
1074 		bool update = false;
1075 
1076 		if (phy_interface_is_rgmii(phydev)) {
1077 			result = ksz9031_config_rgmii_delay(phydev);
1078 			if (result < 0)
1079 				return result;
1080 		}
1081 
1082 		ksz9031_of_load_skew_values(phydev, of_node,
1083 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1084 				clk_skews, 2, &update);
1085 
1086 		ksz9031_of_load_skew_values(phydev, of_node,
1087 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1088 				control_skews, 2, &update);
1089 
1090 		ksz9031_of_load_skew_values(phydev, of_node,
1091 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1092 				rx_data_skews, 4, &update);
1093 
1094 		ksz9031_of_load_skew_values(phydev, of_node,
1095 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1096 				tx_data_skews, 4, &update);
1097 
1098 		if (update && !phy_interface_is_rgmii(phydev))
1099 			phydev_warn(phydev,
1100 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1101 
1102 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1103 		 * When the device links in the 1000BASE-T slave mode only,
1104 		 * the optional 125MHz reference output clock (CLK125_NDO)
1105 		 * has wide duty cycle variation.
1106 		 *
1107 		 * The optional CLK125_NDO clock does not meet the RGMII
1108 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1109 		 * cannot be used directly by the MAC side for clocking
1110 		 * applications that have setup/hold time requirements on
1111 		 * rising and falling clock edges.
1112 		 *
1113 		 * Workaround:
1114 		 * Force the phy to be the master to receive a stable clock
1115 		 * which meets the duty cycle requirement.
1116 		 */
1117 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1118 			result = phy_read(phydev, MII_CTRL1000);
1119 			if (result < 0)
1120 				goto err_force_master;
1121 
1122 			/* enable master mode, config & prefer master */
1123 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1124 			result = phy_write(phydev, MII_CTRL1000, result);
1125 			if (result < 0)
1126 				goto err_force_master;
1127 		}
1128 	}
1129 
1130 	return ksz9031_center_flp_timing(phydev);
1131 
1132 err_force_master:
1133 	phydev_err(phydev, "failed to force the phy to master mode\n");
1134 	return result;
1135 }
1136 
1137 #define KSZ9131_SKEW_5BIT_MAX	2400
1138 #define KSZ9131_SKEW_4BIT_MAX	800
1139 #define KSZ9131_OFFSET		700
1140 #define KSZ9131_STEP		100
1141 
1142 static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1143 				       struct device_node *of_node,
1144 				       u16 reg, size_t field_sz,
1145 				       char *field[], u8 numfields)
1146 {
1147 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1148 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1149 	int skewval, skewmax = 0;
1150 	int matches = 0;
1151 	u16 maxval;
1152 	u16 newval;
1153 	u16 mask;
1154 	int i;
1155 
1156 	/* psec properties in dts should mean x pico seconds */
1157 	if (field_sz == 5)
1158 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1159 	else
1160 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1161 
1162 	for (i = 0; i < numfields; i++)
1163 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1164 			if (skewval < -KSZ9131_OFFSET)
1165 				skewval = -KSZ9131_OFFSET;
1166 			else if (skewval > skewmax)
1167 				skewval = skewmax;
1168 
1169 			val[i] = skewval + KSZ9131_OFFSET;
1170 			matches++;
1171 		}
1172 
1173 	if (!matches)
1174 		return 0;
1175 
1176 	if (matches < numfields)
1177 		newval = phy_read_mmd(phydev, 2, reg);
1178 	else
1179 		newval = 0;
1180 
1181 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1182 	for (i = 0; i < numfields; i++)
1183 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1184 			mask = 0xffff;
1185 			mask ^= maxval << (field_sz * i);
1186 			newval = (newval & mask) |
1187 				(((val[i] / KSZ9131_STEP) & maxval)
1188 					<< (field_sz * i));
1189 		}
1190 
1191 	return phy_write_mmd(phydev, 2, reg, newval);
1192 }
1193 
1194 #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1195 #define KSZ9131RN_RXC_DLL_CTRL		76
1196 #define KSZ9131RN_TXC_DLL_CTRL		77
1197 #define KSZ9131RN_DLL_ENABLE_DELAY	0
1198 
1199 static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1200 {
1201 	const struct kszphy_type *type = phydev->drv->driver_data;
1202 	u16 rxcdll_val, txcdll_val;
1203 	int ret;
1204 
1205 	switch (phydev->interface) {
1206 	case PHY_INTERFACE_MODE_RGMII:
1207 		rxcdll_val = type->disable_dll_rx_bit;
1208 		txcdll_val = type->disable_dll_tx_bit;
1209 		break;
1210 	case PHY_INTERFACE_MODE_RGMII_ID:
1211 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1212 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1213 		break;
1214 	case PHY_INTERFACE_MODE_RGMII_RXID:
1215 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1216 		txcdll_val = type->disable_dll_tx_bit;
1217 		break;
1218 	case PHY_INTERFACE_MODE_RGMII_TXID:
1219 		rxcdll_val = type->disable_dll_rx_bit;
1220 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1221 		break;
1222 	default:
1223 		return 0;
1224 	}
1225 
1226 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1227 			     KSZ9131RN_RXC_DLL_CTRL, type->disable_dll_mask,
1228 			     rxcdll_val);
1229 	if (ret < 0)
1230 		return ret;
1231 
1232 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1233 			      KSZ9131RN_TXC_DLL_CTRL, type->disable_dll_mask,
1234 			      txcdll_val);
1235 }
1236 
1237 /* Silicon Errata DS80000693B
1238  *
1239  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
1240  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
1241  * according to the datasheet (off if there is no link).
1242  */
1243 static int ksz9131_led_errata(struct phy_device *phydev)
1244 {
1245 	int reg;
1246 
1247 	reg = phy_read_mmd(phydev, 2, 0);
1248 	if (reg < 0)
1249 		return reg;
1250 
1251 	if (!(reg & BIT(4)))
1252 		return 0;
1253 
1254 	return phy_set_bits(phydev, 0x1e, BIT(9));
1255 }
1256 
1257 static int ksz9131_config_init(struct phy_device *phydev)
1258 {
1259 	struct device_node *of_node;
1260 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1261 	char *rx_data_skews[4] = {
1262 		"rxd0-skew-psec", "rxd1-skew-psec",
1263 		"rxd2-skew-psec", "rxd3-skew-psec"
1264 	};
1265 	char *tx_data_skews[4] = {
1266 		"txd0-skew-psec", "txd1-skew-psec",
1267 		"txd2-skew-psec", "txd3-skew-psec"
1268 	};
1269 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1270 	const struct device *dev_walker;
1271 	int ret;
1272 
1273 	dev_walker = &phydev->mdio.dev;
1274 	do {
1275 		of_node = dev_walker->of_node;
1276 		dev_walker = dev_walker->parent;
1277 	} while (!of_node && dev_walker);
1278 
1279 	if (!of_node)
1280 		return 0;
1281 
1282 	if (phy_interface_is_rgmii(phydev)) {
1283 		ret = ksz9131_config_rgmii_delay(phydev);
1284 		if (ret < 0)
1285 			return ret;
1286 	}
1287 
1288 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1289 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1290 					  clk_skews, 2);
1291 	if (ret < 0)
1292 		return ret;
1293 
1294 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1295 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1296 					  control_skews, 2);
1297 	if (ret < 0)
1298 		return ret;
1299 
1300 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1301 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1302 					  rx_data_skews, 4);
1303 	if (ret < 0)
1304 		return ret;
1305 
1306 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1307 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1308 					  tx_data_skews, 4);
1309 	if (ret < 0)
1310 		return ret;
1311 
1312 	ret = ksz9131_led_errata(phydev);
1313 	if (ret < 0)
1314 		return ret;
1315 
1316 	return 0;
1317 }
1318 
1319 #define MII_KSZ9131_AUTO_MDIX		0x1C
1320 #define MII_KSZ9131_AUTO_MDI_SET	BIT(7)
1321 #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF	BIT(6)
1322 
1323 static int ksz9131_mdix_update(struct phy_device *phydev)
1324 {
1325 	int ret;
1326 
1327 	ret = phy_read(phydev, MII_KSZ9131_AUTO_MDIX);
1328 	if (ret < 0)
1329 		return ret;
1330 
1331 	if (ret & MII_KSZ9131_AUTO_MDIX_SWAP_OFF) {
1332 		if (ret & MII_KSZ9131_AUTO_MDI_SET)
1333 			phydev->mdix_ctrl = ETH_TP_MDI;
1334 		else
1335 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1336 	} else {
1337 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1338 	}
1339 
1340 	if (ret & MII_KSZ9131_AUTO_MDI_SET)
1341 		phydev->mdix = ETH_TP_MDI;
1342 	else
1343 		phydev->mdix = ETH_TP_MDI_X;
1344 
1345 	return 0;
1346 }
1347 
1348 static int ksz9131_config_mdix(struct phy_device *phydev, u8 ctrl)
1349 {
1350 	u16 val;
1351 
1352 	switch (ctrl) {
1353 	case ETH_TP_MDI:
1354 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1355 		      MII_KSZ9131_AUTO_MDI_SET;
1356 		break;
1357 	case ETH_TP_MDI_X:
1358 		val = MII_KSZ9131_AUTO_MDIX_SWAP_OFF;
1359 		break;
1360 	case ETH_TP_MDI_AUTO:
1361 		val = 0;
1362 		break;
1363 	default:
1364 		return 0;
1365 	}
1366 
1367 	return phy_modify(phydev, MII_KSZ9131_AUTO_MDIX,
1368 			  MII_KSZ9131_AUTO_MDIX_SWAP_OFF |
1369 			  MII_KSZ9131_AUTO_MDI_SET, val);
1370 }
1371 
1372 static int ksz9131_read_status(struct phy_device *phydev)
1373 {
1374 	int ret;
1375 
1376 	ret = ksz9131_mdix_update(phydev);
1377 	if (ret < 0)
1378 		return ret;
1379 
1380 	return genphy_read_status(phydev);
1381 }
1382 
1383 static int ksz9131_config_aneg(struct phy_device *phydev)
1384 {
1385 	int ret;
1386 
1387 	ret = ksz9131_config_mdix(phydev, phydev->mdix_ctrl);
1388 	if (ret)
1389 		return ret;
1390 
1391 	return genphy_config_aneg(phydev);
1392 }
1393 
1394 static int ksz9477_get_features(struct phy_device *phydev)
1395 {
1396 	int ret;
1397 
1398 	ret = genphy_read_abilities(phydev);
1399 	if (ret)
1400 		return ret;
1401 
1402 	/* The "EEE control and capability 1" (Register 3.20) seems to be
1403 	 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
1404 	 * on the 7.60 will affect 3.20. So, we need to construct our own list
1405 	 * of caps.
1406 	 * KSZ8563R should have 100BaseTX/Full only.
1407 	 */
1408 	linkmode_and(phydev->supported_eee, phydev->supported,
1409 		     PHY_EEE_CAP1_FEATURES);
1410 
1411 	return 0;
1412 }
1413 
1414 #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
1415 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
1416 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
1417 static int ksz8873mll_read_status(struct phy_device *phydev)
1418 {
1419 	int regval;
1420 
1421 	/* dummy read */
1422 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1423 
1424 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1425 
1426 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
1427 		phydev->duplex = DUPLEX_HALF;
1428 	else
1429 		phydev->duplex = DUPLEX_FULL;
1430 
1431 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
1432 		phydev->speed = SPEED_10;
1433 	else
1434 		phydev->speed = SPEED_100;
1435 
1436 	phydev->link = 1;
1437 	phydev->pause = phydev->asym_pause = 0;
1438 
1439 	return 0;
1440 }
1441 
1442 static int ksz9031_get_features(struct phy_device *phydev)
1443 {
1444 	int ret;
1445 
1446 	ret = genphy_read_abilities(phydev);
1447 	if (ret < 0)
1448 		return ret;
1449 
1450 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1451 	 * Whenever the device's Asymmetric Pause capability is set to 1,
1452 	 * link-up may fail after a link-up to link-down transition.
1453 	 *
1454 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1455 	 *
1456 	 * Workaround:
1457 	 * Do not enable the Asymmetric Pause capability bit.
1458 	 */
1459 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1460 
1461 	/* We force setting the Pause capability as the core will force the
1462 	 * Asymmetric Pause capability to 1 otherwise.
1463 	 */
1464 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1465 
1466 	return 0;
1467 }
1468 
1469 static int ksz9031_read_status(struct phy_device *phydev)
1470 {
1471 	int err;
1472 	int regval;
1473 
1474 	err = genphy_read_status(phydev);
1475 	if (err)
1476 		return err;
1477 
1478 	/* Make sure the PHY is not broken. Read idle error count,
1479 	 * and reset the PHY if it is maxed out.
1480 	 */
1481 	regval = phy_read(phydev, MII_STAT1000);
1482 	if ((regval & 0xFF) == 0xFF) {
1483 		phy_init_hw(phydev);
1484 		phydev->link = 0;
1485 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1486 			phydev->drv->config_intr(phydev);
1487 		return genphy_config_aneg(phydev);
1488 	}
1489 
1490 	return 0;
1491 }
1492 
1493 static int ksz9x31_cable_test_start(struct phy_device *phydev)
1494 {
1495 	struct kszphy_priv *priv = phydev->priv;
1496 	int ret;
1497 
1498 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1499 	 * Prior to running the cable diagnostics, Auto-negotiation should
1500 	 * be disabled, full duplex set and the link speed set to 1000Mbps
1501 	 * via the Basic Control Register.
1502 	 */
1503 	ret = phy_modify(phydev, MII_BMCR,
1504 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
1505 			 BMCR_ANENABLE | BMCR_SPEED100,
1506 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
1507 	if (ret)
1508 		return ret;
1509 
1510 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1511 	 * The Master-Slave configuration should be set to Slave by writing
1512 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
1513 	 * Register.
1514 	 */
1515 	ret = phy_read(phydev, MII_CTRL1000);
1516 	if (ret < 0)
1517 		return ret;
1518 
1519 	/* Cache these bits, they need to be restored once LinkMD finishes. */
1520 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1521 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1522 	ret |= CTL1000_ENABLE_MASTER;
1523 
1524 	return phy_write(phydev, MII_CTRL1000, ret);
1525 }
1526 
1527 static int ksz9x31_cable_test_result_trans(u16 status)
1528 {
1529 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1530 	case KSZ9x31_LMD_VCT_ST_NORMAL:
1531 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1532 	case KSZ9x31_LMD_VCT_ST_OPEN:
1533 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1534 	case KSZ9x31_LMD_VCT_ST_SHORT:
1535 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1536 	case KSZ9x31_LMD_VCT_ST_FAIL:
1537 		fallthrough;
1538 	default:
1539 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1540 	}
1541 }
1542 
1543 static bool ksz9x31_cable_test_failed(u16 status)
1544 {
1545 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
1546 
1547 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
1548 }
1549 
1550 static bool ksz9x31_cable_test_fault_length_valid(u16 status)
1551 {
1552 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1553 	case KSZ9x31_LMD_VCT_ST_OPEN:
1554 		fallthrough;
1555 	case KSZ9x31_LMD_VCT_ST_SHORT:
1556 		return true;
1557 	}
1558 	return false;
1559 }
1560 
1561 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
1562 {
1563 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
1564 
1565 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1566 	 *
1567 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
1568 	 */
1569 	if (phydev_id_compare(phydev, PHY_ID_KSZ9131))
1570 		dt = clamp(dt - 22, 0, 255);
1571 
1572 	return (dt * 400) / 10;
1573 }
1574 
1575 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
1576 {
1577 	int val, ret;
1578 
1579 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
1580 				    !(val & KSZ9x31_LMD_VCT_EN),
1581 				    30000, 100000, true);
1582 
1583 	return ret < 0 ? ret : 0;
1584 }
1585 
1586 static int ksz9x31_cable_test_get_pair(int pair)
1587 {
1588 	static const int ethtool_pair[] = {
1589 		ETHTOOL_A_CABLE_PAIR_A,
1590 		ETHTOOL_A_CABLE_PAIR_B,
1591 		ETHTOOL_A_CABLE_PAIR_C,
1592 		ETHTOOL_A_CABLE_PAIR_D,
1593 	};
1594 
1595 	return ethtool_pair[pair];
1596 }
1597 
1598 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
1599 {
1600 	int ret, val;
1601 
1602 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1603 	 * To test each individual cable pair, set the cable pair in the Cable
1604 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
1605 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
1606 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
1607 	 * will self clear when the test is concluded.
1608 	 */
1609 	ret = phy_write(phydev, KSZ9x31_LMD,
1610 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
1611 	if (ret)
1612 		return ret;
1613 
1614 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
1615 	if (ret)
1616 		return ret;
1617 
1618 	val = phy_read(phydev, KSZ9x31_LMD);
1619 	if (val < 0)
1620 		return val;
1621 
1622 	if (ksz9x31_cable_test_failed(val))
1623 		return -EAGAIN;
1624 
1625 	ret = ethnl_cable_test_result(phydev,
1626 				      ksz9x31_cable_test_get_pair(pair),
1627 				      ksz9x31_cable_test_result_trans(val));
1628 	if (ret)
1629 		return ret;
1630 
1631 	if (!ksz9x31_cable_test_fault_length_valid(val))
1632 		return 0;
1633 
1634 	return ethnl_cable_test_fault_length(phydev,
1635 					     ksz9x31_cable_test_get_pair(pair),
1636 					     ksz9x31_cable_test_fault_length(phydev, val));
1637 }
1638 
1639 static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
1640 					 bool *finished)
1641 {
1642 	struct kszphy_priv *priv = phydev->priv;
1643 	unsigned long pair_mask = 0xf;
1644 	int retries = 20;
1645 	int pair, ret, rv;
1646 
1647 	*finished = false;
1648 
1649 	/* Try harder if link partner is active */
1650 	while (pair_mask && retries--) {
1651 		for_each_set_bit(pair, &pair_mask, 4) {
1652 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
1653 			if (ret == -EAGAIN)
1654 				continue;
1655 			if (ret < 0)
1656 				return ret;
1657 			clear_bit(pair, &pair_mask);
1658 		}
1659 		/* If link partner is in autonegotiation mode it will send 2ms
1660 		 * of FLPs with at least 6ms of silence.
1661 		 * Add 2ms sleep to have better chances to hit this silence.
1662 		 */
1663 		if (pair_mask)
1664 			usleep_range(2000, 3000);
1665 	}
1666 
1667 	/* Report remaining unfinished pair result as unknown. */
1668 	for_each_set_bit(pair, &pair_mask, 4) {
1669 		ret = ethnl_cable_test_result(phydev,
1670 					      ksz9x31_cable_test_get_pair(pair),
1671 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
1672 	}
1673 
1674 	*finished = true;
1675 
1676 	/* Restore cached bits from before LinkMD got started. */
1677 	rv = phy_modify(phydev, MII_CTRL1000,
1678 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
1679 			priv->vct_ctrl1000);
1680 	if (rv)
1681 		return rv;
1682 
1683 	return ret;
1684 }
1685 
1686 static int ksz8873mll_config_aneg(struct phy_device *phydev)
1687 {
1688 	return 0;
1689 }
1690 
1691 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
1692 {
1693 	u16 val;
1694 
1695 	switch (ctrl) {
1696 	case ETH_TP_MDI:
1697 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
1698 		break;
1699 	case ETH_TP_MDI_X:
1700 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
1701 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
1702 		 * sheet seems to be missing:
1703 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
1704 		 * 0 = Normal operation (transmit on TX+/TX- pins)
1705 		 */
1706 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
1707 		break;
1708 	case ETH_TP_MDI_AUTO:
1709 		val = 0;
1710 		break;
1711 	default:
1712 		return 0;
1713 	}
1714 
1715 	return phy_modify(phydev, MII_BMCR,
1716 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
1717 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
1718 			  KSZ886X_BMCR_HP_MDIX | val);
1719 }
1720 
1721 static int ksz886x_config_aneg(struct phy_device *phydev)
1722 {
1723 	int ret;
1724 
1725 	ret = genphy_config_aneg(phydev);
1726 	if (ret)
1727 		return ret;
1728 
1729 	/* The MDI-X configuration is automatically changed by the PHY after
1730 	 * switching from autoneg off to on. So, take MDI-X configuration under
1731 	 * own control and set it after autoneg configuration was done.
1732 	 */
1733 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
1734 }
1735 
1736 static int ksz886x_mdix_update(struct phy_device *phydev)
1737 {
1738 	int ret;
1739 
1740 	ret = phy_read(phydev, MII_BMCR);
1741 	if (ret < 0)
1742 		return ret;
1743 
1744 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
1745 		if (ret & KSZ886X_BMCR_FORCE_MDI)
1746 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1747 		else
1748 			phydev->mdix_ctrl = ETH_TP_MDI;
1749 	} else {
1750 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1751 	}
1752 
1753 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
1754 	if (ret < 0)
1755 		return ret;
1756 
1757 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
1758 	if (ret & KSZ886X_CTRL_MDIX_STAT)
1759 		phydev->mdix = ETH_TP_MDI_X;
1760 	else
1761 		phydev->mdix = ETH_TP_MDI;
1762 
1763 	return 0;
1764 }
1765 
1766 static int ksz886x_read_status(struct phy_device *phydev)
1767 {
1768 	int ret;
1769 
1770 	ret = ksz886x_mdix_update(phydev);
1771 	if (ret < 0)
1772 		return ret;
1773 
1774 	return genphy_read_status(phydev);
1775 }
1776 
1777 struct ksz9477_errata_write {
1778 	u8 dev_addr;
1779 	u8 reg_addr;
1780 	u16 val;
1781 };
1782 
1783 static const struct ksz9477_errata_write ksz9477_errata_writes[] = {
1784 	 /* Register settings are needed to improve PHY receive performance */
1785 	{0x01, 0x6f, 0xdd0b},
1786 	{0x01, 0x8f, 0x6032},
1787 	{0x01, 0x9d, 0x248c},
1788 	{0x01, 0x75, 0x0060},
1789 	{0x01, 0xd3, 0x7777},
1790 	{0x1c, 0x06, 0x3008},
1791 	{0x1c, 0x08, 0x2000},
1792 
1793 	/* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
1794 	{0x1c, 0x04, 0x00d0},
1795 
1796 	/* Energy Efficient Ethernet (EEE) feature select must be manually disabled */
1797 	{0x07, 0x3c, 0x0000},
1798 
1799 	/* Register settings are required to meet data sheet supply current specifications */
1800 	{0x1c, 0x13, 0x6eff},
1801 	{0x1c, 0x14, 0xe6ff},
1802 	{0x1c, 0x15, 0x6eff},
1803 	{0x1c, 0x16, 0xe6ff},
1804 	{0x1c, 0x17, 0x00ff},
1805 	{0x1c, 0x18, 0x43ff},
1806 	{0x1c, 0x19, 0xc3ff},
1807 	{0x1c, 0x1a, 0x6fff},
1808 	{0x1c, 0x1b, 0x07ff},
1809 	{0x1c, 0x1c, 0x0fff},
1810 	{0x1c, 0x1d, 0xe7ff},
1811 	{0x1c, 0x1e, 0xefff},
1812 	{0x1c, 0x20, 0xeeee},
1813 };
1814 
1815 static int ksz9477_config_init(struct phy_device *phydev)
1816 {
1817 	int err;
1818 	int i;
1819 
1820 	/* Apply PHY settings to address errata listed in
1821 	 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1822 	 * Silicon Errata and Data Sheet Clarification documents.
1823 	 *
1824 	 * Document notes: Before configuring the PHY MMD registers, it is
1825 	 * necessary to set the PHY to 100 Mbps speed with auto-negotiation
1826 	 * disabled by writing to register 0xN100-0xN101. After writing the
1827 	 * MMD registers, and after all errata workarounds that involve PHY
1828 	 * register settings, write register 0xN100-0xN101 again to enable
1829 	 * and restart auto-negotiation.
1830 	 */
1831 	err = phy_write(phydev, MII_BMCR, BMCR_SPEED100 | BMCR_FULLDPLX);
1832 	if (err)
1833 		return err;
1834 
1835 	for (i = 0; i < ARRAY_SIZE(ksz9477_errata_writes); ++i) {
1836 		const struct ksz9477_errata_write *errata = &ksz9477_errata_writes[i];
1837 
1838 		err = phy_write_mmd(phydev, errata->dev_addr, errata->reg_addr, errata->val);
1839 		if (err)
1840 			return err;
1841 	}
1842 
1843 	err = genphy_restart_aneg(phydev);
1844 	if (err)
1845 		return err;
1846 
1847 	return kszphy_config_init(phydev);
1848 }
1849 
1850 static int kszphy_get_sset_count(struct phy_device *phydev)
1851 {
1852 	return ARRAY_SIZE(kszphy_hw_stats);
1853 }
1854 
1855 static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
1856 {
1857 	int i;
1858 
1859 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1860 		strscpy(data + i * ETH_GSTRING_LEN,
1861 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
1862 	}
1863 }
1864 
1865 static u64 kszphy_get_stat(struct phy_device *phydev, int i)
1866 {
1867 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
1868 	struct kszphy_priv *priv = phydev->priv;
1869 	int val;
1870 	u64 ret;
1871 
1872 	val = phy_read(phydev, stat.reg);
1873 	if (val < 0) {
1874 		ret = U64_MAX;
1875 	} else {
1876 		val = val & ((1 << stat.bits) - 1);
1877 		priv->stats[i] += val;
1878 		ret = priv->stats[i];
1879 	}
1880 
1881 	return ret;
1882 }
1883 
1884 static void kszphy_get_stats(struct phy_device *phydev,
1885 			     struct ethtool_stats *stats, u64 *data)
1886 {
1887 	int i;
1888 
1889 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
1890 		data[i] = kszphy_get_stat(phydev, i);
1891 }
1892 
1893 static int kszphy_suspend(struct phy_device *phydev)
1894 {
1895 	/* Disable PHY Interrupts */
1896 	if (phy_interrupt_is_valid(phydev)) {
1897 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1898 		if (phydev->drv->config_intr)
1899 			phydev->drv->config_intr(phydev);
1900 	}
1901 
1902 	return genphy_suspend(phydev);
1903 }
1904 
1905 static void kszphy_parse_led_mode(struct phy_device *phydev)
1906 {
1907 	const struct kszphy_type *type = phydev->drv->driver_data;
1908 	const struct device_node *np = phydev->mdio.dev.of_node;
1909 	struct kszphy_priv *priv = phydev->priv;
1910 	int ret;
1911 
1912 	if (type && type->led_mode_reg) {
1913 		ret = of_property_read_u32(np, "micrel,led-mode",
1914 					   &priv->led_mode);
1915 
1916 		if (ret)
1917 			priv->led_mode = -1;
1918 
1919 		if (priv->led_mode > 3) {
1920 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1921 				   priv->led_mode);
1922 			priv->led_mode = -1;
1923 		}
1924 	} else {
1925 		priv->led_mode = -1;
1926 	}
1927 }
1928 
1929 static int kszphy_resume(struct phy_device *phydev)
1930 {
1931 	int ret;
1932 
1933 	genphy_resume(phydev);
1934 
1935 	/* After switching from power-down to normal mode, an internal global
1936 	 * reset is automatically generated. Wait a minimum of 1 ms before
1937 	 * read/write access to the PHY registers.
1938 	 */
1939 	usleep_range(1000, 2000);
1940 
1941 	ret = kszphy_config_reset(phydev);
1942 	if (ret)
1943 		return ret;
1944 
1945 	/* Enable PHY Interrupts */
1946 	if (phy_interrupt_is_valid(phydev)) {
1947 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1948 		if (phydev->drv->config_intr)
1949 			phydev->drv->config_intr(phydev);
1950 	}
1951 
1952 	return 0;
1953 }
1954 
1955 static int kszphy_probe(struct phy_device *phydev)
1956 {
1957 	const struct kszphy_type *type = phydev->drv->driver_data;
1958 	const struct device_node *np = phydev->mdio.dev.of_node;
1959 	struct kszphy_priv *priv;
1960 	struct clk *clk;
1961 
1962 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1963 	if (!priv)
1964 		return -ENOMEM;
1965 
1966 	phydev->priv = priv;
1967 
1968 	priv->type = type;
1969 
1970 	kszphy_parse_led_mode(phydev);
1971 
1972 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1973 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1974 	if (!IS_ERR_OR_NULL(clk)) {
1975 		unsigned long rate = clk_get_rate(clk);
1976 		bool rmii_ref_clk_sel_25_mhz;
1977 
1978 		if (type)
1979 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
1980 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
1981 				"micrel,rmii-reference-clock-select-25-mhz");
1982 
1983 		if (rate > 24500000 && rate < 25500000) {
1984 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
1985 		} else if (rate > 49500000 && rate < 50500000) {
1986 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
1987 		} else {
1988 			phydev_err(phydev, "Clock rate out of range: %ld\n",
1989 				   rate);
1990 			return -EINVAL;
1991 		}
1992 	}
1993 
1994 	if (ksz8041_fiber_mode(phydev))
1995 		phydev->port = PORT_FIBRE;
1996 
1997 	/* Support legacy board-file configuration */
1998 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
1999 		priv->rmii_ref_clk_sel = true;
2000 		priv->rmii_ref_clk_sel_val = true;
2001 	}
2002 
2003 	return 0;
2004 }
2005 
2006 static int lan8814_cable_test_start(struct phy_device *phydev)
2007 {
2008 	/* If autoneg is enabled, we won't be able to test cross pair
2009 	 * short. In this case, the PHY will "detect" a link and
2010 	 * confuse the internal state machine - disable auto neg here.
2011 	 * Set the speed to 1000mbit and full duplex.
2012 	 */
2013 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
2014 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
2015 }
2016 
2017 static int ksz886x_cable_test_start(struct phy_device *phydev)
2018 {
2019 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
2020 		return -EOPNOTSUPP;
2021 
2022 	/* If autoneg is enabled, we won't be able to test cross pair
2023 	 * short. In this case, the PHY will "detect" a link and
2024 	 * confuse the internal state machine - disable auto neg here.
2025 	 * If autoneg is disabled, we should set the speed to 10mbit.
2026 	 */
2027 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
2028 }
2029 
2030 static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
2031 {
2032 	switch (FIELD_GET(mask, status)) {
2033 	case KSZ8081_LMD_STAT_NORMAL:
2034 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
2035 	case KSZ8081_LMD_STAT_SHORT:
2036 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
2037 	case KSZ8081_LMD_STAT_OPEN:
2038 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
2039 	case KSZ8081_LMD_STAT_FAIL:
2040 		fallthrough;
2041 	default:
2042 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
2043 	}
2044 }
2045 
2046 static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
2047 {
2048 	return FIELD_GET(mask, status) ==
2049 		KSZ8081_LMD_STAT_FAIL;
2050 }
2051 
2052 static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
2053 {
2054 	switch (FIELD_GET(mask, status)) {
2055 	case KSZ8081_LMD_STAT_OPEN:
2056 		fallthrough;
2057 	case KSZ8081_LMD_STAT_SHORT:
2058 		return true;
2059 	}
2060 	return false;
2061 }
2062 
2063 static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
2064 							   u16 status, u16 data_mask)
2065 {
2066 	int dt;
2067 
2068 	/* According to the data sheet the distance to the fault is
2069 	 * DELTA_TIME * 0.4 meters for ksz phys.
2070 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
2071 	 */
2072 	dt = FIELD_GET(data_mask, status);
2073 
2074 	if (phydev_id_compare(phydev, PHY_ID_LAN8814))
2075 		return ((dt - 22) * 800) / 10;
2076 	else
2077 		return (dt * 400) / 10;
2078 }
2079 
2080 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
2081 {
2082 	const struct kszphy_type *type = phydev->drv->driver_data;
2083 	int val, ret;
2084 
2085 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
2086 				    !(val & KSZ8081_LMD_ENABLE_TEST),
2087 				    30000, 100000, true);
2088 
2089 	return ret < 0 ? ret : 0;
2090 }
2091 
2092 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
2093 {
2094 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
2095 					    ETHTOOL_A_CABLE_PAIR_B,
2096 					    ETHTOOL_A_CABLE_PAIR_C,
2097 					    ETHTOOL_A_CABLE_PAIR_D,
2098 					  };
2099 	u32 fault_length;
2100 	int ret;
2101 	int val;
2102 
2103 	val = KSZ8081_LMD_ENABLE_TEST;
2104 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
2105 
2106 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
2107 	if (ret < 0)
2108 		return ret;
2109 
2110 	ret = ksz886x_cable_test_wait_for_completion(phydev);
2111 	if (ret)
2112 		return ret;
2113 
2114 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
2115 	if (val < 0)
2116 		return val;
2117 
2118 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
2119 		return -EAGAIN;
2120 
2121 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
2122 				      ksz886x_cable_test_result_trans(val,
2123 								      LAN8814_CABLE_DIAG_STAT_MASK
2124 								      ));
2125 	if (ret)
2126 		return ret;
2127 
2128 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
2129 		return 0;
2130 
2131 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
2132 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
2133 
2134 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
2135 }
2136 
2137 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
2138 {
2139 	static const int ethtool_pair[] = {
2140 		ETHTOOL_A_CABLE_PAIR_A,
2141 		ETHTOOL_A_CABLE_PAIR_B,
2142 	};
2143 	int ret, val, mdix;
2144 	u32 fault_length;
2145 
2146 	/* There is no way to choice the pair, like we do one ksz9031.
2147 	 * We can workaround this limitation by using the MDI-X functionality.
2148 	 */
2149 	if (pair == 0)
2150 		mdix = ETH_TP_MDI;
2151 	else
2152 		mdix = ETH_TP_MDI_X;
2153 
2154 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
2155 	case PHY_ID_KSZ8081:
2156 		ret = ksz8081_config_mdix(phydev, mdix);
2157 		break;
2158 	case PHY_ID_KSZ886X:
2159 		ret = ksz886x_config_mdix(phydev, mdix);
2160 		break;
2161 	default:
2162 		ret = -ENODEV;
2163 	}
2164 
2165 	if (ret)
2166 		return ret;
2167 
2168 	/* Now we are ready to fire. This command will send a 100ns pulse
2169 	 * to the pair.
2170 	 */
2171 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
2172 	if (ret)
2173 		return ret;
2174 
2175 	ret = ksz886x_cable_test_wait_for_completion(phydev);
2176 	if (ret)
2177 		return ret;
2178 
2179 	val = phy_read(phydev, KSZ8081_LMD);
2180 	if (val < 0)
2181 		return val;
2182 
2183 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
2184 		return -EAGAIN;
2185 
2186 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
2187 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
2188 	if (ret)
2189 		return ret;
2190 
2191 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
2192 		return 0;
2193 
2194 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
2195 
2196 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
2197 }
2198 
2199 static int ksz886x_cable_test_get_status(struct phy_device *phydev,
2200 					 bool *finished)
2201 {
2202 	const struct kszphy_type *type = phydev->drv->driver_data;
2203 	unsigned long pair_mask = type->pair_mask;
2204 	int retries = 20;
2205 	int ret = 0;
2206 	int pair;
2207 
2208 	*finished = false;
2209 
2210 	/* Try harder if link partner is active */
2211 	while (pair_mask && retries--) {
2212 		for_each_set_bit(pair, &pair_mask, 4) {
2213 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
2214 				ret = lan8814_cable_test_one_pair(phydev, pair);
2215 			else
2216 				ret = ksz886x_cable_test_one_pair(phydev, pair);
2217 			if (ret == -EAGAIN)
2218 				continue;
2219 			if (ret < 0)
2220 				return ret;
2221 			clear_bit(pair, &pair_mask);
2222 		}
2223 		/* If link partner is in autonegotiation mode it will send 2ms
2224 		 * of FLPs with at least 6ms of silence.
2225 		 * Add 2ms sleep to have better chances to hit this silence.
2226 		 */
2227 		if (pair_mask)
2228 			msleep(2);
2229 	}
2230 
2231 	*finished = true;
2232 
2233 	return ret;
2234 }
2235 
2236 #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
2237 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
2238 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
2239 
2240 #define LAN8814_QSGMII_SOFT_RESET			0x43
2241 #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
2242 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
2243 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
2244 #define LAN8814_ALIGN_SWAP				0x4a
2245 #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
2246 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
2247 
2248 #define LAN8804_ALIGN_SWAP				0x4a
2249 #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
2250 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
2251 #define LAN8814_CLOCK_MANAGEMENT			0xd
2252 #define LAN8814_LINK_QUALITY				0x8e
2253 
2254 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
2255 {
2256 	int data;
2257 
2258 	phy_lock_mdio_bus(phydev);
2259 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
2260 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
2261 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
2262 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
2263 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
2264 	phy_unlock_mdio_bus(phydev);
2265 
2266 	return data;
2267 }
2268 
2269 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
2270 				 u16 val)
2271 {
2272 	phy_lock_mdio_bus(phydev);
2273 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
2274 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
2275 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
2276 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
2277 
2278 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
2279 	if (val != 0)
2280 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
2281 			   val);
2282 	phy_unlock_mdio_bus(phydev);
2283 	return val;
2284 }
2285 
2286 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
2287 {
2288 	u16 val = 0;
2289 
2290 	if (enable)
2291 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2292 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2293 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2294 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
2295 
2296 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2297 }
2298 
2299 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2300 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2301 {
2302 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2303 	*seconds = (*seconds << 16) |
2304 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2305 
2306 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2307 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2308 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2309 
2310 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2311 }
2312 
2313 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2314 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2315 {
2316 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2317 	*seconds = *seconds << 16 |
2318 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2319 
2320 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2321 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2322 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2323 
2324 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2325 }
2326 
2327 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2328 {
2329 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2330 	struct phy_device *phydev = ptp_priv->phydev;
2331 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2332 
2333 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2334 				SOF_TIMESTAMPING_RX_HARDWARE |
2335 				SOF_TIMESTAMPING_RAW_HARDWARE;
2336 
2337 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2338 
2339 	info->tx_types =
2340 		(1 << HWTSTAMP_TX_OFF) |
2341 		(1 << HWTSTAMP_TX_ON) |
2342 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2343 
2344 	info->rx_filters =
2345 		(1 << HWTSTAMP_FILTER_NONE) |
2346 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2347 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2348 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2349 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2350 
2351 	return 0;
2352 }
2353 
2354 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2355 {
2356 	int i;
2357 
2358 	for (i = 0; i < FIFO_SIZE; ++i)
2359 		lanphy_read_page_reg(phydev, 5,
2360 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2361 
2362 	/* Read to clear overflow status bit */
2363 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2364 }
2365 
2366 static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2367 {
2368 	struct kszphy_ptp_priv *ptp_priv =
2369 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2370 	struct phy_device *phydev = ptp_priv->phydev;
2371 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2372 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2373 	struct hwtstamp_config config;
2374 	int txcfg = 0, rxcfg = 0;
2375 	int pkt_ts_enable;
2376 
2377 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2378 		return -EFAULT;
2379 
2380 	ptp_priv->hwts_tx_type = config.tx_type;
2381 	ptp_priv->rx_filter = config.rx_filter;
2382 
2383 	switch (config.rx_filter) {
2384 	case HWTSTAMP_FILTER_NONE:
2385 		ptp_priv->layer = 0;
2386 		ptp_priv->version = 0;
2387 		break;
2388 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2389 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2390 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2391 		ptp_priv->layer = PTP_CLASS_L4;
2392 		ptp_priv->version = PTP_CLASS_V2;
2393 		break;
2394 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2395 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2396 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2397 		ptp_priv->layer = PTP_CLASS_L2;
2398 		ptp_priv->version = PTP_CLASS_V2;
2399 		break;
2400 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2401 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2402 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2403 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2404 		ptp_priv->version = PTP_CLASS_V2;
2405 		break;
2406 	default:
2407 		return -ERANGE;
2408 	}
2409 
2410 	if (ptp_priv->layer & PTP_CLASS_L2) {
2411 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2412 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2413 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2414 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2415 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2416 	}
2417 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2418 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2419 
2420 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2421 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2422 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2423 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2424 
2425 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2426 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2427 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2428 
2429 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2430 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2431 	else
2432 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2433 
2434 	mutex_lock(&shared->shared_lock);
2435 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2436 		shared->ref++;
2437 	else
2438 		shared->ref--;
2439 
2440 	if (shared->ref)
2441 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2442 				      PTP_CMD_CTL_PTP_ENABLE_);
2443 	else
2444 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2445 				      PTP_CMD_CTL_PTP_DISABLE_);
2446 	mutex_unlock(&shared->shared_lock);
2447 
2448 	/* In case of multiple starts and stops, these needs to be cleared */
2449 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2450 		list_del(&rx_ts->list);
2451 		kfree(rx_ts);
2452 	}
2453 	skb_queue_purge(&ptp_priv->rx_queue);
2454 	skb_queue_purge(&ptp_priv->tx_queue);
2455 
2456 	lan8814_flush_fifo(ptp_priv->phydev, false);
2457 	lan8814_flush_fifo(ptp_priv->phydev, true);
2458 
2459 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2460 }
2461 
2462 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2463 			     struct sk_buff *skb, int type)
2464 {
2465 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2466 
2467 	switch (ptp_priv->hwts_tx_type) {
2468 	case HWTSTAMP_TX_ONESTEP_SYNC:
2469 		if (ptp_msg_is_sync(skb, type)) {
2470 			kfree_skb(skb);
2471 			return;
2472 		}
2473 		fallthrough;
2474 	case HWTSTAMP_TX_ON:
2475 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2476 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2477 		break;
2478 	case HWTSTAMP_TX_OFF:
2479 	default:
2480 		kfree_skb(skb);
2481 		break;
2482 	}
2483 }
2484 
2485 static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2486 {
2487 	struct ptp_header *ptp_header;
2488 	u32 type;
2489 
2490 	skb_push(skb, ETH_HLEN);
2491 	type = ptp_classify_raw(skb);
2492 	ptp_header = ptp_parse_header(skb, type);
2493 	skb_pull_inline(skb, ETH_HLEN);
2494 
2495 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2496 }
2497 
2498 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
2499 				 struct sk_buff *skb)
2500 {
2501 	struct skb_shared_hwtstamps *shhwtstamps;
2502 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2503 	unsigned long flags;
2504 	bool ret = false;
2505 	u16 skb_sig;
2506 
2507 	lan8814_get_sig_rx(skb, &skb_sig);
2508 
2509 	/* Iterate over all RX timestamps and match it with the received skbs */
2510 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2511 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2512 		/* Check if we found the signature we were looking for. */
2513 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2514 			continue;
2515 
2516 		shhwtstamps = skb_hwtstamps(skb);
2517 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2518 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2519 						  rx_ts->nsec);
2520 		list_del(&rx_ts->list);
2521 		kfree(rx_ts);
2522 
2523 		ret = true;
2524 		break;
2525 	}
2526 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2527 
2528 	if (ret)
2529 		netif_rx(skb);
2530 	return ret;
2531 }
2532 
2533 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2534 {
2535 	struct kszphy_ptp_priv *ptp_priv =
2536 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2537 
2538 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2539 	    type == PTP_CLASS_NONE)
2540 		return false;
2541 
2542 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2543 		return false;
2544 
2545 	/* If we failed to match then add it to the queue for when the timestamp
2546 	 * will come
2547 	 */
2548 	if (!lan8814_match_rx_skb(ptp_priv, skb))
2549 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2550 
2551 	return true;
2552 }
2553 
2554 static void lan8814_ptp_clock_set(struct phy_device *phydev,
2555 				  u32 seconds, u32 nano_seconds)
2556 {
2557 	u32 sec_low, sec_high, nsec_low, nsec_high;
2558 
2559 	sec_low = seconds & 0xffff;
2560 	sec_high = (seconds >> 16) & 0xffff;
2561 	nsec_low = nano_seconds & 0xffff;
2562 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2563 
2564 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2565 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2566 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2567 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2568 
2569 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2570 }
2571 
2572 static void lan8814_ptp_clock_get(struct phy_device *phydev,
2573 				  u32 *seconds, u32 *nano_seconds)
2574 {
2575 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2576 
2577 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2578 	*seconds = (*seconds << 16) |
2579 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2580 
2581 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2582 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2583 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2584 }
2585 
2586 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2587 				   struct timespec64 *ts)
2588 {
2589 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2590 							  ptp_clock_info);
2591 	struct phy_device *phydev = shared->phydev;
2592 	u32 nano_seconds;
2593 	u32 seconds;
2594 
2595 	mutex_lock(&shared->shared_lock);
2596 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2597 	mutex_unlock(&shared->shared_lock);
2598 	ts->tv_sec = seconds;
2599 	ts->tv_nsec = nano_seconds;
2600 
2601 	return 0;
2602 }
2603 
2604 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2605 				   const struct timespec64 *ts)
2606 {
2607 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2608 							  ptp_clock_info);
2609 	struct phy_device *phydev = shared->phydev;
2610 
2611 	mutex_lock(&shared->shared_lock);
2612 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2613 	mutex_unlock(&shared->shared_lock);
2614 
2615 	return 0;
2616 }
2617 
2618 static void lan8814_ptp_clock_step(struct phy_device *phydev,
2619 				   s64 time_step_ns)
2620 {
2621 	u32 nano_seconds_step;
2622 	u64 abs_time_step_ns;
2623 	u32 unsigned_seconds;
2624 	u32 nano_seconds;
2625 	u32 remainder;
2626 	s32 seconds;
2627 
2628 	if (time_step_ns >  15000000000LL) {
2629 		/* convert to clock set */
2630 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2631 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2632 						&remainder);
2633 		nano_seconds += remainder;
2634 		if (nano_seconds >= 1000000000) {
2635 			unsigned_seconds++;
2636 			nano_seconds -= 1000000000;
2637 		}
2638 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2639 		return;
2640 	} else if (time_step_ns < -15000000000LL) {
2641 		/* convert to clock set */
2642 		time_step_ns = -time_step_ns;
2643 
2644 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2645 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2646 						&remainder);
2647 		nano_seconds_step = remainder;
2648 		if (nano_seconds < nano_seconds_step) {
2649 			unsigned_seconds--;
2650 			nano_seconds += 1000000000;
2651 		}
2652 		nano_seconds -= nano_seconds_step;
2653 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2654 				      nano_seconds);
2655 		return;
2656 	}
2657 
2658 	/* do clock step */
2659 	if (time_step_ns >= 0) {
2660 		abs_time_step_ns = (u64)time_step_ns;
2661 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2662 					   &remainder);
2663 		nano_seconds = remainder;
2664 	} else {
2665 		abs_time_step_ns = (u64)(-time_step_ns);
2666 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2667 			    &remainder));
2668 		nano_seconds = remainder;
2669 		if (nano_seconds > 0) {
2670 			/* subtracting nano seconds is not allowed
2671 			 * convert to subtracting from seconds,
2672 			 * and adding to nanoseconds
2673 			 */
2674 			seconds--;
2675 			nano_seconds = (1000000000 - nano_seconds);
2676 		}
2677 	}
2678 
2679 	if (nano_seconds > 0) {
2680 		/* add 8 ns to cover the likely normal increment */
2681 		nano_seconds += 8;
2682 	}
2683 
2684 	if (nano_seconds >= 1000000000) {
2685 		/* carry into seconds */
2686 		seconds++;
2687 		nano_seconds -= 1000000000;
2688 	}
2689 
2690 	while (seconds) {
2691 		if (seconds > 0) {
2692 			u32 adjustment_value = (u32)seconds;
2693 			u16 adjustment_value_lo, adjustment_value_hi;
2694 
2695 			if (adjustment_value > 0xF)
2696 				adjustment_value = 0xF;
2697 
2698 			adjustment_value_lo = adjustment_value & 0xffff;
2699 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2700 
2701 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2702 					      adjustment_value_lo);
2703 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2704 					      PTP_LTC_STEP_ADJ_DIR_ |
2705 					      adjustment_value_hi);
2706 			seconds -= ((s32)adjustment_value);
2707 		} else {
2708 			u32 adjustment_value = (u32)(-seconds);
2709 			u16 adjustment_value_lo, adjustment_value_hi;
2710 
2711 			if (adjustment_value > 0xF)
2712 				adjustment_value = 0xF;
2713 
2714 			adjustment_value_lo = adjustment_value & 0xffff;
2715 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2716 
2717 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2718 					      adjustment_value_lo);
2719 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2720 					      adjustment_value_hi);
2721 			seconds += ((s32)adjustment_value);
2722 		}
2723 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2724 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2725 	}
2726 	if (nano_seconds) {
2727 		u16 nano_seconds_lo;
2728 		u16 nano_seconds_hi;
2729 
2730 		nano_seconds_lo = nano_seconds & 0xffff;
2731 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2732 
2733 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2734 				      nano_seconds_lo);
2735 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2736 				      PTP_LTC_STEP_ADJ_DIR_ |
2737 				      nano_seconds_hi);
2738 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2739 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2740 	}
2741 }
2742 
2743 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2744 {
2745 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2746 							  ptp_clock_info);
2747 	struct phy_device *phydev = shared->phydev;
2748 
2749 	mutex_lock(&shared->shared_lock);
2750 	lan8814_ptp_clock_step(phydev, delta);
2751 	mutex_unlock(&shared->shared_lock);
2752 
2753 	return 0;
2754 }
2755 
2756 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2757 {
2758 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2759 							  ptp_clock_info);
2760 	struct phy_device *phydev = shared->phydev;
2761 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2762 	bool positive = true;
2763 	u32 kszphy_rate_adj;
2764 
2765 	if (scaled_ppm < 0) {
2766 		scaled_ppm = -scaled_ppm;
2767 		positive = false;
2768 	}
2769 
2770 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2771 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2772 
2773 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2774 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2775 
2776 	if (positive)
2777 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2778 
2779 	mutex_lock(&shared->shared_lock);
2780 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2781 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2782 	mutex_unlock(&shared->shared_lock);
2783 
2784 	return 0;
2785 }
2786 
2787 static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2788 {
2789 	struct ptp_header *ptp_header;
2790 	u32 type;
2791 
2792 	type = ptp_classify_raw(skb);
2793 	ptp_header = ptp_parse_header(skb, type);
2794 
2795 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2796 }
2797 
2798 static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
2799 				 u32 seconds, u32 nsec, u16 seq_id)
2800 {
2801 	struct skb_shared_hwtstamps shhwtstamps;
2802 	struct sk_buff *skb, *skb_tmp;
2803 	unsigned long flags;
2804 	bool ret = false;
2805 	u16 skb_sig;
2806 
2807 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2808 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2809 		lan8814_get_sig_tx(skb, &skb_sig);
2810 
2811 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2812 			continue;
2813 
2814 		__skb_unlink(skb, &ptp_priv->tx_queue);
2815 		ret = true;
2816 		break;
2817 	}
2818 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2819 
2820 	if (ret) {
2821 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2822 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2823 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2824 	}
2825 }
2826 
2827 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2828 {
2829 	struct phy_device *phydev = ptp_priv->phydev;
2830 	u32 seconds, nsec;
2831 	u16 seq_id;
2832 
2833 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2834 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
2835 }
2836 
2837 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2838 {
2839 	struct phy_device *phydev = ptp_priv->phydev;
2840 	u32 reg;
2841 
2842 	do {
2843 		lan8814_dequeue_tx_skb(ptp_priv);
2844 
2845 		/* If other timestamps are available in the FIFO,
2846 		 * process them.
2847 		 */
2848 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2849 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2850 }
2851 
2852 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2853 			      struct lan8814_ptp_rx_ts *rx_ts)
2854 {
2855 	struct skb_shared_hwtstamps *shhwtstamps;
2856 	struct sk_buff *skb, *skb_tmp;
2857 	unsigned long flags;
2858 	bool ret = false;
2859 	u16 skb_sig;
2860 
2861 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2862 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2863 		lan8814_get_sig_rx(skb, &skb_sig);
2864 
2865 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2866 			continue;
2867 
2868 		__skb_unlink(skb, &ptp_priv->rx_queue);
2869 
2870 		ret = true;
2871 		break;
2872 	}
2873 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2874 
2875 	if (ret) {
2876 		shhwtstamps = skb_hwtstamps(skb);
2877 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2878 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2879 		netif_rx(skb);
2880 	}
2881 
2882 	return ret;
2883 }
2884 
2885 static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2886 				struct lan8814_ptp_rx_ts *rx_ts)
2887 {
2888 	unsigned long flags;
2889 
2890 	/* If we failed to match the skb add it to the queue for when
2891 	 * the frame will come
2892 	 */
2893 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2894 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2895 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2896 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2897 	} else {
2898 		kfree(rx_ts);
2899 	}
2900 }
2901 
2902 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2903 {
2904 	struct phy_device *phydev = ptp_priv->phydev;
2905 	struct lan8814_ptp_rx_ts *rx_ts;
2906 	u32 reg;
2907 
2908 	do {
2909 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2910 		if (!rx_ts)
2911 			return;
2912 
2913 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2914 				      &rx_ts->seq_id);
2915 		lan8814_match_rx_ts(ptp_priv, rx_ts);
2916 
2917 		/* If other timestamps are available in the FIFO,
2918 		 * process them.
2919 		 */
2920 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2921 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2922 }
2923 
2924 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
2925 {
2926 	struct kszphy_priv *priv = phydev->priv;
2927 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2928 
2929 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2930 		lan8814_get_tx_ts(ptp_priv);
2931 
2932 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2933 		lan8814_get_rx_ts(ptp_priv);
2934 
2935 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2936 		lan8814_flush_fifo(phydev, true);
2937 		skb_queue_purge(&ptp_priv->tx_queue);
2938 	}
2939 
2940 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2941 		lan8814_flush_fifo(phydev, false);
2942 		skb_queue_purge(&ptp_priv->rx_queue);
2943 	}
2944 }
2945 
2946 static int lan8804_config_init(struct phy_device *phydev)
2947 {
2948 	int val;
2949 
2950 	/* MDI-X setting for swap A,B transmit */
2951 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
2952 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
2953 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
2954 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
2955 
2956 	/* Make sure that the PHY will not stop generating the clock when the
2957 	 * link partner goes down
2958 	 */
2959 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
2960 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
2961 
2962 	return 0;
2963 }
2964 
2965 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
2966 {
2967 	int status;
2968 
2969 	status = phy_read(phydev, LAN8814_INTS);
2970 	if (status < 0) {
2971 		phy_error(phydev);
2972 		return IRQ_NONE;
2973 	}
2974 
2975 	if (status > 0)
2976 		phy_trigger_machine(phydev);
2977 
2978 	return IRQ_HANDLED;
2979 }
2980 
2981 #define LAN8804_OUTPUT_CONTROL			25
2982 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
2983 #define LAN8804_CONTROL				31
2984 #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
2985 
2986 static int lan8804_config_intr(struct phy_device *phydev)
2987 {
2988 	int err;
2989 
2990 	/* This is an internal PHY of lan966x and is not possible to change the
2991 	 * polarity on the GIC found in lan966x, therefore change the polarity
2992 	 * of the interrupt in the PHY from being active low instead of active
2993 	 * high.
2994 	 */
2995 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
2996 
2997 	/* By default interrupt buffer is open-drain in which case the interrupt
2998 	 * can be active only low. Therefore change the interrupt buffer to be
2999 	 * push-pull to be able to change interrupt polarity
3000 	 */
3001 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
3002 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
3003 
3004 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3005 		err = phy_read(phydev, LAN8814_INTS);
3006 		if (err < 0)
3007 			return err;
3008 
3009 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3010 		if (err)
3011 			return err;
3012 	} else {
3013 		err = phy_write(phydev, LAN8814_INTC, 0);
3014 		if (err)
3015 			return err;
3016 
3017 		err = phy_read(phydev, LAN8814_INTS);
3018 		if (err < 0)
3019 			return err;
3020 	}
3021 
3022 	return 0;
3023 }
3024 
3025 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
3026 {
3027 	int ret = IRQ_NONE;
3028 	int irq_status;
3029 
3030 	irq_status = phy_read(phydev, LAN8814_INTS);
3031 	if (irq_status < 0) {
3032 		phy_error(phydev);
3033 		return IRQ_NONE;
3034 	}
3035 
3036 	if (irq_status & LAN8814_INT_LINK) {
3037 		phy_trigger_machine(phydev);
3038 		ret = IRQ_HANDLED;
3039 	}
3040 
3041 	while (true) {
3042 		irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
3043 		if (!irq_status)
3044 			break;
3045 
3046 		lan8814_handle_ptp_interrupt(phydev, irq_status);
3047 		ret = IRQ_HANDLED;
3048 	}
3049 
3050 	return ret;
3051 }
3052 
3053 static int lan8814_ack_interrupt(struct phy_device *phydev)
3054 {
3055 	/* bit[12..0] int status, which is a read and clear register. */
3056 	int rc;
3057 
3058 	rc = phy_read(phydev, LAN8814_INTS);
3059 
3060 	return (rc < 0) ? rc : 0;
3061 }
3062 
3063 static int lan8814_config_intr(struct phy_device *phydev)
3064 {
3065 	int err;
3066 
3067 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
3068 			      LAN8814_INTR_CTRL_REG_POLARITY |
3069 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
3070 
3071 	/* enable / disable interrupts */
3072 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3073 		err = lan8814_ack_interrupt(phydev);
3074 		if (err)
3075 			return err;
3076 
3077 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3078 	} else {
3079 		err = phy_write(phydev, LAN8814_INTC, 0);
3080 		if (err)
3081 			return err;
3082 
3083 		err = lan8814_ack_interrupt(phydev);
3084 	}
3085 
3086 	return err;
3087 }
3088 
3089 static void lan8814_ptp_init(struct phy_device *phydev)
3090 {
3091 	struct kszphy_priv *priv = phydev->priv;
3092 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3093 	u32 temp;
3094 
3095 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
3096 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
3097 		return;
3098 
3099 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
3100 
3101 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
3102 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3103 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
3104 
3105 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
3106 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3107 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
3108 
3109 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
3110 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
3111 
3112 	/* Removing default registers configs related to L2 and IP */
3113 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
3114 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
3115 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
3116 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
3117 
3118 	skb_queue_head_init(&ptp_priv->tx_queue);
3119 	skb_queue_head_init(&ptp_priv->rx_queue);
3120 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
3121 	spin_lock_init(&ptp_priv->rx_ts_lock);
3122 
3123 	ptp_priv->phydev = phydev;
3124 
3125 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
3126 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
3127 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
3128 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
3129 
3130 	phydev->mii_ts = &ptp_priv->mii_ts;
3131 }
3132 
3133 static int lan8814_ptp_probe_once(struct phy_device *phydev)
3134 {
3135 	struct lan8814_shared_priv *shared = phydev->shared->priv;
3136 
3137 	/* Initialise shared lock for clock*/
3138 	mutex_init(&shared->shared_lock);
3139 
3140 	shared->ptp_clock_info.owner = THIS_MODULE;
3141 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
3142 	shared->ptp_clock_info.max_adj = 31249999;
3143 	shared->ptp_clock_info.n_alarm = 0;
3144 	shared->ptp_clock_info.n_ext_ts = 0;
3145 	shared->ptp_clock_info.n_pins = 0;
3146 	shared->ptp_clock_info.pps = 0;
3147 	shared->ptp_clock_info.pin_config = NULL;
3148 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
3149 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
3150 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
3151 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
3152 	shared->ptp_clock_info.getcrosststamp = NULL;
3153 
3154 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
3155 					       &phydev->mdio.dev);
3156 	if (IS_ERR(shared->ptp_clock)) {
3157 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
3158 			   PTR_ERR(shared->ptp_clock));
3159 		return -EINVAL;
3160 	}
3161 
3162 	/* Check if PHC support is missing at the configuration level */
3163 	if (!shared->ptp_clock)
3164 		return 0;
3165 
3166 	phydev_dbg(phydev, "successfully registered ptp clock\n");
3167 
3168 	shared->phydev = phydev;
3169 
3170 	/* The EP.4 is shared between all the PHYs in the package and also it
3171 	 * can be accessed by any of the PHYs
3172 	 */
3173 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
3174 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
3175 			      PTP_OPERATING_MODE_STANDALONE_);
3176 
3177 	return 0;
3178 }
3179 
3180 static void lan8814_setup_led(struct phy_device *phydev, int val)
3181 {
3182 	int temp;
3183 
3184 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
3185 
3186 	if (val)
3187 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3188 	else
3189 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3190 
3191 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
3192 }
3193 
3194 static int lan8814_config_init(struct phy_device *phydev)
3195 {
3196 	struct kszphy_priv *lan8814 = phydev->priv;
3197 	int val;
3198 
3199 	/* Reset the PHY */
3200 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3201 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3202 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3203 
3204 	/* Disable ANEG with QSGMII PCS Host side */
3205 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3206 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3207 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3208 
3209 	/* MDI-X setting for swap A,B transmit */
3210 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3211 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3212 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3213 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3214 
3215 	if (lan8814->led_mode >= 0)
3216 		lan8814_setup_led(phydev, lan8814->led_mode);
3217 
3218 	return 0;
3219 }
3220 
3221 /* It is expected that there will not be any 'lan8814_take_coma_mode'
3222  * function called in suspend. Because the GPIO line can be shared, so if one of
3223  * the phys goes back in coma mode, then all the other PHYs will go, which is
3224  * wrong.
3225  */
3226 static int lan8814_release_coma_mode(struct phy_device *phydev)
3227 {
3228 	struct gpio_desc *gpiod;
3229 
3230 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
3231 					GPIOD_OUT_HIGH_OPEN_DRAIN |
3232 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3233 	if (IS_ERR(gpiod))
3234 		return PTR_ERR(gpiod);
3235 
3236 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3237 	gpiod_set_value_cansleep(gpiod, 0);
3238 
3239 	return 0;
3240 }
3241 
3242 static int lan8814_probe(struct phy_device *phydev)
3243 {
3244 	const struct kszphy_type *type = phydev->drv->driver_data;
3245 	struct kszphy_priv *priv;
3246 	u16 addr;
3247 	int err;
3248 
3249 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3250 	if (!priv)
3251 		return -ENOMEM;
3252 
3253 	phydev->priv = priv;
3254 
3255 	priv->type = type;
3256 
3257 	kszphy_parse_led_mode(phydev);
3258 
3259 	/* Strap-in value for PHY address, below register read gives starting
3260 	 * phy address value
3261 	 */
3262 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3263 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3264 			      addr, sizeof(struct lan8814_shared_priv));
3265 
3266 	if (phy_package_init_once(phydev)) {
3267 		err = lan8814_release_coma_mode(phydev);
3268 		if (err)
3269 			return err;
3270 
3271 		err = lan8814_ptp_probe_once(phydev);
3272 		if (err)
3273 			return err;
3274 	}
3275 
3276 	lan8814_ptp_init(phydev);
3277 
3278 	return 0;
3279 }
3280 
3281 #define LAN8841_MMD_TIMER_REG			0
3282 #define LAN8841_MMD0_REGISTER_17		17
3283 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
3284 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
3285 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
3286 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
3287 #define LAN8841_MMD_ANALOG_REG			28
3288 #define LAN8841_ANALOG_CONTROL_1		1
3289 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
3290 #define LAN8841_ANALOG_CONTROL_10		13
3291 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
3292 #define LAN8841_ANALOG_CONTROL_11		14
3293 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
3294 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
3295 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
3296 #define LAN8841_BTRX_POWER_DOWN			70
3297 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
3298 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
3299 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
3300 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
3301 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
3302 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
3303 #define LAN8841_ADC_CHANNEL_MASK		198
3304 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
3305 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
3306 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
3307 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
3308 #define LAN8841_PTP_CMD_CTL			256
3309 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
3310 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
3311 #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
3312 #define LAN8841_PTP_RX_PARSE_CONFIG		368
3313 #define LAN8841_PTP_TX_PARSE_CONFIG		432
3314 
3315 static int lan8841_config_init(struct phy_device *phydev)
3316 {
3317 	int ret;
3318 
3319 	ret = ksz9131_config_init(phydev);
3320 	if (ret)
3321 		return ret;
3322 
3323 	/* Initialize the HW by resetting everything */
3324 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3325 		       LAN8841_PTP_CMD_CTL,
3326 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
3327 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
3328 
3329 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3330 		       LAN8841_PTP_CMD_CTL,
3331 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
3332 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
3333 
3334 	/* Don't process any frames */
3335 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3336 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
3337 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3338 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
3339 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3340 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
3341 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3342 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
3343 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3344 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
3345 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3346 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
3347 
3348 	/* 100BT Clause 40 improvenent errata */
3349 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3350 		      LAN8841_ANALOG_CONTROL_1,
3351 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
3352 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3353 		      LAN8841_ANALOG_CONTROL_10,
3354 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
3355 
3356 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
3357 	 * Magnetics
3358 	 */
3359 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3360 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
3361 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
3362 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3363 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
3364 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
3365 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3366 			      LAN8841_BTRX_POWER_DOWN,
3367 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
3368 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
3369 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
3370 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
3371 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
3372 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
3373 	}
3374 
3375 	/* LDO Adjustment errata */
3376 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3377 		      LAN8841_ANALOG_CONTROL_11,
3378 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
3379 
3380 	/* 100BT RGMII latency tuning errata */
3381 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
3382 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
3383 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
3384 		      LAN8841_MMD0_REGISTER_17,
3385 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
3386 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
3387 
3388 	return 0;
3389 }
3390 
3391 #define LAN8841_OUTPUT_CTRL			25
3392 #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
3393 #define LAN8841_INT_PTP				BIT(9)
3394 
3395 static int lan8841_config_intr(struct phy_device *phydev)
3396 {
3397 	int err;
3398 
3399 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
3400 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
3401 
3402 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3403 		err = phy_read(phydev, LAN8814_INTS);
3404 		if (err)
3405 			return err;
3406 
3407 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
3408 		 * even if it PTP is not enabled. Because the underneath blocks
3409 		 * will not enable the PTP so we will never get the PTP
3410 		 * interrupt.
3411 		 */
3412 		err = phy_write(phydev, LAN8814_INTC,
3413 				LAN8814_INT_LINK | LAN8841_INT_PTP);
3414 	} else {
3415 		err = phy_write(phydev, LAN8814_INTC, 0);
3416 		if (err)
3417 			return err;
3418 
3419 		err = phy_read(phydev, LAN8814_INTS);
3420 	}
3421 
3422 	return err;
3423 }
3424 
3425 #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
3426 #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
3427 #define LAN8841_PTP_TX_EGRESS_NS_LO			451
3428 #define LAN8841_PTP_TX_EGRESS_NS_HI			450
3429 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
3430 #define LAN8841_PTP_TX_MSG_HEADER2			455
3431 
3432 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
3433 				  u32 *sec, u32 *nsec, u16 *seq)
3434 {
3435 	struct phy_device *phydev = ptp_priv->phydev;
3436 
3437 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
3438 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
3439 		return false;
3440 
3441 	*nsec = ((*nsec & 0x3fff) << 16);
3442 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
3443 
3444 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
3445 	*sec = *sec << 16;
3446 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
3447 
3448 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3449 
3450 	return true;
3451 }
3452 
3453 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
3454 {
3455 	u32 sec, nsec;
3456 	u16 seq;
3457 
3458 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
3459 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
3460 }
3461 
3462 #define LAN8841_PTP_RX_INGRESS_SEC_LO			389
3463 #define LAN8841_PTP_RX_INGRESS_SEC_HI			388
3464 #define LAN8841_PTP_RX_INGRESS_NS_LO			387
3465 #define LAN8841_PTP_RX_INGRESS_NS_HI			386
3466 #define LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID		BIT(15)
3467 #define LAN8841_PTP_RX_MSG_HEADER2			391
3468 
3469 static struct lan8814_ptp_rx_ts *lan8841_ptp_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
3470 {
3471 	struct phy_device *phydev = ptp_priv->phydev;
3472 	struct lan8814_ptp_rx_ts *rx_ts;
3473 	u32 sec, nsec;
3474 	u16 seq;
3475 
3476 	nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_HI);
3477 	if (!(nsec & LAN8841_PTP_RX_INGRESS_NSEC_HI_VALID))
3478 		return NULL;
3479 
3480 	nsec = ((nsec & 0x3fff) << 16);
3481 	nsec = nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_NS_LO);
3482 
3483 	sec = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_HI);
3484 	sec = sec << 16;
3485 	sec = sec | phy_read_mmd(phydev, 2, LAN8841_PTP_RX_INGRESS_SEC_LO);
3486 
3487 	seq = phy_read_mmd(phydev, 2, LAN8841_PTP_RX_MSG_HEADER2);
3488 
3489 	rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
3490 	if (!rx_ts)
3491 		return NULL;
3492 
3493 	rx_ts->seconds = sec;
3494 	rx_ts->nsec = nsec;
3495 	rx_ts->seq_id = seq;
3496 
3497 	return rx_ts;
3498 }
3499 
3500 static void lan8841_ptp_process_rx_ts(struct kszphy_ptp_priv *ptp_priv)
3501 {
3502 	struct lan8814_ptp_rx_ts *rx_ts;
3503 
3504 	while ((rx_ts = lan8841_ptp_get_rx_ts(ptp_priv)) != NULL)
3505 		lan8814_match_rx_ts(ptp_priv, rx_ts);
3506 }
3507 
3508 #define LAN8841_PTP_INT_STS			259
3509 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
3510 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
3511 #define LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT	BIT(9)
3512 #define LAN8841_PTP_INT_STS_PTP_RX_TS_INT	BIT(8)
3513 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
3514 
3515 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv, bool egress)
3516 {
3517 	struct phy_device *phydev = ptp_priv->phydev;
3518 	int i;
3519 
3520 	for (i = 0; i < FIFO_SIZE; ++i)
3521 		phy_read_mmd(phydev, 2,
3522 			     egress ? LAN8841_PTP_TX_MSG_HEADER2 :
3523 				      LAN8841_PTP_RX_MSG_HEADER2);
3524 
3525 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3526 }
3527 
3528 #define LAN8841_PTP_GPIO_CAP_STS			506
3529 #define LAN8841_PTP_GPIO_SEL				327
3530 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
3531 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
3532 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
3533 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
3534 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
3535 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
3536 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
3537 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
3538 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
3539 
3540 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
3541 {
3542 	struct phy_device *phydev = ptp_priv->phydev;
3543 	struct ptp_clock_event ptp_event = {0};
3544 	int pin, ret, tmp;
3545 	s32 sec, nsec;
3546 
3547 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
3548 	if (pin == -1)
3549 		return;
3550 
3551 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
3552 	if (tmp < 0)
3553 		return;
3554 
3555 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
3556 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
3557 	if (ret)
3558 		return;
3559 
3560 	mutex_lock(&ptp_priv->ptp_lock);
3561 	if (tmp & BIT(pin)) {
3562 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
3563 		sec <<= 16;
3564 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
3565 
3566 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
3567 		nsec <<= 16;
3568 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
3569 	} else {
3570 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
3571 		sec <<= 16;
3572 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
3573 
3574 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
3575 		nsec <<= 16;
3576 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
3577 	}
3578 	mutex_unlock(&ptp_priv->ptp_lock);
3579 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
3580 	if (ret)
3581 		return;
3582 
3583 	ptp_event.index = 0;
3584 	ptp_event.timestamp = ktime_set(sec, nsec);
3585 	ptp_event.type = PTP_CLOCK_EXTTS;
3586 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
3587 }
3588 
3589 static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
3590 {
3591 	struct kszphy_priv *priv = phydev->priv;
3592 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3593 	u16 status;
3594 
3595 	do {
3596 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3597 
3598 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
3599 			lan8841_ptp_process_tx_ts(ptp_priv);
3600 
3601 		if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_INT)
3602 			lan8841_ptp_process_rx_ts(ptp_priv);
3603 
3604 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
3605 			lan8841_gpio_process_cap(ptp_priv);
3606 
3607 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
3608 			lan8841_ptp_flush_fifo(ptp_priv, true);
3609 			skb_queue_purge(&ptp_priv->tx_queue);
3610 		}
3611 
3612 		if (status & LAN8841_PTP_INT_STS_PTP_RX_TS_OVRFL_INT) {
3613 			lan8841_ptp_flush_fifo(ptp_priv, false);
3614 			skb_queue_purge(&ptp_priv->rx_queue);
3615 		}
3616 
3617 	} while (status);
3618 }
3619 
3620 #define LAN8841_INTS_PTP		BIT(9)
3621 
3622 static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
3623 {
3624 	irqreturn_t ret = IRQ_NONE;
3625 	int irq_status;
3626 
3627 	irq_status = phy_read(phydev, LAN8814_INTS);
3628 	if (irq_status < 0) {
3629 		phy_error(phydev);
3630 		return IRQ_NONE;
3631 	}
3632 
3633 	if (irq_status & LAN8814_INT_LINK) {
3634 		phy_trigger_machine(phydev);
3635 		ret = IRQ_HANDLED;
3636 	}
3637 
3638 	if (irq_status & LAN8841_INTS_PTP) {
3639 		lan8841_handle_ptp_interrupt(phydev);
3640 		ret = IRQ_HANDLED;
3641 	}
3642 
3643 	return ret;
3644 }
3645 
3646 static int lan8841_ts_info(struct mii_timestamper *mii_ts,
3647 			   struct ethtool_ts_info *info)
3648 {
3649 	struct kszphy_ptp_priv *ptp_priv;
3650 
3651 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3652 
3653 	info->phc_index = ptp_priv->ptp_clock ?
3654 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
3655 	if (info->phc_index == -1) {
3656 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
3657 					 SOF_TIMESTAMPING_RX_SOFTWARE |
3658 					 SOF_TIMESTAMPING_SOFTWARE;
3659 		return 0;
3660 	}
3661 
3662 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3663 				SOF_TIMESTAMPING_RX_HARDWARE |
3664 				SOF_TIMESTAMPING_RAW_HARDWARE;
3665 
3666 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3667 			 (1 << HWTSTAMP_TX_ON) |
3668 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
3669 
3670 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3671 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3672 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3673 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3674 
3675 	return 0;
3676 }
3677 
3678 #define LAN8841_PTP_INT_EN			260
3679 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
3680 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
3681 #define LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN	BIT(9)
3682 #define LAN8841_PTP_INT_EN_PTP_RX_TS_EN		BIT(8)
3683 
3684 static void lan8841_ptp_enable_int(struct kszphy_ptp_priv *ptp_priv,
3685 				   bool enable)
3686 {
3687 	struct phy_device *phydev = ptp_priv->phydev;
3688 
3689 	if (enable)
3690 		/* Enable interrupts */
3691 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3692 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3693 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3694 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3695 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN,
3696 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3697 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3698 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3699 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN);
3700 	else
3701 		/* Disable interrupts */
3702 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3703 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3704 			       LAN8841_PTP_INT_EN_PTP_RX_TS_OVRFL_EN |
3705 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN |
3706 			       LAN8841_PTP_INT_EN_PTP_RX_TS_EN, 0);
3707 }
3708 
3709 #define LAN8841_PTP_RX_TIMESTAMP_EN		379
3710 #define LAN8841_PTP_TX_TIMESTAMP_EN		443
3711 #define LAN8841_PTP_TX_MOD			445
3712 
3713 static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
3714 {
3715 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3716 	struct phy_device *phydev = ptp_priv->phydev;
3717 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
3718 	struct hwtstamp_config config;
3719 	int txcfg = 0, rxcfg = 0;
3720 	int pkt_ts_enable;
3721 
3722 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3723 		return -EFAULT;
3724 
3725 	ptp_priv->hwts_tx_type = config.tx_type;
3726 	ptp_priv->rx_filter = config.rx_filter;
3727 
3728 	switch (config.rx_filter) {
3729 	case HWTSTAMP_FILTER_NONE:
3730 		ptp_priv->layer = 0;
3731 		ptp_priv->version = 0;
3732 		break;
3733 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3734 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3735 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3736 		ptp_priv->layer = PTP_CLASS_L4;
3737 		ptp_priv->version = PTP_CLASS_V2;
3738 		break;
3739 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3740 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3741 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3742 		ptp_priv->layer = PTP_CLASS_L2;
3743 		ptp_priv->version = PTP_CLASS_V2;
3744 		break;
3745 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3746 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3747 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3748 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3749 		ptp_priv->version = PTP_CLASS_V2;
3750 		break;
3751 	default:
3752 		return -ERANGE;
3753 	}
3754 
3755 	/* Setup parsing of the frames and enable the timestamping for ptp
3756 	 * frames
3757 	 */
3758 	if (ptp_priv->layer & PTP_CLASS_L2) {
3759 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3760 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3761 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3762 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3763 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3764 	}
3765 
3766 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
3767 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
3768 
3769 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3770 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3771 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3772 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3773 
3774 	/* Enable / disable of the TX timestamp in the SYNC frames */
3775 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
3776 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3777 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
3778 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
3779 
3780 	/* Now enable/disable the timestamping */
3781 	lan8841_ptp_enable_int(ptp_priv,
3782 			       config.rx_filter != HWTSTAMP_FILTER_NONE);
3783 
3784 	/* In case of multiple starts and stops, these needs to be cleared */
3785 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
3786 		list_del(&rx_ts->list);
3787 		kfree(rx_ts);
3788 	}
3789 
3790 	skb_queue_purge(&ptp_priv->rx_queue);
3791 	skb_queue_purge(&ptp_priv->tx_queue);
3792 
3793 	lan8841_ptp_flush_fifo(ptp_priv, false);
3794 	lan8841_ptp_flush_fifo(ptp_priv, true);
3795 
3796 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
3797 }
3798 
3799 #define LAN8841_EVENT_A		0
3800 #define LAN8841_EVENT_B		1
3801 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
3802 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
3803 #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
3804 #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
3805 
3806 static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
3807 				  s64 sec, u32 nsec)
3808 {
3809 	struct phy_device *phydev = ptp_priv->phydev;
3810 	int ret;
3811 
3812 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
3813 			    upper_16_bits(sec));
3814 	if (ret)
3815 		return ret;
3816 
3817 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
3818 			    lower_16_bits(sec));
3819 	if (ret)
3820 		return ret;
3821 
3822 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
3823 			    upper_16_bits(nsec));
3824 	if (ret)
3825 		return ret;
3826 
3827 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
3828 			    lower_16_bits(nsec));
3829 }
3830 
3831 #define LAN8841_BUFFER_TIME	2
3832 
3833 static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
3834 				     const struct timespec64 *ts)
3835 {
3836 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
3837 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
3838 }
3839 
3840 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
3841 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
3842 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
3843 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
3844 
3845 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
3846 				  s64 sec, u32 nsec)
3847 {
3848 	struct phy_device *phydev = ptp_priv->phydev;
3849 	int ret;
3850 
3851 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
3852 			    upper_16_bits(sec));
3853 	if (ret)
3854 		return ret;
3855 
3856 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
3857 			    lower_16_bits(sec));
3858 	if (ret)
3859 		return ret;
3860 
3861 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
3862 			    upper_16_bits(nsec));
3863 	if (ret)
3864 		return ret;
3865 
3866 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
3867 			     lower_16_bits(nsec));
3868 }
3869 
3870 #define LAN8841_PTP_LTC_SET_SEC_HI	262
3871 #define LAN8841_PTP_LTC_SET_SEC_MID	263
3872 #define LAN8841_PTP_LTC_SET_SEC_LO	264
3873 #define LAN8841_PTP_LTC_SET_NS_HI	265
3874 #define LAN8841_PTP_LTC_SET_NS_LO	266
3875 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
3876 
3877 static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
3878 				 const struct timespec64 *ts)
3879 {
3880 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3881 							ptp_clock_info);
3882 	struct phy_device *phydev = ptp_priv->phydev;
3883 	int ret;
3884 
3885 	/* Set the value to be stored */
3886 	mutex_lock(&ptp_priv->ptp_lock);
3887 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
3888 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
3889 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
3890 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
3891 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
3892 
3893 	/* Set the command to load the LTC */
3894 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3895 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
3896 	ret = lan8841_ptp_update_target(ptp_priv, ts);
3897 	mutex_unlock(&ptp_priv->ptp_lock);
3898 
3899 	return ret;
3900 }
3901 
3902 #define LAN8841_PTP_LTC_RD_SEC_HI	358
3903 #define LAN8841_PTP_LTC_RD_SEC_MID	359
3904 #define LAN8841_PTP_LTC_RD_SEC_LO	360
3905 #define LAN8841_PTP_LTC_RD_NS_HI	361
3906 #define LAN8841_PTP_LTC_RD_NS_LO	362
3907 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
3908 
3909 static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
3910 				 struct timespec64 *ts)
3911 {
3912 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3913 							ptp_clock_info);
3914 	struct phy_device *phydev = ptp_priv->phydev;
3915 	time64_t s;
3916 	s64 ns;
3917 
3918 	mutex_lock(&ptp_priv->ptp_lock);
3919 	/* Issue the command to read the LTC */
3920 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3921 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3922 
3923 	/* Read the LTC */
3924 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3925 	s <<= 16;
3926 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3927 	s <<= 16;
3928 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3929 
3930 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
3931 	ns <<= 16;
3932 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
3933 	mutex_unlock(&ptp_priv->ptp_lock);
3934 
3935 	set_normalized_timespec64(ts, s, ns);
3936 	return 0;
3937 }
3938 
3939 #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
3940 #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
3941 #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
3942 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
3943 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
3944 
3945 static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
3946 {
3947 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3948 							ptp_clock_info);
3949 	struct phy_device *phydev = ptp_priv->phydev;
3950 	struct timespec64 ts;
3951 	bool add = true;
3952 	u32 nsec;
3953 	s32 sec;
3954 	int ret;
3955 
3956 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
3957 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
3958 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
3959 	 * increment so the value can be bigger than 15 sec. Therefore limit the
3960 	 * possible adjustments so we will not have these corner cases
3961 	 */
3962 	if (delta > 10000000000LL || delta < -10000000000LL) {
3963 		/* The timeadjustment is too big, so fall back using set time */
3964 		u64 now;
3965 
3966 		ptp->gettime64(ptp, &ts);
3967 
3968 		now = ktime_to_ns(timespec64_to_ktime(ts));
3969 		ts = ns_to_timespec64(now + delta);
3970 
3971 		ptp->settime64(ptp, &ts);
3972 		return 0;
3973 	}
3974 
3975 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
3976 	if (delta < 0 && nsec != 0) {
3977 		/* It is not allowed to adjust low the nsec part, therefore
3978 		 * subtract more from second part and add to nanosecond such
3979 		 * that would roll over, so the second part will increase
3980 		 */
3981 		sec--;
3982 		nsec = NSEC_PER_SEC - nsec;
3983 	}
3984 
3985 	/* Calculate the adjustments and the direction */
3986 	if (delta < 0)
3987 		add = false;
3988 
3989 	if (nsec > 0)
3990 		/* add 8 ns to cover the likely normal increment */
3991 		nsec += 8;
3992 
3993 	if (nsec >= NSEC_PER_SEC) {
3994 		/* carry into seconds */
3995 		sec++;
3996 		nsec -= NSEC_PER_SEC;
3997 	}
3998 
3999 	mutex_lock(&ptp_priv->ptp_lock);
4000 	if (sec) {
4001 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
4002 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4003 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
4004 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4005 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
4006 	}
4007 
4008 	if (nsec) {
4009 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
4010 			      nsec & 0xffff);
4011 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4012 			      (nsec >> 16) & 0x3fff);
4013 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4014 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
4015 	}
4016 	mutex_unlock(&ptp_priv->ptp_lock);
4017 
4018 	/* Update the target clock */
4019 	ptp->gettime64(ptp, &ts);
4020 	mutex_lock(&ptp_priv->ptp_lock);
4021 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
4022 	mutex_unlock(&ptp_priv->ptp_lock);
4023 
4024 	return ret;
4025 }
4026 
4027 #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
4028 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
4029 #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
4030 
4031 static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
4032 {
4033 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4034 							ptp_clock_info);
4035 	struct phy_device *phydev = ptp_priv->phydev;
4036 	bool faster = true;
4037 	u32 rate;
4038 
4039 	if (!scaled_ppm)
4040 		return 0;
4041 
4042 	if (scaled_ppm < 0) {
4043 		scaled_ppm = -scaled_ppm;
4044 		faster = false;
4045 	}
4046 
4047 	rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
4048 	rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
4049 
4050 	mutex_lock(&ptp_priv->ptp_lock);
4051 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
4052 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
4053 			     : upper_16_bits(rate) & 0x3fff);
4054 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
4055 	mutex_unlock(&ptp_priv->ptp_lock);
4056 
4057 	return 0;
4058 }
4059 
4060 static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
4061 			      enum ptp_pin_function func, unsigned int chan)
4062 {
4063 	switch (func) {
4064 	case PTP_PF_NONE:
4065 	case PTP_PF_PEROUT:
4066 	case PTP_PF_EXTTS:
4067 		break;
4068 	default:
4069 		return -1;
4070 	}
4071 
4072 	return 0;
4073 }
4074 
4075 #define LAN8841_PTP_GPIO_NUM	10
4076 #define LAN8841_GPIO_EN		128
4077 #define LAN8841_GPIO_DIR	129
4078 #define LAN8841_GPIO_BUF	130
4079 
4080 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4081 {
4082 	struct phy_device *phydev = ptp_priv->phydev;
4083 	int ret;
4084 
4085 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4086 	if (ret)
4087 		return ret;
4088 
4089 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4090 	if (ret)
4091 		return ret;
4092 
4093 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4094 }
4095 
4096 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
4097 {
4098 	struct phy_device *phydev = ptp_priv->phydev;
4099 	int ret;
4100 
4101 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4102 	if (ret)
4103 		return ret;
4104 
4105 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4106 	if (ret)
4107 		return ret;
4108 
4109 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4110 }
4111 
4112 #define LAN8841_GPIO_DATA_SEL1				131
4113 #define LAN8841_GPIO_DATA_SEL2				132
4114 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
4115 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
4116 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
4117 #define LAN8841_PTP_GENERAL_CONFIG			257
4118 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
4119 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
4120 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
4121 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
4122 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
4123 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
4124 
4125 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4126 				    u8 event)
4127 {
4128 	struct phy_device *phydev = ptp_priv->phydev;
4129 	u16 tmp;
4130 	int ret;
4131 
4132 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4133 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4134 	 * depending on the pin, it requires to read a different register
4135 	 */
4136 	if (pin < 5) {
4137 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
4138 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
4139 	} else {
4140 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
4141 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
4142 	}
4143 	if (ret)
4144 		return ret;
4145 
4146 	/* Disable the event */
4147 	if (event == LAN8841_EVENT_A)
4148 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4149 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
4150 	else
4151 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4152 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
4153 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
4154 }
4155 
4156 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4157 				    u8 event, int pulse_width)
4158 {
4159 	struct phy_device *phydev = ptp_priv->phydev;
4160 	u16 tmp;
4161 	int ret;
4162 
4163 	/* Enable the event */
4164 	if (event == LAN8841_EVENT_A)
4165 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4166 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4167 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
4168 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4169 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
4170 	else
4171 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4172 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4173 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
4174 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4175 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
4176 	if (ret)
4177 		return ret;
4178 
4179 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4180 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4181 	 * depending on the pin, it requires to read a different register
4182 	 */
4183 	if (event == LAN8841_EVENT_A)
4184 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
4185 	else
4186 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
4187 
4188 	if (pin < 5)
4189 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
4190 				       tmp << (3 * pin));
4191 	else
4192 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
4193 				       tmp << (3 * (pin - 5)));
4194 
4195 	return ret;
4196 }
4197 
4198 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
4199 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
4200 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
4201 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
4202 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
4203 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
4204 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
4205 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
4206 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
4207 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
4208 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
4209 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
4210 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
4211 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
4212 
4213 static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
4214 			      struct ptp_clock_request *rq, int on)
4215 {
4216 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4217 							ptp_clock_info);
4218 	struct phy_device *phydev = ptp_priv->phydev;
4219 	struct timespec64 ts_on, ts_period;
4220 	s64 on_nsec, period_nsec;
4221 	int pulse_width;
4222 	int pin;
4223 	int ret;
4224 
4225 	if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE)
4226 		return -EOPNOTSUPP;
4227 
4228 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
4229 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4230 		return -EINVAL;
4231 
4232 	if (!on) {
4233 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
4234 		if (ret)
4235 			return ret;
4236 
4237 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
4238 	}
4239 
4240 	ts_on.tv_sec = rq->perout.on.sec;
4241 	ts_on.tv_nsec = rq->perout.on.nsec;
4242 	on_nsec = timespec64_to_ns(&ts_on);
4243 
4244 	ts_period.tv_sec = rq->perout.period.sec;
4245 	ts_period.tv_nsec = rq->perout.period.nsec;
4246 	period_nsec = timespec64_to_ns(&ts_period);
4247 
4248 	if (period_nsec < 200) {
4249 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4250 				    phydev_name(phydev));
4251 		return -EOPNOTSUPP;
4252 	}
4253 
4254 	if (on_nsec >= period_nsec) {
4255 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
4256 				    phydev_name(phydev));
4257 		return -EINVAL;
4258 	}
4259 
4260 	switch (on_nsec) {
4261 	case 200000000:
4262 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
4263 		break;
4264 	case 100000000:
4265 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
4266 		break;
4267 	case 50000000:
4268 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
4269 		break;
4270 	case 10000000:
4271 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
4272 		break;
4273 	case 5000000:
4274 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
4275 		break;
4276 	case 1000000:
4277 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
4278 		break;
4279 	case 500000:
4280 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
4281 		break;
4282 	case 100000:
4283 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
4284 		break;
4285 	case 50000:
4286 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
4287 		break;
4288 	case 10000:
4289 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
4290 		break;
4291 	case 5000:
4292 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
4293 		break;
4294 	case 1000:
4295 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
4296 		break;
4297 	case 500:
4298 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
4299 		break;
4300 	case 100:
4301 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4302 		break;
4303 	default:
4304 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
4305 				    phydev_name(phydev));
4306 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4307 		break;
4308 	}
4309 
4310 	mutex_lock(&ptp_priv->ptp_lock);
4311 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
4312 				     rq->perout.start.nsec);
4313 	mutex_unlock(&ptp_priv->ptp_lock);
4314 	if (ret)
4315 		return ret;
4316 
4317 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
4318 				     rq->perout.period.nsec);
4319 	if (ret)
4320 		return ret;
4321 
4322 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
4323 				       pulse_width);
4324 	if (ret)
4325 		return ret;
4326 
4327 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
4328 	if (ret)
4329 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
4330 
4331 	return ret;
4332 }
4333 
4334 #define LAN8841_PTP_GPIO_CAP_EN			496
4335 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
4336 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
4337 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
4338 
4339 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
4340 				u32 flags)
4341 {
4342 	struct phy_device *phydev = ptp_priv->phydev;
4343 	u16 tmp = 0;
4344 	int ret;
4345 
4346 	/* Set GPIO to be intput */
4347 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4348 	if (ret)
4349 		return ret;
4350 
4351 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4352 	if (ret)
4353 		return ret;
4354 
4355 	/* Enable capture on the edges of the pin */
4356 	if (flags & PTP_RISING_EDGE)
4357 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
4358 	if (flags & PTP_FALLING_EDGE)
4359 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
4360 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
4361 	if (ret)
4362 		return ret;
4363 
4364 	/* Enable interrupt */
4365 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4366 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4367 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
4368 }
4369 
4370 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4371 {
4372 	struct phy_device *phydev = ptp_priv->phydev;
4373 	int ret;
4374 
4375 	/* Set GPIO to be output */
4376 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4377 	if (ret)
4378 		return ret;
4379 
4380 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4381 	if (ret)
4382 		return ret;
4383 
4384 	/* Disable capture on both of the edges */
4385 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
4386 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
4387 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
4388 			     0);
4389 	if (ret)
4390 		return ret;
4391 
4392 	/* Disable interrupt */
4393 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4394 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4395 			      0);
4396 }
4397 
4398 static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
4399 			     struct ptp_clock_request *rq, int on)
4400 {
4401 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4402 							ptp_clock_info);
4403 	int pin;
4404 	int ret;
4405 
4406 	/* Reject requests with unsupported flags */
4407 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
4408 				PTP_EXTTS_EDGES |
4409 				PTP_STRICT_FLAGS))
4410 		return -EOPNOTSUPP;
4411 
4412 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
4413 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4414 		return -EINVAL;
4415 
4416 	mutex_lock(&ptp_priv->ptp_lock);
4417 	if (on)
4418 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
4419 	else
4420 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
4421 	mutex_unlock(&ptp_priv->ptp_lock);
4422 
4423 	return ret;
4424 }
4425 
4426 static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
4427 			      struct ptp_clock_request *rq, int on)
4428 {
4429 	switch (rq->type) {
4430 	case PTP_CLK_REQ_EXTTS:
4431 		return lan8841_ptp_extts(ptp, rq, on);
4432 	case PTP_CLK_REQ_PEROUT:
4433 		return lan8841_ptp_perout(ptp, rq, on);
4434 	default:
4435 		return -EOPNOTSUPP;
4436 	}
4437 
4438 	return 0;
4439 }
4440 
4441 static struct ptp_clock_info lan8841_ptp_clock_info = {
4442 	.owner		= THIS_MODULE,
4443 	.name		= "lan8841 ptp",
4444 	.max_adj	= 31249999,
4445 	.gettime64	= lan8841_ptp_gettime64,
4446 	.settime64	= lan8841_ptp_settime64,
4447 	.adjtime	= lan8841_ptp_adjtime,
4448 	.adjfine	= lan8841_ptp_adjfine,
4449 	.verify         = lan8841_ptp_verify,
4450 	.enable         = lan8841_ptp_enable,
4451 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
4452 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
4453 	.n_pins         = LAN8841_PTP_GPIO_NUM,
4454 };
4455 
4456 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
4457 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
4458 
4459 static int lan8841_probe(struct phy_device *phydev)
4460 {
4461 	struct kszphy_ptp_priv *ptp_priv;
4462 	struct kszphy_priv *priv;
4463 	int err;
4464 
4465 	err = kszphy_probe(phydev);
4466 	if (err)
4467 		return err;
4468 
4469 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4470 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
4471 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
4472 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
4473 
4474 	/* Register the clock */
4475 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4476 		return 0;
4477 
4478 	priv = phydev->priv;
4479 	ptp_priv = &priv->ptp_priv;
4480 
4481 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
4482 					    LAN8841_PTP_GPIO_NUM,
4483 					    sizeof(*ptp_priv->pin_config),
4484 					    GFP_KERNEL);
4485 	if (!ptp_priv->pin_config)
4486 		return -ENOMEM;
4487 
4488 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
4489 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
4490 
4491 		snprintf(p->name, sizeof(p->name), "pin%d", i);
4492 		p->index = i;
4493 		p->func = PTP_PF_NONE;
4494 	}
4495 
4496 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
4497 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
4498 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
4499 						 &phydev->mdio.dev);
4500 	if (IS_ERR(ptp_priv->ptp_clock)) {
4501 		phydev_err(phydev, "ptp_clock_register failed: %lu\n",
4502 			   PTR_ERR(ptp_priv->ptp_clock));
4503 		return -EINVAL;
4504 	}
4505 
4506 	if (!ptp_priv->ptp_clock)
4507 		return 0;
4508 
4509 	/* Initialize the SW */
4510 	skb_queue_head_init(&ptp_priv->tx_queue);
4511 	skb_queue_head_init(&ptp_priv->rx_queue);
4512 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
4513 	spin_lock_init(&ptp_priv->rx_ts_lock);
4514 	ptp_priv->phydev = phydev;
4515 	mutex_init(&ptp_priv->ptp_lock);
4516 
4517 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
4518 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4519 	ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp;
4520 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
4521 
4522 	phydev->mii_ts = &ptp_priv->mii_ts;
4523 
4524 	return 0;
4525 }
4526 
4527 static struct phy_driver ksphy_driver[] = {
4528 {
4529 	.phy_id		= PHY_ID_KS8737,
4530 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4531 	.name		= "Micrel KS8737",
4532 	/* PHY_BASIC_FEATURES */
4533 	.driver_data	= &ks8737_type,
4534 	.probe		= kszphy_probe,
4535 	.config_init	= kszphy_config_init,
4536 	.config_intr	= kszphy_config_intr,
4537 	.handle_interrupt = kszphy_handle_interrupt,
4538 	.suspend	= kszphy_suspend,
4539 	.resume		= kszphy_resume,
4540 }, {
4541 	.phy_id		= PHY_ID_KSZ8021,
4542 	.phy_id_mask	= 0x00ffffff,
4543 	.name		= "Micrel KSZ8021 or KSZ8031",
4544 	/* PHY_BASIC_FEATURES */
4545 	.driver_data	= &ksz8021_type,
4546 	.probe		= kszphy_probe,
4547 	.config_init	= kszphy_config_init,
4548 	.config_intr	= kszphy_config_intr,
4549 	.handle_interrupt = kszphy_handle_interrupt,
4550 	.get_sset_count = kszphy_get_sset_count,
4551 	.get_strings	= kszphy_get_strings,
4552 	.get_stats	= kszphy_get_stats,
4553 	.suspend	= kszphy_suspend,
4554 	.resume		= kszphy_resume,
4555 }, {
4556 	.phy_id		= PHY_ID_KSZ8031,
4557 	.phy_id_mask	= 0x00ffffff,
4558 	.name		= "Micrel KSZ8031",
4559 	/* PHY_BASIC_FEATURES */
4560 	.driver_data	= &ksz8021_type,
4561 	.probe		= kszphy_probe,
4562 	.config_init	= kszphy_config_init,
4563 	.config_intr	= kszphy_config_intr,
4564 	.handle_interrupt = kszphy_handle_interrupt,
4565 	.get_sset_count = kszphy_get_sset_count,
4566 	.get_strings	= kszphy_get_strings,
4567 	.get_stats	= kszphy_get_stats,
4568 	.suspend	= kszphy_suspend,
4569 	.resume		= kszphy_resume,
4570 }, {
4571 	.phy_id		= PHY_ID_KSZ8041,
4572 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4573 	.name		= "Micrel KSZ8041",
4574 	/* PHY_BASIC_FEATURES */
4575 	.driver_data	= &ksz8041_type,
4576 	.probe		= kszphy_probe,
4577 	.config_init	= ksz8041_config_init,
4578 	.config_aneg	= ksz8041_config_aneg,
4579 	.config_intr	= kszphy_config_intr,
4580 	.handle_interrupt = kszphy_handle_interrupt,
4581 	.get_sset_count = kszphy_get_sset_count,
4582 	.get_strings	= kszphy_get_strings,
4583 	.get_stats	= kszphy_get_stats,
4584 	/* No suspend/resume callbacks because of errata DS80000700A,
4585 	 * receiver error following software power down.
4586 	 */
4587 }, {
4588 	.phy_id		= PHY_ID_KSZ8041RNLI,
4589 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4590 	.name		= "Micrel KSZ8041RNLI",
4591 	/* PHY_BASIC_FEATURES */
4592 	.driver_data	= &ksz8041_type,
4593 	.probe		= kszphy_probe,
4594 	.config_init	= kszphy_config_init,
4595 	.config_intr	= kszphy_config_intr,
4596 	.handle_interrupt = kszphy_handle_interrupt,
4597 	.get_sset_count = kszphy_get_sset_count,
4598 	.get_strings	= kszphy_get_strings,
4599 	.get_stats	= kszphy_get_stats,
4600 	.suspend	= kszphy_suspend,
4601 	.resume		= kszphy_resume,
4602 }, {
4603 	.name		= "Micrel KSZ8051",
4604 	/* PHY_BASIC_FEATURES */
4605 	.driver_data	= &ksz8051_type,
4606 	.probe		= kszphy_probe,
4607 	.config_init	= kszphy_config_init,
4608 	.config_intr	= kszphy_config_intr,
4609 	.handle_interrupt = kszphy_handle_interrupt,
4610 	.get_sset_count = kszphy_get_sset_count,
4611 	.get_strings	= kszphy_get_strings,
4612 	.get_stats	= kszphy_get_stats,
4613 	.match_phy_device = ksz8051_match_phy_device,
4614 	.suspend	= kszphy_suspend,
4615 	.resume		= kszphy_resume,
4616 }, {
4617 	.phy_id		= PHY_ID_KSZ8001,
4618 	.name		= "Micrel KSZ8001 or KS8721",
4619 	.phy_id_mask	= 0x00fffffc,
4620 	/* PHY_BASIC_FEATURES */
4621 	.driver_data	= &ksz8041_type,
4622 	.probe		= kszphy_probe,
4623 	.config_init	= kszphy_config_init,
4624 	.config_intr	= kszphy_config_intr,
4625 	.handle_interrupt = kszphy_handle_interrupt,
4626 	.get_sset_count = kszphy_get_sset_count,
4627 	.get_strings	= kszphy_get_strings,
4628 	.get_stats	= kszphy_get_stats,
4629 	.suspend	= kszphy_suspend,
4630 	.resume		= kszphy_resume,
4631 }, {
4632 	.phy_id		= PHY_ID_KSZ8081,
4633 	.name		= "Micrel KSZ8081 or KSZ8091",
4634 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4635 	.flags		= PHY_POLL_CABLE_TEST,
4636 	/* PHY_BASIC_FEATURES */
4637 	.driver_data	= &ksz8081_type,
4638 	.probe		= kszphy_probe,
4639 	.config_init	= ksz8081_config_init,
4640 	.soft_reset	= genphy_soft_reset,
4641 	.config_aneg	= ksz8081_config_aneg,
4642 	.read_status	= ksz8081_read_status,
4643 	.config_intr	= kszphy_config_intr,
4644 	.handle_interrupt = kszphy_handle_interrupt,
4645 	.get_sset_count = kszphy_get_sset_count,
4646 	.get_strings	= kszphy_get_strings,
4647 	.get_stats	= kszphy_get_stats,
4648 	.suspend	= kszphy_suspend,
4649 	.resume		= kszphy_resume,
4650 	.cable_test_start	= ksz886x_cable_test_start,
4651 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4652 }, {
4653 	.phy_id		= PHY_ID_KSZ8061,
4654 	.name		= "Micrel KSZ8061",
4655 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4656 	/* PHY_BASIC_FEATURES */
4657 	.probe		= kszphy_probe,
4658 	.config_init	= ksz8061_config_init,
4659 	.config_intr	= kszphy_config_intr,
4660 	.handle_interrupt = kszphy_handle_interrupt,
4661 	.suspend	= kszphy_suspend,
4662 	.resume		= kszphy_resume,
4663 }, {
4664 	.phy_id		= PHY_ID_KSZ9021,
4665 	.phy_id_mask	= 0x000ffffe,
4666 	.name		= "Micrel KSZ9021 Gigabit PHY",
4667 	/* PHY_GBIT_FEATURES */
4668 	.driver_data	= &ksz9021_type,
4669 	.probe		= kszphy_probe,
4670 	.get_features	= ksz9031_get_features,
4671 	.config_init	= ksz9021_config_init,
4672 	.config_intr	= kszphy_config_intr,
4673 	.handle_interrupt = kszphy_handle_interrupt,
4674 	.get_sset_count = kszphy_get_sset_count,
4675 	.get_strings	= kszphy_get_strings,
4676 	.get_stats	= kszphy_get_stats,
4677 	.suspend	= kszphy_suspend,
4678 	.resume		= kszphy_resume,
4679 	.read_mmd	= genphy_read_mmd_unsupported,
4680 	.write_mmd	= genphy_write_mmd_unsupported,
4681 }, {
4682 	.phy_id		= PHY_ID_KSZ9031,
4683 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4684 	.name		= "Micrel KSZ9031 Gigabit PHY",
4685 	.flags		= PHY_POLL_CABLE_TEST,
4686 	.driver_data	= &ksz9021_type,
4687 	.probe		= kszphy_probe,
4688 	.get_features	= ksz9031_get_features,
4689 	.config_init	= ksz9031_config_init,
4690 	.soft_reset	= genphy_soft_reset,
4691 	.read_status	= ksz9031_read_status,
4692 	.config_intr	= kszphy_config_intr,
4693 	.handle_interrupt = kszphy_handle_interrupt,
4694 	.get_sset_count = kszphy_get_sset_count,
4695 	.get_strings	= kszphy_get_strings,
4696 	.get_stats	= kszphy_get_stats,
4697 	.suspend	= kszphy_suspend,
4698 	.resume		= kszphy_resume,
4699 	.cable_test_start	= ksz9x31_cable_test_start,
4700 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4701 }, {
4702 	.phy_id		= PHY_ID_LAN8814,
4703 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4704 	.name		= "Microchip INDY Gigabit Quad PHY",
4705 	.flags          = PHY_POLL_CABLE_TEST,
4706 	.config_init	= lan8814_config_init,
4707 	.driver_data	= &lan8814_type,
4708 	.probe		= lan8814_probe,
4709 	.soft_reset	= genphy_soft_reset,
4710 	.read_status	= ksz9031_read_status,
4711 	.get_sset_count	= kszphy_get_sset_count,
4712 	.get_strings	= kszphy_get_strings,
4713 	.get_stats	= kszphy_get_stats,
4714 	.suspend	= genphy_suspend,
4715 	.resume		= kszphy_resume,
4716 	.config_intr	= lan8814_config_intr,
4717 	.handle_interrupt = lan8814_handle_interrupt,
4718 	.cable_test_start	= lan8814_cable_test_start,
4719 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4720 }, {
4721 	.phy_id		= PHY_ID_LAN8804,
4722 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4723 	.name		= "Microchip LAN966X Gigabit PHY",
4724 	.config_init	= lan8804_config_init,
4725 	.driver_data	= &ksz9021_type,
4726 	.probe		= kszphy_probe,
4727 	.soft_reset	= genphy_soft_reset,
4728 	.read_status	= ksz9031_read_status,
4729 	.get_sset_count	= kszphy_get_sset_count,
4730 	.get_strings	= kszphy_get_strings,
4731 	.get_stats	= kszphy_get_stats,
4732 	.suspend	= genphy_suspend,
4733 	.resume		= kszphy_resume,
4734 	.config_intr	= lan8804_config_intr,
4735 	.handle_interrupt = lan8804_handle_interrupt,
4736 }, {
4737 	.phy_id		= PHY_ID_LAN8841,
4738 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4739 	.name		= "Microchip LAN8841 Gigabit PHY",
4740 	.flags		= PHY_POLL_CABLE_TEST,
4741 	.driver_data	= &lan8841_type,
4742 	.config_init	= lan8841_config_init,
4743 	.probe		= lan8841_probe,
4744 	.soft_reset	= genphy_soft_reset,
4745 	.config_intr	= lan8841_config_intr,
4746 	.handle_interrupt = lan8841_handle_interrupt,
4747 	.get_sset_count = kszphy_get_sset_count,
4748 	.get_strings	= kszphy_get_strings,
4749 	.get_stats	= kszphy_get_stats,
4750 	.suspend	= genphy_suspend,
4751 	.resume		= genphy_resume,
4752 	.cable_test_start	= lan8814_cable_test_start,
4753 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4754 }, {
4755 	.phy_id		= PHY_ID_KSZ9131,
4756 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4757 	.name		= "Microchip KSZ9131 Gigabit PHY",
4758 	/* PHY_GBIT_FEATURES */
4759 	.flags		= PHY_POLL_CABLE_TEST,
4760 	.driver_data	= &ksz9131_type,
4761 	.probe		= kszphy_probe,
4762 	.config_init	= ksz9131_config_init,
4763 	.config_intr	= kszphy_config_intr,
4764 	.config_aneg	= ksz9131_config_aneg,
4765 	.read_status	= ksz9131_read_status,
4766 	.handle_interrupt = kszphy_handle_interrupt,
4767 	.get_sset_count = kszphy_get_sset_count,
4768 	.get_strings	= kszphy_get_strings,
4769 	.get_stats	= kszphy_get_stats,
4770 	.suspend	= kszphy_suspend,
4771 	.resume		= kszphy_resume,
4772 	.cable_test_start	= ksz9x31_cable_test_start,
4773 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4774 	.get_features	= ksz9477_get_features,
4775 }, {
4776 	.phy_id		= PHY_ID_KSZ8873MLL,
4777 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4778 	.name		= "Micrel KSZ8873MLL Switch",
4779 	/* PHY_BASIC_FEATURES */
4780 	.config_init	= kszphy_config_init,
4781 	.config_aneg	= ksz8873mll_config_aneg,
4782 	.read_status	= ksz8873mll_read_status,
4783 	.suspend	= genphy_suspend,
4784 	.resume		= genphy_resume,
4785 }, {
4786 	.phy_id		= PHY_ID_KSZ886X,
4787 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4788 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
4789 	.driver_data	= &ksz886x_type,
4790 	/* PHY_BASIC_FEATURES */
4791 	.flags		= PHY_POLL_CABLE_TEST,
4792 	.config_init	= kszphy_config_init,
4793 	.config_aneg	= ksz886x_config_aneg,
4794 	.read_status	= ksz886x_read_status,
4795 	.suspend	= genphy_suspend,
4796 	.resume		= genphy_resume,
4797 	.cable_test_start	= ksz886x_cable_test_start,
4798 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4799 }, {
4800 	.name		= "Micrel KSZ87XX Switch",
4801 	/* PHY_BASIC_FEATURES */
4802 	.config_init	= kszphy_config_init,
4803 	.match_phy_device = ksz8795_match_phy_device,
4804 	.suspend	= genphy_suspend,
4805 	.resume		= genphy_resume,
4806 }, {
4807 	.phy_id		= PHY_ID_KSZ9477,
4808 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4809 	.name		= "Microchip KSZ9477",
4810 	/* PHY_GBIT_FEATURES */
4811 	.config_init	= ksz9477_config_init,
4812 	.config_intr	= kszphy_config_intr,
4813 	.handle_interrupt = kszphy_handle_interrupt,
4814 	.suspend	= genphy_suspend,
4815 	.resume		= genphy_resume,
4816 	.get_features	= ksz9477_get_features,
4817 } };
4818 
4819 module_phy_driver(ksphy_driver);
4820 
4821 MODULE_DESCRIPTION("Micrel PHY driver");
4822 MODULE_AUTHOR("David J. Choi");
4823 MODULE_LICENSE("GPL");
4824 
4825 static struct mdio_device_id __maybe_unused micrel_tbl[] = {
4826 	{ PHY_ID_KSZ9021, 0x000ffffe },
4827 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
4828 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
4829 	{ PHY_ID_KSZ8001, 0x00fffffc },
4830 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
4831 	{ PHY_ID_KSZ8021, 0x00ffffff },
4832 	{ PHY_ID_KSZ8031, 0x00ffffff },
4833 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
4834 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
4835 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
4836 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
4837 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
4838 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
4839 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
4840 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
4841 	{ PHY_ID_LAN8841, MICREL_PHY_ID_MASK },
4842 	{ }
4843 };
4844 
4845 MODULE_DEVICE_TABLE(mdio, micrel_tbl);
4846