1 /* SPDX-License-Identifier: BSD-3-Clause */
3 /******************************************************************************
5 * Copyright (C) 2016-2018 Texas Instruments Incorporated - http://www.ti.com/
7 ******************************************************************************/
9 #ifndef OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H
10 #define OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H
21 /* Location of flash in memory map */
22 #define FLASHMEM_BASE 0
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]))
29 #if defined(DEVICE_CC26X2)
31 /* Agama (CC26x2) specific definitions */
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
38 /* Bootloader Configuration */
39 #define CCFG_O_BL_CONFIG 0x00001FD8
41 #elif defined(DEVICE_CC26X0)
43 /* Chameleon (CC26x0) specific definitions */
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
50 /* Bootloader Configuration */
51 #define CCFG_O_BL_CONFIG 0x00000FD8
54 #error No DEVICE defined.
57 /******************************************************************************
59 * Values that can be returned from the API functions
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 */
69 /******************************************************************************
71 * Define used by the flash programming and erase functions
73 ******************************************************************************/
74 #define ADDR_OFFSET (0x1F800000 - FLASHMEM_BASE)
76 /******************************************************************************
78 * Define used for access to factory configuration area.
80 ******************************************************************************/
81 #define FCFG1_OFFSET 0x1000
83 /******************************************************************************
85 * Define for the clock frequency input to the flash module in number of MHz
87 ******************************************************************************/
88 #define FLASH_MODULE_CLK_FREQ 48
90 /******************************************************************************
92 * Defined values for Flash State Machine commands
94 ******************************************************************************/
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;
108 /******************************************************************************
110 * Defines for values written to the FLASH_O_FSM_WR_ENA register
112 ******************************************************************************/
113 #define FSM_REG_WRT_ENABLE 5
114 #define FSM_REG_WRT_DISABLE 2
116 /******************************************************************************
118 * Defines for the bank power mode field the FLASH_O_FBFALLBACK register
120 ******************************************************************************/
121 #define FBFALLBACK_SLEEP 0
122 #define FBFALLBACK_DEEP_STDBY 1
123 #define FBFALLBACK_ACTIVE 3
125 /******************************************************************************
127 * Defines for the bank grace period and pump grace period
129 ******************************************************************************/
130 #define FLASH_BAGP 0x14
131 #define FLASH_PAGP 0x14
133 /******************************************************************************
135 * Defines for the FW flag bits in the FLASH_O_FWFLAG register
137 ******************************************************************************/
138 #define FW_WRT_TRIMMED 0x00000001
140 /******************************************************************************
142 * Defines used by the flash programming functions
144 ******************************************************************************/
145 typedef volatile uint8_t fwp_write_byte;
146 #define FWPWRITE_BYTE_ADDRESS \
147 ((fwp_write_byte *)((FLASH_BASE + FLASH_O_FWPWRITE0)))
149 /******************************************************************************
151 * Define for FSM command execution
153 ******************************************************************************/
154 #define FLASH_CMD_EXEC 0x15
156 /******************************************************************************
158 * Get size of a flash sector in number of bytes.
160 * This function will return the size of a flash sector in number of bytes.
162 * Returns size of a flash sector in number of bytes.
164 ******************************************************************************/
165 static inline uint32_t flash_sector_size_get(void)
167 uint32_t sector_size_in_kbyte;
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;
173 /* Return flash sector size in number of bytes. */
174 return sector_size_in_kbyte * 1024;
177 /******************************************************************************
179 * Get the size of the flash.
181 * This function returns the size of the flash main bank in number of bytes.
183 * Returns the flash size in number of bytes.
185 ******************************************************************************/
186 static inline uint32_t flash_size_get(void)
188 uint32_t num_of_sectors;
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;
195 /* Return flash size in number of bytes */
196 return num_of_sectors * flash_sector_size_get();
199 /******************************************************************************
201 * Checks if the Flash state machine has detected an error.
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.
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.
211 * Returns status of Flash state machine:
212 * FAPI_STATUS_FSM_ERROR
213 * FAPI_STATUS_SUCCESS
215 ******************************************************************************/
216 static inline uint32_t flash_check_fsm_for_error(void)
218 if (HWREG(FLASH_BASE + FLASH_O_FMSTAT) & FLASH_FMSTAT_CSTAT)
219 return FAPI_STATUS_FSM_ERROR;
221 return FAPI_STATUS_SUCCESS;
224 /******************************************************************************
226 * Checks if the Flash state machine is ready.
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.
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.
236 * Returns readiness status of Flash state machine:
237 * FAPI_STATUS_FSM_READY
238 * FAPI_STATUS_FSM_BUSY
240 ******************************************************************************/
241 static inline uint32_t flash_check_fsm_for_ready(void)
243 if (HWREG(FLASH_BASE + FLASH_O_STAT) & FLASH_STAT_BUSY)
244 return FAPI_STATUS_FSM_BUSY;
246 return FAPI_STATUS_FSM_READY;
249 /******************************************************************************
251 * Erase a flash sector.
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.
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
265 * sector_address is the starting address in flash of the sector to be
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.
273 ******************************************************************************/
274 extern uint32_t flash_sector_erase(uint32_t sector_address);
276 /******************************************************************************
278 * Erase all unprotected sectors in the flash main bank.
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.
290 * force_precondition controls if erase precondition should be forced.
292 * Returns the status of the sector erase:
293 * FAPI_STATUS_SUCCESS : Success
294 * FAPI_STATUS_FSM_ERROR : Erase error was encountered.
296 ******************************************************************************/
297 extern uint32_t flash_bank_erase(bool force_precondition);
299 /******************************************************************************
301 * Programs unprotected main bank flash sectors.
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.
311 * This function will not return until the data has been programmed or an
312 * programming error has occurred.
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
318 * The data_buffer pointer cannot point to flash.
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.
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.
329 ******************************************************************************/
330 extern uint32_t flash_program(uint8_t *data_buffer, uint32_t address,
333 /******************************************************************************
335 * Disables all sectors for erase and programming on the active bank.
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
342 * Please note that code can not execute in flash while any part of the flash
343 * is being programmed or erased.
345 ******************************************************************************/
346 extern void flash_disable_sectors_for_write(void);
352 #endif /* #ifndef OPENOCD_LOADERS_FLASH_CC26XX_FLASH_H */