2 * Watchdog driver for Cortina Systems Gemini SoC
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 GEMINI_WDCOUNTER 0x0
26 #define GEMINI_WDLOAD 0x4
27 #define GEMINI_WDRESTART 0x8
28 #define GEMINI_WDCR 0xC
30 #define WDRESTART_MAGIC 0x5AB9
32 #define WDCR_CLOCK_5MHZ BIT(4)
33 #define WDCR_SYS_RST BIT(1)
34 #define WDCR_ENABLE BIT(0)
36 #define WDT_CLOCK 5000000 /* 5 MHz */
39 struct watchdog_device wdd
;
45 struct gemini_wdt
*to_gemini_wdt(struct watchdog_device
*wdd
)
47 return container_of(wdd
, struct gemini_wdt
, wdd
);
50 static int gemini_wdt_start(struct watchdog_device
*wdd
)
52 struct gemini_wdt
*gwdt
= to_gemini_wdt(wdd
);
54 writel(wdd
->timeout
* WDT_CLOCK
, gwdt
->base
+ GEMINI_WDLOAD
);
55 writel(WDRESTART_MAGIC
, gwdt
->base
+ GEMINI_WDRESTART
);
56 /* set clock before enabling */
57 writel(WDCR_CLOCK_5MHZ
| WDCR_SYS_RST
,
58 gwdt
->base
+ GEMINI_WDCR
);
59 writel(WDCR_CLOCK_5MHZ
| WDCR_SYS_RST
| WDCR_ENABLE
,
60 gwdt
->base
+ GEMINI_WDCR
);
65 static int gemini_wdt_stop(struct watchdog_device
*wdd
)
67 struct gemini_wdt
*gwdt
= to_gemini_wdt(wdd
);
69 writel(0, gwdt
->base
+ GEMINI_WDCR
);
74 static int gemini_wdt_ping(struct watchdog_device
*wdd
)
76 struct gemini_wdt
*gwdt
= to_gemini_wdt(wdd
);
78 writel(WDRESTART_MAGIC
, gwdt
->base
+ GEMINI_WDRESTART
);
83 static int gemini_wdt_set_timeout(struct watchdog_device
*wdd
,
86 wdd
->timeout
= timeout
;
87 if (watchdog_active(wdd
))
88 gemini_wdt_start(wdd
);
93 static irqreturn_t
gemini_wdt_interrupt(int irq
, void *data
)
95 struct gemini_wdt
*gwdt
= data
;
97 watchdog_notify_pretimeout(&gwdt
->wdd
);
102 static const struct watchdog_ops gemini_wdt_ops
= {
103 .start
= gemini_wdt_start
,
104 .stop
= gemini_wdt_stop
,
105 .ping
= gemini_wdt_ping
,
106 .set_timeout
= gemini_wdt_set_timeout
,
107 .owner
= THIS_MODULE
,
110 static const struct watchdog_info gemini_wdt_info
= {
111 .options
= WDIOF_KEEPALIVEPING
114 .identity
= KBUILD_MODNAME
,
118 static int gemini_wdt_probe(struct platform_device
*pdev
)
120 struct device
*dev
= &pdev
->dev
;
121 struct resource
*res
;
122 struct gemini_wdt
*gwdt
;
127 gwdt
= devm_kzalloc(dev
, sizeof(*gwdt
), GFP_KERNEL
);
131 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
132 gwdt
->base
= devm_ioremap_resource(dev
, res
);
133 if (IS_ERR(gwdt
->base
))
134 return PTR_ERR(gwdt
->base
);
136 irq
= platform_get_irq(pdev
, 0);
141 gwdt
->wdd
.info
= &gemini_wdt_info
;
142 gwdt
->wdd
.ops
= &gemini_wdt_ops
;
143 gwdt
->wdd
.min_timeout
= 1;
144 gwdt
->wdd
.max_timeout
= 0xFFFFFFFF / WDT_CLOCK
;
145 gwdt
->wdd
.parent
= dev
;
148 * If 'timeout-sec' unspecified in devicetree, assume a 13 second
151 gwdt
->wdd
.timeout
= 13U;
152 watchdog_init_timeout(&gwdt
->wdd
, 0, dev
);
154 reg
= readw(gwdt
->base
+ GEMINI_WDCR
);
155 if (reg
& WDCR_ENABLE
) {
156 /* Watchdog was enabled by the bootloader, disable it. */
158 writel(reg
, gwdt
->base
+ GEMINI_WDCR
);
161 ret
= devm_request_irq(dev
, irq
, gemini_wdt_interrupt
, 0,
162 "watchdog bark", gwdt
);
166 ret
= devm_watchdog_register_device(dev
, &gwdt
->wdd
);
168 dev_err(&pdev
->dev
, "failed to register watchdog\n");
172 /* Set up platform driver data */
173 platform_set_drvdata(pdev
, gwdt
);
174 dev_info(dev
, "Gemini watchdog driver enabled\n");
179 static int __maybe_unused
gemini_wdt_suspend(struct device
*dev
)
181 struct gemini_wdt
*gwdt
= dev_get_drvdata(dev
);
184 reg
= readw(gwdt
->base
+ GEMINI_WDCR
);
186 writel(reg
, gwdt
->base
+ GEMINI_WDCR
);
191 static int __maybe_unused
gemini_wdt_resume(struct device
*dev
)
193 struct gemini_wdt
*gwdt
= dev_get_drvdata(dev
);
196 if (watchdog_active(&gwdt
->wdd
)) {
197 reg
= readw(gwdt
->base
+ GEMINI_WDCR
);
199 writel(reg
, gwdt
->base
+ GEMINI_WDCR
);
205 static const struct dev_pm_ops gemini_wdt_dev_pm_ops
= {
206 SET_SYSTEM_SLEEP_PM_OPS(gemini_wdt_suspend
,
211 static const struct of_device_id gemini_wdt_match
[] = {
212 { .compatible
= "cortina,gemini-watchdog" },
215 MODULE_DEVICE_TABLE(of
, gemini_wdt_match
);
218 static struct platform_driver gemini_wdt_driver
= {
219 .probe
= gemini_wdt_probe
,
221 .name
= "gemini-wdt",
222 .of_match_table
= of_match_ptr(gemini_wdt_match
),
223 .pm
= &gemini_wdt_dev_pm_ops
,
226 module_platform_driver(gemini_wdt_driver
);
227 MODULE_AUTHOR("Linus Walleij");
228 MODULE_DESCRIPTION("Watchdog driver for Gemini");
229 MODULE_LICENSE("GPL");