Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 62f2c90 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 62f2c90 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Stephen Warren | 3cdf676 | 2016-06-17 09:43:56 -0600 | [diff] [blame] | 9 | #include <mailbox-uclass.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Stephen Warren | 62f2c90 | 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 | 1ee4819 | 2020-02-03 07:35:50 -0700 | [diff] [blame] | 92 | .rfree = sandbox_mbox_free, |
Stephen Warren | 62f2c90 | 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, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 103 | .priv_auto = sizeof(struct sandbox_mbox), |
Stephen Warren | 62f2c90 | 2016-05-16 17:41:37 -0600 | [diff] [blame] | 104 | .ops = &sandbox_mbox_mbox_ops, |
| 105 | }; |