1 /**
2   ******************************************************************************
3   * @file    rtl8721dhp_sd.h
4   * @author
5   * @version V1.0.0
6   * @date    2018-06-29
7   * @brief   This file contains all the functions prototypes for the SDIOH firmware
8   *          library.
9   ******************************************************************************
10   * @attention
11   *
12   * This module is a confidential and proprietary property of RealTek and
13   * possession or use of this module requires written permission of RealTek.
14   *
15   * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
16   ******************************************************************************
17   */
18 
19 #ifndef _RTL8721D_SDIO_SD_H
20 #define _RTL8721D_SDIO_SD_H
21 
22 /** @addtogroup AmebaD_Periph_Driver
23   * @{
24   */
25 
26 /** @addtogroup SDIOH
27   * @brief SDIOH driver modules
28   * @{
29   */
30 
31 /** @addtogroup SDIOH
32   * @verbatim
33   *****************************************************************************************
34   * Introduction
35   *****************************************************************************************
36   * SDIOH:
37   *		- Base Address: SDIOH_BASE
38   *		- IP Clock: 100MHz
39   *		- Support SD Spec. Version 2.0
40   *		- High Voltage SD Memory Card
41   *		– Operating voltage range: 2.7-3.6 V
42   *		- Support 1/4-bit mode SD
43   *		– Bus Speed Mode (using 4 parallel data lines)
44   *		    -Default Speed mode: 3.3V signaling, Frequency up to 25 MHz, up to 12.5 MB/sec
45   *		    -High Speed mode: 3.3V signaling, Frequency up to 50 MHz, up to 25 MB/sec
46   *		- Support hardware CRC function for SD
47   *
48   *****************************************************************************************
49   * How to use SDIO Host Controller
50   *****************************************************************************************
51   *      To use the SDIO Host Controller, the following steps are mandatory.
52   *
53   *      1. Insert SD card to card slot.
54   *
55   *      2. Call SD_Init() function to initialize SDIOH and SD card, in which the following operations are executed:
56   *        (1) Enable SDIOH peripheral clock.
57   *        (2) Configure the SDIOH pinmux.
58   *        (3) Initialize SDIO Host to initial-mode and enable card interrupt.
59   *        (4) If card insert is detected, card identification is started.
60   *        (5) When card identification is successful, card enters into data transfer mode (Default Speed Mode).
61   *        (6) Set SDIOH and card to 4-bit bus width or High Speed mode if needed according to configure parameters.
62   *
63   *      3.  After initialization, users can call the following functions to read or write blocks.
64   *             SD_RESULT SD_ReadBlocks(u32 sector,u8 *data,u32 count);
65   *             SD_RESULT SD_WriteBlocks(u32 sector,const u8 *data,u32 count);
66   *
67   *      Note that if users access SD card through FATFS, then the above steps don't need to be implemented manually.
68   *      They are already porting to FATFS low-level driver, and users can call FATFS API directly.
69   *      Details can be found in FATFS example.
70   *
71   *****************************************************************************************
72   * @endverbatim
73   */
74 
75 #define  SD      		0
76 #define  EMMC   		1
77 #define  SDIO 		    SD
78 
79 /* Exported constants --------------------------------------------------------*/
80 
81 /** @defgroup SDIOH_Exported_Constants SDIOH Exported Constants
82   * @{
83   */
84 /** @defgroup SD_Command_Index
85   * @{
86   */
87 typedef enum
88 {
89 	SD_CMD_GoIdleSte           = 0,
90 	EMMC_CMD_SendOpCond        = 1,   // CMD only for EMMC
91 	SD_CMD_AllSendCid          = 2,
92 	SD_CMD_SendRelAddr         = 3,
93 	SD_CMD_SetDsr              = 4,
94 	SD_CMD_SwitchFunc          = 6,
95 	SD_CMD_SetBusWidth         = 6,   // ACMD6
96 	SD_CMD_SelDeselCard        = 7,
97 	SD_CMD_SendIfCond          = 8,   // CMD only for SD card
98 	EMMC_CMD_SendExtCsd        = 8,   // CMD only for EMMC
99 	SD_CMD_SendCsd             = 9,
100 	SD_CMD_SendCid             = 10,
101 	SD_CMD_VolSwitch           = 11,  // CMD only for SD card
102 	SD_CMD_StopXsmission       = 12,
103 	SD_CMD_SendSts             = 13,
104 	SD_CMD_SetBlklen           = 16,
105 	SD_CMD_RdSingleBlk         = 17,
106 	SD_CMD_RdMulBlk            = 18,
107 	SD_CMD_SendTuningBlk       = 19,  // CMD only for SD card
108 	SD_CMD_SendNumWrBlks       = 22,  // ACMD22
109 	SD_CMD_SetBlkCnt           = 23,
110 	SD_CMD_SetWrBlkEraseCnt    = 23,  // ACMD23
111 	SD_CMD_WrBlk               = 24,
112 	SD_CMD_WrMulBlk            = 25,
113 	SD_CMD_ProgCsd             = 27,
114 	SD_CMD_EraseBlkSt          = 32,  // CMD only for SD card
115 	SD_CMD_EraseBlkEd          = 33,  // CMD only for SD card
116 	EMMC_CMD_EraseAddrSt       = 35,  // CMD only for EMMC
117 	EMMC_CMD_EraseAddrEd       = 36,  // CMD only for EMMC
118 	SD_CMD_Erase               = 38,
119 	SD_CMD_SdSendOpCond        = 41,  // ACMD41 cmd only for SD card
120 	SD_CMD_SendScr             = 51,  // ACMD51 cmd only for SD card
121 	SD_CMD_AppCmd              = 55   // CMD only for SD card
122 } SD_COMMAND;
123 /**
124   * @}
125   */
126 
127 /** @defgroup SD_Result
128   * @{
129   */
130 typedef enum
131 {
132    SD_OK = 0,
133    SD_NODISK,
134    SD_INSERT,
135    SD_INITERR,
136    SD_PROTECTED,
137    SD_ERROR,
138 } SD_RESULT;
139 /**
140   * @}
141   */
142 
143 /** @defgroup SD_Card_States
144   * @{
145   */
146 #define SD_CARD_READY					0x00000001
147 #define SD_CARD_IDENTIFICATION			0x00000002
148 #define SD_CARD_STANDBY					0x00000003
149 #define SD_CARD_TRANSFER				0x00000004
150 #define SD_CARD_SENDING					0x00000005
151 #define SD_CARD_RECEIVING				0x00000006
152 #define SD_CARD_PROGRAMMING				0x00000007
153 #define SD_CARD_DISCONNECTED			0x00000008
154 #define SD_CARD_ERROR					0x000000FF
155 /**
156   * @}
157   */
158 
159 /** @defgroup SD_Specification_Version SD_Specification_Version
160   * @{
161   */
162 #define SD_SPEC_V101					0
163 #define SD_SPEC_V110					1
164 #define SD_SPEC_V200 					2
165 #define SD_SPEC_V300					3
166 /**
167   * @}
168   */
169 
170 /** @defgroup SD_Access_Mode SD_Access_Mode
171   * @{
172   */
173 #define SD_SPEED_DS						0 // 3.3V Function 0
174 #define SD_SPEED_HS						1 // 3.3V Function 1
175 #define SD_SPEED_SDR12					2 // 1.8V Function 0
176 #define SD_SPEED_SDR25					3 // 1.8V Function 1
177 #define SD_SPEED_SDR50					4 // 1.8V Function 2
178 #define SD_SPEED_SDR104					5 // 1.8V Function 3
179 #define SD_SPEED_DDR50					6 // 1.8V Function 4
180 #define SD_KEEP_CUR_SPEED				15
181 /**
182   * @}
183   */
184 
185 /**
186   * @}
187   */
188 
189 /* Exported Types --------------------------------------------------------*/
190 /** @defgroup SDIOH_Exported_Types SDIOH Exported Types
191   * @{
192   */
193 /**
194   * @brief	 SD card info structure definition
195   */
196 typedef struct {
197 	u8 csd[SDIOH_CSD_LEN]; 	/*!< Store the card-specific data(CSD) of the current SD card. */
198 	u16 rca;				/*!< Store the relative address(RCA) of the current SD card. */
199 	u8 is_sdhc_sdxc;		/*!< Specify the current card is SDSC or SDHC/SDXC. */
200 	u8 sd_spec_ver;			/*!< Specify the physical layer specification version of current
201 								card, which would be a value of @ref SD_Specification_Version */
202 	u32 capaticy;			/*!< Specify the capacity of current card. Unit: KByte */
203 	u32 read_bl_len;		/*!< Specify max. read data block length of current card. Unit: byte */
204 	u32 write_bl_len;		/*!< Specify max. write data block length. Unit: byte */
205 
206 	u8 sig_level;			/*!< Specify current signal level, 0: 3.3v, 1: 1.8v */
207 	u8 bus_spd;				/*!< Specify current bus speed, which would be a value of @ref SD_Access_Mode */
208 	SD_RESULT sd_status;	/*!< Specify current sd status, which would be a value of @ref SD_Result */
209 
210 	u8 dma_buf[SDIOH_C6R2_BUF_LEN] __attribute__((aligned(32)));	/*!< DMA buffer, 32 byte-alignment */
211 } SD_CardInfo;
212 /**
213   * @}
214   */
215 
216 /* Exported functions --------------------------------------------------------*/
217 /** @defgroup SDIOH_Exported_Functions SDIOH Exported Functions
218   * @{
219   */
220 
221 /** @defgroup SD_Card_Functions SD Card Functions
222   * @{
223   */
224 _LONG_CALL_ SD_RESULT SD_Init(void);
225 _LONG_CALL_ SD_RESULT SD_DeInit(void);
226 _LONG_CALL_ SD_RESULT SD_GetCapacity(u32* sector_count);
227 _LONG_CALL_ SD_RESULT SD_ReadBlocks(u32 sector,u8 *data,u32 count);
228 _LONG_CALL_ SD_RESULT SD_WriteBlocks(u32 sector,const u8 *data,u32 count);
229 _LONG_CALL_ SD_RESULT SD_Status(void);
230 _LONG_CALL_ SD_RESULT SD_GetEXTCSD(u8 *pbuf);
231 
232 /**
233   * @}
234   */
235 
236 /**
237   * @}
238   */
239 
240 /**
241   * @}
242   */
243 
244 /**
245   * @}
246   */
247 
248 #define SD_BLOCK_SIZE					512    //Bytes
249 
250 /* SDIO_RESP4 */
251 #define SD_APP_CMD						BIT(5)
252 
253 /* SDIO_RESP0 */
254 #define SD_ADDRESS_ERROR				BIT(6)
255 #define SD_BLOCK_LEN_ERROR				BIT(5)
256 #define SD_WP_VIOLATION					BIT(2)
257 
258 /* SDXC_Power_Control SDXC_Power_Control used in ACMD41*/
259 #define SD_POWER_SAVING					0
260 #define SD_MAX_PERFORM					1
261 
262 /* SD_Switch_1.8v_Request used in ACMD41 */
263 #define SD_USE_CUR_VOL					0
264 #define SD_SWITCH_18V					1
265 
266 /* SD_operation_mode used in CMD6 */
267 #define SD_CMD6_CHECK_MODE				0
268 #define SD_CMD6_SWITCH_MODE				1
269 
270 /* SD_Capacity_Support in ACMD41 */
271 #define SD_SUPPORT_SDSC_ONLY			0
272 #define SD_SUPPORT_SDHC_SDXC			1
273 
274 typedef struct {
275 	u8 sdioh_bus_speed;		/*!< Specify SDIO Host bus speed, should be SD_SPEED_DS or SD_SPEED_HS*/
276 	u8 sdioh_bus_width;		/*!< Specify SDIO Host bus width, should be a value of @ref SDIOH_Bus_Width */
277 	u32 sdioh_cd_pin;		/*!< Specify Card Detect pin, should be a value of _PB_25/_PA_6/_PNC */
278 	u32 sdioh_wp_pin;		/*!< Specify Write Protection pin, should be a value of _PB_25/_PA_6/_PNC */
279 } SDIOHCFG_TypeDef;
280 
281 #endif
282