The dmac trigger is differnt for rx and tx.
[bertos.git] / bertos / cpu / cortex-m3 / drv / hsmci_sam3.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 2011 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief HSMCI driver implementation.
33  *
34  * \author Daniele Basile <asterix@develer.com>
35  */
36
37
38 #include "hsmci_sam3.h"
39
40 #include <drv/timer.h>
41 #include <cpu/irq.h>
42 #include <drv/irq_cm3.h>
43
44 #include <io/cm3.h>
45
46 /** DMA Transfer Descriptor as well as Linked List Item */
47 typedef struct DmacDesc
48 {
49     uint32_t src_addr;     /**< Source buffer address */
50     uint32_t dst_addr;     /**< Destination buffer address */
51     uint32_t ctrl_a;       /**< Control A register settings */
52     uint32_t ctrl_b;       /**< Control B register settings */
53     uint32_t dsc_addr;     /**< Next descriptor address */
54 } DmacDesc;
55
56
57
58
59 #define HSMCI_CLK_DIV(RATE)     ((CPU_FREQ / (RATE << 1)) - 1)
60
61 #define HSMCI_ERROR_MASK   (BV(HSMCI_SR_RINDE)    | \
62                                                         BV(HSMCI_SR_RDIRE)    | \
63                                                         BV(HSMCI_SR_RCRCE)    | \
64                                                         BV(HSMCI_SR_RENDE)    | \
65                                                         BV(HSMCI_SR_RTOE)     | \
66                                                         BV(HSMCI_SR_DCRCE)    | \
67                                                         BV(HSMCI_SR_DTOE)     | \
68                                                         BV(HSMCI_SR_CSTOE)    | \
69                                                         BV(HSMCI_SR_BLKOVRE)  | \
70                                                         BV(HSMCI_SR_ACKRCVE))
71
72
73 #define HSMCI_RESP_ERROR_MASK   (BV(HSMCI_SR_RINDE) | BV(HSMCI_SR_RDIRE) \
74           | BV(HSMCI_SR_RENDE)| BV(HSMCI_SR_RTOE))
75
76 #define HSMCI_DATA_ERROR_MASK   (BV(HSMCI_SR_DCRCE) | BV(HSMCI_SR_DTOE))
77
78 #define HSMCI_READY_MASK     (BV(HSMCI_SR_CMDRDY) | BV(HSMCI_SR_NOTBUSY))
79 #define HSMCI_WAIT()\
80         do { \
81                 cpu_relax(); \
82         } while (!(HSMCI_SR & BV(HSMCI_SR_CMDRDY)))
83
84
85 #define HSMCI_WAIT_DATA_RDY()\
86         do { \
87                 cpu_relax(); \
88         } while (!(HSMCI_SR & BV(HSMCI_SR_RXRDY)))
89
90 #define HSMCI_ERROR()   (HSMCI_SR & HSMCI_ERROR_MASK)
91
92 #define HSMCI_HW_INIT()  \
93 do { \
94         PIOA_PDR = BV(19) | BV(20) | BV(21) | BV(22) | BV(23) | BV(24); \
95         PIO_PERIPH_SEL(PIOA_BASE, BV(19) | BV(20) | BV(21) | BV(22) | BV(23) | BV(24), PIO_PERIPH_A); \
96 } while (0)
97
98
99 #define STROBE_ON()   PIOB_SODR = BV(13)
100 #define STROBE_OFF()  PIOB_CODR = BV(13)
101 #define STROBE_INIT() \
102         do { \
103                 PIOB_OER = BV(13); \
104                 PIOB_PER = BV(13); \
105         } while(0)
106
107 static DECLARE_ISR(hsmci_irq)
108 {
109         uint32_t status = HSMCI_SR;
110         if (status & BV(HSMCI_IER_DMADONE))
111         {
112                 kputs("\n\nfatto\n\n");
113         }
114 }
115
116
117 static DECLARE_ISR(dmac_irq)
118 {
119         uint32_t stat = DMAC_EBCISR;
120
121         if (stat & BV(DMAC_EBCISR_ERR3))
122         {
123                 kprintf("err %08lx\n", stat);
124         }
125 }
126
127 void hsmci_readResp(uint32_t *resp, size_t len)
128 {
129         ASSERT(resp);
130
131         for (size_t i = 0; i < len ; i++)
132                 resp[i] = HSMCI_RSPR;
133 }
134
135 bool hsmci_sendCmd(uint8_t index, uint32_t argument, uint32_t reply_type)
136 {
137         STROBE_ON();
138         HSMCI_WAIT();
139
140         HSMCI_ARGR = argument;
141         HSMCI_CMDR = index | reply_type | BV(HSMCI_CMDR_MAXLAT);// | BV(HSMCI_CMDR_OPDCMD);
142
143         uint32_t status = HSMCI_SR;
144         while (!(status & BV(HSMCI_SR_CMDRDY)))
145         {
146                 if (status & HSMCI_RESP_ERROR_MASK)
147                         return status;
148
149                 cpu_relax();
150
151                 status = HSMCI_SR;
152         }
153
154         STROBE_OFF();
155         return 0;
156 }
157
158 INLINE void hsmci_setBlockSize(size_t blk_size)
159 {
160         HSMCI_IER = BV(HSMCI_IER_DMADONE);
161         HSMCI_DMA |= BV(HSMCI_DMA_DMAEN);
162         HSMCI_BLKR = blk_size << HSMCI_BLKR_BLKLEN_SHIFT;
163 }
164
165 void hsmci_prgTxDMA(uint32_t *buf, size_t word_num, size_t blk_size)
166 {
167
168         hsmci_setBlockSize(blk_size);
169
170         //init DMAC
171         DMAC_EBCIDR = 0x3FFFFF;
172         DMAC_CHDR = 0x1F;
173         DMAC_CFG0 = BV(DMAC_CFG_DST_H2SEL) | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
174
175         pmc_periphEnable(DMAC_ID);
176         DMAC_EN = BV(DMAC_EN_ENABLE);
177         sysirq_setHandler(INT_DMAC, dmac_irq);
178
179         DMAC_EBCIER = BV(DMAC_EBCIER_BTC0) | BV(DMAC_EBCIER_ERR0);
180
181
182         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
183
184         DMAC_SADDR0 = (uint32_t)buf;
185         DMAC_DADDR0 = (uint32_t)&HSMCI_TDR;
186         DMAC_DSCR0 = 0;
187
188         DMAC_CTRLA0 = (word_num & DMAC_CTRLA_BTSIZE_MASK) |
189                 DMAC_CTRLA_SRC_WIDTH_WORD | DMAC_CTRLA_DST_WIDTH_WORD;
190         DMAC_CTRLB0 = (BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) | DMAC_CTRLB_FC_MEM2PER_DMA_FC |
191                                         DMAC_CTRLB_DST_INCR_FIXED | DMAC_CTRLB_SRC_INCR_INCREMENTING | BV(DMAC_CTRLB_IEN));
192
193         kprintf("SDDR %08lx\n", DMAC_SADDR0);
194         kprintf("DDDR %08lx\n", DMAC_DADDR0);
195         kprintf("CTRA %08lx\n", DMAC_CTRLA0);
196         kprintf("CTRB %08lx\n", DMAC_CTRLB0);
197         kprintf("EBCI %08lx\n", DMAC_EBCISR);
198         kprintf("CHSR %08lx\n", DMAC_CHSR);
199
200         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
201         DMAC_CHER = BV(DMAC_CHER_ENA0);
202
203 }
204
205 void hsmci_prgRxDMA(uint32_t *buf, size_t word_num, size_t blk_size)
206 {
207         hsmci_setBlockSize(blk_size);
208
209         //init DMAC
210         DMAC_EBCIDR = 0x3FFFFF;
211         DMAC_CHDR = 0x1F;
212         DMAC_CFG0 = BV(DMAC_CFG_SRC_H2SEL) | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
213
214         pmc_periphEnable(DMAC_ID);
215         DMAC_EN = BV(DMAC_EN_ENABLE);
216         sysirq_setHandler(INT_DMAC, dmac_irq);
217
218         DMAC_EBCIER = BV(DMAC_EBCIER_BTC0) | BV(DMAC_EBCIER_ERR0);
219
220         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
221
222         DMAC_SADDR0 = (uint32_t)&HSMCI_RDR;
223         DMAC_DADDR0 = (uint32_t)buf;
224         DMAC_DSCR0 = 0;
225
226         DMAC_CTRLA0 = (word_num & DMAC_CTRLA_BTSIZE_MASK) |
227                 DMAC_CTRLA_SRC_WIDTH_WORD | DMAC_CTRLA_DST_WIDTH_WORD;
228         DMAC_CTRLB0 = (BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) | DMAC_CTRLB_FC_PER2MEM_DMA_FC |
229                                         DMAC_CTRLB_DST_INCR_INCREMENTING | DMAC_CTRLB_SRC_INCR_FIXED | BV(DMAC_CTRLB_IEN));
230
231         kprintf("SDDR %08lx\n", DMAC_SADDR0);
232         kprintf("DDDR %08lx\n", DMAC_DADDR0);
233         kprintf("CTRA %08lx\n", DMAC_CTRLA0);
234         kprintf("CTRB %08lx\n", DMAC_CTRLB0);
235         kprintf("EBCI %08lx\n", DMAC_EBCISR);
236         kprintf("CHSR %08lx\n", DMAC_CHSR);
237
238         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
239         DMAC_CHER = BV(DMAC_CHER_ENA0);
240 }
241
242
243 void hsmci_waitTransfer(void)
244 {
245         while (!(HSMCI_SR & BV(HSMCI_SR_XFRDONE)))
246                 cpu_relax();
247 }
248
249 void hsmci_setSpeed(uint32_t data_rate, int flag)
250 {
251         if (flag)
252                 HSMCI_CFG |= BV(HSMCI_CFG_HSMODE);
253         else
254                 HSMCI_CFG &= ~BV(HSMCI_CFG_HSMODE);
255
256         HSMCI_MR = HSMCI_CLK_DIV(data_rate) | ((0x7u << HSMCI_MR_PWSDIV_SHIFT) & HSMCI_MR_PWSDIV_MASK);
257
258         timer_delay(10);
259 }
260
261 void hsmci_init(Hsmci *hsmci)
262 {
263         (void)hsmci;
264
265         HSMCI_HW_INIT();
266         STROBE_INIT();
267
268         pmc_periphEnable(HSMCI_ID);
269         HSMCI_CR = BV(HSMCI_CR_SWRST);
270         HSMCI_CR = BV(HSMCI_CR_PWSDIS) | BV(HSMCI_CR_MCIDIS);
271         HSMCI_IDR = 0xFFFFFFFF;
272
273         HSMCI_DTOR = 0xFF | HSMCI_DTOR_DTOMUL_1048576;
274         HSMCI_CSTOR = 0xFF | HSMCI_CSTOR_CSTOMUL_1048576;
275         HSMCI_MR = HSMCI_CLK_DIV(HSMCI_INIT_SPEED) | ((0x7u << HSMCI_MR_PWSDIV_SHIFT) & HSMCI_MR_PWSDIV_MASK) | BV(HSMCI_MR_RDPROOF);
276         HSMCI_CFG = BV(HSMCI_CFG_FIFOMODE) | BV(HSMCI_CFG_FERRCTRL);
277
278         sysirq_setHandler(INT_HSMCI, hsmci_irq);
279         HSMCI_CR = BV(HSMCI_CR_MCIEN);
280         HSMCI_DMA = 0;
281
282 }