1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2017 Google, Inc
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <wdt.h>
9 #include <asm/gpio.h>
10 #include <asm/state.h>
11 #include <asm/test.h>
12 #include <dm/test.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15 #include <linux/delay.h>
16 #include <watchdog.h>
17
18 /* Test that watchdog driver functions are called */
dm_test_wdt_base(struct unit_test_state * uts)19 static int dm_test_wdt_base(struct unit_test_state *uts)
20 {
21 struct sandbox_state *state = state_get_current();
22 struct udevice *dev;
23 const u64 timeout = 42;
24
25 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
26 DM_DRIVER_GET(wdt_sandbox), &dev));
27 ut_assertnonnull(dev);
28 ut_asserteq(0, state->wdt.counter);
29 ut_asserteq(false, state->wdt.running);
30
31 ut_assertok(wdt_start(dev, timeout, 0));
32 ut_asserteq(timeout, state->wdt.counter);
33 ut_asserteq(true, state->wdt.running);
34
35 uint reset_count = state->wdt.reset_count;
36 ut_assertok(wdt_reset(dev));
37 ut_asserteq(reset_count + 1, state->wdt.reset_count);
38 ut_asserteq(true, state->wdt.running);
39
40 ut_assertok(wdt_stop(dev));
41 ut_asserteq(false, state->wdt.running);
42
43 return 0;
44 }
45 DM_TEST(dm_test_wdt_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
46
dm_test_wdt_gpio(struct unit_test_state * uts)47 static int dm_test_wdt_gpio(struct unit_test_state *uts)
48 {
49 /*
50 * The sandbox wdt gpio is "connected" to gpio bank a, offset
51 * 7. Use the sandbox back door to verify that the gpio-wdt
52 * driver behaves as expected.
53 */
54 struct udevice *wdt, *gpio;
55 const u64 timeout = 42;
56 const int offset = 7;
57 int val;
58
59 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
60 DM_DRIVER_GET(wdt_gpio), &wdt));
61 ut_assertnonnull(wdt);
62
63 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
64 ut_assertnonnull(gpio);
65 ut_assertok(wdt_start(wdt, timeout, 0));
66
67 val = sandbox_gpio_get_value(gpio, offset);
68 ut_assertok(wdt_reset(wdt));
69 ut_asserteq(!val, sandbox_gpio_get_value(gpio, offset));
70 ut_assertok(wdt_reset(wdt));
71 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
72
73 ut_asserteq(-ENOSYS, wdt_stop(wdt));
74
75 return 0;
76 }
77 DM_TEST(dm_test_wdt_gpio, UT_TESTF_SCAN_FDT);
78
dm_test_wdt_watchdog_reset(struct unit_test_state * uts)79 static int dm_test_wdt_watchdog_reset(struct unit_test_state *uts)
80 {
81 struct sandbox_state *state = state_get_current();
82 struct udevice *gpio_wdt, *sandbox_wdt;
83 struct udevice *gpio;
84 const u64 timeout = 42;
85 const int offset = 7;
86 uint reset_count;
87 int val;
88
89 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
90 DM_DRIVER_GET(wdt_gpio), &gpio_wdt));
91 ut_assertnonnull(gpio_wdt);
92 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
93 DM_DRIVER_GET(wdt_sandbox), &sandbox_wdt));
94 ut_assertnonnull(sandbox_wdt);
95 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
96 ut_assertnonnull(gpio);
97
98 /* Neither device should be "started", so watchdog_reset() should be a no-op. */
99 reset_count = state->wdt.reset_count;
100 val = sandbox_gpio_get_value(gpio, offset);
101 watchdog_reset();
102 ut_asserteq(reset_count, state->wdt.reset_count);
103 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
104
105 /* Start both devices. */
106 ut_assertok(wdt_start(gpio_wdt, timeout, 0));
107 ut_assertok(wdt_start(sandbox_wdt, timeout, 0));
108
109 /* Make sure both devices have just been pinged. */
110 timer_test_add_offset(100);
111 watchdog_reset();
112 reset_count = state->wdt.reset_count;
113 val = sandbox_gpio_get_value(gpio, offset);
114
115 /* The gpio watchdog should be pinged, the sandbox one not. */
116 timer_test_add_offset(30);
117 watchdog_reset();
118 ut_asserteq(reset_count, state->wdt.reset_count);
119 ut_asserteq(!val, sandbox_gpio_get_value(gpio, offset));
120
121 /* After another ~30ms, both devices should get pinged. */
122 timer_test_add_offset(30);
123 watchdog_reset();
124 ut_asserteq(reset_count + 1, state->wdt.reset_count);
125 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
126
127 return 0;
128 }
129 DM_TEST(dm_test_wdt_watchdog_reset, UT_TESTF_SCAN_FDT);
130