Commit | Line | Data |
---|---|---|
0c817338 LF |
1 | /****************************************************************************** |
2 | * | |
a8d76066 | 3 | * Copyright(c) 2009-2012 Realtek Corporation. |
0c817338 LF |
4 | * |
5 | * Tmis program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Tmis program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
0c817338 LF |
14 | * Tme full GNU General Public License is included in this distribution in the |
15 | * file called LICENSE. | |
16 | * | |
17 | * Contact Information: | |
18 | * wlanfae <wlanfae@realtek.com> | |
19 | * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, | |
20 | * Hsinchu 300, Taiwan. | |
21 | * | |
22 | * Larry Finger <Larry.Finger@lwfinger.net> | |
23 | * | |
24 | *****************************************************************************/ | |
0c817338 LF |
25 | #include "wifi.h" |
26 | #include "efuse.h" | |
3c67b8f9 | 27 | #include <linux/export.h> |
0c817338 LF |
28 | |
29 | static const u8 MAX_PGPKT_SIZE = 9; | |
30 | static const u8 PGPKT_DATA_SIZE = 8; | |
31 | static const int EFUSE_MAX_SIZE = 512; | |
32 | ||
0c817338 LF |
33 | static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = { |
34 | {0, 0, 0, 2}, | |
35 | {0, 1, 0, 2}, | |
36 | {0, 2, 0, 2}, | |
37 | {1, 0, 0, 1}, | |
38 | {1, 0, 1, 1}, | |
39 | {1, 1, 0, 1}, | |
40 | {1, 1, 1, 3}, | |
41 | {1, 3, 0, 17}, | |
42 | {3, 3, 1, 48}, | |
43 | {10, 0, 0, 6}, | |
44 | {10, 3, 0, 1}, | |
45 | {10, 3, 1, 1}, | |
46 | {11, 0, 0, 28} | |
47 | }; | |
48 | ||
0c817338 LF |
49 | static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset, |
50 | u8 *value); | |
51 | static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset, | |
52 | u16 *value); | |
53 | static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset, | |
54 | u32 *value); | |
55 | static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset, | |
56 | u8 value); | |
57 | static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset, | |
58 | u16 value); | |
59 | static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset, | |
60 | u32 value); | |
0c817338 | 61 | static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, |
3c67b8f9 | 62 | u8 data); |
0c817338 LF |
63 | static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse); |
64 | static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, | |
3c67b8f9 | 65 | u8 *data); |
0c817338 LF |
66 | static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset, |
67 | u8 word_en, u8 *data); | |
68 | static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata, | |
69 | u8 *targetdata); | |
3c67b8f9 LF |
70 | static u8 enable_efuse_data_write(struct ieee80211_hw *hw, |
71 | u16 efuse_addr, u8 word_en, u8 *data); | |
e25f51d4 | 72 | static void efuse_power_switch(struct ieee80211_hw *hw, u8 write, |
3c67b8f9 | 73 | u8 pwrstate); |
0c817338 LF |
74 | static u16 efuse_get_current_size(struct ieee80211_hw *hw); |
75 | static u8 efuse_calculate_word_cnts(u8 word_en); | |
76 | ||
77 | void efuse_initialize(struct ieee80211_hw *hw) | |
78 | { | |
79 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
80 | u8 bytetemp; | |
81 | u8 temp; | |
82 | ||
83 | bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1); | |
84 | temp = bytetemp | 0x20; | |
85 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1, temp); | |
86 | ||
87 | bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1); | |
88 | temp = bytetemp & 0xFE; | |
89 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1, temp); | |
90 | ||
91 | bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3); | |
92 | temp = bytetemp | 0x80; | |
93 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3, temp); | |
94 | ||
95 | rtl_write_byte(rtlpriv, 0x2F8, 0x3); | |
96 | ||
97 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72); | |
98 | ||
99 | } | |
100 | ||
101 | u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address) | |
102 | { | |
103 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
104 | u8 data; | |
105 | u8 bytetemp; | |
106 | u8 temp; | |
107 | u32 k = 0; | |
e25f51d4 C |
108 | const u32 efuse_len = |
109 | rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE]; | |
0c817338 | 110 | |
e25f51d4 | 111 | if (address < efuse_len) { |
0c817338 LF |
112 | temp = address & 0xFF; |
113 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1, | |
114 | temp); | |
115 | bytetemp = rtl_read_byte(rtlpriv, | |
116 | rtlpriv->cfg->maps[EFUSE_CTRL] + 2); | |
117 | temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC); | |
118 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2, | |
119 | temp); | |
120 | ||
121 | bytetemp = rtl_read_byte(rtlpriv, | |
122 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3); | |
123 | temp = bytetemp & 0x7F; | |
124 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, | |
125 | temp); | |
126 | ||
127 | bytetemp = rtl_read_byte(rtlpriv, | |
128 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3); | |
129 | while (!(bytetemp & 0x80)) { | |
130 | bytetemp = rtl_read_byte(rtlpriv, | |
131 | rtlpriv->cfg-> | |
132 | maps[EFUSE_CTRL] + 3); | |
133 | k++; | |
134 | if (k == 1000) { | |
135 | k = 0; | |
136 | break; | |
137 | } | |
138 | } | |
139 | data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]); | |
140 | return data; | |
141 | } else | |
142 | return 0xFF; | |
143 | ||
144 | } | |
145 | EXPORT_SYMBOL(efuse_read_1byte); | |
146 | ||
147 | void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value) | |
148 | { | |
149 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
150 | u8 bytetemp; | |
151 | u8 temp; | |
152 | u32 k = 0; | |
e25f51d4 C |
153 | const u32 efuse_len = |
154 | rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE]; | |
0c817338 | 155 | |
f30d7507 JP |
156 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "Addr=%x Data =%x\n", |
157 | address, value); | |
0c817338 | 158 | |
e25f51d4 | 159 | if (address < efuse_len) { |
0c817338 LF |
160 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value); |
161 | ||
162 | temp = address & 0xFF; | |
163 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1, | |
164 | temp); | |
165 | bytetemp = rtl_read_byte(rtlpriv, | |
166 | rtlpriv->cfg->maps[EFUSE_CTRL] + 2); | |
167 | ||
168 | temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC); | |
169 | rtl_write_byte(rtlpriv, | |
170 | rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp); | |
171 | ||
172 | bytetemp = rtl_read_byte(rtlpriv, | |
173 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3); | |
174 | temp = bytetemp | 0x80; | |
175 | rtl_write_byte(rtlpriv, | |
176 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3, temp); | |
177 | ||
178 | bytetemp = rtl_read_byte(rtlpriv, | |
179 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3); | |
180 | ||
181 | while (bytetemp & 0x80) { | |
182 | bytetemp = rtl_read_byte(rtlpriv, | |
183 | rtlpriv->cfg-> | |
184 | maps[EFUSE_CTRL] + 3); | |
185 | k++; | |
186 | if (k == 100) { | |
187 | k = 0; | |
188 | break; | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | } | |
194 | ||
e25f51d4 | 195 | void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf) |
0c817338 LF |
196 | { |
197 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
198 | u32 value32; | |
199 | u8 readbyte; | |
200 | u16 retry; | |
201 | ||
202 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1, | |
203 | (_offset & 0xff)); | |
204 | readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2); | |
205 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2, | |
206 | ((_offset >> 8) & 0x03) | (readbyte & 0xfc)); | |
207 | ||
208 | readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3); | |
209 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, | |
210 | (readbyte & 0x7f)); | |
211 | ||
212 | retry = 0; | |
213 | value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]); | |
214 | while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) { | |
215 | value32 = rtl_read_dword(rtlpriv, | |
216 | rtlpriv->cfg->maps[EFUSE_CTRL]); | |
217 | retry++; | |
218 | } | |
219 | ||
220 | udelay(50); | |
221 | value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]); | |
222 | ||
223 | *pbuf = (u8) (value32 & 0xff); | |
224 | } | |
6f334c2b | 225 | EXPORT_SYMBOL_GPL(read_efuse_byte); |
0c817338 LF |
226 | |
227 | void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf) | |
228 | { | |
229 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
230 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
c84aa5af | 231 | u8 *efuse_tbl; |
0c817338 LF |
232 | u8 rtemp8[1]; |
233 | u16 efuse_addr = 0; | |
234 | u8 offset, wren; | |
26634c4b | 235 | u8 u1temp = 0; |
0c817338 LF |
236 | u16 i; |
237 | u16 j; | |
e25f51d4 C |
238 | const u16 efuse_max_section = |
239 | rtlpriv->cfg->maps[EFUSE_MAX_SECTION_MAP]; | |
240 | const u32 efuse_len = | |
241 | rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE]; | |
c84aa5af | 242 | u16 **efuse_word; |
0c817338 LF |
243 | u16 efuse_utilized = 0; |
244 | u8 efuse_usage; | |
245 | ||
e25f51d4 | 246 | if ((_offset + _size_byte) > rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]) { |
0c817338 | 247 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, |
f30d7507 JP |
248 | "read_efuse(): Invalid offset(%#x) with read bytes(%#x)!!\n", |
249 | _offset, _size_byte); | |
0c817338 LF |
250 | return; |
251 | } | |
252 | ||
c84aa5af | 253 | /* allocate memory for efuse_tbl and efuse_word */ |
ef09396e | 254 | efuse_tbl = kzalloc(rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE] * |
c84aa5af LF |
255 | sizeof(u8), GFP_ATOMIC); |
256 | if (!efuse_tbl) | |
257 | return; | |
450991fd | 258 | efuse_word = kzalloc(EFUSE_MAX_WORD_UNIT * sizeof(u16 *), GFP_ATOMIC); |
c84aa5af | 259 | if (!efuse_word) |
450991fd | 260 | goto out; |
c84aa5af | 261 | for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) { |
ef09396e | 262 | efuse_word[i] = kzalloc(efuse_max_section * sizeof(u16), |
c84aa5af LF |
263 | GFP_ATOMIC); |
264 | if (!efuse_word[i]) | |
265 | goto done; | |
266 | } | |
267 | ||
e25f51d4 | 268 | for (i = 0; i < efuse_max_section; i++) |
0c817338 | 269 | for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) |
c84aa5af | 270 | efuse_word[j][i] = 0xFFFF; |
0c817338 LF |
271 | |
272 | read_efuse_byte(hw, efuse_addr, rtemp8); | |
273 | if (*rtemp8 != 0xFF) { | |
274 | efuse_utilized++; | |
275 | RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL, | |
4c48869f | 276 | "Addr=%d\n", efuse_addr); |
0c817338 LF |
277 | efuse_addr++; |
278 | } | |
279 | ||
e25f51d4 | 280 | while ((*rtemp8 != 0xFF) && (efuse_addr < efuse_len)) { |
26634c4b LF |
281 | /* Check PG header for section num. */ |
282 | if ((*rtemp8 & 0x1F) == 0x0F) {/* extended header */ | |
283 | u1temp = ((*rtemp8 & 0xE0) >> 5); | |
284 | read_efuse_byte(hw, efuse_addr, rtemp8); | |
0c817338 | 285 | |
26634c4b LF |
286 | if ((*rtemp8 & 0x0F) == 0x0F) { |
287 | efuse_addr++; | |
288 | read_efuse_byte(hw, efuse_addr, rtemp8); | |
289 | ||
290 | if (*rtemp8 != 0xFF && | |
291 | (efuse_addr < efuse_len)) { | |
292 | efuse_addr++; | |
293 | } | |
294 | continue; | |
295 | } else { | |
296 | offset = ((*rtemp8 & 0xF0) >> 1) | u1temp; | |
297 | wren = (*rtemp8 & 0x0F); | |
298 | efuse_addr++; | |
299 | } | |
300 | } else { | |
301 | offset = ((*rtemp8 >> 4) & 0x0f); | |
0c817338 | 302 | wren = (*rtemp8 & 0x0f); |
26634c4b LF |
303 | } |
304 | ||
305 | if (offset < efuse_max_section) { | |
0c817338 | 306 | RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL, |
4c48869f | 307 | "offset-%d Worden=%x\n", offset, wren); |
0c817338 LF |
308 | |
309 | for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) { | |
310 | if (!(wren & 0x01)) { | |
311 | RTPRINT(rtlpriv, FEEPROM, | |
4c48869f JP |
312 | EFUSE_READ_ALL, |
313 | "Addr=%d\n", efuse_addr); | |
0c817338 LF |
314 | |
315 | read_efuse_byte(hw, efuse_addr, rtemp8); | |
316 | efuse_addr++; | |
317 | efuse_utilized++; | |
c84aa5af LF |
318 | efuse_word[i][offset] = |
319 | (*rtemp8 & 0xff); | |
0c817338 | 320 | |
e25f51d4 | 321 | if (efuse_addr >= efuse_len) |
0c817338 LF |
322 | break; |
323 | ||
324 | RTPRINT(rtlpriv, FEEPROM, | |
4c48869f JP |
325 | EFUSE_READ_ALL, |
326 | "Addr=%d\n", efuse_addr); | |
0c817338 LF |
327 | |
328 | read_efuse_byte(hw, efuse_addr, rtemp8); | |
329 | efuse_addr++; | |
330 | efuse_utilized++; | |
c84aa5af | 331 | efuse_word[i][offset] |= |
0c817338 LF |
332 | (((u16)*rtemp8 << 8) & 0xff00); |
333 | ||
e25f51d4 | 334 | if (efuse_addr >= efuse_len) |
0c817338 LF |
335 | break; |
336 | } | |
337 | ||
338 | wren >>= 1; | |
339 | } | |
340 | } | |
341 | ||
342 | RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL, | |
4c48869f | 343 | "Addr=%d\n", efuse_addr); |
0c817338 | 344 | read_efuse_byte(hw, efuse_addr, rtemp8); |
e25f51d4 | 345 | if (*rtemp8 != 0xFF && (efuse_addr < efuse_len)) { |
0c817338 LF |
346 | efuse_utilized++; |
347 | efuse_addr++; | |
348 | } | |
349 | } | |
350 | ||
e25f51d4 | 351 | for (i = 0; i < efuse_max_section; i++) { |
0c817338 LF |
352 | for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) { |
353 | efuse_tbl[(i * 8) + (j * 2)] = | |
c84aa5af | 354 | (efuse_word[j][i] & 0xff); |
0c817338 | 355 | efuse_tbl[(i * 8) + ((j * 2) + 1)] = |
c84aa5af | 356 | ((efuse_word[j][i] >> 8) & 0xff); |
0c817338 LF |
357 | } |
358 | } | |
359 | ||
360 | for (i = 0; i < _size_byte; i++) | |
361 | pbuf[i] = efuse_tbl[_offset + i]; | |
362 | ||
363 | rtlefuse->efuse_usedbytes = efuse_utilized; | |
e25f51d4 | 364 | efuse_usage = (u8) ((efuse_utilized * 100) / efuse_len); |
0c817338 LF |
365 | rtlefuse->efuse_usedpercentage = efuse_usage; |
366 | rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_BYTES, | |
367 | (u8 *)&efuse_utilized); | |
368 | rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_USAGE, | |
2c208890 | 369 | &efuse_usage); |
c84aa5af LF |
370 | done: |
371 | for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) | |
372 | kfree(efuse_word[i]); | |
373 | kfree(efuse_word); | |
450991fd | 374 | out: |
c84aa5af | 375 | kfree(efuse_tbl); |
0c817338 LF |
376 | } |
377 | ||
378 | bool efuse_shadow_update_chk(struct ieee80211_hw *hw) | |
379 | { | |
380 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
381 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
382 | u8 section_idx, i, Base; | |
383 | u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used; | |
32473284 | 384 | bool wordchanged, result = true; |
0c817338 LF |
385 | |
386 | for (section_idx = 0; section_idx < 16; section_idx++) { | |
387 | Base = section_idx * 8; | |
32473284 | 388 | wordchanged = false; |
0c817338 LF |
389 | |
390 | for (i = 0; i < 8; i = i + 2) { | |
391 | if ((rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i] != | |
392 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i]) || | |
393 | (rtlefuse->efuse_map[EFUSE_INIT_MAP][Base + i + 1] != | |
394 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][Base + i + | |
395 | 1])) { | |
396 | words_need++; | |
32473284 | 397 | wordchanged = true; |
0c817338 LF |
398 | } |
399 | } | |
400 | ||
e10542c4 | 401 | if (wordchanged) |
0c817338 LF |
402 | hdr_num++; |
403 | } | |
404 | ||
405 | totalbytes = hdr_num + words_need * 2; | |
406 | efuse_used = rtlefuse->efuse_usedbytes; | |
407 | ||
408 | if ((totalbytes + efuse_used) >= | |
3c67b8f9 | 409 | (EFUSE_MAX_SIZE - rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) |
32473284 | 410 | result = false; |
0c817338 LF |
411 | |
412 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, | |
f30d7507 JP |
413 | "efuse_shadow_update_chk(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n", |
414 | totalbytes, hdr_num, words_need, efuse_used); | |
0c817338 | 415 | |
32473284 | 416 | return result; |
0c817338 LF |
417 | } |
418 | ||
419 | void efuse_shadow_read(struct ieee80211_hw *hw, u8 type, | |
420 | u16 offset, u32 *value) | |
421 | { | |
422 | if (type == 1) | |
3c67b8f9 | 423 | efuse_shadow_read_1byte(hw, offset, (u8 *)value); |
0c817338 | 424 | else if (type == 2) |
3c67b8f9 | 425 | efuse_shadow_read_2byte(hw, offset, (u16 *)value); |
0c817338 | 426 | else if (type == 4) |
2c208890 | 427 | efuse_shadow_read_4byte(hw, offset, value); |
0c817338 LF |
428 | |
429 | } | |
3c67b8f9 | 430 | EXPORT_SYMBOL(efuse_shadow_read); |
0c817338 LF |
431 | |
432 | void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset, | |
433 | u32 value) | |
434 | { | |
435 | if (type == 1) | |
436 | efuse_shadow_write_1byte(hw, offset, (u8) value); | |
437 | else if (type == 2) | |
438 | efuse_shadow_write_2byte(hw, offset, (u16) value); | |
439 | else if (type == 4) | |
32473284 | 440 | efuse_shadow_write_4byte(hw, offset, value); |
0c817338 LF |
441 | |
442 | } | |
443 | ||
444 | bool efuse_shadow_update(struct ieee80211_hw *hw) | |
445 | { | |
446 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
447 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
448 | u16 i, offset, base; | |
449 | u8 word_en = 0x0F; | |
450 | u8 first_pg = false; | |
451 | ||
3c67b8f9 | 452 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n"); |
0c817338 LF |
453 | |
454 | if (!efuse_shadow_update_chk(hw)) { | |
455 | efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]); | |
6e9d592f IM |
456 | memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0], |
457 | &rtlefuse->efuse_map[EFUSE_INIT_MAP][0], | |
0c817338 LF |
458 | rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]); |
459 | ||
460 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, | |
3c67b8f9 | 461 | "efuse out of capacity!!\n"); |
0c817338 LF |
462 | return false; |
463 | } | |
464 | efuse_power_switch(hw, true, true); | |
465 | ||
466 | for (offset = 0; offset < 16; offset++) { | |
467 | ||
468 | word_en = 0x0F; | |
469 | base = offset * 8; | |
470 | ||
471 | for (i = 0; i < 8; i++) { | |
e10542c4 | 472 | if (first_pg) { |
0c817338 LF |
473 | word_en &= ~(BIT(i / 2)); |
474 | ||
475 | rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] = | |
476 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]; | |
477 | } else { | |
478 | ||
479 | if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] != | |
480 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) { | |
481 | word_en &= ~(BIT(i / 2)); | |
482 | ||
483 | rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] = | |
484 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
489 | if (word_en != 0x0F) { | |
490 | u8 tmpdata[8]; | |
6e9d592f IM |
491 | memcpy(tmpdata, |
492 | &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base], | |
493 | 8); | |
0c817338 | 494 | RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD, |
3c67b8f9 | 495 | "U-efuse\n", tmpdata, 8); |
0c817338 LF |
496 | |
497 | if (!efuse_pg_packet_write(hw, (u8) offset, word_en, | |
498 | tmpdata)) { | |
499 | RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, | |
f30d7507 | 500 | "PG section(%#x) fail!!\n", offset); |
0c817338 LF |
501 | break; |
502 | } | |
503 | } | |
504 | ||
505 | } | |
506 | ||
507 | efuse_power_switch(hw, true, false); | |
508 | efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]); | |
509 | ||
6e9d592f IM |
510 | memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0], |
511 | &rtlefuse->efuse_map[EFUSE_INIT_MAP][0], | |
0c817338 LF |
512 | rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]); |
513 | ||
3c67b8f9 | 514 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n"); |
0c817338 LF |
515 | return true; |
516 | } | |
517 | ||
518 | void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw) | |
519 | { | |
520 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
521 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
522 | ||
e10542c4 | 523 | if (rtlefuse->autoload_failflag) |
3c67b8f9 LF |
524 | memset((&rtlefuse->efuse_map[EFUSE_INIT_MAP][0]), |
525 | 0xFF, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]); | |
e25f51d4 | 526 | else |
0c817338 LF |
527 | efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]); |
528 | ||
6e9d592f | 529 | memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0], |
3c67b8f9 LF |
530 | &rtlefuse->efuse_map[EFUSE_INIT_MAP][0], |
531 | rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]); | |
0c817338 LF |
532 | |
533 | } | |
534 | EXPORT_SYMBOL(rtl_efuse_shadow_map_update); | |
535 | ||
536 | void efuse_force_write_vendor_Id(struct ieee80211_hw *hw) | |
537 | { | |
538 | u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF }; | |
539 | ||
540 | efuse_power_switch(hw, true, true); | |
541 | ||
542 | efuse_pg_packet_write(hw, 1, 0xD, tmpdata); | |
543 | ||
544 | efuse_power_switch(hw, true, false); | |
545 | ||
546 | } | |
547 | ||
548 | void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx) | |
549 | { | |
550 | } | |
551 | ||
552 | static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, | |
553 | u16 offset, u8 *value) | |
554 | { | |
555 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
556 | *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset]; | |
557 | } | |
558 | ||
559 | static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, | |
560 | u16 offset, u16 *value) | |
561 | { | |
562 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
563 | ||
564 | *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset]; | |
565 | *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8; | |
566 | ||
567 | } | |
568 | ||
569 | static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, | |
570 | u16 offset, u32 *value) | |
571 | { | |
572 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
573 | ||
574 | *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset]; | |
575 | *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8; | |
576 | *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16; | |
577 | *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24; | |
578 | } | |
579 | ||
580 | static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, | |
581 | u16 offset, u8 value) | |
582 | { | |
583 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
584 | ||
585 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value; | |
586 | } | |
587 | ||
588 | static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, | |
589 | u16 offset, u16 value) | |
590 | { | |
591 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
592 | ||
593 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF; | |
594 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8; | |
595 | ||
596 | } | |
597 | ||
598 | static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, | |
599 | u16 offset, u32 value) | |
600 | { | |
601 | struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); | |
602 | ||
603 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = | |
604 | (u8) (value & 0x000000FF); | |
605 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = | |
606 | (u8) ((value >> 8) & 0x0000FF); | |
607 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] = | |
608 | (u8) ((value >> 16) & 0x00FF); | |
609 | rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] = | |
610 | (u8) ((value >> 24) & 0xFF); | |
611 | ||
612 | } | |
613 | ||
3c67b8f9 | 614 | int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data) |
0c817338 LF |
615 | { |
616 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
617 | u8 tmpidx = 0; | |
32473284 | 618 | int result; |
0c817338 LF |
619 | |
620 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1, | |
621 | (u8) (addr & 0xff)); | |
622 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2, | |
623 | ((u8) ((addr >> 8) & 0x03)) | | |
624 | (rtl_read_byte(rtlpriv, | |
625 | rtlpriv->cfg->maps[EFUSE_CTRL] + 2) & | |
626 | 0xFC)); | |
627 | ||
628 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72); | |
629 | ||
630 | while (!(0x80 & rtl_read_byte(rtlpriv, | |
631 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3)) | |
632 | && (tmpidx < 100)) { | |
633 | tmpidx++; | |
634 | } | |
635 | ||
636 | if (tmpidx < 100) { | |
637 | *data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]); | |
32473284 | 638 | result = true; |
0c817338 LF |
639 | } else { |
640 | *data = 0xff; | |
32473284 | 641 | result = false; |
0c817338 | 642 | } |
32473284 | 643 | return result; |
0c817338 | 644 | } |
3c67b8f9 | 645 | EXPORT_SYMBOL(efuse_one_byte_read); |
0c817338 LF |
646 | |
647 | static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data) | |
648 | { | |
649 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
650 | u8 tmpidx = 0; | |
0c817338 | 651 | |
3c67b8f9 LF |
652 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, |
653 | "Addr = %x Data=%x\n", addr, data); | |
0c817338 LF |
654 | |
655 | rtl_write_byte(rtlpriv, | |
656 | rtlpriv->cfg->maps[EFUSE_CTRL] + 1, (u8) (addr & 0xff)); | |
657 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2, | |
658 | (rtl_read_byte(rtlpriv, | |
659 | rtlpriv->cfg->maps[EFUSE_CTRL] + | |
660 | 2) & 0xFC) | (u8) ((addr >> 8) & 0x03)); | |
661 | ||
662 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data); | |
663 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2); | |
664 | ||
665 | while ((0x80 & rtl_read_byte(rtlpriv, | |
666 | rtlpriv->cfg->maps[EFUSE_CTRL] + 3)) | |
667 | && (tmpidx < 100)) { | |
668 | tmpidx++; | |
669 | } | |
670 | ||
671 | if (tmpidx < 100) | |
32473284 | 672 | return true; |
32473284 | 673 | return false; |
0c817338 LF |
674 | } |
675 | ||
3c67b8f9 | 676 | static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse) |
0c817338 | 677 | { |
e25f51d4 | 678 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
0c817338 | 679 | efuse_power_switch(hw, false, true); |
e25f51d4 | 680 | read_efuse(hw, 0, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE], efuse); |
0c817338 LF |
681 | efuse_power_switch(hw, false, false); |
682 | } | |
683 | ||
684 | static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr, | |
685 | u8 efuse_data, u8 offset, u8 *tmpdata, | |
686 | u8 *readstate) | |
687 | { | |
e25f51d4 | 688 | bool dataempty = true; |
0c817338 LF |
689 | u8 hoffset; |
690 | u8 tmpidx; | |
691 | u8 hworden; | |
692 | u8 word_cnts; | |
693 | ||
694 | hoffset = (efuse_data >> 4) & 0x0F; | |
695 | hworden = efuse_data & 0x0F; | |
696 | word_cnts = efuse_calculate_word_cnts(hworden); | |
697 | ||
698 | if (hoffset == offset) { | |
699 | for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) { | |
700 | if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx, | |
3c67b8f9 | 701 | &efuse_data)) { |
0c817338 LF |
702 | tmpdata[tmpidx] = efuse_data; |
703 | if (efuse_data != 0xff) | |
3c67b8f9 | 704 | dataempty = false; |
0c817338 LF |
705 | } |
706 | } | |
707 | ||
3c67b8f9 | 708 | if (!dataempty) { |
0c817338 | 709 | *readstate = PG_STATE_DATA; |
e25f51d4 | 710 | } else { |
0c817338 LF |
711 | *efuse_addr = *efuse_addr + (word_cnts * 2) + 1; |
712 | *readstate = PG_STATE_HEADER; | |
713 | } | |
714 | ||
715 | } else { | |
716 | *efuse_addr = *efuse_addr + (word_cnts * 2) + 1; | |
717 | *readstate = PG_STATE_HEADER; | |
718 | } | |
719 | } | |
720 | ||
721 | static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data) | |
722 | { | |
723 | u8 readstate = PG_STATE_HEADER; | |
3c67b8f9 | 724 | |
32473284 | 725 | bool continual = true; |
3c67b8f9 | 726 | |
0c817338 LF |
727 | u8 efuse_data, word_cnts = 0; |
728 | u16 efuse_addr = 0; | |
0c817338 LF |
729 | u8 tmpdata[8]; |
730 | ||
731 | if (data == NULL) | |
732 | return false; | |
733 | if (offset > 15) | |
734 | return false; | |
735 | ||
6e9d592f IM |
736 | memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8)); |
737 | memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8)); | |
0c817338 | 738 | |
32473284 | 739 | while (continual && (efuse_addr < EFUSE_MAX_SIZE)) { |
0c817338 LF |
740 | if (readstate & PG_STATE_HEADER) { |
741 | if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) | |
742 | && (efuse_data != 0xFF)) | |
743 | efuse_read_data_case1(hw, &efuse_addr, | |
3c67b8f9 LF |
744 | efuse_data, offset, |
745 | tmpdata, &readstate); | |
0c817338 | 746 | else |
32473284 | 747 | continual = false; |
0c817338 | 748 | } else if (readstate & PG_STATE_DATA) { |
32473284 | 749 | efuse_word_enable_data_read(0, tmpdata, data); |
0c817338 LF |
750 | efuse_addr = efuse_addr + (word_cnts * 2) + 1; |
751 | readstate = PG_STATE_HEADER; | |
752 | } | |
753 | ||
754 | } | |
755 | ||
756 | if ((data[0] == 0xff) && (data[1] == 0xff) && | |
757 | (data[2] == 0xff) && (data[3] == 0xff) && | |
758 | (data[4] == 0xff) && (data[5] == 0xff) && | |
759 | (data[6] == 0xff) && (data[7] == 0xff)) | |
760 | return false; | |
761 | else | |
762 | return true; | |
763 | ||
764 | } | |
765 | ||
766 | static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr, | |
3c67b8f9 LF |
767 | u8 efuse_data, u8 offset, |
768 | int *continual, u8 *write_state, | |
769 | struct pgpkt_struct *target_pkt, | |
770 | int *repeat_times, int *result, u8 word_en) | |
0c817338 LF |
771 | { |
772 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
773 | struct pgpkt_struct tmp_pkt; | |
3c67b8f9 | 774 | int dataempty = true; |
0c817338 LF |
775 | u8 originaldata[8 * sizeof(u8)]; |
776 | u8 badworden = 0x0F; | |
777 | u8 match_word_en, tmp_word_en; | |
778 | u8 tmpindex; | |
779 | u8 tmp_header = efuse_data; | |
780 | u8 tmp_word_cnts; | |
781 | ||
782 | tmp_pkt.offset = (tmp_header >> 4) & 0x0F; | |
783 | tmp_pkt.word_en = tmp_header & 0x0F; | |
784 | tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en); | |
785 | ||
bc5892c9 CL |
786 | if (tmp_pkt.offset != target_pkt->offset) { |
787 | *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1; | |
0c817338 LF |
788 | *write_state = PG_STATE_HEADER; |
789 | } else { | |
790 | for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) { | |
3c67b8f9 LF |
791 | if (efuse_one_byte_read(hw, |
792 | (*efuse_addr + 1 + tmpindex), | |
793 | &efuse_data) && | |
794 | (efuse_data != 0xFF)) | |
32473284 | 795 | dataempty = false; |
0c817338 LF |
796 | } |
797 | ||
23677ce3 | 798 | if (!dataempty) { |
bc5892c9 | 799 | *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1; |
0c817338 LF |
800 | *write_state = PG_STATE_HEADER; |
801 | } else { | |
802 | match_word_en = 0x0F; | |
bc5892c9 | 803 | if (!((target_pkt->word_en & BIT(0)) | |
3c67b8f9 | 804 | (tmp_pkt.word_en & BIT(0)))) |
0c817338 LF |
805 | match_word_en &= (~BIT(0)); |
806 | ||
bc5892c9 | 807 | if (!((target_pkt->word_en & BIT(1)) | |
3c67b8f9 | 808 | (tmp_pkt.word_en & BIT(1)))) |
0c817338 LF |
809 | match_word_en &= (~BIT(1)); |
810 | ||
bc5892c9 | 811 | if (!((target_pkt->word_en & BIT(2)) | |
3c67b8f9 | 812 | (tmp_pkt.word_en & BIT(2)))) |
0c817338 LF |
813 | match_word_en &= (~BIT(2)); |
814 | ||
bc5892c9 | 815 | if (!((target_pkt->word_en & BIT(3)) | |
3c67b8f9 | 816 | (tmp_pkt.word_en & BIT(3)))) |
0c817338 LF |
817 | match_word_en &= (~BIT(3)); |
818 | ||
819 | if ((match_word_en & 0x0F) != 0x0F) { | |
3c67b8f9 LF |
820 | badworden = |
821 | enable_efuse_data_write(hw, | |
822 | *efuse_addr + 1, | |
823 | tmp_pkt.word_en, | |
824 | target_pkt->data); | |
0c817338 | 825 | |
3c67b8f9 | 826 | if (0x0F != (badworden & 0x0F)) { |
0c817338 LF |
827 | u8 reorg_offset = offset; |
828 | u8 reorg_worden = badworden; | |
829 | efuse_pg_packet_write(hw, reorg_offset, | |
3c67b8f9 LF |
830 | reorg_worden, |
831 | originaldata); | |
0c817338 LF |
832 | } |
833 | ||
834 | tmp_word_en = 0x0F; | |
bc5892c9 | 835 | if ((target_pkt->word_en & BIT(0)) ^ |
0c817338 LF |
836 | (match_word_en & BIT(0))) |
837 | tmp_word_en &= (~BIT(0)); | |
838 | ||
bc5892c9 | 839 | if ((target_pkt->word_en & BIT(1)) ^ |
0c817338 LF |
840 | (match_word_en & BIT(1))) |
841 | tmp_word_en &= (~BIT(1)); | |
842 | ||
bc5892c9 | 843 | if ((target_pkt->word_en & BIT(2)) ^ |
3c67b8f9 | 844 | (match_word_en & BIT(2))) |
0c817338 LF |
845 | tmp_word_en &= (~BIT(2)); |
846 | ||
bc5892c9 | 847 | if ((target_pkt->word_en & BIT(3)) ^ |
3c67b8f9 | 848 | (match_word_en & BIT(3))) |
0c817338 LF |
849 | tmp_word_en &= (~BIT(3)); |
850 | ||
851 | if ((tmp_word_en & 0x0F) != 0x0F) { | |
852 | *efuse_addr = efuse_get_current_size(hw); | |
bc5892c9 CL |
853 | target_pkt->offset = offset; |
854 | target_pkt->word_en = tmp_word_en; | |
e25f51d4 | 855 | } else { |
32473284 | 856 | *continual = false; |
e25f51d4 | 857 | } |
0c817338 LF |
858 | *write_state = PG_STATE_HEADER; |
859 | *repeat_times += 1; | |
860 | if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) { | |
32473284 LF |
861 | *continual = false; |
862 | *result = false; | |
0c817338 LF |
863 | } |
864 | } else { | |
865 | *efuse_addr += (2 * tmp_word_cnts) + 1; | |
bc5892c9 CL |
866 | target_pkt->offset = offset; |
867 | target_pkt->word_en = word_en; | |
0c817338 LF |
868 | *write_state = PG_STATE_HEADER; |
869 | } | |
870 | } | |
871 | } | |
3c67b8f9 | 872 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse PG_STATE_HEADER-1\n"); |
0c817338 LF |
873 | } |
874 | ||
875 | static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr, | |
32473284 | 876 | int *continual, u8 *write_state, |
0c817338 | 877 | struct pgpkt_struct target_pkt, |
32473284 | 878 | int *repeat_times, int *result) |
0c817338 LF |
879 | { |
880 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
881 | struct pgpkt_struct tmp_pkt; | |
882 | u8 pg_header; | |
883 | u8 tmp_header; | |
884 | u8 originaldata[8 * sizeof(u8)]; | |
885 | u8 tmp_word_cnts; | |
886 | u8 badworden = 0x0F; | |
887 | ||
888 | pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en; | |
889 | efuse_one_byte_write(hw, *efuse_addr, pg_header); | |
890 | efuse_one_byte_read(hw, *efuse_addr, &tmp_header); | |
891 | ||
e25f51d4 | 892 | if (tmp_header == pg_header) { |
0c817338 | 893 | *write_state = PG_STATE_DATA; |
e25f51d4 | 894 | } else if (tmp_header == 0xFF) { |
0c817338 LF |
895 | *write_state = PG_STATE_HEADER; |
896 | *repeat_times += 1; | |
897 | if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) { | |
32473284 LF |
898 | *continual = false; |
899 | *result = false; | |
0c817338 LF |
900 | } |
901 | } else { | |
902 | tmp_pkt.offset = (tmp_header >> 4) & 0x0F; | |
903 | tmp_pkt.word_en = tmp_header & 0x0F; | |
904 | ||
905 | tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en); | |
906 | ||
3c67b8f9 | 907 | memset(originaldata, 0xff, 8 * sizeof(u8)); |
0c817338 LF |
908 | |
909 | if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) { | |
3c67b8f9 LF |
910 | badworden = enable_efuse_data_write(hw, |
911 | *efuse_addr + 1, | |
912 | tmp_pkt.word_en, | |
913 | originaldata); | |
0c817338 LF |
914 | |
915 | if (0x0F != (badworden & 0x0F)) { | |
916 | u8 reorg_offset = tmp_pkt.offset; | |
917 | u8 reorg_worden = badworden; | |
918 | efuse_pg_packet_write(hw, reorg_offset, | |
919 | reorg_worden, | |
920 | originaldata); | |
921 | *efuse_addr = efuse_get_current_size(hw); | |
e25f51d4 | 922 | } else { |
3c67b8f9 LF |
923 | *efuse_addr = *efuse_addr + |
924 | (tmp_word_cnts * 2) + 1; | |
e25f51d4 C |
925 | } |
926 | } else { | |
0c817338 | 927 | *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1; |
e25f51d4 | 928 | } |
0c817338 LF |
929 | |
930 | *write_state = PG_STATE_HEADER; | |
931 | *repeat_times += 1; | |
932 | if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) { | |
32473284 LF |
933 | *continual = false; |
934 | *result = false; | |
0c817338 LF |
935 | } |
936 | ||
937 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, | |
4c48869f | 938 | "efuse PG_STATE_HEADER-2\n"); |
0c817338 LF |
939 | } |
940 | } | |
941 | ||
942 | static int efuse_pg_packet_write(struct ieee80211_hw *hw, | |
943 | u8 offset, u8 word_en, u8 *data) | |
944 | { | |
945 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
946 | struct pgpkt_struct target_pkt; | |
947 | u8 write_state = PG_STATE_HEADER; | |
3c67b8f9 | 948 | int continual = true, dataempty = true, result = true; |
0c817338 LF |
949 | u16 efuse_addr = 0; |
950 | u8 efuse_data; | |
951 | u8 target_word_cnts = 0; | |
952 | u8 badworden = 0x0F; | |
953 | static int repeat_times; | |
954 | ||
26634c4b | 955 | if (efuse_get_current_size(hw) >= (EFUSE_MAX_SIZE - |
3c67b8f9 | 956 | rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) { |
0c817338 | 957 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, |
4c48869f | 958 | "efuse_pg_packet_write error\n"); |
0c817338 LF |
959 | return false; |
960 | } | |
961 | ||
962 | target_pkt.offset = offset; | |
963 | target_pkt.word_en = word_en; | |
964 | ||
3c67b8f9 | 965 | memset(target_pkt.data, 0xFF, 8 * sizeof(u8)); |
0c817338 LF |
966 | |
967 | efuse_word_enable_data_read(word_en, data, target_pkt.data); | |
968 | target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en); | |
969 | ||
3c67b8f9 | 970 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse Power ON\n"); |
0c817338 | 971 | |
26634c4b | 972 | while (continual && (efuse_addr < (EFUSE_MAX_SIZE - |
3c67b8f9 | 973 | rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))) { |
0c817338 LF |
974 | |
975 | if (write_state == PG_STATE_HEADER) { | |
3c67b8f9 | 976 | dataempty = true; |
0c817338 LF |
977 | badworden = 0x0F; |
978 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, | |
4c48869f | 979 | "efuse PG_STATE_HEADER\n"); |
0c817338 LF |
980 | |
981 | if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) && | |
982 | (efuse_data != 0xFF)) | |
983 | efuse_write_data_case1(hw, &efuse_addr, | |
984 | efuse_data, offset, | |
32473284 | 985 | &continual, |
3c67b8f9 LF |
986 | &write_state, |
987 | &target_pkt, | |
32473284 | 988 | &repeat_times, &result, |
0c817338 LF |
989 | word_en); |
990 | else | |
991 | efuse_write_data_case2(hw, &efuse_addr, | |
32473284 | 992 | &continual, |
0c817338 LF |
993 | &write_state, |
994 | target_pkt, | |
995 | &repeat_times, | |
32473284 | 996 | &result); |
0c817338 LF |
997 | |
998 | } else if (write_state == PG_STATE_DATA) { | |
999 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, | |
4c48869f | 1000 | "efuse PG_STATE_DATA\n"); |
3c67b8f9 | 1001 | badworden = 0x0f; |
0c817338 | 1002 | badworden = |
3c67b8f9 LF |
1003 | enable_efuse_data_write(hw, efuse_addr + 1, |
1004 | target_pkt.word_en, | |
1005 | target_pkt.data); | |
0c817338 LF |
1006 | |
1007 | if ((badworden & 0x0F) == 0x0F) { | |
32473284 | 1008 | continual = false; |
0c817338 | 1009 | } else { |
3c67b8f9 LF |
1010 | efuse_addr = |
1011 | efuse_addr + (2 * target_word_cnts) + 1; | |
0c817338 LF |
1012 | |
1013 | target_pkt.offset = offset; | |
1014 | target_pkt.word_en = badworden; | |
1015 | target_word_cnts = | |
1016 | efuse_calculate_word_cnts(target_pkt. | |
1017 | word_en); | |
1018 | write_state = PG_STATE_HEADER; | |
1019 | repeat_times++; | |
1020 | if (repeat_times > EFUSE_REPEAT_THRESHOLD_) { | |
32473284 LF |
1021 | continual = false; |
1022 | result = false; | |
0c817338 LF |
1023 | } |
1024 | RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, | |
4c48869f | 1025 | "efuse PG_STATE_HEADER-3\n"); |
0c817338 LF |
1026 | } |
1027 | } | |
1028 | } | |
1029 | ||
26634c4b | 1030 | if (efuse_addr >= (EFUSE_MAX_SIZE - |
3c67b8f9 | 1031 | rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) { |
0c817338 | 1032 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, |
f30d7507 | 1033 | "efuse_addr(%#x) Out of size!!\n", efuse_addr); |
0c817338 LF |
1034 | } |
1035 | ||
1036 | return true; | |
1037 | } | |
1038 | ||
3c67b8f9 LF |
1039 | static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata, |
1040 | u8 *targetdata) | |
0c817338 LF |
1041 | { |
1042 | if (!(word_en & BIT(0))) { | |
1043 | targetdata[0] = sourdata[0]; | |
1044 | targetdata[1] = sourdata[1]; | |
1045 | } | |
1046 | ||
1047 | if (!(word_en & BIT(1))) { | |
1048 | targetdata[2] = sourdata[2]; | |
1049 | targetdata[3] = sourdata[3]; | |
1050 | } | |
1051 | ||
1052 | if (!(word_en & BIT(2))) { | |
1053 | targetdata[4] = sourdata[4]; | |
1054 | targetdata[5] = sourdata[5]; | |
1055 | } | |
1056 | ||
1057 | if (!(word_en & BIT(3))) { | |
1058 | targetdata[6] = sourdata[6]; | |
1059 | targetdata[7] = sourdata[7]; | |
1060 | } | |
1061 | } | |
1062 | ||
3c67b8f9 LF |
1063 | static u8 enable_efuse_data_write(struct ieee80211_hw *hw, |
1064 | u16 efuse_addr, u8 word_en, u8 *data) | |
0c817338 LF |
1065 | { |
1066 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
1067 | u16 tmpaddr; | |
1068 | u16 start_addr = efuse_addr; | |
1069 | u8 badworden = 0x0F; | |
1070 | u8 tmpdata[8]; | |
1071 | ||
6e9d592f | 1072 | memset(tmpdata, 0xff, PGPKT_DATA_SIZE); |
3c67b8f9 LF |
1073 | RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, |
1074 | "word_en = %x efuse_addr=%x\n", word_en, efuse_addr); | |
0c817338 LF |
1075 | |
1076 | if (!(word_en & BIT(0))) { | |
1077 | tmpaddr = start_addr; | |
1078 | efuse_one_byte_write(hw, start_addr++, data[0]); | |
1079 | efuse_one_byte_write(hw, start_addr++, data[1]); | |
1080 | ||
1081 | efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]); | |
1082 | efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]); | |
1083 | if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1])) | |
1084 | badworden &= (~BIT(0)); | |
1085 | } | |
1086 | ||
1087 | if (!(word_en & BIT(1))) { | |
1088 | tmpaddr = start_addr; | |
1089 | efuse_one_byte_write(hw, start_addr++, data[2]); | |
1090 | efuse_one_byte_write(hw, start_addr++, data[3]); | |
1091 | ||
1092 | efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]); | |
1093 | efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]); | |
1094 | if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3])) | |
1095 | badworden &= (~BIT(1)); | |
1096 | } | |
1097 | ||
1098 | if (!(word_en & BIT(2))) { | |
1099 | tmpaddr = start_addr; | |
1100 | efuse_one_byte_write(hw, start_addr++, data[4]); | |
1101 | efuse_one_byte_write(hw, start_addr++, data[5]); | |
1102 | ||
1103 | efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]); | |
1104 | efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]); | |
1105 | if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5])) | |
1106 | badworden &= (~BIT(2)); | |
1107 | } | |
1108 | ||
1109 | if (!(word_en & BIT(3))) { | |
1110 | tmpaddr = start_addr; | |
1111 | efuse_one_byte_write(hw, start_addr++, data[6]); | |
1112 | efuse_one_byte_write(hw, start_addr++, data[7]); | |
1113 | ||
1114 | efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]); | |
1115 | efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]); | |
1116 | if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7])) | |
1117 | badworden &= (~BIT(3)); | |
1118 | } | |
1119 | ||
1120 | return badworden; | |
1121 | } | |
1122 | ||
32473284 | 1123 | static void efuse_power_switch(struct ieee80211_hw *hw, u8 write, u8 pwrstate) |
0c817338 LF |
1124 | { |
1125 | struct rtl_priv *rtlpriv = rtl_priv(hw); | |
e25f51d4 | 1126 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
0c817338 LF |
1127 | u8 tempval; |
1128 | u16 tmpV16; | |
1129 | ||
5c69177d | 1130 | if (pwrstate && (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)) { |
5c69177d | 1131 | |
3c67b8f9 LF |
1132 | if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE && |
1133 | rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) { | |
1134 | rtl_write_byte(rtlpriv, | |
1135 | rtlpriv->cfg->maps[EFUSE_ACCESS], 0x69); | |
1136 | } else { | |
1137 | tmpV16 = | |
1138 | rtl_read_word(rtlpriv, | |
1139 | rtlpriv->cfg->maps[SYS_ISO_CTRL]); | |
1140 | if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) { | |
1141 | tmpV16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V]; | |
1142 | rtl_write_word(rtlpriv, | |
1143 | rtlpriv->cfg->maps[SYS_ISO_CTRL], | |
1144 | tmpV16); | |
1145 | } | |
0c817338 | 1146 | } |
0c817338 LF |
1147 | tmpV16 = rtl_read_word(rtlpriv, |
1148 | rtlpriv->cfg->maps[SYS_FUNC_EN]); | |
1149 | if (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) { | |
1150 | tmpV16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR]; | |
1151 | rtl_write_word(rtlpriv, | |
1152 | rtlpriv->cfg->maps[SYS_FUNC_EN], tmpV16); | |
1153 | } | |
1154 | ||
1155 | tmpV16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]); | |
1156 | if ((!(tmpV16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) || | |
1157 | (!(tmpV16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) { | |
1158 | tmpV16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] | | |
1159 | rtlpriv->cfg->maps[EFUSE_ANA8M]); | |
1160 | rtl_write_word(rtlpriv, | |
1161 | rtlpriv->cfg->maps[SYS_CLK], tmpV16); | |
1162 | } | |
1163 | } | |
1164 | ||
32473284 LF |
1165 | if (pwrstate) { |
1166 | if (write) { | |
0c817338 LF |
1167 | tempval = rtl_read_byte(rtlpriv, |
1168 | rtlpriv->cfg->maps[EFUSE_TEST] + | |
1169 | 3); | |
e25f51d4 | 1170 | |
3c67b8f9 LF |
1171 | if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) { |
1172 | tempval &= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6)); | |
1173 | tempval |= (VOLTAGE_V25 << 3); | |
1174 | } else if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE) { | |
e25f51d4 C |
1175 | tempval &= 0x0F; |
1176 | tempval |= (VOLTAGE_V25 << 4); | |
1177 | } | |
1178 | ||
0c817338 LF |
1179 | rtl_write_byte(rtlpriv, |
1180 | rtlpriv->cfg->maps[EFUSE_TEST] + 3, | |
1181 | (tempval | 0x80)); | |
1182 | } | |
1183 | ||
e25f51d4 C |
1184 | if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) { |
1185 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK], | |
3c67b8f9 | 1186 | 0x03); |
e25f51d4 | 1187 | } |
0c817338 | 1188 | } else { |
3c67b8f9 LF |
1189 | if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE && |
1190 | rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) | |
5c69177d LF |
1191 | rtl_write_byte(rtlpriv, |
1192 | rtlpriv->cfg->maps[EFUSE_ACCESS], 0); | |
1193 | ||
32473284 | 1194 | if (write) { |
0c817338 LF |
1195 | tempval = rtl_read_byte(rtlpriv, |
1196 | rtlpriv->cfg->maps[EFUSE_TEST] + | |
1197 | 3); | |
1198 | rtl_write_byte(rtlpriv, | |
1199 | rtlpriv->cfg->maps[EFUSE_TEST] + 3, | |
1200 | (tempval & 0x7F)); | |
1201 | } | |
1202 | ||
e25f51d4 C |
1203 | if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) { |
1204 | rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK], | |
3c67b8f9 | 1205 | 0x02); |
e25f51d4 | 1206 | } |
0c817338 | 1207 | } |
0c817338 LF |
1208 | } |
1209 | ||
1210 | static u16 efuse_get_current_size(struct ieee80211_hw *hw) | |
1211 | { | |
3c67b8f9 | 1212 | int continual = true; |
0c817338 | 1213 | u16 efuse_addr = 0; |
3c67b8f9 | 1214 | u8 hoffset, hworden; |
0c817338 LF |
1215 | u8 efuse_data, word_cnts; |
1216 | ||
3c67b8f9 LF |
1217 | while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data) && |
1218 | (efuse_addr < EFUSE_MAX_SIZE)) { | |
1219 | if (efuse_data != 0xFF) { | |
1220 | hoffset = (efuse_data >> 4) & 0x0F; | |
1221 | hworden = efuse_data & 0x0F; | |
1222 | word_cnts = efuse_calculate_word_cnts(hworden); | |
1223 | efuse_addr = efuse_addr + (word_cnts * 2) + 1; | |
1224 | } else { | |
1225 | continual = false; | |
1226 | } | |
0c817338 LF |
1227 | } |
1228 | ||
1229 | return efuse_addr; | |
1230 | } | |
1231 | ||
1232 | static u8 efuse_calculate_word_cnts(u8 word_en) | |
1233 | { | |
1234 | u8 word_cnts = 0; | |
1235 | if (!(word_en & BIT(0))) | |
1236 | word_cnts++; | |
1237 | if (!(word_en & BIT(1))) | |
1238 | word_cnts++; | |
1239 | if (!(word_en & BIT(2))) | |
1240 | word_cnts++; | |
1241 | if (!(word_en & BIT(3))) | |
1242 | word_cnts++; | |
1243 | return word_cnts; | |
1244 | } | |
1245 |