alim1535_wdt.c 9.7 KB

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