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