sf.c 2.7 KB

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