1 // SPDX-License-Identifier: GPL-2.0
3 * Watchdog driver for the K3 RTI module
5 * (c) Copyright 2019-2020 Texas Instruments Inc.
10 #include <linux/device.h>
11 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/types.h>
20 #include <linux/watchdog.h>
22 #define DEFAULT_HEARTBEAT 60
24 /* Max heartbeat is calculated at 32kHz source clock */
25 #define MAX_HEARTBEAT 1000
27 /* Timer register set definition */
28 #define RTIDWDCTRL 0x90
29 #define RTIDWDPRLD 0x94
30 #define RTIWDSTATUS 0x98
32 #define RTIDWDCNTR 0xa0
33 #define RTIWWDRXCTRL 0xa4
34 #define RTIWWDSIZECTRL 0xa8
36 #define RTIWWDRX_NMI 0xa
38 #define RTIWWDSIZE_50P 0x50
40 #define WDENABLE_KEY 0xa98559da
42 #define WDKEY_SEQ0 0xe51a
43 #define WDKEY_SEQ1 0xa35c
45 #define WDT_PRELOAD_SHIFT 13
47 #define WDT_PRELOAD_MAX 0xfff
54 * struct to hold data for each WDT device
55 * @base - base io address of WD device
56 * @freq - source clock frequency of WDT
57 * @wdd - hold watchdog device as is in WDT core
59 struct rti_wdt_device
{
62 struct watchdog_device wdd
;
65 static int rti_wdt_start(struct watchdog_device
*wdd
)
68 struct rti_wdt_device
*wdt
= watchdog_get_drvdata(wdd
);
70 /* set timeout period */
71 timer_margin
= (u64
)wdd
->timeout
* wdt
->freq
;
72 timer_margin
>>= WDT_PRELOAD_SHIFT
;
73 if (timer_margin
> WDT_PRELOAD_MAX
)
74 timer_margin
= WDT_PRELOAD_MAX
;
75 writel_relaxed(timer_margin
, wdt
->base
+ RTIDWDPRLD
);
78 * RTI only supports a windowed mode, where the watchdog can only
79 * be petted during the open window; not too early or not too late.
80 * The HW configuration options only allow for the open window size
81 * to be 50% or less than that; we obviouly want to configure the open
82 * window as large as possible so we select the 50% option. To avoid
83 * any glitches, we accommodate 5% safety margin also, so we setup
84 * the min_hw_hearbeat at 55% of the timeout period.
86 wdd
->min_hw_heartbeat_ms
= 11 * wdd
->timeout
* 1000 / 20;
88 /* Generate NMI when wdt expires */
89 writel_relaxed(RTIWWDRX_NMI
, wdt
->base
+ RTIWWDRXCTRL
);
91 /* Open window size 50%; this is the largest window size available */
92 writel_relaxed(RTIWWDSIZE_50P
, wdt
->base
+ RTIWWDSIZECTRL
);
94 readl_relaxed(wdt
->base
+ RTIWWDSIZECTRL
);
97 writel_relaxed(WDENABLE_KEY
, wdt
->base
+ RTIDWDCTRL
);
101 static int rti_wdt_ping(struct watchdog_device
*wdd
)
103 struct rti_wdt_device
*wdt
= watchdog_get_drvdata(wdd
);
105 /* put watchdog in service state */
106 writel_relaxed(WDKEY_SEQ0
, wdt
->base
+ RTIWDKEY
);
107 /* put watchdog in active state */
108 writel_relaxed(WDKEY_SEQ1
, wdt
->base
+ RTIWDKEY
);
113 static unsigned int rti_wdt_get_timeleft(struct watchdog_device
*wdd
)
117 struct rti_wdt_device
*wdt
= watchdog_get_drvdata(wdd
);
119 /* if timeout has occurred then return 0 */
120 val
= readl_relaxed(wdt
->base
+ RTIWDSTATUS
);
124 timer_counter
= readl_relaxed(wdt
->base
+ RTIDWDCNTR
);
126 do_div(timer_counter
, wdt
->freq
);
128 return timer_counter
;
131 static const struct watchdog_info rti_wdt_info
= {
132 .options
= WDIOF_KEEPALIVEPING
,
133 .identity
= "K3 RTI Watchdog",
136 static const struct watchdog_ops rti_wdt_ops
= {
137 .owner
= THIS_MODULE
,
138 .start
= rti_wdt_start
,
139 .ping
= rti_wdt_ping
,
140 .get_timeleft
= rti_wdt_get_timeleft
,
143 static int rti_wdt_probe(struct platform_device
*pdev
)
146 struct device
*dev
= &pdev
->dev
;
147 struct resource
*wdt_mem
;
148 struct watchdog_device
*wdd
;
149 struct rti_wdt_device
*wdt
;
152 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
156 clk
= clk_get(dev
, NULL
);
158 if (PTR_ERR(clk
) != -EPROBE_DEFER
)
159 dev_err(dev
, "failed to get clock\n");
163 wdt
->freq
= clk_get_rate(clk
);
168 dev_err(dev
, "Failed to get fck rate.\n");
172 pm_runtime_enable(dev
);
173 ret
= pm_runtime_get_sync(dev
);
175 if (ret
!= -EPROBE_DEFER
)
176 dev_err(&pdev
->dev
, "runtime pm failed\n");
180 platform_set_drvdata(pdev
, wdt
);
183 wdd
->info
= &rti_wdt_info
;
184 wdd
->ops
= &rti_wdt_ops
;
185 wdd
->min_timeout
= 1;
186 wdd
->max_hw_heartbeat_ms
= (WDT_PRELOAD_MAX
<< WDT_PRELOAD_SHIFT
) /
188 wdd
->timeout
= DEFAULT_HEARTBEAT
;
191 watchdog_init_timeout(wdd
, heartbeat
, dev
);
193 watchdog_set_drvdata(wdd
, wdt
);
194 watchdog_set_nowayout(wdd
, 1);
195 watchdog_set_restart_priority(wdd
, 128);
197 wdt_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
198 wdt
->base
= devm_ioremap_resource(dev
, wdt_mem
);
199 if (IS_ERR(wdt
->base
)) {
200 ret
= PTR_ERR(wdt
->base
);
204 ret
= watchdog_register_device(wdd
);
206 dev_err(dev
, "cannot register watchdog device\n");
213 pm_runtime_put_sync(&pdev
->dev
);
218 static int rti_wdt_remove(struct platform_device
*pdev
)
220 struct rti_wdt_device
*wdt
= platform_get_drvdata(pdev
);
222 watchdog_unregister_device(&wdt
->wdd
);
223 pm_runtime_put(&pdev
->dev
);
228 static const struct of_device_id rti_wdt_of_match
[] = {
229 { .compatible
= "ti,j7-rti-wdt", },
232 MODULE_DEVICE_TABLE(of
, rti_wdt_of_match
);
234 static struct platform_driver rti_wdt_driver
= {
237 .of_match_table
= rti_wdt_of_match
,
239 .probe
= rti_wdt_probe
,
240 .remove
= rti_wdt_remove
,
243 module_platform_driver(rti_wdt_driver
);
245 MODULE_AUTHOR("Tero Kristo <t-kristo@ti.com>");
246 MODULE_DESCRIPTION("K3 RTI Watchdog Driver");
248 module_param(heartbeat
, int, 0);
249 MODULE_PARM_DESC(heartbeat
,
250 "Watchdog heartbeat period in seconds from 1 to "
251 __MODULE_STRING(MAX_HEARTBEAT
) ", default "
252 __MODULE_STRING(DEFAULT_HEARTBEAT
));
254 MODULE_LICENSE("GPL");
255 MODULE_ALIAS("platform:rti-wdt");