1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sp5100_tco : TCO timer driver for sp5100 chipsets
5 * (c) Copyright 2009 Google Inc., All Rights Reserved.
8 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
10 * https://www.kernelconcepts.de
12 * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide",
13 * AMD Publication 45482 "AMD SB800-Series Southbridges Register
15 * AMD Publication 48751 "BIOS and Kernel Developer’s Guide (BKDG)
16 * for AMD Family 16h Models 00h-0Fh Processors"
17 * AMD Publication 51192 "AMD Bolton FCH Register Reference Guide"
18 * AMD Publication 52740 "BIOS and Kernel Developer’s Guide (BKDG)
19 * for AMD Family 16h Models 30h-3Fh Processors"
20 * AMD Publication 55570-B1-PUB "Processor Programming Reference (PPR)
21 * for AMD Family 17h Model 18h, Revision B1
23 * AMD Publication 55772-A1-PUB "Processor Programming Reference (PPR)
24 * for AMD Family 17h Model 20h, Revision A1
29 * Includes, defines, variables, module parameters, ...
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/init.h>
36 #include <linux/ioport.h>
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/pci.h>
40 #include <linux/platform_device.h>
41 #include <linux/types.h>
42 #include <linux/watchdog.h>
44 #include "sp5100_tco.h"
46 #define TCO_DRIVER_NAME "sp5100-tco"
48 /* internal variables */
55 struct watchdog_device wdd
;
56 void __iomem
*tcobase
;
57 enum tco_reg_layout tco_reg_layout
;
60 /* the watchdog platform device */
61 static struct platform_device
*sp5100_tco_platform_device
;
62 /* the associated PCI device */
63 static struct pci_dev
*sp5100_tco_pci
;
65 /* module parameters */
67 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
68 static int heartbeat
= WATCHDOG_HEARTBEAT
; /* in seconds */
69 module_param(heartbeat
, int, 0);
70 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. (default="
71 __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
73 static bool nowayout
= WATCHDOG_NOWAYOUT
;
74 module_param(nowayout
, bool, 0);
75 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started."
76 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
79 * Some TCO specific functions
82 static enum tco_reg_layout
tco_reg_layout(struct pci_dev
*dev
)
84 if (dev
->vendor
== PCI_VENDOR_ID_ATI
&&
85 dev
->device
== PCI_DEVICE_ID_ATI_SBX00_SMBUS
&&
86 dev
->revision
< 0x40) {
88 } else if (dev
->vendor
== PCI_VENDOR_ID_AMD
&&
89 ((dev
->device
== PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
&&
90 dev
->revision
>= 0x41) ||
91 (dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
&&
92 dev
->revision
>= 0x49))) {
98 static int tco_timer_start(struct watchdog_device
*wdd
)
100 struct sp5100_tco
*tco
= watchdog_get_drvdata(wdd
);
103 val
= readl(SP5100_WDT_CONTROL(tco
->tcobase
));
104 val
|= SP5100_WDT_START_STOP_BIT
;
105 writel(val
, SP5100_WDT_CONTROL(tco
->tcobase
));
110 static int tco_timer_stop(struct watchdog_device
*wdd
)
112 struct sp5100_tco
*tco
= watchdog_get_drvdata(wdd
);
115 val
= readl(SP5100_WDT_CONTROL(tco
->tcobase
));
116 val
&= ~SP5100_WDT_START_STOP_BIT
;
117 writel(val
, SP5100_WDT_CONTROL(tco
->tcobase
));
122 static int tco_timer_ping(struct watchdog_device
*wdd
)
124 struct sp5100_tco
*tco
= watchdog_get_drvdata(wdd
);
127 val
= readl(SP5100_WDT_CONTROL(tco
->tcobase
));
128 val
|= SP5100_WDT_TRIGGER_BIT
;
129 writel(val
, SP5100_WDT_CONTROL(tco
->tcobase
));
134 static int tco_timer_set_timeout(struct watchdog_device
*wdd
,
137 struct sp5100_tco
*tco
= watchdog_get_drvdata(wdd
);
139 /* Write new heartbeat to watchdog */
140 writel(t
, SP5100_WDT_COUNT(tco
->tcobase
));
147 static u8
sp5100_tco_read_pm_reg8(u8 index
)
149 outb(index
, SP5100_IO_PM_INDEX_REG
);
150 return inb(SP5100_IO_PM_DATA_REG
);
153 static void sp5100_tco_update_pm_reg8(u8 index
, u8 reset
, u8 set
)
157 outb(index
, SP5100_IO_PM_INDEX_REG
);
158 val
= inb(SP5100_IO_PM_DATA_REG
);
161 outb(val
, SP5100_IO_PM_DATA_REG
);
164 static void tco_timer_enable(struct sp5100_tco
*tco
)
168 switch (tco
->tco_reg_layout
) {
170 /* For SB800 or later */
171 /* Set the Watchdog timer resolution to 1 sec */
172 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG
,
173 0xff, SB800_PM_WATCHDOG_SECOND_RES
);
175 /* Enable watchdog decode bit and watchdog timer */
176 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL
,
177 ~SB800_PM_WATCHDOG_DISABLE
,
178 SB800_PCI_WATCHDOG_DECODE_EN
);
181 /* For SP5100 or SB7x0 */
182 /* Enable watchdog decode bit */
183 pci_read_config_dword(sp5100_tco_pci
,
184 SP5100_PCI_WATCHDOG_MISC_REG
,
187 val
|= SP5100_PCI_WATCHDOG_DECODE_EN
;
189 pci_write_config_dword(sp5100_tco_pci
,
190 SP5100_PCI_WATCHDOG_MISC_REG
,
193 /* Enable Watchdog timer and set the resolution to 1 sec */
194 sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL
,
195 ~SP5100_PM_WATCHDOG_DISABLE
,
196 SP5100_PM_WATCHDOG_SECOND_RES
);
199 /* Set the Watchdog timer resolution to 1 sec and enable */
200 sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN3
,
201 ~EFCH_PM_WATCHDOG_DISABLE
,
202 EFCH_PM_DECODEEN_SECOND_RES
);
207 static u32
sp5100_tco_read_pm_reg32(u8 index
)
212 for (i
= 3; i
>= 0; i
--)
213 val
= (val
<< 8) + sp5100_tco_read_pm_reg8(index
+ i
);
218 static int sp5100_tco_setupdevice(struct device
*dev
,
219 struct watchdog_device
*wdd
)
221 struct sp5100_tco
*tco
= watchdog_get_drvdata(wdd
);
222 const char *dev_name
;
223 u32 mmio_addr
= 0, val
;
226 /* Request the IO ports used by this driver */
227 if (!request_muxed_region(SP5100_IO_PM_INDEX_REG
,
228 SP5100_PM_IOPORTS_SIZE
, "sp5100_tco")) {
229 dev_err(dev
, "I/O address 0x%04x already in use\n",
230 SP5100_IO_PM_INDEX_REG
);
235 * Determine type of southbridge chipset.
237 switch (tco
->tco_reg_layout
) {
239 dev_name
= SP5100_DEVNAME
;
240 mmio_addr
= sp5100_tco_read_pm_reg32(SP5100_PM_WATCHDOG_BASE
) &
244 dev_name
= SB800_DEVNAME
;
245 mmio_addr
= sp5100_tco_read_pm_reg32(SB800_PM_WATCHDOG_BASE
) &
249 dev_name
= SB800_DEVNAME
;
251 * On Family 17h devices, the EFCH_PM_DECODEEN_WDT_TMREN bit of
252 * EFCH_PM_DECODEEN not only enables the EFCH_PM_WDT_ADDR memory
253 * region, it also enables the watchdog itself.
255 if (boot_cpu_data
.x86
== 0x17) {
256 val
= sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN
);
257 if (!(val
& EFCH_PM_DECODEEN_WDT_TMREN
)) {
258 sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN
, 0xff,
259 EFCH_PM_DECODEEN_WDT_TMREN
);
262 val
= sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN
);
263 if (val
& EFCH_PM_DECODEEN_WDT_TMREN
)
264 mmio_addr
= EFCH_PM_WDT_ADDR
;
270 /* Check MMIO address conflict */
272 !devm_request_mem_region(dev
, mmio_addr
, SP5100_WDT_MEM_MAP_SIZE
,
275 dev_dbg(dev
, "MMIO address 0x%08x already in use\n",
277 switch (tco
->tco_reg_layout
) {
280 * Secondly, Find the watchdog timer MMIO address
281 * from SBResource_MMIO register.
283 /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
284 pci_read_config_dword(sp5100_tco_pci
,
285 SP5100_SB_RESOURCE_MMIO_BASE
,
287 if ((mmio_addr
& (SB800_ACPI_MMIO_DECODE_EN
|
288 SB800_ACPI_MMIO_SEL
)) !=
289 SB800_ACPI_MMIO_DECODE_EN
) {
294 mmio_addr
+= SB800_PM_WDT_MMIO_OFFSET
;
297 /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */
299 sp5100_tco_read_pm_reg32(SB800_PM_ACPI_MMIO_EN
);
300 if ((mmio_addr
& (SB800_ACPI_MMIO_DECODE_EN
|
301 SB800_ACPI_MMIO_SEL
)) !=
302 SB800_ACPI_MMIO_DECODE_EN
) {
307 mmio_addr
+= SB800_PM_WDT_MMIO_OFFSET
;
310 val
= sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL
);
311 if (!(val
& EFCH_PM_ISACONTROL_MMIOEN
)) {
315 mmio_addr
= EFCH_PM_ACPI_MMIO_ADDR
+
316 EFCH_PM_ACPI_MMIO_WDT_OFFSET
;
319 dev_dbg(dev
, "Got 0x%08x from SBResource_MMIO register\n",
321 if (!devm_request_mem_region(dev
, mmio_addr
,
322 SP5100_WDT_MEM_MAP_SIZE
,
324 dev_dbg(dev
, "MMIO address 0x%08x already in use\n",
331 tco
->tcobase
= devm_ioremap(dev
, mmio_addr
, SP5100_WDT_MEM_MAP_SIZE
);
333 dev_err(dev
, "failed to get tcobase address\n");
338 dev_info(dev
, "Using 0x%08x for watchdog MMIO address\n", mmio_addr
);
340 /* Setup the watchdog timer */
341 tco_timer_enable(tco
);
343 val
= readl(SP5100_WDT_CONTROL(tco
->tcobase
));
344 if (val
& SP5100_WDT_DISABLED
) {
345 dev_err(dev
, "Watchdog hardware is disabled\n");
351 * Save WatchDogFired status, because WatchDogFired flag is
354 if (val
& SP5100_WDT_FIRED
)
355 wdd
->bootstatus
= WDIOF_CARDRESET
;
356 /* Set watchdog action to reset the system */
357 val
&= ~SP5100_WDT_ACTION_RESET
;
358 writel(val
, SP5100_WDT_CONTROL(tco
->tcobase
));
360 /* Set a reasonable heartbeat before we stop the timer */
361 tco_timer_set_timeout(wdd
, wdd
->timeout
);
364 * Stop the TCO before we change anything so we don't race with
369 release_region(SP5100_IO_PM_INDEX_REG
, SP5100_PM_IOPORTS_SIZE
);
374 release_region(SP5100_IO_PM_INDEX_REG
, SP5100_PM_IOPORTS_SIZE
);
378 static struct watchdog_info sp5100_tco_wdt_info
= {
379 .identity
= "SP5100 TCO timer",
380 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
383 static const struct watchdog_ops sp5100_tco_wdt_ops
= {
384 .owner
= THIS_MODULE
,
385 .start
= tco_timer_start
,
386 .stop
= tco_timer_stop
,
387 .ping
= tco_timer_ping
,
388 .set_timeout
= tco_timer_set_timeout
,
391 static int sp5100_tco_probe(struct platform_device
*pdev
)
393 struct device
*dev
= &pdev
->dev
;
394 struct watchdog_device
*wdd
;
395 struct sp5100_tco
*tco
;
398 tco
= devm_kzalloc(dev
, sizeof(*tco
), GFP_KERNEL
);
402 tco
->tco_reg_layout
= tco_reg_layout(sp5100_tco_pci
);
406 wdd
->info
= &sp5100_tco_wdt_info
;
407 wdd
->ops
= &sp5100_tco_wdt_ops
;
408 wdd
->timeout
= WATCHDOG_HEARTBEAT
;
409 wdd
->min_timeout
= 1;
410 wdd
->max_timeout
= 0xffff;
412 watchdog_init_timeout(wdd
, heartbeat
, NULL
);
413 watchdog_set_nowayout(wdd
, nowayout
);
414 watchdog_stop_on_reboot(wdd
);
415 watchdog_stop_on_unregister(wdd
);
416 watchdog_set_drvdata(wdd
, tco
);
418 ret
= sp5100_tco_setupdevice(dev
, wdd
);
422 ret
= devm_watchdog_register_device(dev
, wdd
);
426 /* Show module parameters */
427 dev_info(dev
, "initialized. heartbeat=%d sec (nowayout=%d)\n",
428 wdd
->timeout
, nowayout
);
433 static struct platform_driver sp5100_tco_driver
= {
434 .probe
= sp5100_tco_probe
,
436 .name
= TCO_DRIVER_NAME
,
441 * Data for PCI driver interface
443 * This data only exists for exporting the supported
444 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
445 * register a pci_driver, because someone else might
446 * want to register another driver on the same PCI id.
448 static const struct pci_device_id sp5100_tco_pci_tbl
[] = {
449 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_SBX00_SMBUS
, PCI_ANY_ID
,
451 { PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
, PCI_ANY_ID
,
453 { PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
, PCI_ANY_ID
,
455 { 0, }, /* End of list */
457 MODULE_DEVICE_TABLE(pci
, sp5100_tco_pci_tbl
);
459 static int __init
sp5100_tco_init(void)
461 struct pci_dev
*dev
= NULL
;
464 /* Match the PCI device */
465 for_each_pci_dev(dev
) {
466 if (pci_match_id(sp5100_tco_pci_tbl
, dev
) != NULL
) {
467 sp5100_tco_pci
= dev
;
475 pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n");
477 err
= platform_driver_register(&sp5100_tco_driver
);
481 sp5100_tco_platform_device
=
482 platform_device_register_simple(TCO_DRIVER_NAME
, -1, NULL
, 0);
483 if (IS_ERR(sp5100_tco_platform_device
)) {
484 err
= PTR_ERR(sp5100_tco_platform_device
);
485 goto unreg_platform_driver
;
490 unreg_platform_driver
:
491 platform_driver_unregister(&sp5100_tco_driver
);
495 static void __exit
sp5100_tco_exit(void)
497 platform_device_unregister(sp5100_tco_platform_device
);
498 platform_driver_unregister(&sp5100_tco_driver
);
501 module_init(sp5100_tco_init
);
502 module_exit(sp5100_tco_exit
);
504 MODULE_AUTHOR("Priyanka Gupta");
505 MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
506 MODULE_LICENSE("GPL");