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> |
| 10 | #include <asm/state.h> |
| 11 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 12 | #include <test/test.h> |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 13 | #include <test/ut.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | /* Test that block devices can be created */ |
| 18 | static int dm_test_blk_base(struct unit_test_state *uts) |
| 19 | { |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 20 | struct udevice *blk1, *blk3, *dev; |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 21 | |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 22 | /* Create two, one the parent of the other */ |
| 23 | ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 24 | IF_TYPE_HOST, 1, 512, 2, &blk1)); |
| 25 | ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test", |
| 26 | IF_TYPE_HOST, 3, 512, 2, &blk3)); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 27 | |
| 28 | /* Check we can find them */ |
| 29 | ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev)); |
| 30 | ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 31 | ut_asserteq_ptr(blk1, dev); |
| 32 | ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev)); |
| 33 | ut_asserteq_ptr(blk3, dev); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 34 | |
| 35 | /* Check we can iterate */ |
| 36 | ut_assertok(blk_first_device(IF_TYPE_HOST, &dev)); |
Bin Meng | fa583f8 | 2018-10-15 02:21:04 -0700 | [diff] [blame] | 37 | ut_asserteq_ptr(blk1, dev); |
| 38 | ut_assertok(blk_next_device(&dev)); |
| 39 | ut_asserteq_ptr(blk3, dev); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | static int count_blk_devices(void) |
| 46 | { |
| 47 | struct udevice *blk; |
| 48 | struct uclass *uc; |
| 49 | int count = 0; |
| 50 | int ret; |
| 51 | |
| 52 | ret = uclass_get(UCLASS_BLK, &uc); |
| 53 | if (ret) |
| 54 | return ret; |
| 55 | |
| 56 | uclass_foreach_dev(blk, uc) |
| 57 | count++; |
| 58 | |
| 59 | return count; |
| 60 | } |
| 61 | |
| 62 | /* Test that block devices work correctly with USB */ |
| 63 | static int dm_test_blk_usb(struct unit_test_state *uts) |
| 64 | { |
| 65 | struct udevice *usb_dev, *dev; |
| 66 | struct blk_desc *dev_desc; |
| 67 | |
| 68 | /* Get a flash device */ |
| 69 | state_set_skip_delays(true); |
| 70 | ut_assertok(usb_init()); |
| 71 | ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev)); |
| 72 | ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc)); |
| 73 | |
| 74 | /* The parent should be a block device */ |
| 75 | ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev)); |
| 76 | ut_asserteq_ptr(usb_dev, dev_get_parent(dev)); |
| 77 | |
| 78 | /* Check we have one block device for each mass storage device */ |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 79 | ut_asserteq(6, count_blk_devices()); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 80 | |
| 81 | /* Now go around again, making sure the old devices were unbound */ |
| 82 | ut_assertok(usb_stop()); |
| 83 | ut_assertok(usb_init()); |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 84 | ut_asserteq(6, count_blk_devices()); |
Simon Glass | e4fb863 | 2016-03-13 08:22:36 -0600 | [diff] [blame] | 85 | ut_assertok(usb_stop()); |
| 86 | |
| 87 | return 0; |
| 88 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 89 | 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] | 90 | |
| 91 | /* Test that we can find block devices without probing them */ |
| 92 | static int dm_test_blk_find(struct unit_test_state *uts) |
| 93 | { |
| 94 | struct udevice *blk, *dev; |
| 95 | |
| 96 | 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] | 97 | IF_TYPE_HOST, 1, 512, 2, &blk)); |
Simon Glass | 6139281 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 98 | ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev)); |
| 99 | ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev)); |
| 100 | ut_asserteq_ptr(blk, dev); |
| 101 | ut_asserteq(false, device_active(dev)); |
| 102 | |
| 103 | /* Now activate it */ |
| 104 | ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); |
| 105 | ut_asserteq_ptr(blk, dev); |
| 106 | ut_asserteq(true, device_active(dev)); |
| 107 | |
| 108 | return 0; |
| 109 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 110 | 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] | 111 | |
| 112 | /* Test that block device numbering works as expected */ |
| 113 | static int dm_test_blk_devnum(struct unit_test_state *uts) |
| 114 | { |
| 115 | struct udevice *dev, *mmc_dev, *parent; |
| 116 | int i; |
| 117 | |
| 118 | /* |
| 119 | * Probe the devices, with the first one being probed last. This is the |
| 120 | * one with no alias / sequence numnber. |
| 121 | */ |
| 122 | ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev)); |
| 123 | ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev)); |
| 124 | ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev)); |
| 125 | for (i = 0; i < 3; i++) { |
| 126 | struct blk_desc *desc; |
| 127 | |
| 128 | /* Check that the bblock device is attached */ |
| 129 | ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev)); |
| 130 | ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev)); |
| 131 | parent = dev_get_parent(dev); |
| 132 | ut_asserteq_ptr(parent, mmc_dev); |
| 133 | ut_asserteq(trailing_strtol(mmc_dev->name), i); |
| 134 | |
| 135 | /* |
| 136 | * Check that the block device devnum matches its parent's |
| 137 | * sequence number |
| 138 | */ |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 139 | desc = dev_get_uclass_plat(dev); |
Simon Glass | e48eeb9 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 140 | ut_asserteq(desc->devnum, i); |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 145 | 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] | 146 | |
| 147 | /* Test that we can get a block from its parent */ |
| 148 | static int dm_test_blk_get_from_parent(struct unit_test_state *uts) |
| 149 | { |
| 150 | struct udevice *dev, *blk; |
| 151 | |
| 152 | ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev)); |
| 153 | ut_assertok(blk_get_from_parent(dev, &blk)); |
| 154 | |
| 155 | ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev)); |
| 156 | ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk)); |
| 157 | |
| 158 | ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev)); |
| 159 | ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk)); |
| 160 | |
| 161 | return 0; |
| 162 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 163 | DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |