iTCO_wdt.c 25 KB

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