1
2
3
4
5
6#include <common.h>
7#include <dm.h>
8#include <log.h>
9#include <mailbox-uclass.h>
10#include <malloc.h>
11#include <asm/io.h>
12#include <asm/mbox.h>
13
14#define SANDBOX_MBOX_CHANNELS 2
15
16struct sandbox_mbox_chan {
17 bool rx_msg_valid;
18 uint32_t rx_msg;
19};
20
21struct sandbox_mbox {
22 struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS];
23};
24
25static int sandbox_mbox_request(struct mbox_chan *chan)
26{
27 debug("%s(chan=%p)\n", __func__, chan);
28
29 if (chan->id >= SANDBOX_MBOX_CHANNELS)
30 return -EINVAL;
31
32 return 0;
33}
34
35static int sandbox_mbox_free(struct mbox_chan *chan)
36{
37 debug("%s(chan=%p)\n", __func__, chan);
38
39 return 0;
40}
41
42static int sandbox_mbox_send(struct mbox_chan *chan, const void *data)
43{
44 struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
45 const uint32_t *pmsg = data;
46
47 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
48
49 sbm->chans[chan->id].rx_msg = *pmsg ^ SANDBOX_MBOX_PING_XOR;
50 sbm->chans[chan->id].rx_msg_valid = true;
51
52 return 0;
53}
54
55static int sandbox_mbox_recv(struct mbox_chan *chan, void *data)
56{
57 struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
58 uint32_t *pmsg = data;
59
60 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
61
62 if (!sbm->chans[chan->id].rx_msg_valid)
63 return -ENODATA;
64
65 *pmsg = sbm->chans[chan->id].rx_msg;
66 sbm->chans[chan->id].rx_msg_valid = false;
67
68 return 0;
69}
70
71static int sandbox_mbox_bind(struct udevice *dev)
72{
73 debug("%s(dev=%p)\n", __func__, dev);
74
75 return 0;
76}
77
78static int sandbox_mbox_probe(struct udevice *dev)
79{
80 debug("%s(dev=%p)\n", __func__, dev);
81
82 return 0;
83}
84
85static const struct udevice_id sandbox_mbox_ids[] = {
86 { .compatible = "sandbox,mbox" },
87 { }
88};
89
90struct mbox_ops sandbox_mbox_mbox_ops = {
91 .request = sandbox_mbox_request,
92 .rfree = sandbox_mbox_free,
93 .send = sandbox_mbox_send,
94 .recv = sandbox_mbox_recv,
95};
96
97U_BOOT_DRIVER(sandbox_mbox) = {
98 .name = "sandbox_mbox",
99 .id = UCLASS_MAILBOX,
100 .of_match = sandbox_mbox_ids,
101 .bind = sandbox_mbox_bind,
102 .probe = sandbox_mbox_probe,
103 .priv_auto = sizeof(struct sandbox_mbox),
104 .ops = &sandbox_mbox_mbox_ops,
105};
106