Introduction

Linux MTD subsystem has originally provided drivers for some SPI NOR/NAND flash models, and Realtek has made some changes to support SoC’s MCM flash. Limited by the implementation of SPI Flash controller driver, the SPI NOR/NAND flash drivers only work in user mode.

Architecture

The software architectures of SPI NOR/NAND Flash driver are illustrated in the following two figures respectively.

../../_images/nor_flash_driver_software_arch.svg
../../_images/nand_flash_driver_software_arch.svg

Implementation

The SPI NOR/NAND Flash drivers are originally implemented by Linux kernel as following files:

File

Description

<linux>/drivers/mtd/spi-nor/Kconfig

SPI NOR Flash driver Kconfig

<linux>/drivers/mtd/spi-nor/Makefile

SPI NOR Flash driver Makefile

<linux>/drivers/mtd/spi-nor/spi-nor.c

SPI NOR Flash driver implementation

<linux>/drivers/mtd/nand/spi/Kconfig

SPI NAND Flash driver Kconfig

<linux>/drivers/mtd/nand/spi/Makefile

SPI NAND Flash driver Makefile

<linux>/drivers/mtd/nand/spi/core.c

SPI NAND Flash core driver

<linux>/drivers/mtd/nand/spi/*.c

SPI NAND Flash manufacture drivers

Configuration

DTS Configuration

For NOR Flash driver, the DTS node is defined in <dts>/rtl8730e-spi-nor.dtsi.

flash0: flash@0 {
   #address-cells = <1>;
   #size-cells = <1>;
   compatible = "jedec,spi-nor";
   spi-max-frequency = <100000000>;
   reg = <0>;
   partitions {
      compatible = "fixed-partitions";
      #address-cells = <1>;
      #size-cells = <1>;
      /* Partitions */
   };
};

The configurations are listed in the table below:

Property

Description

Configurable?

compatible

ID to match the flash driver with flash device

No

#address-cells

Address cells for flash partition child node

No

#size-cells

Size cells for flash partition child node

No

reg

Reserved

No

mode

Reserved

No

partitions

The partitions for different area at flash, user can modify the partitions’ size, lable, etc.

Yes

Note

Above DTS files will be auto selected after chip is chosen, do not change the DTS configurations if not necessary.

Build Configuration

For SPI NAND/NOR Flash:

  1. Select Device Drivers -> Memory Technology Divece (MTD) support

  2. Select SPI-NOR device support or SPI-NAND device support

    ../../_images/mtd_support_configuration.PNG
    ../../_images/spi_nor_device_support_configuration.PNG
    ../../_images/spi_nand_device_support_configuration.PNG

APIs

APIs for User Space

User can access data on Flash from user space via:

  • C API provided by GLIBC, such as open/close/read/write functions

    Refer to The GNU C Library for details.

  • Shell utilities provided by Busybox, such as mount/unmount/mkdir/rm comands

    Refer to BusyBox for details.

APIs for Kernel Space

The SPI NAND/NOR Flash drivers provide callbacks for MTD framework, the prototypes of these callbacks are defined in <linux>/include/linux/mtd/mtd.h.

  • SPI NOR Flash driver provides the following callbacks:

    API

    Introduction

    _erase

    Erase the specific SPI NOR Flash region

    _read

    Read data from SPI NOR Flash to buffer

    _write

    Write data from buffer to SPI NOR Flash

    _resume

    Resume handler for power management

    _lock

    Lock specific SPI NOR Flash region

    _unlock

    Unlock specific SPI NOR Flash region

    _is_locked

    Check the specific SPI NOR Flash region is locked

    Refer to SPI NOR framework for details of Linux SPI NOR framework.

  • SPI NAND Flash driver provides the following callbacks:

    API

    Introduction

    _read_oob

    Read data from SPI NAND Flash to buffer

    _write_oob

    Write data from buffer to SPI NAND Flash

    _block_isbad

    Check whether the SPI NAND Flash block is marked as bad block

    _block_markbad

    Mark the SPI NAND Flash block as bad block

    _block_isreserved

    Check whether the SPI NAND Flash block is reserved

    _erase

    Erase the specific SPI NAND Flash region

    _max_bad_blocks

    Get the maximum number of bad blocks on a specific SPI NAND Flash region

    Refer to MTD NAND Driver Programming Interface for details of MTD NAND driver programming interfaces.