Skip to content

Commit a9dc4da

Browse files
pelwellpopcornmix
authored andcommitted
mailbox: Add RP1 mailbox support
The Raspberry Pi RP1 includes 2 M3 cores running firmware. This driver adds a mailbox communication channel to them via a doorbell and some shared memory. Signed-off-by: Phil Elwell <[email protected]>
1 parent 042a2cb commit a9dc4da

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

drivers/mailbox/Kconfig

+9
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,13 @@ config THEAD_TH1520_MBOX
330330
kernel is running, and E902 core used for power management among other
331331
things.
332332

333+
config MBOX_RP1
334+
tristate "RP1 Mailbox"
335+
depends on MFD_RP1
336+
help
337+
An implementation of a mailbox interface to the Raspberry Pi RP1 I/O
338+
interface. Although written as a mailbox driver, the hardware only
339+
provides an array of 32 doorbells.
340+
Say Y here if you want to use the RP1 Mailbox.
341+
333342
endif

drivers/mailbox/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ obj-$(CONFIG_QCOM_CPUCP_MBOX) += qcom-cpucp-mbox.o
7070
obj-$(CONFIG_QCOM_IPCC) += qcom-ipcc.o
7171

7272
obj-$(CONFIG_THEAD_TH1520_MBOX) += mailbox-th1520.o
73+
74+
obj-$(CONFIG_MBOX_RP1) += rp1-mailbox.o

drivers/mailbox/rp1-mailbox.c

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2023 Raspberry Pi Ltd.
4+
*
5+
* Parts of this driver are based on:
6+
* - bcm2835-mailbox.c
7+
* Copyright (C) 2010,2015 Broadcom
8+
* Copyright (C) 2013-2014 Lubomir Rintel
9+
* Copyright (C) 2013 Craig McGeachie
10+
*/
11+
12+
#include <linux/compat.h>
13+
#include <linux/device.h>
14+
#include <linux/dma-mapping.h>
15+
#include <linux/err.h>
16+
#include <linux/interrupt.h>
17+
#include <linux/irq.h>
18+
#include <linux/kernel.h>
19+
#include <linux/mailbox_controller.h>
20+
#include <linux/miscdevice.h>
21+
#include <linux/module.h>
22+
#include <linux/of_address.h>
23+
#include <linux/of_irq.h>
24+
#include <linux/platform_device.h>
25+
26+
/*
27+
* RP1's PROC_EVENTS register can generate interrupts on the M3 cores (when
28+
* enabled). The 32-bit register is treated as 32 events, all of which share a
29+
* common interrupt. HOST_EVENTS is the same in the reverse direction.
30+
*/
31+
#define SYSCFG_PROC_EVENTS 0x00000008
32+
#define SYSCFG_HOST_EVENTS 0x0000000c
33+
#define SYSCFG_HOST_EVENT_IRQ_EN 0x00000010
34+
#define SYSCFG_HOST_EVENT_IRQ 0x00000014
35+
36+
#define HW_SET_BITS 0x00002000
37+
#define HW_CLR_BITS 0x00003000
38+
39+
#define MAX_CHANS 4 /* 32 is the hardware limit */
40+
41+
struct rp1_mbox {
42+
void __iomem *regs;
43+
unsigned int irq;
44+
struct mbox_controller controller;
45+
};
46+
47+
static struct rp1_mbox *rp1_chan_mbox(struct mbox_chan *chan)
48+
{
49+
return container_of(chan->mbox, struct rp1_mbox, controller);
50+
}
51+
52+
static unsigned int rp1_chan_event(struct mbox_chan *chan)
53+
{
54+
return (unsigned int)(uintptr_t)chan->con_priv;
55+
}
56+
57+
static irqreturn_t rp1_mbox_irq(int irq, void *dev_id)
58+
{
59+
struct rp1_mbox *mbox = dev_id;
60+
struct mbox_chan *chan;
61+
unsigned int doorbell;
62+
unsigned int evs;
63+
64+
evs = readl(mbox->regs + SYSCFG_HOST_EVENT_IRQ);
65+
writel(evs, mbox->regs + SYSCFG_HOST_EVENTS + HW_CLR_BITS);
66+
67+
while (evs) {
68+
doorbell = __ffs(evs);
69+
chan = &mbox->controller.chans[doorbell];
70+
mbox_chan_received_data(chan, NULL);
71+
evs &= ~(1 << doorbell);
72+
}
73+
return IRQ_HANDLED;
74+
}
75+
76+
static int rp1_send_data(struct mbox_chan *chan, void *data)
77+
{
78+
struct rp1_mbox *mbox = rp1_chan_mbox(chan);
79+
unsigned int event = rp1_chan_event(chan);
80+
81+
writel(event, mbox->regs + SYSCFG_PROC_EVENTS + HW_SET_BITS);
82+
83+
return 0;
84+
}
85+
86+
static int rp1_startup(struct mbox_chan *chan)
87+
{
88+
struct rp1_mbox *mbox = rp1_chan_mbox(chan);
89+
unsigned int event = rp1_chan_event(chan);
90+
91+
writel(event, mbox->regs + SYSCFG_HOST_EVENT_IRQ_EN + HW_SET_BITS);
92+
93+
return 0;
94+
}
95+
96+
static void rp1_shutdown(struct mbox_chan *chan)
97+
{
98+
struct rp1_mbox *mbox = rp1_chan_mbox(chan);
99+
unsigned int event = rp1_chan_event(chan);
100+
101+
writel(event, mbox->regs + SYSCFG_HOST_EVENT_IRQ_EN + HW_CLR_BITS);
102+
}
103+
104+
static bool rp1_last_tx_done(struct mbox_chan *chan)
105+
{
106+
struct rp1_mbox *mbox = rp1_chan_mbox(chan);
107+
unsigned int event = rp1_chan_event(chan);
108+
unsigned int evs;
109+
110+
evs = readl(mbox->regs + SYSCFG_HOST_EVENT_IRQ);
111+
112+
return !(evs & event);
113+
}
114+
115+
static const struct mbox_chan_ops rp1_mbox_chan_ops = {
116+
.send_data = rp1_send_data,
117+
.startup = rp1_startup,
118+
.shutdown = rp1_shutdown,
119+
.last_tx_done = rp1_last_tx_done
120+
};
121+
122+
static struct mbox_chan *rp1_mbox_xlate(struct mbox_controller *mbox,
123+
const struct of_phandle_args *spec)
124+
{
125+
struct mbox_chan *chan;
126+
unsigned int doorbell;
127+
128+
if (spec->args_count != 1)
129+
return ERR_PTR(-EINVAL);
130+
131+
doorbell = spec->args[0];
132+
if (doorbell >= MAX_CHANS)
133+
return ERR_PTR(-EINVAL);
134+
135+
chan = &mbox->chans[doorbell];
136+
if (chan->con_priv)
137+
return ERR_PTR(-EBUSY);
138+
139+
chan->con_priv = (void *)(uintptr_t)(1 << doorbell);
140+
141+
return chan;
142+
}
143+
144+
static int rp1_mbox_probe(struct platform_device *pdev)
145+
{
146+
struct device *dev = &pdev->dev;
147+
struct mbox_chan *chans;
148+
struct rp1_mbox *mbox;
149+
int ret = 0;
150+
151+
mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
152+
if (mbox == NULL)
153+
return -ENOMEM;
154+
155+
ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
156+
rp1_mbox_irq, 0, dev_name(dev), mbox);
157+
if (ret) {
158+
dev_err(dev, "Failed to register a mailbox IRQ handler: %d\n",
159+
ret);
160+
return -ENODEV;
161+
}
162+
163+
mbox->regs = devm_platform_ioremap_resource(pdev, 0);
164+
if (IS_ERR(mbox->regs)) {
165+
ret = PTR_ERR(mbox->regs);
166+
return ret;
167+
}
168+
169+
chans = devm_kcalloc(dev, MAX_CHANS, sizeof(*chans), GFP_KERNEL);
170+
if (!chans)
171+
return -ENOMEM;
172+
173+
mbox->controller.txdone_poll = true;
174+
mbox->controller.txpoll_period = 5;
175+
mbox->controller.ops = &rp1_mbox_chan_ops;
176+
mbox->controller.of_xlate = &rp1_mbox_xlate;
177+
mbox->controller.dev = dev;
178+
mbox->controller.num_chans = MAX_CHANS;
179+
mbox->controller.chans = chans;
180+
181+
ret = devm_mbox_controller_register(dev, &mbox->controller);
182+
if (ret)
183+
return ret;
184+
185+
platform_set_drvdata(pdev, mbox);
186+
187+
return 0;
188+
}
189+
190+
static const struct of_device_id rp1_mbox_of_match[] = {
191+
{ .compatible = "raspberrypi,rp1-mbox", },
192+
{},
193+
};
194+
MODULE_DEVICE_TABLE(of, rp1_mbox_of_match);
195+
196+
static struct platform_driver rp1_mbox_driver = {
197+
.driver = {
198+
.name = "rp1-mbox",
199+
.of_match_table = rp1_mbox_of_match,
200+
},
201+
.probe = rp1_mbox_probe,
202+
};
203+
204+
module_platform_driver(rp1_mbox_driver);
205+
206+
MODULE_AUTHOR("Phil Elwell <[email protected]>");
207+
MODULE_DESCRIPTION("RP1 mailbox IPC driver");
208+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)