1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * VIA Chipset Watchdog Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2011 Sigfox
6*4882a593Smuzhiyun * Author: Marc Vertes <marc.vertes@sigfox.com>
7*4882a593Smuzhiyun * Based on a preliminary version from Harald Welte <HaraldWelte@viatech.com>
8*4882a593Smuzhiyun * Timer code by Wim Van Sebroeck <wim@iguana.be>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Caveat: PnP must be enabled in BIOS to allow full access to watchdog
11*4882a593Smuzhiyun * control registers. If not, the watchdog must be configured in BIOS manually.
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/device.h>
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/jiffies.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/pci.h>
21*4882a593Smuzhiyun #include <linux/timer.h>
22*4882a593Smuzhiyun #include <linux/watchdog.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* Configuration registers relative to the pci device */
25*4882a593Smuzhiyun #define VIA_WDT_MMIO_BASE 0xe8 /* MMIO region base address */
26*4882a593Smuzhiyun #define VIA_WDT_CONF 0xec /* watchdog enable state */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Relevant bits for the VIA_WDT_CONF register */
29*4882a593Smuzhiyun #define VIA_WDT_CONF_ENABLE 0x01 /* 1: enable watchdog */
30*4882a593Smuzhiyun #define VIA_WDT_CONF_MMIO 0x02 /* 1: enable watchdog MMIO */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * The MMIO region contains the watchdog control register and the
34*4882a593Smuzhiyun * hardware timer counter.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun #define VIA_WDT_MMIO_LEN 8 /* MMIO region length in bytes */
37*4882a593Smuzhiyun #define VIA_WDT_CTL 0 /* MMIO addr+0: state/control reg. */
38*4882a593Smuzhiyun #define VIA_WDT_COUNT 4 /* MMIO addr+4: timer counter reg. */
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Bits for the VIA_WDT_CTL register */
41*4882a593Smuzhiyun #define VIA_WDT_RUNNING 0x01 /* 0: stop, 1: running */
42*4882a593Smuzhiyun #define VIA_WDT_FIRED 0x02 /* 1: restarted by expired watchdog */
43*4882a593Smuzhiyun #define VIA_WDT_PWROFF 0x04 /* 0: reset, 1: poweroff */
44*4882a593Smuzhiyun #define VIA_WDT_DISABLED 0x08 /* 1: timer is disabled */
45*4882a593Smuzhiyun #define VIA_WDT_TRIGGER 0x80 /* 1: start a new countdown */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* Hardware heartbeat in seconds */
48*4882a593Smuzhiyun #define WDT_HW_HEARTBEAT 1
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Timer heartbeat (500ms) */
51*4882a593Smuzhiyun #define WDT_HEARTBEAT (HZ/2) /* should be <= ((WDT_HW_HEARTBEAT*HZ)/2) */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* User space timeout in seconds */
54*4882a593Smuzhiyun #define WDT_TIMEOUT_MAX 1023 /* approx. 17 min. */
55*4882a593Smuzhiyun #define WDT_TIMEOUT 60
56*4882a593Smuzhiyun static int timeout = WDT_TIMEOUT;
57*4882a593Smuzhiyun module_param(timeout, int, 0);
58*4882a593Smuzhiyun MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, between 1 and 1023 "
59*4882a593Smuzhiyun "(default = " __MODULE_STRING(WDT_TIMEOUT) ")");
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
62*4882a593Smuzhiyun module_param(nowayout, bool, 0);
63*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
64*4882a593Smuzhiyun "(default = " __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static struct watchdog_device wdt_dev;
67*4882a593Smuzhiyun static struct resource wdt_res;
68*4882a593Smuzhiyun static void __iomem *wdt_mem;
69*4882a593Smuzhiyun static unsigned int mmio;
70*4882a593Smuzhiyun static void wdt_timer_tick(struct timer_list *unused);
71*4882a593Smuzhiyun static DEFINE_TIMER(timer, wdt_timer_tick);
72*4882a593Smuzhiyun /* The timer that pings the watchdog */
73*4882a593Smuzhiyun static unsigned long next_heartbeat; /* the next_heartbeat for the timer */
74*4882a593Smuzhiyun
wdt_reset(void)75*4882a593Smuzhiyun static inline void wdt_reset(void)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun unsigned int ctl = readl(wdt_mem);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun writel(ctl | VIA_WDT_TRIGGER, wdt_mem);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun * Timer tick: the timer will make sure that the watchdog timer hardware
84*4882a593Smuzhiyun * is being reset in time. The conditions to do this are:
85*4882a593Smuzhiyun * 1) the watchdog timer has been started and /dev/watchdog is open
86*4882a593Smuzhiyun * and there is still time left before userspace should send the
87*4882a593Smuzhiyun * next heartbeat/ping. (note: the internal heartbeat is much smaller
88*4882a593Smuzhiyun * then the external/userspace heartbeat).
89*4882a593Smuzhiyun * 2) the watchdog timer has been stopped by userspace.
90*4882a593Smuzhiyun */
wdt_timer_tick(struct timer_list * unused)91*4882a593Smuzhiyun static void wdt_timer_tick(struct timer_list *unused)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun if (time_before(jiffies, next_heartbeat) ||
94*4882a593Smuzhiyun (!watchdog_active(&wdt_dev))) {
95*4882a593Smuzhiyun wdt_reset();
96*4882a593Smuzhiyun mod_timer(&timer, jiffies + WDT_HEARTBEAT);
97*4882a593Smuzhiyun } else
98*4882a593Smuzhiyun pr_crit("I will reboot your machine !\n");
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
wdt_ping(struct watchdog_device * wdd)101*4882a593Smuzhiyun static int wdt_ping(struct watchdog_device *wdd)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun /* calculate when the next userspace timeout will be */
104*4882a593Smuzhiyun next_heartbeat = jiffies + wdd->timeout * HZ;
105*4882a593Smuzhiyun return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
wdt_start(struct watchdog_device * wdd)108*4882a593Smuzhiyun static int wdt_start(struct watchdog_device *wdd)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun unsigned int ctl = readl(wdt_mem);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun writel(wdd->timeout, wdt_mem + VIA_WDT_COUNT);
113*4882a593Smuzhiyun writel(ctl | VIA_WDT_RUNNING | VIA_WDT_TRIGGER, wdt_mem);
114*4882a593Smuzhiyun wdt_ping(wdd);
115*4882a593Smuzhiyun mod_timer(&timer, jiffies + WDT_HEARTBEAT);
116*4882a593Smuzhiyun return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
wdt_stop(struct watchdog_device * wdd)119*4882a593Smuzhiyun static int wdt_stop(struct watchdog_device *wdd)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun unsigned int ctl = readl(wdt_mem);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun writel(ctl & ~VIA_WDT_RUNNING, wdt_mem);
124*4882a593Smuzhiyun return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
wdt_set_timeout(struct watchdog_device * wdd,unsigned int new_timeout)127*4882a593Smuzhiyun static int wdt_set_timeout(struct watchdog_device *wdd,
128*4882a593Smuzhiyun unsigned int new_timeout)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun writel(new_timeout, wdt_mem + VIA_WDT_COUNT);
131*4882a593Smuzhiyun wdd->timeout = new_timeout;
132*4882a593Smuzhiyun return 0;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun static const struct watchdog_info wdt_info = {
136*4882a593Smuzhiyun .identity = "VIA watchdog",
137*4882a593Smuzhiyun .options = WDIOF_CARDRESET |
138*4882a593Smuzhiyun WDIOF_SETTIMEOUT |
139*4882a593Smuzhiyun WDIOF_MAGICCLOSE |
140*4882a593Smuzhiyun WDIOF_KEEPALIVEPING,
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static const struct watchdog_ops wdt_ops = {
144*4882a593Smuzhiyun .owner = THIS_MODULE,
145*4882a593Smuzhiyun .start = wdt_start,
146*4882a593Smuzhiyun .stop = wdt_stop,
147*4882a593Smuzhiyun .ping = wdt_ping,
148*4882a593Smuzhiyun .set_timeout = wdt_set_timeout,
149*4882a593Smuzhiyun };
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun static struct watchdog_device wdt_dev = {
152*4882a593Smuzhiyun .info = &wdt_info,
153*4882a593Smuzhiyun .ops = &wdt_ops,
154*4882a593Smuzhiyun .min_timeout = 1,
155*4882a593Smuzhiyun .max_timeout = WDT_TIMEOUT_MAX,
156*4882a593Smuzhiyun };
157*4882a593Smuzhiyun
wdt_probe(struct pci_dev * pdev,const struct pci_device_id * ent)158*4882a593Smuzhiyun static int wdt_probe(struct pci_dev *pdev,
159*4882a593Smuzhiyun const struct pci_device_id *ent)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun unsigned char conf;
162*4882a593Smuzhiyun int ret = -ENODEV;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (pci_enable_device(pdev)) {
165*4882a593Smuzhiyun dev_err(&pdev->dev, "cannot enable PCI device\n");
166*4882a593Smuzhiyun return -ENODEV;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * Allocate a MMIO region which contains watchdog control register
171*4882a593Smuzhiyun * and counter, then configure the watchdog to use this region.
172*4882a593Smuzhiyun * This is possible only if PnP is properly enabled in BIOS.
173*4882a593Smuzhiyun * If not, the watchdog must be configured in BIOS manually.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun if (allocate_resource(&iomem_resource, &wdt_res, VIA_WDT_MMIO_LEN,
176*4882a593Smuzhiyun 0xf0000000, 0xffffff00, 0xff, NULL, NULL)) {
177*4882a593Smuzhiyun dev_err(&pdev->dev, "MMIO allocation failed\n");
178*4882a593Smuzhiyun goto err_out_disable_device;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun pci_write_config_dword(pdev, VIA_WDT_MMIO_BASE, wdt_res.start);
182*4882a593Smuzhiyun pci_read_config_byte(pdev, VIA_WDT_CONF, &conf);
183*4882a593Smuzhiyun conf |= VIA_WDT_CONF_ENABLE | VIA_WDT_CONF_MMIO;
184*4882a593Smuzhiyun pci_write_config_byte(pdev, VIA_WDT_CONF, conf);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun pci_read_config_dword(pdev, VIA_WDT_MMIO_BASE, &mmio);
187*4882a593Smuzhiyun if (mmio) {
188*4882a593Smuzhiyun dev_info(&pdev->dev, "VIA Chipset watchdog MMIO: %x\n", mmio);
189*4882a593Smuzhiyun } else {
190*4882a593Smuzhiyun dev_err(&pdev->dev, "MMIO setting failed. Check BIOS.\n");
191*4882a593Smuzhiyun goto err_out_resource;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (!request_mem_region(mmio, VIA_WDT_MMIO_LEN, "via_wdt")) {
195*4882a593Smuzhiyun dev_err(&pdev->dev, "MMIO region busy\n");
196*4882a593Smuzhiyun goto err_out_resource;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun wdt_mem = ioremap(mmio, VIA_WDT_MMIO_LEN);
200*4882a593Smuzhiyun if (wdt_mem == NULL) {
201*4882a593Smuzhiyun dev_err(&pdev->dev, "cannot remap VIA wdt MMIO registers\n");
202*4882a593Smuzhiyun goto err_out_release;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (timeout < 1 || timeout > WDT_TIMEOUT_MAX)
206*4882a593Smuzhiyun timeout = WDT_TIMEOUT;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun wdt_dev.timeout = timeout;
209*4882a593Smuzhiyun wdt_dev.parent = &pdev->dev;
210*4882a593Smuzhiyun watchdog_set_nowayout(&wdt_dev, nowayout);
211*4882a593Smuzhiyun if (readl(wdt_mem) & VIA_WDT_FIRED)
212*4882a593Smuzhiyun wdt_dev.bootstatus |= WDIOF_CARDRESET;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun ret = watchdog_register_device(&wdt_dev);
215*4882a593Smuzhiyun if (ret)
216*4882a593Smuzhiyun goto err_out_iounmap;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* start triggering, in case of watchdog already enabled by BIOS */
219*4882a593Smuzhiyun mod_timer(&timer, jiffies + WDT_HEARTBEAT);
220*4882a593Smuzhiyun return 0;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun err_out_iounmap:
223*4882a593Smuzhiyun iounmap(wdt_mem);
224*4882a593Smuzhiyun err_out_release:
225*4882a593Smuzhiyun release_mem_region(mmio, VIA_WDT_MMIO_LEN);
226*4882a593Smuzhiyun err_out_resource:
227*4882a593Smuzhiyun release_resource(&wdt_res);
228*4882a593Smuzhiyun err_out_disable_device:
229*4882a593Smuzhiyun pci_disable_device(pdev);
230*4882a593Smuzhiyun return ret;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
wdt_remove(struct pci_dev * pdev)233*4882a593Smuzhiyun static void wdt_remove(struct pci_dev *pdev)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun watchdog_unregister_device(&wdt_dev);
236*4882a593Smuzhiyun del_timer_sync(&timer);
237*4882a593Smuzhiyun iounmap(wdt_mem);
238*4882a593Smuzhiyun release_mem_region(mmio, VIA_WDT_MMIO_LEN);
239*4882a593Smuzhiyun release_resource(&wdt_res);
240*4882a593Smuzhiyun pci_disable_device(pdev);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun static const struct pci_device_id wdt_pci_table[] = {
244*4882a593Smuzhiyun { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_CX700) },
245*4882a593Smuzhiyun { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX800) },
246*4882a593Smuzhiyun { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855) },
247*4882a593Smuzhiyun { 0 }
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun static struct pci_driver wdt_driver = {
251*4882a593Smuzhiyun .name = "via_wdt",
252*4882a593Smuzhiyun .id_table = wdt_pci_table,
253*4882a593Smuzhiyun .probe = wdt_probe,
254*4882a593Smuzhiyun .remove = wdt_remove,
255*4882a593Smuzhiyun };
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun module_pci_driver(wdt_driver);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun MODULE_AUTHOR("Marc Vertes");
260*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for watchdog timer on VIA chipset");
261*4882a593Smuzhiyun MODULE_LICENSE("GPL");
262