alim7101_wdt.c 11 KB

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