c4cbf2da70a37b288ddf9fa63ce484577d3c5e3a
[bertos.git] / bertos / cpu / arm / drv / spi_dma_at91.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief SPI driver with DMA.
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  * \author Luca Ottaviano <lottaviano@develer.com>
37  */
38
39 #include "cfg/cfg_spi_dma.h"
40
41 #include "spi_dma_at91.h"
42 #include "hw/hw_spi_dma.h"
43
44 #include <io/kfile.h>
45 #include <struct/fifobuf.h>
46 #include <struct/kfile_fifo.h>
47 #include <drv/timer.h>
48
49 #include <cpu/attr.h>
50 #include <cpu/power.h>
51
52 #include <string.h> /* memset */
53
54
55 void spi_dma_setclock(uint32_t rate)
56 {
57         SPI0_CSR0 &= ~SPI_SCBR;
58
59         ASSERT((uint8_t)DIV_ROUND(CPU_FREQ, rate));
60         SPI0_CSR0 |= DIV_ROUND(CPU_FREQ, rate) << SPI_SCBR_SHIFT;
61 }
62
63
64 static int spi_dma_flush(UNUSED_ARG(struct KFile *, fd))
65 {
66         /* Wait for DMA to finish */
67         while (!(SPI0_SR & BV(SPI_TXBUFE)))
68                 cpu_relax();
69
70         /* Wait until last bit has been shifted out */
71         while (!(SPI0_SR & BV(SPI_TXEMPTY)))
72                 cpu_relax();
73
74         return 0;
75 }
76
77 static size_t spi_dma_write(struct KFile *fd, const void *_buf, size_t size)
78 {
79         SPI0_PTCR = BV(PDC_TXTDIS);
80         SPI0_TPR = (reg32_t)_buf;
81         SPI0_TCR = size;
82         SPI0_PTCR = BV(PDC_TXTEN);
83         spi_dma_flush(fd);
84         return size;
85 }
86
87
88 /*
89  * Dummy buffer used to transmit 0xff chars while receiving data.
90  * This buffer is completetly constant and the compiler should allocate it
91  * in flash memory.
92  */
93 static const uint8_t tx_dummy_buf[CONFIG_SPI_DMA_MAX_RX] = { [0 ... (CONFIG_SPI_DMA_MAX_RX - 1)] = 0xFF };
94
95 static size_t spi_dma_read(UNUSED_ARG(struct KFile *, fd), void *_buf, size_t size)
96 {
97         size_t count, total_rx = 0;
98         uint8_t *buf = (uint8_t *)_buf;
99
100         while (size)
101         {
102                 count = MIN(size, (size_t)CONFIG_SPI_DMA_MAX_RX);
103
104                 SPI0_PTCR = BV(PDC_TXTDIS) | BV(PDC_RXTDIS);
105
106                 SPI0_RPR = (reg32_t)buf;
107                 SPI0_RCR = count;
108                 SPI0_TPR = (reg32_t)tx_dummy_buf;
109                 SPI0_TCR = count;
110
111                 /* Avoid reading the previous sent char */
112                 *buf = SPI0_RDR;
113
114                 /* Start transfer */
115                 SPI0_PTCR = BV(PDC_RXTEN) | BV(PDC_TXTEN);
116
117                 /* wait for transfer to finish */
118                 while (!(SPI0_SR & BV(SPI_ENDRX)))
119                         cpu_relax();
120
121                 size -= count;
122                 total_rx += count;
123                 buf += count;
124         }
125         SPI0_PTCR = BV(PDC_RXTDIS) | BV(PDC_TXTDIS);
126
127         return total_rx;
128 }
129
130 #define SPI_DMA_IRQ_PRIORITY 4
131
132 void spi_dma_init(SpiDmaAt91 *spi)
133 {
134         /* Disable PIO on SPI pins */
135         PIOA_PDR = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO);
136
137         /* Reset device */
138         SPI0_CR = BV(SPI_SWRST);
139
140         /*
141          * Set SPI to master mode, fixed peripheral select, chip select directly connected to a peripheral device,
142          * SPI clock set to MCK, mode fault detection disabled, loopback disable, NPCS0 active, Delay between CS = 0
143          */
144         SPI0_MR = BV(SPI_MSTR) | BV(SPI_MODFDIS);
145
146         /*
147          * Set SPI mode.
148          * At reset clock division factor is set to 0, that is
149          * *forbidden*. Set SPI clock to minimum to keep it valid.
150          */
151         SPI0_CSR0 = BV(SPI_NCPHA) | (255 << SPI_SCBR_SHIFT);
152
153         /* Disable all irqs */
154         SPI0_IDR = 0xFFFFFFFF;
155         /* Enable SPI clock. */
156         PMC_PCER = BV(SPI0_ID);
157
158         /* Enable SPI */
159         SPI0_CR = BV(SPI_SPIEN);
160
161         DB(spi->fd._type = KFT_SPIDMAAT91);
162         spi->fd.write = spi_dma_write;
163         spi->fd.read = spi_dma_read;
164         spi->fd.flush = spi_dma_flush;
165
166         SPI_DMA_STROBE_INIT();
167 }