VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / sbus / char / cpwatchdog.c
1 /* cpwatchdog.c - driver implementation for hardware watchdog
2  * timers found on Sun Microsystems CP1400 and CP1500 boards.
3  *
4  * This device supports both the generic Linux watchdog 
5  * interface and Solaris-compatible ioctls as best it is
6  * able.
7  *
8  * NOTE:        CP1400 systems appear to have a defective intr_mask
9  *                      register on the PLD, preventing the disabling of
10  *                      timer interrupts.  We use a timer to periodically 
11  *                      reset 'stopped' watchdogs on affected platforms.
12  *
13  * TODO:        DevFS support (/dev/watchdogs/0 ... /dev/watchdogs/2)
14  *
15  * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/errno.h>
22 #include <linux/major.h>
23 #include <linux/init.h>
24 #include <linux/miscdevice.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioport.h>
28 #include <linux/timer.h>
29 #include <asm/irq.h>
30 #include <asm/ebus.h>
31 #include <asm/oplib.h>
32 #include <asm/uaccess.h>
33
34 #include <asm/watchdog.h>
35
36 #define WD_OBPNAME      "watchdog"
37 #define WD_BADMODEL "SUNW,501-5336"
38 #define WD_BTIMEOUT     (jiffies + (HZ * 1000))
39 #define WD_BLIMIT       0xFFFF
40
41 #define WD0_DEVNAME "watchdog0"
42 #define WD1_DEVNAME "watchdog1"
43 #define WD2_DEVNAME "watchdog2"
44
45 #define WD0_MINOR       212
46 #define WD1_MINOR       213     
47 #define WD2_MINOR       214     
48
49
50 /* Internal driver definitions
51  */
52 #define WD0_ID                  0               /* Watchdog0                                            */
53 #define WD1_ID                  1               /* Watchdog1                                            */
54 #define WD2_ID                  2               /* Watchdog2                                            */
55 #define WD_NUMDEVS              3               /* Device contains 3 timers                     */
56
57 #define WD_INTR_OFF             0               /* Interrupt disable value                      */
58 #define WD_INTR_ON              1               /* Interrupt enable value                       */
59
60 #define WD_STAT_INIT    0x01    /* Watchdog timer is initialized        */
61 #define WD_STAT_BSTOP   0x02    /* Watchdog timer is brokenstopped      */
62 #define WD_STAT_SVCD    0x04    /* Watchdog interrupt occurred          */
63
64 /* Register value definitions
65  */
66 #define WD0_INTR_MASK   0x01    /* Watchdog device interrupt masks      */
67 #define WD1_INTR_MASK   0x02
68 #define WD2_INTR_MASK   0x04
69
70 #define WD_S_RUNNING    0x01    /* Watchdog device status running       */
71 #define WD_S_EXPIRED    0x02    /* Watchdog device status expired       */
72
73 /* Sun uses Altera PLD EPF8820ATC144-4 
74  * providing three hardware watchdogs:
75  *
76  *      1) RIC - sends an interrupt when triggered
77  *      2) XIR - asserts XIR_B_RESET when triggered, resets CPU
78  *      3) POR - asserts POR_B_RESET when triggered, resets CPU, backplane, board
79  *
80  *** Timer register block definition (struct wd_timer_regblk)
81  *
82  * dcntr and limit registers (halfword access):      
83  * -------------------
84  * | 15 | ...| 1 | 0 |
85  * -------------------
86  * |-  counter val  -|
87  * -------------------
88  * dcntr -      Current 16-bit downcounter value.
89  *                      When downcounter reaches '0' watchdog expires.
90  *                      Reading this register resets downcounter with 'limit' value.
91  * limit -      16-bit countdown value in 1/10th second increments.
92  *                      Writing this register begins countdown with input value.
93  *                      Reading from this register does not affect counter.
94  * NOTES:       After watchdog reset, dcntr and limit contain '1'
95  *
96  * status register (byte access):
97  * ---------------------------
98  * | 7 | ... | 2 |  1  |  0  |
99  * --------------+------------
100  * |-   UNUSED  -| EXP | RUN |
101  * ---------------------------
102  * status-      Bit 0 - Watchdog is running
103  *                      Bit 1 - Watchdog has expired
104  *
105  *** PLD register block definition (struct wd_pld_regblk)
106  *
107  * intr_mask register (byte access):
108  * ---------------------------------
109  * | 7 | ... | 3 |  2  |  1  |  0  |
110  * +-------------+------------------
111  * |-   UNUSED  -| WD3 | WD2 | WD1 |
112  * ---------------------------------
113  * WD3 -  1 == Interrupt disabled for watchdog 3
114  * WD2 -  1 == Interrupt disabled for watchdog 2
115  * WD1 -  1 == Interrupt disabled for watchdog 1
116  *
117  * pld_status register (byte access):
118  * UNKNOWN, MAGICAL MYSTERY REGISTER
119  *
120  */
121 struct wd_timer_regblk {
122         volatile __u16  dcntr;          /* down counter         - hw    */
123         volatile __u16  dcntr_pad;
124         volatile __u16  limit;          /* limit register       - hw    */
125         volatile __u16  limit_pad;
126         volatile __u8   status;         /* status register      - b             */
127         volatile __u8   status_pad;
128         volatile __u16  status_pad2;
129         volatile __u32  pad32;          /* yet more padding                     */
130 };
131
132 struct wd_pld_regblk {
133         volatile __u8   intr_mask;      /* interrupt mask       - b             */
134         volatile __u8   intr_mask_pad;
135         volatile __u16  intr_mask_pad2;
136         volatile __u8   status;         /* device status        - b             */
137         volatile __u8   status_pad;
138         volatile __u16  status_pad2;
139 };
140
141 struct wd_regblk {
142         volatile struct wd_timer_regblk         wd0_regs;
143         volatile struct wd_timer_regblk         wd1_regs;
144         volatile struct wd_timer_regblk         wd2_regs;
145         volatile struct wd_pld_regblk           pld_regs;
146 };
147
148 /* Individual timer structure 
149  */
150 struct wd_timer {
151         __u16                   timeout;
152         __u8                    intr_mask;
153         unsigned char   runstatus;
154         volatile struct wd_timer_regblk* regs;
155 };
156
157 /* Device structure
158  */
159 struct wd_device {
160         int                             irq;
161         spinlock_t              lock;
162         unsigned char   isbaddoggie;    /* defective PLD */
163         unsigned char   opt_enable;
164         unsigned char   opt_reboot;
165         unsigned short  opt_timeout;
166         unsigned char   initialized;
167         struct wd_timer watchdog[WD_NUMDEVS];
168         volatile struct wd_regblk* regs;
169 };
170
171 static struct wd_device wd_dev = { 
172                 0, SPIN_LOCK_UNLOCKED, 0, 0, 0, 0,
173 };
174
175 static struct timer_list wd_timer;
176
177 static int wd0_timeout = 0;
178 static int wd1_timeout = 0;
179 static int wd2_timeout = 0;
180
181 #ifdef MODULE
182 MODULE_PARM             (wd0_timeout, "i");
183 MODULE_PARM_DESC(wd0_timeout, "Default watchdog0 timeout in 1/10secs");
184 MODULE_PARM     (wd1_timeout, "i");
185 MODULE_PARM_DESC(wd1_timeout, "Default watchdog1 timeout in 1/10secs");
186 MODULE_PARM     (wd2_timeout, "i");
187 MODULE_PARM_DESC(wd2_timeout, "Default watchdog2 timeout in 1/10secs");
188
189 MODULE_AUTHOR
190         ("Eric Brower <ebrower@usa.net>");
191 MODULE_DESCRIPTION
192         ("Hardware watchdog driver for Sun Microsystems CP1400/1500");
193 MODULE_LICENSE("GPL");
194 MODULE_SUPPORTED_DEVICE
195         ("watchdog");
196 #endif /* ifdef MODULE */
197
198 /* Forward declarations of internal methods
199  */
200 #ifdef WD_DEBUG
201 static void wd_dumpregs(void);
202 #endif
203 static irqreturn_t wd_interrupt(int irq, void *dev_id, struct pt_regs *regs);
204 static void wd_toggleintr(struct wd_timer* pTimer, int enable);
205 static void wd_pingtimer(struct wd_timer* pTimer);
206 static void wd_starttimer(struct wd_timer* pTimer);
207 static void wd_resetbrokentimer(struct wd_timer* pTimer);
208 static void wd_stoptimer(struct wd_timer* pTimer);
209 static void wd_brokentimer(unsigned long data);
210 static int  wd_getstatus(struct wd_timer* pTimer);
211
212 /* PLD expects words to be written in LSB format,
213  * so we must flip all words prior to writing them to regs
214  */
215 static inline unsigned short flip_word(unsigned short word)
216 {
217         return ((word & 0xff) << 8) | ((word >> 8) & 0xff);
218 }
219
220 #define wd_writew(val, addr)    (writew(flip_word(val), addr))
221 #define wd_readw(addr)                  (flip_word(readw(addr)))
222 #define wd_writeb(val, addr)    (writeb(val, addr))
223 #define wd_readb(addr)                  (readb(addr))
224
225
226 /* CP1400s seem to have broken PLD implementations--
227  * the interrupt_mask register cannot be written, so
228  * no timer interrupts can be masked within the PLD.
229  */
230 static inline int wd_isbroken(void)
231 {
232         /* we could test this by read/write/read/restore
233          * on the interrupt mask register only if OBP
234          * 'watchdog-enable?' == FALSE, but it seems 
235          * ubiquitous on CP1400s
236          */
237         char val[32];
238         prom_getproperty(prom_root_node, "model", val, sizeof(val));
239         return((!strcmp(val, WD_BADMODEL)) ? 1 : 0);
240 }
241                 
242 /* Retrieve watchdog-enable? option from OBP
243  * Returns 0 if false, 1 if true
244  */
245 static inline int wd_opt_enable(void)
246 {
247         int opt_node;
248
249         opt_node = prom_getchild(prom_root_node);
250         opt_node = prom_searchsiblings(opt_node, "options");
251         return((-1 == prom_getint(opt_node, "watchdog-enable?")) ? 0 : 1);
252 }
253
254 /* Retrieve watchdog-reboot? option from OBP
255  * Returns 0 if false, 1 if true
256  */
257 static inline int wd_opt_reboot(void)
258 {
259         int opt_node;
260
261         opt_node = prom_getchild(prom_root_node);
262         opt_node = prom_searchsiblings(opt_node, "options");
263         return((-1 == prom_getint(opt_node, "watchdog-reboot?")) ? 0 : 1);
264 }
265
266 /* Retrieve watchdog-timeout option from OBP
267  * Returns OBP value, or 0 if not located
268  */
269 static inline int wd_opt_timeout(void)
270 {
271         int opt_node;
272         char value[32];
273         char *p = value;
274
275         opt_node = prom_getchild(prom_root_node);
276         opt_node = prom_searchsiblings(opt_node, "options");
277         opt_node = prom_getproperty(opt_node, 
278                                                                 "watchdog-timeout", 
279                                                                 value, 
280                                                                 sizeof(value));
281         if(-1 != opt_node) {
282                 /* atoi implementation */
283                 for(opt_node = 0; /* nop */; p++) {
284                         if(*p >= '0' && *p <= '9') {
285                                 opt_node = (10*opt_node)+(*p-'0');
286                         }
287                         else {
288                                 break;
289                         }
290                 }
291         }
292         return((-1 == opt_node) ? (0) : (opt_node)); 
293 }
294
295 static int wd_open(struct inode *inode, struct file *f)
296 {
297         switch(iminor(inode))
298         {
299                 case WD0_MINOR:
300                         f->private_data = &wd_dev.watchdog[WD0_ID];
301                         break;
302                 case WD1_MINOR:
303                         f->private_data = &wd_dev.watchdog[WD1_ID];
304                         break;
305                 case WD2_MINOR:
306                         f->private_data = &wd_dev.watchdog[WD2_ID];
307                         break;
308                 default:
309                         return(-ENODEV);
310         }
311
312         /* Register IRQ on first open of device */
313         if(0 == wd_dev.initialized)
314         {       
315                 if (request_irq(wd_dev.irq, 
316                                                 &wd_interrupt, 
317                                                 SA_SHIRQ,
318                                                 WD_OBPNAME,
319                                                 (void *)wd_dev.regs)) {
320                         printk("%s: Cannot register IRQ %s\n", 
321                                 WD_OBPNAME, __irq_itoa(wd_dev.irq));
322                         return(-EBUSY);
323                 }
324                 wd_dev.initialized = 1;
325         }
326
327         return(nonseekable_open(inode, f));
328 }
329
330 static int wd_release(struct inode *inode, struct file *file)
331 {
332         return 0;
333 }
334
335 static int wd_ioctl(struct inode *inode, struct file *file, 
336                      unsigned int cmd, unsigned long arg)
337 {
338         int     setopt                          = 0;
339         struct  wd_timer* pTimer        = (struct wd_timer*)file->private_data;
340         void __user *argp = (void __user *)arg;
341         struct  watchdog_info info      = {
342                 0,
343                 0,
344                 "Altera EPF8820ATC144-4"
345         };
346
347         if(NULL == pTimer) {
348                 return(-EINVAL);
349         }
350
351         switch(cmd)
352         {
353                 /* Generic Linux IOCTLs */
354                 case WDIOC_GETSUPPORT:
355                         if(copy_to_user(argp, &info, sizeof(struct watchdog_info))) {
356                                 return(-EFAULT);
357                         }
358                         break;
359                 case WDIOC_GETSTATUS:
360                 case WDIOC_GETBOOTSTATUS:
361                         if (put_user(0, (int __user *)argp))
362                                 return -EFAULT;
363                         break;
364                 case WDIOC_KEEPALIVE:
365                         wd_pingtimer(pTimer);
366                         break;
367                 case WDIOC_SETOPTIONS:
368                         if(copy_from_user(&setopt, argp, sizeof(unsigned int))) {
369                                 return -EFAULT;
370                         }
371                         if(setopt & WDIOS_DISABLECARD) {
372                                 if(wd_dev.opt_enable) {
373                                         printk(
374                                                 "%s: cannot disable watchdog in ENABLED mode\n",
375                                                 WD_OBPNAME);
376                                         return(-EINVAL);
377                                 }
378                                 wd_stoptimer(pTimer);
379                         }
380                         else if(setopt & WDIOS_ENABLECARD) {
381                                 wd_starttimer(pTimer);
382                         }
383                         else {
384                                 return(-EINVAL);
385                         }       
386                         break;
387                 /* Solaris-compatible IOCTLs */
388                 case WIOCGSTAT:
389                         setopt = wd_getstatus(pTimer);
390                         if(copy_to_user(argp, &setopt, sizeof(unsigned int))) {
391                                 return(-EFAULT);
392                         }
393                         break;
394                 case WIOCSTART:
395                         wd_starttimer(pTimer);
396                         break;
397                 case WIOCSTOP:
398                         if(wd_dev.opt_enable) {
399                                 printk("%s: cannot disable watchdog in ENABLED mode\n",
400                                         WD_OBPNAME);
401                                 return(-EINVAL);
402                         }
403                         wd_stoptimer(pTimer);
404                         break;
405                 default:
406                         return(-EINVAL);
407         }
408         return(0);
409 }
410
411 static ssize_t wd_write(struct file     *file, 
412                         const char      __user *buf, 
413                         size_t          count, 
414                         loff_t          *ppos)
415 {
416         struct wd_timer* pTimer = (struct wd_timer*)file->private_data;
417
418         if(NULL == pTimer) {
419                 return(-EINVAL);
420         }
421
422         if (count) {
423                 wd_pingtimer(pTimer);
424                 return 1;
425         }
426         return 0;
427 }
428
429 static ssize_t wd_read(struct file * file, char __user *buffer,
430                         size_t count, loff_t *ppos)
431 {
432 #ifdef WD_DEBUG
433         wd_dumpregs();
434         return(0);
435 #else
436         return(-EINVAL);
437 #endif /* ifdef WD_DEBUG */
438 }
439
440 static irqreturn_t wd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
441 {
442         /* Only WD0 will interrupt-- others are NMI and we won't
443          * see them here....
444          */
445         spin_lock_irq(&wd_dev.lock);
446         if((unsigned long)wd_dev.regs == (unsigned long)dev_id)
447         {
448                 wd_stoptimer(&wd_dev.watchdog[WD0_ID]);
449                 wd_dev.watchdog[WD0_ID].runstatus |=  WD_STAT_SVCD;
450         }
451         spin_unlock_irq(&wd_dev.lock);
452         return IRQ_HANDLED;
453 }
454
455 static struct file_operations wd_fops = {
456         .owner =        THIS_MODULE,
457         .ioctl =        wd_ioctl,
458         .open =         wd_open,
459         .write =        wd_write,
460         .read =         wd_read,
461         .release =      wd_release,
462 };
463
464 static struct miscdevice wd0_miscdev = { WD0_MINOR, WD0_DEVNAME, &wd_fops };
465 static struct miscdevice wd1_miscdev = { WD1_MINOR, WD1_DEVNAME, &wd_fops };
466 static struct miscdevice wd2_miscdev = { WD2_MINOR, WD2_DEVNAME, &wd_fops };
467
468 #ifdef WD_DEBUG
469 static void wd_dumpregs(void)
470 {
471         /* Reading from downcounters initiates watchdog countdown--
472          * Example is included below for illustration purposes.
473          */
474         int i;
475         printk("%s: dumping register values\n", WD_OBPNAME);
476         for(i = WD0_ID; i < WD_NUMDEVS; ++i) {
477                         /* printk("\t%s%i: dcntr  at 0x%lx: 0x%x\n", 
478                          *      WD_OBPNAME,
479                          *      i,
480                          *      (unsigned long)(&wd_dev.watchdog[i].regs->dcntr), 
481                          *      readw(&wd_dev.watchdog[i].regs->dcntr));
482                          */
483                         printk("\t%s%i: limit  at 0x%lx: 0x%x\n", 
484                                 WD_OBPNAME,
485                                 i,
486                                 (unsigned long)(&wd_dev.watchdog[i].regs->limit), 
487                                 readw(&wd_dev.watchdog[i].regs->limit));
488                         printk("\t%s%i: status at 0x%lx: 0x%x\n", 
489                                 WD_OBPNAME,
490                                 i,
491                                 (unsigned long)(&wd_dev.watchdog[i].regs->status), 
492                                 readb(&wd_dev.watchdog[i].regs->status));
493                         printk("\t%s%i: driver status: 0x%x\n",
494                                 WD_OBPNAME,
495                                 i,
496                                 wd_getstatus(&wd_dev.watchdog[i]));
497         }
498         printk("\tintr_mask  at 0x%lx: 0x%x\n", 
499                 (unsigned long)(&wd_dev.regs->pld_regs.intr_mask), 
500                 readb(&wd_dev.regs->pld_regs.intr_mask));
501         printk("\tpld_status at 0x%lx: 0x%x\n", 
502                 (unsigned long)(&wd_dev.regs->pld_regs.status), 
503                 readb(&wd_dev.regs->pld_regs.status));
504 }
505 #endif
506
507 /* Enable or disable watchdog interrupts
508  * Because of the CP1400 defect this should only be
509  * called during initialzation or by wd_[start|stop]timer()
510  *
511  * pTimer       - pointer to timer device, or NULL to indicate all timers 
512  * enable       - non-zero to enable interrupts, zero to disable
513  */
514 static void wd_toggleintr(struct wd_timer* pTimer, int enable)
515 {
516         unsigned char curregs = wd_readb(&wd_dev.regs->pld_regs.intr_mask);
517         unsigned char setregs = 
518                 (NULL == pTimer) ? 
519                         (WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) : 
520                         (pTimer->intr_mask);
521
522         (WD_INTR_ON == enable) ?
523                 (curregs &= ~setregs):
524                 (curregs |=  setregs);
525
526         wd_writeb(curregs, &wd_dev.regs->pld_regs.intr_mask);
527         return;
528 }
529
530 /* Reset countdown timer with 'limit' value and continue countdown.
531  * This will not start a stopped timer.
532  *
533  * pTimer       - pointer to timer device
534  */
535 static void wd_pingtimer(struct wd_timer* pTimer)
536 {
537         if(wd_readb(&pTimer->regs->status) & WD_S_RUNNING) {
538                 wd_readw(&pTimer->regs->dcntr);
539         }
540 }
541
542 /* Stop a running watchdog timer-- the timer actually keeps
543  * running, but the interrupt is masked so that no action is
544  * taken upon expiration.
545  *
546  * pTimer       - pointer to timer device
547  */
548 static void wd_stoptimer(struct wd_timer* pTimer)
549 {
550         if(wd_readb(&pTimer->regs->status) & WD_S_RUNNING) {
551                 wd_toggleintr(pTimer, WD_INTR_OFF);
552
553                 if(wd_dev.isbaddoggie) {
554                         pTimer->runstatus |= WD_STAT_BSTOP;
555                         wd_brokentimer((unsigned long)&wd_dev);
556                 }
557         }
558 }
559
560 /* Start a watchdog timer with the specified limit value
561  * If the watchdog is running, it will be restarted with
562  * the provided limit value.
563  *
564  * This function will enable interrupts on the specified
565  * watchdog.
566  *
567  * pTimer       - pointer to timer device
568  * limit        - limit (countdown) value in 1/10th seconds
569  */
570 static void wd_starttimer(struct wd_timer* pTimer)
571 {
572         if(wd_dev.isbaddoggie) {
573                 pTimer->runstatus &= ~WD_STAT_BSTOP;
574         }
575         pTimer->runstatus &= ~WD_STAT_SVCD;
576
577         wd_writew(pTimer->timeout, &pTimer->regs->limit);
578         wd_toggleintr(pTimer, WD_INTR_ON);
579 }
580
581 /* Restarts timer with maximum limit value and
582  * does not unset 'brokenstop' value.
583  */
584 static void wd_resetbrokentimer(struct wd_timer* pTimer)
585 {
586         wd_toggleintr(pTimer, WD_INTR_ON);
587         wd_writew(WD_BLIMIT, &pTimer->regs->limit);
588 }
589
590 /* Timer device initialization helper.
591  * Returns 0 on success, other on failure
592  */
593 static int wd_inittimer(int whichdog)
594 {
595         struct miscdevice                               *whichmisc;
596         volatile struct wd_timer_regblk *whichregs;
597         char                                                    whichident[8];
598         int                                                             whichmask;
599         __u16                                                   whichlimit;
600
601         switch(whichdog)
602         {
603                 case WD0_ID:
604                         whichmisc = &wd0_miscdev;
605                         strcpy(whichident, "RIC");
606                         whichregs = &wd_dev.regs->wd0_regs;
607                         whichmask = WD0_INTR_MASK;
608                         whichlimit= (0 == wd0_timeout)  ? 
609                                                 (wd_dev.opt_timeout): 
610                                                 (wd0_timeout);
611                         break;
612                 case WD1_ID:
613                         whichmisc = &wd1_miscdev;
614                         strcpy(whichident, "XIR");
615                         whichregs = &wd_dev.regs->wd1_regs;
616                         whichmask = WD1_INTR_MASK;
617                         whichlimit= (0 == wd1_timeout)  ? 
618                                                 (wd_dev.opt_timeout): 
619                                                 (wd1_timeout);
620                         break;
621                 case WD2_ID:
622                         whichmisc = &wd2_miscdev;
623                         strcpy(whichident, "POR");
624                         whichregs = &wd_dev.regs->wd2_regs;
625                         whichmask = WD2_INTR_MASK;
626                         whichlimit= (0 == wd2_timeout)  ? 
627                                                 (wd_dev.opt_timeout): 
628                                                 (wd2_timeout);
629                         break;
630                 default:
631                         printk("%s: %s: invalid watchdog id: %i\n",
632                                 WD_OBPNAME, __FUNCTION__, whichdog);
633                         return(1);
634         }
635         if(0 != misc_register(whichmisc))
636         {
637                 return(1);
638         }
639         wd_dev.watchdog[whichdog].regs                  = whichregs;
640         wd_dev.watchdog[whichdog].timeout               = whichlimit;
641         wd_dev.watchdog[whichdog].intr_mask             = whichmask;
642         wd_dev.watchdog[whichdog].runstatus     &= ~WD_STAT_BSTOP;
643         wd_dev.watchdog[whichdog].runstatus     |= WD_STAT_INIT;
644
645         printk("%s%i: %s hardware watchdog [%01i.%i sec] %s\n", 
646                 WD_OBPNAME, 
647                 whichdog, 
648                 whichident, 
649                 wd_dev.watchdog[whichdog].timeout / 10,
650                 wd_dev.watchdog[whichdog].timeout % 10,
651                 (0 != wd_dev.opt_enable) ? "in ENABLED mode" : "");
652         return(0);
653 }
654
655 /* Timer method called to reset stopped watchdogs--
656  * because of the PLD bug on CP1400, we cannot mask
657  * interrupts within the PLD so me must continually
658  * reset the timers ad infinitum.
659  */
660 static void wd_brokentimer(unsigned long data)
661 {
662         struct wd_device* pDev = (struct wd_device*)data;
663         int id, tripped = 0;
664
665         /* kill a running timer instance, in case we
666          * were called directly instead of by kernel timer
667          */
668         if(timer_pending(&wd_timer)) {
669                 del_timer(&wd_timer);
670         }
671
672         for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
673                 if(pDev->watchdog[id].runstatus & WD_STAT_BSTOP) {
674                         ++tripped;
675                         wd_resetbrokentimer(&pDev->watchdog[id]);
676                 }
677         }
678
679         if(tripped) {
680                 /* there is at least one timer brokenstopped-- reschedule */
681                 init_timer(&wd_timer);
682                 wd_timer.expires = WD_BTIMEOUT;
683                 add_timer(&wd_timer);
684         }
685 }
686
687 static int wd_getstatus(struct wd_timer* pTimer)
688 {
689         unsigned char stat = wd_readb(&pTimer->regs->status);
690         unsigned char intr = wd_readb(&wd_dev.regs->pld_regs.intr_mask);
691         unsigned char ret  = WD_STOPPED;
692
693         /* determine STOPPED */
694         if(0 == stat ) { 
695                 return(ret);
696         }
697         /* determine EXPIRED vs FREERUN vs RUNNING */
698         else if(WD_S_EXPIRED & stat) {
699                 ret = WD_EXPIRED;
700         }
701         else if(WD_S_RUNNING & stat) {
702                 if(intr & pTimer->intr_mask) {
703                         ret = WD_FREERUN;
704                 }
705                 else {
706                         /* Fudge WD_EXPIRED status for defective CP1400--
707                          * IF timer is running 
708                          *      AND brokenstop is set 
709                          *      AND an interrupt has been serviced
710                          * we are WD_EXPIRED.
711                          *
712                          * IF timer is running 
713                          *      AND brokenstop is set 
714                          *      AND no interrupt has been serviced
715                          * we are WD_FREERUN.
716                          */
717                         if(wd_dev.isbaddoggie && (pTimer->runstatus & WD_STAT_BSTOP)) {
718                                 if(pTimer->runstatus & WD_STAT_SVCD) {
719                                         ret = WD_EXPIRED;
720                                 }
721                                 else {
722                                         /* we could as well pretend we are expired */
723                                         ret = WD_FREERUN;
724                                 }
725                         }
726                         else {
727                                 ret = WD_RUNNING;
728                         }
729                 }
730         }
731
732         /* determine SERVICED */
733         if(pTimer->runstatus & WD_STAT_SVCD) {
734                 ret |= WD_SERVICED;
735         }
736
737         return(ret);
738 }
739
740 static int __init wd_init(void)
741 {
742         int     id;
743         struct  linux_ebus *ebus = NULL;
744         struct  linux_ebus_device *edev = NULL;
745
746         for_each_ebus(ebus) {
747                 for_each_ebusdev(edev, ebus) {
748                         if (!strcmp(edev->prom_name, WD_OBPNAME))
749                                 goto ebus_done;
750                 }
751         }
752
753 ebus_done:
754         if(!edev) {
755                 printk("%s: unable to locate device\n", WD_OBPNAME);
756                 return -ENODEV;
757         }
758
759         wd_dev.regs = 
760                 ioremap(edev->resource[0].start, sizeof(struct wd_regblk));
761
762         if(NULL == wd_dev.regs) {
763                 printk("%s: unable to map registers\n", WD_OBPNAME);
764                 return(-ENODEV);
765         }
766
767         /* initialize device structure from OBP parameters */
768         wd_dev.irq                      = edev->irqs[0];
769         wd_dev.opt_enable       = wd_opt_enable();
770         wd_dev.opt_reboot       = wd_opt_reboot();
771         wd_dev.opt_timeout      = wd_opt_timeout();
772         wd_dev.isbaddoggie      = wd_isbroken();
773
774         /* disable all interrupts unless watchdog-enabled? == true */
775         if(! wd_dev.opt_enable) {
776                 wd_toggleintr(NULL, WD_INTR_OFF);
777         }
778
779         /* register miscellaneous devices */
780         for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
781                 if(0 != wd_inittimer(id)) {
782                         printk("%s%i: unable to initialize\n", WD_OBPNAME, id);
783                 }
784         }
785
786         /* warn about possible defective PLD */
787         if(wd_dev.isbaddoggie) {
788                 init_timer(&wd_timer);
789                 wd_timer.function       = wd_brokentimer;
790                 wd_timer.data           = (unsigned long)&wd_dev;
791                 wd_timer.expires        = WD_BTIMEOUT;
792
793                 printk("%s: PLD defect workaround enabled for model %s\n",
794                         WD_OBPNAME, WD_BADMODEL);
795         }
796         return(0);
797 }
798
799 static void __exit wd_cleanup(void)
800 {
801         int id;
802
803         /* if 'watchdog-enable?' == TRUE, timers are not stopped 
804          * when module is unloaded.  All brokenstopped timers will
805          * also now eventually trip. 
806          */
807         for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
808                 if(WD_S_RUNNING == wd_readb(&wd_dev.watchdog[id].regs->status)) {
809                         if(wd_dev.opt_enable) {
810                                 printk(KERN_WARNING "%s%i: timer not stopped at release\n",
811                                         WD_OBPNAME, id);
812                         }
813                         else {
814                                 wd_stoptimer(&wd_dev.watchdog[id]);
815                                 if(wd_dev.watchdog[id].runstatus & WD_STAT_BSTOP) {
816                                         wd_resetbrokentimer(&wd_dev.watchdog[id]);
817                                         printk(KERN_WARNING 
818                                                         "%s%i: defect workaround disabled at release, "\
819                                                         "timer expires in ~%01i sec\n",
820                                                         WD_OBPNAME, id, 
821                                                         wd_readw(&wd_dev.watchdog[id].regs->limit) / 10);
822                                 }
823                         }
824                 }
825         }
826
827         if(wd_dev.isbaddoggie && timer_pending(&wd_timer)) {
828                 del_timer(&wd_timer);
829         }
830         if(0 != (wd_dev.watchdog[WD0_ID].runstatus & WD_STAT_INIT)) {
831                 misc_deregister(&wd0_miscdev);
832         }
833         if(0 != (wd_dev.watchdog[WD1_ID].runstatus & WD_STAT_INIT)) {
834                 misc_deregister(&wd1_miscdev);
835         }
836         if(0 != (wd_dev.watchdog[WD2_ID].runstatus & WD_STAT_INIT)) {
837                 misc_deregister(&wd2_miscdev);
838         }
839         if(0 != wd_dev.initialized) {
840                 free_irq(wd_dev.irq, (void *)wd_dev.regs);
841         }
842         iounmap(wd_dev.regs);
843 }
844
845 module_init(wd_init);
846 module_exit(wd_cleanup);