spi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <spi.h>
  9. #include <spi_flash.h>
  10. #include <asm/state.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/test.h>
  13. #include <dm/uclass-internal.h>
  14. #include <dm/util.h>
  15. #include <test/test.h>
  16. #include <test/ut.h>
  17. /* Test that we can find buses and chip-selects */
  18. static int dm_test_spi_find(struct unit_test_state *uts)
  19. {
  20. struct sandbox_state *state = state_get_current();
  21. struct spi_slave *slave;
  22. struct udevice *bus, *dev;
  23. const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 1;
  24. struct spi_cs_info info;
  25. ofnode node;
  26. ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum,
  27. false, &bus));
  28. /*
  29. * The post_bind() method will bind devices to chip selects. Check
  30. * this then remove the emulation and the slave device.
  31. */
  32. ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus));
  33. ut_assertok(spi_cs_info(bus, cs, &info));
  34. node = dev_ofnode(info.dev);
  35. device_remove(info.dev, DM_REMOVE_NORMAL);
  36. device_unbind(info.dev);
  37. /*
  38. * Even though the device is gone, the sandbox SPI drivers always
  39. * reports that CS 0 is present
  40. */
  41. ut_assertok(spi_cs_info(bus, cs, &info));
  42. ut_asserteq_ptr(NULL, info.dev);
  43. /* This finds nothing because we removed the device */
  44. ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
  45. ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
  46. NULL, 0, &bus, &slave));
  47. /*
  48. * This forces the device to be re-added, but there is no emulation
  49. * connected so the probe will fail. We require that bus is left
  50. * alone on failure, and that the spi_get_bus_and_cs() does not add
  51. * a 'partially-inited' device.
  52. */
  53. ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
  54. ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
  55. "jedec_spi_nor", "name", &bus,
  56. &slave));
  57. sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
  58. ut_assertok(spi_cs_info(bus, cs, &info));
  59. ut_asserteq_ptr(NULL, info.dev);
  60. /* Add the emulation and try again */
  61. ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node,
  62. "name"));
  63. ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
  64. ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode,
  65. "jedec_spi_nor", "name", &bus, &slave));
  66. ut_assertok(spi_cs_info(bus, cs, &info));
  67. ut_asserteq_ptr(info.dev, slave->dev);
  68. /* We should be able to add something to another chip select */
  69. ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node,
  70. "name"));
  71. ut_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode,
  72. "jedec_spi_nor", "name", &bus, &slave));
  73. ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info));
  74. ut_asserteq_ptr(NULL, info.dev);
  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(), busnum, cs);
  80. sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
  81. return 0;
  82. }
  83. DM_TEST(dm_test_spi_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  84. /* Test that sandbox SPI works correctly */
  85. static int dm_test_spi_xfer(struct unit_test_state *uts)
  86. {
  87. struct spi_slave *slave;
  88. struct udevice *bus;
  89. const int busnum = 0, cs = 0, mode = 0;
  90. const char dout[5] = {0x9f};
  91. unsigned char din[5];
  92. ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
  93. &bus, &slave));
  94. ut_assertok(spi_claim_bus(slave));
  95. ut_assertok(spi_xfer(slave, 40, dout, din,
  96. SPI_XFER_BEGIN | SPI_XFER_END));
  97. ut_asserteq(0xff, din[0]);
  98. ut_asserteq(0x20, din[1]);
  99. ut_asserteq(0x20, din[2]);
  100. ut_asserteq(0x15, din[3]);
  101. spi_release_bus(slave);
  102. /*
  103. * Since we are about to destroy all devices, we must tell sandbox
  104. * to forget the emulation device
  105. */
  106. #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
  107. sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
  108. #endif
  109. return 0;
  110. }
  111. DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);