RTL8157 and RTL8159 QFN68 support UPS, but rtl8157_runtime_enable() never toggled it on runtime suspend/resume, and r8156_ups_flags() used the older per-chip EEE/lite_mode encoding instead of the per-speed EEE flags their UPS firmware expects. RTL8159 QFN100 does not support UPS. Add r8157_ups_en(), mirroring r8156_ups_en(), and call it from rtl8157_runtime_enable() for VER_16/QFN68 only; QFN100 keeps its existing no-UPS behaviour there. Use r8157_ups_en(tp, false) in r8157_init() in place of r8156_ups_en(tp, false). Extend r8156_ups_flags() with per-speed EEE flags (UPS_FLAGS_EN_100M_EEE/1000M_EEE/2500M_EEE/5000M_EEE/10G_EEE) and skip the lite_mode bits[6:5] encoding for VER_16/QFN68. Add ups_flags_speed() entries 10 and 11 for NWAY_5000M_FULL and NWAY_10000M_FULL, previously unmapped. Add the USB_FW_USB_VER register (0xcfd7), used by the new r8157_ups_en() disable path. If the backup-restore wait in r8157_ups_en() times out, it marks the device inaccessible and logs, without queuing a reset, as r8157_init() does for the same wait. During runtime resume the interface then stays unusable until a replug or an unrelated reset, since r8157_ups_en() returns void and the error is not passed back. Recovery for these timeouts is left to a follow-up series. Signed-off-by: Chih Kai Hsu --- drivers/net/usb/r8152.c | 131 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 120 insertions(+), 11 deletions(-) diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index 703ec5483778b..9c7031f938bc2 100644 --- a/drivers/net/usb/r8152.c +++ b/drivers/net/usb/r8152.c @@ -132,6 +132,7 @@ #define USB_BURST_SIZE 0xcfc0 #define USB_FW_FIX_EN0 0xcfca #define USB_FW_FIX_EN1 0xcfcc +#define USB_FW_USB_VER 0xcfd7 #define USB_LPM_CONFIG 0xcfd8 #define USB_ECM_OPTION 0xcfee #define USB_CSTMR 0xcfef /* RTL8153A */ @@ -619,6 +620,11 @@ #define UPS_FLAGS_250M_CKDIV BIT(2) #define UPS_FLAGS_EN_ALDPS BIT(3) #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) +#define UPS_FLAGS_EN_100M_EEE BIT(9) +#define UPS_FLAGS_EN_1000M_EEE BIT(10) +#define UPS_FLAGS_EN_2500M_EEE BIT(11) +#define UPS_FLAGS_EN_5000M_EEE BIT(12) +#define UPS_FLAGS_EN_10G_EEE BIT(13) #define UPS_FLAGS_SPEED_MASK (0xf << 16) #define ups_flags_speed(x) ((x) << 16) #define UPS_FLAGS_EN_EEE BIT(20) @@ -4256,8 +4262,27 @@ static void r8156_ups_flags(struct r8152 *tp) if (tp->ups_info.aldps) ups_flags |= UPS_FLAGS_EN_ALDPS; - if (tp->ups_info.eee) - ups_flags |= UPS_FLAGS_EN_EEE; + if (tp->ups_info.eee) { + switch (tp->version) { + case RTL_VER_17_QFN68: + if (tp->eee_adv & MDIO_EEE_10GT) + ups_flags |= UPS_FLAGS_EN_10G_EEE; + fallthrough; + case RTL_VER_16: + if (tp->eee_adv & MDIO_EEE_100TX) + ups_flags |= UPS_FLAGS_EN_100M_EEE; + if (tp->eee_adv & MDIO_EEE_1000T) + ups_flags |= UPS_FLAGS_EN_1000M_EEE; + if (tp->eee_adv2 & MDIO_EEE_2_5GT) + ups_flags |= UPS_FLAGS_EN_2500M_EEE; + if (tp->eee_adv2 & MDIO_EEE_5GT) + ups_flags |= UPS_FLAGS_EN_5000M_EEE; + break; + default: + ups_flags |= UPS_FLAGS_EN_EEE; + break; + } + } if (tp->ups_info.flow_control) ups_flags |= UPS_FLAGS_EN_FLOW_CTR; @@ -4308,20 +4333,33 @@ static void r8156_ups_flags(struct r8152 *tp) case NWAY_2500M_FULL: ups_flags |= ups_flags_speed(9); break; + case NWAY_5000M_FULL: + ups_flags |= ups_flags_speed(10); + break; + case NWAY_10000M_FULL: + ups_flags |= ups_flags_speed(11); + break; default: break; } - switch (tp->ups_info.lite_mode) { - case 1: - ups_flags |= 0 << 5; - break; - case 2: - ups_flags |= 2 << 5; + switch (tp->version) { + case RTL_VER_16: + case RTL_VER_17_QFN68: break; - case 0: default: - ups_flags |= 1 << 5; + switch (tp->ups_info.lite_mode) { + case 1: + ups_flags |= 0 << 5; + break; + case 2: + ups_flags |= 2 << 5; + break; + case 0: + default: + ups_flags |= 1 << 5; + break; + } break; } @@ -4496,6 +4534,58 @@ static void r8156_ups_en(struct r8152 *tp, bool enable) } } +static void r8157_ups_en(struct r8152 *tp, bool enable) +{ + u32 ocp_data; + int ret; + + if (enable) { + r8156_ups_flags(tp); + + ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, + UPS_EN | USP_PREWAKE | PHASE2_EN); + + ocp_byte_set_bits(tp, MCU_TYPE_USB, USB_MISC_2, + UPS_FORCE_PWR_DOWN); + } else { + ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_POWER_CUT, + UPS_EN | USP_PREWAKE); + + ocp_byte_clr_bits(tp, MCU_TYPE_USB, USB_MISC_2, + UPS_FORCE_PWR_DOWN); + + ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); + if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) + return; + + if (ocp_data & PCUT_STATUS) { + ret = read_poll_timeout(ocp_read_word, ocp_data, + test_bit(RTL8152_INACCESSIBLE, + &tp->flags) || + (ocp_data & BACKUP_RESTRORE), + 200, 20000, false, tp, + MCU_TYPE_USB, USB_GPHY_CTRL); + if (test_bit(RTL8152_INACCESSIBLE, &tp->flags)) + return; + + if (ret) { + rtl_set_inaccessible(tp); + dev_err(&tp->intf->dev, + "backup-restore timed out\n"); + return; + } + + /* clear USB fw_ver_reg */ + ocp_write_byte(tp, MCU_TYPE_USB, USB_FW_USB_VER, 0); + + tp->rtl_ops.hw_phy_cfg(tp); + + rtl8152_set_speed(tp, tp->autoneg, tp->speed, + tp->duplex, tp->advertising); + } + } +} + static void r8153_power_cut_en(struct r8152 *tp, bool enable) { if (enable) @@ -4654,9 +4744,28 @@ static void rtl8157_runtime_enable(struct r8152 *tp, bool enable) r8153b_u1u2en(tp, false); r8157_u2p3en(tp, false); rtl_runtime_suspend_enable(tp, true); + + switch (tp->version) { + case RTL_VER_16: + case RTL_VER_17_QFN68: + r8157_ups_en(tp, true); + break; + default: + break; + } } else { r8153_queue_wake(tp, false); rtl_runtime_suspend_enable(tp, false); + + switch (tp->version) { + case RTL_VER_16: + case RTL_VER_17_QFN68: + r8157_ups_en(tp, false); + break; + default: + break; + } + r8157_u2p3en(tp, true); if (tp->udev->speed >= USB_SPEED_SUPER) r8153b_u1u2en(tp, true); @@ -9120,7 +9229,7 @@ static void r8157_init(struct r8152 *tp) ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); r8157_power_cut_en(tp, false); - r8156_ups_en(tp, false); + r8157_ups_en(tp, false); r8153_queue_wake(tp, false); rtl_runtime_suspend_enable(tp, false); -- 2.34.1