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,
30 #ifdef CONFIG_HAS_DATAFLASH
31 #include <dataflash.h>
34 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
35 #include <jffs2/jffs2.h>
37 /* partition handling routines */
38 int mtdparts_init(void);
39 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
40 int find_dev_and_part(const char *id, struct mtd_device **dev,
41 u8 *part_num, struct part_info **part);
44 #ifndef CONFIG_SYS_NO_FLASH
45 extern flash_info_t flash_info[]; /* info for FLASH chips */
48 * The user interface starts numbering for Flash banks with 1
49 * for historical reasons.
53 * this routine looks for an abbreviated flash range specification.
54 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
55 * sector to erase, and SL is the last sector to erase (defaults to SF).
56 * bank numbers start at 1 to be consistent with other specs, sector numbers
59 * returns: 1 - correct spec; *pinfo, *psf and *psl are
61 * 0 - doesn't look like an abbreviated spec
62 * -1 - looks like an abbreviated spec, but got
63 * a parsing error, a number out of range,
64 * or an invalid flash bank.
67 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
70 int bank, first, last;
73 if ((p = strchr (str, ':')) == NULL)
77 bank = simple_strtoul (str, &ep, 10);
78 if (ep == str || *ep != '\0' ||
79 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
80 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
84 if ((p = strchr (str, '-')) != NULL)
87 first = simple_strtoul (str, &ep, 10);
88 if (ep == str || *ep != '\0' || first >= fp->sector_count)
92 last = simple_strtoul (p, &ep, 10);
93 if (ep == p || *ep != '\0' ||
94 last < first || last >= fp->sector_count)
108 * Take *addr in Flash and adjust it to fall on the end of its sector
110 int flash_sect_roundb (ulong *addr)
113 ulong bank, sector_end_addr;
117 /* find the end addr of the sector where the *addr is */
119 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
120 info = &flash_info[bank];
121 for (i = 0; i < info->sector_count && !found; ++i) {
122 /* get the end address of the sector */
123 if (i == info->sector_count - 1) {
124 sector_end_addr = info->start[0] +
127 sector_end_addr = info->start[i+1] - 1;
130 if (*addr <= sector_end_addr &&
131 *addr >= info->start[i]) {
133 /* adjust *addr if necessary */
134 if (*addr < sector_end_addr)
135 *addr = sector_end_addr;
140 /* error, addres not in flash */
141 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
149 * This function computes the start and end addresses for both
150 * erase and protect commands. The range of the addresses on which
151 * either of the commands is to operate can be given in two forms:
152 * 1. <cmd> start end - operate on <'start', 'end')
153 * 2. <cmd> start +length - operate on <'start', start + length)
154 * If the second form is used and the end address doesn't fall on the
155 * sector boundary, than it will be adjusted to the next sector boundary.
156 * If it isn't in the flash, the function will fail (return -1).
158 * arg1, arg2: address specification (i.e. both command arguments)
160 * addr_first, addr_last: computed address range
163 * -1: failure (bad format, bad address).
166 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
169 char len_used; /* indicates if the "start +length" form used */
171 *addr_first = simple_strtoul(arg1, &ep, 16);
172 if (ep == arg1 || *ep != '\0')
176 if (arg2 && *arg2 == '+'){
181 *addr_last = simple_strtoul(arg2, &ep, 16);
182 if (ep == arg2 || *ep != '\0')
187 * *addr_last has the length, compute correct *addr_last
188 * XXX watch out for the integer overflow! Right now it is
189 * checked for in both the callers.
191 *addr_last = *addr_first + *addr_last - 1;
194 * It may happen that *addr_last doesn't fall on the sector
195 * boundary. We want to round such an address to the next
196 * sector boundary, so that the commands don't fail later on.
199 if (flash_sect_roundb(addr_last) > 0)
201 } /* "start +length" from used */
207 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
208 int *s_first, int *s_last,
217 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
218 s_first[bank] = -1; /* first sector to erase */
219 s_last [bank] = -1; /* last sector to erase */
222 for (bank=0,info = &flash_info[0];
223 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
229 if (info->flash_id == FLASH_UNKNOWN) {
233 b_end = info->start[0] + info->size - 1; /* bank end addr */
234 s_end = info->sector_count - 1; /* last sector */
237 for (sect=0; sect < info->sector_count; ++sect) {
238 ulong end; /* last address in current sect */
240 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
242 if (addr_first > end)
244 if (addr_last < info->start[sect])
247 if (addr_first == info->start[sect]) {
248 s_first[bank] = sect;
250 if (addr_last == end) {
254 if (s_first[bank] >= 0) {
255 if (s_last[bank] < 0) {
256 if (addr_last > b_end) {
257 s_last[bank] = s_end;
259 puts ("Error: end address"
260 " not on sector boundary\n");
265 if (s_last[bank] < s_first[bank]) {
266 puts ("Error: end sector"
267 " precedes start sector\n");
272 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
273 (*s_count) += s_last[bank] - s_first[bank] + 1;
274 } else if (addr_first >= info->start[0] && addr_first < b_end) {
275 puts ("Error: start address not on sector boundary\n");
278 } else if (s_last[bank] >= 0) {
279 puts ("Error: cannot span across banks when they are"
280 " mapped in reverse order\n");
288 #endif /* CONFIG_SYS_NO_FLASH */
290 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
292 #ifndef CONFIG_SYS_NO_FLASH
296 #ifdef CONFIG_HAS_DATAFLASH
297 dataflash_print_info();
300 #ifndef CONFIG_SYS_NO_FLASH
301 if (argc == 1) { /* print info for all FLASH banks */
302 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
303 printf ("\nBank # %ld: ", bank+1);
305 flash_print_info (&flash_info[bank]);
310 bank = simple_strtoul(argv[1], NULL, 16);
311 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
312 printf ("Only FLASH Banks # 1 ... # %d supported\n",
313 CONFIG_SYS_MAX_FLASH_BANKS);
316 printf ("\nBank # %ld: ", bank);
317 flash_print_info (&flash_info[bank-1]);
318 #endif /* CONFIG_SYS_NO_FLASH */
322 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 #ifndef CONFIG_SYS_NO_FLASH
326 ulong bank, addr_first, addr_last;
327 int n, sect_first, sect_last;
328 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
329 struct mtd_device *dev;
330 struct part_info *part;
331 u8 dev_type, dev_num, pnum;
336 return cmd_usage(cmdtp);
338 if (strcmp(argv[1], "all") == 0) {
339 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
340 printf ("Erase Flash Bank # %ld ", bank);
341 info = &flash_info[bank-1];
342 rcode = flash_erase (info, 0, info->sector_count-1);
347 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
349 puts ("Bad sector specification\n");
352 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
353 sect_first, sect_last, (info-flash_info)+1);
354 rcode = flash_erase(info, sect_first, sect_last);
358 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
359 /* erase <part-id> - erase partition */
360 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
362 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
363 if (dev->id->type == MTD_DEV_TYPE_NOR) {
365 info = &flash_info[bank];
366 addr_first = part->offset + info->start[0];
367 addr_last = addr_first + part->size - 1;
369 printf ("Erase Flash Partition %s, "
370 "bank %ld, 0x%08lx - 0x%08lx ",
371 argv[1], bank, addr_first,
374 rcode = flash_sect_erase(addr_first, addr_last);
378 printf("cannot erase, not a NOR device\n");
385 return cmd_usage(cmdtp);
387 if (strcmp(argv[1], "bank") == 0) {
388 bank = simple_strtoul(argv[2], NULL, 16);
389 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
390 printf ("Only FLASH Banks # 1 ... # %d supported\n",
391 CONFIG_SYS_MAX_FLASH_BANKS);
394 printf ("Erase Flash Bank # %ld ", bank);
395 info = &flash_info[bank-1];
396 rcode = flash_erase (info, 0, info->sector_count-1);
400 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
401 printf ("Bad address format\n");
405 if (addr_first >= addr_last)
406 return cmd_usage(cmdtp);
408 rcode = flash_sect_erase(addr_first, addr_last);
412 #endif /* CONFIG_SYS_NO_FLASH */
415 #ifndef CONFIG_SYS_NO_FLASH
416 int flash_sect_erase (ulong addr_first, ulong addr_last)
420 #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
421 int s_first[CONFIG_SYS_MAX_FLASH_BANKS_DETECT], s_last[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
423 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
429 rcode = flash_fill_sect_ranges (addr_first, addr_last,
430 s_first, s_last, &planned );
432 if (planned && (rcode == 0)) {
433 for (bank=0,info = &flash_info[0];
434 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
436 if (s_first[bank]>=0) {
437 erased += s_last[bank] - s_first[bank] + 1;
438 debug ("Erase Flash from 0x%08lx to 0x%08lx "
440 info->start[s_first[bank]],
441 (s_last[bank] == info->sector_count) ?
442 info->start[0] + info->size - 1:
443 info->start[s_last[bank]+1] - 1,
445 rcode = flash_erase (info, s_first[bank], s_last[bank]);
448 printf ("Erased %d sectors\n", erased);
449 } else if (rcode == 0) {
450 puts ("Error: start and/or end address"
451 " not on sector boundary\n");
456 #endif /* CONFIG_SYS_NO_FLASH */
458 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
460 #ifndef CONFIG_SYS_NO_FLASH
463 int i, n, sect_first, sect_last;
464 #endif /* CONFIG_SYS_NO_FLASH */
465 #if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
466 ulong addr_first, addr_last;
468 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
469 struct mtd_device *dev;
470 struct part_info *part;
471 u8 dev_type, dev_num, pnum;
473 #ifdef CONFIG_HAS_DATAFLASH
480 return cmd_usage(cmdtp);
482 if (strcmp(argv[1], "off") == 0)
484 else if (strcmp(argv[1], "on") == 0)
487 return cmd_usage(cmdtp);
489 #ifdef CONFIG_HAS_DATAFLASH
490 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
491 addr_first = simple_strtoul(argv[2], NULL, 16);
492 addr_last = simple_strtoul(argv[3], NULL, 16);
494 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
495 status = dataflash_real_protect(p,addr_first,addr_last);
497 puts ("Bad DataFlash sector specification\n");
500 printf("%sProtect %d DataFlash Sectors\n",
501 p ? "" : "Un-", status);
507 #ifndef CONFIG_SYS_NO_FLASH
508 if (strcmp(argv[2], "all") == 0) {
509 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
510 info = &flash_info[bank-1];
511 if (info->flash_id == FLASH_UNKNOWN) {
514 printf ("%sProtect Flash Bank # %ld\n",
515 p ? "" : "Un-", bank);
517 for (i=0; i<info->sector_count; ++i) {
518 #if defined(CONFIG_SYS_FLASH_PROTECTION)
519 if (flash_real_protect(info, i, p))
523 info->protect[i] = p;
524 #endif /* CONFIG_SYS_FLASH_PROTECTION */
526 #if defined(CONFIG_SYS_FLASH_PROTECTION)
527 if (!rcode) puts (" done\n");
528 #endif /* CONFIG_SYS_FLASH_PROTECTION */
533 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
535 puts ("Bad sector specification\n");
538 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
539 p ? "" : "Un-", sect_first, sect_last,
540 (info-flash_info)+1);
541 for (i = sect_first; i <= sect_last; i++) {
542 #if defined(CONFIG_SYS_FLASH_PROTECTION)
543 if (flash_real_protect(info, i, p))
547 info->protect[i] = p;
548 #endif /* CONFIG_SYS_FLASH_PROTECTION */
551 #if defined(CONFIG_SYS_FLASH_PROTECTION)
552 if (!rcode) puts (" done\n");
553 #endif /* CONFIG_SYS_FLASH_PROTECTION */
558 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
559 /* protect on/off <part-id> */
560 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
562 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
563 if (dev->id->type == MTD_DEV_TYPE_NOR) {
565 info = &flash_info[bank];
566 addr_first = part->offset + info->start[0];
567 addr_last = addr_first + part->size - 1;
569 printf ("%sProtect Flash Partition %s, "
570 "bank %ld, 0x%08lx - 0x%08lx\n",
571 p ? "" : "Un", argv[1],
572 bank, addr_first, addr_last);
574 rcode = flash_sect_protect (p, addr_first, addr_last);
578 printf("cannot %sprotect, not a NOR device\n",
586 return cmd_usage(cmdtp);
588 if (strcmp(argv[2], "bank") == 0) {
589 bank = simple_strtoul(argv[3], NULL, 16);
590 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
591 printf ("Only FLASH Banks # 1 ... # %d supported\n",
592 CONFIG_SYS_MAX_FLASH_BANKS);
595 printf ("%sProtect Flash Bank # %ld\n",
596 p ? "" : "Un-", bank);
597 info = &flash_info[bank-1];
599 if (info->flash_id == FLASH_UNKNOWN) {
600 puts ("missing or unknown FLASH type\n");
603 for (i=0; i<info->sector_count; ++i) {
604 #if defined(CONFIG_SYS_FLASH_PROTECTION)
605 if (flash_real_protect(info, i, p))
609 info->protect[i] = p;
610 #endif /* CONFIG_SYS_FLASH_PROTECTION */
613 #if defined(CONFIG_SYS_FLASH_PROTECTION)
614 if (!rcode) puts (" done\n");
615 #endif /* CONFIG_SYS_FLASH_PROTECTION */
620 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
621 printf("Bad address format\n");
625 if (addr_first >= addr_last)
626 return cmd_usage(cmdtp);
628 rcode = flash_sect_protect (p, addr_first, addr_last);
629 #endif /* CONFIG_SYS_NO_FLASH */
633 #ifndef CONFIG_SYS_NO_FLASH
634 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
638 #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
639 int s_first[CONFIG_SYS_MAX_FLASH_BANKS_DETECT], s_last[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
641 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
647 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
651 if (planned && (rcode == 0)) {
652 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
653 if (info->flash_id == FLASH_UNKNOWN) {
657 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
658 debug ("%sProtecting sectors %d..%d in bank %ld\n",
660 s_first[bank], s_last[bank], bank+1);
661 protected += s_last[bank] - s_first[bank] + 1;
662 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
663 #if defined(CONFIG_SYS_FLASH_PROTECTION)
664 if (flash_real_protect(info, i, p))
668 info->protect[i] = p;
669 #endif /* CONFIG_SYS_FLASH_PROTECTION */
673 #if defined(CONFIG_SYS_FLASH_PROTECTION)
675 #endif /* CONFIG_SYS_FLASH_PROTECTION */
677 printf ("%sProtected %d sectors\n",
678 p ? "" : "Un-", protected);
679 } else if (rcode == 0) {
680 puts ("Error: start and/or end address"
681 " not on sector boundary\n");
686 #endif /* CONFIG_SYS_NO_FLASH */
689 /**************************************************/
690 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
691 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
692 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
693 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
695 # define TMP_ERASE /* empty */
696 # define TMP_PROT_ON /* empty */
697 # define TMP_PROT_OFF /* empty */
701 flinfo, 2, 1, do_flinfo,
702 "print FLASH memory information",
703 "\n - print information for all FLASH memory banks\n"
704 "flinfo N\n - print information for FLASH memory bank # N"
708 erase, 3, 0, do_flerase,
709 "erase FLASH memory",
711 " - erase FLASH from addr 'start' to addr 'end'\n"
713 " - erase FLASH from addr 'start' to the end of sect "
714 "w/addr 'start'+'len'-1\n"
715 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
716 "erase bank N\n - erase FLASH bank # N\n"
718 "erase all\n - erase all FLASH banks"
722 protect, 4, 0, do_protect,
723 "enable or disable FLASH write protection",
725 " - protect FLASH from addr 'start' to addr 'end'\n"
726 "protect on start +len\n"
727 " - protect FLASH from addr 'start' to end of sect "
728 "w/addr 'start'+'len'-1\n"
729 "protect on N:SF[-SL]\n"
730 " - protect sectors SF-SL in FLASH bank # N\n"
731 "protect on bank N\n - protect FLASH bank # N\n"
733 "protect on all\n - protect all FLASH banks\n"
734 "protect off start end\n"
735 " - make FLASH from addr 'start' to addr 'end' writable\n"
736 "protect off start +len\n"
737 " - make FLASH from addr 'start' to end of sect "
738 "w/addr 'start'+'len'-1 wrtable\n"
739 "protect off N:SF[-SL]\n"
740 " - make sectors SF-SL writable in FLASH bank # N\n"
741 "protect off bank N\n - make FLASH bank # N writable\n"
743 "protect off all\n - make all FLASH banks writable"