blob: 88c0fc6395e140acae5f610cd3126d6449af1572 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: riowatchdog.c,v 1.3.2.2 2002/01/23 18:48:02 davem Exp $
2 * riowatchdog.c - driver for hw watchdog inside Super I/O of RIO
3 *
4 * Copyright (C) 2001 David S. Miller (davem@redhat.com)
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/types.h>
10#include <linux/fs.h>
11#include <linux/errno.h>
12#include <linux/init.h>
13#include <linux/miscdevice.h>
Arnd Bergmannf29b8892008-05-20 19:16:35 +020014#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/io.h>
17#include <asm/ebus.h>
18#include <asm/bbc.h>
19#include <asm/oplib.h>
20#include <asm/uaccess.h>
21
22#include <asm/watchdog.h>
23
24/* RIO uses the NatSemi Super I/O power management logical device
25 * as its' watchdog.
26 *
27 * When the watchdog triggers, it asserts a line to the BBC (Boot Bus
28 * Controller) of the machine. The BBC can only be configured to
29 * trigger a power-on reset when the signal is asserted. The BBC
30 * can be configured to ignore the signal entirely as well.
31 *
32 * The only Super I/O device register we care about is at index
33 * 0x05 (WDTO_INDEX) which is the watchdog time-out in minutes (1-255).
34 * If set to zero, this disables the watchdog. When set, the system
35 * must periodically (before watchdog expires) clear (set to zero) and
36 * re-set the watchdog else it will trigger.
37 *
38 * There are two other indexed watchdog registers inside this Super I/O
39 * logical device, but they are unused. The first, at index 0x06 is
40 * the watchdog control and can be used to make the watchdog timer re-set
41 * when the PS/2 mouse or serial lines show activity. The second, at
42 * index 0x07 is merely a sampling of the line from the watchdog to the
43 * BBC.
44 *
45 * The watchdog device generates no interrupts.
46 */
47
48MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
49MODULE_DESCRIPTION("Hardware watchdog driver for Sun RIO");
50MODULE_SUPPORTED_DEVICE("watchdog");
51MODULE_LICENSE("GPL");
52
53#define RIOWD_NAME "pmc"
54#define RIOWD_MINOR 215
55
56static DEFINE_SPINLOCK(riowd_lock);
57
58static void __iomem *bbc_regs;
59static void __iomem *riowd_regs;
60#define WDTO_INDEX 0x05
61
62static int riowd_timeout = 1; /* in minutes */
63module_param(riowd_timeout, int, 0);
64MODULE_PARM_DESC(riowd_timeout, "Watchdog timeout in minutes");
65
66#if 0 /* Currently unused. */
67static u8 riowd_readreg(int index)
68{
69 unsigned long flags;
70 u8 ret;
71
72 spin_lock_irqsave(&riowd_lock, flags);
73 writeb(index, riowd_regs + 0);
74 ret = readb(riowd_regs + 1);
75 spin_unlock_irqrestore(&riowd_lock, flags);
76
77 return ret;
78}
79#endif
80
81static void riowd_writereg(u8 val, int index)
82{
83 unsigned long flags;
84
85 spin_lock_irqsave(&riowd_lock, flags);
86 writeb(index, riowd_regs + 0);
87 writeb(val, riowd_regs + 1);
88 spin_unlock_irqrestore(&riowd_lock, flags);
89}
90
91static void riowd_pingtimer(void)
92{
93 riowd_writereg(riowd_timeout, WDTO_INDEX);
94}
95
96static void riowd_stoptimer(void)
97{
98 u8 val;
99
100 riowd_writereg(0, WDTO_INDEX);
101
102 val = readb(bbc_regs + BBC_WDACTION);
103 val &= ~BBC_WDACTION_RST;
104 writeb(val, bbc_regs + BBC_WDACTION);
105}
106
107static void riowd_starttimer(void)
108{
109 u8 val;
110
111 riowd_writereg(riowd_timeout, WDTO_INDEX);
112
113 val = readb(bbc_regs + BBC_WDACTION);
114 val |= BBC_WDACTION_RST;
115 writeb(val, bbc_regs + BBC_WDACTION);
116}
117
118static int riowd_open(struct inode *inode, struct file *filp)
119{
Arnd Bergmannf29b8892008-05-20 19:16:35 +0200120 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 nonseekable_open(inode, filp);
122 return 0;
123}
124
125static int riowd_release(struct inode *inode, struct file *filp)
126{
127 return 0;
128}
129
130static int riowd_ioctl(struct inode *inode, struct file *filp,
131 unsigned int cmd, unsigned long arg)
132{
133 static struct watchdog_info info = {
134 WDIOF_SETTIMEOUT, 0, "Natl. Semiconductor PC97317"
135 };
136 void __user *argp = (void __user *)arg;
137 unsigned int options;
138 int new_margin;
139
140 switch (cmd) {
141 case WDIOC_GETSUPPORT:
142 if (copy_to_user(argp, &info, sizeof(info)))
143 return -EFAULT;
144 break;
145
146 case WDIOC_GETSTATUS:
147 case WDIOC_GETBOOTSTATUS:
148 if (put_user(0, (int __user *)argp))
149 return -EFAULT;
150 break;
151
152 case WDIOC_KEEPALIVE:
153 riowd_pingtimer();
154 break;
155
156 case WDIOC_SETOPTIONS:
157 if (copy_from_user(&options, argp, sizeof(options)))
158 return -EFAULT;
159
160 if (options & WDIOS_DISABLECARD)
161 riowd_stoptimer();
162 else if (options & WDIOS_ENABLECARD)
163 riowd_starttimer();
164 else
165 return -EINVAL;
166
167 break;
168
169 case WDIOC_SETTIMEOUT:
170 if (get_user(new_margin, (int __user *)argp))
171 return -EFAULT;
172 if ((new_margin < 60) || (new_margin > (255 * 60)))
173 return -EINVAL;
174 riowd_timeout = (new_margin + 59) / 60;
175 riowd_pingtimer();
176 /* Fall */
177
178 case WDIOC_GETTIMEOUT:
179 return put_user(riowd_timeout * 60, (int __user *)argp);
180
181 default:
182 return -EINVAL;
183 };
184
185 return 0;
186}
187
188static ssize_t riowd_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
189{
190 if (count) {
191 riowd_pingtimer();
192 return 1;
193 }
194
195 return 0;
196}
197
Arjan van de Ven00977a52007-02-12 00:55:34 -0800198static const struct file_operations riowd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .owner = THIS_MODULE,
200 .ioctl = riowd_ioctl,
201 .open = riowd_open,
202 .write = riowd_write,
203 .release = riowd_release,
204};
205
206static struct miscdevice riowd_miscdev = { RIOWD_MINOR, RIOWD_NAME, &riowd_fops };
207
208static int __init riowd_bbc_init(void)
209{
210 struct linux_ebus *ebus = NULL;
211 struct linux_ebus_device *edev = NULL;
212 u8 val;
213
214 for_each_ebus(ebus) {
215 for_each_ebusdev(edev, ebus) {
David S. Miller95b0ce92006-06-25 00:17:11 -0700216 if (!strcmp(edev->ofdev.node->name, "bbc"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto found_bbc;
218 }
219 }
220
221found_bbc:
222 if (!edev)
223 return -ENODEV;
224 bbc_regs = ioremap(edev->resource[0].start, BBC_REGS_SIZE);
225 if (!bbc_regs)
226 return -ENODEV;
227
228 /* Turn it off. */
229 val = readb(bbc_regs + BBC_WDACTION);
230 val &= ~BBC_WDACTION_RST;
231 writeb(val, bbc_regs + BBC_WDACTION);
232
233 return 0;
234}
235
236static int __init riowd_init(void)
237{
238 struct linux_ebus *ebus = NULL;
239 struct linux_ebus_device *edev = NULL;
240
241 for_each_ebus(ebus) {
242 for_each_ebusdev(edev, ebus) {
David S. Miller95b0ce92006-06-25 00:17:11 -0700243 if (!strcmp(edev->ofdev.node->name, RIOWD_NAME))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto ebus_done;
245 }
246 }
247
248ebus_done:
249 if (!edev)
250 goto fail;
251
252 riowd_regs = ioremap(edev->resource[0].start, 2);
253 if (riowd_regs == NULL) {
254 printk(KERN_ERR "pmc: Cannot map registers.\n");
255 return -ENODEV;
256 }
257
258 if (riowd_bbc_init()) {
259 printk(KERN_ERR "pmc: Failure initializing BBC config.\n");
260 goto fail;
261 }
262
263 if (misc_register(&riowd_miscdev)) {
264 printk(KERN_ERR "pmc: Cannot register watchdog misc device.\n");
265 goto fail;
266 }
267
268 printk(KERN_INFO "pmc: Hardware watchdog [%i minutes], "
269 "regs at %p\n", riowd_timeout, riowd_regs);
270
271 return 0;
272
273fail:
274 if (riowd_regs) {
275 iounmap(riowd_regs);
276 riowd_regs = NULL;
277 }
278 if (bbc_regs) {
279 iounmap(bbc_regs);
280 bbc_regs = NULL;
281 }
282 return -ENODEV;
283}
284
285static void __exit riowd_cleanup(void)
286{
287 misc_deregister(&riowd_miscdev);
288 iounmap(riowd_regs);
289 riowd_regs = NULL;
290 iounmap(bbc_regs);
291 bbc_regs = NULL;
292}
293
294module_init(riowd_init);
295module_exit(riowd_cleanup);