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