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