blob: 5bb943adf67b4b4a9291d351bcb67b13ec7c17c4 [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
5 * Copyright (C) 2019 ARM Ltd.
6 *
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>
21
Per Åstrandd7483362020-10-07 13:50:46 +020022#define MHU_V2_REG_STAT_OFS 0x0
23#define MHU_V2_REG_CLR_OFS 0x8
24#define MHU_V2_REG_SET_OFS 0xC
25#define MHU_V2_REG_MSG_NO_CAP_OFS 0xF80
26#define MHU_V2_REG_ACC_REQ_OFS 0xF88
27#define MHU_V2_REG_ACC_RDY_OFS 0xF8C
28#define MHU_V2_INT_EN_OFS 0xF98
29#define MHU_V2_AIDR_OFS 0xFCC
Per Åstrandbe87baf2020-10-07 13:47:24 +020030
Per Åstrandd7483362020-10-07 13:50:46 +020031#define MHU_V2_CHCOMB BIT(2)
32#define MHU_V2_AIDR_MINOR(_reg) ((_reg) & 0xF)
Per Åstrandbe87baf2020-10-07 13:47:24 +020033
Per Åstrandd7483362020-10-07 13:50:46 +020034#define MHU_V2_EACH_CHANNEL_SIZE 0x20
Per Åstrandbe87baf2020-10-07 13:47:24 +020035
36#define mbox_to_arm_mhuv2(c) container_of(c, struct arm_mhuv2, mbox)
37
38struct mhuv2_link {
39 unsigned int irq;
40 void __iomem *tx_reg;
41 void __iomem *rx_reg;
42};
43
44struct arm_mhuv2 {
Per Åstrandd7483362020-10-07 13:50:46 +020045 void __iomem *base;
46 struct mhuv2_link *mlink;
47 struct mbox_chan *chan;
Per Åstrandbe87baf2020-10-07 13:47:24 +020048 struct mbox_controller mbox;
49};
50
Per Åstrandd7483362020-10-07 13:50:46 +020051static irqreturn_t mhuv2_rx_interrupt(int irq,
52 void *p)
Per Åstrandbe87baf2020-10-07 13:47:24 +020053{
54 struct mbox_chan *chan = p;
55 struct mhuv2_link *mlink = chan->con_priv;
56 u32 val;
57
58 val = readl_relaxed(mlink->rx_reg + MHU_V2_REG_STAT_OFS);
59 if (!val)
60 return IRQ_NONE;
61
62 mbox_chan_received_data(chan, (void *)&val);
63
64 writel_relaxed(val, mlink->rx_reg + MHU_V2_REG_CLR_OFS);
65
66 return IRQ_HANDLED;
67}
68
69static bool mhuv2_last_tx_done(struct mbox_chan *chan)
70{
71 struct mhuv2_link *mlink = chan->con_priv;
72 u32 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
73
74 return (val == 0);
75}
76
Per Åstrandd7483362020-10-07 13:50:46 +020077static int mhuv2_send_data(struct mbox_chan *chan,
78 void *data)
Per Åstrandbe87baf2020-10-07 13:47:24 +020079{
80 struct mhuv2_link *mlink = chan->con_priv;
81 u32 *arg = data;
82
83 writel_relaxed(*arg, mlink->tx_reg + MHU_V2_REG_SET_OFS);
84
85 return 0;
86}
87
88static int mhuv2_startup(struct mbox_chan *chan)
89{
90 struct mhuv2_link *mlink = chan->con_priv;
91 u32 val;
92 int ret;
93 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
94
95 writel_relaxed(0x1, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
96
97 val = readl_relaxed(mlink->tx_reg + MHU_V2_REG_STAT_OFS);
98 writel_relaxed(val, mlink->tx_reg + MHU_V2_REG_CLR_OFS);
99
100 ret = request_irq(mlink->irq, mhuv2_rx_interrupt,
101 IRQF_SHARED, "mhuv2_link", chan);
102 if (ret) {
103 dev_err(chan->mbox->dev,
104 "unable to acquire IRQ %d\n", mlink->irq);
Per Åstrandd7483362020-10-07 13:50:46 +0200105
Per Åstrandbe87baf2020-10-07 13:47:24 +0200106 return ret;
107 }
108
109 return 0;
110}
111
112static void mhuv2_shutdown(struct mbox_chan *chan)
113{
114 struct mhuv2_link *mlink = chan->con_priv;
115 struct arm_mhuv2 *mhuv2 = mbox_to_arm_mhuv2(chan->mbox);
116
117 writel_relaxed(0x0, mhuv2->base + MHU_V2_REG_ACC_REQ_OFS);
118
119 free_irq(mlink->irq, chan);
120}
121
122static const struct mbox_chan_ops mhuv2_ops = {
Per Åstrandd7483362020-10-07 13:50:46 +0200123 .send_data = mhuv2_send_data,
124 .startup = mhuv2_startup,
125 .shutdown = mhuv2_shutdown,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200126 .last_tx_done = mhuv2_last_tx_done,
127};
128
129void mhuv2_check_enable_cmbint(struct mhuv2_link *link)
130{
131 const u32 aidr = readl_relaxed(link->rx_reg + MHU_V2_AIDR_OFS);
132
Per Åstrandd7483362020-10-07 13:50:46 +0200133 if (MHU_V2_AIDR_MINOR(aidr) == 1)
134 /* Enable combined receiver interrupt for MHUv2.1 */
Per Åstrandbe87baf2020-10-07 13:47:24 +0200135 writel_relaxed(MHU_V2_CHCOMB, link->rx_reg + MHU_V2_INT_EN_OFS);
Per Åstrandbe87baf2020-10-07 13:47:24 +0200136}
137
Per Åstrandd7483362020-10-07 13:50:46 +0200138static int mhuv2_probe(struct amba_device *adev,
139 const struct amba_id *id)
Per Åstrandbe87baf2020-10-07 13:47:24 +0200140{
141 int i, err;
142 struct arm_mhuv2 *mhuv2;
143 struct device *dev = &adev->dev;
144 void __iomem *rx_base, *tx_base;
145 const struct device_node *np = dev->of_node;
146 unsigned int pchans;
147 struct mhuv2_link *mlink;
148 struct mbox_chan *chan;
149
Per Åstrandbe87baf2020-10-07 13:47:24 +0200150 /* Allocate memory for device */
151 mhuv2 = devm_kzalloc(dev, sizeof(*mhuv2), GFP_KERNEL);
152 if (!mhuv2)
153 return -ENOMEM;
154
155 tx_base = of_iomap((struct device_node *)np, 0);
156 if (!tx_base) {
157 dev_err(dev, "failed to map tx registers\n");
158 iounmap(rx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200159
Per Åstrandbe87baf2020-10-07 13:47:24 +0200160 return -ENOMEM;
161 }
162
163 rx_base = of_iomap((struct device_node *)np, 1);
164 if (!rx_base) {
165 dev_err(dev, "failed to map rx registers\n");
Per Åstrandd7483362020-10-07 13:50:46 +0200166
Per Åstrandbe87baf2020-10-07 13:47:24 +0200167 return -ENOMEM;
168 }
169
170 pchans = readl_relaxed(tx_base + MHU_V2_REG_MSG_NO_CAP_OFS);
171 if (pchans == 0 || pchans % 2) {
172 dev_err(dev, "invalid number of channels %d\n", pchans);
173 iounmap(rx_base);
174 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200175
Per Åstrandbe87baf2020-10-07 13:47:24 +0200176 return -EINVAL;
177 }
178
179 mhuv2->mlink = devm_kcalloc(dev, pchans, sizeof(*mlink), GFP_KERNEL);
180 if (!mhuv2->mlink) {
181 iounmap(rx_base);
182 iounmap(tx_base);
Per Åstrandd7483362020-10-07 13:50:46 +0200183
Per Åstrandbe87baf2020-10-07 13:47:24 +0200184 return -ENOMEM;
185 }
186
187 mhuv2->chan = devm_kcalloc(dev, pchans, sizeof(*chan), GFP_KERNEL);
188 if (!mhuv2->chan) {
189 iounmap(rx_base);
190 iounmap(tx_base);
191 kfree(mhuv2->mlink);
Per Åstrandd7483362020-10-07 13:50:46 +0200192
Per Åstrandbe87baf2020-10-07 13:47:24 +0200193 return -ENOMEM;
194 }
195
196 for (i = 0; i < pchans; i++) {
197 mlink = mhuv2->mlink + i;
198 chan = mhuv2->chan + i;
199 chan->con_priv = mlink;
200 mlink->rx_reg = rx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
201 mlink->tx_reg = tx_base + (i * MHU_V2_EACH_CHANNEL_SIZE);
202 }
203
204 mhuv2->mlink->irq = adev->irq[0];
205 mhuv2_check_enable_cmbint(mhuv2->mlink);
206
207 mhuv2->base = tx_base;
208 mhuv2->mbox.dev = dev;
209 mhuv2->mbox.chans = mhuv2->chan;
210 mhuv2->mbox.num_chans = pchans;
211 mhuv2->mbox.ops = &mhuv2_ops;
212 mhuv2->mbox.txdone_irq = false;
213 mhuv2->mbox.txdone_poll = true;
214 mhuv2->mbox.txpoll_period = 1;
215
216 amba_set_drvdata(adev, mhuv2);
217
218 err = mbox_controller_register(&mhuv2->mbox);
219 if (err) {
220 dev_err(dev, "failed to register mailboxes %d\n", err);
221 iounmap(rx_base);
222 iounmap(tx_base);
223 kfree(mhuv2->mlink);
224 kfree(mhuv2->chan);
Per Åstrandd7483362020-10-07 13:50:46 +0200225
Per Åstrandbe87baf2020-10-07 13:47:24 +0200226 return err;
227 }
228
229 dev_info(dev, "ARM MHUv2 Mailbox driver registered\n");
Per Åstrandd7483362020-10-07 13:50:46 +0200230
Per Åstrandbe87baf2020-10-07 13:47:24 +0200231 return 0;
232}
233
234static int mhuv2_remove(struct amba_device *adev)
235{
236 struct arm_mhuv2 *mhuv2 = amba_get_drvdata(adev);
237
238 mbox_controller_unregister(&mhuv2->mbox);
239
240 return 0;
241}
242
243static struct amba_id mhuv2_ids[] = {
244 {
Per Åstrandd7483362020-10-07 13:50:46 +0200245 .id = 0x4b0d1,
246 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200247 },
248 {
Per Åstrandd7483362020-10-07 13:50:46 +0200249 .id = 0xbb0d1,
250 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200251 },
252 {
Per Åstrandd7483362020-10-07 13:50:46 +0200253 .id = 0xbb076,
254 .mask = 0xfffff,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200255 },
256 { 0, 0 },
257};
258MODULE_DEVICE_TABLE(amba, mhuv2_ids);
259
260static struct amba_driver arm_mhuv2_driver = {
Per Åstrandd7483362020-10-07 13:50:46 +0200261 .drv = {
262 .name = "mhuv2",
Per Åstrandbe87baf2020-10-07 13:47:24 +0200263 },
Per Åstrandd7483362020-10-07 13:50:46 +0200264 .id_table = mhuv2_ids,
265 .probe = mhuv2_probe,
266 .remove = mhuv2_remove,
Per Åstrandbe87baf2020-10-07 13:47:24 +0200267};
268module_amba_driver(arm_mhuv2_driver);
269
270MODULE_LICENSE("GPL v2");
271MODULE_DESCRIPTION("ARM MHUv2 Driver");
272MODULE_AUTHOR("Samarth Parikh <samarthp@ymail.com>");