sc1200wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
  4. * (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
  5. * All Rights Reserved.
  6. * Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
  7. *
  8. * The author(s) of this software shall not be held liable for damages
  9. * of any nature resulting due to the use of this software. This
  10. * software is provided AS-IS with no warranties.
  11. *
  12. * Changelog:
  13. * 20020220 Zwane Mwaikambo Code based on datasheet, no hardware.
  14. * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik
  15. * and Alan Cox.
  16. * 20020222 Zwane Mwaikambo Added probing.
  17. * 20020225 Zwane Mwaikambo Added ISAPNP support.
  18. * 20020412 Rob Radez Broke out start/stop functions
  19. * <rob@osinvestor.com> Return proper status instead of
  20. * temperature warning
  21. * Add WDIOC_GETBOOTSTATUS and
  22. * WDIOC_SETOPTIONS ioctls
  23. * Fix CONFIG_WATCHDOG_NOWAYOUT
  24. * 20020530 Joel Becker Add Matt Domsch's nowayout module
  25. * option
  26. * 20030116 Adam Belay Updated to the latest pnp code
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/watchdog.h>
  33. #include <linux/ioport.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/notifier.h>
  36. #include <linux/reboot.h>
  37. #include <linux/init.h>
  38. #include <linux/pnp.h>
  39. #include <linux/fs.h>
  40. #include <linux/semaphore.h>
  41. #include <linux/io.h>
  42. #include <linux/uaccess.h>
  43. #define SC1200_MODULE_VER "build 20020303"
  44. #define SC1200_MODULE_NAME "sc1200wdt"
  45. #define MAX_TIMEOUT 255 /* 255 minutes */
  46. #define PMIR (io) /* Power Management Index Register */
  47. #define PMDR (io+1) /* Power Management Data Register */
  48. /* Data Register indexes */
  49. #define FER1 0x00 /* Function enable register 1 */
  50. #define FER2 0x01 /* Function enable register 2 */
  51. #define PMC1 0x02 /* Power Management Ctrl 1 */
  52. #define PMC2 0x03 /* Power Management Ctrl 2 */
  53. #define PMC3 0x04 /* Power Management Ctrl 3 */
  54. #define WDTO 0x05 /* Watchdog timeout register */
  55. #define WDCF 0x06 /* Watchdog config register */
  56. #define WDST 0x07 /* Watchdog status register */
  57. /* WDCF bitfields - which devices assert WDO */
  58. #define KBC_IRQ 0x01 /* Keyboard Controller */
  59. #define MSE_IRQ 0x02 /* Mouse */
  60. #define UART1_IRQ 0x03 /* Serial0 */
  61. #define UART2_IRQ 0x04 /* Serial1 */
  62. /* 5 -7 are reserved */
  63. static int timeout = 1;
  64. static int io = -1;
  65. static int io_len = 2; /* for non plug and play */
  66. static unsigned long open_flag;
  67. static char expect_close;
  68. static DEFINE_SPINLOCK(sc1200wdt_lock); /* io port access serialisation */
  69. #if defined CONFIG_PNP
  70. static int isapnp = 1;
  71. static struct pnp_dev *wdt_dev;
  72. module_param(isapnp, int, 0);
  73. MODULE_PARM_DESC(isapnp,
  74. "When set to 0 driver ISA PnP support will be disabled");
  75. #endif
  76. module_param_hw(io, int, ioport, 0);
  77. MODULE_PARM_DESC(io, "io port");
  78. module_param(timeout, int, 0);
  79. MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
  80. static bool nowayout = WATCHDOG_NOWAYOUT;
  81. module_param(nowayout, bool, 0);
  82. MODULE_PARM_DESC(nowayout,
  83. "Watchdog cannot be stopped once started (default="
  84. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  85. /* Read from Data Register */
  86. static inline void __sc1200wdt_read_data(unsigned char index,
  87. unsigned char *data)
  88. {
  89. outb_p(index, PMIR);
  90. *data = inb(PMDR);
  91. }
  92. static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
  93. {
  94. spin_lock(&sc1200wdt_lock);
  95. __sc1200wdt_read_data(index, data);
  96. spin_unlock(&sc1200wdt_lock);
  97. }
  98. /* Write to Data Register */
  99. static inline void __sc1200wdt_write_data(unsigned char index,
  100. unsigned char data)
  101. {
  102. outb_p(index, PMIR);
  103. outb(data, PMDR);
  104. }
  105. static inline void sc1200wdt_write_data(unsigned char index,
  106. unsigned char data)
  107. {
  108. spin_lock(&sc1200wdt_lock);
  109. __sc1200wdt_write_data(index, data);
  110. spin_unlock(&sc1200wdt_lock);
  111. }
  112. static void sc1200wdt_start(void)
  113. {
  114. unsigned char reg;
  115. spin_lock(&sc1200wdt_lock);
  116. __sc1200wdt_read_data(WDCF, &reg);
  117. /* assert WDO when any of the following interrupts are triggered too */
  118. reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
  119. __sc1200wdt_write_data(WDCF, reg);
  120. /* set the timeout and get the ball rolling */
  121. __sc1200wdt_write_data(WDTO, timeout);
  122. spin_unlock(&sc1200wdt_lock);
  123. }
  124. static void sc1200wdt_stop(void)
  125. {
  126. sc1200wdt_write_data(WDTO, 0);
  127. }
  128. /* This returns the status of the WDO signal, inactive high. */
  129. static inline int sc1200wdt_status(void)
  130. {
  131. unsigned char ret;
  132. sc1200wdt_read_data(WDST, &ret);
  133. /* If the bit is inactive, the watchdog is enabled, so return
  134. * KEEPALIVEPING which is a bit of a kludge because there's nothing
  135. * else for enabled/disabled status
  136. */
  137. return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
  138. }
  139. static int sc1200wdt_open(struct inode *inode, struct file *file)
  140. {
  141. /* allow one at a time */
  142. if (test_and_set_bit(0, &open_flag))
  143. return -EBUSY;
  144. if (timeout > MAX_TIMEOUT)
  145. timeout = MAX_TIMEOUT;
  146. sc1200wdt_start();
  147. pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
  148. return stream_open(inode, file);
  149. }
  150. static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
  151. unsigned long arg)
  152. {
  153. int new_timeout;
  154. void __user *argp = (void __user *)arg;
  155. int __user *p = argp;
  156. static const struct watchdog_info ident = {
  157. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
  158. WDIOF_MAGICCLOSE,
  159. .firmware_version = 0,
  160. .identity = "PC87307/PC97307",
  161. };
  162. switch (cmd) {
  163. case WDIOC_GETSUPPORT:
  164. if (copy_to_user(argp, &ident, sizeof(ident)))
  165. return -EFAULT;
  166. return 0;
  167. case WDIOC_GETSTATUS:
  168. return put_user(sc1200wdt_status(), p);
  169. case WDIOC_GETBOOTSTATUS:
  170. return put_user(0, p);
  171. case WDIOC_SETOPTIONS:
  172. {
  173. int options, retval = -EINVAL;
  174. if (get_user(options, p))
  175. return -EFAULT;
  176. if (options & WDIOS_DISABLECARD) {
  177. sc1200wdt_stop();
  178. retval = 0;
  179. }
  180. if (options & WDIOS_ENABLECARD) {
  181. sc1200wdt_start();
  182. retval = 0;
  183. }
  184. return retval;
  185. }
  186. case WDIOC_KEEPALIVE:
  187. sc1200wdt_write_data(WDTO, timeout);
  188. return 0;
  189. case WDIOC_SETTIMEOUT:
  190. if (get_user(new_timeout, p))
  191. return -EFAULT;
  192. /* the API states this is given in secs */
  193. new_timeout /= 60;
  194. if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
  195. return -EINVAL;
  196. timeout = new_timeout;
  197. sc1200wdt_write_data(WDTO, timeout);
  198. fallthrough; /* and return the new timeout */
  199. case WDIOC_GETTIMEOUT:
  200. return put_user(timeout * 60, p);
  201. default:
  202. return -ENOTTY;
  203. }
  204. }
  205. static int sc1200wdt_release(struct inode *inode, struct file *file)
  206. {
  207. if (expect_close == 42) {
  208. sc1200wdt_stop();
  209. pr_info("Watchdog disabled\n");
  210. } else {
  211. sc1200wdt_write_data(WDTO, timeout);
  212. pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
  213. }
  214. clear_bit(0, &open_flag);
  215. expect_close = 0;
  216. return 0;
  217. }
  218. static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
  219. size_t len, loff_t *ppos)
  220. {
  221. if (len) {
  222. if (!nowayout) {
  223. size_t i;
  224. expect_close = 0;
  225. for (i = 0; i != len; i++) {
  226. char c;
  227. if (get_user(c, data + i))
  228. return -EFAULT;
  229. if (c == 'V')
  230. expect_close = 42;
  231. }
  232. }
  233. sc1200wdt_write_data(WDTO, timeout);
  234. return len;
  235. }
  236. return 0;
  237. }
  238. static int sc1200wdt_notify_sys(struct notifier_block *this,
  239. unsigned long code, void *unused)
  240. {
  241. if (code == SYS_DOWN || code == SYS_HALT)
  242. sc1200wdt_stop();
  243. return NOTIFY_DONE;
  244. }
  245. static struct notifier_block sc1200wdt_notifier = {
  246. .notifier_call = sc1200wdt_notify_sys,
  247. };
  248. static const struct file_operations sc1200wdt_fops = {
  249. .owner = THIS_MODULE,
  250. .llseek = no_llseek,
  251. .write = sc1200wdt_write,
  252. .unlocked_ioctl = sc1200wdt_ioctl,
  253. .compat_ioctl = compat_ptr_ioctl,
  254. .open = sc1200wdt_open,
  255. .release = sc1200wdt_release,
  256. };
  257. static struct miscdevice sc1200wdt_miscdev = {
  258. .minor = WATCHDOG_MINOR,
  259. .name = "watchdog",
  260. .fops = &sc1200wdt_fops,
  261. };
  262. static int __init sc1200wdt_probe(void)
  263. {
  264. /* The probe works by reading the PMC3 register's default value of 0x0e
  265. * there is one caveat, if the device disables the parallel port or any
  266. * of the UARTs we won't be able to detect it.
  267. * NB. This could be done with accuracy by reading the SID registers,
  268. * but we don't have access to those io regions.
  269. */
  270. unsigned char reg;
  271. sc1200wdt_read_data(PMC3, &reg);
  272. reg &= 0x0f; /* we don't want the UART busy bits */
  273. return (reg == 0x0e) ? 0 : -ENODEV;
  274. }
  275. #if defined CONFIG_PNP
  276. static const struct pnp_device_id scl200wdt_pnp_devices[] = {
  277. /* National Semiconductor PC87307/PC97307 watchdog component */
  278. {.id = "NSC0800", .driver_data = 0},
  279. {.id = ""},
  280. };
  281. static int scl200wdt_pnp_probe(struct pnp_dev *dev,
  282. const struct pnp_device_id *dev_id)
  283. {
  284. /* this driver only supports one card at a time */
  285. if (wdt_dev || !isapnp)
  286. return -EBUSY;
  287. wdt_dev = dev;
  288. io = pnp_port_start(wdt_dev, 0);
  289. io_len = pnp_port_len(wdt_dev, 0);
  290. if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
  291. pr_err("Unable to register IO port %#x\n", io);
  292. return -EBUSY;
  293. }
  294. pr_info("PnP device found at io port %#x/%d\n", io, io_len);
  295. return 0;
  296. }
  297. static void scl200wdt_pnp_remove(struct pnp_dev *dev)
  298. {
  299. if (wdt_dev) {
  300. release_region(io, io_len);
  301. wdt_dev = NULL;
  302. }
  303. }
  304. static struct pnp_driver scl200wdt_pnp_driver = {
  305. .name = "scl200wdt",
  306. .id_table = scl200wdt_pnp_devices,
  307. .probe = scl200wdt_pnp_probe,
  308. .remove = scl200wdt_pnp_remove,
  309. };
  310. #endif /* CONFIG_PNP */
  311. static int __init sc1200wdt_init(void)
  312. {
  313. int ret;
  314. pr_info("%s\n", SC1200_MODULE_VER);
  315. #if defined CONFIG_PNP
  316. if (isapnp) {
  317. ret = pnp_register_driver(&scl200wdt_pnp_driver);
  318. if (ret)
  319. goto out_clean;
  320. }
  321. #endif
  322. if (io == -1) {
  323. pr_err("io parameter must be specified\n");
  324. ret = -EINVAL;
  325. goto out_pnp;
  326. }
  327. #if defined CONFIG_PNP
  328. /* now that the user has specified an IO port and we haven't detected
  329. * any devices, disable pnp support */
  330. if (isapnp)
  331. pnp_unregister_driver(&scl200wdt_pnp_driver);
  332. isapnp = 0;
  333. #endif
  334. if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
  335. pr_err("Unable to register IO port %#x\n", io);
  336. ret = -EBUSY;
  337. goto out_pnp;
  338. }
  339. ret = sc1200wdt_probe();
  340. if (ret)
  341. goto out_io;
  342. ret = register_reboot_notifier(&sc1200wdt_notifier);
  343. if (ret) {
  344. pr_err("Unable to register reboot notifier err = %d\n", ret);
  345. goto out_io;
  346. }
  347. ret = misc_register(&sc1200wdt_miscdev);
  348. if (ret) {
  349. pr_err("Unable to register miscdev on minor %d\n",
  350. WATCHDOG_MINOR);
  351. goto out_rbt;
  352. }
  353. /* ret = 0 */
  354. out_clean:
  355. return ret;
  356. out_rbt:
  357. unregister_reboot_notifier(&sc1200wdt_notifier);
  358. out_io:
  359. release_region(io, io_len);
  360. out_pnp:
  361. #if defined CONFIG_PNP
  362. if (isapnp)
  363. pnp_unregister_driver(&scl200wdt_pnp_driver);
  364. #endif
  365. goto out_clean;
  366. }
  367. static void __exit sc1200wdt_exit(void)
  368. {
  369. misc_deregister(&sc1200wdt_miscdev);
  370. unregister_reboot_notifier(&sc1200wdt_notifier);
  371. #if defined CONFIG_PNP
  372. if (isapnp)
  373. pnp_unregister_driver(&scl200wdt_pnp_driver);
  374. else
  375. #endif
  376. release_region(io, io_len);
  377. }
  378. module_init(sc1200wdt_init);
  379. module_exit(sc1200wdt_exit);
  380. MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
  381. MODULE_DESCRIPTION(
  382. "Driver for National Semiconductor PC87307/PC97307 watchdog component");
  383. MODULE_LICENSE("GPL");