blob: b23e7f8edd469c3e5a38a50352f47917e334e67f [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>
7#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 Glass25348a42014-10-13 23:42:11 -060014#include <dm/test.h>
15#include <dm/util.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050016#include <test/ut.h>
Simon Glass25348a42014-10-13 23:42:11 -060017
Simon Glass90fcfb12018-11-06 15:21:40 -070018/* Simple test of sandbox SPI flash */
Joe Hershberger3a77be52015-05-20 14:27:27 -050019static int dm_test_spi_flash(struct unit_test_state *uts)
Simon Glass25348a42014-10-13 23:42:11 -060020{
Simon Glass90fcfb12018-11-06 15:21:40 -070021 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 /*
49 * Since we are about to destroy all devices, we must tell sandbox
50 * to forget the emulation device
51 */
52 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
53
54 return 0;
55}
56DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
57
58/* Functional test that sandbox SPI flash works correctly */
59static int dm_test_spi_flash_func(struct unit_test_state *uts)
60{
Simon Glass25348a42014-10-13 23:42:11 -060061 /*
62 * Create an empty test file and run the SPI flash tests. This is a
63 * long way from being a unit test, but it does test SPI device and
64 * emulator binding, probing, the SPI flash emulator including
65 * device tree decoding, plus the file-based backing store of SPI.
66 *
67 * More targeted tests could be created to perform the above steps
68 * one at a time. This might not increase test coverage much, but
69 * it would make bugs easier to find. It's not clear whether the
70 * benefit is worth the extra complexity.
71 */
72 ut_asserteq(0, run_command_list(
Simon Glass73a04452014-12-02 13:17:31 -070073 "sb save hostfs - 0 spi.bin 200000;"
Simon Glass25348a42014-10-13 23:42:11 -060074 "sf probe;"
75 "sf test 0 10000", -1, 0));
76 /*
77 * Since we are about to destroy all devices, we must tell sandbox
78 * to forget the emulation device
79 */
80 sandbox_sf_unbind_emul(state_get_current(), 0, 0);
81
82 return 0;
83}
Simon Glass90fcfb12018-11-06 15:21:40 -070084DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);