Path: blob/main/sys/arm/broadcom/bcm2835/bcm2835_mbox.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Oleksandr Tymoshenko <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>31#include <sys/kernel.h>32#include <sys/lock.h>33#include <sys/module.h>34#include <sys/mutex.h>35#include <sys/sx.h>36#include <sys/rman.h>37#include <machine/bus.h>3839#include <dev/ofw/ofw_bus.h>40#include <dev/ofw/ofw_bus_subr.h>4142#include <arm/broadcom/bcm2835/bcm2835_firmware.h>43#include <arm/broadcom/bcm2835/bcm2835_mbox.h>44#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>45#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>4647#include "mbox_if.h"4849#define REG_READ 0x0050#define REG_POL 0x1051#define REG_SENDER 0x1452#define REG_STATUS 0x1853#define STATUS_FULL 0x8000000054#define STATUS_EMPTY 0x4000000055#define REG_CONFIG 0x1C56#define CONFIG_DATA_IRQ 0x0000000157#define REG_WRITE 0x20 /* This is Mailbox 1 address */5859#define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf))60#define MBOX_CHAN(msg) ((msg) & 0xf)61#define MBOX_DATA(msg) ((msg) & ~0xf)6263#define MBOX_LOCK(sc) do { \64mtx_lock(&(sc)->lock); \65} while(0)6667#define MBOX_UNLOCK(sc) do { \68mtx_unlock(&(sc)->lock); \69} while(0)7071#ifdef DEBUG72#define dprintf(fmt, args...) printf(fmt, ##args)73#else74#define dprintf(fmt, args...)75#endif7677struct bcm_mbox_softc {78struct mtx lock;79struct resource * mem_res;80struct resource * irq_res;81void* intr_hl;82bus_space_tag_t bst;83bus_space_handle_t bsh;84int msg[BCM2835_MBOX_CHANS];85int have_message[BCM2835_MBOX_CHANS];86struct sx property_chan_lock;87};8889#define mbox_read_4(sc, reg) \90bus_space_read_4((sc)->bst, (sc)->bsh, reg)91#define mbox_write_4(sc, reg, val) \92bus_space_write_4((sc)->bst, (sc)->bsh, reg, val)9394static struct ofw_compat_data compat_data[] = {95{"broadcom,bcm2835-mbox", 1},96{"brcm,bcm2835-mbox", 1},97{NULL, 0}98};99100static int101bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan)102{103#ifdef DEBUG104uint32_t data;105#endif106uint32_t msg;107int chan;108109msg = mbox_read_4(sc, REG_READ);110dprintf("bcm_mbox_intr: raw data %08x\n", msg);111chan = MBOX_CHAN(msg);112#ifdef DEBUG113data = MBOX_DATA(msg);114#endif115if (sc->msg[chan]) {116printf("bcm_mbox_intr: channel %d oveflow\n", chan);117return (1);118}119dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data);120sc->msg[chan] = msg;121122if (ochan != NULL)123*ochan = chan;124125return (0);126}127128static void129bcm_mbox_intr(void *arg)130{131struct bcm_mbox_softc *sc = arg;132int chan;133134MBOX_LOCK(sc);135while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))136if (bcm_mbox_read_msg(sc, &chan) == 0) {137sc->have_message[chan] = 1;138wakeup(&sc->have_message[chan]);139}140MBOX_UNLOCK(sc);141}142143static int144bcm_mbox_probe(device_t dev)145{146147if (!ofw_bus_status_okay(dev))148return (ENXIO);149150if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)151return (ENXIO);152153device_set_desc(dev, "BCM2835 VideoCore Mailbox");154155return (BUS_PROBE_DEFAULT);156}157158static int159bcm_mbox_attach(device_t dev)160{161struct bcm_mbox_softc *sc = device_get_softc(dev);162int i;163int rid = 0;164165sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);166if (sc->mem_res == NULL) {167device_printf(dev, "could not allocate memory resource\n");168return (ENXIO);169}170171sc->bst = rman_get_bustag(sc->mem_res);172sc->bsh = rman_get_bushandle(sc->mem_res);173174rid = 0;175sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);176if (sc->irq_res == NULL) {177device_printf(dev, "could not allocate interrupt resource\n");178return (ENXIO);179}180181/* Setup and enable the timer */182if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC,183NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) {184bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res);185device_printf(dev, "Unable to setup the clock irq handler.\n");186return (ENXIO);187}188189mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF);190for (i = 0; i < BCM2835_MBOX_CHANS; i++) {191sc->msg[i] = 0;192sc->have_message[i] = 0;193}194195sx_init(&sc->property_chan_lock, "mboxprop");196197/* Read all pending messages */198while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0)199(void)mbox_read_4(sc, REG_READ);200201mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ);202203return (0);204}205206/*207* Mailbox API208*/209static int210bcm_mbox_write(device_t dev, int chan, uint32_t data)211{212int limit = 1000;213struct bcm_mbox_softc *sc = device_get_softc(dev);214215dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data);216MBOX_LOCK(sc);217sc->have_message[chan] = 0;218while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit)219DELAY(5);220if (limit == 0) {221printf("bcm_mbox_write: STATUS_FULL stuck");222MBOX_UNLOCK(sc);223return (EAGAIN);224}225mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data));226MBOX_UNLOCK(sc);227228return (0);229}230231static int232bcm_mbox_read(device_t dev, int chan, uint32_t *data)233{234struct bcm_mbox_softc *sc = device_get_softc(dev);235int err, read_chan;236237dprintf("bcm_mbox_read: chan %d\n", chan);238239err = 0;240MBOX_LOCK(sc);241if (!cold) {242if (sc->have_message[chan] == 0) {243if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0,244"mbox", 10*hz) != 0) {245device_printf(dev, "timeout waiting for message on chan %d\n", chan);246err = ETIMEDOUT;247}248}249} else {250do {251/* Wait for a message */252while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY))253;254/* Read the message */255if (bcm_mbox_read_msg(sc, &read_chan) != 0) {256err = EINVAL;257goto out;258}259} while (read_chan != chan);260}261/*262* get data from intr handler, the same channel is never coming263* because of holding sc lock.264*/265*data = MBOX_DATA(sc->msg[chan]);266sc->msg[chan] = 0;267sc->have_message[chan] = 0;268out:269MBOX_UNLOCK(sc);270dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data);271272return (err);273}274275static device_method_t bcm_mbox_methods[] = {276DEVMETHOD(device_probe, bcm_mbox_probe),277DEVMETHOD(device_attach, bcm_mbox_attach),278279DEVMETHOD(mbox_read, bcm_mbox_read),280DEVMETHOD(mbox_write, bcm_mbox_write),281282DEVMETHOD_END283};284285static driver_t bcm_mbox_driver = {286"mbox",287bcm_mbox_methods,288sizeof(struct bcm_mbox_softc),289};290291EARLY_DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, 0, 0,292BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);293294static void295bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)296{297bus_addr_t *addr;298299if (err)300return;301addr = (bus_addr_t *)arg;302*addr = ARMC_TO_VCBUS(segs[0].ds_addr);303}304305static void *306bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag,307bus_dmamap_t *map, bus_addr_t *phys)308{309void *buf;310int err;311312err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0,313bcm283x_dmabus_peripheral_lowaddr(), BUS_SPACE_MAXADDR, NULL, NULL,314len, 1, len, 0, NULL, NULL, tag);315if (err != 0) {316device_printf(dev, "can't create DMA tag\n");317return (NULL);318}319320err = bus_dmamem_alloc(*tag, &buf, 0, map);321if (err != 0) {322bus_dma_tag_destroy(*tag);323device_printf(dev, "can't allocate dmamem\n");324return (NULL);325}326327err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb,328phys, 0);329if (err != 0) {330bus_dmamem_free(*tag, buf, *map);331bus_dma_tag_destroy(*tag);332device_printf(dev, "can't load DMA map\n");333return (NULL);334}335336return (buf);337}338339static int340bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys,341struct bcm2835_mbox_hdr *msg, size_t len)342{343int idx;344struct bcm2835_mbox_tag_hdr *tag;345uint8_t *last;346347if ((uint32_t)msg_phys != resp_phys) {348device_printf(dev, "response channel mismatch\n");349return (EIO);350}351if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) {352device_printf(dev, "mbox response error\n");353return (EIO);354}355356/* Loop until the end tag. */357tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1);358last = (uint8_t *)msg + len;359for (idx = 0; tag->tag != 0; idx++) {360/*361* When setting the GPIO config or state the firmware doesn't362* set tag->val_len correctly.363*/364if ((tag->tag == BCM2835_FIRMWARE_TAG_SET_GPIO_CONFIG ||365tag->tag == BCM2835_FIRMWARE_TAG_SET_GPIO_STATE) &&366tag->val_len == 0) {367tag->val_len = BCM2835_MBOX_TAG_VAL_LEN_RESPONSE |368tag->val_buf_size;369}370if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) {371device_printf(dev, "tag %d response error\n", idx);372return (EIO);373}374/* Clear the response bit. */375tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;376377/* Next tag. */378tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag +379sizeof(*tag) + tag->val_buf_size);380381if ((uint8_t *)tag > last) {382device_printf(dev, "mbox buffer size error\n");383return (EIO);384}385}386387return (0);388}389390int391bcm2835_mbox_property(void *msg, size_t msg_size)392{393struct bcm_mbox_softc *sc;394bus_dma_tag_t msg_tag;395bus_dmamap_t msg_map;396bus_addr_t msg_phys;397char *buf;398uint32_t reg;399device_t mbox;400int err;401402/* get mbox device */403mbox = devclass_get_device(devclass_find("mbox"), 0);404if (mbox == NULL)405return (ENXIO);406407sc = device_get_softc(mbox);408sx_xlock(&sc->property_chan_lock);409410/* Allocate memory for the message */411buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map,412&msg_phys);413if (buf == NULL) {414err = ENOMEM;415goto out;416}417418memcpy(buf, msg, msg_size);419420bus_dmamap_sync(msg_tag, msg_map,421BUS_DMASYNC_PREWRITE);422423MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys);424MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®);425426bus_dmamap_sync(msg_tag, msg_map,427BUS_DMASYNC_PREREAD);428429memcpy(msg, buf, msg_size);430431err = bcm2835_mbox_err(mbox, msg_phys, reg,432(struct bcm2835_mbox_hdr *)msg, msg_size);433434bus_dmamap_unload(msg_tag, msg_map);435bus_dmamem_free(msg_tag, buf, msg_map);436bus_dma_tag_destroy(msg_tag);437out:438sx_xunlock(&sc->property_chan_lock);439return (err);440}441442int443bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on)444{445struct msg_set_power_state msg;446int err;447448memset(&msg, 0, sizeof(msg));449msg.hdr.buf_size = sizeof(msg);450msg.hdr.code = BCM2835_MBOX_CODE_REQ;451msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE;452msg.tag_hdr.val_buf_size = sizeof(msg.body);453msg.tag_hdr.val_len = sizeof(msg.body.req);454msg.body.req.device_id = device_id;455msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) |456BCM2835_MBOX_POWER_WAIT;457msg.end_tag = 0;458459err = bcm2835_mbox_property(&msg, sizeof(msg));460461return (err);462}463464int465bcm2835_mbox_notify_xhci_reset(uint32_t pci_dev_addr)466{467struct msg_notify_xhci_reset msg;468int err;469470memset(&msg, 0, sizeof(msg));471msg.hdr.buf_size = sizeof(msg);472msg.hdr.code = BCM2835_MBOX_CODE_REQ;473msg.tag_hdr.tag = BCM2835_MBOX_TAG_NOTIFY_XHCI_RESET;474msg.tag_hdr.val_buf_size = sizeof(msg.body);475msg.tag_hdr.val_len = sizeof(msg.body.req);476msg.body.req.pci_device_addr = pci_dev_addr;477msg.end_tag = 0;478479err = bcm2835_mbox_property(&msg, sizeof(msg));480481return (err);482}483484int485bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz)486{487struct msg_get_clock_rate msg;488int err;489490memset(&msg, 0, sizeof(msg));491msg.hdr.buf_size = sizeof(msg);492msg.hdr.code = BCM2835_MBOX_CODE_REQ;493msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE;494msg.tag_hdr.val_buf_size = sizeof(msg.body);495msg.tag_hdr.val_len = sizeof(msg.body.req);496msg.body.req.clock_id = clock_id;497msg.end_tag = 0;498499err = bcm2835_mbox_property(&msg, sizeof(msg));500*hz = msg.body.resp.rate_hz;501502return (err);503}504505int506bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb)507{508int err;509struct msg_fb_get_w_h msg;510511memset(&msg, 0, sizeof(msg));512msg.hdr.buf_size = sizeof(msg);513msg.hdr.code = BCM2835_MBOX_CODE_REQ;514BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H);515msg.physical_w_h.tag_hdr.val_len = 0;516msg.end_tag = 0;517518err = bcm2835_mbox_property(&msg, sizeof(msg));519if (err == 0) {520fb->xres = msg.physical_w_h.body.resp.width;521fb->yres = msg.physical_w_h.body.resp.height;522}523524return (err);525}526527int528bcm2835_mbox_fb_get_bpp(struct bcm2835_fb_config *fb)529{530int err;531struct msg_fb_get_bpp msg;532533memset(&msg, 0, sizeof(msg));534msg.hdr.buf_size = sizeof(msg);535msg.hdr.code = BCM2835_MBOX_CODE_REQ;536BCM2835_MBOX_INIT_TAG(&msg.bpp, GET_DEPTH);537msg.bpp.tag_hdr.val_len = 0;538msg.end_tag = 0;539540err = bcm2835_mbox_property(&msg, sizeof(msg));541if (err == 0)542fb->bpp = msg.bpp.body.resp.bpp;543544return (err);545}546547int548bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb)549{550int err;551struct msg_fb_setup msg;552553memset(&msg, 0, sizeof(msg));554msg.hdr.buf_size = sizeof(msg);555msg.hdr.code = BCM2835_MBOX_CODE_REQ;556BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H);557msg.physical_w_h.body.req.width = fb->xres;558msg.physical_w_h.body.req.height = fb->yres;559BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H);560msg.virtual_w_h.body.req.width = fb->vxres;561msg.virtual_w_h.body.req.height = fb->vyres;562BCM2835_MBOX_INIT_TAG(&msg.offset, SET_VIRTUAL_OFFSET);563msg.offset.body.req.x = fb->xoffset;564msg.offset.body.req.y = fb->yoffset;565BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH);566msg.depth.body.req.bpp = fb->bpp;567BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE);568msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED;569BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER);570msg.buffer.body.req.alignment = PAGE_SIZE;571BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH);572msg.end_tag = 0;573574err = bcm2835_mbox_property(&msg, sizeof(msg));575if (err == 0) {576fb->xres = msg.physical_w_h.body.resp.width;577fb->yres = msg.physical_w_h.body.resp.height;578fb->vxres = msg.virtual_w_h.body.resp.width;579fb->vyres = msg.virtual_w_h.body.resp.height;580fb->xoffset = msg.offset.body.resp.x;581fb->yoffset = msg.offset.body.resp.y;582fb->pitch = msg.pitch.body.resp.pitch;583fb->base = VCBUS_TO_ARMC(msg.buffer.body.resp.fb_address);584fb->size = msg.buffer.body.resp.fb_size;585}586587return (err);588}589590591