4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
32 * \brief HSMCI driver implementation.
34 * \author Daniele Basile <asterix@develer.com>
37 #include "dmac_sam3.h"
38 #include <drv/irq_cm3.h>
41 #include <cpu/power.h>
57 #define DMAC_CHANNEL_CNT 5
58 struct DmacCh dmac_ch[] =
65 .ctrla = &DMAC_CTRLA0,
66 .ctrlb = &DMAC_CTRLB0,
73 .ctrla = &DMAC_CTRLA1,
74 .ctrlb = &DMAC_CTRLB1,
81 .ctrla = &DMAC_CTRLA2,
82 .ctrlb = &DMAC_CTRLB2,
89 .ctrla = &DMAC_CTRLA3,
90 .ctrlb = &DMAC_CTRLB3,
97 .ctrla = &DMAC_CTRLA4,
98 .ctrlb = &DMAC_CTRLB4,
105 .ctrla = &DMAC_CTRLA5,
106 .ctrlb = &DMAC_CTRLB5,
110 void dmac_setSourcesLLI(Dmac *dmac, DmacDesc *lli, uint32_t src, uint32_t dst, uint32_t desc)
113 DMAC_CHDR = BV(dmac->ch);
117 lli->dsc_addr = desc;
120 void dmac_configureDmacLLI(Dmac *dmac, DmacDesc *lli, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
122 DMAC_CHDR = BV(dmac->ch);
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;
130 void dmac_setSources(Dmac *dmac, uint32_t src, uint32_t dst)
132 DMAC_CHDR = BV(dmac->ch);
134 *dmac_ch[dmac->ch].src = src;
135 *dmac_ch[dmac->ch].dst = dst;
136 *dmac_ch[dmac->ch].desc = 0;
139 void dmac_configureDmac(Dmac *dmac, size_t transfer_size, uint32_t cfg, uint32_t ctrla, uint32_t ctrlb)
141 DMAC_CHDR = BV(dmac->ch);
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);
148 int dmac_start(Dmac *dmac)
150 if (DMAC_CHSR & BV(dmac->ch))
152 dmac->errors |= DMAC_ERR_CH_ALREDY_ON;
155 DMAC_CHER = BV(dmac->ch);
160 bool dmac_isLLIDone(Dmac *dmac)
162 return (DMAC_EBCIMR |= (BV(dmac->ch) << DMAC_EBCISR_CBTC0));
165 bool dmac_waitLLIDone(Dmac *dmac)
167 while(!(DMAC_EBCIMR |= (BV(dmac->ch) << DMAC_EBCISR_CBTC0)))
170 DMAC_CHDR = BV(dmac->ch);
174 bool dmac_isDone(Dmac *dmac)
176 return (DMAC_EBCIMR |= BV(dmac->ch));
179 bool dmac_waitDone(Dmac *dmac)
181 while(!(DMAC_EBCIMR |= BV(dmac->ch)))
184 DMAC_CHDR = BV(dmac->ch);
188 int dmac_error(Dmac *dmac)
190 uint32_t err = ((DMAC_EBCISR & 0x3F0000) | dmac->errors);
195 static DECLARE_ISR(dmac_irq)
197 uint32_t status = DMAC_EBCISR;
205 void dmac_init(Dmac *dmac, int channel)
207 ASSERT(channel <= DMAC_CHANNEL_CNT);
208 memset(dmac, 0, sizeof(dmac));
211 DMAC_EBCIDR = 0x3FFFFF;
214 pmc_periphEnable(DMAC_ID);
215 DMAC_EN = BV(DMAC_EN_ENABLE);
216 sysirq_setHandler(INT_DMAC, dmac_irq);
218 DMAC_EBCIER = (BV(dmac->ch) << DMAC_EBCIER_BTC0) | (BV(dmac->ch) << DMAC_EBCIDR_BTC0);