1 /* cpwatchdog.c - driver implementation for hardware watchdog
2 * timers found on Sun Microsystems CP1400 and CP1500 boards.
4 * This device supports both the generic Linux watchdog
5 * interface and Solaris-compatible ioctls as best it is
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.
13 * TODO: DevFS support (/dev/watchdogs/0 ... /dev/watchdogs/2)
15 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
18 #include <linux/kernel.h>
19 #include <linux/module.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>
31 #include <asm/oplib.h>
32 #include <asm/uaccess.h>
34 #include <asm/watchdog.h>
36 #define WD_OBPNAME "watchdog"
37 #define WD_BADMODEL "SUNW,501-5336"
38 #define WD_BTIMEOUT (jiffies + (HZ * 1000))
39 #define WD_BLIMIT 0xFFFF
41 #define WD0_DEVNAME "watchdog0"
42 #define WD1_DEVNAME "watchdog1"
43 #define WD2_DEVNAME "watchdog2"
50 /* Internal driver definitions
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 */
57 #define WD_INTR_OFF 0 /* Interrupt disable value */
58 #define WD_INTR_ON 1 /* Interrupt enable value */
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 */
64 /* Register value definitions
66 #define WD0_INTR_MASK 0x01 /* Watchdog device interrupt masks */
67 #define WD1_INTR_MASK 0x02
68 #define WD2_INTR_MASK 0x04
70 #define WD_S_RUNNING 0x01 /* Watchdog device status running */
71 #define WD_S_EXPIRED 0x02 /* Watchdog device status expired */
73 /* Sun uses Altera PLD EPF8820ATC144-4
74 * providing three hardware watchdogs:
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
80 *** Timer register block definition (struct wd_timer_regblk)
82 * dcntr and limit registers (halfword access):
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'
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
105 *** PLD register block definition (struct wd_pld_regblk)
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
117 * pld_status register (byte access):
118 * UNKNOWN, MAGICAL MYSTERY REGISTER
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 */
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;
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;
148 /* Individual timer structure
153 unsigned char runstatus;
154 volatile struct wd_timer_regblk* regs;
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;
171 static struct wd_device wd_dev = {
172 0, SPIN_LOCK_UNLOCKED, 0, 0, 0, 0,
175 static struct timer_list wd_timer;
177 static int wd0_timeout = 0;
178 static int wd1_timeout = 0;
179 static int wd2_timeout = 0;
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");
190 ("Eric Brower <ebrower@usa.net>");
192 ("Hardware watchdog driver for Sun Microsystems CP1400/1500");
193 MODULE_LICENSE("GPL");
194 MODULE_SUPPORTED_DEVICE
196 #endif /* ifdef MODULE */
198 /* Forward declarations of internal methods
201 static void wd_dumpregs(void);
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);
212 /* PLD expects words to be written in LSB format,
213 * so we must flip all words prior to writing them to regs
215 static inline unsigned short flip_word(unsigned short word)
217 return ((word & 0xff) << 8) | ((word >> 8) & 0xff);
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))
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.
230 static inline int wd_isbroken(void)
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
238 prom_getproperty(prom_root_node, "model", val, sizeof(val));
239 return((!strcmp(val, WD_BADMODEL)) ? 1 : 0);
242 /* Retrieve watchdog-enable? option from OBP
243 * Returns 0 if false, 1 if true
245 static inline int wd_opt_enable(void)
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);
254 /* Retrieve watchdog-reboot? option from OBP
255 * Returns 0 if false, 1 if true
257 static inline int wd_opt_reboot(void)
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);
266 /* Retrieve watchdog-timeout option from OBP
267 * Returns OBP value, or 0 if not located
269 static inline int wd_opt_timeout(void)
275 opt_node = prom_getchild(prom_root_node);
276 opt_node = prom_searchsiblings(opt_node, "options");
277 opt_node = prom_getproperty(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');
292 return((-1 == opt_node) ? (0) : (opt_node));
295 static int wd_open(struct inode *inode, struct file *f)
297 switch(iminor(inode))
300 f->private_data = &wd_dev.watchdog[WD0_ID];
303 f->private_data = &wd_dev.watchdog[WD1_ID];
306 f->private_data = &wd_dev.watchdog[WD2_ID];
312 /* Register IRQ on first open of device */
313 if(0 == wd_dev.initialized)
315 if (request_irq(wd_dev.irq,
319 (void *)wd_dev.regs)) {
320 printk("%s: Cannot register IRQ %s\n",
321 WD_OBPNAME, __irq_itoa(wd_dev.irq));
324 wd_dev.initialized = 1;
330 static int wd_release(struct inode *inode, struct file *file)
335 static int wd_ioctl(struct inode *inode, struct file *file,
336 unsigned int cmd, unsigned long arg)
339 struct wd_timer* pTimer = (struct wd_timer*)file->private_data;
340 struct watchdog_info info = {
343 "Altera EPF8820ATC144-4"
352 /* Generic Linux IOCTLs */
353 case WDIOC_GETSUPPORT:
354 if(copy_to_user((struct watchdog_info *)arg,
355 (struct watchdog_info *)&info,
356 sizeof(struct watchdog_info))) {
360 case WDIOC_GETSTATUS:
361 case WDIOC_GETBOOTSTATUS:
362 if (put_user(0, (int *) arg))
365 case WDIOC_KEEPALIVE:
366 wd_pingtimer(pTimer);
368 case WDIOC_SETOPTIONS:
369 if(copy_from_user(&setopt, (void*) arg, sizeof(unsigned int))) {
372 if(setopt & WDIOS_DISABLECARD) {
373 if(wd_dev.opt_enable) {
375 "%s: cannot disable watchdog in ENABLED mode\n",
379 wd_stoptimer(pTimer);
381 else if(setopt & WDIOS_ENABLECARD) {
382 wd_starttimer(pTimer);
388 /* Solaris-compatible IOCTLs */
390 setopt = wd_getstatus(pTimer);
391 if(copy_to_user((void*)arg, &setopt, sizeof(unsigned int))) {
396 wd_starttimer(pTimer);
399 if(wd_dev.opt_enable) {
400 printk("%s: cannot disable watchdog in ENABLED mode\n",
404 wd_stoptimer(pTimer);
412 static ssize_t wd_write( struct file *file,
417 struct wd_timer* pTimer = (struct wd_timer*)file->private_data;
423 if (ppos != &file->f_pos)
427 wd_pingtimer(pTimer);
433 static ssize_t wd_read(struct file * file, char * buffer,
434 size_t count, loff_t *ppos)
441 #endif /* ifdef WD_DEBUG */
444 static irqreturn_t wd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
446 /* Only WD0 will interrupt-- others are NMI and we won't
449 spin_lock_irq(&wd_dev.lock);
450 if((unsigned long)wd_dev.regs == (unsigned long)dev_id)
452 wd_stoptimer(&wd_dev.watchdog[WD0_ID]);
453 wd_dev.watchdog[WD0_ID].runstatus |= WD_STAT_SVCD;
455 spin_unlock_irq(&wd_dev.lock);
459 static struct file_operations wd_fops = {
460 .owner = THIS_MODULE,
465 .release = wd_release,
468 static struct miscdevice wd0_miscdev = { WD0_MINOR, WD0_DEVNAME, &wd_fops };
469 static struct miscdevice wd1_miscdev = { WD1_MINOR, WD1_DEVNAME, &wd_fops };
470 static struct miscdevice wd2_miscdev = { WD2_MINOR, WD2_DEVNAME, &wd_fops };
473 static void wd_dumpregs(void)
475 /* Reading from downcounters initiates watchdog countdown--
476 * Example is included below for illustration purposes.
479 printk("%s: dumping register values\n", WD_OBPNAME);
480 for(i = WD0_ID; i < WD_NUMDEVS; ++i) {
481 /* printk("\t%s%i: dcntr at 0x%lx: 0x%x\n",
484 * (unsigned long)(&wd_dev.watchdog[i].regs->dcntr),
485 * readw(&wd_dev.watchdog[i].regs->dcntr));
487 printk("\t%s%i: limit at 0x%lx: 0x%x\n",
490 (unsigned long)(&wd_dev.watchdog[i].regs->limit),
491 readw(&wd_dev.watchdog[i].regs->limit));
492 printk("\t%s%i: status at 0x%lx: 0x%x\n",
495 (unsigned long)(&wd_dev.watchdog[i].regs->status),
496 readb(&wd_dev.watchdog[i].regs->status));
497 printk("\t%s%i: driver status: 0x%x\n",
500 wd_getstatus(&wd_dev.watchdog[i]));
502 printk("\tintr_mask at 0x%lx: 0x%x\n",
503 (unsigned long)(&wd_dev.regs->pld_regs.intr_mask),
504 readb(&wd_dev.regs->pld_regs.intr_mask));
505 printk("\tpld_status at 0x%lx: 0x%x\n",
506 (unsigned long)(&wd_dev.regs->pld_regs.status),
507 readb(&wd_dev.regs->pld_regs.status));
511 /* Enable or disable watchdog interrupts
512 * Because of the CP1400 defect this should only be
513 * called during initialzation or by wd_[start|stop]timer()
515 * pTimer - pointer to timer device, or NULL to indicate all timers
516 * enable - non-zero to enable interrupts, zero to disable
518 static void wd_toggleintr(struct wd_timer* pTimer, int enable)
520 unsigned char curregs = wd_readb(&wd_dev.regs->pld_regs.intr_mask);
521 unsigned char setregs =
523 (WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) :
526 (WD_INTR_ON == enable) ?
527 (curregs &= ~setregs):
528 (curregs |= setregs);
530 wd_writeb(curregs, &wd_dev.regs->pld_regs.intr_mask);
534 /* Reset countdown timer with 'limit' value and continue countdown.
535 * This will not start a stopped timer.
537 * pTimer - pointer to timer device
539 static void wd_pingtimer(struct wd_timer* pTimer)
541 if(wd_readb(&pTimer->regs->status) & WD_S_RUNNING) {
542 wd_readw(&pTimer->regs->dcntr);
546 /* Stop a running watchdog timer-- the timer actually keeps
547 * running, but the interrupt is masked so that no action is
548 * taken upon expiration.
550 * pTimer - pointer to timer device
552 static void wd_stoptimer(struct wd_timer* pTimer)
554 if(wd_readb(&pTimer->regs->status) & WD_S_RUNNING) {
555 wd_toggleintr(pTimer, WD_INTR_OFF);
557 if(wd_dev.isbaddoggie) {
558 pTimer->runstatus |= WD_STAT_BSTOP;
559 wd_brokentimer((unsigned long)&wd_dev);
564 /* Start a watchdog timer with the specified limit value
565 * If the watchdog is running, it will be restarted with
566 * the provided limit value.
568 * This function will enable interrupts on the specified
571 * pTimer - pointer to timer device
572 * limit - limit (countdown) value in 1/10th seconds
574 static void wd_starttimer(struct wd_timer* pTimer)
576 if(wd_dev.isbaddoggie) {
577 pTimer->runstatus &= ~WD_STAT_BSTOP;
579 pTimer->runstatus &= ~WD_STAT_SVCD;
581 wd_writew(pTimer->timeout, &pTimer->regs->limit);
582 wd_toggleintr(pTimer, WD_INTR_ON);
585 /* Restarts timer with maximum limit value and
586 * does not unset 'brokenstop' value.
588 static void wd_resetbrokentimer(struct wd_timer* pTimer)
590 wd_toggleintr(pTimer, WD_INTR_ON);
591 wd_writew(WD_BLIMIT, &pTimer->regs->limit);
594 /* Timer device initialization helper.
595 * Returns 0 on success, other on failure
597 static int wd_inittimer(int whichdog)
599 struct miscdevice *whichmisc;
600 volatile struct wd_timer_regblk *whichregs;
608 whichmisc = &wd0_miscdev;
609 strcpy(whichident, "RIC");
610 whichregs = &wd_dev.regs->wd0_regs;
611 whichmask = WD0_INTR_MASK;
612 whichlimit= (0 == wd0_timeout) ?
613 (wd_dev.opt_timeout):
617 whichmisc = &wd1_miscdev;
618 strcpy(whichident, "XIR");
619 whichregs = &wd_dev.regs->wd1_regs;
620 whichmask = WD1_INTR_MASK;
621 whichlimit= (0 == wd1_timeout) ?
622 (wd_dev.opt_timeout):
626 whichmisc = &wd2_miscdev;
627 strcpy(whichident, "POR");
628 whichregs = &wd_dev.regs->wd2_regs;
629 whichmask = WD2_INTR_MASK;
630 whichlimit= (0 == wd2_timeout) ?
631 (wd_dev.opt_timeout):
635 printk("%s: %s: invalid watchdog id: %i\n",
636 WD_OBPNAME, __FUNCTION__, whichdog);
639 if(0 != misc_register(whichmisc))
643 wd_dev.watchdog[whichdog].regs = whichregs;
644 wd_dev.watchdog[whichdog].timeout = whichlimit;
645 wd_dev.watchdog[whichdog].intr_mask = whichmask;
646 wd_dev.watchdog[whichdog].runstatus &= ~WD_STAT_BSTOP;
647 wd_dev.watchdog[whichdog].runstatus |= WD_STAT_INIT;
649 printk("%s%i: %s hardware watchdog [%01i.%i sec] %s\n",
653 wd_dev.watchdog[whichdog].timeout / 10,
654 wd_dev.watchdog[whichdog].timeout % 10,
655 (0 != wd_dev.opt_enable) ? "in ENABLED mode" : "");
659 /* Timer method called to reset stopped watchdogs--
660 * because of the PLD bug on CP1400, we cannot mask
661 * interrupts within the PLD so me must continually
662 * reset the timers ad infinitum.
664 static void wd_brokentimer(unsigned long data)
666 struct wd_device* pDev = (struct wd_device*)data;
669 /* kill a running timer instance, in case we
670 * were called directly instead of by kernel timer
672 if(timer_pending(&wd_timer)) {
673 del_timer(&wd_timer);
676 for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
677 if(pDev->watchdog[id].runstatus & WD_STAT_BSTOP) {
679 wd_resetbrokentimer(&pDev->watchdog[id]);
684 /* there is at least one timer brokenstopped-- reschedule */
685 init_timer(&wd_timer);
686 wd_timer.expires = WD_BTIMEOUT;
687 add_timer(&wd_timer);
691 static int wd_getstatus(struct wd_timer* pTimer)
693 unsigned char stat = wd_readb(&pTimer->regs->status);
694 unsigned char intr = wd_readb(&wd_dev.regs->pld_regs.intr_mask);
695 unsigned char ret = WD_STOPPED;
697 /* determine STOPPED */
701 /* determine EXPIRED vs FREERUN vs RUNNING */
702 else if(WD_S_EXPIRED & stat) {
705 else if(WD_S_RUNNING & stat) {
706 if(intr & pTimer->intr_mask) {
710 /* Fudge WD_EXPIRED status for defective CP1400--
711 * IF timer is running
712 * AND brokenstop is set
713 * AND an interrupt has been serviced
716 * IF timer is running
717 * AND brokenstop is set
718 * AND no interrupt has been serviced
721 if(wd_dev.isbaddoggie && (pTimer->runstatus & WD_STAT_BSTOP)) {
722 if(pTimer->runstatus & WD_STAT_SVCD) {
726 /* we could as well pretend we are expired */
736 /* determine SERVICED */
737 if(pTimer->runstatus & WD_STAT_SVCD) {
744 static int __init wd_init(void)
747 struct linux_ebus *ebus = NULL;
748 struct linux_ebus_device *edev = NULL;
750 for_each_ebus(ebus) {
751 for_each_ebusdev(edev, ebus) {
752 if (!strcmp(edev->prom_name, WD_OBPNAME))
759 printk("%s: unable to locate device\n", WD_OBPNAME);
764 ioremap(edev->resource[0].start, sizeof(struct wd_regblk));
766 if(NULL == wd_dev.regs) {
767 printk("%s: unable to map registers\n", WD_OBPNAME);
771 /* initialize device structure from OBP parameters */
772 wd_dev.irq = edev->irqs[0];
773 wd_dev.opt_enable = wd_opt_enable();
774 wd_dev.opt_reboot = wd_opt_reboot();
775 wd_dev.opt_timeout = wd_opt_timeout();
776 wd_dev.isbaddoggie = wd_isbroken();
778 /* disable all interrupts unless watchdog-enabled? == true */
779 if(! wd_dev.opt_enable) {
780 wd_toggleintr(NULL, WD_INTR_OFF);
783 /* register miscellaneous devices */
784 for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
785 if(0 != wd_inittimer(id)) {
786 printk("%s%i: unable to initialize\n", WD_OBPNAME, id);
790 /* warn about possible defective PLD */
791 if(wd_dev.isbaddoggie) {
792 init_timer(&wd_timer);
793 wd_timer.function = wd_brokentimer;
794 wd_timer.data = (unsigned long)&wd_dev;
795 wd_timer.expires = WD_BTIMEOUT;
797 printk("%s: PLD defect workaround enabled for model %s\n",
798 WD_OBPNAME, WD_BADMODEL);
803 static void __exit wd_cleanup(void)
807 /* if 'watchdog-enable?' == TRUE, timers are not stopped
808 * when module is unloaded. All brokenstopped timers will
809 * also now eventually trip.
811 for(id = WD0_ID; id < WD_NUMDEVS; ++id) {
812 if(WD_S_RUNNING == wd_readb(&wd_dev.watchdog[id].regs->status)) {
813 if(wd_dev.opt_enable) {
814 printk(KERN_WARNING "%s%i: timer not stopped at release\n",
818 wd_stoptimer(&wd_dev.watchdog[id]);
819 if(wd_dev.watchdog[id].runstatus & WD_STAT_BSTOP) {
820 wd_resetbrokentimer(&wd_dev.watchdog[id]);
822 "%s%i: defect workaround disabled at release, "\
823 "timer expires in ~%01i sec\n",
825 wd_readw(&wd_dev.watchdog[id].regs->limit) / 10);
831 if(wd_dev.isbaddoggie && timer_pending(&wd_timer)) {
832 del_timer(&wd_timer);
834 if(0 != (wd_dev.watchdog[WD0_ID].runstatus & WD_STAT_INIT)) {
835 misc_deregister(&wd0_miscdev);
837 if(0 != (wd_dev.watchdog[WD1_ID].runstatus & WD_STAT_INIT)) {
838 misc_deregister(&wd1_miscdev);
840 if(0 != (wd_dev.watchdog[WD2_ID].runstatus & WD_STAT_INIT)) {
841 misc_deregister(&wd2_miscdev);
843 if(0 != wd_dev.initialized) {
844 free_irq(wd_dev.irq, (void *)wd_dev.regs);
846 iounmap(wd_dev.regs);
849 module_init(wd_init);
850 module_exit(wd_cleanup);