nandlog v1.1

Chip Drivers

How to add support for a SPI NAND part that nandlog has no driver for. You need this page only if your part is not one of the two that ship with the library.

A chip driver is one .c file in chips/ that teaches the log one part. It declares the part's geometry in six macros, includes nandlog_chip_common.h, and implements the fifteen functions of nandlog_chip.h. It reaches the bus only through the port's two transfer functions, so it holds no platform code.

Two drivers ship with the library, each complete on its own: one for the Alliance AS5F18G04SND (8 Gbit, 4 KB pages) and one for the Winbond W25N01GWZEIG (1 Gbit, 2 KB pages). The common header holds no command codes or register bits, so copy whichever driver is closer to your part and work from its datasheet.

The two handle bad blocks in opposite ways. The Alliance driver keeps its own table on the part, and the log steps over bad blocks where they lie. The Winbond driver hands each bad block to the part's built-in remap table. The Alliance driver has run the 1.0 library on hardware, and the Winbond driver the 1.1 library (see Testing).

To write a driver, read Declare the geometry and Implement the functions, then Test the driver. If your part has no remap table of its own, also read Bad blocks and When the reserve cannot hold the table.

Declare the geometry

A driver begins with six macros and then includes the common header. The geometry lives in the driver and nowhere else.

/* chips/nandlog_chip_MYPART.c */

#define NANDLOG_CHIP_NAME                    "MYPART"
#define NANDLOG_CHIP_PAGE_SIZE_BYTES         4096   /* main array, excluding spare  */
#define NANDLOG_CHIP_SPARE_SIZE_BYTES        256    /* spare/ECC bytes that follow  */
#define NANDLOG_CHIP_PAGES_PER_BLOCK         64     /* must be a power of two       */
#define NANDLOG_CHIP_BLOCK_COUNT             4096
#define NANDLOG_CHIP_RESERVED_BLOCKS         16     /* kept out of the log region   */

#include "nandlog_chip_common.h"

The common header stops the build if a macro is missing, if the page or spare area exceeds NANDLOG_MAX_PAGE_SIZE_BYTES or NANDLOG_MAX_SPARE_SIZE_BYTES, if the pages per block are not a power of two, or if the reserve is not smaller than the array. It then defines these:

MacroWhat it is
NANDLOG_CHIP_PAGE_COUNTPages in the whole array.
NANDLOG_CHIP_PAGE_WITH_SPARE_SIZE_BYTESA page plus its spare area.
NANDLOG_CHIP_BLOCK_MASKMasks a page address to the first page of its block.
NANDLOG_CHIP_RESERVED_BASE_PAGEThe first page of the reserve.
NANDLOG_CHIP_GEOMETRY_INITIALIZERFills a nandlog_geometry_t.
NANDLOG_CHIP_NO_INTERNAL_PAGE_COPYA complete nandlog_chip_copy_page() for a part with no internal data move.

The reserve is the blocks at the top of the array, where the log never places a page. (The log keeps its metadata ring in the first eight blocks and logs into everything between.) A driver that keeps its own table reserves room for the table and the on-device tests: the Alliance driver reserves 16 of 4,096 blocks, eight for each. A part that remaps in hardware needs its spare pool reserved: the Winbond driver keeps 40 of 1,024.

Busy waits. Each supplied driver polls the status register at most NANDLOG_BUSY_TIMEOUT_POLLS times, NANDLOG_BUSY_POLL_INTERVAL_US apart (by default 50,000 polls 10 µs apart: at least 0.5 s, plus the reads). On a timeout it calls nandlog_port_fatal(), and if a port returns anyway, the wait hands back a status with every failure bit set, so no caller reads it as success. Do the same in a new driver.

Implement the functions

A driver implements all fifteen functions of nandlog_chip.h, and may use its nandlog_bytes_erased() helper.

Power. Every function that touches the bus may assume that the port and the part are awake, because the core wakes both around each operation or holds them awake for a session. nandlog_init() leaves both asleep, so a bench test that calls the driver directly must call nandlog_begin_session() first.

Identity and setup

const nandlog_geometry_t *nandlog_chip_geometry(void);

Return a static const geometry built with NANDLOG_CHIP_GEOMETRY_INITIALIZER. It is valid at any time, from RAM.

bool nandlog_chip_probe(void);

Confirm that the part is answering, before nandlog_chip_init(). Both drivers read the device ID up to 1,000 times, 1 ms apart.

void nandlog_chip_init(void);

Configure the status registers, then load the persisted bad-block table, or on a first boot build it from the factory marks and persist it. Called once per nandlog_init(), after a successful probe.

void nandlog_chip_low_power(bool sleep);

Enter or leave the part's lowest-power state. Both drivers leave it empty, since neither part has a deep-power-down command worth its wake-up latency.

Data

nandlog_read_result_t nandlog_chip_read_page(uint8_t *buffer, uint32_t page);

Read a whole page with ECC on, and report the part's verdict.

ValueMeaning
NANDLOG_READ_UNCORRECTABLE (0)The ECC could not correct the page.
NANDLOG_READ_OK (1)No error, or errors corrected with margin to spare.
NANDLOG_READ_AT_ECC_LIMIT (2)Corrected at the limit of the ECC. Readable.

The log treats a page that reads back at the limit straight after its program as not written, and rewrites a metadata copy that reads at the limit (see Durability). A part with no such verdict never reports it. Decode the status bits from your datasheet, not by analogy. Both supplied parts report ECC status in bits 5:4 of register C0h, but the Alliance part uses 11b for a page corrected at the limit, while the Winbond part uses 10b and 11b alike for an uncorrectable page.

nandlog_read_result_t nandlog_chip_read_page_region(uint8_t *buffer, uint32_t page,
                                                    uint32_t offset, uint32_t length);

Read length bytes from offset within the page. Both drivers issue PAGE DATA READ, take the verdict from the status word that ends the BUSY wait, then issue READ with a 16-bit column address, most significant byte first, and a dummy byte. A short read is safe, because the part corrects the page as it latches it into the cache register.

This function carries the most weight. The log reads far more 32-byte page headers than whole pages, so issue the column address rather than read the whole page and copy a slice. A full epoch of the Alliance part spans about 260,000 pages, so a bounded download then reads about 8 MB of headers instead of 1 GB of pages.

bool nandlog_chip_page_is_blank(uint32_t page);

Whether every bit of the page and its spare area reads erased. The log uses this to tell the write head from a page a power cut tore. Read with ECC off, because an ECC engine can correct a few stray bits in an erased page to FFh, hiding the lightly programmed page this check exists to find. When unsure, answer false, which costs only a page. Scan with nandlog_bytes_erased(): on the Apollo4 Plus at 96 MHz it checks a 4,352-byte page and spare area in 46 µs, against 364 µs for a byte-at-a-time loop.

bool nandlog_chip_write_page(const uint8_t *data, uint32_t page);

Program a whole page with one PROGRAM EXECUTE, opening and closing write protection around it, and return false if the part reports a failure. Never repeat a failed program, because the page may already hold part of it. The log decides whether to retire the block.

Always start PROGRAM LOAD at column 0. On the Alliance part, a load resets the cache register to FFh only from its own column on, and PROGRAM EXECUTE programs the whole register. A one-byte load at the spare area's column, after a read of a page of zeros, therefore programs 4,096 zeros into the main area. To change a few bytes, such as a bad-block mark, load the whole page and spare area from column 0, FFh elsewhere. Avoid PROGRAM LOAD RANDOM DATA (84h), which the Alliance datasheet allows only inside an internal data move. The simulator enforces both rules.

bool nandlog_chip_erase_block(uint32_t page);

Erase the block that holds page, trying up to NANDLOG_BLOCK_ERRORS_BEFORE_REMOVAL times (3 by default), since an erase can be repeated safely. Return false only if every attempt failed.

nandlog_copy_result_t nandlog_chip_copy_page(uint32_t source_page, uint32_t destination_page);

Move a page inside the part, as the log does when it relocates a failing block. Return NANDLOG_COPY_OK, NANDLOG_COPY_FAILED (a program failure), or NANDLOG_COPY_UNSUPPORTED (the log then reads and writes the page itself). The copy is a page read and one PROGRAM EXECUTE, with no program load between them, so only six address bytes cross the bus. On the Apollo4 Plus it takes 1.24 ms for a 4 KB page, against 2.68 ms for a read and a write. Inside their write-protect calls, both drivers do this:

wait_until_not_busy();
nandlog_port_transfer_write(COMMAND_PAGE_DATA_READ, NULL, 0, source_address, sizeof(source_address));
wait_until_not_busy();
nandlog_port_transfer_write(COMMAND_WRITE_ENABLE, NULL, 0, NULL, 0);
nandlog_port_transfer_write(COMMAND_PROGRAM_EXECUTE, NULL, 0, destination_address, sizeof(destination_address));
const bool programmed = (wait_until_not_busy() & STATUS_WRITE_FAILURE) != STATUS_WRITE_FAILURE;

A part with no internal data move expands NANDLOG_CHIP_NO_INTERNAL_PAGE_COPY instead. The driver need not check the source, because the log copies only a page it has read whole, and reads back the copy's header. NANDLOG_CHIP_PAGE_COPY set to 0 takes the copy out of a build.

Bad blocks

bool nandlog_chip_is_bad_block(uint32_t page);

Whether the block is retired. This must not touch flash: answer from a table in RAM. Every boot asks about every block below the reserve in order, so make in-order questions cheap. The Alliance driver remembers the run of good blocks around its last answer, so a boot searches its table about once per entry.

bool nandlog_chip_mark_bad_block(uint32_t page);

Retire the block persistently and atomically, and return whether it is now retired. The log lays its positions out over the blocks the driver calls good, so a retirement must survive a power cut whole or not at all, and must not show in nandlog_chip_is_bad_block() until it is persistent. Retiring a retired block must not use a second entry or spare.

uint32_t nandlog_chip_table_blocks_wanted(uint32_t *lowest_page);

How many log-region blocks the driver's table needs because it has run out of room, or zero, from RAM. The run starts on a multiple of that many blocks, at or above *lowest_page. See below.

bool nandlog_chip_table_take_blocks(uint32_t first_page);

Take the run at first_page for the table, and retire it. Return true once the table is on the part there and names every block of the run, or false with none retired.

bool nandlog_chip_reset_bad_blocks(void);

The recovery utility behind nandlog_reset_bad_block_table(): discard the persisted table so the next boot rebuilds it, and return whether it was verified gone. Keep the RAM table until the reboot, and never erase a marked block. Discard nothing, log why, and return false if a rebuild would forget a retired block or the part cannot forget.

Bad blocks: two strategies

The Alliance driver keeps a software table on the part, and the log steps over retired blocks where they lie. The Winbond driver uses the part's hardware remap table (its LUT), which maps a retired block's address to a spare block in the reserve.

Software table (Alliance)Hardware remap (Winbond)
The recordVersioned, CRC-checked pages in the reserve's first eight blocks.The part's remap table.
Capacity256 entries. The datasheet allows 80 bad blocks.20 entries, factory-bad blocks included.
First bootRead every block's mark and persist the table, then write an OTP marker.Remap each factory-bad block, then write an OTP marker.
A retirementA new table version, read back, then a 00h mark in the block.One remap entry, then the table read back.
ResetMark every entry again, then erase the table and verify.Refused. Entries are one-way.
Out of roomMove below the reserve, or let the marks stand in.The log stops taking pages.

A software table

The Alliance driver writes its table the way the log writes pages, and a table of your own should follow the same rules.

Read disturb can program one bit of a table block's own mark, outside the ECC, so the driver takes a reserve block whose mark has one programmed bit as unmarked when it holds a newer version that does not name it. The page layout is in On-Flash Format.

A hardware remap table

The Winbond driver adds a remap entry for each retirement, pointing at the first reserve block that reads unmarked and that no entry uses yet. The part's limits become the log's.

What each strategy means for durability, including how the log checks the table against its own record at every boot, is on Durability.

When the reserve cannot hold the table

If the blocks a driver keeps for its table fail, it can ask the log for blocks of the log region, and if none can be had, it can let its spare-area marks stand in. A part that remaps in hardware needs neither, so the Winbond driver returns zero and false.

Asking for blocks

The Alliance driver asks for two blocks when its table has no usable home: no version to build on, a failed last write, or a full block with nowhere to move. It sets *lowest_page to the start of its boot-time search, so a later boot can find the table. The log asks once a boot, when it knows where the current epoch lies and before it writes anything, and again whenever nandlog_chip_mark_bad_block() fails. If it gives blocks then, it tries the mark once more.

The log answers with the highest aligned run below the reserve whose blocks are all in the log region, not retired, outside the current epoch's positions, not the write head's block or one being retired or copied into, and free of any page with a valid header. It tries at most NANDLOG_PAGE_PLACEMENT_ATTEMPTS runs (3 by default). If a retirement still cannot be recorded, the log stops taking pages.

Taking the blocks

Check that the run is aligned and inside your search, and let the old table go only once the new one is written. The Alliance driver moves its table in three steps.

  1. It erases both blocks and writes the first version, naming both, into the lower block, which a search reaches first.
  2. It marks every reserve block that may hold a version, the newest last, so no boot can load the old table.
  3. It writes a second version, which tells later boots that the move finished.

The pair is retired like any failed block, and its two blocks are the only entries with no mark, since a mark would hide the versions in them. A boot searches below the reserve only if no reserve block yields a version, reading one header per block (two if the first page reads erased or unreadable) down to NANDLOG_CHIP_TABLE_SEARCH_BLOCKS from the top, which defaults to the reserve plus 48 blocks. At 279 µs per header read on the Apollo4 Plus, those 48 blocks cost about 13 to 27 ms.

Degraded mode

With no pair to be had, the Alliance driver lets the marks stand in for the table.

Neither fallback costs a healthy part a read.

Test the driver

Test a new driver first over the host simulator, which emulates a part at the SPI command level and cuts power at every program and erase, then on hardware. Testing describes each test.

In the simulator

sim/nandlog_port_sim.c implements nandlog_port.h over a part held in RAM, shaped by your driver's nandlog_chip_geometry(). Your driver runs on it unmodified if your part speaks what it emulates: commands 9Fh, FFh, 0Fh, 1Fh, 06h, 04h, D8h, 02h, 84h, 10h, 13h, and 03h, the Winbond remap commands A1h and A5h, and registers A0h, B0h, and C0h with the supplied parts' bit layout. Otherwise, extend the emulation first. Then:

  1. In sim/Makefile, add a variable for your driver beside ALLIANCE and WINBOND, and builds modeled on nandlog_sim_test_winbond, nandlog_sim_regress_winbond, and nandlog_sim_fuzz_winbond. List them in SUITES, REGRESS, and FUZZ.
  2. In each of those three programs, add your part's device ID and factory-mark offset beside the #ifdef SIM_CHIP_WINBOND case.
  3. Run make and make fuzz in sim/.

make campaign and make exhaustive build only the Alliance driver, with its device ID in nandlog_sim_campaign.c and nandlog_sim_exhaustive.c. A driver that keeps its own table should be built into both too, since they cut power inside every retirement their traces make. The regression tests of the Alliance table, compiled out of the Winbond build, are the model for testing your own.

On hardware

Three on-device tests gate a driver. Run them in order, because the second and third read through the primitive the first one checks.

Source (target)What it checks
test_nandlog_partial_read.c (nandlog_reads)Column-addressed reads land where they claim, and every block's mark reads the same either way.
test_nandlog_page_copy.c (nandlog_copy)The internal copy moves the source page, not the cache register's old contents, and leaves the spare area behind.
test_nandlog_ecc_marker.c (nandlog_marker)A 00h mark can be programmed into the spare area with ECC off, even after the main area holds data.

The tests write only in the top eight blocks of the reserve, so keep nothing there. They are written against the SociTrack firmware's setup_hardware(), print(), and am_hal_delay_us(), which another platform must supply, and tests/README.md lists the Makefile targets. test_nandlog_partial_read.c and test_nandlog_ecc_marker.c carry the Alliance part's command codes, so change them for yours. If the copy test fails, set NANDLOG_CHIP_PAGE_COPY to 0. Afterward, nandlog_bench measures your part and bus, but it erases the log region and metadata ring (see Testing).

A wrong column byte order fails silently. The driver reads FFh from the wrong place, the factory scan finds every block good, and the log writes into blocks the manufacturer condemned. The simulator cannot catch this, because it decodes the column address the same way your driver encodes it. Run nandlog_reads before you deploy.