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$");
29239922Sgonzo
30239922Sgonzo#include <sys/param.h>
31239922Sgonzo#include <sys/systm.h>
32239922Sgonzo#include <sys/bus.h>
33239922Sgonzo#include <sys/kernel.h>
34239922Sgonzo#include <sys/module.h>
35239922Sgonzo#include <sys/malloc.h>
36239922Sgonzo#include <sys/rman.h>
37239922Sgonzo#include <sys/timeet.h>
38239922Sgonzo#include <sys/timetc.h>
39239922Sgonzo#include <sys/watchdog.h>
40239922Sgonzo#include <machine/bus.h>
41239922Sgonzo#include <machine/cpu.h>
42239922Sgonzo#include <machine/intr.h>
43239922Sgonzo
44239922Sgonzo#include <dev/fdt/fdt_common.h>
45239922Sgonzo#include <dev/ofw/openfirm.h>
46239922Sgonzo#include <dev/ofw/ofw_bus.h>
47239922Sgonzo#include <dev/ofw/ofw_bus_subr.h>
48239922Sgonzo
49239922Sgonzo#include <machine/bus.h>
50239922Sgonzo#include <machine/fdt.h>
51253006Srpaulo
52239922Sgonzo#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
53239922Sgonzo
54253006Srpaulo#include "mbox_if.h"
55253006Srpaulo
56239922Sgonzo#define	REG_READ	0x00
57239922Sgonzo#define	REG_POL		0x10
58239922Sgonzo#define	REG_SENDER	0x14
59239922Sgonzo#define	REG_STATUS	0x18
60239922Sgonzo#define		STATUS_FULL	0x80000000
61239922Sgonzo#define		STATUS_EMPTY	0x40000000
62239922Sgonzo#define	REG_CONFIG	0x1C
63239922Sgonzo#define		CONFIG_DATA_IRQ	0x00000001
64239922Sgonzo#define	REG_WRITE	0x20 /* This is Mailbox 1 address */
65239922Sgonzo
66239922Sgonzo#define	MBOX_MSG(chan, data)	(((data) & ~0xf) | ((chan) & 0xf))
67239922Sgonzo#define	MBOX_CHAN(msg)		((msg) & 0xf)
68239922Sgonzo#define	MBOX_DATA(msg)		((msg) & ~0xf)
69239922Sgonzo
70253006Srpaulo#define	MBOX_LOCK(sc)	do {	\
71253006Srpaulo	mtx_lock(&(sc)->lock);	\
72239922Sgonzo} while(0)
73239922Sgonzo
74253006Srpaulo#define	MBOX_UNLOCK(sc)	do {		\
75253006Srpaulo	mtx_unlock(&(sc)->lock);	\
76239922Sgonzo} while(0)
77239922Sgonzo
78239922Sgonzo#ifdef  DEBUG
79239922Sgonzo#define dprintf(fmt, args...) printf(fmt, ##args)
80239922Sgonzo#else
81239922Sgonzo#define dprintf(fmt, args...)
82239922Sgonzo#endif
83239922Sgonzo
84239922Sgonzostruct bcm_mbox_softc {
85239922Sgonzo	struct mtx		lock;
86239922Sgonzo	struct resource *	mem_res;
87239922Sgonzo	struct resource *	irq_res;
88239922Sgonzo	void*			intr_hl;
89239922Sgonzo	bus_space_tag_t		bst;
90239922Sgonzo	bus_space_handle_t	bsh;
91239922Sgonzo	int			valid[BCM2835_MBOX_CHANS];
92239922Sgonzo	int			msg[BCM2835_MBOX_CHANS];
93239922Sgonzo};
94239922Sgonzo
95253006Srpaulo#define	mbox_read_4(sc, reg)		\
96253006Srpaulo    bus_space_read_4((sc)->bst, (sc)->bsh, reg)
97253006Srpaulo#define	mbox_write_4(sc, reg, val)		\
98253006Srpaulo    bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
99239922Sgonzo
100239922Sgonzostatic void
101239922Sgonzobcm_mbox_intr(void *arg)
102239922Sgonzo{
103239922Sgonzo	struct bcm_mbox_softc *sc = arg;
104239922Sgonzo	int chan;
105239922Sgonzo	uint32_t data;
106239922Sgonzo	uint32_t msg;
107239922Sgonzo
108253006Srpaulo	MBOX_LOCK(sc);
109253006Srpaulo	while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) {
110253006Srpaulo		msg = mbox_read_4(sc, REG_READ);
111239922Sgonzo		dprintf("bcm_mbox_intr: raw data %08x\n", msg);
112239922Sgonzo		chan = MBOX_CHAN(msg);
113239922Sgonzo		data = MBOX_DATA(msg);
114239922Sgonzo		if (sc->valid[chan]) {
115239922Sgonzo			printf("bcm_mbox_intr: channel %d oveflow\n", chan);
116239922Sgonzo			continue;
117239922Sgonzo		}
118239922Sgonzo		dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
119239922Sgonzo		sc->msg[chan] = data;
120239922Sgonzo		sc->valid[chan] = 1;
121239922Sgonzo		wakeup(&sc->msg[chan]);
122239922Sgonzo
123239922Sgonzo	}
124253006Srpaulo	MBOX_UNLOCK(sc);
125239922Sgonzo}
126239922Sgonzo
127239922Sgonzostatic int
128239922Sgonzobcm_mbox_probe(device_t dev)
129239922Sgonzo{
130239922Sgonzo
131266152Sian	if (!ofw_bus_status_okay(dev))
132266152Sian		return (ENXIO);
133266152Sian
134239922Sgonzo	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
135239922Sgonzo		device_set_desc(dev, "BCM2835 VideoCore Mailbox");
136239922Sgonzo		return(BUS_PROBE_DEFAULT);
137239922Sgonzo	}
138239922Sgonzo
139239922Sgonzo	return (ENXIO);
140239922Sgonzo}
141239922Sgonzo
142239922Sgonzostatic int
143239922Sgonzobcm_mbox_attach(device_t dev)
144239922Sgonzo{
145239922Sgonzo	struct bcm_mbox_softc *sc = device_get_softc(dev);
146239922Sgonzo	int i;
147239922Sgonzo	int rid = 0;
148239922Sgonzo
149239922Sgonzo	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
150239922Sgonzo	if (sc->mem_res == NULL) {
151239922Sgonzo		device_printf(dev, "could not allocate memory resource\n");
152239922Sgonzo		return (ENXIO);
153239922Sgonzo	}
154239922Sgonzo
155239922Sgonzo	sc->bst = rman_get_bustag(sc->mem_res);
156239922Sgonzo	sc->bsh = rman_get_bushandle(sc->mem_res);
157239922Sgonzo
158239922Sgonzo	rid = 0;
159239922Sgonzo	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
160239922Sgonzo	if (sc->irq_res == NULL) {
161239922Sgonzo		device_printf(dev, "could not allocate interrupt resource\n");
162239922Sgonzo		return (ENXIO);
163239922Sgonzo	}
164239922Sgonzo
165239922Sgonzo	/* Setup and enable the timer */
166252450Srpaulo	if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
167252450Srpaulo	    NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
168252450Srpaulo		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
169239922Sgonzo		device_printf(dev, "Unable to setup the clock irq handler.\n");
170239922Sgonzo		return (ENXIO);
171239922Sgonzo	}
172239922Sgonzo
173262586Sbrueffer	mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
174239922Sgonzo	for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
175239922Sgonzo		sc->valid[0] = 0;
176239922Sgonzo		sc->msg[0] = 0;
177239922Sgonzo	}
178239922Sgonzo
179239922Sgonzo	/* Read all pending messages */
180239922Sgonzo	bcm_mbox_intr(sc);
181239922Sgonzo
182253006Srpaulo	mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
183239922Sgonzo
184239922Sgonzo	return (0);
185239922Sgonzo}
186239922Sgonzo
187239922Sgonzo/*
188239922Sgonzo * Mailbox API
189239922Sgonzo */
190253006Srpaulostatic int
191253006Srpaulobcm_mbox_write(device_t dev, int chan, uint32_t data)
192239922Sgonzo{
193239922Sgonzo	int limit = 20000;
194253006Srpaulo	struct bcm_mbox_softc *sc = device_get_softc(dev);
195239922Sgonzo
196239922Sgonzo	dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
197253006Srpaulo	MBOX_LOCK(sc);
198239922Sgonzo
199253006Srpaulo	while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && limit--) {
200239922Sgonzo		DELAY(2);
201239922Sgonzo	}
202239922Sgonzo
203239922Sgonzo	if (limit == 0) {
204239922Sgonzo		printf("bcm_mbox_write: STATUS_FULL stuck");
205253006Srpaulo		MBOX_UNLOCK(sc);
206239922Sgonzo		return (EAGAIN);
207239922Sgonzo	}
208239922Sgonzo
209253006Srpaulo	mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
210239922Sgonzo
211253006Srpaulo	MBOX_UNLOCK(sc);
212239922Sgonzo	return (0);
213239922Sgonzo}
214239922Sgonzo
215253006Srpaulostatic int
216253006Srpaulobcm_mbox_read(device_t dev, int chan, uint32_t *data)
217239922Sgonzo{
218253006Srpaulo	struct bcm_mbox_softc *sc = device_get_softc(dev);
219239922Sgonzo
220239922Sgonzo	dprintf("bcm_mbox_read: chan %d\n", chan);
221253006Srpaulo	MBOX_LOCK(sc);
222239922Sgonzo	while (!sc->valid[chan])
223239922Sgonzo		msleep(&sc->msg[chan], &sc->lock, PZERO, "vcio mbox read", 0);
224253006Srpaulo	*data = sc->msg[chan];
225253006Srpaulo	sc->valid[chan] = 0;
226253006Srpaulo	MBOX_UNLOCK(sc);
227239922Sgonzo	dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
228239922Sgonzo
229239922Sgonzo	return (0);
230239922Sgonzo}
231253006Srpaulo
232253006Srpaulostatic device_method_t bcm_mbox_methods[] = {
233253006Srpaulo	DEVMETHOD(device_probe,		bcm_mbox_probe),
234253006Srpaulo	DEVMETHOD(device_attach,	bcm_mbox_attach),
235253006Srpaulo
236253006Srpaulo	DEVMETHOD(mbox_read,		bcm_mbox_read),
237253006Srpaulo	DEVMETHOD(mbox_write,		bcm_mbox_write),
238253006Srpaulo
239253006Srpaulo	DEVMETHOD_END
240253006Srpaulo};
241253006Srpaulo
242253006Srpaulostatic driver_t bcm_mbox_driver = {
243253006Srpaulo	"mbox",
244253006Srpaulo	bcm_mbox_methods,
245253006Srpaulo	sizeof(struct bcm_mbox_softc),
246253006Srpaulo};
247253006Srpaulo
248253006Srpaulostatic devclass_t bcm_mbox_devclass;
249253006Srpaulo
250253006SrpauloDRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
251253006Srpaulo
252