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.
10 * https://www.kernelconcepts.de
12 * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide",
13 * AMD Publication 44413 "AMD SP5100 Register Reference Guide"
14 * AMD Publication 45482 "AMD SB800-Series Southbridges Register
16 * AMD Publication 48751 "BIOS and Kernel Developer’s Guide (BKDG)
17 * for AMD Family 16h Models 00h-0Fh Processors"
18 * AMD Publication 51192 "AMD Bolton FCH Register Reference Guide"
19 * AMD Publication 52740 "BIOS and Kernel Developer’s Guide (BKDG)
20 * for AMD Family 16h Models 30h-3Fh Processors"
21 * AMD Publication 55570-B1-PUB "Processor Programming Reference (PPR)
22 * for AMD Family 17h Model 18h, Revision B1
24 * AMD Publication 55772-A1-PUB "Processor Programming Reference (PPR)
25 * for AMD Family 17h Model 20h, Revision A1
30 * Includes, defines, variables, module parameters, ...
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/init.h>
37 #include <linux/ioport.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/pci.h>
41 #include <linux/platform_device.h>
42 #include <linux/types.h>
43 #include <linux/watchdog.h>
45 #include "sp5100_tco.h"
47 #define TCO_DRIVER_NAME "sp5100-tco"
49 /* internal variables */
56 struct watchdog_device wdd;
57 void __iomem *tcobase;
58 enum tco_reg_layout tco_reg_layout;
61 /* the watchdog platform device */
62 static struct platform_device *sp5100_tco_platform_device;
63 /* the associated PCI device */
64 static struct pci_dev *sp5100_tco_pci;
66 /* module parameters */
68 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
69 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
70 module_param(heartbeat, int, 0);
71 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
72 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
74 static bool nowayout = WATCHDOG_NOWAYOUT;
75 module_param(nowayout, bool, 0);
76 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started."
77 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
80 * Some TCO specific functions
83 static enum tco_reg_layout tco_reg_layout(struct pci_dev *dev)
85 if (dev->vendor == PCI_VENDOR_ID_ATI &&
86 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
87 dev->revision < 0x40) {
89 } else if (dev->vendor == PCI_VENDOR_ID_AMD &&
90 ((dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
91 dev->revision >= 0x41) ||
92 (dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
93 dev->revision >= 0x49))) {
99 static int tco_timer_start(struct watchdog_device *wdd)
101 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
104 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
105 val |= SP5100_WDT_START_STOP_BIT;
106 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
111 static int tco_timer_stop(struct watchdog_device *wdd)
113 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
116 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
117 val &= ~SP5100_WDT_START_STOP_BIT;
118 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
123 static int tco_timer_ping(struct watchdog_device *wdd)
125 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
128 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
129 val |= SP5100_WDT_TRIGGER_BIT;
130 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
135 static int tco_timer_set_timeout(struct watchdog_device *wdd,
138 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
140 /* Write new heartbeat to watchdog */
141 writel(t, SP5100_WDT_COUNT(tco->tcobase));
148 static unsigned int tco_timer_get_timeleft(struct watchdog_device *wdd)
150 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
152 return readl(SP5100_WDT_COUNT(tco->tcobase));
155 static u8 sp5100_tco_read_pm_reg8(u8 index)
157 outb(index, SP5100_IO_PM_INDEX_REG);
158 return inb(SP5100_IO_PM_DATA_REG);
161 static void sp5100_tco_update_pm_reg8(u8 index, u8 reset, u8 set)
165 outb(index, SP5100_IO_PM_INDEX_REG);
166 val = inb(SP5100_IO_PM_DATA_REG);
169 outb(val, SP5100_IO_PM_DATA_REG);
172 static void tco_timer_enable(struct sp5100_tco *tco)
176 switch (tco->tco_reg_layout) {
178 /* For SB800 or later */
179 /* Set the Watchdog timer resolution to 1 sec */
180 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG,
181 0xff, SB800_PM_WATCHDOG_SECOND_RES);
183 /* Enable watchdog decode bit and watchdog timer */
184 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL,
185 ~SB800_PM_WATCHDOG_DISABLE,
186 SB800_PCI_WATCHDOG_DECODE_EN);
189 /* For SP5100 or SB7x0 */
190 /* Enable watchdog decode bit */
191 pci_read_config_dword(sp5100_tco_pci,
192 SP5100_PCI_WATCHDOG_MISC_REG,
195 val |= SP5100_PCI_WATCHDOG_DECODE_EN;
197 pci_write_config_dword(sp5100_tco_pci,
198 SP5100_PCI_WATCHDOG_MISC_REG,
201 /* Enable Watchdog timer and set the resolution to 1 sec */
202 sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL,
203 ~SP5100_PM_WATCHDOG_DISABLE,
204 SP5100_PM_WATCHDOG_SECOND_RES);
207 /* Set the Watchdog timer resolution to 1 sec and enable */
208 sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN3,
209 ~EFCH_PM_WATCHDOG_DISABLE,
210 EFCH_PM_DECODEEN_SECOND_RES);
215 static u32 sp5100_tco_read_pm_reg32(u8 index)
220 for (i = 3; i >= 0; i--)
221 val = (val << 8) + sp5100_tco_read_pm_reg8(index + i);
226 static int sp5100_tco_setupdevice(struct device *dev,
227 struct watchdog_device *wdd)
229 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
230 const char *dev_name;
231 u32 mmio_addr = 0, val;
234 /* Request the IO ports used by this driver */
235 if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
236 SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
237 dev_err(dev, "I/O address 0x%04x already in use\n",
238 SP5100_IO_PM_INDEX_REG);
243 * Determine type of southbridge chipset.
245 switch (tco->tco_reg_layout) {
247 dev_name = SP5100_DEVNAME;
248 mmio_addr = sp5100_tco_read_pm_reg32(SP5100_PM_WATCHDOG_BASE) &
252 dev_name = SB800_DEVNAME;
253 mmio_addr = sp5100_tco_read_pm_reg32(SB800_PM_WATCHDOG_BASE) &
257 dev_name = SB800_DEVNAME;
259 * On Family 17h devices, the EFCH_PM_DECODEEN_WDT_TMREN bit of
260 * EFCH_PM_DECODEEN not only enables the EFCH_PM_WDT_ADDR memory
261 * region, it also enables the watchdog itself.
263 if (boot_cpu_data.x86 == 0x17) {
264 val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
265 if (!(val & EFCH_PM_DECODEEN_WDT_TMREN)) {
266 sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN, 0xff,
267 EFCH_PM_DECODEEN_WDT_TMREN);
270 val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
271 if (val & EFCH_PM_DECODEEN_WDT_TMREN)
272 mmio_addr = EFCH_PM_WDT_ADDR;
278 /* Check MMIO address conflict */
280 !devm_request_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE,
283 dev_dbg(dev, "MMIO address 0x%08x already in use\n",
285 switch (tco->tco_reg_layout) {
288 * Secondly, Find the watchdog timer MMIO address
289 * from SBResource_MMIO register.
291 /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
292 pci_read_config_dword(sp5100_tco_pci,
293 SP5100_SB_RESOURCE_MMIO_BASE,
295 if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN |
296 SB800_ACPI_MMIO_SEL)) !=
297 SB800_ACPI_MMIO_DECODE_EN) {
302 mmio_addr += SB800_PM_WDT_MMIO_OFFSET;
305 /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */
307 sp5100_tco_read_pm_reg32(SB800_PM_ACPI_MMIO_EN);
308 if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN |
309 SB800_ACPI_MMIO_SEL)) !=
310 SB800_ACPI_MMIO_DECODE_EN) {
315 mmio_addr += SB800_PM_WDT_MMIO_OFFSET;
318 val = sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL);
319 if (!(val & EFCH_PM_ISACONTROL_MMIOEN)) {
323 mmio_addr = EFCH_PM_ACPI_MMIO_ADDR +
324 EFCH_PM_ACPI_MMIO_WDT_OFFSET;
327 dev_dbg(dev, "Got 0x%08x from SBResource_MMIO register\n",
329 if (!devm_request_mem_region(dev, mmio_addr,
330 SP5100_WDT_MEM_MAP_SIZE,
332 dev_dbg(dev, "MMIO address 0x%08x already in use\n",
339 tco->tcobase = devm_ioremap(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE);
341 dev_err(dev, "failed to get tcobase address\n");
346 dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", mmio_addr);
348 /* Setup the watchdog timer */
349 tco_timer_enable(tco);
351 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
352 if (val & SP5100_WDT_DISABLED) {
353 dev_err(dev, "Watchdog hardware is disabled\n");
359 * Save WatchDogFired status, because WatchDogFired flag is
362 if (val & SP5100_WDT_FIRED)
363 wdd->bootstatus = WDIOF_CARDRESET;
364 /* Set watchdog action to reset the system */
365 val &= ~SP5100_WDT_ACTION_RESET;
366 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
368 /* Set a reasonable heartbeat before we stop the timer */
369 tco_timer_set_timeout(wdd, wdd->timeout);
372 * Stop the TCO before we change anything so we don't race with
377 release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
382 release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
386 static struct watchdog_info sp5100_tco_wdt_info = {
387 .identity = "SP5100 TCO timer",
388 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
391 static const struct watchdog_ops sp5100_tco_wdt_ops = {
392 .owner = THIS_MODULE,
393 .start = tco_timer_start,
394 .stop = tco_timer_stop,
395 .ping = tco_timer_ping,
396 .set_timeout = tco_timer_set_timeout,
397 .get_timeleft = tco_timer_get_timeleft,
400 static int sp5100_tco_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct watchdog_device *wdd;
404 struct sp5100_tco *tco;
407 tco = devm_kzalloc(dev, sizeof(*tco), GFP_KERNEL);
411 tco->tco_reg_layout = tco_reg_layout(sp5100_tco_pci);
415 wdd->info = &sp5100_tco_wdt_info;
416 wdd->ops = &sp5100_tco_wdt_ops;
417 wdd->timeout = WATCHDOG_HEARTBEAT;
418 wdd->min_timeout = 1;
419 wdd->max_timeout = 0xffff;
421 watchdog_init_timeout(wdd, heartbeat, NULL);
422 watchdog_set_nowayout(wdd, nowayout);
423 watchdog_stop_on_reboot(wdd);
424 watchdog_stop_on_unregister(wdd);
425 watchdog_set_drvdata(wdd, tco);
427 ret = sp5100_tco_setupdevice(dev, wdd);
431 ret = devm_watchdog_register_device(dev, wdd);
435 /* Show module parameters */
436 dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n",
437 wdd->timeout, nowayout);
442 static struct platform_driver sp5100_tco_driver = {
443 .probe = sp5100_tco_probe,
445 .name = TCO_DRIVER_NAME,
450 * Data for PCI driver interface
452 * This data only exists for exporting the supported
453 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
454 * register a pci_driver, because someone else might
455 * want to register another driver on the same PCI id.
457 static const struct pci_device_id sp5100_tco_pci_tbl[] = {
458 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
460 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID,
462 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID,
464 { 0, }, /* End of list */
466 MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
468 static int __init sp5100_tco_init(void)
470 struct pci_dev *dev = NULL;
473 /* Match the PCI device */
474 for_each_pci_dev(dev) {
475 if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
476 sp5100_tco_pci = dev;
484 pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n");
486 err = platform_driver_register(&sp5100_tco_driver);
490 sp5100_tco_platform_device =
491 platform_device_register_simple(TCO_DRIVER_NAME, -1, NULL, 0);
492 if (IS_ERR(sp5100_tco_platform_device)) {
493 err = PTR_ERR(sp5100_tco_platform_device);
494 goto unreg_platform_driver;
499 unreg_platform_driver:
500 platform_driver_unregister(&sp5100_tco_driver);
504 static void __exit sp5100_tco_exit(void)
506 platform_device_unregister(sp5100_tco_platform_device);
507 platform_driver_unregister(&sp5100_tco_driver);
510 module_init(sp5100_tco_init);
511 module_exit(sp5100_tco_exit);
513 MODULE_AUTHOR("Priyanka Gupta");
514 MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
515 MODULE_LICENSE("GPL");