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