bcm2835_mbox.c revision 278608
1249268Sglebius/*-
2249268Sglebius * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3249268Sglebius * All rights reserved.
4249268Sglebius *
5249268Sglebius * Redistribution and use in source and binary forms, with or without
6249268Sglebius * modification, are permitted provided that the following conditions
7249268Sglebius * are met:
8249268Sglebius * 1. Redistributions of source code must retain the above copyright
9249268Sglebius *    notice, this list of conditions and the following disclaimer.
10249268Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11249268Sglebius *    notice, this list of conditions and the following disclaimer in the
12249268Sglebius *    documentation and/or other materials provided with the distribution.
13249268Sglebius *
14249268Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15249268Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16249268Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17249268Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18249268Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19249268Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20249268Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249268Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22249268Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23249268Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24249268Sglebius * SUCH DAMAGE.
25249268Sglebius */
26249268Sglebius
27249268Sglebius#include <sys/cdefs.h>
28249268Sglebius__FBSDID("$FreeBSD: stable/10/sys/arm/broadcom/bcm2835/bcm2835_mbox.c 278608 2015-02-12 00:25:33Z ian $");
29249268Sglebius
30249268Sglebius#include <sys/param.h>
31249268Sglebius#include <sys/systm.h>
32249268Sglebius#include <sys/bus.h>
33249268Sglebius#include <sys/kernel.h>
34249268Sglebius#include <sys/module.h>
35249268Sglebius#include <sys/malloc.h>
36249268Sglebius#include <sys/rman.h>
37249268Sglebius#include <sys/sema.h>
38249268Sglebius#include <sys/timeet.h>
39249268Sglebius#include <sys/timetc.h>
40252434Skib#include <sys/watchdog.h>
41252434Skib#include <machine/bus.h>
42252434Skib#include <machine/cpu.h>
43252434Skib#include <machine/intr.h>
44252434Skib
45252434Skib#include <dev/fdt/fdt_common.h>
46252434Skib#include <dev/ofw/openfirm.h>
47252434Skib#include <dev/ofw/ofw_bus.h>
48252434Skib#include <dev/ofw/ofw_bus_subr.h>
49252434Skib
50252434Skib#include <machine/bus.h>
51252434Skib#include <machine/fdt.h>
52252434Skib
53252434Skib#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
54252434Skib
55252434Skib#include "mbox_if.h"
56252434Skib
57252434Skib#define	REG_READ	0x00
58252434Skib#define	REG_POL		0x10
59252434Skib#define	REG_SENDER	0x14
60252434Skib#define	REG_STATUS	0x18
61252434Skib#define		STATUS_FULL	0x80000000
62252434Skib#define		STATUS_EMPTY	0x40000000
63252434Skib#define	REG_CONFIG	0x1C
64252434Skib#define		CONFIG_DATA_IRQ	0x00000001
65252434Skib#define	REG_WRITE	0x20 /* This is Mailbox 1 address */
66252434Skib
67252434Skib#define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
68252434Skib#define	MBOX_CHAN(msg)		((msg) & 0xf)
69252434Skib#define	MBOX_DATA(msg)		((msg) & ~0xf)
70252434Skib
71252434Skib#define	MBOX_LOCK(sc)	do {	\
72252434Skib	mtx_lock(&(sc)->lock);	\
73252434Skib} while(0)
74252434Skib
75252434Skib#define	MBOX_UNLOCK(sc)	do {		\
76252434Skib	mtx_unlock(&(sc)->lock);	\
77252434Skib} while(0)
78252434Skib
79252434Skib#undef DEBUG
80249268Sglebius#ifdef  DEBUG
81249268Sglebius#define dprintf(fmt, args...) printf(fmt, ##args)
82249268Sglebius#else
83249268Sglebius#define dprintf(fmt, args...)
84249268Sglebius#endif
85249268Sglebius
86249268Sglebiusstruct bcm_mbox_softc {
87249268Sglebius	struct mtx		lock;
88249268Sglebius	struct resource *	mem_res;
89249268Sglebius	struct resource *	irq_res;
90249268Sglebius	void*			intr_hl;
91249268Sglebius	bus_space_tag_t		bst;
92249268Sglebius	bus_space_handle_t	bsh;
93249268Sglebius	int			msg[BCM2835_MBOX_CHANS];
94249268Sglebius	struct sema		sema[BCM2835_MBOX_CHANS];
95};
96
97#define	mbox_read_4(sc, reg)		\
98    bus_space_read_4((sc)->bst, (sc)->bsh, reg)
99#define	mbox_write_4(sc, reg, val)		\
100    bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
101
102static void
103bcm_mbox_intr(void *arg)
104{
105	struct bcm_mbox_softc *sc = arg;
106	int chan;
107	uint32_t data;
108	uint32_t msg;
109
110	while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) {
111		msg = mbox_read_4(sc, REG_READ);
112		dprintf("bcm_mbox_intr: raw data %08x\n", msg);
113		chan = MBOX_CHAN(msg);
114		data = MBOX_DATA(msg);
115		if (sc->msg[chan]) {
116			printf("bcm_mbox_intr: channel %d oveflow\n", chan);
117			continue;
118		}
119		dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
120		sc->msg[chan] = msg;
121		sema_post(&sc->sema[chan]);
122	}
123}
124
125static int
126bcm_mbox_probe(device_t dev)
127{
128
129	if (!ofw_bus_status_okay(dev))
130		return (ENXIO);
131
132	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
133		device_set_desc(dev, "BCM2835 VideoCore Mailbox");
134		return(BUS_PROBE_DEFAULT);
135	}
136
137	return (ENXIO);
138}
139
140static int
141bcm_mbox_attach(device_t dev)
142{
143	struct bcm_mbox_softc *sc = device_get_softc(dev);
144	int i;
145	int rid = 0;
146
147	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
148	if (sc->mem_res == NULL) {
149		device_printf(dev, "could not allocate memory resource\n");
150		return (ENXIO);
151	}
152
153	sc->bst = rman_get_bustag(sc->mem_res);
154	sc->bsh = rman_get_bushandle(sc->mem_res);
155
156	rid = 0;
157	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
158	if (sc->irq_res == NULL) {
159		device_printf(dev, "could not allocate interrupt resource\n");
160		return (ENXIO);
161	}
162
163	/* Setup and enable the timer */
164	if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
165	    NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
166		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
167		device_printf(dev, "Unable to setup the clock irq handler.\n");
168		return (ENXIO);
169	}
170
171	mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
172	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
173		sc->msg[i] = 0;
174		sema_init(&sc->sema[i], 0, "mbox");
175	}
176
177	/* Read all pending messages */
178	while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
179		(void)mbox_read_4(sc, REG_READ);
180
181	mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
182
183	return (0);
184}
185
186/*
187 * Mailbox API
188 */
189static int
190bcm_mbox_write(device_t dev, int chan, uint32_t data)
191{
192	int limit = 20000;
193	struct bcm_mbox_softc *sc = device_get_softc(dev);
194
195	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
196	MBOX_LOCK(sc);
197
198	while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && limit--) {
199		DELAY(2);
200	}
201
202	if (limit == 0) {
203		printf("bcm_mbox_write: STATUS_FULL stuck");
204		MBOX_UNLOCK(sc);
205		return (EAGAIN);
206	}
207
208	mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
209
210	MBOX_UNLOCK(sc);
211	return (0);
212}
213
214static int
215bcm_mbox_read(device_t dev, int chan, uint32_t *data)
216{
217	struct bcm_mbox_softc *sc = device_get_softc(dev);
218
219	dprintf("bcm_mbox_read: chan %d\n", chan);
220	MBOX_LOCK(sc);
221	while (sema_trywait(&sc->sema[chan]) == 0) {
222		/* do not unlock sc while waiting for the mbox */
223		if (sema_timedwait(&sc->sema[chan], 10*hz) == 0)
224			break;
225		printf("timeout sema for chan %d\n", chan);
226	}
227	/*
228	 *  get data from intr handler, the same channel is never coming
229	 *  because of holding sc lock.
230	 */
231	*data = MBOX_DATA(sc->msg[chan]);
232	sc->msg[chan] = 0;
233	MBOX_UNLOCK(sc);
234	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
235
236	return (0);
237}
238
239static device_method_t bcm_mbox_methods[] = {
240	DEVMETHOD(device_probe,		bcm_mbox_probe),
241	DEVMETHOD(device_attach,	bcm_mbox_attach),
242
243	DEVMETHOD(mbox_read,		bcm_mbox_read),
244	DEVMETHOD(mbox_write,		bcm_mbox_write),
245
246	DEVMETHOD_END
247};
248
249static driver_t bcm_mbox_driver = {
250	"mbox",
251	bcm_mbox_methods,
252	sizeof(struct bcm_mbox_softc),
253};
254
255static devclass_t bcm_mbox_devclass;
256
257DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
258
259