⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms.c

📁 8032底层驱动部分。因为可以移植 所以单独来拿出来
💻 C
📖 第 1 页 / 共 4 页
字号:
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   ms.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   driver functons for Memory Stick
 *   
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================ 
 ****************************************************************************/
//	Include files
#include "kal_release.h"
#include "msdc_def.h"
#include "ms_def.h"
#include "gpt_sw.h"
#include "intrCtrl.h"
#include "reg_base.h"
#include "drv_comm.h"

#if defined(__MSDC_MS__)

//	global variables
T_MS_HANDLE	 gMS;	// store some important parameters of MS
static kal_uint16 gLPtable[MS_PAGE_SIZE];	// for LBA(Logical adrs) to PBA(Physical adrs) translation

/*************************************************************************
* FUNCTION
*  MS_Initialize
*
* DESCRIPTION
*  1. Initial the global variables for MS.
*  2. Initial the MSDC controller.
*  3. Initial the MS Card. 
*
* PARAMETERS
*
* RETURNS
*  MS_STATUS
*
* GLOBALS AFFECTED
*  1. gMS
*	2. gLPtable
*************************************************************************/
MS_STATUS MS_Initialize(void)
{
	kal_uint32 buffer;
	MS_STATUS status;

	// reset the events
	#ifdef MSDC_USE_INT
	// reset the events
	kal_set_eg_events(MSDC_Events, 0, KAL_AND);
	#endif
			
	// reset msdc
	if(*(volatile kal_uint32*)MSDC_CFG & MSDC_CFG_RST)
	{
		MSDC_CLR_BIT32(MSDC_CFG, MSDC_CFG_RST);
	}
	else
	{
		RESET_MSDC();
	}
			
	// pull down for MS and MS-PRO
	BitFieldWrite32((kal_uint32*)MSDC_CFG,(kal_uint32)1,MSDC_CFG_PRCFG1);
	BitFieldWrite32((kal_uint32*)MSDC_CFG,(kal_uint32)1,MSDC_CFG_PRCFG2);
	
	//set clock of serial clcok
	gMSDC_Handle.msdc_clock = MSDC_CLOCK;
	MSDC_SetClock(MSDC_OP_CLOCK);
	// set ready time out value
	BitFieldWrite32((kal_uint32*)MSC_CFG,(kal_uint32)7,MSC_CFG_BUSYCNT);
	// enable the serial interface
	MSDC_SET_BIT32(MSC_CFG,MSC_CFG_SIEN);
	// initila the structure
	MS_SetDefault();
	if ((status = MS_API_ResetFlash())!=MS_NOERROR)
		return status;
	if ((status =MS_API_ClrBuffer())!=MS_NOERROR)
		return status;
	// check write protect status
	if ((status = MS_TPC_SetRWAdrs(MS_STA0_REG,1,0,0))!=MS_NOERROR)
		return status;
	if ((status = MS_TPC_ReadReg(&buffer,1))!=MS_NOERROR)
		return status;
	gMS.is_wp = (kal_uint8)buffer & STA0_WP;
	// set system parameter to 0x80
	buffer = MS_SYS_DEFAULT;
	if((status = MS_TPC_SetRWAdrs(0,0,MS_SYS_REG,1))!=MS_NOERROR)
		return status;
	if((status = MS_TPC_WriteReg(&buffer,1))!=MS_NOERROR)
		return status;
	// read the boot block
	if((status = MS_API_ReadBootBlk())!=MS_NOERROR)
		return  MS_ERR_READBOOTBLK;
	//if((status = MS_API_CheckLogicalAdrsValid())!=MS_NOERROR)
		//return MS_ERR_LPTABLE;
	// generate the LPTable for the first segment
	if ((status = MS_API_GenLPTable(gMS.pLPTbl,gMS.pFreeTbl,0))!=MS_NOERROR)
		return MS_ERRORS;
	
	gMSDC_Handle.mIsInitialized = KAL_TRUE;
	return MS_NOERROR;
}

/*************************************************************************
* FUNCTION
*  MS_SetDefault
*
* DESCRIPTION
*  1. Initial the global variables for MS.
*
* PARAMETERS
*
* RETURNS
*
* GLOBALS AFFECTED
*  1. gMS
*	2. gLPtable
*************************************************************************/

void MS_SetDefault(void)
{
	kal_mem_set(&gMS,0,sizeof(T_MS_HANDLE));
	// oxFFFF means invalid Physical address
	kal_mem_set((void *)gLPtable,0xFF,sizeof(gLPtable));
	// 512 : (pLPtbl)496 ... (pFreeTbl)16
	gMS.pLPTbl = gLPtable;
	gMS.pFreeTbl = gLPtable+MS_LPTABLE_SIZE;
}

/*************************************************************************
* FUNCTION
*  MS_CardDetect
*
* DESCRIPTION
*  1. Detect if the card is present
*
* PARAMETERS
*
* RETURNS
*	kal_bool
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_bool MS_CardDetect(void)
{
	int i;

	// pull up CD/DAT3(INS) pin
	BitFieldWrite32((kal_uint32*)MSDC_CFG,2,MSDC_CFG_PRCFG0);
	// enable card detection and input pin at the same time
	MSDC_SET_BIT32(MSDC_PS, (MSDC_PS_CDEN|MSDC_PS_PIEN0));
	// trun on power for memory card
	MSDC_SET_BIT32(MSDC_CFG,MSDC_CFG_VDDPD);
	// to be modified : 1000 magic number 
	for( i=0; i<1000; i++)
	{
		if(*(volatile kal_uint32*)(MSDC_PS) & MSDC_PS_PINCH)
			return KAL_TRUE;
	}
	return KAL_FALSE;
}

/*************************************************************************
* FUNCTION
*  MS_WaitCmdRdyOrTo
*
* DESCRIPTION
*  1. Wait until MS command ready or time out
*
* PARAMETERS
*
* RETURNS
*	MS_STATUS
*
* GLOBALS AFFECTED
*  1. MSDC_Events if using HW interrupt.
*	
*************************************************************************/
MS_STATUS MS_WaitCmdRdyOrTo(void)
{
#ifdef MSDC_USE_INT	
	register kal_uint16 msc_sta;
	kal_uint32 	flags = 0;

	if(!gMSDC_Handle.mIsPresent) 
		return MS_ERR_CARD_NOT_PRESENT;	
	MSDC_START_TIMER(MSDC_TIMEOUT_PERIOD_DAT);
	kal_retrieve_eg_events(MSDC_Events,EVENT_MSIFIRQ|EVENT_MSRDYIRQ,KAL_OR_CONSUME,&flags,KAL_SUSPEND);	
	MSDC_STOP_TIMER();
	msc_sta = *(volatile kal_uint16*)MSC_STA;  
	if(msc_sta & MSC_STA_CRCERR)           
	{
		// if autocmd is enabled, then reset MSDC is a MUST!
		MSDC_SET_BIT32(MSDC_CFG,MSDC_CFG_RST);
		return MS_ERR_CRCERR;              
	} 
	else if(msc_sta & MSC_STA_TOER)        
	{
		// if autocmd is enabled, then reset MSDC is a MUST!
		MSDC_SET_BIT32(MSDC_CFG,MSDC_CFG_RST);
		return MS_ERR_TIMEOUT;             
	} else if(msc_sta & MSC_STA_RDY)                     
		return MS_NOERROR;                           
#else	// MSDC_USE_INT
	register kal_uint16 msc_sta;
	kal_uint32 t1;

	t1 = drv_get_current_time();
	MSDC_START_TIMER(MSDC_TIMEOUT_PERIOD_DAT);
	while(!MSDC_IS_INT && MSDC_Check_Card_Present() && !gMSDC_Handle.is_timeout)
	{
		if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
			gMSDC_Handle.is_timeout = KAL_TRUE;
	};
	MSDC_STOP_TIMER();
	MSDC_CLR_INT();	
	msc_sta = *(volatile kal_uint16*)MSC_STA;
	if(msc_sta & MSC_STA_CRCERR)
		return MS_ERR_CRCERR;
	else if(msc_sta & MSC_STA_TOER)
		return MS_ERR_TIMEOUT;
	else if(msc_sta & MSC_STA_RDY)
		return MS_NOERROR;
	if(gMSDC_Handle.is_timeout)
		return MSDC_GPT_TIMEOUT_ERR;

#endif	// MSDC_USE_INT

	return MS_NOERROR;
}

/*************************************************************************
* FUNCTION
*  MS_SendTPC
*
* DESCRIPTION
*  1. tell the controller to launch the TPC code with data size.
*
* PARAMETERS
*	1. cmd: TPC( Transfer Protocol Command)
*	2. size: bytes of data for current transaction
*
* RETURNS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
void MS_SendTPC(kal_uint8 cmd, kal_uint32 size)
{
	kal_uint16 arg = 0;

	arg = cmd << MSC_CMD_PID_START;
	arg |= size;
	MSDC_WriteReg16(MSC_CMD,arg);
}

/*************************************************************************
* FUNCTION
*  MS_SetAcmd
*
* DESCRIPTION
*  1. Enable Auto command
*
* PARAMETERS
*	1. acmd: TPC of GET_INT or READ_REG
*	2. asize: bytes of data for current transaction
*
* RETURNS
*
* GLOBALS AFFECTED
*	
* NOTE
*	Auto command will take one INT bs state
*
*************************************************************************/
void MS_SetAcmd(kal_uint8 acmd, kal_uint32 asize)
{
	kal_uint16 arg = 0;

	arg = (acmd << MSC_ACMD_APID_START);
	arg |= (asize << MSC_ACMD_ACEN_START);
	arg |= MSC_ACMD_ACEN;
	MSDC_WriteReg16(MSC_ACMD,arg);
}

/*************************************************************************
* FUNCTION
*  MS_TPC_ReadPage
*
* DESCRIPTION
*  1. TPC for reading data from PageBuffer(512 bytes)
*
* PARAMETERS
*	1. rxbuffer: used for storing page data read from MS
*
* RETURNS
*	MS_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	1. Two mechanisms of DMA transfer and Polling are implemented.
*	2. Make sure rxbuffer is at least the size of 512 bytes.
*
*************************************************************************/
MS_STATUS MS_TPC_ReadPage(kal_uint32 *rxbuffer)
{
	MS_STATUS status;
	kal_uint8 *ptr;
	#if defined(MT6218)
	kal_uint8 is_aligned;
	
	is_aligned = ((kal_uint32)rxbuffer % 4  == 0);	
	ptr = (is_aligned)?(kal_uint8*)rxbuffer:(kal_uint8*)MSDC_Sector;
	#else
	ptr = (kal_uint8*)rxbuffer;
	#endif // #if defined(MT6218)
	

#ifdef MSDC_DMA
	EnableMSDC_DMA();
#if defined(MT6218)
	MSDC_DMATransferFirst((kal_uint32)ptr, MS_PAGE_SIZE/sizeof(kal_uint32),KAL_FALSE);
#else // MT6218B, MT6219 wit B2W
	MSDC_DMATransferFirst((kal_uint32)ptr,MS_PAGE_SIZE/sizeof(kal_uint32),KAL_FALSE);
#endif //MT6218				

	MS_SendTPC(TPC_READ_PAGE_DATA, MS_PAGE_SIZE);
	#if defined(MT6218)
	status = MSDC_DMATransferFinal();
	#else // MT6218B, MT6219 wit B2W
	status = MSDC_DMATransferFinal();
	#endif //MT6218				
	if(status != MS_NOERROR)
				goto ERR_Exit;		
	
#else	//MSDC_DMA
	{
		int i;	

		MS_SendTPC(TPC_READ_PAGE_DATA,MS_PAGE_SIZE);
		// read data from fifo
		for(i=0; i<MS_PAGE_SIZE/sizeof(kal_uint32);)
		{
			if(!MSDC_IS_FIFO_EMPTY)
			{
				*((kal_uint32*)ptr+i) = *(volatile kal_uint32*)MSDC_DAT;
				i++;
			}
		}
	}
#endif	//MSDC_DMA
	if((status = MS_WaitCmdRdyOrTo())!=MS_NOERROR)
	{
		goto ERR_Exit;
	}	
	#if defined(MT6218)
	if(!is_aligned)
		kal_mem_cpy(rxbuffer,ptr,512);	
	#endif
	DisableMSDC_DMA();

	return MS_NOERROR;
	
ERR_Exit:
	#ifdef MSDC_USE_INT
	// reset the events
	kal_set_eg_events(MSDC_Events, 0, KAL_AND);
	#endif
	DisableMSDC_DMA();
	return status;
}

/*************************************************************************
* FUNCTION
*  MS_TPC_WritePage
*
* DESCRIPTION
*  1. TPC for writing data to PageBuffer(512 bytes)
*
* PARAMETERS
*	1. txbuffer: used for storing data written to MS
*
* RETURNS
*	MS_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	1. Two mechanisms of DMA transfer and Polling are implemented.
*	2. Make sure rxbuffer is at least the size of 512 bytes.
*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -