alim7101_wdt.c 11 KB

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