cdns_sdmmc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Copyright (c) 2022-2023, Intel Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdbool.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include <arch_helpers.h>
  12. #include <common/debug.h>
  13. #include <drivers/cadence/cdns_sdmmc.h>
  14. #include <drivers/delay_timer.h>
  15. #include <drivers/mmc.h>
  16. #include <lib/mmio.h>
  17. #include <lib/utils.h>
  18. /* Card busy and present */
  19. #define CARD_BUSY 1
  20. #define CARD_NOT_BUSY 0
  21. /* 500 ms delay to read the RINST register */
  22. #define DELAY_MS_SRS_READ 500
  23. #define DELAY_RES 10
  24. /* SRS12 error mask */
  25. #define SRS12_ERR_MASK 0xFFFF8000
  26. /* Check DV dfi_init val=0 */
  27. #define IO_MASK_END_DATA 0x0
  28. /* Check DV dfi_init val=2; DDR Mode */
  29. #define IO_MASK_END_DATA_DDR 0x2
  30. #define IO_MASK_START_DATA 0x0
  31. #define DATA_SELECT_OE_END_DATA 0x1
  32. #define TIMEOUT 100000
  33. /* General define */
  34. #define SDHC_REG_MASK UINT_MAX
  35. #define SD_HOST_BLOCK_SIZE 0x200
  36. #define DTCVVAL_DEFAULT_VAL 0xE
  37. #define CDMMC_DMA_MAX_BUFFER_SIZE 64*1024
  38. #define CDNSMMC_ADDRESS_MASK U(0x0f)
  39. #define CONFIG_CDNS_DESC_COUNT 8
  40. void cdns_init(void);
  41. int cdns_send_cmd(struct mmc_cmd *cmd);
  42. int cdns_set_ios(unsigned int clk, unsigned int width);
  43. int cdns_prepare(int lba, uintptr_t buf, size_t size);
  44. int cdns_read(int lba, uintptr_t buf, size_t size);
  45. int cdns_write(int lba, uintptr_t buf, size_t size);
  46. const struct mmc_ops cdns_sdmmc_ops = {
  47. .init = cdns_init,
  48. .send_cmd = cdns_send_cmd,
  49. .set_ios = cdns_set_ios,
  50. .prepare = cdns_prepare,
  51. .read = cdns_read,
  52. .write = cdns_write,
  53. };
  54. struct cdns_sdmmc_params cdns_params;
  55. struct cdns_sdmmc_combo_phy sdmmc_combo_phy_reg;
  56. struct cdns_sdmmc_sdhc sdmmc_sdhc_reg;
  57. #ifdef CONFIG_DMA_ADDR_T_64BIT
  58. struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT];
  59. #else
  60. struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT] __aligned(32);
  61. #endif
  62. bool data_cmd;
  63. int cdns_wait_ics(uint16_t timeout, uint32_t cdn_srs_res)
  64. {
  65. /* Clock for sdmclk and sdclk */
  66. uint32_t count = 0;
  67. uint32_t data = 0;
  68. /* Wait status command response ready */
  69. do {
  70. data = mmio_read_32(cdn_srs_res);
  71. count++;
  72. if (count >= timeout) {
  73. return -ETIMEDOUT;
  74. }
  75. } while ((data & (1 << SDMMC_CDN_ICS)) == 0);
  76. return 0;
  77. }
  78. int cdns_busy(void)
  79. {
  80. unsigned int data;
  81. data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS09);
  82. return (data & STATUS_DATA_BUSY) ? CARD_BUSY : CARD_NOT_BUSY;
  83. }
  84. int cdns_vol_reset(void)
  85. {
  86. /* Reset embedded card */
  87. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
  88. udelay(250);
  89. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (0 << SDMMC_CDN_BP));
  90. udelay(500);
  91. /* Turn on supply voltage */
  92. /* BVS = 7, BP = 1, BP2 only in UHS2 mode */
  93. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
  94. udelay(250);
  95. return 0;
  96. }
  97. void cdns_set_sdmmc_var(struct cdns_sdmmc_combo_phy *combo_phy_reg,
  98. struct cdns_sdmmc_sdhc *sdhc_reg)
  99. {
  100. /* Values are taken by the reference of cadence IP documents */
  101. combo_phy_reg->cp_clk_wr_delay = 0;
  102. combo_phy_reg->cp_clk_wrdqs_delay = 0;
  103. combo_phy_reg->cp_data_select_oe_end = 0;
  104. combo_phy_reg->cp_dll_bypass_mode = 1;
  105. combo_phy_reg->cp_dll_locked_mode = 0;
  106. combo_phy_reg->cp_dll_start_point = 0;
  107. combo_phy_reg->cp_gate_cfg_always_on = 1;
  108. combo_phy_reg->cp_io_mask_always_on = 0;
  109. combo_phy_reg->cp_io_mask_end = 0;
  110. combo_phy_reg->cp_io_mask_start = 0;
  111. combo_phy_reg->cp_rd_del_sel = 52;
  112. combo_phy_reg->cp_read_dqs_cmd_delay = 0;
  113. combo_phy_reg->cp_read_dqs_delay = 0;
  114. combo_phy_reg->cp_sw_half_cycle_shift = 0;
  115. combo_phy_reg->cp_sync_method = 1;
  116. combo_phy_reg->cp_underrun_suppress = 1;
  117. combo_phy_reg->cp_use_ext_lpbk_dqs = 1;
  118. combo_phy_reg->cp_use_lpbk_dqs = 1;
  119. combo_phy_reg->cp_use_phony_dqs = 1;
  120. combo_phy_reg->cp_use_phony_dqs_cmd = 1;
  121. sdhc_reg->sdhc_extended_rd_mode = 1;
  122. sdhc_reg->sdhc_extended_wr_mode = 1;
  123. sdhc_reg->sdhc_hcsdclkadj = 0;
  124. sdhc_reg->sdhc_idelay_val = 0;
  125. sdhc_reg->sdhc_rdcmd_en = 1;
  126. sdhc_reg->sdhc_rddata_en = 1;
  127. sdhc_reg->sdhc_rw_compensate = 9;
  128. sdhc_reg->sdhc_sdcfsh = 0;
  129. sdhc_reg->sdhc_sdcfsl = 1;
  130. sdhc_reg->sdhc_wrcmd0_dly = 1;
  131. sdhc_reg->sdhc_wrcmd0_sdclk_dly = 0;
  132. sdhc_reg->sdhc_wrcmd1_dly = 0;
  133. sdhc_reg->sdhc_wrcmd1_sdclk_dly = 0;
  134. sdhc_reg->sdhc_wrdata0_dly = 1;
  135. sdhc_reg->sdhc_wrdata0_sdclk_dly = 0;
  136. sdhc_reg->sdhc_wrdata1_dly = 0;
  137. sdhc_reg->sdhc_wrdata1_sdclk_dly = 0;
  138. }
  139. static int cdns_program_phy_reg(struct cdns_sdmmc_combo_phy *combo_phy_reg,
  140. struct cdns_sdmmc_sdhc *sdhc_reg)
  141. {
  142. uint32_t value = 0;
  143. int ret = 0;
  144. /* program PHY_DQS_TIMING_REG */
  145. value = (CP_USE_EXT_LPBK_DQS(combo_phy_reg->cp_use_ext_lpbk_dqs)) |
  146. (CP_USE_LPBK_DQS(combo_phy_reg->cp_use_lpbk_dqs)) |
  147. (CP_USE_PHONY_DQS(combo_phy_reg->cp_use_phony_dqs)) |
  148. (CP_USE_PHONY_DQS_CMD(combo_phy_reg->cp_use_phony_dqs_cmd));
  149. ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
  150. COMBO_PHY_REG + PHY_DQS_TIMING_REG, MMC_REG_BASE +
  151. SDHC_CDNS_HRS05, value);
  152. if (ret != 0) {
  153. return ret;
  154. }
  155. /* program PHY_GATE_LPBK_CTRL_REG */
  156. value = (CP_SYNC_METHOD(combo_phy_reg->cp_sync_method)) |
  157. (CP_SW_HALF_CYCLE_SHIFT(combo_phy_reg->cp_sw_half_cycle_shift)) |
  158. (CP_RD_DEL_SEL(combo_phy_reg->cp_rd_del_sel)) |
  159. (CP_UNDERRUN_SUPPRESS(combo_phy_reg->cp_underrun_suppress)) |
  160. (CP_GATE_CFG_ALWAYS_ON(combo_phy_reg->cp_gate_cfg_always_on));
  161. ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
  162. COMBO_PHY_REG + PHY_GATE_LPBK_CTRL_REG, MMC_REG_BASE +
  163. SDHC_CDNS_HRS05, value);
  164. if (ret != 0) {
  165. return ret;
  166. }
  167. /* program PHY_DLL_MASTER_CTRL_REG */
  168. value = (CP_DLL_BYPASS_MODE(combo_phy_reg->cp_dll_bypass_mode))
  169. | (CP_DLL_START_POINT(combo_phy_reg->cp_dll_start_point));
  170. ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
  171. COMBO_PHY_REG + PHY_DLL_MASTER_CTRL_REG, MMC_REG_BASE
  172. + SDHC_CDNS_HRS05, value);
  173. if (ret != 0) {
  174. return ret;
  175. }
  176. /* program PHY_DLL_SLAVE_CTRL_REG */
  177. value = (CP_READ_DQS_CMD_DELAY(combo_phy_reg->cp_read_dqs_cmd_delay))
  178. | (CP_CLK_WRDQS_DELAY(combo_phy_reg->cp_clk_wrdqs_delay))
  179. | (CP_CLK_WR_DELAY(combo_phy_reg->cp_clk_wr_delay))
  180. | (CP_READ_DQS_DELAY(combo_phy_reg->cp_read_dqs_delay));
  181. ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
  182. COMBO_PHY_REG + PHY_DLL_SLAVE_CTRL_REG, MMC_REG_BASE
  183. + SDHC_CDNS_HRS05, value);
  184. if (ret != 0) {
  185. return ret;
  186. }
  187. /* program PHY_CTRL_REG */
  188. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS04, COMBO_PHY_REG
  189. + PHY_CTRL_REG);
  190. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS05);
  191. /* phony_dqs_timing=0 */
  192. value &= ~(CP_PHONY_DQS_TIMING_MASK << CP_PHONY_DQS_TIMING_SHIFT);
  193. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS05, value);
  194. /* switch off DLL_RESET */
  195. do {
  196. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
  197. value |= SDHC_PHY_SW_RESET;
  198. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, value);
  199. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
  200. /* polling PHY_INIT_COMPLETE */
  201. } while ((value & SDHC_PHY_INIT_COMPLETE) != SDHC_PHY_INIT_COMPLETE);
  202. /* program PHY_DQ_TIMING_REG */
  203. combo_phy_reg->cp_io_mask_end = 0U;
  204. value = (CP_IO_MASK_ALWAYS_ON(combo_phy_reg->cp_io_mask_always_on))
  205. | (CP_IO_MASK_END(combo_phy_reg->cp_io_mask_end))
  206. | (CP_IO_MASK_START(combo_phy_reg->cp_io_mask_start))
  207. | (CP_DATA_SELECT_OE_END(combo_phy_reg->cp_data_select_oe_end));
  208. ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
  209. COMBO_PHY_REG + PHY_DQ_TIMING_REG, MMC_REG_BASE
  210. + SDHC_CDNS_HRS05, value);
  211. if (ret != 0) {
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. int cdns_read(int lba, uintptr_t buf, size_t size)
  217. {
  218. inv_dcache_range(buf, size);
  219. return 0;
  220. }
  221. void cdns_init(void)
  222. {
  223. /* Dummy function pointer for cdns_init. */
  224. }
  225. int cdns_prepare(int dma_start_addr, uintptr_t dma_buff, size_t size)
  226. {
  227. data_cmd = true;
  228. struct cdns_idmac_desc *desc;
  229. uint32_t desc_cnt, i;
  230. uint64_t desc_base;
  231. assert(((dma_buff & CDNSMMC_ADDRESS_MASK) == 0) &&
  232. (cdns_params.desc_size > 0) &&
  233. ((MMC_REG_BASE & MMC_BLOCK_MASK) == 0) &&
  234. ((cdns_params.desc_base & MMC_BLOCK_MASK) == 0) &&
  235. ((cdns_params.desc_size & MMC_BLOCK_MASK) == 0));
  236. flush_dcache_range(dma_buff, size);
  237. desc_cnt = (size + (CDMMC_DMA_MAX_BUFFER_SIZE) - 1) / (CDMMC_DMA_MAX_BUFFER_SIZE);
  238. assert(desc_cnt * sizeof(struct cdns_idmac_desc) < cdns_params.desc_size);
  239. if (desc_cnt > CONFIG_CDNS_DESC_COUNT) {
  240. ERROR("Requested data transfer length %ld is greater than configured length %d",
  241. size, (CONFIG_CDNS_DESC_COUNT * CDMMC_DMA_MAX_BUFFER_SIZE));
  242. return -EINVAL;
  243. }
  244. desc = (struct cdns_idmac_desc *)cdns_params.desc_base;
  245. desc_base = (uint64_t)desc;
  246. i = 0;
  247. while ((i + 1) < desc_cnt) {
  248. desc->attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
  249. desc->reserved = 0;
  250. desc->len = MAX_64KB_PAGE;
  251. desc->addr_lo = (dma_buff & UINT_MAX) + (CDMMC_DMA_MAX_BUFFER_SIZE * i);
  252. #if CONFIG_DMA_ADDR_T_64BIT == 1
  253. desc->addr_hi = (dma_buff >> 32) & 0xffffffff;
  254. #endif
  255. size -= CDMMC_DMA_MAX_BUFFER_SIZE;
  256. desc++;
  257. i++;
  258. }
  259. desc->attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA |
  260. ADMA_DESC_ATTR_END;
  261. desc->reserved = 0;
  262. desc->len = size;
  263. #if CONFIG_DMA_ADDR_T_64BIT == 1
  264. desc->addr_lo = (dma_buff & UINT_MAX) + (CDMMC_DMA_MAX_BUFFER_SIZE * i);
  265. desc->addr_hi = (dma_buff >> 32) & UINT_MAX;
  266. #else
  267. desc->addr_lo = (dma_buff & UINT_MAX);
  268. #endif
  269. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS22, (uint32_t)desc_base);
  270. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS23, (uint32_t)(desc_base >> 32));
  271. flush_dcache_range(cdns_params.desc_base,
  272. desc_cnt * CDMMC_DMA_MAX_BUFFER_SIZE);
  273. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS01,
  274. ((512 << BLOCK_SIZE) | ((size/512) << BLK_COUNT_CT) | SDMA_BUF));
  275. return 0;
  276. }
  277. static void cdns_host_set_clk(int clk)
  278. {
  279. uint32_t ret = 0;
  280. uint32_t sdclkfsval = 0;
  281. uint32_t dtcvval = DTCVVAL_DEFAULT_VAL;
  282. sdclkfsval = (cdns_params.clk_rate / 2000) / clk;
  283. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, 0);
  284. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
  285. (sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE));
  286. ret = cdns_wait_ics(5000, MMC_REG_BASE + SDHC_CDNS_SRS11);
  287. if (ret != 0U) {
  288. ERROR("Waiting SDMMC_CDN_ICS timeout");
  289. }
  290. /* Enable DLL reset */
  291. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) &
  292. ~SDHC_DLL_RESET_MASK);
  293. /* Set extended_wr_mode */
  294. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, (mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09)
  295. & SDHC_EXTENDED_WR_MODE_MASK) | (1 << EXTENDED_WR_MODE));
  296. /* Release DLL reset */
  297. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
  298. + SDHC_CDNS_HRS09) | 1);
  299. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
  300. + SDHC_CDNS_HRS09) | (3 << RDCMD_EN));
  301. do {
  302. mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
  303. } while (~mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) & (1 << 1));
  304. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
  305. (sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE) | (1 << SDMMC_CDN_SDCE));
  306. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS13, UINT_MAX);
  307. }
  308. int cdns_set_ios(unsigned int clk, unsigned int width)
  309. {
  310. switch (width) {
  311. case MMC_BUS_WIDTH_1:
  312. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), LEDC_OFF);
  313. break;
  314. case MMC_BUS_WIDTH_4:
  315. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), DTW_4BIT);
  316. break;
  317. case MMC_BUS_WIDTH_8:
  318. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), EDTW_8BIT);
  319. break;
  320. default:
  321. assert(0);
  322. break;
  323. }
  324. cdns_host_set_clk(clk);
  325. return 0;
  326. }
  327. int cdns_sdmmc_write_sd_host_reg(uint32_t addr, uint32_t data)
  328. {
  329. uint32_t value = 0;
  330. value = mmio_read_32(addr);
  331. value &= ~SDHC_REG_MASK;
  332. value |= data;
  333. mmio_write_32(addr, value);
  334. value = mmio_read_32(addr);
  335. if (value != data) {
  336. ERROR("SD host address is not set properly\n");
  337. return -ENXIO;
  338. }
  339. return 0;
  340. }
  341. int cdns_write(int lba, uintptr_t buf, size_t size)
  342. {
  343. return 0;
  344. }
  345. static int cdns_init_hrs_io(struct cdns_sdmmc_combo_phy *combo_phy_reg,
  346. struct cdns_sdmmc_sdhc *sdhc_reg)
  347. {
  348. uint32_t value = 0;
  349. int ret = 0;
  350. /* program HRS09, register 42 */
  351. value = (SDHC_RDDATA_EN(sdhc_reg->sdhc_rddata_en))
  352. | (SDHC_RDCMD_EN(sdhc_reg->sdhc_rdcmd_en))
  353. | (SDHC_EXTENDED_WR_MODE(sdhc_reg->sdhc_extended_wr_mode))
  354. | (SDHC_EXTENDED_RD_MODE(sdhc_reg->sdhc_extended_rd_mode));
  355. ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS09, value);
  356. if (ret != 0) {
  357. ERROR("Program HRS09 failed");
  358. return ret;
  359. }
  360. /* program HRS10, register 43 */
  361. value = (SDHC_HCSDCLKADJ(sdhc_reg->sdhc_hcsdclkadj));
  362. ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS10, value);
  363. if (ret != 0) {
  364. ERROR("Program HRS10 failed");
  365. return ret;
  366. }
  367. /* program HRS16, register 48 */
  368. value = (SDHC_WRDATA1_SDCLK_DLY(sdhc_reg->sdhc_wrdata1_sdclk_dly))
  369. | (SDHC_WRDATA0_SDCLK_DLY(sdhc_reg->sdhc_wrdata0_sdclk_dly))
  370. | (SDHC_WRCMD1_SDCLK_DLY(sdhc_reg->sdhc_wrcmd1_sdclk_dly))
  371. | (SDHC_WRCMD0_SDCLK_DLY(sdhc_reg->sdhc_wrcmd0_sdclk_dly))
  372. | (SDHC_WRDATA1_DLY(sdhc_reg->sdhc_wrdata1_dly))
  373. | (SDHC_WRDATA0_DLY(sdhc_reg->sdhc_wrdata0_dly))
  374. | (SDHC_WRCMD1_DLY(sdhc_reg->sdhc_wrcmd1_dly))
  375. | (SDHC_WRCMD0_DLY(sdhc_reg->sdhc_wrcmd0_dly));
  376. ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS16, value);
  377. if (ret != 0) {
  378. ERROR("Program HRS16 failed");
  379. return ret;
  380. }
  381. /* program HRS07, register 40 */
  382. value = (SDHC_RW_COMPENSATE(sdhc_reg->sdhc_rw_compensate))
  383. | (SDHC_IDELAY_VAL(sdhc_reg->sdhc_idelay_val));
  384. ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS07, value);
  385. if (ret != 0) {
  386. ERROR("Program HRS07 failed");
  387. return ret;
  388. }
  389. return ret;
  390. }
  391. static int cdns_hc_set_clk(struct cdns_sdmmc_params *cdn_sdmmc_dev_mode_params)
  392. {
  393. uint32_t ret = 0;
  394. uint32_t dtcvval, sdclkfsval;
  395. dtcvval = DTC_VAL;
  396. sdclkfsval = 0;
  397. if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_DS) ||
  398. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR12) ||
  399. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_SDR_BC)) {
  400. sdclkfsval = 4;
  401. } else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_HS) ||
  402. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR25) ||
  403. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_DDR50) ||
  404. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_SDR)) {
  405. sdclkfsval = 2;
  406. } else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR50) ||
  407. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_DDR) ||
  408. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS400) ||
  409. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS400es)) {
  410. sdclkfsval = 1;
  411. } else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR104) ||
  412. (cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS200)) {
  413. sdclkfsval = 0;
  414. }
  415. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, 0);
  416. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
  417. (sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE));
  418. ret = cdns_wait_ics(5000, MMC_REG_BASE + SDHC_CDNS_SRS11);
  419. if (ret != 0U) {
  420. ERROR("Waiting SDMMC_CDN_ICS timeout");
  421. return ret;
  422. }
  423. /* Enable DLL reset */
  424. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_HRS09), mmio_read_32(MMC_REG_BASE
  425. + SDHC_CDNS_HRS09) & ~SDHC_DLL_RESET_MASK);
  426. /* Set extended_wr_mode */
  427. mmio_write_32((MMC_REG_BASE + SDHC_CDNS_HRS09),
  428. (mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) & SDHC_EXTENDED_WR_MODE_MASK) |
  429. (1 << EXTENDED_WR_MODE));
  430. /* Release DLL reset */
  431. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
  432. + SDHC_CDNS_HRS09) | 1);
  433. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
  434. + SDHC_CDNS_HRS09) | (3 << RDCMD_EN));
  435. do {
  436. mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
  437. } while (~mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) & (1 << 1));
  438. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
  439. (sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE) | (1 << SDMMC_CDN_SDCE));
  440. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS13, UINT_MAX);
  441. return 0;
  442. }
  443. int cdns_reset(void)
  444. {
  445. uint32_t data = 0;
  446. uint32_t count = 0;
  447. uint32_t value = 0;
  448. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS11);
  449. value &= ~(0xFFFF);
  450. value |= 0x0;
  451. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, value);
  452. udelay(500);
  453. /* Software reset */
  454. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS00, 1);
  455. /* Wait status command response ready */
  456. do {
  457. data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS00);
  458. count++;
  459. if (count >= 5000) {
  460. return -ETIMEDOUT;
  461. }
  462. /* Wait for HRS00.SWR */
  463. } while ((data & 1) == 1);
  464. /* Step 1, switch on DLL_RESET */
  465. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
  466. value &= ~SDHC_PHY_SW_RESET;
  467. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, value);
  468. return 0;
  469. }
  470. int cdns_sd_host_init(struct cdns_sdmmc_combo_phy *mmc_combo_phy_reg,
  471. struct cdns_sdmmc_sdhc *mmc_sdhc_reg)
  472. {
  473. int ret = 0;
  474. ret = cdns_reset();
  475. if (ret != 0) {
  476. ERROR("Program phy reg init failed");
  477. return ret;
  478. }
  479. ret = cdns_program_phy_reg(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
  480. if (ret != 0) {
  481. ERROR("Program phy reg init failed");
  482. return ret;
  483. }
  484. ret = cdns_init_hrs_io(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
  485. if (ret != 0) {
  486. ERROR("Program init for HRS reg is failed");
  487. return ret;
  488. }
  489. ret = cdns_sd_card_detect();
  490. if (ret != 0) {
  491. ERROR("SD card does not detect");
  492. return ret;
  493. }
  494. ret = cdns_vol_reset();
  495. if (ret != 0) {
  496. ERROR("eMMC card reset failed");
  497. return ret;
  498. }
  499. ret = cdns_hc_set_clk(&cdns_params);
  500. if (ret != 0) {
  501. ERROR("hc set clk failed");
  502. return ret;
  503. }
  504. return 0;
  505. }
  506. void cdns_srs10_value_toggle(uint8_t write_val, uint8_t prev_val)
  507. {
  508. uint32_t data_op = 0U;
  509. data_op = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS10);
  510. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS10, (data_op & (prev_val << 0)));
  511. mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS10);
  512. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS10, data_op | (write_val << 0));
  513. }
  514. void cdns_srs11_srs15_config(uint32_t srs11_val, uint32_t srs15_val)
  515. {
  516. uint32_t data = 0U;
  517. data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS11);
  518. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (data | srs11_val));
  519. data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS15);
  520. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS15, (data | srs15_val));
  521. }
  522. int cdns_send_cmd(struct mmc_cmd *cmd)
  523. {
  524. uint32_t op = 0, ret = 0;
  525. uint8_t write_value = 0, prev_val = 0;
  526. uint32_t value;
  527. int32_t timeout;
  528. uint32_t cmd_indx;
  529. uint32_t status = 0, srs15_val = 0, srs11_val = 0;
  530. uint32_t status_check = 0;
  531. assert(cmd);
  532. cmd_indx = (cmd->cmd_idx) << COM_IDX;
  533. if (data_cmd) {
  534. switch (cmd->cmd_idx) {
  535. case SD_SWITCH:
  536. op = DATA_PRESENT;
  537. write_value = ADMA2_32 | DT_WIDTH;
  538. prev_val = ADMA2_32 | DT_WIDTH;
  539. cdns_srs10_value_toggle(write_value, prev_val);
  540. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  541. srs15_val = BIT_AD_64 | HV4E | V18SE;
  542. cdns_srs11_srs15_config(srs11_val, srs15_val);
  543. break;
  544. case SD_WRITE_SINGLE_BLOCK:
  545. case SD_READ_SINGLE_BLOCK:
  546. op = DATA_PRESENT;
  547. write_value = ADMA2_32 | HS_EN | DT_WIDTH | LEDC;
  548. prev_val = ADMA2_32 | HS_EN | DT_WIDTH;
  549. cdns_srs10_value_toggle(write_value, prev_val);
  550. srs15_val = PVE | BIT_AD_64 | HV4E | SDR104_MODE | V18SE;
  551. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  552. cdns_srs11_srs15_config(srs11_val, srs15_val);
  553. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS00, SAAR);
  554. break;
  555. case SD_WRITE_MULTIPLE_BLOCK:
  556. case SD_READ_MULTIPLE_BLOCK:
  557. op = DATA_PRESENT | AUTO_CMD_EN | MULTI_BLK_READ;
  558. write_value = ADMA2_32 | HS_EN | DT_WIDTH | LEDC;
  559. prev_val = ADMA2_32 | HS_EN | DT_WIDTH;
  560. cdns_srs10_value_toggle(write_value, prev_val);
  561. srs15_val = PVE | BIT_AD_64 | HV4E | SDR104_MODE | V18SE;
  562. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  563. cdns_srs11_srs15_config(srs11_val, srs15_val);
  564. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS00, SAAR);
  565. break;
  566. case SD_APP_SEND_SCR:
  567. op = DATA_PRESENT;
  568. write_value = ADMA2_32 | LEDC;
  569. prev_val = LEDC;
  570. cdns_srs10_value_toggle(write_value, prev_val);
  571. srs15_val = BIT_AD_64 | HV4E | V18SE;
  572. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  573. cdns_srs11_srs15_config(srs11_val, srs15_val);
  574. break;
  575. case SD_SEND_IF_COND:
  576. op = DATA_PRESENT | CMD_IDX_CHK_ENABLE;
  577. write_value = LEDC;
  578. prev_val = 0x0;
  579. cdns_srs10_value_toggle(write_value, prev_val);
  580. srs15_val = HV4E;
  581. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  582. cdns_srs11_srs15_config(srs11_val, srs15_val);
  583. break;
  584. default:
  585. write_value = LEDC;
  586. prev_val = 0x0;
  587. cdns_srs10_value_toggle(write_value, prev_val);
  588. op = 0;
  589. break;
  590. }
  591. } else {
  592. switch (cmd->cmd_idx) {
  593. case SD_GO_IDLE_STATE:
  594. write_value = LEDC;
  595. prev_val = 0x0;
  596. cdns_srs10_value_toggle(write_value, prev_val);
  597. srs15_val = HV4E;
  598. srs11_val = SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  599. cdns_srs11_srs15_config(srs11_val, srs15_val);
  600. break;
  601. case SD_ALL_SEND_CID:
  602. write_value = LEDC;
  603. prev_val = 0x0;
  604. cdns_srs10_value_toggle(write_value, prev_val);
  605. srs15_val = HV4E | V18SE;
  606. srs11_val = SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  607. cdns_srs11_srs15_config(srs11_val, srs15_val);
  608. break;
  609. case SD_SEND_IF_COND:
  610. op = CMD_IDX_CHK_ENABLE;
  611. write_value = LEDC;
  612. prev_val = 0x0;
  613. cdns_srs10_value_toggle(write_value, prev_val);
  614. srs15_val = HV4E;
  615. srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
  616. cdns_srs11_srs15_config(srs11_val, srs15_val);
  617. break;
  618. case SD_STOP_TRANSMISSION:
  619. op = CMD_STOP_ABORT_CMD;
  620. break;
  621. case SD_SEND_STATUS:
  622. break;
  623. case 1:
  624. cmd->cmd_arg = 0;
  625. break;
  626. case SD_SELECT_CARD:
  627. op = MULTI_BLK_READ;
  628. break;
  629. case SD_APP_CMD:
  630. default:
  631. write_value = LEDC;
  632. prev_val = 0x0;
  633. cdns_srs10_value_toggle(write_value, prev_val);
  634. op = 0;
  635. break;
  636. }
  637. }
  638. switch (cmd->resp_type) {
  639. case MMC_RESPONSE_NONE:
  640. op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN;
  641. break;
  642. case MMC_RESPONSE_R2:
  643. op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN |
  644. RES_TYPE_SEL_136 | CMD_CHECK_RESP_CRC;
  645. break;
  646. case MMC_RESPONSE_R3:
  647. op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN |
  648. RES_TYPE_SEL_48;
  649. break;
  650. case MMC_RESPONSE_R1:
  651. if ((cmd->cmd_idx == SD_WRITE_SINGLE_BLOCK) || (cmd->cmd_idx
  652. == SD_WRITE_MULTIPLE_BLOCK)) {
  653. op |= DMA_ENABLED | BLK_CNT_EN | RES_TYPE_SEL_48
  654. | CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
  655. } else {
  656. op |= DMA_ENABLED | BLK_CNT_EN | CMD_READ | RES_TYPE_SEL_48
  657. | CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
  658. }
  659. break;
  660. default:
  661. op |= DMA_ENABLED | BLK_CNT_EN | CMD_READ | MULTI_BLK_READ |
  662. RES_TYPE_SEL_48 | CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
  663. break;
  664. }
  665. timeout = TIMEOUT;
  666. do {
  667. udelay(100);
  668. ret = cdns_busy();
  669. if (--timeout <= 0) {
  670. udelay(50);
  671. panic();
  672. }
  673. } while (ret);
  674. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS12, UINT_MAX);
  675. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS02, cmd->cmd_arg);
  676. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS14, 0x00000000);
  677. if (cmd_indx == 1)
  678. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS03, SDHC_CDNS_SRS03_VALUE);
  679. else
  680. mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS03, op | cmd_indx);
  681. timeout = TIMEOUT;
  682. do {
  683. udelay(500);
  684. value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS12);
  685. } while (((value & (INT_CMD_DONE | ERROR_INT)) == 0) && (timeout-- > 0));
  686. timeout = TIMEOUT;
  687. if (data_cmd) {
  688. data_cmd = false;
  689. do {
  690. udelay(250);
  691. } while (((value & TRAN_COMP) == 0) && (timeout-- > 0));
  692. }
  693. status_check = value & SRS12_ERR_MASK;
  694. if (status_check != 0U) {
  695. ERROR("SD host controller send command failed, SRS12 = %x", status);
  696. return -1;
  697. }
  698. if ((op & RES_TYPE_SEL_48) || (op & RES_TYPE_SEL_136)) {
  699. cmd->resp_data[0] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS04);
  700. if (op & RES_TYPE_SEL_136) {
  701. cmd->resp_data[1] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS05);
  702. cmd->resp_data[2] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS06);
  703. cmd->resp_data[3] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS07);
  704. }
  705. }
  706. return 0;
  707. }