1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009 Nuvoton technology corporation.
8 #include <linux/bitops.h>
9 #include <linux/errno.h>
12 #include <linux/clk.h>
13 #include <linux/kernel.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/interrupt.h>
20 #include <linux/types.h>
21 #include <linux/watchdog.h>
22 #include <linux/uaccess.h>
25 #define WTCLK (0x01 << 10)
26 #define WTE (0x01 << 7) /*wdt enable*/
27 #define WTIS (0x03 << 4)
28 #define WTIF (0x01 << 3)
29 #define WTRF (0x01 << 2)
30 #define WTRE (0x01 << 1)
31 #define WTR (0x01 << 0)
33 * The watchdog time interval can be calculated via following formula:
34 * WTIS real time interval (formula)
35 * 0x00 ((2^ 14 ) * ((external crystal freq) / 256))seconds
36 * 0x01 ((2^ 16 ) * ((external crystal freq) / 256))seconds
37 * 0x02 ((2^ 18 ) * ((external crystal freq) / 256))seconds
38 * 0x03 ((2^ 20 ) * ((external crystal freq) / 256))seconds
40 * The external crystal freq is 15Mhz in the nuc900 evaluation board.
41 * So 0x00 = +-0.28 seconds, 0x01 = +-1.12 seconds, 0x02 = +-4.48 seconds,
42 * 0x03 = +- 16.92 seconds..
44 #define WDT_HW_TIMEOUT 0x02
45 #define WDT_TIMEOUT (HZ/2)
46 #define WDT_HEARTBEAT 15
48 static int heartbeat = WDT_HEARTBEAT;
49 module_param(heartbeat, int, 0);
50 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
51 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
53 static bool nowayout = WATCHDOG_NOWAYOUT;
54 module_param(nowayout, bool, 0);
55 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
56 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
59 struct clk *wdt_clock;
60 struct platform_device *pdev;
61 void __iomem *wdt_base;
63 struct timer_list timer;
65 unsigned long next_heartbeat;
68 static unsigned long nuc900wdt_busy;
69 static struct nuc900_wdt *nuc900_wdt;
71 static inline void nuc900_wdt_keepalive(void)
75 spin_lock(&nuc900_wdt->wdt_lock);
77 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
79 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
81 spin_unlock(&nuc900_wdt->wdt_lock);
84 static inline void nuc900_wdt_start(void)
88 spin_lock(&nuc900_wdt->wdt_lock);
90 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
91 val |= (WTRE | WTE | WTR | WTCLK | WTIF);
93 val |= (WDT_HW_TIMEOUT << 0x04);
94 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
96 spin_unlock(&nuc900_wdt->wdt_lock);
98 nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
99 mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
102 static inline void nuc900_wdt_stop(void)
106 del_timer(&nuc900_wdt->timer);
108 spin_lock(&nuc900_wdt->wdt_lock);
110 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
112 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
114 spin_unlock(&nuc900_wdt->wdt_lock);
117 static inline void nuc900_wdt_ping(void)
119 nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
122 static int nuc900_wdt_open(struct inode *inode, struct file *file)
125 if (test_and_set_bit(0, &nuc900wdt_busy))
130 return stream_open(inode, file);
133 static int nuc900_wdt_close(struct inode *inode, struct file *file)
135 if (nuc900_wdt->expect_close == 42)
138 dev_crit(&nuc900_wdt->pdev->dev,
139 "Unexpected close, not stopping watchdog!\n");
143 nuc900_wdt->expect_close = 0;
144 clear_bit(0, &nuc900wdt_busy);
148 static const struct watchdog_info nuc900_wdt_info = {
149 .identity = "nuc900 watchdog",
150 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
154 static long nuc900_wdt_ioctl(struct file *file,
155 unsigned int cmd, unsigned long arg)
157 void __user *argp = (void __user *)arg;
158 int __user *p = argp;
162 case WDIOC_GETSUPPORT:
163 return copy_to_user(argp, &nuc900_wdt_info,
164 sizeof(nuc900_wdt_info)) ? -EFAULT : 0;
165 case WDIOC_GETSTATUS:
166 case WDIOC_GETBOOTSTATUS:
167 return put_user(0, p);
169 case WDIOC_KEEPALIVE:
173 case WDIOC_SETTIMEOUT:
174 if (get_user(new_value, p))
177 heartbeat = new_value;
180 return put_user(new_value, p);
181 case WDIOC_GETTIMEOUT:
182 return put_user(heartbeat, p);
188 static ssize_t nuc900_wdt_write(struct file *file, const char __user *data,
189 size_t len, loff_t *ppos)
194 /* Scan for magic character */
198 nuc900_wdt->expect_close = 0;
200 for (i = 0; i < len; i++) {
202 if (get_user(c, data + i))
205 nuc900_wdt->expect_close = 42;
215 static void nuc900_wdt_timer_ping(struct timer_list *unused)
217 if (time_before(jiffies, nuc900_wdt->next_heartbeat)) {
218 nuc900_wdt_keepalive();
219 mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
221 dev_warn(&nuc900_wdt->pdev->dev, "Will reset the machine !\n");
224 static const struct file_operations nuc900wdt_fops = {
225 .owner = THIS_MODULE,
227 .unlocked_ioctl = nuc900_wdt_ioctl,
228 .open = nuc900_wdt_open,
229 .release = nuc900_wdt_close,
230 .write = nuc900_wdt_write,
233 static struct miscdevice nuc900wdt_miscdev = {
234 .minor = WATCHDOG_MINOR,
236 .fops = &nuc900wdt_fops,
239 static int nuc900wdt_probe(struct platform_device *pdev)
243 nuc900_wdt = devm_kzalloc(&pdev->dev, sizeof(*nuc900_wdt),
248 nuc900_wdt->pdev = pdev;
250 spin_lock_init(&nuc900_wdt->wdt_lock);
252 nuc900_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
253 if (IS_ERR(nuc900_wdt->wdt_base))
254 return PTR_ERR(nuc900_wdt->wdt_base);
256 nuc900_wdt->wdt_clock = devm_clk_get(&pdev->dev, NULL);
257 if (IS_ERR(nuc900_wdt->wdt_clock)) {
258 dev_err(&pdev->dev, "failed to find watchdog clock source\n");
259 return PTR_ERR(nuc900_wdt->wdt_clock);
262 clk_enable(nuc900_wdt->wdt_clock);
264 timer_setup(&nuc900_wdt->timer, nuc900_wdt_timer_ping, 0);
266 ret = misc_register(&nuc900wdt_miscdev);
268 dev_err(&pdev->dev, "err register miscdev on minor=%d (%d)\n",
269 WATCHDOG_MINOR, ret);
276 clk_disable(nuc900_wdt->wdt_clock);
280 static int nuc900wdt_remove(struct platform_device *pdev)
282 misc_deregister(&nuc900wdt_miscdev);
284 clk_disable(nuc900_wdt->wdt_clock);
289 static struct platform_driver nuc900wdt_driver = {
290 .probe = nuc900wdt_probe,
291 .remove = nuc900wdt_remove,
293 .name = "nuc900-wdt",
297 module_platform_driver(nuc900wdt_driver);
300 MODULE_DESCRIPTION("Watchdog driver for NUC900");
301 MODULE_LICENSE("GPL");
302 MODULE_ALIAS("platform:nuc900-wdt");