1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
10 #include <asm/global_data.h>
11 #include <asm/state.h>
13 #include <test/test.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 /* Allow resetting the USB-started flag */
19 extern char usb_started;
21 /* Test that block devices can be created */
22 static int dm_test_blk_base(struct unit_test_state *uts)
24 struct udevice *blk1, *blk3, *dev;
26 /* Create two, one the parent of the other */
27 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
28 UCLASS_ROOT, 1, 512, 2, &blk1));
29 ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
30 UCLASS_ROOT, 3, 512, 2, &blk3));
32 /* Check we can find them */
33 ut_asserteq(-ENODEV, blk_get_device(UCLASS_ROOT, 0, &dev));
34 ut_assertok(blk_get_device(UCLASS_ROOT, 1, &dev));
35 ut_asserteq_ptr(blk1, dev);
36 ut_assertok(blk_get_device(UCLASS_ROOT, 3, &dev));
37 ut_asserteq_ptr(blk3, dev);
39 /* Check we can iterate */
40 ut_assertok(blk_first_device(UCLASS_ROOT, &dev));
41 ut_asserteq_ptr(blk1, dev);
42 ut_assertok(blk_next_device(&dev));
43 ut_asserteq_ptr(blk3, dev);
47 DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
49 static int count_blk_devices(void)
56 ret = uclass_get(UCLASS_BLK, &uc);
60 uclass_foreach_dev(blk, uc)
66 /* Test that block devices work correctly with USB */
67 static int dm_test_blk_usb(struct unit_test_state *uts)
69 struct udevice *usb_dev, *dev;
70 struct blk_desc *dev_desc;
74 /* Get a flash device */
75 state_set_skip_delays(true);
76 ut_assertok(usb_stop());
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(UCLASS_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(6, 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(6, count_blk_devices());
92 ut_assertok(usb_stop());
96 DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
98 /* Test that we can find block devices without probing them */
99 static int dm_test_blk_find(struct unit_test_state *uts)
101 struct udevice *blk, *dev;
103 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
104 UCLASS_ROOT, 1, 512, 2, &blk));
105 ut_asserteq(-ENODEV, blk_find_device(UCLASS_ROOT, 0, &dev));
106 ut_assertok(blk_find_device(UCLASS_ROOT, 1, &dev));
107 ut_asserteq_ptr(blk, dev);
108 ut_asserteq(false, device_active(dev));
110 /* Now activate it */
111 ut_assertok(blk_get_device(UCLASS_ROOT, 1, &dev));
112 ut_asserteq_ptr(blk, dev);
113 ut_asserteq(true, device_active(dev));
117 DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
119 /* Test that block device numbering works as expected */
120 static int dm_test_blk_devnum(struct unit_test_state *uts)
122 struct udevice *dev, *mmc_dev, *parent;
126 * Probe the devices, with the first one being probed last. This is the
127 * one with no alias / sequence numnber.
129 ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
130 ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
131 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
132 for (i = 0; i < 3; i++) {
133 struct blk_desc *desc;
135 /* Check that the bblock device is attached */
136 ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
137 ut_assertok(blk_find_device(UCLASS_MMC, i, &dev));
138 parent = dev_get_parent(dev);
139 ut_asserteq_ptr(parent, mmc_dev);
140 ut_asserteq(trailing_strtol(mmc_dev->name), i);
143 * Check that the block device devnum matches its parent's
146 desc = dev_get_uclass_plat(dev);
147 ut_asserteq(desc->devnum, i);
152 DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
154 /* Test that we can get a block from its parent */
155 static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
157 struct udevice *dev, *blk;
159 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
160 ut_assertok(blk_get_from_parent(dev, &blk));
162 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
163 ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
165 ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
166 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
170 DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
172 /* Test iteration through block devices */
173 static int dm_test_blk_iter(struct unit_test_state *uts)
179 * See sandbox test.dts - it has:
185 ut_assertok(blk_first_device_err(BLKF_FIXED, &dev));
186 ut_asserteq_str("mmc2.blk", dev->name);
187 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
189 ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev));
190 ut_asserteq_str("mmc1.blk", dev->name);
191 ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev));
192 ut_asserteq_str("mmc0.blk", dev->name);
193 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev));
195 ut_assertok(blk_first_device_err(BLKF_BOTH, &dev));
196 ut_asserteq_str("mmc2.blk", dev->name);
197 ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
198 ut_asserteq_str("mmc1.blk", dev->name);
199 ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
200 ut_asserteq_str("mmc0.blk", dev->name);
201 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
203 ut_asserteq(1, blk_count_devices(BLKF_FIXED));
204 ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE));
205 ut_asserteq(3, blk_count_devices(BLKF_BOTH));
208 blk_foreach_probe(BLKF_FIXED, dev)
209 ut_asserteq_str((i++, "mmc2.blk"), dev->name);
213 blk_foreach_probe(BLKF_REMOVABLE, dev)
214 ut_asserteq_str(i++ ? "mmc0.blk" : "mmc1.blk", dev->name);
218 blk_foreach_probe(BLKF_BOTH, dev)
219 ut_asserteq_str((++i == 1 ? "mmc2.blk" : i == 2 ?
220 "mmc1.blk" : "mmc0.blk"), dev->name);
225 DM_TEST(dm_test_blk_iter, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
227 /* Test finding fixed/removable block devices */
228 static int dm_test_blk_flags(struct unit_test_state *uts)
232 /* Iterate through devices without probing them */
233 ut_assertok(blk_find_first(BLKF_BOTH, &dev));
234 ut_assertnonnull(dev);
235 ut_asserteq_str("mmc2.blk", dev->name);
237 ut_assertok(blk_find_next(BLKF_BOTH, &dev));
238 ut_assertnonnull(dev);
239 ut_asserteq_str("mmc1.blk", dev->name);
241 ut_assertok(blk_find_next(BLKF_BOTH, &dev));
242 ut_assertnonnull(dev);
243 ut_asserteq_str("mmc0.blk", dev->name);
245 ut_asserteq(-ENODEV, blk_find_next(BLKF_BOTH, &dev));
248 /* All devices are removable until probed */
249 ut_asserteq(-ENODEV, blk_find_first(BLKF_FIXED, &dev));
251 ut_assertok(blk_find_first(BLKF_REMOVABLE, &dev));
252 ut_assertnonnull(dev);
253 ut_asserteq_str("mmc2.blk", dev->name);
255 /* Now probe them and iterate again */
256 ut_assertok(blk_first_device_err(BLKF_BOTH, &dev));
257 ut_assertnonnull(dev);
258 ut_asserteq_str("mmc2.blk", dev->name);
260 ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
261 ut_assertnonnull(dev);
262 ut_asserteq_str("mmc1.blk", dev->name);
264 ut_assertok(blk_next_device_err(BLKF_BOTH, &dev));
265 ut_assertnonnull(dev);
266 ut_asserteq_str("mmc0.blk", dev->name);
268 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_BOTH, &dev));
270 /* Look only for fixed devices */
271 ut_assertok(blk_first_device_err(BLKF_FIXED, &dev));
272 ut_assertnonnull(dev);
273 ut_asserteq_str("mmc2.blk", dev->name);
275 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev));
277 /* Look only for removable devices */
278 ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev));
279 ut_assertnonnull(dev);
280 ut_asserteq_str("mmc1.blk", dev->name);
282 ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev));
283 ut_assertnonnull(dev);
284 ut_asserteq_str("mmc0.blk", dev->name);
286 ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev));
290 DM_TEST(dm_test_blk_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
292 /* Test blk_foreach() and friend */
293 static int dm_test_blk_foreach(struct unit_test_state *uts)
298 /* Test blk_foreach() - use the 3rd bytes of the name (0/1/2) */
300 blk_foreach(BLKF_BOTH, dev)
301 found |= 1 << dectoul(&dev->name[3], NULL);
302 ut_asserteq(7, found);
304 /* All devices are removable until probed */
306 blk_foreach(BLKF_FIXED, dev)
307 found |= 1 << dectoul(&dev->name[3], NULL);
308 ut_asserteq(0, found);
311 blk_foreach(BLKF_REMOVABLE, dev)
312 found |= 1 << dectoul(&dev->name[3], NULL);
313 ut_asserteq(7, found);
315 /* Now try again with the probing functions */
317 blk_foreach_probe(BLKF_BOTH, dev)
318 found |= 1 << dectoul(&dev->name[3], NULL);
319 ut_asserteq(7, found);
320 ut_asserteq(3, blk_count_devices(BLKF_BOTH));
323 blk_foreach_probe(BLKF_FIXED, dev)
324 found |= 1 << dectoul(&dev->name[3], NULL);
325 ut_asserteq(4, found);
326 ut_asserteq(1, blk_count_devices(BLKF_FIXED));
329 blk_foreach_probe(BLKF_REMOVABLE, dev)
330 found |= 1 << dectoul(&dev->name[3], NULL);
331 ut_asserteq(3, found);
332 ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE));
336 DM_TEST(dm_test_blk_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);