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>
28 /*------------------------Define local variable------------------------------*/
30 u32 fakeEfuseUsedBytes;
31 u8 fakeEfuseContent[EFUSE_MAX_HW_SIZE] = {0};
32 u8 fakeEfuseInitMap[EFUSE_MAX_MAP_LEN] = {0};
33 u8 fakeEfuseModifiedMap[EFUSE_MAX_MAP_LEN] = {0};
36 u8 BTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
37 u8 BTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
38 u8 BTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
40 u32 fakeBTEfuseUsedBytes;
41 u8 fakeBTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
42 u8 fakeBTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
43 u8 fakeBTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
44 /*------------------------Define local variable------------------------------*/
47 #define REG_EFUSE_CTRL 0x0030
48 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
52 Efuse_Read1ByteFromFakeContent(
53 struct adapter *pAdapter,
57 Efuse_Read1ByteFromFakeContent(
58 struct adapter *pAdapter,
62 if (Offset >= EFUSE_MAX_HW_SIZE)
64 if (fakeEfuseBank == 0)
65 *Value = fakeEfuseContent[Offset];
67 *Value = fakeBTEfuseContent[fakeEfuseBank-1][Offset];
72 Efuse_Write1ByteToFakeContent(
73 struct adapter *pAdapter,
77 if (Offset >= EFUSE_MAX_HW_SIZE)
79 if (fakeEfuseBank == 0)
80 fakeEfuseContent[Offset] = Value;
82 fakeBTEfuseContent[fakeEfuseBank-1][Offset] = Value;
86 /*-----------------------------------------------------------------------------
87 * Function: Efuse_PowerSwitch
89 * Overview: When we want to enable write operation, we should change to
90 * pwr on state. When we stop write, we should switch to 500k mode
91 * and disable LDO 2.5V.
101 * 11/17/2008 MHC Create Version 0.
103 *---------------------------------------------------------------------------*/
106 struct adapter *pAdapter,
110 pAdapter->HalFunc.EfusePowerSwitch(pAdapter, write, PwrState);
113 /*-----------------------------------------------------------------------------
114 * Function: efuse_GetCurrentSize
116 * Overview: Get current efuse size!!!
126 * 11/16/2008 MHC Create Version 0.
128 *---------------------------------------------------------------------------*/
130 Efuse_GetCurrentSize(
131 struct adapter *pAdapter,
137 ret = pAdapter->HalFunc.EfuseGetCurrentSize(pAdapter, efuseType, pseudo);
142 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
144 Efuse_CalculateWordCnts(u8 word_en)
147 if (!(word_en & BIT(0)))
148 word_cnts++; /* 0 : write enable */
149 if (!(word_en & BIT(1)))
151 if (!(word_en & BIT(2)))
153 if (!(word_en & BIT(3)))
160 * Execute E-Fuse read byte operation.
161 * Referred from SD1 Richard.
163 * 1. Boot from E-Fuse and successfully auto-load.
164 * 2. PASSIVE_LEVEL (USB interface)
165 * Created by Roger, 2008.10.21.
169 struct adapter *Adapter,
179 Efuse_Read1ByteFromFakeContent(Adapter, _offset, pbuf);
184 rtw_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
185 readbyte = rtw_read8(Adapter, EFUSE_CTRL+2);
186 rtw_write8(Adapter, EFUSE_CTRL+2, ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
189 readbyte = rtw_read8(Adapter, EFUSE_CTRL+3);
190 rtw_write8(Adapter, EFUSE_CTRL+3, (readbyte & 0x7f));
192 /* Check bit 32 read-ready */
194 value32 = rtw_read32(Adapter, EFUSE_CTRL);
195 while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
196 value32 = rtw_read32(Adapter, EFUSE_CTRL);
200 /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
201 /* This fix the problem that Efuse read error in high temperature condition. */
202 /* Designer says that there shall be some delay after ready bit is set, or the */
203 /* result will always stay on last data we read. */
205 value32 = rtw_read32(Adapter, EFUSE_CTRL);
207 *pbuf = (u8)(value32 & 0xff);
211 * 1. Execute E-Fuse read byte operation according as map offset and
212 * save to E-Fuse table.
213 * 2. Referred from SD1 Richard.
215 * 1. Boot from E-Fuse and successfully auto-load.
216 * 2. PASSIVE_LEVEL (USB interface)
217 * Created by Roger, 2008.10.21.
219 * 1. Reorganize code flow and reserve bytes. and add description.
220 * 2. Add efuse utilization collect.
222 * Read Efuse must check if we write section 1 data again!!!
223 * Sec1 write addr must be after sec5.
226 static void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf, bool pseudo)
228 Adapter->HalFunc.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, pseudo);
231 void EFUSE_GetEfuseDefinition(struct adapter *pAdapter, u8 efuseType, u8 type, void *pOut, bool pseudo
234 pAdapter->HalFunc.EFUSEGetEfuseDefinition(pAdapter, efuseType, type, pOut, pseudo);
237 /*-----------------------------------------------------------------------------
238 * Function: EFUSE_Read1Byte
240 * Overview: Copy from WMAC fot EFUSE read 1 byte.
250 * 09/23/2008 MHC Copy from WMAC.
252 *---------------------------------------------------------------------------*/
253 u8 EFUSE_Read1Byte(struct adapter *Adapter, u16 Address)
256 u8 Bytetemp = {0x00};
261 EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI , TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&contentLen, false);
263 if (Address < contentLen) { /* E-fuse 512Byte */
264 /* Write E-fuse Register address bit0~7 */
265 temp = Address & 0xFF;
266 rtw_write8(Adapter, EFUSE_CTRL+1, temp);
267 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);
268 /* Write E-fuse Register address bit8~9 */
269 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
270 rtw_write8(Adapter, EFUSE_CTRL+2, temp);
272 /* Write 0x30[31]= 0 */
273 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
274 temp = Bytetemp & 0x7F;
275 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
277 /* Wait Write-ready (0x30[31]= 1) */
278 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
279 while (!(Bytetemp & 0x80)) {
280 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
287 data = rtw_read8(Adapter, EFUSE_CTRL);
293 } /* EFUSE_Read1Byte */
295 /* 11/16/2008 MH Read one byte from real Efuse. */
296 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data, bool pseudo)
302 result = Efuse_Read1ByteFromFakeContent(pAdapter, addr, data);
305 /* -----------------e-fuse reg ctrl --------------------------------- */
307 rtw_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr & 0xff));
308 rtw_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
309 (rtw_read8(pAdapter, EFUSE_CTRL+2) & 0xFC));
311 rtw_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
313 while (!(0x80 & rtw_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
316 *data = rtw_read8(pAdapter, EFUSE_CTRL);
325 /* 11/16/2008 MH Write one byte to reald Efuse. */
326 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data, bool pseudo)
332 result = Efuse_Write1ByteToFakeContent(pAdapter, addr, data);
336 /* -----------------e-fuse reg ctrl --------------------------------- */
338 rtw_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
339 rtw_write8(pAdapter, EFUSE_CTRL+2,
340 (rtw_read8(pAdapter, EFUSE_CTRL+2) & 0xFC) |
341 (u8)((addr>>8) & 0x03));
342 rtw_write8(pAdapter, EFUSE_CTRL, data);/* data */
344 rtw_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
346 while ((0x80 & rtw_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
357 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data, bool pseudo)
361 ret = pAdapter->HalFunc.Efuse_PgPacketRead(pAdapter, offset, data, pseudo);
366 int Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *data, bool pseudo)
370 ret = pAdapter->HalFunc.Efuse_PgPacketWrite(pAdapter, offset, word_en, data, pseudo);
376 static int Efuse_PgPacketWrite_BT(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *data, bool pseudo)
380 ret = pAdapter->HalFunc.Efuse_PgPacketWrite_BT(pAdapter, offset, word_en, data, pseudo);
385 /*-----------------------------------------------------------------------------
386 * Function: efuse_WordEnableDataRead
388 * Overview: Read allowed word in current efuse section data.
398 * 11/16/2008 MHC Create Version 0.
399 * 11/21/2008 MHC Fix Write bug when we only enable late word.
401 *---------------------------------------------------------------------------*/
402 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
404 if (!(word_en&BIT(0))) {
405 targetdata[0] = sourdata[0];
406 targetdata[1] = sourdata[1];
408 if (!(word_en&BIT(1))) {
409 targetdata[2] = sourdata[2];
410 targetdata[3] = sourdata[3];
412 if (!(word_en&BIT(2))) {
413 targetdata[4] = sourdata[4];
414 targetdata[5] = sourdata[5];
416 if (!(word_en&BIT(3))) {
417 targetdata[6] = sourdata[6];
418 targetdata[7] = sourdata[7];
422 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data, bool pseudo)
426 ret = pAdapter->HalFunc.Efuse_WordEnableDataWrite(pAdapter, efuse_addr, word_en, data, pseudo);
431 static u8 efuse_read8(struct adapter *padapter, u16 address, u8 *value)
433 return efuse_OneByteRead(padapter, address, value, false);
436 static u8 efuse_write8(struct adapter *padapter, u16 address, u8 *value)
438 return efuse_OneByteWrite(padapter, address, *value, false);
442 * read/wirte raw efuse data
444 u8 rtw_efuse_access(struct adapter *padapter, u8 write, u16 start_addr, u16 cnts, u8 *data)
447 u16 real_content_len = 0, max_available_size = 0;
449 u8 (*rw8)(struct adapter *, u16, u8*);
451 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&real_content_len, false);
452 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_available_size, false);
454 if (start_addr > real_content_len)
458 if ((start_addr + cnts) > max_available_size)
465 Efuse_PowerSwitch(padapter, write, true);
467 /* e-fuse one byte read / write */
468 for (i = 0; i < cnts; i++) {
469 if (start_addr >= real_content_len) {
474 res = rw8(padapter, start_addr++, data++);
479 Efuse_PowerSwitch(padapter, write, false);
484 u16 efuse_GetMaxSize(struct adapter *padapter)
487 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_size, false);
491 u8 efuse_GetCurrentSize(struct adapter *padapter, u16 *size)
493 Efuse_PowerSwitch(padapter, false, true);
494 *size = Efuse_GetCurrentSize(padapter, EFUSE_WIFI, false);
495 Efuse_PowerSwitch(padapter, false, false);
500 u8 rtw_efuse_map_read(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
504 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
506 if ((addr + cnts) > mapLen)
509 Efuse_PowerSwitch(padapter, false, true);
511 efuse_ReadEFuse(padapter, EFUSE_WIFI, addr, cnts, data, false);
513 Efuse_PowerSwitch(padapter, false, false);
518 u8 rtw_BT_efuse_map_read(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
522 EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
524 if ((addr + cnts) > mapLen)
527 Efuse_PowerSwitch(padapter, false, true);
529 efuse_ReadEFuse(padapter, EFUSE_BT, addr, cnts, data, false);
531 Efuse_PowerSwitch(padapter, false, false);
536 u8 rtw_efuse_map_write(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
540 u8 newdata[PGPKT_DATA_SIZE + 1];
545 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
547 if ((addr + cnts) > mapLen)
550 map = rtw_zmalloc(mapLen);
554 ret = rtw_efuse_map_read(padapter, 0, mapLen, map);
558 Efuse_PowerSwitch(padapter, true, true);
560 offset = (addr >> 3);
562 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE + 1);
563 i = addr & 0x7; /* index of one package */
564 idx = 0; /* data index */
568 if (data[idx] != map[addr+idx]) {
569 word_en &= ~BIT(i >> 1);
570 newdata[i-1] = map[addr+idx-1];
571 newdata[i] = data[idx];
577 for (; i < PGPKT_DATA_SIZE; i += 2) {
580 if ((cnts - idx) == 1) {
581 if (data[idx] != map[addr+idx]) {
582 word_en &= ~BIT(i >> 1);
583 newdata[i] = data[idx];
584 newdata[i+1] = map[addr+idx+1];
589 if ((data[idx] != map[addr+idx]) ||
590 (data[idx+1] != map[addr+idx+1])) {
591 word_en &= ~BIT(i >> 1);
592 newdata[i] = data[idx];
593 newdata[i+1] = data[idx + 1];
601 if (word_en != 0xF) {
602 ret = Efuse_PgPacketWrite(padapter, offset, word_en, newdata, false);
603 DBG_88E("offset=%x\n", offset);
604 DBG_88E("word_en=%x\n", word_en);
606 for (i = 0; i < PGPKT_DATA_SIZE; i++)
607 DBG_88E("data=%x \t", newdata[i]);
618 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE);
621 Efuse_PowerSwitch(padapter, true, false);
628 u8 rtw_BT_efuse_map_write(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
632 u8 newdata[PGPKT_DATA_SIZE + 1];
637 EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
639 if ((addr + cnts) > mapLen)
642 map = rtw_zmalloc(mapLen);
646 ret = rtw_BT_efuse_map_read(padapter, 0, mapLen, map);
650 Efuse_PowerSwitch(padapter, true, true);
652 offset = (addr >> 3);
654 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE + 1);
655 i = addr & 0x7; /* index of one package */
656 idx = 0; /* data index */
660 if (data[idx] != map[addr+idx]) {
661 word_en &= ~BIT(i >> 1);
662 newdata[i-1] = map[addr+idx-1];
663 newdata[i] = data[idx];
669 for (; i < PGPKT_DATA_SIZE; i += 2) {
672 if ((cnts - idx) == 1) {
673 if (data[idx] != map[addr+idx]) {
674 word_en &= ~BIT(i >> 1);
675 newdata[i] = data[idx];
676 newdata[i+1] = map[addr+idx+1];
681 if ((data[idx] != map[addr+idx]) ||
682 (data[idx+1] != map[addr+idx+1])) {
683 word_en &= ~BIT(i >> 1);
684 newdata[i] = data[idx];
685 newdata[i+1] = data[idx + 1];
693 if (word_en != 0xF) {
694 DBG_88E("%s: offset=%#X\n", __func__, offset);
695 DBG_88E("%s: word_en=%#X\n", __func__, word_en);
696 DBG_88E("%s: data=", __func__);
697 for (i = 0; i < PGPKT_DATA_SIZE; i++)
698 DBG_88E("0x%02X ", newdata[i]);
701 ret = Efuse_PgPacketWrite_BT(padapter, offset, word_en, newdata, false);
712 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE);
715 Efuse_PowerSwitch(padapter, true, false);
724 /*-----------------------------------------------------------------------------
725 * Function: efuse_ShadowRead1Byte
726 * efuse_ShadowRead2Byte
727 * efuse_ShadowRead4Byte
729 * Overview: Read from efuse init map by one/two/four bytes !!!!!
739 * 11/12/2008 MHC Create Version 0.
741 *---------------------------------------------------------------------------*/
743 efuse_ShadowRead1Byte(
744 struct adapter *pAdapter,
748 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
750 *Value = pEEPROM->efuse_eeprom_data[Offset];
752 } /* EFUSE_ShadowRead1Byte */
756 efuse_ShadowRead2Byte(
757 struct adapter *pAdapter,
761 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
763 *Value = pEEPROM->efuse_eeprom_data[Offset];
764 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
766 } /* EFUSE_ShadowRead2Byte */
768 /* Read Four Bytes */
770 efuse_ShadowRead4Byte(
771 struct adapter *pAdapter,
775 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
777 *Value = pEEPROM->efuse_eeprom_data[Offset];
778 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
779 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
780 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
782 } /* efuse_ShadowRead4Byte */
784 /*-----------------------------------------------------------------------------
785 * Function: Efuse_ReadAllMap
787 * Overview: Read All Efuse content
797 * 11/11/2008 MHC Create Version 0.
799 *---------------------------------------------------------------------------*/
800 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse, bool pseudo)
804 Efuse_PowerSwitch(pAdapter, false, true);
806 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, pseudo);
808 efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse, pseudo);
810 Efuse_PowerSwitch(pAdapter, false, false);
813 /*-----------------------------------------------------------------------------
814 * Function: EFUSE_ShadowMapUpdate
816 * Overview: Transfer current EFUSE content to shadow init and modify map.
826 * 11/13/2008 MHC Create Version 0.
828 *---------------------------------------------------------------------------*/
829 void EFUSE_ShadowMapUpdate(
830 struct adapter *pAdapter,
834 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
837 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, pseudo);
839 if (pEEPROM->bautoload_fail_flag)
840 _rtw_memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
842 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data, pseudo);
843 } /* EFUSE_ShadowMapUpdate */
845 /*-----------------------------------------------------------------------------
846 * Function: EFUSE_ShadowRead
848 * Overview: Read from efuse init map !!!!!
858 * 11/12/2008 MHC Create Version 0.
860 *---------------------------------------------------------------------------*/
861 void EFUSE_ShadowRead(struct adapter *pAdapter, u8 Type, u16 Offset, u32 *Value)
864 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
866 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
868 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
870 } /* EFUSE_ShadowRead */