blob: 87e06e492fe7a2c6d73d2790fae3fed8f9573cf0 [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
Stephen Warren8961b522016-05-16 17:41:37 -06006#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06007#include <log.h>
Stephen Warren769d52e2016-06-17 09:43:56 -06008#include <mailbox-uclass.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Stephen Warren8961b522016-05-16 17:41:37 -060010#include <asm/io.h>
11#include <asm/mbox.h>
12
13#define SANDBOX_MBOX_CHANNELS 2
14
15struct sandbox_mbox_chan {
16 bool rx_msg_valid;
17 uint32_t rx_msg;
18};
19
20struct sandbox_mbox {
21 struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS];
22};
23
24static 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
34static int sandbox_mbox_free(struct mbox_chan *chan)
35{
36 debug("%s(chan=%p)\n", __func__, chan);
37
38 return 0;
39}
40
41static 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
54static 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
70static int sandbox_mbox_bind(struct udevice *dev)
71{
72 debug("%s(dev=%p)\n", __func__, dev);
73
74 return 0;
75}
76
77static int sandbox_mbox_probe(struct udevice *dev)
78{
79 debug("%s(dev=%p)\n", __func__, dev);
80
81 return 0;
82}
83
84static const struct udevice_id sandbox_mbox_ids[] = {
85 { .compatible = "sandbox,mbox" },
86 { }
87};
88
89struct mbox_ops sandbox_mbox_mbox_ops = {
90 .request = sandbox_mbox_request,
Simon Glasscc92c3c2020-02-03 07:35:50 -070091 .rfree = sandbox_mbox_free,
Stephen Warren8961b522016-05-16 17:41:37 -060092 .send = sandbox_mbox_send,
93 .recv = sandbox_mbox_recv,
94};
95
96U_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,
Simon Glass41575d82020-12-03 16:55:17 -0700102 .priv_auto = sizeof(struct sandbox_mbox),
Stephen Warren8961b522016-05-16 17:41:37 -0600103 .ops = &sandbox_mbox_mbox_ops,
104};