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