blob: 1e9d1578f65fa4b01dca5f6af5f33424aa40bf35 [file] [log] [blame]
Anson Huang41b630f2019-08-28 09:35:01 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 NXP.
4 */
5
6#include <linux/clk.h>
7#include <linux/init.h>
8#include <linux/io.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/platform_device.h>
13#include <linux/reboot.h>
14#include <linux/watchdog.h>
15
16#define WDOG_CS 0x0
17#define WDOG_CS_CMD32EN BIT(13)
18#define WDOG_CS_ULK BIT(11)
19#define WDOG_CS_RCS BIT(10)
20#define WDOG_CS_EN BIT(7)
21#define WDOG_CS_UPDATE BIT(5)
22
23#define WDOG_CNT 0x4
24#define WDOG_TOVAL 0x8
25
26#define REFRESH_SEQ0 0xA602
27#define REFRESH_SEQ1 0xB480
28#define REFRESH ((REFRESH_SEQ1 << 16) | REFRESH_SEQ0)
29
30#define UNLOCK_SEQ0 0xC520
31#define UNLOCK_SEQ1 0xD928
32#define UNLOCK ((UNLOCK_SEQ1 << 16) | UNLOCK_SEQ0)
33
34#define DEFAULT_TIMEOUT 60
35#define MAX_TIMEOUT 128
36#define WDOG_CLOCK_RATE 1000
37
38static bool nowayout = WATCHDOG_NOWAYOUT;
39module_param(nowayout, bool, 0000);
40MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
41 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42
43struct imx7ulp_wdt_device {
Anson Huang41b630f2019-08-28 09:35:01 -040044 struct watchdog_device wdd;
45 void __iomem *base;
46 struct clk *clk;
47};
48
Fabio Estevamc37e3582019-10-29 14:40:36 -030049static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable)
Anson Huang41b630f2019-08-28 09:35:01 -040050{
Fabio Estevam747d88a2019-10-29 14:40:34 -030051 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
Anson Huang41b630f2019-08-28 09:35:01 -040052
Fabio Estevam747d88a2019-10-29 14:40:34 -030053 u32 val = readl(wdt->base + WDOG_CS);
54
55 writel(UNLOCK, wdt->base + WDOG_CNT);
Anson Huang41b630f2019-08-28 09:35:01 -040056 if (enable)
Fabio Estevam747d88a2019-10-29 14:40:34 -030057 writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
Anson Huang41b630f2019-08-28 09:35:01 -040058 else
Fabio Estevam747d88a2019-10-29 14:40:34 -030059 writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
Anson Huang41b630f2019-08-28 09:35:01 -040060}
61
Fabio Estevamc37e3582019-10-29 14:40:36 -030062static bool imx7ulp_wdt_is_enabled(void __iomem *base)
Anson Huang41b630f2019-08-28 09:35:01 -040063{
64 u32 val = readl(base + WDOG_CS);
65
66 return val & WDOG_CS_EN;
67}
68
69static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
70{
71 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
72
73 writel(REFRESH, wdt->base + WDOG_CNT);
74
75 return 0;
76}
77
78static int imx7ulp_wdt_start(struct watchdog_device *wdog)
79{
Anson Huang41b630f2019-08-28 09:35:01 -040080
Fabio Estevam747d88a2019-10-29 14:40:34 -030081 imx7ulp_wdt_enable(wdog, true);
Anson Huang41b630f2019-08-28 09:35:01 -040082
83 return 0;
84}
85
86static int imx7ulp_wdt_stop(struct watchdog_device *wdog)
87{
Fabio Estevam747d88a2019-10-29 14:40:34 -030088 imx7ulp_wdt_enable(wdog, false);
Anson Huang41b630f2019-08-28 09:35:01 -040089
90 return 0;
91}
92
93static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog,
94 unsigned int timeout)
95{
96 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
97 u32 val = WDOG_CLOCK_RATE * timeout;
98
99 writel(UNLOCK, wdt->base + WDOG_CNT);
100 writel(val, wdt->base + WDOG_TOVAL);
101
102 wdog->timeout = timeout;
103
104 return 0;
105}
106
107static const struct watchdog_ops imx7ulp_wdt_ops = {
108 .owner = THIS_MODULE,
109 .start = imx7ulp_wdt_start,
110 .stop = imx7ulp_wdt_stop,
111 .ping = imx7ulp_wdt_ping,
112 .set_timeout = imx7ulp_wdt_set_timeout,
113};
114
115static const struct watchdog_info imx7ulp_wdt_info = {
116 .identity = "i.MX7ULP watchdog timer",
117 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
118 WDIOF_MAGICCLOSE,
119};
120
Fabio Estevamc37e3582019-10-29 14:40:36 -0300121static void imx7ulp_wdt_init(void __iomem *base, unsigned int timeout)
Anson Huang41b630f2019-08-28 09:35:01 -0400122{
123 u32 val;
124
125 /* unlock the wdog for reconfiguration */
126 writel_relaxed(UNLOCK_SEQ0, base + WDOG_CNT);
127 writel_relaxed(UNLOCK_SEQ1, base + WDOG_CNT);
128
129 /* set an initial timeout value in TOVAL */
130 writel(timeout, base + WDOG_TOVAL);
131 /* enable 32bit command sequence and reconfigure */
132 val = BIT(13) | BIT(8) | BIT(5);
133 writel(val, base + WDOG_CS);
134}
135
136static void imx7ulp_wdt_action(void *data)
137{
138 clk_disable_unprepare(data);
139}
140
141static int imx7ulp_wdt_probe(struct platform_device *pdev)
142{
143 struct imx7ulp_wdt_device *imx7ulp_wdt;
144 struct device *dev = &pdev->dev;
145 struct watchdog_device *wdog;
146 int ret;
147
148 imx7ulp_wdt = devm_kzalloc(dev, sizeof(*imx7ulp_wdt), GFP_KERNEL);
149 if (!imx7ulp_wdt)
150 return -ENOMEM;
151
152 platform_set_drvdata(pdev, imx7ulp_wdt);
153
154 imx7ulp_wdt->base = devm_platform_ioremap_resource(pdev, 0);
155 if (IS_ERR(imx7ulp_wdt->base))
156 return PTR_ERR(imx7ulp_wdt->base);
157
158 imx7ulp_wdt->clk = devm_clk_get(dev, NULL);
159 if (IS_ERR(imx7ulp_wdt->clk)) {
160 dev_err(dev, "Failed to get watchdog clock\n");
161 return PTR_ERR(imx7ulp_wdt->clk);
162 }
163
164 ret = clk_prepare_enable(imx7ulp_wdt->clk);
165 if (ret)
166 return ret;
167
168 ret = devm_add_action_or_reset(dev, imx7ulp_wdt_action, imx7ulp_wdt->clk);
169 if (ret)
170 return ret;
171
172 wdog = &imx7ulp_wdt->wdd;
173 wdog->info = &imx7ulp_wdt_info;
174 wdog->ops = &imx7ulp_wdt_ops;
175 wdog->min_timeout = 1;
176 wdog->max_timeout = MAX_TIMEOUT;
177 wdog->parent = dev;
178 wdog->timeout = DEFAULT_TIMEOUT;
179
180 watchdog_init_timeout(wdog, 0, dev);
181 watchdog_stop_on_reboot(wdog);
182 watchdog_stop_on_unregister(wdog);
183 watchdog_set_drvdata(wdog, imx7ulp_wdt);
184 imx7ulp_wdt_init(imx7ulp_wdt->base, wdog->timeout * WDOG_CLOCK_RATE);
185
186 return devm_watchdog_register_device(dev, wdog);
187}
188
189static int __maybe_unused imx7ulp_wdt_suspend(struct device *dev)
190{
191 struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
192
193 if (watchdog_active(&imx7ulp_wdt->wdd))
194 imx7ulp_wdt_stop(&imx7ulp_wdt->wdd);
195
196 clk_disable_unprepare(imx7ulp_wdt->clk);
197
198 return 0;
199}
200
201static int __maybe_unused imx7ulp_wdt_resume(struct device *dev)
202{
203 struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
204 u32 timeout = imx7ulp_wdt->wdd.timeout * WDOG_CLOCK_RATE;
205 int ret;
206
207 ret = clk_prepare_enable(imx7ulp_wdt->clk);
208 if (ret)
209 return ret;
210
211 if (imx7ulp_wdt_is_enabled(imx7ulp_wdt->base))
212 imx7ulp_wdt_init(imx7ulp_wdt->base, timeout);
213
214 if (watchdog_active(&imx7ulp_wdt->wdd))
215 imx7ulp_wdt_start(&imx7ulp_wdt->wdd);
216
217 return 0;
218}
219
220static SIMPLE_DEV_PM_OPS(imx7ulp_wdt_pm_ops, imx7ulp_wdt_suspend,
221 imx7ulp_wdt_resume);
222
223static const struct of_device_id imx7ulp_wdt_dt_ids[] = {
224 { .compatible = "fsl,imx7ulp-wdt", },
225 { /* sentinel */ }
226};
227MODULE_DEVICE_TABLE(of, imx7ulp_wdt_dt_ids);
228
229static struct platform_driver imx7ulp_wdt_driver = {
230 .probe = imx7ulp_wdt_probe,
231 .driver = {
232 .name = "imx7ulp-wdt",
233 .pm = &imx7ulp_wdt_pm_ops,
234 .of_match_table = imx7ulp_wdt_dt_ids,
235 },
236};
237module_platform_driver(imx7ulp_wdt_driver);
238
239MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
240MODULE_DESCRIPTION("Freescale i.MX7ULP watchdog driver");
241MODULE_LICENSE("GPL v2");