sd_mmc.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2015, 2016 Freescale Semiconductor, Inc.
  3. * Copyright 2017-2021 NXP
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. *
  7. */
  8. #ifndef SD_MMC_H
  9. #define SD_MMC_H
  10. #include <lib/mmio.h>
  11. /* operating freq */
  12. #define CARD_IDENTIFICATION_FREQ 400000
  13. #define SD_SS_25MHZ 20000000
  14. #define SD_HS_50MHZ 40000000
  15. #define MMC_SS_20MHZ 15000000
  16. #define MMC_HS_26MHZ 20000000
  17. #define MMC_HS_52MHZ 40000000
  18. /* Need to check this value ? */
  19. #define MAX_PLATFORM_CLOCK 800000000
  20. /* eSDHC system control register defines */
  21. #define ESDHC_SYSCTL_DTOCV(t) (((t) & 0xF) << 16)
  22. #define ESDHC_SYSCTL_SDCLKFS(f) (((f) & 0xFF) << 8)
  23. #define ESDHC_SYSCTL_DVS(d) (((d) & 0xF) << 4)
  24. #define ESDHC_SYSCTL_SDCLKEN (0x00000008)
  25. #define ESDHC_SYSCTL_RSTA (0x01000000)
  26. /* Data timeout counter value. SDHC_CLK x 227 */
  27. #define TIMEOUT_COUNTER_SDCLK_2_27 0xE
  28. #define ESDHC_SYSCTL_INITA 0x08000000
  29. /* eSDHC interrupt status enable register defines */
  30. #define ESDHC_IRQSTATEN_CINS 0x00000040
  31. #define ESDHC_IRQSTATEN_BWR 0x00000010
  32. /* eSDHC interrupt status register defines */
  33. #define ESDHC_IRQSTAT_DMAE (0x10000000)
  34. #define ESDHC_IRQSTAT_AC12E (0x01000000)
  35. #define ESDHC_IRQSTAT_DEBE (0x00400000)
  36. #define ESDHC_IRQSTAT_DCE (0x00200000)
  37. #define ESDHC_IRQSTAT_DTOE (0x00100000)
  38. #define ESDHC_IRQSTAT_CIE (0x00080000)
  39. #define ESDHC_IRQSTAT_CEBE (0x00040000)
  40. #define ESDHC_IRQSTAT_CCE (0x00020000)
  41. #define ESDHC_IRQSTAT_CTOE (0x00010000)
  42. #define ESDHC_IRQSTAT_CINT (0x00000100)
  43. #define ESDHC_IRQSTAT_CRM (0x00000080)
  44. #define ESDHC_IRQSTAT_CINS (0x00000040)
  45. #define ESDHC_IRQSTAT_BRR (0x00000020)
  46. #define ESDHC_IRQSTAT_BWR (0x00000010)
  47. #define ESDHC_IRQSTAT_DINT (0x00000008)
  48. #define ESDHC_IRQSTAT_BGE (0x00000004)
  49. #define ESDHC_IRQSTAT_TC (0x00000002)
  50. #define ESDHC_IRQSTAT_CC (0x00000001)
  51. #define ESDHC_IRQSTAT_CMD_ERR (ESDHC_IRQSTAT_CIE |\
  52. ESDHC_IRQSTAT_CEBE |\
  53. ESDHC_IRQSTAT_CCE)
  54. #define ESDHC_IRQSTAT_DATA_ERR (ESDHC_IRQSTAT_DEBE |\
  55. ESDHC_IRQSTAT_DCE |\
  56. ESDHC_IRQSTAT_DTOE)
  57. #define ESDHC_IRQSTAT_CLEAR_ALL (0xFFFFFFFF)
  58. /* eSDHC present state register defines */
  59. #define ESDHC_PRSSTAT_CLSL 0x00800000
  60. #define ESDHC_PRSSTAT_WPSPL 0x00080000
  61. #define ESDHC_PRSSTAT_CDPL 0x00040000
  62. #define ESDHC_PRSSTAT_CINS 0x00010000
  63. #define ESDHC_PRSSTAT_BREN 0x00000800
  64. #define ESDHC_PRSSTAT_BWEN 0x00000400
  65. #define ESDHC_PRSSTAT_RTA 0x00000200
  66. #define ESDHC_PRSSTAT_WTA 0x00000100
  67. #define ESDHC_PRSSTAT_SDOFF 0x00000080
  68. #define ESDHC_PRSSTAT_PEROFF 0x00000040
  69. #define ESDHC_PRSSTAT_HCKOFF 0x00000020
  70. #define ESDHC_PRSSTAT_IPGOFF 0x00000010
  71. #define ESDHC_PRSSTAT_DLA 0x00000004
  72. #define ESDHC_PRSSTAT_CDIHB 0x00000002
  73. #define ESDHC_PRSSTAT_CIHB 0x00000001
  74. /* eSDHC protocol control register defines */
  75. #define ESDHC_PROCTL_EMODE_LE 0x00000020
  76. #define ESDHC_PROCTL_DTW_1BIT 0x00000000
  77. #define ESDHC_PROCTL_DTW_4BIT 0x00000002
  78. #define ESDHC_PROCTL_DTW_8BIT 0x00000004
  79. /* Watermark Level Register (WML) */
  80. #define ESDHC_WML_RD_WML(w) ((w) & 0x7F)
  81. #define ESDHC_WML_WR_WML(w) (((w) & 0x7F) << 16)
  82. #define ESDHC_WML_RD_BRST(w) (((w) & 0xF) << 8)
  83. #define ESDHC_WML_WR_BRST(w) (((w) & 0xF) << 24)
  84. #define ESDHC_WML_WR_BRST_MASK (0x0F000000)
  85. #define ESDHC_WML_RD_BRST_MASK (0x00000F00)
  86. #define ESDHC_WML_RD_WML_MASK (0x0000007F)
  87. #define ESDHC_WML_WR_WML_MASK (0x007F0000)
  88. #define WML_512_BYTES (0x0)
  89. #define BURST_128_BYTES (0x0)
  90. /* eSDHC control register define */
  91. #define ESDHC_DCR_SNOOP 0x00000040
  92. /* ESDHC Block attributes register */
  93. #define ESDHC_BLKATTR_BLKCNT(c) (((c) & 0xffff) << 16)
  94. #define ESDHC_BLKATTR_BLKSZE(s) ((s) & 0xfff)
  95. /* Transfer Type Register */
  96. #define ESDHC_XFERTYP_CMD(c) (((c) & 0x3F) << 24)
  97. #define ESDHC_XFERTYP_CMDTYP_NORMAL (0x0)
  98. #define ESDHC_XFERTYP_CMDTYP_SUSPEND (0x00400000)
  99. #define ESDHC_XFERTYP_CMDTYP_RESUME (0x00800000)
  100. #define ESDHC_XFERTYP_CMDTYP_ABORT (0x00C00000)
  101. #define ESDHC_XFERTYP_DPSEL (0x00200000)
  102. #define ESDHC_XFERTYP_CICEN (0x00100000)
  103. #define ESDHC_XFERTYP_CCCEN (0x00080000)
  104. #define ESDHC_XFERTYP_RSPTYP_NONE (0x0)
  105. #define ESDHC_XFERTYP_RSPTYP_136 (0x00010000)
  106. #define ESDHC_XFERTYP_RSPTYP_48 (0x00020000)
  107. #define ESDHC_XFERTYP_RSPTYP_48_BUSY (0x00030000)
  108. #define ESDHC_XFERTYP_MSBSEL (0x00000020)
  109. #define ESDHC_XFERTYP_DTDSEL (0x00000010)
  110. #define ESDHC_XFERTYP_AC12EN (0x00000004)
  111. #define ESDHC_XFERTYP_BCEN (0x00000002)
  112. #define ESDHC_XFERTYP_DMAEN (0x00000001)
  113. #define MMC_VDD_HIGH_VOLTAGE 0x00000100
  114. /* command index */
  115. #define CMD0 0
  116. #define CMD1 1
  117. #define CMD2 2
  118. #define CMD3 3
  119. #define CMD5 5
  120. #define CMD6 6
  121. #define CMD7 7
  122. #define CMD8 8
  123. #define CMD9 9
  124. #define CMD12 12
  125. #define CMD13 13
  126. #define CMD14 14
  127. #define CMD16 16
  128. #define CMD17 17
  129. #define CMD18 18
  130. #define CMD19 19
  131. #define CMD24 24
  132. #define CMD41 41
  133. #define CMD42 42
  134. #define CMD51 51
  135. #define CMD55 55
  136. #define CMD56 56
  137. #define ACMD6 CMD6
  138. #define ACMD13 CMD13
  139. #define ACMD41 CMD41
  140. #define ACMD42 CMD42
  141. #define ACMD51 CMD51
  142. /* commands abbreviations */
  143. #define CMD_GO_IDLE_STATE CMD0
  144. #define CMD_MMC_SEND_OP_COND CMD1
  145. #define CMD_ALL_SEND_CID CMD2
  146. #define CMD_SEND_RELATIVE_ADDR CMD3
  147. #define CMD_SET_DSR CMD4
  148. #define CMD_SWITCH_FUNC CMD6
  149. #define CMD_SELECT_CARD CMD7
  150. #define CMD_DESELECT_CARD CMD7
  151. #define CMD_SEND_IF_COND CMD8
  152. #define CMD_MMC_SEND_EXT_CSD CMD8
  153. #define CMD_SEND_CSD CMD9
  154. #define CMD_SEND_CID CMD10
  155. #define CMD_STOP_TRANSMISSION CMD12
  156. #define CMD_SEND_STATUS CMD13
  157. #define CMD_BUS_TEST_R CMD14
  158. #define CMD_GO_INACTIVE_STATE CMD15
  159. #define CMD_SET_BLOCKLEN CMD16
  160. #define CMD_READ_SINGLE_BLOCK CMD17
  161. #define CMD_READ_MULTIPLE_BLOCK CMD18
  162. #define CMD_WRITE_SINGLE_BLOCK CMD24
  163. #define CMD_BUS_TEST_W CMD19
  164. #define CMD_APP_CMD CMD55
  165. #define CMD_GEN_CMD CMD56
  166. #define CMD_SET_BUS_WIDTH ACMD6
  167. #define CMD_SD_STATUS ACMD13
  168. #define CMD_SD_SEND_OP_COND ACMD41
  169. #define CMD_SET_CLR_CARD_DETECT ACMD42
  170. #define CMD_SEND_SCR ACMD51
  171. /* MMC card spec version */
  172. #define MMC_CARD_VERSION_1_2 0
  173. #define MMC_CARD_VERSION_1_4 1
  174. #define MMC_CARD_VERSION_2_X 2
  175. #define MMC_CARD_VERSION_3_X 3
  176. #define MMC_CARD_VERSION_4_X 4
  177. /* SD Card Spec Version */
  178. /* May need to add version 3 here? */
  179. #define SD_CARD_VERSION_1_0 0
  180. #define SD_CARD_VERSION_1_10 1
  181. #define SD_CARD_VERSION_2_0 2
  182. /* card types */
  183. #define MMC_CARD 0
  184. #define SD_CARD 1
  185. #define NOT_SD_CARD MMC_CARD
  186. /* Card rca */
  187. #define SD_MMC_CARD_RCA 0x1
  188. #define BLOCK_LEN_512 512
  189. /* card state */
  190. #define STATE_IDLE 0
  191. #define STATE_READY 1
  192. #define STATE_IDENT 2
  193. #define STATE_STBY 3
  194. #define STATE_TRAN 4
  195. #define STATE_DATA 5
  196. #define STATE_RCV 6
  197. #define STATE_PRG 7
  198. #define STATE_DIS 8
  199. /* Card OCR register */
  200. /* VDD voltage window 1,65 to 1.95 */
  201. #define MMC_OCR_VDD_165_195 0x00000080
  202. /* VDD voltage window 2.7-2.8 */
  203. #define MMC_OCR_VDD_FF8 0x00FF8000
  204. #define MMC_OCR_CCS 0x40000000/* Card Capacity */
  205. #define MMC_OCR_BUSY 0x80000000/* busy bit */
  206. #define SD_OCR_HCS 0x40000000/* High capacity host */
  207. #define MMC_OCR_SECTOR_MODE 0x40000000/* Access Mode as Sector */
  208. /* mmc Switch function */
  209. #define SET_EXT_CSD_HS_TIMING 0x03B90100/* set High speed */
  210. /* check supports switching or not */
  211. #define SD_SWITCH_FUNC_CHECK_MODE 0x00FFFFF1
  212. #define SD_SWITCH_FUNC_SWITCH_MODE 0x80FFFFF1/* switch */
  213. #define SD_SWITCH_FUNC_HIGH_SPEED 0x02/* HIGH SPEED FUNC */
  214. #define SWITCH_ERROR 0x00000080
  215. /* errors in sending commands */
  216. #define RESP_TIMEOUT 0x1
  217. #define COMMAND_ERROR 0x2
  218. /* error in response */
  219. #define R1_ERROR (1 << 19)
  220. #define R1_CURRENT_STATE(x) (((x) & 0x00001E00) >> 9)
  221. /* Host Controller Capabilities */
  222. #define ESDHC_HOSTCAPBLT_DMAS (0x00400000)
  223. /* SD/MMC memory map */
  224. struct esdhc_regs {
  225. uint32_t dsaddr; /* dma system address */
  226. uint32_t blkattr; /* Block attributes */
  227. uint32_t cmdarg; /* Command argument */
  228. uint32_t xfertyp; /* Command transfer type */
  229. uint32_t cmdrsp[4]; /* Command response0,1,2,3 */
  230. uint32_t datport; /* Data buffer access port */
  231. uint32_t prsstat; /* Present state */
  232. uint32_t proctl; /* Protocol control */
  233. uint32_t sysctl; /* System control */
  234. uint32_t irqstat; /* Interrupt status */
  235. uint32_t irqstaten; /* Interrupt status enable */
  236. uint32_t irqsigen; /* Interrupt signal enable */
  237. uint32_t autoc12err; /* Auto CMD12 status */
  238. uint32_t hostcapblt; /* Host controller capabilities */
  239. uint32_t wml; /* Watermark level */
  240. uint32_t res1[2];
  241. uint32_t fevt; /* Force event */
  242. uint32_t res2;
  243. uint32_t adsaddrl;
  244. uint32_t adsaddrh;
  245. uint32_t res3[39];
  246. uint32_t hostver; /* Host controller version */
  247. uint32_t res4;
  248. uint32_t dmaerr; /* DMA error address */
  249. uint32_t dmaerrh; /* DMA error address high */
  250. uint32_t dmaerrattr; /* DMA error atrribute */
  251. uint32_t res5;
  252. uint32_t hostcapblt2;/* Host controller capabilities2 */
  253. uint32_t res6[2];
  254. uint32_t tcr; /* Tuning control */
  255. uint32_t res7[7];
  256. uint32_t dirctrl; /* Direction control */
  257. uint32_t ccr; /* Clock control */
  258. uint32_t res8[177];
  259. uint32_t ctl; /* Control register */
  260. };
  261. /* SD/MMC card attributes */
  262. struct card_attributes {
  263. uint32_t type; /* sd or mmc card */
  264. uint32_t version; /* version */
  265. uint32_t block_len; /* block length */
  266. uint32_t bus_freq; /* sdhc bus frequency */
  267. uint16_t rca; /* relative card address */
  268. uint8_t is_high_capacity; /* high capacity */
  269. };
  270. struct mmc {
  271. struct esdhc_regs *esdhc_regs;
  272. struct card_attributes card;
  273. uint32_t block_len;
  274. uint32_t voltages_caps; /* supported voltaes */
  275. uint32_t dma_support; /* DMA support */
  276. };
  277. enum cntrl_num {
  278. SDHC1 = 0,
  279. SDHC2
  280. };
  281. int sd_emmc_init(uintptr_t *block_dev_spec,
  282. uintptr_t nxp_esdhc_addr,
  283. size_t nxp_sd_block_offset,
  284. size_t nxp_sd_block_size,
  285. bool card_detect);
  286. int esdhc_emmc_init(struct mmc *mmc, bool card_detect);
  287. int esdhc_read(struct mmc *mmc, uint32_t src_offset, uintptr_t dst,
  288. size_t size);
  289. int esdhc_write(struct mmc *mmc, uintptr_t src, uint32_t dst_offset,
  290. size_t size);
  291. #ifdef NXP_ESDHC_BE
  292. #define esdhc_in32(a) bswap32(mmio_read_32((uintptr_t)(a)))
  293. #define esdhc_out32(a, v) mmio_write_32((uintptr_t)(a), bswap32(v))
  294. #elif defined(NXP_ESDHC_LE)
  295. #define esdhc_in32(a) mmio_read_32((uintptr_t)(a))
  296. #define esdhc_out32(a, v) mmio_write_32((uintptr_t)(a), (v))
  297. #else
  298. #error Please define CCSR ESDHC register endianness
  299. #endif
  300. #endif /*SD_MMC_H*/