pc87413_wdt.c 15 KB

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