/*
* Milkymist SoC (Software)
* Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#include
//#define MEMCARD_DEBUG
static void memcard_start_cmd_tx(void)
{
CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_TX;
}
static void memcard_start_cmd_rx(void)
{
CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX;
CSR_MEMCARD_START = MEMCARD_START_CMD_RX;
CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_RX;
}
static void memcard_start_cmd_dat_rx(void)
{
CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX|MEMCARD_PENDING_DAT_RX;
CSR_MEMCARD_START = MEMCARD_START_CMD_RX|MEMCARD_START_DAT_RX;
CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_RX|MEMCARD_ENABLE_DAT_RX;
}
static void memcard_send_command(unsigned char cmd, unsigned int arg)
{
unsigned char packet[6];
int a;
int i;
unsigned char data;
unsigned char crc;
packet[0] = cmd | 0x40;
packet[1] = ((arg >> 24) & 0xff);
packet[2] = ((arg >> 16) & 0xff);
packet[3] = ((arg >> 8) & 0xff);
packet[4] = (arg & 0xff);
crc = 0;
for(a=0;a<5;a++) {
data = packet[a];
for(i=0;i<8;i++) {
crc <<= 1;
if((data & 0x80) ^ (crc & 0x80))
crc ^= 0x09;
data <<= 1;
}
}
crc = (crc<<1) | 1;
packet[5] = crc;
#ifdef MEMCARD_DEBUG
printf(">> %02x %02x %02x %02x %02x %02x\n", packet[0], packet[1], packet[2], packet[3], packet[4], packet[5]);
#endif
for(i=0;i<6;i++) {
CSR_MEMCARD_CMD = packet[i];
while(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_TX);
}
}
static void memcard_send_dummy(void)
{
CSR_MEMCARD_CMD = 0xff;
while(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_TX);
}
static int memcard_receive_command(unsigned char *buffer, int len)
{
int i;
int timeout;
for(i=0;i