2 * Copyright (C) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/state.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Test that block devices can be created */
17 static int dm_test_blk_base(struct unit_test_state *uts)
19 struct udevice *blk, *usb_blk, *dev;
21 /* Make sure there are no block devices */
22 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk));
24 /* Create two, one the parent of the other */
25 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
26 IF_TYPE_HOST, 1, 512, 1024, &blk));
27 ut_assertok(blk_create_device(blk, "usb_storage_blk", "test",
28 IF_TYPE_USB, 3, 512, 1024, &usb_blk));
30 /* Check we can find them */
31 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
32 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
33 ut_asserteq_ptr(blk, dev);
35 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_USB, 0, &dev));
36 ut_assertok(blk_get_device(IF_TYPE_USB, 3, &dev));
37 ut_asserteq_ptr(usb_blk, dev);
39 /* Check we can iterate */
40 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
41 ut_asserteq_ptr(blk, dev);
42 ut_asserteq(-ENODEV, blk_next_device(&dev));
44 ut_assertok(blk_first_device(IF_TYPE_USB, &dev));
45 ut_asserteq_ptr(usb_blk, dev);
46 ut_asserteq(-ENODEV, blk_next_device(&dev));
50 DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
52 static int count_blk_devices(void)
59 ret = uclass_get(UCLASS_BLK, &uc);
63 uclass_foreach_dev(blk, uc)
69 /* Test that block devices work correctly with USB */
70 static int dm_test_blk_usb(struct unit_test_state *uts)
72 struct udevice *usb_dev, *dev;
73 struct blk_desc *dev_desc;
75 /* Get a flash device */
76 state_set_skip_delays(true);
77 ut_assertok(usb_init());
78 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
79 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
81 /* The parent should be a block device */
82 ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
83 ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
85 /* Check we have one block device for each mass storage device */
86 ut_asserteq(4, count_blk_devices());
88 /* Now go around again, making sure the old devices were unbound */
89 ut_assertok(usb_stop());
90 ut_assertok(usb_init());
91 ut_asserteq(4, count_blk_devices());
92 ut_assertok(usb_stop());
96 DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);