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 ******************************************************************************/
17 #include <osdep_service.h>
18 #include <drv_types.h>
20 #include <rtw_efuse.h>
21 #include <rtl8723a_hal.h>
22 #include <usb_ops_linux.h>
24 /*------------------------Define local variable------------------------------*/
27 #define REG_EFUSE_CTRL 0x0030
28 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control. */
31 #define VOLTAGE_V25 0x03
32 #define LDOE25_SHIFT 28
34 /*-----------------------------------------------------------------------------
35 * Function: Efuse_PowerSwitch
37 * Overview: When we want to enable write operation, we should change to
38 * pwr on state. When we stop write, we should switch to 500k mode
39 * and disable LDO 2.5V.
49 * 11/17/2008 MHC Create Version 0.
51 *---------------------------------------------------------------------------*/
52 static void Efuse_PowerSwitch(struct rtw_adapter *padapter,
53 u8 bWrite, u8 PwrState)
58 if (PwrState == true) {
59 rtl8723au_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON);
61 /* 1.2V Power: From VDDON with Power
62 Cut(0x0000h[15]), default valid */
63 tmpV16 = rtl8723au_read16(padapter, REG_SYS_ISO_CTRL);
64 if (!(tmpV16 & PWC_EV12V)) {
66 rtl8723au_write16(padapter, REG_SYS_ISO_CTRL, tmpV16);
68 /* Reset: 0x0000h[28], default valid */
69 tmpV16 = rtl8723au_read16(padapter, REG_SYS_FUNC_EN);
70 if (!(tmpV16 & FEN_ELDR)) {
72 rtl8723au_write16(padapter, REG_SYS_FUNC_EN, tmpV16);
75 /* Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock
76 from ANA, default valid */
77 tmpV16 = rtl8723au_read16(padapter, REG_SYS_CLKR);
78 if ((!(tmpV16 & LOADER_CLK_EN)) || (!(tmpV16 & ANA8M))) {
79 tmpV16 |= (LOADER_CLK_EN | ANA8M);
80 rtl8723au_write16(padapter, REG_SYS_CLKR, tmpV16);
84 /* Enable LDO 2.5V before read/write action */
85 tempval = rtl8723au_read8(padapter, EFUSE_TEST + 3);
87 tempval |= (VOLTAGE_V25 << 4);
88 rtl8723au_write8(padapter, EFUSE_TEST + 3,
92 rtl8723au_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
95 /* Disable LDO 2.5V after read/write action */
96 tempval = rtl8723au_read8(padapter, EFUSE_TEST + 3);
97 rtl8723au_write8(padapter, EFUSE_TEST + 3,
104 Efuse_GetCurrentSize23a(struct rtw_adapter *pAdapter, u8 efuseType)
108 if (efuseType == EFUSE_WIFI)
109 ret = rtl8723a_EfuseGetCurrentSize_WiFi(pAdapter);
111 ret = rtl8723a_EfuseGetCurrentSize_BT(pAdapter);
116 /* 11/16/2008 MH Add description. Get current efuse area enabled word!!. */
118 Efuse_CalculateWordCnts23a(u8 word_en)
121 if (!(word_en & BIT(0))) word_cnts++; /* 0 : write enable */
122 if (!(word_en & BIT(1))) word_cnts++;
123 if (!(word_en & BIT(2))) word_cnts++;
124 if (!(word_en & BIT(3))) word_cnts++;
130 /* Execute E-Fuse read byte operation. */
131 /* Referred from SD1 Richard. */
134 /* 1. Boot from E-Fuse and successfully auto-load. */
135 /* 2. PASSIVE_LEVEL (USB interface) */
137 /* Created by Roger, 2008.10.21. */
140 ReadEFuseByte23a(struct rtw_adapter *Adapter, u16 _offset, u8 *pbuf)
147 rtl8723au_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
148 readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
149 rtl8723au_write8(Adapter, EFUSE_CTRL+2,
150 ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
153 readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
154 rtl8723au_write8(Adapter, EFUSE_CTRL+3, readbyte & 0x7f);
156 /* Check bit 32 read-ready */
158 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
159 while (!((value32 >> 24) & 0x80) && retry < 10000) {
160 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
164 /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
165 /* This fix the problem that Efuse read error in high temperature condition. */
166 /* Designer says that there shall be some delay after ready bit is set, or the */
167 /* result will always stay on last data we read. */
169 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
171 *pbuf = (u8)(value32 & 0xff);
175 EFUSE_GetEfuseDefinition23a(struct rtw_adapter *pAdapter, u8 efuseType,
183 case TYPE_EFUSE_MAX_SECTION:
184 pMax_section = (u8 *) pOut;
186 if (efuseType == EFUSE_WIFI)
187 *pMax_section = EFUSE_MAX_SECTION_8723A;
189 *pMax_section = EFUSE_BT_MAX_SECTION;
192 case TYPE_EFUSE_REAL_CONTENT_LEN:
193 pu2Tmp = (u16 *) pOut;
195 if (efuseType == EFUSE_WIFI)
196 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
198 *pu2Tmp = EFUSE_BT_REAL_CONTENT_LEN;
201 case TYPE_AVAILABLE_EFUSE_BYTES_BANK:
202 pu2Tmp = (u16 *) pOut;
204 if (efuseType == EFUSE_WIFI)
205 *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
206 EFUSE_OOB_PROTECT_BYTES);
208 *pu2Tmp = (EFUSE_BT_REAL_BANK_CONTENT_LEN -
209 EFUSE_PROTECT_BYTES_BANK);
212 case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL:
213 pu2Tmp = (u16 *) pOut;
215 if (efuseType == EFUSE_WIFI)
216 *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
217 EFUSE_OOB_PROTECT_BYTES);
219 *pu2Tmp = (EFUSE_BT_REAL_CONTENT_LEN -
220 (EFUSE_PROTECT_BYTES_BANK * 3));
223 case TYPE_EFUSE_MAP_LEN:
224 pu2Tmp = (u16 *) pOut;
226 if (efuseType == EFUSE_WIFI)
227 *pu2Tmp = EFUSE_MAP_LEN_8723A;
229 *pu2Tmp = EFUSE_BT_MAP_LEN;
232 case TYPE_EFUSE_PROTECT_BYTES_BANK:
233 pu1Tmp = (u8 *) pOut;
235 if (efuseType == EFUSE_WIFI)
236 *pu1Tmp = EFUSE_OOB_PROTECT_BYTES;
238 *pu1Tmp = EFUSE_PROTECT_BYTES_BANK;
241 case TYPE_EFUSE_CONTENT_LEN_BANK:
242 pu2Tmp = (u16 *) pOut;
244 if (efuseType == EFUSE_WIFI)
245 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
247 *pu2Tmp = EFUSE_BT_REAL_BANK_CONTENT_LEN;
251 pu1Tmp = (u8 *) pOut;
257 /*-----------------------------------------------------------------------------
258 * Function: EFUSE_Read1Byte23a
260 * Overview: Copy from WMAC fot EFUSE read 1 byte.
270 * 09/23/2008 MHC Copy from WMAC.
272 *---------------------------------------------------------------------------*/
274 EFUSE_Read1Byte23a(struct rtw_adapter *Adapter, u16 Address)
277 u8 Bytetemp = {0x00};
282 EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
283 TYPE_EFUSE_REAL_CONTENT_LEN,
284 (void *)&contentLen);
286 if (Address < contentLen) { /* E-fuse 512Byte */
287 /* Write E-fuse Register address bit0~7 */
288 temp = Address & 0xFF;
289 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
290 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
291 /* Write E-fuse Register address bit8~9 */
292 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
293 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
295 /* Write 0x30[31]= 0 */
296 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
297 temp = Bytetemp & 0x7F;
298 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
300 /* Wait Write-ready (0x30[31]= 1) */
301 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
302 while (!(Bytetemp & 0x80)) {
303 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
310 data = rtl8723au_read8(Adapter, EFUSE_CTRL);
315 }/* EFUSE_Read1Byte23a */
317 /*-----------------------------------------------------------------------------
318 * Function: EFUSE_Write1Byte
320 * Overview: Copy from WMAC fot EFUSE write 1 byte.
330 * 09/23/2008 MHC Copy from WMAC.
332 *---------------------------------------------------------------------------*/
336 struct rtw_adapter * Adapter,
341 struct rtw_adapter * Adapter,
345 u8 Bytetemp = {0x00};
350 /* RT_TRACE(COMP_EFUSE, DBG_LOUD, ("Addr =%x Data =%x\n", Address, Value)); */
351 EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
352 TYPE_EFUSE_REAL_CONTENT_LEN,
353 (void *)&contentLen);
355 if (Address < contentLen) { /* E-fuse 512Byte */
356 rtl8723au_write8(Adapter, EFUSE_CTRL, Value);
358 /* Write E-fuse Register address bit0~7 */
359 temp = Address & 0xFF;
360 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
361 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
363 /* Write E-fuse Register address bit8~9 */
364 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
365 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
367 /* Write 0x30[31]= 1 */
368 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
369 temp = Bytetemp | 0x80;
370 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
372 /* Wait Write-ready (0x30[31]= 0) */
373 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
374 while (Bytetemp & 0x80) {
375 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
383 }/* EFUSE_Write1Byte */
385 /* 11/16/2008 MH Read one byte from real Efuse. */
387 efuse_OneByteRead23a(struct rtw_adapter *pAdapter, u16 addr, u8 *data)
392 /* -----------------e-fuse reg ctrl --------------------------------- */
394 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
395 rtl8723au_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) &0x03)) |
396 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC));
398 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
400 while(!(0x80 &rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx<100))
403 *data = rtl8723au_read8(pAdapter, EFUSE_CTRL);
412 /* 11/16/2008 MH Write one byte to reald Efuse. */
414 efuse_OneByteWrite23a(struct rtw_adapter *pAdapter, u16 addr, u8 data)
419 /* RT_TRACE(COMP_EFUSE, DBG_LOUD, ("Addr = %x Data =%x\n", addr, data)); */
423 /* -----------------e-fuse reg ctrl --------------------------------- */
425 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
426 rtl8723au_write8(pAdapter, EFUSE_CTRL+2,
427 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC)|(u8)((addr>>8)&0x03));
428 rtl8723au_write8(pAdapter, EFUSE_CTRL, data);/* data */
430 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
432 while((0x80 & rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) &&
445 /*-----------------------------------------------------------------------------
446 * Function: efuse_WordEnableDataRead23a
448 * Overview: Read allowed word in current efuse section data.
458 * 11/16/2008 MHC Create Version 0.
459 * 11/21/2008 MHC Fix Write bug when we only enable late word.
461 *---------------------------------------------------------------------------*/
463 efuse_WordEnableDataRead23a(u8 word_en,
467 if (!(word_en&BIT(0))) {
468 targetdata[0] = sourdata[0];
469 targetdata[1] = sourdata[1];
471 if (!(word_en&BIT(1))) {
472 targetdata[2] = sourdata[2];
473 targetdata[3] = sourdata[3];
475 if (!(word_en&BIT(2))) {
476 targetdata[4] = sourdata[4];
477 targetdata[5] = sourdata[5];
479 if (!(word_en&BIT(3))) {
480 targetdata[6] = sourdata[6];
481 targetdata[7] = sourdata[7];
485 static int efuse_read8(struct rtw_adapter *padapter, u16 address, u8 *value)
487 return efuse_OneByteRead23a(padapter, address, value);
490 static int efuse_write8(struct rtw_adapter *padapter, u16 address, u8 *value)
492 return efuse_OneByteWrite23a(padapter, address, *value);
496 * read/write raw efuse data
498 int rtw_efuse_access23a(struct rtw_adapter *padapter, u8 bWrite, u16 start_addr,
502 u16 real_content_len = 0, max_available_size = 0;
504 int (*rw8)(struct rtw_adapter *, u16, u8*);
506 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
507 TYPE_EFUSE_REAL_CONTENT_LEN,
508 (void *)&real_content_len);
509 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
510 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
511 (void *)&max_available_size);
513 if (start_addr > real_content_len)
516 if (true == bWrite) {
517 if ((start_addr + cnts) > max_available_size)
523 Efuse_PowerSwitch(padapter, bWrite, true);
525 /* e-fuse one byte read / write */
526 for (i = 0; i < cnts; i++) {
527 if (start_addr >= real_content_len) {
532 res = rw8(padapter, start_addr++, data++);
537 Efuse_PowerSwitch(padapter, bWrite, false);
542 u16 efuse_GetMaxSize23a(struct rtw_adapter *padapter)
545 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
546 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
551 int rtw_efuse_map_read23a(struct rtw_adapter *padapter,
552 u16 addr, u16 cnts, u8 *data)
556 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
557 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
559 if ((addr + cnts) > mapLen)
562 Efuse_PowerSwitch(padapter, false, true);
564 rtl8723a_readefuse(padapter, EFUSE_WIFI, addr, cnts, data);
566 Efuse_PowerSwitch(padapter, false, false);
571 int rtw_BT_efuse_map_read23a(struct rtw_adapter *padapter,
572 u16 addr, u16 cnts, u8 *data)
576 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_BT,
577 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
579 if ((addr + cnts) > mapLen)
582 Efuse_PowerSwitch(padapter, false, true);
584 rtl8723a_readefuse(padapter, EFUSE_BT, addr, cnts, data);
586 Efuse_PowerSwitch(padapter, false, false);
591 /*-----------------------------------------------------------------------------
592 * Function: Efuse_ReadAllMap
594 * Overview: Read All Efuse content
604 * 11/11/2008 MHC Create Version 0.
606 *---------------------------------------------------------------------------*/
608 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse);
610 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse)
614 Efuse_PowerSwitch(pAdapter, false, true);
616 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN,
619 rtl8723a_readefuse(pAdapter, efuseType, 0, mapLen, Efuse);
621 Efuse_PowerSwitch(pAdapter, false, false);
624 /*-----------------------------------------------------------------------------
625 * Function: efuse_ShadowRead1Byte
626 * efuse_ShadowRead2Byte
627 * efuse_ShadowRead4Byte
629 * Overview: Read from efuse init map by one/two/four bytes !!!!!
639 * 11/12/2008 MHC Create Version 0.
641 *---------------------------------------------------------------------------*/
643 efuse_ShadowRead1Byte(
644 struct rtw_adapter * pAdapter,
648 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
650 *Value = pEEPROM->efuse_eeprom_data[Offset];
651 } /* EFUSE_ShadowRead23a1Byte */
655 efuse_ShadowRead2Byte(
656 struct rtw_adapter * pAdapter,
660 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
662 *Value = pEEPROM->efuse_eeprom_data[Offset];
663 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
664 } /* EFUSE_ShadowRead23a2Byte */
666 /* Read Four Bytes */
668 efuse_ShadowRead4Byte(
669 struct rtw_adapter * pAdapter,
673 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
675 *Value = pEEPROM->efuse_eeprom_data[Offset];
676 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
677 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
678 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
679 } /* efuse_ShadowRead4Byte */
681 /*-----------------------------------------------------------------------------
682 * Function: EFUSE_ShadowMapUpdate23a
684 * Overview: Transfer current EFUSE content to shadow init and modify map.
694 * 11/13/2008 MHC Create Version 0.
696 *---------------------------------------------------------------------------*/
697 void EFUSE_ShadowMapUpdate23a(struct rtw_adapter *pAdapter, u8 efuseType)
699 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
702 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType,
703 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
705 if (pEEPROM->bautoload_fail_flag == true)
706 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
708 Efuse_ReadAllMap(pAdapter, efuseType,
709 pEEPROM->efuse_eeprom_data);
711 }/* EFUSE_ShadowMapUpdate23a */
713 /*-----------------------------------------------------------------------------
714 * Function: EFUSE_ShadowRead23a
716 * Overview: Read from efuse init map !!!!!
726 * 11/12/2008 MHC Create Version 0.
728 *---------------------------------------------------------------------------*/
731 struct rtw_adapter * pAdapter,
737 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
739 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
741 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
742 } /* EFUSE_ShadowRead23a */