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>
19 #define INTR_STAT_OFS 0x0
20 #define INTR_SET_OFS 0x8
21 #define INTR_CLR_OFS 0x10
23 #define MHU_LP_OFFSET 0x0
24 #define MHU_HP_OFFSET 0x20
25 #define MHU_SEC_OFFSET 0x200
26 #define TX_REG_OFFSET 0x100
28 #define MHU_CHANS 3 /* Secure, Non-Secure High and Low Priority */
29 #define MHU_CHAN_MAX 20 /* Max channels to save on unused RAM */
30 #define MHU_NUM_DOORBELLS 32
40 struct mhu_db_link mlink
[MHU_CHANS
];
41 struct mbox_controller mbox
;
46 * struct mhu_db_channel - ARM MHU Mailbox allocated channel information
48 * @mhu: Pointer to parent mailbox device
49 * @pchan: Physical channel within which this doorbell resides in
50 * @doorbell: doorbell number pertaining to this channel
52 struct mhu_db_channel
{
55 unsigned int doorbell
;
58 static inline struct mbox_chan
*
59 mhu_db_mbox_to_channel(struct mbox_controller
*mbox
, unsigned int pchan
,
60 unsigned int doorbell
)
63 struct mhu_db_channel
*chan_info
;
65 for (i
= 0; i
< mbox
->num_chans
; i
++) {
66 chan_info
= mbox
->chans
[i
].con_priv
;
67 if (chan_info
&& chan_info
->pchan
== pchan
&&
68 chan_info
->doorbell
== doorbell
)
69 return &mbox
->chans
[i
];
75 static void mhu_db_mbox_clear_irq(struct mbox_chan
*chan
)
77 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
78 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].rx_reg
;
80 writel_relaxed(BIT(chan_info
->doorbell
), base
+ INTR_CLR_OFS
);
83 static unsigned int mhu_db_mbox_irq_to_pchan_num(struct arm_mhu
*mhu
, int irq
)
87 for (pchan
= 0; pchan
< MHU_CHANS
; pchan
++)
88 if (mhu
->mlink
[pchan
].irq
== irq
)
93 static struct mbox_chan
*
94 mhu_db_mbox_irq_to_channel(struct arm_mhu
*mhu
, unsigned int pchan
)
97 unsigned int doorbell
;
98 struct mbox_chan
*chan
= NULL
;
99 struct mbox_controller
*mbox
= &mhu
->mbox
;
100 void __iomem
*base
= mhu
->mlink
[pchan
].rx_reg
;
102 bits
= readl_relaxed(base
+ INTR_STAT_OFS
);
104 /* No IRQs fired in specified physical channel */
107 /* An IRQ has fired, find the associated channel */
108 for (doorbell
= 0; bits
; doorbell
++) {
109 if (!test_and_clear_bit(doorbell
, &bits
))
112 chan
= mhu_db_mbox_to_channel(mbox
, pchan
, doorbell
);
116 "Channel not registered: pchan: %d doorbell: %d\n",
123 static irqreturn_t
mhu_db_mbox_rx_handler(int irq
, void *data
)
125 struct mbox_chan
*chan
;
126 struct arm_mhu
*mhu
= data
;
127 unsigned int pchan
= mhu_db_mbox_irq_to_pchan_num(mhu
, irq
);
129 while (NULL
!= (chan
= mhu_db_mbox_irq_to_channel(mhu
, pchan
))) {
130 mbox_chan_received_data(chan
, NULL
);
131 mhu_db_mbox_clear_irq(chan
);
137 static bool mhu_db_last_tx_done(struct mbox_chan
*chan
)
139 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
140 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].tx_reg
;
142 if (readl_relaxed(base
+ INTR_STAT_OFS
) & BIT(chan_info
->doorbell
))
148 static int mhu_db_send_data(struct mbox_chan
*chan
, void *data
)
150 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
151 void __iomem
*base
= chan_info
->mhu
->mlink
[chan_info
->pchan
].tx_reg
;
153 /* Send event to co-processor */
154 writel_relaxed(BIT(chan_info
->doorbell
), base
+ INTR_SET_OFS
);
159 static int mhu_db_startup(struct mbox_chan
*chan
)
161 mhu_db_mbox_clear_irq(chan
);
165 static void mhu_db_shutdown(struct mbox_chan
*chan
)
167 struct mhu_db_channel
*chan_info
= chan
->con_priv
;
168 struct mbox_controller
*mbox
= &chan_info
->mhu
->mbox
;
171 for (i
= 0; i
< mbox
->num_chans
; i
++)
172 if (chan
== &mbox
->chans
[i
])
175 if (mbox
->num_chans
== i
) {
176 dev_warn(mbox
->dev
, "Request to free non-existent channel\n");
181 mhu_db_mbox_clear_irq(chan
);
182 devm_kfree(mbox
->dev
, chan
->con_priv
);
183 chan
->con_priv
= NULL
;
186 static struct mbox_chan
*mhu_db_mbox_xlate(struct mbox_controller
*mbox
,
187 const struct of_phandle_args
*spec
)
189 struct arm_mhu
*mhu
= dev_get_drvdata(mbox
->dev
);
190 struct mhu_db_channel
*chan_info
;
191 struct mbox_chan
*chan
;
192 unsigned int pchan
= spec
->args
[0];
193 unsigned int doorbell
= spec
->args
[1];
196 /* Bounds checking */
197 if (pchan
>= MHU_CHANS
|| doorbell
>= MHU_NUM_DOORBELLS
) {
199 "Invalid channel requested pchan: %d doorbell: %d\n",
201 return ERR_PTR(-EINVAL
);
204 /* Is requested channel free? */
205 chan
= mhu_db_mbox_to_channel(mbox
, pchan
, doorbell
);
207 dev_err(mbox
->dev
, "Channel in use: pchan: %d doorbell: %d\n",
209 return ERR_PTR(-EBUSY
);
212 /* Find the first free slot */
213 for (i
= 0; i
< mbox
->num_chans
; i
++)
214 if (!mbox
->chans
[i
].con_priv
)
217 if (mbox
->num_chans
== i
) {
218 dev_err(mbox
->dev
, "No free channels left\n");
219 return ERR_PTR(-EBUSY
);
222 chan
= &mbox
->chans
[i
];
224 chan_info
= devm_kzalloc(mbox
->dev
, sizeof(*chan_info
), GFP_KERNEL
);
226 return ERR_PTR(-ENOMEM
);
228 chan_info
->mhu
= mhu
;
229 chan_info
->pchan
= pchan
;
230 chan_info
->doorbell
= doorbell
;
232 chan
->con_priv
= chan_info
;
234 dev_dbg(mbox
->dev
, "mbox: created channel phys: %d doorbell: %d\n",
240 static const struct mbox_chan_ops mhu_db_ops
= {
241 .send_data
= mhu_db_send_data
,
242 .startup
= mhu_db_startup
,
243 .shutdown
= mhu_db_shutdown
,
244 .last_tx_done
= mhu_db_last_tx_done
,
247 static int mhu_db_probe(struct amba_device
*adev
, const struct amba_id
*id
)
250 int i
, err
, max_chans
;
252 struct mbox_chan
*chans
;
253 struct device
*dev
= &adev
->dev
;
254 struct device_node
*np
= dev
->of_node
;
255 int mhu_reg
[MHU_CHANS
] = {
256 MHU_LP_OFFSET
, MHU_HP_OFFSET
, MHU_SEC_OFFSET
,
259 if (!of_device_is_compatible(np
, "arm,mhu-doorbell"))
262 err
= of_property_read_u32(np
, "#mbox-cells", &cell_count
);
264 dev_err(dev
, "failed to read #mbox-cells in '%pOF'\n", np
);
268 if (cell_count
== 2) {
269 max_chans
= MHU_CHAN_MAX
;
271 dev_err(dev
, "incorrect value of #mbox-cells in '%pOF'\n", np
);
275 mhu
= devm_kzalloc(dev
, sizeof(*mhu
), GFP_KERNEL
);
279 mhu
->base
= devm_ioremap_resource(dev
, &adev
->res
);
280 if (IS_ERR(mhu
->base
))
281 return PTR_ERR(mhu
->base
);
283 chans
= devm_kcalloc(dev
, max_chans
, sizeof(*chans
), GFP_KERNEL
);
289 mhu
->mbox
.chans
= chans
;
290 mhu
->mbox
.num_chans
= max_chans
;
291 mhu
->mbox
.txdone_irq
= false;
292 mhu
->mbox
.txdone_poll
= true;
293 mhu
->mbox
.txpoll_period
= 1;
295 mhu
->mbox
.of_xlate
= mhu_db_mbox_xlate
;
296 amba_set_drvdata(adev
, mhu
);
298 mhu
->mbox
.ops
= &mhu_db_ops
;
300 err
= devm_mbox_controller_register(dev
, &mhu
->mbox
);
302 dev_err(dev
, "Failed to register mailboxes %d\n", err
);
306 for (i
= 0; i
< MHU_CHANS
; i
++) {
307 int irq
= mhu
->mlink
[i
].irq
= adev
->irq
[i
];
310 dev_dbg(dev
, "No IRQ found for Channel %d\n", i
);
314 mhu
->mlink
[i
].rx_reg
= mhu
->base
+ mhu_reg
[i
];
315 mhu
->mlink
[i
].tx_reg
= mhu
->mlink
[i
].rx_reg
+ TX_REG_OFFSET
;
317 err
= devm_request_threaded_irq(dev
, irq
, NULL
,
318 mhu_db_mbox_rx_handler
,
319 IRQF_ONESHOT
, "mhu_db_link", mhu
);
321 dev_err(dev
, "Can't claim IRQ %d\n", irq
);
322 mbox_controller_unregister(&mhu
->mbox
);
327 dev_info(dev
, "ARM MHU Doorbell mailbox registered\n");
331 static struct amba_id mhu_ids
[] = {
338 MODULE_DEVICE_TABLE(amba
, mhu_ids
);
340 static struct amba_driver arm_mhu_db_driver
= {
342 .name
= "mhu-doorbell",
345 .probe
= mhu_db_probe
,
347 module_amba_driver(arm_mhu_db_driver
);
349 MODULE_LICENSE("GPL v2");
350 MODULE_DESCRIPTION("ARM MHU Doorbell Driver");
351 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");