sc1200wdt.c 11 KB

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