1 /******************************************************************************
\r
3 * Copyright (C) 2013 - 2015 Xilinx, Inc. All rights reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * Use of the Software is limited solely to applications:
\r
16 * (a) running on a Xilinx device, or
\r
17 * (b) that interact with a Xilinx device through a bus or interconnect.
\r
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
\r
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
27 * Except as contained in this notice, the name of the Xilinx shall not be used
\r
28 * in advertising or otherwise to promote the sale, use or other dealings in
\r
29 * this Software without prior written authorization from Xilinx.
\r
31 ******************************************************************************/
\r
32 /*****************************************************************************/
\r
36 * @addtogroup sdps_v2_5
\r
40 * This file contains the implementation of XSdPs driver.
\r
41 * This driver is used initialize read from and write to the SD card.
\r
42 * Features such as switching bus width to 4-bit and switching to high speed,
\r
43 * changing clock frequency, block size etc. are supported.
\r
44 * SD 2.0 uses 1/4 bus width and speeds of 25/50KHz. Initialization, however
\r
45 * is done using 1-bit bus width and 400KHz clock frequency.
\r
46 * SD commands are classified as broadcast and addressed. Commands can be
\r
47 * those with response only (using only command line) or
\r
48 * response + data (using command and data lines).
\r
49 * Only one command can be sent at a time. During a data transfer however,
\r
50 * when dsta lines are in use, certain commands (which use only the command
\r
51 * line) can be sent, most often to obtain status.
\r
52 * This driver does not support multi card slots at present.
\r
55 * This includes initialization on the host controller side to select
\r
56 * clock frequency, bus power and default transfer related parameters.
\r
57 * The default voltage is 3.3V.
\r
58 * On the SD card side, the initialization and identification state diagram is
\r
59 * implemented. This resets the card, gives it a unique address/ID and
\r
60 * identifies key card related specifications.
\r
63 * The SD card is put in tranfer state to read from or write to it.
\r
64 * The default block size is 512 bytes and if supported,
\r
65 * default bus width is 4-bit and bus speed is High speed.
\r
66 * The read and write functions are implemented in polled mode using ADMA2.
\r
68 * At any point, when key parameters such as block size or
\r
69 * clock/speed or bus width are modified, this driver takes care of
\r
70 * maintaining the same selection on host and card.
\r
71 * All error bits in host controller are monitored by the driver and in the
\r
72 * event one of them is set, driver will clear the interrupt status and
\r
73 * communicate failure to the upper layer.
\r
76 * This driver can be used with xilffs library to read and write files to SD.
\r
77 * (Please refer to procedure in diskio.c). The file system read/write example
\r
78 * in polled mode can used for reference.
\r
80 * There is no example for using SD driver without file system at present.
\r
81 * However, the driver can be used without the file system. The glue layer
\r
82 * in filesytem can be used as reference for the same. The block count
\r
83 * passed to the read/write function in one call is limited by the ADMA2
\r
84 * descriptor table and hence care will have to be taken to call read/write
\r
85 * API's in a loop for large file sizes.
\r
87 * Interrupt mode is not supported because it offers no improvement when used
\r
91 * SD driver supports SD and eMMC based on the "enable MMC" parameter in SDK.
\r
92 * The features of eMMC supported by the driver will depend on those supported
\r
93 * by the host controller. The current driver supports read/write on eMMC card
\r
94 * using 4-bit and high speed mode currently.
\r
96 * Features not supported include - card write protect, password setting,
\r
97 * lock/unlock, interrupts, SDMA mode, programmed I/O mode and
\r
98 * 64-bit addressed ADMA2, erase/pre-erase commands.
\r
101 * MODIFICATION HISTORY:
\r
103 * Ver Who Date Changes
\r
104 * ----- --- -------- -----------------------------------------------
\r
105 * 1.00a hk/sg 10/17/13 Initial release
\r
106 * 2.0 hk 03/07/14 Version number revised.
\r
107 * 2.1 hk 04/18/14 Increase sleep for eMMC switch command.
\r
108 * Add sleep for microblaze designs. CR# 781117.
\r
109 * 2.2 hk 07/28/14 Make changes to enable use of data cache.
\r
110 * 2.3 sk 09/23/14 Send command for relative card address
\r
111 * when re-initialization is done.CR# 819614.
\r
112 * Use XSdPs_Change_ClkFreq API whenever changing
\r
113 * clock.CR# 816586.
\r
114 * 2.4 sk 12/04/14 Added support for micro SD without
\r
115 * WP/CD. CR# 810655.
\r
116 * Checked for DAT Inhibit mask instead of CMD
\r
117 * Inhibit mask in Cmd Transfer API.
\r
118 * Added Support for SD Card v1.0
\r
119 * 2.5 sg 07/09/15 Added SD 3.0 features
\r
120 * kvn 07/15/15 Modified the code according to MISRAC-2012.
\r
121 * 2.6 sk 10/12/15 Added support for SD card v1.0 CR# 840601.
\r
125 ******************************************************************************/
\r
135 #include "xstatus.h"
\r
136 #include "xsdps_hw.h"
\r
137 #include <string.h>
\r
139 /************************** Constant Definitions *****************************/
\r
141 #define XSDPS_CT_ERROR 0x2U /**< Command timeout flag */
\r
143 /**************************** Type Definitions *******************************/
\r
145 * This typedef contains configuration information for the device.
\r
148 u16 DeviceId; /**< Unique ID of device */
\r
149 u32 BaseAddress; /**< Base address of the device */
\r
150 u32 InputClockHz; /**< Input clock frequency */
\r
151 u32 CardDetect; /**< Card Detect */
\r
152 u32 WriteProtect; /**< Write Protect */
\r
155 /* ADMA2 descriptor table */
\r
157 u16 Attribute; /**< Attributes of descriptor */
\r
158 u16 Length; /**< Length of current dma transfer */
\r
159 u32 Address; /**< Address of current dma transfer */
\r
160 } XSdPs_Adma2Descriptor;
\r
163 * The XSdPs driver instance data. The user is required to allocate a
\r
164 * variable of this type for every SD device in the system. A pointer
\r
165 * to a variable of this type is then passed to the driver API functions.
\r
168 XSdPs_Config Config; /**< Configuration structure */
\r
169 u32 IsReady; /**< Device is initialized and ready */
\r
170 u32 Host_Caps; /**< Capabilities of host controller */
\r
171 u32 Host_CapsExt; /**< Extended Capabilities */
\r
172 u32 HCS; /**< High capacity support in card */
\r
173 u8 CardType; /**< Type of card - SD/MMC/eMMC */
\r
174 u8 Card_Version; /**< Card version */
\r
175 u8 HC_Version; /**< Host controller version */
\r
176 u8 BusWidth; /**< Current operating bus width */
\r
177 u32 BusSpeed; /**< Current operating bus speed */
\r
178 u8 Switch1v8; /**< 1.8V Switch support */
\r
179 u32 CardID[4]; /**< Card ID Register */
\r
180 u32 RelCardAddr; /**< Relative Card Address */
\r
181 u32 CardSpecData[4]; /**< Card Specific Data Register */
\r
182 u32 SdCardConfig; /**< Sd Card Configuration Register */
\r
183 /**< ADMA Descriptors */
\r
185 #pragma data_alignment = 32
\r
186 XSdPs_Adma2Descriptor Adma2_DescrTbl[32];
\r
187 #pragma data_alignment = 4
\r
189 XSdPs_Adma2Descriptor Adma2_DescrTbl[32] __attribute__ ((aligned(32)));
\r
193 /***************** Macros (Inline Functions) Definitions *********************/
\r
195 /************************** Function Prototypes ******************************/
\r
196 XSdPs_Config *XSdPs_LookupConfig(u16 DeviceId);
\r
197 s32 XSdPs_CfgInitialize(XSdPs *InstancePtr, XSdPs_Config *ConfigPtr,
\r
198 u32 EffectiveAddr);
\r
199 s32 XSdPs_SdCardInitialize(XSdPs *InstancePtr);
\r
200 s32 XSdPs_ReadPolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, u8 *Buff);
\r
201 s32 XSdPs_WritePolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, const u8 *Buff);
\r
202 s32 XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize);
\r
203 s32 XSdPs_Select_Card (XSdPs *InstancePtr);
\r
204 s32 XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq);
\r
205 s32 XSdPs_Change_BusWidth(XSdPs *InstancePtr);
\r
206 s32 XSdPs_Change_BusSpeed(XSdPs *InstancePtr);
\r
207 s32 XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR);
\r
208 s32 XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff);
\r
209 s32 XSdPs_Pullup(XSdPs *InstancePtr);
\r
210 s32 XSdPs_MmcCardInitialize(XSdPs *InstancePtr);
\r
211 s32 XSdPs_CardInitialize(XSdPs *InstancePtr);
\r
212 s32 XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff);
\r
213 /* Wait for Command/Transfer Complete. */
\r
214 s32 XSdPs_Wait_For(XSdPs *InstancePtr, u32 Mask, u32 Wait);
\r