sc1200wdt.c 11 KB

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