ARM: rename ARMV4_5_STATE_* as ARM_STATE_*
[openocd/ztw.git] / src / flash / nor / core.c
blobc2ea134a393609371be6bfb915476481db8cd24f
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <flash/common.h>
27 #include <flash/nor/core.h>
28 #include <flash/nor/imp.h>
29 #include <target/image.h>
32 struct flash_bank *flash_banks;
34 int flash_driver_erase(struct flash_bank *bank, int first, int last)
36 int retval;
38 retval = bank->driver->erase(bank, first, last);
39 if (retval != ERROR_OK)
41 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
44 return retval;
47 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
49 int retval;
51 retval = bank->driver->protect(bank, set, first, last);
52 if (retval != ERROR_OK)
54 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
57 return retval;
60 int flash_driver_write(struct flash_bank *bank,
61 uint8_t *buffer, uint32_t offset, uint32_t count)
63 int retval;
65 retval = bank->driver->write(bank, buffer, offset, count);
66 if (retval != ERROR_OK)
68 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
69 bank->base, offset, retval);
72 return retval;
75 void flash_bank_add(struct flash_bank *bank)
77 /* put flash bank in linked list */
78 unsigned bank_num = 0;
79 if (flash_banks)
81 /* find last flash bank */
82 struct flash_bank *p = flash_banks;
83 while (NULL != p->next)
85 bank_num += 1;
86 p = p->next;
88 p->next = bank;
89 bank_num += 1;
91 else
92 flash_banks = bank;
94 bank->bank_number = bank_num;
97 struct flash_bank *flash_bank_list(void)
99 return flash_banks;
102 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
104 struct flash_bank *p;
105 int i = 0;
107 for (p = flash_banks; p; p = p->next)
109 if (i++ == num)
111 return p;
114 LOG_ERROR("flash bank %d does not exist", num);
115 return NULL;
118 int flash_get_bank_count(void)
120 struct flash_bank *p;
121 int i = 0;
122 for (p = flash_banks; p; p = p->next)
124 i++;
126 return i;
129 struct flash_bank *get_flash_bank_by_name(const char *name)
131 unsigned requested = get_flash_name_index(name);
132 unsigned found = 0;
134 struct flash_bank *bank;
135 for (bank = flash_banks; NULL != bank; bank = bank->next)
137 if (strcmp(bank->name, name) == 0)
138 return bank;
139 if (!flash_driver_name_matches(bank->driver->name, name))
140 continue;
141 if (++found < requested)
142 continue;
143 return bank;
145 return NULL;
148 struct flash_bank *get_flash_bank_by_num(int num)
150 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
151 int retval;
153 if (p == NULL)
154 return NULL;
156 retval = p->driver->auto_probe(p);
158 if (retval != ERROR_OK)
160 LOG_ERROR("auto_probe failed %d\n", retval);
161 return NULL;
163 return p;
166 /* lookup flash bank by address */
167 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
169 struct flash_bank *c;
171 /* cycle through bank list */
172 for (c = flash_banks; c; c = c->next)
174 int retval;
175 retval = c->driver->auto_probe(c);
177 if (retval != ERROR_OK)
179 LOG_ERROR("auto_probe failed %d\n", retval);
180 return NULL;
182 /* check whether address belongs to this flash bank */
183 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
184 return c;
186 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
187 return NULL;
190 int default_flash_mem_blank_check(struct flash_bank *bank)
192 struct target *target = bank->target;
193 const int buffer_size = 1024;
194 int i;
195 uint32_t nBytes;
196 int retval = ERROR_OK;
198 if (bank->target->state != TARGET_HALTED)
200 LOG_ERROR("Target not halted");
201 return ERROR_TARGET_NOT_HALTED;
204 uint8_t *buffer = malloc(buffer_size);
206 for (i = 0; i < bank->num_sectors; i++)
208 uint32_t j;
209 bank->sectors[i].is_erased = 1;
211 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
213 uint32_t chunk;
214 chunk = buffer_size;
215 if (chunk > (j - bank->sectors[i].size))
217 chunk = (j - bank->sectors[i].size);
220 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
221 if (retval != ERROR_OK)
223 goto done;
226 for (nBytes = 0; nBytes < chunk; nBytes++)
228 if (buffer[nBytes] != 0xFF)
230 bank->sectors[i].is_erased = 0;
231 break;
237 done:
238 free(buffer);
240 return retval;
243 int default_flash_blank_check(struct flash_bank *bank)
245 struct target *target = bank->target;
246 int i;
247 int retval;
248 int fast_check = 0;
249 uint32_t blank;
251 if (bank->target->state != TARGET_HALTED)
253 LOG_ERROR("Target not halted");
254 return ERROR_TARGET_NOT_HALTED;
257 for (i = 0; i < bank->num_sectors; i++)
259 uint32_t address = bank->base + bank->sectors[i].offset;
260 uint32_t size = bank->sectors[i].size;
262 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
264 fast_check = 0;
265 break;
267 if (blank == 0xFF)
268 bank->sectors[i].is_erased = 1;
269 else
270 bank->sectors[i].is_erased = 0;
271 fast_check = 1;
274 if (!fast_check)
276 LOG_USER("Running slow fallback erase check - add working memory");
277 return default_flash_mem_blank_check(bank);
280 return ERROR_OK;
282 /* erase given flash region, selects proper bank according to target and address */
283 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
284 int (*callback)(struct flash_bank *bank, int first, int last))
286 struct flash_bank *c;
287 int first = -1;
288 int last = -1;
289 int i;
291 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
292 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
294 if (c->size == 0 || c->num_sectors == 0)
296 LOG_ERROR("Bank is invalid");
297 return ERROR_FLASH_BANK_INVALID;
300 if (length == 0)
302 /* special case, erase whole bank when length is zero */
303 if (addr != c->base)
304 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
306 return callback(c, 0, c->num_sectors - 1);
309 /* check whether it fits */
310 if (addr + length - 1 > c->base + c->size - 1)
311 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
313 addr -= c->base;
315 for (i = 0; i < c->num_sectors; i++)
317 /* check whether sector overlaps with the given range and is not yet erased */
318 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
319 /* if first is not set yet then this is the first sector */
320 if (first == -1)
321 first = i;
322 last = i; /* and it is the last one so far in any case */
326 if (first == -1 || last == -1)
327 return ERROR_OK;
329 return callback(c, first, last);
332 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
334 return flash_iterate_address_range(target,
335 addr, length, &flash_driver_erase);
338 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
340 return flash_driver_protect(bank, 0, first, last);
343 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
345 return flash_iterate_address_range(target,
346 addr, length, &flash_driver_unprotect);
349 int flash_write_unlock(struct target *target, struct image *image,
350 uint32_t *written, int erase, bool unlock)
352 int retval = ERROR_OK;
354 int section;
355 uint32_t section_offset;
356 struct flash_bank *c;
357 int *padding;
359 section = 0;
360 section_offset = 0;
362 if (written)
363 *written = 0;
365 if (erase)
367 /* assume all sectors need erasing - stops any problems
368 * when flash_write is called multiple times */
370 flash_set_dirty();
373 /* allocate padding array */
374 padding = malloc(image->num_sections * sizeof(padding));
376 /* loop until we reach end of the image */
377 while (section < image->num_sections)
379 uint32_t buffer_size;
380 uint8_t *buffer;
381 int section_first;
382 int section_last;
383 uint32_t run_address = image->sections[section].base_address + section_offset;
384 uint32_t run_size = image->sections[section].size - section_offset;
385 int pad_bytes = 0;
387 if (image->sections[section].size == 0)
389 LOG_WARNING("empty section %d", section);
390 section++;
391 section_offset = 0;
392 continue;
395 /* find the corresponding flash bank */
396 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
398 section++; /* and skip it */
399 section_offset = 0;
400 continue;
403 /* collect consecutive sections which fall into the same bank */
404 section_first = section;
405 section_last = section;
406 padding[section] = 0;
407 while ((run_address + run_size - 1 < c->base + c->size - 1)
408 && (section_last + 1 < image->num_sections))
410 if (image->sections[section_last + 1].base_address < (run_address + run_size))
412 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
413 break;
415 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
416 * attempt to rebuild a consecutive buffer for the flash loader */
417 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
418 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
419 break;
420 padding[section_last] = pad_bytes;
421 run_size += image->sections[++section_last].size;
422 run_size += pad_bytes;
423 padding[section_last] = 0;
425 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
428 /* fit the run into bank constraints */
429 if (run_address + run_size - 1 > c->base + c->size - 1)
431 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
432 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
433 run_size = c->base + c->size - run_address;
436 /* allocate buffer */
437 buffer = malloc(run_size);
438 buffer_size = 0;
440 /* read sections to the buffer */
441 while (buffer_size < run_size)
443 size_t size_read;
445 size_read = run_size - buffer_size;
446 if (size_read > image->sections[section].size - section_offset)
447 size_read = image->sections[section].size - section_offset;
449 if ((retval = image_read_section(image, section, section_offset,
450 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
452 free(buffer);
453 free(padding);
454 return retval;
457 /* see if we need to pad the section */
458 while (padding[section]--)
459 (buffer + buffer_size)[size_read++] = 0xff;
461 buffer_size += size_read;
462 section_offset += size_read;
464 if (section_offset >= image->sections[section].size)
466 section++;
467 section_offset = 0;
471 retval = ERROR_OK;
473 if (unlock)
475 retval = flash_unlock_address_range(target, run_address, run_size);
477 if (retval == ERROR_OK)
479 if (erase)
481 /* calculate and erase sectors */
482 retval = flash_erase_address_range(target, run_address, run_size);
486 if (retval == ERROR_OK)
488 /* write flash sectors */
489 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
492 free(buffer);
494 if (retval != ERROR_OK)
496 free(padding);
497 return retval; /* abort operation */
500 if (written != NULL)
501 *written += run_size; /* add run size to total written counter */
504 free(padding);
506 return retval;
509 int flash_write(struct target *target, struct image *image,
510 uint32_t *written, int erase)
512 return flash_write_unlock(target, image, written, erase, false);