1 /*
2  * Copyright 2017-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_romapi.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 
14 /* Component ID definition, used by tools. */
15 #ifndef FSL_COMPONENT_ID
16 #define FSL_COMPONENT_ID "driver.romapi"
17 #endif
18 
19 /*******************************************************************************
20  * Prototypes
21  ******************************************************************************/
22 
23 /*!
24  * @brief Interface for the ROM FLEXSPI NOR flash driver.
25  */
26 typedef struct
27 {
28     uint32_t version;
29     status_t (*init)(uint32_t instance, flexspi_nor_config_t *config);
30     status_t (*program)(uint32_t instance, flexspi_nor_config_t *config, uint32_t dst_addr, const uint32_t *src);
31     uint32_t reserved0;
32     status_t (*erase)(uint32_t instance, flexspi_nor_config_t *config, uint32_t start, uint32_t lengthInBytes);
33     uint32_t reserved1;
34     void (*clear_cache)(uint32_t instance);
35     status_t (*xfer)(uint32_t instance, flexspi_xfer_t *xfer);
36     status_t (*update_lut)(uint32_t instance, uint32_t seqIndex, const uint32_t *lutBase, uint32_t seqNumber);
37     uint32_t reserved2;
38 } flexspi_nor_driver_interface_t;
39 
40 /*!
41  * @brief Root of the bootloader api tree.
42  *
43  * An instance of this struct resides in read-only memory in the bootloader. It
44  * provides a user application access to APIs exported by the bootloader.
45  *
46  * @note The order of existing fields must not be changed.
47  */
48 typedef struct
49 {
50     void (*runBootloader)(void *arg); /*!< Function to start the bootloader executing */
51     const uint32_t version;           /*!< Bootloader version number */
52     const uint8_t *copyright;         /*!< Bootloader Copyright */
53     const uint32_t reserved0;
54     flexspi_nor_driver_interface_t *flexSpiNorDriver; /*!< FLEXSPI NOR flash api */
55 } bootloader_api_entry_t;
56 
57 /*******************************************************************************
58  * Variables
59  ******************************************************************************/
60 
61 #define g_bootloaderTree ((bootloader_api_entry_t *)*(uint32_t *)0x0020001cU)
62 
63 #define api_flexspi_nor_erase_sector \
64     ((status_t(*)(uint32_t instance, flexspi_nor_config_t * config, uint32_t address))0x0021055dU)
65 #define api_flexspi_nor_erase_block \
66     ((status_t(*)(uint32_t instance, flexspi_nor_config_t * config, uint32_t address))0x002104a9U)
67 /*******************************************************************************
68  * Codes
69  ******************************************************************************/
70 
71 /*******************************************************************************
72  * ROM FLEXSPI NOR driver
73  ******************************************************************************/
74 #if defined(FSL_FEATURE_BOOT_ROM_HAS_ROMAPI) && FSL_FEATURE_BOOT_ROM_HAS_ROMAPI
75 
76 /*!
77  * @brief Initialize Serial NOR devices via FLEXSPI.
78  *
79  * @param instance storage the instance of FLEXSPI.
80  * @param config A pointer to the storage for the driver runtime state.
81  */
ROM_FLEXSPI_NorFlash_Init(uint32_t instance,flexspi_nor_config_t * config)82 status_t ROM_FLEXSPI_NorFlash_Init(uint32_t instance, flexspi_nor_config_t *config)
83 {
84     return g_bootloaderTree->flexSpiNorDriver->init(instance, config);
85 }
86 
87 /*!
88  * @brief Program data to Serial NOR via FLEXSPI.
89  *
90  * @param instance storage the instance of FLEXSPI.
91  * @param config  A pointer to the storage for the driver runtime state.
92  * @param dstAddr A pointer to the desired flash memory to be programmed.
93  * @param src A pointer to the source buffer of data that is to be programmed
94  *            into the NOR flash.
95  */
ROM_FLEXSPI_NorFlash_ProgramPage(uint32_t instance,flexspi_nor_config_t * config,uint32_t dstAddr,const uint32_t * src)96 status_t ROM_FLEXSPI_NorFlash_ProgramPage(uint32_t instance,
97                                           flexspi_nor_config_t *config,
98                                           uint32_t dstAddr,
99                                           const uint32_t *src)
100 {
101     return g_bootloaderTree->flexSpiNorDriver->program(instance, config, dstAddr, src);
102 }
103 
104 /*!
105  * @brief Erase Flash Region specified by address and length.
106  *
107  * @param instance storage the index of FLEXSPI.
108  * @param config A pointer to the storage for the driver runtime state.
109  * @param start The start address of the desired NOR flash memory to be erased.
110  * @param length The length, given in bytes to be erased.
111  */
ROM_FLEXSPI_NorFlash_Erase(uint32_t instance,flexspi_nor_config_t * config,uint32_t start,uint32_t length)112 status_t ROM_FLEXSPI_NorFlash_Erase(uint32_t instance, flexspi_nor_config_t *config, uint32_t start, uint32_t length)
113 {
114     return g_bootloaderTree->flexSpiNorDriver->erase(instance, config, start, length);
115 }
116 
117 #if defined(FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_SECTOR) && FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_SECTOR
118 /*!
119  * @brief Erase one sector specified by address.
120  *
121  * @param instance storage the index of FLEXSPI.
122  * @param config A pointer to the storage for the driver runtime state.
123  * @param start The start address of the desired NOR flash memory to be erased.
124  */
ROM_FLEXSPI_NorFlash_EraseSector(uint32_t instance,flexspi_nor_config_t * config,uint32_t start)125 status_t ROM_FLEXSPI_NorFlash_EraseSector(uint32_t instance, flexspi_nor_config_t *config, uint32_t start)
126 {
127     return api_flexspi_nor_erase_sector(instance, config, start);
128 }
129 #endif /* FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_SECTOR */
130 
131 #if defined(FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_BLOCK) && FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_BLOCK
132 /*!
133  * @brief Erase one block specified by address.
134  *
135  * @param instance storage the index of FLEXSPI.
136  * @param config A pointer to the storage for the driver runtime state.
137  * @param start The start address of the desired NOR flash memory to be erased.
138  */
ROM_FLEXSPI_NorFlash_EraseBlock(uint32_t instance,flexspi_nor_config_t * config,uint32_t start)139 status_t ROM_FLEXSPI_NorFlash_EraseBlock(uint32_t instance, flexspi_nor_config_t *config, uint32_t start)
140 {
141     return api_flexspi_nor_erase_block(instance, config, start);
142 }
143 #endif /* FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_ERASE_BLOCK */
144 
145 #if defined(FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_CMD_XFER) && FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_CMD_XFER
146 /*! @brief FLEXSPI command */
ROM_FLEXSPI_NorFlash_CommandXfer(uint32_t instance,flexspi_xfer_t * xfer)147 status_t ROM_FLEXSPI_NorFlash_CommandXfer(uint32_t instance, flexspi_xfer_t *xfer)
148 {
149     return g_bootloaderTree->flexSpiNorDriver->xfer(instance, xfer);
150 }
151 #endif /* FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_CMD_XFER */
152 
153 #if defined(FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_UPDATE_LUT) && FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_UPDATE_LUT
154 /*! @brief Configure FLEXSPI Lookup table. */
ROM_FLEXSPI_NorFlash_UpdateLut(uint32_t instance,uint32_t seqIndex,const uint32_t * lutBase,uint32_t seqNumber)155 status_t ROM_FLEXSPI_NorFlash_UpdateLut(uint32_t instance,
156                                         uint32_t seqIndex,
157                                         const uint32_t *lutBase,
158                                         uint32_t seqNumber)
159 {
160     return g_bootloaderTree->flexSpiNorDriver->update_lut(instance, seqIndex, lutBase, seqNumber);
161 }
162 #endif /* FSL_ROM_FLEXSPINOR_API_HAS_FEATURE_UPDATE_LUT */
163 
164 /*! @brief Software reset for the FLEXSPI logic. */
ROM_FLEXSPI_NorFlash_ClearCache(uint32_t instance)165 void ROM_FLEXSPI_NorFlash_ClearCache(uint32_t instance)
166 {
167     g_bootloaderTree->flexSpiNorDriver->clear_cache(instance);
168 }
169 
170 #endif /* FSL_FEATURE_BOOT_ROM_HAS_ROMAPI */
171