1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
6 * drivers/mtd/nand_ids.c *
8 * Copyright (C) 2002 Thomas Gleixner (tglx@linutronix.de) *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 ***************************************************************************/
29 #include "replacements.h"
40 #include "time_support.h"
44 int nand_register_commands(struct command_context_s *cmd_ctx);
45 int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_nand_copy_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int nand_read_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
57 int nand_read_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
58 int nand_read_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size);
60 int nand_write_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
61 int nand_write_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
63 /* NAND flash controller
65 extern nand_flash_controller_t lpc3180_nand_controller;
66 extern nand_flash_controller_t s3c2410_nand_controller;
67 extern nand_flash_controller_t s3c2412_nand_controller;
68 extern nand_flash_controller_t s3c2440_nand_controller;
69 extern nand_flash_controller_t s3c2443_nand_controller;
71 /* extern nand_flash_controller_t boundary_scan_nand_controller; */
73 nand_flash_controller_t *nand_flash_controllers[] =
75 &lpc3180_nand_controller,
76 &s3c2410_nand_controller,
77 &s3c2412_nand_controller,
78 &s3c2440_nand_controller,
79 &s3c2443_nand_controller,
80 /* &boundary_scan_nand_controller, */
84 /* configured NAND devices and NAND Flash command handler */
85 nand_device_t *nand_devices = NULL;
86 static command_t *nand_cmd;
90 * Name, ID code, pagesize, chipsize in MegaByte, eraseblock size,
93 * Pagesize; 0, 256, 512
94 * 0 get this information from the extended chip ID
95 * 256 256 Byte page size
96 * 512 512 Byte page size
98 nand_info_t nand_flash_ids[] =
100 {"NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0},
101 {"NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0},
102 {"NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0},
103 {"NAND 1MiB 3,3V 8-bit", 0xe8, 256, 1, 0x1000, 0},
104 {"NAND 1MiB 3,3V 8-bit", 0xec, 256, 1, 0x1000, 0},
105 {"NAND 2MiB 3,3V 8-bit", 0xea, 256, 2, 0x1000, 0},
106 {"NAND 4MiB 3,3V 8-bit", 0xd5, 512, 4, 0x2000, 0},
107 {"NAND 4MiB 3,3V 8-bit", 0xe3, 512, 4, 0x2000, 0},
108 {"NAND 4MiB 3,3V 8-bit", 0xe5, 512, 4, 0x2000, 0},
109 {"NAND 8MiB 3,3V 8-bit", 0xd6, 512, 8, 0x2000, 0},
111 {"NAND 8MiB 1,8V 8-bit", 0x39, 512, 8, 0x2000, 0},
112 {"NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0},
113 {"NAND 8MiB 1,8V 16-bit", 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16},
114 {"NAND 8MiB 3,3V 16-bit", 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16},
116 {"NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0},
117 {"NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0},
118 {"NAND 16MiB 1,8V 16-bit", 0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16},
119 {"NAND 16MiB 3,3V 16-bit", 0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16},
121 {"NAND 32MiB 1,8V 8-bit", 0x35, 512, 32, 0x4000, 0},
122 {"NAND 32MiB 3,3V 8-bit", 0x75, 512, 32, 0x4000, 0},
123 {"NAND 32MiB 1,8V 16-bit", 0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16},
124 {"NAND 32MiB 3,3V 16-bit", 0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16},
126 {"NAND 64MiB 1,8V 8-bit", 0x36, 512, 64, 0x4000, 0},
127 {"NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000, 0},
128 {"NAND 64MiB 1,8V 16-bit", 0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16},
129 {"NAND 64MiB 3,3V 16-bit", 0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16},
131 {"NAND 128MiB 1,8V 8-bit", 0x78, 512, 128, 0x4000, 0},
132 {"NAND 128MiB 1,8V 8-bit", 0x39, 512, 128, 0x4000, 0},
133 {"NAND 128MiB 3,3V 8-bit", 0x79, 512, 128, 0x4000, 0},
134 {"NAND 128MiB 1,8V 16-bit", 0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16},
135 {"NAND 128MiB 1,8V 16-bit", 0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16},
136 {"NAND 128MiB 3,3V 16-bit", 0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16},
137 {"NAND 128MiB 3,3V 16-bit", 0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16},
139 {"NAND 256MiB 3,3V 8-bit", 0x71, 512, 256, 0x4000, 0},
141 {"NAND 64MiB 1,8V 8-bit", 0xA2, 0, 64, 0, LP_OPTIONS},
142 {"NAND 64MiB 3,3V 8-bit", 0xF2, 0, 64, 0, LP_OPTIONS},
143 {"NAND 64MiB 1,8V 16-bit", 0xB2, 0, 64, 0, LP_OPTIONS16},
144 {"NAND 64MiB 3,3V 16-bit", 0xC2, 0, 64, 0, LP_OPTIONS16},
146 {"NAND 128MiB 1,8V 8-bit", 0xA1, 0, 128, 0, LP_OPTIONS},
147 {"NAND 128MiB 3,3V 8-bit", 0xF1, 0, 128, 0, LP_OPTIONS},
148 {"NAND 128MiB 1,8V 16-bit", 0xB1, 0, 128, 0, LP_OPTIONS16},
149 {"NAND 128MiB 3,3V 16-bit", 0xC1, 0, 128, 0, LP_OPTIONS16},
151 {"NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_OPTIONS},
152 {"NAND 256MiB 3,3V 8-bit", 0xDA, 0, 256, 0, LP_OPTIONS},
153 {"NAND 256MiB 1,8V 16-bit", 0xBA, 0, 256, 0, LP_OPTIONS16},
154 {"NAND 256MiB 3,3V 16-bit", 0xCA, 0, 256, 0, LP_OPTIONS16},
156 {"NAND 512MiB 1,8V 8-bit", 0xAC, 0, 512, 0, LP_OPTIONS},
157 {"NAND 512MiB 3,3V 8-bit", 0xDC, 0, 512, 0, LP_OPTIONS},
158 {"NAND 512MiB 1,8V 16-bit", 0xBC, 0, 512, 0, LP_OPTIONS16},
159 {"NAND 512MiB 3,3V 16-bit", 0xCC, 0, 512, 0, LP_OPTIONS16},
161 {"NAND 1GiB 1,8V 8-bit", 0xA3, 0, 1024, 0, LP_OPTIONS},
162 {"NAND 1GiB 3,3V 8-bit", 0xD3, 0, 1024, 0, LP_OPTIONS},
163 {"NAND 1GiB 1,8V 16-bit", 0xB3, 0, 1024, 0, LP_OPTIONS16},
164 {"NAND 1GiB 3,3V 16-bit", 0xC3, 0, 1024, 0, LP_OPTIONS16},
166 {"NAND 2GiB 1,8V 8-bit", 0xA5, 0, 2048, 0, LP_OPTIONS},
167 {"NAND 2GiB 3,3V 8-bit", 0xD5, 0, 2048, 0, LP_OPTIONS},
168 {"NAND 2GiB 1,8V 16-bit", 0xB5, 0, 2048, 0, LP_OPTIONS16},
169 {"NAND 2GiB 3,3V 16-bit", 0xC5, 0, 2048, 0, LP_OPTIONS16},
174 /* Manufacturer ID list
176 nand_manufacturer_t nand_manuf_ids[] =
179 {NAND_MFR_TOSHIBA, "Toshiba"},
180 {NAND_MFR_SAMSUNG, "Samsung"},
181 {NAND_MFR_FUJITSU, "Fujitsu"},
182 {NAND_MFR_NATIONAL, "National"},
183 {NAND_MFR_RENESAS, "Renesas"},
184 {NAND_MFR_STMICRO, "ST Micro"},
185 {NAND_MFR_HYNIX, "Hynix"},
189 /* nand device <nand_controller> [controller options]
191 int handle_nand_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
198 WARNING("incomplete flash device nand configuration");
199 return ERROR_FLASH_BANK_INVALID;
202 for (i = 0; nand_flash_controllers[i]; i++)
204 nand_device_t *p, *c;
206 if (strcmp(args[0], nand_flash_controllers[i]->name) == 0)
208 /* register flash specific commands */
209 if (nand_flash_controllers[i]->register_commands(cmd_ctx) != ERROR_OK)
211 ERROR("couldn't register '%s' commands", args[0]);
215 c = malloc(sizeof(nand_device_t));
217 c->controller = nand_flash_controllers[i];
218 c->controller_priv = NULL;
219 c->manufacturer = NULL;
222 c->address_cycles = 0;
227 if ((retval = nand_flash_controllers[i]->nand_device_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
229 ERROR("'%s' driver rejected nand flash", c->controller->name);
234 /* put NAND device in linked list */
237 /* find last flash device */
238 for (p = nand_devices; p && p->next; p = p->next);
251 /* no valid NAND controller was found (i.e. the configuration option,
252 * didn't match one of the compiled-in controllers)
254 ERROR("No valid NAND flash controller found (%s)", args[0]);
255 ERROR("compiled-in NAND flash controllers:");
256 for (i = 0; nand_flash_controllers[i]; i++)
258 ERROR("%i: %s", i, nand_flash_controllers[i]->name);
264 int nand_register_commands(struct command_context_s *cmd_ctx)
266 nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
268 register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
273 int nand_init(struct command_context_s *cmd_ctx)
277 register_command(cmd_ctx, nand_cmd, "list", handle_nand_list_command, COMMAND_EXEC,
278 "list configured NAND flash devices");
279 register_command(cmd_ctx, nand_cmd, "info", handle_nand_info_command, COMMAND_EXEC,
280 "print info about NAND flash device <num>");
281 register_command(cmd_ctx, nand_cmd, "probe", handle_nand_probe_command, COMMAND_EXEC,
282 "identify NAND flash device <num>");
283 register_command(cmd_ctx, nand_cmd, "check_bad_blocks", handle_nand_check_bad_blocks_command, COMMAND_EXEC,
284 "check NAND flash device <num> for bad blocks [<first> <last>]");
285 register_command(cmd_ctx, nand_cmd, "erase", handle_nand_erase_command, COMMAND_EXEC,
286 "erase blocks on NAND flash device <num> <first> <last>");
287 register_command(cmd_ctx, nand_cmd, "copy", handle_nand_copy_command, COMMAND_EXEC,
288 "copy from NAND flash device <num> <offset> <length> <ram-address>");
289 register_command(cmd_ctx, nand_cmd, "dump", handle_nand_dump_command, COMMAND_EXEC,
290 "dump from NAND flash device <num> <filename> <offset> <size> [options]");
291 register_command(cmd_ctx, nand_cmd, "write", handle_nand_write_command, COMMAND_EXEC,
292 "write to NAND flash device <num> <filename> <offset> [options]");
293 register_command(cmd_ctx, nand_cmd, "raw_access", handle_nand_raw_access_command, COMMAND_EXEC,
294 "raw access to NAND flash device <num> ['enable'|'disable']");
300 nand_device_t *get_nand_device_by_num(int num)
305 for (p = nand_devices; p; p = p->next)
316 int nand_build_bbt(struct nand_device_s *device, int first, int last)
324 if ((first < 0) || (first >= device->num_blocks))
327 if ((last >= device->num_blocks) || (last == -1))
328 last = device->num_blocks - 1;
330 for (i = first; i < last; i++)
332 nand_read_page(device, page, NULL, 0, oob, 6);
334 if (((device->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
335 || (((device->page_size == 512) && (oob[5] != 0xff)) ||
336 ((device->page_size == 2048) && (oob[0] != 0xff))))
338 WARNING("invalid block: %i", i);
339 device->blocks[i].is_bad = 1;
343 device->blocks[i].is_bad = 0;
346 page += (device->erase_size / device->page_size);
352 int nand_read_status(struct nand_device_s *device, u8 *status)
355 return ERROR_NAND_DEVICE_NOT_PROBED;
357 /* Send read status command */
358 device->controller->command(device, NAND_CMD_STATUS);
363 if (device->device->options & NAND_BUSWIDTH_16)
366 device->controller->read_data(device, &data);
367 *status = data & 0xff;
371 device->controller->read_data(device, status);
377 int nand_probe(struct nand_device_s *device)
379 u8 manufacturer_id, device_id;
383 /* clear device data */
384 device->device = NULL;
385 device->manufacturer = NULL;
387 /* clear device parameters */
388 device->bus_width = 0;
389 device->address_cycles = 0;
390 device->page_size = 0;
391 device->erase_size = 0;
393 /* initialize controller (device parameters are zero, use controller default) */
394 if ((retval = device->controller->init(device) != ERROR_OK))
398 case ERROR_NAND_OPERATION_FAILED:
399 DEBUG("controller initialization failed");
400 return ERROR_NAND_OPERATION_FAILED;
401 case ERROR_NAND_OPERATION_NOT_SUPPORTED:
402 ERROR("BUG: controller reported that it doesn't support default parameters");
403 return ERROR_NAND_OPERATION_FAILED;
405 ERROR("BUG: unknown controller initialization failure");
406 return ERROR_NAND_OPERATION_FAILED;
410 device->controller->command(device, NAND_CMD_RESET);
411 device->controller->reset(device);
413 device->controller->command(device, NAND_CMD_READID);
414 device->controller->address(device, 0x0);
416 if (device->bus_width == 8)
418 device->controller->read_data(device, &manufacturer_id);
419 device->controller->read_data(device, &device_id);
424 device->controller->read_data(device, &data_buf);
425 manufacturer_id = data_buf & 0xff;
426 device->controller->read_data(device, &data_buf);
427 device_id = data_buf & 0xff;
430 for (i = 0; nand_flash_ids[i].name; i++)
432 if (nand_flash_ids[i].id == device_id)
434 device->device = &nand_flash_ids[i];
439 for (i = 0; nand_manuf_ids[i].name; i++)
441 if (nand_manuf_ids[i].id == manufacturer_id)
443 device->manufacturer = &nand_manuf_ids[i];
448 if (!device->manufacturer)
450 device->manufacturer = &nand_manuf_ids[0];
451 device->manufacturer->id = manufacturer_id;
456 ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
457 manufacturer_id, device_id);
458 return ERROR_NAND_OPERATION_FAILED;
461 DEBUG("found %s (%s)", device->device->name, device->manufacturer->name);
463 /* initialize device parameters */
466 if (device->device->options & NAND_BUSWIDTH_16)
467 device->bus_width = 16;
469 device->bus_width = 8;
472 if (device->device->page_size == 0)
474 /* TODO: support reading extended chip id to determine page size */
475 return ERROR_NAND_OPERATION_FAILED;
477 else if (device->device->page_size == 256)
479 ERROR("NAND flashes with 256 byte pagesize are not supported");
480 return ERROR_NAND_OPERATION_FAILED;
484 device->page_size = device->device->page_size;
487 /* number of address cycles */
488 if (device->page_size <= 512)
490 /* small page devices */
491 if (device->device->chip_size <= 32)
492 device->address_cycles = 3;
493 else if (device->device->chip_size <= 8*1024)
494 device->address_cycles = 4;
497 ERROR("BUG: small page NAND device with more than 8 GiB encountered");
498 device->address_cycles = 5;
503 /* large page devices */
504 if (device->device->chip_size <= 128)
505 device->address_cycles = 4;
506 else if (device->device->chip_size <= 32*1024)
507 device->address_cycles = 5;
510 ERROR("BUG: small page NAND device with more than 32 GiB encountered");
511 device->address_cycles = 6;
516 if (device->device->erase_size == 0)
518 /* TODO: support reading extended chip id to determine erase size */
522 device->erase_size = device->device->erase_size;
525 /* initialize controller, but leave parameters at the controllers default */
526 if ((retval = device->controller->init(device) != ERROR_OK))
530 case ERROR_NAND_OPERATION_FAILED:
531 DEBUG("controller initialization failed");
532 return ERROR_NAND_OPERATION_FAILED;
533 case ERROR_NAND_OPERATION_NOT_SUPPORTED:
534 ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
535 device->bus_width, device->address_cycles, device->page_size);
536 return ERROR_NAND_OPERATION_FAILED;
538 ERROR("BUG: unknown controller initialization failure");
539 return ERROR_NAND_OPERATION_FAILED;
543 device->num_blocks = (device->device->chip_size * 1024) / (device->erase_size / 1024);
544 device->blocks = malloc(sizeof(nand_block_t) * device->num_blocks);
546 for (i = 0; i < device->num_blocks; i++)
548 device->blocks[i].size = device->erase_size;
549 device->blocks[i].offset = i * device->erase_size;
550 device->blocks[i].is_erased = -1;
551 device->blocks[i].is_bad = -1;
557 int nand_erase(struct nand_device_s *device, int first_block, int last_block)
565 return ERROR_NAND_DEVICE_NOT_PROBED;
567 if ((first_block < 0) || (last_block > device->num_blocks))
568 return ERROR_INVALID_ARGUMENTS;
570 /* make sure we know if a block is bad before erasing it */
571 for (i = first_block; i <= last_block; i++)
573 if (device->blocks[i].is_bad == -1)
575 nand_build_bbt(device, i, last_block);
580 for (i = first_block; i <= last_block; i++)
582 /* Send erase setup command */
583 device->controller->command(device, NAND_CMD_ERASE1);
585 page = i * (device->erase_size / device->page_size);
587 /* Send page address */
588 if (device->page_size <= 512)
591 device->controller->address(device, page & 0xff);
592 device->controller->address(device, (page >> 8) & 0xff);
594 /* 3rd cycle only on devices with more than 32 MiB */
595 if (device->address_cycles >= 4)
596 device->controller->address(device, (page >> 16) & 0xff);
598 /* 4th cycle only on devices with more than 8 GiB */
599 if (device->address_cycles >= 5)
600 device->controller->address(device, (page >> 24) & 0xff);
605 device->controller->address(device, page & 0xff);
606 device->controller->address(device, (page >> 8) & 0xff);
608 /* 3rd cycle only on devices with more than 128 MiB */
609 if (device->address_cycles >= 5)
610 device->controller->address(device, (page >> 16) & 0xff);
613 /* Send erase confirm command */
614 device->controller->command(device, NAND_CMD_ERASE2);
616 if (!device->controller->nand_ready(device, 1000))
618 ERROR("timeout waiting for NAND flash block erase to complete");
619 return ERROR_NAND_OPERATION_TIMEOUT;
622 if ((retval = nand_read_status(device, &status)) != ERROR_OK)
624 ERROR("couldn't read status");
625 return ERROR_NAND_OPERATION_FAILED;
630 ERROR("erase operation didn't pass, status: 0x%2.2x", status);
631 return ERROR_NAND_OPERATION_FAILED;
638 int nand_read_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size)
643 return ERROR_NAND_DEVICE_NOT_PROBED;
645 if (address % device->page_size)
647 ERROR("reads need to be page aligned");
648 return ERROR_NAND_OPERATION_FAILED;
651 page = malloc(device->page_size);
653 while (data_size > 0 )
655 u32 thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
659 page_address = address / device->page_size;
661 nand_read_page(device, page_address, page, device->page_size, NULL, 0);
663 memcpy(data, page, thisrun_size);
665 address += thisrun_size;
666 data += thisrun_size;
667 data_size -= thisrun_size;
675 int nand_write_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size)
680 return ERROR_NAND_DEVICE_NOT_PROBED;
682 if (address % device->page_size)
684 ERROR("writes need to be page aligned");
685 return ERROR_NAND_OPERATION_FAILED;
688 page = malloc(device->page_size);
690 while (data_size > 0 )
692 u32 thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
695 memset(page, 0xff, device->page_size);
696 memcpy(page, data, thisrun_size);
698 page_address = address / device->page_size;
700 nand_write_page(device, page_address, page, device->page_size, NULL, 0);
702 address += thisrun_size;
703 data += thisrun_size;
704 data_size -= thisrun_size;
712 int nand_write_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
715 return ERROR_NAND_DEVICE_NOT_PROBED;
717 if (device->use_raw || device->controller->write_page == NULL)
718 return nand_write_page_raw(device, page, data, data_size, oob, oob_size);
720 return device->controller->write_page(device, page, data, data_size, oob, oob_size);
723 int nand_read_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
726 return ERROR_NAND_DEVICE_NOT_PROBED;
728 if (device->use_raw || device->controller->read_page == NULL)
729 return nand_read_page_raw(device, page, data, data_size, oob, oob_size);
731 return device->controller->read_page(device, page, data, data_size, oob, oob_size);
734 int nand_read_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
739 return ERROR_NAND_DEVICE_NOT_PROBED;
741 if (device->page_size <= 512)
743 /* small page device */
745 device->controller->command(device, NAND_CMD_READ0);
747 device->controller->command(device, NAND_CMD_READOOB);
749 /* column (always 0, we start at the beginning of a page/OOB area) */
750 device->controller->address(device, 0x0);
753 device->controller->address(device, page & 0xff);
754 device->controller->address(device, (page >> 8) & 0xff);
756 /* 4th cycle only on devices with more than 32 MiB */
757 if (device->address_cycles >= 4)
758 device->controller->address(device, (page >> 16) & 0xff);
760 /* 5th cycle only on devices with more than 8 GiB */
761 if (device->address_cycles >= 5)
762 device->controller->address(device, (page >> 24) & 0xff);
766 /* large page device */
767 device->controller->command(device, NAND_CMD_READ0);
769 /* column (0 when we start at the beginning of a page,
770 * or 2048 for the beginning of OOB area)
772 device->controller->address(device, 0x0);
773 device->controller->address(device, 0x8);
776 device->controller->address(device, page & 0xff);
777 device->controller->address(device, (page >> 8) & 0xff);
779 /* 5th cycle only on devices with more than 128 MiB */
780 if (device->address_cycles >= 5)
781 device->controller->address(device, (page >> 16) & 0xff);
783 /* large page devices need a start command */
784 device->controller->command(device, NAND_CMD_READSTART);
787 if (!device->controller->nand_ready(device, 100))
788 return ERROR_NAND_OPERATION_TIMEOUT;
792 if (device->controller->read_block_data != NULL)
793 (device->controller->read_block_data)(device, data, data_size);
796 for (i = 0; i < data_size;)
798 if (device->device->options & NAND_BUSWIDTH_16)
800 device->controller->read_data(device, data);
806 device->controller->read_data(device, data);
816 if (device->controller->read_block_data != NULL)
817 (device->controller->read_block_data)(device, oob, oob_size);
820 for (i = 0; i < oob_size;)
822 if (device->device->options & NAND_BUSWIDTH_16)
824 device->controller->read_data(device, oob);
830 device->controller->read_data(device, oob);
841 int nand_write_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
848 return ERROR_NAND_DEVICE_NOT_PROBED;
850 device->controller->command(device, NAND_CMD_SEQIN);
852 if (device->page_size <= 512)
854 /* column (always 0, we start at the beginning of a page/OOB area) */
855 device->controller->address(device, 0x0);
858 device->controller->address(device, page & 0xff);
859 device->controller->address(device, (page >> 8) & 0xff);
861 /* 4th cycle only on devices with more than 32 MiB */
862 if (device->address_cycles >= 4)
863 device->controller->address(device, (page >> 16) & 0xff);
865 /* 5th cycle only on devices with more than 8 GiB */
866 if (device->address_cycles >= 5)
867 device->controller->address(device, (page >> 24) & 0xff);
871 /* column (0 when we start at the beginning of a page,
872 * or 2048 for the beginning of OOB area)
874 device->controller->address(device, 0x0);
875 device->controller->address(device, 0x8);
878 device->controller->address(device, page & 0xff);
879 device->controller->address(device, (page >> 8) & 0xff);
881 /* 5th cycle only on devices with more than 128 MiB */
882 if (device->address_cycles >= 5)
883 device->controller->address(device, (page >> 16) & 0xff);
888 if (device->controller->write_block_data != NULL)
889 (device->controller->write_block_data)(device, data, data_size);
892 for (i = 0; i < data_size;)
894 if (device->device->options & NAND_BUSWIDTH_16)
896 u16 data_buf = le_to_h_u16(data);
897 device->controller->write_data(device, data_buf);
903 device->controller->write_data(device, *data);
913 if (device->controller->write_block_data != NULL)
914 (device->controller->write_block_data)(device, oob, oob_size);
917 for (i = 0; i < oob_size;)
919 if (device->device->options & NAND_BUSWIDTH_16)
921 u16 oob_buf = le_to_h_u16(data);
922 device->controller->write_data(device, oob_buf);
928 device->controller->write_data(device, *oob);
936 device->controller->command(device, NAND_CMD_PAGEPROG);
938 if (!device->controller->nand_ready(device, 100))
939 return ERROR_NAND_OPERATION_TIMEOUT;
941 if ((retval = nand_read_status(device, &status)) != ERROR_OK)
943 ERROR("couldn't read status");
944 return ERROR_NAND_OPERATION_FAILED;
947 if (status & NAND_STATUS_FAIL)
949 ERROR("write operation didn't pass, status: 0x%2.2x", status);
950 return ERROR_NAND_OPERATION_FAILED;
956 int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
963 command_print(cmd_ctx, "no NAND flash devices configured");
967 for (p = nand_devices; p; p = p->next)
970 command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
971 i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
973 command_print(cmd_ctx, "#%i: not probed");
979 int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
987 if ((argc < 1) || (argc > 3))
989 command_print(cmd_ctx, "usage: nand info <num> [<first> <last>]");
995 first = last = strtoul(args[1], NULL, 0);
999 first = strtoul(args[1], NULL, 0);
1000 last = strtoul(args[2], NULL, 0);
1003 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1008 if (first >= p->num_blocks)
1009 first = p->num_blocks - 1;
1011 if (last >= p->num_blocks)
1012 last = p->num_blocks - 1;
1014 command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
1015 i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
1017 for (j = first; j <= last; j++)
1019 char *erase_state, *bad_state;
1021 if (p->blocks[j].is_erased == 0)
1022 erase_state = "not erased";
1023 else if (p->blocks[j].is_erased == 1)
1024 erase_state = "erased";
1026 erase_state = "erase state unknown";
1028 if (p->blocks[j].is_bad == 0)
1030 else if (p->blocks[j].is_bad == 1)
1031 bad_state = " (marked bad)";
1033 bad_state = " (block condition unknown)";
1035 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s%s",
1036 j, p->blocks[j].offset, p->blocks[j].size / 1024,
1037 erase_state, bad_state);
1042 command_print(cmd_ctx, "#%i: not probed");
1049 int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1056 command_print(cmd_ctx, "usage: nand probe <num>");
1060 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1063 if ((retval = nand_probe(p)) == ERROR_OK)
1065 command_print(cmd_ctx, "NAND flash device '%s' found", p->device->name);
1067 else if (retval == ERROR_NAND_OPERATION_FAILED)
1069 command_print(cmd_ctx, "probing failed for NAND flash device");
1073 command_print(cmd_ctx, "unknown error when probing NAND flash device");
1078 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1084 int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1091 command_print(cmd_ctx, "usage: nand erase <num> <first> <last>");
1095 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1098 int first = strtoul(args[1], NULL, 0);
1099 int last = strtoul(args[2], NULL, 0);
1101 if ((retval = nand_erase(p, first, last)) == ERROR_OK)
1103 command_print(cmd_ctx, "successfully erased blocks %i to %i on NAND flash device '%s'", first, last, p->device->name);
1105 else if (retval == ERROR_NAND_OPERATION_FAILED)
1107 command_print(cmd_ctx, "erase failed");
1111 command_print(cmd_ctx, "unknown error when erasing NAND flash device");
1116 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1122 int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1129 if ((argc < 1) || (argc > 3) || (argc == 2))
1131 command_print(cmd_ctx, "usage: nand check_bad_blocks <num> [<first> <last>]");
1137 first = strtoul(args[1], NULL, 0);
1138 last = strtoul(args[2], NULL, 0);
1141 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1144 if ((retval = nand_build_bbt(p, first, last)) == ERROR_OK)
1146 command_print(cmd_ctx, "checked NAND flash device for bad blocks, use \"nand info\" command to list blocks", p->device->name);
1148 else if (retval == ERROR_NAND_OPERATION_FAILED)
1150 command_print(cmd_ctx, "error when checking for bad blocks on NAND flash device");
1154 command_print(cmd_ctx, "unknown error when checking for bad blocks on NAND flash device");
1159 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1165 int handle_nand_copy_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1171 command_print(cmd_ctx, "usage: nand copy <num> <offset> <length> <ram-address>");
1175 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1182 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1188 int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1193 enum oob_formats oob_format = NAND_OOB_NONE;
1197 duration_t duration;
1198 char *duration_text;
1204 command_print(cmd_ctx, "usage: nand write <num> <file> <offset> [options]");
1208 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1216 duration_start_measure(&duration);
1217 offset = strtoul(args[2], NULL, 0);
1222 for (i = 3; i < argc; i++)
1224 if (!strcmp(args[i], "oob_raw"))
1225 oob_format |= NAND_OOB_RAW;
1226 else if (!strcmp(args[i], "oob_only"))
1227 oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
1230 command_print(cmd_ctx, "unknown option: %s", args[i]);
1235 if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
1237 command_print(cmd_ctx, "file open error: %s", fileio.error_str);
1241 buf_cnt = binary_size = fileio.size;
1243 if (!(oob_format & NAND_OOB_ONLY))
1245 page_size = p->page_size;
1246 page = malloc(p->page_size);
1249 if (oob_format & NAND_OOB_RAW)
1251 if (p->page_size == 512)
1253 else if (p->page_size == 2048)
1255 oob = malloc(oob_size);
1258 if (offset % p->page_size)
1260 command_print(cmd_ctx, "only page size aligned offsets and sizes are supported");
1270 fileio_read(&fileio, page_size, page, &size_read);
1271 buf_cnt -= size_read;
1272 if (size_read < page_size)
1274 memset(page + size_read, 0xff, page_size - size_read);
1280 fileio_read(&fileio, oob_size, oob, &size_read);
1281 buf_cnt -= size_read;
1282 if (size_read < oob_size)
1284 memset(oob + size_read, 0xff, oob_size - size_read);
1288 if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK)
1290 command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8x",
1291 args[1], args[0], offset);
1294 offset += page_size;
1297 fileio_close(&fileio);
1299 duration_stop_measure(&duration, &duration_text);
1300 command_print(cmd_ctx, "wrote file %s to NAND flash %s at offset 0x%8.8x in %s",
1301 args[1], args[0], offset, duration_text);
1302 free(duration_text);
1306 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1312 int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1318 command_print(cmd_ctx, "usage: nand dump <num> <filename> <address> <size> [options]");
1322 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1328 duration_t duration;
1329 char *duration_text;
1336 u32 address = strtoul(args[2], NULL, 0);
1337 u32 size = strtoul(args[3], NULL, 0);
1339 enum oob_formats oob_format = NAND_OOB_NONE;
1344 for (i = 4; i < argc; i++)
1346 if (!strcmp(args[i], "oob_raw"))
1347 oob_format |= NAND_OOB_RAW;
1348 else if (!strcmp(args[i], "oob_only"))
1349 oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
1351 command_print(cmd_ctx, "unknown option: '%s'", args[i]);
1355 if ((address % p->page_size) || (size % p->page_size))
1357 command_print(cmd_ctx, "only page size aligned addresses and sizes are supported");
1361 if (!(oob_format & NAND_OOB_ONLY))
1363 page_size = p->page_size;
1364 page = malloc(p->page_size);
1367 if (oob_format & NAND_OOB_RAW)
1369 if (p->page_size == 512)
1371 else if (p->page_size == 2048)
1373 oob = malloc(oob_size);
1376 if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
1378 command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
1382 duration_start_measure(&duration);
1387 if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK)
1389 command_print(cmd_ctx, "reading NAND flash page failed");
1395 fileio_write(&fileio, page_size, page, &size_written);
1396 bytes_done += page_size;
1401 fileio_write(&fileio, oob_size, oob, &size_written);
1402 bytes_done += oob_size;
1405 size -= p->page_size;
1406 address += p->page_size;
1415 fileio_close(&fileio);
1417 duration_stop_measure(&duration, &duration_text);
1418 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
1419 free(duration_text);
1423 command_print(cmd_ctx, "#%i: not probed");
1428 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
1434 int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1438 if ((argc < 1) || (argc > 2))
1440 command_print(cmd_ctx, "usage: nand raw_access <num> ['enable'|'disable']");
1444 p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
1451 if (strcmp("enable", args[1]) == 0)
1455 else if (strcmp("disable", args[1]) == 0)
1461 command_print(cmd_ctx, "usage: nand raw_access ['enable'|disable']");
1465 command_print(cmd_ctx, "raw access is %s", (p->use_raw) ? "enabled" : "disabled");
1469 command_print(cmd_ctx, "#%i: not probed");
1474 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);