sc1200wdt.c 11 KB

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