i8xx_tco.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * i8xx_tco 0.07: TCO timer driver for i8xx chipsets
  3. *
  4. * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights Reserved.
  5. * http://www.kernelconcepts.de
  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 kernel concepts nor Nils Faerber admit liability nor provide
  13. * warranty for any of this software. This material is provided
  14. * "AS-IS" and at no charge.
  15. *
  16. * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>
  17. * developed for
  18. * Jentro AG, Haar/Munich (Germany)
  19. *
  20. * TCO timer driver for i8xx chipsets
  21. * based on softdog.c by Alan Cox <alan@redhat.com>
  22. *
  23. * The TCO timer is implemented in the following I/O controller hubs:
  24. * (See the intel documentation on http://developer.intel.com.)
  25. * 82801AA (ICH) : document number 290655-003, 290677-014,
  26. * 82801AB (ICHO) : document number 290655-003, 290677-014,
  27. * 82801BA (ICH2) : document number 290687-002, 298242-027,
  28. * 82801BAM (ICH2-M) : document number 290687-002, 298242-027,
  29. * 82801CA (ICH3-S) : document number 290733-003, 290739-013,
  30. * 82801CAM (ICH3-M) : document number 290716-001, 290718-007,
  31. * 82801DB (ICH4) : document number 290744-001, 290745-020,
  32. * 82801DBM (ICH4-M) : document number 252337-001, 252663-005,
  33. * 82801E (C-ICH) : document number 273599-001, 273645-002,
  34. * 82801EB (ICH5) : document number 252516-001, 252517-003,
  35. * 82801ER (ICH5R) : document number 252516-001, 252517-003,
  36. * 82801FB (ICH6) : document number 301473-002, 301474-007,
  37. * 82801FR (ICH6R) : document number 301473-002, 301474-007,
  38. * 82801FBM (ICH6-M) : document number 301473-002, 301474-007,
  39. * 82801FW (ICH6W) : document number 301473-001, 301474-007,
  40. * 82801FRW (ICH6RW) : document number 301473-001, 301474-007
  41. *
  42. * 20000710 Nils Faerber
  43. * Initial Version 0.01
  44. * 20000728 Nils Faerber
  45. * 0.02 Fix for SMI_EN->TCO_EN bit, some cleanups
  46. * 20011214 Matt Domsch <Matt_Domsch@dell.com>
  47. * 0.03 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  48. * Didn't add timeout option as i810_margin already exists.
  49. * 20020224 Joel Becker, Wim Van Sebroeck
  50. * 0.04 Support for 82801CA(M) chipset, timer margin needs to be > 3,
  51. * add support for WDIOC_SETTIMEOUT and WDIOC_GETTIMEOUT.
  52. * 20020412 Rob Radez <rob@osinvestor.com>, Wim Van Sebroeck
  53. * 0.05 Fix possible timer_alive race, add expect close support,
  54. * clean up ioctls (WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS and
  55. * WDIOC_SETOPTIONS), made i810tco_getdevice __init,
  56. * removed boot_status, removed tco_timer_read,
  57. * added support for 82801DB and 82801E chipset,
  58. * added support for 82801EB and 8280ER chipset,
  59. * general cleanup.
  60. * 20030921 Wim Van Sebroeck <wim@iguana.be>
  61. * 0.06 change i810_margin to heartbeat, use module_param,
  62. * added notify system support, renamed module to i8xx_tco.
  63. * 20050128 Wim Van Sebroeck <wim@iguana.be>
  64. * 0.07 Added support for the ICH4-M, ICH6, ICH6R, ICH6-M, ICH6W and ICH6RW
  65. * chipsets. Also added support for the "undocumented" ICH7 chipset.
  66. */
  67. /*
  68. * Includes, defines, variables, module parameters, ...
  69. */
  70. #include <linux/module.h>
  71. #include <linux/moduleparam.h>
  72. #include <linux/types.h>
  73. #include <linux/miscdevice.h>
  74. #include <linux/watchdog.h>
  75. #include <linux/notifier.h>
  76. #include <linux/reboot.h>
  77. #include <linux/init.h>
  78. #include <linux/fs.h>
  79. #include <linux/pci.h>
  80. #include <linux/ioport.h>
  81. #include <asm/uaccess.h>
  82. #include <asm/io.h>
  83. #include "i8xx_tco.h"
  84. /* Module and version information */
  85. #define TCO_VERSION "0.07"
  86. #define TCO_MODULE_NAME "i8xx TCO timer"
  87. #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
  88. #define PFX TCO_MODULE_NAME ": "
  89. /* internal variables */
  90. static unsigned int ACPIBASE;
  91. static spinlock_t tco_lock; /* Guards the hardware */
  92. static unsigned long timer_alive;
  93. static char tco_expect_close;
  94. static struct pci_dev *i8xx_tco_pci;
  95. /* module parameters */
  96. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
  97. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  98. module_param(heartbeat, int, 0);
  99. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  100. static int nowayout = WATCHDOG_NOWAYOUT;
  101. module_param(nowayout, int, 0);
  102. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  103. /*
  104. * Some TCO specific functions
  105. */
  106. static inline unsigned char seconds_to_ticks(int seconds)
  107. {
  108. /* the internal timer is stored as ticks which decrement
  109. * every 0.6 seconds */
  110. return (seconds * 10) / 6;
  111. }
  112. static int tco_timer_start (void)
  113. {
  114. unsigned char val;
  115. spin_lock(&tco_lock);
  116. val = inb (TCO1_CNT + 1);
  117. val &= 0xf7;
  118. outb (val, TCO1_CNT + 1);
  119. val = inb (TCO1_CNT + 1);
  120. spin_unlock(&tco_lock);
  121. if (val & 0x08)
  122. return -1;
  123. return 0;
  124. }
  125. static int tco_timer_stop (void)
  126. {
  127. unsigned char val;
  128. spin_lock(&tco_lock);
  129. val = inb (TCO1_CNT + 1);
  130. val |= 0x08;
  131. outb (val, TCO1_CNT + 1);
  132. val = inb (TCO1_CNT + 1);
  133. spin_unlock(&tco_lock);
  134. if ((val & 0x08) == 0)
  135. return -1;
  136. return 0;
  137. }
  138. static int tco_timer_keepalive (void)
  139. {
  140. spin_lock(&tco_lock);
  141. outb (0x01, TCO1_RLD);
  142. spin_unlock(&tco_lock);
  143. return 0;
  144. }
  145. static int tco_timer_set_heartbeat (int t)
  146. {
  147. unsigned char val;
  148. unsigned char tmrval;
  149. tmrval = seconds_to_ticks(t);
  150. /* from the specs: */
  151. /* "Values of 0h-3h are ignored and should not be attempted" */
  152. if (tmrval > 0x3f || tmrval < 0x04)
  153. return -EINVAL;
  154. /* Write new heartbeat to watchdog */
  155. spin_lock(&tco_lock);
  156. val = inb (TCO1_TMR);
  157. val &= 0xc0;
  158. val |= tmrval;
  159. outb (val, TCO1_TMR);
  160. val = inb (TCO1_TMR);
  161. spin_unlock(&tco_lock);
  162. if ((val & 0x3f) != tmrval)
  163. return -EINVAL;
  164. heartbeat = t;
  165. return 0;
  166. }
  167. /*
  168. * /dev/watchdog handling
  169. */
  170. static int i8xx_tco_open (struct inode *inode, struct file *file)
  171. {
  172. /* /dev/watchdog can only be opened once */
  173. if (test_and_set_bit(0, &timer_alive))
  174. return -EBUSY;
  175. /*
  176. * Reload and activate timer
  177. */
  178. tco_timer_keepalive ();
  179. tco_timer_start ();
  180. return nonseekable_open(inode, file);
  181. }
  182. static int i8xx_tco_release (struct inode *inode, struct file *file)
  183. {
  184. /*
  185. * Shut off the timer.
  186. */
  187. if (tco_expect_close == 42) {
  188. tco_timer_stop ();
  189. } else {
  190. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  191. tco_timer_keepalive ();
  192. }
  193. clear_bit(0, &timer_alive);
  194. tco_expect_close = 0;
  195. return 0;
  196. }
  197. static ssize_t i8xx_tco_write (struct file *file, const char __user *data,
  198. size_t len, loff_t * ppos)
  199. {
  200. /* See if we got the magic character 'V' and reload the timer */
  201. if (len) {
  202. if (!nowayout) {
  203. size_t i;
  204. /* note: just in case someone wrote the magic character
  205. * five months ago... */
  206. tco_expect_close = 0;
  207. /* scan to see whether or not we got the magic character */
  208. for (i = 0; i != len; i++) {
  209. char c;
  210. if(get_user(c, data+i))
  211. return -EFAULT;
  212. if (c == 'V')
  213. tco_expect_close = 42;
  214. }
  215. }
  216. /* someone wrote to us, we should reload the timer */
  217. tco_timer_keepalive ();
  218. }
  219. return len;
  220. }
  221. static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
  222. unsigned int cmd, unsigned long arg)
  223. {
  224. int new_options, retval = -EINVAL;
  225. int new_heartbeat;
  226. void __user *argp = (void __user *)arg;
  227. int __user *p = argp;
  228. static struct watchdog_info ident = {
  229. .options = WDIOF_SETTIMEOUT |
  230. WDIOF_KEEPALIVEPING |
  231. WDIOF_MAGICCLOSE,
  232. .firmware_version = 0,
  233. .identity = TCO_MODULE_NAME,
  234. };
  235. switch (cmd) {
  236. case WDIOC_GETSUPPORT:
  237. return copy_to_user(argp, &ident,
  238. sizeof (ident)) ? -EFAULT : 0;
  239. case WDIOC_GETSTATUS:
  240. case WDIOC_GETBOOTSTATUS:
  241. return put_user (0, p);
  242. case WDIOC_KEEPALIVE:
  243. tco_timer_keepalive ();
  244. return 0;
  245. case WDIOC_SETOPTIONS:
  246. {
  247. if (get_user (new_options, p))
  248. return -EFAULT;
  249. if (new_options & WDIOS_DISABLECARD) {
  250. tco_timer_stop ();
  251. retval = 0;
  252. }
  253. if (new_options & WDIOS_ENABLECARD) {
  254. tco_timer_keepalive ();
  255. tco_timer_start ();
  256. retval = 0;
  257. }
  258. return retval;
  259. }
  260. case WDIOC_SETTIMEOUT:
  261. {
  262. if (get_user(new_heartbeat, p))
  263. return -EFAULT;
  264. if (tco_timer_set_heartbeat(new_heartbeat))
  265. return -EINVAL;
  266. tco_timer_keepalive ();
  267. /* Fall */
  268. }
  269. case WDIOC_GETTIMEOUT:
  270. return put_user(heartbeat, p);
  271. default:
  272. return -ENOIOCTLCMD;
  273. }
  274. }
  275. /*
  276. * Notify system
  277. */
  278. static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
  279. {
  280. if (code==SYS_DOWN || code==SYS_HALT) {
  281. /* Turn the WDT off */
  282. tco_timer_stop ();
  283. }
  284. return NOTIFY_DONE;
  285. }
  286. /*
  287. * Kernel Interfaces
  288. */
  289. static struct file_operations i8xx_tco_fops = {
  290. .owner = THIS_MODULE,
  291. .llseek = no_llseek,
  292. .write = i8xx_tco_write,
  293. .ioctl = i8xx_tco_ioctl,
  294. .open = i8xx_tco_open,
  295. .release = i8xx_tco_release,
  296. };
  297. static struct miscdevice i8xx_tco_miscdev = {
  298. .minor = WATCHDOG_MINOR,
  299. .name = "watchdog",
  300. .fops = &i8xx_tco_fops,
  301. };
  302. static struct notifier_block i8xx_tco_notifier = {
  303. .notifier_call = i8xx_tco_notify_sys,
  304. };
  305. /*
  306. * Data for PCI driver interface
  307. *
  308. * This data only exists for exporting the supported
  309. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  310. * register a pci_driver, because someone else might one day
  311. * want to register another driver on the same PCI id.
  312. */
  313. static struct pci_device_id i8xx_tco_pci_tbl[] = {
  314. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
  315. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
  316. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
  317. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
  318. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
  319. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
  320. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
  321. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, },
  322. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
  323. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, },
  324. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0, PCI_ANY_ID, PCI_ANY_ID, },
  325. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, PCI_ANY_ID, PCI_ANY_ID, },
  326. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_2, PCI_ANY_ID, PCI_ANY_ID, },
  327. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0, PCI_ANY_ID, PCI_ANY_ID, },
  328. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1, PCI_ANY_ID, PCI_ANY_ID, },
  329. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, },
  330. { 0, }, /* End of list */
  331. };
  332. MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
  333. /*
  334. * Init & exit routines
  335. */
  336. static unsigned char __init i8xx_tco_getdevice (void)
  337. {
  338. struct pci_dev *dev = NULL;
  339. u8 val1, val2;
  340. u16 badr;
  341. /*
  342. * Find the PCI device
  343. */
  344. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  345. if (pci_match_id(i8xx_tco_pci_tbl, dev)) {
  346. i8xx_tco_pci = dev;
  347. break;
  348. }
  349. }
  350. if (i8xx_tco_pci) {
  351. /*
  352. * Find the ACPI base I/O address which is the base
  353. * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
  354. * ACPIBASE is bits [15:7] from 0x40-0x43
  355. */
  356. pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
  357. pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
  358. badr = ((val2 << 1) | (val1 >> 7)) << 7;
  359. ACPIBASE = badr;
  360. /* Something's wrong here, ACPIBASE has to be set */
  361. if (badr == 0x0001 || badr == 0x0000) {
  362. printk (KERN_ERR PFX "failed to get TCOBASE address\n");
  363. return 0;
  364. }
  365. /*
  366. * Check chipset's NO_REBOOT bit
  367. */
  368. pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
  369. if (val1 & 0x02) {
  370. val1 &= 0xfd;
  371. pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
  372. pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
  373. if (val1 & 0x02) {
  374. printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
  375. return 0; /* Cannot reset NO_REBOOT bit */
  376. }
  377. }
  378. /* Set the TCO_EN bit in SMI_EN register */
  379. if (!request_region (SMI_EN + 1, 1, "i8xx TCO")) {
  380. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  381. SMI_EN + 1);
  382. return 0;
  383. }
  384. val1 = inb (SMI_EN + 1);
  385. val1 &= 0xdf;
  386. outb (val1, SMI_EN + 1);
  387. release_region (SMI_EN + 1, 1);
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. static int __init watchdog_init (void)
  393. {
  394. int ret;
  395. spin_lock_init(&tco_lock);
  396. /* Check whether or not the hardware watchdog is there */
  397. if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
  398. return -ENODEV;
  399. if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
  400. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  401. TCOBASE);
  402. ret = -EIO;
  403. goto out;
  404. }
  405. /* Clear out the (probably old) status */
  406. outb (0, TCO1_STS);
  407. outb (3, TCO2_STS);
  408. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  409. if (tco_timer_set_heartbeat (heartbeat)) {
  410. heartbeat = WATCHDOG_HEARTBEAT;
  411. tco_timer_set_heartbeat (heartbeat);
  412. printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
  413. heartbeat);
  414. }
  415. ret = register_reboot_notifier(&i8xx_tco_notifier);
  416. if (ret != 0) {
  417. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  418. ret);
  419. goto unreg_region;
  420. }
  421. ret = misc_register(&i8xx_tco_miscdev);
  422. if (ret != 0) {
  423. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  424. WATCHDOG_MINOR, ret);
  425. goto unreg_notifier;
  426. }
  427. tco_timer_stop ();
  428. printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
  429. TCOBASE, heartbeat, nowayout);
  430. return 0;
  431. unreg_notifier:
  432. unregister_reboot_notifier(&i8xx_tco_notifier);
  433. unreg_region:
  434. release_region (TCOBASE, 0x10);
  435. out:
  436. return ret;
  437. }
  438. static void __exit watchdog_cleanup (void)
  439. {
  440. u8 val;
  441. /* Stop the timer before we leave */
  442. if (!nowayout)
  443. tco_timer_stop ();
  444. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  445. pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
  446. val |= 0x02;
  447. pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
  448. /* Deregister */
  449. misc_deregister (&i8xx_tco_miscdev);
  450. unregister_reboot_notifier(&i8xx_tco_notifier);
  451. release_region (TCOBASE, 0x10);
  452. }
  453. module_init(watchdog_init);
  454. module_exit(watchdog_cleanup);
  455. MODULE_AUTHOR("Nils Faerber");
  456. MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
  457. MODULE_LICENSE("GPL");
  458. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);