blob: b65bd78bfe66edd9af4fb5f192dc2c9e653d9e9a [file] [log] [blame]
Per Åstrandd7483362020-10-07 13:50:46 +02001/* SPDX-License-Identifier: GPL-2.0 */
2
Per Åstrandbe87baf2020-10-07 13:47:24 +02003/*
4 * Message Handling Unit version 2 controller driver
Per Åstrand8fac4a42020-10-12 13:37:16 +02005 * Copyright (C) 2019-2020 ARM Ltd.
Per Åstrandbe87baf2020-10-07 13:47:24 +02006 *
7 * Based on drivers/mailbox/arm_mhu.c
8 *
9 */
10
11#include <linux/interrupt.h>
12#include <linux/mutex.h>
13#include <linux/slab.h>
14#include <linux/err.h>
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/amba/bus.h>
18#include <linux/mailbox_controller.h>
19#include <linux/of_device.h>
20#include <linux/of_address.h>
Anton Mobergbdcb6222021-08-25 14:06:24 +020021#include <linux/version.h>
Per Åstrandbe87baf2020-10-07 13:47:24 +020022
Per Åstrandd7483362020-10-07 13:50:46 +020023#define MHU_V2_REG_STAT_OFS 0x0
24#define MHU_V2_REG_CLR_OFS 0x8
25#define MHU_V2_REG_SET_OFS 0xC
26#define MHU_V2_REG_MSG_NO_CAP_OFS 0xF80
27#define MHU_V2_REG_ACC_REQ_OFS 0xF88
28#define MHU_V2_REG_ACC_RDY_OFS 0xF8C
29#define MHU_V2_INT_EN_OFS 0xF98
30#define MHU_V2_AIDR_OFS 0xFCC
Per Åstrandbe87baf2020-10-07 13:47:24 +020031
Per Åstrandd7483362020-10-07 13:50:46 +020032#define MHU_V2_CHCOMB BIT(2)
33#define MHU_V2_AIDR_MINOR(_reg) ((_reg) & 0xF)
Per Åstrandbe87baf2020-10-07 13:47:24 +020034
Per Åstrandd7483362020-10-07 13:50:46 +020035#define MHU_V2_EACH_CHANNEL_SIZE 0x20
Per Åstrandbe87baf2020-10-07 13:47:24 +020036
37#define mbox_to_arm_mhuv2(c) container_of(c, struct arm_mhuv2, mbox)
38
39struct mhuv2_link {
40 unsigned int irq;
41 void __iomem *tx_reg;
42 void __iomem *rx_reg;
43};
44
45struct arm_mhuv2 {
Per Åstrandd7483362020-10-07 13:50:46 +020046 void __iomem *base;
47 struct mhuv2_link *mlink;
48 struct mbox_chan *chan;
Per Åstrandbe87baf2020-10-07 13:47:24 +020049 struct mbox_controller mbox;
50};
51
Per Åstrandd7483362020-10-07 13:50:46 +020052static irqreturn_t mhuv2_rx_interrupt(int irq,
53 void *p)
Per Åstrandbe87baf2020-10-07 13:47:24 +020054{
55 struct mbox_chan *chan = p;
56 struct mhuv2_link *mlink = chan->con_priv;
57 u32 val;
58
59 val = readl_relaxed(mlink->rx_reg + MHU_V2_REG_STAT_OFS);
60 if (!val)
61 return IRQ_NONE;
62
63 mbox_chan_received_data(chan, (void *)&val);
64
65 writel_relaxed(val, mlink->rx_reg + MHU_V2_REG_CLR_OFS);
66
67 return IRQ_HANDLED;
68}
69
70static bool mhuv2_last_tx_done(struct mbox_chan *chan)
71{
72 struct mhuv2_link *mlink = chan->con_priv;
73 u32 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
74
75 return (val == 0);
76}
77
Per Åstrandd7483362020-10-07 13:50:46 +020078static int mhuv2_send_data(struct mbox_chan *chan,
79 void *data)
Per Åstrandbe87baf2020-10-07 13:47:24 +020080{
81 struct mhuv2_link *mlink = chan->con_priv;
Per Åstrand8fac4a42020-10-12 13:37:16 +020082 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
Per Åstrandbe87baf2020-10-07 13:47:24 +020083 u32 *arg = data;
Per Åstrand8fac4a42020-10-12 13:37:16 +020084 u32 tmo = 100000;
85
86 /* If ACCESS_REQUEST is low, we have to wait for the other side
87 * to relase ACCESS_READY before continuing. */
88 if (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_REQ_OFS)) {
89 while (readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
90 --tmo != 0)
91 continue;
92
93 if (!tmo)
94 goto err;
95
96 /* Request access and wait for other side to ack */
97 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
98 tmo = 100000;
99 while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
100 --tmo != 0)
101 continue;
102
103 if (!tmo)
104 goto err;
105 } else {
106 while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
107 --tmo != 0)
108 continue;
109
110 if (!tmo)
111 goto err;
112 }
Per Åstrandbe87baf2020-10-07 13:47:24 +0200113
114 writel_relaxed(*arg, mlink->tx_reg + MHU_V2_REG_SET_OFS);
115
116 return 0;
Per Åstrand8fac4a42020-10-12 13:37:16 +0200117
118err:
119 dev_err(chan->mbox->dev, "Failed to acquire access to mhu.\n");
120
121 return 1;
Per Åstrandbe87baf2020-10-07 13:47:24 +0200122}
123
124static int mhuv2_startup(struct mbox_chan *chan)
125{
126 struct mhuv2_link *mlink = chan->con_priv;
127 u32 val;
128 int ret;
129 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
130
131 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
132
133 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
134 writel_relaxed(val, mlink->tx_reg + MHU_V2_REG_CLR_OFS);
135
136 ret = request_irq(mlink->irq, mhuv2_rx_interrupt,
137 IRQF_SHARED, "mhuv2_link", chan);
138 if (ret) {
139 dev_err(chan->mbox->dev,
140 "unable to acquire IRQ %d\n", mlink->irq);
Per Åstrandd7483362020-10-07 13:50:46 +0200141
Per Åstrandbe87baf2020-10-07 13:47:24 +0200142 return ret;
143 }
144
145 return 0;
146}
147
148static void mhuv2_shutdown(struct mbox_chan *chan)
149{
150 struct mhuv2_link *mlink = chan->con_priv;
151 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
152
153 writel_relaxed(0x0, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
154
155 free_irq(mlink->irq, chan);
156}
157
158static const struct mbox_chan_ops mhuv2_ops = {
Per Åstrandd7483362020-10-07 13:50:46 +0200159 .send_data = mhuv2_send_data,
160 .startup = mhuv2_startup,
161 .shutdown = mhuv2_shutdown,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200162 .last_tx_done = mhuv2_last_tx_done,
163};
164
165void mhuv2_check_enable_cmbint(struct mhuv2_link *link)
166{
167 const u32 aidr = readl_relaxed(link->rx_reg + MHU_V2_AIDR_OFS);
168
Per Åstrandd7483362020-10-07 13:50:46 +0200169 if (MHU_V2_AIDR_MINOR(aidr) == 1)
170 /* Enable combined receiver interrupt for MHUv2.1 */
Per Åstrandbe87baf2020-10-07 13:47:24 +0200171 writel_relaxed(MHU_V2_CHCOMB, link->rx_reg + MHU_V2_INT_EN_OFS);
Per Åstrandbe87baf2020-10-07 13:47:24 +0200172}
173
Per Åstrandd7483362020-10-07 13:50:46 +0200174static int mhuv2_probe(struct amba_device *adev,
175 const struct amba_id *id)
Per Åstrandbe87baf2020-10-07 13:47:24 +0200176{
177 int i, err;
178 struct arm_mhuv2 *mhuv2;
179 struct device *dev = &adev->dev;
180 void __iomem *rx_base, *tx_base;
181 const struct device_node *np = dev->of_node;
182 unsigned int pchans;
183 struct mhuv2_link *mlink;
184 struct mbox_chan *chan;
185
Per Åstrandbe87baf2020-10-07 13:47:24 +0200186 /* Allocate memory for device */
187 mhuv2 = devm_kzalloc(dev, sizeof(*mhuv2), GFP_KERNEL);
188 if (!mhuv2)
189 return -ENOMEM;
190
191 tx_base = of_iomap((struct device_node *)np, 0);
192 if (!tx_base) {
193 dev_err(dev, "failed to map tx registers\n");
Per Åstrandbe87baf2020-10-07 13:47:24 +0200194 return -ENOMEM;
195 }
196
197 rx_base = of_iomap((struct device_node *)np, 1);
198 if (!rx_base) {
199 dev_err(dev, "failed to map rx registers\n");
Jonny Svärd44c507e2021-02-09 16:37:40 +0100200 iounmap(tx_base);
Per Åstrandbe87baf2020-10-07 13:47:24 +0200201 return -ENOMEM;
202 }
203
204 pchans = readl_relaxed(tx_base + MHU_V2_REG_MSG_NO_CAP_OFS);
205 if (pchans == 0 || pchans % 2) {
206 dev_err(dev, "invalid number of channels %d\n", pchans);
207 iounmap(rx_base);
208 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200209
Per Åstrandbe87baf2020-10-07 13:47:24 +0200210 return -EINVAL;
211 }
212
213 mhuv2->mlink = devm_kcalloc(dev, pchans, sizeof(*mlink), GFP_KERNEL);
214 if (!mhuv2->mlink) {
215 iounmap(rx_base);
216 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200217
Per Åstrandbe87baf2020-10-07 13:47:24 +0200218 return -ENOMEM;
219 }
220
221 mhuv2->chan = devm_kcalloc(dev, pchans, sizeof(*chan), GFP_KERNEL);
222 if (!mhuv2->chan) {
223 iounmap(rx_base);
224 iounmap(tx_base);
225 kfree(mhuv2->mlink);
Per Åstrandd7483362020-10-07 13:50:46 +0200226
Per Åstrandbe87baf2020-10-07 13:47:24 +0200227 return -ENOMEM;
228 }
229
230 for (i = 0; i < pchans; i++) {
231 mlink = mhuv2->mlink + i;
232 chan = mhuv2->chan + i;
233 chan->con_priv = mlink;
234 mlink->rx_reg = rx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
235 mlink->tx_reg = tx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
236 }
237
238 mhuv2->mlink->irq = adev->irq[0];
239 mhuv2_check_enable_cmbint(mhuv2->mlink);
240
241 mhuv2->base = tx_base;
242 mhuv2->mbox.dev = dev;
243 mhuv2->mbox.chans = mhuv2->chan;
244 mhuv2->mbox.num_chans = pchans;
245 mhuv2->mbox.ops = &mhuv2_ops;
246 mhuv2->mbox.txdone_irq = false;
247 mhuv2->mbox.txdone_poll = true;
248 mhuv2->mbox.txpoll_period = 1;
249
250 amba_set_drvdata(adev, mhuv2);
251
252 err = mbox_controller_register(&mhuv2->mbox);
253 if (err) {
254 dev_err(dev, "failed to register mailboxes %d\n", err);
255 iounmap(rx_base);
256 iounmap(tx_base);
257 kfree(mhuv2->mlink);
258 kfree(mhuv2->chan);
Per Åstrandd7483362020-10-07 13:50:46 +0200259
Per Åstrandbe87baf2020-10-07 13:47:24 +0200260 return err;
261 }
262
263 dev_info(dev, "ARM MHUv2 Mailbox driver registered\n");
Per Åstrandd7483362020-10-07 13:50:46 +0200264
Per Åstrandbe87baf2020-10-07 13:47:24 +0200265 return 0;
266}
267
Anton Mobergbdcb6222021-08-25 14:06:24 +0200268#if KERNEL_VERSION(5, 12, 0) <= LINUX_VERSION_CODE
269static void mhuv2_remove(struct amba_device *adev)
270{
271 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
272
273 mbox_controller_unregister(&mhuv2->mbox);
274}
275#else
Per Åstrandbe87baf2020-10-07 13:47:24 +0200276static int mhuv2_remove(struct amba_device *adev)
277{
278 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
279
280 mbox_controller_unregister(&mhuv2->mbox);
281
282 return 0;
283}
Anton Mobergbdcb6222021-08-25 14:06:24 +0200284#endif
285
Per Åstrandbe87baf2020-10-07 13:47:24 +0200286
287static struct amba_id mhuv2_ids[] = {
288 {
Per Åstrandd7483362020-10-07 13:50:46 +0200289 .id = 0x4b0d1,
290 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200291 },
292 {
Per Åstrandd7483362020-10-07 13:50:46 +0200293 .id = 0xbb0d1,
294 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200295 },
296 {
Per Åstrandd7483362020-10-07 13:50:46 +0200297 .id = 0xbb076,
298 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200299 },
300 { 0, 0 },
301};
302MODULE_DEVICE_TABLE(amba, mhuv2_ids);
303
304static struct amba_driver arm_mhuv2_driver = {
Per Åstrandd7483362020-10-07 13:50:46 +0200305 .drv = {
306 .name = "mhuv2",
Per Åstrandbe87baf2020-10-07 13:47:24 +0200307 },
Per Åstrandd7483362020-10-07 13:50:46 +0200308 .id_table = mhuv2_ids,
309 .probe = mhuv2_probe,
310 .remove = mhuv2_remove,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200311};
312module_amba_driver(arm_mhuv2_driver);
313
314MODULE_LICENSE("GPL v2");
315MODULE_DESCRIPTION("ARM MHUv2 Driver");
316MODULE_AUTHOR("Samarth Parikh <samarthp@ymail.com>");