2 * ST's Remote Processor Control Driver
4 * Copyright (C) 2015 STMicroelectronics - All Rights Reserved
6 * Author: Ludovic Barre <ludovic.barre@st.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mailbox_client.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/of_reserved_mem.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/remoteproc.h>
27 #include <linux/reset.h>
29 #include "remoteproc_internal.h"
31 #define ST_RPROC_VQ0 0
32 #define ST_RPROC_VQ1 1
33 #define ST_RPROC_MAX_VRING 2
39 struct st_rproc_config
{
42 unsigned long bootaddr_mask
;
46 struct st_rproc_config
*config
;
47 struct reset_control
*sw_reset
;
48 struct reset_control
*pwr_reset
;
51 struct regmap
*boot_base
;
53 struct mbox_chan
*mbox_chan
[ST_RPROC_MAX_VRING
* MBOX_MAX
];
54 struct mbox_client mbox_client_vq0
;
55 struct mbox_client mbox_client_vq1
;
58 static void st_rproc_mbox_callback(struct device
*dev
, u32 msg
)
60 struct rproc
*rproc
= dev_get_drvdata(dev
);
62 if (rproc_vq_interrupt(rproc
, msg
) == IRQ_NONE
)
63 dev_dbg(dev
, "no message was found in vqid %d\n", msg
);
67 void st_rproc_mbox_callback_vq0(struct mbox_client
*mbox_client
, void *data
)
69 st_rproc_mbox_callback(mbox_client
->dev
, 0);
73 void st_rproc_mbox_callback_vq1(struct mbox_client
*mbox_client
, void *data
)
75 st_rproc_mbox_callback(mbox_client
->dev
, 1);
78 static void st_rproc_kick(struct rproc
*rproc
, int vqid
)
80 struct st_rproc
*ddata
= rproc
->priv
;
81 struct device
*dev
= rproc
->dev
.parent
;
84 /* send the index of the triggered virtqueue in the mailbox payload */
85 if (WARN_ON(vqid
>= ST_RPROC_MAX_VRING
))
88 ret
= mbox_send_message(ddata
->mbox_chan
[vqid
* MBOX_MAX
+ MBOX_TX
],
91 dev_err(dev
, "failed to send message via mbox: %d\n", ret
);
94 static int st_rproc_start(struct rproc
*rproc
)
96 struct st_rproc
*ddata
= rproc
->priv
;
99 regmap_update_bits(ddata
->boot_base
, ddata
->boot_offset
,
100 ddata
->config
->bootaddr_mask
, rproc
->bootaddr
);
102 err
= clk_enable(ddata
->clk
);
104 dev_err(&rproc
->dev
, "Failed to enable clock\n");
108 if (ddata
->config
->sw_reset
) {
109 err
= reset_control_deassert(ddata
->sw_reset
);
111 dev_err(&rproc
->dev
, "Failed to deassert S/W Reset\n");
116 if (ddata
->config
->pwr_reset
) {
117 err
= reset_control_deassert(ddata
->pwr_reset
);
119 dev_err(&rproc
->dev
, "Failed to deassert Power Reset\n");
124 dev_info(&rproc
->dev
, "Started from 0x%x\n", rproc
->bootaddr
);
130 if (ddata
->config
->pwr_reset
)
131 reset_control_assert(ddata
->sw_reset
);
133 clk_disable(ddata
->clk
);
138 static int st_rproc_stop(struct rproc
*rproc
)
140 struct st_rproc
*ddata
= rproc
->priv
;
141 int sw_err
= 0, pwr_err
= 0;
143 if (ddata
->config
->sw_reset
) {
144 sw_err
= reset_control_assert(ddata
->sw_reset
);
146 dev_err(&rproc
->dev
, "Failed to assert S/W Reset\n");
149 if (ddata
->config
->pwr_reset
) {
150 pwr_err
= reset_control_assert(ddata
->pwr_reset
);
152 dev_err(&rproc
->dev
, "Failed to assert Power Reset\n");
155 clk_disable(ddata
->clk
);
157 return sw_err
?: pwr_err
;
160 static const struct rproc_ops st_rproc_ops
= {
161 .kick
= st_rproc_kick
,
162 .start
= st_rproc_start
,
163 .stop
= st_rproc_stop
,
167 * Fetch state of the processor: 0 is off, 1 is on.
169 static int st_rproc_state(struct platform_device
*pdev
)
171 struct rproc
*rproc
= platform_get_drvdata(pdev
);
172 struct st_rproc
*ddata
= rproc
->priv
;
173 int reset_sw
= 0, reset_pwr
= 0;
175 if (ddata
->config
->sw_reset
)
176 reset_sw
= reset_control_status(ddata
->sw_reset
);
178 if (ddata
->config
->pwr_reset
)
179 reset_pwr
= reset_control_status(ddata
->pwr_reset
);
181 if (reset_sw
< 0 || reset_pwr
< 0)
184 return !reset_sw
&& !reset_pwr
;
187 static const struct st_rproc_config st40_rproc_cfg
= {
190 .bootaddr_mask
= GENMASK(28, 1),
193 static const struct st_rproc_config st231_rproc_cfg
= {
196 .bootaddr_mask
= GENMASK(31, 6),
199 static const struct of_device_id st_rproc_match
[] = {
200 { .compatible
= "st,st40-rproc", .data
= &st40_rproc_cfg
},
201 { .compatible
= "st,st231-rproc", .data
= &st231_rproc_cfg
},
204 MODULE_DEVICE_TABLE(of
, st_rproc_match
);
206 static int st_rproc_parse_dt(struct platform_device
*pdev
)
208 struct device
*dev
= &pdev
->dev
;
209 struct rproc
*rproc
= platform_get_drvdata(pdev
);
210 struct st_rproc
*ddata
= rproc
->priv
;
211 struct device_node
*np
= dev
->of_node
;
214 if (ddata
->config
->sw_reset
) {
215 ddata
->sw_reset
= devm_reset_control_get(dev
, "sw_reset");
216 if (IS_ERR(ddata
->sw_reset
)) {
217 dev_err(dev
, "Failed to get S/W Reset\n");
218 return PTR_ERR(ddata
->sw_reset
);
222 if (ddata
->config
->pwr_reset
) {
223 ddata
->pwr_reset
= devm_reset_control_get(dev
, "pwr_reset");
224 if (IS_ERR(ddata
->pwr_reset
)) {
225 dev_err(dev
, "Failed to get Power Reset\n");
226 return PTR_ERR(ddata
->pwr_reset
);
230 ddata
->clk
= devm_clk_get(dev
, NULL
);
231 if (IS_ERR(ddata
->clk
)) {
232 dev_err(dev
, "Failed to get clock\n");
233 return PTR_ERR(ddata
->clk
);
236 err
= of_property_read_u32(np
, "clock-frequency", &ddata
->clk_rate
);
238 dev_err(dev
, "failed to get clock frequency\n");
242 ddata
->boot_base
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
243 if (IS_ERR(ddata
->boot_base
)) {
244 dev_err(dev
, "Boot base not found\n");
245 return PTR_ERR(ddata
->boot_base
);
248 err
= of_property_read_u32_index(np
, "st,syscfg", 1,
249 &ddata
->boot_offset
);
251 dev_err(dev
, "Boot offset not found\n");
255 err
= of_reserved_mem_device_init(dev
);
257 dev_err(dev
, "Failed to obtain shared memory\n");
261 err
= clk_prepare(ddata
->clk
);
263 dev_err(dev
, "failed to get clock\n");
268 static int st_rproc_probe(struct platform_device
*pdev
)
270 struct device
*dev
= &pdev
->dev
;
271 const struct of_device_id
*match
;
272 struct st_rproc
*ddata
;
273 struct device_node
*np
= dev
->of_node
;
275 struct mbox_chan
*chan
;
279 match
= of_match_device(st_rproc_match
, dev
);
280 if (!match
|| !match
->data
) {
281 dev_err(dev
, "No device match found\n");
285 rproc
= rproc_alloc(dev
, np
->name
, &st_rproc_ops
, NULL
, sizeof(*ddata
));
289 rproc
->has_iommu
= false;
291 ddata
->config
= (struct st_rproc_config
*)match
->data
;
293 platform_set_drvdata(pdev
, rproc
);
295 ret
= st_rproc_parse_dt(pdev
);
299 enabled
= st_rproc_state(pdev
);
306 atomic_inc(&rproc
->power
);
307 rproc
->state
= RPROC_RUNNING
;
309 clk_set_rate(ddata
->clk
, ddata
->clk_rate
);
312 if (of_get_property(np
, "mbox-names", NULL
)) {
313 ddata
->mbox_client_vq0
.dev
= dev
;
314 ddata
->mbox_client_vq0
.tx_done
= NULL
;
315 ddata
->mbox_client_vq0
.tx_block
= false;
316 ddata
->mbox_client_vq0
.knows_txdone
= false;
317 ddata
->mbox_client_vq0
.rx_callback
= st_rproc_mbox_callback_vq0
;
319 ddata
->mbox_client_vq1
.dev
= dev
;
320 ddata
->mbox_client_vq1
.tx_done
= NULL
;
321 ddata
->mbox_client_vq1
.tx_block
= false;
322 ddata
->mbox_client_vq1
.knows_txdone
= false;
323 ddata
->mbox_client_vq1
.rx_callback
= st_rproc_mbox_callback_vq1
;
326 * To control a co-processor without IPC mechanism.
327 * This driver can be used without mbox and rpmsg.
329 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq0
, "vq0_rx");
331 dev_err(&rproc
->dev
, "failed to request mbox chan 0\n");
335 ddata
->mbox_chan
[ST_RPROC_VQ0
* MBOX_MAX
+ MBOX_RX
] = chan
;
337 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq0
, "vq0_tx");
339 dev_err(&rproc
->dev
, "failed to request mbox chan 0\n");
343 ddata
->mbox_chan
[ST_RPROC_VQ0
* MBOX_MAX
+ MBOX_TX
] = chan
;
345 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq1
, "vq1_rx");
347 dev_err(&rproc
->dev
, "failed to request mbox chan 1\n");
351 ddata
->mbox_chan
[ST_RPROC_VQ1
* MBOX_MAX
+ MBOX_RX
] = chan
;
353 chan
= mbox_request_channel_byname(&ddata
->mbox_client_vq1
, "vq1_tx");
355 dev_err(&rproc
->dev
, "failed to request mbox chan 1\n");
359 ddata
->mbox_chan
[ST_RPROC_VQ1
* MBOX_MAX
+ MBOX_TX
] = chan
;
362 ret
= rproc_add(rproc
);
369 for (i
= 0; i
< ST_RPROC_MAX_VRING
* MBOX_MAX
; i
++)
370 mbox_free_channel(ddata
->mbox_chan
[i
]);
372 clk_unprepare(ddata
->clk
);
378 static int st_rproc_remove(struct platform_device
*pdev
)
380 struct rproc
*rproc
= platform_get_drvdata(pdev
);
381 struct st_rproc
*ddata
= rproc
->priv
;
386 clk_disable_unprepare(ddata
->clk
);
388 of_reserved_mem_device_release(&pdev
->dev
);
390 for (i
= 0; i
< ST_RPROC_MAX_VRING
* MBOX_MAX
; i
++)
391 mbox_free_channel(ddata
->mbox_chan
[i
]);
398 static struct platform_driver st_rproc_driver
= {
399 .probe
= st_rproc_probe
,
400 .remove
= st_rproc_remove
,
403 .of_match_table
= of_match_ptr(st_rproc_match
),
406 module_platform_driver(st_rproc_driver
);
408 MODULE_DESCRIPTION("ST Remote Processor Control Driver");
409 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
410 MODULE_LICENSE("GPL v2");