flash/nor: Add support for TI CC26xx/CC13xx flash
[fw/openocd] / contrib / loaders / flash / cc26xx / main.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 *  Redistributions of source code must retain the above copyright
10 *  notice, this list of conditions and the following disclaimer.
11 *
12 *  Redistributions in binary form must reproduce the above copyright
13 *  notice, this list of conditions and the following disclaimer in the
14 *  documentation and/or other materials provided with the
15 *  distribution.
16 *
17 *  Neither the name of Texas Instruments Incorporated nor the names of
18 *  its contributors may be used to endorse or promote products derived
19 *  from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 ******************************************************************************/
34
35 #include <stdint.h>
36 #include <stdbool.h>
37 #include "flashloader.h"
38
39 /* Data buffers used by host to communicate with flashloader */
40
41 /* Flashloader parameter structure. */
42 __attribute__ ((section(".buffers.g_cfg")))
43 volatile struct flash_params g_cfg[2];
44 /* Data buffer 1. */
45 __attribute__ ((section(".buffers.g_buf1")))
46 uint8_t g_buf1[BUFFER_LEN];
47 /* Data buffer 2. */
48 __attribute__ ((section(".buffers.g_buf2")))
49 uint8_t g_buf2[BUFFER_LEN];
50
51 /* Buffer used for program with retain feature */
52 __attribute__ ((section(".buffers.g_retain_buf")))
53 uint8_t g_retain_buf[BUFFER_LEN];
54
55 uint32_t g_curr_buf; /* Current buffer used. */
56 uint32_t g_vims_ctl; /* Saved flash cache state. */
57
58 /******************************************************************************
59 *
60 * This function stores the current VIMS configuration before
61 * - disabling VIMS flash cache
62 * - flushing the flash line buffers.
63 *
64 * Note Not using driverlib calls because it requires using "NO_ROM" define in
65 *      order to work for both Cha. R1 and R2 using the same code. Manually
66 *      doing the steps to minimize code footprint.
67 *
68 ******************************************************************************/
69 static void disable_flash_cache()
70 {
71         /* 1. Make sure VIMS is not currently changing mode (VIMS:STAT register) */
72         while ((HWREG(0x40034000) & 0x00000008) == 0x8)
73                 ;
74
75         /* Save current VIMS:CTL state */
76         g_vims_ctl = HWREG(0x40034004);
77
78         /* 2. Set VIMS mode to OFF and disable flash line buffers */
79         uint32_t new_vims_ctl = g_vims_ctl | 0x33;
80         HWREG(0x40034004) = new_vims_ctl;
81
82         /* 3. Wait for VIMS to have changed mode (VIMS:STAT register) */
83         while ((HWREG(0x40034000) & 0x00000008) == 0x8)
84                 ;
85 }
86
87 /******************************************************************************
88 *
89 * This function restores the VIMS configuration saved off by
90 * disable_flash_cache().
91 *
92 * Note Not using driverlib calls because it requires using "NO_ROM" define in
93 *      order to work for both Cha. R1 and R2 using the same code. Manually
94 *      doing the steps to minimize code footprint.
95 *
96 ******************************************************************************/
97 static void restore_cache_state()
98 {
99         HWREG(0x40034004) = g_vims_ctl;
100
101         /* Wait for VIMS to have changed mode (VIMS:STAT register) */
102         while ((HWREG(0x40034000) & 0x00000008) == 0x8)
103                 ;
104 }
105
106 /******************************************************************************
107 *
108 * CC13xx/CC26xx flashloader main function.
109 *
110 ******************************************************************************/
111 int main(void)
112 {
113         flashloader_init((struct flash_params *)g_cfg, g_buf1, g_buf2);
114
115         g_curr_buf = 0; /* start with the first buffer */
116         uint32_t status;
117
118         while (1) {
119                 /* Wait for host to signal buffer is ready */
120                 while (g_cfg[g_curr_buf].full == BUFFER_EMPTY)
121                         ;
122
123                 disable_flash_cache();
124
125                 /* Perform requested task */
126                 switch (g_cfg[g_curr_buf].cmd) {
127                         case CMD_ERASE_ALL:
128                                 status = flashloader_erase_all();
129                                 break;
130                         case CMD_PROGRAM:
131                                 status =
132                                         flashloader_program(
133                                                 (uint8_t *)g_cfg[g_curr_buf].buf_addr,
134                                                 g_cfg[g_curr_buf].dest, g_cfg[g_curr_buf].len);
135                                 break;
136                         case CMD_ERASE_AND_PROGRAM:
137                                 status =
138                                         flashloader_erase_and_program(
139                                                 (uint8_t *)g_cfg[g_curr_buf].buf_addr,
140                                                 g_cfg[g_curr_buf].dest, g_cfg[g_curr_buf].len);
141                                 break;
142                         case CMD_ERASE_AND_PROGRAM_WITH_RETAIN:
143                                 status =
144                                         flashloader_program_with_retain(
145                                                 (uint8_t *)g_cfg[g_curr_buf].buf_addr,
146                                                 g_cfg[g_curr_buf].dest, g_cfg[g_curr_buf].len);
147                                 break;
148                         case CMD_ERASE_SECTORS:
149                                 status =
150                                         flashloader_erase_sectors(g_cfg[g_curr_buf].dest,
151                                                 g_cfg[g_curr_buf].len);
152                                 break;
153                         default:
154                                 status = STATUS_FAILED_UNKNOWN_COMMAND;
155                                 break;
156                 }
157
158                 restore_cache_state();
159
160                 /* Enter infinite loop on error condition */
161                 if (status != STATUS_OK) {
162                         g_cfg[g_curr_buf].full = status;
163                         while (1)
164                                 ;
165                 }
166
167                 /* Mark current task complete, and begin looking at next buffer */
168                 g_cfg[g_curr_buf].full = BUFFER_EMPTY;
169                 g_curr_buf ^= 1;
170         }
171 }
172
173 void _exit(int status)
174 {
175         /* Enter infinite loop on hitting an exit condition */
176         (void)status; /* Unused parameter */
177         while (1)
178                 ;
179 }