Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 8 | #include <part.h> |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 9 | #include <usb.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 11 | #include <asm/state.h> |
| 12 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <test/test.h> |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 14 | #include <test/ut.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | /* Test that block devices can be created */ |
| 19 | static int dm_test_blk_base(struct unit_test_state *uts) |
| 20 | { |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 21 | struct udevice *blk1, *blk3, *dev; |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 22 | |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 23 | /* Create two, one the parent of the other */ |
| 24 | ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 25 | IF_TYPE_HOST, 1, 512, 2, &blk1)); |
| 26 | ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test", |
| 27 | IF_TYPE_HOST, 3, 512, 2, &blk3)); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 28 | |
| 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)); |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 32 | ut_asserteq_ptr(blk1, dev); |
| 33 | ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev)); |
| 34 | ut_asserteq_ptr(blk3, dev); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 35 | |
| 36 | /* Check we can iterate */ |
| 37 | ut_assertok(blk_first_device(IF_TYPE_HOST, &dev)); |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 38 | ut_asserteq_ptr(blk1, dev); |
| 39 | ut_assertok(blk_next_device(&dev)); |
| 40 | ut_asserteq_ptr(blk3, dev); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 41 | |
| 42 | return 0; |
| 43 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 44 | DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 45 | |
| 46 | static int count_blk_devices(void) |
| 47 | { |
| 48 | struct udevice *blk; |
| 49 | struct uclass *uc; |
| 50 | int count = 0; |
| 51 | int ret; |
| 52 | |
| 53 | ret = uclass_get(UCLASS_BLK, &uc); |
| 54 | if (ret) |
| 55 | return ret; |
| 56 | |
| 57 | uclass_foreach_dev(blk, uc) |
| 58 | count++; |
| 59 | |
| 60 | return count; |
| 61 | } |
| 62 | |
| 63 | /* Test that block devices work correctly with USB */ |
| 64 | static int dm_test_blk_usb(struct unit_test_state *uts) |
| 65 | { |
| 66 | struct udevice *usb_dev, *dev; |
| 67 | struct blk_desc *dev_desc; |
| 68 | |
| 69 | /* Get a flash device */ |
| 70 | state_set_skip_delays(true); |
| 71 | ut_assertok(usb_init()); |
| 72 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev)); |
| 73 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
| 74 | |
| 75 | /* The parent should be a block device */ |
| 76 | ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev)); |
| 77 | ut_asserteq_ptr(usb_dev, dev_get_parent(dev)); |
| 78 | |
| 79 | /* Check we have one block device for each mass storage device */ |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 80 | ut_asserteq(6, count_blk_devices()); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 81 | |
| 82 | /* Now go around again, making sure the old devices were unbound */ |
| 83 | ut_assertok(usb_stop()); |
| 84 | ut_assertok(usb_init()); |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 85 | ut_asserteq(6, count_blk_devices()); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 86 | ut_assertok(usb_stop()); |
| 87 | |
| 88 | return 0; |
| 89 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 90 | DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 6139281 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 91 | |
| 92 | /* Test that we can find block devices without probing them */ |
| 93 | static int dm_test_blk_find(struct unit_test_state *uts) |
| 94 | { |
| 95 | struct udevice *blk, *dev; |
| 96 | |
| 97 | ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", |
Jean-Jacques Hiblot | 5fe7702 | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 98 | IF_TYPE_HOST, 1, 512, 2, &blk)); |
Simon Glass | 6139281 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 99 | ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev)); |
| 100 | ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev)); |
| 101 | ut_asserteq_ptr(blk, dev); |
| 102 | ut_asserteq(false, device_active(dev)); |
| 103 | |
| 104 | /* Now activate it */ |
| 105 | ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); |
| 106 | ut_asserteq_ptr(blk, dev); |
| 107 | ut_asserteq(true, device_active(dev)); |
| 108 | |
| 109 | return 0; |
| 110 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 111 | DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 112 | |
| 113 | /* Test that block device numbering works as expected */ |
| 114 | static int dm_test_blk_devnum(struct unit_test_state *uts) |
| 115 | { |
| 116 | struct udevice *dev, *mmc_dev, *parent; |
| 117 | int i; |
| 118 | |
| 119 | /* |
| 120 | * Probe the devices, with the first one being probed last. This is the |
| 121 | * one with no alias / sequence numnber. |
| 122 | */ |
| 123 | ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev)); |
| 124 | ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev)); |
| 125 | ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev)); |
| 126 | for (i = 0; i < 3; i++) { |
| 127 | struct blk_desc *desc; |
| 128 | |
| 129 | /* Check that the bblock device is attached */ |
| 130 | ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev)); |
| 131 | ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev)); |
| 132 | parent = dev_get_parent(dev); |
| 133 | ut_asserteq_ptr(parent, mmc_dev); |
| 134 | ut_asserteq(trailing_strtol(mmc_dev->name), i); |
| 135 | |
| 136 | /* |
| 137 | * Check that the block device devnum matches its parent's |
| 138 | * sequence number |
| 139 | */ |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 140 | desc = dev_get_uclass_plat(dev); |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 141 | ut_asserteq(desc->devnum, i); |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 146 | DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 9f103b9 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 147 | |
| 148 | /* Test that we can get a block from its parent */ |
| 149 | static int dm_test_blk_get_from_parent(struct unit_test_state *uts) |
| 150 | { |
| 151 | struct udevice *dev, *blk; |
| 152 | |
| 153 | ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev)); |
| 154 | ut_assertok(blk_get_from_parent(dev, &blk)); |
| 155 | |
| 156 | ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev)); |
| 157 | ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk)); |
| 158 | |
| 159 | ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev)); |
| 160 | ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk)); |
| 161 | |
| 162 | return 0; |
| 163 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 164 | DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 96f37b0 | 2021-07-05 16:32:59 -0600 | [diff] [blame^] | 165 | |
| 166 | /* Test iteration through block devices */ |
| 167 | static int dm_test_blk_iter(struct unit_test_state *uts) |
| 168 | { |
| 169 | struct udevice *dev; |
| 170 | int i; |
| 171 | |
| 172 | /* |
| 173 | * See sandbox test.dts - it has: |
| 174 | * |
| 175 | * mmc0 - removable |
| 176 | * mmc1 - removable |
| 177 | * mmc2 - fixed |
| 178 | */ |
| 179 | ut_assertok(blk_first_device_err(BLKF_FIXED, &dev)); |
| 180 | ut_asserteq_str("mmc2.blk", dev->name); |
| 181 | ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev)); |
| 182 | |
| 183 | ut_assertok(blk_first_device_err(BLKF_REMOVABLE, &dev)); |
| 184 | ut_asserteq_str("mmc1.blk", dev->name); |
| 185 | ut_assertok(blk_next_device_err(BLKF_REMOVABLE, &dev)); |
| 186 | ut_asserteq_str("mmc0.blk", dev->name); |
| 187 | ut_asserteq(-ENODEV, blk_next_device_err(BLKF_REMOVABLE, &dev)); |
| 188 | |
| 189 | ut_assertok(blk_first_device_err(BLKF_BOTH, &dev)); |
| 190 | ut_asserteq_str("mmc2.blk", dev->name); |
| 191 | ut_assertok(blk_next_device_err(BLKF_BOTH, &dev)); |
| 192 | ut_asserteq_str("mmc1.blk", dev->name); |
| 193 | ut_assertok(blk_next_device_err(BLKF_BOTH, &dev)); |
| 194 | ut_asserteq_str("mmc0.blk", dev->name); |
| 195 | ut_asserteq(-ENODEV, blk_next_device_err(BLKF_FIXED, &dev)); |
| 196 | |
| 197 | ut_asserteq(1, blk_count_devices(BLKF_FIXED)); |
| 198 | ut_asserteq(2, blk_count_devices(BLKF_REMOVABLE)); |
| 199 | ut_asserteq(3, blk_count_devices(BLKF_BOTH)); |
| 200 | |
| 201 | i = 0; |
| 202 | blk_foreach_probe(BLKF_FIXED, dev) |
| 203 | ut_asserteq_str((i++, "mmc2.blk"), dev->name); |
| 204 | ut_asserteq(1, i); |
| 205 | |
| 206 | i = 0; |
| 207 | blk_foreach_probe(BLKF_REMOVABLE, dev) |
| 208 | ut_asserteq_str(i++ ? "mmc0.blk" : "mmc1.blk", dev->name); |
| 209 | ut_asserteq(2, i); |
| 210 | |
| 211 | i = 0; |
| 212 | blk_foreach_probe(BLKF_BOTH, dev) |
| 213 | ut_asserteq_str((++i == 1 ? "mmc2.blk" : i == 2 ? |
| 214 | "mmc1.blk" : "mmc0.blk"), dev->name); |
| 215 | ut_asserteq(3, i); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | DM_TEST(dm_test_blk_iter, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |