10ae0cb7bSSimon Glass /*
20ae0cb7bSSimon Glass * Copyright (C) 2013 Google, Inc
30ae0cb7bSSimon Glass *
40ae0cb7bSSimon Glass * SPDX-License-Identifier: GPL-2.0+
50ae0cb7bSSimon Glass */
60ae0cb7bSSimon Glass
70ae0cb7bSSimon Glass #include <common.h>
80ae0cb7bSSimon Glass #include <dm.h>
90ae0cb7bSSimon Glass #include <fdtdec.h>
100ae0cb7bSSimon Glass #include <spi.h>
110ae0cb7bSSimon Glass #include <spi_flash.h>
120ae0cb7bSSimon Glass #include <asm/state.h>
13b6e92505SSimon Glass #include <asm/test.h>
140ae0cb7bSSimon Glass #include <dm/test.h>
150ae0cb7bSSimon Glass #include <dm/util.h>
16e721b882SJoe Hershberger #include <test/ut.h>
170ae0cb7bSSimon Glass
180ae0cb7bSSimon Glass /* Test that sandbox SPI flash works correctly */
dm_test_spi_flash(struct unit_test_state * uts)19e721b882SJoe Hershberger static int dm_test_spi_flash(struct unit_test_state *uts)
200ae0cb7bSSimon Glass {
21b6e92505SSimon Glass struct udevice *dev, *emul;
22b6e92505SSimon Glass int full_size = 0x200000;
23b6e92505SSimon Glass int size = 0x10000;
24b6e92505SSimon Glass u8 *src, *dst;
25*7ac3b0edSSimon Glass uint map_size;
26*7ac3b0edSSimon Glass ulong map_base;
27*7ac3b0edSSimon Glass uint offset;
28b6e92505SSimon Glass int i;
29b6e92505SSimon Glass
30b6e92505SSimon Glass src = map_sysmem(0x20000, full_size);
31b6e92505SSimon Glass ut_assertok(os_write_file("spi.bin", src, full_size));
32b6e92505SSimon Glass ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
33b6e92505SSimon Glass
34b6e92505SSimon Glass dst = map_sysmem(0x20000 + full_size, full_size);
35b6e92505SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
36b6e92505SSimon Glass ut_assertok(memcmp(src, dst, size));
37b6e92505SSimon Glass
38b6e92505SSimon Glass /* Erase */
39b6e92505SSimon Glass ut_assertok(spi_flash_erase_dm(dev, 0, size));
40b6e92505SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
41b6e92505SSimon Glass for (i = 0; i < size; i++)
42b6e92505SSimon Glass ut_asserteq(dst[i], 0xff);
43b6e92505SSimon Glass
44b6e92505SSimon Glass /* Write some new data */
45b6e92505SSimon Glass for (i = 0; i < size; i++)
46b6e92505SSimon Glass src[i] = i;
47b6e92505SSimon Glass ut_assertok(spi_flash_write_dm(dev, 0, size, src));
48b6e92505SSimon Glass ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
49b6e92505SSimon Glass ut_assertok(memcmp(src, dst, size));
50b6e92505SSimon Glass
51b6e92505SSimon Glass /* Try the write-protect stuff */
52b6e92505SSimon Glass ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
53b6e92505SSimon Glass ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
54b6e92505SSimon Glass sandbox_sf_set_block_protect(emul, 1);
55b6e92505SSimon Glass ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
56b6e92505SSimon Glass sandbox_sf_set_block_protect(emul, 0);
57b6e92505SSimon Glass ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
58b6e92505SSimon Glass
59*7ac3b0edSSimon Glass /* Check mapping */
60*7ac3b0edSSimon Glass ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
61*7ac3b0edSSimon Glass ut_asserteq(0x1000, map_base);
62*7ac3b0edSSimon Glass ut_asserteq(0x2000, map_size);
63*7ac3b0edSSimon Glass ut_asserteq(0x100, offset);
64*7ac3b0edSSimon Glass
65b6e92505SSimon Glass /*
66b6e92505SSimon Glass * Since we are about to destroy all devices, we must tell sandbox
67b6e92505SSimon Glass * to forget the emulation device
68b6e92505SSimon Glass */
69b6e92505SSimon Glass sandbox_sf_unbind_emul(state_get_current(), 0, 0);
70b6e92505SSimon Glass
71b6e92505SSimon Glass return 0;
72b6e92505SSimon Glass }
73b6e92505SSimon Glass DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
74b6e92505SSimon Glass
75b6e92505SSimon Glass /* Functional test that sandbox SPI flash works correctly */
dm_test_spi_flash_func(struct unit_test_state * uts)76b6e92505SSimon Glass static int dm_test_spi_flash_func(struct unit_test_state *uts)
77b6e92505SSimon Glass {
780ae0cb7bSSimon Glass /*
790ae0cb7bSSimon Glass * Create an empty test file and run the SPI flash tests. This is a
800ae0cb7bSSimon Glass * long way from being a unit test, but it does test SPI device and
810ae0cb7bSSimon Glass * emulator binding, probing, the SPI flash emulator including
820ae0cb7bSSimon Glass * device tree decoding, plus the file-based backing store of SPI.
830ae0cb7bSSimon Glass *
840ae0cb7bSSimon Glass * More targeted tests could be created to perform the above steps
850ae0cb7bSSimon Glass * one at a time. This might not increase test coverage much, but
860ae0cb7bSSimon Glass * it would make bugs easier to find. It's not clear whether the
870ae0cb7bSSimon Glass * benefit is worth the extra complexity.
880ae0cb7bSSimon Glass */
890ae0cb7bSSimon Glass ut_asserteq(0, run_command_list(
90b5493d17SSimon Glass "sb save hostfs - 0 spi.bin 200000;"
910ae0cb7bSSimon Glass "sf probe;"
920ae0cb7bSSimon Glass "sf test 0 10000", -1, 0));
930ae0cb7bSSimon Glass /*
940ae0cb7bSSimon Glass * Since we are about to destroy all devices, we must tell sandbox
950ae0cb7bSSimon Glass * to forget the emulation device
960ae0cb7bSSimon Glass */
970ae0cb7bSSimon Glass sandbox_sf_unbind_emul(state_get_current(), 0, 0);
980ae0cb7bSSimon Glass
990ae0cb7bSSimon Glass return 0;
1000ae0cb7bSSimon Glass }
1010ae0cb7bSSimon Glass DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
102