1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-26 GuEe-GUI first version
*/
#include <rthw.h>
#include <drivers/mailbox.h>
#include <dt-bindings/size.h>
#define DBG_TAG "scmi.agent.mailbox"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include "agent.h"
struct scmi_agent_mailbox
{
struct rt_mbox_client mbox_client;
struct rt_mbox_chan *chan;
struct scmi_shared_mem *shmem;
struct rt_spinlock lock;
};
#define raw_to_scmi_mailbox(raw) rt_container_of(raw, struct scmi_agent_mailbox, mbox_client)
static void scmi_mailbox_rx_callback(struct rt_mbox_client *client, void *data)
{
struct rt_scmi_msg *msg = data;
if (msg->rx_callback)
{
msg->rx_callback(msg->sdev, msg->out_msg, msg->out_msg_size);
}
}
static void scmi_mailbox_tx_prepare(struct rt_mbox_client *client, const void *data)
{
struct rt_scmi_msg *msg = (void *)data;
struct scmi_agent_mailbox *ambox = raw_to_scmi_mailbox(client);
scmi_shmem_msg_write(ambox->shmem, msg);
}
static void scmi_mailbox_tx_done(struct rt_mbox_client *client, const void *data,
rt_err_t err)
{
struct scmi_agent_mailbox *ambox = raw_to_scmi_mailbox(client);
if (!err)
{
scmi_shmem_clear_channel(ambox->shmem);
}
}
static rt_err_t scmi_agent_mailbox_setup(struct scmi_agent *agent,
struct rt_device *dev)
{
rt_err_t err;
rt_uint64_t shm_addr, shm_size;
int mbox_chan, mbox_count, shmem_count;
struct rt_ofw_node *np = dev->ofw_node, *shmem_np;
struct scmi_agent_mailbox *ambox = rt_calloc(1, sizeof(*ambox));
if (!ambox)
{
return -RT_ENOMEM;
}
mbox_count = rt_ofw_count_phandle_cells(np, "mboxes", "#mbox-cells");
shmem_count = rt_ofw_count_phandle_cells(np, "shmem", RT_NULL);
if (mbox_count < 0)
{
err = mbox_count;
goto _fail;
}
if (shmem_count < 0)
{
err = shmem_count;
goto _fail;
}
mbox_chan = 0;
if (mbox_count == 2 && shmem_count == 2)
{
mbox_chan = 1;
}
else if (mbox_count == 3)
{
mbox_chan = 2;
}
ambox->mbox_client.dev = dev;
ambox->mbox_client.rx_callback = scmi_mailbox_rx_callback;
ambox->mbox_client.tx_prepare = scmi_mailbox_tx_prepare;
ambox->mbox_client.tx_done = scmi_mailbox_tx_done;
ambox->chan = rt_mbox_request_by_index(&ambox->mbox_client, mbox_chan);
if (rt_is_err_or_null(ambox->chan))
{
err = -RT_EIO;
goto _fail;
}
shmem_np = rt_ofw_parse_phandle(np, "shmem", 0);
if (!rt_ofw_node_is_compatible(shmem_np, "arm,scmi-shmem"))
{
err = -RT_EINVAL;
rt_ofw_node_put(shmem_np);
goto _fail;
}
if ((err = rt_ofw_get_address(shmem_np, 0, &shm_addr, &shm_size)))
{
rt_ofw_node_put(shmem_np);
goto _fail;
}
rt_ofw_node_put(shmem_np);
ambox->shmem = rt_ioremap((void *)shm_addr, shm_size);
if (!ambox->shmem)
{
err = -RT_EIO;
goto _fail;
}
agent->priv = ambox;
return RT_EOK;
_fail:
if (!rt_is_err_or_null(ambox->chan))
{
rt_mbox_release(ambox->chan);
}
if (ambox->shmem)
{
rt_iounmap(ambox->shmem);
}
rt_free(ambox);
return err;
}
static rt_err_t scmi_agent_mailbox_process_msg(struct scmi_agent *agent,
struct rt_scmi_msg *msg)
{
rt_err_t err;
struct scmi_agent_mailbox *ambox = agent->priv;
rt_hw_spin_lock(&ambox->lock.lock);
err = rt_mbox_send(ambox->chan, (const void *)msg, 30);
rt_hw_spin_unlock(&ambox->lock.lock);
return err;
}
struct scmi_agent_ops scmi_agent_mailbox_ops =
{
.name = "mailbox",
.setup = scmi_agent_mailbox_setup,
.process_msg = scmi_agent_mailbox_process_msg,
};
|