Merge tag 'dm-pull-6feb20' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[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 #include <dm/device_compat.h>
16 #include <linux/err.h>
17
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
27  * @rsc_table_addr:     resource table address
28  */
29 struct 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;
34         ulong rsc_table_addr;
35 };
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  */
42 static 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  */
85 static 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
112  * @size:       Size of the memory region @da is pointing to
113  * @return converted virtual address
114  */
115 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
116                                         ulong size)
117 {
118         fdt32_t in_addr = cpu_to_be32(da), end_addr;
119         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
127         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
133         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  */
143 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
144 {
145         struct stm32_copro_privdata *priv;
146         ulong rsc_table_size;
147         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
161         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
167         return rproc_elf32_load_image(dev, addr, size);
168 }
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  */
175 static int stm32_copro_start(struct udevice *dev)
176 {
177         struct stm32_copro_privdata *priv;
178         int ret;
179
180         priv = dev_get_priv(dev);
181
182         /* 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);
192         writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
193                TAMP_COPRO_STATE);
194         if (!ret)
195                 /* Store rsc_address in bkp register */
196                 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
197
198         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  */
206 static 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
223         writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
224
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  */
233 static 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
241  * @return 0 if the remote processor is running, 1 otherwise
242  */
243 static int stm32_copro_is_running(struct udevice *dev)
244 {
245         return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
246 }
247
248 static 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
257 static const struct udevice_id stm32_copro_ids[] = {
258         {.compatible = "st,stm32mp1-m4"},
259         {}
260 };
261
262 U_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 };