xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/mtdnand.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=====================================
2*4882a593SmuzhiyunMTD NAND Driver Programming Interface
3*4882a593Smuzhiyun=====================================
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun:Author: Thomas Gleixner
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunIntroduction
8*4882a593Smuzhiyun============
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunThe generic NAND driver supports almost all NAND and AG-AND based chips
11*4882a593Smuzhiyunand connects them to the Memory Technology Devices (MTD) subsystem of
12*4882a593Smuzhiyunthe Linux Kernel.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThis documentation is provided for developers who want to implement
15*4882a593Smuzhiyunboard drivers or filesystem drivers suitable for NAND devices.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunKnown Bugs And Assumptions
18*4882a593Smuzhiyun==========================
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunNone.
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunDocumentation hints
23*4882a593Smuzhiyun===================
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunThe function and structure docs are autogenerated. Each function and
26*4882a593Smuzhiyunstruct member has a short description which is marked with an [XXX]
27*4882a593Smuzhiyunidentifier. The following chapters explain the meaning of those
28*4882a593Smuzhiyunidentifiers.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunFunction identifiers [XXX]
31*4882a593Smuzhiyun--------------------------
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunThe functions are marked with [XXX] identifiers in the short comment.
34*4882a593SmuzhiyunThe identifiers explain the usage and scope of the functions. Following
35*4882a593Smuzhiyunidentifiers are used:
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun-  [MTD Interface]
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun   These functions provide the interface to the MTD kernel API. They are
40*4882a593Smuzhiyun   not replaceable and provide functionality which is complete hardware
41*4882a593Smuzhiyun   independent.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun-  [NAND Interface]
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun   These functions are exported and provide the interface to the NAND
46*4882a593Smuzhiyun   kernel API.
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun-  [GENERIC]
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun   Generic functions are not replaceable and provide functionality which
51*4882a593Smuzhiyun   is complete hardware independent.
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun-  [DEFAULT]
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun   Default functions provide hardware related functionality which is
56*4882a593Smuzhiyun   suitable for most of the implementations. These functions can be
57*4882a593Smuzhiyun   replaced by the board driver if necessary. Those functions are called
58*4882a593Smuzhiyun   via pointers in the NAND chip description structure. The board driver
59*4882a593Smuzhiyun   can set the functions which should be replaced by board dependent
60*4882a593Smuzhiyun   functions before calling nand_scan(). If the function pointer is
61*4882a593Smuzhiyun   NULL on entry to nand_scan() then the pointer is set to the default
62*4882a593Smuzhiyun   function which is suitable for the detected chip type.
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunStruct member identifiers [XXX]
65*4882a593Smuzhiyun-------------------------------
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunThe struct members are marked with [XXX] identifiers in the comment. The
68*4882a593Smuzhiyunidentifiers explain the usage and scope of the members. Following
69*4882a593Smuzhiyunidentifiers are used:
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun-  [INTERN]
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun   These members are for NAND driver internal use only and must not be
74*4882a593Smuzhiyun   modified. Most of these values are calculated from the chip geometry
75*4882a593Smuzhiyun   information which is evaluated during nand_scan().
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun-  [REPLACEABLE]
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun   Replaceable members hold hardware related functions which can be
80*4882a593Smuzhiyun   provided by the board driver. The board driver can set the functions
81*4882a593Smuzhiyun   which should be replaced by board dependent functions before calling
82*4882a593Smuzhiyun   nand_scan(). If the function pointer is NULL on entry to
83*4882a593Smuzhiyun   nand_scan() then the pointer is set to the default function which is
84*4882a593Smuzhiyun   suitable for the detected chip type.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun-  [BOARDSPECIFIC]
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun   Board specific members hold hardware related information which must
89*4882a593Smuzhiyun   be provided by the board driver. The board driver must set the
90*4882a593Smuzhiyun   function pointers and datafields before calling nand_scan().
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun-  [OPTIONAL]
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun   Optional members can hold information relevant for the board driver.
95*4882a593Smuzhiyun   The generic NAND driver code does not use this information.
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunBasic board driver
98*4882a593Smuzhiyun==================
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunFor most boards it will be sufficient to provide just the basic
101*4882a593Smuzhiyunfunctions and fill out some really board dependent members in the nand
102*4882a593Smuzhiyunchip description structure.
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunBasic defines
105*4882a593Smuzhiyun-------------
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunAt least you have to provide a nand_chip structure and a storage for
108*4882a593Smuzhiyunthe ioremap'ed chip address. You can allocate the nand_chip structure
109*4882a593Smuzhiyunusing kmalloc or you can allocate it statically. The NAND chip structure
110*4882a593Smuzhiyunembeds an mtd structure which will be registered to the MTD subsystem.
111*4882a593SmuzhiyunYou can extract a pointer to the mtd structure from a nand_chip pointer
112*4882a593Smuzhiyunusing the nand_to_mtd() helper.
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunKmalloc based example
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun::
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun    static struct mtd_info *board_mtd;
119*4882a593Smuzhiyun    static void __iomem *baseaddr;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunStatic example
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun::
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun    static struct nand_chip board_chip;
127*4882a593Smuzhiyun    static void __iomem *baseaddr;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunPartition defines
131*4882a593Smuzhiyun-----------------
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunIf you want to divide your device into partitions, then define a
134*4882a593Smuzhiyunpartitioning scheme suitable to your board.
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun::
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun    #define NUM_PARTITIONS 2
139*4882a593Smuzhiyun    static struct mtd_partition partition_info[] = {
140*4882a593Smuzhiyun        { .name = "Flash partition 1",
141*4882a593Smuzhiyun          .offset =  0,
142*4882a593Smuzhiyun          .size =    8 * 1024 * 1024 },
143*4882a593Smuzhiyun        { .name = "Flash partition 2",
144*4882a593Smuzhiyun          .offset =  MTDPART_OFS_NEXT,
145*4882a593Smuzhiyun          .size =    MTDPART_SIZ_FULL },
146*4882a593Smuzhiyun    };
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunHardware control function
150*4882a593Smuzhiyun-------------------------
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunThe hardware control function provides access to the control pins of the
153*4882a593SmuzhiyunNAND chip(s). The access can be done by GPIO pins or by address lines.
154*4882a593SmuzhiyunIf you use address lines, make sure that the timing requirements are
155*4882a593Smuzhiyunmet.
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun*GPIO based example*
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun::
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun    static void board_hwcontrol(struct mtd_info *mtd, int cmd)
162*4882a593Smuzhiyun    {
163*4882a593Smuzhiyun        switch(cmd){
164*4882a593Smuzhiyun            case NAND_CTL_SETCLE: /* Set CLE pin high */ break;
165*4882a593Smuzhiyun            case NAND_CTL_CLRCLE: /* Set CLE pin low */ break;
166*4882a593Smuzhiyun            case NAND_CTL_SETALE: /* Set ALE pin high */ break;
167*4882a593Smuzhiyun            case NAND_CTL_CLRALE: /* Set ALE pin low */ break;
168*4882a593Smuzhiyun            case NAND_CTL_SETNCE: /* Set nCE pin low */ break;
169*4882a593Smuzhiyun            case NAND_CTL_CLRNCE: /* Set nCE pin high */ break;
170*4882a593Smuzhiyun        }
171*4882a593Smuzhiyun    }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun*Address lines based example.* It's assumed that the nCE pin is driven
175*4882a593Smuzhiyunby a chip select decoder.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun::
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun    static void board_hwcontrol(struct mtd_info *mtd, int cmd)
180*4882a593Smuzhiyun    {
181*4882a593Smuzhiyun        struct nand_chip *this = mtd_to_nand(mtd);
182*4882a593Smuzhiyun        switch(cmd){
183*4882a593Smuzhiyun            case NAND_CTL_SETCLE: this->legacy.IO_ADDR_W |= CLE_ADRR_BIT;  break;
184*4882a593Smuzhiyun            case NAND_CTL_CLRCLE: this->legacy.IO_ADDR_W &= ~CLE_ADRR_BIT; break;
185*4882a593Smuzhiyun            case NAND_CTL_SETALE: this->legacy.IO_ADDR_W |= ALE_ADRR_BIT;  break;
186*4882a593Smuzhiyun            case NAND_CTL_CLRALE: this->legacy.IO_ADDR_W &= ~ALE_ADRR_BIT; break;
187*4882a593Smuzhiyun        }
188*4882a593Smuzhiyun    }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunDevice ready function
192*4882a593Smuzhiyun---------------------
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunIf the hardware interface has the ready busy pin of the NAND chip
195*4882a593Smuzhiyunconnected to a GPIO or other accessible I/O pin, this function is used
196*4882a593Smuzhiyunto read back the state of the pin. The function has no arguments and
197*4882a593Smuzhiyunshould return 0, if the device is busy (R/B pin is low) and 1, if the
198*4882a593Smuzhiyundevice is ready (R/B pin is high). If the hardware interface does not
199*4882a593Smuzhiyungive access to the ready busy pin, then the function must not be defined
200*4882a593Smuzhiyunand the function pointer this->legacy.dev_ready is set to NULL.
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunInit function
203*4882a593Smuzhiyun-------------
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunThe init function allocates memory and sets up all the board specific
206*4882a593Smuzhiyunparameters and function pointers. When everything is set up nand_scan()
207*4882a593Smuzhiyunis called. This function tries to detect and identify then chip. If a
208*4882a593Smuzhiyunchip is found all the internal data fields are initialized accordingly.
209*4882a593SmuzhiyunThe structure(s) have to be zeroed out first and then filled with the
210*4882a593Smuzhiyunnecessary information about the device.
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun::
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun    static int __init board_init (void)
215*4882a593Smuzhiyun    {
216*4882a593Smuzhiyun        struct nand_chip *this;
217*4882a593Smuzhiyun        int err = 0;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun        /* Allocate memory for MTD device structure and private data */
220*4882a593Smuzhiyun        this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
221*4882a593Smuzhiyun        if (!this) {
222*4882a593Smuzhiyun            printk ("Unable to allocate NAND MTD device structure.\n");
223*4882a593Smuzhiyun            err = -ENOMEM;
224*4882a593Smuzhiyun            goto out;
225*4882a593Smuzhiyun        }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun        board_mtd = nand_to_mtd(this);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun        /* map physical address */
230*4882a593Smuzhiyun        baseaddr = ioremap(CHIP_PHYSICAL_ADDRESS, 1024);
231*4882a593Smuzhiyun        if (!baseaddr) {
232*4882a593Smuzhiyun            printk("Ioremap to access NAND chip failed\n");
233*4882a593Smuzhiyun            err = -EIO;
234*4882a593Smuzhiyun            goto out_mtd;
235*4882a593Smuzhiyun        }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun        /* Set address of NAND IO lines */
238*4882a593Smuzhiyun        this->legacy.IO_ADDR_R = baseaddr;
239*4882a593Smuzhiyun        this->legacy.IO_ADDR_W = baseaddr;
240*4882a593Smuzhiyun        /* Reference hardware control function */
241*4882a593Smuzhiyun        this->hwcontrol = board_hwcontrol;
242*4882a593Smuzhiyun        /* Set command delay time, see datasheet for correct value */
243*4882a593Smuzhiyun        this->legacy.chip_delay = CHIP_DEPENDEND_COMMAND_DELAY;
244*4882a593Smuzhiyun        /* Assign the device ready function, if available */
245*4882a593Smuzhiyun        this->legacy.dev_ready = board_dev_ready;
246*4882a593Smuzhiyun        this->eccmode = NAND_ECC_SOFT;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun        /* Scan to find existence of the device */
249*4882a593Smuzhiyun        if (nand_scan (this, 1)) {
250*4882a593Smuzhiyun            err = -ENXIO;
251*4882a593Smuzhiyun            goto out_ior;
252*4882a593Smuzhiyun        }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun        add_mtd_partitions(board_mtd, partition_info, NUM_PARTITIONS);
255*4882a593Smuzhiyun        goto out;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun    out_ior:
258*4882a593Smuzhiyun        iounmap(baseaddr);
259*4882a593Smuzhiyun    out_mtd:
260*4882a593Smuzhiyun        kfree (this);
261*4882a593Smuzhiyun    out:
262*4882a593Smuzhiyun        return err;
263*4882a593Smuzhiyun    }
264*4882a593Smuzhiyun    module_init(board_init);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun
267*4882a593SmuzhiyunExit function
268*4882a593Smuzhiyun-------------
269*4882a593Smuzhiyun
270*4882a593SmuzhiyunThe exit function is only necessary if the driver is compiled as a
271*4882a593Smuzhiyunmodule. It releases all resources which are held by the chip driver and
272*4882a593Smuzhiyununregisters the partitions in the MTD layer.
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun::
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun    #ifdef MODULE
277*4882a593Smuzhiyun    static void __exit board_cleanup (void)
278*4882a593Smuzhiyun    {
279*4882a593Smuzhiyun        /* Unregister device */
280*4882a593Smuzhiyun        WARN_ON(mtd_device_unregister(board_mtd));
281*4882a593Smuzhiyun        /* Release resources */
282*4882a593Smuzhiyun        nand_cleanup(mtd_to_nand(board_mtd));
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun        /* unmap physical address */
285*4882a593Smuzhiyun        iounmap(baseaddr);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun        /* Free the MTD device structure */
288*4882a593Smuzhiyun        kfree (mtd_to_nand(board_mtd));
289*4882a593Smuzhiyun    }
290*4882a593Smuzhiyun    module_exit(board_cleanup);
291*4882a593Smuzhiyun    #endif
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun
294*4882a593SmuzhiyunAdvanced board driver functions
295*4882a593Smuzhiyun===============================
296*4882a593Smuzhiyun
297*4882a593SmuzhiyunThis chapter describes the advanced functionality of the NAND driver.
298*4882a593SmuzhiyunFor a list of functions which can be overridden by the board driver see
299*4882a593Smuzhiyunthe documentation of the nand_chip structure.
300*4882a593Smuzhiyun
301*4882a593SmuzhiyunMultiple chip control
302*4882a593Smuzhiyun---------------------
303*4882a593Smuzhiyun
304*4882a593SmuzhiyunThe nand driver can control chip arrays. Therefore the board driver must
305*4882a593Smuzhiyunprovide an own select_chip function. This function must (de)select the
306*4882a593Smuzhiyunrequested chip. The function pointer in the nand_chip structure must be
307*4882a593Smuzhiyunset before calling nand_scan(). The maxchip parameter of nand_scan()
308*4882a593Smuzhiyundefines the maximum number of chips to scan for. Make sure that the
309*4882a593Smuzhiyunselect_chip function can handle the requested number of chips.
310*4882a593Smuzhiyun
311*4882a593SmuzhiyunThe nand driver concatenates the chips to one virtual chip and provides
312*4882a593Smuzhiyunthis virtual chip to the MTD layer.
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun*Note: The driver can only handle linear chip arrays of equally sized
315*4882a593Smuzhiyunchips. There is no support for parallel arrays which extend the
316*4882a593Smuzhiyunbuswidth.*
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun*GPIO based example*
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun::
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun    static void board_select_chip (struct mtd_info *mtd, int chip)
323*4882a593Smuzhiyun    {
324*4882a593Smuzhiyun        /* Deselect all chips, set all nCE pins high */
325*4882a593Smuzhiyun        GPIO(BOARD_NAND_NCE) |= 0xff;
326*4882a593Smuzhiyun        if (chip >= 0)
327*4882a593Smuzhiyun            GPIO(BOARD_NAND_NCE) &= ~ (1 << chip);
328*4882a593Smuzhiyun    }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun*Address lines based example.* Its assumed that the nCE pins are
332*4882a593Smuzhiyunconnected to an address decoder.
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun::
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun    static void board_select_chip (struct mtd_info *mtd, int chip)
337*4882a593Smuzhiyun    {
338*4882a593Smuzhiyun        struct nand_chip *this = mtd_to_nand(mtd);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun        /* Deselect all chips */
341*4882a593Smuzhiyun        this->legacy.IO_ADDR_R &= ~BOARD_NAND_ADDR_MASK;
342*4882a593Smuzhiyun        this->legacy.IO_ADDR_W &= ~BOARD_NAND_ADDR_MASK;
343*4882a593Smuzhiyun        switch (chip) {
344*4882a593Smuzhiyun        case 0:
345*4882a593Smuzhiyun            this->legacy.IO_ADDR_R |= BOARD_NAND_ADDR_CHIP0;
346*4882a593Smuzhiyun            this->legacy.IO_ADDR_W |= BOARD_NAND_ADDR_CHIP0;
347*4882a593Smuzhiyun            break;
348*4882a593Smuzhiyun        ....
349*4882a593Smuzhiyun        case n:
350*4882a593Smuzhiyun            this->legacy.IO_ADDR_R |= BOARD_NAND_ADDR_CHIPn;
351*4882a593Smuzhiyun            this->legacy.IO_ADDR_W |= BOARD_NAND_ADDR_CHIPn;
352*4882a593Smuzhiyun            break;
353*4882a593Smuzhiyun        }
354*4882a593Smuzhiyun    }
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun
357*4882a593SmuzhiyunHardware ECC support
358*4882a593Smuzhiyun--------------------
359*4882a593Smuzhiyun
360*4882a593SmuzhiyunFunctions and constants
361*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~
362*4882a593Smuzhiyun
363*4882a593SmuzhiyunThe nand driver supports three different types of hardware ECC.
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun-  NAND_ECC_HW3_256
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun   Hardware ECC generator providing 3 bytes ECC per 256 byte.
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun-  NAND_ECC_HW3_512
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun   Hardware ECC generator providing 3 bytes ECC per 512 byte.
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun-  NAND_ECC_HW6_512
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun   Hardware ECC generator providing 6 bytes ECC per 512 byte.
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun-  NAND_ECC_HW8_512
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun   Hardware ECC generator providing 8 bytes ECC per 512 byte.
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunIf your hardware generator has a different functionality add it at the
382*4882a593Smuzhiyunappropriate place in nand_base.c
383*4882a593Smuzhiyun
384*4882a593SmuzhiyunThe board driver must provide following functions:
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun-  enable_hwecc
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun   This function is called before reading / writing to the chip. Reset
389*4882a593Smuzhiyun   or initialize the hardware generator in this function. The function
390*4882a593Smuzhiyun   is called with an argument which let you distinguish between read and
391*4882a593Smuzhiyun   write operations.
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun-  calculate_ecc
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun   This function is called after read / write from / to the chip.
396*4882a593Smuzhiyun   Transfer the ECC from the hardware to the buffer. If the option
397*4882a593Smuzhiyun   NAND_HWECC_SYNDROME is set then the function is only called on
398*4882a593Smuzhiyun   write. See below.
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun-  correct_data
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun   In case of an ECC error this function is called for error detection
403*4882a593Smuzhiyun   and correction. Return 1 respectively 2 in case the error can be
404*4882a593Smuzhiyun   corrected. If the error is not correctable return -1. If your
405*4882a593Smuzhiyun   hardware generator matches the default algorithm of the nand_ecc
406*4882a593Smuzhiyun   software generator then use the correction function provided by
407*4882a593Smuzhiyun   nand_ecc instead of implementing duplicated code.
408*4882a593Smuzhiyun
409*4882a593SmuzhiyunHardware ECC with syndrome calculation
410*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
411*4882a593Smuzhiyun
412*4882a593SmuzhiyunMany hardware ECC implementations provide Reed-Solomon codes and
413*4882a593Smuzhiyuncalculate an error syndrome on read. The syndrome must be converted to a
414*4882a593Smuzhiyunstandard Reed-Solomon syndrome before calling the error correction code
415*4882a593Smuzhiyunin the generic Reed-Solomon library.
416*4882a593Smuzhiyun
417*4882a593SmuzhiyunThe ECC bytes must be placed immediately after the data bytes in order
418*4882a593Smuzhiyunto make the syndrome generator work. This is contrary to the usual
419*4882a593Smuzhiyunlayout used by software ECC. The separation of data and out of band area
420*4882a593Smuzhiyunis not longer possible. The nand driver code handles this layout and the
421*4882a593Smuzhiyunremaining free bytes in the oob area are managed by the autoplacement
422*4882a593Smuzhiyuncode. Provide a matching oob-layout in this case. See rts_from4.c and
423*4882a593Smuzhiyundiskonchip.c for implementation reference. In those cases we must also
424*4882a593Smuzhiyunuse bad block tables on FLASH, because the ECC layout is interfering
425*4882a593Smuzhiyunwith the bad block marker positions. See bad block table support for
426*4882a593Smuzhiyundetails.
427*4882a593Smuzhiyun
428*4882a593SmuzhiyunBad block table support
429*4882a593Smuzhiyun-----------------------
430*4882a593Smuzhiyun
431*4882a593SmuzhiyunMost NAND chips mark the bad blocks at a defined position in the spare
432*4882a593Smuzhiyunarea. Those blocks must not be erased under any circumstances as the bad
433*4882a593Smuzhiyunblock information would be lost. It is possible to check the bad block
434*4882a593Smuzhiyunmark each time when the blocks are accessed by reading the spare area of
435*4882a593Smuzhiyunthe first page in the block. This is time consuming so a bad block table
436*4882a593Smuzhiyunis used.
437*4882a593Smuzhiyun
438*4882a593SmuzhiyunThe nand driver supports various types of bad block tables.
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun-  Per device
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun   The bad block table contains all bad block information of the device
443*4882a593Smuzhiyun   which can consist of multiple chips.
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun-  Per chip
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun   A bad block table is used per chip and contains the bad block
448*4882a593Smuzhiyun   information for this particular chip.
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun-  Fixed offset
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun   The bad block table is located at a fixed offset in the chip
453*4882a593Smuzhiyun   (device). This applies to various DiskOnChip devices.
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun-  Automatic placed
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun   The bad block table is automatically placed and detected either at
458*4882a593Smuzhiyun   the end or at the beginning of a chip (device)
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun-  Mirrored tables
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun   The bad block table is mirrored on the chip (device) to allow updates
463*4882a593Smuzhiyun   of the bad block table without data loss.
464*4882a593Smuzhiyun
465*4882a593Smuzhiyunnand_scan() calls the function nand_default_bbt().
466*4882a593Smuzhiyunnand_default_bbt() selects appropriate default bad block table
467*4882a593Smuzhiyundescriptors depending on the chip information which was retrieved by
468*4882a593Smuzhiyunnand_scan().
469*4882a593Smuzhiyun
470*4882a593SmuzhiyunThe standard policy is scanning the device for bad blocks and build a
471*4882a593Smuzhiyunram based bad block table which allows faster access than always
472*4882a593Smuzhiyunchecking the bad block information on the flash chip itself.
473*4882a593Smuzhiyun
474*4882a593SmuzhiyunFlash based tables
475*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~
476*4882a593Smuzhiyun
477*4882a593SmuzhiyunIt may be desired or necessary to keep a bad block table in FLASH. For
478*4882a593SmuzhiyunAG-AND chips this is mandatory, as they have no factory marked bad
479*4882a593Smuzhiyunblocks. They have factory marked good blocks. The marker pattern is
480*4882a593Smuzhiyunerased when the block is erased to be reused. So in case of powerloss
481*4882a593Smuzhiyunbefore writing the pattern back to the chip this block would be lost and
482*4882a593Smuzhiyunadded to the bad blocks. Therefore we scan the chip(s) when we detect
483*4882a593Smuzhiyunthem the first time for good blocks and store this information in a bad
484*4882a593Smuzhiyunblock table before erasing any of the blocks.
485*4882a593Smuzhiyun
486*4882a593SmuzhiyunThe blocks in which the tables are stored are protected against
487*4882a593Smuzhiyunaccidental access by marking them bad in the memory bad block table. The
488*4882a593Smuzhiyunbad block table management functions are allowed to circumvent this
489*4882a593Smuzhiyunprotection.
490*4882a593Smuzhiyun
491*4882a593SmuzhiyunThe simplest way to activate the FLASH based bad block table support is
492*4882a593Smuzhiyunto set the option NAND_BBT_USE_FLASH in the bbt_option field of the
493*4882a593Smuzhiyunnand chip structure before calling nand_scan(). For AG-AND chips is
494*4882a593Smuzhiyunthis done by default. This activates the default FLASH based bad block
495*4882a593Smuzhiyuntable functionality of the NAND driver. The default bad block table
496*4882a593Smuzhiyunoptions are
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun-  Store bad block table per chip
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun-  Use 2 bits per block
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun-  Automatic placement at the end of the chip
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun-  Use mirrored tables with version numbers
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun-  Reserve 4 blocks at the end of the chip
507*4882a593Smuzhiyun
508*4882a593SmuzhiyunUser defined tables
509*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~
510*4882a593Smuzhiyun
511*4882a593SmuzhiyunUser defined tables are created by filling out a nand_bbt_descr
512*4882a593Smuzhiyunstructure and storing the pointer in the nand_chip structure member
513*4882a593Smuzhiyunbbt_td before calling nand_scan(). If a mirror table is necessary a
514*4882a593Smuzhiyunsecond structure must be created and a pointer to this structure must be
515*4882a593Smuzhiyunstored in bbt_md inside the nand_chip structure. If the bbt_md member
516*4882a593Smuzhiyunis set to NULL then only the main table is used and no scan for the
517*4882a593Smuzhiyunmirrored table is performed.
518*4882a593Smuzhiyun
519*4882a593SmuzhiyunThe most important field in the nand_bbt_descr structure is the
520*4882a593Smuzhiyunoptions field. The options define most of the table properties. Use the
521*4882a593Smuzhiyunpredefined constants from rawnand.h to define the options.
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun-  Number of bits per block
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun   The supported number of bits is 1, 2, 4, 8.
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun-  Table per chip
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun   Setting the constant NAND_BBT_PERCHIP selects that a bad block
530*4882a593Smuzhiyun   table is managed for each chip in a chip array. If this option is not
531*4882a593Smuzhiyun   set then a per device bad block table is used.
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun-  Table location is absolute
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun   Use the option constant NAND_BBT_ABSPAGE and define the absolute
536*4882a593Smuzhiyun   page number where the bad block table starts in the field pages. If
537*4882a593Smuzhiyun   you have selected bad block tables per chip and you have a multi chip
538*4882a593Smuzhiyun   array then the start page must be given for each chip in the chip
539*4882a593Smuzhiyun   array. Note: there is no scan for a table ident pattern performed, so
540*4882a593Smuzhiyun   the fields pattern, veroffs, offs, len can be left uninitialized
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun-  Table location is automatically detected
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun   The table can either be located in the first or the last good blocks
545*4882a593Smuzhiyun   of the chip (device). Set NAND_BBT_LASTBLOCK to place the bad block
546*4882a593Smuzhiyun   table at the end of the chip (device). The bad block tables are
547*4882a593Smuzhiyun   marked and identified by a pattern which is stored in the spare area
548*4882a593Smuzhiyun   of the first page in the block which holds the bad block table. Store
549*4882a593Smuzhiyun   a pointer to the pattern in the pattern field. Further the length of
550*4882a593Smuzhiyun   the pattern has to be stored in len and the offset in the spare area
551*4882a593Smuzhiyun   must be given in the offs member of the nand_bbt_descr structure.
552*4882a593Smuzhiyun   For mirrored bad block tables different patterns are mandatory.
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun-  Table creation
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun   Set the option NAND_BBT_CREATE to enable the table creation if no
557*4882a593Smuzhiyun   table can be found during the scan. Usually this is done only once if
558*4882a593Smuzhiyun   a new chip is found.
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun-  Table write support
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun   Set the option NAND_BBT_WRITE to enable the table write support.
563*4882a593Smuzhiyun   This allows the update of the bad block table(s) in case a block has
564*4882a593Smuzhiyun   to be marked bad due to wear. The MTD interface function
565*4882a593Smuzhiyun   block_markbad is calling the update function of the bad block table.
566*4882a593Smuzhiyun   If the write support is enabled then the table is updated on FLASH.
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun   Note: Write support should only be enabled for mirrored tables with
569*4882a593Smuzhiyun   version control.
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun-  Table version control
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun   Set the option NAND_BBT_VERSION to enable the table version
574*4882a593Smuzhiyun   control. It's highly recommended to enable this for mirrored tables
575*4882a593Smuzhiyun   with write support. It makes sure that the risk of losing the bad
576*4882a593Smuzhiyun   block table information is reduced to the loss of the information
577*4882a593Smuzhiyun   about the one worn out block which should be marked bad. The version
578*4882a593Smuzhiyun   is stored in 4 consecutive bytes in the spare area of the device. The
579*4882a593Smuzhiyun   position of the version number is defined by the member veroffs in
580*4882a593Smuzhiyun   the bad block table descriptor.
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun-  Save block contents on write
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun   In case that the block which holds the bad block table does contain
585*4882a593Smuzhiyun   other useful information, set the option NAND_BBT_SAVECONTENT. When
586*4882a593Smuzhiyun   the bad block table is written then the whole block is read the bad
587*4882a593Smuzhiyun   block table is updated and the block is erased and everything is
588*4882a593Smuzhiyun   written back. If this option is not set only the bad block table is
589*4882a593Smuzhiyun   written and everything else in the block is ignored and erased.
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun-  Number of reserved blocks
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun   For automatic placement some blocks must be reserved for bad block
594*4882a593Smuzhiyun   table storage. The number of reserved blocks is defined in the
595*4882a593Smuzhiyun   maxblocks member of the bad block table description structure.
596*4882a593Smuzhiyun   Reserving 4 blocks for mirrored tables should be a reasonable number.
597*4882a593Smuzhiyun   This also limits the number of blocks which are scanned for the bad
598*4882a593Smuzhiyun   block table ident pattern.
599*4882a593Smuzhiyun
600*4882a593SmuzhiyunSpare area (auto)placement
601*4882a593Smuzhiyun--------------------------
602*4882a593Smuzhiyun
603*4882a593SmuzhiyunThe nand driver implements different possibilities for placement of
604*4882a593Smuzhiyunfilesystem data in the spare area,
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun-  Placement defined by fs driver
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun-  Automatic placement
609*4882a593Smuzhiyun
610*4882a593SmuzhiyunThe default placement function is automatic placement. The nand driver
611*4882a593Smuzhiyunhas built in default placement schemes for the various chiptypes. If due
612*4882a593Smuzhiyunto hardware ECC functionality the default placement does not fit then
613*4882a593Smuzhiyunthe board driver can provide a own placement scheme.
614*4882a593Smuzhiyun
615*4882a593SmuzhiyunFile system drivers can provide a own placement scheme which is used
616*4882a593Smuzhiyuninstead of the default placement scheme.
617*4882a593Smuzhiyun
618*4882a593SmuzhiyunPlacement schemes are defined by a nand_oobinfo structure
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun::
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun    struct nand_oobinfo {
623*4882a593Smuzhiyun        int useecc;
624*4882a593Smuzhiyun        int eccbytes;
625*4882a593Smuzhiyun        int eccpos[24];
626*4882a593Smuzhiyun        int oobfree[8][2];
627*4882a593Smuzhiyun    };
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun-  useecc
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun   The useecc member controls the ecc and placement function. The header
633*4882a593Smuzhiyun   file include/mtd/mtd-abi.h contains constants to select ecc and
634*4882a593Smuzhiyun   placement. MTD_NANDECC_OFF switches off the ecc complete. This is
635*4882a593Smuzhiyun   not recommended and available for testing and diagnosis only.
636*4882a593Smuzhiyun   MTD_NANDECC_PLACE selects caller defined placement,
637*4882a593Smuzhiyun   MTD_NANDECC_AUTOPLACE selects automatic placement.
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun-  eccbytes
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun   The eccbytes member defines the number of ecc bytes per page.
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun-  eccpos
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun   The eccpos array holds the byte offsets in the spare area where the
646*4882a593Smuzhiyun   ecc codes are placed.
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun-  oobfree
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun   The oobfree array defines the areas in the spare area which can be
651*4882a593Smuzhiyun   used for automatic placement. The information is given in the format
652*4882a593Smuzhiyun   {offset, size}. offset defines the start of the usable area, size the
653*4882a593Smuzhiyun   length in bytes. More than one area can be defined. The list is
654*4882a593Smuzhiyun   terminated by an {0, 0} entry.
655*4882a593Smuzhiyun
656*4882a593SmuzhiyunPlacement defined by fs driver
657*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
658*4882a593Smuzhiyun
659*4882a593SmuzhiyunThe calling function provides a pointer to a nand_oobinfo structure
660*4882a593Smuzhiyunwhich defines the ecc placement. For writes the caller must provide a
661*4882a593Smuzhiyunspare area buffer along with the data buffer. The spare area buffer size
662*4882a593Smuzhiyunis (number of pages) \* (size of spare area). For reads the buffer size
663*4882a593Smuzhiyunis (number of pages) \* ((size of spare area) + (number of ecc steps per
664*4882a593Smuzhiyunpage) \* sizeof (int)). The driver stores the result of the ecc check
665*4882a593Smuzhiyunfor each tuple in the spare buffer. The storage sequence is::
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun	<spare data page 0><ecc result 0>...<ecc result n>
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun	...
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun	<spare data page n><ecc result 0>...<ecc result n>
672*4882a593Smuzhiyun
673*4882a593SmuzhiyunThis is a legacy mode used by YAFFS1.
674*4882a593Smuzhiyun
675*4882a593SmuzhiyunIf the spare area buffer is NULL then only the ECC placement is done
676*4882a593Smuzhiyunaccording to the given scheme in the nand_oobinfo structure.
677*4882a593Smuzhiyun
678*4882a593SmuzhiyunAutomatic placement
679*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~
680*4882a593Smuzhiyun
681*4882a593SmuzhiyunAutomatic placement uses the built in defaults to place the ecc bytes in
682*4882a593Smuzhiyunthe spare area. If filesystem data have to be stored / read into the
683*4882a593Smuzhiyunspare area then the calling function must provide a buffer. The buffer
684*4882a593Smuzhiyunsize per page is determined by the oobfree array in the nand_oobinfo
685*4882a593Smuzhiyunstructure.
686*4882a593Smuzhiyun
687*4882a593SmuzhiyunIf the spare area buffer is NULL then only the ECC placement is done
688*4882a593Smuzhiyunaccording to the default builtin scheme.
689*4882a593Smuzhiyun
690*4882a593SmuzhiyunSpare area autoplacement default schemes
691*4882a593Smuzhiyun----------------------------------------
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun256 byte pagesize
694*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun======== ================== ===================================================
697*4882a593SmuzhiyunOffset   Content            Comment
698*4882a593Smuzhiyun======== ================== ===================================================
699*4882a593Smuzhiyun0x00     ECC byte 0         Error correction code byte 0
700*4882a593Smuzhiyun0x01     ECC byte 1         Error correction code byte 1
701*4882a593Smuzhiyun0x02     ECC byte 2         Error correction code byte 2
702*4882a593Smuzhiyun0x03     Autoplace 0
703*4882a593Smuzhiyun0x04     Autoplace 1
704*4882a593Smuzhiyun0x05     Bad block marker   If any bit in this byte is zero, then this
705*4882a593Smuzhiyun			    block is bad. This applies only to the first
706*4882a593Smuzhiyun			    page in a block. In the remaining pages this
707*4882a593Smuzhiyun			    byte is reserved
708*4882a593Smuzhiyun0x06     Autoplace 2
709*4882a593Smuzhiyun0x07     Autoplace 3
710*4882a593Smuzhiyun======== ================== ===================================================
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun512 byte pagesize
713*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun============= ================== ==============================================
717*4882a593SmuzhiyunOffset        Content            Comment
718*4882a593Smuzhiyun============= ================== ==============================================
719*4882a593Smuzhiyun0x00          ECC byte 0         Error correction code byte 0 of the lower
720*4882a593Smuzhiyun				 256 Byte data in this page
721*4882a593Smuzhiyun0x01          ECC byte 1         Error correction code byte 1 of the lower
722*4882a593Smuzhiyun				 256 Bytes of data in this page
723*4882a593Smuzhiyun0x02          ECC byte 2         Error correction code byte 2 of the lower
724*4882a593Smuzhiyun				 256 Bytes of data in this page
725*4882a593Smuzhiyun0x03          ECC byte 3         Error correction code byte 0 of the upper
726*4882a593Smuzhiyun				 256 Bytes of data in this page
727*4882a593Smuzhiyun0x04          reserved           reserved
728*4882a593Smuzhiyun0x05          Bad block marker   If any bit in this byte is zero, then this
729*4882a593Smuzhiyun				 block is bad. This applies only to the first
730*4882a593Smuzhiyun				 page in a block. In the remaining pages this
731*4882a593Smuzhiyun				 byte is reserved
732*4882a593Smuzhiyun0x06          ECC byte 4         Error correction code byte 1 of the upper
733*4882a593Smuzhiyun				 256 Bytes of data in this page
734*4882a593Smuzhiyun0x07          ECC byte 5         Error correction code byte 2 of the upper
735*4882a593Smuzhiyun				 256 Bytes of data in this page
736*4882a593Smuzhiyun0x08 - 0x0F   Autoplace 0 - 7
737*4882a593Smuzhiyun============= ================== ==============================================
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun2048 byte pagesize
740*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun=========== ================== ================================================
743*4882a593SmuzhiyunOffset      Content            Comment
744*4882a593Smuzhiyun=========== ================== ================================================
745*4882a593Smuzhiyun0x00        Bad block marker   If any bit in this byte is zero, then this block
746*4882a593Smuzhiyun			       is bad. This applies only to the first page in a
747*4882a593Smuzhiyun			       block. In the remaining pages this byte is
748*4882a593Smuzhiyun			       reserved
749*4882a593Smuzhiyun0x01        Reserved           Reserved
750*4882a593Smuzhiyun0x02-0x27   Autoplace 0 - 37
751*4882a593Smuzhiyun0x28        ECC byte 0         Error correction code byte 0 of the first
752*4882a593Smuzhiyun			       256 Byte data in this page
753*4882a593Smuzhiyun0x29        ECC byte 1         Error correction code byte 1 of the first
754*4882a593Smuzhiyun			       256 Bytes of data in this page
755*4882a593Smuzhiyun0x2A        ECC byte 2         Error correction code byte 2 of the first
756*4882a593Smuzhiyun			       256 Bytes data in this page
757*4882a593Smuzhiyun0x2B        ECC byte 3         Error correction code byte 0 of the second
758*4882a593Smuzhiyun			       256 Bytes of data in this page
759*4882a593Smuzhiyun0x2C        ECC byte 4         Error correction code byte 1 of the second
760*4882a593Smuzhiyun			       256 Bytes of data in this page
761*4882a593Smuzhiyun0x2D        ECC byte 5         Error correction code byte 2 of the second
762*4882a593Smuzhiyun			       256 Bytes of data in this page
763*4882a593Smuzhiyun0x2E        ECC byte 6         Error correction code byte 0 of the third
764*4882a593Smuzhiyun			       256 Bytes of data in this page
765*4882a593Smuzhiyun0x2F        ECC byte 7         Error correction code byte 1 of the third
766*4882a593Smuzhiyun			       256 Bytes of data in this page
767*4882a593Smuzhiyun0x30        ECC byte 8         Error correction code byte 2 of the third
768*4882a593Smuzhiyun			       256 Bytes of data in this page
769*4882a593Smuzhiyun0x31        ECC byte 9         Error correction code byte 0 of the fourth
770*4882a593Smuzhiyun			       256 Bytes of data in this page
771*4882a593Smuzhiyun0x32        ECC byte 10        Error correction code byte 1 of the fourth
772*4882a593Smuzhiyun			       256 Bytes of data in this page
773*4882a593Smuzhiyun0x33        ECC byte 11        Error correction code byte 2 of the fourth
774*4882a593Smuzhiyun			       256 Bytes of data in this page
775*4882a593Smuzhiyun0x34        ECC byte 12        Error correction code byte 0 of the fifth
776*4882a593Smuzhiyun			       256 Bytes of data in this page
777*4882a593Smuzhiyun0x35        ECC byte 13        Error correction code byte 1 of the fifth
778*4882a593Smuzhiyun			       256 Bytes of data in this page
779*4882a593Smuzhiyun0x36        ECC byte 14        Error correction code byte 2 of the fifth
780*4882a593Smuzhiyun			       256 Bytes of data in this page
781*4882a593Smuzhiyun0x37        ECC byte 15        Error correction code byte 0 of the sixth
782*4882a593Smuzhiyun			       256 Bytes of data in this page
783*4882a593Smuzhiyun0x38        ECC byte 16        Error correction code byte 1 of the sixth
784*4882a593Smuzhiyun			       256 Bytes of data in this page
785*4882a593Smuzhiyun0x39        ECC byte 17        Error correction code byte 2 of the sixth
786*4882a593Smuzhiyun			       256 Bytes of data in this page
787*4882a593Smuzhiyun0x3A        ECC byte 18        Error correction code byte 0 of the seventh
788*4882a593Smuzhiyun			       256 Bytes of data in this page
789*4882a593Smuzhiyun0x3B        ECC byte 19        Error correction code byte 1 of the seventh
790*4882a593Smuzhiyun			       256 Bytes of data in this page
791*4882a593Smuzhiyun0x3C        ECC byte 20        Error correction code byte 2 of the seventh
792*4882a593Smuzhiyun			       256 Bytes of data in this page
793*4882a593Smuzhiyun0x3D        ECC byte 21        Error correction code byte 0 of the eighth
794*4882a593Smuzhiyun			       256 Bytes of data in this page
795*4882a593Smuzhiyun0x3E        ECC byte 22        Error correction code byte 1 of the eighth
796*4882a593Smuzhiyun			       256 Bytes of data in this page
797*4882a593Smuzhiyun0x3F        ECC byte 23        Error correction code byte 2 of the eighth
798*4882a593Smuzhiyun			       256 Bytes of data in this page
799*4882a593Smuzhiyun=========== ================== ================================================
800*4882a593Smuzhiyun
801*4882a593SmuzhiyunFilesystem support
802*4882a593Smuzhiyun==================
803*4882a593Smuzhiyun
804*4882a593SmuzhiyunThe NAND driver provides all necessary functions for a filesystem via
805*4882a593Smuzhiyunthe MTD interface.
806*4882a593Smuzhiyun
807*4882a593SmuzhiyunFilesystems must be aware of the NAND peculiarities and restrictions.
808*4882a593SmuzhiyunOne major restrictions of NAND Flash is, that you cannot write as often
809*4882a593Smuzhiyunas you want to a page. The consecutive writes to a page, before erasing
810*4882a593Smuzhiyunit again, are restricted to 1-3 writes, depending on the manufacturers
811*4882a593Smuzhiyunspecifications. This applies similar to the spare area.
812*4882a593Smuzhiyun
813*4882a593SmuzhiyunTherefore NAND aware filesystems must either write in page size chunks
814*4882a593Smuzhiyunor hold a writebuffer to collect smaller writes until they sum up to
815*4882a593Smuzhiyunpagesize. Available NAND aware filesystems: JFFS2, YAFFS.
816*4882a593Smuzhiyun
817*4882a593SmuzhiyunThe spare area usage to store filesystem data is controlled by the spare
818*4882a593Smuzhiyunarea placement functionality which is described in one of the earlier
819*4882a593Smuzhiyunchapters.
820*4882a593Smuzhiyun
821*4882a593SmuzhiyunTools
822*4882a593Smuzhiyun=====
823*4882a593Smuzhiyun
824*4882a593SmuzhiyunThe MTD project provides a couple of helpful tools to handle NAND Flash.
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun-  flasherase, flasheraseall: Erase and format FLASH partitions
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun-  nandwrite: write filesystem images to NAND FLASH
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun-  nanddump: dump the contents of a NAND FLASH partitions
831*4882a593Smuzhiyun
832*4882a593SmuzhiyunThese tools are aware of the NAND restrictions. Please use those tools
833*4882a593Smuzhiyuninstead of complaining about errors which are caused by non NAND aware
834*4882a593Smuzhiyunaccess methods.
835*4882a593Smuzhiyun
836*4882a593SmuzhiyunConstants
837*4882a593Smuzhiyun=========
838*4882a593Smuzhiyun
839*4882a593SmuzhiyunThis chapter describes the constants which might be relevant for a
840*4882a593Smuzhiyundriver developer.
841*4882a593Smuzhiyun
842*4882a593SmuzhiyunChip option constants
843*4882a593Smuzhiyun---------------------
844*4882a593Smuzhiyun
845*4882a593SmuzhiyunConstants for chip id table
846*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~
847*4882a593Smuzhiyun
848*4882a593SmuzhiyunThese constants are defined in rawnand.h. They are OR-ed together to
849*4882a593Smuzhiyundescribe the chip functionality::
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun    /* Buswitdh is 16 bit */
852*4882a593Smuzhiyun    #define NAND_BUSWIDTH_16    0x00000002
853*4882a593Smuzhiyun    /* Device supports partial programming without padding */
854*4882a593Smuzhiyun    #define NAND_NO_PADDING     0x00000004
855*4882a593Smuzhiyun    /* Chip has cache program function */
856*4882a593Smuzhiyun    #define NAND_CACHEPRG       0x00000008
857*4882a593Smuzhiyun    /* Chip has copy back function */
858*4882a593Smuzhiyun    #define NAND_COPYBACK       0x00000010
859*4882a593Smuzhiyun    /* AND Chip which has 4 banks and a confusing page / block
860*4882a593Smuzhiyun     * assignment. See Renesas datasheet for further information */
861*4882a593Smuzhiyun    #define NAND_IS_AND     0x00000020
862*4882a593Smuzhiyun    /* Chip has a array of 4 pages which can be read without
863*4882a593Smuzhiyun     * additional ready /busy waits */
864*4882a593Smuzhiyun    #define NAND_4PAGE_ARRAY    0x00000040
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun
867*4882a593SmuzhiyunConstants for runtime options
868*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
869*4882a593Smuzhiyun
870*4882a593SmuzhiyunThese constants are defined in rawnand.h. They are OR-ed together to
871*4882a593Smuzhiyundescribe the functionality::
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun    /* The hw ecc generator provides a syndrome instead a ecc value on read
874*4882a593Smuzhiyun     * This can only work if we have the ecc bytes directly behind the
875*4882a593Smuzhiyun     * data bytes. Applies for DOC and AG-AND Renesas HW Reed Solomon generators */
876*4882a593Smuzhiyun    #define NAND_HWECC_SYNDROME 0x00020000
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun
879*4882a593SmuzhiyunECC selection constants
880*4882a593Smuzhiyun-----------------------
881*4882a593Smuzhiyun
882*4882a593SmuzhiyunUse these constants to select the ECC algorithm::
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun    /* No ECC. Usage is not recommended ! */
885*4882a593Smuzhiyun    #define NAND_ECC_NONE       0
886*4882a593Smuzhiyun    /* Software ECC 3 byte ECC per 256 Byte data */
887*4882a593Smuzhiyun    #define NAND_ECC_SOFT       1
888*4882a593Smuzhiyun    /* Hardware ECC 3 byte ECC per 256 Byte data */
889*4882a593Smuzhiyun    #define NAND_ECC_HW3_256    2
890*4882a593Smuzhiyun    /* Hardware ECC 3 byte ECC per 512 Byte data */
891*4882a593Smuzhiyun    #define NAND_ECC_HW3_512    3
892*4882a593Smuzhiyun    /* Hardware ECC 6 byte ECC per 512 Byte data */
893*4882a593Smuzhiyun    #define NAND_ECC_HW6_512    4
894*4882a593Smuzhiyun    /* Hardware ECC 8 byte ECC per 512 Byte data */
895*4882a593Smuzhiyun    #define NAND_ECC_HW8_512    6
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun
898*4882a593SmuzhiyunHardware control related constants
899*4882a593Smuzhiyun----------------------------------
900*4882a593Smuzhiyun
901*4882a593SmuzhiyunThese constants describe the requested hardware access function when the
902*4882a593Smuzhiyunboardspecific hardware control function is called::
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun    /* Select the chip by setting nCE to low */
905*4882a593Smuzhiyun    #define NAND_CTL_SETNCE     1
906*4882a593Smuzhiyun    /* Deselect the chip by setting nCE to high */
907*4882a593Smuzhiyun    #define NAND_CTL_CLRNCE     2
908*4882a593Smuzhiyun    /* Select the command latch by setting CLE to high */
909*4882a593Smuzhiyun    #define NAND_CTL_SETCLE     3
910*4882a593Smuzhiyun    /* Deselect the command latch by setting CLE to low */
911*4882a593Smuzhiyun    #define NAND_CTL_CLRCLE     4
912*4882a593Smuzhiyun    /* Select the address latch by setting ALE to high */
913*4882a593Smuzhiyun    #define NAND_CTL_SETALE     5
914*4882a593Smuzhiyun    /* Deselect the address latch by setting ALE to low */
915*4882a593Smuzhiyun    #define NAND_CTL_CLRALE     6
916*4882a593Smuzhiyun    /* Set write protection by setting WP to high. Not used! */
917*4882a593Smuzhiyun    #define NAND_CTL_SETWP      7
918*4882a593Smuzhiyun    /* Clear write protection by setting WP to low. Not used! */
919*4882a593Smuzhiyun    #define NAND_CTL_CLRWP      8
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun
922*4882a593SmuzhiyunBad block table related constants
923*4882a593Smuzhiyun---------------------------------
924*4882a593Smuzhiyun
925*4882a593SmuzhiyunThese constants describe the options used for bad block table
926*4882a593Smuzhiyundescriptors::
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun    /* Options for the bad block table descriptors */
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun    /* The number of bits used per block in the bbt on the device */
931*4882a593Smuzhiyun    #define NAND_BBT_NRBITS_MSK 0x0000000F
932*4882a593Smuzhiyun    #define NAND_BBT_1BIT       0x00000001
933*4882a593Smuzhiyun    #define NAND_BBT_2BIT       0x00000002
934*4882a593Smuzhiyun    #define NAND_BBT_4BIT       0x00000004
935*4882a593Smuzhiyun    #define NAND_BBT_8BIT       0x00000008
936*4882a593Smuzhiyun    /* The bad block table is in the last good block of the device */
937*4882a593Smuzhiyun    #define NAND_BBT_LASTBLOCK  0x00000010
938*4882a593Smuzhiyun    /* The bbt is at the given page, else we must scan for the bbt */
939*4882a593Smuzhiyun    #define NAND_BBT_ABSPAGE    0x00000020
940*4882a593Smuzhiyun    /* bbt is stored per chip on multichip devices */
941*4882a593Smuzhiyun    #define NAND_BBT_PERCHIP    0x00000080
942*4882a593Smuzhiyun    /* bbt has a version counter at offset veroffs */
943*4882a593Smuzhiyun    #define NAND_BBT_VERSION    0x00000100
944*4882a593Smuzhiyun    /* Create a bbt if none axists */
945*4882a593Smuzhiyun    #define NAND_BBT_CREATE     0x00000200
946*4882a593Smuzhiyun    /* Write bbt if necessary */
947*4882a593Smuzhiyun    #define NAND_BBT_WRITE      0x00001000
948*4882a593Smuzhiyun    /* Read and write back block contents when writing bbt */
949*4882a593Smuzhiyun    #define NAND_BBT_SAVECONTENT    0x00002000
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun
952*4882a593SmuzhiyunStructures
953*4882a593Smuzhiyun==========
954*4882a593Smuzhiyun
955*4882a593SmuzhiyunThis chapter contains the autogenerated documentation of the structures
956*4882a593Smuzhiyunwhich are used in the NAND driver and might be relevant for a driver
957*4882a593Smuzhiyundeveloper. Each struct member has a short description which is marked
958*4882a593Smuzhiyunwith an [XXX] identifier. See the chapter "Documentation hints" for an
959*4882a593Smuzhiyunexplanation.
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun.. kernel-doc:: include/linux/mtd/rawnand.h
962*4882a593Smuzhiyun   :internal:
963*4882a593Smuzhiyun
964*4882a593SmuzhiyunPublic Functions Provided
965*4882a593Smuzhiyun=========================
966*4882a593Smuzhiyun
967*4882a593SmuzhiyunThis chapter contains the autogenerated documentation of the NAND kernel
968*4882a593SmuzhiyunAPI functions which are exported. Each function has a short description
969*4882a593Smuzhiyunwhich is marked with an [XXX] identifier. See the chapter "Documentation
970*4882a593Smuzhiyunhints" for an explanation.
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun.. kernel-doc:: drivers/mtd/nand/raw/nand_base.c
973*4882a593Smuzhiyun   :export:
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun.. kernel-doc:: drivers/mtd/nand/raw/nand_ecc.c
976*4882a593Smuzhiyun   :export:
977*4882a593Smuzhiyun
978*4882a593SmuzhiyunInternal Functions Provided
979*4882a593Smuzhiyun===========================
980*4882a593Smuzhiyun
981*4882a593SmuzhiyunThis chapter contains the autogenerated documentation of the NAND driver
982*4882a593Smuzhiyuninternal functions. Each function has a short description which is
983*4882a593Smuzhiyunmarked with an [XXX] identifier. See the chapter "Documentation hints"
984*4882a593Smuzhiyunfor an explanation. The functions marked with [DEFAULT] might be
985*4882a593Smuzhiyunrelevant for a board driver developer.
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun.. kernel-doc:: drivers/mtd/nand/raw/nand_base.c
988*4882a593Smuzhiyun   :internal:
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun.. kernel-doc:: drivers/mtd/nand/raw/nand_bbt.c
991*4882a593Smuzhiyun   :internal:
992*4882a593Smuzhiyun
993*4882a593SmuzhiyunCredits
994*4882a593Smuzhiyun=======
995*4882a593Smuzhiyun
996*4882a593SmuzhiyunThe following people have contributed to the NAND driver:
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun1. Steven J. Hill\ sjhill@realitydiluted.com
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun2. David Woodhouse\ dwmw2@infradead.org
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun3. Thomas Gleixner\ tglx@linutronix.de
1003*4882a593Smuzhiyun
1004*4882a593SmuzhiyunA lot of users have provided bugfixes, improvements and helping hands
1005*4882a593Smuzhiyunfor testing. Thanks a lot.
1006*4882a593Smuzhiyun
1007*4882a593SmuzhiyunThe following people have contributed to this document:
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun1. Thomas Gleixner\ tglx@linutronix.de
1010