iTCO_wdt.c 21 KB

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