Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <spi.h> |
| 10 | #include <spi_flash.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | #include <asm/state.h> |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
| 13 | #include <dm/test.h> |
| 14 | #include <dm/uclass-internal.h> |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 15 | #include <dm/util.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 18 | |
| 19 | /* Test that we can find buses and chip-selects */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static int dm_test_spi_find(struct unit_test_state *uts) |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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; |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 27 | ofnode node; |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 28 | |
| 29 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum, |
| 30 | false, &bus)); |
| 31 | |
| 32 | /* |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 33 | * The post_bind() method will bind devices to chip selects. Check |
| 34 | * this then remove the emulation and the slave device. |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 35 | */ |
| 36 | ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus)); |
| 37 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 38 | node = dev_ofnode(info.dev); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 39 | device_remove(info.dev, DM_REMOVE_NORMAL); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 40 | device_unbind(info.dev); |
| 41 | |
| 42 | /* |
| 43 | * Even though the device is gone, the sandbox SPI drivers always |
| 44 | * reports that CS 0 is present |
| 45 | */ |
| 46 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 47 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 48 | |
| 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)); |
| 53 | |
| 54 | /* |
| 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. |
| 59 | */ |
| 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, |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 62 | "jedec_spi_nor", "name", &bus, |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 63 | &slave)); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 64 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 65 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 66 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 67 | |
| 68 | /* Add the emulation and try again */ |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 69 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node, |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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, |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 73 | "jedec_spi_nor", "name", &bus, &slave)); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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 */ |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 79 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node, |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 80 | "name")); |
Bin Meng | bb919a5 | 2019-09-09 06:00:03 -0700 | [diff] [blame] | 81 | ut_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode, |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 82 | "jedec_spi_nor", "name", &bus, &slave)); |
Bin Meng | bb919a5 | 2019-09-09 06:00:03 -0700 | [diff] [blame] | 83 | ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info)); |
| 84 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 95 | DM_TEST(dm_test_spi_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 96 | |
| 97 | /* Test that sandbox SPI works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 98 | static int dm_test_spi_xfer(struct unit_test_state *uts) |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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 | */ |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 121 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 122 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
| 123 | #endif |
| 124 | |
| 125 | return 0; |
| 126 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame^] | 127 | DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |