blob: 59bcedf2937380e8147491bd13eff2bebc8e0c31 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassebcab482014-10-13 23:42:10 -06002/*
3 * Copyright (C) 2013 Google, Inc
Simon Glassebcab482014-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 Hershbergere721b882015-05-20 14:27:27 -050011#include <asm/state.h>
Simon Glassebcab482014-10-13 23:42:10 -060012#include <dm/device-internal.h>
13#include <dm/test.h>
14#include <dm/uclass-internal.h>
Simon Glassebcab482014-10-13 23:42:10 -060015#include <dm/util.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060016#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassebcab482014-10-13 23:42:10 -060018
19/* Test that we can find buses and chip-selects */
Joe Hershbergere721b882015-05-20 14:27:27 -050020static int dm_test_spi_find(struct unit_test_state *uts)
Simon Glassebcab482014-10-13 23:42:10 -060021{
22 struct sandbox_state *state = state_get_current();
23 struct spi_slave *slave;
24 struct udevice *bus, *dev;
Ovidiu Panait1dc53ce2020-12-14 19:06:47 +020025 const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 2;
Simon Glassebcab482014-10-13 23:42:10 -060026 struct spi_cs_info info;
Simon Glass008dcdd2018-06-11 13:07:16 -060027 ofnode node;
Simon Glassebcab482014-10-13 23:42:10 -060028
Simon Glassebcab482014-10-13 23:42:10 -060029 /*
Simon Glass91195482016-07-05 17:10:10 -060030 * The post_bind() method will bind devices to chip selects. Check
31 * this then remove the emulation and the slave device.
Simon Glassebcab482014-10-13 23:42:10 -060032 */
33 ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus));
34 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glass008dcdd2018-06-11 13:07:16 -060035 node = dev_ofnode(info.dev);
Stefan Roese706865a2017-03-20 12:51:48 +010036 device_remove(info.dev, DM_REMOVE_NORMAL);
Simon Glassebcab482014-10-13 23:42:10 -060037 device_unbind(info.dev);
38
39 /*
40 * Even though the device is gone, the sandbox SPI drivers always
41 * reports that CS 0 is present
42 */
43 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glassd0cff032015-01-25 08:27:12 -070044 ut_asserteq_ptr(NULL, info.dev);
Simon Glassebcab482014-10-13 23:42:10 -060045
46 /* This finds nothing because we removed the device */
47 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
48 ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
49 NULL, 0, &bus, &slave));
50
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
54 * alone on failure, and that the spi_get_bus_and_cs() does not add
55 * a 'partially-inited' device.
56 */
57 ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
58 ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
Walter Lozanoe3e24702020-06-25 01:10:04 -030059 "jedec_spi_nor", "name", &bus,
Simon Glassebcab482014-10-13 23:42:10 -060060 &slave));
Simon Glassd0cff032015-01-25 08:27:12 -070061 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
Simon Glassebcab482014-10-13 23:42:10 -060062 ut_assertok(spi_cs_info(bus, cs, &info));
Simon Glassd0cff032015-01-25 08:27:12 -070063 ut_asserteq_ptr(NULL, info.dev);
Simon Glassebcab482014-10-13 23:42:10 -060064
65 /* Add the emulation and try again */
Simon Glass008dcdd2018-06-11 13:07:16 -060066 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node,
Simon Glassebcab482014-10-13 23:42:10 -060067 "name"));
68 ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
69 ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode,
Walter Lozanoe3e24702020-06-25 01:10:04 -030070 "jedec_spi_nor", "name", &bus, &slave));
Simon Glassebcab482014-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 Glass008dcdd2018-06-11 13:07:16 -060076 ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node,
Simon Glassebcab482014-10-13 23:42:10 -060077 "name"));
Bin Mengbfcd9292019-09-09 06:00:03 -070078 ut_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode,
Walter Lozanoe3e24702020-06-25 01:10:04 -030079 "jedec_spi_nor", "name", &bus, &slave));
Bin Mengbfcd9292019-09-09 06:00:03 -070080 ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info));
81 ut_asserteq_ptr(NULL, info.dev);
Simon Glassebcab482014-10-13 23:42:10 -060082
83 /*
84 * Since we are about to destroy all devices, we must tell sandbox
85 * to forget the emulation device
86 */
87 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
88 sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
89
90 return 0;
91}
Simon Glasse180c2b2020-07-28 19:41:12 -060092DM_TEST(dm_test_spi_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassebcab482014-10-13 23:42:10 -060093
94/* Test that sandbox SPI works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -050095static int dm_test_spi_xfer(struct unit_test_state *uts)
Simon Glassebcab482014-10-13 23:42:10 -060096{
97 struct spi_slave *slave;
98 struct udevice *bus;
99 const int busnum = 0, cs = 0, mode = 0;
100 const char dout[5] = {0x9f};
101 unsigned char din[5];
102
103 ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
104 &bus, &slave));
105 ut_assertok(spi_claim_bus(slave));
106 ut_assertok(spi_xfer(slave, 40, dout, din,
107 SPI_XFER_BEGIN | SPI_XFER_END));
108 ut_asserteq(0xff, din[0]);
109 ut_asserteq(0x20, din[1]);
110 ut_asserteq(0x20, din[2]);
111 ut_asserteq(0x15, din[3]);
112 spi_release_bus(slave);
113
114 /*
115 * Since we are about to destroy all devices, we must tell sandbox
116 * to forget the emulation device
117 */
Lukasz Majewski56c40462020-06-04 23:11:53 +0800118#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Simon Glassebcab482014-10-13 23:42:10 -0600119 sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
120#endif
121
122 return 0;
123}
Simon Glasse180c2b2020-07-28 19:41:12 -0600124DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);