Disable debug print for normal use.
[platform/kernel/u-boot.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 static int
100 flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
101                         int *s_first, int *s_last,
102                         int *s_count )
103 {
104         flash_info_t *info;
105         ulong bank;
106         int rcode = 0;
107
108         *s_count = 0;
109
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        */
113         }
114
115         for (bank=0,info=&flash_info[0];
116              (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
117              ++bank, ++info) {
118                 ulong b_end;
119                 int sect;
120                 short s_end;
121
122                 if (info->flash_id == FLASH_UNKNOWN) {
123                         continue;
124                 }
125
126                 b_end = info->start[0] + info->size - 1;        /* bank end addr */
127                 s_end = info->sector_count - 1;                 /* last sector   */
128
129
130                 for (sect=0; sect < info->sector_count; ++sect) {
131                         ulong end;      /* last address in current sect */
132
133                         end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
134
135                         if (addr_first > end)
136                                 continue;
137                         if (addr_last < info->start[sect])
138                                 continue;
139
140                         if (addr_first == info->start[sect]) {
141                                 s_first[bank] = sect;
142                         }
143                         if (addr_last  == end) {
144                                 s_last[bank]  = sect;
145                         }
146                 }
147                 if (s_first[bank] >= 0) {
148                         if (s_last[bank] < 0) {
149                                 if (addr_last > b_end) {
150                                         s_last[bank] = s_end;
151                                 } else {
152                                         printf ("Error: end address"
153                                                 " not on sector boundary\n");
154                                         rcode = 1;
155                                         break;
156                                 }
157                         }
158                         if (s_last[bank] < s_first[bank]) {
159                                 printf ("Error: end sector"
160                                         " precedes start sector\n");
161                                 rcode = 1;
162                                 break;
163                         }
164                         sect = s_last[bank];
165                         addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
166                         (*s_count) += s_last[bank] - s_first[bank] + 1;
167                 }
168         }
169
170         return rcode;
171 }
172
173 int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
174 {
175         ulong bank;
176
177 #ifdef CONFIG_HAS_DATAFLASH
178         dataflash_print_info();
179 #endif
180
181         if (argc == 1) {        /* print info for all FLASH banks */
182                 for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
183                         printf ("\nBank # %ld: ", bank+1);
184
185                         flash_print_info (&flash_info[bank]);
186                 }
187                 return 0;
188         }
189
190         bank = simple_strtoul(argv[1], NULL, 16);
191         if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
192                 printf ("Only FLASH Banks # 1 ... # %d supported\n",
193                         CFG_MAX_FLASH_BANKS);
194                 return 1;
195         }
196         printf ("\nBank # %ld: ", bank);
197         flash_print_info (&flash_info[bank-1]);
198         return 0;
199 }
200 int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
201 {
202         flash_info_t *info;
203         ulong bank, addr_first, addr_last;
204         int n, sect_first, sect_last;
205         int rcode = 0;
206
207         if (argc < 2) {
208                 printf ("Usage:\n%s\n", cmdtp->usage);
209                 return 1;
210         }
211
212         if (strcmp(argv[1], "all") == 0) {
213                 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
214                         printf ("Erase Flash Bank # %ld ", bank);
215                         info = &flash_info[bank-1];
216                         rcode = flash_erase (info, 0, info->sector_count-1);
217                 }
218                 return rcode;
219         }
220
221         if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
222                 if (n < 0) {
223                         printf("Bad sector specification\n");
224                         return 1;
225                 }
226                 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
227                         sect_first, sect_last, (info-flash_info)+1);
228                 rcode = flash_erase(info, sect_first, sect_last);
229                 return rcode;
230         }
231
232         if (argc != 3) {
233                 printf ("Usage:\n%s\n", cmdtp->usage);
234                 return 1;
235         }
236
237         if (strcmp(argv[1], "bank") == 0) {
238                 bank = simple_strtoul(argv[2], NULL, 16);
239                 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
240                         printf ("Only FLASH Banks # 1 ... # %d supported\n",
241                                 CFG_MAX_FLASH_BANKS);
242                         return 1;
243                 }
244                 printf ("Erase Flash Bank # %ld ", bank);
245                 info = &flash_info[bank-1];
246                 rcode = flash_erase (info, 0, info->sector_count-1);
247                 return rcode;
248         }
249
250         addr_first = simple_strtoul(argv[1], NULL, 16);
251         addr_last  = simple_strtoul(argv[2], NULL, 16);
252
253         if (addr_first >= addr_last) {
254                 printf ("Usage:\n%s\n", cmdtp->usage);
255                 return 1;
256         }
257
258         rcode = flash_sect_erase(addr_first, addr_last);
259         return rcode;
260 }
261
262 int flash_sect_erase (ulong addr_first, ulong addr_last)
263 {
264         flash_info_t *info;
265         ulong bank;
266         int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
267         int erased = 0;
268         int planned;
269         int rcode = 0;
270
271         rcode = flash_fill_sect_ranges (addr_first, addr_last,
272                                         s_first, s_last, &planned );
273
274         if (planned && (rcode == 0)) {
275                 for (bank=0,info=&flash_info[0];
276                      (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
277                      ++bank, ++info) {
278                         if (s_first[bank]>=0) {
279                                 erased += s_last[bank] - s_first[bank] + 1;
280                                 debug ("Erase Flash from 0x%08lx to 0x%08lx "
281                                         "in Bank # %ld ",
282                                         info->start[s_first[bank]],
283                                         (s_last[bank] == info->sector_count) ?
284                                                 info->start[0] + info->size - 1:
285                                                 info->start[s_last[bank]+1] - 1,
286                                         bank+1);
287                                 rcode = flash_erase (info, s_first[bank], s_last[bank]);
288                         }
289                 }
290                 printf ("Erased %d sectors\n", erased);
291         } else if (rcode == 0) {
292                 printf ("Error: start and/or end address"
293                         " not on sector boundary\n");
294                 rcode = 1;
295         }
296         return rcode;
297 }
298
299 int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
300 {
301         flash_info_t *info;
302         ulong bank, addr_first, addr_last;
303         int i, p, n, sect_first, sect_last;
304         int rcode = 0;
305
306         if (argc < 3) {
307                 printf ("Usage:\n%s\n", cmdtp->usage);
308                 return 1;
309         }
310
311         if (strcmp(argv[1], "off") == 0) {
312                 p = 0;
313         } else if (strcmp(argv[1], "on") == 0) {
314                 p = 1;
315         } else {
316                 printf ("Usage:\n%s\n", cmdtp->usage);
317                 return 1;
318         }
319
320         if (strcmp(argv[2], "all") == 0) {
321                 for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
322                         info = &flash_info[bank-1];
323                         if (info->flash_id == FLASH_UNKNOWN) {
324                                 continue;
325                         }
326                         printf ("%sProtect Flash Bank # %ld\n",
327                                 p ? "" : "Un-", bank);
328
329                         for (i=0; i<info->sector_count; ++i) {
330 #if defined(CFG_FLASH_PROTECTION)
331                                 if (flash_real_protect(info, i, p))
332                                         rcode = 1;
333                                 putc ('.');
334 #else
335                                 info->protect[i] = p;
336 #endif  /* CFG_FLASH_PROTECTION */
337                         }
338                 }
339
340 #if defined(CFG_FLASH_PROTECTION)
341                 if (!rcode) puts (" done\n");
342 #endif  /* CFG_FLASH_PROTECTION */
343
344                 return rcode;
345         }
346
347         if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
348                 if (n < 0) {
349                         printf("Bad sector specification\n");
350                         return 1;
351                 }
352                 printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
353                         p ? "" : "Un-", sect_first, sect_last,
354                         (info-flash_info)+1);
355                 for (i = sect_first; i <= sect_last; i++) {
356 #if defined(CFG_FLASH_PROTECTION)
357                         if (flash_real_protect(info, i, p))
358                                 rcode =  1;
359                         putc ('.');
360 #else
361                         info->protect[i] = p;
362 #endif  /* CFG_FLASH_PROTECTION */
363                 }
364
365 #if defined(CFG_FLASH_PROTECTION)
366                 if (!rcode) puts (" done\n");
367 #endif  /* CFG_FLASH_PROTECTION */
368
369                 return rcode;
370         }
371
372         if (argc != 4) {
373                 printf ("Usage:\n%s\n", cmdtp->usage);
374                 return 1;
375         }
376
377         if (strcmp(argv[2], "bank") == 0) {
378                 bank = simple_strtoul(argv[3], NULL, 16);
379                 if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
380                         printf ("Only FLASH Banks # 1 ... # %d supported\n",
381                                 CFG_MAX_FLASH_BANKS);
382                         return 1;
383                 }
384                 printf ("%sProtect Flash Bank # %ld\n",
385                         p ? "" : "Un-", bank);
386                 info = &flash_info[bank-1];
387
388                 if (info->flash_id == FLASH_UNKNOWN) {
389                         printf ("missing or unknown FLASH type\n");
390                         return 1;
391                 }
392                 for (i=0; i<info->sector_count; ++i) {
393 #if defined(CFG_FLASH_PROTECTION)
394                         if (flash_real_protect(info, i, p))
395                                 rcode =  1;
396                         putc ('.');
397 #else
398                         info->protect[i] = p;
399 #endif  /* CFG_FLASH_PROTECTION */
400                 }
401
402 #if defined(CFG_FLASH_PROTECTION)
403                 if (!rcode) puts (" done\n");
404 #endif  /* CFG_FLASH_PROTECTION */
405
406                 return rcode;
407         }
408
409         addr_first = simple_strtoul(argv[2], NULL, 16);
410         addr_last  = simple_strtoul(argv[3], NULL, 16);
411
412         if (addr_first >= addr_last) {
413                 printf ("Usage:\n%s\n", cmdtp->usage);
414                 return 1;
415         }
416         rcode = flash_sect_protect (p, addr_first, addr_last);
417         return rcode;
418 }
419
420
421 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
422 {
423         flash_info_t *info;
424         ulong bank;
425         int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
426         int protected, i;
427         int planned;
428         int rcode;
429
430         rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
431
432         protected = 0;
433
434         if (planned && (rcode == 0)) {
435                 for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
436                         if (info->flash_id == FLASH_UNKNOWN) {
437                                 continue;
438                         }
439
440                         if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
441                                 debug ("Protecting sectors %d..%d in bank %ld\n",
442                                         s_first[bank], s_last[bank], bank+1);
443                                 protected += s_last[bank] - s_first[bank] + 1;
444                                 for (i=s_first[bank]; i<=s_last[bank]; ++i) {
445 #if defined(CFG_FLASH_PROTECTION)
446                                         if (flash_real_protect(info, i, p))
447                                                 rcode = 1;
448                                         putc ('.');
449 #else
450                                         info->protect[i] = p;
451 #endif  /* CFG_FLASH_PROTECTION */
452                                 }
453                         }
454 #if defined(CFG_FLASH_PROTECTION)
455                         if (!rcode) putc ('\n');
456 #endif  /* CFG_FLASH_PROTECTION */
457                 }
458
459                 printf ("%sProtected %d sectors\n",
460                         p ? "" : "Un-", protected);
461         } else if (rcode == 0) {
462                 printf ("Error: start and/or end address"
463                         " not on sector boundary\n");
464                 rcode = 1;
465         }
466         return rcode;
467 }
468
469
470 /**************************************************/
471
472 U_BOOT_CMD(
473         flinfo,    2,    1,    do_flinfo,
474         "flinfo  - print FLASH memory information\n",
475         "\n    - print information for all FLASH memory banks\n"
476         "flinfo N\n    - print information for FLASH memory bank # N\n"
477 );
478
479 U_BOOT_CMD(
480         erase,   3,   1,  do_flerase,
481         "erase   - erase FLASH memory\n",
482         "start end\n"
483         "    - erase FLASH from addr 'start' to addr 'end'\n"
484         "erase N:SF[-SL]\n    - erase sectors SF-SL in FLASH bank # N\n"
485         "erase bank N\n    - erase FLASH bank # N\n"
486         "erase all\n    - erase all FLASH banks\n"
487 );
488
489 U_BOOT_CMD(
490         protect,  4,  1,   do_protect,
491         "protect - enable or disable FLASH write protection\n",
492         "on  start end\n"
493         "    - protect FLASH from addr 'start' to addr 'end'\n"
494         "protect on  N:SF[-SL]\n"
495         "    - protect sectors SF-SL in FLASH bank # N\n"
496         "protect on  bank N\n    - protect FLASH bank # N\n"
497         "protect on  all\n    - protect all FLASH banks\n"
498         "protect off start end\n"
499         "    - make FLASH from addr 'start' to addr 'end' writable\n"
500         "protect off N:SF[-SL]\n"
501         "    - make sectors SF-SL writable in FLASH bank # N\n"
502         "protect off bank N\n    - make FLASH bank # N writable\n"
503         "protect off all\n    - make all FLASH banks writable\n"
504 );
505
506 #endif  /* CFG_CMD_FLASH */