xref: /OK3568_Linux_fs/u-boot/test/dm/sf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <spi.h>
11 #include <spi_flash.h>
12 #include <asm/state.h>
13 #include <asm/test.h>
14 #include <dm/test.h>
15 #include <dm/util.h>
16 #include <test/ut.h>
17 
18 /* Test that sandbox SPI flash works correctly */
dm_test_spi_flash(struct unit_test_state * uts)19 static int dm_test_spi_flash(struct unit_test_state *uts)
20 {
21 	struct udevice *dev, *emul;
22 	int full_size = 0x200000;
23 	int size = 0x10000;
24 	u8 *src, *dst;
25 	uint map_size;
26 	ulong map_base;
27 	uint offset;
28 	int i;
29 
30 	src = map_sysmem(0x20000, full_size);
31 	ut_assertok(os_write_file("spi.bin", src, full_size));
32 	ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
33 
34 	dst = map_sysmem(0x20000 + full_size, full_size);
35 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
36 	ut_assertok(memcmp(src, dst, size));
37 
38 	/* Erase */
39 	ut_assertok(spi_flash_erase_dm(dev, 0, size));
40 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
41 	for (i = 0; i < size; i++)
42 		ut_asserteq(dst[i], 0xff);
43 
44 	/* Write some new data */
45 	for (i = 0; i < size; i++)
46 		src[i] = i;
47 	ut_assertok(spi_flash_write_dm(dev, 0, size, src));
48 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
49 	ut_assertok(memcmp(src, dst, size));
50 
51 	/* Try the write-protect stuff */
52 	ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
53 	ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
54 	sandbox_sf_set_block_protect(emul, 1);
55 	ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
56 	sandbox_sf_set_block_protect(emul, 0);
57 	ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
58 
59 	/* Check mapping */
60 	ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
61 	ut_asserteq(0x1000, map_base);
62 	ut_asserteq(0x2000, map_size);
63 	ut_asserteq(0x100, offset);
64 
65 	/*
66 	 * Since we are about to destroy all devices, we must tell sandbox
67 	 * to forget the emulation device
68 	 */
69 	sandbox_sf_unbind_emul(state_get_current(), 0, 0);
70 
71 	return 0;
72 }
73 DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
74 
75 /* Functional test that sandbox SPI flash works correctly */
dm_test_spi_flash_func(struct unit_test_state * uts)76 static int dm_test_spi_flash_func(struct unit_test_state *uts)
77 {
78 	/*
79 	 * Create an empty test file and run the SPI flash tests. This is a
80 	 * long way from being a unit test, but it does test SPI device and
81 	 * emulator binding, probing, the SPI flash emulator including
82 	 * device tree decoding, plus the file-based backing store of SPI.
83 	 *
84 	 * More targeted tests could be created to perform the above steps
85 	 * one at a time. This might not increase test coverage much, but
86 	 * it would make bugs easier to find. It's not clear whether the
87 	 * benefit is worth the extra complexity.
88 	 */
89 	ut_asserteq(0, run_command_list(
90 		"sb save hostfs - 0 spi.bin 200000;"
91 		"sf probe;"
92 		"sf test 0 10000", -1,  0));
93 	/*
94 	 * Since we are about to destroy all devices, we must tell sandbox
95 	 * to forget the emulation device
96 	 */
97 	sandbox_sf_unbind_emul(state_get_current(), 0, 0);
98 
99 	return 0;
100 }
101 DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
102