gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / remoteproc / stm32_rproc.c
blob6a66dbf2df407db0371649d219b5a55247f32a86
1 // 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 */
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>
18 #include <linux/pm_wakeirq.h>
19 #include <linux/regmap.h>
20 #include <linux/remoteproc.h>
21 #include <linux/reset.h>
22 #include <linux/workqueue.h>
24 #include "remoteproc_internal.h"
26 #define HOLD_BOOT 0
27 #define RELEASE_BOOT 1
29 #define MBOX_NB_VQ 2
30 #define MBOX_NB_MBX 3
32 #define STM32_SMC_RCC 0x82001000
33 #define STM32_SMC_REG_WRITE 0x1
35 #define STM32_MBX_VQ0 "vq0"
36 #define STM32_MBX_VQ0_ID 0
37 #define STM32_MBX_VQ1 "vq1"
38 #define STM32_MBX_VQ1_ID 1
39 #define STM32_MBX_SHUTDOWN "shutdown"
41 struct stm32_syscon {
42 struct regmap *map;
43 u32 reg;
44 u32 mask;
47 struct stm32_rproc_mem {
48 char name[20];
49 void __iomem *cpu_addr;
50 phys_addr_t bus_addr;
51 u32 dev_addr;
52 size_t size;
55 struct stm32_rproc_mem_ranges {
56 u32 dev_addr;
57 u32 bus_addr;
58 u32 size;
61 struct stm32_mbox {
62 const unsigned char name[10];
63 struct mbox_chan *chan;
64 struct mbox_client client;
65 struct work_struct vq_work;
66 int vq_id;
69 struct stm32_rproc {
70 struct reset_control *rst;
71 struct stm32_syscon hold_boot;
72 struct stm32_syscon pdds;
73 int wdg_irq;
74 u32 nb_rmems;
75 struct stm32_rproc_mem *rmems;
76 struct stm32_mbox mb[MBOX_NB_MBX];
77 struct workqueue_struct *workqueue;
78 bool secured_soc;
81 static int stm32_rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da)
83 unsigned int i;
84 struct stm32_rproc *ddata = rproc->priv;
85 struct stm32_rproc_mem *p_mem;
87 for (i = 0; i < ddata->nb_rmems; i++) {
88 p_mem = &ddata->rmems[i];
90 if (pa < p_mem->bus_addr ||
91 pa >= p_mem->bus_addr + p_mem->size)
92 continue;
93 *da = pa - p_mem->bus_addr + p_mem->dev_addr;
94 dev_dbg(rproc->dev.parent, "pa %pa to da %llx\n", &pa, *da);
95 return 0;
98 return -EINVAL;
101 static int stm32_rproc_mem_alloc(struct rproc *rproc,
102 struct rproc_mem_entry *mem)
104 struct device *dev = rproc->dev.parent;
105 void *va;
107 dev_dbg(dev, "map memory: %pa+%x\n", &mem->dma, mem->len);
108 va = ioremap_wc(mem->dma, mem->len);
109 if (IS_ERR_OR_NULL(va)) {
110 dev_err(dev, "Unable to map memory region: %pa+%x\n",
111 &mem->dma, mem->len);
112 return -ENOMEM;
115 /* Update memory entry va */
116 mem->va = va;
118 return 0;
121 static int stm32_rproc_mem_release(struct rproc *rproc,
122 struct rproc_mem_entry *mem)
124 dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma);
125 iounmap(mem->va);
127 return 0;
130 static int stm32_rproc_of_memory_translations(struct rproc *rproc)
132 struct device *parent, *dev = rproc->dev.parent;
133 struct stm32_rproc *ddata = rproc->priv;
134 struct device_node *np;
135 struct stm32_rproc_mem *p_mems;
136 struct stm32_rproc_mem_ranges *mem_range;
137 int cnt, array_size, i, ret = 0;
139 parent = dev->parent;
140 np = parent->of_node;
142 cnt = of_property_count_elems_of_size(np, "dma-ranges",
143 sizeof(*mem_range));
144 if (cnt <= 0) {
145 dev_err(dev, "%s: dma-ranges property not defined\n", __func__);
146 return -EINVAL;
149 p_mems = devm_kcalloc(dev, cnt, sizeof(*p_mems), GFP_KERNEL);
150 if (!p_mems)
151 return -ENOMEM;
152 mem_range = kcalloc(cnt, sizeof(*mem_range), GFP_KERNEL);
153 if (!mem_range)
154 return -ENOMEM;
156 array_size = cnt * sizeof(struct stm32_rproc_mem_ranges) / sizeof(u32);
158 ret = of_property_read_u32_array(np, "dma-ranges",
159 (u32 *)mem_range, array_size);
160 if (ret) {
161 dev_err(dev, "error while get dma-ranges property: %x\n", ret);
162 goto free_mem;
165 for (i = 0; i < cnt; i++) {
166 p_mems[i].bus_addr = mem_range[i].bus_addr;
167 p_mems[i].dev_addr = mem_range[i].dev_addr;
168 p_mems[i].size = mem_range[i].size;
170 dev_dbg(dev, "memory range[%i]: da %#x, pa %pa, size %#zx:\n",
171 i, p_mems[i].dev_addr, &p_mems[i].bus_addr,
172 p_mems[i].size);
175 ddata->rmems = p_mems;
176 ddata->nb_rmems = cnt;
178 free_mem:
179 kfree(mem_range);
180 return ret;
183 static int stm32_rproc_mbox_idx(struct rproc *rproc, const unsigned char *name)
185 struct stm32_rproc *ddata = rproc->priv;
186 int i;
188 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) {
189 if (!strncmp(ddata->mb[i].name, name, strlen(name)))
190 return i;
192 dev_err(&rproc->dev, "mailbox %s not found\n", name);
194 return -EINVAL;
197 static int stm32_rproc_elf_load_rsc_table(struct rproc *rproc,
198 const struct firmware *fw)
200 if (rproc_elf_load_rsc_table(rproc, fw))
201 dev_warn(&rproc->dev, "no resource table found for this firmware\n");
203 return 0;
206 static int stm32_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
208 struct device *dev = rproc->dev.parent;
209 struct device_node *np = dev->of_node;
210 struct of_phandle_iterator it;
211 struct rproc_mem_entry *mem;
212 struct reserved_mem *rmem;
213 u64 da;
214 int index = 0;
216 /* Register associated reserved memory regions */
217 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
218 while (of_phandle_iterator_next(&it) == 0) {
219 rmem = of_reserved_mem_lookup(it.node);
220 if (!rmem) {
221 dev_err(dev, "unable to acquire memory-region\n");
222 return -EINVAL;
225 if (stm32_rproc_pa_to_da(rproc, rmem->base, &da) < 0) {
226 dev_err(dev, "memory region not valid %pa\n",
227 &rmem->base);
228 return -EINVAL;
231 /* No need to map vdev buffer */
232 if (strcmp(it.node->name, "vdev0buffer")) {
233 /* Register memory region */
234 mem = rproc_mem_entry_init(dev, NULL,
235 (dma_addr_t)rmem->base,
236 rmem->size, da,
237 stm32_rproc_mem_alloc,
238 stm32_rproc_mem_release,
239 it.node->name);
241 if (mem)
242 rproc_coredump_add_segment(rproc, da,
243 rmem->size);
244 } else {
245 /* Register reserved memory for vdev buffer alloc */
246 mem = rproc_of_resm_mem_entry_init(dev, index,
247 rmem->size,
248 rmem->base,
249 it.node->name);
252 if (!mem)
253 return -ENOMEM;
255 rproc_add_carveout(rproc, mem);
256 index++;
259 return stm32_rproc_elf_load_rsc_table(rproc, fw);
262 static irqreturn_t stm32_rproc_wdg(int irq, void *data)
264 struct rproc *rproc = data;
266 rproc_report_crash(rproc, RPROC_WATCHDOG);
268 return IRQ_HANDLED;
271 static void stm32_rproc_mb_vq_work(struct work_struct *work)
273 struct stm32_mbox *mb = container_of(work, struct stm32_mbox, vq_work);
274 struct rproc *rproc = dev_get_drvdata(mb->client.dev);
276 if (rproc_vq_interrupt(rproc, mb->vq_id) == IRQ_NONE)
277 dev_dbg(&rproc->dev, "no message found in vq%d\n", mb->vq_id);
280 static void stm32_rproc_mb_callback(struct mbox_client *cl, void *data)
282 struct rproc *rproc = dev_get_drvdata(cl->dev);
283 struct stm32_mbox *mb = container_of(cl, struct stm32_mbox, client);
284 struct stm32_rproc *ddata = rproc->priv;
286 queue_work(ddata->workqueue, &mb->vq_work);
289 static void stm32_rproc_free_mbox(struct rproc *rproc)
291 struct stm32_rproc *ddata = rproc->priv;
292 unsigned int i;
294 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) {
295 if (ddata->mb[i].chan)
296 mbox_free_channel(ddata->mb[i].chan);
297 ddata->mb[i].chan = NULL;
301 static const struct stm32_mbox stm32_rproc_mbox[MBOX_NB_MBX] = {
303 .name = STM32_MBX_VQ0,
304 .vq_id = STM32_MBX_VQ0_ID,
305 .client = {
306 .rx_callback = stm32_rproc_mb_callback,
307 .tx_block = false,
311 .name = STM32_MBX_VQ1,
312 .vq_id = STM32_MBX_VQ1_ID,
313 .client = {
314 .rx_callback = stm32_rproc_mb_callback,
315 .tx_block = false,
319 .name = STM32_MBX_SHUTDOWN,
320 .vq_id = -1,
321 .client = {
322 .tx_block = true,
323 .tx_done = NULL,
324 .tx_tout = 500, /* 500 ms time out */
329 static int stm32_rproc_request_mbox(struct rproc *rproc)
331 struct stm32_rproc *ddata = rproc->priv;
332 struct device *dev = &rproc->dev;
333 unsigned int i;
334 int j;
335 const unsigned char *name;
336 struct mbox_client *cl;
338 /* Initialise mailbox structure table */
339 memcpy(ddata->mb, stm32_rproc_mbox, sizeof(stm32_rproc_mbox));
341 for (i = 0; i < MBOX_NB_MBX; i++) {
342 name = ddata->mb[i].name;
344 cl = &ddata->mb[i].client;
345 cl->dev = dev->parent;
347 ddata->mb[i].chan = mbox_request_channel_byname(cl, name);
348 if (IS_ERR(ddata->mb[i].chan)) {
349 if (PTR_ERR(ddata->mb[i].chan) == -EPROBE_DEFER)
350 goto err_probe;
351 dev_warn(dev, "cannot get %s mbox\n", name);
352 ddata->mb[i].chan = NULL;
354 if (ddata->mb[i].vq_id >= 0) {
355 INIT_WORK(&ddata->mb[i].vq_work,
356 stm32_rproc_mb_vq_work);
360 return 0;
362 err_probe:
363 for (j = i - 1; j >= 0; j--)
364 if (ddata->mb[j].chan)
365 mbox_free_channel(ddata->mb[j].chan);
366 return -EPROBE_DEFER;
369 static int stm32_rproc_set_hold_boot(struct rproc *rproc, bool hold)
371 struct stm32_rproc *ddata = rproc->priv;
372 struct stm32_syscon hold_boot = ddata->hold_boot;
373 struct arm_smccc_res smc_res;
374 int val, err;
376 val = hold ? HOLD_BOOT : RELEASE_BOOT;
378 if (IS_ENABLED(CONFIG_HAVE_ARM_SMCCC) && ddata->secured_soc) {
379 arm_smccc_smc(STM32_SMC_RCC, STM32_SMC_REG_WRITE,
380 hold_boot.reg, val, 0, 0, 0, 0, &smc_res);
381 err = smc_res.a0;
382 } else {
383 err = regmap_update_bits(hold_boot.map, hold_boot.reg,
384 hold_boot.mask, val);
387 if (err)
388 dev_err(&rproc->dev, "failed to set hold boot\n");
390 return err;
393 static void stm32_rproc_add_coredump_trace(struct rproc *rproc)
395 struct rproc_debug_trace *trace;
396 struct rproc_dump_segment *segment;
397 bool already_added;
399 list_for_each_entry(trace, &rproc->traces, node) {
400 already_added = false;
402 list_for_each_entry(segment, &rproc->dump_segments, node) {
403 if (segment->da == trace->trace_mem.da) {
404 already_added = true;
405 break;
409 if (!already_added)
410 rproc_coredump_add_segment(rproc, trace->trace_mem.da,
411 trace->trace_mem.len);
415 static int stm32_rproc_start(struct rproc *rproc)
417 struct stm32_rproc *ddata = rproc->priv;
418 int err;
420 stm32_rproc_add_coredump_trace(rproc);
422 /* clear remote proc Deep Sleep */
423 if (ddata->pdds.map) {
424 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg,
425 ddata->pdds.mask, 0);
426 if (err) {
427 dev_err(&rproc->dev, "failed to clear pdds\n");
428 return err;
432 err = stm32_rproc_set_hold_boot(rproc, false);
433 if (err)
434 return err;
436 return stm32_rproc_set_hold_boot(rproc, true);
439 static int stm32_rproc_stop(struct rproc *rproc)
441 struct stm32_rproc *ddata = rproc->priv;
442 int err, dummy_data, idx;
444 /* request shutdown of the remote processor */
445 if (rproc->state != RPROC_OFFLINE) {
446 idx = stm32_rproc_mbox_idx(rproc, STM32_MBX_SHUTDOWN);
447 if (idx >= 0 && ddata->mb[idx].chan) {
448 /* a dummy data is sent to allow to block on transmit */
449 err = mbox_send_message(ddata->mb[idx].chan,
450 &dummy_data);
451 if (err < 0)
452 dev_warn(&rproc->dev, "warning: remote FW shutdown without ack\n");
456 err = stm32_rproc_set_hold_boot(rproc, true);
457 if (err)
458 return err;
460 err = reset_control_assert(ddata->rst);
461 if (err) {
462 dev_err(&rproc->dev, "failed to assert the reset\n");
463 return err;
466 /* to allow platform Standby power mode, set remote proc Deep Sleep */
467 if (ddata->pdds.map) {
468 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg,
469 ddata->pdds.mask, 1);
470 if (err) {
471 dev_err(&rproc->dev, "failed to set pdds\n");
472 return err;
476 return 0;
479 static void stm32_rproc_kick(struct rproc *rproc, int vqid)
481 struct stm32_rproc *ddata = rproc->priv;
482 unsigned int i;
483 int err;
485 if (WARN_ON(vqid >= MBOX_NB_VQ))
486 return;
488 for (i = 0; i < MBOX_NB_MBX; i++) {
489 if (vqid != ddata->mb[i].vq_id)
490 continue;
491 if (!ddata->mb[i].chan)
492 return;
493 err = mbox_send_message(ddata->mb[i].chan, (void *)(long)vqid);
494 if (err < 0)
495 dev_err(&rproc->dev, "%s: failed (%s, err:%d)\n",
496 __func__, ddata->mb[i].name, err);
497 return;
501 static struct rproc_ops st_rproc_ops = {
502 .start = stm32_rproc_start,
503 .stop = stm32_rproc_stop,
504 .kick = stm32_rproc_kick,
505 .load = rproc_elf_load_segments,
506 .parse_fw = stm32_rproc_parse_fw,
507 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
508 .sanity_check = rproc_elf32_sanity_check,
509 .get_boot_addr = rproc_elf_get_boot_addr,
512 static const struct of_device_id stm32_rproc_match[] = {
513 { .compatible = "st,stm32mp1-m4" },
516 MODULE_DEVICE_TABLE(of, stm32_rproc_match);
518 static int stm32_rproc_get_syscon(struct device_node *np, const char *prop,
519 struct stm32_syscon *syscon)
521 int err = 0;
523 syscon->map = syscon_regmap_lookup_by_phandle(np, prop);
524 if (IS_ERR(syscon->map)) {
525 err = PTR_ERR(syscon->map);
526 syscon->map = NULL;
527 goto out;
530 err = of_property_read_u32_index(np, prop, 1, &syscon->reg);
531 if (err)
532 goto out;
534 err = of_property_read_u32_index(np, prop, 2, &syscon->mask);
536 out:
537 return err;
540 static int stm32_rproc_parse_dt(struct platform_device *pdev)
542 struct device *dev = &pdev->dev;
543 struct device_node *np = dev->of_node;
544 struct rproc *rproc = platform_get_drvdata(pdev);
545 struct stm32_rproc *ddata = rproc->priv;
546 struct stm32_syscon tz;
547 unsigned int tzen;
548 int err, irq;
550 irq = platform_get_irq(pdev, 0);
551 if (irq == -EPROBE_DEFER)
552 return -EPROBE_DEFER;
554 if (irq > 0) {
555 err = devm_request_irq(dev, irq, stm32_rproc_wdg, 0,
556 dev_name(dev), rproc);
557 if (err) {
558 dev_err(dev, "failed to request wdg irq\n");
559 return err;
562 ddata->wdg_irq = irq;
564 if (of_property_read_bool(np, "wakeup-source")) {
565 device_init_wakeup(dev, true);
566 dev_pm_set_wake_irq(dev, irq);
569 dev_info(dev, "wdg irq registered\n");
572 ddata->rst = devm_reset_control_get_by_index(dev, 0);
573 if (IS_ERR(ddata->rst)) {
574 dev_err(dev, "failed to get mcu reset\n");
575 return PTR_ERR(ddata->rst);
579 * if platform is secured the hold boot bit must be written by
580 * smc call and read normally.
581 * if not secure the hold boot bit could be read/write normally
583 err = stm32_rproc_get_syscon(np, "st,syscfg-tz", &tz);
584 if (err) {
585 dev_err(dev, "failed to get tz syscfg\n");
586 return err;
589 err = regmap_read(tz.map, tz.reg, &tzen);
590 if (err) {
591 dev_err(&rproc->dev, "failed to read tzen\n");
592 return err;
594 ddata->secured_soc = tzen & tz.mask;
596 err = stm32_rproc_get_syscon(np, "st,syscfg-holdboot",
597 &ddata->hold_boot);
598 if (err) {
599 dev_err(dev, "failed to get hold boot\n");
600 return err;
603 err = stm32_rproc_get_syscon(np, "st,syscfg-pdds", &ddata->pdds);
604 if (err)
605 dev_info(dev, "failed to get pdds\n");
607 rproc->auto_boot = of_property_read_bool(np, "st,auto-boot");
609 return stm32_rproc_of_memory_translations(rproc);
612 static int stm32_rproc_probe(struct platform_device *pdev)
614 struct device *dev = &pdev->dev;
615 struct stm32_rproc *ddata;
616 struct device_node *np = dev->of_node;
617 struct rproc *rproc;
618 int ret;
620 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
621 if (ret)
622 return ret;
624 rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata));
625 if (!rproc)
626 return -ENOMEM;
628 rproc->has_iommu = false;
629 ddata = rproc->priv;
630 ddata->workqueue = create_workqueue(dev_name(dev));
631 if (!ddata->workqueue) {
632 dev_err(dev, "cannot create workqueue\n");
633 ret = -ENOMEM;
634 goto free_rproc;
637 platform_set_drvdata(pdev, rproc);
639 ret = stm32_rproc_parse_dt(pdev);
640 if (ret)
641 goto free_wkq;
643 ret = stm32_rproc_request_mbox(rproc);
644 if (ret)
645 goto free_rproc;
647 ret = rproc_add(rproc);
648 if (ret)
649 goto free_mb;
651 return 0;
653 free_mb:
654 stm32_rproc_free_mbox(rproc);
655 free_wkq:
656 destroy_workqueue(ddata->workqueue);
657 free_rproc:
658 if (device_may_wakeup(dev)) {
659 dev_pm_clear_wake_irq(dev);
660 device_init_wakeup(dev, false);
662 rproc_free(rproc);
663 return ret;
666 static int stm32_rproc_remove(struct platform_device *pdev)
668 struct rproc *rproc = platform_get_drvdata(pdev);
669 struct stm32_rproc *ddata = rproc->priv;
670 struct device *dev = &pdev->dev;
672 if (atomic_read(&rproc->power) > 0)
673 rproc_shutdown(rproc);
675 rproc_del(rproc);
676 stm32_rproc_free_mbox(rproc);
677 destroy_workqueue(ddata->workqueue);
679 if (device_may_wakeup(dev)) {
680 dev_pm_clear_wake_irq(dev);
681 device_init_wakeup(dev, false);
683 rproc_free(rproc);
685 return 0;
688 static int __maybe_unused stm32_rproc_suspend(struct device *dev)
690 struct rproc *rproc = dev_get_drvdata(dev);
691 struct stm32_rproc *ddata = rproc->priv;
693 if (device_may_wakeup(dev))
694 return enable_irq_wake(ddata->wdg_irq);
696 return 0;
699 static int __maybe_unused stm32_rproc_resume(struct device *dev)
701 struct rproc *rproc = dev_get_drvdata(dev);
702 struct stm32_rproc *ddata = rproc->priv;
704 if (device_may_wakeup(dev))
705 return disable_irq_wake(ddata->wdg_irq);
707 return 0;
710 static SIMPLE_DEV_PM_OPS(stm32_rproc_pm_ops,
711 stm32_rproc_suspend, stm32_rproc_resume);
713 static struct platform_driver stm32_rproc_driver = {
714 .probe = stm32_rproc_probe,
715 .remove = stm32_rproc_remove,
716 .driver = {
717 .name = "stm32-rproc",
718 .pm = &stm32_rproc_pm_ops,
719 .of_match_table = of_match_ptr(stm32_rproc_match),
722 module_platform_driver(stm32_rproc_driver);
724 MODULE_DESCRIPTION("STM32 Remote Processor Control Driver");
725 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
726 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
727 MODULE_LICENSE("GPL v2");