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/kernel.h>
10 #include <linux/module.h>
11 #include <linux/phy.h>
12 #include <linux/hwmon.h>
13 #include <linux/bitfield.h>
15 #define PHY_ID_MASK 0xfffffff0
16 #define PHY_ID_TJA1100 0x0180dc40
17 #define PHY_ID_TJA1101 0x0180dd00
20 #define MII_ECTRL_LINK_CONTROL BIT(15)
21 #define MII_ECTRL_POWER_MODE_MASK GENMASK(14, 11)
22 #define MII_ECTRL_POWER_MODE_NO_CHANGE (0x0 << 11)
23 #define MII_ECTRL_POWER_MODE_NORMAL (0x3 << 11)
24 #define MII_ECTRL_POWER_MODE_STANDBY (0xc << 11)
25 #define MII_ECTRL_CONFIG_EN BIT(2)
26 #define MII_ECTRL_WAKE_REQUEST BIT(0)
29 #define MII_CFG1_AUTO_OP BIT(14)
30 #define MII_CFG1_SLEEP_CONFIRM BIT(6)
31 #define MII_CFG1_LED_MODE_MASK GENMASK(5, 4)
32 #define MII_CFG1_LED_MODE_LINKUP 0
33 #define MII_CFG1_LED_ENABLE BIT(3)
36 #define MII_CFG2_SLEEP_REQUEST_TO GENMASK(1, 0)
37 #define MII_CFG2_SLEEP_REQUEST_TO_16MS 0x3
40 #define MII_INTSRC_TEMP_ERR BIT(1)
41 #define MII_INTSRC_UV_ERR BIT(3)
43 #define MII_COMMSTAT 23
44 #define MII_COMMSTAT_LINK_UP BIT(15)
46 #define MII_GENSTAT 24
47 #define MII_GENSTAT_PLL_LOCKED BIT(14)
49 #define MII_COMMCFG 27
50 #define MII_COMMCFG_AUTO_OP BIT(15)
54 struct device
*hwmon_dev
;
57 struct tja11xx_phy_stats
{
64 static struct tja11xx_phy_stats tja11xx_hw_stats
[] = {
65 { "phy_symbol_error_count", 20, 0, GENMASK(15, 0) },
66 { "phy_polarity_detect", 25, 6, BIT(6) },
67 { "phy_open_detect", 25, 7, BIT(7) },
68 { "phy_short_detect", 25, 8, BIT(8) },
69 { "phy_rem_rcvr_count", 26, 0, GENMASK(7, 0) },
70 { "phy_loc_rcvr_count", 26, 8, GENMASK(15, 8) },
73 static int tja11xx_check(struct phy_device
*phydev
, u8 reg
, u16 mask
, u16 set
)
77 for (i
= 0; i
< 200; i
++) {
78 ret
= phy_read(phydev
, reg
);
82 if ((ret
& mask
) == set
)
85 usleep_range(100, 150);
91 static int phy_modify_check(struct phy_device
*phydev
, u8 reg
,
96 ret
= phy_modify(phydev
, reg
, mask
, set
);
100 return tja11xx_check(phydev
, reg
, mask
, set
);
103 static int tja11xx_enable_reg_write(struct phy_device
*phydev
)
105 return phy_set_bits(phydev
, MII_ECTRL
, MII_ECTRL_CONFIG_EN
);
108 static int tja11xx_enable_link_control(struct phy_device
*phydev
)
110 return phy_set_bits(phydev
, MII_ECTRL
, MII_ECTRL_LINK_CONTROL
);
113 static int tja11xx_wakeup(struct phy_device
*phydev
)
117 ret
= phy_read(phydev
, MII_ECTRL
);
121 switch (ret
& MII_ECTRL_POWER_MODE_MASK
) {
122 case MII_ECTRL_POWER_MODE_NO_CHANGE
:
124 case MII_ECTRL_POWER_MODE_NORMAL
:
125 ret
= phy_set_bits(phydev
, MII_ECTRL
, MII_ECTRL_WAKE_REQUEST
);
129 ret
= phy_clear_bits(phydev
, MII_ECTRL
, MII_ECTRL_WAKE_REQUEST
);
133 case MII_ECTRL_POWER_MODE_STANDBY
:
134 ret
= phy_modify_check(phydev
, MII_ECTRL
,
135 MII_ECTRL_POWER_MODE_MASK
,
136 MII_ECTRL_POWER_MODE_STANDBY
);
140 ret
= phy_modify(phydev
, MII_ECTRL
, MII_ECTRL_POWER_MODE_MASK
,
141 MII_ECTRL_POWER_MODE_NORMAL
);
145 ret
= phy_modify_check(phydev
, MII_GENSTAT
,
146 MII_GENSTAT_PLL_LOCKED
,
147 MII_GENSTAT_PLL_LOCKED
);
151 return tja11xx_enable_link_control(phydev
);
159 static int tja11xx_soft_reset(struct phy_device
*phydev
)
163 ret
= tja11xx_enable_reg_write(phydev
);
167 return genphy_soft_reset(phydev
);
170 static int tja11xx_config_init(struct phy_device
*phydev
)
174 ret
= tja11xx_enable_reg_write(phydev
);
178 phydev
->autoneg
= AUTONEG_DISABLE
;
179 phydev
->speed
= SPEED_100
;
180 phydev
->duplex
= DUPLEX_FULL
;
182 switch (phydev
->phy_id
& PHY_ID_MASK
) {
184 ret
= phy_modify(phydev
, MII_CFG1
,
185 MII_CFG1_AUTO_OP
| MII_CFG1_LED_MODE_MASK
|
187 MII_CFG1_AUTO_OP
| MII_CFG1_LED_MODE_LINKUP
|
188 MII_CFG1_LED_ENABLE
);
193 ret
= phy_set_bits(phydev
, MII_COMMCFG
, MII_COMMCFG_AUTO_OP
);
201 ret
= phy_clear_bits(phydev
, MII_CFG1
, MII_CFG1_SLEEP_CONFIRM
);
205 ret
= phy_modify(phydev
, MII_CFG2
, MII_CFG2_SLEEP_REQUEST_TO
,
206 MII_CFG2_SLEEP_REQUEST_TO_16MS
);
210 ret
= tja11xx_wakeup(phydev
);
214 /* ACK interrupts by reading the status register */
215 ret
= phy_read(phydev
, MII_INTSRC
);
222 static int tja11xx_read_status(struct phy_device
*phydev
)
226 ret
= genphy_update_link(phydev
);
231 ret
= phy_read(phydev
, MII_COMMSTAT
);
235 if (!(ret
& MII_COMMSTAT_LINK_UP
))
242 static int tja11xx_get_sset_count(struct phy_device
*phydev
)
244 return ARRAY_SIZE(tja11xx_hw_stats
);
247 static void tja11xx_get_strings(struct phy_device
*phydev
, u8
*data
)
251 for (i
= 0; i
< ARRAY_SIZE(tja11xx_hw_stats
); i
++) {
252 strncpy(data
+ i
* ETH_GSTRING_LEN
,
253 tja11xx_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
257 static void tja11xx_get_stats(struct phy_device
*phydev
,
258 struct ethtool_stats
*stats
, u64
*data
)
262 for (i
= 0; i
< ARRAY_SIZE(tja11xx_hw_stats
); i
++) {
263 ret
= phy_read(phydev
, tja11xx_hw_stats
[i
].reg
);
267 data
[i
] = ret
& tja11xx_hw_stats
[i
].mask
;
268 data
[i
] >>= tja11xx_hw_stats
[i
].off
;
273 static int tja11xx_hwmon_read(struct device
*dev
,
274 enum hwmon_sensor_types type
,
275 u32 attr
, int channel
, long *value
)
277 struct phy_device
*phydev
= dev_get_drvdata(dev
);
280 if (type
== hwmon_in
&& attr
== hwmon_in_lcrit_alarm
) {
281 ret
= phy_read(phydev
, MII_INTSRC
);
285 *value
= !!(ret
& MII_INTSRC_TEMP_ERR
);
289 if (type
== hwmon_temp
&& attr
== hwmon_temp_crit_alarm
) {
290 ret
= phy_read(phydev
, MII_INTSRC
);
294 *value
= !!(ret
& MII_INTSRC_UV_ERR
);
301 static umode_t
tja11xx_hwmon_is_visible(const void *data
,
302 enum hwmon_sensor_types type
,
303 u32 attr
, int channel
)
305 if (type
== hwmon_in
&& attr
== hwmon_in_lcrit_alarm
)
308 if (type
== hwmon_temp
&& attr
== hwmon_temp_crit_alarm
)
314 static const struct hwmon_channel_info
*tja11xx_hwmon_info
[] = {
315 HWMON_CHANNEL_INFO(in
, HWMON_I_LCRIT_ALARM
),
316 HWMON_CHANNEL_INFO(temp
, HWMON_T_CRIT_ALARM
),
320 static const struct hwmon_ops tja11xx_hwmon_hwmon_ops
= {
321 .is_visible
= tja11xx_hwmon_is_visible
,
322 .read
= tja11xx_hwmon_read
,
325 static const struct hwmon_chip_info tja11xx_hwmon_chip_info
= {
326 .ops
= &tja11xx_hwmon_hwmon_ops
,
327 .info
= tja11xx_hwmon_info
,
330 static int tja11xx_probe(struct phy_device
*phydev
)
332 struct device
*dev
= &phydev
->mdio
.dev
;
333 struct tja11xx_priv
*priv
;
336 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
340 priv
->hwmon_name
= devm_kstrdup(dev
, dev_name(dev
), GFP_KERNEL
);
341 if (!priv
->hwmon_name
)
344 for (i
= 0; priv
->hwmon_name
[i
]; i
++)
345 if (hwmon_is_bad_char(priv
->hwmon_name
[i
]))
346 priv
->hwmon_name
[i
] = '_';
349 devm_hwmon_device_register_with_info(dev
, priv
->hwmon_name
,
351 &tja11xx_hwmon_chip_info
,
354 return PTR_ERR_OR_ZERO(priv
->hwmon_dev
);
357 static struct phy_driver tja11xx_driver
[] = {
359 PHY_ID_MATCH_MODEL(PHY_ID_TJA1100
),
360 .name
= "NXP TJA1100",
361 .features
= PHY_BASIC_T1_FEATURES
,
362 .probe
= tja11xx_probe
,
363 .soft_reset
= tja11xx_soft_reset
,
364 .config_init
= tja11xx_config_init
,
365 .read_status
= tja11xx_read_status
,
366 .suspend
= genphy_suspend
,
367 .resume
= genphy_resume
,
368 .set_loopback
= genphy_loopback
,
370 .get_sset_count
= tja11xx_get_sset_count
,
371 .get_strings
= tja11xx_get_strings
,
372 .get_stats
= tja11xx_get_stats
,
374 PHY_ID_MATCH_MODEL(PHY_ID_TJA1101
),
375 .name
= "NXP TJA1101",
376 .features
= PHY_BASIC_T1_FEATURES
,
377 .probe
= tja11xx_probe
,
378 .soft_reset
= tja11xx_soft_reset
,
379 .config_init
= tja11xx_config_init
,
380 .read_status
= tja11xx_read_status
,
381 .suspend
= genphy_suspend
,
382 .resume
= genphy_resume
,
383 .set_loopback
= genphy_loopback
,
385 .get_sset_count
= tja11xx_get_sset_count
,
386 .get_strings
= tja11xx_get_strings
,
387 .get_stats
= tja11xx_get_stats
,
391 module_phy_driver(tja11xx_driver
);
393 static struct mdio_device_id __maybe_unused tja11xx_tbl
[] = {
394 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1100
) },
395 { PHY_ID_MATCH_MODEL(PHY_ID_TJA1101
) },
399 MODULE_DEVICE_TABLE(mdio
, tja11xx_tbl
);
401 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
402 MODULE_DESCRIPTION("NXP TJA11xx BoardR-Reach PHY driver");
403 MODULE_LICENSE("GPL");