blob: da3fc63bca61fc4bf50ee0a0b4e6ea311fd560c2 [file] [log] [blame]
Thomas Koeller825d3742006-08-14 21:55:29 +02001/*
2 * Watchdog implementation for GPI h/w found on PMC-Sierra RM9xxx
3 * chips.
4 *
5 * Copyright (C) 2004 by Basler Vision Technologies AG
6 * Author: Thomas Koeller <thomas.koeller@baslerweb.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/platform_device.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/interrupt.h>
27#include <linux/fs.h>
28#include <linux/reboot.h>
29#include <linux/miscdevice.h>
30#include <linux/watchdog.h>
31#include <asm/io.h>
32#include <asm/atomic.h>
33#include <asm/processor.h>
34#include <asm/uaccess.h>
35#include <asm/system.h>
36#include <asm/rm9k-ocd.h>
37
38#include <rm9k_wdt.h>
39
40
41#define CLOCK 125000000
42#define MAX_TIMEOUT_SECONDS 32
43#define CPCCR 0x0080
44#define CPGIG1SR 0x0044
45#define CPGIG1ER 0x0054
46
47
Thomas Koeller825d3742006-08-14 21:55:29 +020048/* Function prototypes */
49static int __init wdt_gpi_probe(struct device *);
50static int __exit wdt_gpi_remove(struct device *);
51static void wdt_gpi_set_timeout(unsigned int);
52static int wdt_gpi_open(struct inode *, struct file *);
53static int wdt_gpi_release(struct inode *, struct file *);
54static ssize_t wdt_gpi_write(struct file *, const char __user *, size_t, loff_t *);
55static long wdt_gpi_ioctl(struct file *, unsigned int, unsigned long);
56static const struct resource *wdt_gpi_get_resource(struct platform_device *, const char *, unsigned int);
57static int wdt_gpi_notify(struct notifier_block *, unsigned long, void *);
58static irqreturn_t wdt_gpi_irqhdl(int, void *, struct pt_regs *);
59
60
Thomas Koeller825d3742006-08-14 21:55:29 +020061static const char wdt_gpi_name[] = "wdt_gpi";
62static atomic_t opencnt;
63static int expect_close;
64static int locked = 0;
65
66
Thomas Koeller825d3742006-08-14 21:55:29 +020067/* These are set from device resources */
68static void __iomem * wd_regs;
69static unsigned int wd_irq, wd_ctr;
70
71
Thomas Koeller825d3742006-08-14 21:55:29 +020072/* Module arguments */
73static int timeout = MAX_TIMEOUT_SECONDS;
74module_param(timeout, int, 0444);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +010075MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
76
Thomas Koeller825d3742006-08-14 21:55:29 +020077static unsigned long resetaddr = 0xbffdc200;
78module_param(resetaddr, ulong, 0444);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +010079MODULE_PARM_DESC(resetaddr, "Address to write to to force a reset");
80
Thomas Koeller825d3742006-08-14 21:55:29 +020081static unsigned long flagaddr = 0xbffdc104;
82module_param(flagaddr, ulong, 0444);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +010083MODULE_PARM_DESC(flagaddr, "Address to write to boot flags to");
84
Thomas Koeller825d3742006-08-14 21:55:29 +020085static int powercycle = 0;
86module_param(powercycle, bool, 0444);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +010087MODULE_PARM_DESC(powercycle, "Cycle power if watchdog expires");
Thomas Koeller825d3742006-08-14 21:55:29 +020088
89static int nowayout = WATCHDOG_NOWAYOUT;
90module_param(nowayout, bool, 0444);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +010091MODULE_PARM_DESC(nowayout, "Watchdog cannot be disabled once started");
Thomas Koeller825d3742006-08-14 21:55:29 +020092
93
94
95static struct file_operations fops = {
96 .owner = THIS_MODULE,
97 .open = wdt_gpi_open,
98 .release = wdt_gpi_release,
99 .write = wdt_gpi_write,
100 .unlocked_ioctl = wdt_gpi_ioctl,
101};
102
103static struct miscdevice miscdev = {
104 .minor = WATCHDOG_MINOR,
105 .name = wdt_gpi_name,
106 .fops = &fops,
107};
108
109static struct device_driver wdt_gpi_driver = {
110 .name = (char *) wdt_gpi_name,
111 .bus = &platform_bus_type,
112 .owner = THIS_MODULE,
113 .probe = wdt_gpi_probe,
114 .remove = __exit_p(wdt_gpi_remove),
115 .shutdown = NULL,
116 .suspend = NULL,
117 .resume = NULL,
118};
119
120static struct notifier_block wdt_gpi_shutdown = {
121 .notifier_call = wdt_gpi_notify,
122};
123
124
125
126static const struct resource *
127wdt_gpi_get_resource(struct platform_device *pdv, const char *name,
128 unsigned int type)
129{
130 char buf[80];
131 if (snprintf(buf, sizeof buf, "%s_0", name) >= sizeof buf)
132 return NULL;
133 return platform_get_resource_byname(pdv, type, buf);
134}
135
136
137
138/* No hotplugging on the platform bus - use __init */
139static int __init wdt_gpi_probe(struct device *dev)
140{
141 int res;
142 struct platform_device * const pdv = to_platform_device(dev);
143 const struct resource
144 * const rr = wdt_gpi_get_resource(pdv, WDT_RESOURCE_REGS,
145 IORESOURCE_MEM),
146 * const ri = wdt_gpi_get_resource(pdv, WDT_RESOURCE_IRQ,
147 IORESOURCE_IRQ),
148 * const rc = wdt_gpi_get_resource(pdv, WDT_RESOURCE_COUNTER,
149 0);
150
151 if (unlikely(!rr || !ri || !rc))
152 return -ENXIO;
153
154 wd_regs = ioremap_nocache(rr->start, rr->end + 1 - rr->start);
155 if (unlikely(!wd_regs))
156 return -ENOMEM;
157 wd_irq = ri->start;
158 wd_ctr = rc->start;
159 res = misc_register(&miscdev);
160 if (res)
161 iounmap(wd_regs);
162 else
163 register_reboot_notifier(&wdt_gpi_shutdown);
164 return res;
165}
166
167
168
169static int __exit wdt_gpi_remove(struct device *dev)
170{
171 int res;
172
173 unregister_reboot_notifier(&wdt_gpi_shutdown);
174 res = misc_deregister(&miscdev);
175 iounmap(wd_regs);
176 wd_regs = NULL;
177 return res;
178}
179
180
181static void wdt_gpi_set_timeout(unsigned int to)
182{
183 u32 reg;
184 const u32 wdval = (to * CLOCK) & ~0x0000000f;
185
186 lock_titan_regs();
187 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
188 titan_writel(reg, CPCCR);
189 wmb();
190 __raw_writel(wdval, wd_regs + 0x0000);
191 wmb();
192 titan_writel(reg | (0x2 << (wd_ctr * 4)), CPCCR);
193 wmb();
194 titan_writel(reg | (0x5 << (wd_ctr * 4)), CPCCR);
195 iob();
196 unlock_titan_regs();
197}
198
199
200
201static int wdt_gpi_open(struct inode *i, struct file *f)
202{
203 int res;
204 u32 reg;
205
206 if (unlikely(0 > atomic_dec_if_positive(&opencnt)))
207 return -EBUSY;
208
209 expect_close = 0;
210 if (locked) {
211 module_put(THIS_MODULE);
212 free_irq(wd_irq, &miscdev);
213 locked = 0;
214 }
215
216 res = request_irq(wd_irq, wdt_gpi_irqhdl, SA_SHIRQ | SA_INTERRUPT,
217 wdt_gpi_name, &miscdev);
218 if (unlikely(res))
219 return res;
220
221 wdt_gpi_set_timeout(timeout);
222
223 lock_titan_regs();
224 reg = titan_readl(CPGIG1ER);
225 titan_writel(reg | (0x100 << wd_ctr), CPGIG1ER);
226 iob();
227 unlock_titan_regs();
228
229 printk(KERN_INFO "%s: watchdog started, timeout = %u seconds\n",
230 wdt_gpi_name, timeout);
231 return 0;
232}
233
234
235
236static int wdt_gpi_release(struct inode *i, struct file *f)
237{
238 if (nowayout) {
239 printk(KERN_NOTICE "%s: no way out - watchdog left running\n",
240 wdt_gpi_name);
241 __module_get(THIS_MODULE);
242 locked = 1;
243 } else {
244 if (expect_close) {
245 u32 reg;
246
247 lock_titan_regs();
248 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
249 titan_writel(reg, CPCCR);
250 reg = titan_readl(CPGIG1ER);
251 titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
252 iob();
253 unlock_titan_regs();
254 free_irq(wd_irq, &miscdev);
255 printk(KERN_INFO "%s: watchdog stopped\n", wdt_gpi_name);
256 } else {
257 printk(KERN_NOTICE "%s: unexpected close() -"
258 " watchdog left running\n",
259 wdt_gpi_name);
260 wdt_gpi_set_timeout(timeout);
261 __module_get(THIS_MODULE);
262 locked = 1;
263 }
264 }
265
266 atomic_inc(&opencnt);
267 return 0;
268}
269
270
271
272static ssize_t
273wdt_gpi_write(struct file *f, const char __user *d, size_t s, loff_t *o)
274{
275 char val;
276
277 wdt_gpi_set_timeout(timeout);
278 expect_close = (s > 0) && !get_user(val, d) && (val == 'V');
279 return s ? 1 : 0;
280}
281
282
283
284static long
285wdt_gpi_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
286{
287 long res = -ENOTTY;
288 const long size = _IOC_SIZE(cmd);
289 int stat;
290 static struct watchdog_info wdinfo = {
291 .identity = "RM9xxx/GPI watchdog",
292 .firmware_version = 0,
293 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
294 };
295
296 if (unlikely(_IOC_TYPE(cmd) != WATCHDOG_IOCTL_BASE))
297 return -ENOTTY;
298
299 if ((_IOC_DIR(cmd) & _IOC_READ)
300 && !access_ok(VERIFY_WRITE, arg, size))
301 return -EFAULT;
302
303 if ((_IOC_DIR(cmd) & _IOC_WRITE)
304 && !access_ok(VERIFY_READ, arg, size))
305 return -EFAULT;
306
307 expect_close = 0;
308
309 switch (cmd) {
310 case WDIOC_GETSUPPORT:
311 wdinfo.options = nowayout ?
312 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING :
313 WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE;
314 res = __copy_to_user((void __user *)arg, &wdinfo, size) ?
315 -EFAULT : size;
316 break;
317
318 case WDIOC_GETSTATUS:
319 break;
320
321 case WDIOC_GETBOOTSTATUS:
322 stat = (*(volatile char *) flagaddr & 0x01)
323 ? WDIOF_CARDRESET : 0;
324 res = __copy_to_user((void __user *)arg, &stat, size) ?
325 -EFAULT : size;
326 break;
327
328 case WDIOC_SETOPTIONS:
329 break;
330
331 case WDIOC_KEEPALIVE:
332 wdt_gpi_set_timeout(timeout);
333 res = size;
334 break;
335
336 case WDIOC_SETTIMEOUT:
337 {
338 int val;
339 if (unlikely(__copy_from_user(&val, (const void __user *) arg,
340 size))) {
341 res = -EFAULT;
342 break;
343 }
344
345 if (val > 32)
346 val = 32;
347 timeout = val;
348 wdt_gpi_set_timeout(val);
349 res = size;
350 printk("%s: timeout set to %u seconds\n",
351 wdt_gpi_name, timeout);
352 }
353 break;
354
355 case WDIOC_GETTIMEOUT:
356 res = __copy_to_user((void __user *) arg, &timeout, size) ?
357 -EFAULT : size;
358 break;
359 }
360
361 return res;
362}
363
364
365
366
367static irqreturn_t wdt_gpi_irqhdl(int irq, void *ctxt, struct pt_regs *regs)
368{
369 if (!unlikely(__raw_readl(wd_regs + 0x0008) & 0x1))
370 return IRQ_NONE;
371 __raw_writel(0x1, wd_regs + 0x0008);
372
373
374 printk(KERN_WARNING "%s: watchdog expired - resetting system\n",
375 wdt_gpi_name);
376
377 *(volatile char *) flagaddr |= 0x01;
378 *(volatile char *) resetaddr = powercycle ? 0x01 : 0x2;
379 iob();
380 while (1)
381 cpu_relax();
382}
383
384
385
386static int
387wdt_gpi_notify(struct notifier_block *this, unsigned long code, void *unused)
388{
389 if(code == SYS_DOWN || code == SYS_HALT) {
390 u32 reg;
391
392 lock_titan_regs();
393 reg = titan_readl(CPCCR) & ~(0xf << (wd_ctr * 4));
394 titan_writel(reg, CPCCR);
395 reg = titan_readl(CPGIG1ER);
396 titan_writel(reg & ~(0x100 << wd_ctr), CPGIG1ER);
397 iob();
398 unlock_titan_regs();
399 }
400 return NOTIFY_DONE;
401}
402
403
404
405static int __init wdt_gpi_init_module(void)
406{
407 atomic_set(&opencnt, 1);
408 if (timeout > MAX_TIMEOUT_SECONDS)
409 timeout = MAX_TIMEOUT_SECONDS;
410 return driver_register(&wdt_gpi_driver);
411}
412
413
414
415static void __exit wdt_gpi_cleanup_module(void)
416{
417 driver_unregister(&wdt_gpi_driver);
418}
419
420module_init(wdt_gpi_init_module);
421module_exit(wdt_gpi_cleanup_module);
422
423
424
425MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
426MODULE_DESCRIPTION("Basler eXcite watchdog driver for gpi devices");
427MODULE_VERSION("0.1");
428MODULE_LICENSE("GPL");
429MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Wim Van Sebroeckcd57eea2006-11-17 21:51:35 +0100430