brcm2708: update 4.1 patches
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0109-mailbox-Enable-BCM2835-mailbox-support.patch
1 From 97b130d04ed860f89812755c8d576a548b13bd3b Mon Sep 17 00:00:00 2001
2 From: Lubomir Rintel <lkundrak@v3.sk>
3 Date: Tue, 5 May 2015 13:27:45 -0700
4 Subject: [PATCH 109/148] mailbox: Enable BCM2835 mailbox support
5
6 This mailbox driver provides a single mailbox channel to write 32-bit
7 values to the VPU and get a 32-bit response. The Raspberry Pi
8 firmware uses this mailbox channel to implement firmware calls, while
9 Roku 2 (despite being derived from the same firmware tree) doesn't.
10
11 The driver was originally submitted by Lubomir, based on the
12 out-of-tree 2708 mailbox driver. Eric Anholt fixed it up for
13 upstreaming, with the major functional change being that it now has no
14 notion of multiple channels (since that is a firmware-dependent
15 concept) and instead the raspberrypi-firmware driver will do that
16 bit-twiddling in its own messages.
17 [Jassi: made the 'mbox_chan_ops' struct as const and removed a redundant
18 variable]
19
20 Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
21 Signed-off-by: Craig McGeachie <slapdau@yahoo.com.au>
22 Signed-off-by: Eric Anholt <eric@anholt.net>
23 Acked-by: Stephen Warren <swarren@wwwdotorg.org>
24 Signed-off-by: Jassi Brar <jaswinder.singh@linaro.org>
25 ---
26 drivers/mailbox/Kconfig | 9 ++
27 drivers/mailbox/Makefile | 2 +
28 drivers/mailbox/bcm2835-mailbox.c | 216 ++++++++++++++++++++++++++++++++++++++
29 3 files changed, 227 insertions(+)
30 create mode 100644 drivers/mailbox/bcm2835-mailbox.c
31
32 --- a/drivers/mailbox/Kconfig
33 +++ b/drivers/mailbox/Kconfig
34 @@ -66,4 +66,13 @@ config ALTERA_MBOX
35 An implementation of the Altera Mailbox soft core. It is used
36 to send message between processors. Say Y here if you want to use the
37 Altera mailbox support.
38 +
39 +config BCM2835_MBOX
40 + tristate "BCM2835 Mailbox"
41 + depends on ARCH_BCM2835
42 + help
43 + An implementation of the BCM2385 Mailbox. It is used to invoke
44 + the services of the Videocore. Say Y here if you want to use the
45 + BCM2835 Mailbox.
46 +
47 endif
48 --- a/drivers/mailbox/Makefile
49 +++ b/drivers/mailbox/Makefile
50 @@ -13,3 +13,5 @@ obj-$(CONFIG_OMAP2PLUS_MBOX) += omap-mai
51 obj-$(CONFIG_PCC) += pcc.o
52
53 obj-$(CONFIG_ALTERA_MBOX) += mailbox-altera.o
54 +
55 +obj-$(CONFIG_BCM2835_MBOX) += bcm2835-mailbox.o
56 --- /dev/null
57 +++ b/drivers/mailbox/bcm2835-mailbox.c
58 @@ -0,0 +1,216 @@
59 +/*
60 + * Copyright (C) 2010,2015 Broadcom
61 + * Copyright (C) 2013-2014 Lubomir Rintel
62 + * Copyright (C) 2013 Craig McGeachie
63 + *
64 + * This program is free software; you can redistribute it and/or modify
65 + * it under the terms of the GNU General Public License version 2 as
66 + * published by the Free Software Foundation.
67 + *
68 + * This device provides a mechanism for writing to the mailboxes,
69 + * that are shared between the ARM and the VideoCore processor
70 + *
71 + * Parts of the driver are based on:
72 + * - arch/arm/mach-bcm2708/vcio.c file written by Gray Girling that was
73 + * obtained from branch "rpi-3.6.y" of git://github.com/raspberrypi/
74 + * linux.git
75 + * - drivers/mailbox/bcm2835-ipc.c by Lubomir Rintel at
76 + * https://github.com/hackerspace/rpi-linux/blob/lr-raspberry-pi/drivers/
77 + * mailbox/bcm2835-ipc.c
78 + * - documentation available on the following web site:
79 + * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
80 + */
81 +
82 +#include <linux/device.h>
83 +#include <linux/dma-mapping.h>
84 +#include <linux/err.h>
85 +#include <linux/interrupt.h>
86 +#include <linux/irq.h>
87 +#include <linux/kernel.h>
88 +#include <linux/mailbox_controller.h>
89 +#include <linux/module.h>
90 +#include <linux/of_address.h>
91 +#include <linux/of_irq.h>
92 +#include <linux/platform_device.h>
93 +#include <linux/spinlock.h>
94 +
95 +/* Mailboxes */
96 +#define ARM_0_MAIL0 0x00
97 +#define ARM_0_MAIL1 0x20
98 +
99 +/*
100 + * Mailbox registers. We basically only support mailbox 0 & 1. We
101 + * deliver to the VC in mailbox 1, it delivers to us in mailbox 0. See
102 + * BCM2835-ARM-Peripherals.pdf section 1.3 for an explanation about
103 + * the placement of memory barriers.
104 + */
105 +#define MAIL0_RD (ARM_0_MAIL0 + 0x00)
106 +#define MAIL0_POL (ARM_0_MAIL0 + 0x10)
107 +#define MAIL0_STA (ARM_0_MAIL0 + 0x18)
108 +#define MAIL0_CNF (ARM_0_MAIL0 + 0x1C)
109 +#define MAIL1_WRT (ARM_0_MAIL1 + 0x00)
110 +
111 +/* Status register: FIFO state. */
112 +#define ARM_MS_FULL BIT(31)
113 +#define ARM_MS_EMPTY BIT(30)
114 +
115 +/* Configuration register: Enable interrupts. */
116 +#define ARM_MC_IHAVEDATAIRQEN BIT(0)
117 +
118 +struct bcm2835_mbox {
119 + void __iomem *regs;
120 + spinlock_t lock;
121 + struct mbox_controller controller;
122 +};
123 +
124 +static struct bcm2835_mbox *bcm2835_link_mbox(struct mbox_chan *link)
125 +{
126 + return container_of(link->mbox, struct bcm2835_mbox, controller);
127 +}
128 +
129 +static irqreturn_t bcm2835_mbox_irq(int irq, void *dev_id)
130 +{
131 + struct bcm2835_mbox *mbox = dev_id;
132 + struct device *dev = mbox->controller.dev;
133 + struct mbox_chan *link = &mbox->controller.chans[0];
134 +
135 + while (!(readl(mbox->regs + MAIL0_STA) & ARM_MS_EMPTY)) {
136 + u32 msg = readl(mbox->regs + MAIL0_RD);
137 + dev_dbg(dev, "Reply 0x%08X\n", msg);
138 + mbox_chan_received_data(link, &msg);
139 + }
140 + return IRQ_HANDLED;
141 +}
142 +
143 +static int bcm2835_send_data(struct mbox_chan *link, void *data)
144 +{
145 + struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
146 + u32 msg = *(u32 *)data;
147 +
148 + spin_lock(&mbox->lock);
149 + writel(msg, mbox->regs + MAIL1_WRT);
150 + dev_dbg(mbox->controller.dev, "Request 0x%08X\n", msg);
151 + spin_unlock(&mbox->lock);
152 + return 0;
153 +}
154 +
155 +static int bcm2835_startup(struct mbox_chan *link)
156 +{
157 + struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
158 +
159 + /* Enable the interrupt on data reception */
160 + writel(ARM_MC_IHAVEDATAIRQEN, mbox->regs + MAIL0_CNF);
161 +
162 + return 0;
163 +}
164 +
165 +static void bcm2835_shutdown(struct mbox_chan *link)
166 +{
167 + struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
168 +
169 + writel(0, mbox->regs + MAIL0_CNF);
170 +}
171 +
172 +static bool bcm2835_last_tx_done(struct mbox_chan *link)
173 +{
174 + struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
175 + bool ret;
176 +
177 + spin_lock(&mbox->lock);
178 + ret = !(readl(mbox->regs + MAIL0_STA) & ARM_MS_FULL);
179 + spin_unlock(&mbox->lock);
180 + return ret;
181 +}
182 +
183 +static const struct mbox_chan_ops bcm2835_mbox_chan_ops = {
184 + .send_data = bcm2835_send_data,
185 + .startup = bcm2835_startup,
186 + .shutdown = bcm2835_shutdown,
187 + .last_tx_done = bcm2835_last_tx_done
188 +};
189 +
190 +static struct mbox_chan *bcm2835_mbox_index_xlate(struct mbox_controller *mbox,
191 + const struct of_phandle_args *sp)
192 +{
193 + if (sp->args_count != 0)
194 + return NULL;
195 +
196 + return &mbox->chans[0];
197 +}
198 +
199 +static int bcm2835_mbox_probe(struct platform_device *pdev)
200 +{
201 + struct device *dev = &pdev->dev;
202 + int ret = 0;
203 + struct resource *iomem;
204 + struct bcm2835_mbox *mbox;
205 +
206 + mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
207 + if (mbox == NULL)
208 + return -ENOMEM;
209 + spin_lock_init(&mbox->lock);
210 +
211 + ret = devm_request_irq(dev, irq_of_parse_and_map(dev->of_node, 0),
212 + bcm2835_mbox_irq, 0, dev_name(dev), mbox);
213 + if (ret) {
214 + dev_err(dev, "Failed to register a mailbox IRQ handler: %d\n",
215 + ret);
216 + return -ENODEV;
217 + }
218 +
219 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
220 + mbox->regs = devm_ioremap_resource(&pdev->dev, iomem);
221 + if (IS_ERR(mbox->regs)) {
222 + ret = PTR_ERR(mbox->regs);
223 + dev_err(&pdev->dev, "Failed to remap mailbox regs: %d\n", ret);
224 + return ret;
225 + }
226 +
227 + mbox->controller.txdone_poll = true;
228 + mbox->controller.txpoll_period = 5;
229 + mbox->controller.ops = &bcm2835_mbox_chan_ops;
230 + mbox->controller.of_xlate = &bcm2835_mbox_index_xlate;
231 + mbox->controller.dev = dev;
232 + mbox->controller.num_chans = 1;
233 + mbox->controller.chans = devm_kzalloc(dev,
234 + sizeof(*mbox->controller.chans), GFP_KERNEL);
235 + if (!mbox->controller.chans)
236 + return -ENOMEM;
237 +
238 + ret = mbox_controller_register(&mbox->controller);
239 + if (ret)
240 + return ret;
241 +
242 + platform_set_drvdata(pdev, mbox);
243 + dev_info(dev, "mailbox enabled\n");
244 +
245 + return ret;
246 +}
247 +
248 +static int bcm2835_mbox_remove(struct platform_device *pdev)
249 +{
250 + struct bcm2835_mbox *mbox = platform_get_drvdata(pdev);
251 + mbox_controller_unregister(&mbox->controller);
252 + return 0;
253 +}
254 +
255 +static const struct of_device_id bcm2835_mbox_of_match[] = {
256 + { .compatible = "brcm,bcm2835-mbox", },
257 + {},
258 +};
259 +MODULE_DEVICE_TABLE(of, bcm2835_mbox_of_match);
260 +
261 +static struct platform_driver bcm2835_mbox_driver = {
262 + .driver = {
263 + .name = "bcm2835-mbox",
264 + .owner = THIS_MODULE,
265 + .of_match_table = bcm2835_mbox_of_match,
266 + },
267 + .probe = bcm2835_mbox_probe,
268 + .remove = bcm2835_mbox_remove,
269 +};
270 +module_platform_driver(bcm2835_mbox_driver);
271 +
272 +MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
273 +MODULE_DESCRIPTION("BCM2835 mailbox IPC driver");
274 +MODULE_LICENSE("GPL v2");