2 * Watchdog driver for Faraday Technology FTWDT010
4 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
6 * Inspired by the out-of-tree drivers from OpenWRT:
7 * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/bitops.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/watchdog.h>
25 #define FTWDT010_WDCOUNTER 0x0
26 #define FTWDT010_WDLOAD 0x4
27 #define FTWDT010_WDRESTART 0x8
28 #define FTWDT010_WDCR 0xC
30 #define WDRESTART_MAGIC 0x5AB9
32 #define WDCR_CLOCK_5MHZ BIT(4)
33 #define WDCR_WDEXT BIT(3)
34 #define WDCR_WDINTR BIT(2)
35 #define WDCR_SYS_RST BIT(1)
36 #define WDCR_ENABLE BIT(0)
38 #define WDT_CLOCK 5000000 /* 5 MHz */
41 struct watchdog_device wdd
;
48 struct ftwdt010_wdt
*to_ftwdt010_wdt(struct watchdog_device
*wdd
)
50 return container_of(wdd
, struct ftwdt010_wdt
, wdd
);
53 static int ftwdt010_wdt_start(struct watchdog_device
*wdd
)
55 struct ftwdt010_wdt
*gwdt
= to_ftwdt010_wdt(wdd
);
58 writel(wdd
->timeout
* WDT_CLOCK
, gwdt
->base
+ FTWDT010_WDLOAD
);
59 writel(WDRESTART_MAGIC
, gwdt
->base
+ FTWDT010_WDRESTART
);
60 /* set clock before enabling */
61 enable
= WDCR_CLOCK_5MHZ
| WDCR_SYS_RST
;
62 writel(enable
, gwdt
->base
+ FTWDT010_WDCR
);
64 enable
|= WDCR_WDINTR
;
65 enable
|= WDCR_ENABLE
;
66 writel(enable
, gwdt
->base
+ FTWDT010_WDCR
);
71 static int ftwdt010_wdt_stop(struct watchdog_device
*wdd
)
73 struct ftwdt010_wdt
*gwdt
= to_ftwdt010_wdt(wdd
);
75 writel(0, gwdt
->base
+ FTWDT010_WDCR
);
80 static int ftwdt010_wdt_ping(struct watchdog_device
*wdd
)
82 struct ftwdt010_wdt
*gwdt
= to_ftwdt010_wdt(wdd
);
84 writel(WDRESTART_MAGIC
, gwdt
->base
+ FTWDT010_WDRESTART
);
89 static int ftwdt010_wdt_set_timeout(struct watchdog_device
*wdd
,
92 wdd
->timeout
= timeout
;
93 if (watchdog_active(wdd
))
94 ftwdt010_wdt_start(wdd
);
99 static irqreturn_t
ftwdt010_wdt_interrupt(int irq
, void *data
)
101 struct ftwdt010_wdt
*gwdt
= data
;
103 watchdog_notify_pretimeout(&gwdt
->wdd
);
108 static const struct watchdog_ops ftwdt010_wdt_ops
= {
109 .start
= ftwdt010_wdt_start
,
110 .stop
= ftwdt010_wdt_stop
,
111 .ping
= ftwdt010_wdt_ping
,
112 .set_timeout
= ftwdt010_wdt_set_timeout
,
113 .owner
= THIS_MODULE
,
116 static const struct watchdog_info ftwdt010_wdt_info
= {
117 .options
= WDIOF_KEEPALIVEPING
120 .identity
= KBUILD_MODNAME
,
124 static int ftwdt010_wdt_probe(struct platform_device
*pdev
)
126 struct device
*dev
= &pdev
->dev
;
127 struct resource
*res
;
128 struct ftwdt010_wdt
*gwdt
;
133 gwdt
= devm_kzalloc(dev
, sizeof(*gwdt
), GFP_KERNEL
);
137 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
138 gwdt
->base
= devm_ioremap_resource(dev
, res
);
139 if (IS_ERR(gwdt
->base
))
140 return PTR_ERR(gwdt
->base
);
143 gwdt
->wdd
.info
= &ftwdt010_wdt_info
;
144 gwdt
->wdd
.ops
= &ftwdt010_wdt_ops
;
145 gwdt
->wdd
.min_timeout
= 1;
146 gwdt
->wdd
.max_timeout
= 0xFFFFFFFF / WDT_CLOCK
;
147 gwdt
->wdd
.parent
= dev
;
150 * If 'timeout-sec' unspecified in devicetree, assume a 13 second
153 gwdt
->wdd
.timeout
= 13U;
154 watchdog_init_timeout(&gwdt
->wdd
, 0, dev
);
156 reg
= readw(gwdt
->base
+ FTWDT010_WDCR
);
157 if (reg
& WDCR_ENABLE
) {
158 /* Watchdog was enabled by the bootloader, disable it. */
160 writel(reg
, gwdt
->base
+ FTWDT010_WDCR
);
163 irq
= platform_get_irq(pdev
, 0);
165 ret
= devm_request_irq(dev
, irq
, ftwdt010_wdt_interrupt
, 0,
166 "watchdog bark", gwdt
);
169 gwdt
->has_irq
= true;
172 ret
= devm_watchdog_register_device(dev
, &gwdt
->wdd
);
174 dev_err(&pdev
->dev
, "failed to register watchdog\n");
178 /* Set up platform driver data */
179 platform_set_drvdata(pdev
, gwdt
);
180 dev_info(dev
, "FTWDT010 watchdog driver enabled\n");
185 static int __maybe_unused
ftwdt010_wdt_suspend(struct device
*dev
)
187 struct ftwdt010_wdt
*gwdt
= dev_get_drvdata(dev
);
190 reg
= readw(gwdt
->base
+ FTWDT010_WDCR
);
192 writel(reg
, gwdt
->base
+ FTWDT010_WDCR
);
197 static int __maybe_unused
ftwdt010_wdt_resume(struct device
*dev
)
199 struct ftwdt010_wdt
*gwdt
= dev_get_drvdata(dev
);
202 if (watchdog_active(&gwdt
->wdd
)) {
203 reg
= readw(gwdt
->base
+ FTWDT010_WDCR
);
205 writel(reg
, gwdt
->base
+ FTWDT010_WDCR
);
211 static const struct dev_pm_ops ftwdt010_wdt_dev_pm_ops
= {
212 SET_SYSTEM_SLEEP_PM_OPS(ftwdt010_wdt_suspend
,
217 static const struct of_device_id ftwdt010_wdt_match
[] = {
218 { .compatible
= "faraday,ftwdt010" },
219 { .compatible
= "cortina,gemini-watchdog" },
222 MODULE_DEVICE_TABLE(of
, ftwdt010_wdt_match
);
225 static struct platform_driver ftwdt010_wdt_driver
= {
226 .probe
= ftwdt010_wdt_probe
,
228 .name
= "ftwdt010-wdt",
229 .of_match_table
= of_match_ptr(ftwdt010_wdt_match
),
230 .pm
= &ftwdt010_wdt_dev_pm_ops
,
233 module_platform_driver(ftwdt010_wdt_driver
);
234 MODULE_AUTHOR("Linus Walleij");
235 MODULE_DESCRIPTION("Watchdog driver for Faraday Technology FTWDT010");
236 MODULE_LICENSE("GPL");