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