1239922Sgonzo/*-
2239922Sgonzo * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3239922Sgonzo * All rights reserved.
4239922Sgonzo *
5239922Sgonzo * Redistribution and use in source and binary forms, with or without
6239922Sgonzo * modification, are permitted provided that the following conditions
7239922Sgonzo * are met:
8239922Sgonzo * 1. Redistributions of source code must retain the above copyright
9239922Sgonzo *    notice, this list of conditions and the following disclaimer.
10239922Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
11239922Sgonzo *    notice, this list of conditions and the following disclaimer in the
12239922Sgonzo *    documentation and/or other materials provided with the distribution.
13239922Sgonzo *
14239922Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239922Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239922Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239922Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239922Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19239922Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20239922Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21239922Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22239922Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23239922Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24239922Sgonzo * SUCH DAMAGE.
25239922Sgonzo */
26239922Sgonzo
27239922Sgonzo#include <sys/cdefs.h>
28239922Sgonzo__FBSDID("$FreeBSD: releng/10.2/sys/arm/broadcom/bcm2835/bcm2835_mbox.c 278769 2015-02-14 18:45:43Z loos $");
29239922Sgonzo
30239922Sgonzo#include <sys/param.h>
31239922Sgonzo#include <sys/systm.h>
32239922Sgonzo#include <sys/bus.h>
33239922Sgonzo#include <sys/kernel.h>
34278769Sloos#include <sys/lock.h>
35239922Sgonzo#include <sys/module.h>
36278769Sloos#include <sys/mutex.h>
37239922Sgonzo#include <sys/rman.h>
38278608Sian#include <sys/sema.h>
39239922Sgonzo#include <machine/bus.h>
40239922Sgonzo
41239922Sgonzo#include <dev/ofw/ofw_bus.h>
42239922Sgonzo#include <dev/ofw/ofw_bus_subr.h>
43239922Sgonzo
44239922Sgonzo#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
45239922Sgonzo
46253006Srpaulo#include "mbox_if.h"
47253006Srpaulo
48239922Sgonzo#define	REG_READ	0x00
49239922Sgonzo#define	REG_POL		0x10
50239922Sgonzo#define	REG_SENDER	0x14
51239922Sgonzo#define	REG_STATUS	0x18
52239922Sgonzo#define		STATUS_FULL	0x80000000
53239922Sgonzo#define		STATUS_EMPTY	0x40000000
54239922Sgonzo#define	REG_CONFIG	0x1C
55239922Sgonzo#define		CONFIG_DATA_IRQ	0x00000001
56239922Sgonzo#define	REG_WRITE	0x20 /* This is Mailbox 1 address */
57239922Sgonzo
58239922Sgonzo#define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
59239922Sgonzo#define	MBOX_CHAN(msg)		((msg) & 0xf)
60239922Sgonzo#define	MBOX_DATA(msg)		((msg) & ~0xf)
61239922Sgonzo
62253006Srpaulo#define	MBOX_LOCK(sc)	do {	\
63253006Srpaulo	mtx_lock(&(sc)->lock);	\
64239922Sgonzo} while(0)
65239922Sgonzo
66253006Srpaulo#define	MBOX_UNLOCK(sc)	do {		\
67253006Srpaulo	mtx_unlock(&(sc)->lock);	\
68239922Sgonzo} while(0)
69239922Sgonzo
70239922Sgonzo#ifdef  DEBUG
71239922Sgonzo#define dprintf(fmt, args...) printf(fmt, ##args)
72239922Sgonzo#else
73239922Sgonzo#define dprintf(fmt, args...)
74239922Sgonzo#endif
75239922Sgonzo
76239922Sgonzostruct bcm_mbox_softc {
77239922Sgonzo	struct mtx		lock;
78239922Sgonzo	struct resource *	mem_res;
79239922Sgonzo	struct resource *	irq_res;
80239922Sgonzo	void*			intr_hl;
81239922Sgonzo	bus_space_tag_t		bst;
82239922Sgonzo	bus_space_handle_t	bsh;
83239922Sgonzo	int			msg[BCM2835_MBOX_CHANS];
84278608Sian	struct sema		sema[BCM2835_MBOX_CHANS];
85239922Sgonzo};
86239922Sgonzo
87253006Srpaulo#define	mbox_read_4(sc, reg)		\
88253006Srpaulo    bus_space_read_4((sc)->bst, (sc)->bsh, reg)
89253006Srpaulo#define	mbox_write_4(sc, reg, val)		\
90253006Srpaulo    bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
91239922Sgonzo
92239922Sgonzostatic void
93239922Sgonzobcm_mbox_intr(void *arg)
94239922Sgonzo{
95239922Sgonzo	struct bcm_mbox_softc *sc = arg;
96239922Sgonzo	int chan;
97239922Sgonzo	uint32_t data;
98239922Sgonzo	uint32_t msg;
99239922Sgonzo
100253006Srpaulo	while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) {
101253006Srpaulo		msg = mbox_read_4(sc, REG_READ);
102239922Sgonzo		dprintf("bcm_mbox_intr: raw data %08x\n", msg);
103239922Sgonzo		chan = MBOX_CHAN(msg);
104239922Sgonzo		data = MBOX_DATA(msg);
105278608Sian		if (sc->msg[chan]) {
106239922Sgonzo			printf("bcm_mbox_intr: channel %d oveflow\n", chan);
107239922Sgonzo			continue;
108239922Sgonzo		}
109239922Sgonzo		dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
110278608Sian		sc->msg[chan] = msg;
111278608Sian		sema_post(&sc->sema[chan]);
112239922Sgonzo	}
113239922Sgonzo}
114239922Sgonzo
115239922Sgonzostatic int
116239922Sgonzobcm_mbox_probe(device_t dev)
117239922Sgonzo{
118239922Sgonzo
119266152Sian	if (!ofw_bus_status_okay(dev))
120266152Sian		return (ENXIO);
121266152Sian
122239922Sgonzo	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
123239922Sgonzo		device_set_desc(dev, "BCM2835 VideoCore Mailbox");
124239922Sgonzo		return(BUS_PROBE_DEFAULT);
125239922Sgonzo	}
126239922Sgonzo
127239922Sgonzo	return (ENXIO);
128239922Sgonzo}
129239922Sgonzo
130239922Sgonzostatic int
131239922Sgonzobcm_mbox_attach(device_t dev)
132239922Sgonzo{
133239922Sgonzo	struct bcm_mbox_softc *sc = device_get_softc(dev);
134239922Sgonzo	int i;
135239922Sgonzo	int rid = 0;
136239922Sgonzo
137239922Sgonzo	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
138239922Sgonzo	if (sc->mem_res == NULL) {
139239922Sgonzo		device_printf(dev, "could not allocate memory resource\n");
140239922Sgonzo		return (ENXIO);
141239922Sgonzo	}
142239922Sgonzo
143239922Sgonzo	sc->bst = rman_get_bustag(sc->mem_res);
144239922Sgonzo	sc->bsh = rman_get_bushandle(sc->mem_res);
145239922Sgonzo
146239922Sgonzo	rid = 0;
147239922Sgonzo	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
148239922Sgonzo	if (sc->irq_res == NULL) {
149239922Sgonzo		device_printf(dev, "could not allocate interrupt resource\n");
150239922Sgonzo		return (ENXIO);
151239922Sgonzo	}
152239922Sgonzo
153239922Sgonzo	/* Setup and enable the timer */
154252450Srpaulo	if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
155252450Srpaulo	    NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
156252450Srpaulo		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
157239922Sgonzo		device_printf(dev, "Unable to setup the clock irq handler.\n");
158239922Sgonzo		return (ENXIO);
159239922Sgonzo	}
160239922Sgonzo
161262586Sbrueffer	mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
162239922Sgonzo	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
163278608Sian		sc->msg[i] = 0;
164278608Sian		sema_init(&sc->sema[i], 0, "mbox");
165239922Sgonzo	}
166239922Sgonzo
167239922Sgonzo	/* Read all pending messages */
168278608Sian	while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
169278608Sian		(void)mbox_read_4(sc, REG_READ);
170239922Sgonzo
171253006Srpaulo	mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
172239922Sgonzo
173239922Sgonzo	return (0);
174239922Sgonzo}
175239922Sgonzo
176239922Sgonzo/*
177239922Sgonzo * Mailbox API
178239922Sgonzo */
179253006Srpaulostatic int
180253006Srpaulobcm_mbox_write(device_t dev, int chan, uint32_t data)
181239922Sgonzo{
182278769Sloos	int limit = 1000;
183253006Srpaulo	struct bcm_mbox_softc *sc = device_get_softc(dev);
184239922Sgonzo
185239922Sgonzo	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
186253006Srpaulo	MBOX_LOCK(sc);
187278769Sloos	while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)
188278769Sloos		DELAY(5);
189239922Sgonzo	if (limit == 0) {
190239922Sgonzo		printf("bcm_mbox_write: STATUS_FULL stuck");
191253006Srpaulo		MBOX_UNLOCK(sc);
192239922Sgonzo		return (EAGAIN);
193239922Sgonzo	}
194253006Srpaulo	mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
195278769Sloos	MBOX_UNLOCK(sc);
196239922Sgonzo
197239922Sgonzo	return (0);
198239922Sgonzo}
199239922Sgonzo
200253006Srpaulostatic int
201253006Srpaulobcm_mbox_read(device_t dev, int chan, uint32_t *data)
202239922Sgonzo{
203253006Srpaulo	struct bcm_mbox_softc *sc = device_get_softc(dev);
204239922Sgonzo
205239922Sgonzo	dprintf("bcm_mbox_read: chan %d\n", chan);
206253006Srpaulo	MBOX_LOCK(sc);
207278608Sian	while (sema_trywait(&sc->sema[chan]) == 0) {
208278608Sian		/* do not unlock sc while waiting for the mbox */
209278608Sian		if (sema_timedwait(&sc->sema[chan], 10*hz) == 0)
210278608Sian			break;
211278608Sian		printf("timeout sema for chan %d\n", chan);
212278608Sian	}
213278608Sian	/*
214278608Sian	 *  get data from intr handler, the same channel is never coming
215278608Sian	 *  because of holding sc lock.
216278608Sian	 */
217278608Sian	*data = MBOX_DATA(sc->msg[chan]);
218278608Sian	sc->msg[chan] = 0;
219253006Srpaulo	MBOX_UNLOCK(sc);
220239922Sgonzo	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
221239922Sgonzo
222239922Sgonzo	return (0);
223239922Sgonzo}
224253006Srpaulo
225253006Srpaulostatic device_method_t bcm_mbox_methods[] = {
226253006Srpaulo	DEVMETHOD(device_probe,		bcm_mbox_probe),
227253006Srpaulo	DEVMETHOD(device_attach,	bcm_mbox_attach),
228253006Srpaulo
229253006Srpaulo	DEVMETHOD(mbox_read,		bcm_mbox_read),
230253006Srpaulo	DEVMETHOD(mbox_write,		bcm_mbox_write),
231253006Srpaulo
232253006Srpaulo	DEVMETHOD_END
233253006Srpaulo};
234253006Srpaulo
235253006Srpaulostatic driver_t bcm_mbox_driver = {
236253006Srpaulo	"mbox",
237253006Srpaulo	bcm_mbox_methods,
238253006Srpaulo	sizeof(struct bcm_mbox_softc),
239253006Srpaulo};
240253006Srpaulo
241253006Srpaulostatic devclass_t bcm_mbox_devclass;
242253006Srpaulo
243253006SrpauloDRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
244