iTCO_wdt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * intel TCO Watchdog Driver
  3. *
  4. * (c) Copyright 2006-2011 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 322896-001, 322897-001: NM10
  30. * document number 313056-003, 313057-017: 82801H (ICH8)
  31. * document number 316972-004, 316973-012: 82801I (ICH9)
  32. * document number 319973-002, 319974-002: 82801J (ICH10)
  33. * document number 322169-001, 322170-003: 5 Series, 3400 Series (PCH)
  34. * document number 320066-003, 320257-008: EP80597 (IICH)
  35. * document number 324645-001, 324646-001: Cougar Point (CPT)
  36. * document number TBD : Patsburg (PBG)
  37. * document number TBD : DH89xxCC
  38. * document number TBD : Panther Point
  39. * document number TBD : Lynx Point
  40. * document number TBD : Lynx Point-LP
  41. */
  42. /*
  43. * Includes, defines, variables, module parameters, ...
  44. */
  45. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  46. /* Module and version information */
  47. #define DRV_NAME "iTCO_wdt"
  48. #define DRV_VERSION "1.11"
  49. /* Includes */
  50. #include <linux/acpi.h> /* For ACPI support */
  51. #include <linux/module.h> /* For module specific items */
  52. #include <linux/moduleparam.h> /* For new moduleparam's */
  53. #include <linux/types.h> /* For standard types (like size_t) */
  54. #include <linux/errno.h> /* For the -ENODEV/... values */
  55. #include <linux/kernel.h> /* For printk/panic/... */
  56. #include <linux/watchdog.h> /* For the watchdog specific items */
  57. #include <linux/init.h> /* For __init/__exit/... */
  58. #include <linux/fs.h> /* For file operations */
  59. #include <linux/platform_device.h> /* For platform_driver framework */
  60. #include <linux/pci.h> /* For pci functions */
  61. #include <linux/ioport.h> /* For io-port access */
  62. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  63. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  64. #include <linux/io.h> /* For inb/outb/... */
  65. #include <linux/mfd/core.h>
  66. #include <linux/mfd/lpc_ich.h>
  67. #include "iTCO_vendor.h"
  68. /* Address definitions for the TCO */
  69. /* TCO base address */
  70. #define TCOBASE (iTCO_wdt_private.tco_res->start)
  71. /* SMI Control and Enable Register */
  72. #define SMI_EN (iTCO_wdt_private.smi_res->start)
  73. #define TCO_RLD (TCOBASE + 0x00) /* TCO Timer Reload and Curr. Value */
  74. #define TCOv1_TMR (TCOBASE + 0x01) /* TCOv1 Timer Initial Value */
  75. #define TCO_DAT_IN (TCOBASE + 0x02) /* TCO Data In Register */
  76. #define TCO_DAT_OUT (TCOBASE + 0x03) /* TCO Data Out Register */
  77. #define TCO1_STS (TCOBASE + 0x04) /* TCO1 Status Register */
  78. #define TCO2_STS (TCOBASE + 0x06) /* TCO2 Status Register */
  79. #define TCO1_CNT (TCOBASE + 0x08) /* TCO1 Control Register */
  80. #define TCO2_CNT (TCOBASE + 0x0a) /* TCO2 Control Register */
  81. #define TCOv2_TMR (TCOBASE + 0x12) /* TCOv2 Timer Initial Value */
  82. /* internal variables */
  83. static struct { /* this is private data for the iTCO_wdt device */
  84. /* TCO version/generation */
  85. unsigned int iTCO_version;
  86. struct resource *tco_res;
  87. struct resource *smi_res;
  88. /*
  89. * NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2),
  90. * or memory-mapped PMC register bit 4 (TCO version 3).
  91. */
  92. struct resource *gcs_pmc_res;
  93. unsigned long __iomem *gcs_pmc;
  94. /* the lock for io operations */
  95. spinlock_t io_lock;
  96. struct platform_device *dev;
  97. /* the PCI-device */
  98. struct pci_dev *pdev;
  99. /* whether or not the watchdog has been suspended */
  100. bool suspended;
  101. } iTCO_wdt_private;
  102. /* module parameters */
  103. #define WATCHDOG_TIMEOUT 30 /* 30 sec default heartbeat */
  104. static int heartbeat = WATCHDOG_TIMEOUT; /* in seconds */
  105. module_param(heartbeat, int, 0);
  106. MODULE_PARM_DESC(heartbeat, "Watchdog timeout in seconds. "
  107. "5..76 (TCO v1) or 3..614 (TCO v2), default="
  108. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  109. static bool nowayout = WATCHDOG_NOWAYOUT;
  110. module_param(nowayout, bool, 0);
  111. MODULE_PARM_DESC(nowayout,
  112. "Watchdog cannot be stopped once started (default="
  113. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  114. static int turn_SMI_watchdog_clear_off = 1;
  115. module_param(turn_SMI_watchdog_clear_off, int, 0);
  116. MODULE_PARM_DESC(turn_SMI_watchdog_clear_off,
  117. "Turn off SMI clearing watchdog (depends on TCO-version)(default=1)");
  118. /*
  119. * Some TCO specific functions
  120. */
  121. /*
  122. * The iTCO v1 and v2's internal timer is stored as ticks which decrement
  123. * every 0.6 seconds. v3's internal timer is stored as seconds (some
  124. * datasheets incorrectly state 0.6 seconds).
  125. */
  126. static inline unsigned int seconds_to_ticks(int secs)
  127. {
  128. return iTCO_wdt_private.iTCO_version == 3 ? secs : (secs * 10) / 6;
  129. }
  130. static inline unsigned int ticks_to_seconds(int ticks)
  131. {
  132. return iTCO_wdt_private.iTCO_version == 3 ? ticks : (ticks * 6) / 10;
  133. }
  134. static void iTCO_wdt_set_NO_REBOOT_bit(void)
  135. {
  136. u32 val32;
  137. /* Set the NO_REBOOT bit: this disables reboots */
  138. if (iTCO_wdt_private.iTCO_version == 3) {
  139. val32 = readl(iTCO_wdt_private.gcs_pmc);
  140. val32 |= 0x00000010;
  141. writel(val32, iTCO_wdt_private.gcs_pmc);
  142. } else if (iTCO_wdt_private.iTCO_version == 2) {
  143. val32 = readl(iTCO_wdt_private.gcs_pmc);
  144. val32 |= 0x00000020;
  145. writel(val32, iTCO_wdt_private.gcs_pmc);
  146. } else if (iTCO_wdt_private.iTCO_version == 1) {
  147. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  148. val32 |= 0x00000002;
  149. pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
  150. }
  151. }
  152. static int iTCO_wdt_unset_NO_REBOOT_bit(void)
  153. {
  154. int ret = 0;
  155. u32 val32;
  156. /* Unset the NO_REBOOT bit: this enables reboots */
  157. if (iTCO_wdt_private.iTCO_version == 3) {
  158. val32 = readl(iTCO_wdt_private.gcs_pmc);
  159. val32 &= 0xffffffef;
  160. writel(val32, iTCO_wdt_private.gcs_pmc);
  161. val32 = readl(iTCO_wdt_private.gcs_pmc);
  162. if (val32 & 0x00000010)
  163. ret = -EIO;
  164. } else if (iTCO_wdt_private.iTCO_version == 2) {
  165. val32 = readl(iTCO_wdt_private.gcs_pmc);
  166. val32 &= 0xffffffdf;
  167. writel(val32, iTCO_wdt_private.gcs_pmc);
  168. val32 = readl(iTCO_wdt_private.gcs_pmc);
  169. if (val32 & 0x00000020)
  170. ret = -EIO;
  171. } else if (iTCO_wdt_private.iTCO_version == 1) {
  172. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  173. val32 &= 0xfffffffd;
  174. pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
  175. pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
  176. if (val32 & 0x00000002)
  177. ret = -EIO;
  178. }
  179. return ret; /* returns: 0 = OK, -EIO = Error */
  180. }
  181. static int iTCO_wdt_start(struct watchdog_device *wd_dev)
  182. {
  183. unsigned int val;
  184. spin_lock(&iTCO_wdt_private.io_lock);
  185. iTCO_vendor_pre_start(iTCO_wdt_private.smi_res, wd_dev->timeout);
  186. /* disable chipset's NO_REBOOT bit */
  187. if (iTCO_wdt_unset_NO_REBOOT_bit()) {
  188. spin_unlock(&iTCO_wdt_private.io_lock);
  189. pr_err("failed to reset NO_REBOOT flag, reboot disabled by hardware/BIOS\n");
  190. return -EIO;
  191. }
  192. /* Force the timer to its reload value by writing to the TCO_RLD
  193. register */
  194. if (iTCO_wdt_private.iTCO_version >= 2)
  195. outw(0x01, TCO_RLD);
  196. else if (iTCO_wdt_private.iTCO_version == 1)
  197. outb(0x01, TCO_RLD);
  198. /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
  199. val = inw(TCO1_CNT);
  200. val &= 0xf7ff;
  201. outw(val, TCO1_CNT);
  202. val = inw(TCO1_CNT);
  203. spin_unlock(&iTCO_wdt_private.io_lock);
  204. if (val & 0x0800)
  205. return -1;
  206. return 0;
  207. }
  208. static int iTCO_wdt_stop(struct watchdog_device *wd_dev)
  209. {
  210. unsigned int val;
  211. spin_lock(&iTCO_wdt_private.io_lock);
  212. iTCO_vendor_pre_stop(iTCO_wdt_private.smi_res);
  213. /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
  214. val = inw(TCO1_CNT);
  215. val |= 0x0800;
  216. outw(val, TCO1_CNT);
  217. val = inw(TCO1_CNT);
  218. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  219. iTCO_wdt_set_NO_REBOOT_bit();
  220. spin_unlock(&iTCO_wdt_private.io_lock);
  221. if ((val & 0x0800) == 0)
  222. return -1;
  223. return 0;
  224. }
  225. static int iTCO_wdt_ping(struct watchdog_device *wd_dev)
  226. {
  227. spin_lock(&iTCO_wdt_private.io_lock);
  228. iTCO_vendor_pre_keepalive(iTCO_wdt_private.smi_res, wd_dev->timeout);
  229. /* Reload the timer by writing to the TCO Timer Counter register */
  230. if (iTCO_wdt_private.iTCO_version >= 2) {
  231. outw(0x01, TCO_RLD);
  232. } else if (iTCO_wdt_private.iTCO_version == 1) {
  233. /* Reset the timeout status bit so that the timer
  234. * needs to count down twice again before rebooting */
  235. outw(0x0008, TCO1_STS); /* write 1 to clear bit */
  236. outb(0x01, TCO_RLD);
  237. }
  238. spin_unlock(&iTCO_wdt_private.io_lock);
  239. return 0;
  240. }
  241. static int iTCO_wdt_set_timeout(struct watchdog_device *wd_dev, unsigned int t)
  242. {
  243. unsigned int val16;
  244. unsigned char val8;
  245. unsigned int tmrval;
  246. tmrval = seconds_to_ticks(t);
  247. /* For TCO v1 the timer counts down twice before rebooting */
  248. if (iTCO_wdt_private.iTCO_version == 1)
  249. tmrval /= 2;
  250. /* from the specs: */
  251. /* "Values of 0h-3h are ignored and should not be attempted" */
  252. if (tmrval < 0x04)
  253. return -EINVAL;
  254. if (((iTCO_wdt_private.iTCO_version >= 2) && (tmrval > 0x3ff)) ||
  255. ((iTCO_wdt_private.iTCO_version == 1) && (tmrval > 0x03f)))
  256. return -EINVAL;
  257. iTCO_vendor_pre_set_heartbeat(tmrval);
  258. /* Write new heartbeat to watchdog */
  259. if (iTCO_wdt_private.iTCO_version >= 2) {
  260. spin_lock(&iTCO_wdt_private.io_lock);
  261. val16 = inw(TCOv2_TMR);
  262. val16 &= 0xfc00;
  263. val16 |= tmrval;
  264. outw(val16, TCOv2_TMR);
  265. val16 = inw(TCOv2_TMR);
  266. spin_unlock(&iTCO_wdt_private.io_lock);
  267. if ((val16 & 0x3ff) != tmrval)
  268. return -EINVAL;
  269. } else if (iTCO_wdt_private.iTCO_version == 1) {
  270. spin_lock(&iTCO_wdt_private.io_lock);
  271. val8 = inb(TCOv1_TMR);
  272. val8 &= 0xc0;
  273. val8 |= (tmrval & 0xff);
  274. outb(val8, TCOv1_TMR);
  275. val8 = inb(TCOv1_TMR);
  276. spin_unlock(&iTCO_wdt_private.io_lock);
  277. if ((val8 & 0x3f) != tmrval)
  278. return -EINVAL;
  279. }
  280. wd_dev->timeout = t;
  281. return 0;
  282. }
  283. static unsigned int iTCO_wdt_get_timeleft(struct watchdog_device *wd_dev)
  284. {
  285. unsigned int val16;
  286. unsigned char val8;
  287. unsigned int time_left = 0;
  288. /* read the TCO Timer */
  289. if (iTCO_wdt_private.iTCO_version >= 2) {
  290. spin_lock(&iTCO_wdt_private.io_lock);
  291. val16 = inw(TCO_RLD);
  292. val16 &= 0x3ff;
  293. spin_unlock(&iTCO_wdt_private.io_lock);
  294. time_left = ticks_to_seconds(val16);
  295. } else if (iTCO_wdt_private.iTCO_version == 1) {
  296. spin_lock(&iTCO_wdt_private.io_lock);
  297. val8 = inb(TCO_RLD);
  298. val8 &= 0x3f;
  299. if (!(inw(TCO1_STS) & 0x0008))
  300. val8 += (inb(TCOv1_TMR) & 0x3f);
  301. spin_unlock(&iTCO_wdt_private.io_lock);
  302. time_left = ticks_to_seconds(val8);
  303. }
  304. return time_left;
  305. }
  306. /*
  307. * Kernel Interfaces
  308. */
  309. static const struct watchdog_info ident = {
  310. .options = WDIOF_SETTIMEOUT |
  311. WDIOF_KEEPALIVEPING |
  312. WDIOF_MAGICCLOSE,
  313. .firmware_version = 0,
  314. .identity = DRV_NAME,
  315. };
  316. static const struct watchdog_ops iTCO_wdt_ops = {
  317. .owner = THIS_MODULE,
  318. .start = iTCO_wdt_start,
  319. .stop = iTCO_wdt_stop,
  320. .ping = iTCO_wdt_ping,
  321. .set_timeout = iTCO_wdt_set_timeout,
  322. .get_timeleft = iTCO_wdt_get_timeleft,
  323. };
  324. static struct watchdog_device iTCO_wdt_watchdog_dev = {
  325. .info = &ident,
  326. .ops = &iTCO_wdt_ops,
  327. };
  328. /*
  329. * Init & exit routines
  330. */
  331. static void iTCO_wdt_cleanup(void)
  332. {
  333. /* Stop the timer before we leave */
  334. if (!nowayout)
  335. iTCO_wdt_stop(&iTCO_wdt_watchdog_dev);
  336. /* Deregister */
  337. watchdog_unregister_device(&iTCO_wdt_watchdog_dev);
  338. /* release resources */
  339. release_region(iTCO_wdt_private.tco_res->start,
  340. resource_size(iTCO_wdt_private.tco_res));
  341. release_region(iTCO_wdt_private.smi_res->start,
  342. resource_size(iTCO_wdt_private.smi_res));
  343. if (iTCO_wdt_private.iTCO_version >= 2) {
  344. iounmap(iTCO_wdt_private.gcs_pmc);
  345. release_mem_region(iTCO_wdt_private.gcs_pmc_res->start,
  346. resource_size(iTCO_wdt_private.gcs_pmc_res));
  347. }
  348. iTCO_wdt_private.tco_res = NULL;
  349. iTCO_wdt_private.smi_res = NULL;
  350. iTCO_wdt_private.gcs_pmc_res = NULL;
  351. iTCO_wdt_private.gcs_pmc = NULL;
  352. }
  353. static int iTCO_wdt_probe(struct platform_device *dev)
  354. {
  355. int ret = -ENODEV;
  356. unsigned long val32;
  357. struct lpc_ich_info *ich_info = dev_get_platdata(&dev->dev);
  358. if (!ich_info)
  359. goto out;
  360. spin_lock_init(&iTCO_wdt_private.io_lock);
  361. iTCO_wdt_private.tco_res =
  362. platform_get_resource(dev, IORESOURCE_IO, ICH_RES_IO_TCO);
  363. if (!iTCO_wdt_private.tco_res)
  364. goto out;
  365. iTCO_wdt_private.smi_res =
  366. platform_get_resource(dev, IORESOURCE_IO, ICH_RES_IO_SMI);
  367. if (!iTCO_wdt_private.smi_res)
  368. goto out;
  369. iTCO_wdt_private.iTCO_version = ich_info->iTCO_version;
  370. iTCO_wdt_private.dev = dev;
  371. iTCO_wdt_private.pdev = to_pci_dev(dev->dev.parent);
  372. /*
  373. * Get the Memory-Mapped GCS or PMC register, we need it for the
  374. * NO_REBOOT flag (TCO v2 and v3).
  375. */
  376. if (iTCO_wdt_private.iTCO_version >= 2) {
  377. iTCO_wdt_private.gcs_pmc_res = platform_get_resource(dev,
  378. IORESOURCE_MEM,
  379. ICH_RES_MEM_GCS_PMC);
  380. if (!iTCO_wdt_private.gcs_pmc_res)
  381. goto out;
  382. if (!request_mem_region(iTCO_wdt_private.gcs_pmc_res->start,
  383. resource_size(iTCO_wdt_private.gcs_pmc_res), dev->name)) {
  384. ret = -EBUSY;
  385. goto out;
  386. }
  387. iTCO_wdt_private.gcs_pmc = ioremap(iTCO_wdt_private.gcs_pmc_res->start,
  388. resource_size(iTCO_wdt_private.gcs_pmc_res));
  389. if (!iTCO_wdt_private.gcs_pmc) {
  390. ret = -EIO;
  391. goto unreg_gcs_pmc;
  392. }
  393. }
  394. /* Check chipset's NO_REBOOT bit */
  395. if (iTCO_wdt_unset_NO_REBOOT_bit() && iTCO_vendor_check_noreboot_on()) {
  396. pr_info("unable to reset NO_REBOOT flag, device disabled by hardware/BIOS\n");
  397. ret = -ENODEV; /* Cannot reset NO_REBOOT bit */
  398. goto unmap_gcs_pmc;
  399. }
  400. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  401. iTCO_wdt_set_NO_REBOOT_bit();
  402. /* The TCO logic uses the TCO_EN bit in the SMI_EN register */
  403. if (!request_region(iTCO_wdt_private.smi_res->start,
  404. resource_size(iTCO_wdt_private.smi_res), dev->name)) {
  405. pr_err("I/O address 0x%04llx already in use, device disabled\n",
  406. (u64)SMI_EN);
  407. ret = -EBUSY;
  408. goto unmap_gcs_pmc;
  409. }
  410. if (turn_SMI_watchdog_clear_off >= iTCO_wdt_private.iTCO_version) {
  411. /*
  412. * Bit 13: TCO_EN -> 0
  413. * Disables TCO logic generating an SMI#
  414. */
  415. val32 = inl(SMI_EN);
  416. val32 &= 0xffffdfff; /* Turn off SMI clearing watchdog */
  417. outl(val32, SMI_EN);
  418. }
  419. if (!request_region(iTCO_wdt_private.tco_res->start,
  420. resource_size(iTCO_wdt_private.tco_res), dev->name)) {
  421. pr_err("I/O address 0x%04llx already in use, device disabled\n",
  422. (u64)TCOBASE);
  423. ret = -EBUSY;
  424. goto unreg_smi;
  425. }
  426. pr_info("Found a %s TCO device (Version=%d, TCOBASE=0x%04llx)\n",
  427. ich_info->name, ich_info->iTCO_version, (u64)TCOBASE);
  428. /* Clear out the (probably old) status */
  429. if (iTCO_wdt_private.iTCO_version == 3) {
  430. outl(0x20008, TCO1_STS);
  431. } else {
  432. outw(0x0008, TCO1_STS); /* Clear the Time Out Status bit */
  433. outw(0x0002, TCO2_STS); /* Clear SECOND_TO_STS bit */
  434. outw(0x0004, TCO2_STS); /* Clear BOOT_STS bit */
  435. }
  436. iTCO_wdt_watchdog_dev.bootstatus = 0;
  437. iTCO_wdt_watchdog_dev.timeout = WATCHDOG_TIMEOUT;
  438. watchdog_set_nowayout(&iTCO_wdt_watchdog_dev, nowayout);
  439. iTCO_wdt_watchdog_dev.parent = &dev->dev;
  440. /* Make sure the watchdog is not running */
  441. iTCO_wdt_stop(&iTCO_wdt_watchdog_dev);
  442. /* Check that the heartbeat value is within it's range;
  443. if not reset to the default */
  444. if (iTCO_wdt_set_timeout(&iTCO_wdt_watchdog_dev, heartbeat)) {
  445. iTCO_wdt_set_timeout(&iTCO_wdt_watchdog_dev, WATCHDOG_TIMEOUT);
  446. pr_info("timeout value out of range, using %d\n",
  447. WATCHDOG_TIMEOUT);
  448. }
  449. ret = watchdog_register_device(&iTCO_wdt_watchdog_dev);
  450. if (ret != 0) {
  451. pr_err("cannot register watchdog device (err=%d)\n", ret);
  452. goto unreg_tco;
  453. }
  454. pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
  455. heartbeat, nowayout);
  456. return 0;
  457. unreg_tco:
  458. release_region(iTCO_wdt_private.tco_res->start,
  459. resource_size(iTCO_wdt_private.tco_res));
  460. unreg_smi:
  461. release_region(iTCO_wdt_private.smi_res->start,
  462. resource_size(iTCO_wdt_private.smi_res));
  463. unmap_gcs_pmc:
  464. if (iTCO_wdt_private.iTCO_version >= 2)
  465. iounmap(iTCO_wdt_private.gcs_pmc);
  466. unreg_gcs_pmc:
  467. if (iTCO_wdt_private.iTCO_version >= 2)
  468. release_mem_region(iTCO_wdt_private.gcs_pmc_res->start,
  469. resource_size(iTCO_wdt_private.gcs_pmc_res));
  470. out:
  471. iTCO_wdt_private.tco_res = NULL;
  472. iTCO_wdt_private.smi_res = NULL;
  473. iTCO_wdt_private.gcs_pmc_res = NULL;
  474. iTCO_wdt_private.gcs_pmc = NULL;
  475. return ret;
  476. }
  477. static int iTCO_wdt_remove(struct platform_device *dev)
  478. {
  479. if (iTCO_wdt_private.tco_res || iTCO_wdt_private.smi_res)
  480. iTCO_wdt_cleanup();
  481. return 0;
  482. }
  483. static void iTCO_wdt_shutdown(struct platform_device *dev)
  484. {
  485. iTCO_wdt_stop(NULL);
  486. }
  487. #ifdef CONFIG_PM_SLEEP
  488. /*
  489. * Suspend-to-idle requires this, because it stops the ticks and timekeeping, so
  490. * the watchdog cannot be pinged while in that state. In ACPI sleep states the
  491. * watchdog is stopped by the platform firmware.
  492. */
  493. #ifdef CONFIG_ACPI
  494. static inline bool need_suspend(void)
  495. {
  496. return acpi_target_system_state() == ACPI_STATE_S0;
  497. }
  498. #else
  499. static inline bool need_suspend(void) { return true; }
  500. #endif
  501. static int iTCO_wdt_suspend_noirq(struct device *dev)
  502. {
  503. int ret = 0;
  504. iTCO_wdt_private.suspended = false;
  505. if (watchdog_active(&iTCO_wdt_watchdog_dev) && need_suspend()) {
  506. ret = iTCO_wdt_stop(&iTCO_wdt_watchdog_dev);
  507. if (!ret)
  508. iTCO_wdt_private.suspended = true;
  509. }
  510. return ret;
  511. }
  512. static int iTCO_wdt_resume_noirq(struct device *dev)
  513. {
  514. if (iTCO_wdt_private.suspended)
  515. iTCO_wdt_start(&iTCO_wdt_watchdog_dev);
  516. return 0;
  517. }
  518. static struct dev_pm_ops iTCO_wdt_pm = {
  519. .suspend_noirq = iTCO_wdt_suspend_noirq,
  520. .resume_noirq = iTCO_wdt_resume_noirq,
  521. };
  522. #define ITCO_WDT_PM_OPS (&iTCO_wdt_pm)
  523. #else
  524. #define ITCO_WDT_PM_OPS NULL
  525. #endif /* CONFIG_PM_SLEEP */
  526. static struct platform_driver iTCO_wdt_driver = {
  527. .probe = iTCO_wdt_probe,
  528. .remove = iTCO_wdt_remove,
  529. .shutdown = iTCO_wdt_shutdown,
  530. .driver = {
  531. .name = DRV_NAME,
  532. .pm = ITCO_WDT_PM_OPS,
  533. },
  534. };
  535. static int __init iTCO_wdt_init_module(void)
  536. {
  537. int err;
  538. pr_info("Intel TCO WatchDog Timer Driver v%s\n", DRV_VERSION);
  539. err = platform_driver_register(&iTCO_wdt_driver);
  540. if (err)
  541. return err;
  542. return 0;
  543. }
  544. static void __exit iTCO_wdt_cleanup_module(void)
  545. {
  546. platform_driver_unregister(&iTCO_wdt_driver);
  547. pr_info("Watchdog Module Unloaded\n");
  548. }
  549. module_init(iTCO_wdt_init_module);
  550. module_exit(iTCO_wdt_cleanup_module);
  551. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  552. MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
  553. MODULE_VERSION(DRV_VERSION);
  554. MODULE_LICENSE("GPL");
  555. MODULE_ALIAS("platform:" DRV_NAME);