2 * Copyright (C) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <spi_flash.h>
12 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
17 #include <asm/state.h>
19 /* Test that we can find buses and chip-selects */
20 static int dm_test_spi_find(struct dm_test_state *dms)
22 struct sandbox_state *state = state_get_current();
23 struct spi_slave *slave;
24 struct udevice *bus, *dev;
25 const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 1;
26 struct spi_cs_info info;
29 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum,
33 * spi_post_bind() will bind devices to chip selects. Check this then
34 * remove the emulation and the slave device.
36 ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus));
37 ut_assertok(spi_cs_info(bus, cs, &info));
38 of_offset = info.dev->of_offset;
39 device_remove(info.dev);
40 device_unbind(info.dev);
43 * Even though the device is gone, the sandbox SPI drivers always
44 * reports that CS 0 is present
46 ut_assertok(spi_cs_info(bus, cs, &info));
47 ut_asserteq_ptr(NULL, info.dev);
49 /* This finds nothing because we removed the device */
50 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
51 ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
52 NULL, 0, &bus, &slave));
55 * This forces the device to be re-added, but there is no emulation
56 * connected so the probe will fail. We require that bus is left
57 * alone on failure, and that the spi_get_bus_and_cs() does not add
58 * a 'partially-inited' device.
60 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
61 ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
62 "spi_flash_std", "name", &bus,
64 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
65 ut_assertok(spi_cs_info(bus, cs, &info));
66 ut_asserteq_ptr(NULL, info.dev);
68 /* Add the emulation and try again */
69 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, of_offset,
71 ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
72 ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode,
73 "spi_flash_std", "name", &bus, &slave));
75 ut_assertok(spi_cs_info(bus, cs, &info));
76 ut_asserteq_ptr(info.dev, slave->dev);
78 /* We should be able to add something to another chip select */
79 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, of_offset,
81 ut_assertok(spi_get_bus_and_cs(busnum, cs_b, speed, mode,
82 "spi_flash_std", "name", &bus, &slave));
83 ut_assertok(spi_cs_info(bus, cs_b, &info));
84 ut_asserteq_ptr(info.dev, slave->dev);
87 * Since we are about to destroy all devices, we must tell sandbox
88 * to forget the emulation device
90 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
91 sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
95 DM_TEST(dm_test_spi_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
97 /* Test that sandbox SPI works correctly */
98 static int dm_test_spi_xfer(struct dm_test_state *dms)
100 struct spi_slave *slave;
102 const int busnum = 0, cs = 0, mode = 0;
103 const char dout[5] = {0x9f};
104 unsigned char din[5];
106 ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
108 ut_assertok(spi_claim_bus(slave));
109 ut_assertok(spi_xfer(slave, 40, dout, din,
110 SPI_XFER_BEGIN | SPI_XFER_END));
111 ut_asserteq(0xff, din[0]);
112 ut_asserteq(0x20, din[1]);
113 ut_asserteq(0x20, din[2]);
114 ut_asserteq(0x15, din[3]);
115 spi_release_bus(slave);
118 * Since we are about to destroy all devices, we must tell sandbox
119 * to forget the emulation device
121 #ifdef CONFIG_DM_SPI_FLASH
122 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
127 DM_TEST(dm_test_spi_xfer, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);