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