2 * intel TCO Watchdog Driver
4 * (c) Copyright 2006-2011 Wim Van Sebroeck <wim@iguana.be>.
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.
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.
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
44 * Includes, defines, variables, module parameters, ...
47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49 /* Module and version information */
50 #define DRV_NAME "iTCO_wdt"
51 #define DRV_VERSION "1.11"
54 #include <linux/module.h> /* For module specific items */
55 #include <linux/moduleparam.h> /* For new moduleparam's */
56 #include <linux/types.h> /* For standard types (like size_t) */
57 #include <linux/errno.h> /* For the -ENODEV/... values */
58 #include <linux/kernel.h> /* For printk/panic/... */
59 #include <linux/watchdog.h> /* For the watchdog specific items */
60 #include <linux/init.h> /* For __init/__exit/... */
61 #include <linux/fs.h> /* For file operations */
62 #include <linux/platform_device.h> /* For platform_driver framework */
63 #include <linux/pci.h> /* For pci functions */
64 #include <linux/ioport.h> /* For io-port access */
65 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
66 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
67 #include <linux/io.h> /* For inb/outb/... */
68 #include <linux/mfd/core.h>
69 #include <linux/mfd/lpc_ich.h>
71 #include "iTCO_vendor.h"
73 /* Address definitions for the TCO */
74 /* TCO base address */
75 #define TCOBASE (iTCO_wdt_private.tco_res->start)
76 /* SMI Control and Enable Register */
77 #define SMI_EN (iTCO_wdt_private.smi_res->start)
79 #define TCO_RLD (TCOBASE + 0x00) /* TCO Timer Reload and Curr. Value */
80 #define TCOv1_TMR (TCOBASE + 0x01) /* TCOv1 Timer Initial Value */
81 #define TCO_DAT_IN (TCOBASE + 0x02) /* TCO Data In Register */
82 #define TCO_DAT_OUT (TCOBASE + 0x03) /* TCO Data Out Register */
83 #define TCO1_STS (TCOBASE + 0x04) /* TCO1 Status Register */
84 #define TCO2_STS (TCOBASE + 0x06) /* TCO2 Status Register */
85 #define TCO1_CNT (TCOBASE + 0x08) /* TCO1 Control Register */
86 #define TCO2_CNT (TCOBASE + 0x0a) /* TCO2 Control Register */
87 #define TCOv2_TMR (TCOBASE + 0x12) /* TCOv2 Timer Initial Value */
89 /* internal variables */
90 static struct { /* this is private data for the iTCO_wdt device */
91 /* TCO version/generation */
92 unsigned int iTCO_version
;
93 struct resource
*tco_res
;
94 struct resource
*smi_res
;
96 * NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2),
97 * or memory-mapped PMC register bit 4 (TCO version 3).
99 struct resource
*gcs_pmc_res
;
100 unsigned long __iomem
*gcs_pmc
;
101 /* the lock for io operations */
103 struct platform_device
*dev
;
105 struct pci_dev
*pdev
;
108 /* module parameters */
109 #define WATCHDOG_TIMEOUT 30 /* 30 sec default heartbeat */
110 static int heartbeat
= WATCHDOG_TIMEOUT
; /* in seconds */
111 module_param(heartbeat
, int, 0);
112 MODULE_PARM_DESC(heartbeat
, "Watchdog timeout in seconds. "
113 "5..76 (TCO v1) or 3..614 (TCO v2), default="
114 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
116 static bool nowayout
= WATCHDOG_NOWAYOUT
;
117 module_param(nowayout
, bool, 0);
118 MODULE_PARM_DESC(nowayout
,
119 "Watchdog cannot be stopped once started (default="
120 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
122 static int turn_SMI_watchdog_clear_off
= 1;
123 module_param(turn_SMI_watchdog_clear_off
, int, 0);
124 MODULE_PARM_DESC(turn_SMI_watchdog_clear_off
,
125 "Turn off SMI clearing watchdog (depends on TCO-version)(default=1)");
128 * Some TCO specific functions
132 * The iTCO v1 and v2's internal timer is stored as ticks which decrement
133 * every 0.6 seconds. v3's internal timer is stored as seconds (some
134 * datasheets incorrectly state 0.6 seconds).
136 static inline unsigned int seconds_to_ticks(int secs
)
138 return iTCO_wdt_private
.iTCO_version
== 3 ? secs
: (secs
* 10) / 6;
141 static inline unsigned int ticks_to_seconds(int ticks
)
143 return iTCO_wdt_private
.iTCO_version
== 3 ? ticks
: (ticks
* 6) / 10;
146 static void iTCO_wdt_set_NO_REBOOT_bit(void)
150 /* Set the NO_REBOOT bit: this disables reboots */
151 if (iTCO_wdt_private
.iTCO_version
== 3) {
152 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
154 writel(val32
, iTCO_wdt_private
.gcs_pmc
);
155 } else if (iTCO_wdt_private
.iTCO_version
== 2) {
156 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
158 writel(val32
, iTCO_wdt_private
.gcs_pmc
);
159 } else if (iTCO_wdt_private
.iTCO_version
== 1) {
160 pci_read_config_dword(iTCO_wdt_private
.pdev
, 0xd4, &val32
);
162 pci_write_config_dword(iTCO_wdt_private
.pdev
, 0xd4, val32
);
166 static int iTCO_wdt_unset_NO_REBOOT_bit(void)
171 /* Unset the NO_REBOOT bit: this enables reboots */
172 if (iTCO_wdt_private
.iTCO_version
== 3) {
173 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
175 writel(val32
, iTCO_wdt_private
.gcs_pmc
);
177 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
178 if (val32
& 0x00000010)
180 } else if (iTCO_wdt_private
.iTCO_version
== 2) {
181 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
183 writel(val32
, iTCO_wdt_private
.gcs_pmc
);
185 val32
= readl(iTCO_wdt_private
.gcs_pmc
);
186 if (val32
& 0x00000020)
188 } else if (iTCO_wdt_private
.iTCO_version
== 1) {
189 pci_read_config_dword(iTCO_wdt_private
.pdev
, 0xd4, &val32
);
191 pci_write_config_dword(iTCO_wdt_private
.pdev
, 0xd4, val32
);
193 pci_read_config_dword(iTCO_wdt_private
.pdev
, 0xd4, &val32
);
194 if (val32
& 0x00000002)
198 return ret
; /* returns: 0 = OK, -EIO = Error */
201 static int iTCO_wdt_start(struct watchdog_device
*wd_dev
)
205 spin_lock(&iTCO_wdt_private
.io_lock
);
207 iTCO_vendor_pre_start(iTCO_wdt_private
.smi_res
, wd_dev
->timeout
);
209 /* disable chipset's NO_REBOOT bit */
210 if (iTCO_wdt_unset_NO_REBOOT_bit()) {
211 spin_unlock(&iTCO_wdt_private
.io_lock
);
212 pr_err("failed to reset NO_REBOOT flag, reboot disabled by hardware/BIOS\n");
216 /* Force the timer to its reload value by writing to the TCO_RLD
218 if (iTCO_wdt_private
.iTCO_version
>= 2)
220 else if (iTCO_wdt_private
.iTCO_version
== 1)
223 /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
228 spin_unlock(&iTCO_wdt_private
.io_lock
);
235 static int iTCO_wdt_stop(struct watchdog_device
*wd_dev
)
239 spin_lock(&iTCO_wdt_private
.io_lock
);
241 iTCO_vendor_pre_stop(iTCO_wdt_private
.smi_res
);
243 /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
249 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
250 iTCO_wdt_set_NO_REBOOT_bit();
252 spin_unlock(&iTCO_wdt_private
.io_lock
);
254 if ((val
& 0x0800) == 0)
259 static int iTCO_wdt_ping(struct watchdog_device
*wd_dev
)
261 spin_lock(&iTCO_wdt_private
.io_lock
);
263 iTCO_vendor_pre_keepalive(iTCO_wdt_private
.smi_res
, wd_dev
->timeout
);
265 /* Reload the timer by writing to the TCO Timer Counter register */
266 if (iTCO_wdt_private
.iTCO_version
>= 2) {
268 } else if (iTCO_wdt_private
.iTCO_version
== 1) {
269 /* Reset the timeout status bit so that the timer
270 * needs to count down twice again before rebooting */
271 outw(0x0008, TCO1_STS
); /* write 1 to clear bit */
276 spin_unlock(&iTCO_wdt_private
.io_lock
);
280 static int iTCO_wdt_set_timeout(struct watchdog_device
*wd_dev
, unsigned int t
)
286 tmrval
= seconds_to_ticks(t
);
288 /* For TCO v1 the timer counts down twice before rebooting */
289 if (iTCO_wdt_private
.iTCO_version
== 1)
292 /* from the specs: */
293 /* "Values of 0h-3h are ignored and should not be attempted" */
296 if (((iTCO_wdt_private
.iTCO_version
>= 2) && (tmrval
> 0x3ff)) ||
297 ((iTCO_wdt_private
.iTCO_version
== 1) && (tmrval
> 0x03f)))
300 iTCO_vendor_pre_set_heartbeat(tmrval
);
302 /* Write new heartbeat to watchdog */
303 if (iTCO_wdt_private
.iTCO_version
>= 2) {
304 spin_lock(&iTCO_wdt_private
.io_lock
);
305 val16
= inw(TCOv2_TMR
);
308 outw(val16
, TCOv2_TMR
);
309 val16
= inw(TCOv2_TMR
);
310 spin_unlock(&iTCO_wdt_private
.io_lock
);
312 if ((val16
& 0x3ff) != tmrval
)
314 } else if (iTCO_wdt_private
.iTCO_version
== 1) {
315 spin_lock(&iTCO_wdt_private
.io_lock
);
316 val8
= inb(TCOv1_TMR
);
318 val8
|= (tmrval
& 0xff);
319 outb(val8
, TCOv1_TMR
);
320 val8
= inb(TCOv1_TMR
);
321 spin_unlock(&iTCO_wdt_private
.io_lock
);
323 if ((val8
& 0x3f) != tmrval
)
331 static unsigned int iTCO_wdt_get_timeleft(struct watchdog_device
*wd_dev
)
335 unsigned int time_left
= 0;
337 /* read the TCO Timer */
338 if (iTCO_wdt_private
.iTCO_version
>= 2) {
339 spin_lock(&iTCO_wdt_private
.io_lock
);
340 val16
= inw(TCO_RLD
);
342 spin_unlock(&iTCO_wdt_private
.io_lock
);
344 time_left
= ticks_to_seconds(val16
);
345 } else if (iTCO_wdt_private
.iTCO_version
== 1) {
346 spin_lock(&iTCO_wdt_private
.io_lock
);
349 if (!(inw(TCO1_STS
) & 0x0008))
350 val8
+= (inb(TCOv1_TMR
) & 0x3f);
351 spin_unlock(&iTCO_wdt_private
.io_lock
);
353 time_left
= ticks_to_seconds(val8
);
362 static const struct watchdog_info ident
= {
363 .options
= WDIOF_SETTIMEOUT
|
364 WDIOF_KEEPALIVEPING
|
366 .firmware_version
= 0,
367 .identity
= DRV_NAME
,
370 static const struct watchdog_ops iTCO_wdt_ops
= {
371 .owner
= THIS_MODULE
,
372 .start
= iTCO_wdt_start
,
373 .stop
= iTCO_wdt_stop
,
374 .ping
= iTCO_wdt_ping
,
375 .set_timeout
= iTCO_wdt_set_timeout
,
376 .get_timeleft
= iTCO_wdt_get_timeleft
,
379 static struct watchdog_device iTCO_wdt_watchdog_dev
= {
381 .ops
= &iTCO_wdt_ops
,
385 * Init & exit routines
388 static void iTCO_wdt_cleanup(void)
390 /* Stop the timer before we leave */
392 iTCO_wdt_stop(&iTCO_wdt_watchdog_dev
);
395 watchdog_unregister_device(&iTCO_wdt_watchdog_dev
);
397 /* release resources */
398 release_region(iTCO_wdt_private
.tco_res
->start
,
399 resource_size(iTCO_wdt_private
.tco_res
));
400 release_region(iTCO_wdt_private
.smi_res
->start
,
401 resource_size(iTCO_wdt_private
.smi_res
));
402 if (iTCO_wdt_private
.iTCO_version
>= 2) {
403 iounmap(iTCO_wdt_private
.gcs_pmc
);
404 release_mem_region(iTCO_wdt_private
.gcs_pmc_res
->start
,
405 resource_size(iTCO_wdt_private
.gcs_pmc_res
));
408 iTCO_wdt_private
.tco_res
= NULL
;
409 iTCO_wdt_private
.smi_res
= NULL
;
410 iTCO_wdt_private
.gcs_pmc_res
= NULL
;
411 iTCO_wdt_private
.gcs_pmc
= NULL
;
414 static int iTCO_wdt_probe(struct platform_device
*dev
)
418 struct lpc_ich_info
*ich_info
= dev_get_platdata(&dev
->dev
);
423 spin_lock_init(&iTCO_wdt_private
.io_lock
);
425 iTCO_wdt_private
.tco_res
=
426 platform_get_resource(dev
, IORESOURCE_IO
, ICH_RES_IO_TCO
);
427 if (!iTCO_wdt_private
.tco_res
)
430 iTCO_wdt_private
.smi_res
=
431 platform_get_resource(dev
, IORESOURCE_IO
, ICH_RES_IO_SMI
);
432 if (!iTCO_wdt_private
.smi_res
)
435 iTCO_wdt_private
.iTCO_version
= ich_info
->iTCO_version
;
436 iTCO_wdt_private
.dev
= dev
;
437 iTCO_wdt_private
.pdev
= to_pci_dev(dev
->dev
.parent
);
440 * Get the Memory-Mapped GCS or PMC register, we need it for the
441 * NO_REBOOT flag (TCO v2 and v3).
443 if (iTCO_wdt_private
.iTCO_version
>= 2) {
444 iTCO_wdt_private
.gcs_pmc_res
= platform_get_resource(dev
,
446 ICH_RES_MEM_GCS_PMC
);
448 if (!iTCO_wdt_private
.gcs_pmc_res
)
451 if (!request_mem_region(iTCO_wdt_private
.gcs_pmc_res
->start
,
452 resource_size(iTCO_wdt_private
.gcs_pmc_res
), dev
->name
)) {
456 iTCO_wdt_private
.gcs_pmc
= ioremap(iTCO_wdt_private
.gcs_pmc_res
->start
,
457 resource_size(iTCO_wdt_private
.gcs_pmc_res
));
458 if (!iTCO_wdt_private
.gcs_pmc
) {
464 /* Check chipset's NO_REBOOT bit */
465 if (iTCO_wdt_unset_NO_REBOOT_bit() && iTCO_vendor_check_noreboot_on()) {
466 pr_info("unable to reset NO_REBOOT flag, device disabled by hardware/BIOS\n");
467 ret
= -ENODEV
; /* Cannot reset NO_REBOOT bit */
471 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
472 iTCO_wdt_set_NO_REBOOT_bit();
474 /* The TCO logic uses the TCO_EN bit in the SMI_EN register */
475 if (!request_region(iTCO_wdt_private
.smi_res
->start
,
476 resource_size(iTCO_wdt_private
.smi_res
), dev
->name
)) {
477 pr_err("I/O address 0x%04llx already in use, device disabled\n",
482 if (turn_SMI_watchdog_clear_off
>= iTCO_wdt_private
.iTCO_version
) {
484 * Bit 13: TCO_EN -> 0
485 * Disables TCO logic generating an SMI#
488 val32
&= 0xffffdfff; /* Turn off SMI clearing watchdog */
492 if (!request_region(iTCO_wdt_private
.tco_res
->start
,
493 resource_size(iTCO_wdt_private
.tco_res
), dev
->name
)) {
494 pr_err("I/O address 0x%04llx already in use, device disabled\n",
500 pr_info("Found a %s TCO device (Version=%d, TCOBASE=0x%04llx)\n",
501 ich_info
->name
, ich_info
->iTCO_version
, (u64
)TCOBASE
);
503 /* Clear out the (probably old) status */
504 if (iTCO_wdt_private
.iTCO_version
== 3) {
505 outl(0x20008, TCO1_STS
);
507 outw(0x0008, TCO1_STS
); /* Clear the Time Out Status bit */
508 outw(0x0002, TCO2_STS
); /* Clear SECOND_TO_STS bit */
509 outw(0x0004, TCO2_STS
); /* Clear BOOT_STS bit */
512 iTCO_wdt_watchdog_dev
.bootstatus
= 0;
513 iTCO_wdt_watchdog_dev
.timeout
= WATCHDOG_TIMEOUT
;
514 watchdog_set_nowayout(&iTCO_wdt_watchdog_dev
, nowayout
);
515 iTCO_wdt_watchdog_dev
.parent
= &dev
->dev
;
517 /* Make sure the watchdog is not running */
518 iTCO_wdt_stop(&iTCO_wdt_watchdog_dev
);
520 /* Check that the heartbeat value is within it's range;
521 if not reset to the default */
522 if (iTCO_wdt_set_timeout(&iTCO_wdt_watchdog_dev
, heartbeat
)) {
523 iTCO_wdt_set_timeout(&iTCO_wdt_watchdog_dev
, WATCHDOG_TIMEOUT
);
524 pr_info("timeout value out of range, using %d\n",
528 ret
= watchdog_register_device(&iTCO_wdt_watchdog_dev
);
530 pr_err("cannot register watchdog device (err=%d)\n", ret
);
534 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
535 heartbeat
, nowayout
);
540 release_region(iTCO_wdt_private
.tco_res
->start
,
541 resource_size(iTCO_wdt_private
.tco_res
));
543 release_region(iTCO_wdt_private
.smi_res
->start
,
544 resource_size(iTCO_wdt_private
.smi_res
));
546 if (iTCO_wdt_private
.iTCO_version
>= 2)
547 iounmap(iTCO_wdt_private
.gcs_pmc
);
549 if (iTCO_wdt_private
.iTCO_version
>= 2)
550 release_mem_region(iTCO_wdt_private
.gcs_pmc_res
->start
,
551 resource_size(iTCO_wdt_private
.gcs_pmc_res
));
553 iTCO_wdt_private
.tco_res
= NULL
;
554 iTCO_wdt_private
.smi_res
= NULL
;
555 iTCO_wdt_private
.gcs_pmc_res
= NULL
;
556 iTCO_wdt_private
.gcs_pmc
= NULL
;
561 static int iTCO_wdt_remove(struct platform_device
*dev
)
563 if (iTCO_wdt_private
.tco_res
|| iTCO_wdt_private
.smi_res
)
569 static void iTCO_wdt_shutdown(struct platform_device
*dev
)
574 static struct platform_driver iTCO_wdt_driver
= {
575 .probe
= iTCO_wdt_probe
,
576 .remove
= iTCO_wdt_remove
,
577 .shutdown
= iTCO_wdt_shutdown
,
583 static int __init
iTCO_wdt_init_module(void)
587 pr_info("Intel TCO WatchDog Timer Driver v%s\n", DRV_VERSION
);
589 err
= platform_driver_register(&iTCO_wdt_driver
);
596 static void __exit
iTCO_wdt_cleanup_module(void)
598 platform_driver_unregister(&iTCO_wdt_driver
);
599 pr_info("Watchdog Module Unloaded\n");
602 module_init(iTCO_wdt_init_module
);
603 module_exit(iTCO_wdt_cleanup_module
);
605 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
606 MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
607 MODULE_VERSION(DRV_VERSION
);
608 MODULE_LICENSE("GPL");
609 MODULE_ALIAS("platform:" DRV_NAME
);