Remove FSF address from GPL notices
[openocd.git] / src / flash / nand / tcl.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
4 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
5 * *
6 * Partially based on drivers/mtd/nand_ids.c from Linux. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (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, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "core.h"
27 #include "imp.h"
28 #include "fileio.h"
29 #include <target/target.h>
30
31 /* to be removed */
32 extern struct nand_device *nand_devices;
33
34 COMMAND_HANDLER(handle_nand_list_command)
35 {
36 struct nand_device *p;
37 int i;
38
39 if (!nand_devices) {
40 command_print(CMD_CTX, "no NAND flash devices configured");
41 return ERROR_OK;
42 }
43
44 for (p = nand_devices, i = 0; p; p = p->next, i++) {
45 if (p->device)
46 command_print(CMD_CTX, "#%i: %s (%s) "
47 "pagesize: %i, buswidth: %i,\n\t"
48 "blocksize: %i, blocks: %i",
49 i, p->device->name, p->manufacturer->name,
50 p->page_size, p->bus_width,
51 p->erase_size, p->num_blocks);
52 else
53 command_print(CMD_CTX, "#%i: not probed", i);
54 }
55
56 return ERROR_OK;
57 }
58
59 COMMAND_HANDLER(handle_nand_info_command)
60 {
61 int i = 0;
62 int j = 0;
63 int first = -1;
64 int last = -1;
65
66 switch (CMD_ARGC) {
67 default:
68 return ERROR_COMMAND_SYNTAX_ERROR;
69 case 1:
70 first = 0;
71 last = INT32_MAX;
72 break;
73 case 2:
74 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], i);
75 first = last = i;
76 i = 0;
77 break;
78 case 3:
79 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
80 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
81 break;
82 }
83
84 struct nand_device *p;
85 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
86 if (ERROR_OK != retval)
87 return retval;
88
89 if (NULL == p->device) {
90 command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
91 return ERROR_OK;
92 }
93
94 if (first >= p->num_blocks)
95 first = p->num_blocks - 1;
96
97 if (last >= p->num_blocks)
98 last = p->num_blocks - 1;
99
100 command_print(CMD_CTX,
101 "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
102 i++,
103 p->device->name,
104 p->manufacturer->name,
105 p->page_size,
106 p->bus_width,
107 p->erase_size);
108
109 for (j = first; j <= last; j++) {
110 char *erase_state, *bad_state;
111
112 if (p->blocks[j].is_erased == 0)
113 erase_state = "not erased";
114 else if (p->blocks[j].is_erased == 1)
115 erase_state = "erased";
116 else
117 erase_state = "erase state unknown";
118
119 if (p->blocks[j].is_bad == 0)
120 bad_state = "";
121 else if (p->blocks[j].is_bad == 1)
122 bad_state = " (marked bad)";
123 else
124 bad_state = " (block condition unknown)";
125
126 command_print(CMD_CTX,
127 "\t#%i: 0x%8.8" PRIx32 " (%" PRId32 "kB) %s%s",
128 j,
129 p->blocks[j].offset,
130 p->blocks[j].size / 1024,
131 erase_state,
132 bad_state);
133 }
134
135 return ERROR_OK;
136 }
137
138 COMMAND_HANDLER(handle_nand_probe_command)
139 {
140 if (CMD_ARGC != 1)
141 return ERROR_COMMAND_SYNTAX_ERROR;
142
143 struct nand_device *p;
144 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
145 if (ERROR_OK != retval)
146 return retval;
147
148 retval = nand_probe(p);
149 if (retval == ERROR_OK) {
150 command_print(CMD_CTX, "NAND flash device '%s (%s)' found",
151 p->device->name, p->manufacturer->name);
152 }
153
154 return retval;
155 }
156
157 COMMAND_HANDLER(handle_nand_erase_command)
158 {
159 if (CMD_ARGC != 1 && CMD_ARGC != 3)
160 return ERROR_COMMAND_SYNTAX_ERROR;
161
162 struct nand_device *p;
163 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
164 if (ERROR_OK != retval)
165 return retval;
166
167 unsigned long offset;
168 unsigned long length;
169
170 /* erase specified part of the chip; or else everything */
171 if (CMD_ARGC == 3) {
172 unsigned long size = p->erase_size * p->num_blocks;
173
174 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
175 if ((offset % p->erase_size) != 0 || offset >= size)
176 return ERROR_COMMAND_SYNTAX_ERROR;
177
178 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
179 if ((length == 0) || (length % p->erase_size) != 0
180 || (length + offset) > size)
181 return ERROR_COMMAND_SYNTAX_ERROR;
182
183 offset /= p->erase_size;
184 length /= p->erase_size;
185 } else {
186 offset = 0;
187 length = p->num_blocks;
188 }
189
190 retval = nand_erase(p, offset, offset + length - 1);
191 if (retval == ERROR_OK) {
192 command_print(CMD_CTX, "erased blocks %lu to %lu "
193 "on NAND flash device #%s '%s'",
194 offset, offset + length - 1,
195 CMD_ARGV[0], p->device->name);
196 }
197
198 return retval;
199 }
200
201 COMMAND_HANDLER(handle_nand_check_bad_blocks_command)
202 {
203 int first = -1;
204 int last = -1;
205
206 if ((CMD_ARGC < 1) || (CMD_ARGC > 3) || (CMD_ARGC == 2))
207 return ERROR_COMMAND_SYNTAX_ERROR;
208
209 struct nand_device *p;
210 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
211 if (ERROR_OK != retval)
212 return retval;
213
214 if (CMD_ARGC == 3) {
215 unsigned long offset;
216 unsigned long length;
217
218 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], offset);
219 if (offset % p->erase_size)
220 return ERROR_COMMAND_SYNTAX_ERROR;
221 offset /= p->erase_size;
222
223 COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], length);
224 if (length % p->erase_size)
225 return ERROR_COMMAND_SYNTAX_ERROR;
226
227 length -= 1;
228 length /= p->erase_size;
229
230 first = offset;
231 last = offset + length;
232 }
233
234 retval = nand_build_bbt(p, first, last);
235 if (retval == ERROR_OK) {
236 command_print(CMD_CTX, "checked NAND flash device for bad blocks, "
237 "use \"nand info\" command to list blocks");
238 }
239
240 return retval;
241 }
242
243 COMMAND_HANDLER(handle_nand_write_command)
244 {
245 struct nand_device *nand = NULL;
246 struct nand_fileio_state s;
247 int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
248 &s, &nand, FILEIO_READ, false, true);
249 if (ERROR_OK != retval)
250 return retval;
251
252 uint32_t total_bytes = s.size;
253 while (s.size > 0) {
254 int bytes_read = nand_fileio_read(nand, &s);
255 if (bytes_read <= 0) {
256 command_print(CMD_CTX, "error while reading file");
257 return nand_fileio_cleanup(&s);
258 }
259 s.size -= bytes_read;
260
261 retval = nand_write_page(nand, s.address / nand->page_size,
262 s.page, s.page_size, s.oob, s.oob_size);
263 if (ERROR_OK != retval) {
264 command_print(CMD_CTX, "failed writing file %s "
265 "to NAND flash %s at offset 0x%8.8" PRIx32,
266 CMD_ARGV[1], CMD_ARGV[0], s.address);
267 return nand_fileio_cleanup(&s);
268 }
269 s.address += s.page_size;
270 }
271
272 if (nand_fileio_finish(&s) == ERROR_OK) {
273 command_print(CMD_CTX, "wrote file %s to NAND flash %s up to "
274 "offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
275 CMD_ARGV[1], CMD_ARGV[0], s.address, duration_elapsed(&s.bench),
276 duration_kbps(&s.bench, total_bytes));
277 }
278 return ERROR_OK;
279 }
280
281 COMMAND_HANDLER(handle_nand_verify_command)
282 {
283 struct nand_device *nand = NULL;
284 struct nand_fileio_state file;
285 int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
286 &file, &nand, FILEIO_READ, false, true);
287 if (ERROR_OK != retval)
288 return retval;
289
290 struct nand_fileio_state dev;
291 nand_fileio_init(&dev);
292 dev.address = file.address;
293 dev.size = file.size;
294 dev.oob_format = file.oob_format;
295 retval = nand_fileio_start(CMD_CTX, nand, NULL, FILEIO_NONE, &dev);
296 if (ERROR_OK != retval)
297 return retval;
298
299 while (file.size > 0) {
300 retval = nand_read_page(nand, dev.address / dev.page_size,
301 dev.page, dev.page_size, dev.oob, dev.oob_size);
302 if (ERROR_OK != retval) {
303 command_print(CMD_CTX, "reading NAND flash page failed");
304 nand_fileio_cleanup(&dev);
305 nand_fileio_cleanup(&file);
306 return retval;
307 }
308
309 int bytes_read = nand_fileio_read(nand, &file);
310 if (bytes_read <= 0) {
311 command_print(CMD_CTX, "error while reading file");
312 nand_fileio_cleanup(&dev);
313 nand_fileio_cleanup(&file);
314 return ERROR_FAIL;
315 }
316
317 if ((dev.page && memcmp(dev.page, file.page, dev.page_size)) ||
318 (dev.oob && memcmp(dev.oob, file.oob, dev.oob_size))) {
319 command_print(CMD_CTX, "NAND flash contents differ "
320 "at 0x%8.8" PRIx32, dev.address);
321 nand_fileio_cleanup(&dev);
322 nand_fileio_cleanup(&file);
323 return ERROR_FAIL;
324 }
325
326 file.size -= bytes_read;
327 dev.address += nand->page_size;
328 }
329
330 if (nand_fileio_finish(&file) == ERROR_OK) {
331 command_print(CMD_CTX, "verified file %s in NAND flash %s "
332 "up to offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
333 CMD_ARGV[1], CMD_ARGV[0], dev.address, duration_elapsed(&file.bench),
334 duration_kbps(&file.bench, dev.size));
335 }
336
337 return nand_fileio_cleanup(&dev);
338 }
339
340 COMMAND_HANDLER(handle_nand_dump_command)
341 {
342 size_t filesize;
343 struct nand_device *nand = NULL;
344 struct nand_fileio_state s;
345 int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args,
346 &s, &nand, FILEIO_WRITE, true, false);
347 if (ERROR_OK != retval)
348 return retval;
349
350 while (s.size > 0) {
351 size_t size_written;
352 retval = nand_read_page(nand, s.address / nand->page_size,
353 s.page, s.page_size, s.oob, s.oob_size);
354 if (ERROR_OK != retval) {
355 command_print(CMD_CTX, "reading NAND flash page failed");
356 nand_fileio_cleanup(&s);
357 return retval;
358 }
359
360 if (NULL != s.page)
361 fileio_write(s.fileio, s.page_size, s.page, &size_written);
362
363 if (NULL != s.oob)
364 fileio_write(s.fileio, s.oob_size, s.oob, &size_written);
365
366 s.size -= nand->page_size;
367 s.address += nand->page_size;
368 }
369
370 retval = fileio_size(s.fileio, &filesize);
371 if (retval != ERROR_OK)
372 return retval;
373
374 if (nand_fileio_finish(&s) == ERROR_OK) {
375 command_print(CMD_CTX, "dumped %zu bytes in %fs (%0.3f KiB/s)",
376 filesize, duration_elapsed(&s.bench),
377 duration_kbps(&s.bench, filesize));
378 }
379 return ERROR_OK;
380 }
381
382 COMMAND_HANDLER(handle_nand_raw_access_command)
383 {
384 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
385 return ERROR_COMMAND_SYNTAX_ERROR;
386
387 struct nand_device *p;
388 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &p);
389 if (ERROR_OK != retval)
390 return retval;
391
392 if (NULL == p->device) {
393 command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
394 return ERROR_OK;
395 }
396
397 if (CMD_ARGC == 2)
398 COMMAND_PARSE_ENABLE(CMD_ARGV[1], p->use_raw);
399
400 const char *msg = p->use_raw ? "enabled" : "disabled";
401 command_print(CMD_CTX, "raw access is %s", msg);
402
403 return ERROR_OK;
404 }
405
406 static const struct command_registration nand_exec_command_handlers[] = {
407 {
408 .name = "list",
409 .handler = handle_nand_list_command,
410 .mode = COMMAND_EXEC,
411 .help = "list configured NAND flash devices",
412 },
413 {
414 .name = "info",
415 .handler = handle_nand_info_command,
416 .mode = COMMAND_EXEC,
417 .usage = "[banknum | first_bank_num last_bank_num]",
418 .help = "print info about one or more NAND flash devices",
419 },
420 {
421 .name = "probe",
422 .handler = handle_nand_probe_command,
423 .mode = COMMAND_EXEC,
424 .usage = "bank_id",
425 .help = "identify NAND flash device",
426 },
427 {
428 .name = "check_bad_blocks",
429 .handler = handle_nand_check_bad_blocks_command,
430 .mode = COMMAND_EXEC,
431 .usage = "bank_id [offset length]",
432 .help = "check all or part of NAND flash device for bad blocks",
433 },
434 {
435 .name = "erase",
436 .handler = handle_nand_erase_command,
437 .mode = COMMAND_EXEC,
438 .usage = "bank_id [offset length]",
439 .help = "erase all or subset of blocks on NAND flash device",
440 },
441 {
442 .name = "dump",
443 .handler = handle_nand_dump_command,
444 .mode = COMMAND_EXEC,
445 .usage = "bank_id filename offset length "
446 "['oob_raw'|'oob_only']",
447 .help = "dump from NAND flash device",
448 },
449 {
450 .name = "verify",
451 .handler = handle_nand_verify_command,
452 .mode = COMMAND_EXEC,
453 .usage = "bank_id filename offset "
454 "['oob_raw'|'oob_only'|'oob_softecc'|'oob_softecc_kw']",
455 .help = "verify NAND flash device",
456 },
457 {
458 .name = "write",
459 .handler = handle_nand_write_command,
460 .mode = COMMAND_EXEC,
461 .usage = "bank_id filename offset "
462 "['oob_raw'|'oob_only'|'oob_softecc'|'oob_softecc_kw']",
463 .help = "write to NAND flash device",
464 },
465 {
466 .name = "raw_access",
467 .handler = handle_nand_raw_access_command,
468 .mode = COMMAND_EXEC,
469 .usage = "bank_id ['enable'|'disable']",
470 .help = "raw access to NAND flash device",
471 },
472 COMMAND_REGISTRATION_DONE
473 };
474
475 static int nand_init(struct command_context *cmd_ctx)
476 {
477 if (!nand_devices)
478 return ERROR_OK;
479 struct command *parent = command_find_in_context(cmd_ctx, "nand");
480 return register_commands(cmd_ctx, parent, nand_exec_command_handlers);
481 }
482
483 COMMAND_HANDLER(handle_nand_init_command)
484 {
485 if (CMD_ARGC != 0)
486 return ERROR_COMMAND_SYNTAX_ERROR;
487
488 static bool nand_initialized;
489 if (nand_initialized) {
490 LOG_INFO("'nand init' has already been called");
491 return ERROR_OK;
492 }
493 nand_initialized = true;
494
495 LOG_DEBUG("Initializing NAND devices...");
496 return nand_init(CMD_CTX);
497 }
498
499 static int nand_list_walker(struct nand_flash_controller *c, void *x)
500 {
501 struct command_context *cmd_ctx = x;
502 command_print(cmd_ctx, " %s", c->name);
503 return ERROR_OK;
504 }
505
506 COMMAND_HANDLER(handle_nand_list_drivers)
507 {
508 command_print(CMD_CTX, "Available NAND flash controller drivers:");
509 return nand_driver_walk(&nand_list_walker, CMD_CTX);
510 }
511
512 static COMMAND_HELPER(create_nand_device, const char *bank_name,
513 struct nand_flash_controller *controller)
514 {
515 struct nand_device *c;
516 struct target *target;
517 int retval;
518
519 if (CMD_ARGC < 2)
520 return ERROR_COMMAND_SYNTAX_ERROR;
521 target = get_target(CMD_ARGV[1]);
522 if (!target) {
523 LOG_ERROR("invalid target %s", CMD_ARGV[1]);
524 return ERROR_COMMAND_ARGUMENT_INVALID;
525 }
526
527 if (NULL != controller->commands) {
528 retval = register_commands(CMD_CTX, NULL,
529 controller->commands);
530 if (ERROR_OK != retval)
531 return retval;
532 }
533 c = malloc(sizeof(struct nand_device));
534 if (c == NULL) {
535 LOG_ERROR("End of memory");
536 return ERROR_FAIL;
537 }
538
539 c->name = strdup(bank_name);
540 c->target = target;
541 c->controller = controller;
542 c->controller_priv = NULL;
543 c->manufacturer = NULL;
544 c->device = NULL;
545 c->bus_width = 0;
546 c->address_cycles = 0;
547 c->page_size = 0;
548 c->use_raw = false;
549 c->next = NULL;
550
551 retval = CALL_COMMAND_HANDLER(controller->nand_device_command, c);
552 if (ERROR_OK != retval) {
553 LOG_ERROR("'%s' driver rejected nand flash. Usage: %s",
554 controller->name,
555 controller->usage);
556 free(c);
557 return retval;
558 }
559
560 if (controller->usage == NULL)
561 LOG_DEBUG("'%s' driver usage field missing", controller->name);
562
563 nand_device_add(c);
564
565 return ERROR_OK;
566 }
567
568 COMMAND_HANDLER(handle_nand_device_command)
569 {
570 if (CMD_ARGC < 2)
571 return ERROR_COMMAND_SYNTAX_ERROR;
572
573 /* save name and increment (for compatibility) with drivers */
574 const char *bank_name = *CMD_ARGV++;
575 CMD_ARGC--;
576
577 const char *driver_name = CMD_ARGV[0];
578 struct nand_flash_controller *controller;
579 controller = nand_driver_find_by_name(CMD_ARGV[0]);
580 if (NULL == controller) {
581 LOG_ERROR("No valid NAND flash driver found (%s)", driver_name);
582 return CALL_COMMAND_HANDLER(handle_nand_list_drivers);
583 }
584 return CALL_COMMAND_HANDLER(create_nand_device, bank_name, controller);
585 }
586
587 static const struct command_registration nand_config_command_handlers[] = {
588 {
589 .name = "device",
590 .handler = &handle_nand_device_command,
591 .mode = COMMAND_CONFIG,
592 .help = "defines a new NAND bank",
593 .usage = "bank_id driver target [driver_options ...]",
594 },
595 {
596 .name = "drivers",
597 .handler = &handle_nand_list_drivers,
598 .mode = COMMAND_ANY,
599 .help = "lists available NAND drivers",
600 .usage = ""
601 },
602 {
603 .name = "init",
604 .mode = COMMAND_CONFIG,
605 .handler = &handle_nand_init_command,
606 .help = "initialize NAND devices",
607 .usage = ""
608 },
609 COMMAND_REGISTRATION_DONE
610 };
611
612 static const struct command_registration nand_command_handlers[] = {
613 {
614 .name = "nand",
615 .mode = COMMAND_ANY,
616 .help = "NAND flash command group",
617 .usage = "",
618 .chain = nand_config_command_handlers,
619 },
620 COMMAND_REGISTRATION_DONE
621 };
622
623 int nand_register_commands(struct command_context *cmd_ctx)
624 {
625 return register_commands(cmd_ctx, NULL, nand_command_handlers);
626 }