alim7101_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALi M7101 PMU Computer Watchdog Timer driver
  4. *
  5. * Based on w83877f_wdt.c by Scott Jennings <linuxdrivers@oro.net>
  6. * and the Cobalt kernel WDT timer driver by Tim Hockin
  7. * <thockin@cobaltnet.com>
  8. *
  9. * (c)2002 Steve Hill <steve@navaho.co.uk>
  10. *
  11. * This WDT driver is different from most other Linux WDT
  12. * drivers in that the driver will ping the watchdog by itself,
  13. * because this particular WDT has a very short timeout (1.6
  14. * seconds) and it would be insane to count on any userspace
  15. * daemon always getting scheduled within that time frame.
  16. *
  17. * Additions:
  18. * Aug 23, 2004 - Added use_gpio module parameter for use on revision a1d PMUs
  19. * found on very old cobalt hardware.
  20. * -- Mike Waychison <michael.waychison@sun.com>
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/types.h>
  26. #include <linux/timer.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/ioport.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/init.h>
  33. #include <linux/fs.h>
  34. #include <linux/pci.h>
  35. #include <linux/io.h>
  36. #include <linux/uaccess.h>
  37. #define WDT_ENABLE 0x9C
  38. #define WDT_DISABLE 0x8C
  39. #define ALI_7101_WDT 0x92
  40. #define ALI_7101_GPIO 0x7D
  41. #define ALI_7101_GPIO_O 0x7E
  42. #define ALI_WDT_ARM 0x01
  43. /*
  44. * We're going to use a 1 second timeout.
  45. * If we reset the watchdog every ~250ms we should be safe. */
  46. #define WDT_INTERVAL (HZ/4+1)
  47. /*
  48. * We must not require too good response from the userspace daemon.
  49. * Here we require the userspace daemon to send us a heartbeat
  50. * char to /dev/watchdog every 30 seconds.
  51. */
  52. #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
  53. /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
  54. static int timeout = WATCHDOG_TIMEOUT;
  55. module_param(timeout, int, 0);
  56. MODULE_PARM_DESC(timeout,
  57. "Watchdog timeout in seconds. (1<=timeout<=3600, default="
  58. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  59. static int use_gpio; /* Use the pic (for a1d revision alim7101) */
  60. module_param(use_gpio, int, 0);
  61. MODULE_PARM_DESC(use_gpio,
  62. "Use the gpio watchdog (required by old cobalt boards).");
  63. static void wdt_timer_ping(struct timer_list *);
  64. static DEFINE_TIMER(timer, wdt_timer_ping);
  65. static unsigned long next_heartbeat;
  66. static unsigned long wdt_is_open;
  67. static char wdt_expect_close;
  68. static struct pci_dev *alim7101_pmu;
  69. static bool nowayout = WATCHDOG_NOWAYOUT;
  70. module_param(nowayout, bool, 0);
  71. MODULE_PARM_DESC(nowayout,
  72. "Watchdog cannot be stopped once started (default="
  73. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  74. /*
  75. * Whack the dog
  76. */
  77. static void wdt_timer_ping(struct timer_list *unused)
  78. {
  79. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  80. * we agree to ping the WDT
  81. */
  82. char tmp;
  83. if (time_before(jiffies, next_heartbeat)) {
  84. /* Ping the WDT (this is actually a disarm/arm sequence) */
  85. pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
  86. pci_write_config_byte(alim7101_pmu,
  87. ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
  88. pci_write_config_byte(alim7101_pmu,
  89. ALI_7101_WDT, (tmp | ALI_WDT_ARM));
  90. if (use_gpio) {
  91. pci_read_config_byte(alim7101_pmu,
  92. ALI_7101_GPIO_O, &tmp);
  93. pci_write_config_byte(alim7101_pmu,
  94. ALI_7101_GPIO_O, tmp | 0x20);
  95. pci_write_config_byte(alim7101_pmu,
  96. ALI_7101_GPIO_O, tmp & ~0x20);
  97. }
  98. } else {
  99. pr_warn("Heartbeat lost! Will not ping the watchdog\n");
  100. }
  101. /* Re-set the timer interval */
  102. mod_timer(&timer, jiffies + WDT_INTERVAL);
  103. }
  104. /*
  105. * Utility routines
  106. */
  107. static void wdt_change(int writeval)
  108. {
  109. char tmp;
  110. pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
  111. if (writeval == WDT_ENABLE) {
  112. pci_write_config_byte(alim7101_pmu,
  113. ALI_7101_WDT, (tmp | ALI_WDT_ARM));
  114. if (use_gpio) {
  115. pci_read_config_byte(alim7101_pmu,
  116. ALI_7101_GPIO_O, &tmp);
  117. pci_write_config_byte(alim7101_pmu,
  118. ALI_7101_GPIO_O, tmp & ~0x20);
  119. }
  120. } else {
  121. pci_write_config_byte(alim7101_pmu,
  122. ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
  123. if (use_gpio) {
  124. pci_read_config_byte(alim7101_pmu,
  125. ALI_7101_GPIO_O, &tmp);
  126. pci_write_config_byte(alim7101_pmu,
  127. ALI_7101_GPIO_O, tmp | 0x20);
  128. }
  129. }
  130. }
  131. static void wdt_startup(void)
  132. {
  133. next_heartbeat = jiffies + (timeout * HZ);
  134. /* We must enable before we kick off the timer in case the timer
  135. occurs as we ping it */
  136. wdt_change(WDT_ENABLE);
  137. /* Start the timer */
  138. mod_timer(&timer, jiffies + WDT_INTERVAL);
  139. pr_info("Watchdog timer is now enabled\n");
  140. }
  141. static void wdt_turnoff(void)
  142. {
  143. /* Stop the timer */
  144. del_timer_sync(&timer);
  145. wdt_change(WDT_DISABLE);
  146. pr_info("Watchdog timer is now disabled...\n");
  147. }
  148. static void wdt_keepalive(void)
  149. {
  150. /* user land ping */
  151. next_heartbeat = jiffies + (timeout * HZ);
  152. }
  153. /*
  154. * /dev/watchdog handling
  155. */
  156. static ssize_t fop_write(struct file *file, const char __user *buf,
  157. size_t count, loff_t *ppos)
  158. {
  159. /* See if we got the magic character 'V' and reload the timer */
  160. if (count) {
  161. if (!nowayout) {
  162. size_t ofs;
  163. /* note: just in case someone wrote the magic character
  164. * five months ago... */
  165. wdt_expect_close = 0;
  166. /* now scan */
  167. for (ofs = 0; ofs != count; ofs++) {
  168. char c;
  169. if (get_user(c, buf + ofs))
  170. return -EFAULT;
  171. if (c == 'V')
  172. wdt_expect_close = 42;
  173. }
  174. }
  175. /* someone wrote to us, we should restart timer */
  176. wdt_keepalive();
  177. }
  178. return count;
  179. }
  180. static int fop_open(struct inode *inode, struct file *file)
  181. {
  182. /* Just in case we're already talking to someone... */
  183. if (test_and_set_bit(0, &wdt_is_open))
  184. return -EBUSY;
  185. /* Good, fire up the show */
  186. wdt_startup();
  187. return stream_open(inode, file);
  188. }
  189. static int fop_close(struct inode *inode, struct file *file)
  190. {
  191. if (wdt_expect_close == 42)
  192. wdt_turnoff();
  193. else {
  194. /* wim: shouldn't there be a: del_timer(&timer); */
  195. pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
  196. }
  197. clear_bit(0, &wdt_is_open);
  198. wdt_expect_close = 0;
  199. return 0;
  200. }
  201. static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  202. {
  203. void __user *argp = (void __user *)arg;
  204. int __user *p = argp;
  205. static const struct watchdog_info ident = {
  206. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
  207. | WDIOF_MAGICCLOSE,
  208. .firmware_version = 1,
  209. .identity = "ALiM7101",
  210. };
  211. switch (cmd) {
  212. case WDIOC_GETSUPPORT:
  213. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  214. case WDIOC_GETSTATUS:
  215. case WDIOC_GETBOOTSTATUS:
  216. return put_user(0, p);
  217. case WDIOC_SETOPTIONS:
  218. {
  219. int new_options, retval = -EINVAL;
  220. if (get_user(new_options, p))
  221. return -EFAULT;
  222. if (new_options & WDIOS_DISABLECARD) {
  223. wdt_turnoff();
  224. retval = 0;
  225. }
  226. if (new_options & WDIOS_ENABLECARD) {
  227. wdt_startup();
  228. retval = 0;
  229. }
  230. return retval;
  231. }
  232. case WDIOC_KEEPALIVE:
  233. wdt_keepalive();
  234. return 0;
  235. case WDIOC_SETTIMEOUT:
  236. {
  237. int new_timeout;
  238. if (get_user(new_timeout, p))
  239. return -EFAULT;
  240. /* arbitrary upper limit */
  241. if (new_timeout < 1 || new_timeout > 3600)
  242. return -EINVAL;
  243. timeout = new_timeout;
  244. wdt_keepalive();
  245. }
  246. fallthrough;
  247. case WDIOC_GETTIMEOUT:
  248. return put_user(timeout, p);
  249. default:
  250. return -ENOTTY;
  251. }
  252. }
  253. static const struct file_operations wdt_fops = {
  254. .owner = THIS_MODULE,
  255. .llseek = no_llseek,
  256. .write = fop_write,
  257. .open = fop_open,
  258. .release = fop_close,
  259. .unlocked_ioctl = fop_ioctl,
  260. .compat_ioctl = compat_ptr_ioctl,
  261. };
  262. static struct miscdevice wdt_miscdev = {
  263. .minor = WATCHDOG_MINOR,
  264. .name = "watchdog",
  265. .fops = &wdt_fops,
  266. };
  267. static int wdt_restart_handle(struct notifier_block *this, unsigned long mode,
  268. void *cmd)
  269. {
  270. /*
  271. * Cobalt devices have no way of rebooting themselves other
  272. * than getting the watchdog to pull reset, so we restart the
  273. * watchdog on reboot with no heartbeat.
  274. */
  275. wdt_change(WDT_ENABLE);
  276. /* loop until the watchdog fires */
  277. while (true)
  278. ;
  279. return NOTIFY_DONE;
  280. }
  281. static struct notifier_block wdt_restart_handler = {
  282. .notifier_call = wdt_restart_handle,
  283. .priority = 128,
  284. };
  285. /*
  286. * Notifier for system down
  287. */
  288. static int wdt_notify_sys(struct notifier_block *this,
  289. unsigned long code, void *unused)
  290. {
  291. if (code == SYS_DOWN || code == SYS_HALT)
  292. wdt_turnoff();
  293. return NOTIFY_DONE;
  294. }
  295. /*
  296. * The WDT needs to learn about soft shutdowns in order to
  297. * turn the timebomb registers off.
  298. */
  299. static struct notifier_block wdt_notifier = {
  300. .notifier_call = wdt_notify_sys,
  301. };
  302. static void __exit alim7101_wdt_unload(void)
  303. {
  304. wdt_turnoff();
  305. /* Deregister */
  306. misc_deregister(&wdt_miscdev);
  307. unregister_reboot_notifier(&wdt_notifier);
  308. unregister_restart_handler(&wdt_restart_handler);
  309. pci_dev_put(alim7101_pmu);
  310. }
  311. static int __init alim7101_wdt_init(void)
  312. {
  313. int rc = -EBUSY;
  314. struct pci_dev *ali1543_south;
  315. char tmp;
  316. pr_info("Steve Hill <steve@navaho.co.uk>\n");
  317. alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
  318. NULL);
  319. if (!alim7101_pmu) {
  320. pr_info("ALi M7101 PMU not present - WDT not set\n");
  321. return -EBUSY;
  322. }
  323. /* Set the WDT in the PMU to 1 second */
  324. pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
  325. ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
  326. NULL);
  327. if (!ali1543_south) {
  328. pr_info("ALi 1543 South-Bridge not present - WDT not set\n");
  329. goto err_out;
  330. }
  331. pci_read_config_byte(ali1543_south, 0x5e, &tmp);
  332. pci_dev_put(ali1543_south);
  333. if ((tmp & 0x1e) == 0x00) {
  334. if (!use_gpio) {
  335. pr_info("Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
  336. goto err_out;
  337. }
  338. nowayout = 1;
  339. } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
  340. pr_info("ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
  341. goto err_out;
  342. }
  343. if (timeout < 1 || timeout > 3600) {
  344. /* arbitrary upper limit */
  345. timeout = WATCHDOG_TIMEOUT;
  346. pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
  347. timeout);
  348. }
  349. rc = register_reboot_notifier(&wdt_notifier);
  350. if (rc) {
  351. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  352. goto err_out;
  353. }
  354. rc = register_restart_handler(&wdt_restart_handler);
  355. if (rc) {
  356. pr_err("cannot register restart handler (err=%d)\n", rc);
  357. goto err_out_reboot;
  358. }
  359. rc = misc_register(&wdt_miscdev);
  360. if (rc) {
  361. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  362. wdt_miscdev.minor, rc);
  363. goto err_out_restart;
  364. }
  365. if (nowayout)
  366. __module_get(THIS_MODULE);
  367. pr_info("WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
  368. timeout, nowayout);
  369. return 0;
  370. err_out_restart:
  371. unregister_restart_handler(&wdt_restart_handler);
  372. err_out_reboot:
  373. unregister_reboot_notifier(&wdt_notifier);
  374. err_out:
  375. pci_dev_put(alim7101_pmu);
  376. return rc;
  377. }
  378. module_init(alim7101_wdt_init);
  379. module_exit(alim7101_wdt_unload);
  380. static const struct pci_device_id alim7101_pci_tbl[] __used = {
  381. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
  382. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
  386. MODULE_AUTHOR("Steve Hill");
  387. MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
  388. MODULE_LICENSE("GPL");