MTD Driver

The sunxi-nand MTD Linux driver is the MTD driver for the NAND controller present in the Allwinner A20 chip. It is meant to replace the Allwinner driver present in linux-sunxi-3.4 programmed by Allwinner. In contrast with the Allwinner driver, this driver exposes the underlying NAND device using the standard Linux MTD interface and not as an emulated block device. Also, this driver allows writing to the whole NAND flash from Linux, including the part where the boot0 bootloader is stored.

Kernel
The main work on the MTD driver is done by bbrezillon. In order to test the driver you'll have to compile a kernel from these sources and use the dtb compiled for your board as well.

Booting
TODO: dtb, u-boot, boot0/sunxi-spl, special hw-randomization for boot partitions

Vocabulary
NAND devices being very different from traditional block devices, require specific vocabulary in order to be able to talk about them. Making sure you use the right word helps both getting your message across and also getting help faster without wasting time describing what you're trying to talk about. Bellow is some common terminology used when talking about MTD devices in general.

Erase Blocks
An MTD device consists of a number of erase blocks. An erase block is the minimum I/O unit of an erase operation. An erase operation sets all the bits of an erase block to 1s. You can think of them like the blocks of traditional block devices but in reality they behave very differently. See this FAQ for a list of differences between the two.

Pages
An erase block consists of a number of pages. A page is the minimum I/O unit of a read/write operation. When a page is written bits can only change from 1 to 0. In order to change a bit in a page from 0 to 1 a full erase block erase is required before rewriting the block again (and possibly the other blocks that were in the erase block) with the specific bit flipped.

OOB area
A page consists of a data area and a out of band (OOB) area. NAND devices do not store data reliably. Some of the bits you write or read might get flipped. The OOB area is some extra space per page provided by the NAND manufacturer to save Error Correcting Code (ECC) bytes. This though is not something enforced and is completely controlled by software. In theory one could use the whole page to write data but with the caveat of getting bits flipped undetectably.

ECC data
ECC data is written in the OOB area of each page to provide detection and correction of bit flips in the data of the page. The ECC is characterised by its strength, meaning how many bit flips it can correct before failing and the algorithm used to produce the ECC. The generation of the ECC data can be either done in software of using a hardware ECC generator usually located in the SoC of the board.

Bad Blocks
As we already saw, an erase block wears-off as erase operations happen. At some point the erase block will fail to turn all its bits to 1 after an erase operation. When this happens we say that we have a bad block and we must avoid using it for our data. bad blocks are very common and NAND chips usually ship with bad blocks in them from the manufacturer.

Bad Block Marker (BBM)
In order to identify bad blocks, bad block markers are used. The marker is implemented by writing 0x00 on the first 2 bytes of the OOB of the first, last or the first two pages of a block. When a block is marked as bad it is not used for IO operations.

Bad Block Table (BBT)
In order to cope with the fact that bad blocks exist and new might appear in the future the system stores information about which blocks are bad in a Bad Block Table (BBT). This table is consulted before any I/O operation and updated whenever an erase operation fails. This table can be either regenerated every time the device starts up or it can be persisted on the NAND chip itself. Regenerating the table on each boot takes a considerable amount of time as the whole NAND has to be scanned and usually an on-nand BBT is used. The table is stored in the last two good erase blocks of the NAND and is preserved across reboots.