i810-tco.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * i810-tco 0.05: 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 & 82801AB  chip : document number 290655-003, 290677-004,
  26.  * 82801BA & 82801BAM chip : document number 290687-002, 298242-005,
  27.  * 82801CA & 82801CAM chip : document number 290716-001, 290718-001,
  28.  * 82801DB & 82801E   chip : document number 290744-001, 273599-001
  29.  *
  30.  *  20000710 Nils Faerber
  31.  * Initial Version 0.01
  32.  *  20000728 Nils Faerber
  33.  * 0.02 Fix for SMI_EN->TCO_EN bit, some cleanups
  34.  *  20011214 Matt Domsch <Matt_Domsch@dell.com>
  35.  * 0.03 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  36.  *      Didn't add timeout option as i810_margin already exists.
  37.  *  20020224 Joel Becker, Wim Van Sebroeck
  38.  * 0.04 Support for 82801CA(M) chipset, timer margin needs to be > 3,
  39.  *      add support for WDIOC_SETTIMEOUT and WDIOC_GETTIMEOUT.
  40.  *  20020412 Rob Radez <rob@osinvestor.com>, Wim Van Sebroeck
  41.  * 0.05 Fix possible timer_alive race, add expect close support,
  42.  *      clean up ioctls (WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS and
  43.  *      WDIOC_SETOPTIONS), made i810tco_getdevice __init,
  44.  *      removed boot_status, removed tco_timer_read,
  45.  *      added support for 82801DB and 82801E chipset, general cleanup.
  46.  */
  47.  
  48. #include <linux/module.h>
  49. #include <linux/types.h>
  50. #include <linux/kernel.h>
  51. #include <linux/fs.h>
  52. #include <linux/mm.h>
  53. #include <linux/miscdevice.h>
  54. #include <linux/watchdog.h>
  55. #include <linux/reboot.h>
  56. #include <linux/init.h>
  57. #include <linux/pci.h>
  58. #include <linux/ioport.h>
  59. #include <asm/uaccess.h>
  60. #include <asm/io.h>
  61. #include "i810-tco.h"
  62. /* Module and version information */
  63. #define TCO_VERSION "0.05"
  64. #define TCO_MODULE_NAME "i810 TCO timer"
  65. #define TCO_DRIVER_NAME   TCO_MODULE_NAME ", v" TCO_VERSION
  66. /* Default expire timeout */
  67. #define TIMER_MARGIN 50 /* steps of 0.6sec, 3<n<64. Default is 30 seconds */
  68. static unsigned int ACPIBASE;
  69. static spinlock_t tco_lock; /* Guards the hardware */
  70. static int i810_margin = TIMER_MARGIN; /* steps of 0.6sec */
  71. MODULE_PARM(i810_margin, "i");
  72. MODULE_PARM_DESC(i810_margin, "i810-tco timeout in steps of 0.6sec, 3<n<64. Default = 50 (30 seconds)");
  73. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  74. static int nowayout = 1;
  75. #else
  76. static int nowayout = 0;
  77. #endif
  78. MODULE_PARM(nowayout,"i");
  79. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  80. /*
  81.  * Timer active flag
  82.  */
  83. static unsigned long timer_alive;
  84. static char tco_expect_close;
  85. /*
  86.  * Some TCO specific functions
  87.  */
  88. /*
  89.  * Start the timer countdown
  90.  */
  91. static int tco_timer_start (void)
  92. {
  93. unsigned char val;
  94. spin_lock(&tco_lock);
  95. val = inb (TCO1_CNT + 1);
  96. val &= 0xf7;
  97. outb (val, TCO1_CNT + 1);
  98. val = inb (TCO1_CNT + 1);
  99. spin_unlock(&tco_lock);
  100. if (val & 0x08)
  101. return -1;
  102. return 0;
  103. }
  104. /*
  105.  * Stop the timer countdown
  106.  */
  107. static int tco_timer_stop (void)
  108. {
  109. unsigned char val;
  110. spin_lock(&tco_lock);
  111. val = inb (TCO1_CNT + 1);
  112. val |= 0x08;
  113. outb (val, TCO1_CNT + 1);
  114. val = inb (TCO1_CNT + 1);
  115. spin_unlock(&tco_lock);
  116. if ((val & 0x08) == 0)
  117. return -1;
  118. return 0;
  119. }
  120. /*
  121.  * Set the timer reload value
  122.  */
  123. static int tco_timer_settimer (unsigned char tmrval)
  124. {
  125. unsigned char val;
  126. /* from the specs: */
  127. /* "Values of 0h-3h are ignored and should not be attempted" */
  128. if (tmrval > 0x3f || tmrval < 0x04)
  129. return -1;
  130. spin_lock(&tco_lock);
  131. val = inb (TCO1_TMR);
  132. val &= 0xc0;
  133. val |= tmrval;
  134. outb (val, TCO1_TMR);
  135. val = inb (TCO1_TMR);
  136. spin_unlock(&tco_lock);
  137. if ((val & 0x3f) != tmrval)
  138. return -1;
  139. return 0;
  140. }
  141. /*
  142.  * Reload (trigger) the timer. Lock is needed so we dont reload it during
  143.  * a reprogramming event
  144.  */
  145.  
  146. static void tco_timer_reload (void)
  147. {
  148. spin_lock(&tco_lock);
  149. outb (0x01, TCO1_RLD);
  150. spin_unlock(&tco_lock);
  151. }
  152. /*
  153.  * Allow only one person to hold it open
  154.  */
  155. static int i810tco_open (struct inode *inode, struct file *file)
  156. {
  157. if (test_and_set_bit(0, &timer_alive))
  158. return -EBUSY;
  159. /*
  160.  *      Reload and activate timer
  161.  */
  162. tco_timer_reload ();
  163. tco_timer_start ();
  164. return 0;
  165. }
  166. static int i810tco_release (struct inode *inode, struct file *file)
  167. {
  168. /*
  169.  *      Shut off the timer.
  170.  */
  171. if (tco_expect_close == 42 && !nowayout) {
  172. tco_timer_stop ();
  173. } else {
  174. tco_timer_reload ();
  175. printk(KERN_CRIT TCO_MODULE_NAME ": Unexpected close, not stopping watchdog!n");
  176. }
  177. clear_bit(0, &timer_alive);
  178. tco_expect_close = 0;
  179. return 0;
  180. }
  181. static ssize_t i810tco_write (struct file *file, const char *data,
  182.       size_t len, loff_t * ppos)
  183. {
  184. /*  Can't seek (pwrite) on this device  */
  185. if (ppos != &file->f_pos)
  186. return -ESPIPE;
  187. /* See if we got the magic character 'V' and reload the timer */
  188. if (len) {
  189. size_t i;
  190. tco_expect_close = 0;
  191. /* scan to see wether or not we got the magic character */
  192. for (i = 0; i != len; i++) {
  193. u8 c;
  194. if(get_user(c, data+i))
  195. return -EFAULT;
  196. if (c == 'V')
  197. tco_expect_close = 42;
  198. }
  199. /* someone wrote to us, we should reload the timer */
  200. tco_timer_reload ();
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. static int i810tco_ioctl (struct inode *inode, struct file *file,
  206.   unsigned int cmd, unsigned long arg)
  207. {
  208. int new_margin, u_margin;
  209. int options, retval = -EINVAL;
  210. static struct watchdog_info ident = {
  211. options: WDIOF_SETTIMEOUT |
  212. WDIOF_KEEPALIVEPING |
  213. WDIOF_MAGICCLOSE,
  214. firmware_version: 0,
  215. identity: "i810 TCO timer",
  216. };
  217. switch (cmd) {
  218. default:
  219. return -ENOTTY;
  220. case WDIOC_GETSUPPORT:
  221. if (copy_to_user
  222.     ((struct watchdog_info *) arg, &ident, sizeof (ident)))
  223. return -EFAULT;
  224. return 0;
  225. case WDIOC_GETSTATUS:
  226. case WDIOC_GETBOOTSTATUS:
  227. return put_user (0, (int *) arg);
  228. case WDIOC_SETOPTIONS:
  229. if (get_user (options, (int *) arg))
  230. return -EFAULT;
  231. if (options & WDIOS_DISABLECARD) {
  232. tco_timer_stop ();
  233. retval = 0;
  234. }
  235. if (options & WDIOS_ENABLECARD) {
  236. tco_timer_reload ();
  237. tco_timer_start ();
  238. retval = 0;
  239. }
  240. return retval;
  241. case WDIOC_KEEPALIVE:
  242. tco_timer_reload ();
  243. return 0;
  244. case WDIOC_SETTIMEOUT:
  245. if (get_user (u_margin, (int *) arg))
  246. return -EFAULT;
  247. new_margin = (u_margin * 10 + 5) / 6;
  248. if ((new_margin < 4) || (new_margin > 63))
  249. return -EINVAL;
  250. if (tco_timer_settimer ((unsigned char) new_margin))
  251.     return -EINVAL;
  252. i810_margin = new_margin;
  253. tco_timer_reload ();
  254. /* Fall */
  255. case WDIOC_GETTIMEOUT:
  256. return put_user ((int)(i810_margin * 6 / 10), (int *) arg);
  257. }
  258. }
  259. /*
  260.  * Data for PCI driver interface
  261.  *
  262.  * This data only exists for exporting the supported
  263.  * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
  264.  * register a pci_driver, because someone else might one day
  265.  * want to register another driver on the same PCI id.
  266.  */
  267. static struct pci_device_id i810tco_pci_tbl[] __initdata = {
  268. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
  269. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
  270. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
  271. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
  272. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
  273. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
  274. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
  275. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
  276. { 0, },
  277. };
  278. MODULE_DEVICE_TABLE (pci, i810tco_pci_tbl);
  279. static struct pci_dev *i810tco_pci;
  280. static unsigned char __init i810tco_getdevice (void)
  281. {
  282. struct pci_dev *dev;
  283. u8 val1, val2;
  284. u16 badr;
  285. /*
  286.  *      Find the PCI device
  287.  */
  288. pci_for_each_dev(dev) {
  289. if (pci_match_device(i810tco_pci_tbl, dev)) {
  290. i810tco_pci = dev;
  291. break;
  292. }
  293. }
  294. if (i810tco_pci) {
  295. /*
  296.  *      Find the ACPI base I/O address which is the base
  297.  *      for the TCO registers (TCOBASE=ACPIBASE + 0x60)
  298.  *      ACPIBASE is bits [15:7] from 0x40-0x43
  299.  */
  300. pci_read_config_byte (i810tco_pci, 0x40, &val1);
  301. pci_read_config_byte (i810tco_pci, 0x41, &val2);
  302. badr = ((val2 << 1) | (val1 >> 7)) << 7;
  303. ACPIBASE = badr;
  304. /* Something's wrong here, ACPIBASE has to be set */
  305. if (badr == 0x0001 || badr == 0x0000) {
  306. printk (KERN_ERR TCO_MODULE_NAME " init: failed to get TCOBASE addressn");
  307. return 0;
  308. }
  309. /*
  310.  * Check chipset's NO_REBOOT bit
  311.  */
  312. pci_read_config_byte (i810tco_pci, 0xd4, &val1);
  313. if (val1 & 0x02) {
  314. val1 &= 0xfd;
  315. pci_write_config_byte (i810tco_pci, 0xd4, val1);
  316. pci_read_config_byte (i810tco_pci, 0xd4, &val1);
  317. if (val1 & 0x02) {
  318. printk (KERN_ERR TCO_MODULE_NAME " init: failed to reset NO_REBOOT flag, reboot disabled by hardwaren");
  319. return 0; /* Cannot reset NO_REBOOT bit */
  320. }
  321. }
  322. /* Set the TCO_EN bit in SMI_EN register */
  323. val1 = inb (SMI_EN + 1);
  324. val1 &= 0xdf;
  325. outb (val1, SMI_EN + 1);
  326. /* Clear out the (probably old) status */
  327. outb (0, TCO1_STS);
  328. outb (3, TCO2_STS);
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static struct file_operations i810tco_fops = {
  334. owner: THIS_MODULE,
  335. write: i810tco_write,
  336. ioctl: i810tco_ioctl,
  337. open: i810tco_open,
  338. release: i810tco_release,
  339. };
  340. static struct miscdevice i810tco_miscdev = {
  341. minor: WATCHDOG_MINOR,
  342. name: "watchdog",
  343. fops: &i810tco_fops,
  344. };
  345. static int __init watchdog_init (void)
  346. {
  347. spin_lock_init(&tco_lock);
  348. if (!i810tco_getdevice () || i810tco_pci == NULL)
  349. return -ENODEV;
  350. if (!request_region (TCOBASE, 0x10, "i810 TCO")) {
  351. printk (KERN_ERR TCO_MODULE_NAME
  352. ": I/O address 0x%04x already in usen",
  353. TCOBASE);
  354. return -EIO;
  355. }
  356. if (misc_register (&i810tco_miscdev) != 0) {
  357. release_region (TCOBASE, 0x10);
  358. printk (KERN_ERR TCO_MODULE_NAME ": cannot register miscdevn");
  359. return -EIO;
  360. }
  361. tco_timer_settimer ((unsigned char) i810_margin);
  362. tco_timer_reload ();
  363. printk (KERN_INFO TCO_DRIVER_NAME
  364. ": timer margin: %d sec (0x%04x) (nowayout=%d)n",
  365. (int) (i810_margin * 6 / 10), TCOBASE, nowayout);
  366. return 0;
  367. }
  368. static void __exit watchdog_cleanup (void)
  369. {
  370. u8 val;
  371. /* Reset the timer before we leave */
  372. tco_timer_reload ();
  373. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  374. pci_read_config_byte (i810tco_pci, 0xd4, &val);
  375. val |= 0x02;
  376. pci_write_config_byte (i810tco_pci, 0xd4, val);
  377. release_region (TCOBASE, 0x10);
  378. misc_deregister (&i810tco_miscdev);
  379. }
  380. module_init(watchdog_init);
  381. module_exit(watchdog_cleanup);
  382. MODULE_AUTHOR("Nils Faerber");
  383. MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
  384. MODULE_LICENSE("GPL");
  385. EXPORT_NO_SYMBOLS;