1 /* Copyright 2021 Google LLC
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/device.h>
6 #include <zephyr/devicetree.h>
7 #include <zephyr/drivers/syscon.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/linker/devicetree_regions.h>
10 
11 #define RES_SECT LINKER_DT_NODE_REGION_NAME(DT_NODELABEL(res))
12 
13 uint8_t var_in_res0[DT_REG_SIZE(DT_NODELABEL(syscon))] __attribute((__section__(RES_SECT)));
14 
ZTEST(syscon,test_size)15 ZTEST(syscon, test_size)
16 {
17 	const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon));
18 	const size_t expected_size = DT_REG_SIZE(DT_NODELABEL(syscon));
19 	size_t size;
20 
21 	zassert_not_null(dev, NULL);
22 	zassert_ok(syscon_get_size(dev, &size));
23 	zassert_equal(size, expected_size, "size(0x%x) != expected_size(0x%x)", size,
24 		      expected_size);
25 }
26 
ZTEST(syscon,test_out_of_bounds)27 ZTEST(syscon, test_out_of_bounds)
28 {
29 	const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon));
30 	uint32_t val;
31 
32 	zassert_equal(syscon_read_reg(dev, DT_REG_SIZE(DT_NODELABEL(syscon)), &val), -EINVAL);
33 	zassert_equal(syscon_write_reg(dev, DT_REG_SIZE(DT_NODELABEL(syscon)), val), -EINVAL);
34 }
35 
ZTEST(syscon,test_read)36 ZTEST(syscon, test_read)
37 {
38 	const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon));
39 	uintptr_t base_addr;
40 	uint32_t val;
41 
42 	zassert_ok(syscon_get_base(dev, &base_addr));
43 	for (size_t i = 0; i < ARRAY_SIZE(var_in_res0); ++i) {
44 		((uint8_t *)base_addr)[i] = i;
45 		zassert_ok(syscon_read_reg(dev, i, &val));
46 		zassert_equal(i, val);
47 	}
48 }
49 
ZTEST(syscon,test_write)50 ZTEST(syscon, test_write)
51 {
52 	const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon));
53 	uintptr_t base_addr;
54 
55 	zassert_ok(syscon_get_base(dev, &base_addr));
56 	for (uint32_t i = 0; i < ARRAY_SIZE(var_in_res0); ++i) {
57 		zassert_ok(syscon_write_reg(dev, i, i));
58 		zassert_equal(((uint8_t *)base_addr)[i], i);
59 	}
60 }
61 
62 ZTEST_SUITE(syscon, NULL, NULL, NULL, NULL, NULL);
63