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

📄 bspsdhc.cpp

📁 iMX21 SDHC driver(PDD layer)
💻 CPP
字号:
//
//	Copyright (c) Microsoft	Corporation.  All rights reserved.
//
//	Use	of this	source code	is subject to the terms	of the Microsoft end-user
//	license	agreement (EULA) under which you licensed this SOFTWARE	PRODUCT.
//	If you did not accept the terms	of the EULA, you are not authorized	to use
//	this source	code. For a	copy of	the	EULA, please see the LICENSE.RTF on	your
//	install	media.
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
//  File:  bspsdhc.cpp
//
//  This file implements platform specific functions for SDHC.
//
//------------------------------------------------------------------------------
#include "bsp.h"

//------------------------------------------------------------------------------
// External Variables

//------------------------------------------------------------------------------
// Defines
//
// This will define the choice of clock source in SDHC register
#define SDHC_CLK    	BSP_PERCLK2_FREQ

// Use polling for command response and small data transfer or not
//Use DMA for Transfer bulk Data
#define USE_TRANSFER_MODE  USE_POLLING_DMA
#define USE_DMA			1      //  for  use only DMA mode 
#define USE_POLLING_DMA   2   //  misc with polling and  DMA mode for best performance

//------------------------------------------------------------------------------
// Types

//------------------------------------------------------------------------------
// Global Variables

//------------------------------------------------------------------------------
// Local Variables
static GPIOHANDLE g_GpioHandle;			// DDK GPIO handle for card detect
static BSP_EIO_IO0_REGS *g_pEioReg;		// Pointer to EIO register area

//------------------------------------------------------------------------------
//
// Function: BSPSdhcGetCLK
//
// This function returns the BSP-specific clock source selection value
// for SDHC.
//
// Parameters:
//		None
//
// Returns:
//		The clock source for SDHC.
//
//------------------------------------------------------------------------------
UINT32 BSPSdhcGetCLK(void)
{
	return SDHC_CLK;
}


//------------------------------------------------------------------------------
//
// Function: BSPSdhcGetTransMode
//
// This function returns the BSP-specific transfer configurable selection value
// for SDHC.
//
//		USE_POLLING			1	
//		USE_DMA				2       
//		USE_POLLING_DMA   	3
// Parameters:
//		None
//
// Returns:
//		The SDHC Transfer mode.
//
//------------------------------------------------------------------------------
UINT32 BSPSdhcGetTransMode(void)
{
	return USE_TRANSFER_MODE;
}


//------------------------------------------------------------------------------
//
// Function: BSPSdhcSetClockGatingMode
//
// This function enable or disable PLL clock for SDHC.
//
// Parameters:
//		startClocks
//			[in] Boolean variable to enable or disable PLL clock.
//
// Returns:
//		TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcSetClockGatingMode(BOOL startClocks)
{
	DWORD dwTransferred;
	HWCLOCK_ID clockId = HWCLOCK_ID_SDHC1;

	if (startClocks) {
		// Turn SDHC1 clocks on
		if (!KernelIoControl(IOCTL_HAL_POWER_HW_CLK, &clockId, sizeof(clockId),
			NULL, 0, &dwTransferred)) {
			// Error message will be given by caller in CSP
			return FALSE;
		}
	} else {
		// Turn SDHC1 clocks off
		if (!KernelIoControl(IOCTL_HAL_SHUTDOWN_HW_CLK, &clockId, sizeof(clockId), 
			NULL, 0, &dwTransferred)) {
			// Error message will be given by caller in CSP
			return FALSE;
		}
	}

	return TRUE;
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcSetGpioMode
//
// This function calls to the GPIO module to set the GPIO mode,
// turning on or off GPIO to the SDHC.
//
// Parameters:
//		enableGpio
//			[in] If TRUE, turn GPIO to SDHC on, if FALSE, turn off.
//
// Returns:
//		TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcSetGpioMode(BOOL enableGpio)
{	
	DWORD dwTransferred;
	GPIO_IOCTL_CFG ioctl;

	GPIO_SET_IOCTL_STRUCT(ioctl, SDHC1);	
	if (enableGpio) {
		// Turn SDHC GPIO on
		if (!KernelIoControl(IOCTL_HAL_ENABLE_GPIO, &ioctl, sizeof(ioctl),
			NULL, 0, &dwTransferred)) {
			// Error message will be given by caller in CSP
			return FALSE;
		}
	} else {
		// Turn SDHC GPIO off
		if (!KernelIoControl(IOCTL_HAL_DISABLE_GPIO, &ioctl, sizeof(ioctl),
			NULL, 0, &dwTransferred)) {
			// Error message will be given by caller in CSP
			return FALSE;
		}
	}

	return TRUE;
}

//------------------------------------------------------------------------------
//
// Function: BSPGetTxDmaChannel
//
// Get TX DMA channel.
//
// Parameters:
//		None.
//
// Returns:
//		TX DMA channel.
//
//------------------------------------------------------------------------------
UINT8 BSPSdhcGetTxDmaChannel(void)
{
	return BSP_DMAC_CH_SDHC_TX;
}

//------------------------------------------------------------------------------
//
// Function: BSPGetRxDmaChannel
//
// Get RX DMA channel.
//
// Parameters:
//		None.
//
// Returns:
//		RX DMA channel.
//
//------------------------------------------------------------------------------
UINT8 BSPSdhcGetRxDmaChannel(void)
{
	return BSP_DMAC_CH_SDHC_RX;
}

//------------------------------------------------------------------------------
//
// Function: BSPGetTxDmaBufferSize
//
// Get TX DMA buffer size.
//
// Parameters:
//		None.
//
// Returns:
//		TX DMA buffer size.
//
//------------------------------------------------------------------------------
UINT32 BSPSdhcGetTxDmaBufferSize(void)
{
	return BSP_DMAC_SDHC_BUFF_SIZE;
}

//------------------------------------------------------------------------------
//
// Function: BSPGetRxDmaBufferSize
//
// Get RX DMA buffer size.
//
// Parameters:
//		None.
//
// Returns:
//		RX DMA buffer size.
//
//------------------------------------------------------------------------------
UINT32 BSPSdhcGetRxDmaBufferSize(void)
{
	return BSP_DMAC_SDHC_BUFF_SIZE;
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcInit
//
// BSP specific initialization.
//
// Parameters:
//		None.
//
// Returns:
//		TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcInit(void)
{
	PHYSICAL_ADDRESS phyAddr;

	phyAddr.QuadPart = BSP_BASE_REG_PA_EIO_IO0_BASE;
	g_pEioReg = (BSP_EIO_IO0_REGS *)MmMapIoSpace(phyAddr, 
		sizeof(BSP_EIO_IO0_REGS), FALSE);
	if (!g_pEioReg) {
		// Error message will be given by caller in CSP
		return FALSE;
	}

	g_GpioHandle = DDKCreateGpioObject();
	if (!g_GpioHandle) {
		// Get GPIO handle fail and resource will be release by
		// calling for BSPSdhcDeinit() from CSP.
		return FALSE;
	}
	
	return TRUE;
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcDeinit
//
// BSP specific deinitialization.
//
// Parameters:
//		None.
//
// Returns:
//		TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcDeinit(void)
{
	if (g_pEioReg) {
		MmUnmapIoSpace(g_pEioReg, sizeof(BSP_EIO_IO0_REGS));
		g_pEioReg = NULL;
	}
	
	if (g_GpioHandle) {
		DDKDeleteObject(g_GpioHandle);
		g_GpioHandle = NULL;
	}

	return TRUE;
}

//------------------------------------------------------------------------------
//
// Function: BspSdhcCardDetectInit
//
// This function is to setup detect interrupt.
//
// Parameters:
//		None
//
// Returns:
//		Valid sysintr number, succeed; SYSINTR_UNDEFINED, failed.
//
//------------------------------------------------------------------------------
DWORD BSPSdhcCardDetectInit(void)
{
	GPIO_IOCTL_CFG cfg;
	DWORD dwTransferred;
	DWORD irq;
	DWORD dwSysIntrCardDetect;

	// Configure IO pin as interrupt to be used for card detection
	GPIO_SET_IOCTL_STRUCT(cfg, INTR_SDMMC);
	if (!KernelIoControl(IOCTL_HAL_ENABLE_GPIO, &cfg, sizeof(cfg), 
		NULL, 0, &dwTransferred)) {
		// Error message will be given by caller in CSP
		return SYSINTR_UNDEFINED;
	}

	// Request sysintr for card detect interrupt
	irq = BSP_SDHC_DET_IRQ;
	if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &irq, sizeof(DWORD),
		&dwSysIntrCardDetect, sizeof(DWORD), NULL)) {
		// Error message will be given by caller in CSP
		return SYSINTR_UNDEFINED;
	}
	
	return dwSysIntrCardDetect;
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcCardDetectDeinit
//
// This function is to clean up preiously configured detect interrupt.
//
// Parameters:
//		dwSysIntrCardDetect
//			[in] SYSINTR of card detect.
//
// Returns:
//		TRUE indicates success. FALSE indicates failure.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcCardDetectDeinit(DWORD dwSysIntrCardDetect)
{
	DWORD dwTransferred;
	GPIO_IOCTL_CFG cfg;
	BOOL rc = TRUE;

	// Release card detection SYSINTR
	if (dwSysIntrCardDetect != SYSINTR_UNDEFINED) {
		InterruptDisable(dwSysIntrCardDetect);
		if(!KernelIoControl(IOCTL_HAL_RELEASE_SYSINTR, &dwSysIntrCardDetect, 
			sizeof(DWORD), NULL, 0, NULL)) {
			// Error message will be given by caller in CSP
			rc = FALSE;
		}
	}

	// Release GPIO pin use for card detect interrupt
	GPIO_SET_IOCTL_STRUCT(cfg, INTR_SDMMC);
	if (!KernelIoControl(IOCTL_HAL_DISABLE_GPIO, &cfg, sizeof(cfg), 
		NULL, 0, &dwTransferred)) {
		// Error message will be given by caller in CSP
		rc = FALSE;
	}

	return rc;
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcSetcardDetectType
//
// This function is to setup iomux, gpio.
//
// Parameters:
//		type
//			[in] Detction type (insertion or removal).
//
// Returns:
//		None.
//
//------------------------------------------------------------------------------
void BSPSdhcSetcardDetectType(UINT8 type)
{
	GPIO_INT_TYPE rc;
		
	if (type == CARD_DETECT_INSERTION) {
		rc = DDKSetGpioInterruptType(g_GpioHandle, GPIO_INTR_SDMMC_PORT, 
			GPIO_INTR_SDMMC_PIN, GPIO_INTR_SDMMC_INS_TYPE);		
	} else {
		if (type == CARD_DETECT_REMOVAL)
			rc = DDKSetGpioInterruptType(g_GpioHandle, GPIO_INTR_SDMMC_PORT, 
				GPIO_INTR_SDMMC_PIN, GPIO_INTR_SDMMC_RMV_TYPE);
	}

	DEBUGCHK(rc != GPIO_INT_TYPE_MAX);
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcIsCardPresent
//
// This function is to check card existence.
//
// Parameters:
//		None.
//
// Returns:
//		TRUE indicates card is present; otherwize return FALSE.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcIsCardPresent(void)
{
	INT8 rc;

	rc = DDKGetGpioSignalState(g_GpioHandle, GPIO_INTR_SDMMC_PORT,
		GPIO_INTR_SDMMC_PIN);
	
	DEBUGCHK(rc != -1);
	
	return (!rc);
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcIsCardBusy
//
// This function is to check card busy or not.
//
// Parameters:
//		None.
//
// Returns:
//		TRUE indicates card is busy; otherwize return FALSE.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcIsCardBusy(void)
{
	INT8 rc;

	rc = DDKGetGpioSignalState(g_GpioHandle, GPIO_SDHC1_PORT,
		GPIO_SDHC1_DAT0_PIN);
	
	DEBUGCHK(rc != -1);
		
	return (!rc);
}

//------------------------------------------------------------------------------
//
// Function: BSPSdhcIsCardWriteProtected
//
// This function is to check whether card is write protected. 
//
// Parameters:
//		None.
//
// Returns:
//		TRUE if card is write protected;
//		FALSE if card is not write protected.
//
//------------------------------------------------------------------------------
BOOL BSPSdhcIsCardWriteProtected(void)
{
	if (EXTREG16BF(&g_pEioReg->IO0_H, EIO_IO0_H_SD_WP) == EIO_IO0_H_SD_WP_UNLOCK)
		return FALSE;
	else
		return TRUE;
}

⌨️ 快捷键说明

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