Remove FSF address from GPL notices
[openocd.git] / src / flash / nor / dsp5680xx_flash.c
blob38649ff0409ddc857b9bcfb9956f92d5ae1cc952
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 ***************************************************************************/
24 /**
25 * @file dsp5680xx_flash.c
26 * @author Rodrigo L. Rosa <rodrigorosa.LG@gmail.com>
27 * @date Thu Jun 9 18:21:58 2011
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.
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
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>
46 static int dsp5680xx_build_sector_list(struct flash_bank *bank)
48 uint32_t offset = HFM_FLASH_BASE_ADDR;
50 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
51 int i;
53 for (i = 0; i < bank->num_sectors; ++i) {
54 bank->sectors[i].offset = i * HFM_SECTOR_SIZE;
55 bank->sectors[i].size = HFM_SECTOR_SIZE;
56 offset += bank->sectors[i].size;
57 bank->sectors[i].is_erased = -1;
58 bank->sectors[i].is_protected = -1;
60 LOG_USER("%s not tested yet.", __func__);
61 return ERROR_OK;
65 /* flash bank dsp5680xx 0 0 0 0 <target#> */
66 FLASH_BANK_COMMAND_HANDLER(dsp5680xx_flash_bank_command)
68 bank->base = HFM_FLASH_BASE_ADDR;
69 bank->size = HFM_SIZE_BYTES; /* top 4k not accessible */
70 bank->num_sectors = HFM_SECTOR_COUNT;
71 dsp5680xx_build_sector_list(bank);
73 return ERROR_OK;
76 /**
77 * A memory mapped register (PROT) holds information regarding sector protection.
78 * Protection refers to undesired core access.
79 * The value in this register is loaded from flash upon reset.
81 * @param bank
83 * @return
85 static int dsp5680xx_flash_protect_check(struct flash_bank *bank)
87 int retval = ERROR_OK;
89 uint16_t protected = 0;
91 retval = dsp5680xx_f_protect_check(bank->target, &protected);
92 if (retval != ERROR_OK) {
93 for (int i = 0; i < HFM_SECTOR_COUNT; i++)
94 bank->sectors[i].is_protected = -1;
95 return ERROR_OK;
97 for (int i = 0; i < HFM_SECTOR_COUNT / 2; i++) {
98 if (protected & 1) {
99 bank->sectors[2 * i].is_protected = 1;
100 bank->sectors[2 * i + 1].is_protected = 1;
101 } else {
102 bank->sectors[2 * i].is_protected = 0;
103 bank->sectors[2 * i + 1].is_protected = 0;
105 protected = (protected >> 1);
107 return retval;
111 * Protection funcionality is not implemented.
112 * The current implementation applies/removes security on the chip.
113 * The chip is effectively secured/unsecured after the first reset
114 * following the execution of this function.
116 * @param bank
117 * @param set Apply or remove security on the chip.
118 * @param first This parameter is ignored.
119 * @param last This parameter is ignored.
121 * @return
123 static int dsp5680xx_flash_protect(struct flash_bank *bank, int set, int first,
124 int last)
127 * This applies security to flash module after next reset, it does
128 * not actually apply protection (protection refers to undesired access from the core)
130 int retval;
132 if (set)
133 retval = dsp5680xx_f_lock(bank->target);
134 else {
135 retval = dsp5680xx_f_unlock(bank->target);
136 if (retval == ERROR_OK) {
137 /* mark all as erased */
138 for (int i = 0; i <= (HFM_SECTOR_COUNT - 1); i++)
139 /* FM does not recognize it as erased if erased via JTAG. */
140 bank->sectors[i].is_erased = 1;
143 return retval;
147 * The dsp5680xx use word addressing. The "/2" that appear in the following code
148 * are a workaround for the fact that OpenOCD uses byte addressing.
150 * @param bank
151 * @param buffer Data to write to flash.
152 * @param offset
153 * @param count In bytes (2 bytes per address).
155 * @return
157 static int dsp5680xx_flash_write(struct flash_bank *bank, const uint8_t* buffer,
158 uint32_t offset, uint32_t count)
160 int retval;
162 if ((offset + count / 2) > bank->size) {
163 LOG_ERROR("%s: Flash bank cannot fit data.", __func__);
164 return ERROR_FAIL;
166 if (offset % 2) {
168 * Writing to odd addresses not supported.
169 * This chip uses word addressing, Openocd only supports byte addressing.
170 * The workaround results in disabling writing to odd byte addresses
172 LOG_ERROR("%s: Writing to odd addresses not supported for this target", __func__);
173 return ERROR_FAIL;
175 retval = dsp5680xx_f_wr(bank->target, buffer, bank->base + offset / 2, count, 0);
176 uint32_t addr_word;
178 for (addr_word = bank->base + offset / 2; addr_word < count / 2;
179 addr_word += (HFM_SECTOR_SIZE / 2)) {
180 if (retval == ERROR_OK)
181 bank->sectors[addr_word / (HFM_SECTOR_SIZE / 2)].is_erased = 0;
182 else
183 bank->sectors[addr_word / (HFM_SECTOR_SIZE / 2)].is_erased = -1;
185 return retval;
188 static int dsp5680xx_probe(struct flash_bank *bank)
190 LOG_DEBUG("%s not implemented", __func__);
191 return ERROR_OK;
195 * The flash module (FM) on the dsp5680xx supports both individual sector
196 * and mass erase of the flash memory.
197 * If this function is called with @first == @last == 0 or if @first is the
198 * first sector (#0) and @last is the last sector then the mass erase command
199 * is executed (much faster than erasing each sector individually).
201 * @param bank
202 * @param first
203 * @param last
205 * @return
207 static int dsp5680xx_flash_erase(struct flash_bank *bank, int first, int last)
209 int retval;
211 retval = dsp5680xx_f_erase(bank->target, (uint32_t) first, (uint32_t) last);
212 if ((!(first | last)) || ((first == 0) && (last == (HFM_SECTOR_COUNT - 1))))
213 last = HFM_SECTOR_COUNT - 1;
214 if (retval == ERROR_OK)
215 for (int i = first; i <= last; i++)
216 bank->sectors[i].is_erased = 1;
217 else
219 * If an error occurred unknown status
220 *is set even though some sector could have been correctly erased.
222 for (int i = first; i <= last; i++)
223 bank->sectors[i].is_erased = -1;
224 return retval;
228 * The flash module (FM) on the dsp5680xx support a blank check function.
229 * This function executes the FM's blank check functionality on each and every sector.
231 * @param bank
233 * @return
235 static int dsp5680xx_flash_erase_check(struct flash_bank *bank)
237 int retval = ERROR_OK;
239 uint8_t erased = 0;
241 uint32_t i;
243 for (i = 0; i < HFM_SECTOR_COUNT; i++) {
244 if (bank->sectors[i].is_erased == -1) {
245 retval = dsp5680xx_f_erase_check(bank->target, &erased, i);
246 if (retval != ERROR_OK) {
247 bank->sectors[i].is_erased = -1;
248 } else {
249 if (erased)
250 bank->sectors[i].is_erased = 1;
251 else
252 bank->sectors[i].is_erased = 0;
256 return retval;
259 struct flash_driver dsp5680xx_flash = {
260 .name = "dsp5680xx_flash",
261 .flash_bank_command = dsp5680xx_flash_bank_command,
262 .erase = dsp5680xx_flash_erase,
263 .protect = dsp5680xx_flash_protect,
264 .write = dsp5680xx_flash_write,
265 /* .read = default_flash_read, */
266 .probe = dsp5680xx_probe,
267 .auto_probe = dsp5680xx_probe,
268 .erase_check = dsp5680xx_flash_erase_check,
269 .protect_check = dsp5680xx_flash_protect_check,