blob: 56fb31eeaa87b0cabe83190bdcedda41bff47d2c [file] [log] [blame]
Per Åstrandbe87baf2020-10-07 13:47:24 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Message Handling Unit version 2 controller driver
4 * Copyright (C) 2019 ARM Ltd.
5 *
6 * Based on drivers/mailbox/arm_mhu.c
7 *
8 */
9
10#include <linux/interrupt.h>
11#include <linux/mutex.h>
12#include <linux/slab.h>
13#include <linux/err.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/amba/bus.h>
17#include <linux/mailbox_controller.h>
18#include <linux/of_device.h>
19#include <linux/of_address.h>
20
21#define MHU_V2_REG_STAT_OFS 0x0
22#define MHU_V2_REG_CLR_OFS 0x8
23#define MHU_V2_REG_SET_OFS 0xC
24#define MHU_V2_REG_MSG_NO_CAP_OFS 0xF80
25#define MHU_V2_REG_ACC_REQ_OFS 0xF88
26#define MHU_V2_REG_ACC_RDY_OFS 0xF8C
27#define MHU_V2_INT_EN_OFS 0xF98
28#define MHU_V2_AIDR_OFS 0xFCC
29
30#define MHU_V2_CHCOMB BIT(2)
31#define MHU_V2_AIDR_MINOR(_reg) ((_reg) & 0xF)
32
33#define MHU_V2_EACH_CHANNEL_SIZE 0x20
34
35#define mbox_to_arm_mhuv2(c) container_of(c, struct arm_mhuv2, mbox)
36
37struct mhuv2_link {
38 unsigned int irq;
39 void __iomem *tx_reg;
40 void __iomem *rx_reg;
41};
42
43struct arm_mhuv2 {
44 void __iomem *base;
45 struct mhuv2_link *mlink;
46 struct mbox_chan *chan;
47 struct mbox_controller mbox;
48};
49
50static irqreturn_t mhuv2_rx_interrupt(int irq, void *p)
51{
52 struct mbox_chan *chan = p;
53 struct mhuv2_link *mlink = chan->con_priv;
54 u32 val;
55
56 val = readl_relaxed(mlink->rx_reg + MHU_V2_REG_STAT_OFS);
57 if (!val)
58 return IRQ_NONE;
59
60 mbox_chan_received_data(chan, (void *)&val);
61
62 writel_relaxed(val, mlink->rx_reg + MHU_V2_REG_CLR_OFS);
63
64 return IRQ_HANDLED;
65}
66
67static bool mhuv2_last_tx_done(struct mbox_chan *chan)
68{
69 struct mhuv2_link *mlink = chan->con_priv;
70 u32 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
71
72 return (val == 0);
73}
74
75static int mhuv2_send_data(struct mbox_chan *chan, void *data)
76{
77 struct mhuv2_link *mlink = chan->con_priv;
78 u32 *arg = data;
79
80 writel_relaxed(*arg, mlink->tx_reg + MHU_V2_REG_SET_OFS);
81
82 return 0;
83}
84
85static int mhuv2_startup(struct mbox_chan *chan)
86{
87 struct mhuv2_link *mlink = chan->con_priv;
88 u32 val;
89 int ret;
90 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
91
92 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
93
94 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
95 writel_relaxed(val, mlink->tx_reg + MHU_V2_REG_CLR_OFS);
96
97 ret = request_irq(mlink->irq, mhuv2_rx_interrupt,
98 IRQF_SHARED, "mhuv2_link", chan);
99 if (ret) {
100 dev_err(chan->mbox->dev,
101 "unable to acquire IRQ %d\n", mlink->irq);
102 return ret;
103 }
104
105 return 0;
106}
107
108static void mhuv2_shutdown(struct mbox_chan *chan)
109{
110 struct mhuv2_link *mlink = chan->con_priv;
111 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
112
113 writel_relaxed(0x0, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
114
115 free_irq(mlink->irq, chan);
116}
117
118static const struct mbox_chan_ops mhuv2_ops = {
119 .send_data = mhuv2_send_data,
120 .startup = mhuv2_startup,
121 .shutdown = mhuv2_shutdown,
122 .last_tx_done = mhuv2_last_tx_done,
123};
124
125void mhuv2_check_enable_cmbint(struct mhuv2_link *link)
126{
127 const u32 aidr = readl_relaxed(link->rx_reg + MHU_V2_AIDR_OFS);
128
129 if (MHU_V2_AIDR_MINOR(aidr) == 1) {
130 // Enable combined receiver interrupt for MHUv2.1
131 writel_relaxed(MHU_V2_CHCOMB, link->rx_reg + MHU_V2_INT_EN_OFS);
132 }
133}
134
135static int mhuv2_probe(struct amba_device *adev, const struct amba_id *id)
136{
137 int i, err;
138 struct arm_mhuv2 *mhuv2;
139 struct device *dev = &adev->dev;
140 void __iomem *rx_base, *tx_base;
141 const struct device_node *np = dev->of_node;
142 unsigned int pchans;
143 struct mhuv2_link *mlink;
144 struct mbox_chan *chan;
145
146
147 /* Allocate memory for device */
148 mhuv2 = devm_kzalloc(dev, sizeof(*mhuv2), GFP_KERNEL);
149 if (!mhuv2)
150 return -ENOMEM;
151
152 tx_base = of_iomap((struct device_node *)np, 0);
153 if (!tx_base) {
154 dev_err(dev, "failed to map tx registers\n");
155 iounmap(rx_base);
156 return -ENOMEM;
157 }
158
159 rx_base = of_iomap((struct device_node *)np, 1);
160 if (!rx_base) {
161 dev_err(dev, "failed to map rx registers\n");
162 return -ENOMEM;
163 }
164
165 pchans = readl_relaxed(tx_base + MHU_V2_REG_MSG_NO_CAP_OFS);
166 if (pchans == 0 || pchans % 2) {
167 dev_err(dev, "invalid number of channels %d\n", pchans);
168 iounmap(rx_base);
169 iounmap(tx_base);
170 return -EINVAL;
171 }
172
173 mhuv2->mlink = devm_kcalloc(dev, pchans, sizeof(*mlink), GFP_KERNEL);
174 if (!mhuv2->mlink) {
175 iounmap(rx_base);
176 iounmap(tx_base);
177 return -ENOMEM;
178 }
179
180 mhuv2->chan = devm_kcalloc(dev, pchans, sizeof(*chan), GFP_KERNEL);
181 if (!mhuv2->chan) {
182 iounmap(rx_base);
183 iounmap(tx_base);
184 kfree(mhuv2->mlink);
185 return -ENOMEM;
186 }
187
188 for (i = 0; i < pchans; i++) {
189 mlink = mhuv2->mlink + i;
190 chan = mhuv2->chan + i;
191 chan->con_priv = mlink;
192 mlink->rx_reg = rx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
193 mlink->tx_reg = tx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
194 }
195
196 mhuv2->mlink->irq = adev->irq[0];
197 mhuv2_check_enable_cmbint(mhuv2->mlink);
198
199 mhuv2->base = tx_base;
200 mhuv2->mbox.dev = dev;
201 mhuv2->mbox.chans = mhuv2->chan;
202 mhuv2->mbox.num_chans = pchans;
203 mhuv2->mbox.ops = &mhuv2_ops;
204 mhuv2->mbox.txdone_irq = false;
205 mhuv2->mbox.txdone_poll = true;
206 mhuv2->mbox.txpoll_period = 1;
207
208 amba_set_drvdata(adev, mhuv2);
209
210 err = mbox_controller_register(&mhuv2->mbox);
211 if (err) {
212 dev_err(dev, "failed to register mailboxes %d\n", err);
213 iounmap(rx_base);
214 iounmap(tx_base);
215 kfree(mhuv2->mlink);
216 kfree(mhuv2->chan);
217 return err;
218 }
219
220 dev_info(dev, "ARM MHUv2 Mailbox driver registered\n");
221 return 0;
222}
223
224static int mhuv2_remove(struct amba_device *adev)
225{
226 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
227
228 mbox_controller_unregister(&mhuv2->mbox);
229
230 return 0;
231}
232
233static struct amba_id mhuv2_ids[] = {
234 {
235 .id = 0x4b0d1,
236 .mask = 0xfffff,
237 },
238 {
239 .id = 0xbb0d1,
240 .mask = 0xfffff,
241 },
242 {
243 .id = 0xbb076,
244 .mask = 0xfffff,
245 },
246 { 0, 0 },
247};
248MODULE_DEVICE_TABLE(amba, mhuv2_ids);
249
250static struct amba_driver arm_mhuv2_driver = {
251 .drv = {
252 .name = "mhuv2",
253 },
254 .id_table = mhuv2_ids,
255 .probe = mhuv2_probe,
256 .remove = mhuv2_remove,
257};
258module_amba_driver(arm_mhuv2_driver);
259
260MODULE_LICENSE("GPL v2");
261MODULE_DESCRIPTION("ARM MHUv2 Driver");
262MODULE_AUTHOR("Samarth Parikh <samarthp@ymail.com>");