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