3cd4a4eaac2163a425ff53f54a69161dbc5c6734
[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
40 #include <drv/timer.h>
41 #include <cpu/irq.h>
42 #include <drv/irq_cm3.h>
43
44 #include <io/cm3.h>
45
46
47
48 #define HSMCI_INIT_SPEED  400000
49 #define HSMCI_CLK_DIV     ((CPU_FREQ / (HSMCI_INIT_SPEED << 1)) - 1)
50
51 #define HSMCI_ERROR_MASK   (BV(HSMCI_SR_RINDE)    | \
52                                                         BV(HSMCI_SR_RDIRE)    | \
53                                                         BV(HSMCI_SR_RCRCE)    | \
54                                                         BV(HSMCI_SR_RENDE)    | \
55                                                         BV(HSMCI_SR_RTOE)     | \
56                                                         BV(HSMCI_SR_DCRCE)    | \
57                                                         BV(HSMCI_SR_DTOE)     | \
58                                                         BV(HSMCI_SR_CSTOE)    | \
59                                                         BV(HSMCI_SR_BLKOVRE)  | \
60                                                         BV(HSMCI_SR_ACKRCVE))
61
62
63 #define HSMCI_RESP_ERROR_MASK   (BV(HSMCI_SR_RINDE) | BV(HSMCI_SR_RDIRE) \
64           | BV(HSMCI_SR_RENDE)| BV(HSMCI_SR_RTOE))
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 #define HSMCI_ERROR()   (HSMCI_SR & HSMCI_ERROR_MASK)
79
80 #define HSMCI_HW_INIT()  \
81 do { \
82         PIOA_PDR = BV(19) | BV(20) | BV(21) | BV(22) | BV(23) | BV(24); \
83         PIO_PERIPH_SEL(PIOA_BASE, BV(19) | BV(20) | BV(21) | BV(22) | BV(23) | BV(24), PIO_PERIPH_A); \
84 } while (0)
85
86
87 #define STROBE_ON()   PIOB_SODR = BV(13)
88 #define STROBE_OFF()  PIOB_CODR = BV(13)
89 #define STROBE_INIT() \
90         do { \
91                 PIOB_OER = BV(13); \
92                 PIOB_PER = BV(13); \
93         } while(0)
94
95 static DECLARE_ISR(hsmci_irq)
96 {
97         if (HSMCI_SR & BV(HSMCI_IER_RTOE))
98         {
99                 HSMCI_ARGR = 0;
100                 HSMCI_CMDR = 0 | HSMCI_CMDR_RSPTYP_NORESP | BV(HSMCI_CMDR_OPDCMD);
101         }
102 }
103
104 void hsmci_readResp(void *resp, size_t len)
105 {
106         ASSERT(resp);
107         uint32_t *r = (uint32_t *)resp;
108
109         for (size_t i = 0; i < len ; i++)
110                 r[i] = HSMCI_RSPR;
111 }
112
113 bool hsmci_sendCmd(uint8_t index, uint32_t argument, uint32_t reply_type)
114 {
115         STROBE_ON();
116         HSMCI_WAIT();
117
118         HSMCI_ARGR = argument;
119         HSMCI_CMDR = index | reply_type | BV(HSMCI_CMDR_MAXLAT) | BV(HSMCI_CMDR_OPDCMD);
120
121         uint32_t status = HSMCI_SR;
122         while (!(status & BV(HSMCI_SR_CMDRDY)))
123         {
124                 if (status & HSMCI_RESP_ERROR_MASK)
125                         return status;
126
127                 cpu_relax();
128
129                 status = HSMCI_SR;
130         }
131
132         STROBE_OFF();
133         return 0;
134 }
135
136 void hsmci_setBlkSize(size_t blk_size)
137 {
138         HSMCI_DMA = BV(HSMCI_DMA_DMAEN);
139         HSMCI_BLKR = (blk_size << HSMCI_BLKR_BLKLEN_SHIFT);
140 }
141
142 bool hsmci_read(uint32_t *buf, size_t word_num)
143 {
144         ASSERT(buf);
145         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
146
147         kprintf("DMAC status %08lx channel st %08lx\n", DMAC_EBCISR, DMAC_CHSR);
148
149         DMAC_SADDR0 = 0x40000200U;
150         DMAC_DADDR0 = (uint32_t)buf;
151         DMAC_DSCR0 = 0;
152
153         DMAC_CTRLA0 = word_num | DMAC_CTRLA_SRC_WIDTH_WORD | DMAC_CTRLA_DST_WIDTH_WORD;
154         DMAC_CTRLB0 = (BV(DMAC_CTRLB_SRC_DSCR) | DMAC_CTRLB_FC_PER2MEM_DMA_FC |
155                                         DMAC_CTRLB_SRC_INCR_FIXED | DMAC_CTRLB_DST_INCR_INCREMENTING | BV(DMAC_CTRLB_IEN));
156
157         ASSERT(!(DMAC_CHSR & BV(DMAC_CHSR_ENA0)));
158         DMAC_CHER = BV(DMAC_CHER_ENA0);
159
160         while (!(HSMCI_SR & BV(HSMCI_SR_XFRDONE)))
161                 cpu_relax();
162
163         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
164         return 0;
165 }
166
167 void hsmci_init(Hsmci *hsmci)
168 {
169         (void)hsmci;
170
171         HSMCI_HW_INIT();
172         STROBE_INIT();
173
174         pmc_periphEnable(HSMCI_ID);
175         HSMCI_CR = BV(HSMCI_CR_SWRST);
176         HSMCI_CR = BV(HSMCI_CR_PWSDIS) | BV(HSMCI_CR_MCIDIS);
177         HSMCI_IDR = 0xFFFFFFFF;
178
179         HSMCI_DTOR = 0xFF | HSMCI_DTOR_DTOMUL_1048576;
180         HSMCI_CSTOR = 0xFF | HSMCI_CSTOR_CSTOMUL_1048576;
181         HSMCI_MR = HSMCI_CLK_DIV | ((0x7u << HSMCI_MR_PWSDIV_SHIFT) & HSMCI_MR_PWSDIV_MASK) | BV(HSMCI_MR_RDPROOF);
182         HSMCI_SDCR = 0;
183         HSMCI_CFG = BV(HSMCI_CFG_FIFOMODE) | BV(HSMCI_CFG_FERRCTRL);
184
185         sysirq_setHandler(INT_HSMCI, hsmci_irq);
186         HSMCI_CR = BV(HSMCI_CR_MCIEN);
187         HSMCI_DMA &= ~BV(HSMCI_DMA_DMAEN);
188
189         //init DMAC
190         DMAC_EBCIDR = 0x3FFFFF;
191         DMAC_CHDR = BV(DMAC_CHDR_DIS0);
192
193         DMAC_CFG0 = 0;
194         DMAC_CFG0 = BV(DMAC_CFG_SRC_H2SEL) | DMAC_CFG_FIFOCFG_ALAP_CFG | BV(DMAC_CFG_SOD);
195
196         pmc_periphEnable(DMAC_ID);
197         DMAC_EN = BV(DMAC_EN_ENABLE);
198
199         //HSMCI_IER = BV(HSMCI_IER_RTOE);
200 }