Skip to content

Commit 175cfcc

Browse files
lenghonglinxingrz
authored andcommitted
drivers: mbox: Add mailbox driver for SOPHGO CVI series
This implements a mailbox driver for CV18xx, a series of multi-core SoCs by SOPHGO. This driver enables the little core of CV18xx running RTOS to communicate with its big core running Linux. Signed-off-by: honglin leng <[email protected]> Signed-off-by: Chen Xingyu <[email protected]>
1 parent 34255cb commit 175cfcc

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed

drivers/mbox/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ zephyr_library_sources_ifdef(CONFIG_MBOX_IVSHMEM mbox_ivshmem.c)
2222
zephyr_library_sources_ifdef(CONFIG_MBOX_TI_OMAP_MAILBOX mbox_ti_omap.c)
2323
zephyr_library_sources_ifdef(CONFIG_MBOX_RENESAS_RZ_MHU mbox_renesas_rz_mhu.c)
2424
zephyr_library_sources_ifdef(CONFIG_MBOX_MHUV3 mbox_mhuv3.c)
25+
zephyr_library_sources_ifdef(CONFIG_MBOX_SOPHGO_CVI mbox_sophgo_cvi.c)

drivers/mbox/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ source "drivers/mbox/Kconfig.ivshmem"
2626
source "drivers/mbox/Kconfig.ti_omap"
2727
source "drivers/mbox/Kconfig.renesas_rz"
2828
source "drivers/mbox/Kconfig.mhuv3"
29+
source "drivers/mbox/Kconfig.sophgo"
2930

3031

3132
config MBOX_INIT_PRIORITY

drivers/mbox/Kconfig.sophgo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2024 honglin leng <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config MBOX_SOPHGO_CVI
5+
bool "SOPHGO CVI series Mailbox controller"
6+
default y
7+
depends on DT_HAS_SOPHGO_CVI_MAILBOX_ENABLED
8+
help
9+
Enable driver for the SOPHGO CVI series Mailbox.

drivers/mbox/mbox_sophgo_cvi.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2024 honglin leng <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/drivers/mbox.h>
7+
#include <zephyr/irq.h>
8+
#include <string.h>
9+
10+
#define DT_DRV_COMPAT sophgo_cvi_mailbox
11+
12+
#define MBOX_BASE DT_INST_REG_ADDR(0)
13+
#define MBOX_TX_CPU DT_INST_PROP(0, tx_cpu)
14+
#define MBOX_RX_CPU DT_INST_PROP(0, rx_cpu)
15+
16+
#define MBOX_INT_ENABLE(cpu) (MBOX_BASE + 0x00 + (0x04 * cpu))
17+
#define MBOX_INT_CLEAR(cpu) (MBOX_BASE + 0x10 + (0x10 * cpu))
18+
#define MBOX_INT_DONE(cpu) (MBOX_BASE + 0x18 + (0x10 * cpu))
19+
#define MBOX_INT_TRIGER (MBOX_BASE + 0x60)
20+
#define MBOX_BUFFER (MBOX_BASE + 0x400)
21+
22+
#define MBOX_MAX_NUM 8
23+
24+
struct mbox_cvi_data {
25+
mbox_callback_t cb[MBOX_MAX_NUM];
26+
void *user_data[MBOX_MAX_NUM];
27+
};
28+
29+
static struct mbox_cvi_data mbox_data;
30+
31+
static void mbox_isr(const struct device *dev)
32+
{
33+
ARG_UNUSED(dev);
34+
35+
uint8_t set_val;
36+
uint8_t valid_val;
37+
uint8_t tmp_valid_val;
38+
struct mbox_msg msg;
39+
40+
set_val = sys_read8(MBOX_INT_DONE(MBOX_RX_CPU));
41+
if (set_val) {
42+
for (int i = 0; i < MBOX_MAX_NUM; i++) {
43+
valid_val = set_val & BIT(i);
44+
if (valid_val) {
45+
msg.data = (const unsigned long *)MBOX_BUFFER + i;
46+
sys_write8(valid_val, MBOX_INT_CLEAR(MBOX_RX_CPU));
47+
tmp_valid_val = sys_read8(MBOX_INT_ENABLE(MBOX_RX_CPU));
48+
tmp_valid_val &= ~valid_val;
49+
sys_write8(tmp_valid_val, MBOX_INT_ENABLE(MBOX_RX_CPU));
50+
if (mbox_data.cb[i] != NULL) {
51+
mbox_data.cb[i](dev, i, mbox_data.user_data[i], &msg);
52+
*(unsigned long *)msg.data = 0x0;
53+
}
54+
}
55+
}
56+
}
57+
}
58+
59+
static int mbox_cvi_send(const struct device *dev, uint32_t channel, const struct mbox_msg *msg)
60+
{
61+
uint8_t tmp_mbox_info;
62+
63+
ARG_UNUSED(dev);
64+
65+
memcpy((unsigned long *)MBOX_BUFFER + channel, msg->data, msg->size);
66+
sys_write8(BIT(channel), MBOX_INT_CLEAR(MBOX_TX_CPU));
67+
tmp_mbox_info = sys_read8(MBOX_INT_ENABLE(MBOX_TX_CPU));
68+
tmp_mbox_info |= BIT(channel);
69+
sys_write8(tmp_mbox_info, MBOX_INT_ENABLE(MBOX_TX_CPU));
70+
sys_write8(BIT(channel), MBOX_INT_TRIGER);
71+
72+
return 0;
73+
}
74+
75+
static int mbox_cvi_register_callback(const struct device *dev, uint32_t channel,
76+
mbox_callback_t cb, void *user_data)
77+
{
78+
ARG_UNUSED(dev);
79+
80+
if (channel >= MBOX_MAX_NUM) {
81+
return -EINVAL;
82+
}
83+
84+
mbox_data.cb[channel] = cb;
85+
mbox_data.user_data[channel] = user_data;
86+
87+
return 0;
88+
}
89+
90+
static int mbox_cvi_mtu_get(const struct device *dev)
91+
{
92+
ARG_UNUSED(dev);
93+
94+
/* We only support signalling */
95+
return 0;
96+
}
97+
98+
static uint32_t mbox_cvi_max_channels_get(const struct device *dev)
99+
{
100+
ARG_UNUSED(dev);
101+
102+
return MBOX_MAX_NUM;
103+
}
104+
105+
static int mbox_cvi_set_enabled(const struct device *dev, uint32_t channel, bool enable)
106+
{
107+
ARG_UNUSED(dev);
108+
ARG_UNUSED(channel);
109+
ARG_UNUSED(enable);
110+
111+
return 0;
112+
}
113+
114+
static int mbox_cvi_init(const struct device *dev)
115+
{
116+
ARG_UNUSED(dev);
117+
118+
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), mbox_isr, DEVICE_DT_INST_GET(0), 0);
119+
irq_enable(DT_INST_IRQN(0));
120+
121+
return 0;
122+
}
123+
124+
static DEVICE_API(mbox, mbox_cvi_driver_api) = {
125+
.send = mbox_cvi_send,
126+
.register_callback = mbox_cvi_register_callback,
127+
.mtu_get = mbox_cvi_mtu_get,
128+
.max_channels_get = mbox_cvi_max_channels_get,
129+
.set_enabled = mbox_cvi_set_enabled,
130+
};
131+
132+
DEVICE_DT_INST_DEFINE(0, mbox_cvi_init, NULL, NULL, NULL, POST_KERNEL, CONFIG_MBOX_INIT_PRIORITY,
133+
&mbox_cvi_driver_api);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) 2024 honglin leng <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: SOPHGO CVI series Mailbox controller
5+
6+
compatible: "sophgo,cvi-mailbox"
7+
8+
include: [base.yaml, mailbox-controller.yaml]
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
interrupts:
15+
required: true
16+
17+
channel-max:
18+
type: int
19+
required: true
20+
description: Supported channels max
21+
22+
rx-cpu:
23+
type: int
24+
enum: [1, 2, 3, 4]
25+
description: |
26+
Receiver CPU Index.
27+
28+
tx-cpu:
29+
type: int
30+
enum: [1, 2, 3, 4]
31+
description: |
32+
Sender CPU Index.The CVI platform has a total of 4 CPUs, each with its own
33+
mailbox.
34+
35+
The mailbox controller can be used to send messages between CPUs.
36+
37+
mbox-cells:
38+
- channel

dts/riscv/sophgo/cv180x.dtsi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
soc {
1414
compatible = "sophgo,cv180x", "simple-bus";
1515

16+
mbox: mailbox@1900000 {
17+
compatible = "sophgo,cvi-mailbox";
18+
reg = <0x1900000 DT_SIZE_K(4)>;
19+
interrupts = <61 1>;
20+
channel-max = <8>;
21+
tx-cpu = <1>;
22+
rx-cpu = <2>;
23+
#mbox-cells = <1>;
24+
status = "disabled";
25+
};
26+
1627
pinctrl: pin-controller@3001000 {
1728
compatible = "sophgo,cvi-pinctrl";
1829
reg = <0x3001000 DT_SIZE_K(4)>;

dts/riscv/sophgo/cv181x.dtsi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
soc {
1414
compatible = "sophgo,cv181x", "simple-bus";
1515

16+
mbox: mailbox@1900000 {
17+
compatible = "sophgo,cvi-mailbox";
18+
reg = <0x1900000 DT_SIZE_K(4)>;
19+
interrupts = <61 1>;
20+
channel-max = <8>;
21+
tx-cpu = <1>;
22+
rx-cpu = <2>;
23+
#mbox-cells = <1>;
24+
status = "disabled";
25+
};
26+
1627
pinctrl: pin-controller@3001000 {
1728
compatible = "sophgo,cvi-pinctrl";
1829
reg = <0x3001000 DT_SIZE_K(4)>;

0 commit comments

Comments
 (0)