2008-10-29 20:21:59 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-11-09 06:17:21 +00:00
|
|
|
* Copyright (C) 2006 Daniel Ankers
|
2009-03-30 13:26:05 +00:00
|
|
|
* Copyright © 2008-2009 Rafaël Carré
|
2008-10-29 20:21:59 +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.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver for the ARM PL180 SD/MMC controller inside AS3525 SoC */
|
|
|
|
|
2009-03-30 13:26:05 +00:00
|
|
|
/* TODO: Find the real capacity of >2GB models (will be useful for USB) */
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#include "config.h" /* for HAVE_MULTIDRIVE & AMS_OF_SIZE */
|
2008-11-09 06:17:21 +00:00
|
|
|
#include "fat.h"
|
|
|
|
#include "thread.h"
|
2009-07-06 16:44:57 +00:00
|
|
|
#include "led.h"
|
2008-11-09 06:17:21 +00:00
|
|
|
#include "hotswap.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "cpu.h"
|
2008-11-16 22:26:50 +00:00
|
|
|
#include <stdio.h>
|
2008-11-09 06:17:21 +00:00
|
|
|
#include <stdlib.h>
|
2008-11-16 22:26:50 +00:00
|
|
|
#include <string.h>
|
2008-10-29 20:21:59 +00:00
|
|
|
#include "as3525.h"
|
2008-11-25 13:38:32 +00:00
|
|
|
#include "pl180.h" /* SD controller */
|
|
|
|
#include "pl081.h" /* DMA controller */
|
|
|
|
#include "dma-target.h" /* DMA request lines */
|
2008-12-04 20:04:31 +00:00
|
|
|
#include "clock-target.h"
|
2009-07-11 15:26:58 +00:00
|
|
|
#include "panic.h"
|
2009-06-18 22:33:02 +00:00
|
|
|
#ifdef HAVE_BUTTON_LIGHT
|
|
|
|
#include "backlight-target.h"
|
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
#include "stdbool.h"
|
2008-11-09 06:17:21 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2008-11-01 22:55:35 +00:00
|
|
|
#include "sd.h"
|
2009-07-06 14:20:52 +00:00
|
|
|
#include "usb.h"
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
#include "disk.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* command flags */
|
2008-11-16 22:26:50 +00:00
|
|
|
#define MCI_NO_FLAGS (0<<0)
|
|
|
|
#define MCI_RESP (1<<0)
|
|
|
|
#define MCI_LONG_RESP (1<<1)
|
|
|
|
#define MCI_ARG (1<<2)
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
/* ARM PL180 registers */
|
2008-11-16 22:26:50 +00:00
|
|
|
#define MCI_POWER(i) (*(volatile unsigned char *) (pl180_base[i]+0x00))
|
|
|
|
#define MCI_CLOCK(i) (*(volatile unsigned long *) (pl180_base[i]+0x04))
|
|
|
|
#define MCI_ARGUMENT(i) (*(volatile unsigned long *) (pl180_base[i]+0x08))
|
|
|
|
#define MCI_COMMAND(i) (*(volatile unsigned long *) (pl180_base[i]+0x0C))
|
|
|
|
#define MCI_RESPCMD(i) (*(volatile unsigned long *) (pl180_base[i]+0x10))
|
|
|
|
#define MCI_RESP0(i) (*(volatile unsigned long *) (pl180_base[i]+0x14))
|
|
|
|
#define MCI_RESP1(i) (*(volatile unsigned long *) (pl180_base[i]+0x18))
|
|
|
|
#define MCI_RESP2(i) (*(volatile unsigned long *) (pl180_base[i]+0x1C))
|
|
|
|
#define MCI_RESP3(i) (*(volatile unsigned long *) (pl180_base[i]+0x20))
|
|
|
|
#define MCI_DATA_TIMER(i) (*(volatile unsigned long *) (pl180_base[i]+0x24))
|
|
|
|
#define MCI_DATA_LENGTH(i) (*(volatile unsigned short*) (pl180_base[i]+0x28))
|
|
|
|
#define MCI_DATA_CTRL(i) (*(volatile unsigned char *) (pl180_base[i]+0x2C))
|
|
|
|
#define MCI_DATA_CNT(i) (*(volatile unsigned short*) (pl180_base[i]+0x30))
|
|
|
|
#define MCI_STATUS(i) (*(volatile unsigned long *) (pl180_base[i]+0x34))
|
|
|
|
#define MCI_CLEAR(i) (*(volatile unsigned long *) (pl180_base[i]+0x38))
|
|
|
|
#define MCI_MASK0(i) (*(volatile unsigned long *) (pl180_base[i]+0x3C))
|
|
|
|
#define MCI_MASK1(i) (*(volatile unsigned long *) (pl180_base[i]+0x40))
|
|
|
|
#define MCI_SELECT(i) (*(volatile unsigned long *) (pl180_base[i]+0x44))
|
|
|
|
#define MCI_FIFO_CNT(i) (*(volatile unsigned long *) (pl180_base[i]+0x48))
|
|
|
|
|
2009-01-08 08:27:42 +00:00
|
|
|
#define MCI_ERROR \
|
|
|
|
(MCI_DATA_CRC_FAIL | MCI_DATA_TIMEOUT | MCI_RX_OVERRUN | MCI_TX_UNDERRUN)
|
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
#define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
|
2008-11-09 06:17:21 +00:00
|
|
|
/* volumes */
|
2008-11-25 13:38:32 +00:00
|
|
|
#define INTERNAL_AS3525 0 /* embedded SD card */
|
|
|
|
#define SD_SLOT_AS3525 1 /* SD slot if present */
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
static const int pl180_base[NUM_DRIVES] = {
|
2008-11-09 06:17:21 +00:00
|
|
|
NAND_FLASH_BASE
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2008-11-09 06:17:21 +00:00
|
|
|
, SD_MCI_BASE
|
|
|
|
#endif
|
|
|
|
};
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-11 03:11:13 +00:00
|
|
|
static int sd_wait_for_state(const int drive, unsigned int state);
|
2009-03-30 13:26:05 +00:00
|
|
|
static int sd_select_bank(signed char bank);
|
2008-12-15 23:37:16 +00:00
|
|
|
static int sd_init_card(const int drive);
|
|
|
|
static void init_pl180_controller(const int drive);
|
2009-11-25 03:24:28 +00:00
|
|
|
|
2009-03-30 13:26:05 +00:00
|
|
|
#define BLOCKS_PER_BANK 0x7a7800
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
static tCardInfo card_info[NUM_DRIVES];
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-26 07:06:37 +00:00
|
|
|
/* maximum timeouts recommanded in the SD Specification v2.00 */
|
|
|
|
#define SD_MAX_READ_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 100) /* 100 ms */
|
|
|
|
#define SD_MAX_WRITE_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 250) /* 250 ms */
|
2009-06-11 17:17:12 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
/* for compatibility */
|
|
|
|
static long last_disk_activity = -1;
|
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
#define MIN_YIELD_PERIOD 5 /* ticks */
|
2008-11-09 06:17:21 +00:00
|
|
|
static long next_yield = 0;
|
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
|
2008-11-09 06:17:21 +00:00
|
|
|
static const char sd_thread_name[] = "ata/sd";
|
2009-06-02 22:45:02 +00:00
|
|
|
static struct mutex sd_mtx;
|
2008-11-09 06:17:21 +00:00
|
|
|
static struct event_queue sd_queue;
|
2008-12-15 23:47:17 +00:00
|
|
|
#ifndef BOOTLOADER
|
2009-12-01 21:23:11 +00:00
|
|
|
bool sd_enabled = false;
|
2008-12-15 23:47:17 +00:00
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-01-08 08:27:42 +00:00
|
|
|
static struct wakeup transfer_completion_signal;
|
2009-07-11 14:27:26 +00:00
|
|
|
static volatile unsigned int transfer_error[NUM_VOLUMES];
|
|
|
|
#define PL180_MAX_TRANSFER_ERRORS 10
|
2009-01-08 08:27:42 +00:00
|
|
|
|
2009-06-29 18:14:26 +00:00
|
|
|
#define UNALIGNED_NUM_SECTORS 10
|
2009-11-25 03:24:28 +00:00
|
|
|
static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS* SD_BLOCK_SIZE] __attribute__((aligned(32))); /* align on cache line size */
|
2009-06-29 18:14:26 +00:00
|
|
|
static unsigned char *uncached_buffer = UNCACHED_ADDR(&aligned_buffer[0]);
|
|
|
|
|
2009-11-15 17:23:25 +00:00
|
|
|
static inline void mci_delay(void)
|
|
|
|
{
|
|
|
|
int i = 0xffff;
|
|
|
|
do {
|
|
|
|
asm volatile("nop\n");
|
|
|
|
} while (--i);
|
|
|
|
}
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-11-26 00:51:09 +00:00
|
|
|
|
|
|
|
static inline bool card_detect_target(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
|
|
|
return !(GPIOA_PIN(2));
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-15 23:37:16 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2009-01-21 02:44:20 +00:00
|
|
|
static int sd1_oneshot_callback(struct timeout *tmo)
|
2008-12-15 23:37:16 +00:00
|
|
|
{
|
|
|
|
(void)tmo;
|
|
|
|
|
|
|
|
/* This is called only if the state was stable for 300ms - check state
|
|
|
|
* and post appropriate event. */
|
|
|
|
if (card_detect_target())
|
|
|
|
{
|
|
|
|
queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
|
|
|
|
|
2009-01-21 02:44:20 +00:00
|
|
|
return 0;
|
2008-12-15 23:37:16 +00:00
|
|
|
}
|
2009-01-08 08:27:42 +00:00
|
|
|
|
2008-12-15 23:37:16 +00:00
|
|
|
void INT_GPIOA(void)
|
|
|
|
{
|
|
|
|
static struct timeout sd1_oneshot;
|
2009-10-15 07:16:38 +00:00
|
|
|
/* acknowledge interrupt */
|
2008-12-15 23:37:16 +00:00
|
|
|
GPIOA_IC = (1<<2);
|
|
|
|
timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
|
|
|
|
}
|
2009-05-26 21:08:47 +00:00
|
|
|
#endif /* HAVE_HOTSWAP */
|
2008-12-15 23:37:16 +00:00
|
|
|
|
2008-12-04 20:48:19 +00:00
|
|
|
void INT_NAND(void)
|
|
|
|
{
|
2009-01-08 08:27:42 +00:00
|
|
|
const int status = MCI_STATUS(INTERNAL_AS3525);
|
|
|
|
|
2009-07-11 14:27:26 +00:00
|
|
|
transfer_error[INTERNAL_AS3525] = status & MCI_ERROR;
|
2009-01-08 08:27:42 +00:00
|
|
|
|
|
|
|
wakeup_signal(&transfer_completion_signal);
|
|
|
|
MCI_CLEAR(INTERNAL_AS3525) = status;
|
2008-12-04 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2008-12-04 20:48:19 +00:00
|
|
|
void INT_MCI0(void)
|
|
|
|
{
|
2009-01-08 08:27:42 +00:00
|
|
|
const int status = MCI_STATUS(SD_SLOT_AS3525);
|
|
|
|
|
2009-07-11 14:27:26 +00:00
|
|
|
transfer_error[SD_SLOT_AS3525] = status & MCI_ERROR;
|
2009-01-08 08:27:42 +00:00
|
|
|
|
|
|
|
wakeup_signal(&transfer_completion_signal);
|
|
|
|
MCI_CLEAR(SD_SLOT_AS3525) = status;
|
2008-12-04 20:48:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static bool send_cmd(const int drive, const int cmd, const int arg,
|
2009-07-01 10:07:22 +00:00
|
|
|
const int flags, long *response)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2009-11-22 04:50:26 +00:00
|
|
|
int status;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
/* Clear old status flags */
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_CLEAR(drive) = 0x7ff;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
/* Load command argument or clear if none */
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_ARGUMENT(drive) = (flags & MCI_ARG) ? arg : 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
/* Construct MCI_COMMAND & enable CPSM */
|
|
|
|
MCI_COMMAND(drive) =
|
|
|
|
/*b0:5*/ cmd
|
|
|
|
/* b6 */| ((flags & (MCI_RESP|MCI_LONG_RESP)) ? MCI_COMMAND_RESPONSE : 0)
|
|
|
|
/* b7 */| ((flags & MCI_LONG_RESP) ? MCI_COMMAND_LONG_RESPONSE : 0)
|
|
|
|
/* b8 | MCI_COMMAND_INTERRUPT */
|
|
|
|
/* b9 | MCI_COMMAND_PENDING */ /*Only used with stream data transfer*/
|
|
|
|
/* b10*/| MCI_COMMAND_ENABLE; /* Enables CPSM */
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
/* Wait while cmd completes then disable CPSM */
|
|
|
|
while(MCI_STATUS(drive) & MCI_CMD_ACTIVE);
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_COMMAND(drive) = 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
status = MCI_STATUS(drive);
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-11-23 04:22:11 +00:00
|
|
|
/* Handle command responses */
|
|
|
|
if(flags & MCI_RESP) /* CMD expects response */
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2009-11-23 04:22:11 +00:00
|
|
|
response[0] = MCI_RESP0(drive); /* Always prepare short response */
|
|
|
|
|
|
|
|
if(status & (MCI_CMD_TIMEOUT | MCI_CMD_CRC_FAIL)) /* failed response */
|
2008-11-09 06:17:21 +00:00
|
|
|
return false;
|
2009-11-23 04:22:11 +00:00
|
|
|
|
|
|
|
if(status & MCI_CMD_RESP_END) /*Response passed CRC check */
|
|
|
|
{
|
2008-11-16 22:26:50 +00:00
|
|
|
if(flags & MCI_LONG_RESP)
|
2009-11-23 04:22:11 +00:00
|
|
|
{ /* replace short response with long response */
|
2009-05-26 21:08:47 +00:00
|
|
|
/* store the response in reverse words order */
|
2008-11-16 22:26:50 +00:00
|
|
|
response[0] = MCI_RESP3(drive);
|
|
|
|
response[1] = MCI_RESP2(drive);
|
|
|
|
response[2] = MCI_RESP1(drive);
|
|
|
|
response[3] = MCI_RESP0(drive);
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
return true;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
2009-11-23 04:22:11 +00:00
|
|
|
else if(status & MCI_CMD_SENT) /* CMD sent, no response required */
|
2008-11-09 06:17:21 +00:00
|
|
|
return true;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
return false;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 17:22:09 +00:00
|
|
|
#define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
|
|
|
|
#define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
|
|
|
|
#define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
|
|
|
|
#define MCI_IDENTSPEED (MCI_CLOCK_ENABLE | AS3525_SD_IDENT_DIV) /* IDENT */
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static int sd_init_card(const int drive)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2009-07-01 10:07:22 +00:00
|
|
|
unsigned long response;
|
2009-07-10 12:51:50 +00:00
|
|
|
long init_timeout;
|
2009-09-30 20:33:40 +00:00
|
|
|
bool sd_v2 = false;
|
2009-07-01 21:49:13 +00:00
|
|
|
unsigned long temp_reg[4];
|
|
|
|
int i;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
|
2009-12-03 17:22:03 +00:00
|
|
|
/* MCLCK on and set to 400kHz ident frequency */
|
2009-12-03 17:22:09 +00:00
|
|
|
MCI_CLOCK(drive) = MCI_IDENTSPEED;
|
2009-10-19 05:34:25 +00:00
|
|
|
|
2009-12-03 17:22:03 +00:00
|
|
|
/* 100 - 400kHz clock required for Identification Mode */
|
2009-10-19 05:34:25 +00:00
|
|
|
/* Start of Card Identification Mode ************************************/
|
|
|
|
|
|
|
|
/* CMD0 Go Idle */
|
2008-11-16 22:26:50 +00:00
|
|
|
if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_FLAGS, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -1;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
mci_delay();
|
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD8 Check for v2 sd card. Must be sent before using ACMD41
|
|
|
|
Non v2 cards will not respond to this command*/
|
2008-11-16 22:26:50 +00:00
|
|
|
if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP|MCI_ARG, &response))
|
2008-11-09 06:17:21 +00:00
|
|
|
if((response & 0xFFF) == 0x1AA)
|
2009-09-30 20:33:40 +00:00
|
|
|
sd_v2 = true;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-07-10 12:51:50 +00:00
|
|
|
/* timeout for initialization is 1sec, from SD Specification 2.00 */
|
|
|
|
init_timeout = current_tick + HZ;
|
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
do {
|
2009-11-23 04:22:11 +00:00
|
|
|
/* this timeout is the only valid error for this loop*/
|
2009-10-18 15:50:30 +00:00
|
|
|
if(TIME_AFTER(current_tick, init_timeout))
|
2009-07-10 12:51:50 +00:00
|
|
|
return -2;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
/* app_cmd */
|
2009-11-23 04:22:11 +00:00
|
|
|
send_cmd(drive, SD_APP_CMD, 0, MCI_RESP|MCI_ARG, &response);
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
|
2009-11-23 04:22:11 +00:00
|
|
|
send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
|
|
|
|
MCI_RESP|MCI_ARG, &card_info[drive].ocr);
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-07-10 12:51:50 +00:00
|
|
|
} while(!(card_info[drive].ocr & (1<<31)));
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD2 send CID */
|
2008-11-16 22:26:50 +00:00
|
|
|
if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP|MCI_ARG,
|
2009-07-01 21:49:13 +00:00
|
|
|
temp_reg))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -3;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-07-01 21:49:13 +00:00
|
|
|
for(i=0; i<4; i++)
|
|
|
|
card_info[drive].cid[3-i] = temp_reg[i];
|
2009-07-01 10:07:22 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD3 send RCA */
|
2008-11-16 22:26:50 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP|MCI_ARG,
|
2008-11-09 06:17:21 +00:00
|
|
|
&card_info[drive].rca))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -4;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* End of Card Identification Mode ************************************/
|
|
|
|
|
2009-12-03 17:22:09 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE /* The internal SDs are v1 */
|
2009-10-19 05:34:25 +00:00
|
|
|
|
2009-10-12 18:55:10 +00:00
|
|
|
/* Try to switch V2 cards to HS timings, non HS seem to ignore this */
|
|
|
|
if(sd_v2)
|
|
|
|
{
|
2009-10-24 16:42:24 +00:00
|
|
|
/* CMD7 w/rca: Select card to put it in TRAN state */
|
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_ARG, NULL))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -5;
|
2009-12-06 07:32:40 +00:00
|
|
|
mci_delay();
|
2009-10-24 16:42:24 +00:00
|
|
|
|
2009-10-12 18:55:10 +00:00
|
|
|
if(sd_wait_for_state(drive, SD_TRAN))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -6;
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD6 */
|
2009-10-12 18:55:10 +00:00
|
|
|
if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_ARG, NULL))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -7;
|
2009-10-12 18:55:10 +00:00
|
|
|
mci_delay();
|
|
|
|
|
2009-10-24 16:42:24 +00:00
|
|
|
/* go back to STBY state so we can read csd */
|
|
|
|
/* CMD7 w/rca=0: Deselect card to put it in STBY state */
|
|
|
|
if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_ARG, NULL))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -8;
|
2009-10-24 16:42:24 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_MULTIDRIVE */
|
2009-10-12 18:55:10 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD9 send CSD */
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca,
|
2009-07-01 21:49:13 +00:00
|
|
|
MCI_RESP|MCI_LONG_RESP|MCI_ARG, temp_reg))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -9;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-07-01 21:49:13 +00:00
|
|
|
for(i=0; i<4; i++)
|
|
|
|
card_info[drive].csd[3-i] = temp_reg[i];
|
|
|
|
|
|
|
|
sd_parse_csd(&card_info[drive]);
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-12-08 20:26:31 +00:00
|
|
|
/* Boost MCICLK to operating speed */
|
|
|
|
if(drive == INTERNAL_AS3525)
|
|
|
|
MCI_CLOCK(drive) = MCI_QUARTERSPEED; /* MCICLK = PCLK/4 = 15.5MHz */
|
|
|
|
else
|
|
|
|
/* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
|
|
|
|
MCI_CLOCK(drive) = ((card_info[drive].speed == 50000000) ?
|
|
|
|
MCI_HALFSPEED : MCI_QUARTERSPEED);
|
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
/* CMD7 w/rca: Select card to put it in TRAN state */
|
2008-11-16 22:26:50 +00:00
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_ARG, NULL))
|
2009-11-23 04:22:11 +00:00
|
|
|
return -10;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-03-30 13:26:05 +00:00
|
|
|
/*
|
2009-11-23 04:22:11 +00:00
|
|
|
* enable bank switching
|
2009-03-30 13:26:05 +00:00
|
|
|
* without issuing this command, we only have access to 1/4 of the blocks
|
|
|
|
* of the first bank (0x1E9E00 blocks, which is the size reported in the
|
|
|
|
* CSD register)
|
|
|
|
*/
|
|
|
|
if(drive == INTERNAL_AS3525)
|
|
|
|
{
|
|
|
|
const int ret = sd_select_bank(-1);
|
|
|
|
if(ret < 0)
|
2009-11-23 04:22:11 +00:00
|
|
|
return ret - 11;
|
2009-03-30 13:26:05 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 18:55:10 +00:00
|
|
|
card_info[drive].initialized = 1;
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
return 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static void sd_thread(void) __attribute__((noreturn));
|
|
|
|
static void sd_thread(void)
|
|
|
|
{
|
|
|
|
struct queue_event ev;
|
|
|
|
bool idle_notified = false;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
queue_wait_w_tmo(&sd_queue, &ev, HZ);
|
|
|
|
|
|
|
|
switch ( ev.id )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
case SYS_HOTSWAP_INSERTED:
|
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
2009-07-10 15:32:02 +00:00
|
|
|
{
|
|
|
|
int microsd_init = 1;
|
2008-11-09 06:17:21 +00:00
|
|
|
fat_lock(); /* lock-out FAT activity first -
|
|
|
|
prevent deadlocking via disk_mount that
|
|
|
|
would cause a reverse-order attempt with
|
|
|
|
another thread */
|
|
|
|
mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
|
|
|
|
into driver that bypass the fat cache */
|
|
|
|
|
|
|
|
/* We now have exclusive control of fat cache and ata */
|
|
|
|
|
2009-01-16 01:14:58 +00:00
|
|
|
disk_unmount(SD_SLOT_AS3525); /* release "by force", ensure file
|
2008-11-09 06:17:21 +00:00
|
|
|
descriptors aren't leaked and any busy
|
|
|
|
ones are invalid if mounting */
|
|
|
|
|
|
|
|
/* Force card init for new card, re-init for re-inserted one or
|
|
|
|
* clear if the last attempt to init failed with an error. */
|
2009-01-16 01:14:58 +00:00
|
|
|
card_info[SD_SLOT_AS3525].initialized = 0;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
|
|
|
if (ev.id == SYS_HOTSWAP_INSERTED)
|
2008-12-15 23:37:16 +00:00
|
|
|
{
|
2008-12-16 02:04:08 +00:00
|
|
|
sd_enable(true);
|
2008-12-15 23:37:16 +00:00
|
|
|
init_pl180_controller(SD_SLOT_AS3525);
|
2009-07-10 12:51:50 +00:00
|
|
|
microsd_init = sd_init_card(SD_SLOT_AS3525);
|
2009-07-10 15:32:02 +00:00
|
|
|
if (microsd_init < 0) /* initialisation failed */
|
2009-07-10 12:51:50 +00:00
|
|
|
panicf("microSD init failed : %d", microsd_init);
|
|
|
|
|
2009-07-10 15:32:02 +00:00
|
|
|
microsd_init = disk_mount(SD_SLOT_AS3525); /* 0 if fail */
|
2008-12-15 23:37:16 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-07-10 15:32:02 +00:00
|
|
|
/*
|
|
|
|
* Mount succeeded, or this was an EXTRACTED event,
|
2009-07-10 15:44:38 +00:00
|
|
|
* in both cases notify the system about the changed filesystems
|
|
|
|
*/
|
2009-07-10 15:32:02 +00:00
|
|
|
if (microsd_init)
|
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
2008-11-09 06:17:21 +00:00
|
|
|
|
|
|
|
/* Access is now safe */
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
fat_unlock();
|
2009-01-16 01:14:58 +00:00
|
|
|
sd_enable(false);
|
2009-07-10 15:32:02 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case SYS_TIMEOUT:
|
|
|
|
if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
|
|
|
|
{
|
|
|
|
idle_notified = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* never let a timer wrap confuse us */
|
|
|
|
next_yield = current_tick;
|
|
|
|
|
|
|
|
if (!idle_notified)
|
|
|
|
{
|
|
|
|
call_storage_idle_notifys(false);
|
|
|
|
idle_notified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-07-06 14:20:52 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
/* Wait until the USB cable is extracted again */
|
|
|
|
usb_wait_for_disconnect(&sd_queue);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SYS_USB_DISCONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 08:27:42 +00:00
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
static void init_pl180_controller(const int drive)
|
|
|
|
{
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_COMMAND(drive) = MCI_DATA_CTRL(drive) = 0;
|
|
|
|
MCI_CLEAR(drive) = 0x7ff;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
MCI_MASK0(drive) = MCI_ERROR | MCI_DATA_END;
|
|
|
|
MCI_MASK1(drive) = 0;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2009-11-24 12:05:53 +00:00
|
|
|
VIC_INT_ENABLE =
|
2008-12-04 21:55:09 +00:00
|
|
|
(drive == INTERNAL_AS3525) ? INTERRUPT_NAND : INTERRUPT_MCI0;
|
2008-12-15 23:37:16 +00:00
|
|
|
|
|
|
|
/* setup isr for microsd monitoring */
|
2009-11-24 12:05:53 +00:00
|
|
|
VIC_INT_ENABLE = (INTERRUPT_GPIOA);
|
2008-12-15 23:37:16 +00:00
|
|
|
/* clear previous irq */
|
2009-06-11 20:43:53 +00:00
|
|
|
GPIOA_IC = (1<<2);
|
2008-12-15 23:37:16 +00:00
|
|
|
/* enable edge detecting */
|
|
|
|
GPIOA_IS &= ~(1<<2);
|
|
|
|
/* detect both raising and falling edges */
|
|
|
|
GPIOA_IBE |= (1<<2);
|
2009-01-16 01:14:58 +00:00
|
|
|
|
2008-12-04 21:55:09 +00:00
|
|
|
#else
|
2009-11-24 12:05:53 +00:00
|
|
|
VIC_INT_ENABLE = INTERRUPT_NAND;
|
2008-12-04 20:48:19 +00:00
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-10-19 05:34:25 +00:00
|
|
|
MCI_POWER(drive) = MCI_POWER_UP | (MCI_VDD_3_0); /* OF Setting */
|
2008-10-29 20:21:59 +00:00
|
|
|
mci_delay();
|
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_POWER(drive) |= MCI_POWER_ON;
|
2008-10-29 20:21:59 +00:00
|
|
|
mci_delay();
|
|
|
|
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_SELECT(drive) = 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2009-12-03 17:22:03 +00:00
|
|
|
/* Pl180 clocks get turned on at start of card init */
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 22:55:35 +00:00
|
|
|
int sd_init(void)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
int ret;
|
2009-12-03 20:06:46 +00:00
|
|
|
CGU_IDE = (1<<6) /* enable non AHB interface*/
|
|
|
|
| (AS3525_IDE_DIV << 2)
|
|
|
|
| AS3525_CLK_PLLA; /* clock source = PLLA */
|
2009-01-16 01:14:58 +00:00
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2008-10-29 20:21:59 +00:00
|
|
|
CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
|
2009-01-16 01:14:58 +00:00
|
|
|
CCU_IO &= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
|
|
|
|
CCU_IO |= (1<<2);
|
2008-10-29 20:21:59 +00:00
|
|
|
#endif
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-01-08 08:27:42 +00:00
|
|
|
wakeup_init(&transfer_completion_signal);
|
|
|
|
|
2008-11-25 13:38:32 +00:00
|
|
|
init_pl180_controller(INTERNAL_AS3525);
|
|
|
|
ret = sd_init_card(INTERNAL_AS3525);
|
2008-11-09 06:17:21 +00:00
|
|
|
if(ret < 0)
|
|
|
|
return ret;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2008-11-25 13:38:32 +00:00
|
|
|
init_pl180_controller(SD_SLOT_AS3525);
|
2008-12-01 08:18:22 +00:00
|
|
|
#endif
|
2008-12-01 04:17:39 +00:00
|
|
|
|
2009-01-16 01:14:58 +00:00
|
|
|
/* init mutex */
|
2008-11-30 11:38:26 +00:00
|
|
|
mutex_init(&sd_mtx);
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
queue_init(&sd_queue, true);
|
|
|
|
create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
|
|
|
|
sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
|
|
|
|
|
2009-01-16 01:14:58 +00:00
|
|
|
#ifndef BOOTLOADER
|
|
|
|
sd_enabled = true;
|
|
|
|
sd_enable(false);
|
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2009-07-17 22:28:49 +00:00
|
|
|
bool sd_removable(IF_MD_NONVOID(int drive))
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
|
|
|
return (drive==1);
|
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
bool sd_present(IF_MD_NONVOID(int drive))
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
2009-10-15 07:16:38 +00:00
|
|
|
return (drive == 0) ? true : card_detect_target();
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* HAVE_HOTSWAP */
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static int sd_wait_for_state(const int drive, unsigned int state)
|
|
|
|
{
|
2009-07-01 10:07:22 +00:00
|
|
|
unsigned long response = 0;
|
2009-11-24 17:43:53 +00:00
|
|
|
unsigned int timeout = current_tick + 100; /* 100 ticks timeout */
|
2008-11-09 06:17:21 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
|
2008-11-16 22:26:50 +00:00
|
|
|
MCI_RESP|MCI_ARG, &response))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (((response >> 9) & 0xf) == state)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-24 17:43:53 +00:00
|
|
|
if(TIME_AFTER(current_tick, timeout))
|
2009-03-30 13:26:05 +00:00
|
|
|
return -2;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-11-24 17:43:53 +00:00
|
|
|
if (TIME_AFTER(current_tick, next_yield))
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
|
|
|
yield();
|
2009-11-24 17:43:53 +00:00
|
|
|
next_yield = current_tick + MIN_YIELD_PERIOD;
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-30 13:26:05 +00:00
|
|
|
static int sd_select_bank(signed char bank)
|
|
|
|
{
|
|
|
|
int ret;
|
2009-07-11 14:27:26 +00:00
|
|
|
unsigned loops = 0;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
do {
|
2009-07-11 14:27:26 +00:00
|
|
|
if(loops++ > PL180_MAX_TRANSFER_ERRORS)
|
|
|
|
panicf("SD bank %d error : 0x%x", bank,
|
|
|
|
transfer_error[INTERNAL_AS3525]);
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
ret = sd_wait_for_state(INTERNAL_AS3525, SD_TRAN);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret - 2;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
if(!send_cmd(INTERNAL_AS3525, SD_SWITCH_FUNC, 0x80ffffef, MCI_ARG, NULL))
|
|
|
|
return -1;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
mci_delay();
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
if(!send_cmd(INTERNAL_AS3525, 35, 0, MCI_NO_FLAGS, NULL))
|
|
|
|
return -2;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
mci_delay();
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-29 18:14:26 +00:00
|
|
|
memset(uncached_buffer, 0, 512);
|
2009-06-11 17:28:24 +00:00
|
|
|
if(bank == -1)
|
|
|
|
{ /* enable bank switching */
|
2009-06-29 18:14:26 +00:00
|
|
|
uncached_buffer[0] = 16;
|
|
|
|
uncached_buffer[1] = 1;
|
|
|
|
uncached_buffer[2] = 10;
|
2009-06-11 17:28:24 +00:00
|
|
|
}
|
|
|
|
else
|
2009-06-29 18:14:26 +00:00
|
|
|
uncached_buffer[0] = bank;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-06-11 17:28:24 +00:00
|
|
|
dma_retain();
|
2009-06-29 18:14:26 +00:00
|
|
|
/* we don't use the uncached buffer here, because we need the
|
2009-06-29 11:56:49 +00:00
|
|
|
* physical memory address for DMA transfers */
|
2009-06-29 18:14:26 +00:00
|
|
|
dma_enable_channel(0, aligned_buffer, MCI_FIFO(INTERNAL_AS3525),
|
2009-06-29 11:56:49 +00:00
|
|
|
DMA_PERI_SD, DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8,
|
|
|
|
NULL);
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-10-26 07:06:37 +00:00
|
|
|
MCI_DATA_TIMER(INTERNAL_AS3525) = SD_MAX_WRITE_TIMEOUT;
|
2009-06-11 17:28:24 +00:00
|
|
|
MCI_DATA_LENGTH(INTERNAL_AS3525) = 512;
|
|
|
|
MCI_DATA_CTRL(INTERNAL_AS3525) = (1<<0) /* enable */ |
|
|
|
|
(0<<1) /* transfer direction */ |
|
|
|
|
(1<<3) /* DMA */ |
|
|
|
|
(9<<4) /* 2^9 = 512 */ ;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-12-08 20:26:31 +00:00
|
|
|
/* Wakeup signal comes from NAND/MCIO isr on MCI_ERROR | MCI_DATA_END */
|
2009-06-11 17:28:24 +00:00
|
|
|
wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-11-24 06:37:36 +00:00
|
|
|
/* Wait for FIFO to empty, card may still be in PRG state */
|
|
|
|
while(MCI_STATUS(INTERNAL_AS3525) & MCI_TX_ACTIVE );
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2009-10-01 03:54:36 +00:00
|
|
|
dma_release();
|
2009-06-11 17:28:24 +00:00
|
|
|
|
2009-07-11 14:27:26 +00:00
|
|
|
} while(transfer_error[INTERNAL_AS3525]);
|
2009-03-30 13:26:05 +00:00
|
|
|
|
|
|
|
card_info[INTERNAL_AS3525].current_bank = (bank == -1) ? 0 : bank;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
|
2009-04-10 17:03:56 +00:00
|
|
|
int count, void* buf, const bool write)
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2008-11-09 06:17:21 +00:00
|
|
|
const int drive = 0;
|
|
|
|
#endif
|
2008-12-15 23:37:16 +00:00
|
|
|
int ret = 0;
|
2009-07-11 14:27:26 +00:00
|
|
|
unsigned loops = 0;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2008-11-15 19:24:51 +00:00
|
|
|
/* skip SanDisk OF */
|
2008-11-25 13:38:32 +00:00
|
|
|
if (drive == INTERNAL_AS3525)
|
2009-05-21 11:46:52 +00:00
|
|
|
start += AMS_OF_SIZE;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
|
|
|
mutex_lock(&sd_mtx);
|
2008-12-01 08:18:22 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-12-01 04:17:39 +00:00
|
|
|
sd_enable(true);
|
2009-07-06 16:44:57 +00:00
|
|
|
led(true);
|
2008-12-01 08:18:22 +00:00
|
|
|
#endif
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2008-12-15 23:37:16 +00:00
|
|
|
if (card_info[drive].initialized <= 0)
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
2009-01-16 01:14:58 +00:00
|
|
|
ret = sd_init_card(drive);
|
2008-12-15 23:37:16 +00:00
|
|
|
if (!(card_info[drive].initialized))
|
|
|
|
goto sd_transfer_error;
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
2009-03-30 13:26:05 +00:00
|
|
|
|
2008-12-04 22:54:06 +00:00
|
|
|
dma_retain();
|
|
|
|
|
2008-11-25 13:38:32 +00:00
|
|
|
while(count)
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
|
|
|
/* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
|
|
|
|
* register, so we have to transfer maximum 127 sectors at a time. */
|
2008-11-25 13:38:32 +00:00
|
|
|
unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */
|
2009-04-10 17:03:56 +00:00
|
|
|
void *dma_buf;
|
2008-11-25 13:38:32 +00:00
|
|
|
const int cmd =
|
|
|
|
write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
|
2009-05-26 21:08:47 +00:00
|
|
|
unsigned long bank_start = start;
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-05-26 21:08:47 +00:00
|
|
|
/* Only switch banks for internal storage */
|
|
|
|
if(drive == INTERNAL_AS3525)
|
|
|
|
{
|
2009-07-01 10:07:22 +00:00
|
|
|
unsigned int bank = start / BLOCKS_PER_BANK; /* Current bank */
|
2009-05-26 21:08:47 +00:00
|
|
|
|
|
|
|
/* Switch bank if needed */
|
|
|
|
if(card_info[INTERNAL_AS3525].current_bank != bank)
|
|
|
|
{
|
|
|
|
ret = sd_select_bank(bank);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-11-22 14:13:39 +00:00
|
|
|
ret -= 20;
|
2009-05-26 21:08:47 +00:00
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust start block in current bank */
|
|
|
|
bank_start -= bank * BLOCKS_PER_BANK;
|
|
|
|
|
|
|
|
/* Do not cross a bank boundary in a single transfer loop */
|
2009-07-17 21:50:36 +00:00
|
|
|
if((transfer + bank_start) > BLOCKS_PER_BANK)
|
2009-05-26 21:08:47 +00:00
|
|
|
transfer = BLOCKS_PER_BANK - bank_start;
|
|
|
|
}
|
|
|
|
|
2009-12-08 20:26:31 +00:00
|
|
|
/* Set bank_start to the correct unit (blocks or bytes) */
|
|
|
|
if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
|
|
|
|
bank_start *= SD_BLOCK_SIZE;
|
|
|
|
|
2009-06-08 23:05:33 +00:00
|
|
|
dma_buf = aligned_buffer;
|
|
|
|
if(transfer > UNALIGNED_NUM_SECTORS)
|
|
|
|
transfer = UNALIGNED_NUM_SECTORS;
|
2009-12-08 20:26:31 +00:00
|
|
|
|
2009-06-08 23:05:33 +00:00
|
|
|
if(write)
|
2009-11-25 03:24:28 +00:00
|
|
|
memcpy(uncached_buffer, buf, transfer * SD_BLOCK_SIZE);
|
2009-04-10 17:03:56 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
ret = sd_wait_for_state(drive, SD_TRAN);
|
|
|
|
if (ret < 0)
|
2009-03-30 13:26:05 +00:00
|
|
|
{
|
2009-11-22 14:13:39 +00:00
|
|
|
ret -= 2*20;
|
2009-03-30 13:26:05 +00:00
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-11-22 04:50:26 +00:00
|
|
|
if(!send_cmd(drive, cmd, bank_start, MCI_ARG, NULL))
|
|
|
|
{
|
2009-11-22 14:13:39 +00:00
|
|
|
ret -= 3*20;
|
2009-11-22 04:50:26 +00:00
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
|
2008-11-25 13:38:32 +00:00
|
|
|
if(write)
|
2009-12-08 20:26:31 +00:00
|
|
|
{
|
2009-04-10 17:03:56 +00:00
|
|
|
dma_enable_channel(0, dma_buf, MCI_FIFO(drive),
|
2008-12-04 20:48:19 +00:00
|
|
|
(drive == INTERNAL_AS3525) ? DMA_PERI_SD : DMA_PERI_SD_SLOT,
|
|
|
|
DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8, NULL);
|
2009-12-08 20:26:31 +00:00
|
|
|
|
|
|
|
/*Small delay for writes prevents data crc failures at lower freqs*/
|
|
|
|
int write_delay = 125;
|
|
|
|
while(write_delay--);
|
|
|
|
}
|
2008-11-25 13:38:32 +00:00
|
|
|
else
|
2009-04-10 17:03:56 +00:00
|
|
|
dma_enable_channel(0, MCI_FIFO(drive), dma_buf,
|
2008-12-04 20:48:19 +00:00
|
|
|
(drive == INTERNAL_AS3525) ? DMA_PERI_SD : DMA_PERI_SD_SLOT,
|
|
|
|
DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8, NULL);
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-06-11 17:17:12 +00:00
|
|
|
MCI_DATA_TIMER(drive) = write ?
|
|
|
|
SD_MAX_WRITE_TIMEOUT : SD_MAX_READ_TIMEOUT;
|
2009-11-25 03:24:28 +00:00
|
|
|
MCI_DATA_LENGTH(drive) = transfer * SD_BLOCK_SIZE;
|
2008-11-25 13:38:32 +00:00
|
|
|
MCI_DATA_CTRL(drive) = (1<<0) /* enable */ |
|
|
|
|
(!write<<1) /* transfer direction */ |
|
|
|
|
(1<<3) /* DMA */ |
|
2008-11-09 06:17:21 +00:00
|
|
|
(9<<4) /* 2^9 = 512 */ ;
|
|
|
|
|
2009-12-08 20:26:31 +00:00
|
|
|
/* Wakeup signal comes from NAND/MCIO isr on MCI_ERROR | MCI_DATA_END */
|
2009-01-08 08:27:42 +00:00
|
|
|
wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
|
2009-09-25 01:38:51 +00:00
|
|
|
|
2009-11-24 06:37:36 +00:00
|
|
|
/* Wait for FIFO to empty, card may still be in PRG state for writes */
|
|
|
|
while(MCI_STATUS(drive) & MCI_TX_ACTIVE);
|
2009-09-25 01:38:51 +00:00
|
|
|
|
2009-12-03 20:06:40 +00:00
|
|
|
last_disk_activity = current_tick;
|
|
|
|
|
|
|
|
if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_NO_FLAGS, NULL))
|
|
|
|
{
|
|
|
|
ret = -4*20;
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
|
2009-07-11 14:27:26 +00:00
|
|
|
if(!transfer_error[drive])
|
2009-01-08 08:27:42 +00:00
|
|
|
{
|
2009-06-08 23:05:33 +00:00
|
|
|
if(!write)
|
2009-11-25 03:24:28 +00:00
|
|
|
memcpy(buf, uncached_buffer, transfer * SD_BLOCK_SIZE);
|
|
|
|
buf += transfer * SD_BLOCK_SIZE;
|
2009-01-08 08:27:42 +00:00
|
|
|
start += transfer;
|
|
|
|
count -= transfer;
|
2009-07-11 14:27:26 +00:00
|
|
|
loops = 0; /* reset errors counter */
|
2009-01-08 08:27:42 +00:00
|
|
|
}
|
2009-07-11 14:27:26 +00:00
|
|
|
else if(loops++ > PL180_MAX_TRANSFER_ERRORS)
|
2009-12-03 17:21:56 +00:00
|
|
|
panicf("SD Xfer %s err:0x%x Disk%d", (write? "write": "read"),
|
|
|
|
transfer_error[drive], drive);
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
2008-11-25 13:38:32 +00:00
|
|
|
|
2009-07-11 13:46:10 +00:00
|
|
|
ret = 0; /* success */
|
|
|
|
|
|
|
|
sd_transfer_error:
|
|
|
|
|
2009-01-16 01:14:58 +00:00
|
|
|
dma_release();
|
2008-12-04 22:54:06 +00:00
|
|
|
|
2008-12-01 08:18:22 +00:00
|
|
|
#ifndef BOOTLOADER
|
2009-07-06 16:44:57 +00:00
|
|
|
led(false);
|
2009-01-16 01:14:58 +00:00
|
|
|
sd_enable(false);
|
2008-12-01 08:18:22 +00:00
|
|
|
#endif
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2009-07-11 13:46:10 +00:00
|
|
|
if (ret) /* error */
|
|
|
|
card_info[drive].initialized = 0;
|
|
|
|
|
|
|
|
mutex_unlock(&sd_mtx);
|
2009-01-16 01:14:58 +00:00
|
|
|
return ret;
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
|
2008-11-25 13:38:32 +00:00
|
|
|
void* buf)
|
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
return sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
|
2008-11-25 13:38:32 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
|
2008-11-25 13:38:32 +00:00
|
|
|
const void* buf)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef BOOTLOADER /* we don't need write support in bootloader */
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2008-11-25 13:38:32 +00:00
|
|
|
(void) drive;
|
|
|
|
#endif
|
|
|
|
(void) start;
|
|
|
|
(void) count;
|
|
|
|
(void) buf;
|
|
|
|
return -1;
|
|
|
|
#else
|
2009-07-17 22:28:49 +00:00
|
|
|
return sd_transfer_sectors(IF_MD2(drive,) start, count, (void*)buf, true);
|
2008-11-25 13:38:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-11-10 11:04:43 +00:00
|
|
|
#ifndef BOOTLOADER
|
|
|
|
long sd_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_enable(bool on)
|
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
|
2009-06-18 22:33:02 +00:00
|
|
|
extern int buttonlight_is_on;
|
|
|
|
#endif
|
2009-10-15 19:48:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
static bool cpu_boosted = false;
|
|
|
|
#endif
|
|
|
|
|
2008-12-15 23:37:16 +00:00
|
|
|
if (sd_enabled == on)
|
|
|
|
return; /* nothing to do */
|
2008-11-10 11:04:43 +00:00
|
|
|
if(on)
|
|
|
|
{
|
2009-12-01 18:22:25 +00:00
|
|
|
/* Enable both NAF_CLOCK & IDE clk for internal SD */
|
2008-11-10 11:04:43 +00:00
|
|
|
CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
|
2009-12-03 20:06:46 +00:00
|
|
|
CGU_IDE |= (1<<6); /* enable non AHB interface*/
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2009-12-01 18:22:25 +00:00
|
|
|
/* Enable MCI clk for uSD */
|
2008-11-10 11:04:43 +00:00
|
|
|
CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
|
2009-06-18 22:33:02 +00:00
|
|
|
#ifdef HAVE_BUTTON_LIGHT
|
2009-12-01 18:22:25 +00:00
|
|
|
/* buttonlight AMSes need a bit of special handling for the buttonlight
|
|
|
|
* here due to the dual mapping of GPIOD and XPD */
|
|
|
|
CCU_IO |= (1<<2); /* XPD is SD-MCI interface (b3:2 = 01) */
|
2009-06-18 22:33:02 +00:00
|
|
|
if (buttonlight_is_on)
|
|
|
|
GPIOD_DIR &= ~(1<<7);
|
|
|
|
else
|
|
|
|
_buttonlight_off();
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* HAVE_BUTTON_LIGHT */
|
|
|
|
#endif /* HAVE_MULTIDRIVE */
|
2008-12-15 23:49:43 +00:00
|
|
|
sd_enabled = true;
|
2009-10-15 19:48:26 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
if(card_detect_target()) /* If SD card present Boost cpu for voltage */
|
|
|
|
{
|
|
|
|
cpu_boosted = true;
|
|
|
|
cpu_boost(true);
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-10 11:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-01 18:22:25 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
if(cpu_boosted)
|
|
|
|
{
|
|
|
|
cpu_boost(false);
|
|
|
|
cpu_boosted = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sd_enabled = false;
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2009-06-18 22:33:02 +00:00
|
|
|
#ifdef HAVE_BUTTON_LIGHT
|
2009-12-01 18:22:25 +00:00
|
|
|
CCU_IO &= ~(1<<2); /* XPD is general purpose IO (b3:2 = 00) */
|
2009-06-18 22:33:02 +00:00
|
|
|
if (buttonlight_is_on)
|
|
|
|
_buttonlight_on();
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* HAVE_BUTTON_LIGHT */
|
2009-12-01 18:22:25 +00:00
|
|
|
/* Disable MCI clk for uSD */
|
2009-06-18 22:33:02 +00:00
|
|
|
CGU_PERI &= ~CGU_MCI_CLOCK_ENABLE;
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* HAVE_MULTIDRIVE */
|
2009-10-15 19:48:26 +00:00
|
|
|
|
2009-12-01 18:22:25 +00:00
|
|
|
/* Disable both NAF_CLOCK & IDE clk for internal SD */
|
|
|
|
CGU_PERI &= ~CGU_NAF_CLOCK_ENABLE;
|
2009-12-03 20:06:46 +00:00
|
|
|
CGU_IDE &= ~(1<<6); /* disable non AHB interface*/
|
2008-11-10 11:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tCardInfo *card_get_info_target(int card_no)
|
|
|
|
{
|
2009-07-01 21:49:13 +00:00
|
|
|
return &card_info[card_no];
|
2008-11-10 11:04:43 +00:00
|
|
|
}
|
|
|
|
|
2008-11-11 14:46:13 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
void card_enable_monitoring_target(bool on)
|
|
|
|
{
|
2009-10-15 07:16:38 +00:00
|
|
|
if (on) /* enable interrupt */
|
2008-12-15 23:37:16 +00:00
|
|
|
GPIOA_IE |= (1<<2);
|
2009-10-15 07:16:38 +00:00
|
|
|
else /* disable interrupt */
|
2008-12-15 23:37:16 +00:00
|
|
|
GPIOA_IE &= ~(1<<2);
|
2008-11-11 14:46:13 +00:00
|
|
|
}
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* HAVE_HOTSWAP */
|
2008-11-11 14:46:13 +00:00
|
|
|
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* !BOOTLOADER */
|
2009-07-17 22:28:49 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
int sd_num_drives(int first_drive)
|
|
|
|
{
|
|
|
|
/* We don't care which logical drive number(s) we have been assigned */
|
|
|
|
(void)first_drive;
|
2009-10-15 07:16:38 +00:00
|
|
|
|
|
|
|
return NUM_DRIVES;
|
2009-07-17 22:28:49 +00:00
|
|
|
}
|
2009-10-15 07:16:38 +00:00
|
|
|
#endif /* CONFIG_STORAGE_MULTI */
|