blob: 325799bbf104217a03b85959c79f5dd225829cc8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassabae56d2014-10-13 23:42:10 -06002/*
3 * Copyright (C) 2013 Google, Inc
Simon Glassabae56d2014-10-13 23:42:10 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <fdtdec.h>
9#include <spi.h>
10#include <spi_flash.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050011#include <asm/state.h>
Ovidiu Panait136a6dd2020-12-14 19:06:51 +020012#include <asm/test.h>
Simon Glassabae56d2014-10-13 23:42:10 -060013#include <dm/device-internal.h>
14#include <dm/test.h>
15#include <dm/uclass-internal.h>
Simon Glassabae56d2014-10-13 23:42:10 -060016#include <dm/util.h>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glassabae56d2014-10-13 23:42:10 -060019
20/* Test that we can find buses and chip-selects */
Joe Hershberger3a77be52015-05-20 14:27:27 -050021static int dm_test_spi_find(struct unit_test_state *uts)
Simon Glassabae56d2014-10-13 23:42:10 -060022{
23 struct sandbox_state *state = state_get_current();
24 struct spi_slave *slave;
25 struct udevice *bus, *dev;
Ovidiu Panaitae734732020-12-14 19:06:47 +020026 const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 2;
Simon Glassabae56d2014-10-13 23:42:10 -060027 struct spi_cs_info info;
Simon Glassf3548632018-06-11 13:07:16 -060028 ofnode node;
Simon Glassabae56d2014-10-13 23:42:10 -060029
Simon Glassabae56d2014-10-13 23:42:10 -060030 /*
Simon Glass18230342016-07-05 17:10:10 -060031 * The post_bind() method will bind devices to chip selects. Check
32 * this then remove the emulation and the slave device.
Simon Glassabae56d2014-10-13 23:42:10 -060033 */
34 ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus));
35 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glassf3548632018-06-11 13:07:16 -060036 node = dev_ofnode(info.dev);
Stefan Roese80b5bc92017-03-20 12:51:48 +010037 device_remove(info.dev, DM_REMOVE_NORMAL);
Simon Glassabae56d2014-10-13 23:42:10 -060038 device_unbind(info.dev);
39
40 /*
41 * Even though the device is gone, the sandbox SPI drivers always
42 * reports that CS 0 is present
43 */
44 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glass5d2ee052015-01-25 08:27:12 -070045 ut_asserteq_ptr(NULL, info.dev);
Simon Glassabae56d2014-10-13 23:42:10 -060046
47 /* This finds nothing because we removed the device */
48 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
Patrice Chotardf0c00df2022-03-30 09:33:15 +020049 ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, &bus, &slave));
Simon Glassabae56d2014-10-13 23:42:10 -060050
51 /*
52 * This forces the device to be re-added, but there is no emulation
53 * connected so the probe will fail. We require that bus is left
Patrice Chotard86e06ae2022-03-30 09:33:13 +020054 * alone on failure, and that the _spi_get_bus_and_cs() does not add
Simon Glassabae56d2014-10-13 23:42:10 -060055 * a 'partially-inited' device.
56 */
57 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
Patrice Chotard86e06ae2022-03-30 09:33:13 +020058 ut_asserteq(-ENOENT, _spi_get_bus_and_cs(busnum, cs, speed, mode,
59 "jedec_spi_nor", "name", &bus,
60 &slave));
Simon Glass5d2ee052015-01-25 08:27:12 -070061 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
Simon Glassabae56d2014-10-13 23:42:10 -060062 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glass5d2ee052015-01-25 08:27:12 -070063 ut_asserteq_ptr(NULL, info.dev);
Simon Glassabae56d2014-10-13 23:42:10 -060064
65 /* Add the emulation and try again */
Simon Glassf3548632018-06-11 13:07:16 -060066 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node,
Simon Glassabae56d2014-10-13 23:42:10 -060067 "name"));
68 ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
Patrice Chotard86e06ae2022-03-30 09:33:13 +020069 ut_assertok(_spi_get_bus_and_cs(busnum, cs, speed, mode,
70 "jedec_spi_nor", "name", &bus, &slave));
Simon Glassabae56d2014-10-13 23:42:10 -060071
72 ut_assertok(spi_cs_info(bus, cs, &info));
73 ut_asserteq_ptr(info.dev, slave->dev);
74
75 /* We should be able to add something to another chip select */
Simon Glassf3548632018-06-11 13:07:16 -060076 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node,
Simon Glassabae56d2014-10-13 23:42:10 -060077 "name"));
Patrice Chotard86e06ae2022-03-30 09:33:13 +020078 ut_asserteq(-EINVAL, _spi_get_bus_and_cs(busnum, cs_b, speed, mode,
79 "jedec_spi_nor", "name", &bus,
80 &slave));
Bin Mengbb919a52019-09-09 06:00:03 -070081 ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info));
82 ut_asserteq_ptr(NULL, info.dev);
Simon Glassabae56d2014-10-13 23:42:10 -060083
84 /*
85 * Since we are about to destroy all devices, we must tell sandbox
86 * to forget the emulation device
87 */
88 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
89 sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
90
91 return 0;
92}
Simon Glass974dccd2020-07-28 19:41:12 -060093DM_TEST(dm_test_spi_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassabae56d2014-10-13 23:42:10 -060094
Ovidiu Panait136a6dd2020-12-14 19:06:51 +020095/* dm_test_spi_switch_slaves - Helper function to check whether spi_claim_bus
96 * operates correctly with two spi slaves.
97 *
98 * Check that switching back and forth between two slaves claiming the bus
99 * will update dm_spi_bus->speed and sandbox_spi bus speed/mode correctly.
100 *
101 * @uts - unit test state
102 * @slave_a - first spi slave used for testing
103 * @slave_b - second spi slave used for testing
104 */
105static int dm_test_spi_switch_slaves(struct unit_test_state *uts,
106 struct spi_slave *slave_a,
107 struct spi_slave *slave_b)
108{
109 struct udevice *bus;
110 struct dm_spi_bus *bus_data;
111
112 /* Check that slaves are on the same bus */
113 ut_asserteq_ptr(dev_get_parent(slave_a->dev),
114 dev_get_parent(slave_b->dev));
115
116 bus = dev_get_parent(slave_a->dev);
117 bus_data = dev_get_uclass_priv(bus);
118
119 ut_assertok(spi_claim_bus(slave_a));
120 ut_asserteq(slave_a->max_hz, bus_data->speed);
121 ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus));
122 ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus));
123 spi_release_bus(slave_a);
124
125 ut_assertok(spi_claim_bus(slave_b));
126 ut_asserteq(slave_b->max_hz, bus_data->speed);
127 ut_asserteq(slave_b->max_hz, sandbox_spi_get_speed(bus));
128 ut_asserteq(slave_b->mode, sandbox_spi_get_mode(bus));
129 spi_release_bus(slave_b);
130
131 ut_assertok(spi_claim_bus(slave_a));
132 ut_asserteq(slave_a->max_hz, bus_data->speed);
133 ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus));
134 ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus));
135 spi_release_bus(slave_a);
136
137 return 0;
138}
139
140static int dm_test_spi_claim_bus(struct unit_test_state *uts)
141{
142 struct udevice *bus;
143 struct spi_slave *slave_a, *slave_b;
144 struct dm_spi_slave_plat *slave_plat;
Patrice Chotardf0c00df2022-03-30 09:33:15 +0200145 const int busnum = 0, cs_a = 0, cs_b = 1;
Ovidiu Panait136a6dd2020-12-14 19:06:51 +0200146
147 /* Get spi slave on CS0 */
Patrice Chotardf0c00df2022-03-30 09:33:15 +0200148 ut_assertok(spi_get_bus_and_cs(busnum, cs_a, &bus, &slave_a));
Ovidiu Panait136a6dd2020-12-14 19:06:51 +0200149 /* Get spi slave on CS1 */
Patrice Chotardf0c00df2022-03-30 09:33:15 +0200150 ut_assertok(spi_get_bus_and_cs(busnum, cs_b, &bus, &slave_b));
Ovidiu Panait136a6dd2020-12-14 19:06:51 +0200151
152 /* Different max_hz, different mode. */
153 ut_assert(slave_a->max_hz != slave_b->max_hz);
154 ut_assert(slave_a->mode != slave_b->mode);
155 dm_test_spi_switch_slaves(uts, slave_a, slave_b);
156
157 /* Different max_hz, same mode. */
158 slave_a->mode = slave_b->mode;
159 dm_test_spi_switch_slaves(uts, slave_a, slave_b);
160
161 /*
162 * Same max_hz, different mode.
163 * Restore original mode for slave_a, from platdata.
164 */
165 slave_plat = dev_get_parent_plat(slave_a->dev);
166 slave_a->mode = slave_plat->mode;
167 slave_a->max_hz = slave_b->max_hz;
168 dm_test_spi_switch_slaves(uts, slave_a, slave_b);
169
170 return 0;
171}
172DM_TEST(dm_test_spi_claim_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
173
Simon Glassabae56d2014-10-13 23:42:10 -0600174/* Test that sandbox SPI works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500175static int dm_test_spi_xfer(struct unit_test_state *uts)
Simon Glassabae56d2014-10-13 23:42:10 -0600176{
177 struct spi_slave *slave;
178 struct udevice *bus;
Patrice Chotardf0c00df2022-03-30 09:33:15 +0200179 const int busnum = 0, cs = 0;
Simon Glassabae56d2014-10-13 23:42:10 -0600180 const char dout[5] = {0x9f};
181 unsigned char din[5];
182
Patrice Chotardf0c00df2022-03-30 09:33:15 +0200183 ut_assertok(spi_get_bus_and_cs(busnum, cs, &bus, &slave));
Simon Glassabae56d2014-10-13 23:42:10 -0600184 ut_assertok(spi_claim_bus(slave));
185 ut_assertok(spi_xfer(slave, 40, dout, din,
186 SPI_XFER_BEGIN | SPI_XFER_END));
187 ut_asserteq(0xff, din[0]);
188 ut_asserteq(0x20, din[1]);
189 ut_asserteq(0x20, din[2]);
190 ut_asserteq(0x15, din[3]);
191 spi_release_bus(slave);
192
193 /*
194 * Since we are about to destroy all devices, we must tell sandbox
195 * to forget the emulation device
196 */
Lukasz Majewski76f442982020-06-04 23:11:53 +0800197#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Simon Glassabae56d2014-10-13 23:42:10 -0600198 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
199#endif
200
201 return 0;
202}
Simon Glass974dccd2020-07-28 19:41:12 -0600203DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);