1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016, NVIDIA CORPORATION.
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <mailbox-uclass.h>
9 #include <asm/io.h>
10 #include <asm/mbox.h>
11 
12 #define SANDBOX_MBOX_CHANNELS 2
13 
14 struct sandbox_mbox_chan {
15 	bool rx_msg_valid;
16 	uint32_t rx_msg;
17 };
18 
19 struct sandbox_mbox {
20 	struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS];
21 };
22 
sandbox_mbox_request(struct mbox_chan * chan)23 static 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 
sandbox_mbox_free(struct mbox_chan * chan)33 static int sandbox_mbox_free(struct mbox_chan *chan)
34 {
35 	debug("%s(chan=%p)\n", __func__, chan);
36 
37 	return 0;
38 }
39 
sandbox_mbox_send(struct mbox_chan * chan,const void * data)40 static 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 
sandbox_mbox_recv(struct mbox_chan * chan,void * data)53 static 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 
sandbox_mbox_bind(struct udevice * dev)69 static int sandbox_mbox_bind(struct udevice *dev)
70 {
71 	debug("%s(dev=%p)\n", __func__, dev);
72 
73 	return 0;
74 }
75 
sandbox_mbox_probe(struct udevice * dev)76 static int sandbox_mbox_probe(struct udevice *dev)
77 {
78 	debug("%s(dev=%p)\n", __func__, dev);
79 
80 	return 0;
81 }
82 
83 static const struct udevice_id sandbox_mbox_ids[] = {
84 	{ .compatible = "sandbox,mbox" },
85 	{ }
86 };
87 
88 struct mbox_ops sandbox_mbox_mbox_ops = {
89 	.request = sandbox_mbox_request,
90 	.free = sandbox_mbox_free,
91 	.send = sandbox_mbox_send,
92 	.recv = sandbox_mbox_recv,
93 };
94 
95 U_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 };
104