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