1
2
3
4
5#define pr_fmt(fmt) "%s: " fmt, __func__
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
10#include <log.h>
11#include <regmap.h>
12#include <remoteproc.h>
13#include <reset.h>
14#include <syscon.h>
15#include <asm/io.h>
16#include <dm/device_compat.h>
17#include <linux/err.h>
18
19#define RCC_GCR_HOLD_BOOT 0
20#define RCC_GCR_RELEASE_BOOT 1
21
22
23
24
25
26
27
28
29
30struct stm32_copro_privdata {
31 struct reset_ctl reset_ctl;
32 struct regmap *hold_boot_regmap;
33 uint hold_boot_offset;
34 uint hold_boot_mask;
35 ulong rsc_table_addr;
36};
37
38
39
40
41
42
43static int stm32_copro_probe(struct udevice *dev)
44{
45 struct stm32_copro_privdata *priv;
46 struct regmap *regmap;
47 const fdt32_t *cell;
48 int len, ret;
49
50 priv = dev_get_priv(dev);
51
52 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
53 if (IS_ERR(regmap)) {
54 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
55 PTR_ERR(regmap));
56 return PTR_ERR(regmap);
57 }
58
59 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
60 if (len < 3 * sizeof(fdt32_t)) {
61 dev_err(dev, "holdboot offset and mask not available\n");
62 return -EINVAL;
63 }
64
65 priv->hold_boot_regmap = regmap;
66 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
67 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
68
69 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
70 if (ret) {
71 dev_err(dev, "failed to get reset (%d)\n", ret);
72 return ret;
73 }
74
75 dev_dbg(dev, "probed\n");
76
77 return 0;
78}
79
80
81
82
83
84
85
86static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
87{
88 struct stm32_copro_privdata *priv;
89 uint val;
90 int ret;
91
92 priv = dev_get_priv(dev);
93
94 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
95
96
97
98
99
100
101 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
102 priv->hold_boot_mask, val);
103 if (ret)
104 dev_err(dev, "failed to set hold boot\n");
105
106 return ret;
107}
108
109
110
111
112
113
114
115
116static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
117 ulong size)
118{
119 fdt32_t in_addr = cpu_to_be32(da), end_addr;
120 u64 paddr;
121
122 paddr = dev_translate_dma_address(dev, &in_addr);
123 if (paddr == OF_BAD_ADDR) {
124 dev_err(dev, "Unable to convert address %ld\n", da);
125 return NULL;
126 }
127
128 end_addr = cpu_to_be32(da + size - 1);
129 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
130 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
131 return NULL;
132 }
133
134 return phys_to_virt(paddr);
135}
136
137
138
139
140
141
142
143
144static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
145{
146 struct stm32_copro_privdata *priv;
147 ulong rsc_table_size;
148 int ret;
149
150 priv = dev_get_priv(dev);
151
152 ret = stm32_copro_set_hold_boot(dev, true);
153 if (ret)
154 return ret;
155
156 ret = reset_assert(&priv->reset_ctl);
157 if (ret) {
158 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
159 return ret;
160 }
161
162 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
163 &rsc_table_size)) {
164 priv->rsc_table_addr = 0;
165 dev_warn(dev, "No valid resource table for this firmware\n");
166 }
167
168 return rproc_elf32_load_image(dev, addr, size);
169}
170
171
172
173
174
175
176static int stm32_copro_start(struct udevice *dev)
177{
178 struct stm32_copro_privdata *priv;
179 int ret;
180
181 priv = dev_get_priv(dev);
182
183
184 ret = stm32_copro_set_hold_boot(dev, false);
185 if (ret)
186 return ret;
187
188
189
190
191
192 ret = stm32_copro_set_hold_boot(dev, true);
193 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
194 TAMP_COPRO_STATE);
195 if (!ret)
196
197 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
198
199 return ret;
200}
201
202
203
204
205
206
207static int stm32_copro_reset(struct udevice *dev)
208{
209 struct stm32_copro_privdata *priv;
210 int ret;
211
212 priv = dev_get_priv(dev);
213
214 ret = stm32_copro_set_hold_boot(dev, true);
215 if (ret)
216 return ret;
217
218 ret = reset_assert(&priv->reset_ctl);
219 if (ret) {
220 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
221 return ret;
222 }
223
224 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
225
226 return 0;
227}
228
229
230
231
232
233
234static int stm32_copro_stop(struct udevice *dev)
235{
236 return stm32_copro_reset(dev);
237}
238
239
240
241
242
243
244static int stm32_copro_is_running(struct udevice *dev)
245{
246 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
247}
248
249static const struct dm_rproc_ops stm32_copro_ops = {
250 .load = stm32_copro_load,
251 .start = stm32_copro_start,
252 .stop = stm32_copro_stop,
253 .reset = stm32_copro_reset,
254 .is_running = stm32_copro_is_running,
255 .device_to_virt = stm32_copro_device_to_virt,
256};
257
258static const struct udevice_id stm32_copro_ids[] = {
259 {.compatible = "st,stm32mp1-m4"},
260 {}
261};
262
263U_BOOT_DRIVER(stm32_copro) = {
264 .name = "stm32_m4_proc",
265 .of_match = stm32_copro_ids,
266 .id = UCLASS_REMOTEPROC,
267 .ops = &stm32_copro_ops,
268 .probe = stm32_copro_probe,
269 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),
270};
271