iTCO_wdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * intel TCO Watchdog Driver
  4. *
  5. * (c) Copyright 2006-2011 Wim Van Sebroeck <wim@iguana.be>.
  6. *
  7. * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
  8. * provide warranty for any of this software. This material is
  9. * provided "AS-IS" and at no charge.
  10. *
  11. * The TCO watchdog is implemented in the following I/O controller hubs:
  12. * (See the intel documentation on http://developer.intel.com.)
  13. * document number 290655-003, 290677-014: 82801AA (ICH), 82801AB (ICHO)
  14. * document number 290687-002, 298242-027: 82801BA (ICH2)
  15. * document number 290733-003, 290739-013: 82801CA (ICH3-S)
  16. * document number 290716-001, 290718-007: 82801CAM (ICH3-M)
  17. * document number 290744-001, 290745-025: 82801DB (ICH4)
  18. * document number 252337-001, 252663-008: 82801DBM (ICH4-M)
  19. * document number 273599-001, 273645-002: 82801E (C-ICH)
  20. * document number 252516-001, 252517-028: 82801EB (ICH5), 82801ER (ICH5R)
  21. * document number 300641-004, 300884-013: 6300ESB
  22. * document number 301473-002, 301474-026: 82801F (ICH6)
  23. * document number 313082-001, 313075-006: 631xESB, 632xESB
  24. * document number 307013-003, 307014-024: 82801G (ICH7)
  25. * document number 322896-001, 322897-001: NM10
  26. * document number 313056-003, 313057-017: 82801H (ICH8)
  27. * document number 316972-004, 316973-012: 82801I (ICH9)
  28. * document number 319973-002, 319974-002: 82801J (ICH10)
  29. * document number 322169-001, 322170-003: 5 Series, 3400 Series (PCH)
  30. * document number 320066-003, 320257-008: EP80597 (IICH)
  31. * document number 324645-001, 324646-001: Cougar Point (CPT)
  32. * document number TBD : Patsburg (PBG)
  33. * document number TBD : DH89xxCC
  34. * document number TBD : Panther Point
  35. * document number TBD : Lynx Point
  36. * document number TBD : Lynx Point-LP
  37. */
  38. /*
  39. * Includes, defines, variables, module parameters, ...
  40. */
  41. /* Module and version information */
  42. #define DRV_NAME "iTCO_wdt"
  43. #define DRV_VERSION "1.11"
  44. /* Includes */
  45. #include <linux/acpi.h> /* For ACPI support */
  46. #include <linux/bits.h> /* For BIT() */
  47. #include <linux/module.h> /* For module specific items */
  48. #include <linux/moduleparam.h> /* For new moduleparam's */
  49. #include <linux/types.h> /* For standard types (like size_t) */
  50. #include <linux/errno.h> /* For the -ENODEV/... values */
  51. #include <linux/kernel.h> /* For printk/panic/... */
  52. #include <linux/watchdog.h> /* For the watchdog specific items */
  53. #include <linux/init.h> /* For __init/__exit/... */
  54. #include <linux/fs.h> /* For file operations */
  55. #include <linux/platform_device.h> /* For platform_driver framework */
  56. #include <linux/pci.h> /* For pci functions */
  57. #include <linux/ioport.h> /* For io-port access */
  58. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  59. #include <linux/io.h> /* For inb/outb/... */
  60. #include <linux/platform_data/itco_wdt.h>
  61. #include <linux/mfd/intel_pmc_bxt.h>
  62. /* Address definitions for the TCO */
  63. /* TCO base address */
  64. #define TCOBASE(p) ((p)->tco_res->start)
  65. /* SMI Control and Enable Register */
  66. #define SMI_EN(p) ((p)->smi_res->start)
  67. #define TCO_RLD(p) (TCOBASE(p) + 0x00) /* TCO Timer Reload/Curr. Value */
  68. #define TCOv1_TMR(p) (TCOBASE(p) + 0x01) /* TCOv1 Timer Initial Value*/
  69. #define TCO_DAT_IN(p) (TCOBASE(p) + 0x02) /* TCO Data In Register */
  70. #define TCO_DAT_OUT(p) (TCOBASE(p) + 0x03) /* TCO Data Out Register */
  71. #define TCO1_STS(p) (TCOBASE(p) + 0x04) /* TCO1 Status Register */
  72. #define TCO2_STS(p) (TCOBASE(p) + 0x06) /* TCO2 Status Register */
  73. #define TCO1_CNT(p) (TCOBASE(p) + 0x08) /* TCO1 Control Register */
  74. #define TCO2_CNT(p) (TCOBASE(p) + 0x0a) /* TCO2 Control Register */
  75. #define TCOv2_TMR(p) (TCOBASE(p) + 0x12) /* TCOv2 Timer Initial Value*/
  76. /*
  77. * NMI_NOW is bit 8 of TCO1_CNT register
  78. * Read/Write
  79. * This bit is implemented as RW but has no effect on HW.
  80. */
  81. #define NMI_NOW BIT(8)
  82. /* internal variables */
  83. struct iTCO_wdt_private {
  84. struct watchdog_device wddev;
  85. /* TCO version/generation */
  86. unsigned int iTCO_version;
  87. struct resource *tco_res;
  88. struct resource *smi_res;
  89. /*
  90. * NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2),
  91. * or memory-mapped PMC register bit 4 (TCO version 3).
  92. */
  93. unsigned long __iomem *gcs_pmc;
  94. /* the PCI-device */
  95. struct pci_dev *pci_dev;
  96. /* whether or not the watchdog has been suspended */
  97. bool suspended;
  98. /* no reboot API private data */
  99. void *no_reboot_priv;
  100. /* no reboot update function pointer */
  101. int (*update_no_reboot_bit)(void *p, bool set);
  102. };
  103. /* module parameters */
  104. #define WATCHDOG_TIMEOUT 30 /* 30 sec default heartbeat */
  105. static int heartbeat = WATCHDOG_TIMEOUT; /* in seconds */
  106. module_param(heartbeat, int, 0);
  107. MODULE_PARM_DESC(heartbeat, "Watchdog timeout in seconds. "
  108. "5..76 (TCO v1) or 3..614 (TCO v2), default="
  109. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  110. static bool nowayout = WATCHDOG_NOWAYOUT;
  111. module_param(nowayout, bool, 0);
  112. MODULE_PARM_DESC(nowayout,
  113. "Watchdog cannot be stopped once started (default="
  114. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  115. static int turn_SMI_watchdog_clear_off = 1;
  116. module_param(turn_SMI_watchdog_clear_off, int, 0);
  117. MODULE_PARM_DESC(turn_SMI_watchdog_clear_off,
  118. "Turn off SMI clearing watchdog (depends on TCO-version)(default=1)");
  119. /*
  120. * Some TCO specific functions
  121. */
  122. /*
  123. * The iTCO v1 and v2's internal timer is stored as ticks which decrement
  124. * every 0.6 seconds. v3's internal timer is stored as seconds (some
  125. * datasheets incorrectly state 0.6 seconds).
  126. */
  127. static inline unsigned int seconds_to_ticks(struct iTCO_wdt_private *p,
  128. int secs)
  129. {
  130. return p->iTCO_version == 3 ? secs : (secs * 10) / 6;
  131. }
  132. static inline unsigned int ticks_to_seconds(struct iTCO_wdt_private *p,
  133. int ticks)
  134. {
  135. return p->iTCO_version == 3 ? ticks : (ticks * 6) / 10;
  136. }
  137. static inline u32 no_reboot_bit(struct iTCO_wdt_private *p)
  138. {
  139. u32 enable_bit;
  140. switch (p->iTCO_version) {
  141. case 5:
  142. case 3:
  143. enable_bit = 0x00000010;
  144. break;
  145. case 2:
  146. enable_bit = 0x00000020;
  147. break;
  148. case 4:
  149. case 1:
  150. default:
  151. enable_bit = 0x00000002;
  152. break;
  153. }
  154. return enable_bit;
  155. }
  156. static int update_no_reboot_bit_def(void *priv, bool set)
  157. {
  158. return 0;
  159. }
  160. static int update_no_reboot_bit_pci(void *priv, bool set)
  161. {
  162. struct iTCO_wdt_private *p = priv;
  163. u32 val32 = 0, newval32 = 0;
  164. pci_read_config_dword(p->pci_dev, 0xd4, &val32);
  165. if (set)
  166. val32 |= no_reboot_bit(p);
  167. else
  168. val32 &= ~no_reboot_bit(p);
  169. pci_write_config_dword(p->pci_dev, 0xd4, val32);
  170. pci_read_config_dword(p->pci_dev, 0xd4, &newval32);
  171. /* make sure the update is successful */
  172. if (val32 != newval32)
  173. return -EIO;
  174. return 0;
  175. }
  176. static int update_no_reboot_bit_mem(void *priv, bool set)
  177. {
  178. struct iTCO_wdt_private *p = priv;
  179. u32 val32 = 0, newval32 = 0;
  180. val32 = readl(p->gcs_pmc);
  181. if (set)
  182. val32 |= no_reboot_bit(p);
  183. else
  184. val32 &= ~no_reboot_bit(p);
  185. writel(val32, p->gcs_pmc);
  186. newval32 = readl(p->gcs_pmc);
  187. /* make sure the update is successful */
  188. if (val32 != newval32)
  189. return -EIO;
  190. return 0;
  191. }
  192. static int update_no_reboot_bit_cnt(void *priv, bool set)
  193. {
  194. struct iTCO_wdt_private *p = priv;
  195. u16 val, newval;
  196. /*
  197. * writing back 1b1 to NMI_NOW of TCO1_CNT register
  198. * causes NMI_NOW bit inversion what consequently does
  199. * not allow to perform the register's value comparison
  200. * properly.
  201. *
  202. * NMI_NOW bit masking for TCO1_CNT register values
  203. * helps to avoid possible NMI_NOW bit inversions on
  204. * following write operation.
  205. */
  206. val = inw(TCO1_CNT(p)) & ~NMI_NOW;
  207. if (set)
  208. val |= BIT(0);
  209. else
  210. val &= ~BIT(0);
  211. outw(val, TCO1_CNT(p));
  212. newval = inw(TCO1_CNT(p)) & ~NMI_NOW;
  213. /* make sure the update is successful */
  214. return val != newval ? -EIO : 0;
  215. }
  216. static int update_no_reboot_bit_pmc(void *priv, bool set)
  217. {
  218. struct intel_pmc_dev *pmc = priv;
  219. u32 bits = PMC_CFG_NO_REBOOT_EN;
  220. u32 value = set ? bits : 0;
  221. return intel_pmc_gcr_update(pmc, PMC_GCR_PMC_CFG_REG, bits, value);
  222. }
  223. static void iTCO_wdt_no_reboot_bit_setup(struct iTCO_wdt_private *p,
  224. struct platform_device *pdev,
  225. struct itco_wdt_platform_data *pdata)
  226. {
  227. if (pdata->no_reboot_use_pmc) {
  228. struct intel_pmc_dev *pmc = dev_get_drvdata(pdev->dev.parent);
  229. p->update_no_reboot_bit = update_no_reboot_bit_pmc;
  230. p->no_reboot_priv = pmc;
  231. return;
  232. }
  233. if (p->iTCO_version >= 6)
  234. p->update_no_reboot_bit = update_no_reboot_bit_cnt;
  235. else if (p->iTCO_version >= 2)
  236. p->update_no_reboot_bit = update_no_reboot_bit_mem;
  237. else if (p->iTCO_version == 1)
  238. p->update_no_reboot_bit = update_no_reboot_bit_pci;
  239. else
  240. p->update_no_reboot_bit = update_no_reboot_bit_def;
  241. p->no_reboot_priv = p;
  242. }
  243. static int iTCO_wdt_start(struct watchdog_device *wd_dev)
  244. {
  245. struct iTCO_wdt_private *p = watchdog_get_drvdata(wd_dev);
  246. unsigned int val;
  247. /* disable chipset's NO_REBOOT bit */
  248. if (p->update_no_reboot_bit(p->no_reboot_priv, false)) {
  249. dev_err(wd_dev->parent, "failed to reset NO_REBOOT flag, reboot disabled by hardware/BIOS\n");
  250. return -EIO;
  251. }
  252. /* Force the timer to its reload value by writing to the TCO_RLD
  253. register */
  254. if (p->iTCO_version >= 2)
  255. outw(0x01, TCO_RLD(p));
  256. else if (p->iTCO_version == 1)
  257. outb(0x01, TCO_RLD(p));
  258. /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
  259. val = inw(TCO1_CNT(p));
  260. val &= 0xf7ff;
  261. outw(val, TCO1_CNT(p));
  262. val = inw(TCO1_CNT(p));
  263. if (val & 0x0800)
  264. return -1;
  265. return 0;
  266. }
  267. static int iTCO_wdt_stop(struct watchdog_device *wd_dev)
  268. {
  269. struct iTCO_wdt_private *p = watchdog_get_drvdata(wd_dev);
  270. unsigned int val;
  271. /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
  272. val = inw(TCO1_CNT(p));
  273. val |= 0x0800;
  274. outw(val, TCO1_CNT(p));
  275. val = inw(TCO1_CNT(p));
  276. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  277. p->update_no_reboot_bit(p->no_reboot_priv, true);
  278. if ((val & 0x0800) == 0)
  279. return -1;
  280. return 0;
  281. }
  282. static int iTCO_wdt_ping(struct watchdog_device *wd_dev)
  283. {
  284. struct iTCO_wdt_private *p = watchdog_get_drvdata(wd_dev);
  285. /* Reload the timer by writing to the TCO Timer Counter register */
  286. if (p->iTCO_version >= 2) {
  287. outw(0x01, TCO_RLD(p));
  288. } else if (p->iTCO_version == 1) {
  289. /* Reset the timeout status bit so that the timer
  290. * needs to count down twice again before rebooting */
  291. outw(0x0008, TCO1_STS(p)); /* write 1 to clear bit */
  292. outb(0x01, TCO_RLD(p));
  293. }
  294. return 0;
  295. }
  296. static int iTCO_wdt_set_timeout(struct watchdog_device *wd_dev, unsigned int t)
  297. {
  298. struct iTCO_wdt_private *p = watchdog_get_drvdata(wd_dev);
  299. unsigned int val16;
  300. unsigned char val8;
  301. unsigned int tmrval;
  302. tmrval = seconds_to_ticks(p, t);
  303. /* For TCO v1 the timer counts down twice before rebooting */
  304. if (p->iTCO_version == 1)
  305. tmrval /= 2;
  306. /* from the specs: */
  307. /* "Values of 0h-3h are ignored and should not be attempted" */
  308. if (tmrval < 0x04)
  309. return -EINVAL;
  310. if ((p->iTCO_version >= 2 && tmrval > 0x3ff) ||
  311. (p->iTCO_version == 1 && tmrval > 0x03f))
  312. return -EINVAL;
  313. /* Write new heartbeat to watchdog */
  314. if (p->iTCO_version >= 2) {
  315. val16 = inw(TCOv2_TMR(p));
  316. val16 &= 0xfc00;
  317. val16 |= tmrval;
  318. outw(val16, TCOv2_TMR(p));
  319. val16 = inw(TCOv2_TMR(p));
  320. if ((val16 & 0x3ff) != tmrval)
  321. return -EINVAL;
  322. } else if (p->iTCO_version == 1) {
  323. val8 = inb(TCOv1_TMR(p));
  324. val8 &= 0xc0;
  325. val8 |= (tmrval & 0xff);
  326. outb(val8, TCOv1_TMR(p));
  327. val8 = inb(TCOv1_TMR(p));
  328. if ((val8 & 0x3f) != tmrval)
  329. return -EINVAL;
  330. }
  331. wd_dev->timeout = t;
  332. return 0;
  333. }
  334. static unsigned int iTCO_wdt_get_timeleft(struct watchdog_device *wd_dev)
  335. {
  336. struct iTCO_wdt_private *p = watchdog_get_drvdata(wd_dev);
  337. unsigned int val16;
  338. unsigned char val8;
  339. unsigned int time_left = 0;
  340. /* read the TCO Timer */
  341. if (p->iTCO_version >= 2) {
  342. val16 = inw(TCO_RLD(p));
  343. val16 &= 0x3ff;
  344. time_left = ticks_to_seconds(p, val16);
  345. } else if (p->iTCO_version == 1) {
  346. val8 = inb(TCO_RLD(p));
  347. val8 &= 0x3f;
  348. if (!(inw(TCO1_STS(p)) & 0x0008))
  349. val8 += (inb(TCOv1_TMR(p)) & 0x3f);
  350. time_left = ticks_to_seconds(p, val8);
  351. }
  352. return time_left;
  353. }
  354. /* Returns true if the watchdog was running */
  355. static bool iTCO_wdt_set_running(struct iTCO_wdt_private *p)
  356. {
  357. u16 val;
  358. /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled */
  359. val = inw(TCO1_CNT(p));
  360. if (!(val & BIT(11))) {
  361. set_bit(WDOG_HW_RUNNING, &p->wddev.status);
  362. return true;
  363. }
  364. return false;
  365. }
  366. /*
  367. * Kernel Interfaces
  368. */
  369. static struct watchdog_info ident = {
  370. .options = WDIOF_SETTIMEOUT |
  371. WDIOF_KEEPALIVEPING |
  372. WDIOF_MAGICCLOSE,
  373. .identity = DRV_NAME,
  374. };
  375. static const struct watchdog_ops iTCO_wdt_ops = {
  376. .owner = THIS_MODULE,
  377. .start = iTCO_wdt_start,
  378. .stop = iTCO_wdt_stop,
  379. .ping = iTCO_wdt_ping,
  380. .set_timeout = iTCO_wdt_set_timeout,
  381. .get_timeleft = iTCO_wdt_get_timeleft,
  382. };
  383. /*
  384. * Init & exit routines
  385. */
  386. static int iTCO_wdt_probe(struct platform_device *pdev)
  387. {
  388. struct device *dev = &pdev->dev;
  389. struct itco_wdt_platform_data *pdata = dev_get_platdata(dev);
  390. struct iTCO_wdt_private *p;
  391. unsigned long val32;
  392. int ret;
  393. if (!pdata)
  394. return -ENODEV;
  395. p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
  396. if (!p)
  397. return -ENOMEM;
  398. p->tco_res = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_IO_TCO);
  399. if (!p->tco_res)
  400. return -ENODEV;
  401. p->iTCO_version = pdata->version;
  402. p->pci_dev = to_pci_dev(dev->parent);
  403. p->smi_res = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_IO_SMI);
  404. if (p->smi_res) {
  405. /* The TCO logic uses the TCO_EN bit in the SMI_EN register */
  406. if (!devm_request_region(dev, p->smi_res->start,
  407. resource_size(p->smi_res),
  408. pdev->name)) {
  409. dev_err(dev, "I/O address 0x%04llx already in use, device disabled\n",
  410. (u64)SMI_EN(p));
  411. return -EBUSY;
  412. }
  413. } else if (turn_SMI_watchdog_clear_off >= p->iTCO_version) {
  414. dev_err(dev, "SMI I/O resource is missing\n");
  415. return -ENODEV;
  416. }
  417. iTCO_wdt_no_reboot_bit_setup(p, pdev, pdata);
  418. /*
  419. * Get the Memory-Mapped GCS or PMC register, we need it for the
  420. * NO_REBOOT flag (TCO v2 and v3).
  421. */
  422. if (p->iTCO_version >= 2 && p->iTCO_version < 6 &&
  423. !pdata->no_reboot_use_pmc) {
  424. p->gcs_pmc = devm_platform_ioremap_resource(pdev, ICH_RES_MEM_GCS_PMC);
  425. if (IS_ERR(p->gcs_pmc))
  426. return PTR_ERR(p->gcs_pmc);
  427. }
  428. /* Check chipset's NO_REBOOT bit */
  429. if (p->update_no_reboot_bit(p->no_reboot_priv, false)) {
  430. dev_info(dev, "unable to reset NO_REBOOT flag, device disabled by hardware/BIOS\n");
  431. return -ENODEV; /* Cannot reset NO_REBOOT bit */
  432. }
  433. if (turn_SMI_watchdog_clear_off >= p->iTCO_version) {
  434. /*
  435. * Bit 13: TCO_EN -> 0
  436. * Disables TCO logic generating an SMI#
  437. */
  438. val32 = inl(SMI_EN(p));
  439. val32 &= 0xffffdfff; /* Turn off SMI clearing watchdog */
  440. outl(val32, SMI_EN(p));
  441. }
  442. if (!devm_request_region(dev, p->tco_res->start,
  443. resource_size(p->tco_res),
  444. pdev->name)) {
  445. dev_err(dev, "I/O address 0x%04llx already in use, device disabled\n",
  446. (u64)TCOBASE(p));
  447. return -EBUSY;
  448. }
  449. dev_info(dev, "Found a %s TCO device (Version=%d, TCOBASE=0x%04llx)\n",
  450. pdata->name, pdata->version, (u64)TCOBASE(p));
  451. /* Clear out the (probably old) status */
  452. switch (p->iTCO_version) {
  453. case 6:
  454. case 5:
  455. case 4:
  456. outw(0x0008, TCO1_STS(p)); /* Clear the Time Out Status bit */
  457. outw(0x0002, TCO2_STS(p)); /* Clear SECOND_TO_STS bit */
  458. break;
  459. case 3:
  460. outl(0x20008, TCO1_STS(p));
  461. break;
  462. case 2:
  463. case 1:
  464. default:
  465. outw(0x0008, TCO1_STS(p)); /* Clear the Time Out Status bit */
  466. outw(0x0002, TCO2_STS(p)); /* Clear SECOND_TO_STS bit */
  467. outw(0x0004, TCO2_STS(p)); /* Clear BOOT_STS bit */
  468. break;
  469. }
  470. ident.firmware_version = p->iTCO_version;
  471. p->wddev.info = &ident;
  472. p->wddev.ops = &iTCO_wdt_ops;
  473. p->wddev.bootstatus = 0;
  474. p->wddev.timeout = WATCHDOG_TIMEOUT;
  475. watchdog_set_nowayout(&p->wddev, nowayout);
  476. p->wddev.parent = dev;
  477. watchdog_set_drvdata(&p->wddev, p);
  478. platform_set_drvdata(pdev, p);
  479. if (!iTCO_wdt_set_running(p)) {
  480. /*
  481. * If the watchdog was not running set NO_REBOOT now to
  482. * prevent later reboots.
  483. */
  484. p->update_no_reboot_bit(p->no_reboot_priv, true);
  485. }
  486. /* Check that the heartbeat value is within it's range;
  487. if not reset to the default */
  488. if (iTCO_wdt_set_timeout(&p->wddev, heartbeat)) {
  489. ret = iTCO_wdt_set_timeout(&p->wddev, WATCHDOG_TIMEOUT);
  490. if (ret != 0) {
  491. dev_err(dev, "Failed to set watchdog timeout (%d)\n", WATCHDOG_TIMEOUT);
  492. return ret;
  493. }
  494. dev_info(dev, "timeout value out of range, using %d\n",
  495. WATCHDOG_TIMEOUT);
  496. heartbeat = WATCHDOG_TIMEOUT;
  497. }
  498. watchdog_stop_on_reboot(&p->wddev);
  499. watchdog_stop_on_unregister(&p->wddev);
  500. ret = devm_watchdog_register_device(dev, &p->wddev);
  501. if (ret != 0)
  502. return ret;
  503. dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n",
  504. heartbeat, nowayout);
  505. return 0;
  506. }
  507. /*
  508. * Suspend-to-idle requires this, because it stops the ticks and timekeeping, so
  509. * the watchdog cannot be pinged while in that state. In ACPI sleep states the
  510. * watchdog is stopped by the platform firmware.
  511. */
  512. #ifdef CONFIG_ACPI
  513. static inline bool __maybe_unused need_suspend(void)
  514. {
  515. return acpi_target_system_state() == ACPI_STATE_S0;
  516. }
  517. #else
  518. static inline bool __maybe_unused need_suspend(void) { return true; }
  519. #endif
  520. static int __maybe_unused iTCO_wdt_suspend_noirq(struct device *dev)
  521. {
  522. struct iTCO_wdt_private *p = dev_get_drvdata(dev);
  523. int ret = 0;
  524. p->suspended = false;
  525. if (watchdog_active(&p->wddev) && need_suspend()) {
  526. ret = iTCO_wdt_stop(&p->wddev);
  527. if (!ret)
  528. p->suspended = true;
  529. }
  530. return ret;
  531. }
  532. static int __maybe_unused iTCO_wdt_resume_noirq(struct device *dev)
  533. {
  534. struct iTCO_wdt_private *p = dev_get_drvdata(dev);
  535. if (p->suspended)
  536. iTCO_wdt_start(&p->wddev);
  537. return 0;
  538. }
  539. static const struct dev_pm_ops iTCO_wdt_pm = {
  540. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(iTCO_wdt_suspend_noirq,
  541. iTCO_wdt_resume_noirq)
  542. };
  543. static struct platform_driver iTCO_wdt_driver = {
  544. .probe = iTCO_wdt_probe,
  545. .driver = {
  546. .name = DRV_NAME,
  547. .pm = &iTCO_wdt_pm,
  548. },
  549. };
  550. module_platform_driver(iTCO_wdt_driver);
  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);