blob: 94b2855b8e9efa9aa5736022c08aba9474ead81f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass89c1dcd2016-03-13 08:22:36 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass89c1dcd2016-03-13 08:22:36 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <part.h>
Simon Glass89c1dcd2016-03-13 08:22:36 -06009#include <usb.h>
10#include <asm/state.h>
11#include <dm/test.h>
12#include <test/ut.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16/* Test that block devices can be created */
17static int dm_test_blk_base(struct unit_test_state *uts)
18{
Bin Meng8122dd12018-10-15 02:21:04 -070019 struct udevice *blk1, *blk3, *dev;
Simon Glass89c1dcd2016-03-13 08:22:36 -060020
21 /* Make sure there are no block devices */
Bin Meng8122dd12018-10-15 02:21:04 -070022 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
Simon Glass89c1dcd2016-03-13 08:22:36 -060023
24 /* Create two, one the parent of the other */
25 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
Bin Meng8122dd12018-10-15 02:21:04 -070026 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));
Simon Glass89c1dcd2016-03-13 08:22:36 -060029
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));
Bin Meng8122dd12018-10-15 02:21:04 -070033 ut_asserteq_ptr(blk1, dev);
34 ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
35 ut_asserteq_ptr(blk3, dev);
Simon Glass89c1dcd2016-03-13 08:22:36 -060036
37 /* Check we can iterate */
38 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
Bin Meng8122dd12018-10-15 02:21:04 -070039 ut_asserteq_ptr(blk1, dev);
40 ut_assertok(blk_next_device(&dev));
41 ut_asserteq_ptr(blk3, dev);
Simon Glass89c1dcd2016-03-13 08:22:36 -060042
43 return 0;
44}
45DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
46
47static 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 */
65static 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 */
Simon Glasse4fef742017-04-23 20:02:07 -060081 ut_asserteq(6, count_blk_devices());
Simon Glass89c1dcd2016-03-13 08:22:36 -060082
83 /* Now go around again, making sure the old devices were unbound */
84 ut_assertok(usb_stop());
85 ut_assertok(usb_init());
Simon Glasse4fef742017-04-23 20:02:07 -060086 ut_asserteq(6, count_blk_devices());
Simon Glass89c1dcd2016-03-13 08:22:36 -060087 ut_assertok(usb_stop());
88
89 return 0;
90}
91DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassd5d4c102017-04-23 20:02:05 -060092
93/* Test that we can find block devices without probing them */
94static 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",
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +020099 IF_TYPE_HOST, 1, 512, 2, &blk));
Simon Glassd5d4c102017-04-23 20:02:05 -0600100 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}
112DM_TEST(dm_test_blk_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasse4fef742017-04-23 20:02:07 -0600113
114/* Test that block device numbering works as expected */
115static 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}
147DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass4f269132017-05-27 11:37:17 -0600148
149/* Test that we can get a block from its parent */
150static 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}
165DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);