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 #ifdef CONFIG_HAS_DATAFLASH
32 #include <dataflash.h>
35 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
37 extern flash_info_t flash_info[]; /* info for FLASH chips */
40 * The user interface starts numbering for Flash banks with 1
41 * for historical reasons.
45 * this routine looks for an abbreviated flash range specification.
46 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
47 * sector to erase, and SL is the last sector to erase (defaults to SF).
48 * bank numbers start at 1 to be consistent with other specs, sector numbers
51 * returns: 1 - correct spec; *pinfo, *psf and *psl are
53 * 0 - doesn't look like an abbreviated spec
54 * -1 - looks like an abbreviated spec, but got
55 * a parsing error, a number out of range,
56 * or an invalid flash bank.
59 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
62 int bank, first, last;
65 if ((p = strchr (str, ':')) == NULL)
69 bank = simple_strtoul (str, &ep, 10);
70 if (ep == str || *ep != '\0' ||
71 bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
72 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
76 if ((p = strchr (str, '-')) != NULL)
79 first = simple_strtoul (str, &ep, 10);
80 if (ep == str || *ep != '\0' || first >= fp->sector_count)
84 last = simple_strtoul (p, &ep, 10);
85 if (ep == p || *ep != '\0' ||
86 last < first || last >= fp->sector_count)
100 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
101 int *s_first, int *s_last,
110 for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
111 s_first[bank] = -1; /* first sector to erase */
112 s_last [bank] = -1; /* last sector to erase */
115 for (bank=0,info=&flash_info[0];
116 (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
122 if (info->flash_id == FLASH_UNKNOWN) {
126 b_end = info->start[0] + info->size - 1; /* bank end addr */
127 s_end = info->sector_count - 1; /* last sector */
130 for (sect=0; sect < info->sector_count; ++sect) {
131 ulong end; /* last address in current sect */
133 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
135 if (addr_first > end)
137 if (addr_last < info->start[sect])
140 if (addr_first == info->start[sect]) {
141 s_first[bank] = sect;
143 if (addr_last == end) {
147 if (s_first[bank] >= 0) {
148 if (s_last[bank] < 0) {
149 if (addr_last > b_end) {
150 s_last[bank] = s_end;
152 puts ("Error: end address"
153 " not on sector boundary\n");
158 if (s_last[bank] < s_first[bank]) {
159 puts ("Error: end sector"
160 " precedes start sector\n");
165 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
166 (*s_count) += s_last[bank] - s_first[bank] + 1;
167 } else if (addr_first >= info->start[0] && addr_first < b_end) {
168 puts ("Error: start address not on sector boundary\n");
171 } else if (s_last[bank] >= 0) {
172 puts ("Error: cannot span across banks when they are"
173 " mapped in reverse order\n");
182 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
186 #ifdef CONFIG_HAS_DATAFLASH
187 dataflash_print_info();
190 if (argc == 1) { /* print info for all FLASH banks */
191 for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
192 printf ("\nBank # %ld: ", bank+1);
194 flash_print_info (&flash_info[bank]);
199 bank = simple_strtoul(argv[1], NULL, 16);
200 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
201 printf ("Only FLASH Banks # 1 ... # %d supported\n",
202 CFG_MAX_FLASH_BANKS);
205 printf ("\nBank # %ld: ", bank);
206 flash_print_info (&flash_info[bank-1]);
209 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
212 ulong bank, addr_first, addr_last;
213 int n, sect_first, sect_last;
217 printf ("Usage:\n%s\n", cmdtp->usage);
221 if (strcmp(argv[1], "all") == 0) {
222 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
223 printf ("Erase Flash Bank # %ld ", bank);
224 info = &flash_info[bank-1];
225 rcode = flash_erase (info, 0, info->sector_count-1);
230 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
232 puts ("Bad sector specification\n");
235 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
236 sect_first, sect_last, (info-flash_info)+1);
237 rcode = flash_erase(info, sect_first, sect_last);
242 printf ("Usage:\n%s\n", cmdtp->usage);
246 if (strcmp(argv[1], "bank") == 0) {
247 bank = simple_strtoul(argv[2], NULL, 16);
248 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
249 printf ("Only FLASH Banks # 1 ... # %d supported\n",
250 CFG_MAX_FLASH_BANKS);
253 printf ("Erase Flash Bank # %ld ", bank);
254 info = &flash_info[bank-1];
255 rcode = flash_erase (info, 0, info->sector_count-1);
259 addr_first = simple_strtoul(argv[1], NULL, 16);
260 addr_last = simple_strtoul(argv[2], NULL, 16);
262 if (addr_first >= addr_last) {
263 printf ("Usage:\n%s\n", cmdtp->usage);
267 rcode = flash_sect_erase(addr_first, addr_last);
271 int flash_sect_erase (ulong addr_first, ulong addr_last)
275 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
280 rcode = flash_fill_sect_ranges (addr_first, addr_last,
281 s_first, s_last, &planned );
283 if (planned && (rcode == 0)) {
284 for (bank=0,info=&flash_info[0];
285 (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
287 if (s_first[bank]>=0) {
288 erased += s_last[bank] - s_first[bank] + 1;
289 debug ("Erase Flash from 0x%08lx to 0x%08lx "
291 info->start[s_first[bank]],
292 (s_last[bank] == info->sector_count) ?
293 info->start[0] + info->size - 1:
294 info->start[s_last[bank]+1] - 1,
296 rcode = flash_erase (info, s_first[bank], s_last[bank]);
299 printf ("Erased %d sectors\n", erased);
300 } else if (rcode == 0) {
301 puts ("Error: start and/or end address"
302 " not on sector boundary\n");
308 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
311 ulong bank, addr_first, addr_last;
312 int i, p, n, sect_first, sect_last;
314 #ifdef CONFIG_HAS_DATAFLASH
318 printf ("Usage:\n%s\n", cmdtp->usage);
322 if (strcmp(argv[1], "off") == 0) {
324 } else if (strcmp(argv[1], "on") == 0) {
327 printf ("Usage:\n%s\n", cmdtp->usage);
331 #ifdef CONFIG_HAS_DATAFLASH
332 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
333 addr_first = simple_strtoul(argv[2], NULL, 16);
334 addr_last = simple_strtoul(argv[3], NULL, 16);
336 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
337 status = dataflash_real_protect(p,addr_first,addr_last);
339 puts ("Bad DataFlash sector specification\n");
342 printf("%sProtect %d DataFlash Sectors\n",
343 p ? "" : "Un-", status);
349 if (strcmp(argv[2], "all") == 0) {
350 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
351 info = &flash_info[bank-1];
352 if (info->flash_id == FLASH_UNKNOWN) {
355 printf ("%sProtect Flash Bank # %ld\n",
356 p ? "" : "Un-", bank);
358 for (i=0; i<info->sector_count; ++i) {
359 #if defined(CFG_FLASH_PROTECTION)
360 if (flash_real_protect(info, i, p))
364 info->protect[i] = p;
365 #endif /* CFG_FLASH_PROTECTION */
369 #if defined(CFG_FLASH_PROTECTION)
370 if (!rcode) puts (" done\n");
371 #endif /* CFG_FLASH_PROTECTION */
376 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
378 puts ("Bad sector specification\n");
381 printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
382 p ? "" : "Un-", sect_first, sect_last,
383 (info-flash_info)+1);
384 for (i = sect_first; i <= sect_last; i++) {
385 #if defined(CFG_FLASH_PROTECTION)
386 if (flash_real_protect(info, i, p))
390 info->protect[i] = p;
391 #endif /* CFG_FLASH_PROTECTION */
394 #if defined(CFG_FLASH_PROTECTION)
395 if (!rcode) puts (" done\n");
396 #endif /* CFG_FLASH_PROTECTION */
402 printf ("Usage:\n%s\n", cmdtp->usage);
406 if (strcmp(argv[2], "bank") == 0) {
407 bank = simple_strtoul(argv[3], NULL, 16);
408 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
409 printf ("Only FLASH Banks # 1 ... # %d supported\n",
410 CFG_MAX_FLASH_BANKS);
413 printf ("%sProtect Flash Bank # %ld\n",
414 p ? "" : "Un-", bank);
415 info = &flash_info[bank-1];
417 if (info->flash_id == FLASH_UNKNOWN) {
418 puts ("missing or unknown FLASH type\n");
421 for (i=0; i<info->sector_count; ++i) {
422 #if defined(CFG_FLASH_PROTECTION)
423 if (flash_real_protect(info, i, p))
427 info->protect[i] = p;
428 #endif /* CFG_FLASH_PROTECTION */
431 #if defined(CFG_FLASH_PROTECTION)
432 if (!rcode) puts (" done\n");
433 #endif /* CFG_FLASH_PROTECTION */
438 addr_first = simple_strtoul(argv[2], NULL, 16);
439 addr_last = simple_strtoul(argv[3], NULL, 16);
441 if (addr_first >= addr_last) {
442 printf ("Usage:\n%s\n", cmdtp->usage);
445 rcode = flash_sect_protect (p, addr_first, addr_last);
450 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
454 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
459 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
463 if (planned && (rcode == 0)) {
464 for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
465 if (info->flash_id == FLASH_UNKNOWN) {
469 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
470 debug ("%sProtecting sectors %d..%d in bank %ld\n",
472 s_first[bank], s_last[bank], bank+1);
473 protected += s_last[bank] - s_first[bank] + 1;
474 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
475 #if defined(CFG_FLASH_PROTECTION)
476 if (flash_real_protect(info, i, p))
480 info->protect[i] = p;
481 #endif /* CFG_FLASH_PROTECTION */
484 #if defined(CFG_FLASH_PROTECTION)
485 if (!rcode) putc ('\n');
486 #endif /* CFG_FLASH_PROTECTION */
489 printf ("%sProtected %d sectors\n",
490 p ? "" : "Un-", protected);
491 } else if (rcode == 0) {
492 puts ("Error: start and/or end address"
493 " not on sector boundary\n");
500 /**************************************************/
503 flinfo, 2, 1, do_flinfo,
504 "flinfo - print FLASH memory information\n",
505 "\n - print information for all FLASH memory banks\n"
506 "flinfo N\n - print information for FLASH memory bank # N\n"
510 erase, 3, 1, do_flerase,
511 "erase - erase FLASH memory\n",
513 " - erase FLASH from addr 'start' to addr 'end'\n"
514 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
515 "erase bank N\n - erase FLASH bank # N\n"
516 "erase all\n - erase all FLASH banks\n"
520 protect, 4, 1, do_protect,
521 "protect - enable or disable FLASH write protection\n",
523 " - protect FLASH from addr 'start' to addr 'end'\n"
524 "protect on N:SF[-SL]\n"
525 " - protect sectors SF-SL in FLASH bank # N\n"
526 "protect on bank N\n - protect FLASH bank # N\n"
527 "protect on all\n - protect all FLASH banks\n"
528 "protect off start end\n"
529 " - make FLASH from addr 'start' to addr 'end' writable\n"
530 "protect off N:SF[-SL]\n"
531 " - make sectors SF-SL writable in FLASH bank # N\n"
532 "protect off bank N\n - make FLASH bank # N writable\n"
533 "protect off all\n - make all FLASH banks writable\n"
536 #endif /* CFG_CMD_FLASH */