iTCO_wdt.c 23 KB

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