summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/dm/Makefile2
-rw-r--r--test/dm/bus.c31
-rw-r--r--test/dm/sf.c43
-rw-r--r--test/dm/spi.c127
-rwxr-xr-xtest/dm/test-dm.sh2
-rw-r--r--test/dm/test.dts17
6 files changed, 221 insertions, 1 deletions
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 5c2415e3d2..75d3d41536 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -18,4 +18,6 @@ obj-$(CONFIG_DM_TEST) += core.o
obj-$(CONFIG_DM_TEST) += ut.o
ifneq ($(CONFIG_SANDBOX),)
obj-$(CONFIG_DM_GPIO) += gpio.o
+obj-$(CONFIG_DM_SPI) += spi.o
+obj-$(CONFIG_DM_SPI_FLASH) += sf.o
endif
diff --git a/test/dm/bus.c b/test/dm/bus.c
index 873d64e42a..abbaccff50 100644
--- a/test/dm/bus.c
+++ b/test/dm/bus.c
@@ -140,6 +140,37 @@ static int dm_test_bus_children_funcs(struct dm_test_state *dms)
}
DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+/* Test that we can iterate through children */
+static int dm_test_bus_children_iterators(struct dm_test_state *dms)
+{
+ struct udevice *bus, *dev, *child;
+
+ /* Walk through the children one by one */
+ ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
+ ut_assertok(device_find_first_child(bus, &dev));
+ ut_asserteq_str("c-test@5", dev->name);
+ ut_assertok(device_find_next_child(&dev));
+ ut_asserteq_str("c-test@0", dev->name);
+ ut_assertok(device_find_next_child(&dev));
+ ut_asserteq_str("c-test@1", dev->name);
+ ut_assertok(device_find_next_child(&dev));
+ ut_asserteq_ptr(dev, NULL);
+
+ /* Move to the next child without using device_find_first_child() */
+ ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
+ ut_asserteq_str("c-test@5", dev->name);
+ ut_assertok(device_find_next_child(&dev));
+ ut_asserteq_str("c-test@0", dev->name);
+
+ /* Try a device with no children */
+ ut_assertok(device_find_first_child(dev, &child));
+ ut_asserteq_ptr(child, NULL);
+
+ return 0;
+}
+DM_TEST(dm_test_bus_children_iterators,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
/* Test that the bus can store data about each child */
static int dm_test_bus_parent_data(struct dm_test_state *dms)
{
diff --git a/test/dm/sf.c b/test/dm/sf.c
new file mode 100644
index 0000000000..57dd1345c4
--- /dev/null
+++ b/test/dm/sf.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2013 Google, Inc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <fdtdec.h>
+#include <spi.h>
+#include <spi_flash.h>
+#include <asm/state.h>
+#include <dm/ut.h>
+#include <dm/test.h>
+#include <dm/util.h>
+
+/* Test that sandbox SPI flash works correctly */
+static int dm_test_spi_flash(struct dm_test_state *dms)
+{
+ /*
+ * Create an empty test file and run the SPI flash tests. This is a
+ * long way from being a unit test, but it does test SPI device and
+ * emulator binding, probing, the SPI flash emulator including
+ * device tree decoding, plus the file-based backing store of SPI.
+ *
+ * More targeted tests could be created to perform the above steps
+ * one at a time. This might not increase test coverage much, but
+ * it would make bugs easier to find. It's not clear whether the
+ * benefit is worth the extra complexity.
+ */
+ ut_asserteq(0, run_command_list(
+ "sb save hostfs - spi.bin 0 200000;"
+ "sf probe;"
+ "sf test 0 10000", -1, 0));
+ /*
+ * Since we are about to destroy all devices, we must tell sandbox
+ * to forget the emulation device
+ */
+ sandbox_sf_unbind_emul(state_get_current(), 0, 0);
+
+ return 0;
+}
+DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
diff --git a/test/dm/spi.c b/test/dm/spi.c
new file mode 100644
index 0000000000..61b5b2548c
--- /dev/null
+++ b/test/dm/spi.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2013 Google, Inc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <fdtdec.h>
+#include <spi.h>
+#include <spi_flash.h>
+#include <dm/device-internal.h>
+#include <dm/test.h>
+#include <dm/uclass-internal.h>
+#include <dm/ut.h>
+#include <dm/util.h>
+#include <asm/state.h>
+
+/* Test that we can find buses and chip-selects */
+static int dm_test_spi_find(struct dm_test_state *dms)
+{
+ struct sandbox_state *state = state_get_current();
+ struct spi_slave *slave;
+ struct udevice *bus, *dev;
+ const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 1;
+ struct spi_cs_info info;
+ int of_offset;
+
+ ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum,
+ false, &bus));
+
+ /*
+ * spi_post_bind() will bind devices to chip selects. Check this then
+ * remove the emulation and the slave device.
+ */
+ ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus));
+ ut_assertok(spi_cs_info(bus, cs, &info));
+ of_offset = info.dev->of_offset;
+ sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
+ device_remove(info.dev);
+ device_unbind(info.dev);
+
+ /*
+ * Even though the device is gone, the sandbox SPI drivers always
+ * reports that CS 0 is present
+ */
+ ut_assertok(spi_cs_info(bus, cs, &info));
+ ut_asserteq_ptr(info.dev, NULL);
+
+ /* This finds nothing because we removed the device */
+ ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
+ ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode,
+ NULL, 0, &bus, &slave));
+
+ /*
+ * This forces the device to be re-added, but there is no emulation
+ * connected so the probe will fail. We require that bus is left
+ * alone on failure, and that the spi_get_bus_and_cs() does not add
+ * a 'partially-inited' device.
+ */
+ ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev));
+ ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode,
+ "spi_flash_std", "name", &bus,
+ &slave));
+ ut_assertok(spi_cs_info(bus, cs, &info));
+ ut_asserteq_ptr(info.dev, NULL);
+
+ /* Add the emulation and try again */
+ ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, of_offset,
+ "name"));
+ ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev));
+ ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode,
+ "spi_flash_std", "name", &bus, &slave));
+
+ ut_assertok(spi_cs_info(bus, cs, &info));
+ ut_asserteq_ptr(info.dev, slave->dev);
+
+ /* We should be able to add something to another chip select */
+ ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, of_offset,
+ "name"));
+ ut_assertok(spi_get_bus_and_cs(busnum, cs_b, speed, mode,
+ "spi_flash_std", "name", &bus, &slave));
+ ut_assertok(spi_cs_info(bus, cs_b, &info));
+ ut_asserteq_ptr(info.dev, slave->dev);
+
+ /*
+ * Since we are about to destroy all devices, we must tell sandbox
+ * to forget the emulation device
+ */
+ sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
+ sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b);
+
+ return 0;
+}
+DM_TEST(dm_test_spi_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that sandbox SPI works correctly */
+static int dm_test_spi_xfer(struct dm_test_state *dms)
+{
+ struct spi_slave *slave;
+ struct udevice *bus;
+ const int busnum = 0, cs = 0, mode = 0;
+ const char dout[5] = {0x9f};
+ unsigned char din[5];
+
+ ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
+ &bus, &slave));
+ ut_assertok(spi_claim_bus(slave));
+ ut_assertok(spi_xfer(slave, 40, dout, din,
+ SPI_XFER_BEGIN | SPI_XFER_END));
+ ut_asserteq(0xff, din[0]);
+ ut_asserteq(0x20, din[1]);
+ ut_asserteq(0x20, din[2]);
+ ut_asserteq(0x15, din[3]);
+ spi_release_bus(slave);
+
+ /*
+ * Since we are about to destroy all devices, we must tell sandbox
+ * to forget the emulation device
+ */
+#ifdef CONFIG_DM_SPI_FLASH
+ sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
+#endif
+
+ return 0;
+}
+DM_TEST(dm_test_spi_xfer, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
diff --git a/test/dm/test-dm.sh b/test/dm/test-dm.sh
index ef5aca5ac3..bb99677ece 100755
--- a/test/dm/test-dm.sh
+++ b/test/dm/test-dm.sh
@@ -4,4 +4,6 @@ NUM_CPUS=$(cat /proc/cpuinfo |grep -c processor)
dtc -I dts -O dtb test/dm/test.dts -o test/dm/test.dtb
make O=sandbox sandbox_config
make O=sandbox -s -j${NUM_CPUS}
+dd if=/dev/zero of=spi.bin bs=1M count=2
./sandbox/u-boot -d test/dm/test.dtb -c "dm test"
+rm spi.bin
diff --git a/test/dm/test.dts b/test/dm/test.dts
index 8489595155..1fba792564 100644
--- a/test/dm/test.dts
+++ b/test/dm/test.dts
@@ -81,7 +81,7 @@
compatible = "google,another-fdt-test";
};
- base-gpios {
+ gpio_a: base-gpios {
compatible = "sandbox,gpio";
gpio-bank-name = "a";
num-gpios = <20>;
@@ -92,4 +92,19 @@
gpio-bank-name = "b";
num-gpios = <10>;
};
+
+ spi@0 {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ reg = <0>;
+ compatible = "sandbox,spi";
+ cs-gpios = <0>, <&gpio_a 0>;
+ spi.bin@0 {
+ reg = <0>;
+ compatible = "spansion,m25p16", "spi-flash";
+ spi-max-frequency = <40000000>;
+ sandbox,filename = "spi.bin";
+ };
+ };
+
};
OpenPOWER on IntegriCloud