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
|
2008-10-29 20:21:59 +00:00
|
|
|
* Copyright © 2008 Rafaël Carré
|
|
|
|
*
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
#include "config.h" /* for HAVE_MULTIVOLUME */
|
2008-11-09 06:17:21 +00:00
|
|
|
#include "fat.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "hotswap.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include <stdlib.h>
|
2008-10-29 20:21:59 +00:00
|
|
|
#include "as3525.h"
|
2008-11-01 14:38:45 +00:00
|
|
|
#include "pl180.h"
|
2008-10-29 20:21:59 +00:00
|
|
|
#include "panic.h"
|
|
|
|
#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"
|
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 */
|
|
|
|
#define MMC_NO_FLAGS (0<<0)
|
|
|
|
#define MMC_RESP (1<<0)
|
|
|
|
#define MMC_LONG_RESP (1<<1)
|
|
|
|
#define MMC_ARG (1<<2)
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
/* ARM PL180 registers */
|
2008-11-09 06:17:21 +00:00
|
|
|
#define MMC_POWER(i) (*(volatile unsigned char *) (pl180_base[i]+0x00))
|
2008-10-29 20:21:59 +00:00
|
|
|
#define MMC_CLOCK(i) (*(volatile unsigned long *) (pl180_base[i]+0x04))
|
|
|
|
#define MMC_ARGUMENT(i) (*(volatile unsigned long *) (pl180_base[i]+0x08))
|
|
|
|
#define MMC_COMMAND(i) (*(volatile unsigned long *) (pl180_base[i]+0x0C))
|
|
|
|
#define MMC_RESPCMD(i) (*(volatile unsigned long *) (pl180_base[i]+0x10))
|
|
|
|
#define MMC_RESP0(i) (*(volatile unsigned long *) (pl180_base[i]+0x14))
|
|
|
|
#define MMC_RESP1(i) (*(volatile unsigned long *) (pl180_base[i]+0x18))
|
|
|
|
#define MMC_RESP2(i) (*(volatile unsigned long *) (pl180_base[i]+0x1C))
|
|
|
|
#define MMC_RESP3(i) (*(volatile unsigned long *) (pl180_base[i]+0x20))
|
2008-11-09 06:17:21 +00:00
|
|
|
#define MMC_DATA_TIMER(i) (*(volatile unsigned long *) (pl180_base[i]+0x24))
|
|
|
|
#define MMC_DATA_LENGTH(i) (*(volatile unsigned short*) (pl180_base[i]+0x28))
|
|
|
|
#define MMC_DATA_CTRL(i) (*(volatile unsigned char *) (pl180_base[i]+0x2C))
|
|
|
|
#define MMC_DATA_CNT(i) (*(volatile unsigned short*) (pl180_base[i]+0x30))
|
2008-10-29 20:21:59 +00:00
|
|
|
#define MMC_STATUS(i) (*(volatile unsigned long *) (pl180_base[i]+0x34))
|
|
|
|
#define MMC_CLEAR(i) (*(volatile unsigned long *) (pl180_base[i]+0x38))
|
|
|
|
#define MMC_MASK0(i) (*(volatile unsigned long *) (pl180_base[i]+0x3C))
|
|
|
|
#define MMC_MASK1(i) (*(volatile unsigned long *) (pl180_base[i]+0x40))
|
|
|
|
#define MMC_SELECT(i) (*(volatile unsigned long *) (pl180_base[i]+0x44))
|
2008-11-09 06:17:21 +00:00
|
|
|
#define MMC_FIFO_CNT(i) (*(volatile unsigned long *) (pl180_base[i]+0x48))
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
#define MMC_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
|
|
|
|
/* volumes */
|
|
|
|
#define NAND_AS3525 0
|
|
|
|
#define SD_AS3525 1
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static const int pl180_base[NUM_VOLUMES] = {
|
|
|
|
NAND_FLASH_BASE
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
, SD_MCI_BASE
|
|
|
|
#endif
|
|
|
|
};
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
#define BLOCK_SIZE 512
|
|
|
|
#define SECTOR_SIZE 512
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static tSDCardInfo card_info[NUM_VOLUMES];
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
/* for compatibility */
|
|
|
|
static long last_disk_activity = -1;
|
|
|
|
|
|
|
|
#define MIN_YIELD_PERIOD 1000
|
|
|
|
static long next_yield = 0;
|
|
|
|
|
|
|
|
/* Shoot for around 75% usage */
|
|
|
|
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x1c0)/sizeof(long)];
|
|
|
|
static const char sd_thread_name[] = "ata/sd";
|
|
|
|
static struct mutex sd_mtx SHAREDBSS_ATTR;
|
|
|
|
static struct event_queue sd_queue;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
static inline void mci_delay(void) { int i = 0xffff; while(i--) ; }
|
|
|
|
|
|
|
|
static void mci_set_clock_divider(const int drive, int divider)
|
|
|
|
{
|
|
|
|
int clock = MMC_CLOCK(drive);
|
|
|
|
|
|
|
|
if(divider > 1)
|
|
|
|
{
|
|
|
|
/* use divide logic */
|
2008-11-01 14:38:45 +00:00
|
|
|
clock &= ~MCI_CLOCK_BYPASS;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
/* convert divider to MMC_CLOCK logic */
|
|
|
|
divider = (divider/2) - 1;
|
|
|
|
if(divider >= 256)
|
|
|
|
divider = 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* bypass dividing logic */
|
2008-11-01 14:38:45 +00:00
|
|
|
clock |= MCI_CLOCK_BYPASS;
|
2008-10-29 20:21:59 +00:00
|
|
|
divider = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MMC_CLOCK(drive) = clock | divider;
|
|
|
|
|
|
|
|
mci_delay();
|
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static bool send_cmd(const int drive, const int cmd, const int arg,
|
|
|
|
const int flags, int *response)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
|
|
|
int val, status;
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
while(MMC_STATUS(drive) & MCI_CMD_ACTIVE);
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-01 14:38:45 +00:00
|
|
|
if(MMC_COMMAND(drive) & MCI_COMMAND_ENABLE) /* clears existing command */
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
|
|
|
MMC_COMMAND(drive) = 0;
|
|
|
|
mci_delay();
|
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
val = cmd | MCI_COMMAND_ENABLE;
|
|
|
|
if(flags & MMC_RESP)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-01 14:38:45 +00:00
|
|
|
val |= MCI_COMMAND_RESPONSE;
|
2008-11-09 06:17:21 +00:00
|
|
|
if(flags & MMC_LONG_RESP)
|
2008-11-01 14:38:45 +00:00
|
|
|
val |= MCI_COMMAND_LONG_RESPONSE;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MMC_CLEAR(drive) = 0x7ff;
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
MMC_ARGUMENT(drive) = (flags & MMC_ARG) ? arg : 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
MMC_COMMAND(drive) = val;
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
while(MMC_STATUS(drive) & MCI_CMD_ACTIVE); /* wait for cmd completion */
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
MMC_COMMAND(drive) = 0;
|
|
|
|
MMC_ARGUMENT(drive) = ~0;
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
status = MMC_STATUS(drive);
|
|
|
|
MMC_CLEAR(drive) = 0x7ff;
|
|
|
|
|
|
|
|
if(flags & MMC_RESP)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
if(status & MCI_CMD_TIMEOUT)
|
|
|
|
return false;
|
|
|
|
else if(status & (MCI_CMD_CRC_FAIL /* FIXME? */ | MCI_CMD_RESP_END))
|
|
|
|
{ /* resp received */
|
|
|
|
if(flags & MMC_LONG_RESP)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
/* store the response in little endian order for the words */
|
|
|
|
response[0] = MMC_RESP3(drive);
|
|
|
|
response[1] = MMC_RESP2(drive);
|
|
|
|
response[2] = MMC_RESP1(drive);
|
|
|
|
response[3] = MMC_RESP0(drive);
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
else
|
|
|
|
response[0] = MMC_RESP0(drive);
|
|
|
|
return true;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
}
|
|
|
|
else if(status & MCI_CMD_SENT)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
static int sd_init_card(const int drive)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
unsigned int c_size;
|
|
|
|
unsigned long c_mult;
|
|
|
|
int response;
|
|
|
|
int max_tries = 100; /* max acmd41 attemps */
|
|
|
|
bool sdhc;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MMC_NO_FLAGS, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -1;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
mci_delay();
|
|
|
|
|
|
|
|
sdhc = false;
|
2008-11-09 23:40:21 +00:00
|
|
|
if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MMC_RESP|MMC_ARG, &response))
|
2008-11-09 06:17:21 +00:00
|
|
|
if((response & 0xFFF) == 0x1AA)
|
2008-10-29 20:21:59 +00:00
|
|
|
sdhc = true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
mci_delay();
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
/* app_cmd */
|
2008-11-09 23:40:21 +00:00
|
|
|
if( !send_cmd(drive, SD_APP_CMD, 0, MMC_RESP|MMC_ARG, &response) ||
|
2008-11-09 06:17:21 +00:00
|
|
|
!(response & (1<<5)) )
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
return -2;
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
/* acmd41 */
|
|
|
|
if(!send_cmd(drive, SD_APP_OP_COND, (sdhc ? 0x40FF8000 : (1<<23)),
|
|
|
|
MMC_RESP|MMC_ARG, &card_info[drive].ocr))
|
|
|
|
return -3;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
} while(!(card_info[drive].ocr & (1<<31)) && max_tries--);
|
|
|
|
|
|
|
|
if(!max_tries)
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
/* send CID */
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MMC_RESP|MMC_LONG_RESP|MMC_ARG,
|
2008-11-09 06:17:21 +00:00
|
|
|
card_info[drive].cid))
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* send RCA */
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MMC_RESP|MMC_ARG,
|
2008-11-09 06:17:21 +00:00
|
|
|
&card_info[drive].rca))
|
|
|
|
return -6;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
/* send CSD */
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca,
|
2008-11-09 06:17:21 +00:00
|
|
|
MMC_RESP|MMC_LONG_RESP|MMC_ARG, card_info[drive].csd))
|
|
|
|
return -7;
|
|
|
|
|
|
|
|
/* These calculations come from the Sandisk SD card product manual */
|
|
|
|
if( (card_info[drive].csd[3]>>30) == 0)
|
|
|
|
{
|
|
|
|
/* CSD version 1.0 */
|
|
|
|
c_size = ((card_info[drive].csd[2] & 0x3ff) << 2) + (card_info[drive].csd[1]>>30) + 1;
|
|
|
|
c_mult = 4 << ((card_info[drive].csd[1] >> 15) & 7);
|
|
|
|
card_info[drive].max_read_bl_len = 1 << ((card_info[drive].csd[2] >> 16) & 15);
|
|
|
|
card_info[drive].block_size = BLOCK_SIZE; /* Always use 512 byte blocks */
|
|
|
|
card_info[drive].numblocks = c_size * c_mult * (card_info[drive].max_read_bl_len/512);
|
|
|
|
card_info[drive].capacity = card_info[drive].numblocks * card_info[drive].block_size;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
else if( (card_info[drive].csd[3]>>30) == 1)
|
|
|
|
{
|
|
|
|
/* CSD version 2.0 */
|
|
|
|
c_size = ((card_info[drive].csd[2] & 0x3f) << 16) + (card_info[drive].csd[1]>>16) + 1;
|
|
|
|
card_info[drive].max_read_bl_len = 1 << ((card_info[drive].csd[2] >> 16) & 0xf);
|
|
|
|
card_info[drive].block_size = BLOCK_SIZE; /* Always use 512 byte blocks */
|
|
|
|
card_info[drive].numblocks = c_size << 10;
|
|
|
|
card_info[drive].capacity = card_info[drive].numblocks * card_info[drive].block_size;
|
|
|
|
}
|
2008-10-29 20:21:59 +00:00
|
|
|
#endif
|
2008-11-09 06:17:21 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MMC_ARG, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -9;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MMC_ARG, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -10;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SET_BUS_WIDTH, card_info[drive].rca | 2, MMC_ARG, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
return -11;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SET_BLOCKLEN, card_info[drive].block_size, MMC_ARG,
|
2008-11-09 06:17:21 +00:00
|
|
|
NULL))
|
|
|
|
return -12;
|
|
|
|
|
|
|
|
card_info[drive].initialized = 1;
|
|
|
|
|
|
|
|
mci_set_clock_divider(drive, 1); /* full speed */
|
|
|
|
|
|
|
|
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:
|
|
|
|
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 */
|
|
|
|
|
|
|
|
disk_unmount(1); /* release "by force", ensure file
|
|
|
|
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. */
|
|
|
|
card_info[1].initialized = 0;
|
|
|
|
|
|
|
|
if (ev.id == SYS_HOTSWAP_INSERTED)
|
|
|
|
disk_mount(1);
|
|
|
|
|
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
|
|
|
|
|
|
|
/* Access is now safe */
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
fat_unlock();
|
|
|
|
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;
|
|
|
|
#if 0
|
|
|
|
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;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-29 20:21:59 +00:00
|
|
|
static void init_pl180_controller(const int drive)
|
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
#ifdef BOOTLOADER
|
|
|
|
MMC_COMMAND(drive) = MMC_DATA_CTRL(drive) = 0;
|
2008-10-29 20:21:59 +00:00
|
|
|
MMC_CLEAR(drive) = 0x7ff;
|
|
|
|
|
|
|
|
MMC_MASK0(drive) = MMC_MASK1(drive) = 0; /* disable all interrupts */
|
|
|
|
|
2008-11-01 14:38:45 +00:00
|
|
|
MMC_POWER(drive) = MCI_POWER_UP|(10 /*voltage*/ << 2); /* use OF voltage */
|
2008-10-29 20:21:59 +00:00
|
|
|
mci_delay();
|
|
|
|
|
2008-11-01 14:38:45 +00:00
|
|
|
MMC_POWER(drive) |= MCI_POWER_ON;
|
2008-10-29 20:21:59 +00:00
|
|
|
mci_delay();
|
|
|
|
|
|
|
|
MMC_SELECT(drive) = 0;
|
|
|
|
|
2008-11-01 14:38:45 +00:00
|
|
|
MMC_CLOCK(drive) = MCI_CLOCK_ENABLE;
|
|
|
|
MMC_CLOCK(drive) &= ~MCI_CLOCK_POWERSAVE;
|
2008-11-09 23:52:08 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2008-11-09 10:00:52 +00:00
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
/* set MCLK divider */
|
|
|
|
mci_set_clock_divider(drive, 200);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2008-11-09 10:00:52 +00:00
|
|
|
#ifdef BOOTLOADER /* No need to do twice the same thing */
|
2008-11-07 10:52:26 +00:00
|
|
|
CGU_IDE = (1<<7) /* AHB interface enable */ |
|
|
|
|
(1<<6) /* interface enable */ |
|
|
|
|
(2<<2) /* clock didiver = 2+1 */ |
|
|
|
|
1 /* clock source = PLLA */;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CCU_IO &= ~8; /* bits 3:2 = 01, xpd is SD interface */
|
|
|
|
CCU_IO |= 4;
|
|
|
|
|
2008-11-09 10:00:52 +00:00
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
init_pl180_controller(NAND_AS3525);
|
2008-11-09 06:17:21 +00:00
|
|
|
ret = sd_init_card(NAND_AS3525);
|
|
|
|
if(ret < 0)
|
|
|
|
return ret;
|
2008-10-29 20:21:59 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
init_pl180_controller(SD_AS3525);
|
2008-11-09 06:17:21 +00:00
|
|
|
ret = sd_init_card(SD_AS3525);
|
|
|
|
if(ret < 0)
|
|
|
|
return ret;
|
2008-10-29 20:21:59 +00:00
|
|
|
#endif
|
|
|
|
|
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));
|
|
|
|
|
2008-10-29 20:21:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-09 06:17:21 +00:00
|
|
|
#ifdef STORAGE_GET_INFO
|
|
|
|
void sd_get_info(IF_MV2(int drive,) struct storage_info *info)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
2008-11-09 06:17:21 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int drive=0;
|
|
|
|
#endif
|
|
|
|
info->sector_size=card_info[drive].block_size;
|
|
|
|
info->num_sectors=card_info[drive].numblocks;
|
|
|
|
info->vendor="Rockbox";
|
|
|
|
info->product = (drive == 0) ? "Internal Storage" : "SD Card Slot";
|
|
|
|
info->revision="0.00";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
bool sd_removable(IF_MV_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int drive=0;
|
|
|
|
#endif
|
|
|
|
return (drive==1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sd_present(IF_MV_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int drive=0;
|
|
|
|
#endif
|
|
|
|
return (card_info[drive].initialized && card_info[drive].numblocks > 0);
|
2008-10-29 20:21:59 +00:00
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
#endif
|
2008-10-29 20:21:59 +00:00
|
|
|
|
2008-11-01 22:55:35 +00:00
|
|
|
int sd_write_sectors(IF_MV2(int drive,) unsigned long start, int count, const void* buf)
|
2008-10-29 20:21:59 +00:00
|
|
|
{
|
|
|
|
(void)start;
|
|
|
|
(void)count;
|
|
|
|
(void)buf;
|
|
|
|
return 0; /* TODO */
|
|
|
|
}
|
2008-11-09 06:17:21 +00:00
|
|
|
|
|
|
|
static bool sd_poll_status(const int drive, unsigned int trigger, long timeout)
|
|
|
|
{
|
|
|
|
long t = current_tick;
|
|
|
|
|
|
|
|
while ((MMC_STATUS(drive) & trigger) == 0)
|
|
|
|
{
|
|
|
|
long time = current_tick;
|
|
|
|
|
|
|
|
if (TIME_AFTER(time, next_yield))
|
|
|
|
{
|
|
|
|
long ty = current_tick;
|
|
|
|
yield();
|
|
|
|
timeout += current_tick - ty;
|
|
|
|
next_yield = ty + MIN_YIELD_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TIME_AFTER(time, t + timeout))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_wait_for_state(const int drive, unsigned int state)
|
|
|
|
{
|
|
|
|
unsigned int response = 0;
|
|
|
|
unsigned int timeout = 0x80000;
|
|
|
|
|
|
|
|
long t = current_tick;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
long us;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
|
2008-11-09 06:17:21 +00:00
|
|
|
MMC_RESP|MMC_ARG, &response))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (((response >> 9) & 0xf) == state)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(TIME_AFTER(current_tick, t + timeout))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
us = current_tick;
|
|
|
|
if (TIME_AFTER(us, next_yield))
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
timeout += current_tick - us;
|
|
|
|
next_yield = us + MIN_YIELD_PERIOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int incount,
|
|
|
|
void* inbuf)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int drive = 0;
|
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
unsigned char *buf_end, *buf = inbuf;
|
|
|
|
int remaining = incount;
|
|
|
|
const unsigned long *fifo_base = MMC_FIFO(drive);
|
|
|
|
|
2008-11-15 19:24:51 +00:00
|
|
|
/* skip SanDisk OF */
|
|
|
|
if (drive == NAND_AS3525)
|
|
|
|
#if defined(SANSA_E200V2) || defined(SANSA_FUZE)
|
|
|
|
start += 61440;
|
2008-11-10 13:01:46 +00:00
|
|
|
#else
|
2008-11-15 19:24:51 +00:00
|
|
|
start += 20480;
|
2008-11-10 13:01:46 +00:00
|
|
|
#endif
|
2008-11-09 06:17:21 +00:00
|
|
|
/* TODO: Add DMA support. */
|
|
|
|
|
|
|
|
mutex_lock(&sd_mtx);
|
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
if (drive != 0 && !card_detect_target())
|
|
|
|
{
|
|
|
|
/* no external sd-card inserted */
|
|
|
|
ret = -88;
|
|
|
|
goto sd_read_error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (card_info[drive].initialized < 0)
|
|
|
|
{
|
|
|
|
ret = card_info[drive].initialized;
|
|
|
|
goto sd_read_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(drive, SD_TRAN);
|
2008-11-09 06:17:21 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto sd_read_error;
|
|
|
|
|
|
|
|
while(remaining)
|
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
int transfer = (remaining >= 128) ? 127 : remaining; /* sectors */
|
|
|
|
|
|
|
|
if(card_info[drive].ocr & (1<<30) ) /* SDHC */
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = send_cmd(drive, SD_READ_MULTIPLE_BLOCK, start, MMC_ARG, NULL);
|
2008-11-09 06:17:21 +00:00
|
|
|
else
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = send_cmd(drive, SD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE,
|
2008-11-09 06:17:21 +00:00
|
|
|
MMC_ARG, NULL);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto sd_read_error;
|
|
|
|
|
|
|
|
/* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
|
|
|
|
|
|
|
|
|
|
|
|
MMC_DATA_TIMER(drive) = 0x1000000; /* FIXME: arbitrary */
|
|
|
|
MMC_DATA_LENGTH(drive) = transfer * card_info[drive].block_size;
|
|
|
|
MMC_DATA_CTRL(drive) = (1<<0) /* enable */ |
|
|
|
|
(1<<1) /* from card to controller */ |
|
|
|
|
(9<<4) /* 2^9 = 512 */ ;
|
|
|
|
|
|
|
|
buf_end = buf + transfer * card_info[drive].block_size;
|
|
|
|
|
|
|
|
while(buf < buf_end)
|
|
|
|
{
|
|
|
|
/* Wait for the FIFO to be half full */
|
|
|
|
if (!sd_poll_status(drive, ((1<<15)), 100))
|
|
|
|
{
|
|
|
|
ret = -42;
|
|
|
|
goto sd_read_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
"ldmia %2, {r0-r7} \n" /* load 8 * 4 bytes */
|
|
|
|
"stmia %1!, {r0-r7} \n" /* store 8 * 4 bytes */
|
|
|
|
:"=r"(buf) /* output */
|
|
|
|
:"r"(buf), "r"(fifo_base) /* input */
|
|
|
|
:"r0","r1","r2","r3","r4","r5","r6","r7","r8" /* clobbers */
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining -= transfer;
|
|
|
|
start += transfer;
|
|
|
|
last_disk_activity = current_tick;
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MMC_NO_FLAGS, NULL))
|
2008-11-09 06:17:21 +00:00
|
|
|
{
|
|
|
|
ret = -666;
|
|
|
|
goto sd_read_error;
|
|
|
|
}
|
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(drive, SD_TRAN);
|
2008-11-09 06:17:21 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto sd_read_error;
|
|
|
|
|
|
|
|
}
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
sd_read_error:
|
|
|
|
card_info[drive].initialized = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-10 11:04:43 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-11-09 06:17:21 +00:00
|
|
|
void sd_sleep(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_spin(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_spindown(int seconds)
|
|
|
|
{
|
|
|
|
(void)seconds;
|
|
|
|
}
|
2008-11-10 11:04:43 +00:00
|
|
|
|
|
|
|
long sd_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_enable(bool on)
|
|
|
|
{
|
|
|
|
if(on)
|
|
|
|
{
|
|
|
|
CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
|
|
|
|
#endif
|
|
|
|
CGU_IDE |= (1<<7) /* AHB interface enable */ |
|
|
|
|
(1<<6) /* interface enable */;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CGU_PERI &= ~CGU_NAF_CLOCK_ENABLE;
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
CGU_PERI &= ~CGU_MCI_CLOCK_ENABLE;
|
|
|
|
#endif
|
|
|
|
CGU_IDE &= ~((1<<7)|(1<<6));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move the sd-card info to mmc struct */
|
|
|
|
tCardInfo *card_get_info_target(int card_no)
|
|
|
|
{
|
|
|
|
int i, temp;
|
|
|
|
static tCardInfo card;
|
|
|
|
static const char mantissa[] = { /* *10 */
|
|
|
|
0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
|
|
|
|
static const int exponent[] = { /* use varies */
|
|
|
|
1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 };
|
|
|
|
|
|
|
|
card.initialized = card_info[card_no].initialized;
|
|
|
|
card.ocr = card_info[card_no].ocr;
|
|
|
|
for(i=0; i<4; i++) card.csd[i] = card_info[card_no].csd[i];
|
|
|
|
for(i=0; i<4; i++) card.cid[i] = card_info[card_no].cid[i];
|
|
|
|
card.numblocks = card_info[card_no].numblocks;
|
|
|
|
card.blocksize = card_info[card_no].block_size;
|
|
|
|
temp = card_extract_bits(card.csd, 29, 3);
|
|
|
|
card.speed = mantissa[card_extract_bits(card.csd, 25, 4)]
|
|
|
|
* exponent[temp > 2 ? 7 : temp + 4];
|
|
|
|
card.nsac = 100 * card_extract_bits(card.csd, 16, 8);
|
|
|
|
temp = card_extract_bits(card.csd, 13, 3);
|
|
|
|
card.tsac = mantissa[card_extract_bits(card.csd, 9, 4)]
|
|
|
|
* exponent[temp] / 10;
|
|
|
|
card.cid[0] = htobe32(card.cid[0]); /* ascii chars here */
|
|
|
|
card.cid[1] = htobe32(card.cid[1]); /* ascii chars here */
|
|
|
|
temp = *((char*)card.cid+13); /* adjust year<=>month, 1997 <=> 2000 */
|
|
|
|
*((char*)card.cid+13) = (unsigned char)((temp >> 4) | (temp << 4)) + 3;
|
|
|
|
|
|
|
|
return &card;
|
|
|
|
}
|
|
|
|
|
2008-11-11 14:46:13 +00:00
|
|
|
bool card_detect_target(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
/* TODO */
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
void card_enable_monitoring_target(bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-10 11:04:43 +00:00
|
|
|
#endif /* BOOTLOADER */
|