]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_spi.c
Copy ^/vendor/NetBSD/tests/dist/lib/libc/hash/t_hmac.c to
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_spi.c
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.
3  * Copyright (c) 2011-2012 Ian Lepore.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include "opt_platform.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43 #include <sys/sx.h>
44
45 #include <machine/bus.h>
46
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91_spireg.h>
49 #include <arm/at91/at91_pdcreg.h>
50
51 #include <dev/spibus/spi.h>
52 #include <dev/spibus/spibusvar.h>
53
54 #ifdef FDT
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 #endif
58
59 #include "spibus_if.h"
60
61 struct at91_spi_softc
62 {
63         device_t dev;                   /* Myself */
64         void *intrhand;                 /* Interrupt handle */
65         struct resource *irq_res;       /* IRQ resource */
66         struct resource *mem_res;       /* Memory resource */
67         bus_dma_tag_t dmatag;           /* bus dma tag for transfers */
68         bus_dmamap_t map[4];            /* Maps for the transaction */
69         struct sx xfer_mtx;             /* Enforce one transfer at a time */
70         uint32_t xfer_done;             /* interrupt<->mainthread signaling */
71 };
72
73 #define CS_TO_MR(cs)    ((~(1 << (cs)) & 0x0f) << 16)
74
75 static inline uint32_t
76 RD4(struct at91_spi_softc *sc, bus_size_t off)
77 {
78
79         return (bus_read_4(sc->mem_res, off));
80 }
81
82 static inline void
83 WR4(struct at91_spi_softc *sc, bus_size_t off, uint32_t val)
84 {
85
86         bus_write_4(sc->mem_res, off, val);
87 }
88
89 /* bus entry points */
90 static int at91_spi_attach(device_t dev);
91 static int at91_spi_detach(device_t dev);
92 static int at91_spi_probe(device_t dev);
93 static int at91_spi_transfer(device_t dev, device_t child,
94     struct spi_command *cmd);
95
96 /* helper routines */
97 static void at91_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs,
98     int error);
99 static int at91_spi_activate(device_t dev);
100 static void at91_spi_deactivate(device_t dev);
101 static void at91_spi_intr(void *arg);
102
103 static int
104 at91_spi_probe(device_t dev)
105 {
106 #ifdef FDT
107         if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-spi"))
108                 return (ENXIO);
109 #endif
110         device_set_desc(dev, "AT91 SPI");
111         return (0);
112 }
113
114 static int
115 at91_spi_attach(device_t dev)
116 {
117         struct at91_spi_softc *sc;
118         int err;
119         uint32_t csr;
120
121         sc = device_get_softc(dev);
122
123         sc->dev = dev;
124         sx_init(&sc->xfer_mtx, device_get_nameunit(dev));
125
126         /*
127          * Allocate resources.
128          */
129         err = at91_spi_activate(dev);
130         if (err)
131                 goto out;
132
133 #ifdef FDT
134         /*
135          * Disable devices need to hold their resources, so return now and not attach
136          * the spibus, setup interrupt handlers, etc.
137          */
138         if (!ofw_bus_status_okay(dev))
139                 return 0;
140 #endif
141
142         /*
143          * Set up the hardware.
144          */
145
146         WR4(sc, SPI_CR, SPI_CR_SWRST);
147         /* "Software Reset must be Written Twice" erratum */
148         WR4(sc, SPI_CR, SPI_CR_SWRST);
149         WR4(sc, SPI_IDR, 0xffffffff);
150
151         WR4(sc, SPI_MR, (0xf << 24) | SPI_MR_MSTR | SPI_MR_MODFDIS |
152             CS_TO_MR(0));
153
154         /*
155          * For now, run the bus at the slowest speed possible as otherwise we
156          * may encounter data corruption on transmit as seen with ETHERNUT5
157          * and AT45DB321D even though both board and slave device can take
158          * more.
159          * This also serves as a work-around for the "NPCSx rises if no data
160          * data is to be transmitted" erratum.  The ideal workaround for the
161          * latter is to take the chip select control away from the peripheral
162          * and manage it directly as a GPIO line.  The easy solution is to
163          * slow down the bus so dramatically that it just never gets starved
164          * as may be seen when the OCHI controller is running and consuming
165          * memory and APB bandwidth.
166          * Also, currently we lack a way for lettting both the board and the
167          * slave devices take their maximum supported SPI clocks into account.
168          * Also, we hard-wire SPI mode to 3.
169          */
170         csr = SPI_CSR_CPOL | (4 << 16) | (0xff << 8);
171         WR4(sc, SPI_CSR0, csr);
172         WR4(sc, SPI_CSR1, csr);
173         WR4(sc, SPI_CSR2, csr);
174         WR4(sc, SPI_CSR3, csr);
175
176         WR4(sc, SPI_CR, SPI_CR_SPIEN);
177
178         WR4(sc, PDC_PTCR, PDC_PTCR_TXTDIS);
179         WR4(sc, PDC_PTCR, PDC_PTCR_RXTDIS);
180         WR4(sc, PDC_RNPR, 0);
181         WR4(sc, PDC_RNCR, 0);
182         WR4(sc, PDC_TNPR, 0);
183         WR4(sc, PDC_TNCR, 0);
184         WR4(sc, PDC_RPR, 0);
185         WR4(sc, PDC_RCR, 0);
186         WR4(sc, PDC_TPR, 0);
187         WR4(sc, PDC_TCR, 0);
188         RD4(sc, SPI_RDR);
189         RD4(sc, SPI_SR);
190
191         device_add_child(dev, "spibus", -1);
192         bus_generic_attach(dev);
193 out:
194         if (err)
195                 at91_spi_deactivate(dev);
196         return (err);
197 }
198
199 static int
200 at91_spi_detach(device_t dev)
201 {
202
203         return (EBUSY); /* XXX */
204 }
205
206 static int
207 at91_spi_activate(device_t dev)
208 {
209         struct at91_spi_softc *sc;
210         int err, i, rid;
211
212         sc = device_get_softc(dev);
213         err = ENOMEM;
214
215         rid = 0;
216         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
217             RF_ACTIVE);
218         if (sc->mem_res == NULL)
219                 goto out;
220
221         rid = 0;
222         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
223             RF_ACTIVE);
224         if (sc->irq_res == NULL)
225                 goto out;
226         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
227             NULL, at91_spi_intr, sc, &sc->intrhand);
228         if (err != 0)
229                 goto out;
230
231         err = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
232             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 2048, 1,
233             2048, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->dmatag);
234         if (err != 0)
235                 goto out;
236
237         for (i = 0; i < 4; i++) {
238                 err = bus_dmamap_create(sc->dmatag, 0,  &sc->map[i]);
239                 if (err != 0)
240                         goto out;
241         }
242 out:
243         if (err != 0)
244                 at91_spi_deactivate(dev);
245         return (err);
246 }
247
248 static void
249 at91_spi_deactivate(device_t dev)
250 {
251         struct at91_spi_softc *sc;
252         int i;
253
254         sc = device_get_softc(dev);
255         bus_generic_detach(dev);
256
257         for (i = 0; i < 4; i++)
258                 if (sc->map[i])
259                         bus_dmamap_destroy(sc->dmatag, sc->map[i]);
260
261         if (sc->dmatag)
262                 bus_dma_tag_destroy(sc->dmatag);
263
264         if (sc->intrhand)
265                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
266         sc->intrhand = NULL;
267         if (sc->irq_res)
268                 bus_release_resource(dev, SYS_RES_IRQ,
269                     rman_get_rid(sc->irq_res), sc->irq_res);
270         sc->irq_res = NULL;
271
272         if (sc->mem_res)
273                 bus_release_resource(dev, SYS_RES_MEMORY,
274                     rman_get_rid(sc->mem_res), sc->mem_res);
275         sc->mem_res = NULL;
276 }
277
278 static void
279 at91_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs __unused,
280     int error)
281 {
282
283         if (error != 0)
284                 return;
285         *(bus_addr_t *)arg = segs[0].ds_addr;
286 }
287
288 static int
289 at91_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
290 {
291         struct at91_spi_softc *sc;
292         bus_addr_t addr;
293         int err, i, j, mode[4];
294         uint32_t cs;
295
296         KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
297             ("%s: TX/RX command sizes should be equal", __func__));
298         KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
299             ("%s: TX/RX data sizes should be equal", __func__));
300
301         /* get the proper chip select */
302         spibus_get_cs(child, &cs);
303
304         cs &= ~SPIBUS_CS_HIGH;
305
306         sc = device_get_softc(dev);
307         i = 0;
308
309         sx_xlock(&sc->xfer_mtx);
310
311         /*
312          * Disable transfers while we set things up.
313          */
314         WR4(sc, PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
315
316         /*
317          * PSCDEC = 0 has a range of 0..3 for chip select.  We
318          * don't support PSCDEC = 1 which has a range of 0..15.
319          */
320         if (cs > 3) {
321                 device_printf(dev,
322                     "Invalid chip select %d requested by %s\n", cs,
323                     device_get_nameunit(child));
324                 err = EINVAL;
325                 goto out;
326         }
327
328 #ifdef SPI_CHIP_SELECT_HIGH_SUPPORT
329         /*
330          * The AT91RM9200 couldn't do CS high for CS 0.  Other chips can, but we
331          * don't support that yet, or other spi modes.
332          */
333         if (at91_is_rm92() && cs == 0 &&
334             (cmd->flags & SPI_CHIP_SELECT_HIGH) != 0) {
335                 device_printf(dev,
336                     "Invalid chip select high requested by %s for cs 0.\n",
337                     device_get_nameunit(child));
338                 err = EINVAL;
339                 goto out;
340         }
341 #endif
342         err = (RD4(sc, SPI_MR) & ~0x000f0000) | CS_TO_MR(cs);
343         WR4(sc, SPI_MR, err);
344
345         /*
346          * Set up the TX side of the transfer.
347          */
348         if ((err = bus_dmamap_load(sc->dmatag, sc->map[i], cmd->tx_cmd,
349             cmd->tx_cmd_sz, at91_getaddr, &addr, 0)) != 0)
350                 goto out;
351         WR4(sc, PDC_TPR, addr);
352         WR4(sc, PDC_TCR, cmd->tx_cmd_sz);
353         bus_dmamap_sync(sc->dmatag, sc->map[i], BUS_DMASYNC_PREWRITE);
354         mode[i++] = BUS_DMASYNC_POSTWRITE;
355         if (cmd->tx_data_sz > 0) {
356                 if ((err = bus_dmamap_load(sc->dmatag, sc->map[i],
357                     cmd->tx_data, cmd->tx_data_sz, at91_getaddr, &addr, 0)) !=
358                     0)
359                         goto out;
360                 WR4(sc, PDC_TNPR, addr);
361                 WR4(sc, PDC_TNCR, cmd->tx_data_sz);
362                 bus_dmamap_sync(sc->dmatag, sc->map[i], BUS_DMASYNC_PREWRITE);
363                 mode[i++] = BUS_DMASYNC_POSTWRITE;
364         }
365
366         /*
367          * Set up the RX side of the transfer.
368          */
369         if ((err = bus_dmamap_load(sc->dmatag, sc->map[i], cmd->rx_cmd,
370             cmd->rx_cmd_sz, at91_getaddr, &addr, 0)) != 0)
371                 goto out;
372         WR4(sc, PDC_RPR, addr);
373         WR4(sc, PDC_RCR, cmd->rx_cmd_sz);
374         bus_dmamap_sync(sc->dmatag, sc->map[i], BUS_DMASYNC_PREREAD);
375         mode[i++] = BUS_DMASYNC_POSTREAD;
376         if (cmd->rx_data_sz > 0) {
377                 if ((err = bus_dmamap_load(sc->dmatag, sc->map[i],
378                     cmd->rx_data, cmd->rx_data_sz, at91_getaddr, &addr, 0)) !=
379                     0)
380                         goto out;
381                 WR4(sc, PDC_RNPR, addr);
382                 WR4(sc, PDC_RNCR, cmd->rx_data_sz);
383                 bus_dmamap_sync(sc->dmatag, sc->map[i], BUS_DMASYNC_PREREAD);
384                 mode[i++] = BUS_DMASYNC_POSTREAD;
385         }
386
387         /*
388          * Start the transfer, wait for it to complete.
389          */
390         sc->xfer_done = 0;
391         WR4(sc, SPI_IER, SPI_SR_RXBUFF);
392         WR4(sc, PDC_PTCR, PDC_PTCR_TXTEN | PDC_PTCR_RXTEN);
393         do
394                 err = tsleep(&sc->xfer_done, PCATCH | PZERO, "at91_spi", hz);
395         while (sc->xfer_done == 0 && err != EINTR);
396
397         /*
398          * Stop the transfer and clean things up.
399          */
400         WR4(sc, PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
401         if (err == 0)
402                 for (j = 0; j < i; j++)
403                         bus_dmamap_sync(sc->dmatag, sc->map[j], mode[j]);
404 out:
405         for (j = 0; j < i; j++)
406                 bus_dmamap_unload(sc->dmatag, sc->map[j]);
407
408         sx_xunlock(&sc->xfer_mtx);
409
410         return (err);
411 }
412
413 static void
414 at91_spi_intr(void *arg)
415 {
416         struct at91_spi_softc *sc;
417         uint32_t sr;
418
419         sc = (struct at91_spi_softc*)arg;
420
421         sr = RD4(sc, SPI_SR) & RD4(sc, SPI_IMR);
422         if ((sr & SPI_SR_RXBUFF) != 0) {
423                 sc->xfer_done = 1;
424                 WR4(sc, SPI_IDR, SPI_SR_RXBUFF);
425                 wakeup(&sc->xfer_done);
426         }
427         if ((sr & ~SPI_SR_RXBUFF) != 0) {
428                 device_printf(sc->dev, "Unexpected ISR %#x\n", sr);
429                 WR4(sc, SPI_IDR, sr & ~SPI_SR_RXBUFF);
430         }
431 }
432
433 static devclass_t at91_spi_devclass;
434
435 static device_method_t at91_spi_methods[] = {
436         /* Device interface */
437         DEVMETHOD(device_probe,         at91_spi_probe),
438         DEVMETHOD(device_attach,        at91_spi_attach),
439         DEVMETHOD(device_detach,        at91_spi_detach),
440
441         /* spibus interface */
442         DEVMETHOD(spibus_transfer,      at91_spi_transfer),
443
444         DEVMETHOD_END
445 };
446
447 static driver_t at91_spi_driver = {
448         "spi",
449         at91_spi_methods,
450         sizeof(struct at91_spi_softc),
451 };
452
453 #ifdef FDT
454 DRIVER_MODULE(at91_spi, simplebus, at91_spi_driver, at91_spi_devclass, NULL,
455     NULL);
456 #else
457 DRIVER_MODULE(at91_spi, atmelarm, at91_spi_driver, at91_spi_devclass, NULL,
458     NULL);
459 #endif