blob: 442ca633a19a10fa050e61966382d92da76e6a85 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren8961b522016-05-16 17:41:37 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren8961b522016-05-16 17:41:37 -06004 */
5
6#include <common.h>
7#include <dm.h>
Stephen Warren769d52e2016-06-17 09:43:56 -06008#include <mailbox-uclass.h>
Stephen Warren8961b522016-05-16 17:41:37 -06009#include <asm/io.h>
10#include <asm/mbox.h>
11
12#define SANDBOX_MBOX_CHANNELS 2
13
14struct sandbox_mbox_chan {
15 bool rx_msg_valid;
16 uint32_t rx_msg;
17};
18
19struct sandbox_mbox {
20 struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS];
21};
22
23static int sandbox_mbox_request(struct mbox_chan *chan)
24{
25 debug("%s(chan=%p)\n", __func__, chan);
26
27 if (chan->id >= SANDBOX_MBOX_CHANNELS)
28 return -EINVAL;
29
30 return 0;
31}
32
33static int sandbox_mbox_free(struct mbox_chan *chan)
34{
35 debug("%s(chan=%p)\n", __func__, chan);
36
37 return 0;
38}
39
40static int sandbox_mbox_send(struct mbox_chan *chan, const void *data)
41{
42 struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
43 const uint32_t *pmsg = data;
44
45 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
46
47 sbm->chans[chan->id].rx_msg = *pmsg ^ SANDBOX_MBOX_PING_XOR;
48 sbm->chans[chan->id].rx_msg_valid = true;
49
50 return 0;
51}
52
53static int sandbox_mbox_recv(struct mbox_chan *chan, void *data)
54{
55 struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
56 uint32_t *pmsg = data;
57
58 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
59
60 if (!sbm->chans[chan->id].rx_msg_valid)
61 return -ENODATA;
62
63 *pmsg = sbm->chans[chan->id].rx_msg;
64 sbm->chans[chan->id].rx_msg_valid = false;
65
66 return 0;
67}
68
69static int sandbox_mbox_bind(struct udevice *dev)
70{
71 debug("%s(dev=%p)\n", __func__, dev);
72
73 return 0;
74}
75
76static int sandbox_mbox_probe(struct udevice *dev)
77{
78 debug("%s(dev=%p)\n", __func__, dev);
79
80 return 0;
81}
82
83static const struct udevice_id sandbox_mbox_ids[] = {
84 { .compatible = "sandbox,mbox" },
85 { }
86};
87
88struct mbox_ops sandbox_mbox_mbox_ops = {
89 .request = sandbox_mbox_request,
Simon Glasscc92c3c2020-02-03 07:35:50 -070090 .rfree = sandbox_mbox_free,
Stephen Warren8961b522016-05-16 17:41:37 -060091 .send = sandbox_mbox_send,
92 .recv = sandbox_mbox_recv,
93};
94
95U_BOOT_DRIVER(sandbox_mbox) = {
96 .name = "sandbox_mbox",
97 .id = UCLASS_MAILBOX,
98 .of_match = sandbox_mbox_ids,
99 .bind = sandbox_mbox_bind,
100 .probe = sandbox_mbox_probe,
101 .priv_auto_alloc_size = sizeof(struct sandbox_mbox),
102 .ops = &sandbox_mbox_mbox_ops,
103};