[v3, 6/7] test: dm: add comprehensive tests for NVMEM bit field operations

Simon Glass sjg at chromium.org
Tue Mar 31 21:50:26 CEST 2026


Hi Aswin,

On 2026-03-30T17:14:12, Aswin Murugan <aswin.murugan at oss.qualcomm.com> wrote:
> diff --git a/test/dm/reboot-mode.c b/test/dm/reboot-mode.c
> @@ -15,6 +15,8 @@
> +#include <nvmem.h>
> +#include <misc.h>

These NVMEM bit field tests have nothing to do with the reboot-mode
tests in this file. How about a new file like nvmem.c ?

> diff --git a/test/dm/reboot-mode.c b/test/dm/reboot-mode.c
> @@ -66,3 +68,138 @@
> +static int nvmem_test_write_raw(struct udevice *dev, uint offset,
> +                             const void *buf, uint size)
> +{
> +     return misc_write(dev, offset, buf, size);
> +}
> +
> +static int nvmem_test_read_raw(struct udevice *dev, uint offset,
> +                            void *buf, uint size)
> +{
> +     return misc_read(dev, offset, buf, size);
> +}

The device is UCLASS_I2C_EEPROM but you are calling misc_read/write()
which call device_get_ops(dev) expecting UCLASS_MISC ops. This will
interpret the I2C EEPROM ops as misc_ops and likely crash or return
garbage. You could use i2c_eeprom_read/write() here instead.

> diff --git a/test/dm/reboot-mode.c b/test/dm/reboot-mode.c
> @@ -66,3 +68,138 @@
> +     /* Test reg = <0x18 0x4>; bits = <4 12>: Spanning byte boundary */
> +     cell.offset = 0x18;
> +     cell.size = 4;
> +     cell.bit_offset = 4;
> +     cell.nbits = 12;
> +     hw_value_u16 = 0x000F;
> +     ut_assertok(nvmem_test_write_raw(nvmem_dev, cell.offset, (u8 *)&hw_value_u16, 2));
> +     value = 0xFFF;
> +     ut_assertok(nvmem_cell_write(&cell, &value, sizeof(value)));
> +     ut_assertok(nvmem_test_read_raw(nvmem_dev, cell.offset, (u8 *)&hw_value_u16, 2));
> +     ut_asserteq(0xFFFF, hw_value_u16);

Lower-case hex

The cell has size 4 but the raw seed write and verification only touch
2 bytes, so the remaining 2 bytes of the cell are uninited

This test (and the 'upper 2 bytes' test below) should call
nvmem_cell_read() to check that the bit extraction path works for
multi-byte spanning fields.

> diff --git a/test/dm/reboot-mode.c b/test/dm/reboot-mode.c
> @@ -66,3 +68,138 @@
> +     /* Test bit field exceeding cell size */
> +     cell.nvmem = nvmem_dev;
> +     cell.offset = 0xd;
> +     cell.size = 1;
> +     cell.bit_offset = 0;
> +     cell.nbits = 9;
> +
> +     value = 0xFF;
> +     ret = nvmem_cell_write(&cell, &value, sizeof(value));
> +     ut_assert(ret < 0);

Please can you use ut_asserteq(-EINVAL, ret) so we know exactly what
error is used.

Also, the error cases only test the write path - can you add calls to
nvmem_cell_read() calls too?

Regards,
Simon


More information about the U-Boot mailing list