1 // SPDX-License-Identifier: GPL-2.0
2 /* NXP TJA1100 BroadRReach PHY driver
4 * Copyright (C) 2018 Marek Vasut <marex@denx.de>
6 #include <linux/delay.h>
7 #include <linux/ethtool.h>
8 #include <linux/ethtool_netlink.h>
9 #include <linux/kernel.h>
10 #include <linux/mdio.h>
11 #include <linux/mii.h>
12 #include <linux/module.h>
13 #include <linux/phy.h>
14 #include <linux/hwmon.h>
15 #include <linux/bitfield.h>
16 #include <linux/of_mdio.h>
17 #include <linux/of_irq.h>
19 #define PHY_ID_MASK 0xfffffff0
20 #define PHY_ID_TJA1100 0x0180dc40
21 #define PHY_ID_TJA1101 0x0180dd00
22 #define PHY_ID_TJA1102 0x0180dc80
25 #define MII_ECTRL_LINK_CONTROL BIT(15)
26 #define MII_ECTRL_POWER_MODE_MASK GENMASK(14, 11)
27 #define MII_ECTRL_POWER_MODE_NO_CHANGE (0x0 << 11)
28 #define MII_ECTRL_POWER_MODE_NORMAL (0x3 << 11)
29 #define MII_ECTRL_POWER_MODE_STANDBY (0xc << 11)
30 #define MII_ECTRL_CABLE_TEST BIT(5)
31 #define MII_ECTRL_CONFIG_EN BIT(2)
32 #define MII_ECTRL_WAKE_REQUEST BIT(0)
35 #define MII_CFG1_MASTER_SLAVE BIT(15)
36 #define MII_CFG1_AUTO_OP BIT(14)
37 #define MII_CFG1_SLEEP_CONFIRM BIT(6)
38 #define MII_CFG1_LED_MODE_MASK GENMASK(5, 4)
39 #define MII_CFG1_LED_MODE_LINKUP 0
40 #define MII_CFG1_LED_ENABLE BIT(3)
43 #define MII_CFG2_SLEEP_REQUEST_TO GENMASK(1, 0)
44 #define MII_CFG2_SLEEP_REQUEST_TO_16MS 0x3
47 #define MII_INTSRC_TEMP_ERR BIT(1)
48 #define MII_INTSRC_UV_ERR BIT(3)
51 #define MII_INTEN_LINK_FAIL BIT(10)
52 #define MII_INTEN_LINK_UP BIT(9)
54 #define MII_COMMSTAT 23
55 #define MII_COMMSTAT_LINK_UP BIT(15)
56 #define MII_COMMSTAT_SQI_STATE GENMASK(7, 5)
57 #define MII_COMMSTAT_SQI_MAX 7
59 #define MII_GENSTAT 24
60 #define MII_GENSTAT_PLL_LOCKED BIT(14)
62 #define MII_EXTSTAT 25
63 #define MII_EXTSTAT_SHORT_DETECT BIT(8)
64 #define MII_EXTSTAT_OPEN_DETECT BIT(7)
65 #define MII_EXTSTAT_POLARITY_DETECT BIT(6)
67 #define MII_COMMCFG 27
68 #define MII_COMMCFG_AUTO_OP BIT(15)
72 struct device *hwmon_dev;
73 struct phy_device *phydev;
74 struct work_struct phy_register_work;
77 struct tja11xx_phy_stats {
84 static struct tja11xx_phy_stats tja11xx_hw_stats[] = {
85 { "phy_symbol_error_count", 20, 0, GENMASK(15, 0) },
86 { "phy_polarity_detect", 25, 6, BIT(6) },
87 { "phy_open_detect", 25, 7, BIT(7) },
88 { "phy_short_detect", 25, 8, BIT(8) },
89 { "phy_rem_rcvr_count", 26, 0, GENMASK(7, 0) },
90 { "phy_loc_rcvr_count", 26, 8, GENMASK(15, 8) },
93 static int tja11xx_check(struct phy_device *phydev, u8 reg, u16 mask, u16 set)
97 return phy_read_poll_timeout(phydev, reg, val, (val & mask) == set,
101 static int phy_modify_check(struct phy_device *phydev, u8 reg,
106 ret = phy_modify(phydev, reg, mask, set);
110 return tja11xx_check(phydev, reg, mask, set);
113 static int tja11xx_enable_reg_write(struct phy_device *phydev)
115 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CONFIG_EN);
118 static int tja11xx_enable_link_control(struct phy_device *phydev)
120 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
123 static int tja11xx_disable_link_control(struct phy_device *phydev)
125 return phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
128 static int tja11xx_wakeup(struct phy_device *phydev)
132 ret = phy_read(phydev, MII_ECTRL);
136 switch (ret & MII_ECTRL_POWER_MODE_MASK) {
137 case MII_ECTRL_POWER_MODE_NO_CHANGE:
139 case MII_ECTRL_POWER_MODE_NORMAL:
140 ret = phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
144 ret = phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
148 case MII_ECTRL_POWER_MODE_STANDBY:
149 ret = phy_modify_check(phydev, MII_ECTRL,
150 MII_ECTRL_POWER_MODE_MASK,
151 MII_ECTRL_POWER_MODE_STANDBY);
155 ret = phy_modify(phydev, MII_ECTRL, MII_ECTRL_POWER_MODE_MASK,
156 MII_ECTRL_POWER_MODE_NORMAL);
160 ret = phy_modify_check(phydev, MII_GENSTAT,
161 MII_GENSTAT_PLL_LOCKED,
162 MII_GENSTAT_PLL_LOCKED);
166 return tja11xx_enable_link_control(phydev);
174 static int tja11xx_soft_reset(struct phy_device *phydev)
178 ret = tja11xx_enable_reg_write(phydev);
182 return genphy_soft_reset(phydev);
185 static int tja11xx_config_aneg_cable_test(struct phy_device *phydev)
187 bool finished = false;
193 if (!phydev->drv->cable_test_start ||
194 !phydev->drv->cable_test_get_status)
197 ret = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
201 ret = phydev->drv->cable_test_start(phydev);
205 /* According to the documentation this test takes 100 usec */
206 usleep_range(100, 200);
208 ret = phydev->drv->cable_test_get_status(phydev, &finished);
213 ethnl_cable_test_finished(phydev);
218 static int tja11xx_config_aneg(struct phy_device *phydev)
220 int ret, changed = 0;
223 switch (phydev->master_slave_set) {
224 case MASTER_SLAVE_CFG_MASTER_FORCE:
225 ctl |= MII_CFG1_MASTER_SLAVE;
227 case MASTER_SLAVE_CFG_SLAVE_FORCE:
229 case MASTER_SLAVE_CFG_UNKNOWN:
230 case MASTER_SLAVE_CFG_UNSUPPORTED:
233 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
237 changed = phy_modify_changed(phydev, MII_CFG1, MII_CFG1_MASTER_SLAVE, ctl);
242 ret = tja11xx_config_aneg_cable_test(phydev);
246 return __genphy_config_aneg(phydev, changed);
249 static int tja11xx_config_init(struct phy_device *phydev)
253 ret = tja11xx_enable_reg_write(phydev);
257 phydev->autoneg = AUTONEG_DISABLE;
258 phydev->speed = SPEED_100;
259 phydev->duplex = DUPLEX_FULL;
261 switch (phydev->phy_id & PHY_ID_MASK) {
263 ret = phy_modify(phydev, MII_CFG1,
264 MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_MASK |
266 MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_LINKUP |
267 MII_CFG1_LED_ENABLE);
273 ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
281 ret = phy_clear_bits(phydev, MII_CFG1, MII_CFG1_SLEEP_CONFIRM);
285 ret = phy_modify(phydev, MII_CFG2, MII_CFG2_SLEEP_REQUEST_TO,
286 MII_CFG2_SLEEP_REQUEST_TO_16MS);
290 ret = tja11xx_wakeup(phydev);
294 /* ACK interrupts by reading the status register */
295 ret = phy_read(phydev, MII_INTSRC);
302 static int tja11xx_read_status(struct phy_device *phydev)
306 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
307 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
309 ret = genphy_update_link(phydev);
313 ret = phy_read(phydev, MII_CFG1);
317 if (ret & MII_CFG1_MASTER_SLAVE)
318 phydev->master_slave_get = MASTER_SLAVE_CFG_MASTER_FORCE;
320 phydev->master_slave_get = MASTER_SLAVE_CFG_SLAVE_FORCE;
323 ret = phy_read(phydev, MII_COMMSTAT);
327 if (!(ret & MII_COMMSTAT_LINK_UP))
334 static int tja11xx_get_sqi(struct phy_device *phydev)
338 ret = phy_read(phydev, MII_COMMSTAT);
342 return FIELD_GET(MII_COMMSTAT_SQI_STATE, ret);
345 static int tja11xx_get_sqi_max(struct phy_device *phydev)
347 return MII_COMMSTAT_SQI_MAX;
350 static int tja11xx_get_sset_count(struct phy_device *phydev)
352 return ARRAY_SIZE(tja11xx_hw_stats);
355 static void tja11xx_get_strings(struct phy_device *phydev, u8 *data)
359 for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
360 strncpy(data + i * ETH_GSTRING_LEN,
361 tja11xx_hw_stats[i].string, ETH_GSTRING_LEN);
365 static void tja11xx_get_stats(struct phy_device *phydev,
366 struct ethtool_stats *stats, u64 *data)
370 for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
371 ret = phy_read(phydev, tja11xx_hw_stats[i].reg);
375 data[i] = ret & tja11xx_hw_stats[i].mask;
376 data[i] >>= tja11xx_hw_stats[i].off;
381 static int tja11xx_hwmon_read(struct device *dev,
382 enum hwmon_sensor_types type,
383 u32 attr, int channel, long *value)
385 struct phy_device *phydev = dev_get_drvdata(dev);
388 if (type == hwmon_in && attr == hwmon_in_lcrit_alarm) {
389 ret = phy_read(phydev, MII_INTSRC);
393 *value = !!(ret & MII_INTSRC_TEMP_ERR);
397 if (type == hwmon_temp && attr == hwmon_temp_crit_alarm) {
398 ret = phy_read(phydev, MII_INTSRC);
402 *value = !!(ret & MII_INTSRC_UV_ERR);
409 static umode_t tja11xx_hwmon_is_visible(const void *data,
410 enum hwmon_sensor_types type,
411 u32 attr, int channel)
413 if (type == hwmon_in && attr == hwmon_in_lcrit_alarm)
416 if (type == hwmon_temp && attr == hwmon_temp_crit_alarm)
422 static const struct hwmon_channel_info *tja11xx_hwmon_info[] = {
423 HWMON_CHANNEL_INFO(in, HWMON_I_LCRIT_ALARM),
424 HWMON_CHANNEL_INFO(temp, HWMON_T_CRIT_ALARM),
428 static const struct hwmon_ops tja11xx_hwmon_hwmon_ops = {
429 .is_visible = tja11xx_hwmon_is_visible,
430 .read = tja11xx_hwmon_read,
433 static const struct hwmon_chip_info tja11xx_hwmon_chip_info = {
434 .ops = &tja11xx_hwmon_hwmon_ops,
435 .info = tja11xx_hwmon_info,
438 static int tja11xx_hwmon_register(struct phy_device *phydev,
439 struct tja11xx_priv *priv)
441 struct device *dev = &phydev->mdio.dev;
444 priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
445 if (!priv->hwmon_name)
448 for (i = 0; priv->hwmon_name[i]; i++)
449 if (hwmon_is_bad_char(priv->hwmon_name[i]))
450 priv->hwmon_name[i] = '_';
453 devm_hwmon_device_register_with_info(dev, priv->hwmon_name,
455 &tja11xx_hwmon_chip_info,
458 return PTR_ERR_OR_ZERO(priv->hwmon_dev);
461 static int tja11xx_probe(struct phy_device *phydev)
463 struct device *dev = &phydev->mdio.dev;
464 struct tja11xx_priv *priv;
466 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
470 priv->phydev = phydev;
472 return tja11xx_hwmon_register(phydev, priv);
475 static void tja1102_p1_register(struct work_struct *work)
477 struct tja11xx_priv *priv = container_of(work, struct tja11xx_priv,
479 struct phy_device *phydev_phy0 = priv->phydev;
480 struct mii_bus *bus = phydev_phy0->mdio.bus;
481 struct device *dev = &phydev_phy0->mdio.dev;
482 struct device_node *np = dev->of_node;
483 struct device_node *child;
486 for_each_available_child_of_node(np, child) {
487 struct phy_device *phy;
490 addr = of_mdio_parse_addr(dev, child);
492 dev_err(dev, "Can't parse addr\n");
494 } else if (addr != phydev_phy0->mdio.addr + 1) {
495 /* Currently we care only about double PHY chip TJA1102.
496 * If some day NXP will decide to bring chips with more
497 * PHYs, this logic should be reworked.
499 dev_err(dev, "Unexpected address. Should be: %i\n",
500 phydev_phy0->mdio.addr + 1);
504 if (mdiobus_is_registered_device(bus, addr)) {
505 dev_err(dev, "device is already registered\n");
509 /* Real PHY ID of Port 1 is 0 */
510 phy = phy_device_create(bus, addr, PHY_ID_TJA1102, false, NULL);
512 dev_err(dev, "Can't create PHY device for Port 1: %i\n",
517 /* Overwrite parent device. phy_device_create() set parent to
518 * the mii_bus->dev, which is not correct in case.
520 phy->mdio.dev.parent = dev;
522 ret = of_mdiobus_phy_device_register(bus, phy, child, addr);
524 /* All resources needed for Port 1 should be already
525 * available for Port 0. Both ports use the same
526 * interrupt line, so -EPROBE_DEFER would make no sense
529 dev_err(dev, "Can't register Port 1. Unexpected error: %i\n",
531 phy_device_free(phy);
536 static int tja1102_p0_probe(struct phy_device *phydev)
538 struct device *dev = &phydev->mdio.dev;
539 struct tja11xx_priv *priv;
542 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
546 priv->phydev = phydev;
547 INIT_WORK(&priv->phy_register_work, tja1102_p1_register);
549 ret = tja11xx_hwmon_register(phydev, priv);
553 schedule_work(&priv->phy_register_work);
558 static int tja1102_match_phy_device(struct phy_device *phydev, bool port0)
562 if ((phydev->phy_id & PHY_ID_MASK) != PHY_ID_TJA1102)
565 ret = phy_read(phydev, MII_PHYSID2);
569 /* TJA1102 Port 1 has phyid 0 and doesn't support temperature
570 * and undervoltage alarms.
578 static int tja1102_p0_match_phy_device(struct phy_device *phydev)
580 return tja1102_match_phy_device(phydev, true);
583 static int tja1102_p1_match_phy_device(struct phy_device *phydev)
585 return tja1102_match_phy_device(phydev, false);
588 static int tja11xx_ack_interrupt(struct phy_device *phydev)
592 ret = phy_read(phydev, MII_INTSRC);
594 return (ret < 0) ? ret : 0;
597 static int tja11xx_config_intr(struct phy_device *phydev)
601 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
602 value = MII_INTEN_LINK_FAIL | MII_INTEN_LINK_UP;
604 return phy_write(phydev, MII_INTEN, value);
607 static int tja11xx_cable_test_start(struct phy_device *phydev)
611 ret = phy_clear_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
615 ret = tja11xx_wakeup(phydev);
619 ret = tja11xx_disable_link_control(phydev);
623 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CABLE_TEST);
627 * | BI_DA+ | BI_DA- | Result
628 * | open | open | open
629 * | + short to - | - short to + | short
630 * | short to Vdd | open | open
631 * | open | shot to Vdd | open
632 * | short to Vdd | short to Vdd | short
633 * | shot to GND | open | open
634 * | open | shot to GND | open
635 * | short to GND | shot to GND | short
636 * | connected to active link partner (master) | shot and open
638 static int tja11xx_cable_test_report_trans(u32 result)
640 u32 mask = MII_EXTSTAT_SHORT_DETECT | MII_EXTSTAT_OPEN_DETECT;
642 if ((result & mask) == mask) {
643 /* connected to active link partner (master) */
644 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
645 } else if ((result & mask) == 0) {
646 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
647 } else if (result & MII_EXTSTAT_SHORT_DETECT) {
648 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
649 } else if (result & MII_EXTSTAT_OPEN_DETECT) {
650 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
652 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
656 static int tja11xx_cable_test_report(struct phy_device *phydev)
660 ret = phy_read(phydev, MII_EXTSTAT);
664 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
665 tja11xx_cable_test_report_trans(ret));
670 static int tja11xx_cable_test_get_status(struct phy_device *phydev,
677 ret = phy_read(phydev, MII_ECTRL);
681 if (!(ret & MII_ECTRL_CABLE_TEST)) {
684 ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
688 return tja11xx_cable_test_report(phydev);
694 static struct phy_driver tja11xx_driver[] = {
696 PHY_ID_MATCH_MODEL(PHY_ID_TJA1100),
697 .name = "NXP TJA1100",
698 .features = PHY_BASIC_T1_FEATURES,
699 .probe = tja11xx_probe,
700 .soft_reset = tja11xx_soft_reset,
701 .config_aneg = tja11xx_config_aneg,
702 .config_init = tja11xx_config_init,
703 .read_status = tja11xx_read_status,
704 .get_sqi = tja11xx_get_sqi,
705 .get_sqi_max = tja11xx_get_sqi_max,
706 .suspend = genphy_suspend,
707 .resume = genphy_resume,
708 .set_loopback = genphy_loopback,
710 .get_sset_count = tja11xx_get_sset_count,
711 .get_strings = tja11xx_get_strings,
712 .get_stats = tja11xx_get_stats,
714 PHY_ID_MATCH_MODEL(PHY_ID_TJA1101),
715 .name = "NXP TJA1101",
716 .features = PHY_BASIC_T1_FEATURES,
717 .probe = tja11xx_probe,
718 .soft_reset = tja11xx_soft_reset,
719 .config_aneg = tja11xx_config_aneg,
720 .config_init = tja11xx_config_init,
721 .read_status = tja11xx_read_status,
722 .get_sqi = tja11xx_get_sqi,
723 .get_sqi_max = tja11xx_get_sqi_max,
724 .suspend = genphy_suspend,
725 .resume = genphy_resume,
726 .set_loopback = genphy_loopback,
728 .get_sset_count = tja11xx_get_sset_count,
729 .get_strings = tja11xx_get_strings,
730 .get_stats = tja11xx_get_stats,
732 .name = "NXP TJA1102 Port 0",
733 .features = PHY_BASIC_T1_FEATURES,
734 .flags = PHY_POLL_CABLE_TEST,
735 .probe = tja1102_p0_probe,
736 .soft_reset = tja11xx_soft_reset,
737 .config_aneg = tja11xx_config_aneg,
738 .config_init = tja11xx_config_init,
739 .read_status = tja11xx_read_status,
740 .get_sqi = tja11xx_get_sqi,
741 .get_sqi_max = tja11xx_get_sqi_max,
742 .match_phy_device = tja1102_p0_match_phy_device,
743 .suspend = genphy_suspend,
744 .resume = genphy_resume,
745 .set_loopback = genphy_loopback,
747 .get_sset_count = tja11xx_get_sset_count,
748 .get_strings = tja11xx_get_strings,
749 .get_stats = tja11xx_get_stats,
750 .ack_interrupt = tja11xx_ack_interrupt,
751 .config_intr = tja11xx_config_intr,
752 .cable_test_start = tja11xx_cable_test_start,
753 .cable_test_get_status = tja11xx_cable_test_get_status,
755 .name = "NXP TJA1102 Port 1",
756 .features = PHY_BASIC_T1_FEATURES,
757 .flags = PHY_POLL_CABLE_TEST,
758 /* currently no probe for Port 1 is need */
759 .soft_reset = tja11xx_soft_reset,
760 .config_aneg = tja11xx_config_aneg,
761 .config_init = tja11xx_config_init,
762 .read_status = tja11xx_read_status,
763 .get_sqi = tja11xx_get_sqi,
764 .get_sqi_max = tja11xx_get_sqi_max,
765 .match_phy_device = tja1102_p1_match_phy_device,
766 .suspend = genphy_suspend,
767 .resume = genphy_resume,
768 .set_loopback = genphy_loopback,
770 .get_sset_count = tja11xx_get_sset_count,
771 .get_strings = tja11xx_get_strings,
772 .get_stats = tja11xx_get_stats,
773 .ack_interrupt = tja11xx_ack_interrupt,
774 .config_intr = tja11xx_config_intr,
775 .cable_test_start = tja11xx_cable_test_start,
776 .cable_test_get_status = tja11xx_cable_test_get_status,
780 module_phy_driver(tja11xx_driver);
782 static struct mdio_device_id __maybe_unused tja11xx_tbl[] = {
783 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1100) },
784 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1101) },
785 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1102) },
789 MODULE_DEVICE_TABLE(mdio, tja11xx_tbl);
791 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
792 MODULE_DESCRIPTION("NXP TJA11xx BoardR-Reach PHY driver");
793 MODULE_LICENSE("GPL");