1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 *
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 *
6 ******************************************************************************/
7 #define _RTW_EFUSE_C_
8
9 #include <osdep_service.h>
10 #include <drv_types.h>
11 #include <rtw_efuse.h>
12 #include <usb_ops_linux.h>
13 #include <rtl8188e_hal.h>
14 #include <rtw_iol.h>
15
16 #define REG_EFUSE_CTRL 0x0030
17 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
18
19 enum{
20 VOLTAGE_V25 = 0x03,
21 LDOE25_SHIFT = 28,
22 };
23
24 /*
25 * When we want to enable write operation, we should change to pwr on state.
26 * When we stop write, we should switch to 500k mode and disable LDO 2.5V.
27 */
efuse_power_switch(struct adapter * pAdapter,u8 write,u8 pwrstate)28 static void efuse_power_switch(struct adapter *pAdapter, u8 write, u8 pwrstate)
29 {
30 u8 tempval;
31 u16 tmpv16;
32
33 if (pwrstate) {
34 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
35
36 /* 1.2V Power: From VDDON with Power Cut(0x0000h[15]), default valid */
37 tmpv16 = usb_read16(pAdapter, REG_SYS_ISO_CTRL);
38 if (!(tmpv16 & PWC_EV12V)) {
39 tmpv16 |= PWC_EV12V;
40 usb_write16(pAdapter, REG_SYS_ISO_CTRL, tmpv16);
41 }
42 /* Reset: 0x0000h[28], default valid */
43 tmpv16 = usb_read16(pAdapter, REG_SYS_FUNC_EN);
44 if (!(tmpv16 & FEN_ELDR)) {
45 tmpv16 |= FEN_ELDR;
46 usb_write16(pAdapter, REG_SYS_FUNC_EN, tmpv16);
47 }
48
49 /* Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock from ANA, default valid */
50 tmpv16 = usb_read16(pAdapter, REG_SYS_CLKR);
51 if ((!(tmpv16 & LOADER_CLK_EN)) || (!(tmpv16 & ANA8M))) {
52 tmpv16 |= (LOADER_CLK_EN | ANA8M);
53 usb_write16(pAdapter, REG_SYS_CLKR, tmpv16);
54 }
55
56 if (write) {
57 /* Enable LDO 2.5V before read/write action */
58 tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
59 tempval &= 0x0F;
60 tempval |= (VOLTAGE_V25 << 4);
61 usb_write8(pAdapter, EFUSE_TEST + 3, (tempval | 0x80));
62 }
63 } else {
64 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
65
66 if (write) {
67 /* Disable LDO 2.5V after read/write action */
68 tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
69 usb_write8(pAdapter, EFUSE_TEST + 3, (tempval & 0x7F));
70 }
71 }
72 }
73
74 static void
efuse_phymap_to_logical(u8 * phymap,u16 _offset,u16 _size_byte,u8 * pbuf)75 efuse_phymap_to_logical(u8 *phymap, u16 _offset, u16 _size_byte, u8 *pbuf)
76 {
77 u8 *efuseTbl = NULL;
78 u8 rtemp8;
79 u16 eFuse_Addr = 0;
80 u8 offset, wren;
81 u16 i, j;
82 u16 **eFuseWord = NULL;
83 u16 efuse_utilized = 0;
84 u8 u1temp = 0;
85 void **tmp = NULL;
86
87 efuseTbl = kzalloc(EFUSE_MAP_LEN_88E, GFP_KERNEL);
88 if (!efuseTbl)
89 return;
90
91 tmp = kcalloc(EFUSE_MAX_SECTION_88E,
92 sizeof(void *) + EFUSE_MAX_WORD_UNIT * sizeof(u16),
93 GFP_KERNEL);
94 if (!tmp) {
95 DBG_88E("%s: alloc eFuseWord fail!\n", __func__);
96 goto eFuseWord_failed;
97 }
98 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
99 tmp[i] = ((char *)(tmp + EFUSE_MAX_SECTION_88E)) + i * EFUSE_MAX_WORD_UNIT * sizeof(u16);
100 eFuseWord = (u16 **)tmp;
101
102 /* 0. Refresh efuse init map as all oxFF. */
103 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
104 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
105 eFuseWord[i][j] = 0xFFFF;
106
107 /* */
108 /* 1. Read the first byte to check if efuse is empty!!! */
109 /* */
110 /* */
111 rtemp8 = *(phymap + eFuse_Addr);
112 if (rtemp8 != 0xFF) {
113 efuse_utilized++;
114 eFuse_Addr++;
115 } else {
116 DBG_88E("EFUSE is empty efuse_Addr-%d efuse_data =%x\n", eFuse_Addr, rtemp8);
117 goto exit;
118 }
119
120 /* */
121 /* 2. Read real efuse content. Filter PG header and every section data. */
122 /* */
123 while ((rtemp8 != 0xFF) && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
124 /* Check PG header for section num. */
125 if ((rtemp8 & 0x1F) == 0x0F) { /* extended header */
126 u1temp = (rtemp8 & 0xE0) >> 5;
127 rtemp8 = *(phymap + eFuse_Addr);
128 if ((rtemp8 & 0x0F) == 0x0F) {
129 eFuse_Addr++;
130 rtemp8 = *(phymap + eFuse_Addr);
131
132 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E))
133 eFuse_Addr++;
134 continue;
135 } else {
136 offset = ((rtemp8 & 0xF0) >> 1) | u1temp;
137 wren = rtemp8 & 0x0F;
138 eFuse_Addr++;
139 }
140 } else {
141 offset = (rtemp8 >> 4) & 0x0f;
142 wren = rtemp8 & 0x0f;
143 }
144
145 if (offset < EFUSE_MAX_SECTION_88E) {
146 /* Get word enable value from PG header */
147 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
148 /* Check word enable condition in the section */
149 if (!(wren & 0x01)) {
150 rtemp8 = *(phymap + eFuse_Addr);
151 eFuse_Addr++;
152 efuse_utilized++;
153 eFuseWord[offset][i] = (rtemp8 & 0xff);
154 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
155 break;
156 rtemp8 = *(phymap + eFuse_Addr);
157 eFuse_Addr++;
158 efuse_utilized++;
159 eFuseWord[offset][i] |= (((u16)rtemp8 << 8) & 0xff00);
160
161 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
162 break;
163 }
164 wren >>= 1;
165 }
166 }
167 /* Read next PG header */
168 rtemp8 = *(phymap + eFuse_Addr);
169
170 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
171 efuse_utilized++;
172 eFuse_Addr++;
173 }
174 }
175
176 /* */
177 /* 3. Collect 16 sections and 4 word unit into Efuse map. */
178 /* */
179 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++) {
180 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
181 efuseTbl[(i * 8) + (j * 2)] = (eFuseWord[i][j] & 0xff);
182 efuseTbl[(i * 8) + ((j * 2) + 1)] = ((eFuseWord[i][j] >> 8) & 0xff);
183 }
184 }
185
186 /* */
187 /* 4. Copy from Efuse map to output pointer memory!!! */
188 /* */
189 for (i = 0; i < _size_byte; i++)
190 pbuf[i] = efuseTbl[_offset + i];
191
192 /* */
193 /* 5. Calculate Efuse utilization. */
194 /* */
195
196 exit:
197 kfree(eFuseWord);
198
199 eFuseWord_failed:
200 kfree(efuseTbl);
201 }
202
efuse_read_phymap_from_txpktbuf(struct adapter * adapter,int bcnhead,u8 * content,u16 * size)203 static void efuse_read_phymap_from_txpktbuf(
204 struct adapter *adapter,
205 int bcnhead, /* beacon head, where FW store len(2-byte) and efuse physical map. */
206 u8 *content, /* buffer to store efuse physical map */
207 u16 *size /* for efuse content: the max byte to read. will update to byte read */
208 )
209 {
210 u16 dbg_addr = 0;
211 unsigned long start = 0;
212 u8 reg_0x143 = 0;
213 u32 lo32 = 0, hi32 = 0;
214 u16 len = 0, count = 0;
215 int i = 0;
216 u16 limit = *size;
217
218 u8 *pos = content;
219
220 if (bcnhead < 0) /* if not valid */
221 bcnhead = usb_read8(adapter, REG_TDECTRL + 1);
222
223 DBG_88E("%s bcnhead:%d\n", __func__, bcnhead);
224
225 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
226
227 dbg_addr = bcnhead * 128 / 8; /* 8-bytes addressing */
228
229 while (1) {
230 usb_write16(adapter, REG_PKTBUF_DBG_ADDR, dbg_addr + i);
231
232 usb_write8(adapter, REG_TXPKTBUF_DBG, 0);
233 start = jiffies;
234 while (!(reg_0x143 = usb_read8(adapter, REG_TXPKTBUF_DBG)) &&
235 jiffies_to_msecs(jiffies - start) < 1000) {
236 DBG_88E("%s polling reg_0x143:0x%02x, reg_0x106:0x%02x\n", __func__, reg_0x143, usb_read8(adapter, 0x106));
237 usleep_range(1000, 2000);
238 }
239
240 lo32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_L);
241 hi32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_H);
242
243 if (i == 0) {
244 u8 lenc[2];
245 u16 lenbak, aaabak;
246 u16 aaa;
247
248 lenc[0] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L);
249 lenc[1] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L + 1);
250
251 aaabak = le16_to_cpup((__le16 *)lenc);
252 lenbak = le16_to_cpu(*((__le16 *)lenc));
253 aaa = le16_to_cpup((__le16 *)&lo32);
254 len = le16_to_cpu(*((__le16 *)&lo32));
255
256 limit = min_t(u16, len - 2, limit);
257
258 DBG_88E("%s len:%u, lenbak:%u, aaa:%u, aaabak:%u\n", __func__, len, lenbak, aaa, aaabak);
259
260 memcpy(pos, ((u8 *)&lo32) + 2, (limit >= count + 2) ? 2 : limit - count);
261 count += (limit >= count + 2) ? 2 : limit - count;
262 pos = content + count;
263
264 } else {
265 memcpy(pos, ((u8 *)&lo32), (limit >= count + 4) ? 4 : limit - count);
266 count += (limit >= count + 4) ? 4 : limit - count;
267 pos = content + count;
268 }
269
270 if (limit > count && len - 2 > count) {
271 memcpy(pos, (u8 *)&hi32, (limit >= count + 4) ? 4 : limit - count);
272 count += (limit >= count + 4) ? 4 : limit - count;
273 pos = content + count;
274 }
275
276 if (limit <= count || len - 2 <= count)
277 break;
278 i++;
279 }
280 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS);
281 DBG_88E("%s read count:%u\n", __func__, count);
282 *size = count;
283 }
284
iol_read_efuse(struct adapter * padapter,u8 txpktbuf_bndy,u16 offset,u16 size_byte,u8 * logical_map)285 static s32 iol_read_efuse(struct adapter *padapter, u8 txpktbuf_bndy, u16 offset, u16 size_byte, u8 *logical_map)
286 {
287 s32 status = _FAIL;
288 u8 physical_map[512];
289 u16 size = 512;
290
291 usb_write8(padapter, REG_TDECTRL + 1, txpktbuf_bndy);
292 memset(physical_map, 0xFF, 512);
293 usb_write8(padapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
294 status = iol_execute(padapter, CMD_READ_EFUSE_MAP);
295 if (status == _SUCCESS)
296 efuse_read_phymap_from_txpktbuf(padapter, txpktbuf_bndy, physical_map, &size);
297 efuse_phymap_to_logical(physical_map, offset, size_byte, logical_map);
298 return status;
299 }
300
efuse_ReadEFuse(struct adapter * Adapter,u8 efuseType,u16 _offset,u16 _size_byte,u8 * pbuf)301 void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf)
302 {
303 if (rtw_iol_applied(Adapter)) {
304 rtw_hal_power_on(Adapter);
305 iol_mode_enable(Adapter, 1);
306 iol_read_efuse(Adapter, 0, _offset, _size_byte, pbuf);
307 iol_mode_enable(Adapter, 0);
308 }
309 }
310
Efuse_WordEnableDataWrite(struct adapter * pAdapter,u16 efuse_addr,u8 word_en,u8 * data)311 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data)
312 {
313 u16 tmpaddr = 0;
314 u16 start_addr = efuse_addr;
315 u8 badworden = 0x0F;
316 u8 tmpdata[8];
317
318 memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
319
320 if (!(word_en & BIT(0))) {
321 tmpaddr = start_addr;
322 efuse_OneByteWrite(pAdapter, start_addr++, data[0]);
323 efuse_OneByteWrite(pAdapter, start_addr++, data[1]);
324
325 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[0]);
326 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[1]);
327 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
328 badworden &= (~BIT(0));
329 }
330 if (!(word_en & BIT(1))) {
331 tmpaddr = start_addr;
332 efuse_OneByteWrite(pAdapter, start_addr++, data[2]);
333 efuse_OneByteWrite(pAdapter, start_addr++, data[3]);
334
335 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[2]);
336 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[3]);
337 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
338 badworden &= (~BIT(1));
339 }
340 if (!(word_en & BIT(2))) {
341 tmpaddr = start_addr;
342 efuse_OneByteWrite(pAdapter, start_addr++, data[4]);
343 efuse_OneByteWrite(pAdapter, start_addr++, data[5]);
344
345 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[4]);
346 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[5]);
347 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
348 badworden &= (~BIT(2));
349 }
350 if (!(word_en & BIT(3))) {
351 tmpaddr = start_addr;
352 efuse_OneByteWrite(pAdapter, start_addr++, data[6]);
353 efuse_OneByteWrite(pAdapter, start_addr++, data[7]);
354
355 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[6]);
356 efuse_OneByteRead(pAdapter, tmpaddr + 1, &tmpdata[7]);
357 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
358 badworden &= (~BIT(3));
359 }
360 return badworden;
361 }
362
Efuse_GetCurrentSize(struct adapter * pAdapter)363 static u16 Efuse_GetCurrentSize(struct adapter *pAdapter)
364 {
365 u16 efuse_addr = 0;
366 u8 hoffset = 0, hworden = 0;
367 u8 efuse_data, word_cnts = 0;
368
369 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
370
371 while (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) &&
372 AVAILABLE_EFUSE_ADDR(efuse_addr)) {
373 if (efuse_data == 0xFF)
374 break;
375 if ((efuse_data & 0x1F) == 0x0F) { /* extended header */
376 hoffset = efuse_data;
377 efuse_addr++;
378 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
379 if ((efuse_data & 0x0F) == 0x0F) {
380 efuse_addr++;
381 continue;
382 } else {
383 hoffset = ((hoffset & 0xE0) >> 5) |
384 ((efuse_data & 0xF0) >> 1);
385 hworden = efuse_data & 0x0F;
386 }
387 } else {
388 hoffset = (efuse_data >> 4) & 0x0F;
389 hworden = efuse_data & 0x0F;
390 }
391 word_cnts = Efuse_CalculateWordCnts(hworden);
392 /* read next header */
393 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
394 }
395
396 rtw_hal_set_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
397
398 return efuse_addr;
399 }
400
Efuse_PgPacketRead(struct adapter * pAdapter,u8 offset,u8 * data)401 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data)
402 {
403 u8 ReadState = PG_STATE_HEADER;
404 int bDataEmpty = true;
405 u8 efuse_data, word_cnts = 0;
406 u16 efuse_addr = 0;
407 u8 hoffset = 0, hworden = 0;
408 u8 tmpidx = 0;
409 u8 tmpdata[8];
410 u8 tmp_header = 0;
411
412 if (!data)
413 return false;
414 if (offset > EFUSE_MAX_SECTION_88E)
415 return false;
416
417 memset(data, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
418 memset(tmpdata, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
419
420 /* <Roger_TODO> Efuse has been pre-programmed dummy 5Bytes at the end of Efuse by CP. */
421 /* Skip dummy parts to prevent unexpected data read from Efuse. */
422 /* By pass right now. 2009.02.19. */
423 while (AVAILABLE_EFUSE_ADDR(efuse_addr)) {
424 /* Header Read ------------- */
425 if (ReadState & PG_STATE_HEADER) {
426 if (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) && (efuse_data != 0xFF)) {
427 if (EXT_HEADER(efuse_data)) {
428 tmp_header = efuse_data;
429 efuse_addr++;
430 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
431 if (!ALL_WORDS_DISABLED(efuse_data)) {
432 hoffset = ((tmp_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
433 hworden = efuse_data & 0x0F;
434 } else {
435 DBG_88E("Error, All words disabled\n");
436 efuse_addr++;
437 continue;
438 }
439 } else {
440 hoffset = (efuse_data >> 4) & 0x0F;
441 hworden = efuse_data & 0x0F;
442 }
443 word_cnts = Efuse_CalculateWordCnts(hworden);
444 bDataEmpty = true;
445
446 if (hoffset == offset) {
447 for (tmpidx = 0; tmpidx < word_cnts * 2; tmpidx++) {
448 if (efuse_OneByteRead(pAdapter, efuse_addr + 1 + tmpidx, &efuse_data)) {
449 tmpdata[tmpidx] = efuse_data;
450 if (efuse_data != 0xff)
451 bDataEmpty = false;
452 }
453 }
454 if (!bDataEmpty) {
455 ReadState = PG_STATE_DATA;
456 } else {/* read next header */
457 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
458 ReadState = PG_STATE_HEADER;
459 }
460 } else {/* read next header */
461 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
462 ReadState = PG_STATE_HEADER;
463 }
464 } else {
465 break;
466 }
467 } else if (ReadState & PG_STATE_DATA) {
468 /* Data section Read ------------- */
469 efuse_WordEnableDataRead(hworden, tmpdata, data);
470 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
471 ReadState = PG_STATE_HEADER;
472 }
473 }
474
475 if ((data[0] == 0xff) && (data[1] == 0xff) && (data[2] == 0xff) && (data[3] == 0xff) &&
476 (data[4] == 0xff) && (data[5] == 0xff) && (data[6] == 0xff) && (data[7] == 0xff))
477 return false;
478 else
479 return true;
480 }
481
hal_EfuseFixHeaderProcess(struct adapter * pAdapter,u8 efuseType,struct pgpkt * pFixPkt,u16 * pAddr)482 static bool hal_EfuseFixHeaderProcess(struct adapter *pAdapter, u8 efuseType, struct pgpkt *pFixPkt, u16 *pAddr)
483 {
484 u8 originaldata[8], badworden = 0;
485 u16 efuse_addr = *pAddr;
486 u32 PgWriteSuccess = 0;
487
488 memset(originaldata, 0xff, 8);
489
490 if (Efuse_PgPacketRead(pAdapter, pFixPkt->offset, originaldata)) {
491 /* check if data exist */
492 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr + 1, pFixPkt->word_en, originaldata);
493
494 if (badworden != 0xf) { /* write fail */
495 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pFixPkt->offset, badworden, originaldata);
496
497 if (!PgWriteSuccess)
498 return false;
499
500 efuse_addr = Efuse_GetCurrentSize(pAdapter);
501 } else {
502 efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
503 }
504 } else {
505 efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
506 }
507 *pAddr = efuse_addr;
508 return true;
509 }
510
hal_EfusePgPacketWrite2ByteHeader(struct adapter * pAdapter,u8 efuseType,u16 * pAddr,struct pgpkt * pTargetPkt)511 static bool hal_EfusePgPacketWrite2ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
512 {
513 bool ret = false;
514 u16 efuse_addr = *pAddr;
515 u16 efuse_max_available_len =
516 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
517 u8 pg_header = 0, tmp_header = 0, pg_header_temp = 0;
518 u8 repeatcnt = 0;
519
520 while (efuse_addr < efuse_max_available_len) {
521 pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
522 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
523 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
524
525 while (tmp_header == 0xFF) {
526 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
527 return false;
528
529 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
530 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
531 }
532
533 /* to write ext_header */
534 if (tmp_header == pg_header) {
535 efuse_addr++;
536 pg_header_temp = pg_header;
537 pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
538
539 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
540 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
541
542 while (tmp_header == 0xFF) {
543 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
544 return false;
545
546 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
547 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
548 }
549
550 if ((tmp_header & 0x0F) == 0x0F) { /* word_en PG fail */
551 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
552 return false;
553
554 efuse_addr++;
555 continue;
556 } else if (pg_header != tmp_header) { /* offset PG fail */
557 struct pgpkt fixPkt;
558
559 fixPkt.offset = ((pg_header_temp & 0xE0) >> 5) | ((tmp_header & 0xF0) >> 1);
560 fixPkt.word_en = tmp_header & 0x0F;
561 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
562 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
563 return false;
564 } else {
565 ret = true;
566 break;
567 }
568 } else if ((tmp_header & 0x1F) == 0x0F) { /* wrong extended header */
569 efuse_addr += 2;
570 continue;
571 }
572 }
573
574 *pAddr = efuse_addr;
575 return ret;
576 }
577
hal_EfusePgPacketWrite1ByteHeader(struct adapter * pAdapter,u8 efuseType,u16 * pAddr,struct pgpkt * pTargetPkt)578 static bool hal_EfusePgPacketWrite1ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
579 {
580 bool ret = false;
581 u8 pg_header = 0, tmp_header = 0;
582 u16 efuse_addr = *pAddr;
583 u8 repeatcnt = 0;
584
585 pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
586
587 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
588 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
589
590 while (tmp_header == 0xFF) {
591 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
592 return false;
593 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
594 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
595 }
596
597 if (pg_header == tmp_header) {
598 ret = true;
599 } else {
600 struct pgpkt fixPkt;
601
602 fixPkt.offset = (tmp_header >> 4) & 0x0F;
603 fixPkt.word_en = tmp_header & 0x0F;
604 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
605 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
606 return false;
607 }
608
609 *pAddr = efuse_addr;
610 return ret;
611 }
612
hal_EfusePgPacketWriteData(struct adapter * pAdapter,u8 efuseType,u16 * pAddr,struct pgpkt * pTargetPkt)613 static bool hal_EfusePgPacketWriteData(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
614 {
615 u16 efuse_addr = *pAddr;
616 u8 badworden;
617 u32 PgWriteSuccess = 0;
618
619 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr + 1, pTargetPkt->word_en, pTargetPkt->data);
620 if (badworden == 0x0F) {
621 /* write ok */
622 return true;
623 }
624 /* reorganize other pg packet */
625 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
626 if (!PgWriteSuccess)
627 return false;
628 else
629 return true;
630 }
631
632 static bool
hal_EfusePgPacketWriteHeader(struct adapter * pAdapter,u8 efuseType,u16 * pAddr,struct pgpkt * pTargetPkt)633 hal_EfusePgPacketWriteHeader(
634 struct adapter *pAdapter,
635 u8 efuseType,
636 u16 *pAddr,
637 struct pgpkt *pTargetPkt)
638 {
639 bool ret = false;
640
641 if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
642 ret = hal_EfusePgPacketWrite2ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
643 else
644 ret = hal_EfusePgPacketWrite1ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
645
646 return ret;
647 }
648
wordEnMatched(struct pgpkt * pTargetPkt,struct pgpkt * pCurPkt,u8 * pWden)649 static bool wordEnMatched(struct pgpkt *pTargetPkt, struct pgpkt *pCurPkt,
650 u8 *pWden)
651 {
652 u8 match_word_en = 0x0F; /* default all words are disabled */
653
654 /* check if the same words are enabled both target and current PG packet */
655 if (((pTargetPkt->word_en & BIT(0)) == 0) &&
656 ((pCurPkt->word_en & BIT(0)) == 0))
657 match_word_en &= ~BIT(0); /* enable word 0 */
658 if (((pTargetPkt->word_en & BIT(1)) == 0) &&
659 ((pCurPkt->word_en & BIT(1)) == 0))
660 match_word_en &= ~BIT(1); /* enable word 1 */
661 if (((pTargetPkt->word_en & BIT(2)) == 0) &&
662 ((pCurPkt->word_en & BIT(2)) == 0))
663 match_word_en &= ~BIT(2); /* enable word 2 */
664 if (((pTargetPkt->word_en & BIT(3)) == 0) &&
665 ((pCurPkt->word_en & BIT(3)) == 0))
666 match_word_en &= ~BIT(3); /* enable word 3 */
667
668 *pWden = match_word_en;
669
670 if (match_word_en != 0xf)
671 return true;
672 else
673 return false;
674 }
675
hal_EfuseCheckIfDatafollowed(struct adapter * pAdapter,u8 word_cnts,u16 startAddr)676 static bool hal_EfuseCheckIfDatafollowed(struct adapter *pAdapter, u8 word_cnts, u16 startAddr)
677 {
678 bool ret = false;
679 u8 i, efuse_data;
680
681 for (i = 0; i < (word_cnts * 2); i++) {
682 if (efuse_OneByteRead(pAdapter, (startAddr + i), &efuse_data) && (efuse_data != 0xFF))
683 ret = true;
684 }
685 return ret;
686 }
687
hal_EfusePartialWriteCheck(struct adapter * pAdapter,u8 efuseType,u16 * pAddr,struct pgpkt * pTargetPkt)688 static bool hal_EfusePartialWriteCheck(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
689 {
690 bool ret = false;
691 u8 i, efuse_data = 0, cur_header = 0;
692 u8 matched_wden = 0, badworden = 0;
693 u16 startAddr = 0;
694 u16 efuse_max_available_len =
695 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
696 struct pgpkt curPkt;
697
698 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
699 startAddr %= EFUSE_REAL_CONTENT_LEN;
700
701 while (1) {
702 if (startAddr >= efuse_max_available_len) {
703 ret = false;
704 break;
705 }
706
707 if (efuse_OneByteRead(pAdapter, startAddr, &efuse_data) && (efuse_data != 0xFF)) {
708 if (EXT_HEADER(efuse_data)) {
709 cur_header = efuse_data;
710 startAddr++;
711 efuse_OneByteRead(pAdapter, startAddr, &efuse_data);
712 if (ALL_WORDS_DISABLED(efuse_data)) {
713 ret = false;
714 break;
715 }
716 curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
717 curPkt.word_en = efuse_data & 0x0F;
718 } else {
719 cur_header = efuse_data;
720 curPkt.offset = (cur_header >> 4) & 0x0F;
721 curPkt.word_en = cur_header & 0x0F;
722 }
723
724 curPkt.word_cnts = Efuse_CalculateWordCnts(curPkt.word_en);
725 /* if same header is found but no data followed */
726 /* write some part of data followed by the header. */
727 if ((curPkt.offset == pTargetPkt->offset) &&
728 (!hal_EfuseCheckIfDatafollowed(pAdapter, curPkt.word_cnts, startAddr + 1)) &&
729 wordEnMatched(pTargetPkt, &curPkt, &matched_wden)) {
730 /* Here to write partial data */
731 badworden = Efuse_WordEnableDataWrite(pAdapter, startAddr + 1, matched_wden, pTargetPkt->data);
732 if (badworden != 0x0F) {
733 u32 PgWriteSuccess = 0;
734 /* if write fail on some words, write these bad words again */
735
736 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
737
738 if (!PgWriteSuccess) {
739 ret = false; /* write fail, return */
740 break;
741 }
742 }
743 /* partial write ok, update the target packet for later use */
744 for (i = 0; i < 4; i++) {
745 if ((matched_wden & (0x1 << i)) == 0) /* this word has been written */
746 pTargetPkt->word_en |= (0x1 << i); /* disable the word */
747 }
748 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
749 }
750 /* read from next header */
751 startAddr = startAddr + (curPkt.word_cnts * 2) + 1;
752 } else {
753 /* not used header, 0xff */
754 *pAddr = startAddr;
755 ret = true;
756 break;
757 }
758 }
759 return ret;
760 }
761
hal_EfuseConstructPGPkt(u8 offset,u8 word_en,u8 * pData,struct pgpkt * pTargetPkt)762 static void hal_EfuseConstructPGPkt(u8 offset, u8 word_en, u8 *pData, struct pgpkt *pTargetPkt)
763 {
764 memset((void *)pTargetPkt->data, 0xFF, sizeof(u8) * 8);
765 pTargetPkt->offset = offset;
766 pTargetPkt->word_en = word_en;
767 efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
768 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
769 }
770
Efuse_PgPacketWrite(struct adapter * pAdapter,u8 offset,u8 word_en,u8 * pData)771 bool Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *pData)
772 {
773 struct pgpkt targetPkt;
774 u16 startAddr = 0;
775 u8 efuseType = EFUSE_WIFI;
776
777 if (Efuse_GetCurrentSize(pAdapter) >= EFUSE_MAP_LEN_88E)
778 return false;
779
780 hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
781
782 if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt))
783 return false;
784
785 if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt))
786 return false;
787
788 if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt))
789 return false;
790
791 return true;
792 }
793
Efuse_CalculateWordCnts(u8 word_en)794 u8 Efuse_CalculateWordCnts(u8 word_en)
795 {
796 u8 word_cnts = 0;
797
798 if (!(word_en & BIT(0)))
799 word_cnts++; /* 0 : write enable */
800 if (!(word_en & BIT(1)))
801 word_cnts++;
802 if (!(word_en & BIT(2)))
803 word_cnts++;
804 if (!(word_en & BIT(3)))
805 word_cnts++;
806 return word_cnts;
807 }
808
efuse_OneByteRead(struct adapter * pAdapter,u16 addr,u8 * data)809 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data)
810 {
811 u8 tmpidx = 0;
812 u8 result;
813
814 usb_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
815 usb_write8(pAdapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
816 (usb_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC));
817
818 usb_write8(pAdapter, EFUSE_CTRL + 3, 0x72);/* read cmd */
819
820 while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
821 tmpidx++;
822 if (tmpidx < 100) {
823 *data = usb_read8(pAdapter, EFUSE_CTRL);
824 result = true;
825 } else {
826 *data = 0xff;
827 result = false;
828 }
829 return result;
830 }
831
efuse_OneByteWrite(struct adapter * pAdapter,u16 addr,u8 data)832 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data)
833 {
834 u8 tmpidx = 0;
835 u8 result;
836
837 usb_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
838 usb_write8(pAdapter, EFUSE_CTRL + 2,
839 (usb_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC) |
840 (u8)((addr >> 8) & 0x03));
841 usb_write8(pAdapter, EFUSE_CTRL, data);/* data */
842
843 usb_write8(pAdapter, EFUSE_CTRL + 3, 0xF2);/* write cmd */
844
845 while ((0x80 & usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
846 tmpidx++;
847
848 if (tmpidx < 100)
849 result = true;
850 else
851 result = false;
852
853 return result;
854 }
855
856 /* Read allowed word in current efuse section data. */
efuse_WordEnableDataRead(u8 word_en,u8 * sourdata,u8 * targetdata)857 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
858 {
859 if (!(word_en & BIT(0))) {
860 targetdata[0] = sourdata[0];
861 targetdata[1] = sourdata[1];
862 }
863 if (!(word_en & BIT(1))) {
864 targetdata[2] = sourdata[2];
865 targetdata[3] = sourdata[3];
866 }
867 if (!(word_en & BIT(2))) {
868 targetdata[4] = sourdata[4];
869 targetdata[5] = sourdata[5];
870 }
871 if (!(word_en & BIT(3))) {
872 targetdata[6] = sourdata[6];
873 targetdata[7] = sourdata[7];
874 }
875 }
876
877 /* Read All Efuse content */
Efuse_ReadAllMap(struct adapter * pAdapter,u8 efuseType,u8 * Efuse)878 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse)
879 {
880 efuse_power_switch(pAdapter, false, true);
881
882 efuse_ReadEFuse(pAdapter, efuseType, 0, EFUSE_MAP_LEN_88E, Efuse);
883
884 efuse_power_switch(pAdapter, false, false);
885 }
886
887 /* Transfer current EFUSE content to shadow init and modify map. */
EFUSE_ShadowMapUpdate(struct adapter * pAdapter,u8 efuseType)888 void EFUSE_ShadowMapUpdate(struct adapter *pAdapter, u8 efuseType)
889 {
890 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
891
892 if (pEEPROM->bautoload_fail_flag)
893 memset(pEEPROM->efuse_eeprom_data, 0xFF, EFUSE_MAP_LEN_88E);
894 else
895 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data);
896 }
897