1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
10 #include <rtw_debug.h>
12 #include <linux/jiffies.h>
15 /* Define global variables */
17 u32 fakeEfuseUsedBytes;
18 u8 fakeEfuseContent[EFUSE_MAX_HW_SIZE] = {0};
19 u8 fakeEfuseInitMap[EFUSE_MAX_MAP_LEN] = {0};
20 u8 fakeEfuseModifiedMap[EFUSE_MAX_MAP_LEN] = {0};
23 u8 BTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
24 u8 BTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
25 u8 BTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
27 u32 fakeBTEfuseUsedBytes;
28 u8 fakeBTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
29 u8 fakeBTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
30 u8 fakeBTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
32 #define REG_EFUSE_CTRL 0x0030
33 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
36 Efuse_Read1ByteFromFakeContent(
37 struct adapter *padapter,
41 Efuse_Read1ByteFromFakeContent(
42 struct adapter *padapter,
46 if (Offset >= EFUSE_MAX_HW_SIZE) {
49 /* DbgPrint("Read fake content, offset = %d\n", Offset); */
50 if (fakeEfuseBank == 0)
51 *Value = fakeEfuseContent[Offset];
53 *Value = fakeBTEfuseContent[fakeEfuseBank-1][Offset];
58 Efuse_Write1ByteToFakeContent(
59 struct adapter *padapter,
63 Efuse_Write1ByteToFakeContent(
64 struct adapter *padapter,
68 if (Offset >= EFUSE_MAX_HW_SIZE) {
71 if (fakeEfuseBank == 0)
72 fakeEfuseContent[Offset] = Value;
74 fakeBTEfuseContent[fakeEfuseBank-1][Offset] = Value;
79 /*-----------------------------------------------------------------------------
80 * Function: Efuse_PowerSwitch
82 * Overview: When we want to enable write operation, we should change to
83 * pwr on state. When we stop write, we should switch to 500k mode
84 * and disable LDO 2.5V.
94 * 11/17/2008 MHC Create Version 0.
96 *---------------------------------------------------------------------------*/
99 struct adapter *padapter,
103 padapter->HalFunc.EfusePowerSwitch(padapter, bWrite, PwrState);
106 /*-----------------------------------------------------------------------------
107 * Function: Efuse_GetCurrentSize
109 * Overview: Get current efuse size!!!
119 * 11/16/2008 MHC Create Version 0.
121 *---------------------------------------------------------------------------*/
123 Efuse_GetCurrentSize(
124 struct adapter *padapter,
130 ret = padapter->HalFunc.EfuseGetCurrentSize(padapter, efuseType, bPseudoTest);
135 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
137 Efuse_CalculateWordCnts(u8 word_en)
140 if (!(word_en & BIT(0)))
141 word_cnts++; /* 0 : write enable */
142 if (!(word_en & BIT(1)))
144 if (!(word_en & BIT(2)))
146 if (!(word_en & BIT(3)))
153 /* 1. Execute E-Fuse read byte operation according as map offset and */
154 /* save to E-Fuse table. */
155 /* 2. Referred from SD1 Richard. */
158 /* 1. Boot from E-Fuse and successfully auto-load. */
159 /* 2. PASSIVE_LEVEL (USB interface) */
161 /* Created by Roger, 2008.10.21. */
163 /* 2008/12/12 MH 1. Reorganize code flow and reserve bytes. and add description. */
164 /* 2. Add efuse utilization collect. */
165 /* 2008/12/22 MH Read Efuse must check if we write section 1 data again!!! Sec1 */
166 /* write addr must be after sec5. */
171 struct adapter *Adapter,
180 struct adapter *Adapter,
188 Adapter->HalFunc.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, bPseudoTest);
192 EFUSE_GetEfuseDefinition(
193 struct adapter *padapter,
200 padapter->HalFunc.EFUSEGetEfuseDefinition(padapter, efuseType, type, pOut, bPseudoTest);
203 /*-----------------------------------------------------------------------------
204 * Function: EFUSE_Read1Byte
206 * Overview: Copy from WMAC fot EFUSE read 1 byte.
216 * 09/23/2008 MHC Copy from WMAC.
218 *---------------------------------------------------------------------------*/
221 struct adapter *Adapter,
225 u8 Bytetemp = {0x00};
230 EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&contentLen, false);
232 if (Address < contentLen) {/* E-fuse 512Byte */
233 /* Write E-fuse Register address bit0~7 */
234 temp = Address & 0xFF;
235 rtw_write8(Adapter, EFUSE_CTRL+1, temp);
236 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);
237 /* Write E-fuse Register address bit8~9 */
238 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
239 rtw_write8(Adapter, EFUSE_CTRL+2, temp);
241 /* Write 0x30[31]= 0 */
242 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
243 temp = Bytetemp & 0x7F;
244 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
246 /* Wait Write-ready (0x30[31]= 1) */
247 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
248 while (!(Bytetemp & 0x80)) {
249 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
256 data = rtw_read8(Adapter, EFUSE_CTRL);
261 } /* EFUSE_Read1Byte */
263 /* 11/16/2008 MH Read one byte from real Efuse. */
266 struct adapter *padapter,
275 /* DBG_871X("===> EFUSE_OneByteRead(), addr = %x\n", addr); */
276 /* DBG_871X("===> EFUSE_OneByteRead() start, 0x34 = 0x%X\n", rtw_read32(padapter, EFUSE_TEST)); */
279 bResult = Efuse_Read1ByteFromFakeContent(padapter, addr, data);
283 /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
284 /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
285 /* PHY_SetMacReg(padapter, 0x34, BIT11, 0); */
286 rtw_write16(padapter, 0x34, rtw_read16(padapter, 0x34) & (~BIT11));
288 /* -----------------e-fuse reg ctrl --------------------------------- */
290 rtw_write8(padapter, EFUSE_CTRL+1, (u8)(addr&0xff));
291 rtw_write8(padapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
292 (rtw_read8(padapter, EFUSE_CTRL+2)&0xFC));
294 /* rtw_write8(padapter, EFUSE_CTRL+3, 0x72); read cmd */
296 readbyte = rtw_read8(padapter, EFUSE_CTRL+3);
297 rtw_write8(padapter, EFUSE_CTRL+3, (readbyte & 0x7f));
299 while (!(0x80 & rtw_read8(padapter, EFUSE_CTRL+3)) && (tmpidx < 1000)) {
304 *data = rtw_read8(padapter, EFUSE_CTRL);
309 DBG_871X("%s: [ERROR] addr = 0x%x bResult =%d time out 1s !!!\n", __func__, addr, bResult);
310 DBG_871X("%s: [ERROR] EFUSE_CTRL = 0x%08x !!!\n", __func__, rtw_read32(padapter, EFUSE_CTRL));
316 /* 11/16/2008 MH Write one byte to reald Efuse. */
319 struct adapter *padapter,
328 /* DBG_871X("===> EFUSE_OneByteWrite(), addr = %x data =%x\n", addr, data); */
329 /* DBG_871X("===> EFUSE_OneByteWrite() start, 0x34 = 0x%X\n", rtw_read32(padapter, EFUSE_TEST)); */
332 bResult = Efuse_Write1ByteToFakeContent(padapter, addr, data);
337 /* -----------------e-fuse reg ctrl --------------------------------- */
341 efuseValue = rtw_read32(padapter, EFUSE_CTRL);
342 efuseValue |= (BIT21|BIT31);
343 efuseValue &= ~(0x3FFFF);
344 efuseValue |= ((addr<<8 | data) & 0x3FFFF);
347 /* <20130227, Kordan> 8192E MP chip A-cut had better not set 0x34[11] until B-Cut. */
349 /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
350 /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
351 /* PHY_SetMacReg(padapter, 0x34, BIT11, 1); */
352 rtw_write16(padapter, 0x34, rtw_read16(padapter, 0x34) | (BIT11));
353 rtw_write32(padapter, EFUSE_CTRL, 0x90600000|((addr<<8 | data)));
355 while ((0x80 & rtw_read8(padapter, EFUSE_CTRL+3)) && (tmpidx < 100)) {
364 DBG_871X("%s: [ERROR] addr = 0x%x , efuseValue = 0x%x , bResult =%d time out 1s !!!\n",
365 __func__, addr, efuseValue, bResult);
366 DBG_871X("%s: [ERROR] EFUSE_CTRL = 0x%08x !!!\n", __func__, rtw_read32(padapter, EFUSE_CTRL));
369 /* disable Efuse program enable */
370 PHY_SetMacReg(padapter, EFUSE_TEST, BIT(11), 0);
376 Efuse_PgPacketRead(struct adapter *padapter,
383 ret = padapter->HalFunc.Efuse_PgPacketRead(padapter, offset, data, bPseudoTest);
389 Efuse_PgPacketWrite(struct adapter *padapter,
397 ret = padapter->HalFunc.Efuse_PgPacketWrite(padapter, offset, word_en, data, bPseudoTest);
402 /*-----------------------------------------------------------------------------
403 * Function: efuse_WordEnableDataRead
405 * Overview: Read allowed word in current efuse section data.
415 * 11/16/2008 MHC Create Version 0.
416 * 11/21/2008 MHC Fix Write bug when we only enable late word.
418 *---------------------------------------------------------------------------*/
420 efuse_WordEnableDataRead(u8 word_en,
424 if (!(word_en&BIT(0))) {
425 targetdata[0] = sourdata[0];
426 targetdata[1] = sourdata[1];
428 if (!(word_en&BIT(1))) {
429 targetdata[2] = sourdata[2];
430 targetdata[3] = sourdata[3];
432 if (!(word_en&BIT(2))) {
433 targetdata[4] = sourdata[4];
434 targetdata[5] = sourdata[5];
436 if (!(word_en&BIT(3))) {
437 targetdata[6] = sourdata[6];
438 targetdata[7] = sourdata[7];
444 Efuse_WordEnableDataWrite(struct adapter *padapter,
452 ret = padapter->HalFunc.Efuse_WordEnableDataWrite(padapter, efuse_addr, word_en, data, bPseudoTest);
457 /*-----------------------------------------------------------------------------
458 * Function: Efuse_ReadAllMap
460 * Overview: Read All Efuse content
470 * 11/11/2008 MHC Create Version 0.
472 *---------------------------------------------------------------------------*/
475 struct adapter *padapter,
481 struct adapter *padapter,
488 Efuse_PowerSwitch(padapter, false, true);
490 EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, bPseudoTest);
492 efuse_ReadEFuse(padapter, efuseType, 0, mapLen, Efuse, bPseudoTest);
494 Efuse_PowerSwitch(padapter, false, false);
497 /*-----------------------------------------------------------------------------
498 * Function: efuse_ShadowRead1Byte
499 * efuse_ShadowRead2Byte
500 * efuse_ShadowRead4Byte
502 * Overview: Read from efuse init map by one/two/four bytes !!!!!
512 * 11/12/2008 MHC Create Version 0.
514 *---------------------------------------------------------------------------*/
516 efuse_ShadowRead1Byte(
517 struct adapter *padapter,
521 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
523 *Value = pEEPROM->efuse_eeprom_data[Offset];
525 } /* EFUSE_ShadowRead1Byte */
529 efuse_ShadowRead2Byte(
530 struct adapter *padapter,
534 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
536 *Value = pEEPROM->efuse_eeprom_data[Offset];
537 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
539 } /* EFUSE_ShadowRead2Byte */
541 /* Read Four Bytes */
543 efuse_ShadowRead4Byte(
544 struct adapter *padapter,
548 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
550 *Value = pEEPROM->efuse_eeprom_data[Offset];
551 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
552 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
553 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
555 } /* efuse_ShadowRead4Byte */
557 /*-----------------------------------------------------------------------------
558 * Function: EFUSE_ShadowMapUpdate
560 * Overview: Transfer current EFUSE content to shadow init and modify map.
570 * 11/13/2008 MHC Create Version 0.
572 *---------------------------------------------------------------------------*/
573 void EFUSE_ShadowMapUpdate(
574 struct adapter *padapter,
578 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
581 EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, bPseudoTest);
583 if (pEEPROM->bautoload_fail_flag)
584 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
586 Efuse_ReadAllMap(padapter, efuseType, pEEPROM->efuse_eeprom_data, bPseudoTest);
588 /* PlatformMoveMemory((void *)&pHalData->EfuseMap[EFUSE_MODIFY_MAP][0], */
589 /* void *)&pHalData->EfuseMap[EFUSE_INIT_MAP][0], mapLen); */
590 } /* EFUSE_ShadowMapUpdate */
593 /*-----------------------------------------------------------------------------
594 * Function: EFUSE_ShadowRead
596 * Overview: Read from efuse init map !!!!!
606 * 11/12/2008 MHC Create Version 0.
608 *---------------------------------------------------------------------------*/
611 struct adapter *padapter,
617 efuse_ShadowRead1Byte(padapter, Offset, (u8 *)Value);
619 efuse_ShadowRead2Byte(padapter, Offset, (u16 *)Value);
621 efuse_ShadowRead4Byte(padapter, Offset, (u32 *)Value);
623 } /* EFUSE_ShadowRead*/