reset: Add generic reset driver
[platform/kernel/u-boot.git] / test / dm / syscon-reset.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <dm/test.h>
9 #include <regmap.h>
10 #include <reset.h>
11 #include <syscon.h>
12 #include <test/ut.h>
13 #include <asm/test.h>
14 #include <linux/bitops.h>
15
16 /* The following values must match the device tree */
17 #define TEST_RESET_REG 1
18 #define TEST_RESET_ASSERT_HIGH 0
19 #define TEST_RESET_ASSERT (TEST_RESET_ASSERT_HIGH ? (u32)-1 : (u32)0)
20 #define TEST_RESET_DEASSERT (~TEST_RESET_ASSERT)
21
22 #define TEST_RESET_VALID 15
23 #define TEST_RESET_NOMASK 30
24 #define TEST_RESET_OUTOFRANGE 60
25
26 static int dm_test_syscon_reset(struct unit_test_state *uts)
27 {
28         struct regmap *map;
29         struct reset_ctl rst;
30         struct udevice *reset;
31         struct udevice *syscon;
32         struct udevice *syscon_reset;
33         uint reg;
34
35         ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "syscon-reset-test",
36                                               &reset));
37         ut_assertok(uclass_get_device_by_name(UCLASS_SYSCON, "syscon@0",
38                                               &syscon));
39         ut_assertok(uclass_get_device_by_name(UCLASS_RESET, "syscon-reset",
40                                               &syscon_reset));
41         ut_assertok_ptr((map = syscon_get_regmap(syscon)));
42
43         ut_asserteq(-EINVAL, reset_get_by_name(reset, "no_mask", &rst));
44         ut_asserteq(-EINVAL, reset_get_by_name(reset, "out_of_range", &rst));
45         ut_assertok(reset_get_by_name(reset, "valid", &rst));
46
47         sandbox_set_enable_memio(true);
48         ut_assertok(regmap_write(map, TEST_RESET_REG, TEST_RESET_DEASSERT));
49         ut_assertok(reset_assert(&rst));
50         ut_assertok(regmap_read(map, TEST_RESET_REG, &reg));
51         ut_asserteq(TEST_RESET_DEASSERT ^ BIT(TEST_RESET_VALID), reg);
52
53         ut_assertok(reset_deassert(&rst));
54         ut_assertok(regmap_read(map, TEST_RESET_REG, &reg));
55         ut_asserteq(TEST_RESET_DEASSERT, reg);
56
57         return 0;
58 }
59 DM_TEST(dm_test_syscon_reset, DM_TESTF_SCAN_FDT);