bcm2835_mbox.c revision 239922
1/*-
2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/arm/broadcom/bcm2835/bcm2835_mbox.c 239922 2012-08-30 20:59:37Z gonzo $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/malloc.h>
36#include <sys/rman.h>
37#include <sys/timeet.h>
38#include <sys/timetc.h>
39#include <sys/watchdog.h>
40#include <machine/bus.h>
41#include <machine/cpu.h>
42#include <machine/frame.h>
43#include <machine/intr.h>
44
45#include <dev/fdt/fdt_common.h>
46#include <dev/ofw/openfirm.h>
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.h>
49
50#include <machine/bus.h>
51#include <machine/fdt.h>
52#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
53
54#define	REG_READ	0x00
55#define	REG_POL		0x10
56#define	REG_SENDER	0x14
57#define	REG_STATUS	0x18
58#define		STATUS_FULL	0x80000000
59#define		STATUS_EMPTY	0x40000000
60#define	REG_CONFIG	0x1C
61#define		CONFIG_DATA_IRQ	0x00000001
62#define	REG_WRITE	0x20 /* This is Mailbox 1 address */
63
64#define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
65#define	MBOX_CHAN(msg)		((msg) & 0xf)
66#define	MBOX_DATA(msg)		((msg) & ~0xf)
67
68#define	MBOX_LOCK	do {		\
69	mtx_lock(&bcm_mbox_sc->lock);	\
70} while(0)
71
72#define	MBOX_UNLOCK	do {		\
73	mtx_unlock(&bcm_mbox_sc->lock);	\
74} while(0)
75
76#ifdef  DEBUG
77#define dprintf(fmt, args...) printf(fmt, ##args)
78#else
79#define dprintf(fmt, args...)
80#endif
81
82struct bcm_mbox_softc {
83	struct mtx		lock;
84	struct resource *	mem_res;
85	struct resource *	irq_res;
86	void*			intr_hl;
87	bus_space_tag_t		bst;
88	bus_space_handle_t	bsh;
89	int			valid[BCM2835_MBOX_CHANS];
90	int			msg[BCM2835_MBOX_CHANS];
91};
92
93static struct bcm_mbox_softc *bcm_mbox_sc = NULL;
94
95#define	mbox_read_4(reg)		\
96    bus_space_read_4(bcm_mbox_sc->bst, bcm_mbox_sc->bsh, reg)
97#define	mbox_write_4(reg, val)		\
98    bus_space_write_4(bcm_mbox_sc->bst, bcm_mbox_sc->bsh, reg, val)
99
100static void
101bcm_mbox_intr(void *arg)
102{
103	struct bcm_mbox_softc *sc = arg;
104	int chan;
105	uint32_t data;
106	uint32_t msg;
107
108	MBOX_LOCK;
109	while (!(mbox_read_4(REG_STATUS) & STATUS_EMPTY)) {
110		msg = mbox_read_4(REG_READ);
111		dprintf("bcm_mbox_intr: raw data %08x\n", msg);
112		chan = MBOX_CHAN(msg);
113		data = MBOX_DATA(msg);
114		if (sc->valid[chan]) {
115			printf("bcm_mbox_intr: channel %d oveflow\n", chan);
116			continue;
117		}
118		dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
119		sc->msg[chan] = data;
120		sc->valid[chan] = 1;
121		wakeup(&sc->msg[chan]);
122
123	}
124	MBOX_UNLOCK;
125}
126
127static int
128bcm_mbox_probe(device_t dev)
129{
130
131	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
132		device_set_desc(dev, "BCM2835 VideoCore Mailbox");
133		return(BUS_PROBE_DEFAULT);
134	}
135
136	return (ENXIO);
137}
138
139static int
140bcm_mbox_attach(device_t dev)
141{
142	struct bcm_mbox_softc *sc = device_get_softc(dev);
143	int i;
144	int rid = 0;
145
146	if (bcm_mbox_sc != NULL)
147		return (EINVAL);
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_TYPE_MISC,
167			NULL, bcm_mbox_intr, sc,
168			&sc->intr_hl) != 0) {
169		bus_release_resource(dev, SYS_RES_IRQ, rid,
170			sc->irq_res);
171		device_printf(dev, "Unable to setup the clock irq handler.\n");
172		return (ENXIO);
173	}
174
175	mtx_init(&sc->lock, "vcio mbox", MTX_DEF, 0);
176	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
177		sc->valid[0] = 0;
178		sc->msg[0] = 0;
179	}
180
181	bcm_mbox_sc = sc;
182	/* Read all pending messages */
183	bcm_mbox_intr(sc);
184
185	/* Should be called after bcm_mbox_sc initialization */
186	mbox_write_4(REG_CONFIG, CONFIG_DATA_IRQ);
187
188	return (0);
189}
190
191static device_method_t bcm_mbox_methods[] = {
192	DEVMETHOD(device_probe,		bcm_mbox_probe),
193	DEVMETHOD(device_attach,	bcm_mbox_attach),
194	{ 0, 0 }
195};
196
197static driver_t bcm_mbox_driver = {
198	"mbox",
199	bcm_mbox_methods,
200	sizeof(struct bcm_mbox_softc),
201};
202
203static devclass_t bcm_mbox_devclass;
204
205DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
206
207/*
208 * Mailbox API
209 */
210int
211bcm_mbox_write(int chan, uint32_t data)
212{
213	int limit = 20000;
214
215	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
216	MBOX_LOCK;
217
218	while ((mbox_read_4(REG_STATUS) & STATUS_FULL) && limit--) {
219		DELAY(2);
220	}
221
222	if (limit == 0) {
223		printf("bcm_mbox_write: STATUS_FULL stuck");
224		MBOX_UNLOCK;
225		return (EAGAIN);
226	}
227
228	mbox_write_4(REG_WRITE, MBOX_MSG(chan, data));
229
230	MBOX_UNLOCK;
231	return (0);
232}
233
234int
235bcm_mbox_read(int chan, uint32_t *data)
236{
237	struct bcm_mbox_softc *sc = bcm_mbox_sc;
238
239	dprintf("bcm_mbox_read: chan %d\n", chan);
240	MBOX_LOCK;
241	while (!sc->valid[chan])
242		msleep(&sc->msg[chan], &sc->lock, PZERO, "vcio mbox read", 0);
243	*data = bcm_mbox_sc->msg[chan];
244	bcm_mbox_sc->valid[chan] = 0;
245	MBOX_UNLOCK;
246	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
247
248	return (0);
249}
250