blob: ef3157dc9ac1349329563027df8fe21a0f894ba2 [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
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>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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;
Alan Cox20d35f32008-05-19 14:06:48 +010033static spin_lock_t wdt_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define WDT_TICK_RATE (IXP4XX_PERIPHERAL_BUS_CLOCK * 1000000UL)
36
37#define WDT_IN_USE 0
38#define WDT_OK_TO_CLOSE 1
39
Alan Cox20d35f32008-05-19 14:06:48 +010040static void wdt_enable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Alan Cox20d35f32008-05-19 14:06:48 +010042 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
44 *IXP4XX_OSWE = 0;
45 *IXP4XX_OSWT = WDT_TICK_RATE * heartbeat;
46 *IXP4XX_OSWE = IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE;
47 *IXP4XX_OSWK = 0;
Alan Cox20d35f32008-05-19 14:06:48 +010048 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Alan Cox20d35f32008-05-19 14:06:48 +010051static void wdt_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Alan Cox20d35f32008-05-19 14:06:48 +010053 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 *IXP4XX_OSWK = IXP4XX_WDT_KEY;
55 *IXP4XX_OSWE = 0;
56 *IXP4XX_OSWK = 0;
Alan Cox20d35f32008-05-19 14:06:48 +010057 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Alan Cox20d35f32008-05-19 14:06:48 +010060static int ixp4xx_wdt_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
63 return -EBUSY;
64
65 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 wdt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return len;
91}
92
93static struct watchdog_info ident = {
94 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
95 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
96 .identity = "IXP4xx Watchdog",
97};
98
99
Alan Cox20d35f32008-05-19 14:06:48 +0100100static long ixp4xx_wdt_ioctl(struct file *file, unsigned int cmd,
101 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200103 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int time;
105
106 switch (cmd) {
107 case WDIOC_GETSUPPORT:
108 ret = copy_to_user((struct watchdog_info *)arg, &ident,
109 sizeof(ident)) ? -EFAULT : 0;
110 break;
111
112 case WDIOC_GETSTATUS:
113 ret = put_user(0, (int *)arg);
114 break;
115
116 case WDIOC_GETBOOTSTATUS:
117 ret = put_user(boot_status, (int *)arg);
118 break;
119
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000120 case WDIOC_KEEPALIVE:
121 wdt_enable();
122 ret = 0;
123 break;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case WDIOC_SETTIMEOUT:
126 ret = get_user(time, (int *)arg);
127 if (ret)
128 break;
129
130 if (time <= 0 || time > 60) {
131 ret = -EINVAL;
132 break;
133 }
134
135 heartbeat = time;
136 wdt_enable();
137 /* Fall through */
138
139 case WDIOC_GETTIMEOUT:
140 ret = put_user(heartbeat, (int *)arg);
141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 return ret;
144}
145
Alan Cox20d35f32008-05-19 14:06:48 +0100146static int ixp4xx_wdt_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Alan Cox20d35f32008-05-19 14:06:48 +0100148 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 wdt_disable();
Alan Cox20d35f32008-05-19 14:06:48 +0100150 else
Lennert Buytenhekb36bbb62005-06-28 20:44:46 -0700151 printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 "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 Cox20d35f32008-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;
178 unsigned long processor_id;
179
180 asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
Deepak Saxena3adfd4e2006-01-03 12:50:30 -0800181 if (!(processor_id & 0xf) && !cpu_is_ixp46x()) {
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000182 printk(KERN_ERR "IXP4XXX Watchdog: Rev. A0 IXP42x CPU detected"
183 " - watchdog disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 return -ENODEV;
186 }
Alan Cox20d35f32008-05-19 14:06:48 +0100187 spin_lock_init(&wdt_lock);
188 boot_status = (*IXP4XX_OSST & IXP4XX_OSST_TIMER_WARM_RESET) ?
189 WDIOF_CARDRESET : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ret = misc_register(&ixp4xx_wdt_miscdev);
191 if (ret == 0)
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000192 printk(KERN_INFO "IXP4xx Watchdog Timer: heartbeat %d sec\n",
193 heartbeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return ret;
195}
196
197static void __exit ixp4xx_wdt_exit(void)
198{
199 misc_deregister(&ixp4xx_wdt_miscdev);
200}
201
202
203module_init(ixp4xx_wdt_init);
204module_exit(ixp4xx_wdt_exit);
205
206MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
207MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
208
209module_param(heartbeat, int, 0);
210MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 60s)");
211
212module_param(nowayout, int, 0);
213MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
214
215MODULE_LICENSE("GPL");
216MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
217