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
14 * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
15 * ksz8021, ksz8031, ksz8051,
18 * Switch : ksz8873, ksz886x
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/phy.h>
25 #include <linux/micrel_phy.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
30 /* Operation Mode Strap Override */
31 #define MII_KSZPHY_OMSO 0x16
32 #define KSZPHY_OMSO_FACTORY_TEST BIT(15)
33 #define KSZPHY_OMSO_B_CAST_OFF BIT(9)
34 #define KSZPHY_OMSO_NAND_TREE_ON BIT(5)
35 #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1)
36 #define KSZPHY_OMSO_MII_OVERRIDE BIT(0)
38 /* general Interrupt control/status reg in vendor specific block. */
39 #define MII_KSZPHY_INTCS 0x1B
40 #define KSZPHY_INTCS_JABBER BIT(15)
41 #define KSZPHY_INTCS_RECEIVE_ERR BIT(14)
42 #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13)
43 #define KSZPHY_INTCS_PARELLEL BIT(12)
44 #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11)
45 #define KSZPHY_INTCS_LINK_DOWN BIT(10)
46 #define KSZPHY_INTCS_REMOTE_FAULT BIT(9)
47 #define KSZPHY_INTCS_LINK_UP BIT(8)
48 #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
49 KSZPHY_INTCS_LINK_DOWN)
52 #define MII_KSZPHY_CTRL_1 0x1e
54 /* PHY Control 2 / PHY Control (if no PHY Control 1) */
55 #define MII_KSZPHY_CTRL_2 0x1f
56 #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2
57 /* bitmap of PHY register to set interrupt mode */
58 #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9)
59 #define KSZPHY_RMII_REF_CLK_SEL BIT(7)
61 /* Write/read to/from extended registers */
62 #define MII_KSZPHY_EXTREG 0x0b
63 #define KSZPHY_EXTREG_WRITE 0x8000
65 #define MII_KSZPHY_EXTREG_WRITE 0x0c
66 #define MII_KSZPHY_EXTREG_READ 0x0d
68 /* Extended registers */
69 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
70 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
71 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
75 struct kszphy_hw_stat
{
81 static struct kszphy_hw_stat kszphy_hw_stats
[] = {
82 { "phy_receive_errors", 21, 16},
83 { "phy_idle_errors", 10, 8 },
88 u16 interrupt_level_mask
;
89 bool has_broadcast_disable
;
90 bool has_nand_tree_disable
;
91 bool has_rmii_ref_clk_sel
;
95 const struct kszphy_type
*type
;
97 bool rmii_ref_clk_sel
;
98 bool rmii_ref_clk_sel_val
;
99 u64 stats
[ARRAY_SIZE(kszphy_hw_stats
)];
102 static const struct kszphy_type ksz8021_type
= {
103 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
104 .has_broadcast_disable
= true,
105 .has_nand_tree_disable
= true,
106 .has_rmii_ref_clk_sel
= true,
109 static const struct kszphy_type ksz8041_type
= {
110 .led_mode_reg
= MII_KSZPHY_CTRL_1
,
113 static const struct kszphy_type ksz8051_type
= {
114 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
115 .has_nand_tree_disable
= true,
118 static const struct kszphy_type ksz8081_type
= {
119 .led_mode_reg
= MII_KSZPHY_CTRL_2
,
120 .has_broadcast_disable
= true,
121 .has_nand_tree_disable
= true,
122 .has_rmii_ref_clk_sel
= true,
125 static const struct kszphy_type ks8737_type
= {
126 .interrupt_level_mask
= BIT(14),
129 static const struct kszphy_type ksz9021_type
= {
130 .interrupt_level_mask
= BIT(14),
133 static int kszphy_extended_write(struct phy_device
*phydev
,
136 phy_write(phydev
, MII_KSZPHY_EXTREG
, KSZPHY_EXTREG_WRITE
| regnum
);
137 return phy_write(phydev
, MII_KSZPHY_EXTREG_WRITE
, val
);
140 static int kszphy_extended_read(struct phy_device
*phydev
,
143 phy_write(phydev
, MII_KSZPHY_EXTREG
, regnum
);
144 return phy_read(phydev
, MII_KSZPHY_EXTREG_READ
);
147 static int kszphy_ack_interrupt(struct phy_device
*phydev
)
149 /* bit[7..0] int status, which is a read and clear register. */
152 rc
= phy_read(phydev
, MII_KSZPHY_INTCS
);
154 return (rc
< 0) ? rc
: 0;
157 static int kszphy_config_intr(struct phy_device
*phydev
)
159 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
163 if (type
&& type
->interrupt_level_mask
)
164 mask
= type
->interrupt_level_mask
;
166 mask
= KSZPHY_CTRL_INT_ACTIVE_HIGH
;
168 /* set the interrupt pin active low */
169 temp
= phy_read(phydev
, MII_KSZPHY_CTRL
);
173 phy_write(phydev
, MII_KSZPHY_CTRL
, temp
);
175 /* enable / disable interrupts */
176 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
177 temp
= KSZPHY_INTCS_ALL
;
181 return phy_write(phydev
, MII_KSZPHY_INTCS
, temp
);
184 static int kszphy_rmii_clk_sel(struct phy_device
*phydev
, bool val
)
188 ctrl
= phy_read(phydev
, MII_KSZPHY_CTRL
);
193 ctrl
|= KSZPHY_RMII_REF_CLK_SEL
;
195 ctrl
&= ~KSZPHY_RMII_REF_CLK_SEL
;
197 return phy_write(phydev
, MII_KSZPHY_CTRL
, ctrl
);
200 static int kszphy_setup_led(struct phy_device
*phydev
, u32 reg
, int val
)
205 case MII_KSZPHY_CTRL_1
:
208 case MII_KSZPHY_CTRL_2
:
215 temp
= phy_read(phydev
, reg
);
221 temp
&= ~(3 << shift
);
222 temp
|= val
<< shift
;
223 rc
= phy_write(phydev
, reg
, temp
);
226 phydev_err(phydev
, "failed to set led mode\n");
231 /* Disable PHY address 0 as the broadcast address, so that it can be used as a
232 * unique (non-broadcast) address on a shared bus.
234 static int kszphy_broadcast_disable(struct phy_device
*phydev
)
238 ret
= phy_read(phydev
, MII_KSZPHY_OMSO
);
242 ret
= phy_write(phydev
, MII_KSZPHY_OMSO
, ret
| KSZPHY_OMSO_B_CAST_OFF
);
245 phydev_err(phydev
, "failed to disable broadcast address\n");
250 static int kszphy_nand_tree_disable(struct phy_device
*phydev
)
254 ret
= phy_read(phydev
, MII_KSZPHY_OMSO
);
258 if (!(ret
& KSZPHY_OMSO_NAND_TREE_ON
))
261 ret
= phy_write(phydev
, MII_KSZPHY_OMSO
,
262 ret
& ~KSZPHY_OMSO_NAND_TREE_ON
);
265 phydev_err(phydev
, "failed to disable NAND tree mode\n");
270 /* Some config bits need to be set again on resume, handle them here. */
271 static int kszphy_config_reset(struct phy_device
*phydev
)
273 struct kszphy_priv
*priv
= phydev
->priv
;
276 if (priv
->rmii_ref_clk_sel
) {
277 ret
= kszphy_rmii_clk_sel(phydev
, priv
->rmii_ref_clk_sel_val
);
280 "failed to set rmii reference clock\n");
285 if (priv
->led_mode
>= 0)
286 kszphy_setup_led(phydev
, priv
->type
->led_mode_reg
, priv
->led_mode
);
291 static int kszphy_config_init(struct phy_device
*phydev
)
293 struct kszphy_priv
*priv
= phydev
->priv
;
294 const struct kszphy_type
*type
;
301 if (type
->has_broadcast_disable
)
302 kszphy_broadcast_disable(phydev
);
304 if (type
->has_nand_tree_disable
)
305 kszphy_nand_tree_disable(phydev
);
307 return kszphy_config_reset(phydev
);
310 static int ksz8041_config_init(struct phy_device
*phydev
)
312 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
314 struct device_node
*of_node
= phydev
->mdio
.dev
.of_node
;
316 /* Limit supported and advertised modes in fiber mode */
317 if (of_property_read_bool(of_node
, "micrel,fiber-mode")) {
318 phydev
->dev_flags
|= MICREL_PHY_FXEN
;
319 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
, mask
);
320 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
, mask
);
322 linkmode_and(phydev
->supported
, phydev
->supported
, mask
);
323 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT
,
325 linkmode_and(phydev
->advertising
, phydev
->advertising
, mask
);
326 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT
,
327 phydev
->advertising
);
328 phydev
->autoneg
= AUTONEG_DISABLE
;
331 return kszphy_config_init(phydev
);
334 static int ksz8041_config_aneg(struct phy_device
*phydev
)
336 /* Skip auto-negotiation in fiber mode */
337 if (phydev
->dev_flags
& MICREL_PHY_FXEN
) {
338 phydev
->speed
= SPEED_100
;
342 return genphy_config_aneg(phydev
);
345 static int ksz8051_ksz8795_match_phy_device(struct phy_device
*phydev
,
346 const u32 ksz_phy_id
)
350 if ((phydev
->phy_id
& MICREL_PHY_ID_MASK
) != ksz_phy_id
)
353 ret
= phy_read(phydev
, MII_BMSR
);
357 /* KSZ8051 PHY and KSZ8794/KSZ8795/KSZ8765 switch share the same
358 * exact PHY ID. However, they can be told apart by the extended
359 * capability registers presence. The KSZ8051 PHY has them while
360 * the switch does not.
363 if (ksz_phy_id
== PHY_ID_KSZ8051
)
369 static int ksz8051_match_phy_device(struct phy_device
*phydev
)
371 return ksz8051_ksz8795_match_phy_device(phydev
, PHY_ID_KSZ8051
);
374 static int ksz8081_config_init(struct phy_device
*phydev
)
376 /* KSZPHY_OMSO_FACTORY_TEST is set at de-assertion of the reset line
377 * based on the RXER (KSZ8081RNA/RND) or TXC (KSZ8081MNX/RNB) pin. If a
378 * pull-down is missing, the factory test mode should be cleared by
379 * manually writing a 0.
381 phy_clear_bits(phydev
, MII_KSZPHY_OMSO
, KSZPHY_OMSO_FACTORY_TEST
);
383 return kszphy_config_init(phydev
);
386 static int ksz8061_config_init(struct phy_device
*phydev
)
390 ret
= phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_DEVID1
, 0xB61A);
394 return kszphy_config_init(phydev
);
397 static int ksz8795_match_phy_device(struct phy_device
*phydev
)
399 return ksz8051_ksz8795_match_phy_device(phydev
, PHY_ID_KSZ87XX
);
402 static int ksz9021_load_values_from_of(struct phy_device
*phydev
,
403 const struct device_node
*of_node
,
405 const char *field1
, const char *field2
,
406 const char *field3
, const char *field4
)
415 if (!of_property_read_u32(of_node
, field1
, &val1
))
418 if (!of_property_read_u32(of_node
, field2
, &val2
))
421 if (!of_property_read_u32(of_node
, field3
, &val3
))
424 if (!of_property_read_u32(of_node
, field4
, &val4
))
431 newval
= kszphy_extended_read(phydev
, reg
);
436 newval
= ((newval
& 0xfff0) | ((val1
/ PS_TO_REG
) & 0xf) << 0);
439 newval
= ((newval
& 0xff0f) | ((val2
/ PS_TO_REG
) & 0xf) << 4);
442 newval
= ((newval
& 0xf0ff) | ((val3
/ PS_TO_REG
) & 0xf) << 8);
445 newval
= ((newval
& 0x0fff) | ((val4
/ PS_TO_REG
) & 0xf) << 12);
447 return kszphy_extended_write(phydev
, reg
, newval
);
450 static int ksz9021_config_init(struct phy_device
*phydev
)
452 const struct device
*dev
= &phydev
->mdio
.dev
;
453 const struct device_node
*of_node
= dev
->of_node
;
454 const struct device
*dev_walker
;
456 /* The Micrel driver has a deprecated option to place phy OF
457 * properties in the MAC node. Walk up the tree of devices to
458 * find a device with an OF node.
460 dev_walker
= &phydev
->mdio
.dev
;
462 of_node
= dev_walker
->of_node
;
463 dev_walker
= dev_walker
->parent
;
465 } while (!of_node
&& dev_walker
);
468 ksz9021_load_values_from_of(phydev
, of_node
,
469 MII_KSZPHY_CLK_CONTROL_PAD_SKEW
,
470 "txen-skew-ps", "txc-skew-ps",
471 "rxdv-skew-ps", "rxc-skew-ps");
472 ksz9021_load_values_from_of(phydev
, of_node
,
473 MII_KSZPHY_RX_DATA_PAD_SKEW
,
474 "rxd0-skew-ps", "rxd1-skew-ps",
475 "rxd2-skew-ps", "rxd3-skew-ps");
476 ksz9021_load_values_from_of(phydev
, of_node
,
477 MII_KSZPHY_TX_DATA_PAD_SKEW
,
478 "txd0-skew-ps", "txd1-skew-ps",
479 "txd2-skew-ps", "txd3-skew-ps");
484 #define KSZ9031_PS_TO_REG 60
486 /* Extended registers */
487 /* MMD Address 0x0 */
488 #define MII_KSZ9031RN_FLP_BURST_TX_LO 3
489 #define MII_KSZ9031RN_FLP_BURST_TX_HI 4
491 /* MMD Address 0x2 */
492 #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4
493 #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5
494 #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6
495 #define MII_KSZ9031RN_CLK_PAD_SKEW 8
497 /* MMD Address 0x1C */
498 #define MII_KSZ9031RN_EDPD 0x23
499 #define MII_KSZ9031RN_EDPD_ENABLE BIT(0)
501 static int ksz9031_of_load_skew_values(struct phy_device
*phydev
,
502 const struct device_node
*of_node
,
503 u16 reg
, size_t field_sz
,
504 const char *field
[], u8 numfields
)
506 int val
[4] = {-1, -2, -3, -4};
513 for (i
= 0; i
< numfields
; i
++)
514 if (!of_property_read_u32(of_node
, field
[i
], val
+ i
))
520 if (matches
< numfields
)
521 newval
= phy_read_mmd(phydev
, 2, reg
);
525 maxval
= (field_sz
== 4) ? 0xf : 0x1f;
526 for (i
= 0; i
< numfields
; i
++)
527 if (val
[i
] != -(i
+ 1)) {
529 mask
^= maxval
<< (field_sz
* i
);
530 newval
= (newval
& mask
) |
531 (((val
[i
] / KSZ9031_PS_TO_REG
) & maxval
)
535 return phy_write_mmd(phydev
, 2, reg
, newval
);
538 /* Center KSZ9031RNX FLP timing at 16ms. */
539 static int ksz9031_center_flp_timing(struct phy_device
*phydev
)
543 result
= phy_write_mmd(phydev
, 0, MII_KSZ9031RN_FLP_BURST_TX_HI
,
548 result
= phy_write_mmd(phydev
, 0, MII_KSZ9031RN_FLP_BURST_TX_LO
,
553 return genphy_restart_aneg(phydev
);
556 /* Enable energy-detect power-down mode */
557 static int ksz9031_enable_edpd(struct phy_device
*phydev
)
561 reg
= phy_read_mmd(phydev
, 0x1C, MII_KSZ9031RN_EDPD
);
564 return phy_write_mmd(phydev
, 0x1C, MII_KSZ9031RN_EDPD
,
565 reg
| MII_KSZ9031RN_EDPD_ENABLE
);
568 static int ksz9031_config_init(struct phy_device
*phydev
)
570 const struct device
*dev
= &phydev
->mdio
.dev
;
571 const struct device_node
*of_node
= dev
->of_node
;
572 static const char *clk_skews
[2] = {"rxc-skew-ps", "txc-skew-ps"};
573 static const char *rx_data_skews
[4] = {
574 "rxd0-skew-ps", "rxd1-skew-ps",
575 "rxd2-skew-ps", "rxd3-skew-ps"
577 static const char *tx_data_skews
[4] = {
578 "txd0-skew-ps", "txd1-skew-ps",
579 "txd2-skew-ps", "txd3-skew-ps"
581 static const char *control_skews
[2] = {"txen-skew-ps", "rxdv-skew-ps"};
582 const struct device
*dev_walker
;
585 result
= ksz9031_enable_edpd(phydev
);
589 /* The Micrel driver has a deprecated option to place phy OF
590 * properties in the MAC node. Walk up the tree of devices to
591 * find a device with an OF node.
593 dev_walker
= &phydev
->mdio
.dev
;
595 of_node
= dev_walker
->of_node
;
596 dev_walker
= dev_walker
->parent
;
597 } while (!of_node
&& dev_walker
);
600 ksz9031_of_load_skew_values(phydev
, of_node
,
601 MII_KSZ9031RN_CLK_PAD_SKEW
, 5,
604 ksz9031_of_load_skew_values(phydev
, of_node
,
605 MII_KSZ9031RN_CONTROL_PAD_SKEW
, 4,
608 ksz9031_of_load_skew_values(phydev
, of_node
,
609 MII_KSZ9031RN_RX_DATA_PAD_SKEW
, 4,
612 ksz9031_of_load_skew_values(phydev
, of_node
,
613 MII_KSZ9031RN_TX_DATA_PAD_SKEW
, 4,
616 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
617 * When the device links in the 1000BASE-T slave mode only,
618 * the optional 125MHz reference output clock (CLK125_NDO)
619 * has wide duty cycle variation.
621 * The optional CLK125_NDO clock does not meet the RGMII
622 * 45/55 percent (min/max) duty cycle requirement and therefore
623 * cannot be used directly by the MAC side for clocking
624 * applications that have setup/hold time requirements on
625 * rising and falling clock edges.
628 * Force the phy to be the master to receive a stable clock
629 * which meets the duty cycle requirement.
631 if (of_property_read_bool(of_node
, "micrel,force-master")) {
632 result
= phy_read(phydev
, MII_CTRL1000
);
634 goto err_force_master
;
636 /* enable master mode, config & prefer master */
637 result
|= CTL1000_ENABLE_MASTER
| CTL1000_AS_MASTER
;
638 result
= phy_write(phydev
, MII_CTRL1000
, result
);
640 goto err_force_master
;
644 return ksz9031_center_flp_timing(phydev
);
647 phydev_err(phydev
, "failed to force the phy to master mode\n");
651 #define KSZ9131_SKEW_5BIT_MAX 2400
652 #define KSZ9131_SKEW_4BIT_MAX 800
653 #define KSZ9131_OFFSET 700
654 #define KSZ9131_STEP 100
656 static int ksz9131_of_load_skew_values(struct phy_device
*phydev
,
657 struct device_node
*of_node
,
658 u16 reg
, size_t field_sz
,
659 char *field
[], u8 numfields
)
661 int val
[4] = {-(1 + KSZ9131_OFFSET
), -(2 + KSZ9131_OFFSET
),
662 -(3 + KSZ9131_OFFSET
), -(4 + KSZ9131_OFFSET
)};
663 int skewval
, skewmax
= 0;
670 /* psec properties in dts should mean x pico seconds */
672 skewmax
= KSZ9131_SKEW_5BIT_MAX
;
674 skewmax
= KSZ9131_SKEW_4BIT_MAX
;
676 for (i
= 0; i
< numfields
; i
++)
677 if (!of_property_read_s32(of_node
, field
[i
], &skewval
)) {
678 if (skewval
< -KSZ9131_OFFSET
)
679 skewval
= -KSZ9131_OFFSET
;
680 else if (skewval
> skewmax
)
683 val
[i
] = skewval
+ KSZ9131_OFFSET
;
690 if (matches
< numfields
)
691 newval
= phy_read_mmd(phydev
, 2, reg
);
695 maxval
= (field_sz
== 4) ? 0xf : 0x1f;
696 for (i
= 0; i
< numfields
; i
++)
697 if (val
[i
] != -(i
+ 1 + KSZ9131_OFFSET
)) {
699 mask
^= maxval
<< (field_sz
* i
);
700 newval
= (newval
& mask
) |
701 (((val
[i
] / KSZ9131_STEP
) & maxval
)
705 return phy_write_mmd(phydev
, 2, reg
, newval
);
708 #define KSZ9131RN_MMD_COMMON_CTRL_REG 2
709 #define KSZ9131RN_RXC_DLL_CTRL 76
710 #define KSZ9131RN_TXC_DLL_CTRL 77
711 #define KSZ9131RN_DLL_CTRL_BYPASS BIT_MASK(12)
712 #define KSZ9131RN_DLL_ENABLE_DELAY 0
713 #define KSZ9131RN_DLL_DISABLE_DELAY BIT(12)
715 static int ksz9131_config_rgmii_delay(struct phy_device
*phydev
)
717 u16 rxcdll_val
, txcdll_val
;
720 switch (phydev
->interface
) {
721 case PHY_INTERFACE_MODE_RGMII
:
722 rxcdll_val
= KSZ9131RN_DLL_DISABLE_DELAY
;
723 txcdll_val
= KSZ9131RN_DLL_DISABLE_DELAY
;
725 case PHY_INTERFACE_MODE_RGMII_ID
:
726 rxcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
727 txcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
729 case PHY_INTERFACE_MODE_RGMII_RXID
:
730 rxcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
731 txcdll_val
= KSZ9131RN_DLL_DISABLE_DELAY
;
733 case PHY_INTERFACE_MODE_RGMII_TXID
:
734 rxcdll_val
= KSZ9131RN_DLL_DISABLE_DELAY
;
735 txcdll_val
= KSZ9131RN_DLL_ENABLE_DELAY
;
741 ret
= phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
742 KSZ9131RN_RXC_DLL_CTRL
, KSZ9131RN_DLL_CTRL_BYPASS
,
747 return phy_modify_mmd(phydev
, KSZ9131RN_MMD_COMMON_CTRL_REG
,
748 KSZ9131RN_TXC_DLL_CTRL
, KSZ9131RN_DLL_CTRL_BYPASS
,
752 static int ksz9131_config_init(struct phy_device
*phydev
)
754 const struct device
*dev
= &phydev
->mdio
.dev
;
755 struct device_node
*of_node
= dev
->of_node
;
756 char *clk_skews
[2] = {"rxc-skew-psec", "txc-skew-psec"};
757 char *rx_data_skews
[4] = {
758 "rxd0-skew-psec", "rxd1-skew-psec",
759 "rxd2-skew-psec", "rxd3-skew-psec"
761 char *tx_data_skews
[4] = {
762 "txd0-skew-psec", "txd1-skew-psec",
763 "txd2-skew-psec", "txd3-skew-psec"
765 char *control_skews
[2] = {"txen-skew-psec", "rxdv-skew-psec"};
766 const struct device
*dev_walker
;
769 dev_walker
= &phydev
->mdio
.dev
;
771 of_node
= dev_walker
->of_node
;
772 dev_walker
= dev_walker
->parent
;
773 } while (!of_node
&& dev_walker
);
778 if (phy_interface_is_rgmii(phydev
)) {
779 ret
= ksz9131_config_rgmii_delay(phydev
);
784 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
785 MII_KSZ9031RN_CLK_PAD_SKEW
, 5,
790 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
791 MII_KSZ9031RN_CONTROL_PAD_SKEW
, 4,
796 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
797 MII_KSZ9031RN_RX_DATA_PAD_SKEW
, 4,
802 ret
= ksz9131_of_load_skew_values(phydev
, of_node
,
803 MII_KSZ9031RN_TX_DATA_PAD_SKEW
, 4,
811 #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
812 #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6)
813 #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4)
814 static int ksz8873mll_read_status(struct phy_device
*phydev
)
819 regval
= phy_read(phydev
, KSZ8873MLL_GLOBAL_CONTROL_4
);
821 regval
= phy_read(phydev
, KSZ8873MLL_GLOBAL_CONTROL_4
);
823 if (regval
& KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX
)
824 phydev
->duplex
= DUPLEX_HALF
;
826 phydev
->duplex
= DUPLEX_FULL
;
828 if (regval
& KSZ8873MLL_GLOBAL_CONTROL_4_SPEED
)
829 phydev
->speed
= SPEED_10
;
831 phydev
->speed
= SPEED_100
;
834 phydev
->pause
= phydev
->asym_pause
= 0;
839 static int ksz9031_get_features(struct phy_device
*phydev
)
843 ret
= genphy_read_abilities(phydev
);
847 /* Silicon Errata Sheet (DS80000691D or DS80000692D):
848 * Whenever the device's Asymmetric Pause capability is set to 1,
849 * link-up may fail after a link-up to link-down transition.
851 * The Errata Sheet is for ksz9031, but ksz9021 has the same issue
854 * Do not enable the Asymmetric Pause capability bit.
856 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT
, phydev
->supported
);
858 /* We force setting the Pause capability as the core will force the
859 * Asymmetric Pause capability to 1 otherwise.
861 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT
, phydev
->supported
);
866 static int ksz9031_read_status(struct phy_device
*phydev
)
871 err
= genphy_read_status(phydev
);
875 /* Make sure the PHY is not broken. Read idle error count,
876 * and reset the PHY if it is maxed out.
878 regval
= phy_read(phydev
, MII_STAT1000
);
879 if ((regval
& 0xFF) == 0xFF) {
882 if (phydev
->drv
->config_intr
&& phy_interrupt_is_valid(phydev
))
883 phydev
->drv
->config_intr(phydev
);
884 return genphy_config_aneg(phydev
);
890 static int ksz8873mll_config_aneg(struct phy_device
*phydev
)
895 static int kszphy_get_sset_count(struct phy_device
*phydev
)
897 return ARRAY_SIZE(kszphy_hw_stats
);
900 static void kszphy_get_strings(struct phy_device
*phydev
, u8
*data
)
904 for (i
= 0; i
< ARRAY_SIZE(kszphy_hw_stats
); i
++) {
905 strlcpy(data
+ i
* ETH_GSTRING_LEN
,
906 kszphy_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
910 static u64
kszphy_get_stat(struct phy_device
*phydev
, int i
)
912 struct kszphy_hw_stat stat
= kszphy_hw_stats
[i
];
913 struct kszphy_priv
*priv
= phydev
->priv
;
917 val
= phy_read(phydev
, stat
.reg
);
921 val
= val
& ((1 << stat
.bits
) - 1);
922 priv
->stats
[i
] += val
;
923 ret
= priv
->stats
[i
];
929 static void kszphy_get_stats(struct phy_device
*phydev
,
930 struct ethtool_stats
*stats
, u64
*data
)
934 for (i
= 0; i
< ARRAY_SIZE(kszphy_hw_stats
); i
++)
935 data
[i
] = kszphy_get_stat(phydev
, i
);
938 static int kszphy_suspend(struct phy_device
*phydev
)
940 /* Disable PHY Interrupts */
941 if (phy_interrupt_is_valid(phydev
)) {
942 phydev
->interrupts
= PHY_INTERRUPT_DISABLED
;
943 if (phydev
->drv
->config_intr
)
944 phydev
->drv
->config_intr(phydev
);
947 return genphy_suspend(phydev
);
950 static int kszphy_resume(struct phy_device
*phydev
)
954 genphy_resume(phydev
);
956 /* After switching from power-down to normal mode, an internal global
957 * reset is automatically generated. Wait a minimum of 1 ms before
958 * read/write access to the PHY registers.
960 usleep_range(1000, 2000);
962 ret
= kszphy_config_reset(phydev
);
966 /* Enable PHY Interrupts */
967 if (phy_interrupt_is_valid(phydev
)) {
968 phydev
->interrupts
= PHY_INTERRUPT_ENABLED
;
969 if (phydev
->drv
->config_intr
)
970 phydev
->drv
->config_intr(phydev
);
976 static int kszphy_probe(struct phy_device
*phydev
)
978 const struct kszphy_type
*type
= phydev
->drv
->driver_data
;
979 const struct device_node
*np
= phydev
->mdio
.dev
.of_node
;
980 struct kszphy_priv
*priv
;
984 priv
= devm_kzalloc(&phydev
->mdio
.dev
, sizeof(*priv
), GFP_KERNEL
);
992 if (type
->led_mode_reg
) {
993 ret
= of_property_read_u32(np
, "micrel,led-mode",
998 if (priv
->led_mode
> 3) {
999 phydev_err(phydev
, "invalid led mode: 0x%02x\n",
1001 priv
->led_mode
= -1;
1004 priv
->led_mode
= -1;
1007 clk
= devm_clk_get(&phydev
->mdio
.dev
, "rmii-ref");
1008 /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
1009 if (!IS_ERR_OR_NULL(clk
)) {
1010 unsigned long rate
= clk_get_rate(clk
);
1011 bool rmii_ref_clk_sel_25_mhz
;
1013 priv
->rmii_ref_clk_sel
= type
->has_rmii_ref_clk_sel
;
1014 rmii_ref_clk_sel_25_mhz
= of_property_read_bool(np
,
1015 "micrel,rmii-reference-clock-select-25-mhz");
1017 if (rate
> 24500000 && rate
< 25500000) {
1018 priv
->rmii_ref_clk_sel_val
= rmii_ref_clk_sel_25_mhz
;
1019 } else if (rate
> 49500000 && rate
< 50500000) {
1020 priv
->rmii_ref_clk_sel_val
= !rmii_ref_clk_sel_25_mhz
;
1022 phydev_err(phydev
, "Clock rate out of range: %ld\n",
1028 /* Support legacy board-file configuration */
1029 if (phydev
->dev_flags
& MICREL_PHY_50MHZ_CLK
) {
1030 priv
->rmii_ref_clk_sel
= true;
1031 priv
->rmii_ref_clk_sel_val
= true;
1037 static struct phy_driver ksphy_driver
[] = {
1039 .phy_id
= PHY_ID_KS8737
,
1040 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1041 .name
= "Micrel KS8737",
1042 /* PHY_BASIC_FEATURES */
1043 .driver_data
= &ks8737_type
,
1044 .config_init
= kszphy_config_init
,
1045 .ack_interrupt
= kszphy_ack_interrupt
,
1046 .config_intr
= kszphy_config_intr
,
1047 .suspend
= genphy_suspend
,
1048 .resume
= genphy_resume
,
1050 .phy_id
= PHY_ID_KSZ8021
,
1051 .phy_id_mask
= 0x00ffffff,
1052 .name
= "Micrel KSZ8021 or KSZ8031",
1053 /* PHY_BASIC_FEATURES */
1054 .driver_data
= &ksz8021_type
,
1055 .probe
= kszphy_probe
,
1056 .config_init
= kszphy_config_init
,
1057 .ack_interrupt
= kszphy_ack_interrupt
,
1058 .config_intr
= kszphy_config_intr
,
1059 .get_sset_count
= kszphy_get_sset_count
,
1060 .get_strings
= kszphy_get_strings
,
1061 .get_stats
= kszphy_get_stats
,
1062 .suspend
= genphy_suspend
,
1063 .resume
= genphy_resume
,
1065 .phy_id
= PHY_ID_KSZ8031
,
1066 .phy_id_mask
= 0x00ffffff,
1067 .name
= "Micrel KSZ8031",
1068 /* PHY_BASIC_FEATURES */
1069 .driver_data
= &ksz8021_type
,
1070 .probe
= kszphy_probe
,
1071 .config_init
= kszphy_config_init
,
1072 .ack_interrupt
= kszphy_ack_interrupt
,
1073 .config_intr
= kszphy_config_intr
,
1074 .get_sset_count
= kszphy_get_sset_count
,
1075 .get_strings
= kszphy_get_strings
,
1076 .get_stats
= kszphy_get_stats
,
1077 .suspend
= genphy_suspend
,
1078 .resume
= genphy_resume
,
1080 .phy_id
= PHY_ID_KSZ8041
,
1081 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1082 .name
= "Micrel KSZ8041",
1083 /* PHY_BASIC_FEATURES */
1084 .driver_data
= &ksz8041_type
,
1085 .probe
= kszphy_probe
,
1086 .config_init
= ksz8041_config_init
,
1087 .config_aneg
= ksz8041_config_aneg
,
1088 .ack_interrupt
= kszphy_ack_interrupt
,
1089 .config_intr
= kszphy_config_intr
,
1090 .get_sset_count
= kszphy_get_sset_count
,
1091 .get_strings
= kszphy_get_strings
,
1092 .get_stats
= kszphy_get_stats
,
1093 .suspend
= genphy_suspend
,
1094 .resume
= genphy_resume
,
1096 .phy_id
= PHY_ID_KSZ8041RNLI
,
1097 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1098 .name
= "Micrel KSZ8041RNLI",
1099 /* PHY_BASIC_FEATURES */
1100 .driver_data
= &ksz8041_type
,
1101 .probe
= kszphy_probe
,
1102 .config_init
= kszphy_config_init
,
1103 .ack_interrupt
= kszphy_ack_interrupt
,
1104 .config_intr
= kszphy_config_intr
,
1105 .get_sset_count
= kszphy_get_sset_count
,
1106 .get_strings
= kszphy_get_strings
,
1107 .get_stats
= kszphy_get_stats
,
1108 .suspend
= genphy_suspend
,
1109 .resume
= genphy_resume
,
1111 .name
= "Micrel KSZ8051",
1112 /* PHY_BASIC_FEATURES */
1113 .driver_data
= &ksz8051_type
,
1114 .probe
= kszphy_probe
,
1115 .config_init
= kszphy_config_init
,
1116 .ack_interrupt
= kszphy_ack_interrupt
,
1117 .config_intr
= kszphy_config_intr
,
1118 .get_sset_count
= kszphy_get_sset_count
,
1119 .get_strings
= kszphy_get_strings
,
1120 .get_stats
= kszphy_get_stats
,
1121 .match_phy_device
= ksz8051_match_phy_device
,
1122 .suspend
= genphy_suspend
,
1123 .resume
= genphy_resume
,
1125 .phy_id
= PHY_ID_KSZ8001
,
1126 .name
= "Micrel KSZ8001 or KS8721",
1127 .phy_id_mask
= 0x00fffffc,
1128 /* PHY_BASIC_FEATURES */
1129 .driver_data
= &ksz8041_type
,
1130 .probe
= kszphy_probe
,
1131 .config_init
= kszphy_config_init
,
1132 .ack_interrupt
= kszphy_ack_interrupt
,
1133 .config_intr
= kszphy_config_intr
,
1134 .get_sset_count
= kszphy_get_sset_count
,
1135 .get_strings
= kszphy_get_strings
,
1136 .get_stats
= kszphy_get_stats
,
1137 .suspend
= genphy_suspend
,
1138 .resume
= genphy_resume
,
1140 .phy_id
= PHY_ID_KSZ8081
,
1141 .name
= "Micrel KSZ8081 or KSZ8091",
1142 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1143 /* PHY_BASIC_FEATURES */
1144 .driver_data
= &ksz8081_type
,
1145 .probe
= kszphy_probe
,
1146 .config_init
= ksz8081_config_init
,
1147 .ack_interrupt
= kszphy_ack_interrupt
,
1148 .config_intr
= kszphy_config_intr
,
1149 .get_sset_count
= kszphy_get_sset_count
,
1150 .get_strings
= kszphy_get_strings
,
1151 .get_stats
= kszphy_get_stats
,
1152 .suspend
= kszphy_suspend
,
1153 .resume
= kszphy_resume
,
1155 .phy_id
= PHY_ID_KSZ8061
,
1156 .name
= "Micrel KSZ8061",
1157 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1158 /* PHY_BASIC_FEATURES */
1159 .config_init
= ksz8061_config_init
,
1160 .ack_interrupt
= kszphy_ack_interrupt
,
1161 .config_intr
= kszphy_config_intr
,
1162 .suspend
= genphy_suspend
,
1163 .resume
= genphy_resume
,
1165 .phy_id
= PHY_ID_KSZ9021
,
1166 .phy_id_mask
= 0x000ffffe,
1167 .name
= "Micrel KSZ9021 Gigabit PHY",
1168 /* PHY_GBIT_FEATURES */
1169 .driver_data
= &ksz9021_type
,
1170 .probe
= kszphy_probe
,
1171 .get_features
= ksz9031_get_features
,
1172 .config_init
= ksz9021_config_init
,
1173 .ack_interrupt
= kszphy_ack_interrupt
,
1174 .config_intr
= kszphy_config_intr
,
1175 .get_sset_count
= kszphy_get_sset_count
,
1176 .get_strings
= kszphy_get_strings
,
1177 .get_stats
= kszphy_get_stats
,
1178 .suspend
= genphy_suspend
,
1179 .resume
= genphy_resume
,
1180 .read_mmd
= genphy_read_mmd_unsupported
,
1181 .write_mmd
= genphy_write_mmd_unsupported
,
1183 .phy_id
= PHY_ID_KSZ9031
,
1184 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1185 .name
= "Micrel KSZ9031 Gigabit PHY",
1186 .driver_data
= &ksz9021_type
,
1187 .probe
= kszphy_probe
,
1188 .get_features
= ksz9031_get_features
,
1189 .config_init
= ksz9031_config_init
,
1190 .soft_reset
= genphy_soft_reset
,
1191 .read_status
= ksz9031_read_status
,
1192 .ack_interrupt
= kszphy_ack_interrupt
,
1193 .config_intr
= kszphy_config_intr
,
1194 .get_sset_count
= kszphy_get_sset_count
,
1195 .get_strings
= kszphy_get_strings
,
1196 .get_stats
= kszphy_get_stats
,
1197 .suspend
= genphy_suspend
,
1198 .resume
= kszphy_resume
,
1200 .phy_id
= PHY_ID_KSZ9131
,
1201 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1202 .name
= "Microchip KSZ9131 Gigabit PHY",
1203 /* PHY_GBIT_FEATURES */
1204 .driver_data
= &ksz9021_type
,
1205 .probe
= kszphy_probe
,
1206 .config_init
= ksz9131_config_init
,
1207 .read_status
= genphy_read_status
,
1208 .ack_interrupt
= kszphy_ack_interrupt
,
1209 .config_intr
= kszphy_config_intr
,
1210 .get_sset_count
= kszphy_get_sset_count
,
1211 .get_strings
= kszphy_get_strings
,
1212 .get_stats
= kszphy_get_stats
,
1213 .suspend
= genphy_suspend
,
1214 .resume
= kszphy_resume
,
1216 .phy_id
= PHY_ID_KSZ8873MLL
,
1217 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1218 .name
= "Micrel KSZ8873MLL Switch",
1219 /* PHY_BASIC_FEATURES */
1220 .config_init
= kszphy_config_init
,
1221 .config_aneg
= ksz8873mll_config_aneg
,
1222 .read_status
= ksz8873mll_read_status
,
1223 .suspend
= genphy_suspend
,
1224 .resume
= genphy_resume
,
1226 .phy_id
= PHY_ID_KSZ886X
,
1227 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1228 .name
= "Micrel KSZ886X Switch",
1229 /* PHY_BASIC_FEATURES */
1230 .config_init
= kszphy_config_init
,
1231 .suspend
= genphy_suspend
,
1232 .resume
= genphy_resume
,
1234 .name
= "Micrel KSZ87XX Switch",
1235 /* PHY_BASIC_FEATURES */
1236 .config_init
= kszphy_config_init
,
1237 .config_aneg
= ksz8873mll_config_aneg
,
1238 .read_status
= ksz8873mll_read_status
,
1239 .match_phy_device
= ksz8795_match_phy_device
,
1240 .suspend
= genphy_suspend
,
1241 .resume
= genphy_resume
,
1243 .phy_id
= PHY_ID_KSZ9477
,
1244 .phy_id_mask
= MICREL_PHY_ID_MASK
,
1245 .name
= "Microchip KSZ9477",
1246 /* PHY_GBIT_FEATURES */
1247 .config_init
= kszphy_config_init
,
1248 .suspend
= genphy_suspend
,
1249 .resume
= genphy_resume
,
1252 module_phy_driver(ksphy_driver
);
1254 MODULE_DESCRIPTION("Micrel PHY driver");
1255 MODULE_AUTHOR("David J. Choi");
1256 MODULE_LICENSE("GPL");
1258 static struct mdio_device_id __maybe_unused micrel_tbl
[] = {
1259 { PHY_ID_KSZ9021
, 0x000ffffe },
1260 { PHY_ID_KSZ9031
, MICREL_PHY_ID_MASK
},
1261 { PHY_ID_KSZ9131
, MICREL_PHY_ID_MASK
},
1262 { PHY_ID_KSZ8001
, 0x00fffffc },
1263 { PHY_ID_KS8737
, MICREL_PHY_ID_MASK
},
1264 { PHY_ID_KSZ8021
, 0x00ffffff },
1265 { PHY_ID_KSZ8031
, 0x00ffffff },
1266 { PHY_ID_KSZ8041
, MICREL_PHY_ID_MASK
},
1267 { PHY_ID_KSZ8051
, MICREL_PHY_ID_MASK
},
1268 { PHY_ID_KSZ8061
, MICREL_PHY_ID_MASK
},
1269 { PHY_ID_KSZ8081
, MICREL_PHY_ID_MASK
},
1270 { PHY_ID_KSZ8873MLL
, MICREL_PHY_ID_MASK
},
1271 { PHY_ID_KSZ886X
, MICREL_PHY_ID_MASK
},
1275 MODULE_DEVICE_TABLE(mdio
, micrel_tbl
);