1 // SPDX-License-Identifier: GPL-2.0-only
3 * ST's Remote Processor Control Driver
5 * Copyright (C) 2015 STMicroelectronics - All Rights Reserved
7 * Author: Ludovic Barre <ludovic.barre@st.com>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mailbox_client.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
19 #include <linux/of_reserved_mem.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <linux/remoteproc.h>
24 #include <linux/reset.h>
26 #include "remoteproc_internal.h"
28 #define ST_RPROC_VQ0 0
29 #define ST_RPROC_VQ1 1
30 #define ST_RPROC_MAX_VRING 2
36 struct st_rproc_config
{
39 unsigned long bootaddr_mask
;
43 struct st_rproc_config
*config
;
44 struct reset_control
*sw_reset
;
45 struct reset_control
*pwr_reset
;
48 struct regmap
*boot_base
;
50 struct mbox_chan
*mbox_chan
[ST_RPROC_MAX_VRING
* MBOX_MAX
];
51 struct mbox_client mbox_client_vq0
;
52 struct mbox_client mbox_client_vq1
;
55 static void st_rproc_mbox_callback(struct device
*dev
, u32 msg
)
57 struct rproc
*rproc
= dev_get_drvdata(dev
);
59 if (rproc_vq_interrupt(rproc
, msg
) == IRQ_NONE
)
60 dev_dbg(dev
, "no message was found in vqid %d\n", msg
);
64 void st_rproc_mbox_callback_vq0(struct mbox_client
*mbox_client
, void *data
)
66 st_rproc_mbox_callback(mbox_client
->dev
, 0);
70 void st_rproc_mbox_callback_vq1(struct mbox_client
*mbox_client
, void *data
)
72 st_rproc_mbox_callback(mbox_client
->dev
, 1);
75 static void st_rproc_kick(struct rproc
*rproc
, int vqid
)
77 struct st_rproc
*ddata
= rproc
->priv
;
78 struct device
*dev
= rproc
->dev
.parent
;
81 /* send the index of the triggered virtqueue in the mailbox payload */
82 if (WARN_ON(vqid
>= ST_RPROC_MAX_VRING
))
85 ret
= mbox_send_message(ddata
->mbox_chan
[vqid
* MBOX_MAX
+ MBOX_TX
],
88 dev_err(dev
, "failed to send message via mbox: %d\n", ret
);
91 static int st_rproc_mem_alloc(struct rproc
*rproc
,
92 struct rproc_mem_entry
*mem
)
94 struct device
*dev
= rproc
->dev
.parent
;
97 va
= ioremap_wc(mem
->dma
, mem
->len
);
99 dev_err(dev
, "Unable to map memory region: %pa+%zx\n",
100 &mem
->dma
, mem
->len
);
104 /* Update memory entry va */
110 static int st_rproc_mem_release(struct rproc
*rproc
,
111 struct rproc_mem_entry
*mem
)
118 static int st_rproc_parse_fw(struct rproc
*rproc
, const struct firmware
*fw
)
120 struct device
*dev
= rproc
->dev
.parent
;
121 struct device_node
*np
= dev
->of_node
;
122 struct rproc_mem_entry
*mem
;
123 struct reserved_mem
*rmem
;
124 struct of_phandle_iterator it
;
127 of_phandle_iterator_init(&it
, np
, "memory-region", NULL
, 0);
128 while (of_phandle_iterator_next(&it
) == 0) {
129 rmem
= of_reserved_mem_lookup(it
.node
);
131 of_node_put(it
.node
);
132 dev_err(dev
, "unable to acquire memory-region\n");
136 /* No need to map vdev buffer */
137 if (strcmp(it
.node
->name
, "vdev0buffer")) {
138 /* Register memory region */
139 mem
= rproc_mem_entry_init(dev
, NULL
,
140 (dma_addr_t
)rmem
->base
,
141 rmem
->size
, rmem
->base
,
143 st_rproc_mem_release
,
146 /* Register reserved memory for vdev buffer allocation */
147 mem
= rproc_of_resm_mem_entry_init(dev
, index
,
154 of_node_put(it
.node
);
158 rproc_add_carveout(rproc
, mem
);
162 return rproc_elf_load_rsc_table(rproc
, fw
);
165 static int st_rproc_start(struct rproc
*rproc
)
167 struct st_rproc
*ddata
= rproc
->priv
;
170 regmap_update_bits(ddata
->boot_base
, ddata
->boot_offset
,
171 ddata
->config
->bootaddr_mask
, rproc
->bootaddr
);
173 err
= clk_enable(ddata
->clk
);
175 dev_err(&rproc
->dev
, "Failed to enable clock\n");
179 if (ddata
->config
->sw_reset
) {
180 err
= reset_control_deassert(ddata
->sw_reset
);
182 dev_err(&rproc
->dev
, "Failed to deassert S/W Reset\n");
187 if (ddata
->config
->pwr_reset
) {
188 err
= reset_control_deassert(ddata
->pwr_reset
);
190 dev_err(&rproc
->dev
, "Failed to deassert Power Reset\n");
195 dev_info(&rproc
->dev
, "Started from 0x%llx\n", rproc
->bootaddr
);
201 if (ddata
->config
->pwr_reset
)
202 reset_control_assert(ddata
->sw_reset
);
204 clk_disable(ddata
->clk
);
209 static int st_rproc_stop(struct rproc
*rproc
)
211 struct st_rproc
*ddata
= rproc
->priv
;
212 int sw_err
= 0, pwr_err
= 0;
214 if (ddata
->config
->sw_reset
) {
215 sw_err
= reset_control_assert(ddata
->sw_reset
);
217 dev_err(&rproc
->dev
, "Failed to assert S/W Reset\n");
220 if (ddata
->config
->pwr_reset
) {
221 pwr_err
= reset_control_assert(ddata
->pwr_reset
);
223 dev_err(&rproc
->dev
, "Failed to assert Power Reset\n");
226 clk_disable(ddata
->clk
);
228 return sw_err
?: pwr_err
;
231 static const struct rproc_ops st_rproc_ops
= {
232 .kick
= st_rproc_kick
,
233 .start
= st_rproc_start
,
234 .stop
= st_rproc_stop
,
235 .parse_fw
= st_rproc_parse_fw
,
236 .load
= rproc_elf_load_segments
,
237 .find_loaded_rsc_table
= rproc_elf_find_loaded_rsc_table
,
238 .sanity_check
= rproc_elf_sanity_check
,
239 .get_boot_addr
= rproc_elf_get_boot_addr
,
243 * Fetch state of the processor: 0 is off, 1 is on.
245 static int st_rproc_state(struct platform_device
*pdev
)
247 struct rproc
*rproc
= platform_get_drvdata(pdev
);
248 struct st_rproc
*ddata
= rproc
->priv
;
249 int reset_sw
= 0, reset_pwr
= 0;
251 if (ddata
->config
->sw_reset
)
252 reset_sw
= reset_control_status(ddata
->sw_reset
);
254 if (ddata
->config
->pwr_reset
)
255 reset_pwr
= reset_control_status(ddata
->pwr_reset
);
257 if (reset_sw
< 0 || reset_pwr
< 0)
260 return !reset_sw
&& !reset_pwr
;
263 static const struct st_rproc_config st40_rproc_cfg
= {
266 .bootaddr_mask
= GENMASK(28, 1),
269 static const struct st_rproc_config st231_rproc_cfg
= {
272 .bootaddr_mask
= GENMASK(31, 6),
275 static const struct of_device_id st_rproc_match
[] = {
276 { .compatible
= "st,st40-rproc", .data
= &st40_rproc_cfg
},
277 { .compatible
= "st,st231-rproc", .data
= &st231_rproc_cfg
},
280 MODULE_DEVICE_TABLE(of
, st_rproc_match
);
282 static int st_rproc_parse_dt(struct platform_device
*pdev
)
284 struct device
*dev
= &pdev
->dev
;
285 struct rproc
*rproc
= platform_get_drvdata(pdev
);
286 struct st_rproc
*ddata
= rproc
->priv
;
287 struct device_node
*np
= dev
->of_node
;
290 if (ddata
->config
->sw_reset
) {
291 ddata
->sw_reset
= devm_reset_control_get_exclusive(dev
,
293 if (IS_ERR(ddata
->sw_reset
)) {
294 dev_err(dev
, "Failed to get S/W Reset\n");
295 return PTR_ERR(ddata
->sw_reset
);
299 if (ddata
->config
->pwr_reset
) {
300 ddata
->pwr_reset
= devm_reset_control_get_exclusive(dev
,
302 if (IS_ERR(ddata
->pwr_reset
)) {
303 dev_err(dev
, "Failed to get Power Reset\n");
304 return PTR_ERR(ddata
->pwr_reset
);
308 ddata
->clk
= devm_clk_get(dev
, NULL
);
309 if (IS_ERR(ddata
->clk
)) {
310 dev_err(dev
, "Failed to get clock\n");
311 return PTR_ERR(ddata
->clk
);
314 err
= of_property_read_u32(np
, "clock-frequency", &ddata
->clk_rate
);
316 dev_err(dev
, "failed to get clock frequency\n");
320 ddata
->boot_base
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
321 if (IS_ERR(ddata
->boot_base
)) {
322 dev_err(dev
, "Boot base not found\n");
323 return PTR_ERR(ddata
->boot_base
);
326 err
= of_property_read_u32_index(np
, "st,syscfg", 1,
327 &ddata
->boot_offset
);
329 dev_err(dev
, "Boot offset not found\n");
333 err
= clk_prepare(ddata
->clk
);
335 dev_err(dev
, "failed to get clock\n");
340 static int st_rproc_probe(struct platform_device
*pdev
)
342 struct device
*dev
= &pdev
->dev
;
343 struct st_rproc
*ddata
;
344 struct device_node
*np
= dev
->of_node
;
346 struct mbox_chan
*chan
;
350 rproc
= devm_rproc_alloc(dev
, np
->name
, &st_rproc_ops
, NULL
, sizeof(*ddata
));
354 rproc
->has_iommu
= false;
356 ddata
->config
= (struct st_rproc_config
*)device_get_match_data(dev
);
360 platform_set_drvdata(pdev
, rproc
);
362 ret
= st_rproc_parse_dt(pdev
);
366 enabled
= st_rproc_state(pdev
);
373 atomic_inc(&rproc
->power
);
374 rproc
->state
= RPROC_RUNNING
;
376 clk_set_rate(ddata
->clk
, ddata
->clk_rate
);
379 if (of_property_present(np
, "mbox-names")) {
380 ddata
->mbox_client_vq0
.dev
= dev
;
381 ddata
->mbox_client_vq0
.tx_done
= NULL
;
382 ddata
->mbox_client_vq0
.tx_block
= false;
383 ddata
->mbox_client_vq0
.knows_txdone
= false;
384 ddata
->mbox_client_vq0
.rx_callback
= st_rproc_mbox_callback_vq0
;
386 ddata
->mbox_client_vq1
.dev
= dev
;
387 ddata
->mbox_client_vq1
.tx_done
= NULL
;
388 ddata
->mbox_client_vq1
.tx_block
= false;
389 ddata
->mbox_client_vq1
.knows_txdone
= false;
390 ddata
->mbox_client_vq1
.rx_callback
= st_rproc_mbox_callback_vq1
;
393 * To control a co-processor without IPC mechanism.
394 * This driver can be used without mbox and rpmsg.
396 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq0
, "vq0_rx");
398 dev_err(&rproc
->dev
, "failed to request mbox chan 0\n");
402 ddata
->mbox_chan
[ST_RPROC_VQ0
* MBOX_MAX
+ MBOX_RX
] = chan
;
404 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq0
, "vq0_tx");
406 dev_err(&rproc
->dev
, "failed to request mbox chan 0\n");
410 ddata
->mbox_chan
[ST_RPROC_VQ0
* MBOX_MAX
+ MBOX_TX
] = chan
;
412 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq1
, "vq1_rx");
414 dev_err(&rproc
->dev
, "failed to request mbox chan 1\n");
418 ddata
->mbox_chan
[ST_RPROC_VQ1
* MBOX_MAX
+ MBOX_RX
] = chan
;
420 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq1
, "vq1_tx");
422 dev_err(&rproc
->dev
, "failed to request mbox chan 1\n");
426 ddata
->mbox_chan
[ST_RPROC_VQ1
* MBOX_MAX
+ MBOX_TX
] = chan
;
429 ret
= rproc_add(rproc
);
436 for (i
= 0; i
< ST_RPROC_MAX_VRING
* MBOX_MAX
; i
++)
437 mbox_free_channel(ddata
->mbox_chan
[i
]);
439 clk_unprepare(ddata
->clk
);
444 static void st_rproc_remove(struct platform_device
*pdev
)
446 struct rproc
*rproc
= platform_get_drvdata(pdev
);
447 struct st_rproc
*ddata
= rproc
->priv
;
452 clk_disable_unprepare(ddata
->clk
);
454 for (i
= 0; i
< ST_RPROC_MAX_VRING
* MBOX_MAX
; i
++)
455 mbox_free_channel(ddata
->mbox_chan
[i
]);
458 static struct platform_driver st_rproc_driver
= {
459 .probe
= st_rproc_probe
,
460 .remove
= st_rproc_remove
,
463 .of_match_table
= of_match_ptr(st_rproc_match
),
466 module_platform_driver(st_rproc_driver
);
468 MODULE_DESCRIPTION("ST Remote Processor Control Driver");
469 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
470 MODULE_LICENSE("GPL v2");