blob: 8c1a6e9a7cfc4e1c0cf679dfbe9da9aaa7f57f2f [file] [log] [blame]
Per Åstrandbe87baf2020-10-07 13:47:24 +02001/*
Mikael Olsson404a5362023-11-14 10:42:14 +01002 * SPDX-FileCopyrightText: Copyright 2019-2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 * SPDX-License-Identifier: GPL-2.0
4 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
18 *
Per Åstrandbe87baf2020-10-07 13:47:24 +020019 * Message Handling Unit version 2 controller driver
Per Åstrandbe87baf2020-10-07 13:47:24 +020020 * Based on drivers/mailbox/arm_mhu.c
Per Åstrandbe87baf2020-10-07 13:47:24 +020021 */
22
23#include <linux/interrupt.h>
24#include <linux/mutex.h>
25#include <linux/slab.h>
26#include <linux/err.h>
27#include <linux/io.h>
28#include <linux/module.h>
29#include <linux/amba/bus.h>
30#include <linux/mailbox_controller.h>
31#include <linux/of_device.h>
32#include <linux/of_address.h>
Anton Mobergbdcb6222021-08-25 14:06:24 +020033#include <linux/version.h>
Per Åstrandbe87baf2020-10-07 13:47:24 +020034
Per Åstrandd7483362020-10-07 13:50:46 +020035#define MHU_V2_REG_STAT_OFS 0x0
36#define MHU_V2_REG_CLR_OFS 0x8
37#define MHU_V2_REG_SET_OFS 0xC
38#define MHU_V2_REG_MSG_NO_CAP_OFS 0xF80
39#define MHU_V2_REG_ACC_REQ_OFS 0xF88
40#define MHU_V2_REG_ACC_RDY_OFS 0xF8C
41#define MHU_V2_INT_EN_OFS 0xF98
42#define MHU_V2_AIDR_OFS 0xFCC
Per Åstrandbe87baf2020-10-07 13:47:24 +020043
Per Åstrandd7483362020-10-07 13:50:46 +020044#define MHU_V2_CHCOMB BIT(2)
45#define MHU_V2_AIDR_MINOR(_reg) ((_reg) & 0xF)
Per Åstrandbe87baf2020-10-07 13:47:24 +020046
Per Åstrandd7483362020-10-07 13:50:46 +020047#define MHU_V2_EACH_CHANNEL_SIZE 0x20
Per Åstrandbe87baf2020-10-07 13:47:24 +020048
49#define mbox_to_arm_mhuv2(c) container_of(c, struct arm_mhuv2, mbox)
50
51struct mhuv2_link {
52 unsigned int irq;
53 void __iomem *tx_reg;
54 void __iomem *rx_reg;
55};
56
57struct arm_mhuv2 {
Per Åstrandd7483362020-10-07 13:50:46 +020058 void __iomem *base;
59 struct mhuv2_link *mlink;
60 struct mbox_chan *chan;
Per Åstrandbe87baf2020-10-07 13:47:24 +020061 struct mbox_controller mbox;
62};
63
Per Åstrandd7483362020-10-07 13:50:46 +020064static irqreturn_t mhuv2_rx_interrupt(int irq,
65 void *p)
Per Åstrandbe87baf2020-10-07 13:47:24 +020066{
67 struct mbox_chan *chan = p;
68 struct mhuv2_link *mlink = chan->con_priv;
69 u32 val;
70
71 val = readl_relaxed(mlink->rx_reg + MHU_V2_REG_STAT_OFS);
72 if (!val)
73 return IRQ_NONE;
74
75 mbox_chan_received_data(chan, (void *)&val);
76
77 writel_relaxed(val, mlink->rx_reg + MHU_V2_REG_CLR_OFS);
78
79 return IRQ_HANDLED;
80}
81
82static bool mhuv2_last_tx_done(struct mbox_chan *chan)
83{
84 struct mhuv2_link *mlink = chan->con_priv;
85 u32 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
86
87 return (val == 0);
88}
89
Per Åstrandd7483362020-10-07 13:50:46 +020090static int mhuv2_send_data(struct mbox_chan *chan,
91 void *data)
Per Åstrandbe87baf2020-10-07 13:47:24 +020092{
93 struct mhuv2_link *mlink = chan->con_priv;
Per Åstrand8fac4a42020-10-12 13:37:16 +020094 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
Per Åstrandbe87baf2020-10-07 13:47:24 +020095 u32 *arg = data;
Per Åstrand8fac4a42020-10-12 13:37:16 +020096 u32 tmo = 100000;
97
98 /* If ACCESS_REQUEST is low, we have to wait for the other side
99 * to relase ACCESS_READY before continuing. */
100 if (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_REQ_OFS)) {
101 while (readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
102 --tmo != 0)
103 continue;
104
105 if (!tmo)
106 goto err;
107
108 /* Request access and wait for other side to ack */
109 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
110 tmo = 100000;
111 while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
112 --tmo != 0)
113 continue;
114
115 if (!tmo)
116 goto err;
117 } else {
118 while (!readl_relaxed(mhuv2->base + MHU_V2_REG_ACC_RDY_OFS) &&
119 --tmo != 0)
120 continue;
121
122 if (!tmo)
123 goto err;
124 }
Per Åstrandbe87baf2020-10-07 13:47:24 +0200125
126 writel_relaxed(*arg, mlink->tx_reg + MHU_V2_REG_SET_OFS);
127
128 return 0;
Per Åstrand8fac4a42020-10-12 13:37:16 +0200129
130err:
131 dev_err(chan->mbox->dev, "Failed to acquire access to mhu.\n");
132
133 return 1;
Per Åstrandbe87baf2020-10-07 13:47:24 +0200134}
135
136static int mhuv2_startup(struct mbox_chan *chan)
137{
138 struct mhuv2_link *mlink = chan->con_priv;
139 u32 val;
140 int ret;
141 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
142
143 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
144
145 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
146 writel_relaxed(val, mlink->tx_reg + MHU_V2_REG_CLR_OFS);
147
148 ret = request_irq(mlink->irq, mhuv2_rx_interrupt,
149 IRQF_SHARED, "mhuv2_link", chan);
150 if (ret) {
151 dev_err(chan->mbox->dev,
152 "unable to acquire IRQ %d\n", mlink->irq);
Per Åstrandd7483362020-10-07 13:50:46 +0200153
Per Åstrandbe87baf2020-10-07 13:47:24 +0200154 return ret;
155 }
156
157 return 0;
158}
159
160static void mhuv2_shutdown(struct mbox_chan *chan)
161{
162 struct mhuv2_link *mlink = chan->con_priv;
163 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
164
165 writel_relaxed(0x0, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
166
167 free_irq(mlink->irq, chan);
168}
169
170static const struct mbox_chan_ops mhuv2_ops = {
Per Åstrandd7483362020-10-07 13:50:46 +0200171 .send_data = mhuv2_send_data,
172 .startup = mhuv2_startup,
173 .shutdown = mhuv2_shutdown,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200174 .last_tx_done = mhuv2_last_tx_done,
175};
176
Mikael Olsson404a5362023-11-14 10:42:14 +0100177static void mhuv2_check_enable_cmbint(struct mhuv2_link *link)
Per Åstrandbe87baf2020-10-07 13:47:24 +0200178{
179 const u32 aidr = readl_relaxed(link->rx_reg + MHU_V2_AIDR_OFS);
180
Per Åstrandd7483362020-10-07 13:50:46 +0200181 if (MHU_V2_AIDR_MINOR(aidr) == 1)
182 /* Enable combined receiver interrupt for MHUv2.1 */
Per Åstrandbe87baf2020-10-07 13:47:24 +0200183 writel_relaxed(MHU_V2_CHCOMB, link->rx_reg + MHU_V2_INT_EN_OFS);
Per Åstrandbe87baf2020-10-07 13:47:24 +0200184}
185
Per Åstrandd7483362020-10-07 13:50:46 +0200186static int mhuv2_probe(struct amba_device *adev,
187 const struct amba_id *id)
Per Åstrandbe87baf2020-10-07 13:47:24 +0200188{
189 int i, err;
190 struct arm_mhuv2 *mhuv2;
191 struct device *dev = &adev->dev;
192 void __iomem *rx_base, *tx_base;
193 const struct device_node *np = dev->of_node;
194 unsigned int pchans;
195 struct mhuv2_link *mlink;
196 struct mbox_chan *chan;
197
Per Åstrandbe87baf2020-10-07 13:47:24 +0200198 /* Allocate memory for device */
199 mhuv2 = devm_kzalloc(dev, sizeof(*mhuv2), GFP_KERNEL);
200 if (!mhuv2)
201 return -ENOMEM;
202
203 tx_base = of_iomap((struct device_node *)np, 0);
204 if (!tx_base) {
205 dev_err(dev, "failed to map tx registers\n");
Mikael Olsson404a5362023-11-14 10:42:14 +0100206
Per Åstrandbe87baf2020-10-07 13:47:24 +0200207 return -ENOMEM;
208 }
209
210 rx_base = of_iomap((struct device_node *)np, 1);
211 if (!rx_base) {
212 dev_err(dev, "failed to map rx registers\n");
Jonny Svärd44c507e2021-02-09 16:37:40 +0100213 iounmap(tx_base);
Mikael Olsson404a5362023-11-14 10:42:14 +0100214
Per Åstrandbe87baf2020-10-07 13:47:24 +0200215 return -ENOMEM;
216 }
217
218 pchans = readl_relaxed(tx_base + MHU_V2_REG_MSG_NO_CAP_OFS);
219 if (pchans == 0 || pchans % 2) {
220 dev_err(dev, "invalid number of channels %d\n", pchans);
221 iounmap(rx_base);
222 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200223
Per Åstrandbe87baf2020-10-07 13:47:24 +0200224 return -EINVAL;
225 }
226
227 mhuv2->mlink = devm_kcalloc(dev, pchans, sizeof(*mlink), GFP_KERNEL);
228 if (!mhuv2->mlink) {
229 iounmap(rx_base);
230 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200231
Per Åstrandbe87baf2020-10-07 13:47:24 +0200232 return -ENOMEM;
233 }
234
235 mhuv2->chan = devm_kcalloc(dev, pchans, sizeof(*chan), GFP_KERNEL);
236 if (!mhuv2->chan) {
237 iounmap(rx_base);
238 iounmap(tx_base);
239 kfree(mhuv2->mlink);
Per Åstrandd7483362020-10-07 13:50:46 +0200240
Per Åstrandbe87baf2020-10-07 13:47:24 +0200241 return -ENOMEM;
242 }
243
244 for (i = 0; i < pchans; i++) {
245 mlink = mhuv2->mlink + i;
246 chan = mhuv2->chan + i;
247 chan->con_priv = mlink;
248 mlink->rx_reg = rx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
249 mlink->tx_reg = tx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
250 }
251
252 mhuv2->mlink->irq = adev->irq[0];
253 mhuv2_check_enable_cmbint(mhuv2->mlink);
254
255 mhuv2->base = tx_base;
256 mhuv2->mbox.dev = dev;
257 mhuv2->mbox.chans = mhuv2->chan;
258 mhuv2->mbox.num_chans = pchans;
259 mhuv2->mbox.ops = &mhuv2_ops;
260 mhuv2->mbox.txdone_irq = false;
261 mhuv2->mbox.txdone_poll = true;
262 mhuv2->mbox.txpoll_period = 1;
263
264 amba_set_drvdata(adev, mhuv2);
265
266 err = mbox_controller_register(&mhuv2->mbox);
267 if (err) {
268 dev_err(dev, "failed to register mailboxes %d\n", err);
269 iounmap(rx_base);
270 iounmap(tx_base);
271 kfree(mhuv2->mlink);
272 kfree(mhuv2->chan);
Per Åstrandd7483362020-10-07 13:50:46 +0200273
Per Åstrandbe87baf2020-10-07 13:47:24 +0200274 return err;
275 }
276
277 dev_info(dev, "ARM MHUv2 Mailbox driver registered\n");
Per Åstrandd7483362020-10-07 13:50:46 +0200278
Per Åstrandbe87baf2020-10-07 13:47:24 +0200279 return 0;
280}
281
Anton Mobergbdcb6222021-08-25 14:06:24 +0200282#if KERNEL_VERSION(5, 12, 0) <= LINUX_VERSION_CODE
283static void mhuv2_remove(struct amba_device *adev)
284{
285 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
286
287 mbox_controller_unregister(&mhuv2->mbox);
288}
Mikael Olsson404a5362023-11-14 10:42:14 +0100289
Anton Mobergbdcb6222021-08-25 14:06:24 +0200290#else
Per Åstrandbe87baf2020-10-07 13:47:24 +0200291static int mhuv2_remove(struct amba_device *adev)
292{
293 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
294
295 mbox_controller_unregister(&mhuv2->mbox);
296
297 return 0;
298}
Anton Mobergbdcb6222021-08-25 14:06:24 +0200299
Mikael Olsson404a5362023-11-14 10:42:14 +0100300#endif
Per Åstrandbe87baf2020-10-07 13:47:24 +0200301
302static struct amba_id mhuv2_ids[] = {
303 {
Per Åstrandd7483362020-10-07 13:50:46 +0200304 .id = 0x4b0d1,
305 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200306 },
307 {
Per Åstrandd7483362020-10-07 13:50:46 +0200308 .id = 0xbb0d1,
309 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200310 },
311 {
Per Åstrandd7483362020-10-07 13:50:46 +0200312 .id = 0xbb076,
313 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200314 },
315 { 0, 0 },
316};
317MODULE_DEVICE_TABLE(amba, mhuv2_ids);
318
319static struct amba_driver arm_mhuv2_driver = {
Per Åstrandd7483362020-10-07 13:50:46 +0200320 .drv = {
321 .name = "mhuv2",
Per Åstrandbe87baf2020-10-07 13:47:24 +0200322 },
Per Åstrandd7483362020-10-07 13:50:46 +0200323 .id_table = mhuv2_ids,
324 .probe = mhuv2_probe,
325 .remove = mhuv2_remove,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200326};
327module_amba_driver(arm_mhuv2_driver);
328
329MODULE_LICENSE("GPL v2");
330MODULE_DESCRIPTION("ARM MHUv2 Driver");
331MODULE_AUTHOR("Samarth Parikh <samarthp@ymail.com>");