1 // SPDX-License-Identifier: GPL-2.0
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.
8 #include <linux/arm-smccc.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/interrupt.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"
27 #define RELEASE_BOOT 1
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"
47 struct stm32_rproc_mem
{
49 void __iomem
*cpu_addr
;
55 struct stm32_rproc_mem_ranges
{
62 const unsigned char name
[10];
63 struct mbox_chan
*chan
;
64 struct mbox_client client
;
65 struct work_struct vq_work
;
70 struct reset_control
*rst
;
71 struct stm32_syscon hold_boot
;
72 struct stm32_syscon pdds
;
75 struct stm32_rproc_mem
*rmems
;
76 struct stm32_mbox mb
[MBOX_NB_MBX
];
77 struct workqueue_struct
*workqueue
;
81 static int stm32_rproc_pa_to_da(struct rproc
*rproc
, phys_addr_t pa
, u64
*da
)
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
)
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
);
101 static int stm32_rproc_mem_alloc(struct rproc
*rproc
,
102 struct rproc_mem_entry
*mem
)
104 struct device
*dev
= rproc
->dev
.parent
;
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
);
115 /* Update memory entry va */
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
);
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",
145 dev_err(dev
, "%s: dma-ranges property not defined\n", __func__
);
149 p_mems
= devm_kcalloc(dev
, cnt
, sizeof(*p_mems
), GFP_KERNEL
);
152 mem_range
= kcalloc(cnt
, sizeof(*mem_range
), GFP_KERNEL
);
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
);
161 dev_err(dev
, "error while get dma-ranges property: %x\n", ret
);
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
,
175 ddata
->rmems
= p_mems
;
176 ddata
->nb_rmems
= cnt
;
183 static int stm32_rproc_mbox_idx(struct rproc
*rproc
, const unsigned char *name
)
185 struct stm32_rproc
*ddata
= rproc
->priv
;
188 for (i
= 0; i
< ARRAY_SIZE(ddata
->mb
); i
++) {
189 if (!strncmp(ddata
->mb
[i
].name
, name
, strlen(name
)))
192 dev_err(&rproc
->dev
, "mailbox %s not found\n", name
);
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");
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
;
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
);
221 dev_err(dev
, "unable to acquire memory-region\n");
225 if (stm32_rproc_pa_to_da(rproc
, rmem
->base
, &da
) < 0) {
226 dev_err(dev
, "memory region not valid %pa\n",
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
,
237 stm32_rproc_mem_alloc
,
238 stm32_rproc_mem_release
,
242 rproc_coredump_add_segment(rproc
, da
,
245 /* Register reserved memory for vdev buffer alloc */
246 mem
= rproc_of_resm_mem_entry_init(dev
, index
,
255 rproc_add_carveout(rproc
, mem
);
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
);
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
;
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
,
306 .rx_callback
= stm32_rproc_mb_callback
,
311 .name
= STM32_MBX_VQ1
,
312 .vq_id
= STM32_MBX_VQ1_ID
,
314 .rx_callback
= stm32_rproc_mb_callback
,
319 .name
= STM32_MBX_SHUTDOWN
,
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
;
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
)
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
);
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
;
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
);
383 err
= regmap_update_bits(hold_boot
.map
, hold_boot
.reg
,
384 hold_boot
.mask
, val
);
388 dev_err(&rproc
->dev
, "failed to set hold boot\n");
393 static void stm32_rproc_add_coredump_trace(struct rproc
*rproc
)
395 struct rproc_debug_trace
*trace
;
396 struct rproc_dump_segment
*segment
;
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;
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
;
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);
427 dev_err(&rproc
->dev
, "failed to clear pdds\n");
432 err
= stm32_rproc_set_hold_boot(rproc
, false);
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
,
452 dev_warn(&rproc
->dev
, "warning: remote FW shutdown without ack\n");
456 err
= stm32_rproc_set_hold_boot(rproc
, true);
460 err
= reset_control_assert(ddata
->rst
);
462 dev_err(&rproc
->dev
, "failed to assert the reset\n");
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);
471 dev_err(&rproc
->dev
, "failed to set pdds\n");
479 static void stm32_rproc_kick(struct rproc
*rproc
, int vqid
)
481 struct stm32_rproc
*ddata
= rproc
->priv
;
485 if (WARN_ON(vqid
>= MBOX_NB_VQ
))
488 for (i
= 0; i
< MBOX_NB_MBX
; i
++) {
489 if (vqid
!= ddata
->mb
[i
].vq_id
)
491 if (!ddata
->mb
[i
].chan
)
493 err
= mbox_send_message(ddata
->mb
[i
].chan
, (void *)(long)vqid
);
495 dev_err(&rproc
->dev
, "%s: failed (%s, err:%d)\n",
496 __func__
, ddata
->mb
[i
].name
, err
);
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
)
523 syscon
->map
= syscon_regmap_lookup_by_phandle(np
, prop
);
524 if (IS_ERR(syscon
->map
)) {
525 err
= PTR_ERR(syscon
->map
);
530 err
= of_property_read_u32_index(np
, prop
, 1, &syscon
->reg
);
534 err
= of_property_read_u32_index(np
, prop
, 2, &syscon
->mask
);
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
;
550 irq
= platform_get_irq(pdev
, 0);
551 if (irq
== -EPROBE_DEFER
)
552 return -EPROBE_DEFER
;
555 err
= devm_request_irq(dev
, irq
, stm32_rproc_wdg
, 0,
556 dev_name(dev
), rproc
);
558 dev_err(dev
, "failed to request wdg irq\n");
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
);
585 dev_err(dev
, "failed to get tz syscfg\n");
589 err
= regmap_read(tz
.map
, tz
.reg
, &tzen
);
591 dev_err(&rproc
->dev
, "failed to read tzen\n");
594 ddata
->secured_soc
= tzen
& tz
.mask
;
596 err
= stm32_rproc_get_syscon(np
, "st,syscfg-holdboot",
599 dev_err(dev
, "failed to get hold boot\n");
603 err
= stm32_rproc_get_syscon(np
, "st,syscfg-pdds", &ddata
->pdds
);
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
;
620 ret
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(32));
624 rproc
= rproc_alloc(dev
, np
->name
, &st_rproc_ops
, NULL
, sizeof(*ddata
));
628 rproc
->has_iommu
= false;
630 ddata
->workqueue
= create_workqueue(dev_name(dev
));
631 if (!ddata
->workqueue
) {
632 dev_err(dev
, "cannot create workqueue\n");
637 platform_set_drvdata(pdev
, rproc
);
639 ret
= stm32_rproc_parse_dt(pdev
);
643 ret
= stm32_rproc_request_mbox(rproc
);
647 ret
= rproc_add(rproc
);
654 stm32_rproc_free_mbox(rproc
);
656 destroy_workqueue(ddata
->workqueue
);
658 if (device_may_wakeup(dev
)) {
659 dev_pm_clear_wake_irq(dev
);
660 device_init_wakeup(dev
, false);
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
);
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);
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
);
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
);
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
,
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");