mtd: spi: Microchip 23LCV1024 SPI SRAM
Subsystems (2)
cover mtd: spi: Microchip 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, plus a sandbox emulation and a unit test.
The emulation implements all three operating modes of the device and advances the address counter as the data sheet describes for each of them, so the test can tell whether the driver puts the device into the mode it needs. Reaching the emulation means going through the hook of the sandbox SPI bus, which so far bound the SPI NOR flash emulator for every slave, hence the second patch.
Patch 2 and 3 touch sandbox and the tests not the SPI NOR subsystem.
Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=213&view=results
is fine.
Heiko Schocher (2):
sandbox: spi: let a slave name the driver that emulates it
test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
Stefan Bigler (1):
mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
arch/sandbox/dts/test.dts | 14 ++ arch/sandbox/include/asm/test.h | 8 + configs/sandbox_defconfig | 2 + drivers/mtd/spi/Kconfig | 20 +++ drivers/mtd/spi/Makefile | 2 + drivers/mtd/spi/sandbox.c | 25 ++- drivers/mtd/spi/spi_ram.c | 280 ++++++++++++++++++++++++++++++ drivers/mtd/spi/spi_ram_sandbox.c | 189 ++++++++++++++++++++ drivers/spi/Kconfig | 2 +- test/dm/Makefile | 1 + test/dm/spi_ram.c | 96 ++++++++++ 11 files changed, 636 insertions(+), 3 deletions(-) create mode 100644 drivers/mtd/spi/spi_ram.c create mode 100644 drivers/mtd/spi/spi_ram_sandbox.c create mode 100644 test/dm/spi_ram.c
---
base-commit: 75df64103873fac09642860cedadaffb8cf6b2c7
branch: spi-ram-v1
Signature · 1 line
2.55.0
PATCH 1/3 mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
From: Stefan Bigler <[email protected]>
The 23LCV1024 is a 1 Mbit serial SRAM with battery backup, sitting on an SPI bus and used as non volatile scratch storage. Bind it to the spi_flash uclass so that the sf command reaches it, and register it as an MTD device of type MTD_RAM, so mtd command can be used.
The device powers up in byte operation, where every command carries exactly one data byte. Select sequential operation once during probe, as Linux does in drivers/mtd/devices/mchp23k256.c, so that a read or a write of any length is served by a single command.
A RAM needs no erase before a write, but a caller that asks for one expects the area to read back the way an erased flash would, so both erase operations fill it with 0xff, which is what the sandbox flash emulation leaves behind as well. Leaving the operation out is no option either, because the uclass calls it without checking for it first.
---
Two points to discuss.
The erase operation fills with 0xff. A RAM needs no erase at all, so the body could as well be a plain "return 0", and 0x00 would be an equally defensible fill value.
I decided to use 0xff because that is what a caller of the spi_flash uclass gets from a NOR flash, and what the sandbox flash emulation writes. The operation itself cannot be left out as spi_flash_erase_dm() calls ops->erase without checking it for NULL.
The device has no erase blocks, so erasesize is 1. do_mtd_erase() calls mtd_erase() once per erasesize, which turns "mtd erase ram0" over the whole device into 131072 calls, each writing a single byte over SPI.
Through "sf erase" the same range is a single call and gets chunked inside the driver. Raising erasesize would cure the former at the price of demanding an alignment that the hardware does not have.
configs/sandbox_defconfig | 1 + drivers/mtd/spi/Kconfig | 11 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram.c | 280 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 293 insertions(+) create mode 100644 drivers/mtd/spi/spi_ram.c
configs/sandbox_defconfig +1 -0
@@ -276,6 +276,7 @@ CONFIG_SPI_FLASH_SPANSION=y CONFIG_SPI_FLASH_STMICRO=y CONFIG_SPI_FLASH_SST=y CONFIG_SPI_FLASH_WINBOND=y +CONFIG_SPI_RAM=y CONFIG_NVMXIP_QSPI=y CONFIG_MULTIPLEXER=y CONFIG_MUX_MMIO=y
drivers/mtd/spi/Kconfig +11 -0
@@ -248,6 +248,17 @@ config SPI_FLASH_ZBIT endif +config SPI_RAM + bool "Microchip 23LCV1024 SPI SRAM" + depends on DM_SPI_FLASH + help + Enable support for the Microchip 23LCV1024 serial SRAM, a 1 Mbit + SRAM with battery backup on an SPI bus. The device is registered + with the spi_flash uclass, so it is reachable through the sf + command, and, with SPI_FLASH_MTD enabled, as an MTD device. + + If unsure, say N + config SPI_FLASH_USE_4K_SECTORS bool "Use small 4096 B erase sectors" depends on SPI_FLASH
drivers/mtd/spi/Makefile +1 -0
@@ -18,6 +18,7 @@ endif obj-$(CONFIG_SPI_FLASH) += spi-nor.o obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o +obj-$(CONFIG_SPI_RAM) += spi_ram.o obj-$(CONFIG_$(PHASE_)SPI_FLASH_MTD) += sf_mtd.o obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o obj-$(CONFIG_$(PHASE_)BOOTDEV_SPI_FLASH) += sf_bootdev.o
drivers/mtd/spi/spi_ram.c new file +280 -0
280 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v1 1/3] mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
Hi Stefan,
Quoted 6 lines · Heiko Schocher
mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
The 23LCV1024 is a 1 Mbit serial SRAM with battery backup, sitting on an SPI bus and used as non volatile scratch storage. Bind it to the
spi_flashuclass so that the sf command reaches it, and register it as an MTD device of typeMTD_RAM, so mtd command can be used.
Is UCLASS_SPI_FLASH really the right home for a device with no flash semantics? The uclass allocates a struct spi_nor for every instance and every user of it (sf command, bootdev, MTD helpers) assumes NOR behaviour. It works because you shim the fields in, but it feels wrong. Have you considered driving it purely through UCLASS_MTD, or adding a small UCLASS_SPI_RAM with the same ops? What do you think?
Quoted 25 lines
The device powers up in byte operation, where every command carries exactly one data byte. Select sequential operation once during probe, as Linux does in
drivers/mtd/devices/mchp23k256.c, so that a read or a write of any length is served by a single command.A RAM needs no erase before a write, but a caller that asks for one expects the area to read back the way an erased flash would, so both erase operations fill it with
0xff, which is what the sandbox flash emulation leaves behind as well. Leaving the operation out is no option either, because the uclass calls it without checking for it first.Signed-off-by Stefan Bigler <[email protected]>Signed-off-by Heiko Schocher <[email protected]>configs/sandbox_defconfig | 1 +
drivers/mtd/spi/Kconfig | 11 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram.c | 280 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 293 insertions(+)drivers/mtd/spi/spi_ram.c +2
-0@@ -0,0 +1,280 @@ +#include <dm.h> +#include <dm/device_compat.h>
drivers/mtd/spi/spi_ram.c line 12
Quoted 10 lines
drivers/mtd/spi/spi_ram.c +10
-0+#include <log.h> +#include <malloc.h> +#include <spi.h> +#include <spi_flash.h> +#include <vsprintf.h> +#include <linux/bitops.h> +#include <linux/kernel.h> +#include <linux/mtd/mtd.h> +#include <linux/mtd/spi-nor.h> +#include <linux/sizes.h>
The linux/mtd/spi-nor.h include is redundant — spi_flash.h already pulls it in — and it advertises that this driver is a NOR driver, which it is not. Please drop it.
drivers/mtd/spi/spi_ram.c line 7
Quoted 9 lines
drivers/mtd/spi/spi_ram.c +7
-0@@ -0,0 +1,280 @@ +static int spi_ram_erase (struct udevice *dev,u32 offset,size_t len) +{ +size_t step =min_t (size_t, len,SPI_RAM_ERASE_STEP ); +u8 *buf; +int ret =0 ; + + buf =malloc (step);
I wish we could avoid malloc() here?
Quoted 10 lines
drivers/mtd/spi/spi_ram.c +10
-0+if (!buf) +return -ENOMEM ; + +memset (buf,SPI_RAM_ERASE_BYTE , step); + +while (len) { + step =min_t (size_t, len,SPI_RAM_ERASE_STEP ); + + ret =spi_ram_write (dev, offset, step, buf); +if (ret)
drivers/mtd/spi/spi_ram.c line 139
Quoted 10 lines
drivers/mtd/spi/spi_ram.c +10
-0+break ; + + offset += step; + len -= step; + } + +free (buf); + +return ret; +}
Two things. If len is 0 we still malloc(0) - please return early. And each loop iteration calls spi_ram_write(), which claims and releases the bus for every 1 KB chunk. Since you have already put the device into sequential mode at probe, one claim/release around the whole loop (or a helper that emits one WRITE command and streams the fill) would be much cheaper and match what the data sheet allows.
drivers/mtd/spi/spi_ram.c line 10
Quoted 12 lines
drivers/mtd/spi/spi_ram.c +10
-0@@ -0,0 +1,280 @@ + mtd->writesize =1 ; + mtd->erasesize =1 ; + mtd->_erase = spi_ram_mtd_erase; + mtd->_read = spi_ram_mtd_read; + mtd->_write = spi_ram_mtd_write; + + flash->name = data->name; + flash->size = data->size; + flash->erase_size = mtd->erasesize; + flash->sector_size = mtd->erasesize;
Setting sector_size to 1 makes the sf update path in cmd/sf.c drop to a one-byte read/erase/write loop over the whole region (see spi_flash_update() and spi_flash_update_block()) - 128 KB of byte-at-a-time round trips. Please pick something more useful (the 32-byte page, or a larger convenience value) for sector_size, even if erasesize stays at 1.
drivers/mtd/spi/spi_ram.c line 1
Quoted 3 lines
drivers/mtd/spi/spi_ram.c +1
-0@@ -0,0 +1,280 @@ +sprintf (flash->mtd_name,"ram%d" ,dev_seq (dev));
Please use snprintf() with sizeof(flash->mtd_name) - the buffer is MTD_NAME_SIZE(MTD_DEV_TYPE_NOR), which only just happens to fit 'ram' because it is also three characters, and any future change to that macro or DM_MAX_SEQ_STR will bite silently.
drivers/mtd/spi/spi_ram.c line 2
Quoted 4 lines
drivers/mtd/spi/spi_ram.c +2
-0@@ -0,0 +1,280 @@ +* Copyright (C) 2019 S. Bigler Securiton AG +* Copyright (C) 2023 Heiko Schocher, DENX Software Engineering, [email protected].
Nit: please drop the trailing full stop after the email address on the 2023 line - do you have an email for the Bigler person?
Regards,
Simon
Re: [PATCH v1 1/3] mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
Hello Simon,
Thanks for the review!
Quoted 10 lines · Simon Glass
Hi Stefan,
Quoted 6 lines · Heiko Schocher
mtd: spi: add driver for the Microchip 23LCV1024 SPI SRAM
The 23LCV1024 is a 1 Mbit serial SRAM with battery backup, sitting on an SPI bus and used as non volatile scratch storage. Bind it to the
spi_flashuclass so that the sf command reaches it, and register it as an MTD device of typeMTD_RAM, so mtd command can be used.
Quoted 6 lines
Is
UCLASS_SPI_FLASHreally the right home for a device with no flash semantics? The uclass allocates a structspi_norfor every instance and every user of it (sf command, bootdev, MTD helpers) assumes NOR behaviour. It works because you shim the fields in, but it feels wrong. Have you considered driving it purely throughUCLASS_MTD, or adding a smallUCLASS_SPI_RAMwith the same ops? What do you think?
At start of this, we had such an UCLASS_SPI_RAM approach, but we had a lot of duplicated code... I think again about this.
Hmm... I tend to say, I would prefer UCLASS_MTD, which is the lower impact I think, we only use the "sf command" and have only the mtd read/write functions... and driver drivers/mtd/nand/spi makes it the same way.
With UCLASS_SPI_RAM I do not see what we win, and the impact is bigger.
Let me think about it.
Quoted 32 lines
Quoted 32 lines
The device powers up in byte operation, where every command carries exactly one data byte. Select sequential operation once during probe, as Linux does in
drivers/mtd/devices/mchp23k256.c, so that a read or a write of any length is served by a single command.A RAM needs no erase before a write, but a caller that asks for one expects the area to read back the way an erased flash would, so both erase operations fill it with
0xff, which is what the sandbox flash emulation leaves behind as well. Leaving the operation out is no option either, because the uclass calls it without checking for it first.Signed-off-by Stefan Bigler <[email protected]>Signed-off-by Heiko Schocher <[email protected]>configs/sandbox_defconfig | 1 +
drivers/mtd/spi/Kconfig | 11 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram.c | 280 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 293 insertions(+)drivers/mtd/spi/spi_ram.c +8
-0@@ -0,0 +1,280 @@ +#include <dm.h> +#include <dm/device_compat.h> +#include <log.h> +#include <malloc.h> +#include <spi.h> +#include <spi_flash.h> +#include <vsprintf.h> +#include <linux/bitops.h>
Quoted 8 lines
drivers/mtd/spi/spi_ram.c line 21
Quoted 4 lines
drivers/mtd/spi/spi_ram.c +4
-0+#include <linux/kernel.h> +#include <linux/mtd/mtd.h> +#include <linux/mtd/spi-nor.h> +#include <linux/sizes.h> The
linux/mtd/spi-nor.hinclude is redundant —spi_flash.halready pulls it in — and it advertises that this driver is a NOR driver, which it is not. Please drop it.
Will do.
Quoted 12 lines
drivers/mtd/spi/spi_ram.c line 7
Quoted 10 lines
drivers/mtd/spi/spi_ram.c +7
-0@@ -0,0 +1,280 @@ +static int spi_ram_erase (struct udevice *dev,u32 offset,size_t len) +{ +size_t step =min_t (size_t, len,SPI_RAM_ERASE_STEP ); +u8 *buf; +int ret =0 ; + + buf =malloc (step);I wish we could avoid
malloc()here?
I look for it, when preparing v2.
Quoted 20 lines
Quoted 20 lines
drivers/mtd/spi/spi_ram.c +19
-0+if (!buf) +return -ENOMEM ; + +memset (buf,SPI_RAM_ERASE_BYTE , step); + +while (len) { + step =min_t (size_t, len,SPI_RAM_ERASE_STEP ); + + ret =spi_ram_write (dev, offset, step, buf); +if (ret) +break ; + + offset += step; + len -= step; + } + +free (buf); + +return ret;
Quoted 8 lines
drivers/mtd/spi/spi_ram.c line 27
Quoted 1 line
drivers/mtd/spi/spi_ram.c +1
-0+}Two things. If len is 0 we still malloc(0) - please return early. And each loop iteration calls
spi_ram_write(), which claims and releases the bus for every 1 KB chunk. Since you have already put the device into sequential mode at probe, one claim/release around the whole loop (or a helper that emits one WRITE command and streams the fill) would be much cheaper and match what the data sheet allows.
Yes, that sounds better.
Hmm.. can we drop the erase function at all, as we do not need it?
Ah, I see, we already have
mtd->flags and MTD_NO_ERASE from linux.
I will drop erase function from spi ram driver, and if I see it correct I can get rid with this step of the malloc() too!
I just need to add in mtd_erase() the MTD_NO_ERASE handling (will add this in v2 as a seperate patch.
Oh, I see in linux, that mtd_erase() there only checks:
if (!mtd->erasesize || !master->_erase) return -ENOTSUPP;
This linux check is missing currently in U-Boots implementation.
Do you agree, that we can simply add this check?
But why not checking the flag MTD_NO_ERASE too?
What do you prefer, the NULL check from linux and/or check the MTD_NO_ERASE flag?
Quoted 12 lines
Quoted 12 lines
drivers/mtd/spi/spi_ram.c +9
-0@@ -0,0 +1,280 @@ + mtd->writesize =1 ; + mtd->erasesize =1 ; + mtd->_erase = spi_ram_mtd_erase; + mtd->_read = spi_ram_mtd_read; + mtd->_write = spi_ram_mtd_write; + + flash->name = data->name; + flash->size = data->size; + flash->erase_size = mtd->erasesize;
Quoted 8 lines
drivers/mtd/spi/spi_ram.c line 238
Quoted 1 line
drivers/mtd/spi/spi_ram.c +1
-0+ flash->sector_size = mtd->erasesize;Setting
sector_sizeto 1 makes the sf update path incmd/sf.cdrop to a one-byte read/erase/write loop over the whole region (seespi_flash_update()andspi_flash_update_block()) - 128 KB of byte-at-a-time round trips. Please pick something more useful (the 32-byte page, or a larger convenience value) forsector_size, even if erasesize stays at 1.
Yes, on the other side, there is no real sector size, you can write byte per byte... and in case writting exact one byte, we have with other settings for writesize/erasize overhead...
I think, the writesize setting should stay at 1, and erasesize gets 0
Quoted 9 lines
drivers/mtd/spi/spi_ram.c line 1
Quoted 4 lines
drivers/mtd/spi/spi_ram.c +1
-0@@ -0,0 +1,280 @@ +sprintf (flash->mtd_name,"ram%d" ,dev_seq (dev));Please use
snprintf()withsizeof(flash->mtd_name)- the buffer isMTD_NAME_SIZE(MTD_DEV_TYPE_NOR), which only just happens to fit 'ram' because it is also three characters, and any future change to that macro orDM_MAX_SEQ_STRwill bite silently.
Good hint, thanks!
Quoted 7 lines
drivers/mtd/spi/spi_ram.c line 2
Quoted 5 lines
drivers/mtd/spi/spi_ram.c +2
-0@@ -0,0 +1,280 @@ +* Copyright (C) 2019 S. Bigler Securiton AG +* Copyright (C) 2023 Heiko Schocher, DENX Software Engineering, [email protected]. Nit: please drop the trailing full stop after the email address on the
Will do, sorry.
Quoted 1 line
2023 line - do you have an email for the Bigler person?
Yes, I add it in v2
bye,
Heiko
Quoted 4 lines
Regards,
Simon
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic
PATCH 2/3 sandbox: spi: let a slave name the driver that emulates it
In reply to Heiko Schochersandbox_sf_bind_emul() binds the SPI NOR flash emulator for every slave on the sandbox SPI bus, whatever that slave is. There is no way to emulate a different kind of SPI device, and a test for such a device therefore cannot be written.
Take the name of the emulation driver from a "sandbox,emul" property on the node of the slave, and fall back to the flash emulator when the property is absent, so that every node in the test device tree today keeps the emulation it has.
---
drivers/mtd/spi/sandbox.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
drivers/mtd/spi/sandbox.c +23 -2
@@ -518,6 +518,27 @@ static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { };#ifdef CONFIG_SPI_FLASH +/** +* sandbox_spi_emul_lookup() - find the emulator standing in for a slave +* +* A slave that is not a SPI NOR flash names the driver that emulates it +* in its "sandbox,emul" property. Without that property the flash +* emulator is used, which is what every existing node relies on. +* +* @node: device tree node of the slave +* Return: driver to bind, or NULL if it does not exist +*/ +static struct driver *sandbox_spi_emul_lookup (ofnode node) +{ +const char *name; + + name =ofnode_read_string (node,"sandbox,emul" ); +if (!name) + name ="sandbox_sf_emul" ; + +return lists_driver_lookup_name (name); +} +int sandbox_sf_bind_emul (struct sandbox_state *state,int busnum,int cs,struct udevice *bus,ofnode node,const char *spec) {
@@ -530,9 +551,9 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,strncpy (name, spec,sizeof (name) -6 ); name[sizeof (name) -6 ] ='\0' ;strcat (name,"-emul" ); - drv =lists_driver_lookup_name ("sandbox_sf_emul" ); + drv =sandbox_spi_emul_lookup (node);if (!drv) { -puts ("Cannot find sandbox_sf_emul driver\n" ); +puts ("Cannot find SPI emulation driver\n" );return -ENOENT ; } str =strdup (name);
Signature · 1 line
2.55.0
Re: [PATCH v1 2/3] sandbox: spi: let a slave name the driver that emulates it
Hi Heiko,
Quoted 19 lines · Heiko Schocher
sandbox: spi: let a slave name the driver that emulates it
sandbox_sf_bind_emul()binds the SPI NOR flash emulator for every slave on the sandbox SPI bus, whatever that slave is. There is no way to emulate a different kind of SPI device, and a test for such a device therefore cannot be written.Take the name of the emulation driver from a "sandbox,emul" property on the node of the slave, and fall back to the flash emulator when the property is absent, so that every node in the test device tree today keeps the emulation it has.
Signed-off-by Heiko Schocher <[email protected]>
drivers/mtd/spi/sandbox.c| 25 +++++++++++++++++++++++--1 file changed, 23 insertions(+), 2 deletions(-)
drivers/mtd/spi/sandbox.c static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { · line 527
Quoted 10 lines
drivers/mtd/spi/sandbox.c +10
-0@@ -518,6 +518,27 @@ static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { +static struct driver *sandbox_spi_emul_lookup (ofnode node) +{ +const char *name; + + name =ofnode_read_string (node,"sandbox,emul" ); +if (!name) + name ="sandbox_sf_emul" ; + +return lists_driver_lookup_name (name); +}
The property name sandbox,emul is already in use - see arch/sandbox/dts/sandbox.dtsi for i2c-eeprom, sandbox-rtc, the PMIC and the swap-case device, and drivers/i2c/i2c-emul-uclass.c / drivers/pci/pci-emul-uclass.c on the consumer side. Everywhere else it is a phandle pointing at an emulation node whose compatible selects the driver, and dtoc even hard-codes #emul-cells for it in tools/dtoc/dtb_platdata.py. Reusing the same name for a string that happens to be a U-Boot driver name will confuse anyone reading a sandbox DT, and it bakes an implementation detail (the C symbol of the driver) into the device tree. Please follow the I2C and PCI pattern:
put the emulation in its own node with the appropriate compatible, and reference it here with sandbox,emul = <&spi_ram_emul>. Then sandbox_spi_emul_lookup() resolves the phandle, binds the driver named by its compatible, and there is no new schema to invent.
drivers/mtd/spi/sandbox.c int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, · line 555
Quoted 8 lines
drivers/mtd/spi/sandbox.c +1
-1@@ -530,9 +551,9 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, drv =lists_driver_lookup_name ("sandbox_sf_emul" );if (!drv) { -puts ("Cannot find sandbox_sf_emul driver\n" ); +puts ("Cannot find SPI emulation driver\n" );return -ENOENT ; }
Now that the name can come from the DT, the message should say which driver was not found - otherwise a typo in sandbox,emul gives the reader nothing to go on. Something like printf("Cannot find SPI emulation driver '%s'\n", name) would help, which means passing the resolved name back from the lookup helper.
Regards,
Simon
Re: [PATCH v1 2/3] sandbox: spi: let a slave name the driver that emulates it
Hello Simon,
Quoted 39 lines · Simon Glass
Hi Heiko,
Quoted 19 lines · Heiko Schocher
sandbox: spi: let a slave name the driver that emulates it
sandbox_sf_bind_emul()binds the SPI NOR flash emulator for every slave on the sandbox SPI bus, whatever that slave is. There is no way to emulate a different kind of SPI device, and a test for such a device therefore cannot be written.Take the name of the emulation driver from a "sandbox,emul" property on the node of the slave, and fall back to the flash emulator when the property is absent, so that every node in the test device tree today keeps the emulation it has.
Signed-off-by Heiko Schocher <[email protected]>
drivers/mtd/spi/sandbox.c| 25 +++++++++++++++++++++++--1 file changed, 23 insertions(+), 2 deletions(-)drivers/mtd/spi/sandbox.c static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { · line 527
Quoted 10 lines
drivers/mtd/spi/sandbox.c +10
-0@@ -518,6 +518,27 @@ static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { +static struct driver *sandbox_spi_emul_lookup (ofnode node) +{ +const char *name; + + name =ofnode_read_string (node,"sandbox,emul" ); +if (!name) + name ="sandbox_sf_emul" ; + +return lists_driver_lookup_name (name); +}The property name sandbox,emul is already in use - see
arch/sandbox/dts/sandbox.dtsifor i2c-eeprom, sandbox-rtc, the PMIC and the swap-case device, anddrivers/i2c/i2c-emul-uclass.c/drivers/pci/pci-emul-uclass.con the consumer side. Everywhere else it is a phandle pointing at an emulation node whose compatible selects the driver, and dtoc even hard-codes #emul-cells for it in
Quoted 8 lines
tools/dtoc/dtb_platdata.py. Reusing the same name for a string that happens to be a U-Boot driver name will confuse anyone reading a sandbox DT, and it bakes an implementation detail (the C symbol of the driver) into the device tree. Please follow the I2C and PCI pattern:
put the emulation in its own node with the appropriate compatible, and reference it here with sandbox,emul = <&spi_ram_emul>. Thensandbox_spi_emul_lookup()resolves the phandle, binds the driver named by its compatible, and there is no new schema to invent.
Ouch. Yes, I am not to deep in writing sandbox test. I try to rework this part.
Quoted 15 lines
drivers/mtd/spi/sandbox.c int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, · line 555
Quoted 9 lines
drivers/mtd/spi/sandbox.c +1
-1@@ -530,9 +551,9 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, drv =lists_driver_lookup_name ("sandbox_sf_emul" );if (!drv) { -puts ("Cannot find sandbox_sf_emul driver\n" ); +puts ("Cannot find SPI emulation driver\n" );return -ENOENT ; }Now that the name can come from the DT, the message should say which driver was not found - otherwise a typo in sandbox,emul gives the reader nothing to go on. Something like
printf("Cannot find SPI emulation driver '%s'\n", name)would help, which means passing the resolved name back from the lookup helper.
Indeed, I try to rework.
Many thanks for your feedback and time!
bye,
Heiko
Quoted 4 lines
Regards,
Simon
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic
PATCH 3/3 test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
In reply to Heiko SchocherEmulate the device on a second sandbox SPI bus and exercise the driver through the spi_flash uclass.
The emulation implements byte, page and sequential operation and advances the internal address counter the way the data sheet describes for each of them. A transfer running past the 32 byte page of the device therefore only comes back unharmed once the driver has selected sequential operation, which is what its probe is for. The test asserts that mode directly as well, so a failure says which of the two went wrong.
The erase is checked on both counts that matter for a device that has nothing to erase: that the area reads back as 0xff afterwards, and that the fill stops at the end of the range it was given rather than running into the data next to it.
---
arch/sandbox/dts/test.dts | 14 +++ arch/sandbox/include/asm/test.h | 8 ++ configs/sandbox_defconfig | 1 + drivers/mtd/spi/Kconfig | 9 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram_sandbox.c | 189 ++++++++++++++++++++++++++++++ drivers/spi/Kconfig | 2 +- test/dm/Makefile | 1 + test/dm/spi_ram.c | 96 +++++++++++++++ 9 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 drivers/mtd/spi/spi_ram_sandbox.c create mode 100644 test/dm/spi_ram.c
arch/sandbox/dts/test.dts +14 -0
@@ -1644,6 +1644,20 @@ }; }; + spi@1 { + #address-cells = <1>; + #size-cells = <0>; + reg = <1 1>; + compatible = "sandbox,spi"; + + sram@0 { + reg = <0>; + compatible = "microchip,mchp23lcv1024"; + spi-max-frequency = <20000000>; + sandbox,emul = "sandbox_spi_ram_emul"; + }; + }; + syscon0: syscon@0 { compatible = "sandbox,syscon0"; reg = <0x10 16>;
arch/sandbox/include/asm/test.h +8 -0
@@ -251,6 +251,14 @@ uint sandbox_spi_get_mode(struct udevice *dev);*/ uint sandbox_spi_get_wordlen (struct udevice *dev); +/** +* sandbox_spi_ram_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_spi_ram_is_sequential (struct udevice *dev); +/** * sandbox_get_pch_spi_protect() - Get the PCI SPI protection status *
configs/sandbox_defconfig +1 -0
@@ -277,6 +277,7 @@ CONFIG_SPI_FLASH_STMICRO=y CONFIG_SPI_FLASH_SST=y CONFIG_SPI_FLASH_WINBOND=y CONFIG_SPI_RAM=y +CONFIG_SPI_RAM_SANDBOX=y CONFIG_NVMXIP_QSPI=y CONFIG_MULTIPLEXER=y CONFIG_MUX_MMIO=y
drivers/mtd/spi/Kconfig +9 -0
@@ -259,6 +259,15 @@ config SPI_RAM If unsure, say N +config SPI_RAM_SANDBOX + bool "Sandbox emulation of the Microchip 23LCV1024" + depends on SANDBOX && SPI_RAM + 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. + config SPI_FLASH_USE_4K_SECTORS bool "Use small 4096 B erase sectors" depends on SPI_FLASH
drivers/mtd/spi/Makefile +1 -0
@@ -19,6 +19,7 @@ endif obj-$(CONFIG_SPI_FLASH) += spi-nor.o obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o obj-$(CONFIG_SPI_RAM) += spi_ram.o +obj-$(CONFIG_SPI_RAM_SANDBOX) += spi_ram_sandbox.o obj-$(CONFIG_$(PHASE_)SPI_FLASH_MTD) += sf_mtd.o obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o obj-$(CONFIG_$(PHASE_)BOOTDEV_SPI_FLASH) += sf_bootdev.o
drivers/mtd/spi/spi_ram_sandbox.c new file +189 -0
189 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
@@ -119,6 +119,7 @@ obj-$(CONFIG_SIMPLE_PM_BUS) += simple-pm-bus.o obj-$(CONFIG_SOC_DEVICE) += soc.o obj-$(CONFIG_SOUND) += sound.o obj-$(CONFIG_DM_SPI) += spi.o +obj-$(CONFIG_SPI_RAM) += spi_ram.o obj-$(CONFIG_SPMI) += spmi.o obj-y += syscon.o obj-$(CONFIG_RESET_SYSCON) += syscon-reset.o
test/dm/spi_ram.c new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v1 3/3] test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
Hi Heiko,
Quoted 33 lines · Heiko Schocher
test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
Emulate the device on a second sandbox SPI bus and exercise the driver through the
spi_flashuclass.The emulation implements byte, page and sequential operation and advances the internal address counter the way the data sheet describes for each of them. A transfer running past the 32 byte page of the device therefore only comes back unharmed once the driver has selected sequential operation, which is what its probe is for. The test asserts that mode directly as well, so a failure says which of the two went wrong.
The erase is checked on both counts that matter for a device that has nothing to erase: that the area reads back as
0xffafterwards, and that the fill stops at the end of the range it was given rather than running into the data next to it.Signed-off-by Heiko Schocher <[email protected]>
arch/sandbox/dts/test.dts| 14 +++arch/sandbox/include/asm/test.h | 8 ++ configs/sandbox_defconfig | 1 + drivers/mtd/spi/Kconfig | 9 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram_sandbox.c | 189 ++++++++++++++++++++++++++++++++++++++ drivers/spi/Kconfig | 2 +- test/dm/Makefile | 1 + test/dm/spi_ram.c | 96 +++++++++++++++++++ 9 files changed, 320 insertions(+), 1 deletion(-)
test/dm/spi_ram.c line 2
Quoted 2 lines
test/dm/spi_ram.c +2
-0@@ -0,0 +1,96 @@ +/* Now erase the data itself, over more than one staging step */ +ut_assertok (spi_flash_erase_dm (dev,SPI_RAM_OFFSET ,SPI_RAM_LEN ));
The comment does not match the code: SPI_RAM_LEN is 100 and SPI_RAM_ERASE_STEP in patch 1 is SZ_1K, so spi_ram_erase() runs a single iteration here. If the intent is to cover the staging loop, please raise the length past SZ_1K (or drop the staging step for the test); otherwise reword the comment.
test/dm/spi_ram.c line 4
Quoted 6 lines
test/dm/spi_ram.c +4
-0@@ -0,0 +1,96 @@ + bus =dev_get_parent (dev); + emul = state->spi[dev_seq (bus)][SPI_RAM_CS ].emul; +ut_assertnonnull (emul); +ut_assert (sandbox_spi_ram_is_sequential (emul));
Please use sandbox_spi_get_emul() here rather than reaching into state->spi[][] directly - it already knows how to locate the emulator for a given slave.
test/dm/spi_ram.c line 5
Quoted 7 lines
test/dm/spi_ram.c +5
-0@@ -0,0 +1,96 @@ +/* +* Since we are about to destroy all devices, we must tell sandbox +* to forget the emulation device +*/ +sandbox_sf_unbind_emul (state,dev_seq (bus),SPI_RAM_CS );
The name sandbox_sf_unbind_emul() reads as SPI-NOR specific, but after patch 2 it is used for any SPI slave's emulator. Please rename it (e.g. sandbox_spi_unbind_emul()) in patch 2 and update the callers, so this new test does not entrench the misleading name.
arch/sandbox/dts/test.dts line 1676
Quoted 7 lines
arch/sandbox/dts/test.dts +5
-0@@ -1672,6 +1672,20 @@ + spi@1 { + #address-cells = <1>; + #size-cells = <0>; + reg = <1 1>; + compatible = "sandbox,spi";
Please add a spi1 entry to the aliases block above, so dev_seq(bus) is deterministic rather than depending on probe order - the test relies on it to look up the emul.
Regards,
Simon
Re: [PATCH v1 3/3] test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
Hello Simon,
Quoted 34 lines · Simon Glass
Hi Heiko,
Quoted 30 lines · Heiko Schocher
test: dm: add a test for the Microchip 23LCV1024 SPI SRAM
Emulate the device on a second sandbox SPI bus and exercise the driver through the
spi_flashuclass.The emulation implements byte, page and sequential operation and advances the internal address counter the way the data sheet describes for each of them. A transfer running past the 32 byte page of the device therefore only comes back unharmed once the driver has selected sequential operation, which is what its probe is for. The test asserts that mode directly as well, so a failure says which of the two went wrong.
The erase is checked on both counts that matter for a device that has nothing to erase: that the area reads back as
0xffafterwards, and that the fill stops at the end of the range it was given rather than running into the data next to it.Signed-off-by Heiko Schocher <[email protected]>
arch/sandbox/dts/test.dts| 14 +++arch/sandbox/include/asm/test.h | 8 ++ configs/sandbox_defconfig | 1 + drivers/mtd/spi/Kconfig | 9 ++ drivers/mtd/spi/Makefile | 1 + drivers/mtd/spi/spi_ram_sandbox.c | 189 ++++++++++++++++++++++++++++++++++++++ drivers/spi/Kconfig | 2 +- test/dm/Makefile | 1 + test/dm/spi_ram.c | 96 +++++++++++++++++++ 9 files changed, 320 insertions(+), 1 deletion(-)
Quoted 10 lines
test/dm/spi_ram.c line 2
Quoted 4 lines
test/dm/spi_ram.c +2
-0@@ -0,0 +1,96 @@ +/* Now erase the data itself, over more than one staging step */ +ut_assertok (spi_flash_erase_dm (dev,SPI_RAM_OFFSET ,SPI_RAM_LEN ));The comment does not match the code:
SPI_RAM_LENis 100 andSPI_RAM_ERASE_STEPin patch 1 isSZ_1K, sospi_ram_erase()runs a single iteration here. If the intent is to cover the staging loop, please raise the length pastSZ_1K(or drop the staging step for the test); otherwise reword the comment.
Good catch... Hmm.. I rework this too, as I drop the erase function.
Quoted 11 lines
test/dm/spi_ram.c line 4
Quoted 7 lines
test/dm/spi_ram.c +4
-0@@ -0,0 +1,96 @@ + bus =dev_get_parent (dev); + emul = state->spi[dev_seq (bus)][SPI_RAM_CS ].emul; +ut_assertnonnull (emul); +ut_assert (sandbox_spi_ram_is_sequential (emul));Please use
sandbox_spi_get_emul()here rather than reaching into state->spi[][] directly - it already knows how to locate the emulator for a given slave.
I will do.
Quoted 13 lines
test/dm/spi_ram.c line 5
Quoted 8 lines
test/dm/spi_ram.c +5
-0@@ -0,0 +1,96 @@ +/* +* Since we are about to destroy all devices, we must tell sandbox +* to forget the emulation device +*/ +sandbox_sf_unbind_emul (state,dev_seq (bus),SPI_RAM_CS );The name
sandbox_sf_unbind_emul()reads as SPI-NOR specific, but after patch 2 it is used for any SPI slave's emulator. Please rename it (e.g.sandbox_spi_unbind_emul()) in patch 2 and update the callers, so this new test does not entrench the misleading name.
I look into it.
Quoted 12 lines
arch/sandbox/dts/test.dts line 1676
Quoted 8 lines
arch/sandbox/dts/test.dts +5
-0@@ -1672,6 +1672,20 @@ + spi@1 { + #address-cells = <1>; + #size-cells = <0>; + reg = <1 1>; + compatible = "sandbox,spi";Please add a spi1 entry to the aliases block above, so
dev_seq(bus)is deterministic rather than depending on probe order - the test relies on it to look up the emul.
I add it.
Thanks!
bye,
Heiko
Quoted 4 lines
Regards,
Simon
Signature · 5 lines
Nabla Software Engineering HRB 40522 Augsburg Phone: +49 821 45592596 E-Mail: [email protected] Geschäftsführer : Stefano Babic
Hi Heiko,
Quoted 3 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, plus a sandbox emulation and a unit test.
Just to check the placement: this is an SRAM, not a NOR flash, yet both the driver and its sandbox emulation land in drivers/mtd/spi/ next to the flash code, and the driver is bound to the spi_flash uclass. Have you considered drivers/misc/ or a new drivers/mtd/spi-ram/, with either a dedicated uclass or UCLASS_MTD directly? Piggy-backing on spi_flash to reach the sf command works, but the naming is a bit confusing.
Quoted 1 line
Patch 2 and 3 touch sandbox and the tests not the SPI NOR subsystem.
Not quite - patch 2 modifies drivers/mtd/spi/sandbox.c and patch 3 adds drivers/mtd/spi/spi_ram_sandbox.c plus Kconfig/Makefile entries in the same directory, so they very much sit inside the SPI NOR subsystem tree. Please reword this in v2.
Patch 2 introduces a new sandbox,emul device-tree property but I don't see a binding document for it. Please add a short binding under doc/device-tree-bindings/ (or extend the existing sandbox SPI binding) describing what it means, with an example.
Regards,
Simon