Move all pins init into sd hw file.
[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 #include "hw/hw_sd.h"
40
41 #include <drv/timer.h>
42 #include <drv/irq_cm3.h>
43
44 #include <cpu/irq.h>
45
46 #include <io/cm3.h>
47
48 /** DMA Transfer Descriptor as well as Linked List Item */
49 typedef struct DmacDesc
50 {
51     uint32_t src_addr;     /**< Source buffer address */
52     uint32_t dst_addr;     /**< Destination buffer address */
53     uint32_t ctrl_a;       /**< Control A register settings */
54     uint32_t ctrl_b;       /**< Control B register settings */
55     uint32_t dsc_addr;     /**< Next descriptor address */
56 } DmacDesc;
57
58 #define HSMCI_CLK_DIV(RATE)     ((CPU_FREQ / (RATE << 1)) - 1)
59
60
61 #define HSMCI_RESP_ERROR_MASK   (BV(HSMCI_SR_RINDE) | BV(HSMCI_SR_RDIRE) \
62           | BV(HSMCI_SR_RENDE)| BV(HSMCI_SR_RTOE))
63
64 #define HSMCI_DATA_ERROR_MASK   (BV(HSMCI_SR_DCRCE) | BV(HSMCI_SR_DTOE))
65
66 #define HSMCI_READY_MASK     (BV(HSMCI_SR_CMDRDY) | BV(HSMCI_SR_NOTBUSY))
67 #define HSMCI_WAIT()\
68         do { \
69                 cpu_relax(); \
70         } while (!(HSMCI_SR & BV(HSMCI_SR_CMDRDY)))
71
72
73 #define HSMCI_WAIT_DATA_RDY()\
74         do { \
75                 cpu_relax(); \
76         } while (!(HSMCI_SR & BV(HSMCI_SR_RXRDY)))
77
78 static DECLARE_ISR(hsmci_irq)
79 {
80         uint32_t status = HSMCI_SR;
81         if (status & BV(HSMCI_IER_DMADONE))
82         {
83         }
84 }
85
86 static DECLARE_ISR(dmac_irq)
87 {
88         uint32_t stat = DMAC_EBCISR;
89
90         if (stat & BV(DMAC_EBCISR_ERR3))
91         {
92                 kprintf("err %08lx\n", stat);
93         }
94 }
95
96 void hsmci_readResp(uint32_t *resp, size_t len)
97 {
98         ASSERT(resp);
99
100         for (size_t i = 0; i < len ; i++)
101                 resp[i] = HSMCI_RSPR;
102 }
103
104 bool hsmci_sendCmd(uint8_t index, uint32_t argument, uint32_t reply_type)
105 {
106         HSMCI_WAIT();
107
108         HSMCI_ARGR = argument;
109         HSMCI_CMDR = index | reply_type | BV(HSMCI_CMDR_MAXLAT);
110
111         uint32_t status = HSMCI_SR;
112         while (!(status & BV(HSMCI_SR_CMDRDY)))
113         {
114                 if (status & HSMCI_RESP_ERROR_MASK)
115                         return status;
116
117                 cpu_relax();
118
119                 status = HSMCI_SR;
120         }
121
122         return 0;
123 }
124
125 INLINE void hsmci_setBlockSize(size_t blk_size)
126 {
127         HSMCI_DMA |= BV(HSMCI_DMA_DMAEN);
128         HSMCI_BLKR = blk_size << HSMCI_BLKR_BLKLEN_SHIFT;
129 }
130
131 void hsmci_prgTxDMA(const uint32_t *buf, size_t word_num, size_t blk_size)
132 {
133
134         hsmci_setBlockSize(blk_size);
135
136         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
137
138         DMAC_SADDR0 = (uint32_t)buf;
139         DMAC_DADDR0 = (uint32_t)&HSMCI_TDR;
140         DMAC_DSCR0 = 0;
141
142         DMAC_CFG0 = BV(DMAC_CFG_DST_H2SEL) | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
143         DMAC_CTRLA0 = (word_num & DMAC_CTRLA_BTSIZE_MASK) |
144                 DMAC_CTRLA_SRC_WIDTH_WORD | DMAC_CTRLA_DST_WIDTH_WORD;
145         DMAC_CTRLB0 = (BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) | DMAC_CTRLB_FC_MEM2PER_DMA_FC |
146                                         DMAC_CTRLB_DST_INCR_FIXED | DMAC_CTRLB_SRC_INCR_INCREMENTING | BV(DMAC_CTRLB_IEN));
147
148         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
149         DMAC_CHER = BV(DMAC_CHER_ENA0);
150
151 }
152
153 void hsmci_prgRxDMA(uint32_t *buf, size_t word_num, size_t blk_size)
154 {
155         hsmci_setBlockSize(blk_size);
156
157         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
158
159         DMAC_SADDR0 = (uint32_t)&HSMCI_RDR;
160         DMAC_DADDR0 = (uint32_t)buf;
161         DMAC_DSCR0 = 0;
162
163         DMAC_CFG0 = BV(DMAC_CFG_SRC_H2SEL) | DMAC_CFG_FIFOCFG_ALAP_CFG | (0x1 << DMAC_CFG_AHB_PROT_SHIFT);
164         DMAC_CTRLA0 = (word_num & DMAC_CTRLA_BTSIZE_MASK) |
165                 DMAC_CTRLA_SRC_WIDTH_WORD | DMAC_CTRLA_DST_WIDTH_WORD;
166         DMAC_CTRLB0 = (BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) | DMAC_CTRLB_FC_PER2MEM_DMA_FC |
167                                         DMAC_CTRLB_DST_INCR_INCREMENTING | DMAC_CTRLB_SRC_INCR_FIXED | BV(DMAC_CTRLB_IEN));
168
169         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
170         DMAC_CHER = BV(DMAC_CHER_ENA0);
171 }
172
173
174 void hsmci_waitTransfer(void)
175 {
176         while (!(HSMCI_SR & BV(HSMCI_SR_XFRDONE)))
177                 cpu_relax();
178 }
179
180 void hsmci_setSpeed(uint32_t data_rate, int flag)
181 {
182         if (flag)
183                 HSMCI_CFG |= BV(HSMCI_CFG_HSMODE);
184         else
185                 HSMCI_CFG &= ~BV(HSMCI_CFG_HSMODE);
186
187         HSMCI_MR = HSMCI_CLK_DIV(data_rate) | ((0x7u << HSMCI_MR_PWSDIV_SHIFT) & HSMCI_MR_PWSDIV_MASK);
188
189         timer_delay(10);
190 }
191
192 void hsmci_init(Hsmci *hsmci)
193 {
194         (void)hsmci;
195
196         SD_PIN_INIT();
197
198         pmc_periphEnable(HSMCI_ID);
199         HSMCI_CR = BV(HSMCI_CR_SWRST);
200         HSMCI_CR = BV(HSMCI_CR_PWSDIS) | BV(HSMCI_CR_MCIDIS);
201         HSMCI_IDR = 0xFFFFFFFF;
202
203         HSMCI_DTOR = 0xFF | HSMCI_DTOR_DTOMUL_1048576;
204         HSMCI_CSTOR = 0xFF | HSMCI_CSTOR_CSTOMUL_1048576;
205         HSMCI_MR = HSMCI_CLK_DIV(HSMCI_INIT_SPEED) | ((0x7u << HSMCI_MR_PWSDIV_SHIFT) & HSMCI_MR_PWSDIV_MASK) | BV(HSMCI_MR_RDPROOF);
206         HSMCI_CFG = BV(HSMCI_CFG_FIFOMODE) | BV(HSMCI_CFG_FERRCTRL);
207
208         sysirq_setHandler(INT_HSMCI, hsmci_irq);
209         HSMCI_CR = BV(HSMCI_CR_MCIEN);
210         HSMCI_DMA = 0;
211
212         //init DMAC
213         DMAC_EBCIDR = 0x3FFFFF;
214         DMAC_CHDR = 0x1F;
215
216         pmc_periphEnable(DMAC_ID);
217         DMAC_EN = BV(DMAC_EN_ENABLE);
218         sysirq_setHandler(INT_DMAC, dmac_irq);
219
220         DMAC_EBCIER = BV(DMAC_EBCIER_BTC0) | BV(DMAC_EBCIER_ERR0);
221 }