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