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/regmap.h>
19 #include <linux/remoteproc.h>
20 #include <linux/reset.h>
22 #include "remoteproc_internal.h"
25 #define RELEASE_BOOT 1
30 #define STM32_SMC_RCC 0x82001000
31 #define STM32_SMC_REG_WRITE 0x1
33 #define STM32_MBX_VQ0 "vq0"
34 #define STM32_MBX_VQ1 "vq1"
35 #define STM32_MBX_SHUTDOWN "shutdown"
43 struct stm32_rproc_mem
{
45 void __iomem
*cpu_addr
;
51 struct stm32_rproc_mem_ranges
{
58 const unsigned char name
[10];
59 struct mbox_chan
*chan
;
60 struct mbox_client client
;
65 struct reset_control
*rst
;
66 struct stm32_syscon hold_boot
;
67 struct stm32_syscon pdds
;
69 struct stm32_rproc_mem
*rmems
;
70 struct stm32_mbox mb
[MBOX_NB_MBX
];
74 static int stm32_rproc_pa_to_da(struct rproc
*rproc
, phys_addr_t pa
, u64
*da
)
77 struct stm32_rproc
*ddata
= rproc
->priv
;
78 struct stm32_rproc_mem
*p_mem
;
80 for (i
= 0; i
< ddata
->nb_rmems
; i
++) {
81 p_mem
= &ddata
->rmems
[i
];
83 if (pa
< p_mem
->bus_addr
||
84 pa
>= p_mem
->bus_addr
+ p_mem
->size
)
86 *da
= pa
- p_mem
->bus_addr
+ p_mem
->dev_addr
;
87 dev_dbg(rproc
->dev
.parent
, "pa %pa to da %llx\n", &pa
, *da
);
94 static int stm32_rproc_mem_alloc(struct rproc
*rproc
,
95 struct rproc_mem_entry
*mem
)
97 struct device
*dev
= rproc
->dev
.parent
;
100 dev_dbg(dev
, "map memory: %pa+%x\n", &mem
->dma
, mem
->len
);
101 va
= ioremap_wc(mem
->dma
, mem
->len
);
102 if (IS_ERR_OR_NULL(va
)) {
103 dev_err(dev
, "Unable to map memory region: %pa+%x\n",
104 &mem
->dma
, mem
->len
);
108 /* Update memory entry va */
114 static int stm32_rproc_mem_release(struct rproc
*rproc
,
115 struct rproc_mem_entry
*mem
)
117 dev_dbg(rproc
->dev
.parent
, "unmap memory: %pa\n", &mem
->dma
);
123 static int stm32_rproc_of_memory_translations(struct rproc
*rproc
)
125 struct device
*parent
, *dev
= rproc
->dev
.parent
;
126 struct stm32_rproc
*ddata
= rproc
->priv
;
127 struct device_node
*np
;
128 struct stm32_rproc_mem
*p_mems
;
129 struct stm32_rproc_mem_ranges
*mem_range
;
130 int cnt
, array_size
, i
, ret
= 0;
132 parent
= dev
->parent
;
133 np
= parent
->of_node
;
135 cnt
= of_property_count_elems_of_size(np
, "dma-ranges",
138 dev_err(dev
, "%s: dma-ranges property not defined\n", __func__
);
142 p_mems
= devm_kcalloc(dev
, cnt
, sizeof(*p_mems
), GFP_KERNEL
);
145 mem_range
= kcalloc(cnt
, sizeof(*mem_range
), GFP_KERNEL
);
149 array_size
= cnt
* sizeof(struct stm32_rproc_mem_ranges
) / sizeof(u32
);
151 ret
= of_property_read_u32_array(np
, "dma-ranges",
152 (u32
*)mem_range
, array_size
);
154 dev_err(dev
, "error while get dma-ranges property: %x\n", ret
);
158 for (i
= 0; i
< cnt
; i
++) {
159 p_mems
[i
].bus_addr
= mem_range
[i
].bus_addr
;
160 p_mems
[i
].dev_addr
= mem_range
[i
].dev_addr
;
161 p_mems
[i
].size
= mem_range
[i
].size
;
163 dev_dbg(dev
, "memory range[%i]: da %#x, pa %pa, size %#zx:\n",
164 i
, p_mems
[i
].dev_addr
, &p_mems
[i
].bus_addr
,
168 ddata
->rmems
= p_mems
;
169 ddata
->nb_rmems
= cnt
;
176 static int stm32_rproc_mbox_idx(struct rproc
*rproc
, const unsigned char *name
)
178 struct stm32_rproc
*ddata
= rproc
->priv
;
181 for (i
= 0; i
< ARRAY_SIZE(ddata
->mb
); i
++) {
182 if (!strncmp(ddata
->mb
[i
].name
, name
, strlen(name
)))
185 dev_err(&rproc
->dev
, "mailbox %s not found\n", name
);
190 static int stm32_rproc_elf_load_rsc_table(struct rproc
*rproc
,
191 const struct firmware
*fw
)
193 if (rproc_elf_load_rsc_table(rproc
, fw
))
194 dev_warn(&rproc
->dev
, "no resource table found for this firmware\n");
199 static int stm32_rproc_parse_fw(struct rproc
*rproc
, const struct firmware
*fw
)
201 struct device
*dev
= rproc
->dev
.parent
;
202 struct device_node
*np
= dev
->of_node
;
203 struct of_phandle_iterator it
;
204 struct rproc_mem_entry
*mem
;
205 struct reserved_mem
*rmem
;
209 /* Register associated reserved memory regions */
210 of_phandle_iterator_init(&it
, np
, "memory-region", NULL
, 0);
211 while (of_phandle_iterator_next(&it
) == 0) {
212 rmem
= of_reserved_mem_lookup(it
.node
);
214 dev_err(dev
, "unable to acquire memory-region\n");
218 if (stm32_rproc_pa_to_da(rproc
, rmem
->base
, &da
) < 0) {
219 dev_err(dev
, "memory region not valid %pa\n",
224 /* No need to map vdev buffer */
225 if (strcmp(it
.node
->name
, "vdev0buffer")) {
226 /* Register memory region */
227 mem
= rproc_mem_entry_init(dev
, NULL
,
228 (dma_addr_t
)rmem
->base
,
230 stm32_rproc_mem_alloc
,
231 stm32_rproc_mem_release
,
235 rproc_coredump_add_segment(rproc
, da
,
238 /* Register reserved memory for vdev buffer alloc */
239 mem
= rproc_of_resm_mem_entry_init(dev
, index
,
248 rproc_add_carveout(rproc
, mem
);
252 return stm32_rproc_elf_load_rsc_table(rproc
, fw
);
255 static irqreturn_t
stm32_rproc_wdg(int irq
, void *data
)
257 struct rproc
*rproc
= data
;
259 rproc_report_crash(rproc
, RPROC_WATCHDOG
);
264 static void stm32_rproc_mb_callback(struct mbox_client
*cl
, void *data
)
266 struct rproc
*rproc
= dev_get_drvdata(cl
->dev
);
267 struct stm32_mbox
*mb
= container_of(cl
, struct stm32_mbox
, client
);
269 if (rproc_vq_interrupt(rproc
, mb
->vq_id
) == IRQ_NONE
)
270 dev_dbg(&rproc
->dev
, "no message found in vq%d\n", mb
->vq_id
);
273 static void stm32_rproc_free_mbox(struct rproc
*rproc
)
275 struct stm32_rproc
*ddata
= rproc
->priv
;
278 for (i
= 0; i
< ARRAY_SIZE(ddata
->mb
); i
++) {
279 if (ddata
->mb
[i
].chan
)
280 mbox_free_channel(ddata
->mb
[i
].chan
);
281 ddata
->mb
[i
].chan
= NULL
;
285 static const struct stm32_mbox stm32_rproc_mbox
[MBOX_NB_MBX
] = {
287 .name
= STM32_MBX_VQ0
,
290 .rx_callback
= stm32_rproc_mb_callback
,
295 .name
= STM32_MBX_VQ1
,
298 .rx_callback
= stm32_rproc_mb_callback
,
303 .name
= STM32_MBX_SHUTDOWN
,
308 .tx_tout
= 500, /* 500 ms time out */
313 static void stm32_rproc_request_mbox(struct rproc
*rproc
)
315 struct stm32_rproc
*ddata
= rproc
->priv
;
316 struct device
*dev
= &rproc
->dev
;
318 const unsigned char *name
;
319 struct mbox_client
*cl
;
321 /* Initialise mailbox structure table */
322 memcpy(ddata
->mb
, stm32_rproc_mbox
, sizeof(stm32_rproc_mbox
));
324 for (i
= 0; i
< MBOX_NB_MBX
; i
++) {
325 name
= ddata
->mb
[i
].name
;
327 cl
= &ddata
->mb
[i
].client
;
328 cl
->dev
= dev
->parent
;
330 ddata
->mb
[i
].chan
= mbox_request_channel_byname(cl
, name
);
331 if (IS_ERR(ddata
->mb
[i
].chan
)) {
332 dev_warn(dev
, "cannot get %s mbox\n", name
);
333 ddata
->mb
[i
].chan
= NULL
;
338 static int stm32_rproc_set_hold_boot(struct rproc
*rproc
, bool hold
)
340 struct stm32_rproc
*ddata
= rproc
->priv
;
341 struct stm32_syscon hold_boot
= ddata
->hold_boot
;
342 struct arm_smccc_res smc_res
;
345 val
= hold
? HOLD_BOOT
: RELEASE_BOOT
;
347 if (IS_ENABLED(CONFIG_HAVE_ARM_SMCCC
) && ddata
->secured_soc
) {
348 arm_smccc_smc(STM32_SMC_RCC
, STM32_SMC_REG_WRITE
,
349 hold_boot
.reg
, val
, 0, 0, 0, 0, &smc_res
);
352 err
= regmap_update_bits(hold_boot
.map
, hold_boot
.reg
,
353 hold_boot
.mask
, val
);
357 dev_err(&rproc
->dev
, "failed to set hold boot\n");
362 static void stm32_rproc_add_coredump_trace(struct rproc
*rproc
)
364 struct rproc_debug_trace
*trace
;
365 struct rproc_dump_segment
*segment
;
368 list_for_each_entry(trace
, &rproc
->traces
, node
) {
369 already_added
= false;
371 list_for_each_entry(segment
, &rproc
->dump_segments
, node
) {
372 if (segment
->da
== trace
->trace_mem
.da
) {
373 already_added
= true;
379 rproc_coredump_add_segment(rproc
, trace
->trace_mem
.da
,
380 trace
->trace_mem
.len
);
384 static int stm32_rproc_start(struct rproc
*rproc
)
388 stm32_rproc_add_coredump_trace(rproc
);
390 err
= stm32_rproc_set_hold_boot(rproc
, false);
394 return stm32_rproc_set_hold_boot(rproc
, true);
397 static int stm32_rproc_stop(struct rproc
*rproc
)
399 struct stm32_rproc
*ddata
= rproc
->priv
;
400 int err
, dummy_data
, idx
;
402 /* request shutdown of the remote processor */
403 if (rproc
->state
!= RPROC_OFFLINE
) {
404 idx
= stm32_rproc_mbox_idx(rproc
, STM32_MBX_SHUTDOWN
);
405 if (idx
>= 0 && ddata
->mb
[idx
].chan
) {
406 /* a dummy data is sent to allow to block on transmit */
407 err
= mbox_send_message(ddata
->mb
[idx
].chan
,
410 dev_warn(&rproc
->dev
, "warning: remote FW shutdown without ack\n");
414 err
= stm32_rproc_set_hold_boot(rproc
, true);
418 err
= reset_control_assert(ddata
->rst
);
420 dev_err(&rproc
->dev
, "failed to assert the reset\n");
424 /* to allow platform Standby power mode, set remote proc Deep Sleep */
425 if (ddata
->pdds
.map
) {
426 err
= regmap_update_bits(ddata
->pdds
.map
, ddata
->pdds
.reg
,
427 ddata
->pdds
.mask
, 1);
429 dev_err(&rproc
->dev
, "failed to set pdds\n");
437 static void stm32_rproc_kick(struct rproc
*rproc
, int vqid
)
439 struct stm32_rproc
*ddata
= rproc
->priv
;
443 if (WARN_ON(vqid
>= MBOX_NB_VQ
))
446 for (i
= 0; i
< MBOX_NB_MBX
; i
++) {
447 if (vqid
!= ddata
->mb
[i
].vq_id
)
449 if (!ddata
->mb
[i
].chan
)
451 err
= mbox_send_message(ddata
->mb
[i
].chan
, (void *)(long)vqid
);
453 dev_err(&rproc
->dev
, "%s: failed (%s, err:%d)\n",
454 __func__
, ddata
->mb
[i
].name
, err
);
459 static struct rproc_ops st_rproc_ops
= {
460 .start
= stm32_rproc_start
,
461 .stop
= stm32_rproc_stop
,
462 .kick
= stm32_rproc_kick
,
463 .load
= rproc_elf_load_segments
,
464 .parse_fw
= stm32_rproc_parse_fw
,
465 .find_loaded_rsc_table
= rproc_elf_find_loaded_rsc_table
,
466 .sanity_check
= rproc_elf_sanity_check
,
467 .get_boot_addr
= rproc_elf_get_boot_addr
,
470 static const struct of_device_id stm32_rproc_match
[] = {
471 { .compatible
= "st,stm32mp1-m4" },
474 MODULE_DEVICE_TABLE(of
, stm32_rproc_match
);
476 static int stm32_rproc_get_syscon(struct device_node
*np
, const char *prop
,
477 struct stm32_syscon
*syscon
)
481 syscon
->map
= syscon_regmap_lookup_by_phandle(np
, prop
);
482 if (IS_ERR(syscon
->map
)) {
483 err
= PTR_ERR(syscon
->map
);
488 err
= of_property_read_u32_index(np
, prop
, 1, &syscon
->reg
);
492 err
= of_property_read_u32_index(np
, prop
, 2, &syscon
->mask
);
498 static int stm32_rproc_parse_dt(struct platform_device
*pdev
)
500 struct device
*dev
= &pdev
->dev
;
501 struct device_node
*np
= dev
->of_node
;
502 struct rproc
*rproc
= platform_get_drvdata(pdev
);
503 struct stm32_rproc
*ddata
= rproc
->priv
;
504 struct stm32_syscon tz
;
508 irq
= platform_get_irq(pdev
, 0);
510 err
= devm_request_irq(dev
, irq
, stm32_rproc_wdg
, 0,
511 dev_name(dev
), rproc
);
513 dev_err(dev
, "failed to request wdg irq\n");
517 dev_info(dev
, "wdg irq registered\n");
520 ddata
->rst
= devm_reset_control_get_by_index(dev
, 0);
521 if (IS_ERR(ddata
->rst
)) {
522 dev_err(dev
, "failed to get mcu reset\n");
523 return PTR_ERR(ddata
->rst
);
527 * if platform is secured the hold boot bit must be written by
528 * smc call and read normally.
529 * if not secure the hold boot bit could be read/write normally
531 err
= stm32_rproc_get_syscon(np
, "st,syscfg-tz", &tz
);
533 dev_err(dev
, "failed to get tz syscfg\n");
537 err
= regmap_read(tz
.map
, tz
.reg
, &tzen
);
539 dev_err(&rproc
->dev
, "failed to read tzen\n");
542 ddata
->secured_soc
= tzen
& tz
.mask
;
544 err
= stm32_rproc_get_syscon(np
, "st,syscfg-holdboot",
547 dev_err(dev
, "failed to get hold boot\n");
551 err
= stm32_rproc_get_syscon(np
, "st,syscfg-pdds", &ddata
->pdds
);
553 dev_warn(dev
, "failed to get pdds\n");
555 rproc
->auto_boot
= of_property_read_bool(np
, "st,auto-boot");
557 return stm32_rproc_of_memory_translations(rproc
);
560 static int stm32_rproc_probe(struct platform_device
*pdev
)
562 struct device
*dev
= &pdev
->dev
;
563 struct stm32_rproc
*ddata
;
564 struct device_node
*np
= dev
->of_node
;
568 ret
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(32));
572 rproc
= rproc_alloc(dev
, np
->name
, &st_rproc_ops
, NULL
, sizeof(*ddata
));
576 rproc
->has_iommu
= false;
579 platform_set_drvdata(pdev
, rproc
);
581 ret
= stm32_rproc_parse_dt(pdev
);
585 stm32_rproc_request_mbox(rproc
);
587 ret
= rproc_add(rproc
);
594 stm32_rproc_free_mbox(rproc
);
600 static int stm32_rproc_remove(struct platform_device
*pdev
)
602 struct rproc
*rproc
= platform_get_drvdata(pdev
);
604 if (atomic_read(&rproc
->power
) > 0)
605 rproc_shutdown(rproc
);
608 stm32_rproc_free_mbox(rproc
);
614 static struct platform_driver stm32_rproc_driver
= {
615 .probe
= stm32_rproc_probe
,
616 .remove
= stm32_rproc_remove
,
618 .name
= "stm32-rproc",
619 .of_match_table
= of_match_ptr(stm32_rproc_match
),
622 module_platform_driver(stm32_rproc_driver
);
624 MODULE_DESCRIPTION("STM32 Remote Processor Control Driver");
625 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
626 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
627 MODULE_LICENSE("GPL v2");