blob: db124cebe83883bb260a5f8c3a14928e45ba9eb3 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Wan ZongShun0400e312009-08-17 18:00:01 +08002/*
3 * Copyright (c) 2009 Nuvoton technology corporation.
4 *
5 * Wan ZongShun <mcuos.com@gmail.com>
Wan ZongShun0400e312009-08-17 18:00:01 +08006 */
7
8#include <linux/bitops.h>
9#include <linux/errno.h>
10#include <linux/fs.h>
Wan ZongShun0400e312009-08-17 18:00:01 +080011#include <linux/io.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Wan ZongShun0400e312009-08-17 18:00:01 +080019#include <linux/interrupt.h>
20#include <linux/types.h>
21#include <linux/watchdog.h>
22#include <linux/uaccess.h>
23
24#define REG_WTCR 0x1c
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)
32/*
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
39 *
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..
43 */
44#define WDT_HW_TIMEOUT 0x02
45#define WDT_TIMEOUT (HZ/2)
46#define WDT_HEARTBEAT 15
47
48static int heartbeat = WDT_HEARTBEAT;
49module_param(heartbeat, int, 0);
50MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
51 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
52
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010053static bool nowayout = WATCHDOG_NOWAYOUT;
54module_param(nowayout, bool, 0);
Wan ZongShun0400e312009-08-17 18:00:01 +080055MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
56 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
57
58struct nuc900_wdt {
Wan ZongShun0400e312009-08-17 18:00:01 +080059 struct clk *wdt_clock;
60 struct platform_device *pdev;
61 void __iomem *wdt_base;
62 char expect_close;
63 struct timer_list timer;
64 spinlock_t wdt_lock;
65 unsigned long next_heartbeat;
66};
67
68static unsigned long nuc900wdt_busy;
Axel Line3528292012-01-18 10:45:20 +080069static struct nuc900_wdt *nuc900_wdt;
Wan ZongShun0400e312009-08-17 18:00:01 +080070
71static inline void nuc900_wdt_keepalive(void)
72{
73 unsigned int val;
74
75 spin_lock(&nuc900_wdt->wdt_lock);
76
77 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
78 val |= (WTR | WTIF);
79 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
80
81 spin_unlock(&nuc900_wdt->wdt_lock);
82}
83
84static inline void nuc900_wdt_start(void)
85{
86 unsigned int val;
87
88 spin_lock(&nuc900_wdt->wdt_lock);
89
90 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
91 val |= (WTRE | WTE | WTR | WTCLK | WTIF);
92 val &= ~WTIS;
93 val |= (WDT_HW_TIMEOUT << 0x04);
94 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
95
96 spin_unlock(&nuc900_wdt->wdt_lock);
97
98 nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
99 mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
100}
101
102static inline void nuc900_wdt_stop(void)
103{
104 unsigned int val;
105
106 del_timer(&nuc900_wdt->timer);
107
108 spin_lock(&nuc900_wdt->wdt_lock);
109
110 val = __raw_readl(nuc900_wdt->wdt_base + REG_WTCR);
111 val &= ~WTE;
112 __raw_writel(val, nuc900_wdt->wdt_base + REG_WTCR);
113
114 spin_unlock(&nuc900_wdt->wdt_lock);
115}
116
117static inline void nuc900_wdt_ping(void)
118{
119 nuc900_wdt->next_heartbeat = jiffies + heartbeat * HZ;
120}
121
122static int nuc900_wdt_open(struct inode *inode, struct file *file)
123{
124
125 if (test_and_set_bit(0, &nuc900wdt_busy))
126 return -EBUSY;
127
128 nuc900_wdt_start();
129
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300130 return stream_open(inode, file);
Wan ZongShun0400e312009-08-17 18:00:01 +0800131}
132
133static int nuc900_wdt_close(struct inode *inode, struct file *file)
134{
135 if (nuc900_wdt->expect_close == 42)
136 nuc900_wdt_stop();
137 else {
138 dev_crit(&nuc900_wdt->pdev->dev,
139 "Unexpected close, not stopping watchdog!\n");
140 nuc900_wdt_ping();
141 }
142
143 nuc900_wdt->expect_close = 0;
144 clear_bit(0, &nuc900wdt_busy);
145 return 0;
146}
147
148static const struct watchdog_info nuc900_wdt_info = {
149 .identity = "nuc900 watchdog",
150 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
151 WDIOF_MAGICCLOSE,
152};
153
154static long nuc900_wdt_ioctl(struct file *file,
155 unsigned int cmd, unsigned long arg)
156{
157 void __user *argp = (void __user *)arg;
158 int __user *p = argp;
159 int new_value;
160
161 switch (cmd) {
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);
168
169 case WDIOC_KEEPALIVE:
170 nuc900_wdt_ping();
171 return 0;
172
173 case WDIOC_SETTIMEOUT:
174 if (get_user(new_value, p))
175 return -EFAULT;
176
177 heartbeat = new_value;
178 nuc900_wdt_ping();
179
180 return put_user(new_value, p);
181 case WDIOC_GETTIMEOUT:
182 return put_user(heartbeat, p);
183 default:
184 return -ENOTTY;
185 }
186}
187
188static ssize_t nuc900_wdt_write(struct file *file, const char __user *data,
189 size_t len, loff_t *ppos)
190{
191 if (!len)
192 return 0;
193
194 /* Scan for magic character */
195 if (!nowayout) {
196 size_t i;
197
198 nuc900_wdt->expect_close = 0;
199
200 for (i = 0; i < len; i++) {
201 char c;
202 if (get_user(c, data + i))
203 return -EFAULT;
204 if (c == 'V') {
205 nuc900_wdt->expect_close = 42;
206 break;
207 }
208 }
209 }
210
211 nuc900_wdt_ping();
212 return len;
213}
214
Kees Cooke99e88a2017-10-16 14:43:17 -0700215static void nuc900_wdt_timer_ping(struct timer_list *unused)
Wan ZongShun0400e312009-08-17 18:00:01 +0800216{
217 if (time_before(jiffies, nuc900_wdt->next_heartbeat)) {
218 nuc900_wdt_keepalive();
219 mod_timer(&nuc900_wdt->timer, jiffies + WDT_TIMEOUT);
220 } else
221 dev_warn(&nuc900_wdt->pdev->dev, "Will reset the machine !\n");
222}
223
224static const struct file_operations nuc900wdt_fops = {
225 .owner = THIS_MODULE,
226 .llseek = no_llseek,
227 .unlocked_ioctl = nuc900_wdt_ioctl,
228 .open = nuc900_wdt_open,
229 .release = nuc900_wdt_close,
230 .write = nuc900_wdt_write,
231};
232
233static struct miscdevice nuc900wdt_miscdev = {
234 .minor = WATCHDOG_MINOR,
235 .name = "watchdog",
236 .fops = &nuc900wdt_fops,
237};
238
Bill Pemberton2d991a12012-11-19 13:21:41 -0500239static int nuc900wdt_probe(struct platform_device *pdev)
Wan ZongShun0400e312009-08-17 18:00:01 +0800240{
241 int ret = 0;
242
Jingoo Han3666eb02013-04-30 14:01:25 +0900243 nuc900_wdt = devm_kzalloc(&pdev->dev, sizeof(*nuc900_wdt),
244 GFP_KERNEL);
Wan ZongShun0400e312009-08-17 18:00:01 +0800245 if (!nuc900_wdt)
246 return -ENOMEM;
247
248 nuc900_wdt->pdev = pdev;
249
250 spin_lock_init(&nuc900_wdt->wdt_lock);
251
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700252 nuc900_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han3666eb02013-04-30 14:01:25 +0900253 if (IS_ERR(nuc900_wdt->wdt_base))
254 return PTR_ERR(nuc900_wdt->wdt_base);
Wan ZongShun0400e312009-08-17 18:00:01 +0800255
Jingoo Han3666eb02013-04-30 14:01:25 +0900256 nuc900_wdt->wdt_clock = devm_clk_get(&pdev->dev, NULL);
Wan ZongShun0400e312009-08-17 18:00:01 +0800257 if (IS_ERR(nuc900_wdt->wdt_clock)) {
258 dev_err(&pdev->dev, "failed to find watchdog clock source\n");
Jingoo Han3666eb02013-04-30 14:01:25 +0900259 return PTR_ERR(nuc900_wdt->wdt_clock);
Wan ZongShun0400e312009-08-17 18:00:01 +0800260 }
261
262 clk_enable(nuc900_wdt->wdt_clock);
263
Kees Cooke99e88a2017-10-16 14:43:17 -0700264 timer_setup(&nuc900_wdt->timer, nuc900_wdt_timer_ping, 0);
Wan ZongShun0400e312009-08-17 18:00:01 +0800265
Axel Lin2865e772012-01-18 10:46:52 +0800266 ret = misc_register(&nuc900wdt_miscdev);
267 if (ret) {
Wan ZongShun0400e312009-08-17 18:00:01 +0800268 dev_err(&pdev->dev, "err register miscdev on minor=%d (%d)\n",
269 WATCHDOG_MINOR, ret);
270 goto err_clk;
271 }
272
273 return 0;
274
275err_clk:
276 clk_disable(nuc900_wdt->wdt_clock);
Wan ZongShun0400e312009-08-17 18:00:01 +0800277 return ret;
278}
279
Bill Pemberton4b12b892012-11-19 13:26:24 -0500280static int nuc900wdt_remove(struct platform_device *pdev)
Wan ZongShun0400e312009-08-17 18:00:01 +0800281{
282 misc_deregister(&nuc900wdt_miscdev);
283
284 clk_disable(nuc900_wdt->wdt_clock);
Wan ZongShun0400e312009-08-17 18:00:01 +0800285
286 return 0;
287}
288
289static struct platform_driver nuc900wdt_driver = {
290 .probe = nuc900wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500291 .remove = nuc900wdt_remove,
Wan ZongShun0400e312009-08-17 18:00:01 +0800292 .driver = {
293 .name = "nuc900-wdt",
Wan ZongShun0400e312009-08-17 18:00:01 +0800294 },
295};
296
Axel Linb8ec6112011-11-29 13:56:27 +0800297module_platform_driver(nuc900wdt_driver);
Wan ZongShun0400e312009-08-17 18:00:01 +0800298
299MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
300MODULE_DESCRIPTION("Watchdog driver for NUC900");
301MODULE_LICENSE("GPL");
Wan ZongShun0400e312009-08-17 18:00:01 +0800302MODULE_ALIAS("platform:nuc900-wdt");