iTCO_wdt.c 23 KB

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