blob: 3a75f3b53452cdb51f49882a26480cbcf8673fb9 [file] [log] [blame]
Florian Fainelli03ec5852008-02-25 13:11:31 +01001/*
2 * IDT Interprise 79RC32434 watchdog driver
3 *
4 * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
6 *
7 * based on
8 * SoftDog 0.05: A Software Watchdog Device
9 *
Alan Cox29fa0582008-10-27 15:17:56 +000010 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
Florian Fainelli03ec5852008-02-25 13:11:31 +010012 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 */
19
Joe Perches27c766a2012-02-15 15:06:19 -080020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Phil Sutter9b655e02009-02-08 16:44:42 +010022#include <linux/module.h> /* For module specific items */
23#include <linux/moduleparam.h> /* For new moduleparam's */
24#include <linux/types.h> /* For standard types (like size_t) */
25#include <linux/errno.h> /* For the -ENODEV/... values */
26#include <linux/kernel.h> /* For printk/panic/... */
27#include <linux/fs.h> /* For file operations */
Jean Delvare487722c2013-10-21 17:38:49 +020028#include <linux/miscdevice.h> /* For struct miscdevice */
Phil Sutter9b655e02009-02-08 16:44:42 +010029#include <linux/watchdog.h> /* For the watchdog specific items */
30#include <linux/init.h> /* For __init/__exit/... */
31#include <linux/platform_device.h> /* For platform_driver framework */
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000032#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Phil Sutter9b655e02009-02-08 16:44:42 +010033#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
Jingoo Han52ccc5a2013-04-30 14:01:41 +090034#include <linux/io.h> /* For devm_ioremap_nocache */
Florian Fainelli03ec5852008-02-25 13:11:31 +010035
Phil Sutter9b655e02009-02-08 16:44:42 +010036#include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
37
Wim Van Sebroeckf296b142009-02-23 13:08:37 +000038#define VERSION "1.0"
Florian Fainelli03ec5852008-02-25 13:11:31 +010039
40static struct {
Florian Fainelli03ec5852008-02-25 13:11:31 +010041 unsigned long inuse;
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000042 spinlock_t io_lock;
Florian Fainelli03ec5852008-02-25 13:11:31 +010043} rc32434_wdt_device;
44
45static struct integ __iomem *wdt_reg;
Florian Fainelli03ec5852008-02-25 13:11:31 +010046
47static int expect_close;
Phil Sutter0af98d32009-02-08 16:44:42 +010048
49/* Board internal clock speed in Hz,
50 * the watchdog timer ticks at. */
51extern unsigned int idt_cpu_freq;
52
53/* translate wtcompare value to seconds and vice versa */
54#define WTCOMP2SEC(x) (x / idt_cpu_freq)
55#define SEC2WTCOMP(x) (x * idt_cpu_freq)
56
57/* Use a default timeout of 20s. This should be
58 * safe for CPU clock speeds up to 400MHz, as
59 * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
60#define WATCHDOG_TIMEOUT 20
61
62static int timeout = WATCHDOG_TIMEOUT;
Phil Sutter08eb2e02009-02-08 16:44:42 +010063module_param(timeout, int, 0);
64MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
Florian Fainelli810a90a2009-12-02 13:21:23 +010065 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Florian Fainelli03ec5852008-02-25 13:11:31 +010066
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010067static bool nowayout = WATCHDOG_NOWAYOUT;
68module_param(nowayout, bool, 0);
Florian Fainelli03ec5852008-02-25 13:11:31 +010069MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71
Phil Sutter0af98d32009-02-08 16:44:42 +010072/* apply or and nand masks to data read from addr and write back */
73#define SET_BITS(addr, or, nand) \
74 writel((readl(&addr) | or) & ~nand, &addr)
Florian Fainelli03ec5852008-02-25 13:11:31 +010075
Phil Sutter08eb2e02009-02-08 16:44:42 +010076static int rc32434_wdt_set(int new_timeout)
77{
78 int max_to = WTCOMP2SEC((u32)-1);
79
80 if (new_timeout < 0 || new_timeout > max_to) {
Joe Perches27c766a2012-02-15 15:06:19 -080081 pr_err("timeout value must be between 0 and %d\n", max_to);
Phil Sutter08eb2e02009-02-08 16:44:42 +010082 return -EINVAL;
83 }
84 timeout = new_timeout;
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000085 spin_lock(&rc32434_wdt_device.io_lock);
Phil Sutter08eb2e02009-02-08 16:44:42 +010086 writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000087 spin_unlock(&rc32434_wdt_device.io_lock);
Phil Sutter08eb2e02009-02-08 16:44:42 +010088
89 return 0;
90}
91
Florian Fainelli03ec5852008-02-25 13:11:31 +010092static void rc32434_wdt_start(void)
93{
Phil Sutter0af98d32009-02-08 16:44:42 +010094 u32 or, nand;
Florian Fainelli03ec5852008-02-25 13:11:31 +010095
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +000096 spin_lock(&rc32434_wdt_device.io_lock);
97
Phil Sutter0af98d32009-02-08 16:44:42 +010098 /* zero the counter before enabling */
99 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100100
Phil Sutter0af98d32009-02-08 16:44:42 +0100101 /* don't generate a non-maskable interrupt,
102 * do a warm reset instead */
103 nand = 1 << RC32434_ERR_WNE;
104 or = 1 << RC32434_ERR_WRE;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100105
Phil Sutter0af98d32009-02-08 16:44:42 +0100106 /* reset the ERRCS timeout bit in case it's set */
107 nand |= 1 << RC32434_ERR_WTO;
108
109 SET_BITS(wdt_reg->errcs, or, nand);
110
Phil Sutter08eb2e02009-02-08 16:44:42 +0100111 /* set the timeout (either default or based on module param) */
112 rc32434_wdt_set(timeout);
113
Phil Sutter0af98d32009-02-08 16:44:42 +0100114 /* reset WTC timeout bit and enable WDT */
115 nand = 1 << RC32434_WTC_TO;
116 or = 1 << RC32434_WTC_EN;
117
118 SET_BITS(wdt_reg->wtc, or, nand);
Phil Sutter9b655e02009-02-08 16:44:42 +0100119
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000120 spin_unlock(&rc32434_wdt_device.io_lock);
Joe Perches27c766a2012-02-15 15:06:19 -0800121 pr_info("Started watchdog timer\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100122}
123
124static void rc32434_wdt_stop(void)
125{
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000126 spin_lock(&rc32434_wdt_device.io_lock);
127
Phil Sutter0af98d32009-02-08 16:44:42 +0100128 /* Disable WDT */
129 SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
Phil Sutter9b655e02009-02-08 16:44:42 +0100130
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000131 spin_unlock(&rc32434_wdt_device.io_lock);
Joe Perches27c766a2012-02-15 15:06:19 -0800132 pr_info("Stopped watchdog timer\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100133}
Florian Fainelli03ec5852008-02-25 13:11:31 +0100134
Phil Sutter0af98d32009-02-08 16:44:42 +0100135static void rc32434_wdt_ping(void)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100136{
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000137 spin_lock(&rc32434_wdt_device.io_lock);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100138 writel(0, &wdt_reg->wtcount);
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000139 spin_unlock(&rc32434_wdt_device.io_lock);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100140}
141
142static int rc32434_wdt_open(struct inode *inode, struct file *file)
143{
144 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
145 return -EBUSY;
146
147 if (nowayout)
148 __module_get(THIS_MODULE);
149
Phil Sutter0af98d32009-02-08 16:44:42 +0100150 rc32434_wdt_start();
151 rc32434_wdt_ping();
152
Florian Fainelli03ec5852008-02-25 13:11:31 +0100153 return nonseekable_open(inode, file);
154}
155
156static int rc32434_wdt_release(struct inode *inode, struct file *file)
157{
Phil Sutter0af98d32009-02-08 16:44:42 +0100158 if (expect_close == 42) {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100159 rc32434_wdt_stop();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100160 module_put(THIS_MODULE);
Phil Sutter0af98d32009-02-08 16:44:42 +0100161 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800162 pr_crit("device closed unexpectedly. WDT will not stop!\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100163 rc32434_wdt_ping();
164 }
Florian Fainelli03ec5852008-02-25 13:11:31 +0100165 clear_bit(0, &rc32434_wdt_device.inuse);
166 return 0;
167}
168
169static ssize_t rc32434_wdt_write(struct file *file, const char *data,
170 size_t len, loff_t *ppos)
171{
172 if (len) {
173 if (!nowayout) {
174 size_t i;
175
176 /* In case it was set long ago */
177 expect_close = 0;
178
179 for (i = 0; i != len; i++) {
180 char c;
181 if (get_user(c, data + i))
182 return -EFAULT;
183 if (c == 'V')
Phil Sutter0af98d32009-02-08 16:44:42 +0100184 expect_close = 42;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100185 }
186 }
Phil Sutter0af98d32009-02-08 16:44:42 +0100187 rc32434_wdt_ping();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100188 return len;
189 }
190 return 0;
191}
192
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000193static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
194 unsigned long arg)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100195{
196 void __user *argp = (void __user *)arg;
197 int new_timeout;
198 unsigned int value;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000199 static const struct watchdog_info ident = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100200 .options = WDIOF_SETTIMEOUT |
201 WDIOF_KEEPALIVEPING |
202 WDIOF_MAGICCLOSE,
203 .identity = "RC32434_WDT Watchdog",
204 };
205 switch (cmd) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100206 case WDIOC_GETSUPPORT:
207 if (copy_to_user(argp, &ident, sizeof(ident)))
208 return -EFAULT;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100209 break;
210 case WDIOC_GETSTATUS:
211 case WDIOC_GETBOOTSTATUS:
Phil Sutter0af98d32009-02-08 16:44:42 +0100212 value = 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100213 if (copy_to_user(argp, &value, sizeof(int)))
214 return -EFAULT;
215 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100216 case WDIOC_SETOPTIONS:
217 if (copy_from_user(&value, argp, sizeof(int)))
218 return -EFAULT;
219 switch (value) {
220 case WDIOS_ENABLECARD:
221 rc32434_wdt_start();
222 break;
223 case WDIOS_DISABLECARD:
224 rc32434_wdt_stop();
Phil Sutter0af98d32009-02-08 16:44:42 +0100225 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100226 default:
227 return -EINVAL;
228 }
229 break;
Phil Sutter9b655e02009-02-08 16:44:42 +0100230 case WDIOC_KEEPALIVE:
231 rc32434_wdt_ping();
232 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100233 case WDIOC_SETTIMEOUT:
234 if (copy_from_user(&new_timeout, argp, sizeof(int)))
235 return -EFAULT;
Phil Sutter0af98d32009-02-08 16:44:42 +0100236 if (rc32434_wdt_set(new_timeout))
Florian Fainelli03ec5852008-02-25 13:11:31 +0100237 return -EINVAL;
Phil Sutter0af98d32009-02-08 16:44:42 +0100238 /* Fall through */
Florian Fainelli03ec5852008-02-25 13:11:31 +0100239 case WDIOC_GETTIMEOUT:
Michael S. Tsirkin10e7ac22016-02-28 17:44:09 +0200240 return copy_to_user(argp, &timeout, sizeof(int)) ? -EFAULT : 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100241 default:
242 return -ENOTTY;
243 }
244
245 return 0;
246}
247
Wim Van Sebroeckd5c26a52009-03-18 08:18:43 +0000248static const struct file_operations rc32434_wdt_fops = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100249 .owner = THIS_MODULE,
250 .llseek = no_llseek,
251 .write = rc32434_wdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000252 .unlocked_ioctl = rc32434_wdt_ioctl,
Florian Fainelli03ec5852008-02-25 13:11:31 +0100253 .open = rc32434_wdt_open,
254 .release = rc32434_wdt_release,
255};
256
257static struct miscdevice rc32434_wdt_miscdev = {
258 .minor = WATCHDOG_MINOR,
259 .name = "watchdog",
260 .fops = &rc32434_wdt_fops,
261};
262
Bill Pemberton2d991a12012-11-19 13:21:41 -0500263static int rc32434_wdt_probe(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100264{
265 int ret;
266 struct resource *r;
267
Phil Sutter0af98d32009-02-08 16:44:42 +0100268 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100269 if (!r) {
Joe Perches27c766a2012-02-15 15:06:19 -0800270 pr_err("failed to retrieve resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100271 return -ENODEV;
272 }
273
Jingoo Han52ccc5a2013-04-30 14:01:41 +0900274 wdt_reg = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
Florian Fainelli03ec5852008-02-25 13:11:31 +0100275 if (!wdt_reg) {
Joe Perches27c766a2012-02-15 15:06:19 -0800276 pr_err("failed to remap I/O resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100277 return -ENXIO;
278 }
279
Wim Van Sebroecke455b6b2009-02-23 13:08:36 +0000280 spin_lock_init(&rc32434_wdt_device.io_lock);
281
Wim Van Sebroeckf296b142009-02-23 13:08:37 +0000282 /* Make sure the watchdog is not running */
283 rc32434_wdt_stop();
284
Phil Sutter08eb2e02009-02-08 16:44:42 +0100285 /* Check that the heartbeat value is within it's range;
286 * if not reset to the default */
287 if (rc32434_wdt_set(timeout)) {
288 rc32434_wdt_set(WATCHDOG_TIMEOUT);
Joe Perches27c766a2012-02-15 15:06:19 -0800289 pr_info("timeout value must be between 0 and %d\n",
Phil Sutter08eb2e02009-02-08 16:44:42 +0100290 WTCOMP2SEC((u32)-1));
291 }
292
Florian Fainelli03ec5852008-02-25 13:11:31 +0100293 ret = misc_register(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100294 if (ret < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800295 pr_err("failed to register watchdog device\n");
Jingoo Han52ccc5a2013-04-30 14:01:41 +0900296 return ret;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100297 }
298
Joe Perches27c766a2012-02-15 15:06:19 -0800299 pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n",
300 timeout);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100301
302 return 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100303}
304
Bill Pemberton4b12b892012-11-19 13:26:24 -0500305static int rc32434_wdt_remove(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100306{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100307 misc_deregister(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100308 return 0;
309}
310
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000311static void rc32434_wdt_shutdown(struct platform_device *pdev)
312{
313 rc32434_wdt_stop();
314}
315
Phil Sutter9b655e02009-02-08 16:44:42 +0100316static struct platform_driver rc32434_wdt_driver = {
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000317 .probe = rc32434_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500318 .remove = rc32434_wdt_remove,
Wim Van Sebroeck0aaae662009-02-23 13:08:35 +0000319 .shutdown = rc32434_wdt_shutdown,
320 .driver = {
321 .name = "rc32434_wdt",
Florian Fainelli03ec5852008-02-25 13:11:31 +0100322 }
323};
324
Axel Linb8ec6112011-11-29 13:56:27 +0800325module_platform_driver(rc32434_wdt_driver);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100326
327MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
328 "Florian Fainelli <florian@openwrt.org>");
329MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
330MODULE_LICENSE("GPL");