2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-13 14:22:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <part.h>
|
2022-10-30 01:47:17 +00:00
|
|
|
#include <sandbox_host.h>
|
2016-03-13 14:22:36 +00:00
|
|
|
#include <usb.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-03-13 14:22:36 +00:00
|
|
|
#include <asm/state.h>
|
|
|
|
#include <dm/test.h>
|
2020-07-19 16:15:37 +00:00
|
|
|
#include <test/test.h>
|
2016-03-13 14:22:36 +00:00
|
|
|
#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)
|
|
|
|
{
|
2022-10-30 01:47:17 +00:00
|
|
|
struct udevice *blk0, *blk1, *dev0, *dev1, *dev, *chk0, *chk1;
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
/* Create two, one the parent of the other */
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_assertok(host_create_device("test0", false, &dev0));
|
|
|
|
ut_assertok(host_create_device("test1", false, &dev1));
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
/* Check we can find them */
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_assertok(blk_get_device(UCLASS_HOST, 0, &blk0));
|
|
|
|
ut_assertok(blk_get_from_parent(dev0, &chk0));
|
|
|
|
ut_asserteq_ptr(blk0, chk0);
|
|
|
|
|
|
|
|
ut_assertok(blk_get_device(UCLASS_HOST, 1, &blk1));
|
|
|
|
ut_assertok(blk_get_from_parent(dev1, &chk1));
|
|
|
|
ut_asserteq_ptr(blk1, chk1);
|
|
|
|
ut_asserteq(-ENODEV, blk_get_device(UCLASS_HOST, 2, &dev0));
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
/* Check we can iterate */
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_assertok(blk_first_device(UCLASS_HOST, &dev));
|
|
|
|
ut_asserteq_ptr(blk0, dev);
|
2018-10-15 09:21:04 +00:00
|
|
|
ut_assertok(blk_next_device(&dev));
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_asserteq_ptr(blk1, dev);
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-04-25 05:31:02 +00:00
|
|
|
usb_started = false;
|
|
|
|
|
2016-03-13 14:22:36 +00:00
|
|
|
/* Get a flash device */
|
|
|
|
state_set_skip_delays(true);
|
2022-04-25 05:31:02 +00:00
|
|
|
ut_assertok(usb_stop());
|
2016-03-13 14:22:36 +00:00
|
|
|
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 */
|
2022-08-12 01:34:59 +00:00
|
|
|
ut_assertok(blk_get_device(UCLASS_USB, 0, &dev));
|
2016-03-13 14:22:36 +00:00
|
|
|
ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
|
|
|
|
|
|
|
|
/* Check we have one block device for each mass storage device */
|
2017-04-24 02:02:07 +00:00
|
|
|
ut_asserteq(6, count_blk_devices());
|
2016-03-13 14:22:36 +00:00
|
|
|
|
|
|
|
/* Now go around again, making sure the old devices were unbound */
|
|
|
|
ut_assertok(usb_stop());
|
|
|
|
ut_assertok(usb_init());
|
2017-04-24 02:02:07 +00:00
|
|
|
ut_asserteq(6, count_blk_devices());
|
2016-03-13 14:22:36 +00:00
|
|
|
ut_assertok(usb_stop());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-04-24 02:02:05 +00:00
|
|
|
|
|
|
|
/* Test that we can find block devices without probing them */
|
|
|
|
static int dm_test_blk_find(struct unit_test_state *uts)
|
|
|
|
{
|
2022-10-30 01:47:17 +00:00
|
|
|
struct udevice *blk, *chk, *dev;
|
|
|
|
|
|
|
|
ut_assertok(host_create_device("test0", false, &dev));
|
2017-04-24 02:02:05 +00:00
|
|
|
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_assertok(blk_find_device(UCLASS_HOST, 0, &chk));
|
|
|
|
ut_assertok(device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk));
|
|
|
|
ut_asserteq_ptr(chk, blk);
|
2017-04-24 02:02:05 +00:00
|
|
|
ut_asserteq(false, device_active(dev));
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_asserteq(-ENODEV, blk_find_device(UCLASS_HOST, 1, &dev));
|
2017-04-24 02:02:05 +00:00
|
|
|
|
|
|
|
/* Now activate it */
|
2022-10-30 01:47:17 +00:00
|
|
|
ut_assertok(blk_get_device(UCLASS_HOST, 0, &blk));
|
|
|
|
ut_asserteq_ptr(chk, blk);
|
|
|
|
ut_asserteq(true, device_active(blk));
|
2017-04-24 02:02:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-04-24 02:02:07 +00:00
|
|
|
|
|
|
|
/* Test that block device numbering works as expected */
|
|
|
|
static int dm_test_blk_devnum(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev, *mmc_dev, *parent;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Probe the devices, with the first one being probed last. This is the
|
2022-12-02 06:23:07 +00:00
|
|
|
* one with no alias / sequence number.
|
2017-04-24 02:02:07 +00:00
|
|
|
*/
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
struct blk_desc *desc;
|
|
|
|
|
|
|
|
/* Check that the bblock device is attached */
|
|
|
|
ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
|
2022-08-12 01:34:59 +00:00
|
|
|
ut_assertok(blk_find_device(UCLASS_MMC, i, &dev));
|
2017-04-24 02:02:07 +00:00
|
|
|
parent = dev_get_parent(dev);
|
|
|
|
ut_asserteq_ptr(parent, mmc_dev);
|
|
|
|
ut_asserteq(trailing_strtol(mmc_dev->name), i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the block device devnum matches its parent's
|
|
|
|
* sequence number
|
|
|
|
*/
|
2020-12-03 23:55:18 +00:00
|
|
|
desc = dev_get_uclass_plat(dev);
|
2017-04-24 02:02:07 +00:00
|
|
|
ut_asserteq(desc->devnum, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-05-27 17:37:17 +00:00
|
|
|
|
|
|
|
/* Test that we can get a block from its parent */
|
|
|
|
static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev, *blk;
|
|
|
|
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
|
|
|
|
ut_assertok(blk_get_from_parent(dev, &blk));
|
|
|
|
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
|
2022-10-30 01:47:14 +00:00
|
|
|
ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
|
2017-05-27 17:37:17 +00:00
|
|
|
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
|
|
|
|
ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2021-07-05 22:32:59 +00:00
|
|
|
|
|
|
|
/* Test iteration through block devices */
|
|
|
|
static int dm_test_blk_iter(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See sandbox test.dts - it has:
|
|
|
|
*
|
|
|
|
* mmc0 - removable
|
|
|
|
* mmc1 - removable
|
|
|
|
* mmc2 - fixed
|
|
|
|
*/
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_FIXED, &dev));
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
|
|
|
|
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
ut_asserteq_str("mmc1.blk", dev->name);
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
ut_asserteq_str("mmc0.blk", dev->name);
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_asserteq_str("mmc1.blk", dev->name);
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_asserteq_str("mmc0.blk", dev->name);
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
|
|
|
|
|
|
|
|
ut_asserteq(1, blk_count_devices(BLKF_FIXED));
|
|
|
|
ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE));
|
|
|
|
ut_asserteq(3, blk_count_devices(BLKF_BOTH));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
blk_foreach_probe(BLKF_FIXED, dev)
|
|
|
|
ut_asserteq_str((i++, "mmc2.blk"), dev->name);
|
|
|
|
ut_asserteq(1, i);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
blk_foreach_probe(BLKF_REMOVABLE, dev)
|
|
|
|
ut_asserteq_str(i++ ? "mmc0.blk" : "mmc1.blk", dev->name);
|
|
|
|
ut_asserteq(2, i);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
blk_foreach_probe(BLKF_BOTH, dev)
|
|
|
|
ut_asserteq_str((++i == 1 ? "mmc2.blk" : i == 2 ?
|
|
|
|
"mmc1.blk" : "mmc0.blk"), dev->name);
|
|
|
|
ut_asserteq(3, i);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DM_TEST(dm_test_blk_iter, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2022-02-28 19:08:35 +00:00
|
|
|
|
|
|
|
/* Test finding fixed/removable block devices */
|
|
|
|
static int dm_test_blk_flags(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
/* Iterate through devices without probing them */
|
|
|
|
ut_assertok(blk_find_first(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
|
|
|
|
ut_assertok(blk_find_next(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc1.blk", dev->name);
|
|
|
|
|
|
|
|
ut_assertok(blk_find_next(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc0.blk", dev->name);
|
|
|
|
|
|
|
|
ut_asserteq(-ENODEV, blk_find_next(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnull(dev);
|
|
|
|
|
|
|
|
/* All devices are removable until probed */
|
|
|
|
ut_asserteq(-ENODEV, blk_find_first(BLKF_FIXED, &dev));
|
|
|
|
|
|
|
|
ut_assertok(blk_find_first(BLKF_REMOVABLE, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
|
|
|
|
/* Now probe them and iterate again */
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc1.blk", dev->name);
|
|
|
|
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc0.blk", dev->name);
|
|
|
|
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_BOTH, &dev));
|
|
|
|
|
|
|
|
/* Look only for fixed devices */
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_FIXED, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc2.blk", dev->name);
|
|
|
|
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
|
|
|
|
|
|
|
|
/* Look only for removable devices */
|
|
|
|
ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc1.blk", dev->name);
|
|
|
|
|
|
|
|
ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
ut_assertnonnull(dev);
|
|
|
|
ut_asserteq_str("mmc0.blk", dev->name);
|
|
|
|
|
|
|
|
ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DM_TEST(dm_test_blk_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
|
|
|
|
|
|
|
/* Test blk_foreach() and friend */
|
|
|
|
static int dm_test_blk_foreach(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int found;
|
|
|
|
|
|
|
|
/* Test blk_foreach() - use the 3rd bytes of the name (0/1/2) */
|
|
|
|
found = 0;
|
|
|
|
blk_foreach(BLKF_BOTH, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(7, found);
|
|
|
|
|
|
|
|
/* All devices are removable until probed */
|
|
|
|
found = 0;
|
|
|
|
blk_foreach(BLKF_FIXED, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(0, found);
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
blk_foreach(BLKF_REMOVABLE, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(7, found);
|
|
|
|
|
|
|
|
/* Now try again with the probing functions */
|
|
|
|
found = 0;
|
|
|
|
blk_foreach_probe(BLKF_BOTH, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(7, found);
|
|
|
|
ut_asserteq(3, blk_count_devices(BLKF_BOTH));
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
blk_foreach_probe(BLKF_FIXED, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(4, found);
|
|
|
|
ut_asserteq(1, blk_count_devices(BLKF_FIXED));
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
blk_foreach_probe(BLKF_REMOVABLE, dev)
|
|
|
|
found |= 1 << dectoul(&dev->name[3], NULL);
|
|
|
|
ut_asserteq(3, found);
|
|
|
|
ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DM_TEST(dm_test_blk_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|