sc1200wdt.c 11 KB

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