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 * Function: efuse_power_switch
27 * Overview: When we want to enable write operation, we should change to
28 * pwr on state. When we stop write, we should switch to 500k mode
29 * and disable LDO 2.5V.
32 void efuse_power_switch(struct adapter *pAdapter, u8 write, u8 pwrstate)
38 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
40 /* 1.2V Power: From VDDON with Power Cut(0x0000h[15]), default valid */
41 tmpv16 = usb_read16(pAdapter, REG_SYS_ISO_CTRL);
42 if (!(tmpv16 & PWC_EV12V)) {
44 usb_write16(pAdapter, REG_SYS_ISO_CTRL, tmpv16);
46 /* Reset: 0x0000h[28], default valid */
47 tmpv16 = usb_read16(pAdapter, REG_SYS_FUNC_EN);
48 if (!(tmpv16 & FEN_ELDR)) {
50 usb_write16(pAdapter, REG_SYS_FUNC_EN, tmpv16);
53 /* Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock from ANA, default valid */
54 tmpv16 = usb_read16(pAdapter, REG_SYS_CLKR);
55 if ((!(tmpv16 & LOADER_CLK_EN)) || (!(tmpv16 & ANA8M))) {
56 tmpv16 |= (LOADER_CLK_EN | ANA8M);
57 usb_write16(pAdapter, REG_SYS_CLKR, tmpv16);
61 /* Enable LDO 2.5V before read/write action */
62 tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
64 tempval |= (VOLTAGE_V25 << 4);
65 usb_write8(pAdapter, EFUSE_TEST + 3, (tempval | 0x80));
68 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
71 /* Disable LDO 2.5V after read/write action */
72 tempval = usb_read8(pAdapter, EFUSE_TEST + 3);
73 usb_write8(pAdapter, EFUSE_TEST + 3, (tempval & 0x7F));
79 efuse_phymap_to_logical(u8 *phymap, u16 _offset, u16 _size_byte, u8 *pbuf)
86 u16 **eFuseWord = NULL;
87 u16 efuse_utilized = 0;
90 efuseTbl = kzalloc(EFUSE_MAP_LEN_88E, GFP_KERNEL);
94 eFuseWord = (u16 **)rtw_malloc2d(EFUSE_MAX_SECTION_88E, EFUSE_MAX_WORD_UNIT, sizeof(u16));
96 DBG_88E("%s: alloc eFuseWord fail!\n", __func__);
97 goto eFuseWord_failed;
100 /* 0. Refresh efuse init map as all oxFF. */
101 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
102 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
103 eFuseWord[i][j] = 0xFFFF;
106 /* 1. Read the first byte to check if efuse is empty!!! */
109 rtemp8 = *(phymap+eFuse_Addr);
110 if (rtemp8 != 0xFF) {
114 DBG_88E("EFUSE is empty efuse_Addr-%d efuse_data =%x\n", eFuse_Addr, rtemp8);
119 /* 2. Read real efuse content. Filter PG header and every section data. */
121 while ((rtemp8 != 0xFF) && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
122 /* Check PG header for section num. */
123 if ((rtemp8 & 0x1F) == 0x0F) { /* extended header */
124 u1temp = (rtemp8 & 0xE0) >> 5;
125 rtemp8 = *(phymap+eFuse_Addr);
126 if ((rtemp8 & 0x0F) == 0x0F) {
128 rtemp8 = *(phymap+eFuse_Addr);
130 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E))
134 offset = ((rtemp8 & 0xF0) >> 1) | u1temp;
135 wren = rtemp8 & 0x0F;
139 offset = (rtemp8 >> 4) & 0x0f;
140 wren = rtemp8 & 0x0f;
143 if (offset < EFUSE_MAX_SECTION_88E) {
144 /* Get word enable value from PG header */
145 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
146 /* Check word enable condition in the section */
147 if (!(wren & 0x01)) {
148 rtemp8 = *(phymap+eFuse_Addr);
151 eFuseWord[offset][i] = (rtemp8 & 0xff);
152 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
154 rtemp8 = *(phymap+eFuse_Addr);
157 eFuseWord[offset][i] |= (((u16)rtemp8 << 8) & 0xff00);
159 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
165 /* Read next PG header */
166 rtemp8 = *(phymap+eFuse_Addr);
168 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
175 /* 3. Collect 16 sections and 4 word unit into Efuse map. */
177 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++) {
178 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
179 efuseTbl[(i*8)+(j*2)] = (eFuseWord[i][j] & 0xff);
180 efuseTbl[(i*8)+((j*2)+1)] = ((eFuseWord[i][j] >> 8) & 0xff);
185 /* 4. Copy from Efuse map to output pointer memory!!! */
187 for (i = 0; i < _size_byte; i++)
188 pbuf[i] = efuseTbl[_offset+i];
191 /* 5. Calculate Efuse utilization. */
201 static void efuse_read_phymap_from_txpktbuf(
202 struct adapter *adapter,
203 int bcnhead, /* beacon head, where FW store len(2-byte) and efuse physical map. */
204 u8 *content, /* buffer to store efuse physical map */
205 u16 *size /* for efuse content: the max byte to read. will update to byte read */
209 unsigned long start = 0;
211 u32 lo32 = 0, hi32 = 0;
212 u16 len = 0, count = 0;
218 if (bcnhead < 0) /* if not valid */
219 bcnhead = usb_read8(adapter, REG_TDECTRL+1);
221 DBG_88E("%s bcnhead:%d\n", __func__, bcnhead);
223 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
225 dbg_addr = bcnhead*128/8; /* 8-bytes addressing */
228 usb_write16(adapter, REG_PKTBUF_DBG_ADDR, dbg_addr+i);
230 usb_write8(adapter, REG_TXPKTBUF_DBG, 0);
232 while (!(reg_0x143 = usb_read8(adapter, REG_TXPKTBUF_DBG)) &&
233 jiffies_to_msecs(jiffies - start) < 1000) {
234 DBG_88E("%s polling reg_0x143:0x%02x, reg_0x106:0x%02x\n", __func__, reg_0x143, usb_read8(adapter, 0x106));
235 usleep_range(1000, 2000);
238 lo32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_L);
239 hi32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_H);
246 lenc[0] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L);
247 lenc[1] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L+1);
249 aaabak = le16_to_cpup((__le16 *)lenc);
250 lenbak = le16_to_cpu(*((__le16 *)lenc));
251 aaa = le16_to_cpup((__le16 *)&lo32);
252 len = le16_to_cpu(*((__le16 *)&lo32));
254 limit = min_t(u16, len-2, limit);
256 DBG_88E("%s len:%u, lenbak:%u, aaa:%u, aaabak:%u\n", __func__, len, lenbak, aaa, aaabak);
258 memcpy(pos, ((u8 *)&lo32)+2, (limit >= count+2) ? 2 : limit-count);
259 count += (limit >= count+2) ? 2 : limit-count;
263 memcpy(pos, ((u8 *)&lo32), (limit >= count+4) ? 4 : limit-count);
264 count += (limit >= count+4) ? 4 : limit-count;
268 if (limit > count && len-2 > count) {
269 memcpy(pos, (u8 *)&hi32, (limit >= count+4) ? 4 : limit-count);
270 count += (limit >= count+4) ? 4 : limit-count;
274 if (limit <= count || len-2 <= count)
278 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS);
279 DBG_88E("%s read count:%u\n", __func__, count);
283 static s32 iol_read_efuse(struct adapter *padapter, u8 txpktbuf_bndy, u16 offset, u16 size_byte, u8 *logical_map)
286 u8 physical_map[512];
289 usb_write8(padapter, REG_TDECTRL+1, txpktbuf_bndy);
290 memset(physical_map, 0xFF, 512);
291 usb_write8(padapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
292 status = iol_execute(padapter, CMD_READ_EFUSE_MAP);
293 if (status == _SUCCESS)
294 efuse_read_phymap_from_txpktbuf(padapter, txpktbuf_bndy, physical_map, &size);
295 efuse_phymap_to_logical(physical_map, offset, size_byte, logical_map);
299 void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf)
301 if (rtw_iol_applied(Adapter)) {
302 rtw_hal_power_on(Adapter);
303 iol_mode_enable(Adapter, 1);
304 iol_read_efuse(Adapter, 0, _offset, _size_byte, pbuf);
305 iol_mode_enable(Adapter, 0);
309 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data)
312 u16 start_addr = efuse_addr;
316 memset(tmpdata, 0xff, PGPKT_DATA_SIZE);
318 if (!(word_en & BIT(0))) {
319 tmpaddr = start_addr;
320 efuse_OneByteWrite(pAdapter, start_addr++, data[0]);
321 efuse_OneByteWrite(pAdapter, start_addr++, data[1]);
323 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[0]);
324 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[1]);
325 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
326 badworden &= (~BIT(0));
328 if (!(word_en & BIT(1))) {
329 tmpaddr = start_addr;
330 efuse_OneByteWrite(pAdapter, start_addr++, data[2]);
331 efuse_OneByteWrite(pAdapter, start_addr++, data[3]);
333 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[2]);
334 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[3]);
335 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
336 badworden &= (~BIT(1));
338 if (!(word_en & BIT(2))) {
339 tmpaddr = start_addr;
340 efuse_OneByteWrite(pAdapter, start_addr++, data[4]);
341 efuse_OneByteWrite(pAdapter, start_addr++, data[5]);
343 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[4]);
344 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[5]);
345 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
346 badworden &= (~BIT(2));
348 if (!(word_en & BIT(3))) {
349 tmpaddr = start_addr;
350 efuse_OneByteWrite(pAdapter, start_addr++, data[6]);
351 efuse_OneByteWrite(pAdapter, start_addr++, data[7]);
353 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[6]);
354 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[7]);
355 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
356 badworden &= (~BIT(3));
361 static u16 Efuse_GetCurrentSize(struct adapter *pAdapter)
363 int bContinual = true;
365 u8 hoffset = 0, hworden = 0;
366 u8 efuse_data, word_cnts = 0;
368 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
371 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) &&
372 AVAILABLE_EFUSE_ADDR(efuse_addr)) {
373 if (efuse_data != 0xFF) {
374 if ((efuse_data&0x1F) == 0x0F) { /* extended header */
375 hoffset = efuse_data;
377 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
378 if ((efuse_data & 0x0F) == 0x0F) {
382 hoffset = ((hoffset & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
383 hworden = efuse_data & 0x0F;
386 hoffset = (efuse_data>>4) & 0x0F;
387 hworden = efuse_data & 0x0F;
389 word_cnts = Efuse_CalculateWordCnts(hworden);
390 /* read next header */
391 efuse_addr = efuse_addr + (word_cnts*2)+1;
397 rtw_hal_set_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
402 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data)
404 u8 ReadState = PG_STATE_HEADER;
405 int bContinual = true;
406 int bDataEmpty = true;
407 u8 efuse_data, word_cnts = 0;
409 u8 hoffset = 0, hworden = 0;
416 if (offset > EFUSE_MAX_SECTION_88E)
419 memset(data, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
420 memset(tmpdata, 0xff, sizeof(u8) * PGPKT_DATA_SIZE);
422 /* <Roger_TODO> Efuse has been pre-programmed dummy 5Bytes at the end of Efuse by CP. */
423 /* Skip dummy parts to prevent unexpected data read from Efuse. */
424 /* By pass right now. 2009.02.19. */
425 while (bContinual && AVAILABLE_EFUSE_ADDR(efuse_addr)) {
426 /* Header Read ------------- */
427 if (ReadState & PG_STATE_HEADER) {
428 if (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) && (efuse_data != 0xFF)) {
429 if (EXT_HEADER(efuse_data)) {
430 tmp_header = efuse_data;
432 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
433 if (!ALL_WORDS_DISABLED(efuse_data)) {
434 hoffset = ((tmp_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
435 hworden = efuse_data & 0x0F;
437 DBG_88E("Error, All words disabled\n");
442 hoffset = (efuse_data>>4) & 0x0F;
443 hworden = efuse_data & 0x0F;
445 word_cnts = Efuse_CalculateWordCnts(hworden);
448 if (hoffset == offset) {
449 for (tmpidx = 0; tmpidx < word_cnts*2; tmpidx++) {
450 if (efuse_OneByteRead(pAdapter, efuse_addr+1+tmpidx, &efuse_data)) {
451 tmpdata[tmpidx] = efuse_data;
452 if (efuse_data != 0xff)
456 if (bDataEmpty == false) {
457 ReadState = PG_STATE_DATA;
458 } else {/* read next header */
459 efuse_addr = efuse_addr + (word_cnts*2)+1;
460 ReadState = PG_STATE_HEADER;
462 } else {/* read next header */
463 efuse_addr = efuse_addr + (word_cnts*2)+1;
464 ReadState = PG_STATE_HEADER;
469 } else if (ReadState & PG_STATE_DATA) {
470 /* Data section Read ------------- */
471 efuse_WordEnableDataRead(hworden, tmpdata, data);
472 efuse_addr = efuse_addr + (word_cnts*2)+1;
473 ReadState = PG_STATE_HEADER;
477 if ((data[0] == 0xff) && (data[1] == 0xff) && (data[2] == 0xff) && (data[3] == 0xff) &&
478 (data[4] == 0xff) && (data[5] == 0xff) && (data[6] == 0xff) && (data[7] == 0xff))
484 static bool hal_EfuseFixHeaderProcess(struct adapter *pAdapter, u8 efuseType, struct pgpkt *pFixPkt, u16 *pAddr)
486 u8 originaldata[8], badworden = 0;
487 u16 efuse_addr = *pAddr;
488 u32 PgWriteSuccess = 0;
490 memset(originaldata, 0xff, 8);
492 if (Efuse_PgPacketRead(pAdapter, pFixPkt->offset, originaldata)) {
493 /* check if data exist */
494 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr+1, pFixPkt->word_en, originaldata);
496 if (badworden != 0xf) { /* write fail */
497 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pFixPkt->offset, badworden, originaldata);
502 efuse_addr = Efuse_GetCurrentSize(pAdapter);
504 efuse_addr = efuse_addr + (pFixPkt->word_cnts*2) + 1;
507 efuse_addr = efuse_addr + (pFixPkt->word_cnts*2) + 1;
513 static bool hal_EfusePgPacketWrite2ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
516 u16 efuse_addr = *pAddr;
517 u16 efuse_max_available_len =
518 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
519 u8 pg_header = 0, tmp_header = 0, pg_header_temp = 0;
522 while (efuse_addr < efuse_max_available_len) {
523 pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
524 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
525 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
527 while (tmp_header == 0xFF) {
528 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
531 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
532 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
535 /* to write ext_header */
536 if (tmp_header == pg_header) {
538 pg_header_temp = pg_header;
539 pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
541 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
542 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
544 while (tmp_header == 0xFF) {
545 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
548 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
549 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
552 if ((tmp_header & 0x0F) == 0x0F) { /* word_en PG fail */
553 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
558 } else if (pg_header != tmp_header) { /* offset PG fail */
561 fixPkt.offset = ((pg_header_temp & 0xE0) >> 5) | ((tmp_header & 0xF0) >> 1);
562 fixPkt.word_en = tmp_header & 0x0F;
563 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
564 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
570 } else if ((tmp_header & 0x1F) == 0x0F) { /* wrong extended header */
580 static bool hal_EfusePgPacketWrite1ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
583 u8 pg_header = 0, tmp_header = 0;
584 u16 efuse_addr = *pAddr;
587 pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
589 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
590 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
592 while (tmp_header == 0xFF) {
593 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
595 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
596 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
599 if (pg_header == tmp_header) {
604 fixPkt.offset = (tmp_header>>4) & 0x0F;
605 fixPkt.word_en = tmp_header & 0x0F;
606 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
607 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
615 static bool hal_EfusePgPacketWriteData(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
617 u16 efuse_addr = *pAddr;
619 u32 PgWriteSuccess = 0;
622 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr+1, pTargetPkt->word_en, pTargetPkt->data);
623 if (badworden == 0x0F) {
627 /* reorganize other pg packet */
628 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
636 hal_EfusePgPacketWriteHeader(
637 struct adapter *pAdapter,
640 struct pgpkt *pTargetPkt)
644 if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
645 bRet = hal_EfusePgPacketWrite2ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
647 bRet = hal_EfusePgPacketWrite1ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
652 static bool wordEnMatched(struct pgpkt *pTargetPkt, struct pgpkt *pCurPkt,
655 u8 match_word_en = 0x0F; /* default all words are disabled */
657 /* check if the same words are enabled both target and current PG packet */
658 if (((pTargetPkt->word_en & BIT(0)) == 0) &&
659 ((pCurPkt->word_en & BIT(0)) == 0))
660 match_word_en &= ~BIT(0); /* enable word 0 */
661 if (((pTargetPkt->word_en & BIT(1)) == 0) &&
662 ((pCurPkt->word_en & BIT(1)) == 0))
663 match_word_en &= ~BIT(1); /* enable word 1 */
664 if (((pTargetPkt->word_en & BIT(2)) == 0) &&
665 ((pCurPkt->word_en & BIT(2)) == 0))
666 match_word_en &= ~BIT(2); /* enable word 2 */
667 if (((pTargetPkt->word_en & BIT(3)) == 0) &&
668 ((pCurPkt->word_en & BIT(3)) == 0))
669 match_word_en &= ~BIT(3); /* enable word 3 */
671 *pWden = match_word_en;
673 if (match_word_en != 0xf)
679 static bool hal_EfuseCheckIfDatafollowed(struct adapter *pAdapter, u8 word_cnts, u16 startAddr)
684 for (i = 0; i < (word_cnts*2); i++) {
685 if (efuse_OneByteRead(pAdapter, (startAddr+i), &efuse_data) && (efuse_data != 0xFF))
691 static bool hal_EfusePartialWriteCheck(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
694 u8 i, efuse_data = 0, cur_header = 0;
695 u8 matched_wden = 0, badworden = 0;
697 u16 efuse_max_available_len =
698 EFUSE_REAL_CONTENT_LEN_88E - EFUSE_OOB_PROTECT_BYTES_88E;
701 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
702 startAddr %= EFUSE_REAL_CONTENT_LEN;
705 if (startAddr >= efuse_max_available_len) {
710 if (efuse_OneByteRead(pAdapter, startAddr, &efuse_data) && (efuse_data != 0xFF)) {
711 if (EXT_HEADER(efuse_data)) {
712 cur_header = efuse_data;
714 efuse_OneByteRead(pAdapter, startAddr, &efuse_data);
715 if (ALL_WORDS_DISABLED(efuse_data)) {
719 curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
720 curPkt.word_en = efuse_data & 0x0F;
723 cur_header = efuse_data;
724 curPkt.offset = (cur_header>>4) & 0x0F;
725 curPkt.word_en = cur_header & 0x0F;
728 curPkt.word_cnts = Efuse_CalculateWordCnts(curPkt.word_en);
729 /* if same header is found but no data followed */
730 /* write some part of data followed by the header. */
731 if ((curPkt.offset == pTargetPkt->offset) &&
732 (!hal_EfuseCheckIfDatafollowed(pAdapter, curPkt.word_cnts, startAddr+1)) &&
733 wordEnMatched(pTargetPkt, &curPkt, &matched_wden)) {
734 /* Here to write partial data */
735 badworden = Efuse_WordEnableDataWrite(pAdapter, startAddr+1, matched_wden, pTargetPkt->data);
736 if (badworden != 0x0F) {
737 u32 PgWriteSuccess = 0;
738 /* if write fail on some words, write these bad words again */
740 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
742 if (!PgWriteSuccess) {
743 bRet = false; /* write fail, return */
747 /* partial write ok, update the target packet for later use */
748 for (i = 0; i < 4; i++) {
749 if ((matched_wden & (0x1<<i)) == 0) /* this word has been written */
750 pTargetPkt->word_en |= (0x1<<i); /* disable the word */
752 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
754 /* read from next header */
755 startAddr = startAddr + (curPkt.word_cnts*2) + 1;
757 /* not used header, 0xff */
767 hal_EfusePgCheckAvailableAddr(
768 struct adapter *pAdapter,
772 if (Efuse_GetCurrentSize(pAdapter) >= EFUSE_MAP_LEN_88E)
777 static void hal_EfuseConstructPGPkt(u8 offset, u8 word_en, u8 *pData, struct pgpkt *pTargetPkt)
779 memset((void *)pTargetPkt->data, 0xFF, sizeof(u8)*8);
780 pTargetPkt->offset = offset;
781 pTargetPkt->word_en = word_en;
782 efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
783 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
786 bool Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *pData)
788 struct pgpkt targetPkt;
790 u8 efuseType = EFUSE_WIFI;
792 if (!hal_EfusePgCheckAvailableAddr(pAdapter, efuseType))
795 hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
797 if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt))
800 if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt))
803 if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt))
809 u8 Efuse_CalculateWordCnts(u8 word_en)
813 if (!(word_en & BIT(0)))
814 word_cnts++; /* 0 : write enable */
815 if (!(word_en & BIT(1)))
817 if (!(word_en & BIT(2)))
819 if (!(word_en & BIT(3)))
824 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data)
829 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr & 0xff));
830 usb_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
831 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC));
833 usb_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
835 while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
838 *data = usb_read8(pAdapter, EFUSE_CTRL);
847 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data)
852 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
853 usb_write8(pAdapter, EFUSE_CTRL+2,
854 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC) |
855 (u8)((addr>>8) & 0x03));
856 usb_write8(pAdapter, EFUSE_CTRL, data);/* data */
858 usb_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
860 while ((0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
872 * Overview: Read allowed word in current efuse section data.
874 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
876 if (!(word_en & BIT(0))) {
877 targetdata[0] = sourdata[0];
878 targetdata[1] = sourdata[1];
880 if (!(word_en & BIT(1))) {
881 targetdata[2] = sourdata[2];
882 targetdata[3] = sourdata[3];
884 if (!(word_en & BIT(2))) {
885 targetdata[4] = sourdata[4];
886 targetdata[5] = sourdata[5];
888 if (!(word_en & BIT(3))) {
889 targetdata[6] = sourdata[6];
890 targetdata[7] = sourdata[7];
895 * Overview: Read All Efuse content
897 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse)
899 efuse_power_switch(pAdapter, false, true);
901 efuse_ReadEFuse(pAdapter, efuseType, 0, EFUSE_MAP_LEN_88E, Efuse);
903 efuse_power_switch(pAdapter, false, false);
907 * Overview: Transfer current EFUSE content to shadow init and modify map.
909 void EFUSE_ShadowMapUpdate(
910 struct adapter *pAdapter,
913 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
915 if (pEEPROM->bautoload_fail_flag)
916 memset(pEEPROM->efuse_eeprom_data, 0xFF, EFUSE_MAP_LEN_88E);
918 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data);