2004-09-11 03:48:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2004-09-28 06:23:57 +00:00
|
|
|
* Copyright (C) 2004 by Jens Arnold
|
2004-09-11 03:48:05 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2004-09-11 03:48:05 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdbool.h>
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "mmc.h"
|
2004-10-06 20:43:12 +00:00
|
|
|
#include "ata_mmc.h"
|
2010-06-05 21:12:16 +00:00
|
|
|
#include "sdmmc.h"
|
2006-11-08 02:23:01 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2004-09-11 03:48:05 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "led.h"
|
|
|
|
#include "sh7034.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "power.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "hwcompat.h"
|
2004-09-11 09:06:58 +00:00
|
|
|
#include "adc.h"
|
2004-09-28 06:23:57 +00:00
|
|
|
#include "bitswap.h"
|
2005-01-28 22:35:20 +00:00
|
|
|
#include "disk.h" /* for mount/unmount */
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2008-09-14 20:33:24 +00:00
|
|
|
#define BLOCK_SIZE 512 /* fixed */
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
/* Command definitions */
|
|
|
|
#define CMD_GO_IDLE_STATE 0x40 /* R1 */
|
|
|
|
#define CMD_SEND_OP_COND 0x41 /* R1 */
|
|
|
|
#define CMD_SEND_CSD 0x49 /* R1 */
|
2004-10-03 23:32:09 +00:00
|
|
|
#define CMD_SEND_CID 0x4a /* R1 */
|
|
|
|
#define CMD_STOP_TRANSMISSION 0x4c /* R1 */
|
|
|
|
#define CMD_SEND_STATUS 0x4d /* R2 */
|
2005-04-28 01:11:21 +00:00
|
|
|
#define CMD_SET_BLOCKLEN 0x50 /* R1 */
|
2004-09-28 06:23:57 +00:00
|
|
|
#define CMD_READ_SINGLE_BLOCK 0x51 /* R1 */
|
|
|
|
#define CMD_READ_MULTIPLE_BLOCK 0x52 /* R1 */
|
|
|
|
#define CMD_WRITE_BLOCK 0x58 /* R1b */
|
|
|
|
#define CMD_WRITE_MULTIPLE_BLOCK 0x59 /* R1b */
|
2004-10-03 23:32:09 +00:00
|
|
|
#define CMD_READ_OCR 0x7a /* R3 */
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
/* Response formats:
|
|
|
|
R1 = single byte, msb=0, various error flags
|
|
|
|
R1b = R1 + busy token(s)
|
|
|
|
R2 = 2 bytes (1st byte identical to R1), additional flags
|
|
|
|
R3 = 5 bytes (R1 + OCR register)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define R1_PARAMETER_ERR 0x40
|
|
|
|
#define R1_ADDRESS_ERR 0x20
|
|
|
|
#define R1_ERASE_SEQ_ERR 0x10
|
|
|
|
#define R1_COM_CRC_ERR 0x08
|
|
|
|
#define R1_ILLEGAL_CMD 0x04
|
|
|
|
#define R1_ERASE_RESET 0x02
|
|
|
|
#define R1_IN_IDLE_STATE 0x01
|
|
|
|
|
|
|
|
#define R2_OUT_OF_RANGE 0x80
|
|
|
|
#define R2_ERASE_PARAM 0x40
|
|
|
|
#define R2_WP_VIOLATION 0x20
|
|
|
|
#define R2_CARD_ECC_FAIL 0x10
|
|
|
|
#define R2_CC_ERROR 0x08
|
|
|
|
#define R2_ERROR 0x04
|
|
|
|
#define R2_ERASE_SKIP 0x02
|
|
|
|
#define R2_CARD_LOCKED 0x01
|
|
|
|
|
2004-10-03 23:32:09 +00:00
|
|
|
/* Data start tokens */
|
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
#define DT_START_BLOCK 0xfe
|
|
|
|
#define DT_START_WRITE_MULTIPLE 0xfc
|
|
|
|
#define DT_STOP_TRAN 0xfd
|
2004-10-03 23:32:09 +00:00
|
|
|
|
2004-09-11 03:48:05 +00:00
|
|
|
/* for compatibility */
|
2008-11-01 16:14:28 +00:00
|
|
|
static long last_disk_activity = -1;
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
/* private variables */
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2004-10-03 23:32:09 +00:00
|
|
|
static struct mutex mmc_mutex;
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2005-01-28 22:35:20 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-01-23 13:40:44 +00:00
|
|
|
static long mmc_stack[((DEFAULT_STACK_SIZE*2) + 0x800)/sizeof(long)];
|
2006-11-08 08:04:11 +00:00
|
|
|
#else
|
2007-01-23 13:40:44 +00:00
|
|
|
static long mmc_stack[(DEFAULT_STACK_SIZE*2)/sizeof(long)];
|
2006-11-08 08:04:11 +00:00
|
|
|
#endif
|
2005-01-28 22:35:20 +00:00
|
|
|
static const char mmc_thread_name[] = "mmc";
|
|
|
|
static struct event_queue mmc_queue;
|
2004-09-11 03:48:05 +00:00
|
|
|
static bool initialized = false;
|
2005-02-20 00:21:20 +00:00
|
|
|
static bool new_mmc_circuit;
|
2004-10-04 22:29:06 +00:00
|
|
|
|
2005-05-17 22:10:51 +00:00
|
|
|
static enum {
|
|
|
|
MMC_UNKNOWN,
|
|
|
|
MMC_UNTOUCHED,
|
|
|
|
MMC_TOUCHED
|
|
|
|
} mmc_status = MMC_UNKNOWN;
|
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
static enum {
|
|
|
|
SER_POLL_WRITE,
|
|
|
|
SER_POLL_READ,
|
|
|
|
SER_DISABLED
|
|
|
|
} serial_mode;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
static const unsigned char dummy[] = {
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
|
|
|
|
};
|
|
|
|
|
2008-09-14 20:33:24 +00:00
|
|
|
/* 2 buffers used alternatively for writing, including start token,
|
|
|
|
* dummy CRC and an extra byte to keep word alignment. */
|
2008-09-14 23:38:31 +00:00
|
|
|
static unsigned char write_buffer[2][BLOCK_SIZE+4];
|
2008-09-14 20:33:24 +00:00
|
|
|
static int current_buffer = 0;
|
2008-09-14 23:38:31 +00:00
|
|
|
static const unsigned char *send_block_addr = NULL;
|
2005-05-02 00:33:01 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
static tCardInfo card_info[2];
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2004-10-09 01:14:55 +00:00
|
|
|
static int current_card = 0;
|
2005-01-03 23:20:31 +00:00
|
|
|
#endif
|
2004-10-10 00:35:19 +00:00
|
|
|
static bool last_mmc_status = false;
|
2010-06-06 13:20:47 +00:00
|
|
|
static int countdown = -1; /* for mmc switch debouncing. -1 because the
|
|
|
|
countdown should not happen if the card
|
|
|
|
is inserted at boot */
|
2008-09-14 20:33:24 +00:00
|
|
|
static bool usb_activity; /* monitoring the USB bridge */
|
2005-02-20 00:21:20 +00:00
|
|
|
static long last_usb_activity;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
/* private function declarations */
|
|
|
|
|
|
|
|
static int select_card(int card_no);
|
|
|
|
static void deselect_card(void);
|
|
|
|
static void setup_sci1(int bitrate_register);
|
2004-10-09 01:14:55 +00:00
|
|
|
static void set_sci1_poll_read(void);
|
2004-09-28 06:23:57 +00:00
|
|
|
static void write_transfer(const unsigned char *buf, int len)
|
|
|
|
__attribute__ ((section(".icode")));
|
|
|
|
static void read_transfer(unsigned char *buf, int len)
|
|
|
|
__attribute__ ((section(".icode")));
|
2005-04-28 01:11:21 +00:00
|
|
|
static unsigned char poll_byte(long timeout);
|
|
|
|
static unsigned char poll_busy(long timeout);
|
2008-09-19 07:27:08 +00:00
|
|
|
static unsigned char send_cmd(int cmd, unsigned long parameter, void *data);
|
2004-10-09 01:14:55 +00:00
|
|
|
static int receive_cxd(unsigned char *buf);
|
2004-09-28 06:23:57 +00:00
|
|
|
static int initialize_card(int card_no);
|
2008-09-14 20:33:24 +00:00
|
|
|
static int receive_block(unsigned char *inbuf, long timeout);
|
2008-09-14 23:38:31 +00:00
|
|
|
static void send_block_prepare(void);
|
|
|
|
static int send_block_send(unsigned char start_token, long timeout,
|
|
|
|
bool prepare_next);
|
2004-10-10 00:35:19 +00:00
|
|
|
static void mmc_tick(void);
|
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
/* implementation */
|
|
|
|
|
2005-05-22 00:42:00 +00:00
|
|
|
void mmc_enable_int_flash_clock(bool on)
|
2004-09-28 06:23:57 +00:00
|
|
|
{
|
2005-05-22 00:42:00 +00:00
|
|
|
/* Internal flash clock is enabled by setting PA12 high with the new
|
|
|
|
* clock circuit, and by setting it low with the old clock circuit */
|
|
|
|
if (on ^ new_mmc_circuit)
|
2004-11-17 20:14:43 +00:00
|
|
|
and_b(~0x10, &PADRH); /* clear clock gate PA12 */
|
2005-05-22 00:42:00 +00:00
|
|
|
else
|
|
|
|
or_b(0x10, &PADRH); /* set clock gate PA12 */
|
2004-10-10 19:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int select_card(int card_no)
|
|
|
|
{
|
2005-05-14 17:23:13 +00:00
|
|
|
mutex_lock(&mmc_mutex);
|
|
|
|
led(true);
|
2004-10-06 20:43:12 +00:00
|
|
|
last_disk_activity = current_tick;
|
2004-10-01 17:01:40 +00:00
|
|
|
|
2008-09-17 18:53:11 +00:00
|
|
|
mmc_enable_int_flash_clock(card_no == 0);
|
|
|
|
|
2004-09-29 00:50:40 +00:00
|
|
|
if (!card_info[card_no].initialized)
|
|
|
|
{
|
2004-09-29 01:10:32 +00:00
|
|
|
setup_sci1(7); /* Initial rate: 375 kbps (need <= 400 per mmc specs) */
|
2004-09-29 00:50:40 +00:00
|
|
|
write_transfer(dummy, 10); /* allow the card to synchronize */
|
|
|
|
while (!(SSR1 & SCI_TEND));
|
|
|
|
}
|
|
|
|
|
2004-10-01 17:01:40 +00:00
|
|
|
if (card_no == 0) /* internal */
|
2004-09-28 06:23:57 +00:00
|
|
|
and_b(~0x04, &PADRH); /* assert CS */
|
2004-10-01 17:01:40 +00:00
|
|
|
else /* external */
|
2004-09-28 06:23:57 +00:00
|
|
|
and_b(~0x02, &PADRH); /* assert CS */
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
if (card_info[card_no].initialized)
|
|
|
|
{
|
|
|
|
setup_sci1(card_info[card_no].bitrate_register);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return initialize_card(card_no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deselect_card(void)
|
|
|
|
{
|
2004-09-29 00:50:40 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
2004-09-28 06:23:57 +00:00
|
|
|
or_b(0x06, &PADRH); /* deassert CS (both cards) */
|
2004-10-06 20:43:12 +00:00
|
|
|
|
2005-05-14 17:23:13 +00:00
|
|
|
led(false);
|
|
|
|
mutex_unlock(&mmc_mutex);
|
2004-10-06 20:43:12 +00:00
|
|
|
last_disk_activity = current_tick;
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_sci1(int bitrate_register)
|
|
|
|
{
|
2004-09-29 00:50:40 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
SCR1 = 0; /* disable serial port */
|
|
|
|
SMR1 = SYNC_MODE; /* no prescale */
|
|
|
|
BRR1 = bitrate_register;
|
|
|
|
SSR1 = 0;
|
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
SCR1 = SCI_TE; /* enable transmitter */
|
|
|
|
serial_mode = SER_POLL_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_sci1_poll_read(void)
|
|
|
|
{
|
2004-11-14 07:35:48 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
2004-10-09 01:14:55 +00:00
|
|
|
SCR1 = 0; /* disable transmitter (& receiver) */
|
|
|
|
SCR1 = (SCI_TE|SCI_RE); /* re-enable transmitter & receiver */
|
2004-11-14 07:35:48 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for SCI init completion (!) */
|
2004-10-09 01:14:55 +00:00
|
|
|
serial_mode = SER_POLL_READ;
|
|
|
|
TDR1 = 0xFF; /* send do-nothing while reading */
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_transfer(const unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
const unsigned char *buf_end = buf + len;
|
2004-10-04 17:53:53 +00:00
|
|
|
register unsigned char data;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
if (serial_mode != SER_POLL_WRITE)
|
|
|
|
{
|
2004-11-14 07:35:48 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
2005-04-28 01:11:21 +00:00
|
|
|
SCR1 = 0; /* disable transmitter & receiver */
|
|
|
|
SSR1 = 0; /* clear all flags */
|
|
|
|
SCR1 = SCI_TE; /* enable transmitter only */
|
2004-10-09 01:14:55 +00:00
|
|
|
serial_mode = SER_POLL_WRITE;
|
|
|
|
}
|
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
while (buf < buf_end)
|
|
|
|
{
|
2004-10-04 17:53:53 +00:00
|
|
|
data = fliptable[(signed char)(*buf++)]; /* bitswap */
|
2004-10-09 01:14:55 +00:00
|
|
|
while (!(SSR1 & SCI_TDRE)); /* wait for end of transfer */
|
2004-10-04 17:53:53 +00:00
|
|
|
TDR1 = data; /* write byte */
|
2004-09-28 06:23:57 +00:00
|
|
|
SSR1 = 0; /* start transmitting */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-04 17:53:53 +00:00
|
|
|
/* don't call this with len == 0 */
|
2004-09-28 06:23:57 +00:00
|
|
|
static void read_transfer(unsigned char *buf, int len)
|
|
|
|
{
|
2004-10-04 17:53:53 +00:00
|
|
|
unsigned char *buf_end = buf + len - 1;
|
|
|
|
register signed char data;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
if (serial_mode != SER_POLL_READ)
|
|
|
|
set_sci1_poll_read();
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2004-10-04 17:53:53 +00:00
|
|
|
SSR1 = 0; /* start receiving first byte */
|
2004-09-28 06:23:57 +00:00
|
|
|
while (buf < buf_end)
|
|
|
|
{
|
2004-10-04 17:53:53 +00:00
|
|
|
while (!(SSR1 & SCI_RDRF)); /* wait for data */
|
|
|
|
data = RDR1; /* read byte */
|
|
|
|
SSR1 = 0; /* start receiving */
|
|
|
|
*buf++ = fliptable[data]; /* bitswap */
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
2004-10-04 17:53:53 +00:00
|
|
|
while (!(SSR1 & SCI_RDRF)); /* wait for last byte */
|
|
|
|
*buf = fliptable[(signed char)(RDR1)]; /* read & bitswap */
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
|
|
|
|
2004-10-09 22:48:10 +00:00
|
|
|
/* returns 0xFF on timeout, timeout is in bytes */
|
2005-04-28 01:11:21 +00:00
|
|
|
static unsigned char poll_byte(long timeout)
|
2004-09-28 06:23:57 +00:00
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
long i;
|
2004-09-28 06:23:57 +00:00
|
|
|
unsigned char data = 0; /* stop the compiler complaining */
|
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
if (serial_mode != SER_POLL_READ)
|
|
|
|
set_sci1_poll_read();
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
SSR1 = 0; /* start receiving */
|
|
|
|
while (!(SSR1 & SCI_RDRF)); /* wait for data */
|
|
|
|
data = RDR1; /* read byte */
|
|
|
|
} while ((data == 0xFF) && (++i < timeout));
|
|
|
|
|
|
|
|
return fliptable[(signed char)data];
|
|
|
|
}
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2004-10-09 22:48:10 +00:00
|
|
|
/* returns 0 on timeout, timeout is in bytes */
|
2005-04-28 01:11:21 +00:00
|
|
|
static unsigned char poll_busy(long timeout)
|
2004-09-29 00:50:40 +00:00
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
long i;
|
2004-09-29 22:44:02 +00:00
|
|
|
unsigned char data, dummy;
|
2004-09-29 00:50:40 +00:00
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
if (serial_mode != SER_POLL_READ)
|
|
|
|
set_sci1_poll_read();
|
2004-09-29 22:44:02 +00:00
|
|
|
|
|
|
|
/* get data response */
|
|
|
|
SSR1 = 0; /* start receiving */
|
|
|
|
while (!(SSR1 & SCI_RDRF)); /* wait for data */
|
2004-10-04 22:29:06 +00:00
|
|
|
data = fliptable[(signed char)(RDR1)]; /* read byte */
|
2004-09-29 22:44:02 +00:00
|
|
|
|
|
|
|
/* wait until the card is ready again */
|
2004-09-29 00:50:40 +00:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
SSR1 = 0; /* start receiving */
|
|
|
|
while (!(SSR1 & SCI_RDRF)); /* wait for data */
|
2004-09-29 22:44:02 +00:00
|
|
|
dummy = RDR1; /* read byte */
|
|
|
|
} while ((dummy != 0xFF) && (++i < timeout));
|
|
|
|
|
2004-10-09 22:48:10 +00:00
|
|
|
return (dummy == 0xFF) ? data : 0;
|
2004-09-29 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
2008-09-19 07:27:08 +00:00
|
|
|
/* Send MMC command and get response. Returns R1 byte directly.
|
|
|
|
* Returns further R2 or R3 bytes in *data (can be NULL for other commands) */
|
|
|
|
static unsigned char send_cmd(int cmd, unsigned long parameter, void *data)
|
2004-09-28 06:23:57 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
static struct {
|
|
|
|
unsigned char cmd;
|
|
|
|
unsigned long parameter;
|
|
|
|
const unsigned char crc7; /* fixed, valid for CMD0 only */
|
|
|
|
const unsigned char trailer;
|
|
|
|
} __attribute__((packed)) command = {0x40, 0, 0x95, 0xFF};
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2008-09-19 07:27:08 +00:00
|
|
|
unsigned char ret;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2008-09-19 07:27:08 +00:00
|
|
|
command.cmd = cmd;
|
|
|
|
command.parameter = htobe32(parameter);
|
2004-10-09 01:14:55 +00:00
|
|
|
|
2008-09-19 07:27:08 +00:00
|
|
|
write_transfer((unsigned char *)&command, sizeof(command));
|
|
|
|
|
|
|
|
ret = poll_byte(20);
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case CMD_SEND_CSD: /* R1 response, leave open */
|
|
|
|
case CMD_SEND_CID:
|
|
|
|
case CMD_READ_SINGLE_BLOCK:
|
2004-10-03 23:32:09 +00:00
|
|
|
case CMD_READ_MULTIPLE_BLOCK:
|
2008-09-19 07:27:08 +00:00
|
|
|
return ret;
|
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
case CMD_SEND_STATUS: /* R2 response, close with dummy */
|
2008-09-19 07:27:08 +00:00
|
|
|
read_transfer(data, 1);
|
2004-09-28 06:23:57 +00:00
|
|
|
break;
|
2008-09-19 07:27:08 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
case CMD_READ_OCR: /* R3 response, close with dummy */
|
2008-09-19 07:27:08 +00:00
|
|
|
read_transfer(data, 4);
|
2004-09-28 06:23:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* R1 response, close with dummy */
|
|
|
|
break; /* also catches block writes */
|
|
|
|
}
|
2008-09-19 07:27:08 +00:00
|
|
|
write_transfer(dummy, 1);
|
|
|
|
return ret;
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
/* Receive CID/ CSD data (16 bytes) */
|
|
|
|
static int receive_cxd(unsigned char *buf)
|
2004-09-28 06:23:57 +00:00
|
|
|
{
|
2004-10-09 01:14:55 +00:00
|
|
|
if (poll_byte(20) != DT_START_BLOCK)
|
2004-09-28 06:23:57 +00:00
|
|
|
{
|
|
|
|
write_transfer(dummy, 1);
|
2005-04-28 01:11:21 +00:00
|
|
|
return -1; /* not start of data */
|
2004-09-28 06:23:57 +00:00
|
|
|
}
|
2004-09-29 00:50:40 +00:00
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
read_transfer(buf, 16);
|
|
|
|
write_transfer(dummy, 3); /* 2 bytes dontcare crc + 1 byte trailer */
|
|
|
|
return 0;
|
2004-09-29 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
2004-10-06 20:43:12 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
static int initialize_card(int card_no)
|
|
|
|
{
|
2008-09-14 20:33:24 +00:00
|
|
|
int rc, i;
|
|
|
|
int blk_exp, ts_exp, taac_exp;
|
2004-09-28 06:23:57 +00:00
|
|
|
tCardInfo *card = &card_info[card_no];
|
|
|
|
|
|
|
|
static const char mantissa[] = { /* *10 */
|
|
|
|
0, 10, 12, 13, 15, 20, 25, 30,
|
|
|
|
35, 40, 45, 50, 55, 60, 70, 80
|
|
|
|
};
|
2004-10-06 20:43:12 +00:00
|
|
|
static const int exponent[] = { /* use varies */
|
|
|
|
1, 10, 100, 1000, 10000, 100000, 1000000,
|
|
|
|
10000000, 100000000, 1000000000
|
2004-09-28 06:23:57 +00:00
|
|
|
};
|
|
|
|
|
2005-05-17 22:10:51 +00:00
|
|
|
if (card_no == 1)
|
|
|
|
mmc_status = MMC_TOUCHED;
|
2008-09-19 07:27:08 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
/* switch to SPI mode */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_GO_IDLE_STATE, 0, NULL) != 0x01)
|
|
|
|
return -1; /* error or no response */
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
/* initialize card */
|
2008-09-19 07:27:08 +00:00
|
|
|
for (i = HZ;;) /* try for 1 second*/
|
2004-11-14 13:50:03 +00:00
|
|
|
{
|
|
|
|
sleep(1);
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0)
|
2004-11-14 13:50:03 +00:00
|
|
|
break;
|
2008-09-19 07:27:08 +00:00
|
|
|
if (--i <= 0)
|
|
|
|
return -2; /* timeout */
|
2004-11-14 13:50:03 +00:00
|
|
|
}
|
2008-09-19 07:27:08 +00:00
|
|
|
|
2004-10-06 20:43:12 +00:00
|
|
|
/* get OCR register */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_READ_OCR, 0, &card->ocr))
|
|
|
|
return -3;
|
|
|
|
card->ocr = betoh32(card->ocr); /* no-op on big endian */
|
|
|
|
|
2004-10-06 20:43:12 +00:00
|
|
|
/* check voltage */
|
|
|
|
if (!(card->ocr & 0x00100000)) /* 3.2 .. 3.3 V */
|
|
|
|
return -4;
|
2008-09-19 07:27:08 +00:00
|
|
|
|
2004-10-06 20:43:12 +00:00
|
|
|
/* get CSD register */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_SEND_CSD, 0, NULL))
|
|
|
|
return -5;
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = receive_cxd((unsigned char*)card->csd);
|
|
|
|
if (rc)
|
2008-09-19 07:27:08 +00:00
|
|
|
return rc * 10 - 5;
|
2005-04-28 01:11:21 +00:00
|
|
|
|
2009-07-01 19:13:01 +00:00
|
|
|
blk_exp = card_extract_bits(card->csd, 83, 4);
|
2008-09-14 20:33:24 +00:00
|
|
|
if (blk_exp < 9) /* block size < 512 bytes not supported */
|
2008-09-19 07:27:08 +00:00
|
|
|
return -6;
|
2005-04-28 01:11:21 +00:00
|
|
|
|
2009-07-01 19:13:01 +00:00
|
|
|
card->numblocks = (card_extract_bits(card->csd, 73, 12) + 1)
|
|
|
|
<< (card_extract_bits(card->csd, 49, 3) + 2 + blk_exp - 9);
|
2008-09-14 20:33:24 +00:00
|
|
|
card->blocksize = BLOCK_SIZE;
|
2004-10-06 20:43:12 +00:00
|
|
|
|
|
|
|
/* max transmission speed, clock divider */
|
2009-07-01 19:13:01 +00:00
|
|
|
ts_exp = card_extract_bits(card->csd, 98, 3);
|
2008-09-14 20:33:24 +00:00
|
|
|
ts_exp = (ts_exp > 3) ? 3 : ts_exp;
|
2009-07-01 19:13:01 +00:00
|
|
|
card->speed = mantissa[card_extract_bits(card->csd, 102, 4)]
|
2008-09-14 20:33:24 +00:00
|
|
|
* exponent[ts_exp + 4];
|
2004-09-28 06:23:57 +00:00
|
|
|
card->bitrate_register = (FREQ/4-1) / card->speed;
|
|
|
|
|
2009-07-01 10:07:22 +00:00
|
|
|
/* NSAC, TAAC, read timeout */
|
2009-07-01 19:13:01 +00:00
|
|
|
card->nsac = 100 * card_extract_bits(card->csd, 111, 8);
|
|
|
|
card->taac = mantissa[card_extract_bits(card->csd, 118, 4)];
|
|
|
|
taac_exp = card_extract_bits(card->csd, 114, 3);
|
2004-10-06 20:43:12 +00:00
|
|
|
card->read_timeout = ((FREQ/4) / (card->bitrate_register + 1)
|
2009-07-01 10:07:22 +00:00
|
|
|
* card->taac / exponent[9 - taac_exp]
|
2004-10-06 20:43:12 +00:00
|
|
|
+ (10 * card->nsac));
|
|
|
|
card->read_timeout /= 8; /* clocks -> bytes */
|
2009-07-01 10:07:22 +00:00
|
|
|
card->taac = card->taac * exponent[taac_exp] / 10;
|
2004-10-06 20:43:12 +00:00
|
|
|
|
|
|
|
/* r2w_factor, write timeout */
|
2009-07-01 19:13:01 +00:00
|
|
|
card->r2w_factor = BIT_N(card_extract_bits(card->csd, 28, 3));
|
2008-09-17 18:53:11 +00:00
|
|
|
card->write_timeout = card->read_timeout * card->r2w_factor;
|
|
|
|
|
|
|
|
if (card->r2w_factor > 32) /* Such cards often need extra read delay */
|
|
|
|
card->read_timeout *= 4;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
/* switch to full speed */
|
|
|
|
setup_sci1(card->bitrate_register);
|
2008-09-14 20:33:24 +00:00
|
|
|
|
|
|
|
/* always use 512 byte blocks */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_SET_BLOCKLEN, BLOCK_SIZE, NULL))
|
|
|
|
return -7;
|
2008-09-14 20:33:24 +00:00
|
|
|
|
2004-09-28 06:23:57 +00:00
|
|
|
/* get CID register */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_SEND_CID, 0, NULL))
|
|
|
|
return -8;
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = receive_cxd((unsigned char*)card->cid);
|
|
|
|
if (rc)
|
2008-09-19 07:27:08 +00:00
|
|
|
return rc * 10 - 8;
|
2004-09-28 06:23:57 +00:00
|
|
|
|
|
|
|
card->initialized = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2004-10-06 20:43:12 +00:00
|
|
|
tCardInfo *mmc_card_info(int card_no)
|
|
|
|
{
|
|
|
|
tCardInfo *card = &card_info[card_no];
|
|
|
|
|
2004-10-10 00:35:19 +00:00
|
|
|
if (!card->initialized && ((card_no == 0) || mmc_detect()))
|
2004-10-06 20:43:12 +00:00
|
|
|
{
|
|
|
|
select_card(card_no);
|
|
|
|
deselect_card();
|
|
|
|
}
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2008-09-14 23:38:31 +00:00
|
|
|
/* Receive one block with DMA and bitswap it (chasing bitswap). */
|
2008-09-14 20:33:24 +00:00
|
|
|
static int receive_block(unsigned char *inbuf, long timeout)
|
2004-10-09 01:14:55 +00:00
|
|
|
{
|
2008-09-14 23:38:31 +00:00
|
|
|
unsigned long buf_end;
|
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
if (poll_byte(timeout) != DT_START_BLOCK)
|
|
|
|
{
|
|
|
|
write_transfer(dummy, 1);
|
2005-04-28 01:11:21 +00:00
|
|
|
return -1; /* not start of data */
|
2004-10-09 01:14:55 +00:00
|
|
|
}
|
2005-04-28 01:11:21 +00:00
|
|
|
|
2004-10-09 01:14:55 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
|
|
|
|
|
|
|
SCR1 = 0; /* disable serial */
|
|
|
|
SSR1 = 0; /* clear all flags */
|
|
|
|
|
2006-05-07 20:20:27 +00:00
|
|
|
/* setup DMA channel 0 */
|
|
|
|
CHCR0 = 0; /* disable */
|
|
|
|
SAR0 = RDR1_ADDR;
|
|
|
|
DAR0 = (unsigned long) inbuf;
|
2008-09-14 20:33:24 +00:00
|
|
|
DTCR0 = BLOCK_SIZE;
|
2006-05-07 20:20:27 +00:00
|
|
|
CHCR0 = 0x4601; /* fixed source address, RXI1, enable */
|
2004-10-09 01:14:55 +00:00
|
|
|
DMAOR = 0x0001;
|
|
|
|
SCR1 = (SCI_RE|SCI_RIE); /* kick off DMA */
|
2008-09-14 23:38:31 +00:00
|
|
|
|
|
|
|
/* DMA receives 2 bytes more than DTCR2, but the last 2 bytes are not
|
2004-10-09 01:14:55 +00:00
|
|
|
* stored. The first extra byte is available from RDR1 after the DMA ends,
|
2008-09-14 23:38:31 +00:00
|
|
|
* the second one is lost because of the SCI overrun. However, this
|
2004-10-09 01:14:55 +00:00
|
|
|
* behaviour conveniently discards the crc. */
|
|
|
|
|
|
|
|
yield(); /* be nice */
|
2008-09-14 23:38:31 +00:00
|
|
|
|
|
|
|
/* Bitswap received data, chasing the DMA pointer */
|
|
|
|
buf_end = (unsigned long)inbuf + BLOCK_SIZE;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Call bitswap whenever (a multiple of) 8 bytes are
|
|
|
|
* available (value optimised by experimentation). */
|
|
|
|
int swap_now = (DAR0 - (unsigned long)inbuf) & ~0x00000007;
|
|
|
|
if (swap_now)
|
|
|
|
{
|
|
|
|
bitswap(inbuf, swap_now);
|
|
|
|
inbuf += swap_now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((unsigned long)inbuf < buf_end);
|
2004-10-09 01:14:55 +00:00
|
|
|
|
2006-05-07 20:20:27 +00:00
|
|
|
while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
|
2004-10-09 01:14:55 +00:00
|
|
|
while (!(SSR1 & SCI_ORER)); /* wait for the trailing bytes */
|
|
|
|
SCR1 = 0;
|
|
|
|
serial_mode = SER_DISABLED;
|
|
|
|
|
|
|
|
write_transfer(dummy, 1); /* send trailer */
|
2005-05-02 00:33:01 +00:00
|
|
|
last_disk_activity = current_tick;
|
2004-10-09 01:14:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 23:38:31 +00:00
|
|
|
/* Prepare a block for sending by copying it to the next write buffer
|
|
|
|
* and bitswapping it. */
|
|
|
|
static void send_block_prepare(void)
|
|
|
|
{
|
|
|
|
unsigned char *dest;
|
|
|
|
|
|
|
|
current_buffer ^= 1; /* toggle buffer */
|
|
|
|
dest = write_buffer[current_buffer] + 2;
|
|
|
|
|
|
|
|
memcpy(dest, send_block_addr, BLOCK_SIZE);
|
|
|
|
bitswap(dest, BLOCK_SIZE);
|
|
|
|
|
|
|
|
send_block_addr += BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send one block with DMA from the current write buffer, possibly preparing
|
|
|
|
* the next block within the next write buffer in the background. */
|
2008-09-15 21:10:08 +00:00
|
|
|
static int send_block_send(unsigned char start_token, long timeout,
|
2008-09-14 23:38:31 +00:00
|
|
|
bool prepare_next)
|
2004-10-09 01:14:55 +00:00
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
int rc = 0;
|
2008-09-14 23:38:31 +00:00
|
|
|
unsigned char *curbuf = write_buffer[current_buffer];
|
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
curbuf[1] = fliptable[(signed char)start_token];
|
2008-09-14 20:33:24 +00:00
|
|
|
*(unsigned short *)(curbuf + BLOCK_SIZE + 2) = 0xFFFF;
|
2004-10-09 01:14:55 +00:00
|
|
|
|
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
|
|
|
|
|
|
|
SCR1 = 0; /* disable serial */
|
|
|
|
SSR1 = 0; /* clear all flags */
|
|
|
|
|
2006-05-07 20:20:27 +00:00
|
|
|
/* setup DMA channel 0 */
|
|
|
|
CHCR0 = 0; /* disable */
|
|
|
|
SAR0 = (unsigned long)(curbuf + 1);
|
|
|
|
DAR0 = TDR1_ADDR;
|
2008-09-14 20:33:24 +00:00
|
|
|
DTCR0 = BLOCK_SIZE + 3; /* start token + block + dummy crc */
|
2006-05-07 20:20:27 +00:00
|
|
|
CHCR0 = 0x1701; /* fixed dest. address, TXI1, enable */
|
2004-10-09 01:14:55 +00:00
|
|
|
DMAOR = 0x0001;
|
|
|
|
SCR1 = (SCI_TE|SCI_TIE); /* kick off DMA */
|
|
|
|
|
2008-09-14 23:38:31 +00:00
|
|
|
if (prepare_next)
|
|
|
|
send_block_prepare();
|
2004-10-09 01:14:55 +00:00
|
|
|
yield(); /* be nice */
|
|
|
|
|
2006-05-07 20:20:27 +00:00
|
|
|
while (!(CHCR0 & 0x0002)); /* wait for end of DMA */
|
2004-10-09 01:14:55 +00:00
|
|
|
while (!(SSR1 & SCI_TEND)); /* wait for end of transfer */
|
|
|
|
SCR1 = 0;
|
|
|
|
serial_mode = SER_DISABLED;
|
|
|
|
|
|
|
|
if ((poll_busy(timeout) & 0x1F) != 0x05) /* something went wrong */
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = -1;
|
2004-10-09 01:14:55 +00:00
|
|
|
|
|
|
|
write_transfer(dummy, 1);
|
2005-05-02 00:33:01 +00:00
|
|
|
last_disk_activity = current_tick;
|
2004-10-09 01:14:55 +00:00
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
return rc;
|
2004-10-09 01:14:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
int mmc_read_sectors(IF_MD2(int drive,)
|
2005-02-04 00:58:47 +00:00
|
|
|
unsigned long start,
|
|
|
|
int incount,
|
|
|
|
void* inbuf)
|
2004-10-03 23:32:09 +00:00
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
int rc = 0;
|
2008-09-14 20:33:24 +00:00
|
|
|
int lastblock = 0;
|
|
|
|
unsigned long end_block;
|
2004-12-28 22:16:07 +00:00
|
|
|
tCardInfo *card;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2005-05-14 17:23:13 +00:00
|
|
|
int drive = current_card;
|
|
|
|
#endif
|
2005-01-03 23:20:31 +00:00
|
|
|
|
|
|
|
card = &card_info[drive];
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = select_card(drive);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 1;
|
|
|
|
goto error;
|
|
|
|
}
|
2008-09-14 20:33:24 +00:00
|
|
|
|
|
|
|
end_block = start + incount;
|
|
|
|
if (end_block > card->numblocks)
|
2005-03-10 05:43:44 +00:00
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = -2;
|
|
|
|
goto error;
|
2005-03-10 05:43:44 +00:00
|
|
|
}
|
2005-02-04 00:58:47 +00:00
|
|
|
|
2008-09-14 20:33:24 +00:00
|
|
|
/* Some cards don't like reading the very last block with
|
|
|
|
* CMD_READ_MULTIPLE_BLOCK, so make sure this block is always
|
|
|
|
* read with CMD_READ_SINGLE_BLOCK. */
|
|
|
|
if (end_block == card->numblocks)
|
|
|
|
lastblock = 1;
|
|
|
|
|
|
|
|
if (incount > 1)
|
2005-04-28 01:11:21 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
/* MMC4.2: make multiplication conditional */
|
|
|
|
if (send_cmd(CMD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE, NULL))
|
2005-04-28 01:11:21 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
rc = -3;
|
2005-04-28 01:11:21 +00:00
|
|
|
goto error;
|
2004-10-03 23:32:09 +00:00
|
|
|
}
|
2008-09-19 20:17:17 +00:00
|
|
|
while (--incount >= lastblock)
|
2004-10-03 23:32:09 +00:00
|
|
|
{
|
2008-09-14 20:33:24 +00:00
|
|
|
rc = receive_block(inbuf, card->read_timeout);
|
2005-04-28 01:11:21 +00:00
|
|
|
if (rc)
|
2004-10-03 23:32:09 +00:00
|
|
|
{
|
2008-09-15 21:10:08 +00:00
|
|
|
/* If an error occurs during multiple block reading, the
|
|
|
|
* host still needs to send CMD_STOP_TRANSMISSION */
|
2008-09-19 07:27:08 +00:00
|
|
|
send_cmd(CMD_STOP_TRANSMISSION, 0, NULL);
|
2008-09-14 20:33:24 +00:00
|
|
|
rc = rc * 10 - 4;
|
2005-04-28 01:11:21 +00:00
|
|
|
goto error;
|
2004-10-03 23:32:09 +00:00
|
|
|
}
|
2008-09-14 20:33:24 +00:00
|
|
|
inbuf += BLOCK_SIZE;
|
2008-09-14 23:38:31 +00:00
|
|
|
start++;
|
2008-09-14 20:33:24 +00:00
|
|
|
/* ^^ necessary for the abovementioned last block special case */
|
2004-10-03 23:32:09 +00:00
|
|
|
}
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_STOP_TRANSMISSION, 0, NULL))
|
2005-04-28 01:11:21 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
rc = -5;
|
2008-09-14 20:33:24 +00:00
|
|
|
goto error;
|
2005-04-28 01:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-14 20:33:24 +00:00
|
|
|
if (incount > 0)
|
2005-04-28 01:11:21 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
/* MMC4.2: make multiplication conditional */
|
|
|
|
if (send_cmd(CMD_READ_SINGLE_BLOCK, start * BLOCK_SIZE, NULL))
|
2008-09-14 20:33:24 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
rc = -6;
|
2008-09-14 20:33:24 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = receive_block(inbuf, card->read_timeout);
|
2005-04-28 01:11:21 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 7;
|
|
|
|
goto error;
|
2008-09-14 20:33:24 +00:00
|
|
|
}
|
2004-10-03 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
error:
|
|
|
|
|
2004-10-03 23:32:09 +00:00
|
|
|
deselect_card();
|
2005-02-14 23:42:32 +00:00
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
return rc;
|
2004-10-03 23:32:09 +00:00
|
|
|
}
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
int mmc_write_sectors(IF_MD2(int drive,)
|
2004-12-28 22:16:07 +00:00
|
|
|
unsigned long start,
|
2004-09-11 03:48:05 +00:00
|
|
|
int count,
|
|
|
|
const void* buf)
|
|
|
|
{
|
2005-04-28 01:11:21 +00:00
|
|
|
int rc = 0;
|
2008-09-14 20:33:24 +00:00
|
|
|
int write_cmd;
|
|
|
|
unsigned char start_token;
|
2004-12-28 22:16:07 +00:00
|
|
|
tCardInfo *card;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2005-05-14 17:23:13 +00:00
|
|
|
int drive = current_card;
|
|
|
|
#endif
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2005-01-03 23:20:31 +00:00
|
|
|
card = &card_info[drive];
|
2005-04-28 01:11:21 +00:00
|
|
|
rc = select_card(drive);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
rc = rc * 10 - 1;
|
|
|
|
goto error;
|
|
|
|
}
|
2008-09-14 20:33:24 +00:00
|
|
|
|
|
|
|
if (start + count > card->numblocks)
|
2008-03-08 23:56:00 +00:00
|
|
|
panicf("Writing past end of card");
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2008-09-14 23:38:31 +00:00
|
|
|
send_block_addr = buf;
|
|
|
|
send_block_prepare();
|
2005-04-28 20:47:55 +00:00
|
|
|
|
2008-09-14 20:33:24 +00:00
|
|
|
if (count > 1)
|
2005-04-28 20:47:55 +00:00
|
|
|
{
|
2008-09-14 20:33:24 +00:00
|
|
|
write_cmd = CMD_WRITE_MULTIPLE_BLOCK;
|
|
|
|
start_token = DT_START_WRITE_MULTIPLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_cmd = CMD_WRITE_BLOCK;
|
|
|
|
start_token = DT_START_BLOCK;
|
|
|
|
}
|
2008-09-19 07:27:08 +00:00
|
|
|
/* MMC4.2: make multiplication conditional */
|
|
|
|
if (send_cmd(write_cmd, start * BLOCK_SIZE, NULL))
|
2008-09-14 20:33:24 +00:00
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
rc = -2;
|
2008-09-14 20:33:24 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2008-09-19 20:17:17 +00:00
|
|
|
while (--count >= 0)
|
2008-09-14 20:33:24 +00:00
|
|
|
{
|
2008-09-19 20:17:17 +00:00
|
|
|
rc = send_block_send(start_token, card->write_timeout, count > 0);
|
2005-04-28 01:11:21 +00:00
|
|
|
if (rc)
|
|
|
|
{
|
2005-04-28 20:47:55 +00:00
|
|
|
rc = rc * 10 - 3;
|
2008-09-15 21:10:08 +00:00
|
|
|
break;
|
2008-09-19 20:17:17 +00:00
|
|
|
/* If an error occurs during multiple block writing,
|
|
|
|
* the STOP_TRAN token still needs to be sent. */
|
2005-04-28 01:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-14 20:33:24 +00:00
|
|
|
if (write_cmd == CMD_WRITE_MULTIPLE_BLOCK)
|
|
|
|
{
|
2008-09-19 07:27:08 +00:00
|
|
|
static const unsigned char stop_tran = DT_STOP_TRAN;
|
|
|
|
write_transfer(&stop_tran, 1);
|
2008-09-14 20:33:24 +00:00
|
|
|
poll_busy(card->write_timeout);
|
2004-09-29 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
error:
|
|
|
|
|
2004-09-29 00:50:40 +00:00
|
|
|
deselect_card();
|
2004-10-03 23:32:09 +00:00
|
|
|
|
2005-04-28 01:11:21 +00:00
|
|
|
return rc;
|
2004-09-11 03:48:05 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
bool mmc_disk_is_active(void)
|
2004-10-09 22:48:10 +00:00
|
|
|
{
|
2004-10-03 23:32:09 +00:00
|
|
|
/* this is correct unless early return from write gets implemented */
|
|
|
|
return mmc_mutex.locked;
|
2004-09-11 03:48:05 +00:00
|
|
|
}
|
|
|
|
|
2005-01-28 22:35:20 +00:00
|
|
|
static void mmc_thread(void)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2006-11-09 18:57:47 +00:00
|
|
|
bool idle_notified = false;
|
2008-09-15 21:10:08 +00:00
|
|
|
|
2005-01-28 22:35:20 +00:00
|
|
|
while (1) {
|
2006-11-08 07:32:53 +00:00
|
|
|
queue_wait_w_tmo(&mmc_queue, &ev, HZ);
|
2005-04-28 01:11:21 +00:00
|
|
|
switch ( ev.id )
|
|
|
|
{
|
2005-01-28 22:35:20 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
/* Wait until the USB cable is extracted again */
|
|
|
|
usb_wait_for_disconnect(&mmc_queue);
|
|
|
|
break;
|
|
|
|
|
2006-11-08 07:32:53 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-06-30 02:08:27 +00:00
|
|
|
case SYS_HOTSWAP_INSERTED:
|
2005-01-28 22:35:20 +00:00
|
|
|
disk_mount(1); /* mount MMC */
|
2006-12-19 17:05:20 +00:00
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
2005-01-28 22:35:20 +00:00
|
|
|
break;
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
2005-01-28 22:35:20 +00:00
|
|
|
disk_unmount(1); /* release "by force" */
|
2006-12-19 17:05:20 +00:00
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
2005-01-28 22:35:20 +00:00
|
|
|
break;
|
2006-11-08 07:32:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2006-11-09 18:57:47 +00:00
|
|
|
if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
|
2006-11-08 07:32:53 +00:00
|
|
|
{
|
2006-11-09 18:57:47 +00:00
|
|
|
idle_notified = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!idle_notified)
|
2006-11-08 07:32:53 +00:00
|
|
|
{
|
2008-11-01 16:14:28 +00:00
|
|
|
call_storage_idle_notifys(false);
|
2006-11-09 18:57:47 +00:00
|
|
|
idle_notified = true;
|
2006-11-08 07:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-01-28 22:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-16 13:25:02 +00:00
|
|
|
|
2004-10-10 19:51:11 +00:00
|
|
|
bool mmc_detect(void)
|
2004-09-11 03:48:05 +00:00
|
|
|
{
|
2010-01-01 22:47:25 +00:00
|
|
|
return (adc_read(ADC_MMC_SWITCH) < 0x200);
|
2004-10-10 00:35:19 +00:00
|
|
|
}
|
2004-09-28 06:23:57 +00:00
|
|
|
|
2005-05-17 22:10:51 +00:00
|
|
|
bool mmc_touched(void)
|
|
|
|
{
|
|
|
|
if (mmc_status == MMC_UNKNOWN) /* try to detect */
|
|
|
|
{
|
|
|
|
mutex_lock(&mmc_mutex);
|
|
|
|
setup_sci1(7); /* safe value */
|
|
|
|
and_b(~0x02, &PADRH); /* assert CS */
|
2008-09-19 07:27:08 +00:00
|
|
|
if (send_cmd(CMD_SEND_OP_COND, 0, NULL) == 0xFF)
|
2005-05-17 22:10:51 +00:00
|
|
|
mmc_status = MMC_UNTOUCHED;
|
|
|
|
else
|
|
|
|
mmc_status = MMC_TOUCHED;
|
|
|
|
|
|
|
|
deselect_card();
|
|
|
|
}
|
|
|
|
return mmc_status == MMC_TOUCHED;
|
|
|
|
}
|
|
|
|
|
2005-02-19 14:45:34 +00:00
|
|
|
bool mmc_usb_active(int delayticks)
|
|
|
|
{
|
|
|
|
/* reading "inactive" is delayed by user-supplied monoflop value */
|
|
|
|
return (usb_activity ||
|
2005-02-20 00:21:20 +00:00
|
|
|
TIME_BEFORE(current_tick, last_usb_activity + delayticks));
|
2005-02-19 14:45:34 +00:00
|
|
|
}
|
|
|
|
|
2004-10-10 00:35:19 +00:00
|
|
|
static void mmc_tick(void)
|
|
|
|
{
|
|
|
|
bool current_status;
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2005-02-20 00:21:20 +00:00
|
|
|
if (new_mmc_circuit)
|
|
|
|
/* USB bridge activity is 0 on idle, ~527 on active */
|
|
|
|
current_status = adc_read(ADC_USB_ACTIVE) > 0x100;
|
|
|
|
else
|
|
|
|
current_status = adc_read(ADC_USB_ACTIVE) < 0x190;
|
|
|
|
|
2005-02-19 14:45:34 +00:00
|
|
|
if (!current_status && usb_activity)
|
2005-02-20 00:21:20 +00:00
|
|
|
last_usb_activity = current_tick;
|
2005-02-19 14:45:34 +00:00
|
|
|
usb_activity = current_status;
|
|
|
|
|
2010-06-06 13:20:47 +00:00
|
|
|
current_status = mmc_detect();
|
|
|
|
/* Only report when the status has changed */
|
|
|
|
if (current_status != last_mmc_status)
|
2004-10-10 00:35:19 +00:00
|
|
|
{
|
2010-06-06 13:20:47 +00:00
|
|
|
last_mmc_status = current_status;
|
|
|
|
countdown = HZ/3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Count down until it gets negative */
|
|
|
|
if (countdown >= 0)
|
|
|
|
countdown--;
|
2005-05-16 13:25:02 +00:00
|
|
|
|
2010-06-06 13:20:47 +00:00
|
|
|
if (countdown == 0)
|
|
|
|
{
|
|
|
|
if (current_status)
|
2004-10-10 00:35:19 +00:00
|
|
|
{
|
2010-06-06 13:20:47 +00:00
|
|
|
queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
|
|
|
|
mmc_status = MMC_UNTOUCHED;
|
|
|
|
card_info[1].initialized = false;
|
2004-10-10 00:35:19 +00:00
|
|
|
}
|
2004-09-11 03:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
void mmc_enable(bool on)
|
2004-09-11 03:48:05 +00:00
|
|
|
{
|
2008-09-17 18:53:11 +00:00
|
|
|
PBCR1 &= ~0x0CF0; /* PB13, PB11 and PB10 become GPIO,
|
|
|
|
* if not modified below */
|
2004-09-11 15:18:10 +00:00
|
|
|
if (on)
|
2008-09-17 18:53:11 +00:00
|
|
|
PBCR1 |= 0x08A0; /* as SCK1, TxD1, RxD1 */
|
|
|
|
|
|
|
|
and_b(~0x80, &PADRL); /* assert flash reset */
|
|
|
|
sleep(HZ/100);
|
|
|
|
or_b(0x80, &PADRL); /* de-assert flash reset */
|
|
|
|
sleep(HZ/100);
|
2004-09-29 00:50:40 +00:00
|
|
|
card_info[0].initialized = false;
|
|
|
|
card_info[1].initialized = false;
|
2004-09-11 03:48:05 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
int mmc_init(void)
|
2004-09-11 03:48:05 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
2008-01-20 12:44:13 +00:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
mutex_init(&mmc_mutex);
|
|
|
|
queue_init(&mmc_queue, true);
|
|
|
|
}
|
|
|
|
mutex_lock(&mmc_mutex);
|
2004-09-11 03:48:05 +00:00
|
|
|
led(false);
|
|
|
|
|
2004-10-10 00:35:19 +00:00
|
|
|
last_mmc_status = mmc_detect();
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2008-09-17 18:53:11 +00:00
|
|
|
/* Use MMC if inserted, internal flash otherwise */
|
|
|
|
current_card = last_mmc_status ? 1 : 0;
|
2005-01-03 23:20:31 +00:00
|
|
|
#endif
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2008-09-17 18:53:11 +00:00
|
|
|
if (!initialized)
|
2004-10-10 00:35:19 +00:00
|
|
|
{
|
2005-05-17 22:10:51 +00:00
|
|
|
if (!last_mmc_status)
|
|
|
|
mmc_status = MMC_UNTOUCHED;
|
2006-11-08 07:32:53 +00:00
|
|
|
|
2008-09-17 18:53:11 +00:00
|
|
|
/* Port setup */
|
|
|
|
PACR1 &= ~0x0F3C; /* GPIO function for PA13 (flash busy), PA12
|
|
|
|
* (clk gate), PA10 (flash CS), PA9 (MMC CS) */
|
|
|
|
PACR2 &= ~0x4000; /* GPIO for PA7 (flash reset) */
|
|
|
|
PADR |= 0x0680; /* set all the selects + reset high (=inactive) */
|
|
|
|
PAIOR |= 0x1680; /* make outputs for them and the PA12 clock gate */
|
|
|
|
|
|
|
|
PBCR1 &= ~0x0CF0; /* GPIO function for PB13, PB11 and PB10 */
|
|
|
|
PBDR |= 0x2C00; /* SCK1, TxD1 and RxD1 high in GPIO */
|
|
|
|
PBIOR |= 0x2000; /* SCK1 output */
|
|
|
|
PBIOR &= ~0x0C00; /* TxD1, RxD1 input */
|
|
|
|
|
|
|
|
IPRE &= 0x0FFF; /* disable SCI1 interrupts for the CPU */
|
|
|
|
|
|
|
|
new_mmc_circuit = ((HW_MASK & MMC_CLOCK_POLARITY) != 0);
|
|
|
|
|
2005-01-28 22:35:20 +00:00
|
|
|
create_thread(mmc_thread, mmc_stack,
|
2007-10-16 01:25:17 +00:00
|
|
|
sizeof(mmc_stack), 0, mmc_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_SYSTEM)
|
2009-07-17 22:28:49 +00:00
|
|
|
IF_COP(, CPU));
|
2004-10-10 00:35:19 +00:00
|
|
|
tick_add_task(mmc_tick);
|
2004-09-11 03:48:05 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
2008-11-01 16:14:28 +00:00
|
|
|
mmc_enable(true);
|
2004-09-11 03:48:05 +00:00
|
|
|
|
2008-01-20 12:44:13 +00:00
|
|
|
mutex_unlock(&mmc_mutex);
|
2004-09-11 03:48:05 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
long mmc_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:33:21 +00:00
|
|
|
#ifdef STORAGE_GET_INFO
|
2009-07-17 22:28:49 +00:00
|
|
|
void mmc_get_info(IF_MD2(int drive,) struct storage_info *info)
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2008-11-01 16:14:28 +00:00
|
|
|
const int drive=0;
|
|
|
|
#endif
|
|
|
|
info->sector_size=card_info[drive].blocksize;
|
|
|
|
info->num_sectors=card_info[drive].numblocks;
|
|
|
|
info->vendor="Rockbox";
|
|
|
|
if(drive==0)
|
|
|
|
{
|
|
|
|
info->product="Internal Storage";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->product="MMC Card Slot";
|
|
|
|
}
|
|
|
|
info->revision="0.00";
|
|
|
|
}
|
2008-11-01 17:33:21 +00:00
|
|
|
#endif
|
2008-11-01 16:14:28 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
2009-07-17 22:28:49 +00:00
|
|
|
bool mmc_removable(IF_MD_NONVOID(int drive))
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2008-11-01 16:14:28 +00:00
|
|
|
const int drive=0;
|
|
|
|
#endif
|
|
|
|
return (drive==1);
|
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
bool mmc_present(IF_MD_NONVOID(int drive))
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2008-11-01 16:14:28 +00:00
|
|
|
const int drive=0;
|
|
|
|
#endif
|
2009-07-20 13:47:21 +00:00
|
|
|
if(drive==0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return mmc_detect();
|
|
|
|
}
|
2008-11-01 16:14:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-02 01:14:46 +00:00
|
|
|
|
|
|
|
void mmc_sleep(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void mmc_spin(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void mmc_spindown(int seconds)
|
|
|
|
{
|
|
|
|
(void)seconds;
|
|
|
|
}
|
2009-07-17 22:28:49 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
int mmc_num_drives(int first_drive)
|
|
|
|
{
|
|
|
|
/* We don't care which logical drive number(s) we have been assigned */
|
|
|
|
(void)first_drive;
|
|
|
|
|
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
return 2;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|