]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/cmd_flash.c
Merge with /home/sr/git/u-boot
[karo-tx-uboot.git] / common / cmd_flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * FLASH support
26  */
27 #include <common.h>
28 #include <command.h>
29
30
31 #ifdef CONFIG_HAS_DATAFLASH
32 #include <dataflash.h>
33 #endif
34
35 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
36
37 extern flash_info_t flash_info[];       /* info for FLASH chips */
38
39 /*
40  * The user interface starts numbering for Flash banks with 1
41  * for historical reasons.
42  */
43
44 /*
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
49  * start at zero.
50  *
51  * returns:     1       - correct spec; *pinfo, *psf and *psl are
52  *                        set appropriately
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.
57  */
58 static int
59 abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
60 {
61         flash_info_t *fp;
62         int bank, first, last;
63         char *p, *ep;
64
65         if ((p = strchr (str, ':')) == NULL)
66                 return 0;
67         *p++ = '\0';
68
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)
73                 return -1;
74
75         str = p;
76         if ((p = strchr (str, '-')) != NULL)
77                 *p++ = '\0';
78
79         first = simple_strtoul (str, &ep, 10);
80         if (ep == str || *ep != '\0' || first >= fp->sector_count)
81                 return -1;
82
83         if (p != NULL) {
84                 last = simple_strtoul (p, &ep, 10);
85                 if (ep == p || *ep != '\0' ||
86                         last < first || last >= fp->sector_count)
87                         return -1;
88         } else {
89                 last = first;
90         }
91
92         *pinfo = fp;
93         *psf = first;
94         *psl = last;
95
96         return 1;
97 }
98
99 /*
100  * This function computes the start and end addresses for both
101  * erase and protect commands. The range of the addresses on which
102  * either of the commands is to operate can be given in two forms:
103  * 1. <cmd> start end - operate on <'start',  'end')
104  * 2. <cmd> start +length - operate on <'start', start + length)
105  * If the second form is used and the end address doesn't fall on the
106  * sector boundary, than it will be adjusted to the next sector boundary.
107  * If it isn't in the flash, the function will fail (return -1).
108  * Input:
109  *    arg1, arg2: address specification (i.e. both command arguments)
110  * Output:
111  *    addr_first, addr_last: computed address range
112  * Return:
113  *    1: success
114  *   -1: failure (bad format, bad address).
115 */
116 static int
117 addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
118 {
119         char len_used = 0; /* indicates if the "start +length" form used */
120         char *ep;
121
122         *addr_first = simple_strtoul(arg1, &ep, 16);
123         if (ep == arg1 || *ep != '\0')
124                 return -1;
125
126         if (arg2 && *arg2 == '+'){
127                 len_used = 1;
128                 ++arg2;
129         }
130
131         *addr_last = simple_strtoul(arg2, &ep, 16);
132         if (ep == arg2 || *ep != '\0')
133                 return -1;
134
135         if (len_used){
136                 char found = 0;
137                 ulong bank;
138
139                 /*
140                  * *addr_last has the length, compute correct *addr_last
141                  * XXX watch out for the integer overflow! Right now it is
142                  * checked for in both the callers.
143                  */
144                 *addr_last = *addr_first + *addr_last - 1;
145
146                 /*
147                  * It may happen that *addr_last doesn't fall on the sector
148                  * boundary. We want to round such an address to the next
149                  * sector boundary, so that the commands don't fail later on.
150                  */
151
152                 /* find the end addr of the sector where the *addr_last is */
153                 for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
154                         int i;
155                         flash_info_t *info = &flash_info[bank];
156                         for (i = 0; i < info->sector_count && !found; ++i){
157                                 /* get the end address of the sector */
158                                 ulong sector_end_addr;
159                                 if (i == info->sector_count - 1){
160                                         sector_end_addr =
161                                                 info->start[0] + info->size - 1;
162                                 } else {
163                                         sector_end_addr =
164                                                 info->start[i+1] - 1;
165                                 }
166                                 if (*addr_last <= sector_end_addr &&
167                                                 *addr_last >= info->start[i]){
168                                         /* sector found */
169                                         found = 1;
170                                         /* adjust *addr_last if necessary */
171                                         if (*addr_last < sector_end_addr){
172                                                 *addr_last = sector_end_addr;
173                                         }
174                                 }
175                         } /* sector */
176                 } /* bank */
177                 if (!found){
178                         /* error, addres not in flash */
179                         printf("Error: end address (0x%08lx) not in flash!\n",
180                                                                 *addr_last);
181                         return -1;
182                 }
183         } /* "start +length" from used */
184
185         return 1;
186 }
187
188 static int
189 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
190                         int *s_first, int *s_last,
191                         int *s_count )
192 {
193         flash_info_t *info;
194         ulong bank;
195         int rcode = 0;
196
197         *s_count = 0;
198
199         for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
200                 s_first[bank] = -1;     /* first sector to erase        */
201                 s_last [bank] = -1;     /* last  sector to erase        */
202         }
203
204         for (bank=0,info=&flash_info[0];
205              (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
206              ++bank, ++info) {
207                 ulong b_end;
208                 int sect;
209                 short s_end;
210
211                 if (info->flash_id == FLASH_UNKNOWN) {
212                         continue;
213                 }
214
215                 b_end = info->start[0] + info->size - 1;        /* bank end addr */
216                 s_end = info->sector_count - 1;                 /* last sector   */
217
218
219                 for (sect=0; sect < info->sector_count; ++sect) {
220                         ulong end;      /* last address in current sect */
221
222                         end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
223
224                         if (addr_first > end)
225                                 continue;
226                         if (addr_last < info->start[sect])
227                                 continue;
228
229                         if (addr_first == info->start[sect]) {
230                                 s_first[bank] = sect;
231                         }
232                         if (addr_last  == end) {
233                                 s_last[bank]  = sect;
234                         }
235                 }
236                 if (s_first[bank] >= 0) {
237                         if (s_last[bank] < 0) {
238                                 if (addr_last > b_end) {
239                                         s_last[bank] = s_end;
240                                 } else {
241                                         puts ("Error: end address"
242                                                 " not on sector boundary\n");
243                                         rcode = 1;
244                                         break;
245                                 }
246                         }
247                         if (s_last[bank] < s_first[bank]) {
248                                 puts ("Error: end sector"
249                                         " precedes start sector\n");
250                                 rcode = 1;
251                                 break;
252                         }
253                         sect = s_last[bank];
254                         addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
255                         (*s_count) += s_last[bank] - s_first[bank] + 1;
256                 } else if (addr_first >= info->start[0] && addr_first < b_end) {
257                         puts ("Error: start address not on sector boundary\n");
258                         rcode = 1;
259                         break;
260                 } else if (s_last[bank] >= 0) {
261                         puts ("Error: cannot span across banks when they are"
262                                " mapped in reverse order\n");
263                         rcode = 1;
264                         break;
265                 }
266         }
267
268         return rcode;
269 }
270
271 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
272 {
273         ulong bank;
274
275 #ifdef CONFIG_HAS_DATAFLASH
276         dataflash_print_info();
277 #endif
278
279         if (argc == 1) {        /* print info for all FLASH banks */
280                 for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
281                         printf ("\nBank # %ld: ", bank+1);
282
283                         flash_print_info (&flash_info[bank]);
284                 }
285                 return 0;
286         }
287
288         bank = simple_strtoul(argv[1], NULL, 16);
289         if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
290                 printf ("Only FLASH Banks # 1 ... # %d supported\n",
291                         CFG_MAX_FLASH_BANKS);
292                 return 1;
293         }
294         printf ("\nBank # %ld: ", bank);
295         flash_print_info (&flash_info[bank-1]);
296         return 0;
297 }
298 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
299 {
300         flash_info_t *info;
301         ulong bank, addr_first, addr_last;
302         int n, sect_first, sect_last;
303         int rcode = 0;
304
305         if (argc < 2) {
306                 printf ("Usage:\n%s\n", cmdtp->usage);
307                 return 1;
308         }
309
310         if (strcmp(argv[1], "all") == 0) {
311                 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
312                         printf ("Erase Flash Bank # %ld ", bank);
313                         info = &flash_info[bank-1];
314                         rcode = flash_erase (info, 0, info->sector_count-1);
315                 }
316                 return rcode;
317         }
318
319         if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
320                 if (n < 0) {
321                         puts ("Bad sector specification\n");
322                         return 1;
323                 }
324                 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
325                         sect_first, sect_last, (info-flash_info)+1);
326                 rcode = flash_erase(info, sect_first, sect_last);
327                 return rcode;
328         }
329
330         if (argc != 3) {
331                 printf ("Usage:\n%s\n", cmdtp->usage);
332                 return 1;
333         }
334
335         if (strcmp(argv[1], "bank") == 0) {
336                 bank = simple_strtoul(argv[2], NULL, 16);
337                 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
338                         printf ("Only FLASH Banks # 1 ... # %d supported\n",
339                                 CFG_MAX_FLASH_BANKS);
340                         return 1;
341                 }
342                 printf ("Erase Flash Bank # %ld ", bank);
343                 info = &flash_info[bank-1];
344                 rcode = flash_erase (info, 0, info->sector_count-1);
345                 return rcode;
346         }
347
348         if (addr_spec(argv[1], argv[2], &addr_first, &addr_last) < 0){
349                 printf ("Bad address format\n");
350                 return 1;
351         }
352
353         if (addr_first >= addr_last) {
354                 printf ("Usage:\n%s\n", cmdtp->usage);
355                 return 1;
356         }
357
358         rcode = flash_sect_erase(addr_first, addr_last);
359         return rcode;
360 }
361
362 int flash_sect_erase (ulong addr_first, ulong addr_last)
363 {
364         flash_info_t *info;
365         ulong bank;
366         int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
367         int erased = 0;
368         int planned;
369         int rcode = 0;
370
371         rcode = flash_fill_sect_ranges (addr_first, addr_last,
372                                         s_first, s_last, &planned );
373
374         if (planned && (rcode == 0)) {
375                 for (bank=0,info=&flash_info[0];
376                      (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
377                      ++bank, ++info) {
378                         if (s_first[bank]>=0) {
379                                 erased += s_last[bank] - s_first[bank] + 1;
380                                 debug ("Erase Flash from 0x%08lx to 0x%08lx "
381                                         "in Bank # %ld ",
382                                         info->start[s_first[bank]],
383                                         (s_last[bank] == info->sector_count) ?
384                                                 info->start[0] + info->size - 1:
385                                                 info->start[s_last[bank]+1] - 1,
386                                         bank+1);
387                                 rcode = flash_erase (info, s_first[bank], s_last[bank]);
388                         }
389                 }
390                 printf ("Erased %d sectors\n", erased);
391         } else if (rcode == 0) {
392                 puts ("Error: start and/or end address"
393                         " not on sector boundary\n");
394                 rcode = 1;
395         }
396         return rcode;
397 }
398
399 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
400 {
401         flash_info_t *info;
402         ulong bank, addr_first, addr_last;
403         int i, p, n, sect_first, sect_last;
404         int rcode = 0;
405 #ifdef CONFIG_HAS_DATAFLASH
406         int status;
407 #endif
408         if (argc < 3) {
409                 printf ("Usage:\n%s\n", cmdtp->usage);
410                 return 1;
411         }
412
413         if (strcmp(argv[1], "off") == 0) {
414                 p = 0;
415         } else if (strcmp(argv[1], "on") == 0) {
416                 p = 1;
417         } else {
418                 printf ("Usage:\n%s\n", cmdtp->usage);
419                 return 1;
420         }
421
422 #ifdef CONFIG_HAS_DATAFLASH
423         if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
424                 addr_first = simple_strtoul(argv[2], NULL, 16);
425                 addr_last  = simple_strtoul(argv[3], NULL, 16);
426
427                 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
428                         status = dataflash_real_protect(p,addr_first,addr_last);
429                         if (status < 0){
430                                 puts ("Bad DataFlash sector specification\n");
431                                 return 1;
432                         }
433                         printf("%sProtect %d DataFlash Sectors\n",
434                                 p ? "" : "Un-", status);
435                         return 0;
436                 }
437         }
438 #endif
439
440         if (strcmp(argv[2], "all") == 0) {
441                 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
442                         info = &flash_info[bank-1];
443                         if (info->flash_id == FLASH_UNKNOWN) {
444                                 continue;
445                         }
446                         printf ("%sProtect Flash Bank # %ld\n",
447                                 p ? "" : "Un-", bank);
448
449                         for (i=0; i<info->sector_count; ++i) {
450 #if defined(CFG_FLASH_PROTECTION)
451                                 if (flash_real_protect(info, i, p))
452                                         rcode = 1;
453                                 putc ('.');
454 #else
455                                 info->protect[i] = p;
456 #endif  /* CFG_FLASH_PROTECTION */
457                         }
458                 }
459
460 #if defined(CFG_FLASH_PROTECTION)
461                 if (!rcode) puts (" done\n");
462 #endif  /* CFG_FLASH_PROTECTION */
463
464                 return rcode;
465         }
466
467         if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
468                 if (n < 0) {
469                         puts ("Bad sector specification\n");
470                         return 1;
471                 }
472                 printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
473                         p ? "" : "Un-", sect_first, sect_last,
474                         (info-flash_info)+1);
475                 for (i = sect_first; i <= sect_last; i++) {
476 #if defined(CFG_FLASH_PROTECTION)
477                         if (flash_real_protect(info, i, p))
478                                 rcode =  1;
479                         putc ('.');
480 #else
481                         info->protect[i] = p;
482 #endif  /* CFG_FLASH_PROTECTION */
483                 }
484
485 #if defined(CFG_FLASH_PROTECTION)
486                 if (!rcode) puts (" done\n");
487 #endif  /* CFG_FLASH_PROTECTION */
488
489                 return rcode;
490         }
491
492         if (argc != 4) {
493                 printf ("Usage:\n%s\n", cmdtp->usage);
494                 return 1;
495         }
496
497         if (strcmp(argv[2], "bank") == 0) {
498                 bank = simple_strtoul(argv[3], NULL, 16);
499                 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
500                         printf ("Only FLASH Banks # 1 ... # %d supported\n",
501                                 CFG_MAX_FLASH_BANKS);
502                         return 1;
503                 }
504                 printf ("%sProtect Flash Bank # %ld\n",
505                         p ? "" : "Un-", bank);
506                 info = &flash_info[bank-1];
507
508                 if (info->flash_id == FLASH_UNKNOWN) {
509                         puts ("missing or unknown FLASH type\n");
510                         return 1;
511                 }
512                 for (i=0; i<info->sector_count; ++i) {
513 #if defined(CFG_FLASH_PROTECTION)
514                         if (flash_real_protect(info, i, p))
515                                 rcode =  1;
516                         putc ('.');
517 #else
518                         info->protect[i] = p;
519 #endif  /* CFG_FLASH_PROTECTION */
520                 }
521
522 #if defined(CFG_FLASH_PROTECTION)
523                 if (!rcode) puts (" done\n");
524 #endif  /* CFG_FLASH_PROTECTION */
525
526                 return rcode;
527         }
528
529         if (addr_spec(argv[2], argv[3], &addr_first, &addr_last) < 0){
530                 printf("Bad address format\n");
531                 return 1;
532         }
533
534         if (addr_first >= addr_last) {
535                 printf ("Usage:\n%s\n", cmdtp->usage);
536                 return 1;
537         }
538         rcode = flash_sect_protect (p, addr_first, addr_last);
539         return rcode;
540 }
541
542
543 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
544 {
545         flash_info_t *info;
546         ulong bank;
547         int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
548         int protected, i;
549         int planned;
550         int rcode;
551
552         rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
553
554         protected = 0;
555
556         if (planned && (rcode == 0)) {
557                 for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
558                         if (info->flash_id == FLASH_UNKNOWN) {
559                                 continue;
560                         }
561
562                         if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
563                                 debug ("%sProtecting sectors %d..%d in bank %ld\n",
564                                         p ? "" : "Un-",
565                                         s_first[bank], s_last[bank], bank+1);
566                                 protected += s_last[bank] - s_first[bank] + 1;
567                                 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
568 #if defined(CFG_FLASH_PROTECTION)
569                                         if (flash_real_protect(info, i, p))
570                                                 rcode = 1;
571                                         putc ('.');
572 #else
573                                         info->protect[i] = p;
574 #endif  /* CFG_FLASH_PROTECTION */
575                                 }
576                         }
577 #if defined(CFG_FLASH_PROTECTION)
578                         if (!rcode) putc ('\n');
579 #endif  /* CFG_FLASH_PROTECTION */
580                 }
581
582                 printf ("%sProtected %d sectors\n",
583                         p ? "" : "Un-", protected);
584         } else if (rcode == 0) {
585                 puts ("Error: start and/or end address"
586                         " not on sector boundary\n");
587                 rcode = 1;
588         }
589         return rcode;
590 }
591
592
593 /**************************************************/
594
595 U_BOOT_CMD(
596         flinfo,    2,    1,    do_flinfo,
597         "flinfo  - print FLASH memory information\n",
598         "\n    - print information for all FLASH memory banks\n"
599         "flinfo N\n    - print information for FLASH memory bank # N\n"
600 );
601
602 U_BOOT_CMD(
603         erase,   3,   1,  do_flerase,
604         "erase   - erase FLASH memory\n",
605         "start end\n"
606         "    - erase FLASH from addr 'start' to addr 'end'\n"
607         "erase start +len\n"
608         "    - erase FLASH from addr 'start' to the end of sect "
609         "w/addr 'start'+'len'-1\n"
610         "erase N:SF[-SL]\n    - erase sectors SF-SL in FLASH bank # N\n"
611         "erase bank N\n    - erase FLASH bank # N\n"
612         "erase all\n    - erase all FLASH banks\n"
613 );
614
615 U_BOOT_CMD(
616         protect,  4,  1,   do_protect,
617         "protect - enable or disable FLASH write protection\n",
618         "on  start end\n"
619         "    - protect FLASH from addr 'start' to addr 'end'\n"
620         "protect on start +len\n"
621         "    - protect FLASH from addr 'start' to end of sect "
622         "w/addr 'start'+'len'-1\n"
623         "protect on  N:SF[-SL]\n"
624         "    - protect sectors SF-SL in FLASH bank # N\n"
625         "protect on  bank N\n    - protect FLASH bank # N\n"
626         "protect on  all\n    - protect all FLASH banks\n"
627         "protect off start end\n"
628         "    - make FLASH from addr 'start' to addr 'end' writable\n"
629         "protect off start +len\n"
630         "    - make FLASH from addr 'start' to end of sect "
631         "w/addr 'start'+'len'-1 wrtable\n"
632         "protect off N:SF[-SL]\n"
633         "    - make sectors SF-SL writable in FLASH bank # N\n"
634         "protect off bank N\n    - make FLASH bank # N writable\n"
635         "protect off all\n    - make all FLASH banks writable\n"
636 );
637
638 #endif  /* CFG_CMD_FLASH */