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. /* Fall through */
  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. };
  261. static struct miscdevice wdt_miscdev = {
  262. .minor = WATCHDOG_MINOR,
  263. .name = "watchdog",
  264. .fops = &wdt_fops,
  265. };
  266. static int wdt_restart_handle(struct notifier_block *this, unsigned long mode,
  267. void *cmd)
  268. {
  269. /*
  270. * Cobalt devices have no way of rebooting themselves other
  271. * than getting the watchdog to pull reset, so we restart the
  272. * watchdog on reboot with no heartbeat.
  273. */
  274. wdt_change(WDT_ENABLE);
  275. /* loop until the watchdog fires */
  276. while (true)
  277. ;
  278. return NOTIFY_DONE;
  279. }
  280. static struct notifier_block wdt_restart_handler = {
  281. .notifier_call = wdt_restart_handle,
  282. .priority = 128,
  283. };
  284. /*
  285. * Notifier for system down
  286. */
  287. static int wdt_notify_sys(struct notifier_block *this,
  288. unsigned long code, void *unused)
  289. {
  290. if (code == SYS_DOWN || code == SYS_HALT)
  291. wdt_turnoff();
  292. return NOTIFY_DONE;
  293. }
  294. /*
  295. * The WDT needs to learn about soft shutdowns in order to
  296. * turn the timebomb registers off.
  297. */
  298. static struct notifier_block wdt_notifier = {
  299. .notifier_call = wdt_notify_sys,
  300. };
  301. static void __exit alim7101_wdt_unload(void)
  302. {
  303. wdt_turnoff();
  304. /* Deregister */
  305. misc_deregister(&wdt_miscdev);
  306. unregister_reboot_notifier(&wdt_notifier);
  307. unregister_restart_handler(&wdt_restart_handler);
  308. pci_dev_put(alim7101_pmu);
  309. }
  310. static int __init alim7101_wdt_init(void)
  311. {
  312. int rc = -EBUSY;
  313. struct pci_dev *ali1543_south;
  314. char tmp;
  315. pr_info("Steve Hill <steve@navaho.co.uk>\n");
  316. alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
  317. NULL);
  318. if (!alim7101_pmu) {
  319. pr_info("ALi M7101 PMU not present - WDT not set\n");
  320. return -EBUSY;
  321. }
  322. /* Set the WDT in the PMU to 1 second */
  323. pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
  324. ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
  325. NULL);
  326. if (!ali1543_south) {
  327. pr_info("ALi 1543 South-Bridge not present - WDT not set\n");
  328. goto err_out;
  329. }
  330. pci_read_config_byte(ali1543_south, 0x5e, &tmp);
  331. pci_dev_put(ali1543_south);
  332. if ((tmp & 0x1e) == 0x00) {
  333. if (!use_gpio) {
  334. pr_info("Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
  335. goto err_out;
  336. }
  337. nowayout = 1;
  338. } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
  339. pr_info("ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
  340. goto err_out;
  341. }
  342. if (timeout < 1 || timeout > 3600) {
  343. /* arbitrary upper limit */
  344. timeout = WATCHDOG_TIMEOUT;
  345. pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
  346. timeout);
  347. }
  348. rc = register_reboot_notifier(&wdt_notifier);
  349. if (rc) {
  350. pr_err("cannot register reboot notifier (err=%d)\n", rc);
  351. goto err_out;
  352. }
  353. rc = register_restart_handler(&wdt_restart_handler);
  354. if (rc) {
  355. pr_err("cannot register restart handler (err=%d)\n", rc);
  356. goto err_out_reboot;
  357. }
  358. rc = misc_register(&wdt_miscdev);
  359. if (rc) {
  360. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  361. wdt_miscdev.minor, rc);
  362. goto err_out_restart;
  363. }
  364. if (nowayout)
  365. __module_get(THIS_MODULE);
  366. pr_info("WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
  367. timeout, nowayout);
  368. return 0;
  369. err_out_restart:
  370. unregister_restart_handler(&wdt_restart_handler);
  371. err_out_reboot:
  372. unregister_reboot_notifier(&wdt_notifier);
  373. err_out:
  374. pci_dev_put(alim7101_pmu);
  375. return rc;
  376. }
  377. module_init(alim7101_wdt_init);
  378. module_exit(alim7101_wdt_unload);
  379. static const struct pci_device_id alim7101_pci_tbl[] __used = {
  380. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
  381. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
  382. { }
  383. };
  384. MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
  385. MODULE_AUTHOR("Steve Hill");
  386. MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
  387. MODULE_LICENSE("GPL");