Commit Graph

691 Commits

Author SHA1 Message Date
Michal Lenc
014f22b1c2 drivers/mtd/w25.c: ensure the device is not in power down mode
If power down mode is set, trying to read ID ends in an infinite loop
because w25_waitwritecomplete never returns as status register never
signalizes write complete. Therefore ensure the device is not in a
power down mode before trying to read from it.

This can be an issue if the board is trying to check for more NOR
memories on one SPI bus and one chip select. For example GD25 driver
returns the memory to power down state after read id is finished,
therefore board initialization is stuck in an infinite loop if it first
checks for GD25 and then fallbacks to W25.

The commit fixes the possible issue by ensuring W25 is brought back
to normal operation mode before trying to obtain the manufacturer ID.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2026-03-19 16:23:45 -04:00
Neil Berkman
c1c6e26395 drivers/mtd/ftl: bypass rwbuffer when O_DIRECT is set
ftl_write() unconditionally routes writes through rwb_write() when
FTL_HAVE_RWBUFFER is defined, ignoring the O_DIRECT flag stored in
dev->oflags. This means callers that open with O_DIRECT expecting
unbuffered writes still get their data buffered in RAM.

The O_DIRECT flag is already checked in ftl_flush() to select
between direct and read-modify-write paths, but the buffering
decision in ftl_write() never consults it.

Add O_DIRECT checks in both ftl_write() and ftl_read() to bypass
rwbuffer entirely when the flag is set. For writes, go directly to
ftl_flush() and normalize the return value to match the block
driver convention (nsectors on success). For reads, go directly to
ftl_reload(), bypassing any read-ahead cache.

This gives O_DIRECT callers a complete bypass of the rwbuffer layer
for both reads and writes, matching the documented contract.

Signed-off-by: Neil Berkman <neil@xuku.com>
2026-03-18 13:05:20 +01:00
zhaoxingyu1
2061ef5d2b mtdconfig/nvs: clean up block without valid data during GC
Invalid data is cleared during garbage collection rather than
deleted during initialization, thus accelerating the
initialization speed.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
dd60589895 mtdconfig/nvs: add key check when gc
When key string found corrupted during garbage collection,
then remove key-value pair from the flash.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
9a3f2003df mtdconfig/nvs: add dirty data clean ablility
Remove dirty data to ensure sufficient available
storage space in the flash memory.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
84a16e6842 mtdconfig/nvs: fix -Werror=unused-variable
when enable MTD_CONFIG_BUFFER_SIZE,
1669:10: error: unused variable 'ate_size' [-Werror=unused-variable]
1669 |   size_t ate_size = nvs_ate_size(fs);
2160:10: error: unused variable 'ate_size' [-Werror=unused-variable]
2160 |   size_t ate_size = nvs_ate_size(fs);

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
1b587db667 mtdconfig/nvs: use a fixed size buffer instead of vla
use a fixed size buffer on stack instead of vla,
When it needs to pass misra-c check

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
2e4ce22774 mtd_config/nvs: add data crc8
The purpose is to reduce the conflict rate of ate CRC and
verify the data

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
59f05be525 mtd_config/nvs: keep offset increasing during ate traversal
Enhance the robustness of ATE traversal.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
73de239ad2 mtdconfig/nvs: return 0 when delete a non-exist kv
Deleting a non-existent key-value pair is deemed a successful deletion.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
4b7b56a096 mtd/nvs: Fix the issue of compilation failure using the tasking compiler
the tasking compiler reports the error: expression must be constant
in line 510

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-29 14:56:19 +08:00
zhaoxingyu1
0e1dd46f3b mtd/mtd_config: add mtdconfig_register_by_path/mtdconfig_unregister_by_path api
mtd_config/mtd_config_nvs should all support
mtdconfig_register_by_path/mtdconfig_unregister_by_path api

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-26 10:52:51 +08:00
zhaoxingyu1
e4a73edec8 mtdconfig: lomtdconfig device change to depends on !MTD_CONFIG_NONE
The structure of mtd_config in Kconfig has changed.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-26 10:52:51 +08:00
zhaoxingyu1
273222c06e mtdconfig: modify mtdconfig kconfig
change filename  mtd/mtd_config_fs.c to mtd/mtd_config_nvs.c
and optimize the configuration related to mtdconfig

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-26 10:52:51 +08:00
guohao15
5e117fda60 mtdconfig: support ram_mtdconfig device && lomtdconfig device
For nvs test in qemu

Signed-off-by: guohao15 <guohao15@xiaomi.com>
2026-01-22 23:18:26 +08:00
guohao15
0af8d03d88 mtd/nvs: add nvs cache buffer
improve the speed of finding the target ate

Signed-off-by: guohao15 <guohao15@xiaomi.com>
2026-01-22 19:56:28 +08:00
jingfei
9483df3571 driver/cfi-flash: Add a config option for the page size of CFI flash.
We have added a defconfig configuration to set the page size of the flash.
According to the CFI protocol, the flash supports a single write operation
with a data volume ranging from byte size up to the maximum number of bytes.
However, the MTD device structure requires defining a page size, which serves
as the minimum write unit of the flash. Hence, this configuration is introduced.
Any page size within the range of 1 to the maximum number of bytes is considered
valid.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2026-01-16 20:38:26 +08:00
zhaoxingyu1
c4cf7ead30 bugfix: reading expired content need in expired range
By obtaining the erase value to set the nvs special ID
instead of using a fixed value

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-16 20:35:34 +08:00
zhaoxingyu1
61f4bd522c mtd/nvs: rename MTD_BLOCKSIZE_MULTIPLE
rename MTD_BLOCKSIZE_MULTIPLE to
CONFIG_MTD_CONFIG_BLOCKSIZE_MULTIPLE

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-16 20:35:34 +08:00
Xiang Xiao
23e1066322 mtd/mtd_config_fs: Add NVS_ prefix to ADDR_XXX
and rename ret to rc for keeping the consistency with other macros

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-01-16 20:35:34 +08:00
zhaoxingyu1
3fa0844da9 mtd/nvs: remove macro NVS_SPECIAL_ATE_ID
By obtaining the erase value to set the nvs
special ID instead of using a fixed value

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-16 20:35:34 +08:00
zhaoxingyu1
525a27b859 mtd/nvs: compatible with bread/bwrite
add MTD_BREAD/MTD_BWRITE in nvs when
CONFIG_MTD_BYTE_WRITE is n

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-16 20:35:34 +08:00
zhaoxingyu1
2274f1aeb3 mtd/nvs: remove CONFIG_MTD_WRITE_ALIGN_SIZE kconfig
remove CONFIG_MTD_WRITE_ALIGN_SIZE kconfig instead of
using geo.blocksize directly

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
2026-01-16 20:35:34 +08:00
jingfei
36e5752205 ftl:optimize ftl's MTD_ERASE return value check
the MTD ERASE interface has inconsistent return values.
Some implementations return the number of erased sectors,
while others return OK (0). It is currently recommended
to uniformly treat ERASE success as OK. Therefore, the
logic for judging the return value of MTD_ERASE in the
FTL erase interface should be changed to check if it is
greater than 0.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2026-01-14 09:56:04 +08:00
jingfei
3559e97659 mtd/parition:MTD partiton check whether the erase interface exist
Since there is a storage device like RRAM that doesn't
require erasing, the MTD erase function may not exist.
Here, we should first check whether the erase interface
exists before performing the erasing operation.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2026-01-14 09:56:04 +08:00
jingfei
e8fbc997fa ftl:adjust ftl for RRAM/MRAM
Accessing RRAM/MRAM from the FTL interface can
cause errors because RRAM lacks an erase interface.
To make RRAM/MRAM compatible with FTL, the FTL layer
erase interface needs to be modified.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2026-01-14 09:56:04 +08:00
jingfei
152ea6c4bd ftl:remove erase buffer for mtd device without erase interface
When there's no erase function for mtd, we decide if we
need erase_buffer based on this:

1. If we've got bad block marking, we still need the erase buffer.
   If a write fails, it's used to read back the entire bad block's
   contents before writing to a new block.
2. If we don't have bad block marking, the erase buffer isn't
   needed and can be skipped.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2026-01-14 09:56:04 +08:00
dongjiuzhu1
0a381a3ff7 driver/cfi: fix write failed issue for unalign length with bankwidth
1. In cfi_write_unalign(): Add additional check to ensure nbytes is
   at least bankwidth size before skipping unaligned start handling.
   This prevents incorrect behavior when writing small amounts of
   data that are less than bankwidth.

2. In cfi_write(): Align down the write size to bankwidth boundary
   when the remaining nbytes is less than the buffer write size.
   This ensures the buffer write operation always works with properly
   aligned data size, preventing write failures.))

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-12-31 02:36:01 +08:00
zhengyu9
491ce83508 drivers/mtd: add MTDIOC_ISBAD ioctl
add an ioctl item MTDIOC_ISBAD
used for flashtool command to check bad blocks

Signed-off-by: zhengyu9 <zhengyu9@xiaomi.com>
2025-12-23 09:26:34 -03:00
Julian Oes
7e46d282eb drivers/mtd: Add W25N SPI NAND flash driver
Some checks failed
Build Documentation / build-html (push) Has been cancelled
Add MTD driver for Winbond W25N series SPI NAND flash.

Currently supports W25N01GV (1Gbit/128MB).

Features:
- Standard SPI interface with configurable frequency (up to 104 MHz)
- Hardware ECC enabled by default
- Block erase with sleep-based wait (releases SPI bus)
- Page read/write with busy-wait for fast operations

Limitations:
- No bad block management (BBM). Factory bad blocks and runtime bad
  blocks are not tracked.
- No bad block table (BBT) scanning at initialization.
- No Quad SPI support (standard SPI only).
- No access to spare area (64 bytes/page) or OTP region.

Tested using:
- Board with STM32H743
- W25N01GV on SPI at 96 MHz with DMA enabled
- Flash mounted using littlefs
- Tested using sdbench:
  - Sequential write speed of 1760 KB/s
  - Sequential read speed of 4900 KB/s

Signed-off-by: Julian Oes <julian@oes.ch>
2025-12-19 10:00:33 +08:00
Antoine Juckler
d5633f75a8 mtd/at25ee: Use eeprom/spi_xx25xx internally
In order to reduce code duplication, use the eeprom/spi_xx25xx
driver within mtd/at25ee.

The eeprom/xxx.h includes have been merged into eeprom/eeprom.h, to
provide a common include file like mtd/mtd.h.

Signed-off-by: Antoine Juckler <6445757+ajuckler@users.noreply.github.com>
2025-12-17 19:03:54 +01:00
Michal Lenc
edf3cc55a5 drivers/mtd/w25: support custom SPI transfers delay
This commit adds SPI_SETDELAY interface if CONFIG_SPI_DELAY_CONTROL
option is set. This allows to set custom delay between SPI transfers,
chip selects and so on.

Default values are set to 0. W25 SPI NOR flash works with them and
I haven't found any other values in the datasheet.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-11-27 22:27:40 +08:00
Antoine Juckler
81550800d4 drivers/mtd/at25ee: Multi-device SPI bus support
Adds a device ID to at25ee_initialize, which is stored in the
at25ee_dev_s structure. This ID is used when calling SPI_SELECT.

The implementation is based on the handling of the chip select in
mtd/ramtron.c

This change is NOT backward compatible: the function signature has changed

Signed-off-by: Antoine Juckler <6445757+ajuckler@users.noreply.github.com>
2025-11-18 09:37:07 -03:00
Antoine Juckler
d996c2e758 mtd/at25ee.c: Fix write check condition
Some checks failed
Build Documentation / build-html (push) Has been cancelled
Fix writing till the very last byte of the memory

Signed-off-by: Antoine Juckler <6445757+ajuckler@users.noreply.github.com>
2025-11-13 08:57:52 -03:00
Antoine Juckler
934dfdf645 drivers/eeprom: Retrieve the geometry
Add EEPIOC_GEOMETRY IOCTL command to retrieve the
EEPROM geometry.

Signed-off-by: Antoine Juckler <6445757+ajuckler@users.noreply.github.com>
2025-11-13 08:57:52 -03:00
chao an
87f134cfaa sched/sleep: replace all Signal-based sleep implement to Scheduled sleep
Nuttx currently has 2 types of sleep interfaces:

1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
Weaknesses:
a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.

2. Busy sleep: up_mdelay() / up_udelay()
Weaknesses:
a. Does not actively trigger scheduling, occupy the CPU loading.

3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
Strengths:
a. Does not depend on the signal framework.
b. Tick-based, without additional computational overhead.

Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.

(This PR also aims to make signals optional, further reducing the code size of Nuttx.)

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-17 14:05:02 +08:00
simbit18
4442cb7826 drivers/mtd/CMakeLists.txt: Aligned Cmake with Make
Add:
- GD55 QSPI NOR Flash #15523
- nvblk  #16789
- virtual NAND Flash device simulator #11806

Signed-off-by: simbit18 <simbit18@gmail.com>
2025-10-09 08:31:01 +08:00
wangjianyu3
23e5e1b86e mtd/nvs: Save events if not waited
This patch will report events in the following scenarios:

1. Events that have changed but not been waited for before being added to
   the interest list.
2. Events that occur after `epoll_wait()` returns and before it is called
   again.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-09-10 10:54:39 -03:00
wangjianyu3
7e5c970cf2 mtd/nvs: Trigger POLLPRI on config change
Report the POLLPRI event if any configuration is updated.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-09-05 09:33:37 +08:00
Laczen JMS
c506c25f19 drivers/mtd: introduce nvblk
NVBLK provides a block device that operates on top of a non volatile
memory (as a mtd device) that enables wear levelling for non volatile
memory. It's operation is similar to the dhara wear levelling library,
but nvblk is meant to be used on smaller (nor, mram, rram) memory.

I am also the author and maintainer of the nvblk library.

A block device can be created during startup by using:
```
nvblk_initialize(0, mtd, CONFIG_MTD_NVBLK_DEFAULT_LBS,
                         CONFIG_MTD_NVBLK_DEFAULT_IOBS,
                         CONFIG_MTD_NVBLK_DEFAULT_SPEB);
```
and a fat filesystem on top of this as:
```
nsh> mkfatfs /dev/mtdblock0
nsh> mount -t vfat /dev/mtdblock0 /mnt
```
this fat filesystem can then be used:
```
nsh> echo "test" > /mnt/test.txt
nsh> cat test.txt
test
nsh>
```

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
2025-08-06 01:53:56 +08:00
jingfei
c3e87dd3d1 drivers/fs: Control the behavior of FTL by passing oflags during the open process.
To save more space (equivalent to the size of one erase sector of
MTD device) and to achieve faster read and write speeds, a method
for direct writing was introduced at the FTL layer.
This can be accomplished simply by using the following oflags during
the open operation:

1. O_DIRECT. when this flag is passed in, ftl internally uses
   the direct write strategy and no read cache is used in ftl;
   otherwise, each write will be executed with the minimum
   granularity of flash erase sector size which means a
   "sector read back - erase sector - write sector" operation
   is performed by using a read cache buffer in heap.

2. O_SYNC. When this flag is passed in, we assume that the
   flash has been erased in advance and no erasure operation
   will be performed internally within ftl. O_SYNC will take
   effect only when both O_DIRECT and O_SYNC are passed in
   simultaneously.

3. For uniformity, we remapped the mount flag in mount.h and
   unified it with the open flag in fcntl.h. The repetitive
   parts of their definitions were reused, and the remaining
   part of the mount flag redefine to the unused bit of open
   flags.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2025-07-16 14:11:41 +08:00
jingfei
a6fdbc538b Revert "ftl: should pre-allocate eblock for car case"
This reverts commit 20fcdcf905f279a9c4527be399a90590f458db1f.
The reason is that the erase buffer isn't always used in most cases.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2025-07-16 14:11:41 +08:00
chao an
2ff44978c2 drivers/mtd: fix compile warning
mtd/mtd_rwbuffer.c:42:
mtd/mtd_rwbuffer.c: In function 'mtd_erase':
mtd/mtd_rwbuffer.c:189:9: warning: format '%zx' expects argument of type 'size_t', but argument 3 has type 'long long int' [-Wformat=]
  189 |   finfo("block: %08zx nsectors: %zu\n",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190 |         (intmax_t)block, nsectors);
      |         ~~~~~~~~~~~~~~~
      |         |
      |         long long int
mtd/mtd_rwbuffer.c:189:21: note: format string is defined here
  189 |   finfo("block: %08zx nsectors: %zu\n",
      |                 ~~~~^
      |                     |
      |                     unsigned int
      |                 %08llx
mtd/mtd_rwbuffer.c: In function 'mtd_ioctl':
mtd/mtd_rwbuffer.c:298:21: warning: format '%d' expects argument of type 'int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'} [-Wformat=]
  298 |               finfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  299 |                     geo->blocksize, geo->erasesize, geo->neraseblocks);
      |                     ~~~~~~~~~~~~~~
      |                        |
      |                        uint32_t {aka long unsigned int}
...

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-06-03 17:33:14 +08:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
zhangshoukui
0550355c92 drivers/mtd/mtd_cfi: Add MTDIOC_ERASESTATE
We need to give a default erase value when we register cfi flash for mtd devices
he pseudo-code is as follows:
register_cfi_driver(xxx)
find_mtddriver("xxx", &inode);
mtdconfig_register(inode->u.i_mtd);

https://github.com/Zhangshoukui/nuttx/blob/master/drivers/mtd/mtd_config.c#L1775C28-L1775C45

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
2025-03-28 19:19:35 +08:00
Tim Hardisty
3ad4ae503a drivers/mtd/gd55.c: fix uninitialised variables.
These caused runtime errors and compiler warnings.

Signed-off-by: Tim Hardisty <timh@jti.uk.com>
2025-03-27 02:16:48 +08:00
stbenn
c4a84e30b7 driver/mtd: ramtron multi-device spi bus support
Adds a device ID to ramtron_initialize, which is stored in the ramtron_dev_s structure.
This ID is used when calling SPI_SELECT to board specific logic to allow chip select on the SPI bus.

This change is NOT backwards compatible, as it changes the function signature of ramtron_initialize.

This implementation is based on the handling of chip select in nuttx/drivers/mtd/sst26.c:sst26_initialize_spi().

Additional Changes:
  - Add MB85RS64V to ramtron supported parts list.
2025-03-26 19:26:01 +08:00
Javier Casas
e0b221016f driver/mtd/at45db.c: write to page without built-in erase
Using the FTL driver over a MTD flash device, when writing to flash, eventually the ftl_flush function is called and it does an erase (MTD_ERASE) and then the write (MTD_BWRITE). Currently, the at45db driver (at45db.c), uses a write command 0x82 ("Main Memory Page Program through Buffer 1 with Built-In Erase") that also performs a built-in erase before the write. In summary, each time a write to flash is performed, the page is erased twice before it is written, first in the FTL driver and then in the MTD driver.

This PR is to change the page writes to not use that built-in command.

Signed-off-by: Javier Casas <javiercasas@geotab.com>
2025-03-26 10:29:12 +08:00
Michal Lenc
99c7b64072 mtd/w25q: ensure the correct behavior if erase sector fails
Function w25qxxxjv_erase wasn't correctly handling an error in
w25qxxxjv_erase_sector call and was returning success even on failure.
Moreover this change does not immediately return EBUSY but waits for
the previous operation to finish. If the timeout is significant (more
than erase time of the entire flash), then it returns EBUSY.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-02-13 10:02:06 +08:00
zhengyu9
2c4285d9ac mtd/mtd_cfi: cfi flash bind to mtd interface
Bind cfi flash driver with the existing mtd interface

Signed-off-by: zhengyu9 <zhengyu9@xiaomi.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-02-11 17:23:44 +08:00