📄 sd_drv.c
字号:
/*****************************************************************************
* 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_drv.c
*
* Project:
* --------
* Maui_Software
*
* Description:
* ------------
* Adaption driver of file system for SD card
*
* 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!
* 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 "kal_release.h"
#include "app_buff_alloc.h"
#include "fat_fs.h"
#include "msdc_def.h"
#include "sd_def.h"
#include "ms_def.h"
#include "intrCtrl.h"
#if defined(__MSDC_SD_MMC__)
#define MAX_CLUSTERS (0x0FFF6 - 2)
#define SECTOR_SIZE 512
#define MSDC_MEDIA_DESCRIPTOR 0xF8
#define SD_MAX_RETRY 3
static int ReadSectors(void * DriveData, DWORD Sector, UINT Sectors, void * Buffer);
static int WriteSectors(void * DriveData, DWORD Sector, UINT Sectors, void * Buffer);
static kal_uint32 sd_r,sd_w;
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
static int DiscardSectors(void * DriveData, DWORD Sector, UINT Sectors)
{
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
return FS_NO_ERROR;
}
/*-----------------------------------*/
static int ReadSectors(void * DriveData, DWORD Sector, UINT Sectors, void * Buffer)
{
SDC_CMD_STATUS status;
kal_uint8 retry = 0;
// kal_prompt_trace(MOD_AUD,"R: %d, %d",Sector Sectors);
//dbg_print("R: %x, %x\r\n",Sector, Sectors);
gMSDC_Handle.timeout_count = 0;
start:
if(!gMSDC_Handle.mIsInitialized)
{
//dbg_print("Read but not Initialized \r\n");
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_READ_SECTOR_ERROR;
}
retry ++;
MSDC_PDNControl(KAL_FALSE);
if(Sectors > 1)
status = SD_ReadMultiBlock((kal_uint32)Sector*SECTOR_SIZE,(kal_uint32*)Buffer,(kal_uint32)Sectors);
else
status = SD_ReadSingleBlock((kal_uint32)Sector*SECTOR_SIZE,(kal_uint32*)Buffer);
if(status != NO_ERROR)
{
sd_r++;
//dbg_print("read retry:%d,status:%d,total %d\r\n",retry,status,sd_r);
if(status == ERR_CMD_TIMEOUT || status == MSDC_GPT_TIMEOUT_ERR)
gMSDC_Handle.timeout_count++;
if(gMSDC_Handle.timeout_count == 3 && gMSDC_Handle.mIsPresent == KAL_TRUE)
{
kal_print("[MSDC]:re-mount(read fail)");
gMSDC_Handle.mIsInitialized = KAL_FALSE;
retry = 0;
if(SD_Initialize() != NO_ERROR)
{
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_READ_SECTOR_ERROR;
}
}
if(retry >= SD_MAX_RETRY)
{
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_READ_SECTOR_ERROR;
}
else
{
// kal_prompt_trace(MOD_AUD,"CRC etry:%d,status:%d",retry,status);
goto start;
}
}
MSDC_PDNControl(KAL_TRUE);
return FS_NO_ERROR;
}
/*-----------------------------------*/
static int WriteSectors(void * DriveData, DWORD Sector, UINT Sectors, void * Buffer)
{
SDC_CMD_STATUS status;
kal_uint8 retry = 0;
//dbg_print("W: %x, %x\r\n",Sector, Sectors);
gMSDC_Handle.timeout_count = 0;
start:
if(!gMSDC_Handle.mIsInitialized)
{
//dbg_print("Write but not Initialized \r\n");
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_WRITE_SECTOR_ERROR;
}
retry++;
MSDC_PDNControl(KAL_FALSE);
if(Sectors > 1)
{
if(gMSDC_Handle.mMSDC_type == SD_CARD)
SD_SetPreEraseBlk(Sectors);
status = SD_WriteMultiBlock((kal_uint32)Sector*SECTOR_SIZE,(kal_uint32*)Buffer,(kal_uint32)Sectors);
}
else
status = SD_WriteSingleBlock((kal_uint32)Sector*SECTOR_SIZE,(kal_uint32*)Buffer);
if(status != NO_ERROR)
{
sd_w++;
//dbg_print("write retry:%d,status:%d,total %d\r\n",retry,status,sd_w);
if(status == ERR_CMD_TIMEOUT || status == MSDC_GPT_TIMEOUT_ERR)
gMSDC_Handle.timeout_count++;
if(gMSDC_Handle.timeout_count == 3 && gMSDC_Handle.mIsPresent == KAL_TRUE)
{
kal_print("[MSDC]:SD re-mount (write fail)");
gMSDC_Handle.mIsInitialized = KAL_FALSE;
retry = 0;
if(SD_Initialize() != NO_ERROR)
{
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_WRITE_SECTOR_ERROR;
}
}
if(retry >= SD_MAX_RETRY)
{
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_WRITE_SECTOR_ERROR;
}
else
{
// kal_prompt_trace(MOD_AUD,"CRC write Error retry %d",retry);
goto start;
}
}
MSDC_PDNControl(KAL_TRUE);
return FS_NO_ERROR;
}
/*-----------------------------------*/
static int MountDevice(void * DriveData, int DeviceNumber, int DeviceType, DWORD Flags)
{
kal_uint8 retry = 0;
//dbg_print("MountDevice_SD \r\n");
if(gMSDC_Handle.mIsInitialized)
return SECTOR_SIZE;
start:
if(!gMSDC_Handle.mIsPresent)
{
//dbg_print("not present \r\n");
#if defined(_NAND_FLASH_BOOTING_)
// add the following code for solving plug in or out the SD card during
// NFB loading process. The card detection interruptwill disapperared
// while interrupt controller is masked.
IRQMask(IRQ_MSDC_CODE);
if(*(volatile kal_uint16*)MSDC_PS & MSDC_PS_PIN0)
{
if(gMSDC_Handle.ins_level == MSDC_IOCTRL_PULL_UP)
gMSDC_Handle.mIsPresent = KAL_FALSE;
else
gMSDC_Handle.mIsPresent = KAL_TRUE;
}
else
{
if(gMSDC_Handle.ins_level == MSDC_IOCTRL_PULL_UP)
gMSDC_Handle.mIsPresent = KAL_TRUE;
else
gMSDC_Handle.mIsPresent = KAL_FALSE;
}
IRQUnmask(IRQ_MSDC_CODE);
#endif // _NAND_FLASH_BOOTING_
if(!gMSDC_Handle.mIsPresent)
{
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_MOUNT_ERROR;
}
}
MSDC_PDNControl(KAL_FALSE);
if(SD_Initialize() != NO_ERROR)
{
goto err_exit;
}
//if(SD_MountDevice(DeviceType) != FS_NO_ERROR)
//return FS_MSDC_MOUNT_ERROR;
//dbg_print("Mount success! \r\n");
MSDC_PDNControl(KAL_TRUE);
return SECTOR_SIZE;
err_exit:
if(retry++ <= SD_MAX_RETRY)
{
//dbg_print("SD MountDevice failed! retry: %d \r\n",retry);
goto start;
}
#if defined(__MSDC_NOT_SUPPORT_HOT_PLUG__)
gMSDC_Handle.mIsPresent = KAL_FALSE;
#endif
MSDC_PDNControl(KAL_TRUE);
return FS_MSDC_MOUNT_ERROR;
}
/*-----------------------------------*/
static int MediaChanged(void * DriveData)
{
//dbg_print("MediaChanged\n\r");
if(gMSDC_Handle.mIsPresent)
{
if(gMSDC_Handle.mIsInitialized)
{
//dbg_print("Ready\n\r");
return FS_NO_ERROR;
}
else
{
//dbg_print("Not Init\n\r");
return FS_MSDC_PRESNET_NOT_READY;
}
}
//dbg_print("No card\n\r");
return FS_MSDC_NOT_PRESENT;
}
/*-----------------------------------*/
static int GetDiskGeometry(void * DriveData, FS_PartitionRecord * DiskGeometry, BYTE * MediaDescriptor)
{
kal_mem_set((void*)DiskGeometry, 0, sizeof * DiskGeometry);
DiskGeometry->Sectors = gSD.mCSD.capacity/512;
// 0xF8 is the standard value for fixed media, 0xF0 is for removable media.
// The important point is whatever value is put in here must also be put in the first
// byte of he FAT[0].
*MediaDescriptor = MSDC_MEDIA_DESCRIPTOR;
if(gSD.mWPEnabled)
return FS_WRITE_PROTECTION;
return FS_NO_ERROR;
}
/*-----------------------------------*/
static int ShutDown(void * DriveData)
{
return FS_NO_ERROR;
}
FS_Driver FS_SdDrv = {
MountDevice,
ShutDown,
ReadSectors,
WriteSectors,
MediaChanged,
DiscardSectors,
GetDiskGeometry,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
MSDC_GetCardStatus
};
#endif // (__MSDC_SD_MMC__)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -