blob: 75de664ef4b018a72d77fceb8934a48b94b2e88b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Gabor Juhosf8394f612011-01-04 21:28:19 +01002/*
3 * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
4 *
5 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 *
8 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
9 * Author: Deepak Saxena <dsaxena@plexity.net>
10 * Copyright 2004 (c) MontaVista, Software, Inc.
11 *
12 * which again was based on sa1100 driver,
13 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
Gabor Juhosf8394f612011-01-04 21:28:19 +010014 */
15
Joe Perches27c766a2012-02-15 15:06:19 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Gabor Juhosf8394f612011-01-04 21:28:19 +010018#include <linux/bitops.h>
Gabor Juhos23afeb62014-04-16 11:34:41 +020019#include <linux/delay.h>
Gabor Juhosf8394f612011-01-04 21:28:19 +010020#include <linux/errno.h>
21#include <linux/fs.h>
Gabor Juhos09f51002012-12-27 15:38:26 +010022#include <linux/io.h>
Gabor Juhosf8394f612011-01-04 21:28:19 +010023#include <linux/kernel.h>
24#include <linux/miscdevice.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/platform_device.h>
28#include <linux/types.h>
29#include <linux/watchdog.h>
30#include <linux/clk.h>
31#include <linux/err.h>
Gabor Juhos15920d12013-02-02 10:34:54 +010032#include <linux/of.h>
33#include <linux/of_platform.h>
Al Viro29abfbd2016-09-05 11:35:50 -040034#include <linux/uaccess.h>
Gabor Juhosf8394f612011-01-04 21:28:19 +010035
Gabor Juhosf8394f612011-01-04 21:28:19 +010036#define DRIVER_NAME "ath79-wdt"
37
38#define WDT_TIMEOUT 15 /* seconds */
39
Gabor Juhos09f51002012-12-27 15:38:26 +010040#define WDOG_REG_CTRL 0x00
41#define WDOG_REG_TIMER 0x04
42
Gabor Juhosf8394f612011-01-04 21:28:19 +010043#define WDOG_CTRL_LAST_RESET BIT(31)
44#define WDOG_CTRL_ACTION_MASK 3
45#define WDOG_CTRL_ACTION_NONE 0 /* no action */
46#define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
47#define WDOG_CTRL_ACTION_NMI 2 /* NMI */
48#define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
49
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010050static bool nowayout = WATCHDOG_NOWAYOUT;
51module_param(nowayout, bool, 0);
Gabor Juhosf8394f612011-01-04 21:28:19 +010052MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
53 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
54
55static int timeout = WDT_TIMEOUT;
56module_param(timeout, int, 0);
57MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
58 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
59
60static unsigned long wdt_flags;
61
62#define WDT_FLAGS_BUSY 0
63#define WDT_FLAGS_EXPECT_CLOSE 1
64
65static struct clk *wdt_clk;
66static unsigned long wdt_freq;
67static int boot_status;
68static int max_timeout;
Gabor Juhos09f51002012-12-27 15:38:26 +010069static void __iomem *wdt_base;
70
71static inline void ath79_wdt_wr(unsigned reg, u32 val)
72{
73 iowrite32(val, wdt_base + reg);
74}
75
76static inline u32 ath79_wdt_rr(unsigned reg)
77{
78 return ioread32(wdt_base + reg);
79}
Gabor Juhosf8394f612011-01-04 21:28:19 +010080
81static inline void ath79_wdt_keepalive(void)
82{
Gabor Juhos09f51002012-12-27 15:38:26 +010083 ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout);
Gabor Juhos86955e22011-12-23 19:25:42 +010084 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +010085 ath79_wdt_rr(WDOG_REG_TIMER);
Gabor Juhosf8394f612011-01-04 21:28:19 +010086}
87
88static inline void ath79_wdt_enable(void)
89{
90 ath79_wdt_keepalive();
Gabor Juhos23afeb62014-04-16 11:34:41 +020091
92 /*
93 * Updating the TIMER register requires a few microseconds
94 * on the AR934x SoCs at least. Use a small delay to ensure
95 * that the TIMER register is updated within the hardware
96 * before enabling the watchdog.
97 */
98 udelay(2);
99
Gabor Juhos09f51002012-12-27 15:38:26 +0100100 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR);
Gabor Juhos86955e22011-12-23 19:25:42 +0100101 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +0100102 ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100103}
104
105static inline void ath79_wdt_disable(void)
106{
Gabor Juhos09f51002012-12-27 15:38:26 +0100107 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE);
Gabor Juhos86955e22011-12-23 19:25:42 +0100108 /* flush write */
Gabor Juhos09f51002012-12-27 15:38:26 +0100109 ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100110}
111
112static int ath79_wdt_set_timeout(int val)
113{
114 if (val < 1 || val > max_timeout)
115 return -EINVAL;
116
117 timeout = val;
118 ath79_wdt_keepalive();
119
120 return 0;
121}
122
123static int ath79_wdt_open(struct inode *inode, struct file *file)
124{
125 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
126 return -EBUSY;
127
128 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
129 ath79_wdt_enable();
130
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300131 return stream_open(inode, file);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100132}
133
134static int ath79_wdt_release(struct inode *inode, struct file *file)
135{
136 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
137 ath79_wdt_disable();
138 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800139 pr_crit("device closed unexpectedly, watchdog timer will not stop!\n");
Gabor Juhosf8394f612011-01-04 21:28:19 +0100140 ath79_wdt_keepalive();
141 }
142
143 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
144 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
145
146 return 0;
147}
148
149static ssize_t ath79_wdt_write(struct file *file, const char *data,
150 size_t len, loff_t *ppos)
151{
152 if (len) {
153 if (!nowayout) {
154 size_t i;
155
156 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
157
158 for (i = 0; i != len; i++) {
159 char c;
160
161 if (get_user(c, data + i))
162 return -EFAULT;
163
164 if (c == 'V')
165 set_bit(WDT_FLAGS_EXPECT_CLOSE,
166 &wdt_flags);
167 }
168 }
169
170 ath79_wdt_keepalive();
171 }
172
173 return len;
174}
175
176static const struct watchdog_info ath79_wdt_info = {
177 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
178 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
179 .firmware_version = 0,
180 .identity = "ATH79 watchdog",
181};
182
183static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
184 unsigned long arg)
185{
186 void __user *argp = (void __user *)arg;
187 int __user *p = argp;
188 int err;
189 int t;
190
191 switch (cmd) {
192 case WDIOC_GETSUPPORT:
193 err = copy_to_user(argp, &ath79_wdt_info,
194 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
195 break;
196
197 case WDIOC_GETSTATUS:
198 err = put_user(0, p);
199 break;
200
201 case WDIOC_GETBOOTSTATUS:
202 err = put_user(boot_status, p);
203 break;
204
205 case WDIOC_KEEPALIVE:
206 ath79_wdt_keepalive();
207 err = 0;
208 break;
209
210 case WDIOC_SETTIMEOUT:
211 err = get_user(t, p);
212 if (err)
213 break;
214
215 err = ath79_wdt_set_timeout(t);
216 if (err)
217 break;
218
219 /* fallthrough */
220 case WDIOC_GETTIMEOUT:
221 err = put_user(timeout, p);
222 break;
223
224 default:
225 err = -ENOTTY;
226 break;
227 }
228
229 return err;
230}
231
232static const struct file_operations ath79_wdt_fops = {
233 .owner = THIS_MODULE,
234 .llseek = no_llseek,
235 .write = ath79_wdt_write,
236 .unlocked_ioctl = ath79_wdt_ioctl,
237 .open = ath79_wdt_open,
238 .release = ath79_wdt_release,
239};
240
241static struct miscdevice ath79_wdt_miscdev = {
242 .minor = WATCHDOG_MINOR,
243 .name = "watchdog",
244 .fops = &ath79_wdt_fops,
245};
246
Bill Pemberton2d991a12012-11-19 13:21:41 -0500247static int ath79_wdt_probe(struct platform_device *pdev)
Gabor Juhosf8394f612011-01-04 21:28:19 +0100248{
249 u32 ctrl;
250 int err;
251
Gabor Juhos09f51002012-12-27 15:38:26 +0100252 if (wdt_base)
253 return -EBUSY;
254
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700255 wdt_base = devm_platform_ioremap_resource(pdev, 0);
Sachin Kamat6330c702013-03-04 10:36:41 +0530256 if (IS_ERR(wdt_base))
257 return PTR_ERR(wdt_base);
Gabor Juhos09f51002012-12-27 15:38:26 +0100258
Gabor Juhos5071a882012-12-27 15:38:24 +0100259 wdt_clk = devm_clk_get(&pdev->dev, "wdt");
Gabor Juhosf8394f612011-01-04 21:28:19 +0100260 if (IS_ERR(wdt_clk))
261 return PTR_ERR(wdt_clk);
262
Gabor Juhosb4e62d92014-04-16 11:35:50 +0200263 err = clk_prepare_enable(wdt_clk);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100264 if (err)
Gabor Juhos5071a882012-12-27 15:38:24 +0100265 return err;
Gabor Juhosf8394f612011-01-04 21:28:19 +0100266
267 wdt_freq = clk_get_rate(wdt_clk);
268 if (!wdt_freq) {
269 err = -EINVAL;
270 goto err_clk_disable;
271 }
272
273 max_timeout = (0xfffffffful / wdt_freq);
274 if (timeout < 1 || timeout > max_timeout) {
275 timeout = max_timeout;
276 dev_info(&pdev->dev,
277 "timeout value must be 0 < timeout < %d, using %d\n",
278 max_timeout, timeout);
279 }
280
Gabor Juhos09f51002012-12-27 15:38:26 +0100281 ctrl = ath79_wdt_rr(WDOG_REG_CTRL);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100282 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
283
284 err = misc_register(&ath79_wdt_miscdev);
285 if (err) {
286 dev_err(&pdev->dev,
287 "unable to register misc device, err=%d\n", err);
288 goto err_clk_disable;
289 }
290
291 return 0;
292
293err_clk_disable:
Gabor Juhosb4e62d92014-04-16 11:35:50 +0200294 clk_disable_unprepare(wdt_clk);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100295 return err;
296}
297
Bill Pemberton4b12b892012-11-19 13:26:24 -0500298static int ath79_wdt_remove(struct platform_device *pdev)
Gabor Juhosf8394f612011-01-04 21:28:19 +0100299{
300 misc_deregister(&ath79_wdt_miscdev);
Gabor Juhosb4e62d92014-04-16 11:35:50 +0200301 clk_disable_unprepare(wdt_clk);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100302 return 0;
303}
304
Christophe JAILLETa18670f2019-07-21 14:55:47 +0200305static void ath79_wdt_shutdown(struct platform_device *pdev)
Gabor Juhosf8394f612011-01-04 21:28:19 +0100306{
307 ath79_wdt_disable();
308}
309
Gabor Juhos15920d12013-02-02 10:34:54 +0100310#ifdef CONFIG_OF
311static const struct of_device_id ath79_wdt_match[] = {
312 { .compatible = "qca,ar7130-wdt" },
313 {},
314};
315MODULE_DEVICE_TABLE(of, ath79_wdt_match);
316#endif
317
Gabor Juhosf8394f612011-01-04 21:28:19 +0100318static struct platform_driver ath79_wdt_driver = {
Gabor Juhos8157bec2012-08-14 15:35:19 +0200319 .probe = ath79_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500320 .remove = ath79_wdt_remove,
Christophe JAILLETa18670f2019-07-21 14:55:47 +0200321 .shutdown = ath79_wdt_shutdown,
Gabor Juhosf8394f612011-01-04 21:28:19 +0100322 .driver = {
323 .name = DRIVER_NAME,
Gabor Juhos15920d12013-02-02 10:34:54 +0100324 .of_match_table = of_match_ptr(ath79_wdt_match),
Gabor Juhosf8394f612011-01-04 21:28:19 +0100325 },
326};
327
Gabor Juhos8157bec2012-08-14 15:35:19 +0200328module_platform_driver(ath79_wdt_driver);
Gabor Juhosf8394f612011-01-04 21:28:19 +0100329
330MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
331MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
332MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
333MODULE_LICENSE("GPL v2");
334MODULE_ALIAS("platform:" DRIVER_NAME);