3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
6 * Additional technical information is available on
7 * http://www.linux-mtd.infradead.org/doc/nand.html
9 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
10 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
13 * David Woodhouse for adding multichip support
15 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
19 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
21 * if we have HW ECC support.
22 * BBT table is not serialized, has to be fixed
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
39 #include <linux/types.h>
40 #include <linux/mtd/mtd.h>
41 #include <linux/mtd/nand.h>
42 #include <linux/mtd/nand_ecc.h>
43 #include <linux/mtd/nand_bch.h>
44 #include <linux/interrupt.h>
45 #include <linux/bitops.h>
47 #include <linux/mtd/partitions.h>
50 static int nand_get_device(struct mtd_info
*mtd
, int new_state
);
52 static int nand_do_write_oob(struct mtd_info
*mtd
, loff_t to
,
53 struct mtd_oob_ops
*ops
);
55 /* Define default oob placement schemes for large and small page devices */
56 static int nand_ooblayout_ecc_sp(struct mtd_info
*mtd
, int section
,
57 struct mtd_oob_region
*oobregion
)
59 struct nand_chip
*chip
= mtd_to_nand(mtd
);
60 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
66 oobregion
->offset
= 0;
67 if (mtd
->oobsize
== 16)
68 oobregion
->length
= 4;
70 oobregion
->length
= 3;
72 if (mtd
->oobsize
== 8)
75 oobregion
->offset
= 6;
76 oobregion
->length
= ecc
->total
- 4;
82 static int nand_ooblayout_free_sp(struct mtd_info
*mtd
, int section
,
83 struct mtd_oob_region
*oobregion
)
88 if (mtd
->oobsize
== 16) {
92 oobregion
->length
= 8;
93 oobregion
->offset
= 8;
95 oobregion
->length
= 2;
97 oobregion
->offset
= 3;
99 oobregion
->offset
= 6;
105 const struct mtd_ooblayout_ops nand_ooblayout_sp_ops
= {
106 .ecc
= nand_ooblayout_ecc_sp
,
107 .free
= nand_ooblayout_free_sp
,
109 EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops
);
111 static int nand_ooblayout_ecc_lp(struct mtd_info
*mtd
, int section
,
112 struct mtd_oob_region
*oobregion
)
114 struct nand_chip
*chip
= mtd_to_nand(mtd
);
115 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
120 oobregion
->length
= ecc
->total
;
121 oobregion
->offset
= mtd
->oobsize
- oobregion
->length
;
126 static int nand_ooblayout_free_lp(struct mtd_info
*mtd
, int section
,
127 struct mtd_oob_region
*oobregion
)
129 struct nand_chip
*chip
= mtd_to_nand(mtd
);
130 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
135 oobregion
->length
= mtd
->oobsize
- ecc
->total
- 2;
136 oobregion
->offset
= 2;
141 const struct mtd_ooblayout_ops nand_ooblayout_lp_ops
= {
142 .ecc
= nand_ooblayout_ecc_lp
,
143 .free
= nand_ooblayout_free_lp
,
145 EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops
);
148 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
149 * are placed at a fixed offset.
151 static int nand_ooblayout_ecc_lp_hamming(struct mtd_info
*mtd
, int section
,
152 struct mtd_oob_region
*oobregion
)
154 struct nand_chip
*chip
= mtd_to_nand(mtd
);
155 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
160 switch (mtd
->oobsize
) {
162 oobregion
->offset
= 40;
165 oobregion
->offset
= 80;
171 oobregion
->length
= ecc
->total
;
172 if (oobregion
->offset
+ oobregion
->length
> mtd
->oobsize
)
178 static int nand_ooblayout_free_lp_hamming(struct mtd_info
*mtd
, int section
,
179 struct mtd_oob_region
*oobregion
)
181 struct nand_chip
*chip
= mtd_to_nand(mtd
);
182 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
185 if (section
< 0 || section
> 1)
188 switch (mtd
->oobsize
) {
200 oobregion
->offset
= 2;
201 oobregion
->length
= ecc_offset
- 2;
203 oobregion
->offset
= ecc_offset
+ ecc
->total
;
204 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
210 const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops
= {
211 .ecc
= nand_ooblayout_ecc_lp_hamming
,
212 .free
= nand_ooblayout_free_lp_hamming
,
215 static int check_offs_len(struct mtd_info
*mtd
,
216 loff_t ofs
, uint64_t len
)
218 struct nand_chip
*chip
= mtd_to_nand(mtd
);
221 /* Start address must align on block boundary */
222 if (ofs
& ((1ULL << chip
->phys_erase_shift
) - 1)) {
223 pr_debug("%s: unaligned address\n", __func__
);
227 /* Length must align on block boundary */
228 if (len
& ((1ULL << chip
->phys_erase_shift
) - 1)) {
229 pr_debug("%s: length not block aligned\n", __func__
);
237 * nand_release_device - [GENERIC] release chip
238 * @mtd: MTD device structure
240 * Release chip lock and wake up anyone waiting on the device.
242 static void nand_release_device(struct mtd_info
*mtd
)
244 struct nand_chip
*chip
= mtd_to_nand(mtd
);
246 /* Release the controller and the chip */
247 spin_lock(&chip
->controller
->lock
);
248 chip
->controller
->active
= NULL
;
249 chip
->state
= FL_READY
;
250 wake_up(&chip
->controller
->wq
);
251 spin_unlock(&chip
->controller
->lock
);
255 * nand_read_byte - [DEFAULT] read one byte from the chip
256 * @mtd: MTD device structure
258 * Default read function for 8bit buswidth
260 static uint8_t nand_read_byte(struct mtd_info
*mtd
)
262 struct nand_chip
*chip
= mtd_to_nand(mtd
);
263 return readb(chip
->IO_ADDR_R
);
267 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
268 * @mtd: MTD device structure
270 * Default read function for 16bit buswidth with endianness conversion.
273 static uint8_t nand_read_byte16(struct mtd_info
*mtd
)
275 struct nand_chip
*chip
= mtd_to_nand(mtd
);
276 return (uint8_t) cpu_to_le16(readw(chip
->IO_ADDR_R
));
280 * nand_read_word - [DEFAULT] read one word from the chip
281 * @mtd: MTD device structure
283 * Default read function for 16bit buswidth without endianness conversion.
285 static u16
nand_read_word(struct mtd_info
*mtd
)
287 struct nand_chip
*chip
= mtd_to_nand(mtd
);
288 return readw(chip
->IO_ADDR_R
);
292 * nand_select_chip - [DEFAULT] control CE line
293 * @mtd: MTD device structure
294 * @chipnr: chipnumber to select, -1 for deselect
296 * Default select function for 1 chip devices.
298 static void nand_select_chip(struct mtd_info
*mtd
, int chipnr
)
300 struct nand_chip
*chip
= mtd_to_nand(mtd
);
304 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
, 0 | NAND_CTRL_CHANGE
);
315 * nand_write_byte - [DEFAULT] write single byte to chip
316 * @mtd: MTD device structure
317 * @byte: value to write
319 * Default function to write a byte to I/O[7:0]
321 static void nand_write_byte(struct mtd_info
*mtd
, uint8_t byte
)
323 struct nand_chip
*chip
= mtd_to_nand(mtd
);
325 chip
->write_buf(mtd
, &byte
, 1);
329 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
330 * @mtd: MTD device structure
331 * @byte: value to write
333 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 static void nand_write_byte16(struct mtd_info
*mtd
, uint8_t byte
)
337 struct nand_chip
*chip
= mtd_to_nand(mtd
);
338 uint16_t word
= byte
;
341 * It's not entirely clear what should happen to I/O[15:8] when writing
342 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 * When the host supports a 16-bit bus width, only data is
345 * transferred at the 16-bit width. All address and command line
346 * transfers shall use only the lower 8-bits of the data bus. During
347 * command transfers, the host may place any value on the upper
348 * 8-bits of the data bus. During address transfers, the host shall
349 * set the upper 8-bits of the data bus to 00h.
351 * One user of the write_byte callback is nand_onfi_set_features. The
352 * four parameters are specified to be written to I/O[7:0], but this is
353 * neither an address nor a command transfer. Let's assume a 0 on the
354 * upper I/O lines is OK.
356 chip
->write_buf(mtd
, (uint8_t *)&word
, 2);
360 * nand_write_buf - [DEFAULT] write buffer to chip
361 * @mtd: MTD device structure
363 * @len: number of bytes to write
365 * Default write function for 8bit buswidth.
367 static void nand_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
369 struct nand_chip
*chip
= mtd_to_nand(mtd
);
371 iowrite8_rep(chip
->IO_ADDR_W
, buf
, len
);
375 * nand_read_buf - [DEFAULT] read chip data into buffer
376 * @mtd: MTD device structure
377 * @buf: buffer to store date
378 * @len: number of bytes to read
380 * Default read function for 8bit buswidth.
382 static void nand_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
384 struct nand_chip
*chip
= mtd_to_nand(mtd
);
386 ioread8_rep(chip
->IO_ADDR_R
, buf
, len
);
390 * nand_write_buf16 - [DEFAULT] write buffer to chip
391 * @mtd: MTD device structure
393 * @len: number of bytes to write
395 * Default write function for 16bit buswidth.
397 static void nand_write_buf16(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
399 struct nand_chip
*chip
= mtd_to_nand(mtd
);
400 u16
*p
= (u16
*) buf
;
402 iowrite16_rep(chip
->IO_ADDR_W
, p
, len
>> 1);
406 * nand_read_buf16 - [DEFAULT] read chip data into buffer
407 * @mtd: MTD device structure
408 * @buf: buffer to store date
409 * @len: number of bytes to read
411 * Default read function for 16bit buswidth.
413 static void nand_read_buf16(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
415 struct nand_chip
*chip
= mtd_to_nand(mtd
);
416 u16
*p
= (u16
*) buf
;
418 ioread16_rep(chip
->IO_ADDR_R
, p
, len
>> 1);
422 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
423 * @mtd: MTD device structure
424 * @ofs: offset from device start
426 * Check, if the block is bad.
428 static int nand_block_bad(struct mtd_info
*mtd
, loff_t ofs
)
430 int page
, res
= 0, i
= 0;
431 struct nand_chip
*chip
= mtd_to_nand(mtd
);
434 if (chip
->bbt_options
& NAND_BBT_SCANLASTPAGE
)
435 ofs
+= mtd
->erasesize
- mtd
->writesize
;
437 page
= (int)(ofs
>> chip
->page_shift
) & chip
->pagemask
;
440 if (chip
->options
& NAND_BUSWIDTH_16
) {
441 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
,
442 chip
->badblockpos
& 0xFE, page
);
443 bad
= cpu_to_le16(chip
->read_word(mtd
));
444 if (chip
->badblockpos
& 0x1)
449 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, chip
->badblockpos
,
451 bad
= chip
->read_byte(mtd
);
454 if (likely(chip
->badblockbits
== 8))
457 res
= hweight8(bad
) < chip
->badblockbits
;
458 ofs
+= mtd
->writesize
;
459 page
= (int)(ofs
>> chip
->page_shift
) & chip
->pagemask
;
461 } while (!res
&& i
< 2 && (chip
->bbt_options
& NAND_BBT_SCAN2NDPAGE
));
467 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
468 * @mtd: MTD device structure
469 * @ofs: offset from device start
471 * This is the default implementation, which can be overridden by a hardware
472 * specific driver. It provides the details for writing a bad block marker to a
475 static int nand_default_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
477 struct nand_chip
*chip
= mtd_to_nand(mtd
);
478 struct mtd_oob_ops ops
;
479 uint8_t buf
[2] = { 0, 0 };
480 int ret
= 0, res
, i
= 0;
482 memset(&ops
, 0, sizeof(ops
));
484 ops
.ooboffs
= chip
->badblockpos
;
485 if (chip
->options
& NAND_BUSWIDTH_16
) {
486 ops
.ooboffs
&= ~0x01;
487 ops
.len
= ops
.ooblen
= 2;
489 ops
.len
= ops
.ooblen
= 1;
491 ops
.mode
= MTD_OPS_PLACE_OOB
;
493 /* Write to first/last page(s) if necessary */
494 if (chip
->bbt_options
& NAND_BBT_SCANLASTPAGE
)
495 ofs
+= mtd
->erasesize
- mtd
->writesize
;
497 res
= nand_do_write_oob(mtd
, ofs
, &ops
);
502 ofs
+= mtd
->writesize
;
503 } while ((chip
->bbt_options
& NAND_BBT_SCAN2NDPAGE
) && i
< 2);
509 * nand_block_markbad_lowlevel - mark a block bad
510 * @mtd: MTD device structure
511 * @ofs: offset from device start
513 * This function performs the generic NAND bad block marking steps (i.e., bad
514 * block table(s) and/or marker(s)). We only allow the hardware driver to
515 * specify how to write bad block markers to OOB (chip->block_markbad).
517 * We try operations in the following order:
518 * (1) erase the affected block, to allow OOB marker to be written cleanly
519 * (2) write bad block marker to OOB area of affected block (unless flag
520 * NAND_BBT_NO_OOB_BBM is present)
522 * Note that we retain the first error encountered in (2) or (3), finish the
523 * procedures, and dump the error in the end.
525 static int nand_block_markbad_lowlevel(struct mtd_info
*mtd
, loff_t ofs
)
527 struct nand_chip
*chip
= mtd_to_nand(mtd
);
530 if (!(chip
->bbt_options
& NAND_BBT_NO_OOB_BBM
)) {
531 struct erase_info einfo
;
533 /* Attempt erase before marking OOB */
534 memset(&einfo
, 0, sizeof(einfo
));
537 einfo
.len
= 1ULL << chip
->phys_erase_shift
;
538 nand_erase_nand(mtd
, &einfo
, 0);
540 /* Write bad block marker to OOB */
541 nand_get_device(mtd
, FL_WRITING
);
542 ret
= chip
->block_markbad(mtd
, ofs
);
543 nand_release_device(mtd
);
546 /* Mark block bad in BBT */
548 res
= nand_markbad_bbt(mtd
, ofs
);
554 mtd
->ecc_stats
.badblocks
++;
560 * nand_check_wp - [GENERIC] check if the chip is write protected
561 * @mtd: MTD device structure
563 * Check, if the device is write protected. The function expects, that the
564 * device is already selected.
566 static int nand_check_wp(struct mtd_info
*mtd
)
568 struct nand_chip
*chip
= mtd_to_nand(mtd
);
570 /* Broken xD cards report WP despite being writable */
571 if (chip
->options
& NAND_BROKEN_XD
)
574 /* Check the WP bit */
575 chip
->cmdfunc(mtd
, NAND_CMD_STATUS
, -1, -1);
576 return (chip
->read_byte(mtd
) & NAND_STATUS_WP
) ? 0 : 1;
580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
581 * @mtd: MTD device structure
582 * @ofs: offset from device start
584 * Check if the block is marked as reserved.
586 static int nand_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
588 struct nand_chip
*chip
= mtd_to_nand(mtd
);
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd
, ofs
);
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
600 * @allowbbt: 1, if its allowed to access the bbt area
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
605 static int nand_block_checkbad(struct mtd_info
*mtd
, loff_t ofs
, int allowbbt
)
607 struct nand_chip
*chip
= mtd_to_nand(mtd
);
610 return chip
->block_bad(mtd
, ofs
);
612 /* Return info from the table */
613 return nand_isbad_bbt(mtd
, ofs
, allowbbt
);
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
618 * @mtd: MTD device structure
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
624 static void panic_nand_wait_ready(struct mtd_info
*mtd
, unsigned long timeo
)
626 struct nand_chip
*chip
= mtd_to_nand(mtd
);
629 /* Wait for the device to get ready */
630 for (i
= 0; i
< timeo
; i
++) {
631 if (chip
->dev_ready(mtd
))
633 touch_softlockup_watchdog();
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
644 void nand_wait_ready(struct mtd_info
*mtd
)
646 struct nand_chip
*chip
= mtd_to_nand(mtd
);
647 unsigned long timeo
= 400;
649 if (in_interrupt() || oops_in_progress
)
650 return panic_nand_wait_ready(mtd
, timeo
);
652 /* Wait until command is processed or timeout occurs */
653 timeo
= jiffies
+ msecs_to_jiffies(timeo
);
655 if (chip
->dev_ready(mtd
))
658 } while (time_before(jiffies
, timeo
));
660 if (!chip
->dev_ready(mtd
))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
663 EXPORT_SYMBOL_GPL(nand_wait_ready
);
666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
670 * Wait for status ready (i.e. command done) or timeout.
672 static void nand_wait_status_ready(struct mtd_info
*mtd
, unsigned long timeo
)
674 register struct nand_chip
*chip
= mtd_to_nand(mtd
);
676 timeo
= jiffies
+ msecs_to_jiffies(timeo
);
678 if ((chip
->read_byte(mtd
) & NAND_STATUS_READY
))
680 touch_softlockup_watchdog();
681 } while (time_before(jiffies
, timeo
));
685 * nand_command - [DEFAULT] Send command to NAND device
686 * @mtd: MTD device structure
687 * @command: the command to be sent
688 * @column: the column address for this command, -1 if none
689 * @page_addr: the page address for this command, -1 if none
691 * Send command to NAND device. This function is used for small page devices
692 * (512 Bytes per page).
694 static void nand_command(struct mtd_info
*mtd
, unsigned int command
,
695 int column
, int page_addr
)
697 register struct nand_chip
*chip
= mtd_to_nand(mtd
);
698 int ctrl
= NAND_CTRL_CLE
| NAND_CTRL_CHANGE
;
700 /* Write out the command to the device */
701 if (command
== NAND_CMD_SEQIN
) {
704 if (column
>= mtd
->writesize
) {
706 column
-= mtd
->writesize
;
707 readcmd
= NAND_CMD_READOOB
;
708 } else if (column
< 256) {
709 /* First 256 bytes --> READ0 */
710 readcmd
= NAND_CMD_READ0
;
713 readcmd
= NAND_CMD_READ1
;
715 chip
->cmd_ctrl(mtd
, readcmd
, ctrl
);
716 ctrl
&= ~NAND_CTRL_CHANGE
;
718 if (command
!= NAND_CMD_NONE
)
719 chip
->cmd_ctrl(mtd
, command
, ctrl
);
721 /* Address cycle, when necessary */
722 ctrl
= NAND_CTRL_ALE
| NAND_CTRL_CHANGE
;
723 /* Serially input address */
725 /* Adjust columns for 16 bit buswidth */
726 if (chip
->options
& NAND_BUSWIDTH_16
&&
727 !nand_opcode_8bits(command
))
729 chip
->cmd_ctrl(mtd
, column
, ctrl
);
730 ctrl
&= ~NAND_CTRL_CHANGE
;
732 if (page_addr
!= -1) {
733 chip
->cmd_ctrl(mtd
, page_addr
, ctrl
);
734 ctrl
&= ~NAND_CTRL_CHANGE
;
735 chip
->cmd_ctrl(mtd
, page_addr
>> 8, ctrl
);
736 /* One more address cycle for devices > 32MiB */
737 if (chip
->chipsize
> (32 << 20))
738 chip
->cmd_ctrl(mtd
, page_addr
>> 16, ctrl
);
740 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
743 * Program and erase have their own busy handlers status and sequential
749 case NAND_CMD_PAGEPROG
:
750 case NAND_CMD_ERASE1
:
751 case NAND_CMD_ERASE2
:
753 case NAND_CMD_STATUS
:
759 udelay(chip
->chip_delay
);
760 chip
->cmd_ctrl(mtd
, NAND_CMD_STATUS
,
761 NAND_CTRL_CLE
| NAND_CTRL_CHANGE
);
763 NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
764 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
765 nand_wait_status_ready(mtd
, 250);
768 /* This applies to read commands */
771 * If we don't have access to the busy pin, we apply the given
774 if (!chip
->dev_ready
) {
775 udelay(chip
->chip_delay
);
780 * Apply this short delay always to ensure that we do wait tWB in
781 * any case on any machine.
785 nand_wait_ready(mtd
);
789 * nand_command_lp - [DEFAULT] Send command to NAND large page device
790 * @mtd: MTD device structure
791 * @command: the command to be sent
792 * @column: the column address for this command, -1 if none
793 * @page_addr: the page address for this command, -1 if none
795 * Send command to NAND device. This is the version for the new large page
796 * devices. We don't have the separate regions as we have in the small page
797 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
799 static void nand_command_lp(struct mtd_info
*mtd
, unsigned int command
,
800 int column
, int page_addr
)
802 register struct nand_chip
*chip
= mtd_to_nand(mtd
);
804 /* Emulate NAND_CMD_READOOB */
805 if (command
== NAND_CMD_READOOB
) {
806 column
+= mtd
->writesize
;
807 command
= NAND_CMD_READ0
;
810 /* Command latch cycle */
811 if (command
!= NAND_CMD_NONE
)
812 chip
->cmd_ctrl(mtd
, command
,
813 NAND_NCE
| NAND_CLE
| NAND_CTRL_CHANGE
);
815 if (column
!= -1 || page_addr
!= -1) {
816 int ctrl
= NAND_CTRL_CHANGE
| NAND_NCE
| NAND_ALE
;
818 /* Serially input address */
820 /* Adjust columns for 16 bit buswidth */
821 if (chip
->options
& NAND_BUSWIDTH_16
&&
822 !nand_opcode_8bits(command
))
824 chip
->cmd_ctrl(mtd
, column
, ctrl
);
825 ctrl
&= ~NAND_CTRL_CHANGE
;
827 /* Only output a single addr cycle for 8bits opcodes. */
828 if (!nand_opcode_8bits(command
))
829 chip
->cmd_ctrl(mtd
, column
>> 8, ctrl
);
831 if (page_addr
!= -1) {
832 chip
->cmd_ctrl(mtd
, page_addr
, ctrl
);
833 chip
->cmd_ctrl(mtd
, page_addr
>> 8,
834 NAND_NCE
| NAND_ALE
);
835 /* One more address cycle for devices > 128MiB */
836 if (chip
->chipsize
> (128 << 20))
837 chip
->cmd_ctrl(mtd
, page_addr
>> 16,
838 NAND_NCE
| NAND_ALE
);
841 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
844 * Program and erase have their own busy handlers status, sequential
845 * in and status need no delay.
850 case NAND_CMD_CACHEDPROG
:
851 case NAND_CMD_PAGEPROG
:
852 case NAND_CMD_ERASE1
:
853 case NAND_CMD_ERASE2
:
856 case NAND_CMD_STATUS
:
862 udelay(chip
->chip_delay
);
863 chip
->cmd_ctrl(mtd
, NAND_CMD_STATUS
,
864 NAND_NCE
| NAND_CLE
| NAND_CTRL_CHANGE
);
865 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
,
866 NAND_NCE
| NAND_CTRL_CHANGE
);
867 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
868 nand_wait_status_ready(mtd
, 250);
871 case NAND_CMD_RNDOUT
:
872 /* No ready / busy check necessary */
873 chip
->cmd_ctrl(mtd
, NAND_CMD_RNDOUTSTART
,
874 NAND_NCE
| NAND_CLE
| NAND_CTRL_CHANGE
);
875 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
,
876 NAND_NCE
| NAND_CTRL_CHANGE
);
880 chip
->cmd_ctrl(mtd
, NAND_CMD_READSTART
,
881 NAND_NCE
| NAND_CLE
| NAND_CTRL_CHANGE
);
882 chip
->cmd_ctrl(mtd
, NAND_CMD_NONE
,
883 NAND_NCE
| NAND_CTRL_CHANGE
);
885 /* This applies to read commands */
888 * If we don't have access to the busy pin, we apply the given
891 if (!chip
->dev_ready
) {
892 udelay(chip
->chip_delay
);
898 * Apply this short delay always to ensure that we do wait tWB in
899 * any case on any machine.
903 nand_wait_ready(mtd
);
907 * panic_nand_get_device - [GENERIC] Get chip for selected access
908 * @chip: the nand chip descriptor
909 * @mtd: MTD device structure
910 * @new_state: the state which is requested
912 * Used when in panic, no locks are taken.
914 static void panic_nand_get_device(struct nand_chip
*chip
,
915 struct mtd_info
*mtd
, int new_state
)
917 /* Hardware controller shared among independent devices */
918 chip
->controller
->active
= chip
;
919 chip
->state
= new_state
;
923 * nand_get_device - [GENERIC] Get chip for selected access
924 * @mtd: MTD device structure
925 * @new_state: the state which is requested
927 * Get the device and lock it for exclusive access
930 nand_get_device(struct mtd_info
*mtd
, int new_state
)
932 struct nand_chip
*chip
= mtd_to_nand(mtd
);
933 spinlock_t
*lock
= &chip
->controller
->lock
;
934 wait_queue_head_t
*wq
= &chip
->controller
->wq
;
935 DECLARE_WAITQUEUE(wait
, current
);
939 /* Hardware controller shared among independent devices */
940 if (!chip
->controller
->active
)
941 chip
->controller
->active
= chip
;
943 if (chip
->controller
->active
== chip
&& chip
->state
== FL_READY
) {
944 chip
->state
= new_state
;
948 if (new_state
== FL_PM_SUSPENDED
) {
949 if (chip
->controller
->active
->state
== FL_PM_SUSPENDED
) {
950 chip
->state
= FL_PM_SUSPENDED
;
955 set_current_state(TASK_UNINTERRUPTIBLE
);
956 add_wait_queue(wq
, &wait
);
959 remove_wait_queue(wq
, &wait
);
964 * panic_nand_wait - [GENERIC] wait until the command is done
965 * @mtd: MTD device structure
966 * @chip: NAND chip structure
969 * Wait for command done. This is a helper function for nand_wait used when
970 * we are in interrupt context. May happen when in panic and trying to write
971 * an oops through mtdoops.
973 static void panic_nand_wait(struct mtd_info
*mtd
, struct nand_chip
*chip
,
977 for (i
= 0; i
< timeo
; i
++) {
978 if (chip
->dev_ready
) {
979 if (chip
->dev_ready(mtd
))
982 if (chip
->read_byte(mtd
) & NAND_STATUS_READY
)
990 * nand_wait - [DEFAULT] wait until the command is done
991 * @mtd: MTD device structure
992 * @chip: NAND chip structure
994 * Wait for command done. This applies to erase and program only.
996 static int nand_wait(struct mtd_info
*mtd
, struct nand_chip
*chip
)
1000 unsigned long timeo
= 400;
1003 * Apply this short delay always to ensure that we do wait tWB in any
1004 * case on any machine.
1008 chip
->cmdfunc(mtd
, NAND_CMD_STATUS
, -1, -1);
1010 if (in_interrupt() || oops_in_progress
)
1011 panic_nand_wait(mtd
, chip
, timeo
);
1013 timeo
= jiffies
+ msecs_to_jiffies(timeo
);
1015 if (chip
->dev_ready
) {
1016 if (chip
->dev_ready(mtd
))
1019 if (chip
->read_byte(mtd
) & NAND_STATUS_READY
)
1023 } while (time_before(jiffies
, timeo
));
1026 status
= (int)chip
->read_byte(mtd
);
1027 /* This can happen if in case of timeout or buggy dev_ready */
1028 WARN_ON(!(status
& NAND_STATUS_READY
));
1033 * nand_reset_data_interface - Reset data interface and timings
1034 * @chip: The NAND chip
1036 * Reset the Data interface and timings to ONFI mode 0.
1038 * Returns 0 for success or negative error code otherwise.
1040 static int nand_reset_data_interface(struct nand_chip
*chip
)
1042 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1043 const struct nand_data_interface
*conf
;
1046 if (!chip
->setup_data_interface
)
1050 * The ONFI specification says:
1052 * To transition from NV-DDR or NV-DDR2 to the SDR data
1053 * interface, the host shall use the Reset (FFh) command
1054 * using SDR timing mode 0. A device in any timing mode is
1055 * required to recognize Reset (FFh) command issued in SDR
1059 * Configure the data interface in SDR mode and set the
1060 * timings to timing mode 0.
1063 conf
= nand_get_default_data_interface();
1064 ret
= chip
->setup_data_interface(mtd
, conf
, false);
1066 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1072 * nand_setup_data_interface - Setup the best data interface and timings
1073 * @chip: The NAND chip
1075 * Find and configure the best data interface and NAND timings supported by
1076 * the chip and the driver.
1077 * First tries to retrieve supported timing modes from ONFI information,
1078 * and if the NAND chip does not support ONFI, relies on the
1079 * ->onfi_timing_mode_default specified in the nand_ids table.
1081 * Returns 0 for success or negative error code otherwise.
1083 static int nand_setup_data_interface(struct nand_chip
*chip
)
1085 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1088 if (!chip
->setup_data_interface
|| !chip
->data_interface
)
1092 * Ensure the timing mode has been changed on the chip side
1093 * before changing timings on the controller side.
1095 if (chip
->onfi_version
&&
1096 (le16_to_cpu(chip
->onfi_params
.opt_cmd
) &
1097 ONFI_OPT_CMD_SET_GET_FEATURES
)) {
1098 u8 tmode_param
[ONFI_SUBFEATURE_PARAM_LEN
] = {
1099 chip
->onfi_timing_mode_default
,
1102 ret
= chip
->onfi_set_features(mtd
, chip
,
1103 ONFI_FEATURE_ADDR_TIMING_MODE
,
1109 ret
= chip
->setup_data_interface(mtd
, chip
->data_interface
, false);
1115 * nand_init_data_interface - find the best data interface and timings
1116 * @chip: The NAND chip
1118 * Find the best data interface and NAND timings supported by the chip
1120 * First tries to retrieve supported timing modes from ONFI information,
1121 * and if the NAND chip does not support ONFI, relies on the
1122 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1123 * function nand_chip->data_interface is initialized with the best timing mode
1126 * Returns 0 for success or negative error code otherwise.
1128 static int nand_init_data_interface(struct nand_chip
*chip
)
1130 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1131 int modes
, mode
, ret
;
1133 if (!chip
->setup_data_interface
)
1137 * First try to identify the best timings from ONFI parameters and
1138 * if the NAND does not support ONFI, fallback to the default ONFI
1141 modes
= onfi_get_async_timing_mode(chip
);
1142 if (modes
== ONFI_TIMING_MODE_UNKNOWN
) {
1143 if (!chip
->onfi_timing_mode_default
)
1146 modes
= GENMASK(chip
->onfi_timing_mode_default
, 0);
1149 chip
->data_interface
= kzalloc(sizeof(*chip
->data_interface
),
1151 if (!chip
->data_interface
)
1154 for (mode
= fls(modes
) - 1; mode
>= 0; mode
--) {
1155 ret
= onfi_init_data_interface(chip
, chip
->data_interface
,
1156 NAND_SDR_IFACE
, mode
);
1160 ret
= chip
->setup_data_interface(mtd
, chip
->data_interface
,
1163 chip
->onfi_timing_mode_default
= mode
;
1171 static void nand_release_data_interface(struct nand_chip
*chip
)
1173 kfree(chip
->data_interface
);
1177 * nand_reset - Reset and initialize a NAND device
1178 * @chip: The NAND chip
1179 * @chipnr: Internal die id
1181 * Returns 0 for success or negative error code otherwise
1183 int nand_reset(struct nand_chip
*chip
, int chipnr
)
1185 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1188 ret
= nand_reset_data_interface(chip
);
1193 * The CS line has to be released before we can apply the new NAND
1194 * interface settings, hence this weird ->select_chip() dance.
1196 chip
->select_chip(mtd
, chipnr
);
1197 chip
->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
1198 chip
->select_chip(mtd
, -1);
1200 chip
->select_chip(mtd
, chipnr
);
1201 ret
= nand_setup_data_interface(chip
);
1202 chip
->select_chip(mtd
, -1);
1210 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1212 * @ofs: offset to start unlock from
1213 * @len: length to unlock
1214 * @invert: when = 0, unlock the range of blocks within the lower and
1215 * upper boundary address
1216 * when = 1, unlock the range of blocks outside the boundaries
1217 * of the lower and upper boundary address
1219 * Returs unlock status.
1221 static int __nand_unlock(struct mtd_info
*mtd
, loff_t ofs
,
1222 uint64_t len
, int invert
)
1226 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1228 /* Submit address of first page to unlock */
1229 page
= ofs
>> chip
->page_shift
;
1230 chip
->cmdfunc(mtd
, NAND_CMD_UNLOCK1
, -1, page
& chip
->pagemask
);
1232 /* Submit address of last page to unlock */
1233 page
= (ofs
+ len
) >> chip
->page_shift
;
1234 chip
->cmdfunc(mtd
, NAND_CMD_UNLOCK2
, -1,
1235 (page
| invert
) & chip
->pagemask
);
1237 /* Call wait ready function */
1238 status
= chip
->waitfunc(mtd
, chip
);
1239 /* See if device thinks it succeeded */
1240 if (status
& NAND_STATUS_FAIL
) {
1241 pr_debug("%s: error status = 0x%08x\n",
1250 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1252 * @ofs: offset to start unlock from
1253 * @len: length to unlock
1255 * Returns unlock status.
1257 int nand_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1261 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1263 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1264 __func__
, (unsigned long long)ofs
, len
);
1266 if (check_offs_len(mtd
, ofs
, len
))
1269 /* Align to last block address if size addresses end of the device */
1270 if (ofs
+ len
== mtd
->size
)
1271 len
-= mtd
->erasesize
;
1273 nand_get_device(mtd
, FL_UNLOCKING
);
1275 /* Shift to get chip number */
1276 chipnr
= ofs
>> chip
->chip_shift
;
1280 * If we want to check the WP through READ STATUS and check the bit 7
1281 * we must reset the chip
1282 * some operation can also clear the bit 7 of status register
1283 * eg. erase/program a locked block
1285 nand_reset(chip
, chipnr
);
1287 chip
->select_chip(mtd
, chipnr
);
1289 /* Check, if it is write protected */
1290 if (nand_check_wp(mtd
)) {
1291 pr_debug("%s: device is write protected!\n",
1297 ret
= __nand_unlock(mtd
, ofs
, len
, 0);
1300 chip
->select_chip(mtd
, -1);
1301 nand_release_device(mtd
);
1305 EXPORT_SYMBOL(nand_unlock
);
1308 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1310 * @ofs: offset to start unlock from
1311 * @len: length to unlock
1313 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1314 * have this feature, but it allows only to lock all blocks, not for specified
1315 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1318 * Returns lock status.
1320 int nand_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1323 int chipnr
, status
, page
;
1324 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1326 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1327 __func__
, (unsigned long long)ofs
, len
);
1329 if (check_offs_len(mtd
, ofs
, len
))
1332 nand_get_device(mtd
, FL_LOCKING
);
1334 /* Shift to get chip number */
1335 chipnr
= ofs
>> chip
->chip_shift
;
1339 * If we want to check the WP through READ STATUS and check the bit 7
1340 * we must reset the chip
1341 * some operation can also clear the bit 7 of status register
1342 * eg. erase/program a locked block
1344 nand_reset(chip
, chipnr
);
1346 chip
->select_chip(mtd
, chipnr
);
1348 /* Check, if it is write protected */
1349 if (nand_check_wp(mtd
)) {
1350 pr_debug("%s: device is write protected!\n",
1352 status
= MTD_ERASE_FAILED
;
1357 /* Submit address of first page to lock */
1358 page
= ofs
>> chip
->page_shift
;
1359 chip
->cmdfunc(mtd
, NAND_CMD_LOCK
, -1, page
& chip
->pagemask
);
1361 /* Call wait ready function */
1362 status
= chip
->waitfunc(mtd
, chip
);
1363 /* See if device thinks it succeeded */
1364 if (status
& NAND_STATUS_FAIL
) {
1365 pr_debug("%s: error status = 0x%08x\n",
1371 ret
= __nand_unlock(mtd
, ofs
, len
, 0x1);
1374 chip
->select_chip(mtd
, -1);
1375 nand_release_device(mtd
);
1379 EXPORT_SYMBOL(nand_lock
);
1382 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1383 * @buf: buffer to test
1384 * @len: buffer length
1385 * @bitflips_threshold: maximum number of bitflips
1387 * Check if a buffer contains only 0xff, which means the underlying region
1388 * has been erased and is ready to be programmed.
1389 * The bitflips_threshold specify the maximum number of bitflips before
1390 * considering the region is not erased.
1391 * Note: The logic of this function has been extracted from the memweight
1392 * implementation, except that nand_check_erased_buf function exit before
1393 * testing the whole buffer if the number of bitflips exceed the
1394 * bitflips_threshold value.
1396 * Returns a positive number of bitflips less than or equal to
1397 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1400 static int nand_check_erased_buf(void *buf
, int len
, int bitflips_threshold
)
1402 const unsigned char *bitmap
= buf
;
1406 for (; len
&& ((uintptr_t)bitmap
) % sizeof(long);
1408 weight
= hweight8(*bitmap
);
1409 bitflips
+= BITS_PER_BYTE
- weight
;
1410 if (unlikely(bitflips
> bitflips_threshold
))
1414 for (; len
>= sizeof(long);
1415 len
-= sizeof(long), bitmap
+= sizeof(long)) {
1416 weight
= hweight_long(*((unsigned long *)bitmap
));
1417 bitflips
+= BITS_PER_LONG
- weight
;
1418 if (unlikely(bitflips
> bitflips_threshold
))
1422 for (; len
> 0; len
--, bitmap
++) {
1423 weight
= hweight8(*bitmap
);
1424 bitflips
+= BITS_PER_BYTE
- weight
;
1425 if (unlikely(bitflips
> bitflips_threshold
))
1433 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1435 * @data: data buffer to test
1436 * @datalen: data length
1438 * @ecclen: ECC length
1439 * @extraoob: extra OOB buffer
1440 * @extraooblen: extra OOB length
1441 * @bitflips_threshold: maximum number of bitflips
1443 * Check if a data buffer and its associated ECC and OOB data contains only
1444 * 0xff pattern, which means the underlying region has been erased and is
1445 * ready to be programmed.
1446 * The bitflips_threshold specify the maximum number of bitflips before
1447 * considering the region as not erased.
1450 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1451 * different from the NAND page size. When fixing bitflips, ECC engines will
1452 * report the number of errors per chunk, and the NAND core infrastructure
1453 * expect you to return the maximum number of bitflips for the whole page.
1454 * This is why you should always use this function on a single chunk and
1455 * not on the whole page. After checking each chunk you should update your
1456 * max_bitflips value accordingly.
1457 * 2/ When checking for bitflips in erased pages you should not only check
1458 * the payload data but also their associated ECC data, because a user might
1459 * have programmed almost all bits to 1 but a few. In this case, we
1460 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1462 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1463 * data are protected by the ECC engine.
1464 * It could also be used if you support subpages and want to attach some
1465 * extra OOB data to an ECC chunk.
1467 * Returns a positive number of bitflips less than or equal to
1468 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1469 * threshold. In case of success, the passed buffers are filled with 0xff.
1471 int nand_check_erased_ecc_chunk(void *data
, int datalen
,
1472 void *ecc
, int ecclen
,
1473 void *extraoob
, int extraooblen
,
1474 int bitflips_threshold
)
1476 int data_bitflips
= 0, ecc_bitflips
= 0, extraoob_bitflips
= 0;
1478 data_bitflips
= nand_check_erased_buf(data
, datalen
,
1479 bitflips_threshold
);
1480 if (data_bitflips
< 0)
1481 return data_bitflips
;
1483 bitflips_threshold
-= data_bitflips
;
1485 ecc_bitflips
= nand_check_erased_buf(ecc
, ecclen
, bitflips_threshold
);
1486 if (ecc_bitflips
< 0)
1487 return ecc_bitflips
;
1489 bitflips_threshold
-= ecc_bitflips
;
1491 extraoob_bitflips
= nand_check_erased_buf(extraoob
, extraooblen
,
1492 bitflips_threshold
);
1493 if (extraoob_bitflips
< 0)
1494 return extraoob_bitflips
;
1497 memset(data
, 0xff, datalen
);
1500 memset(ecc
, 0xff, ecclen
);
1502 if (extraoob_bitflips
)
1503 memset(extraoob
, 0xff, extraooblen
);
1505 return data_bitflips
+ ecc_bitflips
+ extraoob_bitflips
;
1507 EXPORT_SYMBOL(nand_check_erased_ecc_chunk
);
1510 * nand_read_page_raw - [INTERN] read raw page data without ecc
1511 * @mtd: mtd info structure
1512 * @chip: nand chip info structure
1513 * @buf: buffer to store read data
1514 * @oob_required: caller requires OOB data read to chip->oob_poi
1515 * @page: page number to read
1517 * Not for syndrome calculating ECC controllers, which use a special oob layout.
1519 static int nand_read_page_raw(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1520 uint8_t *buf
, int oob_required
, int page
)
1522 chip
->read_buf(mtd
, buf
, mtd
->writesize
);
1524 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1529 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1530 * @mtd: mtd info structure
1531 * @chip: nand chip info structure
1532 * @buf: buffer to store read data
1533 * @oob_required: caller requires OOB data read to chip->oob_poi
1534 * @page: page number to read
1536 * We need a special oob layout and handling even when OOB isn't used.
1538 static int nand_read_page_raw_syndrome(struct mtd_info
*mtd
,
1539 struct nand_chip
*chip
, uint8_t *buf
,
1540 int oob_required
, int page
)
1542 int eccsize
= chip
->ecc
.size
;
1543 int eccbytes
= chip
->ecc
.bytes
;
1544 uint8_t *oob
= chip
->oob_poi
;
1547 for (steps
= chip
->ecc
.steps
; steps
> 0; steps
--) {
1548 chip
->read_buf(mtd
, buf
, eccsize
);
1551 if (chip
->ecc
.prepad
) {
1552 chip
->read_buf(mtd
, oob
, chip
->ecc
.prepad
);
1553 oob
+= chip
->ecc
.prepad
;
1556 chip
->read_buf(mtd
, oob
, eccbytes
);
1559 if (chip
->ecc
.postpad
) {
1560 chip
->read_buf(mtd
, oob
, chip
->ecc
.postpad
);
1561 oob
+= chip
->ecc
.postpad
;
1565 size
= mtd
->oobsize
- (oob
- chip
->oob_poi
);
1567 chip
->read_buf(mtd
, oob
, size
);
1573 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1574 * @mtd: mtd info structure
1575 * @chip: nand chip info structure
1576 * @buf: buffer to store read data
1577 * @oob_required: caller requires OOB data read to chip->oob_poi
1578 * @page: page number to read
1580 static int nand_read_page_swecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1581 uint8_t *buf
, int oob_required
, int page
)
1583 int i
, eccsize
= chip
->ecc
.size
, ret
;
1584 int eccbytes
= chip
->ecc
.bytes
;
1585 int eccsteps
= chip
->ecc
.steps
;
1587 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
1588 uint8_t *ecc_code
= chip
->buffers
->ecccode
;
1589 unsigned int max_bitflips
= 0;
1591 chip
->ecc
.read_page_raw(mtd
, chip
, buf
, 1, page
);
1593 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
)
1594 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
1596 ret
= mtd_ooblayout_get_eccbytes(mtd
, ecc_code
, chip
->oob_poi
, 0,
1601 eccsteps
= chip
->ecc
.steps
;
1604 for (i
= 0 ; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
1607 stat
= chip
->ecc
.correct(mtd
, p
, &ecc_code
[i
], &ecc_calc
[i
]);
1609 mtd
->ecc_stats
.failed
++;
1611 mtd
->ecc_stats
.corrected
+= stat
;
1612 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1615 return max_bitflips
;
1619 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1620 * @mtd: mtd info structure
1621 * @chip: nand chip info structure
1622 * @data_offs: offset of requested data within the page
1623 * @readlen: data length
1624 * @bufpoi: buffer to store read data
1625 * @page: page number to read
1627 static int nand_read_subpage(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1628 uint32_t data_offs
, uint32_t readlen
, uint8_t *bufpoi
,
1631 int start_step
, end_step
, num_steps
, ret
;
1633 int data_col_addr
, i
, gaps
= 0;
1634 int datafrag_len
, eccfrag_len
, aligned_len
, aligned_pos
;
1635 int busw
= (chip
->options
& NAND_BUSWIDTH_16
) ? 2 : 1;
1636 int index
, section
= 0;
1637 unsigned int max_bitflips
= 0;
1638 struct mtd_oob_region oobregion
= { };
1640 /* Column address within the page aligned to ECC size (256bytes) */
1641 start_step
= data_offs
/ chip
->ecc
.size
;
1642 end_step
= (data_offs
+ readlen
- 1) / chip
->ecc
.size
;
1643 num_steps
= end_step
- start_step
+ 1;
1644 index
= start_step
* chip
->ecc
.bytes
;
1646 /* Data size aligned to ECC ecc.size */
1647 datafrag_len
= num_steps
* chip
->ecc
.size
;
1648 eccfrag_len
= num_steps
* chip
->ecc
.bytes
;
1650 data_col_addr
= start_step
* chip
->ecc
.size
;
1651 /* If we read not a page aligned data */
1652 if (data_col_addr
!= 0)
1653 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, data_col_addr
, -1);
1655 p
= bufpoi
+ data_col_addr
;
1656 chip
->read_buf(mtd
, p
, datafrag_len
);
1659 for (i
= 0; i
< eccfrag_len
; i
+= chip
->ecc
.bytes
, p
+= chip
->ecc
.size
)
1660 chip
->ecc
.calculate(mtd
, p
, &chip
->buffers
->ecccalc
[i
]);
1663 * The performance is faster if we position offsets according to
1664 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1666 ret
= mtd_ooblayout_find_eccregion(mtd
, index
, §ion
, &oobregion
);
1670 if (oobregion
.length
< eccfrag_len
)
1674 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
1675 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1678 * Send the command to read the particular ECC bytes take care
1679 * about buswidth alignment in read_buf.
1681 aligned_pos
= oobregion
.offset
& ~(busw
- 1);
1682 aligned_len
= eccfrag_len
;
1683 if (oobregion
.offset
& (busw
- 1))
1685 if ((oobregion
.offset
+ (num_steps
* chip
->ecc
.bytes
)) &
1689 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
1690 mtd
->writesize
+ aligned_pos
, -1);
1691 chip
->read_buf(mtd
, &chip
->oob_poi
[aligned_pos
], aligned_len
);
1694 ret
= mtd_ooblayout_get_eccbytes(mtd
, chip
->buffers
->ecccode
,
1695 chip
->oob_poi
, index
, eccfrag_len
);
1699 p
= bufpoi
+ data_col_addr
;
1700 for (i
= 0; i
< eccfrag_len
; i
+= chip
->ecc
.bytes
, p
+= chip
->ecc
.size
) {
1703 stat
= chip
->ecc
.correct(mtd
, p
,
1704 &chip
->buffers
->ecccode
[i
], &chip
->buffers
->ecccalc
[i
]);
1705 if (stat
== -EBADMSG
&&
1706 (chip
->ecc
.options
& NAND_ECC_GENERIC_ERASED_CHECK
)) {
1707 /* check for empty pages with bitflips */
1708 stat
= nand_check_erased_ecc_chunk(p
, chip
->ecc
.size
,
1709 &chip
->buffers
->ecccode
[i
],
1712 chip
->ecc
.strength
);
1716 mtd
->ecc_stats
.failed
++;
1718 mtd
->ecc_stats
.corrected
+= stat
;
1719 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1722 return max_bitflips
;
1726 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1727 * @mtd: mtd info structure
1728 * @chip: nand chip info structure
1729 * @buf: buffer to store read data
1730 * @oob_required: caller requires OOB data read to chip->oob_poi
1731 * @page: page number to read
1733 * Not for syndrome calculating ECC controllers which need a special oob layout.
1735 static int nand_read_page_hwecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1736 uint8_t *buf
, int oob_required
, int page
)
1738 int i
, eccsize
= chip
->ecc
.size
, ret
;
1739 int eccbytes
= chip
->ecc
.bytes
;
1740 int eccsteps
= chip
->ecc
.steps
;
1742 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
1743 uint8_t *ecc_code
= chip
->buffers
->ecccode
;
1744 unsigned int max_bitflips
= 0;
1746 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
1747 chip
->ecc
.hwctl(mtd
, NAND_ECC_READ
);
1748 chip
->read_buf(mtd
, p
, eccsize
);
1749 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
1751 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1753 ret
= mtd_ooblayout_get_eccbytes(mtd
, ecc_code
, chip
->oob_poi
, 0,
1758 eccsteps
= chip
->ecc
.steps
;
1761 for (i
= 0 ; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
1764 stat
= chip
->ecc
.correct(mtd
, p
, &ecc_code
[i
], &ecc_calc
[i
]);
1765 if (stat
== -EBADMSG
&&
1766 (chip
->ecc
.options
& NAND_ECC_GENERIC_ERASED_CHECK
)) {
1767 /* check for empty pages with bitflips */
1768 stat
= nand_check_erased_ecc_chunk(p
, eccsize
,
1769 &ecc_code
[i
], eccbytes
,
1771 chip
->ecc
.strength
);
1775 mtd
->ecc_stats
.failed
++;
1777 mtd
->ecc_stats
.corrected
+= stat
;
1778 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1781 return max_bitflips
;
1785 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1786 * @mtd: mtd info structure
1787 * @chip: nand chip info structure
1788 * @buf: buffer to store read data
1789 * @oob_required: caller requires OOB data read to chip->oob_poi
1790 * @page: page number to read
1792 * Hardware ECC for large page chips, require OOB to be read first. For this
1793 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1794 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1795 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1796 * the data area, by overwriting the NAND manufacturer bad block markings.
1798 static int nand_read_page_hwecc_oob_first(struct mtd_info
*mtd
,
1799 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
1801 int i
, eccsize
= chip
->ecc
.size
, ret
;
1802 int eccbytes
= chip
->ecc
.bytes
;
1803 int eccsteps
= chip
->ecc
.steps
;
1805 uint8_t *ecc_code
= chip
->buffers
->ecccode
;
1806 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
1807 unsigned int max_bitflips
= 0;
1809 /* Read the OOB area first */
1810 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, 0, page
);
1811 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1812 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, 0, page
);
1814 ret
= mtd_ooblayout_get_eccbytes(mtd
, ecc_code
, chip
->oob_poi
, 0,
1819 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
1822 chip
->ecc
.hwctl(mtd
, NAND_ECC_READ
);
1823 chip
->read_buf(mtd
, p
, eccsize
);
1824 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
1826 stat
= chip
->ecc
.correct(mtd
, p
, &ecc_code
[i
], NULL
);
1827 if (stat
== -EBADMSG
&&
1828 (chip
->ecc
.options
& NAND_ECC_GENERIC_ERASED_CHECK
)) {
1829 /* check for empty pages with bitflips */
1830 stat
= nand_check_erased_ecc_chunk(p
, eccsize
,
1831 &ecc_code
[i
], eccbytes
,
1833 chip
->ecc
.strength
);
1837 mtd
->ecc_stats
.failed
++;
1839 mtd
->ecc_stats
.corrected
+= stat
;
1840 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1843 return max_bitflips
;
1847 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1848 * @mtd: mtd info structure
1849 * @chip: nand chip info structure
1850 * @buf: buffer to store read data
1851 * @oob_required: caller requires OOB data read to chip->oob_poi
1852 * @page: page number to read
1854 * The hw generator calculates the error syndrome automatically. Therefore we
1855 * need a special oob layout and handling.
1857 static int nand_read_page_syndrome(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1858 uint8_t *buf
, int oob_required
, int page
)
1860 int i
, eccsize
= chip
->ecc
.size
;
1861 int eccbytes
= chip
->ecc
.bytes
;
1862 int eccsteps
= chip
->ecc
.steps
;
1863 int eccpadbytes
= eccbytes
+ chip
->ecc
.prepad
+ chip
->ecc
.postpad
;
1865 uint8_t *oob
= chip
->oob_poi
;
1866 unsigned int max_bitflips
= 0;
1868 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
1871 chip
->ecc
.hwctl(mtd
, NAND_ECC_READ
);
1872 chip
->read_buf(mtd
, p
, eccsize
);
1874 if (chip
->ecc
.prepad
) {
1875 chip
->read_buf(mtd
, oob
, chip
->ecc
.prepad
);
1876 oob
+= chip
->ecc
.prepad
;
1879 chip
->ecc
.hwctl(mtd
, NAND_ECC_READSYN
);
1880 chip
->read_buf(mtd
, oob
, eccbytes
);
1881 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
1885 if (chip
->ecc
.postpad
) {
1886 chip
->read_buf(mtd
, oob
, chip
->ecc
.postpad
);
1887 oob
+= chip
->ecc
.postpad
;
1890 if (stat
== -EBADMSG
&&
1891 (chip
->ecc
.options
& NAND_ECC_GENERIC_ERASED_CHECK
)) {
1892 /* check for empty pages with bitflips */
1893 stat
= nand_check_erased_ecc_chunk(p
, chip
->ecc
.size
,
1897 chip
->ecc
.strength
);
1901 mtd
->ecc_stats
.failed
++;
1903 mtd
->ecc_stats
.corrected
+= stat
;
1904 max_bitflips
= max_t(unsigned int, max_bitflips
, stat
);
1908 /* Calculate remaining oob bytes */
1909 i
= mtd
->oobsize
- (oob
- chip
->oob_poi
);
1911 chip
->read_buf(mtd
, oob
, i
);
1913 return max_bitflips
;
1917 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1918 * @mtd: mtd info structure
1919 * @oob: oob destination address
1920 * @ops: oob ops structure
1921 * @len: size of oob to transfer
1923 static uint8_t *nand_transfer_oob(struct mtd_info
*mtd
, uint8_t *oob
,
1924 struct mtd_oob_ops
*ops
, size_t len
)
1926 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1929 switch (ops
->mode
) {
1931 case MTD_OPS_PLACE_OOB
:
1933 memcpy(oob
, chip
->oob_poi
+ ops
->ooboffs
, len
);
1936 case MTD_OPS_AUTO_OOB
:
1937 ret
= mtd_ooblayout_get_databytes(mtd
, oob
, chip
->oob_poi
,
1949 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1950 * @mtd: MTD device structure
1951 * @retry_mode: the retry mode to use
1953 * Some vendors supply a special command to shift the Vt threshold, to be used
1954 * when there are too many bitflips in a page (i.e., ECC error). After setting
1955 * a new threshold, the host should retry reading the page.
1957 static int nand_setup_read_retry(struct mtd_info
*mtd
, int retry_mode
)
1959 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1961 pr_debug("setting READ RETRY mode %d\n", retry_mode
);
1963 if (retry_mode
>= chip
->read_retries
)
1966 if (!chip
->setup_read_retry
)
1969 return chip
->setup_read_retry(mtd
, retry_mode
);
1973 * nand_do_read_ops - [INTERN] Read data with ECC
1974 * @mtd: MTD device structure
1975 * @from: offset to read from
1976 * @ops: oob ops structure
1978 * Internal function. Called with chip held.
1980 static int nand_do_read_ops(struct mtd_info
*mtd
, loff_t from
,
1981 struct mtd_oob_ops
*ops
)
1983 int chipnr
, page
, realpage
, col
, bytes
, aligned
, oob_required
;
1984 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1986 uint32_t readlen
= ops
->len
;
1987 uint32_t oobreadlen
= ops
->ooblen
;
1988 uint32_t max_oobsize
= mtd_oobavail(mtd
, ops
);
1990 uint8_t *bufpoi
, *oob
, *buf
;
1992 unsigned int max_bitflips
= 0;
1994 bool ecc_fail
= false;
1996 chipnr
= (int)(from
>> chip
->chip_shift
);
1997 chip
->select_chip(mtd
, chipnr
);
1999 realpage
= (int)(from
>> chip
->page_shift
);
2000 page
= realpage
& chip
->pagemask
;
2002 col
= (int)(from
& (mtd
->writesize
- 1));
2006 oob_required
= oob
? 1 : 0;
2009 unsigned int ecc_failures
= mtd
->ecc_stats
.failed
;
2011 bytes
= min(mtd
->writesize
- col
, readlen
);
2012 aligned
= (bytes
== mtd
->writesize
);
2016 else if (chip
->options
& NAND_USE_BOUNCE_BUFFER
)
2017 use_bufpoi
= !virt_addr_valid(buf
);
2021 /* Is the current page in the buffer? */
2022 if (realpage
!= chip
->pagebuf
|| oob
) {
2023 bufpoi
= use_bufpoi
? chip
->buffers
->databuf
: buf
;
2025 if (use_bufpoi
&& aligned
)
2026 pr_debug("%s: using read bounce buffer for buf@%p\n",
2030 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, 0x00, page
);
2033 * Now read the page into the buffer. Absent an error,
2034 * the read methods return max bitflips per ecc step.
2036 if (unlikely(ops
->mode
== MTD_OPS_RAW
))
2037 ret
= chip
->ecc
.read_page_raw(mtd
, chip
, bufpoi
,
2040 else if (!aligned
&& NAND_HAS_SUBPAGE_READ(chip
) &&
2042 ret
= chip
->ecc
.read_subpage(mtd
, chip
,
2046 ret
= chip
->ecc
.read_page(mtd
, chip
, bufpoi
,
2047 oob_required
, page
);
2050 /* Invalidate page cache */
2055 max_bitflips
= max_t(unsigned int, max_bitflips
, ret
);
2057 /* Transfer not aligned data */
2059 if (!NAND_HAS_SUBPAGE_READ(chip
) && !oob
&&
2060 !(mtd
->ecc_stats
.failed
- ecc_failures
) &&
2061 (ops
->mode
!= MTD_OPS_RAW
)) {
2062 chip
->pagebuf
= realpage
;
2063 chip
->pagebuf_bitflips
= ret
;
2065 /* Invalidate page cache */
2068 memcpy(buf
, chip
->buffers
->databuf
+ col
, bytes
);
2071 if (unlikely(oob
)) {
2072 int toread
= min(oobreadlen
, max_oobsize
);
2075 oob
= nand_transfer_oob(mtd
,
2077 oobreadlen
-= toread
;
2081 if (chip
->options
& NAND_NEED_READRDY
) {
2082 /* Apply delay or wait for ready/busy pin */
2083 if (!chip
->dev_ready
)
2084 udelay(chip
->chip_delay
);
2086 nand_wait_ready(mtd
);
2089 if (mtd
->ecc_stats
.failed
- ecc_failures
) {
2090 if (retry_mode
+ 1 < chip
->read_retries
) {
2092 ret
= nand_setup_read_retry(mtd
,
2097 /* Reset failures; retry */
2098 mtd
->ecc_stats
.failed
= ecc_failures
;
2101 /* No more retry modes; real failure */
2108 memcpy(buf
, chip
->buffers
->databuf
+ col
, bytes
);
2110 max_bitflips
= max_t(unsigned int, max_bitflips
,
2111 chip
->pagebuf_bitflips
);
2116 /* Reset to retry mode 0 */
2118 ret
= nand_setup_read_retry(mtd
, 0);
2127 /* For subsequent reads align to page boundary */
2129 /* Increment page address */
2132 page
= realpage
& chip
->pagemask
;
2133 /* Check, if we cross a chip boundary */
2136 chip
->select_chip(mtd
, -1);
2137 chip
->select_chip(mtd
, chipnr
);
2140 chip
->select_chip(mtd
, -1);
2142 ops
->retlen
= ops
->len
- (size_t) readlen
;
2144 ops
->oobretlen
= ops
->ooblen
- oobreadlen
;
2152 return max_bitflips
;
2156 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
2157 * @mtd: MTD device structure
2158 * @from: offset to read from
2159 * @len: number of bytes to read
2160 * @retlen: pointer to variable to store the number of read bytes
2161 * @buf: the databuffer to put data
2163 * Get hold of the chip and call nand_do_read.
2165 static int nand_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2166 size_t *retlen
, uint8_t *buf
)
2168 struct mtd_oob_ops ops
;
2171 nand_get_device(mtd
, FL_READING
);
2172 memset(&ops
, 0, sizeof(ops
));
2175 ops
.mode
= MTD_OPS_PLACE_OOB
;
2176 ret
= nand_do_read_ops(mtd
, from
, &ops
);
2177 *retlen
= ops
.retlen
;
2178 nand_release_device(mtd
);
2183 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2184 * @mtd: mtd info structure
2185 * @chip: nand chip info structure
2186 * @page: page number to read
2188 int nand_read_oob_std(struct mtd_info
*mtd
, struct nand_chip
*chip
, int page
)
2190 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, 0, page
);
2191 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
2194 EXPORT_SYMBOL(nand_read_oob_std
);
2197 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
2199 * @mtd: mtd info structure
2200 * @chip: nand chip info structure
2201 * @page: page number to read
2203 int nand_read_oob_syndrome(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2206 int length
= mtd
->oobsize
;
2207 int chunk
= chip
->ecc
.bytes
+ chip
->ecc
.prepad
+ chip
->ecc
.postpad
;
2208 int eccsize
= chip
->ecc
.size
;
2209 uint8_t *bufpoi
= chip
->oob_poi
;
2210 int i
, toread
, sndrnd
= 0, pos
;
2212 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, chip
->ecc
.size
, page
);
2213 for (i
= 0; i
< chip
->ecc
.steps
; i
++) {
2215 pos
= eccsize
+ i
* (eccsize
+ chunk
);
2216 if (mtd
->writesize
> 512)
2217 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, pos
, -1);
2219 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, pos
, page
);
2222 toread
= min_t(int, length
, chunk
);
2223 chip
->read_buf(mtd
, bufpoi
, toread
);
2228 chip
->read_buf(mtd
, bufpoi
, length
);
2232 EXPORT_SYMBOL(nand_read_oob_syndrome
);
2235 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2236 * @mtd: mtd info structure
2237 * @chip: nand chip info structure
2238 * @page: page number to write
2240 int nand_write_oob_std(struct mtd_info
*mtd
, struct nand_chip
*chip
, int page
)
2243 const uint8_t *buf
= chip
->oob_poi
;
2244 int length
= mtd
->oobsize
;
2246 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, mtd
->writesize
, page
);
2247 chip
->write_buf(mtd
, buf
, length
);
2248 /* Send command to program the OOB data */
2249 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
2251 status
= chip
->waitfunc(mtd
, chip
);
2253 return status
& NAND_STATUS_FAIL
? -EIO
: 0;
2255 EXPORT_SYMBOL(nand_write_oob_std
);
2258 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2259 * with syndrome - only for large page flash
2260 * @mtd: mtd info structure
2261 * @chip: nand chip info structure
2262 * @page: page number to write
2264 int nand_write_oob_syndrome(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2267 int chunk
= chip
->ecc
.bytes
+ chip
->ecc
.prepad
+ chip
->ecc
.postpad
;
2268 int eccsize
= chip
->ecc
.size
, length
= mtd
->oobsize
;
2269 int i
, len
, pos
, status
= 0, sndcmd
= 0, steps
= chip
->ecc
.steps
;
2270 const uint8_t *bufpoi
= chip
->oob_poi
;
2273 * data-ecc-data-ecc ... ecc-oob
2275 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2277 if (!chip
->ecc
.prepad
&& !chip
->ecc
.postpad
) {
2278 pos
= steps
* (eccsize
+ chunk
);
2283 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, pos
, page
);
2284 for (i
= 0; i
< steps
; i
++) {
2286 if (mtd
->writesize
<= 512) {
2287 uint32_t fill
= 0xFFFFFFFF;
2291 int num
= min_t(int, len
, 4);
2292 chip
->write_buf(mtd
, (uint8_t *)&fill
,
2297 pos
= eccsize
+ i
* (eccsize
+ chunk
);
2298 chip
->cmdfunc(mtd
, NAND_CMD_RNDIN
, pos
, -1);
2302 len
= min_t(int, length
, chunk
);
2303 chip
->write_buf(mtd
, bufpoi
, len
);
2308 chip
->write_buf(mtd
, bufpoi
, length
);
2310 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
2311 status
= chip
->waitfunc(mtd
, chip
);
2313 return status
& NAND_STATUS_FAIL
? -EIO
: 0;
2315 EXPORT_SYMBOL(nand_write_oob_syndrome
);
2318 * nand_do_read_oob - [INTERN] NAND read out-of-band
2319 * @mtd: MTD device structure
2320 * @from: offset to read from
2321 * @ops: oob operations description structure
2323 * NAND read out-of-band data from the spare area.
2325 static int nand_do_read_oob(struct mtd_info
*mtd
, loff_t from
,
2326 struct mtd_oob_ops
*ops
)
2328 unsigned int max_bitflips
= 0;
2329 int page
, realpage
, chipnr
;
2330 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2331 struct mtd_ecc_stats stats
;
2332 int readlen
= ops
->ooblen
;
2334 uint8_t *buf
= ops
->oobbuf
;
2337 pr_debug("%s: from = 0x%08Lx, len = %i\n",
2338 __func__
, (unsigned long long)from
, readlen
);
2340 stats
= mtd
->ecc_stats
;
2342 len
= mtd_oobavail(mtd
, ops
);
2344 if (unlikely(ops
->ooboffs
>= len
)) {
2345 pr_debug("%s: attempt to start read outside oob\n",
2350 /* Do not allow reads past end of device */
2351 if (unlikely(from
>= mtd
->size
||
2352 ops
->ooboffs
+ readlen
> ((mtd
->size
>> chip
->page_shift
) -
2353 (from
>> chip
->page_shift
)) * len
)) {
2354 pr_debug("%s: attempt to read beyond end of device\n",
2359 chipnr
= (int)(from
>> chip
->chip_shift
);
2360 chip
->select_chip(mtd
, chipnr
);
2362 /* Shift to get page */
2363 realpage
= (int)(from
>> chip
->page_shift
);
2364 page
= realpage
& chip
->pagemask
;
2367 if (ops
->mode
== MTD_OPS_RAW
)
2368 ret
= chip
->ecc
.read_oob_raw(mtd
, chip
, page
);
2370 ret
= chip
->ecc
.read_oob(mtd
, chip
, page
);
2375 len
= min(len
, readlen
);
2376 buf
= nand_transfer_oob(mtd
, buf
, ops
, len
);
2378 if (chip
->options
& NAND_NEED_READRDY
) {
2379 /* Apply delay or wait for ready/busy pin */
2380 if (!chip
->dev_ready
)
2381 udelay(chip
->chip_delay
);
2383 nand_wait_ready(mtd
);
2386 max_bitflips
= max_t(unsigned int, max_bitflips
, ret
);
2392 /* Increment page address */
2395 page
= realpage
& chip
->pagemask
;
2396 /* Check, if we cross a chip boundary */
2399 chip
->select_chip(mtd
, -1);
2400 chip
->select_chip(mtd
, chipnr
);
2403 chip
->select_chip(mtd
, -1);
2405 ops
->oobretlen
= ops
->ooblen
- readlen
;
2410 if (mtd
->ecc_stats
.failed
- stats
.failed
)
2413 return max_bitflips
;
2417 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
2418 * @mtd: MTD device structure
2419 * @from: offset to read from
2420 * @ops: oob operation description structure
2422 * NAND read data and/or out-of-band data.
2424 static int nand_read_oob(struct mtd_info
*mtd
, loff_t from
,
2425 struct mtd_oob_ops
*ops
)
2431 /* Do not allow reads past end of device */
2432 if (ops
->datbuf
&& (from
+ ops
->len
) > mtd
->size
) {
2433 pr_debug("%s: attempt to read beyond end of device\n",
2438 if (ops
->mode
!= MTD_OPS_PLACE_OOB
&&
2439 ops
->mode
!= MTD_OPS_AUTO_OOB
&&
2440 ops
->mode
!= MTD_OPS_RAW
)
2443 nand_get_device(mtd
, FL_READING
);
2446 ret
= nand_do_read_oob(mtd
, from
, ops
);
2448 ret
= nand_do_read_ops(mtd
, from
, ops
);
2450 nand_release_device(mtd
);
2456 * nand_write_page_raw - [INTERN] raw page write function
2457 * @mtd: mtd info structure
2458 * @chip: nand chip info structure
2460 * @oob_required: must write chip->oob_poi to OOB
2461 * @page: page number to write
2463 * Not for syndrome calculating ECC controllers, which use a special oob layout.
2465 static int nand_write_page_raw(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2466 const uint8_t *buf
, int oob_required
, int page
)
2468 chip
->write_buf(mtd
, buf
, mtd
->writesize
);
2470 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
2476 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2477 * @mtd: mtd info structure
2478 * @chip: nand chip info structure
2480 * @oob_required: must write chip->oob_poi to OOB
2481 * @page: page number to write
2483 * We need a special oob layout and handling even when ECC isn't checked.
2485 static int nand_write_page_raw_syndrome(struct mtd_info
*mtd
,
2486 struct nand_chip
*chip
,
2487 const uint8_t *buf
, int oob_required
,
2490 int eccsize
= chip
->ecc
.size
;
2491 int eccbytes
= chip
->ecc
.bytes
;
2492 uint8_t *oob
= chip
->oob_poi
;
2495 for (steps
= chip
->ecc
.steps
; steps
> 0; steps
--) {
2496 chip
->write_buf(mtd
, buf
, eccsize
);
2499 if (chip
->ecc
.prepad
) {
2500 chip
->write_buf(mtd
, oob
, chip
->ecc
.prepad
);
2501 oob
+= chip
->ecc
.prepad
;
2504 chip
->write_buf(mtd
, oob
, eccbytes
);
2507 if (chip
->ecc
.postpad
) {
2508 chip
->write_buf(mtd
, oob
, chip
->ecc
.postpad
);
2509 oob
+= chip
->ecc
.postpad
;
2513 size
= mtd
->oobsize
- (oob
- chip
->oob_poi
);
2515 chip
->write_buf(mtd
, oob
, size
);
2520 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2521 * @mtd: mtd info structure
2522 * @chip: nand chip info structure
2524 * @oob_required: must write chip->oob_poi to OOB
2525 * @page: page number to write
2527 static int nand_write_page_swecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2528 const uint8_t *buf
, int oob_required
,
2531 int i
, eccsize
= chip
->ecc
.size
, ret
;
2532 int eccbytes
= chip
->ecc
.bytes
;
2533 int eccsteps
= chip
->ecc
.steps
;
2534 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
2535 const uint8_t *p
= buf
;
2537 /* Software ECC calculation */
2538 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
)
2539 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
2541 ret
= mtd_ooblayout_set_eccbytes(mtd
, ecc_calc
, chip
->oob_poi
, 0,
2546 return chip
->ecc
.write_page_raw(mtd
, chip
, buf
, 1, page
);
2550 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2551 * @mtd: mtd info structure
2552 * @chip: nand chip info structure
2554 * @oob_required: must write chip->oob_poi to OOB
2555 * @page: page number to write
2557 static int nand_write_page_hwecc(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2558 const uint8_t *buf
, int oob_required
,
2561 int i
, eccsize
= chip
->ecc
.size
, ret
;
2562 int eccbytes
= chip
->ecc
.bytes
;
2563 int eccsteps
= chip
->ecc
.steps
;
2564 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
2565 const uint8_t *p
= buf
;
2567 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
2568 chip
->ecc
.hwctl(mtd
, NAND_ECC_WRITE
);
2569 chip
->write_buf(mtd
, p
, eccsize
);
2570 chip
->ecc
.calculate(mtd
, p
, &ecc_calc
[i
]);
2573 ret
= mtd_ooblayout_set_eccbytes(mtd
, ecc_calc
, chip
->oob_poi
, 0,
2578 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
2585 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
2586 * @mtd: mtd info structure
2587 * @chip: nand chip info structure
2588 * @offset: column address of subpage within the page
2589 * @data_len: data length
2591 * @oob_required: must write chip->oob_poi to OOB
2592 * @page: page number to write
2594 static int nand_write_subpage_hwecc(struct mtd_info
*mtd
,
2595 struct nand_chip
*chip
, uint32_t offset
,
2596 uint32_t data_len
, const uint8_t *buf
,
2597 int oob_required
, int page
)
2599 uint8_t *oob_buf
= chip
->oob_poi
;
2600 uint8_t *ecc_calc
= chip
->buffers
->ecccalc
;
2601 int ecc_size
= chip
->ecc
.size
;
2602 int ecc_bytes
= chip
->ecc
.bytes
;
2603 int ecc_steps
= chip
->ecc
.steps
;
2604 uint32_t start_step
= offset
/ ecc_size
;
2605 uint32_t end_step
= (offset
+ data_len
- 1) / ecc_size
;
2606 int oob_bytes
= mtd
->oobsize
/ ecc_steps
;
2609 for (step
= 0; step
< ecc_steps
; step
++) {
2610 /* configure controller for WRITE access */
2611 chip
->ecc
.hwctl(mtd
, NAND_ECC_WRITE
);
2613 /* write data (untouched subpages already masked by 0xFF) */
2614 chip
->write_buf(mtd
, buf
, ecc_size
);
2616 /* mask ECC of un-touched subpages by padding 0xFF */
2617 if ((step
< start_step
) || (step
> end_step
))
2618 memset(ecc_calc
, 0xff, ecc_bytes
);
2620 chip
->ecc
.calculate(mtd
, buf
, ecc_calc
);
2622 /* mask OOB of un-touched subpages by padding 0xFF */
2623 /* if oob_required, preserve OOB metadata of written subpage */
2624 if (!oob_required
|| (step
< start_step
) || (step
> end_step
))
2625 memset(oob_buf
, 0xff, oob_bytes
);
2628 ecc_calc
+= ecc_bytes
;
2629 oob_buf
+= oob_bytes
;
2632 /* copy calculated ECC for whole page to chip->buffer->oob */
2633 /* this include masked-value(0xFF) for unwritten subpages */
2634 ecc_calc
= chip
->buffers
->ecccalc
;
2635 ret
= mtd_ooblayout_set_eccbytes(mtd
, ecc_calc
, chip
->oob_poi
, 0,
2640 /* write OOB buffer to NAND device */
2641 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
2648 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2649 * @mtd: mtd info structure
2650 * @chip: nand chip info structure
2652 * @oob_required: must write chip->oob_poi to OOB
2653 * @page: page number to write
2655 * The hw generator calculates the error syndrome automatically. Therefore we
2656 * need a special oob layout and handling.
2658 static int nand_write_page_syndrome(struct mtd_info
*mtd
,
2659 struct nand_chip
*chip
,
2660 const uint8_t *buf
, int oob_required
,
2663 int i
, eccsize
= chip
->ecc
.size
;
2664 int eccbytes
= chip
->ecc
.bytes
;
2665 int eccsteps
= chip
->ecc
.steps
;
2666 const uint8_t *p
= buf
;
2667 uint8_t *oob
= chip
->oob_poi
;
2669 for (i
= 0; eccsteps
; eccsteps
--, i
+= eccbytes
, p
+= eccsize
) {
2671 chip
->ecc
.hwctl(mtd
, NAND_ECC_WRITE
);
2672 chip
->write_buf(mtd
, p
, eccsize
);
2674 if (chip
->ecc
.prepad
) {
2675 chip
->write_buf(mtd
, oob
, chip
->ecc
.prepad
);
2676 oob
+= chip
->ecc
.prepad
;
2679 chip
->ecc
.calculate(mtd
, p
, oob
);
2680 chip
->write_buf(mtd
, oob
, eccbytes
);
2683 if (chip
->ecc
.postpad
) {
2684 chip
->write_buf(mtd
, oob
, chip
->ecc
.postpad
);
2685 oob
+= chip
->ecc
.postpad
;
2689 /* Calculate remaining oob bytes */
2690 i
= mtd
->oobsize
- (oob
- chip
->oob_poi
);
2692 chip
->write_buf(mtd
, oob
, i
);
2698 * nand_write_page - [REPLACEABLE] write one page
2699 * @mtd: MTD device structure
2700 * @chip: NAND chip descriptor
2701 * @offset: address offset within the page
2702 * @data_len: length of actual data to be written
2703 * @buf: the data to write
2704 * @oob_required: must write chip->oob_poi to OOB
2705 * @page: page number to write
2706 * @cached: cached programming
2707 * @raw: use _raw version of write_page
2709 static int nand_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2710 uint32_t offset
, int data_len
, const uint8_t *buf
,
2711 int oob_required
, int page
, int cached
, int raw
)
2713 int status
, subpage
;
2715 if (!(chip
->options
& NAND_NO_SUBPAGE_WRITE
) &&
2716 chip
->ecc
.write_subpage
)
2717 subpage
= offset
|| (data_len
< mtd
->writesize
);
2721 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, 0x00, page
);
2724 status
= chip
->ecc
.write_page_raw(mtd
, chip
, buf
,
2725 oob_required
, page
);
2727 status
= chip
->ecc
.write_subpage(mtd
, chip
, offset
, data_len
,
2728 buf
, oob_required
, page
);
2730 status
= chip
->ecc
.write_page(mtd
, chip
, buf
, oob_required
,
2737 * Cached progamming disabled for now. Not sure if it's worth the
2738 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
2742 if (!cached
|| !NAND_HAS_CACHEPROG(chip
)) {
2744 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
2745 status
= chip
->waitfunc(mtd
, chip
);
2747 * See if operation failed and additional status checks are
2750 if ((status
& NAND_STATUS_FAIL
) && (chip
->errstat
))
2751 status
= chip
->errstat(mtd
, chip
, FL_WRITING
, status
,
2754 if (status
& NAND_STATUS_FAIL
)
2757 chip
->cmdfunc(mtd
, NAND_CMD_CACHEDPROG
, -1, -1);
2758 status
= chip
->waitfunc(mtd
, chip
);
2765 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2766 * @mtd: MTD device structure
2767 * @oob: oob data buffer
2768 * @len: oob data write length
2769 * @ops: oob ops structure
2771 static uint8_t *nand_fill_oob(struct mtd_info
*mtd
, uint8_t *oob
, size_t len
,
2772 struct mtd_oob_ops
*ops
)
2774 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2778 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2779 * data from a previous OOB read.
2781 memset(chip
->oob_poi
, 0xff, mtd
->oobsize
);
2783 switch (ops
->mode
) {
2785 case MTD_OPS_PLACE_OOB
:
2787 memcpy(chip
->oob_poi
+ ops
->ooboffs
, oob
, len
);
2790 case MTD_OPS_AUTO_OOB
:
2791 ret
= mtd_ooblayout_set_databytes(mtd
, oob
, chip
->oob_poi
,
2802 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
2805 * nand_do_write_ops - [INTERN] NAND write with ECC
2806 * @mtd: MTD device structure
2807 * @to: offset to write to
2808 * @ops: oob operations description structure
2810 * NAND write with ECC.
2812 static int nand_do_write_ops(struct mtd_info
*mtd
, loff_t to
,
2813 struct mtd_oob_ops
*ops
)
2815 int chipnr
, realpage
, page
, blockmask
, column
;
2816 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2817 uint32_t writelen
= ops
->len
;
2819 uint32_t oobwritelen
= ops
->ooblen
;
2820 uint32_t oobmaxlen
= mtd_oobavail(mtd
, ops
);
2822 uint8_t *oob
= ops
->oobbuf
;
2823 uint8_t *buf
= ops
->datbuf
;
2825 int oob_required
= oob
? 1 : 0;
2831 /* Reject writes, which are not page aligned */
2832 if (NOTALIGNED(to
) || NOTALIGNED(ops
->len
)) {
2833 pr_notice("%s: attempt to write non page aligned data\n",
2838 column
= to
& (mtd
->writesize
- 1);
2840 chipnr
= (int)(to
>> chip
->chip_shift
);
2841 chip
->select_chip(mtd
, chipnr
);
2843 /* Check, if it is write protected */
2844 if (nand_check_wp(mtd
)) {
2849 realpage
= (int)(to
>> chip
->page_shift
);
2850 page
= realpage
& chip
->pagemask
;
2851 blockmask
= (1 << (chip
->phys_erase_shift
- chip
->page_shift
)) - 1;
2853 /* Invalidate the page cache, when we write to the cached page */
2854 if (to
<= ((loff_t
)chip
->pagebuf
<< chip
->page_shift
) &&
2855 ((loff_t
)chip
->pagebuf
<< chip
->page_shift
) < (to
+ ops
->len
))
2858 /* Don't allow multipage oob writes with offset */
2859 if (oob
&& ops
->ooboffs
&& (ops
->ooboffs
+ ops
->ooblen
> oobmaxlen
)) {
2865 int bytes
= mtd
->writesize
;
2866 int cached
= writelen
> bytes
&& page
!= blockmask
;
2867 uint8_t *wbuf
= buf
;
2869 int part_pagewr
= (column
|| writelen
< mtd
->writesize
);
2873 else if (chip
->options
& NAND_USE_BOUNCE_BUFFER
)
2874 use_bufpoi
= !virt_addr_valid(buf
);
2878 /* Partial page write?, or need to use bounce buffer */
2880 pr_debug("%s: using write bounce buffer for buf@%p\n",
2884 bytes
= min_t(int, bytes
- column
, writelen
);
2886 memset(chip
->buffers
->databuf
, 0xff, mtd
->writesize
);
2887 memcpy(&chip
->buffers
->databuf
[column
], buf
, bytes
);
2888 wbuf
= chip
->buffers
->databuf
;
2891 if (unlikely(oob
)) {
2892 size_t len
= min(oobwritelen
, oobmaxlen
);
2893 oob
= nand_fill_oob(mtd
, oob
, len
, ops
);
2896 /* We still need to erase leftover OOB data */
2897 memset(chip
->oob_poi
, 0xff, mtd
->oobsize
);
2899 ret
= chip
->write_page(mtd
, chip
, column
, bytes
, wbuf
,
2900 oob_required
, page
, cached
,
2901 (ops
->mode
== MTD_OPS_RAW
));
2913 page
= realpage
& chip
->pagemask
;
2914 /* Check, if we cross a chip boundary */
2917 chip
->select_chip(mtd
, -1);
2918 chip
->select_chip(mtd
, chipnr
);
2922 ops
->retlen
= ops
->len
- writelen
;
2924 ops
->oobretlen
= ops
->ooblen
;
2927 chip
->select_chip(mtd
, -1);
2932 * panic_nand_write - [MTD Interface] NAND write with ECC
2933 * @mtd: MTD device structure
2934 * @to: offset to write to
2935 * @len: number of bytes to write
2936 * @retlen: pointer to variable to store the number of written bytes
2937 * @buf: the data to write
2939 * NAND write with ECC. Used when performing writes in interrupt context, this
2940 * may for example be called by mtdoops when writing an oops while in panic.
2942 static int panic_nand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2943 size_t *retlen
, const uint8_t *buf
)
2945 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2946 int chipnr
= (int)(to
>> chip
->chip_shift
);
2947 struct mtd_oob_ops ops
;
2950 /* Grab the device */
2951 panic_nand_get_device(chip
, mtd
, FL_WRITING
);
2953 chip
->select_chip(mtd
, chipnr
);
2955 /* Wait for the device to get ready */
2956 panic_nand_wait(mtd
, chip
, 400);
2958 memset(&ops
, 0, sizeof(ops
));
2960 ops
.datbuf
= (uint8_t *)buf
;
2961 ops
.mode
= MTD_OPS_PLACE_OOB
;
2963 ret
= nand_do_write_ops(mtd
, to
, &ops
);
2965 *retlen
= ops
.retlen
;
2970 * nand_write - [MTD Interface] NAND write with ECC
2971 * @mtd: MTD device structure
2972 * @to: offset to write to
2973 * @len: number of bytes to write
2974 * @retlen: pointer to variable to store the number of written bytes
2975 * @buf: the data to write
2977 * NAND write with ECC.
2979 static int nand_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2980 size_t *retlen
, const uint8_t *buf
)
2982 struct mtd_oob_ops ops
;
2985 nand_get_device(mtd
, FL_WRITING
);
2986 memset(&ops
, 0, sizeof(ops
));
2988 ops
.datbuf
= (uint8_t *)buf
;
2989 ops
.mode
= MTD_OPS_PLACE_OOB
;
2990 ret
= nand_do_write_ops(mtd
, to
, &ops
);
2991 *retlen
= ops
.retlen
;
2992 nand_release_device(mtd
);
2997 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2998 * @mtd: MTD device structure
2999 * @to: offset to write to
3000 * @ops: oob operation description structure
3002 * NAND write out-of-band.
3004 static int nand_do_write_oob(struct mtd_info
*mtd
, loff_t to
,
3005 struct mtd_oob_ops
*ops
)
3007 int chipnr
, page
, status
, len
;
3008 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3010 pr_debug("%s: to = 0x%08x, len = %i\n",
3011 __func__
, (unsigned int)to
, (int)ops
->ooblen
);
3013 len
= mtd_oobavail(mtd
, ops
);
3015 /* Do not allow write past end of page */
3016 if ((ops
->ooboffs
+ ops
->ooblen
) > len
) {
3017 pr_debug("%s: attempt to write past end of page\n",
3022 if (unlikely(ops
->ooboffs
>= len
)) {
3023 pr_debug("%s: attempt to start write outside oob\n",
3028 /* Do not allow write past end of device */
3029 if (unlikely(to
>= mtd
->size
||
3030 ops
->ooboffs
+ ops
->ooblen
>
3031 ((mtd
->size
>> chip
->page_shift
) -
3032 (to
>> chip
->page_shift
)) * len
)) {
3033 pr_debug("%s: attempt to write beyond end of device\n",
3038 chipnr
= (int)(to
>> chip
->chip_shift
);
3041 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3042 * of my DiskOnChip 2000 test units) will clear the whole data page too
3043 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3044 * it in the doc2000 driver in August 1999. dwmw2.
3046 nand_reset(chip
, chipnr
);
3048 chip
->select_chip(mtd
, chipnr
);
3050 /* Shift to get page */
3051 page
= (int)(to
>> chip
->page_shift
);
3053 /* Check, if it is write protected */
3054 if (nand_check_wp(mtd
)) {
3055 chip
->select_chip(mtd
, -1);
3059 /* Invalidate the page cache, if we write to the cached page */
3060 if (page
== chip
->pagebuf
)
3063 nand_fill_oob(mtd
, ops
->oobbuf
, ops
->ooblen
, ops
);
3065 if (ops
->mode
== MTD_OPS_RAW
)
3066 status
= chip
->ecc
.write_oob_raw(mtd
, chip
, page
& chip
->pagemask
);
3068 status
= chip
->ecc
.write_oob(mtd
, chip
, page
& chip
->pagemask
);
3070 chip
->select_chip(mtd
, -1);
3075 ops
->oobretlen
= ops
->ooblen
;
3081 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
3082 * @mtd: MTD device structure
3083 * @to: offset to write to
3084 * @ops: oob operation description structure
3086 static int nand_write_oob(struct mtd_info
*mtd
, loff_t to
,
3087 struct mtd_oob_ops
*ops
)
3089 int ret
= -ENOTSUPP
;
3093 /* Do not allow writes past end of device */
3094 if (ops
->datbuf
&& (to
+ ops
->len
) > mtd
->size
) {
3095 pr_debug("%s: attempt to write beyond end of device\n",
3100 nand_get_device(mtd
, FL_WRITING
);
3102 switch (ops
->mode
) {
3103 case MTD_OPS_PLACE_OOB
:
3104 case MTD_OPS_AUTO_OOB
:
3113 ret
= nand_do_write_oob(mtd
, to
, ops
);
3115 ret
= nand_do_write_ops(mtd
, to
, ops
);
3118 nand_release_device(mtd
);
3123 * single_erase - [GENERIC] NAND standard block erase command function
3124 * @mtd: MTD device structure
3125 * @page: the page address of the block which will be erased
3127 * Standard erase command for NAND chips. Returns NAND status.
3129 static int single_erase(struct mtd_info
*mtd
, int page
)
3131 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3132 /* Send commands to erase a block */
3133 chip
->cmdfunc(mtd
, NAND_CMD_ERASE1
, -1, page
);
3134 chip
->cmdfunc(mtd
, NAND_CMD_ERASE2
, -1, -1);
3136 return chip
->waitfunc(mtd
, chip
);
3140 * nand_erase - [MTD Interface] erase block(s)
3141 * @mtd: MTD device structure
3142 * @instr: erase instruction
3144 * Erase one ore more blocks.
3146 static int nand_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
3148 return nand_erase_nand(mtd
, instr
, 0);
3152 * nand_erase_nand - [INTERN] erase block(s)
3153 * @mtd: MTD device structure
3154 * @instr: erase instruction
3155 * @allowbbt: allow erasing the bbt area
3157 * Erase one ore more blocks.
3159 int nand_erase_nand(struct mtd_info
*mtd
, struct erase_info
*instr
,
3162 int page
, status
, pages_per_block
, ret
, chipnr
;
3163 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3166 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3167 __func__
, (unsigned long long)instr
->addr
,
3168 (unsigned long long)instr
->len
);
3170 if (check_offs_len(mtd
, instr
->addr
, instr
->len
))
3173 /* Grab the lock and see if the device is available */
3174 nand_get_device(mtd
, FL_ERASING
);
3176 /* Shift to get first page */
3177 page
= (int)(instr
->addr
>> chip
->page_shift
);
3178 chipnr
= (int)(instr
->addr
>> chip
->chip_shift
);
3180 /* Calculate pages in each block */
3181 pages_per_block
= 1 << (chip
->phys_erase_shift
- chip
->page_shift
);
3183 /* Select the NAND device */
3184 chip
->select_chip(mtd
, chipnr
);
3186 /* Check, if it is write protected */
3187 if (nand_check_wp(mtd
)) {
3188 pr_debug("%s: device is write protected!\n",
3190 instr
->state
= MTD_ERASE_FAILED
;
3194 /* Loop through the pages */
3197 instr
->state
= MTD_ERASING
;
3200 /* Check if we have a bad block, we do not erase bad blocks! */
3201 if (nand_block_checkbad(mtd
, ((loff_t
) page
) <<
3202 chip
->page_shift
, allowbbt
)) {
3203 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3205 instr
->state
= MTD_ERASE_FAILED
;
3210 * Invalidate the page cache, if we erase the block which
3211 * contains the current cached page.
3213 if (page
<= chip
->pagebuf
&& chip
->pagebuf
<
3214 (page
+ pages_per_block
))
3217 status
= chip
->erase(mtd
, page
& chip
->pagemask
);
3220 * See if operation failed and additional status checks are
3223 if ((status
& NAND_STATUS_FAIL
) && (chip
->errstat
))
3224 status
= chip
->errstat(mtd
, chip
, FL_ERASING
,
3227 /* See if block erase succeeded */
3228 if (status
& NAND_STATUS_FAIL
) {
3229 pr_debug("%s: failed erase, page 0x%08x\n",
3231 instr
->state
= MTD_ERASE_FAILED
;
3233 ((loff_t
)page
<< chip
->page_shift
);
3237 /* Increment page address and decrement length */
3238 len
-= (1ULL << chip
->phys_erase_shift
);
3239 page
+= pages_per_block
;
3241 /* Check, if we cross a chip boundary */
3242 if (len
&& !(page
& chip
->pagemask
)) {
3244 chip
->select_chip(mtd
, -1);
3245 chip
->select_chip(mtd
, chipnr
);
3248 instr
->state
= MTD_ERASE_DONE
;
3252 ret
= instr
->state
== MTD_ERASE_DONE
? 0 : -EIO
;
3254 /* Deselect and wake up anyone waiting on the device */
3255 chip
->select_chip(mtd
, -1);
3256 nand_release_device(mtd
);
3258 /* Do call back function */
3260 mtd_erase_callback(instr
);
3262 /* Return more or less happy */
3267 * nand_sync - [MTD Interface] sync
3268 * @mtd: MTD device structure
3270 * Sync is actually a wait for chip ready function.
3272 static void nand_sync(struct mtd_info
*mtd
)
3274 pr_debug("%s: called\n", __func__
);
3276 /* Grab the lock and see if the device is available */
3277 nand_get_device(mtd
, FL_SYNCING
);
3278 /* Release it and go back */
3279 nand_release_device(mtd
);
3283 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
3284 * @mtd: MTD device structure
3285 * @offs: offset relative to mtd start
3287 static int nand_block_isbad(struct mtd_info
*mtd
, loff_t offs
)
3289 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3290 int chipnr
= (int)(offs
>> chip
->chip_shift
);
3293 /* Select the NAND device */
3294 nand_get_device(mtd
, FL_READING
);
3295 chip
->select_chip(mtd
, chipnr
);
3297 ret
= nand_block_checkbad(mtd
, offs
, 0);
3299 chip
->select_chip(mtd
, -1);
3300 nand_release_device(mtd
);
3306 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
3307 * @mtd: MTD device structure
3308 * @ofs: offset relative to mtd start
3310 static int nand_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
3314 ret
= nand_block_isbad(mtd
, ofs
);
3316 /* If it was bad already, return success and do nothing */
3322 return nand_block_markbad_lowlevel(mtd
, ofs
);
3326 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3327 * @mtd: MTD device structure
3328 * @chip: nand chip info structure
3329 * @addr: feature address.
3330 * @subfeature_param: the subfeature parameters, a four bytes array.
3332 static int nand_onfi_set_features(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3333 int addr
, uint8_t *subfeature_param
)
3338 if (!chip
->onfi_version
||
3339 !(le16_to_cpu(chip
->onfi_params
.opt_cmd
)
3340 & ONFI_OPT_CMD_SET_GET_FEATURES
))
3343 chip
->cmdfunc(mtd
, NAND_CMD_SET_FEATURES
, addr
, -1);
3344 for (i
= 0; i
< ONFI_SUBFEATURE_PARAM_LEN
; ++i
)
3345 chip
->write_byte(mtd
, subfeature_param
[i
]);
3347 status
= chip
->waitfunc(mtd
, chip
);
3348 if (status
& NAND_STATUS_FAIL
)
3354 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3355 * @mtd: MTD device structure
3356 * @chip: nand chip info structure
3357 * @addr: feature address.
3358 * @subfeature_param: the subfeature parameters, a four bytes array.
3360 static int nand_onfi_get_features(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3361 int addr
, uint8_t *subfeature_param
)
3365 if (!chip
->onfi_version
||
3366 !(le16_to_cpu(chip
->onfi_params
.opt_cmd
)
3367 & ONFI_OPT_CMD_SET_GET_FEATURES
))
3370 chip
->cmdfunc(mtd
, NAND_CMD_GET_FEATURES
, addr
, -1);
3371 for (i
= 0; i
< ONFI_SUBFEATURE_PARAM_LEN
; ++i
)
3372 *subfeature_param
++ = chip
->read_byte(mtd
);
3377 * nand_suspend - [MTD Interface] Suspend the NAND flash
3378 * @mtd: MTD device structure
3380 static int nand_suspend(struct mtd_info
*mtd
)
3382 return nand_get_device(mtd
, FL_PM_SUSPENDED
);
3386 * nand_resume - [MTD Interface] Resume the NAND flash
3387 * @mtd: MTD device structure
3389 static void nand_resume(struct mtd_info
*mtd
)
3391 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3393 if (chip
->state
== FL_PM_SUSPENDED
)
3394 nand_release_device(mtd
);
3396 pr_err("%s called for a chip which is not in suspended state\n",
3401 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3402 * prevent further operations
3403 * @mtd: MTD device structure
3405 static void nand_shutdown(struct mtd_info
*mtd
)
3407 nand_get_device(mtd
, FL_PM_SUSPENDED
);
3410 /* Set default functions */
3411 static void nand_set_defaults(struct nand_chip
*chip
, int busw
)
3413 /* check for proper chip_delay setup, set 20us if not */
3414 if (!chip
->chip_delay
)
3415 chip
->chip_delay
= 20;
3417 /* check, if a user supplied command function given */
3418 if (chip
->cmdfunc
== NULL
)
3419 chip
->cmdfunc
= nand_command
;
3421 /* check, if a user supplied wait function given */
3422 if (chip
->waitfunc
== NULL
)
3423 chip
->waitfunc
= nand_wait
;
3425 if (!chip
->select_chip
)
3426 chip
->select_chip
= nand_select_chip
;
3428 /* set for ONFI nand */
3429 if (!chip
->onfi_set_features
)
3430 chip
->onfi_set_features
= nand_onfi_set_features
;
3431 if (!chip
->onfi_get_features
)
3432 chip
->onfi_get_features
= nand_onfi_get_features
;
3434 /* If called twice, pointers that depend on busw may need to be reset */
3435 if (!chip
->read_byte
|| chip
->read_byte
== nand_read_byte
)
3436 chip
->read_byte
= busw
? nand_read_byte16
: nand_read_byte
;
3437 if (!chip
->read_word
)
3438 chip
->read_word
= nand_read_word
;
3439 if (!chip
->block_bad
)
3440 chip
->block_bad
= nand_block_bad
;
3441 if (!chip
->block_markbad
)
3442 chip
->block_markbad
= nand_default_block_markbad
;
3443 if (!chip
->write_buf
|| chip
->write_buf
== nand_write_buf
)
3444 chip
->write_buf
= busw
? nand_write_buf16
: nand_write_buf
;
3445 if (!chip
->write_byte
|| chip
->write_byte
== nand_write_byte
)
3446 chip
->write_byte
= busw
? nand_write_byte16
: nand_write_byte
;
3447 if (!chip
->read_buf
|| chip
->read_buf
== nand_read_buf
)
3448 chip
->read_buf
= busw
? nand_read_buf16
: nand_read_buf
;
3449 if (!chip
->scan_bbt
)
3450 chip
->scan_bbt
= nand_default_bbt
;
3452 if (!chip
->controller
) {
3453 chip
->controller
= &chip
->hwcontrol
;
3454 nand_hw_control_init(chip
->controller
);
3459 /* Sanitize ONFI strings so we can safely print them */
3460 static void sanitize_string(uint8_t *s
, size_t len
)
3464 /* Null terminate */
3467 /* Remove non printable chars */
3468 for (i
= 0; i
< len
- 1; i
++) {
3469 if (s
[i
] < ' ' || s
[i
] > 127)
3473 /* Remove trailing spaces */
3477 static u16
onfi_crc16(u16 crc
, u8
const *p
, size_t len
)
3482 for (i
= 0; i
< 8; i
++)
3483 crc
= (crc
<< 1) ^ ((crc
& 0x8000) ? 0x8005 : 0);
3489 /* Parse the Extended Parameter Page. */
3490 static int nand_flash_detect_ext_param_page(struct mtd_info
*mtd
,
3491 struct nand_chip
*chip
, struct nand_onfi_params
*p
)
3493 struct onfi_ext_param_page
*ep
;
3494 struct onfi_ext_section
*s
;
3495 struct onfi_ext_ecc_info
*ecc
;
3501 len
= le16_to_cpu(p
->ext_param_page_length
) * 16;
3502 ep
= kmalloc(len
, GFP_KERNEL
);
3506 /* Send our own NAND_CMD_PARAM. */
3507 chip
->cmdfunc(mtd
, NAND_CMD_PARAM
, 0, -1);
3509 /* Use the Change Read Column command to skip the ONFI param pages. */
3510 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
3511 sizeof(*p
) * p
->num_of_param_pages
, -1);
3513 /* Read out the Extended Parameter Page. */
3514 chip
->read_buf(mtd
, (uint8_t *)ep
, len
);
3515 if ((onfi_crc16(ONFI_CRC_BASE
, ((uint8_t *)ep
) + 2, len
- 2)
3516 != le16_to_cpu(ep
->crc
))) {
3517 pr_debug("fail in the CRC.\n");
3522 * Check the signature.
3523 * Do not strictly follow the ONFI spec, maybe changed in future.
3525 if (strncmp(ep
->sig
, "EPPS", 4)) {
3526 pr_debug("The signature is invalid.\n");
3530 /* find the ECC section. */
3531 cursor
= (uint8_t *)(ep
+ 1);
3532 for (i
= 0; i
< ONFI_EXT_SECTION_MAX
; i
++) {
3533 s
= ep
->sections
+ i
;
3534 if (s
->type
== ONFI_SECTION_TYPE_2
)
3536 cursor
+= s
->length
* 16;
3538 if (i
== ONFI_EXT_SECTION_MAX
) {
3539 pr_debug("We can not find the ECC section.\n");
3543 /* get the info we want. */
3544 ecc
= (struct onfi_ext_ecc_info
*)cursor
;
3546 if (!ecc
->codeword_size
) {
3547 pr_debug("Invalid codeword size\n");
3551 chip
->ecc_strength_ds
= ecc
->ecc_bits
;
3552 chip
->ecc_step_ds
= 1 << ecc
->codeword_size
;
3560 static int nand_setup_read_retry_micron(struct mtd_info
*mtd
, int retry_mode
)
3562 struct nand_chip
*chip
= mtd_to_nand(mtd
);
3563 uint8_t feature
[ONFI_SUBFEATURE_PARAM_LEN
] = {retry_mode
};
3565 return chip
->onfi_set_features(mtd
, chip
, ONFI_FEATURE_ADDR_READ_RETRY
,
3570 * Configure chip properties from Micron vendor-specific ONFI table
3572 static void nand_onfi_detect_micron(struct nand_chip
*chip
,
3573 struct nand_onfi_params
*p
)
3575 struct nand_onfi_vendor_micron
*micron
= (void *)p
->vendor
;
3577 if (le16_to_cpu(p
->vendor_revision
) < 1)
3580 chip
->read_retries
= micron
->read_retry_options
;
3581 chip
->setup_read_retry
= nand_setup_read_retry_micron
;
3585 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3587 static int nand_flash_detect_onfi(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3590 struct nand_onfi_params
*p
= &chip
->onfi_params
;
3594 /* Try ONFI for unknown chip or LP */
3595 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x20, -1);
3596 if (chip
->read_byte(mtd
) != 'O' || chip
->read_byte(mtd
) != 'N' ||
3597 chip
->read_byte(mtd
) != 'F' || chip
->read_byte(mtd
) != 'I')
3600 chip
->cmdfunc(mtd
, NAND_CMD_PARAM
, 0, -1);
3601 for (i
= 0; i
< 3; i
++) {
3602 for (j
= 0; j
< sizeof(*p
); j
++)
3603 ((uint8_t *)p
)[j
] = chip
->read_byte(mtd
);
3604 if (onfi_crc16(ONFI_CRC_BASE
, (uint8_t *)p
, 254) ==
3605 le16_to_cpu(p
->crc
)) {
3611 pr_err("Could not find valid ONFI parameter page; aborting\n");
3616 val
= le16_to_cpu(p
->revision
);
3618 chip
->onfi_version
= 23;
3619 else if (val
& (1 << 4))
3620 chip
->onfi_version
= 22;
3621 else if (val
& (1 << 3))
3622 chip
->onfi_version
= 21;
3623 else if (val
& (1 << 2))
3624 chip
->onfi_version
= 20;
3625 else if (val
& (1 << 1))
3626 chip
->onfi_version
= 10;
3628 if (!chip
->onfi_version
) {
3629 pr_info("unsupported ONFI version: %d\n", val
);
3633 sanitize_string(p
->manufacturer
, sizeof(p
->manufacturer
));
3634 sanitize_string(p
->model
, sizeof(p
->model
));
3636 mtd
->name
= p
->model
;
3638 mtd
->writesize
= le32_to_cpu(p
->byte_per_page
);
3641 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3642 * (don't ask me who thought of this...). MTD assumes that these
3643 * dimensions will be power-of-2, so just truncate the remaining area.
3645 mtd
->erasesize
= 1 << (fls(le32_to_cpu(p
->pages_per_block
)) - 1);
3646 mtd
->erasesize
*= mtd
->writesize
;
3648 mtd
->oobsize
= le16_to_cpu(p
->spare_bytes_per_page
);
3650 /* See erasesize comment */
3651 chip
->chipsize
= 1 << (fls(le32_to_cpu(p
->blocks_per_lun
)) - 1);
3652 chip
->chipsize
*= (uint64_t)mtd
->erasesize
* p
->lun_count
;
3653 chip
->bits_per_cell
= p
->bits_per_cell
;
3655 if (onfi_feature(chip
) & ONFI_FEATURE_16_BIT_BUS
)
3656 *busw
= NAND_BUSWIDTH_16
;
3660 if (p
->ecc_bits
!= 0xff) {
3661 chip
->ecc_strength_ds
= p
->ecc_bits
;
3662 chip
->ecc_step_ds
= 512;
3663 } else if (chip
->onfi_version
>= 21 &&
3664 (onfi_feature(chip
) & ONFI_FEATURE_EXT_PARAM_PAGE
)) {
3667 * The nand_flash_detect_ext_param_page() uses the
3668 * Change Read Column command which maybe not supported
3669 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3670 * now. We do not replace user supplied command function.
3672 if (mtd
->writesize
> 512 && chip
->cmdfunc
== nand_command
)
3673 chip
->cmdfunc
= nand_command_lp
;
3675 /* The Extended Parameter Page is supported since ONFI 2.1. */
3676 if (nand_flash_detect_ext_param_page(mtd
, chip
, p
))
3677 pr_warn("Failed to detect ONFI extended param page\n");
3679 pr_warn("Could not retrieve ONFI ECC requirements\n");
3682 if (p
->jedec_id
== NAND_MFR_MICRON
)
3683 nand_onfi_detect_micron(chip
, p
);
3689 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3691 static int nand_flash_detect_jedec(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3694 struct nand_jedec_params
*p
= &chip
->jedec_params
;
3695 struct jedec_ecc_info
*ecc
;
3699 /* Try JEDEC for unknown chip or LP */
3700 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x40, -1);
3701 if (chip
->read_byte(mtd
) != 'J' || chip
->read_byte(mtd
) != 'E' ||
3702 chip
->read_byte(mtd
) != 'D' || chip
->read_byte(mtd
) != 'E' ||
3703 chip
->read_byte(mtd
) != 'C')
3706 chip
->cmdfunc(mtd
, NAND_CMD_PARAM
, 0x40, -1);
3707 for (i
= 0; i
< 3; i
++) {
3708 for (j
= 0; j
< sizeof(*p
); j
++)
3709 ((uint8_t *)p
)[j
] = chip
->read_byte(mtd
);
3711 if (onfi_crc16(ONFI_CRC_BASE
, (uint8_t *)p
, 510) ==
3712 le16_to_cpu(p
->crc
))
3717 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3722 val
= le16_to_cpu(p
->revision
);
3724 chip
->jedec_version
= 10;
3725 else if (val
& (1 << 1))
3726 chip
->jedec_version
= 1; /* vendor specific version */
3728 if (!chip
->jedec_version
) {
3729 pr_info("unsupported JEDEC version: %d\n", val
);
3733 sanitize_string(p
->manufacturer
, sizeof(p
->manufacturer
));
3734 sanitize_string(p
->model
, sizeof(p
->model
));
3736 mtd
->name
= p
->model
;
3738 mtd
->writesize
= le32_to_cpu(p
->byte_per_page
);
3740 /* Please reference to the comment for nand_flash_detect_onfi. */
3741 mtd
->erasesize
= 1 << (fls(le32_to_cpu(p
->pages_per_block
)) - 1);
3742 mtd
->erasesize
*= mtd
->writesize
;
3744 mtd
->oobsize
= le16_to_cpu(p
->spare_bytes_per_page
);
3746 /* Please reference to the comment for nand_flash_detect_onfi. */
3747 chip
->chipsize
= 1 << (fls(le32_to_cpu(p
->blocks_per_lun
)) - 1);
3748 chip
->chipsize
*= (uint64_t)mtd
->erasesize
* p
->lun_count
;
3749 chip
->bits_per_cell
= p
->bits_per_cell
;
3751 if (jedec_feature(chip
) & JEDEC_FEATURE_16_BIT_BUS
)
3752 *busw
= NAND_BUSWIDTH_16
;
3757 ecc
= &p
->ecc_info
[0];
3759 if (ecc
->codeword_size
>= 9) {
3760 chip
->ecc_strength_ds
= ecc
->ecc_bits
;
3761 chip
->ecc_step_ds
= 1 << ecc
->codeword_size
;
3763 pr_warn("Invalid codeword size\n");
3770 * nand_id_has_period - Check if an ID string has a given wraparound period
3771 * @id_data: the ID string
3772 * @arrlen: the length of the @id_data array
3773 * @period: the period of repitition
3775 * Check if an ID string is repeated within a given sequence of bytes at
3776 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3777 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3778 * if the repetition has a period of @period; otherwise, returns zero.
3780 static int nand_id_has_period(u8
*id_data
, int arrlen
, int period
)
3783 for (i
= 0; i
< period
; i
++)
3784 for (j
= i
+ period
; j
< arrlen
; j
+= period
)
3785 if (id_data
[i
] != id_data
[j
])
3791 * nand_id_len - Get the length of an ID string returned by CMD_READID
3792 * @id_data: the ID string
3793 * @arrlen: the length of the @id_data array
3795 * Returns the length of the ID string, according to known wraparound/trailing
3796 * zero patterns. If no pattern exists, returns the length of the array.
3798 static int nand_id_len(u8
*id_data
, int arrlen
)
3800 int last_nonzero
, period
;
3802 /* Find last non-zero byte */
3803 for (last_nonzero
= arrlen
- 1; last_nonzero
>= 0; last_nonzero
--)
3804 if (id_data
[last_nonzero
])
3808 if (last_nonzero
< 0)
3811 /* Calculate wraparound period */
3812 for (period
= 1; period
< arrlen
; period
++)
3813 if (nand_id_has_period(id_data
, arrlen
, period
))
3816 /* There's a repeated pattern */
3817 if (period
< arrlen
)
3820 /* There are trailing zeros */
3821 if (last_nonzero
< arrlen
- 1)
3822 return last_nonzero
+ 1;
3824 /* No pattern detected */
3828 /* Extract the bits of per cell from the 3rd byte of the extended ID */
3829 static int nand_get_bits_per_cell(u8 cellinfo
)
3833 bits
= cellinfo
& NAND_CI_CELLTYPE_MSK
;
3834 bits
>>= NAND_CI_CELLTYPE_SHIFT
;
3839 * Many new NAND share similar device ID codes, which represent the size of the
3840 * chip. The rest of the parameters must be decoded according to generic or
3841 * manufacturer-specific "extended ID" decoding patterns.
3843 static void nand_decode_ext_id(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3844 u8 id_data
[8], int *busw
)
3847 /* The 3rd id byte holds MLC / multichip data */
3848 chip
->bits_per_cell
= nand_get_bits_per_cell(id_data
[2]);
3849 /* The 4th id byte is the important one */
3852 id_len
= nand_id_len(id_data
, 8);
3855 * Field definitions are in the following datasheets:
3856 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3857 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3858 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3860 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3861 * ID to decide what to do.
3863 if (id_len
== 6 && id_data
[0] == NAND_MFR_SAMSUNG
&&
3864 !nand_is_slc(chip
) && id_data
[5] != 0x00) {
3866 mtd
->writesize
= 2048 << (extid
& 0x03);
3869 switch (((extid
>> 2) & 0x04) | (extid
& 0x03)) {
3889 default: /* Other cases are "reserved" (unknown) */
3890 mtd
->oobsize
= 1024;
3894 /* Calc blocksize */
3895 mtd
->erasesize
= (128 * 1024) <<
3896 (((extid
>> 1) & 0x04) | (extid
& 0x03));
3898 } else if (id_len
== 6 && id_data
[0] == NAND_MFR_HYNIX
&&
3899 !nand_is_slc(chip
)) {
3903 mtd
->writesize
= 2048 << (extid
& 0x03);
3906 switch (((extid
>> 2) & 0x04) | (extid
& 0x03)) {
3930 /* Calc blocksize */
3931 tmp
= ((extid
>> 1) & 0x04) | (extid
& 0x03);
3933 mtd
->erasesize
= (128 * 1024) << tmp
;
3934 else if (tmp
== 0x03)
3935 mtd
->erasesize
= 768 * 1024;
3937 mtd
->erasesize
= (64 * 1024) << tmp
;
3941 mtd
->writesize
= 1024 << (extid
& 0x03);
3944 mtd
->oobsize
= (8 << (extid
& 0x01)) *
3945 (mtd
->writesize
>> 9);
3947 /* Calc blocksize. Blocksize is multiples of 64KiB */
3948 mtd
->erasesize
= (64 * 1024) << (extid
& 0x03);
3950 /* Get buswidth information */
3951 *busw
= (extid
& 0x01) ? NAND_BUSWIDTH_16
: 0;
3954 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3955 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3957 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3959 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3961 if (id_len
>= 6 && id_data
[0] == NAND_MFR_TOSHIBA
&&
3962 nand_is_slc(chip
) &&
3963 (id_data
[5] & 0x7) == 0x6 /* 24nm */ &&
3964 !(id_data
[4] & 0x80) /* !BENAND */) {
3965 mtd
->oobsize
= 32 * mtd
->writesize
>> 9;
3972 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3973 * decodes a matching ID table entry and assigns the MTD size parameters for
3976 static void nand_decode_id(struct mtd_info
*mtd
, struct nand_chip
*chip
,
3977 struct nand_flash_dev
*type
, u8 id_data
[8],
3980 int maf_id
= id_data
[0];
3982 mtd
->erasesize
= type
->erasesize
;
3983 mtd
->writesize
= type
->pagesize
;
3984 mtd
->oobsize
= mtd
->writesize
/ 32;
3985 *busw
= type
->options
& NAND_BUSWIDTH_16
;
3987 /* All legacy ID NAND are small-page, SLC */
3988 chip
->bits_per_cell
= 1;
3991 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3992 * some Spansion chips have erasesize that conflicts with size
3993 * listed in nand_ids table.
3994 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3996 if (maf_id
== NAND_MFR_AMD
&& id_data
[4] != 0x00 && id_data
[5] == 0x00
3997 && id_data
[6] == 0x00 && id_data
[7] == 0x00
3998 && mtd
->writesize
== 512) {
3999 mtd
->erasesize
= 128 * 1024;
4000 mtd
->erasesize
<<= ((id_data
[3] & 0x03) << 1);
4005 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4006 * heuristic patterns using various detected parameters (e.g., manufacturer,
4007 * page size, cell-type information).
4009 static void nand_decode_bbm_options(struct mtd_info
*mtd
,
4010 struct nand_chip
*chip
, u8 id_data
[8])
4012 int maf_id
= id_data
[0];
4014 /* Set the bad block position */
4015 if (mtd
->writesize
> 512 || (chip
->options
& NAND_BUSWIDTH_16
))
4016 chip
->badblockpos
= NAND_LARGE_BADBLOCK_POS
;
4018 chip
->badblockpos
= NAND_SMALL_BADBLOCK_POS
;
4021 * Bad block marker is stored in the last page of each block on Samsung
4022 * and Hynix MLC devices; stored in first two pages of each block on
4023 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4024 * AMD/Spansion, and Macronix. All others scan only the first page.
4026 if (!nand_is_slc(chip
) &&
4027 (maf_id
== NAND_MFR_SAMSUNG
||
4028 maf_id
== NAND_MFR_HYNIX
))
4029 chip
->bbt_options
|= NAND_BBT_SCANLASTPAGE
;
4030 else if ((nand_is_slc(chip
) &&
4031 (maf_id
== NAND_MFR_SAMSUNG
||
4032 maf_id
== NAND_MFR_HYNIX
||
4033 maf_id
== NAND_MFR_TOSHIBA
||
4034 maf_id
== NAND_MFR_AMD
||
4035 maf_id
== NAND_MFR_MACRONIX
)) ||
4036 (mtd
->writesize
== 2048 &&
4037 maf_id
== NAND_MFR_MICRON
))
4038 chip
->bbt_options
|= NAND_BBT_SCAN2NDPAGE
;
4041 static inline bool is_full_id_nand(struct nand_flash_dev
*type
)
4043 return type
->id_len
;
4046 static bool find_full_id_nand(struct mtd_info
*mtd
, struct nand_chip
*chip
,
4047 struct nand_flash_dev
*type
, u8
*id_data
, int *busw
)
4049 if (!strncmp(type
->id
, id_data
, type
->id_len
)) {
4050 mtd
->writesize
= type
->pagesize
;
4051 mtd
->erasesize
= type
->erasesize
;
4052 mtd
->oobsize
= type
->oobsize
;
4054 chip
->bits_per_cell
= nand_get_bits_per_cell(id_data
[2]);
4055 chip
->chipsize
= (uint64_t)type
->chipsize
<< 20;
4056 chip
->options
|= type
->options
;
4057 chip
->ecc_strength_ds
= NAND_ECC_STRENGTH(type
);
4058 chip
->ecc_step_ds
= NAND_ECC_STEP(type
);
4059 chip
->onfi_timing_mode_default
=
4060 type
->onfi_timing_mode_default
;
4062 *busw
= type
->options
& NAND_BUSWIDTH_16
;
4065 mtd
->name
= type
->name
;
4073 * Get the flash and manufacturer id and lookup if the type is supported.
4075 static struct nand_flash_dev
*nand_get_flash_type(struct mtd_info
*mtd
,
4076 struct nand_chip
*chip
,
4077 int *maf_id
, int *dev_id
,
4078 struct nand_flash_dev
*type
)
4085 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
4088 nand_reset(chip
, 0);
4090 /* Select the device */
4091 chip
->select_chip(mtd
, 0);
4093 /* Send the command for reading device ID */
4094 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x00, -1);
4096 /* Read manufacturer and device IDs */
4097 *maf_id
= chip
->read_byte(mtd
);
4098 *dev_id
= chip
->read_byte(mtd
);
4101 * Try again to make sure, as some systems the bus-hold or other
4102 * interface concerns can cause random data which looks like a
4103 * possibly credible NAND flash to appear. If the two results do
4104 * not match, ignore the device completely.
4107 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x00, -1);
4109 /* Read entire ID string */
4110 for (i
= 0; i
< 8; i
++)
4111 id_data
[i
] = chip
->read_byte(mtd
);
4113 if (id_data
[0] != *maf_id
|| id_data
[1] != *dev_id
) {
4114 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
4115 *maf_id
, *dev_id
, id_data
[0], id_data
[1]);
4116 return ERR_PTR(-ENODEV
);
4120 type
= nand_flash_ids
;
4122 for (; type
->name
!= NULL
; type
++) {
4123 if (is_full_id_nand(type
)) {
4124 if (find_full_id_nand(mtd
, chip
, type
, id_data
, &busw
))
4126 } else if (*dev_id
== type
->dev_id
) {
4131 chip
->onfi_version
= 0;
4132 if (!type
->name
|| !type
->pagesize
) {
4133 /* Check if the chip is ONFI compliant */
4134 if (nand_flash_detect_onfi(mtd
, chip
, &busw
))
4137 /* Check if the chip is JEDEC compliant */
4138 if (nand_flash_detect_jedec(mtd
, chip
, &busw
))
4143 return ERR_PTR(-ENODEV
);
4146 mtd
->name
= type
->name
;
4148 chip
->chipsize
= (uint64_t)type
->chipsize
<< 20;
4150 if (!type
->pagesize
) {
4151 /* Decode parameters from extended ID */
4152 nand_decode_ext_id(mtd
, chip
, id_data
, &busw
);
4154 nand_decode_id(mtd
, chip
, type
, id_data
, &busw
);
4156 /* Get chip options */
4157 chip
->options
|= type
->options
;
4160 * Check if chip is not a Samsung device. Do not clear the
4161 * options for chips which do not have an extended id.
4163 if (*maf_id
!= NAND_MFR_SAMSUNG
&& !type
->pagesize
)
4164 chip
->options
&= ~NAND_SAMSUNG_LP_OPTIONS
;
4167 /* Try to identify manufacturer */
4168 for (maf_idx
= 0; nand_manuf_ids
[maf_idx
].id
!= 0x0; maf_idx
++) {
4169 if (nand_manuf_ids
[maf_idx
].id
== *maf_id
)
4173 if (chip
->options
& NAND_BUSWIDTH_AUTO
) {
4174 WARN_ON(chip
->options
& NAND_BUSWIDTH_16
);
4175 chip
->options
|= busw
;
4176 nand_set_defaults(chip
, busw
);
4177 } else if (busw
!= (chip
->options
& NAND_BUSWIDTH_16
)) {
4179 * Check, if buswidth is correct. Hardware drivers should set
4182 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4184 pr_info("%s %s\n", nand_manuf_ids
[maf_idx
].name
, mtd
->name
);
4185 pr_warn("bus width %d instead %d bit\n",
4186 (chip
->options
& NAND_BUSWIDTH_16
) ? 16 : 8,
4188 return ERR_PTR(-EINVAL
);
4191 nand_decode_bbm_options(mtd
, chip
, id_data
);
4193 /* Calculate the address shift from the page size */
4194 chip
->page_shift
= ffs(mtd
->writesize
) - 1;
4195 /* Convert chipsize to number of pages per chip -1 */
4196 chip
->pagemask
= (chip
->chipsize
>> chip
->page_shift
) - 1;
4198 chip
->bbt_erase_shift
= chip
->phys_erase_shift
=
4199 ffs(mtd
->erasesize
) - 1;
4200 if (chip
->chipsize
& 0xffffffff)
4201 chip
->chip_shift
= ffs((unsigned)chip
->chipsize
) - 1;
4203 chip
->chip_shift
= ffs((unsigned)(chip
->chipsize
>> 32));
4204 chip
->chip_shift
+= 32 - 1;
4207 chip
->badblockbits
= 8;
4208 chip
->erase
= single_erase
;
4210 /* Do not replace user supplied command function! */
4211 if (mtd
->writesize
> 512 && chip
->cmdfunc
== nand_command
)
4212 chip
->cmdfunc
= nand_command_lp
;
4214 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4217 if (chip
->onfi_version
)
4218 pr_info("%s %s\n", nand_manuf_ids
[maf_idx
].name
,
4219 chip
->onfi_params
.model
);
4220 else if (chip
->jedec_version
)
4221 pr_info("%s %s\n", nand_manuf_ids
[maf_idx
].name
,
4222 chip
->jedec_params
.model
);
4224 pr_info("%s %s\n", nand_manuf_ids
[maf_idx
].name
,
4227 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
4228 (int)(chip
->chipsize
>> 20), nand_is_slc(chip
) ? "SLC" : "MLC",
4229 mtd
->erasesize
>> 10, mtd
->writesize
, mtd
->oobsize
);
4233 static const char * const nand_ecc_modes
[] = {
4234 [NAND_ECC_NONE
] = "none",
4235 [NAND_ECC_SOFT
] = "soft",
4236 [NAND_ECC_HW
] = "hw",
4237 [NAND_ECC_HW_SYNDROME
] = "hw_syndrome",
4238 [NAND_ECC_HW_OOB_FIRST
] = "hw_oob_first",
4241 static int of_get_nand_ecc_mode(struct device_node
*np
)
4246 err
= of_property_read_string(np
, "nand-ecc-mode", &pm
);
4250 for (i
= 0; i
< ARRAY_SIZE(nand_ecc_modes
); i
++)
4251 if (!strcasecmp(pm
, nand_ecc_modes
[i
]))
4255 * For backward compatibility we support few obsoleted values that don't
4256 * have their mappings into nand_ecc_modes_t anymore (they were merged
4257 * with other enums).
4259 if (!strcasecmp(pm
, "soft_bch"))
4260 return NAND_ECC_SOFT
;
4265 static const char * const nand_ecc_algos
[] = {
4266 [NAND_ECC_HAMMING
] = "hamming",
4267 [NAND_ECC_BCH
] = "bch",
4270 static int of_get_nand_ecc_algo(struct device_node
*np
)
4275 err
= of_property_read_string(np
, "nand-ecc-algo", &pm
);
4277 for (i
= NAND_ECC_HAMMING
; i
< ARRAY_SIZE(nand_ecc_algos
); i
++)
4278 if (!strcasecmp(pm
, nand_ecc_algos
[i
]))
4284 * For backward compatibility we also read "nand-ecc-mode" checking
4285 * for some obsoleted values that were specifying ECC algorithm.
4287 err
= of_property_read_string(np
, "nand-ecc-mode", &pm
);
4291 if (!strcasecmp(pm
, "soft"))
4292 return NAND_ECC_HAMMING
;
4293 else if (!strcasecmp(pm
, "soft_bch"))
4294 return NAND_ECC_BCH
;
4299 static int of_get_nand_ecc_step_size(struct device_node
*np
)
4304 ret
= of_property_read_u32(np
, "nand-ecc-step-size", &val
);
4305 return ret
? ret
: val
;
4308 static int of_get_nand_ecc_strength(struct device_node
*np
)
4313 ret
= of_property_read_u32(np
, "nand-ecc-strength", &val
);
4314 return ret
? ret
: val
;
4317 static int of_get_nand_bus_width(struct device_node
*np
)
4321 if (of_property_read_u32(np
, "nand-bus-width", &val
))
4333 static bool of_get_nand_on_flash_bbt(struct device_node
*np
)
4335 return of_property_read_bool(np
, "nand-on-flash-bbt");
4338 static int nand_dt_init(struct nand_chip
*chip
)
4340 struct device_node
*dn
= nand_get_flash_node(chip
);
4341 int ecc_mode
, ecc_algo
, ecc_strength
, ecc_step
;
4346 if (of_get_nand_bus_width(dn
) == 16)
4347 chip
->options
|= NAND_BUSWIDTH_16
;
4349 if (of_get_nand_on_flash_bbt(dn
))
4350 chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
4352 ecc_mode
= of_get_nand_ecc_mode(dn
);
4353 ecc_algo
= of_get_nand_ecc_algo(dn
);
4354 ecc_strength
= of_get_nand_ecc_strength(dn
);
4355 ecc_step
= of_get_nand_ecc_step_size(dn
);
4357 if ((ecc_step
>= 0 && !(ecc_strength
>= 0)) ||
4358 (!(ecc_step
>= 0) && ecc_strength
>= 0)) {
4359 pr_err("must set both strength and step size in DT\n");
4364 chip
->ecc
.mode
= ecc_mode
;
4367 chip
->ecc
.algo
= ecc_algo
;
4369 if (ecc_strength
>= 0)
4370 chip
->ecc
.strength
= ecc_strength
;
4373 chip
->ecc
.size
= ecc_step
;
4375 if (of_property_read_bool(dn
, "nand-ecc-maximize"))
4376 chip
->ecc
.options
|= NAND_ECC_MAXIMIZE
;
4382 * nand_scan_ident - [NAND Interface] Scan for the NAND device
4383 * @mtd: MTD device structure
4384 * @maxchips: number of chips to scan for
4385 * @table: alternative NAND ID table
4387 * This is the first phase of the normal nand_scan() function. It reads the
4388 * flash ID and sets up MTD fields accordingly.
4391 int nand_scan_ident(struct mtd_info
*mtd
, int maxchips
,
4392 struct nand_flash_dev
*table
)
4394 int i
, nand_maf_id
, nand_dev_id
;
4395 struct nand_chip
*chip
= mtd_to_nand(mtd
);
4396 struct nand_flash_dev
*type
;
4399 ret
= nand_dt_init(chip
);
4403 if (!mtd
->name
&& mtd
->dev
.parent
)
4404 mtd
->name
= dev_name(mtd
->dev
.parent
);
4406 if ((!chip
->cmdfunc
|| !chip
->select_chip
) && !chip
->cmd_ctrl
) {
4408 * Default functions assigned for chip_select() and
4409 * cmdfunc() both expect cmd_ctrl() to be populated,
4410 * so we need to check that that's the case
4412 pr_err("chip.cmd_ctrl() callback is not provided");
4415 /* Set the default functions */
4416 nand_set_defaults(chip
, chip
->options
& NAND_BUSWIDTH_16
);
4418 /* Read the flash type */
4419 type
= nand_get_flash_type(mtd
, chip
, &nand_maf_id
,
4420 &nand_dev_id
, table
);
4423 if (!(chip
->options
& NAND_SCAN_SILENT_NODEV
))
4424 pr_warn("No NAND device found\n");
4425 chip
->select_chip(mtd
, -1);
4426 return PTR_ERR(type
);
4429 /* Initialize the ->data_interface field. */
4430 ret
= nand_init_data_interface(chip
);
4435 * Setup the data interface correctly on the chip and controller side.
4436 * This explicit call to nand_setup_data_interface() is only required
4437 * for the first die, because nand_reset() has been called before
4438 * ->data_interface and ->default_onfi_timing_mode were set.
4439 * For the other dies, nand_reset() will automatically switch to the
4442 ret
= nand_setup_data_interface(chip
);
4446 chip
->select_chip(mtd
, -1);
4448 /* Check for a chip array */
4449 for (i
= 1; i
< maxchips
; i
++) {
4450 /* See comment in nand_get_flash_type for reset */
4451 nand_reset(chip
, i
);
4453 chip
->select_chip(mtd
, i
);
4454 /* Send the command for reading device ID */
4455 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0x00, -1);
4456 /* Read manufacturer and device IDs */
4457 if (nand_maf_id
!= chip
->read_byte(mtd
) ||
4458 nand_dev_id
!= chip
->read_byte(mtd
)) {
4459 chip
->select_chip(mtd
, -1);
4462 chip
->select_chip(mtd
, -1);
4465 pr_info("%d chips detected\n", i
);
4467 /* Store the number of chips and calc total size for mtd */
4469 mtd
->size
= i
* chip
->chipsize
;
4473 EXPORT_SYMBOL(nand_scan_ident
);
4475 static int nand_set_ecc_soft_ops(struct mtd_info
*mtd
)
4477 struct nand_chip
*chip
= mtd_to_nand(mtd
);
4478 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
4480 if (WARN_ON(ecc
->mode
!= NAND_ECC_SOFT
))
4483 switch (ecc
->algo
) {
4484 case NAND_ECC_HAMMING
:
4485 ecc
->calculate
= nand_calculate_ecc
;
4486 ecc
->correct
= nand_correct_data
;
4487 ecc
->read_page
= nand_read_page_swecc
;
4488 ecc
->read_subpage
= nand_read_subpage
;
4489 ecc
->write_page
= nand_write_page_swecc
;
4490 ecc
->read_page_raw
= nand_read_page_raw
;
4491 ecc
->write_page_raw
= nand_write_page_raw
;
4492 ecc
->read_oob
= nand_read_oob_std
;
4493 ecc
->write_oob
= nand_write_oob_std
;
4500 if (!mtd_nand_has_bch()) {
4501 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4504 ecc
->calculate
= nand_bch_calculate_ecc
;
4505 ecc
->correct
= nand_bch_correct_data
;
4506 ecc
->read_page
= nand_read_page_swecc
;
4507 ecc
->read_subpage
= nand_read_subpage
;
4508 ecc
->write_page
= nand_write_page_swecc
;
4509 ecc
->read_page_raw
= nand_read_page_raw
;
4510 ecc
->write_page_raw
= nand_write_page_raw
;
4511 ecc
->read_oob
= nand_read_oob_std
;
4512 ecc
->write_oob
= nand_write_oob_std
;
4515 * Board driver should supply ecc.size and ecc.strength
4516 * values to select how many bits are correctable.
4517 * Otherwise, default to 4 bits for large page devices.
4519 if (!ecc
->size
&& (mtd
->oobsize
>= 64)) {
4525 * if no ecc placement scheme was provided pickup the default
4528 if (!mtd
->ooblayout
) {
4529 /* handle large page devices only */
4530 if (mtd
->oobsize
< 64) {
4531 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4535 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_ops
);
4540 * We can only maximize ECC config when the default layout is
4541 * used, otherwise we don't know how many bytes can really be
4544 if (mtd
->ooblayout
== &nand_ooblayout_lp_ops
&&
4545 ecc
->options
& NAND_ECC_MAXIMIZE
) {
4548 /* Always prefer 1k blocks over 512bytes ones */
4550 steps
= mtd
->writesize
/ ecc
->size
;
4552 /* Reserve 2 bytes for the BBM */
4553 bytes
= (mtd
->oobsize
- 2) / steps
;
4554 ecc
->strength
= bytes
* 8 / fls(8 * ecc
->size
);
4557 /* See nand_bch_init() for details. */
4559 ecc
->priv
= nand_bch_init(mtd
);
4561 WARN(1, "BCH ECC initialization failed!\n");
4566 WARN(1, "Unsupported ECC algorithm!\n");
4572 * Check if the chip configuration meet the datasheet requirements.
4574 * If our configuration corrects A bits per B bytes and the minimum
4575 * required correction level is X bits per Y bytes, then we must ensure
4576 * both of the following are true:
4578 * (1) A / B >= X / Y
4581 * Requirement (1) ensures we can correct for the required bitflip density.
4582 * Requirement (2) ensures we can correct even when all bitflips are clumped
4583 * in the same sector.
4585 static bool nand_ecc_strength_good(struct mtd_info
*mtd
)
4587 struct nand_chip
*chip
= mtd_to_nand(mtd
);
4588 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
4591 if (ecc
->size
== 0 || chip
->ecc_step_ds
== 0)
4592 /* Not enough information */
4596 * We get the number of corrected bits per page to compare
4597 * the correction density.
4599 corr
= (mtd
->writesize
* ecc
->strength
) / ecc
->size
;
4600 ds_corr
= (mtd
->writesize
* chip
->ecc_strength_ds
) / chip
->ecc_step_ds
;
4602 return corr
>= ds_corr
&& ecc
->strength
>= chip
->ecc_strength_ds
;
4606 * nand_scan_tail - [NAND Interface] Scan for the NAND device
4607 * @mtd: MTD device structure
4609 * This is the second phase of the normal nand_scan() function. It fills out
4610 * all the uninitialized function pointers with the defaults and scans for a
4611 * bad block table if appropriate.
4613 int nand_scan_tail(struct mtd_info
*mtd
)
4615 struct nand_chip
*chip
= mtd_to_nand(mtd
);
4616 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
4617 struct nand_buffers
*nbuf
;
4620 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4621 if (WARN_ON((chip
->bbt_options
& NAND_BBT_NO_OOB_BBM
) &&
4622 !(chip
->bbt_options
& NAND_BBT_USE_FLASH
)))
4625 if (!(chip
->options
& NAND_OWN_BUFFERS
)) {
4626 nbuf
= kzalloc(sizeof(*nbuf
) + mtd
->writesize
4627 + mtd
->oobsize
* 3, GFP_KERNEL
);
4630 nbuf
->ecccalc
= (uint8_t *)(nbuf
+ 1);
4631 nbuf
->ecccode
= nbuf
->ecccalc
+ mtd
->oobsize
;
4632 nbuf
->databuf
= nbuf
->ecccode
+ mtd
->oobsize
;
4634 chip
->buffers
= nbuf
;
4640 /* Set the internal oob buffer location, just after the page data */
4641 chip
->oob_poi
= chip
->buffers
->databuf
+ mtd
->writesize
;
4644 * If no default placement scheme is given, select an appropriate one.
4646 if (!mtd
->ooblayout
&&
4647 !(ecc
->mode
== NAND_ECC_SOFT
&& ecc
->algo
== NAND_ECC_BCH
)) {
4648 switch (mtd
->oobsize
) {
4651 mtd_set_ooblayout(mtd
, &nand_ooblayout_sp_ops
);
4655 mtd_set_ooblayout(mtd
, &nand_ooblayout_lp_hamming_ops
);
4658 WARN(1, "No oob scheme defined for oobsize %d\n",
4665 if (!chip
->write_page
)
4666 chip
->write_page
= nand_write_page
;
4669 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
4670 * selected and we have 256 byte pagesize fallback to software ECC
4673 switch (ecc
->mode
) {
4674 case NAND_ECC_HW_OOB_FIRST
:
4675 /* Similar to NAND_ECC_HW, but a separate read_page handle */
4676 if (!ecc
->calculate
|| !ecc
->correct
|| !ecc
->hwctl
) {
4677 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4681 if (!ecc
->read_page
)
4682 ecc
->read_page
= nand_read_page_hwecc_oob_first
;
4685 /* Use standard hwecc read page function? */
4686 if (!ecc
->read_page
)
4687 ecc
->read_page
= nand_read_page_hwecc
;
4688 if (!ecc
->write_page
)
4689 ecc
->write_page
= nand_write_page_hwecc
;
4690 if (!ecc
->read_page_raw
)
4691 ecc
->read_page_raw
= nand_read_page_raw
;
4692 if (!ecc
->write_page_raw
)
4693 ecc
->write_page_raw
= nand_write_page_raw
;
4695 ecc
->read_oob
= nand_read_oob_std
;
4696 if (!ecc
->write_oob
)
4697 ecc
->write_oob
= nand_write_oob_std
;
4698 if (!ecc
->read_subpage
)
4699 ecc
->read_subpage
= nand_read_subpage
;
4700 if (!ecc
->write_subpage
&& ecc
->hwctl
&& ecc
->calculate
)
4701 ecc
->write_subpage
= nand_write_subpage_hwecc
;
4703 case NAND_ECC_HW_SYNDROME
:
4704 if ((!ecc
->calculate
|| !ecc
->correct
|| !ecc
->hwctl
) &&
4706 ecc
->read_page
== nand_read_page_hwecc
||
4708 ecc
->write_page
== nand_write_page_hwecc
)) {
4709 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4713 /* Use standard syndrome read/write page function? */
4714 if (!ecc
->read_page
)
4715 ecc
->read_page
= nand_read_page_syndrome
;
4716 if (!ecc
->write_page
)
4717 ecc
->write_page
= nand_write_page_syndrome
;
4718 if (!ecc
->read_page_raw
)
4719 ecc
->read_page_raw
= nand_read_page_raw_syndrome
;
4720 if (!ecc
->write_page_raw
)
4721 ecc
->write_page_raw
= nand_write_page_raw_syndrome
;
4723 ecc
->read_oob
= nand_read_oob_syndrome
;
4724 if (!ecc
->write_oob
)
4725 ecc
->write_oob
= nand_write_oob_syndrome
;
4727 if (mtd
->writesize
>= ecc
->size
) {
4728 if (!ecc
->strength
) {
4729 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4735 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4736 ecc
->size
, mtd
->writesize
);
4737 ecc
->mode
= NAND_ECC_SOFT
;
4738 ecc
->algo
= NAND_ECC_HAMMING
;
4741 ret
= nand_set_ecc_soft_ops(mtd
);
4749 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
4750 ecc
->read_page
= nand_read_page_raw
;
4751 ecc
->write_page
= nand_write_page_raw
;
4752 ecc
->read_oob
= nand_read_oob_std
;
4753 ecc
->read_page_raw
= nand_read_page_raw
;
4754 ecc
->write_page_raw
= nand_write_page_raw
;
4755 ecc
->write_oob
= nand_write_oob_std
;
4756 ecc
->size
= mtd
->writesize
;
4762 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc
->mode
);
4767 /* For many systems, the standard OOB write also works for raw */
4768 if (!ecc
->read_oob_raw
)
4769 ecc
->read_oob_raw
= ecc
->read_oob
;
4770 if (!ecc
->write_oob_raw
)
4771 ecc
->write_oob_raw
= ecc
->write_oob
;
4773 /* propagate ecc info to mtd_info */
4774 mtd
->ecc_strength
= ecc
->strength
;
4775 mtd
->ecc_step_size
= ecc
->size
;
4778 * Set the number of read / write steps for one page depending on ECC
4781 ecc
->steps
= mtd
->writesize
/ ecc
->size
;
4782 if (ecc
->steps
* ecc
->size
!= mtd
->writesize
) {
4783 WARN(1, "Invalid ECC parameters\n");
4787 ecc
->total
= ecc
->steps
* ecc
->bytes
;
4788 if (ecc
->total
> mtd
->oobsize
) {
4789 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4795 * The number of bytes available for a client to place data into
4796 * the out of band area.
4798 ret
= mtd_ooblayout_count_freebytes(mtd
);
4802 mtd
->oobavail
= ret
;
4804 /* ECC sanity check: warn if it's too weak */
4805 if (!nand_ecc_strength_good(mtd
))
4806 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4809 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
4810 if (!(chip
->options
& NAND_NO_SUBPAGE_WRITE
) && nand_is_slc(chip
)) {
4811 switch (ecc
->steps
) {
4813 mtd
->subpage_sft
= 1;
4818 mtd
->subpage_sft
= 2;
4822 chip
->subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
4824 /* Initialize state */
4825 chip
->state
= FL_READY
;
4827 /* Invalidate the pagebuffer reference */
4830 /* Large page NAND with SOFT_ECC should support subpage reads */
4831 switch (ecc
->mode
) {
4833 if (chip
->page_shift
> 9)
4834 chip
->options
|= NAND_SUBPAGE_READ
;
4841 /* Fill in remaining MTD driver data */
4842 mtd
->type
= nand_is_slc(chip
) ? MTD_NANDFLASH
: MTD_MLCNANDFLASH
;
4843 mtd
->flags
= (chip
->options
& NAND_ROM
) ? MTD_CAP_ROM
:
4845 mtd
->_erase
= nand_erase
;
4847 mtd
->_unpoint
= NULL
;
4848 mtd
->_read
= nand_read
;
4849 mtd
->_write
= nand_write
;
4850 mtd
->_panic_write
= panic_nand_write
;
4851 mtd
->_read_oob
= nand_read_oob
;
4852 mtd
->_write_oob
= nand_write_oob
;
4853 mtd
->_sync
= nand_sync
;
4855 mtd
->_unlock
= NULL
;
4856 mtd
->_suspend
= nand_suspend
;
4857 mtd
->_resume
= nand_resume
;
4858 mtd
->_reboot
= nand_shutdown
;
4859 mtd
->_block_isreserved
= nand_block_isreserved
;
4860 mtd
->_block_isbad
= nand_block_isbad
;
4861 mtd
->_block_markbad
= nand_block_markbad
;
4862 mtd
->writebufsize
= mtd
->writesize
;
4865 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4866 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4869 if (!mtd
->bitflip_threshold
)
4870 mtd
->bitflip_threshold
= DIV_ROUND_UP(mtd
->ecc_strength
* 3, 4);
4872 /* Check, if we should skip the bad block table scan */
4873 if (chip
->options
& NAND_SKIP_BBTSCAN
)
4876 /* Build bad block table */
4877 return chip
->scan_bbt(mtd
);
4879 if (!(chip
->options
& NAND_OWN_BUFFERS
))
4880 kfree(chip
->buffers
);
4883 EXPORT_SYMBOL(nand_scan_tail
);
4886 * is_module_text_address() isn't exported, and it's mostly a pointless
4887 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4888 * to call us from in-kernel code if the core NAND support is modular.
4891 #define caller_is_module() (1)
4893 #define caller_is_module() \
4894 is_module_text_address((unsigned long)__builtin_return_address(0))
4898 * nand_scan - [NAND Interface] Scan for the NAND device
4899 * @mtd: MTD device structure
4900 * @maxchips: number of chips to scan for
4902 * This fills out all the uninitialized function pointers with the defaults.
4903 * The flash ID is read and the mtd/chip structures are filled with the
4904 * appropriate values.
4906 int nand_scan(struct mtd_info
*mtd
, int maxchips
)
4910 ret
= nand_scan_ident(mtd
, maxchips
, NULL
);
4912 ret
= nand_scan_tail(mtd
);
4915 EXPORT_SYMBOL(nand_scan
);
4918 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4919 * @chip: NAND chip object
4921 void nand_cleanup(struct nand_chip
*chip
)
4923 if (chip
->ecc
.mode
== NAND_ECC_SOFT
&&
4924 chip
->ecc
.algo
== NAND_ECC_BCH
)
4925 nand_bch_free((struct nand_bch_control
*)chip
->ecc
.priv
);
4927 nand_release_data_interface(chip
);
4929 /* Free bad block table memory */
4931 if (!(chip
->options
& NAND_OWN_BUFFERS
))
4932 kfree(chip
->buffers
);
4934 /* Free bad block descriptor memory */
4935 if (chip
->badblock_pattern
&& chip
->badblock_pattern
->options
4936 & NAND_BBT_DYNAMICSTRUCT
)
4937 kfree(chip
->badblock_pattern
);
4939 EXPORT_SYMBOL_GPL(nand_cleanup
);
4942 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4943 * held by the NAND device
4944 * @chip: NAND chip object
4946 void nand_release(struct nand_chip
*chip
)
4948 mtd_device_unregister(nand_to_mtd(chip
));
4951 EXPORT_SYMBOL_GPL(nand_release
);
4953 MODULE_LICENSE("GPL");
4954 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4955 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4956 MODULE_DESCRIPTION("Generic NAND flash driver code");