183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20ae0cb7bSSimon Glass /*
30ae0cb7bSSimon Glass * Copyright (C) 2013 Google, Inc
40ae0cb7bSSimon Glass */
50ae0cb7bSSimon Glass
60ae0cb7bSSimon Glass #include <common.h>
70ae0cb7bSSimon Glass #include <dm.h>
80ae0cb7bSSimon Glass #include <fdtdec.h>
9f9d49f92SSimon Glass #include <mapmem.h>
10f9d49f92SSimon Glass #include <os.h>
110ae0cb7bSSimon Glass #include <spi.h>
120ae0cb7bSSimon Glass #include <spi_flash.h>
130ae0cb7bSSimon Glass #include <asm/state.h>
14a58986caSSimon Glass #include <asm/test.h>
150ae0cb7bSSimon Glass #include <dm/test.h>
160ae0cb7bSSimon Glass #include <dm/util.h>
17e721b882SJoe Hershberger #include <test/ut.h>
180ae0cb7bSSimon Glass
19f9d49f92SSimon Glass /* Simple test of sandbox SPI flash */
dm_test_spi_flash(struct unit_test_state * uts)20e721b882SJoe Hershberger static int dm_test_spi_flash(struct unit_test_state *uts)
210ae0cb7bSSimon Glass {
22f9d49f92SSimon Glass struct udevice *dev, *emul;
23f9d49f92SSimon Glass int full_size = 0x200000;
24f9d49f92SSimon Glass int size = 0x10000;
25f9d49f92SSimon Glass u8 *src, *dst;
26f9d49f92SSimon Glass int i;
27f9d49f92SSimon Glass
28f9d49f92SSimon Glass src = map_sysmem(0x20000, full_size);
29f9d49f92SSimon Glass ut_assertok(os_write_file("spi.bin", src, full_size));
30f9d49f92SSimon Glass ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
31f9d49f92SSimon Glass
32f9d49f92SSimon Glass dst = map_sysmem(0x20000 + full_size, full_size);
33f9d49f92SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
34f9d49f92SSimon Glass ut_assertok(memcmp(src, dst, size));
35f9d49f92SSimon Glass
36f9d49f92SSimon Glass /* Erase */
37f9d49f92SSimon Glass ut_assertok(spi_flash_erase_dm(dev, 0, size));
38f9d49f92SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
39f9d49f92SSimon Glass for (i = 0; i < size; i++)
40f9d49f92SSimon Glass ut_asserteq(dst[i], 0xff);
41f9d49f92SSimon Glass
42f9d49f92SSimon Glass /* Write some new data */
43f9d49f92SSimon Glass for (i = 0; i < size; i++)
44f9d49f92SSimon Glass src[i] = i;
45f9d49f92SSimon Glass ut_assertok(spi_flash_write_dm(dev, 0, size, src));
46f9d49f92SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
47f9d49f92SSimon Glass ut_assertok(memcmp(src, dst, size));
48f9d49f92SSimon Glass
49a58986caSSimon Glass /* Try the write-protect stuff */
50a58986caSSimon Glass ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
51a58986caSSimon Glass ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
52a58986caSSimon Glass sandbox_sf_set_block_protect(emul, 1);
53a58986caSSimon Glass ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
54a58986caSSimon Glass sandbox_sf_set_block_protect(emul, 0);
55a58986caSSimon Glass ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
56a58986caSSimon Glass
57f9d49f92SSimon Glass /*
58f9d49f92SSimon Glass * Since we are about to destroy all devices, we must tell sandbox
59f9d49f92SSimon Glass * to forget the emulation device
60f9d49f92SSimon Glass */
61f9d49f92SSimon Glass sandbox_sf_unbind_emul(state_get_current(), 0, 0);
62f9d49f92SSimon Glass
63f9d49f92SSimon Glass return 0;
64f9d49f92SSimon Glass }
65f9d49f92SSimon Glass DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
66f9d49f92SSimon Glass
67f9d49f92SSimon Glass /* Functional test that sandbox SPI flash works correctly */
dm_test_spi_flash_func(struct unit_test_state * uts)68f9d49f92SSimon Glass static int dm_test_spi_flash_func(struct unit_test_state *uts)
69f9d49f92SSimon Glass {
700ae0cb7bSSimon Glass /*
710ae0cb7bSSimon Glass * Create an empty test file and run the SPI flash tests. This is a
720ae0cb7bSSimon Glass * long way from being a unit test, but it does test SPI device and
730ae0cb7bSSimon Glass * emulator binding, probing, the SPI flash emulator including
740ae0cb7bSSimon Glass * device tree decoding, plus the file-based backing store of SPI.
750ae0cb7bSSimon Glass *
760ae0cb7bSSimon Glass * More targeted tests could be created to perform the above steps
770ae0cb7bSSimon Glass * one at a time. This might not increase test coverage much, but
780ae0cb7bSSimon Glass * it would make bugs easier to find. It's not clear whether the
790ae0cb7bSSimon Glass * benefit is worth the extra complexity.
800ae0cb7bSSimon Glass */
810ae0cb7bSSimon Glass ut_asserteq(0, run_command_list(
82*6d07d63dSSimon Glass "host save hostfs - 0 spi.bin 200000;"
830ae0cb7bSSimon Glass "sf probe;"
840ae0cb7bSSimon Glass "sf test 0 10000", -1, 0));
850ae0cb7bSSimon Glass /*
860ae0cb7bSSimon Glass * Since we are about to destroy all devices, we must tell sandbox
870ae0cb7bSSimon Glass * to forget the emulation device
880ae0cb7bSSimon Glass */
890ae0cb7bSSimon Glass sandbox_sf_unbind_emul(state_get_current(), 0, 0);
900ae0cb7bSSimon Glass
910ae0cb7bSSimon Glass return 0;
920ae0cb7bSSimon Glass }
93f9d49f92SSimon Glass DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
94