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