bcm2835_mbox.c revision 1.14
1/*	$NetBSD: bcm2835_mbox.c,v 1.14 2019/12/30 18:43:38 jmcneill 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.14 2019/12/30 18:43:38 jmcneill 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
48static struct bcm2835mbox_softc *bcm2835mbox_sc;
49
50static int
51bcmmbox_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
85void
86bcmmbox_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	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_MBOX_CFG,
101	    BCM2835_MBOX_CFG_DATAIRQEN);
102
103	baa.baa_dmat = sc->sc_dmat;
104	sc->sc_platdev = config_found_ia(sc->sc_dev, "bcmmboxbus", &baa, NULL);
105}
106
107int
108bcmmbox_intr(void *cookie)
109{
110	struct bcm2835mbox_softc *sc = cookie;
111	int ret;
112
113	mutex_enter(&sc->sc_intr_lock);
114
115	ret = bcmmbox_intr1(sc, 1);
116
117	mutex_exit(&sc->sc_intr_lock);
118
119	return ret;
120}
121
122void
123bcmmbox_read(uint8_t chan, uint32_t *data)
124{
125	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
126
127	KASSERT(sc != NULL);
128
129	mutex_enter(&sc->sc_lock);
130
131	mutex_enter(&sc->sc_intr_lock);
132	while (BCM2835_MBOX_CHAN(sc->sc_mbox[chan]) == 0) {
133		if (cold)
134			bcmmbox_intr1(sc, 0);
135		else
136			cv_wait(&sc->sc_chan[chan], &sc->sc_intr_lock);
137	}
138	*data = BCM2835_MBOX_DATA(sc->sc_mbox[chan]);
139	sc->sc_mbox[chan] = 0;
140	mutex_exit(&sc->sc_intr_lock);
141
142	mutex_exit(&sc->sc_lock);
143}
144
145void
146bcmmbox_write(uint8_t chan, uint32_t data)
147{
148	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
149
150	KASSERT(sc != NULL);
151	KASSERT(BCM2835_MBOX_CHAN(chan) == chan);
152	KASSERT(BCM2835_MBOX_CHAN(data) == 0);
153
154	mutex_enter(&sc->sc_lock);
155
156	bcm2835_mbox_write(sc->sc_iot, sc->sc_ioh, chan, data);
157
158	mutex_exit(&sc->sc_lock);
159}
160
161int
162bcmmbox_request(uint8_t chan, void *buf, size_t buflen, uint32_t *pres)
163{
164	struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
165	void *dma_buf;
166	bus_dmamap_t map;
167	bus_dma_segment_t segs[1];
168	int nsegs;
169	int error;
170
171	KASSERT(sc != NULL);
172
173	error = bus_dmamem_alloc(sc->sc_dmat, buflen, 16, 0, segs, 1,
174	    &nsegs, BUS_DMA_WAITOK);
175	if (error)
176		return error;
177	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, buflen, &dma_buf,
178	    BUS_DMA_WAITOK);
179	if (error)
180		goto map_failed;
181	error = bus_dmamap_create(sc->sc_dmat, buflen, 1, buflen, 0,
182	    BUS_DMA_WAITOK, &map);
183	if (error)
184		goto create_failed;
185	error = bus_dmamap_load(sc->sc_dmat, map, dma_buf, buflen, NULL,
186	    BUS_DMA_WAITOK);
187	if (error)
188		goto load_failed;
189
190	memcpy(dma_buf, buf, buflen);
191
192	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
193	     BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
194	bcmmbox_write(chan, map->dm_segs[0].ds_addr);
195	bcmmbox_read(chan, pres);
196	bus_dmamap_sync(sc->sc_dmat, map, 0, buflen,
197	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
198
199	memcpy(buf, dma_buf, buflen);
200
201	bus_dmamap_unload(sc->sc_dmat, map);
202load_failed:
203	bus_dmamap_destroy(sc->sc_dmat, map);
204create_failed:
205	bus_dmamem_unmap(sc->sc_dmat, dma_buf, buflen);
206map_failed:
207	bus_dmamem_free(sc->sc_dmat, segs, nsegs);
208
209	return error;
210}
211