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