iTCO_wdt.c 25 KB

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