Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <dm/root.h> |
| 10 | #include <dm/test.h> |
| 11 | #include <dm/ut.h> |
| 12 | #include <dm/util.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
| 16 | static int testbus_drv_probe(struct udevice *dev) |
| 17 | { |
| 18 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
| 19 | } |
| 20 | |
| 21 | static const struct udevice_id testbus_ids[] = { |
| 22 | { |
| 23 | .compatible = "denx,u-boot-test-bus", |
| 24 | .data = DM_TEST_TYPE_FIRST }, |
| 25 | { } |
| 26 | }; |
| 27 | |
| 28 | U_BOOT_DRIVER(testbus_drv) = { |
| 29 | .name = "testbus_drv", |
| 30 | .of_match = testbus_ids, |
| 31 | .id = UCLASS_TEST_BUS, |
| 32 | .probe = testbus_drv_probe, |
| 33 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), |
| 34 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), |
| 35 | }; |
| 36 | |
| 37 | UCLASS_DRIVER(testbus) = { |
| 38 | .name = "testbus", |
| 39 | .id = UCLASS_TEST_BUS, |
| 40 | }; |
| 41 | |
| 42 | /* Test that we can probe for children */ |
| 43 | static int dm_test_bus_children(struct dm_test_state *dms) |
| 44 | { |
| 45 | int num_devices = 4; |
| 46 | struct udevice *bus; |
| 47 | struct uclass *uc; |
| 48 | |
| 49 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 50 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 51 | |
| 52 | /* Probe the bus, which should yield 3 more devices */ |
| 53 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 54 | num_devices += 3; |
| 55 | |
| 56 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 57 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 58 | |
| 59 | ut_assert(!dm_check_devices(dms, num_devices)); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame^] | 64 | |
| 65 | /* Test our functions for accessing children */ |
| 66 | static int dm_test_bus_children_funcs(struct dm_test_state *dms) |
| 67 | { |
| 68 | const void *blob = gd->fdt_blob; |
| 69 | struct udevice *bus, *dev; |
| 70 | int node; |
| 71 | |
| 72 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 73 | |
| 74 | /* device_get_child() */ |
| 75 | ut_assertok(device_get_child(bus, 0, &dev)); |
| 76 | ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev)); |
| 77 | ut_assertok(device_get_child_by_seq(bus, 5, &dev)); |
| 78 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 79 | ut_asserteq_str("c-test@5", dev->name); |
| 80 | |
| 81 | /* Device with sequence number 0 should be accessible */ |
| 82 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev)); |
| 83 | ut_assertok(device_find_child_by_seq(bus, 0, true, &dev)); |
| 84 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 85 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev)); |
| 86 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
| 87 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 88 | |
| 89 | /* There is no device with sequence number 2 */ |
| 90 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev)); |
| 91 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev)); |
| 92 | ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev)); |
| 93 | |
| 94 | /* Looking for something that is not a child */ |
| 95 | node = fdt_path_offset(blob, "/junk"); |
| 96 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 97 | node = fdt_path_offset(blob, "/d-test"); |
| 98 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 99 | |
| 100 | /* Find a valid child */ |
| 101 | node = fdt_path_offset(blob, "/some-bus/c-test@1"); |
| 102 | ut_assertok(device_find_child_by_of_offset(bus, node, &dev)); |
| 103 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 104 | ut_assertok(device_get_child_by_of_offset(bus, node, &dev)); |
| 105 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |