1 // SPDX-License-Identifier: GPL-2.0-only
3 * Watchdog driver for Faraday Technology FTWDT010
5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
7 * Inspired by the out-of-tree drivers from OpenWRT:
8 * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
11 #include <linux/bitops.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/watchdog.h>
22 #define FTWDT010_WDCOUNTER 0x0
23 #define FTWDT010_WDLOAD 0x4
24 #define FTWDT010_WDRESTART 0x8
25 #define FTWDT010_WDCR 0xC
27 #define WDRESTART_MAGIC 0x5AB9
29 #define WDCR_CLOCK_5MHZ BIT(4)
30 #define WDCR_WDEXT BIT(3)
31 #define WDCR_WDINTR BIT(2)
32 #define WDCR_SYS_RST BIT(1)
33 #define WDCR_ENABLE BIT(0)
35 #define WDT_CLOCK 5000000 /* 5 MHz */
38 struct watchdog_device wdd;
45 struct ftwdt010_wdt *to_ftwdt010_wdt(struct watchdog_device *wdd)
47 return container_of(wdd, struct ftwdt010_wdt, wdd);
50 static void ftwdt010_enable(struct ftwdt010_wdt *gwdt,
56 writel(timeout * WDT_CLOCK, gwdt->base + FTWDT010_WDLOAD);
57 writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
58 /* set clock before enabling */
59 enable = WDCR_CLOCK_5MHZ | WDCR_SYS_RST;
60 writel(enable, gwdt->base + FTWDT010_WDCR);
62 enable |= WDCR_WDINTR;
63 enable |= WDCR_ENABLE;
64 writel(enable, gwdt->base + FTWDT010_WDCR);
67 static int ftwdt010_wdt_start(struct watchdog_device *wdd)
69 struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
71 ftwdt010_enable(gwdt, wdd->timeout, gwdt->has_irq);
75 static int ftwdt010_wdt_stop(struct watchdog_device *wdd)
77 struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
79 writel(0, gwdt->base + FTWDT010_WDCR);
84 static int ftwdt010_wdt_ping(struct watchdog_device *wdd)
86 struct ftwdt010_wdt *gwdt = to_ftwdt010_wdt(wdd);
88 writel(WDRESTART_MAGIC, gwdt->base + FTWDT010_WDRESTART);
93 static int ftwdt010_wdt_set_timeout(struct watchdog_device *wdd,
96 wdd->timeout = timeout;
97 if (watchdog_active(wdd))
98 ftwdt010_wdt_start(wdd);
103 static int ftwdt010_wdt_restart(struct watchdog_device *wdd,
104 unsigned long action, void *data)
106 ftwdt010_enable(to_ftwdt010_wdt(wdd), 0, false);
110 static irqreturn_t ftwdt010_wdt_interrupt(int irq, void *data)
112 struct ftwdt010_wdt *gwdt = data;
114 watchdog_notify_pretimeout(&gwdt->wdd);
119 static const struct watchdog_ops ftwdt010_wdt_ops = {
120 .start = ftwdt010_wdt_start,
121 .stop = ftwdt010_wdt_stop,
122 .ping = ftwdt010_wdt_ping,
123 .set_timeout = ftwdt010_wdt_set_timeout,
124 .restart = ftwdt010_wdt_restart,
125 .owner = THIS_MODULE,
128 static const struct watchdog_info ftwdt010_wdt_info = {
129 .options = WDIOF_KEEPALIVEPING
132 .identity = KBUILD_MODNAME,
136 static int ftwdt010_wdt_probe(struct platform_device *pdev)
138 struct device *dev = &pdev->dev;
139 struct ftwdt010_wdt *gwdt;
144 gwdt = devm_kzalloc(dev, sizeof(*gwdt), GFP_KERNEL);
148 gwdt->base = devm_platform_ioremap_resource(pdev, 0);
149 if (IS_ERR(gwdt->base))
150 return PTR_ERR(gwdt->base);
153 gwdt->wdd.info = &ftwdt010_wdt_info;
154 gwdt->wdd.ops = &ftwdt010_wdt_ops;
155 gwdt->wdd.min_timeout = 1;
156 gwdt->wdd.max_timeout = 0xFFFFFFFF / WDT_CLOCK;
157 gwdt->wdd.parent = dev;
160 * If 'timeout-sec' unspecified in devicetree, assume a 13 second
163 gwdt->wdd.timeout = 13U;
164 watchdog_init_timeout(&gwdt->wdd, 0, dev);
166 reg = readw(gwdt->base + FTWDT010_WDCR);
167 if (reg & WDCR_ENABLE) {
168 /* Watchdog was enabled by the bootloader, disable it. */
170 writel(reg, gwdt->base + FTWDT010_WDCR);
173 irq = platform_get_irq(pdev, 0);
175 ret = devm_request_irq(dev, irq, ftwdt010_wdt_interrupt, 0,
176 "watchdog bark", gwdt);
179 gwdt->has_irq = true;
182 ret = devm_watchdog_register_device(dev, &gwdt->wdd);
186 /* Set up platform driver data */
187 platform_set_drvdata(pdev, gwdt);
188 dev_info(dev, "FTWDT010 watchdog driver enabled\n");
193 static int __maybe_unused ftwdt010_wdt_suspend(struct device *dev)
195 struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
198 reg = readw(gwdt->base + FTWDT010_WDCR);
200 writel(reg, gwdt->base + FTWDT010_WDCR);
205 static int __maybe_unused ftwdt010_wdt_resume(struct device *dev)
207 struct ftwdt010_wdt *gwdt = dev_get_drvdata(dev);
210 if (watchdog_active(&gwdt->wdd)) {
211 reg = readw(gwdt->base + FTWDT010_WDCR);
213 writel(reg, gwdt->base + FTWDT010_WDCR);
219 static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops = {
220 SET_SYSTEM_SLEEP_PM_OPS(ftwdt010_wdt_suspend,
224 static const struct of_device_id ftwdt010_wdt_match[] = {
225 { .compatible = "faraday,ftwdt010" },
226 { .compatible = "cortina,gemini-watchdog" },
229 MODULE_DEVICE_TABLE(of, ftwdt010_wdt_match);
231 static struct platform_driver ftwdt010_wdt_driver = {
232 .probe = ftwdt010_wdt_probe,
234 .name = "ftwdt010-wdt",
235 .of_match_table = ftwdt010_wdt_match,
236 .pm = &ftwdt010_wdt_dev_pm_ops,
239 module_platform_driver(ftwdt010_wdt_driver);
240 MODULE_AUTHOR("Linus Walleij");
241 MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
242 MODULE_LICENSE("GPL");