1 |
580 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
//------------------------------------------------------------------------------
|
31 |
|
|
// Headers
|
32 |
|
|
//------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
#include "mci.h"
|
35 |
|
|
#include <utility/assert.h>
|
36 |
|
|
#include <utility/trace.h>
|
37 |
|
|
|
38 |
|
|
//------------------------------------------------------------------------------
|
39 |
|
|
// Local constants
|
40 |
|
|
//------------------------------------------------------------------------------
|
41 |
|
|
|
42 |
|
|
/// Bit mask for status register errors.
|
43 |
|
|
#define STATUS_ERRORS (AT91C_MCI_UNRE \
|
44 |
|
|
| AT91C_MCI_OVRE \
|
45 |
|
|
| AT91C_MCI_DTOE \
|
46 |
|
|
| AT91C_MCI_DCRCE \
|
47 |
|
|
| AT91C_MCI_RTOE \
|
48 |
|
|
| AT91C_MCI_RENDE \
|
49 |
|
|
| AT91C_MCI_RCRCE \
|
50 |
|
|
| AT91C_MCI_RDIRE \
|
51 |
|
|
| AT91C_MCI_RINDE)
|
52 |
|
|
|
53 |
|
|
/// MCI data timeout configuration with 1048576 MCK cycles between 2 data transfers.
|
54 |
|
|
#define DTOR_1MEGA_CYCLES (AT91C_MCI_DTOCYC | AT91C_MCI_DTOMUL)
|
55 |
|
|
|
56 |
|
|
/// MCI MR: disable MCI Clock when FIFO is full
|
57 |
|
|
#ifndef AT91C_MCI_WRPROOF
|
58 |
|
|
#define AT91C_MCI_WRPROOF 0
|
59 |
|
|
#endif
|
60 |
|
|
#ifndef AT91C_MCI_RDPROOF
|
61 |
|
|
#define AT91C_MCI_RDPROOF 0
|
62 |
|
|
#endif
|
63 |
|
|
|
64 |
|
|
#define SDCARD_APP_OP_COND_CMD (41 | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_48 | AT91C_MCI_TRCMD_NO )
|
65 |
|
|
#define MMC_SEND_OP_COND_CMD (1 | AT91C_MCI_TRCMD_NO | AT91C_MCI_SPCMD_NONE | AT91C_MCI_RSPTYP_48 | AT91C_MCI_OPDCMD)
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
#define DISABLE 0 // Disable MCI interface
|
69 |
|
|
#define ENABLE 1 // Enable MCI interface
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
//------------------------------------------------------------------------------
|
73 |
|
|
// Local macros
|
74 |
|
|
//------------------------------------------------------------------------------
|
75 |
|
|
|
76 |
|
|
/// Used to write in PMC registers.
|
77 |
|
|
#define WRITE_PMC(pPmc, regName, value) pPmc->regName = (value)
|
78 |
|
|
|
79 |
|
|
/// Used to write in MCI registers.
|
80 |
|
|
#define WRITE_MCI(pMci, regName, value) pMci->regName = (value)
|
81 |
|
|
|
82 |
|
|
/// Used to read from MCI registers.
|
83 |
|
|
#define READ_MCI(pMci, regName) (pMci->regName)
|
84 |
|
|
|
85 |
|
|
//------------------------------------------------------------------------------
|
86 |
|
|
// Global functions
|
87 |
|
|
//------------------------------------------------------------------------------
|
88 |
|
|
|
89 |
|
|
//------------------------------------------------------------------------------
|
90 |
|
|
/// Enable/disable a MCI driver instance.
|
91 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
92 |
|
|
/// \param enb 0 for disable MCI and 1 for enable MCI.
|
93 |
|
|
//------------------------------------------------------------------------------
|
94 |
|
|
void MCI_Enable(Mci *pMci, unsigned char enb)
|
95 |
|
|
{
|
96 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
97 |
|
|
|
98 |
|
|
SANITY_CHECK(pMci);
|
99 |
|
|
SANITY_CHECK(pMci->pMciHw);
|
100 |
|
|
|
101 |
|
|
// Set the Control Register: Enable/Disable MCI interface clock
|
102 |
|
|
if(enb == DISABLE) {
|
103 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIDIS);
|
104 |
|
|
}
|
105 |
|
|
else {
|
106 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIEN);
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
//------------------------------------------------------------------------------
|
111 |
|
|
/// Initializes a MCI driver instance and the underlying peripheral.
|
112 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
113 |
|
|
/// \param pMciHw Pointer to a MCI peripheral.
|
114 |
|
|
/// \param mciId MCI peripheral identifier.
|
115 |
|
|
/// \param mode Slot and type of connected card.
|
116 |
|
|
//------------------------------------------------------------------------------
|
117 |
|
|
void MCI_Init(
|
118 |
|
|
Mci *pMci,
|
119 |
|
|
AT91S_MCI *pMciHw,
|
120 |
|
|
unsigned char mciId,
|
121 |
|
|
unsigned int mode)
|
122 |
|
|
{
|
123 |
|
|
unsigned short clkDiv;
|
124 |
|
|
|
125 |
|
|
SANITY_CHECK(pMci);
|
126 |
|
|
SANITY_CHECK(pMciHw);
|
127 |
|
|
SANITY_CHECK((mode == MCI_MMC_SLOTA) || (mode == MCI_MMC_SLOTB)
|
128 |
|
|
|| (mode == MCI_SD_SLOTA) || (mode == MCI_SD_SLOTB));
|
129 |
|
|
|
130 |
|
|
// Initialize the MCI driver structure
|
131 |
|
|
pMci->pMciHw = pMciHw;
|
132 |
|
|
pMci->mciId = mciId;
|
133 |
|
|
pMci->semaphore = 1;
|
134 |
|
|
pMci->pCommand = 0;
|
135 |
|
|
|
136 |
|
|
// Enable the MCI clock
|
137 |
|
|
WRITE_PMC(AT91C_BASE_PMC, PMC_PCER, (1 << mciId));
|
138 |
|
|
|
139 |
|
|
// Reset the MCI
|
140 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_SWRST);
|
141 |
|
|
|
142 |
|
|
// Disable the MCI
|
143 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIDIS | AT91C_MCI_PWSDIS);
|
144 |
|
|
|
145 |
|
|
// Disable all the interrupts
|
146 |
|
|
WRITE_MCI(pMciHw, MCI_IDR, 0xFFFFFFFF);
|
147 |
|
|
|
148 |
|
|
// Set the Data Timeout Register
|
149 |
|
|
WRITE_MCI(pMciHw, MCI_DTOR, DTOR_1MEGA_CYCLES);
|
150 |
|
|
|
151 |
|
|
// Set the Mode Register: 400KHz for MCK = 48MHz (CLKDIV = 58)
|
152 |
|
|
clkDiv = (BOARD_MCK / (400000 * 2)) - 1;
|
153 |
|
|
WRITE_MCI(pMciHw, MCI_MR, (clkDiv | (AT91C_MCI_PWSDIV & (0x7 << 8))));
|
154 |
|
|
|
155 |
|
|
// Set the SDCard Register
|
156 |
|
|
WRITE_MCI(pMciHw, MCI_SDCR, mode);
|
157 |
|
|
|
158 |
|
|
// Enable the MCI and the Power Saving
|
159 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIEN);
|
160 |
|
|
|
161 |
|
|
// Disable the MCI peripheral clock.
|
162 |
|
|
WRITE_PMC(AT91C_BASE_PMC, PMC_PCDR, (1 << mciId));
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
//------------------------------------------------------------------------------
|
166 |
|
|
/// Close a MCI driver instance and the underlying peripheral.
|
167 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
168 |
|
|
/// \param pMciHw Pointer to a MCI peripheral.
|
169 |
|
|
/// \param mciId MCI peripheral identifier.
|
170 |
|
|
//------------------------------------------------------------------------------
|
171 |
|
|
void MCI_Close(Mci *pMci)
|
172 |
|
|
{
|
173 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
174 |
|
|
|
175 |
|
|
SANITY_CHECK(pMci);
|
176 |
|
|
SANITY_CHECK(pMciHw);
|
177 |
|
|
|
178 |
|
|
// Initialize the MCI driver structure
|
179 |
|
|
pMci->semaphore = 1;
|
180 |
|
|
pMci->pCommand = 0;
|
181 |
|
|
|
182 |
|
|
// Disable the MCI peripheral clock.
|
183 |
|
|
WRITE_PMC(AT91C_BASE_PMC, PMC_PCDR, (1 << pMci->mciId));
|
184 |
|
|
|
185 |
|
|
// Disable the MCI
|
186 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIDIS);
|
187 |
|
|
|
188 |
|
|
// Disable all the interrupts
|
189 |
|
|
WRITE_MCI(pMciHw, MCI_IDR, 0xFFFFFFFF);
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
//------------------------------------------------------------------------------
|
193 |
|
|
/// Configure the MCI CLKDIV in the MCI_MR register. The max. for MCI clock is
|
194 |
|
|
/// MCK/2 and corresponds to CLKDIV = 0
|
195 |
|
|
/// \param pMci Pointer to the low level MCI driver.
|
196 |
|
|
/// \param mciSpeed MCI clock speed in Hz.
|
197 |
|
|
//------------------------------------------------------------------------------
|
198 |
|
|
void MCI_SetSpeed(Mci *pMci, unsigned int mciSpeed)
|
199 |
|
|
{
|
200 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
201 |
|
|
unsigned int mciMr;
|
202 |
|
|
unsigned int clkdiv;
|
203 |
|
|
|
204 |
|
|
SANITY_CHECK(pMci);
|
205 |
|
|
SANITY_CHECK(pMci->pMciHw);
|
206 |
|
|
|
207 |
|
|
// Set the Mode Register: 400KHz for MCK = 48MHz (CLKDIV = 58)
|
208 |
|
|
mciMr = READ_MCI(pMciHw, MCI_MR) & (~AT91C_MCI_CLKDIV);
|
209 |
|
|
|
210 |
|
|
// Multimedia Card Interface clock (MCCK or MCI_CK) is Master Clock (MCK)
|
211 |
|
|
// divided by (2*(CLKDIV+1))
|
212 |
|
|
if (mciSpeed > 0) {
|
213 |
|
|
|
214 |
|
|
clkdiv = (BOARD_MCK / (mciSpeed * 2));
|
215 |
|
|
if (clkdiv > 0) {
|
216 |
|
|
|
217 |
|
|
clkdiv -= 1;
|
218 |
|
|
}
|
219 |
|
|
ASSERT( (clkdiv & 0xFFFFFF00) == 0, "mciSpeed too small");
|
220 |
|
|
}
|
221 |
|
|
else {
|
222 |
|
|
|
223 |
|
|
clkdiv = 0;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
WRITE_MCI(pMciHw, MCI_MR, mciMr | clkdiv);
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
//------------------------------------------------------------------------------
|
230 |
|
|
/// Configure the MCI SDCBUS in the MCI_SDCR register. Only two modes available
|
231 |
|
|
///
|
232 |
|
|
/// \param pMci Pointer to the low level MCI driver.
|
233 |
|
|
/// \param busWidth MCI bus width mode.
|
234 |
|
|
//------------------------------------------------------------------------------
|
235 |
|
|
void MCI_SetBusWidth(Mci *pMci, unsigned char busWidth)
|
236 |
|
|
{
|
237 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
238 |
|
|
unsigned int mciSdcr;
|
239 |
|
|
|
240 |
|
|
SANITY_CHECK(pMci);
|
241 |
|
|
SANITY_CHECK(pMci->pMciHw);
|
242 |
|
|
|
243 |
|
|
mciSdcr = (READ_MCI(pMciHw, MCI_SDCR) & ~(AT91C_MCI_SCDBUS));
|
244 |
|
|
|
245 |
|
|
WRITE_MCI(pMciHw, MCI_SDCR, mciSdcr | busWidth);
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
//------------------------------------------------------------------------------
|
249 |
|
|
/// Starts a MCI transfer. This is a non blocking function. It will return
|
250 |
|
|
/// as soon as the transfer is started.
|
251 |
|
|
/// Return 0 if successful; otherwise returns MCI_ERROR_LOCK if the driver is
|
252 |
|
|
/// already in use.
|
253 |
|
|
/// \param pMci Pointer to an MCI driver instance.
|
254 |
|
|
/// \param pCommand Pointer to the command to execute.
|
255 |
|
|
//------------------------------------------------------------------------------
|
256 |
|
|
unsigned char MCI_SendCommand(Mci *pMci, MciCmd *pCommand)
|
257 |
|
|
{
|
258 |
|
|
AT91PS_MCI pMciHw = pMci->pMciHw;
|
259 |
|
|
unsigned int mciIer, mciMr;
|
260 |
|
|
|
261 |
|
|
SANITY_CHECK(pMci);
|
262 |
|
|
SANITY_CHECK(pMciHw);
|
263 |
|
|
SANITY_CHECK(pCommand);
|
264 |
|
|
|
265 |
|
|
// Try to acquire the MCI semaphore
|
266 |
|
|
if (pMci->semaphore == 0) {
|
267 |
|
|
|
268 |
|
|
return MCI_ERROR_LOCK;
|
269 |
|
|
}
|
270 |
|
|
pMci->semaphore--;
|
271 |
|
|
// TRACE_DEBUG("MCI_SendCommand %x %d\n\r", READ_MCI(pMciHw, MCI_SR), pCommand->cmd & 0x3f);
|
272 |
|
|
|
273 |
|
|
// Command is now being executed
|
274 |
|
|
pMci->pCommand = pCommand;
|
275 |
|
|
pCommand->status = MCI_STATUS_PENDING;
|
276 |
|
|
|
277 |
|
|
// Enable the MCI clock
|
278 |
|
|
WRITE_PMC(AT91C_BASE_PMC, PMC_PCER, (1 << pMci->mciId));
|
279 |
|
|
|
280 |
|
|
//Disable MCI clock, for multi-block data transfer
|
281 |
|
|
MCI_Enable(pMci, DISABLE);
|
282 |
|
|
|
283 |
|
|
// Set PDC data transfer direction
|
284 |
|
|
if(pCommand->blockSize > 0) {
|
285 |
|
|
if(pCommand->isRead) {
|
286 |
|
|
WRITE_MCI(pMciHw, MCI_PTCR, AT91C_PDC_RXTEN);
|
287 |
|
|
}
|
288 |
|
|
else {
|
289 |
|
|
WRITE_MCI(pMciHw, MCI_PTCR, AT91C_PDC_TXTEN);
|
290 |
|
|
}
|
291 |
|
|
}
|
292 |
|
|
// Disable transmitter and receiver
|
293 |
|
|
WRITE_MCI(pMciHw, MCI_PTCR, AT91C_PDC_RXTDIS | AT91C_PDC_TXTDIS);
|
294 |
|
|
|
295 |
|
|
mciMr = READ_MCI(pMciHw, MCI_MR) & (~(AT91C_MCI_WRPROOF|AT91C_MCI_RDPROOF|AT91C_MCI_BLKLEN | AT91C_MCI_PDCMODE));
|
296 |
|
|
|
297 |
|
|
// Command with DATA stage
|
298 |
|
|
if (pCommand->blockSize > 0) {
|
299 |
|
|
// Enable PDC mode and set block size
|
300 |
|
|
if(pCommand->conTrans != MCI_CONTINUE_TRANSFER) {
|
301 |
|
|
|
302 |
|
|
WRITE_MCI(pMciHw, MCI_MR, mciMr | AT91C_MCI_PDCMODE |AT91C_MCI_RDPROOF|AT91C_MCI_WRPROOF|(pCommand->blockSize << 16));
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
// DATA transfer from card to host
|
306 |
|
|
if (pCommand->isRead) {
|
307 |
|
|
WRITE_MCI(pMciHw, MCI_RPR, (int) pCommand->pData);
|
308 |
|
|
|
309 |
|
|
// Sanity check
|
310 |
|
|
if (pCommand->nbBlock == 0)
|
311 |
|
|
pCommand->nbBlock = 1;
|
312 |
|
|
////////
|
313 |
|
|
if ((pCommand->blockSize & 0x3) != 0) {
|
314 |
|
|
WRITE_MCI(pMciHw, MCI_RCR, (pCommand->nbBlock * pCommand->blockSize) / 4 + 1);
|
315 |
|
|
}
|
316 |
|
|
else {
|
317 |
|
|
WRITE_MCI(pMciHw, MCI_RCR, (pCommand->nbBlock * pCommand->blockSize) / 4);
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
WRITE_MCI(pMciHw, MCI_PTCR, AT91C_PDC_RXTEN);
|
321 |
|
|
mciIer = AT91C_MCI_ENDRX | STATUS_ERRORS;
|
322 |
|
|
// mciIer = AT91C_MCI_RXBUFF | STATUS_ERRORS;
|
323 |
|
|
}
|
324 |
|
|
|
325 |
|
|
// DATA transfer from host to card
|
326 |
|
|
else {
|
327 |
|
|
// Sanity check
|
328 |
|
|
if (pCommand->nbBlock == 0)
|
329 |
|
|
pCommand->nbBlock = 1;
|
330 |
|
|
WRITE_MCI(pMciHw, MCI_TPR, (int) pCommand->pData);
|
331 |
|
|
// Update the PDC counter
|
332 |
|
|
if ((pCommand->blockSize & 0x3) != 0) {
|
333 |
|
|
WRITE_MCI(pMciHw, MCI_TCR, (pCommand->nbBlock * pCommand->blockSize) / 4 + 1);
|
334 |
|
|
}
|
335 |
|
|
else {
|
336 |
|
|
WRITE_MCI(pMciHw, MCI_TCR, (pCommand->nbBlock * pCommand->blockSize) / 4);
|
337 |
|
|
}
|
338 |
|
|
// MCI_BLKE notifies the end of Multiblock command
|
339 |
|
|
mciIer = AT91C_MCI_BLKE | STATUS_ERRORS;
|
340 |
|
|
}
|
341 |
|
|
}
|
342 |
|
|
// No data transfer: stop at the end of the command
|
343 |
|
|
else {
|
344 |
|
|
WRITE_MCI(pMciHw, MCI_MR, mciMr);
|
345 |
|
|
mciIer = AT91C_MCI_CMDRDY | STATUS_ERRORS;
|
346 |
|
|
}
|
347 |
|
|
// Enable MCI clock
|
348 |
|
|
MCI_Enable(pMci, ENABLE);
|
349 |
|
|
|
350 |
|
|
// Send the command
|
351 |
|
|
if((pCommand->conTrans != MCI_CONTINUE_TRANSFER)
|
352 |
|
|
|| (pCommand->blockSize == 0)) {
|
353 |
|
|
|
354 |
|
|
WRITE_MCI(pMciHw, MCI_ARGR, pCommand->arg);
|
355 |
|
|
WRITE_MCI(pMciHw, MCI_CMDR, pCommand->cmd);
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
// In case of transmit, the PDC shall be enabled after sending the command
|
359 |
|
|
if ((pCommand->blockSize > 0) && !(pCommand->isRead)) {
|
360 |
|
|
WRITE_MCI(pMciHw, MCI_PTCR, AT91C_PDC_TXTEN);
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
// Ignore data error
|
364 |
|
|
mciIer &= ~(AT91C_MCI_UNRE | AT91C_MCI_OVRE \
|
365 |
|
|
| AT91C_MCI_DTOE | AT91C_MCI_DCRCE);
|
366 |
|
|
|
367 |
|
|
// Interrupt enable shall be done after PDC TXTEN and RXTEN
|
368 |
|
|
WRITE_MCI(pMciHw, MCI_IER, mciIer);
|
369 |
|
|
|
370 |
|
|
return 0;
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
//------------------------------------------------------------------------------
|
374 |
|
|
/// Check NOTBUSY and DTIP bits of status register on the given MCI driver.
|
375 |
|
|
/// Return value, 0 for bus ready, 1 for bus busy
|
376 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
377 |
|
|
//------------------------------------------------------------------------------
|
378 |
|
|
unsigned char MCI_CheckBusy(Mci *pMci)
|
379 |
|
|
{
|
380 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
381 |
|
|
unsigned int status;
|
382 |
|
|
|
383 |
|
|
// Enable MCI clock
|
384 |
|
|
MCI_Enable(pMci, ENABLE);
|
385 |
|
|
|
386 |
|
|
status = READ_MCI(pMciHw, MCI_SR);
|
387 |
|
|
// TRACE_DEBUG("status %x\n\r",status);
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
if(((status & AT91C_MCI_NOTBUSY)!=0)
|
391 |
|
|
&& ((status & AT91C_MCI_DTIP)==0)) {
|
392 |
|
|
|
393 |
|
|
// Disable MCI clock
|
394 |
|
|
MCI_Enable(pMci, DISABLE);
|
395 |
|
|
|
396 |
|
|
return 0;
|
397 |
|
|
}
|
398 |
|
|
else {
|
399 |
|
|
return 1;
|
400 |
|
|
}
|
401 |
|
|
}
|
402 |
|
|
|
403 |
|
|
//------------------------------------------------------------------------------
|
404 |
|
|
/// Check BLKE bit of status register on the given MCI driver.
|
405 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
406 |
|
|
//------------------------------------------------------------------------------
|
407 |
|
|
unsigned char MCI_CheckBlke(Mci *pMci)
|
408 |
|
|
{
|
409 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
410 |
|
|
unsigned int status;
|
411 |
|
|
|
412 |
|
|
status = READ_MCI(pMciHw, MCI_SR);
|
413 |
|
|
// TRACE_DEBUG("status %x\n\r",status);
|
414 |
|
|
|
415 |
|
|
if((status & AT91C_MCI_BLKE)!=0) {
|
416 |
|
|
return 0;
|
417 |
|
|
}
|
418 |
|
|
else {
|
419 |
|
|
return 1;
|
420 |
|
|
}
|
421 |
|
|
}
|
422 |
|
|
|
423 |
|
|
//------------------------------------------------------------------------------
|
424 |
|
|
/// Processes pending events on the given MCI driver.
|
425 |
|
|
/// \param pMci Pointer to a MCI driver instance.
|
426 |
|
|
//------------------------------------------------------------------------------
|
427 |
|
|
void MCI_Handler(Mci *pMci)
|
428 |
|
|
{
|
429 |
|
|
AT91S_MCI *pMciHw = pMci->pMciHw;
|
430 |
|
|
MciCmd *pCommand = pMci->pCommand;
|
431 |
|
|
unsigned int status;
|
432 |
|
|
unsigned char i;
|
433 |
|
|
#if defined(at91rm9200)
|
434 |
|
|
unsigned int mciCr, mciSdcr, mciMr, mciDtor;
|
435 |
|
|
#endif
|
436 |
|
|
|
437 |
|
|
SANITY_CHECK(pMci);
|
438 |
|
|
SANITY_CHECK(pMciHw);
|
439 |
|
|
SANITY_CHECK(pCommand);
|
440 |
|
|
|
441 |
|
|
// Read the status register
|
442 |
|
|
status = READ_MCI(pMciHw, MCI_SR) & READ_MCI(pMciHw, MCI_IMR);
|
443 |
|
|
// TRACE_DEBUG("status %x\n\r", status);
|
444 |
|
|
|
445 |
|
|
// Check if an error has occured
|
446 |
|
|
if ((status & STATUS_ERRORS) != 0) {
|
447 |
|
|
|
448 |
|
|
// Check error code
|
449 |
|
|
if ((status & STATUS_ERRORS) == AT91C_MCI_RTOE) {
|
450 |
|
|
|
451 |
|
|
pCommand->status = MCI_STATUS_NORESPONSE;
|
452 |
|
|
}
|
453 |
|
|
// if the command is SEND_OP_COND the CRC error flag is always present
|
454 |
|
|
// (cf : R3 response)
|
455 |
|
|
else if (((status & STATUS_ERRORS) != AT91C_MCI_RCRCE)
|
456 |
|
|
|| ((pCommand->cmd != SDCARD_APP_OP_COND_CMD)
|
457 |
|
|
&& (pCommand->cmd != MMC_SEND_OP_COND_CMD))) {
|
458 |
|
|
|
459 |
|
|
pCommand->status = MCI_STATUS_ERROR;
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
|
463 |
|
|
// Check if a transfer has been completed
|
464 |
|
|
if (((status & AT91C_MCI_CMDRDY) != 0)
|
465 |
|
|
|| ((status & AT91C_MCI_ENDRX) != 0)
|
466 |
|
|
|| ((status & AT91C_MCI_RXBUFF) != 0)
|
467 |
|
|
|| ((status & AT91C_MCI_ENDTX) != 0)
|
468 |
|
|
|| ((status & AT91C_MCI_BLKE) != 0)
|
469 |
|
|
|| ((status & AT91C_MCI_RTOE) != 0)) {
|
470 |
|
|
|
471 |
|
|
if (((status & AT91C_MCI_ENDRX) != 0)
|
472 |
|
|
|| ((status & AT91C_MCI_RXBUFF) != 0)
|
473 |
|
|
|| ((status & AT91C_MCI_ENDTX) != 0)) {
|
474 |
|
|
|
475 |
|
|
MCI_Enable(pMci, DISABLE);
|
476 |
|
|
}
|
477 |
|
|
|
478 |
|
|
/// On AT91RM9200-EK, if stop transmission, software reset MCI.
|
479 |
|
|
#if defined(at91rm9200)
|
480 |
|
|
if ((pCommand->cmd & AT91C_MCI_TRCMD_STOP) != 0) {
|
481 |
|
|
mciMr = READ_MCI(pMciHw, MCI_MR);
|
482 |
|
|
mciSdcr = READ_MCI(pMciHw, MCI_SDCR);
|
483 |
|
|
mciDtor = READ_MCI(pMciHw, MCI_DTOR);
|
484 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_SWRST);
|
485 |
|
|
// TRACE_DEBUG("reset MCI\n\r");
|
486 |
|
|
|
487 |
|
|
WRITE_MCI(pMciHw, MCI_CR, AT91C_MCI_MCIDIS | AT91C_MCI_PWSDIS);
|
488 |
|
|
WRITE_MCI(pMciHw, MCI_MR, mciMr);
|
489 |
|
|
WRITE_MCI(pMciHw, MCI_SDCR, mciSdcr);
|
490 |
|
|
WRITE_MCI(pMciHw, MCI_DTOR, mciDtor);
|
491 |
|
|
}
|
492 |
|
|
#endif
|
493 |
|
|
|
494 |
|
|
// If no error occured, the transfer is successful
|
495 |
|
|
if (pCommand->status == MCI_STATUS_PENDING) {
|
496 |
|
|
pCommand->status = 0;
|
497 |
|
|
}
|
498 |
|
|
#if 0
|
499 |
|
|
if ((status & AT91C_MCI_CMDRDY) != 0)
|
500 |
|
|
TRACE_DEBUG_WP(".");
|
501 |
|
|
if ((status & AT91C_MCI_ENDRX) != 0)
|
502 |
|
|
TRACE_DEBUG_WP("<");
|
503 |
|
|
if ((status & AT91C_MCI_ENDTX) != 0)
|
504 |
|
|
TRACE_DEBUG_WP("-");
|
505 |
|
|
if ((status & AT91C_MCI_BLKE) != 0)
|
506 |
|
|
TRACE_DEBUG_WP(">");
|
507 |
|
|
TRACE_DEBUG_WP("\n\r");
|
508 |
|
|
#endif
|
509 |
|
|
// Store the card response in the provided buffer
|
510 |
|
|
if (pCommand->pResp) {
|
511 |
|
|
unsigned char resSize;
|
512 |
|
|
|
513 |
|
|
switch (pCommand->resType) {
|
514 |
|
|
case 1:
|
515 |
|
|
resSize = 1;
|
516 |
|
|
break;
|
517 |
|
|
|
518 |
|
|
case 2:
|
519 |
|
|
resSize = 4;
|
520 |
|
|
break;
|
521 |
|
|
|
522 |
|
|
case 3:
|
523 |
|
|
resSize = 1;
|
524 |
|
|
break;
|
525 |
|
|
|
526 |
|
|
case 4:
|
527 |
|
|
resSize = 1;
|
528 |
|
|
break;
|
529 |
|
|
|
530 |
|
|
case 5:
|
531 |
|
|
resSize = 1;
|
532 |
|
|
break;
|
533 |
|
|
|
534 |
|
|
case 6:
|
535 |
|
|
resSize = 1;
|
536 |
|
|
break;
|
537 |
|
|
|
538 |
|
|
case 7:
|
539 |
|
|
resSize = 1;
|
540 |
|
|
break;
|
541 |
|
|
|
542 |
|
|
default:
|
543 |
|
|
resSize = 0;
|
544 |
|
|
break;
|
545 |
|
|
}
|
546 |
|
|
for (i=0; i < resSize; i++) {
|
547 |
|
|
|
548 |
|
|
pCommand->pResp[i] = READ_MCI(pMciHw, MCI_RSPR[0]);
|
549 |
|
|
}
|
550 |
|
|
}
|
551 |
|
|
|
552 |
|
|
// Disable interrupts
|
553 |
|
|
WRITE_MCI(pMciHw, MCI_IDR, READ_MCI(pMciHw, MCI_IMR));
|
554 |
|
|
|
555 |
|
|
// Release the semaphore
|
556 |
|
|
pMci->semaphore++;
|
557 |
|
|
|
558 |
|
|
// Invoke the callback associated with the current command (if any)
|
559 |
|
|
if (pCommand->callback) {
|
560 |
|
|
(pCommand->callback)(pCommand->status, pCommand);
|
561 |
|
|
}
|
562 |
|
|
}
|
563 |
|
|
}
|
564 |
|
|
|
565 |
|
|
//------------------------------------------------------------------------------
|
566 |
|
|
/// Returns 1 if the given MCI transfer is complete; otherwise returns 0.
|
567 |
|
|
/// \param pCommand Pointer to a MciCmd instance.
|
568 |
|
|
//------------------------------------------------------------------------------
|
569 |
|
|
unsigned char MCI_IsTxComplete(MciCmd *pCommand)
|
570 |
|
|
{
|
571 |
|
|
if (pCommand->status != MCI_STATUS_PENDING) {
|
572 |
|
|
if (pCommand->status != 0) {
|
573 |
|
|
TRACE_DEBUG("MCI_IsTxComplete %d\n\r", pCommand->status);
|
574 |
|
|
}
|
575 |
|
|
return 1;
|
576 |
|
|
}
|
577 |
|
|
else {
|
578 |
|
|
return 0;
|
579 |
|
|
}
|
580 |
|
|
}
|