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