alim7101_wdt.c 11 KB

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