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