1 /******************************************************************************
3 * Copyright (C) 2013 - 2014 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_1
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.
112 ******************************************************************************/
123 #include "xsdps_hw.h"
126 /************************** Constant Definitions *****************************/
128 /**************************** Type Definitions *******************************/
130 * This typedef contains configuration information for the device.
133 u16 DeviceId; /**< Unique ID of device */
134 u32 BaseAddress; /**< Base address of the device */
135 u32 InputClockHz; /**< Input clock frequency */
139 * ADMA2 descriptor table
142 u16 Attribute; /**< Attributes of descriptor */
143 u16 Length; /**< Length of current dma transfer */
144 u32 Address; /**< Address of current dma transfer */
145 } XSdPs_Adma2Descriptor;
148 * The XSdPs driver instance data. The user is required to allocate a
149 * variable of this type for every SD device in the system. A pointer
150 * to a variable of this type is then passed to the driver API functions.
153 XSdPs_Config Config; /**< Configuration structure */
154 u32 IsReady; /**< Device is initialized and ready */
155 u32 Host_Caps; /**< Capabilities of host controller */
156 u32 HCS; /**< High capacity support in card */
157 u32 CardID[4]; /**< Card ID */
158 u32 RelCardAddr; /**< Relative Card Address */
159 XSdPs_Adma2Descriptor Adma2_DescrTbl[32]; /**< ADMA Descriptors */
162 /***************** Macros (Inline Functions) Definitions *********************/
164 /************************** Function Prototypes ******************************/
165 XSdPs_Config *XSdPs_LookupConfig(u16 DeviceId);
166 int XSdPs_CfgInitialize(XSdPs *InstancePtr, XSdPs_Config *ConfigPtr,
168 int XSdPs_SdCardInitialize(XSdPs *InstancePtr);
169 int XSdPs_ReadPolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, u8 *Buff);
170 int XSdPs_WritePolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, const u8 *Buff);
171 int XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize);
172 int XSdPs_Select_Card (XSdPs *InstancePtr);
173 int XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq);
174 int XSdPs_Change_BusWidth(XSdPs *InstancePtr);
175 int XSdPs_Change_BusSpeed(XSdPs *InstancePtr);
176 int XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR);
177 int XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff);
178 int XSdPs_Pullup(XSdPs *InstancePtr);
179 int XSdPs_MmcCardInitialize(XSdPs *InstancePtr);
180 int XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff);