xref: /rk3399_rockchip-uboot/test/dm/sf.c (revision fcdd83d4455871c49a93236c3f1cb964727f8c0c)
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 */
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 	int i;
26 
27 	src = map_sysmem(0x20000, full_size);
28 	ut_assertok(os_write_file("spi.bin", src, full_size));
29 	ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
30 
31 	dst = map_sysmem(0x20000 + full_size, full_size);
32 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
33 	ut_assertok(memcmp(src, dst, size));
34 
35 	/* Erase */
36 	ut_assertok(spi_flash_erase_dm(dev, 0, size));
37 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
38 	for (i = 0; i < size; i++)
39 		ut_asserteq(dst[i], 0xff);
40 
41 	/* Write some new data */
42 	for (i = 0; i < size; i++)
43 		src[i] = i;
44 	ut_assertok(spi_flash_write_dm(dev, 0, size, src));
45 	ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
46 	ut_assertok(memcmp(src, dst, size));
47 
48 	/* Try the write-protect stuff */
49 	ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul));
50 	ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
51 	sandbox_sf_set_block_protect(emul, 1);
52 	ut_asserteq(1, spl_flash_get_sw_write_prot(dev));
53 	sandbox_sf_set_block_protect(emul, 0);
54 	ut_asserteq(0, spl_flash_get_sw_write_prot(dev));
55 
56 	/*
57 	 * Since we are about to destroy all devices, we must tell sandbox
58 	 * to forget the emulation device
59 	 */
60 	sandbox_sf_unbind_emul(state_get_current(), 0, 0);
61 
62 	return 0;
63 }
64 DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
65 
66 /* Functional test that sandbox SPI flash works correctly */
67 static int dm_test_spi_flash_func(struct unit_test_state *uts)
68 {
69 	/*
70 	 * Create an empty test file and run the SPI flash tests. This is a
71 	 * long way from being a unit test, but it does test SPI device and
72 	 * emulator binding, probing, the SPI flash emulator including
73 	 * device tree decoding, plus the file-based backing store of SPI.
74 	 *
75 	 * More targeted tests could be created to perform the above steps
76 	 * one at a time. This might not increase test coverage much, but
77 	 * it would make bugs easier to find. It's not clear whether the
78 	 * benefit is worth the extra complexity.
79 	 */
80 	ut_asserteq(0, run_command_list(
81 		"sb save hostfs - 0 spi.bin 200000;"
82 		"sf probe;"
83 		"sf test 0 10000", -1,  0));
84 	/*
85 	 * Since we are about to destroy all devices, we must tell sandbox
86 	 * to forget the emulation device
87 	 */
88 	sandbox_sf_unbind_emul(state_get_current(), 0, 0);
89 
90 	return 0;
91 }
92 DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
93