blob: f20cc53ff71976cdfb48bb956fb83e8fd88b1e91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * drivers/char/watchdog/ixp4xx_wdt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Watchdog driver for Intel IXP4xx network processors
5 *
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 *
8 * Copyright 2004 (c) MontaVista, Software, Inc.
9 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
Joe Perches27c766a2012-02-15 15:06:19 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/types.h>
21#include <linux/kernel.h>
22#include <linux/fs.h>
23#include <linux/miscdevice.h>
24#include <linux/watchdog.h>
25#include <linux/init.h>
26#include <linux/bitops.h>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000027#include <linux/uaccess.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010030static bool nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int heartbeat = 60; /* (secs) Default is 1 minute */
32static unsigned long wdt_status;
33static unsigned long boot_status;
Adrian Bunk92293762008-08-10 14:03:41 +030034static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
37
38#define WDT_IN_USE 0
39#define WDT_OK_TO_CLOSE 1
40
Alan Cox20d35f3e2008-05-19 14:06:48 +010041static void wdt_enable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Alan Cox20d35f3e2008-05-19 14:06:48 +010043 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
45 *IXP4XX_OSWE = 0;
46 *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
47 *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
48 *IXP4XX_OSWK = 0;
Alan Cox20d35f3e2008-05-19 14:06:48 +010049 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Alan Cox20d35f3e2008-05-19 14:06:48 +010052static void wdt_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Alan Cox20d35f3e2008-05-19 14:06:48 +010054 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
56 *IXP4XX_OSWE = 0;
57 *IXP4XX_OSWK = 0;
Alan Cox20d35f3e2008-05-19 14:06:48 +010058 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Alan Cox20d35f3e2008-05-19 14:06:48 +010061static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
64 return -EBUSY;
65
66 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 wdt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return nonseekable_open(inode, file);
69}
70
71static ssize_t
72ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
73{
74 if (len) {
75 if (!nowayout) {
76 size_t i;
77
78 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
79
80 for (i = 0; i != len; i++) {
81 char c;
82
83 if (get_user(c, data + i))
84 return -EFAULT;
85 if (c == 'V')
86 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
87 }
88 }
89 wdt_enable();
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return len;
92}
93
Wim Van Sebroeck42747d72009-12-26 18:55:22 +000094static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
96 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
97 .identity = "IXP4xx Watchdog",
98};
99
100
Alan Cox20d35f3e2008-05-19 14:06:48 +0100101static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
102 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200104 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int time;
106
107 switch (cmd) {
108 case WDIOC_GETSUPPORT:
109 ret = copy_to_user((struct watchdog_info *)arg, &ident,
110 sizeof(ident)) ? -EFAULT : 0;
111 break;
112
113 case WDIOC_GETSTATUS:
114 ret = put_user(0, (int *)arg);
115 break;
116
117 case WDIOC_GETBOOTSTATUS:
118 ret = put_user(boot_status, (int *)arg);
119 break;
120
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000121 case WDIOC_KEEPALIVE:
122 wdt_enable();
123 ret = 0;
124 break;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 case WDIOC_SETTIMEOUT:
127 ret = get_user(time, (int *)arg);
128 if (ret)
129 break;
130
131 if (time <= 0 || time > 60) {
132 ret = -EINVAL;
133 break;
134 }
135
136 heartbeat = time;
137 wdt_enable();
138 /* Fall through */
139
140 case WDIOC_GETTIMEOUT:
141 ret = put_user(heartbeat, (int *)arg);
142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 return ret;
145}
146
Alan Cox20d35f3e2008-05-19 14:06:48 +0100147static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Alan Cox20d35f3e2008-05-19 14:06:48 +0100149 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 wdt_disable();
Alan Cox20d35f3e2008-05-19 14:06:48 +0100151 else
Joe Perches27c766a2012-02-15 15:06:19 -0800152 pr_crit("Device closed unexpectedly - timer will not stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 clear_bit(WDT_IN_USE, &wdt_status);
154 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
155
156 return 0;
157}
158
159
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000160static const struct file_operations ixp4xx_wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 .owner = THIS_MODULE,
162 .llseek = no_llseek,
163 .write = ixp4xx_wdt_write,
Alan Cox20d35f3e2008-05-19 14:06:48 +0100164 .unlocked_ioctl = ixp4xx_wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .open = ixp4xx_wdt_open,
166 .release = ixp4xx_wdt_release,
167};
168
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000169static struct miscdevice ixp4xx_wdt_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .minor = WATCHDOG_MINOR,
Olaf Hering2dab3ca2005-08-17 08:58:34 +0200171 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .fops = &ixp4xx_wdt_fops,
173};
174
175static int __init ixp4xx_wdt_init(void)
176{
177 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Russell King0ba8b9b2008-08-10 18:08:10 +0100179 if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) {
Joe Perches27c766a2012-02-15 15:06:19 -0800180 pr_err("Rev. A0 IXP42x CPU detected - watchdog disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 return -ENODEV;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
185 WDIOF_CARDRESET : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ret = misc_register(&ixp4xx_wdt_miscdev);
187 if (ret == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_info("timer heartbeat %d sec\n", heartbeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return ret;
190}
191
192static void __exit ixp4xx_wdt_exit(void)
193{
194 misc_deregister(&ixp4xx_wdt_miscdev);
195}
196
197
198module_init(ixp4xx_wdt_init);
199module_exit(ixp4xx_wdt_exit);
200
201MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
202MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
203
204module_param(heartbeat, int, 0);
205MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
206
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100207module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
209
210MODULE_LICENSE("GPL");