1*b7e04f8cSWim Van Sebroeck /* 2*b7e04f8cSWim Van Sebroeck * drivers/char/watchdog/shwdt.c 3*b7e04f8cSWim Van Sebroeck * 4*b7e04f8cSWim Van Sebroeck * Watchdog driver for integrated watchdog in the SuperH processors. 5*b7e04f8cSWim Van Sebroeck * 6*b7e04f8cSWim Van Sebroeck * Copyright (C) 2001, 2002, 2003 Paul Mundt <lethal@linux-sh.org> 7*b7e04f8cSWim Van Sebroeck * 8*b7e04f8cSWim Van Sebroeck * This program is free software; you can redistribute it and/or modify it 9*b7e04f8cSWim Van Sebroeck * under the terms of the GNU General Public License as published by the 10*b7e04f8cSWim Van Sebroeck * Free Software Foundation; either version 2 of the License, or (at your 11*b7e04f8cSWim Van Sebroeck * option) any later version. 12*b7e04f8cSWim Van Sebroeck * 13*b7e04f8cSWim Van Sebroeck * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> 14*b7e04f8cSWim Van Sebroeck * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT 15*b7e04f8cSWim Van Sebroeck * 16*b7e04f8cSWim Van Sebroeck * 19-Apr-2002 Rob Radez <rob@osinvestor.com> 17*b7e04f8cSWim Van Sebroeck * Added expect close support, made emulated timeout runtime changeable 18*b7e04f8cSWim Van Sebroeck * general cleanups, add some ioctls 19*b7e04f8cSWim Van Sebroeck */ 20*b7e04f8cSWim Van Sebroeck #include <linux/module.h> 21*b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h> 22*b7e04f8cSWim Van Sebroeck #include <linux/init.h> 23*b7e04f8cSWim Van Sebroeck #include <linux/types.h> 24*b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h> 25*b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h> 26*b7e04f8cSWim Van Sebroeck #include <linux/reboot.h> 27*b7e04f8cSWim Van Sebroeck #include <linux/notifier.h> 28*b7e04f8cSWim Van Sebroeck #include <linux/ioport.h> 29*b7e04f8cSWim Van Sebroeck #include <linux/fs.h> 30*b7e04f8cSWim Van Sebroeck #include <linux/mm.h> 31*b7e04f8cSWim Van Sebroeck #include <asm/io.h> 32*b7e04f8cSWim Van Sebroeck #include <asm/uaccess.h> 33*b7e04f8cSWim Van Sebroeck #include <asm/watchdog.h> 34*b7e04f8cSWim Van Sebroeck 35*b7e04f8cSWim Van Sebroeck #define PFX "shwdt: " 36*b7e04f8cSWim Van Sebroeck 37*b7e04f8cSWim Van Sebroeck /* 38*b7e04f8cSWim Van Sebroeck * Default clock division ratio is 5.25 msecs. For an additional table of 39*b7e04f8cSWim Van Sebroeck * values, consult the asm-sh/watchdog.h. Overload this at module load 40*b7e04f8cSWim Van Sebroeck * time. 41*b7e04f8cSWim Van Sebroeck * 42*b7e04f8cSWim Van Sebroeck * In order for this to work reliably we need to have HZ set to 1000 or 43*b7e04f8cSWim Van Sebroeck * something quite higher than 100 (or we need a proper high-res timer 44*b7e04f8cSWim Van Sebroeck * implementation that will deal with this properly), otherwise the 10ms 45*b7e04f8cSWim Van Sebroeck * resolution of a jiffy is enough to trigger the overflow. For things like 46*b7e04f8cSWim Van Sebroeck * the SH-4 and SH-5, this isn't necessarily that big of a problem, though 47*b7e04f8cSWim Van Sebroeck * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely 48*b7e04f8cSWim Van Sebroeck * necssary. 49*b7e04f8cSWim Van Sebroeck * 50*b7e04f8cSWim Van Sebroeck * As a result of this timing problem, the only modes that are particularly 51*b7e04f8cSWim Van Sebroeck * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms 52*b7e04f8cSWim Van Sebroeck * overflow periods respectively. 53*b7e04f8cSWim Van Sebroeck * 54*b7e04f8cSWim Van Sebroeck * Also, since we can't really expect userspace to be responsive enough 55*b7e04f8cSWim Van Sebroeck * before the overflow happens, we maintain two seperate timers .. One in 56*b7e04f8cSWim Van Sebroeck * the kernel for clearing out WOVF every 2ms or so (again, this depends on 57*b7e04f8cSWim Van Sebroeck * HZ == 1000), and another for monitoring userspace writes to the WDT device. 58*b7e04f8cSWim Van Sebroeck * 59*b7e04f8cSWim Van Sebroeck * As such, we currently use a configurable heartbeat interval which defaults 60*b7e04f8cSWim Van Sebroeck * to 30s. In this case, the userspace daemon is only responsible for periodic 61*b7e04f8cSWim Van Sebroeck * writes to the device before the next heartbeat is scheduled. If the daemon 62*b7e04f8cSWim Van Sebroeck * misses its deadline, the kernel timer will allow the WDT to overflow. 63*b7e04f8cSWim Van Sebroeck */ 64*b7e04f8cSWim Van Sebroeck static int clock_division_ratio = WTCSR_CKS_4096; 65*b7e04f8cSWim Van Sebroeck 66*b7e04f8cSWim Van Sebroeck #define next_ping_period(cks) msecs_to_jiffies(cks - 4) 67*b7e04f8cSWim Van Sebroeck 68*b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data); 69*b7e04f8cSWim Van Sebroeck 70*b7e04f8cSWim Van Sebroeck static unsigned long shwdt_is_open; 71*b7e04f8cSWim Van Sebroeck static struct watchdog_info sh_wdt_info; 72*b7e04f8cSWim Van Sebroeck static char shwdt_expect_close; 73*b7e04f8cSWim Van Sebroeck static DEFINE_TIMER(timer, sh_wdt_ping, 0, 0); 74*b7e04f8cSWim Van Sebroeck static unsigned long next_heartbeat; 75*b7e04f8cSWim Van Sebroeck 76*b7e04f8cSWim Van Sebroeck #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ 77*b7e04f8cSWim Van Sebroeck static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ 78*b7e04f8cSWim Van Sebroeck 79*b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT; 80*b7e04f8cSWim Van Sebroeck 81*b7e04f8cSWim Van Sebroeck /** 82*b7e04f8cSWim Van Sebroeck * sh_wdt_start - Start the Watchdog 83*b7e04f8cSWim Van Sebroeck * 84*b7e04f8cSWim Van Sebroeck * Starts the watchdog. 85*b7e04f8cSWim Van Sebroeck */ 86*b7e04f8cSWim Van Sebroeck static void sh_wdt_start(void) 87*b7e04f8cSWim Van Sebroeck { 88*b7e04f8cSWim Van Sebroeck __u8 csr; 89*b7e04f8cSWim Van Sebroeck 90*b7e04f8cSWim Van Sebroeck next_heartbeat = jiffies + (heartbeat * HZ); 91*b7e04f8cSWim Van Sebroeck mod_timer(&timer, next_ping_period(clock_division_ratio)); 92*b7e04f8cSWim Van Sebroeck 93*b7e04f8cSWim Van Sebroeck csr = sh_wdt_read_csr(); 94*b7e04f8cSWim Van Sebroeck csr |= WTCSR_WT | clock_division_ratio; 95*b7e04f8cSWim Van Sebroeck sh_wdt_write_csr(csr); 96*b7e04f8cSWim Van Sebroeck 97*b7e04f8cSWim Van Sebroeck sh_wdt_write_cnt(0); 98*b7e04f8cSWim Van Sebroeck 99*b7e04f8cSWim Van Sebroeck /* 100*b7e04f8cSWim Van Sebroeck * These processors have a bit of an inconsistent initialization 101*b7e04f8cSWim Van Sebroeck * process.. starting with SH-3, RSTS was moved to WTCSR, and the 102*b7e04f8cSWim Van Sebroeck * RSTCSR register was removed. 103*b7e04f8cSWim Van Sebroeck * 104*b7e04f8cSWim Van Sebroeck * On the SH-2 however, in addition with bits being in different 105*b7e04f8cSWim Van Sebroeck * locations, we must deal with RSTCSR outright.. 106*b7e04f8cSWim Van Sebroeck */ 107*b7e04f8cSWim Van Sebroeck csr = sh_wdt_read_csr(); 108*b7e04f8cSWim Van Sebroeck csr |= WTCSR_TME; 109*b7e04f8cSWim Van Sebroeck csr &= ~WTCSR_RSTS; 110*b7e04f8cSWim Van Sebroeck sh_wdt_write_csr(csr); 111*b7e04f8cSWim Van Sebroeck 112*b7e04f8cSWim Van Sebroeck #ifdef CONFIG_CPU_SH2 113*b7e04f8cSWim Van Sebroeck /* 114*b7e04f8cSWim Van Sebroeck * Whoever came up with the RSTCSR semantics must've been smoking 115*b7e04f8cSWim Van Sebroeck * some of the good stuff, since in addition to the WTCSR/WTCNT write 116*b7e04f8cSWim Van Sebroeck * brain-damage, it's managed to fuck things up one step further.. 117*b7e04f8cSWim Van Sebroeck * 118*b7e04f8cSWim Van Sebroeck * If we need to clear the WOVF bit, the upper byte has to be 0xa5.. 119*b7e04f8cSWim Van Sebroeck * but if we want to touch RSTE or RSTS, the upper byte has to be 120*b7e04f8cSWim Van Sebroeck * 0x5a.. 121*b7e04f8cSWim Van Sebroeck */ 122*b7e04f8cSWim Van Sebroeck csr = sh_wdt_read_rstcsr(); 123*b7e04f8cSWim Van Sebroeck csr &= ~RSTCSR_RSTS; 124*b7e04f8cSWim Van Sebroeck sh_wdt_write_rstcsr(csr); 125*b7e04f8cSWim Van Sebroeck #endif 126*b7e04f8cSWim Van Sebroeck } 127*b7e04f8cSWim Van Sebroeck 128*b7e04f8cSWim Van Sebroeck /** 129*b7e04f8cSWim Van Sebroeck * sh_wdt_stop - Stop the Watchdog 130*b7e04f8cSWim Van Sebroeck * Stops the watchdog. 131*b7e04f8cSWim Van Sebroeck */ 132*b7e04f8cSWim Van Sebroeck static void sh_wdt_stop(void) 133*b7e04f8cSWim Van Sebroeck { 134*b7e04f8cSWim Van Sebroeck __u8 csr; 135*b7e04f8cSWim Van Sebroeck 136*b7e04f8cSWim Van Sebroeck del_timer(&timer); 137*b7e04f8cSWim Van Sebroeck 138*b7e04f8cSWim Van Sebroeck csr = sh_wdt_read_csr(); 139*b7e04f8cSWim Van Sebroeck csr &= ~WTCSR_TME; 140*b7e04f8cSWim Van Sebroeck sh_wdt_write_csr(csr); 141*b7e04f8cSWim Van Sebroeck } 142*b7e04f8cSWim Van Sebroeck 143*b7e04f8cSWim Van Sebroeck /** 144*b7e04f8cSWim Van Sebroeck * sh_wdt_keepalive - Keep the Userspace Watchdog Alive 145*b7e04f8cSWim Van Sebroeck * The Userspace watchdog got a KeepAlive: schedule the next heartbeat. 146*b7e04f8cSWim Van Sebroeck */ 147*b7e04f8cSWim Van Sebroeck static inline void sh_wdt_keepalive(void) 148*b7e04f8cSWim Van Sebroeck { 149*b7e04f8cSWim Van Sebroeck next_heartbeat = jiffies + (heartbeat * HZ); 150*b7e04f8cSWim Van Sebroeck } 151*b7e04f8cSWim Van Sebroeck 152*b7e04f8cSWim Van Sebroeck /** 153*b7e04f8cSWim Van Sebroeck * sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat 154*b7e04f8cSWim Van Sebroeck * Set the Userspace Watchdog heartbeat 155*b7e04f8cSWim Van Sebroeck */ 156*b7e04f8cSWim Van Sebroeck static int sh_wdt_set_heartbeat(int t) 157*b7e04f8cSWim Van Sebroeck { 158*b7e04f8cSWim Van Sebroeck if (unlikely((t < 1) || (t > 3600))) /* arbitrary upper limit */ 159*b7e04f8cSWim Van Sebroeck return -EINVAL; 160*b7e04f8cSWim Van Sebroeck 161*b7e04f8cSWim Van Sebroeck heartbeat = t; 162*b7e04f8cSWim Van Sebroeck return 0; 163*b7e04f8cSWim Van Sebroeck } 164*b7e04f8cSWim Van Sebroeck 165*b7e04f8cSWim Van Sebroeck /** 166*b7e04f8cSWim Van Sebroeck * sh_wdt_ping - Ping the Watchdog 167*b7e04f8cSWim Van Sebroeck * @data: Unused 168*b7e04f8cSWim Van Sebroeck * 169*b7e04f8cSWim Van Sebroeck * Clears overflow bit, resets timer counter. 170*b7e04f8cSWim Van Sebroeck */ 171*b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data) 172*b7e04f8cSWim Van Sebroeck { 173*b7e04f8cSWim Van Sebroeck if (time_before(jiffies, next_heartbeat)) { 174*b7e04f8cSWim Van Sebroeck __u8 csr; 175*b7e04f8cSWim Van Sebroeck 176*b7e04f8cSWim Van Sebroeck csr = sh_wdt_read_csr(); 177*b7e04f8cSWim Van Sebroeck csr &= ~WTCSR_IOVF; 178*b7e04f8cSWim Van Sebroeck sh_wdt_write_csr(csr); 179*b7e04f8cSWim Van Sebroeck 180*b7e04f8cSWim Van Sebroeck sh_wdt_write_cnt(0); 181*b7e04f8cSWim Van Sebroeck 182*b7e04f8cSWim Van Sebroeck mod_timer(&timer, next_ping_period(clock_division_ratio)); 183*b7e04f8cSWim Van Sebroeck } else 184*b7e04f8cSWim Van Sebroeck printk(KERN_WARNING PFX "Heartbeat lost! Will not ping " 185*b7e04f8cSWim Van Sebroeck "the watchdog\n"); 186*b7e04f8cSWim Van Sebroeck } 187*b7e04f8cSWim Van Sebroeck 188*b7e04f8cSWim Van Sebroeck /** 189*b7e04f8cSWim Van Sebroeck * sh_wdt_open - Open the Device 190*b7e04f8cSWim Van Sebroeck * @inode: inode of device 191*b7e04f8cSWim Van Sebroeck * @file: file handle of device 192*b7e04f8cSWim Van Sebroeck * 193*b7e04f8cSWim Van Sebroeck * Watchdog device is opened and started. 194*b7e04f8cSWim Van Sebroeck */ 195*b7e04f8cSWim Van Sebroeck static int sh_wdt_open(struct inode *inode, struct file *file) 196*b7e04f8cSWim Van Sebroeck { 197*b7e04f8cSWim Van Sebroeck if (test_and_set_bit(0, &shwdt_is_open)) 198*b7e04f8cSWim Van Sebroeck return -EBUSY; 199*b7e04f8cSWim Van Sebroeck if (nowayout) 200*b7e04f8cSWim Van Sebroeck __module_get(THIS_MODULE); 201*b7e04f8cSWim Van Sebroeck 202*b7e04f8cSWim Van Sebroeck sh_wdt_start(); 203*b7e04f8cSWim Van Sebroeck 204*b7e04f8cSWim Van Sebroeck return nonseekable_open(inode, file); 205*b7e04f8cSWim Van Sebroeck } 206*b7e04f8cSWim Van Sebroeck 207*b7e04f8cSWim Van Sebroeck /** 208*b7e04f8cSWim Van Sebroeck * sh_wdt_close - Close the Device 209*b7e04f8cSWim Van Sebroeck * @inode: inode of device 210*b7e04f8cSWim Van Sebroeck * @file: file handle of device 211*b7e04f8cSWim Van Sebroeck * 212*b7e04f8cSWim Van Sebroeck * Watchdog device is closed and stopped. 213*b7e04f8cSWim Van Sebroeck */ 214*b7e04f8cSWim Van Sebroeck static int sh_wdt_close(struct inode *inode, struct file *file) 215*b7e04f8cSWim Van Sebroeck { 216*b7e04f8cSWim Van Sebroeck if (shwdt_expect_close == 42) { 217*b7e04f8cSWim Van Sebroeck sh_wdt_stop(); 218*b7e04f8cSWim Van Sebroeck } else { 219*b7e04f8cSWim Van Sebroeck printk(KERN_CRIT PFX "Unexpected close, not " 220*b7e04f8cSWim Van Sebroeck "stopping watchdog!\n"); 221*b7e04f8cSWim Van Sebroeck sh_wdt_keepalive(); 222*b7e04f8cSWim Van Sebroeck } 223*b7e04f8cSWim Van Sebroeck 224*b7e04f8cSWim Van Sebroeck clear_bit(0, &shwdt_is_open); 225*b7e04f8cSWim Van Sebroeck shwdt_expect_close = 0; 226*b7e04f8cSWim Van Sebroeck 227*b7e04f8cSWim Van Sebroeck return 0; 228*b7e04f8cSWim Van Sebroeck } 229*b7e04f8cSWim Van Sebroeck 230*b7e04f8cSWim Van Sebroeck /** 231*b7e04f8cSWim Van Sebroeck * sh_wdt_write - Write to Device 232*b7e04f8cSWim Van Sebroeck * @file: file handle of device 233*b7e04f8cSWim Van Sebroeck * @buf: buffer to write 234*b7e04f8cSWim Van Sebroeck * @count: length of buffer 235*b7e04f8cSWim Van Sebroeck * @ppos: offset 236*b7e04f8cSWim Van Sebroeck * 237*b7e04f8cSWim Van Sebroeck * Pings the watchdog on write. 238*b7e04f8cSWim Van Sebroeck */ 239*b7e04f8cSWim Van Sebroeck static ssize_t sh_wdt_write(struct file *file, const char *buf, 240*b7e04f8cSWim Van Sebroeck size_t count, loff_t *ppos) 241*b7e04f8cSWim Van Sebroeck { 242*b7e04f8cSWim Van Sebroeck if (count) { 243*b7e04f8cSWim Van Sebroeck if (!nowayout) { 244*b7e04f8cSWim Van Sebroeck size_t i; 245*b7e04f8cSWim Van Sebroeck 246*b7e04f8cSWim Van Sebroeck shwdt_expect_close = 0; 247*b7e04f8cSWim Van Sebroeck 248*b7e04f8cSWim Van Sebroeck for (i = 0; i != count; i++) { 249*b7e04f8cSWim Van Sebroeck char c; 250*b7e04f8cSWim Van Sebroeck if (get_user(c, buf + i)) 251*b7e04f8cSWim Van Sebroeck return -EFAULT; 252*b7e04f8cSWim Van Sebroeck if (c == 'V') 253*b7e04f8cSWim Van Sebroeck shwdt_expect_close = 42; 254*b7e04f8cSWim Van Sebroeck } 255*b7e04f8cSWim Van Sebroeck } 256*b7e04f8cSWim Van Sebroeck sh_wdt_keepalive(); 257*b7e04f8cSWim Van Sebroeck } 258*b7e04f8cSWim Van Sebroeck 259*b7e04f8cSWim Van Sebroeck return count; 260*b7e04f8cSWim Van Sebroeck } 261*b7e04f8cSWim Van Sebroeck 262*b7e04f8cSWim Van Sebroeck /** 263*b7e04f8cSWim Van Sebroeck * sh_wdt_mmap - map WDT/CPG registers into userspace 264*b7e04f8cSWim Van Sebroeck * @file: file structure for the device 265*b7e04f8cSWim Van Sebroeck * @vma: VMA to map the registers into 266*b7e04f8cSWim Van Sebroeck * 267*b7e04f8cSWim Van Sebroeck * A simple mmap() implementation for the corner cases where the counter 268*b7e04f8cSWim Van Sebroeck * needs to be mapped in userspace directly. Due to the relatively small 269*b7e04f8cSWim Van Sebroeck * size of the area, neighbouring registers not necessarily tied to the 270*b7e04f8cSWim Van Sebroeck * CPG will also be accessible through the register page, so this remains 271*b7e04f8cSWim Van Sebroeck * configurable for users that really know what they're doing. 272*b7e04f8cSWim Van Sebroeck * 273*b7e04f8cSWim Van Sebroeck * Additionaly, the register page maps in the CPG register base relative 274*b7e04f8cSWim Van Sebroeck * to the nearest page-aligned boundary, which requires that userspace do 275*b7e04f8cSWim Van Sebroeck * the appropriate CPU subtype math for calculating the page offset for 276*b7e04f8cSWim Van Sebroeck * the counter value. 277*b7e04f8cSWim Van Sebroeck */ 278*b7e04f8cSWim Van Sebroeck static int sh_wdt_mmap(struct file *file, struct vm_area_struct *vma) 279*b7e04f8cSWim Van Sebroeck { 280*b7e04f8cSWim Van Sebroeck int ret = -ENOSYS; 281*b7e04f8cSWim Van Sebroeck 282*b7e04f8cSWim Van Sebroeck #ifdef CONFIG_SH_WDT_MMAP 283*b7e04f8cSWim Van Sebroeck unsigned long addr; 284*b7e04f8cSWim Van Sebroeck 285*b7e04f8cSWim Van Sebroeck /* Only support the simple cases where we map in a register page. */ 286*b7e04f8cSWim Van Sebroeck if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff) 287*b7e04f8cSWim Van Sebroeck return -EINVAL; 288*b7e04f8cSWim Van Sebroeck 289*b7e04f8cSWim Van Sebroeck /* 290*b7e04f8cSWim Van Sebroeck * Pick WTCNT as the start, it's usually the first register after the 291*b7e04f8cSWim Van Sebroeck * FRQCR, and neither one are generally page-aligned out of the box. 292*b7e04f8cSWim Van Sebroeck */ 293*b7e04f8cSWim Van Sebroeck addr = WTCNT & ~(PAGE_SIZE - 1); 294*b7e04f8cSWim Van Sebroeck 295*b7e04f8cSWim Van Sebroeck vma->vm_flags |= VM_IO; 296*b7e04f8cSWim Van Sebroeck vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 297*b7e04f8cSWim Van Sebroeck 298*b7e04f8cSWim Van Sebroeck if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT, 299*b7e04f8cSWim Van Sebroeck PAGE_SIZE, vma->vm_page_prot)) { 300*b7e04f8cSWim Van Sebroeck printk(KERN_ERR PFX "%s: io_remap_pfn_range failed\n", 301*b7e04f8cSWim Van Sebroeck __FUNCTION__); 302*b7e04f8cSWim Van Sebroeck return -EAGAIN; 303*b7e04f8cSWim Van Sebroeck } 304*b7e04f8cSWim Van Sebroeck 305*b7e04f8cSWim Van Sebroeck ret = 0; 306*b7e04f8cSWim Van Sebroeck #endif 307*b7e04f8cSWim Van Sebroeck 308*b7e04f8cSWim Van Sebroeck return ret; 309*b7e04f8cSWim Van Sebroeck } 310*b7e04f8cSWim Van Sebroeck 311*b7e04f8cSWim Van Sebroeck /** 312*b7e04f8cSWim Van Sebroeck * sh_wdt_ioctl - Query Device 313*b7e04f8cSWim Van Sebroeck * @inode: inode of device 314*b7e04f8cSWim Van Sebroeck * @file: file handle of device 315*b7e04f8cSWim Van Sebroeck * @cmd: watchdog command 316*b7e04f8cSWim Van Sebroeck * @arg: argument 317*b7e04f8cSWim Van Sebroeck * 318*b7e04f8cSWim Van Sebroeck * Query basic information from the device or ping it, as outlined by the 319*b7e04f8cSWim Van Sebroeck * watchdog API. 320*b7e04f8cSWim Van Sebroeck */ 321*b7e04f8cSWim Van Sebroeck static int sh_wdt_ioctl(struct inode *inode, struct file *file, 322*b7e04f8cSWim Van Sebroeck unsigned int cmd, unsigned long arg) 323*b7e04f8cSWim Van Sebroeck { 324*b7e04f8cSWim Van Sebroeck int new_heartbeat; 325*b7e04f8cSWim Van Sebroeck int options, retval = -EINVAL; 326*b7e04f8cSWim Van Sebroeck 327*b7e04f8cSWim Van Sebroeck switch (cmd) { 328*b7e04f8cSWim Van Sebroeck case WDIOC_GETSUPPORT: 329*b7e04f8cSWim Van Sebroeck return copy_to_user((struct watchdog_info *)arg, 330*b7e04f8cSWim Van Sebroeck &sh_wdt_info, 331*b7e04f8cSWim Van Sebroeck sizeof(sh_wdt_info)) ? -EFAULT : 0; 332*b7e04f8cSWim Van Sebroeck case WDIOC_GETSTATUS: 333*b7e04f8cSWim Van Sebroeck case WDIOC_GETBOOTSTATUS: 334*b7e04f8cSWim Van Sebroeck return put_user(0, (int *)arg); 335*b7e04f8cSWim Van Sebroeck case WDIOC_KEEPALIVE: 336*b7e04f8cSWim Van Sebroeck sh_wdt_keepalive(); 337*b7e04f8cSWim Van Sebroeck return 0; 338*b7e04f8cSWim Van Sebroeck case WDIOC_SETTIMEOUT: 339*b7e04f8cSWim Van Sebroeck if (get_user(new_heartbeat, (int *)arg)) 340*b7e04f8cSWim Van Sebroeck return -EFAULT; 341*b7e04f8cSWim Van Sebroeck 342*b7e04f8cSWim Van Sebroeck if (sh_wdt_set_heartbeat(new_heartbeat)) 343*b7e04f8cSWim Van Sebroeck return -EINVAL; 344*b7e04f8cSWim Van Sebroeck 345*b7e04f8cSWim Van Sebroeck sh_wdt_keepalive(); 346*b7e04f8cSWim Van Sebroeck /* Fall */ 347*b7e04f8cSWim Van Sebroeck case WDIOC_GETTIMEOUT: 348*b7e04f8cSWim Van Sebroeck return put_user(heartbeat, (int *)arg); 349*b7e04f8cSWim Van Sebroeck case WDIOC_SETOPTIONS: 350*b7e04f8cSWim Van Sebroeck if (get_user(options, (int *)arg)) 351*b7e04f8cSWim Van Sebroeck return -EFAULT; 352*b7e04f8cSWim Van Sebroeck 353*b7e04f8cSWim Van Sebroeck if (options & WDIOS_DISABLECARD) { 354*b7e04f8cSWim Van Sebroeck sh_wdt_stop(); 355*b7e04f8cSWim Van Sebroeck retval = 0; 356*b7e04f8cSWim Van Sebroeck } 357*b7e04f8cSWim Van Sebroeck 358*b7e04f8cSWim Van Sebroeck if (options & WDIOS_ENABLECARD) { 359*b7e04f8cSWim Van Sebroeck sh_wdt_start(); 360*b7e04f8cSWim Van Sebroeck retval = 0; 361*b7e04f8cSWim Van Sebroeck } 362*b7e04f8cSWim Van Sebroeck 363*b7e04f8cSWim Van Sebroeck return retval; 364*b7e04f8cSWim Van Sebroeck default: 365*b7e04f8cSWim Van Sebroeck return -ENOTTY; 366*b7e04f8cSWim Van Sebroeck } 367*b7e04f8cSWim Van Sebroeck 368*b7e04f8cSWim Van Sebroeck return 0; 369*b7e04f8cSWim Van Sebroeck } 370*b7e04f8cSWim Van Sebroeck 371*b7e04f8cSWim Van Sebroeck /** 372*b7e04f8cSWim Van Sebroeck * sh_wdt_notify_sys - Notifier Handler 373*b7e04f8cSWim Van Sebroeck * @this: notifier block 374*b7e04f8cSWim Van Sebroeck * @code: notifier event 375*b7e04f8cSWim Van Sebroeck * @unused: unused 376*b7e04f8cSWim Van Sebroeck * 377*b7e04f8cSWim Van Sebroeck * Handles specific events, such as turning off the watchdog during a 378*b7e04f8cSWim Van Sebroeck * shutdown event. 379*b7e04f8cSWim Van Sebroeck */ 380*b7e04f8cSWim Van Sebroeck static int sh_wdt_notify_sys(struct notifier_block *this, 381*b7e04f8cSWim Van Sebroeck unsigned long code, void *unused) 382*b7e04f8cSWim Van Sebroeck { 383*b7e04f8cSWim Van Sebroeck if (code == SYS_DOWN || code == SYS_HALT) 384*b7e04f8cSWim Van Sebroeck sh_wdt_stop(); 385*b7e04f8cSWim Van Sebroeck 386*b7e04f8cSWim Van Sebroeck return NOTIFY_DONE; 387*b7e04f8cSWim Van Sebroeck } 388*b7e04f8cSWim Van Sebroeck 389*b7e04f8cSWim Van Sebroeck static const struct file_operations sh_wdt_fops = { 390*b7e04f8cSWim Van Sebroeck .owner = THIS_MODULE, 391*b7e04f8cSWim Van Sebroeck .llseek = no_llseek, 392*b7e04f8cSWim Van Sebroeck .write = sh_wdt_write, 393*b7e04f8cSWim Van Sebroeck .ioctl = sh_wdt_ioctl, 394*b7e04f8cSWim Van Sebroeck .open = sh_wdt_open, 395*b7e04f8cSWim Van Sebroeck .release = sh_wdt_close, 396*b7e04f8cSWim Van Sebroeck .mmap = sh_wdt_mmap, 397*b7e04f8cSWim Van Sebroeck }; 398*b7e04f8cSWim Van Sebroeck 399*b7e04f8cSWim Van Sebroeck static struct watchdog_info sh_wdt_info = { 400*b7e04f8cSWim Van Sebroeck .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | 401*b7e04f8cSWim Van Sebroeck WDIOF_MAGICCLOSE, 402*b7e04f8cSWim Van Sebroeck .firmware_version = 1, 403*b7e04f8cSWim Van Sebroeck .identity = "SH WDT", 404*b7e04f8cSWim Van Sebroeck }; 405*b7e04f8cSWim Van Sebroeck 406*b7e04f8cSWim Van Sebroeck static struct notifier_block sh_wdt_notifier = { 407*b7e04f8cSWim Van Sebroeck .notifier_call = sh_wdt_notify_sys, 408*b7e04f8cSWim Van Sebroeck }; 409*b7e04f8cSWim Van Sebroeck 410*b7e04f8cSWim Van Sebroeck static struct miscdevice sh_wdt_miscdev = { 411*b7e04f8cSWim Van Sebroeck .minor = WATCHDOG_MINOR, 412*b7e04f8cSWim Van Sebroeck .name = "watchdog", 413*b7e04f8cSWim Van Sebroeck .fops = &sh_wdt_fops, 414*b7e04f8cSWim Van Sebroeck }; 415*b7e04f8cSWim Van Sebroeck 416*b7e04f8cSWim Van Sebroeck /** 417*b7e04f8cSWim Van Sebroeck * sh_wdt_init - Initialize module 418*b7e04f8cSWim Van Sebroeck * Registers the device and notifier handler. Actual device 419*b7e04f8cSWim Van Sebroeck * initialization is handled by sh_wdt_open(). 420*b7e04f8cSWim Van Sebroeck */ 421*b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void) 422*b7e04f8cSWim Van Sebroeck { 423*b7e04f8cSWim Van Sebroeck int rc; 424*b7e04f8cSWim Van Sebroeck 425*b7e04f8cSWim Van Sebroeck if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) { 426*b7e04f8cSWim Van Sebroeck clock_division_ratio = WTCSR_CKS_4096; 427*b7e04f8cSWim Van Sebroeck printk(KERN_INFO PFX "clock_division_ratio value must " 428*b7e04f8cSWim Van Sebroeck "be 0x5<=x<=0x7, using %d\n", clock_division_ratio); 429*b7e04f8cSWim Van Sebroeck } 430*b7e04f8cSWim Van Sebroeck 431*b7e04f8cSWim Van Sebroeck rc = sh_wdt_set_heartbeat(heartbeat); 432*b7e04f8cSWim Van Sebroeck if (unlikely(rc)) { 433*b7e04f8cSWim Van Sebroeck heartbeat = WATCHDOG_HEARTBEAT; 434*b7e04f8cSWim Van Sebroeck printk(KERN_INFO PFX "heartbeat value must " 435*b7e04f8cSWim Van Sebroeck "be 1<=x<=3600, using %d\n", heartbeat); 436*b7e04f8cSWim Van Sebroeck } 437*b7e04f8cSWim Van Sebroeck 438*b7e04f8cSWim Van Sebroeck rc = register_reboot_notifier(&sh_wdt_notifier); 439*b7e04f8cSWim Van Sebroeck if (unlikely(rc)) { 440*b7e04f8cSWim Van Sebroeck printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n", 441*b7e04f8cSWim Van Sebroeck rc); 442*b7e04f8cSWim Van Sebroeck return rc; 443*b7e04f8cSWim Van Sebroeck } 444*b7e04f8cSWim Van Sebroeck 445*b7e04f8cSWim Van Sebroeck rc = misc_register(&sh_wdt_miscdev); 446*b7e04f8cSWim Van Sebroeck if (unlikely(rc)) { 447*b7e04f8cSWim Van Sebroeck printk(KERN_ERR PFX "Can't register miscdev on " 448*b7e04f8cSWim Van Sebroeck "minor=%d (err=%d)\n", sh_wdt_miscdev.minor, rc); 449*b7e04f8cSWim Van Sebroeck unregister_reboot_notifier(&sh_wdt_notifier); 450*b7e04f8cSWim Van Sebroeck return rc; 451*b7e04f8cSWim Van Sebroeck } 452*b7e04f8cSWim Van Sebroeck 453*b7e04f8cSWim Van Sebroeck printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", 454*b7e04f8cSWim Van Sebroeck heartbeat, nowayout); 455*b7e04f8cSWim Van Sebroeck 456*b7e04f8cSWim Van Sebroeck return 0; 457*b7e04f8cSWim Van Sebroeck } 458*b7e04f8cSWim Van Sebroeck 459*b7e04f8cSWim Van Sebroeck /** 460*b7e04f8cSWim Van Sebroeck * sh_wdt_exit - Deinitialize module 461*b7e04f8cSWim Van Sebroeck * Unregisters the device and notifier handler. Actual device 462*b7e04f8cSWim Van Sebroeck * deinitialization is handled by sh_wdt_close(). 463*b7e04f8cSWim Van Sebroeck */ 464*b7e04f8cSWim Van Sebroeck static void __exit sh_wdt_exit(void) 465*b7e04f8cSWim Van Sebroeck { 466*b7e04f8cSWim Van Sebroeck misc_deregister(&sh_wdt_miscdev); 467*b7e04f8cSWim Van Sebroeck unregister_reboot_notifier(&sh_wdt_notifier); 468*b7e04f8cSWim Van Sebroeck } 469*b7e04f8cSWim Van Sebroeck 470*b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); 471*b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("SuperH watchdog driver"); 472*b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL"); 473*b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 474*b7e04f8cSWim Van Sebroeck 475*b7e04f8cSWim Van Sebroeck module_param(clock_division_ratio, int, 0); 476*b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")"); 477*b7e04f8cSWim Van Sebroeck 478*b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0); 479*b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); 480*b7e04f8cSWim Van Sebroeck 481*b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0); 482*b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 483*b7e04f8cSWim Van Sebroeck 484*b7e04f8cSWim Van Sebroeck module_init(sh_wdt_init); 485*b7e04f8cSWim Van Sebroeck module_exit(sh_wdt_exit); 486