Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel 21285 watchdog driver |
| 3 | * Copyright (c) Phil Blundell <pb@nexus.co.uk>, 1998 |
| 4 | * |
| 5 | * based on |
| 6 | * |
| 7 | * SoftDog 0.05: A Software Watchdog Device |
| 8 | * |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 9 | * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
| 10 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | */ |
| 18 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/watchdog.h> |
| 29 | #include <linux/reboot.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/interrupt.h> |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 32 | #include <linux/uaccess.h> |
| 33 | #include <linux/irq.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 34 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/mach-types.h> |
David Howells | 9f97da7 | 2012-03-28 18:30:01 +0100 | [diff] [blame] | 37 | #include <asm/system_info.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/hardware/dec21285.h> |
| 39 | |
| 40 | /* |
| 41 | * Define this to stop the watchdog actually rebooting the machine. |
| 42 | */ |
| 43 | #undef ONLY_TESTING |
| 44 | |
| 45 | static unsigned int soft_margin = 60; /* in seconds */ |
| 46 | static unsigned int reload; |
| 47 | static unsigned long timer_alive; |
| 48 | |
| 49 | #ifdef ONLY_TESTING |
| 50 | /* |
| 51 | * If the timer expires.. |
| 52 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 53 | static void watchdog_fire(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 55 | pr_crit("Would Reboot\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | *CSR_TIMER4_CNTL = 0; |
| 57 | *CSR_TIMER4_CLR = 0; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | * Refresh the timer. |
| 63 | */ |
| 64 | static void watchdog_ping(void) |
| 65 | { |
| 66 | *CSR_TIMER4_LOAD = reload; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Allow only one person to hold it open |
| 71 | */ |
| 72 | static int watchdog_open(struct inode *inode, struct file *file) |
| 73 | { |
| 74 | int ret; |
| 75 | |
| 76 | if (*CSR_SA110_CNTL & (1 << 13)) |
| 77 | return -EBUSY; |
| 78 | |
| 79 | if (test_and_set_bit(1, &timer_alive)) |
| 80 | return -EBUSY; |
| 81 | |
| 82 | reload = soft_margin * (mem_fclk_21285 / 256); |
| 83 | |
| 84 | *CSR_TIMER4_CLR = 0; |
| 85 | watchdog_ping(); |
| 86 | *CSR_TIMER4_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD |
| 87 | | TIMER_CNTL_DIV256; |
| 88 | |
| 89 | #ifdef ONLY_TESTING |
| 90 | ret = request_irq(IRQ_TIMER4, watchdog_fire, 0, "watchdog", NULL); |
| 91 | if (ret) { |
| 92 | *CSR_TIMER4_CNTL = 0; |
| 93 | clear_bit(1, &timer_alive); |
| 94 | } |
| 95 | #else |
| 96 | /* |
| 97 | * Setting this bit is irreversible; once enabled, there is |
| 98 | * no way to disable the watchdog. |
| 99 | */ |
| 100 | *CSR_SA110_CNTL |= 1 << 13; |
| 101 | |
| 102 | ret = 0; |
| 103 | #endif |
| 104 | nonseekable_open(inode, file); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Shut off the timer. |
| 110 | * Note: if we really have enabled the watchdog, there |
| 111 | * is no way to turn off. |
| 112 | */ |
| 113 | static int watchdog_release(struct inode *inode, struct file *file) |
| 114 | { |
| 115 | #ifdef ONLY_TESTING |
| 116 | free_irq(IRQ_TIMER4, NULL); |
| 117 | clear_bit(1, &timer_alive); |
| 118 | #endif |
| 119 | return 0; |
| 120 | } |
| 121 | |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 122 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 123 | size_t len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | /* |
| 126 | * Refresh the timer. |
| 127 | */ |
| 128 | if (len) |
| 129 | watchdog_ping(); |
| 130 | |
| 131 | return len; |
| 132 | } |
| 133 | |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 134 | static const struct watchdog_info ident = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .options = WDIOF_SETTIMEOUT, |
| 136 | .identity = "Footbridge Watchdog", |
| 137 | }; |
| 138 | |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 139 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 140 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 142 | int __user *int_arg = (int __user *)arg; |
Alexander Shiyan | 70605d9 | 2014-02-15 13:23:25 +0400 | [diff] [blame^] | 143 | int new_margin, ret = -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 145 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | case WDIOC_GETSUPPORT: |
| 147 | ret = 0; |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 148 | if (copy_to_user((void __user *)arg, &ident, sizeof(ident))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | ret = -EFAULT; |
| 150 | break; |
| 151 | |
| 152 | case WDIOC_GETSTATUS: |
| 153 | case WDIOC_GETBOOTSTATUS: |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 154 | ret = put_user(0, int_arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | case WDIOC_KEEPALIVE: |
| 158 | watchdog_ping(); |
| 159 | ret = 0; |
| 160 | break; |
| 161 | |
| 162 | case WDIOC_SETTIMEOUT: |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 163 | ret = get_user(new_margin, int_arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (ret) |
| 165 | break; |
| 166 | |
| 167 | /* Arbitrary, can't find the card's limits */ |
| 168 | if (new_margin < 0 || new_margin > 60) { |
| 169 | ret = -EINVAL; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | soft_margin = new_margin; |
| 174 | reload = soft_margin * (mem_fclk_21285 / 256); |
| 175 | watchdog_ping(); |
| 176 | /* Fall */ |
| 177 | case WDIOC_GETTIMEOUT: |
Ben Dooks | edf86c9 | 2008-09-16 11:31:01 +0100 | [diff] [blame] | 178 | ret = put_user(soft_margin, int_arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | return ret; |
| 182 | } |
| 183 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 184 | static const struct file_operations watchdog_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | .owner = THIS_MODULE, |
| 186 | .llseek = no_llseek, |
| 187 | .write = watchdog_write, |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 188 | .unlocked_ioctl = watchdog_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .open = watchdog_open, |
| 190 | .release = watchdog_release, |
| 191 | }; |
| 192 | |
| 193 | static struct miscdevice watchdog_miscdev = { |
| 194 | .minor = WATCHDOG_MINOR, |
| 195 | .name = "watchdog", |
| 196 | .fops = &watchdog_fops, |
| 197 | }; |
| 198 | |
| 199 | static int __init footbridge_watchdog_init(void) |
| 200 | { |
| 201 | int retval; |
| 202 | |
| 203 | if (machine_is_netwinder()) |
| 204 | return -ENODEV; |
| 205 | |
| 206 | retval = misc_register(&watchdog_miscdev); |
| 207 | if (retval < 0) |
| 208 | return retval; |
| 209 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 210 | pr_info("Footbridge Watchdog Timer: 0.01, timer margin: %d sec\n", |
| 211 | soft_margin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | if (machine_is_cats()) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 214 | pr_warn("Warning: Watchdog reset may not work on this machine\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static void __exit footbridge_watchdog_exit(void) |
| 219 | { |
| 220 | misc_deregister(&watchdog_miscdev); |
| 221 | } |
| 222 | |
| 223 | MODULE_AUTHOR("Phil Blundell <pb@nexus.co.uk>"); |
| 224 | MODULE_DESCRIPTION("Footbridge watchdog driver"); |
| 225 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | module_param(soft_margin, int, 0); |
Alan Cox | d0e58ee | 2008-05-19 14:09:51 +0100 | [diff] [blame] | 228 | MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | module_init(footbridge_watchdog_init); |
| 231 | module_exit(footbridge_watchdog_exit); |