spi.c 3.9 KB

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