lib/sw_services/xilloader/src/xloader_sd.c
int XLoader_RawInit(u32 DeviceFlags)
{
	int Status = XST_FAILURE;
	u8 PdiSrc = (u8)(DeviceFlags & XLOADER_PDISRC_FLAGS_MASK);
	u8 DrvNum = XLoader_GetDrvNumSD(PdiSrc);
	XSdPs_Config *SdConfig;
	u32 CapSecureAccess = (u32)PM_CAP_ACCESS | (u32)PM_CAP_SECURE;
	u32 ErrorCode;
	u32 SdRawBootVal = DeviceFlags & XLOADER_SD_RAWBOOT_MASK;
	u32 MmcConfig;

	Status = XPlmi_MemSetBytes(&SdInstance, sizeof(SdInstance),
				0U, sizeof(SdInstance));
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_MEMSET,
			(int)XLOADER_ERR_MEMSET_SD_INSTANCE);
		goto END;
	}
	if ((PdiSrc == XLOADER_PDI_SRC_SD0) ||
		(PdiSrc == XLOADER_PDI_SRC_EMMC0)) {
		SdDeviceNode = PM_DEV_SDIO_0;
		SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0;
	}
	else {
		SdDeviceNode = PM_DEV_SDIO_1;
		SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1;
	}

	Status = XPm_RequestDevice(PM_SUBSYS_PMC, SdDeviceNode,
		CapSecureAccess, XPM_DEF_QOS, 0U, XPLMI_CMD_SECURE);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(ErrorCode, Status);
		goto END;
	}
	SdCdnVal = XPlmi_In32(SdCdnReg);
	XPlmi_Out32(SdCdnReg, PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK);

	if (DrvNum != XLOADER_SD_DRV_NUM_0) {
		DrvNum = XLOADER_SD_DRV_NUM_1;
	}

	/*
	 * Initialize the host controller
	 */
	SdConfig = XSdPs_LookupConfig(DrvNum);
	if (NULL == SdConfig) {
		XLoader_Printf(DEBUG_GENERAL,"RAW Lookup config failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_LOOKUP, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}

	Status = XSdPs_CfgInitialize(&SdInstance, SdConfig,
				SdConfig->BaseAddress);
	if (Status != XST_SUCCESS) {
		XLoader_Printf(DEBUG_GENERAL, "RAW Config init failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_CFG, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}

	Status = XSdPs_CardInitialize(&SdInstance);
	if (Status != XST_SUCCESS) {
		XLoader_Printf(DEBUG_GENERAL, "RAW SD Card init failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_CARD_INIT, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}

	if ((PdiSrc != XLOADER_PDI_SRC_EMMC1) &&
		(PdiSrc != XLOADER_PDI_SRC_EMMC0)) {
		goto END1;
	}

	if (SdRawBootVal == XLOADER_SD_RAWBOOT_VAL) {
		MmcConfig = XSDPS_MMC_PART_CFG_0_ARG;
	}
	else if (SdRawBootVal == XLOADER_EMMC_BP1_RAW_VAL) {
		MmcConfig = XSDPS_MMC_PART_CFG_1_ARG;
	}
	else if (SdRawBootVal == XLOADER_EMMC_BP2_RAW_VAL)
	{
		MmcConfig = XSDPS_MMC_PART_CFG_2_ARG;
	}
	else {
		goto END1;
	}
	Status = XSdPs_Set_Mmc_ExtCsd(&SdInstance, MmcConfig);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(
					XLOADER_ERR_MMC_PART_CONFIG, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}

END1:
	XLoader_Printf(DEBUG_INFO,"Raw init completed\n\r");
END:
	return Status;
}
/*****************************************************************************/
/**
 * @brief	This function is used to initialize the sd controller and driver.
 * It is only called in raw boot mode.
 *
 * @param	DeviceFlags contains the boot mode information
 *
 * @return	XST_SUCCESS on success and error code on failure
 *
 *****************************************************************************/
@brief       This function is used to initialize the sd controller and driver.
It is only called in raw boot mode.

@param       DeviceFlags contains the boot mode information

@return      XST_SUCCESS on success and error code on failure
@brief This function is used to initialize the sd controller and driver . It is only called in raw boot mode . @param DeviceFlags contains the boot mode information @return XST_SUCCESS on success and error code on failure
int XLoader_RawInit(u32 DeviceFlags)
{
	int Status = XST_FAILURE;
	u8 PdiSrc = (u8)(DeviceFlags & XLOADER_PDISRC_FLAGS_MASK);
	u8 DrvNum = XLoader_GetDrvNumSD(PdiSrc);
	XSdPs_Config *SdConfig;
	u32 CapSecureAccess = (u32)PM_CAP_ACCESS | (u32)PM_CAP_SECURE;
	u32 ErrorCode;
	u32 SdRawBootVal = DeviceFlags & XLOADER_SD_RAWBOOT_MASK;
	u32 MmcConfig;
	Status = XPlmi_MemSetBytes(&SdInstance, sizeof(SdInstance),
				0U, sizeof(SdInstance));
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(XLOADER_ERR_MEMSET,
			(int)XLOADER_ERR_MEMSET_SD_INSTANCE);
		goto END;
	}
	if ((PdiSrc == XLOADER_PDI_SRC_SD0) ||
		(PdiSrc == XLOADER_PDI_SRC_EMMC0)) {
		SdDeviceNode = PM_DEV_SDIO_0;
		SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0;
	}
	else {
		SdDeviceNode = PM_DEV_SDIO_1;
		SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL;
		ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1;
	}
	Status = XPm_RequestDevice(PM_SUBSYS_PMC, SdDeviceNode,
		CapSecureAccess, XPM_DEF_QOS, 0U, XPLMI_CMD_SECURE);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(ErrorCode, Status);
		goto END;
	}
	SdCdnVal = XPlmi_In32(SdCdnReg);
	XPlmi_Out32(SdCdnReg, PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK);
	if (DrvNum != XLOADER_SD_DRV_NUM_0) {
		DrvNum = XLOADER_SD_DRV_NUM_1;
	}
	SdConfig = XSdPs_LookupConfig(DrvNum);
	if (NULL == SdConfig) {
		XLoader_Printf(DEBUG_GENERAL,"RAW Lookup config failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_LOOKUP, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}
	Status = XSdPs_CfgInitialize(&SdInstance, SdConfig,
				SdConfig->BaseAddress);
	if (Status != XST_SUCCESS) {
		XLoader_Printf(DEBUG_GENERAL, "RAW Config init failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_CFG, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}
	Status = XSdPs_CardInitialize(&SdInstance);
	if (Status != XST_SUCCESS) {
		XLoader_Printf(DEBUG_GENERAL, "RAW SD Card init failed\r\n");
		Status = XPlmi_UpdateStatus(XLOADER_ERR_SD_CARD_INIT, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}
	if ((PdiSrc != XLOADER_PDI_SRC_EMMC1) &&
		(PdiSrc != XLOADER_PDI_SRC_EMMC0)) {
		goto END1;
	}
	if (SdRawBootVal == XLOADER_SD_RAWBOOT_VAL) {
		MmcConfig = XSDPS_MMC_PART_CFG_0_ARG;
	}
	else if (SdRawBootVal == XLOADER_EMMC_BP1_RAW_VAL) {
		MmcConfig = XSDPS_MMC_PART_CFG_1_ARG;
	}
	else if (SdRawBootVal == XLOADER_EMMC_BP2_RAW_VAL)
	{
		MmcConfig = XSDPS_MMC_PART_CFG_2_ARG;
	}
	else {
		goto END1;
	}
	Status = XSdPs_Set_Mmc_ExtCsd(&SdInstance, MmcConfig);
	if (Status != XST_SUCCESS) {
		Status = XPlmi_UpdateStatus(
					XLOADER_ERR_MMC_PART_CONFIG, Status);
		XPlmi_Out32(SdCdnReg, SdCdnVal);
		goto END;
	}
END1:
	XLoader_Printf(DEBUG_INFO,"Raw init completed\n\r");
END:
	return Status;
}
int XLoader_RawInit ( u32 DeviceFlags ) { int Status = XST_FAILURE ; u8 PdiSrc = ( u8 ) ( DeviceFlags & XLOADER_PDISRC_FLAGS_MASK ) ; u8 DrvNum = XLoader_GetDrvNumSD ( PdiSrc ) ; XSdPs_Config * SdConfig ; u32 CapSecureAccess = ( u32 ) PM_CAP_ACCESS | ( u32 ) PM_CAP_SECURE ; u32 ErrorCode ; u32 SdRawBootVal = DeviceFlags & XLOADER_SD_RAWBOOT_MASK ; u32 MmcConfig ; Status = XPlmi_MemSetBytes ( & SdInstance , sizeof ( SdInstance ) , 0U , sizeof ( SdInstance ) ) ; if ( Status != XST_SUCCESS ) { Status = XPlmi_UpdateStatus ( XLOADER_ERR_MEMSET , ( int ) XLOADER_ERR_MEMSET_SD_INSTANCE ) ; goto END ; } if ( ( PdiSrc == XLOADER_PDI_SRC_SD0 ) || ( PdiSrc == XLOADER_PDI_SRC_EMMC0 ) ) { SdDeviceNode = PM_DEV_SDIO_0 ; SdCdnReg = PMC_IOU_SLCR_SD0_CDN_CTRL ; ErrorCode = XLOADER_ERR_PM_DEV_SDIO_0 ; } else { SdDeviceNode = PM_DEV_SDIO_1 ; SdCdnReg = PMC_IOU_SLCR_SD1_CDN_CTRL ; ErrorCode = XLOADER_ERR_PM_DEV_SDIO_1 ; } Status = XPm_RequestDevice ( PM_SUBSYS_PMC , SdDeviceNode , CapSecureAccess , XPM_DEF_QOS , 0U , XPLMI_CMD_SECURE ) ; if ( Status != XST_SUCCESS ) { Status = XPlmi_UpdateStatus ( ErrorCode , Status ) ; goto END ; } SdCdnVal = XPlmi_In32 ( SdCdnReg ) ; XPlmi_Out32 ( SdCdnReg , PMC_IOU_SLCR_SD0_CDN_CTRL_SD0_CDN_CTRL_MASK ) ; if ( DrvNum != XLOADER_SD_DRV_NUM_0 ) { DrvNum = XLOADER_SD_DRV_NUM_1 ; } SdConfig = XSdPs_LookupConfig ( DrvNum ) ; if ( NULL == SdConfig ) { XLoader_Printf ( DEBUG_GENERAL , " \r \n " ) ; Status = XPlmi_UpdateStatus ( XLOADER_ERR_SD_LOOKUP , Status ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; goto END ; } Status = XSdPs_CfgInitialize ( & SdInstance , SdConfig , SdConfig -> BaseAddress ) ; if ( Status != XST_SUCCESS ) { XLoader_Printf ( DEBUG_GENERAL , " \r \n " ) ; Status = XPlmi_UpdateStatus ( XLOADER_ERR_SD_CFG , Status ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; goto END ; } Status = XSdPs_CardInitialize ( & SdInstance ) ; if ( Status != XST_SUCCESS ) { XLoader_Printf ( DEBUG_GENERAL , " \r \n " ) ; Status = XPlmi_UpdateStatus ( XLOADER_ERR_SD_CARD_INIT , Status ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; goto END ; } if ( ( PdiSrc != XLOADER_PDI_SRC_EMMC1 ) && ( PdiSrc != XLOADER_PDI_SRC_EMMC0 ) ) { goto END1 ; } if ( SdRawBootVal == XLOADER_SD_RAWBOOT_VAL ) { MmcConfig = XSDPS_MMC_PART_CFG_0_ARG ; } else if ( SdRawBootVal == XLOADER_EMMC_BP1_RAW_VAL ) { MmcConfig = XSDPS_MMC_PART_CFG_1_ARG ; } else if ( SdRawBootVal == XLOADER_EMMC_BP2_RAW_VAL ) { MmcConfig = XSDPS_MMC_PART_CFG_2_ARG ; } else { goto END1 ; } Status = XSdPs_Set_Mmc_ExtCsd ( & SdInstance , MmcConfig ) ; if ( Status != XST_SUCCESS ) { Status = XPlmi_UpdateStatus ( XLOADER_ERR_MMC_PART_CONFIG , Status ) ; XPlmi_Out32 ( SdCdnReg , SdCdnVal ) ; goto END ; } END1 : XLoader_Printf ( DEBUG_INFO , " \n \r " ) ; END : return Status ; }
@brief       This function is used to initialize the sd controller and driver.
@brief This function is used to initialize the sd controller and driver .
/*
	 * Initialize the host controller
	 */