blob: 3f047a58d3aecf41235a1a5f9e622a0347b68061 [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
Joe Perches27c766a2012-02-15 15:06:19 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/types.h>
Peter Huewee605d552010-01-07 03:35:49 +010024#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/fs.h>
27#include <linux/miscdevice.h>
28#include <linux/watchdog.h>
29#include <linux/init.h>
30#include <linux/bitops.h>
Alan Cox640b4f62008-05-19 14:06:42 +010031#include <linux/uaccess.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010034static bool nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static unsigned int heartbeat = 60; /* (secs) Default is 1 minute */
36static unsigned long wdt_status;
Axel Lin1334f322011-11-29 13:54:01 +080037static DEFINE_SPINLOCK(wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define WDT_IN_USE 0
40#define WDT_OK_TO_CLOSE 1
41
42static unsigned long wdt_tick_rate;
43
Alan Cox640b4f62008-05-19 14:06:42 +010044static void wdt_enable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Alan Cox640b4f62008-05-19 14:06:42 +010046 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ixp2000_reg_write(IXP2000_RESET0, *(IXP2000_RESET0) | WDT_RESET_ENABLE);
48 ixp2000_reg_write(IXP2000_TWDE, WDT_ENABLE);
49 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
50 ixp2000_reg_write(IXP2000_T4_CTL, TIMER_DIVIDER_256 | TIMER_ENABLE);
Alan Cox640b4f62008-05-19 14:06:42 +010051 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Alan Cox640b4f62008-05-19 14:06:42 +010054static void wdt_disable(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Alan Cox640b4f62008-05-19 14:06:42 +010056 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 ixp2000_reg_write(IXP2000_T4_CTL, 0);
Alan Cox640b4f62008-05-19 14:06:42 +010058 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Alan Cox640b4f62008-05-19 14:06:42 +010061static void wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Alan Cox640b4f62008-05-19 14:06:42 +010063 spin_lock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ixp2000_reg_write(IXP2000_T4_CLD, heartbeat * wdt_tick_rate);
Alan Cox640b4f62008-05-19 14:06:42 +010065 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Alan Cox640b4f62008-05-19 14:06:42 +010068static int ixp2000_wdt_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
71 return -EBUSY;
72
73 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
74
75 wdt_enable();
76
77 return nonseekable_open(inode, file);
78}
79
Alan Cox640b4f62008-05-19 14:06:42 +010080static ssize_t ixp2000_wdt_write(struct file *file, const char *data,
81 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 if (len) {
84 if (!nowayout) {
85 size_t i;
86
87 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
88
89 for (i = 0; i != len; i++) {
90 char c;
91
92 if (get_user(c, data + i))
93 return -EFAULT;
94 if (c == 'V')
95 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
96 }
97 }
98 wdt_keepalive();
99 }
100
101 return len;
102}
103
104
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000105static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
107 WDIOF_KEEPALIVEPING,
108 .identity = "IXP2000 Watchdog",
109};
110
Alan Cox640b4f62008-05-19 14:06:42 +0100111static long ixp2000_wdt_ioctl(struct file *file, unsigned int cmd,
112 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200114 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int time;
116
117 switch (cmd) {
118 case WDIOC_GETSUPPORT:
119 ret = copy_to_user((struct watchdog_info *)arg, &ident,
120 sizeof(ident)) ? -EFAULT : 0;
121 break;
122
123 case WDIOC_GETSTATUS:
124 ret = put_user(0, (int *)arg);
125 break;
126
127 case WDIOC_GETBOOTSTATUS:
128 ret = put_user(0, (int *)arg);
129 break;
130
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000131 case WDIOC_KEEPALIVE:
132 wdt_enable();
133 ret = 0;
134 break;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case WDIOC_SETTIMEOUT:
137 ret = get_user(time, (int *)arg);
138 if (ret)
139 break;
140
141 if (time <= 0 || time > 60) {
142 ret = -EINVAL;
143 break;
144 }
145
146 heartbeat = time;
147 wdt_keepalive();
148 /* Fall through */
149
150 case WDIOC_GETTIMEOUT:
151 ret = put_user(heartbeat, (int *)arg);
152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 return ret;
156}
157
Alan Cox640b4f62008-05-19 14:06:42 +0100158static int ixp2000_wdt_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Alan Cox640b4f62008-05-19 14:06:42 +0100160 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 wdt_disable();
Alan Cox640b4f62008-05-19 14:06:42 +0100162 else
Joe Perches27c766a2012-02-15 15:06:19 -0800163 pr_crit("Device closed unexpectedly - timer will not stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 clear_bit(WDT_IN_USE, &wdt_status);
165 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
166
167 return 0;
168}
169
170
Alan Cox640b4f62008-05-19 14:06:42 +0100171static const struct file_operations ixp2000_wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .owner = THIS_MODULE,
173 .llseek = no_llseek,
174 .write = ixp2000_wdt_write,
Alan Cox640b4f62008-05-19 14:06:42 +0100175 .unlocked_ioctl = ixp2000_wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .open = ixp2000_wdt_open,
177 .release = ixp2000_wdt_release,
178};
179
Alan Cox640b4f62008-05-19 14:06:42 +0100180static struct miscdevice ixp2000_wdt_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .minor = WATCHDOG_MINOR,
Olaf Hering2dab3ca2005-08-17 08:58:34 +0200182 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .fops = &ixp2000_wdt_fops,
184};
185
186static int __init ixp2000_wdt_init(void)
187{
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100188 if ((*IXP2000_PRODUCT_ID & 0x001ffef0) == 0x00000000) {
Joe Perches27c766a2012-02-15 15:06:19 -0800189 pr_info("Unable to use IXP2000 watchdog due to IXP2800 erratum #25\n");
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100190 return -EIO;
191 }
Lennert Buytenheke4fe1982005-06-20 18:51:07 +0100192 wdt_tick_rate = (*IXP2000_T1_CLD * HZ) / 256;
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
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100210module_param(nowayout, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
212
213MODULE_LICENSE("GPL");
214MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
215