1 /**************************************************************************//**
2 * @file RMC.h
3 * @version V1.00
4 * @brief M2L31 Series RRAM Memory Controller Driver Header File
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8 ******************************************************************************/
9 #ifndef __RMC_H__
10 #define __RMC_H__
11
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16
17 /** @addtogroup Standard_Driver Standard Driver
18 @{
19 */
20
21 /** @addtogroup RMC_Driver RMC Driver
22 @{
23 */
24
25
26 /** @addtogroup RMC_EXPORTED_CONSTANTS RMC Exported Constants
27 @{
28 */
29
30
31 /*---------------------------------------------------------------------------------------------------------*/
32 /* Define Base Address */
33 /*---------------------------------------------------------------------------------------------------------*/
34 #define RMC_APROM_BASE 0x00000000UL /*!< APROM base address \hideinitializer */
35 #define RMC_APROM_END 0x00080000UL /*!< APROM end address \hideinitializer */
36 #define RMC_APROM_BANK0_END (RMC_APROM_END/2UL) /*!< APROM bank0 end address \hideinitializer */
37 #define RMC_LDROM_BASE 0x0F100000UL /*!< LDROM base address \hideinitializer */
38 #define RMC_LDROM_END 0x0F102000UL /*!< LDROM end address \hideinitializer */
39 #define RMC_XOM_BASE 0x0F200000UL /*!< XOM Base Address \hideinitializer */
40 #define RMC_XOMR0_BASE 0x0F200000UL /*!< XOMR 0 Base Address \hideinitializer */
41 #define RMC_XOMR1_BASE 0x0F200010UL /*!< XOMR 1 Base Address \hideinitializer */
42 #define RMC_XOMR2_BASE 0x0F200020UL /*!< XOMR 2 Base Address \hideinitializer */
43 #define RMC_XOMR3_BASE 0x0F200030UL /*!< XOMR 3 Base Address \hideinitializer */
44 #define RMC_CONFIG_BASE 0x0F300000UL /*!< User Configuration address \hideinitializer */
45 #define RMC_USER_CONFIG_0 0x0F300000UL /*!< User Config 0 address \hideinitializer */
46 #define RMC_USER_CONFIG_1 0x0F300004UL /*!< User Config 1 address \hideinitializer */
47 #define RMC_USER_CONFIG_2 0x0F300008UL /*!< User Config 2 address \hideinitializer */
48 #define RMC_USER_CONFIG_3 0x0F30000CUL /*!< User Config 3 address \hideinitializer */
49 #define RMC_OTP_BASE 0x0F310000UL /*!< OTP flash base address \hideinitializer */
50
51 #define RMC_FLASH_PAGE_SIZE 0x1000UL /*!< Flash Page Size (4K bytes) \hideinitializer */
52 #define RMC_PAGE_ADDR_MASK 0xFFFFF000UL /*!< Flash page address mask \hideinitializer */
53 #define RMC_MULTI_WORD_PROG_LEN 512 /*!< The maximum length of a multi-word program. */
54
55 #define RMC_APROM_SIZE RMC_APROM_END /*!< APROM Size \hideinitializer */
56 #define RMC_BANK_SIZE (RMC_APROM_SIZE/2UL) /*!< APROM Bank Size \hideinitializer */
57 #define RMC_LDROM_SIZE 0x1000UL /*!< LDROM Size (4 Kbytes) \hideinitializer */
58 #define RMC_OTP_ENTRY_CNT 256UL /*!< OTP entry number \hideinitializer */
59
60 #define XOM_OFF_MARK 0x5A
61 #define XOM_DEBUG_MARK 0x50
62
63
64 /*---------------------------------------------------------------------------------------------------------*/
65 /* XOM region number constant definitions */
66 /*---------------------------------------------------------------------------------------------------------*/
67 #define XOMR0 0UL /*!< XOM region 0 */
68 #define XOMR1 1UL /*!< XOM region 1 */
69 #define XOMR2 2UL /*!< XOM region 2 */
70 #define XOMR3 3UL /*!< XOM region 3 */
71
72 /*---------------------------------------------------------------------------------------------------------*/
73 /* ISPCTL constant definitions */
74 /*---------------------------------------------------------------------------------------------------------*/
75 #define IS_BOOT_FROM_LDROM 0x1UL /*!< ISPCTL setting to select to boot from LDROM */
76 #define IS_BOOT_FROM_APROM 0x0UL /*!< ISPCTL setting to select to boot from APROM */
77
78 /*---------------------------------------------------------------------------------------------------------*/
79 /* ISPCMD constant definitions */
80 /*---------------------------------------------------------------------------------------------------------*/
81 #define RMC_ISPCMD_READ 0x00UL /*!< ISP Command: Read flash word \hideinitializer */
82 #define RMC_ISPCMD_READ_UID 0x04UL /*!< ISP Command: Read Unique ID \hideinitializer */
83 #define RMC_ISPCMD_READ_ALL1 0x08UL /*!< ISP Command: Read all-one result \hideinitializer */
84 #define RMC_ISPCMD_READ_CID 0x0BUL /*!< ISP Command: Read Company ID \hideinitializer */
85 #define RMC_ISPCMD_READ_DID 0x0CUL /*!< ISP Command: Read Device ID \hideinitializer */
86 #define RMC_ISPCMD_READ_CKS 0x0DUL /*!< ISP Command: Read checksum \hideinitializer */
87 #define RMC_ISPCMD_PROGRAM 0x21UL /*!< ISP Command: Write flash word \hideinitializer */
88 #define RMC_ISPCMD_LOAD_DATA_BUFFER 0x27UL /*!< ISP Command: Load Data Buffer \hideinitializer */
89 #define RMC_ISPCMD_CLEAR_DATA_BUFFER 0x2FUL /*!< ISP Command: Clear Data Buffer \hideinitializer */
90 #define RMC_ISPCMD_BANK_REMAP 0x2CUL /*!< ISP Command: Bank Remap \hideinitializer */
91
92 #define RMC_ISPCMD_PAGE_ERASE 0x22UL /*!< ISP Command: Page Erase Flash \hideinitializer */
93 #define RMC_ISPCMD_RUN_ALL1 0x28UL /*!< ISP Command: Run all-one verification \hideinitializer */
94 #define RMC_ISPCMD_RUN_CKS 0x2DUL /*!< ISP Command: Run checksum calculation \hideinitializer */
95 #define RMC_ISPCMD_VECMAP 0x2EUL /*!< ISP Command: Vector Page Remap \hideinitializer */
96
97 #define READ_ALLONE_YES 0xA11FFFFFUL /*!< Check-all-one result is all one. \hideinitializer */
98 #define READ_ALLONE_NOT 0xA1100000UL /*!< Check-all-one result is not all one. \hideinitializer */
99 #define READ_ALLONE_CMD_FAIL 0xFFFFFFFFUL /*!< Check-all-one command failed. \hideinitializer */
100
101 #define RMC_TIMEOUT_READ ((SystemCoreClock/10)/4) /*!< Read command time-out 100 ms \hideinitializer */
102 #define RMC_TIMEOUT_WRITE ((SystemCoreClock/10)/4) /*!< Write command time-out 100 ms \hideinitializer */
103 #define RMC_TIMEOUT_ERASE ((SystemCoreClock/10)/2) /*!< Erase command time-out 200 ms \hideinitializer */
104 #define RMC_TIMEOUT_CHKSUM (SystemCoreClock/2) /*!< Get checksum command time-out 2 s \hideinitializer */
105 #define RMC_TIMEOUT_CHKALLONE (SystemCoreClock/2) /*!< Check-all-one command time-out 2 s \hideinitializer */
106
107 /*@}*/ /* end of group RMC_EXPORTED_CONSTANTS */
108
109
110 /** @addtogroup RMC_EXPORTED_MACROS RMC Exported Macros
111 @{
112 */
113
114 /*---------------------------------------------------------------------------------------------------------*/
115 /* RMC Macro Definitions */
116 /*---------------------------------------------------------------------------------------------------------*/
117 /**
118 * @brief Set Boot from APROM
119 *
120 * @param None
121 *
122 * @return None
123 *
124 * @details This function is select booting from APROM.
125 *
126 */
127 #define RMC_SET_APROM_BOOT() (RMC->ISPCTL &= ~RMC_ISPCTL_BS_Msk) /*!< Select booting from APROM \hideinitializer */
128
129 /**
130 * @brief Set Boot from LDROM
131 *
132 * @param None
133 *
134 * @return None
135 *
136 * @details This function is select booting from LDROM.
137 *
138 */
139 #define RMC_SET_LDROM_BOOT() (RMC->ISPCTL |= RMC_ISPCTL_BS_Msk) /*!< Select booting from LDROM \hideinitializer */
140
141 /**
142 * @brief Enable APROM Update Function
143 *
144 * @param None
145 *
146 * @return None
147 *
148 * @details This function will set APUEN bit of ISPCTL control register to enable APROM update function.
149 * User needs to set APUEN bit before they can update APROM in APROM boot mode.
150 *
151 */
152 #define RMC_ENABLE_AP_UPDATE() (RMC->ISPCTL |= RMC_ISPCTL_APUEN_Msk) /*!< Enable APROM update \hideinitializer */
153
154 /**
155 * @brief Disable APROM Update Function
156 *
157 * @param None
158 *
159 * @return None
160 *
161 * @details This function will clear APUEN bit of ISPCTL control register to disable APROM update function.
162 *
163 */
164 #define RMC_DISABLE_AP_UPDATE() (RMC->ISPCTL &= ~RMC_ISPCTL_APUEN_Msk) /*!< Disable APROM update \hideinitializer */
165
166 /**
167 * @brief Enable User Configuration Update Function
168 *
169 * @param None
170 *
171 * @return None
172 *
173 * @details This function will set CFGUEN bit of ISPCTL control register to enable User Configuration update function.
174 * User needs to set CFGUEN bit before they can update User Configuration area.
175 *
176 */
177 #define RMC_ENABLE_CFG_UPDATE() (RMC->ISPCTL |= RMC_ISPCTL_CFGUEN_Msk) /*!< Enable User Config update \hideinitializer */
178
179 /**
180 * @brief Disable User Configuration Update Function
181 *
182 * @param None
183 *
184 * @return None
185 *
186 * @details This function will clear CFGUEN bit of ISPCTL control register to disable User Configuration update function.
187 *
188 */
189 #define RMC_DISABLE_CFG_UPDATE() (RMC->ISPCTL &= ~RMC_ISPCTL_CFGUEN_Msk) /*!< Disable User Config update \hideinitializer */
190
191 /**
192 * @brief Enable LDROM Update Function
193 *
194 * @param None
195 *
196 * @return None
197 *
198 * @details This function will set LDUEN bit of ISPCTL control register to enable LDROM update function.
199 * User needs to set LDUEN bit before they can update LDROM.
200 *
201 */
202 #define RMC_ENABLE_LD_UPDATE() (RMC->ISPCTL |= RMC_ISPCTL_LDUEN_Msk) /*!< Enable LDROM update \hideinitializer */
203
204 /**
205 * @brief Disable LDROM Update Function
206 *
207 * @param None
208 *
209 * @return None
210 *
211 * @details This function will set ISPEN bit of ISPCTL control register to disable LDROM update function.
212 *
213 */
214 #define RMC_DISABLE_LD_UPDATE() (RMC->ISPCTL &= ~RMC_ISPCTL_LDUEN_Msk) /*!< Disable LDROM update \hideinitializer */
215
216 /**
217 * @brief Disable ISP Function
218 *
219 * @param None
220 *
221 * @return None
222 *
223 * @details This function will clear ISPEN bit of ISPCTL control register to disable ISP function.
224 *
225 */
226 #define RMC_DISABLE_ISP() (RMC->ISPCTL &= ~RMC_ISPCTL_ISPEN_Msk) /*!< Disable ISP function \hideinitializer */
227
228 /**
229 * @brief Enable ISP Function
230 *
231 * @param None
232 *
233 * @return None
234 *
235 * @details This function will set ISPEN bit of ISPCTL control register to enable ISP function.
236 *
237 */
238 #define RMC_ENABLE_ISP() (RMC->ISPCTL |= RMC_ISPCTL_ISPEN_Msk) /*!< Enable ISP function \hideinitializer */
239
240 /**
241 * @brief Get ISP Fail Flag
242 *
243 * @param None
244 *
245 * @return None
246 *
247 * @details This function is used to get ISP fail flag when do ISP actoin.
248 *
249 */
250 #define RMC_GET_FAIL_FLAG() ((RMC->ISPCTL & RMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL) /*!< Get ISP fail flag \hideinitializer */
251
252 /**
253 * @brief Clear ISP Fail Flag
254 *
255 * @param None
256 *
257 * @return None
258 *
259 * @details This function is used to clear ISP fail flag when ISP fail flag set.
260 *
261 */
262 #define RMC_CLR_FAIL_FLAG() (RMC->ISPCTL |= RMC_ISPCTL_ISPFF_Msk) /*!< Clear ISP fail flag \hideinitializer */
263
264
265 /**
266 * @brief Enable ISP Interrupt
267 *
268 * @param None
269 *
270 * @return None
271 *
272 * @details This function will enable ISP action interrupt.
273 *
274 */
275 #define RMC_ENABLE_ISP_INT() (RMC_ISP->ISPCTL |= RMC_ISPCTL_INTEN_Msk) /*!< Enable ISP interrupt */
276
277 /**
278 * @brief Disable ISP Interrupt
279 *
280 * @param None
281 *
282 * @return None
283 *
284 * @details This function will disable ISP action interrupt.
285 *
286 */
287 #define RMC_DISABLE_ISP_INT() (RMC_ISP->ISPCTL &= ~RMC_ISPCTL_INTEN_Msk) /*!< Disable ISP interrupt */
288
289 /**
290 * @brief Get ISP Interrupt Flag
291 *
292 * @param None
293 *
294 * @return None
295 *
296 * @details This function will get ISP action interrupt status
297 *
298 */
299 #define RMC_GET_ISP_INT_FLAG() ((RMC_ISP->ISPSTS & RMC_ISPSTS_INTFLAG_Msk) ? 1UL : 0UL) /*!< Get ISP interrupt flag Status */
300
301 /**
302 * @brief Clear ISP Interrupt Flag
303 *
304 * @param None
305 *
306 * @return None
307 *
308 * @details This function will clear ISP interrupt flag
309 *
310 */
311 #define RMC_CLEAR_ISP_INT_FLAG() (RMC_ISP->ISPSTS = RMC_ISPSTS_INTFLAG_Msk) /*!< Clear ISP interrupt flag*/
312
313 /*@}*/ /* end of group RMC_EXPORTED_MACROS */
314
315 extern int32_t g_RMC_i32ErrCode;
316
317 /** @addtogroup RMC_EXPORTED_FUNCTIONS RMC Exported Functions
318 @{
319 */
320
321 /*---------------------------------------------------------------------------------------------------------*/
322 /* inline functions */
323 /*---------------------------------------------------------------------------------------------------------*/
324 __STATIC_INLINE uint32_t RMC_ReadCID(void);
325 __STATIC_INLINE uint32_t RMC_ReadPID(void);
326 __STATIC_INLINE uint32_t RMC_ReadUID(uint8_t u8Index);
327 __STATIC_INLINE uint32_t RMC_ReadUCID(uint32_t u32Index);
328 __STATIC_INLINE int32_t RMC_SetVectorPageAddr(uint32_t u32PageAddr);
329 __STATIC_INLINE uint32_t RMC_GetVECMAP(void);
330
331 /**
332 * @brief Get current vector mapping address.
333 * @param None
334 * @return The current vector mapping address.
335 * @details To get VECMAP value which is the page address for remapping to vector page (0x0).
336 * @note
337 * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
338 */
RMC_GetVECMAP(void)339 __STATIC_INLINE uint32_t RMC_GetVECMAP(void)
340 {
341 if((RMC->ISPSTS & RMC_ISPSTS_VECMAP_Msk) & 0x100000)
342 return (RMC->ISPSTS & RMC_ISPSTS_VECMAP_Msk) | 0xF000000;
343 else
344 return (RMC->ISPSTS & RMC_ISPSTS_VECMAP_Msk);
345 }
346
347 /**
348 * @brief Read company ID
349 * @param None
350 * @return The company ID (32-bit)
351 * @details The company ID of Nuvoton is fixed to be 0xDA
352 */
RMC_ReadCID(void)353 __STATIC_INLINE uint32_t RMC_ReadCID(void)
354 {
355 uint32_t tout = RMC_TIMEOUT_READ;
356
357 g_RMC_i32ErrCode = 0;
358
359 RMC->ISPCMD = RMC_ISPCMD_READ_CID; /* Set ISP Command Code */
360 RMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
361 RMC->ISPTRG = RMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
362 #if ISBEN
363 __ISB();
364 #endif /* To make sure ISP/CPU be Synchronized */
365 while (tout-- > 0)
366 {
367 if (!(RMC->ISPTRG & RMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
368 {
369 if (RMC->ISPDAT != 0x530000DA)
370 g_RMC_i32ErrCode = -1;
371 return RMC->ISPDAT;
372 }
373 }
374 g_RMC_i32ErrCode = -1;
375 return 0xFFFFFFFF;
376 }
377
378 /**
379 * @brief Read product ID
380 * @param None
381 * @return The product ID (32-bit)
382 * @details This function is used to read product ID.
383 */
RMC_ReadPID(void)384 __STATIC_INLINE uint32_t RMC_ReadPID(void)
385 {
386 uint32_t tout = RMC_TIMEOUT_READ;
387
388 g_RMC_i32ErrCode = 0;
389
390 RMC->ISPCMD = RMC_ISPCMD_READ_DID; /* Set ISP Command Code */
391 RMC->ISPADDR = 0x04u; /* Must keep 0x4 when read PID */
392 RMC->ISPTRG = RMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
393 #if ISBEN
394 __ISB();
395 #endif /* To make sure ISP/CPU be Synchronized */
396 while (tout-- > 0)
397 {
398 if (!(RMC->ISPTRG & RMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
399 return RMC->ISPDAT;
400 }
401 g_RMC_i32ErrCode = -1;
402 return 0xFFFFFFFF;
403 }
404
405 /**
406 * @brief Read Unique ID
407 * @param[in] u8Index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
408 * @return The 32-bit unique ID data of specified UID index.
409 * @details To read out 96-bit Unique ID.
410 */
RMC_ReadUID(uint8_t u8Index)411 __STATIC_INLINE uint32_t RMC_ReadUID(uint8_t u8Index)
412 {
413 uint32_t tout = RMC_TIMEOUT_READ;
414
415 g_RMC_i32ErrCode = 0;
416
417 RMC->ISPCMD = RMC_ISPCMD_READ_UID;
418 RMC->ISPADDR = ((uint32_t)u8Index << 2u);
419 RMC->ISPDAT = 0u;
420 RMC->ISPTRG = 0x1u;
421 #if ISBEN
422 __ISB();
423 #endif
424 while (tout-- > 0)
425 {
426 if (!(RMC->ISPTRG & RMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
427 return RMC->ISPDAT;
428 }
429 g_RMC_i32ErrCode = -1;
430 return 0xFFFFFFFF;
431 }
432
433 /**
434 * @brief To read UCID
435 * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
436 * @return The UCID of specified index
437 * @details This function is used to read unique chip ID (UCID).
438 */
RMC_ReadUCID(uint32_t u32Index)439 __STATIC_INLINE uint32_t RMC_ReadUCID(uint32_t u32Index)
440 {
441 uint32_t tout = RMC_TIMEOUT_READ;
442
443 g_RMC_i32ErrCode = 0;
444
445 RMC->ISPCMD = RMC_ISPCMD_READ_UID; /* Set ISP Command Code */
446 RMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
447 RMC->ISPTRG = RMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
448 #if ISBEN
449 __ISB();
450 #endif /* To make sure ISP/CPU be Synchronized */
451 while (tout-- > 0)
452 {
453 if (!(RMC->ISPTRG & RMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
454 return RMC->ISPDAT;
455 }
456 g_RMC_i32ErrCode = -1;
457 return 0xFFFFFFFF;
458 }
459
460 /**
461 * @brief Set vector mapping address
462 * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment.
463 * @return To set VECMAP to remap specified page address to 0x0.
464 * @details This function is used to set VECMAP to map specified page to vector page (0x0).
465 * @note
466 * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
467 */
RMC_SetVectorPageAddr(uint32_t u32PageAddr)468 __STATIC_INLINE int32_t RMC_SetVectorPageAddr(uint32_t u32PageAddr)
469 {
470 uint32_t tout = RMC_TIMEOUT_WRITE;
471
472 g_RMC_i32ErrCode = 0;
473
474 RMC->ISPCMD = RMC_ISPCMD_VECMAP; /* Set ISP Command Code */
475 RMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
476 RMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
477 #if ISBEN
478 __ISB();
479 #endif /* To make sure ISP/CPU be Synchronized */
480 while (tout-- > 0)
481 {
482 if (!RMC->ISPTRG) /* Waiting for ISP Done */
483 return 0;
484 }
485 g_RMC_i32ErrCode = -1;
486 return -1;
487 }
488
489
490 /*---------------------------------------------------------------------------------------------------------*/
491 /* Functions */
492 /*---------------------------------------------------------------------------------------------------------*/
493
494 extern void RMC_Close(void);
495 extern int32_t RMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
496 extern int32_t RMC_Erase(uint32_t u32PageAddr);
497 extern int32_t RMC_EraseXOM(uint32_t xom_num);
498 extern int32_t RMC_GetXOMState(uint32_t xom_num);
499 extern int32_t RMC_GetBootSource(void);
500 extern void RMC_Open(void);
501 extern uint32_t RMC_Read(uint32_t u32Addr);
502 extern uint32_t RMC_ReadDataFlashBaseAddr(void);
503 extern void RMC_SetBootSource(int32_t i32BootSrc);
504 extern int32_t RMC_Write(uint32_t u32Addr, uint32_t u32Data);
505 extern int32_t RMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
506 extern int32_t RMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
507 extern uint32_t RMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
508 extern uint32_t RMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
509 extern int32_t RMC_ReadOTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
510 extern int32_t RMC_WriteOTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
511 extern int32_t RMC_IsOTPLocked(uint32_t otp_num);
512 extern int32_t RMC_LockOTP(uint32_t otp_num);
513
514 /*@}*/ /* end of group RMC_EXPORTED_FUNCTIONS */
515
516 /*@}*/ /* end of group RMC_Driver */
517
518 /*@}*/ /* end of group Standard_Driver */
519
520 #ifdef __cplusplus
521 }
522 #endif
523
524 #endif /* __RMC_H__ */
525
526 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
527