alim1535_wdt.c 9.8 KB

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