2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
39 #include <machine/bus.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
45 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
46 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
52 #define REG_SENDER 0x14
53 #define REG_STATUS 0x18
54 #define STATUS_FULL 0x80000000
55 #define STATUS_EMPTY 0x40000000
56 #define REG_CONFIG 0x1C
57 #define CONFIG_DATA_IRQ 0x00000001
58 #define REG_WRITE 0x20 /* This is Mailbox 1 address */
60 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf))
61 #define MBOX_CHAN(msg) ((msg) & 0xf)
62 #define MBOX_DATA(msg) ((msg) & ~0xf)
64 #define MBOX_LOCK(sc) do { \
65 mtx_lock(&(sc)->lock); \
68 #define MBOX_UNLOCK(sc) do { \
69 mtx_unlock(&(sc)->lock); \
73 #define dprintf(fmt, args...) printf(fmt, ##args)
75 #define dprintf(fmt, args...)
78 struct bcm_mbox_softc {
80 struct resource * mem_res;
81 struct resource * irq_res;
84 bus_space_handle_t bsh;
85 int msg[BCM2835_MBOX_CHANS];
86 struct sema sema[BCM2835_MBOX_CHANS];
89 #define mbox_read_4(sc, reg) \
90 bus_space_read_4((sc)->bst, (sc)->bsh, reg)
91 #define mbox_write_4(sc, reg, val) \
92 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
95 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan)
101 msg = mbox_read_4(sc, REG_READ);
102 dprintf("bcm_mbox_intr: raw data %08x\n", msg);
103 chan = MBOX_CHAN(msg);
104 data = MBOX_DATA(msg);
106 printf("bcm_mbox_intr: channel %d oveflow\n", chan);
109 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
119 bcm_mbox_intr(void *arg)
121 struct bcm_mbox_softc *sc = arg;
124 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
125 if (bcm_mbox_read_msg(sc, &chan) == 0)
126 sema_post(&sc->sema[chan]);
130 bcm_mbox_probe(device_t dev)
133 if (!ofw_bus_status_okay(dev))
136 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
137 device_set_desc(dev, "BCM2835 VideoCore Mailbox");
138 return(BUS_PROBE_DEFAULT);
145 bcm_mbox_attach(device_t dev)
147 struct bcm_mbox_softc *sc = device_get_softc(dev);
151 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
152 if (sc->mem_res == NULL) {
153 device_printf(dev, "could not allocate memory resource\n");
157 sc->bst = rman_get_bustag(sc->mem_res);
158 sc->bsh = rman_get_bushandle(sc->mem_res);
161 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
162 if (sc->irq_res == NULL) {
163 device_printf(dev, "could not allocate interrupt resource\n");
167 /* Setup and enable the timer */
168 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
169 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
170 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
171 device_printf(dev, "Unable to setup the clock irq handler.\n");
175 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
176 for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
178 sema_init(&sc->sema[i], 0, "mbox");
181 /* Read all pending messages */
182 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
183 (void)mbox_read_4(sc, REG_READ);
185 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
194 bcm_mbox_write(device_t dev, int chan, uint32_t data)
197 struct bcm_mbox_softc *sc = device_get_softc(dev);
199 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
201 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)
204 printf("bcm_mbox_write: STATUS_FULL stuck");
208 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
215 bcm_mbox_read(device_t dev, int chan, uint32_t *data)
217 struct bcm_mbox_softc *sc = device_get_softc(dev);
220 dprintf("bcm_mbox_read: chan %d\n", chan);
225 while (sema_trywait(&sc->sema[chan]) == 0) {
226 /* do not unlock sc while waiting for the mbox */
227 if (sema_timedwait(&sc->sema[chan], 10*hz) == 0)
229 printf("timeout sema for chan %d\n", chan);
233 /* Wait for a message */
234 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
236 /* Read the message */
237 if (bcm_mbox_read_msg(sc, &read_chan) != 0) {
241 } while (read_chan != chan);
244 * get data from intr handler, the same channel is never coming
245 * because of holding sc lock.
247 *data = MBOX_DATA(sc->msg[chan]);
251 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
256 static device_method_t bcm_mbox_methods[] = {
257 DEVMETHOD(device_probe, bcm_mbox_probe),
258 DEVMETHOD(device_attach, bcm_mbox_attach),
260 DEVMETHOD(mbox_read, bcm_mbox_read),
261 DEVMETHOD(mbox_write, bcm_mbox_write),
266 static driver_t bcm_mbox_driver = {
269 sizeof(struct bcm_mbox_softc),
272 static devclass_t bcm_mbox_devclass;
274 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
277 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
283 addr = (bus_addr_t *)arg;
284 *addr = PHYS_TO_VCBUS(segs[0].ds_addr);
288 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag,
289 bus_dmamap_t *map, bus_addr_t *phys)
294 err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,
295 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
296 len, 1, len, 0, NULL, NULL, tag);
298 device_printf(dev, "can't create DMA tag\n");
302 err = bus_dmamem_alloc(*tag, &buf, 0, map);
304 bus_dma_tag_destroy(*tag);
305 device_printf(dev, "can't allocate dmamem\n");
309 err = bus_dmamap_load(*tag, *map, buf,
310 sizeof(struct msg_set_power_state), bcm2835_mbox_dma_cb,
313 bus_dmamem_free(*tag, buf, *map);
314 bus_dma_tag_destroy(*tag);
315 device_printf(dev, "can't load DMA map\n");
323 bcm2835_mbox_set_power_state(device_t dev, uint32_t device_id, boolean_t on)
325 struct msg_set_power_state *msg;
326 bus_dma_tag_t msg_tag;
327 bus_dmamap_t msg_map;
332 /* get mbox device */
333 mbox = devclass_get_device(devclass_find("mbox"), 0);
335 device_printf(dev, "can't find mbox\n");
339 /* Allocate memory for the message */
340 msg = bcm2835_mbox_init_dma(dev, sizeof(*msg), &msg_tag, &msg_map,
345 memset(msg, 0, sizeof(*msg));
346 msg->hdr.buf_size = sizeof(*msg);
347 msg->hdr.code = BCM2835_MBOX_CODE_REQ;
348 msg->tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;
349 msg->tag_hdr.val_buf_size = sizeof(msg->body);
350 msg->tag_hdr.val_len = sizeof(msg->body.req);
351 msg->body.req.device_id = device_id;
352 msg->body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) |
353 BCM2835_MBOX_POWER_WAIT;
356 bus_dmamap_sync(msg_tag, msg_map,
357 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
359 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
360 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®);
362 bus_dmamap_unload(msg_tag, msg_map);
363 bus_dmamem_free(msg_tag, msg, msg_map);
364 bus_dma_tag_destroy(msg_tag);
370 bcm2835_mbox_get_clock_rate(device_t dev, uint32_t clock_id, uint32_t *hz)
372 struct msg_get_clock_rate *msg;
373 bus_dma_tag_t msg_tag;
374 bus_dmamap_t msg_map;
379 /* get mbox device */
380 mbox = devclass_get_device(devclass_find("mbox"), 0);
382 device_printf(dev, "can't find mbox\n");
386 /* Allocate memory for the message */
387 msg = bcm2835_mbox_init_dma(dev, sizeof(*msg), &msg_tag, &msg_map,
392 memset(msg, 0, sizeof(*msg));
393 msg->hdr.buf_size = sizeof(*msg);
394 msg->hdr.code = BCM2835_MBOX_CODE_REQ;
395 msg->tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE;
396 msg->tag_hdr.val_buf_size = sizeof(msg->body);
397 msg->tag_hdr.val_len = sizeof(msg->body.req);
398 msg->body.req.clock_id = clock_id;
401 bus_dmamap_sync(msg_tag, msg_map, BUS_DMASYNC_PREWRITE);
402 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
403 bus_dmamap_sync(msg_tag, msg_map, BUS_DMASYNC_POSTWRITE);
405 bus_dmamap_sync(msg_tag, msg_map, BUS_DMASYNC_PREREAD);
406 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®);
407 bus_dmamap_sync(msg_tag, msg_map, BUS_DMASYNC_POSTREAD);
409 *hz = msg->body.resp.rate_hz;
411 bus_dmamap_unload(msg_tag, msg_map);
412 bus_dmamem_free(msg_tag, msg, msg_map);
413 bus_dma_tag_destroy(msg_tag);