Mark Kettenis | 456305e | 2022-01-22 20:38:12 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
Mark Kettenis | 456305e | 2022-01-22 20:38:12 +0100 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <mailbox-uclass.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <linux/apple-mailbox.h> |
| 10 | #include <linux/delay.h> |
| 11 | |
| 12 | #define REG_A2I_STAT 0x110 |
| 13 | #define REG_A2I_STAT_EMPTY BIT(17) |
| 14 | #define REG_A2I_STAT_FULL BIT(16) |
| 15 | #define REG_I2A_STAT 0x114 |
| 16 | #define REG_I2A_STAT_EMPTY BIT(17) |
| 17 | #define REG_I2A_STAT_FULL BIT(16) |
| 18 | #define REG_A2I_MSG0 0x800 |
| 19 | #define REG_A2I_MSG1 0x808 |
| 20 | #define REG_I2A_MSG0 0x830 |
| 21 | #define REG_I2A_MSG1 0x838 |
| 22 | |
| 23 | struct apple_mbox_priv { |
| 24 | void *base; |
| 25 | }; |
| 26 | |
| 27 | static int apple_mbox_of_xlate(struct mbox_chan *chan, |
| 28 | struct ofnode_phandle_args *args) |
| 29 | { |
| 30 | if (args->args_count != 0) |
| 31 | return -EINVAL; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int apple_mbox_send(struct mbox_chan *chan, const void *data) |
| 37 | { |
| 38 | struct apple_mbox_priv *priv = dev_get_priv(chan->dev); |
| 39 | const struct apple_mbox_msg *msg = data; |
| 40 | |
| 41 | writeq(msg->msg0, priv->base + REG_A2I_MSG0); |
| 42 | writeq(msg->msg1, priv->base + REG_A2I_MSG1); |
| 43 | while (readl(priv->base + REG_A2I_STAT) & REG_A2I_STAT_FULL) |
| 44 | udelay(1); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int apple_mbox_recv(struct mbox_chan *chan, void *data) |
| 50 | { |
| 51 | struct apple_mbox_priv *priv = dev_get_priv(chan->dev); |
| 52 | struct apple_mbox_msg *msg = data; |
| 53 | |
| 54 | if (readl(priv->base + REG_I2A_STAT) & REG_I2A_STAT_EMPTY) |
| 55 | return -ENODATA; |
| 56 | |
| 57 | msg->msg0 = readq(priv->base + REG_I2A_MSG0); |
| 58 | msg->msg1 = readq(priv->base + REG_I2A_MSG1); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | struct mbox_ops apple_mbox_ops = { |
| 63 | .of_xlate = apple_mbox_of_xlate, |
| 64 | .send = apple_mbox_send, |
| 65 | .recv = apple_mbox_recv, |
| 66 | }; |
| 67 | |
| 68 | static int apple_mbox_probe(struct udevice *dev) |
| 69 | { |
| 70 | struct apple_mbox_priv *priv = dev_get_priv(dev); |
| 71 | |
| 72 | priv->base = dev_read_addr_ptr(dev); |
| 73 | if (!priv->base) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static const struct udevice_id apple_mbox_of_match[] = { |
| 80 | { .compatible = "apple,asc-mailbox-v4" }, |
| 81 | { /* sentinel */ } |
| 82 | }; |
| 83 | |
| 84 | U_BOOT_DRIVER(apple_mbox) = { |
| 85 | .name = "apple-mbox", |
| 86 | .id = UCLASS_MAILBOX, |
| 87 | .of_match = apple_mbox_of_match, |
| 88 | .probe = apple_mbox_probe, |
| 89 | .priv_auto = sizeof(struct apple_mbox_priv), |
| 90 | .ops = &apple_mbox_ops, |
| 91 | }; |