1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright 2019 Broadcom. 4 */ 5 6 #ifndef DRIVERS_WDT_H 7 #define DRIVERS_WDT_H 8 9 #include <assert.h> 10 #include <kernel/interrupt.h> 11 #include <tee_api_types.h> 12 13 struct wdt_chip { 14 const struct wdt_ops *ops; 15 struct itr_handler *wdt_itr; 16 }; 17 18 /* 19 * struct wdt_ops - The watchdog device operations 20 * 21 * @init: The routine to initialized the watchdog resources. 22 * @start: The routine for starting the watchdog device. 23 * @stop: The routine for stopping the watchdog device. 24 * @ping: The routine that sends a keepalive ping to the watchdog device. 25 * @set_timeout:The routine that finds the load value that will reset system in 26 * required timeout (in seconds). 27 * 28 * The wdt_ops structure contains a list of low-level operations 29 * that control a watchdog device. 30 */ 31 struct wdt_ops { 32 TEE_Result (*init)(struct wdt_chip *chip, unsigned long *min_timeout, 33 unsigned long *max_timeout); 34 void (*start)(struct wdt_chip *chip); 35 void (*stop)(struct wdt_chip *chip); 36 void (*ping)(struct wdt_chip *chip); 37 TEE_Result (*set_timeout)(struct wdt_chip *chip, unsigned long timeout); 38 }; 39 40 #ifdef CFG_WDT 41 extern struct wdt_chip *wdt_chip; 42 43 /* Register a watchdog as the system watchdog */ 44 TEE_Result watchdog_register(struct wdt_chip *chip); 45 46 static inline 47 TEE_Result watchdog_init(unsigned long *min_timeout, unsigned long *max_timeout) 48 { 49 if (!wdt_chip) 50 return TEE_ERROR_NOT_SUPPORTED; 51 52 if (!wdt_chip->ops->init) 53 return TEE_SUCCESS; 54 55 return wdt_chip->ops->init(wdt_chip, min_timeout, max_timeout); 56 } 57 58 static inline void watchdog_start(void) 59 { 60 if (wdt_chip) 61 wdt_chip->ops->start(wdt_chip); 62 } 63 64 static inline void watchdog_stop(void) 65 { 66 if (wdt_chip && wdt_chip->ops->stop) 67 wdt_chip->ops->stop(wdt_chip); 68 } 69 70 static inline void watchdog_ping(void) 71 { 72 if (wdt_chip) 73 wdt_chip->ops->ping(wdt_chip); 74 } 75 76 static inline void watchdog_settimeout(unsigned long timeout) 77 { 78 if (wdt_chip) 79 wdt_chip->ops->set_timeout(wdt_chip, timeout); 80 } 81 #else 82 static inline TEE_Result watchdog_register(struct wdt_chip *chip __unused) 83 { 84 return TEE_ERROR_NOT_SUPPORTED; 85 } 86 87 static inline TEE_Result watchdog_init(unsigned long *min_timeout __unused, 88 unsigned long *max_timeout __unused) 89 { 90 return TEE_ERROR_NOT_SUPPORTED; 91 } 92 93 static inline void watchdog_start(void) {} 94 static inline void watchdog_stop(void) {} 95 static inline void watchdog_ping(void) {} 96 static inline void watchdog_settimeout(unsigned long timeout __unused) {} 97 #endif 98 99 #endif /* DRIVERS_WDT_H */ 100