1 /**
2   ******************************************************************************
3   * @file    rtl8721d_sdioh.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_HOST_H
20 #define _RTL8721D_SDIO_HOST_H
21 
22 /** @addtogroup AmebaD_Periph_Driver
23   * @{
24   */
25 
26 /** @defgroup SDIOH
27   * @brief SDIOH driver modules
28   * @{
29   */
30 
31 /* Exported Types --------------------------------------------------------*/
32 /** @defgroup SDIOH_Exported_Types SDIOH Exported Types
33   * @{
34   */
35 /**
36   * @brief	 SDIOH init structure definition
37   */
38 typedef struct {
39 	u8 SDIOH_idle_level;	/*!> Indicate the idle pin level mask. When operate in 1-Bit bus width, this value is 0x03.
40 								When operate in 4-Bit bus width, this value is 0x1F. */
41 } SDIOH_InitTypeDef;
42 
43 /**
44   * @brief	 SDIOH DMA control structure definition
45   */
46 typedef struct {
47     u32 start_addr;		/*!< Specify the DMA start address. Unit: 8 Bytes. */
48     u16 blk_cnt;		/*!< Specify the DMA transfer length.  Unit: 512 Bytes). */
49     u8 op;				/*!< Specify the data move direction. Should be a value of @ref SDIOH_DMA_Operation. */
50     u8 type;			/*!< Specify the transfer type. Shold be a value of @ref SDIOH_DMA_Transfer_Type. */
51 } SDIOH_DmaCtl;
52 
53 /**
54   * @brief	 SDIOH command parameters structure definition
55   */
56 typedef struct{
57     u32 arg;			/*!< Specify the argument to be transfered with command. */
58     u8 idx;				/*!< Specify the command to be transfered. */
59     u8 rsp_type;		/*!< Specify the response type. Should be a value of @ref SDIOH_Card_Response_Classfication. */
60     u8 rsp_crc_chk;		/*!< Specify CRC7 check enable or not. Should be ENABLE or DISABLE. */
61     u8 data_present;	/*!< Specify which thers is data need to read after get response from card. Should be a value of
62     						@ref SDIOH_Data_Present */
63 } SDIOH_CmdTypeDef;
64 /**
65   * @}
66   */
67 
68 /* Exported constants --------------------------------------------------------*/
69 
70 /** @defgroup SDIOH_Exported_Constants SDIOH Exported Constants
71   * @{
72   */
73 
74 /** @defgroup SDIOH_Working_Mode
75   * @{
76   */
77 #define SDIOH_NORMAL_WRITE				0
78 #define SDIOH_AUTO_WRITE3				1
79 #define SDIOH_AUTO_WRITE4				2
80 #define SDIOH_AUTO_READ3				5
81 #define SDIOH_AUTO_READ4				6
82 #define SDIOH_SEND_CMD_GET_RSP			8
83 #define SDIOH_AUTO_WRITE1				9
84 #define SDIOH_AUTO_WRITE2				10
85 #define SDIOH_NORMAL_READ				12
86 #define SDIOH_AUTO_READ1				13
87 #define SDIOH_AUTO_READ2				14
88 #define SDIOH_TUNING					15
89 /**
90   * @}
91   */
92 
93 /** @defgroup SDIOH_Card_Response_Type
94   * @{
95   */
96 #define SDIOH_NO_RESP					0
97 #define SDIOH_RESP_R1					1
98 #define SDIOH_RESP_R2					2
99 #define SDIOH_RESP_R3					3
100 #define SDIOH_RESP_R6					4
101 #define SDIOH_RESP_R7					5
102 /**
103   * @}
104   */
105 
106 /** @defgroup SDIOH_Card_Response_Classification
107   * @{
108   */
109 #define SDIOH_NO_RESP					0
110 #define SDIOH_RSP_6B					1
111 #define SDIOH_RSP_17B					2
112 /**
113   * @}
114   */
115 
116 /** @defgroup SDIOH_Data_Present
117   * @{
118   */
119 #define SDIOH_NO_DATA					0
120 #define SDIOH_DATA_EXIST				1
121 /**
122   * @}
123   */
124 
125 /** @defgroup SDIOH_DMA_Transfer_Type
126   * @{
127   */
128 #define SDIOH_DMA_NORMAL				0
129 #define SDIOH_DMA_64B					1
130 #define SDIOH_DMA_R2					2
131 /**
132   * @}
133   */
134 
135 /** @defgroup SDIOH_Mode_Definition
136   * @{
137   */
138 #define SDIOH_SD20_MODE					0
139 #define SDIOH_DDR_MODE					1
140 #define SDIOH_SD30_MODE					2
141 /**
142   * @}
143   */
144 
145 /** @defgroup SDIOH_Bus_Width
146   * @{
147   */
148 #define SDIOH_BUS_WIDTH_1BIT			0
149 #define SDIOH_BUS_WIDTH_4BIT			1
150 /**
151   * @}
152   */
153 
154 /** @defgroup SDIOH_DMA_Operation
155   * @{
156   */
157 #define SDIOH_DMA_WRITE					0
158 #define SDIOH_DMA_READ					1
159 /**
160   * @}
161   */
162 
163 /**
164   * @}
165   */
166 
167 /* Exported functions --------------------------------------------------------*/
168 /** @defgroup SDIOH_Exported_Functions SDIOH Exported Functions
169   * @{
170   */
171 
172 /** @defgroup SDIO_HOST_Functions SDIO Host Functions
173   * @{
174   */
175 _LONG_CALL_ u32 SDIOH_Busy(void);
176 _LONG_CALL_ u32 SDIOH_CheckTxError(u16 *status);
177 _LONG_CALL_ u32 SDIOH_WaitTxDone(u32 timeout_us);
178 _LONG_CALL_ u32 SDIOH_WaitDMADone(u32 timeout_us);
179 _LONG_CALL_ u32 SDIOH_GetISR(void);
180 _LONG_CALL_ void SDIOH_INTConfig(u8 SDIO_IT, u32 newState);
181 _LONG_CALL_ void SDIOH_INTClearPendingBit(u8 SDIO_IT);
182 _LONG_CALL_ u32 SDIOH_CheckBusState(u8 status, u32 timeout_us);
183 _LONG_CALL_ u8 SDIOH_GetBusWidth(void);
184 _LONG_CALL_ void SDIOH_SetBusWidth(u8 width);
185 _LONG_CALL_ void SDIOH_DMAConfig(SDIOH_DmaCtl *dma_ctl);
186 _LONG_CALL_ void SDIOH_DMAReset(void);
187 _LONG_CALL_ u32 SDIOH_SendCommand(SDIOH_CmdTypeDef *cmd_attrib, u32 timeout_us);
188 _LONG_CALL_ u8 SDIOH_GetResponse(u8 byte_index);
189 _LONG_CALL_ void SDIOH_SwitchSpeed(u8 clk_div, u8 mode);
190 _LONG_CALL_ u32 SDIOH_InitialModeCmd(u8 NewState, u8 Level);
191 _LONG_CALL_ u32 SDIOH_Init (u8 BusBitMode);
192 _LONG_CALL_ void SDIOH_DeInit (void);
193 
194 /**
195 * @}
196 */
197 
198 /**
199 * @}
200 */
201 
202 /* Registers Definitions --------------------------------------------------------*/
203 /**************************************************************************//**
204  * @defgroup SDIOH_Register_Definitions SDIOH Register Definitions
205  * @{
206  *****************************************************************************/
207 
208 /**************************************************************************//**
209  * @defgroup SDIOH_SRAM_CRL
210  * @{
211  *****************************************************************************/
212 #define SDIOH_SHIFT_LX_BURST_SIZE									6
213 #define SDIOH_LX_BURST_SIZE_64B										(0 << SDIOH_SHIFT_LX_BURST_SIZE)
214 #define SDIOH_SHIFT_MAP_SEL											5
215 #define SDIOH_MAP_SEL_DEC											(1 << SDIOH_SHIFT_MAP_SEL)
216 /** @} */
217 
218 /**************************************************************************//**
219  * @defgroup SDIOH_DMA_CRL1
220  * @{
221  *****************************************************************************/
222 #define SDIOH_MASK_DRAM_SA											0x0FFFFFFF
223 /** @} */
224 
225 /**************************************************************************//**
226  * @defgroup SDIOH_DMA_CRL2
227  * @{
228  *****************************************************************************/
229 #define SDIOH_MASK_DMA_LEN											0x0000FFFF
230 /** @} */
231 
232 /**************************************************************************//**
233  * @defgroup SDIOH_DMA_CRL3
234  * @{
235  *****************************************************************************/
236 #define SDIOH_DAT64_SEL												BIT(5)
237 #define SDIOH_RSP17_SEL												BIT(4)
238 #define SDIOH_SHIFT_DDR_WR											1
239 #define SDIOH_DMA_XFER												BIT(0)
240 /** @} */
241 
242 /**************************************************************************//**
243  * @defgroup SDIOH_SD_ISR
244  * @{
245  *****************************************************************************/
246 #define SDIOH_DMA_TRANSFER_DONE										BIT(4)
247 #define SDIOH_CARD_ERROR											BIT(2)
248 #define SDIOH_CARD_END												BIT(1)
249 #define SDIOH_SD_ISR_ALL											(0x16)
250 /** @} */
251 
252 /**************************************************************************//**
253  * @defgroup SDIOH_SD_ISREN
254  * @{
255  *****************************************************************************/
256 #define SDIOH_DMA_CTL_INT_EN										BIT(4)
257 #define SDIOH_CARD_ERR_INT_EN										BIT(2)
258 #define SDIOH_CARD_END_INT_EN										BIT(1)
259 #define SDIOH_WRITE_DATA											BIT(0)
260 /** @} */
261 
262 /**************************************************************************//**
263  * @defgroup SDIOH_CKGEN_CTL
264  * @{
265  *****************************************************************************/
266 #define SDIOH_SHIFT_SD30_SAMP_CLK_SRC								12
267 #define SDIOH_SHIFT_SD30_PUSH_CLK_SRC								8
268 #define SDIOH_SHIFT_CRC_CLK_SRC										4
269 #define SDIOH_SD30_SAMP_CLK_VP1										(2 << SDIOH_SHIFT_SD30_SAMP_CLK_SRC)
270 #define SDIOH_SD30_PUSH_CLK_VP0										(1 << SDIOH_SHIFT_SD30_PUSH_CLK_SRC)
271 #define SDIOH_CRC_CLK_SSC											(0 << SDIOH_SHIFT_CRC_CLK_SRC)
272 #define SDIOH_MASK_CLKDIV											(0x7)
273 #define SDIOH_CLK_DIV1												0
274 #define SDIOH_CLK_DIV2												1
275 #define SDIOH_CLK_DIV4												2
276 #define SDIOH_CLK_DIV8												3
277 
278 /** @} */
279 
280 /**************************************************************************//**
281  * @defgroup SDIOH_CARD_STOP
282  * @{
283  *****************************************************************************/
284 #define SDIOH_TARGET_MODULE_SD										BIT(2)
285 /** @} */
286 
287 /**************************************************************************//**
288  * @defgroup SDIOH_CARD_SELECT
289  * @{
290  *****************************************************************************/
291 #define SDIOH_CARD_SEL_SD_MODULE									0x2
292 /** @} */
293 
294 /**************************************************************************//**
295  * @defgroup SDIOH_CARD_EXIST
296  * @{
297  *****************************************************************************/
298 #define SDIOH_SD_WP													BIT(5)
299 #define SDIOH_SD_EXIST												BIT(2)
300 /** @} */
301 
302 /**************************************************************************//**
303  * @defgroup SDIOH_CARD_INT_EN
304  * @{
305  *****************************************************************************/
306 #define SDIOH_SDMMC_INT_EN											BIT(2)
307 /** @} */
308 
309 /**************************************************************************//**
310  * @defgroup SDIOH_CARD_INT_PEND
311  * @{
312  *****************************************************************************/
313 #define SDIOH_SDMMC_INT_PEND										BIT(2)
314 /** @} */
315 
316 /**************************************************************************//**
317  * @defgroup SDIOH_CARD_CLK_EN_CTL
318  * @{
319  *****************************************************************************/
320 #define SDIOH_SD_CARD_MOUDLE_EN										BIT(2)
321 /** @} */
322 
323 /**************************************************************************//**
324  * @defgroup SDIOH_SD_CONFIG1
325  * @{
326  *****************************************************************************/
327 #define SDIOH_INITIAL_MODE											BIT(7)
328 #define SDIOH_CLK_DIV_BY_128										0
329 #define SDIOH_CLK_DIV_BY_256										BIT(6)
330 #define SDIOH_SD30_ASYNC_FIFO_RST_N									BIT(4)
331 #define SDIOH_SHIFT_MODE_SEL										2
332 #define SDIOH_MASK_MODE_SEL											0xc
333 #define SDIOH_SHIFT_BUS_WIDTH										0
334 #define SDIOH_MASK_BUS_WIDTH										0x3
335 /** @} */
336 
337 /**************************************************************************//**
338  * @defgroup SDIOH_SD_CONFIG2
339  * @{
340  *****************************************************************************/
341 #define SDIOH_CRC7_CAL_EN											0
342 #define SDIOH_CRC7_CAL_DIS											BIT(7)
343 #define SDIOH_CRC16_CHK_EN											0
344 #define SDIOH_CRC16_CHK_DIS											BIT(6)
345 #define SDIOH_WAIT_WR_CRCSTA_TO_EN									0
346 #define SDIOH_WAIT_WR_CRCSTA_TO_DIS									BIT(5)
347 #define SDIOH_IGNORE_WR_CRC_ERR_EN									0
348 #define SDIOH_IGNORE_WR_CRC_ERR_DIS									BIT(4)
349 #define SDIOH_WAIT_BUSY_END_DIS										0
350 #define SDIOH_WAIT_BUSY_END_EN										BIT(3)
351 #define SDIOH_CRC7_CHK_EN											0
352 #define SDIOH_CRC7_CHK_DIS											BIT(2)
353 
354 /** @} */
355 
356 /**************************************************************************//**
357  * @defgroup SDIOH_SD_CONFIG3
358  * @{
359  *****************************************************************************/
360 #define SDIOH_STOP_STA_WAIT_BUSY_EN									0
361 #define SDIOH_STOP_STA_WAIT_BUSY_DIS								BIT(7)
362 #define SDIOH_CMD_STA_WAIT_BUSY_EN									0
363 #define SDIOH_CMD_STA_WAIT_BUSY_DIS									BIT(6)
364 #define SDIOH_DATA_PHA_WAIT_BUSY_EN									BIT(5)
365 #define SDIOH_DATA_PHA_WAIT_BUSY_DIS								0
366 #define SDIOH_SD30_CLK_STOP_EN										BIT(4)
367 #define SDIOH_SD30_CLK_STOP_DIS										0
368 #define SDIOH_SD20_CLK_STOP_EN										BIT(3)
369 #define SDIOH_SD20_CLK_STOP_DIS										0
370 #define SDIOH_SD_CMD_RESP_CHK_EN									BIT(2)
371 #define SDIOH_SD_CMD_RESP_CHK_DIS									0
372 #define SDIOH_ADDR_MODE_SECTOR										0
373 #define SDIOH_ADDR_MODE_BYTE										BIT(1)
374 #define SDIOH_CMD_RESP_TO_EN										BIT(0)
375 #define SDIOH_CMD_RESP_TO_DIS										0
376 /** @} */
377 
378 /**************************************************************************//**
379  * @defgroup SDIOH_SD_STATUS1_2
380  * @{
381  *****************************************************************************/
382 #define SDIOH_SD_TUNNING_PAT_COMP_ERR								BIT(0)
383 #define SDIOH_GET_WRCRC_STA_TO_ERR									BIT(1)
384 #define SDIOH_MASK_WR_CRC_STA										0x1C
385 #define SDIOH_WR_CRC_ERR											BIT(5)
386 #define SDIOH_CRC16_ERR												BIT(6)
387 #define SDIOH_CRC7_ERR												BIT(7)
388 #define SDIOH_SD_CMD_RSP_TO_ERR										BIT(8)
389 #define SDIOH_SD_CMD_RSP_INVALID									BIT(9)
390 /** @} */
391 
392 /**************************************************************************//**
393  * @defgroup SDIOH_SD_BLOCK_CNT_L
394  * @{
395  *****************************************************************************/
396 #define SDIOH_MASK_BLOCL_CNT_L										0xFF
397 /** @} */
398 
399 /**************************************************************************//**
400  * @defgroup SDIOH_SD_BLOCK_CNT_H
401  * @{
402  *****************************************************************************/
403 #define SDIOH_MASK_BLOCL_CNT_H										0x7F
404 /** @} */
405 
406 /**************************************************************************//**
407  * @defgroup SDIOH_SD_TRANSFER
408  * @{
409  *****************************************************************************/
410 #define SDIOH_START_TRANSFER										BIT(7)
411 #define SDIOH_TRANSFER_END											BIT(6)
412 #define SDIOH_SD_MODULE_FSM_IDLE									BIT(5)
413 #define SDIOH_ERR_OCCUR												BIT(4)
414 #define SDIOH_MASK_COM_CODE											0xF
415 /** @} */
416 
417 /**************************************************************************//**
418  * @defgroup SDIOH_SD_CMD_STATE
419  * @{
420  *****************************************************************************/
421 #define SDIOH_CMD_FSM_IDLE											BIT(7)
422 /** @} */
423 
424 /**************************************************************************//**
425  * @defgroup SDIOH_SD_DATA_STATE
426  * @{
427  *****************************************************************************/
428 #define SDIOH_DATA_FSM_IDLE											BIT(7)
429 /** @} */
430 /** @} */
431 
432 /**
433 * @}
434 */
435 
436 /**
437 * @}
438 */
439 
440 
441 #undef SDIOH_SUPPORT_SD30
442 
443 #define SDIOH_CMD_CPLT_TIMEOUT		  	5000		/* Max. timeout value when checking the flag of command complete, unit: us */
444 #define SDIOH_XFER_CPLT_TIMEOUT			1000000	/* Max. timeout value when checking the flag of transfer complete, unit: us */
445 
446 #define SDIOH_READ_TIMEOUT				100000
447 #define SDIOH_WRITE_TIMEOUT				250000
448 #define SDIOH_ERASE_TIMEOUT				2000000//250000
449 
450 #define HOST_COMMAND					BIT(6)	/* Transmission bit of register "SD_CMD0", indicating the direction of transmission (host = 1)*/
451 #define SDIOH_CMD_IDX_MASK				0x3F		/* Command index mask of register "SD_CMD0" */
452 #define SDIOH_CMD8_VHS					0x1		/* Value of "VHS" field in CMD8, 2.7-3.6V */
453 #define SDIOH_CMD8_CHK_PATN				0xAA		/* Value of "Check pattern" field in CMD8 */
454 #define SDIOH_OCR_VDD_WIN				0xFF8000	/* Value of "OCR" field in ACMD41, OCR bit[23:0] */
455 
456 #define SDIOH_C6R2_BUF_LEN				64		/* Buffer for CMD6, R2, etc.*/
457 #define SDIOH_CSD_LEN					16
458 
459 /* SDIOH_Card_Response_Byte_Index */
460 #define SDIO_RESP0						0
461 #define SDIO_RESP1						1
462 #define SDIO_RESP2						2
463 #define SDIO_RESP3						3
464 #define SDIO_RESP4						4
465 #define SDIO_RESP5						5
466 
467 /* SDIOH_Signal_Level */
468 #define SDIOH_SIG_VOL_33				0
469 #define SDIOH_SIG_VOL_18	 			1
470 
471 
472 
473 #endif
474