xref: /openbmc/linux/drivers/net/phy/micrel.c (revision 3438dccd)
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 
2392 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2393 		return -EFAULT;
2394 
2395 	ptp_priv->hwts_tx_type = config.tx_type;
2396 	ptp_priv->rx_filter = config.rx_filter;
2397 
2398 	switch (config.rx_filter) {
2399 	case HWTSTAMP_FILTER_NONE:
2400 		ptp_priv->layer = 0;
2401 		ptp_priv->version = 0;
2402 		break;
2403 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2404 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2405 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2406 		ptp_priv->layer = PTP_CLASS_L4;
2407 		ptp_priv->version = PTP_CLASS_V2;
2408 		break;
2409 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2410 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2411 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2412 		ptp_priv->layer = PTP_CLASS_L2;
2413 		ptp_priv->version = PTP_CLASS_V2;
2414 		break;
2415 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2416 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2417 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2418 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
2419 		ptp_priv->version = PTP_CLASS_V2;
2420 		break;
2421 	default:
2422 		return -ERANGE;
2423 	}
2424 
2425 	if (ptp_priv->layer & PTP_CLASS_L2) {
2426 		rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
2427 		txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
2428 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
2429 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
2430 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
2431 	}
2432 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
2433 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
2434 
2435 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
2436 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
2437 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
2438 	lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
2439 
2440 	if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
2441 		lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
2442 				      PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
2443 
2444 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2445 		lan8814_config_ts_intr(ptp_priv->phydev, true);
2446 	else
2447 		lan8814_config_ts_intr(ptp_priv->phydev, false);
2448 
2449 	mutex_lock(&shared->shared_lock);
2450 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
2451 		shared->ref++;
2452 	else
2453 		shared->ref--;
2454 
2455 	if (shared->ref)
2456 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2457 				      PTP_CMD_CTL_PTP_ENABLE_);
2458 	else
2459 		lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
2460 				      PTP_CMD_CTL_PTP_DISABLE_);
2461 	mutex_unlock(&shared->shared_lock);
2462 
2463 	/* In case of multiple starts and stops, these needs to be cleared */
2464 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2465 		list_del(&rx_ts->list);
2466 		kfree(rx_ts);
2467 	}
2468 	skb_queue_purge(&ptp_priv->rx_queue);
2469 	skb_queue_purge(&ptp_priv->tx_queue);
2470 
2471 	lan8814_flush_fifo(ptp_priv->phydev, false);
2472 	lan8814_flush_fifo(ptp_priv->phydev, true);
2473 
2474 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
2475 }
2476 
2477 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
2478 			     struct sk_buff *skb, int type)
2479 {
2480 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2481 
2482 	switch (ptp_priv->hwts_tx_type) {
2483 	case HWTSTAMP_TX_ONESTEP_SYNC:
2484 		if (ptp_msg_is_sync(skb, type)) {
2485 			kfree_skb(skb);
2486 			return;
2487 		}
2488 		fallthrough;
2489 	case HWTSTAMP_TX_ON:
2490 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2491 		skb_queue_tail(&ptp_priv->tx_queue, skb);
2492 		break;
2493 	case HWTSTAMP_TX_OFF:
2494 	default:
2495 		kfree_skb(skb);
2496 		break;
2497 	}
2498 }
2499 
2500 static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2501 {
2502 	struct ptp_header *ptp_header;
2503 	u32 type;
2504 
2505 	skb_push(skb, ETH_HLEN);
2506 	type = ptp_classify_raw(skb);
2507 	ptp_header = ptp_parse_header(skb, type);
2508 	skb_pull_inline(skb, ETH_HLEN);
2509 
2510 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2511 }
2512 
2513 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv *ptp_priv,
2514 				 struct sk_buff *skb)
2515 {
2516 	struct skb_shared_hwtstamps *shhwtstamps;
2517 	struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2518 	unsigned long flags;
2519 	bool ret = false;
2520 	u16 skb_sig;
2521 
2522 	lan8814_get_sig_rx(skb, &skb_sig);
2523 
2524 	/* Iterate over all RX timestamps and match it with the received skbs */
2525 	spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2526 	list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2527 		/* Check if we found the signature we were looking for. */
2528 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2529 			continue;
2530 
2531 		shhwtstamps = skb_hwtstamps(skb);
2532 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2533 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2534 						  rx_ts->nsec);
2535 		list_del(&rx_ts->list);
2536 		kfree(rx_ts);
2537 
2538 		ret = true;
2539 		break;
2540 	}
2541 	spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2542 
2543 	if (ret)
2544 		netif_rx(skb);
2545 	return ret;
2546 }
2547 
2548 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2549 {
2550 	struct kszphy_ptp_priv *ptp_priv =
2551 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2552 
2553 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2554 	    type == PTP_CLASS_NONE)
2555 		return false;
2556 
2557 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2558 		return false;
2559 
2560 	/* If we failed to match then add it to the queue for when the timestamp
2561 	 * will come
2562 	 */
2563 	if (!lan8814_match_rx_skb(ptp_priv, skb))
2564 		skb_queue_tail(&ptp_priv->rx_queue, skb);
2565 
2566 	return true;
2567 }
2568 
2569 static void lan8814_ptp_clock_set(struct phy_device *phydev,
2570 				  u32 seconds, u32 nano_seconds)
2571 {
2572 	u32 sec_low, sec_high, nsec_low, nsec_high;
2573 
2574 	sec_low = seconds & 0xffff;
2575 	sec_high = (seconds >> 16) & 0xffff;
2576 	nsec_low = nano_seconds & 0xffff;
2577 	nsec_high = (nano_seconds >> 16) & 0x3fff;
2578 
2579 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2580 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2581 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2582 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2583 
2584 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2585 }
2586 
2587 static void lan8814_ptp_clock_get(struct phy_device *phydev,
2588 				  u32 *seconds, u32 *nano_seconds)
2589 {
2590 	lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2591 
2592 	*seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2593 	*seconds = (*seconds << 16) |
2594 		   lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2595 
2596 	*nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2597 	*nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2598 			lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2599 }
2600 
2601 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2602 				   struct timespec64 *ts)
2603 {
2604 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2605 							  ptp_clock_info);
2606 	struct phy_device *phydev = shared->phydev;
2607 	u32 nano_seconds;
2608 	u32 seconds;
2609 
2610 	mutex_lock(&shared->shared_lock);
2611 	lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2612 	mutex_unlock(&shared->shared_lock);
2613 	ts->tv_sec = seconds;
2614 	ts->tv_nsec = nano_seconds;
2615 
2616 	return 0;
2617 }
2618 
2619 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2620 				   const struct timespec64 *ts)
2621 {
2622 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2623 							  ptp_clock_info);
2624 	struct phy_device *phydev = shared->phydev;
2625 
2626 	mutex_lock(&shared->shared_lock);
2627 	lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2628 	mutex_unlock(&shared->shared_lock);
2629 
2630 	return 0;
2631 }
2632 
2633 static void lan8814_ptp_clock_step(struct phy_device *phydev,
2634 				   s64 time_step_ns)
2635 {
2636 	u32 nano_seconds_step;
2637 	u64 abs_time_step_ns;
2638 	u32 unsigned_seconds;
2639 	u32 nano_seconds;
2640 	u32 remainder;
2641 	s32 seconds;
2642 
2643 	if (time_step_ns >  15000000000LL) {
2644 		/* convert to clock set */
2645 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2646 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2647 						&remainder);
2648 		nano_seconds += remainder;
2649 		if (nano_seconds >= 1000000000) {
2650 			unsigned_seconds++;
2651 			nano_seconds -= 1000000000;
2652 		}
2653 		lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2654 		return;
2655 	} else if (time_step_ns < -15000000000LL) {
2656 		/* convert to clock set */
2657 		time_step_ns = -time_step_ns;
2658 
2659 		lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2660 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2661 						&remainder);
2662 		nano_seconds_step = remainder;
2663 		if (nano_seconds < nano_seconds_step) {
2664 			unsigned_seconds--;
2665 			nano_seconds += 1000000000;
2666 		}
2667 		nano_seconds -= nano_seconds_step;
2668 		lan8814_ptp_clock_set(phydev, unsigned_seconds,
2669 				      nano_seconds);
2670 		return;
2671 	}
2672 
2673 	/* do clock step */
2674 	if (time_step_ns >= 0) {
2675 		abs_time_step_ns = (u64)time_step_ns;
2676 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2677 					   &remainder);
2678 		nano_seconds = remainder;
2679 	} else {
2680 		abs_time_step_ns = (u64)(-time_step_ns);
2681 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2682 			    &remainder));
2683 		nano_seconds = remainder;
2684 		if (nano_seconds > 0) {
2685 			/* subtracting nano seconds is not allowed
2686 			 * convert to subtracting from seconds,
2687 			 * and adding to nanoseconds
2688 			 */
2689 			seconds--;
2690 			nano_seconds = (1000000000 - nano_seconds);
2691 		}
2692 	}
2693 
2694 	if (nano_seconds > 0) {
2695 		/* add 8 ns to cover the likely normal increment */
2696 		nano_seconds += 8;
2697 	}
2698 
2699 	if (nano_seconds >= 1000000000) {
2700 		/* carry into seconds */
2701 		seconds++;
2702 		nano_seconds -= 1000000000;
2703 	}
2704 
2705 	while (seconds) {
2706 		if (seconds > 0) {
2707 			u32 adjustment_value = (u32)seconds;
2708 			u16 adjustment_value_lo, adjustment_value_hi;
2709 
2710 			if (adjustment_value > 0xF)
2711 				adjustment_value = 0xF;
2712 
2713 			adjustment_value_lo = adjustment_value & 0xffff;
2714 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2715 
2716 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2717 					      adjustment_value_lo);
2718 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2719 					      PTP_LTC_STEP_ADJ_DIR_ |
2720 					      adjustment_value_hi);
2721 			seconds -= ((s32)adjustment_value);
2722 		} else {
2723 			u32 adjustment_value = (u32)(-seconds);
2724 			u16 adjustment_value_lo, adjustment_value_hi;
2725 
2726 			if (adjustment_value > 0xF)
2727 				adjustment_value = 0xF;
2728 
2729 			adjustment_value_lo = adjustment_value & 0xffff;
2730 			adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2731 
2732 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2733 					      adjustment_value_lo);
2734 			lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2735 					      adjustment_value_hi);
2736 			seconds += ((s32)adjustment_value);
2737 		}
2738 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2739 				      PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2740 	}
2741 	if (nano_seconds) {
2742 		u16 nano_seconds_lo;
2743 		u16 nano_seconds_hi;
2744 
2745 		nano_seconds_lo = nano_seconds & 0xffff;
2746 		nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2747 
2748 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2749 				      nano_seconds_lo);
2750 		lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2751 				      PTP_LTC_STEP_ADJ_DIR_ |
2752 				      nano_seconds_hi);
2753 		lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2754 				      PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2755 	}
2756 }
2757 
2758 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2759 {
2760 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2761 							  ptp_clock_info);
2762 	struct phy_device *phydev = shared->phydev;
2763 
2764 	mutex_lock(&shared->shared_lock);
2765 	lan8814_ptp_clock_step(phydev, delta);
2766 	mutex_unlock(&shared->shared_lock);
2767 
2768 	return 0;
2769 }
2770 
2771 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2772 {
2773 	struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2774 							  ptp_clock_info);
2775 	struct phy_device *phydev = shared->phydev;
2776 	u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2777 	bool positive = true;
2778 	u32 kszphy_rate_adj;
2779 
2780 	if (scaled_ppm < 0) {
2781 		scaled_ppm = -scaled_ppm;
2782 		positive = false;
2783 	}
2784 
2785 	kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2786 	kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2787 
2788 	kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2789 	kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2790 
2791 	if (positive)
2792 		kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2793 
2794 	mutex_lock(&shared->shared_lock);
2795 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2796 	lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2797 	mutex_unlock(&shared->shared_lock);
2798 
2799 	return 0;
2800 }
2801 
2802 static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2803 {
2804 	struct ptp_header *ptp_header;
2805 	u32 type;
2806 
2807 	type = ptp_classify_raw(skb);
2808 	ptp_header = ptp_parse_header(skb, type);
2809 
2810 	*sig = (__force u16)(ntohs(ptp_header->sequence_id));
2811 }
2812 
2813 static void lan8814_match_tx_skb(struct kszphy_ptp_priv *ptp_priv,
2814 				 u32 seconds, u32 nsec, u16 seq_id)
2815 {
2816 	struct skb_shared_hwtstamps shhwtstamps;
2817 	struct sk_buff *skb, *skb_tmp;
2818 	unsigned long flags;
2819 	bool ret = false;
2820 	u16 skb_sig;
2821 
2822 	spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2823 	skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2824 		lan8814_get_sig_tx(skb, &skb_sig);
2825 
2826 		if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2827 			continue;
2828 
2829 		__skb_unlink(skb, &ptp_priv->tx_queue);
2830 		ret = true;
2831 		break;
2832 	}
2833 	spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2834 
2835 	if (ret) {
2836 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2837 		shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2838 		skb_complete_tx_timestamp(skb, &shhwtstamps);
2839 	}
2840 }
2841 
2842 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2843 {
2844 	struct phy_device *phydev = ptp_priv->phydev;
2845 	u32 seconds, nsec;
2846 	u16 seq_id;
2847 
2848 	lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2849 	lan8814_match_tx_skb(ptp_priv, seconds, nsec, seq_id);
2850 }
2851 
2852 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2853 {
2854 	struct phy_device *phydev = ptp_priv->phydev;
2855 	u32 reg;
2856 
2857 	do {
2858 		lan8814_dequeue_tx_skb(ptp_priv);
2859 
2860 		/* If other timestamps are available in the FIFO,
2861 		 * process them.
2862 		 */
2863 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2864 	} while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2865 }
2866 
2867 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2868 			      struct lan8814_ptp_rx_ts *rx_ts)
2869 {
2870 	struct skb_shared_hwtstamps *shhwtstamps;
2871 	struct sk_buff *skb, *skb_tmp;
2872 	unsigned long flags;
2873 	bool ret = false;
2874 	u16 skb_sig;
2875 
2876 	spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2877 	skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2878 		lan8814_get_sig_rx(skb, &skb_sig);
2879 
2880 		if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2881 			continue;
2882 
2883 		__skb_unlink(skb, &ptp_priv->rx_queue);
2884 
2885 		ret = true;
2886 		break;
2887 	}
2888 	spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2889 
2890 	if (ret) {
2891 		shhwtstamps = skb_hwtstamps(skb);
2892 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2893 		shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2894 		netif_rx(skb);
2895 	}
2896 
2897 	return ret;
2898 }
2899 
2900 static void lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2901 				struct lan8814_ptp_rx_ts *rx_ts)
2902 {
2903 	unsigned long flags;
2904 
2905 	/* If we failed to match the skb add it to the queue for when
2906 	 * the frame will come
2907 	 */
2908 	if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2909 		spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2910 		list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2911 		spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2912 	} else {
2913 		kfree(rx_ts);
2914 	}
2915 }
2916 
2917 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2918 {
2919 	struct phy_device *phydev = ptp_priv->phydev;
2920 	struct lan8814_ptp_rx_ts *rx_ts;
2921 	u32 reg;
2922 
2923 	do {
2924 		rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2925 		if (!rx_ts)
2926 			return;
2927 
2928 		lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2929 				      &rx_ts->seq_id);
2930 		lan8814_match_rx_ts(ptp_priv, rx_ts);
2931 
2932 		/* If other timestamps are available in the FIFO,
2933 		 * process them.
2934 		 */
2935 		reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2936 	} while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2937 }
2938 
2939 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
2940 {
2941 	struct kszphy_priv *priv = phydev->priv;
2942 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2943 
2944 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2945 		lan8814_get_tx_ts(ptp_priv);
2946 
2947 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2948 		lan8814_get_rx_ts(ptp_priv);
2949 
2950 	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2951 		lan8814_flush_fifo(phydev, true);
2952 		skb_queue_purge(&ptp_priv->tx_queue);
2953 	}
2954 
2955 	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2956 		lan8814_flush_fifo(phydev, false);
2957 		skb_queue_purge(&ptp_priv->rx_queue);
2958 	}
2959 }
2960 
2961 static int lan8804_config_init(struct phy_device *phydev)
2962 {
2963 	int val;
2964 
2965 	/* MDI-X setting for swap A,B transmit */
2966 	val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
2967 	val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
2968 	val |= LAN8804_ALIGN_TX_A_B_SWAP;
2969 	lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
2970 
2971 	/* Make sure that the PHY will not stop generating the clock when the
2972 	 * link partner goes down
2973 	 */
2974 	lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
2975 	lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
2976 
2977 	return 0;
2978 }
2979 
2980 static irqreturn_t lan8804_handle_interrupt(struct phy_device *phydev)
2981 {
2982 	int status;
2983 
2984 	status = phy_read(phydev, LAN8814_INTS);
2985 	if (status < 0) {
2986 		phy_error(phydev);
2987 		return IRQ_NONE;
2988 	}
2989 
2990 	if (status > 0)
2991 		phy_trigger_machine(phydev);
2992 
2993 	return IRQ_HANDLED;
2994 }
2995 
2996 #define LAN8804_OUTPUT_CONTROL			25
2997 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER	BIT(14)
2998 #define LAN8804_CONTROL				31
2999 #define LAN8804_CONTROL_INTR_POLARITY		BIT(14)
3000 
3001 static int lan8804_config_intr(struct phy_device *phydev)
3002 {
3003 	int err;
3004 
3005 	/* This is an internal PHY of lan966x and is not possible to change the
3006 	 * polarity on the GIC found in lan966x, therefore change the polarity
3007 	 * of the interrupt in the PHY from being active low instead of active
3008 	 * high.
3009 	 */
3010 	phy_write(phydev, LAN8804_CONTROL, LAN8804_CONTROL_INTR_POLARITY);
3011 
3012 	/* By default interrupt buffer is open-drain in which case the interrupt
3013 	 * can be active only low. Therefore change the interrupt buffer to be
3014 	 * push-pull to be able to change interrupt polarity
3015 	 */
3016 	phy_write(phydev, LAN8804_OUTPUT_CONTROL,
3017 		  LAN8804_OUTPUT_CONTROL_INTR_BUFFER);
3018 
3019 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3020 		err = phy_read(phydev, LAN8814_INTS);
3021 		if (err < 0)
3022 			return err;
3023 
3024 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3025 		if (err)
3026 			return err;
3027 	} else {
3028 		err = phy_write(phydev, LAN8814_INTC, 0);
3029 		if (err)
3030 			return err;
3031 
3032 		err = phy_read(phydev, LAN8814_INTS);
3033 		if (err < 0)
3034 			return err;
3035 	}
3036 
3037 	return 0;
3038 }
3039 
3040 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
3041 {
3042 	int ret = IRQ_NONE;
3043 	int irq_status;
3044 
3045 	irq_status = phy_read(phydev, LAN8814_INTS);
3046 	if (irq_status < 0) {
3047 		phy_error(phydev);
3048 		return IRQ_NONE;
3049 	}
3050 
3051 	if (irq_status & LAN8814_INT_LINK) {
3052 		phy_trigger_machine(phydev);
3053 		ret = IRQ_HANDLED;
3054 	}
3055 
3056 	while (true) {
3057 		irq_status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
3058 		if (!irq_status)
3059 			break;
3060 
3061 		lan8814_handle_ptp_interrupt(phydev, irq_status);
3062 		ret = IRQ_HANDLED;
3063 	}
3064 
3065 	return ret;
3066 }
3067 
3068 static int lan8814_ack_interrupt(struct phy_device *phydev)
3069 {
3070 	/* bit[12..0] int status, which is a read and clear register. */
3071 	int rc;
3072 
3073 	rc = phy_read(phydev, LAN8814_INTS);
3074 
3075 	return (rc < 0) ? rc : 0;
3076 }
3077 
3078 static int lan8814_config_intr(struct phy_device *phydev)
3079 {
3080 	int err;
3081 
3082 	lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
3083 			      LAN8814_INTR_CTRL_REG_POLARITY |
3084 			      LAN8814_INTR_CTRL_REG_INTR_ENABLE);
3085 
3086 	/* enable / disable interrupts */
3087 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3088 		err = lan8814_ack_interrupt(phydev);
3089 		if (err)
3090 			return err;
3091 
3092 		err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
3093 	} else {
3094 		err = phy_write(phydev, LAN8814_INTC, 0);
3095 		if (err)
3096 			return err;
3097 
3098 		err = lan8814_ack_interrupt(phydev);
3099 	}
3100 
3101 	return err;
3102 }
3103 
3104 static void lan8814_ptp_init(struct phy_device *phydev)
3105 {
3106 	struct kszphy_priv *priv = phydev->priv;
3107 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3108 	u32 temp;
3109 
3110 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
3111 	    !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
3112 		return;
3113 
3114 	lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
3115 
3116 	temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
3117 	temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3118 	lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
3119 
3120 	temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
3121 	temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
3122 	lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
3123 
3124 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
3125 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
3126 
3127 	/* Removing default registers configs related to L2 and IP */
3128 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
3129 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
3130 	lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
3131 	lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
3132 
3133 	/* Disable checking for minorVersionPTP field */
3134 	lanphy_write_page_reg(phydev, 5, PTP_RX_VERSION,
3135 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3136 	lanphy_write_page_reg(phydev, 5, PTP_TX_VERSION,
3137 			      PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3138 
3139 	skb_queue_head_init(&ptp_priv->tx_queue);
3140 	skb_queue_head_init(&ptp_priv->rx_queue);
3141 	INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
3142 	spin_lock_init(&ptp_priv->rx_ts_lock);
3143 
3144 	ptp_priv->phydev = phydev;
3145 
3146 	ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
3147 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
3148 	ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
3149 	ptp_priv->mii_ts.ts_info  = lan8814_ts_info;
3150 
3151 	phydev->mii_ts = &ptp_priv->mii_ts;
3152 }
3153 
3154 static int lan8814_ptp_probe_once(struct phy_device *phydev)
3155 {
3156 	struct lan8814_shared_priv *shared = phydev->shared->priv;
3157 
3158 	/* Initialise shared lock for clock*/
3159 	mutex_init(&shared->shared_lock);
3160 
3161 	shared->ptp_clock_info.owner = THIS_MODULE;
3162 	snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
3163 	shared->ptp_clock_info.max_adj = 31249999;
3164 	shared->ptp_clock_info.n_alarm = 0;
3165 	shared->ptp_clock_info.n_ext_ts = 0;
3166 	shared->ptp_clock_info.n_pins = 0;
3167 	shared->ptp_clock_info.pps = 0;
3168 	shared->ptp_clock_info.pin_config = NULL;
3169 	shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
3170 	shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
3171 	shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
3172 	shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
3173 	shared->ptp_clock_info.getcrosststamp = NULL;
3174 
3175 	shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
3176 					       &phydev->mdio.dev);
3177 	if (IS_ERR(shared->ptp_clock)) {
3178 		phydev_err(phydev, "ptp_clock_register failed %lu\n",
3179 			   PTR_ERR(shared->ptp_clock));
3180 		return -EINVAL;
3181 	}
3182 
3183 	/* Check if PHC support is missing at the configuration level */
3184 	if (!shared->ptp_clock)
3185 		return 0;
3186 
3187 	phydev_dbg(phydev, "successfully registered ptp clock\n");
3188 
3189 	shared->phydev = phydev;
3190 
3191 	/* The EP.4 is shared between all the PHYs in the package and also it
3192 	 * can be accessed by any of the PHYs
3193 	 */
3194 	lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
3195 	lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
3196 			      PTP_OPERATING_MODE_STANDALONE_);
3197 
3198 	return 0;
3199 }
3200 
3201 static void lan8814_setup_led(struct phy_device *phydev, int val)
3202 {
3203 	int temp;
3204 
3205 	temp = lanphy_read_page_reg(phydev, 5, LAN8814_LED_CTRL_1);
3206 
3207 	if (val)
3208 		temp |= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3209 	else
3210 		temp &= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_;
3211 
3212 	lanphy_write_page_reg(phydev, 5, LAN8814_LED_CTRL_1, temp);
3213 }
3214 
3215 static int lan8814_config_init(struct phy_device *phydev)
3216 {
3217 	struct kszphy_priv *lan8814 = phydev->priv;
3218 	int val;
3219 
3220 	/* Reset the PHY */
3221 	val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
3222 	val |= LAN8814_QSGMII_SOFT_RESET_BIT;
3223 	lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
3224 
3225 	/* Disable ANEG with QSGMII PCS Host side */
3226 	val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
3227 	val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
3228 	lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
3229 
3230 	/* MDI-X setting for swap A,B transmit */
3231 	val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
3232 	val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
3233 	val |= LAN8814_ALIGN_TX_A_B_SWAP;
3234 	lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
3235 
3236 	if (lan8814->led_mode >= 0)
3237 		lan8814_setup_led(phydev, lan8814->led_mode);
3238 
3239 	return 0;
3240 }
3241 
3242 /* It is expected that there will not be any 'lan8814_take_coma_mode'
3243  * function called in suspend. Because the GPIO line can be shared, so if one of
3244  * the phys goes back in coma mode, then all the other PHYs will go, which is
3245  * wrong.
3246  */
3247 static int lan8814_release_coma_mode(struct phy_device *phydev)
3248 {
3249 	struct gpio_desc *gpiod;
3250 
3251 	gpiod = devm_gpiod_get_optional(&phydev->mdio.dev, "coma-mode",
3252 					GPIOD_OUT_HIGH_OPEN_DRAIN |
3253 					GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3254 	if (IS_ERR(gpiod))
3255 		return PTR_ERR(gpiod);
3256 
3257 	gpiod_set_consumer_name(gpiod, "LAN8814 coma mode");
3258 	gpiod_set_value_cansleep(gpiod, 0);
3259 
3260 	return 0;
3261 }
3262 
3263 static int lan8814_probe(struct phy_device *phydev)
3264 {
3265 	const struct kszphy_type *type = phydev->drv->driver_data;
3266 	struct kszphy_priv *priv;
3267 	u16 addr;
3268 	int err;
3269 
3270 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3271 	if (!priv)
3272 		return -ENOMEM;
3273 
3274 	phydev->priv = priv;
3275 
3276 	priv->type = type;
3277 
3278 	kszphy_parse_led_mode(phydev);
3279 
3280 	/* Strap-in value for PHY address, below register read gives starting
3281 	 * phy address value
3282 	 */
3283 	addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
3284 	devm_phy_package_join(&phydev->mdio.dev, phydev,
3285 			      addr, sizeof(struct lan8814_shared_priv));
3286 
3287 	if (phy_package_init_once(phydev)) {
3288 		err = lan8814_release_coma_mode(phydev);
3289 		if (err)
3290 			return err;
3291 
3292 		err = lan8814_ptp_probe_once(phydev);
3293 		if (err)
3294 			return err;
3295 	}
3296 
3297 	lan8814_ptp_init(phydev);
3298 
3299 	return 0;
3300 }
3301 
3302 #define LAN8841_MMD_TIMER_REG			0
3303 #define LAN8841_MMD0_REGISTER_17		17
3304 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x)	((x) & 0x3)
3305 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS	BIT(3)
3306 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG	2
3307 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK	BIT(14)
3308 #define LAN8841_MMD_ANALOG_REG			28
3309 #define LAN8841_ANALOG_CONTROL_1		1
3310 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x)	(((x) & 0x3) << 5)
3311 #define LAN8841_ANALOG_CONTROL_10		13
3312 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x)	((x) & 0x3)
3313 #define LAN8841_ANALOG_CONTROL_11		14
3314 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x)	(((x) & 0x7) << 12)
3315 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT	69
3316 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
3317 #define LAN8841_BTRX_POWER_DOWN			70
3318 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A	BIT(0)
3319 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A	BIT(1)
3320 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B	BIT(2)
3321 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B	BIT(3)
3322 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C	BIT(5)
3323 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D	BIT(7)
3324 #define LAN8841_ADC_CHANNEL_MASK		198
3325 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN		370
3326 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN		371
3327 #define LAN8841_PTP_RX_VERSION			374
3328 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN		434
3329 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN		435
3330 #define LAN8841_PTP_TX_VERSION			438
3331 #define LAN8841_PTP_CMD_CTL			256
3332 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE		BIT(2)
3333 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE		BIT(1)
3334 #define LAN8841_PTP_CMD_CTL_PTP_RESET		BIT(0)
3335 #define LAN8841_PTP_RX_PARSE_CONFIG		368
3336 #define LAN8841_PTP_TX_PARSE_CONFIG		432
3337 #define LAN8841_PTP_RX_MODE			381
3338 #define LAN8841_PTP_INSERT_TS_EN		BIT(0)
3339 #define LAN8841_PTP_INSERT_TS_32BIT		BIT(1)
3340 
3341 static int lan8841_config_init(struct phy_device *phydev)
3342 {
3343 	int ret;
3344 
3345 	ret = ksz9131_config_init(phydev);
3346 	if (ret)
3347 		return ret;
3348 
3349 	/* Initialize the HW by resetting everything */
3350 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3351 		       LAN8841_PTP_CMD_CTL,
3352 		       LAN8841_PTP_CMD_CTL_PTP_RESET,
3353 		       LAN8841_PTP_CMD_CTL_PTP_RESET);
3354 
3355 	phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3356 		       LAN8841_PTP_CMD_CTL,
3357 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE,
3358 		       LAN8841_PTP_CMD_CTL_PTP_ENABLE);
3359 
3360 	/* Don't process any frames */
3361 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3362 		      LAN8841_PTP_RX_PARSE_CONFIG, 0);
3363 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3364 		      LAN8841_PTP_TX_PARSE_CONFIG, 0);
3365 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3366 		      LAN8841_PTP_TX_PARSE_L2_ADDR_EN, 0);
3367 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3368 		      LAN8841_PTP_RX_PARSE_L2_ADDR_EN, 0);
3369 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3370 		      LAN8841_PTP_TX_PARSE_IP_ADDR_EN, 0);
3371 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3372 		      LAN8841_PTP_RX_PARSE_IP_ADDR_EN, 0);
3373 
3374 	/* Disable checking for minorVersionPTP field */
3375 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3376 		      LAN8841_PTP_RX_VERSION, 0xff00);
3377 	phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3378 		      LAN8841_PTP_TX_VERSION, 0xff00);
3379 
3380 	/* 100BT Clause 40 improvenent errata */
3381 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3382 		      LAN8841_ANALOG_CONTROL_1,
3383 		      LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
3384 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3385 		      LAN8841_ANALOG_CONTROL_10,
3386 		      LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
3387 
3388 	/* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
3389 	 * Magnetics
3390 	 */
3391 	ret = phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3392 			   LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG);
3393 	if (ret & LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK) {
3394 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3395 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT,
3396 			      LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL);
3397 		phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3398 			      LAN8841_BTRX_POWER_DOWN,
3399 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A |
3400 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_A |
3401 			      LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B |
3402 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_B |
3403 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_C |
3404 			      LAN8841_BTRX_POWER_DOWN_BTRX_CH_D);
3405 	}
3406 
3407 	/* LDO Adjustment errata */
3408 	phy_write_mmd(phydev, LAN8841_MMD_ANALOG_REG,
3409 		      LAN8841_ANALOG_CONTROL_11,
3410 		      LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
3411 
3412 	/* 100BT RGMII latency tuning errata */
3413 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
3414 		      LAN8841_ADC_CHANNEL_MASK, 0x0);
3415 	phy_write_mmd(phydev, LAN8841_MMD_TIMER_REG,
3416 		      LAN8841_MMD0_REGISTER_17,
3417 		      LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
3418 		      LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS);
3419 
3420 	return 0;
3421 }
3422 
3423 #define LAN8841_OUTPUT_CTRL			25
3424 #define LAN8841_OUTPUT_CTRL_INT_BUFFER		BIT(14)
3425 #define LAN8841_INT_PTP				BIT(9)
3426 
3427 static int lan8841_config_intr(struct phy_device *phydev)
3428 {
3429 	int err;
3430 
3431 	phy_modify(phydev, LAN8841_OUTPUT_CTRL,
3432 		   LAN8841_OUTPUT_CTRL_INT_BUFFER, 0);
3433 
3434 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
3435 		err = phy_read(phydev, LAN8814_INTS);
3436 		if (err)
3437 			return err;
3438 
3439 		/* Enable / disable interrupts. It is OK to enable PTP interrupt
3440 		 * even if it PTP is not enabled. Because the underneath blocks
3441 		 * will not enable the PTP so we will never get the PTP
3442 		 * interrupt.
3443 		 */
3444 		err = phy_write(phydev, LAN8814_INTC,
3445 				LAN8814_INT_LINK | LAN8841_INT_PTP);
3446 	} else {
3447 		err = phy_write(phydev, LAN8814_INTC, 0);
3448 		if (err)
3449 			return err;
3450 
3451 		err = phy_read(phydev, LAN8814_INTS);
3452 	}
3453 
3454 	return err;
3455 }
3456 
3457 #define LAN8841_PTP_TX_EGRESS_SEC_LO			453
3458 #define LAN8841_PTP_TX_EGRESS_SEC_HI			452
3459 #define LAN8841_PTP_TX_EGRESS_NS_LO			451
3460 #define LAN8841_PTP_TX_EGRESS_NS_HI			450
3461 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID		BIT(15)
3462 #define LAN8841_PTP_TX_MSG_HEADER2			455
3463 
3464 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv *ptp_priv,
3465 				  u32 *sec, u32 *nsec, u16 *seq)
3466 {
3467 	struct phy_device *phydev = ptp_priv->phydev;
3468 
3469 	*nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_HI);
3470 	if (!(*nsec & LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID))
3471 		return false;
3472 
3473 	*nsec = ((*nsec & 0x3fff) << 16);
3474 	*nsec = *nsec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_NS_LO);
3475 
3476 	*sec = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_HI);
3477 	*sec = *sec << 16;
3478 	*sec = *sec | phy_read_mmd(phydev, 2, LAN8841_PTP_TX_EGRESS_SEC_LO);
3479 
3480 	*seq = phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3481 
3482 	return true;
3483 }
3484 
3485 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv *ptp_priv)
3486 {
3487 	u32 sec, nsec;
3488 	u16 seq;
3489 
3490 	while (lan8841_ptp_get_tx_ts(ptp_priv, &sec, &nsec, &seq))
3491 		lan8814_match_tx_skb(ptp_priv, sec, nsec, seq);
3492 }
3493 
3494 #define LAN8841_PTP_INT_STS			259
3495 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT	BIT(13)
3496 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT	BIT(12)
3497 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT	BIT(2)
3498 
3499 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv *ptp_priv)
3500 {
3501 	struct phy_device *phydev = ptp_priv->phydev;
3502 	int i;
3503 
3504 	for (i = 0; i < FIFO_SIZE; ++i)
3505 		phy_read_mmd(phydev, 2, LAN8841_PTP_TX_MSG_HEADER2);
3506 
3507 	phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3508 }
3509 
3510 #define LAN8841_PTP_GPIO_CAP_STS			506
3511 #define LAN8841_PTP_GPIO_SEL				327
3512 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio)		((gpio) << 8)
3513 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP		498
3514 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP		499
3515 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP		500
3516 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP		501
3517 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP		502
3518 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP		503
3519 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP		504
3520 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP		505
3521 
3522 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv *ptp_priv)
3523 {
3524 	struct phy_device *phydev = ptp_priv->phydev;
3525 	struct ptp_clock_event ptp_event = {0};
3526 	int pin, ret, tmp;
3527 	s32 sec, nsec;
3528 
3529 	pin = ptp_find_pin_unlocked(ptp_priv->ptp_clock, PTP_PF_EXTTS, 0);
3530 	if (pin == -1)
3531 		return;
3532 
3533 	tmp = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_STS);
3534 	if (tmp < 0)
3535 		return;
3536 
3537 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL,
3538 			    LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin));
3539 	if (ret)
3540 		return;
3541 
3542 	mutex_lock(&ptp_priv->ptp_lock);
3543 	if (tmp & BIT(pin)) {
3544 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP);
3545 		sec <<= 16;
3546 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP);
3547 
3548 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP) & 0x3fff;
3549 		nsec <<= 16;
3550 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP);
3551 	} else {
3552 		sec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP);
3553 		sec <<= 16;
3554 		sec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP);
3555 
3556 		nsec = phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP) & 0x3fff;
3557 		nsec <<= 16;
3558 		nsec |= phy_read_mmd(phydev, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP);
3559 	}
3560 	mutex_unlock(&ptp_priv->ptp_lock);
3561 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_SEL, 0);
3562 	if (ret)
3563 		return;
3564 
3565 	ptp_event.index = 0;
3566 	ptp_event.timestamp = ktime_set(sec, nsec);
3567 	ptp_event.type = PTP_CLOCK_EXTTS;
3568 	ptp_clock_event(ptp_priv->ptp_clock, &ptp_event);
3569 }
3570 
3571 static void lan8841_handle_ptp_interrupt(struct phy_device *phydev)
3572 {
3573 	struct kszphy_priv *priv = phydev->priv;
3574 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
3575 	u16 status;
3576 
3577 	do {
3578 		status = phy_read_mmd(phydev, 2, LAN8841_PTP_INT_STS);
3579 
3580 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_INT)
3581 			lan8841_ptp_process_tx_ts(ptp_priv);
3582 
3583 		if (status & LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT)
3584 			lan8841_gpio_process_cap(ptp_priv);
3585 
3586 		if (status & LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT) {
3587 			lan8841_ptp_flush_fifo(ptp_priv);
3588 			skb_queue_purge(&ptp_priv->tx_queue);
3589 		}
3590 
3591 	} while (status & (LAN8841_PTP_INT_STS_PTP_TX_TS_INT |
3592 			   LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT |
3593 			   LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT));
3594 }
3595 
3596 #define LAN8841_INTS_PTP		BIT(9)
3597 
3598 static irqreturn_t lan8841_handle_interrupt(struct phy_device *phydev)
3599 {
3600 	irqreturn_t ret = IRQ_NONE;
3601 	int irq_status;
3602 
3603 	irq_status = phy_read(phydev, LAN8814_INTS);
3604 	if (irq_status < 0) {
3605 		phy_error(phydev);
3606 		return IRQ_NONE;
3607 	}
3608 
3609 	if (irq_status & LAN8814_INT_LINK) {
3610 		phy_trigger_machine(phydev);
3611 		ret = IRQ_HANDLED;
3612 	}
3613 
3614 	if (irq_status & LAN8841_INTS_PTP) {
3615 		lan8841_handle_ptp_interrupt(phydev);
3616 		ret = IRQ_HANDLED;
3617 	}
3618 
3619 	return ret;
3620 }
3621 
3622 static int lan8841_ts_info(struct mii_timestamper *mii_ts,
3623 			   struct ethtool_ts_info *info)
3624 {
3625 	struct kszphy_ptp_priv *ptp_priv;
3626 
3627 	ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3628 
3629 	info->phc_index = ptp_priv->ptp_clock ?
3630 				ptp_clock_index(ptp_priv->ptp_clock) : -1;
3631 	if (info->phc_index == -1)
3632 		return 0;
3633 
3634 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
3635 				SOF_TIMESTAMPING_RX_HARDWARE |
3636 				SOF_TIMESTAMPING_RAW_HARDWARE;
3637 
3638 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3639 			 (1 << HWTSTAMP_TX_ON) |
3640 			 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
3641 
3642 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3643 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
3644 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3645 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
3646 
3647 	return 0;
3648 }
3649 
3650 #define LAN8841_PTP_INT_EN			260
3651 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN	BIT(13)
3652 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN		BIT(12)
3653 
3654 static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv *ptp_priv,
3655 					  bool enable)
3656 {
3657 	struct phy_device *phydev = ptp_priv->phydev;
3658 
3659 	if (enable) {
3660 		/* Enable interrupts on the TX side */
3661 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3662 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3663 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN,
3664 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3665 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN);
3666 
3667 		/* Enable the modification of the frame on RX side,
3668 		 * this will add the ns and 2 bits of sec in the reserved field
3669 		 * of the PTP header
3670 		 */
3671 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3672 			       LAN8841_PTP_RX_MODE,
3673 			       LAN8841_PTP_INSERT_TS_EN |
3674 			       LAN8841_PTP_INSERT_TS_32BIT,
3675 			       LAN8841_PTP_INSERT_TS_EN |
3676 			       LAN8841_PTP_INSERT_TS_32BIT);
3677 
3678 		ptp_schedule_worker(ptp_priv->ptp_clock, 0);
3679 	} else {
3680 		/* Disable interrupts on the TX side */
3681 		phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
3682 			       LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN |
3683 			       LAN8841_PTP_INT_EN_PTP_TX_TS_EN, 0);
3684 
3685 		/* Disable modification of the RX frames */
3686 		phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
3687 			       LAN8841_PTP_RX_MODE,
3688 			       LAN8841_PTP_INSERT_TS_EN |
3689 			       LAN8841_PTP_INSERT_TS_32BIT, 0);
3690 
3691 		ptp_cancel_worker_sync(ptp_priv->ptp_clock);
3692 	}
3693 }
3694 
3695 #define LAN8841_PTP_RX_TIMESTAMP_EN		379
3696 #define LAN8841_PTP_TX_TIMESTAMP_EN		443
3697 #define LAN8841_PTP_TX_MOD			445
3698 
3699 static int lan8841_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
3700 {
3701 	struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3702 	struct phy_device *phydev = ptp_priv->phydev;
3703 	struct hwtstamp_config config;
3704 	int txcfg = 0, rxcfg = 0;
3705 	int pkt_ts_enable;
3706 
3707 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3708 		return -EFAULT;
3709 
3710 	ptp_priv->hwts_tx_type = config.tx_type;
3711 	ptp_priv->rx_filter = config.rx_filter;
3712 
3713 	switch (config.rx_filter) {
3714 	case HWTSTAMP_FILTER_NONE:
3715 		ptp_priv->layer = 0;
3716 		ptp_priv->version = 0;
3717 		break;
3718 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3719 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3720 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3721 		ptp_priv->layer = PTP_CLASS_L4;
3722 		ptp_priv->version = PTP_CLASS_V2;
3723 		break;
3724 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3725 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3726 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3727 		ptp_priv->layer = PTP_CLASS_L2;
3728 		ptp_priv->version = PTP_CLASS_V2;
3729 		break;
3730 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3731 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3732 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3733 		ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
3734 		ptp_priv->version = PTP_CLASS_V2;
3735 		break;
3736 	default:
3737 		return -ERANGE;
3738 	}
3739 
3740 	/* Setup parsing of the frames and enable the timestamping for ptp
3741 	 * frames
3742 	 */
3743 	if (ptp_priv->layer & PTP_CLASS_L2) {
3744 		rxcfg |= PTP_RX_PARSE_CONFIG_LAYER2_EN_;
3745 		txcfg |= PTP_TX_PARSE_CONFIG_LAYER2_EN_;
3746 	} else if (ptp_priv->layer & PTP_CLASS_L4) {
3747 		rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
3748 		txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
3749 	}
3750 
3751 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_PARSE_CONFIG, rxcfg);
3752 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_PARSE_CONFIG, txcfg);
3753 
3754 	pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
3755 			PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
3756 	phy_write_mmd(phydev, 2, LAN8841_PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
3757 	phy_write_mmd(phydev, 2, LAN8841_PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
3758 
3759 	/* Enable / disable of the TX timestamp in the SYNC frames */
3760 	phy_modify_mmd(phydev, 2, LAN8841_PTP_TX_MOD,
3761 		       PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_,
3762 		       ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC ?
3763 				PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ : 0);
3764 
3765 	/* Now enable/disable the timestamping */
3766 	lan8841_ptp_enable_processing(ptp_priv,
3767 				      config.rx_filter != HWTSTAMP_FILTER_NONE);
3768 
3769 	skb_queue_purge(&ptp_priv->tx_queue);
3770 
3771 	lan8841_ptp_flush_fifo(ptp_priv);
3772 
3773 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
3774 }
3775 
3776 static bool lan8841_rxtstamp(struct mii_timestamper *mii_ts,
3777 			     struct sk_buff *skb, int type)
3778 {
3779 	struct kszphy_ptp_priv *ptp_priv =
3780 			container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
3781 	struct ptp_header *header = ptp_parse_header(skb, type);
3782 	struct skb_shared_hwtstamps *shhwtstamps;
3783 	struct timespec64 ts;
3784 	unsigned long flags;
3785 	u32 ts_header;
3786 
3787 	if (!header)
3788 		return false;
3789 
3790 	if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
3791 	    type == PTP_CLASS_NONE)
3792 		return false;
3793 
3794 	if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
3795 		return false;
3796 
3797 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
3798 	ts.tv_sec = ptp_priv->seconds;
3799 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
3800 	ts_header = __be32_to_cpu(header->reserved2);
3801 
3802 	shhwtstamps = skb_hwtstamps(skb);
3803 	memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3804 
3805 	/* Check for any wrap arounds for the second part */
3806 	if ((ts.tv_sec & GENMASK(1, 0)) == 0 && (ts_header >> 30) == 3)
3807 		ts.tv_sec -= GENMASK(1, 0) + 1;
3808 	else if ((ts.tv_sec & GENMASK(1, 0)) == 3 && (ts_header >> 30) == 0)
3809 		ts.tv_sec += 1;
3810 
3811 	shhwtstamps->hwtstamp =
3812 		ktime_set((ts.tv_sec & ~(GENMASK(1, 0))) | ts_header >> 30,
3813 			  ts_header & GENMASK(29, 0));
3814 	header->reserved2 = 0;
3815 
3816 	netif_rx(skb);
3817 
3818 	return true;
3819 }
3820 
3821 #define LAN8841_EVENT_A		0
3822 #define LAN8841_EVENT_B		1
3823 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 278 : 288)
3824 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 279 : 289)
3825 #define LAN8841_PTP_LTC_TARGET_NS_HI(event)	((event) == LAN8841_EVENT_A ? 280 : 290)
3826 #define LAN8841_PTP_LTC_TARGET_NS_LO(event)	((event) == LAN8841_EVENT_A ? 281 : 291)
3827 
3828 static int lan8841_ptp_set_target(struct kszphy_ptp_priv *ptp_priv, u8 event,
3829 				  s64 sec, u32 nsec)
3830 {
3831 	struct phy_device *phydev = ptp_priv->phydev;
3832 	int ret;
3833 
3834 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event),
3835 			    upper_16_bits(sec));
3836 	if (ret)
3837 		return ret;
3838 
3839 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event),
3840 			    lower_16_bits(sec));
3841 	if (ret)
3842 		return ret;
3843 
3844 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event) & 0x3fff,
3845 			    upper_16_bits(nsec));
3846 	if (ret)
3847 		return ret;
3848 
3849 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event),
3850 			    lower_16_bits(nsec));
3851 }
3852 
3853 #define LAN8841_BUFFER_TIME	2
3854 
3855 static int lan8841_ptp_update_target(struct kszphy_ptp_priv *ptp_priv,
3856 				     const struct timespec64 *ts)
3857 {
3858 	return lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A,
3859 				      ts->tv_sec + LAN8841_BUFFER_TIME, 0);
3860 }
3861 
3862 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event)	((event) == LAN8841_EVENT_A ? 282 : 292)
3863 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event)	((event) == LAN8841_EVENT_A ? 283 : 293)
3864 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event)	((event) == LAN8841_EVENT_A ? 284 : 294)
3865 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event)	((event) == LAN8841_EVENT_A ? 285 : 295)
3866 
3867 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv *ptp_priv, u8 event,
3868 				  s64 sec, u32 nsec)
3869 {
3870 	struct phy_device *phydev = ptp_priv->phydev;
3871 	int ret;
3872 
3873 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event),
3874 			    upper_16_bits(sec));
3875 	if (ret)
3876 		return ret;
3877 
3878 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event),
3879 			    lower_16_bits(sec));
3880 	if (ret)
3881 		return ret;
3882 
3883 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) & 0x3fff,
3884 			    upper_16_bits(nsec));
3885 	if (ret)
3886 		return ret;
3887 
3888 	return phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event),
3889 			     lower_16_bits(nsec));
3890 }
3891 
3892 #define LAN8841_PTP_LTC_SET_SEC_HI	262
3893 #define LAN8841_PTP_LTC_SET_SEC_MID	263
3894 #define LAN8841_PTP_LTC_SET_SEC_LO	264
3895 #define LAN8841_PTP_LTC_SET_NS_HI	265
3896 #define LAN8841_PTP_LTC_SET_NS_LO	266
3897 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD	BIT(4)
3898 
3899 static int lan8841_ptp_settime64(struct ptp_clock_info *ptp,
3900 				 const struct timespec64 *ts)
3901 {
3902 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3903 							ptp_clock_info);
3904 	struct phy_device *phydev = ptp_priv->phydev;
3905 	unsigned long flags;
3906 	int ret;
3907 
3908 	/* Set the value to be stored */
3909 	mutex_lock(&ptp_priv->ptp_lock);
3910 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_LO, lower_16_bits(ts->tv_sec));
3911 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_MID, upper_16_bits(ts->tv_sec));
3912 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_SEC_HI, upper_32_bits(ts->tv_sec) & 0xffff);
3913 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_LO, lower_16_bits(ts->tv_nsec));
3914 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_SET_NS_HI, upper_16_bits(ts->tv_nsec) & 0x3fff);
3915 
3916 	/* Set the command to load the LTC */
3917 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3918 		      LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD);
3919 	ret = lan8841_ptp_update_target(ptp_priv, ts);
3920 	mutex_unlock(&ptp_priv->ptp_lock);
3921 
3922 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
3923 	ptp_priv->seconds = ts->tv_sec;
3924 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
3925 
3926 	return ret;
3927 }
3928 
3929 #define LAN8841_PTP_LTC_RD_SEC_HI	358
3930 #define LAN8841_PTP_LTC_RD_SEC_MID	359
3931 #define LAN8841_PTP_LTC_RD_SEC_LO	360
3932 #define LAN8841_PTP_LTC_RD_NS_HI	361
3933 #define LAN8841_PTP_LTC_RD_NS_LO	362
3934 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ	BIT(3)
3935 
3936 static int lan8841_ptp_gettime64(struct ptp_clock_info *ptp,
3937 				 struct timespec64 *ts)
3938 {
3939 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3940 							ptp_clock_info);
3941 	struct phy_device *phydev = ptp_priv->phydev;
3942 	time64_t s;
3943 	s64 ns;
3944 
3945 	mutex_lock(&ptp_priv->ptp_lock);
3946 	/* Issue the command to read the LTC */
3947 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3948 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3949 
3950 	/* Read the LTC */
3951 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3952 	s <<= 16;
3953 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3954 	s <<= 16;
3955 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3956 
3957 	ns = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_HI) & 0x3fff;
3958 	ns <<= 16;
3959 	ns |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_NS_LO);
3960 	mutex_unlock(&ptp_priv->ptp_lock);
3961 
3962 	set_normalized_timespec64(ts, s, ns);
3963 	return 0;
3964 }
3965 
3966 static void lan8841_ptp_getseconds(struct ptp_clock_info *ptp,
3967 				   struct timespec64 *ts)
3968 {
3969 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3970 							ptp_clock_info);
3971 	struct phy_device *phydev = ptp_priv->phydev;
3972 	time64_t s;
3973 
3974 	mutex_lock(&ptp_priv->ptp_lock);
3975 	/* Issue the command to read the LTC */
3976 	phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
3977 		      LAN8841_PTP_CMD_CTL_PTP_LTC_READ);
3978 
3979 	/* Read the LTC */
3980 	s = phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_HI);
3981 	s <<= 16;
3982 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_MID);
3983 	s <<= 16;
3984 	s |= phy_read_mmd(phydev, 2, LAN8841_PTP_LTC_RD_SEC_LO);
3985 	mutex_unlock(&ptp_priv->ptp_lock);
3986 
3987 	set_normalized_timespec64(ts, s, 0);
3988 }
3989 
3990 #define LAN8841_PTP_LTC_STEP_ADJ_LO			276
3991 #define LAN8841_PTP_LTC_STEP_ADJ_HI			275
3992 #define LAN8841_PTP_LTC_STEP_ADJ_DIR			BIT(15)
3993 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS	BIT(5)
3994 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS	BIT(6)
3995 
3996 static int lan8841_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
3997 {
3998 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
3999 							ptp_clock_info);
4000 	struct phy_device *phydev = ptp_priv->phydev;
4001 	struct timespec64 ts;
4002 	bool add = true;
4003 	u32 nsec;
4004 	s32 sec;
4005 	int ret;
4006 
4007 	/* The HW allows up to 15 sec to adjust the time, but here we limit to
4008 	 * 10 sec the adjustment. The reason is, in case the adjustment is 14
4009 	 * sec and 999999999 nsec, then we add 8ns to compansate the actual
4010 	 * increment so the value can be bigger than 15 sec. Therefore limit the
4011 	 * possible adjustments so we will not have these corner cases
4012 	 */
4013 	if (delta > 10000000000LL || delta < -10000000000LL) {
4014 		/* The timeadjustment is too big, so fall back using set time */
4015 		u64 now;
4016 
4017 		ptp->gettime64(ptp, &ts);
4018 
4019 		now = ktime_to_ns(timespec64_to_ktime(ts));
4020 		ts = ns_to_timespec64(now + delta);
4021 
4022 		ptp->settime64(ptp, &ts);
4023 		return 0;
4024 	}
4025 
4026 	sec = div_u64_rem(delta < 0 ? -delta : delta, NSEC_PER_SEC, &nsec);
4027 	if (delta < 0 && nsec != 0) {
4028 		/* It is not allowed to adjust low the nsec part, therefore
4029 		 * subtract more from second part and add to nanosecond such
4030 		 * that would roll over, so the second part will increase
4031 		 */
4032 		sec--;
4033 		nsec = NSEC_PER_SEC - nsec;
4034 	}
4035 
4036 	/* Calculate the adjustments and the direction */
4037 	if (delta < 0)
4038 		add = false;
4039 
4040 	if (nsec > 0)
4041 		/* add 8 ns to cover the likely normal increment */
4042 		nsec += 8;
4043 
4044 	if (nsec >= NSEC_PER_SEC) {
4045 		/* carry into seconds */
4046 		sec++;
4047 		nsec -= NSEC_PER_SEC;
4048 	}
4049 
4050 	mutex_lock(&ptp_priv->ptp_lock);
4051 	if (sec) {
4052 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO, sec);
4053 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4054 			      add ? LAN8841_PTP_LTC_STEP_ADJ_DIR : 0);
4055 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4056 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS);
4057 	}
4058 
4059 	if (nsec) {
4060 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_LO,
4061 			      nsec & 0xffff);
4062 		phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_STEP_ADJ_HI,
4063 			      (nsec >> 16) & 0x3fff);
4064 		phy_write_mmd(phydev, 2, LAN8841_PTP_CMD_CTL,
4065 			      LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS);
4066 	}
4067 	mutex_unlock(&ptp_priv->ptp_lock);
4068 
4069 	/* Update the target clock */
4070 	ptp->gettime64(ptp, &ts);
4071 	mutex_lock(&ptp_priv->ptp_lock);
4072 	ret = lan8841_ptp_update_target(ptp_priv, &ts);
4073 	mutex_unlock(&ptp_priv->ptp_lock);
4074 
4075 	return ret;
4076 }
4077 
4078 #define LAN8841_PTP_LTC_RATE_ADJ_HI		269
4079 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR		BIT(15)
4080 #define LAN8841_PTP_LTC_RATE_ADJ_LO		270
4081 
4082 static int lan8841_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
4083 {
4084 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4085 							ptp_clock_info);
4086 	struct phy_device *phydev = ptp_priv->phydev;
4087 	bool faster = true;
4088 	u32 rate;
4089 
4090 	if (!scaled_ppm)
4091 		return 0;
4092 
4093 	if (scaled_ppm < 0) {
4094 		scaled_ppm = -scaled_ppm;
4095 		faster = false;
4096 	}
4097 
4098 	rate = LAN8814_1PPM_FORMAT * (upper_16_bits(scaled_ppm));
4099 	rate += (LAN8814_1PPM_FORMAT * (lower_16_bits(scaled_ppm))) >> 16;
4100 
4101 	mutex_lock(&ptp_priv->ptp_lock);
4102 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_HI,
4103 		      faster ? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR | (upper_16_bits(rate) & 0x3fff)
4104 			     : upper_16_bits(rate) & 0x3fff);
4105 	phy_write_mmd(phydev, 2, LAN8841_PTP_LTC_RATE_ADJ_LO, lower_16_bits(rate));
4106 	mutex_unlock(&ptp_priv->ptp_lock);
4107 
4108 	return 0;
4109 }
4110 
4111 static int lan8841_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
4112 			      enum ptp_pin_function func, unsigned int chan)
4113 {
4114 	switch (func) {
4115 	case PTP_PF_NONE:
4116 	case PTP_PF_PEROUT:
4117 	case PTP_PF_EXTTS:
4118 		break;
4119 	default:
4120 		return -1;
4121 	}
4122 
4123 	return 0;
4124 }
4125 
4126 #define LAN8841_PTP_GPIO_NUM	10
4127 #define LAN8841_GPIO_EN		128
4128 #define LAN8841_GPIO_DIR	129
4129 #define LAN8841_GPIO_BUF	130
4130 
4131 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4132 {
4133 	struct phy_device *phydev = ptp_priv->phydev;
4134 	int ret;
4135 
4136 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4137 	if (ret)
4138 		return ret;
4139 
4140 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4141 	if (ret)
4142 		return ret;
4143 
4144 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4145 }
4146 
4147 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv *ptp_priv, int pin)
4148 {
4149 	struct phy_device *phydev = ptp_priv->phydev;
4150 	int ret;
4151 
4152 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4153 	if (ret)
4154 		return ret;
4155 
4156 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DIR, BIT(pin));
4157 	if (ret)
4158 		return ret;
4159 
4160 	return phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4161 }
4162 
4163 #define LAN8841_GPIO_DATA_SEL1				131
4164 #define LAN8841_GPIO_DATA_SEL2				132
4165 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK	GENMASK(2, 0)
4166 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A	1
4167 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B	2
4168 #define LAN8841_PTP_GENERAL_CONFIG			257
4169 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A	BIT(1)
4170 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B	BIT(3)
4171 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK	GENMASK(7, 4)
4172 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK	GENMASK(11, 8)
4173 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A		4
4174 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B		7
4175 
4176 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4177 				    u8 event)
4178 {
4179 	struct phy_device *phydev = ptp_priv->phydev;
4180 	u16 tmp;
4181 	int ret;
4182 
4183 	/* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4184 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4185 	 * depending on the pin, it requires to read a different register
4186 	 */
4187 	if (pin < 5) {
4188 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * pin);
4189 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1, tmp);
4190 	} else {
4191 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK << (3 * (pin - 5));
4192 		ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2, tmp);
4193 	}
4194 	if (ret)
4195 		return ret;
4196 
4197 	/* Disable the event */
4198 	if (event == LAN8841_EVENT_A)
4199 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4200 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK;
4201 	else
4202 		tmp = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4203 		      LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK;
4204 	return phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, tmp);
4205 }
4206 
4207 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv *ptp_priv, int pin,
4208 				    u8 event, int pulse_width)
4209 {
4210 	struct phy_device *phydev = ptp_priv->phydev;
4211 	u16 tmp;
4212 	int ret;
4213 
4214 	/* Enable the event */
4215 	if (event == LAN8841_EVENT_A)
4216 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4217 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4218 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK,
4219 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A |
4220 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A);
4221 	else
4222 		ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GENERAL_CONFIG,
4223 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4224 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK,
4225 				     LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B |
4226 				     pulse_width << LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B);
4227 	if (ret)
4228 		return ret;
4229 
4230 	/* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4231 	 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4232 	 * depending on the pin, it requires to read a different register
4233 	 */
4234 	if (event == LAN8841_EVENT_A)
4235 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A;
4236 	else
4237 		tmp = LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B;
4238 
4239 	if (pin < 5)
4240 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL1,
4241 				       tmp << (3 * pin));
4242 	else
4243 		ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_DATA_SEL2,
4244 				       tmp << (3 * (pin - 5)));
4245 
4246 	return ret;
4247 }
4248 
4249 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS	13
4250 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS	12
4251 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS	11
4252 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS	10
4253 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS	9
4254 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS	8
4255 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US	7
4256 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US	6
4257 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US	5
4258 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US	4
4259 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US	3
4260 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US	2
4261 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS	1
4262 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS	0
4263 
4264 static int lan8841_ptp_perout(struct ptp_clock_info *ptp,
4265 			      struct ptp_clock_request *rq, int on)
4266 {
4267 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4268 							ptp_clock_info);
4269 	struct phy_device *phydev = ptp_priv->phydev;
4270 	struct timespec64 ts_on, ts_period;
4271 	s64 on_nsec, period_nsec;
4272 	int pulse_width;
4273 	int pin;
4274 	int ret;
4275 
4276 	if (rq->perout.flags & ~PTP_PEROUT_DUTY_CYCLE)
4277 		return -EOPNOTSUPP;
4278 
4279 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_PEROUT, rq->perout.index);
4280 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4281 		return -EINVAL;
4282 
4283 	if (!on) {
4284 		ret = lan8841_ptp_perout_off(ptp_priv, pin);
4285 		if (ret)
4286 			return ret;
4287 
4288 		return lan8841_ptp_remove_event(ptp_priv, LAN8841_EVENT_A, pin);
4289 	}
4290 
4291 	ts_on.tv_sec = rq->perout.on.sec;
4292 	ts_on.tv_nsec = rq->perout.on.nsec;
4293 	on_nsec = timespec64_to_ns(&ts_on);
4294 
4295 	ts_period.tv_sec = rq->perout.period.sec;
4296 	ts_period.tv_nsec = rq->perout.period.nsec;
4297 	period_nsec = timespec64_to_ns(&ts_period);
4298 
4299 	if (period_nsec < 200) {
4300 		pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4301 				    phydev_name(phydev));
4302 		return -EOPNOTSUPP;
4303 	}
4304 
4305 	if (on_nsec >= period_nsec) {
4306 		pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
4307 				    phydev_name(phydev));
4308 		return -EINVAL;
4309 	}
4310 
4311 	switch (on_nsec) {
4312 	case 200000000:
4313 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS;
4314 		break;
4315 	case 100000000:
4316 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS;
4317 		break;
4318 	case 50000000:
4319 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS;
4320 		break;
4321 	case 10000000:
4322 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS;
4323 		break;
4324 	case 5000000:
4325 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS;
4326 		break;
4327 	case 1000000:
4328 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS;
4329 		break;
4330 	case 500000:
4331 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US;
4332 		break;
4333 	case 100000:
4334 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US;
4335 		break;
4336 	case 50000:
4337 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US;
4338 		break;
4339 	case 10000:
4340 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US;
4341 		break;
4342 	case 5000:
4343 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US;
4344 		break;
4345 	case 1000:
4346 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US;
4347 		break;
4348 	case 500:
4349 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS;
4350 		break;
4351 	case 100:
4352 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4353 		break;
4354 	default:
4355 		pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
4356 				    phydev_name(phydev));
4357 		pulse_width = LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS;
4358 		break;
4359 	}
4360 
4361 	mutex_lock(&ptp_priv->ptp_lock);
4362 	ret = lan8841_ptp_set_target(ptp_priv, LAN8841_EVENT_A, rq->perout.start.sec,
4363 				     rq->perout.start.nsec);
4364 	mutex_unlock(&ptp_priv->ptp_lock);
4365 	if (ret)
4366 		return ret;
4367 
4368 	ret = lan8841_ptp_set_reload(ptp_priv, LAN8841_EVENT_A, rq->perout.period.sec,
4369 				     rq->perout.period.nsec);
4370 	if (ret)
4371 		return ret;
4372 
4373 	ret = lan8841_ptp_enable_event(ptp_priv, pin, LAN8841_EVENT_A,
4374 				       pulse_width);
4375 	if (ret)
4376 		return ret;
4377 
4378 	ret = lan8841_ptp_perout_on(ptp_priv, pin);
4379 	if (ret)
4380 		lan8841_ptp_remove_event(ptp_priv, pin, LAN8841_EVENT_A);
4381 
4382 	return ret;
4383 }
4384 
4385 #define LAN8841_PTP_GPIO_CAP_EN			496
4386 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio)	(BIT(gpio))
4387 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio)	(BIT(gpio) << 8)
4388 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN	BIT(2)
4389 
4390 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv *ptp_priv, int pin,
4391 				u32 flags)
4392 {
4393 	struct phy_device *phydev = ptp_priv->phydev;
4394 	u16 tmp = 0;
4395 	int ret;
4396 
4397 	/* Set GPIO to be intput */
4398 	ret = phy_set_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4399 	if (ret)
4400 		return ret;
4401 
4402 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4403 	if (ret)
4404 		return ret;
4405 
4406 	/* Enable capture on the edges of the pin */
4407 	if (flags & PTP_RISING_EDGE)
4408 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin);
4409 	if (flags & PTP_FALLING_EDGE)
4410 		tmp |= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin);
4411 	ret = phy_write_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN, tmp);
4412 	if (ret)
4413 		return ret;
4414 
4415 	/* Enable interrupt */
4416 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4417 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4418 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN);
4419 }
4420 
4421 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv *ptp_priv, int pin)
4422 {
4423 	struct phy_device *phydev = ptp_priv->phydev;
4424 	int ret;
4425 
4426 	/* Set GPIO to be output */
4427 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_EN, BIT(pin));
4428 	if (ret)
4429 		return ret;
4430 
4431 	ret = phy_clear_bits_mmd(phydev, 2, LAN8841_GPIO_BUF, BIT(pin));
4432 	if (ret)
4433 		return ret;
4434 
4435 	/* Disable capture on both of the edges */
4436 	ret = phy_modify_mmd(phydev, 2, LAN8841_PTP_GPIO_CAP_EN,
4437 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin) |
4438 			     LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin),
4439 			     0);
4440 	if (ret)
4441 		return ret;
4442 
4443 	/* Disable interrupt */
4444 	return phy_modify_mmd(phydev, 2, LAN8841_PTP_INT_EN,
4445 			      LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN,
4446 			      0);
4447 }
4448 
4449 static int lan8841_ptp_extts(struct ptp_clock_info *ptp,
4450 			     struct ptp_clock_request *rq, int on)
4451 {
4452 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4453 							ptp_clock_info);
4454 	int pin;
4455 	int ret;
4456 
4457 	/* Reject requests with unsupported flags */
4458 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
4459 				PTP_EXTTS_EDGES |
4460 				PTP_STRICT_FLAGS))
4461 		return -EOPNOTSUPP;
4462 
4463 	pin = ptp_find_pin(ptp_priv->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
4464 	if (pin == -1 || pin >= LAN8841_PTP_GPIO_NUM)
4465 		return -EINVAL;
4466 
4467 	mutex_lock(&ptp_priv->ptp_lock);
4468 	if (on)
4469 		ret = lan8841_ptp_extts_on(ptp_priv, pin, rq->extts.flags);
4470 	else
4471 		ret = lan8841_ptp_extts_off(ptp_priv, pin);
4472 	mutex_unlock(&ptp_priv->ptp_lock);
4473 
4474 	return ret;
4475 }
4476 
4477 static int lan8841_ptp_enable(struct ptp_clock_info *ptp,
4478 			      struct ptp_clock_request *rq, int on)
4479 {
4480 	switch (rq->type) {
4481 	case PTP_CLK_REQ_EXTTS:
4482 		return lan8841_ptp_extts(ptp, rq, on);
4483 	case PTP_CLK_REQ_PEROUT:
4484 		return lan8841_ptp_perout(ptp, rq, on);
4485 	default:
4486 		return -EOPNOTSUPP;
4487 	}
4488 
4489 	return 0;
4490 }
4491 
4492 static long lan8841_ptp_do_aux_work(struct ptp_clock_info *ptp)
4493 {
4494 	struct kszphy_ptp_priv *ptp_priv = container_of(ptp, struct kszphy_ptp_priv,
4495 							ptp_clock_info);
4496 	struct timespec64 ts;
4497 	unsigned long flags;
4498 
4499 	lan8841_ptp_getseconds(&ptp_priv->ptp_clock_info, &ts);
4500 
4501 	spin_lock_irqsave(&ptp_priv->seconds_lock, flags);
4502 	ptp_priv->seconds = ts.tv_sec;
4503 	spin_unlock_irqrestore(&ptp_priv->seconds_lock, flags);
4504 
4505 	return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY);
4506 }
4507 
4508 static struct ptp_clock_info lan8841_ptp_clock_info = {
4509 	.owner		= THIS_MODULE,
4510 	.name		= "lan8841 ptp",
4511 	.max_adj	= 31249999,
4512 	.gettime64	= lan8841_ptp_gettime64,
4513 	.settime64	= lan8841_ptp_settime64,
4514 	.adjtime	= lan8841_ptp_adjtime,
4515 	.adjfine	= lan8841_ptp_adjfine,
4516 	.verify         = lan8841_ptp_verify,
4517 	.enable         = lan8841_ptp_enable,
4518 	.do_aux_work	= lan8841_ptp_do_aux_work,
4519 	.n_per_out      = LAN8841_PTP_GPIO_NUM,
4520 	.n_ext_ts       = LAN8841_PTP_GPIO_NUM,
4521 	.n_pins         = LAN8841_PTP_GPIO_NUM,
4522 };
4523 
4524 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
4525 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
4526 
4527 static int lan8841_probe(struct phy_device *phydev)
4528 {
4529 	struct kszphy_ptp_priv *ptp_priv;
4530 	struct kszphy_priv *priv;
4531 	int err;
4532 
4533 	err = kszphy_probe(phydev);
4534 	if (err)
4535 		return err;
4536 
4537 	if (phy_read_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
4538 			 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER) &
4539 	    LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN)
4540 		phydev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
4541 
4542 	/* Register the clock */
4543 	if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING))
4544 		return 0;
4545 
4546 	priv = phydev->priv;
4547 	ptp_priv = &priv->ptp_priv;
4548 
4549 	ptp_priv->pin_config = devm_kcalloc(&phydev->mdio.dev,
4550 					    LAN8841_PTP_GPIO_NUM,
4551 					    sizeof(*ptp_priv->pin_config),
4552 					    GFP_KERNEL);
4553 	if (!ptp_priv->pin_config)
4554 		return -ENOMEM;
4555 
4556 	for (int i = 0; i < LAN8841_PTP_GPIO_NUM; ++i) {
4557 		struct ptp_pin_desc *p = &ptp_priv->pin_config[i];
4558 
4559 		snprintf(p->name, sizeof(p->name), "pin%d", i);
4560 		p->index = i;
4561 		p->func = PTP_PF_NONE;
4562 	}
4563 
4564 	ptp_priv->ptp_clock_info = lan8841_ptp_clock_info;
4565 	ptp_priv->ptp_clock_info.pin_config = ptp_priv->pin_config;
4566 	ptp_priv->ptp_clock = ptp_clock_register(&ptp_priv->ptp_clock_info,
4567 						 &phydev->mdio.dev);
4568 	if (IS_ERR(ptp_priv->ptp_clock)) {
4569 		phydev_err(phydev, "ptp_clock_register failed: %lu\n",
4570 			   PTR_ERR(ptp_priv->ptp_clock));
4571 		return -EINVAL;
4572 	}
4573 
4574 	if (!ptp_priv->ptp_clock)
4575 		return 0;
4576 
4577 	/* Initialize the SW */
4578 	skb_queue_head_init(&ptp_priv->tx_queue);
4579 	ptp_priv->phydev = phydev;
4580 	mutex_init(&ptp_priv->ptp_lock);
4581 	spin_lock_init(&ptp_priv->seconds_lock);
4582 
4583 	ptp_priv->mii_ts.rxtstamp = lan8841_rxtstamp;
4584 	ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
4585 	ptp_priv->mii_ts.hwtstamp = lan8841_hwtstamp;
4586 	ptp_priv->mii_ts.ts_info = lan8841_ts_info;
4587 
4588 	phydev->mii_ts = &ptp_priv->mii_ts;
4589 
4590 	return 0;
4591 }
4592 
4593 static int lan8841_suspend(struct phy_device *phydev)
4594 {
4595 	struct kszphy_priv *priv = phydev->priv;
4596 	struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
4597 
4598 	ptp_cancel_worker_sync(ptp_priv->ptp_clock);
4599 
4600 	return genphy_suspend(phydev);
4601 }
4602 
4603 static struct phy_driver ksphy_driver[] = {
4604 {
4605 	.phy_id		= PHY_ID_KS8737,
4606 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4607 	.name		= "Micrel KS8737",
4608 	/* PHY_BASIC_FEATURES */
4609 	.driver_data	= &ks8737_type,
4610 	.probe		= kszphy_probe,
4611 	.config_init	= kszphy_config_init,
4612 	.config_intr	= kszphy_config_intr,
4613 	.handle_interrupt = kszphy_handle_interrupt,
4614 	.suspend	= kszphy_suspend,
4615 	.resume		= kszphy_resume,
4616 }, {
4617 	.phy_id		= PHY_ID_KSZ8021,
4618 	.phy_id_mask	= 0x00ffffff,
4619 	.name		= "Micrel KSZ8021 or KSZ8031",
4620 	/* PHY_BASIC_FEATURES */
4621 	.driver_data	= &ksz8021_type,
4622 	.probe		= kszphy_probe,
4623 	.config_init	= kszphy_config_init,
4624 	.config_intr	= kszphy_config_intr,
4625 	.handle_interrupt = kszphy_handle_interrupt,
4626 	.get_sset_count = kszphy_get_sset_count,
4627 	.get_strings	= kszphy_get_strings,
4628 	.get_stats	= kszphy_get_stats,
4629 	.suspend	= kszphy_suspend,
4630 	.resume		= kszphy_resume,
4631 }, {
4632 	.phy_id		= PHY_ID_KSZ8031,
4633 	.phy_id_mask	= 0x00ffffff,
4634 	.name		= "Micrel KSZ8031",
4635 	/* PHY_BASIC_FEATURES */
4636 	.driver_data	= &ksz8021_type,
4637 	.probe		= kszphy_probe,
4638 	.config_init	= kszphy_config_init,
4639 	.config_intr	= kszphy_config_intr,
4640 	.handle_interrupt = kszphy_handle_interrupt,
4641 	.get_sset_count = kszphy_get_sset_count,
4642 	.get_strings	= kszphy_get_strings,
4643 	.get_stats	= kszphy_get_stats,
4644 	.suspend	= kszphy_suspend,
4645 	.resume		= kszphy_resume,
4646 }, {
4647 	.phy_id		= PHY_ID_KSZ8041,
4648 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4649 	.name		= "Micrel KSZ8041",
4650 	/* PHY_BASIC_FEATURES */
4651 	.driver_data	= &ksz8041_type,
4652 	.probe		= kszphy_probe,
4653 	.config_init	= ksz8041_config_init,
4654 	.config_aneg	= ksz8041_config_aneg,
4655 	.config_intr	= kszphy_config_intr,
4656 	.handle_interrupt = kszphy_handle_interrupt,
4657 	.get_sset_count = kszphy_get_sset_count,
4658 	.get_strings	= kszphy_get_strings,
4659 	.get_stats	= kszphy_get_stats,
4660 	/* No suspend/resume callbacks because of errata DS80000700A,
4661 	 * receiver error following software power down.
4662 	 */
4663 }, {
4664 	.phy_id		= PHY_ID_KSZ8041RNLI,
4665 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4666 	.name		= "Micrel KSZ8041RNLI",
4667 	/* PHY_BASIC_FEATURES */
4668 	.driver_data	= &ksz8041_type,
4669 	.probe		= kszphy_probe,
4670 	.config_init	= kszphy_config_init,
4671 	.config_intr	= kszphy_config_intr,
4672 	.handle_interrupt = kszphy_handle_interrupt,
4673 	.get_sset_count = kszphy_get_sset_count,
4674 	.get_strings	= kszphy_get_strings,
4675 	.get_stats	= kszphy_get_stats,
4676 	.suspend	= kszphy_suspend,
4677 	.resume		= kszphy_resume,
4678 }, {
4679 	.name		= "Micrel KSZ8051",
4680 	/* PHY_BASIC_FEATURES */
4681 	.driver_data	= &ksz8051_type,
4682 	.probe		= kszphy_probe,
4683 	.config_init	= kszphy_config_init,
4684 	.config_intr	= kszphy_config_intr,
4685 	.handle_interrupt = kszphy_handle_interrupt,
4686 	.get_sset_count = kszphy_get_sset_count,
4687 	.get_strings	= kszphy_get_strings,
4688 	.get_stats	= kszphy_get_stats,
4689 	.match_phy_device = ksz8051_match_phy_device,
4690 	.suspend	= kszphy_suspend,
4691 	.resume		= kszphy_resume,
4692 }, {
4693 	.phy_id		= PHY_ID_KSZ8001,
4694 	.name		= "Micrel KSZ8001 or KS8721",
4695 	.phy_id_mask	= 0x00fffffc,
4696 	/* PHY_BASIC_FEATURES */
4697 	.driver_data	= &ksz8041_type,
4698 	.probe		= kszphy_probe,
4699 	.config_init	= kszphy_config_init,
4700 	.config_intr	= kszphy_config_intr,
4701 	.handle_interrupt = kszphy_handle_interrupt,
4702 	.get_sset_count = kszphy_get_sset_count,
4703 	.get_strings	= kszphy_get_strings,
4704 	.get_stats	= kszphy_get_stats,
4705 	.suspend	= kszphy_suspend,
4706 	.resume		= kszphy_resume,
4707 }, {
4708 	.phy_id		= PHY_ID_KSZ8081,
4709 	.name		= "Micrel KSZ8081 or KSZ8091",
4710 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4711 	.flags		= PHY_POLL_CABLE_TEST,
4712 	/* PHY_BASIC_FEATURES */
4713 	.driver_data	= &ksz8081_type,
4714 	.probe		= kszphy_probe,
4715 	.config_init	= ksz8081_config_init,
4716 	.soft_reset	= genphy_soft_reset,
4717 	.config_aneg	= ksz8081_config_aneg,
4718 	.read_status	= ksz8081_read_status,
4719 	.config_intr	= kszphy_config_intr,
4720 	.handle_interrupt = kszphy_handle_interrupt,
4721 	.get_sset_count = kszphy_get_sset_count,
4722 	.get_strings	= kszphy_get_strings,
4723 	.get_stats	= kszphy_get_stats,
4724 	.suspend	= kszphy_suspend,
4725 	.resume		= kszphy_resume,
4726 	.cable_test_start	= ksz886x_cable_test_start,
4727 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4728 }, {
4729 	.phy_id		= PHY_ID_KSZ8061,
4730 	.name		= "Micrel KSZ8061",
4731 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4732 	/* PHY_BASIC_FEATURES */
4733 	.probe		= kszphy_probe,
4734 	.config_init	= ksz8061_config_init,
4735 	.config_intr	= kszphy_config_intr,
4736 	.handle_interrupt = kszphy_handle_interrupt,
4737 	.suspend	= kszphy_suspend,
4738 	.resume		= kszphy_resume,
4739 }, {
4740 	.phy_id		= PHY_ID_KSZ9021,
4741 	.phy_id_mask	= 0x000ffffe,
4742 	.name		= "Micrel KSZ9021 Gigabit PHY",
4743 	/* PHY_GBIT_FEATURES */
4744 	.driver_data	= &ksz9021_type,
4745 	.probe		= kszphy_probe,
4746 	.get_features	= ksz9031_get_features,
4747 	.config_init	= ksz9021_config_init,
4748 	.config_intr	= kszphy_config_intr,
4749 	.handle_interrupt = kszphy_handle_interrupt,
4750 	.get_sset_count = kszphy_get_sset_count,
4751 	.get_strings	= kszphy_get_strings,
4752 	.get_stats	= kszphy_get_stats,
4753 	.suspend	= kszphy_suspend,
4754 	.resume		= kszphy_resume,
4755 	.read_mmd	= genphy_read_mmd_unsupported,
4756 	.write_mmd	= genphy_write_mmd_unsupported,
4757 }, {
4758 	.phy_id		= PHY_ID_KSZ9031,
4759 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4760 	.name		= "Micrel KSZ9031 Gigabit PHY",
4761 	.flags		= PHY_POLL_CABLE_TEST,
4762 	.driver_data	= &ksz9021_type,
4763 	.probe		= kszphy_probe,
4764 	.get_features	= ksz9031_get_features,
4765 	.config_init	= ksz9031_config_init,
4766 	.soft_reset	= genphy_soft_reset,
4767 	.read_status	= ksz9031_read_status,
4768 	.config_intr	= kszphy_config_intr,
4769 	.handle_interrupt = kszphy_handle_interrupt,
4770 	.get_sset_count = kszphy_get_sset_count,
4771 	.get_strings	= kszphy_get_strings,
4772 	.get_stats	= kszphy_get_stats,
4773 	.suspend	= kszphy_suspend,
4774 	.resume		= kszphy_resume,
4775 	.cable_test_start	= ksz9x31_cable_test_start,
4776 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4777 }, {
4778 	.phy_id		= PHY_ID_LAN8814,
4779 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4780 	.name		= "Microchip INDY Gigabit Quad PHY",
4781 	.flags          = PHY_POLL_CABLE_TEST,
4782 	.config_init	= lan8814_config_init,
4783 	.driver_data	= &lan8814_type,
4784 	.probe		= lan8814_probe,
4785 	.soft_reset	= genphy_soft_reset,
4786 	.read_status	= ksz9031_read_status,
4787 	.get_sset_count	= kszphy_get_sset_count,
4788 	.get_strings	= kszphy_get_strings,
4789 	.get_stats	= kszphy_get_stats,
4790 	.suspend	= genphy_suspend,
4791 	.resume		= kszphy_resume,
4792 	.config_intr	= lan8814_config_intr,
4793 	.handle_interrupt = lan8814_handle_interrupt,
4794 	.cable_test_start	= lan8814_cable_test_start,
4795 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4796 }, {
4797 	.phy_id		= PHY_ID_LAN8804,
4798 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4799 	.name		= "Microchip LAN966X Gigabit PHY",
4800 	.config_init	= lan8804_config_init,
4801 	.driver_data	= &ksz9021_type,
4802 	.probe		= kszphy_probe,
4803 	.soft_reset	= genphy_soft_reset,
4804 	.read_status	= ksz9031_read_status,
4805 	.get_sset_count	= kszphy_get_sset_count,
4806 	.get_strings	= kszphy_get_strings,
4807 	.get_stats	= kszphy_get_stats,
4808 	.suspend	= genphy_suspend,
4809 	.resume		= kszphy_resume,
4810 	.config_intr	= lan8804_config_intr,
4811 	.handle_interrupt = lan8804_handle_interrupt,
4812 }, {
4813 	.phy_id		= PHY_ID_LAN8841,
4814 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4815 	.name		= "Microchip LAN8841 Gigabit PHY",
4816 	.flags		= PHY_POLL_CABLE_TEST,
4817 	.driver_data	= &lan8841_type,
4818 	.config_init	= lan8841_config_init,
4819 	.probe		= lan8841_probe,
4820 	.soft_reset	= genphy_soft_reset,
4821 	.config_intr	= lan8841_config_intr,
4822 	.handle_interrupt = lan8841_handle_interrupt,
4823 	.get_sset_count = kszphy_get_sset_count,
4824 	.get_strings	= kszphy_get_strings,
4825 	.get_stats	= kszphy_get_stats,
4826 	.suspend	= lan8841_suspend,
4827 	.resume		= genphy_resume,
4828 	.cable_test_start	= lan8814_cable_test_start,
4829 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4830 }, {
4831 	.phy_id		= PHY_ID_KSZ9131,
4832 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4833 	.name		= "Microchip KSZ9131 Gigabit PHY",
4834 	/* PHY_GBIT_FEATURES */
4835 	.flags		= PHY_POLL_CABLE_TEST,
4836 	.driver_data	= &ksz9131_type,
4837 	.probe		= kszphy_probe,
4838 	.soft_reset	= genphy_soft_reset,
4839 	.config_init	= ksz9131_config_init,
4840 	.config_intr	= kszphy_config_intr,
4841 	.config_aneg	= ksz9131_config_aneg,
4842 	.read_status	= ksz9131_read_status,
4843 	.handle_interrupt = kszphy_handle_interrupt,
4844 	.get_sset_count = kszphy_get_sset_count,
4845 	.get_strings	= kszphy_get_strings,
4846 	.get_stats	= kszphy_get_stats,
4847 	.suspend	= kszphy_suspend,
4848 	.resume		= kszphy_resume,
4849 	.cable_test_start	= ksz9x31_cable_test_start,
4850 	.cable_test_get_status	= ksz9x31_cable_test_get_status,
4851 	.get_features	= ksz9477_get_features,
4852 }, {
4853 	.phy_id		= PHY_ID_KSZ8873MLL,
4854 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4855 	.name		= "Micrel KSZ8873MLL Switch",
4856 	/* PHY_BASIC_FEATURES */
4857 	.config_init	= kszphy_config_init,
4858 	.config_aneg	= ksz8873mll_config_aneg,
4859 	.read_status	= ksz8873mll_read_status,
4860 	.suspend	= genphy_suspend,
4861 	.resume		= genphy_resume,
4862 }, {
4863 	.phy_id		= PHY_ID_KSZ886X,
4864 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4865 	.name		= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
4866 	.driver_data	= &ksz886x_type,
4867 	/* PHY_BASIC_FEATURES */
4868 	.flags		= PHY_POLL_CABLE_TEST,
4869 	.config_init	= kszphy_config_init,
4870 	.config_aneg	= ksz886x_config_aneg,
4871 	.read_status	= ksz886x_read_status,
4872 	.suspend	= genphy_suspend,
4873 	.resume		= genphy_resume,
4874 	.cable_test_start	= ksz886x_cable_test_start,
4875 	.cable_test_get_status	= ksz886x_cable_test_get_status,
4876 }, {
4877 	.name		= "Micrel KSZ87XX Switch",
4878 	/* PHY_BASIC_FEATURES */
4879 	.config_init	= kszphy_config_init,
4880 	.match_phy_device = ksz8795_match_phy_device,
4881 	.suspend	= genphy_suspend,
4882 	.resume		= genphy_resume,
4883 }, {
4884 	.phy_id		= PHY_ID_KSZ9477,
4885 	.phy_id_mask	= MICREL_PHY_ID_MASK,
4886 	.name		= "Microchip KSZ9477",
4887 	/* PHY_GBIT_FEATURES */
4888 	.config_init	= ksz9477_config_init,
4889 	.config_intr	= kszphy_config_intr,
4890 	.handle_interrupt = kszphy_handle_interrupt,
4891 	.suspend	= genphy_suspend,
4892 	.resume		= genphy_resume,
4893 	.get_features	= ksz9477_get_features,
4894 } };
4895 
4896 module_phy_driver(ksphy_driver);
4897 
4898 MODULE_DESCRIPTION("Micrel PHY driver");
4899 MODULE_AUTHOR("David J. Choi");
4900 MODULE_LICENSE("GPL");
4901 
4902 static struct mdio_device_id __maybe_unused micrel_tbl[] = {
4903 	{ PHY_ID_KSZ9021, 0x000ffffe },
4904 	{ PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
4905 	{ PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
4906 	{ PHY_ID_KSZ8001, 0x00fffffc },
4907 	{ PHY_ID_KS8737, MICREL_PHY_ID_MASK },
4908 	{ PHY_ID_KSZ8021, 0x00ffffff },
4909 	{ PHY_ID_KSZ8031, 0x00ffffff },
4910 	{ PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
4911 	{ PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
4912 	{ PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
4913 	{ PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
4914 	{ PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
4915 	{ PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
4916 	{ PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
4917 	{ PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
4918 	{ PHY_ID_LAN8841, MICREL_PHY_ID_MASK },
4919 	{ }
4920 };
4921 
4922 MODULE_DEVICE_TABLE(mdio, micrel_tbl);
4923