blob: e786f644e14bb80431429ceab602c645ddb1a2f8 [file] [log] [blame]
Gabor Juhosf8394f612011-01-04 21:28:19 +01001/*
2 * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
3 *
4 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
8 * Author: Deepak Saxena <dsaxena@plexity.net>
9 * Copyright 2004 (c) MontaVista, Software, Inc.
10 *
11 * which again was based on sa1100 driver,
12 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
17 *
18 */
19
Joe Perches27c766a2012-02-15 15:06:19 -080020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Gabor Juhosf8394f612011-01-04 21:28:19 +010022#include <linux/bitops.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
25#include <linux/init.h>
Gabor Juhos09f51002012-12-27 15:38:26 +010026#include <linux/io.h>
Gabor Juhosf8394f612011-01-04 21:28:19 +010027#include <linux/kernel.h>
28#include <linux/miscdevice.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/platform_device.h>
32#include <linux/types.h>
33#include <linux/watchdog.h>
34#include <linux/clk.h>
35#include <linux/err.h>
36
Gabor Juhosf8394f612011-01-04 21:28:19 +010037#define DRIVER_NAME "ath79-wdt"
38
39#define WDT_TIMEOUT 15 /* seconds */
40
Gabor Juhos09f51002012-12-27 15:38:26 +010041#define WDOG_REG_CTRL 0x00
42#define WDOG_REG_TIMER 0x04
43
Gabor Juhosf8394f612011-01-04 21:28:19 +010044#define WDOG_CTRL_LAST_RESET BIT(31)
45#define WDOG_CTRL_ACTION_MASK 3
46#define WDOG_CTRL_ACTION_NONE 0 /* no action */
47#define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
48#define WDOG_CTRL_ACTION_NMI 2 /* NMI */
49#define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
50
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010051static bool nowayout = WATCHDOG_NOWAYOUT;
52module_param(nowayout, bool, 0);
Gabor Juhosf8394f612011-01-04 21:28:19 +010053MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
54 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
55
56static int timeout = WDT_TIMEOUT;
57module_param(timeout, int, 0);
58MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
59 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
60
61static unsigned long wdt_flags;
62
63#define WDT_FLAGS_BUSY 0
64#define WDT_FLAGS_EXPECT_CLOSE 1
65
66static struct clk *wdt_clk;
67static unsigned long wdt_freq;
68static int boot_status;
69static int max_timeout;
Gabor Juhos09f51002012-12-27 15:38:26 +010070static void __iomem *wdt_base;
71
72static inline void ath79_wdt_wr(unsigned reg, u32 val)
73{
74 iowrite32(val, wdt_base + reg);
75}
76
77static inline u32 ath79_wdt_rr(unsigned reg)
78{
79 return ioread32(wdt_base + reg);
80}
Gabor Juhosf8394f612011-01-04 21:28:19 +010081
82static inline void ath79_wdt_keepalive(void)
83{
Gabor Juhos09f51002012-12-27 15:38:26 +010084 ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout);
Gabor Juhos86955e22011-12-23 19:25:42 +010085 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +010086 ath79_wdt_rr(WDOG_REG_TIMER);
Gabor Juhosf8394f612011-01-04 21:28:19 +010087}
88
89static inline void ath79_wdt_enable(void)
90{
91 ath79_wdt_keepalive();
Gabor Juhos09f51002012-12-27 15:38:26 +010092 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR);
Gabor Juhos86955e22011-12-23 19:25:42 +010093 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +010094 ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +010095}
96
97static inline void ath79_wdt_disable(void)
98{
Gabor Juhos09f51002012-12-27 15:38:26 +010099 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE);
Gabor Juhos86955e22011-12-23 19:25:42 +0100100 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +0100101 ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100102}
103
104static int ath79_wdt_set_timeout(int val)
105{
106 if (val < 1 || val > max_timeout)
107 return -EINVAL;
108
109 timeout = val;
110 ath79_wdt_keepalive();
111
112 return 0;
113}
114
115static int ath79_wdt_open(struct inode *inode, struct file *file)
116{
117 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
118 return -EBUSY;
119
120 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
121 ath79_wdt_enable();
122
123 return nonseekable_open(inode, file);
124}
125
126static int ath79_wdt_release(struct inode *inode, struct file *file)
127{
128 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
129 ath79_wdt_disable();
130 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800131 pr_crit("device closed unexpectedly, watchdog timer will not stop!\n");
Gabor Juhosf8394f612011-01-04 21:28:19 +0100132 ath79_wdt_keepalive();
133 }
134
135 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
136 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
137
138 return 0;
139}
140
141static ssize_t ath79_wdt_write(struct file *file, const char *data,
142 size_t len, loff_t *ppos)
143{
144 if (len) {
145 if (!nowayout) {
146 size_t i;
147
148 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
149
150 for (i = 0; i != len; i++) {
151 char c;
152
153 if (get_user(c, data + i))
154 return -EFAULT;
155
156 if (c == 'V')
157 set_bit(WDT_FLAGS_EXPECT_CLOSE,
158 &wdt_flags);
159 }
160 }
161
162 ath79_wdt_keepalive();
163 }
164
165 return len;
166}
167
168static const struct watchdog_info ath79_wdt_info = {
169 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
170 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
171 .firmware_version = 0,
172 .identity = "ATH79 watchdog",
173};
174
175static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
176 unsigned long arg)
177{
178 void __user *argp = (void __user *)arg;
179 int __user *p = argp;
180 int err;
181 int t;
182
183 switch (cmd) {
184 case WDIOC_GETSUPPORT:
185 err = copy_to_user(argp, &ath79_wdt_info,
186 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
187 break;
188
189 case WDIOC_GETSTATUS:
190 err = put_user(0, p);
191 break;
192
193 case WDIOC_GETBOOTSTATUS:
194 err = put_user(boot_status, p);
195 break;
196
197 case WDIOC_KEEPALIVE:
198 ath79_wdt_keepalive();
199 err = 0;
200 break;
201
202 case WDIOC_SETTIMEOUT:
203 err = get_user(t, p);
204 if (err)
205 break;
206
207 err = ath79_wdt_set_timeout(t);
208 if (err)
209 break;
210
211 /* fallthrough */
212 case WDIOC_GETTIMEOUT:
213 err = put_user(timeout, p);
214 break;
215
216 default:
217 err = -ENOTTY;
218 break;
219 }
220
221 return err;
222}
223
224static const struct file_operations ath79_wdt_fops = {
225 .owner = THIS_MODULE,
226 .llseek = no_llseek,
227 .write = ath79_wdt_write,
228 .unlocked_ioctl = ath79_wdt_ioctl,
229 .open = ath79_wdt_open,
230 .release = ath79_wdt_release,
231};
232
233static struct miscdevice ath79_wdt_miscdev = {
234 .minor = WATCHDOG_MINOR,
235 .name = "watchdog",
236 .fops = &ath79_wdt_fops,
237};
238
Bill Pemberton2d991a12012-11-19 13:21:41 -0500239static int ath79_wdt_probe(struct platform_device *pdev)
Gabor Juhosf8394f612011-01-04 21:28:19 +0100240{
Gabor Juhos09f51002012-12-27 15:38:26 +0100241 struct resource *res;
Gabor Juhosf8394f612011-01-04 21:28:19 +0100242 u32 ctrl;
243 int err;
244
Gabor Juhos09f51002012-12-27 15:38:26 +0100245 if (wdt_base)
246 return -EBUSY;
247
248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 if (!res) {
250 dev_err(&pdev->dev, "no memory resource found\n");
251 return -EINVAL;
252 }
253
254 wdt_base = devm_request_and_ioremap(&pdev->dev, res);
255 if (!wdt_base) {
256 dev_err(&pdev->dev, "unable to remap memory region\n");
257 return -ENOMEM;
258 }
259
Gabor Juhos5071a882012-12-27 15:38:24 +0100260 wdt_clk = devm_clk_get(&pdev->dev, "wdt");
Gabor Juhosf8394f612011-01-04 21:28:19 +0100261 if (IS_ERR(wdt_clk))
262 return PTR_ERR(wdt_clk);
263
264 err = clk_enable(wdt_clk);
265 if (err)
Gabor Juhos5071a882012-12-27 15:38:24 +0100266 return err;
Gabor Juhosf8394f612011-01-04 21:28:19 +0100267
268 wdt_freq = clk_get_rate(wdt_clk);
269 if (!wdt_freq) {
270 err = -EINVAL;
271 goto err_clk_disable;
272 }
273
274 max_timeout = (0xfffffffful / wdt_freq);
275 if (timeout < 1 || timeout > max_timeout) {
276 timeout = max_timeout;
277 dev_info(&pdev->dev,
278 "timeout value must be 0 < timeout < %d, using %d\n",
279 max_timeout, timeout);
280 }
281
Gabor Juhos09f51002012-12-27 15:38:26 +0100282 ctrl = ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100283 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
284
285 err = misc_register(&ath79_wdt_miscdev);
286 if (err) {
287 dev_err(&pdev->dev,
288 "unable to register misc device, err=%d\n", err);
289 goto err_clk_disable;
290 }
291
292 return 0;
293
294err_clk_disable:
295 clk_disable(wdt_clk);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100296 return err;
297}
298
Bill Pemberton4b12b892012-11-19 13:26:24 -0500299static int ath79_wdt_remove(struct platform_device *pdev)
Gabor Juhosf8394f612011-01-04 21:28:19 +0100300{
301 misc_deregister(&ath79_wdt_miscdev);
302 clk_disable(wdt_clk);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100303 return 0;
304}
305
306static void ath97_wdt_shutdown(struct platform_device *pdev)
307{
308 ath79_wdt_disable();
309}
310
311static struct platform_driver ath79_wdt_driver = {
Gabor Juhos8157bec2012-08-14 15:35:19 +0200312 .probe = ath79_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500313 .remove = ath79_wdt_remove,
Gabor Juhosf8394f612011-01-04 21:28:19 +0100314 .shutdown = ath97_wdt_shutdown,
315 .driver = {
316 .name = DRIVER_NAME,
317 .owner = THIS_MODULE,
318 },
319};
320
Gabor Juhos8157bec2012-08-14 15:35:19 +0200321module_platform_driver(ath79_wdt_driver);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100322
323MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
324MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
325MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
326MODULE_LICENSE("GPL v2");
327MODULE_ALIAS("platform:" DRIVER_NAME);
328MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);