blob: ad941f67e866dbb92cb8b9aed6224150a3bad64b [file] [log] [blame]
Fabien Dessennee09846a2019-05-31 15:11:34 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
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 <regmap.h>
11#include <remoteproc.h>
12#include <reset.h>
13#include <syscon.h>
14#include <asm/io.h>
15
16#define RCC_GCR_HOLD_BOOT 0
17#define RCC_GCR_RELEASE_BOOT 1
18
19/**
20 * struct stm32_copro_privdata - power processor private data
21 * @reset_ctl: reset controller handle
22 * @hold_boot_regmap: regmap for remote processor reset hold boot
23 * @hold_boot_offset: offset of the register controlling the hold boot setting
24 * @hold_boot_mask: bitmask of the register for the hold boot field
25 * @is_running: is the remote processor running
26 */
27struct stm32_copro_privdata {
28 struct reset_ctl reset_ctl;
29 struct regmap *hold_boot_regmap;
30 uint hold_boot_offset;
31 uint hold_boot_mask;
32 bool is_running;
33};
34
35/**
36 * stm32_copro_probe() - Basic probe
37 * @dev: corresponding STM32 remote processor device
38 * @return 0 if all went ok, else corresponding -ve error
39 */
40static int stm32_copro_probe(struct udevice *dev)
41{
42 struct stm32_copro_privdata *priv;
43 struct regmap *regmap;
44 const fdt32_t *cell;
45 int len, ret;
46
47 priv = dev_get_priv(dev);
48
49 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
50 if (IS_ERR(regmap)) {
51 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
52 PTR_ERR(regmap));
53 return PTR_ERR(regmap);
54 }
55
56 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
57 if (len < 3 * sizeof(fdt32_t)) {
58 dev_err(dev, "holdboot offset and mask not available\n");
59 return -EINVAL;
60 }
61
62 priv->hold_boot_regmap = regmap;
63 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
64 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
65
66 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
67 if (ret) {
68 dev_err(dev, "failed to get reset (%d)\n", ret);
69 return ret;
70 }
71
72 dev_dbg(dev, "probed\n");
73
74 return 0;
75}
76
77/**
78 * stm32_copro_set_hold_boot() - Hold boot bit management
79 * @dev: corresponding STM32 remote processor device
80 * @hold: hold boot value
81 * @return 0 if all went ok, else corresponding -ve error
82 */
83static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
84{
85 struct stm32_copro_privdata *priv;
86 uint val;
87 int ret;
88
89 priv = dev_get_priv(dev);
90
91 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
92
93 /*
94 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
95 * To be updated when the code for this SMC service is available which
96 * is not the case for the time being.
97 */
98 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
99 priv->hold_boot_mask, val);
100 if (ret)
101 dev_err(dev, "failed to set hold boot\n");
102
103 return ret;
104}
105
106/**
107 * stm32_copro_device_to_virt() - Convert device address to virtual address
108 * @dev: corresponding STM32 remote processor device
109 * @da: device address
110 * @return converted virtual address
111 */
112static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da)
113{
114 fdt32_t in_addr = cpu_to_be32(da);
115 u64 paddr;
116
117 paddr = dev_translate_dma_address(dev, &in_addr);
118 if (paddr == OF_BAD_ADDR) {
119 dev_err(dev, "Unable to convert address %ld\n", da);
120 return NULL;
121 }
122
123 return phys_to_virt(paddr);
124}
125
126/**
127 * stm32_copro_load() - Loadup the STM32 remote processor
128 * @dev: corresponding STM32 remote processor device
129 * @addr: Address in memory where image is stored
130 * @size: Size in bytes of the image
131 * @return 0 if all went ok, else corresponding -ve error
132 */
133static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
134{
135 struct stm32_copro_privdata *priv;
136 int ret;
137
138 priv = dev_get_priv(dev);
139
140 ret = stm32_copro_set_hold_boot(dev, true);
141 if (ret)
142 return ret;
143
144 ret = reset_assert(&priv->reset_ctl);
145 if (ret) {
146 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
147 return ret;
148 }
149
150 /* Support only ELF32 image */
151 ret = rproc_elf32_sanity_check(addr, size);
152 if (ret) {
153 dev_err(dev, "Invalid ELF32 image (%d)\n", ret);
154 return ret;
155 }
156
157 return rproc_elf32_load_image(dev, addr);
158}
159
160/**
161 * stm32_copro_start() - Start the STM32 remote processor
162 * @dev: corresponding STM32 remote processor device
163 * @return 0 if all went ok, else corresponding -ve error
164 */
165static int stm32_copro_start(struct udevice *dev)
166{
167 struct stm32_copro_privdata *priv;
168 int ret;
169
170 priv = dev_get_priv(dev);
171
172 /* move hold boot from true to false start the copro */
173 ret = stm32_copro_set_hold_boot(dev, false);
174 if (ret)
175 return ret;
176
177 /*
178 * Once copro running, reset hold boot flag to avoid copro
179 * rebooting autonomously
180 */
181 ret = stm32_copro_set_hold_boot(dev, true);
182 priv->is_running = !ret;
183 return ret;
184}
185
186/**
187 * stm32_copro_reset() - Reset the STM32 remote processor
188 * @dev: corresponding STM32 remote processor device
189 * @return 0 if all went ok, else corresponding -ve error
190 */
191static int stm32_copro_reset(struct udevice *dev)
192{
193 struct stm32_copro_privdata *priv;
194 int ret;
195
196 priv = dev_get_priv(dev);
197
198 ret = stm32_copro_set_hold_boot(dev, true);
199 if (ret)
200 return ret;
201
202 ret = reset_assert(&priv->reset_ctl);
203 if (ret) {
204 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
205 return ret;
206 }
207
208 priv->is_running = false;
209
210 return 0;
211}
212
213/**
214 * stm32_copro_stop() - Stop the STM32 remote processor
215 * @dev: corresponding STM32 remote processor device
216 * @return 0 if all went ok, else corresponding -ve error
217 */
218static int stm32_copro_stop(struct udevice *dev)
219{
220 return stm32_copro_reset(dev);
221}
222
223/**
224 * stm32_copro_is_running() - Is the STM32 remote processor running
225 * @dev: corresponding STM32 remote processor device
226 * @return 1 if the remote processor is running, 0 otherwise
227 */
228static int stm32_copro_is_running(struct udevice *dev)
229{
230 struct stm32_copro_privdata *priv;
231
232 priv = dev_get_priv(dev);
233 return priv->is_running;
234}
235
236static const struct dm_rproc_ops stm32_copro_ops = {
237 .load = stm32_copro_load,
238 .start = stm32_copro_start,
239 .stop = stm32_copro_stop,
240 .reset = stm32_copro_reset,
241 .is_running = stm32_copro_is_running,
242 .device_to_virt = stm32_copro_device_to_virt,
243};
244
245static const struct udevice_id stm32_copro_ids[] = {
Patrick Delaunay26c24b42019-08-02 15:07:18 +0200246 {.compatible = "st,stm32mp1-m4"},
Fabien Dessennee09846a2019-05-31 15:11:34 +0200247 {}
248};
249
250U_BOOT_DRIVER(stm32_copro) = {
251 .name = "stm32_m4_proc",
252 .of_match = stm32_copro_ids,
253 .id = UCLASS_REMOTEPROC,
254 .ops = &stm32_copro_ops,
255 .probe = stm32_copro_probe,
256 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),
257};