2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
41 #include <machine/bus.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
47 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
48 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
54 #define REG_SENDER 0x14
55 #define REG_STATUS 0x18
56 #define STATUS_FULL 0x80000000
57 #define STATUS_EMPTY 0x40000000
58 #define REG_CONFIG 0x1C
59 #define CONFIG_DATA_IRQ 0x00000001
60 #define REG_WRITE 0x20 /* This is Mailbox 1 address */
62 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf))
63 #define MBOX_CHAN(msg) ((msg) & 0xf)
64 #define MBOX_DATA(msg) ((msg) & ~0xf)
66 #define MBOX_LOCK(sc) do { \
67 mtx_lock(&(sc)->lock); \
70 #define MBOX_UNLOCK(sc) do { \
71 mtx_unlock(&(sc)->lock); \
75 #define dprintf(fmt, args...) printf(fmt, ##args)
77 #define dprintf(fmt, args...)
80 struct bcm_mbox_softc {
82 struct resource * mem_res;
83 struct resource * irq_res;
86 bus_space_handle_t bsh;
87 int msg[BCM2835_MBOX_CHANS];
88 int have_message[BCM2835_MBOX_CHANS];
89 struct sx property_chan_lock;
92 #define mbox_read_4(sc, reg) \
93 bus_space_read_4((sc)->bst, (sc)->bsh, reg)
94 #define mbox_write_4(sc, reg, val) \
95 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)
97 static struct ofw_compat_data compat_data[] = {
98 {"broadcom,bcm2835-mbox", 1},
99 {"brcm,bcm2835-mbox", 1},
104 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan)
112 msg = mbox_read_4(sc, REG_READ);
113 dprintf("bcm_mbox_intr: raw data %08x\n", msg);
114 chan = MBOX_CHAN(msg);
116 data = MBOX_DATA(msg);
119 printf("bcm_mbox_intr: channel %d oveflow\n", chan);
122 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);
132 bcm_mbox_intr(void *arg)
134 struct bcm_mbox_softc *sc = arg;
138 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
139 if (bcm_mbox_read_msg(sc, &chan) == 0) {
140 sc->have_message[chan] = 1;
141 wakeup(&sc->have_message[chan]);
147 bcm_mbox_probe(device_t dev)
150 if (!ofw_bus_status_okay(dev))
153 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
156 device_set_desc(dev, "BCM2835 VideoCore Mailbox");
158 return (BUS_PROBE_DEFAULT);
162 bcm_mbox_attach(device_t dev)
164 struct bcm_mbox_softc *sc = device_get_softc(dev);
168 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
169 if (sc->mem_res == NULL) {
170 device_printf(dev, "could not allocate memory resource\n");
174 sc->bst = rman_get_bustag(sc->mem_res);
175 sc->bsh = rman_get_bushandle(sc->mem_res);
178 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
179 if (sc->irq_res == NULL) {
180 device_printf(dev, "could not allocate interrupt resource\n");
184 /* Setup and enable the timer */
185 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
186 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {
187 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);
188 device_printf(dev, "Unable to setup the clock irq handler.\n");
192 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);
193 for (i = 0; i < BCM2835_MBOX_CHANS; i++) {
195 sc->have_message[i] = 0;
198 sx_init(&sc->property_chan_lock, "mboxprop");
200 /* Read all pending messages */
201 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)
202 (void)mbox_read_4(sc, REG_READ);
204 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);
213 bcm_mbox_write(device_t dev, int chan, uint32_t data)
216 struct bcm_mbox_softc *sc = device_get_softc(dev);
218 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);
220 sc->have_message[chan] = 0;
221 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)
224 printf("bcm_mbox_write: STATUS_FULL stuck");
228 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));
235 bcm_mbox_read(device_t dev, int chan, uint32_t *data)
237 struct bcm_mbox_softc *sc = device_get_softc(dev);
240 dprintf("bcm_mbox_read: chan %d\n", chan);
245 if (sc->have_message[chan] == 0) {
246 if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0,
247 "mbox", 10*hz) != 0) {
248 device_printf(dev, "timeout waiting for message on chan %d\n", chan);
254 /* Wait for a message */
255 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))
257 /* Read the message */
258 if (bcm_mbox_read_msg(sc, &read_chan) != 0) {
262 } while (read_chan != chan);
265 * get data from intr handler, the same channel is never coming
266 * because of holding sc lock.
268 *data = MBOX_DATA(sc->msg[chan]);
270 sc->have_message[chan] = 0;
273 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);
278 static device_method_t bcm_mbox_methods[] = {
279 DEVMETHOD(device_probe, bcm_mbox_probe),
280 DEVMETHOD(device_attach, bcm_mbox_attach),
282 DEVMETHOD(mbox_read, bcm_mbox_read),
283 DEVMETHOD(mbox_write, bcm_mbox_write),
288 static driver_t bcm_mbox_driver = {
291 sizeof(struct bcm_mbox_softc),
294 static devclass_t bcm_mbox_devclass;
296 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0);
299 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
305 addr = (bus_addr_t *)arg;
306 *addr = ARMC_TO_VCBUS(segs[0].ds_addr);
310 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag,
311 bus_dmamap_t *map, bus_addr_t *phys)
316 err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,
317 bcm283x_dmabus_peripheral_lowaddr(), BUS_SPACE_MAXADDR, NULL, NULL,
318 len, 1, len, 0, NULL, NULL, tag);
320 device_printf(dev, "can't create DMA tag\n");
324 err = bus_dmamem_alloc(*tag, &buf, 0, map);
326 bus_dma_tag_destroy(*tag);
327 device_printf(dev, "can't allocate dmamem\n");
331 err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb,
334 bus_dmamem_free(*tag, buf, *map);
335 bus_dma_tag_destroy(*tag);
336 device_printf(dev, "can't load DMA map\n");
344 bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys,
345 struct bcm2835_mbox_hdr *msg, size_t len)
348 struct bcm2835_mbox_tag_hdr *tag;
351 if ((uint32_t)msg_phys != resp_phys) {
352 device_printf(dev, "response channel mismatch\n");
355 if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) {
356 device_printf(dev, "mbox response error\n");
360 /* Loop until the end tag. */
361 tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1);
362 last = (uint8_t *)msg + len;
363 for (idx = 0; tag->tag != 0; idx++) {
364 if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) {
365 device_printf(dev, "tag %d response error\n", idx);
368 /* Clear the response bit. */
369 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
372 tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag +
373 sizeof(*tag) + tag->val_buf_size);
375 if ((uint8_t *)tag > last) {
376 device_printf(dev, "mbox buffer size error\n");
385 bcm2835_mbox_property(void *msg, size_t msg_size)
387 struct bcm_mbox_softc *sc;
388 struct msg_set_power_state *buf;
389 bus_dma_tag_t msg_tag;
390 bus_dmamap_t msg_map;
396 /* get mbox device */
397 mbox = devclass_get_device(devclass_find("mbox"), 0);
401 sc = device_get_softc(mbox);
402 sx_xlock(&sc->property_chan_lock);
404 /* Allocate memory for the message */
405 buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map,
412 memcpy(buf, msg, msg_size);
414 bus_dmamap_sync(msg_tag, msg_map,
415 BUS_DMASYNC_PREWRITE);
417 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);
418 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®);
420 bus_dmamap_sync(msg_tag, msg_map,
421 BUS_DMASYNC_PREREAD);
423 memcpy(msg, buf, msg_size);
425 err = bcm2835_mbox_err(mbox, msg_phys, reg,
426 (struct bcm2835_mbox_hdr *)msg, msg_size);
428 bus_dmamap_unload(msg_tag, msg_map);
429 bus_dmamem_free(msg_tag, buf, msg_map);
430 bus_dma_tag_destroy(msg_tag);
432 sx_xunlock(&sc->property_chan_lock);
437 bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on)
439 struct msg_set_power_state msg;
442 memset(&msg, 0, sizeof(msg));
443 msg.hdr.buf_size = sizeof(msg);
444 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
445 msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;
446 msg.tag_hdr.val_buf_size = sizeof(msg.body);
447 msg.tag_hdr.val_len = sizeof(msg.body.req);
448 msg.body.req.device_id = device_id;
449 msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) |
450 BCM2835_MBOX_POWER_WAIT;
453 err = bcm2835_mbox_property(&msg, sizeof(msg));
459 bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz)
461 struct msg_get_clock_rate msg;
464 memset(&msg, 0, sizeof(msg));
465 msg.hdr.buf_size = sizeof(msg);
466 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
467 msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE;
468 msg.tag_hdr.val_buf_size = sizeof(msg.body);
469 msg.tag_hdr.val_len = sizeof(msg.body.req);
470 msg.body.req.clock_id = clock_id;
473 err = bcm2835_mbox_property(&msg, sizeof(msg));
474 *hz = msg.body.resp.rate_hz;
480 bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb)
483 struct msg_fb_get_w_h msg;
485 memset(&msg, 0, sizeof(msg));
486 msg.hdr.buf_size = sizeof(msg);
487 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
488 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H);
489 msg.physical_w_h.tag_hdr.val_len = 0;
492 err = bcm2835_mbox_property(&msg, sizeof(msg));
494 fb->xres = msg.physical_w_h.body.resp.width;
495 fb->yres = msg.physical_w_h.body.resp.height;
502 bcm2835_mbox_fb_get_bpp(struct bcm2835_fb_config *fb)
505 struct msg_fb_get_bpp msg;
507 memset(&msg, 0, sizeof(msg));
508 msg.hdr.buf_size = sizeof(msg);
509 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
510 BCM2835_MBOX_INIT_TAG(&msg.bpp, GET_DEPTH);
511 msg.bpp.tag_hdr.val_len = 0;
514 err = bcm2835_mbox_property(&msg, sizeof(msg));
516 fb->bpp = msg.bpp.body.resp.bpp;
522 bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb)
525 struct msg_fb_setup msg;
527 memset(&msg, 0, sizeof(msg));
528 msg.hdr.buf_size = sizeof(msg);
529 msg.hdr.code = BCM2835_MBOX_CODE_REQ;
530 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H);
531 msg.physical_w_h.body.req.width = fb->xres;
532 msg.physical_w_h.body.req.height = fb->yres;
533 BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H);
534 msg.virtual_w_h.body.req.width = fb->vxres;
535 msg.virtual_w_h.body.req.height = fb->vyres;
536 BCM2835_MBOX_INIT_TAG(&msg.offset, SET_VIRTUAL_OFFSET);
537 msg.offset.body.req.x = fb->xoffset;
538 msg.offset.body.req.y = fb->yoffset;
539 BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH);
540 msg.depth.body.req.bpp = fb->bpp;
541 BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE);
542 msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED;
543 BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER);
544 msg.buffer.body.req.alignment = PAGE_SIZE;
545 BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH);
548 err = bcm2835_mbox_property(&msg, sizeof(msg));
550 fb->xres = msg.physical_w_h.body.resp.width;
551 fb->yres = msg.physical_w_h.body.resp.height;
552 fb->vxres = msg.virtual_w_h.body.resp.width;
553 fb->vyres = msg.virtual_w_h.body.resp.height;
554 fb->xoffset = msg.offset.body.resp.x;
555 fb->yoffset = msg.offset.body.resp.y;
556 fb->pitch = msg.pitch.body.resp.pitch;
557 fb->base = VCBUS_TO_ARMC(msg.buffer.body.resp.fb_address);
558 fb->size = msg.buffer.body.resp.fb_size;