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 /*****************************************************************************/
37 * This file contains the implementation of XSdPs driver.
38 * This driver is used initialize read from and write to the SD card.
39 * Features such as switching bus width to 4-bit and switching to high speed,
40 * changing clock frequency, block size etc. are supported.
41 * SD 2.0 uses 1/4 bus width and speeds of 25/50KHz. Initialization, however
42 * is done using 1-bit bus width and 400KHz clock frequency.
43 * SD commands are classified as broadcast and addressed. Commands can be
44 * those with response only (using only command line) or
45 * response + data (using command and data lines).
46 * Only one command can be sent at a time. During a data transfer however,
47 * when dsta lines are in use, certain commands (which use only the command
48 * line) can be sent, most often to obtain status.
49 * This driver does not support multi card slots at present.
52 * This includes initialization on the host controller side to select
53 * clock frequency, bus power and default transfer related parameters.
54 * The default voltage is 3.3V.
55 * On the SD card side, the initialization and identification state diagram is
56 * implemented. This resets the card, gives it a unique address/ID and
57 * identifies key card related specifications.
60 * The SD card is put in tranfer state to read from or write to it.
61 * The default block size is 512 bytes and if supported,
62 * default bus width is 4-bit and bus speed is High speed.
63 * The read and write functions are implemented in polled mode using ADMA2.
65 * At any point, when key parameters such as block size or
66 * clock/speed or bus width are modified, this driver takes care of
67 * maintaining the same selection on host and card.
68 * All error bits in host controller are monitored by the driver and in the
69 * event one of them is set, driver will clear the interrupt status and
70 * communicate failure to the upper layer.
73 * This driver can be used with xilffs library to read and write files to SD.
74 * (Please refer to procedure in diskio.c). The file system read/write example
75 * in polled mode can used for reference.
77 * There is no example for using SD driver without file system at present.
78 * However, the driver can be used without the file system. The glue layer
79 * in filesytem can be used as reference for the same. The block count
80 * passed to the read/write function in one call is limited by the ADMA2
81 * descriptor table and hence care will have to be taken to call read/write
82 * API's in a loop for large file sizes.
84 * Interrupt mode is not supported because it offers no improvement when used
88 * SD driver supports SD and eMMC based on the "enable MMC" parameter in SDK.
89 * The features of eMMC supported by the driver will depend on those supported
90 * by the host controller. The current driver supports read/write on eMMC card
91 * using 4-bit and high speed mode currently.
93 * Features not supported include - card write protect, password setting,
94 * lock/unlock, interrupts, SDMA mode, programmed I/O mode and
95 * 64-bit addressed ADMA2, erase/pre-erase commands.
98 * MODIFICATION HISTORY:
100 * Ver Who Date Changes
101 * ----- --- -------- -----------------------------------------------
102 * 1.00a hk/sg 10/17/13 Initial release
103 * 2.0 hk 03/07/14 Version number revised.
104 * 2.1 hk 04/18/14 Increase sleep for eMMC switch command.
105 * Add sleep for microblaze designs. CR# 781117.
106 * 2.2 hk 07/28/14 Make changes to enable use of data cache.
107 * 2.3 sk 09/23/14 Send command for relative card address
108 * when re-initialization is done.CR# 819614.
109 * Use XSdPs_Change_ClkFreq API whenever changing
111 * 2.4 sk 12/04/14 Added support for micro SD without
113 * Checked for DAT Inhibit mask instead of CMD
114 * Inhibit mask in Cmd Transfer API.
115 * Added Support for SD Card v1.0
119 ******************************************************************************/
130 #include "xsdps_hw.h"
133 /************************** Constant Definitions *****************************/
135 #define XSDPS_CLK_400_KHZ 400000 /**< 400 KHZ */
136 #define XSDPS_CLK_50_MHZ 50000000 /**< 50 MHZ */
137 #define CT_MMC 0x1 /**< MMC Card */
138 #define CT_SD1 0x2 /**< SD ver 1 */
139 #define CT_SD2 0x3 /**< SD ver 2 */
140 /**************************** Type Definitions *******************************/
142 * This typedef contains configuration information for the device.
145 u16 DeviceId; /**< Unique ID of device */
146 u32 BaseAddress; /**< Base address of the device */
147 u32 InputClockHz; /**< Input clock frequency */
148 u32 CardDetect; /**< Card Detect */
149 u32 WriteProtect; /**< Write Protect */
153 * ADMA2 descriptor table
156 u16 Attribute; /**< Attributes of descriptor */
157 u16 Length; /**< Length of current dma transfer */
158 u32 Address; /**< Address of current dma transfer */
159 } XSdPs_Adma2Descriptor;
162 * The XSdPs driver instance data. The user is required to allocate a
163 * variable of this type for every SD device in the system. A pointer
164 * to a variable of this type is then passed to the driver API functions.
167 XSdPs_Config Config; /**< Configuration structure */
168 u32 IsReady; /**< Device is initialized and ready */
169 u32 Host_Caps; /**< Capabilities of host controller */
170 u32 HCS; /**< High capacity support in card */
171 u32 CardID[4]; /**< Card ID */
172 u32 RelCardAddr; /**< Relative Card Address */
173 u32 CardType; /**< Card Type(version) */
174 /**< ADMA Descriptors */
176 #pragma data_alignment = 32
177 XSdPs_Adma2Descriptor Adma2_DescrTbl[32];
178 #pragma data_alignment = 4
180 XSdPs_Adma2Descriptor Adma2_DescrTbl[32] __attribute__ ((aligned(32)));
184 /***************** Macros (Inline Functions) Definitions *********************/
186 /************************** Function Prototypes ******************************/
187 XSdPs_Config *XSdPs_LookupConfig(u16 DeviceId);
188 int XSdPs_CfgInitialize(XSdPs *InstancePtr, XSdPs_Config *ConfigPtr,
190 int XSdPs_SdCardInitialize(XSdPs *InstancePtr);
191 int XSdPs_ReadPolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, u8 *Buff);
192 int XSdPs_WritePolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, const u8 *Buff);
193 int XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize);
194 int XSdPs_Select_Card (XSdPs *InstancePtr);
195 int XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq);
196 int XSdPs_Change_BusWidth(XSdPs *InstancePtr);
197 int XSdPs_Change_BusSpeed(XSdPs *InstancePtr);
198 int XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR);
199 int XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff);
200 int XSdPs_Pullup(XSdPs *InstancePtr);
201 int XSdPs_MmcCardInitialize(XSdPs *InstancePtr);
202 int XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff);