1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
10 #include <asm/state.h>
12 #include <test/test.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 /* Test that block devices can be created */
18 static int dm_test_blk_base(struct unit_test_state *uts)
20 struct udevice *blk1, *blk3, *dev;
22 /* Make sure there are no block devices */
23 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
25 /* Create two, one the parent of the other */
26 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
27 IF_TYPE_HOST, 1, 512, 2, &blk1));
28 ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
29 IF_TYPE_HOST, 3, 512, 2, &blk3));
31 /* Check we can find them */
32 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
33 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
34 ut_asserteq_ptr(blk1, dev);
35 ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
36 ut_asserteq_ptr(blk3, dev);
38 /* Check we can iterate */
39 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
40 ut_asserteq_ptr(blk1, dev);
41 ut_assertok(blk_next_device(&dev));
42 ut_asserteq_ptr(blk3, dev);
46 DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
48 static int count_blk_devices(void)
55 ret = uclass_get(UCLASS_BLK, &uc);
59 uclass_foreach_dev(blk, uc)
65 /* Test that block devices work correctly with USB */
66 static int dm_test_blk_usb(struct unit_test_state *uts)
68 struct udevice *usb_dev, *dev;
69 struct blk_desc *dev_desc;
71 /* Get a flash device */
72 state_set_skip_delays(true);
73 ut_assertok(usb_init());
74 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
75 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
77 /* The parent should be a block device */
78 ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
79 ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
81 /* Check we have one block device for each mass storage device */
82 ut_asserteq(6, count_blk_devices());
84 /* Now go around again, making sure the old devices were unbound */
85 ut_assertok(usb_stop());
86 ut_assertok(usb_init());
87 ut_asserteq(6, count_blk_devices());
88 ut_assertok(usb_stop());
92 DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
94 /* Test that we can find block devices without probing them */
95 static int dm_test_blk_find(struct unit_test_state *uts)
97 struct udevice *blk, *dev;
99 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
100 IF_TYPE_HOST, 1, 512, 2, &blk));
101 ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
102 ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
103 ut_asserteq_ptr(blk, dev);
104 ut_asserteq(false, device_active(dev));
106 /* Now activate it */
107 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
108 ut_asserteq_ptr(blk, dev);
109 ut_asserteq(true, device_active(dev));
113 DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
115 /* Test that block device numbering works as expected */
116 static int dm_test_blk_devnum(struct unit_test_state *uts)
118 struct udevice *dev, *mmc_dev, *parent;
122 * Probe the devices, with the first one being probed last. This is the
123 * one with no alias / sequence numnber.
125 ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
126 ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
127 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
128 for (i = 0; i < 3; i++) {
129 struct blk_desc *desc;
131 /* Check that the bblock device is attached */
132 ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
133 ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev));
134 parent = dev_get_parent(dev);
135 ut_asserteq_ptr(parent, mmc_dev);
136 ut_asserteq(trailing_strtol(mmc_dev->name), i);
139 * Check that the block device devnum matches its parent's
142 desc = dev_get_uclass_platdata(dev);
143 ut_asserteq(desc->devnum, i);
148 DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
150 /* Test that we can get a block from its parent */
151 static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
153 struct udevice *dev, *blk;
155 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
156 ut_assertok(blk_get_from_parent(dev, &blk));
158 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
159 ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
161 ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
162 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
166 DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);