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