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