iTCO_wdt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * intel TCO Watchdog Driver (Used in i82801 and i6300ESB chipsets)
  3. *
  4. * (c) Copyright 2006 Wim Van Sebroeck <wim@iguana.be>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
  12. * provide warranty for any of this software. This material is
  13. * provided "AS-IS" and at no charge.
  14. *
  15. * The TCO watchdog is implemented in the following I/O controller hubs:
  16. * (See the intel documentation on http://developer.intel.com.)
  17. * 82801AA (ICH) : document number 290655-003, 290677-014,
  18. * 82801AB (ICHO) : document number 290655-003, 290677-014,
  19. * 82801BA (ICH2) : document number 290687-002, 298242-027,
  20. * 82801BAM (ICH2-M) : document number 290687-002, 298242-027,
  21. * 82801CA (ICH3-S) : document number 290733-003, 290739-013,
  22. * 82801CAM (ICH3-M) : document number 290716-001, 290718-007,
  23. * 82801DB (ICH4) : document number 290744-001, 290745-020,
  24. * 82801DBM (ICH4-M) : document number 252337-001, 252663-005,
  25. * 82801E (C-ICH) : document number 273599-001, 273645-002,
  26. * 82801EB (ICH5) : document number 252516-001, 252517-003,
  27. * 82801ER (ICH5R) : document number 252516-001, 252517-003,
  28. * 82801FB (ICH6) : document number 301473-002, 301474-007,
  29. * 82801FR (ICH6R) : document number 301473-002, 301474-007,
  30. * 82801FBM (ICH6-M) : document number 301473-002, 301474-007,
  31. * 82801FW (ICH6W) : document number 301473-001, 301474-007,
  32. * 82801FRW (ICH6RW) : document number 301473-001, 301474-007,
  33. * 82801GB (ICH7) : document number 307013-002, 307014-009,
  34. * 82801GR (ICH7R) : document number 307013-002, 307014-009,
  35. * 82801GDH (ICH7DH) : document number 307013-002, 307014-009,
  36. * 82801GBM (ICH7-M) : document number 307013-002, 307014-009,
  37. * 82801GHM (ICH7-M DH) : document number 307013-002, 307014-009,
  38. * 6300ESB (6300ESB) : document number 300641-003
  39. */
  40. /*
  41. * Includes, defines, variables, module parameters, ...
  42. */
  43. /* Module and version information */
  44. #define DRV_NAME "iTCO_wdt"
  45. #define DRV_VERSION "1.00"
  46. #define DRV_RELDATE "21-May-2006"
  47. #define PFX DRV_NAME ": "
  48. /* Includes */
  49. #include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
  50. #include <linux/module.h> /* For module specific items */
  51. #include <linux/moduleparam.h> /* For new moduleparam's */
  52. #include <linux/types.h> /* For standard types (like size_t) */
  53. #include <linux/errno.h> /* For the -ENODEV/... values */
  54. #include <linux/kernel.h> /* For printk/panic/... */
  55. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
  56. #include <linux/watchdog.h> /* For the watchdog specific items */
  57. #include <linux/notifier.h> /* For notifier support */
  58. #include <linux/reboot.h> /* For reboot_notifier stuff */
  59. #include <linux/init.h> /* For __init/__exit/... */
  60. #include <linux/fs.h> /* For file operations */
  61. #include <linux/pci.h> /* For pci functions */
  62. #include <linux/ioport.h> /* For io-port access */
  63. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  64. #include <asm/uaccess.h> /* For copy_to_user/put_user/... */
  65. #include <asm/io.h> /* For inb/outb/... */
  66. /* TCO related info */
  67. enum iTCO_chipsets {
  68. TCO_ICH = 0, /* ICH */
  69. TCO_ICH0, /* ICH0 */
  70. TCO_ICH2, /* ICH2 */
  71. TCO_ICH2M, /* ICH2-M */
  72. TCO_ICH3, /* ICH3-S */
  73. TCO_ICH3M, /* ICH3-M */
  74. TCO_ICH4, /* ICH4 */
  75. TCO_ICH4M, /* ICH4-M */
  76. TCO_CICH, /* C-ICH */
  77. TCO_ICH5, /* ICH5 & ICH5R */
  78. TCO_6300ESB, /* 6300ESB */
  79. TCO_ICH6, /* ICH6 & ICH6R */
  80. TCO_ICH6M, /* ICH6-M */
  81. TCO_ICH6W, /* ICH6W & ICH6RW */
  82. TCO_ICH7, /* ICH7 & ICH7R */
  83. TCO_ICH7M, /* ICH7-M */
  84. TCO_ICH7MDH, /* ICH7-M DH */
  85. };
  86. static struct {
  87. char *name;
  88. unsigned int iTCO_version;
  89. } iTCO_chipset_info[] __devinitdata = {
  90. {"ICH", 1},
  91. {"ICH0", 1},
  92. {"ICH2", 1},
  93. {"ICH2-M", 1},
  94. {"ICH3-S", 1},
  95. {"ICH3-M", 1},
  96. {"ICH4", 1},
  97. {"ICH4-M", 1},
  98. {"C-ICH", 1},
  99. {"ICH5 or ICH5R", 1},
  100. {"6300ESB", 1},
  101. {"ICH6 or ICH6R", 2},
  102. {"ICH6-M", 2},
  103. {"ICH6W or ICH6RW", 2},
  104. {"ICH7 or ICH7R", 2},
  105. {"ICH7-M", 2},
  106. {"ICH7-M DH", 2},
  107. {NULL,0}
  108. };
  109. /*
  110. * This data only exists for exporting the supported PCI ids
  111. * via MODULE_DEVICE_TABLE. We do not actually register a
  112. * pci_driver, because the I/O Controller Hub has also other
  113. * functions that probably will be registered by other drivers.
  114. */
  115. static struct pci_device_id iTCO_wdt_pci_tbl[] = {
  116. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH },
  117. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH0 },
  118. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2 },
  119. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2M },
  120. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3 },
  121. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3M },
  122. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4 },
  123. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4M },
  124. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_CICH },
  125. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH5 },
  126. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_6300ESB },
  127. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6 },
  128. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6M },
  129. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6W },
  130. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7 },
  131. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7M },
  132. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_31, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7MDH },
  133. { 0, }, /* End of list */
  134. };
  135. MODULE_DEVICE_TABLE (pci, iTCO_wdt_pci_tbl);
  136. /* Address definitions for the TCO */
  137. #define TCOBASE iTCO_wdt_private.ACPIBASE + 0x60 /* TCO base address */
  138. #define SMI_EN iTCO_wdt_private.ACPIBASE + 0x30 /* SMI Control and Enable Register */
  139. #define TCO_RLD TCOBASE + 0x00 /* TCO Timer Reload and Current Value */
  140. #define TCOv1_TMR TCOBASE + 0x01 /* TCOv1 Timer Initial Value */
  141. #define TCO_DAT_IN TCOBASE + 0x02 /* TCO Data In Register */
  142. #define TCO_DAT_OUT TCOBASE + 0x03 /* TCO Data Out Register */
  143. #define TCO1_STS TCOBASE + 0x04 /* TCO1 Status Register */
  144. #define TCO2_STS TCOBASE + 0x06 /* TCO2 Status Register */
  145. #define TCO1_CNT TCOBASE + 0x08 /* TCO1 Control Register */
  146. #define TCO2_CNT TCOBASE + 0x0a /* TCO2 Control Register */
  147. #define TCOv2_TMR TCOBASE + 0x12 /* TCOv2 Timer Initial Value */
  148. /* internal variables */
  149. static unsigned long is_active;
  150. static char expect_release;
  151. static struct { /* this is private data for the iTCO_wdt device */
  152. unsigned int iTCO_version; /* TCO version/generation */
  153. unsigned long ACPIBASE; /* The cards ACPIBASE address (TCOBASE = ACPIBASE+0x60) */
  154. unsigned long __iomem *gcs; /* NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2) */
  155. spinlock_t io_lock; /* the lock for io operations */
  156. struct pci_dev *pdev; /* the PCI-device */
  157. } iTCO_wdt_private;
  158. /* module parameters */
  159. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
  160. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  161. module_param(heartbeat, int, 0);
  162. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39 (TCO v1) or 613 (TCO v2), default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  163. static int nowayout = WATCHDOG_NOWAYOUT;
  164. module_param(nowayout, int, 0);
  165. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  166. /*
  167. * Some TCO specific functions
  168. */
  169. static inline unsigned int seconds_to_ticks(int seconds)
  170. {
  171. /* the internal timer is stored as ticks which decrement
  172. * every 0.6 seconds */
  173. return (seconds * 10) / 6;
  174. }
  175. static void iTCO_wdt_set_NO_REBOOT_bit(void)
  176. {
  177. u32 val32;
  178. /* Set the NO_REBOOT bit: this disables reboots */
  179. if (iTCO_wdt_private.iTCO_version == 2) {
  180. val32 = readl(iTCO_wdt_private.gcs);
  181. val32 |= 0x00000020;
  182. writel(val32, iTCO_wdt_private.gcs);
  183. } else if (iTCO_wdt_private.iTCO_version == 1) {
  184. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  185. val32 |= 0x00000002;
  186. pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
  187. }
  188. }
  189. static int iTCO_wdt_unset_NO_REBOOT_bit(void)
  190. {
  191. int ret = 0;
  192. u32 val32;
  193. /* Unset the NO_REBOOT bit: this enables reboots */
  194. if (iTCO_wdt_private.iTCO_version == 2) {
  195. val32 = readl(iTCO_wdt_private.gcs);
  196. val32 &= 0xffffffdf;
  197. writel(val32, iTCO_wdt_private.gcs);
  198. val32 = readl(iTCO_wdt_private.gcs);
  199. if (val32 & 0x00000020)
  200. ret = -EIO;
  201. } else if (iTCO_wdt_private.iTCO_version == 1) {
  202. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  203. val32 &= 0xfffffffd;
  204. pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
  205. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  206. if (val32 & 0x00000002)
  207. ret = -EIO;
  208. }
  209. return ret; /* returns: 0 = OK, -EIO = Error */
  210. }
  211. static int iTCO_wdt_start(void)
  212. {
  213. unsigned int val;
  214. spin_lock(&iTCO_wdt_private.io_lock);
  215. /* disable chipset's NO_REBOOT bit */
  216. if (iTCO_wdt_unset_NO_REBOOT_bit()) {
  217. printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
  218. return -EIO;
  219. }
  220. /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
  221. val = inw(TCO1_CNT);
  222. val &= 0xf7ff;
  223. outw(val, TCO1_CNT);
  224. val = inw(TCO1_CNT);
  225. spin_unlock(&iTCO_wdt_private.io_lock);
  226. if (val & 0x0800)
  227. return -1;
  228. return 0;
  229. }
  230. static int iTCO_wdt_stop(void)
  231. {
  232. unsigned int val;
  233. spin_lock(&iTCO_wdt_private.io_lock);
  234. /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
  235. val = inw(TCO1_CNT);
  236. val |= 0x0800;
  237. outw(val, TCO1_CNT);
  238. val = inw(TCO1_CNT);
  239. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  240. iTCO_wdt_set_NO_REBOOT_bit();
  241. spin_unlock(&iTCO_wdt_private.io_lock);
  242. if ((val & 0x0800) == 0)
  243. return -1;
  244. return 0;
  245. }
  246. static int iTCO_wdt_keepalive(void)
  247. {
  248. spin_lock(&iTCO_wdt_private.io_lock);
  249. /* Reload the timer by writing to the TCO Timer Counter register */
  250. if (iTCO_wdt_private.iTCO_version == 2) {
  251. outw(0x01, TCO_RLD);
  252. } else if (iTCO_wdt_private.iTCO_version == 1) {
  253. outb(0x01, TCO_RLD);
  254. }
  255. spin_unlock(&iTCO_wdt_private.io_lock);
  256. return 0;
  257. }
  258. static int iTCO_wdt_set_heartbeat(int t)
  259. {
  260. unsigned int val16;
  261. unsigned char val8;
  262. unsigned int tmrval;
  263. tmrval = seconds_to_ticks(t);
  264. /* from the specs: */
  265. /* "Values of 0h-3h are ignored and should not be attempted" */
  266. if (tmrval < 0x04)
  267. return -EINVAL;
  268. if (((iTCO_wdt_private.iTCO_version == 2) && (tmrval > 0x3ff)) ||
  269. ((iTCO_wdt_private.iTCO_version == 1) && (tmrval > 0x03f)))
  270. return -EINVAL;
  271. /* Write new heartbeat to watchdog */
  272. if (iTCO_wdt_private.iTCO_version == 2) {
  273. spin_lock(&iTCO_wdt_private.io_lock);
  274. val16 = inw(TCOv2_TMR);
  275. val16 &= 0xfc00;
  276. val16 |= tmrval;
  277. outw(val16, TCOv2_TMR);
  278. val16 = inw(TCOv2_TMR);
  279. spin_unlock(&iTCO_wdt_private.io_lock);
  280. if ((val16 & 0x3ff) != tmrval)
  281. return -EINVAL;
  282. } else if (iTCO_wdt_private.iTCO_version == 1) {
  283. spin_lock(&iTCO_wdt_private.io_lock);
  284. val8 = inb(TCOv1_TMR);
  285. val8 &= 0xc0;
  286. val8 |= (tmrval & 0xff);
  287. outb(val8, TCOv1_TMR);
  288. val8 = inb(TCOv1_TMR);
  289. spin_unlock(&iTCO_wdt_private.io_lock);
  290. if ((val8 & 0x3f) != tmrval)
  291. return -EINVAL;
  292. }
  293. heartbeat = t;
  294. return 0;
  295. }
  296. static int iTCO_wdt_get_timeleft (int *time_left)
  297. {
  298. unsigned int val16;
  299. unsigned char val8;
  300. /* read the TCO Timer */
  301. if (iTCO_wdt_private.iTCO_version == 2) {
  302. spin_lock(&iTCO_wdt_private.io_lock);
  303. val16 = inw(TCO_RLD);
  304. val16 &= 0x3ff;
  305. spin_unlock(&iTCO_wdt_private.io_lock);
  306. *time_left = (val16 * 6) / 10;
  307. } else if (iTCO_wdt_private.iTCO_version == 1) {
  308. spin_lock(&iTCO_wdt_private.io_lock);
  309. val8 = inb(TCO_RLD);
  310. val8 &= 0x3f;
  311. spin_unlock(&iTCO_wdt_private.io_lock);
  312. *time_left = (val8 * 6) / 10;
  313. }
  314. return 0;
  315. }
  316. /*
  317. * /dev/watchdog handling
  318. */
  319. static int iTCO_wdt_open (struct inode *inode, struct file *file)
  320. {
  321. /* /dev/watchdog can only be opened once */
  322. if (test_and_set_bit(0, &is_active))
  323. return -EBUSY;
  324. /*
  325. * Reload and activate timer
  326. */
  327. iTCO_wdt_keepalive();
  328. iTCO_wdt_start();
  329. return nonseekable_open(inode, file);
  330. }
  331. static int iTCO_wdt_release (struct inode *inode, struct file *file)
  332. {
  333. /*
  334. * Shut off the timer.
  335. */
  336. if (expect_release == 42) {
  337. iTCO_wdt_stop();
  338. } else {
  339. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  340. iTCO_wdt_keepalive();
  341. }
  342. clear_bit(0, &is_active);
  343. expect_release = 0;
  344. return 0;
  345. }
  346. static ssize_t iTCO_wdt_write (struct file *file, const char __user *data,
  347. size_t len, loff_t * ppos)
  348. {
  349. /* See if we got the magic character 'V' and reload the timer */
  350. if (len) {
  351. if (!nowayout) {
  352. size_t i;
  353. /* note: just in case someone wrote the magic character
  354. * five months ago... */
  355. expect_release = 0;
  356. /* scan to see whether or not we got the magic character */
  357. for (i = 0; i != len; i++) {
  358. char c;
  359. if (get_user(c, data+i))
  360. return -EFAULT;
  361. if (c == 'V')
  362. expect_release = 42;
  363. }
  364. }
  365. /* someone wrote to us, we should reload the timer */
  366. iTCO_wdt_keepalive();
  367. }
  368. return len;
  369. }
  370. static int iTCO_wdt_ioctl (struct inode *inode, struct file *file,
  371. unsigned int cmd, unsigned long arg)
  372. {
  373. int new_options, retval = -EINVAL;
  374. int new_heartbeat;
  375. int time_left;
  376. void __user *argp = (void __user *)arg;
  377. int __user *p = argp;
  378. static struct watchdog_info ident = {
  379. .options = WDIOF_SETTIMEOUT |
  380. WDIOF_KEEPALIVEPING |
  381. WDIOF_MAGICCLOSE,
  382. .firmware_version = 0,
  383. .identity = DRV_NAME,
  384. };
  385. switch (cmd) {
  386. case WDIOC_GETSUPPORT:
  387. return copy_to_user(argp, &ident,
  388. sizeof (ident)) ? -EFAULT : 0;
  389. case WDIOC_GETSTATUS:
  390. case WDIOC_GETBOOTSTATUS:
  391. return put_user(0, p);
  392. case WDIOC_KEEPALIVE:
  393. iTCO_wdt_keepalive();
  394. return 0;
  395. case WDIOC_SETOPTIONS:
  396. {
  397. if (get_user(new_options, p))
  398. return -EFAULT;
  399. if (new_options & WDIOS_DISABLECARD) {
  400. iTCO_wdt_stop();
  401. retval = 0;
  402. }
  403. if (new_options & WDIOS_ENABLECARD) {
  404. iTCO_wdt_keepalive();
  405. iTCO_wdt_start();
  406. retval = 0;
  407. }
  408. return retval;
  409. }
  410. case WDIOC_SETTIMEOUT:
  411. {
  412. if (get_user(new_heartbeat, p))
  413. return -EFAULT;
  414. if (iTCO_wdt_set_heartbeat(new_heartbeat))
  415. return -EINVAL;
  416. iTCO_wdt_keepalive();
  417. /* Fall */
  418. }
  419. case WDIOC_GETTIMEOUT:
  420. return put_user(heartbeat, p);
  421. case WDIOC_GETTIMELEFT:
  422. {
  423. if (iTCO_wdt_get_timeleft(&time_left))
  424. return -EINVAL;
  425. return put_user(time_left, p);
  426. }
  427. default:
  428. return -ENOIOCTLCMD;
  429. }
  430. }
  431. /*
  432. * Notify system
  433. */
  434. static int iTCO_wdt_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
  435. {
  436. if (code==SYS_DOWN || code==SYS_HALT) {
  437. /* Turn the WDT off */
  438. iTCO_wdt_stop();
  439. }
  440. return NOTIFY_DONE;
  441. }
  442. /*
  443. * Kernel Interfaces
  444. */
  445. static struct file_operations iTCO_wdt_fops = {
  446. .owner = THIS_MODULE,
  447. .llseek = no_llseek,
  448. .write = iTCO_wdt_write,
  449. .ioctl = iTCO_wdt_ioctl,
  450. .open = iTCO_wdt_open,
  451. .release = iTCO_wdt_release,
  452. };
  453. static struct miscdevice iTCO_wdt_miscdev = {
  454. .minor = WATCHDOG_MINOR,
  455. .name = "watchdog",
  456. .fops = &iTCO_wdt_fops,
  457. };
  458. static struct notifier_block iTCO_wdt_notifier = {
  459. .notifier_call = iTCO_wdt_notify_sys,
  460. };
  461. /*
  462. * Init & exit routines
  463. */
  464. static int __init iTCO_wdt_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  465. {
  466. int ret;
  467. u32 base_address;
  468. unsigned long RCBA;
  469. unsigned long val32;
  470. /*
  471. * Find the ACPI/PM base I/O address which is the base
  472. * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
  473. * ACPIBASE is bits [15:7] from 0x40-0x43
  474. */
  475. pci_read_config_dword(pdev, 0x40, &base_address);
  476. base_address &= 0x00007f80;
  477. if (base_address == 0x00000000) {
  478. /* Something's wrong here, ACPIBASE has to be set */
  479. printk(KERN_ERR PFX "failed to get TCOBASE address\n");
  480. return -ENODEV;
  481. }
  482. iTCO_wdt_private.iTCO_version = iTCO_chipset_info[ent->driver_data].iTCO_version;
  483. iTCO_wdt_private.ACPIBASE = base_address;
  484. iTCO_wdt_private.pdev = pdev;
  485. /* Get the Memory-Mapped GCS register, we need it for the NO_REBOOT flag (TCO v2) */
  486. /* To get access to it you have to read RCBA from PCI Config space 0xf0
  487. and use it as base. GCS = RCBA + ICH6_GCS(0x3410). */
  488. if (iTCO_wdt_private.iTCO_version == 2) {
  489. pci_read_config_dword(pdev, 0xf0, &base_address);
  490. RCBA = base_address & 0xffffc000;
  491. iTCO_wdt_private.gcs = ioremap((RCBA + 0x3410),4);
  492. }
  493. /* Check chipset's NO_REBOOT bit */
  494. if (iTCO_wdt_unset_NO_REBOOT_bit()) {
  495. printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
  496. ret = -ENODEV; /* Cannot reset NO_REBOOT bit */
  497. goto out;
  498. }
  499. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  500. iTCO_wdt_set_NO_REBOOT_bit();
  501. /* Set the TCO_EN bit in SMI_EN register */
  502. if (!request_region(SMI_EN, 4, "iTCO_wdt")) {
  503. printk(KERN_ERR PFX "I/O address 0x%04lx already in use\n",
  504. SMI_EN );
  505. ret = -EIO;
  506. goto out;
  507. }
  508. val32 = inl(SMI_EN);
  509. val32 &= 0xffffdfff; /* Turn off SMI clearing watchdog */
  510. outl(val32, SMI_EN);
  511. release_region(SMI_EN, 4);
  512. /* The TCO I/O registers reside in a 32-byte range pointed to by the TCOBASE value */
  513. if (!request_region (TCOBASE, 0x20, "iTCO_wdt")) {
  514. printk (KERN_ERR PFX "I/O address 0x%04lx already in use\n",
  515. TCOBASE);
  516. ret = -EIO;
  517. goto out;
  518. }
  519. printk(KERN_INFO PFX "Found a %s TCO device (Version=%d, TCOBASE=0x%04lx)\n",
  520. iTCO_chipset_info[ent->driver_data].name,
  521. iTCO_chipset_info[ent->driver_data].iTCO_version,
  522. TCOBASE);
  523. /* Clear out the (probably old) status */
  524. outb(0, TCO1_STS);
  525. outb(3, TCO2_STS);
  526. /* Make sure the watchdog is not running */
  527. iTCO_wdt_stop();
  528. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  529. if (iTCO_wdt_set_heartbeat(heartbeat)) {
  530. iTCO_wdt_set_heartbeat(WATCHDOG_HEARTBEAT);
  531. printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39 (TCO v1) or 613 (TCO v2), using %d\n",
  532. heartbeat);
  533. }
  534. ret = register_reboot_notifier(&iTCO_wdt_notifier);
  535. if (ret != 0) {
  536. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  537. ret);
  538. goto unreg_region;
  539. }
  540. ret = misc_register(&iTCO_wdt_miscdev);
  541. if (ret != 0) {
  542. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  543. WATCHDOG_MINOR, ret);
  544. goto unreg_notifier;
  545. }
  546. printk (KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  547. heartbeat, nowayout);
  548. return 0;
  549. unreg_notifier:
  550. unregister_reboot_notifier(&iTCO_wdt_notifier);
  551. unreg_region:
  552. release_region (TCOBASE, 0x20);
  553. out:
  554. if (iTCO_wdt_private.iTCO_version == 2)
  555. iounmap(iTCO_wdt_private.gcs);
  556. iTCO_wdt_private.ACPIBASE = 0;
  557. return ret;
  558. }
  559. static void __exit iTCO_wdt_cleanup(void)
  560. {
  561. /* Stop the timer before we leave */
  562. if (!nowayout)
  563. iTCO_wdt_stop();
  564. /* Deregister */
  565. misc_deregister(&iTCO_wdt_miscdev);
  566. unregister_reboot_notifier(&iTCO_wdt_notifier);
  567. release_region(TCOBASE, 0x20);
  568. if (iTCO_wdt_private.iTCO_version == 2)
  569. iounmap(iTCO_wdt_private.gcs);
  570. }
  571. static int __init iTCO_wdt_init_module(void)
  572. {
  573. int found = 0;
  574. struct pci_dev *pdev = NULL;
  575. const struct pci_device_id *ent;
  576. spin_lock_init(&iTCO_wdt_private.io_lock);
  577. for_each_pci_dev(pdev) {
  578. ent = pci_match_id(iTCO_wdt_pci_tbl, pdev);
  579. if (ent) {
  580. if (!(iTCO_wdt_init(pdev, ent))) {
  581. found++;
  582. break;
  583. }
  584. }
  585. }
  586. if (!found) {
  587. printk(KERN_INFO PFX "No card detected\n");
  588. return -ENODEV;
  589. }
  590. return 0;
  591. }
  592. static void __exit iTCO_wdt_cleanup_module(void)
  593. {
  594. if (iTCO_wdt_private.ACPIBASE)
  595. iTCO_wdt_cleanup();
  596. printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
  597. }
  598. module_init(iTCO_wdt_init_module);
  599. module_exit(iTCO_wdt_cleanup_module);
  600. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  601. MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
  602. MODULE_LICENSE("GPL");
  603. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);