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