sch311x_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. 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. outb(0x0e, sch311x_wdt_data.runtime_reg + GP60);
  130. spin_unlock(&sch311x_wdt_data.io_lock);
  131. }
  132. static void sch311x_wdt_stop(void)
  133. {
  134. spin_lock(&sch311x_wdt_data.io_lock);
  135. /* stop the watchdog */
  136. outb(0x01, sch311x_wdt_data.runtime_reg + GP60);
  137. /* disable timeout by setting it to 0 */
  138. sch311x_wdt_set_timeout(0);
  139. spin_unlock(&sch311x_wdt_data.io_lock);
  140. }
  141. static void sch311x_wdt_keepalive(void)
  142. {
  143. spin_lock(&sch311x_wdt_data.io_lock);
  144. sch311x_wdt_set_timeout(timeout);
  145. spin_unlock(&sch311x_wdt_data.io_lock);
  146. }
  147. static int sch311x_wdt_set_heartbeat(int t)
  148. {
  149. if (t < 1 || t > (255*60))
  150. return -EINVAL;
  151. /* When new timeout is bigger then 255 seconds,
  152. * we will round up to minutes (with a max of 255) */
  153. if (t > 255)
  154. t = (((t - 1) / 60) + 1) * 60;
  155. timeout = t;
  156. return 0;
  157. }
  158. static void sch311x_wdt_get_status(int *status)
  159. {
  160. unsigned char new_status;
  161. *status = 0;
  162. spin_lock(&sch311x_wdt_data.io_lock);
  163. /* -- Watchdog timer control --
  164. * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
  165. * Bit 1 Reserved
  166. * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
  167. * Bit 3 P20 Force Timeout enabled:
  168. * 0 = P20 activity does not generate the WD timeout event
  169. * 1 = P20 Allows rising edge of P20, from the keyboard
  170. * controller, to force the WD timeout event.
  171. * Bit 4-7 Reserved
  172. */
  173. new_status = inb(sch311x_wdt_data.runtime_reg + WDT_CTRL);
  174. if (new_status & 0x01)
  175. *status |= WDIOF_CARDRESET;
  176. spin_unlock(&sch311x_wdt_data.io_lock);
  177. }
  178. /*
  179. * /dev/watchdog handling
  180. */
  181. static ssize_t sch311x_wdt_write(struct file *file, const char __user *buf,
  182. size_t count, loff_t *ppos)
  183. {
  184. if (count) {
  185. if (!nowayout) {
  186. size_t i;
  187. sch311x_wdt_expect_close = 0;
  188. for (i = 0; i != count; i++) {
  189. char c;
  190. if (get_user(c, buf + i))
  191. return -EFAULT;
  192. if (c == 'V')
  193. sch311x_wdt_expect_close = 42;
  194. }
  195. }
  196. sch311x_wdt_keepalive();
  197. }
  198. return count;
  199. }
  200. static long sch311x_wdt_ioctl(struct file *file, unsigned int cmd,
  201. unsigned long arg)
  202. {
  203. int status;
  204. int new_timeout;
  205. void __user *argp = (void __user *)arg;
  206. int __user *p = argp;
  207. static const struct watchdog_info ident = {
  208. .options = WDIOF_KEEPALIVEPING |
  209. WDIOF_SETTIMEOUT |
  210. WDIOF_MAGICCLOSE,
  211. .firmware_version = 1,
  212. .identity = DRV_NAME,
  213. };
  214. switch (cmd) {
  215. case WDIOC_GETSUPPORT:
  216. if (copy_to_user(argp, &ident, sizeof(ident)))
  217. return -EFAULT;
  218. break;
  219. case WDIOC_GETSTATUS:
  220. {
  221. sch311x_wdt_get_status(&status);
  222. return put_user(status, p);
  223. }
  224. case WDIOC_GETBOOTSTATUS:
  225. return put_user(sch311x_wdt_data.boot_status, p);
  226. case WDIOC_SETOPTIONS:
  227. {
  228. int options, retval = -EINVAL;
  229. if (get_user(options, p))
  230. return -EFAULT;
  231. if (options & WDIOS_DISABLECARD) {
  232. sch311x_wdt_stop();
  233. retval = 0;
  234. }
  235. if (options & WDIOS_ENABLECARD) {
  236. sch311x_wdt_start();
  237. retval = 0;
  238. }
  239. return retval;
  240. }
  241. case WDIOC_KEEPALIVE:
  242. sch311x_wdt_keepalive();
  243. break;
  244. case WDIOC_SETTIMEOUT:
  245. if (get_user(new_timeout, p))
  246. return -EFAULT;
  247. if (sch311x_wdt_set_heartbeat(new_timeout))
  248. return -EINVAL;
  249. sch311x_wdt_keepalive();
  250. /* Fall */
  251. case WDIOC_GETTIMEOUT:
  252. return put_user(timeout, p);
  253. default:
  254. return -ENOTTY;
  255. }
  256. return 0;
  257. }
  258. static int sch311x_wdt_open(struct inode *inode, struct file *file)
  259. {
  260. if (test_and_set_bit(0, &sch311x_wdt_is_open))
  261. return -EBUSY;
  262. /*
  263. * Activate
  264. */
  265. sch311x_wdt_start();
  266. return nonseekable_open(inode, file);
  267. }
  268. static int sch311x_wdt_close(struct inode *inode, struct file *file)
  269. {
  270. if (sch311x_wdt_expect_close == 42) {
  271. sch311x_wdt_stop();
  272. } else {
  273. pr_crit("Unexpected close, not stopping watchdog!\n");
  274. sch311x_wdt_keepalive();
  275. }
  276. clear_bit(0, &sch311x_wdt_is_open);
  277. sch311x_wdt_expect_close = 0;
  278. return 0;
  279. }
  280. /*
  281. * Kernel Interfaces
  282. */
  283. static const struct file_operations sch311x_wdt_fops = {
  284. .owner = THIS_MODULE,
  285. .llseek = no_llseek,
  286. .write = sch311x_wdt_write,
  287. .unlocked_ioctl = sch311x_wdt_ioctl,
  288. .open = sch311x_wdt_open,
  289. .release = sch311x_wdt_close,
  290. };
  291. static struct miscdevice sch311x_wdt_miscdev = {
  292. .minor = WATCHDOG_MINOR,
  293. .name = "watchdog",
  294. .fops = &sch311x_wdt_fops,
  295. };
  296. /*
  297. * Init & exit routines
  298. */
  299. static int __devinit sch311x_wdt_probe(struct platform_device *pdev)
  300. {
  301. struct device *dev = &pdev->dev;
  302. int err;
  303. spin_lock_init(&sch311x_wdt_data.io_lock);
  304. if (!request_region(sch311x_wdt_data.runtime_reg + GP60, 1, DRV_NAME)) {
  305. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  306. sch311x_wdt_data.runtime_reg + GP60,
  307. sch311x_wdt_data.runtime_reg + GP60);
  308. err = -EBUSY;
  309. goto exit;
  310. }
  311. if (!request_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4,
  312. DRV_NAME)) {
  313. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  314. sch311x_wdt_data.runtime_reg + WDT_TIME_OUT,
  315. sch311x_wdt_data.runtime_reg + WDT_CTRL);
  316. err = -EBUSY;
  317. goto exit_release_region;
  318. }
  319. /* Make sure that the watchdog is not running */
  320. sch311x_wdt_stop();
  321. /* Disable keyboard and mouse interaction and interrupt */
  322. /* -- Watchdog timer configuration --
  323. * Bit 0 Reserved
  324. * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
  325. * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
  326. * Bit 3 Reserved
  327. * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
  328. * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
  329. */
  330. outb(0, sch311x_wdt_data.runtime_reg + WDT_CFG);
  331. /* Check that the heartbeat value is within it's range ;
  332. * if not reset to the default */
  333. if (sch311x_wdt_set_heartbeat(timeout)) {
  334. sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  335. dev_info(dev, "timeout value must be 1<=x<=15300, using %d\n",
  336. timeout);
  337. }
  338. /* Get status at boot */
  339. sch311x_wdt_get_status(&sch311x_wdt_data.boot_status);
  340. sch311x_wdt_miscdev.parent = dev;
  341. err = misc_register(&sch311x_wdt_miscdev);
  342. if (err != 0) {
  343. dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
  344. WATCHDOG_MINOR, err);
  345. goto exit_release_region2;
  346. }
  347. dev_info(dev,
  348. "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
  349. timeout, nowayout);
  350. return 0;
  351. exit_release_region2:
  352. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  353. exit_release_region:
  354. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  355. sch311x_wdt_data.runtime_reg = 0;
  356. exit:
  357. return err;
  358. }
  359. static int __devexit sch311x_wdt_remove(struct platform_device *pdev)
  360. {
  361. /* Stop the timer before we leave */
  362. if (!nowayout)
  363. sch311x_wdt_stop();
  364. /* Deregister */
  365. misc_deregister(&sch311x_wdt_miscdev);
  366. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  367. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  368. sch311x_wdt_data.runtime_reg = 0;
  369. return 0;
  370. }
  371. static void sch311x_wdt_shutdown(struct platform_device *dev)
  372. {
  373. /* Turn the WDT off if we have a soft shutdown */
  374. sch311x_wdt_stop();
  375. }
  376. static struct platform_driver sch311x_wdt_driver = {
  377. .probe = sch311x_wdt_probe,
  378. .remove = __devexit_p(sch311x_wdt_remove),
  379. .shutdown = sch311x_wdt_shutdown,
  380. .driver = {
  381. .owner = THIS_MODULE,
  382. .name = DRV_NAME,
  383. },
  384. };
  385. static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
  386. {
  387. int err = 0, reg;
  388. unsigned short base_addr;
  389. unsigned char dev_id;
  390. sch311x_sio_enter(sio_config_port);
  391. /* Check device ID. We currently know about:
  392. * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
  393. reg = force_id ? force_id : sch311x_sio_inb(sio_config_port, 0x20);
  394. if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
  395. err = -ENODEV;
  396. goto exit;
  397. }
  398. dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
  399. /* Select logical device A (runtime registers) */
  400. sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
  401. /* Check if Logical Device Register is currently active */
  402. if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
  403. pr_info("Seems that LDN 0x0a is not active...\n");
  404. /* Get the base address of the runtime registers */
  405. base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
  406. sch311x_sio_inb(sio_config_port, 0x61);
  407. if (!base_addr) {
  408. pr_err("Base address not set\n");
  409. err = -ENODEV;
  410. goto exit;
  411. }
  412. *addr = base_addr;
  413. pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
  414. exit:
  415. sch311x_sio_exit(sio_config_port);
  416. return err;
  417. }
  418. static int __init sch311x_wdt_init(void)
  419. {
  420. int err, i, found = 0;
  421. unsigned short addr = 0;
  422. for (i = 0; !found && sch311x_ioports[i]; i++)
  423. if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
  424. found++;
  425. if (!found)
  426. return -ENODEV;
  427. sch311x_wdt_data.runtime_reg = addr;
  428. err = platform_driver_register(&sch311x_wdt_driver);
  429. if (err)
  430. return err;
  431. sch311x_wdt_pdev = platform_device_register_simple(DRV_NAME, addr,
  432. NULL, 0);
  433. if (IS_ERR(sch311x_wdt_pdev)) {
  434. err = PTR_ERR(sch311x_wdt_pdev);
  435. goto unreg_platform_driver;
  436. }
  437. return 0;
  438. unreg_platform_driver:
  439. platform_driver_unregister(&sch311x_wdt_driver);
  440. return err;
  441. }
  442. static void __exit sch311x_wdt_exit(void)
  443. {
  444. platform_device_unregister(sch311x_wdt_pdev);
  445. platform_driver_unregister(&sch311x_wdt_driver);
  446. }
  447. module_init(sch311x_wdt_init);
  448. module_exit(sch311x_wdt_exit);
  449. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  450. MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
  451. MODULE_LICENSE("GPL");
  452. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);