blob: 535f00a874032393d5dd3bc6531f427605efbc95 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.comdaea6d42017-04-17 12:00:21 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.comdaea6d42017-04-17 12:00:21 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <wdt.h>
Rasmus Villemoes2b673872021-08-19 11:57:05 +02009#include <asm/gpio.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070010#include <asm/state.h>
11#include <asm/test.h>
12#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060013#include <test/test.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070014#include <test/ut.h>
Rasmus Villemoesf91ff5a2021-08-19 11:57:06 +020015#include <linux/delay.h>
16#include <watchdog.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070017
18/* Test that watchdog driver functions are called */
19static 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
Rasmus Villemoes2b673872021-08-19 11:57:05 +020025 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
26 DM_DRIVER_GET(wdt_sandbox), &dev));
Simon Glass2e8faa22017-06-07 10:28:43 -060027 ut_assertnonnull(dev);
maxims@google.comdaea6d42017-04-17 12:00:21 -070028 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}
Simon Glass974dccd2020-07-28 19:41:12 -060045DM_TEST(dm_test_wdt_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Rasmus Villemoes2b673872021-08-19 11:57:05 +020046
Paul Doelle709f0372022-07-04 09:00:25 +000047static int dm_test_wdt_gpio_toggle(struct unit_test_state *uts)
Rasmus Villemoes2b673872021-08-19 11:57:05 +020048{
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
Paul Doelle709f0372022-07-04 09:00:25 +000052 * driver behaves as expected when using the 'toggle' algorithm.
Rasmus Villemoes2b673872021-08-19 11:57:05 +020053 */
54 struct udevice *wdt, *gpio;
55 const u64 timeout = 42;
56 const int offset = 7;
57 int val;
58
Paul Doelle709f0372022-07-04 09:00:25 +000059 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
60 "wdt-gpio-toggle", &wdt));
Rasmus Villemoes2b673872021-08-19 11:57:05 +020061 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}
Paul Doelle709f0372022-07-04 09:00:25 +000077DM_TEST(dm_test_wdt_gpio_toggle, UT_TESTF_SCAN_FDT);
78
79static int dm_test_wdt_gpio_level(struct unit_test_state *uts)
80{
81 /*
82 * The sandbox wdt gpio is "connected" to gpio bank a, offset
83 * 7. Use the sandbox back door to verify that the gpio-wdt
84 * driver behaves as expected when using the 'level' algorithm.
85 */
86 struct udevice *wdt, *gpio;
87 const u64 timeout = 42;
88 const int offset = 7;
89 int val;
90
91 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
92 "wdt-gpio-level", &wdt));
93 ut_assertnonnull(wdt);
94
95 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
96 ut_assertnonnull(gpio);
97 ut_assertok(wdt_start(wdt, timeout, 0));
98
99 val = sandbox_gpio_get_value(gpio, offset);
100 ut_assertok(wdt_reset(wdt));
101 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
102 ut_assertok(wdt_reset(wdt));
103 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
104
105 ut_asserteq(-ENOSYS, wdt_stop(wdt));
106
107 return 0;
108}
109DM_TEST(dm_test_wdt_gpio_level, UT_TESTF_SCAN_FDT);
Rasmus Villemoesf91ff5a2021-08-19 11:57:06 +0200110
111static int dm_test_wdt_watchdog_reset(struct unit_test_state *uts)
112{
113 struct sandbox_state *state = state_get_current();
114 struct udevice *gpio_wdt, *sandbox_wdt;
115 struct udevice *gpio;
116 const u64 timeout = 42;
117 const int offset = 7;
118 uint reset_count;
119 int val;
120
Paul Doelle709f0372022-07-04 09:00:25 +0000121 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
122 "wdt-gpio-toggle", &gpio_wdt));
Rasmus Villemoesf91ff5a2021-08-19 11:57:06 +0200123 ut_assertnonnull(gpio_wdt);
124 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
125 DM_DRIVER_GET(wdt_sandbox), &sandbox_wdt));
126 ut_assertnonnull(sandbox_wdt);
127 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
128 ut_assertnonnull(gpio);
129
130 /* Neither device should be "started", so watchdog_reset() should be a no-op. */
131 reset_count = state->wdt.reset_count;
132 val = sandbox_gpio_get_value(gpio, offset);
133 watchdog_reset();
134 ut_asserteq(reset_count, state->wdt.reset_count);
135 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
136
137 /* Start both devices. */
138 ut_assertok(wdt_start(gpio_wdt, timeout, 0));
139 ut_assertok(wdt_start(sandbox_wdt, timeout, 0));
140
141 /* Make sure both devices have just been pinged. */
142 timer_test_add_offset(100);
143 watchdog_reset();
144 reset_count = state->wdt.reset_count;
145 val = sandbox_gpio_get_value(gpio, offset);
146
147 /* The gpio watchdog should be pinged, the sandbox one not. */
148 timer_test_add_offset(30);
149 watchdog_reset();
150 ut_asserteq(reset_count, state->wdt.reset_count);
151 ut_asserteq(!val, sandbox_gpio_get_value(gpio, offset));
152
153 /* After another ~30ms, both devices should get pinged. */
154 timer_test_add_offset(30);
155 watchdog_reset();
156 ut_asserteq(reset_count + 1, state->wdt.reset_count);
157 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
158
159 return 0;
160}
161DM_TEST(dm_test_wdt_watchdog_reset, UT_TESTF_SCAN_FDT);