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