MAINTAINERS: Add Maximilian Brune to RISC-V
[coreboot.git] / tests / commonlib / device_tree-test.c
blob0c1e2dd8f33301193449a21f0b95f9779c2187d4
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/device_tree.h>
4 #include <console/console.h>
5 #include <helpers/file.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <tests/test.h>
13 static int setup_device_tree_test_group(void **state)
16 * fattest FDT I could find from Linux Kernel to test the worst cases
17 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm/boot/dts/nvidia/tegra30-ouya.dts?id=443b349019f2d946
19 const char dtb_path[] = "lib/devicetree-test/tegra30-ouya.dtb";
20 int file_size = test_get_file_size(dtb_path);
21 assert_int_not_equal(file_size, -1);
23 void *big_fat_dtb = test_malloc(file_size);
24 assert_int_not_equal(big_fat_dtb, NULL);
25 assert_int_equal(test_read_file(dtb_path, big_fat_dtb, file_size), file_size);
26 *state = big_fat_dtb;
28 return 0;
31 static int teardown_device_tree_test_group(void **state)
33 test_free(*state);
34 return 0;
37 static void test_fdt_find_node_by_path(void **state)
39 uint32_t addrcp, sizecp;
40 assert_int_equal(0, fdt_find_node_by_path(*state, "test", &addrcp, &sizecp));
41 assert_int_equal(56, fdt_find_node_by_path(*state, "/", &addrcp, &sizecp));
42 assert_int_equal(2, addrcp);
43 assert_int_equal(1, sizecp);
44 assert_int_equal(0, fdt_find_node_by_path(*state, "/test", &addrcp, &sizecp));
45 assert_int_equal(0x181f4, fdt_find_node_by_path(*state, "/chosen", &addrcp, &sizecp));
46 assert_int_equal(1, addrcp);
47 assert_int_equal(1, sizecp);
48 assert_int_equal(0x156d4, fdt_find_node_by_path(*state, "/cpus", &addrcp, &sizecp));
49 assert_int_equal(1, addrcp);
50 assert_int_equal(1, sizecp);
51 assert_int_equal(0x1517c, fdt_find_node_by_path(*state, "/usb@7d004000/ethernet@2", &addrcp, &sizecp));
52 assert_int_equal(1, addrcp);
53 assert_int_equal(0, sizecp);
54 assert_int_equal(0x1517c, fdt_find_node_by_path(*state, "/usb@7d004000/ethernet@2/", &addrcp, &sizecp));
55 assert_int_equal(1, addrcp);
56 assert_int_equal(0, sizecp);
58 assert_int_equal(0xee08, fdt_find_node_by_path(*state, "/pinmux@70000868/pinmux/drive_groups",
59 &addrcp, &sizecp));
62 static void test_fdt_find_subnodes_by_prefix(void **state)
64 uint32_t offset = fdt_find_node_by_path(*state, "/cpus", NULL, NULL);
65 uint32_t results[3] = { 0 };
66 uint32_t addrcp, sizecp;
67 size_t count_results = fdt_find_subnodes_by_prefix(*state, offset, "cpu@",
68 &addrcp, &sizecp, results, 3);
69 assert_int_equal(3, count_results);
70 assert_int_equal(0x15700, results[0]);
71 assert_int_equal(0x157a0, results[1]);
72 assert_int_equal(0x15840, results[2]);
74 results[1] = 0xDEADBEEF;
75 results[2] = 0xDEADBEEF;
76 count_results = fdt_find_subnodes_by_prefix(*state, offset, "cpu@",
77 &addrcp, &sizecp, results, 1);
78 assert_int_equal(1, count_results);
79 assert_int_equal(0x15700, results[0]);
80 assert_int_equal(0xDEADBEEF, results[1]);
81 assert_int_equal(0xDEADBEEF, results[2]);
84 static void test_fdt_find_node_by_alias(void **state)
86 assert_int_equal(0xf298, fdt_find_node_by_alias(*state, "serial0", NULL, NULL));
87 assert_int_equal(0, fdt_find_node_by_alias(*state, "mmc2", NULL, NULL));
90 static void test_fdt_find_prop_in_node(void **state)
92 uintptr_t cnode_offset = fdt_find_node_by_path(*state, "/clock", NULL, NULL);
93 uintptr_t mnode_offset = fdt_find_node_by_path(*state, "/memory@80000000", NULL, NULL);
94 assert_int_equal(0x18400, cnode_offset);
96 struct fdt_property fdt_prop;
97 assert_int_equal(0x1840c, fdt_read_prop(*state, cnode_offset, "compatible", &fdt_prop));
98 assert_string_equal("fixed-clock", (char *)fdt_prop.data);
99 assert_int_equal(0x6094, fdt_read_prop(*state, mnode_offset, "reg", &fdt_prop));
100 assert_int_equal(0x0, fdt_read_prop(*state, cnode_offset, "notfound", &fdt_prop));
103 static void test_fdt_read_reg_prop(void **state)
105 uint32_t addrcp1, sizecp1, addrcp2, sizecp2;
106 uint64_t node_offset1 = fdt_find_node_by_path(*state, "/memory@80000000", &addrcp1, &sizecp1);
107 uint64_t node_offset2 = fdt_find_node_by_path(*state, "/reserved-memory/ramoops@bfdf0000", &addrcp2, &sizecp2);
109 struct device_tree_region regions[3];
110 regions[0].addr = 0xDEADBEEF;
111 regions[0].size = 0xDEADBEEF;
112 fdt_read_reg_prop(*state, node_offset1, addrcp1, sizecp1, regions, 0);
113 assert_int_equal(0xDEADBEEF, regions[0].addr);
114 assert_int_equal(0xDEADBEEF, regions[0].size);
115 fdt_read_reg_prop(*state, node_offset2, addrcp2, sizecp2, regions, 1);
116 assert_int_equal(0xbfdf0000, regions[0].addr);
117 assert_int_equal(0x00010000, regions[0].size);
120 int main(void)
122 const struct CMUnitTest tests[] = {
123 cmocka_unit_test(test_fdt_find_node_by_path),
124 cmocka_unit_test(test_fdt_find_subnodes_by_prefix),
125 cmocka_unit_test(test_fdt_find_node_by_alias),
126 cmocka_unit_test(test_fdt_find_prop_in_node),
127 cmocka_unit_test(test_fdt_read_reg_prop),
130 return cb_run_group_tests(tests, setup_device_tree_test_group,
131 teardown_device_tree_test_group);