1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
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 *blk1, *blk3, *dev;
21 /* Make sure there are no block devices */
22 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
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, 2, &blk1));
27 ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
28 IF_TYPE_HOST, 3, 512, 2, &blk3));
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(blk1, dev);
34 ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
35 ut_asserteq_ptr(blk3, dev);
37 /* Check we can iterate */
38 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
39 ut_asserteq_ptr(blk1, dev);
40 ut_assertok(blk_next_device(&dev));
41 ut_asserteq_ptr(blk3, dev);
45 DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
47 static int count_blk_devices(void)
54 ret = uclass_get(UCLASS_BLK, &uc);
58 uclass_foreach_dev(blk, uc)
64 /* Test that block devices work correctly with USB */
65 static int dm_test_blk_usb(struct unit_test_state *uts)
67 struct udevice *usb_dev, *dev;
68 struct blk_desc *dev_desc;
70 /* Get a flash device */
71 state_set_skip_delays(true);
72 ut_assertok(usb_init());
73 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
74 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
76 /* The parent should be a block device */
77 ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
78 ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
80 /* Check we have one block device for each mass storage device */
81 ut_asserteq(6, count_blk_devices());
83 /* Now go around again, making sure the old devices were unbound */
84 ut_assertok(usb_stop());
85 ut_assertok(usb_init());
86 ut_asserteq(6, count_blk_devices());
87 ut_assertok(usb_stop());
91 DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
93 /* Test that we can find block devices without probing them */
94 static int dm_test_blk_find(struct unit_test_state *uts)
96 struct udevice *blk, *dev;
98 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
99 IF_TYPE_HOST, 1, 512, 2, &blk));
100 ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
101 ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
102 ut_asserteq_ptr(blk, dev);
103 ut_asserteq(false, device_active(dev));
105 /* Now activate it */
106 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
107 ut_asserteq_ptr(blk, dev);
108 ut_asserteq(true, device_active(dev));
112 DM_TEST(dm_test_blk_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
114 /* Test that block device numbering works as expected */
115 static int dm_test_blk_devnum(struct unit_test_state *uts)
117 struct udevice *dev, *mmc_dev, *parent;
121 * Probe the devices, with the first one being probed last. This is the
122 * one with no alias / sequence numnber.
124 ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
125 ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
126 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
127 for (i = 0; i < 3; i++) {
128 struct blk_desc *desc;
130 /* Check that the bblock device is attached */
131 ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
132 ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev));
133 parent = dev_get_parent(dev);
134 ut_asserteq_ptr(parent, mmc_dev);
135 ut_asserteq(trailing_strtol(mmc_dev->name), i);
138 * Check that the block device devnum matches its parent's
141 desc = dev_get_uclass_platdata(dev);
142 ut_asserteq(desc->devnum, i);
147 DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
149 /* Test that we can get a block from its parent */
150 static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
152 struct udevice *dev, *blk;
154 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
155 ut_assertok(blk_get_from_parent(dev, &blk));
157 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
158 ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
160 ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
161 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
165 DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);