Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | ebcab48 | 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 | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | #include <asm/state.h> |
Simon Glass | ebcab48 | 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 | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 15 | #include <dm/util.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 18 | |
| 19 | /* Test that we can find buses and chip-selects */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static int dm_test_spi_find(struct unit_test_state *uts) |
Simon Glass | ebcab48 | 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 | 1dc53ce | 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 | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 26 | struct spi_cs_info info; |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 27 | ofnode node; |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 28 | |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 29 | /* |
Simon Glass | 9119548 | 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 | ebcab48 | 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 | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 35 | node = dev_ofnode(info.dev); |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 36 | device_remove(info.dev, DM_REMOVE_NORMAL); |
Simon Glass | ebcab48 | 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 | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 44 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | ebcab48 | 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)); |
| 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 Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 59 | "jedec_spi_nor", "name", &bus, |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 60 | &slave)); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 61 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 62 | ut_assertok(spi_cs_info(bus, cs, &info)); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 63 | ut_asserteq_ptr(NULL, info.dev); |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 64 | |
| 65 | /* Add the emulation and try again */ |
Simon Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 66 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node, |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 67 | "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 Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 70 | "jedec_spi_nor", "name", &bus, &slave)); |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 71 | |
| 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 Glass | 008dcdd | 2018-06-11 13:07:16 -0600 | [diff] [blame] | 76 | ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node, |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 77 | "name")); |
Bin Meng | bfcd929 | 2019-09-09 06:00:03 -0700 | [diff] [blame] | 78 | ut_asserteq(-EINVAL, spi_get_bus_and_cs(busnum, cs_b, speed, mode, |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 79 | "jedec_spi_nor", "name", &bus, &slave)); |
Bin Meng | bfcd929 | 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 | ebcab48 | 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 | e180c2b | 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 | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 93 | |
| 94 | /* Test that sandbox SPI works correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 95 | static int dm_test_spi_xfer(struct unit_test_state *uts) |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 96 | { |
| 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 Majewski | 56c4046 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 118 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Simon Glass | ebcab48 | 2014-10-13 23:42:10 -0600 | [diff] [blame] | 119 | sandbox_sf_unbind_emul(state_get_current(), busnum, cs); |
| 120 | #endif |
| 121 | |
| 122 | return 0; |
| 123 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 124 | DM_TEST(dm_test_spi_xfer, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |