1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT nordic_npm1300_wdt
7
8 #include <errno.h>
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/watchdog.h>
13 #include <zephyr/drivers/mfd/npm1300.h>
14 #include <zephyr/dt-bindings/gpio/nordic-npm1300-gpio.h>
15
16 /* nPM1300 TIMER base address */
17 #define TIME_BASE 0x07U
18
19 /* nPM1300 timer register offsets */
20 #define TIME_OFFSET_START 0x00U
21 #define TIME_OFFSET_STOP 0x01U
22 #define TIME_OFFSET_WDOG_KICK 0x04U
23 #define TIME_OFFSET_MODE 0x05U
24
25 /* nPM1300 timer modes */
26 #define TIME_MODE_BOOT 0x00U
27 #define TIME_MODE_WARN 0x01U
28 #define TIME_MODE_RESET 0x02U
29 #define TIME_MODE_GEN 0x03U
30
31 struct wdt_npm1300_config {
32 const struct device *mfd;
33 struct gpio_dt_spec reset_gpios;
34 };
35
36 struct wdt_npm1300_data {
37 bool timeout_valid;
38 };
39
wdt_npm1300_setup(const struct device * dev,uint8_t options)40 static int wdt_npm1300_setup(const struct device *dev, uint8_t options)
41 {
42 const struct wdt_npm1300_config *config = dev->config;
43 struct wdt_npm1300_data *data = dev->data;
44
45 if (!data->timeout_valid) {
46 return -EINVAL;
47 }
48
49 return mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_START, 1U);
50 }
51
wdt_npm1300_disable(const struct device * dev)52 static int wdt_npm1300_disable(const struct device *dev)
53 {
54 const struct wdt_npm1300_config *config = dev->config;
55 struct wdt_npm1300_data *data = dev->data;
56 int ret;
57
58 ret = mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_STOP, 1U);
59 if (ret < 0) {
60 return ret;
61 }
62
63 data->timeout_valid = false;
64
65 return 0;
66 }
67
wdt_npm1300_install_timeout(const struct device * dev,const struct wdt_timeout_cfg * timeout)68 static int wdt_npm1300_install_timeout(const struct device *dev,
69 const struct wdt_timeout_cfg *timeout)
70 {
71 const struct wdt_npm1300_config *config = dev->config;
72 struct wdt_npm1300_data *data = dev->data;
73 uint8_t mode;
74 int ret;
75
76 if (data->timeout_valid) {
77 return -ENOMEM;
78 }
79
80 if (timeout->window.min != 0U) {
81 return -EINVAL;
82 }
83
84 ret = mfd_npm1300_set_timer(config->mfd, timeout->window.max);
85 if (ret < 0) {
86 return ret;
87 }
88
89 switch (timeout->flags & WDT_FLAG_RESET_MASK) {
90 case WDT_FLAG_RESET_NONE:
91 /* Watchdog expiry causes warn event only, and does not reset */
92 mode = TIME_MODE_GEN;
93 break;
94 case WDT_FLAG_RESET_CPU_CORE:
95 /* Watchdog expiry causes warn event, then asserts reset output */
96 mode = TIME_MODE_WARN;
97 break;
98 case WDT_FLAG_RESET_SOC:
99 /* Watchdog expiry causes warn event, then full power cycle */
100 mode = TIME_MODE_RESET;
101 break;
102 default:
103 return -EINVAL;
104 }
105
106 ret = mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_MODE, mode);
107 if (ret < 0) {
108 return ret;
109 }
110
111 data->timeout_valid = true;
112
113 return 0;
114 }
115
wdt_npm1300_feed(const struct device * dev,int channel_id)116 static int wdt_npm1300_feed(const struct device *dev, int channel_id)
117 {
118 const struct wdt_npm1300_config *config = dev->config;
119
120 if (channel_id != 0) {
121 return -EINVAL;
122 }
123
124 return mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_WDOG_KICK, 1U);
125 }
126
127 static DEVICE_API(wdt, wdt_npm1300_api) = {
128 .setup = wdt_npm1300_setup,
129 .disable = wdt_npm1300_disable,
130 .install_timeout = wdt_npm1300_install_timeout,
131 .feed = wdt_npm1300_feed,
132 };
133
wdt_npm1300_init(const struct device * dev)134 static int wdt_npm1300_init(const struct device *dev)
135 {
136 const struct wdt_npm1300_config *config = dev->config;
137 int ret;
138
139 if (!device_is_ready(config->mfd)) {
140 return -ENODEV;
141 }
142
143 if (config->reset_gpios.port != NULL) {
144 if (!gpio_is_ready_dt(&config->reset_gpios)) {
145 return -ENODEV;
146 }
147
148 ret = gpio_pin_configure_dt(&config->reset_gpios, NPM1300_GPIO_WDT_RESET_ON);
149 if (ret != 0) {
150 return ret;
151 }
152 }
153
154 return 0;
155 }
156
157 #define WDT_NPM1300_DEFINE(n) \
158 static struct wdt_npm1300_data data##n; \
159 \
160 static const struct wdt_npm1300_config config##n = { \
161 .mfd = DEVICE_DT_GET(DT_INST_PARENT(n)), \
162 .reset_gpios = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \
163 }; \
164 \
165 DEVICE_DT_INST_DEFINE(n, &wdt_npm1300_init, NULL, &data##n, &config##n, POST_KERNEL, \
166 CONFIG_WDT_NPM1300_INIT_PRIORITY, &wdt_npm1300_api);
167
168 DT_INST_FOREACH_STATUS_OKAY(WDT_NPM1300_DEFINE)
169