alim1535_wdt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/types.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/ioport.h>
  16. #include <linux/notifier.h>
  17. #include <linux/reboot.h>
  18. #include <linux/init.h>
  19. #include <linux/fs.h>
  20. #include <linux/pci.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/io.h>
  23. #define WATCHDOG_NAME "ALi_M1535"
  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 DEFINE_SPINLOCK(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,
  35. "Watchdog timeout in seconds. (0 < timeout < 18000, default="
  36. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. module_param(nowayout, bool, 0);
  39. MODULE_PARM_DESC(nowayout,
  40. "Watchdog cannot be stopped once started (default="
  41. __MODULE_STRING(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
  99. return -EINVAL;
  100. timeout = t;
  101. return 0;
  102. }
  103. /*
  104. * /dev/watchdog handling
  105. */
  106. /*
  107. * ali_write - writes to ALi watchdog
  108. * @file: file from VFS
  109. * @data: user address of data
  110. * @len: length of data
  111. * @ppos: pointer to the file offset
  112. *
  113. * Handle a write to the ALi watchdog. Writing to the file pings
  114. * the watchdog and resets it. Writing the magic 'V' sequence allows
  115. * the next close to turn off the watchdog.
  116. */
  117. static ssize_t ali_write(struct file *file, const char __user *data,
  118. size_t len, loff_t *ppos)
  119. {
  120. /* See if we got the magic character 'V' and reload the timer */
  121. if (len) {
  122. if (!nowayout) {
  123. size_t i;
  124. /* note: just in case someone wrote the
  125. magic character five months ago... */
  126. ali_expect_release = 0;
  127. /* scan to see whether or not we got
  128. the magic character */
  129. for (i = 0; i != len; i++) {
  130. char c;
  131. if (get_user(c, data + i))
  132. return -EFAULT;
  133. if (c == 'V')
  134. ali_expect_release = 42;
  135. }
  136. }
  137. /* someone wrote to us, we should reload the timer */
  138. ali_start();
  139. }
  140. return len;
  141. }
  142. /*
  143. * ali_ioctl - handle watchdog ioctls
  144. * @file: VFS file pointer
  145. * @cmd: ioctl number
  146. * @arg: arguments to the ioctl
  147. *
  148. * Handle the watchdog ioctls supported by the ALi driver. Really
  149. * we want an extension to enable irq ack monitoring and the like
  150. */
  151. static long ali_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  152. {
  153. void __user *argp = (void __user *)arg;
  154. int __user *p = argp;
  155. static const 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, sizeof(ident)) ? -EFAULT : 0;
  165. case WDIOC_GETSTATUS:
  166. case WDIOC_GETBOOTSTATUS:
  167. return put_user(0, p);
  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_KEEPALIVE:
  184. ali_keepalive();
  185. return 0;
  186. case WDIOC_SETTIMEOUT:
  187. {
  188. int new_timeout;
  189. if (get_user(new_timeout, p))
  190. return -EFAULT;
  191. if (ali_settimer(new_timeout))
  192. return -EINVAL;
  193. ali_keepalive();
  194. /* Fall */
  195. }
  196. case WDIOC_GETTIMEOUT:
  197. return put_user(timeout, p);
  198. default:
  199. return -ENOTTY;
  200. }
  201. }
  202. /*
  203. * ali_open - handle open of ali watchdog
  204. * @inode: inode from VFS
  205. * @file: file from VFS
  206. *
  207. * Open the ALi watchdog device. Ensure only one person opens it
  208. * at a time. Also start the watchdog running.
  209. */
  210. static int ali_open(struct inode *inode, struct file *file)
  211. {
  212. /* /dev/watchdog can only be opened once */
  213. if (test_and_set_bit(0, &ali_is_open))
  214. return -EBUSY;
  215. /* Activate */
  216. ali_start();
  217. return nonseekable_open(inode, file);
  218. }
  219. /*
  220. * ali_release - close an ALi watchdog
  221. * @inode: inode from VFS
  222. * @file: file from VFS
  223. *
  224. * Close the ALi watchdog device. Actual shutdown of the timer
  225. * only occurs if the magic sequence has been set.
  226. */
  227. static int ali_release(struct inode *inode, struct file *file)
  228. {
  229. /*
  230. * Shut off the timer.
  231. */
  232. if (ali_expect_release == 42)
  233. ali_stop();
  234. else {
  235. pr_crit("Unexpected close, not stopping watchdog!\n");
  236. ali_keepalive();
  237. }
  238. clear_bit(0, &ali_is_open);
  239. ali_expect_release = 0;
  240. return 0;
  241. }
  242. /*
  243. * ali_notify_sys - System down notifier
  244. *
  245. * Notifier for system down
  246. */
  247. static int ali_notify_sys(struct notifier_block *this,
  248. unsigned long code, void *unused)
  249. {
  250. if (code == SYS_DOWN || code == SYS_HALT)
  251. ali_stop(); /* Turn the WDT off */
  252. return NOTIFY_DONE;
  253. }
  254. /*
  255. * Data for PCI driver interface
  256. *
  257. * This data only exists for exporting the supported
  258. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  259. * register a pci_driver, because someone else might one day
  260. * want to register another driver on the same PCI id.
  261. */
  262. static const struct pci_device_id ali_pci_tbl[] __used = {
  263. { PCI_VENDOR_ID_AL, 0x1533, PCI_ANY_ID, PCI_ANY_ID,},
  264. { PCI_VENDOR_ID_AL, 0x1535, PCI_ANY_ID, PCI_ANY_ID,},
  265. { 0, },
  266. };
  267. MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
  268. /*
  269. * ali_find_watchdog - find a 1535 and 7101
  270. *
  271. * Scans the PCI hardware for a 1535 series bridge and matching 7101
  272. * watchdog device. This may be overtight but it is better to be safe
  273. */
  274. static int __init ali_find_watchdog(void)
  275. {
  276. struct pci_dev *pdev;
  277. u32 wdog;
  278. /* Check for a 1533/1535 series bridge */
  279. pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
  280. if (pdev == NULL)
  281. pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1533, NULL);
  282. if (pdev == NULL)
  283. return -ENODEV;
  284. pci_dev_put(pdev);
  285. /* Check for the a 7101 PMU */
  286. pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
  287. if (pdev == NULL)
  288. return -ENODEV;
  289. if (pci_enable_device(pdev)) {
  290. pci_dev_put(pdev);
  291. return -EIO;
  292. }
  293. ali_pci = pdev;
  294. /*
  295. * Initialize the timer bits
  296. */
  297. pci_read_config_dword(pdev, 0xCC, &wdog);
  298. /* Timer bits */
  299. wdog &= ~0x3F;
  300. /* Issued events */
  301. wdog &= ~((1 << 27)|(1 << 26)|(1 << 25)|(1 << 24));
  302. /* No monitor bits */
  303. wdog &= ~((1 << 16)|(1 << 13)|(1 << 12)|(1 << 11)|(1 << 10)|(1 << 9));
  304. pci_write_config_dword(pdev, 0xCC, wdog);
  305. return 0;
  306. }
  307. /*
  308. * Kernel Interfaces
  309. */
  310. static const struct file_operations ali_fops = {
  311. .owner = THIS_MODULE,
  312. .llseek = no_llseek,
  313. .write = ali_write,
  314. .unlocked_ioctl = ali_ioctl,
  315. .open = ali_open,
  316. .release = ali_release,
  317. };
  318. static struct miscdevice ali_miscdev = {
  319. .minor = WATCHDOG_MINOR,
  320. .name = "watchdog",
  321. .fops = &ali_fops,
  322. };
  323. static struct notifier_block ali_notifier = {
  324. .notifier_call = ali_notify_sys,
  325. };
  326. /*
  327. * watchdog_init - module initialiser
  328. *
  329. * Scan for a suitable watchdog and if so initialize it. Return an error
  330. * if we cannot, the error causes the module to unload
  331. */
  332. static int __init watchdog_init(void)
  333. {
  334. int ret;
  335. /* Check whether or not the hardware watchdog is there */
  336. if (ali_find_watchdog() != 0)
  337. return -ENODEV;
  338. /* Check that the timeout value is within it's range;
  339. if not reset to the default */
  340. if (timeout < 1 || timeout >= 18000) {
  341. timeout = WATCHDOG_TIMEOUT;
  342. pr_info("timeout value must be 0 < timeout < 18000, using %d\n",
  343. timeout);
  344. }
  345. /* Calculate the watchdog's timeout */
  346. ali_settimer(timeout);
  347. ret = register_reboot_notifier(&ali_notifier);
  348. if (ret != 0) {
  349. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  350. goto out;
  351. }
  352. ret = misc_register(&ali_miscdev);
  353. if (ret != 0) {
  354. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  355. WATCHDOG_MINOR, ret);
  356. goto unreg_reboot;
  357. }
  358. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  359. timeout, nowayout);
  360. out:
  361. return ret;
  362. unreg_reboot:
  363. unregister_reboot_notifier(&ali_notifier);
  364. goto out;
  365. }
  366. /*
  367. * watchdog_exit - module de-initialiser
  368. *
  369. * Called while unloading a successfully installed watchdog module.
  370. */
  371. static void __exit watchdog_exit(void)
  372. {
  373. /* Stop the timer before we leave */
  374. ali_stop();
  375. /* Deregister */
  376. misc_deregister(&ali_miscdev);
  377. unregister_reboot_notifier(&ali_notifier);
  378. pci_dev_put(ali_pci);
  379. }
  380. module_init(watchdog_init);
  381. module_exit(watchdog_exit);
  382. MODULE_AUTHOR("Alan Cox");
  383. MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
  384. MODULE_LICENSE("GPL");