1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/micrel.c
5 * Driver for Micrel PHYs
7 * Author: David J. Choi
9 * Copyright (c) 2010-2013 Micrel, Inc.
10 * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
12 * Support : Micrel Phys:
13 * Giga phys: ksz9021, ksz9031, ksz9131, lan8841, lan8814
14 * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
15 * ksz8021, ksz8031, ksz8051,
18 * Switch : ksz8873, ksz886x
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>
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>
37 /* Operation Mode Strap Override */
38 #define MII_KSZPHY_OMSO 0x16
39 #define KSZPHY_OMSO_FACTORY_TEST BIT(15)
40 #define KSZPHY_OMSO_B_CAST_OFF BIT(9)
41 #define KSZPHY_OMSO_NAND_TREE_ON BIT(5)
42 #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1)
43 #define KSZPHY_OMSO_MII_OVERRIDE BIT(0)
45 /* general Interrupt control/status reg in vendor specific block. */
46 #define MII_KSZPHY_INTCS 0x1B
47 #define KSZPHY_INTCS_JABBER BIT(15)
48 #define KSZPHY_INTCS_RECEIVE_ERR BIT(14)
49 #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13)
50 #define KSZPHY_INTCS_PARELLEL BIT(12)
51 #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11)
52 #define KSZPHY_INTCS_LINK_DOWN BIT(10)
53 #define KSZPHY_INTCS_REMOTE_FAULT BIT(9)
54 #define KSZPHY_INTCS_LINK_UP BIT(8)
55 #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
56 KSZPHY_INTCS_LINK_DOWN)
57 #define KSZPHY_INTCS_LINK_DOWN_STATUS BIT(2)
58 #define KSZPHY_INTCS_LINK_UP_STATUS BIT(0)
59 #define KSZPHY_INTCS_STATUS (KSZPHY_INTCS_LINK_DOWN_STATUS |\
60 KSZPHY_INTCS_LINK_UP_STATUS)
62 /* LinkMD Control/Status */
63 #define KSZ8081_LMD 0x1d
64 #define KSZ8081_LMD_ENABLE_TEST BIT(15)
65 #define KSZ8081_LMD_STAT_NORMAL 0
66 #define KSZ8081_LMD_STAT_OPEN 1
67 #define KSZ8081_LMD_STAT_SHORT 2
68 #define KSZ8081_LMD_STAT_FAIL 3
69 #define KSZ8081_LMD_STAT_MASK GENMASK(14, 13)
70 /* Short cable (<10 meter) has been detected by LinkMD */
71 #define KSZ8081_LMD_SHORT_INDICATOR BIT(12)
72 #define KSZ8081_LMD_DELTA_TIME_MASK GENMASK(8, 0)
74 #define KSZ9x31_LMD 0x12
75 #define KSZ9x31_LMD_VCT_EN BIT(15)
76 #define KSZ9x31_LMD_VCT_DIS_TX BIT(14)
77 #define KSZ9x31_LMD_VCT_PAIR(n) (((n) & 0x3) << 12)
78 #define KSZ9x31_LMD_VCT_SEL_RESULT 0
79 #define KSZ9x31_LMD_VCT_SEL_THRES_HI BIT(10)
80 #define KSZ9x31_LMD_VCT_SEL_THRES_LO BIT(11)
81 #define KSZ9x31_LMD_VCT_SEL_MASK GENMASK(11, 10)
82 #define KSZ9x31_LMD_VCT_ST_NORMAL 0
83 #define KSZ9x31_LMD_VCT_ST_OPEN 1
84 #define KSZ9x31_LMD_VCT_ST_SHORT 2
85 #define KSZ9x31_LMD_VCT_ST_FAIL 3
86 #define KSZ9x31_LMD_VCT_ST_MASK GENMASK(9, 8)
87 #define KSZ9x31_LMD_VCT_DATA_REFLECTED_INVALID BIT(7)
88 #define KSZ9x31_LMD_VCT_DATA_SIG_WAIT_TOO_LONG BIT(6)
89 #define KSZ9x31_LMD_VCT_DATA_MASK100 BIT(5)
90 #define KSZ9x31_LMD_VCT_DATA_NLP_FLP BIT(4)
91 #define KSZ9x31_LMD_VCT_DATA_LO_PULSE_MASK GENMASK(3, 2)
92 #define KSZ9x31_LMD_VCT_DATA_HI_PULSE_MASK GENMASK(1, 0)
93 #define KSZ9x31_LMD_VCT_DATA_MASK GENMASK(7, 0)
95 #define KSZPHY_WIRE_PAIR_MASK 0x3
97 #define LAN8814_CABLE_DIAG 0x12
98 #define LAN8814_CABLE_DIAG_STAT_MASK GENMASK(9, 8)
99 #define LAN8814_CABLE_DIAG_VCT_DATA_MASK GENMASK(7, 0)
100 #define LAN8814_PAIR_BIT_SHIFT 12
102 #define LAN8814_WIRE_PAIR_MASK 0xF
104 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
105 #define LAN8814_INTC 0x18
106 #define LAN8814_INTS 0x1B
108 #define LAN8814_INT_LINK_DOWN BIT(2)
109 #define LAN8814_INT_LINK_UP BIT(0)
110 #define LAN8814_INT_LINK (LAN8814_INT_LINK_UP |\
111 LAN8814_INT_LINK_DOWN)
113 #define LAN8814_INTR_CTRL_REG 0x34
114 #define LAN8814_INTR_CTRL_REG_POLARITY BIT(1)
115 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE BIT(0)
117 #define LAN8814_EEE_STATE 0x38
118 #define LAN8814_EEE_STATE_MASK2P5P BIT(10)
120 #define LAN8814_PD_CONTROLS 0x9d
121 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK GENMASK(3, 0)
122 #define LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL 0xb
124 /* Represents 1ppm adjustment in 2^32 format with
125 * each nsec contains 4 clock cycles.
126 * The value is calculated as following: (1/1000000)/((2^-32)/4)
128 #define LAN8814_1PPM_FORMAT 17179
130 /* Represents 1ppm adjustment in 2^32 format with
131 * each nsec contains 8 clock cycles.
132 * The value is calculated as following: (1/1000000)/((2^-32)/8)
134 #define LAN8841_1PPM_FORMAT 34360
136 #define PTP_RX_VERSION 0x0248
137 #define PTP_TX_VERSION 0x0288
138 #define PTP_MAX_VERSION(x) (((x) & GENMASK(7, 0)) << 8)
139 #define PTP_MIN_VERSION(x) ((x) & GENMASK(7, 0))
141 #define PTP_RX_MOD 0x024F
142 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
143 #define PTP_RX_TIMESTAMP_EN 0x024D
144 #define PTP_TX_TIMESTAMP_EN 0x028D
146 #define PTP_TIMESTAMP_EN_SYNC_ BIT(0)
147 #define PTP_TIMESTAMP_EN_DREQ_ BIT(1)
148 #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2)
149 #define PTP_TIMESTAMP_EN_PDRES_ BIT(3)
151 #define PTP_TX_PARSE_L2_ADDR_EN 0x0284
152 #define PTP_RX_PARSE_L2_ADDR_EN 0x0244
154 #define PTP_TX_PARSE_IP_ADDR_EN 0x0285
155 #define PTP_RX_PARSE_IP_ADDR_EN 0x0245
156 #define LTC_HARD_RESET 0x023F
157 #define LTC_HARD_RESET_ BIT(0)
159 #define TSU_HARD_RESET 0x02C1
160 #define TSU_HARD_RESET_ BIT(0)
162 #define PTP_CMD_CTL 0x0200
163 #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0)
164 #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1)
165 #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3)
166 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4)
167 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5)
168 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6)
170 #define PTP_COMMON_INT_ENA 0x0204
171 #define PTP_COMMON_INT_ENA_GPIO_CAP_EN BIT(2)
173 #define PTP_CLOCK_SET_SEC_HI 0x0205
174 #define PTP_CLOCK_SET_SEC_MID 0x0206
175 #define PTP_CLOCK_SET_SEC_LO 0x0207
176 #define PTP_CLOCK_SET_NS_HI 0x0208
177 #define PTP_CLOCK_SET_NS_LO 0x0209
179 #define PTP_CLOCK_READ_SEC_HI 0x0229
180 #define PTP_CLOCK_READ_SEC_MID 0x022A
181 #define PTP_CLOCK_READ_SEC_LO 0x022B
182 #define PTP_CLOCK_READ_NS_HI 0x022C
183 #define PTP_CLOCK_READ_NS_LO 0x022D
185 #define PTP_GPIO_SEL 0x0230
186 #define PTP_GPIO_SEL_GPIO_SEL(pin) ((pin) << 8)
187 #define PTP_GPIO_CAP_MAP_LO 0x0232
189 #define PTP_GPIO_CAP_EN 0x0233
190 #define PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio) BIT(gpio)
191 #define PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio) (BIT(gpio) << 8)
193 #define PTP_GPIO_RE_LTC_SEC_HI_CAP 0x0235
194 #define PTP_GPIO_RE_LTC_SEC_LO_CAP 0x0236
195 #define PTP_GPIO_RE_LTC_NS_HI_CAP 0x0237
196 #define PTP_GPIO_RE_LTC_NS_LO_CAP 0x0238
197 #define PTP_GPIO_FE_LTC_SEC_HI_CAP 0x0239
198 #define PTP_GPIO_FE_LTC_SEC_LO_CAP 0x023A
199 #define PTP_GPIO_FE_LTC_NS_HI_CAP 0x023B
200 #define PTP_GPIO_FE_LTC_NS_LO_CAP 0x023C
202 #define PTP_GPIO_CAP_STS 0x023D
203 #define PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(gpio) BIT(gpio)
204 #define PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(gpio) (BIT(gpio) << 8)
206 #define PTP_OPERATING_MODE 0x0241
207 #define PTP_OPERATING_MODE_STANDALONE_ BIT(0)
209 #define PTP_TX_MOD 0x028F
210 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12)
211 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
213 #define PTP_RX_PARSE_CONFIG 0x0242
214 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0)
215 #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1)
216 #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2)
218 #define PTP_TX_PARSE_CONFIG 0x0282
219 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0)
220 #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1)
221 #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2)
223 #define PTP_CLOCK_RATE_ADJ_HI 0x020C
224 #define PTP_CLOCK_RATE_ADJ_LO 0x020D
225 #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15)
227 #define PTP_LTC_STEP_ADJ_HI 0x0212
228 #define PTP_LTC_STEP_ADJ_LO 0x0213
229 #define PTP_LTC_STEP_ADJ_DIR_ BIT(15)
231 #define LAN8814_INTR_STS_REG 0x0033
232 #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0)
233 #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1)
234 #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2)
235 #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3)
237 #define PTP_CAP_INFO 0x022A
238 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8)
239 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f)
241 #define PTP_TX_EGRESS_SEC_HI 0x0296
242 #define PTP_TX_EGRESS_SEC_LO 0x0297
243 #define PTP_TX_EGRESS_NS_HI 0x0294
244 #define PTP_TX_EGRESS_NS_LO 0x0295
245 #define PTP_TX_MSG_HEADER2 0x0299
247 #define PTP_RX_INGRESS_SEC_HI 0x0256
248 #define PTP_RX_INGRESS_SEC_LO 0x0257
249 #define PTP_RX_INGRESS_NS_HI 0x0254
250 #define PTP_RX_INGRESS_NS_LO 0x0255
251 #define PTP_RX_MSG_HEADER2 0x0259
253 #define PTP_TSU_INT_EN 0x0200
254 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3)
255 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2)
256 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1)
257 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0)
259 #define PTP_TSU_INT_STS 0x0201
260 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3)
261 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2)
262 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1)
263 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0)
265 #define LAN8814_LED_CTRL_1 0x0
266 #define LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_ BIT(6)
269 #define MII_KSZPHY_CTRL_1 0x1e
270 #define KSZ8081_CTRL1_MDIX_STAT BIT(4)
272 /* PHY Control 2 / PHY Control (if no PHY Control 1) */
273 #define MII_KSZPHY_CTRL_2 0x1f
274 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2
275 /* bitmap of PHY register to set interrupt mode */
276 #define KSZ8081_CTRL2_HP_MDIX BIT(15)
277 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14)
278 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13)
279 #define KSZ8081_CTRL2_FORCE_LINK BIT(11)
280 #define KSZ8081_CTRL2_POWER_SAVING BIT(10)
281 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9)
282 #define KSZPHY_RMII_REF_CLK_SEL BIT(7)
284 /* Write/read to/from extended registers */
285 #define MII_KSZPHY_EXTREG 0x0b
286 #define KSZPHY_EXTREG_WRITE 0x8000
288 #define MII_KSZPHY_EXTREG_WRITE 0x0c
289 #define MII_KSZPHY_EXTREG_READ 0x0d
291 /* Extended registers */
292 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
293 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
294 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
296 #define PS_TO_REG 200
299 #define LAN8814_PTP_GPIO_NUM 24
300 #define LAN8814_PTP_PEROUT_NUM 2
301 #define LAN8814_PTP_EXTTS_NUM 3
303 #define LAN8814_BUFFER_TIME 2
305 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS 13
306 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS 12
307 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS 11
308 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS 10
309 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS 9
310 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS 8
311 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US 7
312 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US 6
313 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US 5
314 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US 4
315 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US 3
316 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US 2
317 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS 1
318 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS 0
320 #define LAN8814_GPIO_EN1 0x20
321 #define LAN8814_GPIO_EN2 0x21
322 #define LAN8814_GPIO_DIR1 0x22
323 #define LAN8814_GPIO_DIR2 0x23
324 #define LAN8814_GPIO_BUF1 0x24
325 #define LAN8814_GPIO_BUF2 0x25
327 #define LAN8814_GPIO_EN_ADDR(pin) \
328 ((pin) > 15 ? LAN8814_GPIO_EN1 : LAN8814_GPIO_EN2)
329 #define LAN8814_GPIO_EN_BIT(pin) BIT(pin)
330 #define LAN8814_GPIO_DIR_ADDR(pin) \
331 ((pin) > 15 ? LAN8814_GPIO_DIR1 : LAN8814_GPIO_DIR2)
332 #define LAN8814_GPIO_DIR_BIT(pin) BIT(pin)
333 #define LAN8814_GPIO_BUF_ADDR(pin) \
334 ((pin) > 15 ? LAN8814_GPIO_BUF1 : LAN8814_GPIO_BUF2)
335 #define LAN8814_GPIO_BUF_BIT(pin) BIT(pin)
337 #define LAN8814_EVENT_A 0
338 #define LAN8814_EVENT_B 1
340 #define LAN8814_PTP_GENERAL_CONFIG 0x0201
341 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event) \
342 ((event) ? GENMASK(11, 8) : GENMASK(7, 4))
343 #define LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event, value) \
344 (((value) & GENMASK(3, 0)) << (4 + ((event) << 2)))
345 #define LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event) \
346 ((event) ? BIT(2) : BIT(0))
347 #define LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event) \
348 ((event) ? BIT(3) : BIT(1))
350 #define LAN8814_PTP_CLOCK_TARGET_SEC_HI(event) ((event) ? 0x21F : 0x215)
351 #define LAN8814_PTP_CLOCK_TARGET_SEC_LO(event) ((event) ? 0x220 : 0x216)
352 #define LAN8814_PTP_CLOCK_TARGET_NS_HI(event) ((event) ? 0x221 : 0x217)
353 #define LAN8814_PTP_CLOCK_TARGET_NS_LO(event) ((event) ? 0x222 : 0x218)
355 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event) ((event) ? 0x223 : 0x219)
356 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event) ((event) ? 0x224 : 0x21A)
357 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event) ((event) ? 0x225 : 0x21B)
358 #define LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event) ((event) ? 0x226 : 0x21C)
360 /* Delay used to get the second part from the LTC */
361 #define LAN8841_GET_SEC_LTC_DELAY (500 * NSEC_PER_MSEC)
363 struct kszphy_hw_stat
{
369 static struct kszphy_hw_stat kszphy_hw_stats
[] = {
370 { "phy_receive_errors", 21, 16},
371 { "phy_idle_errors", 10, 8 },
376 u16 interrupt_level_mask
;
378 unsigned long pair_mask
;
379 u16 disable_dll_tx_bit
;
380 u16 disable_dll_rx_bit
;
381 u16 disable_dll_mask
;
382 bool has_broadcast_disable
;
383 bool has_nand_tree_disable
;
384 bool has_rmii_ref_clk_sel
;
387 /* Shared structure between the PHYs of the same package. */
388 struct lan8814_shared_priv
{
389 struct phy_device
*phydev
;
390 struct ptp_clock
*ptp_clock
;
391 struct ptp_clock_info ptp_clock_info
;
392 struct ptp_pin_desc
*pin_config
;
394 /* Lock for ptp_clock */
395 struct mutex shared_lock
;
398 struct lan8814_ptp_rx_ts
{
399 struct list_head list
;
405 struct kszphy_ptp_priv
{
406 struct mii_timestamper mii_ts
;
407 struct phy_device
*phydev
;
409 struct sk_buff_head tx_queue
;
410 struct sk_buff_head rx_queue
;
412 struct list_head rx_ts_list
;
413 /* Lock for Rx ts fifo */
414 spinlock_t rx_ts_lock
;
417 enum hwtstamp_rx_filters rx_filter
;
421 struct ptp_clock
*ptp_clock
;
422 struct ptp_clock_info ptp_clock_info
;
423 /* Lock for ptp_clock */
424 struct mutex ptp_lock
;
425 struct ptp_pin_desc
*pin_config
;
428 /* Lock for accessing seconds */
429 spinlock_t seconds_lock
;
433 struct kszphy_ptp_priv ptp_priv
;
434 const struct kszphy_type
*type
;
437 bool rmii_ref_clk_sel
;
438 bool rmii_ref_clk_sel_val
;
439 u64 stats
[ARRAY_SIZE(kszphy_hw_stats
)];
442 static const struct kszphy_type lan8814_type
= {
443 .led_mode_reg
= ~LAN8814_LED_CTRL_1
,
444 .cable_diag_reg
= LAN8814_CABLE_DIAG
,
445 .pair_mask
= LAN8814_WIRE_PAIR_MASK
,
448 static const struct kszphy_type ksz886x_type
= {
449 .cable_diag_reg
= KSZ8081_LMD
,
450 .pair_mask
= KSZPHY_WIRE_PAIR_MASK
,
453 static const struct kszphy_type ksz8021_type
= {
454 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
455 .has_broadcast_disable
= true,
456 .has_nand_tree_disable
= true,
457 .has_rmii_ref_clk_sel
= true,
460 static const struct kszphy_type ksz8041_type
= {
461 .led_mode_reg
= MII_KSZPHY_CTRL_1
,
464 static const struct kszphy_type ksz8051_type
= {
465 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
466 .has_nand_tree_disable
= true,
469 static const struct kszphy_type ksz8081_type
= {
470 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
471 .has_broadcast_disable
= true,
472 .has_nand_tree_disable
= true,
473 .has_rmii_ref_clk_sel
= true,
476 static const struct kszphy_type ks8737_type
= {
477 .interrupt_level_mask
= BIT(14),
480 static const struct kszphy_type ksz9021_type
= {
481 .interrupt_level_mask
= BIT(14),
484 static const struct kszphy_type ksz9131_type
= {
485 .interrupt_level_mask
= BIT(14),
486 .disable_dll_tx_bit
= BIT(12),
487 .disable_dll_rx_bit
= BIT(12),
488 .disable_dll_mask
= BIT_MASK(12),
491 static const struct kszphy_type lan8841_type
= {
492 .disable_dll_tx_bit
= BIT(14),
493 .disable_dll_rx_bit
= BIT(14),
494 .disable_dll_mask
= BIT_MASK(14),
495 .cable_diag_reg
= LAN8814_CABLE_DIAG
,
496 .pair_mask
= LAN8814_WIRE_PAIR_MASK
,
499 static int kszphy_extended_write(struct phy_device
*phydev
,
502 phy_write(phydev
, MII_KSZPHY_EXTREG
, KSZPHY_EXTREG_WRITE
| regnum
);
503 return phy_write(phydev
, MII_KSZPHY_EXTREG_WRITE
, val
);
506 static int kszphy_extended_read(struct phy_device
*phydev
,
509 phy_write(phydev
, MII_KSZPHY_EXTREG
, regnum
);
510 return phy_read(phydev
, MII_KSZPHY_EXTREG_READ
);
513 static int kszphy_ack_interrupt(struct phy_device
*phydev
)
515 /* bit[7..0] int status, which is a read and clear register. */
518 rc
= phy_read(phydev
, MII_KSZPHY_INTCS
);
520 return (rc
< 0) ? rc
: 0;
523 static int kszphy_config_intr(struct phy_device
*phydev
)
525 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
529 if (type
&& type
->interrupt_level_mask
)
530 mask
= type
->interrupt_level_mask
;
532 mask
= KSZPHY_CTRL_INT_ACTIVE_HIGH
;
534 /* set the interrupt pin active low */
535 temp
= phy_read(phydev
, MII_KSZPHY_CTRL
);
539 phy_write(phydev
, MII_KSZPHY_CTRL
, temp
);
541 /* enable / disable interrupts */
542 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
543 err
= kszphy_ack_interrupt(phydev
);
547 err
= phy_write(phydev
, MII_KSZPHY_INTCS
, KSZPHY_INTCS_ALL
);
549 err
= phy_write(phydev
, MII_KSZPHY_INTCS
, 0);
553 err
= kszphy_ack_interrupt(phydev
);
559 static irqreturn_t
kszphy_handle_interrupt(struct phy_device
*phydev
)
563 irq_status
= phy_read(phydev
, MII_KSZPHY_INTCS
);
564 if (irq_status
< 0) {
569 if (!(irq_status
& KSZPHY_INTCS_STATUS
))
572 phy_trigger_machine(phydev
);
577 static int kszphy_rmii_clk_sel(struct phy_device
*phydev
, bool val
)
581 ctrl
= phy_read(phydev
, MII_KSZPHY_CTRL
);
586 ctrl
|= KSZPHY_RMII_REF_CLK_SEL
;
588 ctrl
&= ~KSZPHY_RMII_REF_CLK_SEL
;
590 return phy_write(phydev
, MII_KSZPHY_CTRL
, ctrl
);
593 static int kszphy_setup_led(struct phy_device
*phydev
, u32 reg
, int val
)
598 case MII_KSZPHY_CTRL_1
:
601 case MII_KSZPHY_CTRL_2
:
608 temp
= phy_read(phydev
, reg
);
614 temp
&= ~(3 << shift
);
615 temp
|= val
<< shift
;
616 rc
= phy_write(phydev
, reg
, temp
);
619 phydev_err(phydev
, "failed to set led mode\n");
624 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
625 * unique (non-broadcast) address on a shared bus.
627 static int kszphy_broadcast_disable(struct phy_device
*phydev
)
631 ret
= phy_read(phydev
, MII_KSZPHY_OMSO
);
635 ret
= phy_write(phydev
, MII_KSZPHY_OMSO
, ret
| KSZPHY_OMSO_B_CAST_OFF
);
638 phydev_err(phydev
, "failed to disable broadcast address\n");
643 static int kszphy_nand_tree_disable(struct phy_device
*phydev
)
647 ret
= phy_read(phydev
, MII_KSZPHY_OMSO
);
651 if (!(ret
& KSZPHY_OMSO_NAND_TREE_ON
))
654 ret
= phy_write(phydev
, MII_KSZPHY_OMSO
,
655 ret
& ~KSZPHY_OMSO_NAND_TREE_ON
);
658 phydev_err(phydev
, "failed to disable NAND tree mode\n");
663 /* Some config bits need to be set again on resume, handle them here. */
664 static int kszphy_config_reset(struct phy_device
*phydev
)
666 struct kszphy_priv
*priv
= phydev
->priv
;
669 if (priv
->rmii_ref_clk_sel
) {
670 ret
= kszphy_rmii_clk_sel(phydev
, priv
->rmii_ref_clk_sel_val
);
673 "failed to set rmii reference clock\n");
678 if (priv
->type
&& priv
->led_mode
>= 0)
679 kszphy_setup_led(phydev
, priv
->type
->led_mode_reg
, priv
->led_mode
);
684 static int kszphy_config_init(struct phy_device
*phydev
)
686 struct kszphy_priv
*priv
= phydev
->priv
;
687 const struct kszphy_type
*type
;
694 if (type
&& type
->has_broadcast_disable
)
695 kszphy_broadcast_disable(phydev
);
697 if (type
&& type
->has_nand_tree_disable
)
698 kszphy_nand_tree_disable(phydev
);
700 return kszphy_config_reset(phydev
);
703 static int ksz8041_fiber_mode(struct phy_device
*phydev
)
705 struct device_node
*of_node
= phydev
->mdio
.dev
.of_node
;
707 return of_property_read_bool(of_node
, "micrel,fiber-mode");
710 static int ksz8041_config_init(struct phy_device
*phydev
)
712 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
714 /* Limit supported and advertised modes in fiber mode */
715 if (ksz8041_fiber_mode(phydev
)) {
716 phydev
->dev_flags
|= MICREL_PHY_FXEN
;
717 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
, mask
);
718 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
, mask
);
720 linkmode_and(phydev
->supported
, phydev
->supported
, mask
);
721 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT
,
723 linkmode_and(phydev
->advertising
, phydev
->advertising
, mask
);
724 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT
,
725 phydev
->advertising
);
726 phydev
->autoneg
= AUTONEG_DISABLE
;
729 return kszphy_config_init(phydev
);
732 static int ksz8041_config_aneg(struct phy_device
*phydev
)
734 /* Skip auto-negotiation in fiber mode */
735 if (phydev
->dev_flags
& MICREL_PHY_FXEN
) {
736 phydev
->speed
= SPEED_100
;
740 return genphy_config_aneg(phydev
);
743 static int ksz8051_ksz8795_match_phy_device(struct phy_device
*phydev
,
748 if (!phy_id_compare(phydev
->phy_id
, PHY_ID_KSZ8051
, MICREL_PHY_ID_MASK
))
751 ret
= phy_read(phydev
, MII_BMSR
);
755 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
756 * exact PHY ID. However, they can be told apart by the extended
757 * capability registers presence. The KSZ8051 PHY has them while
758 * the switch does not.
767 static int ksz8051_match_phy_device(struct phy_device
*phydev
)
769 return ksz8051_ksz8795_match_phy_device(phydev
, true);
772 static int ksz8081_config_init(struct phy_device
*phydev
)
774 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
775 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
776 * pull-down is missing, the factory test mode should be cleared by
777 * manually writing a 0.
779 phy_clear_bits(phydev
, MII_KSZPHY_OMSO
, KSZPHY_OMSO_FACTORY_TEST
);
781 return kszphy_config_init(phydev
);
784 static int ksz8081_config_mdix(struct phy_device
*phydev
, u8 ctrl
)
790 val
= KSZ8081_CTRL2_DISABLE_AUTO_MDIX
;
793 val
= KSZ8081_CTRL2_DISABLE_AUTO_MDIX
|
794 KSZ8081_CTRL2_MDI_MDI_X_SELECT
;
796 case ETH_TP_MDI_AUTO
:
803 return phy_modify(phydev
, MII_KSZPHY_CTRL_2
,
804 KSZ8081_CTRL2_HP_MDIX
|
805 KSZ8081_CTRL2_MDI_MDI_X_SELECT
|
806 KSZ8081_CTRL2_DISABLE_AUTO_MDIX
,
807 KSZ8081_CTRL2_HP_MDIX
| val
);
810 static int ksz8081_config_aneg(struct phy_device
*phydev
)
814 ret
= genphy_config_aneg(phydev
);
818 /* The MDI-X configuration is automatically changed by the PHY after
819 * switching from autoneg off to on. So, take MDI-X configuration under
820 * own control and set it after autoneg configuration was done.
822 return ksz8081_config_mdix(phydev
, phydev
->mdix_ctrl
);
825 static int ksz8081_mdix_update(struct phy_device
*phydev
)
829 ret
= phy_read(phydev
, MII_KSZPHY_CTRL_2
);
833 if (ret
& KSZ8081_CTRL2_DISABLE_AUTO_MDIX
) {
834 if (ret
& KSZ8081_CTRL2_MDI_MDI_X_SELECT
)
835 phydev
->mdix_ctrl
= ETH_TP_MDI_X
;
837 phydev
->mdix_ctrl
= ETH_TP_MDI
;
839 phydev
->mdix_ctrl
= ETH_TP_MDI_AUTO
;
842 ret
= phy_read(phydev
, MII_KSZPHY_CTRL_1
);
846 if (ret
& KSZ8081_CTRL1_MDIX_STAT
)
847 phydev
->mdix
= ETH_TP_MDI
;
849 phydev
->mdix
= ETH_TP_MDI_X
;
854 static int ksz8081_read_status(struct phy_device
*phydev
)
858 ret
= ksz8081_mdix_update(phydev
);
862 return genphy_read_status(phydev
);
865 static int ksz8061_config_init(struct phy_device
*phydev
)
869 /* Chip can be powered down by the bootstrap code. */
870 ret
= phy_read(phydev
, MII_BMCR
);
873 if (ret
& BMCR_PDOWN
) {
874 ret
= phy_write(phydev
, MII_BMCR
, ret
& ~BMCR_PDOWN
);
877 usleep_range(1000, 2000);
880 ret
= phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_DEVID1
, 0xB61A);
884 return kszphy_config_init(phydev
);
887 static int ksz8795_match_phy_device(struct phy_device
*phydev
)
889 return ksz8051_ksz8795_match_phy_device(phydev
, false);
892 static int ksz9021_load_values_from_of(struct phy_device
*phydev
,
893 const struct device_node
*of_node
,
895 const char *field1
, const char *field2
,
896 const char *field3
, const char *field4
)
905 if (!of_property_read_u32(of_node
, field1
, &val1
))
908 if (!of_property_read_u32(of_node
, field2
, &val2
))
911 if (!of_property_read_u32(of_node
, field3
, &val3
))
914 if (!of_property_read_u32(of_node
, field4
, &val4
))
921 newval
= kszphy_extended_read(phydev
, reg
);
926 newval
= ((newval
& 0xfff0) | ((val1
/ PS_TO_REG
) & 0xf) << 0);
929 newval
= ((newval
& 0xff0f) | ((val2
/ PS_TO_REG
) & 0xf) << 4);
932 newval
= ((newval
& 0xf0ff) | ((val3
/ PS_TO_REG
) & 0xf) << 8);
935 newval
= ((newval
& 0x0fff) | ((val4
/ PS_TO_REG
) & 0xf) << 12);
937 return kszphy_extended_write(phydev
, reg
, newval
);
940 static int ksz9021_config_init(struct phy_device
*phydev
)
942 const struct device_node
*of_node
;
943 const struct device
*dev_walker
;
945 /* The Micrel driver has a deprecated option to place phy OF
946 * properties in the MAC node. Walk up the tree of devices to
947 * find a device with an OF node.
949 dev_walker
= &phydev
->mdio
.dev
;
951 of_node
= dev_walker
->of_node
;
952 dev_walker
= dev_walker
->parent
;
954 } while (!of_node
&& dev_walker
);
957 ksz9021_load_values_from_of(phydev
, of_node
,
958 MII_KSZPHY_CLK_CONTROL_PAD_SKEW
,
959 "txen-skew-ps", "txc-skew-ps",
960 "rxdv-skew-ps", "rxc-skew-ps");
961 ksz9021_load_values_from_of(phydev
, of_node
,
962 MII_KSZPHY_RX_DATA_PAD_SKEW
,
963 "rxd0-skew-ps", "rxd1-skew-ps",
964 "rxd2-skew-ps", "rxd3-skew-ps");
965 ksz9021_load_values_from_of(phydev
, of_node
,
966 MII_KSZPHY_TX_DATA_PAD_SKEW
,
967 "txd0-skew-ps", "txd1-skew-ps",
968 "txd2-skew-ps", "txd3-skew-ps");
973 #define KSZ9031_PS_TO_REG 60
975 /* Extended registers */
976 /* MMD Address 0x0 */
977 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3
978 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4
980 /* MMD Address 0x2 */
981 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4
982 #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4)
983 #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0)
985 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5
986 #define MII_KSZ9031RN_RXD3 GENMASK(15, 12)
987 #define MII_KSZ9031RN_RXD2 GENMASK(11, 8)
988 #define MII_KSZ9031RN_RXD1 GENMASK(7, 4)
989 #define MII_KSZ9031RN_RXD0 GENMASK(3, 0)
991 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6
992 #define MII_KSZ9031RN_TXD3 GENMASK(15, 12)
993 #define MII_KSZ9031RN_TXD2 GENMASK(11, 8)
994 #define MII_KSZ9031RN_TXD1 GENMASK(7, 4)
995 #define MII_KSZ9031RN_TXD0 GENMASK(3, 0)
997 #define MII_KSZ9031RN_CLK_PAD_SKEW 8
998 #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5)
999 #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0)
1001 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
1002 * provide different RGMII options we need to configure delay offset
1003 * for each pad relative to build in delay.
1005 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
1009 #define RX_CLK_ID 0x19
1011 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
1012 * internal 1.2ns delay.
1015 #define RX_CLK_ND 0x0
1017 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
1019 #define TX_CLK_ID 0x1f
1021 /* set tx and tx_clk to "No delay adjustment" to keep 0ns
1025 #define TX_CLK_ND 0xf
1027 /* MMD Address 0x1C */
1028 #define MII_KSZ9031RN_EDPD 0x23
1029 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0)
1031 static int ksz9031_of_load_skew_values(struct phy_device
*phydev
,
1032 const struct device_node
*of_node
,
1033 u16 reg
, size_t field_sz
,
1034 const char *field
[], u8 numfields
,
1037 int val
[4] = {-1, -2, -3, -4};
1044 for (i
= 0; i
< numfields
; i
++)
1045 if (!of_property_read_u32(of_node
, field
[i
], val
+ i
))
1053 if (matches
< numfields
)
1054 newval
= phy_read_mmd(phydev
, 2, reg
);
1058 maxval
= (field_sz
== 4) ? 0xf : 0x1f;
1059 for (i
= 0; i
< numfields
; i
++)
1060 if (val
[i
] != -(i
+ 1)) {
1062 mask
^= maxval
<< (field_sz
* i
);
1063 newval
= (newval
& mask
) |
1064 (((val
[i
] / KSZ9031_PS_TO_REG
) & maxval
)
1068 return phy_write_mmd(phydev
, 2, reg
, newval
);
1071 /* Center KSZ9031RNX FLP timing at 16ms. */
1072 static int ksz9031_center_flp_timing(struct phy_device
*phydev
)
1076 result
= phy_write_mmd(phydev
, 0, MII_KSZ9031RN_FLP_BURST_TX_HI
,
1081 result
= phy_write_mmd(phydev
, 0, MII_KSZ9031RN_FLP_BURST_TX_LO
,
1086 return genphy_restart_aneg(phydev
);
1089 /* Enable energy-detect power-down mode */
1090 static int ksz9031_enable_edpd(struct phy_device
*phydev
)
1094 reg
= phy_read_mmd(phydev
, 0x1C, MII_KSZ9031RN_EDPD
);
1097 return phy_write_mmd(phydev
, 0x1C, MII_KSZ9031RN_EDPD
,
1098 reg
| MII_KSZ9031RN_EDPD_ENABLE
);
1101 static int ksz9031_config_rgmii_delay(struct phy_device
*phydev
)
1103 u16 rx
, tx
, rx_clk
, tx_clk
;
1106 switch (phydev
->interface
) {
1107 case PHY_INTERFACE_MODE_RGMII
:
1113 case PHY_INTERFACE_MODE_RGMII_ID
:
1119 case PHY_INTERFACE_MODE_RGMII_RXID
:
1125 case PHY_INTERFACE_MODE_RGMII_TXID
:
1135 ret
= phy_write_mmd(phydev
, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW
,
1136 FIELD_PREP(MII_KSZ9031RN_RX_CTL_M
, rx
) |
1137 FIELD_PREP(MII_KSZ9031RN_TX_CTL_M
, tx
));
1141 ret
= phy_write_mmd(phydev
, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW
,
1142 FIELD_PREP(MII_KSZ9031RN_RXD3
, rx
) |
1143 FIELD_PREP(MII_KSZ9031RN_RXD2
, rx
) |
1144 FIELD_PREP(MII_KSZ9031RN_RXD1
, rx
) |
1145 FIELD_PREP(MII_KSZ9031RN_RXD0
, rx
));
1149 ret
= phy_write_mmd(phydev
, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW
,
1150 FIELD_PREP(MII_KSZ9031RN_TXD3
, tx
) |
1151 FIELD_PREP(MII_KSZ9031RN_TXD2
, tx
) |
1152 FIELD_PREP(MII_KSZ9031RN_TXD1
, tx
) |
1153 FIELD_PREP(MII_KSZ9031RN_TXD0
, tx
));
1157 return phy_write_mmd(phydev
, 2, MII_KSZ9031RN_CLK_PAD_SKEW
,
1158 FIELD_PREP(MII_KSZ9031RN_GTX_CLK
, tx_clk
) |
1159 FIELD_PREP(MII_KSZ9031RN_RX_CLK
, rx_clk
));
1162 static int ksz9031_config_init(struct phy_device
*phydev
)
1164 const struct device_node
*of_node
;
1165 static const char *clk_skews
[2] = {"rxc-skew-ps", "txc-skew-ps"};
1166 static const char *rx_data_skews
[4] = {
1167 "rxd0-skew-ps", "rxd1-skew-ps",
1168 "rxd2-skew-ps", "rxd3-skew-ps"
1170 static const char *tx_data_skews
[4] = {
1171 "txd0-skew-ps", "txd1-skew-ps",
1172 "txd2-skew-ps", "txd3-skew-ps"
1174 static const char *control_skews
[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1175 const struct device
*dev_walker
;
1178 result
= ksz9031_enable_edpd(phydev
);
1182 /* The Micrel driver has a deprecated option to place phy OF
1183 * properties in the MAC node. Walk up the tree of devices to
1184 * find a device with an OF node.
1186 dev_walker
= &phydev
->mdio
.dev
;
1188 of_node
= dev_walker
->of_node
;
1189 dev_walker
= dev_walker
->parent
;
1190 } while (!of_node
&& dev_walker
);
1193 bool update
= false;
1195 if (phy_interface_is_rgmii(phydev
)) {
1196 result
= ksz9031_config_rgmii_delay(phydev
);
1201 ksz9031_of_load_skew_values(phydev
, of_node
,
1202 MII_KSZ9031RN_CLK_PAD_SKEW
, 5,
1203 clk_skews
, 2, &update
);
1205 ksz9031_of_load_skew_values(phydev
, of_node
,
1206 MII_KSZ9031RN_CONTROL_PAD_SKEW
, 4,
1207 control_skews
, 2, &update
);
1209 ksz9031_of_load_skew_values(phydev
, of_node
,
1210 MII_KSZ9031RN_RX_DATA_PAD_SKEW
, 4,
1211 rx_data_skews
, 4, &update
);
1213 ksz9031_of_load_skew_values(phydev
, of_node
,
1214 MII_KSZ9031RN_TX_DATA_PAD_SKEW
, 4,
1215 tx_data_skews
, 4, &update
);
1217 if (update
&& !phy_interface_is_rgmii(phydev
))
1219 "*-skew-ps values should be used only with RGMII PHY modes\n");
1221 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
1222 * When the device links in the 1000BASE-T slave mode only,
1223 * the optional 125MHz reference output clock (CLK125_NDO)
1224 * has wide duty cycle variation.
1226 * The optional CLK125_NDO clock does not meet the RGMII
1227 * 45/55 percent (min/max) duty cycle requirement and therefore
1228 * cannot be used directly by the MAC side for clocking
1229 * applications that have setup/hold time requirements on
1230 * rising and falling clock edges.
1233 * Force the phy to be the master to receive a stable clock
1234 * which meets the duty cycle requirement.
1236 if (of_property_read_bool(of_node
, "micrel,force-master")) {
1237 result
= phy_read(phydev
, MII_CTRL1000
);
1239 goto err_force_master
;
1241 /* enable master mode, config & prefer master */
1242 result
|= CTL1000_ENABLE_MASTER
| CTL1000_AS_MASTER
;
1243 result
= phy_write(phydev
, MII_CTRL1000
, result
);
1245 goto err_force_master
;
1249 return ksz9031_center_flp_timing(phydev
);
1252 phydev_err(phydev
, "failed to force the phy to master mode\n");
1256 #define KSZ9131_SKEW_5BIT_MAX 2400
1257 #define KSZ9131_SKEW_4BIT_MAX 800
1258 #define KSZ9131_OFFSET 700
1259 #define KSZ9131_STEP 100
1261 static int ksz9131_of_load_skew_values(struct phy_device
*phydev
,
1262 struct device_node
*of_node
,
1263 u16 reg
, size_t field_sz
,
1264 char *field
[], u8 numfields
)
1266 int val
[4] = {-(1 + KSZ9131_OFFSET
), -(2 + KSZ9131_OFFSET
),
1267 -(3 + KSZ9131_OFFSET
), -(4 + KSZ9131_OFFSET
)};
1268 int skewval
, skewmax
= 0;
1275 /* psec properties in dts should mean x pico seconds */
1277 skewmax
= KSZ9131_SKEW_5BIT_MAX
;
1279 skewmax
= KSZ9131_SKEW_4BIT_MAX
;
1281 for (i
= 0; i
< numfields
; i
++)
1282 if (!of_property_read_s32(of_node
, field
[i
], &skewval
)) {
1283 if (skewval
< -KSZ9131_OFFSET
)
1284 skewval
= -KSZ9131_OFFSET
;
1285 else if (skewval
> skewmax
)
1288 val
[i
] = skewval
+ KSZ9131_OFFSET
;
1295 if (matches
< numfields
)
1296 newval
= phy_read_mmd(phydev
, 2, reg
);
1300 maxval
= (field_sz
== 4) ? 0xf : 0x1f;
1301 for (i
= 0; i
< numfields
; i
++)
1302 if (val
[i
] != -(i
+ 1 + KSZ9131_OFFSET
)) {
1304 mask
^= maxval
<< (field_sz
* i
);
1305 newval
= (newval
& mask
) |
1306 (((val
[i
] / KSZ9131_STEP
) & maxval
)
1310 return phy_write_mmd(phydev
, 2, reg
, newval
);
1313 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2
1314 #define KSZ9131RN_RXC_DLL_CTRL 76
1315 #define KSZ9131RN_TXC_DLL_CTRL 77
1316 #define KSZ9131RN_DLL_ENABLE_DELAY 0
1318 static int ksz9131_config_rgmii_delay(struct phy_device
*phydev
)
1320 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
1321 u16 rxcdll_val
, txcdll_val
;
1324 switch (phydev
->interface
) {
1325 case PHY_INTERFACE_MODE_RGMII
:
1326 rxcdll_val
= type
->disable_dll_rx_bit
;
1327 txcdll_val
= type
->disable_dll_tx_bit
;
1329 case PHY_INTERFACE_MODE_RGMII_ID
:
1330 rxcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
1331 txcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
1333 case PHY_INTERFACE_MODE_RGMII_RXID
:
1334 rxcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
1335 txcdll_val
= type
->disable_dll_tx_bit
;
1337 case PHY_INTERFACE_MODE_RGMII_TXID
:
1338 rxcdll_val
= type
->disable_dll_rx_bit
;
1339 txcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
1345 ret
= phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
1346 KSZ9131RN_RXC_DLL_CTRL
, type
->disable_dll_mask
,
1351 return phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
1352 KSZ9131RN_TXC_DLL_CTRL
, type
->disable_dll_mask
,
1356 /* Silicon Errata DS80000693B
1358 * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
1359 * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
1360 * according to the datasheet (off if there is no link).
1362 static int ksz9131_led_errata(struct phy_device
*phydev
)
1366 reg
= phy_read_mmd(phydev
, 2, 0);
1370 if (!(reg
& BIT(4)))
1373 return phy_set_bits(phydev
, 0x1e, BIT(9));
1376 static int ksz9131_config_init(struct phy_device
*phydev
)
1378 struct device_node
*of_node
;
1379 char *clk_skews
[2] = {"rxc-skew-psec", "txc-skew-psec"};
1380 char *rx_data_skews
[4] = {
1381 "rxd0-skew-psec", "rxd1-skew-psec",
1382 "rxd2-skew-psec", "rxd3-skew-psec"
1384 char *tx_data_skews
[4] = {
1385 "txd0-skew-psec", "txd1-skew-psec",
1386 "txd2-skew-psec", "txd3-skew-psec"
1388 char *control_skews
[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1389 const struct device
*dev_walker
;
1392 phydev
->mdix_ctrl
= ETH_TP_MDI_AUTO
;
1394 dev_walker
= &phydev
->mdio
.dev
;
1396 of_node
= dev_walker
->of_node
;
1397 dev_walker
= dev_walker
->parent
;
1398 } while (!of_node
&& dev_walker
);
1403 if (phy_interface_is_rgmii(phydev
)) {
1404 ret
= ksz9131_config_rgmii_delay(phydev
);
1409 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
1410 MII_KSZ9031RN_CLK_PAD_SKEW
, 5,
1415 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
1416 MII_KSZ9031RN_CONTROL_PAD_SKEW
, 4,
1421 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
1422 MII_KSZ9031RN_RX_DATA_PAD_SKEW
, 4,
1427 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
1428 MII_KSZ9031RN_TX_DATA_PAD_SKEW
, 4,
1433 ret
= ksz9131_led_errata(phydev
);
1440 #define MII_KSZ9131_AUTO_MDIX 0x1C
1441 #define MII_KSZ9131_AUTO_MDI_SET BIT(7)
1442 #define MII_KSZ9131_AUTO_MDIX_SWAP_OFF BIT(6)
1443 #define MII_KSZ9131_DIG_AXAN_STS 0x14
1444 #define MII_KSZ9131_DIG_AXAN_STS_LINK_DET BIT(14)
1445 #define MII_KSZ9131_DIG_AXAN_STS_A_SELECT BIT(12)
1447 static int ksz9131_mdix_update(struct phy_device
*phydev
)
1451 if (phydev
->mdix_ctrl
!= ETH_TP_MDI_AUTO
) {
1452 phydev
->mdix
= phydev
->mdix_ctrl
;
1454 ret
= phy_read(phydev
, MII_KSZ9131_DIG_AXAN_STS
);
1458 if (ret
& MII_KSZ9131_DIG_AXAN_STS_LINK_DET
) {
1459 if (ret
& MII_KSZ9131_DIG_AXAN_STS_A_SELECT
)
1460 phydev
->mdix
= ETH_TP_MDI
;
1462 phydev
->mdix
= ETH_TP_MDI_X
;
1464 phydev
->mdix
= ETH_TP_MDI_INVALID
;
1471 static int ksz9131_config_mdix(struct phy_device
*phydev
, u8 ctrl
)
1477 val
= MII_KSZ9131_AUTO_MDIX_SWAP_OFF
|
1478 MII_KSZ9131_AUTO_MDI_SET
;
1481 val
= MII_KSZ9131_AUTO_MDIX_SWAP_OFF
;
1483 case ETH_TP_MDI_AUTO
:
1490 return phy_modify(phydev
, MII_KSZ9131_AUTO_MDIX
,
1491 MII_KSZ9131_AUTO_MDIX_SWAP_OFF
|
1492 MII_KSZ9131_AUTO_MDI_SET
, val
);
1495 static int ksz9131_read_status(struct phy_device
*phydev
)
1499 ret
= ksz9131_mdix_update(phydev
);
1503 return genphy_read_status(phydev
);
1506 static int ksz9131_config_aneg(struct phy_device
*phydev
)
1510 ret
= ksz9131_config_mdix(phydev
, phydev
->mdix_ctrl
);
1514 return genphy_config_aneg(phydev
);
1517 static int ksz9477_get_features(struct phy_device
*phydev
)
1521 ret
= genphy_read_abilities(phydev
);
1525 /* The "EEE control and capability 1" (Register 3.20) seems to be
1526 * influenced by the "EEE advertisement 1" (Register 7.60). Changes
1527 * on the 7.60 will affect 3.20. So, we need to construct our own list
1529 * KSZ8563R should have 100BaseTX/Full only.
1531 linkmode_and(phydev
->supported_eee
, phydev
->supported
,
1532 PHY_EEE_CAP1_FEATURES
);
1537 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
1538 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6)
1539 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4)
1540 static int ksz8873mll_read_status(struct phy_device
*phydev
)
1545 regval
= phy_read(phydev
, KSZ8873MLL_GLOBAL_CONTROL_4
);
1547 regval
= phy_read(phydev
, KSZ8873MLL_GLOBAL_CONTROL_4
);
1549 if (regval
& KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX
)
1550 phydev
->duplex
= DUPLEX_HALF
;
1552 phydev
->duplex
= DUPLEX_FULL
;
1554 if (regval
& KSZ8873MLL_GLOBAL_CONTROL_4_SPEED
)
1555 phydev
->speed
= SPEED_10
;
1557 phydev
->speed
= SPEED_100
;
1560 phydev
->pause
= phydev
->asym_pause
= 0;
1565 static int ksz9031_get_features(struct phy_device
*phydev
)
1569 ret
= genphy_read_abilities(phydev
);
1573 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
1574 * Whenever the device's Asymmetric Pause capability is set to 1,
1575 * link-up may fail after a link-up to link-down transition.
1577 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1580 * Do not enable the Asymmetric Pause capability bit.
1582 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT
, phydev
->supported
);
1584 /* We force setting the Pause capability as the core will force the
1585 * Asymmetric Pause capability to 1 otherwise.
1587 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT
, phydev
->supported
);
1592 static int ksz9031_read_status(struct phy_device
*phydev
)
1597 err
= genphy_read_status(phydev
);
1601 /* Make sure the PHY is not broken. Read idle error count,
1602 * and reset the PHY if it is maxed out.
1604 regval
= phy_read(phydev
, MII_STAT1000
);
1605 if ((regval
& 0xFF) == 0xFF) {
1606 phy_init_hw(phydev
);
1608 if (phydev
->drv
->config_intr
&& phy_interrupt_is_valid(phydev
))
1609 phydev
->drv
->config_intr(phydev
);
1610 return genphy_config_aneg(phydev
);
1616 static int ksz9x31_cable_test_start(struct phy_device
*phydev
)
1618 struct kszphy_priv
*priv
= phydev
->priv
;
1621 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1622 * Prior to running the cable diagnostics, Auto-negotiation should
1623 * be disabled, full duplex set and the link speed set to 1000Mbps
1624 * via the Basic Control Register.
1626 ret
= phy_modify(phydev
, MII_BMCR
,
1627 BMCR_SPEED1000
| BMCR_FULLDPLX
|
1628 BMCR_ANENABLE
| BMCR_SPEED100
,
1629 BMCR_SPEED1000
| BMCR_FULLDPLX
);
1633 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1634 * The Master-Slave configuration should be set to Slave by writing
1635 * a value of 0x1000 to the Auto-Negotiation Master Slave Control
1638 ret
= phy_read(phydev
, MII_CTRL1000
);
1642 /* Cache these bits, they need to be restored once LinkMD finishes. */
1643 priv
->vct_ctrl1000
= ret
& (CTL1000_ENABLE_MASTER
| CTL1000_AS_MASTER
);
1644 ret
&= ~(CTL1000_ENABLE_MASTER
| CTL1000_AS_MASTER
);
1645 ret
|= CTL1000_ENABLE_MASTER
;
1647 return phy_write(phydev
, MII_CTRL1000
, ret
);
1650 static int ksz9x31_cable_test_result_trans(u16 status
)
1652 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK
, status
)) {
1653 case KSZ9x31_LMD_VCT_ST_NORMAL
:
1654 return ETHTOOL_A_CABLE_RESULT_CODE_OK
;
1655 case KSZ9x31_LMD_VCT_ST_OPEN
:
1656 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN
;
1657 case KSZ9x31_LMD_VCT_ST_SHORT
:
1658 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT
;
1659 case KSZ9x31_LMD_VCT_ST_FAIL
:
1662 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC
;
1666 static bool ksz9x31_cable_test_failed(u16 status
)
1668 int stat
= FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK
, status
);
1670 return stat
== KSZ9x31_LMD_VCT_ST_FAIL
;
1673 static bool ksz9x31_cable_test_fault_length_valid(u16 status
)
1675 switch (FIELD_GET(KSZ9x31_LMD_VCT_ST_MASK
, status
)) {
1676 case KSZ9x31_LMD_VCT_ST_OPEN
:
1678 case KSZ9x31_LMD_VCT_ST_SHORT
:
1684 static int ksz9x31_cable_test_fault_length(struct phy_device
*phydev
, u16 stat
)
1686 int dt
= FIELD_GET(KSZ9x31_LMD_VCT_DATA_MASK
, stat
);
1688 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1690 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
1692 if (phydev_id_compare(phydev
, PHY_ID_KSZ9131
))
1693 dt
= clamp(dt
- 22, 0, 255);
1695 return (dt
* 400) / 10;
1698 static int ksz9x31_cable_test_wait_for_completion(struct phy_device
*phydev
)
1702 ret
= phy_read_poll_timeout(phydev
, KSZ9x31_LMD
, val
,
1703 !(val
& KSZ9x31_LMD_VCT_EN
),
1704 30000, 100000, true);
1706 return ret
< 0 ? ret
: 0;
1709 static int ksz9x31_cable_test_get_pair(int pair
)
1711 static const int ethtool_pair
[] = {
1712 ETHTOOL_A_CABLE_PAIR_A
,
1713 ETHTOOL_A_CABLE_PAIR_B
,
1714 ETHTOOL_A_CABLE_PAIR_C
,
1715 ETHTOOL_A_CABLE_PAIR_D
,
1718 return ethtool_pair
[pair
];
1721 static int ksz9x31_cable_test_one_pair(struct phy_device
*phydev
, int pair
)
1725 /* KSZ9131RNX, DS00002841B-page 38, 4.14 LinkMD (R) Cable Diagnostic
1726 * To test each individual cable pair, set the cable pair in the Cable
1727 * Diagnostics Test Pair (VCT_PAIR[1:0]) field of the LinkMD Cable
1728 * Diagnostic Register, along with setting the Cable Diagnostics Test
1729 * Enable (VCT_EN) bit. The Cable Diagnostics Test Enable (VCT_EN) bit
1730 * will self clear when the test is concluded.
1732 ret
= phy_write(phydev
, KSZ9x31_LMD
,
1733 KSZ9x31_LMD_VCT_EN
| KSZ9x31_LMD_VCT_PAIR(pair
));
1737 ret
= ksz9x31_cable_test_wait_for_completion(phydev
);
1741 val
= phy_read(phydev
, KSZ9x31_LMD
);
1745 if (ksz9x31_cable_test_failed(val
))
1748 ret
= ethnl_cable_test_result(phydev
,
1749 ksz9x31_cable_test_get_pair(pair
),
1750 ksz9x31_cable_test_result_trans(val
));
1754 if (!ksz9x31_cable_test_fault_length_valid(val
))
1757 return ethnl_cable_test_fault_length(phydev
,
1758 ksz9x31_cable_test_get_pair(pair
),
1759 ksz9x31_cable_test_fault_length(phydev
, val
));
1762 static int ksz9x31_cable_test_get_status(struct phy_device
*phydev
,
1765 struct kszphy_priv
*priv
= phydev
->priv
;
1766 unsigned long pair_mask
= 0xf;
1772 /* Try harder if link partner is active */
1773 while (pair_mask
&& retries
--) {
1774 for_each_set_bit(pair
, &pair_mask
, 4) {
1775 ret
= ksz9x31_cable_test_one_pair(phydev
, pair
);
1780 clear_bit(pair
, &pair_mask
);
1782 /* If link partner is in autonegotiation mode it will send 2ms
1783 * of FLPs with at least 6ms of silence.
1784 * Add 2ms sleep to have better chances to hit this silence.
1787 usleep_range(2000, 3000);
1790 /* Report remaining unfinished pair result as unknown. */
1791 for_each_set_bit(pair
, &pair_mask
, 4) {
1792 ret
= ethnl_cable_test_result(phydev
,
1793 ksz9x31_cable_test_get_pair(pair
),
1794 ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC
);
1799 /* Restore cached bits from before LinkMD got started. */
1800 rv
= phy_modify(phydev
, MII_CTRL1000
,
1801 CTL1000_ENABLE_MASTER
| CTL1000_AS_MASTER
,
1802 priv
->vct_ctrl1000
);
1809 static int ksz8873mll_config_aneg(struct phy_device
*phydev
)
1814 static int ksz886x_config_mdix(struct phy_device
*phydev
, u8 ctrl
)
1820 val
= KSZ886X_BMCR_DISABLE_AUTO_MDIX
;
1823 /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
1824 * counter intuitive, the "-X" in "1 = Force MDI" in the data
1825 * sheet seems to be missing:
1826 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
1827 * 0 = Normal operation (transmit on TX+/TX- pins)
1829 val
= KSZ886X_BMCR_DISABLE_AUTO_MDIX
| KSZ886X_BMCR_FORCE_MDI
;
1831 case ETH_TP_MDI_AUTO
:
1838 return phy_modify(phydev
, MII_BMCR
,
1839 KSZ886X_BMCR_HP_MDIX
| KSZ886X_BMCR_FORCE_MDI
|
1840 KSZ886X_BMCR_DISABLE_AUTO_MDIX
,
1841 KSZ886X_BMCR_HP_MDIX
| val
);
1844 static int ksz886x_config_aneg(struct phy_device
*phydev
)
1848 ret
= genphy_config_aneg(phydev
);
1852 if (phydev
->autoneg
!= AUTONEG_ENABLE
) {
1853 /* When autonegotation is disabled, we need to manually force
1854 * the link state. If we don't do this, the PHY will keep
1855 * sending Fast Link Pulses (FLPs) which are part of the
1856 * autonegotiation process. This is not desired when
1857 * autonegotiation is off.
1859 ret
= phy_set_bits(phydev
, MII_KSZPHY_CTRL
,
1860 KSZ886X_CTRL_FORCE_LINK
);
1864 /* If we had previously forced the link state, we need to
1865 * clear KSZ886X_CTRL_FORCE_LINK bit now. Otherwise, the PHY
1866 * will not perform autonegotiation.
1868 ret
= phy_clear_bits(phydev
, MII_KSZPHY_CTRL
,
1869 KSZ886X_CTRL_FORCE_LINK
);
1874 /* The MDI-X configuration is automatically changed by the PHY after
1875 * switching from autoneg off to on. So, take MDI-X configuration under
1876 * own control and set it after autoneg configuration was done.
1878 return ksz886x_config_mdix(phydev
, phydev
->mdix_ctrl
);
1881 static int ksz886x_mdix_update(struct phy_device
*phydev
)
1885 ret
= phy_read(phydev
, MII_BMCR
);
1889 if (ret
& KSZ886X_BMCR_DISABLE_AUTO_MDIX
) {
1890 if (ret
& KSZ886X_BMCR_FORCE_MDI
)
1891 phydev
->mdix_ctrl
= ETH_TP_MDI_X
;
1893 phydev
->mdix_ctrl
= ETH_TP_MDI
;
1895 phydev
->mdix_ctrl
= ETH_TP_MDI_AUTO
;
1898 ret
= phy_read(phydev
, MII_KSZPHY_CTRL
);
1902 /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
1903 if (ret
& KSZ886X_CTRL_MDIX_STAT
)
1904 phydev
->mdix
= ETH_TP_MDI_X
;
1906 phydev
->mdix
= ETH_TP_MDI
;
1911 static int ksz886x_read_status(struct phy_device
*phydev
)
1915 ret
= ksz886x_mdix_update(phydev
);
1919 return genphy_read_status(phydev
);
1922 struct ksz9477_errata_write
{
1928 static const struct ksz9477_errata_write ksz9477_errata_writes
[] = {
1929 /* Register settings are needed to improve PHY receive performance */
1930 {0x01, 0x6f, 0xdd0b},
1931 {0x01, 0x8f, 0x6032},
1932 {0x01, 0x9d, 0x248c},
1933 {0x01, 0x75, 0x0060},
1934 {0x01, 0xd3, 0x7777},
1935 {0x1c, 0x06, 0x3008},
1936 {0x1c, 0x08, 0x2000},
1938 /* Transmit waveform amplitude can be improved (1000BASE-T, 100BASE-TX, 10BASE-Te) */
1939 {0x1c, 0x04, 0x00d0},
1941 /* Register settings are required to meet data sheet supply current specifications */
1942 {0x1c, 0x13, 0x6eff},
1943 {0x1c, 0x14, 0xe6ff},
1944 {0x1c, 0x15, 0x6eff},
1945 {0x1c, 0x16, 0xe6ff},
1946 {0x1c, 0x17, 0x00ff},
1947 {0x1c, 0x18, 0x43ff},
1948 {0x1c, 0x19, 0xc3ff},
1949 {0x1c, 0x1a, 0x6fff},
1950 {0x1c, 0x1b, 0x07ff},
1951 {0x1c, 0x1c, 0x0fff},
1952 {0x1c, 0x1d, 0xe7ff},
1953 {0x1c, 0x1e, 0xefff},
1954 {0x1c, 0x20, 0xeeee},
1957 static int ksz9477_phy_errata(struct phy_device
*phydev
)
1962 /* Apply PHY settings to address errata listed in
1963 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1964 * Silicon Errata and Data Sheet Clarification documents.
1966 * Document notes: Before configuring the PHY MMD registers, it is
1967 * necessary to set the PHY to 100 Mbps speed with auto-negotiation
1968 * disabled by writing to register 0xN100-0xN101. After writing the
1969 * MMD registers, and after all errata workarounds that involve PHY
1970 * register settings, write register 0xN100-0xN101 again to enable
1971 * and restart auto-negotiation.
1973 err
= phy_write(phydev
, MII_BMCR
, BMCR_SPEED100
| BMCR_FULLDPLX
);
1977 for (i
= 0; i
< ARRAY_SIZE(ksz9477_errata_writes
); ++i
) {
1978 const struct ksz9477_errata_write
*errata
= &ksz9477_errata_writes
[i
];
1980 err
= phy_write_mmd(phydev
, errata
->dev_addr
, errata
->reg_addr
, errata
->val
);
1985 err
= genphy_restart_aneg(phydev
);
1992 static int ksz9477_config_init(struct phy_device
*phydev
)
1996 /* Only KSZ9897 family of switches needs this fix. */
1997 if ((phydev
->phy_id
& 0xf) == 1) {
1998 err
= ksz9477_phy_errata(phydev
);
2003 /* According to KSZ9477 Errata DS80000754C (Module 4) all EEE modes
2004 * in this switch shall be regarded as broken.
2006 if (phydev
->dev_flags
& MICREL_NO_EEE
)
2007 linkmode_fill(phydev
->eee_broken_modes
);
2009 return kszphy_config_init(phydev
);
2012 static int kszphy_get_sset_count(struct phy_device
*phydev
)
2014 return ARRAY_SIZE(kszphy_hw_stats
);
2017 static void kszphy_get_strings(struct phy_device
*phydev
, u8
*data
)
2021 for (i
= 0; i
< ARRAY_SIZE(kszphy_hw_stats
); i
++)
2022 ethtool_puts(&data
, kszphy_hw_stats
[i
].string
);
2025 static u64
kszphy_get_stat(struct phy_device
*phydev
, int i
)
2027 struct kszphy_hw_stat stat
= kszphy_hw_stats
[i
];
2028 struct kszphy_priv
*priv
= phydev
->priv
;
2032 val
= phy_read(phydev
, stat
.reg
);
2036 val
= val
& ((1 << stat
.bits
) - 1);
2037 priv
->stats
[i
] += val
;
2038 ret
= priv
->stats
[i
];
2044 static void kszphy_get_stats(struct phy_device
*phydev
,
2045 struct ethtool_stats
*stats
, u64
*data
)
2049 for (i
= 0; i
< ARRAY_SIZE(kszphy_hw_stats
); i
++)
2050 data
[i
] = kszphy_get_stat(phydev
, i
);
2053 static int kszphy_suspend(struct phy_device
*phydev
)
2055 /* Disable PHY Interrupts */
2056 if (phy_interrupt_is_valid(phydev
)) {
2057 phydev
->interrupts
= PHY_INTERRUPT_DISABLED
;
2058 if (phydev
->drv
->config_intr
)
2059 phydev
->drv
->config_intr(phydev
);
2062 return genphy_suspend(phydev
);
2065 static void kszphy_parse_led_mode(struct phy_device
*phydev
)
2067 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
2068 const struct device_node
*np
= phydev
->mdio
.dev
.of_node
;
2069 struct kszphy_priv
*priv
= phydev
->priv
;
2072 if (type
&& type
->led_mode_reg
) {
2073 ret
= of_property_read_u32(np
, "micrel,led-mode",
2077 priv
->led_mode
= -1;
2079 if (priv
->led_mode
> 3) {
2080 phydev_err(phydev
, "invalid led mode: 0x%02x\n",
2082 priv
->led_mode
= -1;
2085 priv
->led_mode
= -1;
2089 static int kszphy_resume(struct phy_device
*phydev
)
2093 genphy_resume(phydev
);
2095 /* After switching from power-down to normal mode, an internal global
2096 * reset is automatically generated. Wait a minimum of 1 ms before
2097 * read/write access to the PHY registers.
2099 usleep_range(1000, 2000);
2101 ret
= kszphy_config_reset(phydev
);
2105 /* Enable PHY Interrupts */
2106 if (phy_interrupt_is_valid(phydev
)) {
2107 phydev
->interrupts
= PHY_INTERRUPT_ENABLED
;
2108 if (phydev
->drv
->config_intr
)
2109 phydev
->drv
->config_intr(phydev
);
2115 static int ksz9477_resume(struct phy_device
*phydev
)
2119 /* No need to initialize registers if not powered down. */
2120 ret
= phy_read(phydev
, MII_BMCR
);
2123 if (!(ret
& BMCR_PDOWN
))
2126 genphy_resume(phydev
);
2128 /* After switching from power-down to normal mode, an internal global
2129 * reset is automatically generated. Wait a minimum of 1 ms before
2130 * read/write access to the PHY registers.
2132 usleep_range(1000, 2000);
2134 /* Only KSZ9897 family of switches needs this fix. */
2135 if ((phydev
->phy_id
& 0xf) == 1) {
2136 ret
= ksz9477_phy_errata(phydev
);
2141 /* Enable PHY Interrupts */
2142 if (phy_interrupt_is_valid(phydev
)) {
2143 phydev
->interrupts
= PHY_INTERRUPT_ENABLED
;
2144 if (phydev
->drv
->config_intr
)
2145 phydev
->drv
->config_intr(phydev
);
2151 static int ksz8061_resume(struct phy_device
*phydev
)
2155 /* This function can be called twice when the Ethernet device is on. */
2156 ret
= phy_read(phydev
, MII_BMCR
);
2159 if (!(ret
& BMCR_PDOWN
))
2162 genphy_resume(phydev
);
2163 usleep_range(1000, 2000);
2165 /* Re-program the value after chip is reset. */
2166 ret
= phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_DEVID1
, 0xB61A);
2170 /* Enable PHY Interrupts */
2171 if (phy_interrupt_is_valid(phydev
)) {
2172 phydev
->interrupts
= PHY_INTERRUPT_ENABLED
;
2173 if (phydev
->drv
->config_intr
)
2174 phydev
->drv
->config_intr(phydev
);
2180 static int kszphy_probe(struct phy_device
*phydev
)
2182 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
2183 const struct device_node
*np
= phydev
->mdio
.dev
.of_node
;
2184 struct kszphy_priv
*priv
;
2187 priv
= devm_kzalloc(&phydev
->mdio
.dev
, sizeof(*priv
), GFP_KERNEL
);
2191 phydev
->priv
= priv
;
2195 kszphy_parse_led_mode(phydev
);
2197 clk
= devm_clk_get_optional_enabled(&phydev
->mdio
.dev
, "rmii-ref");
2198 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
2199 if (!IS_ERR_OR_NULL(clk
)) {
2200 unsigned long rate
= clk_get_rate(clk
);
2201 bool rmii_ref_clk_sel_25_mhz
;
2204 priv
->rmii_ref_clk_sel
= type
->has_rmii_ref_clk_sel
;
2205 rmii_ref_clk_sel_25_mhz
= of_property_read_bool(np
,
2206 "micrel,rmii-reference-clock-select-25-mhz");
2208 if (rate
> 24500000 && rate
< 25500000) {
2209 priv
->rmii_ref_clk_sel_val
= rmii_ref_clk_sel_25_mhz
;
2210 } else if (rate
> 49500000 && rate
< 50500000) {
2211 priv
->rmii_ref_clk_sel_val
= !rmii_ref_clk_sel_25_mhz
;
2213 phydev_err(phydev
, "Clock rate out of range: %ld\n",
2218 /* unnamed clock from the generic ethernet-phy binding */
2219 clk
= devm_clk_get_optional_enabled(&phydev
->mdio
.dev
, NULL
);
2221 return PTR_ERR(clk
);
2224 if (ksz8041_fiber_mode(phydev
))
2225 phydev
->port
= PORT_FIBRE
;
2227 /* Support legacy board-file configuration */
2228 if (phydev
->dev_flags
& MICREL_PHY_50MHZ_CLK
) {
2229 priv
->rmii_ref_clk_sel
= true;
2230 priv
->rmii_ref_clk_sel_val
= true;
2236 static int lan8814_cable_test_start(struct phy_device
*phydev
)
2238 /* If autoneg is enabled, we won't be able to test cross pair
2239 * short. In this case, the PHY will "detect" a link and
2240 * confuse the internal state machine - disable auto neg here.
2241 * Set the speed to 1000mbit and full duplex.
2243 return phy_modify(phydev
, MII_BMCR
, BMCR_ANENABLE
| BMCR_SPEED100
,
2244 BMCR_SPEED1000
| BMCR_FULLDPLX
);
2247 static int ksz886x_cable_test_start(struct phy_device
*phydev
)
2249 if (phydev
->dev_flags
& MICREL_KSZ8_P1_ERRATA
)
2252 /* If autoneg is enabled, we won't be able to test cross pair
2253 * short. In this case, the PHY will "detect" a link and
2254 * confuse the internal state machine - disable auto neg here.
2255 * If autoneg is disabled, we should set the speed to 10mbit.
2257 return phy_clear_bits(phydev
, MII_BMCR
, BMCR_ANENABLE
| BMCR_SPEED100
);
2260 static __always_inline
int ksz886x_cable_test_result_trans(u16 status
, u16 mask
)
2262 switch (FIELD_GET(mask
, status
)) {
2263 case KSZ8081_LMD_STAT_NORMAL
:
2264 return ETHTOOL_A_CABLE_RESULT_CODE_OK
;
2265 case KSZ8081_LMD_STAT_SHORT
:
2266 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT
;
2267 case KSZ8081_LMD_STAT_OPEN
:
2268 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN
;
2269 case KSZ8081_LMD_STAT_FAIL
:
2272 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC
;
2276 static __always_inline
bool ksz886x_cable_test_failed(u16 status
, u16 mask
)
2278 return FIELD_GET(mask
, status
) ==
2279 KSZ8081_LMD_STAT_FAIL
;
2282 static __always_inline
bool ksz886x_cable_test_fault_length_valid(u16 status
, u16 mask
)
2284 switch (FIELD_GET(mask
, status
)) {
2285 case KSZ8081_LMD_STAT_OPEN
:
2287 case KSZ8081_LMD_STAT_SHORT
:
2293 static __always_inline
int ksz886x_cable_test_fault_length(struct phy_device
*phydev
,
2294 u16 status
, u16 data_mask
)
2298 /* According to the data sheet the distance to the fault is
2299 * DELTA_TIME * 0.4 meters for ksz phys.
2300 * (DELTA_TIME - 22) * 0.8 for lan8814 phy.
2302 dt
= FIELD_GET(data_mask
, status
);
2304 if (phydev_id_compare(phydev
, PHY_ID_LAN8814
))
2305 return ((dt
- 22) * 800) / 10;
2307 return (dt
* 400) / 10;
2310 static int ksz886x_cable_test_wait_for_completion(struct phy_device
*phydev
)
2312 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
2315 ret
= phy_read_poll_timeout(phydev
, type
->cable_diag_reg
, val
,
2316 !(val
& KSZ8081_LMD_ENABLE_TEST
),
2317 30000, 100000, true);
2319 return ret
< 0 ? ret
: 0;
2322 static int lan8814_cable_test_one_pair(struct phy_device
*phydev
, int pair
)
2324 static const int ethtool_pair
[] = { ETHTOOL_A_CABLE_PAIR_A
,
2325 ETHTOOL_A_CABLE_PAIR_B
,
2326 ETHTOOL_A_CABLE_PAIR_C
,
2327 ETHTOOL_A_CABLE_PAIR_D
,
2333 val
= KSZ8081_LMD_ENABLE_TEST
;
2334 val
= val
| (pair
<< LAN8814_PAIR_BIT_SHIFT
);
2336 ret
= phy_write(phydev
, LAN8814_CABLE_DIAG
, val
);
2340 ret
= ksz886x_cable_test_wait_for_completion(phydev
);
2344 val
= phy_read(phydev
, LAN8814_CABLE_DIAG
);
2348 if (ksz886x_cable_test_failed(val
, LAN8814_CABLE_DIAG_STAT_MASK
))
2351 ret
= ethnl_cable_test_result(phydev
, ethtool_pair
[pair
],
2352 ksz886x_cable_test_result_trans(val
,
2353 LAN8814_CABLE_DIAG_STAT_MASK
2358 if (!ksz886x_cable_test_fault_length_valid(val
, LAN8814_CABLE_DIAG_STAT_MASK
))
2361 fault_length
= ksz886x_cable_test_fault_length(phydev
, val
,
2362 LAN8814_CABLE_DIAG_VCT_DATA_MASK
);
2364 return ethnl_cable_test_fault_length(phydev
, ethtool_pair
[pair
], fault_length
);
2367 static int ksz886x_cable_test_one_pair(struct phy_device
*phydev
, int pair
)
2369 static const int ethtool_pair
[] = {
2370 ETHTOOL_A_CABLE_PAIR_A
,
2371 ETHTOOL_A_CABLE_PAIR_B
,
2376 /* There is no way to choice the pair, like we do one ksz9031.
2377 * We can workaround this limitation by using the MDI-X functionality.
2382 mdix
= ETH_TP_MDI_X
;
2384 switch (phydev
->phy_id
& MICREL_PHY_ID_MASK
) {
2385 case PHY_ID_KSZ8081
:
2386 ret
= ksz8081_config_mdix(phydev
, mdix
);
2388 case PHY_ID_KSZ886X
:
2389 ret
= ksz886x_config_mdix(phydev
, mdix
);
2398 /* Now we are ready to fire. This command will send a 100ns pulse
2401 ret
= phy_write(phydev
, KSZ8081_LMD
, KSZ8081_LMD_ENABLE_TEST
);
2405 ret
= ksz886x_cable_test_wait_for_completion(phydev
);
2409 val
= phy_read(phydev
, KSZ8081_LMD
);
2413 if (ksz886x_cable_test_failed(val
, KSZ8081_LMD_STAT_MASK
))
2416 ret
= ethnl_cable_test_result(phydev
, ethtool_pair
[pair
],
2417 ksz886x_cable_test_result_trans(val
, KSZ8081_LMD_STAT_MASK
));
2421 if (!ksz886x_cable_test_fault_length_valid(val
, KSZ8081_LMD_STAT_MASK
))
2424 fault_length
= ksz886x_cable_test_fault_length(phydev
, val
, KSZ8081_LMD_DELTA_TIME_MASK
);
2426 return ethnl_cable_test_fault_length(phydev
, ethtool_pair
[pair
], fault_length
);
2429 static int ksz886x_cable_test_get_status(struct phy_device
*phydev
,
2432 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
2433 unsigned long pair_mask
= type
->pair_mask
;
2440 /* Try harder if link partner is active */
2441 while (pair_mask
&& retries
--) {
2442 for_each_set_bit(pair
, &pair_mask
, 4) {
2443 if (type
->cable_diag_reg
== LAN8814_CABLE_DIAG
)
2444 ret
= lan8814_cable_test_one_pair(phydev
, pair
);
2446 ret
= ksz886x_cable_test_one_pair(phydev
, pair
);
2451 clear_bit(pair
, &pair_mask
);
2453 /* If link partner is in autonegotiation mode it will send 2ms
2454 * of FLPs with at least 6ms of silence.
2455 * Add 2ms sleep to have better chances to hit this silence.
2466 #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16
2467 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17
2468 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000
2470 #define LAN8814_QSGMII_SOFT_RESET 0x43
2471 #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0)
2472 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13
2473 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3)
2474 #define LAN8814_ALIGN_SWAP 0x4a
2475 #define LAN8814_ALIGN_TX_A_B_SWAP 0x1
2476 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0)
2478 #define LAN8804_ALIGN_SWAP 0x4a
2479 #define LAN8804_ALIGN_TX_A_B_SWAP 0x1
2480 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0)
2481 #define LAN8814_CLOCK_MANAGEMENT 0xd
2482 #define LAN8814_LINK_QUALITY 0x8e
2484 static int lanphy_read_page_reg(struct phy_device
*phydev
, int page
, u32 addr
)
2488 phy_lock_mdio_bus(phydev
);
2489 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_CONTROL
, page
);
2490 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA
, addr
);
2491 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_CONTROL
,
2492 (page
| LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC
));
2493 data
= __phy_read(phydev
, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA
);
2494 phy_unlock_mdio_bus(phydev
);
2499 static int lanphy_write_page_reg(struct phy_device
*phydev
, int page
, u16 addr
,
2502 phy_lock_mdio_bus(phydev
);
2503 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_CONTROL
, page
);
2504 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA
, addr
);
2505 __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_CONTROL
,
2506 page
| LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC
);
2508 val
= __phy_write(phydev
, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA
, val
);
2510 phydev_err(phydev
, "Error: phy_write has returned error %d\n",
2512 phy_unlock_mdio_bus(phydev
);
2516 static int lan8814_config_ts_intr(struct phy_device
*phydev
, bool enable
)
2521 val
= PTP_TSU_INT_EN_PTP_TX_TS_EN_
|
2522 PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_
|
2523 PTP_TSU_INT_EN_PTP_RX_TS_EN_
|
2524 PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_
;
2526 return lanphy_write_page_reg(phydev
, 5, PTP_TSU_INT_EN
, val
);
2529 static void lan8814_ptp_rx_ts_get(struct phy_device
*phydev
,
2530 u32
*seconds
, u32
*nano_seconds
, u16
*seq_id
)
2532 *seconds
= lanphy_read_page_reg(phydev
, 5, PTP_RX_INGRESS_SEC_HI
);
2533 *seconds
= (*seconds
<< 16) |
2534 lanphy_read_page_reg(phydev
, 5, PTP_RX_INGRESS_SEC_LO
);
2536 *nano_seconds
= lanphy_read_page_reg(phydev
, 5, PTP_RX_INGRESS_NS_HI
);
2537 *nano_seconds
= ((*nano_seconds
& 0x3fff) << 16) |
2538 lanphy_read_page_reg(phydev
, 5, PTP_RX_INGRESS_NS_LO
);
2540 *seq_id
= lanphy_read_page_reg(phydev
, 5, PTP_RX_MSG_HEADER2
);
2543 static void lan8814_ptp_tx_ts_get(struct phy_device
*phydev
,
2544 u32
*seconds
, u32
*nano_seconds
, u16
*seq_id
)
2546 *seconds
= lanphy_read_page_reg(phydev
, 5, PTP_TX_EGRESS_SEC_HI
);
2547 *seconds
= *seconds
<< 16 |
2548 lanphy_read_page_reg(phydev
, 5, PTP_TX_EGRESS_SEC_LO
);
2550 *nano_seconds
= lanphy_read_page_reg(phydev
, 5, PTP_TX_EGRESS_NS_HI
);
2551 *nano_seconds
= ((*nano_seconds
& 0x3fff) << 16) |
2552 lanphy_read_page_reg(phydev
, 5, PTP_TX_EGRESS_NS_LO
);
2554 *seq_id
= lanphy_read_page_reg(phydev
, 5, PTP_TX_MSG_HEADER2
);
2557 static int lan8814_ts_info(struct mii_timestamper
*mii_ts
, struct kernel_ethtool_ts_info
*info
)
2559 struct kszphy_ptp_priv
*ptp_priv
= container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
2560 struct phy_device
*phydev
= ptp_priv
->phydev
;
2561 struct lan8814_shared_priv
*shared
= phydev
->shared
->priv
;
2563 info
->so_timestamping
= SOF_TIMESTAMPING_TX_HARDWARE
|
2564 SOF_TIMESTAMPING_RX_HARDWARE
|
2565 SOF_TIMESTAMPING_RAW_HARDWARE
;
2567 info
->phc_index
= ptp_clock_index(shared
->ptp_clock
);
2570 (1 << HWTSTAMP_TX_OFF
) |
2571 (1 << HWTSTAMP_TX_ON
) |
2572 (1 << HWTSTAMP_TX_ONESTEP_SYNC
);
2575 (1 << HWTSTAMP_FILTER_NONE
) |
2576 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT
) |
2577 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT
) |
2578 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
2579 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT
);
2584 static void lan8814_flush_fifo(struct phy_device
*phydev
, bool egress
)
2588 for (i
= 0; i
< FIFO_SIZE
; ++i
)
2589 lanphy_read_page_reg(phydev
, 5,
2590 egress
? PTP_TX_MSG_HEADER2
: PTP_RX_MSG_HEADER2
);
2592 /* Read to clear overflow status bit */
2593 lanphy_read_page_reg(phydev
, 5, PTP_TSU_INT_STS
);
2596 static int lan8814_hwtstamp(struct mii_timestamper
*mii_ts
,
2597 struct kernel_hwtstamp_config
*config
,
2598 struct netlink_ext_ack
*extack
)
2600 struct kszphy_ptp_priv
*ptp_priv
=
2601 container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
2602 struct lan8814_ptp_rx_ts
*rx_ts
, *tmp
;
2603 int txcfg
= 0, rxcfg
= 0;
2607 ptp_priv
->hwts_tx_type
= config
->tx_type
;
2608 ptp_priv
->rx_filter
= config
->rx_filter
;
2610 switch (config
->rx_filter
) {
2611 case HWTSTAMP_FILTER_NONE
:
2612 ptp_priv
->layer
= 0;
2613 ptp_priv
->version
= 0;
2615 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
2616 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
2617 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
2618 ptp_priv
->layer
= PTP_CLASS_L4
;
2619 ptp_priv
->version
= PTP_CLASS_V2
;
2621 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
2622 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
2623 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
2624 ptp_priv
->layer
= PTP_CLASS_L2
;
2625 ptp_priv
->version
= PTP_CLASS_V2
;
2627 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
2628 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
2629 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
2630 ptp_priv
->layer
= PTP_CLASS_L4
| PTP_CLASS_L2
;
2631 ptp_priv
->version
= PTP_CLASS_V2
;
2637 if (ptp_priv
->layer
& PTP_CLASS_L2
) {
2638 rxcfg
= PTP_RX_PARSE_CONFIG_LAYER2_EN_
;
2639 txcfg
= PTP_TX_PARSE_CONFIG_LAYER2_EN_
;
2640 } else if (ptp_priv
->layer
& PTP_CLASS_L4
) {
2641 rxcfg
|= PTP_RX_PARSE_CONFIG_IPV4_EN_
| PTP_RX_PARSE_CONFIG_IPV6_EN_
;
2642 txcfg
|= PTP_TX_PARSE_CONFIG_IPV4_EN_
| PTP_TX_PARSE_CONFIG_IPV6_EN_
;
2644 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_RX_PARSE_CONFIG
, rxcfg
);
2645 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_TX_PARSE_CONFIG
, txcfg
);
2647 pkt_ts_enable
= PTP_TIMESTAMP_EN_SYNC_
| PTP_TIMESTAMP_EN_DREQ_
|
2648 PTP_TIMESTAMP_EN_PDREQ_
| PTP_TIMESTAMP_EN_PDRES_
;
2649 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_RX_TIMESTAMP_EN
, pkt_ts_enable
);
2650 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_TX_TIMESTAMP_EN
, pkt_ts_enable
);
2652 tx_mod
= lanphy_read_page_reg(ptp_priv
->phydev
, 5, PTP_TX_MOD
);
2653 if (ptp_priv
->hwts_tx_type
== HWTSTAMP_TX_ONESTEP_SYNC
) {
2654 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_TX_MOD
,
2655 tx_mod
| PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_
);
2656 } else if (ptp_priv
->hwts_tx_type
== HWTSTAMP_TX_ON
) {
2657 lanphy_write_page_reg(ptp_priv
->phydev
, 5, PTP_TX_MOD
,
2658 tx_mod
& ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_
);
2661 if (config
->rx_filter
!= HWTSTAMP_FILTER_NONE
)
2662 lan8814_config_ts_intr(ptp_priv
->phydev
, true);
2664 lan8814_config_ts_intr(ptp_priv
->phydev
, false);
2666 /* In case of multiple starts and stops, these needs to be cleared */
2667 list_for_each_entry_safe(rx_ts
, tmp
, &ptp_priv
->rx_ts_list
, list
) {
2668 list_del(&rx_ts
->list
);
2671 skb_queue_purge(&ptp_priv
->rx_queue
);
2672 skb_queue_purge(&ptp_priv
->tx_queue
);
2674 lan8814_flush_fifo(ptp_priv
->phydev
, false);
2675 lan8814_flush_fifo(ptp_priv
->phydev
, true);
2680 static void lan8814_txtstamp(struct mii_timestamper
*mii_ts
,
2681 struct sk_buff
*skb
, int type
)
2683 struct kszphy_ptp_priv
*ptp_priv
= container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
2685 switch (ptp_priv
->hwts_tx_type
) {
2686 case HWTSTAMP_TX_ONESTEP_SYNC
:
2687 if (ptp_msg_is_sync(skb
, type
)) {
2692 case HWTSTAMP_TX_ON
:
2693 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
2694 skb_queue_tail(&ptp_priv
->tx_queue
, skb
);
2696 case HWTSTAMP_TX_OFF
:
2703 static bool lan8814_get_sig_rx(struct sk_buff
*skb
, u16
*sig
)
2705 struct ptp_header
*ptp_header
;
2708 skb_push(skb
, ETH_HLEN
);
2709 type
= ptp_classify_raw(skb
);
2710 ptp_header
= ptp_parse_header(skb
, type
);
2711 skb_pull_inline(skb
, ETH_HLEN
);
2716 *sig
= (__force u16
)(ntohs(ptp_header
->sequence_id
));
2720 static bool lan8814_match_rx_skb(struct kszphy_ptp_priv
*ptp_priv
,
2721 struct sk_buff
*skb
)
2723 struct skb_shared_hwtstamps
*shhwtstamps
;
2724 struct lan8814_ptp_rx_ts
*rx_ts
, *tmp
;
2725 unsigned long flags
;
2729 if (!lan8814_get_sig_rx(skb
, &skb_sig
))
2732 /* Iterate over all RX timestamps and match it with the received skbs */
2733 spin_lock_irqsave(&ptp_priv
->rx_ts_lock
, flags
);
2734 list_for_each_entry_safe(rx_ts
, tmp
, &ptp_priv
->rx_ts_list
, list
) {
2735 /* Check if we found the signature we were looking for. */
2736 if (memcmp(&skb_sig
, &rx_ts
->seq_id
, sizeof(rx_ts
->seq_id
)))
2739 shhwtstamps
= skb_hwtstamps(skb
);
2740 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
2741 shhwtstamps
->hwtstamp
= ktime_set(rx_ts
->seconds
,
2743 list_del(&rx_ts
->list
);
2749 spin_unlock_irqrestore(&ptp_priv
->rx_ts_lock
, flags
);
2756 static bool lan8814_rxtstamp(struct mii_timestamper
*mii_ts
, struct sk_buff
*skb
, int type
)
2758 struct kszphy_ptp_priv
*ptp_priv
=
2759 container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
2761 if (ptp_priv
->rx_filter
== HWTSTAMP_FILTER_NONE
||
2762 type
== PTP_CLASS_NONE
)
2765 if ((type
& ptp_priv
->version
) == 0 || (type
& ptp_priv
->layer
) == 0)
2768 /* If we failed to match then add it to the queue for when the timestamp
2771 if (!lan8814_match_rx_skb(ptp_priv
, skb
))
2772 skb_queue_tail(&ptp_priv
->rx_queue
, skb
);
2777 static void lan8814_ptp_clock_set(struct phy_device
*phydev
,
2778 time64_t sec
, u32 nsec
)
2780 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_SET_SEC_LO
, lower_16_bits(sec
));
2781 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_SET_SEC_MID
, upper_16_bits(sec
));
2782 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_SET_SEC_HI
, upper_32_bits(sec
));
2783 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_SET_NS_LO
, lower_16_bits(nsec
));
2784 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_SET_NS_HI
, upper_16_bits(nsec
));
2786 lanphy_write_page_reg(phydev
, 4, PTP_CMD_CTL
, PTP_CMD_CTL_PTP_CLOCK_LOAD_
);
2789 static void lan8814_ptp_clock_get(struct phy_device
*phydev
,
2790 time64_t
*sec
, u32
*nsec
)
2792 lanphy_write_page_reg(phydev
, 4, PTP_CMD_CTL
, PTP_CMD_CTL_PTP_CLOCK_READ_
);
2794 *sec
= lanphy_read_page_reg(phydev
, 4, PTP_CLOCK_READ_SEC_HI
);
2796 *sec
|= lanphy_read_page_reg(phydev
, 4, PTP_CLOCK_READ_SEC_MID
);
2798 *sec
|= lanphy_read_page_reg(phydev
, 4, PTP_CLOCK_READ_SEC_LO
);
2800 *nsec
= lanphy_read_page_reg(phydev
, 4, PTP_CLOCK_READ_NS_HI
);
2802 *nsec
|= lanphy_read_page_reg(phydev
, 4, PTP_CLOCK_READ_NS_LO
);
2805 static int lan8814_ptpci_gettime64(struct ptp_clock_info
*ptpci
,
2806 struct timespec64
*ts
)
2808 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
2810 struct phy_device
*phydev
= shared
->phydev
;
2814 mutex_lock(&shared
->shared_lock
);
2815 lan8814_ptp_clock_get(phydev
, &seconds
, &nano_seconds
);
2816 mutex_unlock(&shared
->shared_lock
);
2817 ts
->tv_sec
= seconds
;
2818 ts
->tv_nsec
= nano_seconds
;
2823 static int lan8814_ptpci_settime64(struct ptp_clock_info
*ptpci
,
2824 const struct timespec64
*ts
)
2826 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
2828 struct phy_device
*phydev
= shared
->phydev
;
2830 mutex_lock(&shared
->shared_lock
);
2831 lan8814_ptp_clock_set(phydev
, ts
->tv_sec
, ts
->tv_nsec
);
2832 mutex_unlock(&shared
->shared_lock
);
2837 static void lan8814_ptp_set_target(struct phy_device
*phydev
, int event
,
2838 s64 start_sec
, u32 start_nsec
)
2840 /* Set the start time */
2841 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_CLOCK_TARGET_SEC_LO(event
),
2842 lower_16_bits(start_sec
));
2843 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_CLOCK_TARGET_SEC_HI(event
),
2844 upper_16_bits(start_sec
));
2846 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_CLOCK_TARGET_NS_LO(event
),
2847 lower_16_bits(start_nsec
));
2848 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_CLOCK_TARGET_NS_HI(event
),
2849 upper_16_bits(start_nsec
) & 0x3fff);
2852 static void lan8814_ptp_update_target(struct phy_device
*phydev
, time64_t sec
)
2854 lan8814_ptp_set_target(phydev
, LAN8814_EVENT_A
,
2855 sec
+ LAN8814_BUFFER_TIME
, 0);
2856 lan8814_ptp_set_target(phydev
, LAN8814_EVENT_B
,
2857 sec
+ LAN8814_BUFFER_TIME
, 0);
2860 static void lan8814_ptp_clock_step(struct phy_device
*phydev
,
2863 u32 nano_seconds_step
;
2864 u64 abs_time_step_ns
;
2865 time64_t set_seconds
;
2870 if (time_step_ns
> 15000000000LL) {
2871 /* convert to clock set */
2872 lan8814_ptp_clock_get(phydev
, &set_seconds
, &nano_seconds
);
2873 set_seconds
+= div_u64_rem(time_step_ns
, 1000000000LL,
2875 nano_seconds
+= remainder
;
2876 if (nano_seconds
>= 1000000000) {
2878 nano_seconds
-= 1000000000;
2880 lan8814_ptp_clock_set(phydev
, set_seconds
, nano_seconds
);
2881 lan8814_ptp_update_target(phydev
, set_seconds
);
2883 } else if (time_step_ns
< -15000000000LL) {
2884 /* convert to clock set */
2885 time_step_ns
= -time_step_ns
;
2887 lan8814_ptp_clock_get(phydev
, &set_seconds
, &nano_seconds
);
2888 set_seconds
-= div_u64_rem(time_step_ns
, 1000000000LL,
2890 nano_seconds_step
= remainder
;
2891 if (nano_seconds
< nano_seconds_step
) {
2893 nano_seconds
+= 1000000000;
2895 nano_seconds
-= nano_seconds_step
;
2896 lan8814_ptp_clock_set(phydev
, set_seconds
, nano_seconds
);
2897 lan8814_ptp_update_target(phydev
, set_seconds
);
2902 if (time_step_ns
>= 0) {
2903 abs_time_step_ns
= (u64
)time_step_ns
;
2904 seconds
= (s32
)div_u64_rem(abs_time_step_ns
, 1000000000,
2906 nano_seconds
= remainder
;
2908 abs_time_step_ns
= (u64
)(-time_step_ns
);
2909 seconds
= -((s32
)div_u64_rem(abs_time_step_ns
, 1000000000,
2911 nano_seconds
= remainder
;
2912 if (nano_seconds
> 0) {
2913 /* subtracting nano seconds is not allowed
2914 * convert to subtracting from seconds,
2915 * and adding to nanoseconds
2918 nano_seconds
= (1000000000 - nano_seconds
);
2922 if (nano_seconds
> 0) {
2923 /* add 8 ns to cover the likely normal increment */
2927 if (nano_seconds
>= 1000000000) {
2928 /* carry into seconds */
2930 nano_seconds
-= 1000000000;
2937 u32 adjustment_value
= (u32
)seconds
;
2938 u16 adjustment_value_lo
, adjustment_value_hi
;
2940 if (adjustment_value
> 0xF)
2941 adjustment_value
= 0xF;
2943 adjustment_value_lo
= adjustment_value
& 0xffff;
2944 adjustment_value_hi
= (adjustment_value
>> 16) & 0x3fff;
2946 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_LO
,
2947 adjustment_value_lo
);
2948 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_HI
,
2949 PTP_LTC_STEP_ADJ_DIR_
|
2950 adjustment_value_hi
);
2951 seconds
-= ((s32
)adjustment_value
);
2953 lan8814_ptp_clock_get(phydev
, &set_seconds
, &nsec
);
2954 set_seconds
-= adjustment_value
;
2955 lan8814_ptp_update_target(phydev
, set_seconds
);
2957 u32 adjustment_value
= (u32
)(-seconds
);
2958 u16 adjustment_value_lo
, adjustment_value_hi
;
2960 if (adjustment_value
> 0xF)
2961 adjustment_value
= 0xF;
2963 adjustment_value_lo
= adjustment_value
& 0xffff;
2964 adjustment_value_hi
= (adjustment_value
>> 16) & 0x3fff;
2966 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_LO
,
2967 adjustment_value_lo
);
2968 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_HI
,
2969 adjustment_value_hi
);
2970 seconds
+= ((s32
)adjustment_value
);
2972 lan8814_ptp_clock_get(phydev
, &set_seconds
, &nsec
);
2973 set_seconds
+= adjustment_value
;
2974 lan8814_ptp_update_target(phydev
, set_seconds
);
2976 lanphy_write_page_reg(phydev
, 4, PTP_CMD_CTL
,
2977 PTP_CMD_CTL_PTP_LTC_STEP_SEC_
);
2980 u16 nano_seconds_lo
;
2981 u16 nano_seconds_hi
;
2983 nano_seconds_lo
= nano_seconds
& 0xffff;
2984 nano_seconds_hi
= (nano_seconds
>> 16) & 0x3fff;
2986 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_LO
,
2988 lanphy_write_page_reg(phydev
, 4, PTP_LTC_STEP_ADJ_HI
,
2989 PTP_LTC_STEP_ADJ_DIR_
|
2991 lanphy_write_page_reg(phydev
, 4, PTP_CMD_CTL
,
2992 PTP_CMD_CTL_PTP_LTC_STEP_NSEC_
);
2996 static int lan8814_ptpci_adjtime(struct ptp_clock_info
*ptpci
, s64 delta
)
2998 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
3000 struct phy_device
*phydev
= shared
->phydev
;
3002 mutex_lock(&shared
->shared_lock
);
3003 lan8814_ptp_clock_step(phydev
, delta
);
3004 mutex_unlock(&shared
->shared_lock
);
3009 static int lan8814_ptpci_adjfine(struct ptp_clock_info
*ptpci
, long scaled_ppm
)
3011 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
3013 struct phy_device
*phydev
= shared
->phydev
;
3014 u16 kszphy_rate_adj_lo
, kszphy_rate_adj_hi
;
3015 bool positive
= true;
3016 u32 kszphy_rate_adj
;
3018 if (scaled_ppm
< 0) {
3019 scaled_ppm
= -scaled_ppm
;
3023 kszphy_rate_adj
= LAN8814_1PPM_FORMAT
* (scaled_ppm
>> 16);
3024 kszphy_rate_adj
+= (LAN8814_1PPM_FORMAT
* (0xffff & scaled_ppm
)) >> 16;
3026 kszphy_rate_adj_lo
= kszphy_rate_adj
& 0xffff;
3027 kszphy_rate_adj_hi
= (kszphy_rate_adj
>> 16) & 0x3fff;
3030 kszphy_rate_adj_hi
|= PTP_CLOCK_RATE_ADJ_DIR_
;
3032 mutex_lock(&shared
->shared_lock
);
3033 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_RATE_ADJ_HI
, kszphy_rate_adj_hi
);
3034 lanphy_write_page_reg(phydev
, 4, PTP_CLOCK_RATE_ADJ_LO
, kszphy_rate_adj_lo
);
3035 mutex_unlock(&shared
->shared_lock
);
3040 static void lan8814_ptp_set_reload(struct phy_device
*phydev
, int event
,
3041 s64 period_sec
, u32 period_nsec
)
3043 lanphy_write_page_reg(phydev
, 4,
3044 LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_LO(event
),
3045 lower_16_bits(period_sec
));
3046 lanphy_write_page_reg(phydev
, 4,
3047 LAN8814_PTP_CLOCK_TARGET_RELOAD_SEC_HI(event
),
3048 upper_16_bits(period_sec
));
3050 lanphy_write_page_reg(phydev
, 4,
3051 LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_LO(event
),
3052 lower_16_bits(period_nsec
));
3053 lanphy_write_page_reg(phydev
, 4,
3054 LAN8814_PTP_CLOCK_TARGET_RELOAD_NS_HI(event
),
3055 upper_16_bits(period_nsec
) & 0x3fff);
3058 static void lan8814_ptp_enable_event(struct phy_device
*phydev
, int event
,
3063 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_PTP_GENERAL_CONFIG
);
3064 /* Set the pulse width of the event */
3065 val
&= ~(LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_MASK(event
));
3066 /* Make sure that the target clock will be incremented each time when
3067 * local time reaches or pass it
3069 val
|= LAN8814_PTP_GENERAL_CONFIG_LTC_EVENT_SET(event
, pulse_width
);
3070 val
&= ~(LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event
));
3071 /* Set the polarity high */
3072 val
|= LAN8814_PTP_GENERAL_CONFIG_POLARITY_X(event
);
3073 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_GENERAL_CONFIG
, val
);
3076 static void lan8814_ptp_disable_event(struct phy_device
*phydev
, int event
)
3080 /* Set target to too far in the future, effectively disabling it */
3081 lan8814_ptp_set_target(phydev
, event
, 0xFFFFFFFF, 0);
3083 /* And then reload once it recheas the target */
3084 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_PTP_GENERAL_CONFIG
);
3085 val
|= LAN8814_PTP_GENERAL_CONFIG_RELOAD_ADD_X(event
);
3086 lanphy_write_page_reg(phydev
, 4, LAN8814_PTP_GENERAL_CONFIG
, val
);
3089 static void lan8814_ptp_perout_off(struct phy_device
*phydev
, int pin
)
3093 /* Disable gpio alternate function,
3094 * 1: select as gpio,
3095 * 0: select alt func
3097 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
));
3098 val
|= LAN8814_GPIO_EN_BIT(pin
);
3099 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
), val
);
3101 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
));
3102 val
&= ~LAN8814_GPIO_DIR_BIT(pin
);
3103 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
), val
);
3105 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_BUF_ADDR(pin
));
3106 val
&= ~LAN8814_GPIO_BUF_BIT(pin
);
3107 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_BUF_ADDR(pin
), val
);
3110 static void lan8814_ptp_perout_on(struct phy_device
*phydev
, int pin
)
3114 /* Set as gpio output */
3115 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
));
3116 val
|= LAN8814_GPIO_DIR_BIT(pin
);
3117 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
), val
);
3119 /* Enable gpio 0:for alternate function, 1:gpio */
3120 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
));
3121 val
&= ~LAN8814_GPIO_EN_BIT(pin
);
3122 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
), val
);
3124 /* Set buffer type to push pull */
3125 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_BUF_ADDR(pin
));
3126 val
|= LAN8814_GPIO_BUF_BIT(pin
);
3127 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_BUF_ADDR(pin
), val
);
3130 static int lan8814_ptp_perout(struct ptp_clock_info
*ptpci
,
3131 struct ptp_clock_request
*rq
, int on
)
3133 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
3135 struct phy_device
*phydev
= shared
->phydev
;
3136 struct timespec64 ts_on
, ts_period
;
3137 s64 on_nsec
, period_nsec
;
3141 /* Reject requests with unsupported flags */
3142 if (rq
->perout
.flags
& ~PTP_PEROUT_DUTY_CYCLE
)
3145 mutex_lock(&shared
->shared_lock
);
3146 event
= rq
->perout
.index
;
3147 pin
= ptp_find_pin(shared
->ptp_clock
, PTP_PF_PEROUT
, event
);
3148 if (pin
< 0 || pin
>= LAN8814_PTP_PEROUT_NUM
) {
3149 mutex_unlock(&shared
->shared_lock
);
3154 lan8814_ptp_perout_off(phydev
, pin
);
3155 lan8814_ptp_disable_event(phydev
, event
);
3156 mutex_unlock(&shared
->shared_lock
);
3160 ts_on
.tv_sec
= rq
->perout
.on
.sec
;
3161 ts_on
.tv_nsec
= rq
->perout
.on
.nsec
;
3162 on_nsec
= timespec64_to_ns(&ts_on
);
3164 ts_period
.tv_sec
= rq
->perout
.period
.sec
;
3165 ts_period
.tv_nsec
= rq
->perout
.period
.nsec
;
3166 period_nsec
= timespec64_to_ns(&ts_period
);
3168 if (period_nsec
< 200) {
3169 pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
3170 phydev_name(phydev
));
3171 mutex_unlock(&shared
->shared_lock
);
3175 if (on_nsec
>= period_nsec
) {
3176 pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
3177 phydev_name(phydev
));
3178 mutex_unlock(&shared
->shared_lock
);
3184 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS
;
3187 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS
;
3190 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS
;
3193 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS
;
3196 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS
;
3199 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS
;
3202 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US
;
3205 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US
;
3208 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US
;
3211 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US
;
3214 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US
;
3217 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US
;
3220 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS
;
3223 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS
;
3226 pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
3227 phydev_name(phydev
));
3228 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS
;
3232 /* Configure to pulse every period */
3233 lan8814_ptp_enable_event(phydev
, event
, pulse_width
);
3234 lan8814_ptp_set_target(phydev
, event
, rq
->perout
.start
.sec
,
3235 rq
->perout
.start
.nsec
);
3236 lan8814_ptp_set_reload(phydev
, event
, rq
->perout
.period
.sec
,
3237 rq
->perout
.period
.nsec
);
3238 lan8814_ptp_perout_on(phydev
, pin
);
3239 mutex_unlock(&shared
->shared_lock
);
3244 static void lan8814_ptp_extts_on(struct phy_device
*phydev
, int pin
, u32 flags
)
3248 /* Set as gpio input */
3249 tmp
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
));
3250 tmp
&= ~LAN8814_GPIO_DIR_BIT(pin
);
3251 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
), tmp
);
3253 /* Map the pin to ltc pin 0 of the capture map registers */
3254 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_CAP_MAP_LO
);
3256 lanphy_write_page_reg(phydev
, 4, PTP_GPIO_CAP_MAP_LO
, tmp
);
3258 /* Enable capture on the edges of the ltc pin */
3259 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_CAP_EN
);
3260 if (flags
& PTP_RISING_EDGE
)
3261 tmp
|= PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(0);
3262 if (flags
& PTP_FALLING_EDGE
)
3263 tmp
|= PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(0);
3264 lanphy_write_page_reg(phydev
, 4, PTP_GPIO_CAP_EN
, tmp
);
3266 /* Enable interrupt top interrupt */
3267 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_COMMON_INT_ENA
);
3268 tmp
|= PTP_COMMON_INT_ENA_GPIO_CAP_EN
;
3269 lanphy_write_page_reg(phydev
, 4, PTP_COMMON_INT_ENA
, tmp
);
3272 static void lan8814_ptp_extts_off(struct phy_device
*phydev
, int pin
)
3276 /* Set as gpio out */
3277 tmp
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
));
3278 tmp
|= LAN8814_GPIO_DIR_BIT(pin
);
3279 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_DIR_ADDR(pin
), tmp
);
3281 /* Enable alternate, 0:for alternate function, 1:gpio */
3282 tmp
= lanphy_read_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
));
3283 tmp
&= ~LAN8814_GPIO_EN_BIT(pin
);
3284 lanphy_write_page_reg(phydev
, 4, LAN8814_GPIO_EN_ADDR(pin
), tmp
);
3286 /* Clear the mapping of pin to registers 0 of the capture registers */
3287 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_CAP_MAP_LO
);
3288 tmp
&= ~GENMASK(3, 0);
3289 lanphy_write_page_reg(phydev
, 4, PTP_GPIO_CAP_MAP_LO
, tmp
);
3291 /* Disable capture on both of the edges */
3292 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_CAP_EN
);
3293 tmp
&= ~PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin
);
3294 tmp
&= ~PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin
);
3295 lanphy_write_page_reg(phydev
, 4, PTP_GPIO_CAP_EN
, tmp
);
3297 /* Disable interrupt top interrupt */
3298 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_COMMON_INT_ENA
);
3299 tmp
&= ~PTP_COMMON_INT_ENA_GPIO_CAP_EN
;
3300 lanphy_write_page_reg(phydev
, 4, PTP_COMMON_INT_ENA
, tmp
);
3303 static int lan8814_ptp_extts(struct ptp_clock_info
*ptpci
,
3304 struct ptp_clock_request
*rq
, int on
)
3306 struct lan8814_shared_priv
*shared
= container_of(ptpci
, struct lan8814_shared_priv
,
3308 struct phy_device
*phydev
= shared
->phydev
;
3311 if (rq
->extts
.flags
& ~(PTP_ENABLE_FEATURE
|
3316 pin
= ptp_find_pin(shared
->ptp_clock
, PTP_PF_EXTTS
,
3318 if (pin
== -1 || pin
!= LAN8814_PTP_EXTTS_NUM
)
3321 mutex_lock(&shared
->shared_lock
);
3323 lan8814_ptp_extts_on(phydev
, pin
, rq
->extts
.flags
);
3325 lan8814_ptp_extts_off(phydev
, pin
);
3327 mutex_unlock(&shared
->shared_lock
);
3332 static int lan8814_ptpci_enable(struct ptp_clock_info
*ptpci
,
3333 struct ptp_clock_request
*rq
, int on
)
3336 case PTP_CLK_REQ_PEROUT
:
3337 return lan8814_ptp_perout(ptpci
, rq
, on
);
3338 case PTP_CLK_REQ_EXTTS
:
3339 return lan8814_ptp_extts(ptpci
, rq
, on
);
3345 static int lan8814_ptpci_verify(struct ptp_clock_info
*ptp
, unsigned int pin
,
3346 enum ptp_pin_function func
, unsigned int chan
)
3351 /* Only pins 0 and 1 can generate perout signals. And for pin 0
3352 * there is only chan 0 (event A) and for pin 1 there is only
3355 if (pin
>= LAN8814_PTP_PEROUT_NUM
|| pin
!= chan
)
3359 if (pin
!= LAN8814_PTP_EXTTS_NUM
)
3369 static bool lan8814_get_sig_tx(struct sk_buff
*skb
, u16
*sig
)
3371 struct ptp_header
*ptp_header
;
3374 type
= ptp_classify_raw(skb
);
3375 ptp_header
= ptp_parse_header(skb
, type
);
3380 *sig
= (__force u16
)(ntohs(ptp_header
->sequence_id
));
3384 static void lan8814_match_tx_skb(struct kszphy_ptp_priv
*ptp_priv
,
3385 u32 seconds
, u32 nsec
, u16 seq_id
)
3387 struct skb_shared_hwtstamps shhwtstamps
;
3388 struct sk_buff
*skb
, *skb_tmp
;
3389 unsigned long flags
;
3393 spin_lock_irqsave(&ptp_priv
->tx_queue
.lock
, flags
);
3394 skb_queue_walk_safe(&ptp_priv
->tx_queue
, skb
, skb_tmp
) {
3395 if (!lan8814_get_sig_tx(skb
, &skb_sig
))
3398 if (memcmp(&skb_sig
, &seq_id
, sizeof(seq_id
)))
3401 __skb_unlink(skb
, &ptp_priv
->tx_queue
);
3405 spin_unlock_irqrestore(&ptp_priv
->tx_queue
.lock
, flags
);
3408 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
3409 shhwtstamps
.hwtstamp
= ktime_set(seconds
, nsec
);
3410 skb_complete_tx_timestamp(skb
, &shhwtstamps
);
3414 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv
*ptp_priv
)
3416 struct phy_device
*phydev
= ptp_priv
->phydev
;
3420 lan8814_ptp_tx_ts_get(phydev
, &seconds
, &nsec
, &seq_id
);
3421 lan8814_match_tx_skb(ptp_priv
, seconds
, nsec
, seq_id
);
3424 static void lan8814_get_tx_ts(struct kszphy_ptp_priv
*ptp_priv
)
3426 struct phy_device
*phydev
= ptp_priv
->phydev
;
3430 lan8814_dequeue_tx_skb(ptp_priv
);
3432 /* If other timestamps are available in the FIFO,
3435 reg
= lanphy_read_page_reg(phydev
, 5, PTP_CAP_INFO
);
3436 } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg
) > 0);
3439 static bool lan8814_match_skb(struct kszphy_ptp_priv
*ptp_priv
,
3440 struct lan8814_ptp_rx_ts
*rx_ts
)
3442 struct skb_shared_hwtstamps
*shhwtstamps
;
3443 struct sk_buff
*skb
, *skb_tmp
;
3444 unsigned long flags
;
3448 spin_lock_irqsave(&ptp_priv
->rx_queue
.lock
, flags
);
3449 skb_queue_walk_safe(&ptp_priv
->rx_queue
, skb
, skb_tmp
) {
3450 if (!lan8814_get_sig_rx(skb
, &skb_sig
))
3453 if (memcmp(&skb_sig
, &rx_ts
->seq_id
, sizeof(rx_ts
->seq_id
)))
3456 __skb_unlink(skb
, &ptp_priv
->rx_queue
);
3461 spin_unlock_irqrestore(&ptp_priv
->rx_queue
.lock
, flags
);
3464 shhwtstamps
= skb_hwtstamps(skb
);
3465 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
3466 shhwtstamps
->hwtstamp
= ktime_set(rx_ts
->seconds
, rx_ts
->nsec
);
3473 static void lan8814_match_rx_ts(struct kszphy_ptp_priv
*ptp_priv
,
3474 struct lan8814_ptp_rx_ts
*rx_ts
)
3476 unsigned long flags
;
3478 /* If we failed to match the skb add it to the queue for when
3479 * the frame will come
3481 if (!lan8814_match_skb(ptp_priv
, rx_ts
)) {
3482 spin_lock_irqsave(&ptp_priv
->rx_ts_lock
, flags
);
3483 list_add(&rx_ts
->list
, &ptp_priv
->rx_ts_list
);
3484 spin_unlock_irqrestore(&ptp_priv
->rx_ts_lock
, flags
);
3490 static void lan8814_get_rx_ts(struct kszphy_ptp_priv
*ptp_priv
)
3492 struct phy_device
*phydev
= ptp_priv
->phydev
;
3493 struct lan8814_ptp_rx_ts
*rx_ts
;
3497 rx_ts
= kzalloc(sizeof(*rx_ts
), GFP_KERNEL
);
3501 lan8814_ptp_rx_ts_get(phydev
, &rx_ts
->seconds
, &rx_ts
->nsec
,
3503 lan8814_match_rx_ts(ptp_priv
, rx_ts
);
3505 /* If other timestamps are available in the FIFO,
3508 reg
= lanphy_read_page_reg(phydev
, 5, PTP_CAP_INFO
);
3509 } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg
) > 0);
3512 static void lan8814_handle_ptp_interrupt(struct phy_device
*phydev
, u16 status
)
3514 struct kszphy_priv
*priv
= phydev
->priv
;
3515 struct kszphy_ptp_priv
*ptp_priv
= &priv
->ptp_priv
;
3517 if (status
& PTP_TSU_INT_STS_PTP_TX_TS_EN_
)
3518 lan8814_get_tx_ts(ptp_priv
);
3520 if (status
& PTP_TSU_INT_STS_PTP_RX_TS_EN_
)
3521 lan8814_get_rx_ts(ptp_priv
);
3523 if (status
& PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_
) {
3524 lan8814_flush_fifo(phydev
, true);
3525 skb_queue_purge(&ptp_priv
->tx_queue
);
3528 if (status
& PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_
) {
3529 lan8814_flush_fifo(phydev
, false);
3530 skb_queue_purge(&ptp_priv
->rx_queue
);
3534 static int lan8814_gpio_process_cap(struct lan8814_shared_priv
*shared
)
3536 struct phy_device
*phydev
= shared
->phydev
;
3537 struct ptp_clock_event ptp_event
= {0};
3542 /* This is 0 because whatever was the input pin it was mapped it to
3545 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_SEL
);
3546 tmp
|= PTP_GPIO_SEL_GPIO_SEL(0);
3547 lanphy_write_page_reg(phydev
, 4, PTP_GPIO_SEL
, tmp
);
3549 tmp
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_CAP_STS
);
3550 if (!(tmp
& PTP_GPIO_CAP_STS_PTP_GPIO_RE_STS(0)) &&
3551 !(tmp
& PTP_GPIO_CAP_STS_PTP_GPIO_FE_STS(0)))
3555 sec
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_RE_LTC_SEC_HI_CAP
);
3557 sec
|= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_RE_LTC_SEC_LO_CAP
);
3559 nsec
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_RE_LTC_NS_HI_CAP
) & 0x3fff;
3561 nsec
|= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_RE_LTC_NS_LO_CAP
);
3563 sec
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_FE_LTC_SEC_HI_CAP
);
3565 sec
|= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_FE_LTC_SEC_LO_CAP
);
3567 nsec
= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_FE_LTC_NS_HI_CAP
) & 0x3fff;
3569 nsec
|= lanphy_read_page_reg(phydev
, 4, PTP_GPIO_RE_LTC_NS_LO_CAP
);
3572 ptp_event
.index
= 0;
3573 ptp_event
.timestamp
= ktime_set(sec
, nsec
);
3574 ptp_event
.type
= PTP_CLOCK_EXTTS
;
3575 ptp_clock_event(shared
->ptp_clock
, &ptp_event
);
3580 static int lan8814_handle_gpio_interrupt(struct phy_device
*phydev
, u16 status
)
3582 struct lan8814_shared_priv
*shared
= phydev
->shared
->priv
;
3585 mutex_lock(&shared
->shared_lock
);
3586 ret
= lan8814_gpio_process_cap(shared
);
3587 mutex_unlock(&shared
->shared_lock
);
3592 static int lan8804_config_init(struct phy_device
*phydev
)
3596 /* MDI-X setting for swap A,B transmit */
3597 val
= lanphy_read_page_reg(phydev
, 2, LAN8804_ALIGN_SWAP
);
3598 val
&= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK
;
3599 val
|= LAN8804_ALIGN_TX_A_B_SWAP
;
3600 lanphy_write_page_reg(phydev
, 2, LAN8804_ALIGN_SWAP
, val
);
3602 /* Make sure that the PHY will not stop generating the clock when the
3603 * link partner goes down
3605 lanphy_write_page_reg(phydev
, 31, LAN8814_CLOCK_MANAGEMENT
, 0x27e);
3606 lanphy_read_page_reg(phydev
, 1, LAN8814_LINK_QUALITY
);
3611 static irqreturn_t
lan8804_handle_interrupt(struct phy_device
*phydev
)
3615 status
= phy_read(phydev
, LAN8814_INTS
);
3622 phy_trigger_machine(phydev
);
3627 #define LAN8804_OUTPUT_CONTROL 25
3628 #define LAN8804_OUTPUT_CONTROL_INTR_BUFFER BIT(14)
3629 #define LAN8804_CONTROL 31
3630 #define LAN8804_CONTROL_INTR_POLARITY BIT(14)
3632 static int lan8804_config_intr(struct phy_device
*phydev
)
3636 /* This is an internal PHY of lan966x and is not possible to change the
3637 * polarity on the GIC found in lan966x, therefore change the polarity
3638 * of the interrupt in the PHY from being active low instead of active
3641 phy_write(phydev
, LAN8804_CONTROL
, LAN8804_CONTROL_INTR_POLARITY
);
3643 /* By default interrupt buffer is open-drain in which case the interrupt
3644 * can be active only low. Therefore change the interrupt buffer to be
3645 * push-pull to be able to change interrupt polarity
3647 phy_write(phydev
, LAN8804_OUTPUT_CONTROL
,
3648 LAN8804_OUTPUT_CONTROL_INTR_BUFFER
);
3650 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
3651 err
= phy_read(phydev
, LAN8814_INTS
);
3655 err
= phy_write(phydev
, LAN8814_INTC
, LAN8814_INT_LINK
);
3659 err
= phy_write(phydev
, LAN8814_INTC
, 0);
3663 err
= phy_read(phydev
, LAN8814_INTS
);
3671 static irqreturn_t
lan8814_handle_interrupt(struct phy_device
*phydev
)
3676 irq_status
= phy_read(phydev
, LAN8814_INTS
);
3677 if (irq_status
< 0) {
3682 if (irq_status
& LAN8814_INT_LINK
) {
3683 phy_trigger_machine(phydev
);
3688 irq_status
= lanphy_read_page_reg(phydev
, 5, PTP_TSU_INT_STS
);
3692 lan8814_handle_ptp_interrupt(phydev
, irq_status
);
3696 if (!lan8814_handle_gpio_interrupt(phydev
, irq_status
))
3702 static int lan8814_ack_interrupt(struct phy_device
*phydev
)
3704 /* bit[12..0] int status, which is a read and clear register. */
3707 rc
= phy_read(phydev
, LAN8814_INTS
);
3709 return (rc
< 0) ? rc
: 0;
3712 static int lan8814_config_intr(struct phy_device
*phydev
)
3716 lanphy_write_page_reg(phydev
, 4, LAN8814_INTR_CTRL_REG
,
3717 LAN8814_INTR_CTRL_REG_POLARITY
|
3718 LAN8814_INTR_CTRL_REG_INTR_ENABLE
);
3720 /* enable / disable interrupts */
3721 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
3722 err
= lan8814_ack_interrupt(phydev
);
3726 err
= phy_write(phydev
, LAN8814_INTC
, LAN8814_INT_LINK
);
3728 err
= phy_write(phydev
, LAN8814_INTC
, 0);
3732 err
= lan8814_ack_interrupt(phydev
);
3738 static void lan8814_ptp_init(struct phy_device
*phydev
)
3740 struct kszphy_priv
*priv
= phydev
->priv
;
3741 struct kszphy_ptp_priv
*ptp_priv
= &priv
->ptp_priv
;
3744 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK
) ||
3745 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING
))
3748 lanphy_write_page_reg(phydev
, 5, TSU_HARD_RESET
, TSU_HARD_RESET_
);
3750 temp
= lanphy_read_page_reg(phydev
, 5, PTP_TX_MOD
);
3751 temp
|= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_
;
3752 lanphy_write_page_reg(phydev
, 5, PTP_TX_MOD
, temp
);
3754 temp
= lanphy_read_page_reg(phydev
, 5, PTP_RX_MOD
);
3755 temp
|= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_
;
3756 lanphy_write_page_reg(phydev
, 5, PTP_RX_MOD
, temp
);
3758 lanphy_write_page_reg(phydev
, 5, PTP_RX_PARSE_CONFIG
, 0);
3759 lanphy_write_page_reg(phydev
, 5, PTP_TX_PARSE_CONFIG
, 0);
3761 /* Removing default registers configs related to L2 and IP */
3762 lanphy_write_page_reg(phydev
, 5, PTP_TX_PARSE_L2_ADDR_EN
, 0);
3763 lanphy_write_page_reg(phydev
, 5, PTP_RX_PARSE_L2_ADDR_EN
, 0);
3764 lanphy_write_page_reg(phydev
, 5, PTP_TX_PARSE_IP_ADDR_EN
, 0);
3765 lanphy_write_page_reg(phydev
, 5, PTP_RX_PARSE_IP_ADDR_EN
, 0);
3767 /* Disable checking for minorVersionPTP field */
3768 lanphy_write_page_reg(phydev
, 5, PTP_RX_VERSION
,
3769 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3770 lanphy_write_page_reg(phydev
, 5, PTP_TX_VERSION
,
3771 PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
3773 skb_queue_head_init(&ptp_priv
->tx_queue
);
3774 skb_queue_head_init(&ptp_priv
->rx_queue
);
3775 INIT_LIST_HEAD(&ptp_priv
->rx_ts_list
);
3776 spin_lock_init(&ptp_priv
->rx_ts_lock
);
3778 ptp_priv
->phydev
= phydev
;
3780 ptp_priv
->mii_ts
.rxtstamp
= lan8814_rxtstamp
;
3781 ptp_priv
->mii_ts
.txtstamp
= lan8814_txtstamp
;
3782 ptp_priv
->mii_ts
.hwtstamp
= lan8814_hwtstamp
;
3783 ptp_priv
->mii_ts
.ts_info
= lan8814_ts_info
;
3785 phydev
->mii_ts
= &ptp_priv
->mii_ts
;
3787 /* Timestamp selected by default to keep legacy API */
3788 phydev
->default_timestamp
= true;
3791 static int lan8814_ptp_probe_once(struct phy_device
*phydev
)
3793 struct lan8814_shared_priv
*shared
= phydev
->shared
->priv
;
3795 /* Initialise shared lock for clock*/
3796 mutex_init(&shared
->shared_lock
);
3798 shared
->pin_config
= devm_kmalloc_array(&phydev
->mdio
.dev
,
3799 LAN8814_PTP_GPIO_NUM
,
3800 sizeof(*shared
->pin_config
),
3802 if (!shared
->pin_config
)
3805 for (int i
= 0; i
< LAN8814_PTP_GPIO_NUM
; i
++) {
3806 struct ptp_pin_desc
*ptp_pin
= &shared
->pin_config
[i
];
3808 memset(ptp_pin
, 0, sizeof(*ptp_pin
));
3809 snprintf(ptp_pin
->name
,
3810 sizeof(ptp_pin
->name
), "lan8814_ptp_pin_%02d", i
);
3812 ptp_pin
->func
= PTP_PF_NONE
;
3815 shared
->ptp_clock_info
.owner
= THIS_MODULE
;
3816 snprintf(shared
->ptp_clock_info
.name
, 30, "%s", phydev
->drv
->name
);
3817 shared
->ptp_clock_info
.max_adj
= 31249999;
3818 shared
->ptp_clock_info
.n_alarm
= 0;
3819 shared
->ptp_clock_info
.n_ext_ts
= LAN8814_PTP_EXTTS_NUM
;
3820 shared
->ptp_clock_info
.n_pins
= LAN8814_PTP_GPIO_NUM
;
3821 shared
->ptp_clock_info
.pps
= 0;
3822 shared
->ptp_clock_info
.pin_config
= shared
->pin_config
;
3823 shared
->ptp_clock_info
.n_per_out
= LAN8814_PTP_PEROUT_NUM
;
3824 shared
->ptp_clock_info
.adjfine
= lan8814_ptpci_adjfine
;
3825 shared
->ptp_clock_info
.adjtime
= lan8814_ptpci_adjtime
;
3826 shared
->ptp_clock_info
.gettime64
= lan8814_ptpci_gettime64
;
3827 shared
->ptp_clock_info
.settime64
= lan8814_ptpci_settime64
;
3828 shared
->ptp_clock_info
.getcrosststamp
= NULL
;
3829 shared
->ptp_clock_info
.enable
= lan8814_ptpci_enable
;
3830 shared
->ptp_clock_info
.verify
= lan8814_ptpci_verify
;
3832 shared
->ptp_clock
= ptp_clock_register(&shared
->ptp_clock_info
,
3834 if (IS_ERR(shared
->ptp_clock
)) {
3835 phydev_err(phydev
, "ptp_clock_register failed %lu\n",
3836 PTR_ERR(shared
->ptp_clock
));
3840 /* Check if PHC support is missing at the configuration level */
3841 if (!shared
->ptp_clock
)
3844 phydev_dbg(phydev
, "successfully registered ptp clock\n");
3846 shared
->phydev
= phydev
;
3848 /* The EP.4 is shared between all the PHYs in the package and also it
3849 * can be accessed by any of the PHYs
3851 lanphy_write_page_reg(phydev
, 4, LTC_HARD_RESET
, LTC_HARD_RESET_
);
3852 lanphy_write_page_reg(phydev
, 4, PTP_OPERATING_MODE
,
3853 PTP_OPERATING_MODE_STANDALONE_
);
3855 /* Enable ptp to run LTC clock for ptp and gpio 1PPS operation */
3856 lanphy_write_page_reg(phydev
, 4, PTP_CMD_CTL
, PTP_CMD_CTL_PTP_ENABLE_
);
3861 static void lan8814_setup_led(struct phy_device
*phydev
, int val
)
3865 temp
= lanphy_read_page_reg(phydev
, 5, LAN8814_LED_CTRL_1
);
3868 temp
|= LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_
;
3870 temp
&= ~LAN8814_LED_CTRL_1_KSZ9031_LED_MODE_
;
3872 lanphy_write_page_reg(phydev
, 5, LAN8814_LED_CTRL_1
, temp
);
3875 static int lan8814_config_init(struct phy_device
*phydev
)
3877 struct kszphy_priv
*lan8814
= phydev
->priv
;
3881 val
= lanphy_read_page_reg(phydev
, 4, LAN8814_QSGMII_SOFT_RESET
);
3882 val
|= LAN8814_QSGMII_SOFT_RESET_BIT
;
3883 lanphy_write_page_reg(phydev
, 4, LAN8814_QSGMII_SOFT_RESET
, val
);
3885 /* Disable ANEG with QSGMII PCS Host side */
3886 val
= lanphy_read_page_reg(phydev
, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG
);
3887 val
&= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA
;
3888 lanphy_write_page_reg(phydev
, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG
, val
);
3890 /* MDI-X setting for swap A,B transmit */
3891 val
= lanphy_read_page_reg(phydev
, 2, LAN8814_ALIGN_SWAP
);
3892 val
&= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK
;
3893 val
|= LAN8814_ALIGN_TX_A_B_SWAP
;
3894 lanphy_write_page_reg(phydev
, 2, LAN8814_ALIGN_SWAP
, val
);
3896 if (lan8814
->led_mode
>= 0)
3897 lan8814_setup_led(phydev
, lan8814
->led_mode
);
3902 /* It is expected that there will not be any 'lan8814_take_coma_mode'
3903 * function called in suspend. Because the GPIO line can be shared, so if one of
3904 * the phys goes back in coma mode, then all the other PHYs will go, which is
3907 static int lan8814_release_coma_mode(struct phy_device
*phydev
)
3909 struct gpio_desc
*gpiod
;
3911 gpiod
= devm_gpiod_get_optional(&phydev
->mdio
.dev
, "coma-mode",
3912 GPIOD_OUT_HIGH_OPEN_DRAIN
|
3913 GPIOD_FLAGS_BIT_NONEXCLUSIVE
);
3915 return PTR_ERR(gpiod
);
3917 gpiod_set_consumer_name(gpiod
, "LAN8814 coma mode");
3918 gpiod_set_value_cansleep(gpiod
, 0);
3923 static void lan8814_clear_2psp_bit(struct phy_device
*phydev
)
3927 /* It was noticed that when traffic is passing through the PHY and the
3928 * cable is removed then the LED was still one even though there is no
3931 val
= lanphy_read_page_reg(phydev
, 2, LAN8814_EEE_STATE
);
3932 val
&= ~LAN8814_EEE_STATE_MASK2P5P
;
3933 lanphy_write_page_reg(phydev
, 2, LAN8814_EEE_STATE
, val
);
3936 static void lan8814_update_meas_time(struct phy_device
*phydev
)
3940 /* By setting the measure time to a value of 0xb this will allow cables
3941 * longer than 100m to be used. This configuration can be used
3942 * regardless of the mode of operation of the PHY
3944 val
= lanphy_read_page_reg(phydev
, 1, LAN8814_PD_CONTROLS
);
3945 val
&= ~LAN8814_PD_CONTROLS_PD_MEAS_TIME_MASK
;
3946 val
|= LAN8814_PD_CONTROLS_PD_MEAS_TIME_VAL
;
3947 lanphy_write_page_reg(phydev
, 1, LAN8814_PD_CONTROLS
, val
);
3950 static int lan8814_probe(struct phy_device
*phydev
)
3952 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
3953 struct kszphy_priv
*priv
;
3957 priv
= devm_kzalloc(&phydev
->mdio
.dev
, sizeof(*priv
), GFP_KERNEL
);
3961 phydev
->priv
= priv
;
3965 kszphy_parse_led_mode(phydev
);
3967 /* Strap-in value for PHY address, below register read gives starting
3970 addr
= lanphy_read_page_reg(phydev
, 4, 0) & 0x1F;
3971 devm_phy_package_join(&phydev
->mdio
.dev
, phydev
,
3972 addr
, sizeof(struct lan8814_shared_priv
));
3974 if (phy_package_init_once(phydev
)) {
3975 err
= lan8814_release_coma_mode(phydev
);
3979 err
= lan8814_ptp_probe_once(phydev
);
3984 lan8814_ptp_init(phydev
);
3986 /* Errata workarounds */
3987 lan8814_clear_2psp_bit(phydev
);
3988 lan8814_update_meas_time(phydev
);
3993 #define LAN8841_MMD_TIMER_REG 0
3994 #define LAN8841_MMD0_REGISTER_17 17
3995 #define LAN8841_MMD0_REGISTER_17_DROP_OPT(x) ((x) & 0x3)
3996 #define LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS BIT(3)
3997 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG 2
3998 #define LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK BIT(14)
3999 #define LAN8841_MMD_ANALOG_REG 28
4000 #define LAN8841_ANALOG_CONTROL_1 1
4001 #define LAN8841_ANALOG_CONTROL_1_PLL_TRIM(x) (((x) & 0x3) << 5)
4002 #define LAN8841_ANALOG_CONTROL_10 13
4003 #define LAN8841_ANALOG_CONTROL_10_PLL_DIV(x) ((x) & 0x3)
4004 #define LAN8841_ANALOG_CONTROL_11 14
4005 #define LAN8841_ANALOG_CONTROL_11_LDO_REF(x) (((x) & 0x7) << 12)
4006 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT 69
4007 #define LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL 0xbffc
4008 #define LAN8841_BTRX_POWER_DOWN 70
4009 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A BIT(0)
4010 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_A BIT(1)
4011 #define LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B BIT(2)
4012 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_B BIT(3)
4013 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C BIT(5)
4014 #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D BIT(7)
4015 #define LAN8841_ADC_CHANNEL_MASK 198
4016 #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN 370
4017 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN 371
4018 #define LAN8841_PTP_RX_VERSION 374
4019 #define LAN8841_PTP_TX_PARSE_L2_ADDR_EN 434
4020 #define LAN8841_PTP_TX_PARSE_IP_ADDR_EN 435
4021 #define LAN8841_PTP_TX_VERSION 438
4022 #define LAN8841_PTP_CMD_CTL 256
4023 #define LAN8841_PTP_CMD_CTL_PTP_ENABLE BIT(2)
4024 #define LAN8841_PTP_CMD_CTL_PTP_DISABLE BIT(1)
4025 #define LAN8841_PTP_CMD_CTL_PTP_RESET BIT(0)
4026 #define LAN8841_PTP_RX_PARSE_CONFIG 368
4027 #define LAN8841_PTP_TX_PARSE_CONFIG 432
4028 #define LAN8841_PTP_RX_MODE 381
4029 #define LAN8841_PTP_INSERT_TS_EN BIT(0)
4030 #define LAN8841_PTP_INSERT_TS_32BIT BIT(1)
4032 static int lan8841_config_init(struct phy_device
*phydev
)
4036 ret
= ksz9131_config_init(phydev
);
4040 /* Initialize the HW by resetting everything */
4041 phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4042 LAN8841_PTP_CMD_CTL
,
4043 LAN8841_PTP_CMD_CTL_PTP_RESET
,
4044 LAN8841_PTP_CMD_CTL_PTP_RESET
);
4046 phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4047 LAN8841_PTP_CMD_CTL
,
4048 LAN8841_PTP_CMD_CTL_PTP_ENABLE
,
4049 LAN8841_PTP_CMD_CTL_PTP_ENABLE
);
4051 /* Don't process any frames */
4052 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4053 LAN8841_PTP_RX_PARSE_CONFIG
, 0);
4054 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4055 LAN8841_PTP_TX_PARSE_CONFIG
, 0);
4056 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4057 LAN8841_PTP_TX_PARSE_L2_ADDR_EN
, 0);
4058 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4059 LAN8841_PTP_RX_PARSE_L2_ADDR_EN
, 0);
4060 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4061 LAN8841_PTP_TX_PARSE_IP_ADDR_EN
, 0);
4062 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4063 LAN8841_PTP_RX_PARSE_IP_ADDR_EN
, 0);
4065 /* Disable checking for minorVersionPTP field */
4066 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4067 LAN8841_PTP_RX_VERSION
, 0xff00);
4068 phy_write_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4069 LAN8841_PTP_TX_VERSION
, 0xff00);
4071 /* 100BT Clause 40 improvenent errata */
4072 phy_write_mmd(phydev
, LAN8841_MMD_ANALOG_REG
,
4073 LAN8841_ANALOG_CONTROL_1
,
4074 LAN8841_ANALOG_CONTROL_1_PLL_TRIM(0x2));
4075 phy_write_mmd(phydev
, LAN8841_MMD_ANALOG_REG
,
4076 LAN8841_ANALOG_CONTROL_10
,
4077 LAN8841_ANALOG_CONTROL_10_PLL_DIV(0x1));
4079 /* 10M/100M Ethernet Signal Tuning Errata for Shorted-Center Tap
4082 ret
= phy_read_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4083 LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG
);
4084 if (ret
& LAN8841_OPERATION_MODE_STRAP_OVERRIDE_LOW_REG_MAGJACK
) {
4085 phy_write_mmd(phydev
, LAN8841_MMD_ANALOG_REG
,
4086 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT
,
4087 LAN8841_TX_LOW_I_CH_C_D_POWER_MANAGMENT_VAL
);
4088 phy_write_mmd(phydev
, LAN8841_MMD_ANALOG_REG
,
4089 LAN8841_BTRX_POWER_DOWN
,
4090 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_A
|
4091 LAN8841_BTRX_POWER_DOWN_BTRX_CH_A
|
4092 LAN8841_BTRX_POWER_DOWN_QBIAS_CH_B
|
4093 LAN8841_BTRX_POWER_DOWN_BTRX_CH_B
|
4094 LAN8841_BTRX_POWER_DOWN_BTRX_CH_C
|
4095 LAN8841_BTRX_POWER_DOWN_BTRX_CH_D
);
4098 /* LDO Adjustment errata */
4099 phy_write_mmd(phydev
, LAN8841_MMD_ANALOG_REG
,
4100 LAN8841_ANALOG_CONTROL_11
,
4101 LAN8841_ANALOG_CONTROL_11_LDO_REF(1));
4103 /* 100BT RGMII latency tuning errata */
4104 phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
,
4105 LAN8841_ADC_CHANNEL_MASK
, 0x0);
4106 phy_write_mmd(phydev
, LAN8841_MMD_TIMER_REG
,
4107 LAN8841_MMD0_REGISTER_17
,
4108 LAN8841_MMD0_REGISTER_17_DROP_OPT(2) |
4109 LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS
);
4114 #define LAN8841_OUTPUT_CTRL 25
4115 #define LAN8841_OUTPUT_CTRL_INT_BUFFER BIT(14)
4116 #define LAN8841_INT_PTP BIT(9)
4118 static int lan8841_config_intr(struct phy_device
*phydev
)
4122 phy_modify(phydev
, LAN8841_OUTPUT_CTRL
,
4123 LAN8841_OUTPUT_CTRL_INT_BUFFER
, 0);
4125 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
4126 err
= phy_read(phydev
, LAN8814_INTS
);
4130 /* Enable / disable interrupts. It is OK to enable PTP interrupt
4131 * even if it PTP is not enabled. Because the underneath blocks
4132 * will not enable the PTP so we will never get the PTP
4135 err
= phy_write(phydev
, LAN8814_INTC
,
4136 LAN8814_INT_LINK
| LAN8841_INT_PTP
);
4138 err
= phy_write(phydev
, LAN8814_INTC
, 0);
4142 err
= phy_read(phydev
, LAN8814_INTS
);
4146 /* Getting a positive value doesn't mean that is an error, it
4147 * just indicates what was the status. Therefore make sure to
4148 * clear the value and say that there is no error.
4156 #define LAN8841_PTP_TX_EGRESS_SEC_LO 453
4157 #define LAN8841_PTP_TX_EGRESS_SEC_HI 452
4158 #define LAN8841_PTP_TX_EGRESS_NS_LO 451
4159 #define LAN8841_PTP_TX_EGRESS_NS_HI 450
4160 #define LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID BIT(15)
4161 #define LAN8841_PTP_TX_MSG_HEADER2 455
4163 static bool lan8841_ptp_get_tx_ts(struct kszphy_ptp_priv
*ptp_priv
,
4164 u32
*sec
, u32
*nsec
, u16
*seq
)
4166 struct phy_device
*phydev
= ptp_priv
->phydev
;
4168 *nsec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_EGRESS_NS_HI
);
4169 if (!(*nsec
& LAN8841_PTP_TX_EGRESS_NSEC_HI_VALID
))
4172 *nsec
= ((*nsec
& 0x3fff) << 16);
4173 *nsec
= *nsec
| phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_EGRESS_NS_LO
);
4175 *sec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_EGRESS_SEC_HI
);
4177 *sec
= *sec
| phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_EGRESS_SEC_LO
);
4179 *seq
= phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_MSG_HEADER2
);
4184 static void lan8841_ptp_process_tx_ts(struct kszphy_ptp_priv
*ptp_priv
)
4189 while (lan8841_ptp_get_tx_ts(ptp_priv
, &sec
, &nsec
, &seq
))
4190 lan8814_match_tx_skb(ptp_priv
, sec
, nsec
, seq
);
4193 #define LAN8841_PTP_INT_STS 259
4194 #define LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT BIT(13)
4195 #define LAN8841_PTP_INT_STS_PTP_TX_TS_INT BIT(12)
4196 #define LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT BIT(2)
4198 static void lan8841_ptp_flush_fifo(struct kszphy_ptp_priv
*ptp_priv
)
4200 struct phy_device
*phydev
= ptp_priv
->phydev
;
4203 for (i
= 0; i
< FIFO_SIZE
; ++i
)
4204 phy_read_mmd(phydev
, 2, LAN8841_PTP_TX_MSG_HEADER2
);
4206 phy_read_mmd(phydev
, 2, LAN8841_PTP_INT_STS
);
4209 #define LAN8841_PTP_GPIO_CAP_STS 506
4210 #define LAN8841_PTP_GPIO_SEL 327
4211 #define LAN8841_PTP_GPIO_SEL_GPIO_SEL(gpio) ((gpio) << 8)
4212 #define LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP 498
4213 #define LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP 499
4214 #define LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP 500
4215 #define LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP 501
4216 #define LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP 502
4217 #define LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP 503
4218 #define LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP 504
4219 #define LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP 505
4221 static void lan8841_gpio_process_cap(struct kszphy_ptp_priv
*ptp_priv
)
4223 struct phy_device
*phydev
= ptp_priv
->phydev
;
4224 struct ptp_clock_event ptp_event
= {0};
4228 pin
= ptp_find_pin_unlocked(ptp_priv
->ptp_clock
, PTP_PF_EXTTS
, 0);
4232 tmp
= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_CAP_STS
);
4236 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_GPIO_SEL
,
4237 LAN8841_PTP_GPIO_SEL_GPIO_SEL(pin
));
4241 mutex_lock(&ptp_priv
->ptp_lock
);
4242 if (tmp
& BIT(pin
)) {
4243 sec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_HI_CAP
);
4245 sec
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_RE_LTC_SEC_LO_CAP
);
4247 nsec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_RE_LTC_NS_HI_CAP
) & 0x3fff;
4249 nsec
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_RE_LTC_NS_LO_CAP
);
4251 sec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_HI_CAP
);
4253 sec
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_FE_LTC_SEC_LO_CAP
);
4255 nsec
= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_FE_LTC_NS_HI_CAP
) & 0x3fff;
4257 nsec
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_GPIO_FE_LTC_NS_LO_CAP
);
4259 mutex_unlock(&ptp_priv
->ptp_lock
);
4260 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_GPIO_SEL
, 0);
4264 ptp_event
.index
= 0;
4265 ptp_event
.timestamp
= ktime_set(sec
, nsec
);
4266 ptp_event
.type
= PTP_CLOCK_EXTTS
;
4267 ptp_clock_event(ptp_priv
->ptp_clock
, &ptp_event
);
4270 static void lan8841_handle_ptp_interrupt(struct phy_device
*phydev
)
4272 struct kszphy_priv
*priv
= phydev
->priv
;
4273 struct kszphy_ptp_priv
*ptp_priv
= &priv
->ptp_priv
;
4277 status
= phy_read_mmd(phydev
, 2, LAN8841_PTP_INT_STS
);
4279 if (status
& LAN8841_PTP_INT_STS_PTP_TX_TS_INT
)
4280 lan8841_ptp_process_tx_ts(ptp_priv
);
4282 if (status
& LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT
)
4283 lan8841_gpio_process_cap(ptp_priv
);
4285 if (status
& LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT
) {
4286 lan8841_ptp_flush_fifo(ptp_priv
);
4287 skb_queue_purge(&ptp_priv
->tx_queue
);
4290 } while (status
& (LAN8841_PTP_INT_STS_PTP_TX_TS_INT
|
4291 LAN8841_PTP_INT_STS_PTP_GPIO_CAP_INT
|
4292 LAN8841_PTP_INT_STS_PTP_TX_TS_OVRFL_INT
));
4295 #define LAN8841_INTS_PTP BIT(9)
4297 static irqreturn_t
lan8841_handle_interrupt(struct phy_device
*phydev
)
4299 irqreturn_t ret
= IRQ_NONE
;
4302 irq_status
= phy_read(phydev
, LAN8814_INTS
);
4303 if (irq_status
< 0) {
4308 if (irq_status
& LAN8814_INT_LINK
) {
4309 phy_trigger_machine(phydev
);
4313 if (irq_status
& LAN8841_INTS_PTP
) {
4314 lan8841_handle_ptp_interrupt(phydev
);
4321 static int lan8841_ts_info(struct mii_timestamper
*mii_ts
,
4322 struct kernel_ethtool_ts_info
*info
)
4324 struct kszphy_ptp_priv
*ptp_priv
;
4326 ptp_priv
= container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
4328 info
->phc_index
= ptp_priv
->ptp_clock
?
4329 ptp_clock_index(ptp_priv
->ptp_clock
) : -1;
4330 if (info
->phc_index
== -1)
4333 info
->so_timestamping
= SOF_TIMESTAMPING_TX_HARDWARE
|
4334 SOF_TIMESTAMPING_RX_HARDWARE
|
4335 SOF_TIMESTAMPING_RAW_HARDWARE
;
4337 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
4338 (1 << HWTSTAMP_TX_ON
) |
4339 (1 << HWTSTAMP_TX_ONESTEP_SYNC
);
4341 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
4342 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT
) |
4343 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
) |
4344 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT
);
4349 #define LAN8841_PTP_INT_EN 260
4350 #define LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN BIT(13)
4351 #define LAN8841_PTP_INT_EN_PTP_TX_TS_EN BIT(12)
4353 static void lan8841_ptp_enable_processing(struct kszphy_ptp_priv
*ptp_priv
,
4356 struct phy_device
*phydev
= ptp_priv
->phydev
;
4359 /* Enable interrupts on the TX side */
4360 phy_modify_mmd(phydev
, 2, LAN8841_PTP_INT_EN
,
4361 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN
|
4362 LAN8841_PTP_INT_EN_PTP_TX_TS_EN
,
4363 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN
|
4364 LAN8841_PTP_INT_EN_PTP_TX_TS_EN
);
4366 /* Enable the modification of the frame on RX side,
4367 * this will add the ns and 2 bits of sec in the reserved field
4370 phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4371 LAN8841_PTP_RX_MODE
,
4372 LAN8841_PTP_INSERT_TS_EN
|
4373 LAN8841_PTP_INSERT_TS_32BIT
,
4374 LAN8841_PTP_INSERT_TS_EN
|
4375 LAN8841_PTP_INSERT_TS_32BIT
);
4377 ptp_schedule_worker(ptp_priv
->ptp_clock
, 0);
4379 /* Disable interrupts on the TX side */
4380 phy_modify_mmd(phydev
, 2, LAN8841_PTP_INT_EN
,
4381 LAN8841_PTP_INT_EN_PTP_TX_TS_OVRFL_EN
|
4382 LAN8841_PTP_INT_EN_PTP_TX_TS_EN
, 0);
4384 /* Disable modification of the RX frames */
4385 phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
4386 LAN8841_PTP_RX_MODE
,
4387 LAN8841_PTP_INSERT_TS_EN
|
4388 LAN8841_PTP_INSERT_TS_32BIT
, 0);
4390 ptp_cancel_worker_sync(ptp_priv
->ptp_clock
);
4394 #define LAN8841_PTP_RX_TIMESTAMP_EN 379
4395 #define LAN8841_PTP_TX_TIMESTAMP_EN 443
4396 #define LAN8841_PTP_TX_MOD 445
4398 static int lan8841_hwtstamp(struct mii_timestamper
*mii_ts
,
4399 struct kernel_hwtstamp_config
*config
,
4400 struct netlink_ext_ack
*extack
)
4402 struct kszphy_ptp_priv
*ptp_priv
= container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
4403 struct phy_device
*phydev
= ptp_priv
->phydev
;
4404 int txcfg
= 0, rxcfg
= 0;
4407 ptp_priv
->hwts_tx_type
= config
->tx_type
;
4408 ptp_priv
->rx_filter
= config
->rx_filter
;
4410 switch (config
->rx_filter
) {
4411 case HWTSTAMP_FILTER_NONE
:
4412 ptp_priv
->layer
= 0;
4413 ptp_priv
->version
= 0;
4415 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
4416 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
4417 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
4418 ptp_priv
->layer
= PTP_CLASS_L4
;
4419 ptp_priv
->version
= PTP_CLASS_V2
;
4421 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
4422 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
4423 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
4424 ptp_priv
->layer
= PTP_CLASS_L2
;
4425 ptp_priv
->version
= PTP_CLASS_V2
;
4427 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
4428 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
4429 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
4430 ptp_priv
->layer
= PTP_CLASS_L4
| PTP_CLASS_L2
;
4431 ptp_priv
->version
= PTP_CLASS_V2
;
4437 /* Setup parsing of the frames and enable the timestamping for ptp
4440 if (ptp_priv
->layer
& PTP_CLASS_L2
) {
4441 rxcfg
|= PTP_RX_PARSE_CONFIG_LAYER2_EN_
;
4442 txcfg
|= PTP_TX_PARSE_CONFIG_LAYER2_EN_
;
4443 } else if (ptp_priv
->layer
& PTP_CLASS_L4
) {
4444 rxcfg
|= PTP_RX_PARSE_CONFIG_IPV4_EN_
| PTP_RX_PARSE_CONFIG_IPV6_EN_
;
4445 txcfg
|= PTP_TX_PARSE_CONFIG_IPV4_EN_
| PTP_TX_PARSE_CONFIG_IPV6_EN_
;
4448 phy_write_mmd(phydev
, 2, LAN8841_PTP_RX_PARSE_CONFIG
, rxcfg
);
4449 phy_write_mmd(phydev
, 2, LAN8841_PTP_TX_PARSE_CONFIG
, txcfg
);
4451 pkt_ts_enable
= PTP_TIMESTAMP_EN_SYNC_
| PTP_TIMESTAMP_EN_DREQ_
|
4452 PTP_TIMESTAMP_EN_PDREQ_
| PTP_TIMESTAMP_EN_PDRES_
;
4453 phy_write_mmd(phydev
, 2, LAN8841_PTP_RX_TIMESTAMP_EN
, pkt_ts_enable
);
4454 phy_write_mmd(phydev
, 2, LAN8841_PTP_TX_TIMESTAMP_EN
, pkt_ts_enable
);
4456 /* Enable / disable of the TX timestamp in the SYNC frames */
4457 phy_modify_mmd(phydev
, 2, LAN8841_PTP_TX_MOD
,
4458 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_
,
4459 ptp_priv
->hwts_tx_type
== HWTSTAMP_TX_ONESTEP_SYNC
?
4460 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_
: 0);
4462 /* Now enable/disable the timestamping */
4463 lan8841_ptp_enable_processing(ptp_priv
,
4464 config
->rx_filter
!= HWTSTAMP_FILTER_NONE
);
4466 skb_queue_purge(&ptp_priv
->tx_queue
);
4468 lan8841_ptp_flush_fifo(ptp_priv
);
4473 static bool lan8841_rxtstamp(struct mii_timestamper
*mii_ts
,
4474 struct sk_buff
*skb
, int type
)
4476 struct kszphy_ptp_priv
*ptp_priv
=
4477 container_of(mii_ts
, struct kszphy_ptp_priv
, mii_ts
);
4478 struct ptp_header
*header
= ptp_parse_header(skb
, type
);
4479 struct skb_shared_hwtstamps
*shhwtstamps
;
4480 struct timespec64 ts
;
4481 unsigned long flags
;
4487 if (ptp_priv
->rx_filter
== HWTSTAMP_FILTER_NONE
||
4488 type
== PTP_CLASS_NONE
)
4491 if ((type
& ptp_priv
->version
) == 0 || (type
& ptp_priv
->layer
) == 0)
4494 spin_lock_irqsave(&ptp_priv
->seconds_lock
, flags
);
4495 ts
.tv_sec
= ptp_priv
->seconds
;
4496 spin_unlock_irqrestore(&ptp_priv
->seconds_lock
, flags
);
4497 ts_header
= __be32_to_cpu(header
->reserved2
);
4499 shhwtstamps
= skb_hwtstamps(skb
);
4500 memset(shhwtstamps
, 0, sizeof(*shhwtstamps
));
4502 /* Check for any wrap arounds for the second part */
4503 if ((ts
.tv_sec
& GENMASK(1, 0)) == 0 && (ts_header
>> 30) == 3)
4504 ts
.tv_sec
-= GENMASK(1, 0) + 1;
4505 else if ((ts
.tv_sec
& GENMASK(1, 0)) == 3 && (ts_header
>> 30) == 0)
4508 shhwtstamps
->hwtstamp
=
4509 ktime_set((ts
.tv_sec
& ~(GENMASK(1, 0))) | ts_header
>> 30,
4510 ts_header
& GENMASK(29, 0));
4511 header
->reserved2
= 0;
4518 #define LAN8841_EVENT_A 0
4519 #define LAN8841_EVENT_B 1
4520 #define LAN8841_PTP_LTC_TARGET_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 278 : 288)
4521 #define LAN8841_PTP_LTC_TARGET_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 279 : 289)
4522 #define LAN8841_PTP_LTC_TARGET_NS_HI(event) ((event) == LAN8841_EVENT_A ? 280 : 290)
4523 #define LAN8841_PTP_LTC_TARGET_NS_LO(event) ((event) == LAN8841_EVENT_A ? 281 : 291)
4525 static int lan8841_ptp_set_target(struct kszphy_ptp_priv
*ptp_priv
, u8 event
,
4528 struct phy_device
*phydev
= ptp_priv
->phydev
;
4531 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_SEC_HI(event
),
4532 upper_16_bits(sec
));
4536 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_SEC_LO(event
),
4537 lower_16_bits(sec
));
4541 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_NS_HI(event
) & 0x3fff,
4542 upper_16_bits(nsec
));
4546 return phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_NS_LO(event
),
4547 lower_16_bits(nsec
));
4550 #define LAN8841_BUFFER_TIME 2
4552 static int lan8841_ptp_update_target(struct kszphy_ptp_priv
*ptp_priv
,
4553 const struct timespec64
*ts
)
4555 return lan8841_ptp_set_target(ptp_priv
, LAN8841_EVENT_A
,
4556 ts
->tv_sec
+ LAN8841_BUFFER_TIME
, 0);
4559 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event) ((event) == LAN8841_EVENT_A ? 282 : 292)
4560 #define LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event) ((event) == LAN8841_EVENT_A ? 283 : 293)
4561 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event) ((event) == LAN8841_EVENT_A ? 284 : 294)
4562 #define LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event) ((event) == LAN8841_EVENT_A ? 285 : 295)
4564 static int lan8841_ptp_set_reload(struct kszphy_ptp_priv
*ptp_priv
, u8 event
,
4567 struct phy_device
*phydev
= ptp_priv
->phydev
;
4570 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_HI(event
),
4571 upper_16_bits(sec
));
4575 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_RELOAD_SEC_LO(event
),
4576 lower_16_bits(sec
));
4580 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_HI(event
) & 0x3fff,
4581 upper_16_bits(nsec
));
4585 return phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_TARGET_RELOAD_NS_LO(event
),
4586 lower_16_bits(nsec
));
4589 #define LAN8841_PTP_LTC_SET_SEC_HI 262
4590 #define LAN8841_PTP_LTC_SET_SEC_MID 263
4591 #define LAN8841_PTP_LTC_SET_SEC_LO 264
4592 #define LAN8841_PTP_LTC_SET_NS_HI 265
4593 #define LAN8841_PTP_LTC_SET_NS_LO 266
4594 #define LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD BIT(4)
4596 static int lan8841_ptp_settime64(struct ptp_clock_info
*ptp
,
4597 const struct timespec64
*ts
)
4599 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4601 struct phy_device
*phydev
= ptp_priv
->phydev
;
4602 unsigned long flags
;
4605 /* Set the value to be stored */
4606 mutex_lock(&ptp_priv
->ptp_lock
);
4607 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_SET_SEC_LO
, lower_16_bits(ts
->tv_sec
));
4608 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_SET_SEC_MID
, upper_16_bits(ts
->tv_sec
));
4609 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_SET_SEC_HI
, upper_32_bits(ts
->tv_sec
) & 0xffff);
4610 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_SET_NS_LO
, lower_16_bits(ts
->tv_nsec
));
4611 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_SET_NS_HI
, upper_16_bits(ts
->tv_nsec
) & 0x3fff);
4613 /* Set the command to load the LTC */
4614 phy_write_mmd(phydev
, 2, LAN8841_PTP_CMD_CTL
,
4615 LAN8841_PTP_CMD_CTL_PTP_LTC_LOAD
);
4616 ret
= lan8841_ptp_update_target(ptp_priv
, ts
);
4617 mutex_unlock(&ptp_priv
->ptp_lock
);
4619 spin_lock_irqsave(&ptp_priv
->seconds_lock
, flags
);
4620 ptp_priv
->seconds
= ts
->tv_sec
;
4621 spin_unlock_irqrestore(&ptp_priv
->seconds_lock
, flags
);
4626 #define LAN8841_PTP_LTC_RD_SEC_HI 358
4627 #define LAN8841_PTP_LTC_RD_SEC_MID 359
4628 #define LAN8841_PTP_LTC_RD_SEC_LO 360
4629 #define LAN8841_PTP_LTC_RD_NS_HI 361
4630 #define LAN8841_PTP_LTC_RD_NS_LO 362
4631 #define LAN8841_PTP_CMD_CTL_PTP_LTC_READ BIT(3)
4633 static int lan8841_ptp_gettime64(struct ptp_clock_info
*ptp
,
4634 struct timespec64
*ts
)
4636 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4638 struct phy_device
*phydev
= ptp_priv
->phydev
;
4642 mutex_lock(&ptp_priv
->ptp_lock
);
4643 /* Issue the command to read the LTC */
4644 phy_write_mmd(phydev
, 2, LAN8841_PTP_CMD_CTL
,
4645 LAN8841_PTP_CMD_CTL_PTP_LTC_READ
);
4648 s
= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_HI
);
4650 s
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_MID
);
4652 s
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_LO
);
4654 ns
= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_NS_HI
) & 0x3fff;
4656 ns
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_NS_LO
);
4657 mutex_unlock(&ptp_priv
->ptp_lock
);
4659 set_normalized_timespec64(ts
, s
, ns
);
4663 static void lan8841_ptp_getseconds(struct ptp_clock_info
*ptp
,
4664 struct timespec64
*ts
)
4666 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4668 struct phy_device
*phydev
= ptp_priv
->phydev
;
4671 mutex_lock(&ptp_priv
->ptp_lock
);
4672 /* Issue the command to read the LTC */
4673 phy_write_mmd(phydev
, 2, LAN8841_PTP_CMD_CTL
,
4674 LAN8841_PTP_CMD_CTL_PTP_LTC_READ
);
4677 s
= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_HI
);
4679 s
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_MID
);
4681 s
|= phy_read_mmd(phydev
, 2, LAN8841_PTP_LTC_RD_SEC_LO
);
4682 mutex_unlock(&ptp_priv
->ptp_lock
);
4684 set_normalized_timespec64(ts
, s
, 0);
4687 #define LAN8841_PTP_LTC_STEP_ADJ_LO 276
4688 #define LAN8841_PTP_LTC_STEP_ADJ_HI 275
4689 #define LAN8841_PTP_LTC_STEP_ADJ_DIR BIT(15)
4690 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS BIT(5)
4691 #define LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS BIT(6)
4693 static int lan8841_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
4695 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4697 struct phy_device
*phydev
= ptp_priv
->phydev
;
4698 struct timespec64 ts
;
4704 /* The HW allows up to 15 sec to adjust the time, but here we limit to
4705 * 10 sec the adjustment. The reason is, in case the adjustment is 14
4706 * sec and 999999999 nsec, then we add 8ns to compansate the actual
4707 * increment so the value can be bigger than 15 sec. Therefore limit the
4708 * possible adjustments so we will not have these corner cases
4710 if (delta
> 10000000000LL || delta
< -10000000000LL) {
4711 /* The timeadjustment is too big, so fall back using set time */
4714 ptp
->gettime64(ptp
, &ts
);
4716 now
= ktime_to_ns(timespec64_to_ktime(ts
));
4717 ts
= ns_to_timespec64(now
+ delta
);
4719 ptp
->settime64(ptp
, &ts
);
4723 sec
= div_u64_rem(delta
< 0 ? -delta
: delta
, NSEC_PER_SEC
, &nsec
);
4724 if (delta
< 0 && nsec
!= 0) {
4725 /* It is not allowed to adjust low the nsec part, therefore
4726 * subtract more from second part and add to nanosecond such
4727 * that would roll over, so the second part will increase
4730 nsec
= NSEC_PER_SEC
- nsec
;
4733 /* Calculate the adjustments and the direction */
4738 /* add 8 ns to cover the likely normal increment */
4741 if (nsec
>= NSEC_PER_SEC
) {
4742 /* carry into seconds */
4744 nsec
-= NSEC_PER_SEC
;
4747 mutex_lock(&ptp_priv
->ptp_lock
);
4749 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_STEP_ADJ_LO
, sec
);
4750 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_STEP_ADJ_HI
,
4751 add
? LAN8841_PTP_LTC_STEP_ADJ_DIR
: 0);
4752 phy_write_mmd(phydev
, 2, LAN8841_PTP_CMD_CTL
,
4753 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_SECONDS
);
4757 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_STEP_ADJ_LO
,
4759 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_STEP_ADJ_HI
,
4760 (nsec
>> 16) & 0x3fff);
4761 phy_write_mmd(phydev
, 2, LAN8841_PTP_CMD_CTL
,
4762 LAN8841_PTP_CMD_CTL_PTP_LTC_STEP_NANOSECONDS
);
4764 mutex_unlock(&ptp_priv
->ptp_lock
);
4766 /* Update the target clock */
4767 ptp
->gettime64(ptp
, &ts
);
4768 mutex_lock(&ptp_priv
->ptp_lock
);
4769 ret
= lan8841_ptp_update_target(ptp_priv
, &ts
);
4770 mutex_unlock(&ptp_priv
->ptp_lock
);
4775 #define LAN8841_PTP_LTC_RATE_ADJ_HI 269
4776 #define LAN8841_PTP_LTC_RATE_ADJ_HI_DIR BIT(15)
4777 #define LAN8841_PTP_LTC_RATE_ADJ_LO 270
4779 static int lan8841_ptp_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
4781 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4783 struct phy_device
*phydev
= ptp_priv
->phydev
;
4790 if (scaled_ppm
< 0) {
4791 scaled_ppm
= -scaled_ppm
;
4795 rate
= LAN8841_1PPM_FORMAT
* (upper_16_bits(scaled_ppm
));
4796 rate
+= (LAN8841_1PPM_FORMAT
* (lower_16_bits(scaled_ppm
))) >> 16;
4798 mutex_lock(&ptp_priv
->ptp_lock
);
4799 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_RATE_ADJ_HI
,
4800 faster
? LAN8841_PTP_LTC_RATE_ADJ_HI_DIR
| (upper_16_bits(rate
) & 0x3fff)
4801 : upper_16_bits(rate
) & 0x3fff);
4802 phy_write_mmd(phydev
, 2, LAN8841_PTP_LTC_RATE_ADJ_LO
, lower_16_bits(rate
));
4803 mutex_unlock(&ptp_priv
->ptp_lock
);
4808 static int lan8841_ptp_verify(struct ptp_clock_info
*ptp
, unsigned int pin
,
4809 enum ptp_pin_function func
, unsigned int chan
)
4823 #define LAN8841_PTP_GPIO_NUM 10
4824 #define LAN8841_GPIO_EN 128
4825 #define LAN8841_GPIO_DIR 129
4826 #define LAN8841_GPIO_BUF 130
4828 static int lan8841_ptp_perout_off(struct kszphy_ptp_priv
*ptp_priv
, int pin
)
4830 struct phy_device
*phydev
= ptp_priv
->phydev
;
4833 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_EN
, BIT(pin
));
4837 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_DIR
, BIT(pin
));
4841 return phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_BUF
, BIT(pin
));
4844 static int lan8841_ptp_perout_on(struct kszphy_ptp_priv
*ptp_priv
, int pin
)
4846 struct phy_device
*phydev
= ptp_priv
->phydev
;
4849 ret
= phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_EN
, BIT(pin
));
4853 ret
= phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_DIR
, BIT(pin
));
4857 return phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_BUF
, BIT(pin
));
4860 #define LAN8841_GPIO_DATA_SEL1 131
4861 #define LAN8841_GPIO_DATA_SEL2 132
4862 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK GENMASK(2, 0)
4863 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A 1
4864 #define LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B 2
4865 #define LAN8841_PTP_GENERAL_CONFIG 257
4866 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A BIT(1)
4867 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B BIT(3)
4868 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK GENMASK(7, 4)
4869 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK GENMASK(11, 8)
4870 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A 4
4871 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B 7
4873 static int lan8841_ptp_remove_event(struct kszphy_ptp_priv
*ptp_priv
, int pin
,
4876 struct phy_device
*phydev
= ptp_priv
->phydev
;
4880 /* Now remove pin from the event. GPIO_DATA_SEL1 contains the GPIO
4881 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4882 * depending on the pin, it requires to read a different register
4885 tmp
= LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK
<< (3 * pin
);
4886 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_DATA_SEL1
, tmp
);
4888 tmp
= LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_MASK
<< (3 * (pin
- 5));
4889 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_DATA_SEL2
, tmp
);
4894 /* Disable the event */
4895 if (event
== LAN8841_EVENT_A
)
4896 tmp
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A
|
4897 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK
;
4899 tmp
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B
|
4900 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK
;
4901 return phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_EN
, tmp
);
4904 static int lan8841_ptp_enable_event(struct kszphy_ptp_priv
*ptp_priv
, int pin
,
4905 u8 event
, int pulse_width
)
4907 struct phy_device
*phydev
= ptp_priv
->phydev
;
4911 /* Enable the event */
4912 if (event
== LAN8841_EVENT_A
)
4913 ret
= phy_modify_mmd(phydev
, 2, LAN8841_PTP_GENERAL_CONFIG
,
4914 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A
|
4915 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A_MASK
,
4916 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_A
|
4917 pulse_width
<< LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_A
);
4919 ret
= phy_modify_mmd(phydev
, 2, LAN8841_PTP_GENERAL_CONFIG
,
4920 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B
|
4921 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B_MASK
,
4922 LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_POL_B
|
4923 pulse_width
<< LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_B
);
4927 /* Now connect the pin to the event. GPIO_DATA_SEL1 contains the GPIO
4928 * pins 0-4 while GPIO_DATA_SEL2 contains GPIO pins 5-9, therefore
4929 * depending on the pin, it requires to read a different register
4931 if (event
== LAN8841_EVENT_A
)
4932 tmp
= LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_A
;
4934 tmp
= LAN8841_GPIO_DATA_SEL_GPIO_DATA_SEL_EVENT_B
;
4937 ret
= phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_DATA_SEL1
,
4940 ret
= phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_DATA_SEL2
,
4941 tmp
<< (3 * (pin
- 5)));
4946 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS 13
4947 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS 12
4948 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS 11
4949 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS 10
4950 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS 9
4951 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS 8
4952 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US 7
4953 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US 6
4954 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US 5
4955 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US 4
4956 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US 3
4957 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US 2
4958 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS 1
4959 #define LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS 0
4961 static int lan8841_ptp_perout(struct ptp_clock_info
*ptp
,
4962 struct ptp_clock_request
*rq
, int on
)
4964 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
4966 struct phy_device
*phydev
= ptp_priv
->phydev
;
4967 struct timespec64 ts_on
, ts_period
;
4968 s64 on_nsec
, period_nsec
;
4973 if (rq
->perout
.flags
& ~PTP_PEROUT_DUTY_CYCLE
)
4976 pin
= ptp_find_pin(ptp_priv
->ptp_clock
, PTP_PF_PEROUT
, rq
->perout
.index
);
4977 if (pin
== -1 || pin
>= LAN8841_PTP_GPIO_NUM
)
4981 ret
= lan8841_ptp_perout_off(ptp_priv
, pin
);
4985 return lan8841_ptp_remove_event(ptp_priv
, LAN8841_EVENT_A
, pin
);
4988 ts_on
.tv_sec
= rq
->perout
.on
.sec
;
4989 ts_on
.tv_nsec
= rq
->perout
.on
.nsec
;
4990 on_nsec
= timespec64_to_ns(&ts_on
);
4992 ts_period
.tv_sec
= rq
->perout
.period
.sec
;
4993 ts_period
.tv_nsec
= rq
->perout
.period
.nsec
;
4994 period_nsec
= timespec64_to_ns(&ts_period
);
4996 if (period_nsec
< 200) {
4997 pr_warn_ratelimited("%s: perout period too small, minimum is 200 nsec\n",
4998 phydev_name(phydev
));
5002 if (on_nsec
>= period_nsec
) {
5003 pr_warn_ratelimited("%s: pulse width must be smaller than period\n",
5004 phydev_name(phydev
));
5010 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_200MS
;
5013 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100MS
;
5016 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50MS
;
5019 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10MS
;
5022 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5MS
;
5025 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1MS
;
5028 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500US
;
5031 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100US
;
5034 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_50US
;
5037 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_10US
;
5040 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_5US
;
5043 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_1US
;
5046 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_500NS
;
5049 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS
;
5052 pr_warn_ratelimited("%s: Use default duty cycle of 100ns\n",
5053 phydev_name(phydev
));
5054 pulse_width
= LAN8841_PTP_GENERAL_CONFIG_LTC_EVENT_100NS
;
5058 mutex_lock(&ptp_priv
->ptp_lock
);
5059 ret
= lan8841_ptp_set_target(ptp_priv
, LAN8841_EVENT_A
, rq
->perout
.start
.sec
,
5060 rq
->perout
.start
.nsec
);
5061 mutex_unlock(&ptp_priv
->ptp_lock
);
5065 ret
= lan8841_ptp_set_reload(ptp_priv
, LAN8841_EVENT_A
, rq
->perout
.period
.sec
,
5066 rq
->perout
.period
.nsec
);
5070 ret
= lan8841_ptp_enable_event(ptp_priv
, pin
, LAN8841_EVENT_A
,
5075 ret
= lan8841_ptp_perout_on(ptp_priv
, pin
);
5077 lan8841_ptp_remove_event(ptp_priv
, pin
, LAN8841_EVENT_A
);
5082 #define LAN8841_PTP_GPIO_CAP_EN 496
5083 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(gpio) (BIT(gpio))
5084 #define LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(gpio) (BIT(gpio) << 8)
5085 #define LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN BIT(2)
5087 static int lan8841_ptp_extts_on(struct kszphy_ptp_priv
*ptp_priv
, int pin
,
5090 struct phy_device
*phydev
= ptp_priv
->phydev
;
5094 /* Set GPIO to be intput */
5095 ret
= phy_set_bits_mmd(phydev
, 2, LAN8841_GPIO_EN
, BIT(pin
));
5099 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_BUF
, BIT(pin
));
5103 /* Enable capture on the edges of the pin */
5104 if (flags
& PTP_RISING_EDGE
)
5105 tmp
|= LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin
);
5106 if (flags
& PTP_FALLING_EDGE
)
5107 tmp
|= LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin
);
5108 ret
= phy_write_mmd(phydev
, 2, LAN8841_PTP_GPIO_CAP_EN
, tmp
);
5112 /* Enable interrupt */
5113 return phy_modify_mmd(phydev
, 2, LAN8841_PTP_INT_EN
,
5114 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN
,
5115 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN
);
5118 static int lan8841_ptp_extts_off(struct kszphy_ptp_priv
*ptp_priv
, int pin
)
5120 struct phy_device
*phydev
= ptp_priv
->phydev
;
5123 /* Set GPIO to be output */
5124 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_EN
, BIT(pin
));
5128 ret
= phy_clear_bits_mmd(phydev
, 2, LAN8841_GPIO_BUF
, BIT(pin
));
5132 /* Disable capture on both of the edges */
5133 ret
= phy_modify_mmd(phydev
, 2, LAN8841_PTP_GPIO_CAP_EN
,
5134 LAN8841_PTP_GPIO_CAP_EN_GPIO_RE_CAPTURE_ENABLE(pin
) |
5135 LAN8841_PTP_GPIO_CAP_EN_GPIO_FE_CAPTURE_ENABLE(pin
),
5140 /* Disable interrupt */
5141 return phy_modify_mmd(phydev
, 2, LAN8841_PTP_INT_EN
,
5142 LAN8841_PTP_INT_EN_PTP_GPIO_CAP_EN
,
5146 static int lan8841_ptp_extts(struct ptp_clock_info
*ptp
,
5147 struct ptp_clock_request
*rq
, int on
)
5149 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
5154 /* Reject requests with unsupported flags */
5155 if (rq
->extts
.flags
& ~(PTP_ENABLE_FEATURE
|
5160 pin
= ptp_find_pin(ptp_priv
->ptp_clock
, PTP_PF_EXTTS
, rq
->extts
.index
);
5161 if (pin
== -1 || pin
>= LAN8841_PTP_GPIO_NUM
)
5164 mutex_lock(&ptp_priv
->ptp_lock
);
5166 ret
= lan8841_ptp_extts_on(ptp_priv
, pin
, rq
->extts
.flags
);
5168 ret
= lan8841_ptp_extts_off(ptp_priv
, pin
);
5169 mutex_unlock(&ptp_priv
->ptp_lock
);
5174 static int lan8841_ptp_enable(struct ptp_clock_info
*ptp
,
5175 struct ptp_clock_request
*rq
, int on
)
5178 case PTP_CLK_REQ_EXTTS
:
5179 return lan8841_ptp_extts(ptp
, rq
, on
);
5180 case PTP_CLK_REQ_PEROUT
:
5181 return lan8841_ptp_perout(ptp
, rq
, on
);
5189 static long lan8841_ptp_do_aux_work(struct ptp_clock_info
*ptp
)
5191 struct kszphy_ptp_priv
*ptp_priv
= container_of(ptp
, struct kszphy_ptp_priv
,
5193 struct timespec64 ts
;
5194 unsigned long flags
;
5196 lan8841_ptp_getseconds(&ptp_priv
->ptp_clock_info
, &ts
);
5198 spin_lock_irqsave(&ptp_priv
->seconds_lock
, flags
);
5199 ptp_priv
->seconds
= ts
.tv_sec
;
5200 spin_unlock_irqrestore(&ptp_priv
->seconds_lock
, flags
);
5202 return nsecs_to_jiffies(LAN8841_GET_SEC_LTC_DELAY
);
5205 static struct ptp_clock_info lan8841_ptp_clock_info
= {
5206 .owner
= THIS_MODULE
,
5207 .name
= "lan8841 ptp",
5208 .max_adj
= 31249999,
5209 .gettime64
= lan8841_ptp_gettime64
,
5210 .settime64
= lan8841_ptp_settime64
,
5211 .adjtime
= lan8841_ptp_adjtime
,
5212 .adjfine
= lan8841_ptp_adjfine
,
5213 .verify
= lan8841_ptp_verify
,
5214 .enable
= lan8841_ptp_enable
,
5215 .do_aux_work
= lan8841_ptp_do_aux_work
,
5216 .n_per_out
= LAN8841_PTP_GPIO_NUM
,
5217 .n_ext_ts
= LAN8841_PTP_GPIO_NUM
,
5218 .n_pins
= LAN8841_PTP_GPIO_NUM
,
5221 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER 3
5222 #define LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN BIT(0)
5224 static int lan8841_probe(struct phy_device
*phydev
)
5226 struct kszphy_ptp_priv
*ptp_priv
;
5227 struct kszphy_priv
*priv
;
5230 err
= kszphy_probe(phydev
);
5234 if (phy_read_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
5235 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER
) &
5236 LAN8841_OPERATION_MODE_STRAP_LOW_REGISTER_STRAP_RGMII_EN
)
5237 phydev
->interface
= PHY_INTERFACE_MODE_RGMII_RXID
;
5239 /* Register the clock */
5240 if (!IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING
))
5243 priv
= phydev
->priv
;
5244 ptp_priv
= &priv
->ptp_priv
;
5246 ptp_priv
->pin_config
= devm_kcalloc(&phydev
->mdio
.dev
,
5247 LAN8841_PTP_GPIO_NUM
,
5248 sizeof(*ptp_priv
->pin_config
),
5250 if (!ptp_priv
->pin_config
)
5253 for (int i
= 0; i
< LAN8841_PTP_GPIO_NUM
; ++i
) {
5254 struct ptp_pin_desc
*p
= &ptp_priv
->pin_config
[i
];
5256 snprintf(p
->name
, sizeof(p
->name
), "pin%d", i
);
5258 p
->func
= PTP_PF_NONE
;
5261 ptp_priv
->ptp_clock_info
= lan8841_ptp_clock_info
;
5262 ptp_priv
->ptp_clock_info
.pin_config
= ptp_priv
->pin_config
;
5263 ptp_priv
->ptp_clock
= ptp_clock_register(&ptp_priv
->ptp_clock_info
,
5265 if (IS_ERR(ptp_priv
->ptp_clock
)) {
5266 phydev_err(phydev
, "ptp_clock_register failed: %lu\n",
5267 PTR_ERR(ptp_priv
->ptp_clock
));
5271 if (!ptp_priv
->ptp_clock
)
5274 /* Initialize the SW */
5275 skb_queue_head_init(&ptp_priv
->tx_queue
);
5276 ptp_priv
->phydev
= phydev
;
5277 mutex_init(&ptp_priv
->ptp_lock
);
5278 spin_lock_init(&ptp_priv
->seconds_lock
);
5280 ptp_priv
->mii_ts
.rxtstamp
= lan8841_rxtstamp
;
5281 ptp_priv
->mii_ts
.txtstamp
= lan8814_txtstamp
;
5282 ptp_priv
->mii_ts
.hwtstamp
= lan8841_hwtstamp
;
5283 ptp_priv
->mii_ts
.ts_info
= lan8841_ts_info
;
5285 phydev
->mii_ts
= &ptp_priv
->mii_ts
;
5287 /* Timestamp selected by default to keep legacy API */
5288 phydev
->default_timestamp
= true;
5293 static int lan8841_suspend(struct phy_device
*phydev
)
5295 struct kszphy_priv
*priv
= phydev
->priv
;
5296 struct kszphy_ptp_priv
*ptp_priv
= &priv
->ptp_priv
;
5298 if (ptp_priv
->ptp_clock
)
5299 ptp_cancel_worker_sync(ptp_priv
->ptp_clock
);
5301 return genphy_suspend(phydev
);
5304 static struct phy_driver ksphy_driver
[] = {
5306 .phy_id
= PHY_ID_KS8737
,
5307 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5308 .name
= "Micrel KS8737",
5309 /* PHY_BASIC_FEATURES */
5310 .driver_data
= &ks8737_type
,
5311 .probe
= kszphy_probe
,
5312 .config_init
= kszphy_config_init
,
5313 .config_intr
= kszphy_config_intr
,
5314 .handle_interrupt
= kszphy_handle_interrupt
,
5315 .suspend
= kszphy_suspend
,
5316 .resume
= kszphy_resume
,
5318 .phy_id
= PHY_ID_KSZ8021
,
5319 .phy_id_mask
= 0x00ffffff,
5320 .name
= "Micrel KSZ8021 or KSZ8031",
5321 /* PHY_BASIC_FEATURES */
5322 .driver_data
= &ksz8021_type
,
5323 .probe
= kszphy_probe
,
5324 .config_init
= kszphy_config_init
,
5325 .config_intr
= kszphy_config_intr
,
5326 .handle_interrupt
= kszphy_handle_interrupt
,
5327 .get_sset_count
= kszphy_get_sset_count
,
5328 .get_strings
= kszphy_get_strings
,
5329 .get_stats
= kszphy_get_stats
,
5330 .suspend
= kszphy_suspend
,
5331 .resume
= kszphy_resume
,
5333 .phy_id
= PHY_ID_KSZ8031
,
5334 .phy_id_mask
= 0x00ffffff,
5335 .name
= "Micrel KSZ8031",
5336 /* PHY_BASIC_FEATURES */
5337 .driver_data
= &ksz8021_type
,
5338 .probe
= kszphy_probe
,
5339 .config_init
= kszphy_config_init
,
5340 .config_intr
= kszphy_config_intr
,
5341 .handle_interrupt
= kszphy_handle_interrupt
,
5342 .get_sset_count
= kszphy_get_sset_count
,
5343 .get_strings
= kszphy_get_strings
,
5344 .get_stats
= kszphy_get_stats
,
5345 .suspend
= kszphy_suspend
,
5346 .resume
= kszphy_resume
,
5348 .phy_id
= PHY_ID_KSZ8041
,
5349 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5350 .name
= "Micrel KSZ8041",
5351 /* PHY_BASIC_FEATURES */
5352 .driver_data
= &ksz8041_type
,
5353 .probe
= kszphy_probe
,
5354 .config_init
= ksz8041_config_init
,
5355 .config_aneg
= ksz8041_config_aneg
,
5356 .config_intr
= kszphy_config_intr
,
5357 .handle_interrupt
= kszphy_handle_interrupt
,
5358 .get_sset_count
= kszphy_get_sset_count
,
5359 .get_strings
= kszphy_get_strings
,
5360 .get_stats
= kszphy_get_stats
,
5361 /* No suspend/resume callbacks because of errata DS80000700A,
5362 * receiver error following software power down.
5365 .phy_id
= PHY_ID_KSZ8041RNLI
,
5366 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5367 .name
= "Micrel KSZ8041RNLI",
5368 /* PHY_BASIC_FEATURES */
5369 .driver_data
= &ksz8041_type
,
5370 .probe
= kszphy_probe
,
5371 .config_init
= kszphy_config_init
,
5372 .config_intr
= kszphy_config_intr
,
5373 .handle_interrupt
= kszphy_handle_interrupt
,
5374 .get_sset_count
= kszphy_get_sset_count
,
5375 .get_strings
= kszphy_get_strings
,
5376 .get_stats
= kszphy_get_stats
,
5377 .suspend
= kszphy_suspend
,
5378 .resume
= kszphy_resume
,
5380 .name
= "Micrel KSZ8051",
5381 /* PHY_BASIC_FEATURES */
5382 .driver_data
= &ksz8051_type
,
5383 .probe
= kszphy_probe
,
5384 .config_init
= kszphy_config_init
,
5385 .config_intr
= kszphy_config_intr
,
5386 .handle_interrupt
= kszphy_handle_interrupt
,
5387 .get_sset_count
= kszphy_get_sset_count
,
5388 .get_strings
= kszphy_get_strings
,
5389 .get_stats
= kszphy_get_stats
,
5390 .match_phy_device
= ksz8051_match_phy_device
,
5391 .suspend
= kszphy_suspend
,
5392 .resume
= kszphy_resume
,
5394 .phy_id
= PHY_ID_KSZ8001
,
5395 .name
= "Micrel KSZ8001 or KS8721",
5396 .phy_id_mask
= 0x00fffffc,
5397 /* PHY_BASIC_FEATURES */
5398 .driver_data
= &ksz8041_type
,
5399 .probe
= kszphy_probe
,
5400 .config_init
= kszphy_config_init
,
5401 .config_intr
= kszphy_config_intr
,
5402 .handle_interrupt
= kszphy_handle_interrupt
,
5403 .get_sset_count
= kszphy_get_sset_count
,
5404 .get_strings
= kszphy_get_strings
,
5405 .get_stats
= kszphy_get_stats
,
5406 .suspend
= kszphy_suspend
,
5407 .resume
= kszphy_resume
,
5409 .phy_id
= PHY_ID_KSZ8081
,
5410 .name
= "Micrel KSZ8081 or KSZ8091",
5411 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5412 .flags
= PHY_POLL_CABLE_TEST
,
5413 /* PHY_BASIC_FEATURES */
5414 .driver_data
= &ksz8081_type
,
5415 .probe
= kszphy_probe
,
5416 .config_init
= ksz8081_config_init
,
5417 .soft_reset
= genphy_soft_reset
,
5418 .config_aneg
= ksz8081_config_aneg
,
5419 .read_status
= ksz8081_read_status
,
5420 .config_intr
= kszphy_config_intr
,
5421 .handle_interrupt
= kszphy_handle_interrupt
,
5422 .get_sset_count
= kszphy_get_sset_count
,
5423 .get_strings
= kszphy_get_strings
,
5424 .get_stats
= kszphy_get_stats
,
5425 .suspend
= kszphy_suspend
,
5426 .resume
= kszphy_resume
,
5427 .cable_test_start
= ksz886x_cable_test_start
,
5428 .cable_test_get_status
= ksz886x_cable_test_get_status
,
5430 .phy_id
= PHY_ID_KSZ8061
,
5431 .name
= "Micrel KSZ8061",
5432 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5433 /* PHY_BASIC_FEATURES */
5434 .probe
= kszphy_probe
,
5435 .config_init
= ksz8061_config_init
,
5436 .soft_reset
= genphy_soft_reset
,
5437 .config_intr
= kszphy_config_intr
,
5438 .handle_interrupt
= kszphy_handle_interrupt
,
5439 .suspend
= kszphy_suspend
,
5440 .resume
= ksz8061_resume
,
5442 .phy_id
= PHY_ID_KSZ9021
,
5443 .phy_id_mask
= 0x000ffffe,
5444 .name
= "Micrel KSZ9021 Gigabit PHY",
5445 /* PHY_GBIT_FEATURES */
5446 .driver_data
= &ksz9021_type
,
5447 .probe
= kszphy_probe
,
5448 .get_features
= ksz9031_get_features
,
5449 .config_init
= ksz9021_config_init
,
5450 .config_intr
= kszphy_config_intr
,
5451 .handle_interrupt
= kszphy_handle_interrupt
,
5452 .get_sset_count
= kszphy_get_sset_count
,
5453 .get_strings
= kszphy_get_strings
,
5454 .get_stats
= kszphy_get_stats
,
5455 .suspend
= kszphy_suspend
,
5456 .resume
= kszphy_resume
,
5457 .read_mmd
= genphy_read_mmd_unsupported
,
5458 .write_mmd
= genphy_write_mmd_unsupported
,
5460 .phy_id
= PHY_ID_KSZ9031
,
5461 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5462 .name
= "Micrel KSZ9031 Gigabit PHY",
5463 .flags
= PHY_POLL_CABLE_TEST
,
5464 .driver_data
= &ksz9021_type
,
5465 .probe
= kszphy_probe
,
5466 .get_features
= ksz9031_get_features
,
5467 .config_init
= ksz9031_config_init
,
5468 .soft_reset
= genphy_soft_reset
,
5469 .read_status
= ksz9031_read_status
,
5470 .config_intr
= kszphy_config_intr
,
5471 .handle_interrupt
= kszphy_handle_interrupt
,
5472 .get_sset_count
= kszphy_get_sset_count
,
5473 .get_strings
= kszphy_get_strings
,
5474 .get_stats
= kszphy_get_stats
,
5475 .suspend
= kszphy_suspend
,
5476 .resume
= kszphy_resume
,
5477 .cable_test_start
= ksz9x31_cable_test_start
,
5478 .cable_test_get_status
= ksz9x31_cable_test_get_status
,
5480 .phy_id
= PHY_ID_LAN8814
,
5481 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5482 .name
= "Microchip INDY Gigabit Quad PHY",
5483 .flags
= PHY_POLL_CABLE_TEST
,
5484 .config_init
= lan8814_config_init
,
5485 .driver_data
= &lan8814_type
,
5486 .probe
= lan8814_probe
,
5487 .soft_reset
= genphy_soft_reset
,
5488 .read_status
= ksz9031_read_status
,
5489 .get_sset_count
= kszphy_get_sset_count
,
5490 .get_strings
= kszphy_get_strings
,
5491 .get_stats
= kszphy_get_stats
,
5492 .suspend
= genphy_suspend
,
5493 .resume
= kszphy_resume
,
5494 .config_intr
= lan8814_config_intr
,
5495 .handle_interrupt
= lan8814_handle_interrupt
,
5496 .cable_test_start
= lan8814_cable_test_start
,
5497 .cable_test_get_status
= ksz886x_cable_test_get_status
,
5499 .phy_id
= PHY_ID_LAN8804
,
5500 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5501 .name
= "Microchip LAN966X Gigabit PHY",
5502 .config_init
= lan8804_config_init
,
5503 .driver_data
= &ksz9021_type
,
5504 .probe
= kszphy_probe
,
5505 .soft_reset
= genphy_soft_reset
,
5506 .read_status
= ksz9031_read_status
,
5507 .get_sset_count
= kszphy_get_sset_count
,
5508 .get_strings
= kszphy_get_strings
,
5509 .get_stats
= kszphy_get_stats
,
5510 .suspend
= genphy_suspend
,
5511 .resume
= kszphy_resume
,
5512 .config_intr
= lan8804_config_intr
,
5513 .handle_interrupt
= lan8804_handle_interrupt
,
5515 .phy_id
= PHY_ID_LAN8841
,
5516 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5517 .name
= "Microchip LAN8841 Gigabit PHY",
5518 .flags
= PHY_POLL_CABLE_TEST
,
5519 .driver_data
= &lan8841_type
,
5520 .config_init
= lan8841_config_init
,
5521 .probe
= lan8841_probe
,
5522 .soft_reset
= genphy_soft_reset
,
5523 .config_intr
= lan8841_config_intr
,
5524 .handle_interrupt
= lan8841_handle_interrupt
,
5525 .get_sset_count
= kszphy_get_sset_count
,
5526 .get_strings
= kszphy_get_strings
,
5527 .get_stats
= kszphy_get_stats
,
5528 .suspend
= lan8841_suspend
,
5529 .resume
= genphy_resume
,
5530 .cable_test_start
= lan8814_cable_test_start
,
5531 .cable_test_get_status
= ksz886x_cable_test_get_status
,
5533 .phy_id
= PHY_ID_KSZ9131
,
5534 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5535 .name
= "Microchip KSZ9131 Gigabit PHY",
5536 /* PHY_GBIT_FEATURES */
5537 .flags
= PHY_POLL_CABLE_TEST
,
5538 .driver_data
= &ksz9131_type
,
5539 .probe
= kszphy_probe
,
5540 .soft_reset
= genphy_soft_reset
,
5541 .config_init
= ksz9131_config_init
,
5542 .config_intr
= kszphy_config_intr
,
5543 .config_aneg
= ksz9131_config_aneg
,
5544 .read_status
= ksz9131_read_status
,
5545 .handle_interrupt
= kszphy_handle_interrupt
,
5546 .get_sset_count
= kszphy_get_sset_count
,
5547 .get_strings
= kszphy_get_strings
,
5548 .get_stats
= kszphy_get_stats
,
5549 .suspend
= kszphy_suspend
,
5550 .resume
= kszphy_resume
,
5551 .cable_test_start
= ksz9x31_cable_test_start
,
5552 .cable_test_get_status
= ksz9x31_cable_test_get_status
,
5553 .get_features
= ksz9477_get_features
,
5555 .phy_id
= PHY_ID_KSZ8873MLL
,
5556 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5557 .name
= "Micrel KSZ8873MLL Switch",
5558 /* PHY_BASIC_FEATURES */
5559 .config_init
= kszphy_config_init
,
5560 .config_aneg
= ksz8873mll_config_aneg
,
5561 .read_status
= ksz8873mll_read_status
,
5562 .suspend
= genphy_suspend
,
5563 .resume
= genphy_resume
,
5565 .phy_id
= PHY_ID_KSZ886X
,
5566 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5567 .name
= "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
5568 .driver_data
= &ksz886x_type
,
5569 /* PHY_BASIC_FEATURES */
5570 .flags
= PHY_POLL_CABLE_TEST
,
5571 .config_init
= kszphy_config_init
,
5572 .config_aneg
= ksz886x_config_aneg
,
5573 .read_status
= ksz886x_read_status
,
5574 .suspend
= genphy_suspend
,
5575 .resume
= genphy_resume
,
5576 .cable_test_start
= ksz886x_cable_test_start
,
5577 .cable_test_get_status
= ksz886x_cable_test_get_status
,
5579 .name
= "Micrel KSZ87XX Switch",
5580 /* PHY_BASIC_FEATURES */
5581 .config_init
= kszphy_config_init
,
5582 .match_phy_device
= ksz8795_match_phy_device
,
5583 .suspend
= genphy_suspend
,
5584 .resume
= genphy_resume
,
5586 .phy_id
= PHY_ID_KSZ9477
,
5587 .phy_id_mask
= MICREL_PHY_ID_MASK
,
5588 .name
= "Microchip KSZ9477",
5589 /* PHY_GBIT_FEATURES */
5590 .config_init
= ksz9477_config_init
,
5591 .config_intr
= kszphy_config_intr
,
5592 .handle_interrupt
= kszphy_handle_interrupt
,
5593 .suspend
= genphy_suspend
,
5594 .resume
= ksz9477_resume
,
5595 .get_features
= ksz9477_get_features
,
5598 module_phy_driver(ksphy_driver
);
5600 MODULE_DESCRIPTION("Micrel PHY driver");
5601 MODULE_AUTHOR("David J. Choi");
5602 MODULE_LICENSE("GPL");
5604 static struct mdio_device_id __maybe_unused micrel_tbl
[] = {
5605 { PHY_ID_KSZ9021
, 0x000ffffe },
5606 { PHY_ID_KSZ9031
, MICREL_PHY_ID_MASK
},
5607 { PHY_ID_KSZ9131
, MICREL_PHY_ID_MASK
},
5608 { PHY_ID_KSZ8001
, 0x00fffffc },
5609 { PHY_ID_KS8737
, MICREL_PHY_ID_MASK
},
5610 { PHY_ID_KSZ8021
, 0x00ffffff },
5611 { PHY_ID_KSZ8031
, 0x00ffffff },
5612 { PHY_ID_KSZ8041
, MICREL_PHY_ID_MASK
},
5613 { PHY_ID_KSZ8051
, MICREL_PHY_ID_MASK
},
5614 { PHY_ID_KSZ8061
, MICREL_PHY_ID_MASK
},
5615 { PHY_ID_KSZ8081
, MICREL_PHY_ID_MASK
},
5616 { PHY_ID_KSZ8873MLL
, MICREL_PHY_ID_MASK
},
5617 { PHY_ID_KSZ886X
, MICREL_PHY_ID_MASK
},
5618 { PHY_ID_KSZ9477
, MICREL_PHY_ID_MASK
},
5619 { PHY_ID_LAN8814
, MICREL_PHY_ID_MASK
},
5620 { PHY_ID_LAN8804
, MICREL_PHY_ID_MASK
},
5621 { PHY_ID_LAN8841
, MICREL_PHY_ID_MASK
},
5625 MODULE_DEVICE_TABLE(mdio
, micrel_tbl
);