blob: d8c50b6291cd5ee6e3dfc86c028ab40e40e718fb [file] [log] [blame]
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Watchdog driver for Intel Keem Bay non-secure watchdog.
4 *
5 * Copyright (C) 2020 Intel Corporation
6 */
7
8#include <linux/arm-smccc.h>
9#include <linux/bits.h>
10#include <linux/clk.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/limits.h>
14#include <linux/module.h>
15#include <linux/mod_devicetable.h>
16#include <linux/platform_device.h>
17#include <linux/reboot.h>
18#include <linux/watchdog.h>
19
20/* Non-secure watchdog register offsets */
21#define TIM_WATCHDOG 0x0
22#define TIM_WATCHDOG_INT_THRES 0x4
23#define TIM_WDOG_EN 0x8
24#define TIM_SAFE 0xc
25
Shruthi Sanil0e36a092021-05-17 23:19:48 +053026#define WDT_TH_INT_MASK BIT(8)
27#define WDT_TO_INT_MASK BIT(9)
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +080028#define WDT_ISR_CLEAR 0x8200ff18
29#define WDT_UNLOCK 0xf1d0dead
Shruthi Sanil624873f2021-05-17 23:19:51 +053030#define WDT_DISABLE 0x0
31#define WDT_ENABLE 0x1
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +080032#define WDT_LOAD_MAX U32_MAX
33#define WDT_LOAD_MIN 1
34#define WDT_TIMEOUT 5
Shruthi Sanil29353812021-05-17 23:19:45 +053035#define WDT_PRETIMEOUT 4
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +080036
37static unsigned int timeout = WDT_TIMEOUT;
38module_param(timeout, int, 0);
39MODULE_PARM_DESC(timeout, "Watchdog timeout period in seconds (default = "
40 __MODULE_STRING(WDT_TIMEOUT) ")");
41
42static bool nowayout = WATCHDOG_NOWAYOUT;
43module_param(nowayout, bool, 0);
44MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = "
45 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
46
47struct keembay_wdt {
48 struct watchdog_device wdd;
49 struct clk *clk;
50 unsigned int rate;
51 int to_irq;
52 int th_irq;
53 void __iomem *base;
54};
55
56static inline u32 keembay_wdt_readl(struct keembay_wdt *wdt, u32 offset)
57{
58 return readl(wdt->base + offset);
59}
60
61static inline void keembay_wdt_writel(struct keembay_wdt *wdt, u32 offset, u32 val)
62{
63 writel(WDT_UNLOCK, wdt->base + TIM_SAFE);
64 writel(val, wdt->base + offset);
65}
66
67static void keembay_wdt_set_timeout_reg(struct watchdog_device *wdog)
68{
69 struct keembay_wdt *wdt = watchdog_get_drvdata(wdog);
70
71 keembay_wdt_writel(wdt, TIM_WATCHDOG, wdog->timeout * wdt->rate);
72}
73
74static void keembay_wdt_set_pretimeout_reg(struct watchdog_device *wdog)
75{
76 struct keembay_wdt *wdt = watchdog_get_drvdata(wdog);
77 u32 th_val = 0;
78
79 if (wdog->pretimeout)
80 th_val = wdog->timeout - wdog->pretimeout;
81
82 keembay_wdt_writel(wdt, TIM_WATCHDOG_INT_THRES, th_val * wdt->rate);
83}
84
85static int keembay_wdt_start(struct watchdog_device *wdog)
86{
87 struct keembay_wdt *wdt = watchdog_get_drvdata(wdog);
88
Shruthi Sanil624873f2021-05-17 23:19:51 +053089 keembay_wdt_writel(wdt, TIM_WDOG_EN, WDT_ENABLE);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +080090
91 return 0;
92}
93
94static int keembay_wdt_stop(struct watchdog_device *wdog)
95{
96 struct keembay_wdt *wdt = watchdog_get_drvdata(wdog);
97
Shruthi Sanil624873f2021-05-17 23:19:51 +053098 keembay_wdt_writel(wdt, TIM_WDOG_EN, WDT_DISABLE);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +080099
100 return 0;
101}
102
103static int keembay_wdt_ping(struct watchdog_device *wdog)
104{
105 keembay_wdt_set_timeout_reg(wdog);
106
107 return 0;
108}
109
110static int keembay_wdt_set_timeout(struct watchdog_device *wdog, u32 t)
111{
112 wdog->timeout = t;
113 keembay_wdt_set_timeout_reg(wdog);
Shruthi Sanil0f7bfaf2021-05-17 23:19:46 +0530114 keembay_wdt_set_pretimeout_reg(wdog);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +0800115
116 return 0;
117}
118
119static int keembay_wdt_set_pretimeout(struct watchdog_device *wdog, u32 t)
120{
121 if (t > wdog->timeout)
122 return -EINVAL;
123
124 wdog->pretimeout = t;
125 keembay_wdt_set_pretimeout_reg(wdog);
126
127 return 0;
128}
129
130static unsigned int keembay_wdt_get_timeleft(struct watchdog_device *wdog)
131{
132 struct keembay_wdt *wdt = watchdog_get_drvdata(wdog);
133
134 return keembay_wdt_readl(wdt, TIM_WATCHDOG) / wdt->rate;
135}
136
137/*
138 * SMC call is used to clear the interrupt bits, because the TIM_GEN_CONFIG
139 * register is in the secure bank.
140 */
141static irqreturn_t keembay_wdt_to_isr(int irq, void *dev_id)
142{
143 struct keembay_wdt *wdt = dev_id;
144 struct arm_smccc_res res;
145
Shruthi Sanil0e36a092021-05-17 23:19:48 +0530146 arm_smccc_smc(WDT_ISR_CLEAR, WDT_TO_INT_MASK, 0, 0, 0, 0, 0, 0, &res);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +0800147 dev_crit(wdt->wdd.parent, "Intel Keem Bay non-sec wdt timeout.\n");
148 emergency_restart();
149
150 return IRQ_HANDLED;
151}
152
153static irqreturn_t keembay_wdt_th_isr(int irq, void *dev_id)
154{
155 struct keembay_wdt *wdt = dev_id;
156 struct arm_smccc_res res;
157
Shruthi Sanil75f6c562021-05-17 23:19:47 +0530158 keembay_wdt_set_pretimeout(&wdt->wdd, 0x0);
159
Shruthi Sanil0e36a092021-05-17 23:19:48 +0530160 arm_smccc_smc(WDT_ISR_CLEAR, WDT_TH_INT_MASK, 0, 0, 0, 0, 0, 0, &res);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +0800161 dev_crit(wdt->wdd.parent, "Intel Keem Bay non-sec wdt pre-timeout.\n");
162 watchdog_notify_pretimeout(&wdt->wdd);
163
164 return IRQ_HANDLED;
165}
166
167static const struct watchdog_info keembay_wdt_info = {
168 .identity = "Intel Keem Bay Watchdog Timer",
169 .options = WDIOF_SETTIMEOUT |
170 WDIOF_PRETIMEOUT |
171 WDIOF_MAGICCLOSE |
172 WDIOF_KEEPALIVEPING,
173};
174
175static const struct watchdog_ops keembay_wdt_ops = {
176 .owner = THIS_MODULE,
177 .start = keembay_wdt_start,
178 .stop = keembay_wdt_stop,
179 .ping = keembay_wdt_ping,
180 .set_timeout = keembay_wdt_set_timeout,
181 .set_pretimeout = keembay_wdt_set_pretimeout,
182 .get_timeleft = keembay_wdt_get_timeleft,
183};
184
185static int keembay_wdt_probe(struct platform_device *pdev)
186{
187 struct device *dev = &pdev->dev;
188 struct keembay_wdt *wdt;
189 int ret;
190
191 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
192 if (!wdt)
193 return -ENOMEM;
194
195 wdt->base = devm_platform_ioremap_resource(pdev, 0);
196 if (IS_ERR(wdt->base))
197 return PTR_ERR(wdt->base);
198
199 /* we do not need to enable the clock as it is enabled by default */
200 wdt->clk = devm_clk_get(dev, NULL);
201 if (IS_ERR(wdt->clk))
202 return dev_err_probe(dev, PTR_ERR(wdt->clk), "Failed to get clock\n");
203
204 wdt->rate = clk_get_rate(wdt->clk);
205 if (!wdt->rate)
206 return dev_err_probe(dev, -EINVAL, "Failed to get clock rate\n");
207
208 wdt->th_irq = platform_get_irq_byname(pdev, "threshold");
209 if (wdt->th_irq < 0)
210 return dev_err_probe(dev, wdt->th_irq, "Failed to get IRQ for threshold\n");
211
212 ret = devm_request_irq(dev, wdt->th_irq, keembay_wdt_th_isr, 0,
213 "keembay-wdt", wdt);
214 if (ret)
215 return dev_err_probe(dev, ret, "Failed to request IRQ for threshold\n");
216
217 wdt->to_irq = platform_get_irq_byname(pdev, "timeout");
218 if (wdt->to_irq < 0)
219 return dev_err_probe(dev, wdt->to_irq, "Failed to get IRQ for timeout\n");
220
221 ret = devm_request_irq(dev, wdt->to_irq, keembay_wdt_to_isr, 0,
222 "keembay-wdt", wdt);
223 if (ret)
224 return dev_err_probe(dev, ret, "Failed to request IRQ for timeout\n");
225
226 wdt->wdd.parent = dev;
227 wdt->wdd.info = &keembay_wdt_info;
228 wdt->wdd.ops = &keembay_wdt_ops;
229 wdt->wdd.min_timeout = WDT_LOAD_MIN;
230 wdt->wdd.max_timeout = WDT_LOAD_MAX / wdt->rate;
231 wdt->wdd.timeout = WDT_TIMEOUT;
Shruthi Sanil29353812021-05-17 23:19:45 +0530232 wdt->wdd.pretimeout = WDT_PRETIMEOUT;
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +0800233
234 watchdog_set_drvdata(&wdt->wdd, wdt);
235 watchdog_set_nowayout(&wdt->wdd, nowayout);
236 watchdog_init_timeout(&wdt->wdd, timeout, dev);
237 keembay_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
Shruthi Sanil29353812021-05-17 23:19:45 +0530238 keembay_wdt_set_pretimeout(&wdt->wdd, wdt->wdd.pretimeout);
Vijayakannan Ayyathuraifa0f8d52020-12-17 02:32:48 +0800239
240 ret = devm_watchdog_register_device(dev, &wdt->wdd);
241 if (ret)
242 return dev_err_probe(dev, ret, "Failed to register watchdog device.\n");
243
244 platform_set_drvdata(pdev, wdt);
245 dev_info(dev, "Initial timeout %d sec%s.\n",
246 wdt->wdd.timeout, nowayout ? ", nowayout" : "");
247
248 return 0;
249}
250
251static int __maybe_unused keembay_wdt_suspend(struct device *dev)
252{
253 struct keembay_wdt *wdt = dev_get_drvdata(dev);
254
255 if (watchdog_active(&wdt->wdd))
256 return keembay_wdt_stop(&wdt->wdd);
257
258 return 0;
259}
260
261static int __maybe_unused keembay_wdt_resume(struct device *dev)
262{
263 struct keembay_wdt *wdt = dev_get_drvdata(dev);
264
265 if (watchdog_active(&wdt->wdd))
266 return keembay_wdt_start(&wdt->wdd);
267
268 return 0;
269}
270
271static SIMPLE_DEV_PM_OPS(keembay_wdt_pm_ops, keembay_wdt_suspend,
272 keembay_wdt_resume);
273
274static const struct of_device_id keembay_wdt_match[] = {
275 { .compatible = "intel,keembay-wdt" },
276 { }
277};
278MODULE_DEVICE_TABLE(of, keembay_wdt_match);
279
280static struct platform_driver keembay_wdt_driver = {
281 .probe = keembay_wdt_probe,
282 .driver = {
283 .name = "keembay_wdt",
284 .of_match_table = keembay_wdt_match,
285 .pm = &keembay_wdt_pm_ops,
286 },
287};
288
289module_platform_driver(keembay_wdt_driver);
290
291MODULE_DESCRIPTION("Intel Keem Bay SoC watchdog driver");
292MODULE_AUTHOR("Wan Ahmad Zainie <wan.ahmad.zainie.wan.mohamad@intel.com");
293MODULE_LICENSE("GPL v2");