Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 2 | /* |
| 3 | * sp5100_tco : TCO timer driver for sp5100 chipsets |
| 4 | * |
| 5 | * (c) Copyright 2009 Google Inc., All Rights Reserved. |
| 6 | * |
| 7 | * Based on i8xx_tco.c: |
| 8 | * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights |
| 9 | * Reserved. |
Alexander A. Klimov | 2ab77a3 | 2020-07-13 22:58:21 +0200 | [diff] [blame] | 10 | * https://www.kernelconcepts.de |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 11 | * |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 12 | * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide", |
Thomas Weißschuh | 4d3d50f | 2021-09-28 08:57:35 +0200 | [diff] [blame] | 13 | * AMD Publication 44413 "AMD SP5100 Register Reference Guide" |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 14 | * AMD Publication 45482 "AMD SB800-Series Southbridges Register |
| 15 | * Reference Guide" |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 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" |
Guenter Roeck | 09da89a | 2020-09-10 09:31:09 -0700 | [diff] [blame] | 21 | * AMD Publication 55570-B1-PUB "Processor Programming Reference (PPR) |
| 22 | * for AMD Family 17h Model 18h, Revision B1 |
| 23 | * Processors (PUB) |
| 24 | * AMD Publication 55772-A1-PUB "Processor Programming Reference (PPR) |
| 25 | * for AMD Family 17h Model 20h, Revision A1 |
| 26 | * Processors (PUB) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * Includes, defines, variables, module parameters, ... |
| 31 | */ |
| 32 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 34 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 35 | #include <linux/init.h> |
| 36 | #include <linux/io.h> |
| 37 | #include <linux/ioport.h> |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | #include <linux/moduleparam.h> |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 40 | #include <linux/pci.h> |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 41 | #include <linux/platform_device.h> |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 42 | #include <linux/types.h> |
| 43 | #include <linux/watchdog.h> |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 44 | |
| 45 | #include "sp5100_tco.h" |
| 46 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 47 | #define TCO_DRIVER_NAME "sp5100-tco" |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 48 | |
| 49 | /* internal variables */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 50 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 51 | enum tco_reg_layout { |
| 52 | sp5100, sb800, efch |
| 53 | }; |
| 54 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 55 | struct sp5100_tco { |
| 56 | struct watchdog_device wdd; |
| 57 | void __iomem *tcobase; |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 58 | enum tco_reg_layout tco_reg_layout; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 59 | }; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 60 | |
| 61 | /* the watchdog platform device */ |
| 62 | static struct platform_device *sp5100_tco_platform_device; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 63 | /* the associated PCI device */ |
| 64 | static struct pci_dev *sp5100_tco_pci; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 65 | |
| 66 | /* module parameters */ |
| 67 | |
| 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) ")"); |
| 73 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 74 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 75 | module_param(nowayout, bool, 0); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 76 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started." |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 77 | " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 78 | |
| 79 | /* |
| 80 | * Some TCO specific functions |
| 81 | */ |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 82 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 83 | static enum tco_reg_layout tco_reg_layout(struct pci_dev *dev) |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 84 | { |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 85 | if (dev->vendor == PCI_VENDOR_ID_ATI && |
| 86 | dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS && |
| 87 | dev->revision < 0x40) { |
| 88 | return sp5100; |
| 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))) { |
| 94 | return efch; |
| 95 | } |
| 96 | return sb800; |
Lucas Stach | 46856fa | 2016-05-03 19:15:58 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 99 | static int tco_timer_start(struct watchdog_device *wdd) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 100 | { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 101 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 102 | u32 val; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 103 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 104 | val = readl(SP5100_WDT_CONTROL(tco->tcobase)); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 105 | val |= SP5100_WDT_START_STOP_BIT; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 106 | writel(val, SP5100_WDT_CONTROL(tco->tcobase)); |
| 107 | |
| 108 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 111 | static int tco_timer_stop(struct watchdog_device *wdd) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 112 | { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 113 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 114 | u32 val; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 115 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 116 | val = readl(SP5100_WDT_CONTROL(tco->tcobase)); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 117 | val &= ~SP5100_WDT_START_STOP_BIT; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 118 | writel(val, SP5100_WDT_CONTROL(tco->tcobase)); |
| 119 | |
| 120 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 123 | static int tco_timer_ping(struct watchdog_device *wdd) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 124 | { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 125 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 126 | u32 val; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 127 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 128 | val = readl(SP5100_WDT_CONTROL(tco->tcobase)); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 129 | val |= SP5100_WDT_TRIGGER_BIT; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 130 | writel(val, SP5100_WDT_CONTROL(tco->tcobase)); |
| 131 | |
| 132 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 135 | static int tco_timer_set_timeout(struct watchdog_device *wdd, |
| 136 | unsigned int t) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 137 | { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 138 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 139 | |
| 140 | /* Write new heartbeat to watchdog */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 141 | writel(t, SP5100_WDT_COUNT(tco->tcobase)); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 142 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 143 | wdd->timeout = t; |
| 144 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Thomas Weißschuh | 4d3d50f | 2021-09-28 08:57:35 +0200 | [diff] [blame] | 148 | static unsigned int tco_timer_get_timeleft(struct watchdog_device *wdd) |
| 149 | { |
| 150 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
| 151 | |
| 152 | return readl(SP5100_WDT_COUNT(tco->tcobase)); |
| 153 | } |
| 154 | |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 155 | static u8 sp5100_tco_read_pm_reg8(u8 index) |
| 156 | { |
| 157 | outb(index, SP5100_IO_PM_INDEX_REG); |
| 158 | return inb(SP5100_IO_PM_DATA_REG); |
| 159 | } |
| 160 | |
| 161 | static void sp5100_tco_update_pm_reg8(u8 index, u8 reset, u8 set) |
| 162 | { |
| 163 | u8 val; |
| 164 | |
| 165 | outb(index, SP5100_IO_PM_INDEX_REG); |
| 166 | val = inb(SP5100_IO_PM_DATA_REG); |
| 167 | val &= reset; |
| 168 | val |= set; |
| 169 | outb(val, SP5100_IO_PM_DATA_REG); |
| 170 | } |
| 171 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 172 | static void tco_timer_enable(struct sp5100_tco *tco) |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 173 | { |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 174 | u32 val; |
| 175 | |
| 176 | switch (tco->tco_reg_layout) { |
| 177 | case sb800: |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 178 | /* For SB800 or later */ |
| 179 | /* Set the Watchdog timer resolution to 1 sec */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 180 | sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG, |
| 181 | 0xff, SB800_PM_WATCHDOG_SECOND_RES); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 182 | |
| 183 | /* Enable watchdog decode bit and watchdog timer */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 184 | sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL, |
| 185 | ~SB800_PM_WATCHDOG_DISABLE, |
| 186 | SB800_PCI_WATCHDOG_DECODE_EN); |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 187 | break; |
| 188 | case sp5100: |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 189 | /* For SP5100 or SB7x0 */ |
| 190 | /* Enable watchdog decode bit */ |
| 191 | pci_read_config_dword(sp5100_tco_pci, |
| 192 | SP5100_PCI_WATCHDOG_MISC_REG, |
| 193 | &val); |
| 194 | |
| 195 | val |= SP5100_PCI_WATCHDOG_DECODE_EN; |
| 196 | |
| 197 | pci_write_config_dword(sp5100_tco_pci, |
| 198 | SP5100_PCI_WATCHDOG_MISC_REG, |
| 199 | val); |
| 200 | |
| 201 | /* Enable Watchdog timer and set the resolution to 1 sec */ |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 202 | sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL, |
| 203 | ~SP5100_PM_WATCHDOG_DISABLE, |
| 204 | SP5100_PM_WATCHDOG_SECOND_RES); |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 205 | break; |
| 206 | case efch: |
| 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); |
| 211 | break; |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 215 | static u32 sp5100_tco_read_pm_reg32(u8 index) |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 216 | { |
| 217 | u32 val = 0; |
| 218 | int i; |
| 219 | |
| 220 | for (i = 3; i >= 0; i--) |
| 221 | val = (val << 8) + sp5100_tco_read_pm_reg8(index + i); |
| 222 | |
| 223 | return val; |
| 224 | } |
| 225 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 226 | static int sp5100_tco_setupdevice(struct device *dev, |
| 227 | struct watchdog_device *wdd) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 228 | { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 229 | struct sp5100_tco *tco = watchdog_get_drvdata(wdd); |
| 230 | const char *dev_name; |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 231 | u32 mmio_addr = 0, val; |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 232 | int ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 233 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 234 | /* Request the IO ports used by this driver */ |
Guenter Roeck | 16e7730 | 2017-12-24 13:04:08 -0800 | [diff] [blame] | 235 | if (!request_muxed_region(SP5100_IO_PM_INDEX_REG, |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 236 | SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame] | 237 | dev_err(dev, "I/O address 0x%04x already in use\n", |
| 238 | SP5100_IO_PM_INDEX_REG); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 239 | return -EBUSY; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 242 | /* |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 243 | * Determine type of southbridge chipset. |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 244 | */ |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 245 | switch (tco->tco_reg_layout) { |
| 246 | case sp5100: |
| 247 | dev_name = SP5100_DEVNAME; |
| 248 | mmio_addr = sp5100_tco_read_pm_reg32(SP5100_PM_WATCHDOG_BASE) & |
| 249 | 0xfffffff8; |
| 250 | break; |
| 251 | case sb800: |
| 252 | dev_name = SB800_DEVNAME; |
| 253 | mmio_addr = sp5100_tco_read_pm_reg32(SB800_PM_WATCHDOG_BASE) & |
| 254 | 0xfffffff8; |
| 255 | break; |
| 256 | case efch: |
| 257 | dev_name = SB800_DEVNAME; |
Guenter Roeck | 09da89a | 2020-09-10 09:31:09 -0700 | [diff] [blame] | 258 | /* |
| 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. |
| 262 | */ |
| 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); |
| 268 | } |
| 269 | } |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 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; |
| 273 | break; |
| 274 | default: |
| 275 | return -ENODEV; |
| 276 | } |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 277 | |
| 278 | /* Check MMIO address conflict */ |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 279 | if (!mmio_addr || |
| 280 | !devm_request_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE, |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 281 | dev_name)) { |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 282 | if (mmio_addr) |
| 283 | dev_dbg(dev, "MMIO address 0x%08x already in use\n", |
| 284 | mmio_addr); |
| 285 | switch (tco->tco_reg_layout) { |
| 286 | case sp5100: |
| 287 | /* |
| 288 | * Secondly, Find the watchdog timer MMIO address |
| 289 | * from SBResource_MMIO register. |
| 290 | */ |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 291 | /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */ |
| 292 | pci_read_config_dword(sp5100_tco_pci, |
| 293 | SP5100_SB_RESOURCE_MMIO_BASE, |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 294 | &mmio_addr); |
| 295 | if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN | |
| 296 | SB800_ACPI_MMIO_SEL)) != |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 297 | SB800_ACPI_MMIO_DECODE_EN) { |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 298 | ret = -ENODEV; |
| 299 | goto unreg_region; |
| 300 | } |
| 301 | mmio_addr &= ~0xFFF; |
| 302 | mmio_addr += SB800_PM_WDT_MMIO_OFFSET; |
| 303 | break; |
| 304 | case sb800: |
| 305 | /* Read SBResource_MMIO from AcpiMmioEn(PM_Reg: 24h) */ |
| 306 | mmio_addr = |
| 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) { |
| 311 | ret = -ENODEV; |
| 312 | goto unreg_region; |
| 313 | } |
| 314 | mmio_addr &= ~0xFFF; |
| 315 | mmio_addr += SB800_PM_WDT_MMIO_OFFSET; |
| 316 | break; |
| 317 | case efch: |
| 318 | val = sp5100_tco_read_pm_reg8(EFCH_PM_ISACONTROL); |
| 319 | if (!(val & EFCH_PM_ISACONTROL_MMIOEN)) { |
| 320 | ret = -ENODEV; |
| 321 | goto unreg_region; |
| 322 | } |
| 323 | mmio_addr = EFCH_PM_ACPI_MMIO_ADDR + |
| 324 | EFCH_PM_ACPI_MMIO_WDT_OFFSET; |
| 325 | break; |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 326 | } |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 327 | dev_dbg(dev, "Got 0x%08x from SBResource_MMIO register\n", |
| 328 | mmio_addr); |
| 329 | if (!devm_request_mem_region(dev, mmio_addr, |
| 330 | SP5100_WDT_MEM_MAP_SIZE, |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 331 | dev_name)) { |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 332 | dev_dbg(dev, "MMIO address 0x%08x already in use\n", |
| 333 | mmio_addr); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 334 | ret = -EBUSY; |
| 335 | goto unreg_region; |
| 336 | } |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 337 | } |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 338 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 339 | tco->tcobase = devm_ioremap(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE); |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 340 | if (!tco->tcobase) { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame] | 341 | dev_err(dev, "failed to get tcobase address\n"); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 342 | ret = -ENOMEM; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 343 | goto unreg_region; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 346 | dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", mmio_addr); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 347 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 348 | /* Setup the watchdog timer */ |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 349 | tco_timer_enable(tco); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 350 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 351 | val = readl(SP5100_WDT_CONTROL(tco->tcobase)); |
Guenter Roeck | f7781b0 | 2017-12-24 13:04:16 -0800 | [diff] [blame] | 352 | if (val & SP5100_WDT_DISABLED) { |
| 353 | dev_err(dev, "Watchdog hardware is disabled\n"); |
| 354 | ret = -ENODEV; |
| 355 | goto unreg_region; |
| 356 | } |
| 357 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 358 | /* |
| 359 | * Save WatchDogFired status, because WatchDogFired flag is |
| 360 | * cleared here. |
| 361 | */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 362 | if (val & SP5100_WDT_FIRED) |
| 363 | wdd->bootstatus = WDIOF_CARDRESET; |
Guenter Roeck | f7781b0 | 2017-12-24 13:04:16 -0800 | [diff] [blame] | 364 | /* Set watchdog action to reset the system */ |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 365 | val &= ~SP5100_WDT_ACTION_RESET; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 366 | writel(val, SP5100_WDT_CONTROL(tco->tcobase)); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 367 | |
| 368 | /* Set a reasonable heartbeat before we stop the timer */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 369 | tco_timer_set_timeout(wdd, wdd->timeout); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Stop the TCO before we change anything so we don't race with |
| 373 | * a zeroed timer. |
| 374 | */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 375 | tco_timer_stop(wdd); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 376 | |
Guenter Roeck | 16e7730 | 2017-12-24 13:04:08 -0800 | [diff] [blame] | 377 | release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE); |
Guenter Roeck | e189410 | 2017-12-24 13:04:10 -0800 | [diff] [blame] | 378 | |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 379 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 380 | |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 381 | unreg_region: |
Guenter Roeck | 2b750cf | 2017-12-24 13:04:06 -0800 | [diff] [blame] | 382 | release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 383 | return ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 386 | static struct watchdog_info sp5100_tco_wdt_info = { |
| 387 | .identity = "SP5100 TCO timer", |
| 388 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 389 | }; |
| 390 | |
| 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, |
Thomas Weißschuh | 4d3d50f | 2021-09-28 08:57:35 +0200 | [diff] [blame] | 397 | .get_timeleft = tco_timer_get_timeleft, |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 398 | }; |
| 399 | |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 400 | static int sp5100_tco_probe(struct platform_device *pdev) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 401 | { |
Guenter Roeck | fd8f909 | 2017-12-24 13:04:12 -0800 | [diff] [blame] | 402 | struct device *dev = &pdev->dev; |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 403 | struct watchdog_device *wdd; |
| 404 | struct sp5100_tco *tco; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 405 | int ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 406 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 407 | tco = devm_kzalloc(dev, sizeof(*tco), GFP_KERNEL); |
| 408 | if (!tco) |
| 409 | return -ENOMEM; |
| 410 | |
Guenter Roeck | 887d2ec | 2017-12-24 13:04:17 -0800 | [diff] [blame] | 411 | tco->tco_reg_layout = tco_reg_layout(sp5100_tco_pci); |
| 412 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 413 | wdd = &tco->wdd; |
| 414 | wdd->parent = dev; |
| 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; |
| 420 | |
Wolfram Sang | 2d505e3 | 2019-04-19 20:15:57 +0200 | [diff] [blame] | 421 | watchdog_init_timeout(wdd, heartbeat, NULL); |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 422 | watchdog_set_nowayout(wdd, nowayout); |
| 423 | watchdog_stop_on_reboot(wdd); |
| 424 | watchdog_stop_on_unregister(wdd); |
| 425 | watchdog_set_drvdata(wdd, tco); |
| 426 | |
| 427 | ret = sp5100_tco_setupdevice(dev, wdd); |
Guenter Roeck | 23dfe14 | 2017-12-24 13:04:09 -0800 | [diff] [blame] | 428 | if (ret) |
| 429 | return ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 430 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 431 | ret = devm_watchdog_register_device(dev, wdd); |
Wolfram Sang | d41e3f4 | 2019-05-18 23:27:52 +0200 | [diff] [blame] | 432 | if (ret) |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 433 | return ret; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 434 | |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 435 | /* Show module parameters */ |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 436 | dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n", |
| 437 | wdd->timeout, nowayout); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 438 | |
| 439 | return 0; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static struct platform_driver sp5100_tco_driver = { |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 443 | .probe = sp5100_tco_probe, |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 444 | .driver = { |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 445 | .name = TCO_DRIVER_NAME, |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 446 | }, |
| 447 | }; |
| 448 | |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 449 | /* |
| 450 | * Data for PCI driver interface |
| 451 | * |
| 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. |
| 456 | */ |
| 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, |
| 459 | PCI_ANY_ID, }, |
| 460 | { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, PCI_ANY_ID, |
| 461 | PCI_ANY_ID, }, |
| 462 | { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS, PCI_ANY_ID, |
| 463 | PCI_ANY_ID, }, |
| 464 | { 0, }, /* End of list */ |
| 465 | }; |
| 466 | MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl); |
| 467 | |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 468 | static int __init sp5100_tco_init(void) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 469 | { |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 470 | struct pci_dev *dev = NULL; |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 471 | int err; |
| 472 | |
Guenter Roeck | a3483443 | 2017-12-24 13:04:11 -0800 | [diff] [blame] | 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; |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (!sp5100_tco_pci) |
| 482 | return -ENODEV; |
| 483 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 484 | pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n"); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 485 | |
| 486 | err = platform_driver_register(&sp5100_tco_driver); |
| 487 | if (err) |
| 488 | return err; |
| 489 | |
Guenter Roeck | 7cd9d5f | 2017-12-24 13:04:14 -0800 | [diff] [blame] | 490 | sp5100_tco_platform_device = |
| 491 | platform_device_register_simple(TCO_DRIVER_NAME, -1, NULL, 0); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 492 | if (IS_ERR(sp5100_tco_platform_device)) { |
| 493 | err = PTR_ERR(sp5100_tco_platform_device); |
| 494 | goto unreg_platform_driver; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | |
| 499 | unreg_platform_driver: |
| 500 | platform_driver_unregister(&sp5100_tco_driver); |
| 501 | return err; |
| 502 | } |
| 503 | |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 504 | static void __exit sp5100_tco_exit(void) |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 505 | { |
| 506 | platform_device_unregister(sp5100_tco_platform_device); |
| 507 | platform_driver_unregister(&sp5100_tco_driver); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Guenter Roeck | 5bbecc5 | 2017-12-24 13:04:13 -0800 | [diff] [blame] | 510 | module_init(sp5100_tco_init); |
| 511 | module_exit(sp5100_tco_exit); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 512 | |
| 513 | MODULE_AUTHOR("Priyanka Gupta"); |
Takahisa Tanaka | 740fbdd | 2012-12-02 14:33:18 +0900 | [diff] [blame] | 514 | MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset"); |
Priyanka Gupta | 15e28bf | 2010-10-25 17:58:04 -0700 | [diff] [blame] | 515 | MODULE_LICENSE("GPL"); |