bcm2835_mbox.c revision 1.5 1 /* $NetBSD: bcm2835_mbox.c,v 1.5 2013/01/08 12:15:42 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.5 2013/01/08 12:15:42 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/bcm_amba.h>
44 #include <arm/broadcom/bcm2835_mbox.h>
45 #include <arm/broadcom/bcm2835_mboxreg.h>
46 #include <arm/broadcom/bcm2835reg.h>
47
48 struct bcm2835mbox_softc {
49 device_t sc_dev;
50 device_t sc_platdev;
51
52 bus_space_tag_t sc_iot;
53 bus_space_handle_t sc_ioh;
54 bus_dma_tag_t sc_dmat;
55
56 kmutex_t sc_lock;
57 };
58
59 static struct bcm2835mbox_softc *bcm2835mbox_sc;
60
61 static int bcmmbox_match(device_t, cfdata_t, void *);
62 static void bcmmbox_attach(device_t, device_t, void *);
63
64 CFATTACH_DECL_NEW(bcmmbox, sizeof(struct bcm2835mbox_softc),
65 bcmmbox_match, bcmmbox_attach, NULL, NULL);
66
67 /* ARGSUSED */
68 static int
69 bcmmbox_match(device_t parent, cfdata_t match, void *aux)
70 {
71 struct amba_attach_args *aaa = aux;
72
73 if (strcmp(aaa->aaa_name, "bcmmbox") != 0)
74 return 0;
75
76 return 1;
77 }
78
79 static void
80 bcmmbox_attach(device_t parent, device_t self, void *aux)
81 {
82 struct bcm2835mbox_softc *sc = device_private(self);
83 struct amba_attach_args *aaa = aux;
84 struct bcmmbox_attach_args baa;
85
86 aprint_naive("\n");
87 aprint_normal(": VC mailbox\n");
88
89 if (bcm2835mbox_sc == NULL)
90 bcm2835mbox_sc = sc;
91
92 sc->sc_dev = self;
93 sc->sc_iot = aaa->aaa_iot;
94 sc->sc_dmat = aaa->aaa_dmat;
95 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
96
97 if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_MBOX_SIZE, 0,
98 &sc->sc_ioh)) {
99 aprint_error_dev(sc->sc_dev, "unable to map device\n");
100 return;
101 }
102
103 baa.baa_dmat = aaa->aaa_dmat;
104 sc->sc_platdev = config_found_ia(self, "bcmmboxbus", &baa, NULL);
105 }
106
107 void
108 bcmmbox_read(uint8_t chan, uint32_t *data)
109 {
110 struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
111
112 KASSERT(sc != NULL);
113
114 return bcm2835_mbox_read(sc->sc_iot, sc->sc_ioh, chan, data);
115 }
116
117 void
118 bcmmbox_write(uint8_t chan, uint32_t data)
119 {
120 struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
121
122 KASSERT(sc != NULL);
123
124 return bcm2835_mbox_write(sc->sc_iot, sc->sc_ioh, chan, data);
125 }
126
127 int
128 bcmmbox_request(uint8_t chan, void *buf, size_t buflen, uint32_t *pres)
129 {
130 struct bcm2835mbox_softc *sc = bcm2835mbox_sc;
131 void *dma_buf;
132 bus_dmamap_t map;
133 bus_dma_segment_t segs[1];
134 int nsegs;
135 int error;
136
137 KASSERT(sc != NULL);
138
139 error = bus_dmamem_alloc(sc->sc_dmat, buflen, 16, 0, segs, 1,
140 &nsegs, BUS_DMA_WAITOK);
141 if (error)
142 return error;
143 error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, buflen, &dma_buf,
144 BUS_DMA_WAITOK);
145 if (error)
146 goto map_failed;
147 error = bus_dmamap_create(sc->sc_dmat, buflen, 1, buflen, 0,
148 BUS_DMA_WAITOK, &map);
149 if (error)
150 goto create_failed;
151 error = bus_dmamap_load(sc->sc_dmat, map, dma_buf, buflen, NULL,
152 BUS_DMA_WAITOK);
153 if (error)
154 goto load_failed;
155
156 memcpy(dma_buf, buf, buflen);
157
158 mutex_enter(&sc->sc_lock);
159
160 bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_PREWRITE);
161 bcmmbox_write(chan, map->dm_segs[0].ds_addr);
162 bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_POSTWRITE);
163 bus_dmamap_sync(sc->sc_dmat, map, 0, buflen, BUS_DMASYNC_PREREAD);
164 bcmmbox_read(chan, pres);
165
166 mutex_exit(&sc->sc_lock);
167
168 memcpy(buf, dma_buf, buflen);
169
170 bus_dmamap_unload(sc->sc_dmat, map);
171 load_failed:
172 bus_dmamap_destroy(sc->sc_dmat, map);
173 create_failed:
174 bus_dmamem_unmap(sc->sc_dmat, dma_buf, buflen);
175 map_failed:
176 bus_dmamem_free(sc->sc_dmat, segs, nsegs);
177
178 return error;
179 }
180