Home | History | Annotate | Line # | Download | only in broadcom
      1 /*	$NetBSD: bcm2835_mbox.c,v 1.17 2021/08/07 16:18:43 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_mbox.c,v 1.17 2021/08/07 16:18:43 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kernel.h>
     39 #include <sys/timetc.h>
     40 #include <sys/bus.h>
     41 #include <sys/mutex.h>
     42 
     43 #include <arm/broadcom/bcm2835_mbox.h>
     44 #include <arm/broadcom/bcm2835_mboxreg.h>
     45 #include <arm/broadcom/bcm2835reg.h>
     46 #include <arm/broadcom/bcm2835var.h>
     47 
     48 static struct bcm2835mbox_softc *bcm2835mbox_sc;
     49 
     50 static int
     51 bcmmbox_intr1(struct bcm2835mbox_softc *sc, int cv)
     52 {
     53 	uint32_t mbox, chan, data;
     54 	int ret = 0;
     55 
     56 	KASSERT(mutex_owned(&sc->sc_intr_lock));
     57 
     58 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, BCM2835_MBOX_SIZE,
     59 	    BUS_SPACE_BARRIER_READ);
     60 
     61 	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
     62 	    BCM2835_MBOX0_STATUS) & BCM2835_MBOX_STATUS_EMPTY) == 0) {
     63 
     64 		mbox = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
     65 		    BCM2835_MBOX0_READ);
     66 
     67 		chan = BCM2835_MBOX_CHAN(mbox);
     68 		data = BCM2835_MBOX_DATA(mbox);
     69 		ret = 1;
     70 
     71 		if (BCM2835_MBOX_CHAN(sc->sc_mbox[chan]) != 0) {
     72 			aprint_error("bcmmbox_intr: chan %d overflow\n",chan);
     73 			continue;
     74 		}
     75 
     76 		sc->sc_mbox[chan] = data | BCM2835_MBOX_CHANMASK;
     77 
     78 		if (cv)
     79 			cv_broadcast(&sc->sc_chan[chan]);
     80 	}
     81 
     82 	return ret;
     83 }
     84 
     85 void
     86 bcmmbox_attach(struct bcm2835mbox_softc *sc)
     87 {
     88 	struct bcmmbox_attach_args baa;
     89 	int i;
     90 
     91 	if (bcm2835mbox_sc == NULL)
     92 		bcm2835mbox_sc = sc;
     93 
     94 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
     95 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
     96 	for (i = 0; i < BCM2835_MBOX_NUMCHANNELS; ++i)
     97 		cv_init(&sc->sc_chan[i], "bcmmbox");
     98 
     99 	/* enable mbox interrupt */
    100 	if (sc->sc_intrh != NULL)
    101 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_MBOX_CFG,
    102 		    BCM2835_MBOX_CFG_DATAIRQEN);
    103 
    104 	baa.baa_dmat = sc->sc_dmat;
    105 	sc->sc_platdev = config_found(sc->sc_dev, &baa, NULL, CFARGS_NONE);
    106 }
    107 
    108 int
    109 bcmmbox_intr(void *cookie)
    110 {
    111 	struct bcm2835mbox_softc *sc = cookie;
    112 	int ret;
    113 
    114 	mutex_enter(&sc->sc_intr_lock);
    115 
    116 	ret = bcmmbox_intr1(sc, 1);
    117 
    118 	mutex_exit(&sc->sc_intr_lock);
    119 
    120 	return ret;
    121 }
    122 
    123 void
    124 bcmmbox_read(uint8_t chan, uint32_t *data)
    125 {
    126 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    127 
    128 	KASSERT(sc != NULL);
    129 
    130 	mutex_enter(&sc->sc_lock);
    131 
    132 	mutex_enter(&sc->sc_intr_lock);
    133 	while (BCM2835_MBOX_CHAN(sc->sc_mbox[chan]) == 0) {
    134 		if (cold || sc->sc_intrh == NULL)
    135 			bcmmbox_intr1(sc, 0);
    136 		else
    137 			cv_wait(&sc->sc_chan[chan], &sc->sc_intr_lock);
    138 	}
    139 	*data = BCM2835_MBOX_DATA(sc->sc_mbox[chan]);
    140 	sc->sc_mbox[chan] = 0;
    141 	mutex_exit(&sc->sc_intr_lock);
    142 
    143 	mutex_exit(&sc->sc_lock);
    144 }
    145 
    146 void
    147 bcmmbox_write(uint8_t chan, uint32_t data)
    148 {
    149 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    150 
    151 	KASSERT(sc != NULL);
    152 	KASSERT(BCM2835_MBOX_CHAN(chan) == chan);
    153 	KASSERT(BCM2835_MBOX_CHAN(data) == 0);
    154 
    155 	mutex_enter(&sc->sc_lock);
    156 
    157 	bcm2835_mbox_write(sc->sc_iot, sc->sc_ioh, chan, data);
    158 
    159 	mutex_exit(&sc->sc_lock);
    160 }
    161 
    162 int
    163 bcmmbox_request(uint8_t chan, void *buf, size_t buflen, uint32_t *pres)
    164 {
    165 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    166 	void *dma_buf;
    167 	bus_dmamap_t map;
    168 	bus_dma_segment_t segs[1];
    169 	int nsegs;
    170 	int error;
    171 
    172 	KASSERT(sc != NULL);
    173 
    174 	error = bus_dmamem_alloc(sc->sc_dmat, buflen, 16, 0, segs, 1,
    175 	    &nsegs, BUS_DMA_WAITOK);
    176 	if (error)
    177 		return error;
    178 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, buflen, &dma_buf,
    179 	    BUS_DMA_WAITOK);
    180 	if (error)
    181 		goto map_failed;
    182 	error = bus_dmamap_create(sc->sc_dmat, buflen, 1, buflen, 0,
    183 	    BUS_DMA_WAITOK, &map);
    184 	if (error)
    185 		goto create_failed;
    186 	error = bus_dmamap_load(sc->sc_dmat, map, dma_buf, buflen, NULL,
    187 	    BUS_DMA_WAITOK);
    188 	if (error)
    189 		goto load_failed;
    190 
    191 	memcpy(dma_buf, buf, buflen);
    192 
    193 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
    194 	     BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    195 	bcmmbox_write(chan, map->dm_segs[0].ds_addr);
    196 	bcmmbox_read(chan, pres);
    197 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
    198 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
    199 
    200 	memcpy(buf, dma_buf, buflen);
    201 
    202 	bus_dmamap_unload(sc->sc_dmat, map);
    203 load_failed:
    204 	bus_dmamap_destroy(sc->sc_dmat, map);
    205 create_failed:
    206 	bus_dmamem_unmap(sc->sc_dmat, dma_buf, buflen);
    207 map_failed:
    208 	bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    209 
    210 	return error;
    211 }
    212