1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Google, Inc
12 #include <spi_flash.h>
13 #include <asm/state.h>
19 /* Simple test of sandbox SPI flash */
20 static int dm_test_spi_flash(struct unit_test_state *uts)
22 struct udevice *dev, *emul;
23 int full_size = 0x200000;
28 src = map_sysmem(0x20000, full_size);
29 ut_assertok(os_write_file("spi.bin", src, full_size));
30 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
32 dst = map_sysmem(0x20000 + full_size, full_size);
33 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
34 ut_assertok(memcmp(src, dst, size));
37 ut_assertok(spi_flash_erase_dm(dev, 0, size));
38 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
39 for (i = 0; i < size; i++)
40 ut_asserteq(dst[i], 0xff);
42 /* Write some new data */
43 for (i = 0; i < size; i++)
45 ut_assertok(spi_flash_write_dm(dev, 0, size, src));
46 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
47 ut_assertok(memcmp(src, dst, size));
49 /* Try the write-protect stuff */
50 ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
51 ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
52 sandbox_sf_set_block_protect(emul, 1);
53 ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
54 sandbox_sf_set_block_protect(emul, 0);
55 ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
58 * Since we are about to destroy all devices, we must tell sandbox
59 * to forget the emulation device
61 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
65 DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
67 /* Functional test that sandbox SPI flash works correctly */
68 static int dm_test_spi_flash_func(struct unit_test_state *uts)
71 * Create an empty test file and run the SPI flash tests. This is a
72 * long way from being a unit test, but it does test SPI device and
73 * emulator binding, probing, the SPI flash emulator including
74 * device tree decoding, plus the file-based backing store of SPI.
76 * More targeted tests could be created to perform the above steps
77 * one at a time. This might not increase test coverage much, but
78 * it would make bugs easier to find. It's not clear whether the
79 * benefit is worth the extra complexity.
81 ut_asserteq(0, run_command_list(
82 "host save hostfs - 0 spi.bin 200000;"
84 "sf test 0 10000", -1, 0));
86 * Since we are about to destroy all devices, we must tell sandbox
87 * to forget the emulation device
89 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
93 DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);