dd32a2fae8761d398473502921d1a3c70962c4ce
[openocd.git] / 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, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ***************************************************************************/
25
26 /**
27 * @file dsp5680xx_flash.c
28 * @author Rodrigo L. Rosa <rodrigorosa.LG@gmail.com>
29 * @date Thu Jun 9 18:21:58 2011
30 *
31 * @brief This file implements the basic functions to run flashing commands
32 * from the TCL interface.
33 * It allows the user to flash the Freescale 5680xx DSP.
34 *
35 *
36 */
37
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifndef DSP5680XX_FLASH_H
44 #define DSP5680XX_FLASH_H
45
46 #include "imp.h"
47 #include <helper/binarybuffer.h>
48 #include <helper/time_support.h>
49 #include <target/algorithm.h>
50 #include <target/dsp5680xx.h>
51
52 struct dsp5680xx_flash_bank {
53 struct working_area *write_algorithm;
54 };
55
56 static int dsp5680xx_build_sector_list(struct flash_bank *bank){
57 uint32_t offset = HFM_FLASH_BASE_ADDR;
58 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
59 int i;
60 for (i = 0; i < bank->num_sectors; ++i){
61 bank->sectors[i].offset = i*HFM_SECTOR_SIZE;
62 bank->sectors[i].size = HFM_SECTOR_SIZE;
63 offset += bank->sectors[i].size;
64 bank->sectors[i].is_erased = -1;
65 bank->sectors[i].is_protected = -1;
66 }
67 LOG_USER("%s not tested yet.",__FUNCTION__);
68 return ERROR_OK;
69
70 }
71
72 // flash bank dsp5680xx 0 0 0 0 <target#>
73 FLASH_BANK_COMMAND_HANDLER(dsp5680xx_flash_bank_command){
74 struct dsp5680xx_flash_bank *nbank;
75
76 nbank = malloc(sizeof(struct dsp5680xx_flash_bank));
77
78 bank->base = HFM_FLASH_BASE_ADDR;
79 bank->size = HFM_SIZE_BYTES; // top 4k not accessible
80 bank->driver_priv = nbank;
81 bank->num_sectors = HFM_SECTOR_COUNT;
82 dsp5680xx_build_sector_list(bank);
83
84 return ERROR_OK;
85 }
86
87 /**
88 * A memory mapped register (PROT) holds information regarding sector protection.
89 * Protection refers to undesired core access.
90 * The value in this register is loaded from flash upon reset.
91 *
92 * @param bank
93 *
94 * @return
95 */
96 static int dsp5680xx_flash_protect_check(struct flash_bank *bank){
97 int retval = ERROR_OK;
98 uint16_t protected = 0;
99 retval = dsp5680xx_f_protect_check(bank->target,&protected);
100 if(retval != ERROR_OK){
101 for(int i = 0;i<HFM_SECTOR_COUNT;i++)
102 bank->sectors[i].is_protected = -1;
103 return ERROR_OK;
104 }
105 for(int i = 0;i<HFM_SECTOR_COUNT/2;i++){
106 if(protected & 1){
107 bank->sectors[2*i].is_protected = 1;
108 bank->sectors[2*i+1].is_protected = 1;
109 }else{
110 bank->sectors[2*i].is_protected = 0;
111 bank->sectors[2*i+1].is_protected = 0;
112 }
113 protected = (protected >> 1);
114 }
115 return retval;
116 }
117
118 /**
119 * Protection funcionality is not implemented.
120 * The current implementation applies/removes security on the chip.
121 * The chip is effectively secured/unsecured after the first reset following the execution of this function.
122 *
123 * @param bank
124 * @param set Apply or remove security on the chip.
125 * @param first This parameter is ignored.
126 * @param last This parameter is ignored.
127 *
128 * @return
129 */
130 static int dsp5680xx_flash_protect(struct flash_bank *bank, int set, int first, int last){
131 // This applies security to flash module after next reset, it does not actually apply protection (protection refers to undesired access from the core)
132 int retval;
133 if(set)
134 retval = dsp5680xx_f_lock(bank->target);
135 else{
136 retval = dsp5680xx_f_unlock(bank->target);
137 if (retval == ERROR_OK) {
138 /* mark all as erased */
139 for (int i = 0; i <= (HFM_SECTOR_COUNT-1); i++)
140 /* FM does not recognize it as erased if erased via JTAG. */
141 bank->sectors[i].is_erased = 1;
142 }
143 }
144 return retval;
145 }
146
147 /**
148 * The dsp5680xx use word addressing. The "/2" that appear in the following code are a workaround for the fact that OpenOCD uses byte addressing.
149 *
150 * @param bank
151 * @param buffer Data to write to flash.
152 * @param offset
153 * @param count In bytes (2 bytes per address).
154 *
155 * @return
156 */
157 static int dsp5680xx_flash_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count){
158 int retval;
159 if((offset + count/2)>bank->size){
160 LOG_ERROR("%s: Flash bank cannot fit data.",__FUNCTION__);
161 return ERROR_FAIL;
162 }
163 if(offset%2){
164 LOG_ERROR("%s: Writing to odd addresses not supported. This chip uses word addressing, Openocd only supports byte addressing. The workaround results in disabling writing to odd byte addresses.",__FUNCTION__);
165 return ERROR_FAIL;
166 }
167 retval = dsp5680xx_f_wr(bank->target, buffer, bank->base + offset/2, count, 0);
168 uint32_t addr_word;
169 for(addr_word = bank->base + offset/2;addr_word<count/2;addr_word+=(HFM_SECTOR_SIZE/2)){
170 if(retval == ERROR_OK)
171 bank->sectors[addr_word/(HFM_SECTOR_SIZE/2)].is_erased = 0;
172 else
173 bank->sectors[addr_word/(HFM_SECTOR_SIZE/2)].is_erased = -1;
174 }
175 return retval;
176 }
177
178 static int dsp5680xx_probe(struct flash_bank *bank){
179 LOG_DEBUG("%s not implemented",__FUNCTION__);
180 return ERROR_OK;
181 }
182
183 static int dsp5680xx_flash_info(struct flash_bank *bank, char *buf, int buf_size){
184 snprintf(buf, buf_size, "\ndsp5680xx flash driver info:\n - Currently only full erase/lock/unlock are implemented. \n - Call with bank==0 and sector 0 to 0.\n - Protect requires arp_init-reset to complete. \n - Before removing protection the master tap must be selected, and arp_init-reset is required to complete unlocking.");
185 return ERROR_OK;
186 }
187
188 /**
189 * The flash module (FM) on the dsp5680xx supports both individual sector and mass erase of the flash memory.
190 * If this function is called with @first == @last == 0 or if @first is the first sector (#0) and @last is the last sector then the mass erase command is executed (much faster than erasing each sector individually).
191 *
192 * @param bank
193 * @param first
194 * @param last
195 *
196 * @return
197 */
198 static int dsp5680xx_flash_erase(struct flash_bank * bank, int first, int last){
199 int retval;
200 retval = dsp5680xx_f_erase(bank->target, (uint32_t) first, (uint32_t) last);
201 if ((!(first|last)) || ((first == 0) && (last == (HFM_SECTOR_COUNT-1))))
202 last = HFM_SECTOR_COUNT-1;
203 if(retval == ERROR_OK)
204 for(int i = first;i<=last;i++)
205 bank->sectors[i].is_erased = 1;
206 else
207 // If an error occurred unknown status is set even though some sector could have been correctly erased.
208 for(int i = first;i<=last;i++)
209 bank->sectors[i].is_erased = -1;
210 return retval;
211 }
212
213 /**
214 * The flash module (FM) on the dsp5680xx support a blank check function.
215 * This function executes the FM's blank check functionality on each and every sector.
216 *
217 * @param bank
218 *
219 * @return
220 */
221 static int dsp5680xx_flash_erase_check(struct flash_bank * bank){
222 int retval = ERROR_OK;
223 uint8_t erased = 0;
224 uint32_t i;
225 for(i=0;i<HFM_SECTOR_COUNT;i++){
226 if(bank->sectors[i].is_erased == -1){
227 retval = dsp5680xx_f_erase_check(bank->target,&erased,i);
228 if (retval != ERROR_OK){
229 bank->sectors[i].is_erased = -1;
230 }else{
231 if(erased)
232 bank->sectors[i].is_erased = 1;
233 else
234 bank->sectors[i].is_erased = 0;
235 }
236 }
237 }
238 return retval;
239 }
240
241 struct flash_driver dsp5680xx_flash = {
242 .name = "dsp5680xx_flash",
243 .flash_bank_command = dsp5680xx_flash_bank_command,
244 .erase = dsp5680xx_flash_erase,
245 .protect = dsp5680xx_flash_protect,
246 .write = dsp5680xx_flash_write,
247 //.read = default_flash_read,
248 .probe = dsp5680xx_probe,
249 .auto_probe = dsp5680xx_probe,
250 .erase_check = dsp5680xx_flash_erase_check,
251 .protect_check = dsp5680xx_flash_protect_check,
252 .info = dsp5680xx_flash_info
253 };
254 #endif // dsp5680xx_flash.h

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)