avionic design with actual uboot and tooling

submodule of avionic design uboot bootloader and with included tools to
get you started , read readme.md and readme-tk1-loader.md
This commit is contained in:
2026-03-03 21:46:32 +02:00
parent fe3ba02c96
commit 68d74d3181
11967 changed files with 2221897 additions and 0 deletions

8
u-boot/test/dm/Kconfig Normal file
View File

@@ -0,0 +1,8 @@
config UT_DM
bool "Enable driver model unit test command"
depends on SANDBOX && UNIT_TEST
help
This enables the 'ut dm' command which runs a series of unit
tests on the driver model code. Each subsystem (uclass) is tested.
If all is well then all tests pass although there will be a few
messages printed along the way.

43
u-boot/test/dm/Makefile Normal file
View File

@@ -0,0 +1,43 @@
#
# Copyright (c) 2013 Google, Inc
#
# SPDX-License-Identifier: GPL-2.0+
#
obj-$(CONFIG_CMD_DM) += cmd_dm.o
obj-$(CONFIG_UT_DM) += bus.o
obj-$(CONFIG_UT_DM) += test-driver.o
obj-$(CONFIG_UT_DM) += test-fdt.o
obj-$(CONFIG_UT_DM) += test-main.o
obj-$(CONFIG_UT_DM) += test-uclass.o
# Tests for particular subsystems - when enabling driver model for a new
# subsystem you must add sandbox tests here.
obj-$(CONFIG_UT_DM) += core.o
ifneq ($(CONFIG_SANDBOX),)
obj-$(CONFIG_BLK) += blk.o
obj-$(CONFIG_CLK) += clk.o
obj-$(CONFIG_DM_ETH) += eth.o
obj-$(CONFIG_DM_GPIO) += gpio.o
obj-$(CONFIG_DM_I2C) += i2c.o
obj-$(CONFIG_LED) += led.o
obj-$(CONFIG_DM_MAILBOX) += mailbox.o
obj-$(CONFIG_DM_MMC) += mmc.o
obj-$(CONFIG_DM_PCI) += pci.o
obj-$(CONFIG_RAM) += ram.o
obj-y += regmap.o
obj-$(CONFIG_REMOTEPROC) += remoteproc.o
obj-$(CONFIG_DM_RESET) += reset.o
obj-$(CONFIG_SYSRESET) += sysreset.o
obj-$(CONFIG_DM_RTC) += rtc.o
obj-$(CONFIG_DM_SPI_FLASH) += sf.o
obj-$(CONFIG_DM_SPI) += spi.o
obj-y += syscon.o
obj-$(CONFIG_DM_USB) += usb.o
obj-$(CONFIG_DM_PMIC) += pmic.o
obj-$(CONFIG_DM_REGULATOR) += regulator.o
obj-$(CONFIG_TIMER) += timer.o
obj-$(CONFIG_DM_VIDEO) += video.o
obj-$(CONFIG_ADC) += adc.o
obj-$(CONFIG_SPMI) += spmi.o
endif

165
u-boot/test/dm/adc.c Normal file
View File

@@ -0,0 +1,165 @@
/*
* Tests for the driver model ADC API
*
* Copyright (c) 2015 Samsung Electronics
* Przemyslaw Marczak <p.marczak@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <adc.h>
#include <dm.h>
#include <dm/root.h>
#include <dm/util.h>
#include <dm/test.h>
#include <errno.h>
#include <fdtdec.h>
#include <power/regulator.h>
#include <power/sandbox_pmic.h>
#include <sandbox-adc.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
static int dm_test_adc_bind(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
return 0;
}
DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
return 0;
}
DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
static int dm_test_adc_supply(struct unit_test_state *uts)
{
struct udevice *supply;
struct udevice *dev;
int uV;
ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
/* Test Vss value - predefined 0 uV */
ut_assertok(adc_vss_value(dev, &uV));
ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
/* Test Vdd initial value - buck2 */
ut_assertok(adc_vdd_value(dev, &uV));
ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
/* Change Vdd value - buck2 manual preset */
ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
/* Update ADC platdata and get new Vdd value */
ut_assertok(adc_vdd_value(dev, &uV));
ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
/* Disable buck2 and test ADC supply enable function */
ut_assertok(regulator_set_enable(supply, false));
ut_asserteq(false, regulator_get_enable(supply));
/* adc_start_channel() should enable the supply regulator */
ut_assertok(adc_start_channel(dev, 0));
ut_asserteq(true, regulator_get_enable(supply));
return 0;
}
DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
struct adc_channel adc_channel_test_data[] = {
{ 0, SANDBOX_ADC_CHANNEL0_DATA },
{ 1, SANDBOX_ADC_CHANNEL1_DATA },
{ 2, SANDBOX_ADC_CHANNEL2_DATA },
{ 3, SANDBOX_ADC_CHANNEL3_DATA },
};
static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
{
struct adc_channel *tdata = adc_channel_test_data;
unsigned int i, data;
struct udevice *dev;
ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
/* Test each ADC channel's value */
for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
ut_assertok(adc_start_channel(dev, tdata->id));
ut_assertok(adc_channel_data(dev, tdata->id, &data));
ut_asserteq(tdata->data, data);
}
return 0;
}
DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
{
struct adc_channel channels[SANDBOX_ADC_CHANNELS];
struct udevice *dev;
struct adc_channel *tdata = adc_channel_test_data;
unsigned int i, channel_mask;
channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
ADC_CHANNEL(2) | ADC_CHANNEL(3);
/* Start multi channel conversion */
ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
ut_assertok(adc_start_channels(dev, channel_mask));
ut_assertok(adc_channels_data(dev, channel_mask, channels));
/* Compare the expected and returned conversion data. */
for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
ut_asserteq(tdata->data, channels[i].data);
return 0;
}
DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
{
struct adc_channel *tdata = adc_channel_test_data;
unsigned int i, data;
for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
/* Start single channel conversion */
ut_assertok(adc_channel_single_shot("adc", tdata->id, &data));
/* Compare the expected and returned conversion data. */
ut_asserteq(tdata->data, data);
}
return 0;
}
DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
{
struct adc_channel channels[SANDBOX_ADC_CHANNELS];
struct adc_channel *tdata = adc_channel_test_data;
unsigned int i, channel_mask;
channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
ADC_CHANNEL(2) | ADC_CHANNEL(3);
/* Start single call and multi channel conversion */
ut_assertok(adc_channels_single_shot("adc", channel_mask, channels));
/* Compare the expected and returned conversion data. */
for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
ut_asserteq(tdata->data, channels[i].data);
return 0;
}
DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);

96
u-boot/test/dm/blk.c Normal file
View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <usb.h>
#include <asm/state.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Test that block devices can be created */
static int dm_test_blk_base(struct unit_test_state *uts)
{
struct udevice *blk, *usb_blk, *dev;
/* Make sure there are no block devices */
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk));
/* Create two, one the parent of the other */
ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
IF_TYPE_HOST, 1, 512, 1024, &blk));
ut_assertok(blk_create_device(blk, "usb_storage_blk", "test",
IF_TYPE_USB, 3, 512, 1024, &usb_blk));
/* Check we can find them */
ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
ut_asserteq_ptr(blk, dev);
ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_USB, 0, &dev));
ut_assertok(blk_get_device(IF_TYPE_USB, 3, &dev));
ut_asserteq_ptr(usb_blk, dev);
/* Check we can iterate */
ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
ut_asserteq_ptr(blk, dev);
ut_asserteq(-ENODEV, blk_next_device(&dev));
ut_assertok(blk_first_device(IF_TYPE_USB, &dev));
ut_asserteq_ptr(usb_blk, dev);
ut_asserteq(-ENODEV, blk_next_device(&dev));
return 0;
}
DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int count_blk_devices(void)
{
struct udevice *blk;
struct uclass *uc;
int count = 0;
int ret;
ret = uclass_get(UCLASS_BLK, &uc);
if (ret)
return ret;
uclass_foreach_dev(blk, uc)
count++;
return count;
}
/* Test that block devices work correctly with USB */
static int dm_test_blk_usb(struct unit_test_state *uts)
{
struct udevice *usb_dev, *dev;
struct blk_desc *dev_desc;
/* Get a flash device */
state_set_skip_delays(true);
ut_assertok(usb_init());
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
/* The parent should be a block device */
ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
/* Check we have one block device for each mass storage device */
ut_asserteq(4, count_blk_devices());
/* Now go around again, making sure the old devices were unbound */
ut_assertok(usb_stop());
ut_assertok(usb_init());
ut_asserteq(4, count_blk_devices());
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

522
u-boot/test/dm/bus.c Normal file
View File

@@ -0,0 +1,522 @@
/*
* Copyright (c) 2014 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
struct dm_test_parent_platdata {
int count;
int bind_flag;
int uclass_bind_flag;
};
enum {
FLAG_CHILD_PROBED = 10,
FLAG_CHILD_REMOVED = -7,
};
static struct dm_test_state *test_state;
static int testbus_drv_probe(struct udevice *dev)
{
return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
}
static int testbus_child_post_bind(struct udevice *dev)
{
struct dm_test_parent_platdata *plat;
plat = dev_get_parent_platdata(dev);
plat->bind_flag = 1;
plat->uclass_bind_flag = 2;
return 0;
}
static int testbus_child_pre_probe(struct udevice *dev)
{
struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
parent_data->flag += FLAG_CHILD_PROBED;
return 0;
}
static int testbus_child_pre_probe_uclass(struct udevice *dev)
{
struct dm_test_priv *priv = dev_get_priv(dev);
priv->uclass_flag++;
return 0;
}
static int testbus_child_post_remove(struct udevice *dev)
{
struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
struct dm_test_state *dms = test_state;
parent_data->flag += FLAG_CHILD_REMOVED;
if (dms)
dms->removed = dev;
return 0;
}
static const struct udevice_id testbus_ids[] = {
{
.compatible = "denx,u-boot-test-bus",
.data = DM_TEST_TYPE_FIRST },
{ }
};
U_BOOT_DRIVER(testbus_drv) = {
.name = "testbus_drv",
.of_match = testbus_ids,
.id = UCLASS_TEST_BUS,
.probe = testbus_drv_probe,
.child_post_bind = testbus_child_post_bind,
.priv_auto_alloc_size = sizeof(struct dm_test_priv),
.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
.per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
.per_child_platdata_auto_alloc_size =
sizeof(struct dm_test_parent_platdata),
.child_pre_probe = testbus_child_pre_probe,
.child_post_remove = testbus_child_post_remove,
};
UCLASS_DRIVER(testbus) = {
.name = "testbus",
.id = UCLASS_TEST_BUS,
.flags = DM_UC_FLAG_SEQ_ALIAS,
.child_pre_probe = testbus_child_pre_probe_uclass,
};
/* Test that we can probe for children */
static int dm_test_bus_children(struct unit_test_state *uts)
{
int num_devices = 6;
struct udevice *bus;
struct uclass *uc;
ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
ut_asserteq(num_devices, list_count_items(&uc->dev_head));
/* Probe the bus, which should yield 3 more devices */
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
num_devices += 3;
ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
ut_asserteq(num_devices, list_count_items(&uc->dev_head));
ut_assert(!dm_check_devices(uts, num_devices));
return 0;
}
DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test our functions for accessing children */
static int dm_test_bus_children_funcs(struct unit_test_state *uts)
{
const void *blob = gd->fdt_blob;
struct udevice *bus, *dev;
int node;
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
/* device_get_child() */
ut_assertok(device_get_child(bus, 0, &dev));
ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
ut_assertok(device_get_child_by_seq(bus, 5, &dev));
ut_assert(dev->flags & DM_FLAG_ACTIVATED);
ut_asserteq_str("c-test@5", dev->name);
/* Device with sequence number 0 should be accessible */
ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
ut_assertok(device_get_child_by_seq(bus, 0, &dev));
ut_assert(dev->flags & DM_FLAG_ACTIVATED);
/* There is no device with sequence number 2 */
ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
/* Looking for something that is not a child */
node = fdt_path_offset(blob, "/junk");
ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
node = fdt_path_offset(blob, "/d-test");
ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
/* Find a valid child */
node = fdt_path_offset(blob, "/some-bus/c-test@1");
ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
ut_assert(dev->flags & DM_FLAG_ACTIVATED);
return 0;
}
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 unit_test_state *uts)
{
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 test_bus_parent_data(struct unit_test_state *uts)
{
struct dm_test_parent_data *parent_data;
struct udevice *bus, *dev;
struct uclass *uc;
int value;
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
/* Check that parent data is allocated */
ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
ut_assertok(device_get_child_by_seq(bus, 0, &dev));
parent_data = dev_get_parent_priv(dev);
ut_assert(NULL != parent_data);
/* Check that it starts at 0 and goes away when device is removed */
parent_data->sum += 5;
ut_asserteq(5, parent_data->sum);
device_remove(dev);
ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
/* Check that we can do this twice */
ut_assertok(device_get_child_by_seq(bus, 0, &dev));
parent_data = dev_get_parent_priv(dev);
ut_assert(NULL != parent_data);
parent_data->sum += 5;
ut_asserteq(5, parent_data->sum);
/* Add parent data to all children */
ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
value = 5;
uclass_foreach_dev(dev, uc) {
/* Ignore these if they are not on this bus */
if (dev->parent != bus) {
ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
continue;
}
ut_assertok(device_probe(dev));
parent_data = dev_get_parent_priv(dev);
parent_data->sum = value;
value += 5;
}
/* Check it is still there */
value = 5;
uclass_foreach_dev(dev, uc) {
/* Ignore these if they are not on this bus */
if (dev->parent != bus)
continue;
parent_data = dev_get_parent_priv(dev);
ut_asserteq(value, parent_data->sum);
value += 5;
}
return 0;
}
/* Test that the bus can store data about each child */
static int dm_test_bus_parent_data(struct unit_test_state *uts)
{
return test_bus_parent_data(uts);
}
DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* As above but the size is controlled by the uclass */
static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
{
struct driver *drv;
struct udevice *bus;
int size;
int ret;
/* Set the driver size to 0 so that the uclass size is used */
ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
drv = (struct driver *)bus->driver;
size = drv->per_child_auto_alloc_size;
bus->uclass->uc_drv->per_child_auto_alloc_size = size;
drv->per_child_auto_alloc_size = 0;
ret = test_bus_parent_data(uts);
if (ret)
return ret;
bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
drv->per_child_auto_alloc_size = size;
return 0;
}
DM_TEST(dm_test_bus_parent_data_uclass,
DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that the bus ops are called when a child is probed/removed */
static int dm_test_bus_parent_ops(struct unit_test_state *uts)
{
struct dm_test_parent_data *parent_data;
struct dm_test_state *dms = uts->priv;
struct udevice *bus, *dev;
struct uclass *uc;
test_state = dms;
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
uclass_foreach_dev(dev, uc) {
/* Ignore these if they are not on this bus */
if (dev->parent != bus)
continue;
ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
ut_assertok(device_probe(dev));
parent_data = dev_get_parent_priv(dev);
ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
}
uclass_foreach_dev(dev, uc) {
/* Ignore these if they are not on this bus */
if (dev->parent != bus)
continue;
parent_data = dev_get_parent_priv(dev);
ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
ut_assertok(device_remove(dev));
ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
ut_asserteq_ptr(dms->removed, dev);
}
test_state = NULL;
return 0;
}
DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int test_bus_parent_platdata(struct unit_test_state *uts)
{
struct dm_test_parent_platdata *plat;
struct udevice *bus, *dev;
int child_count;
/* Check that the bus has no children */
ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
device_find_first_child(bus, &dev);
ut_asserteq_ptr(NULL, dev);
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
/* Check that platform data is allocated */
plat = dev_get_parent_platdata(dev);
ut_assert(plat != NULL);
/*
* Check that it is not affected by the device being
* probed/removed
*/
plat->count++;
ut_asserteq(1, plat->count);
device_probe(dev);
device_remove(dev);
ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
ut_asserteq(1, plat->count);
ut_assertok(device_probe(dev));
child_count++;
}
ut_asserteq(3, child_count);
/* Removing the bus should also have no effect (it is still bound) */
device_remove(bus);
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
/* Check that platform data is allocated */
plat = dev_get_parent_platdata(dev);
ut_assert(plat != NULL);
ut_asserteq(1, plat->count);
child_count++;
}
ut_asserteq(3, child_count);
/* Unbind all the children */
do {
device_find_first_child(bus, &dev);
if (dev)
device_unbind(dev);
} while (dev);
/* Now the child platdata should be removed and re-added */
device_probe(bus);
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
/* Check that platform data is allocated */
plat = dev_get_parent_platdata(dev);
ut_assert(plat != NULL);
ut_asserteq(0, plat->count);
child_count++;
}
ut_asserteq(3, child_count);
return 0;
}
/* Test that the bus can store platform data about each child */
static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
{
return test_bus_parent_platdata(uts);
}
DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* As above but the size is controlled by the uclass */
static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
{
struct udevice *bus;
struct driver *drv;
int size;
int ret;
/* Set the driver size to 0 so that the uclass size is used */
ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
drv = (struct driver *)bus->driver;
size = drv->per_child_platdata_auto_alloc_size;
bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
drv->per_child_platdata_auto_alloc_size = 0;
ret = test_bus_parent_platdata(uts);
if (ret)
return ret;
bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
drv->per_child_platdata_auto_alloc_size = size;
return 0;
}
DM_TEST(dm_test_bus_parent_platdata_uclass,
DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that the child post_bind method is called */
static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
{
struct dm_test_parent_platdata *plat;
struct udevice *bus, *dev;
int child_count;
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
/* Check that platform data is allocated */
plat = dev_get_parent_platdata(dev);
ut_assert(plat != NULL);
ut_asserteq(1, plat->bind_flag);
child_count++;
}
ut_asserteq(3, child_count);
return 0;
}
DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that the child post_bind method is called */
static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
{
struct dm_test_parent_platdata *plat;
struct udevice *bus, *dev;
int child_count;
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
/* Check that platform data is allocated */
plat = dev_get_parent_platdata(dev);
ut_assert(plat != NULL);
ut_asserteq(2, plat->uclass_bind_flag);
child_count++;
}
ut_asserteq(3, child_count);
return 0;
}
DM_TEST(dm_test_bus_child_post_bind_uclass,
DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/*
* Test that the bus' uclass' child_pre_probe() is called before the
* device's probe() method
*/
static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
int child_count;
/*
* See testfdt_drv_probe() which effectively checks that the uclass
* flag is set before that method is called
*/
ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
for (device_find_first_child(bus, &dev), child_count = 0;
dev;
device_find_next_child(&dev)) {
struct dm_test_priv *priv = dev_get_priv(dev);
/* Check that things happened in the right order */
ut_asserteq_ptr(NULL, priv);
ut_assertok(device_probe(dev));
priv = dev_get_priv(dev);
ut_assert(priv != NULL);
ut_asserteq(1, priv->uclass_flag);
ut_asserteq(1, priv->uclass_total);
child_count++;
}
ut_asserteq(3, child_count);
return 0;
}
DM_TEST(dm_test_bus_child_pre_probe_uclass,
DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

103
u-boot/test/dm/clk.c Normal file
View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <asm/clk.h>
#include <dm/test.h>
#include <linux/err.h>
#include <test/ut.h>
static int dm_test_clk(struct unit_test_state *uts)
{
struct udevice *dev_fixed, *dev_clk, *dev_test;
ulong rate;
ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
&dev_fixed));
ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
&dev_clk));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
&dev_test));
ut_assertok(sandbox_clk_test_get(dev_test));
ut_asserteq(1234,
sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_FIXED));
ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_SPI));
ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_I2C));
rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
12345);
ut_assert(IS_ERR_VALUE(rate));
rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
ut_asserteq(1234, rate);
ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
SANDBOX_CLK_TEST_ID_SPI,
1000));
ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
SANDBOX_CLK_TEST_ID_I2C,
2000));
ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_SPI));
ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_I2C));
ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
SANDBOX_CLK_TEST_ID_SPI,
10000));
ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
SANDBOX_CLK_TEST_ID_I2C,
20000));
rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
ut_assert(IS_ERR_VALUE(rate));
rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
ut_assert(IS_ERR_VALUE(rate));
ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_SPI));
ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
SANDBOX_CLK_TEST_ID_I2C));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(sandbox_clk_test_disable(dev_test,
SANDBOX_CLK_TEST_ID_SPI));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(sandbox_clk_test_disable(dev_test,
SANDBOX_CLK_TEST_ID_I2C));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
ut_assertok(sandbox_clk_test_free(dev_test));
return 0;
}
DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT);

89
u-boot/test/dm/cmd_dm.c Normal file
View File

@@ -0,0 +1,89 @@
/*
* Copyright (c) 2013 Google, Inc
*
* (C) Copyright 2012
* Marek Vasut <marex@denx.de>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <command.h>
#include <dm.h>
#include <malloc.h>
#include <mapmem.h>
#include <errno.h>
#include <asm/io.h>
#include <dm/root.h>
#include <dm/util.h>
static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
dm_dump_all();
return 0;
}
static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
dm_dump_uclass();
return 0;
}
static int do_dm_dump_devres(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
dm_dump_devres();
return 0;
}
static cmd_tbl_t test_commands[] = {
U_BOOT_CMD_MKENT(tree, 0, 1, do_dm_dump_all, "", ""),
U_BOOT_CMD_MKENT(uclass, 1, 1, do_dm_dump_uclass, "", ""),
U_BOOT_CMD_MKENT(devres, 1, 1, do_dm_dump_devres, "", ""),
};
static __maybe_unused void dm_reloc(void)
{
static int relocated;
if (!relocated) {
fixup_cmdtable(test_commands, ARRAY_SIZE(test_commands));
relocated = 1;
}
}
static int do_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
cmd_tbl_t *test_cmd;
int ret;
#ifdef CONFIG_NEEDS_MANUAL_RELOC
dm_reloc();
#endif
if (argc < 2)
return CMD_RET_USAGE;
test_cmd = find_cmd_tbl(argv[1], test_commands,
ARRAY_SIZE(test_commands));
argc -= 2;
argv += 2;
if (!test_cmd || argc > test_cmd->maxargs)
return CMD_RET_USAGE;
ret = test_cmd->cmd(test_cmd, flag, argc, argv);
return cmd_process_error(test_cmd, ret);
}
U_BOOT_CMD(
dm, 3, 1, do_dm,
"Driver model low level access",
"tree Dump driver model tree ('*' = activated)\n"
"dm uclass Dump list of instances for each uclass\n"
"dm devres Dump list of device resources for each device"
);

798
u-boot/test/dm/core.c Normal file
View File

@@ -0,0 +1,798 @@
/*
* Tests for the core driver model code
*
* Copyright (c) 2013 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <errno.h>
#include <dm.h>
#include <fdtdec.h>
#include <malloc.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/util.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
enum {
TEST_INTVAL1 = 0,
TEST_INTVAL2 = 3,
TEST_INTVAL3 = 6,
TEST_INTVAL_MANUAL = 101112,
TEST_INTVAL_PRE_RELOC = 7,
};
static const struct dm_test_pdata test_pdata[] = {
{ .ping_add = TEST_INTVAL1, },
{ .ping_add = TEST_INTVAL2, },
{ .ping_add = TEST_INTVAL3, },
};
static const struct dm_test_pdata test_pdata_manual = {
.ping_add = TEST_INTVAL_MANUAL,
};
static const struct dm_test_pdata test_pdata_pre_reloc = {
.ping_add = TEST_INTVAL_PRE_RELOC,
};
U_BOOT_DEVICE(dm_test_info1) = {
.name = "test_drv",
.platdata = &test_pdata[0],
};
U_BOOT_DEVICE(dm_test_info2) = {
.name = "test_drv",
.platdata = &test_pdata[1],
};
U_BOOT_DEVICE(dm_test_info3) = {
.name = "test_drv",
.platdata = &test_pdata[2],
};
static struct driver_info driver_info_manual = {
.name = "test_manual_drv",
.platdata = &test_pdata_manual,
};
static struct driver_info driver_info_pre_reloc = {
.name = "test_pre_reloc_drv",
.platdata = &test_pdata_pre_reloc,
};
void dm_leak_check_start(struct unit_test_state *uts)
{
uts->start = mallinfo();
if (!uts->start.uordblks)
puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
}
int dm_leak_check_end(struct unit_test_state *uts)
{
struct mallinfo end;
int id, diff;
/* Don't delete the root class, since we started with that */
for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
struct uclass *uc;
uc = uclass_find(id);
if (!uc)
continue;
ut_assertok(uclass_destroy(uc));
}
end = mallinfo();
diff = end.uordblks - uts->start.uordblks;
if (diff > 0)
printf("Leak: lost %#xd bytes\n", diff);
else if (diff < 0)
printf("Leak: gained %#xd bytes\n", -diff);
ut_asserteq(uts->start.uordblks, end.uordblks);
return 0;
}
/* Test that binding with platdata occurs correctly */
static int dm_test_autobind(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
struct udevice *dev;
/*
* We should have a single class (UCLASS_ROOT) and a single root
* device with no children.
*/
ut_assert(dms->root);
ut_asserteq(1, list_count_items(&gd->uclass_root));
ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
ut_assertok(dm_scan_platdata(false));
/* We should have our test class now at least, plus more children */
ut_assert(1 < list_count_items(&gd->uclass_root));
ut_assert(0 < list_count_items(&gd->dm_root->child_head));
/* Our 3 dm_test_infox children should be bound to the test uclass */
ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
/* No devices should be probed */
list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
/* Our test driver should have been bound 3 times */
ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
return 0;
}
DM_TEST(dm_test_autobind, 0);
/* Test that binding with uclass platdata allocation occurs correctly */
static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
{
struct dm_test_perdev_uc_pdata *uc_pdata;
struct udevice *dev;
struct uclass *uc;
ut_assertok(uclass_get(UCLASS_TEST, &uc));
ut_assert(uc);
/**
* Test if test uclass driver requires allocation for the uclass
* platform data and then check the dev->uclass_platdata pointer.
*/
ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
for (uclass_find_first_device(UCLASS_TEST, &dev);
dev;
uclass_find_next_device(&dev)) {
ut_assert(dev);
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
}
return 0;
}
DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
/* Test that binding with uclass platdata setting occurs correctly */
static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
{
struct dm_test_perdev_uc_pdata *uc_pdata;
struct udevice *dev;
/**
* In the test_postbind() method of test uclass driver, the uclass
* platform data should be set to three test int values - test it.
*/
for (uclass_find_first_device(UCLASS_TEST, &dev);
dev;
uclass_find_next_device(&dev)) {
ut_assert(dev);
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
}
return 0;
}
DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
/* Test that autoprobe finds all the expected devices */
static int dm_test_autoprobe(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
int expected_base_add;
struct udevice *dev;
struct uclass *uc;
int i;
ut_assertok(uclass_get(UCLASS_TEST, &uc));
ut_assert(uc);
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
/* The root device should not be activated until needed */
ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
/*
* We should be able to find the three test devices, and they should
* all be activated as they are used (lazy activation, required by
* U-Boot)
*/
for (i = 0; i < 3; i++) {
ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
ut_assert(dev);
ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
"Driver %d/%s already activated", i, dev->name);
/* This should activate it */
ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
ut_assert(dev);
ut_assert(dev->flags & DM_FLAG_ACTIVATED);
/* Activating a device should activate the root device */
if (!i)
ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
}
/*
* Our 3 dm_test_info children should be passed to pre_probe and
* post_probe
*/
ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
/* Also we can check the per-device data */
expected_base_add = 0;
for (i = 0; i < 3; i++) {
struct dm_test_uclass_perdev_priv *priv;
struct dm_test_pdata *pdata;
ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
ut_assert(dev);
priv = dev_get_uclass_priv(dev);
ut_assert(priv);
ut_asserteq(expected_base_add, priv->base_add);
pdata = dev->platdata;
expected_base_add += pdata->ping_add;
}
return 0;
}
DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
/* Check that we see the correct platdata in each device */
static int dm_test_platdata(struct unit_test_state *uts)
{
const struct dm_test_pdata *pdata;
struct udevice *dev;
int i;
for (i = 0; i < 3; i++) {
ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
ut_assert(dev);
pdata = dev->platdata;
ut_assert(pdata->ping_add == test_pdata[i].ping_add);
}
return 0;
}
DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
/* Test that we can bind, probe, remove, unbind a driver */
static int dm_test_lifecycle(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
int op_count[DM_TEST_OP_COUNT];
struct udevice *dev, *test_dev;
int pingret;
int ret;
memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev));
ut_assert(dev);
ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
== op_count[DM_TEST_OP_BIND] + 1);
ut_assert(!dev->priv);
/* Probe the device - it should fail allocating private data */
dms->force_fail_alloc = 1;
ret = device_probe(dev);
ut_assert(ret == -ENOMEM);
ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
== op_count[DM_TEST_OP_PROBE] + 1);
ut_assert(!dev->priv);
/* Try again without the alloc failure */
dms->force_fail_alloc = 0;
ut_assertok(device_probe(dev));
ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
== op_count[DM_TEST_OP_PROBE] + 2);
ut_assert(dev->priv);
/* This should be device 3 in the uclass */
ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
ut_assert(dev == test_dev);
/* Try ping */
ut_assertok(test_ping(dev, 100, &pingret));
ut_assert(pingret == 102);
/* Now remove device 3 */
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
ut_assertok(device_remove(dev));
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
ut_assertok(device_unbind(dev));
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
return 0;
}
DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
/* Test that we can bind/unbind and the lists update correctly */
static int dm_test_ordering(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
int pingret;
ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev));
ut_assert(dev);
/* Bind two new devices (numbers 4 and 5) */
ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev_penultimate));
ut_assert(dev_penultimate);
ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev_last));
ut_assert(dev_last);
/* Now remove device 3 */
ut_assertok(device_remove(dev));
ut_assertok(device_unbind(dev));
/* The device numbering should have shifted down one */
ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
ut_assert(dev_penultimate == test_dev);
ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
ut_assert(dev_last == test_dev);
/* Add back the original device 3, now in position 5 */
ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev));
ut_assert(dev);
/* Try ping */
ut_assertok(test_ping(dev, 100, &pingret));
ut_assert(pingret == 102);
/* Remove 3 and 4 */
ut_assertok(device_remove(dev_penultimate));
ut_assertok(device_unbind(dev_penultimate));
ut_assertok(device_remove(dev_last));
ut_assertok(device_unbind(dev_last));
/* Our device should now be in position 3 */
ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
ut_assert(dev == test_dev);
/* Now remove device 3 */
ut_assertok(device_remove(dev));
ut_assertok(device_unbind(dev));
return 0;
}
DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
/* Check that we can perform operations on a device (do a ping) */
int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
uint32_t base, struct dm_test_priv *priv)
{
int expected;
int pingret;
/* Getting the child device should allocate platdata / priv */
ut_assertok(testfdt_ping(dev, 10, &pingret));
ut_assert(dev->priv);
ut_assert(dev->platdata);
expected = 10 + base;
ut_asserteq(expected, pingret);
/* Do another ping */
ut_assertok(testfdt_ping(dev, 20, &pingret));
expected = 20 + base;
ut_asserteq(expected, pingret);
/* Now check the ping_total */
priv = dev->priv;
ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
priv->ping_total);
return 0;
}
/* Check that we can perform operations on devices */
static int dm_test_operations(struct unit_test_state *uts)
{
struct udevice *dev;
int i;
/*
* Now check that the ping adds are what we expect. This is using the
* ping-add property in each node.
*/
for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
uint32_t base;
ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
/*
* Get the 'reg' property, which tells us what the ping add
* should be. We don't use the platdata because we want
* to test the code that sets that up (testfdt_drv_probe()).
*/
base = test_pdata[i].ping_add;
debug("dev=%d, base=%d\n", i, base);
ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
}
return 0;
}
DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
/* Remove all drivers and check that things work */
static int dm_test_remove(struct unit_test_state *uts)
{
struct udevice *dev;
int i;
for (i = 0; i < 3; i++) {
ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
ut_assert(dev);
ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
"Driver %d/%s not activated", i, dev->name);
ut_assertok(device_remove(dev));
ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
"Driver %d/%s should have deactivated", i,
dev->name);
ut_assert(!dev->priv);
}
return 0;
}
DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
/* Remove and recreate everything, check for memory leaks */
static int dm_test_leak(struct unit_test_state *uts)
{
int i;
for (i = 0; i < 2; i++) {
struct udevice *dev;
int ret;
int id;
dm_leak_check_start(uts);
ut_assertok(dm_scan_platdata(false));
ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
/* Scanning the uclass is enough to probe all the devices */
for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
for (ret = uclass_first_device(UCLASS_TEST, &dev);
dev;
ret = uclass_next_device(&dev))
;
ut_assertok(ret);
}
ut_assertok(dm_leak_check_end(uts));
}
return 0;
}
DM_TEST(dm_test_leak, 0);
/* Test uclass init/destroy methods */
static int dm_test_uclass(struct unit_test_state *uts)
{
struct uclass *uc;
ut_assertok(uclass_get(UCLASS_TEST, &uc));
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
ut_assert(uc->priv);
ut_assertok(uclass_destroy(uc));
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
return 0;
}
DM_TEST(dm_test_uclass, 0);
/**
* create_children() - Create children of a parent node
*
* @dms: Test system state
* @parent: Parent device
* @count: Number of children to create
* @key: Key value to put in first child. Subsequence children
* receive an incrementing value
* @child: If not NULL, then the child device pointers are written into
* this array.
* @return 0 if OK, -ve on error
*/
static int create_children(struct unit_test_state *uts, struct udevice *parent,
int count, int key, struct udevice *child[])
{
struct udevice *dev;
int i;
for (i = 0; i < count; i++) {
struct dm_test_pdata *pdata;
ut_assertok(device_bind_by_name(parent, false,
&driver_info_manual, &dev));
pdata = calloc(1, sizeof(*pdata));
pdata->ping_add = key + i;
dev->platdata = pdata;
if (child)
child[i] = dev;
}
return 0;
}
#define NODE_COUNT 10
static int dm_test_children(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
struct udevice *top[NODE_COUNT];
struct udevice *child[NODE_COUNT];
struct udevice *grandchild[NODE_COUNT];
struct udevice *dev;
int total;
int ret;
int i;
/* We don't care about the numbering for this test */
dms->skip_post_probe = 1;
ut_assert(NODE_COUNT > 5);
/* First create 10 top-level children */
ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
/* Now a few have their own children */
ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
/* And grandchildren */
for (i = 0; i < NODE_COUNT; i++)
ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
i == 2 ? grandchild : NULL));
/* Check total number of devices */
total = NODE_COUNT * (3 + NODE_COUNT);
ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
/* Try probing one of the grandchildren */
ut_assertok(uclass_get_device(UCLASS_TEST,
NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
ut_asserteq_ptr(grandchild[0], dev);
/*
* This should have probed the child and top node also, for a total
* of 3 nodes.
*/
ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
/* Probe the other grandchildren */
for (i = 1; i < NODE_COUNT; i++)
ut_assertok(device_probe(grandchild[i]));
ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
/* Probe everything */
for (ret = uclass_first_device(UCLASS_TEST, &dev);
dev;
ret = uclass_next_device(&dev))
;
ut_assertok(ret);
ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
/* Remove a top-level child and check that the children are removed */
ut_assertok(device_remove(top[2]));
ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
/* Try one with grandchildren */
ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
ut_asserteq_ptr(dev, top[5]);
ut_assertok(device_remove(dev));
ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
/* Try the same with unbind */
ut_assertok(device_unbind(top[2]));
ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
/* Try one with grandchildren */
ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
ut_asserteq_ptr(dev, top[6]);
ut_assertok(device_unbind(top[5]));
ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
return 0;
}
DM_TEST(dm_test_children, 0);
/* Test that pre-relocation devices work as expected */
static int dm_test_pre_reloc(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
struct udevice *dev;
/* The normal driver should refuse to bind before relocation */
ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
&driver_info_manual, &dev));
/* But this one is marked pre-reloc */
ut_assertok(device_bind_by_name(dms->root, true,
&driver_info_pre_reloc, &dev));
return 0;
}
DM_TEST(dm_test_pre_reloc, 0);
static int dm_test_uclass_before_ready(struct unit_test_state *uts)
{
struct uclass *uc;
ut_assertok(uclass_get(UCLASS_TEST, &uc));
gd->dm_root = NULL;
gd->dm_root_f = NULL;
memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
return 0;
}
DM_TEST(dm_test_uclass_before_ready, 0);
static int dm_test_uclass_devices_find(struct unit_test_state *uts)
{
struct udevice *dev;
int ret;
for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
dev;
ret = uclass_find_next_device(&dev)) {
ut_assert(!ret);
ut_assert(dev);
}
return 0;
}
DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
{
struct udevice *finddev;
struct udevice *testdev;
int findret, ret;
/*
* For each test device found in fdt like: "a-test", "b-test", etc.,
* use its name and try to find it by uclass_find_device_by_name().
* Then, on success check if:
* - current 'testdev' name is equal to the returned 'finddev' name
* - current 'testdev' pointer is equal to the returned 'finddev'
*
* We assume that, each uclass's device name is unique, so if not, then
* this will fail on checking condition: testdev == finddev, since the
* uclass_find_device_by_name(), returns the first device by given name.
*/
for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
testdev;
ret = uclass_find_next_device(&testdev)) {
ut_assertok(ret);
ut_assert(testdev);
findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
testdev->name,
&finddev);
ut_assertok(findret);
ut_assert(testdev);
ut_asserteq_str(testdev->name, finddev->name);
ut_asserteq_ptr(testdev, finddev);
}
return 0;
}
DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
static int dm_test_uclass_devices_get(struct unit_test_state *uts)
{
struct udevice *dev;
int ret;
for (ret = uclass_first_device(UCLASS_TEST, &dev);
dev;
ret = uclass_next_device(&dev)) {
ut_assert(!ret);
ut_assert(dev);
ut_assert(device_active(dev));
}
return 0;
}
DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
{
struct udevice *finddev;
struct udevice *testdev;
int ret, findret;
/*
* For each test device found in fdt like: "a-test", "b-test", etc.,
* use its name and try to get it by uclass_get_device_by_name().
* On success check if:
* - returned finddev' is active
* - current 'testdev' name is equal to the returned 'finddev' name
* - current 'testdev' pointer is equal to the returned 'finddev'
*
* We asserts that the 'testdev' is active on each loop entry, so we
* could be sure that the 'finddev' is activated too, but for sure
* we check it again.
*
* We assume that, each uclass's device name is unique, so if not, then
* this will fail on checking condition: testdev == finddev, since the
* uclass_get_device_by_name(), returns the first device by given name.
*/
for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
testdev;
ret = uclass_next_device(&testdev)) {
ut_assertok(ret);
ut_assert(testdev);
ut_assert(device_active(testdev));
findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
testdev->name,
&finddev);
ut_assertok(findret);
ut_assert(finddev);
ut_assert(device_active(finddev));
ut_asserteq_str(testdev->name, finddev->name);
ut_asserteq_ptr(testdev, finddev);
}
return 0;
}
DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
return 0;
}
DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);

261
u-boot/test/dm/eth.c Normal file
View File

@@ -0,0 +1,261 @@
/*
* Copyright (c) 2015 National Instruments
*
* (C) Copyright 2015
* Joe Hershberger <joe.hershberger@ni.com>
*
* SPDX-License-Identifier: GPL-2.0
*/
#include <common.h>
#include <dm.h>
#include <fdtdec.h>
#include <malloc.h>
#include <net.h>
#include <dm/test.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
#include <asm/eth.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
#define DM_TEST_ETH_NUM 4
static int dm_test_eth(struct unit_test_state *uts)
{
net_ping_ip = string_to_ip("1.1.2.2");
setenv("ethact", "eth@10002000");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
setenv("ethact", "eth@10003000");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10003000", getenv("ethact"));
setenv("ethact", "eth@10004000");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10004000", getenv("ethact"));
return 0;
}
DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
static int dm_test_eth_alias(struct unit_test_state *uts)
{
net_ping_ip = string_to_ip("1.1.2.2");
setenv("ethact", "eth0");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
setenv("ethact", "eth1");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10004000", getenv("ethact"));
/* Expected to fail since eth2 is not defined in the device tree */
setenv("ethact", "eth2");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
setenv("ethact", "eth5");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10003000", getenv("ethact"));
return 0;
}
DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
static int dm_test_eth_prime(struct unit_test_state *uts)
{
net_ping_ip = string_to_ip("1.1.2.2");
/* Expected to be "eth@10003000" because of ethprime variable */
setenv("ethact", NULL);
setenv("ethprime", "eth5");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10003000", getenv("ethact"));
/* Expected to be "eth@10002000" because it is first */
setenv("ethact", NULL);
setenv("ethprime", NULL);
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
return 0;
}
DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
/**
* This test case is trying to test the following scenario:
* - All ethernet devices are not probed
* - "ethaddr" for all ethernet devices are not set
* - "ethact" is set to a valid ethernet device name
*
* With Sandbox default test configuration, all ethernet devices are
* probed after power-up, so we have to manually create such scenario:
* - Remove all ethernet devices
* - Remove all "ethaddr" environment variables
* - Set "ethact" to the first ethernet device
*
* Do a ping test to see if anything goes wrong.
*/
static int dm_test_eth_act(struct unit_test_state *uts)
{
struct udevice *dev[DM_TEST_ETH_NUM];
const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000",
"sbe5", "eth@10004000"};
const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
"eth3addr", "eth1addr"};
char ethaddr[DM_TEST_ETH_NUM][18];
int i;
net_ping_ip = string_to_ip("1.1.2.2");
/* Prepare the test scenario */
for (i = 0; i < DM_TEST_ETH_NUM; i++) {
ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
ethname[i], &dev[i]));
ut_assertok(device_remove(dev[i]));
/* Invalidate MAC address */
strcpy(ethaddr[i], getenv(addrname[i]));
/* Must disable access protection for ethaddr before clearing */
setenv(".flags", addrname[i]);
setenv(addrname[i], NULL);
}
/* Set ethact to "eth@10002000" */
setenv("ethact", ethname[0]);
/* Segment fault might happen if something is wrong */
ut_asserteq(-ENODEV, net_loop(PING));
for (i = 0; i < DM_TEST_ETH_NUM; i++) {
/* Restore the env */
setenv(".flags", addrname[i]);
setenv(addrname[i], ethaddr[i]);
/* Probe the device again */
ut_assertok(device_probe(dev[i]));
}
setenv(".flags", NULL);
setenv("ethact", NULL);
return 0;
}
DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
/* The asserts include a return on fail; cleanup in the caller */
static int _dm_test_eth_rotate1(struct unit_test_state *uts)
{
/* Make sure that the default is to rotate to the next interface */
setenv("ethact", "eth@10004000");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
/* If ethrotate is no, then we should fail on a bad MAC */
setenv("ethact", "eth@10004000");
setenv("ethrotate", "no");
ut_asserteq(-EINVAL, net_loop(PING));
ut_asserteq_str("eth@10004000", getenv("ethact"));
return 0;
}
static int _dm_test_eth_rotate2(struct unit_test_state *uts)
{
/* Make sure we can skip invalid devices */
setenv("ethact", "eth@10004000");
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10004000", getenv("ethact"));
/* Make sure we can handle device name which is not eth# */
setenv("ethact", "sbe5");
ut_assertok(net_loop(PING));
ut_asserteq_str("sbe5", getenv("ethact"));
return 0;
}
static int dm_test_eth_rotate(struct unit_test_state *uts)
{
char ethaddr[18];
int retval;
/* Set target IP to mock ping */
net_ping_ip = string_to_ip("1.1.2.2");
/* Invalidate eth1's MAC address */
strcpy(ethaddr, getenv("eth1addr"));
/* Must disable access protection for eth1addr before clearing */
setenv(".flags", "eth1addr");
setenv("eth1addr", NULL);
retval = _dm_test_eth_rotate1(uts);
/* Restore the env */
setenv("eth1addr", ethaddr);
setenv("ethrotate", NULL);
if (!retval) {
/* Invalidate eth0's MAC address */
strcpy(ethaddr, getenv("ethaddr"));
/* Must disable access protection for ethaddr before clearing */
setenv(".flags", "ethaddr");
setenv("ethaddr", NULL);
retval = _dm_test_eth_rotate2(uts);
/* Restore the env */
setenv("ethaddr", ethaddr);
}
/* Restore the env */
setenv(".flags", NULL);
return retval;
}
DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
/* The asserts include a return on fail; cleanup in the caller */
static int _dm_test_net_retry(struct unit_test_state *uts)
{
/*
* eth1 is disabled and netretry is yes, so the ping should succeed and
* the active device should be eth0
*/
sandbox_eth_disable_response(1, true);
setenv("ethact", "eth@10004000");
setenv("netretry", "yes");
sandbox_eth_skip_timeout();
ut_assertok(net_loop(PING));
ut_asserteq_str("eth@10002000", getenv("ethact"));
/*
* eth1 is disabled and netretry is no, so the ping should fail and the
* active device should be eth1
*/
setenv("ethact", "eth@10004000");
setenv("netretry", "no");
sandbox_eth_skip_timeout();
ut_asserteq(-ETIMEDOUT, net_loop(PING));
ut_asserteq_str("eth@10004000", getenv("ethact"));
return 0;
}
static int dm_test_net_retry(struct unit_test_state *uts)
{
int retval;
net_ping_ip = string_to_ip("1.1.2.2");
retval = _dm_test_net_retry(uts);
/* Restore the env */
setenv("netretry", NULL);
sandbox_eth_disable_response(1, false);
return retval;
}
DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);

252
u-boot/test/dm/gpio.c Normal file
View File

@@ -0,0 +1,252 @@
/*
* Copyright (C) 2013 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <fdtdec.h>
#include <dm.h>
#include <dm/root.h>
#include <dm/test.h>
#include <dm/util.h>
#include <asm/gpio.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Test that sandbox GPIOs work correctly */
static int dm_test_gpio(struct unit_test_state *uts)
{
unsigned int offset, gpio;
struct dm_gpio_ops *ops;
struct udevice *dev;
const char *name;
int offset_count;
char buf[80];
/*
* We expect to get 3 banks. One is anonymous (just numbered) and
* comes from platdata. The other two are named a (20 gpios)
* and b (10 gpios) and come from the device tree. See
* test/dm/test.dts.
*/
ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
ut_asserteq_str(dev->name, "extra-gpios");
ut_asserteq(4, offset);
ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
name = gpio_get_bank_info(dev, &offset_count);
ut_asserteq_str("b", name);
ut_asserteq(10, offset_count);
/* Get the operations for this device */
ops = gpio_get_ops(dev);
ut_assert(ops->get_function);
/* Cannot get a value until it is reserved */
ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
/*
* Now some tests that use the 'sandbox' back door. All GPIOs
* should default to input, include b4 that we are using here.
*/
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: input: 0 [ ]", buf);
/* Change it to an output */
sandbox_gpio_set_direction(dev, offset, 1);
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: output: 0 [ ]", buf);
sandbox_gpio_set_value(dev, offset, 1);
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: output: 1 [ ]", buf);
ut_assertok(gpio_request(gpio, "testing"));
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: output: 1 [x] testing", buf);
/* Change the value a bit */
ut_asserteq(1, ops->get_value(dev, offset));
ut_assertok(ops->set_value(dev, offset, 0));
ut_asserteq(0, ops->get_value(dev, offset));
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: output: 0 [x] testing", buf);
ut_assertok(ops->set_value(dev, offset, 1));
ut_asserteq(1, ops->get_value(dev, offset));
/* Make it an open drain output, and reset it */
ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
ut_assertok(ops->set_open_drain(dev, offset, 1));
ut_asserteq(1, sandbox_gpio_get_open_drain(dev, offset));
ut_assertok(ops->set_open_drain(dev, offset, 0));
ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
/* Make it an input */
ut_assertok(ops->direction_input(dev, offset));
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: input: 1 [x] testing", buf);
sandbox_gpio_set_value(dev, offset, 0);
ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: input: 0 [x] testing", buf);
ut_assertok(gpio_free(gpio));
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b4: input: 0 [ ]", buf);
/* Check the 'a' bank also */
ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
ut_asserteq_str(dev->name, "base-gpios");
ut_asserteq(15, offset);
ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
name = gpio_get_bank_info(dev, &offset_count);
ut_asserteq_str("a", name);
ut_asserteq(20, offset_count);
return 0;
}
DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that sandbox anonymous GPIOs work correctly */
static int dm_test_gpio_anon(struct unit_test_state *uts)
{
unsigned int offset, gpio;
struct udevice *dev;
const char *name;
int offset_count;
/* And the anonymous bank */
ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
ut_asserteq_str(dev->name, "gpio_sandbox");
ut_asserteq(14, offset);
ut_asserteq(14, gpio);
name = gpio_get_bank_info(dev, &offset_count);
ut_asserteq_ptr(NULL, name);
ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
return 0;
}
DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that gpio_requestf() works as expected */
static int dm_test_gpio_requestf(struct unit_test_state *uts)
{
unsigned int offset, gpio;
struct udevice *dev;
char buf[80];
ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
sandbox_gpio_set_direction(dev, offset, 1);
sandbox_gpio_set_value(dev, offset, 1);
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
return 0;
}
DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that gpio_request() copies its string */
static int dm_test_gpio_copy(struct unit_test_state *uts)
{
unsigned int offset, gpio;
struct udevice *dev;
char buf[80], name[10];
ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
strcpy(name, "odd_name");
ut_assertok(gpio_request(gpio, name));
sandbox_gpio_set_direction(dev, offset, 1);
sandbox_gpio_set_value(dev, offset, 1);
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
strcpy(name, "nothing");
ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
return 0;
}
DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that we don't leak memory with GPIOs */
static int dm_test_gpio_leak(struct unit_test_state *uts)
{
ut_assertok(dm_test_gpio(uts));
ut_assertok(dm_test_gpio_anon(uts));
ut_assertok(dm_test_gpio_requestf(uts));
ut_assertok(dm_leak_check_end(uts));
return 0;
}
DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that we can find GPIOs using phandles */
static int dm_test_gpio_phandles(struct unit_test_state *uts)
{
struct gpio_desc desc, desc_list[8], desc_list2[8];
struct udevice *dev, *gpio_a, *gpio_b;
ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
ut_asserteq_str("a-test", dev->name);
ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
ut_asserteq_str("base-gpios", gpio_a->name);
ut_asserteq(true, !!device_active(gpio_a));
ut_asserteq_ptr(gpio_a, desc.dev);
ut_asserteq(4, desc.offset);
/* GPIOF_INPUT is the sandbox GPIO driver default */
ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
ut_assertok(dm_gpio_free(dev, &desc));
ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
0));
ut_asserteq_ptr(NULL, desc.dev);
ut_asserteq(desc.offset, 0);
ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
0));
/* Last GPIO is ignord as it comes after <0> */
ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
ARRAY_SIZE(desc_list), 0));
ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
desc_list2,
ARRAY_SIZE(desc_list2),
0));
ut_assertok(gpio_free_list(dev, desc_list, 3));
ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
ARRAY_SIZE(desc_list),
GPIOD_IS_OUT |
GPIOD_IS_OUT_ACTIVE));
ut_asserteq_ptr(gpio_a, desc_list[0].dev);
ut_asserteq(1, desc_list[0].offset);
ut_asserteq_ptr(gpio_a, desc_list[1].dev);
ut_asserteq(4, desc_list[1].offset);
ut_asserteq_ptr(gpio_b, desc_list[2].dev);
ut_asserteq(5, desc_list[2].offset);
ut_asserteq(1, dm_gpio_get_value(desc_list));
ut_assertok(gpio_free_list(dev, desc_list, 3));
ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
ARRAY_SIZE(desc_list), 0));
/* This was set to output previously, so still will be */
ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL));
/* Active low should invert the input value */
ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
return 0;
}
DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

224
u-boot/test/dm/i2c.c Normal file
View File

@@ -0,0 +1,224 @@
/*
* Copyright (C) 2013 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*
* Note: Test coverage does not include 10-bit addressing
*/
#include <common.h>
#include <dm.h>
#include <fdtdec.h>
#include <i2c.h>
#include <asm/state.h>
#include <asm/test.h>
#include <dm/device-internal.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
#include <test/ut.h>
static const int busnum;
static const int chip = 0x2c;
/* Test that we can find buses and chips */
static int dm_test_i2c_find(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
const int no_chip = 0x10;
ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_I2C, busnum,
false, &bus));
/*
* i2c_post_bind() will bind devices to chip selects. Check this then
* remove the emulation and the slave device.
*/
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
ut_asserteq(-ENODEV, dm_i2c_probe(bus, no_chip, 0, &dev));
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
return 0;
}
DM_TEST(dm_test_i2c_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_read_write(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
uint8_t buf[5];
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0AB\0", sizeof(buf)));
return 0;
}
DM_TEST(dm_test_i2c_read_write, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_speed(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
uint8_t buf[5];
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
/* Use test mode so we create the required errors for invalid speeds */
sandbox_i2c_set_test_mode(bus, true);
ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
ut_assertok(dm_i2c_set_bus_speed(bus, 100000));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(dm_i2c_set_bus_speed(bus, 400000));
ut_asserteq(400000, dm_i2c_get_bus_speed(bus));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5));
sandbox_i2c_set_test_mode(bus, false);
return 0;
}
DM_TEST(dm_test_i2c_speed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_offset_len(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
uint8_t buf[5];
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
ut_assertok(i2c_set_chip_offset_len(dev, 1));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
/* This is not supported by the uclass */
ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5));
return 0;
}
DM_TEST(dm_test_i2c_offset_len, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_probe_empty(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
/* Use test mode so that this chip address will always probe */
sandbox_i2c_set_test_mode(bus, true);
ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
sandbox_i2c_set_test_mode(bus, false);
return 0;
}
DM_TEST(dm_test_i2c_probe_empty, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_bytewise(struct unit_test_state *uts)
{
struct udevice *bus, *dev;
struct udevice *eeprom;
uint8_t buf[5];
ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
/* Tell the EEPROM to only read/write one register at a time */
ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
ut_assertnonnull(eeprom);
sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE);
/* Now we only get the first byte - the rest will be 0xff */
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
/* If we do a separate transaction for each byte, it works */
ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0\0\0\0", sizeof(buf)));
/* This will only write A */
ut_assertok(i2c_set_chip_flags(dev, 0));
ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
/* Check that the B was ignored */
ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0A\0\0\0", sizeof(buf)));
/* Now write it again with the new flags, it should work */
ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS));
ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\xff\xff\xff\xff", sizeof(buf)));
ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS |
DM_I2C_CHIP_RD_ADDRESS));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "\0\0AB\0\0", sizeof(buf)));
/* Restore defaults */
sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE);
ut_assertok(i2c_set_chip_flags(dev, 0));
return 0;
}
DM_TEST(dm_test_i2c_bytewise, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_i2c_offset(struct unit_test_state *uts)
{
struct udevice *eeprom;
struct udevice *dev;
uint8_t buf[5];
ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
/* Do a transfer so we can find the emulator */
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(uclass_first_device(UCLASS_I2C_EMUL, &eeprom));
/* Offset length 0 */
sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
ut_assertok(i2c_set_chip_offset_len(dev, 0));
ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "AB\0\0\0\0", sizeof(buf)));
/* Offset length 1 */
sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
ut_assertok(i2c_set_chip_offset_len(dev, 1));
ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0, buf, 5));
ut_assertok(memcmp(buf, "ABAB\0", sizeof(buf)));
/* Offset length 2 */
sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
ut_assertok(i2c_set_chip_offset_len(dev, 2));
ut_assertok(dm_i2c_write(dev, 0x210, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0x210, buf, 5));
ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
/* Offset length 3 */
sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
ut_assertok(i2c_set_chip_offset_len(dev, 2));
ut_assertok(dm_i2c_write(dev, 0x410, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0x410, buf, 5));
ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
/* Offset length 4 */
sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
ut_assertok(i2c_set_chip_offset_len(dev, 2));
ut_assertok(dm_i2c_write(dev, 0x420, (uint8_t *)"AB", 2));
ut_assertok(dm_i2c_read(dev, 0x420, buf, 5));
ut_assertok(memcmp(buf, "AB\0\0\0", sizeof(buf)));
/* Restore defaults */
sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
return 0;
}
DM_TEST(dm_test_i2c_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

72
u-boot/test/dm/led.c Normal file
View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <led.h>
#include <asm/gpio.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Base test of the led uclass */
static int dm_test_led_base(struct unit_test_state *uts)
{
struct udevice *dev;
/* Get the top-level device */
ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 3, &dev));
return 0;
}
DM_TEST(dm_test_led_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test of the led uclass using the led_gpio driver */
static int dm_test_led_gpio(struct unit_test_state *uts)
{
const int offset = 1;
struct udevice *dev, *gpio;
/*
* Check that we can manipulate an LED. LED 1 is connected to GPIO
* bank gpio_a, offset 1.
*/
ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
led_set_on(dev, 1);
ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
led_set_on(dev, 0);
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
return 0;
}
DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test obtaining an LED by label */
static int dm_test_led_label(struct unit_test_state *uts)
{
struct udevice *dev, *cmp;
ut_assertok(led_get_by_label("sandbox:red", &dev));
ut_asserteq(1, device_active(dev));
ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
ut_asserteq_ptr(dev, cmp);
ut_assertok(led_get_by_label("sandbox:green", &dev));
ut_asserteq(1, device_active(dev));
ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp));
ut_asserteq_ptr(dev, cmp);
ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
return 0;
}
DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

31
u-boot/test/dm/mailbox.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* SPDX-License-Identifier: GPL-2.0
*/
#include <common.h>
#include <dm.h>
#include <dm/test.h>
#include <asm/mbox.h>
#include <test/ut.h>
static int dm_test_mailbox(struct unit_test_state *uts)
{
struct udevice *dev;
uint32_t msg;
ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "mbox-test", &dev));
ut_assertok(sandbox_mbox_test_get(dev));
ut_asserteq(-ETIMEDOUT, sandbox_mbox_test_recv(dev, &msg));
ut_assertok(sandbox_mbox_test_send(dev, 0xaaff9955UL));
ut_assertok(sandbox_mbox_test_recv(dev, &msg));
ut_asserteq(msg, 0xaaff9955UL ^ SANDBOX_MBOX_PING_XOR);
ut_asserteq(-ETIMEDOUT, sandbox_mbox_test_recv(dev, &msg));
ut_assertok(sandbox_mbox_test_free(dev));
return 0;
}
DM_TEST(dm_test_mailbox, DM_TESTF_SCAN_FDT);

46
u-boot/test/dm/mmc.c Normal file
View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <mmc.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/*
* Basic test of the mmc uclass. We could expand this by implementing an MMC
* stack for sandbox, or at least implementing the basic operation.
*/
static int dm_test_mmc_base(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
return 0;
}
DM_TEST(dm_test_mmc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_mmc_blk(struct unit_test_state *uts)
{
struct udevice *dev;
struct blk_desc *dev_desc;
char cmp[1024];
ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
ut_assertok(blk_get_device_by_str("mmc", "0", &dev_desc));
/* Read a few blocks and look for the string we expect */
ut_asserteq(512, dev_desc->blksz);
memset(cmp, '\0', sizeof(cmp));
ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
ut_assertok(strcmp(cmp, "this is a test"));
return 0;
}
DM_TEST(dm_test_mmc_blk, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

67
u-boot/test/dm/pci.c Normal file
View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <asm/io.h>
#include <dm/test.h>
#include <test/ut.h>
/* Test that sandbox PCI works correctly */
static int dm_test_pci_base(struct unit_test_state *uts)
{
struct udevice *bus;
ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
return 0;
}
DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that sandbox PCI bus numbering works correctly */
static int dm_test_pci_busnum(struct unit_test_state *uts)
{
struct udevice *bus;
ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
return 0;
}
DM_TEST(dm_test_pci_busnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that we can use the swapcase device correctly */
static int dm_test_pci_swapcase(struct unit_test_state *uts)
{
struct udevice *emul, *swap;
ulong io_addr, mem_addr;
char *ptr;
/* Check that asking for the device automatically fires up PCI */
ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
ut_assert(device_active(swap));
/* First test I/O */
io_addr = dm_pci_read_bar32(swap, 0);
outb(2, io_addr);
ut_asserteq(2, inb(io_addr));
/*
* Now test memory mapping - note we must unmap and remap to cause
* the swapcase emulation to see our data and response.
*/
mem_addr = dm_pci_read_bar32(swap, 1);
ptr = map_sysmem(mem_addr, 20);
strcpy(ptr, "This is a TesT");
unmap_sysmem(ptr);
ptr = map_sysmem(mem_addr, 20);
ut_asserteq_str("tHIS IS A tESt", ptr);
unmap_sysmem(ptr);
return 0;
}
DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

69
u-boot/test/dm/pmic.c Normal file
View File

@@ -0,0 +1,69 @@
/*
* Tests for the driver model pmic API
*
* Copyright (c) 2015 Samsung Electronics
* Przemyslaw Marczak <p.marczak@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <errno.h>
#include <dm.h>
#include <fdtdec.h>
#include <malloc.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/util.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <power/pmic.h>
#include <power/sandbox_pmic.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Test PMIC get method */
static int dm_test_power_pmic_get(struct unit_test_state *uts)
{
const char *name = "sandbox_pmic";
struct udevice *dev;
ut_assertok(pmic_get(name, &dev));
ut_assertnonnull(dev);
/* Check PMIC's name */
ut_asserteq_str(name, dev->name);
return 0;
}
DM_TEST(dm_test_power_pmic_get, DM_TESTF_SCAN_FDT);
/* Test PMIC I/O */
static int dm_test_power_pmic_io(struct unit_test_state *uts)
{
const char *name = "sandbox_pmic";
uint8_t out_buffer, in_buffer;
struct udevice *dev;
int reg_count, i;
ut_assertok(pmic_get(name, &dev));
reg_count = pmic_reg_count(dev);
ut_asserteq(reg_count, SANDBOX_PMIC_REG_COUNT);
/*
* Test PMIC I/O - write and read a loop counter.
* usually we can't write to all PMIC's registers in the real hardware,
* but we can to the sandbox pmic.
*/
for (i = 0; i < reg_count; i++) {
out_buffer = i;
ut_assertok(pmic_write(dev, i, &out_buffer, 1));
ut_assertok(pmic_read(dev, i, &in_buffer, 1));
ut_asserteq(out_buffer, in_buffer);
}
return 0;
}
DM_TEST(dm_test_power_pmic_io, DM_TESTF_SCAN_FDT);

28
u-boot/test/dm/ram.c Normal file
View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <ram.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Basic test of the ram uclass */
static int dm_test_ram_base(struct unit_test_state *uts)
{
struct udevice *dev;
struct ram_info info;
ut_assertok(uclass_get_device(UCLASS_RAM, 0, &dev));
ut_assertok(ram_get_info(dev, &info));
ut_asserteq(0, info.base);
ut_asserteq(gd->ram_size, info.size);
return 0;
}
DM_TEST(dm_test_ram_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

82
u-boot/test/dm/regmap.c Normal file
View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2015 Google, Inc
2 *
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <mapmem.h>
#include <regmap.h>
#include <syscon.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Base test of register maps */
static int dm_test_regmap_base(struct unit_test_state *uts)
{
struct udevice *dev;
struct regmap *map;
int i;
ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
map = syscon_get_regmap(dev);
ut_assertok_ptr(map);
ut_asserteq(1, map->range_count);
ut_asserteq(0x10, map->base);
ut_asserteq(0x10, map->range->start);
ut_asserteq(4, map->range->size);
ut_asserteq_ptr(&map->base_range, map->range);
ut_asserteq(0x10, map_to_sysmem(regmap_get_range(map, 0)));
ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
map = syscon_get_regmap(dev);
ut_assertok_ptr(map);
ut_asserteq(4, map->range_count);
ut_asserteq(0x20, map->base);
ut_assert(&map->base_range != map->range);
for (i = 0; i < 4; i++) {
const unsigned long addr = 0x20 + 8 * i;
ut_asserteq(addr, map->range[i].start);
ut_asserteq(5 + i, map->range[i].size);
ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
}
/* Check that we can't pretend a different device is a syscon */
ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
map = syscon_get_regmap(dev);
ut_asserteq_ptr(ERR_PTR(-ENOEXEC), map);
return 0;
}
DM_TEST(dm_test_regmap_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test we can access a regmap through syscon */
static int dm_test_regmap_syscon(struct unit_test_state *uts)
{
struct regmap *map;
map = syscon_get_regmap_by_driver_data(SYSCON0);
ut_assertok_ptr(map);
ut_asserteq(1, map->range_count);
map = syscon_get_regmap_by_driver_data(SYSCON1);
ut_assertok_ptr(map);
ut_asserteq(4, map->range_count);
map = syscon_get_regmap_by_driver_data(SYSCON_COUNT);
ut_asserteq_ptr(ERR_PTR(-ENODEV), map);
ut_asserteq(0x10, map_to_sysmem(syscon_get_first_range(SYSCON0)));
ut_asserteq(0x20, map_to_sysmem(syscon_get_first_range(SYSCON1)));
ut_asserteq_ptr(ERR_PTR(-ENODEV),
syscon_get_first_range(SYSCON_COUNT));
return 0;
}
DM_TEST(dm_test_regmap_syscon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

325
u-boot/test/dm/regulator.c Normal file
View File

@@ -0,0 +1,325 @@
/*
* Tests for the driver model regulator API
*
* Copyright (c) 2015 Samsung Electronics
* Przemyslaw Marczak <p.marczak@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <errno.h>
#include <dm.h>
#include <fdtdec.h>
#include <malloc.h>
#include <dm/device-internal.h>
#include <dm/root.h>
#include <dm/util.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <power/pmic.h>
#include <power/regulator.h>
#include <power/sandbox_pmic.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
enum {
BUCK1,
BUCK2,
LDO1,
LDO2,
OUTPUT_COUNT,
};
enum {
DEVNAME = 0,
PLATNAME,
OUTPUT_NAME_COUNT,
};
static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = {
/* devname, platname */
{ SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME },
{ SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME },
{ SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME},
{ SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME},
};
/* Test regulator get method */
static int dm_test_power_regulator_get(struct unit_test_state *uts)
{
struct dm_regulator_uclass_platdata *uc_pdata;
struct udevice *dev_by_devname;
struct udevice *dev_by_platname;
const char *devname;
const char *platname;
int i;
for (i = 0; i < OUTPUT_COUNT; i++) {
/*
* Do the test for each regulator's devname and platname,
* which are related to a single device.
*/
devname = regulator_names[i][DEVNAME];
platname = regulator_names[i][PLATNAME];
/*
* Check, that regulator_get_by_devname() function, returns
* a device with the name equal to the requested one.
*/
ut_assertok(regulator_get_by_devname(devname, &dev_by_devname));
ut_asserteq_str(devname, dev_by_devname->name);
/*
* Check, that regulator_get_by_platname() function, returns
* a device with the name equal to the requested one.
*/
ut_assertok(regulator_get_by_platname(platname, &dev_by_platname));
uc_pdata = dev_get_uclass_platdata(dev_by_platname);
ut_assert(uc_pdata);
ut_asserteq_str(platname, uc_pdata->name);
/*
* Check, that the pointers returned by both get functions,
* points to the same regulator device.
*/
ut_asserteq_ptr(dev_by_devname, dev_by_platname);
}
return 0;
}
DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT);
/* Test regulator set and get Voltage method */
static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts)
{
struct dm_regulator_uclass_platdata *uc_pdata;
struct udevice *dev;
const char *platname;
int val_set, val_get;
/* Set and get Voltage of BUCK1 - set to 'min' constraint */
platname = regulator_names[BUCK1][PLATNAME];
ut_assertok(regulator_get_by_platname(platname, &dev));
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
val_set = uc_pdata->min_uV;
ut_assertok(regulator_set_value(dev, val_set));
val_get = regulator_get_value(dev);
ut_assert(val_get >= 0);
ut_asserteq(val_set, val_get);
return 0;
}
DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT);
/* Test regulator set and get Current method */
static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts)
{
struct dm_regulator_uclass_platdata *uc_pdata;
struct udevice *dev;
const char *platname;
int val_set, val_get;
/* Set and get the Current of LDO1 - set to 'min' constraint */
platname = regulator_names[LDO1][PLATNAME];
ut_assertok(regulator_get_by_platname(platname, &dev));
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
val_set = uc_pdata->min_uA;
ut_assertok(regulator_set_current(dev, val_set));
val_get = regulator_get_current(dev);
ut_assert(val_get >= 0);
ut_asserteq(val_set, val_get);
/* Check LDO2 current limit constraints - should be -ENODATA */
platname = regulator_names[LDO2][PLATNAME];
ut_assertok(regulator_get_by_platname(platname, &dev));
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
ut_asserteq(-ENODATA, uc_pdata->min_uA);
ut_asserteq(-ENODATA, uc_pdata->max_uA);
/* Try set the Current of LDO2 - should return -ENOSYS */
ut_asserteq(-ENOSYS, regulator_set_current(dev, 0));
return 0;
}
DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT);
/* Test regulator set and get Enable method */
static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts)
{
const char *platname;
struct udevice *dev;
bool val_set = true;
/* Set the Enable of LDO1 - default is disabled */
platname = regulator_names[LDO1][PLATNAME];
ut_assertok(regulator_get_by_platname(platname, &dev));
ut_assertok(regulator_set_enable(dev, val_set));
/* Get the Enable state of LDO1 and compare it with the requested one */
ut_asserteq(regulator_get_enable(dev), val_set);
return 0;
}
DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT);
/* Test regulator set and get mode method */
static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts)
{
const char *platname;
struct udevice *dev;
int val_set = LDO_OM_SLEEP;
/* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */
platname = regulator_names[LDO1][PLATNAME];
ut_assertok(regulator_get_by_platname(platname, &dev));
ut_assertok(regulator_set_mode(dev, val_set));
/* Get the mode id of LDO1 and compare it with the requested one */
ut_asserteq(regulator_get_mode(dev), val_set);
return 0;
}
DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT);
/* Test regulator autoset method */
static int dm_test_power_regulator_autoset(struct unit_test_state *uts)
{
const char *platname;
struct udevice *dev, *dev_autoset;
/*
* Test the BUCK1 with fdt properties
* - min-microvolt = max-microvolt = 1200000
* - min-microamp = max-microamp = 200000
* - always-on = set
* - boot-on = not set
* Expected output state: uV=1200000; uA=200000; output enabled
*/
platname = regulator_names[BUCK1][PLATNAME];
ut_assertok(regulator_autoset_by_name(platname, &dev_autoset));
/* Check, that the returned device is proper */
ut_assertok(regulator_get_by_platname(platname, &dev));
ut_asserteq_ptr(dev, dev_autoset);
/* Check the setup after autoset */
ut_asserteq(regulator_get_value(dev),
SANDBOX_BUCK1_AUTOSET_EXPECTED_UV);
ut_asserteq(regulator_get_current(dev),
SANDBOX_BUCK1_AUTOSET_EXPECTED_UA);
ut_asserteq(regulator_get_enable(dev),
SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE);
return 0;
}
DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT);
/*
* Struct setting: to keep the expected output settings.
* @voltage: Voltage value [uV]
* @current: Current value [uA]
* @enable: output enable state: true/false
*/
struct setting {
int voltage;
int current;
bool enable;
};
/*
* platname_list: an array of regulator platform names.
* For testing regulator_list_autoset() for outputs:
* - LDO1
* - LDO2
*/
static const char *platname_list[] = {
SANDBOX_LDO1_PLATNAME,
SANDBOX_LDO2_PLATNAME,
NULL,
};
/*
* expected_setting_list: an array of regulator output setting, expected after
* call of the regulator_list_autoset() for the "platname_list" array.
* For testing results of regulator_list_autoset() for outputs:
* - LDO1
* - LDO2
* The settings are defined in: include/power/sandbox_pmic.h
*/
static const struct setting expected_setting_list[] = {
[0] = { /* LDO1 */
.voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV,
.current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA,
.enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE,
},
[1] = { /* LDO2 */
.voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV,
.current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA,
.enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE,
},
};
static int list_count = ARRAY_SIZE(expected_setting_list);
/* Test regulator list autoset method */
static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts)
{
struct udevice *dev_list[2], *dev;
int i;
/*
* Test the settings of the regulator list:
* LDO1 with fdt properties:
* - min-microvolt = max-microvolt = 1800000
* - min-microamp = max-microamp = 100000
* - always-on = not set
* - boot-on = set
* Expected output state: uV=1800000; uA=100000; output enabled
*
* LDO2 with fdt properties:
* - min-microvolt = max-microvolt = 3300000
* - always-on = not set
* - boot-on = not set
* Expected output state: uV=300000(default); output disabled(default)
* The expected settings are defined in: include/power/sandbox_pmic.h.
*/
ut_assertok(regulator_list_autoset(platname_list, dev_list, false));
for (i = 0; i < list_count; i++) {
/* Check, that the returned device is non-NULL */
ut_assert(dev_list[i]);
/* Check, that the returned device is proper */
ut_assertok(regulator_get_by_platname(platname_list[i], &dev));
ut_asserteq_ptr(dev_list[i], dev);
/* Check, that regulator output Voltage value is as expected */
ut_asserteq(regulator_get_value(dev_list[i]),
expected_setting_list[i].voltage);
/* Check, that regulator output Current value is as expected */
ut_asserteq(regulator_get_current(dev_list[i]),
expected_setting_list[i].current);
/* Check, that regulator output Enable state is as expected */
ut_asserteq(regulator_get_enable(dev_list[i]),
expected_setting_list[i].enable);
}
return 0;
}
DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT);

View File

@@ -0,0 +1,67 @@
/*
* (C) Copyright 2015
* Texas Instruments Incorporated - http://www.ti.com/
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <remoteproc.h>
#include <dm/test.h>
#include <test/ut.h>
/**
* dm_test_remoteproc_base() - test the operations after initializations
* @uts: unit test state
*
* Return: 0 if test passed, else error
*/
static int dm_test_remoteproc_base(struct unit_test_state *uts)
{
if (!rproc_is_initialized())
ut_assertok(rproc_init());
/* Ensure we are initialized */
ut_asserteq(true, rproc_is_initialized());
/* platform data device 1 */
ut_assertok(rproc_stop(0));
ut_assertok(rproc_reset(0));
/* -> invalid attempt tests */
ut_asserteq(-EINVAL, rproc_start(0));
ut_asserteq(-EINVAL, rproc_ping(0));
/* Valid tests */
ut_assertok(rproc_load(0, 1, 0));
ut_assertok(rproc_start(0));
ut_assertok(rproc_is_running(0));
ut_assertok(rproc_ping(0));
ut_assertok(rproc_reset(0));
ut_assertok(rproc_stop(0));
/* dt device device 1 */
ut_assertok(rproc_stop(1));
ut_assertok(rproc_reset(1));
ut_assertok(rproc_load(1, 1, 0));
ut_assertok(rproc_start(1));
ut_assertok(rproc_is_running(1));
ut_assertok(rproc_ping(1));
ut_assertok(rproc_reset(1));
ut_assertok(rproc_stop(1));
/* dt device device 2 */
ut_assertok(rproc_stop(0));
ut_assertok(rproc_reset(0));
/* -> invalid attempt tests */
ut_asserteq(-EINVAL, rproc_start(0));
ut_asserteq(-EINVAL, rproc_ping(0));
/* Valid tests */
ut_assertok(rproc_load(2, 1, 0));
ut_assertok(rproc_start(2));
ut_assertok(rproc_is_running(2));
ut_assertok(rproc_ping(2));
ut_assertok(rproc_reset(2));
ut_assertok(rproc_stop(2));
return 0;
}
DM_TEST(dm_test_remoteproc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

39
u-boot/test/dm/reset.c Normal file
View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016, NVIDIA CORPORATION.
*
* SPDX-License-Identifier: GPL-2.0
*/
#include <common.h>
#include <dm.h>
#include <dm/test.h>
#include <asm/reset.h>
#include <test/ut.h>
/* This must match the specifier for mbox-names="test" in the DT node */
#define TEST_RESET_ID 2
static int dm_test_reset(struct unit_test_state *uts)
{
struct udevice *dev_reset;
struct udevice *dev_test;
ut_assertok(uclass_get_device_by_name(UCLASS_RESET, "reset-ctl",
&dev_reset));
ut_asserteq(0, sandbox_reset_query(dev_reset, TEST_RESET_ID));
ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "reset-ctl-test",
&dev_test));
ut_assertok(sandbox_reset_test_get(dev_test));
ut_assertok(sandbox_reset_test_assert(dev_test));
ut_asserteq(1, sandbox_reset_query(dev_reset, TEST_RESET_ID));
ut_assertok(sandbox_reset_test_deassert(dev_test));
ut_asserteq(0, sandbox_reset_query(dev_reset, TEST_RESET_ID));
ut_assertok(sandbox_reset_test_free(dev_test));
return 0;
}
DM_TEST(dm_test_reset, DM_TESTF_SCAN_FDT);

175
u-boot/test/dm/rtc.c Normal file
View File

@@ -0,0 +1,175 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <dm.h>
#include <rtc.h>
#include <asm/io.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>
/* Simple RTC sanity check */
static int dm_test_rtc_base(struct unit_test_state *uts)
{
struct udevice *dev;
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_RTC, 2, &dev));
ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev));
return 0;
}
DM_TEST(dm_test_rtc_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static void show_time(const char *msg, struct rtc_time *time)
{
printf("%s: %02d/%02d/%04d %02d:%02d:%02d\n", msg,
time->tm_mday, time->tm_mon, time->tm_year,
time->tm_hour, time->tm_min, time->tm_sec);
}
static int cmp_times(struct rtc_time *expect, struct rtc_time *time, bool show)
{
bool same;
same = expect->tm_sec == time->tm_sec;
same &= expect->tm_min == time->tm_min;
same &= expect->tm_hour == time->tm_hour;
same &= expect->tm_mday == time->tm_mday;
same &= expect->tm_mon == time->tm_mon;
same &= expect->tm_year == time->tm_year;
if (!same && show) {
show_time("expected", expect);
show_time("actual", time);
}
return same ? 0 : -EINVAL;
}
/* Set and get the time */
static int dm_test_rtc_set_get(struct unit_test_state *uts)
{
struct rtc_time now, time, cmp;
struct udevice *dev, *emul;
long offset, old_offset, old_base_time;
ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
ut_assertok(dm_rtc_get(dev, &now));
ut_assertok(device_find_first_child(dev, &emul));
ut_assert(emul != NULL);
/* Tell the RTC to go into manual mode */
old_offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
memset(&time, '\0', sizeof(time));
time.tm_mday = 25;
time.tm_mon = 8;
time.tm_year = 2004;
time.tm_sec = 0;
time.tm_min = 18;
time.tm_hour = 18;
ut_assertok(dm_rtc_set(dev, &time));
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev, &cmp));
ut_assertok(cmp_times(&time, &cmp, true));
/* Increment by 1 second */
offset = sandbox_i2c_rtc_set_offset(emul, false, 0);
sandbox_i2c_rtc_set_offset(emul, false, offset + 1);
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev, &cmp));
ut_asserteq(1, cmp.tm_sec);
/* Check against original offset */
sandbox_i2c_rtc_set_offset(emul, false, old_offset);
ut_assertok(dm_rtc_get(dev, &cmp));
ut_assertok(cmp_times(&now, &cmp, true));
/* Back to the original offset */
sandbox_i2c_rtc_set_offset(emul, false, 0);
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev, &cmp));
ut_assertok(cmp_times(&now, &cmp, true));
/* Increment the base time by 1 emul */
sandbox_i2c_rtc_get_set_base_time(emul, old_base_time + 1);
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev, &cmp));
if (now.tm_sec == 59) {
ut_asserteq(0, cmp.tm_sec);
} else {
ut_asserteq(now.tm_sec + 1, cmp.tm_sec);
}
old_offset = sandbox_i2c_rtc_set_offset(emul, true, 0);
return 0;
}
DM_TEST(dm_test_rtc_set_get, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Reset the time */
static int dm_test_rtc_reset(struct unit_test_state *uts)
{
struct rtc_time now;
struct udevice *dev, *emul;
long old_base_time, base_time;
ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
ut_assertok(dm_rtc_get(dev, &now));
ut_assertok(device_find_first_child(dev, &emul));
ut_assert(emul != NULL);
old_base_time = sandbox_i2c_rtc_get_set_base_time(emul, 0);
ut_asserteq(0, sandbox_i2c_rtc_get_set_base_time(emul, -1));
/* Resetting the RTC should put he base time back to normal */
ut_assertok(dm_rtc_reset(dev));
base_time = sandbox_i2c_rtc_get_set_base_time(emul, -1);
ut_asserteq(old_base_time, base_time);
return 0;
}
DM_TEST(dm_test_rtc_reset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Check that two RTC devices can be used independently */
static int dm_test_rtc_dual(struct unit_test_state *uts)
{
struct rtc_time now1, now2, cmp;
struct udevice *dev1, *dev2;
struct udevice *emul1, *emul2;
long offset;
ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev1));
ut_assertok(dm_rtc_get(dev1, &now1));
ut_assertok(uclass_get_device(UCLASS_RTC, 1, &dev2));
ut_assertok(dm_rtc_get(dev2, &now2));
ut_assertok(device_find_first_child(dev1, &emul1));
ut_assert(emul1 != NULL);
ut_assertok(device_find_first_child(dev2, &emul2));
ut_assert(emul2 != NULL);
offset = sandbox_i2c_rtc_set_offset(emul1, false, -1);
sandbox_i2c_rtc_set_offset(emul2, false, offset + 1);
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev2, &cmp));
ut_asserteq(-EINVAL, cmp_times(&now1, &cmp, false));
memset(&cmp, '\0', sizeof(cmp));
ut_assertok(dm_rtc_get(dev1, &cmp));
ut_assertok(cmp_times(&now1, &cmp, true));
return 0;
}
DM_TEST(dm_test_rtc_dual, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

43
u-boot/test/dm/sf.c Normal file
View File

@@ -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/test.h>
#include <dm/util.h>
#include <test/ut.h>
/* Test that sandbox SPI flash works correctly */
static int dm_test_spi_flash(struct unit_test_state *uts)
{
/*
* 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 - 0 spi.bin 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);

127
u-boot/test/dm/spi.c Normal file
View File

@@ -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 <asm/state.h>
#include <dm/device-internal.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
#include <test/ut.h>
/* Test that we can find buses and chip-selects */
static int dm_test_spi_find(struct unit_test_state *uts)
{
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;
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(NULL, info.dev);
/* 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));
sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
ut_assertok(spi_cs_info(bus, cs, &info));
ut_asserteq_ptr(NULL, info.dev);
/* 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 unit_test_state *uts)
{
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);

114
u-boot/test/dm/spmi.c Normal file
View File

@@ -0,0 +1,114 @@
/*
* (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <fdtdec.h>
#include <dm.h>
#include <dm/device.h>
#include <dm/root.h>
#include <dm/test.h>
#include <dm/util.h>
#include <power/pmic.h>
#include <spmi/spmi.h>
#include <asm/gpio.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Test if bus childs got probed propperly*/
static int dm_test_spmi_probe(struct unit_test_state *uts)
{
const char *name = "spmi@0";
struct udevice *bus, *dev;
ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
/* Check bus name */
ut_asserteq_str(name, bus->name);
/* Check that it has some devices */
ut_asserteq(device_has_children(bus), true);
ut_assertok(device_find_first_child(bus, &dev));
/* There should be at least one child */
ut_assertnonnull(dev);
/* Check that only PMICs are connected to the bus */
while (dev) {
ut_asserteq(device_get_uclass_id(dev), UCLASS_PMIC);
device_find_next_child(&dev);
}
return 0;
}
DM_TEST(dm_test_spmi_probe, DM_TESTF_SCAN_FDT);
/* Test if it's possible to read bus directly and indirectly */
static int dm_test_spmi_access(struct unit_test_state *uts)
{
const char *pmic_name = "pm8916@0";
struct udevice *bus, *pmic;
ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
ut_assertok(device_get_child(bus, 0, &pmic));
/* Sanity check if it's proper PMIC */
ut_asserteq_str(pmic_name, pmic->name);
/* Read PMIC ID reg using SPMI bus - it assumes it has slaveID == 0*/
ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x4), 0x10);
ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x5), 0x5);
/* Read ID reg via pmic interface */
ut_asserteq(pmic_reg_read(pmic, 0xC004), 0x10);
ut_asserteq(pmic_reg_read(pmic, 0xC005), 0x5);
return 0;
}
DM_TEST(dm_test_spmi_access, DM_TESTF_SCAN_FDT);
/* Test if it's possible to access GPIO that should be in pmic */
static int dm_test_spmi_access_peripheral(struct unit_test_state *uts)
{
struct udevice *dev;
unsigned int offset, gpio;
const char *name;
int offset_count;
/* Get second pin of PMIC GPIO */
ut_assertok(gpio_lookup_name("spmi1", &dev, &offset, &gpio));
/* Check if PMIC is parent */
ut_asserteq(device_get_uclass_id(dev->parent), UCLASS_PMIC);
/* This should be second gpio */
ut_asserteq(1, offset);
name = gpio_get_bank_info(dev, &offset_count);
/* Check bank name */
ut_asserteq_str("spmi", name);
/* Check pin count */
ut_asserteq(4, offset_count);
ut_assertok(gpio_request(gpio, "testing"));
/* Try to set/clear gpio */
ut_assertok(gpio_direction_output(gpio, 0));
ut_asserteq(gpio_get_value(gpio), 0);
ut_assertok(gpio_direction_output(gpio, 1));
ut_asserteq(gpio_get_value(gpio), 1);
ut_assertok(gpio_direction_input(gpio));
ut_asserteq(gpio_get_value(gpio), 1);
ut_assertok(gpio_free(gpio));
return 0;
}
DM_TEST(dm_test_spmi_access_peripheral, DM_TESTF_SCAN_FDT);

48
u-boot/test/dm/syscon.c Normal file
View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <syscon.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Base test of system controllers */
static int dm_test_syscon_base(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
ut_asserteq(SYSCON0, dev->driver_data);
ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
ut_asserteq(SYSCON1, dev->driver_data);
ut_asserteq(-ENODEV, uclass_get_device(UCLASS_SYSCON, 2, &dev));
return 0;
}
DM_TEST(dm_test_syscon_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test system controller finding */
static int dm_test_syscon_by_driver_data(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(syscon_get_by_driver_data(SYSCON0, &dev));
ut_asserteq(SYSCON0, dev->driver_data);
ut_assertok(syscon_get_by_driver_data(SYSCON1, &dev));
ut_asserteq(SYSCON1, dev->driver_data);
ut_asserteq(-ENODEV, syscon_get_by_driver_data(2, &dev));
return 0;
}
DM_TEST(dm_test_syscon_by_driver_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

74
u-boot/test/dm/sysreset.c Normal file
View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <sysreset.h>
#include <asm/state.h>
#include <asm/test.h>
#include <dm/test.h>
#include <test/ut.h>
/* Test that we can use particular sysreset devices */
static int dm_test_sysreset_base(struct unit_test_state *uts)
{
struct sandbox_state *state = state_get_current();
struct udevice *dev;
/* Device 0 is the platform data device - it should never respond */
ut_assertok(uclass_get_device(UCLASS_SYSRESET, 0, &dev));
ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_WARM));
ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_COLD));
ut_asserteq(-ENODEV, sysreset_request(dev, SYSRESET_POWER));
/* Device 1 is the warm sysreset device */
ut_assertok(uclass_get_device(UCLASS_SYSRESET, 1, &dev));
ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_WARM));
ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_COLD));
ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_POWER));
state->sysreset_allowed[SYSRESET_WARM] = true;
ut_asserteq(-EINPROGRESS, sysreset_request(dev, SYSRESET_WARM));
state->sysreset_allowed[SYSRESET_WARM] = false;
/* Device 2 is the cold sysreset device */
ut_assertok(uclass_get_device(UCLASS_SYSRESET, 2, &dev));
ut_asserteq(-ENOSYS, sysreset_request(dev, SYSRESET_WARM));
ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_COLD));
state->sysreset_allowed[SYSRESET_POWER] = false;
ut_asserteq(-EACCES, sysreset_request(dev, SYSRESET_POWER));
state->sysreset_allowed[SYSRESET_POWER] = true;
return 0;
}
DM_TEST(dm_test_sysreset_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that we can walk through the sysreset devices */
static int dm_test_sysreset_walk(struct unit_test_state *uts)
{
struct sandbox_state *state = state_get_current();
/* If we generate a power sysreset, we will exit sandbox! */
state->sysreset_allowed[SYSRESET_POWER] = false;
ut_asserteq(-EACCES, sysreset_walk(SYSRESET_WARM));
ut_asserteq(-EACCES, sysreset_walk(SYSRESET_COLD));
ut_asserteq(-EACCES, sysreset_walk(SYSRESET_POWER));
/*
* Enable cold system reset - this should make cold system reset work,
* plus a warm system reset should be promoted to cold, since this is
* the next step along.
*/
state->sysreset_allowed[SYSRESET_COLD] = true;
ut_asserteq(-EINPROGRESS, sysreset_walk(SYSRESET_WARM));
ut_asserteq(-EINPROGRESS, sysreset_walk(SYSRESET_COLD));
ut_asserteq(-EACCES, sysreset_walk(SYSRESET_POWER));
state->sysreset_allowed[SYSRESET_COLD] = false;
state->sysreset_allowed[SYSRESET_POWER] = true;
return 0;
}
DM_TEST(dm_test_sysreset_walk, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

View File

@@ -0,0 +1,159 @@
/*
* Copyright (c) 2013 Google, Inc
*
* (C) Copyright 2012
* Pavel Herrmann <morpheus.ibis@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <malloc.h>
#include <dm/test.h>
#include <test/ut.h>
#include <asm/io.h>
int dm_testdrv_op_count[DM_TEST_OP_COUNT];
static struct unit_test_state *uts = &global_dm_test_state;
static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
{
const struct dm_test_pdata *pdata = dev_get_platdata(dev);
struct dm_test_priv *priv = dev_get_priv(dev);
*pingret = pingval + pdata->ping_add;
priv->ping_total += *pingret;
return 0;
}
static const struct test_ops test_ops = {
.ping = testdrv_ping,
};
static int test_bind(struct udevice *dev)
{
/* Private data should not be allocated */
ut_assert(!dev_get_priv(dev));
dm_testdrv_op_count[DM_TEST_OP_BIND]++;
return 0;
}
static int test_probe(struct udevice *dev)
{
struct dm_test_priv *priv = dev_get_priv(dev);
/* Private data should be allocated */
ut_assert(priv);
dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
priv->ping_total += DM_TEST_START_TOTAL;
return 0;
}
static int test_remove(struct udevice *dev)
{
/* Private data should still be allocated */
ut_assert(dev_get_priv(dev));
dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
return 0;
}
static int test_unbind(struct udevice *dev)
{
/* Private data should not be allocated */
ut_assert(!dev->priv);
dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
return 0;
}
U_BOOT_DRIVER(test_drv) = {
.name = "test_drv",
.id = UCLASS_TEST,
.ops = &test_ops,
.bind = test_bind,
.probe = test_probe,
.remove = test_remove,
.unbind = test_unbind,
.priv_auto_alloc_size = sizeof(struct dm_test_priv),
};
U_BOOT_DRIVER(test2_drv) = {
.name = "test2_drv",
.id = UCLASS_TEST,
.ops = &test_ops,
.bind = test_bind,
.probe = test_probe,
.remove = test_remove,
.unbind = test_unbind,
.priv_auto_alloc_size = sizeof(struct dm_test_priv),
};
static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
{
*pingret = pingval + 2;
return 0;
}
static const struct test_ops test_manual_ops = {
.ping = test_manual_drv_ping,
};
static int test_manual_bind(struct udevice *dev)
{
dm_testdrv_op_count[DM_TEST_OP_BIND]++;
return 0;
}
static int test_manual_probe(struct udevice *dev)
{
struct dm_test_state *dms = uts->priv;
dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
if (!dms->force_fail_alloc)
dev->priv = calloc(1, sizeof(struct dm_test_priv));
if (!dev->priv)
return -ENOMEM;
return 0;
}
static int test_manual_remove(struct udevice *dev)
{
dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
return 0;
}
static int test_manual_unbind(struct udevice *dev)
{
dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
return 0;
}
U_BOOT_DRIVER(test_manual_drv) = {
.name = "test_manual_drv",
.id = UCLASS_TEST,
.ops = &test_manual_ops,
.bind = test_manual_bind,
.probe = test_manual_probe,
.remove = test_manual_remove,
.unbind = test_manual_unbind,
};
U_BOOT_DRIVER(test_pre_reloc_drv) = {
.name = "test_pre_reloc_drv",
.id = UCLASS_TEST,
.ops = &test_manual_ops,
.bind = test_manual_bind,
.probe = test_manual_probe,
.remove = test_manual_remove,
.unbind = test_manual_unbind,
.flags = DM_FLAG_PRE_RELOC,
};

268
u-boot/test/dm/test-fdt.c Normal file
View File

@@ -0,0 +1,268 @@
/*
* Copyright (c) 2013 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <fdtdec.h>
#include <malloc.h>
#include <asm/io.h>
#include <dm/test.h>
#include <dm/root.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
{
const struct dm_test_pdata *pdata = dev->platdata;
struct dm_test_priv *priv = dev_get_priv(dev);
*pingret = pingval + pdata->ping_add;
priv->ping_total += *pingret;
return 0;
}
static const struct test_ops test_ops = {
.ping = testfdt_drv_ping,
};
static int testfdt_ofdata_to_platdata(struct udevice *dev)
{
struct dm_test_pdata *pdata = dev_get_platdata(dev);
pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
"ping-add", -1);
pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
"ping-expect");
return 0;
}
static int testfdt_drv_probe(struct udevice *dev)
{
struct dm_test_priv *priv = dev_get_priv(dev);
priv->ping_total += DM_TEST_START_TOTAL;
/*
* If this device is on a bus, the uclass_flag will be set before
* calling this function. This is used by
* dm_test_bus_child_pre_probe_uclass().
*/
priv->uclass_total += priv->uclass_flag;
return 0;
}
static const struct udevice_id testfdt_ids[] = {
{
.compatible = "denx,u-boot-fdt-test",
.data = DM_TEST_TYPE_FIRST },
{
.compatible = "google,another-fdt-test",
.data = DM_TEST_TYPE_SECOND },
{ }
};
U_BOOT_DRIVER(testfdt_drv) = {
.name = "testfdt_drv",
.of_match = testfdt_ids,
.id = UCLASS_TEST_FDT,
.ofdata_to_platdata = testfdt_ofdata_to_platdata,
.probe = testfdt_drv_probe,
.ops = &test_ops,
.priv_auto_alloc_size = sizeof(struct dm_test_priv),
.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
};
/* From here is the testfdt uclass code */
int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
{
const struct test_ops *ops = device_get_ops(dev);
if (!ops->ping)
return -ENOSYS;
return ops->ping(dev, pingval, pingret);
}
UCLASS_DRIVER(testfdt) = {
.name = "testfdt",
.id = UCLASS_TEST_FDT,
.flags = DM_UC_FLAG_SEQ_ALIAS,
};
int dm_check_devices(struct unit_test_state *uts, int num_devices)
{
struct udevice *dev;
int ret;
int i;
/*
* Now check that the ping adds are what we expect. This is using the
* ping-add property in each node.
*/
for (i = 0; i < num_devices; i++) {
uint32_t base;
ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
ut_assert(!ret);
/*
* Get the 'ping-expect' property, which tells us what the
* ping add should be. We don't use the platdata because we
* want to test the code that sets that up
* (testfdt_drv_probe()).
*/
base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
"ping-expect");
debug("dev=%d, base=%d: %s\n", i, base,
fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
ut_assert(!dm_check_operations(uts, dev, base,
dev_get_priv(dev)));
}
return 0;
}
/* Test that FDT-based binding works correctly */
static int dm_test_fdt(struct unit_test_state *uts)
{
const int num_devices = 6;
struct udevice *dev;
struct uclass *uc;
int ret;
int i;
ret = dm_scan_fdt(gd->fdt_blob, false);
ut_assert(!ret);
ret = uclass_get(UCLASS_TEST_FDT, &uc);
ut_assert(!ret);
/* These are num_devices compatible root-level device tree nodes */
ut_asserteq(num_devices, list_count_items(&uc->dev_head));
/* Each should have platform data but no private data */
for (i = 0; i < num_devices; i++) {
ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
ut_assert(!ret);
ut_assert(!dev_get_priv(dev));
ut_assert(dev->platdata);
}
ut_assertok(dm_check_devices(uts, num_devices));
return 0;
}
DM_TEST(dm_test_fdt, 0);
static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
{
struct uclass *uc;
int ret;
ret = dm_scan_fdt(gd->fdt_blob, true);
ut_assert(!ret);
ret = uclass_get(UCLASS_TEST_FDT, &uc);
ut_assert(!ret);
/* These is only one pre-reloc device */
ut_asserteq(1, list_count_items(&uc->dev_head));
return 0;
}
DM_TEST(dm_test_fdt_pre_reloc, 0);
/* Test that sequence numbers are allocated properly */
static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
{
struct udevice *dev;
/* A few basic santiy tests */
ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
ut_asserteq_str("b-test", dev->name);
ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
ut_asserteq_str("a-test", dev->name);
ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
true, &dev));
ut_asserteq_ptr(NULL, dev);
/* Test aliases */
ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
ut_asserteq_str("e-test", dev->name);
ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
true, &dev));
/*
* Note that c-test nodes are not probed since it is not a top-level
* node
*/
ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
ut_asserteq_str("b-test", dev->name);
/*
* d-test wants sequence number 3 also, but it can't have it because
* b-test gets it first.
*/
ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
ut_asserteq_str("d-test", dev->name);
/* d-test actually gets 0 */
ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
ut_asserteq_str("d-test", dev->name);
/* initially no one wants seq 1 */
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
&dev));
ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
/* But now that it is probed, we can find it */
ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
ut_asserteq_str("f-test", dev->name);
return 0;
}
DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test that we can find a device by device tree offset */
static int dm_test_fdt_offset(struct unit_test_state *uts)
{
const void *blob = gd->fdt_blob;
struct udevice *dev;
int node;
node = fdt_path_offset(blob, "/e-test");
ut_assert(node > 0);
ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
&dev));
ut_asserteq_str("e-test", dev->name);
/* This node should not be bound */
node = fdt_path_offset(blob, "/junk");
ut_assert(node > 0);
ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
node, &dev));
/* This is not a top level node so should not be probed */
node = fdt_path_offset(blob, "/some-bus/c-test@5");
ut_assert(node > 0);
ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
node, &dev));
return 0;
}
DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

155
u-boot/test/dm/test-main.c Normal file
View File

@@ -0,0 +1,155 @@
/*
* Copyright (c) 2013 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <command.h>
#include <console.h>
#include <dm.h>
#include <errno.h>
#include <malloc.h>
#include <asm/state.h>
#include <dm/test.h>
#include <dm/root.h>
#include <dm/uclass-internal.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
struct unit_test_state global_dm_test_state;
static struct dm_test_state _global_priv_dm_test_state;
/* Get ready for testing */
static int dm_test_init(struct unit_test_state *uts)
{
struct dm_test_state *dms = uts->priv;
memset(dms, '\0', sizeof(*dms));
gd->dm_root = NULL;
memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
ut_assertok(dm_init());
dms->root = dm_root();
return 0;
}
/* Ensure all the test devices are probed */
static int do_autoprobe(struct unit_test_state *uts)
{
struct udevice *dev;
int ret;
/* Scanning the uclass is enough to probe all the devices */
for (ret = uclass_first_device(UCLASS_TEST, &dev);
dev;
ret = uclass_next_device(&dev))
;
return ret;
}
static int dm_test_destroy(struct unit_test_state *uts)
{
int id;
for (id = 0; id < UCLASS_COUNT; id++) {
struct uclass *uc;
/*
* If the uclass doesn't exist we don't want to create it. So
* check that here before we call uclass_find_device()/
*/
uc = uclass_find(id);
if (!uc)
continue;
ut_assertok(uclass_destroy(uc));
}
return 0;
}
static int dm_test_main(const char *test_name)
{
struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
const int n_ents = ll_entry_count(struct unit_test, dm_test);
struct unit_test_state *uts = &global_dm_test_state;
struct sandbox_state *state = state_get_current();
uts->priv = &_global_priv_dm_test_state;
struct unit_test *test;
int run_count;
uts->fail_count = 0;
/*
* If we have no device tree, or it only has a root node, then these
* tests clearly aren't going to work...
*/
if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
puts("Please run with test device tree:\n"
" ./u-boot -d arch/sandbox/dts/test.dtb\n");
ut_assert(gd->fdt_blob);
}
if (!test_name)
printf("Running %d driver model tests\n", n_ents);
run_count = 0;
for (test = tests; test < tests + n_ents; test++) {
const char *name = test->name;
/* All tests have this prefix */
if (!strncmp(name, "dm_test_", 8))
name += 8;
if (test_name && strcmp(test_name, name))
continue;
printf("Test: %s\n", test->name);
run_count++;
ut_assertok(dm_test_init(uts));
uts->start = mallinfo();
if (test->flags & DM_TESTF_SCAN_PDATA)
ut_assertok(dm_scan_platdata(false));
if (test->flags & DM_TESTF_PROBE_TEST)
ut_assertok(do_autoprobe(uts));
if (test->flags & DM_TESTF_SCAN_FDT)
ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
/*
* Silence the console and rely on console reocrding to get
* our output.
*/
console_record_reset();
if (!state->show_test_output)
gd->flags |= GD_FLG_SILENT;
test->func(uts);
gd->flags &= ~GD_FLG_SILENT;
state_set_skip_delays(false);
ut_assertok(dm_test_destroy(uts));
}
if (test_name && !run_count)
printf("Test '%s' not found\n", test_name);
else
printf("Failures: %d\n", uts->fail_count);
gd->dm_root = NULL;
ut_assertok(dm_init());
dm_scan_platdata(false);
dm_scan_fdt(gd->fdt_blob, false);
return uts->fail_count ? CMD_RET_FAILURE : 0;
}
int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
const char *test_name = NULL;
if (argc > 1)
test_name = argv[1];
return dm_test_main(test_name);
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (c) 2013 Google, Inc
*
* (C) Copyright 2012
* Pavel Herrmann <morpheus.ibis@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <malloc.h>
#include <dm.h>
#include <errno.h>
#include <asm/io.h>
#include <dm/test.h>
#include <linux/list.h>
#include <test/ut.h>
static struct unit_test_state *uts = &global_dm_test_state;
int test_ping(struct udevice *dev, int pingval, int *pingret)
{
const struct test_ops *ops = device_get_ops(dev);
if (!ops->ping)
return -ENOSYS;
return ops->ping(dev, pingval, pingret);
}
static int test_post_bind(struct udevice *dev)
{
struct dm_test_perdev_uc_pdata *uc_pdata;
dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
ut_assert(!device_active(dev));
uc_pdata = dev_get_uclass_platdata(dev);
ut_assert(uc_pdata);
uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1;
uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2;
uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3;
return 0;
}
static int test_pre_unbind(struct udevice *dev)
{
dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
return 0;
}
static int test_pre_probe(struct udevice *dev)
{
struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
ut_assert(priv);
ut_assert(device_active(dev));
return 0;
}
static int test_post_probe(struct udevice *dev)
{
struct udevice *prev = list_entry(dev->uclass_node.prev,
struct udevice, uclass_node);
struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
struct uclass *uc = dev->uclass;
struct dm_test_state *dms = uts->priv;
dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++;
ut_assert(priv);
ut_assert(device_active(dev));
priv->base_add = 0;
if (dms->skip_post_probe)
return 0;
if (&prev->uclass_node != &uc->dev_head) {
struct dm_test_uclass_perdev_priv *prev_uc_priv
= dev_get_uclass_priv(prev);
struct dm_test_pdata *pdata = prev->platdata;
ut_assert(pdata);
ut_assert(prev_uc_priv);
priv->base_add = prev_uc_priv->base_add + pdata->ping_add;
}
return 0;
}
static int test_pre_remove(struct udevice *dev)
{
dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++;
return 0;
}
static int test_init(struct uclass *uc)
{
dm_testdrv_op_count[DM_TEST_OP_INIT]++;
ut_assert(uc->priv);
return 0;
}
static int test_destroy(struct uclass *uc)
{
dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
return 0;
}
UCLASS_DRIVER(test) = {
.name = "test",
.id = UCLASS_TEST,
.post_bind = test_post_bind,
.pre_unbind = test_pre_unbind,
.pre_probe = test_pre_probe,
.post_probe = test_post_probe,
.pre_remove = test_pre_remove,
.init = test_init,
.destroy = test_destroy,
.priv_auto_alloc_size = sizeof(struct dm_test_uclass_priv),
.per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv),
.per_device_platdata_auto_alloc_size =
sizeof(struct dm_test_perdev_uc_pdata),
};

27
u-boot/test/dm/timer.c Normal file
View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <timer.h>
#include <dm/test.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/*
* Basic test of the timer uclass.
*/
static int dm_test_timer_base(struct unit_test_state *uts)
{
struct udevice *dev;
ut_assertok(uclass_get_device(UCLASS_TIMER, 0, &dev));
ut_asserteq(1000000, timer_get_rate(dev));
return 0;
}
DM_TEST(dm_test_timer_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

302
u-boot/test/dm/usb.c Normal file
View File

@@ -0,0 +1,302 @@
/*
* Copyright (C) 2015 Google, Inc
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <console.h>
#include <dm.h>
#include <usb.h>
#include <asm/io.h>
#include <asm/state.h>
#include <asm/test.h>
#include <dm/device-internal.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <test/ut.h>
DECLARE_GLOBAL_DATA_PTR;
/* Test that sandbox USB works correctly */
static int dm_test_usb_base(struct unit_test_state *uts)
{
struct udevice *bus;
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 0, &bus));
ut_assertok(uclass_get_device(UCLASS_USB, 0, &bus));
ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_USB, 2, &bus));
return 0;
}
DM_TEST(dm_test_usb_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/*
* Test that we can use the flash stick. This is more of a functional test. It
* covers scanning the bug, setting up a hub and a flash stick and reading
* data from the flash stick.
*/
static int dm_test_usb_flash(struct unit_test_state *uts)
{
struct udevice *dev;
struct blk_desc *dev_desc;
char cmp[1024];
state_set_skip_delays(true);
ut_assertok(usb_init());
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
/* Read a few blocks and look for the string we expect */
ut_asserteq(512, dev_desc->blksz);
memset(cmp, '\0', sizeof(cmp));
ut_asserteq(2, blk_dread(dev_desc, 0, 2, cmp));
ut_assertok(strcmp(cmp, "this is a test"));
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* test that we can handle multiple storage devices */
static int dm_test_usb_multi(struct unit_test_state *uts)
{
struct udevice *dev;
state_set_skip_delays(true);
ut_assertok(usb_init());
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_multi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int count_usb_devices(void)
{
struct udevice *hub;
struct uclass *uc;
int count = 0;
int ret;
ret = uclass_get(UCLASS_USB_HUB, &uc);
if (ret)
return ret;
uclass_foreach_dev(hub, uc) {
struct udevice *dev;
count++;
for (device_find_first_child(hub, &dev);
dev;
device_find_next_child(&dev)) {
count++;
}
}
return count;
}
/* test that we can remove an emulated device and it is then not found */
static int dm_test_usb_remove(struct unit_test_state *uts)
{
struct udevice *dev, *emul;
/* Scan and check that all devices are present */
state_set_skip_delays(true);
ut_assertok(usb_init());
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
ut_asserteq(6, count_usb_devices());
ut_assertok(usb_stop());
ut_asserteq(6, count_usb_devices());
/* Remove the second emulation device */
ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
&dev));
ut_assertok(device_unbind(dev));
/* Rescan - only the first and third should be present */
ut_assertok(usb_init());
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &dev));
ut_assertok(usb_emul_find_for_dev(dev, &emul));
ut_asserteq_str("flash-stick@0", emul->name);
ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 1, &dev));
ut_assertok(usb_emul_find_for_dev(dev, &emul));
ut_asserteq_str("flash-stick@2", emul->name);
ut_asserteq(-ENODEV, uclass_get_device(UCLASS_MASS_STORAGE, 2, &dev));
ut_asserteq(5, count_usb_devices());
ut_assertok(usb_stop());
ut_asserteq(5, count_usb_devices());
return 0;
}
DM_TEST(dm_test_usb_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
const char usb_tree_base[] =
" 1 Hub (12 Mb/s, 100mA)\n"
" | sandbox hub 2345\n"
" |\n"
" |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@0\n"
" | \n"
" |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@1\n"
" | \n"
" |\b+-4 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@2\n"
" | \n"
" |\b+-5 Human Interface (12 Mb/s, 100mA)\n"
" sandbox keyboard keyb@3\n"
" \n";
/* test that the 'usb tree' command output looks correct */
static int dm_test_usb_tree(struct unit_test_state *uts)
{
char *data;
int len;
state_set_skip_delays(true);
ut_assertok(usb_init());
console_record_reset_enable();
usb_show_tree();
len = membuff_getraw(&gd->console_out, -1, true, &data);
if (len)
data[len] = '\0';
ut_asserteq_str(usb_tree_base, data);
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_tree, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
const char usb_tree_remove[] =
" 1 Hub (12 Mb/s, 100mA)\n"
" | sandbox hub 2345\n"
" |\n"
" |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@0\n"
" | \n"
" |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@2\n"
" | \n"
" |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
" sandbox keyboard keyb@3\n"
" \n";
/*
* test that the 'usb tree' command output looks correct when we remove a
* device
*/
static int dm_test_usb_tree_remove(struct unit_test_state *uts)
{
struct udevice *dev;
char *data;
int len;
/* Remove the second emulation device */
ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
&dev));
ut_assertok(device_unbind(dev));
state_set_skip_delays(true);
ut_assertok(usb_init());
console_record_reset_enable();
usb_show_tree();
len = membuff_getraw(&gd->console_out, -1, true, &data);
if (len)
data[len] = '\0';
ut_asserteq_str(usb_tree_remove, data);
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_tree_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
const char usb_tree_reorder[] =
" 1 Hub (12 Mb/s, 100mA)\n"
" | sandbox hub 2345\n"
" |\n"
" |\b+-2 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@0\n"
" | \n"
" |\b+-3 Mass Storage (12 Mb/s, 100mA)\n"
" | sandbox flash flash-stick@2\n"
" | \n"
" |\b+-4 Human Interface (12 Mb/s, 100mA)\n"
" | sandbox keyboard keyb@3\n"
" | \n"
" |\b+-5 Mass Storage (12 Mb/s, 100mA)\n"
" sandbox flash flash-stick@1\n"
" \n";
/*
* test that the 'usb tree' command output looks correct when we reorder two
* devices.
*/
static int dm_test_usb_tree_reorder(struct unit_test_state *uts)
{
struct udevice *dev, *parent;
char *data;
int len;
/* Remove the second emulation device */
ut_assertok(uclass_find_device_by_name(UCLASS_USB_EMUL, "flash-stick@1",
&dev));
parent = dev->parent;
/* Reorder the devices in the parent list and uclass list */
list_del(&dev->sibling_node);
list_add_tail(&dev->sibling_node, &parent->child_head);
list_del(&dev->uclass_node);
list_add_tail(&dev->uclass_node, &dev->uclass->dev_head);
state_set_skip_delays(true);
ut_assertok(usb_init());
console_record_reset_enable();
usb_show_tree();
len = membuff_getraw(&gd->console_out, -1, true, &data);
if (len)
data[len] = '\0';
ut_asserteq_str(usb_tree_reorder, data);
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_tree_reorder, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
static int dm_test_usb_keyb(struct unit_test_state *uts)
{
struct udevice *dev;
state_set_skip_delays(true);
ut_assertok(usb_init());
/* Initially there should be no characters */
ut_asserteq(0, tstc());
ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb",
&dev));
/*
* Add a string to the USB keyboard buffer - it should appear in
* stdin
*/
ut_assertok(sandbox_usb_keyb_add_string(dev, "ab"));
ut_asserteq(1, tstc());
ut_asserteq('a', getc());
ut_asserteq(1, tstc());
ut_asserteq('b', getc());
ut_asserteq(0, tstc());
ut_assertok(usb_stop());
return 0;
}
DM_TEST(dm_test_usb_keyb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

353
u-boot/test/dm/video.c Normal file
View File

@@ -0,0 +1,353 @@
/*
* Copyright (c) 2014 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <bzlib.h>
#include <dm.h>
#include <mapmem.h>
#include <os.h>
#include <video.h>
#include <video_console.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
#include <test/ut.h>
/*
* These tests use the standard sandbox frame buffer, the resolution of which
* is defined in the device tree. This only supports 16bpp so the tests only
* test that code path. It would be possible to adjust this fairly easily,
* by adjusting the bpix value in struct sandbox_sdl_plat. However the code
* in sandbox_sdl_sync() would also need to change to handle the different
* surface depth.
*/
DECLARE_GLOBAL_DATA_PTR;
/* Basic test of the video uclass */
static int dm_test_video_base(struct unit_test_state *uts)
{
struct video_priv *priv;
struct udevice *dev;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_asserteq(1366, video_get_xsize(dev));
ut_asserteq(768, video_get_ysize(dev));
priv = dev_get_uclass_priv(dev);
ut_asserteq(priv->fb_size, 1366 * 768 * 2);
return 0;
}
DM_TEST(dm_test_video_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/**
* compress_frame_buffer() - Compress the frame buffer and return its size
*
* We want to write tests which perform operations on the video console and
* check that the frame buffer ends up with the correct contents. But it is
* painful to store 'known good' images for comparison with the frame
* buffer. As an alternative, we can compress the frame buffer and check the
* size of the compressed data. This provides a pretty good level of
* certainty and the resulting tests need only check a single value.
*
* @dev: Video device
* @return compressed size of the frame buffer, or -ve on error
*/
static int compress_frame_buffer(struct udevice *dev)
{
struct video_priv *priv = dev_get_uclass_priv(dev);
uint destlen;
void *dest;
int ret;
destlen = priv->fb_size;
dest = malloc(priv->fb_size);
if (!dest)
return -ENOMEM;
ret = BZ2_bzBuffToBuffCompress(dest, &destlen,
priv->fb, priv->fb_size,
3, 0, 0);
free(dest);
if (ret)
return ret;
return destlen;
}
/*
* Call this function at any point to halt and show the current display. Be
* sure to run the test with the -l flag.
*/
static void __maybe_unused see_output(void)
{
video_sync_all();
while (1);
}
/* Select the video console driver to use for a video device */
static int select_vidconsole(struct unit_test_state *uts, const char *drv_name)
{
struct sandbox_sdl_plat *plat;
struct udevice *dev;
ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
ut_assert(!device_active(dev));
plat = dev_get_platdata(dev);
plat->vidconsole_drv_name = "vidconsole0";
return 0;
}
/* Test text output works on the video console */
static int dm_test_video_text(struct unit_test_state *uts)
{
struct udevice *dev, *con;
int i;
#define WHITE 0xffff
#define SCROLL_LINES 100
ut_assertok(select_vidconsole(uts, "vidconsole0"));
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_asserteq(46, compress_frame_buffer(dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
vidconsole_putc_xy(con, 0, 0, 'a');
ut_asserteq(79, compress_frame_buffer(dev));
vidconsole_putc_xy(con, 0, 0, ' ');
ut_asserteq(46, compress_frame_buffer(dev));
for (i = 0; i < 20; i++)
vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
ut_asserteq(273, compress_frame_buffer(dev));
vidconsole_set_row(con, 0, WHITE);
ut_asserteq(46, compress_frame_buffer(dev));
for (i = 0; i < 20; i++)
vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
ut_asserteq(273, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_text, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test handling of special characters in the console */
static int dm_test_video_chars(struct unit_test_state *uts)
{
struct udevice *dev, *con;
const char *test_string = "Well\b\b\b\bxhe is\r \n\ta very \amodest \bman\n\t\tand Has much to\b\bto be modest about.";
const char *s;
ut_assertok(select_vidconsole(uts, "vidconsole0"));
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
for (s = test_string; *s; s++)
vidconsole_put_char(con, *s);
ut_asserteq(466, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_chars, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/**
* check_vidconsole_output() - Run a text console test
*
* @uts: Test state
* @rot: Console rotation (0, 90, 180, 270)
* @wrap_size: Expected size of compressed frame buffer for the wrap test
* @scroll_size: Same for the scroll test
* @return 0 on success
*/
static int check_vidconsole_output(struct unit_test_state *uts, int rot,
int wrap_size, int scroll_size)
{
struct udevice *dev, *con;
struct sandbox_sdl_plat *plat;
int i;
ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
ut_assert(!device_active(dev));
plat = dev_get_platdata(dev);
plat->rot = rot;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
ut_asserteq(46, compress_frame_buffer(dev));
/* Check display wrap */
for (i = 0; i < 120; i++)
vidconsole_put_char(con, 'A' + i % 50);
ut_asserteq(wrap_size, compress_frame_buffer(dev));
/* Check display scrolling */
for (i = 0; i < SCROLL_LINES; i++) {
vidconsole_put_char(con, 'A' + i % 50);
vidconsole_put_char(con, '\n');
}
ut_asserteq(scroll_size, compress_frame_buffer(dev));
/* If we scroll enough, the screen becomes blank again */
for (i = 0; i < SCROLL_LINES; i++)
vidconsole_put_char(con, '\n');
ut_asserteq(46, compress_frame_buffer(dev));
return 0;
}
/* Test text output through the console uclass */
static int dm_test_video_context(struct unit_test_state *uts)
{
ut_assertok(select_vidconsole(uts, "vidconsole0"));
ut_assertok(check_vidconsole_output(uts, 0, 788, 453));
return 0;
}
DM_TEST(dm_test_video_context, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test rotated text output through the console uclass */
static int dm_test_video_rotation1(struct unit_test_state *uts)
{
ut_assertok(check_vidconsole_output(uts, 1, 1112, 680));
return 0;
}
DM_TEST(dm_test_video_rotation1, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test rotated text output through the console uclass */
static int dm_test_video_rotation2(struct unit_test_state *uts)
{
ut_assertok(check_vidconsole_output(uts, 2, 785, 446));
return 0;
}
DM_TEST(dm_test_video_rotation2, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test rotated text output through the console uclass */
static int dm_test_video_rotation3(struct unit_test_state *uts)
{
ut_assertok(check_vidconsole_output(uts, 3, 1134, 681));
return 0;
}
DM_TEST(dm_test_video_rotation3, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Read a file into memory and return a pointer to it */
static int read_file(struct unit_test_state *uts, const char *fname,
ulong *addrp)
{
int buf_size = 100000;
ulong addr = 0;
int size, fd;
char *buf;
buf = map_sysmem(addr, 0);
ut_assert(buf != NULL);
fd = os_open(fname, OS_O_RDONLY);
ut_assert(fd >= 0);
size = os_read(fd, buf, buf_size);
os_close(fd);
ut_assert(size >= 0);
ut_assert(size < buf_size);
*addrp = addr;
return 0;
}
/* Test drawing a bitmap file */
static int dm_test_video_bmp(struct unit_test_state *uts)
{
struct udevice *dev;
ulong addr;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(read_file(uts, "tools/logos/denx.bmp", &addr));
ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
ut_asserteq(1368, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_bmp, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test drawing a compressed bitmap file */
static int dm_test_video_bmp_comp(struct unit_test_state *uts)
{
struct udevice *dev;
ulong addr;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(read_file(uts, "tools/logos/denx-comp.bmp", &addr));
ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
ut_asserteq(1368, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_bmp_comp, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test TrueType console */
static int dm_test_video_truetype(struct unit_test_state *uts)
{
struct udevice *dev, *con;
const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
const char *s;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
for (s = test_string; *s; s++)
vidconsole_put_char(con, *s);
ut_asserteq(12619, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_truetype, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test scrolling TrueType console */
static int dm_test_video_truetype_scroll(struct unit_test_state *uts)
{
struct sandbox_sdl_plat *plat;
struct udevice *dev, *con;
const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
const char *s;
ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
ut_assert(!device_active(dev));
plat = dev_get_platdata(dev);
plat->font_size = 100;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
for (s = test_string; *s; s++)
vidconsole_put_char(con, *s);
ut_asserteq(33849, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_truetype_scroll, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
/* Test TrueType backspace, within and across lines */
static int dm_test_video_truetype_bs(struct unit_test_state *uts)
{
struct sandbox_sdl_plat *plat;
struct udevice *dev, *con;
const char *test_string = "...Criticism may or may\b\b\b\b\b\bnot be agreeable, but seldom it is necessary\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bit is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things.";
const char *s;
ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
ut_assert(!device_active(dev));
plat = dev_get_platdata(dev);
plat->font_size = 100;
ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
for (s = test_string; *s; s++)
vidconsole_put_char(con, *s);
ut_asserteq(34871, compress_frame_buffer(dev));
return 0;
}
DM_TEST(dm_test_video_truetype_bs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);