199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2006 M. Warner Losh. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software is derived from software provide by Kwikbyte who specifically
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disclaimed copyright on the code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* ATMEL Microcontroller Software Support - ROUSSET -
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* The software is delivered "AS IS" without warranty or condition of any
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* kind, either express, implied or statutory. This includes without
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* limitation any warranty or condition with respect to merchantability or
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* fitness for any particular purpose, or against the infringements of
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* intellectual property rights of others.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* File Name : main.c
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* Object : main application written in C
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* Creation : FB 21/11/2002
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "at91rm9200.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "lib_AT91RM9200.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "mci_device.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "lib.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "sd-card.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SD_BLOCK_SIZE 512
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* Global Variables
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic AT91S_MciDevice MCI_Device;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/******************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome**Error return codes
199767f8919635c4928607450d9e0abb932109ceToomas Soome******************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MCI_UNSUPP_SIZE_ERROR 5
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MCI_UNSUPP_OFFSET_ERROR 6
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCIDeviceWaitReady
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Wait for MCI Device ready
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic unsigned int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCIDeviceWaitReady(unsigned int timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome volatile unsigned int status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int waitfor;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome waitfor = AT91C_MCI_RXBUFF;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome waitfor = AT91C_MCI_NOTBUSY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = AT91C_BASE_MCI->MCI_SR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome timeout--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while( !(status & waitfor) && (timeout>0) );
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = AT91C_BASE_MCI->MCI_SR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // If End of Tx Buffer Empty interrupt occurred
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.state == AT91C_MCI_TX_SINGLE_BLOCK && status & AT91C_MCI_TXBUFE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.state = AT91C_MCI_IDLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } // End of if AT91C_MCI_TXBUFF
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // If End of Rx Buffer Full interrupt occurred
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.state == AT91C_MCI_RX_SINGLE_BLOCK && status & AT91C_MCI_RXBUFF) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.state = AT91C_MCI_IDLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } // End of if AT91C_MCI_RXBUFF
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome //printf("WaitReady returning status %x\n", status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic inline unsigned int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeswap(unsigned int v)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int t1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome __asm __volatile("eor %1, %0, %0, ror #16\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "bic %1, %1, #0x00ff0000\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "mov %0, %0, ror #8\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "eor %0, %0, %1, lsr #8\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome : "+r" (v), "=r" (t1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (v);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeinline static unsigned int
199767f8919635c4928607450d9e0abb932109ceToomas Soomewait_ready()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int timeout = AT91C_MCI_TIMEOUT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // wait for CMDRDY Status flag to read the response
199767f8919635c4928607450d9e0abb932109ceToomas Soome do
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = AT91C_BASE_MCI->MCI_SR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while( !(status & AT91C_MCI_CMDRDY) && (--timeout > 0) );
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_SendCommand
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Generic function to send a command to the MMC or SDCard
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_SendCommand(
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int Cmd,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int Arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int errorMask = AT91C_MCI_SR_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int opcode = Cmd & 0x3F;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome //printf("SendCmd %d (%x) arg %x\n", opcode, Cmd, Arg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Don't check response CRC on ACMD41 (R3 response type).
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (opcode == 41)
199767f8919635c4928607450d9e0abb932109ceToomas Soome errorMask &= ~AT91C_MCI_RCRCE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_ARGR = Arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_CMDR = Cmd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = wait_ready();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error & errorMask) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_GetStatus
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Addressed card sends its status register
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic unsigned int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_GetStatus()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SEND_STATUS_CMD, MCI_Device.RCA << 16))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (AT91C_BASE_MCI->MCI_RSPR[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_ReadBlock
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Start the read for a single 512-byte block
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_StartReadBlock(unsigned blknum, void *dataBuffer)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Init Mode Register
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_MR |= ((SD_BLOCK_SIZE << 16) | AT91C_MCI_PDCMODE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // (PDC) Receiver Transfer Enable
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_RPR = (unsigned int)dataBuffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_RCR = SD_BLOCK_SIZE / 4;;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // SDHC wants block offset, non-HC wants byte offset.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!MCI_Device.IsSDHC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome blknum *= SD_BLOCK_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Send the Read single block command
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(READ_SINGLE_BLOCK_CMD, blknum)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return AT91C_READ_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.state = AT91C_MCI_RX_SINGLE_BLOCK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_readblocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Read one or more blocks
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_readblocks(char* dest, unsigned blknum, unsigned blkcount)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int *walker;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.state != AT91C_MCI_IDLE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((MCI_GetStatus() & AT91C_SR_READY_FOR_DATA) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // As long as there is data to read
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (blkcount)
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome //Do the reading
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_StartReadBlock(blknum, dest))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Wait MCI Device Ready
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status & AT91C_MCI_SR_ERROR)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Fix erratum in MCI part - endian-swap all data.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (walker = (unsigned int *)dest;
199767f8919635c4928607450d9e0abb932109ceToomas Soome walker < (unsigned int *)(dest + SD_BLOCK_SIZE); walker++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *walker = swap(*walker);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Update counters & pointers
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++blknum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome --blkcount;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest += SD_BLOCK_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_read
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Legacy read function, takes byte offset and length but was always
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* used to read full blocks; interface preserved for existing boot code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_read(char* dest, unsigned byteoffset, unsigned length)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return MCI_readblocks(dest,
199767f8919635c4928607450d9e0abb932109ceToomas Soome byteoffset/SD_BLOCK_SIZE, length/SD_BLOCK_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_SDCard_SendAppCommand
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Specific function to send a specific command to the SDCard
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_SDCard_SendAppCommand(
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int Cmd_App,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int Arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((status = MCI_SendCommand(APP_CMD, (MCI_Device.RCA << 16))) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = MCI_SendCommand(Cmd_App,Arg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_GetCSD
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Asks to the specified card to send its CSD
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_GetCSD(unsigned int rca, unsigned int *response)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SEND_CSD_CMD, (rca << 16)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_SDCard_GetOCR
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Wait for card to power up and determine whether it's SDHC or not.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_SDCard_GetOCR()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int response;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int arg = AT91C_MMC_HOST_VOLTAGE_RANGE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int timeout = AT91C_MCI_TIMEOUT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Force card to idle state.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_SendCommand(GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Begin probe for SDHC by sending CMD8; only v2.0 cards respond to it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome //
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Arg is vvpp where vv is voltage range and pp is an arbitrary bit
199767f8919635c4928607450d9e0abb932109ceToomas Soome // pattern that gets echoed back in the response. The only voltage
199767f8919635c4928607450d9e0abb932109ceToomas Soome // ranges defined are:
199767f8919635c4928607450d9e0abb932109ceToomas Soome // 0x01 = 2.7 - 3.6
199767f8919635c4928607450d9e0abb932109ceToomas Soome // 0x02 = "reserved for low voltage" whatever that means.
199767f8919635c4928607450d9e0abb932109ceToomas Soome //
199767f8919635c4928607450d9e0abb932109ceToomas Soome // If the card fails to respond then it's not v2.0. If it responds by
199767f8919635c4928607450d9e0abb932109ceToomas Soome // echoing back exactly the arg we sent, then it's a v2.0 card and can
199767f8919635c4928607450d9e0abb932109ceToomas Soome // run at our voltage. That means that when we send the ACMD41 (in
199767f8919635c4928607450d9e0abb932109ceToomas Soome // MCI_SDCard_GetOCR) we can include the HCS bit to inquire about SDHC.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SD_SEND_IF_COND_CMD, 0x01AA) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.IsSDv2 = (AT91C_BASE_MCI->MCI_RSPR[0] == 0x01AA);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // If we've determined the card supports v2.0 functionality, set the
199767f8919635c4928607450d9e0abb932109ceToomas Soome // HCS/CCS bit to indicate that we support SDHC. This will cause a
199767f8919635c4928607450d9e0abb932109ceToomas Soome // v2.0 card to report whether it is SDHC in the ACMD41 response.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.IsSDv2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome arg |= AT91C_CCS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // The RCA to be used for CMD55 in Idle state shall be the card's
199767f8919635c4928607450d9e0abb932109ceToomas Soome // default RCA=0x0000.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.RCA = 0x0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Repeat ACMD41 until the card comes out of power-up-busy state.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SDCard_SendAppCommand(SDCARD_APP_OP_COND_CMD, arg)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome response = AT91C_BASE_MCI->MCI_RSPR[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (!(response & AT91C_CARD_POWER_UP_DONE) && (--timeout > 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // A v2.0 card sets CCS (card capacity status) in the response if it's SDHC.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_Device.IsSDv2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.IsSDHC = ((response & AT91C_CCS) == AT91C_CCS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn MCI_SDCard_GetCID
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Asks to the SDCard on the chosen slot to send its CID
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeMCI_SDCard_GetCID(unsigned int *response)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(ALL_SEND_CID_CMD, AT91C_NO_ARGUMENT))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn sdcard_4wire
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief Set bus width to 1-bit or 4-bit according to the parm.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* Unlike most functions in this file, the return value from this one is
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* bool-ish; returns 0 on failure, 1 on success.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomesdcard_use4wire(int use4wire)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome volatile int ret_value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret_value=MCI_GetStatus();
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while((ret_value > 0) && ((ret_value & AT91C_SR_READY_FOR_DATA) == 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // If going to 4-wire mode, ask the card to turn off the DAT3 card detect
199767f8919635c4928607450d9e0abb932109ceToomas Soome // pullup resistor, if going to 1-wire ask it to turn it back on.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret_value = MCI_SDCard_SendAppCommand(SDCARD_SET_CLR_CARD_DETECT_CMD,
199767f8919635c4928607450d9e0abb932109ceToomas Soome use4wire ? 0 : 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret_value != AT91C_CMD_SEND_OK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Ask the card to go into the requested mode.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret_value = MCI_SDCard_SendAppCommand(SDCARD_SET_BUS_WIDTH_CMD,
199767f8919635c4928607450d9e0abb932109ceToomas Soome use4wire ? AT91C_BUS_WIDTH_4BITS :
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BUS_WIDTH_1BIT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret_value != AT91C_CMD_SEND_OK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Set the MCI device to match the mode we set in the card.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (use4wire) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.SDCard_bus_width = AT91C_BUS_WIDTH_4BITS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_SDCR |= AT91C_MCI_SCDBUS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.SDCard_bus_width = AT91C_BUS_WIDTH_1BIT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_SDCR &= ~AT91C_MCI_SCDBUS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \fn sdcard_init
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* \brief get the mci device ready to read from an SD or SDHC card.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* Unlike most functions in this file, the return value from this one is
199767f8919635c4928607450d9e0abb932109ceToomas Soome//* bool-ish; returns 0 on failure, 1 on success.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//*----------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomesdcard_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int tab_response[4];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Init MCI for MMC and SDCard interface
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91F_MCI_CfgPIO();
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91F_MCI_CfgPMC();
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Init Device Structure
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.state = AT91C_MCI_IDLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.SDCard_bus_width = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.IsSDv2 = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.IsSDHC = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Reset the MCI and set the bus speed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Using MCK/230 gives a legal (under 400khz) bus speed for the card id
199767f8919635c4928607450d9e0abb932109ceToomas Soome // sequence for all reasonable master clock speeds.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_CR = AT91C_MCI_MCIDIS | 0x80;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_DTOR = AT91C_MCI_DTOR_1MEGA_CYCLES;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_MR = AT91C_MCI_PDCMODE | 114; /* clkdiv 114 = MCK/230 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_SDCR = AT91C_MCI_MMC_SLOTA;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_CR = AT91C_MCI_MCIEN|AT91C_MCI_PWSEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Wait for the card to come out of power-up-busy state by repeatedly
199767f8919635c4928607450d9e0abb932109ceToomas Soome // sending ACMD41. This also probes for SDHC versus standard cards.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 100; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SDCard_GetOCR() == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((i & 0x01) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(".");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i >= 100)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SDCard_GetCID(tab_response))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Tell the card to set its address, and remember the result.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SET_RELATIVE_ADDR_CMD, 0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.RCA = (AT91C_BASE_MCI->MCI_RSPR[0] >> 16);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // After sending CMD3 (set addr) we can increase the clock to full speed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Using MCK/4 gives a legal (under 25mhz) bus speed for all reasonable
199767f8919635c4928607450d9e0abb932109ceToomas Soome // master clock speeds.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_MCI->MCI_MR = AT91C_MCI_PDCMODE | 1; /* clkdiv 1 = MCK/4 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_GetCSD(MCI_Device.RCA,tab_response))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.READ_BL_LEN = (tab_response[1] >> CSD_1_RD_B_LEN_S) &
199767f8919635c4928607450d9e0abb932109ceToomas Soome CSD_1_RD_B_LEN_M;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef REPORT_SIZE
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int mult,blocknr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome // compute MULT
199767f8919635c4928607450d9e0abb932109ceToomas Soome mult = 1 << ( ((tab_response[2] >> CSD_2_C_SIZE_M_S) &
199767f8919635c4928607450d9e0abb932109ceToomas Soome CSD_2_C_SIZE_M_M) + 2 );
199767f8919635c4928607450d9e0abb932109ceToomas Soome // compute MSB of C_SIZE
199767f8919635c4928607450d9e0abb932109ceToomas Soome blocknr = ((tab_response[1] >> CSD_1_CSIZE_H_S) &
199767f8919635c4928607450d9e0abb932109ceToomas Soome CSD_1_CSIZE_H_M) << 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome // compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
199767f8919635c4928607450d9e0abb932109ceToomas Soome blocknr = mult * ((blocknr + ((tab_response[2] >> CSD_2_CSIZE_L_S) &
199767f8919635c4928607450d9e0abb932109ceToomas Soome CSD_2_CSIZE_L_M)) + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome MCI_Device.Memory_Capacity = (1 << MCI_Device.READ_BL_LEN) * blocknr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Found SD card %u bytes\n", MCI_Device.Memory_Capacity);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Select card and set block length for following transfers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SEL_DESEL_CARD_CMD, (MCI_Device.RCA)<<16))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (MCI_SendCommand(SET_BLOCKLEN_CMD, SD_BLOCK_SIZE))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}