1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
5 * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
6 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
8 * Partially based on drivers/mtd/nand_ids.c from Linux. *
9 ***************************************************************************/
17 /* configured NAND devices and NAND Flash command handler */
18 struct nand_device
*nand_devices
;
20 void nand_device_add(struct nand_device
*c
)
23 struct nand_device
*p
= nand_devices
;
34 * Manufacturer, ID code, pagesize, chipsize in MegaByte, eraseblock size,
37 * Pagesize; 0, 256, 512
38 * 0 get this information from the extended chip ID
39 * 256 256 Byte page size
40 * 512 512 Byte page size
42 static struct nand_info nand_flash_ids
[] = {
43 /* Vendor Specific Entries */
44 { NAND_MFR_SAMSUNG
, 0xD5, 8192, 2048, 0x100000, LP_OPTIONS
,
45 "K9GAG08 2GB NAND 3.3V x8 MLC 2b/cell"},
46 { NAND_MFR_SAMSUNG
, 0xD7, 8192, 4096, 0x100000, LP_OPTIONS
,
47 "K9LBG08 4GB NAND 3.3V x8 MLC 2b/cell"},
49 /* start "museum" IDs */
50 { 0x0, 0x6e, 256, 1, 0x1000, 0, "NAND 1MiB 5V 8-bit"},
51 { 0x0, 0x64, 256, 2, 0x1000, 0, "NAND 2MiB 5V 8-bit"},
52 { 0x0, 0x6b, 512, 4, 0x2000, 0, "NAND 4MiB 5V 8-bit"},
53 { 0x0, 0xe8, 256, 1, 0x1000, 0, "NAND 1MiB 3.3V 8-bit"},
54 { 0x0, 0xec, 256, 1, 0x1000, 0, "NAND 1MiB 3.3V 8-bit"},
55 { 0x0, 0xea, 256, 2, 0x1000, 0, "NAND 2MiB 3.3V 8-bit"},
56 { 0x0, 0xd5, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
57 { 0x0, 0xe3, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
58 { 0x0, 0xe5, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
59 { 0x0, 0xd6, 512, 8, 0x2000, 0, "NAND 8MiB 3.3V 8-bit"},
61 { 0x0, 0x39, 512, 8, 0x2000, 0, "NAND 8MiB 1.8V 8-bit"},
62 { 0x0, 0xe6, 512, 8, 0x2000, 0, "NAND 8MiB 3.3V 8-bit"},
63 { 0x0, 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16
, "NAND 8MiB 1.8V 16-bit"},
64 { 0x0, 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16
, "NAND 8MiB 3.3V 16-bit"},
65 /* end "museum" IDs */
67 { 0x0, 0x33, 512, 16, 0x4000, 0, "NAND 16MiB 1.8V 8-bit"},
68 { 0x0, 0x73, 512, 16, 0x4000, 0, "NAND 16MiB 3.3V 8-bit"},
69 { 0x0, 0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16
, "NAND 16MiB 1.8V 16-bit"},
70 { 0x0, 0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16
, "NAND 16MiB 3.3V 16-bit"},
72 { 0x0, 0x35, 512, 32, 0x4000, 0, "NAND 32MiB 1.8V 8-bit"},
73 { 0x0, 0x75, 512, 32, 0x4000, 0, "NAND 32MiB 3.3V 8-bit"},
74 { 0x0, 0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16
, "NAND 32MiB 1.8V 16-bit"},
75 { 0x0, 0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16
, "NAND 32MiB 3.3V 16-bit"},
77 { 0x0, 0x36, 512, 64, 0x4000, 0, "NAND 64MiB 1.8V 8-bit"},
78 { 0x0, 0x76, 512, 64, 0x4000, 0, "NAND 64MiB 3.3V 8-bit"},
79 { 0x0, 0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16
, "NAND 64MiB 1.8V 16-bit"},
80 { 0x0, 0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16
, "NAND 64MiB 3.3V 16-bit"},
82 { 0x0, 0x78, 512, 128, 0x4000, 0, "NAND 128MiB 1.8V 8-bit"},
83 { 0x0, 0x39, 512, 128, 0x4000, 0, "NAND 128MiB 1.8V 8-bit"},
84 { 0x0, 0x79, 512, 128, 0x4000, 0, "NAND 128MiB 3.3V 8-bit"},
85 { 0x0, 0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16
, "NAND 128MiB 1.8V 16-bit"},
86 { 0x0, 0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16
, "NAND 128MiB 1.8V 16-bit"},
87 { 0x0, 0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16
, "NAND 128MiB 3.3V 16-bit"},
88 { 0x0, 0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16
, "NAND 128MiB 3.3V 16-bit"},
90 { 0x0, 0x71, 512, 256, 0x4000, 0, "NAND 256MiB 3.3V 8-bit"},
92 { 0x0, 0xA2, 0, 64, 0, LP_OPTIONS
, "NAND 64MiB 1.8V 8-bit"},
93 { 0x0, 0xF2, 0, 64, 0, LP_OPTIONS
, "NAND 64MiB 3.3V 8-bit"},
94 { 0x0, 0xB2, 0, 64, 0, LP_OPTIONS16
, "NAND 64MiB 1.8V 16-bit"},
95 { 0x0, 0xC2, 0, 64, 0, LP_OPTIONS16
, "NAND 64MiB 3.3V 16-bit"},
97 { 0x0, 0xA1, 0, 128, 0, LP_OPTIONS
, "NAND 128MiB 1.8V 8-bit"},
98 { 0x0, 0xF1, 0, 128, 0, LP_OPTIONS
, "NAND 128MiB 3.3V 8-bit"},
99 { 0x0, 0xB1, 0, 128, 0, LP_OPTIONS16
, "NAND 128MiB 1.8V 16-bit"},
100 { 0x0, 0xC1, 0, 128, 0, LP_OPTIONS16
, "NAND 128MiB 3.3V 16-bit"},
102 { 0x0, 0xAA, 0, 256, 0, LP_OPTIONS
, "NAND 256MiB 1.8V 8-bit"},
103 { 0x0, 0xDA, 0, 256, 0, LP_OPTIONS
, "NAND 256MiB 3.3V 8-bit"},
104 { 0x0, 0xBA, 0, 256, 0, LP_OPTIONS16
, "NAND 256MiB 1.8V 16-bit"},
105 { 0x0, 0xCA, 0, 256, 0, LP_OPTIONS16
, "NAND 256MiB 3.3V 16-bit"},
107 { 0x0, 0xAC, 0, 512, 0, LP_OPTIONS
, "NAND 512MiB 1.8V 8-bit"},
108 { 0x0, 0xDC, 0, 512, 0, LP_OPTIONS
, "NAND 512MiB 3.3V 8-bit"},
109 { 0x0, 0xBC, 0, 512, 0, LP_OPTIONS16
, "NAND 512MiB 1.8V 16-bit"},
110 { 0x0, 0xCC, 0, 512, 0, LP_OPTIONS16
, "NAND 512MiB 3.3V 16-bit"},
112 { 0x0, 0xA3, 0, 1024, 0, LP_OPTIONS
, "NAND 1GiB 1.8V 8-bit"},
113 { 0x0, 0xD3, 0, 1024, 0, LP_OPTIONS
, "NAND 1GiB 3.3V 8-bit"},
114 { 0x0, 0xB3, 0, 1024, 0, LP_OPTIONS16
, "NAND 1GiB 1.8V 16-bit"},
115 { 0x0, 0xC3, 0, 1024, 0, LP_OPTIONS16
, "NAND 1GiB 3.3V 16-bit"},
117 { 0x0, 0xA5, 0, 2048, 0, LP_OPTIONS
, "NAND 2GiB 1.8V 8-bit"},
118 { 0x0, 0xD5, 0, 8192, 0, LP_OPTIONS
, "NAND 2GiB 3.3V 8-bit"},
119 { 0x0, 0xB5, 0, 2048, 0, LP_OPTIONS16
, "NAND 2GiB 1.8V 16-bit"},
120 { 0x0, 0xC5, 0, 2048, 0, LP_OPTIONS16
, "NAND 2GiB 3.3V 16-bit"},
122 { 0x0, 0x48, 0, 2048, 0, LP_OPTIONS
, "NAND 2GiB 3.3V 8-bit"},
124 {0, 0, 0, 0, 0, 0, NULL
}
127 /* Manufacturer ID list
129 static struct nand_manufacturer nand_manuf_ids
[] = {
131 {NAND_MFR_TOSHIBA
, "Toshiba"},
132 {NAND_MFR_SAMSUNG
, "Samsung"},
133 {NAND_MFR_FUJITSU
, "Fujitsu"},
134 {NAND_MFR_NATIONAL
, "National"},
135 {NAND_MFR_RENESAS
, "Renesas"},
136 {NAND_MFR_STMICRO
, "ST Micro"},
137 {NAND_MFR_HYNIX
, "Hynix"},
138 {NAND_MFR_MICRON
, "Micron"},
143 * Define default oob placement schemes for large and small page devices
147 static struct nand_ecclayout nand_oob_8
= {
160 * Returns the flash bank specified by @a name, which matches the
161 * driver name and a suffix (option) specify the driver-specific
162 * bank number. The suffix consists of the '.' and the driver-specific
163 * bank number: when two davinci banks are defined, then 'davinci.1' refers
164 * to the second (e.g. DM355EVM).
166 static struct nand_device
*get_nand_device_by_name(const char *name
)
168 unsigned int requested
= get_flash_name_index(name
);
169 unsigned int found
= 0;
171 struct nand_device
*nand
;
172 for (nand
= nand_devices
; nand
; nand
= nand
->next
) {
173 if (strcmp(nand
->name
, name
) == 0)
175 if (!flash_driver_name_matches(nand
->controller
->name
, name
))
177 if (++found
< requested
)
184 struct nand_device
*get_nand_device_by_num(int num
)
186 struct nand_device
*p
;
189 for (p
= nand_devices
; p
; p
= p
->next
) {
197 COMMAND_HELPER(nand_command_get_device
, unsigned int name_index
,
198 struct nand_device
**nand
)
200 const char *str
= CMD_ARGV
[name_index
];
201 *nand
= get_nand_device_by_name(str
);
206 COMMAND_PARSE_NUMBER(uint
, str
, num
);
207 *nand
= get_nand_device_by_num(num
);
209 command_print(CMD
, "NAND flash device '%s' not found", str
);
210 return ERROR_COMMAND_SYNTAX_ERROR
;
215 int nand_build_bbt(struct nand_device
*nand
, int first
, int last
)
219 int pages_per_block
= (nand
->erase_size
/ nand
->page_size
);
223 if ((first
< 0) || (first
>= nand
->num_blocks
))
226 if ((last
>= nand
->num_blocks
) || (last
== -1))
227 last
= nand
->num_blocks
- 1;
229 page
= first
* pages_per_block
;
230 for (i
= first
; i
<= last
; i
++) {
231 ret
= nand_read_page(nand
, page
, NULL
, 0, oob
, 6);
235 if (((nand
->device
->options
& NAND_BUSWIDTH_16
) && ((oob
[0] & oob
[1]) != 0xff))
236 || (((nand
->page_size
== 512) && (oob
[5] != 0xff)) ||
237 ((nand
->page_size
== 2048) && (oob
[0] != 0xff)))) {
238 LOG_WARNING("bad block: %i", i
);
239 nand
->blocks
[i
].is_bad
= 1;
241 nand
->blocks
[i
].is_bad
= 0;
243 page
+= pages_per_block
;
249 int nand_read_status(struct nand_device
*nand
, uint8_t *status
)
252 return ERROR_NAND_DEVICE_NOT_PROBED
;
254 /* Send read status command */
255 /* FIXME: errors returned from nand->controller are mostly ignored! */
256 nand
->controller
->command(nand
, NAND_CMD_STATUS
);
261 if (nand
->device
->options
& NAND_BUSWIDTH_16
) {
263 nand
->controller
->read_data(nand
, &data
);
264 *status
= data
& 0xff;
266 nand
->controller
->read_data(nand
, status
);
271 static int nand_poll_ready(struct nand_device
*nand
, int timeout
)
275 nand
->controller
->command(nand
, NAND_CMD_STATUS
);
277 if (nand
->device
->options
& NAND_BUSWIDTH_16
) {
279 nand
->controller
->read_data(nand
, &data
);
280 status
= data
& 0xff;
282 nand
->controller
->read_data(nand
, &status
);
283 if (status
& NAND_STATUS_READY
)
288 return (status
& NAND_STATUS_READY
) != 0;
291 int nand_probe(struct nand_device
*nand
)
293 uint8_t manufacturer_id
, device_id
;
294 uint8_t id_buff
[6] = { 0 }; /* zero buff to silence false warning
295 * from clang static analyzer */
299 /* clear device data */
301 nand
->manufacturer
= NULL
;
303 /* clear device parameters */
305 nand
->address_cycles
= 0;
307 nand
->erase_size
= 0;
309 /* initialize controller (device parameters are zero, use controller default) */
310 retval
= nand
->controller
->init(nand
);
311 if (retval
!= ERROR_OK
) {
313 case ERROR_NAND_OPERATION_FAILED
:
314 LOG_DEBUG("controller initialization failed");
315 return ERROR_NAND_OPERATION_FAILED
;
316 case ERROR_NAND_OPERATION_NOT_SUPPORTED
:
318 "BUG: controller reported that it doesn't support default parameters");
319 return ERROR_NAND_OPERATION_FAILED
;
321 LOG_ERROR("BUG: unknown controller initialization failure");
322 return ERROR_NAND_OPERATION_FAILED
;
326 nand
->controller
->command(nand
, NAND_CMD_RESET
);
327 nand
->controller
->reset(nand
);
329 nand
->controller
->command(nand
, NAND_CMD_READID
);
330 nand
->controller
->address(nand
, 0x0);
332 if (nand
->bus_width
== 8) {
333 nand
->controller
->read_data(nand
, &manufacturer_id
);
334 nand
->controller
->read_data(nand
, &device_id
);
337 nand
->controller
->read_data(nand
, &data_buf
);
338 manufacturer_id
= data_buf
& 0xff;
339 nand
->controller
->read_data(nand
, &data_buf
);
340 device_id
= data_buf
& 0xff;
343 for (i
= 0; nand_flash_ids
[i
].name
; i
++) {
344 if (nand_flash_ids
[i
].id
== device_id
&&
345 (nand_flash_ids
[i
].mfr_id
== manufacturer_id
||
346 nand_flash_ids
[i
].mfr_id
== 0)) {
347 nand
->device
= &nand_flash_ids
[i
];
352 for (i
= 0; nand_manuf_ids
[i
].name
; i
++) {
353 if (nand_manuf_ids
[i
].id
== manufacturer_id
) {
354 nand
->manufacturer
= &nand_manuf_ids
[i
];
359 if (!nand
->manufacturer
) {
360 nand
->manufacturer
= &nand_manuf_ids
[0];
361 nand
->manufacturer
->id
= manufacturer_id
;
366 "unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
369 return ERROR_NAND_OPERATION_FAILED
;
372 LOG_DEBUG("found %s (%s)", nand
->device
->name
, nand
->manufacturer
->name
);
374 /* initialize device parameters */
377 if (nand
->device
->options
& NAND_BUSWIDTH_16
)
378 nand
->bus_width
= 16;
382 /* Do we need extended device probe information? */
383 if (nand
->device
->page_size
== 0 ||
384 nand
->device
->erase_size
== 0) {
385 if (nand
->bus_width
== 8) {
386 retval
= nand
->controller
->read_data(nand
, id_buff
+ 3);
387 if (retval
!= ERROR_OK
)
390 retval
= nand
->controller
->read_data(nand
, id_buff
+ 4);
391 if (retval
!= ERROR_OK
)
394 retval
= nand
->controller
->read_data(nand
, id_buff
+ 5);
395 if (retval
!= ERROR_OK
)
401 retval
= nand
->controller
->read_data(nand
, &data_buf
);
402 if (retval
!= ERROR_OK
)
404 id_buff
[3] = data_buf
;
406 retval
= nand
->controller
->read_data(nand
, &data_buf
);
407 if (retval
!= ERROR_OK
)
409 id_buff
[4] = data_buf
;
411 retval
= nand
->controller
->read_data(nand
, &data_buf
);
412 if (retval
!= ERROR_OK
)
414 id_buff
[5] = data_buf
>> 8;
419 if (nand
->device
->page_size
== 0)
420 nand
->page_size
= 1 << (10 + (id_buff
[4] & 3));
421 else if (nand
->device
->page_size
== 256) {
422 LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
423 return ERROR_NAND_OPERATION_FAILED
;
425 nand
->page_size
= nand
->device
->page_size
;
427 /* number of address cycles */
428 if (nand
->page_size
<= 512) {
429 /* small page devices */
430 if (nand
->device
->chip_size
<= 32)
431 nand
->address_cycles
= 3;
432 else if (nand
->device
->chip_size
<= 8*1024)
433 nand
->address_cycles
= 4;
435 LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
436 nand
->address_cycles
= 5;
439 /* large page devices */
440 if (nand
->device
->chip_size
<= 128)
441 nand
->address_cycles
= 4;
442 else if (nand
->device
->chip_size
<= 32*1024)
443 nand
->address_cycles
= 5;
445 LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
446 nand
->address_cycles
= 6;
451 if (nand
->device
->erase_size
== 0) {
452 switch ((id_buff
[4] >> 4) & 3) {
454 nand
->erase_size
= 64 << 10;
457 nand
->erase_size
= 128 << 10;
460 nand
->erase_size
= 256 << 10;
463 nand
->erase_size
= 512 << 10;
467 nand
->erase_size
= nand
->device
->erase_size
;
469 /* initialize controller, but leave parameters at the controllers default */
470 retval
= nand
->controller
->init(nand
);
471 if (retval
!= ERROR_OK
) {
473 case ERROR_NAND_OPERATION_FAILED
:
474 LOG_DEBUG("controller initialization failed");
475 return ERROR_NAND_OPERATION_FAILED
;
476 case ERROR_NAND_OPERATION_NOT_SUPPORTED
:
478 "controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
480 nand
->address_cycles
,
482 return ERROR_NAND_OPERATION_FAILED
;
484 LOG_ERROR("BUG: unknown controller initialization failure");
485 return ERROR_NAND_OPERATION_FAILED
;
489 nand
->num_blocks
= (nand
->device
->chip_size
* 1024) / (nand
->erase_size
/ 1024);
490 nand
->blocks
= malloc(sizeof(struct nand_block
) * nand
->num_blocks
);
492 for (i
= 0; i
< nand
->num_blocks
; i
++) {
493 nand
->blocks
[i
].size
= nand
->erase_size
;
494 nand
->blocks
[i
].offset
= i
* nand
->erase_size
;
495 nand
->blocks
[i
].is_erased
= -1;
496 nand
->blocks
[i
].is_bad
= -1;
502 int nand_erase(struct nand_device
*nand
, int first_block
, int last_block
)
510 return ERROR_NAND_DEVICE_NOT_PROBED
;
512 if ((first_block
< 0) || (last_block
>= nand
->num_blocks
))
513 return ERROR_COMMAND_SYNTAX_ERROR
;
515 /* make sure we know if a block is bad before erasing it */
516 for (i
= first_block
; i
<= last_block
; i
++) {
517 if (nand
->blocks
[i
].is_bad
== -1) {
518 nand_build_bbt(nand
, i
, last_block
);
523 for (i
= first_block
; i
<= last_block
; i
++) {
524 /* Send erase setup command */
525 nand
->controller
->command(nand
, NAND_CMD_ERASE1
);
527 page
= i
* (nand
->erase_size
/ nand
->page_size
);
529 /* Send page address */
530 if (nand
->page_size
<= 512) {
532 nand
->controller
->address(nand
, page
& 0xff);
533 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
535 /* 3rd cycle only on devices with more than 32 MiB */
536 if (nand
->address_cycles
>= 4)
537 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
539 /* 4th cycle only on devices with more than 8 GiB */
540 if (nand
->address_cycles
>= 5)
541 nand
->controller
->address(nand
, (page
>> 24) & 0xff);
544 nand
->controller
->address(nand
, page
& 0xff);
545 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
547 /* 3rd cycle only on devices with more than 128 MiB */
548 if (nand
->address_cycles
>= 5)
549 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
552 /* Send erase confirm command */
553 nand
->controller
->command(nand
, NAND_CMD_ERASE2
);
555 retval
= nand
->controller
->nand_ready
?
556 nand
->controller
->nand_ready(nand
, 1000) :
557 nand_poll_ready(nand
, 1000);
559 LOG_ERROR("timeout waiting for NAND flash block erase to complete");
560 return ERROR_NAND_OPERATION_TIMEOUT
;
563 retval
= nand_read_status(nand
, &status
);
564 if (retval
!= ERROR_OK
) {
565 LOG_ERROR("couldn't read status");
566 return ERROR_NAND_OPERATION_FAILED
;
570 LOG_ERROR("didn't erase %sblock %d; status: 0x%2.2x",
571 (nand
->blocks
[i
].is_bad
== 1)
574 /* continue; other blocks might still be erasable */
577 nand
->blocks
[i
].is_erased
= 1;
584 static int nand_read_plain(struct nand_device
*nand
,
592 return ERROR_NAND_DEVICE_NOT_PROBED
;
594 if (address
% nand
->page_size
) {
595 LOG_ERROR("reads need to be page aligned");
596 return ERROR_NAND_OPERATION_FAILED
;
599 page
= malloc(nand
->page_size
);
601 while (data_size
> 0) {
602 uint32_t thisrun_size
= (data_size
> nand
->page_size
) ? nand
->page_size
: data_size
;
603 uint32_t page_address
;
606 page_address
= address
/ nand
->page_size
;
608 nand_read_page(nand
, page_address
, page
, nand
->page_size
, NULL
, 0);
610 memcpy(data
, page
, thisrun_size
);
612 address
+= thisrun_size
;
613 data
+= thisrun_size
;
614 data_size
-= thisrun_size
;
622 static int nand_write_plain(struct nand_device
*nand
,
630 return ERROR_NAND_DEVICE_NOT_PROBED
;
632 if (address
% nand
->page_size
) {
633 LOG_ERROR("writes need to be page aligned");
634 return ERROR_NAND_OPERATION_FAILED
;
637 page
= malloc(nand
->page_size
);
639 while (data_size
> 0) {
640 uint32_t thisrun_size
= (data_size
> nand
->page_size
) ? nand
->page_size
: data_size
;
641 uint32_t page_address
;
643 memset(page
, 0xff, nand
->page_size
);
644 memcpy(page
, data
, thisrun_size
);
646 page_address
= address
/ nand
->page_size
;
648 nand_write_page(nand
, page_address
, page
, nand
->page_size
, NULL
, 0);
650 address
+= thisrun_size
;
651 data
+= thisrun_size
;
652 data_size
-= thisrun_size
;
661 int nand_write_page(struct nand_device
*nand
, uint32_t page
,
662 uint8_t *data
, uint32_t data_size
,
663 uint8_t *oob
, uint32_t oob_size
)
668 return ERROR_NAND_DEVICE_NOT_PROBED
;
670 block
= page
/ (nand
->erase_size
/ nand
->page_size
);
671 if (nand
->blocks
[block
].is_erased
== 1)
672 nand
->blocks
[block
].is_erased
= 0;
674 if (nand
->use_raw
|| !nand
->controller
->write_page
)
675 return nand_write_page_raw(nand
, page
, data
, data_size
, oob
, oob_size
);
677 return nand
->controller
->write_page(nand
, page
, data
, data_size
, oob
, oob_size
);
680 int nand_read_page(struct nand_device
*nand
, uint32_t page
,
681 uint8_t *data
, uint32_t data_size
,
682 uint8_t *oob
, uint32_t oob_size
)
685 return ERROR_NAND_DEVICE_NOT_PROBED
;
687 if (nand
->use_raw
|| !nand
->controller
->read_page
)
688 return nand_read_page_raw(nand
, page
, data
, data_size
, oob
, oob_size
);
690 return nand
->controller
->read_page(nand
, page
, data
, data_size
, oob
, oob_size
);
693 int nand_page_command(struct nand_device
*nand
, uint32_t page
,
694 uint8_t cmd
, bool oob_only
)
697 return ERROR_NAND_DEVICE_NOT_PROBED
;
699 if (oob_only
&& NAND_CMD_READ0
== cmd
&& nand
->page_size
<= 512)
700 cmd
= NAND_CMD_READOOB
;
702 nand
->controller
->command(nand
, cmd
);
704 if (nand
->page_size
<= 512) {
705 /* small page device */
707 /* column (always 0, we start at the beginning of a page/OOB area) */
708 nand
->controller
->address(nand
, 0x0);
711 nand
->controller
->address(nand
, page
& 0xff);
712 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
714 /* 4th cycle only on devices with more than 32 MiB */
715 if (nand
->address_cycles
>= 4)
716 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
718 /* 5th cycle only on devices with more than 8 GiB */
719 if (nand
->address_cycles
>= 5)
720 nand
->controller
->address(nand
, (page
>> 24) & 0xff);
722 /* large page device */
724 /* column (0 when we start at the beginning of a page,
725 * or 2048 for the beginning of OOB area)
727 nand
->controller
->address(nand
, 0x0);
729 nand
->controller
->address(nand
, 0x8);
731 nand
->controller
->address(nand
, 0x0);
734 nand
->controller
->address(nand
, page
& 0xff);
735 nand
->controller
->address(nand
, (page
>> 8) & 0xff);
737 /* 5th cycle only on devices with more than 128 MiB */
738 if (nand
->address_cycles
>= 5)
739 nand
->controller
->address(nand
, (page
>> 16) & 0xff);
741 /* large page devices need a start command if reading */
742 if (cmd
== NAND_CMD_READ0
)
743 nand
->controller
->command(nand
, NAND_CMD_READSTART
);
746 if (nand
->controller
->nand_ready
) {
747 if (!nand
->controller
->nand_ready(nand
, 100))
748 return ERROR_NAND_OPERATION_TIMEOUT
;
750 /* nand_poll_read() cannot be used during nand read */
757 int nand_read_data_page(struct nand_device
*nand
, uint8_t *data
, uint32_t size
)
759 int retval
= ERROR_NAND_NO_BUFFER
;
761 if (nand
->controller
->read_block_data
)
762 retval
= (nand
->controller
->read_block_data
)(nand
, data
, size
);
764 if (retval
== ERROR_NAND_NO_BUFFER
) {
766 int incr
= (nand
->device
->options
& NAND_BUSWIDTH_16
) ? 2 : 1;
769 for (i
= 0; retval
== ERROR_OK
&& i
< size
; i
+= incr
) {
770 retval
= nand
->controller
->read_data(nand
, data
);
778 int nand_read_page_raw(struct nand_device
*nand
, uint32_t page
,
779 uint8_t *data
, uint32_t data_size
,
780 uint8_t *oob
, uint32_t oob_size
)
784 retval
= nand_page_command(nand
, page
, NAND_CMD_READ0
, !data
);
785 if (retval
!= ERROR_OK
)
789 nand_read_data_page(nand
, data
, data_size
);
792 nand_read_data_page(nand
, oob
, oob_size
);
797 int nand_write_data_page(struct nand_device
*nand
, uint8_t *data
, uint32_t size
)
799 int retval
= ERROR_NAND_NO_BUFFER
;
801 if (nand
->controller
->write_block_data
)
802 retval
= (nand
->controller
->write_block_data
)(nand
, data
, size
);
804 if (retval
== ERROR_NAND_NO_BUFFER
) {
805 bool is16bit
= nand
->device
->options
& NAND_BUSWIDTH_16
;
806 uint32_t incr
= is16bit
? 2 : 1;
810 for (i
= 0; i
< size
; i
+= incr
) {
812 write_data
= le_to_h_u16(data
);
816 retval
= nand
->controller
->write_data(nand
, write_data
);
817 if (retval
!= ERROR_OK
)
827 int nand_write_finish(struct nand_device
*nand
)
832 nand
->controller
->command(nand
, NAND_CMD_PAGEPROG
);
834 retval
= nand
->controller
->nand_ready
?
835 nand
->controller
->nand_ready(nand
, 100) :
836 nand_poll_ready(nand
, 100);
838 return ERROR_NAND_OPERATION_TIMEOUT
;
840 retval
= nand_read_status(nand
, &status
);
841 if (retval
!= ERROR_OK
) {
842 LOG_ERROR("couldn't read status");
843 return ERROR_NAND_OPERATION_FAILED
;
846 if (status
& NAND_STATUS_FAIL
) {
847 LOG_ERROR("write operation didn't pass, status: 0x%2.2x",
849 return ERROR_NAND_OPERATION_FAILED
;
855 int nand_write_page_raw(struct nand_device
*nand
, uint32_t page
,
856 uint8_t *data
, uint32_t data_size
,
857 uint8_t *oob
, uint32_t oob_size
)
861 retval
= nand_page_command(nand
, page
, NAND_CMD_SEQIN
, !data
);
862 if (retval
!= ERROR_OK
)
866 retval
= nand_write_data_page(nand
, data
, data_size
);
867 if (retval
!= ERROR_OK
) {
868 LOG_ERROR("Unable to write data to NAND device");
874 retval
= nand_write_data_page(nand
, oob
, oob_size
);
875 if (retval
!= ERROR_OK
) {
876 LOG_ERROR("Unable to write OOB data to NAND device");
881 return nand_write_finish(nand
);