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)
120 return hweight8((~word_en) & 0xf);
125 /* Execute E-Fuse read byte operation. */
126 /* Referred from SD1 Richard. */
129 /* 1. Boot from E-Fuse and successfully auto-load. */
130 /* 2. PASSIVE_LEVEL (USB interface) */
132 /* Created by Roger, 2008.10.21. */
135 ReadEFuseByte23a(struct rtw_adapter *Adapter, u16 _offset, u8 *pbuf)
142 rtl8723au_write8(Adapter, EFUSE_CTRL+1, (_offset & 0xff));
143 readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
144 rtl8723au_write8(Adapter, EFUSE_CTRL+2,
145 ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
148 readbyte = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
149 rtl8723au_write8(Adapter, EFUSE_CTRL+3, readbyte & 0x7f);
151 /* Check bit 32 read-ready */
153 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
154 while (!((value32 >> 24) & 0x80) && retry < 10000) {
155 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
159 /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
160 /* This fix the problem that Efuse read error in high temperature condition. */
161 /* Designer says that there shall be some delay after ready bit is set, or the */
162 /* result will always stay on last data we read. */
164 value32 = rtl8723au_read32(Adapter, EFUSE_CTRL);
166 *pbuf = (u8)(value32 & 0xff);
170 EFUSE_GetEfuseDefinition23a(struct rtw_adapter *pAdapter, u8 efuseType,
178 case TYPE_EFUSE_MAX_SECTION:
181 if (efuseType == EFUSE_WIFI)
182 *pMax_section = EFUSE_MAX_SECTION_8723A;
184 *pMax_section = EFUSE_BT_MAX_SECTION;
187 case TYPE_EFUSE_REAL_CONTENT_LEN:
190 if (efuseType == EFUSE_WIFI)
191 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
193 *pu2Tmp = EFUSE_BT_REAL_CONTENT_LEN;
196 case TYPE_AVAILABLE_EFUSE_BYTES_BANK:
199 if (efuseType == EFUSE_WIFI)
200 *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
201 EFUSE_OOB_PROTECT_BYTES);
203 *pu2Tmp = (EFUSE_BT_REAL_BANK_CONTENT_LEN -
204 EFUSE_PROTECT_BYTES_BANK);
207 case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL:
210 if (efuseType == EFUSE_WIFI)
211 *pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723A -
212 EFUSE_OOB_PROTECT_BYTES);
214 *pu2Tmp = (EFUSE_BT_REAL_CONTENT_LEN -
215 (EFUSE_PROTECT_BYTES_BANK * 3));
218 case TYPE_EFUSE_MAP_LEN:
221 if (efuseType == EFUSE_WIFI)
222 *pu2Tmp = EFUSE_MAP_LEN_8723A;
224 *pu2Tmp = EFUSE_BT_MAP_LEN;
227 case TYPE_EFUSE_PROTECT_BYTES_BANK:
230 if (efuseType == EFUSE_WIFI)
231 *pu1Tmp = EFUSE_OOB_PROTECT_BYTES;
233 *pu1Tmp = EFUSE_PROTECT_BYTES_BANK;
236 case TYPE_EFUSE_CONTENT_LEN_BANK:
239 if (efuseType == EFUSE_WIFI)
240 *pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723A;
242 *pu2Tmp = EFUSE_BT_REAL_BANK_CONTENT_LEN;
252 /*-----------------------------------------------------------------------------
253 * Function: EFUSE_Read1Byte23a
255 * Overview: Copy from WMAC fot EFUSE read 1 byte.
265 * 09/23/2008 MHC Copy from WMAC.
267 *---------------------------------------------------------------------------*/
269 EFUSE_Read1Byte23a(struct rtw_adapter *Adapter, u16 Address)
272 u8 Bytetemp = {0x00};
277 EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
278 TYPE_EFUSE_REAL_CONTENT_LEN,
279 (void *)&contentLen);
281 if (Address < contentLen) { /* E-fuse 512Byte */
282 /* Write E-fuse Register address bit0~7 */
283 temp = Address & 0xFF;
284 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
285 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
286 /* Write E-fuse Register address bit8~9 */
287 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
288 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
290 /* Write 0x30[31]= 0 */
291 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
292 temp = Bytetemp & 0x7F;
293 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
295 /* Wait Write-ready (0x30[31]= 1) */
296 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
297 while (!(Bytetemp & 0x80)) {
298 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
305 data = rtl8723au_read8(Adapter, EFUSE_CTRL);
310 }/* EFUSE_Read1Byte23a */
312 /*-----------------------------------------------------------------------------
313 * Function: EFUSE_Write1Byte
315 * Overview: Copy from WMAC fot EFUSE write 1 byte.
325 * 09/23/2008 MHC Copy from WMAC.
327 *---------------------------------------------------------------------------*/
331 struct rtw_adapter * Adapter,
336 struct rtw_adapter * Adapter,
340 u8 Bytetemp = {0x00};
345 /* RT_TRACE(COMP_EFUSE, DBG_LOUD, ("Addr =%x Data =%x\n", Address, Value)); */
346 EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
347 TYPE_EFUSE_REAL_CONTENT_LEN,
348 (void *)&contentLen);
350 if (Address < contentLen) { /* E-fuse 512Byte */
351 rtl8723au_write8(Adapter, EFUSE_CTRL, Value);
353 /* Write E-fuse Register address bit0~7 */
354 temp = Address & 0xFF;
355 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
356 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
358 /* Write E-fuse Register address bit8~9 */
359 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
360 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
362 /* Write 0x30[31]= 1 */
363 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
364 temp = Bytetemp | 0x80;
365 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
367 /* Wait Write-ready (0x30[31]= 0) */
368 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
369 while (Bytetemp & 0x80) {
370 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
378 }/* EFUSE_Write1Byte */
380 /* 11/16/2008 MH Read one byte from real Efuse. */
382 efuse_OneByteRead23a(struct rtw_adapter *pAdapter, u16 addr, u8 *data)
387 /* -----------------e-fuse reg ctrl --------------------------------- */
389 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
390 rtl8723au_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) &0x03)) |
391 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC));
393 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
395 while(!(0x80 &rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx<100))
398 *data = rtl8723au_read8(pAdapter, EFUSE_CTRL);
407 /* 11/16/2008 MH Write one byte to reald Efuse. */
409 efuse_OneByteWrite23a(struct rtw_adapter *pAdapter, u16 addr, u8 data)
414 /* RT_TRACE(COMP_EFUSE, DBG_LOUD, ("Addr = %x Data =%x\n", addr, data)); */
418 /* -----------------e-fuse reg ctrl --------------------------------- */
420 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
421 rtl8723au_write8(pAdapter, EFUSE_CTRL+2,
422 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC)|(u8)((addr>>8)&0x03));
423 rtl8723au_write8(pAdapter, EFUSE_CTRL, data);/* data */
425 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
427 while((0x80 & rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) &&
440 /*-----------------------------------------------------------------------------
441 * Function: efuse_WordEnableDataRead23a
443 * Overview: Read allowed word in current efuse section data.
453 * 11/16/2008 MHC Create Version 0.
454 * 11/21/2008 MHC Fix Write bug when we only enable late word.
456 *---------------------------------------------------------------------------*/
458 efuse_WordEnableDataRead23a(u8 word_en,
462 if (!(word_en&BIT(0))) {
463 targetdata[0] = sourdata[0];
464 targetdata[1] = sourdata[1];
466 if (!(word_en&BIT(1))) {
467 targetdata[2] = sourdata[2];
468 targetdata[3] = sourdata[3];
470 if (!(word_en&BIT(2))) {
471 targetdata[4] = sourdata[4];
472 targetdata[5] = sourdata[5];
474 if (!(word_en&BIT(3))) {
475 targetdata[6] = sourdata[6];
476 targetdata[7] = sourdata[7];
480 static int efuse_read8(struct rtw_adapter *padapter, u16 address, u8 *value)
482 return efuse_OneByteRead23a(padapter, address, value);
485 static int efuse_write8(struct rtw_adapter *padapter, u16 address, u8 *value)
487 return efuse_OneByteWrite23a(padapter, address, *value);
491 * read/write raw efuse data
493 int rtw_efuse_access23a(struct rtw_adapter *padapter, u8 bWrite, u16 start_addr,
497 u16 real_content_len = 0, max_available_size = 0;
499 int (*rw8)(struct rtw_adapter *, u16, u8*);
501 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
502 TYPE_EFUSE_REAL_CONTENT_LEN,
503 (void *)&real_content_len);
504 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
505 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
506 (void *)&max_available_size);
508 if (start_addr > real_content_len)
511 if (true == bWrite) {
512 if ((start_addr + cnts) > max_available_size)
518 Efuse_PowerSwitch(padapter, bWrite, true);
520 /* e-fuse one byte read / write */
521 for (i = 0; i < cnts; i++) {
522 if (start_addr >= real_content_len) {
527 res = rw8(padapter, start_addr++, data++);
532 Efuse_PowerSwitch(padapter, bWrite, false);
537 u16 efuse_GetMaxSize23a(struct rtw_adapter *padapter)
540 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
541 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
546 int rtw_efuse_map_read23a(struct rtw_adapter *padapter,
547 u16 addr, u16 cnts, u8 *data)
551 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
552 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
554 if ((addr + cnts) > mapLen)
557 Efuse_PowerSwitch(padapter, false, true);
559 rtl8723a_readefuse(padapter, EFUSE_WIFI, addr, cnts, data);
561 Efuse_PowerSwitch(padapter, false, false);
566 int rtw_BT_efuse_map_read23a(struct rtw_adapter *padapter,
567 u16 addr, u16 cnts, u8 *data)
571 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_BT,
572 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
574 if ((addr + cnts) > mapLen)
577 Efuse_PowerSwitch(padapter, false, true);
579 rtl8723a_readefuse(padapter, EFUSE_BT, addr, cnts, data);
581 Efuse_PowerSwitch(padapter, false, false);
586 /*-----------------------------------------------------------------------------
587 * Function: Efuse_ReadAllMap
589 * Overview: Read All Efuse content
599 * 11/11/2008 MHC Create Version 0.
601 *---------------------------------------------------------------------------*/
603 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse);
605 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse)
609 Efuse_PowerSwitch(pAdapter, false, true);
611 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN,
614 rtl8723a_readefuse(pAdapter, efuseType, 0, mapLen, Efuse);
616 Efuse_PowerSwitch(pAdapter, false, false);
619 /*-----------------------------------------------------------------------------
620 * Function: efuse_ShadowRead1Byte
621 * efuse_ShadowRead2Byte
622 * efuse_ShadowRead4Byte
624 * Overview: Read from efuse init map by one/two/four bytes !!!!!
634 * 11/12/2008 MHC Create Version 0.
636 *---------------------------------------------------------------------------*/
638 efuse_ShadowRead1Byte(
639 struct rtw_adapter * pAdapter,
643 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
645 *Value = pEEPROM->efuse_eeprom_data[Offset];
646 } /* EFUSE_ShadowRead23a1Byte */
650 efuse_ShadowRead2Byte(
651 struct rtw_adapter * pAdapter,
655 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
657 *Value = pEEPROM->efuse_eeprom_data[Offset];
658 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
659 } /* EFUSE_ShadowRead23a2Byte */
661 /* Read Four Bytes */
663 efuse_ShadowRead4Byte(
664 struct rtw_adapter * pAdapter,
668 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
670 *Value = pEEPROM->efuse_eeprom_data[Offset];
671 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
672 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
673 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
674 } /* efuse_ShadowRead4Byte */
676 /*-----------------------------------------------------------------------------
677 * Function: EFUSE_ShadowMapUpdate23a
679 * Overview: Transfer current EFUSE content to shadow init and modify map.
689 * 11/13/2008 MHC Create Version 0.
691 *---------------------------------------------------------------------------*/
692 void EFUSE_ShadowMapUpdate23a(struct rtw_adapter *pAdapter, u8 efuseType)
694 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
697 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType,
698 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
700 if (pEEPROM->bautoload_fail_flag == true)
701 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
703 Efuse_ReadAllMap(pAdapter, efuseType,
704 pEEPROM->efuse_eeprom_data);
706 }/* EFUSE_ShadowMapUpdate23a */
708 /*-----------------------------------------------------------------------------
709 * Function: EFUSE_ShadowRead23a
711 * Overview: Read from efuse init map !!!!!
721 * 11/12/2008 MHC Create Version 0.
723 *---------------------------------------------------------------------------*/
726 struct rtw_adapter * pAdapter,
732 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
734 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
736 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
737 } /* EFUSE_ShadowRead23a */