RTL8159 is sold in QFN68 and QFN100 packages, and only QFN68 needs the u2phy backup-restore patch and enables UPS (in a later patch). There was previously no way to tell the two apart at runtime, since both used the same RTL_VER_17. Split RTL_VER_17 into RTL_VER_17_QFN68 and RTL_VER_17_QFN100 and update every switch/case on RTL_VER_17 in the driver to handle both. Detect the package by reading the new PLA_PKG_DET register (0xdc48, masked with PKG_MASK = 0x1e) via a new __rtl_get_pkg_det() helper, called from __rtl_get_hw_ver() when the chip ID is 0x2020: pkg_det value 0x1e/0x1c selects QFN68 and 0x18/0x1a selects QFN100. A genuine RTL8159 reports one of these, so any other value is a part the driver does not know and logs "Unknown package". A failed read of the register is logged separately. Both yield RTL_VER_UNKNOWN, which rtl8152_probe() and the configuration selector reject with -ENODEV. Before this patch every 0x2020 chip became RTL_VER_17, so a chip whose package cannot be identified no longer binds to r8152. Skipping only the firmware would not be enough, as the package also selects the QFN68-only u2phy backup-restore write in r8157_init() and, in a later patch, whether UPS is enabled. Guessing could apply them to a part that does not support them. The package register is read independently by the configuration selector, rtl8152_probe() and r8153_ecm_probe(). If the selector's read succeeds and the one in rtl8152_probe() fails after its retries, the device is left in vendor mode without a driver. A genuine RTL8159 can only be treated as unknown through such a read failure, which means the device is in trouble anyway. QFN100 uses a different firmware image from QFN68, so rtl_fw_init() selects rtl8159-2.fw for it and keeps rtl8159-1.fw for QFN68. r8157_init() gains a QFN68-only write ("Enable u2phy backup restore patch", 0xb99c BIT(0)) that does not apply to QFN100. Signed-off-by: Chih Kai Hsu --- drivers/net/usb/r8152.c | 122 ++++++++++++++++++++++++++++++++-------- 1 file changed, 100 insertions(+), 22 deletions(-) diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index 3e16d0f832bc3..103d8430da424 100644 --- a/drivers/net/usb/r8152.c +++ b/drivers/net/usb/r8152.c @@ -64,6 +64,7 @@ #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */ #define PLA_EXTRA_STATUS 0xd398 #define PLA_GPHY_CTRL 0xd3ae +#define PLA_PKG_DET 0xdc48 #define PLA_POL_GPIO_CTRL 0xdc6a #define PLA_EFUSE_DATA 0xdd00 #define PLA_EFUSE_CMD 0xdd02 @@ -291,6 +292,9 @@ #define IFG_144NS BIT(9) #define IFG_96NS (BIT(9) | BIT(8)) +/* PLA_PKG_DET */ +#define PKG_MASK 0x1e + /* PLA_MTPS */ #define MTPS_JUMBO (12 * 1024 / 64) #define MTPS_DEFAULT (6 * 1024 / 64) @@ -1258,7 +1262,8 @@ enum rtl_version { RTL_VER_14, RTL_VER_15, RTL_VER_16, - RTL_VER_17, + RTL_VER_17_QFN68, + RTL_VER_17_QFN100, RTL_VER_MAX }; @@ -3423,7 +3428,8 @@ static void rtl8152_nic_reset(struct r8152 *tp) break; case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: ocp_byte_clr_bits(tp, MCU_TYPE_PLA, PLA_CR, CR_RE | CR_TE); break; @@ -3463,7 +3469,7 @@ static void rtl_eee_plus_en(struct r8152 *tp, bool enable) static void rtl_set_eee_plus(struct r8152 *tp) { - if (tp->version == RTL_VER_17) + if (tp->version == RTL_VER_17_QFN68 || tp->version == RTL_VER_17_QFN100) return rtl_eee_plus_en(tp, false); if (rtl8152_get_speed(tp) & _10bps) @@ -3651,7 +3657,8 @@ static void r8153_set_rx_early_timeout(struct r8152 *tp) case RTL_VER_13: case RTL_VER_15: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, 640 / 8); ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, @@ -3696,7 +3703,8 @@ static void r8153_set_rx_early_size(struct r8152 *tp) ocp_data / 8); break; case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data / 16); break; @@ -3812,6 +3820,8 @@ static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) case RTL_VER_13: case RTL_VER_15: case RTL_VER_16: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: default: if (enable) ocp_word_set_bits(tp, MCU_TYPE_PLA, PLA_RCR1, @@ -4492,6 +4502,8 @@ static void r8153_teredo_off(struct r8152 *tp) case RTL_VER_14: case RTL_VER_15: case RTL_VER_16: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: default: /* The bit 0 ~ 7 are relative with teredo settings. They are * W1C (write 1 to clear), so set all 1 to disable it. @@ -4546,7 +4558,8 @@ static void rtl_clear_bp(struct r8152 *tp, u16 type) break; case RTL_VER_14: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: default: ocp_write_word(tp, type, USB_BP2_EN, 0); bp_num = 16; @@ -4658,7 +4671,8 @@ static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_ case RTL_VER_13: case RTL_VER_15: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: default: break; } @@ -4905,7 +4919,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) break; case RTL_VER_14: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: fw_reg = 0xf800; bp_ba_addr = PLA_BP_BA; bp_en_addr = USB_BP2_EN; @@ -4935,7 +4950,8 @@ static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) case RTL_VER_14: case RTL_VER_15: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: fw_reg = 0xe600; bp_ba_addr = USB_BP_BA; bp_en_addr = USB_BP2_EN; @@ -5822,7 +5838,8 @@ static void rtl_eee_enable(struct r8152 *tp, bool enable) case RTL_VER_13: case RTL_VER_15: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: if (enable) { r8156_eee_en(tp, true); ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); @@ -6013,7 +6030,8 @@ static void rtl_fc_pause_pkt_en(struct r8152 *tp, u16 speed) ocp_word_set_bits(tp, MCU_TYPE_USB, USB_FW_TASK, FC_PATCH_TASK); break; case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: ocp_word_clr_bits(tp, MCU_TYPE_PLA, PLA_RCR, SLOT_EN); num_pause_pkts = 0xa; @@ -6512,7 +6530,7 @@ static int rtl8156_enable(struct r8152 *tp) set_tx_qlen(tp); rtl_set_eee_plus(tp); - if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_17) + if (tp->version >= RTL_VER_12 && tp->version <= RTL_VER_17_QFN100) ocp_word_clr_bits(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, RX_AGGR_NUM_MASK); r8153_set_rx_early_timeout(tp); @@ -7008,7 +7026,8 @@ static void rtl8156_up(struct r8152 *tp) ocp_word_set_bits(tp, MCU_TYPE_USB, 0xd3ca, BIT(15)); break; case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: /* Enable Clear_SDR */ ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xd378, BIT(7)); /* Clear SDR bit */ @@ -7052,7 +7071,8 @@ static void rtl8156_down(struct r8152 *tp) switch (tp->version) { case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, 1526); ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64); break; @@ -8258,7 +8278,8 @@ static void r8157_hw_phy_cfg(struct r8152 *tp) sram2_write_w0w1(tp, 0x809d, 0xff00, 0x5000); break; - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: /* Disable bypass turn off clk in ALDPS */ ocp_byte_clr_bits(tp, MCU_TYPE_PLA, 0xd3c8, BIT(0)); @@ -8652,7 +8673,8 @@ static void r8157_init(struct r8152 *tp) return; switch (tp->version) { - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: if (r8159_wait_backup_restore(tp)) { rtl_set_inaccessible(tp); dev_err(&tp->intf->dev, @@ -8699,7 +8721,8 @@ static void r8157_init(struct r8152 *tp) ocp_word_clr_bits(tp, MCU_TYPE_USB, 0xcd06, BIT(11)); switch (tp->version) { - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: /* Disable FW u1u2 patch option */ ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xb9a6, BIT(0)); break; @@ -8737,7 +8760,11 @@ static void r8157_init(struct r8152 *tp) ocp_byte_clr_bits(tp, MCU_TYPE_USB, 0xd4ae, BIT(1)); switch (tp->version) { - case RTL_VER_17: + case RTL_VER_17_QFN68: + /* Enable u2phy backup restore patch */ + ocp_byte_set_bits(tp, MCU_TYPE_USB, 0xb99c, BIT(0)); + fallthrough; + case RTL_VER_17_QFN100: /* Enable u3phy patch backup */ ocp_write_word(tp, MCU_TYPE_USB, 0xb9a2, 0x0448); break; @@ -10131,7 +10158,8 @@ static int rtl_ops_init(struct r8152 *tp) r8157_desc_init(tp); break; - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: tp->eee_en = true; tp->eee_adv = MDIO_EEE_100TX | MDIO_EEE_1000T | MDIO_EEE_10GT; tp->eee_adv2 = MDIO_EEE_2_5GT | MDIO_EEE_5GT; @@ -10172,6 +10200,7 @@ static int rtl_ops_init(struct r8152 *tp) #define FIRMWARE_8156B_2 "rtl_nic/rtl8156b-2.fw" #define FIRMWARE_8157_1 "rtl_nic/rtl8157-1.fw" #define FIRMWARE_8159_1 "rtl_nic/rtl8159-1.fw" +#define FIRMWARE_8159_2 "rtl_nic/rtl8159-2.fw" MODULE_FIRMWARE(FIRMWARE_8153A_2); MODULE_FIRMWARE(FIRMWARE_8153A_3); @@ -10182,6 +10211,7 @@ MODULE_FIRMWARE(FIRMWARE_8156A_2); MODULE_FIRMWARE(FIRMWARE_8156B_2); MODULE_FIRMWARE(FIRMWARE_8157_1); MODULE_FIRMWARE(FIRMWARE_8159_1); +MODULE_FIRMWARE(FIRMWARE_8159_2); static int rtl_fw_init(struct r8152 *tp) { @@ -10223,9 +10253,12 @@ static int rtl_fw_init(struct r8152 *tp) case RTL_VER_16: rtl_fw->fw_name = FIRMWARE_8157_1; break; - case RTL_VER_17: + case RTL_VER_17_QFN68: rtl_fw->fw_name = FIRMWARE_8159_1; break; + case RTL_VER_17_QFN100: + rtl_fw->fw_name = FIRMWARE_8159_2; + break; default: break; } @@ -10233,9 +10266,40 @@ static int rtl_fw_init(struct r8152 *tp) return 0; } +static int __rtl_get_pkg_det(struct usb_device *udev, u32 *pkg_det) +{ + __le32 *tmp; + int ret, i; + + tmp = kmalloc_obj(*tmp); + if (!tmp) + return -ENOMEM; + + for (i = 0, ret = 0; i < 3 && ret != 4; i++) + ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), + RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, + PLA_PKG_DET, MCU_TYPE_PLA, tmp, + sizeof(*tmp), USB_CTRL_GET_TIMEOUT); + + if (ret == sizeof(*tmp)) { + if (i != 1) + dev_warn(&udev->dev, + "Needed %d retries to read package\n", i - 1); + + *pkg_det = __le32_to_cpu(*tmp) & PKG_MASK; + ret = 0; + } else if (ret >= 0) { + ret = -EIO; + } + + kfree(tmp); + return ret; +} + static u8 __rtl_get_hw_ver(struct usb_device *udev) { u32 ocp_data = 0; + u32 pkg_det = 0; __le32 *tmp; u8 version; int ret; @@ -10318,7 +10382,20 @@ static u8 __rtl_get_hw_ver(struct usb_device *udev) version = RTL_VER_16; break; case 0x2020: - version = RTL_VER_17; + ret = __rtl_get_pkg_det(udev, &pkg_det); + if (ret < 0) { + version = RTL_VER_UNKNOWN; + dev_err(&udev->dev, "Failed to read package: %d\n", + ret); + } else if (pkg_det == 0x1e || pkg_det == 0x1c) { + version = RTL_VER_17_QFN68; + } else if (pkg_det == 0x18 || pkg_det == 0x1a) { + version = RTL_VER_17_QFN100; + } else { + version = RTL_VER_UNKNOWN; + dev_err(&udev->dev, "Unknown package 0x%04x\n", + pkg_det); + } break; default: version = RTL_VER_UNKNOWN; @@ -10477,7 +10554,8 @@ static int rtl8152_probe_once(struct usb_interface *intf, case RTL_VER_13: case RTL_VER_15: case RTL_VER_16: - case RTL_VER_17: + case RTL_VER_17_QFN68: + case RTL_VER_17_QFN100: netdev->max_mtu = size_to_mtu(16 * 1024); break; case RTL_VER_01: -- 2.34.1