2006-08-01 22:28:14 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2006-11-21 22:55:39 +00:00
|
|
|
* Copyright (C) 2006 Daniel Ankers
|
2006-08-01 22:28:14 +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.
|
2006-08-01 22:28:14 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-11-09 06:17:14 +00:00
|
|
|
#include "config.h" /* for HAVE_MULTIVOLUME */
|
2008-03-12 10:03:52 +00:00
|
|
|
#include "fat.h"
|
2008-03-12 11:08:41 +00:00
|
|
|
#include "hotswap.h"
|
2008-05-13 01:53:19 +00:00
|
|
|
#include "ata-sd-target.h"
|
2006-11-25 13:04:50 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2006-11-21 22:55:39 +00:00
|
|
|
#include "system.h"
|
2006-08-01 22:28:14 +00:00
|
|
|
#include <string.h>
|
2006-11-21 22:55:39 +00:00
|
|
|
#include "thread.h"
|
2007-06-30 02:08:27 +00:00
|
|
|
#include "led.h"
|
|
|
|
#include "disk.h"
|
2007-11-05 16:12:13 +00:00
|
|
|
#include "cpu.h"
|
2007-06-30 02:08:27 +00:00
|
|
|
#include "panic.h"
|
2007-07-25 06:15:07 +00:00
|
|
|
#include "usb.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "sd.h"
|
|
|
|
#include "storage.h"
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2008-03-12 10:03:52 +00:00
|
|
|
#define BLOCK_SIZE 512
|
|
|
|
#define SECTOR_SIZE 512
|
2006-12-21 18:32:47 +00:00
|
|
|
#define BLOCKS_PER_BANK 0x7a7800
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
/* Comparing documentations of various MMC/SD controllers revealed, */
|
|
|
|
/* that this controller seems to be a mix of PXA27x, PXA255 and */
|
|
|
|
/* some PP specific stuff. The register and bit definitions are */
|
|
|
|
/* taken from the 'PXA27x Developers Manual', as it appears to be */
|
|
|
|
/* the closest match. Known differences and obscurities are commented.*/
|
|
|
|
|
|
|
|
#define MMC_STRPCL (*(volatile unsigned int *)(0x70008200))
|
|
|
|
#define MMC_STAT (*(volatile unsigned int *)(0x70008204))
|
|
|
|
#define MMC_CLKRT (*(volatile unsigned int *)(0x70008208))
|
|
|
|
#define MMC_SPI (*(volatile unsigned int *)(0x7000820c))
|
|
|
|
#define MMC_CMDAT (*(volatile unsigned int *)(0x70008210))
|
|
|
|
#define MMC_RESTO (*(volatile unsigned int *)(0x70008214))
|
|
|
|
#define MMC_RDTO (*(volatile unsigned int *)(0x70008218))
|
|
|
|
#define MMC_BLKLEN (*(volatile unsigned int *)(0x7000821c))
|
|
|
|
#define MMC_NUMBLK (*(volatile unsigned int *)(0x70008220))
|
|
|
|
#define MMC_I_MASK (*(volatile unsigned int *)(0x70008224))
|
|
|
|
#define MMC_CMD (*(volatile unsigned int *)(0x70008228))
|
|
|
|
#define MMC_ARGH (*(volatile unsigned int *)(0x7000822c))
|
|
|
|
#define MMC_ARGL (*(volatile unsigned int *)(0x70008230))
|
|
|
|
#define MMC_RES (*(volatile unsigned int *)(0x70008234))
|
|
|
|
|
|
|
|
/* PXA255/27x have separate RX/TX FIFOs with 32x8 bit */
|
|
|
|
/* PP502x has a combined Data FIFO with 16x16 bit */
|
|
|
|
#define MMC_DATA_FIFO (*(volatile unsigned int *)(0x70008280))
|
|
|
|
|
|
|
|
/* PP specific registers, no other controller seem to have such. */
|
|
|
|
#define MMC_SD_STATE (*(volatile unsigned int *)(0x70008238))
|
|
|
|
#define MMC_INIT_1 (*(volatile unsigned int *)(0x70008240))
|
|
|
|
#define MMC_INIT_2 (*(volatile unsigned int *)(0x70008244))
|
|
|
|
|
|
|
|
/* MMC_STAT bits */
|
|
|
|
#define STAT_SDIO_SUSPEND_ACK (1 << 16)
|
|
|
|
#define STAT_SDIO_INT (1 << 15)
|
|
|
|
#define STAT_RD_STALLED (1 << 14)
|
|
|
|
#define STAT_END_CMD_RES (1 << 13)
|
|
|
|
#define STAT_PRG_DONE (1 << 12)
|
|
|
|
#define STAT_DATA_TRAN_DONE (1 << 11)
|
|
|
|
#define STAT_SPI_WR_ERR (1 << 10)
|
|
|
|
#define STAT_FLASH_ERR (1 << 9)
|
|
|
|
#define STAT_CLK_EN (1 << 8)
|
|
|
|
#define STAT_RECV_FIFO_FULL (1 << 7) /* taken from PXA255 */
|
|
|
|
#define STAT_XMIT_FIFO_EMPTY (1 << 6) /* taken from PXA255 */
|
|
|
|
#define STAT_RES_CRC_ERR (1 << 5)
|
|
|
|
#define STAT_DAT_ERR_TOKEN (1 << 4)
|
|
|
|
#define STAT_CRC_RD_ERR (1 << 3)
|
|
|
|
#define STAT_CRC_WR_ERR (1 << 2)
|
|
|
|
#define STAT_TIME_OUT_RES (1 << 1)
|
|
|
|
#define STAT_TIME_OUT_READ (1)
|
|
|
|
#define STAT_ERROR_BITS (0x3f)
|
|
|
|
|
|
|
|
/* MMC_CMDAT bits */
|
|
|
|
/* Some of the bits used by the OF don't make much sense with these */
|
|
|
|
/* definitions. So they're probably different between PXA and PP502x */
|
|
|
|
/* Bits 0-5 appear to match though. */
|
|
|
|
#define CMDAT_SDIO_RESUME (1 << 13)
|
|
|
|
#define CMDAT_SDIO_SUSPEND (1 << 12)
|
|
|
|
#define CMDAT_SDIO_INT_EN (1 << 11)
|
|
|
|
#define CMDAT_STOP_TRAN (1 << 10)
|
|
|
|
#define CMDAT_SD_4DAT (1 << 8)
|
|
|
|
#define CMDAT_DMA_EN (1 << 7)
|
|
|
|
#define CMDAT_INIT (1 << 6)
|
|
|
|
#define CMDAT_BUSY (1 << 5)
|
|
|
|
#define CMDAT_STRM_BLK (1 << 4)
|
|
|
|
#define CMDAT_WR_RD (1 << 3)
|
|
|
|
#define CMDAT_DATA_EN (1 << 2)
|
|
|
|
#define CMDAT_RES_TYPE3 (3)
|
|
|
|
#define CMDAT_RES_TYPE2 (2)
|
|
|
|
#define CMDAT_RES_TYPE1 (1)
|
|
|
|
|
|
|
|
/* MMC_I_MASK bits */
|
|
|
|
/* PP502x apparently only has bits 0-3 */
|
|
|
|
#define I_MASK_SDIO_SUSPEND_ACK (1 << 12)
|
|
|
|
#define I_MASK_SDIO_INT (1 << 11)
|
|
|
|
#define I_MASK_RD_STALLED (1 << 10)
|
|
|
|
#define I_MASK_RES_ERR (1 << 9)
|
|
|
|
#define I_MASK_DAT_ERR (1 << 8)
|
|
|
|
#define I_MASK_TINT (1 << 7)
|
|
|
|
#define I_MASK_TXFIFO_WR_REQ (1 << 6)
|
|
|
|
#define I_MASK_RXFIFO_RD_REQ (1 << 5)
|
|
|
|
#define I_MASK_CLK_IS_OFF (1 << 4)
|
|
|
|
#define I_MASK_STOP_CMD (1 << 3)
|
|
|
|
#define I_MASK_END_CMD_RES (1 << 2)
|
|
|
|
#define I_MASK_PRG_DONE (1 << 1)
|
|
|
|
#define I_MASK_DATA_TRAN_DONE (1 << 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
#define FIFO_LEN 16 /* FIFO is 16 words deep */
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
#define EC_OK 0
|
|
|
|
#define EC_FAILED 1
|
|
|
|
#define EC_NOCARD 2
|
|
|
|
#define EC_WAIT_STATE_FAILED 3
|
|
|
|
#define EC_CHECK_TIMEOUT_FAILED 4
|
|
|
|
#define EC_POWER_UP 5
|
|
|
|
#define EC_READ_TIMEOUT 6
|
|
|
|
#define EC_WRITE_TIMEOUT 7
|
|
|
|
#define EC_TRAN_SEL_BANK 8
|
|
|
|
#define EC_TRAN_READ_ENTRY 9
|
|
|
|
#define EC_TRAN_READ_EXIT 10
|
|
|
|
#define EC_TRAN_WRITE_ENTRY 11
|
|
|
|
#define EC_TRAN_WRITE_EXIT 12
|
|
|
|
#define EC_FIFO_SEL_BANK_EMPTY 13
|
|
|
|
#define EC_FIFO_SEL_BANK_DONE 14
|
|
|
|
#define EC_FIFO_ENA_BANK_EMPTY 15
|
|
|
|
#define EC_FIFO_READ_FULL 16
|
|
|
|
#define EC_FIFO_WR_EMPTY 17
|
|
|
|
#define EC_FIFO_WR_DONE 18
|
|
|
|
#define EC_COMMAND 19
|
|
|
|
#define NUM_EC 20
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* for compatibility */
|
2008-11-01 16:14:28 +00:00
|
|
|
static long last_disk_activity = -1;
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
/** static, private data **/
|
2007-06-30 02:08:27 +00:00
|
|
|
static bool initialized = false;
|
2007-07-25 06:15:07 +00:00
|
|
|
|
|
|
|
static long next_yield = 0;
|
2007-11-20 22:45:46 +00:00
|
|
|
#define MIN_YIELD_PERIOD 1000
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2006-11-21 22:55:39 +00:00
|
|
|
static tSDCardInfo card_info[2];
|
2007-07-25 06:15:07 +00:00
|
|
|
static tSDCardInfo *currcard = NULL; /* current active card */
|
|
|
|
|
|
|
|
struct sd_card_status
|
|
|
|
{
|
|
|
|
int retry;
|
|
|
|
int retry_max;
|
|
|
|
};
|
|
|
|
|
2008-11-09 06:17:14 +00:00
|
|
|
static struct sd_card_status sd_status[NUM_VOLUMES] =
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
|
|
|
{ 0, 1 },
|
2008-10-29 18:32:24 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2007-07-25 06:15:07 +00:00
|
|
|
{ 0, 10 }
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
};
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Shoot for around 75% usage */
|
|
|
|
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x1c0)/sizeof(long)];
|
|
|
|
static const char sd_thread_name[] = "ata/sd";
|
2008-04-06 04:34:57 +00:00
|
|
|
static struct mutex sd_mtx SHAREDBSS_ATTR;
|
2006-11-25 13:04:50 +00:00
|
|
|
static struct event_queue sd_queue;
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Posted when card plugged status has changed */
|
|
|
|
#define SD_HOTSWAP 1
|
2007-07-25 06:15:07 +00:00
|
|
|
/* Actions taken by sd_thread when card status has changed */
|
|
|
|
enum sd_thread_actions
|
|
|
|
{
|
|
|
|
SDA_NONE = 0x0,
|
|
|
|
SDA_UNMOUNTED = 0x1,
|
|
|
|
SDA_MOUNTED = 0x2
|
|
|
|
};
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2006-11-21 22:55:39 +00:00
|
|
|
/* Private Functions */
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static unsigned int check_time[NUM_EC];
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static inline bool sd_check_timeout(long timeout, int id)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
return !TIME_AFTER(USEC_TIMER, check_time[id] + timeout);
|
2007-06-30 02:08:27 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static bool sd_poll_status(unsigned int trigger, long timeout)
|
2007-06-30 02:08:27 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
long t = USEC_TIMER;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
while ((MMC_STAT & trigger) == 0)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
long time = USEC_TIMER;
|
|
|
|
|
|
|
|
if (TIME_AFTER(time, next_yield))
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
long ty = USEC_TIMER;
|
2008-03-25 02:34:12 +00:00
|
|
|
yield();
|
2007-07-25 06:15:07 +00:00
|
|
|
timeout += USEC_TIMER - ty;
|
|
|
|
next_yield = ty + MIN_YIELD_PERIOD;
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (TIME_AFTER(time, t + timeout))
|
2007-06-30 02:08:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static int sd_command(unsigned int cmd, unsigned long arg1,
|
2009-02-01 09:08:12 +00:00
|
|
|
unsigned int *response, unsigned int cmdat)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
int i, words; /* Number of 16 bit words to read from MMC_RES */
|
2007-06-30 02:08:27 +00:00
|
|
|
unsigned int data[9];
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_CMD = cmd;
|
|
|
|
MMC_ARGH = (unsigned int)((arg1 & 0xffff0000) >> 16);
|
|
|
|
MMC_ARGL = (unsigned int)((arg1 & 0xffff));
|
|
|
|
MMC_CMDAT = cmdat;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
if (!sd_poll_status(STAT_END_CMD_RES, 100000))
|
2007-07-25 06:15:07 +00:00
|
|
|
return -EC_COMMAND;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
if ((MMC_STAT & STAT_ERROR_BITS) != 0)
|
2007-07-25 06:15:07 +00:00
|
|
|
/* Error sending command */
|
2009-02-01 09:08:12 +00:00
|
|
|
return -EC_COMMAND - (MMC_STAT & STAT_ERROR_BITS)*100;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
if (cmd == SD_GO_IDLE_STATE)
|
2007-07-25 06:15:07 +00:00
|
|
|
return 0; /* no response here */
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
words = (cmdat == CMDAT_RES_TYPE2) ? 9 : 3;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
for (i = 0; i < words; i++) /* MMC_RES is read MSB first */
|
|
|
|
data[i] = MMC_RES; /* Read most significant 16-bit word */
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (response == NULL)
|
|
|
|
{
|
|
|
|
/* response discarded */
|
|
|
|
}
|
2009-02-01 09:08:12 +00:00
|
|
|
else if (cmdat == CMDAT_RES_TYPE2)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Response type 2 has the following structure:
|
|
|
|
* [135:135] Start Bit - '0'
|
|
|
|
* [134:134] Transmission bit - '0'
|
|
|
|
* [133:128] Reserved - '111111'
|
|
|
|
* [127:001] CID or CSD register including internal CRC7
|
|
|
|
* [000:000] End Bit - '1'
|
|
|
|
*/
|
2007-07-25 06:15:07 +00:00
|
|
|
response[3] = (data[0]<<24) + (data[1]<<8) + (data[2]>>8);
|
|
|
|
response[2] = (data[2]<<24) + (data[3]<<8) + (data[4]>>8);
|
|
|
|
response[1] = (data[4]<<24) + (data[5]<<8) + (data[6]>>8);
|
|
|
|
response[0] = (data[6]<<24) + (data[7]<<8) + (data[8]>>8);
|
2007-06-30 02:08:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-22 00:32:45 +00:00
|
|
|
/* Response types 1, 1b, 3, 6, 7 have the following structure:
|
2007-06-30 02:08:27 +00:00
|
|
|
* Types 4 and 5 are not supported.
|
|
|
|
*
|
|
|
|
* [47] Start bit - '0'
|
|
|
|
* [46] Transmission bit - '0'
|
2007-08-22 00:32:45 +00:00
|
|
|
* [45:40] R1, R1b, R6, R7: Command index
|
2007-06-30 02:08:27 +00:00
|
|
|
* R3: Reserved - '111111'
|
|
|
|
* [39:8] R1, R1b: Card Status
|
|
|
|
* R3: OCR Register
|
|
|
|
* R6: [31:16] RCA
|
|
|
|
* [15: 0] Card Status Bits 23, 22, 19, 12:0
|
|
|
|
* [23] COM_CRC_ERROR
|
|
|
|
* [22] ILLEGAL_COMMAND
|
|
|
|
* [19] ERROR
|
|
|
|
* [12:9] CURRENT_STATE
|
|
|
|
* [8] READY_FOR_DATA
|
|
|
|
* [7:6]
|
2008-11-09 23:40:21 +00:00
|
|
|
* [5] SD_APP_CMD
|
2007-06-30 02:08:27 +00:00
|
|
|
* [4]
|
|
|
|
* [3] AKE_SEQ_ERROR
|
|
|
|
* [2] Reserved
|
|
|
|
* [1:0] Reserved for test mode
|
2007-08-22 00:32:45 +00:00
|
|
|
* R7: [19:16] Voltage accepted
|
|
|
|
* [15:8] echo-back of check pattern
|
2007-06-30 02:08:27 +00:00
|
|
|
* [7:1] R1, R1b: CRC7
|
|
|
|
* R3: Reserved - '1111111'
|
|
|
|
* [0] End Bit - '1'
|
|
|
|
*/
|
2007-07-25 06:15:07 +00:00
|
|
|
response[0] = (data[0]<<24) + (data[1]<<8) + (data[2]>>8);
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
return 0;
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static int sd_wait_for_state(unsigned int state, int id)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2006-11-21 22:55:39 +00:00
|
|
|
unsigned int response = 0;
|
2007-07-25 06:15:07 +00:00
|
|
|
unsigned int timeout = 0x80000;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
check_time[id] = USEC_TIMER;
|
|
|
|
|
|
|
|
while (1)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
int ret = sd_command(SD_SEND_STATUS, currcard->rca, &response, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
long us;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret*100 - id;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
if (((response >> 9) & 0xf) == state)
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_SD_STATE = state;
|
2007-07-25 06:15:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (!sd_check_timeout(timeout, id))
|
|
|
|
return -EC_WAIT_STATE_FAILED*100 - id;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
us = USEC_TIMER;
|
|
|
|
if (TIME_AFTER(us, next_yield))
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
yield();
|
2007-07-25 06:15:07 +00:00
|
|
|
timeout += USEC_TIMER - us;
|
|
|
|
next_yield = us + MIN_YIELD_PERIOD;
|
|
|
|
}
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
static inline void copy_read_sectors_fast(unsigned char **buf)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Copy one chunk of 16 words using best method for start alignment */
|
|
|
|
switch ( (intptr_t)*buf & 3 )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
asm volatile (
|
|
|
|
"ldmia %[data], { r2-r9 } \r\n"
|
|
|
|
"orr r2, r2, r3, lsl #16 \r\n"
|
|
|
|
"orr r4, r4, r5, lsl #16 \r\n"
|
|
|
|
"orr r6, r6, r7, lsl #16 \r\n"
|
|
|
|
"orr r8, r8, r9, lsl #16 \r\n"
|
|
|
|
"stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
|
|
|
|
"ldmia %[data], { r2-r9 } \r\n"
|
|
|
|
"orr r2, r2, r3, lsl #16 \r\n"
|
|
|
|
"orr r4, r4, r5, lsl #16 \r\n"
|
|
|
|
"orr r6, r6, r7, lsl #16 \r\n"
|
|
|
|
"orr r8, r8, r9, lsl #16 \r\n"
|
|
|
|
"stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
|
|
|
|
: [buf]"+&r"(*buf)
|
2009-02-01 09:08:12 +00:00
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
2007-06-30 02:08:27 +00:00
|
|
|
: "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
asm volatile (
|
|
|
|
"ldmia %[data], { r2-r9 } \r\n"
|
|
|
|
"orr r3, r2, r3, lsl #16 \r\n"
|
|
|
|
"strb r3, [%[buf]], #1 \r\n"
|
|
|
|
"mov r3, r3, lsr #8 \r\n"
|
|
|
|
"strh r3, [%[buf]], #2 \r\n"
|
|
|
|
"mov r3, r3, lsr #16 \r\n"
|
|
|
|
"orr r3, r3, r4, lsl #8 \r\n"
|
|
|
|
"orr r3, r3, r5, lsl #24 \r\n"
|
|
|
|
"mov r5, r5, lsr #8 \r\n"
|
|
|
|
"orr r5, r5, r6, lsl #8 \r\n"
|
|
|
|
"orr r5, r5, r7, lsl #24 \r\n"
|
|
|
|
"mov r7, r7, lsr #8 \r\n"
|
|
|
|
"orr r7, r7, r8, lsl #8 \r\n"
|
|
|
|
"orr r7, r7, r9, lsl #24 \r\n"
|
|
|
|
"mov r2, r9, lsr #8 \r\n"
|
|
|
|
"stmia %[buf]!, { r3, r5, r7 } \r\n"
|
|
|
|
"ldmia %[data], { r3-r10 } \r\n"
|
|
|
|
"orr r2, r2, r3, lsl #8 \r\n"
|
|
|
|
"orr r2, r2, r4, lsl #24 \r\n"
|
|
|
|
"mov r4, r4, lsr #8 \r\n"
|
|
|
|
"orr r4, r4, r5, lsl #8 \r\n"
|
|
|
|
"orr r4, r4, r6, lsl #24 \r\n"
|
|
|
|
"mov r6, r6, lsr #8 \r\n"
|
|
|
|
"orr r6, r6, r7, lsl #8 \r\n"
|
|
|
|
"orr r6, r6, r8, lsl #24 \r\n"
|
|
|
|
"mov r8, r8, lsr #8 \r\n"
|
|
|
|
"orr r8, r8, r9, lsl #8 \r\n"
|
|
|
|
"orr r8, r8, r10, lsl #24 \r\n"
|
|
|
|
"mov r10, r10, lsr #8 \r\n"
|
|
|
|
"stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
|
|
|
|
"strb r10, [%[buf]], #1 \r\n"
|
|
|
|
: [buf]"+&r"(*buf)
|
2009-02-01 09:08:12 +00:00
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
2007-06-30 02:08:27 +00:00
|
|
|
: "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
asm volatile (
|
|
|
|
"ldmia %[data], { r2-r9 } \r\n"
|
|
|
|
"strh r2, [%[buf]], #2 \r\n"
|
|
|
|
"orr r3, r3, r4, lsl #16 \r\n"
|
|
|
|
"orr r5, r5, r6, lsl #16 \r\n"
|
|
|
|
"orr r7, r7, r8, lsl #16 \r\n"
|
|
|
|
"stmia %[buf]!, { r3, r5, r7 } \r\n"
|
|
|
|
"ldmia %[data], { r2-r8, r10 } \r\n"
|
|
|
|
"orr r2, r9, r2, lsl #16 \r\n"
|
|
|
|
"orr r3, r3, r4, lsl #16 \r\n"
|
|
|
|
"orr r5, r5, r6, lsl #16 \r\n"
|
|
|
|
"orr r7, r7, r8, lsl #16 \r\n"
|
|
|
|
"stmia %[buf]!, { r2, r3, r5, r7 } \r\n"
|
|
|
|
"strh r10, [%[buf]], #2 \r\n"
|
|
|
|
: [buf]"+&r"(*buf)
|
2009-02-01 09:08:12 +00:00
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
2007-06-30 02:08:27 +00:00
|
|
|
: "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
asm volatile (
|
|
|
|
"ldmia %[data], { r2-r9 } \r\n"
|
|
|
|
"orr r3, r2, r3, lsl #16 \r\n"
|
|
|
|
"strb r3, [%[buf]], #1 \r\n"
|
|
|
|
"mov r3, r3, lsr #8 \r\n"
|
|
|
|
"orr r3, r3, r4, lsl #24 \r\n"
|
|
|
|
"mov r4, r4, lsr #8 \r\n"
|
|
|
|
"orr r5, r4, r5, lsl #8 \r\n"
|
|
|
|
"orr r5, r5, r6, lsl #24 \r\n"
|
|
|
|
"mov r6, r6, lsr #8 \r\n"
|
|
|
|
"orr r7, r6, r7, lsl #8 \r\n"
|
|
|
|
"orr r7, r7, r8, lsl #24 \r\n"
|
|
|
|
"mov r8, r8, lsr #8 \r\n"
|
|
|
|
"orr r2, r8, r9, lsl #8 \r\n"
|
|
|
|
"stmia %[buf]!, { r3, r5, r7 } \r\n"
|
|
|
|
"ldmia %[data], { r3-r10 } \r\n"
|
|
|
|
"orr r2, r2, r3, lsl #24 \r\n"
|
|
|
|
"mov r3, r3, lsr #8 \r\n"
|
|
|
|
"orr r4, r3, r4, lsl #8 \r\n"
|
|
|
|
"orr r4, r4, r5, lsl #24 \r\n"
|
|
|
|
"mov r5, r5, lsr #8 \r\n"
|
|
|
|
"orr r6, r5, r6, lsl #8 \r\n"
|
|
|
|
"orr r6, r6, r7, lsl #24 \r\n"
|
|
|
|
"mov r7, r7, lsr #8 \r\n"
|
|
|
|
"orr r8, r7, r8, lsl #8 \r\n"
|
|
|
|
"orr r8, r8, r9, lsl #24 \r\n"
|
|
|
|
"mov r9, r9, lsr #8 \r\n"
|
|
|
|
"orr r10, r9, r10, lsl #8 \r\n"
|
|
|
|
"stmia %[buf]!, { r2, r4, r6, r8 } \r\n"
|
|
|
|
"strh r10, [%[buf]], #2 \r\n"
|
|
|
|
"mov r10, r10, lsr #16 \r\n"
|
|
|
|
"strb r10, [%[buf]], #1 \r\n"
|
|
|
|
: [buf]"+&r"(*buf)
|
2009-02-01 09:08:12 +00:00
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
2007-06-30 02:08:27 +00:00
|
|
|
: "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
|
|
|
|
);
|
|
|
|
break;
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
static inline void copy_read_sectors_slow(unsigned char** buf)
|
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
int cnt = FIFO_LEN;
|
2007-06-30 02:08:27 +00:00
|
|
|
int t;
|
|
|
|
|
|
|
|
/* Copy one chunk of 16 words */
|
|
|
|
asm volatile (
|
|
|
|
"1: \r\n"
|
|
|
|
"ldrh %[t], [%[data]] \r\n"
|
|
|
|
"strb %[t], [%[buf]], #1 \r\n"
|
|
|
|
"mov %[t], %[t], lsr #8 \r\n"
|
|
|
|
"strb %[t], [%[buf]], #1 \r\n"
|
|
|
|
"subs %[cnt], %[cnt], #1 \r\n"
|
|
|
|
"bgt 1b \r\n"
|
|
|
|
: [cnt]"+&r"(cnt), [buf]"+&r"(*buf),
|
|
|
|
[t]"=&r"(t)
|
2009-02-01 09:08:12 +00:00
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
2007-06-30 02:08:27 +00:00
|
|
|
);
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Writes have to be kept slow for now */
|
2007-07-25 06:15:07 +00:00
|
|
|
static inline void copy_write_sectors(const unsigned char** buf)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2009-02-03 20:21:02 +00:00
|
|
|
int cnt = FIFO_LEN - 1;
|
2007-07-25 06:15:07 +00:00
|
|
|
unsigned t;
|
2009-02-03 20:21:02 +00:00
|
|
|
long time;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-03 20:21:02 +00:00
|
|
|
time = USEC_TIMER + 3;
|
|
|
|
if (((intptr_t)*buf & 3) == 0)
|
|
|
|
{
|
|
|
|
asm volatile (
|
|
|
|
"ldmia %[buf]!, { r3, r5, r7, r9 } \r\n"
|
|
|
|
"mov r4, r3, lsr #16 \r\n"
|
|
|
|
"mov r6, r5, lsr #16 \r\n"
|
|
|
|
"mov r8, r7, lsr #16 \r\n"
|
|
|
|
"mov r10, r9, lsr #16 \r\n"
|
|
|
|
"stmia %[data], { r3-r10 } \r\n"
|
|
|
|
"ldmia %[buf]!, { r3, r5, r7, r9 } \r\n"
|
|
|
|
"mov r4, r3, lsr #16 \r\n"
|
|
|
|
"mov r6, r5, lsr #16 \r\n"
|
|
|
|
"mov r8, r7, lsr #16 \r\n"
|
|
|
|
"mov %[t], r9, lsr #16 \r\n"
|
|
|
|
"stmia %[data], { r3-r9 } \r\n"
|
|
|
|
: [buf]"+&r"(*buf), [t]"=&r"(t)
|
|
|
|
: [data]"r"(&MMC_DATA_FIFO)
|
|
|
|
: "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
2007-02-04 03:20:36 +00:00
|
|
|
{
|
2009-02-03 20:21:02 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
t = *(*buf)++;
|
|
|
|
t |= *(*buf)++ << 8;
|
|
|
|
MMC_DATA_FIFO = t;
|
|
|
|
} while (--cnt > 0); /* tail loop is faster */
|
2007-07-25 06:15:07 +00:00
|
|
|
t = *(*buf)++;
|
|
|
|
t |= *(*buf)++ << 8;
|
2009-02-03 20:21:02 +00:00
|
|
|
}
|
|
|
|
/* Don't write the last word before at least 3 usec have elapsed since FIFO_EMPTY */
|
|
|
|
/* This prevents the 'two bytes inserted' bug. */
|
|
|
|
|
|
|
|
while (!TIME_AFTER(USEC_TIMER, time));
|
|
|
|
MMC_DATA_FIFO = t;
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static int sd_select_bank(unsigned char bank)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2006-11-21 22:55:39 +00:00
|
|
|
unsigned char card_data[512];
|
2007-07-25 06:15:07 +00:00
|
|
|
const unsigned char* write_buf;
|
|
|
|
int i, ret;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
memset(card_data, 0, 512);
|
2007-07-25 06:15:07 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_SEL_BANK);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_BLKLEN = 512;
|
|
|
|
MMC_NUMBLK = 1;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(35, 0, NULL, /* CMD35 is vendor specific */
|
|
|
|
0x1c00 | CMDAT_WR_RD | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_SD_STATE = SD_PRG;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
card_data[0] = bank;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Write the card data */
|
|
|
|
write_buf = card_data;
|
2007-07-25 06:15:07 +00:00
|
|
|
for (i = 0; i < BLOCK_SIZE/2; i += FIFO_LEN)
|
2007-06-30 02:08:27 +00:00
|
|
|
{
|
|
|
|
/* Wait for the FIFO to empty */
|
2009-02-01 09:08:12 +00:00
|
|
|
if (sd_poll_status(STAT_XMIT_FIFO_EMPTY, 10000))
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
|
|
|
copy_write_sectors(&write_buf); /* Copy one chunk of 16 words */
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
return -EC_FIFO_SEL_BANK_EMPTY;
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
if (!sd_poll_status(STAT_PRG_DONE, 10000))
|
2007-07-25 06:15:07 +00:00
|
|
|
return -EC_FIFO_SEL_BANK_DONE;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
currcard->current_bank = bank;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
return 0;
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
static void sd_card_mux(int card_no)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
/* Set the current card mux */
|
2009-03-14 02:47:14 +00:00
|
|
|
#if defined(SANSA_E200)
|
2007-06-30 02:08:27 +00:00
|
|
|
if (card_no == 0)
|
|
|
|
{
|
2007-11-05 16:12:13 +00:00
|
|
|
GPO32_VAL |= 0x4;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_ENABLE, 0x7a);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x7a);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_ENABLE, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x1f);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-05 16:12:13 +00:00
|
|
|
GPO32_VAL &= ~0x4;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOD_ENABLE, 0x1f);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_ENABLE, 0x7a);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x7a);
|
|
|
|
GPIO_SET_BITWISE( GPIOA_OUTPUT_EN, 0x7a);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
|
|
|
|
}
|
2009-03-14 02:47:14 +00:00
|
|
|
#elif defined(SANSA_C200)
|
2007-09-06 03:28:58 +00:00
|
|
|
if (card_no == 0)
|
|
|
|
{
|
2007-11-05 16:12:13 +00:00
|
|
|
GPO32_VAL |= 0x4;
|
2007-09-06 03:28:58 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOD_ENABLE, 0x1f);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_ENABLE, 0x7a);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x7a);
|
|
|
|
GPIO_SET_BITWISE( GPIOA_OUTPUT_EN, 0x7a);
|
2007-09-06 03:28:58 +00:00
|
|
|
|
|
|
|
outl(inl(0x70000014) & ~(0x3ffff), 0x70000014);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-05 16:12:13 +00:00
|
|
|
GPO32_VAL &= ~0x4;
|
2007-09-06 03:28:58 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_ENABLE, 0x7a);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x7a);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_ENABLE, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x1f);
|
|
|
|
GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x1f);
|
2007-09-06 03:28:58 +00:00
|
|
|
|
|
|
|
outl((inl(0x70000014) & ~(0x3ffff)) | 0x255aa, 0x70000014);
|
|
|
|
}
|
2009-03-14 02:47:14 +00:00
|
|
|
#elif defined(PHILIPS_SA9200)
|
|
|
|
/* only 1 "card" (no external memory card) */
|
|
|
|
(void)card_no;
|
|
|
|
|
|
|
|
GPIO_SET_BITWISE(GPIOH_ENABLE, 0x80);
|
|
|
|
GPIO_SET_BITWISE(GPIOH_OUTPUT_EN, 0x80);
|
|
|
|
|
|
|
|
outl(0x255aa, 0x70000014);
|
|
|
|
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_ENABLE, 0x04);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x04);
|
|
|
|
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_ENABLE, 0x7a);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x7a);
|
|
|
|
|
|
|
|
GPIO_SET_BITWISE(GPIOH_OUTPUT_VAL, 0x80);
|
|
|
|
GPIO_SET_BITWISE(GPIOH_OUTPUT_EN, 0x80);
|
2007-09-06 03:28:58 +00:00
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_init_device(int card_no)
|
|
|
|
{
|
|
|
|
/* SD Protocol registers */
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
unsigned int response = 0;
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int c_size;
|
|
|
|
unsigned long c_mult;
|
|
|
|
unsigned char carddata[512];
|
|
|
|
unsigned char *dataptr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Enable and initialise controller */
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_CLKRT = 6; /* switch to lowest clock rate */
|
2007-07-25 06:15:07 +00:00
|
|
|
|
|
|
|
/* Initialise card data as blank */
|
|
|
|
memset(currcard, 0, sizeof(*currcard));
|
|
|
|
|
|
|
|
/* Switch card mux to card to initialize */
|
|
|
|
sd_card_mux(card_no);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
/* Init NAND */
|
2009-03-14 02:47:14 +00:00
|
|
|
#if defined(PHILIPS_SA9200)
|
|
|
|
MMC_INIT_1 |= (1 << 15);
|
|
|
|
MMC_INIT_2 |= (1 << 15);
|
|
|
|
MMC_INIT_2 &= ~(3 << 12);
|
|
|
|
MMC_INIT_2 |= (1 << 12);
|
|
|
|
MMC_INIT_1 &= ~(3 << 12);
|
|
|
|
MMC_INIT_1 |= (1 << 12);
|
|
|
|
#else
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_INIT_1 |= (1 << 15);
|
|
|
|
MMC_INIT_2 |= (1 << 15);
|
|
|
|
MMC_INIT_2 &= ~(3 << 12);
|
|
|
|
MMC_INIT_2 |= (1 << 13);
|
|
|
|
MMC_INIT_1 &= ~(3 << 12);
|
|
|
|
MMC_INIT_1 |= (1 << 13);
|
2009-03-14 02:47:14 +00:00
|
|
|
#endif
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
DEV_EN |= DEV_ATA; /* Enable controller */
|
|
|
|
DEV_RS |= DEV_ATA; /* Reset controller */
|
|
|
|
DEV_RS &=~DEV_ATA; /* Clear Reset */
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_SD_STATE = SD_TRAN;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_I_MASK = 0xf; /* disable interrupts */
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_GO_IDLE_STATE, 0, NULL, 0x100);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
check_time[EC_POWER_UP] = USEC_TIMER;
|
2007-10-08 20:39:46 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
/* Check for SDHC:
|
2008-11-09 23:40:21 +00:00
|
|
|
- non-SDHC cards simply ignore SD_SEND_IF_COND (CMD8) and we get error -219,
|
2007-08-22 00:32:45 +00:00
|
|
|
which we can just ignore and assume we're dealing with standard SD.
|
|
|
|
- SDHC cards echo back the argument into the response. This is how we
|
|
|
|
tell if the card is SDHC.
|
|
|
|
*/
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SEND_IF_COND,0x1aa, &response,
|
|
|
|
CMDAT_DATA_EN | CMDAT_RES_TYPE3);
|
2007-08-22 00:32:45 +00:00
|
|
|
if ( (ret < 0) && (ret!=-219) )
|
|
|
|
goto card_init_error;
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-08-22 00:32:45 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
while ((currcard->ocr & (1 << 31)) == 0) /* until card is powered up */
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_APP_CMD, currcard->rca, NULL, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
if(response == 0x1aa)
|
|
|
|
{
|
|
|
|
/* SDHC */
|
|
|
|
ret = sd_command(SD_APP_OP_COND, (1<<30)|0x100000,
|
2009-02-01 09:08:12 +00:00
|
|
|
&currcard->ocr, CMDAT_RES_TYPE3);
|
2007-10-08 20:39:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* HAVE_HOTSWAP */
|
|
|
|
{
|
2007-08-22 00:32:45 +00:00
|
|
|
/* SD Standard */
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_APP_OP_COND, 0x100000, &currcard->ocr,
|
|
|
|
CMDAT_RES_TYPE3);
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
2007-10-08 20:39:46 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (!sd_check_timeout(5000000, EC_POWER_UP))
|
|
|
|
{
|
|
|
|
ret = -EC_POWER_UP;
|
|
|
|
goto card_init_error;
|
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_ALL_SEND_CID, 0, currcard->cid, CMDAT_RES_TYPE2);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SEND_RELATIVE_ADDR, 0, &currcard->rca, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SEND_CSD, currcard->rca, currcard->csd, CMDAT_RES_TYPE2);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
|
|
|
/* These calculations come from the Sandisk SD card product manual */
|
2007-08-22 00:32:45 +00:00
|
|
|
if( (currcard->csd[3]>>30) == 0)
|
|
|
|
{
|
|
|
|
/* CSD version 1.0 */
|
|
|
|
c_size = ((currcard->csd[2] & 0x3ff) << 2) + (currcard->csd[1]>>30) + 1;
|
|
|
|
c_mult = 4 << ((currcard->csd[1] >> 15) & 7);
|
|
|
|
currcard->max_read_bl_len = 1 << ((currcard->csd[2] >> 16) & 15);
|
|
|
|
currcard->block_size = BLOCK_SIZE; /* Always use 512 byte blocks */
|
|
|
|
currcard->numblocks = c_size * c_mult * (currcard->max_read_bl_len/512);
|
|
|
|
currcard->capacity = currcard->numblocks * currcard->block_size;
|
|
|
|
}
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
else if( (currcard->csd[3]>>30) == 1)
|
|
|
|
{
|
|
|
|
/* CSD version 2.0 */
|
|
|
|
c_size = ((currcard->csd[2] & 0x3f) << 16) + (currcard->csd[1]>>16) + 1;
|
|
|
|
currcard->max_read_bl_len = 1 << ((currcard->csd[2] >> 16) & 0xf);
|
|
|
|
currcard->block_size = BLOCK_SIZE; /* Always use 512 byte blocks */
|
2008-02-09 21:07:44 +00:00
|
|
|
currcard->numblocks = c_size << 10;
|
2007-08-22 00:32:45 +00:00
|
|
|
currcard->capacity = currcard->numblocks * currcard->block_size;
|
|
|
|
}
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif /* HAVE_HOTSWAP */
|
2007-08-22 00:32:45 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_CLKRT = 0; /* switch to highest clock rate */
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SELECT_CARD, currcard->rca, NULL,
|
|
|
|
0x80 | CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_APP_CMD, currcard->rca, NULL, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SET_BUS_WIDTH, currcard->rca | 2, NULL,
|
|
|
|
CMDAT_RES_TYPE1); /* 4 bit */
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SET_BLOCKLEN, currcard->block_size, NULL,
|
|
|
|
CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_BLKLEN = currcard->block_size;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-08-22 00:32:45 +00:00
|
|
|
/* If this card is >4GB & not SDHC, then we need to enable bank switching */
|
|
|
|
if( (currcard->numblocks >= BLOCKS_PER_BANK) &&
|
|
|
|
((currcard->ocr & (1<<30)) == 0) )
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_SD_STATE = SD_TRAN;
|
|
|
|
MMC_NUMBLK = 1;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_SWITCH_FUNC, 0x80ffffef, NULL,
|
|
|
|
0x1c00 | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2007-07-12 06:50:42 +00:00
|
|
|
goto card_init_error;
|
|
|
|
|
2006-11-21 22:55:39 +00:00
|
|
|
/* Read 512 bytes from the card.
|
|
|
|
The first 512 bits contain the status information
|
|
|
|
TODO: Do something useful with this! */
|
|
|
|
dataptr = carddata;
|
2007-07-25 06:15:07 +00:00
|
|
|
for (i = 0; i < BLOCK_SIZE/2; i += FIFO_LEN)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
|
|
|
/* Wait for the FIFO to be full */
|
2009-02-01 09:08:12 +00:00
|
|
|
if (sd_poll_status(STAT_RECV_FIFO_FULL, 100000))
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
|
|
|
copy_read_sectors_slow(&dataptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -EC_FIFO_ENA_BANK_EMPTY;
|
|
|
|
goto card_init_error;
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-12 06:50:42 +00:00
|
|
|
currcard->initialized = 1;
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Card failed to initialize so disable it */
|
|
|
|
card_init_error:
|
2007-07-25 06:15:07 +00:00
|
|
|
currcard->initialized = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock must already be aquired */
|
|
|
|
static void sd_select_device(int card_no)
|
|
|
|
{
|
|
|
|
currcard = &card_info[card_no];
|
|
|
|
|
|
|
|
if (card_no == 0)
|
|
|
|
{
|
|
|
|
/* Main card always gets a chance */
|
|
|
|
sd_status[0].retry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currcard->initialized > 0)
|
|
|
|
{
|
|
|
|
/* This card is already initialized - switch to it */
|
|
|
|
sd_card_mux(card_no);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currcard->initialized == 0)
|
|
|
|
{
|
|
|
|
/* Card needs (re)init */
|
|
|
|
sd_init_device(card_no);
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2006-11-21 22:55:39 +00:00
|
|
|
/* API Functions */
|
2006-08-01 22:28:14 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
static void sd_led(bool onoff)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
led(onoff);
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int incount,
|
2006-08-01 22:28:14 +00:00
|
|
|
void* inbuf)
|
|
|
|
{
|
2008-10-29 18:32:24 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
2007-10-08 20:39:46 +00:00
|
|
|
const int drive = 0;
|
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
int ret;
|
2007-06-30 02:08:27 +00:00
|
|
|
unsigned char *buf, *buf_end;
|
|
|
|
int bank;
|
|
|
|
|
2006-11-21 22:55:39 +00:00
|
|
|
/* TODO: Add DMA support. */
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&sd_mtx);
|
2008-11-24 03:59:21 +00:00
|
|
|
sd_enable(true);
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_led(true);
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_read_retry:
|
2007-11-06 07:21:08 +00:00
|
|
|
if (drive != 0 && !card_detect_target())
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
/* no external sd-card inserted */
|
2007-07-25 06:15:07 +00:00
|
|
|
ret = -EC_NOCARD;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
sd_select_device(drive);
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (currcard->initialized < 0)
|
|
|
|
{
|
|
|
|
ret = currcard->initialized;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-07-12 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
last_disk_activity = current_tick;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-08-22 00:32:45 +00:00
|
|
|
/* Only switch banks with non-SDHC cards */
|
|
|
|
if((currcard->ocr & (1<<30))==0)
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2007-08-22 00:32:45 +00:00
|
|
|
bank = start / BLOCKS_PER_BANK;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-08-22 00:32:45 +00:00
|
|
|
if (currcard->current_bank != bank)
|
|
|
|
{
|
|
|
|
ret = sd_select_bank(bank);
|
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start -= bank * BLOCKS_PER_BANK;
|
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_ENTRY);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_NUMBLK = incount;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
if(currcard->ocr & (1<<30) )
|
|
|
|
{
|
|
|
|
/* SDHC */
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_READ_MULTIPLE_BLOCK, start, NULL,
|
|
|
|
0x1c00 | CMDAT_BUSY | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-08-22 00:32:45 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_READ_MULTIPLE_BLOCK, start * BLOCK_SIZE, NULL,
|
|
|
|
0x1c00 | CMDAT_BUSY | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* TODO: Don't assume BLOCK_SIZE == SECTOR_SIZE */
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
buf_end = (unsigned char *)inbuf + incount * currcard->block_size;
|
|
|
|
for (buf = inbuf; buf < buf_end;)
|
|
|
|
{
|
|
|
|
/* Wait for the FIFO to be full */
|
2009-02-01 09:08:12 +00:00
|
|
|
if (sd_poll_status(STAT_RECV_FIFO_FULL, 0x80000))
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
|
|
|
copy_read_sectors_fast(&buf); /* Copy one chunk of 16 words */
|
|
|
|
/* TODO: Switch bank if necessary */
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
ret = -EC_FIFO_READ_FULL;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_READ_EXIT);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_error;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_led(false);
|
2008-11-24 03:59:21 +00:00
|
|
|
sd_enable(false);
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
return ret;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_read_error:
|
2007-07-25 06:15:07 +00:00
|
|
|
if (sd_status[drive].retry < sd_status[drive].retry_max
|
|
|
|
&& ret != -EC_NOCARD)
|
|
|
|
{
|
|
|
|
sd_status[drive].retry++;
|
|
|
|
currcard->initialized = 0;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_read_retry;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
int sd_write_sectors(IF_MV2(int drive,) unsigned long start, int count,
|
2007-06-30 02:08:27 +00:00
|
|
|
const void* outbuf)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2006-11-21 22:55:39 +00:00
|
|
|
/* Write support is not finished yet */
|
2007-06-30 02:08:27 +00:00
|
|
|
/* TODO: The standard suggests using ACMD23 prior to writing multiple blocks
|
2006-11-21 22:55:39 +00:00
|
|
|
to improve performance */
|
2008-10-29 18:32:24 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
2007-10-08 20:39:46 +00:00
|
|
|
const int drive = 0;
|
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
int ret;
|
|
|
|
const unsigned char *buf, *buf_end;
|
2007-06-30 02:08:27 +00:00
|
|
|
int bank;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&sd_mtx);
|
2008-11-24 03:59:21 +00:00
|
|
|
sd_enable(true);
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_led(true);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_write_retry:
|
2007-11-06 07:21:08 +00:00
|
|
|
if (drive != 0 && !card_detect_target())
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
/* no external sd-card inserted */
|
2007-07-25 06:15:07 +00:00
|
|
|
ret = -EC_NOCARD;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
sd_select_device(drive);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (currcard->initialized < 0)
|
|
|
|
{
|
|
|
|
ret = currcard->initialized;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-07-12 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 00:32:45 +00:00
|
|
|
/* Only switch banks with non-SDHC cards */
|
|
|
|
if((currcard->ocr & (1<<30))==0)
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2007-08-22 00:32:45 +00:00
|
|
|
bank = start / BLOCKS_PER_BANK;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-08-22 00:32:45 +00:00
|
|
|
if (currcard->current_bank != bank)
|
|
|
|
{
|
|
|
|
ret = sd_select_bank(bank);
|
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start -= bank * BLOCKS_PER_BANK;
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
check_time[EC_WRITE_TIMEOUT] = USEC_TIMER;
|
2007-07-25 06:15:07 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_ENTRY);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
MMC_NUMBLK = count;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-08-22 00:32:45 +00:00
|
|
|
if(currcard->ocr & (1<<30) )
|
|
|
|
{
|
|
|
|
/* SDHC */
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start, NULL,
|
2009-02-03 20:21:02 +00:00
|
|
|
CMDAT_WR_RD | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-08-22 00:32:45 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_WRITE_MULTIPLE_BLOCK, start*BLOCK_SIZE, NULL,
|
2009-02-03 20:21:02 +00:00
|
|
|
CMDAT_WR_RD | CMDAT_DATA_EN | CMDAT_RES_TYPE1);
|
2007-08-22 00:32:45 +00:00
|
|
|
}
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
buf_end = outbuf + count * currcard->block_size - 2*FIFO_LEN;
|
|
|
|
|
|
|
|
for (buf = outbuf; buf <= buf_end;)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
if (buf == buf_end)
|
2006-11-21 22:55:39 +00:00
|
|
|
{
|
2009-02-01 09:08:12 +00:00
|
|
|
/* Set MMC_SD_STATE to SD_PRG for the last buffer fill */
|
|
|
|
MMC_SD_STATE = SD_PRG;
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2009-02-03 20:21:02 +00:00
|
|
|
copy_write_sectors(&buf); /* Copy one chunk of 16 words */
|
|
|
|
/* TODO: Switch bank if necessary */
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* Wait for the FIFO to empty */
|
2009-02-03 20:21:02 +00:00
|
|
|
if (!sd_poll_status(STAT_XMIT_FIFO_EMPTY, 0x80000))
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
2009-02-03 20:21:02 +00:00
|
|
|
ret = -EC_FIFO_WR_EMPTY;
|
|
|
|
goto sd_write_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
2006-11-21 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
last_disk_activity = current_tick;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
if (!sd_poll_status(STAT_PRG_DONE, 0x80000))
|
2007-07-25 06:15:07 +00:00
|
|
|
{
|
|
|
|
ret = -EC_FIFO_WR_DONE;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2009-02-01 09:08:12 +00:00
|
|
|
ret = sd_command(SD_STOP_TRANSMISSION, 0, NULL, CMDAT_RES_TYPE1);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2008-11-09 23:40:21 +00:00
|
|
|
ret = sd_wait_for_state(SD_TRAN, EC_TRAN_WRITE_EXIT);
|
2007-07-25 06:15:07 +00:00
|
|
|
if (ret < 0)
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_error;
|
2006-11-21 22:55:39 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_led(false);
|
2008-11-24 03:59:21 +00:00
|
|
|
sd_enable(false);
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
return ret;
|
2007-04-23 23:26:23 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_write_error:
|
2007-07-25 06:15:07 +00:00
|
|
|
if (sd_status[drive].retry < sd_status[drive].retry_max
|
|
|
|
&& ret != -EC_NOCARD)
|
|
|
|
{
|
|
|
|
sd_status[drive].retry++;
|
|
|
|
currcard->initialized = 0;
|
2008-11-01 16:14:28 +00:00
|
|
|
goto sd_write_retry;
|
2007-07-25 06:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
static void sd_thread(void) __attribute__((noreturn));
|
2006-11-25 13:04:50 +00:00
|
|
|
static void sd_thread(void)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2006-11-25 13:04:50 +00:00
|
|
|
bool idle_notified = false;
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2006-11-25 13:04:50 +00:00
|
|
|
queue_wait_w_tmo(&sd_queue, &ev, HZ);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2006-11-25 13:04:50 +00:00
|
|
|
switch ( ev.id )
|
|
|
|
{
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2008-03-09 14:49:10 +00:00
|
|
|
case SYS_HOTSWAP_INSERTED:
|
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
2008-03-12 10:03:52 +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 */
|
|
|
|
|
|
|
|
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;
|
2008-03-12 02:50:07 +00:00
|
|
|
sd_status[1].retry = 0;
|
2008-03-12 10:03:52 +00:00
|
|
|
|
|
|
|
if (ev.id == SYS_HOTSWAP_INSERTED)
|
|
|
|
disk_mount(1);
|
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
2008-03-12 10:03:52 +00:00
|
|
|
|
|
|
|
/* Access is now safe */
|
2008-03-12 02:50:07 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
2008-03-12 10:03:52 +00:00
|
|
|
fat_unlock();
|
2008-03-09 14:49:10 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2007-06-30 02:08:27 +00:00
|
|
|
case SYS_TIMEOUT:
|
|
|
|
if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
|
|
|
|
{
|
|
|
|
idle_notified = false;
|
|
|
|
}
|
2007-07-25 06:15:07 +00:00
|
|
|
else
|
2007-06-30 02:08:27 +00:00
|
|
|
{
|
2007-07-25 06:15:07 +00:00
|
|
|
/* never let a timer wrap confuse us */
|
|
|
|
next_yield = USEC_TIMER;
|
|
|
|
|
|
|
|
if (!idle_notified)
|
|
|
|
{
|
2008-11-01 16:14:28 +00:00
|
|
|
call_storage_idle_notifys(false);
|
2007-07-25 06:15:07 +00:00
|
|
|
idle_notified = true;
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-07-25 06:15:07 +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);
|
2008-03-09 14:49:10 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
break;
|
2008-02-28 22:51:39 +00:00
|
|
|
case SYS_USB_DISCONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
break;
|
2006-11-25 13:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
void sd_enable(bool on)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-04-23 23:26:23 +00:00
|
|
|
if(on)
|
|
|
|
{
|
|
|
|
DEV_EN |= DEV_ATA; /* Enable controller */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEV_EN &= ~DEV_ATA; /* Disable controller */
|
|
|
|
}
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
2008-03-12 10:03:52 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2008-03-12 11:08:41 +00:00
|
|
|
void card_enable_monitoring_target(bool on)
|
2008-03-12 10:03:52 +00:00
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
#ifdef SANSA_E200
|
|
|
|
GPIO_SET_BITWISE(GPIOA_INT_EN, 0x80);
|
|
|
|
#elif defined(SANSA_C200)
|
|
|
|
GPIO_SET_BITWISE(GPIOL_INT_EN, 0x08);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef SANSA_E200
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x80);
|
|
|
|
#elif defined(SANSA_C200)
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOL_INT_EN, 0x08);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
int sd_init(void)
|
2006-08-01 22:28:14 +00:00
|
|
|
{
|
2007-07-12 06:50:42 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2008-01-18 12:32:03 +00:00
|
|
|
if (!initialized)
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_init(&sd_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_lock(&sd_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
sd_led(false);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
if (!initialized)
|
2006-11-25 13:04:50 +00:00
|
|
|
{
|
|
|
|
initialized = true;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
/* init controller */
|
2009-03-14 02:47:14 +00:00
|
|
|
#if defined(PHILIPS_SA9200)
|
|
|
|
GPIOA_ENABLE = 0x00;
|
|
|
|
GPIO_SET_BITWISE(GPIOD_ENABLE, 0x01);
|
|
|
|
#else
|
2007-06-30 02:08:27 +00:00
|
|
|
outl(inl(0x70000088) & ~(0x4), 0x70000088);
|
|
|
|
outl(inl(0x7000008c) & ~(0x4), 0x7000008c);
|
2007-11-05 17:19:00 +00:00
|
|
|
GPO32_ENABLE |= 0x4;
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_SET_BITWISE(GPIOG_ENABLE, (0x3 << 5));
|
|
|
|
GPIO_SET_BITWISE(GPIOG_OUTPUT_EN, (0x3 << 5));
|
|
|
|
GPIO_SET_BITWISE(GPIOG_OUTPUT_VAL, (0x3 << 5));
|
2009-03-14 02:47:14 +00:00
|
|
|
#endif
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-06-30 02:08:27 +00:00
|
|
|
/* enable card detection port - mask interrupt first */
|
2007-11-04 13:22:17 +00:00
|
|
|
#ifdef SANSA_E200
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x80);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x80);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_ENABLE, 0x80);
|
2007-11-04 13:22:17 +00:00
|
|
|
#elif defined SANSA_C200
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOL_INT_EN, 0x08);
|
2007-11-04 13:22:17 +00:00
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_EN, 0x08);
|
|
|
|
GPIO_SET_BITWISE(GPIOL_ENABLE, 0x08);
|
2007-11-04 13:22:17 +00:00
|
|
|
#endif
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2007-07-25 06:15:07 +00:00
|
|
|
sd_select_device(0);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-07-25 06:15:07 +00:00
|
|
|
if (currcard->initialized < 0)
|
|
|
|
ret = currcard->initialized;
|
2007-07-12 06:50:42 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
queue_init(&sd_queue, true);
|
2007-10-16 01:25:17 +00:00
|
|
|
create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
|
2008-02-11 07:42:11 +00:00
|
|
|
sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE)
|
|
|
|
IF_COP(, CPU));
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
/* enable interupt for the mSD card */
|
|
|
|
sleep(HZ/10);
|
2007-10-08 20:39:46 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-11-04 13:22:17 +00:00
|
|
|
#ifdef SANSA_E200
|
2007-06-30 02:08:27 +00:00
|
|
|
CPU_INT_EN = HI_MASK;
|
|
|
|
CPU_HI_INT_EN = GPIO0_MASK;
|
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIOA_INT_LEV = (0x80 << 8) | (~GPIOA_INPUT_VAL & 0x80);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
GPIOA_INT_CLR = 0x80;
|
2007-11-04 13:22:17 +00:00
|
|
|
#elif defined SANSA_C200
|
|
|
|
CPU_INT_EN = HI_MASK;
|
|
|
|
CPU_HI_INT_EN = GPIO2_MASK;
|
|
|
|
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIOL_INT_LEV = (0x08 << 8) | (~GPIOL_INPUT_VAL & 0x08);
|
2007-11-04 13:22:17 +00:00
|
|
|
|
|
|
|
GPIOL_INT_CLR = 0x08;
|
|
|
|
#endif
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif
|
2006-11-25 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2008-01-18 13:12:33 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
2008-01-18 12:32:03 +00:00
|
|
|
|
2007-07-12 06:50:42 +00:00
|
|
|
return ret;
|
2006-08-01 22:28:14 +00:00
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
|
|
|
/* 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[3-i];
|
|
|
|
for(i=0; i<4; i++) card.cid[i] = card_info[card_no].cid[3-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool card_detect_target(void)
|
|
|
|
{
|
2008-05-13 02:50:31 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-11-04 13:22:17 +00:00
|
|
|
#ifdef SANSA_E200
|
|
|
|
return (GPIOA_INPUT_VAL & 0x80) == 0; /* low active */
|
|
|
|
#elif defined SANSA_C200
|
|
|
|
return (GPIOL_INPUT_VAL & 0x08) != 0; /* high active */
|
|
|
|
#endif
|
2008-05-13 02:50:31 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2007-06-30 02:08:27 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 02:50:31 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2009-01-21 02:44:20 +00:00
|
|
|
static int sd1_oneshot_callback(struct timeout *tmo)
|
2007-07-29 04:49:19 +00:00
|
|
|
{
|
2008-03-09 14:49:10 +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;
|
2007-07-29 04:49:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
/* called on insertion/removal interrupt */
|
|
|
|
void microsd_int(void)
|
|
|
|
{
|
2007-07-29 04:49:19 +00:00
|
|
|
static struct timeout sd1_oneshot;
|
|
|
|
|
2007-11-04 13:22:17 +00:00
|
|
|
#ifdef SANSA_E200
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x80);
|
|
|
|
GPIOA_INT_LEV = (0x80 << 8) | (~GPIOA_INPUT_VAL & 0x80);
|
2007-07-25 06:15:07 +00:00
|
|
|
GPIOA_INT_CLR = 0x80;
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_SET_BITWISE(GPIOA_INT_EN, 0x80);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
2007-11-04 13:22:17 +00:00
|
|
|
#elif defined SANSA_C200
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOL_INT_EN, 0x08);
|
|
|
|
GPIOL_INT_LEV = (0x08 << 8) | (~GPIOL_INPUT_VAL & 0x08);
|
2007-11-04 13:22:17 +00:00
|
|
|
GPIOL_INT_CLR = 0x08;
|
2008-03-09 14:49:10 +00:00
|
|
|
GPIO_SET_BITWISE(GPIOL_INT_EN, 0x08);
|
2007-11-04 13:22:17 +00:00
|
|
|
#endif
|
2008-03-09 14:49:10 +00:00
|
|
|
timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
|
2007-06-30 02:08:27 +00:00
|
|
|
}
|
2007-10-08 20:39:46 +00:00
|
|
|
#endif /* HAVE_HOTSWAP */
|
2008-11-01 16:14:28 +00:00
|
|
|
|
|
|
|
long sd_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:33:21 +00:00
|
|
|
#ifdef STORAGE_GET_INFO
|
2008-11-01 16:14:28 +00:00
|
|
|
void sd_get_info(IF_MV2(int drive,) struct storage_info *info)
|
|
|
|
{
|
|
|
|
#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";
|
|
|
|
if(drive==0)
|
|
|
|
{
|
|
|
|
info->product="Internal Storage";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->product="SD 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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-02 01:14:46 +00:00
|
|
|
void sd_sleep(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_spin(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_spindown(int seconds)
|
|
|
|
{
|
|
|
|
(void)seconds;
|
|
|
|
}
|