📄 mmcoem.c
字号:
/*****************************************************************************
* FileName: MMCOEM.C
*
* SanDisk Host Developer's Toolkit
*
* Copyright (c) 1997 - 2000 SanDisk Corporation
* All rights reserved.
* This code may not be redistributed in source or linkable object form
* without the consent of its author.
*
* Description:
*
* MMC platform specific routines.
*
******************************************************************************/
#include "sdapi.h"
#include "sdmmc.h"
#include <stdio.h>
#include "plx9054.h"
#include <bvd1.h>
#include <oalintr.h>
#include <nkintr.h>
#include <windows.h>
#include <blrbits.h>
#include "bcr.h"
#include "blrbits.h"
#include <oalintr.h>
#include <nkintr.h>
#include <bvd1bd.h>
#include "xllp_mmc.h"
#include "xllp_common.h"
#include "cken.h"
#include "drive.h"
#define SSTOP_TRANSMISSION 0x4C
extern int SD_Card_In_Use;
//#define NMD_DEBUG
HANDLE gSDMMCIntrEvent;
extern void msWait(unsigned);
extern void XllpMmcSdHWInit(P_XLLP_GPIO_T GpioReg, P_XLLP_CLKMGR_T ClckReg, P_XLLP_BCR_T BcrReg);
extern SDVOID setupMMcHostDataRegister(UINT16 cmdIndex, UINT16 *cmdDatContReg, UINT16 *noBlocks, UINT16 *blkLength);
extern XLLP_BOOL_T XllpMmcSdSetupCmd(P_XLLP_MMC_T mmcRegs, XLLP_MMC_CMD cmd, XLLP_INT32_T arg, XLLP_INT32_T To, XLLP_BOOL_T fourBitMode );
extern XLLP_BOOL_T XllpMmcSdSetupXCmd(P_XLLP_MMC_T mmcRegs, XLLP_INT32_T FlSz, XLLP_INT32_T BlkSz, XLLP_INT32_T To );
UINT32 portAddress = 0;
INT16 driveSave = 0;
extern SDBOOL mmc_init_setup(SDVOID);
volatile P_XLLP_GPIO_T v_pGPIOReg = NULL;
volatile P_XLLP_MMC_T v_pMMCReg = NULL;
volatile P_XLLP_BCR_T v_pBLReg = NULL;
volatile P_XLLP_CLKMGR_T v_pClockRegs= NULL;
//volatile XLLP_OST_T *v_pOSTReg = NULL;
// pointer to the MMC RX FIFO
volatile UCHAR *pMMC_RX_Fifo;
int data_trans_flag = 0;
UINT32 total;
PVOID VirtualAllocCopy(unsigned size,char *str,PVOID pVirtualAddress);
/************************************************************************/
/* PCI Internal variables */
/************************************************************************/
SDLOCAL INT16 noController = 0;
SDLOCAL ADAPTER myAdapters[N_CONTROLLERS];
/* Function prototypes */
SDBOOL mmcAdapterInfo(INT16 controller, UINT32 *pPort, INT16 *pIntr);
static XLLP_BOOL_T HW_PWRUP = FALSE;
void hw_powerup(void);
void hw_powerdown(void);
void hw_mmc_hex_led(DWORD );
extern SDBOOL mmc_drive_open( INT16 driveno );
void hw_mmc_hex_led(DWORD dis)
{
#ifdef PLAT_LUBBOCK
v_pBLReg->hex_led=dis;
#endif
}
void hw_powerdown()
{
if(data_trans_flag == 1)
while(!(v_pMMCReg->MMC_STAT & 0x00000800));
stopMMCClock();
}
void hw_powerup()
{
XllpMmcSdHWInit(v_pGPIOReg,v_pClockRegs, v_pBLReg);
msWait(20); //wait 1ms per MMC specification.
v_pMMCReg->MMC_I_MASK = 0xFF; // enable all interrupts
startMMCClock();
HW_PWRUP= TRUE;
#if 0 //hzh
v_pBLReg->HLDR1 = 0xaaaaaaaa;
#endif
}
/**************************** MMC Controller ********************************/
/****************************************************************************/
SDVOID MMCSelectController(INT16 ctrlNo, INT16 phys_drive)
{
return;
}
/*
/****************************************************************************
* Name: mmc_hw_init - Initialize MMC hardware
*
* Description:
* MMC controller intialization
*
* Input:
* controller_no Controller number
*
* Output:
* None
*
* Return:
* None
*
****************************************************************************/
SDVOID mmc_hw_init(INT16 ctrlno) /*__fn__*/
{
VOID *SDMMCIntrEvent;
// v_pOSTReg = (XLLP_OST_T *) VirtualAllocCopy(0x400,(char *)TEXT("TouchPanelEnable: OST_BASE_U_VIRTUAL"),
// (PVOID)OST_BASE_U_VIRTUAL);
v_pBLReg = (P_XLLP_BCR_T )VirtualAllocCopy(sizeof(XLLP_BCR_T),"FPGA service", (PVOID)FPGA_REGS_BASE_U_VIRTUAL);
// RETAILMSG(1, (TEXT("mmc_hw_init is entered\r\n"))); //do some stuff here
SDMMCIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
v_pGPIOReg = (P_XLLP_GPIO_T )VirtualAllocCopy(0x400,(char *)TEXT("mmc_hw_init gpio regs"),
(PVOID)GPIO_BASE_U_VIRTUAL); //just modify one line.
if (!v_pGPIOReg)
return ;
v_pMMCReg = (P_XLLP_MMC_T )VirtualAllocCopy(0x400,(char *)TEXT("TouchPanelEnable: GPIO_BASE_U_VIRTUAL"),
(PVOID)MMC_BASE_U_VIRTUAL);
if (!v_pMMCReg)
return;
pMMC_RX_Fifo = (volatile UCHAR *)VirtualAllocCopy(sizeof(MMC_REGS),"pMMC_RX_Fifo",(PVOID)(MMC_BASE_U_VIRTUAL+0x40));
if (!pMMC_RX_Fifo)
return;
v_pClockRegs = (XLLP_CLKMGR_T *) VirtualAllocCopy(0x400,(char *)TEXT("inside start clock vAllocCopy"),
(PVOID)CLK_BASE_U_VIRTUAL);
if(!v_pClockRegs)
return;
XllpMmcSdHWInit(v_pGPIOReg,v_pClockRegs, v_pBLReg);
msWait(20); //wait 1ms per MMC specification.
v_pMMCReg->MMC_I_MASK = 0xFF; // enable all interrupts
gSDMMCIntrEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("SDMMC"));
startMMCClock();
// RETAILMSG(1, (TEXT("Finished mmc_hw_init\r\n")));
// test_mmc();
return;
}
/**************************************************************************
* Name: stopMMCClock - Stop the MMC Host controller clock
*
* Description:
* The clocks of the MMC Host controller is stopped.
*
* Input:
* portAddress Controller base address
*
* Output:
* Stopping the MMC Host clock.
*
* Return:
* None
*
**************************************************************************/
SDBOOL stopMMCClock(SDVOID)
{
UINT16 dTime;
UINT32 dStatus;
#ifdef NMD_DEBUG
RETAILMSG(1, (TEXT("stopMMCClock>\r\n")));
#endif
/* Disable MMC clock. */
v_pMMCReg->MMC_STRPCL = STOP_CLOCK;
dTime = 0x200;
dStatus = 0;
/* Check status register */
dStatus = v_pMMCReg->MMC_STAT;
while ( dTime )
{
dStatus = v_pMMCReg->MMC_STAT;
dTime--;
if ( !(dStatus & CLOCK_ENABLE) )
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("stopMMCClock<\r\n")));
#endif
return YES;
}
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("stopMMCClock<-\r\n")));
#endif
return NO;
}
/**************************************************************************
* Name: startMMCClock - Start the MMC Host Controller clock
*
* Description:
* The MMC clock is started by the.
*
* Input:
* portAddress Controller bas address
*
* Output:
* Starting the clock.
*
* Return:
* None
*
**************************************************************************/
SDVOID startMMCClock(SDVOID)
{
#ifdef NMD_DEBUG
RETAILMSG(1, (TEXT("startMMCClock>\r\n")));
#endif
/* Start the MMC clock. */
v_pMMCReg->MMC_STRPCL = START_CLOCK;
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("startMMCClock<\r\n")));
#endif
}
/**************************************************************************
* Name: setMMCClockRate - Set the MMC clock rate
*
* Description:
* Stop the MMC clock and set the new clock rate
* then start the clock again.
*
* Input:
* ClockRate Clock rate
*
* Output:
* Set new clock rate.
*
* Return:
* YES if successful
* NO if failure
*
**************************************************************************/
SDBOOL setMMCClockRate(UINT16 ClockRate)
{
#ifdef NMD_DEBUG
RETAILMSG(1, (TEXT("setMMCClockRate>\r\n")));
#endif
/* The clock needs to be stopped if currently active */
if ( !stopMMCClock() ) /* STOP MMC CLOCK */
{
// RETAILMSG(1, (TEXT("fail to stopMMCClock()\r\n")));
return NO;
}
/* Set the clock rate */
v_pMMCReg->MMC_CLKRT = ClockRate;
v_pMMCReg->MMC_RESTO = 0x0000007f;
v_pMMCReg->MMC_RDTO = 0xFFFF;
/* Start the clock again */
startMMCClock(); /* START MMC CLOCK */
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("setMMCClockRate<\r\n")));
#endif
return YES;
}
#define GO_IDLE_STATE 0x0
#define INIT_80CLCOK (1u << 6)
/**************************************************************************
* Name: startMMC80Clocks - Start the MMC devices
*
* Description:
* The 80 clocks is required to start initializing the
* devices.
*
* Input:
* ctrlNo Controller number
*
* Output:
* Sending out 80 clocks.
*
* Return:
* None
*
**************************************************************************/
SDVOID startMMC80Clocks(INT16 ctrlNo)
{
UINT32 ij, dStatus;
ij = (UINT16)ctrlNo; /* Make compiler happy! */
#ifdef NMD_DEBUG
RETAILMSG(1, (TEXT("startMMC80Clocks>\r\n")));
#endif
//Send the 80 clocks to start all devices
// if ( !stopMMCClock() ) /* STOP MMC CLOCK */
// return;
XllpMmcSdSetupCmd(v_pMMCReg, GO_IDLE_STATE, 0x0, 64, FALSE);
// v_pMMCReg->MMC_CMD = GO_IDLE_STATE; //CMD0
// v_pMMCReg->MMC_ARGH = 0x00000000;
// v_pMMCReg->MMC_ARGL = 0x00000000;
// v_pMMCReg->MMC_CMDAT = RESPONSE_TYPE_R0 |SEND_80_CLOCKS; //set init bit for initial 80 clocks, expect response 3
// v_pMMCReg->MMC_STRPCL = START_CLOCK; //start clock
for (ij = 0; ij < 0x200; ij++ )
{
dStatus = v_pMMCReg->MMC_STAT;
if ( dStatus & END_CMD_RES )
{
break;
}
}
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("startMMC80Clocks<\r\n")));
#endif
//Ramp-up time
OS_WAIT(200);
}
/**************************************************************************
* Name: resetMMCController - Reset MMC host controller
*
* Description:
* Writing to the port with any value will reset the
* MMC Host controller
*
* Input:
* ctrlNo Controller number
*
* Output:
* Reset the controller
*
* Return:
* None
*
**************************************************************************/
SDVOID resetMMCController(UINT16 ctrlNo)
{
}
/**************************************************************************
* Name: setupMMCSetupInterrupt - Enable MMC interrupt setting
*
* Description:
*
* Input:
* bitMask set the bis to 1's which need to be enabled
*
* Output:
*
* Return:
*
* Function: mmc interrupt enable subroutine
*
*************************************************************************/
SDVOID SetupMMCInterrupt(UINT16 bitMask)
{
UINT32 dtmp;
/* Set interrupt mask value */
dtmp = v_pMMCReg->MMC_I_MASK;
#if (USE_INTERRUPTS)
dtmp &= 0x0F;
dtmp |= bitMask;
#else
dtmp = bitMask;
dtmp &= 0;
#endif
v_pMMCReg->MMC_I_MASK = dtmp;
}
/**************************************************************************
* Name: disableMMCInterrupt - Disable MMC interrupt setting
*
* Description:
*
* Input:
* unMaskBit set the bis to 1's which need to be disabled
*
* Output:
*
* Return:
*
* Function: mmc interrupt disable subroutine
**************************************************************************/
SDVOID disableMMCInterrupt(UINT16 unMaskBit)
{
UINT32 dtmp;
/* Get Interrupt Mask value */
dtmp = v_pMMCReg->MMC_I_MASK;
/* Mask off unwanted interrupts. */
dtmp &= (~unMaskBit);
v_pMMCReg->MMC_I_MASK = dtmp;
}
/*********************** MMC command and response ***************************/
/****************************************************************************/
/***************************************************************************
* Name: MMCSendCommand
*
* Description:
*
* Input:
* portAddress Base Address
* cmd Command index
* arg Argument
*
* Output:
* 6-byte command is sent to the card
*
* Return:
* Complete code
*
* Function: send the command to MMC card. CRC is not needed for
* the FPGA controller.
*
***************************************************************************/
SDVOID MMCSendCommand( UINT32 Arg, UINT16 Cmd, UINT16 crcDATA)
{
UINT16 dtmp;
#ifdef NMD_DEBUG
RETAILMSG(1, (TEXT("MMCSendCommand>\r\n")));
#endif
dtmp = (UINT16)crcDATA;
/* Set up the argument. */
/* Set up the argument. */
dtmp = (UINT16)(Arg >> 16);
v_pMMCReg->MMC_ARGH = dtmp;
dtmp = (UINT16)(Arg & 0xFFFF);
v_pMMCReg->MMC_ARGL = dtmp;
/* Set up the command. */
dtmp = (Cmd & 0x7F);
v_pMMCReg->MMC_CMD = dtmp;
#ifdef NMD_DEBUG1
RETAILMSG(1, (TEXT("MMCSendCommand<\r\n")));
#endif
}
BOOL is_cardinserted()
{
#if 0 //hzh
if( (v_pBLReg->MISCRR1 & XLLP_BCR_MISCRR1_nMMC_CD) == 0x0)
return TRUE;
else
return FALSE;
#else
if(v_pGPIOReg->GPLR3 & (1<<2))
return FALSE;
else
return TRUE;
#endif
}
/***************************************************************************
* Name: getMMCResponseInfo - Get the response from MMC card
*
* Description:
* Get the response from MMC card
*
* Input:
* RespBuff Response buffer
* RespLength Length of the response
*
* Output:
* Response information
*
* Return:
* MMC Error code
*
***************************************************************************/
MMC_CC getMMCResponseInfo(UCHAR *respBuff, UINT16 respLength, UINT16 respType)
{
UINT32 dtmp, dtimer;
UINT16 i;
i = respType; /* Make compiler happy! */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -