blob: e7640bc4904b3fb869f75f770027edcdd9022ba9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/watchdog/ixp2000_wdt.c
3 *
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
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/types.h>
23#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>
29
30#include <asm/hardware.h>
31#include <asm/uaccess.h>
32
Andrey Panin4bfdf372005-07-27 11:43:58 -070033static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
35static unsigned long wdt_status;
36
37#define WDT_IN_USE 0
38#define WDT_OK_TO_CLOSE 1
39
40static unsigned long wdt_tick_rate;
41
42static void
43wdt_enable(void)
44{
45 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);
49}
50
51static void
52wdt_disable(void)
53{
54 ixp2000_reg_write(IXP2000_T4_CTL, 0);
55}
56
57static void
58wdt_keepalive(void)
59{
60 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
61}
62
63static int
64ixp2000_wdt_open(struct inode *inode, struct file *file)
65{
66 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
67 return -EBUSY;
68
69 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
70
71 wdt_enable();
72
73 return nonseekable_open(inode, file);
74}
75
76static ssize_t
77ixp2000_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
78{
79 if (len) {
80 if (!nowayout) {
81 size_t i;
82
83 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
84
85 for (i = 0; i != len; i++) {
86 char c;
87
88 if (get_user(c, data + i))
89 return -EFAULT;
90 if (c == 'V')
91 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
92 }
93 }
94 wdt_keepalive();
95 }
96
97 return len;
98}
99
100
101static struct watchdog_info ident = {
102 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
103 WDIOF_KEEPALIVEPING,
104 .identity = "IXP2000 Watchdog",
105};
106
107static int
108ixp2000_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
109 unsigned long arg)
110{
111 int ret = -ENOIOCTLCMD;
112 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
128 case WDIOC_SETTIMEOUT:
129 ret = get_user(time, (int *)arg);
130 if (ret)
131 break;
132
133 if (time <= 0 || time > 60) {
134 ret = -EINVAL;
135 break;
136 }
137
138 heartbeat = time;
139 wdt_keepalive();
140 /* Fall through */
141
142 case WDIOC_GETTIMEOUT:
143 ret = put_user(heartbeat, (int *)arg);
144 break;
145
146 case WDIOC_KEEPALIVE:
147 wdt_enable();
148 ret = 0;
149 break;
150 }
151
152 return ret;
153}
154
155static int
156ixp2000_wdt_release(struct inode *inode, struct file *file)
157{
158 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
159 wdt_disable();
160 } 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");
163 }
164
165 clear_bit(WDT_IN_USE, &wdt_status);
166 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
167
168 return 0;
169}
170
171
172static struct file_operations ixp2000_wdt_fops =
173{
174 .owner = THIS_MODULE,
175 .llseek = no_llseek,
176 .write = ixp2000_wdt_write,
177 .ioctl = ixp2000_wdt_ioctl,
178 .open = ixp2000_wdt_open,
179 .release = ixp2000_wdt_release,
180};
181
182static struct miscdevice ixp2000_wdt_miscdev =
183{
184 .minor = WATCHDOG_MINOR,
185 .name = "IXP2000 Watchdog",
186 .fops = &ixp2000_wdt_fops,
187};
188
189static int __init ixp2000_wdt_init(void)
190{
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100191 if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
192 printk(KERN_INFO "Unable to use IXP2000 watchdog due to IXP2800 erratum #25.\n");
193 return -EIO;
194 }
195
196 wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 return misc_register(&ixp2000_wdt_miscdev);
199}
200
201static void __exit ixp2000_wdt_exit(void)
202{
203 misc_deregister(&ixp2000_wdt_miscdev);
204}
205
206module_init(ixp2000_wdt_init);
207module_exit(ixp2000_wdt_exit);
208
209MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net">);
210MODULE_DESCRIPTION("IXP2000 Network Processor Watchdog");
211
212module_param(heartbeat, int, 0);
213MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
214
215module_param(nowayout, int, 0);
216MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
217
218MODULE_LICENSE("GPL");
219MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
220