sch311x_wdt.c 13 KB

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