1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
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>
16 #define REG_EFUSE_CTRL 0x0030
17 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
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.
28 static void efuse_power_switch(struct adapter *pAdapter, u8 write, u8 pwrstate)
34 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
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)) {
40 usb_write16(pAdapter, REG_SYS_ISO_CTRL, tmpv16);
42 /* Reset: 0x0000h[28], default valid */
43 tmpv16 = usb_read16(pAdapter, REG_SYS_FUNC_EN);
44 if (!(tmpv16 & FEN_ELDR)) {
46 usb_write16(pAdapter, REG_SYS_FUNC_EN, tmpv16);
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);
57 /* Enable LDO 2.5V before read/write action */
58 tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
60 tempval |= (VOLTAGE_V25 << 4);
61 usb_write8(pAdapter, EFUSE_TEST + 3, (tempval | 0x80));
64 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
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));
75 efuse_phymap_to_logical(u8 *phymap, u16 _offset, u16 _size_byte, u8 *pbuf)
82 u16 **eFuseWord = NULL;
83 u16 efuse_utilized = 0;
87 efuseTbl = kzalloc(EFUSE_MAP_LEN_88E, GFP_KERNEL);
91 tmp = kcalloc(EFUSE_MAX_SECTION_88E,
92 sizeof(void *) + EFUSE_MAX_WORD_UNIT * sizeof(u16),
95 DBG_88E("%s: alloc eFuseWord fail!\n", __func__);
96 goto eFuseWord_failed;
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;
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;
108 /* 1. Read the first byte to check if efuse is empty!!! */
111 rtemp8 = *(phymap + eFuse_Addr);
112 if (rtemp8 != 0xFF) {
116 DBG_88E("EFUSE is empty efuse_Addr-%d efuse_data =%x\n", eFuse_Addr, rtemp8);
121 /* 2. Read real efuse content. Filter PG header and every section data. */
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) {
130 rtemp8 = *(phymap + eFuse_Addr);
132 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E))
136 offset = ((rtemp8 & 0xF0) >> 1) | u1temp;
137 wren = rtemp8 & 0x0F;
141 offset = (rtemp8 >> 4) & 0x0f;
142 wren = rtemp8 & 0x0f;
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);
153 eFuseWord[offset][i] = (rtemp8 & 0xff);
154 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
156 rtemp8 = *(phymap + eFuse_Addr);
159 eFuseWord[offset][i] |= (((u16)rtemp8 << 8) & 0xff00);
161 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
167 /* Read next PG header */
168 rtemp8 = *(phymap + eFuse_Addr);
170 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
177 /* 3. Collect 16 sections and 4 word unit into Efuse map. */
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);
187 /* 4. Copy from Efuse map to output pointer memory!!! */
189 for (i = 0; i < _size_byte; i++)
190 pbuf[i] = efuseTbl[_offset + i];
193 /* 5. Calculate Efuse utilization. */
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 */
211 unsigned long start = 0;
213 u32 lo32 = 0, hi32 = 0;
214 u16 len = 0, count = 0;
220 if (bcnhead < 0) /* if not valid */
221 bcnhead = usb_read8(adapter, REG_TDECTRL + 1);
223 DBG_88E("%s bcnhead:%d\n", __func__, bcnhead);
225 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
227 dbg_addr = bcnhead * 128 / 8; /* 8-bytes addressing */
230 usb_write16(adapter, REG_PKTBUF_DBG_ADDR, dbg_addr + i);
232 usb_write8(adapter, REG_TXPKTBUF_DBG, 0);
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);
240 lo32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_L);
241 hi32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_H);
248 lenc[0] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L);
249 lenc[1] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L + 1);
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));
256 limit = min_t(u16, len - 2, limit);
258 DBG_88E("%s len:%u, lenbak:%u, aaa:%u, aaabak:%u\n", __func__, len, lenbak, aaa, aaabak);
260 memcpy(pos, ((u8 *)&lo32) + 2, (limit >= count + 2) ? 2 : limit - count);
261 count += (limit >= count + 2) ? 2 : limit - count;
262 pos = content + count;
265 memcpy(pos, ((u8 *)&lo32), (limit >= count + 4) ? 4 : limit - count);
266 count += (limit >= count + 4) ? 4 : limit - count;
267 pos = content + count;
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;
276 if (limit <= count || len - 2 <= count)
280 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS);
281 DBG_88E("%s read count:%u\n", __func__, count);
285 static s32 iol_read_efuse(struct adapter *padapter, u8 txpktbuf_bndy, u16 offset, u16 size_byte, u8 *logical_map)
288 u8 physical_map[512];
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);
301 void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf)
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);
311 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data)
314 u16 start_addr = efuse_addr;
318 memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
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]);
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));
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]);
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));
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]);
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));
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]);
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));
363 static u16 Efuse_GetCurrentSize(struct adapter *pAdapter)
366 u8 hoffset = 0, hworden = 0;
367 u8 efuse_data, word_cnts = 0;
369 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
371 while (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) &&
372 AVAILABLE_EFUSE_ADDR(efuse_addr)) {
373 if (efuse_data == 0xFF)
375 if ((efuse_data & 0x1F) == 0x0F) { /* extended header */
376 hoffset = efuse_data;
378 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
379 if ((efuse_data & 0x0F) == 0x0F) {
383 hoffset = ((hoffset & 0xE0) >> 5) |
384 ((efuse_data & 0xF0) >> 1);
385 hworden = efuse_data & 0x0F;
388 hoffset = (efuse_data >> 4) & 0x0F;
389 hworden = efuse_data & 0x0F;
391 word_cnts = Efuse_CalculateWordCnts(hworden);
392 /* read next header */
393 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
396 rtw_hal_set_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
401 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data)
403 u8 ReadState = PG_STATE_HEADER;
404 int bDataEmpty = true;
405 u8 efuse_data, word_cnts = 0;
407 u8 hoffset = 0, hworden = 0;
414 if (offset > EFUSE_MAX_SECTION_88E)
417 memset(data, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
418 memset(tmpdata, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
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;
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;
435 DBG_88E("Error, All words disabled\n");
440 hoffset = (efuse_data >> 4) & 0x0F;
441 hworden = efuse_data & 0x0F;
443 word_cnts = Efuse_CalculateWordCnts(hworden);
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)
455 ReadState = PG_STATE_DATA;
456 } else {/* read next header */
457 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
458 ReadState = PG_STATE_HEADER;
460 } else {/* read next header */
461 efuse_addr = efuse_addr + (word_cnts * 2) + 1;
462 ReadState = PG_STATE_HEADER;
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;
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))
482 static bool hal_EfuseFixHeaderProcess(struct adapter *pAdapter, u8 efuseType, struct pgpkt *pFixPkt, u16 *pAddr)
484 u8 originaldata[8], badworden = 0;
485 u16 efuse_addr = *pAddr;
486 u32 PgWriteSuccess = 0;
488 memset(originaldata, 0xff, 8);
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);
494 if (badworden != 0xf) { /* write fail */
495 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pFixPkt->offset, badworden, originaldata);
500 efuse_addr = Efuse_GetCurrentSize(pAdapter);
502 efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
505 efuse_addr = efuse_addr + (pFixPkt->word_cnts * 2) + 1;
511 static bool hal_EfusePgPacketWrite2ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
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;
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);
525 while (tmp_header == 0xFF) {
526 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
529 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
530 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
533 /* to write ext_header */
534 if (tmp_header == pg_header) {
536 pg_header_temp = pg_header;
537 pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
539 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
540 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
542 while (tmp_header == 0xFF) {
543 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
546 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
547 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
550 if ((tmp_header & 0x0F) == 0x0F) { /* word_en PG fail */
551 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
556 } else if (pg_header != tmp_header) { /* offset PG fail */
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))
568 } else if ((tmp_header & 0x1F) == 0x0F) { /* wrong extended header */
578 static bool hal_EfusePgPacketWrite1ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
581 u8 pg_header = 0, tmp_header = 0;
582 u16 efuse_addr = *pAddr;
585 pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
587 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
588 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
590 while (tmp_header == 0xFF) {
591 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
593 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
594 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
597 if (pg_header == tmp_header) {
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))
613 static bool hal_EfusePgPacketWriteData(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
615 u16 efuse_addr = *pAddr;
617 u32 PgWriteSuccess = 0;
619 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr + 1, pTargetPkt->word_en, pTargetPkt->data);
620 if (badworden == 0x0F) {
624 /* reorganize other pg packet */
625 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
633 hal_EfusePgPacketWriteHeader(
634 struct adapter *pAdapter,
637 struct pgpkt *pTargetPkt)
641 if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
642 ret = hal_EfusePgPacketWrite2ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
644 ret = hal_EfusePgPacketWrite1ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
649 static bool wordEnMatched(struct pgpkt *pTargetPkt, struct pgpkt *pCurPkt,
652 u8 match_word_en = 0x0F; /* default all words are disabled */
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 */
668 *pWden = match_word_en;
670 if (match_word_en != 0xf)
676 static bool hal_EfuseCheckIfDatafollowed(struct adapter *pAdapter, u8 word_cnts, u16 startAddr)
681 for (i = 0; i < (word_cnts * 2); i++) {
682 if (efuse_OneByteRead(pAdapter, (startAddr + i), &efuse_data) && (efuse_data != 0xFF))
688 static bool hal_EfusePartialWriteCheck(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
691 u8 i, efuse_data = 0, cur_header = 0;
692 u8 matched_wden = 0, badworden = 0;
694 u16 efuse_max_available_len =
695 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
698 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
699 startAddr %= EFUSE_REAL_CONTENT_LEN;
702 if (startAddr >= efuse_max_available_len) {
707 if (efuse_OneByteRead(pAdapter, startAddr, &efuse_data) && (efuse_data != 0xFF)) {
708 if (EXT_HEADER(efuse_data)) {
709 cur_header = efuse_data;
711 efuse_OneByteRead(pAdapter, startAddr, &efuse_data);
712 if (ALL_WORDS_DISABLED(efuse_data)) {
716 curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
717 curPkt.word_en = efuse_data & 0x0F;
719 cur_header = efuse_data;
720 curPkt.offset = (cur_header >> 4) & 0x0F;
721 curPkt.word_en = cur_header & 0x0F;
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 */
736 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
738 if (!PgWriteSuccess) {
739 ret = false; /* write fail, return */
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 */
748 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
750 /* read from next header */
751 startAddr = startAddr + (curPkt.word_cnts * 2) + 1;
753 /* not used header, 0xff */
762 static void hal_EfuseConstructPGPkt(u8 offset, u8 word_en, u8 *pData, struct pgpkt *pTargetPkt)
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);
771 bool Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *pData)
773 struct pgpkt targetPkt;
775 u8 efuseType = EFUSE_WIFI;
777 if (Efuse_GetCurrentSize(pAdapter) >= EFUSE_MAP_LEN_88E)
780 hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
782 if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt))
785 if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt))
788 if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt))
794 u8 Efuse_CalculateWordCnts(u8 word_en)
798 if (!(word_en & BIT(0)))
799 word_cnts++; /* 0 : write enable */
800 if (!(word_en & BIT(1)))
802 if (!(word_en & BIT(2)))
804 if (!(word_en & BIT(3)))
809 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data)
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));
818 usb_write8(pAdapter, EFUSE_CTRL + 3, 0x72);/* read cmd */
820 while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
823 *data = usb_read8(pAdapter, EFUSE_CTRL);
832 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data)
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 */
843 usb_write8(pAdapter, EFUSE_CTRL + 3, 0xF2);/* write cmd */
845 while ((0x80 & usb_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100))
856 /* Read allowed word in current efuse section data. */
857 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
859 if (!(word_en & BIT(0))) {
860 targetdata[0] = sourdata[0];
861 targetdata[1] = sourdata[1];
863 if (!(word_en & BIT(1))) {
864 targetdata[2] = sourdata[2];
865 targetdata[3] = sourdata[3];
867 if (!(word_en & BIT(2))) {
868 targetdata[4] = sourdata[4];
869 targetdata[5] = sourdata[5];
871 if (!(word_en & BIT(3))) {
872 targetdata[6] = sourdata[6];
873 targetdata[7] = sourdata[7];
877 /* Read All Efuse content */
878 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse)
880 efuse_power_switch(pAdapter, false, true);
882 efuse_ReadEFuse(pAdapter, efuseType, 0, EFUSE_MAP_LEN_88E, Efuse);
884 efuse_power_switch(pAdapter, false, false);
887 /* Transfer current EFUSE content to shadow init and modify map. */
888 void EFUSE_ShadowMapUpdate(struct adapter *pAdapter, u8 efuseType)
890 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
892 if (pEEPROM->bautoload_fail_flag)
893 memset(pEEPROM->efuse_eeprom_data, 0xFF, EFUSE_MAP_LEN_88E);
895 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data);