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

📄 sdhcslot.cpp

📁 6410BSP3
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//

// Copyright (c) 2002 BSQUARE Corporation.  All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE

#include <bsp.h>

#include "SDHC.h"
#include "SDHCSlot.h"

static volatile BSP_ARGS *v_gBspArgs;
#define CARD_INSERTED 1
#define CARD_REMOVED 2

#define _SRCCLK_48MHZ_  // USB_PHY 48MHZ Clock. Keep sync with "hsmmc_s3c6410.cpp"

// Macros
#define DX_D1_OR_D2(cps)            ((cps) == D1 || (cps) == D2)
#define SETFNAME()  LPCTSTR pszFname = _T(__FUNCTION__) _T(":")

#ifdef DEBUG
// dump the current request info to the debugger
static 
VOID 
DumpRequest(
            PSD_BUS_REQUEST pRequest,
            DWORD dwZone
            )
{   
    PREFAST_DEBUGCHK(pRequest);

    if (dwZone) {
        DEBUGMSG(1, (TEXT("DumpCurrentRequest: 0x%08X\n"), pRequest)); 
        DEBUGMSG(1, (TEXT("\t Command: %d\n"),  pRequest->CommandCode));
        DEBUGMSG(1, (TEXT("\t Argument: 0x%08x\n"),  pRequest->CommandArgument));
        DEBUGMSG(1, (TEXT("\t ResponseType: %d\n"),  pRequest->CommandResponse.ResponseType)); 
        DEBUGMSG(1, (TEXT("\t NumBlocks: %d\n"),  pRequest->NumBlocks)); 
        DEBUGMSG(1, (TEXT("\t BlockSize: %d\n"),  pRequest->BlockSize)); 
        DEBUGMSG(1, (TEXT("\t HCParam: %d\n"),    pRequest->HCParam)); 
    }
}
#else
#define DumpRequest(ptr, dw)
#endif


CSDHCSlotBase::CSDHCSlotBase(
        )
{
    m_pregDevice = NULL;
    m_SlotDma = NULL;
    m_dwSlot = 0;
    m_pbRegisters = NULL;
    m_pHCDContext = NULL;
    m_dwSysIntr = 0;
    m_hBusAccess = NULL;
    m_interfaceType = InterfaceTypeUndefined;
    m_dwBusNumber = 0;  

    m_dwVddWindows = 0;
    m_dwMaxClockRate = 0;
    m_dwTimeoutControl = 0;
    m_dwMaxBlockLen = 0;

    m_wRegClockControl = 0; 
    m_wIntSignals = 0;
    m_cpsCurrent = D0;
    m_cpsAtPowerDown = D0;

    m_dwDefaultWakeupControl = 0;
    m_bWakeupControl = 0;

#ifdef DEBUG
    m_dwReadyInts = 0;
#endif
    m_fCommandCompleteOccurred = FALSE;

    m_fSleepsWithPower = FALSE;
    m_fPowerUpDisabledInts = FALSE;
    m_fIsPowerManaged = FALSE;
    m_fSDIOInterruptsEnabled = FALSE;
    m_fCardPresent = FALSE;
    m_fAutoCMD12Success = FALSE;
    m_fCheckSlot = TRUE;
    m_fCanWakeOnSDIOInterrupts = FALSE;
    m_f4BitMode = FALSE;
    m_fFakeCardRemoval = FALSE;

    m_pCurrentRequest = NULL;
    m_fCurrentRequestFastPath = FALSE;

    m_fCommandPolling = TRUE;
    m_fDisableDMA = FALSE;

    m_dwPollingModeSize = NUM_BYTE_FOR_POLLING_MODE ;
}


CSDHCSlotBase::~CSDHCSlotBase(
        )
{
    if (m_SlotDma)
        delete m_SlotDma;
}


BOOL
CSDHCSlotBase::Init(
        DWORD               dwSlot,
        volatile BYTE      *pbRegisters,
        PSDCARD_HC_CONTEXT  pHCDContext,
        DWORD               dwSysIntr,
        HANDLE              hBusAccess,
        INTERFACE_TYPE      interfaceType, 
        DWORD               dwBusNumber,
        CReg               *pregDevice
        )
{
    BOOL fRet = TRUE;

    DEBUGCHK(dwSlot < SDHC_MAX_SLOTS);
    DEBUGCHK(pbRegisters);
    DEBUGCHK(pHCDContext);
    DEBUGCHK(hBusAccess);
    PREFAST_DEBUGCHK(pregDevice && pregDevice->IsOK());
    PREFAST_DEBUGCHK(m_SlotDma==NULL);

    m_dwSlot = dwSlot;
    m_pbRegisters = pbRegisters;
    m_pHCDContext = pHCDContext;
    m_dwSysIntr = dwSysIntr;
    m_hBusAccess = hBusAccess;
    m_interfaceType = interfaceType;
    m_dwBusNumber = dwBusNumber;
    m_pregDevice = pregDevice;

    fRet = SoftwareReset(SOFT_RESET_ALL);
    if (fRet) { 
        Sleep(10); // Allow time for card to power down after a device reset

        SSDHC_CAPABILITIES caps = GetCapabilities();

        DEBUGMSG(SDCARD_ZONE_INIT && caps.bits.SDMA,
                (_T("SDHC Will use DMA for slot %u\n"), m_dwSlot));

        m_dwVddWindows = DetermineVddWindows();
        m_dwMaxClockRate = DetermineMaxClockRate();
        m_dwMaxBlockLen = DetermineMaxBlockLen();
        m_dwTimeoutControl = DetermineTimeoutControl();
        m_dwDefaultWakeupControl = DetermineWakeupSources();
        m_fCanWakeOnSDIOInterrupts = m_pregDevice->ValueDW(SDHC_CAN_WAKE_ON_INT_KEY);
        m_dwPollingModeSize = m_pregDevice->ValueDW(SDHC_POLLINGMODE_SIZE, NUM_BYTE_FOR_POLLING_MODE);
        m_dwFastPathTimeoutTicks = m_pregDevice->ValueDW(SDHC_POLLINGMODE_TIMEOUT,POLLING_MODE_TIMEOUT_DEFAULT);
        m_fDisableDMA = (m_pregDevice->ValueDW(SDHC_DISABLE_DMA_KEY,0)!=0);
        Validate();

        DumpRegisters();

        PHYSICAL_ADDRESS    ioPhysicalBase = {0,0};
        // Below code lines are needed for working as a mass storage device
        if (v_gBspArgs == NULL)
        {
            ioPhysicalBase.LowPart = IMAGE_SHARE_ARGS_PA_START;
            v_gBspArgs = (volatile BSP_ARGS *)MmMapIoSpace(ioPhysicalBase, sizeof(BSP_ARGS), FALSE);
            if (v_gBspArgs == NULL)
            {
                RETAILMSG(TRUE, (TEXT("[HSMMC1] HSMMC MmMapIoSpace: FAILED\r\n")));
                if (v_gBspArgs)
                {
                    MmUnmapIoSpace((PVOID) v_gBspArgs, sizeof(BSP_ARGS));    
                    v_gBspArgs = NULL;
                }
                return FALSE;
            }

            v_gBspArgs->g_SDCardState = CARD_REMOVED;    //initialize
            v_gBspArgs->g_SDCardDetectEvent = CreateEvent(NULL, FALSE, FALSE,NULL);
        }

    }

    return fRet;
}


SD_API_STATUS
CSDHCSlotBase::Start(
        )
{
    Validate();

    SD_API_STATUS status;

    if (SoftwareReset(SOFT_RESET_ALL)) {
        // timeout control
        WriteByte(SDHC_TIMEOUT_CONTROL, (BYTE) m_dwTimeoutControl);

        // Enable error interrupt status and signals for all but the vendor
        // errors. This allows any normal error to generate an interrupt.
        WriteWord(SDHC_ERROR_INT_STATUS_ENABLE, ~0 & ~ERR_INT_STATUS_VENDOR);
        WriteWord(SDHC_ERROR_INT_SIGNAL_ENABLE, ~0 & ~ERR_INT_STATUS_VENDOR);

        // disable the interrupt signals on the FIFO.
        WriteWord(SDHC_NORMAL_INT_SIGNAL_ENABLE, 0x1FF);
        WriteWord(SDHC_NORMAL_INT_STATUS_ENABLE,
                NORMAL_INT_ENABLE_CARD_INSERTION | NORMAL_INT_ENABLE_CARD_REMOVAL);

        status = SD_API_STATUS_SUCCESS;
    }
    else {
        status = SD_API_STATUS_DEVICE_NOT_RESPONDING;
    }

    return status;
}


SD_API_STATUS
CSDHCSlotBase::Stop(
        )
{    
    if (m_fCardPresent) {
        // Remove device
        HandleRemoval(FALSE);
    }

    SoftwareReset(SOFT_RESET_ALL);

    if(NULL != v_gBspArgs->g_SDCardDetectEvent) 
    {
        CloseHandle(v_gBspArgs->g_SDCardDetectEvent);
        v_gBspArgs->g_SDCardDetectEvent = NULL;
    }
    if (v_gBspArgs)
    {
        MmUnmapIoSpace((PVOID) v_gBspArgs, sizeof(BSP_ARGS));    
        v_gBspArgs = NULL;
    }

    return SD_API_STATUS_SUCCESS;
}


SD_API_STATUS 
CSDHCSlotBase::GetSlotInfo(
        PSDCARD_HC_SLOT_INFO pSlotInfo
        )
{
    PREFAST_DEBUGCHK(pSlotInfo);
    DEBUGCHK(m_pregDevice->IsOK());

    // set the slot capabilities
    DWORD dwCap = SD_SLOT_SD_4BIT_CAPABLE | 
        SD_SLOT_SD_1BIT_CAPABLE | 
#ifdef _SMDK6410_CH1_8BIT_
        // In order to distinguish Ch1 from Ch0 and 2, a capability for DAT 8-Bit on MMCplus is added.
        SD_SLOT_SD_8BIT_CAPABLE | 
#endif
        SD_SLOT_SDIO_CAPABLE    |
        SD_SLOT_SDIO_INT_DETECT_4BIT_MULTI_BLOCK;

    if (GetCapabilities().bits.HighSpeed){
        dwCap |= SD_SLOT_HIGH_SPEED_CAPABLE;
    }

    SDHCDSetSlotCapabilities(pSlotInfo,dwCap );

    SDHCDSetVoltageWindowMask(pSlotInfo, m_dwVddWindows); 

    // Set optimal voltage
    SDHCDSetDesiredSlotVoltage(pSlotInfo, GetDesiredVddWindow());     

    // Controller may be able to clock at higher than the max SD rate,
    // but we should only report the highest rate in the range.
    DWORD dwMaxClockRateInSDRange = SD_FULL_SPEED_RATE;
    SetClockRate(&dwMaxClockRateInSDRange);
    SDHCDSetMaxClockRate(pSlotInfo, dwMaxClockRateInSDRange);

    // Set power up delay. We handle this in SetVoltage().
    SDHCDSetPowerUpDelay(pSlotInfo, 1);

    return SD_API_STATUS_SUCCESS;
}


SD_API_STATUS
CSDHCSlotBase::SlotOptionHandler(
        SD_SLOT_OPTION_CODE   sdOption, 
        PVOID                 pData,
        DWORD                 cbData
        )
{
    SD_API_STATUS status = SD_API_STATUS_SUCCESS;

    switch (sdOption) {
        case SDHCDSetSlotPower: {
                                    DEBUGCHK(cbData == sizeof(DWORD));
                                    PDWORD pdwVddSetting = (PDWORD) pData;
                                    SetVoltage(*pdwVddSetting);
                                    break;
                                }

        case SDHCDSetSlotInterface: {
                                        DEBUGCHK(cbData == sizeof(SD_CARD_INTERFACE));
                                        PSD_CARD_INTERFACE pInterface = (PSD_CARD_INTERFACE) pData;

                                        DEBUGMSG(SDCARD_ZONE_INFO, 
                                                (TEXT("CSDHCSlotBase::SlotOptionHandler: Clock Setting: %d\n"), 
                                                 pInterface->ClockRate));
                                        SD_CARD_INTERFACE_EX sdCardInterfaceEx;
                                        memset(&sdCardInterfaceEx,0, sizeof(sdCardInterfaceEx));

                                        sdCardInterfaceEx.InterfaceModeEx.bit.sd4Bit = (((pInterface->InterfaceMode) == SD_INTERFACE_SD_4BIT)? 1:0);
                                        // An interface for DAT 8-Bit on MMCplus is added.
                                        sdCardInterfaceEx.InterfaceModeEx.bit.hsmmc8Bit = (((pInterface->InterfaceMode) == SD_INTERFACE_MMC_8BIT)? 1:0);

                                        sdCardInterfaceEx.ClockRate = pInterface->ClockRate;
                                        sdCardInterfaceEx.InterfaceModeEx.bit.sdWriteProtected = (pInterface->WriteProtected?1:0);
                                        SetInterface(&sdCardInterfaceEx);
                                        // Update the argument back.
                                        // The interface for DAT 8-Bit on MMCplus must be checked prior to orther interfaces.
                                        if (sdCardInterfaceEx.InterfaceModeEx.bit.hsmmc8Bit != 0) {
                                            pInterface->InterfaceMode = SD_INTERFACE_MMC_8BIT;
                                        } else {
                                            pInterface->InterfaceMode = (sdCardInterfaceEx.InterfaceModeEx.bit.sd4Bit!=0?SD_INTERFACE_SD_4BIT:SD_INTERFACE_SD_MMC_1BIT);
                                        }
                                        pInterface->ClockRate =  sdCardInterfaceEx.ClockRate;
                                        pInterface->WriteProtected = (sdCardInterfaceEx.InterfaceModeEx.bit.sdWriteProtected!=0?TRUE:FALSE);
                                        break;
                                    }
        case SDHCDSetSlotInterfaceEx: {
                                          DEBUGCHK(cbData == sizeof(SD_CARD_INTERFACE_EX));
                                          PSD_CARD_INTERFACE_EX pInterface = (PSD_CARD_INTERFACE_EX) pData;

                                          DEBUGMSG(SDCARD_ZONE_INFO, 
                                                  (TEXT("CSDHCSlotBase::SlotOptionHandler: Clock Setting: %d\n"), 
                                                   pInterface->ClockRate));

                                          SetInterface((PSD_CARD_INTERFACE_EX)pInterface);
                                      }

        case SDHCDEnableSDIOInterrupts:
        case SDHCDAckSDIOInterrupt:
                                      EnableSDIOInterrupts(TRUE);
                                      break;

        case SDHCDDisableSDIOInterrupts:

⌨️ 快捷键说明

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