1 /**
2   ******************************************************************************
3   * @file    rtl8721d_efuse.h
4   * @author
5   * @version V1.0.0
6   * @date    2016-05-17
7   * @brief   This file contains all the functions prototypes for the EFUSE firmware
8   *          library.
9   ******************************************************************************
10   * @attention
11   *
12   * This module is a confidential and proprietary property of RealTek and
13   * possession or use of this module requires written permission of RealTek.
14   *
15   * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
16   ******************************************************************************
17   */
18 
19 #ifndef _8710B_EFUSE_H_
20 #define _8710B_EFUSE_H_
21 
22 /** @addtogroup AmebaD_Platform
23   * @{
24   */
25 
26 /** @defgroup EFUSE
27   * @brief EFUSE driver modules
28   * @{
29   */
30 
31 /** @addtogroup EFUSE
32   * @verbatim
33   *****************************************************************************************
34   * logical map (512B)
35   *****************************************************************************************
36   *
37   * 0x00~0x1F		32bytes system autoload
38   * 0x20~0xCF	WIFI calibration data
39   * 0xD0~0x11F	HCI CIS
40   * 0x130~0x13F	SW/RF  Reserved
41   * 0x160~0x17F	32bytes USER1
42   * 0x180~0x19F	32bytes USER2
43   * 0x1A0~0x1Bf	32bytes USER3
44   * 0x1C0~0x1Df	USB HCI
45   *
46   *****************************************************************************************
47   * physical map (256B)
48   *****************************************************************************************
49   *
50   * 0x00~0x7E	127bytes for logical efuse, user can read
51   * 0x80~0x9F		32bytes for user OTP, user can read
52   * 0xA0~0xAF 	16bytes OTF KEY, can not read by user
53   * 0xB0~0xBF 	16bytes RDP KEY, can not read by user
54   * 0xC0 			1byte RDP EN, can not read by user
55   * 0xC1~0xD2 	18bytes for Security section
56   * 0xD3 			1byte JTAG ON/OFF
57   * 0xD4~0xEF	29bytes RF rsvd, user can read
58   * 0xF0~0xFF		16bytes RTK rsvd, user can read
59   *
60   *****************************************************************************************
61   * USER Section (3 * 32B)
62   *****************************************************************************************
63   * can be changed after write
64   *
65   * USER1 32B = 4 sections * 8B
66   * USER2 32B = 4 sections * 8B
67   * USER3 32B = 4 sections * 8B
68   *
69   *****************************************************************************************
70   * OTP Section (32B)
71   *****************************************************************************************
72   *
73   * can not be changed after write
74   *
75   * OTP 32B
76   *
77   *****************************************************************************************
78   * FW protection
79   *****************************************************************************************
80   *
81   * can not be changed after write
82   *
83   * OTF KEY: 16B, can not read
84   * RDP KEY: 16B, can not read
85   * RDP EN: 1B, can not read
86   * JTAG OFF: 1B
87   *
88   *****************************************************************************************
89   * @endverbatim
90   */
91 
92 /* Exported constants --------------------------------------------------------*/
93 
94 /** @defgroup EFUSE_Exported_Constants EFUSE Exported Constants
95   * @{
96   */
97 
98 /** @defgroup EFUSE_LOGICAL_definitions
99   * @{
100   */
101 #define EFUSE_MAP_LEN_8711B			1024 /*!< logical map len in byte */
102 #define EFUSE_MAX_SECTION_8711B		(EFUSE_MAP_LEN_8711B >> 3) /*!< logical map len in section */
103 #define PGPKT_DATA_SIZE				8 /*!< logical map section len */
104 
105 /* logical EFUSE User area */
106 #define USER_SECTION					(0x160 >> 3)/*!< user area section index  */
107 /**
108   * @}
109   */
110 
111 /** @defgroup EFUSE_PHYSICAL_definitions
112   * @{
113   */
114 #define OTP_SECTION						0x80 /*!< EFUSE OTP area: physical address */
115 #define OTP_SECTION_LEN				0x20 /*!< EFUSE OTP area: 32 bytes */
116 
117 /*  physical EFUSE len */
118 #define EFUSE_REAL_CONTENT_LEN		512
119 #define AVAILABLE_EFUSE_ADDR(addr)		(addr < EFUSE_REAL_CONTENT_LEN)
120 
121 /* physical EFUSE write forbid */
122 #define LOGICAL_MAP_SECTION_LEN		0x11E /*!< logical mapping efuse len in physical address */
123 #define EFUSE_OOB_PROTECT_BYTES		(EFUSE_REAL_CONTENT_LEN - LOGICAL_MAP_SECTION_LEN) // Security + RF + MAC + OTP
124 /**
125   * @}
126   */
127 
128 /** @defgroup EFUSE_CHIPID_definitions
129   * @{
130   */
131 //#define CHIPID_8710BN		0xFF /* PACKAGE_QFN32 */
132 //#define CHIPID_8710BU		0xFE /* PACKAGE_QFN48_MCM */
133 //#define CHIPID_8711BN		0xFD /* PACKAGE_QFN48 */
134 //#define CHIPID_8711BG		0xFC /* PACKAGE_QFN68 */
135 //#define CHIPID_8710BN_L0	0xFB /* PACKAGE_QFN32 L0 */
136 /**
137   * @}
138   */
139 
140 /** @defgroup EFUSE_VOLTAGE_definitions
141   * @{
142   */
143 #define L25EOUTVOLTAGE						7
144 /**
145   * @}
146   */
147 
148 /**
149   * @}
150   */
151 
152 /* Exported functions --------------------------------------------------------*/
153 /** @defgroup EFUSE_Exported_Functions EFUSE Exported Functions
154   * @{
155   */
156 /** @defgroup EFUSE_Physical_Address_functions
157   * @{
158   */
159 _LONG_CALL_ extern void EFUSEPowerSwitch(u8 bWrite, u8 PwrState, u8 L25OutVoltage);
160 _LONG_CALL_ extern u32 EFUSERead8(u32 CtrlSetting, u32 Addr, u8 *Data, u8 L25OutVoltage);
161 /* please use EFUSE_PMAP_WRITE8, dont use this API direclty, or chip will be damaged */
162 _LONG_CALL_ extern u32 EFUSEWrite8(u32 CtrlSetting, u32 Addr, u8 Data, u8 L25OutVoltage);
163 _LONG_CALL_ extern u32 EFUSE_LogicalMap_Read(u8 *pbuf);
164 /* please use EFUSE_LMAP_WRITE, dont use this API direclty, or chip will be damaged */
165 _LONG_CALL_ extern u32 EFUSE_LogicalMap_Write(u32 addr, u32 cnts, u8 *data);
166 /**
167   * @}
168   */
169 
170 /**
171   * @}
172   */
173 
174 
175 /**
176   * @}
177   */
178 
179 /**
180   * @}
181   */
182 
183 
184 /* Other functions --------------------------------------------------------*/
185 
186 #define EFUSE_POLL_TIMES	20000
187 
188 #define EFUSE_SECURE_START					0x150	/* 0x150~0x17F: TrustZone Secure EFUSE */
189 #define EFUSE_SECURE_END					0x17F
190 #define EFUSE_RDP_KEY_ADDR					0x170	/* 0x170~0x17F: 16B*/
191 
192 #define EFUSE_SWD_PWD_ADDR				0x180	/* 0x180~0x18F: 16B */
193 #define RSIP_KEY_ADDR						0x190	/* 0x190~0x19F: 16B */
194 #define SBOOT_PK_ADDR						0x1A0	/* 0x1A0~0x1BF: 32B*/
195 
196 #define EFUSE_SEC_CONFIG_ADDR0			0x1C0	/* 0x1c0 & 0x1c1, security config, please ref REG_LP_EFUSE_PROTECTION for bit define */
197 #define EFUSE_SEC_CONFIG_ADDR1			0x1C1	/* 0x1c0 & 0x1c1, security config, please ref REG_LP_EFUSE_PROTECTION for bit define */
198 
199 extern u8 EFUSE_MAP[1024];
200 
EFUSE_PMAP_READ8(u32 CtrlSetting,u32 Addr,u8 * Data,u8 L25OutVoltage)201 __STATIC_INLINE u32 EFUSE_PMAP_READ8(u32 CtrlSetting, u32 Addr, u8 *Data, u8 L25OutVoltage)
202 {
203 	return EFUSERead8(CtrlSetting, Addr, Data, L25OutVoltage);
204 }
205 
EFUSE_PMAP_WRITE8(u32 CtrlSetting,u32 Addr,u8 Data,u8 L25OutVoltage)206 __STATIC_INLINE u32 EFUSE_PMAP_WRITE8(u32 CtrlSetting, u32 Addr, u8 Data, u8 L25OutVoltage)
207 {
208 	if (is_power_supply18() == TRUE) {
209 		DBG_8195A("Please Switch to 3.3V to PG EFUSE !!!!!");
210 		//while (1);
211 
212 		return FALSE;
213 	} else {
214 		return EFUSEWrite8(CtrlSetting, Addr, Data, L25OutVoltage);
215 	}
216 }
217 
EFUSE_LMAP_READ(u8 * pbuf)218 __STATIC_INLINE u32 EFUSE_LMAP_READ(u8 *pbuf)
219 {
220 	return EFUSE_LogicalMap_Read(pbuf);
221 }
222 
EFUSE_LMAP_WRITE(u32 addr,u32 cnts,u8 * data)223 __STATIC_INLINE u32 EFUSE_LMAP_WRITE(u32 addr, u32 cnts, u8 *data)
224 {
225 	if (is_power_supply18() == TRUE) {
226 		DBG_8195A("Please Switch to 3.3V to PG EFUSE !!!!!");
227 		//while (1);
228 
229 		return FALSE;
230 	} else {
231 		return EFUSE_LogicalMap_Write(addr, cnts, data);
232 	}
233 }
234 
EFUSE_IsSecure(u32 Addr)235 __STATIC_INLINE u32 EFUSE_IsSecure(u32 Addr)
236 {
237 	if (TrustZone_IsSecure()) {
238 		if ((Addr >= 0x150) && (Addr <= 0x17F)) {
239 			return TRUE;
240 		}
241 	}
242 
243 	return FALSE;
244 }
245 
246 /**
247   * @brief  Get EFUSE physical address remain length.
248   * @param  none
249   * @retval EFUSE physical address remain length
250   */
251 
EFUSE_RemainLength(void)252 __STATIC_INLINE u32 EFUSE_RemainLength(void)
253 {
254 	u32 Idx = 0;
255 	u8 DataTemp0, WordEn;
256 
257 	//find start add
258 	while(Idx < LOGICAL_MAP_SECTION_LEN){
259 
260 		EFUSERead8(0, Idx, &DataTemp0, L25EOUTVOLTAGE);
261 
262 		if (DataTemp0 != 0xff) {
263 
264 			if((DataTemp0&0x0f) == 0xf){
265 
266 				Idx++;
267 				EFUSERead8(0, Idx, &DataTemp0, L25EOUTVOLTAGE);
268 				WordEn = ((~DataTemp0)&0x0f);
269 
270 				while(WordEn!=0){
271 					if (WordEn & BIT0) {
272 						Idx = Idx + 2;
273 					}
274 					WordEn = WordEn>>1;
275 				}
276 			}
277 			else {
278 				WordEn = ((~DataTemp0)&0x0f);
279 				while(WordEn!=0){
280 					if (WordEn & BIT0) {
281 						Idx = Idx + 2;
282 					}
283 					WordEn = WordEn>>1;
284 				}
285 			}
286 		}
287 		else {
288 			break;
289 		}
290 
291 		Idx++;
292 	}
293 
294 	return (LOGICAL_MAP_SECTION_LEN - Idx);
295 }
296 
297 #endif //_8710B_EFUSE_H_
298 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
299