Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 8961b52 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 8961b52 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Stephen Warren | 769d52e | 2016-06-17 09:43:56 -0600 | [diff] [blame] | 9 | #include <mailbox-uclass.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Stephen Warren | 8961b52 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/mbox.h> |
| 13 | |
| 14 | #define SANDBOX_MBOX_CHANNELS 2 |
| 15 | |
| 16 | struct sandbox_mbox_chan { |
| 17 | bool rx_msg_valid; |
| 18 | uint32_t rx_msg; |
| 19 | }; |
| 20 | |
| 21 | struct sandbox_mbox { |
| 22 | struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS]; |
| 23 | }; |
| 24 | |
| 25 | static 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 | |
| 35 | static int sandbox_mbox_free(struct mbox_chan *chan) |
| 36 | { |
| 37 | debug("%s(chan=%p)\n", __func__, chan); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static 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 | |
| 55 | static 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 | |
| 71 | static int sandbox_mbox_bind(struct udevice *dev) |
| 72 | { |
| 73 | debug("%s(dev=%p)\n", __func__, dev); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int sandbox_mbox_probe(struct udevice *dev) |
| 79 | { |
| 80 | debug("%s(dev=%p)\n", __func__, dev); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static const struct udevice_id sandbox_mbox_ids[] = { |
| 86 | { .compatible = "sandbox,mbox" }, |
| 87 | { } |
| 88 | }; |
| 89 | |
| 90 | struct mbox_ops sandbox_mbox_mbox_ops = { |
| 91 | .request = sandbox_mbox_request, |
Simon Glass | cc92c3c | 2020-02-03 07:35:50 -0700 | [diff] [blame] | 92 | .rfree = sandbox_mbox_free, |
Stephen Warren | 8961b52 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 93 | .send = sandbox_mbox_send, |
| 94 | .recv = sandbox_mbox_recv, |
| 95 | }; |
| 96 | |
| 97 | U_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, |
| 103 | .priv_auto_alloc_size = sizeof(struct sandbox_mbox), |
| 104 | .ops = &sandbox_mbox_mbox_ops, |
| 105 | }; |