1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
32 #include <target/image.h>
33 #include <helper/time_support.h>
35 struct flash_bank *flash_banks;
37 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
42 for (p = flash_banks; p; p = p->next)
49 LOG_ERROR("flash bank %d does not exist", num);
53 int flash_get_bank_count(void)
57 for (p = flash_banks; p; p = p->next)
64 struct flash_bank *get_flash_bank_by_name(const char *name)
66 unsigned requested = get_flash_name_index(name);
69 struct flash_bank *bank;
70 for (bank = flash_banks; NULL != bank; bank = bank->next)
72 if (strcmp(bank->name, name) == 0)
74 if (!flash_driver_name_matches(bank->driver->name, name))
76 if (++found < requested)
83 struct flash_bank *get_flash_bank_by_num(int num)
85 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
91 retval = p->driver->auto_probe(p);
93 if (retval != ERROR_OK)
95 LOG_ERROR("auto_probe failed %d\n", retval);
101 /* lookup flash bank by address */
102 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
104 struct flash_bank *c;
106 /* cycle through bank list */
107 for (c = flash_banks; c; c = c->next)
110 retval = c->driver->auto_probe(c);
112 if (retval != ERROR_OK)
114 LOG_ERROR("auto_probe failed %d\n", retval);
117 /* check whether address belongs to this flash bank */
118 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
121 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
125 int default_flash_mem_blank_check(struct flash_bank *bank)
127 struct target *target = bank->target;
128 const int buffer_size = 1024;
131 int retval = ERROR_OK;
133 if (bank->target->state != TARGET_HALTED)
135 LOG_ERROR("Target not halted");
136 return ERROR_TARGET_NOT_HALTED;
139 uint8_t *buffer = malloc(buffer_size);
141 for (i = 0; i < bank->num_sectors; i++)
144 bank->sectors[i].is_erased = 1;
146 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
150 if (chunk > (j - bank->sectors[i].size))
152 chunk = (j - bank->sectors[i].size);
155 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
156 if (retval != ERROR_OK)
161 for (nBytes = 0; nBytes < chunk; nBytes++)
163 if (buffer[nBytes] != 0xFF)
165 bank->sectors[i].is_erased = 0;
178 int default_flash_blank_check(struct flash_bank *bank)
180 struct target *target = bank->target;
186 if (bank->target->state != TARGET_HALTED)
188 LOG_ERROR("Target not halted");
189 return ERROR_TARGET_NOT_HALTED;
192 for (i = 0; i < bank->num_sectors; i++)
194 uint32_t address = bank->base + bank->sectors[i].offset;
195 uint32_t size = bank->sectors[i].size;
197 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
203 bank->sectors[i].is_erased = 1;
205 bank->sectors[i].is_erased = 0;
211 LOG_USER("Running slow fallback erase check - add working memory");
212 return default_flash_mem_blank_check(bank);