Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m480_fmc.h Source File

m480_fmc.h

00001 /**************************************************************************//**
00002  * @file     fmc.h
00003  * @version  V1.00
00004  * @brief    M480 Series Flash Memory Controller Driver Header File
00005  *
00006  * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without modification,
00009  * are permitted provided that the following conditions are met:
00010  *   1. Redistributions of source code must retain the above copyright notice,
00011  *      this list of conditions and the following disclaimer.
00012  *   2. Redistributions in binary form must reproduce the above copyright notice,
00013  *      this list of conditions and the following disclaimer in the documentation
00014  *      and/or other materials provided with the distribution.
00015  *   3. Neither the name of Nuvoton Technology Corp. nor the names of its contributors
00016  *      may be used to endorse or promote products derived from this software
00017  *      without specific prior written permission.
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  ******************************************************************************/
00030 #ifndef __FMC_H__
00031 #define __FMC_H__
00032 
00033 #ifdef __cplusplus
00034 extern "C"
00035 {
00036 #endif
00037 
00038 /** @addtogroup Standard_Driver Standard Driver
00039   @{
00040 */
00041 
00042 /** @addtogroup FMC_Driver FMC Driver
00043   @{
00044 */
00045 
00046 
00047 /** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants
00048   @{
00049 */
00050 
00051 
00052 /*---------------------------------------------------------------------------------------------------------*/
00053 /* Define Base Address                                                                                     */
00054 /*---------------------------------------------------------------------------------------------------------*/
00055 #define FMC_APROM_BASE          0x00000000UL    /*!< APROM base address          \hideinitializer */
00056 #define FMC_APROM_END           0x00080000UL    /*!< APROM end address           \hideinitializer */
00057 #define FMC_APROM_BANK0_END     (FMC_APROM_END/2UL)  /*!< APROM bank0 end address     \hideinitializer */
00058 #define FMC_LDROM_BASE          0x00100000UL    /*!< LDROM base address          \hideinitializer */
00059 #define FMC_LDROM_END           0x00101000UL    /*!< LDROM end address           \hideinitializer */
00060 #define FMC_SPROM_BASE          0x00200000UL    /*!< SPROM base address          \hideinitializer */
00061 #define FMC_SPROM_END           0x00201000UL    /*!< SPROM end address           \hideinitializer */
00062 #define FMC_CONFIG_BASE         0x00300000UL    /*!< User Configuration address  \hideinitializer */
00063 #define FMC_USER_CONFIG_0       0x00300000UL    /*!< User Config 0 address       \hideinitializer */
00064 #define FMC_USER_CONFIG_1       0x00300004UL    /*!< User Config 1 address       \hideinitializer */
00065 #define FMC_USER_CONFIG_2       0x00300008UL    /*!< User Config 2 address       \hideinitializer */
00066 #define FMC_KPROM_BASE          0x00301000UL    /*!< Security ROM base address   \hideinitializer */
00067 #define FMC_OTP_BASE            0x00310000UL    /*!< OTP flash base address      \hideinitializer */
00068 
00069 #define FMC_FLASH_PAGE_SIZE     0x1000UL        /*!< Flash Page Size (4K bytes)  \hideinitializer */
00070 #define FMC_PAGE_ADDR_MASK      0xFFFFF000UL    /*!< Flash page address mask     \hideinitializer */
00071 #define FMC_MULTI_WORD_PROG_LEN 512             /*!< The maximum length of a multi-word program.  */
00072 
00073 #define FMC_APROM_SIZE          FMC_APROM_END   /*!< APROM Size                  \hideinitializer */
00074 #define FMC_BANK_SIZE           (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size             \hideinitializer */
00075 #define FMC_LDROM_SIZE          0x1000UL        /*!< LDROM Size (4 Kbytes)       \hideinitializer */
00076 #define FMC_SPROM_SIZE          0x1000UL        /*!< SPROM Size (4 Kbytes)       \hideinitializer */
00077 #define FMC_OTP_ENTRY_CNT       256UL           /*!< OTP entry number            \hideinitializer */
00078 
00079 
00080 /*---------------------------------------------------------------------------------------------------------*/
00081 /*  ISPCMD constant definitions                                                                            */
00082 /*---------------------------------------------------------------------------------------------------------*/
00083 #define FMC_ISPCMD_READ         0x00UL          /*!< ISP Command: Read flash word         \hideinitializer */
00084 #define FMC_ISPCMD_READ_UID     0x04UL          /*!< ISP Command: Read Unique ID          \hideinitializer */
00085 #define FMC_ISPCMD_READ_ALL1    0x08UL          /*!< ISP Command: Read all-one result     \hideinitializer */
00086 #define FMC_ISPCMD_READ_CID     0x0BUL          /*!< ISP Command: Read Company ID         \hideinitializer */
00087 #define FMC_ISPCMD_READ_DID     0x0CUL          /*!< ISP Command: Read Device ID          \hideinitializer */
00088 #define FMC_ISPCMD_READ_CKS     0x0DUL          /*!< ISP Command: Read checksum           \hideinitializer */
00089 #define FMC_ISPCMD_PROGRAM      0x21UL          /*!< ISP Command: Write flash word        \hideinitializer */
00090 #define FMC_ISPCMD_PAGE_ERASE   0x22UL          /*!< ISP Command: Page Erase Flash        \hideinitializer */
00091 #define FMC_ISPCMD_BANK_ERASE   0x23UL          /*!< ISP Command: Erase Flash bank 0 or 1 \hideinitializer */
00092 #define FMC_ISPCMD_BLOCK_ERASE  0x25UL          /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1  \hideinitializer */
00093 #define FMC_ISPCMD_PROGRAM_MUL  0x27UL          /*!< ISP Command: Multuple word program   \hideinitializer */
00094 #define FMC_ISPCMD_RUN_ALL1     0x28UL          /*!< ISP Command: Run all-one verification \hideinitializer */
00095 #define FMC_ISPCMD_RUN_CKS      0x2DUL          /*!< ISP Command: Run checksum calculation \hideinitializer */
00096 #define FMC_ISPCMD_VECMAP       0x2EUL          /*!< ISP Command: Vector Page Remap       \hideinitializer */
00097 #define FMC_ISPCMD_READ_64      0x40UL          /*!< ISP Command: Read double flash word  \hideinitializer */
00098 #define FMC_ISPCMD_PROGRAM_64   0x61UL          /*!< ISP Command: Write double flash word \hideinitializer */
00099 
00100 #define IS_BOOT_FROM_APROM      0UL             /*!< Is booting from APROM                \hideinitializer */
00101 #define IS_BOOT_FROM_LDROM      1UL             /*!< Is booting from LDROM                \hideinitializer */
00102 
00103 #define READ_ALLONE_YES         0xA11FFFFFUL    /*!< Check-all-one result is all one.     \hideinitializer */
00104 #define READ_ALLONE_NOT         0xA1100000UL    /*!< Check-all-one result is not all one. \hideinitializer */
00105 #define READ_ALLONE_CMD_FAIL    0xFFFFFFFFUL    /*!< Check-all-one command failed.        \hideinitializer */
00106 
00107 
00108 /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
00109 
00110 
00111 /** @addtogroup FMC_EXPORTED_MACROS FMC Exported Macros
00112   @{
00113 */
00114 
00115 
00116 /*---------------------------------------------------------------------------------------------------------*/
00117 /*  Macros                                                                                                 */
00118 /*---------------------------------------------------------------------------------------------------------*/
00119 
00120 #define FMC_SET_APROM_BOOT()        (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk)         /*!< Select booting from APROM  \hideinitializer */
00121 #define FMC_SET_LDROM_BOOT()        (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk)          /*!< Select booting from LDROM  \hideinitializer */
00122 #define FMC_ENABLE_AP_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_APUEN_Msk)      /*!< Enable APROM update        \hideinitializer */
00123 #define FMC_DISABLE_AP_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk)      /*!< Disable APROM update       \hideinitializer */
00124 #define FMC_ENABLE_CFG_UPDATE()     (FMC->ISPCTL |=  FMC_ISPCTL_CFGUEN_Msk)     /*!< Enable User Config update  \hideinitializer */
00125 #define FMC_DISABLE_CFG_UPDATE()    (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk)     /*!< Disable User Config update \hideinitializer */
00126 #define FMC_ENABLE_LD_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_LDUEN_Msk)      /*!< Enable LDROM update        \hideinitializer */
00127 #define FMC_DISABLE_LD_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk)      /*!< Disable LDROM update       \hideinitializer */
00128 #define FMC_ENABLE_SP_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_SPUEN_Msk)      /*!< Enable SPROM update        \hideinitializer */
00129 #define FMC_DISABLE_SP_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_SPUEN_Msk)      /*!< Disable SPROM update       \hideinitializer */
00130 #define FMC_DISABLE_ISP()           (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk)      /*!< Disable ISP function       \hideinitializer */
00131 #define FMC_ENABLE_ISP()            (FMC->ISPCTL |=  FMC_ISPCTL_ISPEN_Msk)      /*!< Enable ISP function        \hideinitializer */
00132 #define FMC_GET_FAIL_FLAG()         ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL)  /*!< Get ISP fail flag  \hideinitializer */
00133 #define FMC_CLR_FAIL_FLAG()         (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk)       /*!< Clear ISP fail flag        \hideinitializer */
00134 
00135 /*@}*/ /* end of group FMC_EXPORTED_MACROS */
00136 
00137 
00138 /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
00139   @{
00140 */
00141 
00142 /*---------------------------------------------------------------------------------------------------------*/
00143 /* inline functions                                                                                        */
00144 /*---------------------------------------------------------------------------------------------------------*/
00145 
00146 __STATIC_INLINE uint32_t FMC_ReadCID(void);
00147 __STATIC_INLINE uint32_t FMC_ReadPID(void);
00148 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
00149 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
00150 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr);
00151 __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
00152 
00153 /**
00154  * @brief       Get current vector mapping address.
00155  * @param       None
00156  * @return      The current vector mapping address.
00157  * @details     To get VECMAP value which is the page address for remapping to vector page (0x0).
00158  * @note
00159  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
00160  */
00161 __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
00162 {
00163     return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
00164 }
00165 
00166 /**
00167   * @brief    Read company ID
00168   * @param    None
00169   * @return   The company ID (32-bit)
00170   * @details  The company ID of Nuvoton is fixed to be 0xDA
00171   */
00172 __STATIC_INLINE uint32_t FMC_ReadCID(void)
00173 {
00174     FMC->ISPCMD = FMC_ISPCMD_READ_CID;           /* Set ISP Command Code */
00175     FMC->ISPADDR = 0x0u;                         /* Must keep 0x0 when read CID */
00176     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;          /* Trigger to start ISP procedure */
00177 #if ISBEN
00178     __ISB();
00179 #endif                                           /* To make sure ISP/CPU be Synchronized */
00180 
00181     while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
00182 
00183     return FMC->ISPDAT;
00184 }
00185 
00186 /**
00187   * @brief    Read product ID
00188   * @param    None
00189   * @return   The product ID (32-bit)
00190   * @details  This function is used to read product ID.
00191   */
00192 __STATIC_INLINE uint32_t FMC_ReadPID(void)
00193 {
00194     FMC->ISPCMD = FMC_ISPCMD_READ_DID;          /* Set ISP Command Code */
00195     FMC->ISPADDR = 0x04u;                       /* Must keep 0x4 when read PID */
00196     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;         /* Trigger to start ISP procedure */
00197 #if ISBEN
00198     __ISB();
00199 #endif                                          /* To make sure ISP/CPU be Synchronized */
00200 
00201     while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
00202 
00203     return FMC->ISPDAT;
00204 }
00205 
00206 /**
00207  * @brief       Read Unique ID
00208  * @param[in]   u8Index  UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
00209  * @return      The 32-bit unique ID data of specified UID index.
00210  * @details     To read out 96-bit Unique ID.
00211  */
00212 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
00213 {
00214     FMC->ISPCMD = FMC_ISPCMD_READ_UID;
00215     FMC->ISPADDR = ((uint32_t)u8Index << 2u);
00216     FMC->ISPDAT = 0u;
00217     FMC->ISPTRG = 0x1u;
00218 #if ISBEN
00219     __ISB();
00220 #endif
00221 
00222     while (FMC->ISPTRG) {}
00223 
00224     return FMC->ISPDAT;
00225 }
00226 
00227 /**
00228   * @brief      To read UCID
00229   * @param[in]  u32Index    Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
00230   * @return     The UCID of specified index
00231   * @details    This function is used to read unique chip ID (UCID).
00232   */
00233 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
00234 {
00235     FMC->ISPCMD = FMC_ISPCMD_READ_UID;            /* Set ISP Command Code */
00236     FMC->ISPADDR = (0x04u * u32Index) + 0x10u;    /* The UCID is at offset 0x10 with word alignment. */
00237     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;           /* Trigger to start ISP procedure */
00238 #if ISBEN
00239     __ISB();
00240 #endif                                            /* To make sure ISP/CPU be Synchronized */
00241 
00242     while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
00243 
00244     return FMC->ISPDAT;
00245 }
00246 
00247 /**
00248  * @brief       Set vector mapping address
00249  * @param[in]   u32PageAddr  The page address to remap to address 0x0. The address must be page alignment.
00250  * @return      To set VECMAP to remap specified page address to 0x0.
00251  * @details     This function is used to set VECMAP to map specified page to vector page (0x0).
00252  * @note
00253  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
00254  */
00255 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
00256 {
00257     FMC->ISPCMD = FMC_ISPCMD_VECMAP;  /* Set ISP Command Code */
00258     FMC->ISPADDR = u32PageAddr;       /* The address of specified page which will be map to address 0x0. It must be page alignment. */
00259     FMC->ISPTRG = 0x1u;               /* Trigger to start ISP procedure */
00260 #if ISBEN
00261     __ISB();
00262 #endif                                /* To make sure ISP/CPU be Synchronized */
00263 
00264     while (FMC->ISPTRG) {}            /* Waiting for ISP Done */
00265 }
00266 
00267 
00268 /*---------------------------------------------------------------------------------------------------------*/
00269 /*  Functions                                                                                              */
00270 /*---------------------------------------------------------------------------------------------------------*/
00271 
00272 extern void FMC_Close(void);
00273 extern int32_t FMC_Erase(uint32_t u32PageAddr);
00274 extern int32_t FMC_Erase_SPROM(void);
00275 extern int32_t FMC_Erase_Block(uint32_t u32BlockAddr);
00276 extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
00277 extern int32_t FMC_GetBootSource(void);
00278 extern void FMC_Open(void);
00279 extern uint32_t FMC_Read(uint32_t u32Addr);
00280 extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t *u32data0, uint32_t *u32data1);
00281 extern uint32_t FMC_ReadDataFlashBaseAddr(void);
00282 extern void FMC_SetBootSource(int32_t i32BootSrc);
00283 extern void FMC_Write(uint32_t u32Addr, uint32_t u32Data);
00284 extern int32_t  FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
00285 extern int32_t  FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
00286 extern int32_t  FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
00287 extern int32_t  FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
00288 extern int32_t  FMC_Lock_OTP(uint32_t otp_num);
00289 extern int32_t  FMC_Is_OTP_Locked(uint32_t otp_num);
00290 extern int32_t  FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
00291 extern int32_t  FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
00292 extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
00293 extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
00294 extern int32_t  FMC_SetSPKey(uint32_t key[3], uint32_t kpmax, uint32_t kemax, const int32_t lock_CONFIG, const int32_t lock_SPROM);
00295 extern int32_t  FMC_CompareSPKey(uint32_t key[3]);
00296 
00297 
00298 /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
00299 
00300 /*@}*/ /* end of group FMC_Driver */
00301 
00302 /*@}*/ /* end of group Standard_Driver */
00303 
00304 #ifdef __cplusplus
00305 }
00306 #endif
00307 
00308 #endif   /* __FMC_H__ */
00309 
00310 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/