blob: 0bc72dd69b70371f8d90869df4e98e6df619c995 [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0
James Chapman3be10212005-08-17 09:01:33 +02002/*
3 * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
4 *
5 * Author: James Chapman <jchapman@katalix.com>
6 *
7 * Platform-specific setup code should configure the dog to generate
8 * interrupt or reset as required. This code only enables/disables
9 * and services the watchdog.
10 *
11 * Derived from mpc8xx_wdt.c, with the following copyright.
Alan Coxa86b8492008-05-19 14:07:26 +010012 *
Guenter Roeckd0173272019-06-20 09:28:46 -070013 * 2002 (c) Florian Schirmer <jolt@tuxbox.org>
James Chapman3be10212005-08-17 09:01:33 +020014 */
15
Joe Perches27c766a2012-02-15 15:06:19 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
James Chapman3be10212005-08-17 09:01:33 +020018#include <linux/fs.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/miscdevice.h>
22#include <linux/module.h>
23#include <linux/watchdog.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Dale Farnsworth7e07a152007-07-24 11:12:24 -070025#include <linux/mv643xx.h>
Alan Coxa86b8492008-05-19 14:07:26 +010026#include <linux/uaccess.h>
27#include <linux/io.h>
James Chapman3be10212005-08-17 09:01:33 +020028
Dale Farnsworth8a5cfa642007-07-24 11:09:18 -070029#define MV64x60_WDT_WDC_OFFSET 0
30
Dale Farnsworthf1869992007-07-24 11:31:25 -070031/*
32 * The watchdog configuration register contains a pair of 2-bit fields,
33 * 1. a reload field, bits 27-26, which triggers a reload of
34 * the countdown register, and
35 * 2. an enable field, bits 25-24, which toggles between
36 * enabling and disabling the watchdog timer.
37 * Bit 31 is a read-only field which indicates whether the
38 * watchdog timer is currently enabled.
39 *
40 * The low 24 bits contain the timer reload value.
41 */
42#define MV64x60_WDC_ENABLE_SHIFT 24
43#define MV64x60_WDC_SERVICE_SHIFT 26
44#define MV64x60_WDC_ENABLED_SHIFT 31
45
46#define MV64x60_WDC_ENABLED_TRUE 1
47#define MV64x60_WDC_ENABLED_FALSE 0
James Chapman3be10212005-08-17 09:01:33 +020048
49/* Flags bits */
50#define MV64x60_WDOG_FLAG_OPENED 0
James Chapman3be10212005-08-17 09:01:33 +020051
52static unsigned long wdt_flags;
53static int wdt_status;
Dale Farnsworth8a5cfa642007-07-24 11:09:18 -070054static void __iomem *mv64x60_wdt_regs;
James Chapman3be10212005-08-17 09:01:33 +020055static int mv64x60_wdt_timeout;
Dale Farnsworthf1869992007-07-24 11:31:25 -070056static int mv64x60_wdt_count;
Dale Farnsworth94796f92007-07-24 11:15:26 -070057static unsigned int bus_clk;
Dale Farnsworthbf2fc922007-07-24 11:18:14 -070058static char expect_close;
Dale Farnsworthf1869992007-07-24 11:31:25 -070059static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
James Chapman3be10212005-08-17 09:01:33 +020060
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010061static bool nowayout = WATCHDOG_NOWAYOUT;
62module_param(nowayout, bool, 0);
Alan Coxa86b8492008-05-19 14:07:26 +010063MODULE_PARM_DESC(nowayout,
64 "Watchdog cannot be stopped once started (default="
65 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Dale Farnsworthd37a5c32007-07-24 11:17:23 -070066
Dale Farnsworthf1869992007-07-24 11:31:25 -070067static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
James Chapman3be10212005-08-17 09:01:33 +020068{
Dale Farnsworthf1869992007-07-24 11:31:25 -070069 u32 data;
70 u32 enabled;
71 int ret = 0;
72
73 spin_lock(&mv64x60_wdt_spinlock);
74 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
75 enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
76
77 /* only toggle the requested field if enabled state matches predicate */
78 if ((enabled ^ enabled_predicate) == 0) {
79 /* We write a 1, then a 2 -- to the appropriate field */
80 data = (1 << field_shift) | mv64x60_wdt_count;
81 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
82
83 data = (2 << field_shift) | mv64x60_wdt_count;
84 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
85 ret = 1;
86 }
87 spin_unlock(&mv64x60_wdt_spinlock);
88
89 return ret;
James Chapman3be10212005-08-17 09:01:33 +020090}
91
92static void mv64x60_wdt_service(void)
93{
Dale Farnsworthf1869992007-07-24 11:31:25 -070094 mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
95 MV64x60_WDC_SERVICE_SHIFT);
James Chapman3be10212005-08-17 09:01:33 +020096}
97
98static void mv64x60_wdt_handler_enable(void)
99{
Dale Farnsworthf1869992007-07-24 11:31:25 -0700100 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
101 MV64x60_WDC_ENABLE_SHIFT)) {
102 mv64x60_wdt_service();
Joe Perches27c766a2012-02-15 15:06:19 -0800103 pr_notice("watchdog activated\n");
James Chapman3be10212005-08-17 09:01:33 +0200104 }
105}
106
Dale Farnsworthf1869992007-07-24 11:31:25 -0700107static void mv64x60_wdt_handler_disable(void)
108{
109 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
110 MV64x60_WDC_ENABLE_SHIFT))
Joe Perches27c766a2012-02-15 15:06:19 -0800111 pr_notice("watchdog deactivated\n");
Dale Farnsworthf1869992007-07-24 11:31:25 -0700112}
113
114static void mv64x60_wdt_set_timeout(unsigned int timeout)
Dale Farnsworth94796f92007-07-24 11:15:26 -0700115{
116 /* maximum bus cycle count is 0xFFFFFFFF */
117 if (timeout > 0xFFFFFFFF / bus_clk)
118 timeout = 0xFFFFFFFF / bus_clk;
119
Dale Farnsworthf1869992007-07-24 11:31:25 -0700120 mv64x60_wdt_count = timeout * bus_clk >> 8;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700121 mv64x60_wdt_timeout = timeout;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700122}
123
James Chapman3be10212005-08-17 09:01:33 +0200124static int mv64x60_wdt_open(struct inode *inode, struct file *file)
125{
126 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
127 return -EBUSY;
128
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700129 if (nowayout)
130 __module_get(THIS_MODULE);
131
James Chapman3be10212005-08-17 09:01:33 +0200132 mv64x60_wdt_handler_enable();
133
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300134 return stream_open(inode, file);
James Chapman3be10212005-08-17 09:01:33 +0200135}
136
137static int mv64x60_wdt_release(struct inode *inode, struct file *file)
138{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700139 if (expect_close == 42)
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700140 mv64x60_wdt_handler_disable();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700141 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800142 pr_crit("unexpected close, not stopping timer!\n");
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700143 mv64x60_wdt_service();
144 }
145 expect_close = 0;
James Chapman3be10212005-08-17 09:01:33 +0200146
147 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
148
149 return 0;
150}
151
Al Virob2846df2005-09-29 00:42:27 +0100152static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
Alan Coxa86b8492008-05-19 14:07:26 +0100153 size_t len, loff_t *ppos)
James Chapman3be10212005-08-17 09:01:33 +0200154{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700155 if (len) {
156 if (!nowayout) {
157 size_t i;
158
159 expect_close = 0;
160
161 for (i = 0; i != len; i++) {
162 char c;
Alan Coxa86b8492008-05-19 14:07:26 +0100163 if (get_user(c, data + i))
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700164 return -EFAULT;
165 if (c == 'V')
166 expect_close = 42;
167 }
168 }
James Chapman3be10212005-08-17 09:01:33 +0200169 mv64x60_wdt_service();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700170 }
James Chapman3be10212005-08-17 09:01:33 +0200171
172 return len;
173}
174
Alan Coxa86b8492008-05-19 14:07:26 +0100175static long mv64x60_wdt_ioctl(struct file *file,
176 unsigned int cmd, unsigned long arg)
James Chapman3be10212005-08-17 09:01:33 +0200177{
Dale Farnsworth94796f92007-07-24 11:15:26 -0700178 int timeout;
Dale Farnsworth85d57232007-07-24 11:16:29 -0700179 int options;
Al Virob2846df2005-09-29 00:42:27 +0100180 void __user *argp = (void __user *)arg;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000181 static const struct watchdog_info info = {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700182 .options = WDIOF_SETTIMEOUT |
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700183 WDIOF_MAGICCLOSE |
Dale Farnsworth94796f92007-07-24 11:15:26 -0700184 WDIOF_KEEPALIVEPING,
James Chapman3be10212005-08-17 09:01:33 +0200185 .firmware_version = 0,
186 .identity = "MV64x60 watchdog",
187 };
188
189 switch (cmd) {
190 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100191 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200192 return -EFAULT;
193 break;
194
195 case WDIOC_GETSTATUS:
196 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100197 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200198 return -EFAULT;
199 wdt_status &= ~WDIOF_KEEPALIVEPING;
200 break;
201
202 case WDIOC_GETTEMP:
203 return -EOPNOTSUPP;
204
205 case WDIOC_SETOPTIONS:
Dale Farnsworth85d57232007-07-24 11:16:29 -0700206 if (get_user(options, (int __user *)argp))
207 return -EFAULT;
208
209 if (options & WDIOS_DISABLECARD)
210 mv64x60_wdt_handler_disable();
211
212 if (options & WDIOS_ENABLECARD)
213 mv64x60_wdt_handler_enable();
214 break;
James Chapman3be10212005-08-17 09:01:33 +0200215
216 case WDIOC_KEEPALIVE:
217 mv64x60_wdt_service();
218 wdt_status |= WDIOF_KEEPALIVEPING;
219 break;
220
221 case WDIOC_SETTIMEOUT:
Dale Farnsworth94796f92007-07-24 11:15:26 -0700222 if (get_user(timeout, (int __user *)argp))
223 return -EFAULT;
224 mv64x60_wdt_set_timeout(timeout);
225 /* Fall through */
James Chapman3be10212005-08-17 09:01:33 +0200226
227 case WDIOC_GETTIMEOUT:
Dale Farnsworth264f0992007-07-24 11:14:21 -0700228 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200229 return -EFAULT;
230 break;
231
232 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200233 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200234 }
235
236 return 0;
237}
238
Arjan van de Ven62322d22006-07-03 00:24:21 -0700239static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200240 .owner = THIS_MODULE,
241 .llseek = no_llseek,
242 .write = mv64x60_wdt_write,
Alan Coxa86b8492008-05-19 14:07:26 +0100243 .unlocked_ioctl = mv64x60_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200244 .compat_ioctl = compat_ptr_ioctl,
James Chapman3be10212005-08-17 09:01:33 +0200245 .open = mv64x60_wdt_open,
246 .release = mv64x60_wdt_release,
247};
248
249static struct miscdevice mv64x60_wdt_miscdev = {
250 .minor = WATCHDOG_MINOR,
251 .name = "watchdog",
252 .fops = &mv64x60_wdt_fops,
253};
254
Bill Pemberton2d991a12012-11-19 13:21:41 -0500255static int mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200256{
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900257 struct mv64x60_wdt_pdata *pdata = dev_get_platdata(&dev->dev);
Dale Farnsworth8a5cfa642007-07-24 11:09:18 -0700258 struct resource *r;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700259 int timeout = 10;
James Chapman3be10212005-08-17 09:01:33 +0200260
Dale Farnsworth94796f92007-07-24 11:15:26 -0700261 bus_clk = 133; /* in MHz */
James Chapman3be10212005-08-17 09:01:33 +0200262 if (pdata) {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700263 timeout = pdata->timeout;
James Chapman3be10212005-08-17 09:01:33 +0200264 bus_clk = pdata->bus_clk;
265 }
266
Dale Farnsworth94796f92007-07-24 11:15:26 -0700267 /* Since bus_clk is truncated MHz, actual frequency could be
268 * up to 1MHz higher. Round up, since it's better to time out
269 * too late than too soon.
270 */
271 bus_clk++;
272 bus_clk *= 1000000; /* convert to Hz */
273
Dale Farnsworth8a5cfa642007-07-24 11:09:18 -0700274 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
275 if (!r)
276 return -ENODEV;
277
Jingoo Hanac1bb692013-04-30 14:00:49 +0900278 mv64x60_wdt_regs = devm_ioremap(&dev->dev, r->start, resource_size(r));
Dale Farnsworth8a5cfa642007-07-24 11:09:18 -0700279 if (mv64x60_wdt_regs == NULL)
280 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200281
Dale Farnsworth94796f92007-07-24 11:15:26 -0700282 mv64x60_wdt_set_timeout(timeout);
James Chapman3be10212005-08-17 09:01:33 +0200283
Dale Farnsworth2422df52007-07-24 11:19:47 -0700284 mv64x60_wdt_handler_disable(); /* in case timer was already running */
285
James Chapman3be10212005-08-17 09:01:33 +0200286 return misc_register(&mv64x60_wdt_miscdev);
287}
288
Bill Pemberton4b12b892012-11-19 13:26:24 -0500289static int mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200290{
291 misc_deregister(&mv64x60_wdt_miscdev);
292
James Chapman3be10212005-08-17 09:01:33 +0200293 mv64x60_wdt_handler_disable();
294
295 return 0;
296}
297
Russell King3ae5eae2005-11-09 22:32:44 +0000298static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200299 .probe = mv64x60_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500300 .remove = mv64x60_wdt_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000301 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000302 .name = MV64x60_WDT_NAME,
303 },
James Chapman3be10212005-08-17 09:01:33 +0200304};
305
James Chapman3be10212005-08-17 09:01:33 +0200306static int __init mv64x60_wdt_init(void)
307{
Joe Perches27c766a2012-02-15 15:06:19 -0800308 pr_info("MV64x60 watchdog driver\n");
James Chapman3be10212005-08-17 09:01:33 +0200309
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700310 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200311}
312
313static void __exit mv64x60_wdt_exit(void)
314{
Russell King3ae5eae2005-11-09 22:32:44 +0000315 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200316}
317
318module_init(mv64x60_wdt_init);
319module_exit(mv64x60_wdt_exit);
320
321MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
322MODULE_DESCRIPTION("MV64x60 watchdog driver");
323MODULE_LICENSE("GPL");
Kay Sieversf37d1932008-04-10 21:29:23 -0700324MODULE_ALIAS("platform:" MV64x60_WDT_NAME);