blob: e74802f3a32e88d3bc00b16995944083f0aa13e9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Florian Fainelli03ec5852008-02-25 13:11:31 +01002/*
3 * IDT Interprise 79RC32434 watchdog driver
4 *
5 * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
6 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
7 *
8 * based on
9 * SoftDog 0.05: A Software Watchdog Device
10 *
Alan Cox29fa0582008-10-27 15:17:56 +000011 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
12 * All Rights Reserved.
Florian Fainelli03ec5852008-02-25 13:11:31 +010013 */
14
Joe Perches27c766a2012-02-15 15:06:19 -080015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Phil Sutter9b655e02009-02-08 16:44:42 +010017#include <linux/module.h> /* For module specific items */
18#include <linux/moduleparam.h> /* For new moduleparam's */
19#include <linux/types.h> /* For standard types (like size_t) */
20#include <linux/errno.h> /* For the -ENODEV/... values */
21#include <linux/kernel.h> /* For printk/panic/... */
22#include <linux/fs.h> /* For file operations */
Jean Delvare487722c2013-10-21 17:38:49 +020023#include <linux/miscdevice.h> /* For struct miscdevice */
Phil Sutter9b655e02009-02-08 16:44:42 +010024#include <linux/watchdog.h> /* For the watchdog specific items */
25#include <linux/init.h> /* For __init/__exit/... */
26#include <linux/platform_device.h> /* For platform_driver framework */
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000027#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Phil Sutter9b655e02009-02-08 16:44:42 +010028#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +010029#include <linux/io.h> /* For devm_ioremap */
Florian Fainelli03ec5852008-02-25 13:11:31 +010030
Phil Sutter9b655e02009-02-08 16:44:42 +010031#include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
32
Wim Van Sebroeckf296b142009-02-23 13:08:37 +000033#define VERSION "1.0"
Florian Fainelli03ec5852008-02-25 13:11:31 +010034
35static struct {
Florian Fainelli03ec5852008-02-25 13:11:31 +010036 unsigned long inuse;
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000037 spinlock_t io_lock;
Florian Fainelli03ec5852008-02-25 13:11:31 +010038} rc32434_wdt_device;
39
40static struct integ __iomem *wdt_reg;
Florian Fainelli03ec5852008-02-25 13:11:31 +010041
42static int expect_close;
Phil Sutter0af98d32009-02-08 16:44:42 +010043
44/* Board internal clock speed in Hz,
45 * the watchdog timer ticks at. */
46extern unsigned int idt_cpu_freq;
47
48/* translate wtcompare value to seconds and vice versa */
49#define WTCOMP2SEC(x) (x / idt_cpu_freq)
50#define SEC2WTCOMP(x) (x * idt_cpu_freq)
51
52/* Use a default timeout of 20s. This should be
53 * safe for CPU clock speeds up to 400MHz, as
54 * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
55#define WATCHDOG_TIMEOUT 20
56
57static int timeout = WATCHDOG_TIMEOUT;
Phil Sutter08eb2e02009-02-08 16:44:42 +010058module_param(timeout, int, 0);
59MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
Florian Fainelli810a90a2009-12-02 13:21:23 +010060 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Florian Fainelli03ec5852008-02-25 13:11:31 +010061
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010062static bool nowayout = WATCHDOG_NOWAYOUT;
63module_param(nowayout, bool, 0);
Florian Fainelli03ec5852008-02-25 13:11:31 +010064MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
65 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
66
Phil Sutter0af98d32009-02-08 16:44:42 +010067/* apply or and nand masks to data read from addr and write back */
68#define SET_BITS(addr, or, nand) \
69 writel((readl(&addr) | or) & ~nand, &addr)
Florian Fainelli03ec5852008-02-25 13:11:31 +010070
Phil Sutter08eb2e02009-02-08 16:44:42 +010071static int rc32434_wdt_set(int new_timeout)
72{
73 int max_to = WTCOMP2SEC((u32)-1);
74
75 if (new_timeout < 0 || new_timeout > max_to) {
Joe Perches27c766a2012-02-15 15:06:19 -080076 pr_err("timeout value must be between 0 and %d\n", max_to);
Phil Sutter08eb2e02009-02-08 16:44:42 +010077 return -EINVAL;
78 }
79 timeout = new_timeout;
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000080 spin_lock(&rc32434_wdt_device.io_lock);
Phil Sutter08eb2e02009-02-08 16:44:42 +010081 writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000082 spin_unlock(&rc32434_wdt_device.io_lock);
Phil Sutter08eb2e02009-02-08 16:44:42 +010083
84 return 0;
85}
86
Florian Fainelli03ec5852008-02-25 13:11:31 +010087static void rc32434_wdt_start(void)
88{
Phil Sutter0af98d32009-02-08 16:44:42 +010089 u32 or, nand;
Florian Fainelli03ec5852008-02-25 13:11:31 +010090
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000091 spin_lock(&rc32434_wdt_device.io_lock);
92
Phil Sutter0af98d32009-02-08 16:44:42 +010093 /* zero the counter before enabling */
94 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +010095
Phil Sutter0af98d32009-02-08 16:44:42 +010096 /* don't generate a non-maskable interrupt,
97 * do a warm reset instead */
98 nand = 1 << RC32434_ERR_WNE;
99 or = 1 << RC32434_ERR_WRE;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100100
Phil Sutter0af98d32009-02-08 16:44:42 +0100101 /* reset the ERRCS timeout bit in case it's set */
102 nand |= 1 << RC32434_ERR_WTO;
103
104 SET_BITS(wdt_reg->errcs, or, nand);
105
Phil Sutter08eb2e02009-02-08 16:44:42 +0100106 /* set the timeout (either default or based on module param) */
107 rc32434_wdt_set(timeout);
108
Phil Sutter0af98d32009-02-08 16:44:42 +0100109 /* reset WTC timeout bit and enable WDT */
110 nand = 1 << RC32434_WTC_TO;
111 or = 1 << RC32434_WTC_EN;
112
113 SET_BITS(wdt_reg->wtc, or, nand);
Phil Sutter9b655e02009-02-08 16:44:42 +0100114
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000115 spin_unlock(&rc32434_wdt_device.io_lock);
Joe Perches27c766a2012-02-15 15:06:19 -0800116 pr_info("Started watchdog timer\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100117}
118
119static void rc32434_wdt_stop(void)
120{
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000121 spin_lock(&rc32434_wdt_device.io_lock);
122
Phil Sutter0af98d32009-02-08 16:44:42 +0100123 /* Disable WDT */
124 SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
Phil Sutter9b655e02009-02-08 16:44:42 +0100125
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000126 spin_unlock(&rc32434_wdt_device.io_lock);
Joe Perches27c766a2012-02-15 15:06:19 -0800127 pr_info("Stopped watchdog timer\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100128}
Florian Fainelli03ec5852008-02-25 13:11:31 +0100129
Phil Sutter0af98d32009-02-08 16:44:42 +0100130static void rc32434_wdt_ping(void)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100131{
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000132 spin_lock(&rc32434_wdt_device.io_lock);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100133 writel(0, &wdt_reg->wtcount);
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000134 spin_unlock(&rc32434_wdt_device.io_lock);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100135}
136
137static int rc32434_wdt_open(struct inode *inode, struct file *file)
138{
139 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
140 return -EBUSY;
141
142 if (nowayout)
143 __module_get(THIS_MODULE);
144
Phil Sutter0af98d32009-02-08 16:44:42 +0100145 rc32434_wdt_start();
146 rc32434_wdt_ping();
147
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300148 return stream_open(inode, file);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100149}
150
151static int rc32434_wdt_release(struct inode *inode, struct file *file)
152{
Phil Sutter0af98d32009-02-08 16:44:42 +0100153 if (expect_close == 42) {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100154 rc32434_wdt_stop();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100155 module_put(THIS_MODULE);
Phil Sutter0af98d32009-02-08 16:44:42 +0100156 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800157 pr_crit("device closed unexpectedly. WDT will not stop!\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100158 rc32434_wdt_ping();
159 }
Florian Fainelli03ec5852008-02-25 13:11:31 +0100160 clear_bit(0, &rc32434_wdt_device.inuse);
161 return 0;
162}
163
164static ssize_t rc32434_wdt_write(struct file *file, const char *data,
165 size_t len, loff_t *ppos)
166{
167 if (len) {
168 if (!nowayout) {
169 size_t i;
170
171 /* In case it was set long ago */
172 expect_close = 0;
173
174 for (i = 0; i != len; i++) {
175 char c;
176 if (get_user(c, data + i))
177 return -EFAULT;
178 if (c == 'V')
Phil Sutter0af98d32009-02-08 16:44:42 +0100179 expect_close = 42;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100180 }
181 }
Phil Sutter0af98d32009-02-08 16:44:42 +0100182 rc32434_wdt_ping();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100183 return len;
184 }
185 return 0;
186}
187
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000188static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
189 unsigned long arg)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100190{
191 void __user *argp = (void __user *)arg;
192 int new_timeout;
193 unsigned int value;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000194 static const struct watchdog_info ident = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100195 .options = WDIOF_SETTIMEOUT |
196 WDIOF_KEEPALIVEPING |
197 WDIOF_MAGICCLOSE,
198 .identity = "RC32434_WDT Watchdog",
199 };
200 switch (cmd) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100201 case WDIOC_GETSUPPORT:
202 if (copy_to_user(argp, &ident, sizeof(ident)))
203 return -EFAULT;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100204 break;
205 case WDIOC_GETSTATUS:
206 case WDIOC_GETBOOTSTATUS:
Phil Sutter0af98d32009-02-08 16:44:42 +0100207 value = 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100208 if (copy_to_user(argp, &value, sizeof(int)))
209 return -EFAULT;
210 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100211 case WDIOC_SETOPTIONS:
212 if (copy_from_user(&value, argp, sizeof(int)))
213 return -EFAULT;
214 switch (value) {
215 case WDIOS_ENABLECARD:
216 rc32434_wdt_start();
217 break;
218 case WDIOS_DISABLECARD:
219 rc32434_wdt_stop();
Phil Sutter0af98d32009-02-08 16:44:42 +0100220 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100221 default:
222 return -EINVAL;
223 }
224 break;
Phil Sutter9b655e02009-02-08 16:44:42 +0100225 case WDIOC_KEEPALIVE:
226 rc32434_wdt_ping();
227 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100228 case WDIOC_SETTIMEOUT:
229 if (copy_from_user(&new_timeout, argp, sizeof(int)))
230 return -EFAULT;
Phil Sutter0af98d32009-02-08 16:44:42 +0100231 if (rc32434_wdt_set(new_timeout))
Florian Fainelli03ec5852008-02-25 13:11:31 +0100232 return -EINVAL;
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500233 fallthrough;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100234 case WDIOC_GETTIMEOUT:
Michael S. Tsirkin10e7ac22016-02-28 17:44:09 +0200235 return copy_to_user(argp, &timeout, sizeof(int)) ? -EFAULT : 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100236 default:
237 return -ENOTTY;
238 }
239
240 return 0;
241}
242
Wim Van Sebroeckd5c26a52009-03-18 08:18:43 +0000243static const struct file_operations rc32434_wdt_fops = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100244 .owner = THIS_MODULE,
245 .llseek = no_llseek,
246 .write = rc32434_wdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000247 .unlocked_ioctl = rc32434_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200248 .compat_ioctl = compat_ptr_ioctl,
Florian Fainelli03ec5852008-02-25 13:11:31 +0100249 .open = rc32434_wdt_open,
250 .release = rc32434_wdt_release,
251};
252
253static struct miscdevice rc32434_wdt_miscdev = {
254 .minor = WATCHDOG_MINOR,
255 .name = "watchdog",
256 .fops = &rc32434_wdt_fops,
257};
258
Bill Pemberton2d991a12012-11-19 13:21:41 -0500259static int rc32434_wdt_probe(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100260{
261 int ret;
262 struct resource *r;
263
Phil Sutter0af98d32009-02-08 16:44:42 +0100264 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100265 if (!r) {
Joe Perches27c766a2012-02-15 15:06:19 -0800266 pr_err("failed to retrieve resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100267 return -ENODEV;
268 }
269
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100270 wdt_reg = devm_ioremap(&pdev->dev, r->start, resource_size(r));
Florian Fainelli03ec5852008-02-25 13:11:31 +0100271 if (!wdt_reg) {
Joe Perches27c766a2012-02-15 15:06:19 -0800272 pr_err("failed to remap I/O resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100273 return -ENXIO;
274 }
275
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000276 spin_lock_init(&rc32434_wdt_device.io_lock);
277
Wim Van Sebroeckf296b142009-02-23 13:08:37 +0000278 /* Make sure the watchdog is not running */
279 rc32434_wdt_stop();
280
Phil Sutter08eb2e02009-02-08 16:44:42 +0100281 /* Check that the heartbeat value is within it's range;
282 * if not reset to the default */
283 if (rc32434_wdt_set(timeout)) {
284 rc32434_wdt_set(WATCHDOG_TIMEOUT);
Joe Perches27c766a2012-02-15 15:06:19 -0800285 pr_info("timeout value must be between 0 and %d\n",
Phil Sutter08eb2e02009-02-08 16:44:42 +0100286 WTCOMP2SEC((u32)-1));
287 }
288
Florian Fainelli03ec5852008-02-25 13:11:31 +0100289 ret = misc_register(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100290 if (ret < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800291 pr_err("failed to register watchdog device\n");
Jingoo Han52ccc5a2013-04-30 14:01:41 +0900292 return ret;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100293 }
294
Joe Perches27c766a2012-02-15 15:06:19 -0800295 pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n",
296 timeout);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100297
298 return 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100299}
300
Bill Pemberton4b12b892012-11-19 13:26:24 -0500301static int rc32434_wdt_remove(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100302{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100303 misc_deregister(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100304 return 0;
305}
306
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000307static void rc32434_wdt_shutdown(struct platform_device *pdev)
308{
309 rc32434_wdt_stop();
310}
311
Phil Sutter9b655e02009-02-08 16:44:42 +0100312static struct platform_driver rc32434_wdt_driver = {
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000313 .probe = rc32434_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500314 .remove = rc32434_wdt_remove,
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000315 .shutdown = rc32434_wdt_shutdown,
316 .driver = {
317 .name = "rc32434_wdt",
Florian Fainelli03ec5852008-02-25 13:11:31 +0100318 }
319};
320
Axel Linb8ec6112011-11-29 13:56:27 +0800321module_platform_driver(rc32434_wdt_driver);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100322
323MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
324 "Florian Fainelli <florian@openwrt.org>");
325MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
326MODULE_LICENSE("GPL");