Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _WDT_H_ |
| 8 | #define _WDT_H_ |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 9 | |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 10 | #include <zephyr/types.h> |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 11 | #include <device.h> |
| 12 | #include <misc/util.h> |
Anas Nashif | 2318f02 | 2015-11-29 12:35:59 -0500 | [diff] [blame] | 13 | |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
Anas Nashif | 2318f02 | 2015-11-29 12:35:59 -0500 | [diff] [blame] | 18 | #define WDT_MODE (BIT(1)) |
| 19 | #define WDT_MODE_OFFSET (1) |
| 20 | #define WDT_TIMEOUT_MASK (0xF) |
| 21 | |
| 22 | enum wdt_mode { |
| 23 | WDT_MODE_RESET = 0, |
| 24 | WDT_MODE_INTERRUPT_RESET |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * WDT clock cycles for timeout type. |
| 29 | */ |
| 30 | enum wdt_clock_timeout_cycles { |
| 31 | WDT_2_16_CYCLES, |
| 32 | WDT_2_17_CYCLES, |
| 33 | WDT_2_18_CYCLES, |
| 34 | WDT_2_19_CYCLES, |
| 35 | WDT_2_20_CYCLES, |
| 36 | WDT_2_21_CYCLES, |
| 37 | WDT_2_22_CYCLES, |
| 38 | WDT_2_23_CYCLES, |
| 39 | WDT_2_24_CYCLES, |
| 40 | WDT_2_25_CYCLES, |
| 41 | WDT_2_26_CYCLES, |
| 42 | WDT_2_27_CYCLES, |
| 43 | WDT_2_28_CYCLES, |
| 44 | WDT_2_29_CYCLES, |
| 45 | WDT_2_30_CYCLES, |
| 46 | WDT_2_31_CYCLES |
| 47 | }; |
| 48 | |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * WDT configuration struct. |
| 52 | */ |
| 53 | struct wdt_config { |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 54 | u32_t timeout; |
Anas Nashif | 2318f02 | 2015-11-29 12:35:59 -0500 | [diff] [blame] | 55 | enum wdt_mode mode; |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 56 | void (*interrupt_fn)(struct device *dev); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 59 | typedef void (*wdt_api_enable)(struct device *dev); |
| 60 | typedef void (*wdt_api_disable)(struct device *dev); |
| 61 | typedef int (*wdt_api_set_config)(struct device *dev, |
| 62 | struct wdt_config *config); |
| 63 | typedef void (*wdt_api_get_config)(struct device *dev, |
| 64 | struct wdt_config *config); |
| 65 | typedef void (*wdt_api_reload)(struct device *dev); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 66 | |
| 67 | struct wdt_driver_api { |
| 68 | wdt_api_enable enable; |
| 69 | wdt_api_disable disable; |
| 70 | wdt_api_get_config get_config; |
| 71 | wdt_api_set_config set_config; |
| 72 | wdt_api_reload reload; |
| 73 | }; |
| 74 | |
| 75 | static inline void wdt_enable(struct device *dev) |
| 76 | { |
Michael Hope | 5f67a61 | 2018-03-17 12:44:40 +0100 | [diff] [blame] | 77 | const struct wdt_driver_api *api = |
| 78 | (const struct wdt_driver_api *)dev->driver_api; |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 79 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 80 | api->enable(dev); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static inline void wdt_disable(struct device *dev) |
| 84 | { |
Michael Hope | 5f67a61 | 2018-03-17 12:44:40 +0100 | [diff] [blame] | 85 | const struct wdt_driver_api *api = |
| 86 | (const struct wdt_driver_api *)dev->driver_api; |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 87 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 88 | api->disable(dev); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 91 | static inline void wdt_get_config(struct device *dev, |
| 92 | struct wdt_config *config) |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 93 | { |
Michael Hope | 5f67a61 | 2018-03-17 12:44:40 +0100 | [diff] [blame] | 94 | const struct wdt_driver_api *api = |
| 95 | (const struct wdt_driver_api *)dev->driver_api; |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 96 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 97 | api->get_config(dev, config); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 100 | static inline int wdt_set_config(struct device *dev, |
| 101 | struct wdt_config *config) |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 102 | { |
Michael Hope | 5f67a61 | 2018-03-17 12:44:40 +0100 | [diff] [blame] | 103 | const struct wdt_driver_api *api = |
| 104 | (const struct wdt_driver_api *)dev->driver_api; |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 105 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 106 | return api->set_config(dev, config); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline void wdt_reload(struct device *dev) |
| 110 | { |
Michael Hope | 5f67a61 | 2018-03-17 12:44:40 +0100 | [diff] [blame] | 111 | const struct wdt_driver_api *api = |
| 112 | (const struct wdt_driver_api *)dev->driver_api; |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 113 | |
Tomasz Bursztyka | e8c4335 | 2015-12-03 09:43:26 +0100 | [diff] [blame] | 114 | api->reload(dev); |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |
| 120 | |
Anas Nashif | a9ed4ba | 2015-11-21 21:07:43 -0500 | [diff] [blame] | 121 | #endif |