sch311x_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * sch311x_wdt.c - Driver for the SCH311x Super-I/O chips
  3. * integrated watchdog.
  4. *
  5. * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
  13. * provide warranty for any of this software. This material is
  14. * provided "AS-IS" and at no charge.
  15. */
  16. /*
  17. * Includes, defines, variables, module parameters, ...
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. /* Includes */
  21. #include <linux/module.h> /* For module specific items */
  22. #include <linux/moduleparam.h> /* For new moduleparam's */
  23. #include <linux/types.h> /* For standard types (like size_t) */
  24. #include <linux/errno.h> /* For the -ENODEV/... values */
  25. #include <linux/kernel.h> /* For printk/... */
  26. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
  27. (WATCHDOG_MINOR) */
  28. #include <linux/watchdog.h> /* For the watchdog specific items */
  29. #include <linux/init.h> /* For __init/__exit/... */
  30. #include <linux/fs.h> /* For file operations */
  31. #include <linux/platform_device.h> /* For platform_driver framework */
  32. #include <linux/ioport.h> /* For io-port access */
  33. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  34. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  35. #include <linux/io.h> /* For inb/outb/... */
  36. /* Module and version information */
  37. #define DRV_NAME "sch311x_wdt"
  38. /* Runtime registers */
  39. #define GP60 0x47
  40. #define WDT_TIME_OUT 0x65
  41. #define WDT_VAL 0x66
  42. #define WDT_CFG 0x67
  43. #define WDT_CTRL 0x68
  44. /* internal variables */
  45. static unsigned long sch311x_wdt_is_open;
  46. static char sch311x_wdt_expect_close;
  47. static struct platform_device *sch311x_wdt_pdev;
  48. static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
  49. static struct { /* The devices private data */
  50. /* the Runtime Register base address */
  51. unsigned short runtime_reg;
  52. /* The card's boot status */
  53. int boot_status;
  54. /* the lock for io operations */
  55. spinlock_t io_lock;
  56. } sch311x_wdt_data;
  57. /* Module load parameters */
  58. static unsigned short force_id;
  59. module_param(force_id, ushort, 0);
  60. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  61. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  62. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  63. module_param(timeout, int, 0);
  64. MODULE_PARM_DESC(timeout,
  65. "Watchdog timeout in seconds. 1<= timeout <=15300, default="
  66. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  67. static bool nowayout = WATCHDOG_NOWAYOUT;
  68. module_param(nowayout, bool, 0);
  69. MODULE_PARM_DESC(nowayout,
  70. "Watchdog cannot be stopped once started (default="
  71. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  72. /*
  73. * Super-IO functions
  74. */
  75. static inline void sch311x_sio_enter(int sio_config_port)
  76. {
  77. outb(0x55, sio_config_port);
  78. }
  79. static inline void sch311x_sio_exit(int sio_config_port)
  80. {
  81. outb(0xaa, sio_config_port);
  82. }
  83. static inline int sch311x_sio_inb(int sio_config_port, int reg)
  84. {
  85. outb(reg, sio_config_port);
  86. return inb(sio_config_port + 1);
  87. }
  88. static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
  89. {
  90. outb(reg, sio_config_port);
  91. outb(val, sio_config_port + 1);
  92. }
  93. /*
  94. * Watchdog Operations
  95. */
  96. static void sch311x_wdt_set_timeout(int t)
  97. {
  98. unsigned char timeout_unit = 0x80;
  99. /* When new timeout is bigger then 255 seconds, we will use minutes */
  100. if (t > 255) {
  101. timeout_unit = 0;
  102. t /= 60;
  103. }
  104. /* -- Watchdog Timeout --
  105. * Bit 0-6 (Reserved)
  106. * Bit 7 WDT Time-out Value Units Select
  107. * (0 = Minutes, 1 = Seconds)
  108. */
  109. outb(timeout_unit, sch311x_wdt_data.runtime_reg + WDT_TIME_OUT);
  110. /* -- Watchdog Timer Time-out Value --
  111. * Bit 0-7 Binary coded units (0=Disabled, 1..255)
  112. */
  113. outb(t, sch311x_wdt_data.runtime_reg + WDT_VAL);
  114. }
  115. static void sch311x_wdt_start(void)
  116. {
  117. unsigned char t;
  118. spin_lock(&sch311x_wdt_data.io_lock);
  119. /* set watchdog's timeout */
  120. sch311x_wdt_set_timeout(timeout);
  121. /* enable the watchdog */
  122. /* -- General Purpose I/O Bit 6.0 --
  123. * Bit 0, In/Out: 0 = Output, 1 = Input
  124. * Bit 1, Polarity: 0 = No Invert, 1 = Invert
  125. * Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
  126. * 10 = Either Edge Triggered Intr.4
  127. * Bit 4-6 (Reserved)
  128. * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
  129. */
  130. t = inb(sch311x_wdt_data.runtime_reg + GP60);
  131. outb((t & ~0x0d) | 0x0c, sch311x_wdt_data.runtime_reg + GP60);
  132. spin_unlock(&sch311x_wdt_data.io_lock);
  133. }
  134. static void sch311x_wdt_stop(void)
  135. {
  136. unsigned char t;
  137. spin_lock(&sch311x_wdt_data.io_lock);
  138. /* stop the watchdog */
  139. t = inb(sch311x_wdt_data.runtime_reg + GP60);
  140. outb((t & ~0x0d) | 0x01, sch311x_wdt_data.runtime_reg + GP60);
  141. /* disable timeout by setting it to 0 */
  142. sch311x_wdt_set_timeout(0);
  143. spin_unlock(&sch311x_wdt_data.io_lock);
  144. }
  145. static void sch311x_wdt_keepalive(void)
  146. {
  147. spin_lock(&sch311x_wdt_data.io_lock);
  148. sch311x_wdt_set_timeout(timeout);
  149. spin_unlock(&sch311x_wdt_data.io_lock);
  150. }
  151. static int sch311x_wdt_set_heartbeat(int t)
  152. {
  153. if (t < 1 || t > (255*60))
  154. return -EINVAL;
  155. /* When new timeout is bigger then 255 seconds,
  156. * we will round up to minutes (with a max of 255) */
  157. if (t > 255)
  158. t = (((t - 1) / 60) + 1) * 60;
  159. timeout = t;
  160. return 0;
  161. }
  162. static void sch311x_wdt_get_status(int *status)
  163. {
  164. unsigned char new_status;
  165. *status = 0;
  166. spin_lock(&sch311x_wdt_data.io_lock);
  167. /* -- Watchdog timer control --
  168. * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
  169. * Bit 1 Reserved
  170. * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
  171. * Bit 3 P20 Force Timeout enabled:
  172. * 0 = P20 activity does not generate the WD timeout event
  173. * 1 = P20 Allows rising edge of P20, from the keyboard
  174. * controller, to force the WD timeout event.
  175. * Bit 4-7 Reserved
  176. */
  177. new_status = inb(sch311x_wdt_data.runtime_reg + WDT_CTRL);
  178. if (new_status & 0x01)
  179. *status |= WDIOF_CARDRESET;
  180. spin_unlock(&sch311x_wdt_data.io_lock);
  181. }
  182. /*
  183. * /dev/watchdog handling
  184. */
  185. static ssize_t sch311x_wdt_write(struct file *file, const char __user *buf,
  186. size_t count, loff_t *ppos)
  187. {
  188. if (count) {
  189. if (!nowayout) {
  190. size_t i;
  191. sch311x_wdt_expect_close = 0;
  192. for (i = 0; i != count; i++) {
  193. char c;
  194. if (get_user(c, buf + i))
  195. return -EFAULT;
  196. if (c == 'V')
  197. sch311x_wdt_expect_close = 42;
  198. }
  199. }
  200. sch311x_wdt_keepalive();
  201. }
  202. return count;
  203. }
  204. static long sch311x_wdt_ioctl(struct file *file, unsigned int cmd,
  205. unsigned long arg)
  206. {
  207. int status;
  208. int new_timeout;
  209. void __user *argp = (void __user *)arg;
  210. int __user *p = argp;
  211. static const struct watchdog_info ident = {
  212. .options = WDIOF_KEEPALIVEPING |
  213. WDIOF_SETTIMEOUT |
  214. WDIOF_MAGICCLOSE,
  215. .firmware_version = 1,
  216. .identity = DRV_NAME,
  217. };
  218. switch (cmd) {
  219. case WDIOC_GETSUPPORT:
  220. if (copy_to_user(argp, &ident, sizeof(ident)))
  221. return -EFAULT;
  222. break;
  223. case WDIOC_GETSTATUS:
  224. {
  225. sch311x_wdt_get_status(&status);
  226. return put_user(status, p);
  227. }
  228. case WDIOC_GETBOOTSTATUS:
  229. return put_user(sch311x_wdt_data.boot_status, p);
  230. case WDIOC_SETOPTIONS:
  231. {
  232. int options, retval = -EINVAL;
  233. if (get_user(options, p))
  234. return -EFAULT;
  235. if (options & WDIOS_DISABLECARD) {
  236. sch311x_wdt_stop();
  237. retval = 0;
  238. }
  239. if (options & WDIOS_ENABLECARD) {
  240. sch311x_wdt_start();
  241. retval = 0;
  242. }
  243. return retval;
  244. }
  245. case WDIOC_KEEPALIVE:
  246. sch311x_wdt_keepalive();
  247. break;
  248. case WDIOC_SETTIMEOUT:
  249. if (get_user(new_timeout, p))
  250. return -EFAULT;
  251. if (sch311x_wdt_set_heartbeat(new_timeout))
  252. return -EINVAL;
  253. sch311x_wdt_keepalive();
  254. /* Fall */
  255. case WDIOC_GETTIMEOUT:
  256. return put_user(timeout, p);
  257. default:
  258. return -ENOTTY;
  259. }
  260. return 0;
  261. }
  262. static int sch311x_wdt_open(struct inode *inode, struct file *file)
  263. {
  264. if (test_and_set_bit(0, &sch311x_wdt_is_open))
  265. return -EBUSY;
  266. /*
  267. * Activate
  268. */
  269. sch311x_wdt_start();
  270. return nonseekable_open(inode, file);
  271. }
  272. static int sch311x_wdt_close(struct inode *inode, struct file *file)
  273. {
  274. if (sch311x_wdt_expect_close == 42) {
  275. sch311x_wdt_stop();
  276. } else {
  277. pr_crit("Unexpected close, not stopping watchdog!\n");
  278. sch311x_wdt_keepalive();
  279. }
  280. clear_bit(0, &sch311x_wdt_is_open);
  281. sch311x_wdt_expect_close = 0;
  282. return 0;
  283. }
  284. /*
  285. * Kernel Interfaces
  286. */
  287. static const struct file_operations sch311x_wdt_fops = {
  288. .owner = THIS_MODULE,
  289. .llseek = no_llseek,
  290. .write = sch311x_wdt_write,
  291. .unlocked_ioctl = sch311x_wdt_ioctl,
  292. .open = sch311x_wdt_open,
  293. .release = sch311x_wdt_close,
  294. };
  295. static struct miscdevice sch311x_wdt_miscdev = {
  296. .minor = WATCHDOG_MINOR,
  297. .name = "watchdog",
  298. .fops = &sch311x_wdt_fops,
  299. };
  300. /*
  301. * Init & exit routines
  302. */
  303. static int sch311x_wdt_probe(struct platform_device *pdev)
  304. {
  305. struct device *dev = &pdev->dev;
  306. int err;
  307. spin_lock_init(&sch311x_wdt_data.io_lock);
  308. if (!request_region(sch311x_wdt_data.runtime_reg + GP60, 1, DRV_NAME)) {
  309. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  310. sch311x_wdt_data.runtime_reg + GP60,
  311. sch311x_wdt_data.runtime_reg + GP60);
  312. err = -EBUSY;
  313. goto exit;
  314. }
  315. if (!request_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4,
  316. DRV_NAME)) {
  317. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  318. sch311x_wdt_data.runtime_reg + WDT_TIME_OUT,
  319. sch311x_wdt_data.runtime_reg + WDT_CTRL);
  320. err = -EBUSY;
  321. goto exit_release_region;
  322. }
  323. /* Make sure that the watchdog is not running */
  324. sch311x_wdt_stop();
  325. /* Disable keyboard and mouse interaction and interrupt */
  326. /* -- Watchdog timer configuration --
  327. * Bit 0 Reserved
  328. * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
  329. * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
  330. * Bit 3 Reserved
  331. * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
  332. * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
  333. */
  334. outb(0, sch311x_wdt_data.runtime_reg + WDT_CFG);
  335. /* Check that the heartbeat value is within it's range ;
  336. * if not reset to the default */
  337. if (sch311x_wdt_set_heartbeat(timeout)) {
  338. sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  339. dev_info(dev, "timeout value must be 1<=x<=15300, using %d\n",
  340. timeout);
  341. }
  342. /* Get status at boot */
  343. sch311x_wdt_get_status(&sch311x_wdt_data.boot_status);
  344. sch311x_wdt_miscdev.parent = dev;
  345. err = misc_register(&sch311x_wdt_miscdev);
  346. if (err != 0) {
  347. dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
  348. WATCHDOG_MINOR, err);
  349. goto exit_release_region2;
  350. }
  351. dev_info(dev,
  352. "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
  353. timeout, nowayout);
  354. return 0;
  355. exit_release_region2:
  356. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  357. exit_release_region:
  358. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  359. sch311x_wdt_data.runtime_reg = 0;
  360. exit:
  361. return err;
  362. }
  363. static int sch311x_wdt_remove(struct platform_device *pdev)
  364. {
  365. /* Stop the timer before we leave */
  366. if (!nowayout)
  367. sch311x_wdt_stop();
  368. /* Deregister */
  369. misc_deregister(&sch311x_wdt_miscdev);
  370. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  371. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  372. sch311x_wdt_data.runtime_reg = 0;
  373. return 0;
  374. }
  375. static void sch311x_wdt_shutdown(struct platform_device *dev)
  376. {
  377. /* Turn the WDT off if we have a soft shutdown */
  378. sch311x_wdt_stop();
  379. }
  380. static struct platform_driver sch311x_wdt_driver = {
  381. .probe = sch311x_wdt_probe,
  382. .remove = sch311x_wdt_remove,
  383. .shutdown = sch311x_wdt_shutdown,
  384. .driver = {
  385. .owner = THIS_MODULE,
  386. .name = DRV_NAME,
  387. },
  388. };
  389. static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
  390. {
  391. int err = 0, reg;
  392. unsigned short base_addr;
  393. unsigned char dev_id;
  394. sch311x_sio_enter(sio_config_port);
  395. /* Check device ID. We currently know about:
  396. * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
  397. reg = force_id ? force_id : sch311x_sio_inb(sio_config_port, 0x20);
  398. if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
  399. err = -ENODEV;
  400. goto exit;
  401. }
  402. dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
  403. /* Select logical device A (runtime registers) */
  404. sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
  405. /* Check if Logical Device Register is currently active */
  406. if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
  407. pr_info("Seems that LDN 0x0a is not active...\n");
  408. /* Get the base address of the runtime registers */
  409. base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
  410. sch311x_sio_inb(sio_config_port, 0x61);
  411. if (!base_addr) {
  412. pr_err("Base address not set\n");
  413. err = -ENODEV;
  414. goto exit;
  415. }
  416. *addr = base_addr;
  417. pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
  418. exit:
  419. sch311x_sio_exit(sio_config_port);
  420. return err;
  421. }
  422. static int __init sch311x_wdt_init(void)
  423. {
  424. int err, i, found = 0;
  425. unsigned short addr = 0;
  426. for (i = 0; !found && sch311x_ioports[i]; i++)
  427. if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
  428. found++;
  429. if (!found)
  430. return -ENODEV;
  431. sch311x_wdt_data.runtime_reg = addr;
  432. err = platform_driver_register(&sch311x_wdt_driver);
  433. if (err)
  434. return err;
  435. sch311x_wdt_pdev = platform_device_register_simple(DRV_NAME, addr,
  436. NULL, 0);
  437. if (IS_ERR(sch311x_wdt_pdev)) {
  438. err = PTR_ERR(sch311x_wdt_pdev);
  439. goto unreg_platform_driver;
  440. }
  441. return 0;
  442. unreg_platform_driver:
  443. platform_driver_unregister(&sch311x_wdt_driver);
  444. return err;
  445. }
  446. static void __exit sch311x_wdt_exit(void)
  447. {
  448. platform_device_unregister(sch311x_wdt_pdev);
  449. platform_driver_unregister(&sch311x_wdt_driver);
  450. }
  451. module_init(sch311x_wdt_init);
  452. module_exit(sch311x_wdt_exit);
  453. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  454. MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
  455. MODULE_LICENSE("GPL");
  456. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);