2 * Copyright (C) 2010,2015 Broadcom
3 * Copyright (C) 2013-2014 Lubomir Rintel
4 * Copyright (C) 2013 Craig McGeachie
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This device provides a mechanism for writing to the mailboxes,
11 * that are shared between the ARM and the VideoCore processor
13 * Parts of the driver are based on:
14 * - arch/arm/mach-bcm2708/vcio.c file written by Gray Girling that was
15 * obtained from branch "rpi-3.6.y" of git://github.com/raspberrypi/
17 * - drivers/mailbox/bcm2835-ipc.c by Lubomir Rintel at
18 * https://github.com/hackerspace/rpi-linux/blob/lr-raspberry-pi/drivers/
19 * mailbox/bcm2835-ipc.c
20 * - documentation available on the following web site:
21 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
24 #include <linux/device.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/kernel.h>
30 #include <linux/mailbox_controller.h>
31 #include <linux/module.h>
32 #include <linux/of_address.h>
33 #include <linux/of_irq.h>
34 #include <linux/platform_device.h>
35 #include <linux/spinlock.h>
38 #define ARM_0_MAIL0 0x00
39 #define ARM_0_MAIL1 0x20
42 * Mailbox registers. We basically only support mailbox 0 & 1. We
43 * deliver to the VC in mailbox 1, it delivers to us in mailbox 0. See
44 * BCM2835-ARM-Peripherals.pdf section 1.3 for an explanation about
45 * the placement of memory barriers.
47 #define MAIL0_RD (ARM_0_MAIL0 + 0x00)
48 #define MAIL0_POL (ARM_0_MAIL0 + 0x10)
49 #define MAIL0_STA (ARM_0_MAIL0 + 0x18)
50 #define MAIL0_CNF (ARM_0_MAIL0 + 0x1C)
51 #define MAIL1_WRT (ARM_0_MAIL1 + 0x00)
52 #define MAIL1_STA (ARM_0_MAIL1 + 0x18)
54 /* Status register: FIFO state. */
55 #define ARM_MS_FULL BIT(31)
56 #define ARM_MS_EMPTY BIT(30)
58 /* Configuration register: Enable interrupts. */
59 #define ARM_MC_IHAVEDATAIRQEN BIT(0)
64 struct mbox_controller controller
;
67 static struct bcm2835_mbox
*bcm2835_link_mbox(struct mbox_chan
*link
)
69 return container_of(link
->mbox
, struct bcm2835_mbox
, controller
);
72 static irqreturn_t
bcm2835_mbox_irq(int irq
, void *dev_id
)
74 struct bcm2835_mbox
*mbox
= dev_id
;
75 struct device
*dev
= mbox
->controller
.dev
;
76 struct mbox_chan
*link
= &mbox
->controller
.chans
[0];
78 while (!(readl(mbox
->regs
+ MAIL0_STA
) & ARM_MS_EMPTY
)) {
79 u32 msg
= readl(mbox
->regs
+ MAIL0_RD
);
80 dev_dbg(dev
, "Reply 0x%08X\n", msg
);
81 mbox_chan_received_data(link
, &msg
);
86 static int bcm2835_send_data(struct mbox_chan
*link
, void *data
)
88 struct bcm2835_mbox
*mbox
= bcm2835_link_mbox(link
);
89 u32 msg
= *(u32
*)data
;
91 spin_lock(&mbox
->lock
);
92 writel(msg
, mbox
->regs
+ MAIL1_WRT
);
93 dev_dbg(mbox
->controller
.dev
, "Request 0x%08X\n", msg
);
94 spin_unlock(&mbox
->lock
);
98 static int bcm2835_startup(struct mbox_chan
*link
)
100 struct bcm2835_mbox
*mbox
= bcm2835_link_mbox(link
);
102 /* Enable the interrupt on data reception */
103 writel(ARM_MC_IHAVEDATAIRQEN
, mbox
->regs
+ MAIL0_CNF
);
108 static void bcm2835_shutdown(struct mbox_chan
*link
)
110 struct bcm2835_mbox
*mbox
= bcm2835_link_mbox(link
);
112 writel(0, mbox
->regs
+ MAIL0_CNF
);
115 static bool bcm2835_last_tx_done(struct mbox_chan
*link
)
117 struct bcm2835_mbox
*mbox
= bcm2835_link_mbox(link
);
120 spin_lock(&mbox
->lock
);
121 ret
= !(readl(mbox
->regs
+ MAIL1_STA
) & ARM_MS_FULL
);
122 spin_unlock(&mbox
->lock
);
126 static const struct mbox_chan_ops bcm2835_mbox_chan_ops
= {
127 .send_data
= bcm2835_send_data
,
128 .startup
= bcm2835_startup
,
129 .shutdown
= bcm2835_shutdown
,
130 .last_tx_done
= bcm2835_last_tx_done
133 static struct mbox_chan
*bcm2835_mbox_index_xlate(struct mbox_controller
*mbox
,
134 const struct of_phandle_args
*sp
)
136 if (sp
->args_count
!= 0)
139 return &mbox
->chans
[0];
142 static int bcm2835_mbox_probe(struct platform_device
*pdev
)
144 struct device
*dev
= &pdev
->dev
;
146 struct resource
*iomem
;
147 struct bcm2835_mbox
*mbox
;
149 mbox
= devm_kzalloc(dev
, sizeof(*mbox
), GFP_KERNEL
);
152 spin_lock_init(&mbox
->lock
);
154 ret
= devm_request_irq(dev
, irq_of_parse_and_map(dev
->of_node
, 0),
155 bcm2835_mbox_irq
, 0, dev_name(dev
), mbox
);
157 dev_err(dev
, "Failed to register a mailbox IRQ handler: %d\n",
162 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
163 mbox
->regs
= devm_ioremap_resource(&pdev
->dev
, iomem
);
164 if (IS_ERR(mbox
->regs
)) {
165 ret
= PTR_ERR(mbox
->regs
);
166 dev_err(&pdev
->dev
, "Failed to remap mailbox regs: %d\n", ret
);
170 mbox
->controller
.txdone_poll
= true;
171 mbox
->controller
.txpoll_period
= 5;
172 mbox
->controller
.ops
= &bcm2835_mbox_chan_ops
;
173 mbox
->controller
.of_xlate
= &bcm2835_mbox_index_xlate
;
174 mbox
->controller
.dev
= dev
;
175 mbox
->controller
.num_chans
= 1;
176 mbox
->controller
.chans
= devm_kzalloc(dev
,
177 sizeof(*mbox
->controller
.chans
), GFP_KERNEL
);
178 if (!mbox
->controller
.chans
)
181 ret
= mbox_controller_register(&mbox
->controller
);
185 platform_set_drvdata(pdev
, mbox
);
186 dev_info(dev
, "mailbox enabled\n");
191 static int bcm2835_mbox_remove(struct platform_device
*pdev
)
193 struct bcm2835_mbox
*mbox
= platform_get_drvdata(pdev
);
194 mbox_controller_unregister(&mbox
->controller
);
198 static const struct of_device_id bcm2835_mbox_of_match
[] = {
199 { .compatible
= "brcm,bcm2835-mbox", },
202 MODULE_DEVICE_TABLE(of
, bcm2835_mbox_of_match
);
204 static struct platform_driver bcm2835_mbox_driver
= {
206 .name
= "bcm2835-mbox",
207 .of_match_table
= bcm2835_mbox_of_match
,
209 .probe
= bcm2835_mbox_probe
,
210 .remove
= bcm2835_mbox_remove
,
212 module_platform_driver(bcm2835_mbox_driver
);
214 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
215 MODULE_DESCRIPTION("BCM2835 mailbox IPC driver");
216 MODULE_LICENSE("GPL v2");