test: Use ut_asserteq_mem() where possible
[oweals/u-boot.git] / test / dm / blk.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <part.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 *blk1, *blk3, *dev;
20
21         /* Make sure there are no block devices */
22         ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
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, 2, &blk1));
27         ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
28                                       IF_TYPE_HOST, 3, 512, 2, &blk3));
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(blk1, dev);
34         ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
35         ut_asserteq_ptr(blk3, dev);
36
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);
42
43         return 0;
44 }
45 DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
46
47 static int count_blk_devices(void)
48 {
49         struct udevice *blk;
50         struct uclass *uc;
51         int count = 0;
52         int ret;
53
54         ret = uclass_get(UCLASS_BLK, &uc);
55         if (ret)
56                 return ret;
57
58         uclass_foreach_dev(blk, uc)
59                 count++;
60
61         return count;
62 }
63
64 /* Test that block devices work correctly with USB */
65 static int dm_test_blk_usb(struct unit_test_state *uts)
66 {
67         struct udevice *usb_dev, *dev;
68         struct blk_desc *dev_desc;
69
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));
75
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));
79
80         /* Check we have one block device for each mass storage device */
81         ut_asserteq(6, count_blk_devices());
82
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());
88
89         return 0;
90 }
91 DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
92
93 /* Test that we can find block devices without probing them */
94 static int dm_test_blk_find(struct unit_test_state *uts)
95 {
96         struct udevice *blk, *dev;
97
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));
104
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));
109
110         return 0;
111 }
112 DM_TEST(dm_test_blk_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
113
114 /* Test that block device numbering works as expected */
115 static int dm_test_blk_devnum(struct unit_test_state *uts)
116 {
117         struct udevice *dev, *mmc_dev, *parent;
118         int i;
119
120         /*
121          * Probe the devices, with the first one being probed last. This is the
122          * one with no alias / sequence numnber.
123          */
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;
129
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);
136
137                 /*
138                  * Check that the block device devnum matches its parent's
139                  * sequence number
140                  */
141                 desc = dev_get_uclass_platdata(dev);
142                 ut_asserteq(desc->devnum, i);
143         }
144
145         return 0;
146 }
147 DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
148
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)
151 {
152         struct udevice *dev, *blk;
153
154         ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
155         ut_assertok(blk_get_from_parent(dev, &blk));
156
157         ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
158         ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
159
160         ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
161         ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
162
163         return 0;
164 }
165 DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);