1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
11 #include <asm/state.h>
14 #include <test/test.h>
16 #include <linux/delay.h>
19 /* Test that watchdog driver functions are called */
20 static int dm_test_wdt_base(struct unit_test_state *uts)
22 struct sandbox_state *state = state_get_current();
24 const u64 timeout = 42;
26 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
27 DM_DRIVER_GET(wdt_sandbox), &dev));
28 ut_assertnonnull(dev);
29 ut_asserteq(0, state->wdt.counter);
30 ut_asserteq(false, state->wdt.running);
32 ut_assertok(wdt_start(dev, timeout, 0));
33 ut_asserteq(timeout, state->wdt.counter);
34 ut_asserteq(true, state->wdt.running);
36 uint reset_count = state->wdt.reset_count;
37 ut_assertok(wdt_reset(dev));
38 ut_asserteq(reset_count + 1, state->wdt.reset_count);
39 ut_asserteq(true, state->wdt.running);
41 ut_assertok(wdt_stop(dev));
42 ut_asserteq(false, state->wdt.running);
46 DM_TEST(dm_test_wdt_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
48 static int dm_test_wdt_gpio_toggle(struct unit_test_state *uts)
51 * The sandbox wdt gpio is "connected" to gpio bank a, offset
52 * 7. Use the sandbox back door to verify that the gpio-wdt
53 * driver behaves as expected when using the 'toggle' algorithm.
55 struct udevice *wdt, *gpio;
56 const u64 timeout = 42;
60 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
61 "wdt-gpio-toggle", &wdt));
62 ut_assertnonnull(wdt);
64 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
65 ut_assertnonnull(gpio);
66 ut_assertok(wdt_start(wdt, timeout, 0));
68 val = sandbox_gpio_get_value(gpio, offset);
69 ut_assertok(wdt_reset(wdt));
70 ut_asserteq(!val, sandbox_gpio_get_value(gpio, offset));
71 ut_assertok(wdt_reset(wdt));
72 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
74 ut_asserteq(-ENOSYS, wdt_stop(wdt));
78 DM_TEST(dm_test_wdt_gpio_toggle, UT_TESTF_SCAN_FDT);
80 static int dm_test_wdt_gpio_level(struct unit_test_state *uts)
83 * The sandbox wdt gpio is "connected" to gpio bank a, offset
84 * 7. Use the sandbox back door to verify that the gpio-wdt
85 * driver behaves as expected when using the 'level' algorithm.
87 struct udevice *wdt, *gpio;
88 const u64 timeout = 42;
92 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
93 "wdt-gpio-level", &wdt));
94 ut_assertnonnull(wdt);
96 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
97 ut_assertnonnull(gpio);
98 ut_assertok(wdt_start(wdt, timeout, 0));
100 val = sandbox_gpio_get_value(gpio, offset);
101 ut_assertok(wdt_reset(wdt));
102 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
103 ut_assertok(wdt_reset(wdt));
104 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
106 ut_asserteq(-ENOSYS, wdt_stop(wdt));
110 DM_TEST(dm_test_wdt_gpio_level, UT_TESTF_SCAN_FDT);
112 static int dm_test_wdt_watchdog_reset(struct unit_test_state *uts)
114 struct sandbox_state *state = state_get_current();
115 struct udevice *gpio_wdt, *sandbox_wdt;
116 struct udevice *gpio;
117 const u64 timeout = 42;
118 const int offset = 8;
122 ut_assertok(uclass_get_device_by_name(UCLASS_WDT,
123 "wdt-gpio-toggle", &gpio_wdt));
124 ut_assertnonnull(gpio_wdt);
125 ut_assertok(uclass_get_device_by_driver(UCLASS_WDT,
126 DM_DRIVER_GET(wdt_sandbox), &sandbox_wdt));
127 ut_assertnonnull(sandbox_wdt);
128 ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
129 ut_assertnonnull(gpio);
131 /* Neither device should be "started", so watchdog_reset() should be a no-op. */
132 reset_count = state->wdt.reset_count;
133 val = sandbox_gpio_get_value(gpio, offset);
135 ut_asserteq(reset_count, state->wdt.reset_count);
136 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
138 /* Start both devices. */
139 ut_assertok(wdt_start(gpio_wdt, timeout, 0));
140 ut_assertok(wdt_start(sandbox_wdt, timeout, 0));
142 /* Make sure both devices have just been pinged. */
143 timer_test_add_offset(100);
145 reset_count = state->wdt.reset_count;
146 val = sandbox_gpio_get_value(gpio, offset);
148 /* The gpio watchdog should be pinged, the sandbox one not. */
149 timer_test_add_offset(30);
151 ut_asserteq(reset_count, state->wdt.reset_count);
152 ut_asserteq(!val, sandbox_gpio_get_value(gpio, offset));
154 /* After another ~30ms, both devices should get pinged. */
155 timer_test_add_offset(30);
157 ut_asserteq(reset_count + 1, state->wdt.reset_count);
158 ut_asserteq(val, sandbox_gpio_get_value(gpio, offset));
162 DM_TEST(dm_test_wdt_watchdog_reset, UT_TESTF_SCAN_FDT);