blob: 99168d0cad03983e8c3b35b52b33bb11289cdb4d [file] [log] [blame]
Jan Kiszkaddf21d52020-06-23 13:15:08 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) Siemens AG, 2020
4 *
5 * Authors:
6 * Jan Kiszka <jan.kiszka@siemens.com>
7 *
8 * Derived from linux/drivers/watchdog/rti_wdt.c
9 */
10
Jan Kiszkaddf21d52020-06-23 13:15:08 +020011#include <clk.h>
12#include <dm.h>
Jan Kiszkad35cdfb2021-09-18 08:17:55 +020013#include <dm/device_compat.h>
Jan Kiszkaddf21d52020-06-23 13:15:08 +020014#include <power-domain.h>
15#include <wdt.h>
16#include <asm/io.h>
Jan Kiszkad35cdfb2021-09-18 08:17:55 +020017#include <remoteproc.h>
Jan Kiszkaddf21d52020-06-23 13:15:08 +020018
19/* Timer register set definition */
20#define RTIDWDCTRL 0x90
21#define RTIDWDPRLD 0x94
22#define RTIWDSTATUS 0x98
23#define RTIWDKEY 0x9c
24#define RTIDWDCNTR 0xa0
25#define RTIWWDRXCTRL 0xa4
26#define RTIWWDSIZECTRL 0xa8
27
28#define RTIWWDRX_NMI 0xa
29
30#define RTIWWDSIZE_50P 0x50
31
32#define WDENABLE_KEY 0xa98559da
33
34#define WDKEY_SEQ0 0xe51a
35#define WDKEY_SEQ1 0xa35c
36
37#define WDT_PRELOAD_SHIFT 13
38
39#define WDT_PRELOAD_MAX 0xfff
40
41struct rti_wdt_priv {
42 phys_addr_t regs;
Jan Kiszka7c48f892022-03-08 07:25:50 +010043 unsigned int clk_hz;
Jan Kiszkaddf21d52020-06-23 13:15:08 +020044};
45
Jan Kiszkad35cdfb2021-09-18 08:17:55 +020046#ifdef CONFIG_WDT_K3_RTI_LOAD_FW
47#define RTI_WDT_FIT_PATH "/fit-images/k3-rti-wdt-firmware"
48
49static int rti_wdt_load_fw(struct udevice *dev)
50{
51 struct udevice *rproc_dev;
52 int primary_core, ret;
53 u32 cluster_mode;
54 ofnode node;
55 u64 rti_wdt_fw;
56 u32 rti_wdt_fw_size;
57
58 node = ofnode_path(RTI_WDT_FIT_PATH);
59 if (!ofnode_valid(node))
60 goto fit_error;
61
62 ret = ofnode_read_u64(node, "load", &rti_wdt_fw);
63 if (ret)
64 goto fit_error;
65 ret = ofnode_read_u32(node, "size", &rti_wdt_fw_size);
66 if (ret)
67 goto fit_error;
68
69 node = ofnode_by_compatible(ofnode_null(), "ti,am654-r5fss");
70 if (!ofnode_valid(node))
71 goto dt_error;
72
73 ret = ofnode_read_u32(node, "ti,cluster-mode", &cluster_mode);
74 if (ret)
75 cluster_mode = 1;
76
77 node = ofnode_by_compatible(node, "ti,am654-r5f");
78 if (!ofnode_valid(node))
79 goto dt_error;
80
81 ret = uclass_get_device_by_ofnode(UCLASS_REMOTEPROC, node, &rproc_dev);
82 if (ret)
83 return ret;
84
85 primary_core = dev_seq(rproc_dev);
86
87 ret = rproc_dev_init(primary_core);
88 if (ret)
89 goto fw_error;
90
91 if (cluster_mode == 1) {
92 ret = rproc_dev_init(primary_core + 1);
93 if (ret)
94 goto fw_error;
95 }
96
97 ret = rproc_load(primary_core, (ulong)rti_wdt_fw,
98 rti_wdt_fw_size);
99 if (ret)
100 goto fw_error;
101
102 ret = rproc_start(primary_core);
103 if (ret)
104 goto fw_error;
105
106 return 0;
107
108fit_error:
109 dev_err(dev, "No loadable firmware found under %s\n", RTI_WDT_FIT_PATH);
110 return -ENOENT;
111
112dt_error:
113 dev_err(dev, "No compatible firmware target processor found\n");
114 return -ENODEV;
115
116fw_error:
117 dev_err(dev, "Failed to load watchdog firmware into remote processor %d\n",
118 primary_core);
119 return ret;
120}
121#else
122static inline int rti_wdt_load_fw(struct udevice *dev)
123{
124 return 0;
125}
126#endif
127
Jan Kiszkaddf21d52020-06-23 13:15:08 +0200128static int rti_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
129{
130 struct rti_wdt_priv *priv = dev_get_priv(dev);
131 u32 timer_margin;
132 int ret;
133
134 if (readl(priv->regs + RTIDWDCTRL) == WDENABLE_KEY)
135 return -EBUSY;
136
Jan Kiszkad35cdfb2021-09-18 08:17:55 +0200137 ret = rti_wdt_load_fw(dev);
138 if (ret < 0)
139 return ret;
140
Jan Kiszka7c48f892022-03-08 07:25:50 +0100141 timer_margin = timeout_ms * priv->clk_hz / 1000;
Jan Kiszkaddf21d52020-06-23 13:15:08 +0200142 timer_margin >>= WDT_PRELOAD_SHIFT;
143 if (timer_margin > WDT_PRELOAD_MAX)
144 timer_margin = WDT_PRELOAD_MAX;
145
146 writel(timer_margin, priv->regs + RTIDWDPRLD);
147 writel(RTIWWDRX_NMI, priv->regs + RTIWWDRXCTRL);
148 writel(RTIWWDSIZE_50P, priv->regs + RTIWWDSIZECTRL);
149
150 readl(priv->regs + RTIWWDSIZECTRL);
151
152 writel(WDENABLE_KEY, priv->regs + RTIDWDCTRL);
153
154 return 0;
155}
156
157static int rti_wdt_reset(struct udevice *dev)
158{
159 struct rti_wdt_priv *priv = dev_get_priv(dev);
160 u32 prld;
161
162 /* Make sure we do not reset too early */
163 prld = readl(priv->regs + RTIDWDPRLD) << WDT_PRELOAD_SHIFT;
164 if (readl(priv->regs + RTIDWDCNTR) >= prld / 2)
165 return -EPERM;
166
167 writel(WDKEY_SEQ0, priv->regs + RTIWDKEY);
168 writel(WDKEY_SEQ1, priv->regs + RTIWDKEY);
169
170 return 0;
171}
172
173static int rti_wdt_probe(struct udevice *dev)
174{
175 struct rti_wdt_priv *priv = dev_get_priv(dev);
176 struct clk clk;
177 int ret;
178
179 priv->regs = devfdt_get_addr(dev);
180 if (!priv->regs)
181 return -EINVAL;
182
183 ret = clk_get_by_index(dev, 0, &clk);
184 if (ret)
185 return ret;
186
Jan Kiszka7c48f892022-03-08 07:25:50 +0100187 priv->clk_hz = clk_get_rate(&clk);
188
189 /*
190 * If watchdog is running at 32k clock, it is not accurate.
191 * Adjust frequency down in this case so that it does not expire
192 * earlier than expected.
193 */
194 if (priv->clk_hz < 32768)
195 priv->clk_hz = priv->clk_hz * 9 / 10;
Jan Kiszkaddf21d52020-06-23 13:15:08 +0200196
197 return 0;
198}
199
200static const struct wdt_ops rti_wdt_ops = {
201 .start = rti_wdt_start,
202 .reset = rti_wdt_reset,
203};
204
205static const struct udevice_id rti_wdt_ids[] = {
206 { .compatible = "ti,j7-rti-wdt" },
207 { }
208};
209
210U_BOOT_DRIVER(rti_wdt) = {
211 .name = "rti_wdt",
212 .id = UCLASS_WDT,
213 .of_match = rti_wdt_ids,
214 .ops = &rti_wdt_ops,
215 .probe = rti_wdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700216 .priv_auto = sizeof(struct rti_wdt_priv),
Simon Glass12810652021-01-24 14:32:42 -0700217 .flags = DM_FLAG_LEAVE_PD_ON,
Jan Kiszkaddf21d52020-06-23 13:15:08 +0200218};