blob: e9dce0d173a14f559910f0d9f47880c8f1510b46 [file] [log] [blame]
Fabien Dessenne6bed04f2019-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>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Fabien Dessenne6bed04f2019-05-31 15:11:34 +020017
18#define RCC_GCR_HOLD_BOOT 0
19#define RCC_GCR_RELEASE_BOOT 1
20
21/**
22 * struct stm32_copro_privdata - power processor private data
23 * @reset_ctl: reset controller handle
24 * @hold_boot_regmap: regmap for remote processor reset hold boot
25 * @hold_boot_offset: offset of the register controlling the hold boot setting
26 * @hold_boot_mask: bitmask of the register for the hold boot field
Fabien Dessenne33fd4192019-10-30 14:38:33 +010027 * @rsc_table_addr: resource table address
Fabien Dessenne6bed04f2019-05-31 15:11:34 +020028 */
29struct stm32_copro_privdata {
30 struct reset_ctl reset_ctl;
31 struct regmap *hold_boot_regmap;
32 uint hold_boot_offset;
33 uint hold_boot_mask;
Fabien Dessenne33fd4192019-10-30 14:38:33 +010034 ulong rsc_table_addr;
Fabien Dessenne6bed04f2019-05-31 15:11:34 +020035};
36
37/**
38 * stm32_copro_probe() - Basic probe
39 * @dev: corresponding STM32 remote processor device
40 * @return 0 if all went ok, else corresponding -ve error
41 */
42static int stm32_copro_probe(struct udevice *dev)
43{
44 struct stm32_copro_privdata *priv;
45 struct regmap *regmap;
46 const fdt32_t *cell;
47 int len, ret;
48
49 priv = dev_get_priv(dev);
50
51 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
52 if (IS_ERR(regmap)) {
53 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
54 PTR_ERR(regmap));
55 return PTR_ERR(regmap);
56 }
57
58 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
59 if (len < 3 * sizeof(fdt32_t)) {
60 dev_err(dev, "holdboot offset and mask not available\n");
61 return -EINVAL;
62 }
63
64 priv->hold_boot_regmap = regmap;
65 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
66 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
67
68 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
69 if (ret) {
70 dev_err(dev, "failed to get reset (%d)\n", ret);
71 return ret;
72 }
73
74 dev_dbg(dev, "probed\n");
75
76 return 0;
77}
78
79/**
80 * stm32_copro_set_hold_boot() - Hold boot bit management
81 * @dev: corresponding STM32 remote processor device
82 * @hold: hold boot value
83 * @return 0 if all went ok, else corresponding -ve error
84 */
85static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
86{
87 struct stm32_copro_privdata *priv;
88 uint val;
89 int ret;
90
91 priv = dev_get_priv(dev);
92
93 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
94
95 /*
96 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
97 * To be updated when the code for this SMC service is available which
98 * is not the case for the time being.
99 */
100 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
101 priv->hold_boot_mask, val);
102 if (ret)
103 dev_err(dev, "failed to set hold boot\n");
104
105 return ret;
106}
107
108/**
109 * stm32_copro_device_to_virt() - Convert device address to virtual address
110 * @dev: corresponding STM32 remote processor device
111 * @da: device address
Lokesh Vutlac08eb932019-09-04 16:01:27 +0530112 * @size: Size of the memory region @da is pointing to
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200113 * @return converted virtual address
114 */
Lokesh Vutlac08eb932019-09-04 16:01:27 +0530115static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
116 ulong size)
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200117{
Lokesh Vutlac08eb932019-09-04 16:01:27 +0530118 fdt32_t in_addr = cpu_to_be32(da), end_addr;
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200119 u64 paddr;
120
121 paddr = dev_translate_dma_address(dev, &in_addr);
122 if (paddr == OF_BAD_ADDR) {
123 dev_err(dev, "Unable to convert address %ld\n", da);
124 return NULL;
125 }
126
Lokesh Vutlac08eb932019-09-04 16:01:27 +0530127 end_addr = cpu_to_be32(da + size - 1);
128 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
129 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
130 return NULL;
131 }
132
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200133 return phys_to_virt(paddr);
134}
135
136/**
137 * stm32_copro_load() - Loadup the STM32 remote processor
138 * @dev: corresponding STM32 remote processor device
139 * @addr: Address in memory where image is stored
140 * @size: Size in bytes of the image
141 * @return 0 if all went ok, else corresponding -ve error
142 */
143static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
144{
145 struct stm32_copro_privdata *priv;
Fabien Dessenne33fd4192019-10-30 14:38:33 +0100146 ulong rsc_table_size;
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200147 int ret;
148
149 priv = dev_get_priv(dev);
150
151 ret = stm32_copro_set_hold_boot(dev, true);
152 if (ret)
153 return ret;
154
155 ret = reset_assert(&priv->reset_ctl);
156 if (ret) {
157 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
158 return ret;
159 }
160
Fabien Dessenne33fd4192019-10-30 14:38:33 +0100161 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
162 &rsc_table_size)) {
163 priv->rsc_table_addr = 0;
164 dev_warn(dev, "No valid resource table for this firmware\n");
165 }
166
Lokesh Vutla14d963d2019-09-04 16:01:28 +0530167 return rproc_elf32_load_image(dev, addr, size);
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200168}
169
170/**
171 * stm32_copro_start() - Start the STM32 remote processor
172 * @dev: corresponding STM32 remote processor device
173 * @return 0 if all went ok, else corresponding -ve error
174 */
175static int stm32_copro_start(struct udevice *dev)
176{
Fabien Dessenne33fd4192019-10-30 14:38:33 +0100177 struct stm32_copro_privdata *priv;
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200178 int ret;
179
Fabien Dessenne33fd4192019-10-30 14:38:33 +0100180 priv = dev_get_priv(dev);
181
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200182 /* move hold boot from true to false start the copro */
183 ret = stm32_copro_set_hold_boot(dev, false);
184 if (ret)
185 return ret;
186
187 /*
188 * Once copro running, reset hold boot flag to avoid copro
189 * rebooting autonomously
190 */
191 ret = stm32_copro_set_hold_boot(dev, true);
Fabien Dessenne4a4deb82019-10-30 14:38:31 +0100192 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
193 TAMP_COPRO_STATE);
Fabien Dessenne33fd4192019-10-30 14:38:33 +0100194 if (!ret)
195 /* Store rsc_address in bkp register */
196 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
197
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200198 return ret;
199}
200
201/**
202 * stm32_copro_reset() - Reset the STM32 remote processor
203 * @dev: corresponding STM32 remote processor device
204 * @return 0 if all went ok, else corresponding -ve error
205 */
206static int stm32_copro_reset(struct udevice *dev)
207{
208 struct stm32_copro_privdata *priv;
209 int ret;
210
211 priv = dev_get_priv(dev);
212
213 ret = stm32_copro_set_hold_boot(dev, true);
214 if (ret)
215 return ret;
216
217 ret = reset_assert(&priv->reset_ctl);
218 if (ret) {
219 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
220 return ret;
221 }
222
Fabien Dessenne4a4deb82019-10-30 14:38:31 +0100223 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200224
225 return 0;
226}
227
228/**
229 * stm32_copro_stop() - Stop the STM32 remote processor
230 * @dev: corresponding STM32 remote processor device
231 * @return 0 if all went ok, else corresponding -ve error
232 */
233static int stm32_copro_stop(struct udevice *dev)
234{
235 return stm32_copro_reset(dev);
236}
237
238/**
239 * stm32_copro_is_running() - Is the STM32 remote processor running
240 * @dev: corresponding STM32 remote processor device
Fabien Dessenne4a4deb82019-10-30 14:38:31 +0100241 * @return 0 if the remote processor is running, 1 otherwise
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200242 */
243static int stm32_copro_is_running(struct udevice *dev)
244{
Fabien Dessenne4a4deb82019-10-30 14:38:31 +0100245 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200246}
247
248static const struct dm_rproc_ops stm32_copro_ops = {
249 .load = stm32_copro_load,
250 .start = stm32_copro_start,
251 .stop = stm32_copro_stop,
252 .reset = stm32_copro_reset,
253 .is_running = stm32_copro_is_running,
254 .device_to_virt = stm32_copro_device_to_virt,
255};
256
257static const struct udevice_id stm32_copro_ids[] = {
Patrick Delaunay5d2901a2019-08-02 15:07:18 +0200258 {.compatible = "st,stm32mp1-m4"},
Fabien Dessenne6bed04f2019-05-31 15:11:34 +0200259 {}
260};
261
262U_BOOT_DRIVER(stm32_copro) = {
263 .name = "stm32_m4_proc",
264 .of_match = stm32_copro_ids,
265 .id = UCLASS_REMOTEPROC,
266 .ops = &stm32_copro_ops,
267 .probe = stm32_copro_probe,
268 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),
269};