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