sc1200wdt.c 11 KB

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