spi: Introduce driver-model support for SPI EEPROMs
Subsystems (2)
PATCH v7 2/3 spi: Introduce initial driver-model support for SPI EEPROMs
In reply to João LoureiroU-Boot has driver-model support for EEPROMs on several buses - I2C through UCLASS_I2C_EEPROM and 1-Wire through UCLASS_W1_EEPROM - but nothing for SPI, even though SPI EEPROMs are common and are usually the faster of the two.
These devices typically hold a board's identity: its serial number, a unique device ID and the hardware variant. U-Boot needs that early, both to decide which device tree and overlays to load for the variant it happens to be running on, and to hand the unique IDs to the system it is about to boot. Today a board that wants any of this has to open-code raw SPI transfers in board code, which is neither reusable nor testable.
Introduce a UCLASS_SPI_EEPROM uclass with read/write/size operations, analogous to the I2C EEPROM uclass, along with a driver for AT25-style parts. The chip geometry - total size, page size and address width - is taken from the device tree as described by the atmel,at25 binding in dts/upstream/Bindings/eeprom/at25.yaml, which requires those properties, falling back to per-compatible defaults held in driver data. Devices using 8-, 16- and 24-bit addressing are therefore all covered, and adding a part later is a driver-data entry rather than a change to the uclass API. The 9-bit addressing mode the binding also allows, where the ninth address bit travels in the opcode, is not implemented.
Only reading is implemented for now; spi_eeprom_write() returns -ENOSYS. Enable the uclass in sandbox_defconfig so that the emulator and DM test added by the next patch are built.
---
Changes in v7:
- The status-register read in probe() is diagnostic only and no longer fails probe(); SPI has no acknowledge, so it cannot detect presence.
- Fix the comment above spi_eeprom_read_status().
- Commit message nit: "the faster of the two".
MAINTAINERS | 7 + configs/sandbox_defconfig | 1 + drivers/misc/Kconfig | 15 ++ drivers/misc/Makefile | 1 + drivers/misc/spi_eeprom.c | 266 +++++++++++++++++++++++++++++++++ drivers/misc/spi_eeprom_priv.h | 18 +++ include/dm/uclass-id.h | 1 + include/spi_eeprom.h | 95 ++++++++++++ 8 files changed, 404 insertions(+) create mode 100644 drivers/misc/spi_eeprom.c create mode 100644 drivers/misc/spi_eeprom_priv.h create mode 100644 include/spi_eeprom.h
MAINTAINERS +7 -0
@@ -1708,6 +1708,13 @@ T: git https://git.u-boot-project.org/u-boot/custodians/u-boot-spi.git F: drivers/spi/ F: include/spi* +SPI EEPROM +M: João Loureiro <[email protected]> +S: Maintained +F: drivers/misc/spi_eeprom.c +F: drivers/misc/spi_eeprom_priv.h +F: include/spi_eeprom.h + SPI NAND M: Dario Binacchi <[email protected]> M: Michael Trimarchi <[email protected]>
configs/sandbox_defconfig +1 -0
@@ -267,6 +267,7 @@ CONFIG_SYS_NAND_USE_FLASH_BBT=y CONFIG_NAND_SANDBOX=y CONFIG_SYS_NAND_ONFI_DETECTION=y CONFIG_SYS_NAND_PAGE_SIZE=0x200 +CONFIG_SPI_EEPROM=y CONFIG_SPI_FLASH_SANDBOX=y CONFIG_BOOTDEV_SPI_FLASH=y CONFIG_SPI_FLASH_ATMEL=y
drivers/misc/Kconfig +15 -0
@@ -567,6 +567,21 @@ config I2C_EEPROM help Enable a generic driver for EEPROMs attached via I2C. +config SPI_EEPROM + bool "Enable driver for generic SPI-attached EEPROMs" + help + Enable a generic driver for EEPROMs attached via SPI. + + This provides the UCLASS_SPI_EEPROM uclass together with a driver + for AT25-style parts, i.e. devices described by the "atmel,at25" + device tree binding. The chip geometry (size, pagesize and + address-width) is read from the device tree, with per-compatible + defaults for the parts listed in the driver. Parts using 8-, 16- + and 24-bit addressing are supported; 9-bit addressing, where the + ninth address bit travels in the opcode, is not. + + Only read access is implemented at present; spi_eeprom_write() + returns -ENOSYS. config SPL_I2C_EEPROM bool "Enable driver for generic I2C-attached EEPROMs for SPL"
drivers/misc/Makefile +1 -0
@@ -44,6 +44,7 @@ obj-$(CONFIG_GDSYS_SOC) += gdsys_soc.o obj-$(CONFIG_IRQ) += irq-uclass.o obj-$(CONFIG_SANDBOX) += irq_sandbox.o irq_sandbox_test.o obj-$(CONFIG_$(PHASE_)I2C_EEPROM) += i2c_eeprom.o +obj-$(CONFIG_$(PHASE_)SPI_EEPROM) += spi_eeprom.o obj-$(CONFIG_IHS_FPGA) += ihs_fpga.o obj-$(CONFIG_IMX8) += imx8/ obj-$(CONFIG_IMX_ELE) += imx_ele/
drivers/misc/spi_eeprom.c new file +266 -0
266 lines not on this page · raw message
drivers/misc/spi_eeprom_priv.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Command set shared by the AT25-style SPI EEPROM driver and its sandbox +* emulator. These opcodes are an implementation detail of the driver, not +* part of the UCLASS_SPI_EEPROM API. +* +* Copyright (c) 2024 Koninklijke Philips N.V. +* Copyright (c) 2026 João Loureiro <[email protected]> +*/ + +#ifndef __SPI_EEPROM_PRIV_H +#define __SPI_EEPROM_PRIV_H + +/* AT25-style command set */ +#define AT25_CMD_READ_DATA 0x03/* Read data from memory array */ +#define AT25_CMD_READ_STATUS 0x05/* Read status register */ + +#endif /* __SPI_EEPROM_PRIV_H */
include/dm/uclass-id.h +1 -0
@@ -138,6 +138,7 @@ enum uclass_id {UCLASS_SOC ,/* SOC Device */ UCLASS_SOUND ,/* Playing simple sounds */ UCLASS_SPI ,/* SPI bus */ +UCLASS_SPI_EEPROM ,/* SPI EEPROM device */ UCLASS_SPI_FLASH ,/* SPI flash */ UCLASS_SPI_GENERIC ,/* Generic SPI flash target */ UCLASS_SPMI, /* System Power Management Interface bus */
include/spi_eeprom.h new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.55.0
v7 cover spi: Introduce driver-model support for SPI EEPROMs
This series adds a UCLASS_SPI_EEPROM uclass, a driver for AT25-style parts, a sandbox emulator and a DM test.
v7 addresses Simon's review of v6 2/3 and picks up his Reviewed-by on patches 1 and 2. Thanks, Simon.
On the status-register read in probe(): it was diagnostic, not a presence check - and on SPI it cannot be one. There is no acknowledge, so an unpopulated socket does not make the transfer fail; MISO just floats and the read returns 0x00 or 0xff. The only way the read could fail is a controller or bus-claim error, and failing probe() for that was, as you say, too strong. probe() now ignores a failed read and only logs the value at debug level, with a comment saying why. Any real bus problem still surfaces on the first spi_eeprom_read().
Testing on sandbox, against unpatched v2026.10-rc5: "ut dm" runs 583 tests to rc5's 582 with an identical list of failures (all environment/fixture related), and the new spi_eeprom test passes. The sandbox_vpl CI selection ("vpl or test_spl") passes 12/12 on both.
Changes in v7:
- Patch 2: the status-register read in probe() is diagnostic only and can no longer fail probe(); comment explains why SPI cannot detect presence this way.
- Patch 2: fix the comment above spi_eeprom_read_status(), which described the return value wrongly.
- Patch 2: commit message nit, "the faster of the two".
- Patches 1 and 2: add Simon's Reviewed-by.
Changes in v6:
- Patch 3: build the sandbox SPI EEPROM emulator only in U-Boot proper (not in TPL/VPL/SPL), fixing the sandbox_vpl CI failure.
- Resent as a new, correctly threaded series.
Changes in v5:
- New patch 1: test/dm/sf.c now asks for the emulator attached to its own slave rather than the first one in UCLASS_SPI_EMUL, fixing a heap corruption once a second SPI emulator exists.
- Patch 2 commit message reworded: imperative mood, "driver model" rather than "driver mode", and it now explains the motivation.
- SPI_EEPROM_CMD_SIZE removed. Address length is an addr_len field in driver data, overridable from the device tree, so 8-, 16- and 24-bit addressed parts are handled.
- AT25_CMD_* opcodes moved to a private header, drivers/misc/spi_eeprom_priv.h.
- Read path no longer computes "offset + size"; probe() rejects a "size" that does not fit the configured address width.
- of_to_plat() dropped; geometry read in probe().
- "size", "pagesize" and "address-width" follow the in-tree atmel,at25 binding (dts/upstream/Bindings/eeprom/at25.yaml).
- Compatible list taken from that binding, plus the "atmel,at25" fallback.
- Kconfig: "depends on MISC" dropped, help text expanded.
- include/spi_eeprom.h converted to kerneldoc.
- Sandbox EEPROM node in test.dts follows the binding.
Changes in v4:
- Emulator frees its backing store in a remove() method.
- Full kerneldoc for sandbox_spi_emul_get().
Changes in v3:
- Reindented with tabs; the series is now checkpatch-clean.
- Dropped the no-op write stub in favour of returning -ENOSYS.
- Removed dead code, fixed the read bounds check, corrected the AT25160 geometry, switched to u8 types and added a MAINTAINERS entry.
- Added the sandbox emulator and the DM test.
João Loureiro (3):
test: dm: sf: Get the emulator attached to the flash slave
spi: Introduce initial driver-model support for SPI EEPROMs
sandbox: spi: Add SPI EEPROM emulator and DM test
MAINTAINERS | 9 ++ arch/sandbox/dts/test.dts | 16 +- configs/sandbox_defconfig | 1 + drivers/misc/Kconfig | 15 ++ drivers/misc/Makefile | 2 + drivers/misc/spi_eeprom.c | 266 +++++++++++++++++++++++++++++++++ drivers/misc/spi_eeprom_emul.c | 130 ++++++++++++++++ drivers/misc/spi_eeprom_priv.h | 18 +++ drivers/spi/sandbox_spi.c | 37 ++++- include/dm/uclass-id.h | 1 + include/spi_eeprom.h | 95 ++++++++++++ test/dm/Makefile | 1 + test/dm/sf.c | 9 +- test/dm/spi_eeprom.c | 46 ++++++ 14 files changed, 642 insertions(+), 4 deletions(-) create mode 100644 drivers/misc/spi_eeprom.c create mode 100644 drivers/misc/spi_eeprom_emul.c create mode 100644 drivers/misc/spi_eeprom_priv.h create mode 100644 include/spi_eeprom.h create mode 100644 test/dm/spi_eeprom.c
base-commit: a06e89ab05eaa2b8344d521319399333cd760ae5
Signature · 1 line
2.55.0
PATCH v7 1/3 test: dm: sf: Get the emulator attached to the flash slave
dm_test_spi_flash() picks up its SPI emulator with uclass_first_device_err(UCLASS_SPI_EMUL), which assumes the sandbox SPI flash emulator is the only device in that uclass. That holds today only because the flash emulator is bound lazily, on the first transfer.
As soon as a second SPI emulator is described in the device tree it is bound during the devicetree scan and so comes first in the uclass. The test then hands a foreign device to sandbox_sf_set_block_protect(), which casts its private data to struct sandbox_spi_flash and writes past the end of it, corrupting the heap.
Ask for the emulator attached to this particular slave instead, which is what the test means, and which stays correct however many SPI emulators exist.
--- test/dm/sf.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
test/dm/sf.c +7 -2
@@ -50,8 +50,13 @@ static int dm_test_spi_flash(struct unit_test_state *uts)ut_assertok (spi_flash_read_dm (dev,0 , size, dst));ut_asserteq_mem (src, dst, size); -/* Try the write-protect stuff */ -ut_assertok (uclass_first_device_err (UCLASS_SPI_EMUL , &emul)); +/* +* Try the write-protect stuff. Ask for the emulator attached to this +* particular slave rather than the first one in the uclass, since +* other SPI emulators may be present. +*/ +ut_assertok (sandbox_spi_get_emul (state_get_current (), dev->parent, dev, + &emul));ut_asserteq (0 ,spl_flash_get_sw_write_prot (dev));sandbox_sf_set_block_protect (emul,1 );ut_asserteq (1 ,spl_flash_get_sw_write_prot (dev));
Signature · 1 line
2.55.0
PATCH v7 3/3 sandbox: spi: Add SPI EEPROM emulator and DM test
In reply to João LoureiroAdd a sandbox emulator for an AT25-style SPI EEPROM so the new SPI EEPROM uclass can be exercised without real hardware.
The sandbox SPI controller previously always bound the SPI flash emulator for every chip select. Allow a slave node to select its own emulator through a sandbox,emul phandle (mirroring the sandbox I2C bus), falling back to the SPI flash emulator when none is given so that existing behaviour is preserved.
A test EEPROM is wired up on chip select 3 of the sandbox SPI bus and a test/dm test verifies size reporting, patterned reads, out-of-bounds rejection and the unimplemented write path. The node follows the atmel,at25 binding: it carries the "atmel,at25" fallback compatible and the required size, pagesize and address-width properties.
--- MAINTAINERS | 2 + arch/sandbox/dts/test.dts | 16 +++- drivers/misc/Makefile | 1 + drivers/misc/spi_eeprom_emul.c | 130 +++++++++++++++++++++++++++++++++ drivers/spi/sandbox_spi.c | 37 +++++++++- test/dm/Makefile | 1 + test/dm/spi_eeprom.c | 46 ++++++++++++ 7 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 drivers/misc/spi_eeprom_emul.c create mode 100644 test/dm/spi_eeprom.c
MAINTAINERS +2 -0
@@ -1712,8 +1712,10 @@ SPI EEPROM M: João Loureiro <[email protected]> S: Maintained F: drivers/misc/spi_eeprom.c +F: drivers/misc/spi_eeprom_emul.c F: drivers/misc/spi_eeprom_priv.h F: include/spi_eeprom.h +F: test/dm/spi_eeprom.c SPI NAND M: Dario Binacchi <[email protected]>
arch/sandbox/dts/test.dts +15 -1
@@ -1618,7 +1618,7 @@ #size-cells = <0>; reg = <0 1>; compatible = "sandbox,spi"; - cs-gpios = <0>, <0>, <&gpio_a 0>; + cs-gpios = <0>, <0>, <&gpio_a 0>, <0>; pinctrl-names = "default"; pinctrl-0 = <&pinmux_spi0_pins>;
@@ -1636,6 +1636,20 @@ spi-cpol; spi-cpha; }; + eeprom@3 { + reg = <3>; + compatible = "microchip,at25160bn", "atmel,at25"; + spi-max-frequency = <1000000>; + size = <2048>; + pagesize = <32>; + address-width = <16>; + sandbox,emul = <&spi_eeprom_emul>; + }; + }; + + spi_eeprom_emul: spi-eeprom-emul { + compatible = "sandbox,spi-eeprom"; + sandbox,size = <2048>; }; syscon0: syscon@0 {
drivers/misc/Makefile +1 -0
@@ -16,6 +16,7 @@ obj-$(CONFIG_CROS_EC_I2C) += cros_ec_i2c.o obj-$(CONFIG_CROS_EC_SPI) += cros_ec_spi.o obj-$(CONFIG_SANDBOX) += p2sb_sandbox.o p2sb_emul.o obj-$(CONFIG_SANDBOX) += swap_case.o +obj-$(CONFIG_SANDBOX) += spi_eeprom_emul.o endif ifdef CONFIG_$(PHASE_)DM_I2C
drivers/misc/spi_eeprom_emul.c new file +130 -0
130 lines not on this page · raw message
drivers/spi/sandbox_spi.c +36 -1
@@ -77,6 +77,41 @@ static int sandbox_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)return 0 ; } +/** +* sandbox_spi_emul_get() - find the emulator for a SPI slave +* +* A slave can name a dedicated emulator through a "sandbox,emul" phandle (for +* example an SPI EEPROM). When no such phandle is present we fall back to the +* built-in SPI flash emulator, preserving the previous behaviour. +* +* @state: Sandbox state, used to cache the bound emulator per chip select +* @bus: SPI bus the slave is attached to +* @slave: SPI slave to find the emulator for +* @emulp: Returns the emulator device on success +* Return: 0 if OK, -ve on error +*/ +static int sandbox_spi_emul_get (struct sandbox_state *state,struct udevice *bus, +struct udevice *slave,struct udevice **emulp) +{ +struct sandbox_spi_info *info; +int ret; + + info = &state->spi[dev_seq (bus)][spi_chip_select (slave)]; +if (info->emul) { + *emulp = info->emul; +return 0 ; + } + + ret =uclass_get_device_by_phandle (UCLASS_SPI_EMUL , slave,"sandbox,emul" , + emulp); +if (!ret) { + info->emul = *emulp; +return 0 ; + } + +return sandbox_spi_get_emul (state, bus, slave, emulp); +} +static int sandbox_spi_xfer (struct udevice *slave,unsigned int bitlen,const void *dout,void *din,unsigned long flags) {
@@ -106,7 +141,7 @@ static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen, busnum, cs);return -ENOENT ; } - ret =sandbox_spi_get_emul (state, bus, slave, &emul); + ret =sandbox_spi_emul_get (state, bus, slave, &emul);if (ret) {printf ("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n" , __func__, busnum, cs, ret);
test/dm/Makefile +1 -0
@@ -121,6 +121,7 @@ obj-$(CONFIG_SMEM) += smem.o obj-$(CONFIG_SOC_DEVICE) += soc.o obj-$(CONFIG_SOUND) += sound.o obj-$(CONFIG_DM_SPI) += spi.o +obj-$(CONFIG_SPI_EEPROM) += spi_eeprom.o obj-$(CONFIG_SPMI) += spmi.o obj-y += syscon.o obj-$(CONFIG_RESET_SYSCON) += syscon-reset.o
test/dm/spi_eeprom.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.55.0
Quoted 15 lines · João Loureiro
This series adds a
UCLASS_SPI_EEPROMuclass, a driver for AT25-style parts, a sandbox emulator and a DM test.v7 addresses Simon's review of v6 2/3 and picks up his Reviewed-by on patches 1 and 2. Thanks, Simon.
On the status-register read in
probe(): it was diagnostic, not a presence check - and on SPI it cannot be one. There is no acknowledge, so an unpopulated socket does not make the transfer fail; MISO just floats and the read returns0x00or0xff. The only way the read could fail is a controller or bus-claim error, and failingprobe()for that was, as you say, too strong.probe()now ignores a failed read and only logs the value at debug level, with a comment saying why. Any real bus problem still surfaces on the firstspi_eeprom_read().
Quoted 1 line
[...]
Applied to u-boot/main, thanks!
[1/3] test: dm: sf: Get the emulator attached to the flash slave
commit: 0d9a51b7e549dbdc688f1a1c408b55c827efe338
[2/3] spi: Introduce initial driver-model support for SPI EEPROMs
commit: e882a98361facf5fe3c314fc508da265e0d1a004
[3/3] sandbox: spi: Add SPI EEPROM emulator and DM test
commit: 3b748d780a37592e66b9ecba629834ae9480a3c0Signature · 1 line
Tom