blob: 33b574b1bd3d47d3607a28bfc80830a873aa31d6 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020011#include <regmap.h>
12#include <remoteproc.h>
13#include <reset.h>
14#include <syscon.h>
15#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020018
19#define RCC_GCR_HOLD_BOOT 0
20#define RCC_GCR_RELEASE_BOOT 1
21
22/**
23 * struct stm32_copro_privdata - power processor private data
24 * @reset_ctl: reset controller handle
25 * @hold_boot_regmap: regmap for remote processor reset hold boot
26 * @hold_boot_offset: offset of the register controlling the hold boot setting
27 * @hold_boot_mask: bitmask of the register for the hold boot field
Fabien Dessenneec452e12019-10-30 14:38:33 +010028 * @rsc_table_addr: resource table address
Fabien Dessennee09846a2019-05-31 15:11:34 +020029 */
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;
Fabien Dessenneec452e12019-10-30 14:38:33 +010035 ulong rsc_table_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +020036};
37
38/**
39 * stm32_copro_probe() - Basic probe
40 * @dev: corresponding STM32 remote processor device
41 * @return 0 if all went ok, else corresponding -ve error
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 * stm32_copro_set_hold_boot() - Hold boot bit management
82 * @dev: corresponding STM32 remote processor device
83 * @hold: hold boot value
84 * @return 0 if all went ok, else corresponding -ve error
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 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
98 * To be updated when the code for this SMC service is available which
99 * is not the case for the time being.
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 * stm32_copro_device_to_virt() - Convert device address to virtual address
111 * @dev: corresponding STM32 remote processor device
112 * @da: device address
Lokesh Vutlae18166f2019-09-04 16:01:27 +0530113 * @size: Size of the memory region @da is pointing to
Fabien Dessennee09846a2019-05-31 15:11:34 +0200114 * @return converted virtual address
115 */
Lokesh Vutlae18166f2019-09-04 16:01:27 +0530116static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
117 ulong size)
Fabien Dessennee09846a2019-05-31 15:11:34 +0200118{
Lokesh Vutlae18166f2019-09-04 16:01:27 +0530119 fdt32_t in_addr = cpu_to_be32(da), end_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200120 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
Lokesh Vutlae18166f2019-09-04 16:01:27 +0530128 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
Fabien Dessennee09846a2019-05-31 15:11:34 +0200134 return phys_to_virt(paddr);
135}
136
137/**
138 * stm32_copro_load() - Loadup the STM32 remote processor
139 * @dev: corresponding STM32 remote processor device
140 * @addr: Address in memory where image is stored
141 * @size: Size in bytes of the image
142 * @return 0 if all went ok, else corresponding -ve error
143 */
144static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
145{
146 struct stm32_copro_privdata *priv;
Fabien Dessenneec452e12019-10-30 14:38:33 +0100147 ulong rsc_table_size;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200148 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
Fabien Dessenneec452e12019-10-30 14:38:33 +0100162 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
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530168 return rproc_elf32_load_image(dev, addr, size);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200169}
170
171/**
172 * stm32_copro_start() - Start the STM32 remote processor
173 * @dev: corresponding STM32 remote processor device
174 * @return 0 if all went ok, else corresponding -ve error
175 */
176static int stm32_copro_start(struct udevice *dev)
177{
Fabien Dessenneec452e12019-10-30 14:38:33 +0100178 struct stm32_copro_privdata *priv;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200179 int ret;
180
Fabien Dessenneec452e12019-10-30 14:38:33 +0100181 priv = dev_get_priv(dev);
182
Fabien Dessennee09846a2019-05-31 15:11:34 +0200183 /* move hold boot from true to false start the copro */
184 ret = stm32_copro_set_hold_boot(dev, false);
185 if (ret)
186 return ret;
187
188 /*
189 * Once copro running, reset hold boot flag to avoid copro
190 * rebooting autonomously
191 */
192 ret = stm32_copro_set_hold_boot(dev, true);
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100193 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
194 TAMP_COPRO_STATE);
Fabien Dessenneec452e12019-10-30 14:38:33 +0100195 if (!ret)
196 /* Store rsc_address in bkp register */
197 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
198
Fabien Dessennee09846a2019-05-31 15:11:34 +0200199 return ret;
200}
201
202/**
203 * stm32_copro_reset() - Reset the STM32 remote processor
204 * @dev: corresponding STM32 remote processor device
205 * @return 0 if all went ok, else corresponding -ve error
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
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100224 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200225
226 return 0;
227}
228
229/**
230 * stm32_copro_stop() - Stop the STM32 remote processor
231 * @dev: corresponding STM32 remote processor device
232 * @return 0 if all went ok, else corresponding -ve error
233 */
234static int stm32_copro_stop(struct udevice *dev)
235{
236 return stm32_copro_reset(dev);
237}
238
239/**
240 * stm32_copro_is_running() - Is the STM32 remote processor running
241 * @dev: corresponding STM32 remote processor device
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100242 * @return 0 if the remote processor is running, 1 otherwise
Fabien Dessennee09846a2019-05-31 15:11:34 +0200243 */
244static int stm32_copro_is_running(struct udevice *dev)
245{
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100246 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200247}
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[] = {
Patrick Delaunay26c24b42019-08-02 15:07:18 +0200259 {.compatible = "st,stm32mp1-m4"},
Fabien Dessennee09846a2019-05-31 15:11:34 +0200260 {}
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};