1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) Siemens AG, 2020
6 * Jan Kiszka <jan.kiszka@siemens.com>
8 * Derived from linux/drivers/watchdog/rti_wdt.c
14 #include <power-domain.h>
18 /* Timer register set definition */
19 #define RTIDWDCTRL 0x90
20 #define RTIDWDPRLD 0x94
21 #define RTIWDSTATUS 0x98
23 #define RTIDWDCNTR 0xa0
24 #define RTIWWDRXCTRL 0xa4
25 #define RTIWWDSIZECTRL 0xa8
27 #define RTIWWDRX_NMI 0xa
29 #define RTIWWDSIZE_50P 0x50
31 #define WDENABLE_KEY 0xa98559da
33 #define WDKEY_SEQ0 0xe51a
34 #define WDKEY_SEQ1 0xa35c
36 #define WDT_PRELOAD_SHIFT 13
38 #define WDT_PRELOAD_MAX 0xfff
45 static int rti_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
47 struct rti_wdt_priv *priv = dev_get_priv(dev);
51 if (readl(priv->regs + RTIDWDCTRL) == WDENABLE_KEY)
54 timer_margin = timeout_ms * priv->clk_khz / 1000;
55 timer_margin >>= WDT_PRELOAD_SHIFT;
56 if (timer_margin > WDT_PRELOAD_MAX)
57 timer_margin = WDT_PRELOAD_MAX;
59 writel(timer_margin, priv->regs + RTIDWDPRLD);
60 writel(RTIWWDRX_NMI, priv->regs + RTIWWDRXCTRL);
61 writel(RTIWWDSIZE_50P, priv->regs + RTIWWDSIZECTRL);
63 readl(priv->regs + RTIWWDSIZECTRL);
65 writel(WDENABLE_KEY, priv->regs + RTIDWDCTRL);
70 static int rti_wdt_reset(struct udevice *dev)
72 struct rti_wdt_priv *priv = dev_get_priv(dev);
75 /* Make sure we do not reset too early */
76 prld = readl(priv->regs + RTIDWDPRLD) << WDT_PRELOAD_SHIFT;
77 if (readl(priv->regs + RTIDWDCNTR) >= prld / 2)
80 writel(WDKEY_SEQ0, priv->regs + RTIWDKEY);
81 writel(WDKEY_SEQ1, priv->regs + RTIWDKEY);
86 static int rti_wdt_probe(struct udevice *dev)
88 struct rti_wdt_priv *priv = dev_get_priv(dev);
92 priv->regs = devfdt_get_addr(dev);
96 ret = clk_get_by_index(dev, 0, &clk);
100 priv->clk_khz = clk_get_rate(&clk);
105 static const struct wdt_ops rti_wdt_ops = {
106 .start = rti_wdt_start,
107 .reset = rti_wdt_reset,
110 static const struct udevice_id rti_wdt_ids[] = {
111 { .compatible = "ti,j7-rti-wdt" },
115 U_BOOT_DRIVER(rti_wdt) = {
118 .of_match = rti_wdt_ids,
120 .probe = rti_wdt_probe,
121 .priv_auto_alloc_size = sizeof(struct rti_wdt_priv),
122 .flags = DM_FLAG_REMOVE_WITH_PD_ON,