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