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 /*****************************************************************************/
36 * @addtogroup sdps_v2_5
40 * This file contains the implementation of XSdPs driver.
41 * This driver is used initialize read from and write to the SD card.
42 * Features such as switching bus width to 4-bit and switching to high speed,
43 * changing clock frequency, block size etc. are supported.
44 * SD 2.0 uses 1/4 bus width and speeds of 25/50KHz. Initialization, however
45 * is done using 1-bit bus width and 400KHz clock frequency.
46 * SD commands are classified as broadcast and addressed. Commands can be
47 * those with response only (using only command line) or
48 * response + data (using command and data lines).
49 * Only one command can be sent at a time. During a data transfer however,
50 * when dsta lines are in use, certain commands (which use only the command
51 * line) can be sent, most often to obtain status.
52 * This driver does not support multi card slots at present.
55 * This includes initialization on the host controller side to select
56 * clock frequency, bus power and default transfer related parameters.
57 * The default voltage is 3.3V.
58 * On the SD card side, the initialization and identification state diagram is
59 * implemented. This resets the card, gives it a unique address/ID and
60 * identifies key card related specifications.
63 * The SD card is put in tranfer state to read from or write to it.
64 * The default block size is 512 bytes and if supported,
65 * default bus width is 4-bit and bus speed is High speed.
66 * The read and write functions are implemented in polled mode using ADMA2.
68 * At any point, when key parameters such as block size or
69 * clock/speed or bus width are modified, this driver takes care of
70 * maintaining the same selection on host and card.
71 * All error bits in host controller are monitored by the driver and in the
72 * event one of them is set, driver will clear the interrupt status and
73 * communicate failure to the upper layer.
76 * This driver can be used with xilffs library to read and write files to SD.
77 * (Please refer to procedure in diskio.c). The file system read/write example
78 * in polled mode can used for reference.
80 * There is no example for using SD driver without file system at present.
81 * However, the driver can be used without the file system. The glue layer
82 * in filesytem can be used as reference for the same. The block count
83 * passed to the read/write function in one call is limited by the ADMA2
84 * descriptor table and hence care will have to be taken to call read/write
85 * API's in a loop for large file sizes.
87 * Interrupt mode is not supported because it offers no improvement when used
91 * SD driver supports SD and eMMC based on the "enable MMC" parameter in SDK.
92 * The features of eMMC supported by the driver will depend on those supported
93 * by the host controller. The current driver supports read/write on eMMC card
94 * using 4-bit and high speed mode currently.
96 * Features not supported include - card write protect, password setting,
97 * lock/unlock, interrupts, SDMA mode, programmed I/O mode and
98 * 64-bit addressed ADMA2, erase/pre-erase commands.
101 * MODIFICATION HISTORY:
103 * Ver Who Date Changes
104 * ----- --- -------- -----------------------------------------------
105 * 1.00a hk/sg 10/17/13 Initial release
106 * 2.0 hk 03/07/14 Version number revised.
107 * 2.1 hk 04/18/14 Increase sleep for eMMC switch command.
108 * Add sleep for microblaze designs. CR# 781117.
109 * 2.2 hk 07/28/14 Make changes to enable use of data cache.
110 * 2.3 sk 09/23/14 Send command for relative card address
111 * when re-initialization is done.CR# 819614.
112 * Use XSdPs_Change_ClkFreq API whenever changing
114 * 2.4 sk 12/04/14 Added support for micro SD without
116 * Checked for DAT Inhibit mask instead of CMD
117 * Inhibit mask in Cmd Transfer API.
118 * Added Support for SD Card v1.0
119 * 2.5 sg 07/09/15 Added SD 3.0 features
120 * kvn 07/15/15 Modified the code according to MISRAC-2012.
121 * 2.6 sk 10/12/15 Added support for SD card v1.0 CR# 840601.
122 * 2.7 sk 11/24/15 Considered the slot type befoe checking CD/WP pins.
123 * sk 12/10/15 Added support for MMC cards.
124 * 01/08/16 Added workaround for issue in auto tuning mode
125 * of SDR50, SDR104 and HS200.
126 * sk 02/16/16 Corrected the Tuning logic.
127 * sk 03/01/16 Removed Bus Width check for eMMC. CR# 938311.
131 ******************************************************************************/
141 #include "xil_printf.h"
142 #include "xil_cache.h"
144 #include "xsdps_hw.h"
147 /************************** Constant Definitions *****************************/
149 #define XSDPS_CT_ERROR 0x2U /**< Command timeout flag */
150 #define MAX_TUNING_COUNT 40U /**< Maximum Tuning count */
152 /**************************** Type Definitions *******************************/
154 * This typedef contains configuration information for the device.
157 u16 DeviceId; /**< Unique ID of device */
158 u32 BaseAddress; /**< Base address of the device */
159 u32 InputClockHz; /**< Input clock frequency */
160 u32 CardDetect; /**< Card Detect */
161 u32 WriteProtect; /**< Write Protect */
164 /* ADMA2 descriptor table */
166 u16 Attribute; /**< Attributes of descriptor */
167 u16 Length; /**< Length of current dma transfer */
168 u32 Address; /**< Address of current dma transfer */
169 } XSdPs_Adma2Descriptor;
172 * The XSdPs driver instance data. The user is required to allocate a
173 * variable of this type for every SD device in the system. A pointer
174 * to a variable of this type is then passed to the driver API functions.
177 XSdPs_Config Config; /**< Configuration structure */
178 u32 IsReady; /**< Device is initialized and ready */
179 u32 Host_Caps; /**< Capabilities of host controller */
180 u32 Host_CapsExt; /**< Extended Capabilities */
181 u32 HCS; /**< High capacity support in card */
182 u8 CardType; /**< Type of card - SD/MMC/eMMC */
183 u8 Card_Version; /**< Card version */
184 u8 HC_Version; /**< Host controller version */
185 u8 BusWidth; /**< Current operating bus width */
186 u32 BusSpeed; /**< Current operating bus speed */
187 u8 Switch1v8; /**< 1.8V Switch support */
188 u32 CardID[4]; /**< Card ID Register */
189 u32 RelCardAddr; /**< Relative Card Address */
190 u32 CardSpecData[4]; /**< Card Specific Data Register */
191 u32 SdCardConfig; /**< Sd Card Configuration Register */
192 /**< ADMA Descriptors */
194 #pragma data_alignment = 32
195 XSdPs_Adma2Descriptor Adma2_DescrTbl[32];
196 #pragma data_alignment = 4
198 XSdPs_Adma2Descriptor Adma2_DescrTbl[32] __attribute__ ((aligned(32)));
202 /***************** Macros (Inline Functions) Definitions *********************/
204 /************************** Function Prototypes ******************************/
205 XSdPs_Config *XSdPs_LookupConfig(u16 DeviceId);
206 s32 XSdPs_CfgInitialize(XSdPs *InstancePtr, XSdPs_Config *ConfigPtr,
208 s32 XSdPs_SdCardInitialize(XSdPs *InstancePtr);
209 s32 XSdPs_ReadPolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, u8 *Buff);
210 s32 XSdPs_WritePolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, const u8 *Buff);
211 s32 XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize);
212 s32 XSdPs_Select_Card (XSdPs *InstancePtr);
213 s32 XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq);
214 s32 XSdPs_Change_BusWidth(XSdPs *InstancePtr);
215 s32 XSdPs_Change_BusSpeed(XSdPs *InstancePtr);
216 s32 XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR);
217 s32 XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff);
218 s32 XSdPs_Pullup(XSdPs *InstancePtr);
219 s32 XSdPs_MmcCardInitialize(XSdPs *InstancePtr);
220 s32 XSdPs_CardInitialize(XSdPs *InstancePtr);
221 s32 XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff);