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 int have_message[BCM2835_MBOX_CHANS];
87 struct sx property_chan_lock;
90 #define mbox_read_4(sc, reg) \
91 bus_space_read_4((sc)->bst, (sc)->bsh, reg)
92 #define mbox_write_4(sc, reg, val) \
93 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
96 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan)
102 msg = mbox_read_4(sc, REG_READ);
103 dprintf("bcm_mbox_intr: raw data %08x\n", msg);
104 chan = MBOX_CHAN(msg);
105 data = MBOX_DATA(msg);
107 printf("bcm_mbox_intr: channel %d oveflow\n", chan);
110 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
120 bcm_mbox_intr(void *arg)
122 struct bcm_mbox_softc *sc = arg;
126 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
127 if (bcm_mbox_read_msg(sc, &chan) == 0) {
128 sc->have_message[chan] = 1;
129 wakeup(&sc->have_message[chan]);
135 bcm_mbox_probe(device_t dev)
138 if (!ofw_bus_status_okay(dev))
141 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) {
142 device_set_desc(dev, "BCM2835 VideoCore Mailbox");
143 return(BUS_PROBE_DEFAULT);
150 bcm_mbox_attach(device_t dev)
152 struct bcm_mbox_softc *sc = device_get_softc(dev);
156 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
157 if (sc->mem_res == NULL) {
158 device_printf(dev, "could not allocate memory resource\n");
162 sc->bst = rman_get_bustag(sc->mem_res);
163 sc->bsh = rman_get_bushandle(sc->mem_res);
166 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
167 if (sc->irq_res == NULL) {
168 device_printf(dev, "could not allocate interrupt resource\n");
172 /* Setup and enable the timer */
173 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
174 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
175 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
176 device_printf(dev, "Unable to setup the clock irq handler.\n");
180 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
181 for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
183 sc->have_message[i] = 0;
186 sx_init(&sc->property_chan_lock, "mboxprop");
188 /* Read all pending messages */
189 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
190 (void)mbox_read_4(sc, REG_READ);
192 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
201 bcm_mbox_write(device_t dev, int chan, uint32_t data)
204 struct bcm_mbox_softc *sc = device_get_softc(dev);
206 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
208 sc->have_message[chan] = 0;
209 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)
212 printf("bcm_mbox_write: STATUS_FULL stuck");
216 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
223 bcm_mbox_read(device_t dev, int chan, uint32_t *data)
225 struct bcm_mbox_softc *sc = device_get_softc(dev);
228 dprintf("bcm_mbox_read: chan %d\n", chan);
233 if (sc->have_message[chan] == 0) {
234 if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0,
235 "mbox", 10*hz) != 0) {
236 device_printf(dev, "timeout waiting for message on chan %d\n", chan);
242 /* Wait for a message */
243 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
245 /* Read the message */
246 if (bcm_mbox_read_msg(sc, &read_chan) != 0) {
250 } while (read_chan != chan);
253 * get data from intr handler, the same channel is never coming
254 * because of holding sc lock.
256 *data = MBOX_DATA(sc->msg[chan]);
258 sc->have_message[chan] = 0;
261 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
266 static device_method_t bcm_mbox_methods[] = {
267 DEVMETHOD(device_probe, bcm_mbox_probe),
268 DEVMETHOD(device_attach, bcm_mbox_attach),
270 DEVMETHOD(mbox_read, bcm_mbox_read),
271 DEVMETHOD(mbox_write, bcm_mbox_write),
276 static driver_t bcm_mbox_driver = {
279 sizeof(struct bcm_mbox_softc),
282 static devclass_t bcm_mbox_devclass;
284 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
287 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
293 addr = (bus_addr_t *)arg;
294 *addr = PHYS_TO_VCBUS(segs[0].ds_addr);
298 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag,
299 bus_dmamap_t *map, bus_addr_t *phys)
304 err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,
305 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
306 len, 1, len, 0, NULL, NULL, tag);
308 device_printf(dev, "can't create DMA tag\n");
312 err = bus_dmamem_alloc(*tag, &buf, 0, map);
314 bus_dma_tag_destroy(*tag);
315 device_printf(dev, "can't allocate dmamem\n");
319 err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb,
322 bus_dmamem_free(*tag, buf, *map);
323 bus_dma_tag_destroy(*tag);
324 device_printf(dev, "can't load DMA map\n");
332 bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys,
333 struct bcm2835_mbox_hdr *msg, size_t len)
336 struct bcm2835_mbox_tag_hdr *tag;
339 if ((uint32_t)msg_phys != resp_phys) {
340 device_printf(dev, "response channel mismatch\n");
343 if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) {
344 device_printf(dev, "mbox response error\n");
348 /* Loop until the end tag. */
349 tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1);
350 last = (uint8_t *)msg + len;
351 for (idx = 0; tag->tag != 0; idx++) {
352 if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) {
353 device_printf(dev, "tag %d response error\n", idx);
356 /* Clear the response bit. */
357 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
360 tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag +
361 sizeof(*tag) + tag->val_buf_size);
363 if ((uint8_t *)tag > last) {
364 device_printf(dev, "mbox buffer size error\n");
373 bcm2835_mbox_property(void *msg, size_t msg_size)
375 struct bcm_mbox_softc *sc;
376 struct msg_set_power_state *buf;
377 bus_dma_tag_t msg_tag;
378 bus_dmamap_t msg_map;
384 /* get mbox device */
385 mbox = devclass_get_device(devclass_find("mbox"), 0);
389 sc = device_get_softc(mbox);
390 sx_xlock(&sc->property_chan_lock);
392 /* Allocate memory for the message */
393 buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map,
400 memcpy(buf, msg, msg_size);
402 bus_dmamap_sync(msg_tag, msg_map,
403 BUS_DMASYNC_PREWRITE);
405 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
406 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®);
408 bus_dmamap_sync(msg_tag, msg_map,
409 BUS_DMASYNC_PREREAD);
411 memcpy(msg, buf, msg_size);
413 err = bcm2835_mbox_err(mbox, msg_phys, reg,
414 (struct bcm2835_mbox_hdr *)msg, msg_size);
416 bus_dmamap_unload(msg_tag, msg_map);
417 bus_dmamem_free(msg_tag, buf, msg_map);
418 bus_dma_tag_destroy(msg_tag);
420 sx_xunlock(&sc->property_chan_lock);
425 bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on)
427 struct msg_set_power_state msg;
430 memset(&msg, 0, sizeof(msg));
431 msg.hdr.buf_size = sizeof(msg);
432 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
433 msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;
434 msg.tag_hdr.val_buf_size = sizeof(msg.body);
435 msg.tag_hdr.val_len = sizeof(msg.body.req);
436 msg.body.req.device_id = device_id;
437 msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) |
438 BCM2835_MBOX_POWER_WAIT;
441 err = bcm2835_mbox_property(&msg, sizeof(msg));
447 bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz)
449 struct msg_get_clock_rate msg;
452 memset(&msg, 0, sizeof(msg));
453 msg.hdr.buf_size = sizeof(msg);
454 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
455 msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE;
456 msg.tag_hdr.val_buf_size = sizeof(msg.body);
457 msg.tag_hdr.val_len = sizeof(msg.body.req);
458 msg.body.req.clock_id = clock_id;
461 err = bcm2835_mbox_property(&msg, sizeof(msg));
462 *hz = msg.body.resp.rate_hz;
468 bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb)
471 struct msg_fb_get_w_h msg;
473 memset(&msg, 0, sizeof(msg));
474 msg.hdr.buf_size = sizeof(msg);
475 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
476 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H);
477 msg.physical_w_h.tag_hdr.val_len = 0;
478 BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, GET_VIRTUAL_W_H);
479 msg.virtual_w_h.tag_hdr.val_len = 0;
480 BCM2835_MBOX_INIT_TAG(&msg.offset, GET_VIRTUAL_OFFSET);
481 msg.offset.tag_hdr.val_len = 0;
484 err = bcm2835_mbox_property(&msg, sizeof(msg));
486 fb->xres = msg.physical_w_h.body.resp.width;
487 fb->yres = msg.physical_w_h.body.resp.height;
488 fb->vxres = msg.virtual_w_h.body.resp.width;
489 fb->vyres = msg.virtual_w_h.body.resp.height;
490 fb->xoffset = msg.offset.body.resp.x;
491 fb->yoffset = msg.offset.body.resp.y;
498 bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb)
501 struct msg_fb_setup msg;
503 memset(&msg, 0, sizeof(msg));
504 msg.hdr.buf_size = sizeof(msg);
505 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
506 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H);
507 msg.physical_w_h.body.req.width = fb->xres;
508 msg.physical_w_h.body.req.height = fb->yres;
509 BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H);
510 msg.virtual_w_h.body.req.width = fb->vxres;
511 msg.virtual_w_h.body.req.height = fb->vyres;
512 BCM2835_MBOX_INIT_TAG(&msg.offset, GET_VIRTUAL_OFFSET);
513 msg.offset.body.req.x = fb->xoffset;
514 msg.offset.body.req.y = fb->yoffset;
515 BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH);
516 msg.depth.body.req.bpp = fb->bpp;
517 BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE);
518 msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED;
519 BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER);
520 msg.buffer.body.req.alignment = PAGE_SIZE;
521 BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH);
524 err = bcm2835_mbox_property(&msg, sizeof(msg));
526 fb->xres = msg.physical_w_h.body.resp.width;
527 fb->yres = msg.physical_w_h.body.resp.height;
528 fb->vxres = msg.virtual_w_h.body.resp.width;
529 fb->vyres = msg.virtual_w_h.body.resp.height;
530 fb->xoffset = msg.offset.body.resp.x;
531 fb->yoffset = msg.offset.body.resp.y;
532 fb->pitch = msg.pitch.body.resp.pitch;
533 fb->base = VCBUS_TO_PHYS(msg.buffer.body.resp.fb_address);
534 fb->size = msg.buffer.body.resp.fb_size;