xref: /openbmc/linux/drivers/net/phy/micrel.c (revision f59a3ee6)
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
14  *		100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
15  *			   ksz8021, ksz8031, ksz8051,
16  *			   ksz8081, ksz8091,
17  *			   ksz8061,
18  *		Switch : ksz8873, ksz886x
19  *			 ksz9477
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 	bool has_broadcast_disable;
272 	bool has_nand_tree_disable;
273 	bool has_rmii_ref_clk_sel;
274 };
275 
276 /* Shared structure between the PHYs of the same package. */
277 struct lan8814_shared_priv {
278 	struct phy_device *phydev;
279 	struct ptp_clock *ptp_clock;
280 	struct ptp_clock_info ptp_clock_info;
281 
282 	/* Reference counter to how many ports in the package are enabling the
283 	 * timestamping
284 	 */
285 	u8 ref;
286 
287 	/* Lock for ptp_clock and ref */
288 	struct mutex shared_lock;
289 };
290 
291 struct lan8814_ptp_rx_ts {
292 	struct list_head list;
293 	u32 seconds;
294 	u32 nsec;
295 	u16 seq_id;
296 };
297 
298 struct kszphy_ptp_priv {
299 	struct mii_timestamper mii_ts;
300 	struct phy_device *phydev;
301 
302 	struct sk_buff_head tx_queue;
303 	struct sk_buff_head rx_queue;
304 
305 	struct list_head rx_ts_list;
306 	/* Lock for Rx ts fifo */
307 	spinlock_t rx_ts_lock;
308 
309 	int hwts_tx_type;
310 	enum hwtstamp_rx_filters rx_filter;
311 	int layer;
312 	int version;
313 };
314 
315 struct kszphy_priv {
316 	struct kszphy_ptp_priv ptp_priv;
317 	const struct kszphy_type *type;
318 	int led_mode;
319 	u16 vct_ctrl1000;
320 	bool rmii_ref_clk_sel;
321 	bool rmii_ref_clk_sel_val;
322 	u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
323 };
324 
325 static const struct kszphy_type lan8814_type = {
326 	.led_mode_reg		= ~LAN8814_LED_CTRL_1,
327 	.cable_diag_reg		= LAN8814_CABLE_DIAG,
328 	.pair_mask		= LAN8814_WIRE_PAIR_MASK,
329 };
330 
331 static const struct kszphy_type ksz886x_type = {
332 	.cable_diag_reg		= KSZ8081_LMD,
333 	.pair_mask		= KSZPHY_WIRE_PAIR_MASK,
334 };
335 
336 static const struct kszphy_type ksz8021_type = {
337 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
338 	.has_broadcast_disable	= true,
339 	.has_nand_tree_disable	= true,
340 	.has_rmii_ref_clk_sel	= true,
341 };
342 
343 static const struct kszphy_type ksz8041_type = {
344 	.led_mode_reg		= MII_KSZPHY_CTRL_1,
345 };
346 
347 static const struct kszphy_type ksz8051_type = {
348 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
349 	.has_nand_tree_disable	= true,
350 };
351 
352 static const struct kszphy_type ksz8081_type = {
353 	.led_mode_reg		= MII_KSZPHY_CTRL_2,
354 	.has_broadcast_disable	= true,
355 	.has_nand_tree_disable	= true,
356 	.has_rmii_ref_clk_sel	= true,
357 };
358 
359 static const struct kszphy_type ks8737_type = {
360 	.interrupt_level_mask	= BIT(14),
361 };
362 
363 static const struct kszphy_type ksz9021_type = {
364 	.interrupt_level_mask	= BIT(14),
365 };
366 
367 static int kszphy_extended_write(struct phy_device *phydev,
368 				u32 regnum, u16 val)
369 {
370 	phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
371 	return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
372 }
373 
374 static int kszphy_extended_read(struct phy_device *phydev,
375 				u32 regnum)
376 {
377 	phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
378 	return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
379 }
380 
381 static int kszphy_ack_interrupt(struct phy_device *phydev)
382 {
383 	/* bit[7..0] int status, which is a read and clear register. */
384 	int rc;
385 
386 	rc = phy_read(phydev, MII_KSZPHY_INTCS);
387 
388 	return (rc < 0) ? rc : 0;
389 }
390 
391 static int kszphy_config_intr(struct phy_device *phydev)
392 {
393 	const struct kszphy_type *type = phydev->drv->driver_data;
394 	int temp, err;
395 	u16 mask;
396 
397 	if (type && type->interrupt_level_mask)
398 		mask = type->interrupt_level_mask;
399 	else
400 		mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
401 
402 	/* set the interrupt pin active low */
403 	temp = phy_read(phydev, MII_KSZPHY_CTRL);
404 	if (temp < 0)
405 		return temp;
406 	temp &= ~mask;
407 	phy_write(phydev, MII_KSZPHY_CTRL, temp);
408 
409 	/* enable / disable interrupts */
410 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
411 		err = kszphy_ack_interrupt(phydev);
412 		if (err)
413 			return err;
414 
415 		temp = KSZPHY_INTCS_ALL;
416 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
417 	} else {
418 		temp = 0;
419 		err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
420 		if (err)
421 			return err;
422 
423 		err = kszphy_ack_interrupt(phydev);
424 	}
425 
426 	return err;
427 }
428 
429 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
430 {
431 	int irq_status;
432 
433 	irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
434 	if (irq_status < 0) {
435 		phy_error(phydev);
436 		return IRQ_NONE;
437 	}
438 
439 	if (!(irq_status & KSZPHY_INTCS_STATUS))
440 		return IRQ_NONE;
441 
442 	phy_trigger_machine(phydev);
443 
444 	return IRQ_HANDLED;
445 }
446 
447 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
448 {
449 	int ctrl;
450 
451 	ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
452 	if (ctrl < 0)
453 		return ctrl;
454 
455 	if (val)
456 		ctrl |= KSZPHY_RMII_REF_CLK_SEL;
457 	else
458 		ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
459 
460 	return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
461 }
462 
463 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
464 {
465 	int rc, temp, shift;
466 
467 	switch (reg) {
468 	case MII_KSZPHY_CTRL_1:
469 		shift = 14;
470 		break;
471 	case MII_KSZPHY_CTRL_2:
472 		shift = 4;
473 		break;
474 	default:
475 		return -EINVAL;
476 	}
477 
478 	temp = phy_read(phydev, reg);
479 	if (temp < 0) {
480 		rc = temp;
481 		goto out;
482 	}
483 
484 	temp &= ~(3 << shift);
485 	temp |= val << shift;
486 	rc = phy_write(phydev, reg, temp);
487 out:
488 	if (rc < 0)
489 		phydev_err(phydev, "failed to set led mode\n");
490 
491 	return rc;
492 }
493 
494 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
495  * unique (non-broadcast) address on a shared bus.
496  */
497 static int kszphy_broadcast_disable(struct phy_device *phydev)
498 {
499 	int ret;
500 
501 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
502 	if (ret < 0)
503 		goto out;
504 
505 	ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
506 out:
507 	if (ret)
508 		phydev_err(phydev, "failed to disable broadcast address\n");
509 
510 	return ret;
511 }
512 
513 static int kszphy_nand_tree_disable(struct phy_device *phydev)
514 {
515 	int ret;
516 
517 	ret = phy_read(phydev, MII_KSZPHY_OMSO);
518 	if (ret < 0)
519 		goto out;
520 
521 	if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
522 		return 0;
523 
524 	ret = phy_write(phydev, MII_KSZPHY_OMSO,
525 			ret & ~KSZPHY_OMSO_NAND_TREE_ON);
526 out:
527 	if (ret)
528 		phydev_err(phydev, "failed to disable NAND tree mode\n");
529 
530 	return ret;
531 }
532 
533 /* Some config bits need to be set again on resume, handle them here. */
534 static int kszphy_config_reset(struct phy_device *phydev)
535 {
536 	struct kszphy_priv *priv = phydev->priv;
537 	int ret;
538 
539 	if (priv->rmii_ref_clk_sel) {
540 		ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
541 		if (ret) {
542 			phydev_err(phydev,
543 				   "failed to set rmii reference clock\n");
544 			return ret;
545 		}
546 	}
547 
548 	if (priv->type && priv->led_mode >= 0)
549 		kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
550 
551 	return 0;
552 }
553 
554 static int kszphy_config_init(struct phy_device *phydev)
555 {
556 	struct kszphy_priv *priv = phydev->priv;
557 	const struct kszphy_type *type;
558 
559 	if (!priv)
560 		return 0;
561 
562 	type = priv->type;
563 
564 	if (type && type->has_broadcast_disable)
565 		kszphy_broadcast_disable(phydev);
566 
567 	if (type && type->has_nand_tree_disable)
568 		kszphy_nand_tree_disable(phydev);
569 
570 	return kszphy_config_reset(phydev);
571 }
572 
573 static int ksz8041_fiber_mode(struct phy_device *phydev)
574 {
575 	struct device_node *of_node = phydev->mdio.dev.of_node;
576 
577 	return of_property_read_bool(of_node, "micrel,fiber-mode");
578 }
579 
580 static int ksz8041_config_init(struct phy_device *phydev)
581 {
582 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
583 
584 	/* Limit supported and advertised modes in fiber mode */
585 	if (ksz8041_fiber_mode(phydev)) {
586 		phydev->dev_flags |= MICREL_PHY_FXEN;
587 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
588 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
589 
590 		linkmode_and(phydev->supported, phydev->supported, mask);
591 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
592 				 phydev->supported);
593 		linkmode_and(phydev->advertising, phydev->advertising, mask);
594 		linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
595 				 phydev->advertising);
596 		phydev->autoneg = AUTONEG_DISABLE;
597 	}
598 
599 	return kszphy_config_init(phydev);
600 }
601 
602 static int ksz8041_config_aneg(struct phy_device *phydev)
603 {
604 	/* Skip auto-negotiation in fiber mode */
605 	if (phydev->dev_flags & MICREL_PHY_FXEN) {
606 		phydev->speed = SPEED_100;
607 		return 0;
608 	}
609 
610 	return genphy_config_aneg(phydev);
611 }
612 
613 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
614 					    const bool ksz_8051)
615 {
616 	int ret;
617 
618 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
619 		return 0;
620 
621 	ret = phy_read(phydev, MII_BMSR);
622 	if (ret < 0)
623 		return ret;
624 
625 	/* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
626 	 * exact PHY ID. However, they can be told apart by the extended
627 	 * capability registers presence. The KSZ8051 PHY has them while
628 	 * the switch does not.
629 	 */
630 	ret &= BMSR_ERCAP;
631 	if (ksz_8051)
632 		return ret;
633 	else
634 		return !ret;
635 }
636 
637 static int ksz8051_match_phy_device(struct phy_device *phydev)
638 {
639 	return ksz8051_ksz8795_match_phy_device(phydev, true);
640 }
641 
642 static int ksz8081_config_init(struct phy_device *phydev)
643 {
644 	/* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
645 	 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
646 	 * pull-down is missing, the factory test mode should be cleared by
647 	 * manually writing a 0.
648 	 */
649 	phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
650 
651 	return kszphy_config_init(phydev);
652 }
653 
654 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
655 {
656 	u16 val;
657 
658 	switch (ctrl) {
659 	case ETH_TP_MDI:
660 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
661 		break;
662 	case ETH_TP_MDI_X:
663 		val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
664 			KSZ8081_CTRL2_MDI_MDI_X_SELECT;
665 		break;
666 	case ETH_TP_MDI_AUTO:
667 		val = 0;
668 		break;
669 	default:
670 		return 0;
671 	}
672 
673 	return phy_modify(phydev, MII_KSZPHY_CTRL_2,
674 			  KSZ8081_CTRL2_HP_MDIX |
675 			  KSZ8081_CTRL2_MDI_MDI_X_SELECT |
676 			  KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
677 			  KSZ8081_CTRL2_HP_MDIX | val);
678 }
679 
680 static int ksz8081_config_aneg(struct phy_device *phydev)
681 {
682 	int ret;
683 
684 	ret = genphy_config_aneg(phydev);
685 	if (ret)
686 		return ret;
687 
688 	/* The MDI-X configuration is automatically changed by the PHY after
689 	 * switching from autoneg off to on. So, take MDI-X configuration under
690 	 * own control and set it after autoneg configuration was done.
691 	 */
692 	return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
693 }
694 
695 static int ksz8081_mdix_update(struct phy_device *phydev)
696 {
697 	int ret;
698 
699 	ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
700 	if (ret < 0)
701 		return ret;
702 
703 	if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
704 		if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
705 			phydev->mdix_ctrl = ETH_TP_MDI_X;
706 		else
707 			phydev->mdix_ctrl = ETH_TP_MDI;
708 	} else {
709 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
710 	}
711 
712 	ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
713 	if (ret < 0)
714 		return ret;
715 
716 	if (ret & KSZ8081_CTRL1_MDIX_STAT)
717 		phydev->mdix = ETH_TP_MDI;
718 	else
719 		phydev->mdix = ETH_TP_MDI_X;
720 
721 	return 0;
722 }
723 
724 static int ksz8081_read_status(struct phy_device *phydev)
725 {
726 	int ret;
727 
728 	ret = ksz8081_mdix_update(phydev);
729 	if (ret < 0)
730 		return ret;
731 
732 	return genphy_read_status(phydev);
733 }
734 
735 static int ksz8061_config_init(struct phy_device *phydev)
736 {
737 	int ret;
738 
739 	ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
740 	if (ret)
741 		return ret;
742 
743 	return kszphy_config_init(phydev);
744 }
745 
746 static int ksz8795_match_phy_device(struct phy_device *phydev)
747 {
748 	return ksz8051_ksz8795_match_phy_device(phydev, false);
749 }
750 
751 static int ksz9021_load_values_from_of(struct phy_device *phydev,
752 				       const struct device_node *of_node,
753 				       u16 reg,
754 				       const char *field1, const char *field2,
755 				       const char *field3, const char *field4)
756 {
757 	int val1 = -1;
758 	int val2 = -2;
759 	int val3 = -3;
760 	int val4 = -4;
761 	int newval;
762 	int matches = 0;
763 
764 	if (!of_property_read_u32(of_node, field1, &val1))
765 		matches++;
766 
767 	if (!of_property_read_u32(of_node, field2, &val2))
768 		matches++;
769 
770 	if (!of_property_read_u32(of_node, field3, &val3))
771 		matches++;
772 
773 	if (!of_property_read_u32(of_node, field4, &val4))
774 		matches++;
775 
776 	if (!matches)
777 		return 0;
778 
779 	if (matches < 4)
780 		newval = kszphy_extended_read(phydev, reg);
781 	else
782 		newval = 0;
783 
784 	if (val1 != -1)
785 		newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
786 
787 	if (val2 != -2)
788 		newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
789 
790 	if (val3 != -3)
791 		newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
792 
793 	if (val4 != -4)
794 		newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
795 
796 	return kszphy_extended_write(phydev, reg, newval);
797 }
798 
799 static int ksz9021_config_init(struct phy_device *phydev)
800 {
801 	const struct device_node *of_node;
802 	const struct device *dev_walker;
803 
804 	/* The Micrel driver has a deprecated option to place phy OF
805 	 * properties in the MAC node. Walk up the tree of devices to
806 	 * find a device with an OF node.
807 	 */
808 	dev_walker = &phydev->mdio.dev;
809 	do {
810 		of_node = dev_walker->of_node;
811 		dev_walker = dev_walker->parent;
812 
813 	} while (!of_node && dev_walker);
814 
815 	if (of_node) {
816 		ksz9021_load_values_from_of(phydev, of_node,
817 				    MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
818 				    "txen-skew-ps", "txc-skew-ps",
819 				    "rxdv-skew-ps", "rxc-skew-ps");
820 		ksz9021_load_values_from_of(phydev, of_node,
821 				    MII_KSZPHY_RX_DATA_PAD_SKEW,
822 				    "rxd0-skew-ps", "rxd1-skew-ps",
823 				    "rxd2-skew-ps", "rxd3-skew-ps");
824 		ksz9021_load_values_from_of(phydev, of_node,
825 				    MII_KSZPHY_TX_DATA_PAD_SKEW,
826 				    "txd0-skew-ps", "txd1-skew-ps",
827 				    "txd2-skew-ps", "txd3-skew-ps");
828 	}
829 	return 0;
830 }
831 
832 #define KSZ9031_PS_TO_REG		60
833 
834 /* Extended registers */
835 /* MMD Address 0x0 */
836 #define MII_KSZ9031RN_FLP_BURST_TX_LO	3
837 #define MII_KSZ9031RN_FLP_BURST_TX_HI	4
838 
839 /* MMD Address 0x2 */
840 #define MII_KSZ9031RN_CONTROL_PAD_SKEW	4
841 #define MII_KSZ9031RN_RX_CTL_M		GENMASK(7, 4)
842 #define MII_KSZ9031RN_TX_CTL_M		GENMASK(3, 0)
843 
844 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW	5
845 #define MII_KSZ9031RN_RXD3		GENMASK(15, 12)
846 #define MII_KSZ9031RN_RXD2		GENMASK(11, 8)
847 #define MII_KSZ9031RN_RXD1		GENMASK(7, 4)
848 #define MII_KSZ9031RN_RXD0		GENMASK(3, 0)
849 
850 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW	6
851 #define MII_KSZ9031RN_TXD3		GENMASK(15, 12)
852 #define MII_KSZ9031RN_TXD2		GENMASK(11, 8)
853 #define MII_KSZ9031RN_TXD1		GENMASK(7, 4)
854 #define MII_KSZ9031RN_TXD0		GENMASK(3, 0)
855 
856 #define MII_KSZ9031RN_CLK_PAD_SKEW	8
857 #define MII_KSZ9031RN_GTX_CLK		GENMASK(9, 5)
858 #define MII_KSZ9031RN_RX_CLK		GENMASK(4, 0)
859 
860 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
861  * provide different RGMII options we need to configure delay offset
862  * for each pad relative to build in delay.
863  */
864 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
865  * 1.80ns
866  */
867 #define RX_ID				0x7
868 #define RX_CLK_ID			0x19
869 
870 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
871  * internal 1.2ns delay.
872  */
873 #define RX_ND				0xc
874 #define RX_CLK_ND			0x0
875 
876 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
877 #define TX_ID				0x0
878 #define TX_CLK_ID			0x1f
879 
880 /* set tx and tx_clk to "No delay adjustment" to keep 0ns
881  * dealy
882  */
883 #define TX_ND				0x7
884 #define TX_CLK_ND			0xf
885 
886 /* MMD Address 0x1C */
887 #define MII_KSZ9031RN_EDPD		0x23
888 #define MII_KSZ9031RN_EDPD_ENABLE	BIT(0)
889 
890 static int ksz9031_of_load_skew_values(struct phy_device *phydev,
891 				       const struct device_node *of_node,
892 				       u16 reg, size_t field_sz,
893 				       const char *field[], u8 numfields,
894 				       bool *update)
895 {
896 	int val[4] = {-1, -2, -3, -4};
897 	int matches = 0;
898 	u16 mask;
899 	u16 maxval;
900 	u16 newval;
901 	int i;
902 
903 	for (i = 0; i < numfields; i++)
904 		if (!of_property_read_u32(of_node, field[i], val + i))
905 			matches++;
906 
907 	if (!matches)
908 		return 0;
909 
910 	*update |= true;
911 
912 	if (matches < numfields)
913 		newval = phy_read_mmd(phydev, 2, reg);
914 	else
915 		newval = 0;
916 
917 	maxval = (field_sz == 4) ? 0xf : 0x1f;
918 	for (i = 0; i < numfields; i++)
919 		if (val[i] != -(i + 1)) {
920 			mask = 0xffff;
921 			mask ^= maxval << (field_sz * i);
922 			newval = (newval & mask) |
923 				(((val[i] / KSZ9031_PS_TO_REG) & maxval)
924 					<< (field_sz * i));
925 		}
926 
927 	return phy_write_mmd(phydev, 2, reg, newval);
928 }
929 
930 /* Center KSZ9031RNX FLP timing at 16ms. */
931 static int ksz9031_center_flp_timing(struct phy_device *phydev)
932 {
933 	int result;
934 
935 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
936 			       0x0006);
937 	if (result)
938 		return result;
939 
940 	result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
941 			       0x1A80);
942 	if (result)
943 		return result;
944 
945 	return genphy_restart_aneg(phydev);
946 }
947 
948 /* Enable energy-detect power-down mode */
949 static int ksz9031_enable_edpd(struct phy_device *phydev)
950 {
951 	int reg;
952 
953 	reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
954 	if (reg < 0)
955 		return reg;
956 	return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
957 			     reg | MII_KSZ9031RN_EDPD_ENABLE);
958 }
959 
960 static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
961 {
962 	u16 rx, tx, rx_clk, tx_clk;
963 	int ret;
964 
965 	switch (phydev->interface) {
966 	case PHY_INTERFACE_MODE_RGMII:
967 		tx = TX_ND;
968 		tx_clk = TX_CLK_ND;
969 		rx = RX_ND;
970 		rx_clk = RX_CLK_ND;
971 		break;
972 	case PHY_INTERFACE_MODE_RGMII_ID:
973 		tx = TX_ID;
974 		tx_clk = TX_CLK_ID;
975 		rx = RX_ID;
976 		rx_clk = RX_CLK_ID;
977 		break;
978 	case PHY_INTERFACE_MODE_RGMII_RXID:
979 		tx = TX_ND;
980 		tx_clk = TX_CLK_ND;
981 		rx = RX_ID;
982 		rx_clk = RX_CLK_ID;
983 		break;
984 	case PHY_INTERFACE_MODE_RGMII_TXID:
985 		tx = TX_ID;
986 		tx_clk = TX_CLK_ID;
987 		rx = RX_ND;
988 		rx_clk = RX_CLK_ND;
989 		break;
990 	default:
991 		return 0;
992 	}
993 
994 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
995 			    FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
996 			    FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
997 	if (ret < 0)
998 		return ret;
999 
1000 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
1001 			    FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
1002 			    FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
1003 			    FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
1004 			    FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
1005 	if (ret < 0)
1006 		return ret;
1007 
1008 	ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
1009 			    FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
1010 			    FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
1011 			    FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
1012 			    FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
1013 	if (ret < 0)
1014 		return ret;
1015 
1016 	return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
1017 			     FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
1018 			     FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1019 }
1020 
1021 static int ksz9031_config_init(struct phy_device *phydev)
1022 {
1023 	const struct device_node *of_node;
1024 	static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
1025 	static const char *rx_data_skews[4] = {
1026 		"rxd0-skew-ps", "rxd1-skew-ps",
1027 		"rxd2-skew-ps", "rxd3-skew-ps"
1028 	};
1029 	static const char *tx_data_skews[4] = {
1030 		"txd0-skew-ps", "txd1-skew-ps",
1031 		"txd2-skew-ps", "txd3-skew-ps"
1032 	};
1033 	static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1034 	const struct device *dev_walker;
1035 	int result;
1036 
1037 	result = ksz9031_enable_edpd(phydev);
1038 	if (result < 0)
1039 		return result;
1040 
1041 	/* The Micrel driver has a deprecated option to place phy OF
1042 	 * properties in the MAC node. Walk up the tree of devices to
1043 	 * find a device with an OF node.
1044 	 */
1045 	dev_walker = &phydev->mdio.dev;
1046 	do {
1047 		of_node = dev_walker->of_node;
1048 		dev_walker = dev_walker->parent;
1049 	} while (!of_node && dev_walker);
1050 
1051 	if (of_node) {
1052 		bool update = false;
1053 
1054 		if (phy_interface_is_rgmii(phydev)) {
1055 			result = ksz9031_config_rgmii_delay(phydev);
1056 			if (result < 0)
1057 				return result;
1058 		}
1059 
1060 		ksz9031_of_load_skew_values(phydev, of_node,
1061 				MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1062 				clk_skews, 2, &update);
1063 
1064 		ksz9031_of_load_skew_values(phydev, of_node,
1065 				MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1066 				control_skews, 2, &update);
1067 
1068 		ksz9031_of_load_skew_values(phydev, of_node,
1069 				MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1070 				rx_data_skews, 4, &update);
1071 
1072 		ksz9031_of_load_skew_values(phydev, of_node,
1073 				MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1074 				tx_data_skews, 4, &update);
1075 
1076 		if (update && !phy_interface_is_rgmii(phydev))
1077 			phydev_warn(phydev,
1078 				    "*-skew-ps values should be used only with RGMII PHY modes\n");
1079 
1080 		/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1081 		 * When the device links in the 1000BASE-T slave mode only,
1082 		 * the optional 125MHz reference output clock (CLK125_NDO)
1083 		 * has wide duty cycle variation.
1084 		 *
1085 		 * The optional CLK125_NDO clock does not meet the RGMII
1086 		 * 45/55 percent (min/max) duty cycle requirement and therefore
1087 		 * cannot be used directly by the MAC side for clocking
1088 		 * applications that have setup/hold time requirements on
1089 		 * rising and falling clock edges.
1090 		 *
1091 		 * Workaround:
1092 		 * Force the phy to be the master to receive a stable clock
1093 		 * which meets the duty cycle requirement.
1094 		 */
1095 		if (of_property_read_bool(of_node, "micrel,force-master")) {
1096 			result = phy_read(phydev, MII_CTRL1000);
1097 			if (result < 0)
1098 				goto err_force_master;
1099 
1100 			/* enable master mode, config & prefer master */
1101 			result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1102 			result = phy_write(phydev, MII_CTRL1000, result);
1103 			if (result < 0)
1104 				goto err_force_master;
1105 		}
1106 	}
1107 
1108 	return ksz9031_center_flp_timing(phydev);
1109 
1110 err_force_master:
1111 	phydev_err(phydev, "failed to force the phy to master mode\n");
1112 	return result;
1113 }
1114 
1115 #define KSZ9131_SKEW_5BIT_MAX	2400
1116 #define KSZ9131_SKEW_4BIT_MAX	800
1117 #define KSZ9131_OFFSET		700
1118 #define KSZ9131_STEP		100
1119 
1120 static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1121 				       struct device_node *of_node,
1122 				       u16 reg, size_t field_sz,
1123 				       char *field[], u8 numfields)
1124 {
1125 	int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1126 		      -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1127 	int skewval, skewmax = 0;
1128 	int matches = 0;
1129 	u16 maxval;
1130 	u16 newval;
1131 	u16 mask;
1132 	int i;
1133 
1134 	/* psec properties in dts should mean x pico seconds */
1135 	if (field_sz == 5)
1136 		skewmax = KSZ9131_SKEW_5BIT_MAX;
1137 	else
1138 		skewmax = KSZ9131_SKEW_4BIT_MAX;
1139 
1140 	for (i = 0; i < numfields; i++)
1141 		if (!of_property_read_s32(of_node, field[i], &skewval)) {
1142 			if (skewval < -KSZ9131_OFFSET)
1143 				skewval = -KSZ9131_OFFSET;
1144 			else if (skewval > skewmax)
1145 				skewval = skewmax;
1146 
1147 			val[i] = skewval + KSZ9131_OFFSET;
1148 			matches++;
1149 		}
1150 
1151 	if (!matches)
1152 		return 0;
1153 
1154 	if (matches < numfields)
1155 		newval = phy_read_mmd(phydev, 2, reg);
1156 	else
1157 		newval = 0;
1158 
1159 	maxval = (field_sz == 4) ? 0xf : 0x1f;
1160 	for (i = 0; i < numfields; i++)
1161 		if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1162 			mask = 0xffff;
1163 			mask ^= maxval << (field_sz * i);
1164 			newval = (newval & mask) |
1165 				(((val[i] / KSZ9131_STEP) & maxval)
1166 					<< (field_sz * i));
1167 		}
1168 
1169 	return phy_write_mmd(phydev, 2, reg, newval);
1170 }
1171 
1172 #define KSZ9131RN_MMD_COMMON_CTRL_REG	2
1173 #define KSZ9131RN_RXC_DLL_CTRL		76
1174 #define KSZ9131RN_TXC_DLL_CTRL		77
1175 #define KSZ9131RN_DLL_CTRL_BYPASS	BIT_MASK(12)
1176 #define KSZ9131RN_DLL_ENABLE_DELAY	0
1177 #define KSZ9131RN_DLL_DISABLE_DELAY	BIT(12)
1178 
1179 static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1180 {
1181 	u16 rxcdll_val, txcdll_val;
1182 	int ret;
1183 
1184 	switch (phydev->interface) {
1185 	case PHY_INTERFACE_MODE_RGMII:
1186 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1187 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1188 		break;
1189 	case PHY_INTERFACE_MODE_RGMII_ID:
1190 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1191 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1192 		break;
1193 	case PHY_INTERFACE_MODE_RGMII_RXID:
1194 		rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1195 		txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1196 		break;
1197 	case PHY_INTERFACE_MODE_RGMII_TXID:
1198 		rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1199 		txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1200 		break;
1201 	default:
1202 		return 0;
1203 	}
1204 
1205 	ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1206 			     KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1207 			     rxcdll_val);
1208 	if (ret < 0)
1209 		return ret;
1210 
1211 	return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1212 			      KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1213 			      txcdll_val);
1214 }
1215 
1216 /* Silicon Errata DS80000693B
1217  *
1218  * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
1219  * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
1220  * according to the datasheet (off if there is no link).
1221  */
1222 static int ksz9131_led_errata(struct phy_device *phydev)
1223 {
1224 	int reg;
1225 
1226 	reg = phy_read_mmd(phydev, 2, 0);
1227 	if (reg < 0)
1228 		return reg;
1229 
1230 	if (!(reg & BIT(4)))
1231 		return 0;
1232 
1233 	return phy_set_bits(phydev, 0x1e, BIT(9));
1234 }
1235 
1236 static int ksz9131_config_init(struct phy_device *phydev)
1237 {
1238 	struct device_node *of_node;
1239 	char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1240 	char *rx_data_skews[4] = {
1241 		"rxd0-skew-psec", "rxd1-skew-psec",
1242 		"rxd2-skew-psec", "rxd3-skew-psec"
1243 	};
1244 	char *tx_data_skews[4] = {
1245 		"txd0-skew-psec", "txd1-skew-psec",
1246 		"txd2-skew-psec", "txd3-skew-psec"
1247 	};
1248 	char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1249 	const struct device *dev_walker;
1250 	int ret;
1251 
1252 	dev_walker = &phydev->mdio.dev;
1253 	do {
1254 		of_node = dev_walker->of_node;
1255 		dev_walker = dev_walker->parent;
1256 	} while (!of_node && dev_walker);
1257 
1258 	if (!of_node)
1259 		return 0;
1260 
1261 	if (phy_interface_is_rgmii(phydev)) {
1262 		ret = ksz9131_config_rgmii_delay(phydev);
1263 		if (ret < 0)
1264 			return ret;
1265 	}
1266 
1267 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1268 					  MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1269 					  clk_skews, 2);
1270 	if (ret < 0)
1271 		return ret;
1272 
1273 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1274 					  MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1275 					  control_skews, 2);
1276 	if (ret < 0)
1277 		return ret;
1278 
1279 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1280 					  MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1281 					  rx_data_skews, 4);
1282 	if (ret < 0)
1283 		return ret;
1284 
1285 	ret = ksz9131_of_load_skew_values(phydev, of_node,
1286 					  MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1287 					  tx_data_skews, 4);
1288 	if (ret < 0)
1289 		return ret;
1290 
1291 	ret = ksz9131_led_errata(phydev);
1292 	if (ret < 0)
1293 		return ret;
1294 
1295 	return 0;
1296 }
1297 
1298 #define KSZ8873MLL_GLOBAL_CONTROL_4	0x06
1299 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX	BIT(6)
1300 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED	BIT(4)
1301 static int ksz8873mll_read_status(struct phy_device *phydev)
1302 {
1303 	int regval;
1304 
1305 	/* dummy read */
1306 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1307 
1308 	regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1309 
1310 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
1311 		phydev->duplex = DUPLEX_HALF;
1312 	else
1313 		phydev->duplex = DUPLEX_FULL;
1314 
1315 	if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
1316 		phydev->speed = SPEED_10;
1317 	else
1318 		phydev->speed = SPEED_100;
1319 
1320 	phydev->link = 1;
1321 	phydev->pause = phydev->asym_pause = 0;
1322 
1323 	return 0;
1324 }
1325 
1326 static int ksz9031_get_features(struct phy_device *phydev)
1327 {
1328 	int ret;
1329 
1330 	ret = genphy_read_abilities(phydev);
1331 	if (ret < 0)
1332 		return ret;
1333 
1334 	/* Silicon Errata Sheet (DS80000691D or DS80000692D):
1335 	 * Whenever the device's Asymmetric Pause capability is set to 1,
1336 	 * link-up may fail after a link-up to link-down transition.
1337 	 *
1338 	 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1339 	 *
1340 	 * Workaround:
1341 	 * Do not enable the Asymmetric Pause capability bit.
1342 	 */
1343 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1344 
1345 	/* We force setting the Pause capability as the core will force the
1346 	 * Asymmetric Pause capability to 1 otherwise.
1347 	 */
1348 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1349 
1350 	return 0;
1351 }
1352 
1353 static int ksz9031_read_status(struct phy_device *phydev)
1354 {
1355 	int err;
1356 	int regval;
1357 
1358 	err = genphy_read_status(phydev);
1359 	if (err)
1360 		return err;
1361 
1362 	/* Make sure the PHY is not broken. Read idle error count,
1363 	 * and reset the PHY if it is maxed out.
1364 	 */
1365 	regval = phy_read(phydev, MII_STAT1000);
1366 	if ((regval & 0xFF) == 0xFF) {
1367 		phy_init_hw(phydev);
1368 		phydev->link = 0;
1369 		if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1370 			phydev->drv->config_intr(phydev);
1371 		return genphy_config_aneg(phydev);
1372 	}
1373 
1374 	return 0;
1375 }
1376 
1377 static int ksz9x31_cable_test_start(struct phy_device *phydev)
1378 {
1379 	struct kszphy_priv *priv = phydev->priv;
1380 	int ret;
1381 
1382 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1383 	 * Prior to running the cable diagnostics, Auto-negotiation should
1384 	 * be disabled, full duplex set and the link speed set to 1000Mbps
1385 	 * via the Basic Control Register.
1386 	 */
1387 	ret = phy_modify(phydev, MII_BMCR,
1388 			 BMCR_SPEED1000 | BMCR_FULLDPLX |
1389 			 BMCR_ANENABLE | BMCR_SPEED100,
1390 			 BMCR_SPEED1000 | BMCR_FULLDPLX);
1391 	if (ret)
1392 		return ret;
1393 
1394 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1395 	 * The Master-Slave configuration should be set to Slave by writing
1396 	 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
1397 	 * Register.
1398 	 */
1399 	ret = phy_read(phydev, MII_CTRL1000);
1400 	if (ret < 0)
1401 		return ret;
1402 
1403 	/* Cache these bits, they need to be restored once LinkMD finishes. */
1404 	priv->vct_ctrl1000 = ret & (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1405 	ret &= ~(CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
1406 	ret |= CTL1000_ENABLE_MASTER;
1407 
1408 	return phy_write(phydev, MII_CTRL1000, ret);
1409 }
1410 
1411 static int ksz9x31_cable_test_result_trans(u16 status)
1412 {
1413 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1414 	case KSZ9x31_LMD_VCT_ST_NORMAL:
1415 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1416 	case KSZ9x31_LMD_VCT_ST_OPEN:
1417 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1418 	case KSZ9x31_LMD_VCT_ST_SHORT:
1419 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1420 	case KSZ9x31_LMD_VCT_ST_FAIL:
1421 		fallthrough;
1422 	default:
1423 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1424 	}
1425 }
1426 
1427 static bool ksz9x31_cable_test_failed(u16 status)
1428 {
1429 	int stat = FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status);
1430 
1431 	return stat == KSZ9x31_LMD_VCT_ST_FAIL;
1432 }
1433 
1434 static bool ksz9x31_cable_test_fault_length_valid(u16 status)
1435 {
1436 	switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK, status)) {
1437 	case KSZ9x31_LMD_VCT_ST_OPEN:
1438 		fallthrough;
1439 	case KSZ9x31_LMD_VCT_ST_SHORT:
1440 		return true;
1441 	}
1442 	return false;
1443 }
1444 
1445 static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
1446 {
1447 	int dt = FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK, stat);
1448 
1449 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1450 	 *
1451 	 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
1452 	 */
1453 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131)
1454 		dt = clamp(dt - 22, 0, 255);
1455 
1456 	return (dt * 400) / 10;
1457 }
1458 
1459 static int ksz9x31_cable_test_wait_for_completion(struct phy_device *phydev)
1460 {
1461 	int val, ret;
1462 
1463 	ret = phy_read_poll_timeout(phydev, KSZ9x31_LMD, val,
1464 				    !(val & KSZ9x31_LMD_VCT_EN),
1465 				    30000, 100000, true);
1466 
1467 	return ret < 0 ? ret : 0;
1468 }
1469 
1470 static int ksz9x31_cable_test_get_pair(int pair)
1471 {
1472 	static const int ethtool_pair[] = {
1473 		ETHTOOL_A_CABLE_PAIR_A,
1474 		ETHTOOL_A_CABLE_PAIR_B,
1475 		ETHTOOL_A_CABLE_PAIR_C,
1476 		ETHTOOL_A_CABLE_PAIR_D,
1477 	};
1478 
1479 	return ethtool_pair[pair];
1480 }
1481 
1482 static int ksz9x31_cable_test_one_pair(struct phy_device *phydev, int pair)
1483 {
1484 	int ret, val;
1485 
1486 	/* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1487 	 * To test each individual cable pair, set the cable pair in the Cable
1488 	 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
1489 	 * Diagnostic Register, along with setting the Cable Diagnostics Test
1490 	 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
1491 	 * will self clear when the test is concluded.
1492 	 */
1493 	ret = phy_write(phydev, KSZ9x31_LMD,
1494 			KSZ9x31_LMD_VCT_EN | KSZ9x31_LMD_VCT_PAIR(pair));
1495 	if (ret)
1496 		return ret;
1497 
1498 	ret = ksz9x31_cable_test_wait_for_completion(phydev);
1499 	if (ret)
1500 		return ret;
1501 
1502 	val = phy_read(phydev, KSZ9x31_LMD);
1503 	if (val < 0)
1504 		return val;
1505 
1506 	if (ksz9x31_cable_test_failed(val))
1507 		return -EAGAIN;
1508 
1509 	ret = ethnl_cable_test_result(phydev,
1510 				      ksz9x31_cable_test_get_pair(pair),
1511 				      ksz9x31_cable_test_result_trans(val));
1512 	if (ret)
1513 		return ret;
1514 
1515 	if (!ksz9x31_cable_test_fault_length_valid(val))
1516 		return 0;
1517 
1518 	return ethnl_cable_test_fault_length(phydev,
1519 					     ksz9x31_cable_test_get_pair(pair),
1520 					     ksz9x31_cable_test_fault_length(phydev, val));
1521 }
1522 
1523 static int ksz9x31_cable_test_get_status(struct phy_device *phydev,
1524 					 bool *finished)
1525 {
1526 	struct kszphy_priv *priv = phydev->priv;
1527 	unsigned long pair_mask = 0xf;
1528 	int retries = 20;
1529 	int pair, ret, rv;
1530 
1531 	*finished = false;
1532 
1533 	/* Try harder if link partner is active */
1534 	while (pair_mask && retries--) {
1535 		for_each_set_bit(pair, &pair_mask, 4) {
1536 			ret = ksz9x31_cable_test_one_pair(phydev, pair);
1537 			if (ret == -EAGAIN)
1538 				continue;
1539 			if (ret < 0)
1540 				return ret;
1541 			clear_bit(pair, &pair_mask);
1542 		}
1543 		/* If link partner is in autonegotiation mode it will send 2ms
1544 		 * of FLPs with at least 6ms of silence.
1545 		 * Add 2ms sleep to have better chances to hit this silence.
1546 		 */
1547 		if (pair_mask)
1548 			usleep_range(2000, 3000);
1549 	}
1550 
1551 	/* Report remaining unfinished pair result as unknown. */
1552 	for_each_set_bit(pair, &pair_mask, 4) {
1553 		ret = ethnl_cable_test_result(phydev,
1554 					      ksz9x31_cable_test_get_pair(pair),
1555 					      ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC);
1556 	}
1557 
1558 	*finished = true;
1559 
1560 	/* Restore cached bits from before LinkMD got started. */
1561 	rv = phy_modify(phydev, MII_CTRL1000,
1562 			CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER,
1563 			priv->vct_ctrl1000);
1564 	if (rv)
1565 		return rv;
1566 
1567 	return ret;
1568 }
1569 
1570 static int ksz8873mll_config_aneg(struct phy_device *phydev)
1571 {
1572 	return 0;
1573 }
1574 
1575 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
1576 {
1577 	u16 val;
1578 
1579 	switch (ctrl) {
1580 	case ETH_TP_MDI:
1581 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
1582 		break;
1583 	case ETH_TP_MDI_X:
1584 		/* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
1585 		 * counter intuitive, the "-X" in "1 = Force MDI" in the data
1586 		 * sheet seems to be missing:
1587 		 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
1588 		 * 0 = Normal operation (transmit on TX+/TX- pins)
1589 		 */
1590 		val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
1591 		break;
1592 	case ETH_TP_MDI_AUTO:
1593 		val = 0;
1594 		break;
1595 	default:
1596 		return 0;
1597 	}
1598 
1599 	return phy_modify(phydev, MII_BMCR,
1600 			  KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
1601 			  KSZ886X_BMCR_DISABLE_AUTO_MDIX,
1602 			  KSZ886X_BMCR_HP_MDIX | val);
1603 }
1604 
1605 static int ksz886x_config_aneg(struct phy_device *phydev)
1606 {
1607 	int ret;
1608 
1609 	ret = genphy_config_aneg(phydev);
1610 	if (ret)
1611 		return ret;
1612 
1613 	/* The MDI-X configuration is automatically changed by the PHY after
1614 	 * switching from autoneg off to on. So, take MDI-X configuration under
1615 	 * own control and set it after autoneg configuration was done.
1616 	 */
1617 	return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
1618 }
1619 
1620 static int ksz886x_mdix_update(struct phy_device *phydev)
1621 {
1622 	int ret;
1623 
1624 	ret = phy_read(phydev, MII_BMCR);
1625 	if (ret < 0)
1626 		return ret;
1627 
1628 	if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
1629 		if (ret & KSZ886X_BMCR_FORCE_MDI)
1630 			phydev->mdix_ctrl = ETH_TP_MDI_X;
1631 		else
1632 			phydev->mdix_ctrl = ETH_TP_MDI;
1633 	} else {
1634 		phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1635 	}
1636 
1637 	ret = phy_read(phydev, MII_KSZPHY_CTRL);
1638 	if (ret < 0)
1639 		return ret;
1640 
1641 	/* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
1642 	if (ret & KSZ886X_CTRL_MDIX_STAT)
1643 		phydev->mdix = ETH_TP_MDI_X;
1644 	else
1645 		phydev->mdix = ETH_TP_MDI;
1646 
1647 	return 0;
1648 }
1649 
1650 static int ksz886x_read_status(struct phy_device *phydev)
1651 {
1652 	int ret;
1653 
1654 	ret = ksz886x_mdix_update(phydev);
1655 	if (ret < 0)
1656 		return ret;
1657 
1658 	return genphy_read_status(phydev);
1659 }
1660 
1661 static int kszphy_get_sset_count(struct phy_device *phydev)
1662 {
1663 	return ARRAY_SIZE(kszphy_hw_stats);
1664 }
1665 
1666 static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
1667 {
1668 	int i;
1669 
1670 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1671 		strscpy(data + i * ETH_GSTRING_LEN,
1672 			kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
1673 	}
1674 }
1675 
1676 static u64 kszphy_get_stat(struct phy_device *phydev, int i)
1677 {
1678 	struct kszphy_hw_stat stat = kszphy_hw_stats[i];
1679 	struct kszphy_priv *priv = phydev->priv;
1680 	int val;
1681 	u64 ret;
1682 
1683 	val = phy_read(phydev, stat.reg);
1684 	if (val < 0) {
1685 		ret = U64_MAX;
1686 	} else {
1687 		val = val & ((1 << stat.bits) - 1);
1688 		priv->stats[i] += val;
1689 		ret = priv->stats[i];
1690 	}
1691 
1692 	return ret;
1693 }
1694 
1695 static void kszphy_get_stats(struct phy_device *phydev,
1696 			     struct ethtool_stats *stats, u64 *data)
1697 {
1698 	int i;
1699 
1700 	for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
1701 		data[i] = kszphy_get_stat(phydev, i);
1702 }
1703 
1704 static int kszphy_suspend(struct phy_device *phydev)
1705 {
1706 	/* Disable PHY Interrupts */
1707 	if (phy_interrupt_is_valid(phydev)) {
1708 		phydev->interrupts = PHY_INTERRUPT_DISABLED;
1709 		if (phydev->drv->config_intr)
1710 			phydev->drv->config_intr(phydev);
1711 	}
1712 
1713 	return genphy_suspend(phydev);
1714 }
1715 
1716 static void kszphy_parse_led_mode(struct phy_device *phydev)
1717 {
1718 	const struct kszphy_type *type = phydev->drv->driver_data;
1719 	const struct device_node *np = phydev->mdio.dev.of_node;
1720 	struct kszphy_priv *priv = phydev->priv;
1721 	int ret;
1722 
1723 	if (type && type->led_mode_reg) {
1724 		ret = of_property_read_u32(np, "micrel,led-mode",
1725 					   &priv->led_mode);
1726 
1727 		if (ret)
1728 			priv->led_mode = -1;
1729 
1730 		if (priv->led_mode > 3) {
1731 			phydev_err(phydev, "invalid led mode: 0x%02x\n",
1732 				   priv->led_mode);
1733 			priv->led_mode = -1;
1734 		}
1735 	} else {
1736 		priv->led_mode = -1;
1737 	}
1738 }
1739 
1740 static int kszphy_resume(struct phy_device *phydev)
1741 {
1742 	int ret;
1743 
1744 	genphy_resume(phydev);
1745 
1746 	/* After switching from power-down to normal mode, an internal global
1747 	 * reset is automatically generated. Wait a minimum of 1 ms before
1748 	 * read/write access to the PHY registers.
1749 	 */
1750 	usleep_range(1000, 2000);
1751 
1752 	ret = kszphy_config_reset(phydev);
1753 	if (ret)
1754 		return ret;
1755 
1756 	/* Enable PHY Interrupts */
1757 	if (phy_interrupt_is_valid(phydev)) {
1758 		phydev->interrupts = PHY_INTERRUPT_ENABLED;
1759 		if (phydev->drv->config_intr)
1760 			phydev->drv->config_intr(phydev);
1761 	}
1762 
1763 	return 0;
1764 }
1765 
1766 static int kszphy_probe(struct phy_device *phydev)
1767 {
1768 	const struct kszphy_type *type = phydev->drv->driver_data;
1769 	const struct device_node *np = phydev->mdio.dev.of_node;
1770 	struct kszphy_priv *priv;
1771 	struct clk *clk;
1772 
1773 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1774 	if (!priv)
1775 		return -ENOMEM;
1776 
1777 	phydev->priv = priv;
1778 
1779 	priv->type = type;
1780 
1781 	kszphy_parse_led_mode(phydev);
1782 
1783 	clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1784 	/* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1785 	if (!IS_ERR_OR_NULL(clk)) {
1786 		unsigned long rate = clk_get_rate(clk);
1787 		bool rmii_ref_clk_sel_25_mhz;
1788 
1789 		if (type)
1790 			priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
1791 		rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
1792 				"micrel,rmii-reference-clock-select-25-mhz");
1793 
1794 		if (rate > 24500000 && rate < 25500000) {
1795 			priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
1796 		} else if (rate > 49500000 && rate < 50500000) {
1797 			priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
1798 		} else {
1799 			phydev_err(phydev, "Clock rate out of range: %ld\n",
1800 				   rate);
1801 			return -EINVAL;
1802 		}
1803 	}
1804 
1805 	if (ksz8041_fiber_mode(phydev))
1806 		phydev->port = PORT_FIBRE;
1807 
1808 	/* Support legacy board-file configuration */
1809 	if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
1810 		priv->rmii_ref_clk_sel = true;
1811 		priv->rmii_ref_clk_sel_val = true;
1812 	}
1813 
1814 	return 0;
1815 }
1816 
1817 static int lan8814_cable_test_start(struct phy_device *phydev)
1818 {
1819 	/* If autoneg is enabled, we won't be able to test cross pair
1820 	 * short. In this case, the PHY will "detect" a link and
1821 	 * confuse the internal state machine - disable auto neg here.
1822 	 * Set the speed to 1000mbit and full duplex.
1823 	 */
1824 	return phy_modify(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100,
1825 			  BMCR_SPEED1000 | BMCR_FULLDPLX);
1826 }
1827 
1828 static int ksz886x_cable_test_start(struct phy_device *phydev)
1829 {
1830 	if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
1831 		return -EOPNOTSUPP;
1832 
1833 	/* If autoneg is enabled, we won't be able to test cross pair
1834 	 * short. In this case, the PHY will "detect" a link and
1835 	 * confuse the internal state machine - disable auto neg here.
1836 	 * If autoneg is disabled, we should set the speed to 10mbit.
1837 	 */
1838 	return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
1839 }
1840 
1841 static __always_inline int ksz886x_cable_test_result_trans(u16 status, u16 mask)
1842 {
1843 	switch (FIELD_GET(mask, status)) {
1844 	case KSZ8081_LMD_STAT_NORMAL:
1845 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1846 	case KSZ8081_LMD_STAT_SHORT:
1847 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1848 	case KSZ8081_LMD_STAT_OPEN:
1849 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1850 	case KSZ8081_LMD_STAT_FAIL:
1851 		fallthrough;
1852 	default:
1853 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1854 	}
1855 }
1856 
1857 static __always_inline bool ksz886x_cable_test_failed(u16 status, u16 mask)
1858 {
1859 	return FIELD_GET(mask, status) ==
1860 		KSZ8081_LMD_STAT_FAIL;
1861 }
1862 
1863 static __always_inline bool ksz886x_cable_test_fault_length_valid(u16 status, u16 mask)
1864 {
1865 	switch (FIELD_GET(mask, status)) {
1866 	case KSZ8081_LMD_STAT_OPEN:
1867 		fallthrough;
1868 	case KSZ8081_LMD_STAT_SHORT:
1869 		return true;
1870 	}
1871 	return false;
1872 }
1873 
1874 static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *phydev,
1875 							   u16 status, u16 data_mask)
1876 {
1877 	int dt;
1878 
1879 	/* According to the data sheet the distance to the fault is
1880 	 * DELTA_TIME * 0.4 meters for ksz phys.
1881 	 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
1882 	 */
1883 	dt = FIELD_GET(data_mask, status);
1884 
1885 	if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814)
1886 		return ((dt - 22) * 800) / 10;
1887 	else
1888 		return (dt * 400) / 10;
1889 }
1890 
1891 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
1892 {
1893 	const struct kszphy_type *type = phydev->drv->driver_data;
1894 	int val, ret;
1895 
1896 	ret = phy_read_poll_timeout(phydev, type->cable_diag_reg, val,
1897 				    !(val & KSZ8081_LMD_ENABLE_TEST),
1898 				    30000, 100000, true);
1899 
1900 	return ret < 0 ? ret : 0;
1901 }
1902 
1903 static int lan8814_cable_test_one_pair(struct phy_device *phydev, int pair)
1904 {
1905 	static const int ethtool_pair[] = { ETHTOOL_A_CABLE_PAIR_A,
1906 					    ETHTOOL_A_CABLE_PAIR_B,
1907 					    ETHTOOL_A_CABLE_PAIR_C,
1908 					    ETHTOOL_A_CABLE_PAIR_D,
1909 					  };
1910 	u32 fault_length;
1911 	int ret;
1912 	int val;
1913 
1914 	val = KSZ8081_LMD_ENABLE_TEST;
1915 	val = val | (pair << LAN8814_PAIR_BIT_SHIFT);
1916 
1917 	ret = phy_write(phydev, LAN8814_CABLE_DIAG, val);
1918 	if (ret < 0)
1919 		return ret;
1920 
1921 	ret = ksz886x_cable_test_wait_for_completion(phydev);
1922 	if (ret)
1923 		return ret;
1924 
1925 	val = phy_read(phydev, LAN8814_CABLE_DIAG);
1926 	if (val < 0)
1927 		return val;
1928 
1929 	if (ksz886x_cable_test_failed(val, LAN8814_CABLE_DIAG_STAT_MASK))
1930 		return -EAGAIN;
1931 
1932 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
1933 				      ksz886x_cable_test_result_trans(val,
1934 								      LAN8814_CABLE_DIAG_STAT_MASK
1935 								      ));
1936 	if (ret)
1937 		return ret;
1938 
1939 	if (!ksz886x_cable_test_fault_length_valid(val, LAN8814_CABLE_DIAG_STAT_MASK))
1940 		return 0;
1941 
1942 	fault_length = ksz886x_cable_test_fault_length(phydev, val,
1943 						       LAN8814_CABLE_DIAG_VCT_DATA_MASK);
1944 
1945 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
1946 }
1947 
1948 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
1949 {
1950 	static const int ethtool_pair[] = {
1951 		ETHTOOL_A_CABLE_PAIR_A,
1952 		ETHTOOL_A_CABLE_PAIR_B,
1953 	};
1954 	int ret, val, mdix;
1955 	u32 fault_length;
1956 
1957 	/* There is no way to choice the pair, like we do one ksz9031.
1958 	 * We can workaround this limitation by using the MDI-X functionality.
1959 	 */
1960 	if (pair == 0)
1961 		mdix = ETH_TP_MDI;
1962 	else
1963 		mdix = ETH_TP_MDI_X;
1964 
1965 	switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
1966 	case PHY_ID_KSZ8081:
1967 		ret = ksz8081_config_mdix(phydev, mdix);
1968 		break;
1969 	case PHY_ID_KSZ886X:
1970 		ret = ksz886x_config_mdix(phydev, mdix);
1971 		break;
1972 	default:
1973 		ret = -ENODEV;
1974 	}
1975 
1976 	if (ret)
1977 		return ret;
1978 
1979 	/* Now we are ready to fire. This command will send a 100ns pulse
1980 	 * to the pair.
1981 	 */
1982 	ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
1983 	if (ret)
1984 		return ret;
1985 
1986 	ret = ksz886x_cable_test_wait_for_completion(phydev);
1987 	if (ret)
1988 		return ret;
1989 
1990 	val = phy_read(phydev, KSZ8081_LMD);
1991 	if (val < 0)
1992 		return val;
1993 
1994 	if (ksz886x_cable_test_failed(val, KSZ8081_LMD_STAT_MASK))
1995 		return -EAGAIN;
1996 
1997 	ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
1998 				      ksz886x_cable_test_result_trans(val, KSZ8081_LMD_STAT_MASK));
1999 	if (ret)
2000 		return ret;
2001 
2002 	if (!ksz886x_cable_test_fault_length_valid(val, KSZ8081_LMD_STAT_MASK))
2003 		return 0;
2004 
2005 	fault_length = ksz886x_cable_test_fault_length(phydev, val, KSZ8081_LMD_DELTA_TIME_MASK);
2006 
2007 	return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair], fault_length);
2008 }
2009 
2010 static int ksz886x_cable_test_get_status(struct phy_device *phydev,
2011 					 bool *finished)
2012 {
2013 	const struct kszphy_type *type = phydev->drv->driver_data;
2014 	unsigned long pair_mask = type->pair_mask;
2015 	int retries = 20;
2016 	int pair, ret;
2017 
2018 	*finished = false;
2019 
2020 	/* Try harder if link partner is active */
2021 	while (pair_mask && retries--) {
2022 		for_each_set_bit(pair, &pair_mask, 4) {
2023 			if (type->cable_diag_reg == LAN8814_CABLE_DIAG)
2024 				ret = lan8814_cable_test_one_pair(phydev, pair);
2025 			else
2026 				ret = ksz886x_cable_test_one_pair(phydev, pair);
2027 			if (ret == -EAGAIN)
2028 				continue;
2029 			if (ret < 0)
2030 				return ret;
2031 			clear_bit(pair, &pair_mask);
2032 		}
2033 		/* If link partner is in autonegotiation mode it will send 2ms
2034 		 * of FLPs with at least 6ms of silence.
2035 		 * Add 2ms sleep to have better chances to hit this silence.
2036 		 */
2037 		if (pair_mask)
2038 			msleep(2);
2039 	}
2040 
2041 	*finished = true;
2042 
2043 	return ret;
2044 }
2045 
2046 #define LAN_EXT_PAGE_ACCESS_CONTROL			0x16
2047 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA		0x17
2048 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC		0x4000
2049 
2050 #define LAN8814_QSGMII_SOFT_RESET			0x43
2051 #define LAN8814_QSGMII_SOFT_RESET_BIT			BIT(0)
2052 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG		0x13
2053 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA	BIT(3)
2054 #define LAN8814_ALIGN_SWAP				0x4a
2055 #define LAN8814_ALIGN_TX_A_B_SWAP			0x1
2056 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
2057 
2058 #define LAN8804_ALIGN_SWAP				0x4a
2059 #define LAN8804_ALIGN_TX_A_B_SWAP			0x1
2060 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK			GENMASK(2, 0)
2061 #define LAN8814_CLOCK_MANAGEMENT			0xd
2062 #define LAN8814_LINK_QUALITY				0x8e
2063 
2064 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
2065 {
2066 	int data;
2067 
2068 	phy_lock_mdio_bus(phydev);
2069 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
2070 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
2071 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
2072 		    (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
2073 	data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
2074 	phy_unlock_mdio_bus(phydev);
2075 
2076 	return data;
2077 }
2078 
2079 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
2080 				 u16 val)
2081 {
2082 	phy_lock_mdio_bus(phydev);
2083 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
2084 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
2085 	__phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
2086 		    page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
2087 
2088 	val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
2089 	if (val != 0)
2090 		phydev_err(phydev, "Error: phy_write has returned error %d\n",
2091 			   val);
2092 	phy_unlock_mdio_bus(phydev);
2093 	return val;
2094 }
2095 
2096 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
2097 {
2098 	u16 val = 0;
2099 
2100 	if (enable)
2101 		val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
2102 		      PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
2103 		      PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
2104 		      PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
2105 
2106 	return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
2107 }
2108 
2109 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
2110 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2111 {
2112 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
2113 	*seconds = (*seconds << 16) |
2114 		   lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
2115 
2116 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
2117 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2118 			lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
2119 
2120 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
2121 }
2122 
2123 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
2124 				  u32 *seconds, u32 *nano_seconds, u16 *seq_id)
2125 {
2126 	*seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
2127 	*seconds = *seconds << 16 |
2128 		   lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
2129 
2130 	*nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
2131 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2132 			lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
2133 
2134 	*seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
2135 }
2136 
2137 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
2138 {
2139 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2140 	struct phy_device *phydev = ptp_priv->phydev;
2141 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2142 
2143 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
2144 				SOF_TIMESTAMPING_RX_HARDWARE |
2145 				SOF_TIMESTAMPING_RAW_HARDWARE;
2146 
2147 	info->phc_index = ptp_clock_index(shared->ptp_clock);
2148 
2149 	info->tx_types =
2150 		(1 << HWTSTAMP_TX_OFF) |
2151 		(1 << HWTSTAMP_TX_ON) |
2152 		(1 << HWTSTAMP_TX_ONESTEP_SYNC);
2153 
2154 	info->rx_filters =
2155 		(1 << HWTSTAMP_FILTER_NONE) |
2156 		(1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2157 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2158 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2159 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2160 
2161 	return 0;
2162 }
2163 
2164 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
2165 {
2166 	int i;
2167 
2168 	for (i = 0; i < FIFO_SIZE; ++i)
2169 		lanphy_read_page_reg(phydev, 5,
2170 				     egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
2171 
2172 	/* Read to clear overflow status bit */
2173 	lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2174 }
2175 
2176 static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
2177 {
2178 	struct kszphy_ptp_priv *ptp_priv =
2179 			  container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2180 	struct phy_device *phydev = ptp_priv->phydev;
2181 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2182 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2183 	struct hwtstamp_config config;
2184 	int txcfg = 0, rxcfg = 0;
2185 	int pkt_ts_enable;
2186 
2187 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2188 		return -EFAULT;
2189 
2190 	ptp_priv->hwts_tx_type = config.tx_type;
2191 	ptp_priv->rx_filter = config.rx_filter;
2192 
2193 	switch (config.rx_filter) {
2194 	case HWTSTAMP_FILTER_NONE:
2195 		ptp_priv->layer = 0;
2196 		ptp_priv->version = 0;
2197 		break;
2198 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2199 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2200 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2201 		ptp_priv->layer = PTP_CLASS_L4;
2202 		ptp_priv->version = PTP_CLASS_V2;
2203 		break;
2204 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2205 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2206 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2207 		ptp_priv->layer = PTP_CLASS_L2;
2208 		ptp_priv->version = PTP_CLASS_V2;
2209 		break;
2210 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2211 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2212 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2213 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2214 		ptp_priv->version = PTP_CLASS_V2;
2215 		break;
2216 	default:
2217 		return -ERANGE;
2218 	}
2219 
2220 	if (ptp_priv->layer & PTP_CLASS_L2) {
2221 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2222 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2223 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2224 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2225 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2226 	}
2227 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2228 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2229 
2230 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2231 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2232 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2233 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2234 
2235 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2236 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2237 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2238 
2239 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2240 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2241 	else
2242 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2243 
2244 	mutex_lock(&shared->shared_lock);
2245 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2246 		shared->ref++;
2247 	else
2248 		shared->ref--;
2249 
2250 	if (shared->ref)
2251 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2252 				      PTP_CMD_CTL_PTP_ENABLE_);
2253 	else
2254 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2255 				      PTP_CMD_CTL_PTP_DISABLE_);
2256 	mutex_unlock(&shared->shared_lock);
2257 
2258 	/* In case of multiple starts and stops, these needs to be cleared */
2259 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2260 		list_del(&rx_ts->list);
2261 		kfree(rx_ts);
2262 	}
2263 	skb_queue_purge(&ptp_priv->rx_queue);
2264 	skb_queue_purge(&ptp_priv->tx_queue);
2265 
2266 	lan8814_flush_fifo(ptp_priv->phydev, false);
2267 	lan8814_flush_fifo(ptp_priv->phydev, true);
2268 
2269 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2270 }
2271 
2272 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2273 			     struct sk_buff *skb, int type)
2274 {
2275 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2276 
2277 	switch (ptp_priv->hwts_tx_type) {
2278 	case HWTSTAMP_TX_ONESTEP_SYNC:
2279 		if (ptp_msg_is_sync(skb, type)) {
2280 			kfree_skb(skb);
2281 			return;
2282 		}
2283 		fallthrough;
2284 	case HWTSTAMP_TX_ON:
2285 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2286 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2287 		break;
2288 	case HWTSTAMP_TX_OFF:
2289 	default:
2290 		kfree_skb(skb);
2291 		break;
2292 	}
2293 }
2294 
2295 static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2296 {
2297 	struct ptp_header *ptp_header;
2298 	u32 type;
2299 
2300 	skb_push(skb, ETH_HLEN);
2301 	type = ptp_classify_raw(skb);
2302 	ptp_header = ptp_parse_header(skb, type);
2303 	skb_pull_inline(skb, ETH_HLEN);
2304 
2305 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2306 }
2307 
2308 static bool lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2309 				struct sk_buff *skb)
2310 {
2311 	struct skb_shared_hwtstamps *shhwtstamps;
2312 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2313 	unsigned long flags;
2314 	bool ret = false;
2315 	u16 skb_sig;
2316 
2317 	lan8814_get_sig_rx(skb, &skb_sig);
2318 
2319 	/* Iterate over all RX timestamps and match it with the received skbs */
2320 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2321 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2322 		/* Check if we found the signature we were looking for. */
2323 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2324 			continue;
2325 
2326 		shhwtstamps = skb_hwtstamps(skb);
2327 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2328 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2329 						  rx_ts->nsec);
2330 		list_del(&rx_ts->list);
2331 		kfree(rx_ts);
2332 
2333 		ret = true;
2334 		break;
2335 	}
2336 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2337 
2338 	if (ret)
2339 		netif_rx(skb);
2340 	return ret;
2341 }
2342 
2343 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2344 {
2345 	struct kszphy_ptp_priv *ptp_priv =
2346 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2347 
2348 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2349 	    type == PTP_CLASS_NONE)
2350 		return false;
2351 
2352 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2353 		return false;
2354 
2355 	/* If we failed to match then add it to the queue for when the timestamp
2356 	 * will come
2357 	 */
2358 	if (!lan8814_match_rx_ts(ptp_priv, skb))
2359 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2360 
2361 	return true;
2362 }
2363 
2364 static void lan8814_ptp_clock_set(struct phy_device *phydev,
2365 				  u32 seconds, u32 nano_seconds)
2366 {
2367 	u32 sec_low, sec_high, nsec_low, nsec_high;
2368 
2369 	sec_low = seconds & 0xffff;
2370 	sec_high = (seconds >> 16) & 0xffff;
2371 	nsec_low = nano_seconds & 0xffff;
2372 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2373 
2374 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2375 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2376 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2377 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2378 
2379 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2380 }
2381 
2382 static void lan8814_ptp_clock_get(struct phy_device *phydev,
2383 				  u32 *seconds, u32 *nano_seconds)
2384 {
2385 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2386 
2387 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2388 	*seconds = (*seconds << 16) |
2389 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2390 
2391 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2392 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2393 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2394 }
2395 
2396 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2397 				   struct timespec64 *ts)
2398 {
2399 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2400 							  ptp_clock_info);
2401 	struct phy_device *phydev = shared->phydev;
2402 	u32 nano_seconds;
2403 	u32 seconds;
2404 
2405 	mutex_lock(&shared->shared_lock);
2406 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2407 	mutex_unlock(&shared->shared_lock);
2408 	ts->tv_sec = seconds;
2409 	ts->tv_nsec = nano_seconds;
2410 
2411 	return 0;
2412 }
2413 
2414 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2415 				   const struct timespec64 *ts)
2416 {
2417 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2418 							  ptp_clock_info);
2419 	struct phy_device *phydev = shared->phydev;
2420 
2421 	mutex_lock(&shared->shared_lock);
2422 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2423 	mutex_unlock(&shared->shared_lock);
2424 
2425 	return 0;
2426 }
2427 
2428 static void lan8814_ptp_clock_step(struct phy_device *phydev,
2429 				   s64 time_step_ns)
2430 {
2431 	u32 nano_seconds_step;
2432 	u64 abs_time_step_ns;
2433 	u32 unsigned_seconds;
2434 	u32 nano_seconds;
2435 	u32 remainder;
2436 	s32 seconds;
2437 
2438 	if (time_step_ns >  15000000000LL) {
2439 		/* convert to clock set */
2440 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2441 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2442 						&remainder);
2443 		nano_seconds += remainder;
2444 		if (nano_seconds >= 1000000000) {
2445 			unsigned_seconds++;
2446 			nano_seconds -= 1000000000;
2447 		}
2448 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2449 		return;
2450 	} else if (time_step_ns < -15000000000LL) {
2451 		/* convert to clock set */
2452 		time_step_ns = -time_step_ns;
2453 
2454 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2455 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2456 						&remainder);
2457 		nano_seconds_step = remainder;
2458 		if (nano_seconds < nano_seconds_step) {
2459 			unsigned_seconds--;
2460 			nano_seconds += 1000000000;
2461 		}
2462 		nano_seconds -= nano_seconds_step;
2463 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2464 				      nano_seconds);
2465 		return;
2466 	}
2467 
2468 	/* do clock step */
2469 	if (time_step_ns >= 0) {
2470 		abs_time_step_ns = (u64)time_step_ns;
2471 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2472 					   &remainder);
2473 		nano_seconds = remainder;
2474 	} else {
2475 		abs_time_step_ns = (u64)(-time_step_ns);
2476 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2477 			    &remainder));
2478 		nano_seconds = remainder;
2479 		if (nano_seconds > 0) {
2480 			/* subtracting nano seconds is not allowed
2481 			 * convert to subtracting from seconds,
2482 			 * and adding to nanoseconds
2483 			 */
2484 			seconds--;
2485 			nano_seconds = (1000000000 - nano_seconds);
2486 		}
2487 	}
2488 
2489 	if (nano_seconds > 0) {
2490 		/* add 8 ns to cover the likely normal increment */
2491 		nano_seconds += 8;
2492 	}
2493 
2494 	if (nano_seconds >= 1000000000) {
2495 		/* carry into seconds */
2496 		seconds++;
2497 		nano_seconds -= 1000000000;
2498 	}
2499 
2500 	while (seconds) {
2501 		if (seconds > 0) {
2502 			u32 adjustment_value = (u32)seconds;
2503 			u16 adjustment_value_lo, adjustment_value_hi;
2504 
2505 			if (adjustment_value > 0xF)
2506 				adjustment_value = 0xF;
2507 
2508 			adjustment_value_lo = adjustment_value & 0xffff;
2509 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2510 
2511 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2512 					      adjustment_value_lo);
2513 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2514 					      PTP_LTC_STEP_ADJ_DIR_ |
2515 					      adjustment_value_hi);
2516 			seconds -= ((s32)adjustment_value);
2517 		} else {
2518 			u32 adjustment_value = (u32)(-seconds);
2519 			u16 adjustment_value_lo, adjustment_value_hi;
2520 
2521 			if (adjustment_value > 0xF)
2522 				adjustment_value = 0xF;
2523 
2524 			adjustment_value_lo = adjustment_value & 0xffff;
2525 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2526 
2527 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2528 					      adjustment_value_lo);
2529 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2530 					      adjustment_value_hi);
2531 			seconds += ((s32)adjustment_value);
2532 		}
2533 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2534 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2535 	}
2536 	if (nano_seconds) {
2537 		u16 nano_seconds_lo;
2538 		u16 nano_seconds_hi;
2539 
2540 		nano_seconds_lo = nano_seconds & 0xffff;
2541 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2542 
2543 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2544 				      nano_seconds_lo);
2545 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2546 				      PTP_LTC_STEP_ADJ_DIR_ |
2547 				      nano_seconds_hi);
2548 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2549 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2550 	}
2551 }
2552 
2553 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2554 {
2555 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2556 							  ptp_clock_info);
2557 	struct phy_device *phydev = shared->phydev;
2558 
2559 	mutex_lock(&shared->shared_lock);
2560 	lan8814_ptp_clock_step(phydev, delta);
2561 	mutex_unlock(&shared->shared_lock);
2562 
2563 	return 0;
2564 }
2565 
2566 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2567 {
2568 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2569 							  ptp_clock_info);
2570 	struct phy_device *phydev = shared->phydev;
2571 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2572 	bool positive = true;
2573 	u32 kszphy_rate_adj;
2574 
2575 	if (scaled_ppm < 0) {
2576 		scaled_ppm = -scaled_ppm;
2577 		positive = false;
2578 	}
2579 
2580 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2581 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2582 
2583 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2584 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2585 
2586 	if (positive)
2587 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2588 
2589 	mutex_lock(&shared->shared_lock);
2590 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2591 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2592 	mutex_unlock(&shared->shared_lock);
2593 
2594 	return 0;
2595 }
2596 
2597 static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2598 {
2599 	struct ptp_header *ptp_header;
2600 	u32 type;
2601 
2602 	type = ptp_classify_raw(skb);
2603 	ptp_header = ptp_parse_header(skb, type);
2604 
2605 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2606 }
2607 
2608 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2609 {
2610 	struct phy_device *phydev = ptp_priv->phydev;
2611 	struct skb_shared_hwtstamps shhwtstamps;
2612 	struct sk_buff *skb, *skb_tmp;
2613 	unsigned long flags;
2614 	u32 seconds, nsec;
2615 	bool ret = false;
2616 	u16 skb_sig;
2617 	u16 seq_id;
2618 
2619 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2620 
2621 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2622 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2623 		lan8814_get_sig_tx(skb, &skb_sig);
2624 
2625 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2626 			continue;
2627 
2628 		__skb_unlink(skb, &ptp_priv->tx_queue);
2629 		ret = true;
2630 		break;
2631 	}
2632 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2633 
2634 	if (ret) {
2635 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2636 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2637 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2638 	}
2639 }
2640 
2641 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2642 {
2643 	struct phy_device *phydev = ptp_priv->phydev;
2644 	u32 reg;
2645 
2646 	do {
2647 		lan8814_dequeue_tx_skb(ptp_priv);
2648 
2649 		/* If other timestamps are available in the FIFO,
2650 		 * process them.
2651 		 */
2652 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2653 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2654 }
2655 
2656 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2657 			      struct lan8814_ptp_rx_ts *rx_ts)
2658 {
2659 	struct skb_shared_hwtstamps *shhwtstamps;
2660 	struct sk_buff *skb, *skb_tmp;
2661 	unsigned long flags;
2662 	bool ret = false;
2663 	u16 skb_sig;
2664 
2665 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2666 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2667 		lan8814_get_sig_rx(skb, &skb_sig);
2668 
2669 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2670 			continue;
2671 
2672 		__skb_unlink(skb, &ptp_priv->rx_queue);
2673 
2674 		ret = true;
2675 		break;
2676 	}
2677 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2678 
2679 	if (ret) {
2680 		shhwtstamps = skb_hwtstamps(skb);
2681 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2682 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2683 		netif_rx(skb);
2684 	}
2685 
2686 	return ret;
2687 }
2688 
2689 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2690 {
2691 	struct phy_device *phydev = ptp_priv->phydev;
2692 	struct lan8814_ptp_rx_ts *rx_ts;
2693 	unsigned long flags;
2694 	u32 reg;
2695 
2696 	do {
2697 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2698 		if (!rx_ts)
2699 			return;
2700 
2701 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2702 				      &rx_ts->seq_id);
2703 
2704 		/* If we failed to match the skb add it to the queue for when
2705 		 * the frame will come
2706 		 */
2707 		if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2708 			spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2709 			list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2710 			spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2711 		} else {
2712 			kfree(rx_ts);
2713 		}
2714 
2715 		/* If other timestamps are available in the FIFO,
2716 		 * process them.
2717 		 */
2718 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2719 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2720 }
2721 
2722 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev)
2723 {
2724 	struct kszphy_priv *priv = phydev->priv;
2725 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2726 	u16 status;
2727 
2728 	status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2729 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2730 		lan8814_get_tx_ts(ptp_priv);
2731 
2732 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2733 		lan8814_get_rx_ts(ptp_priv);
2734 
2735 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2736 		lan8814_flush_fifo(phydev, true);
2737 		skb_queue_purge(&ptp_priv->tx_queue);
2738 	}
2739 
2740 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2741 		lan8814_flush_fifo(phydev, false);
2742 		skb_queue_purge(&ptp_priv->rx_queue);
2743 	}
2744 }
2745 
2746 static int lan8804_config_init(struct phy_device *phydev)
2747 {
2748 	int val;
2749 
2750 	/* MDI-X setting for swap A,B transmit */
2751 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
2752 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
2753 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
2754 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
2755 
2756 	/* Make sure that the PHY will not stop generating the clock when the
2757 	 * link partner goes down
2758 	 */
2759 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
2760 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
2761 
2762 	return 0;
2763 }
2764 
2765 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
2766 {
2767 	int status;
2768 
2769 	status = phy_read(phydev, LAN8814_INTS);
2770 	if (status < 0) {
2771 		phy_error(phydev);
2772 		return IRQ_NONE;
2773 	}
2774 
2775 	if (status > 0)
2776 		phy_trigger_machine(phydev);
2777 
2778 	return IRQ_HANDLED;
2779 }
2780 
2781 #define LAN8804_OUTPUT_CONTROL			25
2782 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
2783 #define LAN8804_CONTROL				31
2784 #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
2785 
2786 static int lan8804_config_intr(struct phy_device *phydev)
2787 {
2788 	int err;
2789 
2790 	/* This is an internal PHY of lan966x and is not possible to change the
2791 	 * polarity on the GIC found in lan966x, therefore change the polarity
2792 	 * of the interrupt in the PHY from being active low instead of active
2793 	 * high.
2794 	 */
2795 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
2796 
2797 	/* By default interrupt buffer is open-drain in which case the interrupt
2798 	 * can be active only low. Therefore change the interrupt buffer to be
2799 	 * push-pull to be able to change interrupt polarity
2800 	 */
2801 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
2802 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
2803 
2804 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
2805 		err = phy_read(phydev, LAN8814_INTS);
2806 		if (err < 0)
2807 			return err;
2808 
2809 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
2810 		if (err)
2811 			return err;
2812 	} else {
2813 		err = phy_write(phydev, LAN8814_INTC, 0);
2814 		if (err)
2815 			return err;
2816 
2817 		err = phy_read(phydev, LAN8814_INTS);
2818 		if (err < 0)
2819 			return err;
2820 	}
2821 
2822 	return 0;
2823 }
2824 
2825 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
2826 {
2827 	int irq_status, tsu_irq_status;
2828 	int ret = IRQ_NONE;
2829 
2830 	irq_status = phy_read(phydev, LAN8814_INTS);
2831 	if (irq_status < 0) {
2832 		phy_error(phydev);
2833 		return IRQ_NONE;
2834 	}
2835 
2836 	if (irq_status & LAN8814_INT_LINK) {
2837 		phy_trigger_machine(phydev);
2838 		ret = IRQ_HANDLED;
2839 	}
2840 
2841 	while (1) {
2842 		tsu_irq_status = lanphy_read_page_reg(phydev, 4,
2843 						      LAN8814_INTR_STS_REG);
2844 
2845 		if (tsu_irq_status > 0 &&
2846 		    (tsu_irq_status & (LAN8814_INTR_STS_REG_1588_TSU0_ |
2847 				       LAN8814_INTR_STS_REG_1588_TSU1_ |
2848 				       LAN8814_INTR_STS_REG_1588_TSU2_ |
2849 				       LAN8814_INTR_STS_REG_1588_TSU3_))) {
2850 			lan8814_handle_ptp_interrupt(phydev);
2851 			ret = IRQ_HANDLED;
2852 		} else {
2853 			break;
2854 		}
2855 	}
2856 
2857 	return ret;
2858 }
2859 
2860 static int lan8814_ack_interrupt(struct phy_device *phydev)
2861 {
2862 	/* bit[12..0] int status, which is a read and clear register. */
2863 	int rc;
2864 
2865 	rc = phy_read(phydev, LAN8814_INTS);
2866 
2867 	return (rc < 0) ? rc : 0;
2868 }
2869 
2870 static int lan8814_config_intr(struct phy_device *phydev)
2871 {
2872 	int err;
2873 
2874 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
2875 			      LAN8814_INTR_CTRL_REG_POLARITY |
2876 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
2877 
2878 	/* enable / disable interrupts */
2879 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
2880 		err = lan8814_ack_interrupt(phydev);
2881 		if (err)
2882 			return err;
2883 
2884 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
2885 	} else {
2886 		err = phy_write(phydev, LAN8814_INTC, 0);
2887 		if (err)
2888 			return err;
2889 
2890 		err = lan8814_ack_interrupt(phydev);
2891 	}
2892 
2893 	return err;
2894 }
2895 
2896 static void lan8814_ptp_init(struct phy_device *phydev)
2897 {
2898 	struct kszphy_priv *priv = phydev->priv;
2899 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2900 	u32 temp;
2901 
2902 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
2903 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
2904 		return;
2905 
2906 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
2907 
2908 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
2909 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2910 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
2911 
2912 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
2913 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2914 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
2915 
2916 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
2917 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
2918 
2919 	/* Removing default registers configs related to L2 and IP */
2920 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
2921 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
2922 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
2923 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
2924 
2925 	skb_queue_head_init(&ptp_priv->tx_queue);
2926 	skb_queue_head_init(&ptp_priv->rx_queue);
2927 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
2928 	spin_lock_init(&ptp_priv->rx_ts_lock);
2929 
2930 	ptp_priv->phydev = phydev;
2931 
2932 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
2933 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
2934 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
2935 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
2936 
2937 	phydev->mii_ts = &ptp_priv->mii_ts;
2938 }
2939 
2940 static int lan8814_ptp_probe_once(struct phy_device *phydev)
2941 {
2942 	struct lan8814_shared_priv *shared = phydev->shared->priv;
2943 
2944 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
2945 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
2946 		return 0;
2947 
2948 	/* Initialise shared lock for clock*/
2949 	mutex_init(&shared->shared_lock);
2950 
2951 	shared->ptp_clock_info.owner = THIS_MODULE;
2952 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
2953 	shared->ptp_clock_info.max_adj = 31249999;
2954 	shared->ptp_clock_info.n_alarm = 0;
2955 	shared->ptp_clock_info.n_ext_ts = 0;
2956 	shared->ptp_clock_info.n_pins = 0;
2957 	shared->ptp_clock_info.pps = 0;
2958 	shared->ptp_clock_info.pin_config = NULL;
2959 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
2960 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
2961 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
2962 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
2963 	shared->ptp_clock_info.getcrosststamp = NULL;
2964 
2965 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
2966 					       &phydev->mdio.dev);
2967 	if (IS_ERR_OR_NULL(shared->ptp_clock)) {
2968 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
2969 			   PTR_ERR(shared->ptp_clock));
2970 		return -EINVAL;
2971 	}
2972 
2973 	phydev_dbg(phydev, "successfully registered ptp clock\n");
2974 
2975 	shared->phydev = phydev;
2976 
2977 	/* The EP.4 is shared between all the PHYs in the package and also it
2978 	 * can be accessed by any of the PHYs
2979 	 */
2980 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
2981 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
2982 			      PTP_OPERATING_MODE_STANDALONE_);
2983 
2984 	return 0;
2985 }
2986 
2987 static void lan8814_setup_led(struct phy_device *phydev, int val)
2988 {
2989 	int temp;
2990 
2991 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
2992 
2993 	if (val)
2994 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
2995 	else
2996 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
2997 
2998 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
2999 }
3000 
3001 static int lan8814_config_init(struct phy_device *phydev)
3002 {
3003 	struct kszphy_priv *lan8814 = phydev->priv;
3004 	int val;
3005 
3006 	/* Reset the PHY */
3007 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3008 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3009 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3010 
3011 	/* Disable ANEG with QSGMII PCS Host side */
3012 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3013 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3014 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3015 
3016 	/* MDI-X setting for swap A,B transmit */
3017 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3018 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3019 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3020 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3021 
3022 	if (lan8814->led_mode >= 0)
3023 		lan8814_setup_led(phydev, lan8814->led_mode);
3024 
3025 	return 0;
3026 }
3027 
3028 /* It is expected that there will not be any 'lan8814_take_coma_mode'
3029  * function called in suspend. Because the GPIO line can be shared, so if one of
3030  * the phys goes back in coma mode, then all the other PHYs will go, which is
3031  * wrong.
3032  */
3033 static int lan8814_release_coma_mode(struct phy_device *phydev)
3034 {
3035 	struct gpio_desc *gpiod;
3036 
3037 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
3038 					GPIOD_OUT_HIGH_OPEN_DRAIN |
3039 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3040 	if (IS_ERR(gpiod))
3041 		return PTR_ERR(gpiod);
3042 
3043 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3044 	gpiod_set_value_cansleep(gpiod, 0);
3045 
3046 	return 0;
3047 }
3048 
3049 static int lan8814_probe(struct phy_device *phydev)
3050 {
3051 	const struct kszphy_type *type = phydev->drv->driver_data;
3052 	struct kszphy_priv *priv;
3053 	u16 addr;
3054 	int err;
3055 
3056 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3057 	if (!priv)
3058 		return -ENOMEM;
3059 
3060 	phydev->priv = priv;
3061 
3062 	priv->type = type;
3063 
3064 	kszphy_parse_led_mode(phydev);
3065 
3066 	/* Strap-in value for PHY address, below register read gives starting
3067 	 * phy address value
3068 	 */
3069 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3070 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3071 			      addr, sizeof(struct lan8814_shared_priv));
3072 
3073 	if (phy_package_init_once(phydev)) {
3074 		err = lan8814_release_coma_mode(phydev);
3075 		if (err)
3076 			return err;
3077 
3078 		err = lan8814_ptp_probe_once(phydev);
3079 		if (err)
3080 			return err;
3081 	}
3082 
3083 	lan8814_ptp_init(phydev);
3084 
3085 	return 0;
3086 }
3087 
3088 static struct phy_driver ksphy_driver[] = {
3089 {
3090 	.phy_id		= PHY_ID_KS8737,
3091 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3092 	.name		= "Micrel KS8737",
3093 	/* PHY_BASIC_FEATURES */
3094 	.driver_data	= &ks8737_type,
3095 	.probe		= kszphy_probe,
3096 	.config_init	= kszphy_config_init,
3097 	.config_intr	= kszphy_config_intr,
3098 	.handle_interrupt = kszphy_handle_interrupt,
3099 	.suspend	= kszphy_suspend,
3100 	.resume		= kszphy_resume,
3101 }, {
3102 	.phy_id		= PHY_ID_KSZ8021,
3103 	.phy_id_mask	= 0x00ffffff,
3104 	.name		= "Micrel KSZ8021 or KSZ8031",
3105 	/* PHY_BASIC_FEATURES */
3106 	.driver_data	= &ksz8021_type,
3107 	.probe		= kszphy_probe,
3108 	.config_init	= kszphy_config_init,
3109 	.config_intr	= kszphy_config_intr,
3110 	.handle_interrupt = kszphy_handle_interrupt,
3111 	.get_sset_count = kszphy_get_sset_count,
3112 	.get_strings	= kszphy_get_strings,
3113 	.get_stats	= kszphy_get_stats,
3114 	.suspend	= kszphy_suspend,
3115 	.resume		= kszphy_resume,
3116 }, {
3117 	.phy_id		= PHY_ID_KSZ8031,
3118 	.phy_id_mask	= 0x00ffffff,
3119 	.name		= "Micrel KSZ8031",
3120 	/* PHY_BASIC_FEATURES */
3121 	.driver_data	= &ksz8021_type,
3122 	.probe		= kszphy_probe,
3123 	.config_init	= kszphy_config_init,
3124 	.config_intr	= kszphy_config_intr,
3125 	.handle_interrupt = kszphy_handle_interrupt,
3126 	.get_sset_count = kszphy_get_sset_count,
3127 	.get_strings	= kszphy_get_strings,
3128 	.get_stats	= kszphy_get_stats,
3129 	.suspend	= kszphy_suspend,
3130 	.resume		= kszphy_resume,
3131 }, {
3132 	.phy_id		= PHY_ID_KSZ8041,
3133 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3134 	.name		= "Micrel KSZ8041",
3135 	/* PHY_BASIC_FEATURES */
3136 	.driver_data	= &ksz8041_type,
3137 	.probe		= kszphy_probe,
3138 	.config_init	= ksz8041_config_init,
3139 	.config_aneg	= ksz8041_config_aneg,
3140 	.config_intr	= kszphy_config_intr,
3141 	.handle_interrupt = kszphy_handle_interrupt,
3142 	.get_sset_count = kszphy_get_sset_count,
3143 	.get_strings	= kszphy_get_strings,
3144 	.get_stats	= kszphy_get_stats,
3145 	/* No suspend/resume callbacks because of errata DS80000700A,
3146 	 * receiver error following software power down.
3147 	 */
3148 }, {
3149 	.phy_id		= PHY_ID_KSZ8041RNLI,
3150 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3151 	.name		= "Micrel KSZ8041RNLI",
3152 	/* PHY_BASIC_FEATURES */
3153 	.driver_data	= &ksz8041_type,
3154 	.probe		= kszphy_probe,
3155 	.config_init	= kszphy_config_init,
3156 	.config_intr	= kszphy_config_intr,
3157 	.handle_interrupt = kszphy_handle_interrupt,
3158 	.get_sset_count = kszphy_get_sset_count,
3159 	.get_strings	= kszphy_get_strings,
3160 	.get_stats	= kszphy_get_stats,
3161 	.suspend	= kszphy_suspend,
3162 	.resume		= kszphy_resume,
3163 }, {
3164 	.name		= "Micrel KSZ8051",
3165 	/* PHY_BASIC_FEATURES */
3166 	.driver_data	= &ksz8051_type,
3167 	.probe		= kszphy_probe,
3168 	.config_init	= kszphy_config_init,
3169 	.config_intr	= kszphy_config_intr,
3170 	.handle_interrupt = kszphy_handle_interrupt,
3171 	.get_sset_count = kszphy_get_sset_count,
3172 	.get_strings	= kszphy_get_strings,
3173 	.get_stats	= kszphy_get_stats,
3174 	.match_phy_device = ksz8051_match_phy_device,
3175 	.suspend	= kszphy_suspend,
3176 	.resume		= kszphy_resume,
3177 }, {
3178 	.phy_id		= PHY_ID_KSZ8001,
3179 	.name		= "Micrel KSZ8001 or KS8721",
3180 	.phy_id_mask	= 0x00fffffc,
3181 	/* PHY_BASIC_FEATURES */
3182 	.driver_data	= &ksz8041_type,
3183 	.probe		= kszphy_probe,
3184 	.config_init	= kszphy_config_init,
3185 	.config_intr	= kszphy_config_intr,
3186 	.handle_interrupt = kszphy_handle_interrupt,
3187 	.get_sset_count = kszphy_get_sset_count,
3188 	.get_strings	= kszphy_get_strings,
3189 	.get_stats	= kszphy_get_stats,
3190 	.suspend	= kszphy_suspend,
3191 	.resume		= kszphy_resume,
3192 }, {
3193 	.phy_id		= PHY_ID_KSZ8081,
3194 	.name		= "Micrel KSZ8081 or KSZ8091",
3195 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3196 	.flags		= PHY_POLL_CABLE_TEST,
3197 	/* PHY_BASIC_FEATURES */
3198 	.driver_data	= &ksz8081_type,
3199 	.probe		= kszphy_probe,
3200 	.config_init	= ksz8081_config_init,
3201 	.soft_reset	= genphy_soft_reset,
3202 	.config_aneg	= ksz8081_config_aneg,
3203 	.read_status	= ksz8081_read_status,
3204 	.config_intr	= kszphy_config_intr,
3205 	.handle_interrupt = kszphy_handle_interrupt,
3206 	.get_sset_count = kszphy_get_sset_count,
3207 	.get_strings	= kszphy_get_strings,
3208 	.get_stats	= kszphy_get_stats,
3209 	.suspend	= kszphy_suspend,
3210 	.resume		= kszphy_resume,
3211 	.cable_test_start	= ksz886x_cable_test_start,
3212 	.cable_test_get_status	= ksz886x_cable_test_get_status,
3213 }, {
3214 	.phy_id		= PHY_ID_KSZ8061,
3215 	.name		= "Micrel KSZ8061",
3216 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3217 	/* PHY_BASIC_FEATURES */
3218 	.probe		= kszphy_probe,
3219 	.config_init	= ksz8061_config_init,
3220 	.config_intr	= kszphy_config_intr,
3221 	.handle_interrupt = kszphy_handle_interrupt,
3222 	.suspend	= kszphy_suspend,
3223 	.resume		= kszphy_resume,
3224 }, {
3225 	.phy_id		= PHY_ID_KSZ9021,
3226 	.phy_id_mask	= 0x000ffffe,
3227 	.name		= "Micrel KSZ9021 Gigabit PHY",
3228 	/* PHY_GBIT_FEATURES */
3229 	.driver_data	= &ksz9021_type,
3230 	.probe		= kszphy_probe,
3231 	.get_features	= ksz9031_get_features,
3232 	.config_init	= ksz9021_config_init,
3233 	.config_intr	= kszphy_config_intr,
3234 	.handle_interrupt = kszphy_handle_interrupt,
3235 	.get_sset_count = kszphy_get_sset_count,
3236 	.get_strings	= kszphy_get_strings,
3237 	.get_stats	= kszphy_get_stats,
3238 	.suspend	= kszphy_suspend,
3239 	.resume		= kszphy_resume,
3240 	.read_mmd	= genphy_read_mmd_unsupported,
3241 	.write_mmd	= genphy_write_mmd_unsupported,
3242 }, {
3243 	.phy_id		= PHY_ID_KSZ9031,
3244 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3245 	.name		= "Micrel KSZ9031 Gigabit PHY",
3246 	.flags		= PHY_POLL_CABLE_TEST,
3247 	.driver_data	= &ksz9021_type,
3248 	.probe		= kszphy_probe,
3249 	.get_features	= ksz9031_get_features,
3250 	.config_init	= ksz9031_config_init,
3251 	.soft_reset	= genphy_soft_reset,
3252 	.read_status	= ksz9031_read_status,
3253 	.config_intr	= kszphy_config_intr,
3254 	.handle_interrupt = kszphy_handle_interrupt,
3255 	.get_sset_count = kszphy_get_sset_count,
3256 	.get_strings	= kszphy_get_strings,
3257 	.get_stats	= kszphy_get_stats,
3258 	.suspend	= kszphy_suspend,
3259 	.resume		= kszphy_resume,
3260 	.cable_test_start	= ksz9x31_cable_test_start,
3261 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
3262 }, {
3263 	.phy_id		= PHY_ID_LAN8814,
3264 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3265 	.name		= "Microchip INDY Gigabit Quad PHY",
3266 	.flags          = PHY_POLL_CABLE_TEST,
3267 	.config_init	= lan8814_config_init,
3268 	.driver_data	= &lan8814_type,
3269 	.probe		= lan8814_probe,
3270 	.soft_reset	= genphy_soft_reset,
3271 	.read_status	= ksz9031_read_status,
3272 	.get_sset_count	= kszphy_get_sset_count,
3273 	.get_strings	= kszphy_get_strings,
3274 	.get_stats	= kszphy_get_stats,
3275 	.suspend	= genphy_suspend,
3276 	.resume		= kszphy_resume,
3277 	.config_intr	= lan8814_config_intr,
3278 	.handle_interrupt = lan8814_handle_interrupt,
3279 	.cable_test_start	= lan8814_cable_test_start,
3280 	.cable_test_get_status	= ksz886x_cable_test_get_status,
3281 }, {
3282 	.phy_id		= PHY_ID_LAN8804,
3283 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3284 	.name		= "Microchip LAN966X Gigabit PHY",
3285 	.config_init	= lan8804_config_init,
3286 	.driver_data	= &ksz9021_type,
3287 	.probe		= kszphy_probe,
3288 	.soft_reset	= genphy_soft_reset,
3289 	.read_status	= ksz9031_read_status,
3290 	.get_sset_count	= kszphy_get_sset_count,
3291 	.get_strings	= kszphy_get_strings,
3292 	.get_stats	= kszphy_get_stats,
3293 	.suspend	= genphy_suspend,
3294 	.resume		= kszphy_resume,
3295 	.config_intr	= lan8804_config_intr,
3296 	.handle_interrupt = lan8804_handle_interrupt,
3297 }, {
3298 	.phy_id		= PHY_ID_KSZ9131,
3299 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3300 	.name		= "Microchip KSZ9131 Gigabit PHY",
3301 	/* PHY_GBIT_FEATURES */
3302 	.flags		= PHY_POLL_CABLE_TEST,
3303 	.driver_data	= &ksz9021_type,
3304 	.probe		= kszphy_probe,
3305 	.config_init	= ksz9131_config_init,
3306 	.config_intr	= kszphy_config_intr,
3307 	.handle_interrupt = kszphy_handle_interrupt,
3308 	.get_sset_count = kszphy_get_sset_count,
3309 	.get_strings	= kszphy_get_strings,
3310 	.get_stats	= kszphy_get_stats,
3311 	.suspend	= kszphy_suspend,
3312 	.resume		= kszphy_resume,
3313 	.cable_test_start	= ksz9x31_cable_test_start,
3314 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
3315 }, {
3316 	.phy_id		= PHY_ID_KSZ8873MLL,
3317 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3318 	.name		= "Micrel KSZ8873MLL Switch",
3319 	/* PHY_BASIC_FEATURES */
3320 	.config_init	= kszphy_config_init,
3321 	.config_aneg	= ksz8873mll_config_aneg,
3322 	.read_status	= ksz8873mll_read_status,
3323 	.suspend	= genphy_suspend,
3324 	.resume		= genphy_resume,
3325 }, {
3326 	.phy_id		= PHY_ID_KSZ886X,
3327 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3328 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
3329 	.driver_data	= &ksz886x_type,
3330 	/* PHY_BASIC_FEATURES */
3331 	.flags		= PHY_POLL_CABLE_TEST,
3332 	.config_init	= kszphy_config_init,
3333 	.config_aneg	= ksz886x_config_aneg,
3334 	.read_status	= ksz886x_read_status,
3335 	.suspend	= genphy_suspend,
3336 	.resume		= genphy_resume,
3337 	.cable_test_start	= ksz886x_cable_test_start,
3338 	.cable_test_get_status	= ksz886x_cable_test_get_status,
3339 }, {
3340 	.name		= "Micrel KSZ87XX Switch",
3341 	/* PHY_BASIC_FEATURES */
3342 	.config_init	= kszphy_config_init,
3343 	.match_phy_device = ksz8795_match_phy_device,
3344 	.suspend	= genphy_suspend,
3345 	.resume		= genphy_resume,
3346 }, {
3347 	.phy_id		= PHY_ID_KSZ9477,
3348 	.phy_id_mask	= MICREL_PHY_ID_MASK,
3349 	.name		= "Microchip KSZ9477",
3350 	/* PHY_GBIT_FEATURES */
3351 	.config_init	= kszphy_config_init,
3352 	.config_intr	= kszphy_config_intr,
3353 	.handle_interrupt = kszphy_handle_interrupt,
3354 	.suspend	= genphy_suspend,
3355 	.resume		= genphy_resume,
3356 } };
3357 
3358 module_phy_driver(ksphy_driver);
3359 
3360 MODULE_DESCRIPTION("Micrel PHY driver");
3361 MODULE_AUTHOR("David J. Choi");
3362 MODULE_LICENSE("GPL");
3363 
3364 static struct mdio_device_id __maybe_unused micrel_tbl[] = {
3365 	{ PHY_ID_KSZ9021, 0x000ffffe },
3366 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
3367 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
3368 	{ PHY_ID_KSZ8001, 0x00fffffc },
3369 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
3370 	{ PHY_ID_KSZ8021, 0x00ffffff },
3371 	{ PHY_ID_KSZ8031, 0x00ffffff },
3372 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
3373 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
3374 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
3375 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
3376 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
3377 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
3378 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
3379 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
3380 	{ }
3381 };
3382 
3383 MODULE_DEVICE_TABLE(mdio, micrel_tbl);
3384