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_LINK_FAIL BIT(10)
48 #define MII_INTSRC_LINK_UP BIT(9)
49 #define MII_INTSRC_MASK (MII_INTSRC_LINK_FAIL | MII_INTSRC_LINK_UP)
50 #define MII_INTSRC_UV_ERR BIT(3)
51 #define MII_INTSRC_TEMP_ERR BIT(1)
54 #define MII_INTEN_LINK_FAIL BIT(10)
55 #define MII_INTEN_LINK_UP BIT(9)
56 #define MII_INTEN_UV_ERR BIT(3)
57 #define MII_INTEN_TEMP_ERR BIT(1)
59 #define MII_COMMSTAT 23
60 #define MII_COMMSTAT_LINK_UP BIT(15)
61 #define MII_COMMSTAT_SQI_STATE GENMASK(7, 5)
62 #define MII_COMMSTAT_SQI_MAX 7
64 #define MII_GENSTAT 24
65 #define MII_GENSTAT_PLL_LOCKED BIT(14)
67 #define MII_EXTSTAT 25
68 #define MII_EXTSTAT_SHORT_DETECT BIT(8)
69 #define MII_EXTSTAT_OPEN_DETECT BIT(7)
70 #define MII_EXTSTAT_POLARITY_DETECT BIT(6)
72 #define MII_COMMCFG 27
73 #define MII_COMMCFG_AUTO_OP BIT(15)
77 struct device *hwmon_dev;
78 struct phy_device *phydev;
79 struct work_struct phy_register_work;
82 struct tja11xx_phy_stats {
89 static struct tja11xx_phy_stats tja11xx_hw_stats[] = {
90 { "phy_symbol_error_count", 20, 0, GENMASK(15, 0) },
91 { "phy_polarity_detect", 25, 6, BIT(6) },
92 { "phy_open_detect", 25, 7, BIT(7) },
93 { "phy_short_detect", 25, 8, BIT(8) },
94 { "phy_rem_rcvr_count", 26, 0, GENMASK(7, 0) },
95 { "phy_loc_rcvr_count", 26, 8, GENMASK(15, 8) },
98 static int tja11xx_check(struct phy_device *phydev, u8 reg, u16 mask, u16 set)
102 return phy_read_poll_timeout(phydev, reg, val, (val & mask) == set,
106 static int phy_modify_check(struct phy_device *phydev, u8 reg,
111 ret = phy_modify(phydev, reg, mask, set);
115 return tja11xx_check(phydev, reg, mask, set);
118 static int tja11xx_enable_reg_write(struct phy_device *phydev)
120 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CONFIG_EN);
123 static int tja11xx_enable_link_control(struct phy_device *phydev)
125 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
128 static int tja11xx_disable_link_control(struct phy_device *phydev)
130 return phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
133 static int tja11xx_wakeup(struct phy_device *phydev)
137 ret = phy_read(phydev, MII_ECTRL);
141 switch (ret & MII_ECTRL_POWER_MODE_MASK) {
142 case MII_ECTRL_POWER_MODE_NO_CHANGE:
144 case MII_ECTRL_POWER_MODE_NORMAL:
145 ret = phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
149 ret = phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
153 case MII_ECTRL_POWER_MODE_STANDBY:
154 ret = phy_modify_check(phydev, MII_ECTRL,
155 MII_ECTRL_POWER_MODE_MASK,
156 MII_ECTRL_POWER_MODE_STANDBY);
160 ret = phy_modify(phydev, MII_ECTRL, MII_ECTRL_POWER_MODE_MASK,
161 MII_ECTRL_POWER_MODE_NORMAL);
165 ret = phy_modify_check(phydev, MII_GENSTAT,
166 MII_GENSTAT_PLL_LOCKED,
167 MII_GENSTAT_PLL_LOCKED);
171 return tja11xx_enable_link_control(phydev);
179 static int tja11xx_soft_reset(struct phy_device *phydev)
183 ret = tja11xx_enable_reg_write(phydev);
187 return genphy_soft_reset(phydev);
190 static int tja11xx_config_aneg_cable_test(struct phy_device *phydev)
192 bool finished = false;
198 if (!phydev->drv->cable_test_start ||
199 !phydev->drv->cable_test_get_status)
202 ret = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
206 ret = phydev->drv->cable_test_start(phydev);
210 /* According to the documentation this test takes 100 usec */
211 usleep_range(100, 200);
213 ret = phydev->drv->cable_test_get_status(phydev, &finished);
218 ethnl_cable_test_finished(phydev);
223 static int tja11xx_config_aneg(struct phy_device *phydev)
225 int ret, changed = 0;
228 switch (phydev->master_slave_set) {
229 case MASTER_SLAVE_CFG_MASTER_FORCE:
230 ctl |= MII_CFG1_MASTER_SLAVE;
232 case MASTER_SLAVE_CFG_SLAVE_FORCE:
234 case MASTER_SLAVE_CFG_UNKNOWN:
235 case MASTER_SLAVE_CFG_UNSUPPORTED:
238 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
242 changed = phy_modify_changed(phydev, MII_CFG1, MII_CFG1_MASTER_SLAVE, ctl);
247 ret = tja11xx_config_aneg_cable_test(phydev);
251 return __genphy_config_aneg(phydev, changed);
254 static int tja11xx_config_init(struct phy_device *phydev)
258 ret = tja11xx_enable_reg_write(phydev);
262 phydev->autoneg = AUTONEG_DISABLE;
263 phydev->speed = SPEED_100;
264 phydev->duplex = DUPLEX_FULL;
266 switch (phydev->phy_id & PHY_ID_MASK) {
268 ret = phy_modify(phydev, MII_CFG1,
269 MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_MASK |
271 MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_LINKUP |
272 MII_CFG1_LED_ENABLE);
278 ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
286 ret = phy_clear_bits(phydev, MII_CFG1, MII_CFG1_SLEEP_CONFIRM);
290 ret = phy_modify(phydev, MII_CFG2, MII_CFG2_SLEEP_REQUEST_TO,
291 MII_CFG2_SLEEP_REQUEST_TO_16MS);
295 ret = tja11xx_wakeup(phydev);
299 /* ACK interrupts by reading the status register */
300 ret = phy_read(phydev, MII_INTSRC);
307 static int tja11xx_read_status(struct phy_device *phydev)
311 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
312 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
314 ret = genphy_update_link(phydev);
318 ret = phy_read(phydev, MII_CFG1);
322 if (ret & MII_CFG1_MASTER_SLAVE)
323 phydev->master_slave_get = MASTER_SLAVE_CFG_MASTER_FORCE;
325 phydev->master_slave_get = MASTER_SLAVE_CFG_SLAVE_FORCE;
328 ret = phy_read(phydev, MII_COMMSTAT);
332 if (!(ret & MII_COMMSTAT_LINK_UP))
339 static int tja11xx_get_sqi(struct phy_device *phydev)
343 ret = phy_read(phydev, MII_COMMSTAT);
347 return FIELD_GET(MII_COMMSTAT_SQI_STATE, ret);
350 static int tja11xx_get_sqi_max(struct phy_device *phydev)
352 return MII_COMMSTAT_SQI_MAX;
355 static int tja11xx_get_sset_count(struct phy_device *phydev)
357 return ARRAY_SIZE(tja11xx_hw_stats);
360 static void tja11xx_get_strings(struct phy_device *phydev, u8 *data)
364 for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
365 strncpy(data + i * ETH_GSTRING_LEN,
366 tja11xx_hw_stats[i].string, ETH_GSTRING_LEN);
370 static void tja11xx_get_stats(struct phy_device *phydev,
371 struct ethtool_stats *stats, u64 *data)
375 for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
376 ret = phy_read(phydev, tja11xx_hw_stats[i].reg);
380 data[i] = ret & tja11xx_hw_stats[i].mask;
381 data[i] >>= tja11xx_hw_stats[i].off;
386 static int tja11xx_hwmon_read(struct device *dev,
387 enum hwmon_sensor_types type,
388 u32 attr, int channel, long *value)
390 struct phy_device *phydev = dev_get_drvdata(dev);
393 if (type == hwmon_in && attr == hwmon_in_lcrit_alarm) {
394 ret = phy_read(phydev, MII_INTSRC);
398 *value = !!(ret & MII_INTSRC_TEMP_ERR);
402 if (type == hwmon_temp && attr == hwmon_temp_crit_alarm) {
403 ret = phy_read(phydev, MII_INTSRC);
407 *value = !!(ret & MII_INTSRC_UV_ERR);
414 static umode_t tja11xx_hwmon_is_visible(const void *data,
415 enum hwmon_sensor_types type,
416 u32 attr, int channel)
418 if (type == hwmon_in && attr == hwmon_in_lcrit_alarm)
421 if (type == hwmon_temp && attr == hwmon_temp_crit_alarm)
427 static const struct hwmon_channel_info *tja11xx_hwmon_info[] = {
428 HWMON_CHANNEL_INFO(in, HWMON_I_LCRIT_ALARM),
429 HWMON_CHANNEL_INFO(temp, HWMON_T_CRIT_ALARM),
433 static const struct hwmon_ops tja11xx_hwmon_hwmon_ops = {
434 .is_visible = tja11xx_hwmon_is_visible,
435 .read = tja11xx_hwmon_read,
438 static const struct hwmon_chip_info tja11xx_hwmon_chip_info = {
439 .ops = &tja11xx_hwmon_hwmon_ops,
440 .info = tja11xx_hwmon_info,
443 static int tja11xx_hwmon_register(struct phy_device *phydev,
444 struct tja11xx_priv *priv)
446 struct device *dev = &phydev->mdio.dev;
448 priv->hwmon_name = devm_hwmon_sanitize_name(dev, dev_name(dev));
449 if (IS_ERR(priv->hwmon_name))
450 return PTR_ERR(priv->hwmon_name);
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)
602 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
603 err = tja11xx_ack_interrupt(phydev);
607 value = MII_INTEN_LINK_FAIL | MII_INTEN_LINK_UP |
608 MII_INTEN_UV_ERR | MII_INTEN_TEMP_ERR;
609 err = phy_write(phydev, MII_INTEN, value);
611 err = phy_write(phydev, MII_INTEN, value);
615 err = tja11xx_ack_interrupt(phydev);
621 static irqreturn_t tja11xx_handle_interrupt(struct phy_device *phydev)
623 struct device *dev = &phydev->mdio.dev;
626 irq_status = phy_read(phydev, MII_INTSRC);
627 if (irq_status < 0) {
632 if (irq_status & MII_INTSRC_TEMP_ERR)
633 dev_warn(dev, "Overtemperature error detected (temp > 155C°).\n");
634 if (irq_status & MII_INTSRC_UV_ERR)
635 dev_warn(dev, "Undervoltage error detected.\n");
637 if (!(irq_status & MII_INTSRC_MASK))
640 phy_trigger_machine(phydev);
645 static int tja11xx_cable_test_start(struct phy_device *phydev)
649 ret = phy_clear_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
653 ret = tja11xx_wakeup(phydev);
657 ret = tja11xx_disable_link_control(phydev);
661 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CABLE_TEST);
665 * | BI_DA+ | BI_DA- | Result
666 * | open | open | open
667 * | + short to - | - short to + | short
668 * | short to Vdd | open | open
669 * | open | shot to Vdd | open
670 * | short to Vdd | short to Vdd | short
671 * | shot to GND | open | open
672 * | open | shot to GND | open
673 * | short to GND | shot to GND | short
674 * | connected to active link partner (master) | shot and open
676 static int tja11xx_cable_test_report_trans(u32 result)
678 u32 mask = MII_EXTSTAT_SHORT_DETECT | MII_EXTSTAT_OPEN_DETECT;
680 if ((result & mask) == mask) {
681 /* connected to active link partner (master) */
682 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
683 } else if ((result & mask) == 0) {
684 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
685 } else if (result & MII_EXTSTAT_SHORT_DETECT) {
686 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
687 } else if (result & MII_EXTSTAT_OPEN_DETECT) {
688 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
690 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
694 static int tja11xx_cable_test_report(struct phy_device *phydev)
698 ret = phy_read(phydev, MII_EXTSTAT);
702 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
703 tja11xx_cable_test_report_trans(ret));
708 static int tja11xx_cable_test_get_status(struct phy_device *phydev,
715 ret = phy_read(phydev, MII_ECTRL);
719 if (!(ret & MII_ECTRL_CABLE_TEST)) {
722 ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
726 return tja11xx_cable_test_report(phydev);
732 static struct phy_driver tja11xx_driver[] = {
734 PHY_ID_MATCH_MODEL(PHY_ID_TJA1100),
735 .name = "NXP TJA1100",
736 .features = PHY_BASIC_T1_FEATURES,
737 .probe = tja11xx_probe,
738 .soft_reset = tja11xx_soft_reset,
739 .config_aneg = tja11xx_config_aneg,
740 .config_init = tja11xx_config_init,
741 .read_status = tja11xx_read_status,
742 .get_sqi = tja11xx_get_sqi,
743 .get_sqi_max = tja11xx_get_sqi_max,
744 .suspend = genphy_suspend,
745 .resume = genphy_resume,
746 .set_loopback = genphy_loopback,
748 .get_sset_count = tja11xx_get_sset_count,
749 .get_strings = tja11xx_get_strings,
750 .get_stats = tja11xx_get_stats,
752 PHY_ID_MATCH_MODEL(PHY_ID_TJA1101),
753 .name = "NXP TJA1101",
754 .features = PHY_BASIC_T1_FEATURES,
755 .probe = tja11xx_probe,
756 .soft_reset = tja11xx_soft_reset,
757 .config_aneg = tja11xx_config_aneg,
758 .config_init = tja11xx_config_init,
759 .read_status = tja11xx_read_status,
760 .get_sqi = tja11xx_get_sqi,
761 .get_sqi_max = tja11xx_get_sqi_max,
762 .suspend = genphy_suspend,
763 .resume = genphy_resume,
764 .set_loopback = genphy_loopback,
766 .get_sset_count = tja11xx_get_sset_count,
767 .get_strings = tja11xx_get_strings,
768 .get_stats = tja11xx_get_stats,
770 .name = "NXP TJA1102 Port 0",
771 .features = PHY_BASIC_T1_FEATURES,
772 .flags = PHY_POLL_CABLE_TEST,
773 .probe = tja1102_p0_probe,
774 .soft_reset = tja11xx_soft_reset,
775 .config_aneg = tja11xx_config_aneg,
776 .config_init = tja11xx_config_init,
777 .read_status = tja11xx_read_status,
778 .get_sqi = tja11xx_get_sqi,
779 .get_sqi_max = tja11xx_get_sqi_max,
780 .match_phy_device = tja1102_p0_match_phy_device,
781 .suspend = genphy_suspend,
782 .resume = genphy_resume,
783 .set_loopback = genphy_loopback,
785 .get_sset_count = tja11xx_get_sset_count,
786 .get_strings = tja11xx_get_strings,
787 .get_stats = tja11xx_get_stats,
788 .config_intr = tja11xx_config_intr,
789 .handle_interrupt = tja11xx_handle_interrupt,
790 .cable_test_start = tja11xx_cable_test_start,
791 .cable_test_get_status = tja11xx_cable_test_get_status,
793 .name = "NXP TJA1102 Port 1",
794 .features = PHY_BASIC_T1_FEATURES,
795 .flags = PHY_POLL_CABLE_TEST,
796 /* currently no probe for Port 1 is need */
797 .soft_reset = tja11xx_soft_reset,
798 .config_aneg = tja11xx_config_aneg,
799 .config_init = tja11xx_config_init,
800 .read_status = tja11xx_read_status,
801 .get_sqi = tja11xx_get_sqi,
802 .get_sqi_max = tja11xx_get_sqi_max,
803 .match_phy_device = tja1102_p1_match_phy_device,
804 .suspend = genphy_suspend,
805 .resume = genphy_resume,
806 .set_loopback = genphy_loopback,
808 .get_sset_count = tja11xx_get_sset_count,
809 .get_strings = tja11xx_get_strings,
810 .get_stats = tja11xx_get_stats,
811 .config_intr = tja11xx_config_intr,
812 .handle_interrupt = tja11xx_handle_interrupt,
813 .cable_test_start = tja11xx_cable_test_start,
814 .cable_test_get_status = tja11xx_cable_test_get_status,
818 module_phy_driver(tja11xx_driver);
820 static struct mdio_device_id __maybe_unused tja11xx_tbl[] = {
821 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1100) },
822 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1101) },
823 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1102) },
827 MODULE_DEVICE_TABLE(mdio, tja11xx_tbl);
829 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
830 MODULE_DESCRIPTION("NXP TJA11xx BoardR-Reach PHY driver");
831 MODULE_LICENSE("GPL");