sc1200wdt.c 11 KB

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