mtd: Microchip 23K256 and 23LCV1024 SPI SRAM
Subsystems (2)
v3 cover mtd: Microchip 23K256 and 23LCV1024 SPI SRAM
The 23LCV1024 is a 1 Mbit serial SRAM with battery backup on an SPI bus, used as non volatile scratch storage. This adds a driver for it and for the 23K256 of the same family, plus a sandbox emulation and unit tests.
The driver sits in UCLASS_MTD, the way the SPI NAND driver does, so the device is reached through the mtd command. v1 put it in UCLASS_SPI_FLASH to borrow the sf command, which meant shimming struct spi_nor fields for a device with no flash semantics.
Patch 1 prepares the MTD core for devices which have no erase function.
Patch 2 does the same for partitions on such a device, which are aligned on the write size instead of the erase size the device does not have. Without it "mtd list" divides by zero as soon as the device tree gives the RAM partitions.
Patch 3 is the driver itself, now in drivers/mtd/devices/mchp23k256.c without erase function, named after the Linux driver and with the 23K256 of the same family alongside the 23LCV1024.
Patch 4 is new in v3 and lets the imagemap test pick the SPI-NOR by the partition it needs. It takes the first MTD block device today, which the next patch takes away from it. Found by CI run:
https://dev.azure.com/hs0298/hs/_build/results?buildId=223&view=results
Patch 5 is the sandbox emulation and the tests. v2 had a patch before it that taught the sandbox SPI bus to bind an emulation from a sandbox,emul phandle. That patch is gone, mainline has the hook since commit 3b748d780a37 ("sandbox: spi: Add SPI EEPROM emulator and DM test"), so the emulation nodes sit at the root of test.dts the way the EEPROM one does.
Patch 6 is new in v3 and does for the mtd command what patch 1 does for the MTD core. Read, write and dump get through a device without an erase size, and an erase is refused with a message.
Patch 7 is new in v3 as well and moves the transfers of the driver to spi-mem, so that a controller which limits the transfer size or only offers spi-mem operations works.
The series was tested on i.MX6 and i.MX8 hardware.
This series is now based on the current head of the main branch, dfcfc4c9f78d ("Prepare v2027.01-rc1").
Azure build is clean:
https://dev.azure.com/hs0298/hs/_build/results?buildId=224&view=results
Changes in v3:
- Dropped the sentence saying nobody reads MTD_NO_ERASE, which is wrong, spi-nor sets it for SPI_NOR_NO_ERASE parts. Say instead why the check asks for erasesize and _erase (Simon)
- Mention the mtdpart.c hunk, a partition gets part_erase() only when its master has an _erase (Simon)
- Added Reviewed-by from Simon, as all comments are in, beside mtd_is_aligned_with_block_size() comment which is moved to patch "cmd: mtd: get through a device that has no erase size"
- The two alignment checks no longer rely on MTD_NO_ERASE being set correctly. An alignment of zero counts as 1 with a WARN_ON(), and the partition erase size is only taken when there is one (Simon)
- Updated the comment above the first check, which still spoke of the erase size alone (Simon)
- Added Reviewed-by from Simon
- Named after the Linux driver and moved to drivers/mtd/devices/mchp23k256.c, with CONFIG_MTD_MCHP23K256 and the driver name mchp23k256. drivers/mtd/devices/ is new in U-Boot, it is where the related parts of the family would go (Simon)
- Added the microchip,mchp23k256 compatible, 32 KiB with a two byte address, so that the name covers what the driver does. The address width comes from the per compatible data
- spi_ram_xfer() dropped, the driver calls spi_write_then_read(), which does the same two transfers (Simon)
- The message and the comment in the probe claimed the device powers up in byte operation. The data sheet (DS25156A, section 2.5) gives sequential as the default, and the Linux driver writes the MODE register all the same (Simon)
- Removed two comments in probe. The reason for selecting sequential operation is in the commit message, and mtd->_erase is set to NULL explicitly (even if that is redundant), so it is obvious that there is no erase function
- did not add Simons comment about the single 128k comment in this patch, instead moved that to a new patch:
mtd: mchp23k256: move the transfers to spi-mem
- Added Reviewed-by from Simon
- New patch. Problem found on CI run:
https://dev.azure.com/hs0298/hs/_build/results?buildId=223&view=results
The next patch gives the test device tree two MTD devices of its own, one of which took the first mtd_blk away from this test
- The patch that taught the sandbox SPI bus to bind an emulation from a sandbox,emul phandle is gone. Mainline has that since commit 3b748d780a37 ("sandbox: spi: Add SPI EEPROM emulator and DM test"), which looks the emulation up with uclass_get_device_by_phandle(). The emulation nodes therefore sit at the root of test.dts, bound by the device tree scan, and the tests no longer unbind anything by hand
- The emulation treats whatever follows the address in the first transfer as data, so it follows the data sheet rather than the way the driver splits its transfers. The test sends a write in a single transfer to cover it (Simon)
- The erase is asked for over the data that was written, so the check afterwards would see an erase that went through (Simon)
- spi_get_bus_and_cs() dropped, the emulation is looked up with the MTD device itself (Simon)
- The second partition sits at 0xa001, an offset no erase block boundary allows, and the comment says why that matters. 0xa000 was 8K aligned and proved nothing (Simon)
- The emulation powers up in sequential operation, as the data sheet says. The test puts it into byte operation and probes again to see the driver select the mode
- Added a test for the 23K256 with its own emulation instance, which is what the second compatible of the driver needs
- New patch. Patch 1 only covers mtd_erase(), the mtd command divides by the erase size before it ever gets there.
- New patch. Simon pointed out that a single spi_xfer() over the whole array fails where the SPI NAND driver would work
Changes in v2:
- New patch. The SRAM driver of patch 3 has no erase, which the old mtd_erase() would have turned into a NULL pointer jump
- New patch mtd: align a partition on the write size if the device cannot erase Testing on the board dropped for "mtd list" command:
### ERROR ### Please RESET the board ###
This is because division by erase_size, which the SPI RAM driver sets to 0. Added for this case also sandbox test.
Added comments from Simon:
- Moved from UCLASS_SPI_FLASH to UCLASS_MTD, the way the SPI NAND driver sits. The struct spi_nor shim and the dm_spi_flash_ops are gone with it, and the device is reached through the mtd command rather than sf.
- Moved from drivers/mtd/spi/ to drivers/mtd/, where the other standalone MTD drivers live.
- Dropped the erase operation. v1 filled the range with 0xff from a malloc'd buffer and claimed the bus once per 1 KB chunk. A RAM needs no erase, and patch 1 makes mtd_erase() answer -EOPNOTSUPP.
- Dropped the linux/mtd/spi-nor.h include, spi_flash.h pulled it in anyway and it advertised a NOR driver.
- sprintf() into a fixed field replaced by snprintf() into a buffer the driver owns.
- Copyright: dropped the trailing full stop and added Stefan Bigler's address.
fixed checkpatch warning:
- Added a MAINTAINERS entry, as the move out of drivers/mtd/spi/ dropped a checkpatch warning.
Added comments from Simon:
- The test drives the device through the MTD layer instead of the spi_flash uclass.
- Uses sandbox_spi_get_emul() rather than reaching into state->spi[][].
- Added a spi1 alias so that dev_seq() of the bus does not depend on probe order.
- The erase part checks for -EOPNOTSUPP now. The v1 comment about covering more than one staging step went with the erase loop.
- The emulation lives in its own node with its own compatible, which the sandbox SPI bus resolves the phandle to.
- Added the emulation and the test to the MAINTAINERS entry.
Found while Stefan Bigler tested the series on hardware:
- Added partitions to the device in the test device tree and a second test that writes through one of them, which is what "mtd list" on his board fell over.
Heiko Schocher (7):
mtd: refuse an erase on a device that cannot erase
mtd: align a partition on the write size if the device cannot erase
mtd: add driver for the Microchip 23K256 and 23LCV1024 SPI SRAM
test: boot: imagemap: pick the SPI-NOR by its partition
test: dm: add a test for the Microchip 23K256 and 23LCV1024 SPI SRAM
cmd: mtd: get through a device that has no erase size
mtd: mchp23k256: move the transfers to spi-mem
MAINTAINERS | 8 + arch/sandbox/dts/test.dts | 49 ++++ arch/sandbox/include/asm/test.h | 18 ++ cmd/mtd.c | 14 ++ configs/sandbox_defconfig | 2 + drivers/mtd/Kconfig | 2 + drivers/mtd/Makefile | 1 + drivers/mtd/devices/Kconfig | 26 +++ drivers/mtd/devices/Makefile | 4 + drivers/mtd/devices/mchp23k256.c | 220 ++++++++++++++++++ drivers/mtd/devices/mchp23k256_sandbox.c | 268 ++++++++++++++++++++++ drivers/mtd/mtdcore.c | 3 + drivers/mtd/mtdpart.c | 47 +++- drivers/spi/Kconfig | 2 +- test/boot/imagemap.c | 11 +- test/cmd/Makefile | 3 + test/cmd/mtd.c | 79 +++++++ test/dm/Makefile | 1 + test/dm/mchp23k256.c | 272 +++++++++++++++++++++++ 19 files changed, 1015 insertions(+), 15 deletions(-) create mode 100644 drivers/mtd/devices/Kconfig create mode 100644 drivers/mtd/devices/Makefile create mode 100644 drivers/mtd/devices/mchp23k256.c create mode 100644 drivers/mtd/devices/mchp23k256_sandbox.c create mode 100644 test/cmd/mtd.c create mode 100644 test/dm/mchp23k256.c
---
base-commit: dfcfc4c9f78d1743f490a497082a4ec8e058aae8
branch: spi-ram-v3
Signature · 1 line
2.55.0
PATCH v3 1/7 mtd: refuse an erase on a device that cannot erase
mtd_erase() calls mtd->_erase() without checking that the driver has one, so on a device which does not provide one it jumps to a NULL pointer.
MTD_NO_ERASE alone does not tell us whether erase is supported, as the flag means erase is unnecessary, not unavailable.
Linux guards mtd_erase() with
if (!mtd->erasesize || !master->_erase) return -ENOTSUPP;
so do the same in U-Boot, but with EOPNOTSUPP as the error code, as checkpatch asks for ("ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP").
A partition gets part_erase() only when its master has an _erase, so part_erase() cannot call through a NULL master->_erase either.
---
Changes in v3:
- Dropped the sentence saying nobody reads MTD_NO_ERASE, which is wrong, spi-nor sets it for SPI_NOR_NO_ERASE parts. Say instead why the check asks for erasesize and _erase (Simon)
- Mention the mtdpart.c hunk, a partition gets part_erase() only when its master has an _erase (Simon)
- Added Reviewed-by from Simon, as all comments are in, beside mtd_is_aligned_with_block_size() comment which is moved to patch "cmd: mtd: get through a device that has no erase size"
Changes in v2:
- New patch. The SRAM driver of patch 3 has no erase, which the old mtd_erase() would have turned into a NULL pointer jump
drivers/mtd/mtdcore.c | 3 +++ drivers/mtd/mtdpart.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/mtd/mtdcore.c +3 -0
@@ -945,6 +945,9 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);int mtd_erase (struct mtd_info *mtd,struct erase_info *instr) { +if (!mtd->erasesize || !mtd->_erase) +return -EOPNOTSUPP ; +if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)return -EINVAL ;if (!(mtd->flags &MTD_WRITEABLE ))
drivers/mtd/mtdpart.c +2 -1
@@ -654,7 +654,8 @@ static struct mtd_info *allocate_partition(struct mtd_info *master, slave->_block_isbad = part_block_isbad;if (master->_block_markbad) slave->_block_markbad = part_block_markbad; - slave->_erase = part_erase; +if (master->_erase) + slave->_erase = part_erase; slave->parent = master; slave->offset = part->offset;INIT_LIST_HEAD (&slave->partitions);
Signature · 1 line
2.55.0
PATCH v3 2/7 mtd: align a partition on the write size if the device cannot erase
In reply to Heiko Schocherallocate_partition() checks a partition offset and size against the erase size of its master. A device which cannot erase has no erase size, so the check divides by zero and "mtd list" ends in
### ERROR ### Please RESET the board ###
Align on the write size where there is no erase size, the way Linux does in allocate_partition(). The MTDPART_OFS_NXTBLK rounding divides by the erase size too and takes the same alignment.
A driver which declares neither size gets an alignment of 1 and a WARN_ON(), as a missing size must not take the boot down.
---
checkpatch reports two warnings and four checks on the two printk(KERN_WARNING"mtd: partition ... force read-only\n") lines. This patch only changes s/"erase block"/"erase/write block" in the string of the print, so ignoring the warnings.
Changes in v3:
- The two alignment checks no longer rely on MTD_NO_ERASE being set correctly. An alignment of zero counts as 1 with a WARN_ON(), and the partition erase size is only taken when there is one (Simon)
- Updated the comment above the first check, which still spoke of the erase size alone (Simon)
- Added Reviewed-by from Simon
Changes in v2:
- New patch mtd: align a partition on the write size if the device cannot erase Testing on the board dropped for "mtd list" command:
### ERROR ### Please RESET the board ###
This is because division by erase_size, which the SPI RAM driver sets to 0. Added for this case also sandbox test.
drivers/mtd/mtdpart.c | 44 +++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-)
drivers/mtd/mtdpart.c +34 -10
@@ -563,8 +563,19 @@ static struct mtd_info *allocate_partition(struct mtd_info *master,const struct mtd_partition *part, int partno, uint64_t cur_offset) { + u32 wr_alignment = (master->flags & MTD_NO_ERASE) ? master->writesize : + master->erasesize;struct mtd_info *slave; +u32 remainder;char *name; +u64 tmp; + +/* +* A master that declares neither of the two sizes leaves nothing to +* align to. Take every offset as aligned instead of dividing by zero. +*/ +if (WARN_ON (!wr_alignment)) + wr_alignment =1 ;/* allocate the partition structure */ slave =kzalloc (sizeof (*slave),GFP_KERNEL );
@@ -665,9 +676,11 @@ static struct mtd_info *allocate_partition(struct mtd_info *master, slave->offset = cur_offset;if (slave->offset ==MTDPART_OFS_NXTBLK ) { slave->offset = cur_offset; -if (mtd_mod_by_eb (cur_offset, master) !=0 ) { -/* Round up to next erasesize */ - slave->offset = (mtd_div_by_eb (cur_offset, master) +1 ) * master->erasesize; + tmp = cur_offset; + remainder =do_div (tmp, wr_alignment); +if (remainder) { +/* Round up to the next alignment boundary */ + slave->offset += wr_alignment - remainder;debug ("Moving partition %d: " "0x%012llx -> 0x%012llx\n" , partno, (unsigned long long )cur_offset, (unsigned long long )slave->offset);
@@ -731,19 +744,30 @@ static struct mtd_info *allocate_partition(struct mtd_info *master, slave->erasesize = master->erasesize; } -if ((slave->flags &MTD_WRITEABLE ) && -mtd_mod_by_eb (slave->offset, slave)) { -/* Doesn't start on a boundary of major erase size */ +/* +* The erase size of the partition may differ from the one of the +* master if the master exposes several erase regions. Align to the +* one the partition ended up with, as long as there is one. +*/ +if (!(slave->flags &MTD_NO_ERASE ) && slave->erasesize) + wr_alignment = slave->erasesize; + + tmp = slave->offset; + remainder =do_div (tmp, wr_alignment); +if ((slave->flags &MTD_WRITEABLE ) && remainder) { +/* Doesn't start on a boundary of the alignment above */ /* FIXME: Let it be writable if it is on a boundary of * _minor_ erase size though */ slave->flags &= ~MTD_WRITEABLE ; -printk (KERN_WARNING "mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n" , +printk (KERN_WARNING "mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n" , part->name); } -if ((slave->flags &MTD_WRITEABLE ) && -mtd_mod_by_eb (slave->size, slave)) { + + tmp = slave->size; + remainder =do_div (tmp, wr_alignment); +if ((slave->flags &MTD_WRITEABLE ) && remainder) { slave->flags &= ~MTD_WRITEABLE ; -printk (KERN_WARNING "mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n" , +printk (KERN_WARNING "mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n" , part->name); }
Signature · 1 line
2.55.0
PATCH v3 3/7 mtd: add driver for the Microchip 23K256 and 23LCV1024 SPI SRAM
In reply to Heiko SchocherThe 23K256 and the 23LCV1024 are a 32 KiB and a 128 KiB serial SRAM on an SPI bus, the larger one with battery backup, used as non volatile scratch storage. Register them as an MTD device of type MTD_RAM, so the mtd command reaches them. A command carries a two byte address on the smaller part and a three byte one on the larger, which is what the per compatible data holds.
Sequential operation, where a command transfers as many bytes as the master clocks, is the mode the devices power up in. Select it during probe, as nothing says which mode the device was left in by whoever talked to it before, and in byte operation a command would carry a single byte. The Linux driver does the same.
Erasing is not required for writing, so the driver sets mtd->_erase to NULL and says MTD_NO_ERASE through MTD_CAP_RAM in the MTD flags.
---
Changes in v3:
- Named after the Linux driver and moved to drivers/mtd/devices/mchp23k256.c, with CONFIG_MTD_MCHP23K256 and the driver name mchp23k256. drivers/mtd/devices/ is new in U-Boot, it is where the related parts of the family would go (Simon)
- Added the microchip,mchp23k256 compatible, 32 KiB with a two byte address, so that the name covers what the driver does. The address width comes from the per compatible data
- spi_ram_xfer() dropped, the driver calls spi_write_then_read(), which does the same two transfers (Simon)
- The message and the comment in the probe claimed the device powers up in byte operation. The data sheet (DS25156A, section 2.5) gives sequential as the default, and the Linux driver writes the MODE register all the same (Simon)
- Removed two comments in probe. The reason for selecting sequential operation is in the commit message, and mtd->_erase is set to NULL explicitly (even if that is redundant), so it is obvious that there is no erase function
- did not add Simons comment about the single 128k comment in this patch, instead moved that to a new patch:
mtd: mchp23k256: move the transfers to spi-mem
- Added Reviewed-by from Simon
Changes in v2:
Added comments from Simon:
- Moved from UCLASS_SPI_FLASH to UCLASS_MTD, the way the SPI NAND driver sits. The struct spi_nor shim and the dm_spi_flash_ops are gone with it, and the device is reached through the mtd command rather than sf.
- Moved from drivers/mtd/spi/ to drivers/mtd/, where the other standalone MTD drivers live.
- Dropped the erase operation. v1 filled the range with 0xff from a malloc'd buffer and claimed the bus once per 1 KB chunk. A RAM needs no erase, and patch 1 makes mtd_erase() answer -EOPNOTSUPP.
- Dropped the linux/mtd/spi-nor.h include, spi_flash.h pulled it in anyway and it advertised a NOR driver.
- sprintf() into a fixed field replaced by snprintf() into a buffer the driver owns.
- Copyright: dropped the trailing full stop and added Stefan Bigler's address.
fixed checkpatch warning:
- Added a MAINTAINERS entry, as the move out of drivers/mtd/spi/ dropped a checkpatch warning.
MAINTAINERS | 5 + configs/sandbox_defconfig | 1 + drivers/mtd/Kconfig | 2 + drivers/mtd/Makefile | 1 + drivers/mtd/devices/Kconfig | 17 +++ drivers/mtd/devices/Makefile | 3 + drivers/mtd/devices/mchp23k256.c | 200 +++++++++++++++++++++++++++++++ 7 files changed, 229 insertions(+) create mode 100644 drivers/mtd/devices/Kconfig create mode 100644 drivers/mtd/devices/Makefile create mode 100644 drivers/mtd/devices/mchp23k256.c
MAINTAINERS +5 -0
@@ -1663,6 +1663,11 @@ S: Maintained T: git https://git.u-boot-project.org/u-boot/custodians/u-boot-nand-flash.git F: drivers/mtd/nand/spi/ +MICROCHIP 23K256/23LCV1024 SPI SRAM +M: Heiko Schocher <[email protected]> +S: Maintained +F: drivers/mtd/devices/mchp23k256.c + SPI-NOR M: Vignesh R <[email protected]> R: Takahiro Kuwano <[email protected]>
configs/sandbox_defconfig +1 -0
@@ -265,6 +265,7 @@ CONFIG_MMC_SANDBOX=y CONFIG_MMC_SDHCI=y CONFIG_DM_MTD=y CONFIG_MTD_BLOCK=y +CONFIG_MTD_MCHP23K256=y CONFIG_MTD_RAW_NAND=y CONFIG_SYS_MAX_NAND_DEVICE=8 CONFIG_SYS_NAND_USE_FLASH_BBT=y
drivers/mtd/Kconfig +2 -0
@@ -248,6 +248,8 @@ config SYS_MAX_FLASH_BANKS_DETECT to reduce the effective number of flash bank, between 0 and CONFIG_SYS_MAX_FLASH_BANKS +source "drivers/mtd/devices/Kconfig" + source "drivers/mtd/nand/Kconfig" config SYS_NAND_MAX_OOBFREE
drivers/mtd/Makefile +1 -0
@@ -21,6 +21,7 @@ ifeq ($(CONFIG_XPL_BUILD)$(CONFIG_TPL_BUILD),) ifneq ($(mtd-y),) obj-y += mtd.o endif +obj-$(CONFIG_MTD_MCHP23K256) += devices/ obj-y += nand/ obj-y += onenand/ obj-y += spi/
drivers/mtd/devices/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0+ + +config MTD_MCHP23K256 + bool "Microchip 23K256 and 23LCV1024 SPI SRAM" + depends on DM_SPI && DM_MTD + help + Enable support for the Microchip 23K256 and 23LCV1024 serial SRAMs, + a 32 KiB and a 128 KiB SRAM on an SPI bus, the latter with battery + backup. The device is registered as an MTD device of type MTD_RAM, + so the mtd command reaches it. + + Writing needs no erase, so the driver implements none and the MTD + flags say MTD_NO_ERASE. An erase request is answered with + -EOPNOTSUPP rather than silently doing nothing. + + If unsure, say N +
drivers/mtd/devices/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0+ + +obj-$(CONFIG_MTD_MCHP23K256) += mchp23k256.o
drivers/mtd/devices/mchp23k256.c new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v3 4/7 test: boot: imagemap: pick the SPI-NOR by its partition
In reply to Heiko Schocherimagemap_test_mtd_blk() takes the first block device of type PART_TYPE_MTD and works on it as the SPI-NOR of the test device tree. Every MTD device binds an mtd_blk, so a further MTD device in the device tree can hold that place, and the test then looks for the "nor-fit" partition on a device that does not have it.
Walk the block devices for the one that carries the partition the test needs, probing each candidate on the way, which is what the single probe after the loop did for the first device.
---
Changes in v3:
- New patch. Problem found on CI run:
https://dev.azure.com/hs0298/hs/_build/results?buildId=223&view=results
The next patch gives the test device tree two MTD devices of its own, one of which took the first mtd_blk away from this test
test/boot/imagemap.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
test/boot/imagemap.c +8 -3
@@ -539,17 +539,22 @@ static int imagemap_test_mtd_blk(struct unit_test_state *uts)*/ mtd_probe_devices (); -/* Find the bound mtd_blk (a PART_TYPE_MTD block device) */ +/* +* Every MTD device of the test device tree binds an mtd_blk, so the +* partition this test needs is what tells the SPI-NOR apart. +*/ for (uclass_find_first_device (UCLASS_BLK , &dev); dev;uclass_find_next_device (&dev)) { desc =dev_get_uclass_plat (dev); -if (desc->part_type ==PART_TYPE_MTD ) { +if (desc->part_type !=PART_TYPE_MTD ||device_probe (dev)) +continue ; + +if (part_get_info_by_name (desc,"nor-fit" , &info) >=1 ) { blk = dev;break ; } }ut_assertnonnull (blk); -ut_assertok (device_probe (blk)); desc =dev_get_uclass_plat (blk);/* Write a known pattern into the "nor-fit" partition */
Signature · 1 line
2.55.0
PATCH v3 5/7 test: dm: add a test for the Microchip 23K256 and 23LCV1024 SPI SRAM
In reply to Heiko SchocherEmulate both devices on a second sandbox SPI bus and exercise the driver through the MTD layer.
The emulation implements the three operating modes, because a transfer past the 32 byte page only comes back unharmed in sequential operation, and it takes the device as the byte stream it is, so a master which splits the transfers differently from the driver has to get the same result.
The tests cover the cases which would otherwise pass unnoticed, a device left in byte operation, an erase on a device which has none, the two byte address of the smaller part, and a partition at an offset no erase block boundary allows, which divided by zero before the alignment earlier in this series.
---
checkpatch asks for a blank line after the test functions, before the DM_TEST() that registers them. But in test/dm/ all over is without a blank line, so ignore this warning.
Changes in v3:
- The patch that taught the sandbox SPI bus to bind an emulation from a sandbox,emul phandle is gone. Mainline has that since commit 3b748d780a37 ("sandbox: spi: Add SPI EEPROM emulator and DM test"), which looks the emulation up with uclass_get_device_by_phandle(). The emulation nodes therefore sit at the root of test.dts, bound by the device tree scan, and the tests no longer unbind anything by hand
- The emulation treats whatever follows the address in the first transfer as data, so it follows the data sheet rather than the way the driver splits its transfers. The test sends a write in a single transfer to cover it (Simon)
- The erase is asked for over the data that was written, so the check afterwards would see an erase that went through (Simon)
- spi_get_bus_and_cs() dropped, the emulation is looked up with the MTD device itself (Simon)
- The second partition sits at 0xa001, an offset no erase block boundary allows, and the comment says why that matters. 0xa000 was 8K aligned and proved nothing (Simon)
- The emulation powers up in sequential operation, as the data sheet says. The test puts it into byte operation and probes again to see the driver select the mode
- Added a test for the 23K256 with its own emulation instance, which is what the second compatible of the driver needs
Changes in v2:
Added comments from Simon:
- The test drives the device through the MTD layer instead of the spi_flash uclass.
- Uses sandbox_spi_get_emul() rather than reaching into state->spi[][].
- Added a spi1 alias so that dev_seq() of the bus does not depend on probe order.
- The erase part checks for -EOPNOTSUPP now. The v1 comment about covering more than one staging step went with the erase loop.
- The emulation lives in its own node with its own compatible, which the sandbox SPI bus resolves the phandle to.
- Added the emulation and the test to the MAINTAINERS entry. Found while Stefan Bigler tested the series on hardware:
- Added partitions to the device in the test device tree and a second test that writes through one of them, which is what "mtd list" on his board fell over.
MAINTAINERS | 2 + arch/sandbox/dts/test.dts | 49 +++++ arch/sandbox/include/asm/test.h | 18 ++ configs/sandbox_defconfig | 1 + drivers/mtd/devices/Kconfig | 8 + drivers/mtd/devices/Makefile | 1 + drivers/mtd/devices/mchp23k256_sandbox.c | 268 +++++++++++++++++++++++ drivers/spi/Kconfig | 2 +- test/dm/Makefile | 1 + test/dm/mchp23k256.c | 228 +++++++++++++++++++ 10 files changed, 577 insertions(+), 1 deletion(-) create mode 100644 drivers/mtd/devices/mchp23k256_sandbox.c create mode 100644 test/dm/mchp23k256.c
MAINTAINERS +2 -0
@@ -1667,6 +1667,8 @@ MICROCHIP 23K256/23LCV1024 SPI SRAM M: Heiko Schocher <[email protected]> S: Maintained F: drivers/mtd/devices/mchp23k256.c +F: drivers/mtd/devices/mchp23k256_sandbox.c +F: test/dm/mchp23k256.c SPI-NOR M: Vignesh R <[email protected]>
arch/sandbox/dts/test.dts +49 -0
61 lines not on this page · raw message
arch/sandbox/include/asm/test.h +18 -0
@@ -251,6 +251,24 @@ uint sandbox_spi_get_mode(struct udevice *dev);*/ uint sandbox_spi_get_wordlen (struct udevice *dev); +/** +* sandbox_mchp23k256_is_sequential() - check the mode of the emulated SPI SRAM +* +* @dev: the emulation device +* Return: true if the MODE register selects sequential operation +*/ +bool sandbox_mchp23k256_is_sequential (struct udevice *dev); + +/** +* sandbox_mchp23k256_force_byte_mode() - leave the emulated SRAM in byte mode +* +* The device powers up in sequential operation, so a test that wants to see +* the driver select a mode has to put the emulation into another one first. +* +* @dev: the emulation device +*/ +void sandbox_mchp23k256_force_byte_mode (struct udevice *dev); +/** * sandbox_get_pch_spi_protect() - Get the PCI SPI protection status *
configs/sandbox_defconfig +1 -0
@@ -266,6 +266,7 @@ CONFIG_MMC_SDHCI=y CONFIG_DM_MTD=y CONFIG_MTD_BLOCK=y CONFIG_MTD_MCHP23K256=y +CONFIG_MTD_MCHP23K256_SANDBOX=y CONFIG_MTD_RAW_NAND=y CONFIG_SYS_MAX_NAND_DEVICE=8 CONFIG_SYS_NAND_USE_FLASH_BBT=y
drivers/mtd/devices/Kconfig +8 -0
@@ -15,3 +15,11 @@ config MTD_MCHP23K256 If unsure, say N +config MTD_MCHP23K256_SANDBOX + bool "Sandbox emulation of the Microchip 23LCV1024" + depends on SANDBOX && MTD_MCHP23K256 + help + Emulate a 23LCV1024 on the sandbox SPI bus, so that the driver can + be exercised by the unit tests. The emulation implements all three + operating modes of the device, which is what makes it able to tell + whether the driver selects the right one.
drivers/mtd/devices/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0+ obj-$(CONFIG_MTD_MCHP23K256) += mchp23k256.o +obj-$(CONFIG_MTD_MCHP23K256_SANDBOX) += mchp23k256_sandbox.o
drivers/mtd/devices/mchp23k256_sandbox.c new file +268 -0
268 lines not on this page · raw message
drivers/spi/Kconfig +1 -1
@@ -518,7 +518,7 @@ config SANDBOX_SPI config SANDBOX_SPI_MAX_BUS int depends on SANDBOX - default 1 + default 2 config SANDBOX_SPI_MAX_CS int
test/dm/Makefile +1 -0
@@ -85,6 +85,7 @@ obj-$(CONFIG_IOMMU) += iommu.o obj-$(CONFIG_LED) += led.o obj-$(CONFIG_VIDEO_BRIDGE_LVDS_CODEC) += video_bridge.o obj-$(CONFIG_DM_MAILBOX) += mailbox.o +obj-$(CONFIG_MTD_MCHP23K256_SANDBOX) += mchp23k256.o obj-$(CONFIG_DM_MDIO) += mdio.o obj-$(CONFIG_DM_MDIO_MUX) += mdio_mux.o obj-$(CONFIG_MEMORY) += memory.o
test/dm/mchp23k256.c new file +228 -0
228 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v3 6/7 cmd: mtd: get through a device that has no erase size
In reply to Heiko Schochermtd_is_aligned_with_block_size() divides by mtd->erasesize, which a device of type MTD_RAM does not have, so "mtd read", "mtd write", "mtd dump" and "mtd erase" all end in a division by zero.
Take a missing erase size as "nothing to align to", and refuse an erase on the condition mtd_erase() refuses on, before the line announcing it divides through mtd_div_by_eb().
The test sits in test/cmd/ as the mtd command walks a global list that outlives the device tree a driver model test replaces.
---
checkpatch asks for a blank line after the test function, before the CMD_TEST() that registers it. But in test/cmd/ all over is without a blank line, so ignore this warning.
Changes in v3:
- New patch. Patch 1 only covers mtd_erase(), the mtd command divides by the erase size before it ever gets there.
MAINTAINERS | 1 + cmd/mtd.c | 14 +++++++++ test/cmd/Makefile | 3 ++ test/cmd/mtd.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 test/cmd/mtd.c
MAINTAINERS +1 -0
@@ -1668,6 +1668,7 @@ M: Heiko Schocher <[email protected]> S: Maintained F: drivers/mtd/devices/mchp23k256.c F: drivers/mtd/devices/mchp23k256_sandbox.c +F: test/cmd/mtd.c F: test/dm/mchp23k256.c SPI-NOR
cmd/mtd.c +14 -0
@@ -53,6 +53,10 @@ static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)static bool mtd_is_aligned_with_block_size (struct mtd_info *mtd,u64 size) { +/* A device with no erase blocks, a RAM say, has nothing to align to */ +if (!mtd->erasesize) +return true; +return !do_div (size, mtd->erasesize); }
@@ -642,6 +646,16 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,if (IS_ERR_OR_NULL (mtd))return CMD_RET_FAILURE ; +/* +* The same condition mtd_erase() refuses on. Every step below works +* in units of the erase size, so there is nothing to do here either. +*/ +if (!mtd->erasesize || !mtd->_erase) { +printf ("%s cannot erase\n" , mtd->name); + ret =CMD_RET_FAILURE ; + goto out_put_mtd; + } + scrub =strstr (argv[0 ],".dontskipbad" ); argc -=2 ;
test/cmd/Makefile +3 -0
@@ -28,6 +28,9 @@ obj-$(CONFIG_CMD_LOADM) += loadm.o obj-$(CONFIG_CMD_MEMINFO) += meminfo.o obj-$(CONFIG_CMD_MEMORY) += mem_copy.o obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o +ifdef CONFIG_CMD_MTD +obj-$(CONFIG_MTD_MCHP23K256_SANDBOX) += mtd.o +endif obj-$(CONFIG_CMD_PART) += part.o ifdef CONFIG_CMD_PCI obj-$(CONFIG_CMD_PCI_MPS) += pci_mps.o
test/cmd/mtd.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v3 7/7 mtd: mchp23k256: move the transfers to spi-mem
In reply to Heiko SchocherA read or a write of the whole array goes out as one transfer, which fails on a controller that limits the transfer size or only offers spi-mem operations. spi-mem answers both, as spi_mem_adjust_op_size() cuts an operation to what the controller takes and spi_mem_exec_op() claims the bus per operation, so the driver needs no explicit bus claim.
The test sets a transfer size smaller than the data length, forcing the loop to process multiple chunks.
---
checkpatch asks for a blank line after the test function, before the DM_TEST() that registers it. But in test/dm/ all over is without a blank line, so ignore this warning.
Changes in v3:
- New patch. Simon pointed out that a single spi_xfer() over the whole array fails where the SPI NAND driver would work
drivers/mtd/devices/Kconfig | 1 + drivers/mtd/devices/mchp23k256.c | 88 ++++++++++++++++++++------------ test/dm/mchp23k256.c | 44 ++++++++++++++++ 3 files changed, 99 insertions(+), 34 deletions(-)
drivers/mtd/devices/Kconfig +1 -0
@@ -3,6 +3,7 @@ config MTD_MCHP23K256 bool "Microchip 23K256 and 23LCV1024 SPI SRAM" depends on DM_SPI && DM_MTD + select SPI_MEM help Enable support for the Microchip 23K256 and 23LCV1024 serial SRAMs, a 32 KiB and a 128 KiB SRAM on an SPI bus, the latter with battery
drivers/mtd/devices/mchp23k256.c +54 -34
124 lines not on this page · raw message
test/dm/mchp23k256.c +44 -0
56 lines not on this page · raw message
Signature · 1 line
2.55.0
Hi all,
Quoted 196 lines · Heiko Schocher
The 23LCV1024 is a 1 Mbit serial SRAM with battery backup on an SPI bus, used as non volatile scratch storage. This adds a driver for it and for the 23K256 of the same family, plus a sandbox emulation and unit tests.
The driver sits in
UCLASS_MTD, the way the SPI NAND driver does, so the device is reached through the mtd command. v1 put it inUCLASS_SPI_FLASHto borrow the sf command, which meant shimming structspi_norfields for a device with no flash semantics.Patch 1 prepares the MTD core for devices which have no erase function.
Patch 2 does the same for partitions on such a device, which are aligned on the write size instead of the erase size the device does not have. Without it "mtd list" divides by zero as soon as the device tree gives the RAM partitions.
Patch 3 is the driver itself, now in
drivers/mtd/devices/mchp23k256.cwithout erase function, named after the Linux driver and with the 23K256 of the same family alongside the 23LCV1024.Patch 4 is new in v3 and lets the imagemap test pick the SPI-NOR by the partition it needs. It takes the first MTD block device today, which the next patch takes away from it. Found by CI run:
https://dev.azure.com/hs0298/hs/_build/results?buildId=223&view=resultsPatch 5 is the sandbox emulation and the tests. v2 had a patch before it that taught the sandbox SPI bus to bind an emulation from a sandbox,emul phandle. That patch is gone, mainline has the hook since commit
3b748d780a37("sandbox: spi: Add SPI EEPROM emulator and DM test"), so the emulation nodes sit at the root oftest.dtsthe way the EEPROM one does.Patch 6 is new in v3 and does for the mtd command what patch 1 does for the MTD core. Read, write and dump get through a device without an erase size, and an erase is refused with a message.
Patch 7 is new in v3 as well and moves the transfers of the driver to spi-mem, so that a controller which limits the transfer size or only offers spi-mem operations works.
The series was tested on i.MX6 and i.MX8 hardware.
This series is now based on the current head of the main branch,
dfcfc4c9f78d("Prepare v2027.01-rc1").Azure build is clean:
https://dev.azure.com/hs0298/hs/_build/results?buildId=224&view=resultsChanges in v3:
- Dropped the sentence saying nobody readsMTD_NO_ERASE, which is wrong, spi-nor sets it forSPI_NOR_NO_ERASEparts. Say instead why the check asks for erasesize and_erase(Simon)
- Mention themtdpart.chunk, a partition getspart_erase()only when its master has an_erase(Simon)
- Added Reviewed-by from Simon, as all comments are in, besidemtd_is_aligned_with_block_size()comment which is moved to patch "cmd: mtd: get through a device that has no erase size"
- The two alignment checks no longer rely onMTD_NO_ERASEbeing set correctly. An alignment of zero counts as 1 with aWARN_ON(), and the partition erase size is only taken when there is one (Simon)
- Updated the comment above the first check, which still spoke of the erase size alone (Simon)
- Added Reviewed-by from Simon
- Named after the Linux driver and moved todrivers/mtd/devices/mchp23k256.c, withCONFIG_MTD_MCHP23K256and the driver name mchp23k256.drivers/mtd/devices/is new in U-Boot, it is where the related parts of the family would go (Simon)
- Added the microchip,mchp23k256 compatible, 32 KiB with a two byte address, so that the name covers what the driver does. The address width comes from the per compatible data
-spi_ram_xfer()dropped, the driver callsspi_write_then_read(), which does the same two transfers (Simon)
- The message and the comment in the probe claimed the device powers up in byte operation. The data sheet (DS25156A, section 2.5) gives sequential as the default, and the Linux driver writes the MODE register all the same (Simon)
- Removed two comments in probe. The reason for selecting sequential operation is in the commit message, andmtd->_eraseis set to NULL explicitly (even if that is redundant), so it is obvious that there is no erase function
- did not add Simons comment about the single 128k comment in this patch, instead moved that to a new patch:
mtd: mchp23k256: move the transfers to spi-mem
- Added Reviewed-by from Simon
- New patch. Problem found on CI run:
https://dev.azure.com/hs0298/hs/_build/results?buildId=223&view=results
The next patch gives the test device tree two MTD devices of its own, one of which took the firstmtd_blkaway from this test
- The patch that taught the sandbox SPI bus to bind an emulation from a sandbox,emul phandle is gone. Mainline has that since commit3b748d780a37("sandbox: spi: Add SPI EEPROM emulator and DM test"), which looks the emulation up withuclass_get_device_by_phandle(). The emulation nodes therefore sit at the root oftest.dts, bound by the device tree scan, and the tests no longer unbind anything by hand
- The emulation treats whatever follows the address in the first transfer as data, so it follows the data sheet rather than the way the driver splits its transfers. The test sends a write in a single transfer to cover it (Simon)
- The erase is asked for over the data that was written, so the check afterwards would see an erase that went through (Simon)
-spi_get_bus_and_cs()dropped, the emulation is looked up with the MTD device itself (Simon)
- The second partition sits at0xa001, an offset no erase block boundary allows, and the comment says why that matters.0xa000was 8K aligned and proved nothing (Simon)
- The emulation powers up in sequential operation, as the data sheet says. The test puts it into byte operation and probes again to see the driver select the mode
- Added a test for the 23K256 with its own emulation instance, which is what the second compatible of the driver needs
- New patch. Patch 1 only coversmtd_erase(), the mtd command divides by the erase size before it ever gets there.
- New patch. Simon pointed out that a singlespi_xfer()over the whole array fails where the SPI NAND driver would workChanges in v2:
- New patch. The SRAM driver of patch 3 has no erase, which the oldmtd_erase()would have turned into a NULL pointer jump
- New patch mtd: align a partition on the write size if the device cannot erase Testing on the board dropped for "mtd list" command:
### ERROR ### Please RESET the board ###
This is because division byerase_size, which the SPI RAM driver sets to 0. Added for this case also sandbox test.
Added comments from Simon:
- Moved fromUCLASS_SPI_FLASHtoUCLASS_MTD, the way the SPI NAND driver sits. The structspi_norshim and thedm_spi_flash_opsare gone with it, and the device is reached through the mtd command rather than sf.
- Moved fromdrivers/mtd/spi/todrivers/mtd/, where the other standalone MTD drivers live.
- Dropped the erase operation. v1 filled the range with0xfffrom a malloc'd buffer and claimed the bus once per 1 KB chunk. A RAM needs no erase, and patch 1 makesmtd_erase()answer-EOPNOTSUPP.
- Dropped thelinux/mtd/spi-nor.hinclude,spi_flash.hpulled it in anyway and it advertised a NOR driver.
-sprintf()into a fixed field replaced bysnprintf()into a buffer the driver owns.
- Copyright: dropped the trailing full stop and added Stefan Bigler's address.
fixed checkpatch warning:
- Added aMAINTAINERSentry, as the move out ofdrivers/mtd/spi/dropped a checkpatch warning.
Added comments from Simon:
- The test drives the device through the MTD layer instead of thespi_flashuclass.
- Usessandbox_spi_get_emul()rather than reaching into state->spi[][].
- Added a spi1 alias so thatdev_seq()of the bus does not depend on probe order.
- The erase part checks for-EOPNOTSUPPnow. The v1 comment about covering more than one staging step went with the erase loop.
- The emulation lives in its own node with its own compatible, which the sandbox SPI bus resolves the phandle to.
- Added the emulation and the test to theMAINTAINERSentry.
Found while Stefan Bigler tested the series on hardware:
- Added partitions to the device in the test device tree and a second test that writes through one of them, which is what "mtd list" on his board fell over.Heiko Schocher (7):
mtd: refuse an erase on a device that cannot erase
mtd: align a partition on the write size if the device cannot erase
mtd: add driver for the Microchip 23K256 and 23LCV1024 SPI SRAM
test: boot: imagemap: pick the SPI-NOR by its partition
test: dm: add a test for the Microchip 23K256 and 23LCV1024 SPI SRAM
cmd: mtd: get through a device that has no erase size
mtd: mchp23k256: move the transfers to spi-memMAINTAINERS | 8 + arch/sandbox/dts/test.dts | 49 ++++ arch/sandbox/include/asm/test.h | 18 ++ cmd/mtd.c | 14 ++ configs/sandbox_defconfig | 2 + drivers/mtd/Kconfig | 2 + drivers/mtd/Makefile | 1 + drivers/mtd/devices/Kconfig | 26 +++ drivers/mtd/devices/Makefile | 4 + drivers/mtd/devices/mchp23k256.c | 220 ++++++++++++++++++ drivers/mtd/devices/mchp23k256_sandbox.c | 268 ++++++++++++++++++++++ drivers/mtd/mtdcore.c | 3 + drivers/mtd/mtdpart.c | 47 +++- drivers/spi/Kconfig | 2 +- test/boot/imagemap.c | 11 +- test/cmd/Makefile | 3 + test/cmd/mtd.c | 79 +++++++ test/dm/Makefile | 1 + test/dm/mchp23k256.c | 272 +++++++++++++++++++++++ 19 files changed, 1015 insertions(+), 15 deletions(-)create mode 100644
drivers/mtd/devices/Kconfigcreate mode 100644drivers/mtd/devices/Makefilecreate mode 100644drivers/mtd/devices/mchp23k256.ccreate mode 100644drivers/mtd/devices/mchp23k256_sandbox.ccreate mode 100644test/cmd/mtd.c
create mode 100644test/dm/mchp23k256.c
Quoted 4 lines
---
base-commit:dfcfc4c9f78d1743f490a497082a4ec8e058aae8
branch: spi-ram-v3
gentle ping!
Any issues with this series?
Thanks
bye,
Heiko
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic