blob: 4f4b35a20d84ed6642fb789b49e6c601b8742d86 [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>
22#include <linux/kernel.h>
23#include <linux/fs.h>
24#include <linux/miscdevice.h>
25#include <linux/watchdog.h>
26#include <linux/init.h>
27#include <linux/bitops.h>
Alan Cox640b4f62008-05-19 14:06:42 +010028#include <linux/uaccess.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Andrey Panin4bfdf372005-07-27 11:43:58 -070031static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
33static unsigned long wdt_status;
Alan Cox640b4f62008-05-19 14:06:42 +010034static spinlock_t wdt_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define WDT_IN_USE 0
37#define WDT_OK_TO_CLOSE 1
38
39static unsigned long wdt_tick_rate;
40
Alan Cox640b4f62008-05-19 14:06:42 +010041static void wdt_enable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Alan Cox640b4f62008-05-19 14:06:42 +010043 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
45 ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
46 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
47 ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
Alan Cox640b4f62008-05-19 14:06:42 +010048 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Alan Cox640b4f62008-05-19 14:06:42 +010051static void wdt_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Alan Cox640b4f62008-05-19 14:06:42 +010053 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ixp2000_reg_write(IXP2000_T4_CTL, 0);
Alan Cox640b4f62008-05-19 14:06:42 +010055 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Alan Cox640b4f62008-05-19 14:06:42 +010058static void wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Alan Cox640b4f62008-05-19 14:06:42 +010060 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
Alan Cox640b4f62008-05-19 14:06:42 +010062 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Alan Cox640b4f62008-05-19 14:06:42 +010065static int ixp2000_wdt_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
68 return -EBUSY;
69
70 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
71
72 wdt_enable();
73
74 return nonseekable_open(inode, file);
75}
76
Alan Cox640b4f62008-05-19 14:06:42 +010077static ssize_t ixp2000_wdt_write(struct file *file, const char *data,
78 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 if (len) {
81 if (!nowayout) {
82 size_t i;
83
84 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
85
86 for (i = 0; i != len; i++) {
87 char c;
88
89 if (get_user(c, data + i))
90 return -EFAULT;
91 if (c == 'V')
92 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
93 }
94 }
95 wdt_keepalive();
96 }
97
98 return len;
99}
100
101
102static struct watchdog_info ident = {
103 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
104 WDIOF_KEEPALIVEPING,
105 .identity = "IXP2000 Watchdog",
106};
107
Alan Cox640b4f62008-05-19 14:06:42 +0100108static long ixp2000_wdt_ioctl(struct file *file, unsigned int cmd,
109 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200111 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int time;
113
114 switch (cmd) {
115 case WDIOC_GETSUPPORT:
116 ret = copy_to_user((struct watchdog_info *)arg, &ident,
117 sizeof(ident)) ? -EFAULT : 0;
118 break;
119
120 case WDIOC_GETSTATUS:
121 ret = put_user(0, (int *)arg);
122 break;
123
124 case WDIOC_GETBOOTSTATUS:
125 ret = put_user(0, (int *)arg);
126 break;
127
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000128 case WDIOC_KEEPALIVE:
129 wdt_enable();
130 ret = 0;
131 break;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 case WDIOC_SETTIMEOUT:
134 ret = get_user(time, (int *)arg);
135 if (ret)
136 break;
137
138 if (time <= 0 || time > 60) {
139 ret = -EINVAL;
140 break;
141 }
142
143 heartbeat = time;
144 wdt_keepalive();
145 /* Fall through */
146
147 case WDIOC_GETTIMEOUT:
148 ret = put_user(heartbeat, (int *)arg);
149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 return ret;
153}
154
Alan Cox640b4f62008-05-19 14:06:42 +0100155static int ixp2000_wdt_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Alan Cox640b4f62008-05-19 14:06:42 +0100157 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 wdt_disable();
Alan Cox640b4f62008-05-19 14:06:42 +0100159 else
Lennert Buytenhekb36bbb62005-06-28 20:44:46 -0700160 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 "timer will not stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 clear_bit(WDT_IN_USE, &wdt_status);
163 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
164
165 return 0;
166}
167
168
Alan Cox640b4f62008-05-19 14:06:42 +0100169static const struct file_operations ixp2000_wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .owner = THIS_MODULE,
171 .llseek = no_llseek,
172 .write = ixp2000_wdt_write,
Alan Cox640b4f62008-05-19 14:06:42 +0100173 .unlocked_ioctl = ixp2000_wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .open = ixp2000_wdt_open,
175 .release = ixp2000_wdt_release,
176};
177
Alan Cox640b4f62008-05-19 14:06:42 +0100178static struct miscdevice ixp2000_wdt_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .minor = WATCHDOG_MINOR,
Olaf Hering2dab3ca2005-08-17 08:58:34 +0200180 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .fops = &ixp2000_wdt_fops,
182};
183
184static int __init ixp2000_wdt_init(void)
185{
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100186 if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
187 printk(KERN_INFO "Unable to use IXP2000 watchdog due to IXP2800 erratum #25.\n");
188 return -EIO;
189 }
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100190 wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
Alan Cox640b4f62008-05-19 14:06:42 +0100191 spin_lock_init(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return misc_register(&ixp2000_wdt_miscdev);
193}
194
195static void __exit ixp2000_wdt_exit(void)
196{
197 misc_deregister(&ixp2000_wdt_miscdev);
198}
199
200module_init(ixp2000_wdt_init);
201module_exit(ixp2000_wdt_exit);
202
Yoann Padioleau632155e2007-06-01 00:46:35 -0700203MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204MODULE_DESCRIPTION("IXP2000 Network Processor Watchdog");
205
206module_param(heartbeat, int, 0);
207MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
208
209module_param(nowayout, int, 0);
210MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
211
212MODULE_LICENSE("GPL");
213MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
214