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.
12 * Support : Micrel Phys:
13 * Giga phys: ksz9021, ksz9031, ksz9131
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>
36 /* Operation Mode Strap Override */
37 #define MII_KSZPHY_OMSO 0x16
38 #define KSZPHY_OMSO_FACTORY_TEST BIT(15)
39 #define KSZPHY_OMSO_B_CAST_OFF BIT(9)
40 #define KSZPHY_OMSO_NAND_TREE_ON BIT(5)
41 #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1)
42 #define KSZPHY_OMSO_MII_OVERRIDE BIT(0)
44 /* general Interrupt control/status reg in vendor specific block. */
45 #define MII_KSZPHY_INTCS 0x1B
46 #define KSZPHY_INTCS_JABBER BIT(15)
47 #define KSZPHY_INTCS_RECEIVE_ERR BIT(14)
48 #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13)
49 #define KSZPHY_INTCS_PARELLEL BIT(12)
50 #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11)
51 #define KSZPHY_INTCS_LINK_DOWN BIT(10)
52 #define KSZPHY_INTCS_REMOTE_FAULT BIT(9)
53 #define KSZPHY_INTCS_LINK_UP BIT(8)
54 #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
55 KSZPHY_INTCS_LINK_DOWN)
56 #define KSZPHY_INTCS_LINK_DOWN_STATUS BIT(2)
57 #define KSZPHY_INTCS_LINK_UP_STATUS BIT(0)
58 #define KSZPHY_INTCS_STATUS (KSZPHY_INTCS_LINK_DOWN_STATUS |\
59 KSZPHY_INTCS_LINK_UP_STATUS)
61 /* LinkMD Control/Status */
62 #define KSZ8081_LMD 0x1d
63 #define KSZ8081_LMD_ENABLE_TEST BIT(15)
64 #define KSZ8081_LMD_STAT_NORMAL 0
65 #define KSZ8081_LMD_STAT_OPEN 1
66 #define KSZ8081_LMD_STAT_SHORT 2
67 #define KSZ8081_LMD_STAT_FAIL 3
68 #define KSZ8081_LMD_STAT_MASK GENMASK(14, 13)
69 /* Short cable (<10 meter) has been detected by LinkMD */
70 #define KSZ8081_LMD_SHORT_INDICATOR BIT(12)
71 #define KSZ8081_LMD_DELTA_TIME_MASK GENMASK(8, 0)
73 /* Lan8814 general Interrupt control/status reg in GPHY specific block. */
74 #define LAN8814_INTC 0x18
75 #define LAN8814_INTS 0x1B
77 #define LAN8814_INT_LINK_DOWN BIT(2)
78 #define LAN8814_INT_LINK_UP BIT(0)
79 #define LAN8814_INT_LINK (LAN8814_INT_LINK_UP |\
80 LAN8814_INT_LINK_DOWN)
82 #define LAN8814_INTR_CTRL_REG 0x34
83 #define LAN8814_INTR_CTRL_REG_POLARITY BIT(1)
84 #define LAN8814_INTR_CTRL_REG_INTR_ENABLE BIT(0)
86 /* Represents 1ppm adjustment in 2^32 format with
87 * each nsec contains 4 clock cycles.
88 * The value is calculated as following: (1/1000000)/((2^-32)/4)
90 #define LAN8814_1PPM_FORMAT 17179
92 #define PTP_RX_MOD 0x024F
93 #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
94 #define PTP_RX_TIMESTAMP_EN 0x024D
95 #define PTP_TX_TIMESTAMP_EN 0x028D
97 #define PTP_TIMESTAMP_EN_SYNC_ BIT(0)
98 #define PTP_TIMESTAMP_EN_DREQ_ BIT(1)
99 #define PTP_TIMESTAMP_EN_PDREQ_ BIT(2)
100 #define PTP_TIMESTAMP_EN_PDRES_ BIT(3)
102 #define PTP_RX_LATENCY_1000 0x0224
103 #define PTP_TX_LATENCY_1000 0x0225
105 #define PTP_RX_LATENCY_100 0x0222
106 #define PTP_TX_LATENCY_100 0x0223
108 #define PTP_RX_LATENCY_10 0x0220
109 #define PTP_TX_LATENCY_10 0x0221
111 #define PTP_TX_PARSE_L2_ADDR_EN 0x0284
112 #define PTP_RX_PARSE_L2_ADDR_EN 0x0244
114 #define PTP_TX_PARSE_IP_ADDR_EN 0x0285
115 #define PTP_RX_PARSE_IP_ADDR_EN 0x0245
116 #define LTC_HARD_RESET 0x023F
117 #define LTC_HARD_RESET_ BIT(0)
119 #define TSU_HARD_RESET 0x02C1
120 #define TSU_HARD_RESET_ BIT(0)
122 #define PTP_CMD_CTL 0x0200
123 #define PTP_CMD_CTL_PTP_DISABLE_ BIT(0)
124 #define PTP_CMD_CTL_PTP_ENABLE_ BIT(1)
125 #define PTP_CMD_CTL_PTP_CLOCK_READ_ BIT(3)
126 #define PTP_CMD_CTL_PTP_CLOCK_LOAD_ BIT(4)
127 #define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5)
128 #define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6)
130 #define PTP_CLOCK_SET_SEC_MID 0x0206
131 #define PTP_CLOCK_SET_SEC_LO 0x0207
132 #define PTP_CLOCK_SET_NS_HI 0x0208
133 #define PTP_CLOCK_SET_NS_LO 0x0209
135 #define PTP_CLOCK_READ_SEC_MID 0x022A
136 #define PTP_CLOCK_READ_SEC_LO 0x022B
137 #define PTP_CLOCK_READ_NS_HI 0x022C
138 #define PTP_CLOCK_READ_NS_LO 0x022D
140 #define PTP_OPERATING_MODE 0x0241
141 #define PTP_OPERATING_MODE_STANDALONE_ BIT(0)
143 #define PTP_TX_MOD 0x028F
144 #define PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_ BIT(12)
145 #define PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
147 #define PTP_RX_PARSE_CONFIG 0x0242
148 #define PTP_RX_PARSE_CONFIG_LAYER2_EN_ BIT(0)
149 #define PTP_RX_PARSE_CONFIG_IPV4_EN_ BIT(1)
150 #define PTP_RX_PARSE_CONFIG_IPV6_EN_ BIT(2)
152 #define PTP_TX_PARSE_CONFIG 0x0282
153 #define PTP_TX_PARSE_CONFIG_LAYER2_EN_ BIT(0)
154 #define PTP_TX_PARSE_CONFIG_IPV4_EN_ BIT(1)
155 #define PTP_TX_PARSE_CONFIG_IPV6_EN_ BIT(2)
157 #define PTP_CLOCK_RATE_ADJ_HI 0x020C
158 #define PTP_CLOCK_RATE_ADJ_LO 0x020D
159 #define PTP_CLOCK_RATE_ADJ_DIR_ BIT(15)
161 #define PTP_LTC_STEP_ADJ_HI 0x0212
162 #define PTP_LTC_STEP_ADJ_LO 0x0213
163 #define PTP_LTC_STEP_ADJ_DIR_ BIT(15)
165 #define LAN8814_INTR_STS_REG 0x0033
166 #define LAN8814_INTR_STS_REG_1588_TSU0_ BIT(0)
167 #define LAN8814_INTR_STS_REG_1588_TSU1_ BIT(1)
168 #define LAN8814_INTR_STS_REG_1588_TSU2_ BIT(2)
169 #define LAN8814_INTR_STS_REG_1588_TSU3_ BIT(3)
171 #define PTP_CAP_INFO 0x022A
172 #define PTP_CAP_INFO_TX_TS_CNT_GET_(reg_val) (((reg_val) & 0x0f00) >> 8)
173 #define PTP_CAP_INFO_RX_TS_CNT_GET_(reg_val) ((reg_val) & 0x000f)
175 #define PTP_TX_EGRESS_SEC_HI 0x0296
176 #define PTP_TX_EGRESS_SEC_LO 0x0297
177 #define PTP_TX_EGRESS_NS_HI 0x0294
178 #define PTP_TX_EGRESS_NS_LO 0x0295
179 #define PTP_TX_MSG_HEADER2 0x0299
181 #define PTP_RX_INGRESS_SEC_HI 0x0256
182 #define PTP_RX_INGRESS_SEC_LO 0x0257
183 #define PTP_RX_INGRESS_NS_HI 0x0254
184 #define PTP_RX_INGRESS_NS_LO 0x0255
185 #define PTP_RX_MSG_HEADER2 0x0259
187 #define PTP_TSU_INT_EN 0x0200
188 #define PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ BIT(3)
189 #define PTP_TSU_INT_EN_PTP_TX_TS_EN_ BIT(2)
190 #define PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_ BIT(1)
191 #define PTP_TSU_INT_EN_PTP_RX_TS_EN_ BIT(0)
193 #define PTP_TSU_INT_STS 0x0201
194 #define PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_ BIT(3)
195 #define PTP_TSU_INT_STS_PTP_TX_TS_EN_ BIT(2)
196 #define PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_ BIT(1)
197 #define PTP_TSU_INT_STS_PTP_RX_TS_EN_ BIT(0)
200 #define MII_KSZPHY_CTRL_1 0x1e
201 #define KSZ8081_CTRL1_MDIX_STAT BIT(4)
203 /* PHY Control 2 / PHY Control (if no PHY Control 1) */
204 #define MII_KSZPHY_CTRL_2 0x1f
205 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2
206 /* bitmap of PHY register to set interrupt mode */
207 #define KSZ8081_CTRL2_HP_MDIX BIT(15)
208 #define KSZ8081_CTRL2_MDI_MDI_X_SELECT BIT(14)
209 #define KSZ8081_CTRL2_DISABLE_AUTO_MDIX BIT(13)
210 #define KSZ8081_CTRL2_FORCE_LINK BIT(11)
211 #define KSZ8081_CTRL2_POWER_SAVING BIT(10)
212 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9)
213 #define KSZPHY_RMII_REF_CLK_SEL BIT(7)
215 /* Write/read to/from extended registers */
216 #define MII_KSZPHY_EXTREG 0x0b
217 #define KSZPHY_EXTREG_WRITE 0x8000
219 #define MII_KSZPHY_EXTREG_WRITE 0x0c
220 #define MII_KSZPHY_EXTREG_READ 0x0d
222 /* Extended registers */
223 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
224 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
225 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
227 #define PS_TO_REG 200
230 struct kszphy_hw_stat {
236 static struct kszphy_hw_stat kszphy_hw_stats[] = {
237 { "phy_receive_errors", 21, 16},
238 { "phy_idle_errors", 10, 8 },
243 u16 interrupt_level_mask;
244 bool has_broadcast_disable;
245 bool has_nand_tree_disable;
246 bool has_rmii_ref_clk_sel;
249 /* Shared structure between the PHYs of the same package. */
250 struct lan8814_shared_priv {
251 struct phy_device *phydev;
252 struct ptp_clock *ptp_clock;
253 struct ptp_clock_info ptp_clock_info;
255 /* Reference counter to how many ports in the package are enabling the
260 /* Lock for ptp_clock and ref */
261 struct mutex shared_lock;
264 struct lan8814_ptp_rx_ts {
265 struct list_head list;
271 struct kszphy_latencies {
280 struct kszphy_ptp_priv {
281 struct mii_timestamper mii_ts;
282 struct phy_device *phydev;
284 struct sk_buff_head tx_queue;
285 struct sk_buff_head rx_queue;
287 struct list_head rx_ts_list;
288 /* Lock for Rx ts fifo */
289 spinlock_t rx_ts_lock;
292 enum hwtstamp_rx_filters rx_filter;
298 struct kszphy_ptp_priv ptp_priv;
299 struct kszphy_latencies latencies;
300 const struct kszphy_type *type;
302 bool rmii_ref_clk_sel;
303 bool rmii_ref_clk_sel_val;
304 u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
307 static struct kszphy_latencies lan8814_latencies = {
315 static const struct kszphy_type ksz8021_type = {
316 .led_mode_reg = MII_KSZPHY_CTRL_2,
317 .has_broadcast_disable = true,
318 .has_nand_tree_disable = true,
319 .has_rmii_ref_clk_sel = true,
322 static const struct kszphy_type ksz8041_type = {
323 .led_mode_reg = MII_KSZPHY_CTRL_1,
326 static const struct kszphy_type ksz8051_type = {
327 .led_mode_reg = MII_KSZPHY_CTRL_2,
328 .has_nand_tree_disable = true,
331 static const struct kszphy_type ksz8081_type = {
332 .led_mode_reg = MII_KSZPHY_CTRL_2,
333 .has_broadcast_disable = true,
334 .has_nand_tree_disable = true,
335 .has_rmii_ref_clk_sel = true,
338 static const struct kszphy_type ks8737_type = {
339 .interrupt_level_mask = BIT(14),
342 static const struct kszphy_type ksz9021_type = {
343 .interrupt_level_mask = BIT(14),
346 static int kszphy_extended_write(struct phy_device *phydev,
349 phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
350 return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
353 static int kszphy_extended_read(struct phy_device *phydev,
356 phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
357 return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
360 static int kszphy_ack_interrupt(struct phy_device *phydev)
362 /* bit[7..0] int status, which is a read and clear register. */
365 rc = phy_read(phydev, MII_KSZPHY_INTCS);
367 return (rc < 0) ? rc : 0;
370 static int kszphy_config_intr(struct phy_device *phydev)
372 const struct kszphy_type *type = phydev->drv->driver_data;
376 if (type && type->interrupt_level_mask)
377 mask = type->interrupt_level_mask;
379 mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
381 /* set the interrupt pin active low */
382 temp = phy_read(phydev, MII_KSZPHY_CTRL);
386 phy_write(phydev, MII_KSZPHY_CTRL, temp);
388 /* enable / disable interrupts */
389 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
390 err = kszphy_ack_interrupt(phydev);
394 temp = KSZPHY_INTCS_ALL;
395 err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
398 err = phy_write(phydev, MII_KSZPHY_INTCS, temp);
402 err = kszphy_ack_interrupt(phydev);
408 static irqreturn_t kszphy_handle_interrupt(struct phy_device *phydev)
412 irq_status = phy_read(phydev, MII_KSZPHY_INTCS);
413 if (irq_status < 0) {
418 if (!(irq_status & KSZPHY_INTCS_STATUS))
421 phy_trigger_machine(phydev);
426 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
430 ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
435 ctrl |= KSZPHY_RMII_REF_CLK_SEL;
437 ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
439 return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
442 static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
447 case MII_KSZPHY_CTRL_1:
450 case MII_KSZPHY_CTRL_2:
457 temp = phy_read(phydev, reg);
463 temp &= ~(3 << shift);
464 temp |= val << shift;
465 rc = phy_write(phydev, reg, temp);
468 phydev_err(phydev, "failed to set led mode\n");
473 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
474 * unique (non-broadcast) address on a shared bus.
476 static int kszphy_broadcast_disable(struct phy_device *phydev)
480 ret = phy_read(phydev, MII_KSZPHY_OMSO);
484 ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
487 phydev_err(phydev, "failed to disable broadcast address\n");
492 static int kszphy_nand_tree_disable(struct phy_device *phydev)
496 ret = phy_read(phydev, MII_KSZPHY_OMSO);
500 if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
503 ret = phy_write(phydev, MII_KSZPHY_OMSO,
504 ret & ~KSZPHY_OMSO_NAND_TREE_ON);
507 phydev_err(phydev, "failed to disable NAND tree mode\n");
512 /* Some config bits need to be set again on resume, handle them here. */
513 static int kszphy_config_reset(struct phy_device *phydev)
515 struct kszphy_priv *priv = phydev->priv;
518 if (priv->rmii_ref_clk_sel) {
519 ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
522 "failed to set rmii reference clock\n");
527 if (priv->led_mode >= 0)
528 kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
533 static int kszphy_config_init(struct phy_device *phydev)
535 struct kszphy_priv *priv = phydev->priv;
536 const struct kszphy_type *type;
543 if (type->has_broadcast_disable)
544 kszphy_broadcast_disable(phydev);
546 if (type->has_nand_tree_disable)
547 kszphy_nand_tree_disable(phydev);
549 return kszphy_config_reset(phydev);
552 static int ksz8041_fiber_mode(struct phy_device *phydev)
554 struct device_node *of_node = phydev->mdio.dev.of_node;
556 return of_property_read_bool(of_node, "micrel,fiber-mode");
559 static int ksz8041_config_init(struct phy_device *phydev)
561 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
563 /* Limit supported and advertised modes in fiber mode */
564 if (ksz8041_fiber_mode(phydev)) {
565 phydev->dev_flags |= MICREL_PHY_FXEN;
566 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, mask);
567 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, mask);
569 linkmode_and(phydev->supported, phydev->supported, mask);
570 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
572 linkmode_and(phydev->advertising, phydev->advertising, mask);
573 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT,
574 phydev->advertising);
575 phydev->autoneg = AUTONEG_DISABLE;
578 return kszphy_config_init(phydev);
581 static int ksz8041_config_aneg(struct phy_device *phydev)
583 /* Skip auto-negotiation in fiber mode */
584 if (phydev->dev_flags & MICREL_PHY_FXEN) {
585 phydev->speed = SPEED_100;
589 return genphy_config_aneg(phydev);
592 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
597 if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
600 ret = phy_read(phydev, MII_BMSR);
604 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
605 * exact PHY ID. However, they can be told apart by the extended
606 * capability registers presence. The KSZ8051 PHY has them while
607 * the switch does not.
616 static int ksz8051_match_phy_device(struct phy_device *phydev)
618 return ksz8051_ksz8795_match_phy_device(phydev, true);
621 static int ksz8081_config_init(struct phy_device *phydev)
623 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
624 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
625 * pull-down is missing, the factory test mode should be cleared by
626 * manually writing a 0.
628 phy_clear_bits(phydev, MII_KSZPHY_OMSO, KSZPHY_OMSO_FACTORY_TEST);
630 return kszphy_config_init(phydev);
633 static int ksz8081_config_mdix(struct phy_device *phydev, u8 ctrl)
639 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX;
642 val = KSZ8081_CTRL2_DISABLE_AUTO_MDIX |
643 KSZ8081_CTRL2_MDI_MDI_X_SELECT;
645 case ETH_TP_MDI_AUTO:
652 return phy_modify(phydev, MII_KSZPHY_CTRL_2,
653 KSZ8081_CTRL2_HP_MDIX |
654 KSZ8081_CTRL2_MDI_MDI_X_SELECT |
655 KSZ8081_CTRL2_DISABLE_AUTO_MDIX,
656 KSZ8081_CTRL2_HP_MDIX | val);
659 static int ksz8081_config_aneg(struct phy_device *phydev)
663 ret = genphy_config_aneg(phydev);
667 /* The MDI-X configuration is automatically changed by the PHY after
668 * switching from autoneg off to on. So, take MDI-X configuration under
669 * own control and set it after autoneg configuration was done.
671 return ksz8081_config_mdix(phydev, phydev->mdix_ctrl);
674 static int ksz8081_mdix_update(struct phy_device *phydev)
678 ret = phy_read(phydev, MII_KSZPHY_CTRL_2);
682 if (ret & KSZ8081_CTRL2_DISABLE_AUTO_MDIX) {
683 if (ret & KSZ8081_CTRL2_MDI_MDI_X_SELECT)
684 phydev->mdix_ctrl = ETH_TP_MDI_X;
686 phydev->mdix_ctrl = ETH_TP_MDI;
688 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
691 ret = phy_read(phydev, MII_KSZPHY_CTRL_1);
695 if (ret & KSZ8081_CTRL1_MDIX_STAT)
696 phydev->mdix = ETH_TP_MDI;
698 phydev->mdix = ETH_TP_MDI_X;
703 static int ksz8081_read_status(struct phy_device *phydev)
707 ret = ksz8081_mdix_update(phydev);
711 return genphy_read_status(phydev);
714 static int ksz8061_config_init(struct phy_device *phydev)
718 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
722 return kszphy_config_init(phydev);
725 static int ksz8795_match_phy_device(struct phy_device *phydev)
727 return ksz8051_ksz8795_match_phy_device(phydev, false);
730 static int ksz9021_load_values_from_of(struct phy_device *phydev,
731 const struct device_node *of_node,
733 const char *field1, const char *field2,
734 const char *field3, const char *field4)
743 if (!of_property_read_u32(of_node, field1, &val1))
746 if (!of_property_read_u32(of_node, field2, &val2))
749 if (!of_property_read_u32(of_node, field3, &val3))
752 if (!of_property_read_u32(of_node, field4, &val4))
759 newval = kszphy_extended_read(phydev, reg);
764 newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
767 newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
770 newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
773 newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
775 return kszphy_extended_write(phydev, reg, newval);
778 static int ksz9021_config_init(struct phy_device *phydev)
780 const struct device_node *of_node;
781 const struct device *dev_walker;
783 /* The Micrel driver has a deprecated option to place phy OF
784 * properties in the MAC node. Walk up the tree of devices to
785 * find a device with an OF node.
787 dev_walker = &phydev->mdio.dev;
789 of_node = dev_walker->of_node;
790 dev_walker = dev_walker->parent;
792 } while (!of_node && dev_walker);
795 ksz9021_load_values_from_of(phydev, of_node,
796 MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
797 "txen-skew-ps", "txc-skew-ps",
798 "rxdv-skew-ps", "rxc-skew-ps");
799 ksz9021_load_values_from_of(phydev, of_node,
800 MII_KSZPHY_RX_DATA_PAD_SKEW,
801 "rxd0-skew-ps", "rxd1-skew-ps",
802 "rxd2-skew-ps", "rxd3-skew-ps");
803 ksz9021_load_values_from_of(phydev, of_node,
804 MII_KSZPHY_TX_DATA_PAD_SKEW,
805 "txd0-skew-ps", "txd1-skew-ps",
806 "txd2-skew-ps", "txd3-skew-ps");
811 #define KSZ9031_PS_TO_REG 60
813 /* Extended registers */
814 /* MMD Address 0x0 */
815 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3
816 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4
818 /* MMD Address 0x2 */
819 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4
820 #define MII_KSZ9031RN_RX_CTL_M GENMASK(7, 4)
821 #define MII_KSZ9031RN_TX_CTL_M GENMASK(3, 0)
823 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5
824 #define MII_KSZ9031RN_RXD3 GENMASK(15, 12)
825 #define MII_KSZ9031RN_RXD2 GENMASK(11, 8)
826 #define MII_KSZ9031RN_RXD1 GENMASK(7, 4)
827 #define MII_KSZ9031RN_RXD0 GENMASK(3, 0)
829 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6
830 #define MII_KSZ9031RN_TXD3 GENMASK(15, 12)
831 #define MII_KSZ9031RN_TXD2 GENMASK(11, 8)
832 #define MII_KSZ9031RN_TXD1 GENMASK(7, 4)
833 #define MII_KSZ9031RN_TXD0 GENMASK(3, 0)
835 #define MII_KSZ9031RN_CLK_PAD_SKEW 8
836 #define MII_KSZ9031RN_GTX_CLK GENMASK(9, 5)
837 #define MII_KSZ9031RN_RX_CLK GENMASK(4, 0)
839 /* KSZ9031 has internal RGMII_IDRX = 1.2ns and RGMII_IDTX = 0ns. To
840 * provide different RGMII options we need to configure delay offset
841 * for each pad relative to build in delay.
843 /* keep rx as "No delay adjustment" and set rx_clk to +0.60ns to get delays of
847 #define RX_CLK_ID 0x19
849 /* set rx to +0.30ns and rx_clk to -0.90ns to compensate the
850 * internal 1.2ns delay.
853 #define RX_CLK_ND 0x0
855 /* set tx to -0.42ns and tx_clk to +0.96ns to get 1.38ns delay */
857 #define TX_CLK_ID 0x1f
859 /* set tx and tx_clk to "No delay adjustment" to keep 0ns
863 #define TX_CLK_ND 0xf
865 /* MMD Address 0x1C */
866 #define MII_KSZ9031RN_EDPD 0x23
867 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0)
869 static int ksz9031_of_load_skew_values(struct phy_device *phydev,
870 const struct device_node *of_node,
871 u16 reg, size_t field_sz,
872 const char *field[], u8 numfields,
875 int val[4] = {-1, -2, -3, -4};
882 for (i = 0; i < numfields; i++)
883 if (!of_property_read_u32(of_node, field[i], val + i))
891 if (matches < numfields)
892 newval = phy_read_mmd(phydev, 2, reg);
896 maxval = (field_sz == 4) ? 0xf : 0x1f;
897 for (i = 0; i < numfields; i++)
898 if (val[i] != -(i + 1)) {
900 mask ^= maxval << (field_sz * i);
901 newval = (newval & mask) |
902 (((val[i] / KSZ9031_PS_TO_REG) & maxval)
906 return phy_write_mmd(phydev, 2, reg, newval);
909 /* Center KSZ9031RNX FLP timing at 16ms. */
910 static int ksz9031_center_flp_timing(struct phy_device *phydev)
914 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
919 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
924 return genphy_restart_aneg(phydev);
927 /* Enable energy-detect power-down mode */
928 static int ksz9031_enable_edpd(struct phy_device *phydev)
932 reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
935 return phy_write_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD,
936 reg | MII_KSZ9031RN_EDPD_ENABLE);
939 static int ksz9031_config_rgmii_delay(struct phy_device *phydev)
941 u16 rx, tx, rx_clk, tx_clk;
944 switch (phydev->interface) {
945 case PHY_INTERFACE_MODE_RGMII:
951 case PHY_INTERFACE_MODE_RGMII_ID:
957 case PHY_INTERFACE_MODE_RGMII_RXID:
963 case PHY_INTERFACE_MODE_RGMII_TXID:
973 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_CONTROL_PAD_SKEW,
974 FIELD_PREP(MII_KSZ9031RN_RX_CTL_M, rx) |
975 FIELD_PREP(MII_KSZ9031RN_TX_CTL_M, tx));
979 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_RX_DATA_PAD_SKEW,
980 FIELD_PREP(MII_KSZ9031RN_RXD3, rx) |
981 FIELD_PREP(MII_KSZ9031RN_RXD2, rx) |
982 FIELD_PREP(MII_KSZ9031RN_RXD1, rx) |
983 FIELD_PREP(MII_KSZ9031RN_RXD0, rx));
987 ret = phy_write_mmd(phydev, 2, MII_KSZ9031RN_TX_DATA_PAD_SKEW,
988 FIELD_PREP(MII_KSZ9031RN_TXD3, tx) |
989 FIELD_PREP(MII_KSZ9031RN_TXD2, tx) |
990 FIELD_PREP(MII_KSZ9031RN_TXD1, tx) |
991 FIELD_PREP(MII_KSZ9031RN_TXD0, tx));
995 return phy_write_mmd(phydev, 2, MII_KSZ9031RN_CLK_PAD_SKEW,
996 FIELD_PREP(MII_KSZ9031RN_GTX_CLK, tx_clk) |
997 FIELD_PREP(MII_KSZ9031RN_RX_CLK, rx_clk));
1000 static int ksz9031_config_init(struct phy_device *phydev)
1002 const struct device_node *of_node;
1003 static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
1004 static const char *rx_data_skews[4] = {
1005 "rxd0-skew-ps", "rxd1-skew-ps",
1006 "rxd2-skew-ps", "rxd3-skew-ps"
1008 static const char *tx_data_skews[4] = {
1009 "txd0-skew-ps", "txd1-skew-ps",
1010 "txd2-skew-ps", "txd3-skew-ps"
1012 static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
1013 const struct device *dev_walker;
1016 result = ksz9031_enable_edpd(phydev);
1020 /* The Micrel driver has a deprecated option to place phy OF
1021 * properties in the MAC node. Walk up the tree of devices to
1022 * find a device with an OF node.
1024 dev_walker = &phydev->mdio.dev;
1026 of_node = dev_walker->of_node;
1027 dev_walker = dev_walker->parent;
1028 } while (!of_node && dev_walker);
1031 bool update = false;
1033 if (phy_interface_is_rgmii(phydev)) {
1034 result = ksz9031_config_rgmii_delay(phydev);
1039 ksz9031_of_load_skew_values(phydev, of_node,
1040 MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1041 clk_skews, 2, &update);
1043 ksz9031_of_load_skew_values(phydev, of_node,
1044 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1045 control_skews, 2, &update);
1047 ksz9031_of_load_skew_values(phydev, of_node,
1048 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1049 rx_data_skews, 4, &update);
1051 ksz9031_of_load_skew_values(phydev, of_node,
1052 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1053 tx_data_skews, 4, &update);
1055 if (update && !phy_interface_is_rgmii(phydev))
1057 "*-skew-ps values should be used only with RGMII PHY modes\n");
1059 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
1060 * When the device links in the 1000BASE-T slave mode only,
1061 * the optional 125MHz reference output clock (CLK125_NDO)
1062 * has wide duty cycle variation.
1064 * The optional CLK125_NDO clock does not meet the RGMII
1065 * 45/55 percent (min/max) duty cycle requirement and therefore
1066 * cannot be used directly by the MAC side for clocking
1067 * applications that have setup/hold time requirements on
1068 * rising and falling clock edges.
1071 * Force the phy to be the master to receive a stable clock
1072 * which meets the duty cycle requirement.
1074 if (of_property_read_bool(of_node, "micrel,force-master")) {
1075 result = phy_read(phydev, MII_CTRL1000);
1077 goto err_force_master;
1079 /* enable master mode, config & prefer master */
1080 result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
1081 result = phy_write(phydev, MII_CTRL1000, result);
1083 goto err_force_master;
1087 return ksz9031_center_flp_timing(phydev);
1090 phydev_err(phydev, "failed to force the phy to master mode\n");
1094 #define KSZ9131_SKEW_5BIT_MAX 2400
1095 #define KSZ9131_SKEW_4BIT_MAX 800
1096 #define KSZ9131_OFFSET 700
1097 #define KSZ9131_STEP 100
1099 static int ksz9131_of_load_skew_values(struct phy_device *phydev,
1100 struct device_node *of_node,
1101 u16 reg, size_t field_sz,
1102 char *field[], u8 numfields)
1104 int val[4] = {-(1 + KSZ9131_OFFSET), -(2 + KSZ9131_OFFSET),
1105 -(3 + KSZ9131_OFFSET), -(4 + KSZ9131_OFFSET)};
1106 int skewval, skewmax = 0;
1113 /* psec properties in dts should mean x pico seconds */
1115 skewmax = KSZ9131_SKEW_5BIT_MAX;
1117 skewmax = KSZ9131_SKEW_4BIT_MAX;
1119 for (i = 0; i < numfields; i++)
1120 if (!of_property_read_s32(of_node, field[i], &skewval)) {
1121 if (skewval < -KSZ9131_OFFSET)
1122 skewval = -KSZ9131_OFFSET;
1123 else if (skewval > skewmax)
1126 val[i] = skewval + KSZ9131_OFFSET;
1133 if (matches < numfields)
1134 newval = phy_read_mmd(phydev, 2, reg);
1138 maxval = (field_sz == 4) ? 0xf : 0x1f;
1139 for (i = 0; i < numfields; i++)
1140 if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
1142 mask ^= maxval << (field_sz * i);
1143 newval = (newval & mask) |
1144 (((val[i] / KSZ9131_STEP) & maxval)
1148 return phy_write_mmd(phydev, 2, reg, newval);
1151 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2
1152 #define KSZ9131RN_RXC_DLL_CTRL 76
1153 #define KSZ9131RN_TXC_DLL_CTRL 77
1154 #define KSZ9131RN_DLL_CTRL_BYPASS BIT_MASK(12)
1155 #define KSZ9131RN_DLL_ENABLE_DELAY 0
1156 #define KSZ9131RN_DLL_DISABLE_DELAY BIT(12)
1158 static int ksz9131_config_rgmii_delay(struct phy_device *phydev)
1160 u16 rxcdll_val, txcdll_val;
1163 switch (phydev->interface) {
1164 case PHY_INTERFACE_MODE_RGMII:
1165 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1166 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1168 case PHY_INTERFACE_MODE_RGMII_ID:
1169 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1170 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1172 case PHY_INTERFACE_MODE_RGMII_RXID:
1173 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1174 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1176 case PHY_INTERFACE_MODE_RGMII_TXID:
1177 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
1178 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
1184 ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1185 KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1190 return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
1191 KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
1195 /* Silicon Errata DS80000693B
1197 * When LEDs are configured in Individual Mode, LED1 is ON in a no-link
1198 * condition. Workaround is to set register 0x1e, bit 9, this way LED1 behaves
1199 * according to the datasheet (off if there is no link).
1201 static int ksz9131_led_errata(struct phy_device *phydev)
1205 reg = phy_read_mmd(phydev, 2, 0);
1209 if (!(reg & BIT(4)))
1212 return phy_set_bits(phydev, 0x1e, BIT(9));
1215 static int ksz9131_config_init(struct phy_device *phydev)
1217 struct device_node *of_node;
1218 char *clk_skews[2] = {"rxc-skew-psec", "txc-skew-psec"};
1219 char *rx_data_skews[4] = {
1220 "rxd0-skew-psec", "rxd1-skew-psec",
1221 "rxd2-skew-psec", "rxd3-skew-psec"
1223 char *tx_data_skews[4] = {
1224 "txd0-skew-psec", "txd1-skew-psec",
1225 "txd2-skew-psec", "txd3-skew-psec"
1227 char *control_skews[2] = {"txen-skew-psec", "rxdv-skew-psec"};
1228 const struct device *dev_walker;
1231 dev_walker = &phydev->mdio.dev;
1233 of_node = dev_walker->of_node;
1234 dev_walker = dev_walker->parent;
1235 } while (!of_node && dev_walker);
1240 if (phy_interface_is_rgmii(phydev)) {
1241 ret = ksz9131_config_rgmii_delay(phydev);
1246 ret = ksz9131_of_load_skew_values(phydev, of_node,
1247 MII_KSZ9031RN_CLK_PAD_SKEW, 5,
1252 ret = ksz9131_of_load_skew_values(phydev, of_node,
1253 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
1258 ret = ksz9131_of_load_skew_values(phydev, of_node,
1259 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
1264 ret = ksz9131_of_load_skew_values(phydev, of_node,
1265 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
1270 ret = ksz9131_led_errata(phydev);
1277 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
1278 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6)
1279 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4)
1280 static int ksz8873mll_read_status(struct phy_device *phydev)
1285 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1287 regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
1289 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
1290 phydev->duplex = DUPLEX_HALF;
1292 phydev->duplex = DUPLEX_FULL;
1294 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
1295 phydev->speed = SPEED_10;
1297 phydev->speed = SPEED_100;
1300 phydev->pause = phydev->asym_pause = 0;
1305 static int ksz9031_get_features(struct phy_device *phydev)
1309 ret = genphy_read_abilities(phydev);
1313 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
1314 * Whenever the device's Asymmetric Pause capability is set to 1,
1315 * link-up may fail after a link-up to link-down transition.
1317 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
1320 * Do not enable the Asymmetric Pause capability bit.
1322 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1324 /* We force setting the Pause capability as the core will force the
1325 * Asymmetric Pause capability to 1 otherwise.
1327 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1332 static int ksz9031_read_status(struct phy_device *phydev)
1337 err = genphy_read_status(phydev);
1341 /* Make sure the PHY is not broken. Read idle error count,
1342 * and reset the PHY if it is maxed out.
1344 regval = phy_read(phydev, MII_STAT1000);
1345 if ((regval & 0xFF) == 0xFF) {
1346 phy_init_hw(phydev);
1348 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
1349 phydev->drv->config_intr(phydev);
1350 return genphy_config_aneg(phydev);
1356 static int ksz8873mll_config_aneg(struct phy_device *phydev)
1361 static int ksz886x_config_mdix(struct phy_device *phydev, u8 ctrl)
1367 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX;
1370 /* Note: The naming of the bit KSZ886X_BMCR_FORCE_MDI is bit
1371 * counter intuitive, the "-X" in "1 = Force MDI" in the data
1372 * sheet seems to be missing:
1373 * 1 = Force MDI (sic!) (transmit on RX+/RX- pins)
1374 * 0 = Normal operation (transmit on TX+/TX- pins)
1376 val = KSZ886X_BMCR_DISABLE_AUTO_MDIX | KSZ886X_BMCR_FORCE_MDI;
1378 case ETH_TP_MDI_AUTO:
1385 return phy_modify(phydev, MII_BMCR,
1386 KSZ886X_BMCR_HP_MDIX | KSZ886X_BMCR_FORCE_MDI |
1387 KSZ886X_BMCR_DISABLE_AUTO_MDIX,
1388 KSZ886X_BMCR_HP_MDIX | val);
1391 static int ksz886x_config_aneg(struct phy_device *phydev)
1395 ret = genphy_config_aneg(phydev);
1399 /* The MDI-X configuration is automatically changed by the PHY after
1400 * switching from autoneg off to on. So, take MDI-X configuration under
1401 * own control and set it after autoneg configuration was done.
1403 return ksz886x_config_mdix(phydev, phydev->mdix_ctrl);
1406 static int ksz886x_mdix_update(struct phy_device *phydev)
1410 ret = phy_read(phydev, MII_BMCR);
1414 if (ret & KSZ886X_BMCR_DISABLE_AUTO_MDIX) {
1415 if (ret & KSZ886X_BMCR_FORCE_MDI)
1416 phydev->mdix_ctrl = ETH_TP_MDI_X;
1418 phydev->mdix_ctrl = ETH_TP_MDI;
1420 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1423 ret = phy_read(phydev, MII_KSZPHY_CTRL);
1427 /* Same reverse logic as KSZ886X_BMCR_FORCE_MDI */
1428 if (ret & KSZ886X_CTRL_MDIX_STAT)
1429 phydev->mdix = ETH_TP_MDI_X;
1431 phydev->mdix = ETH_TP_MDI;
1436 static int ksz886x_read_status(struct phy_device *phydev)
1440 ret = ksz886x_mdix_update(phydev);
1444 return genphy_read_status(phydev);
1447 static int kszphy_get_sset_count(struct phy_device *phydev)
1449 return ARRAY_SIZE(kszphy_hw_stats);
1452 static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
1456 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
1457 strlcpy(data + i * ETH_GSTRING_LEN,
1458 kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
1462 static u64 kszphy_get_stat(struct phy_device *phydev, int i)
1464 struct kszphy_hw_stat stat = kszphy_hw_stats[i];
1465 struct kszphy_priv *priv = phydev->priv;
1469 val = phy_read(phydev, stat.reg);
1473 val = val & ((1 << stat.bits) - 1);
1474 priv->stats[i] += val;
1475 ret = priv->stats[i];
1481 static void kszphy_get_stats(struct phy_device *phydev,
1482 struct ethtool_stats *stats, u64 *data)
1486 for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
1487 data[i] = kszphy_get_stat(phydev, i);
1490 static int kszphy_suspend(struct phy_device *phydev)
1492 /* Disable PHY Interrupts */
1493 if (phy_interrupt_is_valid(phydev)) {
1494 phydev->interrupts = PHY_INTERRUPT_DISABLED;
1495 if (phydev->drv->config_intr)
1496 phydev->drv->config_intr(phydev);
1499 return genphy_suspend(phydev);
1502 static int kszphy_resume(struct phy_device *phydev)
1506 genphy_resume(phydev);
1508 /* After switching from power-down to normal mode, an internal global
1509 * reset is automatically generated. Wait a minimum of 1 ms before
1510 * read/write access to the PHY registers.
1512 usleep_range(1000, 2000);
1514 ret = kszphy_config_reset(phydev);
1518 /* Enable PHY Interrupts */
1519 if (phy_interrupt_is_valid(phydev)) {
1520 phydev->interrupts = PHY_INTERRUPT_ENABLED;
1521 if (phydev->drv->config_intr)
1522 phydev->drv->config_intr(phydev);
1528 static int kszphy_probe(struct phy_device *phydev)
1530 const struct kszphy_type *type = phydev->drv->driver_data;
1531 const struct device_node *np = phydev->mdio.dev.of_node;
1532 struct kszphy_priv *priv;
1536 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
1540 phydev->priv = priv;
1544 if (type->led_mode_reg) {
1545 ret = of_property_read_u32(np, "micrel,led-mode",
1548 priv->led_mode = -1;
1550 if (priv->led_mode > 3) {
1551 phydev_err(phydev, "invalid led mode: 0x%02x\n",
1553 priv->led_mode = -1;
1556 priv->led_mode = -1;
1559 clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
1560 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1561 if (!IS_ERR_OR_NULL(clk)) {
1562 unsigned long rate = clk_get_rate(clk);
1563 bool rmii_ref_clk_sel_25_mhz;
1565 priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
1566 rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
1567 "micrel,rmii-reference-clock-select-25-mhz");
1569 if (rate > 24500000 && rate < 25500000) {
1570 priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
1571 } else if (rate > 49500000 && rate < 50500000) {
1572 priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
1574 phydev_err(phydev, "Clock rate out of range: %ld\n",
1580 if (ksz8041_fiber_mode(phydev))
1581 phydev->port = PORT_FIBRE;
1583 /* Support legacy board-file configuration */
1584 if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
1585 priv->rmii_ref_clk_sel = true;
1586 priv->rmii_ref_clk_sel_val = true;
1592 static int ksz886x_cable_test_start(struct phy_device *phydev)
1594 if (phydev->dev_flags & MICREL_KSZ8_P1_ERRATA)
1597 /* If autoneg is enabled, we won't be able to test cross pair
1598 * short. In this case, the PHY will "detect" a link and
1599 * confuse the internal state machine - disable auto neg here.
1600 * If autoneg is disabled, we should set the speed to 10mbit.
1602 return phy_clear_bits(phydev, MII_BMCR, BMCR_ANENABLE | BMCR_SPEED100);
1605 static int ksz886x_cable_test_result_trans(u16 status)
1607 switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
1608 case KSZ8081_LMD_STAT_NORMAL:
1609 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
1610 case KSZ8081_LMD_STAT_SHORT:
1611 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
1612 case KSZ8081_LMD_STAT_OPEN:
1613 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
1614 case KSZ8081_LMD_STAT_FAIL:
1617 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
1621 static bool ksz886x_cable_test_failed(u16 status)
1623 return FIELD_GET(KSZ8081_LMD_STAT_MASK, status) ==
1624 KSZ8081_LMD_STAT_FAIL;
1627 static bool ksz886x_cable_test_fault_length_valid(u16 status)
1629 switch (FIELD_GET(KSZ8081_LMD_STAT_MASK, status)) {
1630 case KSZ8081_LMD_STAT_OPEN:
1632 case KSZ8081_LMD_STAT_SHORT:
1638 static int ksz886x_cable_test_fault_length(u16 status)
1642 /* According to the data sheet the distance to the fault is
1643 * DELTA_TIME * 0.4 meters.
1645 dt = FIELD_GET(KSZ8081_LMD_DELTA_TIME_MASK, status);
1647 return (dt * 400) / 10;
1650 static int ksz886x_cable_test_wait_for_completion(struct phy_device *phydev)
1654 ret = phy_read_poll_timeout(phydev, KSZ8081_LMD, val,
1655 !(val & KSZ8081_LMD_ENABLE_TEST),
1656 30000, 100000, true);
1658 return ret < 0 ? ret : 0;
1661 static int ksz886x_cable_test_one_pair(struct phy_device *phydev, int pair)
1663 static const int ethtool_pair[] = {
1664 ETHTOOL_A_CABLE_PAIR_A,
1665 ETHTOOL_A_CABLE_PAIR_B,
1669 /* There is no way to choice the pair, like we do one ksz9031.
1670 * We can workaround this limitation by using the MDI-X functionality.
1675 mdix = ETH_TP_MDI_X;
1677 switch (phydev->phy_id & MICREL_PHY_ID_MASK) {
1678 case PHY_ID_KSZ8081:
1679 ret = ksz8081_config_mdix(phydev, mdix);
1681 case PHY_ID_KSZ886X:
1682 ret = ksz886x_config_mdix(phydev, mdix);
1691 /* Now we are ready to fire. This command will send a 100ns pulse
1694 ret = phy_write(phydev, KSZ8081_LMD, KSZ8081_LMD_ENABLE_TEST);
1698 ret = ksz886x_cable_test_wait_for_completion(phydev);
1702 val = phy_read(phydev, KSZ8081_LMD);
1706 if (ksz886x_cable_test_failed(val))
1709 ret = ethnl_cable_test_result(phydev, ethtool_pair[pair],
1710 ksz886x_cable_test_result_trans(val));
1714 if (!ksz886x_cable_test_fault_length_valid(val))
1717 return ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
1718 ksz886x_cable_test_fault_length(val));
1721 static int ksz886x_cable_test_get_status(struct phy_device *phydev,
1724 unsigned long pair_mask = 0x3;
1730 /* Try harder if link partner is active */
1731 while (pair_mask && retries--) {
1732 for_each_set_bit(pair, &pair_mask, 4) {
1733 ret = ksz886x_cable_test_one_pair(phydev, pair);
1738 clear_bit(pair, &pair_mask);
1740 /* If link partner is in autonegotiation mode it will send 2ms
1741 * of FLPs with at least 6ms of silence.
1742 * Add 2ms sleep to have better chances to hit this silence.
1753 #define LAN_EXT_PAGE_ACCESS_CONTROL 0x16
1754 #define LAN_EXT_PAGE_ACCESS_ADDRESS_DATA 0x17
1755 #define LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC 0x4000
1757 #define LAN8814_QSGMII_SOFT_RESET 0x43
1758 #define LAN8814_QSGMII_SOFT_RESET_BIT BIT(0)
1759 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG 0x13
1760 #define LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA BIT(3)
1761 #define LAN8814_ALIGN_SWAP 0x4a
1762 #define LAN8814_ALIGN_TX_A_B_SWAP 0x1
1763 #define LAN8814_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0)
1765 #define LAN8804_ALIGN_SWAP 0x4a
1766 #define LAN8804_ALIGN_TX_A_B_SWAP 0x1
1767 #define LAN8804_ALIGN_TX_A_B_SWAP_MASK GENMASK(2, 0)
1768 #define LAN8814_CLOCK_MANAGEMENT 0xd
1769 #define LAN8814_LINK_QUALITY 0x8e
1771 static int lanphy_read_page_reg(struct phy_device *phydev, int page, u32 addr)
1775 phy_lock_mdio_bus(phydev);
1776 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
1777 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
1778 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
1779 (page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC));
1780 data = __phy_read(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA);
1781 phy_unlock_mdio_bus(phydev);
1786 static int lanphy_write_page_reg(struct phy_device *phydev, int page, u16 addr,
1789 phy_lock_mdio_bus(phydev);
1790 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL, page);
1791 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, addr);
1792 __phy_write(phydev, LAN_EXT_PAGE_ACCESS_CONTROL,
1793 page | LAN_EXT_PAGE_ACCESS_CTRL_EP_FUNC);
1795 val = __phy_write(phydev, LAN_EXT_PAGE_ACCESS_ADDRESS_DATA, val);
1797 phydev_err(phydev, "Error: phy_write has returned error %d\n",
1799 phy_unlock_mdio_bus(phydev);
1803 static int lan8814_config_ts_intr(struct phy_device *phydev, bool enable)
1808 val = PTP_TSU_INT_EN_PTP_TX_TS_EN_ |
1809 PTP_TSU_INT_EN_PTP_TX_TS_OVRFL_EN_ |
1810 PTP_TSU_INT_EN_PTP_RX_TS_EN_ |
1811 PTP_TSU_INT_EN_PTP_RX_TS_OVRFL_EN_;
1813 return lanphy_write_page_reg(phydev, 5, PTP_TSU_INT_EN, val);
1816 static void lan8814_ptp_rx_ts_get(struct phy_device *phydev,
1817 u32 *seconds, u32 *nano_seconds, u16 *seq_id)
1819 *seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_HI);
1820 *seconds = (*seconds << 16) |
1821 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_SEC_LO);
1823 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_HI);
1824 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
1825 lanphy_read_page_reg(phydev, 5, PTP_RX_INGRESS_NS_LO);
1827 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_RX_MSG_HEADER2);
1830 static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
1831 u32 *seconds, u32 *nano_seconds, u16 *seq_id)
1833 *seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_HI);
1834 *seconds = *seconds << 16 |
1835 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_SEC_LO);
1837 *nano_seconds = lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_HI);
1838 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
1839 lanphy_read_page_reg(phydev, 5, PTP_TX_EGRESS_NS_LO);
1841 *seq_id = lanphy_read_page_reg(phydev, 5, PTP_TX_MSG_HEADER2);
1844 static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct ethtool_ts_info *info)
1846 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
1847 struct phy_device *phydev = ptp_priv->phydev;
1848 struct lan8814_shared_priv *shared = phydev->shared->priv;
1850 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1851 SOF_TIMESTAMPING_RX_HARDWARE |
1852 SOF_TIMESTAMPING_RAW_HARDWARE;
1854 info->phc_index = ptp_clock_index(shared->ptp_clock);
1857 (1 << HWTSTAMP_TX_OFF) |
1858 (1 << HWTSTAMP_TX_ON) |
1859 (1 << HWTSTAMP_TX_ONESTEP_SYNC);
1862 (1 << HWTSTAMP_FILTER_NONE) |
1863 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1864 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1865 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1866 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1871 static void lan8814_flush_fifo(struct phy_device *phydev, bool egress)
1875 for (i = 0; i < FIFO_SIZE; ++i)
1876 lanphy_read_page_reg(phydev, 5,
1877 egress ? PTP_TX_MSG_HEADER2 : PTP_RX_MSG_HEADER2);
1879 /* Read to clear overflow status bit */
1880 lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
1883 static int lan8814_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
1885 struct kszphy_ptp_priv *ptp_priv =
1886 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
1887 struct phy_device *phydev = ptp_priv->phydev;
1888 struct lan8814_shared_priv *shared = phydev->shared->priv;
1889 struct lan8814_ptp_rx_ts *rx_ts, *tmp;
1890 struct hwtstamp_config config;
1891 int txcfg = 0, rxcfg = 0;
1894 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1897 ptp_priv->hwts_tx_type = config.tx_type;
1898 ptp_priv->rx_filter = config.rx_filter;
1900 switch (config.rx_filter) {
1901 case HWTSTAMP_FILTER_NONE:
1902 ptp_priv->layer = 0;
1903 ptp_priv->version = 0;
1905 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1906 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1907 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1908 ptp_priv->layer = PTP_CLASS_L4;
1909 ptp_priv->version = PTP_CLASS_V2;
1911 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1912 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1913 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1914 ptp_priv->layer = PTP_CLASS_L2;
1915 ptp_priv->version = PTP_CLASS_V2;
1917 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1918 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1919 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1920 ptp_priv->layer = PTP_CLASS_L4 | PTP_CLASS_L2;
1921 ptp_priv->version = PTP_CLASS_V2;
1927 if (ptp_priv->layer & PTP_CLASS_L2) {
1928 rxcfg = PTP_RX_PARSE_CONFIG_LAYER2_EN_;
1929 txcfg = PTP_TX_PARSE_CONFIG_LAYER2_EN_;
1930 } else if (ptp_priv->layer & PTP_CLASS_L4) {
1931 rxcfg |= PTP_RX_PARSE_CONFIG_IPV4_EN_ | PTP_RX_PARSE_CONFIG_IPV6_EN_;
1932 txcfg |= PTP_TX_PARSE_CONFIG_IPV4_EN_ | PTP_TX_PARSE_CONFIG_IPV6_EN_;
1934 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_PARSE_CONFIG, rxcfg);
1935 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_PARSE_CONFIG, txcfg);
1937 pkt_ts_enable = PTP_TIMESTAMP_EN_SYNC_ | PTP_TIMESTAMP_EN_DREQ_ |
1938 PTP_TIMESTAMP_EN_PDREQ_ | PTP_TIMESTAMP_EN_PDRES_;
1939 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_RX_TIMESTAMP_EN, pkt_ts_enable);
1940 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_TIMESTAMP_EN, pkt_ts_enable);
1942 if (ptp_priv->hwts_tx_type == HWTSTAMP_TX_ONESTEP_SYNC)
1943 lanphy_write_page_reg(ptp_priv->phydev, 5, PTP_TX_MOD,
1944 PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_);
1946 if (config.rx_filter != HWTSTAMP_FILTER_NONE)
1947 lan8814_config_ts_intr(ptp_priv->phydev, true);
1949 lan8814_config_ts_intr(ptp_priv->phydev, false);
1951 mutex_lock(&shared->shared_lock);
1952 if (config.rx_filter != HWTSTAMP_FILTER_NONE)
1958 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
1959 PTP_CMD_CTL_PTP_ENABLE_);
1961 lanphy_write_page_reg(ptp_priv->phydev, 4, PTP_CMD_CTL,
1962 PTP_CMD_CTL_PTP_DISABLE_);
1963 mutex_unlock(&shared->shared_lock);
1965 /* In case of multiple starts and stops, these needs to be cleared */
1966 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
1967 list_del(&rx_ts->list);
1970 skb_queue_purge(&ptp_priv->rx_queue);
1971 skb_queue_purge(&ptp_priv->tx_queue);
1973 lan8814_flush_fifo(ptp_priv->phydev, false);
1974 lan8814_flush_fifo(ptp_priv->phydev, true);
1976 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
1979 static void lan8814_txtstamp(struct mii_timestamper *mii_ts,
1980 struct sk_buff *skb, int type)
1982 struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
1984 switch (ptp_priv->hwts_tx_type) {
1985 case HWTSTAMP_TX_ONESTEP_SYNC:
1986 if (ptp_msg_is_sync(skb, type)) {
1991 case HWTSTAMP_TX_ON:
1992 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1993 skb_queue_tail(&ptp_priv->tx_queue, skb);
1995 case HWTSTAMP_TX_OFF:
2002 static void lan8814_get_sig_rx(struct sk_buff *skb, u16 *sig)
2004 struct ptp_header *ptp_header;
2007 skb_push(skb, ETH_HLEN);
2008 type = ptp_classify_raw(skb);
2009 ptp_header = ptp_parse_header(skb, type);
2010 skb_pull_inline(skb, ETH_HLEN);
2012 *sig = (__force u16)(ntohs(ptp_header->sequence_id));
2015 static bool lan8814_match_rx_ts(struct kszphy_ptp_priv *ptp_priv,
2016 struct sk_buff *skb)
2018 struct skb_shared_hwtstamps *shhwtstamps;
2019 struct lan8814_ptp_rx_ts *rx_ts, *tmp;
2020 unsigned long flags;
2024 lan8814_get_sig_rx(skb, &skb_sig);
2026 /* Iterate over all RX timestamps and match it with the received skbs */
2027 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2028 list_for_each_entry_safe(rx_ts, tmp, &ptp_priv->rx_ts_list, list) {
2029 /* Check if we found the signature we were looking for. */
2030 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2033 shhwtstamps = skb_hwtstamps(skb);
2034 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2035 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds,
2037 list_del(&rx_ts->list);
2043 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2050 static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb, int type)
2052 struct kszphy_ptp_priv *ptp_priv =
2053 container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
2055 if (ptp_priv->rx_filter == HWTSTAMP_FILTER_NONE ||
2056 type == PTP_CLASS_NONE)
2059 if ((type & ptp_priv->version) == 0 || (type & ptp_priv->layer) == 0)
2062 /* If we failed to match then add it to the queue for when the timestamp
2065 if (!lan8814_match_rx_ts(ptp_priv, skb))
2066 skb_queue_tail(&ptp_priv->rx_queue, skb);
2071 static void lan8814_ptp_clock_set(struct phy_device *phydev,
2072 u32 seconds, u32 nano_seconds)
2074 u32 sec_low, sec_high, nsec_low, nsec_high;
2076 sec_low = seconds & 0xffff;
2077 sec_high = (seconds >> 16) & 0xffff;
2078 nsec_low = nano_seconds & 0xffff;
2079 nsec_high = (nano_seconds >> 16) & 0x3fff;
2081 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
2082 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
2083 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
2084 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
2086 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
2089 static void lan8814_ptp_clock_get(struct phy_device *phydev,
2090 u32 *seconds, u32 *nano_seconds)
2092 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
2094 *seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
2095 *seconds = (*seconds << 16) |
2096 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
2098 *nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
2099 *nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
2100 lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
2103 static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
2104 struct timespec64 *ts)
2106 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2108 struct phy_device *phydev = shared->phydev;
2112 mutex_lock(&shared->shared_lock);
2113 lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
2114 mutex_unlock(&shared->shared_lock);
2115 ts->tv_sec = seconds;
2116 ts->tv_nsec = nano_seconds;
2121 static int lan8814_ptpci_settime64(struct ptp_clock_info *ptpci,
2122 const struct timespec64 *ts)
2124 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2126 struct phy_device *phydev = shared->phydev;
2128 mutex_lock(&shared->shared_lock);
2129 lan8814_ptp_clock_set(phydev, ts->tv_sec, ts->tv_nsec);
2130 mutex_unlock(&shared->shared_lock);
2135 static void lan8814_ptp_clock_step(struct phy_device *phydev,
2138 u32 nano_seconds_step;
2139 u64 abs_time_step_ns;
2140 u32 unsigned_seconds;
2145 if (time_step_ns > 15000000000LL) {
2146 /* convert to clock set */
2147 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2148 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
2150 nano_seconds += remainder;
2151 if (nano_seconds >= 1000000000) {
2153 nano_seconds -= 1000000000;
2155 lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
2157 } else if (time_step_ns < -15000000000LL) {
2158 /* convert to clock set */
2159 time_step_ns = -time_step_ns;
2161 lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
2162 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
2164 nano_seconds_step = remainder;
2165 if (nano_seconds < nano_seconds_step) {
2167 nano_seconds += 1000000000;
2169 nano_seconds -= nano_seconds_step;
2170 lan8814_ptp_clock_set(phydev, unsigned_seconds,
2176 if (time_step_ns >= 0) {
2177 abs_time_step_ns = (u64)time_step_ns;
2178 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
2180 nano_seconds = remainder;
2182 abs_time_step_ns = (u64)(-time_step_ns);
2183 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
2185 nano_seconds = remainder;
2186 if (nano_seconds > 0) {
2187 /* subtracting nano seconds is not allowed
2188 * convert to subtracting from seconds,
2189 * and adding to nanoseconds
2192 nano_seconds = (1000000000 - nano_seconds);
2196 if (nano_seconds > 0) {
2197 /* add 8 ns to cover the likely normal increment */
2201 if (nano_seconds >= 1000000000) {
2202 /* carry into seconds */
2204 nano_seconds -= 1000000000;
2209 u32 adjustment_value = (u32)seconds;
2210 u16 adjustment_value_lo, adjustment_value_hi;
2212 if (adjustment_value > 0xF)
2213 adjustment_value = 0xF;
2215 adjustment_value_lo = adjustment_value & 0xffff;
2216 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2218 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2219 adjustment_value_lo);
2220 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2221 PTP_LTC_STEP_ADJ_DIR_ |
2222 adjustment_value_hi);
2223 seconds -= ((s32)adjustment_value);
2225 u32 adjustment_value = (u32)(-seconds);
2226 u16 adjustment_value_lo, adjustment_value_hi;
2228 if (adjustment_value > 0xF)
2229 adjustment_value = 0xF;
2231 adjustment_value_lo = adjustment_value & 0xffff;
2232 adjustment_value_hi = (adjustment_value >> 16) & 0x3fff;
2234 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2235 adjustment_value_lo);
2236 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2237 adjustment_value_hi);
2238 seconds += ((s32)adjustment_value);
2240 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2241 PTP_CMD_CTL_PTP_LTC_STEP_SEC_);
2244 u16 nano_seconds_lo;
2245 u16 nano_seconds_hi;
2247 nano_seconds_lo = nano_seconds & 0xffff;
2248 nano_seconds_hi = (nano_seconds >> 16) & 0x3fff;
2250 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_LO,
2252 lanphy_write_page_reg(phydev, 4, PTP_LTC_STEP_ADJ_HI,
2253 PTP_LTC_STEP_ADJ_DIR_ |
2255 lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL,
2256 PTP_CMD_CTL_PTP_LTC_STEP_NSEC_);
2260 static int lan8814_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
2262 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2264 struct phy_device *phydev = shared->phydev;
2266 mutex_lock(&shared->shared_lock);
2267 lan8814_ptp_clock_step(phydev, delta);
2268 mutex_unlock(&shared->shared_lock);
2273 static int lan8814_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
2275 struct lan8814_shared_priv *shared = container_of(ptpci, struct lan8814_shared_priv,
2277 struct phy_device *phydev = shared->phydev;
2278 u16 kszphy_rate_adj_lo, kszphy_rate_adj_hi;
2279 bool positive = true;
2280 u32 kszphy_rate_adj;
2282 if (scaled_ppm < 0) {
2283 scaled_ppm = -scaled_ppm;
2287 kszphy_rate_adj = LAN8814_1PPM_FORMAT * (scaled_ppm >> 16);
2288 kszphy_rate_adj += (LAN8814_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
2290 kszphy_rate_adj_lo = kszphy_rate_adj & 0xffff;
2291 kszphy_rate_adj_hi = (kszphy_rate_adj >> 16) & 0x3fff;
2294 kszphy_rate_adj_hi |= PTP_CLOCK_RATE_ADJ_DIR_;
2296 mutex_lock(&shared->shared_lock);
2297 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_HI, kszphy_rate_adj_hi);
2298 lanphy_write_page_reg(phydev, 4, PTP_CLOCK_RATE_ADJ_LO, kszphy_rate_adj_lo);
2299 mutex_unlock(&shared->shared_lock);
2304 static void lan8814_get_sig_tx(struct sk_buff *skb, u16 *sig)
2306 struct ptp_header *ptp_header;
2309 type = ptp_classify_raw(skb);
2310 ptp_header = ptp_parse_header(skb, type);
2312 *sig = (__force u16)(ntohs(ptp_header->sequence_id));
2315 static void lan8814_dequeue_tx_skb(struct kszphy_ptp_priv *ptp_priv)
2317 struct phy_device *phydev = ptp_priv->phydev;
2318 struct skb_shared_hwtstamps shhwtstamps;
2319 struct sk_buff *skb, *skb_tmp;
2320 unsigned long flags;
2326 lan8814_ptp_tx_ts_get(phydev, &seconds, &nsec, &seq_id);
2328 spin_lock_irqsave(&ptp_priv->tx_queue.lock, flags);
2329 skb_queue_walk_safe(&ptp_priv->tx_queue, skb, skb_tmp) {
2330 lan8814_get_sig_tx(skb, &skb_sig);
2332 if (memcmp(&skb_sig, &seq_id, sizeof(seq_id)))
2335 __skb_unlink(skb, &ptp_priv->tx_queue);
2339 spin_unlock_irqrestore(&ptp_priv->tx_queue.lock, flags);
2342 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2343 shhwtstamps.hwtstamp = ktime_set(seconds, nsec);
2344 skb_complete_tx_timestamp(skb, &shhwtstamps);
2348 static void lan8814_get_tx_ts(struct kszphy_ptp_priv *ptp_priv)
2350 struct phy_device *phydev = ptp_priv->phydev;
2354 lan8814_dequeue_tx_skb(ptp_priv);
2356 /* If other timestamps are available in the FIFO,
2359 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2360 } while (PTP_CAP_INFO_TX_TS_CNT_GET_(reg) > 0);
2363 static bool lan8814_match_skb(struct kszphy_ptp_priv *ptp_priv,
2364 struct lan8814_ptp_rx_ts *rx_ts)
2366 struct skb_shared_hwtstamps *shhwtstamps;
2367 struct sk_buff *skb, *skb_tmp;
2368 unsigned long flags;
2372 spin_lock_irqsave(&ptp_priv->rx_queue.lock, flags);
2373 skb_queue_walk_safe(&ptp_priv->rx_queue, skb, skb_tmp) {
2374 lan8814_get_sig_rx(skb, &skb_sig);
2376 if (memcmp(&skb_sig, &rx_ts->seq_id, sizeof(rx_ts->seq_id)))
2379 __skb_unlink(skb, &ptp_priv->rx_queue);
2384 spin_unlock_irqrestore(&ptp_priv->rx_queue.lock, flags);
2387 shhwtstamps = skb_hwtstamps(skb);
2388 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2389 shhwtstamps->hwtstamp = ktime_set(rx_ts->seconds, rx_ts->nsec);
2396 static void lan8814_get_rx_ts(struct kszphy_ptp_priv *ptp_priv)
2398 struct phy_device *phydev = ptp_priv->phydev;
2399 struct lan8814_ptp_rx_ts *rx_ts;
2400 unsigned long flags;
2404 rx_ts = kzalloc(sizeof(*rx_ts), GFP_KERNEL);
2408 lan8814_ptp_rx_ts_get(phydev, &rx_ts->seconds, &rx_ts->nsec,
2411 /* If we failed to match the skb add it to the queue for when
2412 * the frame will come
2414 if (!lan8814_match_skb(ptp_priv, rx_ts)) {
2415 spin_lock_irqsave(&ptp_priv->rx_ts_lock, flags);
2416 list_add(&rx_ts->list, &ptp_priv->rx_ts_list);
2417 spin_unlock_irqrestore(&ptp_priv->rx_ts_lock, flags);
2422 /* If other timestamps are available in the FIFO,
2425 reg = lanphy_read_page_reg(phydev, 5, PTP_CAP_INFO);
2426 } while (PTP_CAP_INFO_RX_TS_CNT_GET_(reg) > 0);
2429 static void lan8814_handle_ptp_interrupt(struct phy_device *phydev)
2431 struct kszphy_priv *priv = phydev->priv;
2432 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2435 status = lanphy_read_page_reg(phydev, 5, PTP_TSU_INT_STS);
2436 if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
2437 lan8814_get_tx_ts(ptp_priv);
2439 if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
2440 lan8814_get_rx_ts(ptp_priv);
2442 if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
2443 lan8814_flush_fifo(phydev, true);
2444 skb_queue_purge(&ptp_priv->tx_queue);
2447 if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
2448 lan8814_flush_fifo(phydev, false);
2449 skb_queue_purge(&ptp_priv->rx_queue);
2453 static int lan8804_config_init(struct phy_device *phydev)
2457 /* MDI-X setting for swap A,B transmit */
2458 val = lanphy_read_page_reg(phydev, 2, LAN8804_ALIGN_SWAP);
2459 val &= ~LAN8804_ALIGN_TX_A_B_SWAP_MASK;
2460 val |= LAN8804_ALIGN_TX_A_B_SWAP;
2461 lanphy_write_page_reg(phydev, 2, LAN8804_ALIGN_SWAP, val);
2463 /* Make sure that the PHY will not stop generating the clock when the
2464 * link partner goes down
2466 lanphy_write_page_reg(phydev, 31, LAN8814_CLOCK_MANAGEMENT, 0x27e);
2467 lanphy_read_page_reg(phydev, 1, LAN8814_LINK_QUALITY);
2472 static irqreturn_t lan8814_handle_interrupt(struct phy_device *phydev)
2477 irq_status = phy_read(phydev, LAN8814_INTS);
2478 if (irq_status > 0 && (irq_status & LAN8814_INT_LINK))
2479 phy_trigger_machine(phydev);
2481 if (irq_status < 0) {
2487 tsu_irq_status = lanphy_read_page_reg(phydev, 4,
2488 LAN8814_INTR_STS_REG);
2490 if (tsu_irq_status > 0 &&
2491 (tsu_irq_status & (LAN8814_INTR_STS_REG_1588_TSU0_ |
2492 LAN8814_INTR_STS_REG_1588_TSU1_ |
2493 LAN8814_INTR_STS_REG_1588_TSU2_ |
2494 LAN8814_INTR_STS_REG_1588_TSU3_)))
2495 lan8814_handle_ptp_interrupt(phydev);
2502 static int lan8814_ack_interrupt(struct phy_device *phydev)
2504 /* bit[12..0] int status, which is a read and clear register. */
2507 rc = phy_read(phydev, LAN8814_INTS);
2509 return (rc < 0) ? rc : 0;
2512 static int lan8814_config_intr(struct phy_device *phydev)
2516 lanphy_write_page_reg(phydev, 4, LAN8814_INTR_CTRL_REG,
2517 LAN8814_INTR_CTRL_REG_POLARITY |
2518 LAN8814_INTR_CTRL_REG_INTR_ENABLE);
2520 /* enable / disable interrupts */
2521 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
2522 err = lan8814_ack_interrupt(phydev);
2526 err = phy_write(phydev, LAN8814_INTC, LAN8814_INT_LINK);
2528 err = phy_write(phydev, LAN8814_INTC, 0);
2532 err = lan8814_ack_interrupt(phydev);
2538 static void lan8814_ptp_init(struct phy_device *phydev)
2540 struct kszphy_priv *priv = phydev->priv;
2541 struct kszphy_ptp_priv *ptp_priv = &priv->ptp_priv;
2544 lanphy_write_page_reg(phydev, 5, TSU_HARD_RESET, TSU_HARD_RESET_);
2546 temp = lanphy_read_page_reg(phydev, 5, PTP_TX_MOD);
2547 temp |= PTP_TX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2548 lanphy_write_page_reg(phydev, 5, PTP_TX_MOD, temp);
2550 temp = lanphy_read_page_reg(phydev, 5, PTP_RX_MOD);
2551 temp |= PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_;
2552 lanphy_write_page_reg(phydev, 5, PTP_RX_MOD, temp);
2554 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_CONFIG, 0);
2555 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_CONFIG, 0);
2557 /* Removing default registers configs related to L2 and IP */
2558 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_L2_ADDR_EN, 0);
2559 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_L2_ADDR_EN, 0);
2560 lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
2561 lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
2563 skb_queue_head_init(&ptp_priv->tx_queue);
2564 skb_queue_head_init(&ptp_priv->rx_queue);
2565 INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
2566 spin_lock_init(&ptp_priv->rx_ts_lock);
2568 ptp_priv->phydev = phydev;
2570 ptp_priv->mii_ts.rxtstamp = lan8814_rxtstamp;
2571 ptp_priv->mii_ts.txtstamp = lan8814_txtstamp;
2572 ptp_priv->mii_ts.hwtstamp = lan8814_hwtstamp;
2573 ptp_priv->mii_ts.ts_info = lan8814_ts_info;
2575 phydev->mii_ts = &ptp_priv->mii_ts;
2578 static int lan8814_ptp_probe_once(struct phy_device *phydev)
2580 struct lan8814_shared_priv *shared = phydev->shared->priv;
2582 /* Initialise shared lock for clock*/
2583 mutex_init(&shared->shared_lock);
2585 shared->ptp_clock_info.owner = THIS_MODULE;
2586 snprintf(shared->ptp_clock_info.name, 30, "%s", phydev->drv->name);
2587 shared->ptp_clock_info.max_adj = 31249999;
2588 shared->ptp_clock_info.n_alarm = 0;
2589 shared->ptp_clock_info.n_ext_ts = 0;
2590 shared->ptp_clock_info.n_pins = 0;
2591 shared->ptp_clock_info.pps = 0;
2592 shared->ptp_clock_info.pin_config = NULL;
2593 shared->ptp_clock_info.adjfine = lan8814_ptpci_adjfine;
2594 shared->ptp_clock_info.adjtime = lan8814_ptpci_adjtime;
2595 shared->ptp_clock_info.gettime64 = lan8814_ptpci_gettime64;
2596 shared->ptp_clock_info.settime64 = lan8814_ptpci_settime64;
2597 shared->ptp_clock_info.getcrosststamp = NULL;
2599 shared->ptp_clock = ptp_clock_register(&shared->ptp_clock_info,
2601 if (IS_ERR_OR_NULL(shared->ptp_clock)) {
2602 phydev_err(phydev, "ptp_clock_register failed %lu\n",
2603 PTR_ERR(shared->ptp_clock));
2607 phydev_dbg(phydev, "successfully registered ptp clock\n");
2609 shared->phydev = phydev;
2611 /* The EP.4 is shared between all the PHYs in the package and also it
2612 * can be accessed by any of the PHYs
2614 lanphy_write_page_reg(phydev, 4, LTC_HARD_RESET, LTC_HARD_RESET_);
2615 lanphy_write_page_reg(phydev, 4, PTP_OPERATING_MODE,
2616 PTP_OPERATING_MODE_STANDALONE_);
2621 static int lan8814_read_status(struct phy_device *phydev)
2623 struct kszphy_priv *priv = phydev->priv;
2624 struct kszphy_latencies *latencies = &priv->latencies;
2628 err = genphy_read_status(phydev);
2632 switch (phydev->speed) {
2634 lanphy_write_page_reg(phydev, 5, PTP_RX_LATENCY_1000,
2635 latencies->rx_1000);
2636 lanphy_write_page_reg(phydev, 5, PTP_TX_LATENCY_1000,
2637 latencies->tx_1000);
2640 lanphy_write_page_reg(phydev, 5, PTP_RX_LATENCY_100,
2642 lanphy_write_page_reg(phydev, 5, PTP_TX_LATENCY_100,
2646 lanphy_write_page_reg(phydev, 5, PTP_RX_LATENCY_10,
2648 lanphy_write_page_reg(phydev, 5, PTP_TX_LATENCY_10,
2655 /* Make sure the PHY is not broken. Read idle error count,
2656 * and reset the PHY if it is maxed out.
2658 regval = phy_read(phydev, MII_STAT1000);
2659 if ((regval & 0xFF) == 0xFF) {
2660 phy_init_hw(phydev);
2662 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
2663 phydev->drv->config_intr(phydev);
2664 return genphy_config_aneg(phydev);
2670 static int lan8814_config_init(struct phy_device *phydev)
2675 val = lanphy_read_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET);
2676 val |= LAN8814_QSGMII_SOFT_RESET_BIT;
2677 lanphy_write_page_reg(phydev, 4, LAN8814_QSGMII_SOFT_RESET, val);
2679 /* Disable ANEG with QSGMII PCS Host side */
2680 val = lanphy_read_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG);
2681 val &= ~LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA;
2682 lanphy_write_page_reg(phydev, 5, LAN8814_QSGMII_PCS1G_ANEG_CONFIG, val);
2684 /* MDI-X setting for swap A,B transmit */
2685 val = lanphy_read_page_reg(phydev, 2, LAN8814_ALIGN_SWAP);
2686 val &= ~LAN8814_ALIGN_TX_A_B_SWAP_MASK;
2687 val |= LAN8814_ALIGN_TX_A_B_SWAP;
2688 lanphy_write_page_reg(phydev, 2, LAN8814_ALIGN_SWAP, val);
2693 static void lan8814_parse_latency(struct phy_device *phydev)
2695 const struct device_node *np = phydev->mdio.dev.of_node;
2696 struct kszphy_priv *priv = phydev->priv;
2697 struct kszphy_latencies *latency = &priv->latencies;
2700 if (!of_property_read_u32(np, "lan8814,latency_rx_10", &val))
2701 latency->rx_10 = val;
2702 if (!of_property_read_u32(np, "lan8814,latency_tx_10", &val))
2703 latency->tx_10 = val;
2704 if (!of_property_read_u32(np, "lan8814,latency_rx_100", &val))
2705 latency->rx_100 = val;
2706 if (!of_property_read_u32(np, "lan8814,latency_tx_100", &val))
2707 latency->tx_100 = val;
2708 if (!of_property_read_u32(np, "lan8814,latency_rx_1000", &val))
2709 latency->rx_1000 = val;
2710 if (!of_property_read_u32(np, "lan8814,latency_tx_1000", &val))
2711 latency->tx_1000 = val;
2714 static int lan8814_probe(struct phy_device *phydev)
2716 const struct device_node *np = phydev->mdio.dev.of_node;
2717 struct kszphy_priv *priv;
2721 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2725 priv->led_mode = -1;
2727 priv->latencies = lan8814_latencies;
2729 phydev->priv = priv;
2731 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK) ||
2732 !IS_ENABLED(CONFIG_NETWORK_PHY_TIMESTAMPING) ||
2733 of_property_read_bool(np, "lan8814,ignore-ts"))
2736 /* Strap-in value for PHY address, below register read gives starting
2739 addr = lanphy_read_page_reg(phydev, 4, 0) & 0x1F;
2740 devm_phy_package_join(&phydev->mdio.dev, phydev,
2741 addr, sizeof(struct lan8814_shared_priv));
2743 if (phy_package_init_once(phydev)) {
2744 err = lan8814_ptp_probe_once(phydev);
2749 lan8814_parse_latency(phydev);
2750 lan8814_ptp_init(phydev);
2755 static struct phy_driver ksphy_driver[] = {
2757 .phy_id = PHY_ID_KS8737,
2758 .phy_id_mask = MICREL_PHY_ID_MASK,
2759 .name = "Micrel KS8737",
2760 /* PHY_BASIC_FEATURES */
2761 .driver_data = &ks8737_type,
2762 .config_init = kszphy_config_init,
2763 .config_intr = kszphy_config_intr,
2764 .handle_interrupt = kszphy_handle_interrupt,
2765 .suspend = kszphy_suspend,
2766 .resume = kszphy_resume,
2768 .phy_id = PHY_ID_KSZ8021,
2769 .phy_id_mask = 0x00ffffff,
2770 .name = "Micrel KSZ8021 or KSZ8031",
2771 /* PHY_BASIC_FEATURES */
2772 .driver_data = &ksz8021_type,
2773 .probe = kszphy_probe,
2774 .config_init = kszphy_config_init,
2775 .config_intr = kszphy_config_intr,
2776 .handle_interrupt = kszphy_handle_interrupt,
2777 .get_sset_count = kszphy_get_sset_count,
2778 .get_strings = kszphy_get_strings,
2779 .get_stats = kszphy_get_stats,
2780 .suspend = kszphy_suspend,
2781 .resume = kszphy_resume,
2783 .phy_id = PHY_ID_KSZ8031,
2784 .phy_id_mask = 0x00ffffff,
2785 .name = "Micrel KSZ8031",
2786 /* PHY_BASIC_FEATURES */
2787 .driver_data = &ksz8021_type,
2788 .probe = kszphy_probe,
2789 .config_init = kszphy_config_init,
2790 .config_intr = kszphy_config_intr,
2791 .handle_interrupt = kszphy_handle_interrupt,
2792 .get_sset_count = kszphy_get_sset_count,
2793 .get_strings = kszphy_get_strings,
2794 .get_stats = kszphy_get_stats,
2795 .suspend = kszphy_suspend,
2796 .resume = kszphy_resume,
2798 .phy_id = PHY_ID_KSZ8041,
2799 .phy_id_mask = MICREL_PHY_ID_MASK,
2800 .name = "Micrel KSZ8041",
2801 /* PHY_BASIC_FEATURES */
2802 .driver_data = &ksz8041_type,
2803 .probe = kszphy_probe,
2804 .config_init = ksz8041_config_init,
2805 .config_aneg = ksz8041_config_aneg,
2806 .config_intr = kszphy_config_intr,
2807 .handle_interrupt = kszphy_handle_interrupt,
2808 .get_sset_count = kszphy_get_sset_count,
2809 .get_strings = kszphy_get_strings,
2810 .get_stats = kszphy_get_stats,
2811 /* No suspend/resume callbacks because of errata DS80000700A,
2812 * receiver error following software power down.
2815 .phy_id = PHY_ID_KSZ8041RNLI,
2816 .phy_id_mask = MICREL_PHY_ID_MASK,
2817 .name = "Micrel KSZ8041RNLI",
2818 /* PHY_BASIC_FEATURES */
2819 .driver_data = &ksz8041_type,
2820 .probe = kszphy_probe,
2821 .config_init = kszphy_config_init,
2822 .config_intr = kszphy_config_intr,
2823 .handle_interrupt = kszphy_handle_interrupt,
2824 .get_sset_count = kszphy_get_sset_count,
2825 .get_strings = kszphy_get_strings,
2826 .get_stats = kszphy_get_stats,
2827 .suspend = kszphy_suspend,
2828 .resume = kszphy_resume,
2830 .name = "Micrel KSZ8051",
2831 /* PHY_BASIC_FEATURES */
2832 .driver_data = &ksz8051_type,
2833 .probe = kszphy_probe,
2834 .config_init = kszphy_config_init,
2835 .config_intr = kszphy_config_intr,
2836 .handle_interrupt = kszphy_handle_interrupt,
2837 .get_sset_count = kszphy_get_sset_count,
2838 .get_strings = kszphy_get_strings,
2839 .get_stats = kszphy_get_stats,
2840 .match_phy_device = ksz8051_match_phy_device,
2841 .suspend = kszphy_suspend,
2842 .resume = kszphy_resume,
2844 .phy_id = PHY_ID_KSZ8001,
2845 .name = "Micrel KSZ8001 or KS8721",
2846 .phy_id_mask = 0x00fffffc,
2847 /* PHY_BASIC_FEATURES */
2848 .driver_data = &ksz8041_type,
2849 .probe = kszphy_probe,
2850 .config_init = kszphy_config_init,
2851 .config_intr = kszphy_config_intr,
2852 .handle_interrupt = kszphy_handle_interrupt,
2853 .get_sset_count = kszphy_get_sset_count,
2854 .get_strings = kszphy_get_strings,
2855 .get_stats = kszphy_get_stats,
2856 .suspend = kszphy_suspend,
2857 .resume = kszphy_resume,
2859 .phy_id = PHY_ID_KSZ8081,
2860 .name = "Micrel KSZ8081 or KSZ8091",
2861 .phy_id_mask = MICREL_PHY_ID_MASK,
2862 .flags = PHY_POLL_CABLE_TEST,
2863 /* PHY_BASIC_FEATURES */
2864 .driver_data = &ksz8081_type,
2865 .probe = kszphy_probe,
2866 .config_init = ksz8081_config_init,
2867 .soft_reset = genphy_soft_reset,
2868 .config_aneg = ksz8081_config_aneg,
2869 .read_status = ksz8081_read_status,
2870 .config_intr = kszphy_config_intr,
2871 .handle_interrupt = kszphy_handle_interrupt,
2872 .get_sset_count = kszphy_get_sset_count,
2873 .get_strings = kszphy_get_strings,
2874 .get_stats = kszphy_get_stats,
2875 .suspend = kszphy_suspend,
2876 .resume = kszphy_resume,
2877 .cable_test_start = ksz886x_cable_test_start,
2878 .cable_test_get_status = ksz886x_cable_test_get_status,
2880 .phy_id = PHY_ID_KSZ8061,
2881 .name = "Micrel KSZ8061",
2882 .phy_id_mask = MICREL_PHY_ID_MASK,
2883 /* PHY_BASIC_FEATURES */
2884 .config_init = ksz8061_config_init,
2885 .config_intr = kszphy_config_intr,
2886 .handle_interrupt = kszphy_handle_interrupt,
2887 .suspend = kszphy_suspend,
2888 .resume = kszphy_resume,
2890 .phy_id = PHY_ID_KSZ9021,
2891 .phy_id_mask = 0x000ffffe,
2892 .name = "Micrel KSZ9021 Gigabit PHY",
2893 /* PHY_GBIT_FEATURES */
2894 .driver_data = &ksz9021_type,
2895 .probe = kszphy_probe,
2896 .get_features = ksz9031_get_features,
2897 .config_init = ksz9021_config_init,
2898 .config_intr = kszphy_config_intr,
2899 .handle_interrupt = kszphy_handle_interrupt,
2900 .get_sset_count = kszphy_get_sset_count,
2901 .get_strings = kszphy_get_strings,
2902 .get_stats = kszphy_get_stats,
2903 .suspend = kszphy_suspend,
2904 .resume = kszphy_resume,
2905 .read_mmd = genphy_read_mmd_unsupported,
2906 .write_mmd = genphy_write_mmd_unsupported,
2908 .phy_id = PHY_ID_KSZ9031,
2909 .phy_id_mask = MICREL_PHY_ID_MASK,
2910 .name = "Micrel KSZ9031 Gigabit PHY",
2911 .driver_data = &ksz9021_type,
2912 .probe = kszphy_probe,
2913 .get_features = ksz9031_get_features,
2914 .config_init = ksz9031_config_init,
2915 .soft_reset = genphy_soft_reset,
2916 .read_status = ksz9031_read_status,
2917 .config_intr = kszphy_config_intr,
2918 .handle_interrupt = kszphy_handle_interrupt,
2919 .get_sset_count = kszphy_get_sset_count,
2920 .get_strings = kszphy_get_strings,
2921 .get_stats = kszphy_get_stats,
2922 .suspend = kszphy_suspend,
2923 .resume = kszphy_resume,
2925 .phy_id = PHY_ID_LAN8814,
2926 .phy_id_mask = MICREL_PHY_ID_MASK,
2927 .name = "Microchip INDY Gigabit Quad PHY",
2928 .config_init = lan8814_config_init,
2929 .probe = lan8814_probe,
2930 .soft_reset = genphy_soft_reset,
2931 .read_status = lan8814_read_status,
2932 .get_sset_count = kszphy_get_sset_count,
2933 .get_strings = kszphy_get_strings,
2934 .get_stats = kszphy_get_stats,
2935 .suspend = genphy_suspend,
2936 .resume = kszphy_resume,
2937 .config_intr = lan8814_config_intr,
2938 .handle_interrupt = lan8814_handle_interrupt,
2940 .phy_id = PHY_ID_LAN8804,
2941 .phy_id_mask = MICREL_PHY_ID_MASK,
2942 .name = "Microchip LAN966X Gigabit PHY",
2943 .config_init = lan8804_config_init,
2944 .driver_data = &ksz9021_type,
2945 .probe = kszphy_probe,
2946 .soft_reset = genphy_soft_reset,
2947 .read_status = ksz9031_read_status,
2948 .get_sset_count = kszphy_get_sset_count,
2949 .get_strings = kszphy_get_strings,
2950 .get_stats = kszphy_get_stats,
2951 .suspend = genphy_suspend,
2952 .resume = kszphy_resume,
2954 .phy_id = PHY_ID_KSZ9131,
2955 .phy_id_mask = MICREL_PHY_ID_MASK,
2956 .name = "Microchip KSZ9131 Gigabit PHY",
2957 /* PHY_GBIT_FEATURES */
2958 .driver_data = &ksz9021_type,
2959 .probe = kszphy_probe,
2960 .config_init = ksz9131_config_init,
2961 .config_intr = kszphy_config_intr,
2962 .handle_interrupt = kszphy_handle_interrupt,
2963 .get_sset_count = kszphy_get_sset_count,
2964 .get_strings = kszphy_get_strings,
2965 .get_stats = kszphy_get_stats,
2966 .suspend = kszphy_suspend,
2967 .resume = kszphy_resume,
2969 .phy_id = PHY_ID_KSZ8873MLL,
2970 .phy_id_mask = MICREL_PHY_ID_MASK,
2971 .name = "Micrel KSZ8873MLL Switch",
2972 /* PHY_BASIC_FEATURES */
2973 .config_init = kszphy_config_init,
2974 .config_aneg = ksz8873mll_config_aneg,
2975 .read_status = ksz8873mll_read_status,
2976 .suspend = genphy_suspend,
2977 .resume = genphy_resume,
2979 .phy_id = PHY_ID_KSZ886X,
2980 .phy_id_mask = MICREL_PHY_ID_MASK,
2981 .name = "Micrel KSZ8851 Ethernet MAC or KSZ886X Switch",
2982 /* PHY_BASIC_FEATURES */
2983 .flags = PHY_POLL_CABLE_TEST,
2984 .config_init = kszphy_config_init,
2985 .config_aneg = ksz886x_config_aneg,
2986 .read_status = ksz886x_read_status,
2987 .suspend = genphy_suspend,
2988 .resume = genphy_resume,
2989 .cable_test_start = ksz886x_cable_test_start,
2990 .cable_test_get_status = ksz886x_cable_test_get_status,
2992 .name = "Micrel KSZ87XX Switch",
2993 /* PHY_BASIC_FEATURES */
2994 .config_init = kszphy_config_init,
2995 .match_phy_device = ksz8795_match_phy_device,
2996 .suspend = genphy_suspend,
2997 .resume = genphy_resume,
2999 .phy_id = PHY_ID_KSZ9477,
3000 .phy_id_mask = MICREL_PHY_ID_MASK,
3001 .name = "Microchip KSZ9477",
3002 /* PHY_GBIT_FEATURES */
3003 .config_init = kszphy_config_init,
3004 .suspend = genphy_suspend,
3005 .resume = genphy_resume,
3008 module_phy_driver(ksphy_driver);
3010 MODULE_DESCRIPTION("Micrel PHY driver");
3011 MODULE_AUTHOR("David J. Choi");
3012 MODULE_LICENSE("GPL");
3014 static struct mdio_device_id __maybe_unused micrel_tbl[] = {
3015 { PHY_ID_KSZ9021, 0x000ffffe },
3016 { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
3017 { PHY_ID_KSZ9131, MICREL_PHY_ID_MASK },
3018 { PHY_ID_KSZ8001, 0x00fffffc },
3019 { PHY_ID_KS8737, MICREL_PHY_ID_MASK },
3020 { PHY_ID_KSZ8021, 0x00ffffff },
3021 { PHY_ID_KSZ8031, 0x00ffffff },
3022 { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
3023 { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
3024 { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
3025 { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
3026 { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
3027 { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
3028 { PHY_ID_LAN8814, MICREL_PHY_ID_MASK },
3029 { PHY_ID_LAN8804, MICREL_PHY_ID_MASK },
3033 MODULE_DEVICE_TABLE(mdio, micrel_tbl);