dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / net / phy / micrel.c
blob3a4d83fa52dca3c31245428dc0cba5171a641838
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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,
16 * ksz8081, ksz8091,
17 * ksz8061,
18 * Switch : ksz8873, ksz886x
19 * ksz9477
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/phy.h>
25 #include <linux/micrel_phy.h>
26 #include <linux/of.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)
51 /* PHY Control 1 */
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
73 #define PS_TO_REG 200
75 struct kszphy_hw_stat {
76 const char *string;
77 u8 reg;
78 u8 bits;
81 static struct kszphy_hw_stat kszphy_hw_stats[] = {
82 { "phy_receive_errors", 21, 16},
83 { "phy_idle_errors", 10, 8 },
86 struct kszphy_type {
87 u32 led_mode_reg;
88 u16 interrupt_level_mask;
89 bool has_broadcast_disable;
90 bool has_nand_tree_disable;
91 bool has_rmii_ref_clk_sel;
94 struct kszphy_priv {
95 const struct kszphy_type *type;
96 int led_mode;
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,
134 u32 regnum, u16 val)
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,
141 u32 regnum)
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. */
150 int rc;
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;
160 int temp;
161 u16 mask;
163 if (type && type->interrupt_level_mask)
164 mask = type->interrupt_level_mask;
165 else
166 mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
168 /* set the interrupt pin active low */
169 temp = phy_read(phydev, MII_KSZPHY_CTRL);
170 if (temp < 0)
171 return temp;
172 temp &= ~mask;
173 phy_write(phydev, MII_KSZPHY_CTRL, temp);
175 /* enable / disable interrupts */
176 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
177 temp = KSZPHY_INTCS_ALL;
178 else
179 temp = 0;
181 return phy_write(phydev, MII_KSZPHY_INTCS, temp);
184 static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
186 int ctrl;
188 ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
189 if (ctrl < 0)
190 return ctrl;
192 if (val)
193 ctrl |= KSZPHY_RMII_REF_CLK_SEL;
194 else
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)
202 int rc, temp, shift;
204 switch (reg) {
205 case MII_KSZPHY_CTRL_1:
206 shift = 14;
207 break;
208 case MII_KSZPHY_CTRL_2:
209 shift = 4;
210 break;
211 default:
212 return -EINVAL;
215 temp = phy_read(phydev, reg);
216 if (temp < 0) {
217 rc = temp;
218 goto out;
221 temp &= ~(3 << shift);
222 temp |= val << shift;
223 rc = phy_write(phydev, reg, temp);
224 out:
225 if (rc < 0)
226 phydev_err(phydev, "failed to set led mode\n");
228 return rc;
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)
236 int ret;
238 ret = phy_read(phydev, MII_KSZPHY_OMSO);
239 if (ret < 0)
240 goto out;
242 ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
243 out:
244 if (ret)
245 phydev_err(phydev, "failed to disable broadcast address\n");
247 return ret;
250 static int kszphy_nand_tree_disable(struct phy_device *phydev)
252 int ret;
254 ret = phy_read(phydev, MII_KSZPHY_OMSO);
255 if (ret < 0)
256 goto out;
258 if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
259 return 0;
261 ret = phy_write(phydev, MII_KSZPHY_OMSO,
262 ret & ~KSZPHY_OMSO_NAND_TREE_ON);
263 out:
264 if (ret)
265 phydev_err(phydev, "failed to disable NAND tree mode\n");
267 return ret;
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;
274 int ret;
276 if (priv->rmii_ref_clk_sel) {
277 ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
278 if (ret) {
279 phydev_err(phydev,
280 "failed to set rmii reference clock\n");
281 return ret;
285 if (priv->led_mode >= 0)
286 kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
288 return 0;
291 static int kszphy_config_init(struct phy_device *phydev)
293 struct kszphy_priv *priv = phydev->priv;
294 const struct kszphy_type *type;
296 if (!priv)
297 return 0;
299 type = priv->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,
324 phydev->supported);
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;
339 return 0;
342 return genphy_config_aneg(phydev);
345 static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
346 const u32 ksz_phy_id)
348 int ret;
350 if ((phydev->phy_id & MICREL_PHY_ID_MASK) != ksz_phy_id)
351 return 0;
353 ret = phy_read(phydev, MII_BMSR);
354 if (ret < 0)
355 return ret;
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.
362 ret &= BMSR_ERCAP;
363 if (ksz_phy_id == PHY_ID_KSZ8051)
364 return ret;
365 else
366 return !ret;
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)
388 int ret;
390 ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
391 if (ret)
392 return ret;
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,
404 u16 reg,
405 const char *field1, const char *field2,
406 const char *field3, const char *field4)
408 int val1 = -1;
409 int val2 = -2;
410 int val3 = -3;
411 int val4 = -4;
412 int newval;
413 int matches = 0;
415 if (!of_property_read_u32(of_node, field1, &val1))
416 matches++;
418 if (!of_property_read_u32(of_node, field2, &val2))
419 matches++;
421 if (!of_property_read_u32(of_node, field3, &val3))
422 matches++;
424 if (!of_property_read_u32(of_node, field4, &val4))
425 matches++;
427 if (!matches)
428 return 0;
430 if (matches < 4)
431 newval = kszphy_extended_read(phydev, reg);
432 else
433 newval = 0;
435 if (val1 != -1)
436 newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
438 if (val2 != -2)
439 newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
441 if (val3 != -3)
442 newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
444 if (val4 != -4)
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;
461 do {
462 of_node = dev_walker->of_node;
463 dev_walker = dev_walker->parent;
465 } while (!of_node && dev_walker);
467 if (of_node) {
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");
481 return 0;
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};
507 int matches = 0;
508 u16 mask;
509 u16 maxval;
510 u16 newval;
511 int i;
513 for (i = 0; i < numfields; i++)
514 if (!of_property_read_u32(of_node, field[i], val + i))
515 matches++;
517 if (!matches)
518 return 0;
520 if (matches < numfields)
521 newval = phy_read_mmd(phydev, 2, reg);
522 else
523 newval = 0;
525 maxval = (field_sz == 4) ? 0xf : 0x1f;
526 for (i = 0; i < numfields; i++)
527 if (val[i] != -(i + 1)) {
528 mask = 0xffff;
529 mask ^= maxval << (field_sz * i);
530 newval = (newval & mask) |
531 (((val[i] / KSZ9031_PS_TO_REG) & maxval)
532 << (field_sz * i));
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)
541 int result;
543 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_HI,
544 0x0006);
545 if (result)
546 return result;
548 result = phy_write_mmd(phydev, 0, MII_KSZ9031RN_FLP_BURST_TX_LO,
549 0x1A80);
550 if (result)
551 return result;
553 return genphy_restart_aneg(phydev);
556 /* Enable energy-detect power-down mode */
557 static int ksz9031_enable_edpd(struct phy_device *phydev)
559 int reg;
561 reg = phy_read_mmd(phydev, 0x1C, MII_KSZ9031RN_EDPD);
562 if (reg < 0)
563 return reg;
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;
583 int result;
585 result = ksz9031_enable_edpd(phydev);
586 if (result < 0)
587 return result;
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;
594 do {
595 of_node = dev_walker->of_node;
596 dev_walker = dev_walker->parent;
597 } while (!of_node && dev_walker);
599 if (of_node) {
600 ksz9031_of_load_skew_values(phydev, of_node,
601 MII_KSZ9031RN_CLK_PAD_SKEW, 5,
602 clk_skews, 2);
604 ksz9031_of_load_skew_values(phydev, of_node,
605 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
606 control_skews, 2);
608 ksz9031_of_load_skew_values(phydev, of_node,
609 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
610 rx_data_skews, 4);
612 ksz9031_of_load_skew_values(phydev, of_node,
613 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
614 tx_data_skews, 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.
627 * Workaround:
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);
633 if (result < 0)
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);
639 if (result < 0)
640 goto err_force_master;
644 return ksz9031_center_flp_timing(phydev);
646 err_force_master:
647 phydev_err(phydev, "failed to force the phy to master mode\n");
648 return result;
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;
664 int matches = 0;
665 u16 maxval;
666 u16 newval;
667 u16 mask;
668 int i;
670 /* psec properties in dts should mean x pico seconds */
671 if (field_sz == 5)
672 skewmax = KSZ9131_SKEW_5BIT_MAX;
673 else
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)
681 skewval = skewmax;
683 val[i] = skewval + KSZ9131_OFFSET;
684 matches++;
687 if (!matches)
688 return 0;
690 if (matches < numfields)
691 newval = phy_read_mmd(phydev, 2, reg);
692 else
693 newval = 0;
695 maxval = (field_sz == 4) ? 0xf : 0x1f;
696 for (i = 0; i < numfields; i++)
697 if (val[i] != -(i + 1 + KSZ9131_OFFSET)) {
698 mask = 0xffff;
699 mask ^= maxval << (field_sz * i);
700 newval = (newval & mask) |
701 (((val[i] / KSZ9131_STEP) & maxval)
702 << (field_sz * i));
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;
718 int ret;
720 switch (phydev->interface) {
721 case PHY_INTERFACE_MODE_RGMII:
722 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
723 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
724 break;
725 case PHY_INTERFACE_MODE_RGMII_ID:
726 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
727 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
728 break;
729 case PHY_INTERFACE_MODE_RGMII_RXID:
730 rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
731 txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
732 break;
733 case PHY_INTERFACE_MODE_RGMII_TXID:
734 rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY;
735 txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY;
736 break;
737 default:
738 return 0;
741 ret = phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
742 KSZ9131RN_RXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
743 rxcdll_val);
744 if (ret < 0)
745 return ret;
747 return phy_modify_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG,
748 KSZ9131RN_TXC_DLL_CTRL, KSZ9131RN_DLL_CTRL_BYPASS,
749 txcdll_val);
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;
767 int ret;
769 dev_walker = &phydev->mdio.dev;
770 do {
771 of_node = dev_walker->of_node;
772 dev_walker = dev_walker->parent;
773 } while (!of_node && dev_walker);
775 if (!of_node)
776 return 0;
778 if (phy_interface_is_rgmii(phydev)) {
779 ret = ksz9131_config_rgmii_delay(phydev);
780 if (ret < 0)
781 return ret;
784 ret = ksz9131_of_load_skew_values(phydev, of_node,
785 MII_KSZ9031RN_CLK_PAD_SKEW, 5,
786 clk_skews, 2);
787 if (ret < 0)
788 return ret;
790 ret = ksz9131_of_load_skew_values(phydev, of_node,
791 MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
792 control_skews, 2);
793 if (ret < 0)
794 return ret;
796 ret = ksz9131_of_load_skew_values(phydev, of_node,
797 MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
798 rx_data_skews, 4);
799 if (ret < 0)
800 return ret;
802 ret = ksz9131_of_load_skew_values(phydev, of_node,
803 MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
804 tx_data_skews, 4);
805 if (ret < 0)
806 return ret;
808 return 0;
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)
816 int regval;
818 /* dummy read */
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;
825 else
826 phydev->duplex = DUPLEX_FULL;
828 if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
829 phydev->speed = SPEED_10;
830 else
831 phydev->speed = SPEED_100;
833 phydev->link = 1;
834 phydev->pause = phydev->asym_pause = 0;
836 return 0;
839 static int ksz9031_get_features(struct phy_device *phydev)
841 int ret;
843 ret = genphy_read_abilities(phydev);
844 if (ret < 0)
845 return ret;
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
853 * Workaround:
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);
863 return 0;
866 static int ksz9031_read_status(struct phy_device *phydev)
868 int err;
869 int regval;
871 err = genphy_read_status(phydev);
872 if (err)
873 return err;
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) {
880 phy_init_hw(phydev);
881 phydev->link = 0;
882 if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
883 phydev->drv->config_intr(phydev);
884 return genphy_config_aneg(phydev);
887 return 0;
890 static int ksz8873mll_config_aneg(struct phy_device *phydev)
892 return 0;
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)
902 int i;
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;
914 int val;
915 u64 ret;
917 val = phy_read(phydev, stat.reg);
918 if (val < 0) {
919 ret = U64_MAX;
920 } else {
921 val = val & ((1 << stat.bits) - 1);
922 priv->stats[i] += val;
923 ret = priv->stats[i];
926 return ret;
929 static void kszphy_get_stats(struct phy_device *phydev,
930 struct ethtool_stats *stats, u64 *data)
932 int i;
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)
952 int ret;
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);
963 if (ret)
964 return ret;
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);
973 return 0;
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;
981 struct clk *clk;
982 int ret;
984 priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
985 if (!priv)
986 return -ENOMEM;
988 phydev->priv = priv;
990 priv->type = type;
992 if (type->led_mode_reg) {
993 ret = of_property_read_u32(np, "micrel,led-mode",
994 &priv->led_mode);
995 if (ret)
996 priv->led_mode = -1;
998 if (priv->led_mode > 3) {
999 phydev_err(phydev, "invalid led mode: 0x%02x\n",
1000 priv->led_mode);
1001 priv->led_mode = -1;
1003 } else {
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;
1021 } else {
1022 phydev_err(phydev, "Clock rate out of range: %ld\n",
1023 rate);
1024 return -EINVAL;
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;
1034 return 0;
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,
1049 }, {
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,
1064 }, {
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,
1079 }, {
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,
1095 }, {
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,
1110 }, {
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,
1124 }, {
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,
1139 }, {
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,
1154 }, {
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,
1164 }, {
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,
1182 }, {
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,
1199 }, {
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,
1215 }, {
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,
1225 }, {
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,
1233 }, {
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,
1242 }, {
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,
1250 } };
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);