pc87413_wdt.c 14 KB

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