alim7101_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 DEFINE_TIMER(timer, wdt_timer_ping, 0, 1);
  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="
  69. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  70. /*
  71. * Whack the dog
  72. */
  73. static void wdt_timer_ping(unsigned long data)
  74. {
  75. /* If we got a heartbeat pulse within the WDT_US_INTERVAL
  76. * we agree to ping the WDT
  77. */
  78. char tmp;
  79. if(time_before(jiffies, next_heartbeat))
  80. {
  81. /* Ping the WDT (this is actually a disarm/arm sequence) */
  82. pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
  83. pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
  84. pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
  85. if (use_gpio) {
  86. pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
  87. pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
  88. | 0x20);
  89. pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
  90. & ~0x20);
  91. }
  92. } else {
  93. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  94. }
  95. /* Re-set the timer interval */
  96. mod_timer(&timer, jiffies + WDT_INTERVAL);
  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. mod_timer(&timer, jiffies + WDT_INTERVAL);
  127. printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
  128. }
  129. static void wdt_turnoff(void)
  130. {
  131. /* Stop the timer */
  132. del_timer_sync(&timer);
  133. wdt_change(WDT_DISABLE);
  134. printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
  135. }
  136. static void wdt_keepalive(void)
  137. {
  138. /* user land ping */
  139. next_heartbeat = jiffies + (timeout * HZ);
  140. }
  141. /*
  142. * /dev/watchdog handling
  143. */
  144. static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
  145. {
  146. /* See if we got the magic character 'V' and reload the timer */
  147. if(count) {
  148. if (!nowayout) {
  149. size_t ofs;
  150. /* note: just in case someone wrote the magic character
  151. * five months ago... */
  152. wdt_expect_close = 0;
  153. /* now scan */
  154. for (ofs = 0; ofs != count; ofs++) {
  155. char c;
  156. if (get_user(c, buf+ofs))
  157. return -EFAULT;
  158. if (c == 'V')
  159. wdt_expect_close = 42;
  160. }
  161. }
  162. /* someone wrote to us, we should restart timer */
  163. wdt_keepalive();
  164. }
  165. return count;
  166. }
  167. static int fop_open(struct inode * inode, struct file * file)
  168. {
  169. /* Just in case we're already talking to someone... */
  170. if(test_and_set_bit(0, &wdt_is_open))
  171. return -EBUSY;
  172. /* Good, fire up the show */
  173. wdt_startup();
  174. return nonseekable_open(inode, file);
  175. }
  176. static int fop_close(struct inode * inode, struct file * file)
  177. {
  178. if(wdt_expect_close == 42)
  179. wdt_turnoff();
  180. else {
  181. /* wim: shouldn't there be a: del_timer(&timer); */
  182. printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
  183. }
  184. clear_bit(0, &wdt_is_open);
  185. wdt_expect_close = 0;
  186. return 0;
  187. }
  188. static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  189. {
  190. void __user *argp = (void __user *)arg;
  191. int __user *p = argp;
  192. static struct watchdog_info ident =
  193. {
  194. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  195. .firmware_version = 1,
  196. .identity = "ALiM7101",
  197. };
  198. switch(cmd)
  199. {
  200. case WDIOC_GETSUPPORT:
  201. return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
  202. case WDIOC_GETSTATUS:
  203. case WDIOC_GETBOOTSTATUS:
  204. return put_user(0, p);
  205. case WDIOC_KEEPALIVE:
  206. wdt_keepalive();
  207. return 0;
  208. case WDIOC_SETOPTIONS:
  209. {
  210. int new_options, retval = -EINVAL;
  211. if(get_user(new_options, p))
  212. return -EFAULT;
  213. if(new_options & WDIOS_DISABLECARD) {
  214. wdt_turnoff();
  215. retval = 0;
  216. }
  217. if(new_options & WDIOS_ENABLECARD) {
  218. wdt_startup();
  219. retval = 0;
  220. }
  221. return retval;
  222. }
  223. case WDIOC_SETTIMEOUT:
  224. {
  225. int new_timeout;
  226. if(get_user(new_timeout, p))
  227. return -EFAULT;
  228. if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
  229. return -EINVAL;
  230. timeout = new_timeout;
  231. wdt_keepalive();
  232. /* Fall through */
  233. }
  234. case WDIOC_GETTIMEOUT:
  235. return put_user(timeout, p);
  236. default:
  237. return -ENOTTY;
  238. }
  239. }
  240. static const struct file_operations wdt_fops = {
  241. .owner= THIS_MODULE,
  242. .llseek= no_llseek,
  243. .write= fop_write,
  244. .open= fop_open,
  245. .release= fop_close,
  246. .ioctl= fop_ioctl,
  247. };
  248. static struct miscdevice wdt_miscdev = {
  249. .minor=WATCHDOG_MINOR,
  250. .name="watchdog",
  251. .fops=&wdt_fops,
  252. };
  253. /*
  254. * Notifier for system down
  255. */
  256. static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  257. {
  258. if (code==SYS_DOWN || code==SYS_HALT)
  259. wdt_turnoff();
  260. if (code==SYS_RESTART) {
  261. /*
  262. * Cobalt devices have no way of rebooting themselves other than
  263. * getting the watchdog to pull reset, so we restart the watchdog on
  264. * reboot with no heartbeat
  265. */
  266. wdt_change(WDT_ENABLE);
  267. printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
  268. }
  269. return NOTIFY_DONE;
  270. }
  271. /*
  272. * The WDT needs to learn about soft shutdowns in order to
  273. * turn the timebomb registers off.
  274. */
  275. static struct notifier_block wdt_notifier=
  276. {
  277. .notifier_call = wdt_notify_sys,
  278. };
  279. static void __exit alim7101_wdt_unload(void)
  280. {
  281. wdt_turnoff();
  282. /* Deregister */
  283. misc_deregister(&wdt_miscdev);
  284. unregister_reboot_notifier(&wdt_notifier);
  285. pci_dev_put(alim7101_pmu);
  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_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
  294. NULL);
  295. if (!alim7101_pmu) {
  296. printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
  297. return -EBUSY;
  298. }
  299. /* Set the WDT in the PMU to 1 second */
  300. pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
  301. ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
  302. NULL);
  303. if (!ali1543_south) {
  304. printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
  305. goto err_out;
  306. }
  307. pci_read_config_byte(ali1543_south, 0x5e, &tmp);
  308. pci_dev_put(ali1543_south);
  309. if ((tmp & 0x1e) == 0x00) {
  310. if (!use_gpio) {
  311. printk(KERN_INFO PFX "Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
  312. goto err_out;
  313. }
  314. nowayout = 1;
  315. } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
  316. printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
  317. goto err_out;
  318. }
  319. if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
  320. {
  321. timeout = WATCHDOG_TIMEOUT;
  322. printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
  323. timeout);
  324. }
  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. pci_dev_put(alim7101_pmu);
  347. return rc;
  348. }
  349. module_init(alim7101_wdt_init);
  350. module_exit(alim7101_wdt_unload);
  351. static struct pci_device_id alim7101_pci_tbl[] __devinitdata = {
  352. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
  353. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
  354. { }
  355. };
  356. MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
  357. MODULE_AUTHOR("Steve Hill");
  358. MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
  359. MODULE_LICENSE("GPL");
  360. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);