pc87413_wdt.c 15 KB

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