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 | |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <fdtdec.h> |
| 8 | #include <spi.h> |
| 9 | #include <spi_flash.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 10 | #include <asm/state.h> |
Ovidiu Panait | 136a6dd | 2020-12-14 19:06:51 +0200 | [diff] [blame] | 11 | #include <asm/test.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; |
Ovidiu Panait | ae73473 | 2020-12-14 19:06:47 +0200 | [diff] [blame] | 25 | const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 2; |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 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 | |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 29 | /* |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 30 | * The post_bind() method will bind devices to chip selects. Check |
| 31 | * this then remove the emulation and the slave device. |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 32 | */ |
| 33 | ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus)); |
| 34 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 35 | node = dev_ofnode(info.dev); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 36 | device_remove(info.dev, DM_REMOVE_NORMAL); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 37 | 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 Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 44 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 45 | |
| 46 | /* This finds nothing because we removed the device */ |
| 47 | ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev)); |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 48 | ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, &bus, &slave)); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * This forces the device to be re-added, but there is no emulation |
| 52 | * connected so the probe will fail. We require that bus is left |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 53 | * alone on failure, and that the _spi_get_bus_and_cs() does not add |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 54 | * a 'partially-inited' device. |
| 55 | */ |
| 56 | ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev)); |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 57 | ut_asserteq(-ENOENT, _spi_get_bus_and_cs(busnum, cs, speed, mode, |
| 58 | "jedec_spi_nor", "name", &bus, |
| 59 | &slave)); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 60 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 61 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 62 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 63 | |
| 64 | /* Add the emulation and try again */ |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 65 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node, |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 66 | "name")); |
| 67 | ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev)); |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 68 | ut_assertok(_spi_get_bus_and_cs(busnum, cs, speed, mode, |
| 69 | "jedec_spi_nor", "name", &bus, &slave)); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 70 | |
| 71 | ut_assertok(spi_cs_info(bus, cs, &info)); |
| 72 | ut_asserteq_ptr(info.dev, slave->dev); |
| 73 | |
| 74 | /* We should be able to add something to another chip select */ |
Simon Glass | f354863 | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 75 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node, |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 76 | "name")); |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 77 | ut_asserteq(-EINVAL, _spi_get_bus_and_cs(busnum, cs_b, speed, mode, |
| 78 | "jedec_spi_nor", "name", &bus, |
| 79 | &slave)); |
Bin Meng | bb919a5 | 2019-09-09 06:00:03 -0700 | [diff] [blame] | 80 | ut_asserteq(-EINVAL, spi_cs_info(bus, cs_b, &info)); |
| 81 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 82 | |
| 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 92 | 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] | 93 | |
Ovidiu Panait | 136a6dd | 2020-12-14 19:06:51 +0200 | [diff] [blame] | 94 | /* dm_test_spi_switch_slaves - Helper function to check whether spi_claim_bus |
| 95 | * operates correctly with two spi slaves. |
| 96 | * |
| 97 | * Check that switching back and forth between two slaves claiming the bus |
| 98 | * will update dm_spi_bus->speed and sandbox_spi bus speed/mode correctly. |
| 99 | * |
| 100 | * @uts - unit test state |
| 101 | * @slave_a - first spi slave used for testing |
| 102 | * @slave_b - second spi slave used for testing |
| 103 | */ |
| 104 | static int dm_test_spi_switch_slaves(struct unit_test_state *uts, |
| 105 | struct spi_slave *slave_a, |
| 106 | struct spi_slave *slave_b) |
| 107 | { |
| 108 | struct udevice *bus; |
| 109 | struct dm_spi_bus *bus_data; |
| 110 | |
| 111 | /* Check that slaves are on the same bus */ |
| 112 | ut_asserteq_ptr(dev_get_parent(slave_a->dev), |
| 113 | dev_get_parent(slave_b->dev)); |
| 114 | |
| 115 | bus = dev_get_parent(slave_a->dev); |
| 116 | bus_data = dev_get_uclass_priv(bus); |
| 117 | |
| 118 | ut_assertok(spi_claim_bus(slave_a)); |
| 119 | ut_asserteq(slave_a->max_hz, bus_data->speed); |
| 120 | ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus)); |
| 121 | ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus)); |
| 122 | spi_release_bus(slave_a); |
| 123 | |
| 124 | ut_assertok(spi_claim_bus(slave_b)); |
| 125 | ut_asserteq(slave_b->max_hz, bus_data->speed); |
| 126 | ut_asserteq(slave_b->max_hz, sandbox_spi_get_speed(bus)); |
| 127 | ut_asserteq(slave_b->mode, sandbox_spi_get_mode(bus)); |
| 128 | spi_release_bus(slave_b); |
| 129 | |
| 130 | ut_assertok(spi_claim_bus(slave_a)); |
| 131 | ut_asserteq(slave_a->max_hz, bus_data->speed); |
| 132 | ut_asserteq(slave_a->max_hz, sandbox_spi_get_speed(bus)); |
| 133 | ut_asserteq(slave_a->mode, sandbox_spi_get_mode(bus)); |
| 134 | spi_release_bus(slave_a); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int dm_test_spi_claim_bus(struct unit_test_state *uts) |
| 140 | { |
| 141 | struct udevice *bus; |
| 142 | struct spi_slave *slave_a, *slave_b; |
| 143 | struct dm_spi_slave_plat *slave_plat; |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 144 | const int busnum = 0, cs_a = 0, cs_b = 1; |
Ovidiu Panait | 136a6dd | 2020-12-14 19:06:51 +0200 | [diff] [blame] | 145 | |
| 146 | /* Get spi slave on CS0 */ |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 147 | ut_assertok(spi_get_bus_and_cs(busnum, cs_a, &bus, &slave_a)); |
Ovidiu Panait | 136a6dd | 2020-12-14 19:06:51 +0200 | [diff] [blame] | 148 | /* Get spi slave on CS1 */ |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 149 | ut_assertok(spi_get_bus_and_cs(busnum, cs_b, &bus, &slave_b)); |
Ovidiu Panait | 136a6dd | 2020-12-14 19:06:51 +0200 | [diff] [blame] | 150 | |
| 151 | /* Different max_hz, different mode. */ |
| 152 | ut_assert(slave_a->max_hz != slave_b->max_hz); |
| 153 | ut_assert(slave_a->mode != slave_b->mode); |
| 154 | dm_test_spi_switch_slaves(uts, slave_a, slave_b); |
| 155 | |
| 156 | /* Different max_hz, same mode. */ |
| 157 | slave_a->mode = slave_b->mode; |
| 158 | dm_test_spi_switch_slaves(uts, slave_a, slave_b); |
| 159 | |
| 160 | /* |
| 161 | * Same max_hz, different mode. |
| 162 | * Restore original mode for slave_a, from platdata. |
| 163 | */ |
| 164 | slave_plat = dev_get_parent_plat(slave_a->dev); |
| 165 | slave_a->mode = slave_plat->mode; |
| 166 | slave_a->max_hz = slave_b->max_hz; |
| 167 | dm_test_spi_switch_slaves(uts, slave_a, slave_b); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | DM_TEST(dm_test_spi_claim_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 172 | |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 173 | /* Test that sandbox SPI works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 174 | static int dm_test_spi_xfer(struct unit_test_state *uts) |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 175 | { |
| 176 | struct spi_slave *slave; |
| 177 | struct udevice *bus; |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 178 | const int busnum = 0, cs = 0; |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 179 | const char dout[5] = {0x9f}; |
| 180 | unsigned char din[5]; |
| 181 | |
Patrice Chotard | f0c00df | 2022-03-30 09:33:15 +0200 | [diff] [blame] | 182 | ut_assertok(spi_get_bus_and_cs(busnum, cs, &bus, &slave)); |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 183 | ut_assertok(spi_claim_bus(slave)); |
| 184 | ut_assertok(spi_xfer(slave, 40, dout, din, |
| 185 | SPI_XFER_BEGIN | SPI_XFER_END)); |
| 186 | ut_asserteq(0xff, din[0]); |
| 187 | ut_asserteq(0x20, din[1]); |
| 188 | ut_asserteq(0x20, din[2]); |
| 189 | ut_asserteq(0x15, din[3]); |
| 190 | spi_release_bus(slave); |
| 191 | |
| 192 | /* |
| 193 | * Since we are about to destroy all devices, we must tell sandbox |
| 194 | * to forget the emulation device |
| 195 | */ |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 196 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Simon Glass | abae56d | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 197 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
| 198 | #endif |
| 199 | |
| 200 | return 0; |
| 201 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 202 | DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |