contrib: replace the BSD-3-Clause license tag
[fw/openocd] / contrib / loaders / flash / cc26xx / flash.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2
3 /******************************************************************************
4 *
5 * Copyright (C) 2016-2018 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 ******************************************************************************/
8
9 #ifndef OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H
10 #define OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H
11
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include "hw_regs.h"
20
21 /* Location of flash in memory map */
22 #define FLASHMEM_BASE 0
23
24 /* Defines to access flash API calls in ROM */
25 #define ROM_API_TABLE       ((uint32_t *) 0x10000180)
26 #define ROM_VERSION         (ROM_API_TABLE[0])
27 #define ROM_API_FLASH_TABLE ((uint32_t *) (ROM_API_TABLE[10]))
28
29 #if defined(DEVICE_CC26X2)
30
31 /* Agama (CC26x2) specific definitions */
32
33 #define FLASH_ERASE_SIZE       8192
34 /* Agama (and Agama 1M) has a maximum of 132 flash sectors (1056KB / 8KB) */
35 #define FLASH_MAX_SECTOR_COUNT 132
36 #define FLASH_SECTOR_BASE_M    0xFFFFE000
37
38 /* Bootloader Configuration */
39 #define CCFG_O_BL_CONFIG 0x00001FD8
40
41 #elif defined(DEVICE_CC26X0)
42
43 /* Chameleon (CC26x0) specific definitions */
44
45 #define FLASH_ERASE_SIZE       4096
46 /* Chameleon has a maximum of 32 flash sectors (128KB / 4KB) */
47 #define FLASH_MAX_SECTOR_COUNT 32
48 #define FLASH_SECTOR_BASE_M    0xFFFFF000
49
50 /* Bootloader Configuration */
51 #define CCFG_O_BL_CONFIG 0x00000FD8
52
53 #else
54 #error No DEVICE defined.
55 #endif
56
57 /******************************************************************************
58 *
59 * Values that can be returned from the API functions
60 *
61 ******************************************************************************/
62 #define FAPI_STATUS_SUCCESS   0x00000000 /* Function completed successfully */
63 #define FAPI_STATUS_FSM_BUSY  0x00000001 /* FSM is Busy */
64 #define FAPI_STATUS_FSM_READY 0x00000002 /* FSM is Ready */
65 #define FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH \
66                                                           0x00000003 /* Incorrect parameter value */
67 #define FAPI_STATUS_FSM_ERROR 0x00000004 /* Operation failed */
68
69 /******************************************************************************
70 *
71 * Define used by the flash programming and erase functions
72 *
73 ******************************************************************************/
74 #define ADDR_OFFSET (0x1F800000 - FLASHMEM_BASE)
75
76 /******************************************************************************
77 *
78 * Define used for access to factory configuration area.
79 *
80 ******************************************************************************/
81 #define FCFG1_OFFSET 0x1000
82
83 /******************************************************************************
84 *
85 * Define for the clock frequency input to the flash module in number of MHz
86 *
87 ******************************************************************************/
88 #define FLASH_MODULE_CLK_FREQ 48
89
90 /******************************************************************************
91 *
92 * Defined values for Flash State Machine commands
93 *
94 ******************************************************************************/
95 typedef enum {
96         FAPI_PROGRAM_DATA    = 0x0002, /* Program data. */
97         FAPI_ERASE_SECTOR    = 0x0006, /* Erase sector. */
98         FAPI_ERASE_BANK      = 0x0008, /* Erase bank. */
99         FAPI_VALIDATE_SECTOR = 0x000E, /* Validate sector. */
100         FAPI_CLEAR_STATUS    = 0x0010, /* Clear status. */
101         FAPI_PROGRAM_RESUME  = 0x0014, /* Program resume. */
102         FAPI_ERASE_RESUME    = 0x0016, /* Erase resume. */
103         FAPI_CLEAR_MORE      = 0x0018, /* Clear more. */
104         FAPI_PROGRAM_SECTOR  = 0x0020, /* Program sector. */
105         FAPI_ERASE_OTP       = 0x0030  /* Erase OTP. */
106 } flash_state_command_t;
107
108 /******************************************************************************
109 *
110 * Defines for values written to the FLASH_O_FSM_WR_ENA register
111 *
112 ******************************************************************************/
113 #define FSM_REG_WRT_ENABLE  5
114 #define FSM_REG_WRT_DISABLE 2
115
116 /******************************************************************************
117 *
118 * Defines for the bank power mode field the FLASH_O_FBFALLBACK register
119 *
120 ******************************************************************************/
121 #define FBFALLBACK_SLEEP      0
122 #define FBFALLBACK_DEEP_STDBY 1
123 #define FBFALLBACK_ACTIVE     3
124
125 /******************************************************************************
126 *
127 * Defines for the bank grace period and pump grace period
128 *
129 ******************************************************************************/
130 #define FLASH_BAGP 0x14
131 #define FLASH_PAGP 0x14
132
133 /******************************************************************************
134 *
135 * Defines for the FW flag bits in the FLASH_O_FWFLAG register
136 *
137 ******************************************************************************/
138 #define FW_WRT_TRIMMED 0x00000001
139
140 /******************************************************************************
141 *
142 * Defines used by the flash programming functions
143 *
144 ******************************************************************************/
145 typedef volatile uint8_t fwp_write_byte;
146 #define FWPWRITE_BYTE_ADDRESS \
147         ((fwp_write_byte *)((FLASH_BASE + FLASH_O_FWPWRITE0)))
148
149 /******************************************************************************
150 *
151 * Define for FSM command execution
152 *
153 ******************************************************************************/
154 #define FLASH_CMD_EXEC 0x15
155
156 /******************************************************************************
157 *
158 * Get size of a flash sector in number of bytes.
159 *
160 * This function will return the size of a flash sector in number of bytes.
161 *
162 * Returns size of a flash sector in number of bytes.
163 *
164 ******************************************************************************/
165 static inline uint32_t flash_sector_size_get(void)
166 {
167         uint32_t sector_size_in_kbyte;
168
169         sector_size_in_kbyte = (HWREG(FLASH_BASE + FLASH_O_FCFG_B0_SSIZE0) &
170                 FLASH_FCFG_B0_SSIZE0_B0_SECT_SIZE_M) >>
171                 FLASH_FCFG_B0_SSIZE0_B0_SECT_SIZE_S;
172
173         /* Return flash sector size in number of bytes. */
174         return sector_size_in_kbyte * 1024;
175 }
176
177 /******************************************************************************
178 *
179 * Get the size of the flash.
180 *
181 * This function returns the size of the flash main bank in number of bytes.
182 *
183 * Returns the flash size in number of bytes.
184 *
185 ******************************************************************************/
186 static inline uint32_t flash_size_get(void)
187 {
188         uint32_t num_of_sectors;
189
190         /* Get number of flash sectors */
191         num_of_sectors = (HWREG(FLASH_BASE + FLASH_O_FLASH_SIZE) &
192                                                 FLASH_FLASH_SIZE_SECTORS_M) >>
193                                                 FLASH_FLASH_SIZE_SECTORS_S;
194
195         /* Return flash size in number of bytes */
196         return num_of_sectors * flash_sector_size_get();
197 }
198
199 /******************************************************************************
200 *
201 * Checks if the Flash state machine has detected an error.
202 *
203 * This function returns the status of the Flash State Machine indicating if
204 * an error is detected or not. Primary use is to check if an Erase or
205 * Program operation has failed.
206 *
207 * Please note that code can not execute in flash while any part of the flash
208 * is being programmed or erased. This function must be called from ROM or
209 * SRAM while any part of the flash is being programmed or erased.
210 *
211 * Returns status of Flash state machine:
212 * FAPI_STATUS_FSM_ERROR
213 * FAPI_STATUS_SUCCESS
214 *
215 ******************************************************************************/
216 static inline uint32_t flash_check_fsm_for_error(void)
217 {
218         if (HWREG(FLASH_BASE + FLASH_O_FMSTAT) & FLASH_FMSTAT_CSTAT)
219                 return FAPI_STATUS_FSM_ERROR;
220         else
221                 return FAPI_STATUS_SUCCESS;
222 }
223
224 /******************************************************************************
225 *
226 * Checks if the Flash state machine is ready.
227 *
228 * This function returns the status of the Flash State Machine indicating if
229 * it is ready to accept a new command or not. Primary use is to check if an
230 * Erase or Program operation has finished.
231 *
232 * Please note that code can not execute in flash while any part of the flash
233 * is being programmed or erased. This function must be called from ROM or
234 * SRAM while any part of the flash is being programmed or erased.
235 *
236 * Returns readiness status of Flash state machine:
237 * FAPI_STATUS_FSM_READY
238 * FAPI_STATUS_FSM_BUSY
239 *
240 ******************************************************************************/
241 static inline uint32_t flash_check_fsm_for_ready(void)
242 {
243         if (HWREG(FLASH_BASE + FLASH_O_STAT) & FLASH_STAT_BUSY)
244                 return FAPI_STATUS_FSM_BUSY;
245         else
246                 return FAPI_STATUS_FSM_READY;
247 }
248
249 /******************************************************************************
250 *
251 * Erase a flash sector.
252 *
253 * This function will erase the specified flash sector. The function will
254 * not return until the flash sector has been erased or an error condition
255 * occurred. If flash top sector is erased the function will program the
256 * device security data bytes with default values. The device security
257 * data located in the customer configuration area of the flash top sector,
258 * must have valid values at all times. These values affect the configuration
259 * of the device during boot.
260 *
261 * Please note that code can not execute in flash while any part of the flash
262 * is being programmed or erased. This function must only be executed from ROM
263 * or SRAM.
264 *
265 * sector_address is the starting address in flash of the sector to be
266 * erased.
267 *
268 * Returns the status of the sector erase:
269 * FAPI_STATUS_SUCCESS                     : Success.
270 * FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH : Invalid argument.
271 * FAPI_STATUS_FSM_ERROR                   : Programming error was encountered.
272 *
273 ******************************************************************************/
274 extern uint32_t flash_sector_erase(uint32_t sector_address);
275
276 /******************************************************************************
277 *
278 * Erase all unprotected sectors in the flash main bank.
279 *
280 * This function will erase all unprotected flash sectors. The function will
281 * not return until the flash sectors has been erased or an error condition
282 * occurred. Since the flash top sector is erased the function will program
283 * the device security data bytes with default values. The device security
284 * data located in the customer configuration area of the flash top sector,
285 * must have valid values at all times. These values affect the configuration
286 * of the device during boot. The execution time of the operation increases if
287 * erase precondition is forced. This will cause the flash module to first
288 * program all 1 bits in the bank to 0 before the actual erase is started.
289 *
290 * force_precondition controls if erase precondition should be forced.
291 *
292 * Returns the status of the sector erase:
293 * FAPI_STATUS_SUCCESS   : Success
294 * FAPI_STATUS_FSM_ERROR : Erase error was encountered.
295 *
296 ******************************************************************************/
297 extern uint32_t flash_bank_erase(bool force_precondition);
298
299 /******************************************************************************
300 *
301 * Programs unprotected main bank flash sectors.
302 *
303 * This function will program a sequence of bytes into the on-chip flash.
304 * Programming each location consists of the result of an AND operation
305 * of the new data and the existing data; in other words bits that contain
306 * 1 can remain 1 or be changed to 0, but bits that are 0 cannot be changed
307 * to 1.  Therefore, a byte can be programmed multiple times as long as these
308 * rules are followed; if a program operation attempts to change a 0 bit to
309 * a 1 bit, that bit will not have its value changed.
310 *
311 * This function will not return until the data has been programmed or an
312 * programming error has occurred.
313 *
314 * Please note that code can not execute in flash while any part of the flash
315 * is being programmed or erased. This function must only be executed from ROM
316 * or SRAM.
317 *
318 * The data_buffer pointer cannot point to flash.
319 *
320 * data_buffer is a pointer to the data to be programmed.
321 * address is the starting address in flash to be programmed.
322 * count is the number of bytes to be programmed.
323 *
324 * Returns status of the flash programming:
325 * FAPI_STATUS_SUCCESS                     : Success.
326 * FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH : Too many bytes were requested.
327 * FAPI_STATUS_FSM_ERROR                   : Programming error was encountered.
328 *
329 ******************************************************************************/
330 extern uint32_t flash_program(uint8_t *data_buffer, uint32_t address,
331         uint32_t count);
332
333 /******************************************************************************
334 *
335 * Disables all sectors for erase and programming on the active bank.
336 *
337 * This function disables all sectors for erase and programming on the active
338 * bank and enables the Idle Reading Power reduction mode if no low power
339 * mode is configured. Furthermore, an additional level of protection from
340 * erase is enabled.
341 *
342 * Please note that code can not execute in flash while any part of the flash
343 * is being programmed or erased.
344 *
345 ******************************************************************************/
346 extern void flash_disable_sectors_for_write(void);
347
348 #ifdef __cplusplus
349 }
350 #endif
351
352 #endif /* #ifndef OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H */