]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/watchdog/pc87413_wdt.c
Remove all #inclusions of asm/system.h
[karo-tx-linux.git] / drivers / watchdog / pc87413_wdt.c
1 /*
2  *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
3  *
4  *      This code is based on wdt.c with original copyright.
5  *
6  *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7  *                     and Marcus Junker, <junker@anduras.de>
8  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  *
14  *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
15  *      admit liability nor provide warranty for any of this software.
16  *      This material is provided "AS-IS" and at no charge.
17  *
18  *      Release 1.1
19  */
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/miscdevice.h>
24 #include <linux/watchdog.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/notifier.h>
28 #include <linux/fs.h>
29 #include <linux/reboot.h>
30 #include <linux/init.h>
31 #include <linux/spinlock.h>
32 #include <linux/moduleparam.h>
33 #include <linux/io.h>
34 #include <linux/uaccess.h>
35
36
37 /* #define DEBUG 1 */
38
39 #define DEFAULT_TIMEOUT     1           /* 1 minute */
40 #define MAX_TIMEOUT         255
41
42 #define VERSION             "1.1"
43 #define MODNAME             "pc87413 WDT"
44 #define PFX                 MODNAME ": "
45 #define DPFX                MODNAME " - DEBUG: "
46
47 #define WDT_INDEX_IO_PORT   (io+0)      /* I/O port base (index register) */
48 #define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
49 #define SWC_LDN             0x04
50 #define SIOCFG2             0x22        /* Serial IO register */
51 #define WDCTL               0x10        /* Watchdog-Timer-Control-Register */
52 #define WDTO                0x11        /* Watchdog timeout register */
53 #define WDCFG               0x12        /* Watchdog config register */
54
55 #define IO_DEFAULT      0x2E            /* Address used on Portwell Boards */
56
57 static int io = IO_DEFAULT;
58 static int swc_base_addr = -1;
59
60 static int timeout = DEFAULT_TIMEOUT;   /* timeout value */
61 static unsigned long timer_enabled;     /* is the timer enabled? */
62
63 static char expect_close;               /* is the close expected? */
64
65 static DEFINE_SPINLOCK(io_lock);        /* to guard us from io races */
66
67 static int nowayout = WATCHDOG_NOWAYOUT;
68
69 /* -- Low level function ----------------------------------------*/
70
71 /* Select pins for Watchdog output */
72
73 static inline void pc87413_select_wdt_out(void)
74 {
75         unsigned int cr_data = 0;
76
77         /* Step 1: Select multiple pin,pin55,as WDT output */
78
79         outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
80
81         cr_data = inb(WDT_DATA_IO_PORT);
82
83         cr_data |= 0x80; /* Set Bit7 to 1*/
84         outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
85
86         outb_p(cr_data, WDT_DATA_IO_PORT);
87
88 #ifdef DEBUG
89         printk(KERN_INFO DPFX
90                 "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
91                                                                 cr_data);
92 #endif
93 }
94
95 /* Enable SWC functions */
96
97 static inline void pc87413_enable_swc(void)
98 {
99         unsigned int cr_data = 0;
100
101         /* Step 2: Enable SWC functions */
102
103         outb_p(0x07, WDT_INDEX_IO_PORT);        /* Point SWC_LDN (LDN=4) */
104         outb_p(SWC_LDN, WDT_DATA_IO_PORT);
105
106         outb_p(0x30, WDT_INDEX_IO_PORT);        /* Read Index 0x30 First */
107         cr_data = inb(WDT_DATA_IO_PORT);
108         cr_data |= 0x01;                        /* Set Bit0 to 1 */
109         outb_p(0x30, WDT_INDEX_IO_PORT);
110         outb_p(cr_data, WDT_DATA_IO_PORT);      /* Index0x30_bit0P1 */
111
112 #ifdef DEBUG
113         printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
114 #endif
115 }
116
117 /* Read SWC I/O base address */
118
119 static void pc87413_get_swc_base_addr(void)
120 {
121         unsigned char addr_l, addr_h = 0;
122
123         /* Step 3: Read SWC I/O Base Address */
124
125         outb_p(0x60, WDT_INDEX_IO_PORT);        /* Read Index 0x60 */
126         addr_h = inb(WDT_DATA_IO_PORT);
127
128         outb_p(0x61, WDT_INDEX_IO_PORT);        /* Read Index 0x61 */
129
130         addr_l = inb(WDT_DATA_IO_PORT);
131
132         swc_base_addr = (addr_h << 8) + addr_l;
133 #ifdef DEBUG
134         printk(KERN_INFO DPFX
135                 "Read SWC I/O Base Address: low %d, high %d, res %d\n",
136                                                 addr_l, addr_h, swc_base_addr);
137 #endif
138 }
139
140 /* Select Bank 3 of SWC */
141
142 static inline void pc87413_swc_bank3(void)
143 {
144         /* Step 4: Select Bank3 of SWC */
145         outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
146 #ifdef DEBUG
147         printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
148 #endif
149 }
150
151 /* Set watchdog timeout to x minutes */
152
153 static inline void pc87413_programm_wdto(char pc87413_time)
154 {
155         /* Step 5: Programm WDTO, Twd. */
156         outb_p(pc87413_time, swc_base_addr + WDTO);
157 #ifdef DEBUG
158         printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
159 #endif
160 }
161
162 /* Enable WDEN */
163
164 static inline void pc87413_enable_wden(void)
165 {
166         /* Step 6: Enable WDEN */
167         outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
168 #ifdef DEBUG
169         printk(KERN_INFO DPFX "Enable WDEN\n");
170 #endif
171 }
172
173 /* Enable SW_WD_TREN */
174 static inline void pc87413_enable_sw_wd_tren(void)
175 {
176         /* Enable SW_WD_TREN */
177         outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
178 #ifdef DEBUG
179         printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
180 #endif
181 }
182
183 /* Disable SW_WD_TREN */
184
185 static inline void pc87413_disable_sw_wd_tren(void)
186 {
187         /* Disable SW_WD_TREN */
188         outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
189 #ifdef DEBUG
190         printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
191 #endif
192 }
193
194 /* Enable SW_WD_TRG */
195
196 static inline void pc87413_enable_sw_wd_trg(void)
197 {
198         /* Enable SW_WD_TRG */
199         outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
200 #ifdef DEBUG
201         printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
202 #endif
203 }
204
205 /* Disable SW_WD_TRG */
206
207 static inline void pc87413_disable_sw_wd_trg(void)
208 {
209         /* Disable SW_WD_TRG */
210         outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
211 #ifdef DEBUG
212         printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
213 #endif
214 }
215
216 /* -- Higher level functions ------------------------------------*/
217
218 /* Enable the watchdog */
219
220 static void pc87413_enable(void)
221 {
222         spin_lock(&io_lock);
223
224         pc87413_swc_bank3();
225         pc87413_programm_wdto(timeout);
226         pc87413_enable_wden();
227         pc87413_enable_sw_wd_tren();
228         pc87413_enable_sw_wd_trg();
229
230         spin_unlock(&io_lock);
231 }
232
233 /* Disable the watchdog */
234
235 static void pc87413_disable(void)
236 {
237         spin_lock(&io_lock);
238
239         pc87413_swc_bank3();
240         pc87413_disable_sw_wd_tren();
241         pc87413_disable_sw_wd_trg();
242         pc87413_programm_wdto(0);
243
244         spin_unlock(&io_lock);
245 }
246
247 /* Refresh the watchdog */
248
249 static void pc87413_refresh(void)
250 {
251         spin_lock(&io_lock);
252
253         pc87413_swc_bank3();
254         pc87413_disable_sw_wd_tren();
255         pc87413_disable_sw_wd_trg();
256         pc87413_programm_wdto(timeout);
257         pc87413_enable_wden();
258         pc87413_enable_sw_wd_tren();
259         pc87413_enable_sw_wd_trg();
260
261         spin_unlock(&io_lock);
262 }
263
264 /* -- File operations -------------------------------------------*/
265
266 /**
267  *      pc87413_open:
268  *      @inode: inode of device
269  *      @file: file handle to device
270  *
271  */
272
273 static int pc87413_open(struct inode *inode, struct file *file)
274 {
275         /* /dev/watchdog can only be opened once */
276
277         if (test_and_set_bit(0, &timer_enabled))
278                 return -EBUSY;
279
280         if (nowayout)
281                 __module_get(THIS_MODULE);
282
283         /* Reload and activate timer */
284         pc87413_refresh();
285
286         printk(KERN_INFO MODNAME
287                 "Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
288
289         return nonseekable_open(inode, file);
290 }
291
292 /**
293  *      pc87413_release:
294  *      @inode: inode to board
295  *      @file: file handle to board
296  *
297  *      The watchdog has a configurable API. There is a religious dispute
298  *      between people who want their watchdog to be able to shut down and
299  *      those who want to be sure if the watchdog manager dies the machine
300  *      reboots. In the former case we disable the counters, in the latter
301  *      case you have to open it again very soon.
302  */
303
304 static int pc87413_release(struct inode *inode, struct file *file)
305 {
306         /* Shut off the timer. */
307
308         if (expect_close == 42) {
309                 pc87413_disable();
310                 printk(KERN_INFO MODNAME
311                                 "Watchdog disabled, sleeping again...\n");
312         } else {
313                 printk(KERN_CRIT MODNAME
314                                 "Unexpected close, not stopping watchdog!\n");
315                 pc87413_refresh();
316         }
317         clear_bit(0, &timer_enabled);
318         expect_close = 0;
319         return 0;
320 }
321
322 /**
323  *      pc87413_status:
324  *
325  *      return, if the watchdog is enabled (timeout is set...)
326  */
327
328
329 static int pc87413_status(void)
330 {
331           return 0; /* currently not supported */
332 }
333
334 /**
335  *      pc87413_write:
336  *      @file: file handle to the watchdog
337  *      @data: data buffer to write
338  *      @len: length in bytes
339  *      @ppos: pointer to the position to write. No seeks allowed
340  *
341  *      A write to a watchdog device is defined as a keepalive signal. Any
342  *      write of data will do, as we we don't define content meaning.
343  */
344
345 static ssize_t pc87413_write(struct file *file, const char __user *data,
346                              size_t len, loff_t *ppos)
347 {
348         /* See if we got the magic character 'V' and reload the timer */
349         if (len) {
350                 if (!nowayout) {
351                         size_t i;
352
353                         /* reset expect flag */
354                         expect_close = 0;
355
356                         /* scan to see whether or not we got the
357                            magic character */
358                         for (i = 0; i != len; i++) {
359                                 char c;
360                                 if (get_user(c, data + i))
361                                         return -EFAULT;
362                                 if (c == 'V')
363                                         expect_close = 42;
364                         }
365                 }
366
367                 /* someone wrote to us, we should reload the timer */
368                 pc87413_refresh();
369         }
370         return len;
371 }
372
373 /**
374  *      pc87413_ioctl:
375  *      @file: file handle to the device
376  *      @cmd: watchdog command
377  *      @arg: argument pointer
378  *
379  *      The watchdog API defines a common set of functions for all watchdogs
380  *      according to their available features. We only actually usefully support
381  *      querying capabilities and current status.
382  */
383
384 static long pc87413_ioctl(struct file *file, unsigned int cmd,
385                                                 unsigned long arg)
386 {
387         int new_timeout;
388
389         union {
390                 struct watchdog_info __user *ident;
391                 int __user *i;
392         } uarg;
393
394         static const struct watchdog_info ident = {
395                 .options          = WDIOF_KEEPALIVEPING |
396                                     WDIOF_SETTIMEOUT |
397                                     WDIOF_MAGICCLOSE,
398                 .firmware_version = 1,
399                 .identity         = "PC87413(HF/F) watchdog",
400         };
401
402         uarg.i = (int __user *)arg;
403
404         switch (cmd) {
405         case WDIOC_GETSUPPORT:
406                 return copy_to_user(uarg.ident, &ident,
407                                         sizeof(ident)) ? -EFAULT : 0;
408         case WDIOC_GETSTATUS:
409                 return put_user(pc87413_status(), uarg.i);
410         case WDIOC_GETBOOTSTATUS:
411                 return put_user(0, uarg.i);
412         case WDIOC_SETOPTIONS:
413         {
414                 int options, retval = -EINVAL;
415                 if (get_user(options, uarg.i))
416                         return -EFAULT;
417                 if (options & WDIOS_DISABLECARD) {
418                         pc87413_disable();
419                         retval = 0;
420                 }
421                 if (options & WDIOS_ENABLECARD) {
422                         pc87413_enable();
423                         retval = 0;
424                 }
425                 return retval;
426         }
427         case WDIOC_KEEPALIVE:
428                 pc87413_refresh();
429 #ifdef DEBUG
430                 printk(KERN_INFO DPFX "keepalive\n");
431 #endif
432                 return 0;
433         case WDIOC_SETTIMEOUT:
434                 if (get_user(new_timeout, uarg.i))
435                         return -EFAULT;
436                 /* the API states this is given in secs */
437                 new_timeout /= 60;
438                 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
439                         return -EINVAL;
440                 timeout = new_timeout;
441                 pc87413_refresh();
442                 /* fall through and return the new timeout... */
443         case WDIOC_GETTIMEOUT:
444                 new_timeout = timeout * 60;
445                 return put_user(new_timeout, uarg.i);
446         default:
447                 return -ENOTTY;
448         }
449 }
450
451 /* -- Notifier funtions -----------------------------------------*/
452
453 /**
454  *      notify_sys:
455  *      @this: our notifier block
456  *      @code: the event being reported
457  *      @unused: unused
458  *
459  *      Our notifier is called on system shutdowns. We want to turn the card
460  *      off at reboot otherwise the machine will reboot again during memory
461  *      test or worse yet during the following fsck. This would suck, in fact
462  *      trust me - if it happens it does suck.
463  */
464
465 static int pc87413_notify_sys(struct notifier_block *this,
466                               unsigned long code,
467                               void *unused)
468 {
469         if (code == SYS_DOWN || code == SYS_HALT)
470                 /* Turn the card off */
471                 pc87413_disable();
472         return NOTIFY_DONE;
473 }
474
475 /* -- Module's structures ---------------------------------------*/
476
477 static const struct file_operations pc87413_fops = {
478         .owner          = THIS_MODULE,
479         .llseek         = no_llseek,
480         .write          = pc87413_write,
481         .unlocked_ioctl = pc87413_ioctl,
482         .open           = pc87413_open,
483         .release        = pc87413_release,
484 };
485
486 static struct notifier_block pc87413_notifier = {
487         .notifier_call  = pc87413_notify_sys,
488 };
489
490 static struct miscdevice pc87413_miscdev = {
491         .minor          = WATCHDOG_MINOR,
492         .name           = "watchdog",
493         .fops           = &pc87413_fops,
494 };
495
496 /* -- Module init functions -------------------------------------*/
497
498 /**
499  *      pc87413_init: module's "constructor"
500  *
501  *      Set up the WDT watchdog board. All we have to do is grab the
502  *      resources we require and bitch if anyone beat us to them.
503  *      The open() function will actually kick the board off.
504  */
505
506 static int __init pc87413_init(void)
507 {
508         int ret;
509
510         printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
511                                                         WDT_INDEX_IO_PORT);
512
513         if (!request_muxed_region(io, 2, MODNAME))
514                 return -EBUSY;
515
516         ret = register_reboot_notifier(&pc87413_notifier);
517         if (ret != 0) {
518                 printk(KERN_ERR PFX
519                         "cannot register reboot notifier (err=%d)\n", ret);
520         }
521
522         ret = misc_register(&pc87413_miscdev);
523         if (ret != 0) {
524                 printk(KERN_ERR PFX
525                         "cannot register miscdev on minor=%d (err=%d)\n",
526                         WATCHDOG_MINOR, ret);
527                 goto reboot_unreg;
528         }
529         printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
530
531         pc87413_select_wdt_out();
532         pc87413_enable_swc();
533         pc87413_get_swc_base_addr();
534
535         if (!request_region(swc_base_addr, 0x20, MODNAME)) {
536                 printk(KERN_ERR PFX
537                         "cannot request SWC region at 0x%x\n", swc_base_addr);
538                 ret = -EBUSY;
539                 goto misc_unreg;
540         }
541
542         pc87413_enable();
543
544         release_region(io, 2);
545         return 0;
546
547 misc_unreg:
548         misc_deregister(&pc87413_miscdev);
549 reboot_unreg:
550         unregister_reboot_notifier(&pc87413_notifier);
551         release_region(io, 2);
552         return ret;
553 }
554
555 /**
556  *      pc87413_exit: module's "destructor"
557  *
558  *      Unload the watchdog. You cannot do this with any file handles open.
559  *      If your watchdog is set to continue ticking on close and you unload
560  *      it, well it keeps ticking. We won't get the interrupt but the board
561  *      will not touch PC memory so all is fine. You just have to load a new
562  *      module in 60 seconds or reboot.
563  */
564
565 static void __exit pc87413_exit(void)
566 {
567         /* Stop the timer before we leave */
568         if (!nowayout) {
569                 pc87413_disable();
570                 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
571         }
572
573         misc_deregister(&pc87413_miscdev);
574         unregister_reboot_notifier(&pc87413_notifier);
575         release_region(swc_base_addr, 0x20);
576
577         printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
578 }
579
580 module_init(pc87413_init);
581 module_exit(pc87413_exit);
582
583 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
584                 "Marcus Junker <junker@anduras.de>,");
585 MODULE_DESCRIPTION("PC87413 WDT driver");
586 MODULE_LICENSE("GPL");
587
588 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
589
590 module_param(io, int, 0);
591 MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
592                                         __MODULE_STRING(IO_DEFAULT) ").");
593
594 module_param(timeout, int, 0);
595 MODULE_PARM_DESC(timeout,
596                 "Watchdog timeout in minutes (default="
597                                 __MODULE_STRING(DEFAULT_TIMEOUT) ").");
598
599 module_param(nowayout, int, 0);
600 MODULE_PARM_DESC(nowayout,
601                 "Watchdog cannot be stopped once started (default="
602                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
603