1
2
3
4
5
6#include <common.h>
7#include <command.h>
8#include <dm.h>
9#include <fdtdec.h>
10#include <mapmem.h>
11#include <os.h>
12#include <spi.h>
13#include <spi_flash.h>
14#include <asm/state.h>
15#include <asm/test.h>
16#include <dm/test.h>
17#include <dm/util.h>
18#include <test/ut.h>
19
20
21static int dm_test_spi_flash(struct unit_test_state *uts)
22{
23 struct udevice *dev, *emul;
24 int full_size = 0x200000;
25 int size = 0x10000;
26 u8 *src, *dst;
27 uint map_size;
28 ulong map_base;
29 uint offset;
30 int i;
31
32 src = map_sysmem(0x20000, full_size);
33 ut_assertok(os_write_file("spi.bin", src, full_size));
34 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
35
36 dst = map_sysmem(0x20000 + full_size, full_size);
37 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
38 ut_assertok(memcmp(src, dst, size));
39
40
41 ut_assertok(spi_flash_erase_dm(dev, 0, size));
42 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
43 for (i = 0; i < size; i++)
44 ut_asserteq(dst[i], 0xff);
45
46
47 for (i = 0; i < size; i++)
48 src[i] = i;
49 ut_assertok(spi_flash_write_dm(dev, 0, size, src));
50 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
51 ut_assertok(memcmp(src, dst, size));
52
53
54 ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
55 ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
56 sandbox_sf_set_block_protect(emul, 1);
57 ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
58 sandbox_sf_set_block_protect(emul, 0);
59 ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
60
61
62 ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
63 ut_asserteq(0x1000, map_base);
64 ut_asserteq(0x2000, map_size);
65 ut_asserteq(0x100, offset);
66
67
68
69
70
71 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
72
73 return 0;
74}
75DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
76
77
78static int dm_test_spi_flash_func(struct unit_test_state *uts)
79{
80
81
82
83
84
85
86
87
88
89
90
91 ut_asserteq(0, run_command_list(
92 "host save hostfs - 0 spi.bin 200000;"
93 "sf probe;"
94 "sf test 0 10000", -1, 0));
95
96
97
98
99 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
100
101 return 0;
102}
103DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
104