2009-12-31 19:15:20 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Daniel Ankers
|
|
|
|
* Copyright © 2008-2009 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h" /* for HAVE_MULTIVOLUME */
|
|
|
|
#include "fat.h"
|
|
|
|
#include "thread.h"
|
2010-03-22 06:09:14 +00:00
|
|
|
#include "led.h"
|
2009-12-31 19:15:20 +00:00
|
|
|
#include "hotswap.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "as3525v2.h"
|
|
|
|
#include "pl081.h" /* DMA controller */
|
|
|
|
#include "dma-target.h" /* DMA request lines */
|
|
|
|
#include "clock-target.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "stdbool.h"
|
|
|
|
#include "ata_idle_notify.h"
|
|
|
|
#include "sd.h"
|
2010-04-03 05:42:54 +00:00
|
|
|
#include "usb.h"
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-15 02:36:16 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
#include "disk.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-14 22:01:40 +00:00
|
|
|
#ifdef SANSA_FUZEV2
|
|
|
|
#include "backlight-target.h"
|
|
|
|
#endif
|
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
#include "lcd.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "sysfont.h"
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
#define INTERNAL_AS3525 0 /* embedded SD card */
|
|
|
|
#define SD_SLOT_AS3525 1 /* SD slot if present */
|
|
|
|
|
2010-05-14 19:37:10 +00:00
|
|
|
/* Clipv2 Clip+ and Fuzev2 OF all occupy the same size */
|
|
|
|
#define AMS_OF_SIZE 0xf000
|
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
/* command flags */
|
2010-02-21 22:01:23 +00:00
|
|
|
#define MCI_NO_RESP (0<<0)
|
2009-12-31 19:15:20 +00:00
|
|
|
#define MCI_RESP (1<<0)
|
|
|
|
#define MCI_LONG_RESP (1<<1)
|
|
|
|
|
|
|
|
/* controller registers */
|
|
|
|
#define SD_BASE 0xC6070000
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define SD_REG(x) (*(volatile unsigned long *) (SD_BASE+x))
|
|
|
|
|
|
|
|
#define MCI_CTRL SD_REG(0x00)
|
2010-02-21 22:01:09 +00:00
|
|
|
|
2010-02-21 22:01:23 +00:00
|
|
|
/* control bits */
|
2010-02-21 22:01:09 +00:00
|
|
|
#define CTRL_RESET (1<<0)
|
|
|
|
#define FIFO_RESET (1<<1)
|
|
|
|
#define DMA_RESET (1<<2)
|
|
|
|
#define INT_ENABLE (1<<4)
|
|
|
|
#define DMA_ENABLE (1<<5)
|
|
|
|
#define READ_WAIT (1<<6)
|
|
|
|
#define SEND_IRQ_RESP (1<<7)
|
|
|
|
#define ABRT_READ_DATA (1<<8)
|
|
|
|
#define SEND_CCSD (1<<9)
|
|
|
|
#define SEND_AS_CCSD (1<<10)
|
|
|
|
#define EN_OD_PULLUP (1<<24)
|
|
|
|
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_PWREN SD_REG(0x04) /* power enable */
|
2010-02-23 21:31:54 +00:00
|
|
|
|
|
|
|
#define PWR_CRD_0 (1<<0)
|
|
|
|
#define PWR_CRD_1 (1<<1)
|
|
|
|
#define PWR_CRD_2 (1<<2)
|
|
|
|
#define PWR_CRD_3 (1<<3)
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_CLKDIV SD_REG(0x08) /* clock divider */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* CLK_DIV_0 : bits 7:0
|
|
|
|
* CLK_DIV_1 : bits 15:8
|
|
|
|
* CLK_DIV_2 : bits 23:16
|
|
|
|
* CLK_DIV_3 : bits 31:24
|
|
|
|
*/
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_CLKSRC SD_REG(0x0C) /* clock source */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* CLK_SRC_CRD0: bits 1:0
|
2010-02-24 20:24:48 +00:00
|
|
|
* CLK_SRC_CRD1: bits 3:2
|
|
|
|
* CLK_SRC_CRD2: bits 5:4
|
|
|
|
* CLK_SRC_CRD3: bits 7:6
|
2010-02-23 21:31:54 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_CLKENA SD_REG(0x10) /* clock enable */
|
2010-02-23 21:31:54 +00:00
|
|
|
|
|
|
|
#define CCLK_ENA_CRD0 (1<<0)
|
|
|
|
#define CCLK_ENA_CRD1 (1<<1)
|
|
|
|
#define CCLK_ENA_CRD2 (1<<2)
|
|
|
|
#define CCLK_ENA_CRD3 (1<<3)
|
2010-02-24 20:24:48 +00:00
|
|
|
#define CCLK_LP_CRD0 (1<<16) /* LP --> Low Power Mode? */
|
2010-02-23 21:31:54 +00:00
|
|
|
#define CCLK_LP_CRD1 (1<<17)
|
|
|
|
#define CCLK_LP_CRD2 (1<<18)
|
|
|
|
#define CCLK_LP_CRD3 (1<<19)
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_TMOUT SD_REG(0x14) /* timeout */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* response timeout bits 0:7
|
|
|
|
* data timeout bits 8:31
|
|
|
|
*/
|
2010-02-21 22:01:13 +00:00
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_CTYPE SD_REG(0x18) /* card type */
|
2010-02-21 22:01:13 +00:00
|
|
|
/* 1 bit per card, set = wide bus */
|
2010-02-23 21:31:54 +00:00
|
|
|
#define WIDTH4_CRD0 (1<<0)
|
|
|
|
#define WIDTH4_CRD1 (1<<1)
|
|
|
|
#define WIDTH4_CRD2 (1<<2)
|
|
|
|
#define WIDTH4_CRD3 (1<<3)
|
2010-02-21 22:01:13 +00:00
|
|
|
|
2010-02-23 21:31:54 +00:00
|
|
|
#define MCI_BLKSIZ SD_REG(0x1C) /* block size bits 0:15*/
|
|
|
|
#define MCI_BYTCNT SD_REG(0x20) /* byte count bits 0:31*/
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_MASK SD_REG(0x24) /* interrupt mask */
|
|
|
|
|
2010-02-23 21:31:54 +00:00
|
|
|
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_ARGUMENT SD_REG(0x28)
|
|
|
|
#define MCI_COMMAND SD_REG(0x2C)
|
|
|
|
|
2010-02-21 22:01:23 +00:00
|
|
|
/* command bits (bits 5:0 are the command index) */
|
|
|
|
#define CMD_RESP_EXP_BIT (1<<6)
|
|
|
|
#define CMD_RESP_LENGTH_BIT (1<<7)
|
|
|
|
#define CMD_CHECK_CRC_BIT (1<<8)
|
|
|
|
#define CMD_DATA_EXP_BIT (1<<9)
|
|
|
|
#define CMD_RW_BIT (1<<10)
|
|
|
|
#define CMD_TRANSMODE_BIT (1<<11)
|
|
|
|
#define CMD_SENT_AUTO_STOP_BIT (1<<12)
|
|
|
|
#define CMD_WAIT_PRV_DAT_BIT (1<<13)
|
|
|
|
#define CMD_ABRT_CMD_BIT (1<<14)
|
|
|
|
#define CMD_SEND_INIT_BIT (1<<15)
|
2010-03-18 13:25:13 +00:00
|
|
|
#define CMD_CARD_NO(x) ((x)<<16) /* 5 bits wide */
|
2010-02-21 22:01:23 +00:00
|
|
|
#define CMD_SEND_CLK_ONLY (1<<21)
|
|
|
|
#define CMD_READ_CEATA (1<<22)
|
|
|
|
#define CMD_CCS_EXPECTED (1<<23)
|
|
|
|
#define CMD_DONE_BIT (1<<31)
|
|
|
|
|
2010-05-04 17:29:26 +00:00
|
|
|
#define TRANSFER_CMD (cmd == SD_READ_MULTIPLE_BLOCK || \
|
|
|
|
cmd == SD_WRITE_MULTIPLE_BLOCK)
|
2010-02-21 22:01:23 +00:00
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_RESP0 SD_REG(0x30)
|
|
|
|
#define MCI_RESP1 SD_REG(0x34)
|
|
|
|
#define MCI_RESP2 SD_REG(0x38)
|
|
|
|
#define MCI_RESP3 SD_REG(0x3C)
|
|
|
|
|
|
|
|
#define MCI_MASK_STATUS SD_REG(0x40) /* masked interrupt status */
|
|
|
|
#define MCI_RAW_STATUS SD_REG(0x44) /* raw interrupt status, also used as
|
|
|
|
* status clear */
|
2010-02-21 22:01:23 +00:00
|
|
|
|
2010-03-24 02:48:38 +00:00
|
|
|
/* interrupt bits */ /* C D E (Cmd) (Data) (End) */
|
|
|
|
#define MCI_INT_CRDDET (1<<0) /* card detect */
|
|
|
|
#define MCI_INT_RE (1<<1) /* x response error */
|
|
|
|
#define MCI_INT_CD (1<<2) /* x command done */
|
|
|
|
#define MCI_INT_DTO (1<<3) /* x data transfer over */
|
|
|
|
#define MCI_INT_TXDR (1<<4) /* tx fifo data request */
|
|
|
|
#define MCI_INT_RXDR (1<<5) /* rx fifo data request */
|
|
|
|
#define MCI_INT_RCRC (1<<6) /* x response crc error */
|
|
|
|
#define MCI_INT_DCRC (1<<7) /* x data crc error */
|
|
|
|
#define MCI_INT_RTO (1<<8) /* x response timeout */
|
|
|
|
#define MCI_INT_DRTO (1<<9) /* x data read timeout */
|
|
|
|
#define MCI_INT_HTO (1<<10) /* x data starv timeout */
|
|
|
|
#define MCI_INT_FRUN (1<<11) /* x fifo over/underrun */
|
|
|
|
#define MCI_INT_HLE (1<<12) /* x x hw locked while error */
|
|
|
|
#define MCI_INT_SBE (1<<13) /* x start bit error */
|
|
|
|
#define MCI_INT_ACD (1<<14) /* auto command done */
|
|
|
|
#define MCI_INT_EBE (1<<15) /* x end bit error */
|
2010-02-21 22:01:18 +00:00
|
|
|
#define MCI_INT_SDIO (0xf<<16)
|
|
|
|
|
2010-02-23 21:31:54 +00:00
|
|
|
/*
|
|
|
|
* STATUS register
|
|
|
|
* & 0xBA80 = MCI_INT_DCRC | MCI_INT_DRTO | MCI_INT_FRUN | \
|
|
|
|
* MCI_INT_HLE | MCI_INT_SBE | MCI_INT_EBE
|
|
|
|
* & 8 = MCI_INT_DTO
|
|
|
|
* & 0x428 = MCI_INT_DTO | MCI_INT_RXDR | MCI_INT_HTO
|
|
|
|
* & 0x418 = MCI_INT_DTO | MCI_INT_TXDR | MCI_INT_HTO
|
|
|
|
*/
|
|
|
|
|
2010-03-22 02:30:00 +00:00
|
|
|
#define MCI_CMD_ERROR \
|
|
|
|
(MCI_INT_RE | \
|
|
|
|
MCI_INT_RCRC | \
|
|
|
|
MCI_INT_RTO | \
|
|
|
|
MCI_INT_HLE)
|
|
|
|
|
|
|
|
#define MCI_DATA_ERROR \
|
|
|
|
( MCI_INT_DCRC | \
|
|
|
|
MCI_INT_DRTO | \
|
|
|
|
MCI_INT_HTO | \
|
|
|
|
MCI_INT_FRUN | \
|
|
|
|
MCI_INT_HLE | \
|
|
|
|
MCI_INT_SBE | \
|
|
|
|
MCI_INT_EBE)
|
2010-02-21 22:01:18 +00:00
|
|
|
|
2010-02-23 21:31:54 +00:00
|
|
|
#define MCI_STATUS SD_REG(0x48)
|
|
|
|
|
|
|
|
#define FIFO_RX_WM (1<<0)
|
|
|
|
#define FIFO_TX_WM (1<<1)
|
|
|
|
#define FIFO_EMPTY (1<<2)
|
|
|
|
#define FIFO_FULL (1<<3)
|
|
|
|
#define CMD_FSM_STATE_B0 (1<<4)
|
|
|
|
#define CMD_FSM_STATE_B1 (1<<5)
|
|
|
|
#define CMD_FSM_STATE_B2 (1<<6)
|
|
|
|
#define CMD_FSM_STATE_B3 (1<<7)
|
|
|
|
#define DATA_3_STAT (1<<8)
|
|
|
|
#define DATA_BUSY (1<<9)
|
|
|
|
#define DATA_STAT_MC_BUSY (1<<10)
|
|
|
|
#define RESP_IDX_B0 (1<<11)
|
|
|
|
#define RESP_IDX_B1 (1<<12)
|
|
|
|
#define RESP_IDX_B2 (1<<13)
|
|
|
|
#define RESP_IDX_B3 (1<<14)
|
|
|
|
#define RESP_IDX_B4 (1<<15)
|
|
|
|
#define RESP_IDX_B5 (1<<16)
|
|
|
|
#define FIFO_CNT_B00 (1<<17)
|
|
|
|
#define FIFO_CNT_B01 (1<<18)
|
|
|
|
#define FIFO_CNT_B02 (1<<19)
|
|
|
|
#define FIFO_CNT_B03 (1<<20)
|
|
|
|
#define FIFO_CNT_B04 (1<<21)
|
|
|
|
#define FIFO_CNT_B05 (1<<22)
|
|
|
|
#define FIFO_CNT_B06 (1<<23)
|
|
|
|
#define FIFO_CNT_B07 (1<<24)
|
|
|
|
#define FIFO_CNT_B08 (1<<25)
|
|
|
|
#define FIFO_CNT_B09 (1<<26)
|
|
|
|
#define FIFO_CNT_B10 (1<<27)
|
|
|
|
#define FIFO_CNT_B11 (1<<28)
|
|
|
|
#define FIFO_CNT_B12 (1<<29)
|
|
|
|
#define DMA_ACK (1<<30)
|
|
|
|
#define START_CMD (1<<31)
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_FIFOTH SD_REG(0x4C) /* FIFO threshold */
|
2010-02-21 22:01:29 +00:00
|
|
|
/* TX watermark : bits 11:0
|
|
|
|
* RX watermark : bits 27:16
|
|
|
|
* DMA MTRANS SIZE : bits 30:28
|
|
|
|
* bits 31, 15:12 : unused
|
|
|
|
*/
|
|
|
|
#define MCI_FIFOTH_MASK 0x8000f000
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_CDETECT SD_REG(0x50) /* card detect */
|
2010-02-23 21:31:54 +00:00
|
|
|
|
|
|
|
#define CDETECT_CRD_0 (1<<0)
|
|
|
|
#define CDETECT_CRD_1 (1<<1)
|
|
|
|
#define CDETECT_CRD_2 (1<<2)
|
|
|
|
#define CDETECT_CRD_3 (1<<3)
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_WRTPRT SD_REG(0x54) /* write protect */
|
|
|
|
#define MCI_GPIO SD_REG(0x58)
|
2010-02-23 21:31:54 +00:00
|
|
|
#define MCI_TCBCNT SD_REG(0x5C) /* transferred CIU byte count (card)*/
|
|
|
|
#define MCI_TBBCNT SD_REG(0x60) /* transferred host/DMA to/from bytes (FIFO)*/
|
|
|
|
#define MCI_DEBNCE SD_REG(0x64) /* card detect debounce bits 23:0*/
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_USRID SD_REG(0x68) /* user id */
|
|
|
|
#define MCI_VERID SD_REG(0x6C) /* version id */
|
2010-02-21 22:01:33 +00:00
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_HCON SD_REG(0x70) /* hardware config */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* bit 0 : card type
|
|
|
|
* bits 5:1 : maximum card index
|
|
|
|
* bit 6 : BUS TYPE
|
|
|
|
* bits 9:7 : DATA WIDTH
|
|
|
|
* bits 15:10 : ADDR WIDTH
|
|
|
|
* bits 17:16 : DMA IF
|
|
|
|
* bits 20:18 : DMA WIDTH
|
|
|
|
* bit 21 : FIFO RAM INSIDE
|
|
|
|
* bit 22 : IMPL HOLD REG
|
|
|
|
* bit 23 : SET CLK FALSE
|
|
|
|
* bits 25:24 : MAX CLK DIV IDX
|
|
|
|
* bit 26 : AREA OPTIM
|
|
|
|
*/
|
2010-02-21 22:01:05 +00:00
|
|
|
|
|
|
|
#define MCI_BMOD SD_REG(0x80) /* bus mode */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* bit 0 : SWR
|
|
|
|
* bit 1 : FB
|
|
|
|
* bits 6:2 : DSL
|
|
|
|
* bit 7 : DE
|
|
|
|
* bit 10:8 : PBL
|
|
|
|
*/
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_PLDMND SD_REG(0x84) /* poll demand */
|
|
|
|
#define MCI_DBADDR SD_REG(0x88) /* descriptor base address */
|
|
|
|
#define MCI_IDSTS SD_REG(0x8C) /* internal DMAC status */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* bit 0 : TI
|
|
|
|
* bit 1 : RI
|
|
|
|
* bit 2 : FBE
|
|
|
|
* bit 3 : unused
|
|
|
|
* bit 4 : DU
|
|
|
|
* bit 5 : CES
|
|
|
|
* bits 7:6 : unused
|
|
|
|
* bits 8 : NIS
|
|
|
|
* bit 9 : AIS
|
|
|
|
* bits 12:10 : EB
|
|
|
|
* bits 16:13 : FSM
|
|
|
|
*/
|
|
|
|
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_IDINTEN SD_REG(0x90) /* internal DMAC interrupt enable */
|
2010-02-23 21:31:54 +00:00
|
|
|
/* bit 0 : TI
|
|
|
|
* bit 1 : RI
|
|
|
|
* bit 2 : FBE
|
|
|
|
* bit 3 : unused
|
|
|
|
* bit 4 : DU
|
|
|
|
* bit 5 : CES
|
|
|
|
* bits 7:6 : unused
|
|
|
|
* bits 8 : NI
|
|
|
|
* bit 9 : AI
|
|
|
|
*/
|
2010-02-21 22:01:05 +00:00
|
|
|
#define MCI_DSCADDR SD_REG(0x94) /* current host descriptor address */
|
|
|
|
#define MCI_BUFADDR SD_REG(0x98) /* current host buffer address */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
#define MCI_FIFO ((unsigned long *) (SD_BASE+0x100))
|
|
|
|
|
2010-02-22 10:02:20 +00:00
|
|
|
#define UNALIGNED_NUM_SECTORS 10
|
|
|
|
static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS* SD_BLOCK_SIZE] __attribute__((aligned(32))); /* align on cache line size */
|
2010-05-19 18:13:06 +00:00
|
|
|
static unsigned char *uncached_buffer = AS3525_UNCACHED_ADDR(&aligned_buffer[0]);
|
2010-02-22 10:02:20 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
static void init_controller(void);
|
2010-05-24 15:07:15 +00:00
|
|
|
static int sd_wait_for_tran_state(const int drive);
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
static tCardInfo card_info[NUM_DRIVES];
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
/* for compatibility */
|
|
|
|
static long last_disk_activity = -1;
|
|
|
|
|
|
|
|
#define MIN_YIELD_PERIOD 5 /* ticks */
|
|
|
|
static long next_yield = 0;
|
|
|
|
|
|
|
|
static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
|
|
|
|
static const char sd_thread_name[] = "ata/sd";
|
|
|
|
static struct mutex sd_mtx SHAREDBSS_ATTR;
|
|
|
|
static struct event_queue sd_queue;
|
|
|
|
#ifndef BOOTLOADER
|
2010-02-22 02:42:58 +00:00
|
|
|
bool sd_enabled = false;
|
2009-12-31 19:15:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct wakeup transfer_completion_signal;
|
2010-05-03 22:15:56 +00:00
|
|
|
static struct wakeup command_completion_signal;
|
2009-12-31 19:15:20 +00:00
|
|
|
static volatile bool retry;
|
2010-05-03 22:15:56 +00:00
|
|
|
static volatile int cmd_error;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
2010-04-27 10:11:52 +00:00
|
|
|
#define EXT_SD_BITS (1<<2)
|
2010-03-15 01:57:15 +00:00
|
|
|
#endif
|
|
|
|
|
2010-04-27 09:34:29 +00:00
|
|
|
static inline void mci_delay(void) { udelay(1000); }
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
void INT_NAND(void)
|
|
|
|
{
|
2010-02-21 22:49:53 +00:00
|
|
|
MCI_CTRL &= ~INT_ENABLE;
|
2010-05-03 22:15:56 +00:00
|
|
|
/* use raw status here as we need to check some Ints that are masked */
|
|
|
|
const int status = MCI_RAW_STATUS;
|
2010-02-21 22:49:53 +00:00
|
|
|
|
|
|
|
MCI_RAW_STATUS = status; /* clear status */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-22 02:30:00 +00:00
|
|
|
if(status & MCI_DATA_ERROR)
|
2009-12-31 19:15:20 +00:00
|
|
|
retry = true;
|
2010-02-22 07:31:45 +00:00
|
|
|
|
2010-03-22 06:09:08 +00:00
|
|
|
if( status & (MCI_INT_DTO|MCI_DATA_ERROR))
|
2010-02-21 22:49:53 +00:00
|
|
|
wakeup_signal(&transfer_completion_signal);
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-03 22:15:56 +00:00
|
|
|
cmd_error = status & MCI_CMD_ERROR;
|
|
|
|
|
|
|
|
if(status & MCI_INT_CD)
|
|
|
|
wakeup_signal(&command_completion_signal);
|
|
|
|
|
2010-02-21 22:01:09 +00:00
|
|
|
MCI_CTRL |= INT_ENABLE;
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 13:25:13 +00:00
|
|
|
static inline bool card_detect_target(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
2010-03-27 16:53:08 +00:00
|
|
|
#if defined(SANSA_FUZEV2)
|
|
|
|
return GPIOA_PIN(2);
|
|
|
|
#elif defined(SANSA_CLIPPLUS)
|
2010-03-18 13:25:13 +00:00
|
|
|
return !(GPIOA_PIN(2));
|
2010-03-27 16:53:08 +00:00
|
|
|
#else
|
|
|
|
#error "microSD pin not defined for your target"
|
|
|
|
#endif
|
2010-03-18 13:25:13 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool send_cmd(const int drive, const int cmd, const int arg, const int flags,
|
2009-12-31 19:15:20 +00:00
|
|
|
unsigned long *response)
|
|
|
|
{
|
2010-03-18 13:25:13 +00:00
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
|
|
|
if(sd_present(SD_SLOT_AS3525))
|
2010-05-14 22:01:40 +00:00
|
|
|
GPIOB_PIN(5) = (1-drive) << 5;
|
2010-03-18 13:25:13 +00:00
|
|
|
#endif
|
2010-02-22 07:31:45 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
MCI_ARGUMENT = arg;
|
2010-03-26 17:00:25 +00:00
|
|
|
|
|
|
|
/* Construct MCI_COMMAND */
|
|
|
|
MCI_COMMAND =
|
|
|
|
/*b5:0*/ cmd
|
|
|
|
/*b6 */ | ((flags & MCI_RESP) ? CMD_RESP_EXP_BIT: 0)
|
|
|
|
/*b7 */ | ((flags & MCI_LONG_RESP) ? CMD_RESP_LENGTH_BIT: 0)
|
|
|
|
/*b8 | CMD_CHECK_CRC_BIT unused */
|
|
|
|
/*b9 */ | (TRANSFER_CMD ? CMD_DATA_EXP_BIT: 0)
|
|
|
|
/*b10 */ | ((cmd == SD_WRITE_MULTIPLE_BLOCK) ? CMD_RW_BIT: 0)
|
|
|
|
/*b11 | CMD_TRANSMODE_BIT unused */
|
|
|
|
/*b12 | CMD_SENT_AUTO_STOP_BIT unused */
|
|
|
|
/*b13 */ | (TRANSFER_CMD ? CMD_WAIT_PRV_DAT_BIT: 0)
|
|
|
|
/*b14 | CMD_ABRT_CMD_BIT unused */
|
|
|
|
/*b15 | CMD_SEND_INIT_BIT unused */
|
|
|
|
/*b20:16 */ | CMD_CARD_NO(drive)
|
|
|
|
/*b21 | CMD_SEND_CLK_ONLY unused */
|
|
|
|
/*b22 | CMD_READ_CEATA unused */
|
|
|
|
/*b23 | CMD_CCS_EXPECTED unused */
|
|
|
|
/*b31 */ | CMD_DONE_BIT;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-14 22:01:40 +00:00
|
|
|
#ifdef SANSA_FUZEV2
|
|
|
|
extern int buttonlight_is_on;
|
|
|
|
if(buttonlight_is_on)
|
|
|
|
_buttonlight_on();
|
|
|
|
else
|
|
|
|
_buttonlight_off();
|
|
|
|
#endif
|
2010-05-03 22:15:56 +00:00
|
|
|
wakeup_wait(&command_completion_signal, TIMEOUT_BLOCK);
|
|
|
|
|
2010-05-04 17:29:26 +00:00
|
|
|
/* Handle command responses & errors */
|
2009-12-31 19:15:20 +00:00
|
|
|
if(flags & MCI_RESP)
|
|
|
|
{
|
2010-05-04 17:29:26 +00:00
|
|
|
if(cmd_error & (MCI_INT_RCRC | MCI_INT_RTO))
|
2010-05-03 22:15:56 +00:00
|
|
|
return false;
|
2010-03-14 21:38:30 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
if(flags & MCI_LONG_RESP)
|
|
|
|
{
|
2010-03-22 12:12:09 +00:00
|
|
|
response[0] = MCI_RESP3;
|
|
|
|
response[1] = MCI_RESP2;
|
|
|
|
response[2] = MCI_RESP1;
|
|
|
|
response[3] = MCI_RESP0;
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
2010-03-22 12:12:09 +00:00
|
|
|
else
|
|
|
|
response[0] = MCI_RESP0;
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
static int sd_init_card(const int drive)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
|
|
|
unsigned long response;
|
2010-02-25 06:46:16 +00:00
|
|
|
long init_timeout;
|
|
|
|
bool sd_v2 = false;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-24 20:24:48 +00:00
|
|
|
/* assume 24 MHz clock / 60 = 400 kHz */
|
|
|
|
MCI_CLKDIV = (MCI_CLKDIV & ~(0xFF)) | 0x3C; /* CLK_DIV_0 : bits 7:0 */
|
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* 100 - 400kHz clock required for Identification Mode */
|
|
|
|
/* Start of Card Identification Mode ************************************/
|
|
|
|
|
|
|
|
/* CMD0 Go Idle */
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
|
2009-12-31 19:15:20 +00:00
|
|
|
return -1;
|
|
|
|
mci_delay();
|
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* CMD8 Check for v2 sd card. Must be sent before using ACMD41
|
|
|
|
Non v2 cards will not respond to this command*/
|
2010-03-18 13:25:13 +00:00
|
|
|
if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP, &response))
|
2009-12-31 19:15:20 +00:00
|
|
|
if((response & 0xFFF) == 0x1AA)
|
2010-02-22 07:31:45 +00:00
|
|
|
sd_v2 = true;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* timeout for initialization is 1sec, from SD Specification 2.00 */
|
|
|
|
init_timeout = current_tick + HZ;
|
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
do {
|
2010-02-25 06:46:16 +00:00
|
|
|
/* this timeout is the only valid error for this loop*/
|
|
|
|
if(TIME_AFTER(current_tick, init_timeout))
|
|
|
|
return -2;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
/* app_cmd */
|
2010-03-18 13:25:13 +00:00
|
|
|
send_cmd(drive, SD_APP_CMD, 0, MCI_RESP, &response);
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
|
2010-03-21 06:49:42 +00:00
|
|
|
if(!send_cmd(drive, SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
|
2010-03-15 01:57:15 +00:00
|
|
|
MCI_RESP, &card_info[drive].ocr))
|
2009-12-31 19:15:20 +00:00
|
|
|
return -3;
|
2010-03-15 01:57:15 +00:00
|
|
|
} while(!(card_info[drive].ocr & (1<<31)) );
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* CMD2 send CID */
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info[drive].cid))
|
|
|
|
return -4;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* CMD3 send RCA */
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
|
|
|
|
return -5;
|
2010-02-25 06:46:16 +00:00
|
|
|
|
2010-03-18 13:25:13 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
/* Make sure we have 2 unique rca numbers */
|
|
|
|
if(card_info[INTERNAL_AS3525].rca == card_info[SD_SLOT_AS3525].rca)
|
|
|
|
if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info[drive].rca))
|
|
|
|
return -6;
|
|
|
|
#endif
|
2010-02-25 06:46:16 +00:00
|
|
|
/* End of Card Identification Mode ************************************/
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-24 05:41:33 +00:00
|
|
|
/* Attempt to switch cards to HS timings, non HS cards just ignore this */
|
|
|
|
/* CMD7 w/rca: Select card to put it in TRAN state */
|
2010-05-04 17:29:26 +00:00
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
|
2010-03-24 05:41:33 +00:00
|
|
|
return -7;
|
|
|
|
|
2010-05-24 15:07:15 +00:00
|
|
|
if(sd_wait_for_tran_state(drive))
|
2010-03-24 05:41:33 +00:00
|
|
|
return -8;
|
|
|
|
|
|
|
|
/* CMD6 */
|
|
|
|
if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
|
|
|
|
return -9;
|
2010-05-10 17:34:54 +00:00
|
|
|
mci_delay();
|
2010-03-24 05:41:33 +00:00
|
|
|
|
|
|
|
/* We need to go back to STBY state now so we can read csd */
|
|
|
|
/* CMD7 w/rca=0: Deselect card to put it in STBY state */
|
2010-05-04 17:29:26 +00:00
|
|
|
if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
|
2010-03-24 05:41:33 +00:00
|
|
|
return -10;
|
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* CMD9 send CSD */
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca,
|
2010-03-15 01:57:15 +00:00
|
|
|
MCI_RESP|MCI_LONG_RESP, card_info[drive].csd))
|
2010-03-24 05:41:33 +00:00
|
|
|
return -11;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
sd_parse_csd(&card_info[drive]);
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-14 19:37:10 +00:00
|
|
|
if(drive == INTERNAL_AS3525) /* The OF is stored in the first blocks */
|
|
|
|
card_info[INTERNAL_AS3525].numblocks -= AMS_OF_SIZE;
|
|
|
|
|
2010-05-10 17:34:54 +00:00
|
|
|
/* Card back to full speed */
|
|
|
|
MCI_CLKDIV &= ~(0xFF); /* CLK_DIV_0 : bits 7:0 = 0x00 */
|
|
|
|
|
2010-02-25 06:46:16 +00:00
|
|
|
/* CMD7 w/rca: Select card to put it in TRAN state */
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
|
2010-03-24 05:41:33 +00:00
|
|
|
return -12;
|
2010-05-05 04:35:08 +00:00
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
/* Switch to to 4 bit widebus mode */
|
2010-05-24 15:07:15 +00:00
|
|
|
if(sd_wait_for_tran_state(drive) < 0)
|
2010-05-05 04:35:08 +00:00
|
|
|
return -13;
|
2010-05-06 18:53:41 +00:00
|
|
|
/* CMD55 */ /* Response is requested due to timing issue */
|
|
|
|
if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
|
2010-05-05 04:35:08 +00:00
|
|
|
return -14;
|
|
|
|
/* ACMD6 */
|
|
|
|
if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_NO_RESP, NULL))
|
|
|
|
return -15;
|
|
|
|
mci_delay();
|
2010-05-06 18:53:41 +00:00
|
|
|
/* CMD55 */ /* Response is requested due to timing issue */
|
|
|
|
if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
|
2010-05-05 04:35:08 +00:00
|
|
|
return -16;
|
|
|
|
/* ACMD42 */
|
|
|
|
if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_NO_RESP, NULL))
|
|
|
|
return -17;
|
|
|
|
/* Now that card is widebus make controller aware */
|
|
|
|
MCI_CTYPE |= (1<<drive);
|
2010-03-18 13:25:13 +00:00
|
|
|
#endif
|
2010-02-24 20:24:48 +00:00
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
card_info[drive].initialized = 1;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-10 17:35:00 +00:00
|
|
|
MCI_CLKENA |= 1<<(drive + 16); /* Set low power mode */
|
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2010-03-15 02:36:16 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
case SYS_HOTSWAP_INSERTED:
|
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
|
|
|
{
|
|
|
|
int microsd_init = 1;
|
|
|
|
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(SD_SLOT_AS3525); /* 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[SD_SLOT_AS3525].initialized = 0;
|
|
|
|
|
|
|
|
if (ev.id == SYS_HOTSWAP_INSERTED)
|
|
|
|
{
|
|
|
|
sd_enable(true);
|
|
|
|
microsd_init = sd_init_card(SD_SLOT_AS3525);
|
|
|
|
if (microsd_init < 0) /* initialisation failed */
|
|
|
|
panicf("microSD init failed : %d", microsd_init);
|
|
|
|
|
|
|
|
microsd_init = disk_mount(SD_SLOT_AS3525); /* 0 if fail */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mount succeeded, or this was an EXTRACTED event,
|
|
|
|
* in both cases notify the system about the changed filesystems
|
|
|
|
*/
|
|
|
|
if (microsd_init)
|
|
|
|
queue_broadcast(SYS_FS_CHANGED, 0);
|
|
|
|
/* Access is now safe */
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
fat_unlock();
|
|
|
|
sd_enable(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2009-12-31 19:15:20 +00:00
|
|
|
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;
|
2010-04-03 05:42:54 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
case SYS_USB_CONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_CONNECTED_ACK);
|
|
|
|
/* Wait until the USB cable is extracted again */
|
|
|
|
usb_wait_for_disconnect(&sd_queue);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SYS_USB_DISCONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_controller(void)
|
|
|
|
{
|
2010-03-18 13:25:13 +00:00
|
|
|
int hcon_numcards = ((MCI_HCON>>1) & 0x1F) + 1;
|
|
|
|
int card_mask = (1 << hcon_numcards) - 1;
|
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_PWREN &= ~card_mask; /* power off all cards */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_CLKSRC = 0x00; /* All CLK_SRC_CRD set to 0*/
|
|
|
|
MCI_CLKDIV = 0x00; /* CLK_DIV_0 : bits 7:0 */
|
2010-02-24 20:24:48 +00:00
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_PWREN |= card_mask; /* power up cards */
|
2010-02-24 20:24:48 +00:00
|
|
|
mci_delay();
|
2010-02-22 10:02:20 +00:00
|
|
|
|
2010-02-22 07:31:45 +00:00
|
|
|
MCI_CTRL |= CTRL_RESET;
|
2010-02-21 22:01:09 +00:00
|
|
|
while(MCI_CTRL & CTRL_RESET)
|
2009-12-31 19:15:20 +00:00
|
|
|
;
|
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_RAW_STATUS = 0xffffffff; /* Clear all MCI Interrupts */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-24 20:24:48 +00:00
|
|
|
MCI_TMOUT = 0xffffffff; /* data b31:8, response b7:0 */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-24 20:24:48 +00:00
|
|
|
MCI_CTYPE = 0x0; /* all cards 1 bit bus for now */
|
2010-02-21 22:01:13 +00:00
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_CLKENA = card_mask; /* Enables card clocks */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
MCI_ARGUMENT = 0;
|
2010-02-21 22:01:23 +00:00
|
|
|
MCI_COMMAND = CMD_DONE_BIT|CMD_SEND_CLK_ONLY|CMD_WAIT_PRV_DAT_BIT;
|
2010-02-22 07:31:45 +00:00
|
|
|
while(MCI_COMMAND & CMD_DONE_BIT)
|
|
|
|
;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-04-09 00:26:06 +00:00
|
|
|
MCI_DEBNCE = 0xfffff; /* default value */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-04-07 05:47:12 +00:00
|
|
|
/* Rx watermark = 63(sd reads) Tx watermark = 128 (sd writes) */
|
|
|
|
MCI_FIFOTH = (MCI_FIFOTH & MCI_FIFOTH_MASK) | 0x503f0080;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-19 20:41:47 +00:00
|
|
|
/* RCRC & RTO interrupts should be set together with the CD interrupt but
|
|
|
|
* in practice sometimes incorrectly precede the CD interrupt. If we leave
|
|
|
|
* them masked for now we can check them in the isr by reading raw status when
|
|
|
|
* the CD int is triggered.
|
|
|
|
*/
|
|
|
|
MCI_MASK |= (MCI_DATA_ERROR | MCI_INT_DTO | MCI_INT_CD);
|
2010-02-22 07:31:45 +00:00
|
|
|
|
2010-05-19 20:41:47 +00:00
|
|
|
MCI_CTRL |= INT_ENABLE | DMA_ENABLE;
|
|
|
|
|
|
|
|
MCI_BLKSIZ = SD_BLOCK_SIZE;
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sd_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2010-03-23 17:00:59 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
|
|
|
|
|
2010-03-23 17:00:59 +00:00
|
|
|
CGU_IDE = (1<<7) /* AHB interface enable */
|
|
|
|
| (AS3525_IDE_DIV << 2)
|
|
|
|
| 1; /* clock source = PLLA */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-23 17:00:59 +00:00
|
|
|
CGU_MEMSTICK = (1<<7) /* interface enable */
|
|
|
|
| (AS3525_MS_DIV << 2)
|
|
|
|
| 1; /* clock source = PLLA */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-03-23 17:00:59 +00:00
|
|
|
CGU_SDSLOT = (1<<7) /* interface enable */
|
|
|
|
| (AS3525_SDSLOT_DIV << 2)
|
|
|
|
| 1; /* clock source = PLLA */
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
wakeup_init(&transfer_completion_signal);
|
2010-05-03 22:15:56 +00:00
|
|
|
wakeup_init(&command_completion_signal);
|
2010-03-18 13:25:13 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
/* clear previous irq */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IC = EXT_SD_BITS;
|
2010-03-18 13:25:13 +00:00
|
|
|
/* enable edge detecting */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IS &= ~EXT_SD_BITS;
|
2010-03-18 13:25:13 +00:00
|
|
|
/* detect both raising and falling edges */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IBE |= EXT_SD_BITS;
|
2010-04-06 17:22:51 +00:00
|
|
|
/* Configure XPD for SD-MCI interface */
|
|
|
|
CCU_IO |= (1<<2);
|
2010-03-18 13:25:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
VIC_INT_ENABLE = INTERRUPT_NAND;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
init_controller();
|
2010-03-15 01:57:15 +00:00
|
|
|
ret = sd_init_card(INTERNAL_AS3525);
|
2009-12-31 19:15:20 +00:00
|
|
|
if(ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* init mutex */
|
|
|
|
mutex_init(&sd_mtx);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
sd_enabled = true;
|
|
|
|
sd_enable(false);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-24 15:07:15 +00:00
|
|
|
static int sd_wait_for_tran_state(const int drive)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
|
|
|
unsigned long response;
|
|
|
|
unsigned int timeout = 100; /* ticks */
|
|
|
|
long t = current_tick;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
long tick;
|
|
|
|
|
2010-05-03 22:15:56 +00:00
|
|
|
while(!(send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP, &response)));
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-24 15:07:15 +00:00
|
|
|
if (((response >> 9) & 0xf) == SD_TRAN)
|
2009-12-31 19:15:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(TIME_AFTER(current_tick, t + timeout))
|
|
|
|
return -10 * ((response >> 9) & 0xf);
|
|
|
|
|
|
|
|
if (TIME_AFTER((tick = current_tick), next_yield))
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
timeout += current_tick - tick;
|
|
|
|
next_yield = tick + MIN_YIELD_PERIOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
|
|
|
|
int count, void* buf, bool write)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2010-03-15 01:57:15 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
|
|
|
const int drive = 0;
|
|
|
|
#endif
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
mutex_lock(&sd_mtx);
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
sd_enable(true);
|
2010-03-22 06:09:14 +00:00
|
|
|
led(true);
|
2009-12-31 19:15:20 +00:00
|
|
|
#endif
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
if (card_info[drive].initialized <= 0)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
2010-03-15 01:57:15 +00:00
|
|
|
ret = sd_init_card(drive);
|
|
|
|
if (!(card_info[drive].initialized))
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
|
|
|
panicf("card not initialised (%d)", ret);
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-16 10:24:31 +00:00
|
|
|
if(count < 0) /* XXX: why is it signed ? */
|
2010-05-14 19:37:10 +00:00
|
|
|
{
|
|
|
|
ret = -18;
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
2010-05-16 10:24:31 +00:00
|
|
|
if((start+count) > card_info[drive].numblocks)
|
|
|
|
{
|
|
|
|
ret = -19;
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
2010-05-14 19:37:10 +00:00
|
|
|
|
2010-05-15 12:01:53 +00:00
|
|
|
/* skip SanDisk OF */
|
|
|
|
if (drive == INTERNAL_AS3525)
|
|
|
|
start += AMS_OF_SIZE;
|
|
|
|
|
2010-03-18 13:25:13 +00:00
|
|
|
/* CMD7 w/rca: Select card to put it in TRAN state */
|
|
|
|
if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_NO_RESP, NULL))
|
2010-05-16 10:24:31 +00:00
|
|
|
return -20;
|
2010-03-18 13:25:13 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
last_disk_activity = current_tick;
|
|
|
|
dma_retain();
|
|
|
|
|
2010-02-21 22:01:37 +00:00
|
|
|
const int cmd = write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-02-21 22:01:37 +00:00
|
|
|
do
|
|
|
|
{
|
2010-02-22 10:02:20 +00:00
|
|
|
void *dma_buf = aligned_buffer;
|
|
|
|
unsigned int transfer = count;
|
|
|
|
if(transfer > UNALIGNED_NUM_SECTORS)
|
|
|
|
transfer = UNALIGNED_NUM_SECTORS;
|
|
|
|
|
|
|
|
if(write)
|
|
|
|
memcpy(uncached_buffer, buf, transfer * SD_BLOCK_SIZE);
|
|
|
|
|
2010-03-24 05:41:25 +00:00
|
|
|
/* Interrupt handler might set this to true during transfer */
|
2010-02-22 10:02:20 +00:00
|
|
|
retry = false;
|
|
|
|
|
|
|
|
MCI_BYTCNT = transfer * SD_BLOCK_SIZE;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
2010-05-24 15:07:15 +00:00
|
|
|
ret = sd_wait_for_tran_state(drive);
|
2010-03-22 02:29:53 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
static const char *st[9] = {
|
|
|
|
"IDLE", "RDY", "IDENT", "STBY", "TRAN", "DATA", "RCV",
|
|
|
|
"PRG", "DIS"};
|
|
|
|
if(ret <= -10)
|
|
|
|
panicf("wait for TRAN state failed (%s) %d",
|
|
|
|
st[(-ret / 10) % 9], drive);
|
|
|
|
else
|
|
|
|
panicf("wait for state failed");
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
|
2010-03-18 16:40:21 +00:00
|
|
|
int arg = start;
|
|
|
|
if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
|
|
|
|
arg *= SD_BLOCK_SIZE;
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
if(write)
|
2010-02-22 10:02:20 +00:00
|
|
|
dma_enable_channel(0, dma_buf, MCI_FIFO, DMA_PERI_SD,
|
2009-12-31 19:15:20 +00:00
|
|
|
DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8, NULL);
|
|
|
|
else
|
2010-02-22 10:02:20 +00:00
|
|
|
dma_enable_channel(0, MCI_FIFO, dma_buf, DMA_PERI_SD,
|
2009-12-31 19:15:20 +00:00
|
|
|
DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8, NULL);
|
|
|
|
|
2010-05-03 22:15:56 +00:00
|
|
|
unsigned long dummy; /* if we don't ask for a response, writing fails */
|
|
|
|
if(!send_cmd(drive, cmd, arg, MCI_RESP, &dummy))
|
|
|
|
panicf("%s multiple blocks failed", write ? "write" : "read");
|
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
|
2010-03-22 06:09:08 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
last_disk_activity = current_tick;
|
|
|
|
|
2010-03-18 13:25:13 +00:00
|
|
|
if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_NO_RESP, NULL))
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
|
|
|
ret = -666;
|
|
|
|
panicf("STOP TRANSMISSION failed");
|
|
|
|
goto sd_transfer_error;
|
|
|
|
}
|
|
|
|
|
2010-02-22 10:02:20 +00:00
|
|
|
if(!retry)
|
|
|
|
{
|
|
|
|
if(!write)
|
|
|
|
memcpy(buf, uncached_buffer, transfer * SD_BLOCK_SIZE);
|
|
|
|
buf += transfer * SD_BLOCK_SIZE;
|
|
|
|
start += transfer;
|
|
|
|
count -= transfer;
|
|
|
|
}
|
2010-03-24 05:41:25 +00:00
|
|
|
else /* reset controller if we had an error */
|
|
|
|
{
|
|
|
|
MCI_CTRL |= (FIFO_RESET|DMA_RESET);
|
|
|
|
while(MCI_CTRL & (FIFO_RESET|DMA_RESET))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2010-02-22 10:02:20 +00:00
|
|
|
} while(retry || count);
|
2009-12-31 19:15:20 +00:00
|
|
|
|
|
|
|
dma_release();
|
|
|
|
|
2010-03-22 02:29:40 +00:00
|
|
|
/* CMD lines are separate, not common, so we need to actively deselect */
|
|
|
|
/* CMD7 w/rca =0 : deselects card & puts it in STBY state */
|
|
|
|
if(!send_cmd(drive, SD_DESELECT_CARD, 0, MCI_NO_RESP, NULL))
|
2010-05-16 10:24:31 +00:00
|
|
|
return -21;
|
2010-03-18 13:25:13 +00:00
|
|
|
|
2009-12-31 19:15:20 +00:00
|
|
|
#ifndef BOOTLOADER
|
|
|
|
sd_enable(false);
|
2010-03-22 06:09:14 +00:00
|
|
|
led(false);
|
2009-12-31 19:15:20 +00:00
|
|
|
#endif
|
|
|
|
mutex_unlock(&sd_mtx);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sd_transfer_error:
|
|
|
|
panicf("transfer error : %d",ret);
|
2010-03-15 01:57:15 +00:00
|
|
|
card_info[drive].initialized = 0;
|
2009-12-31 19:15:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
|
|
|
|
void* buf)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
2010-03-15 01:57:15 +00:00
|
|
|
return sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
2010-03-15 01:57:15 +00:00
|
|
|
int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
|
|
|
|
const void* buf)
|
2009-12-31 19:15:20 +00:00
|
|
|
{
|
2010-04-11 18:18:24 +00:00
|
|
|
#if defined(BOOTLOADER) /* we don't need write support in bootloader */
|
2010-03-15 01:57:15 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
(void) drive;
|
|
|
|
#endif
|
2009-12-31 19:15:20 +00:00
|
|
|
(void) start;
|
|
|
|
(void) count;
|
|
|
|
(void) buf;
|
|
|
|
return -1;
|
|
|
|
#else
|
2010-05-03 22:15:56 +00:00
|
|
|
return sd_transfer_sectors(IF_MD2(drive,) start, count, (void*)buf, true);
|
2010-04-12 10:25:49 +00:00
|
|
|
#endif /* defined(BOOTLOADER) */
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
long sd_last_disk_activity(void)
|
|
|
|
{
|
|
|
|
return last_disk_activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sd_enable(bool on)
|
|
|
|
{
|
|
|
|
/* TODO */
|
2010-04-06 17:22:51 +00:00
|
|
|
(void) on;
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tCardInfo *card_get_info_target(int card_no)
|
|
|
|
{
|
2010-03-15 01:57:15 +00:00
|
|
|
return &card_info[card_no];
|
2009-12-31 19:15:20 +00:00
|
|
|
}
|
|
|
|
#endif /* BOOTLOADER */
|
2010-03-15 01:57:15 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
bool sd_removable(IF_MD_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
return (drive==1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sd_present(IF_MD_NONVOID(int drive))
|
|
|
|
{
|
|
|
|
return (drive == 0) ? true : card_detect_target();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sd1_oneshot_callback(struct timeout *tmo)
|
|
|
|
{
|
|
|
|
(void)tmo;
|
|
|
|
|
|
|
|
/* This is called only if the state was stable for 300ms - check state
|
2010-04-30 08:57:56 +00:00
|
|
|
* and post appropriate event. */
|
2010-03-15 01:57:15 +00:00
|
|
|
if (card_detect_target())
|
|
|
|
{
|
|
|
|
queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-27 10:11:52 +00:00
|
|
|
void sd_gpioa_isr(void)
|
2010-03-15 01:57:15 +00:00
|
|
|
{
|
|
|
|
static struct timeout sd1_oneshot;
|
2010-04-27 10:11:52 +00:00
|
|
|
if (GPIOA_MIS & EXT_SD_BITS)
|
|
|
|
timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
|
2010-03-15 01:57:15 +00:00
|
|
|
/* acknowledge interrupt */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IC = EXT_SD_BITS;
|
2010-03-15 01:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void card_enable_monitoring_target(bool on)
|
|
|
|
{
|
|
|
|
if (on) /* enable interrupt */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IE |= EXT_SD_BITS;
|
2010-03-15 01:57:15 +00:00
|
|
|
else /* disable interrupt */
|
2010-04-27 10:11:52 +00:00
|
|
|
GPIOA_IE &= ~EXT_SD_BITS;
|
2010-03-15 01:57:15 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_HOTSWAP */
|
2010-03-18 13:25:13 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_STORAGE_MULTI
|
|
|
|
int sd_num_drives(int first_drive)
|
|
|
|
{
|
|
|
|
/* We don't care which logical drive number(s) we have been assigned */
|
|
|
|
(void)first_drive;
|
|
|
|
|
|
|
|
return NUM_DRIVES;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_STORAGE_MULTI */
|