Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | /*
* Copyright (C) 2017 Intel Deutschland GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief Watchdog (WDT) Driver for Atmel SAM MCUs
*
* Note:
* - Once the watchdog disable bit is set, it cannot be cleared till next
* power reset, i.e, the watchdog cannot be started once stopped.
* - Since the MCU boots with WDT enabled, the CONFIG_WDT_DISABLE_AT_BOOT
* is set default at boot and watchdog module is disabled in the MCU for
* systems that don't need watchdog functionality.
* - If the application needs to use the watchdog in the system, then
* CONFIG_WDT_DISABLE_AT_BOOT must be unset in the app's config file
*/
#include <watchdog.h>
#include <soc.h>
#define LOG_LEVEL CONFIG_WDT_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(wdt_sam);
#define SAM_PRESCALAR 128
#define WDT_MAX_VALUE 4095
/* Device constant configuration parameters */
struct wdt_sam_dev_cfg {
Wdt *regs;
};
static struct device DEVICE_NAME_GET(wdt_sam);
struct wdt_sam_dev_data {
wdt_callback_t cb;
u32_t mode;
bool timeout_valid;
bool mode_set;
};
static struct wdt_sam_dev_data wdt_sam_data = { 0 };
#define DEV_CFG(dev) \
((const struct wdt_sam_dev_cfg *const)(dev)->config->config_info)
static void wdt_sam_isr(struct device *dev)
{
u32_t wdt_sr;
Wdt *const wdt = DEV_CFG(dev)->regs;
struct wdt_sam_dev_data *data = dev->driver_data;
/* Clear status bit to acknowledge interrupt by dummy read. */
wdt_sr = wdt->WDT_SR;
data->cb(dev, 0);
}
/**
* @brief Calculates the watchdog counter value (WDV)
* to be installed in the watchdog timer
*
* @param timeout Timeout value in milliseconds.
* @param slow clock on board in Hz.
*/
int wdt_sam_convert_timeout(u32_t timeout, u32_t sclk)
{
u32_t max, min;
timeout = timeout * 1000U;
min = (SAM_PRESCALAR * 1000000) / sclk;
max = min * WDT_MAX_VALUE;
if ((timeout < min) || (timeout > max)) {
LOG_ERR("Invalid timeout value allowed range:"
"%d ms to %d ms", min / 1000U, max / 1000U);
return -EINVAL;
}
return WDT_MR_WDV(timeout / min);
}
static int wdt_sam_disable(struct device *dev)
{
Wdt *const wdt = DEV_CFG(dev)->regs;
struct wdt_sam_dev_data *data = dev->driver_data;
/* since Watchdog mode register is 'write-once', we can't disable if
* someone has already set the mode register
*/
if (data->mode_set) {
return -EPERM;
}
/* do we handle -EFAULT here */
/* Watchdog Mode register is 'write-once' only register.
* Once disabled, it cannot be enabled until the device is reset
*/
wdt->WDT_MR |= WDT_MR_WDDIS;
data->mode_set = true;
return 0;
}
static int wdt_sam_setup(struct device *dev, u8_t options)
{
Wdt *const wdt = DEV_CFG(dev)->regs;
struct wdt_sam_dev_data *data = dev->driver_data;
if (!data->timeout_valid) {
LOG_ERR("No valid timeouts installed");
return -EINVAL;
}
/* since Watchdog mode register is 'write-once', we can't set if
* someone has already set the mode register
*/
if (data->mode_set) {
return -EPERM;
}
if ((options & WDT_OPT_PAUSE_IN_SLEEP) == WDT_OPT_PAUSE_IN_SLEEP) {
data->mode |= WDT_MR_WDIDLEHLT;
}
if ((options & WDT_OPT_PAUSE_HALTED_BY_DBG) ==
WDT_OPT_PAUSE_HALTED_BY_DBG) {
data->mode |= WDT_MR_WDDBGHLT;
}
wdt->WDT_MR = data->mode;
data->mode_set = true;
return 0;
}
static int wdt_sam_install_timeout(struct device *dev,
const struct wdt_timeout_cfg *cfg)
{
u32_t wdt_mode = 0U;
int timeout_value;
struct wdt_sam_dev_data *data = dev->driver_data;
if (data->timeout_valid) {
LOG_ERR("No more timeouts can be installed");
return -ENOMEM;
}
if (cfg->window.min != 0U) {
return -EINVAL;
}
/*
* Convert time to cycles. SAM3X SoC doesn't supports window
* timeout config. So the api expects the timeout to be filled
* in the max field of the timeout config.
*/
timeout_value = wdt_sam_convert_timeout(cfg->window.max,
(u32_t) CHIP_FREQ_XTAL_32K);
if (timeout_value < 0) {
return -EINVAL;
}
switch (cfg->flags) {
case WDT_FLAG_RESET_SOC:
/*A Watchdog fault (underflow or error) activates all resets */
wdt_mode = WDT_MR_WDRSTEN; /* WDT reset enable */
break;
case WDT_FLAG_RESET_NONE:
/* A Watchdog fault (underflow or error) asserts interrupt. */
if (cfg->callback) {
wdt_mode = WDT_MR_WDFIEN; /* WDT fault interrupt. */
data->cb = cfg->callback;
} else {
LOG_ERR("Invalid(NULL) ISR callback passed\n");
return -EINVAL;
}
break;
/* Processor only reset mode not available in same70 series */
#ifdef WDT_MR_WDRPROC
case WDT_FLAG_RESET_CPU_CORE:
/*A Watchdog fault activates the processor reset*/
LOG_DBG("Configuring reset CPU only mode\n");
wdt_mode = WDT_MR_WDRSTEN | /* WDT reset enable */
WDT_MR_WDRPROC; /* WDT reset processor only*/
break;
#endif
default:
LOG_ERR("Unsupported watchdog config Flag\n");
return -ENOTSUP;
}
data->mode = wdt_mode |
WDT_MR_WDV(timeout_value) |
WDT_MR_WDD(timeout_value);
data->timeout_valid = true;
return 0;
}
static int wdt_sam_feed(struct device *dev, int channel_id)
{
/*
* On watchdog restart the Watchdog counter is immediately
* reloaded/feeded with the 12-bit watchdog counter
* value from WDT_MR and restarted
*/
Wdt *const wdt = DEV_CFG(dev)->regs;
wdt->WDT_CR |= WDT_CR_KEY_PASSWD | WDT_CR_WDRSTT;
return 0;
}
static const struct wdt_driver_api wdt_sam_api = {
.setup = wdt_sam_setup,
.disable = wdt_sam_disable,
.install_timeout = wdt_sam_install_timeout,
.feed = wdt_sam_feed,
};
static const struct wdt_sam_dev_cfg wdt_sam_cfg = {
.regs = (Wdt *)DT_WDT_SAM_BASE_ADDRESS,
};
static void wdt_sam_irq_config(void)
{
IRQ_CONNECT(DT_WDT_SAM_IRQ,
DT_WDT_SAM_IRQ_PRIORITY, wdt_sam_isr,
DEVICE_GET(wdt_sam), 0);
irq_enable(DT_WDT_SAM_IRQ);
}
static int wdt_sam_init(struct device *dev)
{
#ifdef CONFIG_WDT_DISABLE_AT_BOOT
wdt_sam_disable(dev);
#endif
wdt_sam_irq_config();
return 0;
}
DEVICE_AND_API_INIT(wdt_sam, DT_WDT_SAM_LABEL, wdt_sam_init,
&wdt_sam_data, &wdt_sam_cfg, PRE_KERNEL_1,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &wdt_sam_api);
|