sf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <mapmem.h>
  10. #include <os.h>
  11. #include <spi.h>
  12. #include <spi_flash.h>
  13. #include <asm/state.h>
  14. #include <asm/test.h>
  15. #include <dm/test.h>
  16. #include <dm/util.h>
  17. #include <test/test.h>
  18. #include <test/ut.h>
  19. /* Simple test of sandbox SPI flash */
  20. static int dm_test_spi_flash(struct unit_test_state *uts)
  21. {
  22. struct udevice *dev;
  23. int full_size = 0x200000;
  24. int size = 0x10000;
  25. u8 *src, *dst;
  26. uint map_size;
  27. ulong map_base;
  28. uint offset;
  29. int i;
  30. src = map_sysmem(0x20000, full_size);
  31. ut_assertok(os_write_file("spi.bin", src, full_size));
  32. ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
  33. dst = map_sysmem(0x20000 + full_size, full_size);
  34. ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
  35. ut_asserteq_mem(src, dst, size);
  36. /* Erase */
  37. ut_assertok(spi_flash_erase_dm(dev, 0, size));
  38. ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
  39. for (i = 0; i < size; i++)
  40. ut_asserteq(dst[i], 0xff);
  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_asserteq_mem(src, dst, size);
  47. /* Check mapping */
  48. ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
  49. ut_asserteq(0x1000, map_base);
  50. ut_asserteq(0x2000, map_size);
  51. ut_asserteq(0x100, offset);
  52. /*
  53. * Since we are about to destroy all devices, we must tell sandbox
  54. * to forget the emulation device
  55. */
  56. sandbox_sf_unbind_emul(state_get_current(), 0, 0);
  57. return 0;
  58. }
  59. DM_TEST(dm_test_spi_flash, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  60. /* Functional test that sandbox SPI flash works correctly */
  61. static int dm_test_spi_flash_func(struct unit_test_state *uts)
  62. {
  63. /*
  64. * Create an empty test file and run the SPI flash tests. This is a
  65. * long way from being a unit test, but it does test SPI device and
  66. * emulator binding, probing, the SPI flash emulator including
  67. * device tree decoding, plus the file-based backing store of SPI.
  68. *
  69. * More targeted tests could be created to perform the above steps
  70. * one at a time. This might not increase test coverage much, but
  71. * it would make bugs easier to find. It's not clear whether the
  72. * benefit is worth the extra complexity.
  73. */
  74. ut_asserteq(0, run_command_list(
  75. "host save hostfs - 0 spi.bin 200000;"
  76. "sf probe;"
  77. "sf test 0 10000", -1, 0));
  78. /*
  79. * Since we are about to destroy all devices, we must tell sandbox
  80. * to forget the emulation device
  81. */
  82. sandbox_sf_unbind_emul(state_get_current(), 0, 0);
  83. return 0;
  84. }
  85. DM_TEST(dm_test_spi_flash_func, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);