21045b7f89e47f3979a216bbe773cd127b632544
[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
39 #include "hsmci_sam3.h"
40 #include "hw/hw_sd.h"
41
42 #include <drv/timer.h>
43 #include <drv/irq_cm3.h>
44
45 #include <cpu/irq.h>
46 #include <cpu/power.h>
47
48 #include <io/cm3.h>
49
50 struct DmacCh
51 {
52         reg32_t *src;
53         reg32_t *dst;
54         reg32_t *desc;
55         reg32_t *cfg;
56         reg32_t *ctrla;
57         reg32_t *ctrlb;
58 };
59
60 #define DMAC_CHANNEL_CNT   5
61 struct DmacCh dmac_ch[] =
62 {
63         {
64                 .src = &DMAC_SADDR0,
65                 .dst = &DMAC_DADDR0,
66                 .desc = &DMAC_DSCR0,
67                 .cfg = &DMAC_CFG0,
68                 .ctrla = &DMAC_CTRLA0,
69                 .ctrlb = &DMAC_CTRLB0,
70         },
71         {
72                 .src = &DMAC_SADDR1,
73                 .dst = &DMAC_DADDR1,
74                 .desc = &DMAC_DSCR1,
75                 .cfg = &DMAC_CFG1,
76                 .ctrla = &DMAC_CTRLA1,
77                 .ctrlb = &DMAC_CTRLB1,
78         },
79         {
80                 .src = &DMAC_SADDR2,
81                 .dst = &DMAC_DADDR2,
82                 .desc = &DMAC_DSCR2,
83                 .cfg = &DMAC_CFG2,
84                 .ctrla = &DMAC_CTRLA2,
85                 .ctrlb = &DMAC_CTRLB2,
86         },
87         {
88                 .src = &DMAC_SADDR3,
89                 .dst = &DMAC_DADDR3,
90                 .desc = &DMAC_DSCR3,
91                 .cfg = &DMAC_CFG3,
92                 .ctrla = &DMAC_CTRLA3,
93                 .ctrlb = &DMAC_CTRLB3,
94         },
95         {
96                 .src = &DMAC_SADDR4,
97                 .dst = &DMAC_DADDR4,
98                 .desc = &DMAC_DSCR4,
99                 .cfg = &DMAC_CFG4,
100                 .ctrla = &DMAC_CTRLA4,
101                 .ctrlb = &DMAC_CTRLB4,
102         },
103         {
104                 .src = &DMAC_SADDR5,
105                 .dst = &DMAC_DADDR5,
106                 .desc = &DMAC_DSCR5,
107                 .cfg = &DMAC_CFG5,
108                 .ctrla = &DMAC_CTRLA5,
109                 .ctrlb = &DMAC_CTRLB5,
110         },
111 };
112
113 void dmac_setSources(Dmac *dmac, uint8_t ch, uint32_t src, uint32_t dst, size_t transfer_size)
114 {
115         ASSERT(ch <= DMAC_CHANNEL_CNT);
116
117         DMAC_CHDR = BV(ch);
118
119         *dmac_ch[ch].src = src;
120         *dmac_ch[ch].dst = dst;
121         *dmac_ch[ch].desc = 0;
122         dmac->transfer_size = transfer_size;
123 }
124
125 void dmac_configureDmac(Dmac *dmac, uint8_t ch, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
126 {
127         ASSERT(ch <= DMAC_CHANNEL_CNT);
128
129         DMAC_CHDR = BV(ch);
130
131         *dmac_ch[ch].cfg = cfg | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
132         *dmac_ch[ch].ctrla = ctrla | (dmac->transfer_size & DMAC_CTRLA_BTSIZE_MASK);
133         *dmac_ch[ch].ctrlb = ctrlb | BV(DMAC_CTRLB_IEN);
134 }
135
136 int dmac_start(Dmac *dmac, uint8_t ch)
137 {
138         ASSERT(ch <= DMAC_CHANNEL_CNT);
139
140         if (DMAC_CHSR & BV(ch))
141         {
142                 dmac->errors |= DMAC_ERR_CH_ALREDY_ON;
143                 return -1;
144         }
145
146         DMAC_CHER = BV(ch);
147         return 0;
148 }
149
150 bool dmac_isDone(Dmac *dmac, uint8_t ch)
151 {
152         (void)dmac;
153         return (DMAC_EBCISR |= BV(ch));
154 }
155
156 bool dmac_waitDone(Dmac *dmac, uint8_t ch)
157 {
158         (void)dmac;
159         while(!(DMAC_EBCISR |= BV(ch)))
160                 cpu_relax();
161
162         return true;
163 }
164
165 static DECLARE_ISR(dmac_irq)
166 {
167 }
168
169 void dmac_init(Dmac *dmac)
170 {
171         (void)dmac;
172
173         //init DMAC
174         DMAC_EBCIDR = 0x3FFFFF;
175         DMAC_CHDR = 0x1F;
176
177         pmc_periphEnable(DMAC_ID);
178         DMAC_EN = BV(DMAC_EN_ENABLE);
179         sysirq_setHandler(INT_DMAC, dmac_irq);
180 }