Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 288b29e | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 10 | #include <mapmem.h> |
| 11 | #include <os.h> |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 12 | #include <spi.h> |
| 13 | #include <spi_flash.h> |
| 14 | #include <asm/state.h> |
Simon Glass | a58986c | 2018-11-06 15:21:41 -0700 | [diff] [blame] | 15 | #include <asm/test.h> |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 16 | #include <dm/test.h> |
| 17 | #include <dm/util.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 18 | #include <test/test.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | #include <test/ut.h> |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 20 | |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 21 | /* Simple test of sandbox SPI flash */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 22 | static int dm_test_spi_flash(struct unit_test_state *uts) |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 23 | { |
Simon Glass | b3b60f5 | 2021-03-15 18:11:17 +1300 | [diff] [blame] | 24 | struct udevice *dev, *emul; |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 25 | int full_size = 0x200000; |
| 26 | int size = 0x10000; |
| 27 | u8 *src, *dst; |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 28 | uint map_size; |
| 29 | ulong map_base; |
| 30 | uint offset; |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 31 | 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 Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 39 | ut_asserteq_mem(src, dst, size); |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 40 | |
| 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 Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 52 | ut_asserteq_mem(src, dst, size); |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 53 | |
Simon Glass | b3b60f5 | 2021-03-15 18:11:17 +1300 | [diff] [blame] | 54 | /* Try the write-protect stuff */ |
| 55 | ut_assertok(uclass_first_device_err(UCLASS_SPI_EMUL, &emul)); |
| 56 | ut_asserteq(0, spl_flash_get_sw_write_prot(dev)); |
| 57 | sandbox_sf_set_block_protect(emul, 1); |
| 58 | ut_asserteq(1, spl_flash_get_sw_write_prot(dev)); |
| 59 | sandbox_sf_set_block_protect(emul, 0); |
| 60 | ut_asserteq(0, spl_flash_get_sw_write_prot(dev)); |
| 61 | |
Simon Glass | c53b318 | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 62 | /* Check mapping */ |
| 63 | ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset)); |
| 64 | ut_asserteq(0x1000, map_base); |
| 65 | ut_asserteq(0x2000, map_size); |
| 66 | ut_asserteq(0x100, offset); |
| 67 | |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Since we are about to destroy all devices, we must tell sandbox |
| 70 | * to forget the emulation device |
| 71 | */ |
| 72 | sandbox_sf_unbind_emul(state_get_current(), 0, 0); |
| 73 | |
| 74 | return 0; |
| 75 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 76 | DM_TEST(dm_test_spi_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | f9d49f9 | 2018-11-06 15:21:40 -0700 | [diff] [blame] | 77 | |
| 78 | /* Functional test that sandbox SPI flash works correctly */ |
| 79 | static int dm_test_spi_flash_func(struct unit_test_state *uts) |
| 80 | { |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 81 | /* |
| 82 | * Create an empty test file and run the SPI flash tests. This is a |
| 83 | * long way from being a unit test, but it does test SPI device and |
| 84 | * emulator binding, probing, the SPI flash emulator including |
| 85 | * device tree decoding, plus the file-based backing store of SPI. |
| 86 | * |
| 87 | * More targeted tests could be created to perform the above steps |
| 88 | * one at a time. This might not increase test coverage much, but |
| 89 | * it would make bugs easier to find. It's not clear whether the |
| 90 | * benefit is worth the extra complexity. |
| 91 | */ |
| 92 | ut_asserteq(0, run_command_list( |
Simon Glass | 6d07d63 | 2018-11-15 18:44:02 -0700 | [diff] [blame] | 93 | "host save hostfs - 0 spi.bin 200000;" |
Simon Glass | 0ae0cb7 | 2014-10-13 23:42:11 -0600 | [diff] [blame] | 94 | "sf probe;" |
| 95 | "sf test 0 10000", -1, 0)); |
| 96 | /* |
| 97 | * Since we are about to destroy all devices, we must tell sandbox |
| 98 | * to forget the emulation device |
| 99 | */ |
| 100 | sandbox_sf_unbind_emul(state_get_current(), 0, 0); |
| 101 | |
| 102 | return 0; |
| 103 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 104 | DM_TEST(dm_test_spi_flash_func, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |