Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_mbox.c revision 1.12
      1 /*	$NetBSD: bcm2835_mbox.c,v 1.12 2017/12/10 21:38:26 skrll 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.12 2017/12/10 21:38:26 skrll 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 
     47 #include <dev/fdt/fdtvar.h>
     48 
     49 struct bcm2835mbox_softc {
     50 	device_t sc_dev;
     51 	device_t sc_platdev;
     52 
     53 	bus_space_tag_t sc_iot;
     54 	bus_space_handle_t sc_ioh;
     55 	bus_dma_tag_t sc_dmat;
     56 	void *sc_intrh;
     57 
     58 	kmutex_t sc_lock;
     59 	kmutex_t sc_intr_lock;
     60 	kcondvar_t sc_chan[BCM2835_MBOX_NUMCHANNELS];
     61 	uint32_t sc_mbox[BCM2835_MBOX_NUMCHANNELS];
     62 };
     63 
     64 static struct bcm2835mbox_softc *bcm2835mbox_sc;
     65 
     66 static int bcmmbox_match(device_t, cfdata_t, void *);
     67 static void bcmmbox_attach(device_t, device_t, void *);
     68 static int bcmmbox_intr1(struct bcm2835mbox_softc *, int);
     69 static int bcmmbox_intr(void *);
     70 
     71 CFATTACH_DECL_NEW(bcmmbox, sizeof(struct bcm2835mbox_softc),
     72     bcmmbox_match, bcmmbox_attach, NULL, NULL);
     73 
     74 /* ARGSUSED */
     75 static int
     76 bcmmbox_match(device_t parent, cfdata_t match, void *aux)
     77 {
     78 	const char * const compatible[] = { "brcm,bcm2835-mbox", NULL };
     79 	struct fdt_attach_args * const faa = aux;
     80 
     81 	return of_match_compatible(faa->faa_phandle, compatible);
     82 }
     83 
     84 static void
     85 bcmmbox_attach(device_t parent, device_t self, void *aux)
     86 {
     87 	struct bcm2835mbox_softc *sc = device_private(self);
     88 	struct fdt_attach_args * const faa = aux;
     89 	struct bcmmbox_attach_args baa;
     90 	const int phandle = faa->faa_phandle;
     91 	int i;
     92 
     93 	aprint_naive("\n");
     94 	aprint_normal(": VC mailbox\n");
     95 
     96 	sc->sc_dev = self;
     97 	sc->sc_iot = faa->faa_bst;
     98 	sc->sc_dmat = faa->faa_dmat;
     99 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    100 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    101 	for (i = 0; i < BCM2835_MBOX_NUMCHANNELS; ++i)
    102 		cv_init(&sc->sc_chan[i], "bcmmbox");
    103 
    104 	bus_addr_t addr;
    105 	bus_size_t size;
    106 
    107 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    108 		aprint_error(": couldn't get register address\n");
    109 		return;
    110 	}
    111 
    112 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
    113 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    114 		return;
    115 	}
    116 
    117 	char intrstr[128];
    118 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    119 		aprint_error(": failed to decode interrupt\n");
    120 		return;
    121 	}
    122 
    123 	sc->sc_intrh = fdtbus_intr_establish(phandle, 0, IPL_VM, IST_LEVEL,
    124 	    bcmmbox_intr, sc);
    125 	if (sc->sc_intrh == NULL) {
    126 		aprint_error_dev(self, "failed to establish interrupt %s\n",
    127 		    intrstr);
    128 		return;
    129 	}
    130 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    131 
    132 	/* enable mbox interrupt */
    133 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_MBOX_CFG,
    134 	    BCM2835_MBOX_CFG_DATAIRQEN);
    135 
    136 	if (bcm2835mbox_sc == NULL)
    137 		bcm2835mbox_sc = sc;
    138 
    139 	baa.baa_dmat = sc->sc_dmat;
    140 	sc->sc_platdev = config_found_ia(self, "bcmmboxbus", &baa, NULL);
    141 }
    142 
    143 static int
    144 bcmmbox_intr1(struct bcm2835mbox_softc *sc, int cv)
    145 {
    146 	uint32_t mbox, chan, data;
    147 	int ret = 0;
    148 
    149 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    150 
    151 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, BCM2835_MBOX_SIZE,
    152 	    BUS_SPACE_BARRIER_READ);
    153 
    154 	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    155 	    BCM2835_MBOX0_STATUS) & BCM2835_MBOX_STATUS_EMPTY) == 0) {
    156 
    157 		mbox = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    158 		    BCM2835_MBOX0_READ);
    159 
    160 		chan = BCM2835_MBOX_CHAN(mbox);
    161 		data = BCM2835_MBOX_DATA(mbox);
    162 		ret = 1;
    163 
    164 		if (BCM2835_MBOX_CHAN(sc->sc_mbox[chan]) != 0) {
    165 			aprint_error("bcmmbox_intr: chan %d overflow\n",chan);
    166 			continue;
    167 		}
    168 
    169 		sc->sc_mbox[chan] = data | BCM2835_MBOX_CHANMASK;
    170 
    171 		if (cv)
    172 			cv_broadcast(&sc->sc_chan[chan]);
    173 	}
    174 
    175 	return ret;
    176 }
    177 
    178 static int
    179 bcmmbox_intr(void *cookie)
    180 {
    181 	struct bcm2835mbox_softc *sc = cookie;
    182 	int ret;
    183 
    184 	mutex_enter(&sc->sc_intr_lock);
    185 
    186 	ret = bcmmbox_intr1(sc, 1);
    187 
    188 	mutex_exit(&sc->sc_intr_lock);
    189 
    190 	return ret;
    191 }
    192 
    193 void
    194 bcmmbox_read(uint8_t chan, uint32_t *data)
    195 {
    196 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    197 
    198 	KASSERT(sc != NULL);
    199 
    200 	mutex_enter(&sc->sc_lock);
    201 
    202 	mutex_enter(&sc->sc_intr_lock);
    203 	while (BCM2835_MBOX_CHAN(sc->sc_mbox[chan]) == 0) {
    204 		if (cold)
    205 			bcmmbox_intr1(sc, 0);
    206 		else
    207 			cv_wait(&sc->sc_chan[chan], &sc->sc_intr_lock);
    208 	}
    209 	*data = BCM2835_MBOX_DATA(sc->sc_mbox[chan]);
    210 	sc->sc_mbox[chan] = 0;
    211 	mutex_exit(&sc->sc_intr_lock);
    212 
    213 	mutex_exit(&sc->sc_lock);
    214 }
    215 
    216 void
    217 bcmmbox_write(uint8_t chan, uint32_t data)
    218 {
    219 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    220 
    221 	KASSERT(sc != NULL);
    222 	KASSERT(BCM2835_MBOX_CHAN(chan) == chan);
    223 	KASSERT(BCM2835_MBOX_CHAN(data) == 0);
    224 
    225 	mutex_enter(&sc->sc_lock);
    226 
    227 	bcm2835_mbox_write(sc->sc_iot, sc->sc_ioh, chan, data);
    228 
    229 	mutex_exit(&sc->sc_lock);
    230 }
    231 
    232 int
    233 bcmmbox_request(uint8_t chan, void *buf, size_t buflen, uint32_t *pres)
    234 {
    235 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    236 	void *dma_buf;
    237 	bus_dmamap_t map;
    238 	bus_dma_segment_t segs[1];
    239 	int nsegs;
    240 	int error;
    241 
    242 	KASSERT(sc != NULL);
    243 
    244 	error = bus_dmamem_alloc(sc->sc_dmat, buflen, 16, 0, segs, 1,
    245 	    &nsegs, BUS_DMA_WAITOK);
    246 	if (error)
    247 		return error;
    248 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, buflen, &dma_buf,
    249 	    BUS_DMA_WAITOK);
    250 	if (error)
    251 		goto map_failed;
    252 	error = bus_dmamap_create(sc->sc_dmat, buflen, 1, buflen, 0,
    253 	    BUS_DMA_WAITOK, &map);
    254 	if (error)
    255 		goto create_failed;
    256 	error = bus_dmamap_load(sc->sc_dmat, map, dma_buf, buflen, NULL,
    257 	    BUS_DMA_WAITOK);
    258 	if (error)
    259 		goto load_failed;
    260 
    261 	memcpy(dma_buf, buf, buflen);
    262 
    263 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
    264 	     BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    265 	bcmmbox_write(chan, map->dm_segs[0].ds_addr);
    266 	bcmmbox_read(chan, pres);
    267 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
    268 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
    269 
    270 	memcpy(buf, dma_buf, buflen);
    271 
    272 	bus_dmamap_unload(sc->sc_dmat, map);
    273 load_failed:
    274 	bus_dmamap_destroy(sc->sc_dmat, map);
    275 create_failed:
    276 	bus_dmamem_unmap(sc->sc_dmat, dma_buf, buflen);
    277 map_failed:
    278 	bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    279 
    280 	return error;
    281 }
    282