nor: use common flash driver
[openocd/ztw.git] / src / flash / nor / core.c
blobdf4668f6d43b4baaa5ee349d46d74759f4100feb
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 object *flash_banks = NULL;
34 int flash_driver_erase(struct flash_bank *bank, int first, int last)
36 int retval;
38 retval = flash_bank_driver(bank)->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 = flash_bank_driver(bank)->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 = flash_bank_driver(bank)->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 object_add(&flash_banks, &bank->object);
79 struct flash_bank *flash_bank_list(void)
81 return flash_banks ? flash_bank_from_object(flash_banks) : NULL;
84 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
86 struct flash_bank *p;
87 int i = 0;
89 for (p = flash_bank_list(); NULL != p; p = flash_bank_next(p))
91 if (i++ == num)
93 return p;
96 LOG_ERROR("flash bank %d does not exist", num);
97 return NULL;
100 int flash_get_bank_count(void)
102 struct flash_bank *p;
103 int i = 0;
104 for (p = flash_bank_list(); NULL != p; p = flash_bank_next(p))
106 i++;
108 return i;
111 struct flash_bank *get_flash_bank_by_name(const char *name)
113 unsigned requested = get_flash_name_index(name);
114 unsigned found = 0;
116 struct flash_bank *bank;
117 for (bank = flash_bank_list(); bank; bank = flash_bank_next(bank))
119 if (strcmp(flash_bank_name(bank), name) == 0)
120 return bank;
121 const char *dname = flash_driver_name(flash_bank_driver(bank));
122 if (!flash_driver_name_matches(dname, name))
123 continue;
124 if (++found < requested)
125 continue;
126 return bank;
128 return NULL;
131 struct flash_bank *get_flash_bank_by_num(int num)
133 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
134 int retval;
136 if (p == NULL)
137 return NULL;
139 retval = flash_bank_driver(p)->auto_probe(p);
141 if (retval != ERROR_OK)
143 LOG_ERROR("auto_probe failed %d\n", retval);
144 return NULL;
146 return p;
149 /* lookup flash bank by address */
150 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
152 struct flash_bank *c;
154 /* cycle through bank list */
155 for (c = flash_bank_list(); NULL != c; c = flash_bank_next(c))
157 int retval;
158 retval = flash_bank_driver(c)->auto_probe(c);
160 if (retval != ERROR_OK)
162 LOG_ERROR("auto_probe failed %d\n", retval);
163 return NULL;
165 /* check whether address belongs to this flash bank */
166 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
167 return c;
169 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
170 return NULL;
173 int default_flash_mem_blank_check(struct flash_bank *bank)
175 struct target *target = bank->target;
176 const int buffer_size = 1024;
177 int i;
178 uint32_t nBytes;
179 int retval = ERROR_OK;
181 if (bank->target->state != TARGET_HALTED)
183 LOG_ERROR("Target not halted");
184 return ERROR_TARGET_NOT_HALTED;
187 uint8_t *buffer = malloc(buffer_size);
189 for (i = 0; i < bank->num_sectors; i++)
191 uint32_t j;
192 bank->sectors[i].is_erased = 1;
194 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
196 uint32_t chunk;
197 chunk = buffer_size;
198 if (chunk > (j - bank->sectors[i].size))
200 chunk = (j - bank->sectors[i].size);
203 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
204 if (retval != ERROR_OK)
206 goto done;
209 for (nBytes = 0; nBytes < chunk; nBytes++)
211 if (buffer[nBytes] != 0xFF)
213 bank->sectors[i].is_erased = 0;
214 break;
220 done:
221 free(buffer);
223 return retval;
226 int default_flash_blank_check(struct flash_bank *bank)
228 struct target *target = bank->target;
229 int i;
230 int retval;
231 int fast_check = 0;
232 uint32_t blank;
234 if (bank->target->state != TARGET_HALTED)
236 LOG_ERROR("Target not halted");
237 return ERROR_TARGET_NOT_HALTED;
240 for (i = 0; i < bank->num_sectors; i++)
242 uint32_t address = bank->base + bank->sectors[i].offset;
243 uint32_t size = bank->sectors[i].size;
245 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
247 fast_check = 0;
248 break;
250 if (blank == 0xFF)
251 bank->sectors[i].is_erased = 1;
252 else
253 bank->sectors[i].is_erased = 0;
254 fast_check = 1;
257 if (!fast_check)
259 LOG_USER("Running slow fallback erase check - add working memory");
260 return default_flash_mem_blank_check(bank);
263 return ERROR_OK;
265 /* erase given flash region, selects proper bank according to target and address */
266 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
267 int (*callback)(struct flash_bank *bank, int first, int last))
269 struct flash_bank *c;
270 int first = -1;
271 int last = -1;
272 int i;
274 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
275 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
277 if (c->size == 0 || c->num_sectors == 0)
279 LOG_ERROR("Bank is invalid");
280 return ERROR_FLASH_BANK_INVALID;
283 if (length == 0)
285 /* special case, erase whole bank when length is zero */
286 if (addr != c->base)
287 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
289 return callback(c, 0, c->num_sectors - 1);
292 /* check whether it fits */
293 if (addr + length - 1 > c->base + c->size - 1)
294 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
296 addr -= c->base;
298 for (i = 0; i < c->num_sectors; i++)
300 /* check whether sector overlaps with the given range and is not yet erased */
301 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
302 /* if first is not set yet then this is the first sector */
303 if (first == -1)
304 first = i;
305 last = i; /* and it is the last one so far in any case */
309 if (first == -1 || last == -1)
310 return ERROR_OK;
312 return callback(c, first, last);
315 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
317 return flash_iterate_address_range(target,
318 addr, length, &flash_driver_erase);
321 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
323 return flash_driver_protect(bank, 0, first, last);
326 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
328 return flash_iterate_address_range(target,
329 addr, length, &flash_driver_unprotect);
332 int flash_write_unlock(struct target *target, struct image *image,
333 uint32_t *written, int erase, bool unlock)
335 int retval = ERROR_OK;
337 int section;
338 uint32_t section_offset;
339 struct flash_bank *c;
340 int *padding;
342 section = 0;
343 section_offset = 0;
345 if (written)
346 *written = 0;
348 if (erase)
350 /* assume all sectors need erasing - stops any problems
351 * when flash_write is called multiple times */
353 flash_set_dirty();
356 /* allocate padding array */
357 padding = malloc(image->num_sections * sizeof(padding));
359 /* loop until we reach end of the image */
360 while (section < image->num_sections)
362 uint32_t buffer_size;
363 uint8_t *buffer;
364 int section_first;
365 int section_last;
366 uint32_t run_address = image->sections[section].base_address + section_offset;
367 uint32_t run_size = image->sections[section].size - section_offset;
368 int pad_bytes = 0;
370 if (image->sections[section].size == 0)
372 LOG_WARNING("empty section %d", section);
373 section++;
374 section_offset = 0;
375 continue;
378 /* find the corresponding flash bank */
379 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
381 section++; /* and skip it */
382 section_offset = 0;
383 continue;
386 /* collect consecutive sections which fall into the same bank */
387 section_first = section;
388 section_last = section;
389 padding[section] = 0;
390 while ((run_address + run_size - 1 < c->base + c->size - 1)
391 && (section_last + 1 < image->num_sections))
393 if (image->sections[section_last + 1].base_address < (run_address + run_size))
395 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
396 break;
398 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
399 * attempt to rebuild a consecutive buffer for the flash loader */
400 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
401 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
402 break;
403 padding[section_last] = pad_bytes;
404 run_size += image->sections[++section_last].size;
405 run_size += pad_bytes;
406 padding[section_last] = 0;
408 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
411 /* fit the run into bank constraints */
412 if (run_address + run_size - 1 > c->base + c->size - 1)
414 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
415 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
416 run_size = c->base + c->size - run_address;
419 /* allocate buffer */
420 buffer = malloc(run_size);
421 buffer_size = 0;
423 /* read sections to the buffer */
424 while (buffer_size < run_size)
426 size_t size_read;
428 size_read = run_size - buffer_size;
429 if (size_read > image->sections[section].size - section_offset)
430 size_read = image->sections[section].size - section_offset;
432 if ((retval = image_read_section(image, section, section_offset,
433 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
435 free(buffer);
436 free(padding);
437 return retval;
440 /* see if we need to pad the section */
441 while (padding[section]--)
442 (buffer + buffer_size)[size_read++] = 0xff;
444 buffer_size += size_read;
445 section_offset += size_read;
447 if (section_offset >= image->sections[section].size)
449 section++;
450 section_offset = 0;
454 retval = ERROR_OK;
456 if (unlock)
458 retval = flash_unlock_address_range(target, run_address, run_size);
460 if (retval == ERROR_OK)
462 if (erase)
464 /* calculate and erase sectors */
465 retval = flash_erase_address_range(target, run_address, run_size);
469 if (retval == ERROR_OK)
471 /* write flash sectors */
472 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
475 free(buffer);
477 if (retval != ERROR_OK)
479 free(padding);
480 return retval; /* abort operation */
483 if (written != NULL)
484 *written += run_size; /* add run size to total written counter */
487 free(padding);
489 return retval;
492 int flash_write(struct target *target, struct image *image,
493 uint32_t *written, int erase)
495 return flash_write_unlock(target, image, written, erase, false);