iTCO_wdt.c 22 KB

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