mtd: Use kasprintf() instead of fixed buffer formatting
[linux/fpc-iii.git] / drivers / staging / rtlwifi / efuse.c
blob1dc71455f270c5539fe2aaff39a07becc9cd8560
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2009-2012 Realtek Corporation.
6 * Contact Information:
7 * wlanfae <wlanfae@realtek.com>
8 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
9 * Hsinchu 300, Taiwan.
11 * Larry Finger <Larry.Finger@lwfinger.net>
13 *****************************************************************************/
14 #include "wifi.h"
15 #include "efuse.h"
16 #include "pci.h"
17 #include <linux/export.h>
19 static const u8 MAX_PGPKT_SIZE = 9;
20 static const u8 PGPKT_DATA_SIZE = 8;
21 static const int EFUSE_MAX_SIZE = 512;
23 #define START_ADDRESS 0x1000
24 #define REG_MCUFWDL 0x0080
26 static const struct efuse_map RTL8712_SDIO_EFUSE_TABLE[] = {
27 {0, 0, 0, 2},
28 {0, 1, 0, 2},
29 {0, 2, 0, 2},
30 {1, 0, 0, 1},
31 {1, 0, 1, 1},
32 {1, 1, 0, 1},
33 {1, 1, 1, 3},
34 {1, 3, 0, 17},
35 {3, 3, 1, 48},
36 {10, 0, 0, 6},
37 {10, 3, 0, 1},
38 {10, 3, 1, 1},
39 {11, 0, 0, 28}
42 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw, u16 offset,
43 u8 *value);
44 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw, u16 offset,
45 u16 *value);
46 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw, u16 offset,
47 u32 *value);
48 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw, u16 offset,
49 u8 value);
50 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw, u16 offset,
51 u16 value);
52 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw, u16 offset,
53 u32 value);
54 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr,
55 u8 data);
56 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse);
57 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset,
58 u8 *data);
59 static int efuse_pg_packet_write(struct ieee80211_hw *hw, u8 offset,
60 u8 word_en, u8 *data);
61 static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
62 u8 *targetdata);
63 static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
64 u16 efuse_addr, u8 word_en, u8 *data);
65 static u16 efuse_get_current_size(struct ieee80211_hw *hw);
66 static u8 efuse_calculate_word_cnts(u8 word_en);
68 void efuse_initialize(struct ieee80211_hw *hw)
70 struct rtl_priv *rtlpriv = rtl_priv(hw);
71 u8 bytetemp;
72 u8 temp;
74 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1);
75 temp = bytetemp | 0x20;
76 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_FUNC_EN] + 1, temp);
78 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1);
79 temp = bytetemp & 0xFE;
80 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[SYS_ISO_CTRL] + 1, temp);
82 bytetemp = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3);
83 temp = bytetemp | 0x80;
84 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST] + 3, temp);
86 rtl_write_byte(rtlpriv, 0x2F8, 0x3);
88 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
91 u8 efuse_read_1byte(struct ieee80211_hw *hw, u16 address)
93 struct rtl_priv *rtlpriv = rtl_priv(hw);
94 u8 data;
95 u8 bytetemp;
96 u8 temp;
97 u32 k = 0;
98 const u32 efuse_len =
99 rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
101 if (address < efuse_len) {
102 temp = address & 0xFF;
103 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
104 temp);
105 bytetemp = rtl_read_byte(rtlpriv,
106 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
107 temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
108 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
109 temp);
111 bytetemp = rtl_read_byte(rtlpriv,
112 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
113 temp = bytetemp & 0x7F;
114 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
115 temp);
117 bytetemp = rtl_read_byte(rtlpriv,
118 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
119 while (!(bytetemp & 0x80)) {
120 bytetemp =
121 rtl_read_byte(rtlpriv,
122 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
123 k++;
124 if (k == 1000) {
125 k = 0;
126 break;
129 data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
130 return data;
132 return 0xFF;
135 void efuse_write_1byte(struct ieee80211_hw *hw, u16 address, u8 value)
137 struct rtl_priv *rtlpriv = rtl_priv(hw);
138 u8 bytetemp;
139 u8 temp;
140 u32 k = 0;
141 const u32 efuse_len =
142 rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
144 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "Addr=%x Data =%x\n",
145 address, value);
147 if (address < efuse_len) {
148 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], value);
150 temp = address & 0xFF;
151 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
152 temp);
153 bytetemp = rtl_read_byte(rtlpriv,
154 rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
156 temp = ((address >> 8) & 0x03) | (bytetemp & 0xFC);
157 rtl_write_byte(rtlpriv,
158 rtlpriv->cfg->maps[EFUSE_CTRL] + 2, temp);
160 bytetemp = rtl_read_byte(rtlpriv,
161 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
162 temp = bytetemp | 0x80;
163 rtl_write_byte(rtlpriv,
164 rtlpriv->cfg->maps[EFUSE_CTRL] + 3, temp);
166 bytetemp = rtl_read_byte(rtlpriv,
167 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
169 while (bytetemp & 0x80) {
170 bytetemp =
171 rtl_read_byte(rtlpriv,
172 rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
173 k++;
174 if (k == 100) {
175 k = 0;
176 break;
182 void read_efuse_byte(struct ieee80211_hw *hw, u16 _offset, u8 *pbuf)
184 struct rtl_priv *rtlpriv = rtl_priv(hw);
185 u32 value32;
186 u8 readbyte;
187 u16 retry;
189 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
190 (_offset & 0xff));
191 readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2);
192 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
193 ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
195 readbyte = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3);
196 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3,
197 (readbyte & 0x7f));
199 retry = 0;
200 value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
201 while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
202 value32 = rtl_read_dword(rtlpriv,
203 rtlpriv->cfg->maps[EFUSE_CTRL]);
204 retry++;
207 udelay(50);
208 value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
210 *pbuf = (u8)(value32 & 0xff);
213 void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
215 struct rtl_priv *rtlpriv = rtl_priv(hw);
216 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
217 u8 *efuse_tbl;
218 u8 rtemp8[1];
219 u16 efuse_addr = 0;
220 u8 offset, wren;
221 u8 u1temp = 0;
222 u16 i;
223 u16 j;
224 const u16 efuse_max_section =
225 rtlpriv->cfg->maps[EFUSE_MAX_SECTION_MAP];
226 const u32 efuse_len =
227 rtlpriv->cfg->maps[EFUSE_REAL_CONTENT_SIZE];
228 u16 **efuse_word;
229 u16 efuse_utilized = 0;
230 u8 efuse_usage;
232 if ((_offset + _size_byte) > rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]) {
233 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
234 "%s(): Invalid offset(%#x) with read bytes(%#x)!!\n",
235 __func__, _offset, _size_byte);
236 return;
239 /* allocate memory for efuse_tbl and efuse_word */
240 efuse_tbl = kzalloc(rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE],
241 GFP_ATOMIC);
242 if (!efuse_tbl)
243 return;
244 efuse_word = kcalloc(EFUSE_MAX_WORD_UNIT, sizeof(u16 *), GFP_ATOMIC);
245 if (!efuse_word)
246 goto out;
247 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
248 efuse_word[i] = kcalloc(efuse_max_section, sizeof(u16), GFP_ATOMIC);
249 if (!efuse_word[i])
250 goto done;
253 for (i = 0; i < efuse_max_section; i++)
254 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
255 efuse_word[j][i] = 0xFFFF;
257 read_efuse_byte(hw, efuse_addr, rtemp8);
258 if (*rtemp8 != 0xFF) {
259 efuse_utilized++;
260 RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
261 "Addr=%d\n", efuse_addr);
262 efuse_addr++;
265 while ((*rtemp8 != 0xFF) && (efuse_addr < efuse_len)) {
266 /* Check PG header for section num. */
267 if ((*rtemp8 & 0x1F) == 0x0F) {/* extended header */
268 u1temp = ((*rtemp8 & 0xE0) >> 5);
269 read_efuse_byte(hw, efuse_addr, rtemp8);
271 if ((*rtemp8 & 0x0F) == 0x0F) {
272 efuse_addr++;
273 read_efuse_byte(hw, efuse_addr, rtemp8);
275 if (*rtemp8 != 0xFF &&
276 (efuse_addr < efuse_len)) {
277 efuse_addr++;
279 continue;
280 } else {
281 offset = ((*rtemp8 & 0xF0) >> 1) | u1temp;
282 wren = (*rtemp8 & 0x0F);
283 efuse_addr++;
285 } else {
286 offset = ((*rtemp8 >> 4) & 0x0f);
287 wren = (*rtemp8 & 0x0f);
290 if (offset < efuse_max_section) {
291 RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
292 "offset-%d Worden=%x\n", offset, wren);
294 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
295 if (!(wren & 0x01)) {
296 RTPRINT(rtlpriv, FEEPROM,
297 EFUSE_READ_ALL,
298 "Addr=%d\n", efuse_addr);
300 read_efuse_byte(hw, efuse_addr, rtemp8);
301 efuse_addr++;
302 efuse_utilized++;
303 efuse_word[i][offset] =
304 (*rtemp8 & 0xff);
306 if (efuse_addr >= efuse_len)
307 break;
309 RTPRINT(rtlpriv, FEEPROM,
310 EFUSE_READ_ALL,
311 "Addr=%d\n", efuse_addr);
313 read_efuse_byte(hw, efuse_addr, rtemp8);
314 efuse_addr++;
315 efuse_utilized++;
316 efuse_word[i][offset] |=
317 (((u16)*rtemp8 << 8) & 0xff00);
319 if (efuse_addr >= efuse_len)
320 break;
323 wren >>= 1;
327 RTPRINT(rtlpriv, FEEPROM, EFUSE_READ_ALL,
328 "Addr=%d\n", efuse_addr);
329 read_efuse_byte(hw, efuse_addr, rtemp8);
330 if (*rtemp8 != 0xFF && (efuse_addr < efuse_len)) {
331 efuse_utilized++;
332 efuse_addr++;
336 for (i = 0; i < efuse_max_section; i++) {
337 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
338 efuse_tbl[(i * 8) + (j * 2)] =
339 (efuse_word[j][i] & 0xff);
340 efuse_tbl[(i * 8) + ((j * 2) + 1)] =
341 ((efuse_word[j][i] >> 8) & 0xff);
345 for (i = 0; i < _size_byte; i++)
346 pbuf[i] = efuse_tbl[_offset + i];
348 rtlefuse->efuse_usedbytes = efuse_utilized;
349 efuse_usage = (u8)((efuse_utilized * 100) / efuse_len);
350 rtlefuse->efuse_usedpercentage = efuse_usage;
351 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_BYTES,
352 (u8 *)&efuse_utilized);
353 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_EFUSE_USAGE,
354 &efuse_usage);
355 done:
356 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++)
357 kfree(efuse_word[i]);
358 kfree(efuse_word);
359 out:
360 kfree(efuse_tbl);
363 bool efuse_shadow_update_chk(struct ieee80211_hw *hw)
365 struct rtl_priv *rtlpriv = rtl_priv(hw);
366 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
367 u8 section_idx, i, base;
368 u16 words_need = 0, hdr_num = 0, totalbytes, efuse_used;
369 bool wordchanged, result = true;
371 for (section_idx = 0; section_idx < 16; section_idx++) {
372 base = section_idx * 8;
373 wordchanged = false;
375 for (i = 0; i < 8; i = i + 2) {
376 if ((rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
377 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) ||
378 (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i + 1] !=
379 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i +
380 1])) {
381 words_need++;
382 wordchanged = true;
386 if (wordchanged)
387 hdr_num++;
390 totalbytes = hdr_num + words_need * 2;
391 efuse_used = rtlefuse->efuse_usedbytes;
393 if ((totalbytes + efuse_used) >=
394 (EFUSE_MAX_SIZE - rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))
395 result = false;
397 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
398 "%s(): totalbytes(%#x), hdr_num(%#x), words_need(%#x), efuse_used(%d)\n",
399 __func__, totalbytes, hdr_num, words_need, efuse_used);
401 return result;
404 void efuse_shadow_read(struct ieee80211_hw *hw, u8 type,
405 u16 offset, u32 *value)
407 if (type == 1)
408 efuse_shadow_read_1byte(hw, offset, (u8 *)value);
409 else if (type == 2)
410 efuse_shadow_read_2byte(hw, offset, (u16 *)value);
411 else if (type == 4)
412 efuse_shadow_read_4byte(hw, offset, value);
415 void efuse_shadow_write(struct ieee80211_hw *hw, u8 type, u16 offset,
416 u32 value)
418 if (type == 1)
419 efuse_shadow_write_1byte(hw, offset, (u8)value);
420 else if (type == 2)
421 efuse_shadow_write_2byte(hw, offset, (u16)value);
422 else if (type == 4)
423 efuse_shadow_write_4byte(hw, offset, value);
426 bool efuse_shadow_update(struct ieee80211_hw *hw)
428 struct rtl_priv *rtlpriv = rtl_priv(hw);
429 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
430 u16 i, offset, base;
431 u8 word_en = 0x0F;
432 u8 first_pg = false;
434 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
436 if (!efuse_shadow_update_chk(hw)) {
437 efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
438 memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
439 &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
440 rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
442 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
443 "efuse out of capacity!!\n");
444 return false;
446 efuse_power_switch(hw, true, true);
448 for (offset = 0; offset < 16; offset++) {
449 word_en = 0x0F;
450 base = offset * 8;
452 for (i = 0; i < 8; i++) {
453 if (first_pg) {
454 word_en &= ~(BIT(i / 2));
456 rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
457 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
458 } else {
459 if (rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] !=
460 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i]) {
461 word_en &= ~(BIT(i / 2));
463 rtlefuse->efuse_map[EFUSE_INIT_MAP][base + i] =
464 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base + i];
468 if (word_en != 0x0F) {
469 u8 tmpdata[8];
471 memcpy(tmpdata,
472 &rtlefuse->efuse_map[EFUSE_MODIFY_MAP][base],
474 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_LOUD,
475 "U-efuse\n", tmpdata, 8);
477 if (!efuse_pg_packet_write(hw, (u8)offset, word_en,
478 tmpdata)) {
479 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
480 "PG section(%#x) fail!!\n", offset);
481 break;
486 efuse_power_switch(hw, true, false);
487 efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
489 memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
490 &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
491 rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
493 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD, "\n");
494 return true;
497 void rtl_efuse_shadow_map_update(struct ieee80211_hw *hw)
499 struct rtl_priv *rtlpriv = rtl_priv(hw);
500 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
502 if (rtlefuse->autoload_failflag)
503 memset((&rtlefuse->efuse_map[EFUSE_INIT_MAP][0]),
504 0xFF, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
505 else
506 efuse_read_all_map(hw, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0]);
508 memcpy(&rtlefuse->efuse_map[EFUSE_MODIFY_MAP][0],
509 &rtlefuse->efuse_map[EFUSE_INIT_MAP][0],
510 rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE]);
513 void efuse_force_write_vendor_id(struct ieee80211_hw *hw)
515 u8 tmpdata[8] = { 0xFF, 0xFF, 0xEC, 0x10, 0xFF, 0xFF, 0xFF, 0xFF };
517 efuse_power_switch(hw, true, true);
519 efuse_pg_packet_write(hw, 1, 0xD, tmpdata);
521 efuse_power_switch(hw, true, false);
524 void efuse_re_pg_section(struct ieee80211_hw *hw, u8 section_idx)
528 static void efuse_shadow_read_1byte(struct ieee80211_hw *hw,
529 u16 offset, u8 *value)
531 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
532 *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
535 static void efuse_shadow_read_2byte(struct ieee80211_hw *hw,
536 u16 offset, u16 *value)
538 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
540 *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
541 *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
544 static void efuse_shadow_read_4byte(struct ieee80211_hw *hw,
545 u16 offset, u32 *value)
547 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
549 *value = rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset];
550 *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] << 8;
551 *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] << 16;
552 *value |= rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] << 24;
555 static void efuse_shadow_write_1byte(struct ieee80211_hw *hw,
556 u16 offset, u8 value)
558 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
560 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value;
563 static void efuse_shadow_write_2byte(struct ieee80211_hw *hw,
564 u16 offset, u16 value)
566 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
568 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] = value & 0x00FF;
569 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] = value >> 8;
572 static void efuse_shadow_write_4byte(struct ieee80211_hw *hw,
573 u16 offset, u32 value)
575 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
577 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset] =
578 (u8)(value & 0x000000FF);
579 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 1] =
580 (u8)((value >> 8) & 0x0000FF);
581 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 2] =
582 (u8)((value >> 16) & 0x00FF);
583 rtlefuse->efuse_map[EFUSE_MODIFY_MAP][offset + 3] =
584 (u8)((value >> 24) & 0xFF);
587 int efuse_one_byte_read(struct ieee80211_hw *hw, u16 addr, u8 *data)
589 struct rtl_priv *rtlpriv = rtl_priv(hw);
590 u8 tmpidx = 0;
591 int result;
593 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 1,
594 (u8)(addr & 0xff));
595 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
596 ((u8)((addr >> 8) & 0x03)) |
597 (rtl_read_byte(rtlpriv,
598 rtlpriv->cfg->maps[EFUSE_CTRL] + 2) &
599 0xFC));
601 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0x72);
603 while (!(0x80 & rtl_read_byte(rtlpriv,
604 rtlpriv->cfg->maps[EFUSE_CTRL] + 3)) &&
605 (tmpidx < 100)) {
606 tmpidx++;
609 if (tmpidx < 100) {
610 *data = rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL]);
611 result = true;
612 } else {
613 *data = 0xff;
614 result = false;
616 return result;
619 static int efuse_one_byte_write(struct ieee80211_hw *hw, u16 addr, u8 data)
621 struct rtl_priv *rtlpriv = rtl_priv(hw);
622 u8 tmpidx = 0;
624 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
625 "Addr = %x Data=%x\n", addr, data);
627 rtl_write_byte(rtlpriv,
628 rtlpriv->cfg->maps[EFUSE_CTRL] + 1, (u8)(addr & 0xff));
629 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 2,
630 (rtl_read_byte(rtlpriv,
631 rtlpriv->cfg->maps[EFUSE_CTRL] +
632 2) & 0xFC) | (u8)((addr >> 8) & 0x03));
634 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL], data);
635 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3, 0xF2);
637 while ((0x80 &
638 rtl_read_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CTRL] + 3)) &&
639 (tmpidx < 100)) {
640 tmpidx++;
643 if (tmpidx < 100)
644 return true;
645 return false;
648 static void efuse_read_all_map(struct ieee80211_hw *hw, u8 *efuse)
650 struct rtl_priv *rtlpriv = rtl_priv(hw);
652 efuse_power_switch(hw, false, true);
653 read_efuse(hw, 0, rtlpriv->cfg->maps[EFUSE_HWSET_MAX_SIZE], efuse);
654 efuse_power_switch(hw, false, false);
657 static void efuse_read_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
658 u8 efuse_data, u8 offset, u8 *tmpdata,
659 u8 *readstate)
661 bool dataempty = true;
662 u8 hoffset;
663 u8 tmpidx;
664 u8 hworden;
665 u8 word_cnts;
667 hoffset = (efuse_data >> 4) & 0x0F;
668 hworden = efuse_data & 0x0F;
669 word_cnts = efuse_calculate_word_cnts(hworden);
671 if (hoffset == offset) {
672 for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
673 if (efuse_one_byte_read(hw, *efuse_addr + 1 + tmpidx,
674 &efuse_data)) {
675 tmpdata[tmpidx] = efuse_data;
676 if (efuse_data != 0xff)
677 dataempty = false;
681 if (!dataempty) {
682 *readstate = PG_STATE_DATA;
683 } else {
684 *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
685 *readstate = PG_STATE_HEADER;
688 } else {
689 *efuse_addr = *efuse_addr + (word_cnts * 2) + 1;
690 *readstate = PG_STATE_HEADER;
694 static int efuse_pg_packet_read(struct ieee80211_hw *hw, u8 offset, u8 *data)
696 u8 readstate = PG_STATE_HEADER;
698 bool continual = true;
700 u8 efuse_data, word_cnts = 0;
701 u16 efuse_addr = 0;
702 u8 tmpdata[8];
704 if (!data)
705 return false;
706 if (offset > 15)
707 return false;
709 memset(data, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
710 memset(tmpdata, 0xff, PGPKT_DATA_SIZE * sizeof(u8));
712 while (continual && (efuse_addr < EFUSE_MAX_SIZE)) {
713 if (readstate & PG_STATE_HEADER) {
714 if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
715 (efuse_data != 0xFF))
716 efuse_read_data_case1(hw, &efuse_addr,
717 efuse_data, offset,
718 tmpdata, &readstate);
719 else
720 continual = false;
721 } else if (readstate & PG_STATE_DATA) {
722 efuse_word_enable_data_read(0, tmpdata, data);
723 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
724 readstate = PG_STATE_HEADER;
728 if ((data[0] == 0xff) && (data[1] == 0xff) &&
729 (data[2] == 0xff) && (data[3] == 0xff) &&
730 (data[4] == 0xff) && (data[5] == 0xff) &&
731 (data[6] == 0xff) && (data[7] == 0xff))
732 return false;
733 return true;
736 static void efuse_write_data_case1(struct ieee80211_hw *hw, u16 *efuse_addr,
737 u8 efuse_data, u8 offset,
738 int *continual, u8 *write_state,
739 struct pgpkt_struct *target_pkt,
740 int *repeat_times, int *result, u8 word_en)
742 struct rtl_priv *rtlpriv = rtl_priv(hw);
743 struct pgpkt_struct tmp_pkt;
744 int dataempty = true;
745 u8 originaldata[8 * sizeof(u8)];
746 u8 badworden = 0x0F;
747 u8 match_word_en, tmp_word_en;
748 u8 tmpindex;
749 u8 tmp_header = efuse_data;
750 u8 tmp_word_cnts;
752 tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
753 tmp_pkt.word_en = tmp_header & 0x0F;
754 tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
756 if (tmp_pkt.offset != target_pkt->offset) {
757 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
758 *write_state = PG_STATE_HEADER;
759 } else {
760 for (tmpindex = 0; tmpindex < (tmp_word_cnts * 2); tmpindex++) {
761 if (efuse_one_byte_read(hw,
762 (*efuse_addr + 1 + tmpindex),
763 &efuse_data) &&
764 (efuse_data != 0xFF))
765 dataempty = false;
768 if (!dataempty) {
769 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
770 *write_state = PG_STATE_HEADER;
771 } else {
772 match_word_en = 0x0F;
773 if (!((target_pkt->word_en & BIT(0)) |
774 (tmp_pkt.word_en & BIT(0))))
775 match_word_en &= (~BIT(0));
777 if (!((target_pkt->word_en & BIT(1)) |
778 (tmp_pkt.word_en & BIT(1))))
779 match_word_en &= (~BIT(1));
781 if (!((target_pkt->word_en & BIT(2)) |
782 (tmp_pkt.word_en & BIT(2))))
783 match_word_en &= (~BIT(2));
785 if (!((target_pkt->word_en & BIT(3)) |
786 (tmp_pkt.word_en & BIT(3))))
787 match_word_en &= (~BIT(3));
789 if ((match_word_en & 0x0F) != 0x0F) {
790 badworden =
791 enable_efuse_data_write(hw,
792 *efuse_addr + 1,
793 tmp_pkt.word_en,
794 target_pkt->data);
796 if (0x0F != (badworden & 0x0F)) {
797 u8 reorg_offset = offset;
798 u8 reorg_worden = badworden;
800 efuse_pg_packet_write(hw, reorg_offset,
801 reorg_worden,
802 originaldata);
805 tmp_word_en = 0x0F;
806 if ((target_pkt->word_en & BIT(0)) ^
807 (match_word_en & BIT(0)))
808 tmp_word_en &= (~BIT(0));
810 if ((target_pkt->word_en & BIT(1)) ^
811 (match_word_en & BIT(1)))
812 tmp_word_en &= (~BIT(1));
814 if ((target_pkt->word_en & BIT(2)) ^
815 (match_word_en & BIT(2)))
816 tmp_word_en &= (~BIT(2));
818 if ((target_pkt->word_en & BIT(3)) ^
819 (match_word_en & BIT(3)))
820 tmp_word_en &= (~BIT(3));
822 if ((tmp_word_en & 0x0F) != 0x0F) {
823 *efuse_addr =
824 efuse_get_current_size(hw);
825 target_pkt->offset = offset;
826 target_pkt->word_en = tmp_word_en;
827 } else {
828 *continual = false;
830 *write_state = PG_STATE_HEADER;
831 *repeat_times += 1;
832 if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
833 *continual = false;
834 *result = false;
836 } else {
837 *efuse_addr += (2 * tmp_word_cnts) + 1;
838 target_pkt->offset = offset;
839 target_pkt->word_en = word_en;
840 *write_state = PG_STATE_HEADER;
844 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse PG_STATE_HEADER-1\n");
847 static void efuse_write_data_case2(struct ieee80211_hw *hw, u16 *efuse_addr,
848 int *continual, u8 *write_state,
849 struct pgpkt_struct target_pkt,
850 int *repeat_times, int *result)
852 struct rtl_priv *rtlpriv = rtl_priv(hw);
853 struct pgpkt_struct tmp_pkt;
854 u8 pg_header;
855 u8 tmp_header;
856 u8 originaldata[8 * sizeof(u8)];
857 u8 tmp_word_cnts;
858 u8 badworden = 0x0F;
860 pg_header = ((target_pkt.offset << 4) & 0xf0) | target_pkt.word_en;
861 efuse_one_byte_write(hw, *efuse_addr, pg_header);
862 efuse_one_byte_read(hw, *efuse_addr, &tmp_header);
864 if (tmp_header == pg_header) {
865 *write_state = PG_STATE_DATA;
866 } else if (tmp_header == 0xFF) {
867 *write_state = PG_STATE_HEADER;
868 *repeat_times += 1;
869 if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
870 *continual = false;
871 *result = false;
873 } else {
874 tmp_pkt.offset = (tmp_header >> 4) & 0x0F;
875 tmp_pkt.word_en = tmp_header & 0x0F;
877 tmp_word_cnts = efuse_calculate_word_cnts(tmp_pkt.word_en);
879 memset(originaldata, 0xff, 8 * sizeof(u8));
881 if (efuse_pg_packet_read(hw, tmp_pkt.offset, originaldata)) {
882 badworden = enable_efuse_data_write(hw,
883 *efuse_addr + 1,
884 tmp_pkt.word_en,
885 originaldata);
887 if (0x0F != (badworden & 0x0F)) {
888 u8 reorg_offset = tmp_pkt.offset;
889 u8 reorg_worden = badworden;
891 efuse_pg_packet_write(hw, reorg_offset,
892 reorg_worden,
893 originaldata);
894 *efuse_addr = efuse_get_current_size(hw);
895 } else {
896 *efuse_addr = *efuse_addr +
897 (tmp_word_cnts * 2) + 1;
899 } else {
900 *efuse_addr = *efuse_addr + (tmp_word_cnts * 2) + 1;
903 *write_state = PG_STATE_HEADER;
904 *repeat_times += 1;
905 if (*repeat_times > EFUSE_REPEAT_THRESHOLD_) {
906 *continual = false;
907 *result = false;
910 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
911 "efuse PG_STATE_HEADER-2\n");
915 static int efuse_pg_packet_write(struct ieee80211_hw *hw,
916 u8 offset, u8 word_en, u8 *data)
918 struct rtl_priv *rtlpriv = rtl_priv(hw);
919 struct pgpkt_struct target_pkt;
920 u8 write_state = PG_STATE_HEADER;
921 int continual = true, dataempty = true, result = true;
922 u16 efuse_addr = 0;
923 u8 efuse_data;
924 u8 target_word_cnts = 0;
925 u8 badworden = 0x0F;
926 static int repeat_times;
928 if (efuse_get_current_size(hw) >= (EFUSE_MAX_SIZE -
929 rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
930 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
931 "%s error\n", __func__);
932 return false;
935 target_pkt.offset = offset;
936 target_pkt.word_en = word_en;
938 memset(target_pkt.data, 0xFF, 8 * sizeof(u8));
940 efuse_word_enable_data_read(word_en, data, target_pkt.data);
941 target_word_cnts = efuse_calculate_word_cnts(target_pkt.word_en);
943 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG, "efuse Power ON\n");
945 while (continual && (efuse_addr < (EFUSE_MAX_SIZE -
946 rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN]))) {
947 if (write_state == PG_STATE_HEADER) {
948 dataempty = true;
949 badworden = 0x0F;
950 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
951 "efuse PG_STATE_HEADER\n");
953 if (efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
954 (efuse_data != 0xFF))
955 efuse_write_data_case1(hw, &efuse_addr,
956 efuse_data, offset,
957 &continual,
958 &write_state,
959 &target_pkt,
960 &repeat_times, &result,
961 word_en);
962 else
963 efuse_write_data_case2(hw, &efuse_addr,
964 &continual,
965 &write_state,
966 target_pkt,
967 &repeat_times,
968 &result);
970 } else if (write_state == PG_STATE_DATA) {
971 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
972 "efuse PG_STATE_DATA\n");
973 badworden = 0x0f;
974 badworden =
975 enable_efuse_data_write(hw, efuse_addr + 1,
976 target_pkt.word_en,
977 target_pkt.data);
979 if ((badworden & 0x0F) == 0x0F) {
980 continual = false;
981 } else {
982 efuse_addr =
983 efuse_addr + (2 * target_word_cnts) + 1;
985 target_pkt.offset = offset;
986 target_pkt.word_en = badworden;
987 target_word_cnts =
988 efuse_calculate_word_cnts(target_pkt.word_en);
989 write_state = PG_STATE_HEADER;
990 repeat_times++;
991 if (repeat_times > EFUSE_REPEAT_THRESHOLD_) {
992 continual = false;
993 result = false;
995 RTPRINT(rtlpriv, FEEPROM, EFUSE_PG,
996 "efuse PG_STATE_HEADER-3\n");
1001 if (efuse_addr >= (EFUSE_MAX_SIZE -
1002 rtlpriv->cfg->maps[EFUSE_OOB_PROTECT_BYTES_LEN])) {
1003 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1004 "efuse_addr(%#x) Out of size!!\n", efuse_addr);
1007 return true;
1010 static void efuse_word_enable_data_read(u8 word_en, u8 *sourdata,
1011 u8 *targetdata)
1013 if (!(word_en & BIT(0))) {
1014 targetdata[0] = sourdata[0];
1015 targetdata[1] = sourdata[1];
1018 if (!(word_en & BIT(1))) {
1019 targetdata[2] = sourdata[2];
1020 targetdata[3] = sourdata[3];
1023 if (!(word_en & BIT(2))) {
1024 targetdata[4] = sourdata[4];
1025 targetdata[5] = sourdata[5];
1028 if (!(word_en & BIT(3))) {
1029 targetdata[6] = sourdata[6];
1030 targetdata[7] = sourdata[7];
1034 static u8 enable_efuse_data_write(struct ieee80211_hw *hw,
1035 u16 efuse_addr, u8 word_en, u8 *data)
1037 struct rtl_priv *rtlpriv = rtl_priv(hw);
1038 u16 tmpaddr;
1039 u16 start_addr = efuse_addr;
1040 u8 badworden = 0x0F;
1041 u8 tmpdata[8];
1043 memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
1044 RT_TRACE(rtlpriv, COMP_EFUSE, DBG_LOUD,
1045 "word_en = %x efuse_addr=%x\n", word_en, efuse_addr);
1047 if (!(word_en & BIT(0))) {
1048 tmpaddr = start_addr;
1049 efuse_one_byte_write(hw, start_addr++, data[0]);
1050 efuse_one_byte_write(hw, start_addr++, data[1]);
1052 efuse_one_byte_read(hw, tmpaddr, &tmpdata[0]);
1053 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[1]);
1054 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
1055 badworden &= (~BIT(0));
1058 if (!(word_en & BIT(1))) {
1059 tmpaddr = start_addr;
1060 efuse_one_byte_write(hw, start_addr++, data[2]);
1061 efuse_one_byte_write(hw, start_addr++, data[3]);
1063 efuse_one_byte_read(hw, tmpaddr, &tmpdata[2]);
1064 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[3]);
1065 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
1066 badworden &= (~BIT(1));
1069 if (!(word_en & BIT(2))) {
1070 tmpaddr = start_addr;
1071 efuse_one_byte_write(hw, start_addr++, data[4]);
1072 efuse_one_byte_write(hw, start_addr++, data[5]);
1074 efuse_one_byte_read(hw, tmpaddr, &tmpdata[4]);
1075 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[5]);
1076 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
1077 badworden &= (~BIT(2));
1080 if (!(word_en & BIT(3))) {
1081 tmpaddr = start_addr;
1082 efuse_one_byte_write(hw, start_addr++, data[6]);
1083 efuse_one_byte_write(hw, start_addr++, data[7]);
1085 efuse_one_byte_read(hw, tmpaddr, &tmpdata[6]);
1086 efuse_one_byte_read(hw, tmpaddr + 1, &tmpdata[7]);
1087 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
1088 badworden &= (~BIT(3));
1091 return badworden;
1094 void efuse_power_switch(struct ieee80211_hw *hw, u8 write, u8 pwrstate)
1096 struct rtl_priv *rtlpriv = rtl_priv(hw);
1097 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1098 u8 tempval;
1099 u16 tmpv16;
1101 if (pwrstate && (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE)) {
1102 if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
1103 rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE) {
1104 rtl_write_byte(rtlpriv,
1105 rtlpriv->cfg->maps[EFUSE_ACCESS], 0x69);
1106 } else {
1107 tmpv16 =
1108 rtl_read_word(rtlpriv,
1109 rtlpriv->cfg->maps[SYS_ISO_CTRL]);
1110 if (!(tmpv16 & rtlpriv->cfg->maps[EFUSE_PWC_EV12V])) {
1111 tmpv16 |= rtlpriv->cfg->maps[EFUSE_PWC_EV12V];
1112 rtl_write_word(rtlpriv,
1113 rtlpriv->cfg->maps[SYS_ISO_CTRL],
1114 tmpv16);
1117 tmpv16 = rtl_read_word(rtlpriv,
1118 rtlpriv->cfg->maps[SYS_FUNC_EN]);
1119 if (!(tmpv16 & rtlpriv->cfg->maps[EFUSE_FEN_ELDR])) {
1120 tmpv16 |= rtlpriv->cfg->maps[EFUSE_FEN_ELDR];
1121 rtl_write_word(rtlpriv,
1122 rtlpriv->cfg->maps[SYS_FUNC_EN], tmpv16);
1125 tmpv16 = rtl_read_word(rtlpriv, rtlpriv->cfg->maps[SYS_CLK]);
1126 if ((!(tmpv16 & rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN])) ||
1127 (!(tmpv16 & rtlpriv->cfg->maps[EFUSE_ANA8M]))) {
1128 tmpv16 |= (rtlpriv->cfg->maps[EFUSE_LOADER_CLK_EN] |
1129 rtlpriv->cfg->maps[EFUSE_ANA8M]);
1130 rtl_write_word(rtlpriv,
1131 rtlpriv->cfg->maps[SYS_CLK], tmpv16);
1135 if (pwrstate) {
1136 if (write) {
1137 tempval = rtl_read_byte(rtlpriv,
1138 rtlpriv->cfg->maps[EFUSE_TEST] +
1141 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1142 tempval &= ~(BIT(3) | BIT(4) | BIT(5) | BIT(6));
1143 tempval |= (VOLTAGE_V25 << 3);
1144 } else if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192SE) {
1145 tempval &= 0x0F;
1146 tempval |= (VOLTAGE_V25 << 4);
1149 rtl_write_byte(rtlpriv,
1150 rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1151 (tempval | 0x80));
1154 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
1155 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1156 0x03);
1158 } else {
1159 if (rtlhal->hw_type != HARDWARE_TYPE_RTL8192CE &&
1160 rtlhal->hw_type != HARDWARE_TYPE_RTL8192DE)
1161 rtl_write_byte(rtlpriv,
1162 rtlpriv->cfg->maps[EFUSE_ACCESS], 0);
1164 if (write) {
1165 tempval = rtl_read_byte(rtlpriv,
1166 rtlpriv->cfg->maps[EFUSE_TEST] +
1168 rtl_write_byte(rtlpriv,
1169 rtlpriv->cfg->maps[EFUSE_TEST] + 3,
1170 (tempval & 0x7F));
1173 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192SE) {
1174 rtl_write_byte(rtlpriv, rtlpriv->cfg->maps[EFUSE_CLK],
1175 0x02);
1180 static u16 efuse_get_current_size(struct ieee80211_hw *hw)
1182 int continual = true;
1183 u16 efuse_addr = 0;
1184 u8 hoffset, hworden;
1185 u8 efuse_data, word_cnts;
1187 while (continual && efuse_one_byte_read(hw, efuse_addr, &efuse_data) &&
1188 (efuse_addr < EFUSE_MAX_SIZE)) {
1189 if (efuse_data != 0xFF) {
1190 hoffset = (efuse_data >> 4) & 0x0F;
1191 hworden = efuse_data & 0x0F;
1192 word_cnts = efuse_calculate_word_cnts(hworden);
1193 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
1194 } else {
1195 continual = false;
1199 return efuse_addr;
1202 static u8 efuse_calculate_word_cnts(u8 word_en)
1204 u8 word_cnts = 0;
1206 if (!(word_en & BIT(0)))
1207 word_cnts++;
1208 if (!(word_en & BIT(1)))
1209 word_cnts++;
1210 if (!(word_en & BIT(2)))
1211 word_cnts++;
1212 if (!(word_en & BIT(3)))
1213 word_cnts++;
1214 return word_cnts;
1217 int rtl_get_hwinfo(struct ieee80211_hw *hw, struct rtl_priv *rtlpriv,
1218 int max_size, u8 *hwinfo, int *params)
1220 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1221 struct rtl_pci_priv *rtlpcipriv = rtl_pcipriv(hw);
1222 struct device *dev = &rtlpcipriv->dev.pdev->dev;
1223 u16 eeprom_id;
1224 u16 i, usvalue;
1226 switch (rtlefuse->epromtype) {
1227 case EEPROM_BOOT_EFUSE:
1228 rtl_efuse_shadow_map_update(hw);
1229 break;
1231 case EEPROM_93C46:
1232 pr_err("RTL8XXX did not boot from eeprom, check it !!\n");
1233 return 1;
1235 default:
1236 dev_warn(dev, "no efuse data\n");
1237 return 1;
1240 memcpy(hwinfo, &rtlefuse->efuse_map[EFUSE_INIT_MAP][0], max_size);
1242 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "MAP",
1243 hwinfo, max_size);
1245 eeprom_id = *((u16 *)&hwinfo[0]);
1246 if (eeprom_id != params[0]) {
1247 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1248 "EEPROM ID(%#x) is invalid!!\n", eeprom_id);
1249 rtlefuse->autoload_failflag = true;
1250 } else {
1251 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Autoload OK\n");
1252 rtlefuse->autoload_failflag = false;
1255 if (rtlefuse->autoload_failflag)
1256 return 1;
1258 rtlefuse->eeprom_vid = *(u16 *)&hwinfo[params[1]];
1259 rtlefuse->eeprom_did = *(u16 *)&hwinfo[params[2]];
1260 rtlefuse->eeprom_svid = *(u16 *)&hwinfo[params[3]];
1261 rtlefuse->eeprom_smid = *(u16 *)&hwinfo[params[4]];
1262 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1263 "EEPROMId = 0x%4x\n", eeprom_id);
1264 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1265 "EEPROM VID = 0x%4x\n", rtlefuse->eeprom_vid);
1266 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1267 "EEPROM DID = 0x%4x\n", rtlefuse->eeprom_did);
1268 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1269 "EEPROM SVID = 0x%4x\n", rtlefuse->eeprom_svid);
1270 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1271 "EEPROM SMID = 0x%4x\n", rtlefuse->eeprom_smid);
1273 for (i = 0; i < 6; i += 2) {
1274 usvalue = *(u16 *)&hwinfo[params[5] + i];
1275 *((u16 *)(&rtlefuse->dev_addr[i])) = usvalue;
1277 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "%pM\n", rtlefuse->dev_addr);
1279 rtlefuse->eeprom_channelplan = *&hwinfo[params[6]];
1280 rtlefuse->eeprom_version = *(u16 *)&hwinfo[params[7]];
1281 rtlefuse->txpwr_fromeprom = true;
1282 rtlefuse->eeprom_oemid = *&hwinfo[params[8]];
1284 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1285 "EEPROM Customer ID: 0x%2x\n", rtlefuse->eeprom_oemid);
1287 /* set channel plan to world wide 13 */
1288 rtlefuse->channel_plan = params[9];
1290 return 0;
1293 void rtl_fw_block_write(struct ieee80211_hw *hw, const u8 *buffer, u32 size)
1295 struct rtl_priv *rtlpriv = rtl_priv(hw);
1296 u8 *pu4byteptr = (u8 *)buffer;
1297 u32 i;
1299 for (i = 0; i < size; i++)
1300 rtl_write_byte(rtlpriv, (START_ADDRESS + i), *(pu4byteptr + i));
1303 void rtl_fw_page_write(struct ieee80211_hw *hw, u32 page, const u8 *buffer,
1304 u32 size)
1306 struct rtl_priv *rtlpriv = rtl_priv(hw);
1307 u8 value8;
1308 u8 u8page = (u8)(page & 0x07);
1310 value8 = (rtl_read_byte(rtlpriv, REG_MCUFWDL + 2) & 0xF8) | u8page;
1312 rtl_write_byte(rtlpriv, (REG_MCUFWDL + 2), value8);
1313 rtl_fw_block_write(hw, buffer, size);
1316 void rtl_fill_dummy(u8 *pfwbuf, u32 *pfwlen)
1318 u32 fwlen = *pfwlen;
1319 u8 remain = (u8)(fwlen % 4);
1321 remain = (remain == 0) ? 0 : (4 - remain);
1323 while (remain > 0) {
1324 pfwbuf[fwlen] = 0;
1325 fwlen++;
1326 remain--;
1329 *pfwlen = fwlen;