183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2ebcab48aSSimon Glass /* 3ebcab48aSSimon Glass * Copyright (C) 2013 Google, Inc 4ebcab48aSSimon Glass */ 5ebcab48aSSimon Glass 6ebcab48aSSimon Glass #include <common.h> 7ebcab48aSSimon Glass #include <dm.h> 8ebcab48aSSimon Glass #include <fdtdec.h> 9ebcab48aSSimon Glass #include <spi.h> 10ebcab48aSSimon Glass #include <spi_flash.h> 11e721b882SJoe Hershberger #include <asm/state.h> 12ebcab48aSSimon Glass #include <dm/device-internal.h> 13ebcab48aSSimon Glass #include <dm/test.h> 14ebcab48aSSimon Glass #include <dm/uclass-internal.h> 15ebcab48aSSimon Glass #include <dm/util.h> 16e721b882SJoe Hershberger #include <test/ut.h> 17ebcab48aSSimon Glass 18ebcab48aSSimon Glass /* Test that we can find buses and chip-selects */ 19e721b882SJoe Hershberger static int dm_test_spi_find(struct unit_test_state *uts) 20ebcab48aSSimon Glass { 21ebcab48aSSimon Glass struct sandbox_state *state = state_get_current(); 22ebcab48aSSimon Glass struct spi_slave *slave; 23ebcab48aSSimon Glass struct udevice *bus, *dev; 24ebcab48aSSimon Glass const int busnum = 0, cs = 0, mode = 0, speed = 1000000, cs_b = 1; 25ebcab48aSSimon Glass struct spi_cs_info info; 26*008dcddfSSimon Glass ofnode node; 27ebcab48aSSimon Glass 28ebcab48aSSimon Glass ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_SPI, busnum, 29ebcab48aSSimon Glass false, &bus)); 30ebcab48aSSimon Glass 31ebcab48aSSimon Glass /* 3291195485SSimon Glass * The post_bind() method will bind devices to chip selects. Check 3391195485SSimon Glass * this then remove the emulation and the slave device. 34ebcab48aSSimon Glass */ 35ebcab48aSSimon Glass ut_asserteq(0, uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus)); 36ebcab48aSSimon Glass ut_assertok(spi_cs_info(bus, cs, &info)); 37*008dcddfSSimon Glass node = dev_ofnode(info.dev); 38706865afSStefan Roese device_remove(info.dev, DM_REMOVE_NORMAL); 39ebcab48aSSimon Glass device_unbind(info.dev); 40ebcab48aSSimon Glass 41ebcab48aSSimon Glass /* 42ebcab48aSSimon Glass * Even though the device is gone, the sandbox SPI drivers always 43ebcab48aSSimon Glass * reports that CS 0 is present 44ebcab48aSSimon Glass */ 45ebcab48aSSimon Glass ut_assertok(spi_cs_info(bus, cs, &info)); 46d0cff03eSSimon Glass ut_asserteq_ptr(NULL, info.dev); 47ebcab48aSSimon Glass 48ebcab48aSSimon Glass /* This finds nothing because we removed the device */ 49ebcab48aSSimon Glass ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev)); 50ebcab48aSSimon Glass ut_asserteq(-ENODEV, spi_get_bus_and_cs(busnum, cs, speed, mode, 51ebcab48aSSimon Glass NULL, 0, &bus, &slave)); 52ebcab48aSSimon Glass 53ebcab48aSSimon Glass /* 54ebcab48aSSimon Glass * This forces the device to be re-added, but there is no emulation 55ebcab48aSSimon Glass * connected so the probe will fail. We require that bus is left 56ebcab48aSSimon Glass * alone on failure, and that the spi_get_bus_and_cs() does not add 57ebcab48aSSimon Glass * a 'partially-inited' device. 58ebcab48aSSimon Glass */ 59ebcab48aSSimon Glass ut_asserteq(-ENODEV, spi_find_bus_and_cs(busnum, cs, &bus, &dev)); 60ebcab48aSSimon Glass ut_asserteq(-ENOENT, spi_get_bus_and_cs(busnum, cs, speed, mode, 61ebcab48aSSimon Glass "spi_flash_std", "name", &bus, 62ebcab48aSSimon Glass &slave)); 63d0cff03eSSimon Glass sandbox_sf_unbind_emul(state_get_current(), busnum, cs); 64ebcab48aSSimon Glass ut_assertok(spi_cs_info(bus, cs, &info)); 65d0cff03eSSimon Glass ut_asserteq_ptr(NULL, info.dev); 66ebcab48aSSimon Glass 67ebcab48aSSimon Glass /* Add the emulation and try again */ 68*008dcddfSSimon Glass ut_assertok(sandbox_sf_bind_emul(state, busnum, cs, bus, node, 69ebcab48aSSimon Glass "name")); 70ebcab48aSSimon Glass ut_assertok(spi_find_bus_and_cs(busnum, cs, &bus, &dev)); 71ebcab48aSSimon Glass ut_assertok(spi_get_bus_and_cs(busnum, cs, speed, mode, 72ebcab48aSSimon Glass "spi_flash_std", "name", &bus, &slave)); 73ebcab48aSSimon Glass 74ebcab48aSSimon Glass ut_assertok(spi_cs_info(bus, cs, &info)); 75ebcab48aSSimon Glass ut_asserteq_ptr(info.dev, slave->dev); 76ebcab48aSSimon Glass 77ebcab48aSSimon Glass /* We should be able to add something to another chip select */ 78*008dcddfSSimon Glass ut_assertok(sandbox_sf_bind_emul(state, busnum, cs_b, bus, node, 79ebcab48aSSimon Glass "name")); 80ebcab48aSSimon Glass ut_assertok(spi_get_bus_and_cs(busnum, cs_b, speed, mode, 81ebcab48aSSimon Glass "spi_flash_std", "name", &bus, &slave)); 82ebcab48aSSimon Glass ut_assertok(spi_cs_info(bus, cs_b, &info)); 83ebcab48aSSimon Glass ut_asserteq_ptr(info.dev, slave->dev); 84ebcab48aSSimon Glass 85ebcab48aSSimon Glass /* 86ebcab48aSSimon Glass * Since we are about to destroy all devices, we must tell sandbox 87ebcab48aSSimon Glass * to forget the emulation device 88ebcab48aSSimon Glass */ 89ebcab48aSSimon Glass sandbox_sf_unbind_emul(state_get_current(), busnum, cs); 90ebcab48aSSimon Glass sandbox_sf_unbind_emul(state_get_current(), busnum, cs_b); 91ebcab48aSSimon Glass 92ebcab48aSSimon Glass return 0; 93ebcab48aSSimon Glass } 94ebcab48aSSimon Glass DM_TEST(dm_test_spi_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); 95ebcab48aSSimon Glass 96ebcab48aSSimon Glass /* Test that sandbox SPI works correctly */ 97e721b882SJoe Hershberger static int dm_test_spi_xfer(struct unit_test_state *uts) 98ebcab48aSSimon Glass { 99ebcab48aSSimon Glass struct spi_slave *slave; 100ebcab48aSSimon Glass struct udevice *bus; 101ebcab48aSSimon Glass const int busnum = 0, cs = 0, mode = 0; 102ebcab48aSSimon Glass const char dout[5] = {0x9f}; 103ebcab48aSSimon Glass unsigned char din[5]; 104ebcab48aSSimon Glass 105ebcab48aSSimon Glass ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0, 106ebcab48aSSimon Glass &bus, &slave)); 107ebcab48aSSimon Glass ut_assertok(spi_claim_bus(slave)); 108ebcab48aSSimon Glass ut_assertok(spi_xfer(slave, 40, dout, din, 109ebcab48aSSimon Glass SPI_XFER_BEGIN | SPI_XFER_END)); 110ebcab48aSSimon Glass ut_asserteq(0xff, din[0]); 111ebcab48aSSimon Glass ut_asserteq(0x20, din[1]); 112ebcab48aSSimon Glass ut_asserteq(0x20, din[2]); 113ebcab48aSSimon Glass ut_asserteq(0x15, din[3]); 114ebcab48aSSimon Glass spi_release_bus(slave); 115ebcab48aSSimon Glass 116ebcab48aSSimon Glass /* 117ebcab48aSSimon Glass * Since we are about to destroy all devices, we must tell sandbox 118ebcab48aSSimon Glass * to forget the emulation device 119ebcab48aSSimon Glass */ 120ebcab48aSSimon Glass #ifdef CONFIG_DM_SPI_FLASH 121ebcab48aSSimon Glass sandbox_sf_unbind_emul(state_get_current(), busnum, cs); 122ebcab48aSSimon Glass #endif 123ebcab48aSSimon Glass 124ebcab48aSSimon Glass return 0; 125ebcab48aSSimon Glass } 126ebcab48aSSimon Glass DM_TEST(dm_test_spi_xfer, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); 127