xref: /openbmc/u-boot/doc/README.nand (revision 592cd5defd4f71d34ffcbd8dd3326bc10f662e20)
183d290c5STom Rini# SPDX-License-Identifier: GPL-2.0+
27a8e9bedSwdenkNAND FLASH commands and notes
37a8e9bedSwdenk
44e3ccd26SWolfgang DenkSee NOTE below!!!
54e3ccd26SWolfgang Denk
67a8e9bedSwdenk# (C) Copyright 2003
77a8e9bedSwdenk# Dave Ellis, SIXNET, dge@sixnetio.com
87a8e9bedSwdenk#
97a8e9bedSwdenk
107a8e9bedSwdenkCommands:
117a8e9bedSwdenk
127a8e9bedSwdenk   nand bad
137a8e9bedSwdenk      Print a list of all of the bad blocks in the current device.
147a8e9bedSwdenk
157a8e9bedSwdenk   nand device
167a8e9bedSwdenk      Print information about the current NAND device.
177a8e9bedSwdenk
187a8e9bedSwdenk   nand device num
197a8e9bedSwdenk      Make device `num' the current device and print information about it.
207a8e9bedSwdenk
21856f0544SStefan Roese   nand erase off|partition size
22856f0544SStefan Roese   nand erase clean [off|partition size]
23856f0544SStefan Roese      Erase `size' bytes starting at offset `off'. Alternatively partition
24856f0544SStefan Roese      name can be specified, in this case size will be eventually limited
25856f0544SStefan Roese      to not exceed partition size (this behaviour applies also to read
26856f0544SStefan Roese      and write commands). Only complete erase blocks can be erased.
27856f0544SStefan Roese
28856f0544SStefan Roese      If `erase' is specified without an offset or size, the entire flash
29856f0544SStefan Roese      is erased. If `erase' is specified with partition but without an
30856f0544SStefan Roese      size, the entire partition is erased.
317a8e9bedSwdenk
327a8e9bedSwdenk      If `clean' is specified, a JFFS2-style clean marker is written to
33856f0544SStefan Roese      each block after it is erased.
347a8e9bedSwdenk
357a8e9bedSwdenk      This command will not erase blocks that are marked bad. There is
367a8e9bedSwdenk      a debug option in cmd_nand.c to allow bad blocks to be erased.
377a8e9bedSwdenk      Please read the warning there before using it, as blocks marked
387a8e9bedSwdenk      bad by the manufacturer must _NEVER_ be erased.
397a8e9bedSwdenk
407a8e9bedSwdenk   nand info
417a8e9bedSwdenk      Print information about all of the NAND devices found.
427a8e9bedSwdenk
43856f0544SStefan Roese   nand read addr ofs|partition size
44984e03cdSScott Wood      Read `size' bytes from `ofs' in NAND flash to `addr'.  Blocks that
45984e03cdSScott Wood      are marked bad are skipped.  If a page cannot be read because an
46984e03cdSScott Wood      uncorrectable data error is found, the command stops with an error.
477a8e9bedSwdenk
48856f0544SStefan Roese   nand read.oob addr ofs|partition size
497a8e9bedSwdenk      Read `size' bytes from the out-of-band data area corresponding to
507a8e9bedSwdenk      `ofs' in NAND flash to `addr'. This is limited to the 16 bytes of
517a8e9bedSwdenk      data for one 512-byte page or 2 256-byte pages. There is no check
527a8e9bedSwdenk      for bad blocks or ECC errors.
537a8e9bedSwdenk
54856f0544SStefan Roese   nand write addr ofs|partition size
55984e03cdSScott Wood      Write `size' bytes from `addr' to `ofs' in NAND flash.  Blocks that
56984e03cdSScott Wood      are marked bad are skipped.  If a page cannot be read because an
57984e03cdSScott Wood      uncorrectable data error is found, the command stops with an error.
587a8e9bedSwdenk
59984e03cdSScott Wood      As JFFS2 skips blocks similarly, this allows writing a JFFS2 image,
60984e03cdSScott Wood      as long as the image is short enough to fit even after skipping the
61984e03cdSScott Wood      bad blocks.  Compact images, such as those produced by mkfs.jffs2
62984e03cdSScott Wood      should work well, but loading an image copied from another flash is
63984e03cdSScott Wood      going to be trouble if there are any bad blocks.
647a8e9bedSwdenk
65c9494866SBen Gardiner   nand write.trimffs addr ofs|partition size
66c9494866SBen Gardiner      Enabled by the CONFIG_CMD_NAND_TRIMFFS macro. This command will write to
67c9494866SBen Gardiner      the NAND flash in a manner identical to the 'nand write' command
68c9494866SBen Gardiner      described above -- with the additional check that all pages at the end
69c9494866SBen Gardiner      of eraseblocks which contain only 0xff data will not be written to the
70c9494866SBen Gardiner      NAND flash. This behaviour is required when flashing UBI images
71c9494866SBen Gardiner      containing UBIFS volumes as per the UBI FAQ[1].
72c9494866SBen Gardiner
73c9494866SBen Gardiner      [1] http://www.linux-mtd.infradead.org/doc/ubi.html#L_flasher_algo
74c9494866SBen Gardiner
75856f0544SStefan Roese   nand write.oob addr ofs|partition size
767a8e9bedSwdenk      Write `size' bytes from `addr' to the out-of-band data area
777a8e9bedSwdenk      corresponding to `ofs' in NAND flash. This is limited to the 16 bytes
787a8e9bedSwdenk      of data for one 512-byte page or 2 256-byte pages. There is no check
797a8e9bedSwdenk      for bad blocks.
807a8e9bedSwdenk
81418396e2SScott Wood   nand read.raw addr ofs|partition [count]
82418396e2SScott Wood   nand write.raw addr ofs|partition [count]
83418396e2SScott Wood      Read or write one or more pages at "ofs" in NAND flash, from or to
84418396e2SScott Wood      "addr" in memory.  This is a raw access, so ECC is avoided and the
85418396e2SScott Wood      OOB area is transferred as well.  If count is absent, it is assumed
86418396e2SScott Wood      to be one page.  As with .yaffs2 accesses, the data is formatted as
87418396e2SScott Wood      a packed sequence of "data, oob, data, oob, ..." -- no alignment of
88418396e2SScott Wood      individual pages is maintained.
89fb3659acSMarek Vasut
907a8e9bedSwdenkConfiguration Options:
917a8e9bedSwdenk
92434f2cfcSpekon gupta   CONFIG_SYS_NAND_U_BOOT_OFFS
93434f2cfcSpekon gupta	NAND Offset from where SPL will read u-boot image. This is the starting
94434f2cfcSpekon gupta	address of u-boot MTD partition in NAND.
95434f2cfcSpekon gupta
96b5501f7dSJon Loeliger   CONFIG_CMD_NAND
973f42dc87SVagrant Cascadian      Enables NAND support and commands.
987a8e9bedSwdenk
993287f6d3SBenoît Thébaudeau   CONFIG_CMD_NAND_TORTURE
1003287f6d3SBenoît Thébaudeau      Enables the torture command (see description of this command below).
1013287f6d3SBenoît Thébaudeau
1026d0f6bcfSJean-Christophe PLAGNIOL-VILLARD   CONFIG_SYS_MAX_NAND_DEVICE
1037a8e9bedSwdenk      The maximum number of NAND devices you want to support.
1047a8e9bedSwdenk
10568ec9c85SPrabhakar Kushwaha   CONFIG_SYS_NAND_MAX_ECCPOS
10668ec9c85SPrabhakar Kushwaha      If specified, overrides the maximum number of ECC bytes
10768ec9c85SPrabhakar Kushwaha      supported.  Useful for reducing image size, especially with SPL.
10868ec9c85SPrabhakar Kushwaha      This must be at least 48 if nand_base.c is used.
10968ec9c85SPrabhakar Kushwaha
11068ec9c85SPrabhakar Kushwaha   CONFIG_SYS_NAND_MAX_OOBFREE
11168ec9c85SPrabhakar Kushwaha      If specified, overrides the maximum number of free OOB regions
11268ec9c85SPrabhakar Kushwaha      supported.  Useful for reducing image size, especially with SPL.
11368ec9c85SPrabhakar Kushwaha      This must be at least 2 if nand_base.c is used.
11468ec9c85SPrabhakar Kushwaha
11599067b08SScott Wood   CONFIG_SYS_NAND_MAX_CHIPS
11699067b08SScott Wood      The maximum number of NAND chips per device to be supported.
1177a8e9bedSwdenk
118578931b3SScott Wood   CONFIG_SYS_NAND_SELF_INIT
119*a430fa06SMiquel Raynal      Traditionally, glue code in drivers/mtd/nand/raw/nand.c has driven
120578931b3SScott Wood      the initialization process -- it provides the mtd and nand
121578931b3SScott Wood      structs, calls a board init function for a specific device,
122578931b3SScott Wood      calls nand_scan(), and registers with mtd.
123578931b3SScott Wood
124578931b3SScott Wood      This arrangement does not provide drivers with the flexibility to
125578931b3SScott Wood      run code between nand_scan_ident() and nand_scan_tail(), or other
126578931b3SScott Wood      deviations from the "normal" flow.
127578931b3SScott Wood
128*a430fa06SMiquel Raynal      If a board defines CONFIG_SYS_NAND_SELF_INIT, drivers/mtd/nand/raw/nand.c
129578931b3SScott Wood      will make one call to board_nand_init(), with no arguments.  That
130578931b3SScott Wood      function is responsible for calling a driver init function for
131578931b3SScott Wood      each NAND device on the board, that performs all initialization
132578931b3SScott Wood      tasks except setting mtd->name, and registering with the rest of
133578931b3SScott Wood      U-Boot.  Those last tasks are accomplished by calling  nand_register()
134578931b3SScott Wood      on the new mtd device.
135578931b3SScott Wood
136578931b3SScott Wood      Example of new init to be added to the end of an existing driver
137578931b3SScott Wood      init:
138578931b3SScott Wood
139578931b3SScott Wood	/* chip is struct nand_chip, and is now provided by the driver. */
14030780f94SBoris Brezillon	mtd = nand_to_mtd(&chip);
141578931b3SScott Wood
142578931b3SScott Wood	/*
143578931b3SScott Wood	 * Fill in appropriate values if this driver uses these fields,
144578931b3SScott Wood	 * or uses the standard read_byte/write_buf/etc. functions from
145578931b3SScott Wood	 * nand_base.c that use these fields.
146578931b3SScott Wood	 */
147578931b3SScott Wood	chip.IO_ADDR_R = ...;
148578931b3SScott Wood	chip.IO_ADDR_W = ...;
149578931b3SScott Wood
150578931b3SScott Wood	if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_CHIPS, NULL))
151578931b3SScott Wood		error out
152578931b3SScott Wood
153578931b3SScott Wood	/*
154578931b3SScott Wood	 * Insert here any code you wish to run after the chip has been
155578931b3SScott Wood	 * identified, but before any other I/O is done.
156578931b3SScott Wood	 */
157578931b3SScott Wood
158578931b3SScott Wood	if (nand_scan_tail(mtd))
159578931b3SScott Wood		error out
160578931b3SScott Wood
161b616d9b0SScott Wood	/*
162b616d9b0SScott Wood	 * devnum is the device number to be used in nand commands
1634dc34be4SSimon Glass	 * and in mtd->name.  Must be less than CONFIG_SYS_MAX_NAND_DEVICE.
164b616d9b0SScott Wood	 */
165b616d9b0SScott Wood	if (nand_register(devnum, mtd))
166578931b3SScott Wood		error out
167578931b3SScott Wood
168578931b3SScott Wood      In addition to providing more flexibility to the driver, it reduces
169578931b3SScott Wood      the difference between a U-Boot driver and its Linux counterpart.
170578931b3SScott Wood      nand_init() is now reduced to calling board_nand_init() once, and
171578931b3SScott Wood      printing a size summary.  This should also make it easier to
172578931b3SScott Wood      transition to delayed NAND initialization.
173578931b3SScott Wood
174578931b3SScott Wood      Please convert your driver even if you don't need the extra
175578931b3SScott Wood      flexibility, so that one day we can eliminate the old mechanism.
176578931b3SScott Wood
177beba5f04Spekon gupta
178d016dc42Spekon gupta   CONFIG_SYS_NAND_ONFI_DETECTION
179d016dc42Spekon gupta	Enables detection of ONFI compliant devices during probe.
180d016dc42Spekon gupta	And fetching device parameters flashed on device, by parsing
181d016dc42Spekon gupta	ONFI parameter page.
182d016dc42Spekon gupta
183beba5f04Spekon guptaPlatform specific options
184beba5f04Spekon gupta=========================
185beba5f04Spekon gupta   CONFIG_NAND_OMAP_GPMC
186beba5f04Spekon gupta	Enables omap_gpmc.c driver for OMAPx and AMxxxx platforms.
187beba5f04Spekon gupta	GPMC controller is used for parallel NAND flash devices, and can
188beba5f04Spekon gupta	do ECC calculation (not ECC error detection) for HAM1, BCH4, BCH8
189beba5f04Spekon gupta	and BCH16 ECC algorithms.
190beba5f04Spekon gupta
191beba5f04Spekon gupta   CONFIG_NAND_OMAP_ELM
192beba5f04Spekon gupta	Enables omap_elm.c driver for OMAPx and AMxxxx platforms.
193beba5f04Spekon gupta	ELM controller is used for ECC error detection (not ECC calculation)
194beba5f04Spekon gupta	of BCH4, BCH8 and BCH16 ECC algorithms.
195beba5f04Spekon gupta	Some legacy platforms like OMAP3xx do not have in-built ELM h/w engine,
196beba5f04Spekon gupta	thus such SoC platforms need to depend on software library for ECC error
197beba5f04Spekon gupta	detection. However ECC calculation on such plaforms would still be
198beba5f04Spekon gupta	done by GPMC controller.
199beba5f04Spekon gupta
200434f2cfcSpekon gupta   CONFIG_SPL_NAND_AM33XX_BCH
201434f2cfcSpekon gupta	Enables SPL-NAND driver (am335x_spl_bch.c) which supports ELM based
202434f2cfcSpekon gupta        hardware ECC correction. This is useful for platforms which have ELM
203434f2cfcSpekon gupta	hardware engine and use NAND boot mode.
204434f2cfcSpekon gupta	Some legacy platforms like OMAP3xx do not have in-built ELM h/w engine,
205434f2cfcSpekon gupta	so those platforms should use CONFIG_SPL_NAND_SIMPLE for enabling
206434f2cfcSpekon gupta        SPL-NAND driver with software ECC correction support.
207434f2cfcSpekon gupta
2083f719069Spekon gupta   CONFIG_NAND_OMAP_ECCSCHEME
2093f719069Spekon gupta	On OMAP platforms, this CONFIG specifies NAND ECC scheme.
2103f719069Spekon gupta	It can take following values:
2113f719069Spekon gupta	OMAP_ECC_HAM1_CODE_SW
2123f719069Spekon gupta		1-bit Hamming code using software lib.
2133f719069Spekon gupta		(for legacy devices only)
2143f719069Spekon gupta	OMAP_ECC_HAM1_CODE_HW
2153f719069Spekon gupta		1-bit Hamming code using GPMC hardware.
2163f719069Spekon gupta		(for legacy devices only)
2173f719069Spekon gupta	OMAP_ECC_BCH4_CODE_HW_DETECTION_SW
2183f719069Spekon gupta		4-bit BCH code (unsupported)
2193f719069Spekon gupta	OMAP_ECC_BCH4_CODE_HW
2203f719069Spekon gupta		4-bit BCH code (unsupported)
2213f719069Spekon gupta	OMAP_ECC_BCH8_CODE_HW_DETECTION_SW
2223f719069Spekon gupta		8-bit BCH code with
2233f719069Spekon gupta		- ecc calculation using GPMC hardware engine,
2243f719069Spekon gupta		- error detection using software library.
2253f719069Spekon gupta		- requires CONFIG_BCH to enable software BCH library
2263f719069Spekon gupta		(For legacy device which do not have ELM h/w engine)
2273f719069Spekon gupta	OMAP_ECC_BCH8_CODE_HW
2283f719069Spekon gupta		8-bit BCH code with
2293f719069Spekon gupta		- ecc calculation using GPMC hardware engine,
2303f719069Spekon gupta		- error detection using ELM hardware engine.
231867f0304Spekon gupta	OMAP_ECC_BCH16_CODE_HW
232867f0304Spekon gupta		16-bit BCH code with
233867f0304Spekon gupta		- ecc calculation using GPMC hardware engine,
234867f0304Spekon gupta		- error detection using ELM hardware engine.
235867f0304Spekon gupta
236867f0304Spekon gupta	How to select ECC scheme on OMAP and AMxx platforms ?
237867f0304Spekon gupta	-----------------------------------------------------
238867f0304Spekon gupta	Though higher ECC schemes have more capability to detect and correct
239867f0304Spekon gupta	bit-flips, but still selection of ECC scheme is dependent on following
240867f0304Spekon gupta	- hardware engines present in SoC.
241867f0304Spekon gupta		Some legacy OMAP SoC do not have ELM h/w engine thus such
242867f0304Spekon gupta		SoC cannot support BCHx_HW ECC schemes.
243867f0304Spekon gupta	- size of OOB/Spare region
244867f0304Spekon gupta		With higher ECC schemes, more OOB/Spare area is required to
245867f0304Spekon gupta		store ECC. So choice of ECC scheme is limited by NAND oobsize.
246867f0304Spekon gupta
247867f0304Spekon gupta	In general following expression can help:
248867f0304Spekon gupta		NAND_OOBSIZE >= 2 + (NAND_PAGESIZE / 512) * ECC_BYTES
249867f0304Spekon gupta	where
250867f0304Spekon gupta		NAND_OOBSIZE	= number of bytes available in
251867f0304Spekon gupta				OOB/spare area per NAND page.
252867f0304Spekon gupta		NAND_PAGESIZE	= bytes in main-area of NAND page.
253867f0304Spekon gupta		ECC_BYTES	= number of ECC bytes generated to
254867f0304Spekon gupta				protect 512 bytes of data, which is:
255867f0304Spekon gupta				3 for HAM1_xx ecc schemes
256867f0304Spekon gupta				7 for BCH4_xx ecc schemes
257867f0304Spekon gupta				14 for BCH8_xx ecc schemes
258867f0304Spekon gupta				26 for BCH16_xx ecc schemes
259867f0304Spekon gupta
260867f0304Spekon gupta		example to check for BCH16 on 2K page NAND
261867f0304Spekon gupta		NAND_PAGESIZE = 2048
262867f0304Spekon gupta		NAND_OOBSIZE = 64
263867f0304Spekon gupta		2 + (2048 / 512) * 26 = 106 > NAND_OOBSIZE
264867f0304Spekon gupta		Thus BCH16 cannot be supported on 2K page NAND.
265867f0304Spekon gupta
266867f0304Spekon gupta		However, for 4K pagesize NAND
267867f0304Spekon gupta		NAND_PAGESIZE = 4096
268caad0d00SFabian Mewes		NAND_OOBSIZE = 224
269867f0304Spekon gupta		ECC_BYTES = 26
270867f0304Spekon gupta		2 + (4096 / 512) * 26 = 210 < NAND_OOBSIZE
271867f0304Spekon gupta		Thus BCH16 can be supported on 4K page NAND.
272867f0304Spekon gupta
273beba5f04Spekon gupta
274c316f577SDaniel Mack    CONFIG_NAND_OMAP_GPMC_PREFETCH
275c316f577SDaniel Mack	On OMAP platforms that use the GPMC controller
276c316f577SDaniel Mack	(CONFIG_NAND_OMAP_GPMC_PREFETCH), this options enables the code that
277c316f577SDaniel Mack	uses the prefetch mode to speed up read operations.
278c316f577SDaniel Mack
2794e3ccd26SWolfgang DenkNOTE:
2804e3ccd26SWolfgang Denk=====
2814e3ccd26SWolfgang Denk
28299067b08SScott WoodThe Disk On Chip driver is currently broken and has been for some time.
283*a430fa06SMiquel RaynalThere is a driver in drivers/mtd/nand/raw, taken from Linux, that works with
28499067b08SScott Woodthe current NAND system but has not yet been adapted to the u-boot
28599067b08SScott Woodenvironment.
2862255b2d2SStefan Roese
2872255b2d2SStefan RoeseAdditional improvements to the NAND subsystem by Guido Classen, 10-10-2006
2882255b2d2SStefan Roese
2892255b2d2SStefan RoeseJFFS2 related commands:
2902255b2d2SStefan Roese
2912255b2d2SStefan Roese  implement "nand erase clean" and old "nand erase"
2922255b2d2SStefan Roese  using both the new code which is able to skip bad blocks
2932255b2d2SStefan Roese  "nand erase clean" additionally writes JFFS2-cleanmarkers in the oob.
2942255b2d2SStefan Roese
2952255b2d2SStefan RoeseMiscellaneous and testing commands:
2962255b2d2SStefan Roese  "markbad [offset]"
2972255b2d2SStefan Roese  create an artificial bad block (for testing bad block handling)
2982255b2d2SStefan Roese
2992255b2d2SStefan Roese  "scrub [offset length]"
3002255b2d2SStefan Roese  like "erase" but don't skip bad block. Instead erase them.
3012255b2d2SStefan Roese  DANGEROUS!!! Factory set bad blocks will be lost. Use only
3022255b2d2SStefan Roese  to remove artificial bad blocks created with the "markbad" command.
3032255b2d2SStefan Roese
3041866be7dSMax Krummenacher  "torture offset [size]"
3053287f6d3SBenoît Thébaudeau  Torture block to determine if it is still reliable.
3063287f6d3SBenoît Thébaudeau  Enabled by the CONFIG_CMD_NAND_TORTURE configuration option.
3073287f6d3SBenoît Thébaudeau  This command returns 0 if the block is still reliable, else 1.
3083287f6d3SBenoît Thébaudeau  If the block is detected as unreliable, it is up to the user to decide to
3093287f6d3SBenoît Thébaudeau  mark this block as bad.
3103287f6d3SBenoît Thébaudeau  The analyzed block is put through 3 erase / write cycles (or less if the block
3113287f6d3SBenoît Thébaudeau  is detected as unreliable earlier).
3123287f6d3SBenoît Thébaudeau  This command can be used in scripts, e.g. together with the markbad command to
3133287f6d3SBenoît Thébaudeau  automate retries and handling of possibly newly detected bad blocks if the
3143287f6d3SBenoît Thébaudeau  nand write command fails.
3153287f6d3SBenoît Thébaudeau  It can also be used manually by users having seen some NAND errors in logs to
3163287f6d3SBenoît Thébaudeau  search the root cause of these errors.
3173287f6d3SBenoît Thébaudeau  The underlying nand_torture() function is also useful for code willing to
3183287f6d3SBenoît Thébaudeau  automate actions following a nand->write() error. This would e.g. be required
3193287f6d3SBenoît Thébaudeau  in order to program or update safely firmware to NAND, especially for the UBI
3203287f6d3SBenoît Thébaudeau  part of such firmware.
3211866be7dSMax Krummenacher  Optionally, a second parameter size can be given to test multiple blocks with
3221866be7dSMax Krummenacher  one call. If size is not a multiple of the NAND's erase size, then the block
3231866be7dSMax Krummenacher  that contains offset + size will be tested in full. If used with size, this
3241866be7dSMax Krummenacher  command returns 0 if all tested blocks have been found reliable, else 1.
3253287f6d3SBenoît Thébaudeau
3262255b2d2SStefan Roese
3272255b2d2SStefan RoeseNAND locking command (for chips with active LOCKPRE pin)
3282255b2d2SStefan Roese
3292255b2d2SStefan Roese  "nand lock"
3302255b2d2SStefan Roese  set NAND chip to lock state (all pages locked)
3312255b2d2SStefan Roese
3322255b2d2SStefan Roese  "nand lock tight"
3332255b2d2SStefan Roese  set NAND chip to lock tight state (software can't change locking anymore)
3342255b2d2SStefan Roese
3352255b2d2SStefan Roese  "nand lock status"
3362255b2d2SStefan Roese  displays current locking status of all pages
3372255b2d2SStefan Roese
3382255b2d2SStefan Roese  "nand unlock [offset] [size]"
3392255b2d2SStefan Roese  unlock consecutive area (can be called multiple times for different areas)
3402255b2d2SStefan Roese
341eee623a5SJoe Hershberger  "nand unlock.allexcept [offset] [size]"
342eee623a5SJoe Hershberger  unlock all except specified consecutive area
3432255b2d2SStefan Roese
3442255b2d2SStefan RoeseI have tested the code with board containing 128MiB NAND large page chips
3452255b2d2SStefan Roeseand 32MiB small page chips.
346