i8xx_tco.c 14 KB

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