flash: fix clang static analyzer build errors
[fw/openocd] / src / flash / nor / dsp5680xx_flash.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Rodrigo L. Rosa                                 *
3  *   rodrigorosa.LG@gmail.com                                              *
4  *                                                                         *
5  *   Based on a file written by:                                           *
6  *   Kevin McGuire                                                         *
7  *   Marcel Wijlaars                                                       *
8  *   Michael Ashton                                                        *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  ***************************************************************************/
23
24 /**
25  * @file   dsp5680xx_flash.c
26  * @author Rodrigo L. Rosa <rodrigorosa.LG@gmail.com>
27  * @date   Thu Jun  9 18:21:58 2011
28  *
29  * @brief  This file implements the basic functions to run flashing commands
30  * from the TCL interface.
31  * It allows the user to flash the Freescale 5680xx DSP.
32  *
33  *
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "imp.h"
41 #include <helper/binarybuffer.h>
42 #include <helper/time_support.h>
43 #include <target/algorithm.h>
44 #include <target/dsp5680xx.h>
45
46 static int dsp5680xx_build_sector_list(struct flash_bank *bank)
47 {
48         bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
49
50         for (unsigned int i = 0; i < bank->num_sectors; ++i) {
51                 bank->sectors[i].offset = i * HFM_SECTOR_SIZE;
52                 bank->sectors[i].size = HFM_SECTOR_SIZE;
53                 bank->sectors[i].is_erased = -1;
54                 bank->sectors[i].is_protected = -1;
55         }
56         LOG_USER("%s not tested yet.", __func__);
57         return ERROR_OK;
58
59 }
60
61 /* flash bank dsp5680xx 0 0 0 0 <target#> */
62 FLASH_BANK_COMMAND_HANDLER(dsp5680xx_flash_bank_command)
63 {
64         bank->base = HFM_FLASH_BASE_ADDR;
65         bank->size = HFM_SIZE_BYTES; /* top 4k not accessible */
66         bank->num_sectors = HFM_SECTOR_COUNT;
67         dsp5680xx_build_sector_list(bank);
68
69         return ERROR_OK;
70 }
71
72 /**
73  * A memory mapped register (PROT) holds information regarding sector protection.
74  * Protection refers to undesired core access.
75  * The value in this register is loaded from flash upon reset.
76  *
77  * @param bank
78  *
79  * @return
80  */
81 static int dsp5680xx_flash_protect_check(struct flash_bank *bank)
82 {
83         int retval = ERROR_OK;
84
85         uint16_t protected = 0;
86
87         retval = dsp5680xx_f_protect_check(bank->target, &protected);
88         if (retval != ERROR_OK) {
89                 for (int i = 0; i < HFM_SECTOR_COUNT; i++)
90                         bank->sectors[i].is_protected = -1;
91                 return ERROR_OK;
92         }
93         for (int i = 0; i < HFM_SECTOR_COUNT / 2; i++) {
94                 if (protected & 1) {
95                         bank->sectors[2 * i].is_protected = 1;
96                         bank->sectors[2 * i + 1].is_protected = 1;
97                 } else {
98                         bank->sectors[2 * i].is_protected = 0;
99                         bank->sectors[2 * i + 1].is_protected = 0;
100                 }
101                 protected = (protected >> 1);
102         }
103         return retval;
104 }
105
106 /**
107  * Protection functionality is not implemented.
108  * The current implementation applies/removes security on the chip.
109  * The chip is effectively secured/unsecured after the first reset
110  * following the execution of this function.
111  *
112  * @param bank
113  * @param set Apply or remove security on the chip.
114  * @param first This parameter is ignored.
115  * @param last This parameter is ignored.
116  *
117  * @return
118  */
119 static int dsp5680xx_flash_protect(struct flash_bank *bank, int set,
120                 unsigned int first, unsigned int last)
121 {
122 /**
123  * This applies security to flash module after next reset, it does
124  * not actually apply protection (protection refers to undesired access from the core)
125  */
126         int retval;
127
128         if (set)
129                 retval = dsp5680xx_f_lock(bank->target);
130         else
131                 retval = dsp5680xx_f_unlock(bank->target);
132
133         return retval;
134 }
135
136 /**
137  * The dsp5680xx use word addressing. The "/2" that appear in the following code
138  * are a workaround for the fact that OpenOCD uses byte addressing.
139  *
140  * @param bank
141  * @param buffer Data to write to flash.
142  * @param offset
143  * @param count In bytes (2 bytes per address).
144  *
145  * @return
146  */
147 static int dsp5680xx_flash_write(struct flash_bank *bank, const uint8_t *buffer,
148                                  uint32_t offset, uint32_t count)
149 {
150         if ((offset + count / 2) > bank->size) {
151                 LOG_ERROR("%s: Flash bank cannot fit data.", __func__);
152                 return ERROR_FAIL;
153         }
154         if (offset % 2) {
155                 /**
156                  * Writing to odd addresses not supported.
157                  * This chip uses word addressing, Openocd only supports byte addressing.
158                  * The workaround results in disabling writing to odd byte addresses
159                  */
160                 LOG_ERROR("%s: Writing to odd addresses not supported for this target", __func__);
161                 return ERROR_FAIL;
162         }
163         return dsp5680xx_f_wr(bank->target, buffer, bank->base + offset / 2, count, 0);
164 }
165
166 static int dsp5680xx_probe(struct flash_bank *bank)
167 {
168         LOG_DEBUG("%s not implemented", __func__);
169         return ERROR_OK;
170 }
171
172 /**
173  * The flash module (FM) on the dsp5680xx supports both individual sector
174  * and mass erase of the flash memory.
175  * If this function is called with @a first == @a last == 0 or if @a first is the
176  * first sector (#0) and @a last is the last sector then the mass erase command
177  * is executed (much faster than erasing each sector individually).
178  *
179  * @param bank
180  * @param first
181  * @param last
182  *
183  * @return
184  */
185 static int dsp5680xx_flash_erase(struct flash_bank *bank, unsigned int first,
186                 unsigned int last)
187 {
188         return dsp5680xx_f_erase(bank->target, (uint32_t) first, (uint32_t) last);
189 }
190
191 /**
192  * The flash module (FM) on the dsp5680xx support a blank check function.
193  * This function executes the FM's blank check functionality on each and every sector.
194  *
195  * @param bank
196  *
197  * @return
198  */
199 static int dsp5680xx_flash_erase_check(struct flash_bank *bank)
200 {
201         int retval = ERROR_OK;
202
203         uint8_t erased = 0;
204
205         uint32_t i;
206
207         for (i = 0; i < HFM_SECTOR_COUNT; i++) {
208                 retval = dsp5680xx_f_erase_check(bank->target, &erased, i);
209                 if (retval != ERROR_OK) {
210                         bank->sectors[i].is_erased = -1;
211                 } else {
212                         if (erased)
213                                 bank->sectors[i].is_erased = 1;
214                         else
215                                 bank->sectors[i].is_erased = 0;
216                 }
217         }
218         return retval;
219 }
220
221 const struct flash_driver dsp5680xx_flash = {
222         .name = "dsp5680xx_flash",
223         .flash_bank_command = dsp5680xx_flash_bank_command,
224         .erase = dsp5680xx_flash_erase,
225         .protect = dsp5680xx_flash_protect,
226         .write = dsp5680xx_flash_write,
227         /* .read = default_flash_read, */
228         .probe = dsp5680xx_probe,
229         .auto_probe = dsp5680xx_probe,
230         .erase_check = dsp5680xx_flash_erase_check,
231         .protect_check = dsp5680xx_flash_protect_check,
232 };