Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 Google, Inc |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <wdt.h> |
Rasmus Villemoes | a9346b9 | 2021-08-19 11:57:05 +0200 | [diff] [blame] | 9 | #include <asm/gpio.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
| 11 | #include <asm/test.h> |
| 12 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <test/test.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 14 | #include <test/ut.h> |
Rasmus Villemoes | 4171c57 | 2021-08-19 11:57:06 +0200 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <watchdog.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 17 | |
| 18 | /* Test that watchdog driver functions are called */ |
| 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 | |
Rasmus Villemoes | a9346b9 | 2021-08-19 11:57:05 +0200 | [diff] [blame] | 25 | ut_assertok(uclass_get_device_by_driver(UCLASS_WDT, |
| 26 | DM_DRIVER_GET(wdt_sandbox), &dev)); |
Simon Glass | 9eace7f | 2017-06-07 10:28:43 -0600 | [diff] [blame] | 27 | ut_assertnonnull(dev); |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 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 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 45 | DM_TEST(dm_test_wdt_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Rasmus Villemoes | a9346b9 | 2021-08-19 11:57:05 +0200 | [diff] [blame] | 46 | |
| 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); |
Rasmus Villemoes | 4171c57 | 2021-08-19 11:57:06 +0200 | [diff] [blame] | 78 | |
| 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); |