Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * m8xx_wdt.c - MPC8xx watchdog driver |
| 3 | * |
| 4 | * Author: Florian Schirmer <jolt@tuxbox.org> |
| 5 | * |
| 6 | * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under |
| 7 | * the terms of the GNU General Public License version 2. This program |
| 8 | * is licensed "as is" without any warranty of any kind, whether express |
| 9 | * or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
Tom Rini | fd16230 | 2005-04-16 15:24:23 -0700 | [diff] [blame] | 14 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/sched.h> |
Marcelo Tosatti | c6d95044 | 2005-11-05 13:01:16 -0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/8xx_immap.h> |
| 19 | #include <syslib/m8xx_wdt.h> |
| 20 | |
| 21 | static int wdt_timeout; |
Marcelo Tosatti | fb64c24 | 2005-11-24 11:32:09 -0200 | [diff] [blame] | 22 | int m8xx_has_internal_rtc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Tom Rini | fd16230 | 2005-04-16 15:24:23 -0700 | [diff] [blame] | 24 | static irqreturn_t m8xx_wdt_interrupt(int, void *, struct pt_regs *); |
| 25 | static struct irqaction m8xx_wdt_irqaction = { |
| 26 | .handler = m8xx_wdt_interrupt, |
| 27 | .name = "watchdog", |
| 28 | }; |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | void m8xx_wdt_reset(void) |
| 31 | { |
| 32 | volatile immap_t *imap = (volatile immap_t *)IMAP_ADDR; |
| 33 | |
Marcelo Tosatti | c6d95044 | 2005-11-05 13:01:16 -0200 | [diff] [blame] | 34 | out_be16(&imap->im_siu_conf.sc_swsr, 0x556c); /* write magic1 */ |
| 35 | out_be16(&imap->im_siu_conf.sc_swsr, 0xaa39); /* write magic2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static irqreturn_t m8xx_wdt_interrupt(int irq, void *dev, struct pt_regs *regs) |
| 39 | { |
| 40 | volatile immap_t *imap = (volatile immap_t *)IMAP_ADDR; |
| 41 | |
| 42 | m8xx_wdt_reset(); |
| 43 | |
Marcelo Tosatti | c6d95044 | 2005-11-05 13:01:16 -0200 | [diff] [blame] | 44 | out_be16(&imap->im_sit.sit_piscr, in_be16(&imap->im_sit.sit_piscr) | PISCR_PS); /* clear irq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | return IRQ_HANDLED; |
| 47 | } |
| 48 | |
Marcelo Tosatti | fb64c24 | 2005-11-24 11:32:09 -0200 | [diff] [blame] | 49 | #define SYPCR_SWP 0x1 |
| 50 | #define SYPCR_SWE 0x4 |
| 51 | |
| 52 | |
| 53 | void __init m8xx_wdt_install_irq(volatile immap_t *imap, bd_t *binfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | u32 pitc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | u32 pitrtclk; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Fire trigger if half of the wdt ticked down |
| 60 | */ |
| 61 | |
| 62 | if (imap->im_sit.sit_rtcsc & RTCSC_38K) |
| 63 | pitrtclk = 9600; |
| 64 | else |
| 65 | pitrtclk = 8192; |
| 66 | |
| 67 | if ((wdt_timeout) > (UINT_MAX / pitrtclk)) |
| 68 | pitc = wdt_timeout / binfo->bi_intfreq * pitrtclk / 2; |
| 69 | else |
| 70 | pitc = pitrtclk * wdt_timeout / binfo->bi_intfreq / 2; |
| 71 | |
Marcelo Tosatti | c6d95044 | 2005-11-05 13:01:16 -0200 | [diff] [blame] | 72 | out_be32(&imap->im_sit.sit_pitc, pitc << 16); |
Marcelo Tosatti | e37b0c9 | 2005-10-28 17:46:10 -0700 | [diff] [blame] | 73 | |
Marcelo Tosatti | c6d95044 | 2005-11-05 13:01:16 -0200 | [diff] [blame] | 74 | out_be16(&imap->im_sit.sit_piscr, (mk_int_int_mask(PIT_INTERRUPT) << 8) | PISCR_PIE | PISCR_PTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Tom Rini | fd16230 | 2005-04-16 15:24:23 -0700 | [diff] [blame] | 76 | if (setup_irq(PIT_INTERRUPT, &m8xx_wdt_irqaction)) |
| 77 | panic("m8xx_wdt: error setting up the watchdog irq!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | printk(KERN_NOTICE |
| 80 | "m8xx_wdt: keep-alive trigger installed (PITC: 0x%04X)\n", pitc); |
| 81 | |
Marcelo Tosatti | fb64c24 | 2005-11-24 11:32:09 -0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void m8xx_wdt_timer_func(unsigned long data); |
| 85 | |
| 86 | static struct timer_list m8xx_wdt_timer = |
| 87 | TIMER_INITIALIZER(m8xx_wdt_timer_func, 0, 0); |
| 88 | |
| 89 | void m8xx_wdt_stop_timer(void) |
| 90 | { |
| 91 | del_timer(&m8xx_wdt_timer); |
| 92 | } |
| 93 | |
| 94 | void m8xx_wdt_install_timer(void) |
| 95 | { |
| 96 | m8xx_wdt_timer.expires = jiffies + (HZ/2); |
| 97 | add_timer(&m8xx_wdt_timer); |
| 98 | } |
| 99 | |
| 100 | static void m8xx_wdt_timer_func(unsigned long data) |
| 101 | { |
| 102 | m8xx_wdt_reset(); |
| 103 | m8xx_wdt_install_timer(); |
| 104 | } |
| 105 | |
| 106 | void __init m8xx_wdt_handler_install(bd_t * binfo) |
| 107 | { |
| 108 | volatile immap_t *imap = (volatile immap_t *)IMAP_ADDR; |
| 109 | u32 sypcr; |
| 110 | |
| 111 | sypcr = in_be32(&imap->im_siu_conf.sc_sypcr); |
| 112 | |
| 113 | if (!(sypcr & SYPCR_SWE)) { |
| 114 | printk(KERN_NOTICE "m8xx_wdt: wdt disabled (SYPCR: 0x%08X)\n", |
| 115 | sypcr); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | m8xx_wdt_reset(); |
| 120 | |
| 121 | printk(KERN_NOTICE |
| 122 | "m8xx_wdt: active wdt found (SWTC: 0x%04X, SWP: 0x%01X)\n", |
| 123 | (sypcr >> 16), sypcr & SYPCR_SWP); |
| 124 | |
| 125 | wdt_timeout = (sypcr >> 16) & 0xFFFF; |
| 126 | |
| 127 | if (!wdt_timeout) |
| 128 | wdt_timeout = 0xFFFF; |
| 129 | |
| 130 | if (sypcr & SYPCR_SWP) |
| 131 | wdt_timeout *= 2048; |
| 132 | |
| 133 | m8xx_has_internal_rtc = in_be16(&imap->im_sit.sit_rtcsc) & RTCSC_RTE; |
| 134 | |
| 135 | /* if the internal RTC is off use a kernel timer */ |
| 136 | if (!m8xx_has_internal_rtc) { |
| 137 | if (wdt_timeout < (binfo->bi_intfreq/HZ)) |
| 138 | printk(KERN_ERR "m8xx_wdt: timeout too short for ktimer!\n"); |
| 139 | m8xx_wdt_install_timer(); |
| 140 | } else |
| 141 | m8xx_wdt_install_irq(imap, binfo); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | wdt_timeout /= binfo->bi_intfreq; |
| 144 | } |
| 145 | |
| 146 | int m8xx_wdt_get_timeout(void) |
| 147 | { |
| 148 | return wdt_timeout; |
| 149 | } |