blob: 9db5cf2c38c37064334875b42c35c93dbce15126 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/watchdog/ixp4xx_wdt.c
3 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/fs.h>
21#include <linux/miscdevice.h>
22#include <linux/watchdog.h>
23#include <linux/init.h>
24#include <linux/bitops.h>
25
26#include <asm/hardware.h>
27#include <asm/uaccess.h>
28
Andrey Panin4bfdf372005-07-27 11:43:58 -070029static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int heartbeat = 60; /* (secs) Default is 1 minute */
31static unsigned long wdt_status;
32static unsigned long boot_status;
33
34#define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
35
36#define WDT_IN_USE 0
37#define WDT_OK_TO_CLOSE 1
38
39static void
40wdt_enable(void)
41{
42 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
43 *IXP4XX_OSWE = 0;
44 *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
45 *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
46 *IXP4XX_OSWK = 0;
47}
48
49static void
50wdt_disable(void)
51{
52 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
53 *IXP4XX_OSWE = 0;
54 *IXP4XX_OSWK = 0;
55}
56
57static int
58ixp4xx_wdt_open(struct inode *inode, struct file *file)
59{
60 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
61 return -EBUSY;
62
63 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
64
65 wdt_enable();
66
67 return nonseekable_open(inode, file);
68}
69
70static ssize_t
71ixp4xx_wdt_write(struct file *file, const char *data, size_t len, loff_t *ppos)
72{
73 if (len) {
74 if (!nowayout) {
75 size_t i;
76
77 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
78
79 for (i = 0; i != len; i++) {
80 char c;
81
82 if (get_user(c, data + i))
83 return -EFAULT;
84 if (c == 'V')
85 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
86 }
87 }
88 wdt_enable();
89 }
90
91 return len;
92}
93
94static struct watchdog_info ident = {
95 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
96 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
97 .identity = "IXP4xx Watchdog",
98};
99
100
101static int
102ixp4xx_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
103 unsigned long arg)
104{
105 int ret = -ENOIOCTLCMD;
106 int time;
107
108 switch (cmd) {
109 case WDIOC_GETSUPPORT:
110 ret = copy_to_user((struct watchdog_info *)arg, &ident,
111 sizeof(ident)) ? -EFAULT : 0;
112 break;
113
114 case WDIOC_GETSTATUS:
115 ret = put_user(0, (int *)arg);
116 break;
117
118 case WDIOC_GETBOOTSTATUS:
119 ret = put_user(boot_status, (int *)arg);
120 break;
121
122 case WDIOC_SETTIMEOUT:
123 ret = get_user(time, (int *)arg);
124 if (ret)
125 break;
126
127 if (time <= 0 || time > 60) {
128 ret = -EINVAL;
129 break;
130 }
131
132 heartbeat = time;
133 wdt_enable();
134 /* Fall through */
135
136 case WDIOC_GETTIMEOUT:
137 ret = put_user(heartbeat, (int *)arg);
138 break;
139
140 case WDIOC_KEEPALIVE:
141 wdt_enable();
142 ret = 0;
143 break;
144 }
145 return ret;
146}
147
148static int
149ixp4xx_wdt_release(struct inode *inode, struct file *file)
150{
151 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
152 wdt_disable();
153 } else {
Lennert Buytenhekb36bbb62005-06-28 20:44:46 -0700154 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 "timer will not stop\n");
156 }
157
158 clear_bit(WDT_IN_USE, &wdt_status);
159 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
160
161 return 0;
162}
163
164
Arjan van de Ven62322d22006-07-03 00:24:21 -0700165static const struct file_operations ixp4xx_wdt_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 .owner = THIS_MODULE,
168 .llseek = no_llseek,
169 .write = ixp4xx_wdt_write,
170 .ioctl = ixp4xx_wdt_ioctl,
171 .open = ixp4xx_wdt_open,
172 .release = ixp4xx_wdt_release,
173};
174
175static struct miscdevice ixp4xx_wdt_miscdev =
176{
177 .minor = WATCHDOG_MINOR,
Olaf Hering2dab3ca2005-08-17 08:58:34 +0200178 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .fops = &ixp4xx_wdt_fops,
180};
181
182static int __init ixp4xx_wdt_init(void)
183{
184 int ret;
185 unsigned long processor_id;
186
187 asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
Deepak Saxena3adfd4e2006-01-03 12:50:30 -0800188 if (!(processor_id & 0xf) && !cpu_is_ixp46x()) {
189 printk("IXP4XXX Watchdog: Rev. A0 IXP42x CPU detected - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 "watchdog disabled\n");
191
192 return -ENODEV;
193 }
194
195 ret = misc_register(&ixp4xx_wdt_miscdev);
196 if (ret == 0)
197 printk("IXP4xx Watchdog Timer: heartbeat %d sec\n", heartbeat);
198
199 boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
200 WDIOF_CARDRESET : 0;
201
202 return ret;
203}
204
205static void __exit ixp4xx_wdt_exit(void)
206{
207 misc_deregister(&ixp4xx_wdt_miscdev);
208}
209
210
211module_init(ixp4xx_wdt_init);
212module_exit(ixp4xx_wdt_exit);
213
214MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
215MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
216
217module_param(heartbeat, int, 0);
218MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
219
220module_param(nowayout, int, 0);
221MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
222
223MODULE_LICENSE("GPL");
224MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
225