1 /******************************************************************************
3 * Copyright (C) 2013 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
35 * @file xsdps_options.c
37 * Contains API's for changing the various options in host and card.
38 * See xsdps.h for a detailed description of the device and driver.
41 * MODIFICATION HISTORY:
43 * Ver Who Date Changes
44 * ----- --- -------- -----------------------------------------------
45 * 1.00a hk/sg 10/17/13 Initial release
46 * 2.1 hk 04/18/14 Increase sleep for eMMC switch command.
47 * Add sleep for microblaze designs. CR# 781117.
48 * 2.3 sk 09/23/14 Use XSdPs_Change_ClkFreq API whenever changing
53 ******************************************************************************/
55 /***************************** Include Files *********************************/
58 * The header sleep.h and API usleep() can only be used with an arm design.
59 * MB_Sleep() is used for microblaze design.
69 #include "microblaze_sleep.h"
73 /************************** Constant Definitions *****************************/
74 #define XSDPS_SCR_BLKCNT 1
75 #define XSDPS_SCR_BLKSIZE 8
76 #define XSDPS_4_BIT_WIDTH 0x2
77 #define XSDPS_SWITCH_CMD_BLKCNT 1
78 #define XSDPS_SWITCH_CMD_BLKSIZE 64
79 #define XSDPS_SWITCH_CMD_HS_GET 0x00FFFFF0
80 #define XSDPS_SWITCH_CMD_HS_SET 0x80FFFFF1
81 #define XSDPS_EXT_CSD_CMD_BLKCNT 1
82 #define XSDPS_EXT_CSD_CMD_BLKSIZE 512
83 #define XSDPS_CLK_52_MHZ 52000000
84 #define XSDPS_MMC_HIGH_SPEED_ARG 0x03B90100
85 #define XSDPS_MMC_4_BIT_BUS_ARG 0x03B70100
86 #define XSDPS_MMC_DELAY_FOR_SWITCH 2000
88 /**************************** Type Definitions *******************************/
90 /***************** Macros (Inline Functions) Definitions *********************/
92 /************************** Function Prototypes ******************************/
93 int XSdPs_CmdTransfer(XSdPs *InstancePtr, u32 Cmd, u32 Arg, u32 BlkCnt);
94 void XSdPs_SetupADMA2DescTbl(XSdPs *InstancePtr, u32 BlkCnt, const u8 *Buff);
96 /*****************************************************************************/
98 * Update Block size for read/write operations.
100 * @param InstancePtr is a pointer to the instance to be worked on.
101 * @param BlkSize - Block size passed by the user.
105 ******************************************************************************/
106 int XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize)
109 u32 PresentStateReg = 0;
111 Xil_AssertNonvoid(InstancePtr != NULL);
112 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
114 PresentStateReg = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
115 XSDPS_PRES_STATE_OFFSET);
117 if (PresentStateReg & (XSDPS_PSR_INHIBIT_CMD_MASK |
118 XSDPS_PSR_INHIBIT_DAT_MASK |
119 XSDPS_PSR_WR_ACTIVE_MASK | XSDPS_PSR_RD_ACTIVE_MASK)) {
120 Status = XST_FAILURE;
126 * Send block write command
128 Status = XSdPs_CmdTransfer(InstancePtr, CMD16, BlkSize, 0);
129 if (Status != XST_SUCCESS) {
130 Status = XST_FAILURE;
134 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
138 * Set block size to the value passed
140 BlkSize &= XSDPS_BLK_SIZE_MASK;
141 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress, XSDPS_BLK_SIZE_OFFSET,
144 Status = XST_SUCCESS;
151 /*****************************************************************************/
154 * API to get bus width support by card.
157 * @param InstancePtr is a pointer to the XSdPs instance.
158 * @param SCR - buffer to store SCR register returned by card.
161 * - XST_SUCCESS if successful.
162 * - XST_FAILURE if fail.
166 ******************************************************************************/
167 int XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR)
175 Xil_AssertNonvoid(InstancePtr != NULL);
176 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
178 for (LoopCnt = 0; LoopCnt < 8; LoopCnt++) {
183 * Send block write command
185 Status = XSdPs_CmdTransfer(InstancePtr, CMD55,
186 InstancePtr->RelCardAddr, 0);
187 if (Status != XST_SUCCESS) {
188 Status = XST_FAILURE;
192 BlkCnt = XSDPS_SCR_BLKCNT;
193 BlkSize = XSDPS_SCR_BLKSIZE;
196 * Set block size to the value passed
198 BlkSize &= XSDPS_BLK_SIZE_MASK;
199 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
200 XSDPS_BLK_SIZE_OFFSET, BlkSize);
202 XSdPs_SetupADMA2DescTbl(InstancePtr, BlkCnt, SCR);
204 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
205 XSDPS_XFER_MODE_OFFSET,
206 XSDPS_TM_DAT_DIR_SEL_MASK | XSDPS_TM_DMA_EN_MASK);
208 Xil_DCacheInvalidateRange(SCR, 8);
210 Status = XSdPs_CmdTransfer(InstancePtr, ACMD51, 0, BlkCnt);
211 if (Status != XST_SUCCESS) {
212 Status = XST_FAILURE;
217 * Check for transfer complete
218 * Polling for response for now
221 StatusReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
222 XSDPS_NORM_INTR_STS_OFFSET);
223 if (StatusReg & XSDPS_INTR_ERR_MASK) {
225 * Write to clear error bits
227 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
228 XSDPS_ERR_INTR_STS_OFFSET,
229 XSDPS_ERROR_INTR_ALL_MASK);
230 Status = XST_FAILURE;
233 } while ((StatusReg & XSDPS_INTR_TC_MASK) == 0);
238 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
239 XSDPS_NORM_INTR_STS_OFFSET, XSDPS_INTR_TC_MASK);
241 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
244 Status = XST_SUCCESS;
251 /*****************************************************************************/
254 * API to set bus width to 4-bit in card and host
257 * @param InstancePtr is a pointer to the XSdPs instance.
260 * - XST_SUCCESS if successful.
261 * - XST_FAILURE if fail.
265 ******************************************************************************/
266 int XSdPs_Change_BusWidth(XSdPs *InstancePtr)
272 Xil_AssertNonvoid(InstancePtr != NULL);
273 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
277 Status = XSdPs_CmdTransfer(InstancePtr, CMD55,
278 InstancePtr->RelCardAddr, 0);
279 if (Status != XST_SUCCESS) {
280 Status = XST_FAILURE;
284 Arg = XSDPS_4_BIT_WIDTH;
285 Status = XSdPs_CmdTransfer(InstancePtr, ACMD6, Arg, 0);
286 if (Status != XST_SUCCESS) {
287 Status = XST_FAILURE;
291 StatusReg = XSdPs_ReadReg8(InstancePtr->Config.BaseAddress,
292 XSDPS_HOST_CTRL1_OFFSET);
293 StatusReg |= XSDPS_HC_WIDTH_MASK;
294 XSdPs_WriteReg8(InstancePtr->Config.BaseAddress,
295 XSDPS_HOST_CTRL1_OFFSET,StatusReg);
297 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
302 Arg = XSDPS_MMC_4_BIT_BUS_ARG;
303 Status = XSdPs_CmdTransfer(InstancePtr, ACMD6, Arg, 0);
304 if (Status != XST_SUCCESS) {
305 Status = XST_FAILURE;
311 usleep(XSDPS_MMC_DELAY_FOR_SWITCH);
315 #ifdef __MICROBLAZE__
322 StatusReg = XSdPs_ReadReg8(InstancePtr->Config.BaseAddress,
323 XSDPS_HOST_CTRL1_OFFSET);
324 StatusReg |= XSDPS_HC_WIDTH_MASK;
325 XSdPs_WriteReg8(InstancePtr->Config.BaseAddress,
326 XSDPS_HOST_CTRL1_OFFSET,StatusReg);
328 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
333 Status = XST_SUCCESS;
340 /*****************************************************************************/
343 * API to get bus speed supported by card.
346 * @param InstancePtr is a pointer to the XSdPs instance.
347 * @param ReadBuff - buffer to store function group support data
351 * - XST_SUCCESS if successful.
352 * - XST_FAILURE if fail.
356 ******************************************************************************/
357 int XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff)
366 Xil_AssertNonvoid(InstancePtr != NULL);
367 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
369 for (LoopCnt = 0; LoopCnt < 64; LoopCnt++) {
370 ReadBuff[LoopCnt] = 0;
373 BlkCnt = XSDPS_SWITCH_CMD_BLKCNT;
374 BlkSize = XSDPS_SWITCH_CMD_BLKSIZE;
375 BlkSize &= XSDPS_BLK_SIZE_MASK;
376 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
377 XSDPS_BLK_SIZE_OFFSET, BlkSize);
379 XSdPs_SetupADMA2DescTbl(InstancePtr, BlkCnt, ReadBuff);
381 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
382 XSDPS_XFER_MODE_OFFSET,
383 XSDPS_TM_DAT_DIR_SEL_MASK | XSDPS_TM_DMA_EN_MASK);
385 Arg = XSDPS_SWITCH_CMD_HS_GET;
387 Xil_DCacheInvalidateRange(ReadBuff, 64);
389 Status = XSdPs_CmdTransfer(InstancePtr, CMD6, Arg, 1);
390 if (Status != XST_SUCCESS) {
391 Status = XST_FAILURE;
396 * Check for transfer complete
397 * Polling for response for now
400 StatusReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
401 XSDPS_NORM_INTR_STS_OFFSET);
402 if (StatusReg & XSDPS_INTR_ERR_MASK) {
404 * Write to clear error bits
406 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
407 XSDPS_ERR_INTR_STS_OFFSET,
408 XSDPS_ERROR_INTR_ALL_MASK);
409 Status = XST_FAILURE;
412 } while ((StatusReg & XSDPS_INTR_TC_MASK) == 0);
417 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
418 XSDPS_NORM_INTR_STS_OFFSET, XSDPS_INTR_TC_MASK);
420 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
423 Status = XST_SUCCESS;
430 /*****************************************************************************/
433 * API to set high speed in card and host. Changes clock in host accordingly.
436 * @param InstancePtr is a pointer to the XSdPs instance.
439 * - XST_SUCCESS if successful.
440 * - XST_FAILURE if fail.
444 ******************************************************************************/
445 int XSdPs_Change_BusSpeed(XSdPs *InstancePtr)
458 Xil_AssertNonvoid(InstancePtr != NULL);
459 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
463 BlkCnt = XSDPS_SWITCH_CMD_BLKCNT;
464 BlkSize = XSDPS_SWITCH_CMD_BLKSIZE;
465 BlkSize &= XSDPS_BLK_SIZE_MASK;
466 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
467 XSDPS_BLK_SIZE_OFFSET, BlkSize);
469 XSdPs_SetupADMA2DescTbl(InstancePtr, BlkCnt, ReadBuff);
471 Xil_DCacheInvalidateRange(ReadBuff, 64);
473 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
474 XSDPS_XFER_MODE_OFFSET,
475 XSDPS_TM_DAT_DIR_SEL_MASK | XSDPS_TM_DMA_EN_MASK);
477 Arg = XSDPS_SWITCH_CMD_HS_SET;
478 Status = XSdPs_CmdTransfer(InstancePtr, CMD6, Arg, 1);
479 if (Status != XST_SUCCESS) {
480 Status = XST_FAILURE;
485 * Check for transfer complete
486 * Polling for response for now
489 StatusReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
490 XSDPS_NORM_INTR_STS_OFFSET);
491 if (StatusReg & XSDPS_INTR_ERR_MASK) {
493 * Write to clear error bits
495 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
496 XSDPS_ERR_INTR_STS_OFFSET,
497 XSDPS_ERROR_INTR_ALL_MASK);
498 Status = XST_FAILURE;
501 } while ((StatusReg & XSDPS_INTR_TC_MASK) == 0);
506 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
507 XSDPS_NORM_INTR_STS_OFFSET, XSDPS_INTR_TC_MASK);
510 * Change the clock frequency to 50 MHz
512 Status = XSdPs_Change_ClkFreq(InstancePtr, XSDPS_CLK_50_MHZ);
513 if (Status != XST_SUCCESS) {
514 Status = XST_FAILURE;
518 StatusReg = XSdPs_ReadReg8(InstancePtr->Config.BaseAddress,
519 XSDPS_HOST_CTRL1_OFFSET);
520 StatusReg |= XSDPS_HC_SPEED_MASK;
521 XSdPs_WriteReg8(InstancePtr->Config.BaseAddress,
522 XSDPS_HOST_CTRL1_OFFSET,StatusReg);
524 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
529 Arg = XSDPS_MMC_HIGH_SPEED_ARG;
530 Status = XSdPs_CmdTransfer(InstancePtr, CMD6, Arg, 0);
531 if (Status != XST_SUCCESS) {
532 Status = XST_FAILURE;
538 usleep(XSDPS_MMC_DELAY_FOR_SWITCH);
542 #ifdef __MICROBLAZE__
549 XSdPs_Change_ClkFreq(InstancePtr, XSDPS_CLK_52_MHZ);
551 StatusReg = XSdPs_ReadReg8(InstancePtr->Config.BaseAddress,
552 XSDPS_HOST_CTRL1_OFFSET);
553 StatusReg |= XSDPS_HC_SPEED_MASK;
554 XSdPs_WriteReg8(InstancePtr->Config.BaseAddress,
555 XSDPS_HOST_CTRL1_OFFSET,StatusReg);
557 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
561 Status = XST_SUCCESS;
568 /*****************************************************************************/
571 * API to change clock freq to given value.
574 * @param InstancePtr is a pointer to the XSdPs instance.
575 * @param SelFreq - Clock frequency in Hz.
579 * @note This API will change clock frequency to the value less than
580 * or equal to the given value using the permissible dividors.
582 ******************************************************************************/
583 int XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq)
591 Xil_AssertNonvoid(InstancePtr != NULL);
592 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
597 ClockReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
598 XSDPS_CLK_CTRL_OFFSET);
599 ClockReg &= ~(XSDPS_CC_INT_CLK_EN_MASK | XSDPS_CC_SD_CLK_EN_MASK);
601 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
602 XSDPS_CLK_CTRL_OFFSET, ClockReg);
608 for(ClkLoopCnt = 0; ClkLoopCnt < XSDPS_CC_MAX_NUM_OF_DIV;
610 if( ((InstancePtr->Config.InputClockHz)/DivCnt) <= SelFreq) {
612 Divisor = Divisor << XSDPS_CC_DIV_SHIFT;
615 DivCnt = DivCnt << 1;
618 if(ClkLoopCnt == 9) {
621 * No valid divisor found for given frequency
623 Status = XST_FAILURE;
630 ClockReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
631 XSDPS_CLK_CTRL_OFFSET);
632 ClockReg &= (~XSDPS_CC_SDCLK_FREQ_SEL_MASK);
634 ClockReg |= Divisor | XSDPS_CC_INT_CLK_EN_MASK;
635 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
636 XSDPS_CLK_CTRL_OFFSET, ClockReg);
639 * Wait for internal clock to stabilize
641 while((XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
642 XSDPS_CLK_CTRL_OFFSET) & XSDPS_CC_INT_CLK_STABLE_MASK) == 0);
647 ClockReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
648 XSDPS_CLK_CTRL_OFFSET);
649 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
650 XSDPS_CLK_CTRL_OFFSET,
651 ClockReg | XSDPS_CC_SD_CLK_EN_MASK);
653 Status = XST_SUCCESS;
660 /*****************************************************************************/
663 * API to send pullup command to card before using DAT line 3(using 4-bit bus)
666 * @param InstancePtr is a pointer to the XSdPs instance.
669 * - XST_SUCCESS if successful.
670 * - XST_FAILURE if fail.
674 ******************************************************************************/
675 int XSdPs_Pullup(XSdPs *InstancePtr)
679 Xil_AssertNonvoid(InstancePtr != NULL);
680 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
682 Status = XSdPs_CmdTransfer(InstancePtr, CMD55,
683 InstancePtr->RelCardAddr, 0);
684 if (Status != XST_SUCCESS) {
685 Status = XST_FAILURE;
689 Status = XSdPs_CmdTransfer(InstancePtr, ACMD42, 0, 0);
690 if (Status != XST_SUCCESS) {
691 Status = XST_FAILURE;
695 Status = XST_SUCCESS;
702 /*****************************************************************************/
705 * API to get EXT_CSD register of eMMC.
708 * @param InstancePtr is a pointer to the XSdPs instance.
709 * @param ReadBuff - buffer to store EXT_CSD
712 * - XST_SUCCESS if successful.
713 * - XST_FAILURE if fail.
717 ******************************************************************************/
718 int XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff)
727 Xil_AssertNonvoid(InstancePtr != NULL);
728 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
730 for (LoopCnt = 0; LoopCnt < 512; LoopCnt++) {
731 ReadBuff[LoopCnt] = 0;
734 BlkCnt = XSDPS_EXT_CSD_CMD_BLKCNT;
735 BlkSize = XSDPS_EXT_CSD_CMD_BLKSIZE;
736 BlkSize &= XSDPS_BLK_SIZE_MASK;
737 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
738 XSDPS_BLK_SIZE_OFFSET, BlkSize);
740 XSdPs_SetupADMA2DescTbl(InstancePtr, BlkCnt, ReadBuff);
742 Xil_DCacheInvalidateRange(ReadBuff, 512);
744 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
745 XSDPS_XFER_MODE_OFFSET,
746 XSDPS_TM_DAT_DIR_SEL_MASK | XSDPS_TM_DMA_EN_MASK);
751 * Send SEND_EXT_CSD command
753 Status = XSdPs_CmdTransfer(InstancePtr, CMD8, Arg, 1);
754 if (Status != XST_SUCCESS) {
755 Status = XST_FAILURE;
760 * Check for transfer complete
761 * Polling for response for now
764 StatusReg = XSdPs_ReadReg16(InstancePtr->Config.BaseAddress,
765 XSDPS_NORM_INTR_STS_OFFSET);
766 if (StatusReg & XSDPS_INTR_ERR_MASK) {
768 * Write to clear error bits
770 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
771 XSDPS_ERR_INTR_STS_OFFSET,
772 XSDPS_ERROR_INTR_ALL_MASK);
773 Status = XST_FAILURE;
776 } while ((StatusReg & XSDPS_INTR_TC_MASK) == 0);
781 XSdPs_WriteReg16(InstancePtr->Config.BaseAddress,
782 XSDPS_NORM_INTR_STS_OFFSET, XSDPS_INTR_TC_MASK);
784 Status = XSdPs_ReadReg(InstancePtr->Config.BaseAddress,
787 Status = XST_SUCCESS;