@@ -46,10 +46,83 @@ See include/nuttx/mtd/mtd.h for additional information.
4646
4747 #. Get an instance of ``struct mtd_dev_s `` from the
4848 hardware-specific MTD device driver, and
49- #. Provide that instance to the initialization method of the
50- higher level device driver.
5149
52- - **Examples **: ``drivers/mtd/m25px.c `` and ``drivers/mtd/ftl.c ``
50+ #. Use the ``register_mtddriver `` interface to register the MTD drivers.
51+
52+ - **Examples **: ``drivers/mtd/m25px.c `` and ``boards/arm/sama5/sama5d4-ek/src/sam_at25.c ``
53+
54+
55+ Registration Method
56+ ===================
57+
58+ The ``register_mtddriver `` function provides a unified interface for
59+ registering MTD devices. Upon registration, the MTD device will automatically
60+ incorporate FTL (Flash Translation Layer) and BCH (Block-to-Character conversion)
61+ wrappers during the ``open() `` process. This automatic wrapping obviates the
62+ need for legacy registration methods ``ftl_initialize() `` and
63+ ``bchdev_register() `` in user code. Users can directly access the MTD device
64+ via standard file operations (e.g., ``open() ``, ``read() ``, ``write() ``)
65+ after registration.
66+
67+ - **Character Device Mode ** (via ``open() ``):
68+ Enables byte-oriented access with both FTL and BCH layers applied
69+ (requires ``CONFIG_BCH ``).
70+
71+ - **Block Device Mode ** (via ``open_blockdriver() ``):
72+ Presents a block interface with only the FTL layer enabled
73+
74+ The functions register_partition_with_mtd() and register_mtdpartition()
75+ are actually wrappers built on top of register_mtddriver(),
76+ and they can be used to create sub-partition devices for MTD devices.
77+
78+ In scenarios where the FTL layer is not suitable for converting MTD to a
79+ block device, alternatives like Dhara can be used instead.
80+ To register a Dhara-backed block device: Use the ``dhara_initialize() ``
81+ function, passing the underlying ``struct mtd_dev_s *dev ``
82+ as a parameter to create a Dhara block device instance. Once Dhara is
83+ initialized, register the block device using ``register_blockdriver() ``
84+ with the Dhara device's block operations: This approach bypasses the FTL
85+ layer and directly integrates Dhara's block management capabilities with the
86+ MTD device. Dhara provides features such as wear-leveling and bad block
87+ management tailored for specific use cases.
88+
89+
90+ Control FTL Behavior via Open Flags
91+ ===================================
92+
93+ The FTL layer translates MTD operations into block-device semantics
94+ while managing NAND-specific challenges (e.g., bad blocks, wear leveling).
95+ By default, FTL employs a read-modify-write cycle for writes:
96+
97+ 1. Read the entire erase block into a cache buffer.
98+ 2. Modify the target data in memory.
99+ 3. Erase the physical block.
100+ 4. Write the entire buffer back.
101+
102+ This approach ensures data consistency but introduces latency and requires
103+ sufficient RAM.
104+ To accommodate performance-sensitive applications,
105+ FTL supports the following flags during ``open() ``:
106+
107+ - **``O_DIRECT`` **:
108+ Bypasses the read-modify-write cycle, enabling direct writes to flash.
109+ Use this flag when: Write speed is critical or Sufficient RAM for caching
110+ is unavailable.
111+
112+ - **``O_SYNC`` **:
113+ Assumes blocks are pre-erased, skipping the erase step during writes.
114+ This flag only takes effect when used in conjunction with ``O_DIRECT ``.
115+
116+ The diagram below illustrates the workflow when opening an MTD device node
117+ via the ``open() `` function, highlighting how ``oflags ``
118+ (e.g., ``O_DIRECT ``, ``O_SYNC ``) are propagated through layers to control
119+ FTL behavior:
120+
121+ .. figure :: ./mtd_open_flow.png
122+ :align: center
123+ :alt: MTD Device Open Sequence with Oflags
124+
125+ *Figure 1: Sequence of opening an MTD device node and oflag propagation *
53126
54127EEPROM
55128======
0 commit comments