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