Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 756ac0b | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 11 | #include <dm/test.h> |
| 12 | #include <dm/root.h> |
| 13 | #include <dm/uclass-internal.h> |
| 14 | #include <dm/ut.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | struct dm_test_state global_test_state; |
| 19 | |
| 20 | /* Get ready for testing */ |
| 21 | static int dm_test_init(struct dm_test_state *dms) |
| 22 | { |
| 23 | memset(dms, '\0', sizeof(*dms)); |
| 24 | gd->dm_root = NULL; |
| 25 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
| 26 | |
| 27 | ut_assertok(dm_init()); |
| 28 | dms->root = dm_root(); |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* Ensure all the test devices are probed */ |
| 34 | static int do_autoprobe(struct dm_test_state *dms) |
| 35 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 36 | struct udevice *dev; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 37 | int ret; |
| 38 | |
| 39 | /* Scanning the uclass is enough to probe all the devices */ |
| 40 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 41 | dev; |
| 42 | ret = uclass_next_device(&dev)) |
| 43 | ; |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static int dm_test_destroy(struct dm_test_state *dms) |
| 49 | { |
| 50 | int id; |
| 51 | |
| 52 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 53 | struct uclass *uc; |
| 54 | |
| 55 | /* |
| 56 | * If the uclass doesn't exist we don't want to create it. So |
| 57 | * check that here before we call uclass_find_device()/ |
| 58 | */ |
| 59 | uc = uclass_find(id); |
| 60 | if (!uc) |
| 61 | continue; |
| 62 | ut_assertok(uclass_destroy(uc)); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Simon Glass | 57f54d5 | 2015-03-25 12:23:04 -0600 | [diff] [blame] | 68 | int dm_test_main(const char *test_name) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 69 | { |
| 70 | struct dm_test *tests = ll_entry_start(struct dm_test, dm_test); |
| 71 | const int n_ents = ll_entry_count(struct dm_test, dm_test); |
| 72 | struct dm_test_state *dms = &global_test_state; |
| 73 | struct dm_test *test; |
| 74 | |
| 75 | /* |
| 76 | * If we have no device tree, or it only has a root node, then these |
| 77 | * tests clearly aren't going to work... |
| 78 | */ |
| 79 | if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) { |
| 80 | puts("Please run with test device tree:\n" |
Przemyslaw Marczak | f64000c | 2015-05-13 13:38:34 +0200 | [diff] [blame^] | 81 | " ./u-boot -d arch/sandbox/dts/test.dtb\n"); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 82 | ut_assert(gd->fdt_blob); |
| 83 | } |
| 84 | |
Simon Glass | 57f54d5 | 2015-03-25 12:23:04 -0600 | [diff] [blame] | 85 | if (!test_name) |
| 86 | printf("Running %d driver model tests\n", n_ents); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 87 | |
| 88 | for (test = tests; test < tests + n_ents; test++) { |
Simon Glass | 57f54d5 | 2015-03-25 12:23:04 -0600 | [diff] [blame] | 89 | if (test_name && strcmp(test_name, test->name)) |
| 90 | continue; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 91 | printf("Test: %s\n", test->name); |
| 92 | ut_assertok(dm_test_init(dms)); |
| 93 | |
Simon Glass | 756ac0b | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 94 | dms->start = mallinfo(); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 95 | if (test->flags & DM_TESTF_SCAN_PDATA) |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 96 | ut_assertok(dm_scan_platdata(false)); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 97 | if (test->flags & DM_TESTF_PROBE_TEST) |
| 98 | ut_assertok(do_autoprobe(dms)); |
| 99 | if (test->flags & DM_TESTF_SCAN_FDT) |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 100 | ut_assertok(dm_scan_fdt(gd->fdt_blob, false)); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 101 | |
| 102 | if (test->func(dms)) |
| 103 | break; |
| 104 | |
| 105 | ut_assertok(dm_test_destroy(dms)); |
| 106 | } |
| 107 | |
| 108 | printf("Failures: %d\n", dms->fail_count); |
| 109 | |
| 110 | return 0; |
| 111 | } |