2018-04-24 04:07:11 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018, NXP
|
|
|
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 03:11:52 +08:00
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_wdog
|
|
|
|
|
2019-06-26 03:53:44 +08:00
|
|
|
#include <drivers/watchdog.h>
|
2019-06-26 03:53:47 +08:00
|
|
|
#include <drivers/clock_control.h>
|
2018-04-24 04:07:11 +08:00
|
|
|
#include <fsl_wdog.h>
|
|
|
|
|
2018-08-09 17:56:26 +08:00
|
|
|
#define LOG_LEVEL CONFIG_WDT_LOG_LEVEL
|
|
|
|
#include <logging/log.h>
|
2018-11-02 22:17:09 +08:00
|
|
|
LOG_MODULE_REGISTER(wdt_mcux_wdog);
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
#define MIN_TIMEOUT 4
|
|
|
|
|
|
|
|
struct mcux_wdog_config {
|
|
|
|
WDOG_Type *base;
|
|
|
|
char *clock_name;
|
|
|
|
clock_control_subsys_t clock_subsys;
|
|
|
|
void (*irq_config_func)(struct device *dev);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_wdog_data {
|
|
|
|
wdt_callback_t callback;
|
|
|
|
wdog_config_t wdog_config;
|
|
|
|
bool timeout_valid;
|
|
|
|
};
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int mcux_wdog_setup(struct device *dev, uint8_t options)
|
2018-04-24 04:07:11 +08:00
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
struct mcux_wdog_data *data = dev->driver_data;
|
|
|
|
WDOG_Type *base = config->base;
|
|
|
|
|
|
|
|
if (!data->timeout_valid) {
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_ERR("No valid timeouts installed");
|
2018-04-24 04:07:11 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->wdog_config.workMode.enableStop =
|
2019-03-27 09:57:45 +08:00
|
|
|
(options & WDT_OPT_PAUSE_IN_SLEEP) == 0U;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
data->wdog_config.workMode.enableDebug =
|
2019-03-27 09:57:45 +08:00
|
|
|
(options & WDT_OPT_PAUSE_HALTED_BY_DBG) == 0U;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
WDOG_Init(base, &data->wdog_config);
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_DBG("Setup the watchdog");
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcux_wdog_disable(struct device *dev)
|
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
struct mcux_wdog_data *data = dev->driver_data;
|
|
|
|
WDOG_Type *base = config->base;
|
|
|
|
|
|
|
|
WDOG_Deinit(base);
|
|
|
|
data->timeout_valid = false;
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_DBG("Disabled the watchdog");
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcux_wdog_install_timeout(struct device *dev,
|
|
|
|
const struct wdt_timeout_cfg *cfg)
|
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
struct mcux_wdog_data *data = dev->driver_data;
|
|
|
|
struct device *clock_dev;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t clock_freq;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
if (data->timeout_valid) {
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_ERR("No more timeouts can be installed");
|
2018-04-24 04:07:11 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_dev = device_get_binding(config->clock_name);
|
|
|
|
if (clock_dev == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clock_control_get_rate(clock_dev, config->clock_subsys,
|
|
|
|
&clock_freq)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WDOG_GetDefaultConfig(&data->wdog_config);
|
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
data->wdog_config.timeoutValue = clock_freq * cfg->window.max / 1000U;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
if (cfg->window.min) {
|
|
|
|
data->wdog_config.enableWindowMode = true;
|
|
|
|
data->wdog_config.windowValue =
|
2019-03-27 09:57:45 +08:00
|
|
|
clock_freq * cfg->window.min / 1000U;
|
2018-04-24 04:07:11 +08:00
|
|
|
} else {
|
|
|
|
data->wdog_config.enableWindowMode = false;
|
|
|
|
data->wdog_config.windowValue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((data->wdog_config.timeoutValue < MIN_TIMEOUT) ||
|
|
|
|
(data->wdog_config.timeoutValue < data->wdog_config.windowValue)) {
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_ERR("Invalid timeout");
|
2018-04-24 04:07:11 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->wdog_config.clockSource = kWDOG_LpoClockSource;
|
|
|
|
data->wdog_config.enableInterrupt = cfg->callback != NULL;
|
|
|
|
data->callback = cfg->callback;
|
|
|
|
data->timeout_valid = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcux_wdog_feed(struct device *dev, int channel_id)
|
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
WDOG_Type *base = config->base;
|
|
|
|
|
|
|
|
if (channel_id != 0) {
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_ERR("Invalid channel id");
|
2018-04-24 04:07:11 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WDOG_Refresh(base);
|
2018-08-09 17:56:26 +08:00
|
|
|
LOG_DBG("Fed the watchdog");
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcux_wdog_isr(void *arg)
|
|
|
|
{
|
|
|
|
struct device *dev = (struct device *)arg;
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
struct mcux_wdog_data *data = dev->driver_data;
|
|
|
|
WDOG_Type *base = config->base;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t flags;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
flags = WDOG_GetStatusFlags(base);
|
|
|
|
WDOG_ClearStatusFlags(base, flags);
|
|
|
|
|
|
|
|
if (data->callback) {
|
|
|
|
data->callback(dev, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcux_wdog_init(struct device *dev)
|
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
const struct mcux_wdog_config *config = dev->config_info;
|
2018-04-24 04:07:11 +08:00
|
|
|
|
|
|
|
config->irq_config_func(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wdt_driver_api mcux_wdog_api = {
|
|
|
|
.setup = mcux_wdog_setup,
|
|
|
|
.disable = mcux_wdog_disable,
|
|
|
|
.install_timeout = mcux_wdog_install_timeout,
|
|
|
|
.feed = mcux_wdog_feed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mcux_wdog_config_func_0(struct device *dev);
|
|
|
|
|
|
|
|
static const struct mcux_wdog_config mcux_wdog_config_0 = {
|
2020-03-25 03:11:52 +08:00
|
|
|
.base = (WDOG_Type *) DT_INST_REG_ADDR(0),
|
|
|
|
.clock_name = DT_INST_CLOCKS_LABEL(0),
|
2020-01-12 04:57:15 +08:00
|
|
|
.clock_subsys = (clock_control_subsys_t)
|
2020-03-25 03:11:52 +08:00
|
|
|
DT_INST_CLOCKS_CELL(0, name),
|
2018-04-24 04:07:11 +08:00
|
|
|
.irq_config_func = mcux_wdog_config_func_0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mcux_wdog_data mcux_wdog_data_0;
|
|
|
|
|
2020-03-25 03:11:52 +08:00
|
|
|
DEVICE_AND_API_INIT(mcux_wdog_0, DT_INST_LABEL(0),
|
2019-11-05 02:10:47 +08:00
|
|
|
&mcux_wdog_init,
|
2018-04-24 04:07:11 +08:00
|
|
|
&mcux_wdog_data_0, &mcux_wdog_config_0,
|
|
|
|
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
|
|
|
&mcux_wdog_api);
|
|
|
|
|
|
|
|
static void mcux_wdog_config_func_0(struct device *dev)
|
|
|
|
{
|
2020-03-25 03:11:52 +08:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
DT_INST_IRQ(0, priority),
|
2018-04-24 04:07:11 +08:00
|
|
|
mcux_wdog_isr, DEVICE_GET(mcux_wdog_0), 0);
|
|
|
|
|
2020-03-25 03:11:52 +08:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-04-24 04:07:11 +08:00
|
|
|
}
|