sc520_wdt.c 11 KB

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