blob: 3684d0217094354455540b3002ea0f455bc2fb63 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass25348a42014-10-13 23:42:11 -06002/*
3 * Copyright (C) 2013 Google, Inc
Simon Glass25348a42014-10-13 23:42:11 -06004 */
5
Simon Glassadaaa482019-11-14 12:57:43 -07006#include <command.h>
Simon Glass25348a42014-10-13 23:42:11 -06007#include <dm.h>
8#include <fdtdec.h>
Simon Glass90fcfb12018-11-06 15:21:40 -07009#include <mapmem.h>
10#include <os.h>
Simon Glass25348a42014-10-13 23:42:11 -060011#include <spi.h>
12#include <spi_flash.h>
13#include <asm/state.h>
Simon Glass36eee8c2018-11-06 15:21:41 -070014#include <asm/test.h>
Simon Glass25348a42014-10-13 23:42:11 -060015#include <dm/test.h>
16#include <dm/util.h>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glass25348a42014-10-13 23:42:11 -060019
Simon Glass90fcfb12018-11-06 15:21:40 -070020/* Simple test of sandbox SPI flash */
Joe Hershberger3a77be52015-05-20 14:27:27 -050021static int dm_test_spi_flash(struct unit_test_state *uts)
Simon Glass25348a42014-10-13 23:42:11 -060022{
Simon Glass61d04642021-03-15 18:11:17 +130023 struct udevice *dev, *emul;
Simon Glass90fcfb12018-11-06 15:21:40 -070024 int full_size = 0x200000;
25 int size = 0x10000;
26 u8 *src, *dst;
Simon Glass37ad0fe2019-10-20 21:31:47 -060027 uint map_size;
28 ulong map_base;
29 uint offset;
Simon Glass90fcfb12018-11-06 15:21:40 -070030 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));
Simon Glassa3186e62020-05-10 12:52:45 -060038 ut_asserteq_mem(src, dst, size);
Simon Glass90fcfb12018-11-06 15:21:40 -070039
40 /* Erase */
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 /* Write some new data */
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));
Simon Glassa3186e62020-05-10 12:52:45 -060051 ut_asserteq_mem(src, dst, size);
Simon Glass90fcfb12018-11-06 15:21:40 -070052
Simon Glass61d04642021-03-15 18:11:17 +130053 /* Try the write-protect stuff */
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
Simon Glass37ad0fe2019-10-20 21:31:47 -060061 /* Check mapping */
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
Simon Glass90fcfb12018-11-06 15:21:40 -070067 /*
68 * Since we are about to destroy all devices, we must tell sandbox
69 * to forget the emulation device
70 */
71 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
72
73 return 0;
74}
Simon Glass1a92f832024-08-22 07:57:48 -060075DM_TEST(dm_test_spi_flash, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass90fcfb12018-11-06 15:21:40 -070076
77/* Functional test that sandbox SPI flash works correctly */
78static int dm_test_spi_flash_func(struct unit_test_state *uts)
79{
Simon Glass25348a42014-10-13 23:42:11 -060080 /*
81 * Create an empty test file and run the SPI flash tests. This is a
82 * long way from being a unit test, but it does test SPI device and
83 * emulator binding, probing, the SPI flash emulator including
84 * device tree decoding, plus the file-based backing store of SPI.
85 *
86 * More targeted tests could be created to perform the above steps
87 * one at a time. This might not increase test coverage much, but
88 * it would make bugs easier to find. It's not clear whether the
89 * benefit is worth the extra complexity.
90 */
91 ut_asserteq(0, run_command_list(
Simon Glassf250d472018-11-15 18:44:02 -070092 "host save hostfs - 0 spi.bin 200000;"
Simon Glass25348a42014-10-13 23:42:11 -060093 "sf probe;"
94 "sf test 0 10000", -1, 0));
95 /*
96 * Since we are about to destroy all devices, we must tell sandbox
97 * to forget the emulation device
98 */
99 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
100
101 return 0;
102}
Simon Glass1a92f832024-08-22 07:57:48 -0600103DM_TEST(dm_test_spi_flash_func, UTF_SCAN_PDATA | UTF_SCAN_FDT);