Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for Kendin/Micrel KS8695. |
| 3 | * |
| 4 | * (C) 2007 Andrew Victor |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/watchdog.h> |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 22 | #include <linux/io.h> |
| 23 | #include <linux/uaccess.h> |
Alexey Dobriyan | b02c387 | 2009-02-12 13:42:41 +0300 | [diff] [blame] | 24 | #include <mach/timex.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/regs-timer.h> |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 26 | |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 27 | #define WDT_DEFAULT_TIME 5 /* seconds */ |
| 28 | #define WDT_MAX_TIME 171 /* seconds */ |
| 29 | |
| 30 | static int wdt_time = WDT_DEFAULT_TIME; |
| 31 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 32 | |
| 33 | module_param(wdt_time, int, 0); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 34 | MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default=" |
| 35 | __MODULE_STRING(WDT_DEFAULT_TIME) ")"); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 36 | |
| 37 | #ifdef CONFIG_WATCHDOG_NOWAYOUT |
| 38 | module_param(nowayout, int, 0); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 39 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 40 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | |
| 44 | static unsigned long ks8695wdt_busy; |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 45 | static spinlock_t ks8695_lock; |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 46 | |
| 47 | /* ......................................................................... */ |
| 48 | |
| 49 | /* |
| 50 | * Disable the watchdog. |
| 51 | */ |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 52 | static inline void ks8695_wdt_stop(void) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 53 | { |
| 54 | unsigned long tmcon; |
| 55 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 56 | spin_lock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 57 | /* disable timer0 */ |
| 58 | tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON); |
| 59 | __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 60 | spin_unlock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Enable and reset the watchdog. |
| 65 | */ |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 66 | static inline void ks8695_wdt_start(void) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 67 | { |
| 68 | unsigned long tmcon; |
| 69 | unsigned long tval = wdt_time * CLOCK_TICK_RATE; |
| 70 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 71 | spin_lock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 72 | /* disable timer0 */ |
| 73 | tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON); |
| 74 | __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON); |
| 75 | |
| 76 | /* program timer0 */ |
| 77 | __raw_writel(tval | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC); |
| 78 | |
| 79 | /* re-enable timer0 */ |
| 80 | tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON); |
| 81 | __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 82 | spin_unlock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Reload the watchdog timer. (ie, pat the watchdog) |
| 87 | */ |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 88 | static inline void ks8695_wdt_reload(void) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 89 | { |
| 90 | unsigned long tmcon; |
| 91 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 92 | spin_lock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 93 | /* disable, then re-enable timer0 */ |
| 94 | tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON); |
| 95 | __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON); |
| 96 | __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 97 | spin_unlock(&ks8695_lock); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Change the watchdog time interval. |
| 102 | */ |
| 103 | static int ks8695_wdt_settimeout(int new_time) |
| 104 | { |
| 105 | /* |
| 106 | * All counting occurs at SLOW_CLOCK / 128 = 0.256 Hz |
| 107 | * |
| 108 | * Since WDV is a 16-bit counter, the maximum period is |
| 109 | * 65536 / 0.256 = 256 seconds. |
| 110 | */ |
| 111 | if ((new_time <= 0) || (new_time > WDT_MAX_TIME)) |
| 112 | return -EINVAL; |
| 113 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 114 | /* Set new watchdog time. It will be used when |
| 115 | ks8695_wdt_start() is called. */ |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 116 | wdt_time = new_time; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* ......................................................................... */ |
| 121 | |
| 122 | /* |
| 123 | * Watchdog device is opened, and watchdog starts running. |
| 124 | */ |
| 125 | static int ks8695_wdt_open(struct inode *inode, struct file *file) |
| 126 | { |
| 127 | if (test_and_set_bit(0, &ks8695wdt_busy)) |
| 128 | return -EBUSY; |
| 129 | |
| 130 | ks8695_wdt_start(); |
| 131 | return nonseekable_open(inode, file); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Close the watchdog device. |
| 136 | * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also |
| 137 | * disabled. |
| 138 | */ |
| 139 | static int ks8695_wdt_close(struct inode *inode, struct file *file) |
| 140 | { |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 141 | /* Disable the watchdog when file is closed */ |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 142 | if (!nowayout) |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 143 | ks8695_wdt_stop(); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 144 | clear_bit(0, &ks8695wdt_busy); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static struct watchdog_info ks8695_wdt_info = { |
| 149 | .identity = "ks8695 watchdog", |
| 150 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 151 | }; |
| 152 | |
| 153 | /* |
| 154 | * Handle commands from user-space. |
| 155 | */ |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 156 | static long ks8695_wdt_ioctl(struct file *file, unsigned int cmd, |
| 157 | unsigned long arg) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 158 | { |
| 159 | void __user *argp = (void __user *)arg; |
| 160 | int __user *p = argp; |
| 161 | int new_value; |
| 162 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 163 | switch (cmd) { |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 164 | case WDIOC_GETSUPPORT: |
| 165 | return copy_to_user(argp, &ks8695_wdt_info, |
| 166 | sizeof(ks8695_wdt_info)) ? -EFAULT : 0; |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 167 | case WDIOC_GETSTATUS: |
| 168 | case WDIOC_GETBOOTSTATUS: |
| 169 | return put_user(0, p); |
| 170 | case WDIOC_SETOPTIONS: |
| 171 | if (get_user(new_value, p)) |
| 172 | return -EFAULT; |
| 173 | if (new_value & WDIOS_DISABLECARD) |
| 174 | ks8695_wdt_stop(); |
| 175 | if (new_value & WDIOS_ENABLECARD) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 176 | ks8695_wdt_start(); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 177 | return 0; |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 178 | case WDIOC_KEEPALIVE: |
| 179 | ks8695_wdt_reload(); /* pat the watchdog */ |
| 180 | return 0; |
| 181 | case WDIOC_SETTIMEOUT: |
| 182 | if (get_user(new_value, p)) |
| 183 | return -EFAULT; |
| 184 | if (ks8695_wdt_settimeout(new_value)) |
| 185 | return -EINVAL; |
| 186 | /* Enable new time value */ |
| 187 | ks8695_wdt_start(); |
| 188 | /* Return current value */ |
| 189 | return put_user(wdt_time, p); |
| 190 | case WDIOC_GETTIMEOUT: |
| 191 | return put_user(wdt_time, p); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 192 | default: |
| 193 | return -ENOTTY; |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Pat the watchdog whenever device is written to. |
| 199 | */ |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 200 | static ssize_t ks8695_wdt_write(struct file *file, const char *data, |
| 201 | size_t len, loff_t *ppos) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 202 | { |
| 203 | ks8695_wdt_reload(); /* pat the watchdog */ |
| 204 | return len; |
| 205 | } |
| 206 | |
| 207 | /* ......................................................................... */ |
| 208 | |
| 209 | static const struct file_operations ks8695wdt_fops = { |
| 210 | .owner = THIS_MODULE, |
| 211 | .llseek = no_llseek, |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 212 | .unlocked_ioctl = ks8695_wdt_ioctl, |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 213 | .open = ks8695_wdt_open, |
| 214 | .release = ks8695_wdt_close, |
| 215 | .write = ks8695_wdt_write, |
| 216 | }; |
| 217 | |
| 218 | static struct miscdevice ks8695wdt_miscdev = { |
| 219 | .minor = WATCHDOG_MINOR, |
| 220 | .name = "watchdog", |
| 221 | .fops = &ks8695wdt_fops, |
| 222 | }; |
| 223 | |
Uwe Kleine-König | c98d58e | 2009-03-28 00:26:45 +0100 | [diff] [blame] | 224 | static int __devinit ks8695wdt_probe(struct platform_device *pdev) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 225 | { |
| 226 | int res; |
| 227 | |
| 228 | if (ks8695wdt_miscdev.parent) |
| 229 | return -EBUSY; |
| 230 | ks8695wdt_miscdev.parent = &pdev->dev; |
| 231 | |
| 232 | res = misc_register(&ks8695wdt_miscdev); |
| 233 | if (res) |
| 234 | return res; |
| 235 | |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 236 | printk(KERN_INFO "KS8695 Watchdog Timer enabled (%d seconds%s)\n", |
| 237 | wdt_time, nowayout ? ", nowayout" : ""); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Uwe Kleine-König | c98d58e | 2009-03-28 00:26:45 +0100 | [diff] [blame] | 241 | static int __devexit ks8695wdt_remove(struct platform_device *pdev) |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 242 | { |
| 243 | int res; |
| 244 | |
| 245 | res = misc_deregister(&ks8695wdt_miscdev); |
| 246 | if (!res) |
| 247 | ks8695wdt_miscdev.parent = NULL; |
| 248 | |
| 249 | return res; |
| 250 | } |
| 251 | |
| 252 | static void ks8695wdt_shutdown(struct platform_device *pdev) |
| 253 | { |
| 254 | ks8695_wdt_stop(); |
| 255 | } |
| 256 | |
| 257 | #ifdef CONFIG_PM |
| 258 | |
| 259 | static int ks8695wdt_suspend(struct platform_device *pdev, pm_message_t message) |
| 260 | { |
| 261 | ks8695_wdt_stop(); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int ks8695wdt_resume(struct platform_device *pdev) |
| 266 | { |
| 267 | if (ks8695wdt_busy) |
| 268 | ks8695_wdt_start(); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | #else |
| 273 | #define ks8695wdt_suspend NULL |
| 274 | #define ks8695wdt_resume NULL |
| 275 | #endif |
| 276 | |
| 277 | static struct platform_driver ks8695wdt_driver = { |
| 278 | .probe = ks8695wdt_probe, |
Uwe Kleine-König | c98d58e | 2009-03-28 00:26:45 +0100 | [diff] [blame] | 279 | .remove = __devexit_p(ks8695wdt_remove), |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 280 | .shutdown = ks8695wdt_shutdown, |
| 281 | .suspend = ks8695wdt_suspend, |
| 282 | .resume = ks8695wdt_resume, |
| 283 | .driver = { |
| 284 | .name = "ks8695_wdt", |
| 285 | .owner = THIS_MODULE, |
| 286 | }, |
| 287 | }; |
| 288 | |
| 289 | static int __init ks8695_wdt_init(void) |
| 290 | { |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 291 | spin_lock_init(&ks8695_lock); |
| 292 | /* Check that the heartbeat value is within range; |
| 293 | if not reset to the default */ |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 294 | if (ks8695_wdt_settimeout(wdt_time)) { |
| 295 | ks8695_wdt_settimeout(WDT_DEFAULT_TIME); |
Alan Cox | f4fabce | 2008-05-19 14:06:53 +0100 | [diff] [blame] | 296 | pr_info("ks8695_wdt: wdt_time value must be 1 <= wdt_time <= %i, using %d\n", |
| 297 | wdt_time, WDT_MAX_TIME); |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 298 | } |
Andrew Victor | ccb8f43 | 2007-05-14 14:45:25 +0200 | [diff] [blame] | 299 | return platform_driver_register(&ks8695wdt_driver); |
| 300 | } |
| 301 | |
| 302 | static void __exit ks8695_wdt_exit(void) |
| 303 | { |
| 304 | platform_driver_unregister(&ks8695wdt_driver); |
| 305 | } |
| 306 | |
| 307 | module_init(ks8695_wdt_init); |
| 308 | module_exit(ks8695_wdt_exit); |
| 309 | |
| 310 | MODULE_AUTHOR("Andrew Victor"); |
| 311 | MODULE_DESCRIPTION("Watchdog driver for KS8695"); |
| 312 | MODULE_LICENSE("GPL"); |
| 313 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 314 | MODULE_ALIAS("platform:ks8695_wdt"); |