bcm2835_mbox.c revision 266152
1142425Snectar/*-
2160814Ssimon * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3142425Snectar * All rights reserved.
4142425Snectar *
5142425Snectar * Redistribution and use in source and binary forms, with or without
6142425Snectar * modification, are permitted provided that the following conditions
7142425Snectar * are met:
8142425Snectar * 1. Redistributions of source code must retain the above copyright
9142425Snectar *    notice, this list of conditions and the following disclaimer.
10142425Snectar * 2. Redistributions in binary form must reproduce the above copyright
11142425Snectar *    notice, this list of conditions and the following disclaimer in the
12142425Snectar *    documentation and/or other materials provided with the distribution.
13142425Snectar *
14142425Snectar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15142425Snectar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16142425Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17142425Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18142425Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19142425Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20142425Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21142425Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22142425Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23142425Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24142425Snectar * SUCH DAMAGE.
25142425Snectar */
26142425Snectar
27142425Snectar#include <sys/cdefs.h>
28142425Snectar__FBSDID("$FreeBSD: stable/10/sys/arm/broadcom/bcm2835/bcm2835_mbox.c 266152 2014-05-15 16:11:06Z ian $");
29142425Snectar
30142425Snectar#include <sys/param.h>
31142425Snectar#include <sys/systm.h>
32142425Snectar#include <sys/bus.h>
33142425Snectar#include <sys/kernel.h>
34142425Snectar#include <sys/module.h>
35142425Snectar#include <sys/malloc.h>
36142425Snectar#include <sys/rman.h>
37142425Snectar#include <sys/timeet.h>
38194206Ssimon#include <sys/timetc.h>
39142425Snectar#include <sys/watchdog.h>
40142425Snectar#include <machine/bus.h>
41142425Snectar#include <machine/cpu.h>
42142425Snectar#include <machine/intr.h>
43142425Snectar
44142425Snectar#include <dev/fdt/fdt_common.h>
45142425Snectar#include <dev/ofw/openfirm.h>
46142425Snectar#include <dev/ofw/ofw_bus.h>
47142425Snectar#include <dev/ofw/ofw_bus_subr.h>
48142425Snectar
49142425Snectar#include <machine/bus.h>
50142425Snectar#include <machine/fdt.h>
51160814Ssimon
52160814Ssimon#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
53142425Snectar
54142425Snectar#include "mbox_if.h"
55142425Snectar
56142425Snectar#define	REG_READ	0x00
57142425Snectar#define	REG_POL		0x10
58142425Snectar#define	REG_SENDER	0x14
59142425Snectar#define	REG_STATUS	0x18
60142425Snectar#define		STATUS_FULL	0x80000000
61142425Snectar#define		STATUS_EMPTY	0x40000000
62142425Snectar#define	REG_CONFIG	0x1C
63142425Snectar#define		CONFIG_DATA_IRQ	0x00000001
64142425Snectar#define	REG_WRITE	0x20 /* This is Mailbox 1 address */
65142425Snectar
66142425Snectar#define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
67160814Ssimon#define	MBOX_CHAN(msg)		((msg) & 0xf)
68142425Snectar#define	MBOX_DATA(msg)		((msg) & ~0xf)
69142425Snectar
70142425Snectar#define	MBOX_LOCK(sc)	do {	\
71142425Snectar	mtx_lock(&(sc)->lock);	\
72142425Snectar} while(0)
73142425Snectar
74142425Snectar#define	MBOX_UNLOCK(sc)	do {		\
75142425Snectar	mtx_unlock(&(sc)->lock);	\
76142425Snectar} while(0)
77142425Snectar
78142425Snectar#ifdef  DEBUG
79142425Snectar#define dprintf(fmt, args...) printf(fmt, ##args)
80142425Snectar#else
81142425Snectar#define dprintf(fmt, args...)
82142425Snectar#endif
83142425Snectar
84160814Ssimonstruct bcm_mbox_softc {
85160814Ssimon	struct mtx		lock;
86160814Ssimon	struct resource *	mem_res;
87142425Snectar	struct resource *	irq_res;
88142425Snectar	void*			intr_hl;
89142425Snectar	bus_space_tag_t		bst;
90142425Snectar	bus_space_handle_t	bsh;
91142425Snectar	int			valid[BCM2835_MBOX_CHANS];
92160814Ssimon	int			msg[BCM2835_MBOX_CHANS];
93160814Ssimon};
94160814Ssimon
95142425Snectar#define	mbox_read_4(sc, reg)		\
96142425Snectar    bus_space_read_4((sc)->bst, (sc)->bsh, reg)
97142425Snectar#define	mbox_write_4(sc, reg, val)		\
98142425Snectar    bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
99160814Ssimon
100160814Ssimonstatic void
101160814Ssimonbcm_mbox_intr(void *arg)
102142425Snectar{
103142425Snectar	struct bcm_mbox_softc *sc = arg;
104142425Snectar	int chan;
105142425Snectar	uint32_t data;
106142425Snectar	uint32_t msg;
107160814Ssimon
108160814Ssimon	MBOX_LOCK(sc);
109160814Ssimon	while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) {
110142425Snectar		msg = mbox_read_4(sc, REG_READ);
111142425Snectar		dprintf("bcm_mbox_intr: raw data %08x\n", msg);
112142425Snectar		chan = MBOX_CHAN(msg);
113142425Snectar		data = MBOX_DATA(msg);
114142425Snectar		if (sc->valid[chan]) {
115160814Ssimon			printf("bcm_mbox_intr: channel %d oveflow\n", chan);
116160814Ssimon			continue;
117160814Ssimon		}
118142425Snectar		dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
119142425Snectar		sc->msg[chan] = data;
120142425Snectar		sc->valid[chan] = 1;
121142425Snectar		wakeup(&sc->msg[chan]);
122142425Snectar
123142425Snectar	}
124160814Ssimon	MBOX_UNLOCK(sc);
125142425Snectar}
126142425Snectar
127142425Snectarstatic int
128142425Snectarbcm_mbox_probe(device_t dev)
129142425Snectar{
130142425Snectar
131142425Snectar	if (!ofw_bus_status_okay(dev))
132160814Ssimon		return (ENXIO);
133160814Ssimon
134160814Ssimon	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
135142425Snectar		device_set_desc(dev, "BCM2835 VideoCore Mailbox");
136142425Snectar		return(BUS_PROBE_DEFAULT);
137142425Snectar	}
138142425Snectar
139142425Snectar	return (ENXIO);
140160814Ssimon}
141160814Ssimon
142160814Ssimonstatic int
143bcm_mbox_attach(device_t dev)
144{
145	struct bcm_mbox_softc *sc = device_get_softc(dev);
146	int i;
147	int rid = 0;
148
149	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
150	if (sc->mem_res == NULL) {
151		device_printf(dev, "could not allocate memory resource\n");
152		return (ENXIO);
153	}
154
155	sc->bst = rman_get_bustag(sc->mem_res);
156	sc->bsh = rman_get_bushandle(sc->mem_res);
157
158	rid = 0;
159	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
160	if (sc->irq_res == NULL) {
161		device_printf(dev, "could not allocate interrupt resource\n");
162		return (ENXIO);
163	}
164
165	/* Setup and enable the timer */
166	if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
167	    NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
168		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
169		device_printf(dev, "Unable to setup the clock irq handler.\n");
170		return (ENXIO);
171	}
172
173	mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
174	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
175		sc->valid[0] = 0;
176		sc->msg[0] = 0;
177	}
178
179	/* Read all pending messages */
180	bcm_mbox_intr(sc);
181
182	mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
183
184	return (0);
185}
186
187/*
188 * Mailbox API
189 */
190static int
191bcm_mbox_write(device_t dev, int chan, uint32_t data)
192{
193	int limit = 20000;
194	struct bcm_mbox_softc *sc = device_get_softc(dev);
195
196	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
197	MBOX_LOCK(sc);
198
199	while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && limit--) {
200		DELAY(2);
201	}
202
203	if (limit == 0) {
204		printf("bcm_mbox_write: STATUS_FULL stuck");
205		MBOX_UNLOCK(sc);
206		return (EAGAIN);
207	}
208
209	mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
210
211	MBOX_UNLOCK(sc);
212	return (0);
213}
214
215static int
216bcm_mbox_read(device_t dev, int chan, uint32_t *data)
217{
218	struct bcm_mbox_softc *sc = device_get_softc(dev);
219
220	dprintf("bcm_mbox_read: chan %d\n", chan);
221	MBOX_LOCK(sc);
222	while (!sc->valid[chan])
223		msleep(&sc->msg[chan], &sc->lock, PZERO, "vcio mbox read", 0);
224	*data = sc->msg[chan];
225	sc->valid[chan] = 0;
226	MBOX_UNLOCK(sc);
227	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
228
229	return (0);
230}
231
232static device_method_t bcm_mbox_methods[] = {
233	DEVMETHOD(device_probe,		bcm_mbox_probe),
234	DEVMETHOD(device_attach,	bcm_mbox_attach),
235
236	DEVMETHOD(mbox_read,		bcm_mbox_read),
237	DEVMETHOD(mbox_write,		bcm_mbox_write),
238
239	DEVMETHOD_END
240};
241
242static driver_t bcm_mbox_driver = {
243	"mbox",
244	bcm_mbox_methods,
245	sizeof(struct bcm_mbox_softc),
246};
247
248static devclass_t bcm_mbox_devclass;
249
250DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
251
252