blob: ee615f0e1487b86098313c0483ed288e7a85a0c7 [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
47static 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}
77DM_TEST(dm_test_wdt_gpio, UT_TESTF_SCAN_FDT);
Rasmus Villemoesf91ff5a2021-08-19 11:57:06 +020078
79static 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}
129DM_TEST(dm_test_wdt_watchdog_reset, UT_TESTF_SCAN_FDT);