Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_mbox.c revision 1.2.4.4
      1  1.2.4.4  yamt /*	$NetBSD: bcm2835_mbox.c,v 1.2.4.4 2014/05/22 11:39:31 yamt Exp $	*/
      2  1.2.4.2  yamt 
      3  1.2.4.2  yamt /*-
      4  1.2.4.2  yamt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.2.4.2  yamt  * All rights reserved.
      6  1.2.4.2  yamt  *
      7  1.2.4.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  yamt  * by Nick Hudson
      9  1.2.4.2  yamt  *
     10  1.2.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.2.4.2  yamt  * are met:
     13  1.2.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  yamt  *
     19  1.2.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  yamt  */
     31  1.2.4.2  yamt 
     32  1.2.4.2  yamt #include <sys/cdefs.h>
     33  1.2.4.4  yamt __KERNEL_RCSID(0, "$NetBSD: bcm2835_mbox.c,v 1.2.4.4 2014/05/22 11:39:31 yamt Exp $");
     34  1.2.4.2  yamt 
     35  1.2.4.2  yamt #include <sys/param.h>
     36  1.2.4.2  yamt #include <sys/systm.h>
     37  1.2.4.2  yamt #include <sys/device.h>
     38  1.2.4.2  yamt #include <sys/kernel.h>
     39  1.2.4.2  yamt #include <sys/timetc.h>
     40  1.2.4.2  yamt #include <sys/bus.h>
     41  1.2.4.3  yamt #include <sys/mutex.h>
     42  1.2.4.2  yamt 
     43  1.2.4.2  yamt #include <arm/broadcom/bcm_amba.h>
     44  1.2.4.2  yamt #include <arm/broadcom/bcm2835_mbox.h>
     45  1.2.4.2  yamt #include <arm/broadcom/bcm2835_mboxreg.h>
     46  1.2.4.2  yamt #include <arm/broadcom/bcm2835reg.h>
     47  1.2.4.2  yamt 
     48  1.2.4.2  yamt struct bcm2835mbox_softc {
     49  1.2.4.2  yamt 	device_t sc_dev;
     50  1.2.4.3  yamt 	device_t sc_platdev;
     51  1.2.4.2  yamt 
     52  1.2.4.2  yamt 	bus_space_tag_t sc_iot;
     53  1.2.4.2  yamt 	bus_space_handle_t sc_ioh;
     54  1.2.4.3  yamt 	bus_dma_tag_t sc_dmat;
     55  1.2.4.3  yamt 
     56  1.2.4.3  yamt 	kmutex_t sc_lock;
     57  1.2.4.2  yamt };
     58  1.2.4.2  yamt 
     59  1.2.4.2  yamt static struct bcm2835mbox_softc *bcm2835mbox_sc;
     60  1.2.4.2  yamt 
     61  1.2.4.2  yamt static int bcmmbox_match(device_t, cfdata_t, void *);
     62  1.2.4.2  yamt static void bcmmbox_attach(device_t, device_t, void *);
     63  1.2.4.2  yamt 
     64  1.2.4.2  yamt CFATTACH_DECL_NEW(bcmmbox, sizeof(struct bcm2835mbox_softc),
     65  1.2.4.2  yamt     bcmmbox_match, bcmmbox_attach, NULL, NULL);
     66  1.2.4.2  yamt 
     67  1.2.4.2  yamt /* ARGSUSED */
     68  1.2.4.2  yamt static int
     69  1.2.4.2  yamt bcmmbox_match(device_t parent, cfdata_t match, void *aux)
     70  1.2.4.2  yamt {
     71  1.2.4.2  yamt 	struct amba_attach_args *aaa = aux;
     72  1.2.4.2  yamt 
     73  1.2.4.2  yamt 	if (strcmp(aaa->aaa_name, "bcmmbox") != 0)
     74  1.2.4.2  yamt 		return 0;
     75  1.2.4.2  yamt 
     76  1.2.4.2  yamt 	return 1;
     77  1.2.4.2  yamt }
     78  1.2.4.2  yamt 
     79  1.2.4.2  yamt static void
     80  1.2.4.2  yamt bcmmbox_attach(device_t parent, device_t self, void *aux)
     81  1.2.4.2  yamt {
     82  1.2.4.2  yamt         struct bcm2835mbox_softc *sc = device_private(self);
     83  1.2.4.2  yamt  	struct amba_attach_args *aaa = aux;
     84  1.2.4.3  yamt 	struct bcmmbox_attach_args baa;
     85  1.2.4.2  yamt 
     86  1.2.4.2  yamt 	aprint_naive("\n");
     87  1.2.4.2  yamt 	aprint_normal(": VC mailbox\n");
     88  1.2.4.2  yamt 
     89  1.2.4.2  yamt 	if (bcm2835mbox_sc == NULL)
     90  1.2.4.2  yamt 		bcm2835mbox_sc = sc;
     91  1.2.4.3  yamt 
     92  1.2.4.2  yamt 	sc->sc_dev = self;
     93  1.2.4.2  yamt 	sc->sc_iot = aaa->aaa_iot;
     94  1.2.4.3  yamt 	sc->sc_dmat = aaa->aaa_dmat;
     95  1.2.4.3  yamt 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
     96  1.2.4.2  yamt 
     97  1.2.4.2  yamt 	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_MBOX_SIZE, 0,
     98  1.2.4.2  yamt 	    &sc->sc_ioh)) {
     99  1.2.4.2  yamt 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    100  1.2.4.2  yamt 		return;
    101  1.2.4.2  yamt 	}
    102  1.2.4.3  yamt 
    103  1.2.4.3  yamt 	baa.baa_dmat = aaa->aaa_dmat;
    104  1.2.4.3  yamt 	sc->sc_platdev = config_found_ia(self, "bcmmboxbus", &baa, NULL);
    105  1.2.4.2  yamt }
    106  1.2.4.2  yamt 
    107  1.2.4.2  yamt void
    108  1.2.4.2  yamt bcmmbox_read(uint8_t chan, uint32_t *data)
    109  1.2.4.2  yamt {
    110  1.2.4.2  yamt 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    111  1.2.4.2  yamt 
    112  1.2.4.2  yamt 	KASSERT(sc != NULL);
    113  1.2.4.2  yamt 
    114  1.2.4.2  yamt 	return bcm2835_mbox_read(sc->sc_iot, sc->sc_ioh, chan, data);
    115  1.2.4.2  yamt }
    116  1.2.4.2  yamt 
    117  1.2.4.2  yamt void
    118  1.2.4.2  yamt bcmmbox_write(uint8_t chan, uint32_t data)
    119  1.2.4.2  yamt {
    120  1.2.4.2  yamt 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    121  1.2.4.2  yamt 
    122  1.2.4.2  yamt 	KASSERT(sc != NULL);
    123  1.2.4.2  yamt 
    124  1.2.4.2  yamt 	return bcm2835_mbox_write(sc->sc_iot, sc->sc_ioh, chan, data);
    125  1.2.4.2  yamt }
    126  1.2.4.3  yamt 
    127  1.2.4.3  yamt int
    128  1.2.4.3  yamt bcmmbox_request(uint8_t chan, void *buf, size_t buflen, uint32_t *pres)
    129  1.2.4.3  yamt {
    130  1.2.4.3  yamt 	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
    131  1.2.4.3  yamt 	void *dma_buf;
    132  1.2.4.3  yamt 	bus_dmamap_t map;
    133  1.2.4.3  yamt 	bus_dma_segment_t segs[1];
    134  1.2.4.3  yamt 	int nsegs;
    135  1.2.4.3  yamt 	int error;
    136  1.2.4.3  yamt 
    137  1.2.4.3  yamt 	KASSERT(sc != NULL);
    138  1.2.4.3  yamt 
    139  1.2.4.3  yamt 	error = bus_dmamem_alloc(sc->sc_dmat, buflen, 16, 0, segs, 1,
    140  1.2.4.3  yamt 	    &nsegs, BUS_DMA_WAITOK);
    141  1.2.4.3  yamt 	if (error)
    142  1.2.4.3  yamt 		return error;
    143  1.2.4.3  yamt 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, buflen, &dma_buf,
    144  1.2.4.3  yamt 	    BUS_DMA_WAITOK);
    145  1.2.4.3  yamt 	if (error)
    146  1.2.4.3  yamt 		goto map_failed;
    147  1.2.4.3  yamt 	error = bus_dmamap_create(sc->sc_dmat, buflen, 1, buflen, 0,
    148  1.2.4.3  yamt 	    BUS_DMA_WAITOK, &map);
    149  1.2.4.3  yamt 	if (error)
    150  1.2.4.3  yamt 		goto create_failed;
    151  1.2.4.3  yamt 	error = bus_dmamap_load(sc->sc_dmat, map, dma_buf, buflen, NULL,
    152  1.2.4.3  yamt 	    BUS_DMA_WAITOK);
    153  1.2.4.3  yamt 	if (error)
    154  1.2.4.3  yamt 		goto load_failed;
    155  1.2.4.3  yamt 
    156  1.2.4.3  yamt 	memcpy(dma_buf, buf, buflen);
    157  1.2.4.3  yamt 
    158  1.2.4.3  yamt 	mutex_enter(&sc->sc_lock);
    159  1.2.4.3  yamt 
    160  1.2.4.3  yamt 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_PREWRITE);
    161  1.2.4.3  yamt 	bcmmbox_write(chan, map->dm_segs[0].ds_addr);
    162  1.2.4.3  yamt 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_POSTWRITE);
    163  1.2.4.3  yamt 	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_PREREAD);
    164  1.2.4.3  yamt 	bcmmbox_read(chan, pres);
    165  1.2.4.3  yamt 
    166  1.2.4.3  yamt 	mutex_exit(&sc->sc_lock);
    167  1.2.4.3  yamt 
    168  1.2.4.3  yamt 	memcpy(buf, dma_buf, buflen);
    169  1.2.4.3  yamt 
    170  1.2.4.3  yamt 	bus_dmamap_unload(sc->sc_dmat, map);
    171  1.2.4.3  yamt load_failed:
    172  1.2.4.3  yamt 	bus_dmamap_destroy(sc->sc_dmat, map);
    173  1.2.4.3  yamt create_failed:
    174  1.2.4.3  yamt 	bus_dmamem_unmap(sc->sc_dmat, dma_buf, buflen);
    175  1.2.4.3  yamt map_failed:
    176  1.2.4.3  yamt 	bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    177  1.2.4.4  yamt 
    178  1.2.4.3  yamt 	return error;
    179  1.2.4.3  yamt }
    180