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 REG_EFUSE_CTRL 0x0030
25 #define EFUSE_CTRL REG_EFUSE_CTRL /* E-Fuse Control */
27 #define VOLTAGE_V25 0x03
28 #define LDOE25_SHIFT 28
31 * When we want to enable write operation, we should change to
32 * pwr on state. When we stop write, we should switch to 500k mode
33 * and disable LDO 2.5V.
35 static void Efuse_PowerSwitch(struct rtw_adapter
*padapter
,
36 u8 bWrite
, u8 PwrState
)
41 if (PwrState
== true) {
42 rtl8723au_write8(padapter
, REG_EFUSE_ACCESS
, EFUSE_ACCESS_ON
);
45 * 1.2V Power: From VDDON with Power
46 * Cut(0x0000h[15]), default valid
48 tmpV16
= rtl8723au_read16(padapter
, REG_SYS_ISO_CTRL
);
49 if (!(tmpV16
& PWC_EV12V
)) {
51 rtl8723au_write16(padapter
, REG_SYS_ISO_CTRL
, tmpV16
);
53 /* Reset: 0x0000h[28], default valid */
54 tmpV16
= rtl8723au_read16(padapter
, REG_SYS_FUNC_EN
);
55 if (!(tmpV16
& FEN_ELDR
)) {
57 rtl8723au_write16(padapter
, REG_SYS_FUNC_EN
, tmpV16
);
61 * Clock: Gated(0x0008h[5]) 8M(0x0008h[1])
62 * clock from ANA, default valid
64 tmpV16
= rtl8723au_read16(padapter
, REG_SYS_CLKR
);
65 if ((!(tmpV16
& LOADER_CLK_EN
)) || (!(tmpV16
& ANA8M
))) {
66 tmpV16
|= (LOADER_CLK_EN
| ANA8M
);
67 rtl8723au_write16(padapter
, REG_SYS_CLKR
, tmpV16
);
71 /* Enable LDO 2.5V before read/write action */
72 tempval
= rtl8723au_read8(padapter
, EFUSE_TEST
+ 3);
74 tempval
|= (VOLTAGE_V25
<< 4);
75 rtl8723au_write8(padapter
, EFUSE_TEST
+ 3,
79 rtl8723au_write8(padapter
, REG_EFUSE_ACCESS
, EFUSE_ACCESS_OFF
);
82 /* Disable LDO 2.5V after read/write action */
83 tempval
= rtl8723au_read8(padapter
, EFUSE_TEST
+ 3);
84 rtl8723au_write8(padapter
, EFUSE_TEST
+ 3,
90 u16
Efuse_GetCurrentSize23a(struct rtw_adapter
*pAdapter
, u8 efuseType
)
94 if (efuseType
== EFUSE_WIFI
)
95 ret
= rtl8723a_EfuseGetCurrentSize_WiFi(pAdapter
);
97 ret
= rtl8723a_EfuseGetCurrentSize_BT(pAdapter
);
102 /* Get current efuse area enabled word */
103 u8
Efuse_CalculateWordCnts23a(u8 word_en
)
105 return hweight8((~word_en
) & 0xf);
109 * Description: Execute E-Fuse read byte operation.
111 * Assumptions: 1. Boot from E-Fuse and successfully auto-load.
112 * 2. PASSIVE_LEVEL (USB interface)
114 void ReadEFuseByte23a(struct rtw_adapter
*Adapter
, u16 _offset
, u8
*pbuf
)
121 rtl8723au_write8(Adapter
, EFUSE_CTRL
+1, (_offset
& 0xff));
122 readbyte
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+2);
123 rtl8723au_write8(Adapter
, EFUSE_CTRL
+2,
124 ((_offset
>> 8) & 0x03) | (readbyte
& 0xfc));
127 readbyte
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+3);
128 rtl8723au_write8(Adapter
, EFUSE_CTRL
+3, readbyte
& 0x7f);
130 /* Check bit 32 read-ready */
132 value32
= rtl8723au_read32(Adapter
, EFUSE_CTRL
);
133 while (!((value32
>> 24) & 0x80) && retry
< 10000) {
134 value32
= rtl8723au_read32(Adapter
, EFUSE_CTRL
);
139 * Added suggested delay. This fixes the problem that
140 * Efuse read error in high temperature condition.
141 * Designer says that there shall be some delay after
142 * ready bit is set, or the result will always stay
143 * on last data we read.
146 value32
= rtl8723au_read32(Adapter
, EFUSE_CTRL
);
148 *pbuf
= (u8
)(value32
& 0xff);
151 void EFUSE_GetEfuseDefinition23a(struct rtw_adapter
*pAdapter
, u8 efuseType
,
159 case TYPE_EFUSE_MAX_SECTION
:
162 if (efuseType
== EFUSE_WIFI
)
163 *pMax_section
= EFUSE_MAX_SECTION_8723A
;
165 *pMax_section
= EFUSE_BT_MAX_SECTION
;
168 case TYPE_EFUSE_REAL_CONTENT_LEN
:
171 if (efuseType
== EFUSE_WIFI
)
172 *pu2Tmp
= EFUSE_REAL_CONTENT_LEN_8723A
;
174 *pu2Tmp
= EFUSE_BT_REAL_CONTENT_LEN
;
177 case TYPE_AVAILABLE_EFUSE_BYTES_BANK
:
180 if (efuseType
== EFUSE_WIFI
)
181 *pu2Tmp
= (EFUSE_REAL_CONTENT_LEN_8723A
-
182 EFUSE_OOB_PROTECT_BYTES
);
184 *pu2Tmp
= (EFUSE_BT_REAL_BANK_CONTENT_LEN
-
185 EFUSE_PROTECT_BYTES_BANK
);
188 case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL
:
191 if (efuseType
== EFUSE_WIFI
)
192 *pu2Tmp
= (EFUSE_REAL_CONTENT_LEN_8723A
-
193 EFUSE_OOB_PROTECT_BYTES
);
195 *pu2Tmp
= (EFUSE_BT_REAL_CONTENT_LEN
-
196 (EFUSE_PROTECT_BYTES_BANK
* 3));
199 case TYPE_EFUSE_MAP_LEN
:
202 if (efuseType
== EFUSE_WIFI
)
203 *pu2Tmp
= EFUSE_MAP_LEN_8723A
;
205 *pu2Tmp
= EFUSE_BT_MAP_LEN
;
208 case TYPE_EFUSE_PROTECT_BYTES_BANK
:
211 if (efuseType
== EFUSE_WIFI
)
212 *pu1Tmp
= EFUSE_OOB_PROTECT_BYTES
;
214 *pu1Tmp
= EFUSE_PROTECT_BYTES_BANK
;
217 case TYPE_EFUSE_CONTENT_LEN_BANK
:
220 if (efuseType
== EFUSE_WIFI
)
221 *pu2Tmp
= EFUSE_REAL_CONTENT_LEN_8723A
;
223 *pu2Tmp
= EFUSE_BT_REAL_BANK_CONTENT_LEN
;
233 /* Copy from WMAC for EFUSE read 1 byte. */
234 u8
EFUSE_Read1Byte23a(struct rtw_adapter
*Adapter
, u16 Address
)
237 u8 Bytetemp
= {0x00};
242 EFUSE_GetEfuseDefinition23a(Adapter
, EFUSE_WIFI
,
243 TYPE_EFUSE_REAL_CONTENT_LEN
,
244 (void *)&contentLen
);
246 if (Address
< contentLen
) { /* E-fuse 512Byte */
247 /* Write E-fuse Register address bit0~7 */
248 temp
= Address
& 0xFF;
249 rtl8723au_write8(Adapter
, EFUSE_CTRL
+1, temp
);
250 Bytetemp
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+2);
251 /* Write E-fuse Register address bit8~9 */
252 temp
= ((Address
>> 8) & 0x03) | (Bytetemp
& 0xFC);
253 rtl8723au_write8(Adapter
, EFUSE_CTRL
+2, temp
);
255 /* Write 0x30[31]= 0 */
256 Bytetemp
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+3);
257 temp
= Bytetemp
& 0x7F;
258 rtl8723au_write8(Adapter
, EFUSE_CTRL
+3, temp
);
260 /* Wait Write-ready (0x30[31]= 1) */
261 Bytetemp
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+3);
262 while (!(Bytetemp
& 0x80)) {
263 Bytetemp
= rtl8723au_read8(Adapter
, EFUSE_CTRL
+3);
270 data
= rtl8723au_read8(Adapter
, EFUSE_CTRL
);
276 /* Read one byte from real Efuse. */
277 int efuse_OneByteRead23a(struct rtw_adapter
*pAdapter
, u16 addr
, u8
*data
)
282 /* -----------------e-fuse reg ctrl ---------------------------- */
284 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 1, (u8
)(addr
& 0xff));
285 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 2,
286 ((u8
)((addr
>> 8) & 0x03)) |
287 (rtl8723au_read8(pAdapter
, EFUSE_CTRL
+ 2) & 0xFC));
289 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 3, 0x72); /* read cmd */
291 while (!(0x80 & rtl8723au_read8(pAdapter
, EFUSE_CTRL
+ 3)) &&
295 *data
= rtl8723au_read8(pAdapter
, EFUSE_CTRL
);
304 /* Write one byte to reald Efuse. */
305 int efuse_OneByteWrite23a(struct rtw_adapter
*pAdapter
, u16 addr
, u8 data
)
312 /* -----------------e-fuse reg ctrl ------------------------- */
314 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 1, (u8
)(addr
& 0xff));
315 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 2,
316 (rtl8723au_read8(pAdapter
, EFUSE_CTRL
+ 2) & 0xFC) |
317 (u8
)((addr
>> 8) & 0x03));
318 rtl8723au_write8(pAdapter
, EFUSE_CTRL
, data
); /* data */
320 rtl8723au_write8(pAdapter
, EFUSE_CTRL
+ 3, 0xF2); /* write cmd */
322 while ((0x80 & rtl8723au_read8(pAdapter
, EFUSE_CTRL
+ 3)) &&
335 /* Read allowed word in current efuse section data. */
336 void efuse_WordEnableDataRead23a(u8 word_en
, u8
*sourdata
, u8
*targetdata
)
338 if (!(word_en
&BIT(0))) {
339 targetdata
[0] = sourdata
[0];
340 targetdata
[1] = sourdata
[1];
342 if (!(word_en
&BIT(1))) {
343 targetdata
[2] = sourdata
[2];
344 targetdata
[3] = sourdata
[3];
346 if (!(word_en
&BIT(2))) {
347 targetdata
[4] = sourdata
[4];
348 targetdata
[5] = sourdata
[5];
350 if (!(word_en
&BIT(3))) {
351 targetdata
[6] = sourdata
[6];
352 targetdata
[7] = sourdata
[7];
356 static int efuse_read8(struct rtw_adapter
*padapter
, u16 address
, u8
*value
)
358 return efuse_OneByteRead23a(padapter
, address
, value
);
361 static int efuse_write8(struct rtw_adapter
*padapter
, u16 address
, u8
*value
)
363 return efuse_OneByteWrite23a(padapter
, address
, *value
);
366 /* read/write raw efuse data */
367 int rtw_efuse_access23a(struct rtw_adapter
*padapter
, u8 bWrite
, u16 start_addr
,
371 u16 real_content_len
= 0, max_available_size
= 0;
373 int (*rw8
)(struct rtw_adapter
*, u16
, u8
*);
375 EFUSE_GetEfuseDefinition23a(padapter
, EFUSE_WIFI
,
376 TYPE_EFUSE_REAL_CONTENT_LEN
,
377 (void *)&real_content_len
);
378 EFUSE_GetEfuseDefinition23a(padapter
, EFUSE_WIFI
,
379 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL
,
380 (void *)&max_available_size
);
382 if (start_addr
> real_content_len
)
385 if (true == bWrite
) {
386 if ((start_addr
+ cnts
) > max_available_size
)
392 Efuse_PowerSwitch(padapter
, bWrite
, true);
394 /* e-fuse one byte read/write */
395 for (i
= 0; i
< cnts
; i
++) {
396 if (start_addr
>= real_content_len
) {
401 res
= rw8(padapter
, start_addr
++, data
++);
406 Efuse_PowerSwitch(padapter
, bWrite
, false);
411 u16
efuse_GetMaxSize23a(struct rtw_adapter
*padapter
)
415 EFUSE_GetEfuseDefinition23a(padapter
, EFUSE_WIFI
,
416 TYPE_AVAILABLE_EFUSE_BYTES_TOTAL
,
421 int rtw_efuse_map_read23a(struct rtw_adapter
*padapter
,
422 u16 addr
, u16 cnts
, u8
*data
)
426 EFUSE_GetEfuseDefinition23a(padapter
, EFUSE_WIFI
,
427 TYPE_EFUSE_MAP_LEN
, (void *)&mapLen
);
429 if ((addr
+ cnts
) > mapLen
)
432 Efuse_PowerSwitch(padapter
, false, true);
434 rtl8723a_readefuse(padapter
, EFUSE_WIFI
, addr
, cnts
, data
);
436 Efuse_PowerSwitch(padapter
, false, false);
441 int rtw_BT_efuse_map_read23a(struct rtw_adapter
*padapter
,
442 u16 addr
, u16 cnts
, u8
*data
)
446 EFUSE_GetEfuseDefinition23a(padapter
, EFUSE_BT
,
447 TYPE_EFUSE_MAP_LEN
, (void *)&mapLen
);
449 if ((addr
+ cnts
) > mapLen
)
452 Efuse_PowerSwitch(padapter
, false, true);
454 rtl8723a_readefuse(padapter
, EFUSE_BT
, addr
, cnts
, data
);
456 Efuse_PowerSwitch(padapter
, false, false);
461 /* Read All Efuse content */
462 static void Efuse_ReadAllMap(struct rtw_adapter
*pAdapter
, u8 efuseType
,
467 Efuse_PowerSwitch(pAdapter
, false, true);
469 EFUSE_GetEfuseDefinition23a(pAdapter
, efuseType
, TYPE_EFUSE_MAP_LEN
,
472 rtl8723a_readefuse(pAdapter
, efuseType
, 0, mapLen
, Efuse
);
474 Efuse_PowerSwitch(pAdapter
, false, false);
478 * Functions: efuse_ShadowRead1Byte
479 * efuse_ShadowRead2Byte
480 * efuse_ShadowRead4Byte
482 * Read from efuse init map by one/two/four bytes
484 static void efuse_ShadowRead1Byte(struct rtw_adapter
*pAdapter
, u16 Offset
,
487 struct eeprom_priv
*pEEPROM
= GET_EEPROM_EFUSE_PRIV(pAdapter
);
489 *Value
= pEEPROM
->efuse_eeprom_data
[Offset
];
492 static void efuse_ShadowRead2Byte(struct rtw_adapter
*pAdapter
, u16 Offset
,
495 struct eeprom_priv
*pEEPROM
= GET_EEPROM_EFUSE_PRIV(pAdapter
);
497 *Value
= pEEPROM
->efuse_eeprom_data
[Offset
];
498 *Value
|= pEEPROM
->efuse_eeprom_data
[Offset
+1]<<8;
501 static void efuse_ShadowRead4Byte(struct rtw_adapter
*pAdapter
, u16 Offset
,
504 struct eeprom_priv
*pEEPROM
= GET_EEPROM_EFUSE_PRIV(pAdapter
);
506 *Value
= pEEPROM
->efuse_eeprom_data
[Offset
];
507 *Value
|= pEEPROM
->efuse_eeprom_data
[Offset
+1]<<8;
508 *Value
|= pEEPROM
->efuse_eeprom_data
[Offset
+2]<<16;
509 *Value
|= pEEPROM
->efuse_eeprom_data
[Offset
+3]<<24;
512 /* Transfer current EFUSE content to shadow init and modify map. */
513 void EFUSE_ShadowMapUpdate23a(struct rtw_adapter
*pAdapter
, u8 efuseType
)
515 struct eeprom_priv
*pEEPROM
= GET_EEPROM_EFUSE_PRIV(pAdapter
);
518 EFUSE_GetEfuseDefinition23a(pAdapter
, efuseType
,
519 TYPE_EFUSE_MAP_LEN
, (void *)&mapLen
);
521 if (pEEPROM
->bautoload_fail_flag
== true)
522 memset(pEEPROM
->efuse_eeprom_data
, 0xFF, mapLen
);
524 Efuse_ReadAllMap(pAdapter
, efuseType
,
525 pEEPROM
->efuse_eeprom_data
);
528 /* Read from efuse init map */
529 void EFUSE_ShadowRead23a(struct rtw_adapter
*pAdapter
, u8 Type
,
530 u16 Offset
, u32
*Value
)
533 efuse_ShadowRead1Byte(pAdapter
, Offset
, (u8
*)Value
);
535 efuse_ShadowRead2Byte(pAdapter
, Offset
, (u16
*)Value
);
537 efuse_ShadowRead4Byte(pAdapter
, Offset
, (u32
*)Value
);