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 #include <flash.h>
30 #include "flash_local.h"
31
32 //#if defined(CONFIG_CMD_FLASH)
33
34 extern flash_info_t flash_info[]; /* info for FLASH chips */
35
36 /*
37 * The user interface starts numbering for Flash banks with 1
38 * for historical reasons.
39 */
40
41 /*
42 * this routine looks for an abbreviated flash range specification.
43 * the syntax is B:SF[-SL], where B is the bank number, SF is the first
44 * sector to erase, and SL is the last sector to erase (defaults to SF).
45 * bank numbers start at 1 to be consistent with other specs, sector numbers
46 * start at zero.
47 *
48 * returns: 1 - correct spec; *pinfo, *psf and *psl are
49 * set appropriately
50 * 0 - doesn't look like an abbreviated spec
51 * -1 - looks like an abbreviated spec, but got
52 * a parsing error, a number out of range,
53 * or an invalid flash bank.
54 */
55 static int
abbrev_spec(char * str,flash_info_t ** pinfo,int * psf,int * psl)56 abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
57 {
58 flash_info_t *fp;
59 int bank, first, last;
60 char *p, *ep;
61
62 if ((p = strchr(str, ':')) == NULL)
63 return 0;
64 *p++ = '\0';
65
66 bank = simple_strtoul(str, &ep, 10);
67 if (ep == str || *ep != '\0' ||
68 bank < 1 || bank > CONFIG_SYS_MAX_FLASH_BANKS ||
69 (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
70 return -1;
71
72 str = p;
73 if ((p = strchr(str, '-')) != NULL)
74 *p++ = '\0';
75
76 first = simple_strtoul(str, &ep, 10);
77 if (ep == str || *ep != '\0' || first >= fp->sector_count)
78 return -1;
79
80 if (p != NULL) {
81 last = simple_strtoul(p, &ep, 10);
82 if (ep == p || *ep != '\0' ||
83 last < first || last >= fp->sector_count)
84 return -1;
85 }
86 else
87 last = first;
88
89 *pinfo = fp;
90 *psf = first;
91 *psl = last;
92
93 return 1;
94 }
do_flinfo(cmd_tbl_t * cmdtp,bd_t * bd,int flag,int argc,char * argv[])95 int do_flinfo (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
96 {
97 ulong bank;
98
99 if (argc == 1) { /* print info for all FLASH banks */
100 for (bank=0; bank <CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
101 printf ("\nBank # %ld: ", bank+1);
102
103 flash_print_info (&flash_info[bank]);
104 }
105 return 0;
106 }
107
108 bank = simple_strtoul(argv[1], NULL, 16);
109 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
110 printf ("Only FLASH Banks # 1 ... # %d supported\n",
111 CONFIG_SYS_MAX_FLASH_BANKS);
112 return 1;
113 }
114 printf ("\nBank # %ld: ", bank);
115 flash_print_info (&flash_info[bank-1]);
116 return 0;
117 }
do_flerase(cmd_tbl_t * cmdtp,bd_t * bd,int flag,int argc,char * argv[])118 int do_flerase(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
119 {
120 flash_info_t *info;
121 ulong bank, addr_first, addr_last;
122 int n, sect_first, sect_last;
123 int rcode = 0;
124
125 if (argc < 2) {
126 printf ("Usage:\n%s\n", cmdtp->usage);
127 return 1;
128 }
129
130 if (strcmp(argv[1], "all") == 0) {
131 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
132 printf ("Erase Flash Bank # %ld ", bank);
133 info = &flash_info[bank-1];
134 rcode = flash_erase (info, 0, info->sector_count-1);
135 }
136 return rcode;
137 }
138
139 if ((n = abbrev_spec(argv[1], &info, §_first, §_last)) != 0) {
140 if (n < 0) {
141 printf("Bad sector specification\n");
142 return 1;
143 }
144 printf ("Erase Flash Sectors %d-%d in Bank # %d ",
145 sect_first, sect_last, (info-flash_info)+1);
146 rcode = flash_erase(info, sect_first, sect_last);
147 return rcode;
148 }
149
150 if (argc != 3) {
151 printf ("Usage:\n%s\n", cmdtp->usage);
152 return 1;
153 }
154
155 if (strcmp(argv[1], "bank") == 0) {
156 bank = simple_strtoul(argv[2], NULL, 16);
157 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
158 printf ("Only FLASH Banks # 1 ... # %d supported\n",
159 CONFIG_SYS_MAX_FLASH_BANKS);
160 return 1;
161 }
162 printf ("Erase Flash Bank # %ld ", bank);
163 info = &flash_info[bank-1];
164 rcode = flash_erase (info, 0, info->sector_count-1);
165 return rcode;
166 }
167
168 addr_first = simple_strtoul(argv[1], NULL, 16);
169 addr_last = simple_strtoul(argv[2], NULL, 16);
170
171 if (addr_first >= addr_last) {
172 printf ("Usage:\n%s\n", cmdtp->usage);
173 return 1;
174 }
175
176 printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last);
177 rcode = flash_sect_erase(addr_first, addr_last);
178 return rcode;
179 }
180
flash_sect_erase(ulong addr_first,ulong addr_last)181 int flash_sect_erase (ulong addr_first, ulong addr_last)
182 {
183 flash_info_t *info;
184 ulong bank;
185 int s_first, s_last;
186 int erased;
187 int rcode = 0;
188
189 erased = 0;
190
191 for (bank=0,info=&flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
192 ulong b_end;
193 int sect;
194
195 if (info->flash_id == FLASH_UNKNOWN) {
196 continue;
197 }
198
199 b_end = info->start[0] + info->size - 1; /* bank end addr */
200
201 s_first = -1; /* first sector to erase */
202 s_last = -1; /* last sector to erase */
203
204 for (sect=0; sect < info->sector_count; ++sect) {
205 ulong end; /* last address in current sect */
206 short s_end;
207
208 s_end = info->sector_count - 1;
209
210 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
211
212 if (addr_first > end)
213 continue;
214 if (addr_last < info->start[sect])
215 continue;
216
217 if (addr_first == info->start[sect]) {
218 s_first = sect;
219 }
220 if (addr_last == end) {
221 s_last = sect;
222 }
223 }
224
225 if (s_first>=0 && s_first<=s_last) {
226 erased += s_last - s_first + 1;
227 rcode = flash_erase (info, s_first, s_last);
228 }
229 }
230 if (erased) {
231 /* printf ("Erased %d sectors\n", erased); */
232 } else {
233 printf ("Error: start and/or end address"
234 " not on sector boundary\n");
235 rcode = 1;
236 }
237 return rcode;
238 }
239
240
do_protect(cmd_tbl_t * cmdtp,bd_t * bd,int flag,int argc,char * argv[])241 int do_protect(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
242 {
243 flash_info_t *info;
244 ulong bank, addr_first, addr_last;
245 int i, p, n, sect_first, sect_last;
246 int rcode = 0;
247
248 if (argc < 3) {
249 printf ("Usage:\n%s\n", cmdtp->usage);
250 return 1;
251 }
252
253 if (strcmp(argv[1], "off") == 0)
254 p = 0;
255 else if (strcmp(argv[1], "on") == 0)
256 p = 1;
257 else {
258 printf ("Usage:\n%s\n", cmdtp->usage);
259 return 1;
260 }
261
262 if (strcmp(argv[2], "all") == 0) {
263 for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
264 info = &flash_info[bank-1];
265 if (info->flash_id == FLASH_UNKNOWN) {
266 continue;
267 }
268 /*printf ("%sProtect Flash Bank # %ld\n", */
269 /* p ? "" : "Un-", bank); */
270
271 for (i=0; i<info->sector_count; ++i) {
272 #if defined(CONFIG_SYS_FLASH_PROTECTION)
273 if (flash_real_protect(info, i, p))
274 rcode = 1;
275 putc ('.');
276 #else
277 info->protect[i] = p;
278 #endif /* CFG_FLASH_PROTECTION */
279 }
280 }
281
282 #if defined(CONFIG_SYS_FLASH_PROTECTION)
283 if (!rcode) puts (" done\n");
284 #endif /* CFG_FLASH_PROTECTION */
285
286 return rcode;
287 }
288
289 if ((n = abbrev_spec(argv[2], &info, §_first, §_last)) != 0) {
290 if (n < 0) {
291 printf("Bad sector specification\n");
292 return 1;
293 }
294 /*printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", */
295 /* p ? "" : "Un-", sect_first, sect_last, */
296 /* (info-flash_info)+1); */
297 for (i = sect_first; i <= sect_last; i++) {
298 #if defined(CONFIG_sys_FLASH_PROTECTION)
299 if (flash_real_protect(info, i, p))
300 rcode = 1;
301 putc ('.');
302 #else
303 info->protect[i] = p;
304 #endif /* CFG_FLASH_PROTECTION */
305 }
306
307 #if defined(CONFIG_SYS_FLASH_PROTECTION)
308 if (!rcode) puts (" done\n");
309 #endif /* CFG_FLASH_PROTECTION */
310
311 return rcode;
312 }
313
314 if (argc != 4) {
315 printf ("Usage:\n%s\n", cmdtp->usage);
316 return 1;
317 }
318
319 if (strcmp(argv[2], "bank") == 0) {
320 bank = simple_strtoul(argv[3], NULL, 16);
321 if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
322 printf ("Only FLASH Banks # 1 ... # %d supported\n",
323 CONFIG_SYS_MAX_FLASH_BANKS);
324 return 1;
325 }
326 printf ("%sProtect Flash Bank # %ld\n",
327 p ? "" : "Un-", bank);
328 info = &flash_info[bank-1];
329
330 if (info->flash_id == FLASH_UNKNOWN) {
331 printf ("missing or unknown FLASH type\n");
332 return 1;
333 }
334 for (i=0; i<info->sector_count; ++i) {
335 #if defined(CONFIG_SYS_FLASH_PROTECTION)
336 if (flash_real_protect(info, i, p))
337 rcode = 1;
338 putc ('.');
339 #else
340 info->protect[i] = p;
341 #endif /* CFG_FLASH_PROTECTION */
342 }
343
344 #if defined(CONFIG_SYS_FLASH_PROTECTION)
345 if (!rcode) puts (" done\n");
346 #endif /* CFG_FLASH_PROTECTION */
347
348 return rcode;
349 }
350
351 addr_first = simple_strtoul(argv[2], NULL, 16);
352 addr_last = simple_strtoul(argv[3], NULL, 16);
353
354 if (addr_first >= addr_last) {
355 printf ("Usage:\n%s\n", cmdtp->usage);
356 return 1;
357 }
358 rcode = flash_sect_protect (p, addr_first, addr_last);
359 return rcode;
360 }
flash_sect_protect(int p,ulong addr_first,ulong addr_last)361 int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
362 {
363 flash_info_t *info;
364 ulong bank;
365 int s_first, s_last;
366 int protected, i;
367 int rcode = 0;
368
369 protected = 0;
370
371 for (bank=0,info=&flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
372 ulong b_end;
373 int sect;
374
375 if (info->flash_id == FLASH_UNKNOWN) {
376 continue;
377 }
378
379 b_end = info->start[0] + info->size - 1; /* bank end addr */
380
381 s_first = -1; /* first sector to erase */
382 s_last = -1; /* last sector to erase */
383
384 for (sect=0; sect < info->sector_count; ++sect) {
385 ulong end; /* last address in current sect */
386 short s_end;
387
388 s_end = info->sector_count - 1;
389
390 end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
391
392 if (addr_first > end)
393 continue;
394 if (addr_last < info->start[sect])
395 continue;
396
397 if (addr_first == info->start[sect]) {
398 s_first = sect;
399 }
400 if (addr_last == end) {
401 s_last = sect;
402 }
403 }
404
405 if (s_first>=0 && s_first<=s_last) {
406 protected += s_last - s_first + 1;
407 for (i=s_first; i<=s_last; ++i) {
408 #if defined(CONFIG_SYS_FLASH_PROTECTION)
409 if (flash_real_protect(info, i, p))
410 rcode = 1;
411 putc ('.');
412 #else
413 info->protect[i] = p;
414 #endif /* CFG_FLASH_PROTECTION */
415 }
416 }
417 #if defined(CONFIG_SYS_FLASH_PROTECTION)
418 if (!rcode) putc ('\n');
419 #endif /* CFG_FLASH_PROTECTION */
420
421 }
422 if (protected) {
423 /* printf ("%sProtected %d sectors\n", */
424 /* p ? "" : "Un-", protected); */
425 } else {
426 printf ("Error: start and/or end address"
427 " not on sector boundary\n");
428 rcode = 1;
429 }
430 return rcode;
431 }
432
433 //#endif
434