blob: 4de477bafa3ad6378ecc31a93d0c938ca15ecd38 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse4fb8632016-03-13 08:22:36 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glasse4fb8632016-03-13 08:22:36 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <usb.h>
9#include <asm/state.h>
10#include <dm/test.h>
11#include <test/ut.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15/* Test that block devices can be created */
16static int dm_test_blk_base(struct unit_test_state *uts)
17{
18 struct udevice *blk, *usb_blk, *dev;
19
20 /* Make sure there are no block devices */
21 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk));
22
23 /* Create two, one the parent of the other */
24 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +020025 IF_TYPE_HOST, 1, 512, 2, &blk));
Simon Glasse4fb8632016-03-13 08:22:36 -060026 ut_assertok(blk_create_device(blk, "usb_storage_blk", "test",
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +020027 IF_TYPE_USB, 3, 512, 2, &usb_blk));
Simon Glasse4fb8632016-03-13 08:22:36 -060028
29 /* Check we can find them */
30 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
31 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
32 ut_asserteq_ptr(blk, dev);
33
34 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_USB, 0, &dev));
35 ut_assertok(blk_get_device(IF_TYPE_USB, 3, &dev));
36 ut_asserteq_ptr(usb_blk, dev);
37
38 /* Check we can iterate */
39 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
40 ut_asserteq_ptr(blk, dev);
41 ut_asserteq(-ENODEV, blk_next_device(&dev));
42
43 ut_assertok(blk_first_device(IF_TYPE_USB, &dev));
44 ut_asserteq_ptr(usb_blk, dev);
45 ut_asserteq(-ENODEV, blk_next_device(&dev));
46
47 return 0;
48}
49DM_TEST(dm_test_blk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
50
51static int count_blk_devices(void)
52{
53 struct udevice *blk;
54 struct uclass *uc;
55 int count = 0;
56 int ret;
57
58 ret = uclass_get(UCLASS_BLK, &uc);
59 if (ret)
60 return ret;
61
62 uclass_foreach_dev(blk, uc)
63 count++;
64
65 return count;
66}
67
68/* Test that block devices work correctly with USB */
69static int dm_test_blk_usb(struct unit_test_state *uts)
70{
71 struct udevice *usb_dev, *dev;
72 struct blk_desc *dev_desc;
73
74 /* Get a flash device */
75 state_set_skip_delays(true);
76 ut_assertok(usb_init());
77 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
78 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
79
80 /* The parent should be a block device */
81 ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
82 ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
83
84 /* Check we have one block device for each mass storage device */
Simon Glasse48eeb92017-04-23 20:02:07 -060085 ut_asserteq(6, count_blk_devices());
Simon Glasse4fb8632016-03-13 08:22:36 -060086
87 /* Now go around again, making sure the old devices were unbound */
88 ut_assertok(usb_stop());
89 ut_assertok(usb_init());
Simon Glasse48eeb92017-04-23 20:02:07 -060090 ut_asserteq(6, count_blk_devices());
Simon Glasse4fb8632016-03-13 08:22:36 -060091 ut_assertok(usb_stop());
92
93 return 0;
94}
95DM_TEST(dm_test_blk_usb, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass61392812017-04-23 20:02:05 -060096
97/* Test that we can find block devices without probing them */
98static int dm_test_blk_find(struct unit_test_state *uts)
99{
100 struct udevice *blk, *dev;
101
102 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200103 IF_TYPE_HOST, 1, 512, 2, &blk));
Simon Glass61392812017-04-23 20:02:05 -0600104 ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
105 ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
106 ut_asserteq_ptr(blk, dev);
107 ut_asserteq(false, device_active(dev));
108
109 /* Now activate it */
110 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
111 ut_asserteq_ptr(blk, dev);
112 ut_asserteq(true, device_active(dev));
113
114 return 0;
115}
116DM_TEST(dm_test_blk_find, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasse48eeb92017-04-23 20:02:07 -0600117
118/* Test that block device numbering works as expected */
119static int dm_test_blk_devnum(struct unit_test_state *uts)
120{
121 struct udevice *dev, *mmc_dev, *parent;
122 int i;
123
124 /*
125 * Probe the devices, with the first one being probed last. This is the
126 * one with no alias / sequence numnber.
127 */
128 ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
129 ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
130 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
131 for (i = 0; i < 3; i++) {
132 struct blk_desc *desc;
133
134 /* Check that the bblock device is attached */
135 ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
136 ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev));
137 parent = dev_get_parent(dev);
138 ut_asserteq_ptr(parent, mmc_dev);
139 ut_asserteq(trailing_strtol(mmc_dev->name), i);
140
141 /*
142 * Check that the block device devnum matches its parent's
143 * sequence number
144 */
145 desc = dev_get_uclass_platdata(dev);
146 ut_asserteq(desc->devnum, i);
147 }
148
149 return 0;
150}
151DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass9f103b92017-05-27 11:37:17 -0600152
153/* Test that we can get a block from its parent */
154static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
155{
156 struct udevice *dev, *blk;
157
158 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
159 ut_assertok(blk_get_from_parent(dev, &blk));
160
161 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
162 ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
163
164 ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
165 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
166
167 return 0;
168}
169DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);