blob: f34c13f7511c3cb5236745a1c3c36736497c54ec [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>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <part.h>
Simon Glasse4fb8632016-03-13 08:22:36 -06009#include <usb.h>
10#include <asm/state.h>
11#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060012#include <test/test.h>
Simon Glasse4fb8632016-03-13 08:22:36 -060013#include <test/ut.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17/* Test that block devices can be created */
18static int dm_test_blk_base(struct unit_test_state *uts)
19{
Bin Mengfa583f82018-10-15 02:21:04 -070020 struct udevice *blk1, *blk3, *dev;
Simon Glasse4fb8632016-03-13 08:22:36 -060021
22 /* Make sure there are no block devices */
Bin Mengfa583f82018-10-15 02:21:04 -070023 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
Simon Glasse4fb8632016-03-13 08:22:36 -060024
25 /* Create two, one the parent of the other */
26 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
Bin Mengfa583f82018-10-15 02:21:04 -070027 IF_TYPE_HOST, 1, 512, 2, &blk1));
28 ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
29 IF_TYPE_HOST, 3, 512, 2, &blk3));
Simon Glasse4fb8632016-03-13 08:22:36 -060030
31 /* Check we can find them */
32 ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
33 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
Bin Mengfa583f82018-10-15 02:21:04 -070034 ut_asserteq_ptr(blk1, dev);
35 ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
36 ut_asserteq_ptr(blk3, dev);
Simon Glasse4fb8632016-03-13 08:22:36 -060037
38 /* Check we can iterate */
39 ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
Bin Mengfa583f82018-10-15 02:21:04 -070040 ut_asserteq_ptr(blk1, dev);
41 ut_assertok(blk_next_device(&dev));
42 ut_asserteq_ptr(blk3, dev);
Simon Glasse4fb8632016-03-13 08:22:36 -060043
44 return 0;
45}
Simon Glasse180c2b2020-07-28 19:41:12 -060046DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse4fb8632016-03-13 08:22:36 -060047
48static int count_blk_devices(void)
49{
50 struct udevice *blk;
51 struct uclass *uc;
52 int count = 0;
53 int ret;
54
55 ret = uclass_get(UCLASS_BLK, &uc);
56 if (ret)
57 return ret;
58
59 uclass_foreach_dev(blk, uc)
60 count++;
61
62 return count;
63}
64
65/* Test that block devices work correctly with USB */
66static int dm_test_blk_usb(struct unit_test_state *uts)
67{
68 struct udevice *usb_dev, *dev;
69 struct blk_desc *dev_desc;
70
71 /* Get a flash device */
72 state_set_skip_delays(true);
73 ut_assertok(usb_init());
74 ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
75 ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
76
77 /* The parent should be a block device */
78 ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
79 ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
80
81 /* Check we have one block device for each mass storage device */
Simon Glasse48eeb92017-04-23 20:02:07 -060082 ut_asserteq(6, count_blk_devices());
Simon Glasse4fb8632016-03-13 08:22:36 -060083
84 /* Now go around again, making sure the old devices were unbound */
85 ut_assertok(usb_stop());
86 ut_assertok(usb_init());
Simon Glasse48eeb92017-04-23 20:02:07 -060087 ut_asserteq(6, count_blk_devices());
Simon Glasse4fb8632016-03-13 08:22:36 -060088 ut_assertok(usb_stop());
89
90 return 0;
91}
Simon Glasse180c2b2020-07-28 19:41:12 -060092DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass61392812017-04-23 20:02:05 -060093
94/* Test that we can find block devices without probing them */
95static int dm_test_blk_find(struct unit_test_state *uts)
96{
97 struct udevice *blk, *dev;
98
99 ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200100 IF_TYPE_HOST, 1, 512, 2, &blk));
Simon Glass61392812017-04-23 20:02:05 -0600101 ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
102 ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
103 ut_asserteq_ptr(blk, dev);
104 ut_asserteq(false, device_active(dev));
105
106 /* Now activate it */
107 ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
108 ut_asserteq_ptr(blk, dev);
109 ut_asserteq(true, device_active(dev));
110
111 return 0;
112}
Simon Glasse180c2b2020-07-28 19:41:12 -0600113DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse48eeb92017-04-23 20:02:07 -0600114
115/* Test that block device numbering works as expected */
116static int dm_test_blk_devnum(struct unit_test_state *uts)
117{
118 struct udevice *dev, *mmc_dev, *parent;
119 int i;
120
121 /*
122 * Probe the devices, with the first one being probed last. This is the
123 * one with no alias / sequence numnber.
124 */
125 ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
126 ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
127 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
128 for (i = 0; i < 3; i++) {
129 struct blk_desc *desc;
130
131 /* Check that the bblock device is attached */
132 ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
133 ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev));
134 parent = dev_get_parent(dev);
135 ut_asserteq_ptr(parent, mmc_dev);
136 ut_asserteq(trailing_strtol(mmc_dev->name), i);
137
138 /*
139 * Check that the block device devnum matches its parent's
140 * sequence number
141 */
142 desc = dev_get_uclass_platdata(dev);
143 ut_asserteq(desc->devnum, i);
144 }
145
146 return 0;
147}
Simon Glasse180c2b2020-07-28 19:41:12 -0600148DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9f103b92017-05-27 11:37:17 -0600149
150/* Test that we can get a block from its parent */
151static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
152{
153 struct udevice *dev, *blk;
154
155 ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
156 ut_assertok(blk_get_from_parent(dev, &blk));
157
158 ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
159 ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
160
161 ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
162 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
163
164 return 0;
165}
Simon Glasse180c2b2020-07-28 19:41:12 -0600166DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);