bbb2b5c0c9f7dd66a7d060decf028c00d8654699
[openwrt/staging/rmilecki.git] /
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
5 request
6
7 commit a08c0d309d8c078d22717d815cf9853f6f2c07bd upstream.
8
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().
13
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.
19
20 Signed-off-by: Hayes Wang <hayeswang@realtek.com>
21 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
22 ---
23 drivers/net/usb/r8152.c | 84 ++++++++++++++++++++++++-----------------
24 1 file changed, 50 insertions(+), 34 deletions(-)
25
26 --- a/drivers/net/usb/r8152.c
27 +++ b/drivers/net/usb/r8152.c
28 @@ -3444,59 +3444,76 @@ static void rtl_clear_bp(struct r8152 *t
29 ocp_write_word(tp, type, PLA_BP_BA, 0);
30 }
31
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)
34 {
35 - u16 data;
36 + u16 data, check;
37 int i;
38
39 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
40 - if (request)
41 + if (request) {
42 data |= PATCH_REQUEST;
43 - else
44 + check = 0;
45 + } else {
46 data &= ~PATCH_REQUEST;
47 + check = PATCH_READY;
48 + }
49 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
50
51 - for (i = 0; request && i < 5000; i++) {
52 + for (i = 0; wait && i < 5000; i++) {
53 + u32 ocp_data;
54 +
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)
59 break;
60 }
61
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);
69 return -ETIME;
70 } else {
71 return 0;
72 }
73 }
74
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)
77 {
78 - if (r8153_patch_request(tp, true)) {
79 - dev_err(&tp->intf->dev, "patch request fail\n");
80 - return -ETIME;
81 - }
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) {
86 + u16 data;
87
88 - sram_write(tp, key_addr, patch_key);
89 - sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
90 + sram_write(tp, 0x0000, 0x0000);
91
92 - return 0;
93 + data = ocp_reg_read(tp, OCP_PHY_LOCK);
94 + data &= ~PATCH_LOCK;
95 + ocp_reg_write(tp, OCP_PHY_LOCK, data);
96 +
97 + sram_write(tp, key_addr, 0x0000);
98 + } else {
99 + WARN_ON_ONCE(1);
100 + }
101 }
102
103 -static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr)
104 +static int
105 +rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
106 {
107 - u16 data;
108 + if (rtl_phy_patch_request(tp, true, wait))
109 + return -ETIME;
110
111 - sram_write(tp, 0x0000, 0x0000);
112 + rtl_patch_key_set(tp, key_addr, patch_key);
113
114 - data = ocp_reg_read(tp, OCP_PHY_LOCK);
115 - data &= ~PATCH_LOCK;
116 - ocp_reg_write(tp, OCP_PHY_LOCK, data);
117 + return 0;
118 +}
119
120 - sram_write(tp, key_addr, 0x0000);
121 +static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
122 +{
123 + rtl_patch_key_set(tp, key_addr, 0);
124
125 - r8153_patch_request(tp, false);
126 + rtl_phy_patch_request(tp, false, wait);
127
128 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
129
130 @@ -3981,7 +3998,7 @@ static void rtl8152_fw_mac_apply(struct
131 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
132 }
133
134 -static void rtl8152_apply_firmware(struct r8152 *tp)
135 +static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
136 {
137 struct rtl_fw *rtl_fw = &tp->rtl_fw;
138 const struct firmware *fw;
139 @@ -4012,12 +4029,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);
146 break;
147 case RTL_FW_PHY_STOP:
148 WARN_ON(!key_addr);
149 - r8153_post_ram_code(tp, key_addr);
150 + rtl_post_ram_code(tp, key_addr, !power_cut);
151 break;
152 case RTL_FW_PHY_NC:
153 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
154 @@ -4222,7 +4238,7 @@ static void rtl8152_disable(struct r8152
155
156 static void r8152b_hw_phy_cfg(struct r8152 *tp)
157 {
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 @@ -4504,7 +4520,7 @@ static void r8153_hw_phy_cfg(struct r815
164 /* disable EEE before updating the PHY parameters */
165 rtl_eee_enable(tp, false);
166
167 - rtl8152_apply_firmware(tp);
168 + rtl8152_apply_firmware(tp, false);
169
170 if (tp->version == RTL_VER_03) {
171 data = ocp_reg_read(tp, OCP_EEE_CFG);
172 @@ -4578,7 +4594,7 @@ static void r8153b_hw_phy_cfg(struct r81
173 /* disable EEE before updating the PHY parameters */
174 rtl_eee_enable(tp, false);
175
176 - rtl8152_apply_firmware(tp);
177 + rtl8152_apply_firmware(tp, false);
178
179 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
180
181 @@ -4619,7 +4635,7 @@ static void r8153b_hw_phy_cfg(struct r81
182 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
183
184 /* Advnace EEE */
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 @@ -4636,7 +4652,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;
193
194 - r8153_patch_request(tp, false);
195 + rtl_phy_patch_request(tp, false, true);
196 }
197
198 if (tp->eee_en)