1 /**
2   ******************************************************************************
3   * @file    bl602_ef_ctrl.h
4   * @version V1.0
5   * @date
6   * @brief   This file is the standard driver header file
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; COPYRIGHT(c) 2020 Bouffalo Lab</center></h2>
11   *
12   * Redistribution and use in source and binary forms, with or without modification,
13   * are permitted provided that the following conditions are met:
14   *   1. Redistributions of source code must retain the above copyright notice,
15   *      this list of conditions and the following disclaimer.
16   *   2. Redistributions in binary form must reproduce the above copyright notice,
17   *      this list of conditions and the following disclaimer in the documentation
18   *      and/or other materials provided with the distribution.
19   *   3. Neither the name of Bouffalo Lab nor the names of its contributors
20   *      may be used to endorse or promote products derived from this software
21   *      without specific prior written permission.
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *
34   ******************************************************************************
35   */
36 #ifndef __BL602_EF_CTRL_H__
37 #define __BL602_EF_CTRL_H__
38 
39 #include "ef_ctrl_reg.h"
40 #include "bl602_common.h"
41 
42 /** @addtogroup  BL602_Peripheral_Driver
43  *  @{
44  */
45 
46 /** @addtogroup  EF_CTRL
47  *  @{
48  */
49 
50 /** @defgroup  EF_CTRL_Public_Types
51  *  @{
52  */
53 
54 /**
55  *  @brief Efuse Ctrl key slot type definition
56  */
57 typedef enum {
58     EF_CTRL_KEY_SLOT0, /*!< key slot 0 */
59     EF_CTRL_KEY_SLOT1, /*!< key slot 1 */
60     EF_CTRL_KEY_SLOT2, /*!< key slot 2 */
61     EF_CTRL_KEY_SLOT3, /*!< key slot 3 */
62     EF_CTRL_KEY_SLOT4, /*!< key slot 4 */
63     EF_CTRL_KEY_SLOT5, /*!< key slot 5 */
64     EF_CTRL_KEY_MAX,   /*!<  */
65 } EF_Ctrl_Key_Type;
66 
67 /**
68  *  @brief Efuse Ctrl sign type definition
69  */
70 typedef enum {
71     EF_CTRL_SIGN_NONE, /*!< no sign */
72     EF_CTRL_SIGN_RSA,  /*!< use RSA to sign */
73     EF_CTRL_SIGN_ECC,  /*!< use ECC to sign */
74 } EF_Ctrl_Sign_Type;
75 
76 /**
77  *  @brief Efuse Ctrl flash AES type definition
78  */
79 typedef enum {
80     EF_CTRL_SF_AES_NONE, /*!< No AES */
81     EF_CTRL_SF_AES_128,  /*!< AES 128 */
82     EF_CTRL_SF_AES_192,  /*!< AES 192 */
83     EF_CTRL_SF_AES_256,  /*!< AES 256 */
84 } EF_Ctrl_SF_AES_Type;
85 
86 /**
87  *  @brief Efuse Ctrl Dbg type definition
88  */
89 typedef enum {
90     EF_CTRL_DBG_OPEN = 0,  /*!< Open debug */
91     EF_CTRL_DBG_PASSWORD,  /*!< Open debug with password */
92     EF_CTRL_DBG_CLOSE = 4, /*!< Close debug */
93 } EF_Ctrl_Dbg_Mode_Type;
94 
95 /**
96  *  @brief Efuse Ctrl clock type definition
97  */
98 typedef enum {
99     EF_CTRL_EF_CLK,   /*!< Select efuse clock */
100     EF_CTRL_SAHB_CLK, /*!< Select SAHB clock */
101 } EF_Ctrl_CLK_Type;
102 
103 /**
104  *  @brief Efuse Ctrl clock type definition
105  */
106 typedef enum {
107     EF_CTRL_PARA_DFT,    /*!< Select default cyc parameter */
108     EF_CTRL_PARA_MANUAL, /*!< Select manual cyc parameter */
109 } EF_Ctrl_CYC_PARA_Type;
110 
111 /**
112  *  @brief Efuse Ctrl clock type definition
113  */
114 typedef enum {
115     EF_CTRL_OP_MODE_AUTO,   /*!< Select efuse program auto mode */
116     EF_CTRL_OP_MODE_MANUAL, /*!< Select efuse program manual mode */
117 } EF_Ctrl_OP_MODE_Type;
118 
119 /**
120  *  @brief Efuse Ctrl secure configuration structure type definition
121  */
122 typedef struct
123 {
124     EF_Ctrl_Dbg_Mode_Type ef_dbg_mode; /*!< Efuse debug mode */
125     uint8_t ef_dbg_jtag_0_dis;         /*!< Jtag debug disable config value */
126     uint8_t ef_sboot_en;               /*!< Secure boot enable config value */
127     uint8_t ef_no_hd_boot_en;          /*!< No header boot enable */
128 } EF_Ctrl_Sec_Param_Type;
129 
130 /**
131  *  @brief Efuse analog RC32M trim type definition
132  */
133 typedef struct
134 {
135     uint32_t trimRc32mCodeFrExt       : 8;  /*!< Efuse analog trim:trim_rc32m_code_fr_ext */
136     uint32_t trimRc32mCodeFrExtParity : 1;  /*!< Efuse analog trim:trim_rc32m_ext_code_en_parity */
137     uint32_t trimRc32mExtCodeEn       : 1;  /*!< Efuse analog trim:trim_rc32m_ext_code_en */
138     uint32_t reserved                 : 22; /*!< Efuse analog trim:reserved */
139 } Efuse_Ana_RC32M_Trim_Type;
140 
141 /**
142  *  @brief Efuse analog RC32K trim type definition
143  */
144 typedef struct
145 {
146     uint32_t trimRc32kCodeFrExt       : 10; /*!< Efuse analog trim:trim_rc32k_code_fr_ext */
147     uint32_t trimRc32kCodeFrExtParity : 1;  /*!< Efuse analog trim:trim_rc32k_code_fr_ext_parity */
148     uint32_t trimRc32kExtCodeEn       : 1;  /*!< Efuse analog trim:trim_rc32k_ext_code_en */
149     uint32_t reserved                 : 20; /*!< Efuse analog trim:reserved */
150 } Efuse_Ana_RC32K_Trim_Type;
151 
152 /**
153  *  @brief Efuse analog TSEN trim type definition
154  */
155 typedef struct
156 {
157     uint32_t tsenRefcodeCorner        : 12; /*!< TSEN refcode */
158     uint32_t tsenRefcodeCornerParity  : 1;  /*!< TSEN refcode parity */
159     uint32_t tsenRefcodeCornerEn      : 1;  /*!< TSEN refcode enable */
160     uint32_t tsenRefcodeCornerVersion : 1;  /*!< TSEN refcode version */
161     uint32_t reserved                 : 17; /*!< TSEN analog trim:reserved */
162 } Efuse_TSEN_Refcode_Corner_Type;
163 
164 /**
165  *  @brief Efuse analog ADC Gain trim type definition
166  */
167 typedef struct
168 {
169     uint32_t adcGainCoeff       : 12; /*!< ADC gain coeff */
170     uint32_t adcGainCoeffParity : 1;  /*!< ADC gain coeff parity */
171     uint32_t adcGainCoeffEn     : 1;  /*!< ADC gain coeff enable */
172     uint32_t reserved           : 18; /*!< ADC gain coeff:reserved */
173 } Efuse_ADC_Gain_Coeff_Type;
174 
175 /**
176  *  @brief Efuse analog device info type definition
177  */
178 typedef struct
179 {
180     uint32_t rsvd       : 19; /*!< Reserved */
181     uint32_t chip_ver   : 3;  /*!< chip revision */
182     uint32_t customerID : 2;  /*!< Efuse customer ID information */
183     uint32_t rsvd_info  : 3;  /*!< Efuse device info extension: 1:BL602C, 2:BL602L, 3:BL602E */
184     uint32_t memoryInfo : 2;  /*!< Efuse memory info 0:no memory, 1:1MB flash, 2:2MB flash */
185     uint32_t coreInfo   : 1;  /*!< Efuse reserved */
186     uint32_t mcuInfo    : 1;  /*!< Efuse mcu info 0:wifi, 1:mcu */
187     uint32_t pinInfo    : 1;  /*!< Efuse pin info 0:QFN32, 1:QFN40 */
188 } Efuse_Device_Info_Type;
189 
190 /**
191  *  @brief Efuse Capcode type definition
192  */
193 typedef struct
194 {
195     uint32_t capCode : 6;  /*!< Cap code value */
196     uint32_t parity  : 1;  /*!< Parity of capcode */
197     uint32_t en      : 1;  /*!< Enable status */
198     uint32_t rsvd    : 24; /*!< Reserved */
199 } Efuse_Capcode_Info_Type;
200 
201 /**
202  *  @brief Efuse Ldo11 Vout Sel Trim definition
203  */
204 typedef struct {
205     uint32_t sel_value : 4;  /*!< value trim */
206     uint32_t parity    : 1;  /*!< Parity of capcode */
207     uint32_t en        : 1;  /*!< Enable status */
208     uint32_t rsvd      : 26; /*!< Reserved */
209 } Efuse_Ldo11VoutSelTrim_Info_Type;
210 
211 /**
212  *  @brief Efuse Tx Power definition
213  */
214 typedef struct {
215     uint32_t txpower : 5;  /*!< txpower value  */
216     uint32_t parity  : 1;  /*!< Parity of capcode */
217     uint32_t en      : 1;  /*!< Enable status */
218     uint32_t rsvd    : 25; /*!< Reserved */
219 } Efuse_TxPower_Info_Type;
220 
221 /*@} end of group EF_CTRL_Public_Types */
222 
223 /** @defgroup  EF_CTRL_Public_Constants
224  *  @{
225  */
226 
227 /** @defgroup  EF_CTRL_KEY_TYPE
228  *  @{
229  */
230 #define IS_EF_CTRL_KEY_TYPE(type) (((type) == EF_CTRL_KEY_SLOT0) || \
231                                    ((type) == EF_CTRL_KEY_SLOT1) || \
232                                    ((type) == EF_CTRL_KEY_SLOT2) || \
233                                    ((type) == EF_CTRL_KEY_SLOT3) || \
234                                    ((type) == EF_CTRL_KEY_SLOT4) || \
235                                    ((type) == EF_CTRL_KEY_SLOT5) || \
236                                    ((type) == EF_CTRL_KEY_MAX))
237 
238 /** @defgroup  EF_CTRL_SIGN_TYPE
239  *  @{
240  */
241 #define IS_EF_CTRL_SIGN_TYPE(type) (((type) == EF_CTRL_SIGN_NONE) || \
242                                     ((type) == EF_CTRL_SIGN_RSA) ||  \
243                                     ((type) == EF_CTRL_SIGN_ECC))
244 
245 /** @defgroup  EF_CTRL_SF_AES_TYPE
246  *  @{
247  */
248 #define IS_EF_CTRL_SF_AES_TYPE(type) (((type) == EF_CTRL_SF_AES_NONE) || \
249                                       ((type) == EF_CTRL_SF_AES_128) ||  \
250                                       ((type) == EF_CTRL_SF_AES_192) ||  \
251                                       ((type) == EF_CTRL_SF_AES_256))
252 
253 /** @defgroup  EF_CTRL_DBG_MODE_TYPE
254  *  @{
255  */
256 #define IS_EF_CTRL_DBG_MODE_TYPE(type) (((type) == EF_CTRL_DBG_OPEN) ||     \
257                                         ((type) == EF_CTRL_DBG_PASSWORD) || \
258                                         ((type) == EF_CTRL_DBG_CLOSE))
259 
260 /** @defgroup  EF_CTRL_CLK_TYPE
261  *  @{
262  */
263 #define IS_EF_CTRL_CLK_TYPE(type) (((type) == EF_CTRL_EF_CLK) || \
264                                    ((type) == EF_CTRL_SAHB_CLK))
265 
266 /** @defgroup  EF_CTRL_CYC_PARA_TYPE
267  *  @{
268  */
269 #define IS_EF_CTRL_CYC_PARA_TYPE(type) (((type) == EF_CTRL_PARA_DFT) || \
270                                         ((type) == EF_CTRL_PARA_MANUAL))
271 
272 /** @defgroup  EF_CTRL_OP_MODE_TYPE
273  *  @{
274  */
275 #define IS_EF_CTRL_OP_MODE_TYPE(type) (((type) == EF_CTRL_OP_MODE_AUTO) || \
276                                        ((type) == EF_CTRL_OP_MODE_MANUAL))
277 
278 /*@} end of group EF_CTRL_Public_Constants */
279 
280 /** @defgroup  EF_CTRL_Public_Macros
281  *  @{
282  */
283 #define EF_CTRL_EFUSE_R0_SIZE 128
284 
285 /*@} end of group EF_CTRL_Public_Macros */
286 
287 /** @defgroup  EF_CTRL_Public_Functions
288  *  @{
289  */
290 void EF_Ctrl_Load_Efuse_R0(void);
291 void EF_Ctrl_Program_Efuse_0(void);
292 BL_Sts_Type EF_Ctrl_Busy(void);
293 BL_Sts_Type EF_Ctrl_AutoLoad_Done(void);
294 void EF_Ctrl_Write_Dbg_Pwd(uint32_t passWdLow, uint32_t passWdHigh, uint8_t program);
295 void EF_Ctrl_Read_Dbg_Pwd(uint32_t *passWdLow, uint32_t *passWdHigh);
296 void EF_Ctrl_Readlock_Dbg_Pwd(uint8_t program);
297 void EF_Ctrl_Writelock_Dbg_Pwd(uint8_t program);
298 void EF_Ctrl_Write_Secure_Cfg(EF_Ctrl_Sec_Param_Type *cfg, uint8_t program);
299 void EF_Ctrl_Read_Secure_Cfg(EF_Ctrl_Sec_Param_Type *cfg);
300 void EF_Ctrl_Write_Secure_Boot(EF_Ctrl_Sign_Type sign[1], EF_Ctrl_SF_AES_Type aes[1], uint8_t program);
301 void EF_Ctrl_Read_Secure_Boot(EF_Ctrl_Sign_Type sign[1], EF_Ctrl_SF_AES_Type aes[1]);
302 void EF_Ctrl_Read_CPU_Cfg(uint8_t *apDisabled, uint8_t *npDisabled, uint8_t *npCpuType);
303 uint8_t EF_Ctrl_Get_Trim_Enable(void);
304 uint8_t EF_Ctrl_Get_Trim_Parity(uint32_t val, uint8_t len);
305 void EF_Ctrl_Write_Ana_Trim(uint32_t index, uint32_t trim, uint8_t program);
306 void EF_Ctrl_Read_Ana_Trim(uint32_t index, uint32_t *trim);
307 void EF_Ctrl_Read_RC32M_Trim(Efuse_Ana_RC32M_Trim_Type *trim);
308 void EF_Ctrl_Read_RC32K_Trim(Efuse_Ana_RC32K_Trim_Type *trim);
309 void EF_Ctrl_Read_TSEN_Trim(Efuse_TSEN_Refcode_Corner_Type *trim);
310 void EF_Ctrl_Read_ADC_Gain_Trim(Efuse_ADC_Gain_Coeff_Type *trim);
311 void EF_Ctrl_Write_Sw_Usage(uint32_t index, uint32_t usage, uint8_t program);
312 void EF_Ctrl_Read_Sw_Usage(uint32_t index, uint32_t *usage);
313 void EF_Ctrl_Writelock_Sw_Usage(uint32_t index, uint8_t program);
314 void EF_Ctrl_Write_MAC_Address(uint8_t mac[6], uint8_t program);
315 BL_Err_Type EF_Ctrl_Read_MAC_Address(uint8_t mac[6]);
316 BL_Err_Type EF_Ctrl_Read_MAC_Address_Raw(uint8_t mac[7]);
317 void EF_Ctrl_Writelock_MAC_Address(uint8_t program);
318 BL_Err_Type EF_Ctrl_Read_Chip_ID(uint8_t id[8]);
319 void EF_Ctrl_Read_Device_Info(Efuse_Device_Info_Type *deviceInfo);
320 void EF_Ctrl_Write_AES_Key(uint8_t index, uint32_t *keyData, uint32_t len, uint8_t program);
321 void EF_Ctrl_Read_AES_Key(uint8_t index, uint32_t *keyData, uint32_t len);
322 void EF_Ctrl_Writelock_AES_Key(uint8_t index, uint8_t program);
323 void EF_Ctrl_Readlock_AES_Key(uint8_t index, uint8_t program);
324 void EF_Ctrl_Program_Direct_R0(uint32_t index, uint32_t *data, uint32_t len);
325 void EF_Ctrl_Read_Direct_R0(uint32_t index, uint32_t *data, uint32_t len);
326 void EF_Ctrl_Program_Direct(uint32_t index, uint32_t *data, uint32_t len);
327 void EF_Ctrl_Read_Direct(uint32_t index, uint32_t *data, uint32_t len);
328 void EF_Ctrl_Clear(uint32_t index, uint32_t len);
329 void EF_Ctrl_Crc_Enable(void);
330 BL_Sts_Type EF_Ctrl_Crc_Is_Busy(void);
331 void EF_Ctrl_Crc_Set_Golden(uint32_t goldenValue);
332 BL_Err_Type EF_Ctrl_Crc_Result(void);
333 void EF_Ctrl_Sw_AHB_Clk_0(void);
334 uint8_t EF_Ctrl_Is_MAC_Address_Slot_Empty(uint8_t slot, uint8_t reload);
335 BL_Err_Type EF_Ctrl_Write_MAC_Address_Opt(uint8_t slot, uint8_t mac[6], uint8_t program);
336 BL_Err_Type EF_Ctrl_Read_MAC_Address_Opt(uint8_t slot, uint8_t mac[6], uint8_t reload);
337 uint8_t EF_Ctrl_Is_CapCode_Slot_Empty(uint8_t slot, uint8_t reload);
338 BL_Err_Type EF_Ctrl_Write_CapCode_Opt(uint8_t slot, uint8_t code, uint8_t program);
339 BL_Err_Type EF_Ctrl_Read_CapCode_Opt(uint8_t slot, uint8_t *code, uint8_t reload);
340 uint8_t EF_Ctrl_Is_PowerOffset_Slot_Empty(uint8_t slot, uint8_t reload);
341 BL_Err_Type EF_Ctrl_Write_PowerOffset_Opt(uint8_t slot, int8_t pwrOffset[3], uint8_t program);
342 BL_Err_Type EF_Ctrl_Read_PowerOffset_Opt(uint8_t slot, int8_t pwrOffset[3], uint8_t reload);
343 void EF_Ctrl_Write_R0(uint32_t index, uint32_t *data, uint32_t len);
344 void EF_Ctrl_Read_R0(uint32_t index, uint32_t *data, uint32_t len);
345 
346 /*@} end of group EF_CTRL_Public_Functions */
347 
348 /*@} end of group EF_CTRL */
349 
350 /*@} end of group BL602_Peripheral_Driver */
351 
352 #endif /* __BL602_EF_CTRL_H__ */
353