1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Partially based on drivers/mtd/nand_ids.c from Linux. *
6 * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
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. *
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. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
29 #include <helper/time_support.h>
30 #include <helper/fileio.h>
32 static int nand_read_page(struct nand_device
*nand
, uint32_t page
, uint8_t *data
, uint32_t data_size
, uint8_t *oob
, uint32_t oob_size
);
33 //static int nand_read_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size);
35 static int nand_write_page(struct nand_device
*nand
, uint32_t page
, uint8_t *data
, uint32_t data_size
, uint8_t *oob
, uint32_t oob_size
);
37 /* configured NAND devices and NAND Flash command handler */
38 static struct nand_device
*nand_devices
= NULL
;
42 * Name, ID code, pagesize, chipsize in MegaByte, eraseblock size,
45 * Pagesize; 0, 256, 512
46 * 0 get this information from the extended chip ID
47 * 256 256 Byte page size
48 * 512 512 Byte page size
50 static struct nand_info nand_flash_ids
[] =
52 /* start "museum" IDs */
53 {"NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0},
54 {"NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0},
55 {"NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0},
56 {"NAND 1MiB 3,3V 8-bit", 0xe8, 256, 1, 0x1000, 0},
57 {"NAND 1MiB 3,3V 8-bit", 0xec, 256, 1, 0x1000, 0},
58 {"NAND 2MiB 3,3V 8-bit", 0xea, 256, 2, 0x1000, 0},
59 {"NAND 4MiB 3,3V 8-bit", 0xd5, 512, 4, 0x2000, 0},
60 {"NAND 4MiB 3,3V 8-bit", 0xe3, 512, 4, 0x2000, 0},
61 {"NAND 4MiB 3,3V 8-bit", 0xe5, 512, 4, 0x2000, 0},
62 {"NAND 8MiB 3,3V 8-bit", 0xd6, 512, 8, 0x2000, 0},
64 {"NAND 8MiB 1,8V 8-bit", 0x39, 512, 8, 0x2000, 0},
65 {"NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0},
66 {"NAND 8MiB 1,8V 16-bit", 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16
},
67 {"NAND 8MiB 3,3V 16-bit", 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16
},
68 /* end "museum" IDs */
70 {"NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0},
71 {"NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0},
72 {"NAND 16MiB 1,8V 16-bit", 0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16
},
73 {"NAND 16MiB 3,3V 16-bit", 0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16
},
75 {"NAND 32MiB 1,8V 8-bit", 0x35, 512, 32, 0x4000, 0},
76 {"NAND 32MiB 3,3V 8-bit", 0x75, 512, 32, 0x4000, 0},
77 {"NAND 32MiB 1,8V 16-bit", 0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16
},
78 {"NAND 32MiB 3,3V 16-bit", 0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16
},
80 {"NAND 64MiB 1,8V 8-bit", 0x36, 512, 64, 0x4000, 0},
81 {"NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000, 0},
82 {"NAND 64MiB 1,8V 16-bit", 0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16
},
83 {"NAND 64MiB 3,3V 16-bit", 0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16
},
85 {"NAND 128MiB 1,8V 8-bit", 0x78, 512, 128, 0x4000, 0},
86 {"NAND 128MiB 1,8V 8-bit", 0x39, 512, 128, 0x4000, 0},
87 {"NAND 128MiB 3,3V 8-bit", 0x79, 512, 128, 0x4000, 0},
88 {"NAND 128MiB 1,8V 16-bit", 0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16
},
89 {"NAND 128MiB 1,8V 16-bit", 0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16
},
90 {"NAND 128MiB 3,3V 16-bit", 0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16
},
91 {"NAND 128MiB 3,3V 16-bit", 0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16
},
93 {"NAND 256MiB 3,3V 8-bit", 0x71, 512, 256, 0x4000, 0},
95 {"NAND 64MiB 1,8V 8-bit", 0xA2, 0, 64, 0, LP_OPTIONS
},
96 {"NAND 64MiB 3,3V 8-bit", 0xF2, 0, 64, 0, LP_OPTIONS
},
97 {"NAND 64MiB 1,8V 16-bit", 0xB2, 0, 64, 0, LP_OPTIONS16
},
98 {"NAND 64MiB 3,3V 16-bit", 0xC2, 0, 64, 0, LP_OPTIONS16
},
100 {"NAND 128MiB 1,8V 8-bit", 0xA1, 0, 128, 0, LP_OPTIONS
},
101 {"NAND 128MiB 3,3V 8-bit", 0xF1, 0, 128, 0, LP_OPTIONS
},
102 {"NAND 128MiB 1,8V 16-bit", 0xB1, 0, 128, 0, LP_OPTIONS16
},
103 {"NAND 128MiB 3,3V 16-bit", 0xC1, 0, 128, 0, LP_OPTIONS16
},
105 {"NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_OPTIONS
},
106 {"NAND 256MiB 3,3V 8-bit", 0xDA, 0, 256, 0, LP_OPTIONS
},
107 {"NAND 256MiB 1,8V 16-bit", 0xBA, 0, 256, 0, LP_OPTIONS16
},
108 {"NAND 256MiB 3,3V 16-bit", 0xCA, 0, 256, 0, LP_OPTIONS16
},
110 {"NAND 512MiB 1,8V 8-bit", 0xAC, 0, 512, 0, LP_OPTIONS
},
111 {"NAND 512MiB 3,3V 8-bit", 0xDC, 0, 512, 0, LP_OPTIONS
},
112 {"NAND 512MiB 1,8V 16-bit", 0xBC, 0, 512, 0, LP_OPTIONS16
},
113 {"NAND 512MiB 3,3V 16-bit", 0xCC, 0, 512, 0, LP_OPTIONS16
},
115 {"NAND 1GiB 1,8V 8-bit", 0xA3, 0, 1024, 0, LP_OPTIONS
},
116 {"NAND 1GiB 3,3V 8-bit", 0xD3, 0, 1024, 0, LP_OPTIONS
},
117 {"NAND 1GiB 1,8V 16-bit", 0xB3, 0, 1024, 0, LP_OPTIONS16
},
118 {"NAND 1GiB 3,3V 16-bit", 0xC3, 0, 1024, 0, LP_OPTIONS16
},
120 {"NAND 2GiB 1,8V 8-bit", 0xA5, 0, 2048, 0, LP_OPTIONS
},
121 {"NAND 2GiB 3,3V 8-bit", 0xD5, 0, 2048, 0, LP_OPTIONS
},
122 {"NAND 2GiB 1,8V 16-bit", 0xB5, 0, 2048, 0, LP_OPTIONS16
},
123 {"NAND 2GiB 3,3V 16-bit", 0xC5, 0, 2048, 0, LP_OPTIONS16
},
125 {NULL
, 0, 0, 0, 0, 0 }
128 /* Manufacturer ID list
130 static struct nand_manufacturer nand_manuf_ids
[] =
133 {NAND_MFR_TOSHIBA
, "Toshiba"},
134 {NAND_MFR_SAMSUNG
, "Samsung"},
135 {NAND_MFR_FUJITSU
, "Fujitsu"},
136 {NAND_MFR_NATIONAL
, "National"},
137 {NAND_MFR_RENESAS
, "Renesas"},
138 {NAND_MFR_STMICRO
, "ST Micro"},
139 {NAND_MFR_HYNIX
, "Hynix"},
140 {NAND_MFR_MICRON
, "Micron"},
145 * Define default oob placement schemes for large and small page devices
149 static struct nand_ecclayout nand_oob_8
= {
160 static struct nand_ecclayout nand_oob_16
= {
162 .eccpos
= {0, 1, 2, 3, 6, 7},
168 static struct nand_ecclayout nand_oob_64
= {
171 40, 41, 42, 43, 44, 45, 46, 47,
172 48, 49, 50, 51, 52, 53, 54, 55,
173 56, 57, 58, 59, 60, 61, 62, 63},
179 int nand_list_walker(struct nand_flash_controller
*c
, void *x
)
181 struct command_context
*cmd_ctx
= (struct command_context
*)x
;
182 command_print(cmd_ctx
, " %s", c
->name
);
185 COMMAND_HANDLER(handle_nand_list_drivers
)
187 command_print(CMD_CTX
, "Available NAND flash controller drivers:");
188 return nand_driver_walk(&nand_list_walker
, CMD_CTX
);
191 static COMMAND_HELPER(create_nand_device
, const char *bank_name
,
192 struct nand_flash_controller
*controller
)
194 if (NULL
!= controller
->commands
)
196 int retval
= register_commands(CMD_CTX
, NULL
,
197 controller
->commands
);
198 if (ERROR_OK
!= retval
)
201 struct nand_device
*c
= malloc(sizeof(struct nand_device
));
203 c
->name
= strdup(bank_name
);
204 c
->controller
= controller
;
205 c
->controller_priv
= NULL
;
206 c
->manufacturer
= NULL
;
209 c
->address_cycles
= 0;
214 int retval
= CALL_COMMAND_HANDLER(controller
->nand_device_command
, c
);
215 if (ERROR_OK
!= retval
)
217 LOG_ERROR("'%s' driver rejected nand flash", controller
->name
);
223 struct nand_device
*p
= nand_devices
;
224 while (p
&& p
->next
) p
= p
->next
;
232 COMMAND_HANDLER(handle_nand_device_command
)
236 LOG_ERROR("incomplete nand device configuration");
237 return ERROR_FLASH_BANK_INVALID
;
240 // save name and increment (for compatibility) with drivers
241 const char *bank_name
= *CMD_ARGV
++;
244 const char *driver_name
= CMD_ARGV
[0];
245 struct nand_flash_controller
*controller
;
246 controller
= nand_driver_find_by_name(CMD_ARGV
[0]);
247 if (NULL
== controller
)
249 LOG_ERROR("No valid NAND flash driver found (%s)", driver_name
);
250 return CALL_COMMAND_HANDLER(handle_nand_list_drivers
);
252 return CALL_COMMAND_HANDLER(create_nand_device
, bank_name
, controller
);
256 COMMAND_HANDLER(handle_nand_init_command
);
258 static const struct command_registration nand_config_command_handlers
[] = {
261 .handler
= &handle_nand_device_command
,
262 .mode
= COMMAND_CONFIG
,
263 .help
= "defines a new NAND bank",
267 .handler
= &handle_nand_list_drivers
,
269 .help
= "lists available NAND drivers",
273 .mode
= COMMAND_CONFIG
,
274 .handler
= &handle_nand_init_command
,
275 .help
= "initialize NAND devices",
277 COMMAND_REGISTRATION_DONE
279 static const struct command_registration nand_command_handlers
[] = {
283 .help
= "NAND flash command group",
284 .chain
= nand_config_command_handlers
,
286 COMMAND_REGISTRATION_DONE
289 int nand_register_commands(struct command_context
*cmd_ctx
)
291 return register_commands(cmd_ctx
, NULL
, nand_command_handlers
);
294 struct nand_device
*get_nand_device_by_name(const char *name
)
296 unsigned requested
= get_flash_name_index(name
);
299 struct nand_device
*nand
;
300 for (nand
= nand_devices
; NULL
!= nand
; nand
= nand
->next
)
302 if (strcmp(nand
->name
, name
) == 0)
304 if (!flash_driver_name_matches(nand
->controller
->name
, name
))
306 if (++found
< requested
)
313 struct nand_device
*get_nand_device_by_num(int num
)
315 struct nand_device
*p
;
318 for (p
= nand_devices
; p
; p
= p
->next
)
329 COMMAND_HELPER(nand_command_get_device
, unsigned name_index
,
330 struct nand_device
**nand
)
332 const char *str
= CMD_ARGV
[name_index
];
333 *nand
= get_nand_device_by_name(str
);
338 COMMAND_PARSE_NUMBER(uint
, str
, num
);
339 *nand
= get_nand_device_by_num(num
);
341 command_print(CMD_CTX
, "NAND flash device '%s' not found", str
);
342 return ERROR_INVALID_ARGUMENTS
;
347 static int nand_build_bbt(struct nand_device
*nand
, int first
, int last
)
353 if ((first
< 0) || (first
>= nand
->num_blocks
))
356 if ((last
>= nand
->num_blocks
) || (last
== -1))
357 last
= nand
->num_blocks
- 1;
359 for (i
= first
; i
< last
; i
++)
361 nand_read_page(nand
, page
, NULL
, 0, oob
, 6);
363 if (((nand
->device
->options
& NAND_BUSWIDTH_16
) && ((oob
[0] & oob
[1]) != 0xff))
364 || (((nand
->page_size
== 512) && (oob
[5] != 0xff)) ||
365 ((nand
->page_size
== 2048) && (oob
[0] != 0xff))))
367 LOG_WARNING("bad block: %i", i
);
368 nand
->blocks
[i
].is_bad
= 1;
372 nand
->blocks
[i
].is_bad
= 0;
375 page
+= (nand
->erase_size
/ nand
->page_size
);
381 int nand_read_status(struct nand_device
*nand
, uint8_t *status
)
384 return ERROR_NAND_DEVICE_NOT_PROBED
;
386 /* Send read status command */
387 nand
->controller
->command(nand
, NAND_CMD_STATUS
);
392 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
395 nand
->controller
->read_data(nand
, &data
);
396 *status
= data
& 0xff;
400 nand
->controller
->read_data(nand
, status
);
406 static int nand_poll_ready(struct nand_device
*nand
, int timeout
)
410 nand
->controller
->command(nand
, NAND_CMD_STATUS
);
412 if (nand
->device
->options
& NAND_BUSWIDTH_16
) {
414 nand
->controller
->read_data(nand
, &data
);
415 status
= data
& 0xff;
417 nand
->controller
->read_data(nand
, &status
);
419 if (status
& NAND_STATUS_READY
)
424 return (status
& NAND_STATUS_READY
) != 0;
427 int nand_probe(struct nand_device
*nand
)
429 uint8_t manufacturer_id
, device_id
;
434 /* clear device data */
436 nand
->manufacturer
= NULL
;
438 /* clear device parameters */
440 nand
->address_cycles
= 0;
442 nand
->erase_size
= 0;
444 /* initialize controller (device parameters are zero, use controller default) */
445 if ((retval
= nand
->controller
->init(nand
) != ERROR_OK
))
449 case ERROR_NAND_OPERATION_FAILED
:
450 LOG_DEBUG("controller initialization failed");
451 return ERROR_NAND_OPERATION_FAILED
;
452 case ERROR_NAND_OPERATION_NOT_SUPPORTED
:
453 LOG_ERROR("BUG: controller reported that it doesn't support default parameters");
454 return ERROR_NAND_OPERATION_FAILED
;
456 LOG_ERROR("BUG: unknown controller initialization failure");
457 return ERROR_NAND_OPERATION_FAILED
;
461 nand
->controller
->command(nand
, NAND_CMD_RESET
);
462 nand
->controller
->reset(nand
);
464 nand
->controller
->command(nand
, NAND_CMD_READID
);
465 nand
->controller
->address(nand
, 0x0);
467 if (nand
->bus_width
== 8)
469 nand
->controller
->read_data(nand
, &manufacturer_id
);
470 nand
->controller
->read_data(nand
, &device_id
);
475 nand
->controller
->read_data(nand
, &data_buf
);
476 manufacturer_id
= data_buf
& 0xff;
477 nand
->controller
->read_data(nand
, &data_buf
);
478 device_id
= data_buf
& 0xff;
481 for (i
= 0; nand_flash_ids
[i
].name
; i
++)
483 if (nand_flash_ids
[i
].id
== device_id
)
485 nand
->device
= &nand_flash_ids
[i
];
490 for (i
= 0; nand_manuf_ids
[i
].name
; i
++)
492 if (nand_manuf_ids
[i
].id
== manufacturer_id
)
494 nand
->manufacturer
= &nand_manuf_ids
[i
];
499 if (!nand
->manufacturer
)
501 nand
->manufacturer
= &nand_manuf_ids
[0];
502 nand
->manufacturer
->id
= manufacturer_id
;
507 LOG_ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
508 manufacturer_id
, device_id
);
509 return ERROR_NAND_OPERATION_FAILED
;
512 LOG_DEBUG("found %s (%s)", nand
->device
->name
, nand
->manufacturer
->name
);
514 /* initialize device parameters */
517 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
518 nand
->bus_width
= 16;
522 /* Do we need extended device probe information? */
523 if (nand
->device
->page_size
== 0 ||
524 nand
->device
->erase_size
== 0)
526 if (nand
->bus_width
== 8)
528 nand
->controller
->read_data(nand
, id_buff
+ 3);
529 nand
->controller
->read_data(nand
, id_buff
+ 4);
530 nand
->controller
->read_data(nand
, id_buff
+ 5);
536 nand
->controller
->read_data(nand
, &data_buf
);
537 id_buff
[3] = data_buf
;
539 nand
->controller
->read_data(nand
, &data_buf
);
540 id_buff
[4] = data_buf
;
542 nand
->controller
->read_data(nand
, &data_buf
);
543 id_buff
[5] = data_buf
>> 8;
548 if (nand
->device
->page_size
== 0)
550 nand
->page_size
= 1 << (10 + (id_buff
[4] & 3));
552 else if (nand
->device
->page_size
== 256)
554 LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
555 return ERROR_NAND_OPERATION_FAILED
;
559 nand
->page_size
= nand
->device
->page_size
;
562 /* number of address cycles */
563 if (nand
->page_size
<= 512)
565 /* small page devices */
566 if (nand
->device
->chip_size
<= 32)
567 nand
->address_cycles
= 3;
568 else if (nand
->device
->chip_size
<= 8*1024)
569 nand
->address_cycles
= 4;
572 LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
573 nand
->address_cycles
= 5;
578 /* large page devices */
579 if (nand
->device
->chip_size
<= 128)
580 nand
->address_cycles
= 4;
581 else if (nand
->device
->chip_size
<= 32*1024)
582 nand
->address_cycles
= 5;
585 LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
586 nand
->address_cycles
= 6;
591 if (nand
->device
->erase_size
== 0)
593 switch ((id_buff
[4] >> 4) & 3) {
595 nand
->erase_size
= 64 << 10;
598 nand
->erase_size
= 128 << 10;
601 nand
->erase_size
= 256 << 10;
604 nand
->erase_size
=512 << 10;
610 nand
->erase_size
= nand
->device
->erase_size
;
613 /* initialize controller, but leave parameters at the controllers default */
614 if ((retval
= nand
->controller
->init(nand
) != ERROR_OK
))
618 case ERROR_NAND_OPERATION_FAILED
:
619 LOG_DEBUG("controller initialization failed");
620 return ERROR_NAND_OPERATION_FAILED
;
621 case ERROR_NAND_OPERATION_NOT_SUPPORTED
:
622 LOG_ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
623 nand
->bus_width
, nand
->address_cycles
, nand
->page_size
);
624 return ERROR_NAND_OPERATION_FAILED
;
626 LOG_ERROR("BUG: unknown controller initialization failure");
627 return ERROR_NAND_OPERATION_FAILED
;
631 nand
->num_blocks
= (nand
->device
->chip_size
* 1024) / (nand
->erase_size
/ 1024);
632 nand
->blocks
= malloc(sizeof(struct nand_block
) * nand
->num_blocks
);
634 for (i
= 0; i
< nand
->num_blocks
; i
++)
636 nand
->blocks
[i
].size
= nand
->erase_size
;
637 nand
->blocks
[i
].offset
= i
* nand
->erase_size
;
638 nand
->blocks
[i
].is_erased
= -1;
639 nand
->blocks
[i
].is_bad
= -1;
645 static int nand_erase(struct nand_device
*nand
, int first_block
, int last_block
)
653 return ERROR_NAND_DEVICE_NOT_PROBED
;
655 if ((first_block
< 0) || (last_block
> nand
->num_blocks
))
656 return ERROR_INVALID_ARGUMENTS
;
658 /* make sure we know if a block is bad before erasing it */
659 for (i
= first_block
; i
<= last_block
; i
++)
661 if (nand
->blocks
[i
].is_bad
== -1)
663 nand_build_bbt(nand
, i
, last_block
);
668 for (i
= first_block
; i
<= last_block
; i
++)
670 /* Send erase setup command */
671 nand
->controller
->command(nand
, NAND_CMD_ERASE1
);
673 page
= i
* (nand
->erase_size
/ nand
->page_size
);
675 /* Send page address */
676 if (nand
->page_size
<= 512)
679 nand
->controller
->address(nand
, page
& 0xff);
680 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
682 /* 3rd cycle only on devices with more than 32 MiB */
683 if (nand
->address_cycles
>= 4)
684 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
686 /* 4th cycle only on devices with more than 8 GiB */
687 if (nand
->address_cycles
>= 5)
688 nand
->controller
->address(nand
, (page
>> 24) & 0xff);
693 nand
->controller
->address(nand
, page
& 0xff);
694 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
696 /* 3rd cycle only on devices with more than 128 MiB */
697 if (nand
->address_cycles
>= 5)
698 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
701 /* Send erase confirm command */
702 nand
->controller
->command(nand
, NAND_CMD_ERASE2
);
704 retval
= nand
->controller
->nand_ready
?
705 nand
->controller
->nand_ready(nand
, 1000) :
706 nand_poll_ready(nand
, 1000);
708 LOG_ERROR("timeout waiting for NAND flash block erase to complete");
709 return ERROR_NAND_OPERATION_TIMEOUT
;
712 if ((retval
= nand_read_status(nand
, &status
)) != ERROR_OK
)
714 LOG_ERROR("couldn't read status");
715 return ERROR_NAND_OPERATION_FAILED
;
720 LOG_ERROR("didn't erase %sblock %d; status: 0x%2.2x",
721 (nand
->blocks
[i
].is_bad
== 1)
724 /* continue; other blocks might still be erasable */
727 nand
->blocks
[i
].is_erased
= 1;
734 static int nand_read_plain(struct nand_device
*nand
, uint32_t address
, uint8_t *data
, uint32_t data_size
)
739 return ERROR_NAND_DEVICE_NOT_PROBED
;
741 if (address
% nand
->page_size
)
743 LOG_ERROR("reads need to be page aligned");
744 return ERROR_NAND_OPERATION_FAILED
;
747 page
= malloc(nand
->page_size
);
749 while (data_size
> 0)
751 uint32_t thisrun_size
= (data_size
> nand
->page_size
) ? nand
->page_size
: data_size
;
752 uint32_t page_address
;
755 page_address
= address
/ nand
->page_size
;
757 nand_read_page(nand
, page_address
, page
, nand
->page_size
, NULL
, 0);
759 memcpy(data
, page
, thisrun_size
);
761 address
+= thisrun_size
;
762 data
+= thisrun_size
;
763 data_size
-= thisrun_size
;
771 static int nand_write_plain(struct nand_device
*nand
, uint32_t address
, uint8_t *data
, uint32_t data_size
)
776 return ERROR_NAND_DEVICE_NOT_PROBED
;
778 if (address
% nand
->page_size
)
780 LOG_ERROR("writes need to be page aligned");
781 return ERROR_NAND_OPERATION_FAILED
;
784 page
= malloc(nand
->page_size
);
786 while (data_size
> 0)
788 uint32_t thisrun_size
= (data_size
> nand
->page_size
) ? nand
->page_size
: data_size
;
789 uint32_t page_address
;
791 memset(page
, 0xff, nand
->page_size
);
792 memcpy(page
, data
, thisrun_size
);
794 page_address
= address
/ nand
->page_size
;
796 nand_write_page(nand
, page_address
, page
, nand
->page_size
, NULL
, 0);
798 address
+= thisrun_size
;
799 data
+= thisrun_size
;
800 data_size
-= thisrun_size
;
809 int nand_write_page(struct nand_device
*nand
, uint32_t page
, uint8_t *data
, uint32_t data_size
, uint8_t *oob
, uint32_t oob_size
)
814 return ERROR_NAND_DEVICE_NOT_PROBED
;
816 block
= page
/ (nand
->erase_size
/ nand
->page_size
);
817 if (nand
->blocks
[block
].is_erased
== 1)
818 nand
->blocks
[block
].is_erased
= 0;
820 if (nand
->use_raw
|| nand
->controller
->write_page
== NULL
)
821 return nand_write_page_raw(nand
, page
, data
, data_size
, oob
, oob_size
);
823 return nand
->controller
->write_page(nand
, page
, data
, data_size
, oob
, oob_size
);
826 static int nand_read_page(struct nand_device
*nand
, uint32_t page
, uint8_t *data
, uint32_t data_size
, uint8_t *oob
, uint32_t oob_size
)
829 return ERROR_NAND_DEVICE_NOT_PROBED
;
831 if (nand
->use_raw
|| nand
->controller
->read_page
== NULL
)
832 return nand_read_page_raw(nand
, page
, data
, data_size
, oob
, oob_size
);
834 return nand
->controller
->read_page(nand
, page
, data
, data_size
, oob
, oob_size
);
837 int nand_page_command(struct nand_device
*nand
, uint32_t page
,
838 uint8_t cmd
, bool oob_only
)
841 return ERROR_NAND_DEVICE_NOT_PROBED
;
843 if (oob_only
&& NAND_CMD_READ0
== cmd
&& nand
->page_size
<= 512)
844 cmd
= NAND_CMD_READOOB
;
846 nand
->controller
->command(nand
, cmd
);
848 if (nand
->page_size
<= 512) {
849 /* small page device */
851 /* column (always 0, we start at the beginning of a page/OOB area) */
852 nand
->controller
->address(nand
, 0x0);
855 nand
->controller
->address(nand
, page
& 0xff);
856 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
858 /* 4th cycle only on devices with more than 32 MiB */
859 if (nand
->address_cycles
>= 4)
860 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
862 /* 5th cycle only on devices with more than 8 GiB */
863 if (nand
->address_cycles
>= 5)
864 nand
->controller
->address(nand
, (page
>> 24) & 0xff);
866 /* large page device */
868 /* column (0 when we start at the beginning of a page,
869 * or 2048 for the beginning of OOB area)
871 nand
->controller
->address(nand
, 0x0);
873 nand
->controller
->address(nand
, 0x8);
875 nand
->controller
->address(nand
, 0x0);
878 nand
->controller
->address(nand
, page
& 0xff);
879 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
881 /* 5th cycle only on devices with more than 128 MiB */
882 if (nand
->address_cycles
>= 5)
883 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
885 /* large page devices need a start command if reading */
886 if (NAND_CMD_READ0
== cmd
)
887 nand
->controller
->command(nand
, NAND_CMD_READSTART
);
890 if (nand
->controller
->nand_ready
) {
891 if (!nand
->controller
->nand_ready(nand
, 100))
892 return ERROR_NAND_OPERATION_TIMEOUT
;
900 int nand_read_page_raw(struct nand_device
*nand
, uint32_t page
,
901 uint8_t *data
, uint32_t data_size
,
902 uint8_t *oob
, uint32_t oob_size
)
907 retval
= nand_page_command(nand
, page
, NAND_CMD_READ0
, !data
);
908 if (ERROR_OK
!= retval
)
913 if (nand
->controller
->read_block_data
!= NULL
)
914 (nand
->controller
->read_block_data
)(nand
, data
, data_size
);
917 for (i
= 0; i
< data_size
;)
919 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
921 nand
->controller
->read_data(nand
, data
);
927 nand
->controller
->read_data(nand
, data
);
937 if (nand
->controller
->read_block_data
!= NULL
)
938 (nand
->controller
->read_block_data
)(nand
, oob
, oob_size
);
941 for (i
= 0; i
< oob_size
;)
943 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
945 nand
->controller
->read_data(nand
, oob
);
951 nand
->controller
->read_data(nand
, oob
);
962 int nand_write_page_raw(struct nand_device
*nand
, uint32_t page
, uint8_t *data
, uint32_t data_size
, uint8_t *oob
, uint32_t oob_size
)
968 retval
= nand_page_command(nand
, page
, NAND_CMD_SEQIN
, !data
);
969 if (ERROR_OK
!= retval
)
974 if (nand
->controller
->write_block_data
!= NULL
)
975 (nand
->controller
->write_block_data
)(nand
, data
, data_size
);
978 for (i
= 0; i
< data_size
;)
980 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
982 uint16_t data_buf
= le_to_h_u16(data
);
983 nand
->controller
->write_data(nand
, data_buf
);
989 nand
->controller
->write_data(nand
, *data
);
999 if (nand
->controller
->write_block_data
!= NULL
)
1000 (nand
->controller
->write_block_data
)(nand
, oob
, oob_size
);
1003 for (i
= 0; i
< oob_size
;)
1005 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
1007 uint16_t oob_buf
= le_to_h_u16(data
);
1008 nand
->controller
->write_data(nand
, oob_buf
);
1014 nand
->controller
->write_data(nand
, *oob
);
1022 nand
->controller
->command(nand
, NAND_CMD_PAGEPROG
);
1024 retval
= nand
->controller
->nand_ready
?
1025 nand
->controller
->nand_ready(nand
, 100) :
1026 nand_poll_ready(nand
, 100);
1028 return ERROR_NAND_OPERATION_TIMEOUT
;
1030 if ((retval
= nand_read_status(nand
, &status
)) != ERROR_OK
)
1032 LOG_ERROR("couldn't read status");
1033 return ERROR_NAND_OPERATION_FAILED
;
1036 if (status
& NAND_STATUS_FAIL
)
1038 LOG_ERROR("write operation didn't pass, status: 0x%2.2x", status
);
1039 return ERROR_NAND_OPERATION_FAILED
;
1045 COMMAND_HANDLER(handle_nand_list_command
)
1047 struct nand_device
*p
;
1052 command_print(CMD_CTX
, "no NAND flash devices configured");
1056 for (p
= nand_devices
, i
= 0; p
; p
= p
->next
, i
++)
1059 command_print(CMD_CTX
, "#%i: %s (%s) "
1060 "pagesize: %i, buswidth: %i,\n\t"
1061 "blocksize: %i, blocks: %i",
1062 i
, p
->device
->name
, p
->manufacturer
->name
,
1063 p
->page_size
, p
->bus_width
,
1064 p
->erase_size
, p
->num_blocks
);
1066 command_print(CMD_CTX
, "#%i: not probed", i
);
1072 COMMAND_HANDLER(handle_nand_info_command
)
1081 return ERROR_COMMAND_SYNTAX_ERROR
;
1087 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], i
);
1092 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[1], first
);
1093 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[2], last
);
1097 struct nand_device
*p
;
1098 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &p
);
1099 if (ERROR_OK
!= retval
)
1102 if (NULL
== p
->device
)
1104 command_print(CMD_CTX
, "#%s: not probed", CMD_ARGV
[0]);
1108 if (first
>= p
->num_blocks
)
1109 first
= p
->num_blocks
- 1;
1111 if (last
>= p
->num_blocks
)
1112 last
= p
->num_blocks
- 1;
1114 command_print(CMD_CTX
, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
1115 i
++, p
->device
->name
, p
->manufacturer
->name
, p
->page_size
, p
->bus_width
, p
->erase_size
);
1117 for (j
= first
; j
<= last
; j
++)
1119 char *erase_state
, *bad_state
;
1121 if (p
->blocks
[j
].is_erased
== 0)
1122 erase_state
= "not erased";
1123 else if (p
->blocks
[j
].is_erased
== 1)
1124 erase_state
= "erased";
1126 erase_state
= "erase state unknown";
1128 if (p
->blocks
[j
].is_bad
== 0)
1130 else if (p
->blocks
[j
].is_bad
== 1)
1131 bad_state
= " (marked bad)";
1133 bad_state
= " (block condition unknown)";
1135 command_print(CMD_CTX
,
1136 "\t#%i: 0x%8.8" PRIx32
" (%" PRId32
"kB) %s%s",
1138 p
->blocks
[j
].offset
,
1139 p
->blocks
[j
].size
/ 1024,
1147 COMMAND_HANDLER(handle_nand_probe_command
)
1151 return ERROR_COMMAND_SYNTAX_ERROR
;
1154 struct nand_device
*p
;
1155 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &p
);
1156 if (ERROR_OK
!= retval
)
1159 if ((retval
= nand_probe(p
)) == ERROR_OK
)
1161 command_print(CMD_CTX
, "NAND flash device '%s' found", p
->device
->name
);
1163 else if (retval
== ERROR_NAND_OPERATION_FAILED
)
1165 command_print(CMD_CTX
, "probing failed for NAND flash device");
1169 command_print(CMD_CTX
, "unknown error when probing NAND flash device");
1175 COMMAND_HANDLER(handle_nand_erase_command
)
1177 if (CMD_ARGC
!= 1 && CMD_ARGC
!= 3)
1179 return ERROR_COMMAND_SYNTAX_ERROR
;
1183 struct nand_device
*p
;
1184 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &p
);
1185 if (ERROR_OK
!= retval
)
1188 unsigned long offset
;
1189 unsigned long length
;
1191 /* erase specified part of the chip; or else everything */
1192 if (CMD_ARGC
== 3) {
1193 unsigned long size
= p
->erase_size
* p
->num_blocks
;
1195 COMMAND_PARSE_NUMBER(ulong
, CMD_ARGV
[1], offset
);
1196 if ((offset
% p
->erase_size
) != 0 || offset
>= size
)
1197 return ERROR_INVALID_ARGUMENTS
;
1199 COMMAND_PARSE_NUMBER(ulong
, CMD_ARGV
[2], length
);
1200 if ((length
== 0) || (length
% p
->erase_size
) != 0
1201 || (length
+ offset
) > size
)
1202 return ERROR_INVALID_ARGUMENTS
;
1204 offset
/= p
->erase_size
;
1205 length
/= p
->erase_size
;
1208 length
= p
->num_blocks
;
1211 retval
= nand_erase(p
, offset
, offset
+ length
- 1);
1212 if (retval
== ERROR_OK
)
1214 command_print(CMD_CTX
, "erased blocks %lu to %lu "
1215 "on NAND flash device #%s '%s'",
1216 offset
, offset
+ length
,
1217 CMD_ARGV
[0], p
->device
->name
);
1219 else if (retval
== ERROR_NAND_OPERATION_FAILED
)
1221 command_print(CMD_CTX
, "erase failed");
1225 command_print(CMD_CTX
, "unknown error when erasing NAND flash device");
1231 COMMAND_HANDLER(handle_nand_check_bad_blocks_command
)
1236 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 3) || (CMD_ARGC
== 2))
1238 return ERROR_COMMAND_SYNTAX_ERROR
;
1242 struct nand_device
*p
;
1243 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &p
);
1244 if (ERROR_OK
!= retval
)
1249 unsigned long offset
;
1250 unsigned long length
;
1252 COMMAND_PARSE_NUMBER(ulong
, CMD_ARGV
[1], offset
);
1253 if (offset
% p
->erase_size
)
1254 return ERROR_INVALID_ARGUMENTS
;
1255 offset
/= p
->erase_size
;
1257 COMMAND_PARSE_NUMBER(ulong
, CMD_ARGV
[2], length
);
1258 if (length
% p
->erase_size
)
1259 return ERROR_INVALID_ARGUMENTS
;
1262 length
/= p
->erase_size
;
1265 last
= offset
+ length
;
1268 retval
= nand_build_bbt(p
, first
, last
);
1269 if (retval
== ERROR_OK
)
1271 command_print(CMD_CTX
, "checked NAND flash device for bad blocks, "
1272 "use \"nand info\" command to list blocks");
1274 else if (retval
== ERROR_NAND_OPERATION_FAILED
)
1276 command_print(CMD_CTX
, "error when checking for bad blocks on "
1277 "NAND flash device");
1281 command_print(CMD_CTX
, "unknown error when checking for bad "
1282 "blocks on NAND flash device");
1288 struct nand_fileio_state
{
1295 enum oob_formats oob_format
;
1302 struct fileio fileio
;
1304 struct duration bench
;
1307 static void nand_fileio_init(struct nand_fileio_state
*state
)
1309 memset(state
, 0, sizeof(*state
));
1310 state
->oob_format
= NAND_OOB_NONE
;
1313 static int nand_fileio_start(struct command_context
*cmd_ctx
,
1314 struct nand_device
*nand
, const char *filename
, int filemode
,
1315 struct nand_fileio_state
*state
)
1317 if (state
->address
% nand
->page_size
)
1319 command_print(cmd_ctx
, "only page-aligned addresses are supported");
1320 return ERROR_COMMAND_SYNTAX_ERROR
;
1323 duration_start(&state
->bench
);
1325 if (NULL
!= filename
)
1327 int retval
= fileio_open(&state
->fileio
, filename
, filemode
, FILEIO_BINARY
);
1328 if (ERROR_OK
!= retval
)
1330 const char *msg
= (FILEIO_READ
== filemode
) ? "read" : "write";
1331 command_print(cmd_ctx
, "failed to open '%s' for %s access",
1335 state
->file_opened
= true;
1338 if (!(state
->oob_format
& NAND_OOB_ONLY
))
1340 state
->page_size
= nand
->page_size
;
1341 state
->page
= malloc(nand
->page_size
);
1344 if (state
->oob_format
& (NAND_OOB_RAW
| NAND_OOB_SW_ECC
| NAND_OOB_SW_ECC_KW
))
1346 if (nand
->page_size
== 512)
1348 state
->oob_size
= 16;
1349 state
->eccpos
= nand_oob_16
.eccpos
;
1351 else if (nand
->page_size
== 2048)
1353 state
->oob_size
= 64;
1354 state
->eccpos
= nand_oob_64
.eccpos
;
1356 state
->oob
= malloc(state
->oob_size
);
1361 static int nand_fileio_cleanup(struct nand_fileio_state
*state
)
1363 if (state
->file_opened
)
1364 fileio_close(&state
->fileio
);
1378 static int nand_fileio_finish(struct nand_fileio_state
*state
)
1380 nand_fileio_cleanup(state
);
1381 return duration_measure(&state
->bench
);
1384 static COMMAND_HELPER(nand_fileio_parse_args
, struct nand_fileio_state
*state
,
1385 struct nand_device
**dev
, enum fileio_access filemode
,
1386 bool need_size
, bool sw_ecc
)
1388 nand_fileio_init(state
);
1390 unsigned minargs
= need_size
? 4 : 3;
1391 if (CMD_ARGC
< minargs
)
1392 return ERROR_COMMAND_SYNTAX_ERROR
;
1394 struct nand_device
*nand
;
1395 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &nand
);
1396 if (ERROR_OK
!= retval
)
1399 if (NULL
== nand
->device
)
1401 command_print(CMD_CTX
, "#%s: not probed", CMD_ARGV
[0]);
1405 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], state
->address
);
1408 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], state
->size
);
1409 if (state
->size
% nand
->page_size
)
1411 command_print(CMD_CTX
, "only page-aligned sizes are supported");
1412 return ERROR_COMMAND_SYNTAX_ERROR
;
1416 if (CMD_ARGC
> minargs
)
1418 for (unsigned i
= minargs
; i
< CMD_ARGC
; i
++)
1420 if (!strcmp(CMD_ARGV
[i
], "oob_raw"))
1421 state
->oob_format
|= NAND_OOB_RAW
;
1422 else if (!strcmp(CMD_ARGV
[i
], "oob_only"))
1423 state
->oob_format
|= NAND_OOB_RAW
| NAND_OOB_ONLY
;
1424 else if (sw_ecc
&& !strcmp(CMD_ARGV
[i
], "oob_softecc"))
1425 state
->oob_format
|= NAND_OOB_SW_ECC
;
1426 else if (sw_ecc
&& !strcmp(CMD_ARGV
[i
], "oob_softecc_kw"))
1427 state
->oob_format
|= NAND_OOB_SW_ECC_KW
;
1430 command_print(CMD_CTX
, "unknown option: %s", CMD_ARGV
[i
]);
1431 return ERROR_COMMAND_SYNTAX_ERROR
;
1436 retval
= nand_fileio_start(CMD_CTX
, nand
, CMD_ARGV
[1], filemode
, state
);
1437 if (ERROR_OK
!= retval
)
1441 state
->size
= state
->fileio
.size
;
1449 * @returns If no error occurred, returns number of bytes consumed;
1450 * otherwise, returns a negative error code.)
1452 static int nand_fileio_read(struct nand_device
*nand
,
1453 struct nand_fileio_state
*s
)
1455 size_t total_read
= 0;
1458 if (NULL
!= s
->page
)
1460 fileio_read(&s
->fileio
, s
->page_size
, s
->page
, &one_read
);
1461 if (one_read
< s
->page_size
)
1462 memset(s
->page
+ one_read
, 0xff, s
->page_size
- one_read
);
1463 total_read
+= one_read
;
1466 if (s
->oob_format
& NAND_OOB_SW_ECC
)
1469 memset(s
->oob
, 0xff, s
->oob_size
);
1470 for (uint32_t i
= 0, j
= 0; i
< s
->page_size
; i
+= 256)
1472 nand_calculate_ecc(nand
, s
->page
+ i
, ecc
);
1473 s
->oob
[s
->eccpos
[j
++]] = ecc
[0];
1474 s
->oob
[s
->eccpos
[j
++]] = ecc
[1];
1475 s
->oob
[s
->eccpos
[j
++]] = ecc
[2];
1478 else if (s
->oob_format
& NAND_OOB_SW_ECC_KW
)
1481 * In this case eccpos is not used as
1482 * the ECC data is always stored contigously
1483 * at the end of the OOB area. It consists
1484 * of 10 bytes per 512-byte data block.
1486 uint8_t *ecc
= s
->oob
+ s
->oob_size
- s
->page_size
/ 512 * 10;
1487 memset(s
->oob
, 0xff, s
->oob_size
);
1488 for (uint32_t i
= 0; i
< s
->page_size
; i
+= 512)
1490 nand_calculate_ecc_kw(nand
, s
->page
+ i
, ecc
);
1494 else if (NULL
!= s
->oob
)
1496 fileio_read(&s
->fileio
, s
->oob_size
, s
->oob
, &one_read
);
1497 if (one_read
< s
->oob_size
)
1498 memset(s
->oob
+ one_read
, 0xff, s
->oob_size
- one_read
);
1499 total_read
+= one_read
;
1504 COMMAND_HANDLER(handle_nand_write_command
)
1506 struct nand_device
*nand
= NULL
;
1507 struct nand_fileio_state s
;
1508 int retval
= CALL_COMMAND_HANDLER(nand_fileio_parse_args
,
1509 &s
, &nand
, FILEIO_READ
, false, true);
1510 if (ERROR_OK
!= retval
)
1513 uint32_t total_bytes
= s
.size
;
1516 int bytes_read
= nand_fileio_read(nand
, &s
);
1517 if (bytes_read
<= 0)
1519 command_print(CMD_CTX
, "error while reading file");
1520 return nand_fileio_cleanup(&s
);
1522 s
.size
-= bytes_read
;
1524 retval
= nand_write_page(nand
, s
.address
/ nand
->page_size
,
1525 s
.page
, s
.page_size
, s
.oob
, s
.oob_size
);
1526 if (ERROR_OK
!= retval
)
1528 command_print(CMD_CTX
, "failed writing file %s "
1529 "to NAND flash %s at offset 0x%8.8" PRIx32
,
1530 CMD_ARGV
[1], CMD_ARGV
[0], s
.address
);
1531 return nand_fileio_cleanup(&s
);
1533 s
.address
+= s
.page_size
;
1536 if (nand_fileio_finish(&s
))
1538 command_print(CMD_CTX
, "wrote file %s to NAND flash %s up to "
1539 "offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
1540 CMD_ARGV
[1], CMD_ARGV
[0], s
.address
, duration_elapsed(&s
.bench
),
1541 duration_kbps(&s
.bench
, total_bytes
));
1546 COMMAND_HANDLER(handle_nand_verify_command
)
1548 struct nand_device
*nand
= NULL
;
1549 struct nand_fileio_state file
;
1550 int retval
= CALL_COMMAND_HANDLER(nand_fileio_parse_args
,
1551 &file
, &nand
, FILEIO_READ
, false, true);
1552 if (ERROR_OK
!= retval
)
1555 struct nand_fileio_state dev
;
1556 nand_fileio_init(&dev
);
1557 dev
.address
= file
.address
;
1558 dev
.size
= file
.size
;
1559 dev
.oob_format
= file
.oob_format
;
1560 retval
= nand_fileio_start(CMD_CTX
, nand
, NULL
, FILEIO_NONE
, &dev
);
1561 if (ERROR_OK
!= retval
)
1564 while (file
.size
> 0)
1566 int retval
= nand_read_page(nand
, dev
.address
/ dev
.page_size
,
1567 dev
.page
, dev
.page_size
, dev
.oob
, dev
.oob_size
);
1568 if (ERROR_OK
!= retval
)
1570 command_print(CMD_CTX
, "reading NAND flash page failed");
1571 nand_fileio_cleanup(&dev
);
1572 return nand_fileio_cleanup(&file
);
1575 int bytes_read
= nand_fileio_read(nand
, &file
);
1576 if (bytes_read
<= 0)
1578 command_print(CMD_CTX
, "error while reading file");
1579 nand_fileio_cleanup(&dev
);
1580 return nand_fileio_cleanup(&file
);
1583 if ((dev
.page
&& memcmp(dev
.page
, file
.page
, dev
.page_size
)) ||
1584 (dev
.oob
&& memcmp(dev
.oob
, file
.oob
, dev
.oob_size
)) )
1586 command_print(CMD_CTX
, "NAND flash contents differ "
1587 "at 0x%8.8" PRIx32
, dev
.address
);
1588 nand_fileio_cleanup(&dev
);
1589 return nand_fileio_cleanup(&file
);
1592 file
.size
-= bytes_read
;
1593 dev
.address
+= nand
->page_size
;
1596 if (nand_fileio_finish(&file
) == ERROR_OK
)
1598 command_print(CMD_CTX
, "verified file %s in NAND flash %s "
1599 "up to offset 0x%8.8" PRIx32
" in %fs (%0.3f kb/s)",
1600 CMD_ARGV
[1], CMD_ARGV
[0], dev
.address
, duration_elapsed(&file
.bench
),
1601 duration_kbps(&file
.bench
, dev
.size
));
1604 return nand_fileio_cleanup(&dev
);
1607 COMMAND_HANDLER(handle_nand_dump_command
)
1609 struct nand_device
*nand
= NULL
;
1610 struct nand_fileio_state s
;
1611 int retval
= CALL_COMMAND_HANDLER(nand_fileio_parse_args
,
1612 &s
, &nand
, FILEIO_WRITE
, true, false);
1613 if (ERROR_OK
!= retval
)
1618 size_t size_written
;
1619 int retval
= nand_read_page(nand
, s
.address
/ nand
->page_size
,
1620 s
.page
, s
.page_size
, s
.oob
, s
.oob_size
);
1621 if (ERROR_OK
!= retval
)
1623 command_print(CMD_CTX
, "reading NAND flash page failed");
1624 return nand_fileio_cleanup(&s
);
1628 fileio_write(&s
.fileio
, s
.page_size
, s
.page
, &size_written
);
1631 fileio_write(&s
.fileio
, s
.oob_size
, s
.oob
, &size_written
);
1633 s
.size
-= nand
->page_size
;
1634 s
.address
+= nand
->page_size
;
1637 if (nand_fileio_finish(&s
) == ERROR_OK
)
1639 command_print(CMD_CTX
, "dumped %zu bytes in %fs (%0.3f kb/s)",
1640 s
.fileio
.size
, duration_elapsed(&s
.bench
),
1641 duration_kbps(&s
.bench
, s
.fileio
.size
));
1646 COMMAND_HANDLER(handle_nand_raw_access_command
)
1648 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
1650 return ERROR_COMMAND_SYNTAX_ERROR
;
1653 struct nand_device
*p
;
1654 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &p
);
1655 if (ERROR_OK
!= retval
)
1658 if (NULL
== p
->device
)
1660 command_print(CMD_CTX
, "#%s: not probed", CMD_ARGV
[0]);
1665 COMMAND_PARSE_ENABLE(CMD_ARGV
[1], p
->use_raw
);
1667 const char *msg
= p
->use_raw
? "enabled" : "disabled";
1668 command_print(CMD_CTX
, "raw access is %s", msg
);
1673 static const struct command_registration nand_exec_command_handlers
[] = {
1676 .handler
= &handle_nand_list_command
,
1677 .mode
= COMMAND_EXEC
,
1678 .help
= "list configured NAND flash devices",
1682 .handler
= &handle_nand_info_command
,
1683 .mode
= COMMAND_EXEC
,
1685 .help
= "print info about a NAND flash device",
1689 .handler
= &handle_nand_probe_command
,
1690 .mode
= COMMAND_EXEC
,
1692 .help
= "identify NAND flash device <num>",
1696 .name
= "check_bad_blocks",
1697 .handler
= &handle_nand_check_bad_blocks_command
,
1698 .mode
= COMMAND_EXEC
,
1699 .usage
= "<bank> [<offset> <length>]",
1700 .help
= "check NAND flash device <num> for bad blocks",
1704 .handler
= &handle_nand_erase_command
,
1705 .mode
= COMMAND_EXEC
,
1706 .usage
= "<bank> [<offset> <length>]",
1707 .help
= "erase blocks on NAND flash device",
1711 .handler
= &handle_nand_dump_command
,
1712 .mode
= COMMAND_EXEC
,
1713 .usage
= "<bank> <filename> <offset> <length> "
1714 "[oob_raw | oob_only]",
1715 .help
= "dump from NAND flash device",
1719 .handler
= &handle_nand_verify_command
,
1720 .mode
= COMMAND_EXEC
,
1721 .usage
= "<bank> <filename> <offset> "
1722 "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]",
1723 .help
= "verify NAND flash device",
1727 .handler
= &handle_nand_write_command
,
1728 .mode
= COMMAND_EXEC
,
1729 .usage
= "<bank> <filename> <offset> "
1730 "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]",
1731 .help
= "write to NAND flash device",
1734 .name
= "raw_access",
1735 .handler
= &handle_nand_raw_access_command
,
1736 .mode
= COMMAND_EXEC
,
1737 .usage
= "<num> ['enable'|'disable']",
1738 .help
= "raw access to NAND flash device",
1740 COMMAND_REGISTRATION_DONE
1743 int nand_init(struct command_context
*cmd_ctx
)
1747 struct command
*parent
= command_find_in_context(cmd_ctx
, "nand");
1748 return register_commands(cmd_ctx
, parent
, nand_exec_command_handlers
);
1751 COMMAND_HANDLER(handle_nand_init_command
)
1754 return ERROR_COMMAND_SYNTAX_ERROR
;
1756 static bool nand_initialized
= false;
1757 if (nand_initialized
)
1759 LOG_INFO("'nand init' has already been called");
1762 nand_initialized
= true;
1764 LOG_DEBUG("Initializing NAND devices...");
1765 return nand_init(CMD_CTX
);