blob: e86952a7168c8b247ea5e9252c1977ba38bde94f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * drivers/char/watchdog/ixp2000_wdt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Watchdog driver for Intel IXP2000 network processors
5 *
6 * Adapted from the IXP4xx watchdog driver by Lennert Buytenhek.
7 * The original version carries these notices:
8 *
9 * Author: Deepak Saxena <dsaxena@plexity.net>
10 *
11 * Copyright 2004 (c) MontaVista, Software, Inc.
12 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
13 *
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/types.h>
Peter Huewee605d552010-01-07 03:35:49 +010022#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/fs.h>
25#include <linux/miscdevice.h>
26#include <linux/watchdog.h>
27#include <linux/init.h>
28#include <linux/bitops.h>
Alan Cox640b4f62008-05-19 14:06:42 +010029#include <linux/uaccess.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Andrey Panin4bfdf372005-07-27 11:43:58 -070032static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
34static unsigned long wdt_status;
Alan Cox640b4f62008-05-19 14:06:42 +010035static spinlock_t wdt_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define WDT_IN_USE 0
38#define WDT_OK_TO_CLOSE 1
39
40static unsigned long wdt_tick_rate;
41
Alan Cox640b4f62008-05-19 14:06:42 +010042static void wdt_enable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Alan Cox640b4f62008-05-19 14:06:42 +010044 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
46 ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
47 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
48 ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
Alan Cox640b4f62008-05-19 14:06:42 +010049 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Alan Cox640b4f62008-05-19 14:06:42 +010052static void wdt_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Alan Cox640b4f62008-05-19 14:06:42 +010054 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ixp2000_reg_write(IXP2000_T4_CTL, 0);
Alan Cox640b4f62008-05-19 14:06:42 +010056 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Alan Cox640b4f62008-05-19 14:06:42 +010059static void wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Alan Cox640b4f62008-05-19 14:06:42 +010061 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
Alan Cox640b4f62008-05-19 14:06:42 +010063 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Alan Cox640b4f62008-05-19 14:06:42 +010066static int ixp2000_wdt_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
69 return -EBUSY;
70
71 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
72
73 wdt_enable();
74
75 return nonseekable_open(inode, file);
76}
77
Alan Cox640b4f62008-05-19 14:06:42 +010078static ssize_t ixp2000_wdt_write(struct file *file, const char *data,
79 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 if (len) {
82 if (!nowayout) {
83 size_t i;
84
85 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
86
87 for (i = 0; i != len; i++) {
88 char c;
89
90 if (get_user(c, data + i))
91 return -EFAULT;
92 if (c == 'V')
93 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
94 }
95 }
96 wdt_keepalive();
97 }
98
99 return len;
100}
101
102
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000103static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
105 WDIOF_KEEPALIVEPING,
106 .identity = "IXP2000 Watchdog",
107};
108
Alan Cox640b4f62008-05-19 14:06:42 +0100109static long ixp2000_wdt_ioctl(struct file *file, unsigned int cmd,
110 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200112 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int time;
114
115 switch (cmd) {
116 case WDIOC_GETSUPPORT:
117 ret = copy_to_user((struct watchdog_info *)arg, &ident,
118 sizeof(ident)) ? -EFAULT : 0;
119 break;
120
121 case WDIOC_GETSTATUS:
122 ret = put_user(0, (int *)arg);
123 break;
124
125 case WDIOC_GETBOOTSTATUS:
126 ret = put_user(0, (int *)arg);
127 break;
128
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000129 case WDIOC_KEEPALIVE:
130 wdt_enable();
131 ret = 0;
132 break;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 case WDIOC_SETTIMEOUT:
135 ret = get_user(time, (int *)arg);
136 if (ret)
137 break;
138
139 if (time <= 0 || time > 60) {
140 ret = -EINVAL;
141 break;
142 }
143
144 heartbeat = time;
145 wdt_keepalive();
146 /* Fall through */
147
148 case WDIOC_GETTIMEOUT:
149 ret = put_user(heartbeat, (int *)arg);
150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 return ret;
154}
155
Alan Cox640b4f62008-05-19 14:06:42 +0100156static int ixp2000_wdt_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Alan Cox640b4f62008-05-19 14:06:42 +0100158 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 wdt_disable();
Alan Cox640b4f62008-05-19 14:06:42 +0100160 else
Lennert Buytenhekb36bbb62005-06-28 20:44:46 -0700161 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 "timer will not stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 clear_bit(WDT_IN_USE, &wdt_status);
164 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
165
166 return 0;
167}
168
169
Alan Cox640b4f62008-05-19 14:06:42 +0100170static const struct file_operations ixp2000_wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .owner = THIS_MODULE,
172 .llseek = no_llseek,
173 .write = ixp2000_wdt_write,
Alan Cox640b4f62008-05-19 14:06:42 +0100174 .unlocked_ioctl = ixp2000_wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .open = ixp2000_wdt_open,
176 .release = ixp2000_wdt_release,
177};
178
Alan Cox640b4f62008-05-19 14:06:42 +0100179static struct miscdevice ixp2000_wdt_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .minor = WATCHDOG_MINOR,
Olaf Hering2dab3ca2005-08-17 08:58:34 +0200181 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .fops = &ixp2000_wdt_fops,
183};
184
185static int __init ixp2000_wdt_init(void)
186{
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100187 if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
188 printk(KERN_INFO "Unable to use IXP2000 watchdog due to IXP2800 erratum #25.\n");
189 return -EIO;
190 }
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100191 wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
Alan Cox640b4f62008-05-19 14:06:42 +0100192 spin_lock_init(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return misc_register(&ixp2000_wdt_miscdev);
194}
195
196static void __exit ixp2000_wdt_exit(void)
197{
198 misc_deregister(&ixp2000_wdt_miscdev);
199}
200
201module_init(ixp2000_wdt_init);
202module_exit(ixp2000_wdt_exit);
203
Yoann Padioleau632155e2007-06-01 00:46:35 -0700204MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205MODULE_DESCRIPTION("IXP2000 Network Processor Watchdog");
206
207module_param(heartbeat, int, 0);
208MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
209
210module_param(nowayout, int, 0);
211MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
212
213MODULE_LICENSE("GPL");
214MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
215