Fix fuctions to configure lli dmac.
[bertos.git] / bertos / cpu / cortex-m3 / drv / dmac_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 #include "dmac_sam3.h"
38 #include <drv/irq_cm3.h>
39
40 #include <cpu/irq.h>
41 #include <cpu/power.h>
42
43 #include <io/cm3.h>
44
45 #include <mware/event.h>
46
47 #include <string.h>
48
49 struct DmacCh
50 {
51         reg32_t *src;
52         reg32_t *dst;
53         reg32_t *desc;
54         reg32_t *cfg;
55         reg32_t *ctrla;
56         reg32_t *ctrlb;
57 };
58
59 #define DMAC_CHANNEL_CNT   5
60 struct DmacCh dmac_ch[] =
61 {
62         {
63                 .src = &DMAC_SADDR0,
64                 .dst = &DMAC_DADDR0,
65                 .desc = &DMAC_DSCR0,
66                 .cfg = &DMAC_CFG0,
67                 .ctrla = &DMAC_CTRLA0,
68                 .ctrlb = &DMAC_CTRLB0,
69         },
70         {
71                 .src = &DMAC_SADDR1,
72                 .dst = &DMAC_DADDR1,
73                 .desc = &DMAC_DSCR1,
74                 .cfg = &DMAC_CFG1,
75                 .ctrla = &DMAC_CTRLA1,
76                 .ctrlb = &DMAC_CTRLB1,
77         },
78         {
79                 .src = &DMAC_SADDR2,
80                 .dst = &DMAC_DADDR2,
81                 .desc = &DMAC_DSCR2,
82                 .cfg = &DMAC_CFG2,
83                 .ctrla = &DMAC_CTRLA2,
84                 .ctrlb = &DMAC_CTRLB2,
85         },
86         {
87                 .src = &DMAC_SADDR3,
88                 .dst = &DMAC_DADDR3,
89                 .desc = &DMAC_DSCR3,
90                 .cfg = &DMAC_CFG3,
91                 .ctrla = &DMAC_CTRLA3,
92                 .ctrlb = &DMAC_CTRLB3,
93         },
94         {
95                 .src = &DMAC_SADDR4,
96                 .dst = &DMAC_DADDR4,
97                 .desc = &DMAC_DSCR4,
98                 .cfg = &DMAC_CFG4,
99                 .ctrla = &DMAC_CTRLA4,
100                 .ctrlb = &DMAC_CTRLB4,
101         },
102         {
103                 .src = &DMAC_SADDR5,
104                 .dst = &DMAC_DADDR5,
105                 .desc = &DMAC_DSCR5,
106                 .cfg = &DMAC_CFG5,
107                 .ctrla = &DMAC_CTRLA5,
108                 .ctrlb = &DMAC_CTRLB5,
109         },
110 };
111
112
113 /* We use event to signal the end of conversion */
114 static Event data_ready;
115 static Dmac dmac[DMAC_CHANNEL_CNT];
116 static uint8_t dmac_ch_enabled;
117
118 void dmac_configureDmacLLI(int ch, DmacDesc *lli, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
119 {
120         DMAC_CHDR = BV(ch);
121
122         *dmac_ch[ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
123         lli->ctrla = ctrla | (transfer_size & DMAC_CTRLA_BTSIZE_MASK);
124         lli->ctrlb = ctrlb;// & ~BV(DMAC_CTRLB_IEN);
125         *dmac_ch[ch].desc = (uint32_t)lli;
126 }
127
128
129 void dmac_configureDmaCfgLLI(int ch, DmacDesc *lli, uint32_t cfg)
130 {
131         DMAC_CHDR = BV(ch);
132
133         *dmac_ch[ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
134         *dmac_ch[ch].desc = (uint32_t)lli;
135 }
136
137 void dmac_setSources(int ch, uint32_t src, uint32_t dst)
138 {
139         DMAC_CHDR = BV(ch);
140
141         *dmac_ch[ch].src = src;
142         *dmac_ch[ch].dst = dst;
143         *dmac_ch[ch].desc = 0;
144 }
145
146 void dmac_configureDmac(int ch, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
147 {
148         DMAC_CHDR = BV(ch);
149
150         *dmac_ch[ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT) | BV(DMAC_CFG_SOD);
151         *dmac_ch[ch].ctrla = ctrla | (transfer_size & DMAC_CTRLA_BTSIZE_MASK);
152         *dmac_ch[ch].ctrlb = ctrlb & ~BV(DMAC_CTRLB_IEN);
153 }
154
155 int dmac_start(int ch)
156 {
157         if (DMAC_CHSR & BV(ch))
158         {
159                 dmac[ch].errors |= DMAC_ERR_CH_ALREDY_ON;
160                 return -1;
161         }
162         DMAC_CHER = BV(ch);
163         return 0;
164 }
165
166 int dmac_stop(int ch)
167 {
168         DMAC_CHDR = BV(ch);
169         return 0;
170 }
171
172 bool dmac_isLLIDone(int ch)
173 {
174         return (DMAC_EBCIMR |= (BV(ch) << DMAC_EBCISR_CBTC0));
175 }
176
177 bool dmac_waitLLIDone(int ch)
178 {
179         while(!(DMAC_EBCIMR |= (BV(ch) << DMAC_EBCISR_CBTC0)))
180                 cpu_relax();
181
182         DMAC_CHDR = BV(ch);
183         return true;
184 }
185
186 bool dmac_isDone(int ch)
187 {
188         //event_wait(&data_ready);
189         return (*dmac_ch[ch].ctrla & BV(31));//(DMAC_CHSR |= (BV(dmac->ch) << DMAC_CHSR_EMPT0));
190 }
191
192 bool dmac_waitDone(int ch)
193 {
194         event_wait(&data_ready);
195         DMAC_CHDR = BV(ch);
196         return true;
197 }
198
199 int dmac_error(int ch)
200 {
201         uint32_t err = ((DMAC_EBCISR & 0x3F0000) | dmac[ch].errors);
202         dmac[ch].errors = 0;
203         return err;
204 }
205
206 static DECLARE_ISR(dmac_irq)
207 {
208         uint32_t status = DMAC_EBCISR;
209         uint32_t irq_ch = (status & (((dmac_ch_enabled |
210                                                                 (dmac_ch_enabled << DMAC_EBCIDR_ERR0) >> DMAC_EBCIDR_ERR0) |
211                                                                 (dmac_ch_enabled << DMAC_EBCIDR_CBTC0) >> DMAC_EBCIDR_CBTC0) & 0xFF));
212         if (irq_ch)
213                 for(int i = 0; i < 6; i++)
214                 {
215                         if (BV(i) & irq_ch)
216                                 if(dmac[i].handler)
217                                         dmac[i].handler(status);
218                 }
219 }
220
221 bool dmac_enableCh(int ch, dmac_handler_t handler)
222 {
223         ASSERT(ch <= DMAC_CHANNEL_CNT);
224
225         dmac_ch_enabled |= BV(ch);
226         if (handler)
227         {
228                 dmac[ch].handler = handler;
229                 DMAC_EBCIER |= (BV(ch) << DMAC_EBCIER_BTC0) | (BV(ch) << DMAC_EBCIDR_CBTC0) | (BV(ch) << DMAC_EBCIDR_ERR0);
230                 kprintf("Init dmac ch[%08lx]\n", DMAC_EBCIMR);
231         }
232
233         return true;
234 }
235
236 void dmac_init(void)
237 {
238         dmac_ch_enabled = 0;
239         memset(&dmac, 0, sizeof(dmac));
240
241         //init DMAC
242         DMAC_EBCIDR = 0x3FFFFF;
243         DMAC_CHDR = 0x1F;
244
245         pmc_periphEnable(DMAC_ID);
246         DMAC_EN = BV(DMAC_EN_ENABLE);
247
248         sysirq_setHandler(INT_DMAC, dmac_irq);
249 }