blob: 61b5b2548c42e4e9895ef95345b5e9e99bc9c52a [file] [log] [blame]
Simon Glassabae56d2014-10-13 23:42:10 -06001/*
2 * Copyright (C) 2013 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <fdtdec.h>
10#include <spi.h>
11#include <spi_flash.h>
12#include <dm/device-internal.h>
13#include <dm/test.h>
14#include <dm/uclass-internal.h>
15#include <dm/ut.h>
16#include <dm/util.h>
17#include <asm/state.h>
18
19/* Test that we can find buses and chip-selects */
20static int dm_test_spi_find(struct dm_test_state *dms)
21{
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;
27 int of_offset;
28
29 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum,
30 false, &bus));
31
32 /*
33 * spi_post_bind() will bind devices to chip selects. Check this then
34 * remove the emulation and the slave device.
35 */
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 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
40 device_remove(info.dev);
41 device_unbind(info.dev);
42
43 /*
44 * Even though the device is gone, the sandbox SPI drivers always
45 * reports that CS 0 is present
46 */
47 ut_assertok(spi_cs_info(bus, cs, &info));
48 ut_asserteq_ptr(info.dev, NULL);
49
50 /* This finds nothing because we removed the device */
51 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
52 ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
53 NULL, 0, &bus, &slave));
54
55 /*
56 * This forces the device to be re-added, but there is no emulation
57 * connected so the probe will fail. We require that bus is left
58 * alone on failure, and that the spi_get_bus_and_cs() does not add
59 * a 'partially-inited' device.
60 */
61 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
62 ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
63 "spi_flash_std", "name", &bus,
64 &slave));
65 ut_assertok(spi_cs_info(bus, cs, &info));
66 ut_asserteq_ptr(info.dev, NULL);
67
68 /* Add the emulation and try again */
69 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, of_offset,
70 "name"));
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));
74
75 ut_assertok(spi_cs_info(bus, cs, &info));
76 ut_asserteq_ptr(info.dev, slave->dev);
77
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,
80 "name"));
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);
85
86 /*
87 * Since we are about to destroy all devices, we must tell sandbox
88 * to forget the emulation device
89 */
90 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
91 sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
92
93 return 0;
94}
95DM_TEST(dm_test_spi_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
96
97/* Test that sandbox SPI works correctly */
98static int dm_test_spi_xfer(struct dm_test_state *dms)
99{
100 struct spi_slave *slave;
101 struct udevice *bus;
102 const int busnum = 0, cs = 0, mode = 0;
103 const char dout[5] = {0x9f};
104 unsigned char din[5];
105
106 ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
107 &bus, &slave));
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);
116
117 /*
118 * Since we are about to destroy all devices, we must tell sandbox
119 * to forget the emulation device
120 */
121#ifdef CONFIG_DM_SPI_FLASH
122 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
123#endif
124
125 return 0;
126}
127DM_TEST(dm_test_spi_xfer, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);