12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
215e28bf1SPriyanka Gupta /*
315e28bf1SPriyanka Gupta * sp5100_tco : TCO timer driver for sp5100 chipsets
415e28bf1SPriyanka Gupta *
515e28bf1SPriyanka Gupta * (c) Copyright 2009 Google Inc., All Rights Reserved.
615e28bf1SPriyanka Gupta *
715e28bf1SPriyanka Gupta * Based on i8xx_tco.c:
815e28bf1SPriyanka Gupta * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
915e28bf1SPriyanka Gupta * Reserved.
102ab77a34SAlexander A. Klimov * https://www.kernelconcepts.de
1115e28bf1SPriyanka Gupta *
12740fbddfSTakahisa Tanaka * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide",
134d3d50f6SThomas Weißschuh * AMD Publication 44413 "AMD SP5100 Register Reference Guide"
14740fbddfSTakahisa Tanaka * AMD Publication 45482 "AMD SB800-Series Southbridges Register
15740fbddfSTakahisa Tanaka * Reference Guide"
16887d2ec5SGuenter Roeck * AMD Publication 48751 "BIOS and Kernel Developer’s Guide (BKDG)
17887d2ec5SGuenter Roeck * for AMD Family 16h Models 00h-0Fh Processors"
18887d2ec5SGuenter Roeck * AMD Publication 51192 "AMD Bolton FCH Register Reference Guide"
19887d2ec5SGuenter Roeck * AMD Publication 52740 "BIOS and Kernel Developer’s Guide (BKDG)
20887d2ec5SGuenter Roeck * for AMD Family 16h Models 30h-3Fh Processors"
2109da89abSGuenter Roeck * AMD Publication 55570-B1-PUB "Processor Programming Reference (PPR)
2209da89abSGuenter Roeck * for AMD Family 17h Model 18h, Revision B1
2309da89abSGuenter Roeck * Processors (PUB)
2409da89abSGuenter Roeck * AMD Publication 55772-A1-PUB "Processor Programming Reference (PPR)
2509da89abSGuenter Roeck * for AMD Family 17h Model 20h, Revision A1
2609da89abSGuenter Roeck * Processors (PUB)
2715e28bf1SPriyanka Gupta */
2815e28bf1SPriyanka Gupta
2915e28bf1SPriyanka Gupta /*
3015e28bf1SPriyanka Gupta * Includes, defines, variables, module parameters, ...
3115e28bf1SPriyanka Gupta */
3215e28bf1SPriyanka Gupta
3327c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3427c766aaSJoe Perches
357cd9d5ffSGuenter Roeck #include <linux/init.h>
367cd9d5ffSGuenter Roeck #include <linux/io.h>
377cd9d5ffSGuenter Roeck #include <linux/ioport.h>
3815e28bf1SPriyanka Gupta #include <linux/module.h>
3915e28bf1SPriyanka Gupta #include <linux/moduleparam.h>
4015e28bf1SPriyanka Gupta #include <linux/pci.h>
4115e28bf1SPriyanka Gupta #include <linux/platform_device.h>
427cd9d5ffSGuenter Roeck #include <linux/types.h>
437cd9d5ffSGuenter Roeck #include <linux/watchdog.h>
4415e28bf1SPriyanka Gupta
4515e28bf1SPriyanka Gupta #include "sp5100_tco.h"
4615e28bf1SPriyanka Gupta
477cd9d5ffSGuenter Roeck #define TCO_DRIVER_NAME "sp5100-tco"
4815e28bf1SPriyanka Gupta
4915e28bf1SPriyanka Gupta /* internal variables */
507cd9d5ffSGuenter Roeck
51887d2ec5SGuenter Roeck enum tco_reg_layout {
520578fff4STerry Bowman sp5100, sb800, efch, efch_mmio
53887d2ec5SGuenter Roeck };
54887d2ec5SGuenter Roeck
557cd9d5ffSGuenter Roeck struct sp5100_tco {
567cd9d5ffSGuenter Roeck struct watchdog_device wdd;
577cd9d5ffSGuenter Roeck void __iomem *tcobase;
58887d2ec5SGuenter Roeck enum tco_reg_layout tco_reg_layout;
597cd9d5ffSGuenter Roeck };
6015e28bf1SPriyanka Gupta
6115e28bf1SPriyanka Gupta /* the watchdog platform device */
6215e28bf1SPriyanka Gupta static struct platform_device *sp5100_tco_platform_device;
637cd9d5ffSGuenter Roeck /* the associated PCI device */
647cd9d5ffSGuenter Roeck static struct pci_dev *sp5100_tco_pci;
6515e28bf1SPriyanka Gupta
6615e28bf1SPriyanka Gupta /* module parameters */
6715e28bf1SPriyanka Gupta
68081574f7SVladimir Panteleev #define WATCHDOG_ACTION 0
69081574f7SVladimir Panteleev static bool action = WATCHDOG_ACTION;
70081574f7SVladimir Panteleev module_param(action, bool, 0);
71081574f7SVladimir Panteleev MODULE_PARM_DESC(action, "Action taken when watchdog expires, 0 to reset, 1 to poweroff (default="
72081574f7SVladimir Panteleev __MODULE_STRING(WATCHDOG_ACTION) ")");
73081574f7SVladimir Panteleev
7415e28bf1SPriyanka Gupta #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
7515e28bf1SPriyanka Gupta static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
7615e28bf1SPriyanka Gupta module_param(heartbeat, int, 0);
7715e28bf1SPriyanka Gupta MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
7815e28bf1SPriyanka Gupta __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
7915e28bf1SPriyanka Gupta
8086a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT;
8186a1e189SWim Van Sebroeck module_param(nowayout, bool, 0);
82740fbddfSTakahisa Tanaka MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started."
8315e28bf1SPriyanka Gupta " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
8415e28bf1SPriyanka Gupta
8515e28bf1SPriyanka Gupta /*
8615e28bf1SPriyanka Gupta * Some TCO specific functions
8715e28bf1SPriyanka Gupta */
8846856fabSLucas Stach
tco_reg_layout(struct pci_dev * dev)89887d2ec5SGuenter Roeck static enum tco_reg_layout tco_reg_layout(struct pci_dev *dev)
9046856fabSLucas Stach {
91887d2ec5SGuenter Roeck if (dev->vendor == PCI_VENDOR_ID_ATI &&
92887d2ec5SGuenter Roeck dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
93887d2ec5SGuenter Roeck dev->revision < 0x40) {
94887d2ec5SGuenter Roeck return sp5100;
95887d2ec5SGuenter Roeck } else if (dev->vendor == PCI_VENDOR_ID_AMD &&
9682627037STerry Bowman sp5100_tco_pci->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
9782627037STerry Bowman sp5100_tco_pci->revision >= AMD_ZEN_SMBUS_PCI_REV) {
9882627037STerry Bowman return efch_mmio;
99*009637deSYuechao Zhao } else if ((dev->vendor == PCI_VENDOR_ID_AMD || dev->vendor == PCI_VENDOR_ID_HYGON) &&
100887d2ec5SGuenter Roeck ((dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
101887d2ec5SGuenter Roeck dev->revision >= 0x41) ||
102887d2ec5SGuenter Roeck (dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
103887d2ec5SGuenter Roeck dev->revision >= 0x49))) {
104887d2ec5SGuenter Roeck return efch;
105887d2ec5SGuenter Roeck }
106887d2ec5SGuenter Roeck return sb800;
10746856fabSLucas Stach }
10846856fabSLucas Stach
tco_timer_start(struct watchdog_device * wdd)1097cd9d5ffSGuenter Roeck static int tco_timer_start(struct watchdog_device *wdd)
11015e28bf1SPriyanka Gupta {
1117cd9d5ffSGuenter Roeck struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
11215e28bf1SPriyanka Gupta u32 val;
11315e28bf1SPriyanka Gupta
1147cd9d5ffSGuenter Roeck val = readl(SP5100_WDT_CONTROL(tco->tcobase));
11515e28bf1SPriyanka Gupta val |= SP5100_WDT_START_STOP_BIT;
1167cd9d5ffSGuenter Roeck writel(val, SP5100_WDT_CONTROL(tco->tcobase));
1177cd9d5ffSGuenter Roeck
1184eda19ccSGregory Oakes /* This must be a distinct write. */
1194eda19ccSGregory Oakes val |= SP5100_WDT_TRIGGER_BIT;
1204eda19ccSGregory Oakes writel(val, SP5100_WDT_CONTROL(tco->tcobase));
1214eda19ccSGregory Oakes
1227cd9d5ffSGuenter Roeck return 0;
12315e28bf1SPriyanka Gupta }
12415e28bf1SPriyanka Gupta
tco_timer_stop(struct watchdog_device * wdd)1257cd9d5ffSGuenter Roeck static int tco_timer_stop(struct watchdog_device *wdd)
12615e28bf1SPriyanka Gupta {
1277cd9d5ffSGuenter Roeck struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
12815e28bf1SPriyanka Gupta u32 val;
12915e28bf1SPriyanka Gupta
1307cd9d5ffSGuenter Roeck val = readl(SP5100_WDT_CONTROL(tco->tcobase));
13115e28bf1SPriyanka Gupta val &= ~SP5100_WDT_START_STOP_BIT;
1327cd9d5ffSGuenter Roeck writel(val, SP5100_WDT_CONTROL(tco->tcobase));
1337cd9d5ffSGuenter Roeck
1347cd9d5ffSGuenter Roeck return 0;
13515e28bf1SPriyanka Gupta }
13615e28bf1SPriyanka Gupta
tco_timer_ping(struct watchdog_device * wdd)1377cd9d5ffSGuenter Roeck static int tco_timer_ping(struct watchdog_device *wdd)
13815e28bf1SPriyanka Gupta {
1397cd9d5ffSGuenter Roeck struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
14015e28bf1SPriyanka Gupta u32 val;
14115e28bf1SPriyanka Gupta
1427cd9d5ffSGuenter Roeck val = readl(SP5100_WDT_CONTROL(tco->tcobase));
14315e28bf1SPriyanka Gupta val |= SP5100_WDT_TRIGGER_BIT;
1447cd9d5ffSGuenter Roeck writel(val, SP5100_WDT_CONTROL(tco->tcobase));
1457cd9d5ffSGuenter Roeck
1467cd9d5ffSGuenter Roeck return 0;
14715e28bf1SPriyanka Gupta }
14815e28bf1SPriyanka Gupta
tco_timer_set_timeout(struct watchdog_device * wdd,unsigned int t)1497cd9d5ffSGuenter Roeck static int tco_timer_set_timeout(struct watchdog_device *wdd,
1507cd9d5ffSGuenter Roeck unsigned int t)
15115e28bf1SPriyanka Gupta {
1527cd9d5ffSGuenter Roeck struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
15315e28bf1SPriyanka Gupta
15415e28bf1SPriyanka Gupta /* Write new heartbeat to watchdog */
1557cd9d5ffSGuenter Roeck writel(t, SP5100_WDT_COUNT(tco->tcobase));
15615e28bf1SPriyanka Gupta
1577cd9d5ffSGuenter Roeck wdd->timeout = t;
1587cd9d5ffSGuenter Roeck
15915e28bf1SPriyanka Gupta return 0;
16015e28bf1SPriyanka Gupta }
16115e28bf1SPriyanka Gupta
tco_timer_get_timeleft(struct watchdog_device * wdd)1624d3d50f6SThomas Weißschuh static unsigned int tco_timer_get_timeleft(struct watchdog_device *wdd)
1634d3d50f6SThomas Weißschuh {
1644d3d50f6SThomas Weißschuh struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
1654d3d50f6SThomas Weißschuh
1664d3d50f6SThomas Weißschuh return readl(SP5100_WDT_COUNT(tco->tcobase));
1674d3d50f6SThomas Weißschuh }
1684d3d50f6SThomas Weißschuh
sp5100_tco_read_pm_reg8(u8 index)1692b750cffSGuenter Roeck static u8 sp5100_tco_read_pm_reg8(u8 index)
1702b750cffSGuenter Roeck {
1712b750cffSGuenter Roeck outb(index, SP5100_IO_PM_INDEX_REG);
1722b750cffSGuenter Roeck return inb(SP5100_IO_PM_DATA_REG);
1732b750cffSGuenter Roeck }
1742b750cffSGuenter Roeck
sp5100_tco_update_pm_reg8(u8 index,u8 reset,u8 set)1752b750cffSGuenter Roeck static void sp5100_tco_update_pm_reg8(u8 index, u8 reset, u8 set)
1762b750cffSGuenter Roeck {
1772b750cffSGuenter Roeck u8 val;
1782b750cffSGuenter Roeck
1792b750cffSGuenter Roeck outb(index, SP5100_IO_PM_INDEX_REG);
1802b750cffSGuenter Roeck val = inb(SP5100_IO_PM_DATA_REG);
1812b750cffSGuenter Roeck val &= reset;
1822b750cffSGuenter Roeck val |= set;
1832b750cffSGuenter Roeck outb(val, SP5100_IO_PM_DATA_REG);
1842b750cffSGuenter Roeck }
1852b750cffSGuenter Roeck
tco_timer_enable(struct sp5100_tco * tco)186887d2ec5SGuenter Roeck static void tco_timer_enable(struct sp5100_tco *tco)
187740fbddfSTakahisa Tanaka {
188887d2ec5SGuenter Roeck u32 val;
189887d2ec5SGuenter Roeck
190887d2ec5SGuenter Roeck switch (tco->tco_reg_layout) {
191887d2ec5SGuenter Roeck case sb800:
192740fbddfSTakahisa Tanaka /* For SB800 or later */
193740fbddfSTakahisa Tanaka /* Set the Watchdog timer resolution to 1 sec */
1942b750cffSGuenter Roeck sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG,
1952b750cffSGuenter Roeck 0xff, SB800_PM_WATCHDOG_SECOND_RES);
196740fbddfSTakahisa Tanaka
197740fbddfSTakahisa Tanaka /* Enable watchdog decode bit and watchdog timer */
1982b750cffSGuenter Roeck sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL,
1992b750cffSGuenter Roeck ~SB800_PM_WATCHDOG_DISABLE,
2002b750cffSGuenter Roeck SB800_PCI_WATCHDOG_DECODE_EN);
201887d2ec5SGuenter Roeck break;
202887d2ec5SGuenter Roeck case sp5100:
203740fbddfSTakahisa Tanaka /* For SP5100 or SB7x0 */
204740fbddfSTakahisa Tanaka /* Enable watchdog decode bit */
205740fbddfSTakahisa Tanaka pci_read_config_dword(sp5100_tco_pci,
206740fbddfSTakahisa Tanaka SP5100_PCI_WATCHDOG_MISC_REG,
207740fbddfSTakahisa Tanaka &val);
208740fbddfSTakahisa Tanaka
209740fbddfSTakahisa Tanaka val |= SP5100_PCI_WATCHDOG_DECODE_EN;
210740fbddfSTakahisa Tanaka
211740fbddfSTakahisa Tanaka pci_write_config_dword(sp5100_tco_pci,
212740fbddfSTakahisa Tanaka SP5100_PCI_WATCHDOG_MISC_REG,
213740fbddfSTakahisa Tanaka val);
214740fbddfSTakahisa Tanaka
215740fbddfSTakahisa Tanaka /* Enable Watchdog timer and set the resolution to 1 sec */
2162b750cffSGuenter Roeck sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL,
2172b750cffSGuenter Roeck ~SP5100_PM_WATCHDOG_DISABLE,
2182b750cffSGuenter Roeck SP5100_PM_WATCHDOG_SECOND_RES);
219887d2ec5SGuenter Roeck break;
220887d2ec5SGuenter Roeck case efch:
221887d2ec5SGuenter Roeck /* Set the Watchdog timer resolution to 1 sec and enable */
222887d2ec5SGuenter Roeck sp5100_tco_update_pm_reg8(EFCH_PM_DECODEEN3,
223887d2ec5SGuenter Roeck ~EFCH_PM_WATCHDOG_DISABLE,
224887d2ec5SGuenter Roeck EFCH_PM_DECODEEN_SECOND_RES);
225887d2ec5SGuenter Roeck break;
2260578fff4STerry Bowman default:
2270578fff4STerry Bowman break;
228740fbddfSTakahisa Tanaka }
229740fbddfSTakahisa Tanaka }
230740fbddfSTakahisa Tanaka
sp5100_tco_read_pm_reg32(u8 index)2317cd9d5ffSGuenter Roeck static u32 sp5100_tco_read_pm_reg32(u8 index)
2322b750cffSGuenter Roeck {
2332b750cffSGuenter Roeck u32 val = 0;
2342b750cffSGuenter Roeck int i;
2352b750cffSGuenter Roeck
2362b750cffSGuenter Roeck for (i = 3; i >= 0; i--)
2372b750cffSGuenter Roeck val = (val << 8) + sp5100_tco_read_pm_reg8(index + i);
2382b750cffSGuenter Roeck
2392b750cffSGuenter Roeck return val;
2402b750cffSGuenter Roeck }
2412b750cffSGuenter Roeck
sp5100_tco_request_region(struct device * dev,u32 mmio_addr,const char * dev_name)2421f182acaSTerry Bowman static u32 sp5100_tco_request_region(struct device *dev,
2431f182acaSTerry Bowman u32 mmio_addr,
2441f182acaSTerry Bowman const char *dev_name)
2451f182acaSTerry Bowman {
2461f182acaSTerry Bowman if (!devm_request_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE,
2471f182acaSTerry Bowman dev_name)) {
2481f182acaSTerry Bowman dev_dbg(dev, "MMIO address 0x%08x already in use\n", mmio_addr);
2491f182acaSTerry Bowman return 0;
2501f182acaSTerry Bowman }
2511f182acaSTerry Bowman
2521f182acaSTerry Bowman return mmio_addr;
2531f182acaSTerry Bowman }
2541f182acaSTerry Bowman
sp5100_tco_prepare_base(struct sp5100_tco * tco,u32 mmio_addr,u32 alt_mmio_addr,const char * dev_name)2551f182acaSTerry Bowman static u32 sp5100_tco_prepare_base(struct sp5100_tco *tco,
2561f182acaSTerry Bowman u32 mmio_addr,
2571f182acaSTerry Bowman u32 alt_mmio_addr,
2581f182acaSTerry Bowman const char *dev_name)
2591f182acaSTerry Bowman {
2601f182acaSTerry Bowman struct device *dev = tco->wdd.parent;
2611f182acaSTerry Bowman
2621f182acaSTerry Bowman dev_dbg(dev, "Got 0x%08x from SBResource_MMIO register\n", mmio_addr);
2631f182acaSTerry Bowman
2641f182acaSTerry Bowman if (!mmio_addr && !alt_mmio_addr)
2651f182acaSTerry Bowman return -ENODEV;
2661f182acaSTerry Bowman
2671f182acaSTerry Bowman /* Check for MMIO address and alternate MMIO address conflicts */
2681f182acaSTerry Bowman if (mmio_addr)
2691f182acaSTerry Bowman mmio_addr = sp5100_tco_request_region(dev, mmio_addr, dev_name);
2701f182acaSTerry Bowman
2711f182acaSTerry Bowman if (!mmio_addr && alt_mmio_addr)
2721f182acaSTerry Bowman mmio_addr = sp5100_tco_request_region(dev, alt_mmio_addr, dev_name);
2731f182acaSTerry Bowman
2741f182acaSTerry Bowman if (!mmio_addr) {
2751f182acaSTerry Bowman dev_err(dev, "Failed to reserve MMIO or alternate MMIO region\n");
2761f182acaSTerry Bowman return -EBUSY;
2771f182acaSTerry Bowman }
2781f182acaSTerry Bowman
2791f182acaSTerry Bowman tco->tcobase = devm_ioremap(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE);
2801f182acaSTerry Bowman if (!tco->tcobase) {
2811f182acaSTerry Bowman dev_err(dev, "MMIO address 0x%08x failed mapping\n", mmio_addr);
2821f182acaSTerry Bowman devm_release_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE);
2831f182acaSTerry Bowman return -ENOMEM;
2841f182acaSTerry Bowman }
2851f182acaSTerry Bowman
2861f182acaSTerry Bowman dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", mmio_addr);
2871f182acaSTerry Bowman
2881f182acaSTerry Bowman return 0;
2891f182acaSTerry Bowman }
2901f182acaSTerry Bowman
sp5100_tco_timer_init(struct sp5100_tco * tco)291abd71a94STerry Bowman static int sp5100_tco_timer_init(struct sp5100_tco *tco)
292abd71a94STerry Bowman {
293abd71a94STerry Bowman struct watchdog_device *wdd = &tco->wdd;
294abd71a94STerry Bowman struct device *dev = wdd->parent;
295abd71a94STerry Bowman u32 val;
296abd71a94STerry Bowman
297abd71a94STerry Bowman val = readl(SP5100_WDT_CONTROL(tco->tcobase));
298abd71a94STerry Bowman if (val & SP5100_WDT_DISABLED) {
299abd71a94STerry Bowman dev_err(dev, "Watchdog hardware is disabled\n");
300abd71a94STerry Bowman return -ENODEV;
301abd71a94STerry Bowman }
302abd71a94STerry Bowman
303abd71a94STerry Bowman /*
304abd71a94STerry Bowman * Save WatchDogFired status, because WatchDogFired flag is
305abd71a94STerry Bowman * cleared here.
306abd71a94STerry Bowman */
307abd71a94STerry Bowman if (val & SP5100_WDT_FIRED)
308abd71a94STerry Bowman wdd->bootstatus = WDIOF_CARDRESET;
309abd71a94STerry Bowman
310081574f7SVladimir Panteleev /* Set watchdog action */
311081574f7SVladimir Panteleev if (action)
312081574f7SVladimir Panteleev val |= SP5100_WDT_ACTION_RESET;
313081574f7SVladimir Panteleev else
314abd71a94STerry Bowman val &= ~SP5100_WDT_ACTION_RESET;
315abd71a94STerry Bowman writel(val, SP5100_WDT_CONTROL(tco->tcobase));
316abd71a94STerry Bowman
317abd71a94STerry Bowman /* Set a reasonable heartbeat before we stop the timer */
318abd71a94STerry Bowman tco_timer_set_timeout(wdd, wdd->timeout);
319abd71a94STerry Bowman
320abd71a94STerry Bowman /*
321abd71a94STerry Bowman * Stop the TCO before we change anything so we don't race with
322abd71a94STerry Bowman * a zeroed timer.
323abd71a94STerry Bowman */
324abd71a94STerry Bowman tco_timer_stop(wdd);
325abd71a94STerry Bowman
326abd71a94STerry Bowman return 0;
327abd71a94STerry Bowman }
328abd71a94STerry Bowman
efch_read_pm_reg8(void __iomem * addr,u8 index)3290578fff4STerry Bowman static u8 efch_read_pm_reg8(void __iomem *addr, u8 index)
3300578fff4STerry Bowman {
3310578fff4STerry Bowman return readb(addr + index);
3320578fff4STerry Bowman }
3330578fff4STerry Bowman
efch_update_pm_reg8(void __iomem * addr,u8 index,u8 reset,u8 set)3340578fff4STerry Bowman static void efch_update_pm_reg8(void __iomem *addr, u8 index, u8 reset, u8 set)
3350578fff4STerry Bowman {
3360578fff4STerry Bowman u8 val;
3370578fff4STerry Bowman
3380578fff4STerry Bowman val = readb(addr + index);
3390578fff4STerry Bowman val &= reset;
3400578fff4STerry Bowman val |= set;
3410578fff4STerry Bowman writeb(val, addr + index);
3420578fff4STerry Bowman }
3430578fff4STerry Bowman
tco_timer_enable_mmio(void __iomem * addr)3440578fff4STerry Bowman static void tco_timer_enable_mmio(void __iomem *addr)
3450578fff4STerry Bowman {
3460578fff4STerry Bowman efch_update_pm_reg8(addr, EFCH_PM_DECODEEN3,
3470578fff4STerry Bowman ~EFCH_PM_WATCHDOG_DISABLE,
3480578fff4STerry Bowman EFCH_PM_DECODEEN_SECOND_RES);
3490578fff4STerry Bowman }
3500578fff4STerry Bowman
sp5100_tco_setupdevice_mmio(struct device * dev,struct watchdog_device * wdd)3510578fff4STerry Bowman static int sp5100_tco_setupdevice_mmio(struct device *dev,
3520578fff4STerry Bowman struct watchdog_device *wdd)
3530578fff4STerry Bowman {
3540578fff4STerry Bowman struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
3550578fff4STerry Bowman const char *dev_name = SB800_DEVNAME;
3560578fff4STerry Bowman u32 mmio_addr = 0, alt_mmio_addr = 0;
3570578fff4STerry Bowman struct resource *res;
3580578fff4STerry Bowman void __iomem *addr;
3590578fff4STerry Bowman int ret;
3600578fff4STerry Bowman u32 val;
3610578fff4STerry Bowman
3620578fff4STerry Bowman res = request_mem_region_muxed(EFCH_PM_ACPI_MMIO_PM_ADDR,
3630578fff4STerry Bowman EFCH_PM_ACPI_MMIO_PM_SIZE,
3640578fff4STerry Bowman "sp5100_tco");
3650578fff4STerry Bowman
3660578fff4STerry Bowman if (!res) {
3670578fff4STerry Bowman dev_err(dev,
3680578fff4STerry Bowman "Memory region 0x%08x already in use\n",
3690578fff4STerry Bowman EFCH_PM_ACPI_MMIO_PM_ADDR);
3700578fff4STerry Bowman return -EBUSY;
3710578fff4STerry Bowman }
3720578fff4STerry Bowman
3730578fff4STerry Bowman addr = ioremap(EFCH_PM_ACPI_MMIO_PM_ADDR, EFCH_PM_ACPI_MMIO_PM_SIZE);
3740578fff4STerry Bowman if (!addr) {
3750578fff4STerry Bowman dev_err(dev, "Address mapping failed\n");
3760578fff4STerry Bowman ret = -ENOMEM;
3770578fff4STerry Bowman goto out;
3780578fff4STerry Bowman }
3790578fff4STerry Bowman
3800578fff4STerry Bowman /*
3810578fff4STerry Bowman * EFCH_PM_DECODEEN_WDT_TMREN is dual purpose. This bitfield
3820578fff4STerry Bowman * enables sp5100_tco register MMIO space decoding. The bitfield
3830578fff4STerry Bowman * also starts the timer operation. Enable if not already enabled.
3840578fff4STerry Bowman */
3850578fff4STerry Bowman val = efch_read_pm_reg8(addr, EFCH_PM_DECODEEN);
3860578fff4STerry Bowman if (!(val & EFCH_PM_DECODEEN_WDT_TMREN)) {
3870578fff4STerry Bowman efch_update_pm_reg8(addr, EFCH_PM_DECODEEN, 0xff,
3880578fff4STerry Bowman EFCH_PM_DECODEEN_WDT_TMREN);
3890578fff4STerry Bowman }
3900578fff4STerry Bowman
3910578fff4STerry Bowman /* Error if the timer could not be enabled */
3920578fff4STerry Bowman val = efch_read_pm_reg8(addr, EFCH_PM_DECODEEN);
3930578fff4STerry Bowman if (!(val & EFCH_PM_DECODEEN_WDT_TMREN)) {
3940578fff4STerry Bowman dev_err(dev, "Failed to enable the timer\n");
3950578fff4STerry Bowman ret = -EFAULT;
3960578fff4STerry Bowman goto out;
3970578fff4STerry Bowman }
3980578fff4STerry Bowman
3990578fff4STerry Bowman mmio_addr = EFCH_PM_WDT_ADDR;
4000578fff4STerry Bowman
4010578fff4STerry Bowman /* Determine alternate MMIO base address */
4020578fff4STerry Bowman val = efch_read_pm_reg8(addr, EFCH_PM_ISACONTROL);
4030578fff4STerry Bowman if (val & EFCH_PM_ISACONTROL_MMIOEN)
4040578fff4STerry Bowman alt_mmio_addr = EFCH_PM_ACPI_MMIO_ADDR +
4050578fff4STerry Bowman EFCH_PM_ACPI_MMIO_WDT_OFFSET;
4060578fff4STerry Bowman
4070578fff4STerry Bowman ret = sp5100_tco_prepare_base(tco, mmio_addr, alt_mmio_addr, dev_name);
4080578fff4STerry Bowman if (!ret) {
4090578fff4STerry Bowman tco_timer_enable_mmio(addr);
4100578fff4STerry Bowman ret = sp5100_tco_timer_init(tco);
4110578fff4STerry Bowman }
4120578fff4STerry Bowman
4130578fff4STerry Bowman out:
4140578fff4STerry Bowman if (addr)
4150578fff4STerry Bowman iounmap(addr);
4160578fff4STerry Bowman
4170578fff4STerry Bowman release_resource(res);
418c6d9c079SJean Delvare kfree(res);
4190578fff4STerry Bowman
4200578fff4STerry Bowman return ret;
4210578fff4STerry Bowman }
4220578fff4STerry Bowman
sp5100_tco_setupdevice(struct device * dev,struct watchdog_device * wdd)4237cd9d5ffSGuenter Roeck static int sp5100_tco_setupdevice(struct device *dev,
4247cd9d5ffSGuenter Roeck struct watchdog_device *wdd)
42515e28bf1SPriyanka Gupta {
4267cd9d5ffSGuenter Roeck struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
4277cd9d5ffSGuenter Roeck const char *dev_name;
428887d2ec5SGuenter Roeck u32 mmio_addr = 0, val;
4291f182acaSTerry Bowman u32 alt_mmio_addr = 0;
43023dfe140SGuenter Roeck int ret;
43115e28bf1SPriyanka Gupta
4320578fff4STerry Bowman if (tco->tco_reg_layout == efch_mmio)
4330578fff4STerry Bowman return sp5100_tco_setupdevice_mmio(dev, wdd);
4340578fff4STerry Bowman
43515e28bf1SPriyanka Gupta /* Request the IO ports used by this driver */
43616e7730bSGuenter Roeck if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
437887d2ec5SGuenter Roeck SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
438fd8f9093SGuenter Roeck dev_err(dev, "I/O address 0x%04x already in use\n",
4392b750cffSGuenter Roeck SP5100_IO_PM_INDEX_REG);
44023dfe140SGuenter Roeck return -EBUSY;
44115e28bf1SPriyanka Gupta }
44215e28bf1SPriyanka Gupta
443740fbddfSTakahisa Tanaka /*
444887d2ec5SGuenter Roeck * Determine type of southbridge chipset.
445740fbddfSTakahisa Tanaka */
446887d2ec5SGuenter Roeck switch (tco->tco_reg_layout) {
447887d2ec5SGuenter Roeck case sp5100:
448887d2ec5SGuenter Roeck dev_name = SP5100_DEVNAME;
449887d2ec5SGuenter Roeck mmio_addr = sp5100_tco_read_pm_reg32(SP5100_PM_WATCHDOG_BASE) &
450887d2ec5SGuenter Roeck 0xfffffff8;
4511f182acaSTerry Bowman
4521f182acaSTerry Bowman /*
4531f182acaSTerry Bowman * Secondly, find the watchdog timer MMIO address
4541f182acaSTerry Bowman * from SBResource_MMIO register.
4551f182acaSTerry Bowman */
4561f182acaSTerry Bowman
4571f182acaSTerry Bowman /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
4581f182acaSTerry Bowman pci_read_config_dword(sp5100_tco_pci,
4591f182acaSTerry Bowman SP5100_SB_RESOURCE_MMIO_BASE,
4601f182acaSTerry Bowman &val);
4611f182acaSTerry Bowman
4621f182acaSTerry Bowman /* Verify MMIO is enabled and using bar0 */
4631f182acaSTerry Bowman if ((val & SB800_ACPI_MMIO_MASK) == SB800_ACPI_MMIO_DECODE_EN)
4641f182acaSTerry Bowman alt_mmio_addr = (val & ~0xfff) + SB800_PM_WDT_MMIO_OFFSET;
465887d2ec5SGuenter Roeck break;
466887d2ec5SGuenter Roeck case sb800:
467887d2ec5SGuenter Roeck dev_name = SB800_DEVNAME;
468887d2ec5SGuenter Roeck mmio_addr = sp5100_tco_read_pm_reg32(SB800_PM_WATCHDOG_BASE) &
469887d2ec5SGuenter Roeck 0xfffffff8;
4701f182acaSTerry Bowman
4711f182acaSTerry Bowman /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */
4721f182acaSTerry Bowman val = sp5100_tco_read_pm_reg32(SB800_PM_ACPI_MMIO_EN);
4731f182acaSTerry Bowman
4741f182acaSTerry Bowman /* Verify MMIO is enabled and using bar0 */
4751f182acaSTerry Bowman if ((val & SB800_ACPI_MMIO_MASK) == SB800_ACPI_MMIO_DECODE_EN)
4761f182acaSTerry Bowman alt_mmio_addr = (val & ~0xfff) + SB800_PM_WDT_MMIO_OFFSET;
477887d2ec5SGuenter Roeck break;
478887d2ec5SGuenter Roeck case efch:
479887d2ec5SGuenter Roeck dev_name = SB800_DEVNAME;
480887d2ec5SGuenter Roeck val = sp5100_tco_read_pm_reg8(EFCH_PM_DECODEEN);
481887d2ec5SGuenter Roeck if (val & EFCH_PM_DECODEEN_WDT_TMREN)
482887d2ec5SGuenter Roeck mmio_addr = EFCH_PM_WDT_ADDR;
4831f182acaSTerry Bowman
4841f182acaSTerry Bowman val = sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL);
4851f182acaSTerry Bowman if (val & EFCH_PM_ISACONTROL_MMIOEN)
4861f182acaSTerry Bowman alt_mmio_addr = EFCH_PM_ACPI_MMIO_ADDR +
4871f182acaSTerry Bowman EFCH_PM_ACPI_MMIO_WDT_OFFSET;
488887d2ec5SGuenter Roeck break;
489887d2ec5SGuenter Roeck default:
490887d2ec5SGuenter Roeck return -ENODEV;
491887d2ec5SGuenter Roeck }
492740fbddfSTakahisa Tanaka
4931f182acaSTerry Bowman ret = sp5100_tco_prepare_base(tco, mmio_addr, alt_mmio_addr, dev_name);
4941f182acaSTerry Bowman if (!ret) {
495740fbddfSTakahisa Tanaka /* Setup the watchdog timer */
496887d2ec5SGuenter Roeck tco_timer_enable(tco);
497abd71a94STerry Bowman ret = sp5100_tco_timer_init(tco);
4981f182acaSTerry Bowman }
49915e28bf1SPriyanka Gupta
5002b750cffSGuenter Roeck release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
50123dfe140SGuenter Roeck return ret;
50215e28bf1SPriyanka Gupta }
50315e28bf1SPriyanka Gupta
5047cd9d5ffSGuenter Roeck static struct watchdog_info sp5100_tco_wdt_info = {
5057cd9d5ffSGuenter Roeck .identity = "SP5100 TCO timer",
5067cd9d5ffSGuenter Roeck .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
5077cd9d5ffSGuenter Roeck };
5087cd9d5ffSGuenter Roeck
5097cd9d5ffSGuenter Roeck static const struct watchdog_ops sp5100_tco_wdt_ops = {
5107cd9d5ffSGuenter Roeck .owner = THIS_MODULE,
5117cd9d5ffSGuenter Roeck .start = tco_timer_start,
5127cd9d5ffSGuenter Roeck .stop = tco_timer_stop,
5137cd9d5ffSGuenter Roeck .ping = tco_timer_ping,
5147cd9d5ffSGuenter Roeck .set_timeout = tco_timer_set_timeout,
5154d3d50f6SThomas Weißschuh .get_timeleft = tco_timer_get_timeleft,
5167cd9d5ffSGuenter Roeck };
5177cd9d5ffSGuenter Roeck
sp5100_tco_probe(struct platform_device * pdev)5185bbecc5dSGuenter Roeck static int sp5100_tco_probe(struct platform_device *pdev)
51915e28bf1SPriyanka Gupta {
520fd8f9093SGuenter Roeck struct device *dev = &pdev->dev;
5217cd9d5ffSGuenter Roeck struct watchdog_device *wdd;
5227cd9d5ffSGuenter Roeck struct sp5100_tco *tco;
52315e28bf1SPriyanka Gupta int ret;
52415e28bf1SPriyanka Gupta
5257cd9d5ffSGuenter Roeck tco = devm_kzalloc(dev, sizeof(*tco), GFP_KERNEL);
5267cd9d5ffSGuenter Roeck if (!tco)
5277cd9d5ffSGuenter Roeck return -ENOMEM;
5287cd9d5ffSGuenter Roeck
529887d2ec5SGuenter Roeck tco->tco_reg_layout = tco_reg_layout(sp5100_tco_pci);
530887d2ec5SGuenter Roeck
5317cd9d5ffSGuenter Roeck wdd = &tco->wdd;
5327cd9d5ffSGuenter Roeck wdd->parent = dev;
5337cd9d5ffSGuenter Roeck wdd->info = &sp5100_tco_wdt_info;
5347cd9d5ffSGuenter Roeck wdd->ops = &sp5100_tco_wdt_ops;
5357cd9d5ffSGuenter Roeck wdd->timeout = WATCHDOG_HEARTBEAT;
5367cd9d5ffSGuenter Roeck wdd->min_timeout = 1;
5377cd9d5ffSGuenter Roeck wdd->max_timeout = 0xffff;
5387cd9d5ffSGuenter Roeck
5392d505e3eSWolfram Sang watchdog_init_timeout(wdd, heartbeat, NULL);
5407cd9d5ffSGuenter Roeck watchdog_set_nowayout(wdd, nowayout);
5417cd9d5ffSGuenter Roeck watchdog_stop_on_reboot(wdd);
5427cd9d5ffSGuenter Roeck watchdog_stop_on_unregister(wdd);
5437cd9d5ffSGuenter Roeck watchdog_set_drvdata(wdd, tco);
5447cd9d5ffSGuenter Roeck
5457cd9d5ffSGuenter Roeck ret = sp5100_tco_setupdevice(dev, wdd);
54623dfe140SGuenter Roeck if (ret)
54723dfe140SGuenter Roeck return ret;
54815e28bf1SPriyanka Gupta
5497cd9d5ffSGuenter Roeck ret = devm_watchdog_register_device(dev, wdd);
550d41e3f4eSWolfram Sang if (ret)
55115e28bf1SPriyanka Gupta return ret;
55215e28bf1SPriyanka Gupta
5537cd9d5ffSGuenter Roeck /* Show module parameters */
5547cd9d5ffSGuenter Roeck dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n",
5557cd9d5ffSGuenter Roeck wdd->timeout, nowayout);
55615e28bf1SPriyanka Gupta
55715e28bf1SPriyanka Gupta return 0;
55815e28bf1SPriyanka Gupta }
55915e28bf1SPriyanka Gupta
56015e28bf1SPriyanka Gupta static struct platform_driver sp5100_tco_driver = {
5615bbecc5dSGuenter Roeck .probe = sp5100_tco_probe,
56215e28bf1SPriyanka Gupta .driver = {
5637cd9d5ffSGuenter Roeck .name = TCO_DRIVER_NAME,
56415e28bf1SPriyanka Gupta },
56515e28bf1SPriyanka Gupta };
56615e28bf1SPriyanka Gupta
567a3483443SGuenter Roeck /*
568a3483443SGuenter Roeck * Data for PCI driver interface
569a3483443SGuenter Roeck *
570a3483443SGuenter Roeck * This data only exists for exporting the supported
571a3483443SGuenter Roeck * PCI ids via MODULE_DEVICE_TABLE. We do not actually
572a3483443SGuenter Roeck * register a pci_driver, because someone else might
573a3483443SGuenter Roeck * want to register another driver on the same PCI id.
574a3483443SGuenter Roeck */
575a3483443SGuenter Roeck static const struct pci_device_id sp5100_tco_pci_tbl[] = {
576a3483443SGuenter Roeck { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
577a3483443SGuenter Roeck PCI_ANY_ID, },
578a3483443SGuenter Roeck { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID,
579a3483443SGuenter Roeck PCI_ANY_ID, },
580a3483443SGuenter Roeck { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID,
581a3483443SGuenter Roeck PCI_ANY_ID, },
582*009637deSYuechao Zhao { PCI_VENDOR_ID_HYGON, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID,
583*009637deSYuechao Zhao PCI_ANY_ID, },
584a3483443SGuenter Roeck { 0, }, /* End of list */
585a3483443SGuenter Roeck };
586a3483443SGuenter Roeck MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
587a3483443SGuenter Roeck
sp5100_tco_init(void)5885bbecc5dSGuenter Roeck static int __init sp5100_tco_init(void)
58915e28bf1SPriyanka Gupta {
590a3483443SGuenter Roeck struct pci_dev *dev = NULL;
59115e28bf1SPriyanka Gupta int err;
59215e28bf1SPriyanka Gupta
593a3483443SGuenter Roeck /* Match the PCI device */
594a3483443SGuenter Roeck for_each_pci_dev(dev) {
595a3483443SGuenter Roeck if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
596a3483443SGuenter Roeck sp5100_tco_pci = dev;
597a3483443SGuenter Roeck break;
598a3483443SGuenter Roeck }
599a3483443SGuenter Roeck }
600a3483443SGuenter Roeck
601a3483443SGuenter Roeck if (!sp5100_tco_pci)
602a3483443SGuenter Roeck return -ENODEV;
603a3483443SGuenter Roeck
6047cd9d5ffSGuenter Roeck pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n");
60515e28bf1SPriyanka Gupta
60615e28bf1SPriyanka Gupta err = platform_driver_register(&sp5100_tco_driver);
60715e28bf1SPriyanka Gupta if (err)
60815e28bf1SPriyanka Gupta return err;
60915e28bf1SPriyanka Gupta
6107cd9d5ffSGuenter Roeck sp5100_tco_platform_device =
6117cd9d5ffSGuenter Roeck platform_device_register_simple(TCO_DRIVER_NAME, -1, NULL, 0);
61215e28bf1SPriyanka Gupta if (IS_ERR(sp5100_tco_platform_device)) {
61315e28bf1SPriyanka Gupta err = PTR_ERR(sp5100_tco_platform_device);
61415e28bf1SPriyanka Gupta goto unreg_platform_driver;
61515e28bf1SPriyanka Gupta }
61615e28bf1SPriyanka Gupta
61715e28bf1SPriyanka Gupta return 0;
61815e28bf1SPriyanka Gupta
61915e28bf1SPriyanka Gupta unreg_platform_driver:
62015e28bf1SPriyanka Gupta platform_driver_unregister(&sp5100_tco_driver);
62115e28bf1SPriyanka Gupta return err;
62215e28bf1SPriyanka Gupta }
62315e28bf1SPriyanka Gupta
sp5100_tco_exit(void)6245bbecc5dSGuenter Roeck static void __exit sp5100_tco_exit(void)
62515e28bf1SPriyanka Gupta {
62615e28bf1SPriyanka Gupta platform_device_unregister(sp5100_tco_platform_device);
62715e28bf1SPriyanka Gupta platform_driver_unregister(&sp5100_tco_driver);
62815e28bf1SPriyanka Gupta }
62915e28bf1SPriyanka Gupta
6305bbecc5dSGuenter Roeck module_init(sp5100_tco_init);
6315bbecc5dSGuenter Roeck module_exit(sp5100_tco_exit);
63215e28bf1SPriyanka Gupta
63315e28bf1SPriyanka Gupta MODULE_AUTHOR("Priyanka Gupta");
634740fbddfSTakahisa Tanaka MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
63515e28bf1SPriyanka Gupta MODULE_LICENSE("GPL");
636