MTD NAND Driver Programming Interface

Author:Thomas Gleixner

Introduction

The generic NAND driver supports almost all NAND and AG-AND based chips and connects them to the Memory Technology Devices (MTD) subsystem of the Linux Kernel.

This documentation is provided for developers who want to implement board drivers or filesystem drivers suitable for NAND devices.

Known Bugs And Assumptions

None.

Documentation hints

The function and structure docs are autogenerated. Each function and struct member has a short description which is marked with an [XXX] identifier. The following chapters explain the meaning of those identifiers.

Function identifiers [XXX]

The functions are marked with [XXX] identifiers in the short comment. The identifiers explain the usage and scope of the functions. Following identifiers are used:

  • [MTD Interface]

    These functions provide the interface to the MTD kernel API. They are not replaceable and provide functionality which is complete hardware independent.

  • [NAND Interface]

    These functions are exported and provide the interface to the NAND kernel API.

  • [GENERIC]

    Generic functions are not replaceable and provide functionality which is complete hardware independent.

  • [DEFAULT]

    Default functions provide hardware related functionality which is suitable for most of the implementations. These functions can be replaced by the board driver if necessary. Those functions are called via pointers in the NAND chip description structure. The board driver can set the functions which should be replaced by board dependent functions before calling nand_scan(). If the function pointer is NULL on entry to nand_scan() then the pointer is set to the default function which is suitable for the detected chip type.

Struct member identifiers [XXX]

The struct members are marked with [XXX] identifiers in the comment. The identifiers explain the usage and scope of the members. Following identifiers are used:

  • [INTERN]

    These members are for NAND driver internal use only and must not be modified. Most of these values are calculated from the chip geometry information which is evaluated during nand_scan().

  • [REPLACEABLE]

    Replaceable members hold hardware related functions which can be provided by the board driver. The board driver can set the functions which should be replaced by board dependent functions before calling nand_scan(). If the function pointer is NULL on entry to nand_scan() then the pointer is set to the default function which is suitable for the detected chip type.

  • [BOARDSPECIFIC]

    Board specific members hold hardware related information which must be provided by the board driver. The board driver must set the function pointers and datafields before calling nand_scan().

  • [OPTIONAL]

    Optional members can hold information relevant for the board driver. The generic NAND driver code does not use this information.

Basic board driver

For most boards it will be sufficient to provide just the basic functions and fill out some really board dependent members in the nand chip description structure.

Basic defines

At least you have to provide a nand_chip structure and a storage for the ioremap’ed chip address. You can allocate the nand_chip structure using kmalloc or you can allocate it statically. The NAND chip structure embeds an mtd structure which will be registered to the MTD subsystem. You can extract a pointer to the mtd structure from a nand_chip pointer using the nand_to_mtd() helper.

Kmalloc based example

static struct mtd_info *board_mtd;
static void __iomem *baseaddr;

Static example

static struct nand_chip board_chip;
static void __iomem *baseaddr;

Partition defines

If you want to divide your device into partitions, then define a partitioning scheme suitable to your board.

#define NUM_PARTITIONS 2
static struct mtd_partition partition_info[] = {
    { .name = "Flash partition 1",
      .offset =  0,
      .size =    8 * 1024 * 1024 },
    { .name = "Flash partition 2",
      .offset =  MTDPART_OFS_NEXT,
      .size =    MTDPART_SIZ_FULL },
};

Hardware control function

The hardware control function provides access to the control pins of the NAND chip(s). The access can be done by GPIO pins or by address lines. If you use address lines, make sure that the timing requirements are met.

GPIO based example

static void board_hwcontrol(struct mtd_info *mtd, int cmd)
{
    switch(cmd){
        case NAND_CTL_SETCLE: /* Set CLE pin high */ break;
        case NAND_CTL_CLRCLE: /* Set CLE pin low */ break;
        case NAND_CTL_SETALE: /* Set ALE pin high */ break;
        case NAND_CTL_CLRALE: /* Set ALE pin low */ break;
        case NAND_CTL_SETNCE: /* Set nCE pin low */ break;
        case NAND_CTL_CLRNCE: /* Set nCE pin high */ break;
    }
}

Address lines based example. It’s assumed that the nCE pin is driven by a chip select decoder.

static void board_hwcontrol(struct mtd_info *mtd, int cmd)
{
    struct nand_chip *this = mtd_to_nand(mtd);
    switch(cmd){
        case NAND_CTL_SETCLE: this->IO_ADDR_W |= CLE_ADRR_BIT;  break;
        case NAND_CTL_CLRCLE: this->IO_ADDR_W &= ~CLE_ADRR_BIT; break;
        case NAND_CTL_SETALE: this->IO_ADDR_W |= ALE_ADRR_BIT;  break;
        case NAND_CTL_CLRALE: this->IO_ADDR_W &= ~ALE_ADRR_BIT; break;
    }
}

Device ready function

If the hardware interface has the ready busy pin of the NAND chip connected to a GPIO or other accessible I/O pin, this function is used to read back the state of the pin. The function has no arguments and should return 0, if the device is busy (R/B pin is low) and 1, if the device is ready (R/B pin is high). If the hardware interface does not give access to the ready busy pin, then the function must not be defined and the function pointer this->dev_ready is set to NULL.

Init function

The init function allocates memory and sets up all the board specific parameters and function pointers. When everything is set up nand_scan() is called. This function tries to detect and identify then chip. If a chip is found all the internal data fields are initialized accordingly. The structure(s) have to be zeroed out first and then filled with the necessary information about the device.

static int __init board_init (void)
{
    struct nand_chip *this;
    int err = 0;

    /* Allocate memory for MTD device structure and private data */
    this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
    if (!this) {
        printk ("Unable to allocate NAND MTD device structure.\n");
        err = -ENOMEM;
        goto out;
    }

    board_mtd = nand_to_mtd(this);

    /* map physical address */
    baseaddr = ioremap(CHIP_PHYSICAL_ADDRESS, 1024);
    if (!baseaddr) {
        printk("Ioremap to access NAND chip failed\n");
        err = -EIO;
        goto out_mtd;
    }

    /* Set address of NAND IO lines */
    this->IO_ADDR_R = baseaddr;
    this->IO_ADDR_W = baseaddr;
    /* Reference hardware control function */
    this->hwcontrol = board_hwcontrol;
    /* Set command delay time, see datasheet for correct value */
    this->chip_delay = CHIP_DEPENDEND_COMMAND_DELAY;
    /* Assign the device ready function, if available */
    this->dev_ready = board_dev_ready;
    this->eccmode = NAND_ECC_SOFT;

    /* Scan to find existence of the device */
    if (nand_scan (board_mtd, 1)) {
        err = -ENXIO;
        goto out_ior;
    }

    add_mtd_partitions(board_mtd, partition_info, NUM_PARTITIONS);
    goto out;

out_ior:
    iounmap(baseaddr);
out_mtd:
    kfree (this);
out:
    return err;
}
module_init(board_init);

Exit function

The exit function is only necessary if the driver is compiled as a module. It releases all resources which are held by the chip driver and unregisters the partitions in the MTD layer.

#ifdef MODULE
static void __exit board_cleanup (void)
{
    /* Release resources, unregister device */
    nand_release (board_mtd);

    /* unmap physical address */
    iounmap(baseaddr);

    /* Free the MTD device structure */
    kfree (mtd_to_nand(board_mtd));
}
module_exit(board_cleanup);
#endif

Advanced board driver functions

This chapter describes the advanced functionality of the NAND driver. For a list of functions which can be overridden by the board driver see the documentation of the nand_chip structure.

Multiple chip control

The nand driver can control chip arrays. Therefore the board driver must provide an own select_chip function. This function must (de)select the requested chip. The function pointer in the nand_chip structure must be set before calling nand_scan(). The maxchip parameter of nand_scan() defines the maximum number of chips to scan for. Make sure that the select_chip function can handle the requested number of chips.

The nand driver concatenates the chips to one virtual chip and provides this virtual chip to the MTD layer.

Note: The driver can only handle linear chip arrays of equally sized chips. There is no support for parallel arrays which extend the buswidth.

GPIO based example

static void board_select_chip (struct mtd_info *mtd, int chip)
{
    /* Deselect all chips, set all nCE pins high */
    GPIO(BOARD_NAND_NCE) |= 0xff;
    if (chip >= 0)
        GPIO(BOARD_NAND_NCE) &= ~ (1 << chip);
}

Address lines based example. Its assumed that the nCE pins are connected to an address decoder.

static void board_select_chip (struct mtd_info *mtd, int chip)
{
    struct nand_chip *this = mtd_to_nand(mtd);

    /* Deselect all chips */
    this->IO_ADDR_R &= ~BOARD_NAND_ADDR_MASK;
    this->IO_ADDR_W &= ~BOARD_NAND_ADDR_MASK;
    switch (chip) {
    case 0:
        this->IO_ADDR_R |= BOARD_NAND_ADDR_CHIP0;
        this->IO_ADDR_W |= BOARD_NAND_ADDR_CHIP0;
        break;
    ....
    case n:
        this->IO_ADDR_R |= BOARD_NAND_ADDR_CHIPn;
        this->IO_ADDR_W |= BOARD_NAND_ADDR_CHIPn;
        break;
    }
}

Hardware ECC support

Functions and constants

The nand driver supports three different types of hardware ECC.

  • NAND_ECC_HW3_256

    Hardware ECC generator providing 3 bytes ECC per 256 byte.

  • NAND_ECC_HW3_512

    Hardware ECC generator providing 3 bytes ECC per 512 byte.

  • NAND_ECC_HW6_512

    Hardware ECC generator providing 6 bytes ECC per 512 byte.

  • NAND_ECC_HW8_512

    Hardware ECC generator providing 6 bytes ECC per 512 byte.

If your hardware generator has a different functionality add it at the appropriate place in nand_base.c

The board driver must provide following functions:

  • enable_hwecc

    This function is called before reading / writing to the chip. Reset or initialize the hardware generator in this function. The function is called with an argument which let you distinguish between read and write operations.

  • calculate_ecc

    This function is called after read / write from / to the chip. Transfer the ECC from the hardware to the buffer. If the option NAND_HWECC_SYNDROME is set then the function is only called on write. See below.

  • correct_data

    In case of an ECC error this function is called for error detection and correction. Return 1 respectively 2 in case the error can be corrected. If the error is not correctable return -1. If your hardware generator matches the default algorithm of the nand_ecc software generator then use the correction function provided by nand_ecc instead of implementing duplicated code.

Hardware ECC with syndrome calculation

Many hardware ECC implementations provide Reed-Solomon codes and calculate an error syndrome on read. The syndrome must be converted to a standard Reed-Solomon syndrome before calling the error correction code in the generic Reed-Solomon library.

The ECC bytes must be placed immediately after the data bytes in order to make the syndrome generator work. This is contrary to the usual layout used by software ECC. The separation of data and out of band area is not longer possible. The nand driver code handles this layout and the remaining free bytes in the oob area are managed by the autoplacement code. Provide a matching oob-layout in this case. See rts_from4.c and diskonchip.c for implementation reference. In those cases we must also use bad block tables on FLASH, because the ECC layout is interfering with the bad block marker positions. See bad block table support for details.

Bad block table support

Most NAND chips mark the bad blocks at a defined position in the spare area. Those blocks must not be erased under any circumstances as the bad block information would be lost. It is possible to check the bad block mark each time when the blocks are accessed by reading the spare area of the first page in the block. This is time consuming so a bad block table is used.

The nand driver supports various types of bad block tables.

  • Per device

    The bad block table contains all bad block information of the device which can consist of multiple chips.

  • Per chip

    A bad block table is used per chip and contains the bad block information for this particular chip.

  • Fixed offset

    The bad block table is located at a fixed offset in the chip (device). This applies to various DiskOnChip devices.

  • Automatic placed

    The bad block table is automatically placed and detected either at the end or at the beginning of a chip (device)

  • Mirrored tables

    The bad block table is mirrored on the chip (device) to allow updates of the bad block table without data loss.

nand_scan() calls the function nand_default_bbt(). nand_default_bbt() selects appropriate default bad block table descriptors depending on the chip information which was retrieved by nand_scan().

The standard policy is scanning the device for bad blocks and build a ram based bad block table which allows faster access than always checking the bad block information on the flash chip itself.

Flash based tables

It may be desired or necessary to keep a bad block table in FLASH. For AG-AND chips this is mandatory, as they have no factory marked bad blocks. They have factory marked good blocks. The marker pattern is erased when the block is erased to be reused. So in case of powerloss before writing the pattern back to the chip this block would be lost and added to the bad blocks. Therefore we scan the chip(s) when we detect them the first time for good blocks and store this information in a bad block table before erasing any of the blocks.

The blocks in which the tables are stored are protected against accidental access by marking them bad in the memory bad block table. The bad block table management functions are allowed to circumvent this protection.

The simplest way to activate the FLASH based bad block table support is to set the option NAND_BBT_USE_FLASH in the bbt_option field of the nand chip structure before calling nand_scan(). For AG-AND chips is this done by default. This activates the default FLASH based bad block table functionality of the NAND driver. The default bad block table options are

  • Store bad block table per chip
  • Use 2 bits per block
  • Automatic placement at the end of the chip
  • Use mirrored tables with version numbers
  • Reserve 4 blocks at the end of the chip

User defined tables

User defined tables are created by filling out a nand_bbt_descr structure and storing the pointer in the nand_chip structure member bbt_td before calling nand_scan(). If a mirror table is necessary a second structure must be created and a pointer to this structure must be stored in bbt_md inside the nand_chip structure. If the bbt_md member is set to NULL then only the main table is used and no scan for the mirrored table is performed.

The most important field in the nand_bbt_descr structure is the options field. The options define most of the table properties. Use the predefined constants from rawnand.h to define the options.

  • Number of bits per block

    The supported number of bits is 1, 2, 4, 8.

  • Table per chip

    Setting the constant NAND_BBT_PERCHIP selects that a bad block table is managed for each chip in a chip array. If this option is not set then a per device bad block table is used.

  • Table location is absolute

    Use the option constant NAND_BBT_ABSPAGE and define the absolute page number where the bad block table starts in the field pages. If you have selected bad block tables per chip and you have a multi chip array then the start page must be given for each chip in the chip array. Note: there is no scan for a table ident pattern performed, so the fields pattern, veroffs, offs, len can be left uninitialized

  • Table location is automatically detected

    The table can either be located in the first or the last good blocks of the chip (device). Set NAND_BBT_LASTBLOCK to place the bad block table at the end of the chip (device). The bad block tables are marked and identified by a pattern which is stored in the spare area of the first page in the block which holds the bad block table. Store a pointer to the pattern in the pattern field. Further the length of the pattern has to be stored in len and the offset in the spare area must be given in the offs member of the nand_bbt_descr structure. For mirrored bad block tables different patterns are mandatory.

  • Table creation

    Set the option NAND_BBT_CREATE to enable the table creation if no table can be found during the scan. Usually this is done only once if a new chip is found.

  • Table write support

    Set the option NAND_BBT_WRITE to enable the table write support. This allows the update of the bad block table(s) in case a block has to be marked bad due to wear. The MTD interface function block_markbad is calling the update function of the bad block table. If the write support is enabled then the table is updated on FLASH.

    Note: Write support should only be enabled for mirrored tables with version control.

  • Table version control

    Set the option NAND_BBT_VERSION to enable the table version control. It’s highly recommended to enable this for mirrored tables with write support. It makes sure that the risk of losing the bad block table information is reduced to the loss of the information about the one worn out block which should be marked bad. The version is stored in 4 consecutive bytes in the spare area of the device. The position of the version number is defined by the member veroffs in the bad block table descriptor.

  • Save block contents on write

    In case that the block which holds the bad block table does contain other useful information, set the option NAND_BBT_SAVECONTENT. When the bad block table is written then the whole block is read the bad block table is updated and the block is erased and everything is written back. If this option is not set only the bad block table is written and everything else in the block is ignored and erased.

  • Number of reserved blocks

    For automatic placement some blocks must be reserved for bad block table storage. The number of reserved blocks is defined in the maxblocks member of the bad block table description structure. Reserving 4 blocks for mirrored tables should be a reasonable number. This also limits the number of blocks which are scanned for the bad block table ident pattern.

Spare area (auto)placement

The nand driver implements different possibilities for placement of filesystem data in the spare area,

  • Placement defined by fs driver
  • Automatic placement

The default placement function is automatic placement. The nand driver has built in default placement schemes for the various chiptypes. If due to hardware ECC functionality the default placement does not fit then the board driver can provide a own placement scheme.

File system drivers can provide a own placement scheme which is used instead of the default placement scheme.

Placement schemes are defined by a nand_oobinfo structure

struct nand_oobinfo {
    int useecc;
    int eccbytes;
    int eccpos[24];
    int oobfree[8][2];
};
  • useecc

    The useecc member controls the ecc and placement function. The header file include/mtd/mtd-abi.h contains constants to select ecc and placement. MTD_NANDECC_OFF switches off the ecc complete. This is not recommended and available for testing and diagnosis only. MTD_NANDECC_PLACE selects caller defined placement, MTD_NANDECC_AUTOPLACE selects automatic placement.

  • eccbytes

    The eccbytes member defines the number of ecc bytes per page.

  • eccpos

    The eccpos array holds the byte offsets in the spare area where the ecc codes are placed.

  • oobfree

    The oobfree array defines the areas in the spare area which can be used for automatic placement. The information is given in the format {offset, size}. offset defines the start of the usable area, size the length in bytes. More than one area can be defined. The list is terminated by an {0, 0} entry.

Placement defined by fs driver

The calling function provides a pointer to a nand_oobinfo structure which defines the ecc placement. For writes the caller must provide a spare area buffer along with the data buffer. The spare area buffer size is (number of pages) * (size of spare area). For reads the buffer size is (number of pages) * ((size of spare area) + (number of ecc steps per page) * sizeof (int)). The driver stores the result of the ecc check for each tuple in the spare buffer. The storage sequence is:

<spare data page 0><ecc result 0>...<ecc result n>

...

<spare data page n><ecc result 0>...<ecc result n>

This is a legacy mode used by YAFFS1.

If the spare area buffer is NULL then only the ECC placement is done according to the given scheme in the nand_oobinfo structure.

Automatic placement

Automatic placement uses the built in defaults to place the ecc bytes in the spare area. If filesystem data have to be stored / read into the spare area then the calling function must provide a buffer. The buffer size per page is determined by the oobfree array in the nand_oobinfo structure.

If the spare area buffer is NULL then only the ECC placement is done according to the default builtin scheme.

Spare area autoplacement default schemes

256 byte pagesize

Offset Content Comment
0x00 ECC byte 0 Error correction code byte 0
0x01 ECC byte 1 Error correction code byte 1
0x02 ECC byte 2 Error correction code byte 2
0x03 Autoplace 0  
0x04 Autoplace 1  
0x05 Bad block marker If any bit in this byte is zero, then this block is bad. This applies only to the first page in a block. In the remaining pages this byte is reserved
0x06 Autoplace 2  
0x07 Autoplace 3  

512 byte pagesize

Offset Content Comment
0x00 ECC byte 0 Error correction code byte 0 of the lower 256 Byte data in this page
0x01 ECC byte 1 Error correction code byte 1 of the lower 256 Bytes of data in this page
0x02 ECC byte 2 Error correction code byte 2 of the lower 256 Bytes of data in this page
0x03 ECC byte 3 Error correction code byte 0 of the upper 256 Bytes of data in this page
0x04 reserved reserved
0x05 Bad block marker If any bit in this byte is zero, then this block is bad. This applies only to the first page in a block. In the remaining pages this byte is reserved
0x06 ECC byte 4 Error correction code byte 1 of the upper 256 Bytes of data in this page
0x07 ECC byte 5 Error correction code byte 2 of the upper 256 Bytes of data in this page
0x08 - 0x0F Autoplace 0 - 7  

2048 byte pagesize

Offset Content Comment
0x00 Bad block marker If any bit in this byte is zero, then this block is bad. This applies only to the first page in a block. In the remaining pages this byte is reserved
0x01 Reserved Reserved
0x02-0x27 Autoplace 0 - 37  
0x28 ECC byte 0 Error correction code byte 0 of the first 256 Byte data in this page
0x29 ECC byte 1 Error correction code byte 1 of the first 256 Bytes of data in this page
0x2A ECC byte 2 Error correction code byte 2 of the first 256 Bytes data in this page
0x2B ECC byte 3 Error correction code byte 0 of the second 256 Bytes of data in this page
0x2C ECC byte 4 Error correction code byte 1 of the second 256 Bytes of data in this page
0x2D ECC byte 5 Error correction code byte 2 of the second 256 Bytes of data in this page
0x2E ECC byte 6 Error correction code byte 0 of the third 256 Bytes of data in this page
0x2F ECC byte 7 Error correction code byte 1 of the third 256 Bytes of data in this page
0x30 ECC byte 8 Error correction code byte 2 of the third 256 Bytes of data in this page
0x31 ECC byte 9 Error correction code byte 0 of the fourth 256 Bytes of data in this page
0x32 ECC byte 10 Error correction code byte 1 of the fourth 256 Bytes of data in this page
0x33 ECC byte 11 Error correction code byte 2 of the fourth 256 Bytes of data in this page
0x34 ECC byte 12 Error correction code byte 0 of the fifth 256 Bytes of data in this page
0x35 ECC byte 13 Error correction code byte 1 of the fifth 256 Bytes of data in this page
0x36 ECC byte 14 Error correction code byte 2 of the fifth 256 Bytes of data in this page
0x37 ECC byte 15 Error correction code byte 0 of the sixth 256 Bytes of data in this page
0x38 ECC byte 16 Error correction code byte 1 of the sixth 256 Bytes of data in this page
0x39 ECC byte 17 Error correction code byte 2 of the sixth 256 Bytes of data in this page
0x3A ECC byte 18 Error correction code byte 0 of the seventh 256 Bytes of data in this page
0x3B ECC byte 19 Error correction code byte 1 of the seventh 256 Bytes of data in this page
0x3C ECC byte 20 Error correction code byte 2 of the seventh 256 Bytes of data in this page
0x3D ECC byte 21 Error correction code byte 0 of the eighth 256 Bytes of data in this page
0x3E ECC byte 22 Error correction code byte 1 of the eighth 256 Bytes of data in this page
0x3F ECC byte 23 Error correction code byte 2 of the eighth 256 Bytes of data in this page

Filesystem support

The NAND driver provides all necessary functions for a filesystem via the MTD interface.

Filesystems must be aware of the NAND peculiarities and restrictions. One major restrictions of NAND Flash is, that you cannot write as often as you want to a page. The consecutive writes to a page, before erasing it again, are restricted to 1-3 writes, depending on the manufacturers specifications. This applies similar to the spare area.

Therefore NAND aware filesystems must either write in page size chunks or hold a writebuffer to collect smaller writes until they sum up to pagesize. Available NAND aware filesystems: JFFS2, YAFFS.

The spare area usage to store filesystem data is controlled by the spare area placement functionality which is described in one of the earlier chapters.

Tools

The MTD project provides a couple of helpful tools to handle NAND Flash.

  • flasherase, flasheraseall: Erase and format FLASH partitions
  • nandwrite: write filesystem images to NAND FLASH
  • nanddump: dump the contents of a NAND FLASH partitions

These tools are aware of the NAND restrictions. Please use those tools instead of complaining about errors which are caused by non NAND aware access methods.

Constants

This chapter describes the constants which might be relevant for a driver developer.

Chip option constants

Constants for chip id table

These constants are defined in rawnand.h. They are OR-ed together to describe the chip functionality:

/* Buswitdh is 16 bit */
#define NAND_BUSWIDTH_16    0x00000002
/* Device supports partial programming without padding */
#define NAND_NO_PADDING     0x00000004
/* Chip has cache program function */
#define NAND_CACHEPRG       0x00000008
/* Chip has copy back function */
#define NAND_COPYBACK       0x00000010
/* AND Chip which has 4 banks and a confusing page / block
 * assignment. See Renesas datasheet for further information */
#define NAND_IS_AND     0x00000020
/* Chip has a array of 4 pages which can be read without
 * additional ready /busy waits */
#define NAND_4PAGE_ARRAY    0x00000040

Constants for runtime options

These constants are defined in rawnand.h. They are OR-ed together to describe the functionality:

/* The hw ecc generator provides a syndrome instead a ecc value on read
 * This can only work if we have the ecc bytes directly behind the
 * data bytes. Applies for DOC and AG-AND Renesas HW Reed Solomon generators */
#define NAND_HWECC_SYNDROME 0x00020000

ECC selection constants

Use these constants to select the ECC algorithm:

/* No ECC. Usage is not recommended ! */
#define NAND_ECC_NONE       0
/* Software ECC 3 byte ECC per 256 Byte data */
#define NAND_ECC_SOFT       1
/* Hardware ECC 3 byte ECC per 256 Byte data */
#define NAND_ECC_HW3_256    2
/* Hardware ECC 3 byte ECC per 512 Byte data */
#define NAND_ECC_HW3_512    3
/* Hardware ECC 6 byte ECC per 512 Byte data */
#define NAND_ECC_HW6_512    4
/* Hardware ECC 6 byte ECC per 512 Byte data */
#define NAND_ECC_HW8_512    6

Structures

This chapter contains the autogenerated documentation of the structures which are used in the NAND driver and might be relevant for a driver developer. Each struct member has a short description which is marked with an [XXX] identifier. See the chapter “Documentation hints” for an explanation.

struct nand_id

NAND id structure

Definition

struct nand_id {
  u8 data;
  int len;
};

Members

data
buffer containing the id bytes.
len
ID length.
struct nand_hw_control

Control structure for hardware controller (e.g ECC generator) shared among independent devices

Definition

struct nand_hw_control {
  spinlock_t lock;
  struct nand_chip * active;
  wait_queue_head_t wq;
};

Members

lock
protection lock
active
the mtd device which holds the controller currently
wq
wait queue to sleep on if a NAND operation is in progress used instead of the per chip wait queue when a hw controller is available.
struct nand_ecc_step_info

ECC step information of ECC engine

Definition

struct nand_ecc_step_info {
  int stepsize;
  const int * strengths;
  int nstrengths;
};

Members

stepsize
data bytes per ECC step
strengths
array of supported strengths
nstrengths
number of supported strengths
struct nand_ecc_caps

capability of ECC engine

Definition

struct nand_ecc_caps {
  const struct nand_ecc_step_info * stepinfos;
  int nstepinfos;
  int (* calc_ecc_bytes) (int step_size, int strength);
};

Members

stepinfos
array of ECC step information
nstepinfos
number of ECC step information
calc_ecc_bytes
driver’s hook to calculate ECC bytes per step
struct nand_ecc_ctrl

Control structure for ECC

Definition

struct nand_ecc_ctrl {
  nand_ecc_modes_t mode;
  enum nand_ecc_algo algo;
  int steps;
  int size;
  int bytes;
  int total;
  int strength;
  int prepad;
  int postpad;
  unsigned int options;
  void * priv;
  void (* hwctl) (struct mtd_info *mtd, int mode);
  int (* calculate) (struct mtd_info *mtd, const uint8_t *dat, uint8_t *ecc_code);
  int (* correct) (struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc);
  int (* read_page_raw) (struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf, int oob_required, int page);
  int (* write_page_raw) (struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf, int oob_required, int page);
  int (* read_page) (struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf, int oob_required, int page);
  int (* read_subpage) (struct mtd_info *mtd, struct nand_chip *chip, uint32_t offs, uint32_t len, uint8_t *buf, int page);
  int (* write_subpage) (struct mtd_info *mtd, struct nand_chip *chip,uint32_t offset, uint32_t data_len, const uint8_t *data_buf, int oob_required, int page);
  int (* write_page) (struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf, int oob_required, int page);
  int (* write_oob_raw) (struct mtd_info *mtd, struct nand_chip *chip, int page);
  int (* read_oob_raw) (struct mtd_info *mtd, struct nand_chip *chip, int page);
  int (* read_oob) (struct mtd_info *mtd, struct nand_chip *chip, int page);
  int (* write_oob) (struct mtd_info *mtd, struct nand_chip *chip, int page);
};

Members

mode
ECC mode
algo
ECC algorithm
steps
number of ECC steps per page
size
data bytes per ECC step
bytes
ECC bytes per step
total
total number of ECC bytes per page
strength
max number of correctible bits per ECC step
prepad
padding information for syndrome based ECC generators
postpad
padding information for syndrome based ECC generators
options
ECC specific options (see NAND_ECC_XXX flags defined above)
priv
pointer to private ECC control data
hwctl
function to control hardware ECC generator. Must only be provided if an hardware ECC is available
calculate
function for ECC calculation or readback from ECC hardware
correct
function for ECC correction, matching to ECC generator (sw/hw). Should return a positive number representing the number of corrected bitflips, -EBADMSG if the number of bitflips exceed ECC strength, or any other error code if the error is not directly related to correction. If -EBADMSG is returned the input buffers should be left untouched.
read_page_raw
function to read a raw page without ECC. This function should hide the specific layout used by the ECC controller and always return contiguous in-band and out-of-band data even if they’re not stored contiguously on the NAND chip (e.g. NAND_ECC_HW_SYNDROME interleaves in-band and out-of-band data).
write_page_raw
function to write a raw page without ECC. This function should hide the specific layout used by the ECC controller and consider the passed data as contiguous in-band and out-of-band data. ECC controller is responsible for doing the appropriate transformations to adapt to its specific layout (e.g. NAND_ECC_HW_SYNDROME interleaves in-band and out-of-band data).
read_page
function to read a page according to the ECC generator requirements; returns maximum number of bitflips corrected in any single ECC step, -EIO hw error
read_subpage
function to read parts of the page covered by ECC; returns same as read_page()
write_subpage
function to write parts of the page covered by ECC.
write_page
function to write a page according to the ECC generator requirements.
write_oob_raw
function to write chip OOB data without ECC
read_oob_raw
function to read chip OOB data without ECC
read_oob
function to read chip OOB data
write_oob
function to write chip OOB data
struct nand_buffers

buffer structure for read/write

Definition

struct nand_buffers {
  uint8_t * ecccalc;
  uint8_t * ecccode;
  uint8_t * databuf;
};

Members

ecccalc
buffer pointer for calculated ECC, size is oobsize.
ecccode
buffer pointer for ECC read from flash, size is oobsize.
databuf
buffer pointer for data, size is (page size + oobsize).

Description

Do not change the order of buffers. databuf and oobrbuf must be in consecutive order.

struct nand_sdr_timings

SDR NAND chip timings

Definition

struct nand_sdr_timings {
  u64 tBERS_max;
  u32 tCCS_min;
  u64 tPROG_max;
  u64 tR_max;
  u32 tALH_min;
  u32 tADL_min;
  u32 tALS_min;
  u32 tAR_min;
  u32 tCEA_max;
  u32 tCEH_min;
  u32 tCH_min;
  u32 tCHZ_max;
  u32 tCLH_min;
  u32 tCLR_min;
  u32 tCLS_min;
  u32 tCOH_min;
  u32 tCS_min;
  u32 tDH_min;
  u32 tDS_min;
  u32 tFEAT_max;
  u32 tIR_min;
  u32 tITC_max;
  u32 tRC_min;
  u32 tREA_max;
  u32 tREH_min;
  u32 tRHOH_min;
  u32 tRHW_min;
  u32 tRHZ_max;
  u32 tRLOH_min;
  u32 tRP_min;
  u32 tRR_min;
  u64 tRST_max;
  u32 tWB_max;
  u32 tWC_min;
  u32 tWH_min;
  u32 tWHR_min;
  u32 tWP_min;
  u32 tWW_min;
};

Members

tBERS_max
Block erase time
tCCS_min
Change column setup time
tPROG_max
Page program time
tR_max
Page read time
tALH_min
ALE hold time
tADL_min
ALE to data loading time
tALS_min
ALE setup time
tAR_min
ALE to RE# delay
tCEA_max
CE# access time
tCEH_min
CE# high hold time
tCH_min
CE# hold time
tCHZ_max
CE# high to output hi-Z
tCLH_min
CLE hold time
tCLR_min
CLE to RE# delay
tCLS_min
CLE setup time
tCOH_min
CE# high to output hold
tCS_min
CE# setup time
tDH_min
Data hold time
tDS_min
Data setup time
tFEAT_max
Busy time for Set Features and Get Features
tIR_min
Output hi-Z to RE# low
tITC_max
Interface and Timing Mode Change time
tRC_min
RE# cycle time
tREA_max
RE# access time
tREH_min
RE# high hold time
tRHOH_min
RE# high to output hold
tRHW_min
RE# high to WE# low
tRHZ_max
RE# high to output hi-Z
tRLOH_min
RE# low to output hold
tRP_min
RE# pulse width
tRR_min
Ready to RE# low (data only)
tRST_max
Device reset time, measured from the falling edge of R/B# to the rising edge of R/B#.
tWB_max
WE# high to SR[6] low
tWC_min
WE# cycle time
tWH_min
WE# high hold time
tWHR_min
WE# high to RE# low
tWP_min
WE# pulse width
tWW_min
WP# transition to WE# low

Description

This struct defines the timing requirements of a SDR NAND chip. These information can be found in every NAND datasheets and the timings meaning are described in the ONFI specifications: www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing Parameters)

All these timings are expressed in picoseconds.

enum nand_data_interface_type

NAND interface timing type

Constants

NAND_SDR_IFACE
Single Data Rate interface
struct nand_data_interface

NAND interface timing

Definition

struct nand_data_interface {
  enum nand_data_interface_type type;
  union timings;
};

Members

type
type of the timing
timings
The timing, type according to type
const struct nand_sdr_timings * nand_get_sdr_timings(const struct nand_data_interface * conf)

get SDR timing from data interface

Parameters

const struct nand_data_interface * conf
The data interface
struct nand_manufacturer_ops

NAND Manufacturer operations

Definition

struct nand_manufacturer_ops {
  void (* detect) (struct nand_chip *chip);
  int (* init) (struct nand_chip *chip);
  void (* cleanup) (struct nand_chip *chip);
};

Members

detect
detect the NAND memory organization and capabilities
init
initialize all vendor specific fields (like the ->:c:func:read_retry() implementation) if any.
cleanup
the ->:c:func:init() function may have allocated resources, ->:c:func:cleanup() is here to let vendor specific code release those resources.
struct nand_chip

NAND Private Flash Chip Data

Definition

struct nand_chip {
  struct mtd_info mtd;
  void __iomem * IO_ADDR_R;
  void __iomem * IO_ADDR_W;
  uint8_t (* read_byte) (struct mtd_info *mtd);
  u16 (* read_word) (struct mtd_info *mtd);
  void (* write_byte) (struct mtd_info *mtd, uint8_t byte);
  void (* write_buf) (struct mtd_info *mtd, const uint8_t *buf, int len);
  void (* read_buf) (struct mtd_info *mtd, uint8_t *buf, int len);
  void (* select_chip) (struct mtd_info *mtd, int chip);
  int (* block_bad) (struct mtd_info *mtd, loff_t ofs);
  int (* block_markbad) (struct mtd_info *mtd, loff_t ofs);
  void (* cmd_ctrl) (struct mtd_info *mtd, int dat, unsigned int ctrl);
  int (* dev_ready) (struct mtd_info *mtd);
  void (* cmdfunc) (struct mtd_info *mtd, unsigned command, int column, int page_addr);
  int(* waitfunc) (struct mtd_info *mtd, struct nand_chip *this);
  int (* erase) (struct mtd_info *mtd, int page);
  int (* scan_bbt) (struct mtd_info *mtd);
  int (* onfi_set_features) (struct mtd_info *mtd, struct nand_chip *chip, int feature_addr, uint8_t *subfeature_para);
  int (* onfi_get_features) (struct mtd_info *mtd, struct nand_chip *chip, int feature_addr, uint8_t *subfeature_para);
  int (* setup_read_retry) (struct mtd_info *mtd, int retry_mode);
  int (* setup_data_interface) (struct mtd_info *mtd, int chipnr, const struct nand_data_interface *conf);
  int chip_delay;
  unsigned int options;
  unsigned int bbt_options;
  int page_shift;
  int phys_erase_shift;
  int bbt_erase_shift;
  int chip_shift;
  int numchips;
  uint64_t chipsize;
  int pagemask;
  int pagebuf;
  unsigned int pagebuf_bitflips;
  int subpagesize;
  uint8_t bits_per_cell;
  uint16_t ecc_strength_ds;
  uint16_t ecc_step_ds;
  int onfi_timing_mode_default;
  int badblockpos;
  int badblockbits;
  struct nand_id id;
  int onfi_version;
  int jedec_version;
  union manufacturer;
};

Members

mtd
MTD device registered to the MTD framework
IO_ADDR_R
[BOARDSPECIFIC] address to read the 8 I/O lines of the flash device
IO_ADDR_W
[BOARDSPECIFIC] address to write the 8 I/O lines of the flash device.
read_byte
[REPLACEABLE] read one byte from the chip
read_word
[REPLACEABLE] read one word from the chip
write_byte
[REPLACEABLE] write a single byte to the chip on the low 8 I/O lines
write_buf
[REPLACEABLE] write data from the buffer to the chip
read_buf
[REPLACEABLE] read data from the chip into the buffer
select_chip
[REPLACEABLE] select chip nr
block_bad
[REPLACEABLE] check if a block is bad, using OOB markers
block_markbad
[REPLACEABLE] mark a block bad
cmd_ctrl
[BOARDSPECIFIC] hardwarespecific function for controlling ALE/CLE/nCE. Also used to write command and address
dev_ready
[BOARDSPECIFIC] hardwarespecific function for accessing device ready/busy line. If set to NULL no access to ready/busy is available and the ready/busy information is read from the chip status register.
cmdfunc
[REPLACEABLE] hardwarespecific function for writing commands to the chip.
waitfunc
[REPLACEABLE] hardwarespecific function for wait on ready.
erase
[REPLACEABLE] erase function
scan_bbt
[REPLACEABLE] function to scan bad block table
onfi_set_features
[REPLACEABLE] set the features for ONFI nand
onfi_get_features
[REPLACEABLE] get the features for ONFI nand
setup_read_retry
[FLASHSPECIFIC] flash (vendor) specific function for setting the read-retry mode. Mostly needed for MLC NAND.
setup_data_interface
[OPTIONAL] setup the data interface and timing. If chipnr is set to NAND_DATA_IFACE_CHECK_ONLY this means the configuration should not be applied but only checked.
chip_delay
[BOARDSPECIFIC] chip dependent delay for transferring data from array to read regs (tR).
options
[BOARDSPECIFIC] various chip options. They can partly be set to inform nand_scan about special functionality. See the defines for further explanation.
bbt_options
[INTERN] bad block specific options. All options used here must come from bbm.h. By default, these options will be copied to the appropriate nand_bbt_descr’s.
page_shift
[INTERN] number of address bits in a page (column address bits).
phys_erase_shift
[INTERN] number of address bits in a physical eraseblock
bbt_erase_shift
[INTERN] number of address bits in a bbt entry
chip_shift
[INTERN] number of address bits in one chip
numchips
[INTERN] number of physical chips
chipsize
[INTERN] the size of one chip for multichip arrays
pagemask
[INTERN] page number mask = number of (pages / chip) - 1
pagebuf
[INTERN] holds the pagenumber which is currently in data_buf.
pagebuf_bitflips
[INTERN] holds the bitflip count for the page which is currently in data_buf.
subpagesize
[INTERN] holds the subpagesize
bits_per_cell
[INTERN] number of bits per cell. i.e., 1 means SLC.
ecc_strength_ds
[INTERN] ECC correctability from the datasheet. Minimum amount of bit errors per ecc_step_ds guaranteed to be correctable. If unknown, set to zero.
ecc_step_ds
[INTERN] ECC step required by the ecc_strength_ds, also from the datasheet. It is the recommended ECC step size, if known; if unknown, set to zero.
onfi_timing_mode_default
[INTERN] default ONFI timing mode. This field is set to the actually used ONFI mode if the chip is ONFI compliant or deduced from the datasheet if the NAND chip is not ONFI compliant.
badblockpos
[INTERN] position of the bad block marker in the oob area.
badblockbits
[INTERN] minimum number of set bits in a good block’s bad block marker position; i.e., BBM == 11110111b is not bad when badblockbits == 7
id
[INTERN] holds NAND ID
onfi_version
[INTERN] holds the chip ONFI version (BCD encoded), non 0 if ONFI supported.
jedec_version
[INTERN] holds the chip JEDEC version (BCD encoded), non 0 if JEDEC supported.
manufacturer
[INTERN] Contains manufacturer information
struct nand_flash_dev

NAND Flash Device ID Structure

Definition

struct nand_flash_dev {
  char * name;
  union ecc;
  int onfi_timing_mode_default;
};

Members

name
a human-readable name of the NAND chip
ecc
The ECC step required by the ecc.strength_ds, same as the ecc_step_ds in nand_chip{}, also from the datasheet. For example, the “4bit ECC for each 512Byte” can be set with NAND_ECC_INFO(4, 512).
onfi_timing_mode_default
the default ONFI timing mode entered after a NAND reset. Should be deduced from timings described in the datasheet.
struct nand_manufacturer

NAND Flash Manufacturer structure

Definition

struct nand_manufacturer {
  int id;
  char * name;
  const struct nand_manufacturer_ops * ops;
};

Members

id
manufacturer ID code of device.
name
Manufacturer name
ops
manufacturer operations
struct platform_nand_chip

chip level device structure

Definition

struct platform_nand_chip {
  int nr_chips;
  int chip_offset;
  int nr_partitions;
  struct mtd_partition * partitions;
  int chip_delay;
  unsigned int options;
  unsigned int bbt_options;
  const char ** part_probe_types;
};

Members

nr_chips
max. number of chips to scan for
chip_offset
chip number offset
nr_partitions
number of partitions pointed to by partitions (or zero)
partitions
mtd partition list
chip_delay
R/B delay value in us
options
Option flags, e.g. 16bit buswidth
bbt_options
BBT option flags, e.g. NAND_BBT_USE_FLASH
part_probe_types
NULL-terminated array of probe types
struct platform_nand_ctrl

controller level device structure

Definition

struct platform_nand_ctrl {
  int (* probe) (struct platform_device *pdev);
  void (* remove) (struct platform_device *pdev);
  void (* hwcontrol) (struct mtd_info *mtd, int cmd);
  int (* dev_ready) (struct mtd_info *mtd);
  void (* select_chip) (struct mtd_info *mtd, int chip);
  void (* cmd_ctrl) (struct mtd_info *mtd, int dat, unsigned int ctrl);
  void (* write_buf) (struct mtd_info *mtd, const uint8_t *buf, int len);
  void (* read_buf) (struct mtd_info *mtd, uint8_t *buf, int len);
  unsigned char (* read_byte) (struct mtd_info *mtd);
  void * priv;
};

Members

probe
platform specific function to probe/setup hardware
remove
platform specific function to remove/teardown hardware
hwcontrol
platform specific hardware control structure
dev_ready
platform specific function to read ready/busy pin
select_chip
platform specific chip select function
cmd_ctrl
platform specific function for controlling ALE/CLE/nCE. Also used to write command and address
write_buf
platform specific function for write buffer
read_buf
platform specific function for read buffer
read_byte
platform specific function to read one byte from chip
priv
private data to transport driver specific settings

Description

All fields are optional and depend on the hardware driver requirements

struct platform_nand_data

container structure for platform-specific data

Definition

struct platform_nand_data {
  struct platform_nand_chip chip;
  struct platform_nand_ctrl ctrl;
};

Members

chip
chip level chip structure
ctrl
controller level device structure
int nand_opcode_8bits(unsigned int command)

Parameters

unsigned int command
opcode to check

Public Functions Provided

This chapter contains the autogenerated documentation of the NAND kernel API functions which are exported. Each function has a short description which is marked with an [XXX] identifier. See the chapter “Documentation hints” for an explanation.

void nand_wait_ready(struct mtd_info * mtd)

[GENERIC] Wait for the ready pin after commands.

Parameters

struct mtd_info * mtd
MTD device structure

Description

Wait for the ready pin after a command, and warn if a timeout occurs.

int nand_check_erased_ecc_chunk(void * data, int datalen, void * ecc, int ecclen, void * extraoob, int extraooblen, int bitflips_threshold)

check if an ECC chunk contains (almost) only 0xff data

Parameters

void * data
data buffer to test
int datalen
data length
void * ecc
ECC buffer
int ecclen
ECC length
void * extraoob
extra OOB buffer
int extraooblen
extra OOB length
int bitflips_threshold
maximum number of bitflips

Description

Check if a data buffer and its associated ECC and OOB data contains only 0xff pattern, which means the underlying region has been erased and is ready to be programmed. The bitflips_threshold specify the maximum number of bitflips before considering the region as not erased.

Note

1/ ECC algorithms are working on pre-defined block sizes which are usually
different from the NAND page size. When fixing bitflips, ECC engines will report the number of errors per chunk, and the NAND core infrastructure expect you to return the maximum number of bitflips for the whole page. This is why you should always use this function on a single chunk and not on the whole page. After checking each chunk you should update your max_bitflips value accordingly.
2/ When checking for bitflips in erased pages you should not only check
the payload data but also their associated ECC data, because a user might have programmed almost all bits to 1 but a few. In this case, we shouldn’t consider the chunk as erased, and checking ECC bytes prevent this case.
3/ The extraoob argument is optional, and should be used if some of your OOB
data are protected by the ECC engine. It could also be used if you support subpages and want to attach some extra OOB data to an ECC chunk.

Returns a positive number of bitflips less than or equal to bitflips_threshold, or -ERROR_CODE for bitflips in excess of the threshold. In case of success, the passed buffers are filled with 0xff.

int nand_read_page_raw(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[INTERN] read raw page data without ecc

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read

Description

Not for syndrome calculating ECC controllers, which use a special oob layout.

int nand_read_oob_std(struct mtd_info * mtd, struct nand_chip * chip, int page)

[REPLACEABLE] the most common OOB data read function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
int page
page number to read
int nand_read_oob_syndrome(struct mtd_info * mtd, struct nand_chip * chip, int page)

[REPLACEABLE] OOB data read function for HW ECC with syndromes

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
int page
page number to read
int nand_write_oob_std(struct mtd_info * mtd, struct nand_chip * chip, int page)

[REPLACEABLE] the most common OOB data write function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
int page
page number to write
int nand_write_oob_syndrome(struct mtd_info * mtd, struct nand_chip * chip, int page)

[REPLACEABLE] OOB data write function for HW ECC with syndrome - only for large page flash

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
int page
page number to write
int nand_write_page_raw(struct mtd_info * mtd, struct nand_chip * chip, const uint8_t * buf, int oob_required, int page)

[INTERN] raw page write function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write

Description

Not for syndrome calculating ECC controllers, which use a special oob layout.

int nand_onfi_get_set_features_notsupp(struct mtd_info * mtd, struct nand_chip * chip, int addr, u8 * subfeature_param)

set/get features stub returning -ENOTSUPP

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
nand chip info structure
int addr
feature address.
u8 * subfeature_param
the subfeature parameters, a four bytes array.

Description

Should be used by NAND controller drivers that do not support the SET/GET FEATURES operations.

int nand_scan_ident(struct mtd_info * mtd, int maxchips, struct nand_flash_dev * table)

[NAND Interface] Scan for the NAND device

Parameters

struct mtd_info * mtd
MTD device structure
int maxchips
number of chips to scan for
struct nand_flash_dev * table
alternative NAND ID table

Description

This is the first phase of the normal nand_scan() function. It reads the flash ID and sets up MTD fields accordingly.

int nand_check_ecc_caps(struct nand_chip * chip, const struct nand_ecc_caps * caps, int oobavail)

check the sanity of preset ECC settings

Parameters

struct nand_chip * chip
nand chip info structure
const struct nand_ecc_caps * caps
ECC caps info structure
int oobavail
OOB size that the ECC engine can use

Description

When ECC step size and strength are already set, check if they are supported by the controller and the calculated ECC bytes fit within the chip’s OOB. On success, the calculated ECC bytes is set.

int nand_match_ecc_req(struct nand_chip * chip, const struct nand_ecc_caps * caps, int oobavail)

meet the chip’s requirement with least ECC bytes

Parameters

struct nand_chip * chip
nand chip info structure
const struct nand_ecc_caps * caps
ECC engine caps info structure
int oobavail
OOB size that the ECC engine can use

Description

If a chip’s ECC requirement is provided, try to meet it with the least number of ECC bytes (i.e. with the largest number of OOB-free bytes). On success, the chosen ECC settings are set.

int nand_maximize_ecc(struct nand_chip * chip, const struct nand_ecc_caps * caps, int oobavail)

choose the max ECC strength available

Parameters

struct nand_chip * chip
nand chip info structure
const struct nand_ecc_caps * caps
ECC engine caps info structure
int oobavail
OOB size that the ECC engine can use

Description

Choose the max ECC strength that is supported on the controller, and can fit within the chip’s OOB. On success, the chosen ECC settings are set.

int nand_scan_tail(struct mtd_info * mtd)

[NAND Interface] Scan for the NAND device

Parameters

struct mtd_info * mtd
MTD device structure

Description

This is the second phase of the normal nand_scan() function. It fills out all the uninitialized function pointers with the defaults and scans for a bad block table if appropriate.

int nand_scan(struct mtd_info * mtd, int maxchips)

[NAND Interface] Scan for the NAND device

Parameters

struct mtd_info * mtd
MTD device structure
int maxchips
number of chips to scan for

Description

This fills out all the uninitialized function pointers with the defaults. The flash ID is read and the mtd/chip structures are filled with the appropriate values.

void nand_cleanup(struct nand_chip * chip)

[NAND Interface] Free resources held by the NAND device

Parameters

struct nand_chip * chip
NAND chip object
void nand_release(struct mtd_info * mtd)

[NAND Interface] Unregister the MTD device and free resources held by the NAND device

Parameters

struct mtd_info * mtd
MTD device structure
void __nand_calculate_ecc(const unsigned char * buf, unsigned int eccsize, unsigned char * code)

[NAND Interface] Calculate 3-byte ECC for 256/512-byte block

Parameters

const unsigned char * buf
input buffer with raw data
unsigned int eccsize
data bytes per ECC step (256 or 512)
unsigned char * code
output buffer with ECC
int nand_calculate_ecc(struct mtd_info * mtd, const unsigned char * buf, unsigned char * code)

[NAND Interface] Calculate 3-byte ECC for 256/512-byte block

Parameters

struct mtd_info * mtd
MTD block structure
const unsigned char * buf
input buffer with raw data
unsigned char * code
output buffer with ECC
int __nand_correct_data(unsigned char * buf, unsigned char * read_ecc, unsigned char * calc_ecc, unsigned int eccsize)

[NAND Interface] Detect and correct bit error(s)

Parameters

unsigned char * buf
raw data read from the chip
unsigned char * read_ecc
ECC from the chip
unsigned char * calc_ecc
the ECC calculated from raw data
unsigned int eccsize
data bytes per ECC step (256 or 512)

Description

Detect and correct a 1 bit error for eccsize byte block

int nand_correct_data(struct mtd_info * mtd, unsigned char * buf, unsigned char * read_ecc, unsigned char * calc_ecc)

[NAND Interface] Detect and correct bit error(s)

Parameters

struct mtd_info * mtd
MTD block structure
unsigned char * buf
raw data read from the chip
unsigned char * read_ecc
ECC from the chip
unsigned char * calc_ecc
the ECC calculated from raw data

Description

Detect and correct a 1 bit error for 256/512 byte block

Internal Functions Provided

This chapter contains the autogenerated documentation of the NAND driver internal functions. Each function has a short description which is marked with an [XXX] identifier. See the chapter “Documentation hints” for an explanation. The functions marked with [DEFAULT] might be relevant for a board driver developer.

void nand_release_device(struct mtd_info * mtd)

[GENERIC] release chip

Parameters

struct mtd_info * mtd
MTD device structure

Description

Release chip lock and wake up anyone waiting on the device.

uint8_t nand_read_byte(struct mtd_info * mtd)

[DEFAULT] read one byte from the chip

Parameters

struct mtd_info * mtd
MTD device structure

Description

Default read function for 8bit buswidth

uint8_t nand_read_byte16(struct mtd_info * mtd)

[DEFAULT] read one byte endianness aware from the chip

Parameters

struct mtd_info * mtd
MTD device structure

Description

Default read function for 16bit buswidth with endianness conversion.

u16 nand_read_word(struct mtd_info * mtd)

[DEFAULT] read one word from the chip

Parameters

struct mtd_info * mtd
MTD device structure

Description

Default read function for 16bit buswidth without endianness conversion.

void nand_select_chip(struct mtd_info * mtd, int chipnr)

[DEFAULT] control CE line

Parameters

struct mtd_info * mtd
MTD device structure
int chipnr
chipnumber to select, -1 for deselect

Description

Default select function for 1 chip devices.

void nand_write_byte(struct mtd_info * mtd, uint8_t byte)

[DEFAULT] write single byte to chip

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t byte
value to write

Description

Default function to write a byte to I/O[7:0]

void nand_write_byte16(struct mtd_info * mtd, uint8_t byte)

[DEFAULT] write single byte to a chip with width 16

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t byte
value to write

Description

Default function to write a byte to I/O[7:0] on a 16-bit wide chip.

void nand_write_buf(struct mtd_info * mtd, const uint8_t * buf, int len)

[DEFAULT] write buffer to chip

Parameters

struct mtd_info * mtd
MTD device structure
const uint8_t * buf
data buffer
int len
number of bytes to write

Description

Default write function for 8bit buswidth.

void nand_read_buf(struct mtd_info * mtd, uint8_t * buf, int len)

[DEFAULT] read chip data into buffer

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
buffer to store date
int len
number of bytes to read

Description

Default read function for 8bit buswidth.

void nand_write_buf16(struct mtd_info * mtd, const uint8_t * buf, int len)

[DEFAULT] write buffer to chip

Parameters

struct mtd_info * mtd
MTD device structure
const uint8_t * buf
data buffer
int len
number of bytes to write

Description

Default write function for 16bit buswidth.

void nand_read_buf16(struct mtd_info * mtd, uint8_t * buf, int len)

[DEFAULT] read chip data into buffer

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
buffer to store date
int len
number of bytes to read

Description

Default read function for 16bit buswidth.

int nand_block_bad(struct mtd_info * mtd, loff_t ofs)

[DEFAULT] Read bad block marker from the chip

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset from device start

Description

Check, if the block is bad.

int nand_default_block_markbad(struct mtd_info * mtd, loff_t ofs)

[DEFAULT] mark a block bad via bad block marker

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset from device start

Description

This is the default implementation, which can be overridden by a hardware specific driver. It provides the details for writing a bad block marker to a block.

int nand_block_markbad_lowlevel(struct mtd_info * mtd, loff_t ofs)

mark a block bad

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset from device start

Description

This function performs the generic NAND bad block marking steps (i.e., bad block table(s) and/or marker(s)). We only allow the hardware driver to specify how to write bad block markers to OOB (chip->block_markbad).

We try operations in the following order:

  1. erase the affected block, to allow OOB marker to be written cleanly
  2. write bad block marker to OOB area of affected block (unless flag NAND_BBT_NO_OOB_BBM is present)
  3. update the BBT

Note that we retain the first error encountered in (2) or (3), finish the procedures, and dump the error in the end.

int nand_check_wp(struct mtd_info * mtd)

[GENERIC] check if the chip is write protected

Parameters

struct mtd_info * mtd
MTD device structure

Description

Check, if the device is write protected. The function expects, that the device is already selected.

int nand_block_isreserved(struct mtd_info * mtd, loff_t ofs)

[GENERIC] Check if a block is marked reserved.

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset from device start

Description

Check if the block is marked as reserved.

int nand_block_checkbad(struct mtd_info * mtd, loff_t ofs, int allowbbt)

[GENERIC] Check if a block is marked bad

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset from device start
int allowbbt
1, if its allowed to access the bbt area

Description

Check, if the block is bad. Either by reading the bad block table or calling of the scan function.

void panic_nand_wait_ready(struct mtd_info * mtd, unsigned long timeo)

[GENERIC] Wait for the ready pin after commands.

Parameters

struct mtd_info * mtd
MTD device structure
unsigned long timeo
Timeout

Description

Helper function for nand_wait_ready used when needing to wait in interrupt context.

void nand_wait_status_ready(struct mtd_info * mtd, unsigned long timeo)

[GENERIC] Wait for the ready status after commands.

Parameters

struct mtd_info * mtd
MTD device structure
unsigned long timeo
Timeout in ms

Description

Wait for status ready (i.e. command done) or timeout.

void nand_command(struct mtd_info * mtd, unsigned int command, int column, int page_addr)

[DEFAULT] Send command to NAND device

Parameters

struct mtd_info * mtd
MTD device structure
unsigned int command
the command to be sent
int column
the column address for this command, -1 if none
int page_addr
the page address for this command, -1 if none

Description

Send command to NAND device. This function is used for small page devices (512 Bytes per page).

void nand_command_lp(struct mtd_info * mtd, unsigned int command, int column, int page_addr)

[DEFAULT] Send command to NAND large page device

Parameters

struct mtd_info * mtd
MTD device structure
unsigned int command
the command to be sent
int column
the column address for this command, -1 if none
int page_addr
the page address for this command, -1 if none

Description

Send command to NAND device. This is the version for the new large page devices. We don’t have the separate regions as we have in the small page devices. We must emulate NAND_CMD_READOOB to keep the code compatible.

void panic_nand_get_device(struct nand_chip * chip, struct mtd_info * mtd, int new_state)

[GENERIC] Get chip for selected access

Parameters

struct nand_chip * chip
the nand chip descriptor
struct mtd_info * mtd
MTD device structure
int new_state
the state which is requested

Description

Used when in panic, no locks are taken.

int nand_get_device(struct mtd_info * mtd, int new_state)

[GENERIC] Get chip for selected access

Parameters

struct mtd_info * mtd
MTD device structure
int new_state
the state which is requested

Description

Get the device and lock it for exclusive access

void panic_nand_wait(struct mtd_info * mtd, struct nand_chip * chip, unsigned long timeo)

[GENERIC] wait until the command is done

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
NAND chip structure
unsigned long timeo
timeout

Description

Wait for command done. This is a helper function for nand_wait used when we are in interrupt context. May happen when in panic and trying to write an oops through mtdoops.

int nand_wait(struct mtd_info * mtd, struct nand_chip * chip)

[DEFAULT] wait until the command is done

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
NAND chip structure

Description

Wait for command done. This applies to erase and program only.

int nand_reset_data_interface(struct nand_chip * chip, int chipnr)

Reset data interface and timings

Parameters

struct nand_chip * chip
The NAND chip
int chipnr
Internal die id

Description

Reset the Data interface and timings to ONFI mode 0.

Returns 0 for success or negative error code otherwise.

int nand_setup_data_interface(struct nand_chip * chip, int chipnr)

Setup the best data interface and timings

Parameters

struct nand_chip * chip
The NAND chip
int chipnr
Internal die id

Description

Find and configure the best data interface and NAND timings supported by the chip and the driver. First tries to retrieve supported timing modes from ONFI information, and if the NAND chip does not support ONFI, relies on the ->onfi_timing_mode_default specified in the nand_ids table.

Returns 0 for success or negative error code otherwise.

int nand_init_data_interface(struct nand_chip * chip)

find the best data interface and timings

Parameters

struct nand_chip * chip
The NAND chip

Description

Find the best data interface and NAND timings supported by the chip and the driver. First tries to retrieve supported timing modes from ONFI information, and if the NAND chip does not support ONFI, relies on the ->onfi_timing_mode_default specified in the nand_ids table. After this function nand_chip->data_interface is initialized with the best timing mode available.

Returns 0 for success or negative error code otherwise.

int nand_reset(struct nand_chip * chip, int chipnr)

Reset and initialize a NAND device

Parameters

struct nand_chip * chip
The NAND chip
int chipnr
Internal die id

Description

Returns 0 for success or negative error code otherwise

int nand_check_erased_buf(void * buf, int len, int bitflips_threshold)

check if a buffer contains (almost) only 0xff data

Parameters

void * buf
buffer to test
int len
buffer length
int bitflips_threshold
maximum number of bitflips

Description

Check if a buffer contains only 0xff, which means the underlying region has been erased and is ready to be programmed. The bitflips_threshold specify the maximum number of bitflips before considering the region is not erased.

Note

The logic of this function has been extracted from the memweight implementation, except that nand_check_erased_buf function exit before testing the whole buffer if the number of bitflips exceed the bitflips_threshold value.

Returns a positive number of bitflips less than or equal to bitflips_threshold, or -ERROR_CODE for bitflips in excess of the threshold.

int nand_read_page_raw_syndrome(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[INTERN] read raw page data without ecc

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read

Description

We need a special oob layout and handling even when OOB isn’t used.

int nand_read_page_swecc(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[REPLACEABLE] software ECC based page read function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read
int nand_read_subpage(struct mtd_info * mtd, struct nand_chip * chip, uint32_t data_offs, uint32_t readlen, uint8_t * bufpoi, int page)

[REPLACEABLE] ECC based sub-page read function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint32_t data_offs
offset of requested data within the page
uint32_t readlen
data length
uint8_t * bufpoi
buffer to store read data
int page
page number to read
int nand_read_page_hwecc(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hardware ECC based page read function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read

Description

Not for syndrome calculating ECC controllers which need a special oob layout.

int nand_read_page_hwecc_oob_first(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hw ecc, read oob first

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read

Description

Hardware ECC for large page chips, require OOB to be read first. For this ECC mode, the write_page method is re-used from ECC_HW. These methods read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with multiple ECC steps, follows the “infix ECC” scheme and reads/writes ECC from the data area, by overwriting the NAND manufacturer bad block markings.

int nand_read_page_syndrome(struct mtd_info * mtd, struct nand_chip * chip, uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hardware ECC syndrome based page read

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint8_t * buf
buffer to store read data
int oob_required
caller requires OOB data read to chip->oob_poi
int page
page number to read

Description

The hw generator calculates the error syndrome automatically. Therefore we need a special oob layout and handling.

uint8_t * nand_transfer_oob(struct mtd_info * mtd, uint8_t * oob, struct mtd_oob_ops * ops, size_t len)

[INTERN] Transfer oob to client buffer

Parameters

struct mtd_info * mtd
mtd info structure
uint8_t * oob
oob destination address
struct mtd_oob_ops * ops
oob ops structure
size_t len
size of oob to transfer
int nand_setup_read_retry(struct mtd_info * mtd, int retry_mode)

[INTERN] Set the READ RETRY mode

Parameters

struct mtd_info * mtd
MTD device structure
int retry_mode
the retry mode to use

Description

Some vendors supply a special command to shift the Vt threshold, to be used when there are too many bitflips in a page (i.e., ECC error). After setting a new threshold, the host should retry reading the page.

int nand_do_read_ops(struct mtd_info * mtd, loff_t from, struct mtd_oob_ops * ops)

[INTERN] Read data with ECC

Parameters

struct mtd_info * mtd
MTD device structure
loff_t from
offset to read from
struct mtd_oob_ops * ops
oob ops structure

Description

Internal function. Called with chip held.

int nand_read(struct mtd_info * mtd, loff_t from, size_t len, size_t * retlen, uint8_t * buf)

[MTD Interface] MTD compatibility function for nand_do_read_ecc

Parameters

struct mtd_info * mtd
MTD device structure
loff_t from
offset to read from
size_t len
number of bytes to read
size_t * retlen
pointer to variable to store the number of read bytes
uint8_t * buf
the databuffer to put data

Description

Get hold of the chip and call nand_do_read.

int nand_do_read_oob(struct mtd_info * mtd, loff_t from, struct mtd_oob_ops * ops)

[INTERN] NAND read out-of-band

Parameters

struct mtd_info * mtd
MTD device structure
loff_t from
offset to read from
struct mtd_oob_ops * ops
oob operations description structure

Description

NAND read out-of-band data from the spare area.

int nand_read_oob(struct mtd_info * mtd, loff_t from, struct mtd_oob_ops * ops)

[MTD Interface] NAND read data and/or out-of-band

Parameters

struct mtd_info * mtd
MTD device structure
loff_t from
offset to read from
struct mtd_oob_ops * ops
oob operation description structure

Description

NAND read data and/or out-of-band data.

int nand_write_page_raw_syndrome(struct mtd_info * mtd, struct nand_chip * chip, const uint8_t * buf, int oob_required, int page)

[INTERN] raw page write function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write

Description

We need a special oob layout and handling even when ECC isn’t checked.

int nand_write_page_swecc(struct mtd_info * mtd, struct nand_chip * chip, const uint8_t * buf, int oob_required, int page)

[REPLACEABLE] software ECC based page write function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write
int nand_write_page_hwecc(struct mtd_info * mtd, struct nand_chip * chip, const uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hardware ECC based page write function

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write
int nand_write_subpage_hwecc(struct mtd_info * mtd, struct nand_chip * chip, uint32_t offset, uint32_t data_len, const uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hardware ECC based subpage write

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
uint32_t offset
column address of subpage within the page
uint32_t data_len
data length
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write
int nand_write_page_syndrome(struct mtd_info * mtd, struct nand_chip * chip, const uint8_t * buf, int oob_required, int page)

[REPLACEABLE] hardware ECC syndrome based page write

Parameters

struct mtd_info * mtd
mtd info structure
struct nand_chip * chip
nand chip info structure
const uint8_t * buf
data buffer
int oob_required
must write chip->oob_poi to OOB
int page
page number to write

Description

The hw generator calculates the error syndrome automatically. Therefore we need a special oob layout and handling.

int nand_write_page(struct mtd_info * mtd, struct nand_chip * chip, uint32_t offset, int data_len, const uint8_t * buf, int oob_required, int page, int raw)

write one page

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
NAND chip descriptor
uint32_t offset
address offset within the page
int data_len
length of actual data to be written
const uint8_t * buf
the data to write
int oob_required
must write chip->oob_poi to OOB
int page
page number to write
int raw
use _raw version of write_page
uint8_t * nand_fill_oob(struct mtd_info * mtd, uint8_t * oob, size_t len, struct mtd_oob_ops * ops)

[INTERN] Transfer client buffer to oob

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * oob
oob data buffer
size_t len
oob data write length
struct mtd_oob_ops * ops
oob ops structure
int nand_do_write_ops(struct mtd_info * mtd, loff_t to, struct mtd_oob_ops * ops)

[INTERN] NAND write with ECC

Parameters

struct mtd_info * mtd
MTD device structure
loff_t to
offset to write to
struct mtd_oob_ops * ops
oob operations description structure

Description

NAND write with ECC.

int panic_nand_write(struct mtd_info * mtd, loff_t to, size_t len, size_t * retlen, const uint8_t * buf)

[MTD Interface] NAND write with ECC

Parameters

struct mtd_info * mtd
MTD device structure
loff_t to
offset to write to
size_t len
number of bytes to write
size_t * retlen
pointer to variable to store the number of written bytes
const uint8_t * buf
the data to write

Description

NAND write with ECC. Used when performing writes in interrupt context, this may for example be called by mtdoops when writing an oops while in panic.

int nand_write(struct mtd_info * mtd, loff_t to, size_t len, size_t * retlen, const uint8_t * buf)

[MTD Interface] NAND write with ECC

Parameters

struct mtd_info * mtd
MTD device structure
loff_t to
offset to write to
size_t len
number of bytes to write
size_t * retlen
pointer to variable to store the number of written bytes
const uint8_t * buf
the data to write

Description

NAND write with ECC.

int nand_do_write_oob(struct mtd_info * mtd, loff_t to, struct mtd_oob_ops * ops)

[MTD Interface] NAND write out-of-band

Parameters

struct mtd_info * mtd
MTD device structure
loff_t to
offset to write to
struct mtd_oob_ops * ops
oob operation description structure

Description

NAND write out-of-band.

int nand_write_oob(struct mtd_info * mtd, loff_t to, struct mtd_oob_ops * ops)

[MTD Interface] NAND write data and/or out-of-band

Parameters

struct mtd_info * mtd
MTD device structure
loff_t to
offset to write to
struct mtd_oob_ops * ops
oob operation description structure
int single_erase(struct mtd_info * mtd, int page)

[GENERIC] NAND standard block erase command function

Parameters

struct mtd_info * mtd
MTD device structure
int page
the page address of the block which will be erased

Description

Standard erase command for NAND chips. Returns NAND status.

int nand_erase(struct mtd_info * mtd, struct erase_info * instr)

[MTD Interface] erase block(s)

Parameters

struct mtd_info * mtd
MTD device structure
struct erase_info * instr
erase instruction

Description

Erase one ore more blocks.

int nand_erase_nand(struct mtd_info * mtd, struct erase_info * instr, int allowbbt)

[INTERN] erase block(s)

Parameters

struct mtd_info * mtd
MTD device structure
struct erase_info * instr
erase instruction
int allowbbt
allow erasing the bbt area

Description

Erase one ore more blocks.

void nand_sync(struct mtd_info * mtd)

[MTD Interface] sync

Parameters

struct mtd_info * mtd
MTD device structure

Description

Sync is actually a wait for chip ready function.

int nand_block_isbad(struct mtd_info * mtd, loff_t offs)

[MTD Interface] Check if block at offset is bad

Parameters

struct mtd_info * mtd
MTD device structure
loff_t offs
offset relative to mtd start
int nand_block_markbad(struct mtd_info * mtd, loff_t ofs)

[MTD Interface] Mark block at the given offset as bad

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset relative to mtd start
int nand_max_bad_blocks(struct mtd_info * mtd, loff_t ofs, size_t len)

[MTD Interface] Max number of bad blocks for an mtd

Parameters

struct mtd_info * mtd
MTD device structure
loff_t ofs
offset relative to mtd start
size_t len
length of mtd
int nand_onfi_set_features(struct mtd_info * mtd, struct nand_chip * chip, int addr, uint8_t * subfeature_param)

[REPLACEABLE] set features for ONFI nand

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
nand chip info structure
int addr
feature address.
uint8_t * subfeature_param
the subfeature parameters, a four bytes array.
int nand_onfi_get_features(struct mtd_info * mtd, struct nand_chip * chip, int addr, uint8_t * subfeature_param)

[REPLACEABLE] get features for ONFI nand

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_chip * chip
nand chip info structure
int addr
feature address.
uint8_t * subfeature_param
the subfeature parameters, a four bytes array.
int nand_suspend(struct mtd_info * mtd)

[MTD Interface] Suspend the NAND flash

Parameters

struct mtd_info * mtd
MTD device structure
void nand_resume(struct mtd_info * mtd)

[MTD Interface] Resume the NAND flash

Parameters

struct mtd_info * mtd
MTD device structure
void nand_shutdown(struct mtd_info * mtd)

[MTD Interface] Finish the current NAND operation and prevent further operations

Parameters

struct mtd_info * mtd
MTD device structure
int check_pattern(uint8_t * buf, int len, int paglen, struct nand_bbt_descr * td)

[GENERIC] check if a pattern is in the buffer

Parameters

uint8_t * buf
the buffer to search
int len
the length of buffer to search
int paglen
the pagelength
struct nand_bbt_descr * td
search pattern descriptor

Description

Check for a pattern at the given place. Used to search bad block tables and good / bad block identifiers.

int check_short_pattern(uint8_t * buf, struct nand_bbt_descr * td)

[GENERIC] check if a pattern is in the buffer

Parameters

uint8_t * buf
the buffer to search
struct nand_bbt_descr * td
search pattern descriptor

Description

Check for a pattern at the given place. Used to search bad block tables and good / bad block identifiers. Same as check_pattern, but no optional empty check.

u32 add_marker_len(struct nand_bbt_descr * td)

compute the length of the marker in data area

Parameters

struct nand_bbt_descr * td
BBT descriptor used for computation

Description

The length will be 0 if the marker is located in OOB area.

int read_bbt(struct mtd_info * mtd, uint8_t * buf, int page, int num, struct nand_bbt_descr * td, int offs)

[GENERIC] Read the bad block table starting from page

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
int page
the starting page
int num
the number of bbt descriptors to read
struct nand_bbt_descr * td
the bbt describtion table
int offs
block number offset in the table

Description

Read the bad block table starting from page.

int read_abs_bbt(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * td, int chip)

[GENERIC] Read the bad block table starting at a given page

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * td
descriptor for the bad block table
int chip
read the table for a specific chip, -1 read all chips; applies only if NAND_BBT_PERCHIP option is set

Description

Read the bad block table for all chips starting at a given page. We assume that the bbt bits are in consecutive order.

int scan_read_oob(struct mtd_info * mtd, uint8_t * buf, loff_t offs, size_t len)

[GENERIC] Scan data+OOB region to buffer

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
loff_t offs
offset at which to scan
size_t len
length of data region to read

Description

Scan read data from data+OOB. May traverse multiple pages, interleaving page,OOB,page,OOB,... in buf. Completes transfer and returns the “strongest” ECC condition (error or bitflip). May quit on the first (non-ECC) error.

void read_abs_bbts(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * td, struct nand_bbt_descr * md)

[GENERIC] Read the bad block table(s) for all chips starting at a given page

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * td
descriptor for the bad block table
struct nand_bbt_descr * md
descriptor for the bad block table mirror

Description

Read the bad block table(s) for all chips starting at a given page. We assume that the bbt bits are in consecutive order.

int create_bbt(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * bd, int chip)

[GENERIC] Create a bad block table by scanning the device

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * bd
descriptor for the good/bad block search pattern
int chip
create the table for a specific chip, -1 read all chips; applies only if NAND_BBT_PERCHIP option is set

Description

Create a bad block table by scanning the device for the given good/bad block identify pattern.

int search_bbt(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * td)

[GENERIC] scan the device for a specific bad block table

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * td
descriptor for the bad block table

Description

Read the bad block table by searching for a given ident pattern. Search is preformed either from the beginning up or from the end of the device downwards. The search starts always at the start of a block. If the option NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains the bad block information of this chip. This is necessary to provide support for certain DOC devices.

The bbt ident pattern resides in the oob area of the first page in a block.

void search_read_bbts(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * td, struct nand_bbt_descr * md)

[GENERIC] scan the device for bad block table(s)

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * td
descriptor for the bad block table
struct nand_bbt_descr * md
descriptor for the bad block table mirror

Description

Search and read the bad block table(s).

int get_bbt_block(struct nand_chip * this, struct nand_bbt_descr * td, struct nand_bbt_descr * md, int chip)

Get the first valid eraseblock suitable to store a BBT

Parameters

struct nand_chip * this
the NAND device
struct nand_bbt_descr * td
the BBT description
struct nand_bbt_descr * md
the mirror BBT descriptor
int chip
the CHIP selector

Description

This functions returns a positive block number pointing a valid eraseblock suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if all blocks are already used of marked bad. If td->pages[chip] was already pointing to a valid block we re-use it, otherwise we search for the next valid one.

void mark_bbt_block_bad(struct nand_chip * this, struct nand_bbt_descr * td, int chip, int block)

Mark one of the block reserved for BBT bad

Parameters

struct nand_chip * this
the NAND device
struct nand_bbt_descr * td
the BBT description
int chip
the CHIP selector
int block
the BBT block to mark

Description

Blocks reserved for BBT can become bad. This functions is an helper to mark such blocks as bad. It takes care of updating the in-memory BBT, marking the block as bad using a bad block marker and invalidating the associated td->pages[] entry.

int write_bbt(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * td, struct nand_bbt_descr * md, int chipsel)

[GENERIC] (Re)write the bad block table

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * td
descriptor for the bad block table
struct nand_bbt_descr * md
descriptor for the bad block table mirror
int chipsel
selector for a specific chip, -1 for all

Description

(Re)write the bad block table.

int nand_memory_bbt(struct mtd_info * mtd, struct nand_bbt_descr * bd)

[GENERIC] create a memory based bad block table

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_bbt_descr * bd
descriptor for the good/bad block search pattern

Description

The function creates a memory based bbt by scanning the device for manufacturer / software marked good / bad blocks.

int check_create(struct mtd_info * mtd, uint8_t * buf, struct nand_bbt_descr * bd)

[GENERIC] create and write bbt(s) if necessary

Parameters

struct mtd_info * mtd
MTD device structure
uint8_t * buf
temporary buffer
struct nand_bbt_descr * bd
descriptor for the good/bad block search pattern

Description

The function checks the results of the previous call to read_bbt and creates / updates the bbt(s) if necessary. Creation is necessary if no bbt was found for the chip/device. Update is necessary if one of the tables is missing or the version nr. of one table is less than the other.

void mark_bbt_region(struct mtd_info * mtd, struct nand_bbt_descr * td)

[GENERIC] mark the bad block table regions

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_bbt_descr * td
bad block table descriptor

Description

The bad block table regions are marked as “bad” to prevent accidental erasures / writes. The regions are identified by the mark 0x02.

void verify_bbt_descr(struct mtd_info * mtd, struct nand_bbt_descr * bd)

verify the bad block description

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_bbt_descr * bd
the table to verify

Description

This functions performs a few sanity checks on the bad block description table.

int nand_scan_bbt(struct mtd_info * mtd, struct nand_bbt_descr * bd)

[NAND Interface] scan, find, read and maybe create bad block table(s)

Parameters

struct mtd_info * mtd
MTD device structure
struct nand_bbt_descr * bd
descriptor for the good/bad block search pattern

Description

The function checks, if a bad block table(s) is/are already available. If not it scans the device for manufacturer marked good / bad blocks and writes the bad block table(s) to the selected place.

The bad block table memory is allocated here. It must be freed by calling the nand_free_bbt function.

int nand_update_bbt(struct mtd_info * mtd, loff_t offs)

update bad block table(s)

Parameters

struct mtd_info * mtd
MTD device structure
loff_t offs
the offset of the newly marked block

Description

The function updates the bad block table(s).

int nand_create_badblock_pattern(struct nand_chip * this)

[INTERN] Creates a BBT descriptor structure

Parameters

struct nand_chip * this
NAND chip to create descriptor for

Description

This function allocates and initializes a nand_bbt_descr for BBM detection based on the properties of this. The new descriptor is stored in this->badblock_pattern. Thus, this->badblock_pattern should be NULL when passed to this function.

int nand_default_bbt(struct mtd_info * mtd)

[NAND Interface] Select a default bad block table for the device

Parameters

struct mtd_info * mtd
MTD device structure

Description

This function selects the default bad block table support for the device and calls the nand_scan_bbt function.

int nand_isreserved_bbt(struct mtd_info * mtd, loff_t offs)

[NAND Interface] Check if a block is reserved

Parameters

struct mtd_info * mtd
MTD device structure
loff_t offs
offset in the device
int nand_isbad_bbt(struct mtd_info * mtd, loff_t offs, int allowbbt)

[NAND Interface] Check if a block is bad

Parameters

struct mtd_info * mtd
MTD device structure
loff_t offs
offset in the device
int allowbbt
allow access to bad block table region
int nand_markbad_bbt(struct mtd_info * mtd, loff_t offs)

[NAND Interface] Mark a block bad in the BBT

Parameters

struct mtd_info * mtd
MTD device structure
loff_t offs
offset of the bad block

Credits

The following people have contributed to the NAND driver:

  1. Steven J. Hillsjhill@realitydiluted.com
  2. David Woodhousedwmw2@infradead.org
  3. Thomas Gleixnertglx@linutronix.de

A lot of users have provided bugfixes, improvements and helping hands for testing. Thanks a lot.

The following people have contributed to this document:

  1. Thomas Gleixnertglx@linutronix.de