iTCO_wdt.c 23 KB

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