blob: cc1fc4d69af824cfc01f779a909c1200bcca7681 [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
6#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -07007#include <command.h>
Simon Glass25348a42014-10-13 23:42:11 -06008#include <dm.h>
9#include <fdtdec.h>
Simon Glass90fcfb12018-11-06 15:21:40 -070010#include <mapmem.h>
11#include <os.h>
Simon Glass25348a42014-10-13 23:42:11 -060012#include <spi.h>
13#include <spi_flash.h>
14#include <asm/state.h>
Simon Glass36eee8c2018-11-06 15:21:41 -070015#include <asm/test.h>
Simon Glass25348a42014-10-13 23:42:11 -060016#include <dm/test.h>
17#include <dm/util.h>
Simon Glass75c4d412020-07-19 10:15:37 -060018#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glass25348a42014-10-13 23:42:11 -060020
Simon Glass90fcfb12018-11-06 15:21:40 -070021/* Simple test of sandbox SPI flash */
Joe Hershberger3a77be52015-05-20 14:27:27 -050022static int dm_test_spi_flash(struct unit_test_state *uts)
Simon Glass25348a42014-10-13 23:42:11 -060023{
Jagan Tekie58ca242020-05-13 18:16:39 +053024 struct udevice *dev;
Simon Glass90fcfb12018-11-06 15:21:40 -070025 int full_size = 0x200000;
26 int size = 0x10000;
27 u8 *src, *dst;
Simon Glass37ad0fe2019-10-20 21:31:47 -060028 uint map_size;
29 ulong map_base;
30 uint offset;
Simon Glass90fcfb12018-11-06 15:21:40 -070031 int i;
32
33 src = map_sysmem(0x20000, full_size);
34 ut_assertok(os_write_file("spi.bin", src, full_size));
35 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
36
37 dst = map_sysmem(0x20000 + full_size, full_size);
38 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
Simon Glassa3186e62020-05-10 12:52:45 -060039 ut_asserteq_mem(src, dst, size);
Simon Glass90fcfb12018-11-06 15:21:40 -070040
41 /* Erase */
42 ut_assertok(spi_flash_erase_dm(dev, 0, size));
43 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
44 for (i = 0; i < size; i++)
45 ut_asserteq(dst[i], 0xff);
46
47 /* Write some new data */
48 for (i = 0; i < size; i++)
49 src[i] = i;
50 ut_assertok(spi_flash_write_dm(dev, 0, size, src));
51 ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
Simon Glassa3186e62020-05-10 12:52:45 -060052 ut_asserteq_mem(src, dst, size);
Simon Glass90fcfb12018-11-06 15:21:40 -070053
Simon Glass37ad0fe2019-10-20 21:31:47 -060054 /* Check mapping */
55 ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
56 ut_asserteq(0x1000, map_base);
57 ut_asserteq(0x2000, map_size);
58 ut_asserteq(0x100, offset);
59
Simon Glass90fcfb12018-11-06 15:21:40 -070060 /*
61 * Since we are about to destroy all devices, we must tell sandbox
62 * to forget the emulation device
63 */
64 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
65
66 return 0;
67}
Simon Glass974dccd2020-07-28 19:41:12 -060068DM_TEST(dm_test_spi_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass90fcfb12018-11-06 15:21:40 -070069
70/* Functional test that sandbox SPI flash works correctly */
71static int dm_test_spi_flash_func(struct unit_test_state *uts)
72{
Simon Glass25348a42014-10-13 23:42:11 -060073 /*
74 * Create an empty test file and run the SPI flash tests. This is a
75 * long way from being a unit test, but it does test SPI device and
76 * emulator binding, probing, the SPI flash emulator including
77 * device tree decoding, plus the file-based backing store of SPI.
78 *
79 * More targeted tests could be created to perform the above steps
80 * one at a time. This might not increase test coverage much, but
81 * it would make bugs easier to find. It's not clear whether the
82 * benefit is worth the extra complexity.
83 */
84 ut_asserteq(0, run_command_list(
Simon Glassf250d472018-11-15 18:44:02 -070085 "host save hostfs - 0 spi.bin 200000;"
Simon Glass25348a42014-10-13 23:42:11 -060086 "sf probe;"
87 "sf test 0 10000", -1, 0));
88 /*
89 * Since we are about to destroy all devices, we must tell sandbox
90 * to forget the emulation device
91 */
92 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
93
94 return 0;
95}
Simon Glass974dccd2020-07-28 19:41:12 -060096DM_TEST(dm_test_spi_flash_func, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);