3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #if defined(CONFIG_CMD_FLASH)
33 extern flash_info_t flash_info
[]; /* info for FLASH chips */
36 * The user interface starts numbering for Flash banks with 1
37 * for historical reasons.
41 * this routine looks for an abbreviated flash range specification.
42 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
43 * sector to erase, and SL is the last sector to erase (defaults to SF).
44 * bank numbers start at 1 to be consistent with other specs, sector numbers
47 * returns: 1 - correct spec; *pinfo, *psf and *psl are
49 * 0 - doesn't look like an abbreviated spec
50 * -1 - looks like an abbreviated spec, but got
51 * a parsing error, a number out of range,
52 * or an invalid flash bank.
55 abbrev_spec(char *str
, flash_info_t
**pinfo
, int *psf
, int *psl
)
58 int bank
, first
, last
;
61 if ((p
= strchr(str
, ':')) == NULL
)
65 bank
= simple_strtoul(str
, &ep
, 10);
66 if (ep
== str
|| *ep
!= '\0' ||
67 bank
< 1 || bank
> CONFIG_SYS_MAX_FLASH_BANKS
||
68 (fp
= &flash_info
[bank
- 1])->flash_id
== FLASH_UNKNOWN
)
72 if ((p
= strchr(str
, '-')) != NULL
)
75 first
= simple_strtoul(str
, &ep
, 10);
76 if (ep
== str
|| *ep
!= '\0' || first
>= fp
->sector_count
)
80 last
= simple_strtoul(p
, &ep
, 10);
81 if (ep
== p
|| *ep
!= '\0' ||
82 last
< first
|| last
>= fp
->sector_count
)
94 int do_flinfo (cmd_tbl_t
*cmdtp
, bd_t
*bd
, int flag
, int argc
, char *argv
[])
98 if (argc
== 1) { /* print info for all FLASH banks */
99 for (bank
=0; bank
<CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
100 printf ("\nBank # %ld: ", bank
+1);
102 flash_print_info (&flash_info
[bank
]);
107 bank
= simple_strtoul(argv
[1], NULL
, 16);
108 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
109 printf ("Only FLASH Banks # 1 ... # %d supported\n",
110 CONFIG_SYS_MAX_FLASH_BANKS
);
113 printf ("\nBank # %ld: ", bank
);
114 flash_print_info (&flash_info
[bank
-1]);
117 int do_flerase(cmd_tbl_t
*cmdtp
, bd_t
*bd
, int flag
, int argc
, char *argv
[])
120 ulong bank
, addr_first
, addr_last
;
121 int n
, sect_first
, sect_last
;
125 printf ("Usage:\n%s\n", cmdtp
->usage
);
129 if (strcmp(argv
[1], "all") == 0) {
130 for (bank
=1; bank
<=CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
131 printf ("Erase Flash Bank # %ld ", bank
);
132 info
= &flash_info
[bank
-1];
133 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
138 if ((n
= abbrev_spec(argv
[1], &info
, §_first
, §_last
)) != 0) {
140 printf("Bad sector specification\n");
143 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
144 sect_first
, sect_last
, (info
-flash_info
)+1);
145 rcode
= flash_erase(info
, sect_first
, sect_last
);
150 printf ("Usage:\n%s\n", cmdtp
->usage
);
154 if (strcmp(argv
[1], "bank") == 0) {
155 bank
= simple_strtoul(argv
[2], NULL
, 16);
156 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
157 printf ("Only FLASH Banks # 1 ... # %d supported\n",
158 CONFIG_SYS_MAX_FLASH_BANKS
);
161 printf ("Erase Flash Bank # %ld ", bank
);
162 info
= &flash_info
[bank
-1];
163 rcode
= flash_erase (info
, 0, info
->sector_count
-1);
167 addr_first
= simple_strtoul(argv
[1], NULL
, 16);
168 addr_last
= simple_strtoul(argv
[2], NULL
, 16);
170 if (addr_first
>= addr_last
) {
171 printf ("Usage:\n%s\n", cmdtp
->usage
);
175 printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first
, addr_last
);
176 rcode
= flash_sect_erase(addr_first
, addr_last
);
180 int flash_sect_erase (ulong addr_first
, ulong addr_last
)
190 for (bank
=0,info
= &flash_info
[0]; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
, ++info
) {
194 if (info
->flash_id
== FLASH_UNKNOWN
) {
198 b_end
= info
->start
[0] + info
->size
- 1; /* bank end addr */
200 s_first
= -1; /* first sector to erase */
201 s_last
= -1; /* last sector to erase */
203 for (sect
=0; sect
< info
->sector_count
; ++sect
) {
204 ulong end
; /* last address in current sect */
207 s_end
= info
->sector_count
- 1;
209 end
= (sect
== s_end
) ? b_end
: info
->start
[sect
+ 1] - 1;
211 if (addr_first
> end
)
213 if (addr_last
< info
->start
[sect
])
216 if (addr_first
== info
->start
[sect
]) {
219 if (addr_last
== end
) {
223 if (s_first
>=0 && s_first
<=s_last
) {
224 erased
+= s_last
- s_first
+ 1;
225 rcode
= flash_erase (info
, s_first
, s_last
);
229 /* printf ("Erased %d sectors\n", erased); */
231 printf ("Error: start and/or end address"
232 " not on sector boundary\n");
239 int do_protect(cmd_tbl_t
*cmdtp
, bd_t
*bd
, int flag
, int argc
, char *argv
[])
242 ulong bank
, addr_first
, addr_last
;
243 int i
, p
, n
, sect_first
, sect_last
;
247 printf ("Usage:\n%s\n", cmdtp
->usage
);
251 if (strcmp(argv
[1], "off") == 0)
253 else if (strcmp(argv
[1], "on") == 0)
256 printf ("Usage:\n%s\n", cmdtp
->usage
);
260 if (strcmp(argv
[2], "all") == 0) {
261 for (bank
=1; bank
<=CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
262 info
= &flash_info
[bank
-1];
263 if (info
->flash_id
== FLASH_UNKNOWN
) {
266 /*printf ("%sProtect Flash Bank # %ld\n", */
267 /* p ? "" : "Un-", bank); */
269 for (i
=0; i
<info
->sector_count
; ++i
) {
270 #if defined(CONFIG_SYS_FLASH_PROTECTION)
271 if (flash_real_protect(info
, i
, p
))
275 info
->protect
[i
] = p
;
276 #endif /* CONFIG_SYS_FLASH_PROTECTION */
280 #if defined(CONFIG_SYS_FLASH_PROTECTION)
281 if (!rcode
) puts (" done\n");
282 #endif /* CONFIG_SYS_FLASH_PROTECTION */
287 if ((n
= abbrev_spec(argv
[2], &info
, §_first
, §_last
)) != 0) {
289 printf("Bad sector specification\n");
292 /*printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", */
293 /* p ? "" : "Un-", sect_first, sect_last, */
294 /* (info-flash_info)+1); */
295 for (i
= sect_first
; i
<= sect_last
; i
++) {
296 #if defined(CONFIG_SYS_FLASH_PROTECTION)
297 if (flash_real_protect(info
, i
, p
))
301 info
->protect
[i
] = p
;
302 #endif /* CONFIG_SYS_FLASH_PROTECTION */
305 #if defined(CONFIG_SYS_FLASH_PROTECTION)
306 if (!rcode
) puts (" done\n");
307 #endif /* CONFIG_SYS_FLASH_PROTECTION */
313 printf ("Usage:\n%s\n", cmdtp
->usage
);
317 if (strcmp(argv
[2], "bank") == 0) {
318 bank
= simple_strtoul(argv
[3], NULL
, 16);
319 if ((bank
< 1) || (bank
> CONFIG_SYS_MAX_FLASH_BANKS
)) {
320 printf ("Only FLASH Banks # 1 ... # %d supported\n",
321 CONFIG_SYS_MAX_FLASH_BANKS
);
324 printf ("%sProtect Flash Bank # %ld\n",
325 p
? "" : "Un-", bank
);
326 info
= &flash_info
[bank
-1];
328 if (info
->flash_id
== FLASH_UNKNOWN
) {
329 printf ("missing or unknown FLASH type\n");
332 for (i
=0; i
<info
->sector_count
; ++i
) {
333 #if defined(CONFIG_SYS_FLASH_PROTECTION)
334 if (flash_real_protect(info
, i
, p
))
338 info
->protect
[i
] = p
;
339 #endif /* CONFIG_SYS_FLASH_PROTECTION */
342 #if defined(CONFIG_SYS_FLASH_PROTECTION)
343 if (!rcode
) puts (" done\n");
344 #endif /* CONFIG_SYS_FLASH_PROTECTION */
349 addr_first
= simple_strtoul(argv
[2], NULL
, 16);
350 addr_last
= simple_strtoul(argv
[3], NULL
, 16);
352 if (addr_first
>= addr_last
) {
353 printf ("Usage:\n%s\n", cmdtp
->usage
);
356 rcode
= flash_sect_protect (p
, addr_first
, addr_last
);
359 int flash_sect_protect (int p
, ulong addr_first
, ulong addr_last
)
369 for (bank
=0,info
= &flash_info
[0]; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
, ++info
) {
373 if (info
->flash_id
== FLASH_UNKNOWN
) {
377 b_end
= info
->start
[0] + info
->size
- 1; /* bank end addr */
379 s_first
= -1; /* first sector to erase */
380 s_last
= -1; /* last sector to erase */
382 for (sect
=0; sect
< info
->sector_count
; ++sect
) {
383 ulong end
; /* last address in current sect */
386 s_end
= info
->sector_count
- 1;
388 end
= (sect
== s_end
) ? b_end
: info
->start
[sect
+ 1] - 1;
390 if (addr_first
> end
)
392 if (addr_last
< info
->start
[sect
])
395 if (addr_first
== info
->start
[sect
]) {
398 if (addr_last
== end
) {
402 if (s_first
>=0 && s_first
<=s_last
) {
403 protected += s_last
- s_first
+ 1;
404 for (i
=s_first
; i
<=s_last
; ++i
) {
405 #if defined(CONFIG_SYS_FLASH_PROTECTION)
406 if (flash_real_protect(info
, i
, p
))
410 info
->protect
[i
] = p
;
411 #endif /* CONFIG_SYS_FLASH_PROTECTION */
414 #if defined(CONFIG_SYS_FLASH_PROTECTION)
415 if (!rcode
) putc ('\n');
416 #endif /* CONFIG_SYS_FLASH_PROTECTION */
420 /* printf ("%sProtected %d sectors\n", */
421 /* p ? "" : "Un-", protected); */
423 printf ("Error: start and/or end address"
424 " not on sector boundary\n");