sc520_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * AMD Elan SC520 processor Watchdog Timer driver
  3. *
  4. * Based on acquirewdt.c by Alan Cox,
  5. * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * The authors do NOT admit liability nor provide warranty for
  13. * any of this software. This material is provided "AS-IS" in
  14. * the hope that it may be useful for others.
  15. *
  16. * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
  17. * 9/27 - 2001 [Initial release]
  18. *
  19. * Additional fixes Alan Cox
  20. * - Fixed formatting
  21. * - Removed debug printks
  22. * - Fixed SMP built kernel deadlock
  23. * - Switched to private locks not lock_kernel
  24. * - Used ioremap/writew/readw
  25. * - Added NOWAYOUT support
  26. * 4/12 - 2002 Changes by Rob Radez <rob@osinvestor.com>
  27. * - Change comments
  28. * - Eliminate fop_llseek
  29. * - Change CONFIG_WATCHDOG_NOWAYOUT semantics
  30. * - Add KERN_* tags to printks
  31. * - fix possible wdt_is_open race
  32. * - Report proper capabilities in watchdog_info
  33. * - Add WDIOC_{GETSTATUS, GETBOOTSTATUS, SETTIMEOUT,
  34. * GETTIMEOUT, SETOPTIONS} ioctls
  35. * 09/8 - 2003 Changes by Wim Van Sebroeck <wim@iguana.be>
  36. * - cleanup of trailing spaces
  37. * - added extra printk's for startup problems
  38. * - use module_param
  39. * - made timeout (the emulated heartbeat) a module_param
  40. * - made the keepalive ping an internal subroutine
  41. * 3/27 - 2004 Changes by Sean Young <sean@mess.org>
  42. * - set MMCR_BASE to 0xfffef000
  43. * - CBAR does not need to be read
  44. * - removed debugging printks
  45. *
  46. * This WDT driver is different from most other Linux WDT
  47. * drivers in that the driver will ping the watchdog by itself,
  48. * because this particular WDT has a very short timeout (1.6
  49. * seconds) and it would be insane to count on any userspace
  50. * daemon always getting scheduled within that time frame.
  51. *
  52. * This driver uses memory mapped IO, and spinlock.
  53. */
  54. #include <linux/module.h>
  55. #include <linux/moduleparam.h>
  56. #include <linux/types.h>
  57. #include <linux/timer.h>
  58. #include <linux/miscdevice.h>
  59. #include <linux/watchdog.h>
  60. #include <linux/fs.h>
  61. #include <linux/ioport.h>
  62. #include <linux/notifier.h>
  63. #include <linux/reboot.h>
  64. #include <linux/init.h>
  65. #include <linux/jiffies.h>
  66. #include <asm/io.h>
  67. #include <asm/uaccess.h>
  68. #include <asm/system.h>
  69. #define OUR_NAME "sc520_wdt"
  70. #define PFX OUR_NAME ": "
  71. /*
  72. * The AMD Elan SC520 timeout value is 492us times a power of 2 (0-7)
  73. *
  74. * 0: 492us 2: 1.01s 4: 4.03s 6: 16.22s
  75. * 1: 503ms 3: 2.01s 5: 8.05s 7: 32.21s
  76. *
  77. * We will program the SC520 watchdog for a timeout of 2.01s.
  78. * If we reset the watchdog every ~250ms we should be safe.
  79. */
  80. #define WDT_INTERVAL (HZ/4+1)
  81. /*
  82. * We must not require too good response from the userspace daemon.
  83. * Here we require the userspace daemon to send us a heartbeat
  84. * char to /dev/watchdog every 30 seconds.
  85. */
  86. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  87. static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
  88. module_param(timeout, int, 0);
  89. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  90. static int nowayout = WATCHDOG_NOWAYOUT;
  91. module_param(nowayout, int, 0);
  92. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  93. /*
  94. * AMD Elan SC520 - Watchdog Timer Registers
  95. */
  96. #define MMCR_BASE 0xfffef000 /* The default base address */
  97. #define OFFS_WDTMRCTL 0xCB0 /* Watchdog Timer Control Register */
  98. /* WDT Control Register bit definitions */
  99. #define WDT_EXP_SEL_01 0x0001 /* [01] Time-out = 496 us (with 33 Mhz clk). */
  100. #define WDT_EXP_SEL_02 0x0002 /* [02] Time-out = 508 ms (with 33 Mhz clk). */
  101. #define WDT_EXP_SEL_03 0x0004 /* [03] Time-out = 1.02 s (with 33 Mhz clk). */
  102. #define WDT_EXP_SEL_04 0x0008 /* [04] Time-out = 2.03 s (with 33 Mhz clk). */
  103. #define WDT_EXP_SEL_05 0x0010 /* [05] Time-out = 4.07 s (with 33 Mhz clk). */
  104. #define WDT_EXP_SEL_06 0x0020 /* [06] Time-out = 8.13 s (with 33 Mhz clk). */
  105. #define WDT_EXP_SEL_07 0x0040 /* [07] Time-out = 16.27s (with 33 Mhz clk). */
  106. #define WDT_EXP_SEL_08 0x0080 /* [08] Time-out = 32.54s (with 33 Mhz clk). */
  107. #define WDT_IRQ_FLG 0x1000 /* [12] Interrupt Request Flag */
  108. #define WDT_WRST_ENB 0x4000 /* [14] Watchdog Timer Reset Enable */
  109. #define WDT_ENB 0x8000 /* [15] Watchdog Timer Enable */
  110. static __u16 __iomem *wdtmrctl;
  111. static void wdt_timer_ping(unsigned long);
  112. static struct timer_list timer;
  113. static unsigned long next_heartbeat;
  114. static unsigned long wdt_is_open;
  115. static char wdt_expect_close;
  116. static spinlock_t wdt_spinlock;
  117. /*
  118. * Whack the dog
  119. */
  120. static void wdt_timer_ping(unsigned long data)
  121. {
  122. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  123. * we agree to ping the WDT
  124. */
  125. if(time_before(jiffies, next_heartbeat))
  126. {
  127. /* Ping the WDT */
  128. spin_lock(&wdt_spinlock);
  129. writew(0xAAAA, wdtmrctl);
  130. writew(0x5555, wdtmrctl);
  131. spin_unlock(&wdt_spinlock);
  132. /* Re-set the timer interval */
  133. timer.expires = jiffies + WDT_INTERVAL;
  134. add_timer(&timer);
  135. } else {
  136. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  137. }
  138. }
  139. /*
  140. * Utility routines
  141. */
  142. static void wdt_config(int writeval)
  143. {
  144. __u16 dummy;
  145. unsigned long flags;
  146. /* buy some time (ping) */
  147. spin_lock_irqsave(&wdt_spinlock, flags);
  148. dummy=readw(wdtmrctl); /* ensure write synchronization */
  149. writew(0xAAAA, wdtmrctl);
  150. writew(0x5555, wdtmrctl);
  151. /* unlock WDT = make WDT configuration register writable one time */
  152. writew(0x3333, wdtmrctl);
  153. writew(0xCCCC, wdtmrctl);
  154. /* write WDT configuration register */
  155. writew(writeval, wdtmrctl);
  156. spin_unlock_irqrestore(&wdt_spinlock, flags);
  157. }
  158. static int wdt_startup(void)
  159. {
  160. next_heartbeat = jiffies + (timeout * HZ);
  161. /* Start the timer */
  162. timer.expires = jiffies + WDT_INTERVAL;
  163. add_timer(&timer);
  164. /* Start the watchdog */
  165. wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
  166. printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
  167. return 0;
  168. }
  169. static int wdt_turnoff(void)
  170. {
  171. /* Stop the timer */
  172. del_timer(&timer);
  173. /* Stop the watchdog */
  174. wdt_config(0);
  175. printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
  176. return 0;
  177. }
  178. static int wdt_keepalive(void)
  179. {
  180. /* user land ping */
  181. next_heartbeat = jiffies + (timeout * HZ);
  182. return 0;
  183. }
  184. static int wdt_set_heartbeat(int t)
  185. {
  186. if ((t < 1) || (t > 3600)) /* arbitrary upper limit */
  187. return -EINVAL;
  188. timeout = t;
  189. return 0;
  190. }
  191. /*
  192. * /dev/watchdog handling
  193. */
  194. static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
  195. {
  196. /* See if we got the magic character 'V' and reload the timer */
  197. if(count) {
  198. if (!nowayout) {
  199. size_t ofs;
  200. /* note: just in case someone wrote the magic character
  201. * five months ago... */
  202. wdt_expect_close = 0;
  203. /* now scan */
  204. for(ofs = 0; ofs != count; ofs++) {
  205. char c;
  206. if (get_user(c, buf + ofs))
  207. return -EFAULT;
  208. if(c == 'V')
  209. wdt_expect_close = 42;
  210. }
  211. }
  212. /* Well, anyhow someone wrote to us, we should return that favour */
  213. wdt_keepalive();
  214. }
  215. return count;
  216. }
  217. static int fop_open(struct inode * inode, struct file * file)
  218. {
  219. nonseekable_open(inode, file);
  220. /* Just in case we're already talking to someone... */
  221. if(test_and_set_bit(0, &wdt_is_open))
  222. return -EBUSY;
  223. if (nowayout)
  224. __module_get(THIS_MODULE);
  225. /* Good, fire up the show */
  226. wdt_startup();
  227. return 0;
  228. }
  229. static int fop_close(struct inode * inode, struct file * file)
  230. {
  231. if(wdt_expect_close == 42) {
  232. wdt_turnoff();
  233. } else {
  234. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  235. wdt_keepalive();
  236. }
  237. clear_bit(0, &wdt_is_open);
  238. wdt_expect_close = 0;
  239. return 0;
  240. }
  241. static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  242. unsigned long arg)
  243. {
  244. void __user *argp = (void __user *)arg;
  245. int __user *p = argp;
  246. static struct watchdog_info ident = {
  247. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  248. .firmware_version = 1,
  249. .identity = "SC520",
  250. };
  251. switch(cmd)
  252. {
  253. default:
  254. return -ENOTTY;
  255. case WDIOC_GETSUPPORT:
  256. return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
  257. case WDIOC_GETSTATUS:
  258. case WDIOC_GETBOOTSTATUS:
  259. return put_user(0, p);
  260. case WDIOC_KEEPALIVE:
  261. wdt_keepalive();
  262. return 0;
  263. case WDIOC_SETOPTIONS:
  264. {
  265. int new_options, retval = -EINVAL;
  266. if(get_user(new_options, p))
  267. return -EFAULT;
  268. if(new_options & WDIOS_DISABLECARD) {
  269. wdt_turnoff();
  270. retval = 0;
  271. }
  272. if(new_options & WDIOS_ENABLECARD) {
  273. wdt_startup();
  274. retval = 0;
  275. }
  276. return retval;
  277. }
  278. case WDIOC_SETTIMEOUT:
  279. {
  280. int new_timeout;
  281. if(get_user(new_timeout, p))
  282. return -EFAULT;
  283. if(wdt_set_heartbeat(new_timeout))
  284. return -EINVAL;
  285. wdt_keepalive();
  286. /* Fall through */
  287. }
  288. case WDIOC_GETTIMEOUT:
  289. return put_user(timeout, p);
  290. }
  291. }
  292. static const struct file_operations wdt_fops = {
  293. .owner = THIS_MODULE,
  294. .llseek = no_llseek,
  295. .write = fop_write,
  296. .open = fop_open,
  297. .release = fop_close,
  298. .ioctl = fop_ioctl,
  299. };
  300. static struct miscdevice wdt_miscdev = {
  301. .minor = WATCHDOG_MINOR,
  302. .name = "watchdog",
  303. .fops = &wdt_fops,
  304. };
  305. /*
  306. * Notifier for system down
  307. */
  308. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  309. void *unused)
  310. {
  311. if(code==SYS_DOWN || code==SYS_HALT)
  312. wdt_turnoff();
  313. return NOTIFY_DONE;
  314. }
  315. /*
  316. * The WDT needs to learn about soft shutdowns in order to
  317. * turn the timebomb registers off.
  318. */
  319. static struct notifier_block wdt_notifier = {
  320. .notifier_call = wdt_notify_sys,
  321. };
  322. static void __exit sc520_wdt_unload(void)
  323. {
  324. if (!nowayout)
  325. wdt_turnoff();
  326. /* Deregister */
  327. misc_deregister(&wdt_miscdev);
  328. unregister_reboot_notifier(&wdt_notifier);
  329. iounmap(wdtmrctl);
  330. }
  331. static int __init sc520_wdt_init(void)
  332. {
  333. int rc = -EBUSY;
  334. spin_lock_init(&wdt_spinlock);
  335. init_timer(&timer);
  336. timer.function = wdt_timer_ping;
  337. timer.data = 0;
  338. /* Check that the timeout value is within it's range ; if not reset to the default */
  339. if (wdt_set_heartbeat(timeout)) {
  340. wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  341. printk(KERN_INFO PFX "timeout value must be 1<=timeout<=3600, using %d\n",
  342. WATCHDOG_TIMEOUT);
  343. }
  344. wdtmrctl = ioremap((unsigned long)(MMCR_BASE + OFFS_WDTMRCTL), 2);
  345. if (!wdtmrctl) {
  346. printk(KERN_ERR PFX "Unable to remap memory\n");
  347. rc = -ENOMEM;
  348. goto err_out_region2;
  349. }
  350. rc = register_reboot_notifier(&wdt_notifier);
  351. if (rc) {
  352. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  353. rc);
  354. goto err_out_ioremap;
  355. }
  356. rc = misc_register(&wdt_miscdev);
  357. if (rc) {
  358. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  359. WATCHDOG_MINOR, rc);
  360. goto err_out_notifier;
  361. }
  362. printk(KERN_INFO PFX "WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
  363. timeout,nowayout);
  364. return 0;
  365. err_out_notifier:
  366. unregister_reboot_notifier(&wdt_notifier);
  367. err_out_ioremap:
  368. iounmap(wdtmrctl);
  369. err_out_region2:
  370. return rc;
  371. }
  372. module_init(sc520_wdt_init);
  373. module_exit(sc520_wdt_unload);
  374. MODULE_AUTHOR("Scott and Bill Jennings");
  375. MODULE_DESCRIPTION("Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
  376. MODULE_LICENSE("GPL");
  377. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);