iTCO_wdt.c 23 KB

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