spi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <asm/test.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/test.h>
  14. #include <dm/uclass-internal.h>
  15. #include <dm/util.h>
  16. #include <test/test.h>
  17. #include <test/ut.h>
  18. /* Test that we can find buses and chip-selects */
  19. static int dm_test_spi_find(struct unit_test_state *uts)
  20. {
  21. struct sandbox_state *state = state_get_current();
  22. struct spi_slave *slave;
  23. struct udevice *bus, *dev;
  24. const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 2;
  25. struct spi_cs_info info;
  26. ofnode node;
  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. "jedec_spi_nor", "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. "jedec_spi_nor", "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_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode,
  71. "jedec_spi_nor", "name", &bus, &slave));
  72. ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info));
  73. ut_asserteq_ptr(NULL, info.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, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  83. /* dm_test_spi_switch_slaves - Helper function to check whether spi_claim_bus
  84. * operates correctly with two spi slaves.
  85. *
  86. * Check that switching back and forth between two slaves claiming the bus
  87. * will update dm_spi_bus->speed and sandbox_spi bus speed/mode correctly.
  88. *
  89. * @uts - unit test state
  90. * @slave_a - first spi slave used for testing
  91. * @slave_b - second spi slave used for testing
  92. */
  93. static int dm_test_spi_switch_slaves(struct unit_test_state *uts,
  94. struct spi_slave *slave_a,
  95. struct spi_slave *slave_b)
  96. {
  97. struct udevice *bus;
  98. struct dm_spi_bus *bus_data;
  99. /* Check that slaves are on the same bus */
  100. ut_asserteq_ptr(dev_get_parent(slave_a->dev),
  101. dev_get_parent(slave_b->dev));
  102. bus = dev_get_parent(slave_a->dev);
  103. bus_data = dev_get_uclass_priv(bus);
  104. ut_assertok(spi_claim_bus(slave_a));
  105. ut_asserteq(slave_a->max_hz, bus_data->speed);
  106. ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus));
  107. ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus));
  108. spi_release_bus(slave_a);
  109. ut_assertok(spi_claim_bus(slave_b));
  110. ut_asserteq(slave_b->max_hz, bus_data->speed);
  111. ut_asserteq(slave_b->max_hz, sandbox_spi_get_speed(bus));
  112. ut_asserteq(slave_b->mode, sandbox_spi_get_mode(bus));
  113. spi_release_bus(slave_b);
  114. ut_assertok(spi_claim_bus(slave_a));
  115. ut_asserteq(slave_a->max_hz, bus_data->speed);
  116. ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus));
  117. ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus));
  118. spi_release_bus(slave_a);
  119. return 0;
  120. }
  121. static int dm_test_spi_claim_bus(struct unit_test_state *uts)
  122. {
  123. struct udevice *bus;
  124. struct spi_slave *slave_a, *slave_b;
  125. struct dm_spi_slave_plat *slave_plat;
  126. const int busnum = 0, cs_a = 0, cs_b = 1, mode = 0;
  127. /* Get spi slave on CS0 */
  128. ut_assertok(spi_get_bus_and_cs(busnum, cs_a, 1000000, mode, NULL, 0,
  129. &bus, &slave_a));
  130. /* Get spi slave on CS1 */
  131. ut_assertok(spi_get_bus_and_cs(busnum, cs_b, 1000000, mode, NULL, 0,
  132. &bus, &slave_b));
  133. /* Different max_hz, different mode. */
  134. ut_assert(slave_a->max_hz != slave_b->max_hz);
  135. ut_assert(slave_a->mode != slave_b->mode);
  136. dm_test_spi_switch_slaves(uts, slave_a, slave_b);
  137. /* Different max_hz, same mode. */
  138. slave_a->mode = slave_b->mode;
  139. dm_test_spi_switch_slaves(uts, slave_a, slave_b);
  140. /*
  141. * Same max_hz, different mode.
  142. * Restore original mode for slave_a, from platdata.
  143. */
  144. slave_plat = dev_get_parent_plat(slave_a->dev);
  145. slave_a->mode = slave_plat->mode;
  146. slave_a->max_hz = slave_b->max_hz;
  147. dm_test_spi_switch_slaves(uts, slave_a, slave_b);
  148. return 0;
  149. }
  150. DM_TEST(dm_test_spi_claim_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  151. /* Test that sandbox SPI works correctly */
  152. static int dm_test_spi_xfer(struct unit_test_state *uts)
  153. {
  154. struct spi_slave *slave;
  155. struct udevice *bus;
  156. const int busnum = 0, cs = 0, mode = 0;
  157. const char dout[5] = {0x9f};
  158. unsigned char din[5];
  159. ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
  160. &bus, &slave));
  161. ut_assertok(spi_claim_bus(slave));
  162. ut_assertok(spi_xfer(slave, 40, dout, din,
  163. SPI_XFER_BEGIN | SPI_XFER_END));
  164. ut_asserteq(0xff, din[0]);
  165. ut_asserteq(0x20, din[1]);
  166. ut_asserteq(0x20, din[2]);
  167. ut_asserteq(0x15, din[3]);
  168. spi_release_bus(slave);
  169. /*
  170. * Since we are about to destroy all devices, we must tell sandbox
  171. * to forget the emulation device
  172. */
  173. #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
  174. sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
  175. #endif
  176. return 0;
  177. }
  178. DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);