sf.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/ut.h>
  18. /* Simple test of sandbox SPI flash */
  19. static int dm_test_spi_flash(struct unit_test_state *uts)
  20. {
  21. struct udevice *dev;
  22. int full_size = 0x200000;
  23. int size = 0x10000;
  24. u8 *src, *dst;
  25. uint map_size;
  26. ulong map_base;
  27. uint offset;
  28. int i;
  29. src = map_sysmem(0x20000, full_size);
  30. ut_assertok(os_write_file("spi.bin", src, full_size));
  31. ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
  32. dst = map_sysmem(0x20000 + full_size, full_size);
  33. ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
  34. ut_asserteq_mem(src, dst, size);
  35. /* Erase */
  36. ut_assertok(spi_flash_erase_dm(dev, 0, size));
  37. ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
  38. for (i = 0; i < size; i++)
  39. ut_asserteq(dst[i], 0xff);
  40. /* Write some new data */
  41. for (i = 0; i < size; i++)
  42. src[i] = i;
  43. ut_assertok(spi_flash_write_dm(dev, 0, size, src));
  44. ut_assertok(spi_flash_read_dm(dev, 0, size, dst));
  45. ut_asserteq_mem(src, dst, size);
  46. /* Check mapping */
  47. ut_assertok(dm_spi_get_mmap(dev, &map_base, &map_size, &offset));
  48. ut_asserteq(0x1000, map_base);
  49. ut_asserteq(0x2000, map_size);
  50. ut_asserteq(0x100, offset);
  51. /*
  52. * Since we are about to destroy all devices, we must tell sandbox
  53. * to forget the emulation device
  54. */
  55. sandbox_sf_unbind_emul(state_get_current(), 0, 0);
  56. return 0;
  57. }
  58. DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
  59. /* Functional test that sandbox SPI flash works correctly */
  60. static int dm_test_spi_flash_func(struct unit_test_state *uts)
  61. {
  62. /*
  63. * Create an empty test file and run the SPI flash tests. This is a
  64. * long way from being a unit test, but it does test SPI device and
  65. * emulator binding, probing, the SPI flash emulator including
  66. * device tree decoding, plus the file-based backing store of SPI.
  67. *
  68. * More targeted tests could be created to perform the above steps
  69. * one at a time. This might not increase test coverage much, but
  70. * it would make bugs easier to find. It's not clear whether the
  71. * benefit is worth the extra complexity.
  72. */
  73. ut_asserteq(0, run_command_list(
  74. "host save hostfs - 0 spi.bin 200000;"
  75. "sf probe;"
  76. "sf test 0 10000", -1, 0));
  77. /*
  78. * Since we are about to destroy all devices, we must tell sandbox
  79. * to forget the emulation device
  80. */
  81. sandbox_sf_unbind_emul(state_get_current(), 0, 0);
  82. return 0;
  83. }
  84. DM_TEST(dm_test_spi_flash_func, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);