202b4f37259b5ce08df04dab6a714a2ee9d54934
[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 <string.h>
46
47 struct DmacCh
48 {
49         reg32_t *src;
50         reg32_t *dst;
51         reg32_t *desc;
52         reg32_t *cfg;
53         reg32_t *ctrla;
54         reg32_t *ctrlb;
55 };
56
57 #define DMAC_CHANNEL_CNT   5
58 struct DmacCh dmac_ch[] =
59 {
60         {
61                 .src = &DMAC_SADDR0,
62                 .dst = &DMAC_DADDR0,
63                 .desc = &DMAC_DSCR0,
64                 .cfg = &DMAC_CFG0,
65                 .ctrla = &DMAC_CTRLA0,
66                 .ctrlb = &DMAC_CTRLB0,
67         },
68         {
69                 .src = &DMAC_SADDR1,
70                 .dst = &DMAC_DADDR1,
71                 .desc = &DMAC_DSCR1,
72                 .cfg = &DMAC_CFG1,
73                 .ctrla = &DMAC_CTRLA1,
74                 .ctrlb = &DMAC_CTRLB1,
75         },
76         {
77                 .src = &DMAC_SADDR2,
78                 .dst = &DMAC_DADDR2,
79                 .desc = &DMAC_DSCR2,
80                 .cfg = &DMAC_CFG2,
81                 .ctrla = &DMAC_CTRLA2,
82                 .ctrlb = &DMAC_CTRLB2,
83         },
84         {
85                 .src = &DMAC_SADDR3,
86                 .dst = &DMAC_DADDR3,
87                 .desc = &DMAC_DSCR3,
88                 .cfg = &DMAC_CFG3,
89                 .ctrla = &DMAC_CTRLA3,
90                 .ctrlb = &DMAC_CTRLB3,
91         },
92         {
93                 .src = &DMAC_SADDR4,
94                 .dst = &DMAC_DADDR4,
95                 .desc = &DMAC_DSCR4,
96                 .cfg = &DMAC_CFG4,
97                 .ctrla = &DMAC_CTRLA4,
98                 .ctrlb = &DMAC_CTRLB4,
99         },
100         {
101                 .src = &DMAC_SADDR5,
102                 .dst = &DMAC_DADDR5,
103                 .desc = &DMAC_DSCR5,
104                 .cfg = &DMAC_CFG5,
105                 .ctrla = &DMAC_CTRLA5,
106                 .ctrlb = &DMAC_CTRLB5,
107         },
108 };
109
110 void dmac_setSourcesLLI(Dmac *dmac, DmacDesc *lli, uint32_t src, uint32_t dst, uint32_t desc)
111 {
112         ASSERT(lli);
113         DMAC_CHDR = BV(dmac->ch);
114
115         lli->src_addr = src;
116         lli->dst_addr = dst;
117         lli->dsc_addr = desc;
118 }
119
120 void dmac_configureDmacLLI(Dmac *dmac, DmacDesc *lli, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
121 {
122         DMAC_CHDR = BV(dmac->ch);
123
124         *dmac_ch[dmac->ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
125         lli->ctrla = ctrla | (transfer_size & DMAC_CTRLA_BTSIZE_MASK);
126         lli->ctrlb = ctrlb | BV(DMAC_CTRLB_IEN);
127         *dmac_ch[dmac->ch].desc = (uint32_t)lli;
128 }
129
130 void dmac_setSources(Dmac *dmac, uint32_t src, uint32_t dst)
131 {
132         DMAC_CHDR = BV(dmac->ch);
133
134         *dmac_ch[dmac->ch].src = src;
135         *dmac_ch[dmac->ch].dst = dst;
136         *dmac_ch[dmac->ch].desc = 0;
137 }
138
139 void dmac_configureDmac(Dmac *dmac, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
140 {
141         DMAC_CHDR = BV(dmac->ch);
142
143         *dmac_ch[dmac->ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
144         *dmac_ch[dmac->ch].ctrla = ctrla | (transfer_size & DMAC_CTRLA_BTSIZE_MASK);
145         *dmac_ch[dmac->ch].ctrlb = ctrlb | BV(DMAC_CTRLB_IEN);
146 }
147
148 int dmac_start(Dmac *dmac)
149 {
150         if (DMAC_CHSR & BV(dmac->ch))
151         {
152                 dmac->errors |= DMAC_ERR_CH_ALREDY_ON;
153                 return -1;
154         }
155         DMAC_CHER = BV(dmac->ch);
156         return 0;
157 }
158
159
160 bool dmac_isLLIDone(Dmac *dmac)
161 {
162         return (DMAC_EBCIMR |= (BV(dmac->ch) << DMAC_EBCISR_CBTC0));
163 }
164
165 bool dmac_waitLLIDone(Dmac *dmac)
166 {
167         while(!(DMAC_EBCIMR |= (BV(dmac->ch) << DMAC_EBCISR_CBTC0)))
168                 cpu_relax();
169
170         DMAC_CHDR = BV(dmac->ch);
171         return true;
172 }
173
174 bool dmac_isDone(Dmac *dmac)
175 {
176         return (DMAC_EBCIMR |= BV(dmac->ch));
177 }
178
179 bool dmac_waitDone(Dmac *dmac)
180 {
181         while(!(DMAC_EBCIMR |= BV(dmac->ch)))
182                 cpu_relax();
183
184         DMAC_CHDR = BV(dmac->ch);
185         return true;
186 }
187
188 int dmac_error(Dmac *dmac)
189 {
190         uint32_t err = ((DMAC_EBCISR & 0x3F0000) | dmac->errors);
191         dmac->errors = 0;
192         return err;
193 }
194
195 static DECLARE_ISR(dmac_irq)
196 {
197         uint32_t status = DMAC_EBCISR;
198         if(status & 0x3f3f)
199         {
200                 kputs("Ends\n");
201         }
202
203 }
204
205 void dmac_init(Dmac *dmac, int channel)
206 {
207         ASSERT(channel <= DMAC_CHANNEL_CNT);
208         memset(dmac, 0, sizeof(dmac));
209         dmac->ch = channel;
210         //init DMAC
211         DMAC_EBCIDR = 0x3FFFFF;
212         DMAC_CHDR = 0x1F;
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->ch) << DMAC_EBCIER_BTC0) | (BV(dmac->ch) << DMAC_EBCIDR_BTC0);
219 }