1 /*!
2     \file    gd32e10x_fmc.h
3     \brief   definitions for the FMC
4 
5     \version 2017-12-26, V1.0.0, firmware for GD32E10x
6     \version 2020-05-11, V1.0.1, firmware for GD32E10x
7     \version 2020-09-30, V1.1.0, firmware for GD32E10x
8     \version 2020-12-31, V1.2.0, firmware for GD32E10x
9     \version 2022-06-30, V1.3.0, firmware for GD32E10x
10 */
11 
12 /*
13     Copyright (c) 2022, GigaDevice Semiconductor Inc.
14 
15     Redistribution and use in source and binary forms, with or without modification,
16 are permitted provided that the following conditions are met:
17 
18     1. Redistributions of source code must retain the above copyright notice, this
19        list of conditions and the following disclaimer.
20     2. Redistributions in binary form must reproduce the above copyright notice,
21        this list of conditions and the following disclaimer in the documentation
22        and/or other materials provided with the distribution.
23     3. Neither the name of the copyright holder nor the names of its contributors
24        may be used to endorse or promote products derived from this software without
25        specific prior written permission.
26 
27     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 OF SUCH DAMAGE.
37 */
38 
39 #ifndef GD32E10X_FMC_H
40 #define GD32E10X_FMC_H
41 
42 #include "gd32e10x.h"
43 
44 /* FMC and option bytes definition */
45 #define FMC                        FMC_BASE                       /*!< FMC register base address */
46 #define OB                         OB_BASE                        /*!< option bytes base address */
47 
48 /* registers definitions */
49 #define FMC_WS                     REG32((FMC) + 0x00000000U)     /*!< FMC wait state register */
50 #define FMC_KEY                    REG32((FMC) + 0x00000004U)     /*!< FMC unlock key register */
51 #define FMC_OBKEY                  REG32((FMC) + 0x00000008U)     /*!< FMC option bytes unlock key register */
52 #define FMC_STAT                   REG32((FMC) + 0x0000000CU)     /*!< FMC status register */
53 #define FMC_CTL                    REG32((FMC) + 0x00000010U)     /*!< FMC control register */
54 #define FMC_ADDR                   REG32((FMC) + 0x00000014U)     /*!< FMC address register */
55 #define FMC_OBSTAT                 REG32((FMC) + 0x0000001CU)     /*!< FMC option bytes status register */
56 #define FMC_WP                     REG32((FMC) + 0x00000020U)     /*!< FMC erase/program protection register */
57 #define FMC_PID                    REG32((FMC) + 0x00000100U)     /*!< FMC product ID register */
58 
59 #define OP_BYTE(x)                 REG32((OB) + ((uint32_t)((uint32_t)0x04U * (x))))     /*!< option byte value */
60 #define OB_SPC_USER                REG32((OB) + 0x00000000U)      /*!< option byte security protection value and user value */
61 #define OB_DATA                    REG32((OB) + 0x00000004U)      /*!< option byte data value*/
62 #define OB_WP0                     REG32((OB) + 0x00000008U)      /*!< option bytes write protection value 0 */
63 #define OB_WP1                     REG32((OB) + 0x0000000CU)      /*!< option bytes write protection value 1 */
64 
65 /* bits definitions */
66 /* FMC_WS */
67 #define FMC_WS_WSCNT               BITS(0,2)                      /*!< wait state counter */
68 #define FMC_WS_PFEN                BIT(4)                         /*!< pre-fetch enable */
69 #define FMC_WS_ICEN                BIT(9)                         /*!< IBUS cache enable */
70 #define FMC_WS_DCEN                BIT(10)                        /*!< DBUS cache enable */
71 #define FMC_WS_ICRST               BIT(11)                        /*!< IBUS cache reset */
72 #define FMC_WS_DCRST               BIT(12)                        /*!< DBUS cache reset */
73 #define FMC_WS_PGW                 BIT(15)                        /*!< program width to flash memory */
74 
75 /* FMC_KEY */
76 #define FMC_KEY_KEY                BITS(0,31)                     /*!< FMC_CTL unlock key bits */
77 
78 /* FMC_OBKEY */
79 #define FMC_OBKEY_OBKEY            BITS(0,31)                     /*!< option bytes unlock key bits */
80 
81 /* FMC_STAT */
82 #define FMC_STAT_BUSY              BIT(0)                         /*!< flash busy flag bit */
83 #define FMC_STAT_PGERR             BIT(2)                         /*!< flash program error flag bit */
84 #define FMC_STAT_PGAERR            BIT(3)                         /*!< flash program alignment error flag bit */
85 #define FMC_STAT_WPERR             BIT(4)                         /*!< erase/program protection error flag bit */
86 #define FMC_STAT_ENDF              BIT(5)                         /*!< end of operation flag bit */
87 
88 /* FMC_CTL */
89 #define FMC_CTL_PG                 BIT(0)                         /*!< main flash program command bit */
90 #define FMC_CTL_PER                BIT(1)                         /*!< main flash page erase command bit */
91 #define FMC_CTL_MER                BIT(2)                         /*!< main flash mass erase command bit */
92 #define FMC_CTL_OBPG               BIT(4)                         /*!< option bytes program command bit */
93 #define FMC_CTL_OBER               BIT(5)                         /*!< option bytes erase command bit */
94 #define FMC_CTL_START              BIT(6)                         /*!< send erase command to FMC bit */
95 #define FMC_CTL_LK                 BIT(7)                         /*!< FMC_CTL lock bit */
96 #define FMC_CTL_OBWEN              BIT(9)                         /*!< option bytes erase/program enable bit */
97 #define FMC_CTL_ERRIE              BIT(10)                        /*!< error interrupt enable bit */
98 #define FMC_CTL_ENDIE              BIT(12)                        /*!< end of operation interrupt enable bit */
99 
100 /* FMC_ADDR */
101 #define FMC_ADDR_ADDR              BITS(0,31)                     /*!< flash erase/program command address bits */
102 
103 /* FMC_OBSTAT */
104 #define FMC_OBSTAT_OBERR           BIT(0)                         /*!< option bytes read error bit. */
105 #define FMC_OBSTAT_SPC             BIT(1)                         /*!< option bytes security protection bit */
106 #define FMC_OBSTAT_USER            BITS(2,9)                      /*!< store USER of option bytes block after system reset */
107 #define FMC_OBSTAT_DATA            BITS(10,25)                    /*!< store DATA of option bytes block after system reset. */
108 
109 /* FMC_WP */
110 #define FMC_WP_WP                  BITS(0,31)                     /*!< store WP of option bytes block after system reset */
111 
112 /* FMC_PID */
113 #define FMC_PID_PID                BITS(0,31)                     /*!< product ID bits */
114 
115 /* constants definitions */
116 /* fmc state */
117 typedef enum
118 {
119     FMC_READY,                                                    /*!< the operation has been completed */
120     FMC_BUSY,                                                     /*!< the operation is in progress */
121     FMC_PGERR,                                                    /*!< program error */
122     FMC_PGAERR,                                                   /*!< program alignment error */
123     FMC_WPERR,                                                    /*!< erase/program protection error */
124     FMC_TOERR,                                                    /*!< timeout error */
125 }fmc_state_enum;
126 
127 /* FMC interrupt enable */
128 #define FMC_INT_ERR                FMC_CTL_ERRIE                  /*!< enable FMC error interrupt */
129 #define FMC_INT_END                FMC_CTL_ENDIE                  /*!< enable FMC end of program interrupt */
130 
131 /* FMC flags */
132 #define FMC_FLAG_BUSY              FMC_STAT_BUSY                  /*!< FMC busy flag */
133 #define FMC_FLAG_PGERR             FMC_STAT_PGERR                 /*!< FMC operation error flag */
134 #define FMC_FLAG_PGAERR            FMC_STAT_PGAERR                /*!< FMC program alignment error flag */
135 #define FMC_FLAG_WPERR             FMC_STAT_WPERR                 /*!< FMC erase/program protection error flag */
136 #define FMC_FLAG_END               FMC_STAT_ENDF                  /*!< FMC end of operation flag */
137 
138 /* FMC interrupt flags */
139 #define FMC_INT_FLAG_PGERR         FMC_STAT_PGERR                 /*!< FMC operation error interrupt flag */
140 #define FMC_INT_FLAG_PGAERR        FMC_STAT_PGAERR                /*!< FMC program alignment error interrupt flag */
141 #define FMC_INT_FLAG_WPERR         FMC_STAT_WPERR                 /*!< FMC erase/program protection error interrupt flag */
142 #define FMC_INT_FLAG_END           FMC_STAT_ENDF                  /*!< FMC end of operation interrupt flag */
143 
144 /* unlock key */
145 #define UNLOCK_KEY0                ((uint32_t)0x45670123U)        /*!< unlock key 0 */
146 #define UNLOCK_KEY1                ((uint32_t)0xCDEF89ABU)        /*!< unlock key 1 */
147 
148 /* FMC wait state counter */
149 #define WS_WSCNT(regval)           (BITS(0,2) & ((uint32_t)(regval)))
150 #define FMC_WAIT_STATE_0           WS_WSCNT(0)                    /*!< FMC 0 wait */
151 #define FMC_WAIT_STATE_1           WS_WSCNT(1)                    /*!< FMC 1 wait */
152 #define FMC_WAIT_STATE_2           WS_WSCNT(2)                    /*!< FMC 2 wait */
153 #define FMC_WAIT_STATE_3           WS_WSCNT(3)                    /*!< FMC 3 wait */
154 
155 /* adc_ctl1 register value */
156 #define FMC_PROG_W_64B             FMC_WS_PGW                     /*!< double word program */
157 #define FMC_PROG_W_32B             ((uint32_t)0x00000000U)        /*!< word program */
158 
159 /* read protect configure */
160 #define FMC_NSPC                   ((uint8_t)0xA5U)               /*!< no security protection */
161 #define FMC_USPC                   ((uint8_t)0xBBU)               /*!< under security protection */
162 
163 /* option byte address */
164 #define OB_SPC_USER_ADDRESS        ((uint32_t)0x1FFFF800U)        /*!< address of option byte security protection and user */
165 #define OB_DATA_ADDRESS            ((uint32_t)0x1FFFF804U)        /*!< address of option byte data */
166 #define OB_WP_ADDRESS0             ((uint32_t)0x1FFFF808U)        /*!< address0 of option byte write protection */
167 #define OB_WP_ADDRESS1             ((uint32_t)0x1FFFF80CU)        /*!< address1 of option byte write protection */
168 
169 /* option bytes software/hardware free watch dog timer */
170 #define OB_FWDGT_HW                ((uint8_t)0x00U)               /*!< hardware free watchdog */
171 #define OB_FWDGT_SW                ((uint8_t)0x01U)               /*!< software free watchdog */
172 
173 /* option bytes reset or not entering deep sleep mode */
174 #define OB_DEEPSLEEP_RST           ((uint8_t)0x00U)               /*!< generate a reset instead of entering deepsleep mode */
175 #define OB_DEEPSLEEP_NRST          ((uint8_t)0x02U)               /*!< no reset when entering deepsleep mode */
176 
177 /* option bytes reset or not entering standby mode */
178 #define OB_STDBY_RST               ((uint8_t)0x00U)               /*!< generate a reset instead of entering standby mode */
179 #define OB_STDBY_NRST              ((uint8_t)0x04U)               /*!< no reset when entering standby mode */
180 
181 /* option bytes write protection */
182 #define OB_WP_NONE                 ((uint32_t)0x00000000U)        /*!< disable all erase/program protection */
183 #define OB_WP_0                    ((uint32_t)0x00000001U)        /*!< erase/program protection of sector 0  */
184 #define OB_WP_1                    ((uint32_t)0x00000002U)        /*!< erase/program protection of sector 1  */
185 #define OB_WP_2                    ((uint32_t)0x00000004U)        /*!< erase/program protection of sector 2  */
186 #define OB_WP_3                    ((uint32_t)0x00000008U)        /*!< erase/program protection of sector 3  */
187 #define OB_WP_4                    ((uint32_t)0x00000010U)        /*!< erase/program protection of sector 4  */
188 #define OB_WP_5                    ((uint32_t)0x00000020U)        /*!< erase/program protection of sector 5  */
189 #define OB_WP_6                    ((uint32_t)0x00000040U)        /*!< erase/program protection of sector 6  */
190 #define OB_WP_7                    ((uint32_t)0x00000080U)        /*!< erase/program protection of sector 7  */
191 #define OB_WP_8                    ((uint32_t)0x00000100U)        /*!< erase/program protection of sector 8  */
192 #define OB_WP_9                    ((uint32_t)0x00000200U)        /*!< erase/program protection of sector 9  */
193 #define OB_WP_10                   ((uint32_t)0x00000400U)        /*!< erase/program protection of sector 10 */
194 #define OB_WP_11                   ((uint32_t)0x00000800U)        /*!< erase/program protection of sector 11 */
195 #define OB_WP_12                   ((uint32_t)0x00001000U)        /*!< erase/program protection of sector 12 */
196 #define OB_WP_13                   ((uint32_t)0x00002000U)        /*!< erase/program protection of sector 13 */
197 #define OB_WP_14                   ((uint32_t)0x00004000U)        /*!< erase/program protection of sector 14 */
198 #define OB_WP_15                   ((uint32_t)0x00008000U)        /*!< erase/program protection of sector 15 */
199 #define OB_WP_16                   ((uint32_t)0x00010000U)        /*!< erase/program protection of sector 16 */
200 #define OB_WP_17                   ((uint32_t)0x00020000U)        /*!< erase/program protection of sector 17 */
201 #define OB_WP_18                   ((uint32_t)0x00040000U)        /*!< erase/program protection of sector 18 */
202 #define OB_WP_19                   ((uint32_t)0x00080000U)        /*!< erase/program protection of sector 19 */
203 #define OB_WP_20                   ((uint32_t)0x00100000U)        /*!< erase/program protection of sector 20 */
204 #define OB_WP_21                   ((uint32_t)0x00200000U)        /*!< erase/program protection of sector 21 */
205 #define OB_WP_22                   ((uint32_t)0x00400000U)        /*!< erase/program protection of sector 22 */
206 #define OB_WP_23                   ((uint32_t)0x00800000U)        /*!< erase/program protection of sector 23 */
207 #define OB_WP_24                   ((uint32_t)0x01000000U)        /*!< erase/program protection of sector 24 */
208 #define OB_WP_25                   ((uint32_t)0x02000000U)        /*!< erase/program protection of sector 25 */
209 #define OB_WP_26                   ((uint32_t)0x04000000U)        /*!< erase/program protection of sector 26 */
210 #define OB_WP_27                   ((uint32_t)0x08000000U)        /*!< erase/program protection of sector 27 */
211 #define OB_WP_28                   ((uint32_t)0x10000000U)        /*!< erase/program protection of sector 28 */
212 #define OB_WP_29                   ((uint32_t)0x20000000U)        /*!< erase/program protection of sector 29 */
213 #define OB_WP_30                   ((uint32_t)0x40000000U)        /*!< erase/program protection of sector 30 */
214 #define OB_WP_31                   ((uint32_t)0x80000000U)        /*!< erase/program protection of sector 31 */
215 #define OB_WP_ALL                  ((uint32_t)0xFFFFFFFFU)        /*!< erase/program protection of all sectors */
216 
217 /* FMC timeout */
218 #define FMC_TIMEOUT_COUNT          ((uint32_t)0x00100000U)        /*!< FMC timeout count value */
219 
220 /* function declarations */
221 /* FMC main memory programming functions */
222 /* enable pre-fetch */
223 void fmc_prefetch_enable(void);
224 /* disable pre-fetch */
225 void fmc_prefetch_disable(void);
226 /* enable IBUS cache */
227 void fmc_ibus_enable(void);
228 /* disable IBUS cache */
229 void fmc_ibus_disable(void);
230 /* enable DBUS cache */
231 void fmc_dbus_enable(void);
232 /* disable DBUS cache */
233 void fmc_dbus_disable(void);
234 /* reset IBUS cache  */
235 void fmc_ibus_reset(void);
236 /* reset DBUS cache */
237 void fmc_dbus_reset(void);
238 /* set program width to flash memory */
239 void fmc_program_width_set(uint32_t pgw);
240 /* unlock the main FMC operation */
241 void fmc_unlock(void);
242 /* lock the main FMC operation */
243 void fmc_lock(void);
244 /* set the FMC wait state counter */
245 void fmc_wscnt_set(uint32_t wscnt);
246 /* FMC erase page */
247 fmc_state_enum fmc_page_erase(uint32_t page_address);
248 /* FMC erase whole chip */
249 fmc_state_enum fmc_mass_erase(void);
250 /* FMC program a double word at the corresponding address */
251 fmc_state_enum fmc_doubleword_program(uint32_t address, uint64_t data);
252 /* FMC program a word at the corresponding address */
253 fmc_state_enum fmc_word_program(uint32_t address, uint32_t data);
254 
255 /* FMC option bytes programming functions */
256 /* unlock the option bytes operation */
257 void ob_unlock(void);
258 /* lock the option bytes operation */
259 void ob_lock(void);
260 /* erase the option bytes */
261 fmc_state_enum ob_erase(void);
262 /* enable write protect */
263 fmc_state_enum ob_write_protection_enable(uint32_t ob_wp);
264 /* configure the option bytes security protection */
265 fmc_state_enum ob_security_protection_config(uint8_t ob_spc);
266 /* write the FMC option bytes */
267 fmc_state_enum ob_user_write(uint8_t ob_fwdgt, uint8_t ob_deepsleep, uint8_t ob_stdby);
268 /* program option bytes data */
269 fmc_state_enum ob_data_program(uint16_t ob_data);
270 /* get the FMC option bytes user */
271 uint8_t ob_user_get(void);
272 /* get OB_DATA in register FMC_OBSTAT */
273 uint16_t ob_data_get(void);
274 /* get the FMC option bytes write protection */
275 uint32_t ob_write_protection_get(void);
276 /* get option bytes security protection state */
277 FlagStatus ob_security_protection_flag_get(void);
278 
279 /* FMC interrupts and flags management functions */
280 /* check flag is set or not */
281 FlagStatus fmc_flag_get(uint32_t flag);
282 /* clear the FMC flag */
283 void fmc_flag_clear(uint32_t flag);
284 /* enable FMC interrupt */
285 void fmc_interrupt_enable(uint32_t interrupt);
286 /* disable FMC interrupt */
287 void fmc_interrupt_disable(uint32_t interrupt);
288 /* get FMC interrupt flag state */
289 FlagStatus fmc_interrupt_flag_get(uint32_t flag);
290 /* clear FMC interrupt flag state */
291 void fmc_interrupt_flag_clear(uint32_t flag);
292 
293 #endif /* GD32E10X_FMC_H */
294