iTCO_wdt.c 25 KB

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