blob: 87d38de0cb612afaf971e47434298f4bb2448e28 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Stephen Warren769d52e2016-06-17 09:43:56 -06009#include <mailbox-uclass.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Stephen Warren8961b522016-05-16 17:41:37 -060011#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,
Simon Glasscc92c3c2020-02-03 07:35:50 -070092 .rfree = sandbox_mbox_free,
Stephen Warren8961b522016-05-16 17:41:37 -060093 .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,
Simon Glass41575d82020-12-03 16:55:17 -0700103 .priv_auto = sizeof(struct sandbox_mbox),
Stephen Warren8961b522016-05-16 17:41:37 -0600104 .ops = &sandbox_mbox_mbox_ops,
105};