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_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
46 #include <mtd/cfi_flash.h>
47 extern flash_info_t flash_info[]; /* info for FLASH chips */
50 * The user interface starts numbering for Flash banks with 1
51 * for historical reasons.
55 * this routine looks for an abbreviated flash range specification.
56 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
57 * sector to erase, and SL is the last sector to erase (defaults to SF).
58 * bank numbers start at 1 to be consistent with other specs, sector numbers
61 * returns: 1 - correct spec; *pinfo, *psf and *psl are
63 * 0 - doesn't look like an abbreviated spec
64 * -1 - looks like an abbreviated spec, but got
65 * a parsing error, a number out of range,
66 * or an invalid flash bank.
69 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
72 int bank, first, last;
75 if ((p = strchr (str, ':')) == NULL)
79 bank = simple_strtoul (str, &ep, 10);
80 if (ep == str || *ep != '\0' ||
81 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
82 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
86 if ((p = strchr (str, '-')) != NULL)
89 first = simple_strtoul (str, &ep, 10);
90 if (ep == str || *ep != '\0' || first >= fp->sector_count)
94 last = simple_strtoul (p, &ep, 10);
95 if (ep == p || *ep != '\0' ||
96 last < first || last >= fp->sector_count)
110 * Take *addr in Flash and adjust it to fall on the end of its sector
112 int flash_sect_roundb (ulong *addr)
115 ulong bank, sector_end_addr;
119 /* find the end addr of the sector where the *addr is */
121 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS && !found; ++bank) {
122 info = &flash_info[bank];
123 for (i = 0; i < info->sector_count && !found; ++i) {
124 /* get the end address of the sector */
125 if (i == info->sector_count - 1) {
126 sector_end_addr = info->start[0] +
129 sector_end_addr = info->start[i+1] - 1;
132 if (*addr <= sector_end_addr &&
133 *addr >= info->start[i]) {
135 /* adjust *addr if necessary */
136 if (*addr < sector_end_addr)
137 *addr = sector_end_addr;
142 /* error, addres not in flash */
143 printf("Error: end address (0x%08lx) not in flash!\n", *addr);
151 * This function computes the start and end addresses for both
152 * erase and protect commands. The range of the addresses on which
153 * either of the commands is to operate can be given in two forms:
154 * 1. <cmd> start end - operate on <'start', 'end')
155 * 2. <cmd> start +length - operate on <'start', start + length)
156 * If the second form is used and the end address doesn't fall on the
157 * sector boundary, than it will be adjusted to the next sector boundary.
158 * If it isn't in the flash, the function will fail (return -1).
160 * arg1, arg2: address specification (i.e. both command arguments)
162 * addr_first, addr_last: computed address range
165 * -1: failure (bad format, bad address).
168 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
171 char len_used; /* indicates if the "start +length" form used */
173 *addr_first = simple_strtoul(arg1, &ep, 16);
174 if (ep == arg1 || *ep != '\0')
178 if (arg2 && *arg2 == '+'){
183 *addr_last = simple_strtoul(arg2, &ep, 16);
184 if (ep == arg2 || *ep != '\0')
189 * *addr_last has the length, compute correct *addr_last
190 * XXX watch out for the integer overflow! Right now it is
191 * checked for in both the callers.
193 *addr_last = *addr_first + *addr_last - 1;
196 * It may happen that *addr_last doesn't fall on the sector
197 * boundary. We want to round such an address to the next
198 * sector boundary, so that the commands don't fail later on.
201 if (flash_sect_roundb(addr_last) > 0)
203 } /* "start +length" from used */
209 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
210 int *s_first, int *s_last,
219 for (bank=0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
220 s_first[bank] = -1; /* first sector to erase */
221 s_last [bank] = -1; /* last sector to erase */
224 for (bank=0,info = &flash_info[0];
225 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (addr_first <= addr_last);
231 if (info->flash_id == FLASH_UNKNOWN) {
235 b_end = info->start[0] + info->size - 1; /* bank end addr */
236 s_end = info->sector_count - 1; /* last sector */
239 for (sect=0; sect < info->sector_count; ++sect) {
240 ulong end; /* last address in current sect */
242 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
244 if (addr_first > end)
246 if (addr_last < info->start[sect])
249 if (addr_first == info->start[sect]) {
250 s_first[bank] = sect;
252 if (addr_last == end) {
256 if (s_first[bank] >= 0) {
257 if (s_last[bank] < 0) {
258 if (addr_last > b_end) {
259 s_last[bank] = s_end;
261 puts ("Error: end address"
262 " not on sector boundary\n");
267 if (s_last[bank] < s_first[bank]) {
268 puts ("Error: end sector"
269 " precedes start sector\n");
274 addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
275 (*s_count) += s_last[bank] - s_first[bank] + 1;
276 } else if (addr_first >= info->start[0] && addr_first < b_end) {
277 puts ("Error: start address not on sector boundary\n");
280 } else if (s_last[bank] >= 0) {
281 puts ("Error: cannot span across banks when they are"
282 " mapped in reverse order\n");
290 #endif /* CONFIG_SYS_NO_FLASH */
292 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
294 #ifndef CONFIG_SYS_NO_FLASH
298 #ifdef CONFIG_HAS_DATAFLASH
299 dataflash_print_info();
302 #ifndef CONFIG_SYS_NO_FLASH
303 if (argc == 1) { /* print info for all FLASH banks */
304 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
305 printf ("\nBank # %ld: ", bank+1);
307 flash_print_info (&flash_info[bank]);
312 bank = simple_strtoul(argv[1], NULL, 16);
313 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
314 printf ("Only FLASH Banks # 1 ... # %d supported\n",
315 CONFIG_SYS_MAX_FLASH_BANKS);
318 printf ("\nBank # %ld: ", bank);
319 flash_print_info (&flash_info[bank-1]);
320 #endif /* CONFIG_SYS_NO_FLASH */
324 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
326 #ifndef CONFIG_SYS_NO_FLASH
328 ulong bank, addr_first, addr_last;
329 int n, sect_first, sect_last;
330 #if defined(CONFIG_CMD_MTDPARTS)
331 struct mtd_device *dev;
332 struct part_info *part;
333 u8 dev_type, dev_num, pnum;
338 return cmd_usage(cmdtp);
340 if (strcmp(argv[1], "all") == 0) {
341 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
342 printf ("Erase Flash Bank # %ld ", bank);
343 info = &flash_info[bank-1];
344 rcode = flash_erase (info, 0, info->sector_count-1);
349 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
351 puts ("Bad sector specification\n");
354 printf ("Erase Flash Sectors %d-%d in Bank # %zu ",
355 sect_first, sect_last, (info-flash_info)+1);
356 rcode = flash_erase(info, sect_first, sect_last);
360 #if defined(CONFIG_CMD_MTDPARTS)
361 /* erase <part-id> - erase partition */
362 if ((argc == 2) && (mtd_id_parse(argv[1], NULL, &dev_type, &dev_num) == 0)) {
364 if (find_dev_and_part(argv[1], &dev, &pnum, &part) == 0) {
365 if (dev->id->type == MTD_DEV_TYPE_NOR) {
367 info = &flash_info[bank];
368 addr_first = part->offset + info->start[0];
369 addr_last = addr_first + part->size - 1;
371 printf ("Erase Flash Partition %s, "
372 "bank %ld, 0x%08lx - 0x%08lx ",
373 argv[1], bank, addr_first,
376 rcode = flash_sect_erase(addr_first, addr_last);
380 printf("cannot erase, not a NOR device\n");
387 return cmd_usage(cmdtp);
389 if (strcmp(argv[1], "bank") == 0) {
390 bank = simple_strtoul(argv[2], NULL, 16);
391 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
392 printf ("Only FLASH Banks # 1 ... # %d supported\n",
393 CONFIG_SYS_MAX_FLASH_BANKS);
396 printf ("Erase Flash Bank # %ld ", bank);
397 info = &flash_info[bank-1];
398 rcode = flash_erase (info, 0, info->sector_count-1);
402 if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
403 printf ("Bad address format\n");
407 if (addr_first >= addr_last)
408 return cmd_usage(cmdtp);
410 rcode = flash_sect_erase(addr_first, addr_last);
414 #endif /* CONFIG_SYS_NO_FLASH */
417 #ifndef CONFIG_SYS_NO_FLASH
418 int flash_sect_erase (ulong addr_first, ulong addr_last)
422 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
427 rcode = flash_fill_sect_ranges (addr_first, addr_last,
428 s_first, s_last, &planned );
430 if (planned && (rcode == 0)) {
431 for (bank=0,info = &flash_info[0];
432 (bank < CONFIG_SYS_MAX_FLASH_BANKS) && (rcode == 0);
434 if (s_first[bank]>=0) {
435 erased += s_last[bank] - s_first[bank] + 1;
436 debug ("Erase Flash from 0x%08lx to 0x%08lx "
438 info->start[s_first[bank]],
439 (s_last[bank] == info->sector_count) ?
440 info->start[0] + info->size - 1:
441 info->start[s_last[bank]+1] - 1,
443 rcode = flash_erase (info, s_first[bank], s_last[bank]);
446 printf ("Erased %d sectors\n", erased);
447 } else if (rcode == 0) {
448 puts ("Error: start and/or end address"
449 " not on sector boundary\n");
454 #endif /* CONFIG_SYS_NO_FLASH */
456 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
458 #ifndef CONFIG_SYS_NO_FLASH
461 int i, n, sect_first, sect_last;
462 #if defined(CONFIG_CMD_MTDPARTS)
463 struct mtd_device *dev;
464 struct part_info *part;
465 u8 dev_type, dev_num, pnum;
467 #endif /* CONFIG_SYS_NO_FLASH */
468 #if !defined(CONFIG_SYS_NO_FLASH) || defined(CONFIG_HAS_DATAFLASH)
469 ulong addr_first, addr_last;
471 #ifdef CONFIG_HAS_DATAFLASH
478 return cmd_usage(cmdtp);
480 if (strcmp(argv[1], "off") == 0)
482 else if (strcmp(argv[1], "on") == 0)
485 return cmd_usage(cmdtp);
487 #ifdef CONFIG_HAS_DATAFLASH
488 if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
489 addr_first = simple_strtoul(argv[2], NULL, 16);
490 addr_last = simple_strtoul(argv[3], NULL, 16);
492 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
493 status = dataflash_real_protect(p,addr_first,addr_last);
495 puts ("Bad DataFlash sector specification\n");
498 printf("%sProtect %d DataFlash Sectors\n",
499 p ? "" : "Un-", status);
505 #ifndef CONFIG_SYS_NO_FLASH
506 if (strcmp(argv[2], "all") == 0) {
507 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
508 info = &flash_info[bank-1];
509 if (info->flash_id == FLASH_UNKNOWN) {
512 printf ("%sProtect Flash Bank # %ld\n",
513 p ? "" : "Un-", bank);
515 for (i=0; i<info->sector_count; ++i) {
516 #if defined(CONFIG_SYS_FLASH_PROTECTION)
517 if (flash_real_protect(info, i, p))
521 info->protect[i] = p;
522 #endif /* CONFIG_SYS_FLASH_PROTECTION */
524 #if defined(CONFIG_SYS_FLASH_PROTECTION)
525 if (!rcode) puts (" done\n");
526 #endif /* CONFIG_SYS_FLASH_PROTECTION */
531 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
533 puts ("Bad sector specification\n");
536 printf("%sProtect Flash Sectors %d-%d in Bank # %zu\n",
537 p ? "" : "Un-", sect_first, sect_last,
538 (info-flash_info)+1);
539 for (i = sect_first; i <= sect_last; i++) {
540 #if defined(CONFIG_SYS_FLASH_PROTECTION)
541 if (flash_real_protect(info, i, p))
545 info->protect[i] = p;
546 #endif /* CONFIG_SYS_FLASH_PROTECTION */
549 #if defined(CONFIG_SYS_FLASH_PROTECTION)
550 if (!rcode) puts (" done\n");
551 #endif /* CONFIG_SYS_FLASH_PROTECTION */
556 #if defined(CONFIG_CMD_MTDPARTS)
557 /* protect on/off <part-id> */
558 if ((argc == 3) && (mtd_id_parse(argv[2], NULL, &dev_type, &dev_num) == 0)) {
560 if (find_dev_and_part(argv[2], &dev, &pnum, &part) == 0) {
561 if (dev->id->type == MTD_DEV_TYPE_NOR) {
563 info = &flash_info[bank];
564 addr_first = part->offset + info->start[0];
565 addr_last = addr_first + part->size - 1;
567 printf ("%sProtect Flash Partition %s, "
568 "bank %ld, 0x%08lx - 0x%08lx\n",
569 p ? "" : "Un", argv[1],
570 bank, addr_first, addr_last);
572 rcode = flash_sect_protect (p, addr_first, addr_last);
576 printf("cannot %sprotect, not a NOR device\n",
584 return cmd_usage(cmdtp);
586 if (strcmp(argv[2], "bank") == 0) {
587 bank = simple_strtoul(argv[3], NULL, 16);
588 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
589 printf ("Only FLASH Banks # 1 ... # %d supported\n",
590 CONFIG_SYS_MAX_FLASH_BANKS);
593 printf ("%sProtect Flash Bank # %ld\n",
594 p ? "" : "Un-", bank);
595 info = &flash_info[bank-1];
597 if (info->flash_id == FLASH_UNKNOWN) {
598 puts ("missing or unknown FLASH type\n");
601 for (i=0; i<info->sector_count; ++i) {
602 #if defined(CONFIG_SYS_FLASH_PROTECTION)
603 if (flash_real_protect(info, i, p))
607 info->protect[i] = p;
608 #endif /* CONFIG_SYS_FLASH_PROTECTION */
611 #if defined(CONFIG_SYS_FLASH_PROTECTION)
612 if (!rcode) puts (" done\n");
613 #endif /* CONFIG_SYS_FLASH_PROTECTION */
618 if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
619 printf("Bad address format\n");
623 if (addr_first >= addr_last)
624 return cmd_usage(cmdtp);
626 rcode = flash_sect_protect (p, addr_first, addr_last);
627 #endif /* CONFIG_SYS_NO_FLASH */
631 #ifndef CONFIG_SYS_NO_FLASH
632 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
636 int s_first[CONFIG_SYS_MAX_FLASH_BANKS], s_last[CONFIG_SYS_MAX_FLASH_BANKS];
641 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
645 if (planned && (rcode == 0)) {
646 for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
647 if (info->flash_id == FLASH_UNKNOWN) {
651 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
652 debug ("%sProtecting sectors %d..%d in bank %ld\n",
654 s_first[bank], s_last[bank], bank+1);
655 protected += s_last[bank] - s_first[bank] + 1;
656 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
657 #if defined(CONFIG_SYS_FLASH_PROTECTION)
658 if (flash_real_protect(info, i, p))
662 info->protect[i] = p;
663 #endif /* CONFIG_SYS_FLASH_PROTECTION */
667 #if defined(CONFIG_SYS_FLASH_PROTECTION)
669 #endif /* CONFIG_SYS_FLASH_PROTECTION */
671 printf ("%sProtected %d sectors\n",
672 p ? "" : "Un-", protected);
673 } else if (rcode == 0) {
674 puts ("Error: start and/or end address"
675 " not on sector boundary\n");
680 #endif /* CONFIG_SYS_NO_FLASH */
683 /**************************************************/
684 #if defined(CONFIG_CMD_MTDPARTS)
685 # define TMP_ERASE "erase <part-id>\n - erase partition\n"
686 # define TMP_PROT_ON "protect on <part-id>\n - protect partition\n"
687 # define TMP_PROT_OFF "protect off <part-id>\n - make partition writable\n"
689 # define TMP_ERASE /* empty */
690 # define TMP_PROT_ON /* empty */
691 # define TMP_PROT_OFF /* empty */
695 flinfo, 2, 1, do_flinfo,
696 "print FLASH memory information",
697 "\n - print information for all FLASH memory banks\n"
698 "flinfo N\n - print information for FLASH memory bank # N"
702 erase, 3, 0, do_flerase,
703 "erase FLASH memory",
705 " - erase FLASH from addr 'start' to addr 'end'\n"
707 " - erase FLASH from addr 'start' to the end of sect "
708 "w/addr 'start'+'len'-1\n"
709 "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
710 "erase bank N\n - erase FLASH bank # N\n"
712 "erase all\n - erase all FLASH banks"
716 protect, 4, 0, do_protect,
717 "enable or disable FLASH write protection",
719 " - protect FLASH from addr 'start' to addr 'end'\n"
720 "protect on start +len\n"
721 " - protect FLASH from addr 'start' to end of sect "
722 "w/addr 'start'+'len'-1\n"
723 "protect on N:SF[-SL]\n"
724 " - protect sectors SF-SL in FLASH bank # N\n"
725 "protect on bank N\n - protect FLASH bank # N\n"
727 "protect on all\n - protect all FLASH banks\n"
728 "protect off start end\n"
729 " - make FLASH from addr 'start' to addr 'end' writable\n"
730 "protect off start +len\n"
731 " - make FLASH from addr 'start' to end of sect "
732 "w/addr 'start'+'len'-1 wrtable\n"
733 "protect off N:SF[-SL]\n"
734 " - make sectors SF-SL writable in FLASH bank # N\n"
735 "protect off bank N\n - make FLASH bank # N writable\n"
737 "protect off all\n - make all FLASH banks writable"