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