1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd.
4 * Copyright (C) 2015 Linaro Ltd.
5 * Based on ARM MHU driver by Jassi Brar <jaswinder.singh@linaro.org>
6 * Copyright (C) 2020 ARM Ltd.
9 #include <linux/amba/bus.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mailbox_controller.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
20 #define INTR_STAT_OFS 0x0
21 #define INTR_SET_OFS 0x8
22 #define INTR_CLR_OFS 0x10
24 #define MHU_LP_OFFSET 0x0
25 #define MHU_HP_OFFSET 0x20
26 #define MHU_SEC_OFFSET 0x200
27 #define TX_REG_OFFSET 0x100
29 #define MHU_CHANS 3 /* Secure, Non-Secure High and Low Priority */
30 #define MHU_CHAN_MAX 20 /* Max channels to save on unused RAM */
31 #define MHU_NUM_DOORBELLS 32
41 struct mhu_db_link mlink
[MHU_CHANS
];
42 struct mbox_controller mbox
;
47 * ARM MHU Mailbox allocated channel information
49 * @mhu: Pointer to parent mailbox device
50 * @pchan: Physical channel within which this doorbell resides in
51 * @doorbell: doorbell number pertaining to this channel
53 struct mhu_db_channel
{
56 unsigned int doorbell
;
59 static inline struct mbox_chan
*
60 mhu_db_mbox_to_channel(struct mbox_controller
*mbox
, unsigned int pchan
,
61 unsigned int doorbell
)
64 struct mhu_db_channel
*chan_info
;
66 for (i
= 0; i
< mbox
->num_chans
; i
++) {
67 chan_info
= mbox
->chans
[i
].con_priv
;
68 if (chan_info
&& chan_info
->pchan
== pchan
&&
69 chan_info
->doorbell
== doorbell
)
70 return &mbox
->chans
[i
];
76 static void mhu_db_mbox_clear_irq(struct mbox_chan
*chan
)
78 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
79 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].rx_reg
;
81 writel_relaxed(BIT(chan_info
->doorbell
), base
+ INTR_CLR_OFS
);
84 static unsigned int mhu_db_mbox_irq_to_pchan_num(struct arm_mhu
*mhu
, int irq
)
88 for (pchan
= 0; pchan
< MHU_CHANS
; pchan
++)
89 if (mhu
->mlink
[pchan
].irq
== irq
)
94 static struct mbox_chan
*
95 mhu_db_mbox_irq_to_channel(struct arm_mhu
*mhu
, unsigned int pchan
)
98 unsigned int doorbell
;
99 struct mbox_chan
*chan
= NULL
;
100 struct mbox_controller
*mbox
= &mhu
->mbox
;
101 void __iomem
*base
= mhu
->mlink
[pchan
].rx_reg
;
103 bits
= readl_relaxed(base
+ INTR_STAT_OFS
);
105 /* No IRQs fired in specified physical channel */
108 /* An IRQ has fired, find the associated channel */
109 for (doorbell
= 0; bits
; doorbell
++) {
110 if (!test_and_clear_bit(doorbell
, &bits
))
113 chan
= mhu_db_mbox_to_channel(mbox
, pchan
, doorbell
);
117 "Channel not registered: pchan: %d doorbell: %d\n",
124 static irqreturn_t
mhu_db_mbox_rx_handler(int irq
, void *data
)
126 struct mbox_chan
*chan
;
127 struct arm_mhu
*mhu
= data
;
128 unsigned int pchan
= mhu_db_mbox_irq_to_pchan_num(mhu
, irq
);
130 while (NULL
!= (chan
= mhu_db_mbox_irq_to_channel(mhu
, pchan
))) {
131 mbox_chan_received_data(chan
, NULL
);
132 mhu_db_mbox_clear_irq(chan
);
138 static bool mhu_db_last_tx_done(struct mbox_chan
*chan
)
140 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
141 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].tx_reg
;
143 if (readl_relaxed(base
+ INTR_STAT_OFS
) & BIT(chan_info
->doorbell
))
149 static int mhu_db_send_data(struct mbox_chan
*chan
, void *data
)
151 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
152 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].tx_reg
;
154 /* Send event to co-processor */
155 writel_relaxed(BIT(chan_info
->doorbell
), base
+ INTR_SET_OFS
);
160 static int mhu_db_startup(struct mbox_chan
*chan
)
162 mhu_db_mbox_clear_irq(chan
);
166 static void mhu_db_shutdown(struct mbox_chan
*chan
)
168 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
169 struct mbox_controller
*mbox
= &chan_info
->mhu
->mbox
;
172 for (i
= 0; i
< mbox
->num_chans
; i
++)
173 if (chan
== &mbox
->chans
[i
])
176 if (mbox
->num_chans
== i
) {
177 dev_warn(mbox
->dev
, "Request to free non-existent channel\n");
182 mhu_db_mbox_clear_irq(chan
);
183 devm_kfree(mbox
->dev
, chan
->con_priv
);
184 chan
->con_priv
= NULL
;
187 static struct mbox_chan
*mhu_db_mbox_xlate(struct mbox_controller
*mbox
,
188 const struct of_phandle_args
*spec
)
190 struct arm_mhu
*mhu
= dev_get_drvdata(mbox
->dev
);
191 struct mhu_db_channel
*chan_info
;
192 struct mbox_chan
*chan
;
193 unsigned int pchan
= spec
->args
[0];
194 unsigned int doorbell
= spec
->args
[1];
197 /* Bounds checking */
198 if (pchan
>= MHU_CHANS
|| doorbell
>= MHU_NUM_DOORBELLS
) {
200 "Invalid channel requested pchan: %d doorbell: %d\n",
202 return ERR_PTR(-EINVAL
);
205 /* Is requested channel free? */
206 chan
= mhu_db_mbox_to_channel(mbox
, pchan
, doorbell
);
208 dev_err(mbox
->dev
, "Channel in use: pchan: %d doorbell: %d\n",
210 return ERR_PTR(-EBUSY
);
213 /* Find the first free slot */
214 for (i
= 0; i
< mbox
->num_chans
; i
++)
215 if (!mbox
->chans
[i
].con_priv
)
218 if (mbox
->num_chans
== i
) {
219 dev_err(mbox
->dev
, "No free channels left\n");
220 return ERR_PTR(-EBUSY
);
223 chan
= &mbox
->chans
[i
];
225 chan_info
= devm_kzalloc(mbox
->dev
, sizeof(*chan_info
), GFP_KERNEL
);
227 return ERR_PTR(-ENOMEM
);
229 chan_info
->mhu
= mhu
;
230 chan_info
->pchan
= pchan
;
231 chan_info
->doorbell
= doorbell
;
233 chan
->con_priv
= chan_info
;
235 dev_dbg(mbox
->dev
, "mbox: created channel phys: %d doorbell: %d\n",
241 static const struct mbox_chan_ops mhu_db_ops
= {
242 .send_data
= mhu_db_send_data
,
243 .startup
= mhu_db_startup
,
244 .shutdown
= mhu_db_shutdown
,
245 .last_tx_done
= mhu_db_last_tx_done
,
248 static int mhu_db_probe(struct amba_device
*adev
, const struct amba_id
*id
)
251 int i
, err
, max_chans
;
253 struct mbox_chan
*chans
;
254 struct device
*dev
= &adev
->dev
;
255 struct device_node
*np
= dev
->of_node
;
256 int mhu_reg
[MHU_CHANS
] = {
257 MHU_LP_OFFSET
, MHU_HP_OFFSET
, MHU_SEC_OFFSET
,
260 if (!of_device_is_compatible(np
, "arm,mhu-doorbell"))
263 err
= of_property_read_u32(np
, "#mbox-cells", &cell_count
);
265 dev_err(dev
, "failed to read #mbox-cells in '%pOF'\n", np
);
269 if (cell_count
== 2) {
270 max_chans
= MHU_CHAN_MAX
;
272 dev_err(dev
, "incorrect value of #mbox-cells in '%pOF'\n", np
);
276 mhu
= devm_kzalloc(dev
, sizeof(*mhu
), GFP_KERNEL
);
280 mhu
->base
= devm_ioremap_resource(dev
, &adev
->res
);
281 if (IS_ERR(mhu
->base
)) {
282 dev_err(dev
, "ioremap failed\n");
283 return PTR_ERR(mhu
->base
);
286 chans
= devm_kcalloc(dev
, max_chans
, sizeof(*chans
), GFP_KERNEL
);
292 mhu
->mbox
.chans
= chans
;
293 mhu
->mbox
.num_chans
= max_chans
;
294 mhu
->mbox
.txdone_irq
= false;
295 mhu
->mbox
.txdone_poll
= true;
296 mhu
->mbox
.txpoll_period
= 1;
298 mhu
->mbox
.of_xlate
= mhu_db_mbox_xlate
;
299 amba_set_drvdata(adev
, mhu
);
301 mhu
->mbox
.ops
= &mhu_db_ops
;
303 err
= devm_mbox_controller_register(dev
, &mhu
->mbox
);
305 dev_err(dev
, "Failed to register mailboxes %d\n", err
);
309 for (i
= 0; i
< MHU_CHANS
; i
++) {
310 int irq
= mhu
->mlink
[i
].irq
= adev
->irq
[i
];
313 dev_dbg(dev
, "No IRQ found for Channel %d\n", i
);
317 mhu
->mlink
[i
].rx_reg
= mhu
->base
+ mhu_reg
[i
];
318 mhu
->mlink
[i
].tx_reg
= mhu
->mlink
[i
].rx_reg
+ TX_REG_OFFSET
;
320 err
= devm_request_threaded_irq(dev
, irq
, NULL
,
321 mhu_db_mbox_rx_handler
,
322 IRQF_ONESHOT
, "mhu_db_link", mhu
);
324 dev_err(dev
, "Can't claim IRQ %d\n", irq
);
325 mbox_controller_unregister(&mhu
->mbox
);
330 dev_info(dev
, "ARM MHU Doorbell mailbox registered\n");
334 static struct amba_id mhu_ids
[] = {
341 MODULE_DEVICE_TABLE(amba
, mhu_ids
);
343 static struct amba_driver arm_mhu_db_driver
= {
345 .name
= "mhu-doorbell",
348 .probe
= mhu_db_probe
,
350 module_amba_driver(arm_mhu_db_driver
);
352 MODULE_LICENSE("GPL v2");
353 MODULE_DESCRIPTION("ARM MHU Doorbell Driver");
354 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");