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>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/types.h>
22 #include <linux/watchdog.h>
24 #define DEFAULT_HEARTBEAT 60
26 /* Max heartbeat is calculated at 32kHz source clock */
27 #define MAX_HEARTBEAT 1000
29 /* Timer register set definition */
30 #define RTIDWDCTRL 0x90
31 #define RTIDWDPRLD 0x94
32 #define RTIWDSTATUS 0x98
34 #define RTIDWDCNTR 0xa0
35 #define RTIWWDRXCTRL 0xa4
36 #define RTIWWDSIZECTRL 0xa8
38 #define RTIWWDRX_NMI 0xa
40 #define RTIWWDSIZE_50P 0x50
41 #define RTIWWDSIZE_25P 0x500
42 #define RTIWWDSIZE_12P5 0x5000
43 #define RTIWWDSIZE_6P25 0x50000
44 #define RTIWWDSIZE_3P125 0x500000
46 #define WDENABLE_KEY 0xa98559da
48 #define WDKEY_SEQ0 0xe51a
49 #define WDKEY_SEQ1 0xa35c
51 #define WDT_PRELOAD_SHIFT 13
53 #define WDT_PRELOAD_MAX 0xfff
57 #define PON_REASON_SOF_NUM 0xBBBBCCCC
58 #define PON_REASON_MAGIC_NUM 0xDDDDDDDD
59 #define PON_REASON_EOF_NUM 0xCCCCBBBB
60 #define RESERVED_MEM_MIN_SIZE 12
62 static int heartbeat = DEFAULT_HEARTBEAT;
65 * struct to hold data for each WDT device
66 * @base - base io address of WD device
67 * @freq - source clock frequency of WDT
68 * @wdd - hold watchdog device as is in WDT core
70 struct rti_wdt_device {
73 struct watchdog_device wdd;
76 static int rti_wdt_start(struct watchdog_device *wdd)
79 struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
82 ret = pm_runtime_resume_and_get(wdd->parent);
86 /* set timeout period */
87 timer_margin = (u64)wdd->timeout * wdt->freq;
88 timer_margin >>= WDT_PRELOAD_SHIFT;
89 if (timer_margin > WDT_PRELOAD_MAX)
90 timer_margin = WDT_PRELOAD_MAX;
91 writel_relaxed(timer_margin, wdt->base + RTIDWDPRLD);
94 * RTI only supports a windowed mode, where the watchdog can only
95 * be petted during the open window; not too early or not too late.
96 * The HW configuration options only allow for the open window size
97 * to be 50% or less than that; we obviouly want to configure the open
98 * window as large as possible so we select the 50% option.
100 wdd->min_hw_heartbeat_ms = 500 * wdd->timeout;
102 /* Generate NMI when wdt expires */
103 writel_relaxed(RTIWWDRX_NMI, wdt->base + RTIWWDRXCTRL);
105 /* Open window size 50%; this is the largest window size available */
106 writel_relaxed(RTIWWDSIZE_50P, wdt->base + RTIWWDSIZECTRL);
108 readl_relaxed(wdt->base + RTIWWDSIZECTRL);
110 /* enable watchdog */
111 writel_relaxed(WDENABLE_KEY, wdt->base + RTIDWDCTRL);
115 static int rti_wdt_ping(struct watchdog_device *wdd)
117 struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
119 /* put watchdog in service state */
120 writel_relaxed(WDKEY_SEQ0, wdt->base + RTIWDKEY);
121 /* put watchdog in active state */
122 writel_relaxed(WDKEY_SEQ1, wdt->base + RTIWDKEY);
127 static int rti_wdt_setup_hw_hb(struct watchdog_device *wdd, u32 wsize)
130 * RTI only supports a windowed mode, where the watchdog can only
131 * be petted during the open window; not too early or not too late.
132 * The HW configuration options only allow for the open window size
133 * to be 50% or less than that.
137 /* 50% open window => 50% min heartbeat */
138 wdd->min_hw_heartbeat_ms = 500 * heartbeat;
142 /* 25% open window => 75% min heartbeat */
143 wdd->min_hw_heartbeat_ms = 750 * heartbeat;
146 case RTIWWDSIZE_12P5:
147 /* 12.5% open window => 87.5% min heartbeat */
148 wdd->min_hw_heartbeat_ms = 875 * heartbeat;
151 case RTIWWDSIZE_6P25:
152 /* 6.5% open window => 93.5% min heartbeat */
153 wdd->min_hw_heartbeat_ms = 935 * heartbeat;
156 case RTIWWDSIZE_3P125:
157 /* 3.125% open window => 96.9% min heartbeat */
158 wdd->min_hw_heartbeat_ms = 969 * heartbeat;
168 static unsigned int rti_wdt_get_timeleft_ms(struct watchdog_device *wdd)
172 struct rti_wdt_device *wdt = watchdog_get_drvdata(wdd);
174 /* if timeout has occurred then return 0 */
175 val = readl_relaxed(wdt->base + RTIWDSTATUS);
179 timer_counter = readl_relaxed(wdt->base + RTIDWDCNTR);
181 timer_counter *= 1000;
183 do_div(timer_counter, wdt->freq);
185 return timer_counter;
188 static unsigned int rti_wdt_get_timeleft(struct watchdog_device *wdd)
190 return rti_wdt_get_timeleft_ms(wdd) / 1000;
193 static const struct watchdog_info rti_wdt_info = {
194 .options = WDIOF_KEEPALIVEPING,
195 .identity = "K3 RTI Watchdog",
198 static const struct watchdog_ops rti_wdt_ops = {
199 .owner = THIS_MODULE,
200 .start = rti_wdt_start,
201 .ping = rti_wdt_ping,
202 .get_timeleft = rti_wdt_get_timeleft,
205 static int rti_wdt_probe(struct platform_device *pdev)
208 struct device *dev = &pdev->dev;
209 struct watchdog_device *wdd;
210 struct rti_wdt_device *wdt;
213 struct device_node *node;
214 u32 reserved_mem_size;
219 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
223 clk = clk_get(dev, NULL);
225 return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
227 wdt->freq = clk_get_rate(clk);
232 dev_err(dev, "Failed to get fck rate.\n");
237 * If watchdog is running at 32k clock, it is not accurate.
238 * Adjust frequency down in this case so that we don't pet
239 * the watchdog too often.
241 if (wdt->freq < 32768)
242 wdt->freq = wdt->freq * 9 / 10;
244 pm_runtime_enable(dev);
245 ret = pm_runtime_resume_and_get(dev);
247 pm_runtime_disable(&pdev->dev);
248 return dev_err_probe(dev, ret, "runtime pm failed\n");
251 platform_set_drvdata(pdev, wdt);
254 wdd->info = &rti_wdt_info;
255 wdd->ops = &rti_wdt_ops;
256 wdd->min_timeout = 1;
257 wdd->max_hw_heartbeat_ms = (WDT_PRELOAD_MAX << WDT_PRELOAD_SHIFT) /
261 watchdog_set_drvdata(wdd, wdt);
262 watchdog_set_nowayout(wdd, 1);
263 watchdog_set_restart_priority(wdd, 128);
265 wdt->base = devm_platform_ioremap_resource(pdev, 0);
266 if (IS_ERR(wdt->base)) {
267 ret = PTR_ERR(wdt->base);
271 if (readl(wdt->base + RTIDWDCTRL) == WDENABLE_KEY) {
272 int preset_heartbeat;
277 set_bit(WDOG_HW_RUNNING, &wdd->status);
278 time_left_ms = rti_wdt_get_timeleft_ms(wdd);
279 heartbeat_ms = readl(wdt->base + RTIDWDPRLD);
280 heartbeat_ms <<= WDT_PRELOAD_SHIFT;
281 heartbeat_ms *= 1000;
282 do_div(heartbeat_ms, wdt->freq);
283 preset_heartbeat = heartbeat_ms + 500;
284 preset_heartbeat /= 1000;
285 if (preset_heartbeat != heartbeat)
286 dev_warn(dev, "watchdog already running, ignoring heartbeat config!\n");
288 heartbeat = preset_heartbeat;
290 wsize = readl(wdt->base + RTIWWDSIZECTRL);
291 ret = rti_wdt_setup_hw_hb(wdd, wsize);
293 dev_err(dev, "bad window size.\n");
297 last_ping = heartbeat_ms - time_left_ms;
298 if (time_left_ms > heartbeat_ms) {
299 dev_warn(dev, "time_left > heartbeat? Assuming last ping just before now.\n");
304 node = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
306 ret = of_address_to_resource(node, 0, &res);
308 dev_err(dev, "No memory address assigned to the region.\n");
313 * If reserved memory is defined for watchdog reset cause.
314 * Readout the Power-on(PON) reason and pass to bootstatus.
317 reserved_mem_size = resource_size(&res);
318 if (reserved_mem_size < RESERVED_MEM_MIN_SIZE) {
319 dev_err(dev, "The size of reserved memory is too small.\n");
324 vaddr = memremap(paddr, reserved_mem_size, MEMREMAP_WB);
326 dev_err(dev, "Failed to map memory-region.\n");
331 if (vaddr[0] == PON_REASON_SOF_NUM &&
332 vaddr[1] == PON_REASON_MAGIC_NUM &&
333 vaddr[2] == PON_REASON_EOF_NUM) {
334 wdd->bootstatus |= WDIOF_CARDRESET;
336 memset(vaddr, 0, reserved_mem_size);
340 watchdog_init_timeout(wdd, heartbeat, dev);
342 ret = watchdog_register_device(wdd);
344 dev_err(dev, "cannot register watchdog device\n");
349 watchdog_set_last_hw_keepalive(wdd, last_ping);
351 if (!watchdog_hw_running(wdd))
352 pm_runtime_put_sync(&pdev->dev);
357 pm_runtime_put_sync(&pdev->dev);
358 pm_runtime_disable(&pdev->dev);
363 static void rti_wdt_remove(struct platform_device *pdev)
365 struct rti_wdt_device *wdt = platform_get_drvdata(pdev);
367 watchdog_unregister_device(&wdt->wdd);
369 if (!pm_runtime_suspended(&pdev->dev))
370 pm_runtime_put(&pdev->dev);
372 pm_runtime_disable(&pdev->dev);
375 static const struct of_device_id rti_wdt_of_match[] = {
376 { .compatible = "ti,j7-rti-wdt", },
379 MODULE_DEVICE_TABLE(of, rti_wdt_of_match);
381 static struct platform_driver rti_wdt_driver = {
384 .of_match_table = rti_wdt_of_match,
386 .probe = rti_wdt_probe,
387 .remove_new = rti_wdt_remove,
390 module_platform_driver(rti_wdt_driver);
392 MODULE_AUTHOR("Tero Kristo <t-kristo@ti.com>");
393 MODULE_DESCRIPTION("K3 RTI Watchdog Driver");
395 module_param(heartbeat, int, 0);
396 MODULE_PARM_DESC(heartbeat,
397 "Watchdog heartbeat period in seconds from 1 to "
398 __MODULE_STRING(MAX_HEARTBEAT) ", default "
399 __MODULE_STRING(DEFAULT_HEARTBEAT));
401 MODULE_LICENSE("GPL");
402 MODULE_ALIAS("platform:rti-wdt");