1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/iopoll.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/reboot.h>
14 #include <linux/watchdog.h>
17 #define WDOG_CS_FLG BIT(14)
18 #define WDOG_CS_CMD32EN BIT(13)
19 #define WDOG_CS_PRES BIT(12)
20 #define WDOG_CS_ULK BIT(11)
21 #define WDOG_CS_RCS BIT(10)
23 #define LPO_CLK_SHIFT 8
24 #define WDOG_CS_CLK (LPO_CLK << LPO_CLK_SHIFT)
25 #define WDOG_CS_EN BIT(7)
26 #define WDOG_CS_UPDATE BIT(5)
27 #define WDOG_CS_WAIT BIT(1)
28 #define WDOG_CS_STOP BIT(0)
31 #define WDOG_TOVAL 0x8
33 #define REFRESH_SEQ0 0xA602
34 #define REFRESH_SEQ1 0xB480
35 #define REFRESH ((REFRESH_SEQ1 << 16) | REFRESH_SEQ0)
37 #define UNLOCK_SEQ0 0xC520
38 #define UNLOCK_SEQ1 0xD928
39 #define UNLOCK ((UNLOCK_SEQ1 << 16) | UNLOCK_SEQ0)
41 #define DEFAULT_TIMEOUT 60
42 #define MAX_TIMEOUT 128
43 #define WDOG_CLOCK_RATE 1000
44 #define WDOG_ULK_WAIT_TIMEOUT 1000
45 #define WDOG_RCS_WAIT_TIMEOUT 10000
46 #define WDOG_RCS_POST_WAIT 3000
50 static bool nowayout = WATCHDOG_NOWAYOUT;
51 module_param(nowayout, bool, 0000);
52 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
53 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
55 struct imx_wdt_hw_feature {
56 bool prescaler_enable;
60 struct imx7ulp_wdt_device {
61 struct watchdog_device wdd;
65 const struct imx_wdt_hw_feature *hw;
68 static int imx7ulp_wdt_wait_ulk(void __iomem *base)
70 u32 val = readl(base + WDOG_CS);
72 if (!(val & WDOG_CS_ULK) &&
73 readl_poll_timeout_atomic(base + WDOG_CS, val,
75 WDOG_ULK_WAIT_TIMEOUT))
81 static int imx7ulp_wdt_wait_rcs(struct imx7ulp_wdt_device *wdt)
84 u32 val = readl(wdt->base + WDOG_CS);
85 u64 timeout = (val & WDOG_CS_PRES) ?
86 WDOG_RCS_WAIT_TIMEOUT * 256 : WDOG_RCS_WAIT_TIMEOUT;
87 unsigned long wait_min = (val & WDOG_CS_PRES) ?
88 WDOG_RCS_POST_WAIT * 256 : WDOG_RCS_POST_WAIT;
90 if (!(val & WDOG_CS_RCS) &&
91 readl_poll_timeout(wdt->base + WDOG_CS, val, val & WDOG_CS_RCS, 100,
95 /* Wait 2.5 clocks after RCS done */
96 if (wdt->post_rcs_wait)
97 usleep_range(wait_min, wait_min + 2000);
102 static int _imx7ulp_wdt_enable(struct imx7ulp_wdt_device *wdt, bool enable)
104 u32 val = readl(wdt->base + WDOG_CS);
108 writel(UNLOCK, wdt->base + WDOG_CNT);
109 ret = imx7ulp_wdt_wait_ulk(wdt->base);
113 writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
115 writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
118 ret = imx7ulp_wdt_wait_rcs(wdt);
127 static int imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable)
129 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
132 u32 loop = RETRY_MAX;
135 ret = _imx7ulp_wdt_enable(wdt, enable);
136 val = readl(wdt->base + WDOG_CS);
137 } while (--loop > 0 && ((!!(val & WDOG_CS_EN)) != enable || ret));
145 static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
147 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
149 writel(REFRESH, wdt->base + WDOG_CNT);
154 static int imx7ulp_wdt_start(struct watchdog_device *wdog)
156 return imx7ulp_wdt_enable(wdog, true);
159 static int imx7ulp_wdt_stop(struct watchdog_device *wdog)
161 return imx7ulp_wdt_enable(wdog, false);
164 static int _imx7ulp_wdt_set_timeout(struct imx7ulp_wdt_device *wdt,
170 writel(UNLOCK, wdt->base + WDOG_CNT);
171 ret = imx7ulp_wdt_wait_ulk(wdt->base);
174 writel(toval, wdt->base + WDOG_TOVAL);
176 ret = imx7ulp_wdt_wait_rcs(wdt);
184 static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog,
185 unsigned int timeout)
187 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
188 u32 toval = wdt->hw->wdog_clock_rate * timeout;
191 u32 loop = RETRY_MAX;
194 ret = _imx7ulp_wdt_set_timeout(wdt, toval);
195 val = readl(wdt->base + WDOG_TOVAL);
196 } while (--loop > 0 && (val != toval || ret));
201 wdog->timeout = timeout;
205 static int imx7ulp_wdt_restart(struct watchdog_device *wdog,
206 unsigned long action, void *data)
208 struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
211 ret = imx7ulp_wdt_enable(wdog, true);
215 ret = imx7ulp_wdt_set_timeout(&wdt->wdd, 1);
219 /* wait for wdog to fire */
226 static const struct watchdog_ops imx7ulp_wdt_ops = {
227 .owner = THIS_MODULE,
228 .start = imx7ulp_wdt_start,
229 .stop = imx7ulp_wdt_stop,
230 .ping = imx7ulp_wdt_ping,
231 .set_timeout = imx7ulp_wdt_set_timeout,
232 .restart = imx7ulp_wdt_restart,
235 static const struct watchdog_info imx7ulp_wdt_info = {
236 .identity = "i.MX7ULP watchdog timer",
237 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
241 static int _imx7ulp_wdt_init(struct imx7ulp_wdt_device *wdt, unsigned int timeout, unsigned int cs)
248 val = readl(wdt->base + WDOG_CS);
249 if (val & WDOG_CS_CMD32EN) {
250 writel(UNLOCK, wdt->base + WDOG_CNT);
253 /* unlock the wdog for reconfiguration */
254 writel_relaxed(UNLOCK_SEQ0, wdt->base + WDOG_CNT);
255 writel_relaxed(UNLOCK_SEQ1, wdt->base + WDOG_CNT);
259 ret = imx7ulp_wdt_wait_ulk(wdt->base);
263 /* set an initial timeout value in TOVAL */
264 writel(timeout, wdt->base + WDOG_TOVAL);
265 writel(cs, wdt->base + WDOG_CS);
267 ret = imx7ulp_wdt_wait_rcs(wdt);
276 static int imx7ulp_wdt_init(struct imx7ulp_wdt_device *wdt, unsigned int timeout)
278 /* enable 32bit command sequence and reconfigure */
279 u32 val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE |
280 WDOG_CS_WAIT | WDOG_CS_STOP;
283 u32 loop = RETRY_MAX;
285 if (wdt->hw->prescaler_enable)
289 ret = _imx7ulp_wdt_init(wdt, timeout, val);
290 toval = readl(wdt->base + WDOG_TOVAL);
291 cs = readl(wdt->base + WDOG_CS);
292 cs &= ~(WDOG_CS_FLG | WDOG_CS_ULK | WDOG_CS_RCS);
293 } while (--loop > 0 && (cs != val || toval != timeout || ret));
301 static int imx7ulp_wdt_probe(struct platform_device *pdev)
303 struct imx7ulp_wdt_device *imx7ulp_wdt;
304 struct device *dev = &pdev->dev;
305 struct watchdog_device *wdog;
308 imx7ulp_wdt = devm_kzalloc(dev, sizeof(*imx7ulp_wdt), GFP_KERNEL);
312 platform_set_drvdata(pdev, imx7ulp_wdt);
314 imx7ulp_wdt->base = devm_platform_ioremap_resource(pdev, 0);
315 if (IS_ERR(imx7ulp_wdt->base))
316 return PTR_ERR(imx7ulp_wdt->base);
318 imx7ulp_wdt->clk = devm_clk_get_enabled(dev, NULL);
319 if (IS_ERR(imx7ulp_wdt->clk)) {
320 dev_err(dev, "Failed to get watchdog clock\n");
321 return PTR_ERR(imx7ulp_wdt->clk);
324 imx7ulp_wdt->post_rcs_wait = true;
325 if (of_device_is_compatible(dev->of_node,
326 "fsl,imx8ulp-wdt")) {
327 dev_info(dev, "imx8ulp wdt probe\n");
328 imx7ulp_wdt->post_rcs_wait = false;
330 dev_info(dev, "imx7ulp wdt probe\n");
333 wdog = &imx7ulp_wdt->wdd;
334 wdog->info = &imx7ulp_wdt_info;
335 wdog->ops = &imx7ulp_wdt_ops;
336 wdog->min_timeout = 1;
337 wdog->max_timeout = MAX_TIMEOUT;
339 wdog->timeout = DEFAULT_TIMEOUT;
341 watchdog_init_timeout(wdog, 0, dev);
342 watchdog_stop_on_reboot(wdog);
343 watchdog_stop_on_unregister(wdog);
344 watchdog_set_drvdata(wdog, imx7ulp_wdt);
346 imx7ulp_wdt->hw = of_device_get_match_data(dev);
347 ret = imx7ulp_wdt_init(imx7ulp_wdt, wdog->timeout * imx7ulp_wdt->hw->wdog_clock_rate);
351 return devm_watchdog_register_device(dev, wdog);
354 static int __maybe_unused imx7ulp_wdt_suspend_noirq(struct device *dev)
356 struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
358 if (watchdog_active(&imx7ulp_wdt->wdd))
359 imx7ulp_wdt_stop(&imx7ulp_wdt->wdd);
361 clk_disable_unprepare(imx7ulp_wdt->clk);
366 static int __maybe_unused imx7ulp_wdt_resume_noirq(struct device *dev)
368 struct imx7ulp_wdt_device *imx7ulp_wdt = dev_get_drvdata(dev);
369 u32 timeout = imx7ulp_wdt->wdd.timeout * imx7ulp_wdt->hw->wdog_clock_rate;
372 ret = clk_prepare_enable(imx7ulp_wdt->clk);
376 if (watchdog_active(&imx7ulp_wdt->wdd)) {
377 imx7ulp_wdt_init(imx7ulp_wdt, timeout);
378 imx7ulp_wdt_start(&imx7ulp_wdt->wdd);
379 imx7ulp_wdt_ping(&imx7ulp_wdt->wdd);
385 static const struct dev_pm_ops imx7ulp_wdt_pm_ops = {
386 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx7ulp_wdt_suspend_noirq,
387 imx7ulp_wdt_resume_noirq)
390 static const struct imx_wdt_hw_feature imx7ulp_wdt_hw = {
391 .prescaler_enable = false,
392 .wdog_clock_rate = 1000,
395 static const struct imx_wdt_hw_feature imx93_wdt_hw = {
396 .prescaler_enable = true,
397 .wdog_clock_rate = 125,
400 static const struct of_device_id imx7ulp_wdt_dt_ids[] = {
401 { .compatible = "fsl,imx8ulp-wdt", .data = &imx7ulp_wdt_hw, },
402 { .compatible = "fsl,imx7ulp-wdt", .data = &imx7ulp_wdt_hw, },
403 { .compatible = "fsl,imx93-wdt", .data = &imx93_wdt_hw, },
406 MODULE_DEVICE_TABLE(of, imx7ulp_wdt_dt_ids);
408 static struct platform_driver imx7ulp_wdt_driver = {
409 .probe = imx7ulp_wdt_probe,
411 .name = "imx7ulp-wdt",
412 .pm = &imx7ulp_wdt_pm_ops,
413 .of_match_table = imx7ulp_wdt_dt_ids,
416 module_platform_driver(imx7ulp_wdt_driver);
418 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
419 MODULE_DESCRIPTION("Freescale i.MX7ULP watchdog driver");
420 MODULE_LICENSE("GPL v2");