alim7101_wdt.c 10 KB

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