1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 * This 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.
9 * This 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
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 ******************************************************************************/
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <rtw_efuse.h>
25 #include <usb_ops_linux.h>
26 #include <rtl8188e_hal.h>
29 #define REG_EFUSE_CTRL 0x0030
30 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
38 * Function: Efuse_PowerSwitch
40 * Overview: When we want to enable write operation, we should change to
41 * pwr on state. When we stop write, we should switch to 500k mode
42 * and disable LDO 2.5V.
45 void Efuse_PowerSwitch(
46 struct adapter *pAdapter,
54 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
56 /* 1.2V Power: From VDDON with Power Cut(0x0000h[15]), defualt valid */
57 tmpV16 = usb_read16(pAdapter, REG_SYS_ISO_CTRL);
58 if (!(tmpV16 & PWC_EV12V)) {
60 usb_write16(pAdapter, REG_SYS_ISO_CTRL, tmpV16);
62 /* Reset: 0x0000h[28], default valid */
63 tmpV16 = usb_read16(pAdapter, REG_SYS_FUNC_EN);
64 if (!(tmpV16 & FEN_ELDR)) {
66 usb_write16(pAdapter, REG_SYS_FUNC_EN, tmpV16);
69 /* Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock from ANA, default valid */
70 tmpV16 = usb_read16(pAdapter, REG_SYS_CLKR);
71 if ((!(tmpV16 & LOADER_CLK_EN)) || (!(tmpV16 & ANA8M))) {
72 tmpV16 |= (LOADER_CLK_EN | ANA8M);
73 usb_write16(pAdapter, REG_SYS_CLKR, tmpV16);
77 /* Enable LDO 2.5V before read/write action */
78 tempval = usb_read8(pAdapter, EFUSE_TEST+3);
80 tempval |= (VOLTAGE_V25 << 4);
81 usb_write8(pAdapter, EFUSE_TEST+3, (tempval | 0x80));
84 usb_write8(pAdapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
87 /* Disable LDO 2.5V after read/write action */
88 tempval = usb_read8(pAdapter, EFUSE_TEST+3);
89 usb_write8(pAdapter, EFUSE_TEST+3, (tempval & 0x7F));
95 efuse_phymap_to_logical(u8 *phymap, u16 _offset, u16 _size_byte, u8 *pbuf)
102 u16 **eFuseWord = NULL;
103 u16 efuse_utilized = 0;
106 efuseTbl = kzalloc(EFUSE_MAP_LEN_88E, GFP_KERNEL);
107 if (efuseTbl == NULL) {
108 DBG_88E("%s: alloc efuseTbl fail!\n", __func__);
112 eFuseWord = (u16 **)rtw_malloc2d(EFUSE_MAX_SECTION_88E, EFUSE_MAX_WORD_UNIT, sizeof(u16));
113 if (eFuseWord == NULL) {
114 DBG_88E("%s: alloc eFuseWord fail!\n", __func__);
115 goto eFuseWord_failed;
118 /* 0. Refresh efuse init map as all oxFF. */
119 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++)
120 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++)
121 eFuseWord[i][j] = 0xFFFF;
124 /* 1. Read the first byte to check if efuse is empty!!! */
127 rtemp8 = *(phymap+eFuse_Addr);
128 if (rtemp8 != 0xFF) {
132 DBG_88E("EFUSE is empty efuse_Addr-%d efuse_data =%x\n", eFuse_Addr, rtemp8);
137 /* 2. Read real efuse content. Filter PG header and every section data. */
139 while ((rtemp8 != 0xFF) && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
140 /* Check PG header for section num. */
141 if ((rtemp8 & 0x1F) == 0x0F) { /* extended header */
142 u1temp = (rtemp8 & 0xE0) >> 5;
143 rtemp8 = *(phymap+eFuse_Addr);
144 if ((rtemp8 & 0x0F) == 0x0F) {
146 rtemp8 = *(phymap+eFuse_Addr);
148 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E))
152 offset = ((rtemp8 & 0xF0) >> 1) | u1temp;
153 wren = rtemp8 & 0x0F;
157 offset = (rtemp8 >> 4) & 0x0f;
158 wren = rtemp8 & 0x0f;
161 if (offset < EFUSE_MAX_SECTION_88E) {
162 /* Get word enable value from PG header */
163 for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
164 /* Check word enable condition in the section */
165 if (!(wren & 0x01)) {
166 rtemp8 = *(phymap+eFuse_Addr);
169 eFuseWord[offset][i] = (rtemp8 & 0xff);
170 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
172 rtemp8 = *(phymap+eFuse_Addr);
175 eFuseWord[offset][i] |= (((u16)rtemp8 << 8) & 0xff00);
177 if (eFuse_Addr >= EFUSE_REAL_CONTENT_LEN_88E)
183 /* Read next PG header */
184 rtemp8 = *(phymap+eFuse_Addr);
186 if (rtemp8 != 0xFF && (eFuse_Addr < EFUSE_REAL_CONTENT_LEN_88E)) {
193 /* 3. Collect 16 sections and 4 word unit into Efuse map. */
195 for (i = 0; i < EFUSE_MAX_SECTION_88E; i++) {
196 for (j = 0; j < EFUSE_MAX_WORD_UNIT; j++) {
197 efuseTbl[(i*8)+(j*2)] = (eFuseWord[i][j] & 0xff);
198 efuseTbl[(i*8)+((j*2)+1)] = ((eFuseWord[i][j] >> 8) & 0xff);
203 /* 4. Copy from Efuse map to output pointer memory!!! */
205 for (i = 0; i < _size_byte; i++)
206 pbuf[i] = efuseTbl[_offset+i];
209 /* 5. Calculate Efuse utilization. */
219 static void efuse_read_phymap_from_txpktbuf(
220 struct adapter *adapter,
221 int bcnhead, /* beacon head, where FW store len(2-byte) and efuse physical map. */
222 u8 *content, /* buffer to store efuse physical map */
223 u16 *size /* for efuse content: the max byte to read. will update to byte read */
227 u32 start = 0, passing_time = 0;
229 u32 lo32 = 0, hi32 = 0;
230 u16 len = 0, count = 0;
236 if (bcnhead < 0) /* if not valid */
237 bcnhead = usb_read8(adapter, REG_TDECTRL+1);
239 DBG_88E("%s bcnhead:%d\n", __func__, bcnhead);
241 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
243 dbg_addr = bcnhead*128/8; /* 8-bytes addressing */
246 usb_write16(adapter, REG_PKTBUF_DBG_ADDR, dbg_addr+i);
248 usb_write8(adapter, REG_TXPKTBUF_DBG, 0);
250 while (!(reg_0x143 = usb_read8(adapter, REG_TXPKTBUF_DBG)) &&
251 (passing_time = rtw_get_passing_time_ms(start)) < 1000) {
252 DBG_88E("%s polling reg_0x143:0x%02x, reg_0x106:0x%02x\n", __func__, reg_0x143, usb_read8(adapter, 0x106));
253 usleep_range(1000, 2000);
256 lo32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_L);
257 hi32 = usb_read32(adapter, REG_PKTBUF_DBG_DATA_H);
263 lenc[0] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L);
264 lenc[1] = usb_read8(adapter, REG_PKTBUF_DBG_DATA_L+1);
266 aaabak = le16_to_cpup((__le16 *)lenc);
267 lenbak = le16_to_cpu(*((__le16 *)lenc));
268 aaa = le16_to_cpup((__le16 *)&lo32);
269 len = le16_to_cpu(*((__le16 *)&lo32));
271 limit = min_t(u16, len-2, limit);
273 DBG_88E("%s len:%u, lenbak:%u, aaa:%u, aaabak:%u\n", __func__, len, lenbak, aaa, aaabak);
275 memcpy(pos, ((u8 *)&lo32)+2, (limit >= count+2) ? 2 : limit-count);
276 count += (limit >= count+2) ? 2 : limit-count;
280 memcpy(pos, ((u8 *)&lo32), (limit >= count+4) ? 4 : limit-count);
281 count += (limit >= count+4) ? 4 : limit-count;
285 if (limit > count && len-2 > count) {
286 memcpy(pos, (u8 *)&hi32, (limit >= count+4) ? 4 : limit-count);
287 count += (limit >= count+4) ? 4 : limit-count;
291 if (limit <= count || len-2 <= count)
295 usb_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS);
296 DBG_88E("%s read count:%u\n", __func__, count);
300 static s32 iol_read_efuse(struct adapter *padapter, u8 txpktbuf_bndy, u16 offset, u16 size_byte, u8 *logical_map)
303 u8 physical_map[512];
306 usb_write8(padapter, REG_TDECTRL+1, txpktbuf_bndy);
307 memset(physical_map, 0xFF, 512);
308 usb_write8(padapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT);
309 status = iol_execute(padapter, CMD_READ_EFUSE_MAP);
310 if (status == _SUCCESS)
311 efuse_read_phymap_from_txpktbuf(padapter, txpktbuf_bndy, physical_map, &size);
312 efuse_phymap_to_logical(physical_map, offset, size_byte, logical_map);
316 void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf)
319 if (rtw_IOL_applied(Adapter)) {
320 rtw_hal_power_on(Adapter);
321 iol_mode_enable(Adapter, 1);
322 iol_read_efuse(Adapter, 0, _offset, _size_byte, pbuf);
323 iol_mode_enable(Adapter, 0);
327 /* Do not support BT */
328 void EFUSE_GetEfuseDefinition(struct adapter *pAdapter, u8 efuseType, u8 type, void *pOut)
331 case TYPE_EFUSE_MAX_SECTION:
335 *pMax_section = EFUSE_MAX_SECTION_88E;
338 case TYPE_EFUSE_REAL_CONTENT_LEN:
342 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_88E;
345 case TYPE_EFUSE_CONTENT_LEN_BANK:
349 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_88E;
352 case TYPE_AVAILABLE_EFUSE_BYTES_BANK:
356 *pu2Tmp = (u16)(EFUSE_REAL_CONTENT_LEN_88E-EFUSE_OOB_PROTECT_BYTES_88E);
359 case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL:
363 *pu2Tmp = (u16)(EFUSE_REAL_CONTENT_LEN_88E-EFUSE_OOB_PROTECT_BYTES_88E);
366 case TYPE_EFUSE_MAP_LEN:
370 *pu2Tmp = (u16)EFUSE_MAP_LEN_88E;
373 case TYPE_EFUSE_PROTECT_BYTES_BANK:
377 *pu1Tmp = (u8)(EFUSE_OOB_PROTECT_BYTES_88E);
390 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data)
393 u16 start_addr = efuse_addr;
397 memset((void *)tmpdata, 0xff, PGPKT_DATA_SIZE);
399 if (!(word_en & BIT(0))) {
400 tmpaddr = start_addr;
401 efuse_OneByteWrite(pAdapter, start_addr++, data[0]);
402 efuse_OneByteWrite(pAdapter, start_addr++, data[1]);
404 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[0]);
405 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[1]);
406 if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
407 badworden &= (~BIT(0));
409 if (!(word_en & BIT(1))) {
410 tmpaddr = start_addr;
411 efuse_OneByteWrite(pAdapter, start_addr++, data[2]);
412 efuse_OneByteWrite(pAdapter, start_addr++, data[3]);
414 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[2]);
415 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[3]);
416 if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
417 badworden &= (~BIT(1));
419 if (!(word_en & BIT(2))) {
420 tmpaddr = start_addr;
421 efuse_OneByteWrite(pAdapter, start_addr++, data[4]);
422 efuse_OneByteWrite(pAdapter, start_addr++, data[5]);
424 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[4]);
425 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[5]);
426 if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
427 badworden &= (~BIT(2));
429 if (!(word_en & BIT(3))) {
430 tmpaddr = start_addr;
431 efuse_OneByteWrite(pAdapter, start_addr++, data[6]);
432 efuse_OneByteWrite(pAdapter, start_addr++, data[7]);
434 efuse_OneByteRead(pAdapter, tmpaddr, &tmpdata[6]);
435 efuse_OneByteRead(pAdapter, tmpaddr+1, &tmpdata[7]);
436 if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
437 badworden &= (~BIT(3));
442 static u16 Efuse_GetCurrentSize(struct adapter *pAdapter)
444 int bContinual = true;
446 u8 hoffset = 0, hworden = 0;
447 u8 efuse_data, word_cnts = 0;
449 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
452 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) &&
453 AVAILABLE_EFUSE_ADDR(efuse_addr)) {
454 if (efuse_data != 0xFF) {
455 if ((efuse_data&0x1F) == 0x0F) { /* extended header */
456 hoffset = efuse_data;
458 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
459 if ((efuse_data & 0x0F) == 0x0F) {
463 hoffset = ((hoffset & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
464 hworden = efuse_data & 0x0F;
467 hoffset = (efuse_data>>4) & 0x0F;
468 hworden = efuse_data & 0x0F;
470 word_cnts = Efuse_CalculateWordCnts(hworden);
471 /* read next header */
472 efuse_addr = efuse_addr + (word_cnts*2)+1;
478 rtw_hal_set_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
483 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data)
485 u8 ReadState = PG_STATE_HEADER;
486 int bContinual = true;
487 int bDataEmpty = true;
488 u8 efuse_data, word_cnts = 0;
490 u8 hoffset = 0, hworden = 0;
496 EFUSE_GetEfuseDefinition(pAdapter, EFUSE_WIFI, TYPE_EFUSE_MAX_SECTION, (void *)&max_section);
500 if (offset > max_section)
503 memset((void *)data, 0xff, sizeof(u8)*PGPKT_DATA_SIZE);
504 memset((void *)tmpdata, 0xff, sizeof(u8)*PGPKT_DATA_SIZE);
506 /* <Roger_TODO> Efuse has been pre-programmed dummy 5Bytes at the end of Efuse by CP. */
507 /* Skip dummy parts to prevent unexpected data read from Efuse. */
508 /* By pass right now. 2009.02.19. */
509 while (bContinual && AVAILABLE_EFUSE_ADDR(efuse_addr)) {
510 /* Header Read ------------- */
511 if (ReadState & PG_STATE_HEADER) {
512 if (efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data) && (efuse_data != 0xFF)) {
513 if (EXT_HEADER(efuse_data)) {
514 tmp_header = efuse_data;
516 efuse_OneByteRead(pAdapter, efuse_addr, &efuse_data);
517 if (!ALL_WORDS_DISABLED(efuse_data)) {
518 hoffset = ((tmp_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
519 hworden = efuse_data & 0x0F;
521 DBG_88E("Error, All words disabled\n");
526 hoffset = (efuse_data>>4) & 0x0F;
527 hworden = efuse_data & 0x0F;
529 word_cnts = Efuse_CalculateWordCnts(hworden);
532 if (hoffset == offset) {
533 for (tmpidx = 0; tmpidx < word_cnts*2; tmpidx++) {
534 if (efuse_OneByteRead(pAdapter, efuse_addr+1+tmpidx, &efuse_data)) {
535 tmpdata[tmpidx] = efuse_data;
536 if (efuse_data != 0xff)
540 if (bDataEmpty == false) {
541 ReadState = PG_STATE_DATA;
542 } else {/* read next header */
543 efuse_addr = efuse_addr + (word_cnts*2)+1;
544 ReadState = PG_STATE_HEADER;
546 } else {/* read next header */
547 efuse_addr = efuse_addr + (word_cnts*2)+1;
548 ReadState = PG_STATE_HEADER;
553 } else if (ReadState & PG_STATE_DATA) {
554 /* Data section Read ------------- */
555 efuse_WordEnableDataRead(hworden, tmpdata, data);
556 efuse_addr = efuse_addr + (word_cnts*2)+1;
557 ReadState = PG_STATE_HEADER;
562 if ((data[0] == 0xff) && (data[1] == 0xff) && (data[2] == 0xff) && (data[3] == 0xff) &&
563 (data[4] == 0xff) && (data[5] == 0xff) && (data[6] == 0xff) && (data[7] == 0xff))
569 static bool hal_EfuseFixHeaderProcess(struct adapter *pAdapter, u8 efuseType, struct pgpkt *pFixPkt, u16 *pAddr)
571 u8 originaldata[8], badworden = 0;
572 u16 efuse_addr = *pAddr;
573 u32 PgWriteSuccess = 0;
575 memset((void *)originaldata, 0xff, 8);
577 if (Efuse_PgPacketRead(pAdapter, pFixPkt->offset, originaldata)) {
578 /* check if data exist */
579 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr+1, pFixPkt->word_en, originaldata);
581 if (badworden != 0xf) { /* write fail */
582 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pFixPkt->offset, badworden, originaldata);
587 efuse_addr = Efuse_GetCurrentSize(pAdapter);
589 efuse_addr = efuse_addr + (pFixPkt->word_cnts*2) + 1;
592 efuse_addr = efuse_addr + (pFixPkt->word_cnts*2) + 1;
598 static bool hal_EfusePgPacketWrite2ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
601 u16 efuse_addr = *pAddr, efuse_max_available_len = 0;
602 u8 pg_header = 0, tmp_header = 0, pg_header_temp = 0;
605 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_AVAILABLE_EFUSE_BYTES_BANK, (void *)&efuse_max_available_len);
607 while (efuse_addr < efuse_max_available_len) {
608 pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
609 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
610 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
612 while (tmp_header == 0xFF) {
613 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
616 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
617 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
620 /* to write ext_header */
621 if (tmp_header == pg_header) {
623 pg_header_temp = pg_header;
624 pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
626 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
627 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
629 while (tmp_header == 0xFF) {
630 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
633 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
634 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
637 if ((tmp_header & 0x0F) == 0x0F) { /* word_en PG fail */
638 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_) {
643 } else if (pg_header != tmp_header) { /* offset PG fail */
645 fixPkt.offset = ((pg_header_temp & 0xE0) >> 5) | ((tmp_header & 0xF0) >> 1);
646 fixPkt.word_en = tmp_header & 0x0F;
647 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
648 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
654 } else if ((tmp_header & 0x1F) == 0x0F) { /* wrong extended header */
664 static bool hal_EfusePgPacketWrite1ByteHeader(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
667 u8 pg_header = 0, tmp_header = 0;
668 u16 efuse_addr = *pAddr;
671 pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
673 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
674 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
676 while (tmp_header == 0xFF) {
677 if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
679 efuse_OneByteWrite(pAdapter, efuse_addr, pg_header);
680 efuse_OneByteRead(pAdapter, efuse_addr, &tmp_header);
683 if (pg_header == tmp_header) {
687 fixPkt.offset = (tmp_header>>4) & 0x0F;
688 fixPkt.word_en = tmp_header & 0x0F;
689 fixPkt.word_cnts = Efuse_CalculateWordCnts(fixPkt.word_en);
690 if (!hal_EfuseFixHeaderProcess(pAdapter, efuseType, &fixPkt, &efuse_addr))
698 static bool hal_EfusePgPacketWriteData(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
700 u16 efuse_addr = *pAddr;
702 u32 PgWriteSuccess = 0;
705 badworden = Efuse_WordEnableDataWrite(pAdapter, efuse_addr+1, pTargetPkt->word_en, pTargetPkt->data);
706 if (badworden == 0x0F) {
710 /* reorganize other pg packet */
711 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
719 hal_EfusePgPacketWriteHeader(
720 struct adapter *pAdapter,
723 struct pgpkt *pTargetPkt)
727 if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
728 bRet = hal_EfusePgPacketWrite2ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
730 bRet = hal_EfusePgPacketWrite1ByteHeader(pAdapter, efuseType, pAddr, pTargetPkt);
735 static bool wordEnMatched(struct pgpkt *pTargetPkt, struct pgpkt *pCurPkt,
738 u8 match_word_en = 0x0F; /* default all words are disabled */
740 /* check if the same words are enabled both target and current PG packet */
741 if (((pTargetPkt->word_en & BIT(0)) == 0) &&
742 ((pCurPkt->word_en & BIT(0)) == 0))
743 match_word_en &= ~BIT(0); /* enable word 0 */
744 if (((pTargetPkt->word_en & BIT(1)) == 0) &&
745 ((pCurPkt->word_en & BIT(1)) == 0))
746 match_word_en &= ~BIT(1); /* enable word 1 */
747 if (((pTargetPkt->word_en & BIT(2)) == 0) &&
748 ((pCurPkt->word_en & BIT(2)) == 0))
749 match_word_en &= ~BIT(2); /* enable word 2 */
750 if (((pTargetPkt->word_en & BIT(3)) == 0) &&
751 ((pCurPkt->word_en & BIT(3)) == 0))
752 match_word_en &= ~BIT(3); /* enable word 3 */
754 *pWden = match_word_en;
756 if (match_word_en != 0xf)
762 static bool hal_EfuseCheckIfDatafollowed(struct adapter *pAdapter, u8 word_cnts, u16 startAddr)
767 for (i = 0; i < (word_cnts*2); i++) {
768 if (efuse_OneByteRead(pAdapter, (startAddr+i), &efuse_data) && (efuse_data != 0xFF))
774 static bool hal_EfusePartialWriteCheck(struct adapter *pAdapter, u8 efuseType, u16 *pAddr, struct pgpkt *pTargetPkt)
777 u8 i, efuse_data = 0, cur_header = 0;
778 u8 matched_wden = 0, badworden = 0;
779 u16 startAddr = 0, efuse_max_available_len = 0, efuse_max = 0;
782 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_AVAILABLE_EFUSE_BYTES_BANK, (void *)&efuse_max_available_len);
783 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&efuse_max);
785 rtw_hal_get_hwreg(pAdapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
786 startAddr %= EFUSE_REAL_CONTENT_LEN;
789 if (startAddr >= efuse_max_available_len) {
794 if (efuse_OneByteRead(pAdapter, startAddr, &efuse_data) && (efuse_data != 0xFF)) {
795 if (EXT_HEADER(efuse_data)) {
796 cur_header = efuse_data;
798 efuse_OneByteRead(pAdapter, startAddr, &efuse_data);
799 if (ALL_WORDS_DISABLED(efuse_data)) {
803 curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
804 curPkt.word_en = efuse_data & 0x0F;
807 cur_header = efuse_data;
808 curPkt.offset = (cur_header>>4) & 0x0F;
809 curPkt.word_en = cur_header & 0x0F;
812 curPkt.word_cnts = Efuse_CalculateWordCnts(curPkt.word_en);
813 /* if same header is found but no data followed */
814 /* write some part of data followed by the header. */
815 if ((curPkt.offset == pTargetPkt->offset) &&
816 (!hal_EfuseCheckIfDatafollowed(pAdapter, curPkt.word_cnts, startAddr+1)) &&
817 wordEnMatched(pTargetPkt, &curPkt, &matched_wden)) {
818 /* Here to write partial data */
819 badworden = Efuse_WordEnableDataWrite(pAdapter, startAddr+1, matched_wden, pTargetPkt->data);
820 if (badworden != 0x0F) {
821 u32 PgWriteSuccess = 0;
822 /* if write fail on some words, write these bad words again */
824 PgWriteSuccess = Efuse_PgPacketWrite(pAdapter, pTargetPkt->offset, badworden, pTargetPkt->data);
826 if (!PgWriteSuccess) {
827 bRet = false; /* write fail, return */
831 /* partial write ok, update the target packet for later use */
832 for (i = 0; i < 4; i++) {
833 if ((matched_wden & (0x1<<i)) == 0) /* this word has been written */
834 pTargetPkt->word_en |= (0x1<<i); /* disable the word */
836 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
838 /* read from next header */
839 startAddr = startAddr + (curPkt.word_cnts*2) + 1;
841 /* not used header, 0xff */
851 hal_EfusePgCheckAvailableAddr(
852 struct adapter *pAdapter,
856 u16 efuse_max_available_len = 0;
858 /* Change to check TYPE_EFUSE_MAP_LEN , because 8188E raw 256, logic map over 256. */
859 EFUSE_GetEfuseDefinition(pAdapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&efuse_max_available_len);
861 if (Efuse_GetCurrentSize(pAdapter) >= efuse_max_available_len)
866 static void hal_EfuseConstructPGPkt(u8 offset, u8 word_en, u8 *pData, struct pgpkt *pTargetPkt)
868 memset((void *)pTargetPkt->data, 0xFF, sizeof(u8)*8);
869 pTargetPkt->offset = offset;
870 pTargetPkt->word_en = word_en;
871 efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
872 pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
875 bool Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *pData)
877 struct pgpkt targetPkt;
879 u8 efuseType = EFUSE_WIFI;
881 if (!hal_EfusePgCheckAvailableAddr(pAdapter, efuseType))
884 hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
886 if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt))
889 if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt))
892 if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt))
898 u8 Efuse_CalculateWordCnts(u8 word_en)
901 if (!(word_en & BIT(0)))
902 word_cnts++; /* 0 : write enable */
903 if (!(word_en & BIT(1)))
905 if (!(word_en & BIT(2)))
907 if (!(word_en & BIT(3)))
912 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data)
917 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr & 0xff));
918 usb_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
919 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC));
921 usb_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
923 while (!(0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
926 *data = usb_read8(pAdapter, EFUSE_CTRL);
935 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data)
940 usb_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
941 usb_write8(pAdapter, EFUSE_CTRL+2,
942 (usb_read8(pAdapter, EFUSE_CTRL+2) & 0xFC) |
943 (u8)((addr>>8) & 0x03));
944 usb_write8(pAdapter, EFUSE_CTRL, data);/* data */
946 usb_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
948 while ((0x80 & usb_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
960 * Overview: Read allowed word in current efuse section data.
962 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
964 if (!(word_en & BIT(0))) {
965 targetdata[0] = sourdata[0];
966 targetdata[1] = sourdata[1];
968 if (!(word_en & BIT(1))) {
969 targetdata[2] = sourdata[2];
970 targetdata[3] = sourdata[3];
972 if (!(word_en & BIT(2))) {
973 targetdata[4] = sourdata[4];
974 targetdata[5] = sourdata[5];
976 if (!(word_en & BIT(3))) {
977 targetdata[6] = sourdata[6];
978 targetdata[7] = sourdata[7];
983 * Overview: Read All Efuse content
985 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse)
989 Efuse_PowerSwitch(pAdapter, false, true);
991 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
993 efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse);
995 Efuse_PowerSwitch(pAdapter, false, false);
999 * Overview: Transfer current EFUSE content to shadow init and modify map.
1001 void EFUSE_ShadowMapUpdate(
1002 struct adapter *pAdapter,
1005 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
1008 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
1010 if (pEEPROM->bautoload_fail_flag)
1011 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
1013 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data);