Doxygen file comments
[openocd/ellerodev.git] / src / flash / nor / core.c
blob9083ed15ef92a95dda02aa4f0d9362922c2b4905
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 /**
33 * @file
34 * Upper level of NOR flash framework.
35 * The lower level interfaces are to drivers. These upper level ones
36 * primarily support access from Tcl scripts or from GDB.
39 struct flash_bank *flash_banks;
41 int flash_driver_erase(struct flash_bank *bank, int first, int last)
43 int retval;
45 retval = bank->driver->erase(bank, first, last);
46 if (retval != ERROR_OK)
48 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
51 return retval;
54 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
56 int retval;
58 retval = bank->driver->protect(bank, set, first, last);
59 if (retval != ERROR_OK)
61 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
64 return retval;
67 int flash_driver_write(struct flash_bank *bank,
68 uint8_t *buffer, uint32_t offset, uint32_t count)
70 int retval;
72 retval = bank->driver->write(bank, buffer, offset, count);
73 if (retval != ERROR_OK)
75 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
76 bank->base, offset, retval);
79 return retval;
82 void flash_bank_add(struct flash_bank *bank)
84 /* put flash bank in linked list */
85 unsigned bank_num = 0;
86 if (flash_banks)
88 /* find last flash bank */
89 struct flash_bank *p = flash_banks;
90 while (NULL != p->next)
92 bank_num += 1;
93 p = p->next;
95 p->next = bank;
96 bank_num += 1;
98 else
99 flash_banks = bank;
101 bank->bank_number = bank_num;
104 struct flash_bank *flash_bank_list(void)
106 return flash_banks;
109 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
111 struct flash_bank *p;
112 int i = 0;
114 for (p = flash_banks; p; p = p->next)
116 if (i++ == num)
118 return p;
121 LOG_ERROR("flash bank %d does not exist", num);
122 return NULL;
125 int flash_get_bank_count(void)
127 struct flash_bank *p;
128 int i = 0;
129 for (p = flash_banks; p; p = p->next)
131 i++;
133 return i;
136 struct flash_bank *get_flash_bank_by_name(const char *name)
138 unsigned requested = get_flash_name_index(name);
139 unsigned found = 0;
141 struct flash_bank *bank;
142 for (bank = flash_banks; NULL != bank; bank = bank->next)
144 if (strcmp(bank->name, name) == 0)
145 return bank;
146 if (!flash_driver_name_matches(bank->driver->name, name))
147 continue;
148 if (++found < requested)
149 continue;
150 return bank;
152 return NULL;
155 struct flash_bank *get_flash_bank_by_num(int num)
157 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
158 int retval;
160 if (p == NULL)
161 return NULL;
163 retval = p->driver->auto_probe(p);
165 if (retval != ERROR_OK)
167 LOG_ERROR("auto_probe failed %d\n", retval);
168 return NULL;
170 return p;
173 /* lookup flash bank by address */
174 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
176 struct flash_bank *c;
178 /* cycle through bank list */
179 for (c = flash_banks; c; c = c->next)
181 int retval;
182 retval = c->driver->auto_probe(c);
184 if (retval != ERROR_OK)
186 LOG_ERROR("auto_probe failed %d\n", retval);
187 return NULL;
189 /* check whether address belongs to this flash bank */
190 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
191 return c;
193 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
194 return NULL;
197 int default_flash_mem_blank_check(struct flash_bank *bank)
199 struct target *target = bank->target;
200 const int buffer_size = 1024;
201 int i;
202 uint32_t nBytes;
203 int retval = ERROR_OK;
205 if (bank->target->state != TARGET_HALTED)
207 LOG_ERROR("Target not halted");
208 return ERROR_TARGET_NOT_HALTED;
211 uint8_t *buffer = malloc(buffer_size);
213 for (i = 0; i < bank->num_sectors; i++)
215 uint32_t j;
216 bank->sectors[i].is_erased = 1;
218 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
220 uint32_t chunk;
221 chunk = buffer_size;
222 if (chunk > (j - bank->sectors[i].size))
224 chunk = (j - bank->sectors[i].size);
227 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
228 if (retval != ERROR_OK)
230 goto done;
233 for (nBytes = 0; nBytes < chunk; nBytes++)
235 if (buffer[nBytes] != 0xFF)
237 bank->sectors[i].is_erased = 0;
238 break;
244 done:
245 free(buffer);
247 return retval;
250 int default_flash_blank_check(struct flash_bank *bank)
252 struct target *target = bank->target;
253 int i;
254 int retval;
255 int fast_check = 0;
256 uint32_t blank;
258 if (bank->target->state != TARGET_HALTED)
260 LOG_ERROR("Target not halted");
261 return ERROR_TARGET_NOT_HALTED;
264 for (i = 0; i < bank->num_sectors; i++)
266 uint32_t address = bank->base + bank->sectors[i].offset;
267 uint32_t size = bank->sectors[i].size;
269 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
271 fast_check = 0;
272 break;
274 if (blank == 0xFF)
275 bank->sectors[i].is_erased = 1;
276 else
277 bank->sectors[i].is_erased = 0;
278 fast_check = 1;
281 if (!fast_check)
283 LOG_USER("Running slow fallback erase check - add working memory");
284 return default_flash_mem_blank_check(bank);
287 return ERROR_OK;
290 /* erase given flash region, selects proper bank according to target and address */
291 static int flash_iterate_address_range(struct target *target,
292 uint32_t addr, uint32_t length,
293 int (*callback)(struct flash_bank *bank, int first, int last))
295 struct flash_bank *c;
296 uint32_t last_addr = addr + length; /* first address AFTER end */
297 int first = -1;
298 int last = -1;
299 int i;
301 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
302 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
304 if (c->size == 0 || c->num_sectors == 0)
306 LOG_ERROR("Bank is invalid");
307 return ERROR_FLASH_BANK_INVALID;
310 if (length == 0)
312 /* special case, erase whole bank when length is zero */
313 if (addr != c->base)
314 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
316 return callback(c, 0, c->num_sectors - 1);
319 /* check whether it all fits in this bank */
320 if (addr + length - 1 > c->base + c->size - 1)
321 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
323 /** @todo: handle erasures that cross into adjacent banks */
325 addr -= c->base;
326 last_addr -= c->base;
328 for (i = 0; i < c->num_sectors; i++)
330 struct flash_sector *f = c->sectors + i;
332 /* start only on a sector boundary */
333 if (first < 0) {
334 /* is this the first sector? */
335 if (addr == f->offset)
336 first = i;
337 else if (addr < f->offset)
338 break;
341 /* is this (also?) the last sector? */
342 if (last_addr == f->offset + f->size) {
343 last = i;
344 break;
347 /* MUST finish on a sector boundary */
348 if (last_addr <= f->offset)
349 break;
352 /* invalid start or end address? */
353 if (first == -1 || last == -1) {
354 LOG_ERROR("address range 0x%8.8x .. 0x%8.8x "
355 "is not sector-aligned",
356 (unsigned) (c->base + addr),
357 (unsigned) (last_addr - 1));
358 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
361 /* The NOR driver may trim this range down, based on
362 * whether or not a given sector is already erased.
364 * REVISIT should *we* trim it... ?
366 return callback(c, first, last);
369 int flash_erase_address_range(struct target *target,
370 uint32_t addr, uint32_t length)
372 return flash_iterate_address_range(target,
373 addr, length, &flash_driver_erase);
376 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
378 return flash_driver_protect(bank, 0, first, last);
381 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
383 return flash_iterate_address_range(target,
384 addr, length, &flash_driver_unprotect);
387 int flash_write_unlock(struct target *target, struct image *image,
388 uint32_t *written, int erase, bool unlock)
390 int retval = ERROR_OK;
392 int section;
393 uint32_t section_offset;
394 struct flash_bank *c;
395 int *padding;
397 section = 0;
398 section_offset = 0;
400 if (written)
401 *written = 0;
403 if (erase)
405 /* assume all sectors need erasing - stops any problems
406 * when flash_write is called multiple times */
408 flash_set_dirty();
411 /* allocate padding array */
412 padding = calloc(image->num_sections, sizeof(*padding));
414 /* loop until we reach end of the image */
415 while (section < image->num_sections)
417 uint32_t buffer_size;
418 uint8_t *buffer;
419 int section_first;
420 int section_last;
421 uint32_t run_address = image->sections[section].base_address + section_offset;
422 uint32_t run_size = image->sections[section].size - section_offset;
423 int pad_bytes = 0;
425 if (image->sections[section].size == 0)
427 LOG_WARNING("empty section %d", section);
428 section++;
429 section_offset = 0;
430 continue;
433 /* find the corresponding flash bank */
434 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
436 section++; /* and skip it */
437 section_offset = 0;
438 continue;
441 /* collect consecutive sections which fall into the same bank */
442 section_first = section;
443 section_last = section;
444 padding[section] = 0;
445 while ((run_address + run_size - 1 < c->base + c->size - 1)
446 && (section_last + 1 < image->num_sections))
448 if (image->sections[section_last + 1].base_address < (run_address + run_size))
450 LOG_DEBUG("section %d out of order "
451 "(surprising, but supported)",
452 section_last + 1);
453 /* REVISIT this can break with autoerase ...
454 * clobbering data after it's written.
456 break;
459 /* FIXME This needlessly touches sectors BETWEEN the
460 * sections it's writing. Without auto erase, it just
461 * writes ones. That WILL INVALIDATE data in cases
462 * like Stellaris Tempest chips, corrupting internal
463 * ECC codes; and at least FreeScale suggests issues
464 * with that approach (in HC11 documentation).
466 * With auto erase enabled, data in those sectors will
467 * be needlessly destroyed; and some of the limited
468 * number of flash erase cycles will be wasted...
470 * In both cases, the extra writes slow things down.
473 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
474 * attempt to rebuild a consecutive buffer for the flash loader */
475 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
476 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
477 break;
478 padding[section_last] = pad_bytes;
479 run_size += image->sections[++section_last].size;
480 run_size += pad_bytes;
482 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
485 /* fit the run into bank constraints */
486 if (run_address + run_size - 1 > c->base + c->size - 1)
488 /* REVISIT isn't this superfluous, given the while()
489 * loop conditions above??
491 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
492 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
493 run_size = c->base + c->size - run_address;
496 /* If we're applying any sector automagic, then pad this
497 * (maybe-combined) segment to the end of its last sector.
499 if (unlock || erase) {
500 int sector;
501 uint32_t offset_start = run_address - c->base;
502 uint32_t offset_end = offset_start + run_size;
503 uint32_t end = offset_end, delta;
505 for (sector = 0; sector < c->num_sectors; sector++) {
506 end = c->sectors[sector].offset
507 + c->sectors[sector].size;
508 if (offset_end <= end)
509 break;
512 delta = end - offset_end;
513 padding[section_last] += delta;
514 run_size += delta;
517 /* allocate buffer */
518 buffer = malloc(run_size);
519 buffer_size = 0;
521 /* read sections to the buffer */
522 while (buffer_size < run_size)
524 size_t size_read;
526 size_read = run_size - buffer_size;
527 if (size_read > image->sections[section].size - section_offset)
528 size_read = image->sections[section].size - section_offset;
530 if ((retval = image_read_section(image, section, section_offset,
531 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
533 free(buffer);
534 free(padding);
535 return retval;
538 /* see if we need to pad the section */
539 while (padding[section]--)
540 (buffer + buffer_size)[size_read++] = 0xff;
542 buffer_size += size_read;
543 section_offset += size_read;
545 if (section_offset >= image->sections[section].size)
547 section++;
548 section_offset = 0;
552 retval = ERROR_OK;
554 if (unlock)
556 retval = flash_unlock_address_range(target, run_address, run_size);
558 if (retval == ERROR_OK)
560 if (erase)
562 /* calculate and erase sectors */
563 retval = flash_erase_address_range(target, run_address, run_size);
567 if (retval == ERROR_OK)
569 /* write flash sectors */
570 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
573 free(buffer);
575 if (retval != ERROR_OK)
577 free(padding);
578 return retval; /* abort operation */
581 if (written != NULL)
582 *written += run_size; /* add run size to total written counter */
585 free(padding);
587 return retval;
590 int flash_write(struct target *target, struct image *image,
591 uint32_t *written, int erase)
593 return flash_write_unlock(target, image, written, erase, false);