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,
128 return padapter->HalFunc.EfuseGetCurrentSize(padapter, efuseType,
132 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
134 Efuse_CalculateWordCnts(u8 word_en)
137 if (!(word_en & BIT(0)))
138 word_cnts++; /* 0 : write enable */
139 if (!(word_en & BIT(1)))
141 if (!(word_en & BIT(2)))
143 if (!(word_en & BIT(3)))
150 /* 1. Execute E-Fuse read byte operation according as map offset and */
151 /* save to E-Fuse table. */
152 /* 2. Referred from SD1 Richard. */
155 /* 1. Boot from E-Fuse and successfully auto-load. */
156 /* 2. PASSIVE_LEVEL (USB interface) */
158 /* Created by Roger, 2008.10.21. */
160 /* 2008/12/12 MH 1. Reorganize code flow and reserve bytes. and add description. */
161 /* 2. Add efuse utilization collect. */
162 /* 2008/12/22 MH Read Efuse must check if we write section 1 data again!!! Sec1 */
163 /* write addr must be after sec5. */
168 struct adapter *Adapter,
177 struct adapter *Adapter,
185 Adapter->HalFunc.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, bPseudoTest);
189 EFUSE_GetEfuseDefinition(
190 struct adapter *padapter,
197 padapter->HalFunc.EFUSEGetEfuseDefinition(padapter, efuseType, type, pOut, bPseudoTest);
200 /*-----------------------------------------------------------------------------
201 * Function: EFUSE_Read1Byte
203 * Overview: Copy from WMAC fot EFUSE read 1 byte.
213 * 09/23/2008 MHC Copy from WMAC.
215 *---------------------------------------------------------------------------*/
218 struct adapter *Adapter,
221 u8 Bytetemp = {0x00};
226 EFUSE_GetEfuseDefinition(Adapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (void *)&contentLen, false);
228 if (Address < contentLen) {/* E-fuse 512Byte */
229 /* Write E-fuse Register address bit0~7 */
230 temp = Address & 0xFF;
231 rtw_write8(Adapter, EFUSE_CTRL+1, temp);
232 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+2);
233 /* Write E-fuse Register address bit8~9 */
234 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
235 rtw_write8(Adapter, EFUSE_CTRL+2, temp);
237 /* Write 0x30[31]= 0 */
238 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
239 temp = Bytetemp & 0x7F;
240 rtw_write8(Adapter, EFUSE_CTRL+3, temp);
242 /* Wait Write-ready (0x30[31]= 1) */
243 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
244 while (!(Bytetemp & 0x80)) {
245 Bytetemp = rtw_read8(Adapter, EFUSE_CTRL+3);
252 return rtw_read8(Adapter, EFUSE_CTRL);
256 } /* EFUSE_Read1Byte */
258 /* 11/16/2008 MH Read one byte from real Efuse. */
261 struct adapter *padapter,
270 /* DBG_871X("===> EFUSE_OneByteRead(), addr = %x\n", addr); */
271 /* DBG_871X("===> EFUSE_OneByteRead() start, 0x34 = 0x%X\n", rtw_read32(padapter, EFUSE_TEST)); */
274 bResult = Efuse_Read1ByteFromFakeContent(padapter, addr, data);
278 /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
279 /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
280 /* PHY_SetMacReg(padapter, 0x34, BIT11, 0); */
281 rtw_write16(padapter, 0x34, rtw_read16(padapter, 0x34) & (~BIT11));
283 /* -----------------e-fuse reg ctrl --------------------------------- */
285 rtw_write8(padapter, EFUSE_CTRL+1, (u8)(addr&0xff));
286 rtw_write8(padapter, EFUSE_CTRL+2, ((u8)((addr>>8) & 0x03)) |
287 (rtw_read8(padapter, EFUSE_CTRL+2)&0xFC));
289 /* rtw_write8(padapter, EFUSE_CTRL+3, 0x72); read cmd */
291 readbyte = rtw_read8(padapter, EFUSE_CTRL+3);
292 rtw_write8(padapter, EFUSE_CTRL+3, (readbyte & 0x7f));
294 while (!(0x80 & rtw_read8(padapter, EFUSE_CTRL+3)) && (tmpidx < 1000)) {
299 *data = rtw_read8(padapter, EFUSE_CTRL);
304 DBG_871X("%s: [ERROR] addr = 0x%x bResult =%d time out 1s !!!\n", __func__, addr, bResult);
305 DBG_871X("%s: [ERROR] EFUSE_CTRL = 0x%08x !!!\n", __func__, rtw_read32(padapter, EFUSE_CTRL));
311 /* 11/16/2008 MH Write one byte to reald Efuse. */
314 struct adapter *padapter,
323 /* DBG_871X("===> EFUSE_OneByteWrite(), addr = %x data =%x\n", addr, data); */
324 /* DBG_871X("===> EFUSE_OneByteWrite() start, 0x34 = 0x%X\n", rtw_read32(padapter, EFUSE_TEST)); */
327 bResult = Efuse_Write1ByteToFakeContent(padapter, addr, data);
332 /* -----------------e-fuse reg ctrl --------------------------------- */
336 efuseValue = rtw_read32(padapter, EFUSE_CTRL);
337 efuseValue |= (BIT21|BIT31);
338 efuseValue &= ~(0x3FFFF);
339 efuseValue |= ((addr<<8 | data) & 0x3FFFF);
342 /* <20130227, Kordan> 8192E MP chip A-cut had better not set 0x34[11] until B-Cut. */
344 /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
345 /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
346 /* PHY_SetMacReg(padapter, 0x34, BIT11, 1); */
347 rtw_write16(padapter, 0x34, rtw_read16(padapter, 0x34) | (BIT11));
348 rtw_write32(padapter, EFUSE_CTRL, 0x90600000|((addr<<8 | data)));
350 while ((0x80 & rtw_read8(padapter, EFUSE_CTRL+3)) && (tmpidx < 100)) {
359 DBG_871X("%s: [ERROR] addr = 0x%x , efuseValue = 0x%x , bResult =%d time out 1s !!!\n",
360 __func__, addr, efuseValue, bResult);
361 DBG_871X("%s: [ERROR] EFUSE_CTRL = 0x%08x !!!\n", __func__, rtw_read32(padapter, EFUSE_CTRL));
364 /* disable Efuse program enable */
365 PHY_SetMacReg(padapter, EFUSE_TEST, BIT(11), 0);
371 Efuse_PgPacketRead(struct adapter *padapter,
376 return padapter->HalFunc.Efuse_PgPacketRead(padapter, offset, data,
381 Efuse_PgPacketWrite(struct adapter *padapter,
387 return padapter->HalFunc.Efuse_PgPacketWrite(padapter, offset, word_en,
391 /*-----------------------------------------------------------------------------
392 * Function: efuse_WordEnableDataRead
394 * Overview: Read allowed word in current efuse section data.
404 * 11/16/2008 MHC Create Version 0.
405 * 11/21/2008 MHC Fix Write bug when we only enable late word.
407 *---------------------------------------------------------------------------*/
409 efuse_WordEnableDataRead(u8 word_en,
413 if (!(word_en&BIT(0))) {
414 targetdata[0] = sourdata[0];
415 targetdata[1] = sourdata[1];
417 if (!(word_en&BIT(1))) {
418 targetdata[2] = sourdata[2];
419 targetdata[3] = sourdata[3];
421 if (!(word_en&BIT(2))) {
422 targetdata[4] = sourdata[4];
423 targetdata[5] = sourdata[5];
425 if (!(word_en&BIT(3))) {
426 targetdata[6] = sourdata[6];
427 targetdata[7] = sourdata[7];
433 Efuse_WordEnableDataWrite(struct adapter *padapter,
439 return padapter->HalFunc.Efuse_WordEnableDataWrite(padapter, efuse_addr,
444 /*-----------------------------------------------------------------------------
445 * Function: Efuse_ReadAllMap
447 * Overview: Read All Efuse content
457 * 11/11/2008 MHC Create Version 0.
459 *---------------------------------------------------------------------------*/
462 struct adapter *padapter,
468 struct adapter *padapter,
475 Efuse_PowerSwitch(padapter, false, true);
477 EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, bPseudoTest);
479 efuse_ReadEFuse(padapter, efuseType, 0, mapLen, Efuse, bPseudoTest);
481 Efuse_PowerSwitch(padapter, false, false);
484 /*-----------------------------------------------------------------------------
485 * Function: efuse_ShadowRead1Byte
486 * efuse_ShadowRead2Byte
487 * efuse_ShadowRead4Byte
489 * Overview: Read from efuse init map by one/two/four bytes !!!!!
499 * 11/12/2008 MHC Create Version 0.
501 *---------------------------------------------------------------------------*/
503 efuse_ShadowRead1Byte(
504 struct adapter *padapter,
508 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
510 *Value = pEEPROM->efuse_eeprom_data[Offset];
512 } /* EFUSE_ShadowRead1Byte */
516 efuse_ShadowRead2Byte(
517 struct adapter *padapter,
521 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
523 *Value = pEEPROM->efuse_eeprom_data[Offset];
524 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
526 } /* EFUSE_ShadowRead2Byte */
528 /* Read Four Bytes */
530 efuse_ShadowRead4Byte(
531 struct adapter *padapter,
535 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
537 *Value = pEEPROM->efuse_eeprom_data[Offset];
538 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
539 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
540 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
542 } /* efuse_ShadowRead4Byte */
544 /*-----------------------------------------------------------------------------
545 * Function: EFUSE_ShadowMapUpdate
547 * Overview: Transfer current EFUSE content to shadow init and modify map.
557 * 11/13/2008 MHC Create Version 0.
559 *---------------------------------------------------------------------------*/
560 void EFUSE_ShadowMapUpdate(
561 struct adapter *padapter,
565 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
568 EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_EFUSE_MAP_LEN, (void *)&mapLen, bPseudoTest);
570 if (pEEPROM->bautoload_fail_flag)
571 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
573 Efuse_ReadAllMap(padapter, efuseType, pEEPROM->efuse_eeprom_data, bPseudoTest);
575 /* PlatformMoveMemory((void *)&pHalData->EfuseMap[EFUSE_MODIFY_MAP][0], */
576 /* void *)&pHalData->EfuseMap[EFUSE_INIT_MAP][0], mapLen); */
577 } /* EFUSE_ShadowMapUpdate */
580 /*-----------------------------------------------------------------------------
581 * Function: EFUSE_ShadowRead
583 * Overview: Read from efuse init map !!!!!
593 * 11/12/2008 MHC Create Version 0.
595 *---------------------------------------------------------------------------*/
598 struct adapter *padapter,
604 efuse_ShadowRead1Byte(padapter, Offset, (u8 *)Value);
606 efuse_ShadowRead2Byte(padapter, Offset, (u16 *)Value);
608 efuse_ShadowRead4Byte(padapter, Offset, (u32 *)Value);
610 } /* EFUSE_ShadowRead*/