1 // SPDX-License-Identifier: GPL-2.0-only
3 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
5 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/watchdog.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
19 #include <asm/txx9tmr.h>
21 #define WD_TIMER_CCD 7 /* 1/256 */
22 #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
23 #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
24 #define TIMER_MARGIN 60 /* Default is 60 seconds */
26 static unsigned int timeout = TIMER_MARGIN; /* in seconds */
27 module_param(timeout, uint, 0);
28 MODULE_PARM_DESC(timeout,
29 "Watchdog timeout in seconds. "
30 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
31 "default=" __MODULE_STRING(TIMER_MARGIN) ")");
33 static bool nowayout = WATCHDOG_NOWAYOUT;
34 module_param(nowayout, bool, 0);
35 MODULE_PARM_DESC(nowayout,
36 "Watchdog cannot be stopped once started "
37 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
39 static struct txx9_tmr_reg __iomem *txx9wdt_reg;
40 static struct clk *txx9_imclk;
41 static DEFINE_SPINLOCK(txx9_lock);
43 static int txx9wdt_ping(struct watchdog_device *wdt_dev)
45 spin_lock(&txx9_lock);
46 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
47 spin_unlock(&txx9_lock);
51 static int txx9wdt_start(struct watchdog_device *wdt_dev)
53 spin_lock(&txx9_lock);
54 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
55 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
56 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
57 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
59 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
60 spin_unlock(&txx9_lock);
64 static int txx9wdt_stop(struct watchdog_device *wdt_dev)
66 spin_lock(&txx9_lock);
67 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
68 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
70 spin_unlock(&txx9_lock);
74 static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
75 unsigned int new_timeout)
77 wdt_dev->timeout = new_timeout;
78 txx9wdt_stop(wdt_dev);
79 txx9wdt_start(wdt_dev);
83 static const struct watchdog_info txx9wdt_info = {
84 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
85 .identity = "Hardware Watchdog for TXx9",
88 static const struct watchdog_ops txx9wdt_ops = {
90 .start = txx9wdt_start,
93 .set_timeout = txx9wdt_set_timeout,
96 static struct watchdog_device txx9wdt = {
97 .info = &txx9wdt_info,
101 static int __init txx9wdt_probe(struct platform_device *dev)
105 txx9_imclk = clk_get(NULL, "imbus_clk");
106 if (IS_ERR(txx9_imclk)) {
107 ret = PTR_ERR(txx9_imclk);
111 ret = clk_prepare_enable(txx9_imclk);
118 txx9wdt_reg = devm_platform_ioremap_resource(dev, 0);
119 if (IS_ERR(txx9wdt_reg)) {
120 ret = PTR_ERR(txx9wdt_reg);
124 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
125 timeout = TIMER_MARGIN;
126 txx9wdt.timeout = timeout;
127 txx9wdt.min_timeout = 1;
128 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
129 txx9wdt.parent = &dev->dev;
130 watchdog_set_nowayout(&txx9wdt, nowayout);
132 ret = watchdog_register_device(&txx9wdt);
136 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
137 timeout, WD_MAX_TIMEOUT, nowayout);
142 clk_disable_unprepare(txx9_imclk);
148 static int __exit txx9wdt_remove(struct platform_device *dev)
150 watchdog_unregister_device(&txx9wdt);
151 clk_disable_unprepare(txx9_imclk);
156 static void txx9wdt_shutdown(struct platform_device *dev)
158 txx9wdt_stop(&txx9wdt);
161 static struct platform_driver txx9wdt_driver = {
162 .remove = __exit_p(txx9wdt_remove),
163 .shutdown = txx9wdt_shutdown,
169 module_platform_driver_probe(txx9wdt_driver, txx9wdt_probe);
171 MODULE_DESCRIPTION("TXx9 Watchdog Driver");
172 MODULE_LICENSE("GPL");
173 MODULE_ALIAS("platform:txx9wdt");