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