ARM: dts: at91: sama5d2_icp: fix i2c eeprom compatible
[platform/kernel/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 <log.h>
11 #include <regmap.h>
12 #include <remoteproc.h>
13 #include <reset.h>
14 #include <syscon.h>
15 #include <asm/io.h>
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
18
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
28  * @rsc_table_addr:     resource table address
29  */
30 struct 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;
35         ulong rsc_table_addr;
36 };
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  */
43 static 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  */
86 static 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
113  * @size:       Size of the memory region @da is pointing to
114  * @return converted virtual address
115  */
116 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
117                                         ulong size)
118 {
119         fdt32_t in_addr = cpu_to_be32(da), end_addr;
120         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
128         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
134         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  */
144 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
145 {
146         struct stm32_copro_privdata *priv;
147         ulong rsc_table_size;
148         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
162         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
168         return rproc_elf32_load_image(dev, addr, size);
169 }
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  */
176 static int stm32_copro_start(struct udevice *dev)
177 {
178         struct stm32_copro_privdata *priv;
179         int ret;
180
181         priv = dev_get_priv(dev);
182
183         /* 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);
193         writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
194                TAMP_COPRO_STATE);
195         if (!ret)
196                 /* Store rsc_address in bkp register */
197                 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
198
199         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  */
207 static 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
224         writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
225
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  */
234 static 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
242  * @return 0 if the remote processor is running, 1 otherwise
243  */
244 static int stm32_copro_is_running(struct udevice *dev)
245 {
246         return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
247 }
248
249 static 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
258 static const struct udevice_id stm32_copro_ids[] = {
259         {.compatible = "st,stm32mp1-m4"},
260         {}
261 };
262
263 U_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 };