xref: /openbmc/u-boot/test/dm/blk.c (revision a818704b)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <usb.h>
10 #include <asm/state.h>
11 #include <dm/test.h>
12 #include <test/ut.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 /* Test that block devices can be created */
17 static int dm_test_blk_base(struct unit_test_state *uts)
18 {
19 	struct udevice *blk, *usb_blk, *dev;
20 
21 	/* Make sure there are no block devices */
22 	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk));
23 
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));
29 
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);
34 
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);
38 
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));
43 
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));
47 
48 	return 0;
49 }
50 DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
51 
52 static int count_blk_devices(void)
53 {
54 	struct udevice *blk;
55 	struct uclass *uc;
56 	int count = 0;
57 	int ret;
58 
59 	ret = uclass_get(UCLASS_BLK, &uc);
60 	if (ret)
61 		return ret;
62 
63 	uclass_foreach_dev(blk, uc)
64 		count++;
65 
66 	return count;
67 }
68 
69 /* Test that block devices work correctly with USB */
70 static int dm_test_blk_usb(struct unit_test_state *uts)
71 {
72 	struct udevice *usb_dev, *dev;
73 	struct blk_desc *dev_desc;
74 
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));
80 
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));
84 
85 	/* Check we have one block device for each mass storage device */
86 	ut_asserteq(6, count_blk_devices());
87 
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());
93 
94 	return 0;
95 }
96 DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
97 
98 /* Test that we can find block devices without probing them */
99 static int dm_test_blk_find(struct unit_test_state *uts)
100 {
101 	struct udevice *blk, *dev;
102 
103 	ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
104 				      IF_TYPE_HOST, 1, 512, 1024, &blk));
105 	ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
106 	ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
107 	ut_asserteq_ptr(blk, dev);
108 	ut_asserteq(false, device_active(dev));
109 
110 	/* Now activate it */
111 	ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
112 	ut_asserteq_ptr(blk, dev);
113 	ut_asserteq(true, device_active(dev));
114 
115 	return 0;
116 }
117 DM_TEST(dm_test_blk_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
118 
119 /* Test that block device numbering works as expected */
120 static int dm_test_blk_devnum(struct unit_test_state *uts)
121 {
122 	struct udevice *dev, *mmc_dev, *parent;
123 	int i;
124 
125 	/*
126 	 * Probe the devices, with the first one being probed last. This is the
127 	 * one with no alias / sequence numnber.
128 	 */
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;
134 
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(IF_TYPE_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);
141 
142 		/*
143 		 * Check that the block device devnum matches its parent's
144 		 * sequence number
145 		 */
146 		desc = dev_get_uclass_platdata(dev);
147 		ut_asserteq(desc->devnum, i);
148 	}
149 
150 	return 0;
151 }
152 DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
153 
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)
156 {
157 	struct udevice *dev, *blk;
158 
159 	ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
160 	ut_assertok(blk_get_from_parent(dev, &blk));
161 
162 	ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
163 	ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
164 
165 	ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
166 	ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
167 
168 	return 0;
169 }
170 DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
171