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