]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/sdps_v3_1/src/xsdps.h
Update Zynq MPSoC hardware definition and BSP files to be those shipped with the...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / sdps_v3_1 / src / xsdps.h
1 /******************************************************************************
2 *
3 * Copyright (C) 2013 - 2016 Xilinx, Inc.  All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 *
35 * @file xsdps.h
36 * @addtogroup sdps_v2_5
37 * @{
38 * @details
39 *
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.
53 *
54 * Intialization:
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.
61 *
62 * Data transfer:
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.
67 *
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.
74 *
75 * File system use:
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.
79 *
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.
86 *
87 * Interrupt mode is not supported because it offers no improvement when used
88 * with file system.
89 *
90 * eMMC support:
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.
95 *
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.
99 *
100 * <pre>
101 * MODIFICATION HISTORY:
102 *
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
113 *                                               clock.CR# 816586.
114 * 2.4   sk         12/04/14 Added support for micro SD without
115 *                                               WP/CD. CR# 810655.
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.
128 * 2.8   sk     04/20/16 Added new workaround for auto tuning.
129 *              05/03/16 Standard Speed for SD to 19MHz in ZynqMPSoC. CR#951024
130 * 3.0   sk     06/09/16 Added support for mkfs to calculate sector count.
131 *       sk     07/16/16 Added support for UHS modes.
132 *       sk     07/07/16 Used usleep API for both arm and microblaze.
133 *       sk     07/16/16 Added Tap delays accordingly to different SD/eMMC
134 *                       operating modes.
135 *       sk     08/13/16 Removed sleep.h from xsdps.h as a temporary fix for
136 *                       CR#956899.
137 * 3.1   mi     09/07/16 Removed compilation warnings with extra compiler flags.
138 *       sk     10/13/16 Reduced the delay during power cycle to 1ms as per spec
139 *       sk     10/19/16 Used emmc_hwreset pin to reset eMMC.
140 *       sk     11/07/16 Enable Rst_n bit in ext_csd reg if not enabled.
141 *       sk     11/16/16 Issue DLL reset at 31 iteration to load new zero value.
142 *
143 * </pre>
144 *
145 ******************************************************************************/
146
147
148 #ifndef SDPS_H_
149 #define SDPS_H_
150
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
154
155 #include "xil_printf.h"
156 #include "xil_cache.h"
157 #include "xstatus.h"
158 #include "xsdps_hw.h"
159 #include <string.h>
160
161 /************************** Constant Definitions *****************************/
162
163 #define XSDPS_CT_ERROR  0x2U    /**< Command timeout flag */
164 #define MAX_TUNING_COUNT        40U             /**< Maximum Tuning count */
165
166 /**************************** Type Definitions *******************************/
167
168 typedef void (*XSdPs_ConfigTap) (u32 Bank, u32 DeviceId, u32 CardType);
169
170 /**
171  * This typedef contains configuration information for the device.
172  */
173 typedef struct {
174         u16 DeviceId;                   /**< Unique ID  of device */
175         u32 BaseAddress;                /**< Base address of the device */
176         u32 InputClockHz;               /**< Input clock frequency */
177         u32 CardDetect;                 /**< Card Detect */
178         u32 WriteProtect;                       /**< Write Protect */
179         u32 BusWidth;                   /**< Bus Width */
180         u32 BankNumber;                 /**< MIO Bank selection for SD */
181         u32 HasEMIO;                    /**< If SD is connected to EMIO */
182 } XSdPs_Config;
183
184 /* ADMA2 descriptor table */
185 typedef struct {
186         u16 Attribute;          /**< Attributes of descriptor */
187         u16 Length;             /**< Length of current dma transfer */
188         u32 Address;            /**< Address of current dma transfer */
189 } XSdPs_Adma2Descriptor;
190
191 /**
192  * The XSdPs driver instance data. The user is required to allocate a
193  * variable of this type for every SD device in the system. A pointer
194  * to a variable of this type is then passed to the driver API functions.
195  */
196 typedef struct {
197         XSdPs_Config Config;    /**< Configuration structure */
198         u32 IsReady;            /**< Device is initialized and ready */
199         u32 Host_Caps;          /**< Capabilities of host controller */
200         u32 Host_CapsExt;       /**< Extended Capabilities */
201         u32 HCS;                /**< High capacity support in card */
202         u8  CardType;           /**< Type of card - SD/MMC/eMMC */
203         u8  Card_Version;       /**< Card version */
204         u8  HC_Version;         /**< Host controller version */
205         u8  BusWidth;           /**< Current operating bus width */
206         u32 BusSpeed;           /**< Current operating bus speed */
207         u8  Switch1v8;          /**< 1.8V Switch support */
208         u32 CardID[4];          /**< Card ID Register */
209         u32 RelCardAddr;        /**< Relative Card Address */
210         u32 CardSpecData[4];    /**< Card Specific Data Register */
211         u32 SectorCount;                /**< Sector Count */
212         u32 SdCardConfig;       /**< Sd Card Configuration Register */
213         u32 Mode;                       /**< Bus Speed Mode */
214         XSdPs_ConfigTap Config_TapDelay;        /**< Configuring the tap delays */
215         /**< ADMA Descriptors */
216 #ifdef __ICCARM__
217 #pragma data_alignment = 32
218         XSdPs_Adma2Descriptor Adma2_DescrTbl[32];
219 #pragma data_alignment = 4
220 #else
221         XSdPs_Adma2Descriptor Adma2_DescrTbl[32] __attribute__ ((aligned(32)));
222 #endif
223 } XSdPs;
224
225 /***************** Macros (Inline Functions) Definitions *********************/
226
227 /************************** Function Prototypes ******************************/
228 XSdPs_Config *XSdPs_LookupConfig(u16 DeviceId);
229 s32 XSdPs_CfgInitialize(XSdPs *InstancePtr, XSdPs_Config *ConfigPtr,
230                                 u32 EffectiveAddr);
231 s32 XSdPs_SdCardInitialize(XSdPs *InstancePtr);
232 s32 XSdPs_ReadPolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, u8 *Buff);
233 s32 XSdPs_WritePolled(XSdPs *InstancePtr, u32 Arg, u32 BlkCnt, const u8 *Buff);
234 s32 XSdPs_SetBlkSize(XSdPs *InstancePtr, u16 BlkSize);
235 s32 XSdPs_Select_Card (XSdPs *InstancePtr);
236 s32 XSdPs_Change_ClkFreq(XSdPs *InstancePtr, u32 SelFreq);
237 s32 XSdPs_Change_BusWidth(XSdPs *InstancePtr);
238 s32 XSdPs_Change_BusSpeed(XSdPs *InstancePtr);
239 s32 XSdPs_Get_BusWidth(XSdPs *InstancePtr, u8 *SCR);
240 s32 XSdPs_Get_BusSpeed(XSdPs *InstancePtr, u8 *ReadBuff);
241 s32 XSdPs_Pullup(XSdPs *InstancePtr);
242 s32 XSdPs_MmcCardInitialize(XSdPs *InstancePtr);
243 s32 XSdPs_CardInitialize(XSdPs *InstancePtr);
244 s32 XSdPs_Get_Mmc_ExtCsd(XSdPs *InstancePtr, u8 *ReadBuff);
245 s32 XSdPs_Set_Mmc_ExtCsd(XSdPs *InstancePtr, u32 Arg);
246 #if defined (ARMR5) || defined (__aarch64__)
247 void XSdPs_Identify_UhsMode(XSdPs *InstancePtr, u8 *ReadBuff);
248 void XSdPs_hsd_sdr25_tapdelay(u32 Bank, u32 DeviceId, u32 CardType);
249 void XSdPs_sdr104_hs200_tapdelay(u32 Bank, u32 DeviceId, u32 CardType);
250 #endif
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif /* SD_H_ */
257 /** @} */