bcm2835_spi.c revision 1.1.4.2 1 1.1.4.2 riz /* $NetBSD: bcm2835_spi.c,v 1.1.4.2 2013/02/13 01:36:14 riz Exp $ */
2 1.1.4.2 riz
3 1.1.4.2 riz /*
4 1.1.4.2 riz * Copyright (c) 2012 Jonathan A. Kollasch
5 1.1.4.2 riz * All rights reserved.
6 1.1.4.2 riz *
7 1.1.4.2 riz * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 riz * modification, are permitted provided that the following conditions
9 1.1.4.2 riz * are met:
10 1.1.4.2 riz * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 riz * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 riz * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 riz * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 riz * documentation and/or other materials provided with the distribution.
15 1.1.4.2 riz *
16 1.1.4.2 riz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.1.4.2 riz * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.4.2 riz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.4.2 riz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 1.1.4.2 riz * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.1.4.2 riz * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1.4.2 riz * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.1.4.2 riz * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1.4.2 riz * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1.4.2 riz * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.1.4.2 riz * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 riz */
28 1.1.4.2 riz
29 1.1.4.2 riz #include <sys/cdefs.h>
30 1.1.4.2 riz __KERNEL_RCSID(0, "$NetBSD: bcm2835_spi.c,v 1.1.4.2 2013/02/13 01:36:14 riz Exp $");
31 1.1.4.2 riz
32 1.1.4.2 riz #include <sys/param.h>
33 1.1.4.2 riz #include <sys/device.h>
34 1.1.4.2 riz #include <sys/systm.h>
35 1.1.4.2 riz #include <sys/mutex.h>
36 1.1.4.2 riz #include <sys/bus.h>
37 1.1.4.2 riz #include <sys/intr.h>
38 1.1.4.2 riz #include <sys/kernel.h>
39 1.1.4.2 riz
40 1.1.4.2 riz #include <sys/bitops.h>
41 1.1.4.2 riz #include <dev/spi/spivar.h>
42 1.1.4.2 riz
43 1.1.4.2 riz #include <arm/broadcom/bcm_amba.h>
44 1.1.4.2 riz #include <arm/broadcom/bcm2835reg.h>
45 1.1.4.2 riz #include <arm/broadcom/bcm2835_spireg.h>
46 1.1.4.2 riz #include <arm/broadcom/bcm2835_gpio_subr.h>
47 1.1.4.2 riz
48 1.1.4.2 riz struct bcmspi_softc {
49 1.1.4.2 riz device_t sc_dev;
50 1.1.4.2 riz bus_space_tag_t sc_iot;
51 1.1.4.2 riz bus_space_handle_t sc_ioh;
52 1.1.4.2 riz bus_size_t sc_ios;
53 1.1.4.2 riz void *sc_intrh;
54 1.1.4.2 riz struct spi_controller sc_spi;
55 1.1.4.2 riz SIMPLEQ_HEAD(,spi_transfer) sc_q;
56 1.1.4.2 riz struct spi_transfer *sc_transfer;
57 1.1.4.2 riz struct spi_chunk *sc_wchunk;
58 1.1.4.2 riz struct spi_chunk *sc_rchunk;
59 1.1.4.2 riz uint32_t sc_CS;
60 1.1.4.2 riz volatile bool sc_running;
61 1.1.4.2 riz };
62 1.1.4.2 riz
63 1.1.4.2 riz static int bcmspi_match(device_t, cfdata_t, void *);
64 1.1.4.2 riz static void bcmspi_attach(device_t, device_t, void *);
65 1.1.4.2 riz
66 1.1.4.2 riz static int bcmspi_configure(void *, int, int, int);
67 1.1.4.2 riz static int bcmspi_transfer(void *, struct spi_transfer *);
68 1.1.4.2 riz
69 1.1.4.2 riz static void bcmspi_start(struct bcmspi_softc * const);
70 1.1.4.2 riz static int bcmspi_intr(void *);
71 1.1.4.2 riz
72 1.1.4.2 riz static void bcmspi_send(struct bcmspi_softc * const);
73 1.1.4.2 riz static void bcmspi_recv(struct bcmspi_softc * const);
74 1.1.4.2 riz
75 1.1.4.2 riz CFATTACH_DECL_NEW(bcmspi, sizeof(struct bcmspi_softc),
76 1.1.4.2 riz bcmspi_match, bcmspi_attach, NULL, NULL);
77 1.1.4.2 riz
78 1.1.4.2 riz static int
79 1.1.4.2 riz bcmspi_match(device_t parent, cfdata_t cf, void *aux)
80 1.1.4.2 riz {
81 1.1.4.2 riz struct amba_attach_args * const aaa = aux;
82 1.1.4.2 riz
83 1.1.4.2 riz if (strcmp(aaa->aaa_name, "bcmspi") != 0)
84 1.1.4.2 riz return 0;
85 1.1.4.2 riz
86 1.1.4.2 riz return 1;
87 1.1.4.2 riz }
88 1.1.4.2 riz
89 1.1.4.2 riz static void
90 1.1.4.2 riz bcmspi_attach(device_t parent, device_t self, void *aux)
91 1.1.4.2 riz {
92 1.1.4.2 riz struct amba_attach_args * const aaa = aux;
93 1.1.4.2 riz struct bcmspi_softc * const sc = device_private(self);
94 1.1.4.2 riz struct spibus_attach_args sba;
95 1.1.4.2 riz
96 1.1.4.2 riz aprint_naive("\n");
97 1.1.4.2 riz aprint_normal(": SPI\n");
98 1.1.4.2 riz
99 1.1.4.2 riz sc->sc_dev = self;
100 1.1.4.2 riz SIMPLEQ_INIT(&sc->sc_q);
101 1.1.4.2 riz sc->sc_iot = aaa->aaa_iot;
102 1.1.4.2 riz if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, aaa->aaa_size, 0,
103 1.1.4.2 riz &sc->sc_ioh) != 0) {
104 1.1.4.2 riz aprint_error_dev(sc->sc_dev, "unable to map device\n");
105 1.1.4.2 riz return;
106 1.1.4.2 riz }
107 1.1.4.2 riz sc->sc_ios = aaa->aaa_size;
108 1.1.4.2 riz
109 1.1.4.2 riz for (u_int pin = 7; pin <= 11; pin++)
110 1.1.4.2 riz bcm2835gpio_function_select(pin, BCM2835_GPIO_ALT0);
111 1.1.4.2 riz
112 1.1.4.2 riz sc->sc_intrh = bcm2835_intr_establish(aaa->aaa_intr, IPL_BIO,
113 1.1.4.2 riz bcmspi_intr, sc);
114 1.1.4.2 riz if (sc->sc_intrh == NULL) {
115 1.1.4.2 riz aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
116 1.1.4.2 riz return;
117 1.1.4.2 riz }
118 1.1.4.2 riz
119 1.1.4.2 riz sc->sc_spi.sct_cookie = sc;
120 1.1.4.2 riz sc->sc_spi.sct_configure = bcmspi_configure;
121 1.1.4.2 riz sc->sc_spi.sct_transfer = bcmspi_transfer;
122 1.1.4.2 riz sc->sc_spi.sct_nslaves = 3;
123 1.1.4.2 riz
124 1.1.4.2 riz sba.sba_controller = &sc->sc_spi;
125 1.1.4.2 riz
126 1.1.4.2 riz (void) config_found_ia(self, "spibus", &sba, spibus_print);
127 1.1.4.2 riz }
128 1.1.4.2 riz
129 1.1.4.2 riz static int
130 1.1.4.2 riz bcmspi_configure(void *cookie, int slave, int mode, int speed)
131 1.1.4.2 riz {
132 1.1.4.2 riz struct bcmspi_softc * const sc = cookie;
133 1.1.4.2 riz uint32_t cs, clk;
134 1.1.4.2 riz
135 1.1.4.2 riz cs = SPI_CS_INTR | SPI_CS_INTD;
136 1.1.4.2 riz
137 1.1.4.2 riz if (slave > 2)
138 1.1.4.2 riz return EINVAL;
139 1.1.4.2 riz
140 1.1.4.2 riz if (speed <= 0)
141 1.1.4.2 riz return EINVAL;
142 1.1.4.2 riz
143 1.1.4.2 riz switch (mode) {
144 1.1.4.2 riz case SPI_MODE_0:
145 1.1.4.2 riz cs |= 0;
146 1.1.4.2 riz break;
147 1.1.4.2 riz case SPI_MODE_1:
148 1.1.4.2 riz cs |= SPI_CS_CPHA;
149 1.1.4.2 riz break;
150 1.1.4.2 riz case SPI_MODE_2:
151 1.1.4.2 riz cs |= SPI_CS_CPOL;
152 1.1.4.2 riz break;
153 1.1.4.2 riz case SPI_MODE_3:
154 1.1.4.2 riz cs |= SPI_CS_CPHA|SPI_CS_CPOL;
155 1.1.4.2 riz break;
156 1.1.4.2 riz default:
157 1.1.4.2 riz return EINVAL;
158 1.1.4.2 riz }
159 1.1.4.2 riz
160 1.1.4.2 riz sc->sc_CS = cs;
161 1.1.4.2 riz
162 1.1.4.2 riz bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
163 1.1.4.2 riz
164 1.1.4.2 riz clk = 2 * 250000000 / speed; /* XXX 250MHz */
165 1.1.4.2 riz clk = (clk / 2) + (clk & 1);
166 1.1.4.2 riz clk = roundup(clk, 2);
167 1.1.4.2 riz clk = __SHIFTIN(clk, SPI_CLK_CDIV);
168 1.1.4.2 riz bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CLK, clk);
169 1.1.4.2 riz
170 1.1.4.2 riz return 0;
171 1.1.4.2 riz }
172 1.1.4.2 riz
173 1.1.4.2 riz static int
174 1.1.4.2 riz bcmspi_transfer(void *cookie, struct spi_transfer *st)
175 1.1.4.2 riz {
176 1.1.4.2 riz struct bcmspi_softc * const sc = cookie;
177 1.1.4.2 riz int s;
178 1.1.4.2 riz
179 1.1.4.2 riz s = splbio();
180 1.1.4.2 riz spi_transq_enqueue(&sc->sc_q, st);
181 1.1.4.2 riz if (sc->sc_running == false) {
182 1.1.4.2 riz bcmspi_start(sc);
183 1.1.4.2 riz }
184 1.1.4.2 riz splx(s);
185 1.1.4.2 riz return 0;
186 1.1.4.2 riz }
187 1.1.4.2 riz
188 1.1.4.2 riz static void
189 1.1.4.2 riz bcmspi_start(struct bcmspi_softc * const sc)
190 1.1.4.2 riz {
191 1.1.4.2 riz struct spi_transfer *st;
192 1.1.4.2 riz uint32_t cs;
193 1.1.4.2 riz
194 1.1.4.2 riz while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
195 1.1.4.2 riz
196 1.1.4.2 riz spi_transq_dequeue(&sc->sc_q);
197 1.1.4.2 riz
198 1.1.4.2 riz KASSERT(sc->sc_transfer == NULL);
199 1.1.4.2 riz sc->sc_transfer = st;
200 1.1.4.2 riz sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
201 1.1.4.2 riz
202 1.1.4.2 riz cs = sc->sc_CS;
203 1.1.4.2 riz cs |= SPI_CS_TA;
204 1.1.4.2 riz cs |= SPI_CS_CLEAR_TX;
205 1.1.4.2 riz cs |= SPI_CS_CLEAR_RX;
206 1.1.4.2 riz KASSERT(st->st_slave <= 2);
207 1.1.4.2 riz cs |= __SHIFTIN(st->st_slave, SPI_CS_CS);
208 1.1.4.2 riz sc->sc_running = true;
209 1.1.4.2 riz bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
210 1.1.4.2 riz
211 1.1.4.2 riz if (!cold)
212 1.1.4.2 riz return;
213 1.1.4.2 riz
214 1.1.4.2 riz int s = splbio();
215 1.1.4.2 riz for (;;) {
216 1.1.4.2 riz bcmspi_intr(sc);
217 1.1.4.2 riz if (ISSET(st->st_flags, SPI_F_DONE))
218 1.1.4.2 riz break;
219 1.1.4.2 riz }
220 1.1.4.2 riz splx(s);
221 1.1.4.2 riz }
222 1.1.4.2 riz
223 1.1.4.2 riz sc->sc_running = false;
224 1.1.4.2 riz }
225 1.1.4.2 riz
226 1.1.4.2 riz static void
227 1.1.4.2 riz bcmspi_send(struct bcmspi_softc * const sc)
228 1.1.4.2 riz {
229 1.1.4.2 riz uint32_t fd;
230 1.1.4.2 riz uint32_t cs;
231 1.1.4.2 riz struct spi_chunk *chunk;
232 1.1.4.2 riz
233 1.1.4.2 riz while ((chunk = sc->sc_wchunk) != NULL) {
234 1.1.4.2 riz while (chunk->chunk_wresid) {
235 1.1.4.2 riz cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
236 1.1.4.2 riz if ((cs & SPI_CS_TXD) == 0)
237 1.1.4.2 riz return;
238 1.1.4.2 riz if (chunk->chunk_wptr) {
239 1.1.4.2 riz fd = *chunk->chunk_wptr++;
240 1.1.4.2 riz } else {
241 1.1.4.2 riz fd = '\0';
242 1.1.4.2 riz }
243 1.1.4.2 riz bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO, fd);
244 1.1.4.2 riz chunk->chunk_wresid--;
245 1.1.4.2 riz }
246 1.1.4.2 riz sc->sc_wchunk = sc->sc_wchunk->chunk_next;
247 1.1.4.2 riz }
248 1.1.4.2 riz }
249 1.1.4.2 riz
250 1.1.4.2 riz static void
251 1.1.4.2 riz bcmspi_recv(struct bcmspi_softc * const sc)
252 1.1.4.2 riz {
253 1.1.4.2 riz uint32_t fd;
254 1.1.4.2 riz uint32_t cs;
255 1.1.4.2 riz struct spi_chunk *chunk;
256 1.1.4.2 riz
257 1.1.4.2 riz while ((chunk = sc->sc_rchunk) != NULL) {
258 1.1.4.2 riz while (chunk->chunk_rresid) {
259 1.1.4.2 riz cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
260 1.1.4.2 riz if ((cs & SPI_CS_RXD) == 0)
261 1.1.4.2 riz return;
262 1.1.4.2 riz fd = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO);
263 1.1.4.2 riz if (chunk->chunk_rptr) {
264 1.1.4.2 riz *chunk->chunk_rptr++ = fd & 0xff;
265 1.1.4.2 riz }
266 1.1.4.2 riz chunk->chunk_rresid--;
267 1.1.4.2 riz }
268 1.1.4.2 riz sc->sc_rchunk = sc->sc_rchunk->chunk_next;
269 1.1.4.2 riz }
270 1.1.4.2 riz }
271 1.1.4.2 riz
272 1.1.4.2 riz static int
273 1.1.4.2 riz bcmspi_intr(void *cookie)
274 1.1.4.2 riz {
275 1.1.4.2 riz struct bcmspi_softc * const sc = cookie;
276 1.1.4.2 riz struct spi_transfer *st;
277 1.1.4.2 riz uint32_t cs;
278 1.1.4.2 riz
279 1.1.4.2 riz cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
280 1.1.4.2 riz if (ISSET(cs, SPI_CS_DONE)) {
281 1.1.4.2 riz if (sc->sc_wchunk != NULL) {
282 1.1.4.2 riz bcmspi_send(sc);
283 1.1.4.2 riz goto end;
284 1.1.4.2 riz } else {
285 1.1.4.2 riz bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS,
286 1.1.4.2 riz sc->sc_CS);
287 1.1.4.2 riz bcmspi_recv(sc);
288 1.1.4.2 riz sc->sc_rchunk = sc->sc_wchunk = NULL;
289 1.1.4.2 riz st = sc->sc_transfer;
290 1.1.4.2 riz sc->sc_transfer = NULL;
291 1.1.4.2 riz KASSERT(st != NULL);
292 1.1.4.2 riz spi_done(st, 0);
293 1.1.4.2 riz sc->sc_running = false;
294 1.1.4.2 riz goto end;
295 1.1.4.2 riz }
296 1.1.4.2 riz } else if (ISSET(cs, SPI_CS_RXR)) {
297 1.1.4.2 riz bcmspi_recv(sc);
298 1.1.4.2 riz bcmspi_send(sc);
299 1.1.4.2 riz }
300 1.1.4.2 riz
301 1.1.4.2 riz end:
302 1.1.4.2 riz return ISSET(cs, SPI_CS_DONE|SPI_CS_RXR);
303 1.1.4.2 riz }
304