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;
87 /*-----------------------------------------------------------------------------
88 * Function: Efuse_PowerSwitch
90 * Overview: When we want to enable write operation, we should change to
91 * pwr on state. When we stop write, we should switch to 500k mode
92 * and disable LDO 2.5V.
102 * 11/17/2008 MHC Create Version 0.
104 *---------------------------------------------------------------------------*/
107 struct adapter *pAdapter,
111 pAdapter->HalFunc.EfusePowerSwitch(pAdapter, write, PwrState);
114 /*-----------------------------------------------------------------------------
115 * Function: efuse_GetCurrentSize
117 * Overview: Get current efuse size!!!
127 * 11/16/2008 MHC Create Version 0.
129 *---------------------------------------------------------------------------*/
131 Efuse_GetCurrentSize(
132 struct adapter *pAdapter,
138 ret = pAdapter->HalFunc.EfuseGetCurrentSize(pAdapter, efuseType, pseudo);
143 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
145 Efuse_CalculateWordCnts(u8 word_en)
148 if (!(word_en & BIT(0)))
149 word_cnts++; /* 0 : write enable */
150 if (!(word_en & BIT(1)))
152 if (!(word_en & BIT(2)))
154 if (!(word_en & BIT(3)))
161 /* Execute E-Fuse read byte operation. */
162 /* Referred from SD1 Richard. */
165 /* 1. Boot from E-Fuse and successfully auto-load. */
166 /* 2. PASSIVE_LEVEL (USB interface) */
168 /* Created by Roger, 2008.10.21. */
172 struct adapter *Adapter,
182 Efuse_Read1ByteFromFakeContent(Adapter, _offset, pbuf);
187 rtw_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
188 readbyte = rtw_read8(Adapter, EFUSE_CTRL+2);
189 rtw_write8(Adapter, EFUSE_CTRL+2, ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
192 readbyte = rtw_read8(Adapter, EFUSE_CTRL+3);
193 rtw_write8(Adapter, EFUSE_CTRL+3, (readbyte & 0x7f));
195 /* Check bit 32 read-ready */
197 value32 = rtw_read32(Adapter, EFUSE_CTRL);
198 while (!(((value32 >> 24) & 0xff) & 0x80) && (retry < 10000)) {
199 value32 = rtw_read32(Adapter, EFUSE_CTRL);
203 /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
204 /* This fix the problem that Efuse read error in high temperature condition. */
205 /* Designer says that there shall be some delay after ready bit is set, or the */
206 /* result will always stay on last data we read. */
208 value32 = rtw_read32(Adapter, EFUSE_CTRL);
210 *pbuf = (u8)(value32 & 0xff);
215 /* 1. Execute E-Fuse read byte operation according as map offset and */
216 /* save to E-Fuse table. */
217 /* 2. Referred from SD1 Richard. */
220 /* 1. Boot from E-Fuse and successfully auto-load. */
221 /* 2. PASSIVE_LEVEL (USB interface) */
223 /* Created by Roger, 2008.10.21. */
225 /* 2008/12/12 MH 1. Reorganize code flow and reserve bytes. and add description. */
226 /* 2. Add efuse utilization collect. */
227 /* 2008/12/22 MH Read Efuse must check if we write section 1 data again!!! Sec1 */
228 /* write addr must be after sec5. */
231 static void efuse_ReadEFuse(struct adapter *Adapter, u8 efuseType, u16 _offset, u16 _size_byte, u8 *pbuf, bool pseudo)
233 Adapter->HalFunc.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, pseudo);
236 void EFUSE_GetEfuseDefinition(struct adapter *pAdapter, u8 efuseType, u8 type, void *pOut, bool pseudo
239 pAdapter->HalFunc.EFUSEGetEfuseDefinition(pAdapter, efuseType, type, pOut, pseudo);
242 /*-----------------------------------------------------------------------------
243 * Function: EFUSE_Read1Byte
245 * Overview: Copy from WMAC fot EFUSE read 1 byte.
255 * 09/23/2008 MHC Copy from WMAC.
257 *---------------------------------------------------------------------------*/
258 u8 EFUSE_Read1Byte(struct adapter *Adapter, u16 Address)
261 u8 Bytetemp = {0x00};
266 EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI , TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&contentLen, false);
268 if (Address < contentLen) { /* E-fuse 512Byte */
269 /* Write E-fuse Register address bit0~7 */
270 temp = Address & 0xFF;
271 rtw_write8(Adapter, EFUSE_CTRL+1, temp);
272 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);
273 /* Write E-fuse Register address bit8~9 */
274 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
275 rtw_write8(Adapter, EFUSE_CTRL+2, temp);
277 /* Write 0x30[31]= 0 */
278 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
279 temp = Bytetemp & 0x7F;
280 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
282 /* Wait Write-ready (0x30[31]= 1) */
283 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
284 while (!(Bytetemp & 0x80)) {
285 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
292 data = rtw_read8(Adapter, EFUSE_CTRL);
298 } /* EFUSE_Read1Byte */
300 /* 11/16/2008 MH Read one byte from real Efuse. */
301 u8 efuse_OneByteRead(struct adapter *pAdapter, u16 addr, u8 *data, bool pseudo)
307 result = Efuse_Read1ByteFromFakeContent(pAdapter, addr, data);
310 /* -----------------e-fuse reg ctrl --------------------------------- */
312 rtw_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr & 0xff));
313 rtw_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
314 (rtw_read8(pAdapter, EFUSE_CTRL+2) & 0xFC));
316 rtw_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
318 while (!(0x80 & rtw_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
321 *data = rtw_read8(pAdapter, EFUSE_CTRL);
330 /* 11/16/2008 MH Write one byte to reald Efuse. */
331 u8 efuse_OneByteWrite(struct adapter *pAdapter, u16 addr, u8 data, bool pseudo)
337 result = Efuse_Write1ByteToFakeContent(pAdapter, addr, data);
341 /* -----------------e-fuse reg ctrl --------------------------------- */
343 rtw_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
344 rtw_write8(pAdapter, EFUSE_CTRL+2,
345 (rtw_read8(pAdapter, EFUSE_CTRL+2) & 0xFC) |
346 (u8)((addr>>8) & 0x03));
347 rtw_write8(pAdapter, EFUSE_CTRL, data);/* data */
349 rtw_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
351 while ((0x80 & rtw_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx < 100))
362 int Efuse_PgPacketRead(struct adapter *pAdapter, u8 offset, u8 *data, bool pseudo)
366 ret = pAdapter->HalFunc.Efuse_PgPacketRead(pAdapter, offset, data, pseudo);
371 int Efuse_PgPacketWrite(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *data, bool pseudo)
375 ret = pAdapter->HalFunc.Efuse_PgPacketWrite(pAdapter, offset, word_en, data, pseudo);
381 static int Efuse_PgPacketWrite_BT(struct adapter *pAdapter, u8 offset, u8 word_en, u8 *data, bool pseudo)
385 ret = pAdapter->HalFunc.Efuse_PgPacketWrite_BT(pAdapter, offset, word_en, data, pseudo);
390 /*-----------------------------------------------------------------------------
391 * Function: efuse_WordEnableDataRead
393 * Overview: Read allowed word in current efuse section data.
403 * 11/16/2008 MHC Create Version 0.
404 * 11/21/2008 MHC Fix Write bug when we only enable late word.
406 *---------------------------------------------------------------------------*/
407 void efuse_WordEnableDataRead(u8 word_en, u8 *sourdata, u8 *targetdata)
409 if (!(word_en&BIT(0))) {
410 targetdata[0] = sourdata[0];
411 targetdata[1] = sourdata[1];
413 if (!(word_en&BIT(1))) {
414 targetdata[2] = sourdata[2];
415 targetdata[3] = sourdata[3];
417 if (!(word_en&BIT(2))) {
418 targetdata[4] = sourdata[4];
419 targetdata[5] = sourdata[5];
421 if (!(word_en&BIT(3))) {
422 targetdata[6] = sourdata[6];
423 targetdata[7] = sourdata[7];
427 u8 Efuse_WordEnableDataWrite(struct adapter *pAdapter, u16 efuse_addr, u8 word_en, u8 *data, bool pseudo)
431 ret = pAdapter->HalFunc.Efuse_WordEnableDataWrite(pAdapter, efuse_addr, word_en, data, pseudo);
436 static u8 efuse_read8(struct adapter *padapter, u16 address, u8 *value)
438 return efuse_OneByteRead(padapter, address, value, false);
441 static u8 efuse_write8(struct adapter *padapter, u16 address, u8 *value)
443 return efuse_OneByteWrite(padapter, address, *value, false);
447 * read/wirte raw efuse data
449 u8 rtw_efuse_access(struct adapter *padapter, u8 write, u16 start_addr, u16 cnts, u8 *data)
452 u16 real_content_len = 0, max_available_size = 0;
454 u8 (*rw8)(struct adapter *, u16, u8*);
456 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&real_content_len, false);
457 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_available_size, false);
459 if (start_addr > real_content_len)
463 if ((start_addr + cnts) > max_available_size)
470 Efuse_PowerSwitch(padapter, write, true);
472 /* e-fuse one byte read / write */
473 for (i = 0; i < cnts; i++) {
474 if (start_addr >= real_content_len) {
479 res = rw8(padapter, start_addr++, data++);
484 Efuse_PowerSwitch(padapter, write, false);
489 u16 efuse_GetMaxSize(struct adapter *padapter)
492 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (void *)&max_size, false);
496 u8 efuse_GetCurrentSize(struct adapter *padapter, u16 *size)
498 Efuse_PowerSwitch(padapter, false, true);
499 *size = Efuse_GetCurrentSize(padapter, EFUSE_WIFI, false);
500 Efuse_PowerSwitch(padapter, false, false);
505 u8 rtw_efuse_map_read(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
509 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
511 if ((addr + cnts) > mapLen)
514 Efuse_PowerSwitch(padapter, false, true);
516 efuse_ReadEFuse(padapter, EFUSE_WIFI, addr, cnts, data, false);
518 Efuse_PowerSwitch(padapter, false, false);
523 u8 rtw_BT_efuse_map_read(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
527 EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
529 if ((addr + cnts) > mapLen)
532 Efuse_PowerSwitch(padapter, false, true);
534 efuse_ReadEFuse(padapter, EFUSE_BT, addr, cnts, data, false);
536 Efuse_PowerSwitch(padapter, false, false);
541 u8 rtw_efuse_map_write(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
545 u8 newdata[PGPKT_DATA_SIZE + 1];
550 EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
552 if ((addr + cnts) > mapLen)
555 map = rtw_zmalloc(mapLen);
559 ret = rtw_efuse_map_read(padapter, 0, mapLen, map);
563 Efuse_PowerSwitch(padapter, true, true);
565 offset = (addr >> 3);
567 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE + 1);
568 i = addr & 0x7; /* index of one package */
569 idx = 0; /* data index */
573 if (data[idx] != map[addr+idx]) {
574 word_en &= ~BIT(i >> 1);
575 newdata[i-1] = map[addr+idx-1];
576 newdata[i] = data[idx];
582 for (; i < PGPKT_DATA_SIZE; i += 2) {
585 if ((cnts - idx) == 1) {
586 if (data[idx] != map[addr+idx]) {
587 word_en &= ~BIT(i >> 1);
588 newdata[i] = data[idx];
589 newdata[i+1] = map[addr+idx+1];
594 if ((data[idx] != map[addr+idx]) ||
595 (data[idx+1] != map[addr+idx+1])) {
596 word_en &= ~BIT(i >> 1);
597 newdata[i] = data[idx];
598 newdata[i+1] = data[idx + 1];
606 if (word_en != 0xF) {
607 ret = Efuse_PgPacketWrite(padapter, offset, word_en, newdata, false);
608 DBG_88E("offset=%x\n", offset);
609 DBG_88E("word_en=%x\n", word_en);
611 for (i = 0; i < PGPKT_DATA_SIZE; i++)
612 DBG_88E("data=%x \t", newdata[i]);
623 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE);
626 Efuse_PowerSwitch(padapter, true, false);
633 u8 rtw_BT_efuse_map_write(struct adapter *padapter, u16 addr, u16 cnts, u8 *data)
637 u8 newdata[PGPKT_DATA_SIZE + 1];
642 EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, false);
644 if ((addr + cnts) > mapLen)
647 map = rtw_zmalloc(mapLen);
651 ret = rtw_BT_efuse_map_read(padapter, 0, mapLen, map);
655 Efuse_PowerSwitch(padapter, true, true);
657 offset = (addr >> 3);
659 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE + 1);
660 i = addr & 0x7; /* index of one package */
661 idx = 0; /* data index */
665 if (data[idx] != map[addr+idx]) {
666 word_en &= ~BIT(i >> 1);
667 newdata[i-1] = map[addr+idx-1];
668 newdata[i] = data[idx];
674 for (; i < PGPKT_DATA_SIZE; i += 2) {
677 if ((cnts - idx) == 1) {
678 if (data[idx] != map[addr+idx]) {
679 word_en &= ~BIT(i >> 1);
680 newdata[i] = data[idx];
681 newdata[i+1] = map[addr+idx+1];
686 if ((data[idx] != map[addr+idx]) ||
687 (data[idx+1] != map[addr+idx+1])) {
688 word_en &= ~BIT(i >> 1);
689 newdata[i] = data[idx];
690 newdata[i+1] = data[idx + 1];
698 if (word_en != 0xF) {
699 DBG_88E("%s: offset=%#X\n", __func__, offset);
700 DBG_88E("%s: word_en=%#X\n", __func__, word_en);
701 DBG_88E("%s: data=", __func__);
702 for (i = 0; i < PGPKT_DATA_SIZE; i++)
703 DBG_88E("0x%02X ", newdata[i]);
706 ret = Efuse_PgPacketWrite_BT(padapter, offset, word_en, newdata, false);
717 _rtw_memset(newdata, 0xFF, PGPKT_DATA_SIZE);
720 Efuse_PowerSwitch(padapter, true, false);
729 /*-----------------------------------------------------------------------------
730 * Function: efuse_ShadowRead1Byte
731 * efuse_ShadowRead2Byte
732 * efuse_ShadowRead4Byte
734 * Overview: Read from efuse init map by one/two/four bytes !!!!!
744 * 11/12/2008 MHC Create Version 0.
746 *---------------------------------------------------------------------------*/
748 efuse_ShadowRead1Byte(
749 struct adapter *pAdapter,
753 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
755 *Value = pEEPROM->efuse_eeprom_data[Offset];
757 } /* EFUSE_ShadowRead1Byte */
761 efuse_ShadowRead2Byte(
762 struct adapter *pAdapter,
766 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
768 *Value = pEEPROM->efuse_eeprom_data[Offset];
769 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
771 } /* EFUSE_ShadowRead2Byte */
773 /* Read Four Bytes */
775 efuse_ShadowRead4Byte(
776 struct adapter *pAdapter,
780 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
782 *Value = pEEPROM->efuse_eeprom_data[Offset];
783 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
784 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
785 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
787 } /* efuse_ShadowRead4Byte */
789 /*-----------------------------------------------------------------------------
790 * Function: Efuse_ReadAllMap
792 * Overview: Read All Efuse content
802 * 11/11/2008 MHC Create Version 0.
804 *---------------------------------------------------------------------------*/
805 static void Efuse_ReadAllMap(struct adapter *pAdapter, u8 efuseType, u8 *Efuse, bool pseudo)
809 Efuse_PowerSwitch(pAdapter, false, true);
811 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, pseudo);
813 efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse, pseudo);
815 Efuse_PowerSwitch(pAdapter, false, false);
818 /*-----------------------------------------------------------------------------
819 * Function: EFUSE_ShadowMapUpdate
821 * Overview: Transfer current EFUSE content to shadow init and modify map.
831 * 11/13/2008 MHC Create Version 0.
833 *---------------------------------------------------------------------------*/
834 void EFUSE_ShadowMapUpdate(
835 struct adapter *pAdapter,
839 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
842 EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, pseudo);
844 if (pEEPROM->bautoload_fail_flag)
845 _rtw_memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
847 Efuse_ReadAllMap(pAdapter, efuseType, pEEPROM->efuse_eeprom_data, pseudo);
848 } /* EFUSE_ShadowMapUpdate */
850 /*-----------------------------------------------------------------------------
851 * Function: EFUSE_ShadowRead
853 * Overview: Read from efuse init map !!!!!
863 * 11/12/2008 MHC Create Version 0.
865 *---------------------------------------------------------------------------*/
866 void EFUSE_ShadowRead(struct adapter *pAdapter, u8 Type, u16 Offset, u32 *Value)
869 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
871 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
873 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
875 } /* EFUSE_ShadowRead */