sch311x_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. /* Includes */
  20. #include <linux/module.h> /* For module specific items */
  21. #include <linux/moduleparam.h> /* For new moduleparam's */
  22. #include <linux/types.h> /* For standard types (like size_t) */
  23. #include <linux/errno.h> /* For the -ENODEV/... values */
  24. #include <linux/kernel.h> /* For printk/... */
  25. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
  26. (WATCHDOG_MINOR) */
  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. #define PFX DRV_NAME ": "
  38. /* Runtime registers */
  39. #define RESGEN 0x1d
  40. #define GP60 0x47
  41. #define WDT_TIME_OUT 0x65
  42. #define WDT_VAL 0x66
  43. #define WDT_CFG 0x67
  44. #define WDT_CTRL 0x68
  45. /* internal variables */
  46. static unsigned long sch311x_wdt_is_open;
  47. static char sch311x_wdt_expect_close;
  48. static struct platform_device *sch311x_wdt_pdev;
  49. static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
  50. static struct { /* The devices private data */
  51. /* the Runtime Register base address */
  52. unsigned short runtime_reg;
  53. /* The card's boot status */
  54. int boot_status;
  55. /* the lock for io operations */
  56. spinlock_t io_lock;
  57. } sch311x_wdt_data;
  58. /* Module load parameters */
  59. static unsigned short force_id;
  60. module_param(force_id, ushort, 0);
  61. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  62. static unsigned short therm_trip;
  63. module_param(therm_trip, ushort, 0);
  64. MODULE_PARM_DESC(therm_trip, "Should a ThermTrip trigger the reset generator");
  65. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  66. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  67. module_param(timeout, int, 0);
  68. MODULE_PARM_DESC(timeout,
  69. "Watchdog timeout in seconds. 1<= timeout <=15300, default="
  70. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  71. static int nowayout = WATCHDOG_NOWAYOUT;
  72. module_param(nowayout, int, 0);
  73. MODULE_PARM_DESC(nowayout,
  74. "Watchdog cannot be stopped once started (default="
  75. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  76. /*
  77. * Super-IO functions
  78. */
  79. static inline void sch311x_sio_enter(int sio_config_port)
  80. {
  81. outb(0x55, sio_config_port);
  82. }
  83. static inline void sch311x_sio_exit(int sio_config_port)
  84. {
  85. outb(0xaa, sio_config_port);
  86. }
  87. static inline int sch311x_sio_inb(int sio_config_port, int reg)
  88. {
  89. outb(reg, sio_config_port);
  90. return inb(sio_config_port + 1);
  91. }
  92. static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
  93. {
  94. outb(reg, sio_config_port);
  95. outb(val, sio_config_port + 1);
  96. }
  97. /*
  98. * Watchdog Operations
  99. */
  100. static void sch311x_wdt_set_timeout(int t)
  101. {
  102. unsigned char timeout_unit = 0x80;
  103. /* When new timeout is bigger then 255 seconds, we will use minutes */
  104. if (t > 255) {
  105. timeout_unit = 0;
  106. t /= 60;
  107. }
  108. /* -- Watchdog Timeout --
  109. * Bit 0-6 (Reserved)
  110. * Bit 7 WDT Time-out Value Units Select
  111. * (0 = Minutes, 1 = Seconds)
  112. */
  113. outb(timeout_unit, sch311x_wdt_data.runtime_reg + WDT_TIME_OUT);
  114. /* -- Watchdog Timer Time-out Value --
  115. * Bit 0-7 Binary coded units (0=Disabled, 1..255)
  116. */
  117. outb(t, sch311x_wdt_data.runtime_reg + WDT_VAL);
  118. }
  119. static void sch311x_wdt_start(void)
  120. {
  121. spin_lock(&sch311x_wdt_data.io_lock);
  122. /* set watchdog's timeout */
  123. sch311x_wdt_set_timeout(timeout);
  124. /* enable the watchdog */
  125. /* -- General Purpose I/O Bit 6.0 --
  126. * Bit 0, In/Out: 0 = Output, 1 = Input
  127. * Bit 1, Polarity: 0 = No Invert, 1 = Invert
  128. * Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
  129. * 10 = Either Edge Triggered Intr.4
  130. * Bit 4-6 (Reserved)
  131. * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
  132. */
  133. outb(0x0e, sch311x_wdt_data.runtime_reg + GP60);
  134. spin_unlock(&sch311x_wdt_data.io_lock);
  135. }
  136. static void sch311x_wdt_stop(void)
  137. {
  138. spin_lock(&sch311x_wdt_data.io_lock);
  139. /* stop the watchdog */
  140. outb(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. printk(KERN_CRIT PFX
  278. "Unexpected close, not stopping watchdog!\n");
  279. sch311x_wdt_keepalive();
  280. }
  281. clear_bit(0, &sch311x_wdt_is_open);
  282. sch311x_wdt_expect_close = 0;
  283. return 0;
  284. }
  285. /*
  286. * Kernel Interfaces
  287. */
  288. static const struct file_operations sch311x_wdt_fops = {
  289. .owner = THIS_MODULE,
  290. .llseek = no_llseek,
  291. .write = sch311x_wdt_write,
  292. .unlocked_ioctl = sch311x_wdt_ioctl,
  293. .open = sch311x_wdt_open,
  294. .release = sch311x_wdt_close,
  295. };
  296. static struct miscdevice sch311x_wdt_miscdev = {
  297. .minor = WATCHDOG_MINOR,
  298. .name = "watchdog",
  299. .fops = &sch311x_wdt_fops,
  300. };
  301. /*
  302. * Init & exit routines
  303. */
  304. static int __devinit sch311x_wdt_probe(struct platform_device *pdev)
  305. {
  306. struct device *dev = &pdev->dev;
  307. unsigned char val;
  308. int err;
  309. spin_lock_init(&sch311x_wdt_data.io_lock);
  310. if (!request_region(sch311x_wdt_data.runtime_reg + RESGEN, 1,
  311. DRV_NAME)) {
  312. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  313. sch311x_wdt_data.runtime_reg + RESGEN,
  314. sch311x_wdt_data.runtime_reg + RESGEN);
  315. err = -EBUSY;
  316. goto exit;
  317. }
  318. if (!request_region(sch311x_wdt_data.runtime_reg + GP60, 1, DRV_NAME)) {
  319. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  320. sch311x_wdt_data.runtime_reg + GP60,
  321. sch311x_wdt_data.runtime_reg + GP60);
  322. err = -EBUSY;
  323. goto exit_release_region;
  324. }
  325. if (!request_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4,
  326. DRV_NAME)) {
  327. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  328. sch311x_wdt_data.runtime_reg + WDT_TIME_OUT,
  329. sch311x_wdt_data.runtime_reg + WDT_CTRL);
  330. err = -EBUSY;
  331. goto exit_release_region2;
  332. }
  333. /* Make sure that the watchdog is not running */
  334. sch311x_wdt_stop();
  335. /* Disable keyboard and mouse interaction and interrupt */
  336. /* -- Watchdog timer configuration --
  337. * Bit 0 Reserved
  338. * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
  339. * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
  340. * Bit 3 Reserved
  341. * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
  342. * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
  343. */
  344. outb(0, sch311x_wdt_data.runtime_reg + WDT_CFG);
  345. /* Check that the heartbeat value is within it's range ;
  346. * if not reset to the default */
  347. if (sch311x_wdt_set_heartbeat(timeout)) {
  348. sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT);
  349. dev_info(dev, "timeout value must be 1<=x<=15300, using %d\n",
  350. timeout);
  351. }
  352. /* Get status at boot */
  353. sch311x_wdt_get_status(&sch311x_wdt_data.boot_status);
  354. /* enable watchdog */
  355. /* -- Reset Generator --
  356. * Bit 0 Enable Watchdog Timer Generation: 0* = Enabled, 1 = Disabled
  357. * Bit 1 Thermtrip Source Select: O* = No Source, 1 = Source
  358. * Bit 2 WDT2_CTL: WDT input bit
  359. * Bit 3-7 Reserved
  360. */
  361. outb(0, sch311x_wdt_data.runtime_reg + RESGEN);
  362. val = therm_trip ? 0x06 : 0x04;
  363. outb(val, sch311x_wdt_data.runtime_reg + RESGEN);
  364. sch311x_wdt_miscdev.parent = dev;
  365. err = misc_register(&sch311x_wdt_miscdev);
  366. if (err != 0) {
  367. dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
  368. WATCHDOG_MINOR, err);
  369. goto exit_release_region3;
  370. }
  371. dev_info(dev,
  372. "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
  373. timeout, nowayout);
  374. return 0;
  375. exit_release_region3:
  376. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  377. exit_release_region2:
  378. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  379. exit_release_region:
  380. release_region(sch311x_wdt_data.runtime_reg + RESGEN, 1);
  381. sch311x_wdt_data.runtime_reg = 0;
  382. exit:
  383. return err;
  384. }
  385. static int __devexit sch311x_wdt_remove(struct platform_device *pdev)
  386. {
  387. /* Stop the timer before we leave */
  388. if (!nowayout)
  389. sch311x_wdt_stop();
  390. /* Deregister */
  391. misc_deregister(&sch311x_wdt_miscdev);
  392. release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
  393. release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
  394. release_region(sch311x_wdt_data.runtime_reg + RESGEN, 1);
  395. sch311x_wdt_data.runtime_reg = 0;
  396. return 0;
  397. }
  398. static void sch311x_wdt_shutdown(struct platform_device *dev)
  399. {
  400. /* Turn the WDT off if we have a soft shutdown */
  401. sch311x_wdt_stop();
  402. }
  403. #define sch311x_wdt_suspend NULL
  404. #define sch311x_wdt_resume NULL
  405. static struct platform_driver sch311x_wdt_driver = {
  406. .probe = sch311x_wdt_probe,
  407. .remove = __devexit_p(sch311x_wdt_remove),
  408. .shutdown = sch311x_wdt_shutdown,
  409. .suspend = sch311x_wdt_suspend,
  410. .resume = sch311x_wdt_resume,
  411. .driver = {
  412. .owner = THIS_MODULE,
  413. .name = DRV_NAME,
  414. },
  415. };
  416. static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
  417. {
  418. int err = 0, reg;
  419. unsigned short base_addr;
  420. unsigned char dev_id;
  421. sch311x_sio_enter(sio_config_port);
  422. /* Check device ID. We currently know about:
  423. * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
  424. reg = force_id ? force_id : sch311x_sio_inb(sio_config_port, 0x20);
  425. if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
  426. err = -ENODEV;
  427. goto exit;
  428. }
  429. dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
  430. /* Select logical device A (runtime registers) */
  431. sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
  432. /* Check if Logical Device Register is currently active */
  433. if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
  434. printk(KERN_INFO PFX "Seems that LDN 0x0a is not active...\n");
  435. /* Get the base address of the runtime registers */
  436. base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
  437. sch311x_sio_inb(sio_config_port, 0x61);
  438. if (!base_addr) {
  439. printk(KERN_ERR PFX "Base address not set.\n");
  440. err = -ENODEV;
  441. goto exit;
  442. }
  443. *addr = base_addr;
  444. printk(KERN_INFO PFX "Found an SMSC SCH311%d chip at 0x%04x\n",
  445. dev_id, base_addr);
  446. exit:
  447. sch311x_sio_exit(sio_config_port);
  448. return err;
  449. }
  450. static int __init sch311x_wdt_init(void)
  451. {
  452. int err, i, found = 0;
  453. unsigned short addr = 0;
  454. for (i = 0; !found && sch311x_ioports[i]; i++)
  455. if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
  456. found++;
  457. if (!found)
  458. return -ENODEV;
  459. sch311x_wdt_data.runtime_reg = addr;
  460. err = platform_driver_register(&sch311x_wdt_driver);
  461. if (err)
  462. return err;
  463. sch311x_wdt_pdev = platform_device_register_simple(DRV_NAME, addr,
  464. NULL, 0);
  465. if (IS_ERR(sch311x_wdt_pdev)) {
  466. err = PTR_ERR(sch311x_wdt_pdev);
  467. goto unreg_platform_driver;
  468. }
  469. return 0;
  470. unreg_platform_driver:
  471. platform_driver_unregister(&sch311x_wdt_driver);
  472. return err;
  473. }
  474. static void __exit sch311x_wdt_exit(void)
  475. {
  476. platform_device_unregister(sch311x_wdt_pdev);
  477. platform_driver_unregister(&sch311x_wdt_driver);
  478. }
  479. module_init(sch311x_wdt_init);
  480. module_exit(sch311x_wdt_exit);
  481. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  482. MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
  483. MODULE_LICENSE("GPL");
  484. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);