hpm_sdmmc_card.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2021-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_SDMMC_CARD_H
  8. #define HPM_SDMMC_CARD_H
  9. #include <stdint.h>
  10. /**
  11. * @brief Common SD/MMC commands
  12. */
  13. enum {
  14. sdmmc_cmd_go_idle_state = 0,
  15. sdmmc_cmd_all_send_cid = 2,
  16. sdmmc_cmd_set_dsr = 4,
  17. sdmmc_cmd_select_card = 7,
  18. sdmmc_cmd_send_csd = 9,
  19. sdmmc_cmd_send_cid = 10,
  20. sdmmc_cmd_stop_transmission = 12,
  21. sdmmc_cmd_send_status = 13,
  22. sdmmc_cmd_go_inactive_state = 15,
  23. sdmmc_cmd_set_block_length = 16,
  24. sdmmc_cmd_read_single_block = 17,
  25. sdmmc_cmd_read_multiple_block = 18,
  26. sdmmc_cmd_set_block_count = 23,
  27. sdmmc_cmd_write_single_block = 24,
  28. sdmmc_cmd_write_multiple_block = 25,
  29. sdmmc_cmd_program_csd = 27,
  30. sdmmc_cmd_set_write_protect = 29,
  31. sdmmc_cmd_clear_write_protect = 30,
  32. sdmmc_cmd_erase = 38,
  33. sdmmc_cmd_lock_unlock = 42,
  34. sdmmc_cmd_app_cmd = 55,
  35. sdmmc_cmd_general_cmd = 56,
  36. sdmmc_cmd_read_ocr = 58,
  37. };
  38. /**
  39. * @brief SD Card specific commands
  40. */
  41. enum {
  42. sd_acmd_set_bus_width = 6,
  43. sd_acmd_sd_status = 13,
  44. sd_acmd_set_num_wr_blocks = 22,
  45. sd_acmd_set_wr_blk_erase_count = 23,
  46. sd_acmd_sd_send_op_cond = 41,
  47. sd_acmd_set_clear_card_detect = 42,
  48. sd_acmd_send_scr = 51,
  49. sd_cmd_all_send_cid = 2,
  50. sd_cmd_send_relative_addr = 3,
  51. sd_cmd_switch = 6,
  52. sd_cmd_send_if_cond = 8,
  53. sd_cmd_send_csd = 9,
  54. sd_voltage_switch = 11,
  55. sd_cmd_send_tuning_block = 19,
  56. sd_cmd_erase_start = 32,
  57. sd_cmd_erase_end = 33,
  58. sd_cmd_crc_option = 59,
  59. };
  60. /**
  61. * @brief MMC specific commands
  62. */
  63. enum {
  64. emmc_cmd_send_op_cond = 1,
  65. emmc_cmd_all_send_cid = sdmmc_cmd_all_send_cid,
  66. emmc_cmd_set_relative_addr = 3,
  67. emmc_cmd_set_dsr = sdmmc_cmd_set_dsr,
  68. emmc_cmd_sleep_awake = 5,
  69. emmc_cmd_switch = 6,
  70. emmc_cmd_select = sdmmc_cmd_select_card,
  71. emmc_cmd_send_ext_csd = 8,
  72. emmc_cmd_send_csd = sdmmc_cmd_send_csd,
  73. emmc_cmd_send_cid = sdmmc_cmd_send_cid,
  74. emmc_cmd_stop_transmission = sdmmc_cmd_stop_transmission,
  75. emmc_cmd_send_status = sdmmc_cmd_send_status,
  76. emmc_cmd_bus_test = 14,
  77. emmc_cmd_go_inactive_state = sdmmc_cmd_go_inactive_state,
  78. emmc_cmd_set_block_length = sdmmc_cmd_set_block_length,
  79. emmc_cmd_read_single_block = sdmmc_cmd_read_single_block,
  80. emmc_cmd_read_multiple_block = sdmmc_cmd_read_multiple_block,
  81. emmc_cmd_send_tuning_block = 21,
  82. emmc_cmd_set_block_count = sdmmc_cmd_set_block_count,
  83. emmc_cmd_write_single_block = sdmmc_cmd_write_single_block,
  84. emmc_cmd_write_multiple_block = sdmmc_cmd_write_multiple_block,
  85. emmc_cmd_program_cid = 26,
  86. emmc_cmd_program_csd = sdmmc_cmd_program_csd,
  87. emmc_cmd_set_time = 49,
  88. emmc_cmd_erase_group_start = 35,
  89. emmc_cmd_erase_group_end = 36,
  90. emmc_cmd_erase = sdmmc_cmd_erase,
  91. emmc_cmd_set_write_prot = 28,
  92. emmc_cmd_clear_write_prot = 29,
  93. emmc_cmd_send_write_prot = 30,
  94. emmc_cmd_send_write_prot_type = 31,
  95. emmc_cmd_fast_io = 39,
  96. emmc_cmd_go_irq_state = 40,
  97. emmc_cmd_lock_unlock = sdmmc_cmd_lock_unlock,
  98. emmc_cmd_app_cmd = sdmmc_cmd_app_cmd,
  99. emmc_cmd_gen_cmd = sdmmc_cmd_general_cmd,
  100. emmc_cmd_protocol_read = 53,
  101. emmc_cmd_protocol_write = 54,
  102. emmc_cmd_queued_task_params = 44,
  103. emmc_cmd_queued_task_address = 45,
  104. emmc_cmd_execute_read_task = 46,
  105. emmc_cmd_execute_write_task = 47,
  106. emmc_cmd_cmdq_task_mgmt = 48,
  107. sdio_cmd_io_send_op_cond = 5,
  108. sdio_cmd_io_rw_direct = 52,
  109. sdio_cmd_io_rw_extend = 53,
  110. };
  111. /**
  112. * @brief SD/MMC R1 register information
  113. */
  114. typedef union {
  115. uint32_t status;
  116. struct {
  117. uint32_t : 3;
  118. uint32_t ake_seq_error: 1;
  119. uint32_t : 1;
  120. uint32_t app_cmd: 1;
  121. uint32_t fx_event: 1;
  122. uint32_t : 1;
  123. uint32_t ready_for_data: 1;
  124. uint32_t current_state: 4;
  125. uint32_t erase_reset: 1;
  126. uint32_t card_ecc_disabled: 1;
  127. uint32_t wp_erase_skip: 1;
  128. uint32_t csd_overwrite: 1;
  129. uint32_t : 2;
  130. uint32_t error: 1;
  131. uint32_t cc_error: 1;
  132. uint32_t card_ecc_failed: 1;
  133. uint32_t illegal_command: 1;
  134. uint32_t com_crc_error: 1;
  135. uint32_t lock_unlock_failed: 1;
  136. uint32_t card_is_locked: 1;
  137. uint32_t wp_violation: 1;
  138. uint32_t erase_param: 1;
  139. uint32_t erase_seq_error: 1;
  140. uint32_t block_len_error: 1;
  141. uint32_t address_error: 1;
  142. uint32_t out_of_range: 1;
  143. };
  144. } sdmmc_r1_status_t;
  145. /**
  146. * @brief SD Card Status Register Information
  147. */
  148. typedef struct {
  149. uint8_t bus_width;
  150. uint8_t secure_mode;
  151. uint16_t card_type;
  152. uint32_t protected_size;
  153. uint8_t speed_class;
  154. uint8_t performance_move;
  155. uint8_t uhs_speed_grade;
  156. uint8_t erase_offset;
  157. uint32_t au_size;
  158. uint32_t erase_timeout;
  159. uint32_t erase_size;
  160. uint32_t uhs_au_size;
  161. uint8_t reserved[3];
  162. } sd_status_t;
  163. /**
  164. * @brief SD CID Register Information
  165. */
  166. typedef union {
  167. struct {
  168. uint64_t : 1;
  169. uint64_t crc7: 7;
  170. uint64_t mdt: 12;
  171. uint64_t : 4;
  172. uint64_t psn: 32;
  173. uint64_t prv: 8;
  174. uint64_t pnm: 40;
  175. uint64_t oid: 16;
  176. uint64_t mid: 8;
  177. };
  178. uint32_t cid_words[4];
  179. } sd_cid_t;
  180. /**
  181. * @brief SD OCR register information
  182. */
  183. typedef union {
  184. uint32_t ocr_word;
  185. struct {
  186. uint32_t : 7;
  187. uint32_t low_voltage_range: 1;
  188. uint32_t : 7;
  189. uint32_t support_2v7_2v8: 1;
  190. uint32_t support_2v8_2v9: 1;
  191. uint32_t support_2v9_3v0: 1;
  192. uint32_t support_3v0_3v1: 1;
  193. uint32_t support_3v1_3v2: 1;
  194. uint32_t support_3v2_3v3: 1;
  195. uint32_t support_3v3_3v4: 1;
  196. uint32_t support_3v4_3v5: 1;
  197. uint32_t support_3v5_3v6: 1;
  198. uint32_t switching_to_1v8_accepted: 1;
  199. uint32_t : 2;
  200. uint32_t over_2tb_support: 1;
  201. uint32_t : 1;
  202. uint32_t uhs2_card_status: 1;
  203. uint32_t card_capacity_status: 1;
  204. uint32_t card_power_up_status: 1;
  205. };
  206. } sd_ocr_t;
  207. /**
  208. * @brief SD Card CSD register information
  209. */
  210. typedef struct _sd_csd {
  211. uint8_t csd_structure;
  212. uint8_t data_read_access_time1;
  213. uint8_t data_read_access_time2;
  214. uint8_t transfer_speed;
  215. uint16_t card_command_class;
  216. bool support_read_block_partial;
  217. bool support_write_block_misalignment;
  218. bool support_read_block_misalignment;
  219. bool is_dsr_implemented;
  220. bool support_sdxc;
  221. bool is_erase_block_enabled;
  222. bool is_write_protection_group_enabled;
  223. bool support_write_block_partial;
  224. bool support_file_format_group;
  225. bool support_copy;
  226. bool support_permanent_write_protect;
  227. bool support_temporary_write_protect;
  228. uint8_t read_current_vdd_min;
  229. uint8_t read_current_vdd_max;
  230. uint8_t write_current_vdd_min;
  231. uint8_t write_current_vdd_max;
  232. uint8_t device_size_multiplier;
  233. uint8_t write_speed_factor;
  234. uint8_t file_format;
  235. uint32_t device_size;
  236. uint32_t read_block_len;
  237. uint32_t erase_sector_size;
  238. uint32_t max_write_block_len;
  239. uint32_t write_protect_group_size;
  240. } sd_csd_t;
  241. /**
  242. * @brief SD Card SCR register information
  243. */
  244. typedef union {
  245. struct {
  246. uint32_t reserved;
  247. uint32_t support_cmd20: 1;
  248. uint32_t support_cmd23: 1;
  249. uint32_t support_cmd48_or_cmd49: 1;
  250. uint32_t support_cmd58_or_cmd59: 1;
  251. uint32_t : 2;
  252. uint32_t sd_specx: 4;
  253. uint32_t sd_spec4: 1;
  254. uint32_t ex_security: 4;
  255. uint32_t sd_spec3: 1;
  256. uint32_t sd_bus_widths: 4;
  257. uint32_t sd_security: 3;
  258. uint32_t data_stat_after_erase: 1;
  259. uint32_t sd_spec: 4;
  260. uint32_t scr_structure: 4;
  261. };
  262. uint32_t scr_word[2];
  263. } sd_scr_t;
  264. typedef enum {
  265. card_type_mmc = 0,
  266. card_type_sd_v1 = 1,
  267. card_type_sd_v2 = 2,
  268. card_type_sd_v2_hc = 4,
  269. } sdcard_type_t;
  270. #define SDMMC_BLOCK_SIZE_DEFAULT (512U)
  271. /**
  272. * @brief Dummy Byte for SD when Card works in SPI mode
  273. */
  274. #define SPISD_DUMMY_BYTE 0xFFU
  275. #define SPISD_START_TOKEN 0xFEU
  276. #define SPISD_START_MULTI_WRITE_TOKEN 0xFCU
  277. #define SPISD_END_MULTI_WRITE_TOKEN 0xFDU
  278. /**
  279. * @brief sdcard block size in SPI mode
  280. */
  281. #define SPI_SD_BLOCK_SIZE (512U)
  282. /**
  283. * @brief R1 register bits when CARD works in SPI mode
  284. */
  285. #define SPISD_R1_IDLE_FLAG (0x01)
  286. #define SPISD_R1_ERASE_RESET_FLAG (0x02)
  287. #define SPISD_R1_ILLEGAL_CMD_FLAG (0x04)
  288. #define SPISD_R1_CMD_CRC_FLAG (0x08)
  289. #define SPISD_R1_ERASE_SEQ_ERROR_FLAG (0x10)
  290. #define SPISD_R1_ADDR_ERROR_FLAG (0x20)
  291. #define SPISD_R1_PARAM_ERROR_FLAG (0x40)
  292. #define SPISD_R1_ZERO_FLAG (0x80)
  293. #endif /* HPM_SDMMC_CARD_H */