blob: 986f34a336a76f5d18494750e02c412b99e430d0 [file] [log] [blame]
Fabien Dessenne13140de2019-05-14 10:26:58 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Authors: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics.
5 * Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
6 */
7
8#include <linux/arm-smccc.h>
9#include <linux/dma-mapping.h>
10#include <linux/interrupt.h>
11#include <linux/io.h>
12#include <linux/mailbox_client.h>
13#include <linux/mfd/syscon.h>
14#include <linux/module.h>
15#include <linux/of_address.h>
16#include <linux/of_device.h>
17#include <linux/of_reserved_mem.h>
Fabien Dessenne410119e2019-08-26 17:38:43 +020018#include <linux/pm_wakeirq.h>
Fabien Dessenne13140de2019-05-14 10:26:58 +020019#include <linux/regmap.h>
20#include <linux/remoteproc.h>
21#include <linux/reset.h>
Michael S. Tsirkinbf02bd92020-04-09 17:09:35 -040022#include <linux/slab.h>
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +020023#include <linux/workqueue.h>
Fabien Dessenne13140de2019-05-14 10:26:58 +020024
25#include "remoteproc_internal.h"
26
27#define HOLD_BOOT 0
28#define RELEASE_BOOT 1
29
30#define MBOX_NB_VQ 2
31#define MBOX_NB_MBX 3
32
33#define STM32_SMC_RCC 0x82001000
34#define STM32_SMC_REG_WRITE 0x1
35
36#define STM32_MBX_VQ0 "vq0"
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +020037#define STM32_MBX_VQ0_ID 0
Fabien Dessenne13140de2019-05-14 10:26:58 +020038#define STM32_MBX_VQ1 "vq1"
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +020039#define STM32_MBX_VQ1_ID 1
Fabien Dessenne13140de2019-05-14 10:26:58 +020040#define STM32_MBX_SHUTDOWN "shutdown"
41
42struct stm32_syscon {
43 struct regmap *map;
44 u32 reg;
45 u32 mask;
46};
47
48struct stm32_rproc_mem {
49 char name[20];
50 void __iomem *cpu_addr;
51 phys_addr_t bus_addr;
52 u32 dev_addr;
53 size_t size;
54};
55
56struct stm32_rproc_mem_ranges {
57 u32 dev_addr;
58 u32 bus_addr;
59 u32 size;
60};
61
62struct stm32_mbox {
63 const unsigned char name[10];
64 struct mbox_chan *chan;
65 struct mbox_client client;
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +020066 struct work_struct vq_work;
Fabien Dessenne13140de2019-05-14 10:26:58 +020067 int vq_id;
68};
69
70struct stm32_rproc {
71 struct reset_control *rst;
72 struct stm32_syscon hold_boot;
73 struct stm32_syscon pdds;
Fabien Dessenne410119e2019-08-26 17:38:43 +020074 int wdg_irq;
Fabien Dessenne13140de2019-05-14 10:26:58 +020075 u32 nb_rmems;
76 struct stm32_rproc_mem *rmems;
77 struct stm32_mbox mb[MBOX_NB_MBX];
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +020078 struct workqueue_struct *workqueue;
Fabien Dessenne13140de2019-05-14 10:26:58 +020079 bool secured_soc;
80};
81
82static int stm32_rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da)
83{
84 unsigned int i;
85 struct stm32_rproc *ddata = rproc->priv;
86 struct stm32_rproc_mem *p_mem;
87
88 for (i = 0; i < ddata->nb_rmems; i++) {
89 p_mem = &ddata->rmems[i];
90
91 if (pa < p_mem->bus_addr ||
92 pa >= p_mem->bus_addr + p_mem->size)
93 continue;
94 *da = pa - p_mem->bus_addr + p_mem->dev_addr;
95 dev_dbg(rproc->dev.parent, "pa %pa to da %llx\n", &pa, *da);
96 return 0;
97 }
98
99 return -EINVAL;
100}
101
102static int stm32_rproc_mem_alloc(struct rproc *rproc,
103 struct rproc_mem_entry *mem)
104{
105 struct device *dev = rproc->dev.parent;
106 void *va;
107
108 dev_dbg(dev, "map memory: %pa+%x\n", &mem->dma, mem->len);
109 va = ioremap_wc(mem->dma, mem->len);
110 if (IS_ERR_OR_NULL(va)) {
111 dev_err(dev, "Unable to map memory region: %pa+%x\n",
112 &mem->dma, mem->len);
113 return -ENOMEM;
114 }
115
116 /* Update memory entry va */
117 mem->va = va;
118
119 return 0;
120}
121
122static int stm32_rproc_mem_release(struct rproc *rproc,
123 struct rproc_mem_entry *mem)
124{
125 dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma);
126 iounmap(mem->va);
127
128 return 0;
129}
130
Mathieu Poirier7b9f18c2020-07-14 14:04:35 -0600131static int stm32_rproc_of_memory_translations(struct platform_device *pdev,
132 struct stm32_rproc *ddata)
Fabien Dessenne13140de2019-05-14 10:26:58 +0200133{
Mathieu Poirier7b9f18c2020-07-14 14:04:35 -0600134 struct device *parent, *dev = &pdev->dev;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200135 struct device_node *np;
136 struct stm32_rproc_mem *p_mems;
137 struct stm32_rproc_mem_ranges *mem_range;
138 int cnt, array_size, i, ret = 0;
139
140 parent = dev->parent;
141 np = parent->of_node;
142
143 cnt = of_property_count_elems_of_size(np, "dma-ranges",
144 sizeof(*mem_range));
145 if (cnt <= 0) {
146 dev_err(dev, "%s: dma-ranges property not defined\n", __func__);
147 return -EINVAL;
148 }
149
150 p_mems = devm_kcalloc(dev, cnt, sizeof(*p_mems), GFP_KERNEL);
151 if (!p_mems)
152 return -ENOMEM;
153 mem_range = kcalloc(cnt, sizeof(*mem_range), GFP_KERNEL);
154 if (!mem_range)
155 return -ENOMEM;
156
157 array_size = cnt * sizeof(struct stm32_rproc_mem_ranges) / sizeof(u32);
158
159 ret = of_property_read_u32_array(np, "dma-ranges",
160 (u32 *)mem_range, array_size);
161 if (ret) {
162 dev_err(dev, "error while get dma-ranges property: %x\n", ret);
163 goto free_mem;
164 }
165
166 for (i = 0; i < cnt; i++) {
167 p_mems[i].bus_addr = mem_range[i].bus_addr;
168 p_mems[i].dev_addr = mem_range[i].dev_addr;
169 p_mems[i].size = mem_range[i].size;
170
171 dev_dbg(dev, "memory range[%i]: da %#x, pa %pa, size %#zx:\n",
172 i, p_mems[i].dev_addr, &p_mems[i].bus_addr,
173 p_mems[i].size);
174 }
175
176 ddata->rmems = p_mems;
177 ddata->nb_rmems = cnt;
178
179free_mem:
180 kfree(mem_range);
181 return ret;
182}
183
184static int stm32_rproc_mbox_idx(struct rproc *rproc, const unsigned char *name)
185{
186 struct stm32_rproc *ddata = rproc->priv;
187 int i;
188
189 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) {
190 if (!strncmp(ddata->mb[i].name, name, strlen(name)))
191 return i;
192 }
193 dev_err(&rproc->dev, "mailbox %s not found\n", name);
194
195 return -EINVAL;
196}
197
198static int stm32_rproc_elf_load_rsc_table(struct rproc *rproc,
199 const struct firmware *fw)
200{
201 if (rproc_elf_load_rsc_table(rproc, fw))
202 dev_warn(&rproc->dev, "no resource table found for this firmware\n");
203
204 return 0;
205}
206
207static int stm32_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
208{
209 struct device *dev = rproc->dev.parent;
210 struct device_node *np = dev->of_node;
211 struct of_phandle_iterator it;
212 struct rproc_mem_entry *mem;
213 struct reserved_mem *rmem;
214 u64 da;
215 int index = 0;
216
217 /* Register associated reserved memory regions */
218 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
219 while (of_phandle_iterator_next(&it) == 0) {
220 rmem = of_reserved_mem_lookup(it.node);
221 if (!rmem) {
222 dev_err(dev, "unable to acquire memory-region\n");
223 return -EINVAL;
224 }
225
226 if (stm32_rproc_pa_to_da(rproc, rmem->base, &da) < 0) {
227 dev_err(dev, "memory region not valid %pa\n",
228 &rmem->base);
229 return -EINVAL;
230 }
231
232 /* No need to map vdev buffer */
233 if (strcmp(it.node->name, "vdev0buffer")) {
234 /* Register memory region */
235 mem = rproc_mem_entry_init(dev, NULL,
236 (dma_addr_t)rmem->base,
237 rmem->size, da,
238 stm32_rproc_mem_alloc,
239 stm32_rproc_mem_release,
240 it.node->name);
241
242 if (mem)
243 rproc_coredump_add_segment(rproc, da,
244 rmem->size);
245 } else {
246 /* Register reserved memory for vdev buffer alloc */
247 mem = rproc_of_resm_mem_entry_init(dev, index,
248 rmem->size,
249 rmem->base,
250 it.node->name);
251 }
252
253 if (!mem)
254 return -ENOMEM;
255
256 rproc_add_carveout(rproc, mem);
257 index++;
258 }
259
260 return stm32_rproc_elf_load_rsc_table(rproc, fw);
261}
262
263static irqreturn_t stm32_rproc_wdg(int irq, void *data)
264{
Mathieu Poirierb8631ab2020-07-14 14:04:36 -0600265 struct platform_device *pdev = data;
266 struct rproc *rproc = platform_get_drvdata(pdev);
Fabien Dessenne13140de2019-05-14 10:26:58 +0200267
268 rproc_report_crash(rproc, RPROC_WATCHDOG);
269
270 return IRQ_HANDLED;
271}
272
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200273static void stm32_rproc_mb_vq_work(struct work_struct *work)
274{
275 struct stm32_mbox *mb = container_of(work, struct stm32_mbox, vq_work);
276 struct rproc *rproc = dev_get_drvdata(mb->client.dev);
277
278 if (rproc_vq_interrupt(rproc, mb->vq_id) == IRQ_NONE)
279 dev_dbg(&rproc->dev, "no message found in vq%d\n", mb->vq_id);
280}
281
Fabien Dessenne13140de2019-05-14 10:26:58 +0200282static void stm32_rproc_mb_callback(struct mbox_client *cl, void *data)
283{
284 struct rproc *rproc = dev_get_drvdata(cl->dev);
285 struct stm32_mbox *mb = container_of(cl, struct stm32_mbox, client);
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200286 struct stm32_rproc *ddata = rproc->priv;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200287
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200288 queue_work(ddata->workqueue, &mb->vq_work);
Fabien Dessenne13140de2019-05-14 10:26:58 +0200289}
290
291static void stm32_rproc_free_mbox(struct rproc *rproc)
292{
293 struct stm32_rproc *ddata = rproc->priv;
294 unsigned int i;
295
296 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) {
297 if (ddata->mb[i].chan)
298 mbox_free_channel(ddata->mb[i].chan);
299 ddata->mb[i].chan = NULL;
300 }
301}
302
303static const struct stm32_mbox stm32_rproc_mbox[MBOX_NB_MBX] = {
304 {
305 .name = STM32_MBX_VQ0,
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200306 .vq_id = STM32_MBX_VQ0_ID,
Fabien Dessenne13140de2019-05-14 10:26:58 +0200307 .client = {
308 .rx_callback = stm32_rproc_mb_callback,
309 .tx_block = false,
310 },
311 },
312 {
313 .name = STM32_MBX_VQ1,
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200314 .vq_id = STM32_MBX_VQ1_ID,
Fabien Dessenne13140de2019-05-14 10:26:58 +0200315 .client = {
316 .rx_callback = stm32_rproc_mb_callback,
317 .tx_block = false,
318 },
319 },
320 {
321 .name = STM32_MBX_SHUTDOWN,
322 .vq_id = -1,
323 .client = {
324 .tx_block = true,
325 .tx_done = NULL,
326 .tx_tout = 500, /* 500 ms time out */
327 },
328 }
329};
330
Fabien Dessenne4a56e4232019-11-15 11:03:08 +0100331static int stm32_rproc_request_mbox(struct rproc *rproc)
Fabien Dessenne13140de2019-05-14 10:26:58 +0200332{
333 struct stm32_rproc *ddata = rproc->priv;
334 struct device *dev = &rproc->dev;
335 unsigned int i;
Fabien Dessenne4a56e4232019-11-15 11:03:08 +0100336 int j;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200337 const unsigned char *name;
338 struct mbox_client *cl;
339
340 /* Initialise mailbox structure table */
341 memcpy(ddata->mb, stm32_rproc_mbox, sizeof(stm32_rproc_mbox));
342
343 for (i = 0; i < MBOX_NB_MBX; i++) {
344 name = ddata->mb[i].name;
345
346 cl = &ddata->mb[i].client;
347 cl->dev = dev->parent;
348
349 ddata->mb[i].chan = mbox_request_channel_byname(cl, name);
350 if (IS_ERR(ddata->mb[i].chan)) {
Fabien Dessenne4a56e4232019-11-15 11:03:08 +0100351 if (PTR_ERR(ddata->mb[i].chan) == -EPROBE_DEFER)
352 goto err_probe;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200353 dev_warn(dev, "cannot get %s mbox\n", name);
354 ddata->mb[i].chan = NULL;
355 }
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200356 if (ddata->mb[i].vq_id >= 0) {
357 INIT_WORK(&ddata->mb[i].vq_work,
358 stm32_rproc_mb_vq_work);
359 }
Fabien Dessenne13140de2019-05-14 10:26:58 +0200360 }
Fabien Dessenne4a56e4232019-11-15 11:03:08 +0100361
362 return 0;
363
364err_probe:
365 for (j = i - 1; j >= 0; j--)
366 if (ddata->mb[j].chan)
367 mbox_free_channel(ddata->mb[j].chan);
368 return -EPROBE_DEFER;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200369}
370
371static int stm32_rproc_set_hold_boot(struct rproc *rproc, bool hold)
372{
373 struct stm32_rproc *ddata = rproc->priv;
374 struct stm32_syscon hold_boot = ddata->hold_boot;
375 struct arm_smccc_res smc_res;
376 int val, err;
377
378 val = hold ? HOLD_BOOT : RELEASE_BOOT;
379
Arnd Bergmann93f1d3e2019-07-08 14:42:51 +0200380 if (IS_ENABLED(CONFIG_HAVE_ARM_SMCCC) && ddata->secured_soc) {
Fabien Dessenne13140de2019-05-14 10:26:58 +0200381 arm_smccc_smc(STM32_SMC_RCC, STM32_SMC_REG_WRITE,
382 hold_boot.reg, val, 0, 0, 0, 0, &smc_res);
383 err = smc_res.a0;
384 } else {
385 err = regmap_update_bits(hold_boot.map, hold_boot.reg,
386 hold_boot.mask, val);
387 }
388
389 if (err)
390 dev_err(&rproc->dev, "failed to set hold boot\n");
391
392 return err;
393}
394
395static void stm32_rproc_add_coredump_trace(struct rproc *rproc)
396{
397 struct rproc_debug_trace *trace;
398 struct rproc_dump_segment *segment;
399 bool already_added;
400
401 list_for_each_entry(trace, &rproc->traces, node) {
402 already_added = false;
403
404 list_for_each_entry(segment, &rproc->dump_segments, node) {
405 if (segment->da == trace->trace_mem.da) {
406 already_added = true;
407 break;
408 }
409 }
410
411 if (!already_added)
412 rproc_coredump_add_segment(rproc, trace->trace_mem.da,
413 trace->trace_mem.len);
414 }
415}
416
417static int stm32_rproc_start(struct rproc *rproc)
418{
Fabien Dessenneabbe4292019-08-26 17:38:06 +0200419 struct stm32_rproc *ddata = rproc->priv;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200420 int err;
421
422 stm32_rproc_add_coredump_trace(rproc);
423
Fabien Dessenneabbe4292019-08-26 17:38:06 +0200424 /* clear remote proc Deep Sleep */
425 if (ddata->pdds.map) {
426 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg,
427 ddata->pdds.mask, 0);
428 if (err) {
429 dev_err(&rproc->dev, "failed to clear pdds\n");
430 return err;
431 }
432 }
433
Fabien Dessenne13140de2019-05-14 10:26:58 +0200434 err = stm32_rproc_set_hold_boot(rproc, false);
435 if (err)
436 return err;
437
438 return stm32_rproc_set_hold_boot(rproc, true);
439}
440
441static int stm32_rproc_stop(struct rproc *rproc)
442{
443 struct stm32_rproc *ddata = rproc->priv;
444 int err, dummy_data, idx;
445
446 /* request shutdown of the remote processor */
447 if (rproc->state != RPROC_OFFLINE) {
448 idx = stm32_rproc_mbox_idx(rproc, STM32_MBX_SHUTDOWN);
449 if (idx >= 0 && ddata->mb[idx].chan) {
450 /* a dummy data is sent to allow to block on transmit */
451 err = mbox_send_message(ddata->mb[idx].chan,
452 &dummy_data);
453 if (err < 0)
454 dev_warn(&rproc->dev, "warning: remote FW shutdown without ack\n");
455 }
456 }
457
458 err = stm32_rproc_set_hold_boot(rproc, true);
459 if (err)
460 return err;
461
462 err = reset_control_assert(ddata->rst);
463 if (err) {
464 dev_err(&rproc->dev, "failed to assert the reset\n");
465 return err;
466 }
467
468 /* to allow platform Standby power mode, set remote proc Deep Sleep */
469 if (ddata->pdds.map) {
470 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg,
471 ddata->pdds.mask, 1);
472 if (err) {
473 dev_err(&rproc->dev, "failed to set pdds\n");
474 return err;
475 }
476 }
477
478 return 0;
479}
480
481static void stm32_rproc_kick(struct rproc *rproc, int vqid)
482{
483 struct stm32_rproc *ddata = rproc->priv;
484 unsigned int i;
485 int err;
486
487 if (WARN_ON(vqid >= MBOX_NB_VQ))
488 return;
489
490 for (i = 0; i < MBOX_NB_MBX; i++) {
491 if (vqid != ddata->mb[i].vq_id)
492 continue;
493 if (!ddata->mb[i].chan)
494 return;
495 err = mbox_send_message(ddata->mb[i].chan, (void *)(long)vqid);
496 if (err < 0)
497 dev_err(&rproc->dev, "%s: failed (%s, err:%d)\n",
498 __func__, ddata->mb[i].name, err);
499 return;
500 }
501}
502
503static struct rproc_ops st_rproc_ops = {
504 .start = stm32_rproc_start,
505 .stop = stm32_rproc_stop,
506 .kick = stm32_rproc_kick,
507 .load = rproc_elf_load_segments,
508 .parse_fw = stm32_rproc_parse_fw,
509 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
Clement Legere29ff72b2020-04-22 11:30:17 +0200510 .sanity_check = rproc_elf_sanity_check,
Fabien Dessenne13140de2019-05-14 10:26:58 +0200511 .get_boot_addr = rproc_elf_get_boot_addr,
512};
513
514static const struct of_device_id stm32_rproc_match[] = {
515 { .compatible = "st,stm32mp1-m4" },
516 {},
517};
518MODULE_DEVICE_TABLE(of, stm32_rproc_match);
519
520static int stm32_rproc_get_syscon(struct device_node *np, const char *prop,
521 struct stm32_syscon *syscon)
522{
523 int err = 0;
524
525 syscon->map = syscon_regmap_lookup_by_phandle(np, prop);
526 if (IS_ERR(syscon->map)) {
527 err = PTR_ERR(syscon->map);
528 syscon->map = NULL;
529 goto out;
530 }
531
532 err = of_property_read_u32_index(np, prop, 1, &syscon->reg);
533 if (err)
534 goto out;
535
536 err = of_property_read_u32_index(np, prop, 2, &syscon->mask);
537
538out:
539 return err;
540}
541
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600542static int stm32_rproc_parse_dt(struct platform_device *pdev,
543 struct stm32_rproc *ddata, bool *auto_boot)
Fabien Dessenne13140de2019-05-14 10:26:58 +0200544{
545 struct device *dev = &pdev->dev;
546 struct device_node *np = dev->of_node;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200547 struct stm32_syscon tz;
548 unsigned int tzen;
549 int err, irq;
550
551 irq = platform_get_irq(pdev, 0);
Fabien Dessenned333de32019-08-26 17:38:29 +0200552 if (irq == -EPROBE_DEFER)
553 return -EPROBE_DEFER;
554
Fabien Dessenne13140de2019-05-14 10:26:58 +0200555 if (irq > 0) {
556 err = devm_request_irq(dev, irq, stm32_rproc_wdg, 0,
Mathieu Poirierb8631ab2020-07-14 14:04:36 -0600557 dev_name(dev), pdev);
Fabien Dessenne13140de2019-05-14 10:26:58 +0200558 if (err) {
559 dev_err(dev, "failed to request wdg irq\n");
560 return err;
561 }
562
Fabien Dessenne410119e2019-08-26 17:38:43 +0200563 ddata->wdg_irq = irq;
564
565 if (of_property_read_bool(np, "wakeup-source")) {
566 device_init_wakeup(dev, true);
567 dev_pm_set_wake_irq(dev, irq);
568 }
569
Fabien Dessenne13140de2019-05-14 10:26:58 +0200570 dev_info(dev, "wdg irq registered\n");
571 }
572
573 ddata->rst = devm_reset_control_get_by_index(dev, 0);
574 if (IS_ERR(ddata->rst)) {
575 dev_err(dev, "failed to get mcu reset\n");
576 return PTR_ERR(ddata->rst);
577 }
578
579 /*
580 * if platform is secured the hold boot bit must be written by
581 * smc call and read normally.
582 * if not secure the hold boot bit could be read/write normally
583 */
584 err = stm32_rproc_get_syscon(np, "st,syscfg-tz", &tz);
585 if (err) {
586 dev_err(dev, "failed to get tz syscfg\n");
587 return err;
588 }
589
590 err = regmap_read(tz.map, tz.reg, &tzen);
591 if (err) {
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600592 dev_err(dev, "failed to read tzen\n");
Fabien Dessenne13140de2019-05-14 10:26:58 +0200593 return err;
594 }
595 ddata->secured_soc = tzen & tz.mask;
596
597 err = stm32_rproc_get_syscon(np, "st,syscfg-holdboot",
598 &ddata->hold_boot);
599 if (err) {
600 dev_err(dev, "failed to get hold boot\n");
601 return err;
602 }
603
604 err = stm32_rproc_get_syscon(np, "st,syscfg-pdds", &ddata->pdds);
605 if (err)
Ahmad Fatoumb1f0fa82020-02-24 18:25:19 +0100606 dev_info(dev, "failed to get pdds\n");
Fabien Dessenne13140de2019-05-14 10:26:58 +0200607
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600608 *auto_boot = of_property_read_bool(np, "st,auto-boot");
Fabien Dessenne13140de2019-05-14 10:26:58 +0200609
Mathieu Poirier95e32f82020-07-14 14:04:38 -0600610 return 0;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200611}
612
613static int stm32_rproc_probe(struct platform_device *pdev)
614{
615 struct device *dev = &pdev->dev;
616 struct stm32_rproc *ddata;
617 struct device_node *np = dev->of_node;
618 struct rproc *rproc;
619 int ret;
620
621 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
622 if (ret)
623 return ret;
624
625 rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata));
626 if (!rproc)
627 return -ENOMEM;
628
Fabien Dessenne13140de2019-05-14 10:26:58 +0200629 ddata = rproc->priv;
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600630
631 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
632
633 ret = stm32_rproc_parse_dt(pdev, ddata, &rproc->auto_boot);
634 if (ret)
635 goto free_rproc;
636
Mathieu Poirier95e32f82020-07-14 14:04:38 -0600637 ret = stm32_rproc_of_memory_translations(pdev, ddata);
638 if (ret)
639 goto free_rproc;
640
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600641 rproc->has_iommu = false;
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200642 ddata->workqueue = create_workqueue(dev_name(dev));
643 if (!ddata->workqueue) {
644 dev_err(dev, "cannot create workqueue\n");
645 ret = -ENOMEM;
646 goto free_rproc;
647 }
Fabien Dessenne13140de2019-05-14 10:26:58 +0200648
649 platform_set_drvdata(pdev, rproc);
650
Fabien Dessenne4a56e4232019-11-15 11:03:08 +0100651 ret = stm32_rproc_request_mbox(rproc);
652 if (ret)
Mathieu Poirier8210fc82020-07-14 14:04:37 -0600653 goto free_wkq;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200654
655 ret = rproc_add(rproc);
656 if (ret)
657 goto free_mb;
658
659 return 0;
660
661free_mb:
662 stm32_rproc_free_mbox(rproc);
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200663free_wkq:
664 destroy_workqueue(ddata->workqueue);
Fabien Dessenne13140de2019-05-14 10:26:58 +0200665free_rproc:
Fabien Dessenne410119e2019-08-26 17:38:43 +0200666 if (device_may_wakeup(dev)) {
667 dev_pm_clear_wake_irq(dev);
668 device_init_wakeup(dev, false);
669 }
Fabien Dessenne13140de2019-05-14 10:26:58 +0200670 rproc_free(rproc);
671 return ret;
672}
673
674static int stm32_rproc_remove(struct platform_device *pdev)
675{
676 struct rproc *rproc = platform_get_drvdata(pdev);
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200677 struct stm32_rproc *ddata = rproc->priv;
Fabien Dessenne410119e2019-08-26 17:38:43 +0200678 struct device *dev = &pdev->dev;
Fabien Dessenne13140de2019-05-14 10:26:58 +0200679
680 if (atomic_read(&rproc->power) > 0)
681 rproc_shutdown(rproc);
682
683 rproc_del(rproc);
684 stm32_rproc_free_mbox(rproc);
Arnaud Pouliquen714cf5e2019-10-25 10:56:39 +0200685 destroy_workqueue(ddata->workqueue);
Fabien Dessenne410119e2019-08-26 17:38:43 +0200686
687 if (device_may_wakeup(dev)) {
688 dev_pm_clear_wake_irq(dev);
689 device_init_wakeup(dev, false);
690 }
Fabien Dessenne13140de2019-05-14 10:26:58 +0200691 rproc_free(rproc);
692
693 return 0;
694}
695
Fabien Dessenne410119e2019-08-26 17:38:43 +0200696static int __maybe_unused stm32_rproc_suspend(struct device *dev)
697{
698 struct rproc *rproc = dev_get_drvdata(dev);
699 struct stm32_rproc *ddata = rproc->priv;
700
701 if (device_may_wakeup(dev))
702 return enable_irq_wake(ddata->wdg_irq);
703
704 return 0;
705}
706
707static int __maybe_unused stm32_rproc_resume(struct device *dev)
708{
709 struct rproc *rproc = dev_get_drvdata(dev);
710 struct stm32_rproc *ddata = rproc->priv;
711
712 if (device_may_wakeup(dev))
713 return disable_irq_wake(ddata->wdg_irq);
714
715 return 0;
716}
717
718static SIMPLE_DEV_PM_OPS(stm32_rproc_pm_ops,
719 stm32_rproc_suspend, stm32_rproc_resume);
720
Fabien Dessenne13140de2019-05-14 10:26:58 +0200721static struct platform_driver stm32_rproc_driver = {
722 .probe = stm32_rproc_probe,
723 .remove = stm32_rproc_remove,
724 .driver = {
725 .name = "stm32-rproc",
Fabien Dessenne410119e2019-08-26 17:38:43 +0200726 .pm = &stm32_rproc_pm_ops,
Fabien Dessenne13140de2019-05-14 10:26:58 +0200727 .of_match_table = of_match_ptr(stm32_rproc_match),
728 },
729};
730module_platform_driver(stm32_rproc_driver);
731
732MODULE_DESCRIPTION("STM32 Remote Processor Control Driver");
733MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
734MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
735MODULE_LICENSE("GPL v2");
736