12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
203ec5856SFlorian Fainelli /*
303ec5856SFlorian Fainelli * IDT Interprise 79RC32434 watchdog driver
403ec5856SFlorian Fainelli *
503ec5856SFlorian Fainelli * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
603ec5856SFlorian Fainelli * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
703ec5856SFlorian Fainelli *
803ec5856SFlorian Fainelli * based on
903ec5856SFlorian Fainelli * SoftDog 0.05: A Software Watchdog Device
1003ec5856SFlorian Fainelli *
1129fa0586SAlan Cox * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
1229fa0586SAlan Cox * All Rights Reserved.
1303ec5856SFlorian Fainelli */
1403ec5856SFlorian Fainelli
1527c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1627c766aaSJoe Perches
179b655e07SPhil Sutter #include <linux/module.h> /* For module specific items */
189b655e07SPhil Sutter #include <linux/moduleparam.h> /* For new moduleparam's */
199b655e07SPhil Sutter #include <linux/types.h> /* For standard types (like size_t) */
209b655e07SPhil Sutter #include <linux/errno.h> /* For the -ENODEV/... values */
219b655e07SPhil Sutter #include <linux/kernel.h> /* For printk/panic/... */
229b655e07SPhil Sutter #include <linux/fs.h> /* For file operations */
23487722cfSJean Delvare #include <linux/miscdevice.h> /* For struct miscdevice */
249b655e07SPhil Sutter #include <linux/watchdog.h> /* For the watchdog specific items */
259b655e07SPhil Sutter #include <linux/init.h> /* For __init/__exit/... */
269b655e07SPhil Sutter #include <linux/platform_device.h> /* For platform_driver framework */
27e455b6b4SWim Van Sebroeck #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
289b655e07SPhil Sutter #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
294bdc0d67SChristoph Hellwig #include <linux/io.h> /* For devm_ioremap */
3003ec5856SFlorian Fainelli
319b655e07SPhil Sutter #include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
329b655e07SPhil Sutter
33f296b143SWim Van Sebroeck #define VERSION "1.0"
3403ec5856SFlorian Fainelli
3503ec5856SFlorian Fainelli static struct {
3603ec5856SFlorian Fainelli unsigned long inuse;
37e455b6b4SWim Van Sebroeck spinlock_t io_lock;
3803ec5856SFlorian Fainelli } rc32434_wdt_device;
3903ec5856SFlorian Fainelli
4003ec5856SFlorian Fainelli static struct integ __iomem *wdt_reg;
4103ec5856SFlorian Fainelli
4203ec5856SFlorian Fainelli static int expect_close;
430af98d37SPhil Sutter
440af98d37SPhil Sutter /* Board internal clock speed in Hz,
450af98d37SPhil Sutter * the watchdog timer ticks at. */
460af98d37SPhil Sutter extern unsigned int idt_cpu_freq;
470af98d37SPhil Sutter
480af98d37SPhil Sutter /* translate wtcompare value to seconds and vice versa */
490af98d37SPhil Sutter #define WTCOMP2SEC(x) (x / idt_cpu_freq)
500af98d37SPhil Sutter #define SEC2WTCOMP(x) (x * idt_cpu_freq)
510af98d37SPhil Sutter
520af98d37SPhil Sutter /* Use a default timeout of 20s. This should be
530af98d37SPhil Sutter * safe for CPU clock speeds up to 400MHz, as
540af98d37SPhil Sutter * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
550af98d37SPhil Sutter #define WATCHDOG_TIMEOUT 20
560af98d37SPhil Sutter
570af98d37SPhil Sutter static int timeout = WATCHDOG_TIMEOUT;
5808eb2e0cSPhil Sutter module_param(timeout, int, 0);
5908eb2e0cSPhil Sutter MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
60810a90aeSFlorian Fainelli __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
6103ec5856SFlorian Fainelli
6286a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT;
6386a1e189SWim Van Sebroeck module_param(nowayout, bool, 0);
6403ec5856SFlorian Fainelli MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
6503ec5856SFlorian Fainelli __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
6603ec5856SFlorian Fainelli
670af98d37SPhil Sutter /* apply or and nand masks to data read from addr and write back */
680af98d37SPhil Sutter #define SET_BITS(addr, or, nand) \
690af98d37SPhil Sutter writel((readl(&addr) | or) & ~nand, &addr)
7003ec5856SFlorian Fainelli
rc32434_wdt_set(int new_timeout)7108eb2e0cSPhil Sutter static int rc32434_wdt_set(int new_timeout)
7208eb2e0cSPhil Sutter {
7308eb2e0cSPhil Sutter int max_to = WTCOMP2SEC((u32)-1);
7408eb2e0cSPhil Sutter
7508eb2e0cSPhil Sutter if (new_timeout < 0 || new_timeout > max_to) {
7627c766aaSJoe Perches pr_err("timeout value must be between 0 and %d\n", max_to);
7708eb2e0cSPhil Sutter return -EINVAL;
7808eb2e0cSPhil Sutter }
7908eb2e0cSPhil Sutter timeout = new_timeout;
80e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock);
8108eb2e0cSPhil Sutter writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
82e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock);
8308eb2e0cSPhil Sutter
8408eb2e0cSPhil Sutter return 0;
8508eb2e0cSPhil Sutter }
8608eb2e0cSPhil Sutter
rc32434_wdt_start(void)8703ec5856SFlorian Fainelli static void rc32434_wdt_start(void)
8803ec5856SFlorian Fainelli {
890af98d37SPhil Sutter u32 or, nand;
9003ec5856SFlorian Fainelli
91e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock);
92e455b6b4SWim Van Sebroeck
930af98d37SPhil Sutter /* zero the counter before enabling */
9403ec5856SFlorian Fainelli writel(0, &wdt_reg->wtcount);
9503ec5856SFlorian Fainelli
960af98d37SPhil Sutter /* don't generate a non-maskable interrupt,
970af98d37SPhil Sutter * do a warm reset instead */
980af98d37SPhil Sutter nand = 1 << RC32434_ERR_WNE;
990af98d37SPhil Sutter or = 1 << RC32434_ERR_WRE;
10003ec5856SFlorian Fainelli
1010af98d37SPhil Sutter /* reset the ERRCS timeout bit in case it's set */
1020af98d37SPhil Sutter nand |= 1 << RC32434_ERR_WTO;
1030af98d37SPhil Sutter
1040af98d37SPhil Sutter SET_BITS(wdt_reg->errcs, or, nand);
1050af98d37SPhil Sutter
10608eb2e0cSPhil Sutter /* set the timeout (either default or based on module param) */
10708eb2e0cSPhil Sutter rc32434_wdt_set(timeout);
10808eb2e0cSPhil Sutter
1090af98d37SPhil Sutter /* reset WTC timeout bit and enable WDT */
1100af98d37SPhil Sutter nand = 1 << RC32434_WTC_TO;
1110af98d37SPhil Sutter or = 1 << RC32434_WTC_EN;
1120af98d37SPhil Sutter
1130af98d37SPhil Sutter SET_BITS(wdt_reg->wtc, or, nand);
1149b655e07SPhil Sutter
115e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock);
11627c766aaSJoe Perches pr_info("Started watchdog timer\n");
11703ec5856SFlorian Fainelli }
11803ec5856SFlorian Fainelli
rc32434_wdt_stop(void)11903ec5856SFlorian Fainelli static void rc32434_wdt_stop(void)
12003ec5856SFlorian Fainelli {
121e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock);
122e455b6b4SWim Van Sebroeck
1230af98d37SPhil Sutter /* Disable WDT */
1240af98d37SPhil Sutter SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
1259b655e07SPhil Sutter
126e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock);
12727c766aaSJoe Perches pr_info("Stopped watchdog timer\n");
12803ec5856SFlorian Fainelli }
12903ec5856SFlorian Fainelli
rc32434_wdt_ping(void)1300af98d37SPhil Sutter static void rc32434_wdt_ping(void)
13103ec5856SFlorian Fainelli {
132e455b6b4SWim Van Sebroeck spin_lock(&rc32434_wdt_device.io_lock);
13303ec5856SFlorian Fainelli writel(0, &wdt_reg->wtcount);
134e455b6b4SWim Van Sebroeck spin_unlock(&rc32434_wdt_device.io_lock);
13503ec5856SFlorian Fainelli }
13603ec5856SFlorian Fainelli
rc32434_wdt_open(struct inode * inode,struct file * file)13703ec5856SFlorian Fainelli static int rc32434_wdt_open(struct inode *inode, struct file *file)
13803ec5856SFlorian Fainelli {
13903ec5856SFlorian Fainelli if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
14003ec5856SFlorian Fainelli return -EBUSY;
14103ec5856SFlorian Fainelli
14203ec5856SFlorian Fainelli if (nowayout)
14303ec5856SFlorian Fainelli __module_get(THIS_MODULE);
14403ec5856SFlorian Fainelli
1450af98d37SPhil Sutter rc32434_wdt_start();
1460af98d37SPhil Sutter rc32434_wdt_ping();
1470af98d37SPhil Sutter
148c5bf68feSKirill Smelkov return stream_open(inode, file);
14903ec5856SFlorian Fainelli }
15003ec5856SFlorian Fainelli
rc32434_wdt_release(struct inode * inode,struct file * file)15103ec5856SFlorian Fainelli static int rc32434_wdt_release(struct inode *inode, struct file *file)
15203ec5856SFlorian Fainelli {
1530af98d37SPhil Sutter if (expect_close == 42) {
15403ec5856SFlorian Fainelli rc32434_wdt_stop();
15503ec5856SFlorian Fainelli module_put(THIS_MODULE);
1560af98d37SPhil Sutter } else {
15727c766aaSJoe Perches pr_crit("device closed unexpectedly. WDT will not stop!\n");
1580af98d37SPhil Sutter rc32434_wdt_ping();
1590af98d37SPhil Sutter }
16003ec5856SFlorian Fainelli clear_bit(0, &rc32434_wdt_device.inuse);
16103ec5856SFlorian Fainelli return 0;
16203ec5856SFlorian Fainelli }
16303ec5856SFlorian Fainelli
rc32434_wdt_write(struct file * file,const char * data,size_t len,loff_t * ppos)16403ec5856SFlorian Fainelli static ssize_t rc32434_wdt_write(struct file *file, const char *data,
16503ec5856SFlorian Fainelli size_t len, loff_t *ppos)
16603ec5856SFlorian Fainelli {
16703ec5856SFlorian Fainelli if (len) {
16803ec5856SFlorian Fainelli if (!nowayout) {
16903ec5856SFlorian Fainelli size_t i;
17003ec5856SFlorian Fainelli
17103ec5856SFlorian Fainelli /* In case it was set long ago */
17203ec5856SFlorian Fainelli expect_close = 0;
17303ec5856SFlorian Fainelli
17403ec5856SFlorian Fainelli for (i = 0; i != len; i++) {
17503ec5856SFlorian Fainelli char c;
17603ec5856SFlorian Fainelli if (get_user(c, data + i))
17703ec5856SFlorian Fainelli return -EFAULT;
17803ec5856SFlorian Fainelli if (c == 'V')
1790af98d37SPhil Sutter expect_close = 42;
18003ec5856SFlorian Fainelli }
18103ec5856SFlorian Fainelli }
1820af98d37SPhil Sutter rc32434_wdt_ping();
18303ec5856SFlorian Fainelli return len;
18403ec5856SFlorian Fainelli }
18503ec5856SFlorian Fainelli return 0;
18603ec5856SFlorian Fainelli }
18703ec5856SFlorian Fainelli
rc32434_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1887275fc8cSWim Van Sebroeck static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
1897275fc8cSWim Van Sebroeck unsigned long arg)
19003ec5856SFlorian Fainelli {
19103ec5856SFlorian Fainelli void __user *argp = (void __user *)arg;
19203ec5856SFlorian Fainelli int new_timeout;
19303ec5856SFlorian Fainelli unsigned int value;
19442747d71SWim Van Sebroeck static const struct watchdog_info ident = {
19503ec5856SFlorian Fainelli .options = WDIOF_SETTIMEOUT |
19603ec5856SFlorian Fainelli WDIOF_KEEPALIVEPING |
19703ec5856SFlorian Fainelli WDIOF_MAGICCLOSE,
19803ec5856SFlorian Fainelli .identity = "RC32434_WDT Watchdog",
19903ec5856SFlorian Fainelli };
20003ec5856SFlorian Fainelli switch (cmd) {
2019b655e07SPhil Sutter case WDIOC_GETSUPPORT:
2029b655e07SPhil Sutter if (copy_to_user(argp, &ident, sizeof(ident)))
2039b655e07SPhil Sutter return -EFAULT;
20403ec5856SFlorian Fainelli break;
20503ec5856SFlorian Fainelli case WDIOC_GETSTATUS:
20603ec5856SFlorian Fainelli case WDIOC_GETBOOTSTATUS:
2070af98d37SPhil Sutter value = 0;
20803ec5856SFlorian Fainelli if (copy_to_user(argp, &value, sizeof(int)))
20903ec5856SFlorian Fainelli return -EFAULT;
21003ec5856SFlorian Fainelli break;
21103ec5856SFlorian Fainelli case WDIOC_SETOPTIONS:
21203ec5856SFlorian Fainelli if (copy_from_user(&value, argp, sizeof(int)))
21303ec5856SFlorian Fainelli return -EFAULT;
21403ec5856SFlorian Fainelli switch (value) {
21503ec5856SFlorian Fainelli case WDIOS_ENABLECARD:
21603ec5856SFlorian Fainelli rc32434_wdt_start();
21703ec5856SFlorian Fainelli break;
21803ec5856SFlorian Fainelli case WDIOS_DISABLECARD:
21903ec5856SFlorian Fainelli rc32434_wdt_stop();
2200af98d37SPhil Sutter break;
22103ec5856SFlorian Fainelli default:
22203ec5856SFlorian Fainelli return -EINVAL;
22303ec5856SFlorian Fainelli }
22403ec5856SFlorian Fainelli break;
2259b655e07SPhil Sutter case WDIOC_KEEPALIVE:
2269b655e07SPhil Sutter rc32434_wdt_ping();
2279b655e07SPhil Sutter break;
22803ec5856SFlorian Fainelli case WDIOC_SETTIMEOUT:
22903ec5856SFlorian Fainelli if (copy_from_user(&new_timeout, argp, sizeof(int)))
23003ec5856SFlorian Fainelli return -EFAULT;
2310af98d37SPhil Sutter if (rc32434_wdt_set(new_timeout))
23203ec5856SFlorian Fainelli return -EINVAL;
233bd490f82SGustavo A. R. Silva fallthrough;
23403ec5856SFlorian Fainelli case WDIOC_GETTIMEOUT:
23510e7ac22SMichael S. Tsirkin return copy_to_user(argp, &timeout, sizeof(int)) ? -EFAULT : 0;
23603ec5856SFlorian Fainelli default:
23703ec5856SFlorian Fainelli return -ENOTTY;
23803ec5856SFlorian Fainelli }
23903ec5856SFlorian Fainelli
24003ec5856SFlorian Fainelli return 0;
24103ec5856SFlorian Fainelli }
24203ec5856SFlorian Fainelli
243d5c26a59SWim Van Sebroeck static const struct file_operations rc32434_wdt_fops = {
24403ec5856SFlorian Fainelli .owner = THIS_MODULE,
24503ec5856SFlorian Fainelli .llseek = no_llseek,
24603ec5856SFlorian Fainelli .write = rc32434_wdt_write,
2477275fc8cSWim Van Sebroeck .unlocked_ioctl = rc32434_wdt_ioctl,
248b6dfb247SArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
24903ec5856SFlorian Fainelli .open = rc32434_wdt_open,
25003ec5856SFlorian Fainelli .release = rc32434_wdt_release,
25103ec5856SFlorian Fainelli };
25203ec5856SFlorian Fainelli
25303ec5856SFlorian Fainelli static struct miscdevice rc32434_wdt_miscdev = {
25403ec5856SFlorian Fainelli .minor = WATCHDOG_MINOR,
25503ec5856SFlorian Fainelli .name = "watchdog",
25603ec5856SFlorian Fainelli .fops = &rc32434_wdt_fops,
25703ec5856SFlorian Fainelli };
25803ec5856SFlorian Fainelli
rc32434_wdt_probe(struct platform_device * pdev)2592d991a16SBill Pemberton static int rc32434_wdt_probe(struct platform_device *pdev)
26003ec5856SFlorian Fainelli {
26103ec5856SFlorian Fainelli int ret;
26203ec5856SFlorian Fainelli struct resource *r;
26303ec5856SFlorian Fainelli
2640af98d37SPhil Sutter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
26503ec5856SFlorian Fainelli if (!r) {
26627c766aaSJoe Perches pr_err("failed to retrieve resources\n");
26703ec5856SFlorian Fainelli return -ENODEV;
26803ec5856SFlorian Fainelli }
26903ec5856SFlorian Fainelli
2704bdc0d67SChristoph Hellwig wdt_reg = devm_ioremap(&pdev->dev, r->start, resource_size(r));
27103ec5856SFlorian Fainelli if (!wdt_reg) {
27227c766aaSJoe Perches pr_err("failed to remap I/O resources\n");
27303ec5856SFlorian Fainelli return -ENXIO;
27403ec5856SFlorian Fainelli }
27503ec5856SFlorian Fainelli
276e455b6b4SWim Van Sebroeck spin_lock_init(&rc32434_wdt_device.io_lock);
277e455b6b4SWim Van Sebroeck
278f296b143SWim Van Sebroeck /* Make sure the watchdog is not running */
279f296b143SWim Van Sebroeck rc32434_wdt_stop();
280f296b143SWim Van Sebroeck
28108eb2e0cSPhil Sutter /* Check that the heartbeat value is within it's range;
28208eb2e0cSPhil Sutter * if not reset to the default */
28308eb2e0cSPhil Sutter if (rc32434_wdt_set(timeout)) {
28408eb2e0cSPhil Sutter rc32434_wdt_set(WATCHDOG_TIMEOUT);
28527c766aaSJoe Perches pr_info("timeout value must be between 0 and %d\n",
28608eb2e0cSPhil Sutter WTCOMP2SEC((u32)-1));
28708eb2e0cSPhil Sutter }
28808eb2e0cSPhil Sutter
28903ec5856SFlorian Fainelli ret = misc_register(&rc32434_wdt_miscdev);
29003ec5856SFlorian Fainelli if (ret < 0) {
29127c766aaSJoe Perches pr_err("failed to register watchdog device\n");
29252ccc5acSJingoo Han return ret;
29303ec5856SFlorian Fainelli }
29403ec5856SFlorian Fainelli
29527c766aaSJoe Perches pr_info("Watchdog Timer version " VERSION ", timer margin: %d sec\n",
29627c766aaSJoe Perches timeout);
29703ec5856SFlorian Fainelli
29803ec5856SFlorian Fainelli return 0;
29903ec5856SFlorian Fainelli }
30003ec5856SFlorian Fainelli
rc32434_wdt_remove(struct platform_device * pdev)301*c5504409SUwe Kleine-König static void rc32434_wdt_remove(struct platform_device *pdev)
30203ec5856SFlorian Fainelli {
30303ec5856SFlorian Fainelli misc_deregister(&rc32434_wdt_miscdev);
30403ec5856SFlorian Fainelli }
30503ec5856SFlorian Fainelli
rc32434_wdt_shutdown(struct platform_device * pdev)3060aaae661SWim Van Sebroeck static void rc32434_wdt_shutdown(struct platform_device *pdev)
3070aaae661SWim Van Sebroeck {
3080aaae661SWim Van Sebroeck rc32434_wdt_stop();
3090aaae661SWim Van Sebroeck }
3100aaae661SWim Van Sebroeck
3119b655e07SPhil Sutter static struct platform_driver rc32434_wdt_driver = {
31203ec5856SFlorian Fainelli .probe = rc32434_wdt_probe,
313*c5504409SUwe Kleine-König .remove_new = rc32434_wdt_remove,
3140aaae661SWim Van Sebroeck .shutdown = rc32434_wdt_shutdown,
31503ec5856SFlorian Fainelli .driver = {
31603ec5856SFlorian Fainelli .name = "rc32434_wdt",
31703ec5856SFlorian Fainelli }
31803ec5856SFlorian Fainelli };
31903ec5856SFlorian Fainelli
320b8ec6118SAxel Lin module_platform_driver(rc32434_wdt_driver);
32103ec5856SFlorian Fainelli
32203ec5856SFlorian Fainelli MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
32303ec5856SFlorian Fainelli "Florian Fainelli <florian@openwrt.org>");
32403ec5856SFlorian Fainelli MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
32503ec5856SFlorian Fainelli MODULE_LICENSE("GPL");
326