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);
309 }/* EFUSE_Read1Byte23a */
311 /*-----------------------------------------------------------------------------
312 * Function: EFUSE_Write1Byte
314 * Overview: Copy from WMAC fot EFUSE write 1 byte.
324 * 09/23/2008 MHC Copy from WMAC.
326 *---------------------------------------------------------------------------*/
329 EFUSE_Write1Byte(struct rtw_adapter *Adapter, u16 Address, u8 Value);
331 EFUSE_Write1Byte(struct rtw_adapter *Adapter, u16 Address, u8 Value)
333 u8 Bytetemp = {0x00};
338 EFUSE_GetEfuseDefinition23a(Adapter, EFUSE_WIFI,
339 TYPE_EFUSE_REAL_CONTENT_LEN,
340 (void *)&contentLen);
342 if (Address < contentLen) { /* E-fuse 512Byte */
343 rtl8723au_write8(Adapter, EFUSE_CTRL, Value);
345 /* Write E-fuse Register address bit0~7 */
346 temp = Address & 0xFF;
347 rtl8723au_write8(Adapter, EFUSE_CTRL+1, temp);
348 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+2);
350 /* Write E-fuse Register address bit8~9 */
351 temp = ((Address >> 8) & 0x03) | (Bytetemp & 0xFC);
352 rtl8723au_write8(Adapter, EFUSE_CTRL+2, temp);
354 /* Write 0x30[31]= 1 */
355 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
356 temp = Bytetemp | 0x80;
357 rtl8723au_write8(Adapter, EFUSE_CTRL+3, temp);
359 /* Wait Write-ready (0x30[31]= 0) */
360 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
361 while (Bytetemp & 0x80) {
362 Bytetemp = rtl8723au_read8(Adapter, EFUSE_CTRL+3);
370 }/* EFUSE_Write1Byte */
372 /* 11/16/2008 MH Read one byte from real Efuse. */
374 efuse_OneByteRead23a(struct rtw_adapter *pAdapter, u16 addr, u8 *data)
379 /* -----------------e-fuse reg ctrl --------------------------------- */
381 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
382 rtl8723au_write8(pAdapter, EFUSE_CTRL+2, ((u8)((addr>>8) &0x03)) |
383 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC));
385 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0x72);/* read cmd */
387 while(!(0x80 &rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) && (tmpidx<100))
390 *data = rtl8723au_read8(pAdapter, EFUSE_CTRL);
399 /* 11/16/2008 MH Write one byte to reald Efuse. */
401 efuse_OneByteWrite23a(struct rtw_adapter *pAdapter, u16 addr, u8 data)
408 /* -----------------e-fuse reg ctrl --------------------------------- */
410 rtl8723au_write8(pAdapter, EFUSE_CTRL+1, (u8)(addr&0xff));
411 rtl8723au_write8(pAdapter, EFUSE_CTRL+2,
412 (rtl8723au_read8(pAdapter, EFUSE_CTRL+2)&0xFC)|(u8)((addr>>8)&0x03));
413 rtl8723au_write8(pAdapter, EFUSE_CTRL, data);/* data */
415 rtl8723au_write8(pAdapter, EFUSE_CTRL+3, 0xF2);/* write cmd */
417 while((0x80 & rtl8723au_read8(pAdapter, EFUSE_CTRL+3)) &&
430 /*-----------------------------------------------------------------------------
431 * Function: efuse_WordEnableDataRead23a
433 * Overview: Read allowed word in current efuse section data.
443 * 11/16/2008 MHC Create Version 0.
444 * 11/21/2008 MHC Fix Write bug when we only enable late word.
446 *---------------------------------------------------------------------------*/
448 efuse_WordEnableDataRead23a(u8 word_en,
452 if (!(word_en&BIT(0))) {
453 targetdata[0] = sourdata[0];
454 targetdata[1] = sourdata[1];
456 if (!(word_en&BIT(1))) {
457 targetdata[2] = sourdata[2];
458 targetdata[3] = sourdata[3];
460 if (!(word_en&BIT(2))) {
461 targetdata[4] = sourdata[4];
462 targetdata[5] = sourdata[5];
464 if (!(word_en&BIT(3))) {
465 targetdata[6] = sourdata[6];
466 targetdata[7] = sourdata[7];
470 static int efuse_read8(struct rtw_adapter *padapter, u16 address, u8 *value)
472 return efuse_OneByteRead23a(padapter, address, value);
475 static int efuse_write8(struct rtw_adapter *padapter, u16 address, u8 *value)
477 return efuse_OneByteWrite23a(padapter, address, *value);
481 * read/write raw efuse data
483 int rtw_efuse_access23a(struct rtw_adapter *padapter, u8 bWrite, u16 start_addr,
487 u16 real_content_len = 0, max_available_size = 0;
489 int (*rw8)(struct rtw_adapter *, u16, u8*);
491 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
492 TYPE_EFUSE_REAL_CONTENT_LEN,
493 (void *)&real_content_len);
494 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
495 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
496 (void *)&max_available_size);
498 if (start_addr > real_content_len)
501 if (true == bWrite) {
502 if ((start_addr + cnts) > max_available_size)
508 Efuse_PowerSwitch(padapter, bWrite, true);
510 /* e-fuse one byte read / write */
511 for (i = 0; i < cnts; i++) {
512 if (start_addr >= real_content_len) {
517 res = rw8(padapter, start_addr++, data++);
522 Efuse_PowerSwitch(padapter, bWrite, false);
527 u16 efuse_GetMaxSize23a(struct rtw_adapter *padapter)
530 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
531 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL,
536 int rtw_efuse_map_read23a(struct rtw_adapter *padapter,
537 u16 addr, u16 cnts, u8 *data)
541 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_WIFI,
542 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
544 if ((addr + cnts) > mapLen)
547 Efuse_PowerSwitch(padapter, false, true);
549 rtl8723a_readefuse(padapter, EFUSE_WIFI, addr, cnts, data);
551 Efuse_PowerSwitch(padapter, false, false);
556 int rtw_BT_efuse_map_read23a(struct rtw_adapter *padapter,
557 u16 addr, u16 cnts, u8 *data)
561 EFUSE_GetEfuseDefinition23a(padapter, EFUSE_BT,
562 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
564 if ((addr + cnts) > mapLen)
567 Efuse_PowerSwitch(padapter, false, true);
569 rtl8723a_readefuse(padapter, EFUSE_BT, addr, cnts, data);
571 Efuse_PowerSwitch(padapter, false, false);
576 /*-----------------------------------------------------------------------------
577 * Function: Efuse_ReadAllMap
579 * Overview: Read All Efuse content
589 * 11/11/2008 MHC Create Version 0.
591 *---------------------------------------------------------------------------*/
593 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse);
595 Efuse_ReadAllMap(struct rtw_adapter *pAdapter, u8 efuseType, u8 *Efuse)
599 Efuse_PowerSwitch(pAdapter, false, true);
601 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN,
604 rtl8723a_readefuse(pAdapter, efuseType, 0, mapLen, Efuse);
606 Efuse_PowerSwitch(pAdapter, false, false);
609 /*-----------------------------------------------------------------------------
610 * Function: efuse_ShadowRead1Byte
611 * efuse_ShadowRead2Byte
612 * efuse_ShadowRead4Byte
614 * Overview: Read from efuse init map by one/two/four bytes !!!!!
624 * 11/12/2008 MHC Create Version 0.
626 *---------------------------------------------------------------------------*/
628 efuse_ShadowRead1Byte(struct rtw_adapter *pAdapter, u16 Offset, u8 *Value)
630 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
632 *Value = pEEPROM->efuse_eeprom_data[Offset];
633 } /* EFUSE_ShadowRead23a1Byte */
637 efuse_ShadowRead2Byte(struct rtw_adapter *pAdapter, u16 Offset, u16 *Value)
639 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
641 *Value = pEEPROM->efuse_eeprom_data[Offset];
642 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
643 } /* EFUSE_ShadowRead23a2Byte */
645 /* Read Four Bytes */
647 efuse_ShadowRead4Byte(struct rtw_adapter *pAdapter, u16 Offset, u32 *Value)
649 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
651 *Value = pEEPROM->efuse_eeprom_data[Offset];
652 *Value |= pEEPROM->efuse_eeprom_data[Offset+1]<<8;
653 *Value |= pEEPROM->efuse_eeprom_data[Offset+2]<<16;
654 *Value |= pEEPROM->efuse_eeprom_data[Offset+3]<<24;
655 } /* efuse_ShadowRead4Byte */
657 /*-----------------------------------------------------------------------------
658 * Function: EFUSE_ShadowMapUpdate23a
660 * Overview: Transfer current EFUSE content to shadow init and modify map.
670 * 11/13/2008 MHC Create Version 0.
672 *---------------------------------------------------------------------------*/
673 void EFUSE_ShadowMapUpdate23a(struct rtw_adapter *pAdapter, u8 efuseType)
675 struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(pAdapter);
678 EFUSE_GetEfuseDefinition23a(pAdapter, efuseType,
679 TYPE_EFUSE_MAP_LEN, (void *)&mapLen);
681 if (pEEPROM->bautoload_fail_flag == true)
682 memset(pEEPROM->efuse_eeprom_data, 0xFF, mapLen);
684 Efuse_ReadAllMap(pAdapter, efuseType,
685 pEEPROM->efuse_eeprom_data);
687 }/* EFUSE_ShadowMapUpdate23a */
689 /*-----------------------------------------------------------------------------
690 * Function: EFUSE_ShadowRead23a
692 * Overview: Read from efuse init map !!!!!
702 * 11/12/2008 MHC Create Version 0.
704 *---------------------------------------------------------------------------*/
706 EFUSE_ShadowRead23a(struct rtw_adapter *pAdapter,
707 u8 Type, u16 Offset, u32 *Value)
710 efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
712 efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
714 efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
715 } /* EFUSE_ShadowRead23a */