alim1535_wdt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/types.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/ioport.h>
  15. #include <linux/notifier.h>
  16. #include <linux/reboot.h>
  17. #include <linux/init.h>
  18. #include <linux/fs.h>
  19. #include <linux/pci.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/io.h>
  22. #define WATCHDOG_NAME "ALi_M1535"
  23. #define PFX WATCHDOG_NAME ": "
  24. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  25. /* internal variables */
  26. static unsigned long ali_is_open;
  27. static char ali_expect_release;
  28. static struct pci_dev *ali_pci;
  29. static u32 ali_timeout_bits; /* stores the computed timeout */
  30. static spinlock_t ali_lock; /* Guards the hardware */
  31. /* module parameters */
  32. static int timeout = WATCHDOG_TIMEOUT;
  33. module_param(timeout, int, 0);
  34. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (0<timeout<18000, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  35. static int nowayout = WATCHDOG_NOWAYOUT;
  36. module_param(nowayout, int, 0);
  37. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  38. /*
  39. * ali_start - start watchdog countdown
  40. *
  41. * Starts the timer running providing the timer has a counter
  42. * configuration set.
  43. */
  44. static void ali_start(void)
  45. {
  46. u32 val;
  47. spin_lock(&ali_lock);
  48. pci_read_config_dword(ali_pci, 0xCC, &val);
  49. val &= ~0x3F; /* Mask count */
  50. val |= (1<<25) | ali_timeout_bits;
  51. pci_write_config_dword(ali_pci, 0xCC, val);
  52. spin_unlock(&ali_lock);
  53. }
  54. /*
  55. * ali_stop - stop the timer countdown
  56. *
  57. * Stop the ALi watchdog countdown
  58. */
  59. static void ali_stop(void)
  60. {
  61. u32 val;
  62. spin_lock(&ali_lock);
  63. pci_read_config_dword(ali_pci, 0xCC, &val);
  64. val &= ~0x3F; /* Mask count to zero (disabled) */
  65. val &= ~(1<<25);/* and for safety mask the reset enable */
  66. pci_write_config_dword(ali_pci, 0xCC, val);
  67. spin_unlock(&ali_lock);
  68. }
  69. /*
  70. * ali_keepalive - send a keepalive to the watchdog
  71. *
  72. * Send a keepalive to the timer (actually we restart the timer).
  73. */
  74. static void ali_keepalive(void)
  75. {
  76. ali_start();
  77. }
  78. /*
  79. * ali_settimer - compute the timer reload value
  80. * @t: time in seconds
  81. *
  82. * Computes the timeout values needed
  83. */
  84. static int ali_settimer(int t)
  85. {
  86. if(t < 0)
  87. return -EINVAL;
  88. else if(t < 60)
  89. ali_timeout_bits = t|(1<<6);
  90. else if(t < 3600)
  91. ali_timeout_bits = (t/60)|(1<<7);
  92. else if(t < 18000)
  93. ali_timeout_bits = (t/300)|(1<<6)|(1<<7);
  94. else return -EINVAL;
  95. timeout = t;
  96. return 0;
  97. }
  98. /*
  99. * /dev/watchdog handling
  100. */
  101. /*
  102. * ali_write - writes to ALi watchdog
  103. * @file: file from VFS
  104. * @data: user address of data
  105. * @len: length of data
  106. * @ppos: pointer to the file offset
  107. *
  108. * Handle a write to the ALi watchdog. Writing to the file pings
  109. * the watchdog and resets it. Writing the magic 'V' sequence allows
  110. * the next close to turn off the watchdog.
  111. */
  112. static ssize_t ali_write(struct file *file, const char __user *data,
  113. size_t len, loff_t * ppos)
  114. {
  115. /* See if we got the magic character 'V' and reload the timer */
  116. if (len) {
  117. if (!nowayout) {
  118. size_t i;
  119. /* note: just in case someone wrote the magic character
  120. * five months ago... */
  121. ali_expect_release = 0;
  122. /* scan to see whether or not we got the magic character */
  123. for (i = 0; i != len; i++) {
  124. char c;
  125. if(get_user(c, data+i))
  126. return -EFAULT;
  127. if (c == 'V')
  128. ali_expect_release = 42;
  129. }
  130. }
  131. /* someone wrote to us, we should reload the timer */
  132. ali_start();
  133. }
  134. return len;
  135. }
  136. /*
  137. * ali_ioctl - handle watchdog ioctls
  138. * @inode: VFS inode
  139. * @file: VFS file pointer
  140. * @cmd: ioctl number
  141. * @arg: arguments to the ioctl
  142. *
  143. * Handle the watchdog ioctls supported by the ALi driver. Really
  144. * we want an extension to enable irq ack monitoring and the like
  145. */
  146. static int ali_ioctl(struct inode *inode, struct file *file,
  147. unsigned int cmd, unsigned long arg)
  148. {
  149. void __user *argp = (void __user *)arg;
  150. int __user *p = argp;
  151. static struct watchdog_info ident = {
  152. .options = WDIOF_KEEPALIVEPING |
  153. WDIOF_SETTIMEOUT |
  154. WDIOF_MAGICCLOSE,
  155. .firmware_version = 0,
  156. .identity = "ALi M1535 WatchDog Timer",
  157. };
  158. switch (cmd) {
  159. case WDIOC_GETSUPPORT:
  160. return copy_to_user(argp, &ident,
  161. sizeof (ident)) ? -EFAULT : 0;
  162. case WDIOC_GETSTATUS:
  163. case WDIOC_GETBOOTSTATUS:
  164. return put_user(0, p);
  165. case WDIOC_KEEPALIVE:
  166. ali_keepalive();
  167. return 0;
  168. case WDIOC_SETOPTIONS:
  169. {
  170. int new_options, retval = -EINVAL;
  171. if (get_user (new_options, p))
  172. return -EFAULT;
  173. if (new_options & WDIOS_DISABLECARD) {
  174. ali_stop();
  175. retval = 0;
  176. }
  177. if (new_options & WDIOS_ENABLECARD) {
  178. ali_start();
  179. retval = 0;
  180. }
  181. return retval;
  182. }
  183. case WDIOC_SETTIMEOUT:
  184. {
  185. int new_timeout;
  186. if (get_user(new_timeout, p))
  187. return -EFAULT;
  188. if (ali_settimer(new_timeout))
  189. return -EINVAL;
  190. ali_keepalive();
  191. /* Fall */
  192. }
  193. case WDIOC_GETTIMEOUT:
  194. return put_user(timeout, p);
  195. default:
  196. return -ENOTTY;
  197. }
  198. }
  199. /*
  200. * ali_open - handle open of ali watchdog
  201. * @inode: inode from VFS
  202. * @file: file from VFS
  203. *
  204. * Open the ALi watchdog device. Ensure only one person opens it
  205. * at a time. Also start the watchdog running.
  206. */
  207. static int ali_open(struct inode *inode, struct file *file)
  208. {
  209. /* /dev/watchdog can only be opened once */
  210. if (test_and_set_bit(0, &ali_is_open))
  211. return -EBUSY;
  212. /* Activate */
  213. ali_start();
  214. return nonseekable_open(inode, file);
  215. }
  216. /*
  217. * ali_release - close an ALi watchdog
  218. * @inode: inode from VFS
  219. * @file: file from VFS
  220. *
  221. * Close the ALi watchdog device. Actual shutdown of the timer
  222. * only occurs if the magic sequence has been set.
  223. */
  224. static int ali_release(struct inode *inode, struct file *file)
  225. {
  226. /*
  227. * Shut off the timer.
  228. */
  229. if (ali_expect_release == 42) {
  230. ali_stop();
  231. } else {
  232. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  233. ali_keepalive();
  234. }
  235. clear_bit(0, &ali_is_open);
  236. ali_expect_release = 0;
  237. return 0;
  238. }
  239. /*
  240. * ali_notify_sys - System down notifier
  241. *
  242. * Notifier for system down
  243. */
  244. static int ali_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  245. {
  246. if (code==SYS_DOWN || code==SYS_HALT) {
  247. /* Turn the WDT off */
  248. ali_stop();
  249. }
  250. return NOTIFY_DONE;
  251. }
  252. /*
  253. * Data for PCI driver interface
  254. *
  255. * This data only exists for exporting the supported
  256. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  257. * register a pci_driver, because someone else might one day
  258. * want to register another driver on the same PCI id.
  259. */
  260. static struct pci_device_id ali_pci_tbl[] = {
  261. { PCI_VENDOR_ID_AL, 0x1535, PCI_ANY_ID, PCI_ANY_ID,},
  262. { 0, },
  263. };
  264. MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
  265. /*
  266. * ali_find_watchdog - find a 1535 and 7101
  267. *
  268. * Scans the PCI hardware for a 1535 series bridge and matching 7101
  269. * watchdog device. This may be overtight but it is better to be safe
  270. */
  271. static int __init ali_find_watchdog(void)
  272. {
  273. struct pci_dev *pdev;
  274. u32 wdog;
  275. /* Check for a 1535 series bridge */
  276. pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
  277. if(pdev == NULL)
  278. return -ENODEV;
  279. pci_dev_put(pdev);
  280. /* Check for the a 7101 PMU */
  281. pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
  282. if(pdev == NULL)
  283. return -ENODEV;
  284. if(pci_enable_device(pdev)) {
  285. pci_dev_put(pdev);
  286. return -EIO;
  287. }
  288. ali_pci = pdev;
  289. /*
  290. * Initialize the timer bits
  291. */
  292. pci_read_config_dword(pdev, 0xCC, &wdog);
  293. wdog &= ~0x3F; /* Timer bits */
  294. wdog &= ~((1<<27)|(1<<26)|(1<<25)|(1<<24)); /* Issued events */
  295. wdog &= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9)); /* No monitor bits */
  296. pci_write_config_dword(pdev, 0xCC, wdog);
  297. return 0;
  298. }
  299. /*
  300. * Kernel Interfaces
  301. */
  302. static const struct file_operations ali_fops = {
  303. .owner = THIS_MODULE,
  304. .llseek = no_llseek,
  305. .write = ali_write,
  306. .ioctl = ali_ioctl,
  307. .open = ali_open,
  308. .release = ali_release,
  309. };
  310. static struct miscdevice ali_miscdev = {
  311. .minor = WATCHDOG_MINOR,
  312. .name = "watchdog",
  313. .fops = &ali_fops,
  314. };
  315. static struct notifier_block ali_notifier = {
  316. .notifier_call = ali_notify_sys,
  317. };
  318. /*
  319. * watchdog_init - module initialiser
  320. *
  321. * Scan for a suitable watchdog and if so initialize it. Return an error
  322. * if we cannot, the error causes the module to unload
  323. */
  324. static int __init watchdog_init(void)
  325. {
  326. int ret;
  327. spin_lock_init(&ali_lock);
  328. /* Check whether or not the hardware watchdog is there */
  329. if (ali_find_watchdog() != 0) {
  330. return -ENODEV;
  331. }
  332. /* Check that the timeout value is within it's range ; if not reset to the default */
  333. if (timeout < 1 || timeout >= 18000) {
  334. timeout = WATCHDOG_TIMEOUT;
  335. printk(KERN_INFO PFX "timeout value must be 0<timeout<18000, using %d\n",
  336. timeout);
  337. }
  338. /* Calculate the watchdog's timeout */
  339. ali_settimer(timeout);
  340. ret = misc_register(&ali_miscdev);
  341. if (ret != 0) {
  342. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  343. WATCHDOG_MINOR, ret);
  344. goto out;
  345. }
  346. ret = register_reboot_notifier(&ali_notifier);
  347. if (ret != 0) {
  348. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  349. ret);
  350. goto unreg_miscdev;
  351. }
  352. printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  353. timeout, nowayout);
  354. out:
  355. return ret;
  356. unreg_miscdev:
  357. misc_deregister(&ali_miscdev);
  358. goto out;
  359. }
  360. /*
  361. * watchdog_exit - module de-initialiser
  362. *
  363. * Called while unloading a successfully installed watchdog module.
  364. */
  365. static void __exit watchdog_exit(void)
  366. {
  367. /* Stop the timer before we leave */
  368. ali_stop();
  369. /* Deregister */
  370. unregister_reboot_notifier(&ali_notifier);
  371. misc_deregister(&ali_miscdev);
  372. pci_dev_put(ali_pci);
  373. }
  374. module_init(watchdog_init);
  375. module_exit(watchdog_exit);
  376. MODULE_AUTHOR("Alan Cox");
  377. MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
  378. MODULE_LICENSE("GPL");
  379. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);