emmc_chal_sd.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2016 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef CHAL_SD_H
  7. #define CHAL_SD_H
  8. #include <stddef.h>
  9. #define BASE_CLK_FREQ (200 * 1000 * 1000)
  10. #define INIT_CLK_FREQ (400 * 1000)
  11. #define SD_ERROR_RECOVERABLE 0
  12. #define SD_ERROR_NON_RECOVERABLE 1
  13. #define SD_OK 0
  14. #define SD_FAIL (-1)
  15. #define SD_INVALID_HANDLE (-2)
  16. #define SD_CEATA_INIT_ERROR (-3)
  17. #define SD_RESET_ERROR (-4)
  18. #define SD_CARD_INIT_ERROR (-5)
  19. #define SD_INV_DATA_WIDTH (-6)
  20. #define SD_SET_BUS_WIDTH_ERROR (-7)
  21. #define SD_DMA_NOT_SUPPORT (-8)
  22. #define SD_SDIO_READ_ERROR (-9)
  23. #define SD_SDIO_WRITE_ERROR (-10)
  24. #define SD_WRITE_ERROR (-11)
  25. #define SD_READ_ERROR (-12)
  26. #define SD_READ_SIZE_ERROR (-13)
  27. #define SD_RW_ADDRESS_ERROR (-14)
  28. #define SD_XFER_ADDRESS_ERROR (-15)
  29. #define SD_DATA_XFER_ADDR_ERROR (-16)
  30. #define SD_DATA_XFER_ERROR (-17)
  31. #define SD_WRITE_SIZE_ERROR (-18)
  32. #define SD_CMD_STATUS_UPDATE_ERR (-19)
  33. #define SD_CMD12_ERROR (-20)
  34. #define SD_CMD_DATA_ERROR (-21)
  35. #define SD_CMD_TIMEOUT (-22)
  36. #define SD_CMD_NO_RESPONSE (-22)
  37. #define SD_CMD_ABORT_ERROR (-23)
  38. #define SD_CMD_INVALID (-24)
  39. #define SD_CMD_RESUME_ERROR (-25)
  40. #define SD_CMD_ERR_INVALID_RESPONSE (-26)
  41. #define SD_WAIT_TIMEOUT (-27)
  42. #define SD_READ_TIMEOUT (-28)
  43. #define SD_CEATA_REST_ERROR (-29)
  44. #define SD_INIT_CAED_FAILED (-30)
  45. #define SD_ERROR_CLOCK_OFFLIMIT (-31)
  46. #define SD_INV_SLOT (-32)
  47. #define SD_NOR_INTERRUPTS 0x000000FF
  48. #define SD_ERR_INTERRUPTS 0x03FF0000
  49. #define SD_CMD_ERROR_INT 0x010F0000
  50. #define SD_DAT_ERROR_INT 0x02F00000
  51. #define SD_DAT_TIMEOUT 0x00100000
  52. /* Operation modes */
  53. #define SD_PIO_MODE 0
  54. #define SD_INT_MODE 1
  55. /* Support both ADMA and SDMA (for version 2.0 and above) */
  56. #define SD_DMA_OFF 0
  57. #define SD_DMA_SDMA 1
  58. #define SD_DMA_ADMA 2
  59. #define SD_NORMAL_SPEED 0
  60. #define SD_HIGH_SPEED 1
  61. #define SD_XFER_CARD_TO_HOST 3
  62. #define SD_XFER_HOST_TO_CARD 4
  63. #define SD_CARD_DETECT_AUTO 0
  64. #define SD_CARD_DETECT_SD 1
  65. #define SD_CARD_DETECT_SDIO 2
  66. #define SD_CARD_DETECT_MMC 3
  67. #define SD_CARD_DETECT_CEATA 4
  68. #define SD_ABORT_SYNC_MODE 0
  69. #define SD_ABORT_ASYNC_MODE 1
  70. #define SD_CMD_ERROR_FLAGS (0x18F << 16)
  71. #define SD_DATA_ERROR_FLAGS (0x70 << 16)
  72. #define SD_AUTO_CMD12_ERROR_FLAGS (0x9F)
  73. #define SD_CARD_STATUS_ERROR 0x10000000
  74. #define SD_CMD_MISSING 0x80000000
  75. #define SD_ERROR_INT 0x8000
  76. #define SD_TRAN_HIGH_SPEED 0x32
  77. #define SD_CARD_HIGH_CAPACITY 0x40000000
  78. #define SD_CARD_POWER_UP_STATUS 0x80000000
  79. #define SD_HOST_CORE_TIMEOUT 0x0E
  80. /* SD CARD and Host Controllers bus width */
  81. #define SD_BUS_DATA_WIDTH_1BIT 0x00
  82. #define SD_BUS_DATA_WIDTH_4BIT 0x02
  83. #define SD_BUS_DATA_WIDTH_8BIT 0x20
  84. /* dma boundary settings */
  85. #define SD_DMA_BOUNDARY_4K 0
  86. #define SD_DMA_BOUNDARY_8K (1 << 12)
  87. #define SD_DMA_BOUNDARY_16K (2 << 12)
  88. #define SD_DMA_BOUNDARY_32K (3 << 12)
  89. #define SD_DMA_BOUNDARY_64K (4 << 12)
  90. #define SD_DMA_BOUNDARY_128K (5 << 12)
  91. #define SD_DMA_BOUNDARY_256K (6 << 12)
  92. #define SD_DMA_BOUNDARY_512K (7 << 12)
  93. #define SD_CMDR_CMD_NORMAL 0x00000000
  94. #define SD_CMDR_CMD_SUSPEND 0x00400000
  95. #define SD_CMDR_CMD_RESUME 0x00800000
  96. #define SD_CMDR_CMD_ABORT 0x00c00000
  97. #define SD_CMDR_RSP_TYPE_NONE 0x0
  98. #define SD_CMDR_RSP_TYPE_R2 0x1
  99. #define SD_CMDR_RSP_TYPE_R3_4 0x2
  100. #define SD_CMDR_RSP_TYPE_R1_5_6 0x2
  101. #define SD_CMDR_RSP_TYPE_R1b_5b 0x3
  102. #define SD_CMDR_RSP_TYPE_S 16
  103. struct sd_ctrl_info {
  104. uint32_t blkReg; /* current block register cache value */
  105. uint32_t cmdReg; /* current command register cache value */
  106. uint32_t argReg; /* current argument register cache value */
  107. uint32_t cmdIndex; /* current command index */
  108. uint32_t cmdStatus; /* current command status, cmd/data compelete */
  109. uint16_t rca; /* relative card address */
  110. uint32_t ocr; /* operation codition */
  111. uint32_t eventList; /* events list */
  112. uint32_t blkGapEnable;
  113. uint32_t capability; /* controller's capbilities */
  114. uint32_t maxCurrent; /* maximum current supported */
  115. uint32_t present; /* if card is inserted or removed */
  116. uint32_t version; /* SD spec version 1.0 or 2.0 */
  117. uint32_t vendor; /* vendor number */
  118. uintptr_t sdRegBaseAddr; /* sdio control registers */
  119. uintptr_t hostRegBaseAddr; /* SD Host control registers */
  120. };
  121. struct sd_cfg {
  122. uint32_t mode; /* interrupt or polling */
  123. uint32_t dma; /* dma enabled or disabled */
  124. uint32_t retryLimit; /* command retry limit */
  125. uint32_t speedMode; /* speed mode, 0 standard, 1 high speed */
  126. uint32_t voltage; /* voltage level */
  127. uint32_t blockSize; /* access block size (512 for HC card) */
  128. uint32_t dmaBoundary; /* dma address boundary */
  129. uint32_t detSignal; /* card det signal src, for test purpose only */
  130. uint32_t rdWaiting;
  131. uint32_t wakeupOut;
  132. uint32_t wakeupIn;
  133. uint32_t wakeupInt;
  134. uint32_t wfe_retry;
  135. uint32_t gapInt;
  136. uint32_t readWait;
  137. uint32_t led;
  138. };
  139. struct sd_dev {
  140. struct sd_cfg cfg; /* SD configuration */
  141. struct sd_ctrl_info ctrl; /* SD info */
  142. };
  143. int32_t chal_sd_start(CHAL_HANDLE *sdHandle, uint32_t mode,
  144. uint32_t sdBase, uint32_t hostBase);
  145. int32_t chal_sd_config(CHAL_HANDLE *sdHandle, uint32_t speed,
  146. uint32_t retry, uint32_t boundary,
  147. uint32_t blkSize, uint32_t dma);
  148. int32_t chal_sd_stop(void);
  149. int32_t chal_sd_set_dma(CHAL_HANDLE *sdHandle, uint32_t mode);
  150. uintptr_t chal_sd_get_dma_addr(CHAL_HANDLE *handle);
  151. int32_t chal_sd_config_bus_width(CHAL_HANDLE *sdHandle, int32_t width);
  152. int32_t chal_sd_send_cmd(CHAL_HANDLE *sdHandle, uint32_t cmdIndex,
  153. uint32_t arg, uint32_t options);
  154. int32_t chal_sd_set_dma_addr(CHAL_HANDLE *sdHandle, uintptr_t address);
  155. int32_t chal_sd_set_clock(CHAL_HANDLE *sdHandle,
  156. uint32_t div_ctrl_setting, uint32_t on);
  157. uint32_t chal_sd_freq_2_div_ctrl_setting(uint32_t desired_freq);
  158. int32_t chal_sd_setup_xfer(CHAL_HANDLE *sdHandle, uint8_t *data,
  159. uint32_t length, int32_t dir);
  160. int32_t chal_sd_write_buffer(CHAL_HANDLE *sdHandle, uint32_t length,
  161. uint8_t *data);
  162. int32_t chal_sd_read_buffer(CHAL_HANDLE *sdHandle, uint32_t length,
  163. uint8_t *data);
  164. int32_t chal_sd_reset_line(CHAL_HANDLE *sdHandle, uint32_t line);
  165. int32_t chal_sd_get_response(CHAL_HANDLE *sdHandle, uint32_t *resp);
  166. int32_t chal_sd_clear_pending_irq(CHAL_HANDLE *sdHandle);
  167. int32_t chal_sd_get_irq_status(CHAL_HANDLE *sdHandle);
  168. int32_t chal_sd_clear_irq(CHAL_HANDLE *sdHandle, uint32_t mask);
  169. uint32_t chal_sd_get_present_status(CHAL_HANDLE *sdHandle);
  170. int32_t chal_sd_get_atuo12_error(CHAL_HANDLE *sdHandle);
  171. void chal_sd_set_speed(CHAL_HANDLE *sdHandle, uint32_t speed);
  172. int32_t chal_sd_check_cap(CHAL_HANDLE *sdHandle, uint32_t cap);
  173. void chal_sd_set_irq_signal(CHAL_HANDLE *sdHandle, uint32_t mask,
  174. uint32_t state);
  175. void chal_sd_dump_fifo(CHAL_HANDLE *sdHandle);
  176. #endif /* CHAL_SD_H */