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>
37 extern struct flash_driver lpc2000_flash
;
38 extern struct flash_driver lpc288x_flash
;
39 extern struct flash_driver lpc2900_flash
;
40 extern struct flash_driver cfi_flash
;
41 extern struct flash_driver at91sam3_flash
;
42 extern struct flash_driver at91sam7_flash
;
43 extern struct flash_driver str7x_flash
;
44 extern struct flash_driver str9x_flash
;
45 extern struct flash_driver aduc702x_flash
;
46 extern struct flash_driver stellaris_flash
;
47 extern struct flash_driver str9xpec_flash
;
48 extern struct flash_driver stm32x_flash
;
49 extern struct flash_driver tms470_flash
;
50 extern struct flash_driver ecosflash_flash
;
51 extern struct flash_driver ocl_flash
;
52 extern struct flash_driver pic32mx_flash
;
53 extern struct flash_driver avr_flash
;
54 extern struct flash_driver faux_flash
;
56 struct flash_driver
*flash_drivers
[] = {
78 struct flash_bank
*flash_banks
;
80 struct flash_bank
*get_flash_bank_by_num_noprobe(int num
)
85 for (p
= flash_banks
; p
; p
= p
->next
)
92 LOG_ERROR("flash bank %d does not exist", num
);
96 int flash_get_bank_count(void)
100 for (p
= flash_banks
; p
; p
= p
->next
)
107 struct flash_bank
*get_flash_bank_by_name(const char *name
)
109 unsigned requested
= get_flash_name_index(name
);
112 struct flash_bank
*bank
;
113 for (bank
= flash_banks
; NULL
!= bank
; bank
= bank
->next
)
115 if (strcmp(bank
->name
, name
) == 0)
117 if (!flash_driver_name_matches(bank
->driver
->name
, name
))
119 if (++found
< requested
)
126 struct flash_bank
*get_flash_bank_by_num(int num
)
128 struct flash_bank
*p
= get_flash_bank_by_num_noprobe(num
);
134 retval
= p
->driver
->auto_probe(p
);
136 if (retval
!= ERROR_OK
)
138 LOG_ERROR("auto_probe failed %d\n", retval
);
144 /* lookup flash bank by address */
145 struct flash_bank
*get_flash_bank_by_addr(struct target
*target
, uint32_t addr
)
147 struct flash_bank
*c
;
149 /* cycle through bank list */
150 for (c
= flash_banks
; c
; c
= c
->next
)
153 retval
= c
->driver
->auto_probe(c
);
155 if (retval
!= ERROR_OK
)
157 LOG_ERROR("auto_probe failed %d\n", retval
);
160 /* check whether address belongs to this flash bank */
161 if ((addr
>= c
->base
) && (addr
<= c
->base
+ (c
->size
- 1)) && target
== c
->target
)
164 LOG_ERROR("No flash at address 0x%08" PRIx32
"\n", addr
);
168 int default_flash_mem_blank_check(struct flash_bank
*bank
)
170 struct target
*target
= bank
->target
;
171 const int buffer_size
= 1024;
174 int retval
= ERROR_OK
;
176 if (bank
->target
->state
!= TARGET_HALTED
)
178 LOG_ERROR("Target not halted");
179 return ERROR_TARGET_NOT_HALTED
;
182 uint8_t *buffer
= malloc(buffer_size
);
184 for (i
= 0; i
< bank
->num_sectors
; i
++)
187 bank
->sectors
[i
].is_erased
= 1;
189 for (j
= 0; j
< bank
->sectors
[i
].size
; j
+= buffer_size
)
193 if (chunk
> (j
- bank
->sectors
[i
].size
))
195 chunk
= (j
- bank
->sectors
[i
].size
);
198 retval
= target_read_memory(target
, bank
->base
+ bank
->sectors
[i
].offset
+ j
, 4, chunk
/4, buffer
);
199 if (retval
!= ERROR_OK
)
204 for (nBytes
= 0; nBytes
< chunk
; nBytes
++)
206 if (buffer
[nBytes
] != 0xFF)
208 bank
->sectors
[i
].is_erased
= 0;
221 int default_flash_blank_check(struct flash_bank
*bank
)
223 struct target
*target
= bank
->target
;
229 if (bank
->target
->state
!= TARGET_HALTED
)
231 LOG_ERROR("Target not halted");
232 return ERROR_TARGET_NOT_HALTED
;
235 for (i
= 0; i
< bank
->num_sectors
; i
++)
237 uint32_t address
= bank
->base
+ bank
->sectors
[i
].offset
;
238 uint32_t size
= bank
->sectors
[i
].size
;
240 if ((retval
= target_blank_check_memory(target
, address
, size
, &blank
)) != ERROR_OK
)
246 bank
->sectors
[i
].is_erased
= 1;
248 bank
->sectors
[i
].is_erased
= 0;
254 LOG_USER("Running slow fallback erase check - add working memory");
255 return default_flash_mem_blank_check(bank
);