blob: b2728ab1d3105f3c7cea465cff6ad3e5debc104a [file] [log] [blame]
wdenk38a24a62002-09-18 12:49:44 +00001/*
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>
wdenk57b2d802003-06-27 21:31:46 +000029
wdenk38a24a62002-09-18 12:49:44 +000030
wdenk381669a2003-06-16 23:50:08 +000031#ifdef CONFIG_HAS_DATAFLASH
32#include <dataflash.h>
33#endif
34
wdenk38a24a62002-09-18 12:49:44 +000035#if (CONFIG_COMMANDS & CFG_CMD_FLASH)
36
37extern 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 */
58static int
wdenk0a658552003-08-05 17:43:17 +000059abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
wdenk38a24a62002-09-18 12:49:44 +000060{
wdenk0a658552003-08-05 17:43:17 +000061 flash_info_t *fp;
62 int bank, first, last;
63 char *p, *ep;
wdenk38a24a62002-09-18 12:49:44 +000064
wdenk0a658552003-08-05 17:43:17 +000065 if ((p = strchr (str, ':')) == NULL)
66 return 0;
67 *p++ = '\0';
wdenk38a24a62002-09-18 12:49:44 +000068
wdenk0a658552003-08-05 17:43:17 +000069 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;
wdenk38a24a62002-09-18 12:49:44 +000074
wdenk0a658552003-08-05 17:43:17 +000075 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
99static int
100flash_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 */
wdenk38a24a62002-09-18 12:49:44 +0000128
wdenk38a24a62002-09-18 12:49:44 +0000129
wdenk0a658552003-08-05 17:43:17 +0000130 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;
wdenk38a24a62002-09-18 12:49:44 +0000134
wdenk0a658552003-08-05 17:43:17 +0000135 if (addr_first > end)
136 continue;
137 if (addr_last < info->start[sect])
138 continue;
wdenk38a24a62002-09-18 12:49:44 +0000139
wdenk0a658552003-08-05 17:43:17 +0000140 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 {
wdenk42c05472004-03-23 22:14:11 +0000152 puts ("Error: end address"
wdenk0a658552003-08-05 17:43:17 +0000153 " not on sector boundary\n");
154 rcode = 1;
155 break;
156 }
157 }
158 if (s_last[bank] < s_first[bank]) {
wdenk42c05472004-03-23 22:14:11 +0000159 puts ("Error: end sector"
wdenk0a658552003-08-05 17:43:17 +0000160 " 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;
wdenk54070ab2004-12-31 09:32:47 +0000167 } else if (addr_first >= info->start[0] && addr_first < b_end) {
168 puts ("Error: start address not on sector boundary\n");
169 rcode = 1;
170 break;
wdenkbd1575f2003-10-14 19:43:55 +0000171 } else if (s_last[bank] >= 0) {
wdenk42c05472004-03-23 22:14:11 +0000172 puts ("Error: cannot span across banks when they are"
wdenkbd1575f2003-10-14 19:43:55 +0000173 " mapped in reverse order\n");
174 rcode = 1;
175 break;
wdenk0a658552003-08-05 17:43:17 +0000176 }
177 }
178
179 return rcode;
wdenk38a24a62002-09-18 12:49:44 +0000180}
wdenk0a658552003-08-05 17:43:17 +0000181
wdenk38a24a62002-09-18 12:49:44 +0000182int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
183{
184 ulong bank;
185
wdenk381669a2003-06-16 23:50:08 +0000186#ifdef CONFIG_HAS_DATAFLASH
187 dataflash_print_info();
188#endif
189
wdenk38a24a62002-09-18 12:49:44 +0000190 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);
193
194 flash_print_info (&flash_info[bank]);
195 }
196 return 0;
197 }
198
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);
203 return 1;
204 }
205 printf ("\nBank # %ld: ", bank);
206 flash_print_info (&flash_info[bank-1]);
207 return 0;
208}
209int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
210{
211 flash_info_t *info;
212 ulong bank, addr_first, addr_last;
213 int n, sect_first, sect_last;
214 int rcode = 0;
215
216 if (argc < 2) {
217 printf ("Usage:\n%s\n", cmdtp->usage);
218 return 1;
219 }
220
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);
226 }
227 return rcode;
228 }
229
230 if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
231 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000232 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000233 return 1;
234 }
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);
238 return rcode;
239 }
240
241 if (argc != 3) {
242 printf ("Usage:\n%s\n", cmdtp->usage);
243 return 1;
244 }
245
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);
251 return 1;
252 }
253 printf ("Erase Flash Bank # %ld ", bank);
254 info = &flash_info[bank-1];
255 rcode = flash_erase (info, 0, info->sector_count-1);
256 return rcode;
257 }
258
259 addr_first = simple_strtoul(argv[1], NULL, 16);
260 addr_last = simple_strtoul(argv[2], NULL, 16);
261
262 if (addr_first >= addr_last) {
263 printf ("Usage:\n%s\n", cmdtp->usage);
264 return 1;
265 }
266
wdenk38a24a62002-09-18 12:49:44 +0000267 rcode = flash_sect_erase(addr_first, addr_last);
268 return rcode;
269}
270
271int flash_sect_erase (ulong addr_first, ulong addr_last)
272{
273 flash_info_t *info;
274 ulong bank;
wdenk0a658552003-08-05 17:43:17 +0000275 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
276 int erased = 0;
277 int planned;
wdenk38a24a62002-09-18 12:49:44 +0000278 int rcode = 0;
279
wdenk0a658552003-08-05 17:43:17 +0000280 rcode = flash_fill_sect_ranges (addr_first, addr_last,
281 s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000282
wdenk0a658552003-08-05 17:43:17 +0000283 if (planned && (rcode == 0)) {
284 for (bank=0,info=&flash_info[0];
285 (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
286 ++bank, ++info) {
287 if (s_first[bank]>=0) {
288 erased += s_last[bank] - s_first[bank] + 1;
wdenk3f08e202003-08-07 14:52:18 +0000289 debug ("Erase Flash from 0x%08lx to 0x%08lx "
wdenk0a658552003-08-05 17:43:17 +0000290 "in Bank # %ld ",
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,
295 bank+1);
296 rcode = flash_erase (info, s_first[bank], s_last[bank]);
wdenk38a24a62002-09-18 12:49:44 +0000297 }
wdenk38a24a62002-09-18 12:49:44 +0000298 }
wdenk38a24a62002-09-18 12:49:44 +0000299 printf ("Erased %d sectors\n", erased);
wdenk0a658552003-08-05 17:43:17 +0000300 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000301 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000302 " not on sector boundary\n");
303 rcode = 1;
304 }
305 return rcode;
306}
307
wdenk38a24a62002-09-18 12:49:44 +0000308int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
309{
310 flash_info_t *info;
311 ulong bank, addr_first, addr_last;
312 int i, p, n, sect_first, sect_last;
313 int rcode = 0;
wdenk86765902003-12-06 23:55:10 +0000314#ifdef CONFIG_HAS_DATAFLASH
315 int status;
316#endif
wdenk38a24a62002-09-18 12:49:44 +0000317 if (argc < 3) {
318 printf ("Usage:\n%s\n", cmdtp->usage);
319 return 1;
320 }
321
wdenk0a658552003-08-05 17:43:17 +0000322 if (strcmp(argv[1], "off") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000323 p = 0;
wdenk0a658552003-08-05 17:43:17 +0000324 } else if (strcmp(argv[1], "on") == 0) {
wdenk38a24a62002-09-18 12:49:44 +0000325 p = 1;
wdenk0a658552003-08-05 17:43:17 +0000326 } else {
wdenk38a24a62002-09-18 12:49:44 +0000327 printf ("Usage:\n%s\n", cmdtp->usage);
328 return 1;
329 }
330
wdenk86765902003-12-06 23:55:10 +0000331#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);
335
336 if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
337 status = dataflash_real_protect(p,addr_first,addr_last);
338 if (status < 0){
wdenk42c05472004-03-23 22:14:11 +0000339 puts ("Bad DataFlash sector specification\n");
wdenk1ebf41e2004-01-02 14:00:00 +0000340 return 1;
341 }
342 printf("%sProtect %d DataFlash Sectors\n",
343 p ? "" : "Un-", status);
wdenk86765902003-12-06 23:55:10 +0000344 return 0;
345 }
346 }
347#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000348
wdenk38a24a62002-09-18 12:49:44 +0000349 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) {
353 continue;
354 }
355 printf ("%sProtect Flash Bank # %ld\n",
356 p ? "" : "Un-", bank);
357
358 for (i=0; i<info->sector_count; ++i) {
359#if defined(CFG_FLASH_PROTECTION)
360 if (flash_real_protect(info, i, p))
361 rcode = 1;
362 putc ('.');
363#else
364 info->protect[i] = p;
365#endif /* CFG_FLASH_PROTECTION */
366 }
367 }
368
369#if defined(CFG_FLASH_PROTECTION)
370 if (!rcode) puts (" done\n");
371#endif /* CFG_FLASH_PROTECTION */
372
373 return rcode;
374 }
375
376 if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
377 if (n < 0) {
wdenk42c05472004-03-23 22:14:11 +0000378 puts ("Bad sector specification\n");
wdenk38a24a62002-09-18 12:49:44 +0000379 return 1;
380 }
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))
387 rcode = 1;
388 putc ('.');
389#else
390 info->protect[i] = p;
391#endif /* CFG_FLASH_PROTECTION */
392 }
393
394#if defined(CFG_FLASH_PROTECTION)
395 if (!rcode) puts (" done\n");
396#endif /* CFG_FLASH_PROTECTION */
397
398 return rcode;
399 }
400
401 if (argc != 4) {
402 printf ("Usage:\n%s\n", cmdtp->usage);
403 return 1;
404 }
405
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);
411 return 1;
412 }
413 printf ("%sProtect Flash Bank # %ld\n",
414 p ? "" : "Un-", bank);
415 info = &flash_info[bank-1];
416
417 if (info->flash_id == FLASH_UNKNOWN) {
wdenk42c05472004-03-23 22:14:11 +0000418 puts ("missing or unknown FLASH type\n");
wdenk38a24a62002-09-18 12:49:44 +0000419 return 1;
420 }
421 for (i=0; i<info->sector_count; ++i) {
422#if defined(CFG_FLASH_PROTECTION)
423 if (flash_real_protect(info, i, p))
424 rcode = 1;
425 putc ('.');
426#else
427 info->protect[i] = p;
428#endif /* CFG_FLASH_PROTECTION */
429 }
430
431#if defined(CFG_FLASH_PROTECTION)
432 if (!rcode) puts (" done\n");
433#endif /* CFG_FLASH_PROTECTION */
434
435 return rcode;
436 }
437
438 addr_first = simple_strtoul(argv[2], NULL, 16);
439 addr_last = simple_strtoul(argv[3], NULL, 16);
440
441 if (addr_first >= addr_last) {
442 printf ("Usage:\n%s\n", cmdtp->usage);
443 return 1;
444 }
445 rcode = flash_sect_protect (p, addr_first, addr_last);
446 return rcode;
447}
448
449
450int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
451{
452 flash_info_t *info;
453 ulong bank;
wdenk0a658552003-08-05 17:43:17 +0000454 int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
wdenk38a24a62002-09-18 12:49:44 +0000455 int protected, i;
wdenk0a658552003-08-05 17:43:17 +0000456 int planned;
457 int rcode;
wdenk38a24a62002-09-18 12:49:44 +0000458
wdenk0a658552003-08-05 17:43:17 +0000459 rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
wdenk38a24a62002-09-18 12:49:44 +0000460
wdenk0a658552003-08-05 17:43:17 +0000461 protected = 0;
wdenk38a24a62002-09-18 12:49:44 +0000462
wdenk0a658552003-08-05 17:43:17 +0000463 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) {
wdenk38a24a62002-09-18 12:49:44 +0000466 continue;
wdenk38a24a62002-09-18 12:49:44 +0000467 }
wdenk0a658552003-08-05 17:43:17 +0000468
469 if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
stroeseeb4a4392003-12-09 14:58:22 +0000470 debug ("%sProtecting sectors %d..%d in bank %ld\n",
471 p ? "" : "Un-",
wdenk0a658552003-08-05 17:43:17 +0000472 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) {
wdenk38a24a62002-09-18 12:49:44 +0000475#if defined(CFG_FLASH_PROTECTION)
wdenk0a658552003-08-05 17:43:17 +0000476 if (flash_real_protect(info, i, p))
477 rcode = 1;
478 putc ('.');
wdenk38a24a62002-09-18 12:49:44 +0000479#else
wdenk0a658552003-08-05 17:43:17 +0000480 info->protect[i] = p;
wdenk38a24a62002-09-18 12:49:44 +0000481#endif /* CFG_FLASH_PROTECTION */
wdenk0a658552003-08-05 17:43:17 +0000482 }
wdenk38a24a62002-09-18 12:49:44 +0000483 }
wdenk38a24a62002-09-18 12:49:44 +0000484#if defined(CFG_FLASH_PROTECTION)
wdenk0a658552003-08-05 17:43:17 +0000485 if (!rcode) putc ('\n');
wdenk38a24a62002-09-18 12:49:44 +0000486#endif /* CFG_FLASH_PROTECTION */
wdenk0a658552003-08-05 17:43:17 +0000487 }
wdenk38a24a62002-09-18 12:49:44 +0000488
wdenk38a24a62002-09-18 12:49:44 +0000489 printf ("%sProtected %d sectors\n",
490 p ? "" : "Un-", protected);
wdenk0a658552003-08-05 17:43:17 +0000491 } else if (rcode == 0) {
wdenk42c05472004-03-23 22:14:11 +0000492 puts ("Error: start and/or end address"
wdenk38a24a62002-09-18 12:49:44 +0000493 " not on sector boundary\n");
494 rcode = 1;
495 }
496 return rcode;
497}
498
wdenk57b2d802003-06-27 21:31:46 +0000499
500/**************************************************/
501
wdenkf287a242003-07-01 21:06:45 +0000502U_BOOT_CMD(
503 flinfo, 2, 1, do_flinfo,
wdenk57b2d802003-06-27 21:31:46 +0000504 "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"
507);
508
wdenkf287a242003-07-01 21:06:45 +0000509U_BOOT_CMD(
510 erase, 3, 1, do_flerase,
wdenk57b2d802003-06-27 21:31:46 +0000511 "erase - erase FLASH memory\n",
512 "start end\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"
517);
518
wdenkf287a242003-07-01 21:06:45 +0000519U_BOOT_CMD(
520 protect, 4, 1, do_protect,
wdenk57b2d802003-06-27 21:31:46 +0000521 "protect - enable or disable FLASH write protection\n",
522 "on start end\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"
534);
535
wdenk38a24a62002-09-18 12:49:44 +0000536#endif /* CFG_CMD_FLASH */