fsdio.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fsdio.h
  15. * Date: 2022-05-26 16:20:52
  16. * LastEditTime: 2022-05-26 16:20:53
  17. * Description:  This files is for sdio user interface definition
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2021/12/2 init
  23. * 1.1 zhugengyu 2022/6/6 modify according to tech manual.
  24. * 1.2 zhugengyu 2022/7/15 adopt to e2000
  25. */
  26. #ifndef DRIVERS_FSDIO_H
  27. #define DRIVERS_FSDIO_H
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #endif
  32. /***************************** Include Files *********************************/
  33. #include "ftypes.h"
  34. #include "ferror_code.h"
  35. #include "fkernel.h"
  36. /************************** Constant Definitions *****************************/
  37. /* SDIO driver error code */
  38. #define FSDIO_SUCCESS FT_SUCCESS
  39. #define FSDIO_ERR_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 1)
  40. #define FSDIO_ERR_NOT_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 2)
  41. #define FSDIO_ERR_SHORT_BUF FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 3)
  42. #define FSDIO_ERR_NOT_SUPPORT FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 4)
  43. #define FSDIO_ERR_INVALID_STATE FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 5)
  44. #define FSDIO_ERR_TRANS_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 6)
  45. #define FSDIO_ERR_CMD_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 7)
  46. #define FSDIO_ERR_NO_CARD FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 8)
  47. #define FSDIO_ERR_BUSY FT_MAKE_ERRCODE(ErrModBsp, ErrBspMmc, 9)
  48. typedef enum
  49. {
  50. FSDIO_IDMA_TRANS_MODE, /* DMA trans mode */
  51. FSDIO_PIO_TRANS_MODE, /* NO-DMA trans by read/write Fifo */
  52. } FSdioTransMode; /* SDIO trans mode */
  53. typedef enum
  54. {
  55. FSDIO_GENERAL_INTR, /* interrupt status belongs to controller */
  56. FSDIO_IDMA_INTR, /* interrupt status belongs to DMA */
  57. } FSdioIntrType; /* SDIO interrupt status type */
  58. typedef enum
  59. {
  60. FSDIO_SD_3_3V_VOLTAGE = 0, /* 3.3v */
  61. FSDIO_SD_1_8V_VOLTAGE /* 1.8v */
  62. } FSdioVoltageType; /* SDIO card voltage type */
  63. typedef enum
  64. {
  65. FSDIO_EVT_CARD_DETECTED = 0, /* card detected event */
  66. FSDIO_EVT_CMD_DONE, /* cmd transfer finish event */
  67. FSDIO_EVT_DATA_DONE, /* cmd with data transfer finish event */
  68. FSDIO_EVT_ERR_OCCURE, /* error occurred in transfer */
  69. FSDIO_NUM_OF_EVT
  70. } FSdioEvtType; /* SDIO event type */
  71. #define FSDIO_DEFAULT_BLOCK_SZ 512U
  72. #define FSDIO_SD_400KHZ 400000U
  73. #define FSDIO_SD_25_MHZ 25000000U
  74. #define FSDIO_SD_50_MHZ 50000000U
  75. /**************************** Type Definitions *******************************/
  76. typedef struct _FSdio FSdio;
  77. typedef void (*FSdioRelaxHandler)(void);
  78. typedef void (*FSdioEvtHandler)(FSdio *const instance_p, void *args);
  79. typedef struct
  80. {
  81. u32 attribute; /* ds0 */
  82. #define FSDIO_IDMAC_DES0_DIC BIT(1)/* 内部描述表不触发TI/RI中断 */
  83. #define FSDIO_IDMAC_DES0_LD BIT(2)/* 数据的最后一个描述符 */
  84. #define FSDIO_IDMAC_DES0_FD BIT(3)/* 数据的第一个描述符 */
  85. #define FSDIO_IDMAC_DES0_CH BIT(4)/* 链接下一个描述符地址 */
  86. #define FSDIO_IDMAC_DES0_ER BIT(5)/* 链表已经到达最后一个链表 */
  87. #define FSDIO_IDMAC_DES0_CES BIT(30)/* RINTSTS寄存器错误汇总 */
  88. #define FSDIO_IDMAC_DES0_OWN BIT(31)/* 描述符关联DMA,完成传输后该位置置0 */
  89. u32 non1; /* ds1 --> unused */
  90. u32 len; /* ds2 bit[25:13] buffer2 size,bit[12:0] buffer1 size*/
  91. #define FSDIO_IDMAC_DES2_BUF1_MASK GENMASK(12, 0)
  92. #define FSDIO_IDMAC_DES2_BUF1_SIZE(x) (FSDIO_IDMAC_DES2_BUF1_MASK & (x))
  93. #define FSDIO_IDMAC_DES2_BUF2_MASK GENMASK(25, 13)
  94. #define FSDIO_IDMAC_DES2_BUF2_SIZE(x) (FSDIO_IDMAC_DES2_BUF2_MASK & (x << 13))
  95. u32 non2; /* ds3 --> unused */
  96. u32 addr_lo; /* ds4 Lower 32-bits of Buffer Address Pointer 1 --> buffer 1 */
  97. u32 addr_hi; /* ds5 Upper 32-bits of Buffer Address Pointer 1 */
  98. u32 desc_lo; /* ds6 Lower 32-bits of Next Descriptor Address --> buffer 2 */
  99. u32 desc_hi; /* ds7 Upper 32-bits of Next Descriptor Address */
  100. } __attribute__((packed)) __attribute((aligned(4))) FSdioIDmaDesc; /* SDIO DMA descriptr */
  101. typedef struct
  102. {
  103. volatile FSdioIDmaDesc *first_desc; /* first descriptor in the list */
  104. u32 desc_num; /* num of descriptors in the list */
  105. } FSdioIDmaDescList; /* SDIO DMA descriptors list */
  106. typedef struct
  107. {
  108. u8 *buf; /* trans buffer */
  109. u32 blksz; /* card block size */
  110. u32 blkcnt; /* num of block in trans */
  111. u32 datalen; /* bytes in trans */
  112. } FSdioData; /* SDIO trans data */
  113. typedef struct
  114. {
  115. u32 cmdidx; /* command index */
  116. u32 cmdarg; /* command argument */
  117. u32 response[4]; /* command response buffer */
  118. u32 flag; /* command flags */
  119. #define FSDIO_CMD_FLAG_NEED_INIT BIT(1) /* need initialization */
  120. #define FSDIO_CMD_FLAG_EXP_RESP BIT(2) /* need reply */
  121. #define FSDIO_CMD_FLAG_EXP_LONG_RESP BIT(3) /* need 136 bits long reply */
  122. #define FSDIO_CMD_FLAG_NEED_RESP_CRC BIT(4) /* need CRC */
  123. #define FSDIO_CMD_FLAG_EXP_DATA BIT(5) /* need trans data */
  124. #define FSDIO_CMD_FLAG_WRITE_DATA BIT(6) /* need trans data to write card */
  125. #define FSDIO_CMD_FLAG_READ_DATA BIT(7) /* need trans data to read card */
  126. #define FSDIO_CMD_FLAG_NEED_AUTO_STOP BIT(8) /* need auto stop after command */
  127. #define FSDIO_CMD_FLAG_ADTC BIT(9) /* need ADTC */
  128. #define FSDIO_CMD_FLAG_SWITCH_VOLTAGE BIT(10) /* need switch voltage */
  129. FSdioData *data_p; /* SDIO trans data */
  130. volatile boolean success; /* TRUE: comand and data transfer success */
  131. } FSdioCmdData; /* SDIO trans command and data */
  132. typedef struct
  133. {
  134. u32 instance_id; /* Device instance id */
  135. uintptr base_addr; /* Device base address */
  136. u32 irq_num; /* Interrupt num */
  137. FSdioTransMode trans_mode; /* Trans mode, PIO/DMA */
  138. FSdioVoltageType voltage; /* Card voltage type */
  139. boolean non_removable; /* No removeable media, e.g eMMC */
  140. } FSdioConfig; /* SDIO intance configuration */
  141. typedef struct _FSdio
  142. {
  143. FSdioConfig config; /* Current active configs */
  144. u32 is_ready; /* Device is initialized and ready */
  145. FSdioIDmaDescList desc_list; /* DMA descriptor list, valid in DMA trans mode */
  146. FSdioEvtHandler evt_handlers[FSDIO_NUM_OF_EVT]; /* call-backs for interrupt event */
  147. void *evt_args[FSDIO_NUM_OF_EVT]; /* arguments for event call-backs */
  148. } FSdio; /* SDIO intance */
  149. /************************** Variable Definitions *****************************/
  150. /***************** Macros (Inline Functions) Definitions *********************/
  151. /************************** Function Prototypes ******************************/
  152. /* Get the device instance default configure */
  153. const FSdioConfig *FSdioLookupConfig(u32 instance_id);
  154. /* initialization SDIO controller instance */
  155. FError FSdioCfgInitialize(FSdio *const instance_p, const FSdioConfig *cofig_p);
  156. /* deinitialization SDIO controller instance */
  157. void FSdioDeInitialize(FSdio *const instance_p);
  158. /* Setup DMA descriptor for SDIO controller instance */
  159. FError FSdioSetIDMAList(FSdio *const instance_p, volatile FSdioIDmaDesc *desc, u32 desc_num);
  160. /* Set the Card clock freqency */
  161. FError FSdioSetClkFreq(FSdio *const instance_p, u32 input_clk_hz);
  162. /* Start command and data transfer in DMA mode */
  163. FError FSdioDMATransfer(FSdio *const instance_p, FSdioCmdData *const cmd_data_p);
  164. /* Wait DMA transfer finished by poll */
  165. FError FSdioPollWaitDMAEnd(FSdio *const instance_p, FSdioCmdData *const cmd_data_p, FSdioRelaxHandler relax);
  166. /* Start command and data transfer in PIO mode */
  167. FError FSdioPIOTransfer(FSdio *const instance_p, FSdioCmdData *const cmd_data_p);
  168. /* Wait PIO transfer finished by poll */
  169. FError FSdioPollWaitPIOEnd(FSdio *const instance_p, FSdioCmdData *const cmd_data_p, FSdioRelaxHandler relax);
  170. /* Get cmd response and received data after wait poll status or interrupt signal */
  171. FError FSdioGetCmdResponse(FSdio *const instance_p, FSdioCmdData *const cmd_data_p);
  172. /* Get SDIO controller interrupt mask */
  173. u32 FSdioGetInterruptMask(FSdio *const instance_p, FSdioIntrType intr_type);
  174. /* Enable/Disable SDIO controller interrupt */
  175. void FSdioSetInterruptMask(FSdio *const instance_p, FSdioIntrType type, u32 set_mask, boolean enable);
  176. /* Interrupt handler for SDIO instance */
  177. void FSdioInterruptHandler(s32 vector, void *param);
  178. /* Reset controller from error state */
  179. FError FSdioRestart(FSdio *const instance_p);
  180. /* Register event call-back function as handler for interrupt events */
  181. void FSdioRegisterEvtHandler(FSdio *const instance_p, FSdioEvtType evt, FSdioEvtHandler handler, void *handler_arg);
  182. /* Dump all register value of SDIO instance */
  183. void FSdioDumpRegister(uintptr base_addr);
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif