pc87413_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
  3. *
  4. * This code is based on wdt.c with original copyright.
  5. *
  6. * (C) Copyright 2006 Sven Anders, <anders@anduras.de>
  7. * and Marcus Junker, <junker@anduras.de>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Neither Sven Anders, Marcus Junker nor ANDURAS AG
  15. * admit liability nor provide warranty for any of this software.
  16. * This material is provided "AS-IS" and at no charge.
  17. *
  18. * Release 1.1
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/ioport.h>
  25. #include <linux/delay.h>
  26. #include <linux/notifier.h>
  27. #include <linux/fs.h>
  28. #include <linux/reboot.h>
  29. #include <linux/init.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/system.h>
  35. /* #define DEBUG 1 */
  36. #define DEFAULT_TIMEOUT 1 /* 1 minute */
  37. #define MAX_TIMEOUT 255
  38. #define VERSION "1.1"
  39. #define MODNAME "pc87413 WDT"
  40. #define PFX MODNAME ": "
  41. #define DPFX MODNAME " - DEBUG: "
  42. #define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
  43. #define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
  44. #define SWC_LDN 0x04
  45. #define SIOCFG2 0x22 /* Serial IO register */
  46. #define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
  47. #define WDTO 0x11 /* Watchdog timeout register */
  48. #define WDCFG 0x12 /* Watchdog config register */
  49. static int io = 0x2E; /* Address used on Portwell Boards */
  50. static int timeout = DEFAULT_TIMEOUT; /* timeout value */
  51. static unsigned long timer_enabled; /* is the timer enabled? */
  52. static char expect_close; /* is the close expected? */
  53. static DEFINE_SPINLOCK(io_lock); /* to guard us from io races */
  54. static int nowayout = WATCHDOG_NOWAYOUT;
  55. /* -- Low level function ----------------------------------------*/
  56. /* Select pins for Watchdog output */
  57. static inline void pc87413_select_wdt_out(void)
  58. {
  59. unsigned int cr_data = 0;
  60. /* Step 1: Select multiple pin,pin55,as WDT output */
  61. outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
  62. cr_data = inb(WDT_DATA_IO_PORT);
  63. cr_data |= 0x80; /* Set Bit7 to 1*/
  64. outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
  65. outb_p(cr_data, WDT_DATA_IO_PORT);
  66. #ifdef DEBUG
  67. printk(KERN_INFO DPFX
  68. "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
  69. cr_data);
  70. #endif
  71. }
  72. /* Enable SWC functions */
  73. static inline void pc87413_enable_swc(void)
  74. {
  75. unsigned int cr_data = 0;
  76. /* Step 2: Enable SWC functions */
  77. outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
  78. outb_p(SWC_LDN, WDT_DATA_IO_PORT);
  79. outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
  80. cr_data = inb(WDT_DATA_IO_PORT);
  81. cr_data |= 0x01; /* Set Bit0 to 1 */
  82. outb_p(0x30, WDT_INDEX_IO_PORT);
  83. outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
  84. #ifdef DEBUG
  85. printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
  86. #endif
  87. }
  88. /* Read SWC I/O base address */
  89. static inline unsigned int pc87413_get_swc_base(void)
  90. {
  91. unsigned int swc_base_addr = 0;
  92. unsigned char addr_l, addr_h = 0;
  93. /* Step 3: Read SWC I/O Base Address */
  94. outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
  95. addr_h = inb(WDT_DATA_IO_PORT);
  96. outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
  97. addr_l = inb(WDT_DATA_IO_PORT);
  98. swc_base_addr = (addr_h << 8) + addr_l;
  99. #ifdef DEBUG
  100. printk(KERN_INFO DPFX
  101. "Read SWC I/O Base Address: low %d, high %d, res %d\n",
  102. addr_l, addr_h, swc_base_addr);
  103. #endif
  104. return swc_base_addr;
  105. }
  106. /* Select Bank 3 of SWC */
  107. static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
  108. {
  109. /* Step 4: Select Bank3 of SWC */
  110. outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
  111. #ifdef DEBUG
  112. printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
  113. #endif
  114. }
  115. /* Set watchdog timeout to x minutes */
  116. static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
  117. char pc87413_time)
  118. {
  119. /* Step 5: Programm WDTO, Twd. */
  120. outb_p(pc87413_time, swc_base_addr + WDTO);
  121. #ifdef DEBUG
  122. printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
  123. #endif
  124. }
  125. /* Enable WDEN */
  126. static inline void pc87413_enable_wden(unsigned int swc_base_addr)
  127. {
  128. /* Step 6: Enable WDEN */
  129. outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
  130. #ifdef DEBUG
  131. printk(KERN_INFO DPFX "Enable WDEN\n");
  132. #endif
  133. }
  134. /* Enable SW_WD_TREN */
  135. static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
  136. {
  137. /* Enable SW_WD_TREN */
  138. outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
  139. #ifdef DEBUG
  140. printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
  141. #endif
  142. }
  143. /* Disable SW_WD_TREN */
  144. static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
  145. {
  146. /* Disable SW_WD_TREN */
  147. outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
  148. #ifdef DEBUG
  149. printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
  150. #endif
  151. }
  152. /* Enable SW_WD_TRG */
  153. static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
  154. {
  155. /* Enable SW_WD_TRG */
  156. outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
  157. #ifdef DEBUG
  158. printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
  159. #endif
  160. }
  161. /* Disable SW_WD_TRG */
  162. static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
  163. {
  164. /* Disable SW_WD_TRG */
  165. outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
  166. #ifdef DEBUG
  167. printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
  168. #endif
  169. }
  170. /* -- Higher level functions ------------------------------------*/
  171. /* Enable the watchdog */
  172. static void pc87413_enable(void)
  173. {
  174. unsigned int swc_base_addr;
  175. spin_lock(&io_lock);
  176. pc87413_select_wdt_out();
  177. pc87413_enable_swc();
  178. swc_base_addr = pc87413_get_swc_base();
  179. pc87413_swc_bank3(swc_base_addr);
  180. pc87413_programm_wdto(swc_base_addr, timeout);
  181. pc87413_enable_wden(swc_base_addr);
  182. pc87413_enable_sw_wd_tren(swc_base_addr);
  183. pc87413_enable_sw_wd_trg(swc_base_addr);
  184. spin_unlock(&io_lock);
  185. }
  186. /* Disable the watchdog */
  187. static void pc87413_disable(void)
  188. {
  189. unsigned int swc_base_addr;
  190. spin_lock(&io_lock);
  191. pc87413_select_wdt_out();
  192. pc87413_enable_swc();
  193. swc_base_addr = pc87413_get_swc_base();
  194. pc87413_swc_bank3(swc_base_addr);
  195. pc87413_disable_sw_wd_tren(swc_base_addr);
  196. pc87413_disable_sw_wd_trg(swc_base_addr);
  197. pc87413_programm_wdto(swc_base_addr, 0);
  198. spin_unlock(&io_lock);
  199. }
  200. /* Refresh the watchdog */
  201. static void pc87413_refresh(void)
  202. {
  203. unsigned int swc_base_addr;
  204. spin_lock(&io_lock);
  205. pc87413_select_wdt_out();
  206. pc87413_enable_swc();
  207. swc_base_addr = pc87413_get_swc_base();
  208. pc87413_swc_bank3(swc_base_addr);
  209. pc87413_disable_sw_wd_tren(swc_base_addr);
  210. pc87413_disable_sw_wd_trg(swc_base_addr);
  211. pc87413_programm_wdto(swc_base_addr, timeout);
  212. pc87413_enable_wden(swc_base_addr);
  213. pc87413_enable_sw_wd_tren(swc_base_addr);
  214. pc87413_enable_sw_wd_trg(swc_base_addr);
  215. spin_unlock(&io_lock);
  216. }
  217. /* -- File operations -------------------------------------------*/
  218. /**
  219. * pc87413_open:
  220. * @inode: inode of device
  221. * @file: file handle to device
  222. *
  223. */
  224. static int pc87413_open(struct inode *inode, struct file *file)
  225. {
  226. /* /dev/watchdog can only be opened once */
  227. if (test_and_set_bit(0, &timer_enabled))
  228. return -EBUSY;
  229. if (nowayout)
  230. __module_get(THIS_MODULE);
  231. /* Reload and activate timer */
  232. pc87413_refresh();
  233. printk(KERN_INFO MODNAME
  234. "Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
  235. return nonseekable_open(inode, file);
  236. }
  237. /**
  238. * pc87413_release:
  239. * @inode: inode to board
  240. * @file: file handle to board
  241. *
  242. * The watchdog has a configurable API. There is a religious dispute
  243. * between people who want their watchdog to be able to shut down and
  244. * those who want to be sure if the watchdog manager dies the machine
  245. * reboots. In the former case we disable the counters, in the latter
  246. * case you have to open it again very soon.
  247. */
  248. static int pc87413_release(struct inode *inode, struct file *file)
  249. {
  250. /* Shut off the timer. */
  251. if (expect_close == 42) {
  252. pc87413_disable();
  253. printk(KERN_INFO MODNAME
  254. "Watchdog disabled, sleeping again...\n");
  255. } else {
  256. printk(KERN_CRIT MODNAME
  257. "Unexpected close, not stopping watchdog!\n");
  258. pc87413_refresh();
  259. }
  260. clear_bit(0, &timer_enabled);
  261. expect_close = 0;
  262. return 0;
  263. }
  264. /**
  265. * pc87413_status:
  266. *
  267. * return, if the watchdog is enabled (timeout is set...)
  268. */
  269. static int pc87413_status(void)
  270. {
  271. return 0; /* currently not supported */
  272. }
  273. /**
  274. * pc87413_write:
  275. * @file: file handle to the watchdog
  276. * @data: data buffer to write
  277. * @len: length in bytes
  278. * @ppos: pointer to the position to write. No seeks allowed
  279. *
  280. * A write to a watchdog device is defined as a keepalive signal. Any
  281. * write of data will do, as we we don't define content meaning.
  282. */
  283. static ssize_t pc87413_write(struct file *file, const char __user *data,
  284. size_t len, loff_t *ppos)
  285. {
  286. /* See if we got the magic character 'V' and reload the timer */
  287. if (len) {
  288. if (!nowayout) {
  289. size_t i;
  290. /* reset expect flag */
  291. expect_close = 0;
  292. /* scan to see whether or not we got the
  293. magic character */
  294. for (i = 0; i != len; i++) {
  295. char c;
  296. if (get_user(c, data + i))
  297. return -EFAULT;
  298. if (c == 'V')
  299. expect_close = 42;
  300. }
  301. }
  302. /* someone wrote to us, we should reload the timer */
  303. pc87413_refresh();
  304. }
  305. return len;
  306. }
  307. /**
  308. * pc87413_ioctl:
  309. * @file: file handle to the device
  310. * @cmd: watchdog command
  311. * @arg: argument pointer
  312. *
  313. * The watchdog API defines a common set of functions for all watchdogs
  314. * according to their available features. We only actually usefully support
  315. * querying capabilities and current status.
  316. */
  317. static long pc87413_ioctl(struct file *file, unsigned int cmd,
  318. unsigned long arg)
  319. {
  320. int new_timeout;
  321. union {
  322. struct watchdog_info __user *ident;
  323. int __user *i;
  324. } uarg;
  325. static struct watchdog_info ident = {
  326. .options = WDIOF_KEEPALIVEPING |
  327. WDIOF_SETTIMEOUT |
  328. WDIOF_MAGICCLOSE,
  329. .firmware_version = 1,
  330. .identity = "PC87413(HF/F) watchdog",
  331. };
  332. uarg.i = (int __user *)arg;
  333. switch (cmd) {
  334. case WDIOC_GETSUPPORT:
  335. return copy_to_user(uarg.ident, &ident,
  336. sizeof(ident)) ? -EFAULT : 0;
  337. case WDIOC_GETSTATUS:
  338. return put_user(pc87413_status(), uarg.i);
  339. case WDIOC_GETBOOTSTATUS:
  340. return put_user(0, uarg.i);
  341. case WDIOC_SETOPTIONS:
  342. {
  343. int options, retval = -EINVAL;
  344. if (get_user(options, uarg.i))
  345. return -EFAULT;
  346. if (options & WDIOS_DISABLECARD) {
  347. pc87413_disable();
  348. retval = 0;
  349. }
  350. if (options & WDIOS_ENABLECARD) {
  351. pc87413_enable();
  352. retval = 0;
  353. }
  354. return retval;
  355. }
  356. case WDIOC_KEEPALIVE:
  357. pc87413_refresh();
  358. #ifdef DEBUG
  359. printk(KERN_INFO DPFX "keepalive\n");
  360. #endif
  361. return 0;
  362. case WDIOC_SETTIMEOUT:
  363. if (get_user(new_timeout, uarg.i))
  364. return -EFAULT;
  365. /* the API states this is given in secs */
  366. new_timeout /= 60;
  367. if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
  368. return -EINVAL;
  369. timeout = new_timeout;
  370. pc87413_refresh();
  371. /* fall through and return the new timeout... */
  372. case WDIOC_GETTIMEOUT:
  373. new_timeout = timeout * 60;
  374. return put_user(new_timeout, uarg.i);
  375. default:
  376. return -ENOTTY;
  377. }
  378. }
  379. /* -- Notifier funtions -----------------------------------------*/
  380. /**
  381. * notify_sys:
  382. * @this: our notifier block
  383. * @code: the event being reported
  384. * @unused: unused
  385. *
  386. * Our notifier is called on system shutdowns. We want to turn the card
  387. * off at reboot otherwise the machine will reboot again during memory
  388. * test or worse yet during the following fsck. This would suck, in fact
  389. * trust me - if it happens it does suck.
  390. */
  391. static int pc87413_notify_sys(struct notifier_block *this,
  392. unsigned long code,
  393. void *unused)
  394. {
  395. if (code == SYS_DOWN || code == SYS_HALT)
  396. /* Turn the card off */
  397. pc87413_disable();
  398. return NOTIFY_DONE;
  399. }
  400. /* -- Module's structures ---------------------------------------*/
  401. static const struct file_operations pc87413_fops = {
  402. .owner = THIS_MODULE,
  403. .llseek = no_llseek,
  404. .write = pc87413_write,
  405. .unlocked_ioctl = pc87413_ioctl,
  406. .open = pc87413_open,
  407. .release = pc87413_release,
  408. };
  409. static struct notifier_block pc87413_notifier = {
  410. .notifier_call = pc87413_notify_sys,
  411. };
  412. static struct miscdevice pc87413_miscdev = {
  413. .minor = WATCHDOG_MINOR,
  414. .name = "watchdog",
  415. .fops = &pc87413_fops,
  416. };
  417. /* -- Module init functions -------------------------------------*/
  418. /**
  419. * pc87413_init: module's "constructor"
  420. *
  421. * Set up the WDT watchdog board. All we have to do is grab the
  422. * resources we require and bitch if anyone beat us to them.
  423. * The open() function will actually kick the board off.
  424. */
  425. static int __init pc87413_init(void)
  426. {
  427. int ret;
  428. printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
  429. WDT_INDEX_IO_PORT);
  430. /* request_region(io, 2, "pc87413"); */
  431. ret = register_reboot_notifier(&pc87413_notifier);
  432. if (ret != 0) {
  433. printk(KERN_ERR PFX
  434. "cannot register reboot notifier (err=%d)\n", ret);
  435. }
  436. ret = misc_register(&pc87413_miscdev);
  437. if (ret != 0) {
  438. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  439. WATCHDOG_MINOR, ret);
  440. unregister_reboot_notifier(&pc87413_notifier);
  441. return ret;
  442. }
  443. printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
  444. pc87413_enable();
  445. return 0;
  446. }
  447. /**
  448. * pc87413_exit: module's "destructor"
  449. *
  450. * Unload the watchdog. You cannot do this with any file handles open.
  451. * If your watchdog is set to continue ticking on close and you unload
  452. * it, well it keeps ticking. We won't get the interrupt but the board
  453. * will not touch PC memory so all is fine. You just have to load a new
  454. * module in 60 seconds or reboot.
  455. */
  456. static void __exit pc87413_exit(void)
  457. {
  458. /* Stop the timer before we leave */
  459. if (!nowayout) {
  460. pc87413_disable();
  461. printk(KERN_INFO MODNAME "Watchdog disabled.\n");
  462. }
  463. misc_deregister(&pc87413_miscdev);
  464. unregister_reboot_notifier(&pc87413_notifier);
  465. /* release_region(io, 2); */
  466. printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
  467. }
  468. module_init(pc87413_init);
  469. module_exit(pc87413_exit);
  470. MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
  471. MODULE_DESCRIPTION("PC87413 WDT driver");
  472. MODULE_LICENSE("GPL");
  473. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  474. module_param(io, int, 0);
  475. MODULE_PARM_DESC(io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
  476. module_param(timeout, int, 0);
  477. MODULE_PARM_DESC(timeout,
  478. "Watchdog timeout in minutes (default="
  479. __MODULE_STRING(timeout) ").");
  480. module_param(nowayout, int, 0);
  481. MODULE_PARM_DESC(nowayout,
  482. "Watchdog cannot be stopped once started (default="
  483. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");