blob: dd9a744f82f850c0a70df0e343b6d202e078d109 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Priyanka Gupta15e28bf2010-10-25 17:58:04 -07002/*
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. Klimov2ab77a32020-07-13 22:58:21 +020010 * https://www.kernelconcepts.de
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070011 *
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +090012 * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide",
Thomas Weißschuh4d3d50f2021-09-28 08:57:35 +020013 * AMD Publication 44413 "AMD SP5100 Register Reference Guide"
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +090014 * AMD Publication 45482 "AMD SB800-Series Southbridges Register
15 * Reference Guide"
Guenter Roeck887d2ec2017-12-24 13:04:17 -080016 * 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 Roeck09da89a2020-09-10 09:31:09 -070021 * 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 Gupta15e28bf2010-10-25 17:58:04 -070027 */
28
29/*
30 * Includes, defines, variables, module parameters, ...
31 */
32
Joe Perches27c766a2012-02-15 15:06:19 -080033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080035#include <linux/init.h>
36#include <linux/io.h>
37#include <linux/ioport.h>
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070038#include <linux/module.h>
39#include <linux/moduleparam.h>
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070040#include <linux/pci.h>
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070041#include <linux/platform_device.h>
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080042#include <linux/types.h>
43#include <linux/watchdog.h>
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070044
45#include "sp5100_tco.h"
46
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080047#define TCO_DRIVER_NAME "sp5100-tco"
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070048
49/* internal variables */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080050
Guenter Roeck887d2ec2017-12-24 13:04:17 -080051enum tco_reg_layout {
52 sp5100, sb800, efch
53};
54
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080055struct sp5100_tco {
56 struct watchdog_device wdd;
57 void __iomem *tcobase;
Guenter Roeck887d2ec2017-12-24 13:04:17 -080058 enum tco_reg_layout tco_reg_layout;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080059};
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070060
61/* the watchdog platform device */
62static struct platform_device *sp5100_tco_platform_device;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080063/* the associated PCI device */
64static struct pci_dev *sp5100_tco_pci;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070065
66/* module parameters */
67
68#define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
69static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
70module_param(heartbeat, int, 0);
71MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
72 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
73
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010074static bool nowayout = WATCHDOG_NOWAYOUT;
75module_param(nowayout, bool, 0);
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +090076MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started."
Priyanka Gupta15e28bf2010-10-25 17:58:04 -070077 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
78
79/*
80 * Some TCO specific functions
81 */
Lucas Stach46856fa2016-05-03 19:15:58 +020082
Guenter Roeck887d2ec2017-12-24 13:04:17 -080083static enum tco_reg_layout tco_reg_layout(struct pci_dev *dev)
Lucas Stach46856fa2016-05-03 19:15:58 +020084{
Guenter Roeck887d2ec2017-12-24 13:04:17 -080085 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 Stach46856fa2016-05-03 19:15:58 +020097}
98
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -080099static int tco_timer_start(struct watchdog_device *wdd)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700100{
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800101 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700102 u32 val;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700103
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800104 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700105 val |= SP5100_WDT_START_STOP_BIT;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800106 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
107
108 return 0;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700109}
110
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800111static int tco_timer_stop(struct watchdog_device *wdd)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700112{
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800113 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700114 u32 val;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700115
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800116 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700117 val &= ~SP5100_WDT_START_STOP_BIT;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800118 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
119
120 return 0;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700121}
122
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800123static int tco_timer_ping(struct watchdog_device *wdd)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700124{
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800125 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700126 u32 val;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700127
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800128 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700129 val |= SP5100_WDT_TRIGGER_BIT;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800130 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
131
132 return 0;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700133}
134
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800135static int tco_timer_set_timeout(struct watchdog_device *wdd,
136 unsigned int t)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700137{
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800138 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700139
140 /* Write new heartbeat to watchdog */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800141 writel(t, SP5100_WDT_COUNT(tco->tcobase));
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700142
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800143 wdd->timeout = t;
144
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700145 return 0;
146}
147
Thomas Weißschuh4d3d50f2021-09-28 08:57:35 +0200148static 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 Roeck2b750cf2017-12-24 13:04:06 -0800155static 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
161static 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 Roeck887d2ec2017-12-24 13:04:17 -0800172static void tco_timer_enable(struct sp5100_tco *tco)
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900173{
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800174 u32 val;
175
176 switch (tco->tco_reg_layout) {
177 case sb800:
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900178 /* For SB800 or later */
179 /* Set the Watchdog timer resolution to 1 sec */
Guenter Roeck2b750cf2017-12-24 13:04:06 -0800180 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONFIG,
181 0xff, SB800_PM_WATCHDOG_SECOND_RES);
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900182
183 /* Enable watchdog decode bit and watchdog timer */
Guenter Roeck2b750cf2017-12-24 13:04:06 -0800184 sp5100_tco_update_pm_reg8(SB800_PM_WATCHDOG_CONTROL,
185 ~SB800_PM_WATCHDOG_DISABLE,
186 SB800_PCI_WATCHDOG_DECODE_EN);
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800187 break;
188 case sp5100:
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900189 /* 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 Roeck2b750cf2017-12-24 13:04:06 -0800202 sp5100_tco_update_pm_reg8(SP5100_PM_WATCHDOG_CONTROL,
203 ~SP5100_PM_WATCHDOG_DISABLE,
204 SP5100_PM_WATCHDOG_SECOND_RES);
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800205 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 Tanaka740fbdd2012-12-02 14:33:18 +0900212 }
213}
214
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800215static u32 sp5100_tco_read_pm_reg32(u8 index)
Guenter Roeck2b750cf2017-12-24 13:04:06 -0800216{
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 Roeck7cd9d5f2017-12-24 13:04:14 -0800226static int sp5100_tco_setupdevice(struct device *dev,
227 struct watchdog_device *wdd)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700228{
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800229 struct sp5100_tco *tco = watchdog_get_drvdata(wdd);
230 const char *dev_name;
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800231 u32 mmio_addr = 0, val;
Guenter Roeck23dfe142017-12-24 13:04:09 -0800232 int ret;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700233
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700234 /* Request the IO ports used by this driver */
Guenter Roeck16e77302017-12-24 13:04:08 -0800235 if (!request_muxed_region(SP5100_IO_PM_INDEX_REG,
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800236 SP5100_PM_IOPORTS_SIZE, "sp5100_tco")) {
Guenter Roeckfd8f9092017-12-24 13:04:12 -0800237 dev_err(dev, "I/O address 0x%04x already in use\n",
238 SP5100_IO_PM_INDEX_REG);
Guenter Roeck23dfe142017-12-24 13:04:09 -0800239 return -EBUSY;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700240 }
241
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900242 /*
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800243 * Determine type of southbridge chipset.
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900244 */
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800245 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 Roeck09da89a2020-09-10 09:31:09 -0700258 /*
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 Roeck887d2ec2017-12-24 13:04:17 -0800270 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 Tanaka740fbdd2012-12-02 14:33:18 +0900277
278 /* Check MMIO address conflict */
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800279 if (!mmio_addr ||
280 !devm_request_mem_region(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE,
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800281 dev_name)) {
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800282 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 Roecke1894102017-12-24 13:04:10 -0800291 /* Read SBResource_MMIO from PCI config(PCI_Reg: 9Ch) */
292 pci_read_config_dword(sp5100_tco_pci,
293 SP5100_SB_RESOURCE_MMIO_BASE,
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800294 &mmio_addr);
295 if ((mmio_addr & (SB800_ACPI_MMIO_DECODE_EN |
296 SB800_ACPI_MMIO_SEL)) !=
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900297 SB800_ACPI_MMIO_DECODE_EN) {
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800298 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 Roecke1894102017-12-24 13:04:10 -0800326 }
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800327 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 Roeck7cd9d5f2017-12-24 13:04:14 -0800331 dev_name)) {
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800332 dev_dbg(dev, "MMIO address 0x%08x already in use\n",
333 mmio_addr);
Guenter Roecke1894102017-12-24 13:04:10 -0800334 ret = -EBUSY;
335 goto unreg_region;
336 }
Guenter Roecke1894102017-12-24 13:04:10 -0800337 }
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900338
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800339 tco->tcobase = devm_ioremap(dev, mmio_addr, SP5100_WDT_MEM_MAP_SIZE);
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800340 if (!tco->tcobase) {
Guenter Roeckfd8f9092017-12-24 13:04:12 -0800341 dev_err(dev, "failed to get tcobase address\n");
Guenter Roeck23dfe142017-12-24 13:04:09 -0800342 ret = -ENOMEM;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800343 goto unreg_region;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700344 }
345
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800346 dev_info(dev, "Using 0x%08x for watchdog MMIO address\n", mmio_addr);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700347
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900348 /* Setup the watchdog timer */
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800349 tco_timer_enable(tco);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700350
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800351 val = readl(SP5100_WDT_CONTROL(tco->tcobase));
Guenter Roeckf7781b02017-12-24 13:04:16 -0800352 if (val & SP5100_WDT_DISABLED) {
353 dev_err(dev, "Watchdog hardware is disabled\n");
354 ret = -ENODEV;
355 goto unreg_region;
356 }
357
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900358 /*
359 * Save WatchDogFired status, because WatchDogFired flag is
360 * cleared here.
361 */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800362 if (val & SP5100_WDT_FIRED)
363 wdd->bootstatus = WDIOF_CARDRESET;
Guenter Roeckf7781b02017-12-24 13:04:16 -0800364 /* Set watchdog action to reset the system */
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800365 val &= ~SP5100_WDT_ACTION_RESET;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800366 writel(val, SP5100_WDT_CONTROL(tco->tcobase));
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700367
368 /* Set a reasonable heartbeat before we stop the timer */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800369 tco_timer_set_timeout(wdd, wdd->timeout);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700370
371 /*
372 * Stop the TCO before we change anything so we don't race with
373 * a zeroed timer.
374 */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800375 tco_timer_stop(wdd);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700376
Guenter Roeck16e77302017-12-24 13:04:08 -0800377 release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
Guenter Roecke1894102017-12-24 13:04:10 -0800378
Guenter Roeck23dfe142017-12-24 13:04:09 -0800379 return 0;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700380
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700381unreg_region:
Guenter Roeck2b750cf2017-12-24 13:04:06 -0800382 release_region(SP5100_IO_PM_INDEX_REG, SP5100_PM_IOPORTS_SIZE);
Guenter Roeck23dfe142017-12-24 13:04:09 -0800383 return ret;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700384}
385
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800386static struct watchdog_info sp5100_tco_wdt_info = {
387 .identity = "SP5100 TCO timer",
388 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
389};
390
391static 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ßschuh4d3d50f2021-09-28 08:57:35 +0200397 .get_timeleft = tco_timer_get_timeleft,
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800398};
399
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800400static int sp5100_tco_probe(struct platform_device *pdev)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700401{
Guenter Roeckfd8f9092017-12-24 13:04:12 -0800402 struct device *dev = &pdev->dev;
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800403 struct watchdog_device *wdd;
404 struct sp5100_tco *tco;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700405 int ret;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700406
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800407 tco = devm_kzalloc(dev, sizeof(*tco), GFP_KERNEL);
408 if (!tco)
409 return -ENOMEM;
410
Guenter Roeck887d2ec2017-12-24 13:04:17 -0800411 tco->tco_reg_layout = tco_reg_layout(sp5100_tco_pci);
412
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800413 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 Sang2d505e32019-04-19 20:15:57 +0200421 watchdog_init_timeout(wdd, heartbeat, NULL);
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800422 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 Roeck23dfe142017-12-24 13:04:09 -0800428 if (ret)
429 return ret;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700430
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800431 ret = devm_watchdog_register_device(dev, wdd);
Wolfram Sangd41e3f42019-05-18 23:27:52 +0200432 if (ret)
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800433 return ret;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700434
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900435 /* Show module parameters */
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800436 dev_info(dev, "initialized. heartbeat=%d sec (nowayout=%d)\n",
437 wdd->timeout, nowayout);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700438
439 return 0;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700440}
441
442static struct platform_driver sp5100_tco_driver = {
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800443 .probe = sp5100_tco_probe,
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700444 .driver = {
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800445 .name = TCO_DRIVER_NAME,
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700446 },
447};
448
Guenter Roecka34834432017-12-24 13:04:11 -0800449/*
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 */
457static 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};
466MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
467
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800468static int __init sp5100_tco_init(void)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700469{
Guenter Roecka34834432017-12-24 13:04:11 -0800470 struct pci_dev *dev = NULL;
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700471 int err;
472
Guenter Roecka34834432017-12-24 13:04:11 -0800473 /* 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 Roeck7cd9d5f2017-12-24 13:04:14 -0800484 pr_info("SP5100/SB800 TCO WatchDog Timer Driver\n");
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700485
486 err = platform_driver_register(&sp5100_tco_driver);
487 if (err)
488 return err;
489
Guenter Roeck7cd9d5f2017-12-24 13:04:14 -0800490 sp5100_tco_platform_device =
491 platform_device_register_simple(TCO_DRIVER_NAME, -1, NULL, 0);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700492 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
499unreg_platform_driver:
500 platform_driver_unregister(&sp5100_tco_driver);
501 return err;
502}
503
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800504static void __exit sp5100_tco_exit(void)
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700505{
506 platform_device_unregister(sp5100_tco_platform_device);
507 platform_driver_unregister(&sp5100_tco_driver);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700508}
509
Guenter Roeck5bbecc52017-12-24 13:04:13 -0800510module_init(sp5100_tco_init);
511module_exit(sp5100_tco_exit);
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700512
513MODULE_AUTHOR("Priyanka Gupta");
Takahisa Tanaka740fbdd2012-12-02 14:33:18 +0900514MODULE_DESCRIPTION("TCO timer driver for SP5100/SB800 chipset");
Priyanka Gupta15e28bf2010-10-25 17:58:04 -0700515MODULE_LICENSE("GPL");