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;
449 priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
450 if (!priv->hwmon_name)
453 for (i = 0; priv->hwmon_name[i]; i++)
454 if (hwmon_is_bad_char(priv->hwmon_name[i]))
455 priv->hwmon_name[i] = '_';
458 devm_hwmon_device_register_with_info(dev, priv->hwmon_name,
460 &tja11xx_hwmon_chip_info,
463 return PTR_ERR_OR_ZERO(priv->hwmon_dev);
466 static int tja11xx_probe(struct phy_device *phydev)
468 struct device *dev = &phydev->mdio.dev;
469 struct tja11xx_priv *priv;
471 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
475 priv->phydev = phydev;
477 return tja11xx_hwmon_register(phydev, priv);
480 static void tja1102_p1_register(struct work_struct *work)
482 struct tja11xx_priv *priv = container_of(work, struct tja11xx_priv,
484 struct phy_device *phydev_phy0 = priv->phydev;
485 struct mii_bus *bus = phydev_phy0->mdio.bus;
486 struct device *dev = &phydev_phy0->mdio.dev;
487 struct device_node *np = dev->of_node;
488 struct device_node *child;
491 for_each_available_child_of_node(np, child) {
492 struct phy_device *phy;
495 addr = of_mdio_parse_addr(dev, child);
497 dev_err(dev, "Can't parse addr\n");
499 } else if (addr != phydev_phy0->mdio.addr + 1) {
500 /* Currently we care only about double PHY chip TJA1102.
501 * If some day NXP will decide to bring chips with more
502 * PHYs, this logic should be reworked.
504 dev_err(dev, "Unexpected address. Should be: %i\n",
505 phydev_phy0->mdio.addr + 1);
509 if (mdiobus_is_registered_device(bus, addr)) {
510 dev_err(dev, "device is already registered\n");
514 /* Real PHY ID of Port 1 is 0 */
515 phy = phy_device_create(bus, addr, PHY_ID_TJA1102, false, NULL);
517 dev_err(dev, "Can't create PHY device for Port 1: %i\n",
522 /* Overwrite parent device. phy_device_create() set parent to
523 * the mii_bus->dev, which is not correct in case.
525 phy->mdio.dev.parent = dev;
527 ret = of_mdiobus_phy_device_register(bus, phy, child, addr);
529 /* All resources needed for Port 1 should be already
530 * available for Port 0. Both ports use the same
531 * interrupt line, so -EPROBE_DEFER would make no sense
534 dev_err(dev, "Can't register Port 1. Unexpected error: %i\n",
536 phy_device_free(phy);
541 static int tja1102_p0_probe(struct phy_device *phydev)
543 struct device *dev = &phydev->mdio.dev;
544 struct tja11xx_priv *priv;
547 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
551 priv->phydev = phydev;
552 INIT_WORK(&priv->phy_register_work, tja1102_p1_register);
554 ret = tja11xx_hwmon_register(phydev, priv);
558 schedule_work(&priv->phy_register_work);
563 static int tja1102_match_phy_device(struct phy_device *phydev, bool port0)
567 if ((phydev->phy_id & PHY_ID_MASK) != PHY_ID_TJA1102)
570 ret = phy_read(phydev, MII_PHYSID2);
574 /* TJA1102 Port 1 has phyid 0 and doesn't support temperature
575 * and undervoltage alarms.
583 static int tja1102_p0_match_phy_device(struct phy_device *phydev)
585 return tja1102_match_phy_device(phydev, true);
588 static int tja1102_p1_match_phy_device(struct phy_device *phydev)
590 return tja1102_match_phy_device(phydev, false);
593 static int tja11xx_ack_interrupt(struct phy_device *phydev)
597 ret = phy_read(phydev, MII_INTSRC);
599 return (ret < 0) ? ret : 0;
602 static int tja11xx_config_intr(struct phy_device *phydev)
607 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
608 err = tja11xx_ack_interrupt(phydev);
612 value = MII_INTEN_LINK_FAIL | MII_INTEN_LINK_UP |
613 MII_INTEN_UV_ERR | MII_INTEN_TEMP_ERR;
614 err = phy_write(phydev, MII_INTEN, value);
616 err = phy_write(phydev, MII_INTEN, value);
620 err = tja11xx_ack_interrupt(phydev);
626 static irqreturn_t tja11xx_handle_interrupt(struct phy_device *phydev)
628 struct device *dev = &phydev->mdio.dev;
631 irq_status = phy_read(phydev, MII_INTSRC);
632 if (irq_status < 0) {
637 if (irq_status & MII_INTSRC_TEMP_ERR)
638 dev_warn(dev, "Overtemperature error detected (temp > 155C°).\n");
639 if (irq_status & MII_INTSRC_UV_ERR)
640 dev_warn(dev, "Undervoltage error detected.\n");
642 if (!(irq_status & MII_INTSRC_MASK))
645 phy_trigger_machine(phydev);
650 static int tja11xx_cable_test_start(struct phy_device *phydev)
654 ret = phy_clear_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
658 ret = tja11xx_wakeup(phydev);
662 ret = tja11xx_disable_link_control(phydev);
666 return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CABLE_TEST);
670 * | BI_DA+ | BI_DA- | Result
671 * | open | open | open
672 * | + short to - | - short to + | short
673 * | short to Vdd | open | open
674 * | open | shot to Vdd | open
675 * | short to Vdd | short to Vdd | short
676 * | shot to GND | open | open
677 * | open | shot to GND | open
678 * | short to GND | shot to GND | short
679 * | connected to active link partner (master) | shot and open
681 static int tja11xx_cable_test_report_trans(u32 result)
683 u32 mask = MII_EXTSTAT_SHORT_DETECT | MII_EXTSTAT_OPEN_DETECT;
685 if ((result & mask) == mask) {
686 /* connected to active link partner (master) */
687 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
688 } else if ((result & mask) == 0) {
689 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
690 } else if (result & MII_EXTSTAT_SHORT_DETECT) {
691 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
692 } else if (result & MII_EXTSTAT_OPEN_DETECT) {
693 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
695 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
699 static int tja11xx_cable_test_report(struct phy_device *phydev)
703 ret = phy_read(phydev, MII_EXTSTAT);
707 ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
708 tja11xx_cable_test_report_trans(ret));
713 static int tja11xx_cable_test_get_status(struct phy_device *phydev,
720 ret = phy_read(phydev, MII_ECTRL);
724 if (!(ret & MII_ECTRL_CABLE_TEST)) {
727 ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
731 return tja11xx_cable_test_report(phydev);
737 static struct phy_driver tja11xx_driver[] = {
739 PHY_ID_MATCH_MODEL(PHY_ID_TJA1100),
740 .name = "NXP TJA1100",
741 .features = PHY_BASIC_T1_FEATURES,
742 .probe = tja11xx_probe,
743 .soft_reset = tja11xx_soft_reset,
744 .config_aneg = tja11xx_config_aneg,
745 .config_init = tja11xx_config_init,
746 .read_status = tja11xx_read_status,
747 .get_sqi = tja11xx_get_sqi,
748 .get_sqi_max = tja11xx_get_sqi_max,
749 .suspend = genphy_suspend,
750 .resume = genphy_resume,
751 .set_loopback = genphy_loopback,
753 .get_sset_count = tja11xx_get_sset_count,
754 .get_strings = tja11xx_get_strings,
755 .get_stats = tja11xx_get_stats,
757 PHY_ID_MATCH_MODEL(PHY_ID_TJA1101),
758 .name = "NXP TJA1101",
759 .features = PHY_BASIC_T1_FEATURES,
760 .probe = tja11xx_probe,
761 .soft_reset = tja11xx_soft_reset,
762 .config_aneg = tja11xx_config_aneg,
763 .config_init = tja11xx_config_init,
764 .read_status = tja11xx_read_status,
765 .get_sqi = tja11xx_get_sqi,
766 .get_sqi_max = tja11xx_get_sqi_max,
767 .suspend = genphy_suspend,
768 .resume = genphy_resume,
769 .set_loopback = genphy_loopback,
771 .get_sset_count = tja11xx_get_sset_count,
772 .get_strings = tja11xx_get_strings,
773 .get_stats = tja11xx_get_stats,
775 .name = "NXP TJA1102 Port 0",
776 .features = PHY_BASIC_T1_FEATURES,
777 .flags = PHY_POLL_CABLE_TEST,
778 .probe = tja1102_p0_probe,
779 .soft_reset = tja11xx_soft_reset,
780 .config_aneg = tja11xx_config_aneg,
781 .config_init = tja11xx_config_init,
782 .read_status = tja11xx_read_status,
783 .get_sqi = tja11xx_get_sqi,
784 .get_sqi_max = tja11xx_get_sqi_max,
785 .match_phy_device = tja1102_p0_match_phy_device,
786 .suspend = genphy_suspend,
787 .resume = genphy_resume,
788 .set_loopback = genphy_loopback,
790 .get_sset_count = tja11xx_get_sset_count,
791 .get_strings = tja11xx_get_strings,
792 .get_stats = tja11xx_get_stats,
793 .config_intr = tja11xx_config_intr,
794 .handle_interrupt = tja11xx_handle_interrupt,
795 .cable_test_start = tja11xx_cable_test_start,
796 .cable_test_get_status = tja11xx_cable_test_get_status,
798 .name = "NXP TJA1102 Port 1",
799 .features = PHY_BASIC_T1_FEATURES,
800 .flags = PHY_POLL_CABLE_TEST,
801 /* currently no probe for Port 1 is need */
802 .soft_reset = tja11xx_soft_reset,
803 .config_aneg = tja11xx_config_aneg,
804 .config_init = tja11xx_config_init,
805 .read_status = tja11xx_read_status,
806 .get_sqi = tja11xx_get_sqi,
807 .get_sqi_max = tja11xx_get_sqi_max,
808 .match_phy_device = tja1102_p1_match_phy_device,
809 .suspend = genphy_suspend,
810 .resume = genphy_resume,
811 .set_loopback = genphy_loopback,
813 .get_sset_count = tja11xx_get_sset_count,
814 .get_strings = tja11xx_get_strings,
815 .get_stats = tja11xx_get_stats,
816 .config_intr = tja11xx_config_intr,
817 .handle_interrupt = tja11xx_handle_interrupt,
818 .cable_test_start = tja11xx_cable_test_start,
819 .cable_test_get_status = tja11xx_cable_test_get_status,
823 module_phy_driver(tja11xx_driver);
825 static struct mdio_device_id __maybe_unused tja11xx_tbl[] = {
826 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1100) },
827 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1101) },
828 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1102) },
832 MODULE_DEVICE_TABLE(mdio, tja11xx_tbl);
834 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
835 MODULE_DESCRIPTION("NXP TJA11xx BoardR-Reach PHY driver");
836 MODULE_LICENSE("GPL");