* Copyright (C) 2007 by Dominic Rath *
* Dominic.Rath@gmx.de *
* *
- * partially based on *
- * drivers/mtd/nand_ids.c *
- * *
- * Copyright (C) 2002 Thomas Gleixner (tglx@linutronix.de) *
+ * Partially based on drivers/mtd/nand_ids.c from Linux. *
+ * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
#include <stdlib.h>
#include <string.h>
+#include <inttypes.h>
#include <errno.h>
/* NAND flash controller
*/
extern nand_flash_controller_t lpc3180_nand_controller;
-/* extern nand_flash_controller_t s3c2410_nand_controller; */
+extern nand_flash_controller_t s3c2410_nand_controller;
+extern nand_flash_controller_t s3c2412_nand_controller;
+extern nand_flash_controller_t s3c2440_nand_controller;
+extern nand_flash_controller_t s3c2443_nand_controller;
+
/* extern nand_flash_controller_t boundary_scan_nand_controller; */
nand_flash_controller_t *nand_flash_controllers[] =
{
&lpc3180_nand_controller,
-/* &s3c2410_nand_controller, */
+ &s3c2410_nand_controller,
+ &s3c2412_nand_controller,
+ &s3c2440_nand_controller,
+ &s3c2443_nand_controller,
/* &boundary_scan_nand_controller, */
NULL
};
int nand_register_commands(struct command_context_s *cmd_ctx)
{
- nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, NULL);
+ nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
int nand_probe(struct nand_device_s *device)
{
u8 manufacturer_id, device_id;
+ u8 id_buff[5];
int retval;
int i;
device->bus_width = 16;
else
device->bus_width = 8;
+
+ /* Do we need extended device probe information? */
+ if (device->device->page_size == 0 ||
+ device->device->erase_size == 0)
+ {
+ if (device->bus_width == 8)
+ {
+ device->controller->read_data(device, id_buff+3);
+ device->controller->read_data(device, id_buff+4);
+ device->controller->read_data(device, id_buff+5);
+ }
+ else
+ {
+ u16 data_buf;
+
+ device->controller->read_data(device, &data_buf);
+ id_buff[3] = data_buf;
+
+ device->controller->read_data(device, &data_buf);
+ id_buff[4] = data_buf;
+
+ device->controller->read_data(device, &data_buf);
+ id_buff[5] = data_buf >> 8;
+ }
+ }
/* page size */
if (device->device->page_size == 0)
{
- /* TODO: support reading extended chip id to determine page size */
- return ERROR_NAND_OPERATION_FAILED;
+ device->page_size = 1 << (10 + (id_buff[4] & 3));
}
else if (device->device->page_size == 256)
{
/* erase size */
if (device->device->erase_size == 0)
{
- /* TODO: support reading extended chip id to determine erase size */
+ switch ((id_buff[4] >> 4) & 3) {
+ case 0:
+ device->erase_size = 64 << 10;
+ break;
+ case 1:
+ device->erase_size = 128 << 10;
+ break;
+ case 2:
+ device->erase_size = 256 << 10;
+ break;
+ case 3:
+ device->erase_size =512 << 10;
+ break;
+ }
}
else
{
if (!device->device)
return ERROR_NAND_DEVICE_NOT_PROBED;
- if (device->use_raw)
+ if (device->use_raw || device->controller->write_page == NULL)
return nand_write_page_raw(device, page, data, data_size, oob, oob_size);
else
return device->controller->write_page(device, page, data, data_size, oob, oob_size);
if (!device->device)
return ERROR_NAND_DEVICE_NOT_PROBED;
- if (device->use_raw)
+ if (device->use_raw || device->controller->read_page == NULL)
return nand_read_page_raw(device, page, data, data_size, oob, oob_size);
else
return device->controller->read_page(device, page, data, data_size, oob, oob_size);
if (data)
{
- for (i = 0; i < data_size;)
+ if (device->controller->read_block_data != NULL)
+ (device->controller->read_block_data)(device, data, data_size);
+ else
{
- if (device->device->options & NAND_BUSWIDTH_16)
+ for (i = 0; i < data_size;)
{
- device->controller->read_data(device, data);
- data += 2;
- i += 2;
- }
- else
- {
- device->controller->read_data(device, data);
- data += 1;
- i += 1;
+ if (device->device->options & NAND_BUSWIDTH_16)
+ {
+ device->controller->read_data(device, data);
+ data += 2;
+ i += 2;
+ }
+ else
+ {
+ device->controller->read_data(device, data);
+ data += 1;
+ i += 1;
+ }
}
}
}
if (oob)
{
- for (i = 0; i < oob_size;)
+ if (device->controller->read_block_data != NULL)
+ (device->controller->read_block_data)(device, oob, oob_size);
+ else
{
- if (device->device->options & NAND_BUSWIDTH_16)
+ for (i = 0; i < oob_size;)
{
- device->controller->read_data(device, oob);
- oob += 2;
- i += 2;
- }
- else
- {
- device->controller->read_data(device, oob);
- oob += 1;
- i += 1;
+ if (device->device->options & NAND_BUSWIDTH_16)
+ {
+ device->controller->read_data(device, oob);
+ oob += 2;
+ i += 2;
+ }
+ else
+ {
+ device->controller->read_data(device, oob);
+ oob += 1;
+ i += 1;
+ }
}
}
}
if (data)
{
- for (i = 0; i < data_size;)
+ if (device->controller->write_block_data != NULL)
+ (device->controller->write_block_data)(device, data, data_size);
+ else
{
- if (device->device->options & NAND_BUSWIDTH_16)
+ for (i = 0; i < data_size;)
{
- u16 data_buf = le_to_h_u16(data);
- device->controller->write_data(device, data_buf);
- data += 2;
- i += 2;
- }
- else
- {
- device->controller->write_data(device, *data);
- data += 1;
- i += 1;
+ if (device->device->options & NAND_BUSWIDTH_16)
+ {
+ u16 data_buf = le_to_h_u16(data);
+ device->controller->write_data(device, data_buf);
+ data += 2;
+ i += 2;
+ }
+ else
+ {
+ device->controller->write_data(device, *data);
+ data += 1;
+ i += 1;
+ }
}
}
}
if (oob)
{
- for (i = 0; i < oob_size;)
+ if (device->controller->write_block_data != NULL)
+ (device->controller->write_block_data)(device, oob, oob_size);
+ else
{
- if (device->device->options & NAND_BUSWIDTH_16)
+ for (i = 0; i < oob_size;)
{
- u16 oob_buf = le_to_h_u16(data);
- device->controller->write_data(device, oob_buf);
- oob += 2;
- i += 2;
- }
- else
- {
- device->controller->write_data(device, *oob);
- oob += 1;
- i += 1;
+ if (device->device->options & NAND_BUSWIDTH_16)
+ {
+ u16 oob_buf = le_to_h_u16(data);
+ device->controller->write_data(device, oob_buf);
+ oob += 2;
+ i += 2;
+ }
+ else
+ {
+ device->controller->write_data(device, *oob);
+ oob += 1;
+ i += 1;
+ }
}
}
}
if ((argc < 1) || (argc > 3))
{
- command_print(cmd_ctx, "usage: nand info <num> [<first> <last>]");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
}
if (argc == 2)
if (argc != 1)
{
- command_print(cmd_ctx, "usage: nand probe <num>");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
if (argc != 3)
{
- command_print(cmd_ctx, "usage: nand erase <num> <first> <last>");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
if ((argc < 1) || (argc > 3) || (argc == 2))
{
- command_print(cmd_ctx, "usage: nand check_bad_blocks <num> [<first> <last>]");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
}
if (argc == 3)
if (argc != 4)
{
- command_print(cmd_ctx, "usage: nand copy <num> <offset> <length> <ram-address>");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
u32 buf_cnt;
enum oob_formats oob_format = NAND_OOB_NONE;
- image_t image;
- int image_type_identified = 0;
+ fileio_t fileio;
duration_t duration;
char *duration_text;
if (argc < 3)
{
- command_print(cmd_ctx, "usage: nand write <num> <file> <offset> [options]");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
u32 oob_size = 0;
duration_start_measure(&duration);
- strtoul(args[2], NULL, 0);
+ offset = strtoul(args[2], NULL, 0);
if (argc > 3)
{
oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
else
{
- if (identify_image_type(&image.type, args[i]) == ERROR_OK)
- {
- image_type_identified = 1;
- }
- else
- {
- command_print(cmd_ctx, "unknown option: %s", args[i]);
- }
+ command_print(cmd_ctx, "unknown option: %s", args[i]);
}
}
}
- /* if no image type option was encountered, set the default */
- if (!image_type_identified)
+ if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
{
-
- identify_image_type(&image.type, NULL);
- image_type_identified = 1;
- }
-
- image.base_address_set = 1;
- image.base_address = strtoul(args[2], NULL, 0);
- image.start_address_set = 0;
-
- if (image_open(&image, args[1], FILEIO_READ) != ERROR_OK)
- {
- command_print(cmd_ctx, "flash write error: %s", image.error_str);
return ERROR_OK;
}
- /* the offset might have been overwritten by the image base address */
- offset = image.base_address;
-
- buf_cnt = binary_size = image.size;
+ buf_cnt = binary_size = fileio.size;
if (!(oob_format & NAND_OOB_ONLY))
{
if (page)
{
- image_read(&image, page_size, page, &size_read);
+ fileio_read(&fileio, page_size, page, &size_read);
buf_cnt -= size_read;
if (size_read < page_size)
{
if (oob)
{
- image_read(&image, oob_size, oob, &size_read);
+ fileio_read(&fileio, oob_size, oob, &size_read);
buf_cnt -= size_read;
if (size_read < oob_size)
{
offset += page_size;
}
+ fileio_close(&fileio);
+
duration_stop_measure(&duration, &duration_text);
command_print(cmd_ctx, "wrote file %s to NAND flash %s at offset 0x%8.8x in %s",
- args[1], args[0], image.base_address, duration_text);
+ args[1], args[0], offset, duration_text);
free(duration_text);
}
else
if (argc < 4)
{
- command_print(cmd_ctx, "usage: nand dump <num> <filename> <address> <size> [options]");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
{
- command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
return ERROR_OK;
}
fileio_close(&fileio);
duration_stop_measure(&duration, &duration_text);
- command_print(cmd_ctx, "dumped %lli byte in %s", fileio.size, duration_text);
+ command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
free(duration_text);
}
else
if ((argc < 1) || (argc > 2))
{
- command_print(cmd_ctx, "usage: nand raw_access <num> ['enable'|'disable']");
- return ERROR_OK;
+ return ERROR_COMMAND_SYNTAX_ERROR;
}
p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
}
else
{
- command_print(cmd_ctx, "usage: nand raw_access ['enable'|disable']");
+ return ERROR_COMMAND_SYNTAX_ERROR;
}
}