1 From 86b98abf4f8c691c260c5113d6a2d32f5377caca Mon Sep 17 00:00:00 2001
2 From: Hayes Wang <hayeswang@realtek.com>
3 Date: Wed, 3 Feb 2021 17:14:28 +0800
4 Subject: [PATCH] r8152: replace several functions about phy patch
7 commit a08c0d309d8c078d22717d815cf9853f6f2c07bd upstream.
9 Replace r8153_patch_request() with rtl_phy_patch_request().
10 Replace r8153_pre_ram_code() with rtl_pre_ram_code().
11 Replace r8153_post_ram_code() with rtl_post_ram_code().
12 Add rtl_patch_key_set().
14 The new functions have an additional parameter. It is used to wait
15 the patch request command finished. When the PHY is resumed from
16 the state of power cut, the PHY is at a safe mode and the
17 OCP_PHY_PATCH_STAT wouldn't be updated. For this situation, it is
18 safe to set patch request command without waiting OCP_PHY_PATCH_STAT.
20 Signed-off-by: Hayes Wang <hayeswang@realtek.com>
21 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
23 drivers/net/usb/r8152.c | 84 ++++++++++++++++++++++++-----------------
24 1 file changed, 50 insertions(+), 34 deletions(-)
26 --- a/drivers/net/usb/r8152.c
27 +++ b/drivers/net/usb/r8152.c
28 @@ -3443,59 +3443,76 @@ static void rtl_clear_bp(struct r8152 *t
29 ocp_write_word(tp, type, PLA_BP_BA, 0);
32 -static int r8153_patch_request(struct r8152 *tp, bool request)
33 +static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
39 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
42 data |= PATCH_REQUEST;
46 data &= ~PATCH_REQUEST;
47 + check = PATCH_READY;
49 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
51 - for (i = 0; request && i < 5000; i++) {
52 + for (i = 0; wait && i < 5000; i++) {
55 usleep_range(1000, 2000);
56 - if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
57 + ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
58 + if ((ocp_data & PATCH_READY) ^ check)
62 - if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
63 - netif_err(tp, drv, tp->netdev, "patch request fail\n");
64 - r8153_patch_request(tp, false);
65 + if (request && wait &&
66 + !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
67 + dev_err(&tp->intf->dev, "PHY patch request fail\n");
68 + rtl_phy_patch_request(tp, false, false);
75 -static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key)
76 +static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
78 - if (r8153_patch_request(tp, true)) {
79 - dev_err(&tp->intf->dev, "patch request fail\n");
82 + if (patch_key && key_addr) {
83 + sram_write(tp, key_addr, patch_key);
84 + sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
85 + } else if (key_addr) {
88 - sram_write(tp, key_addr, patch_key);
89 - sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
90 + sram_write(tp, 0x0000, 0x0000);
93 + data = ocp_reg_read(tp, OCP_PHY_LOCK);
94 + data &= ~PATCH_LOCK;
95 + ocp_reg_write(tp, OCP_PHY_LOCK, data);
97 + sram_write(tp, key_addr, 0x0000);
103 -static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr)
105 +rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
108 + if (rtl_phy_patch_request(tp, true, wait))
111 - sram_write(tp, 0x0000, 0x0000);
112 + rtl_patch_key_set(tp, key_addr, patch_key);
114 - data = ocp_reg_read(tp, OCP_PHY_LOCK);
115 - data &= ~PATCH_LOCK;
116 - ocp_reg_write(tp, OCP_PHY_LOCK, data);
120 - sram_write(tp, key_addr, 0x0000);
121 +static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
123 + rtl_patch_key_set(tp, key_addr, 0);
125 - r8153_patch_request(tp, false);
126 + rtl_phy_patch_request(tp, false, wait);
128 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
130 @@ -3980,7 +3997,7 @@ static void rtl8152_fw_mac_apply(struct
131 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
134 -static void rtl8152_apply_firmware(struct r8152 *tp)
135 +static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
137 struct rtl_fw *rtl_fw = &tp->rtl_fw;
138 const struct firmware *fw;
139 @@ -4011,12 +4028,11 @@ static void rtl8152_apply_firmware(struc
140 case RTL_FW_PHY_START:
141 key = (struct fw_phy_patch_key *)block;
142 key_addr = __le16_to_cpu(key->key_reg);
143 - r8153_pre_ram_code(tp, key_addr,
144 - __le16_to_cpu(key->key_data));
145 + rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
147 case RTL_FW_PHY_STOP:
149 - r8153_post_ram_code(tp, key_addr);
150 + rtl_post_ram_code(tp, key_addr, !power_cut);
153 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
154 @@ -4221,7 +4237,7 @@ static void rtl8152_disable(struct r8152
156 static void r8152b_hw_phy_cfg(struct r8152 *tp)
158 - rtl8152_apply_firmware(tp);
159 + rtl8152_apply_firmware(tp, false);
160 rtl_eee_enable(tp, tp->eee_en);
161 r8152_aldps_en(tp, true);
162 r8152b_enable_fc(tp);
163 @@ -4503,7 +4519,7 @@ static void r8153_hw_phy_cfg(struct r815
164 /* disable EEE before updating the PHY parameters */
165 rtl_eee_enable(tp, false);
167 - rtl8152_apply_firmware(tp);
168 + rtl8152_apply_firmware(tp, false);
170 if (tp->version == RTL_VER_03) {
171 data = ocp_reg_read(tp, OCP_EEE_CFG);
172 @@ -4577,7 +4593,7 @@ static void r8153b_hw_phy_cfg(struct r81
173 /* disable EEE before updating the PHY parameters */
174 rtl_eee_enable(tp, false);
176 - rtl8152_apply_firmware(tp);
177 + rtl8152_apply_firmware(tp, false);
179 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
181 @@ -4618,7 +4634,7 @@ static void r8153b_hw_phy_cfg(struct r81
182 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
185 - if (!r8153_patch_request(tp, true)) {
186 + if (!rtl_phy_patch_request(tp, true, true)) {
187 data = ocp_reg_read(tp, OCP_POWER_CFG);
188 data |= EEE_CLKDIV_EN;
189 ocp_reg_write(tp, OCP_POWER_CFG, data);
190 @@ -4635,7 +4651,7 @@ static void r8153b_hw_phy_cfg(struct r81
191 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
192 tp->ups_info._250m_ckdiv = true;
194 - r8153_patch_request(tp, false);
195 + rtl_phy_patch_request(tp, false, true);