2011-05-30 21:10:37 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Daniel Ankers
|
|
|
|
* Copyright © 2008-2009 Rafaël Carré
|
|
|
|
* Copyright (C) 2011 Marcin Bukat
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h" /* for HAVE_MULTIVOLUME */
|
|
|
|
#include "thread.h"
|
|
|
|
#include "gcc_extensions.h"
|
|
|
|
#include "led.h"
|
|
|
|
#include "sdmmc.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "panic.h"
|
|
|
|
#include "stdbool.h"
|
2017-03-15 05:51:54 +00:00
|
|
|
#include "storage.h"
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
#include "lcd.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "sysfont.h"
|
|
|
|
|
|
|
|
#define RES_NO (-1)
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
/* debug stuff */
|
|
|
|
unsigned long sd_debug_time_rd = 0;
|
|
|
|
unsigned long sd_debug_time_wr = 0;
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
static tCardInfo card_info;
|
|
|
|
|
|
|
|
/* for compatibility */
|
|
|
|
static long last_disk_activity = -1;
|
|
|
|
|
2014-11-29 14:06:35 +00:00
|
|
|
static unsigned char aligned_buf[512] STORAGE_ALIGN_ATTR;
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
static struct mutex sd_mtx SHAREDBSS_ATTR;
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
bool sd_enabled = false;
|
|
|
|
#endif
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
static int sd_first_drive = 0;
|
|
|
|
#else
|
|
|
|
#define sd_first_drive 0
|
|
|
|
#endif
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
static struct semaphore transfer_completion_signal;
|
|
|
|
static struct semaphore command_completion_signal;
|
|
|
|
static volatile bool retry;
|
|
|
|
static volatile int cmd_error;
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
static void enable_controller(bool on)
|
|
|
|
{
|
|
|
|
/* enable or disable clock signal for SD module */
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
SCU_CLKCFG &= ~CLKCFG_SD;
|
|
|
|
led(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SCU_CLKCFG |= CLKCFG_SD;
|
|
|
|
led(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
/* interrupt handler for SD */
|
|
|
|
void INT_SD(void)
|
|
|
|
{
|
|
|
|
const int status = SD_INT;
|
|
|
|
|
|
|
|
SD_INT = 0; /* disable sd interrupts, clear pending interrupts */
|
|
|
|
|
|
|
|
/* cmd and response status pending */
|
|
|
|
if(status & CMD_RES_STAT)
|
|
|
|
{
|
|
|
|
/* get the status */
|
|
|
|
cmd_error = SD_CMDRES;
|
|
|
|
semaphore_release(&command_completion_signal);
|
2018-06-29 20:09:28 +00:00
|
|
|
}
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
/* data transfer status pending */
|
|
|
|
if(status & DATA_XFER_STAT)
|
|
|
|
{
|
|
|
|
cmd_error = SD_DATAT;
|
|
|
|
if (cmd_error & DATA_XFER_ERR)
|
|
|
|
retry = true;
|
|
|
|
|
|
|
|
semaphore_release(&transfer_completion_signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exchange buffers - the one where SD module puts into/reads from
|
|
|
|
* data and the one controlled by MCU. This allows some overlap
|
|
|
|
* in transfer operations and should increase throuput.
|
|
|
|
*/
|
|
|
|
static void mmu_switch_buff(void)
|
|
|
|
{
|
|
|
|
static unsigned int i = 0;
|
|
|
|
|
|
|
|
if (i++ & 0x01)
|
|
|
|
{
|
|
|
|
MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
|
|
|
|
MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MMU_CTRL = MMU_MMU0_BUFI | MMU_CPU_BUFII | MMU_BUFII_RESET |
|
|
|
|
MMU_BUFII_WORD | MMU_BUFI_RESET | MMU_BUFI_BYTE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset internal pointers of the MMU submodule */
|
|
|
|
static void mmu_buff_reset(void)
|
|
|
|
{
|
|
|
|
MMU_CTRL |= MMU_BUFII_RESET | MMU_BUFI_RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool card_detect_target(void)
|
|
|
|
{
|
2018-06-29 20:09:28 +00:00
|
|
|
#if defined(RK27_GENERIC) || defined(IHIFI770) || defined(IHIFI770C) || defined(IHIFI800)
|
|
|
|
/* PC7, active low */
|
2011-05-30 21:10:37 +00:00
|
|
|
return !(GPIO_PCDR & 0x80);
|
2011-11-03 11:53:02 +00:00
|
|
|
#elif defined(HM60X) || defined(HM801)
|
2018-06-29 20:09:28 +00:00
|
|
|
/* PF2, active low */
|
2011-11-03 11:53:02 +00:00
|
|
|
return !(GPIO_PFDR & (1<<2));
|
2013-06-02 19:03:26 +00:00
|
|
|
#elif defined(MA9) || defined(MA9C) || defined(MA8) || defined(MA8C)
|
2018-06-29 20:09:28 +00:00
|
|
|
/* PC7, active high */
|
|
|
|
return (GPIO_PCDR & (1<<7));
|
2013-11-16 14:21:00 +00:00
|
|
|
#elif defined(IHIFI760) || defined(IHIFI960)
|
|
|
|
/* TODO: find out pin */
|
|
|
|
return true;
|
2011-11-03 11:53:02 +00:00
|
|
|
#else
|
|
|
|
#error "Unknown target"
|
|
|
|
#endif
|
2011-05-30 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send command to the SD card. Command finish is signaled in ISR */
|
|
|
|
static bool send_cmd(const int cmd, const int arg, const int res,
|
|
|
|
unsigned long *response)
|
|
|
|
{
|
|
|
|
SD_CMD = arg;
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
SD_CMDREST = CMD_XFER_START | RES_XFER_START | res | cmd;
|
|
|
|
else
|
|
|
|
SD_CMDREST = CMD_XFER_START | RES_XFER_END | RES_R1 | cmd;
|
|
|
|
|
|
|
|
semaphore_wait(&command_completion_signal, TIMEOUT_BLOCK);
|
|
|
|
|
|
|
|
/* Handle command responses & errors */
|
|
|
|
if(res != RES_NO)
|
|
|
|
{
|
|
|
|
if(cmd_error & STAT_CMD_RES_ERR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(res == RES_R2)
|
|
|
|
{
|
|
|
|
response[0] = SD_RES3;
|
|
|
|
response[1] = SD_RES2;
|
|
|
|
response[2] = SD_RES1;
|
|
|
|
response[3] = SD_RES0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
response[0] = SD_RES3;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* for some misterious reason the card does not report itself as being in TRAN
|
|
|
|
* but transfers are successful. Rockchip OF does not check the card state
|
2018-06-29 20:09:28 +00:00
|
|
|
* after SELECT. I checked two different cards.
|
2011-05-30 21:10:37 +00:00
|
|
|
*/
|
|
|
|
static void print_card_status(void)
|
|
|
|
{
|
|
|
|
unsigned long response;
|
|
|
|
send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
|
|
|
|
&response);
|
|
|
|
|
|
|
|
printf("card status: 0x%0x, state: 0x%0x", response, (response>>9)&0xf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_wait_for_tran_state(void)
|
|
|
|
{
|
|
|
|
unsigned long response;
|
|
|
|
unsigned int timeout = current_tick + 5*HZ;
|
|
|
|
int cmd_retry = 10;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
while (!send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
|
|
|
|
&response) && cmd_retry > 0)
|
|
|
|
{
|
|
|
|
cmd_retry--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd_retry <= 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((response >> 9) & 0xf) == SD_TRAN)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-29 20:09:28 +00:00
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
if(TIME_AFTER(current_tick, timeout))
|
|
|
|
{
|
|
|
|
return -10 * ((response >> 9) & 0xf);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool sd_wait_card_busy(void)
|
|
|
|
{
|
|
|
|
unsigned int timeout = current_tick + 5*HZ;
|
|
|
|
|
|
|
|
while (!(SD_CARD & SD_CARD_BSY))
|
|
|
|
{
|
|
|
|
if(TIME_AFTER(current_tick, timeout))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_init_card(void)
|
|
|
|
{
|
|
|
|
unsigned long response;
|
|
|
|
long init_timeout;
|
|
|
|
bool sd_v2 = false;
|
|
|
|
|
|
|
|
card_info.rca = 0;
|
|
|
|
|
|
|
|
/* assume 50 MHz APB freq / 125 = 400 kHz */
|
|
|
|
SD_CTRL = (SD_CTRL & ~(0x7FF)) | 0x7D;
|
|
|
|
|
|
|
|
/* 100 - 400kHz clock required for Identification Mode */
|
|
|
|
/* Start of Card Identification Mode ************************************/
|
|
|
|
|
|
|
|
/* CMD0 Go Idle */
|
|
|
|
if(!send_cmd(SD_GO_IDLE_STATE, 0, RES_NO, NULL))
|
|
|
|
return -1;
|
2018-06-29 20:09:28 +00:00
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
/* CMD8 Check for v2 sd card. Must be sent before using ACMD41
|
|
|
|
Non v2 cards will not respond to this command*/
|
|
|
|
if(send_cmd(SD_SEND_IF_COND, 0x1AA, RES_R6, &response))
|
|
|
|
if((response & 0xFFF) == 0x1AA)
|
|
|
|
sd_v2 = true;
|
|
|
|
|
2013-02-01 07:19:22 +00:00
|
|
|
/* Timeout for inintialization is 2 sec.
|
|
|
|
According to SD Specification 2.00 it should be >= 1,
|
|
|
|
but it's not enough in some rare cases. */
|
|
|
|
init_timeout = current_tick + 2*HZ;
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* this timeout is the only valid error for this loop*/
|
|
|
|
if(TIME_AFTER(current_tick, init_timeout))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if(!send_cmd(SD_APP_CMD, card_info.rca, RES_R1, &response))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
sleep(1); /* bus conflict otherwise */
|
|
|
|
|
|
|
|
/* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
|
|
|
|
if(!send_cmd(SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
|
|
|
|
RES_R3, &card_info.ocr))
|
|
|
|
return -4;
|
|
|
|
} while(!(card_info.ocr & (1<<31)) );
|
|
|
|
|
|
|
|
/* CMD2 send CID */
|
|
|
|
if(!send_cmd(SD_ALL_SEND_CID, 0, RES_R2, card_info.cid))
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* CMD3 send RCA */
|
|
|
|
if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, RES_R6, &card_info.rca))
|
|
|
|
return -6;
|
|
|
|
|
|
|
|
/* End of Card Identification Mode ************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* CMD9 send CSD */
|
|
|
|
if(!send_cmd(SD_SEND_CSD, card_info.rca, RES_R2, card_info.csd))
|
|
|
|
return -11;
|
|
|
|
|
|
|
|
sd_parse_csd(&card_info);
|
|
|
|
|
|
|
|
if(!send_cmd(SD_SELECT_CARD, card_info.rca, RES_R1b, &response))
|
|
|
|
return -20;
|
|
|
|
|
|
|
|
if (!sd_wait_card_busy())
|
|
|
|
return -21;
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
/* CMD6 */
|
|
|
|
if(!send_cmd(SD_SWITCH_FUNC, 0x80fffff1, RES_R1, &response))
|
|
|
|
return -8;
|
|
|
|
sleep(HZ/10);
|
|
|
|
|
|
|
|
/* Card back to full speed 25MHz*/
|
|
|
|
SD_CTRL = (SD_CTRL & ~0x7FF);
|
2011-05-30 21:10:37 +00:00
|
|
|
card_info.initialized = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_controller(void)
|
|
|
|
{
|
|
|
|
/* reset SD module */
|
2012-12-17 08:32:40 +00:00
|
|
|
SCU_RSTCFG |= RSTCFG_SD;
|
2011-05-30 21:10:37 +00:00
|
|
|
sleep(1);
|
2012-12-17 08:32:40 +00:00
|
|
|
SCU_RSTCFG &= ~RSTCFG_SD;
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
/* set pins functions as SD signals */
|
|
|
|
SCU_IOMUXA_CON |= IOMUX_SD;
|
|
|
|
|
|
|
|
/* enable and unmask SD interrupts in interrupt controller */
|
2012-12-17 07:44:09 +00:00
|
|
|
SCU_CLKCFG &= ~CLKCFG_SD;
|
2012-12-17 08:51:08 +00:00
|
|
|
INTC_IMR |= IRQ_ARM_SD;
|
|
|
|
INTC_IECR |= IRQ_ARM_SD;
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
SD_CTRL = SD_PWR_CPU | SD_DETECT_MECH | SD_CLOCK_EN | 0x7D;
|
|
|
|
SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
|
|
|
|
SD_CARD = SD_CARD_SELECT | SD_CARD_PWR_EN;
|
|
|
|
|
|
|
|
/* setup mmu buffers */
|
|
|
|
MMU_PNRI = 0x1ff;
|
|
|
|
MMU_PNRII = 0x1ff;
|
|
|
|
MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
|
|
|
|
MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
/* setup A2A DMA CH0 for SD reads */
|
|
|
|
A2A_ISRC0 = (unsigned long)(&MMU_DATA);
|
|
|
|
A2A_ICNT0 = 512;
|
|
|
|
A2A_LCNT0 = 1;
|
|
|
|
|
|
|
|
/* setup A2A DMA CH1 for SD writes */
|
|
|
|
A2A_IDST1 = (unsigned long)(&MMU_DATA);
|
|
|
|
A2A_ICNT1 = 512;
|
|
|
|
A2A_LCNT1 = 1;
|
|
|
|
|
|
|
|
/* src and dst for CH0 and CH1 is AHB0 */
|
|
|
|
A2A_DOMAIN = 0;
|
|
|
|
|
|
|
|
#ifdef RK27XX_SD_DEBUG
|
|
|
|
/* setup Timer1 for profiling purposes */
|
|
|
|
TMR1CON = (1<<8)|(1<<3);
|
|
|
|
#endif
|
2011-05-30 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sd_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
mutex_init(&sd_mtx);
|
2011-05-30 21:10:37 +00:00
|
|
|
semaphore_init(&transfer_completion_signal, 1, 0);
|
|
|
|
semaphore_init(&command_completion_signal, 1, 0);
|
|
|
|
|
|
|
|
init_controller();
|
|
|
|
|
|
|
|
ret = sd_init_card();
|
|
|
|
if(ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
static inline void read_sd_data(unsigned char **dst)
|
|
|
|
{
|
2014-11-29 14:06:35 +00:00
|
|
|
void *buf = *dst;
|
|
|
|
|
|
|
|
if (!IS_ALIGNED(((unsigned long)*dst), CACHEALIGN_SIZE))
|
|
|
|
buf = aligned_buf;
|
|
|
|
|
|
|
|
commit_discard_dcache_range((const void *)buf, 512);
|
2011-09-06 12:38:41 +00:00
|
|
|
|
2014-11-29 14:06:35 +00:00
|
|
|
|
|
|
|
A2A_IDST0 = (unsigned long)buf;
|
2011-09-06 12:38:41 +00:00
|
|
|
A2A_CON0 = (3<<9) | /* burst 16 */
|
|
|
|
(1<<6) | /* fixed src */
|
|
|
|
(1<<3) | /* DMA start */
|
|
|
|
(2<<1) | /* word transfer size */
|
|
|
|
(1<<0); /* software mode */
|
|
|
|
|
|
|
|
/* wait for DMA engine to finish transfer */
|
|
|
|
while (A2A_DMA_STS & 1);
|
|
|
|
|
2014-11-29 14:06:35 +00:00
|
|
|
if (buf == aligned_buf)
|
|
|
|
memcpy(*dst, aligned_buf, 512);
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
*dst += 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_sd_data(unsigned char **src)
|
|
|
|
{
|
2014-11-29 14:06:35 +00:00
|
|
|
void *buf = *src;
|
|
|
|
|
|
|
|
if (!IS_ALIGNED(((unsigned long)*src), CACHEALIGN_SIZE))
|
|
|
|
{
|
|
|
|
buf = aligned_buf;
|
|
|
|
memcpy(aligned_buf, *src, 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
commit_discard_dcache_range((const void *)buf, 512);
|
2011-09-06 12:38:41 +00:00
|
|
|
|
2014-11-29 14:06:35 +00:00
|
|
|
A2A_ISRC1 = (unsigned long)buf;
|
2011-09-06 12:38:41 +00:00
|
|
|
A2A_CON1 = (3<<9) | /* burst 16 */
|
|
|
|
(1<<5) | /* fixed dst */
|
|
|
|
(1<<3) | /* DMA start */
|
|
|
|
(2<<1) | /* word transfer size */
|
|
|
|
(1<<0); /* software mode */
|
|
|
|
|
|
|
|
/* wait for DMA engine to finish transfer */
|
|
|
|
while (A2A_DMA_STS & 2);
|
|
|
|
|
|
|
|
*src += 512;
|
|
|
|
}
|
|
|
|
|
2013-08-17 16:18:22 +00:00
|
|
|
int sd_read_sectors(IF_MD(int drive,) unsigned long start, int count,
|
2011-05-30 21:10:37 +00:00
|
|
|
void* buf)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
(void)drive;
|
|
|
|
#endif
|
|
|
|
unsigned long response;
|
|
|
|
unsigned int retry_cnt = 0;
|
|
|
|
int cnt, ret = 0;
|
|
|
|
unsigned char *dst;
|
|
|
|
|
|
|
|
mutex_lock(&sd_mtx);
|
2017-03-15 05:51:54 +00:00
|
|
|
enable_controller(true);
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
if (count <= 0 || start + count > card_info.numblocks)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(!(card_info.ocr & (1<<30)))
|
|
|
|
start <<= 9; /* not SDHC */
|
|
|
|
|
|
|
|
while (retry_cnt++ < 20)
|
|
|
|
{
|
|
|
|
cnt = count;
|
|
|
|
dst = (unsigned char *)buf;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
retry = false; /* reset retry flag */
|
2011-08-08 20:09:27 +00:00
|
|
|
|
|
|
|
mmu_buff_reset();
|
|
|
|
|
|
|
|
if (cnt == 1)
|
|
|
|
{
|
2011-08-08 20:21:29 +00:00
|
|
|
/* last block to transfer */
|
2011-08-08 20:09:27 +00:00
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_SINGLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* more than one block to transfer */
|
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_MULTI;
|
|
|
|
}
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
/* issue read command to the card */
|
|
|
|
if (!send_cmd(SD_READ_MULTIPLE_BLOCK, start, RES_R1, &response))
|
|
|
|
{
|
2011-08-08 20:09:27 +00:00
|
|
|
ret = -2;
|
2011-05-30 21:10:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cnt > 0)
|
|
|
|
{
|
2011-09-06 12:38:41 +00:00
|
|
|
#ifdef RK27XX_SD_DEBUG
|
|
|
|
/* debug stuff */
|
|
|
|
TMR1LR = 0xffffffff;
|
|
|
|
#endif
|
2011-05-30 21:10:37 +00:00
|
|
|
/* wait for transfer completion */
|
|
|
|
semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
|
|
|
|
|
2012-04-20 09:11:43 +00:00
|
|
|
#ifdef RK27XX_SD_DEBUG
|
2011-09-06 12:38:41 +00:00
|
|
|
/* debug stuff */
|
|
|
|
sd_debug_time_rd = 0xffffffff - TMR1CVR;
|
|
|
|
#endif
|
2011-05-30 21:10:37 +00:00
|
|
|
if (retry)
|
|
|
|
{
|
|
|
|
/* data transfer error */
|
2011-08-08 20:09:27 +00:00
|
|
|
ret = -3;
|
2011-05-30 21:10:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exchange buffers */
|
|
|
|
mmu_switch_buff();
|
|
|
|
|
|
|
|
cnt--;
|
|
|
|
|
2011-08-08 20:09:27 +00:00
|
|
|
if (cnt == 0)
|
|
|
|
{
|
|
|
|
if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
|
|
|
|
ret = -4;
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
read_sd_data(&dst);
|
|
|
|
|
2011-08-08 20:09:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cnt == 1)
|
|
|
|
{
|
2011-08-08 20:21:29 +00:00
|
|
|
/* last block to transfer */
|
2011-08-08 20:09:27 +00:00
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_SINGLE;
|
2011-09-06 12:38:41 +00:00
|
|
|
|
|
|
|
read_sd_data(&dst);
|
|
|
|
|
2011-08-08 20:09:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* more than one block to transfer */
|
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_MULTI;
|
2011-09-06 12:38:41 +00:00
|
|
|
|
|
|
|
read_sd_data(&dst);
|
2011-08-08 20:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
last_disk_activity = current_tick;
|
|
|
|
|
|
|
|
} /* while (cnt > 0) */
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
/* transfer successfull - leave retry loop */
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
2011-08-08 20:09:27 +00:00
|
|
|
|
|
|
|
} /* while (retry_cnt++ < 20) */
|
2011-05-30 21:10:37 +00:00
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
enable_controller(false);
|
2011-05-30 21:10:37 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not tested */
|
2013-08-17 16:18:22 +00:00
|
|
|
int sd_write_sectors(IF_MD(int drive,) unsigned long start, int count,
|
2011-05-30 21:10:37 +00:00
|
|
|
const void* buf)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
(void) drive;
|
|
|
|
#endif
|
|
|
|
#if defined(BOOTLOADER) /* we don't need write support in bootloader */
|
|
|
|
(void) start;
|
|
|
|
(void) count;
|
|
|
|
(void) buf;
|
|
|
|
return -1;
|
|
|
|
#else
|
2011-09-06 12:38:41 +00:00
|
|
|
|
|
|
|
#ifdef RK27XX_SD_DEBUG
|
|
|
|
/* debug stuff */
|
|
|
|
TMR1LR = 0xffffffff;
|
|
|
|
#endif
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
unsigned long response;
|
|
|
|
unsigned int retry_cnt = 0;
|
|
|
|
int cnt, ret = 0;
|
|
|
|
unsigned char *src;
|
2011-08-08 20:09:27 +00:00
|
|
|
/* bool card_selected = false; */
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
mutex_lock(&sd_mtx);
|
2017-03-15 05:51:54 +00:00
|
|
|
enable_controller(true);
|
2011-05-30 21:10:37 +00:00
|
|
|
|
|
|
|
if (count <= 0 || start + count > card_info.numblocks)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(!(card_info.ocr & (1<<30)))
|
|
|
|
start <<= 9; /* not SDHC */
|
|
|
|
|
|
|
|
while (retry_cnt++ < 20)
|
|
|
|
{
|
|
|
|
cnt = count;
|
|
|
|
src = (unsigned char *)buf;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
retry = false; /* reset retry flag */
|
|
|
|
mmu_buff_reset(); /* reset recive buff state */
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
write_sd_data(&src); /* put data into transfer buffer */
|
2011-08-08 20:09:27 +00:00
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK, start, RES_R1, &response))
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cnt > 0)
|
|
|
|
{
|
|
|
|
/* exchange buffers */
|
|
|
|
mmu_switch_buff();
|
|
|
|
|
|
|
|
if (cnt == 1)
|
|
|
|
{
|
2011-08-08 20:21:29 +00:00
|
|
|
/* last block to transfer */
|
2011-05-30 21:10:37 +00:00
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_SINGLE;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* more than one block to transfer */
|
|
|
|
SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
|
|
|
|
DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
|
|
|
|
DATA_XFER_MULTI;
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
/* put more data */
|
|
|
|
write_sd_data(&src);
|
2011-05-30 21:10:37 +00:00
|
|
|
}
|
|
|
|
/* wait for transfer completion */
|
|
|
|
semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
|
|
|
|
|
|
|
|
if (retry)
|
|
|
|
{
|
|
|
|
/* data transfer error */
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt--;
|
|
|
|
} /* while (cnt > 0) */
|
|
|
|
|
|
|
|
if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
|
|
|
|
ret = -4;
|
|
|
|
|
|
|
|
if (!sd_wait_card_busy())
|
|
|
|
ret = -5;
|
|
|
|
|
|
|
|
/* transfer successfull - leave retry loop */
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-15 05:51:54 +00:00
|
|
|
enable_controller(false);
|
2011-05-30 21:10:37 +00:00
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
|
2011-09-06 12:38:41 +00:00
|
|
|
#ifdef RK27XX_SD_DEBUG
|
|
|
|
/* debug stuff */
|
|
|
|
sd_debug_time_wr = 0xffffffff - TMR1CVR;
|
|
|
|
#endif
|
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
return ret;
|
2018-06-29 20:09:28 +00:00
|
|
|
|
2011-05-30 21:10:37 +00:00
|
|
|
#endif /* defined(BOOTLOADER) */
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_enable(bool on)
|
|
|
|
{
|
2017-03-15 05:51:54 +00:00
|
|
|
mutex_lock(&sd_mtx);
|
|
|
|
enable_controller(on);
|
|
|
|
mutex_unlock(&sd_mtx);
|
2011-05-30 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
long sd_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
tCardInfo *card_get_info_target(int card_no)
|
|
|
|
{
|
|
|
|
(void)card_no;
|
|
|
|
return &card_info;
|
|
|
|
}
|
|
|
|
#endif /* BOOTLOADER */
|
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
/* Not complete and disabled in config */
|
|
|
|
bool sd_removable(IF_MD_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
(void)drive;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sd_present(IF_MD_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
(void)drive;
|
|
|
|
return card_detect_target();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd_oneshot_callback(struct timeout *tmo)
|
|
|
|
{
|
|
|
|
/* This is called only if the state was stable for 300ms - check state
|
|
|
|
* and post appropriate event. */
|
2017-03-15 05:51:54 +00:00
|
|
|
queue_broadcast(card_detect_target() ? SYS_HOTSWAP_INSERTED :
|
|
|
|
SYS_HOTSWAP_EXTRACTED,
|
|
|
|
sd_first_drive);
|
2011-05-30 21:10:37 +00:00
|
|
|
return 0;
|
2017-03-15 05:51:54 +00:00
|
|
|
(void)tmo;
|
2011-05-30 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* interrupt handler for SD detect */
|
|
|
|
|
|
|
|
#endif /* HAVE_HOTSWAP */
|
|
|
|
|
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
int sd_num_drives(int first_drive)
|
|
|
|
{
|
|
|
|
/* we have only one SD drive */
|
2017-03-15 05:51:54 +00:00
|
|
|
sd_first_drive = first_drive;
|
2011-05-30 21:10:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_STORAGE_MULTI */
|
2017-03-15 05:51:54 +00:00
|
|
|
|
|
|
|
int sd_event(long id, intptr_t data)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
case SYS_HOTSWAP_INSERTED:
|
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
|
|
|
mutex_lock(&sd_mtx); /* lock-out card activity */
|
|
|
|
|
|
|
|
/* 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.initialized = 0;
|
|
|
|
|
|
|
|
if (id == SYS_HOTSWAP_INSERTED)
|
|
|
|
{
|
|
|
|
enable_controller(true);
|
|
|
|
rc = sd_init_card();
|
|
|
|
enable_controller(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_HOTSWAP */
|
|
|
|
default:
|
|
|
|
rc = storage_event_default_handler(id, data, last_disk_activity,
|
|
|
|
STORAGE_SD);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|