cdns_sdmmc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Copyright (c) 2022-2023, Intel Corporation. All rights reserved.
  3. * Copyright (c) 2024, Altera Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include <arch_helpers.h>
  13. #include <common/debug.h>
  14. #include <drivers/cadence/cdns_sdmmc.h>
  15. #include <drivers/delay_timer.h>
  16. #include <drivers/mmc.h>
  17. #include <lib/mmio.h>
  18. #include <lib/utils.h>
  19. void cdns_init(void);
  20. int cdns_send_cmd(struct mmc_cmd *cmd);
  21. int cdns_set_ios(unsigned int clk, unsigned int width);
  22. int cdns_prepare(int lba, uintptr_t buf, size_t size);
  23. int cdns_read(int lba, uintptr_t buf, size_t size);
  24. int cdns_write(int lba, uintptr_t buf, size_t size);
  25. const struct mmc_ops cdns_sdmmc_ops = {
  26. .init = cdns_init,
  27. .send_cmd = cdns_send_cmd,
  28. .set_ios = cdns_set_ios,
  29. .prepare = cdns_prepare,
  30. .read = cdns_read,
  31. .write = cdns_write,
  32. };
  33. void sd_host_adma_prepare(struct cdns_idmac_desc *desc_ptr, uintptr_t buf,
  34. size_t size);
  35. struct cdns_sdmmc_params cdns_params;
  36. struct cdns_sdmmc_combo_phy sdmmc_combo_phy_reg;
  37. struct cdns_sdmmc_sdhc sdmmc_sdhc_reg;
  38. #ifdef CONFIG_DMA_ADDR_T_64BIT
  39. struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT];
  40. #else
  41. struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT] __aligned(32);
  42. #endif
  43. bool data_cmd;
  44. int cdns_wait_ics(uint16_t timeout, uint32_t cdn_srs_res)
  45. {
  46. /* Clock for sdmclk and sdclk */
  47. uint32_t count = 0;
  48. uint32_t data = 0;
  49. /* Wait status command response ready */
  50. do {
  51. data = mmio_read_32(cdn_srs_res);
  52. count++;
  53. if (count >= timeout) {
  54. return -ETIMEDOUT;
  55. }
  56. } while ((data & (1 << SDMMC_CDN_ICS)) == 0);
  57. return 0;
  58. }
  59. void cdns_set_sdmmc_var(struct cdns_sdmmc_combo_phy *combo_phy_reg,
  60. struct cdns_sdmmc_sdhc *sdhc_reg)
  61. {
  62. /* Values are taken by the reference of cadence IP documents */
  63. combo_phy_reg->cp_clk_wr_delay = 0;
  64. combo_phy_reg->cp_clk_wrdqs_delay = 0;
  65. combo_phy_reg->cp_data_select_oe_end = 1;
  66. combo_phy_reg->cp_dll_bypass_mode = 1;
  67. combo_phy_reg->cp_dll_locked_mode = 0;
  68. combo_phy_reg->cp_dll_start_point = 254;
  69. combo_phy_reg->cp_gate_cfg_always_on = 1;
  70. combo_phy_reg->cp_io_mask_always_on = 0;
  71. combo_phy_reg->cp_io_mask_end = 5;
  72. combo_phy_reg->cp_io_mask_start = 0;
  73. combo_phy_reg->cp_rd_del_sel = 52;
  74. combo_phy_reg->cp_read_dqs_cmd_delay = 0;
  75. combo_phy_reg->cp_read_dqs_delay = 0;
  76. combo_phy_reg->cp_sw_half_cycle_shift = 0;
  77. combo_phy_reg->cp_sync_method = 1;
  78. combo_phy_reg->cp_underrun_suppress = 1;
  79. combo_phy_reg->cp_use_ext_lpbk_dqs = 1;
  80. combo_phy_reg->cp_use_lpbk_dqs = 1;
  81. combo_phy_reg->cp_use_phony_dqs = 1;
  82. combo_phy_reg->cp_use_phony_dqs_cmd = 1;
  83. sdhc_reg->sdhc_extended_rd_mode = 1;
  84. sdhc_reg->sdhc_extended_wr_mode = 1;
  85. sdhc_reg->sdhc_hcsdclkadj = 3;
  86. sdhc_reg->sdhc_idelay_val = 0;
  87. sdhc_reg->sdhc_rdcmd_en = 1;
  88. sdhc_reg->sdhc_rddata_en = 1;
  89. sdhc_reg->sdhc_rw_compensate = 10;
  90. sdhc_reg->sdhc_sdcfsh = 0;
  91. sdhc_reg->sdhc_sdcfsl = 0;
  92. sdhc_reg->sdhc_wrcmd0_dly = 1;
  93. sdhc_reg->sdhc_wrcmd0_sdclk_dly = 0;
  94. sdhc_reg->sdhc_wrcmd1_dly = 0;
  95. sdhc_reg->sdhc_wrcmd1_sdclk_dly = 0;
  96. sdhc_reg->sdhc_wrdata0_dly = 0;
  97. sdhc_reg->sdhc_wrdata0_sdclk_dly = 0;
  98. sdhc_reg->sdhc_wrdata1_dly = 0;
  99. sdhc_reg->sdhc_wrdata1_sdclk_dly = 0;
  100. }
  101. int cdns_program_phy_reg(struct cdns_sdmmc_combo_phy *combo_phy_reg,
  102. struct cdns_sdmmc_sdhc *sdhc_reg)
  103. {
  104. uint32_t value = 0;
  105. int ret = 0;
  106. uint32_t timeout = 0;
  107. /* HRS00 - Software Reset */
  108. mmio_write_32((cdns_params.reg_base + SDHC_CDNS_HRS00), SDHC_CDNS_HRS00_SWR);
  109. /* Waiting for SDHC_CDNS_HRS00_SWR reset */
  110. timeout = TIMEOUT;
  111. do {
  112. udelay(250);
  113. if (--timeout <= 0) {
  114. NOTICE(" SDHC Software Reset failed!!!\n");
  115. panic();
  116. }
  117. } while (((mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS00) &
  118. SDHC_CDNS_HRS00_SWR) == 1));
  119. /* Step 1, switch on DLL_RESET */
  120. value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09);
  121. value &= ~SDHC_PHY_SW_RESET;
  122. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09, value);
  123. /* program PHY_DQS_TIMING_REG */
  124. value = (CP_USE_EXT_LPBK_DQS(combo_phy_reg->cp_use_ext_lpbk_dqs)) |
  125. (CP_USE_LPBK_DQS(combo_phy_reg->cp_use_lpbk_dqs)) |
  126. (CP_USE_PHONY_DQS(combo_phy_reg->cp_use_phony_dqs)) |
  127. (CP_USE_PHONY_DQS_CMD(combo_phy_reg->cp_use_phony_dqs_cmd));
  128. ret = cdns_sdmmc_write_phy_reg(cdns_params.reg_base + SDHC_CDNS_HRS04,
  129. COMBO_PHY_REG + PHY_DQS_TIMING_REG,
  130. cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  131. if (ret != 0U) {
  132. return ret;
  133. }
  134. /* program PHY_GATE_LPBK_CTRL_REG */
  135. value = (CP_SYNC_METHOD(combo_phy_reg->cp_sync_method)) |
  136. (CP_SW_HALF_CYCLE_SHIFT(combo_phy_reg->cp_sw_half_cycle_shift)) |
  137. (CP_RD_DEL_SEL(combo_phy_reg->cp_rd_del_sel)) |
  138. (CP_UNDERRUN_SUPPRESS(combo_phy_reg->cp_underrun_suppress)) |
  139. (CP_GATE_CFG_ALWAYS_ON(combo_phy_reg->cp_gate_cfg_always_on));
  140. ret = cdns_sdmmc_write_phy_reg(cdns_params.reg_base + SDHC_CDNS_HRS04,
  141. COMBO_PHY_REG + PHY_GATE_LPBK_CTRL_REG,
  142. cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  143. if (ret != 0U) {
  144. return -ret;
  145. }
  146. /* program PHY_DLL_MASTER_CTRL_REG */
  147. value = (CP_DLL_BYPASS_MODE(combo_phy_reg->cp_dll_bypass_mode)) | (2 << 20) |
  148. (CP_DLL_START_POINT(combo_phy_reg->cp_dll_start_point));
  149. ret = cdns_sdmmc_write_phy_reg(cdns_params.reg_base + SDHC_CDNS_HRS04,
  150. COMBO_PHY_REG + PHY_DLL_MASTER_CTRL_REG,
  151. cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  152. if (ret != 0U) {
  153. return ret;
  154. }
  155. /* program PHY_DLL_SLAVE_CTRL_REG */
  156. value = (CP_READ_DQS_CMD_DELAY(combo_phy_reg->cp_read_dqs_cmd_delay)) |
  157. (CP_CLK_WRDQS_DELAY(combo_phy_reg->cp_clk_wrdqs_delay)) |
  158. (CP_CLK_WR_DELAY(combo_phy_reg->cp_clk_wr_delay)) |
  159. (CP_READ_DQS_DELAY(combo_phy_reg->cp_read_dqs_delay));
  160. ret = cdns_sdmmc_write_phy_reg(cdns_params.reg_base + SDHC_CDNS_HRS04,
  161. COMBO_PHY_REG + PHY_DLL_SLAVE_CTRL_REG,
  162. cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  163. if (ret != 0U) {
  164. return ret;
  165. }
  166. /* program PHY_CTRL_REG */
  167. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS04, COMBO_PHY_REG + PHY_CTRL_REG);
  168. value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS05);
  169. /* phony_dqs_timing=0 */
  170. value &= ~(CP_PHONY_DQS_TIMING_MASK << CP_PHONY_DQS_TIMING_SHIFT);
  171. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  172. /* switch off DLL_RESET */
  173. do {
  174. value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09);
  175. value |= SDHC_PHY_SW_RESET;
  176. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09, value);
  177. value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09);
  178. /* polling PHY_INIT_COMPLETE */
  179. } while ((value & SDHC_PHY_INIT_COMPLETE) != SDHC_PHY_INIT_COMPLETE);
  180. /* program PHY_DQ_TIMING_REG */
  181. value = (CP_IO_MASK_ALWAYS_ON(combo_phy_reg->cp_io_mask_always_on)) |
  182. (CP_IO_MASK_END(combo_phy_reg->cp_io_mask_end)) |
  183. (CP_IO_MASK_START(combo_phy_reg->cp_io_mask_start)) |
  184. (CP_DATA_SELECT_OE_END(combo_phy_reg->cp_data_select_oe_end));
  185. ret = cdns_sdmmc_write_phy_reg(cdns_params.reg_base + SDHC_CDNS_HRS04,
  186. COMBO_PHY_REG + PHY_DQ_TIMING_REG,
  187. cdns_params.reg_base + SDHC_CDNS_HRS05, value);
  188. if (ret != 0U) {
  189. return ret;
  190. }
  191. value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09);
  192. value |= (HRS_09_EXTENDED_RD_MODE | HRS_09_EXTENDED_WR_MODE |
  193. HRS_09_RDCMD_EN | HRS_09_RDDATA_EN);
  194. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09, value);
  195. value = 0;
  196. value = SDHC_HCSDCLKADJ(HRS_10_HCSDCLKADJ_VAL);
  197. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS10, value);
  198. value = 0;
  199. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS16, value);
  200. value = (10 << 16);
  201. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS07, value);
  202. return 0;
  203. }
  204. int cdns_read(int lba, uintptr_t buf, size_t size)
  205. {
  206. return 0;
  207. }
  208. int cdns_write(int lba, uintptr_t buf, size_t size)
  209. {
  210. return 0;
  211. }
  212. void cdns_init(void)
  213. {
  214. /* Dummy function pointer for cdns_init. */
  215. }
  216. int cdns_prepare(int dma_start_addr, uintptr_t dma_buff, size_t size)
  217. {
  218. struct cdns_idmac_desc *cdns_desc_data;
  219. assert(((dma_buff & CDNSMMC_ADDRESS_MASK) == 0) &&
  220. (cdns_params.desc_size > 0));
  221. cdns_desc_data = (struct cdns_idmac_desc *)cdns_params.desc_base;
  222. sd_host_adma_prepare(cdns_desc_data, dma_buff, size);
  223. return 0;
  224. }
  225. void cdns_host_set_clk(uint32_t clk)
  226. {
  227. uint32_t ret = 0;
  228. uint32_t sdclkfsval = 0;
  229. uint32_t dtcvval = 0xE;
  230. sdclkfsval = (SD_HOST_CLK / 2) / clk;
  231. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS11, 0);
  232. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS11,
  233. (dtcvval << SDMMC_CDN_DTCV) | (sdclkfsval << SDMMC_CDN_SDCLKFS) |
  234. (1 << SDMMC_CDN_ICE));
  235. ret = cdns_wait_ics(5000, cdns_params.reg_base + SDHC_CDNS_SRS11);
  236. if (ret != 0) {
  237. ERROR("Waiting ICS timeout");
  238. }
  239. /* Enable DLL reset */
  240. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09,
  241. mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09) & ~0x00000001);
  242. /* Set extended_wr_mode */
  243. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09,
  244. (mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09) & 0xFFFFFFF7) |
  245. (1 << EXTENDED_WR_MODE));
  246. /* Release DLL reset */
  247. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09,
  248. mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09) | PHY_SW_RESET_EN);
  249. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_HRS09,
  250. mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09) | RDCMD_EN);
  251. do {
  252. mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09);
  253. } while (~mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS09) &
  254. (PHY_INIT_COMPLETE_BIT));
  255. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
  256. (sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE) |
  257. (1 << SDMMC_CDN_SDCE));
  258. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS13, 0xFFFFFFFF);
  259. }
  260. int cdns_set_ios(unsigned int clk, unsigned int width)
  261. {
  262. uint32_t _status = 0;
  263. _status = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  264. switch (width) {
  265. case MMC_BUS_WIDTH_1:
  266. _status &= ~(BIT4);
  267. break;
  268. case MMC_BUS_WIDTH_4:
  269. _status |= BIT4;
  270. break;
  271. case MMC_BUS_WIDTH_8:
  272. _status |= BIT8;
  273. break;
  274. default:
  275. assert(0);
  276. break;
  277. }
  278. mmio_write_32((cdns_params.reg_base + SDHC_CDNS_SRS10), _status);
  279. cdns_host_set_clk(clk);
  280. return 0;
  281. }
  282. int cdns_sdmmc_write_sd_host_reg(uint32_t addr, uint32_t data)
  283. {
  284. uint32_t value = 0;
  285. value = mmio_read_32(addr);
  286. value &= ~SDHC_REG_MASK;
  287. value |= data;
  288. mmio_write_32(addr, value);
  289. value = mmio_read_32(addr);
  290. if (value != data) {
  291. ERROR("SD host address is not set properly\n");
  292. return -ENXIO;
  293. }
  294. return 0;
  295. }
  296. void sd_host_oper_mode(enum sd_opr_modes opr_mode)
  297. {
  298. uint32_t reg = 0;
  299. switch (opr_mode) {
  300. case SD_HOST_OPR_MODE_HV4E_0_SDMA_32:
  301. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  302. reg &= ~(DMA_SEL_BIT);
  303. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  304. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  305. reg &= ~(HV4E | BIT_AD_64);
  306. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  307. break;
  308. case SD_HOST_OPR_MODE_HV4E_1_SDMA_32:
  309. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  310. reg &= ~(DMA_SEL_BIT);
  311. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  312. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  313. reg &= ~(HV4E | BIT_AD_64);
  314. reg |= (HV4E);
  315. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  316. break;
  317. case SD_HOST_OPR_MODE_HV4E_1_SDMA_64:
  318. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  319. reg &= ~(DMA_SEL_BIT);
  320. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  321. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  322. reg |= (HV4E | BIT_AD_64);
  323. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  324. break;
  325. case SD_HOST_OPR_MODE_HV4E_0_ADMA_32:
  326. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  327. reg &= ~(DMA_SEL_BIT);
  328. reg |= DMA_SEL_BIT_2;
  329. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  330. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  331. reg &= ~(HV4E | BIT_AD_64);
  332. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  333. break;
  334. case SD_HOST_OPR_MODE_HV4E_0_ADMA_64:
  335. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  336. reg &= ~(DMA_SEL_BIT);
  337. reg |= DMA_SEL_BIT_3;
  338. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  339. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  340. reg &= ~(HV4E | BIT_AD_64);
  341. reg |= BIT_AD_64;
  342. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  343. break;
  344. case SD_HOST_OPR_MODE_HV4E_1_ADMA_32:
  345. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  346. reg &= ~(DMA_SEL_BIT);
  347. reg |= DMA_SEL_BIT_2;
  348. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  349. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  350. reg &= ~(HV4E | BIT_AD_64);
  351. reg |= HV4E;
  352. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  353. break;
  354. case SD_HOST_OPR_MODE_HV4E_1_ADMA_64:
  355. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  356. reg &= ~(DMA_SEL_BIT);
  357. reg |= DMA_SEL_BIT_2;
  358. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg);
  359. reg = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS15);
  360. reg |= (HV4E | BIT_AD_64);
  361. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS15, reg);
  362. break;
  363. }
  364. }
  365. void card_reset(bool power_enable)
  366. {
  367. uint32_t reg_value = 0;
  368. /* Reading SRS10 value before writing */
  369. reg_value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  370. if (power_enable == true) {
  371. reg_value &= ~((7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
  372. reg_value = ((1 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
  373. } else {
  374. reg_value &= ~((7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
  375. }
  376. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg_value);
  377. }
  378. void high_speed_enable(bool mode)
  379. {
  380. uint32_t reg_value = 0;
  381. /* Reading SRS10 value before writing */
  382. reg_value = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS10);
  383. if (mode == true) {
  384. reg_value |= HS_EN;
  385. } else {
  386. reg_value &= ~HS_EN;
  387. }
  388. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS10, reg_value);
  389. }
  390. int cdns_reset(void)
  391. {
  392. volatile uint32_t data = 0;
  393. uint32_t count = 0;
  394. /* Software reset */
  395. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS11, SRS11_SRFA);
  396. /* Wait status command response ready */
  397. do {
  398. data = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_HRS00);
  399. count++;
  400. if (count >= CDNS_TIMEOUT) {
  401. return -ETIMEDOUT;
  402. }
  403. /* Wait for SRS11 */
  404. } while (((SRS11_SRFA_CHK(data)) & 1) == 1);
  405. return 0;
  406. }
  407. void sdmmc_host_init(bool uhs2_enable)
  408. {
  409. uint32_t timeout;
  410. /* SRS11 - Host Control default value set */
  411. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS11, 0x0);
  412. /* Waiting for detect card */
  413. timeout = TIMEOUT;
  414. do {
  415. udelay(250);
  416. if (--timeout <= 0) {
  417. NOTICE(" SDHC Card Detecion failed!!!\n");
  418. panic();
  419. }
  420. } while (((mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS09) & CHECK_CARD) == 0));
  421. /* UHS2 Host setting */
  422. if (uhs2_enable == true) {
  423. /** need to implement*/
  424. }
  425. /* Card reset */
  426. card_reset(1);
  427. udelay(2500);
  428. card_reset(0);
  429. udelay(2500);
  430. card_reset(1);
  431. udelay(2500);
  432. /* Enable Interrupt Flags*/
  433. mmio_write_32((cdns_params.reg_base + SDHC_CDNS_SRS13), ~0);
  434. high_speed_enable(true);
  435. }
  436. int cdns_sd_host_init(struct cdns_sdmmc_combo_phy *mmc_combo_phy_reg,
  437. struct cdns_sdmmc_sdhc *mmc_sdhc_reg)
  438. {
  439. int ret = 0;
  440. ret = cdns_reset();
  441. if (ret != 0U) {
  442. ERROR("Program phy reg init failed");
  443. return ret;
  444. }
  445. ret = cdns_program_phy_reg(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
  446. if (ret != 0U) {
  447. ERROR("Program phy reg init failed");
  448. return ret;
  449. }
  450. sdmmc_host_init(0);
  451. cdns_host_set_clk(100000);
  452. sd_host_oper_mode(SD_HOST_OPR_MODE_HV4E_0_ADMA_64);
  453. return 0;
  454. }
  455. int cdns_send_cmd(struct mmc_cmd *cmd)
  456. {
  457. uint32_t cmd_flags = 0;
  458. uint32_t timeout = 0;
  459. uint32_t status_check = 0;
  460. uint32_t mode = 0;
  461. uint32_t status;
  462. assert(cmd);
  463. cmd_flags = CDNS_HOST_CMD_INHIBIT | CDNS_HOST_DATA_INHIBIT;
  464. if ((cmd->cmd_idx == SD_STOP_TRANSMISSION) && (!data_cmd)) {
  465. cmd_flags &= ~CDNS_HOST_DATA_INHIBIT;
  466. }
  467. timeout = TIMEOUT;
  468. do {
  469. udelay(100);
  470. if (--timeout <= 0) {
  471. udelay(50);
  472. NOTICE("Timeout occur data and cmd line %x\n",
  473. mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS09));
  474. panic();
  475. }
  476. } while ((mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS09) & (cmd_flags)));
  477. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS12, 0xFFFFFFFF);
  478. cmd_flags = 0;
  479. cmd_flags = (cmd->cmd_idx) << COM_IDX;
  480. if ((cmd->resp_type & MMC_RSP_136) != 0) {
  481. cmd_flags |= RES_TYPE_SEL_136;
  482. } else if (((cmd->resp_type & MMC_RSP_48) != 0) &&
  483. ((cmd->resp_type & MMC_RSP_BUSY) != 0)) {
  484. cmd_flags |= RES_TYPE_SEL_48_B;
  485. } else if ((cmd->resp_type & MMC_RSP_48) != 0) {
  486. cmd_flags |= RES_TYPE_SEL_48;
  487. } else {
  488. cmd_flags &= ~RES_TYPE_SEL_NO;
  489. }
  490. if ((cmd->resp_type & MMC_RSP_CRC) != 0) {
  491. cmd_flags |= CMD_CHECK_RESP_CRC;
  492. }
  493. if ((cmd->resp_type & MMC_RSP_CMD_IDX) != 0) {
  494. cmd_flags |= CMD_IDX_CHK_ENABLE;
  495. }
  496. if ((cmd->cmd_idx == MMC_ACMD(51)) || (cmd->cmd_idx == MMC_CMD(17)) ||
  497. (cmd->cmd_idx == MMC_CMD(18)) || (cmd->cmd_idx == MMC_CMD(24)) ||
  498. (cmd->cmd_idx == MMC_CMD(25))) {
  499. mmio_write_8((cdns_params.reg_base + DTCV_OFFSET), DTCV_VAL);
  500. cmd_flags |= DATA_PRESENT;
  501. mode |= BLK_CNT_EN;
  502. mode |= (DMA_ENABLED);
  503. if ((cmd->cmd_idx == SD_WRITE_MULTIPLE_BLOCK) ||
  504. (cmd->cmd_idx == SD_READ_MULTIPLE_BLOCK)) {
  505. mode |= (MULTI_BLK_READ);
  506. } else {
  507. mode &= ~(MULTI_BLK_READ);
  508. }
  509. if ((cmd->cmd_idx == SD_WRITE_MULTIPLE_BLOCK) ||
  510. (cmd->cmd_idx == SD_WRITE_SINGLE_BLOCK)) {
  511. mode &= ~CMD_READ;
  512. } else {
  513. mode |= CMD_READ;
  514. }
  515. mmio_write_16(cdns_params.reg_base + SDHC_CDNS_SRS03, mode);
  516. } else {
  517. mmio_write_8((cdns_params.reg_base + DTCV_OFFSET), DTCV_VAL);
  518. }
  519. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS02, cmd->cmd_arg);
  520. mmio_write_16((cdns_params.reg_base + CICE_OFFSET),
  521. SDHCI_MAKE_CMD(cmd->cmd_idx, cmd_flags));
  522. timeout = TIMEOUT;
  523. do {
  524. udelay(CDNS_TIMEOUT);
  525. status = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS12);
  526. } while (((status & (INT_CMD_DONE | ERROR_INT)) == 0) && (timeout-- > 0));
  527. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS12, (SRS_12_CC_EN));
  528. status_check = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS12) & 0xffff8000;
  529. if (status_check != 0U) {
  530. timeout = TIMEOUT;
  531. ERROR("SD host controller send command failed, SRS12 = %x", status_check);
  532. return -1;
  533. }
  534. if (!((cmd_flags & RES_TYPE_SEL_NO) == 0)) {
  535. cmd->resp_data[0] = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS04);
  536. if ((cmd_flags & RES_TYPE_SEL_NO) == RES_TYPE_SEL_136) {
  537. cmd->resp_data[1] = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS05);
  538. cmd->resp_data[2] = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS06);
  539. cmd->resp_data[3] = mmio_read_32(cdns_params.reg_base + SDHC_CDNS_SRS07);
  540. /* 136-bit: RTS=01b, Response field R[127:8] - RESP3[23:0],
  541. * RESP2[31:0], RESP1[31:0], RESP0[31:0]
  542. * Subsystem expects 128 bits response but cadence SDHC sends
  543. * 120 bits response from R[127:8]. Bits manupulation to address
  544. * the correct responses for the 136 bit response type.
  545. */
  546. cmd->resp_data[3] = ((cmd->resp_data[3] << 8) |
  547. ((cmd->resp_data[2] >> 24) &
  548. CDNS_CSD_BYTE_MASK));
  549. cmd->resp_data[2] = ((cmd->resp_data[2] << 8) |
  550. ((cmd->resp_data[1] >> 24) &
  551. CDNS_CSD_BYTE_MASK));
  552. cmd->resp_data[1] = ((cmd->resp_data[1] << 8) |
  553. ((cmd->resp_data[0] >> 24) &
  554. CDNS_CSD_BYTE_MASK));
  555. cmd->resp_data[0] = (cmd->resp_data[0] << 8);
  556. }
  557. }
  558. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS12, (SRS_12_CC_EN));
  559. return 0;
  560. }
  561. void sd_host_adma_prepare(struct cdns_idmac_desc *desc_ptr, uint64_t buf,
  562. size_t size)
  563. {
  564. uint32_t full_desc_cnt = 0;
  565. uint32_t non_full_desc_cnt = 0;
  566. uint64_t desc_address;
  567. uint32_t block_count;
  568. uint32_t transfer_block_size;
  569. full_desc_cnt = (size / PAGE_BUFFER_LEN);
  570. non_full_desc_cnt = (size % PAGE_BUFFER_LEN);
  571. for (int i = 0; i < full_desc_cnt; i++) {
  572. desc_ptr->attr = (ADMA_DESC_TRANSFER_DATA | ADMA_DESC_ATTR_VALID);
  573. desc_ptr->len = 0; // 0 means 64kb page size it will take
  574. desc_ptr->addr_lo = 0;
  575. #if CONFIG_DMA_ADDR_T_64BIT == 1
  576. desc_ptr->addr_hi = (uint32_t)((buf >> 32) & 0xffffffff);
  577. #endif
  578. if (non_full_desc_cnt == 0) {
  579. desc_ptr->attr |= (ADMA_DESC_ATTR_END);
  580. }
  581. buf += PAGE_BUFFER_LEN;
  582. }
  583. if (non_full_desc_cnt != 0) {
  584. desc_ptr->attr =
  585. (ADMA_DESC_TRANSFER_DATA | ADMA_DESC_ATTR_END | ADMA_DESC_ATTR_VALID);
  586. desc_ptr->addr_lo = buf & 0xffffffff;
  587. desc_ptr->len = size;
  588. #if CONFIG_DMA_ADDR_T_64BIT == 1
  589. desc_ptr->addr_hi = (uint32_t)((buf >> 32) & 0xffffffff);
  590. #endif
  591. desc_address = (uint64_t)desc_ptr;
  592. if (size > MMC_MAX_BLOCK_LEN) {
  593. transfer_block_size = MMC_MAX_BLOCK_LEN;
  594. } else {
  595. transfer_block_size = size;
  596. }
  597. block_count = (size / transfer_block_size);
  598. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS01,
  599. ((transfer_block_size << BLOCK_SIZE) | SDMA_BUF |
  600. (block_count << BLK_COUNT_CT)));
  601. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS22,
  602. (uint32_t)desc_address & 0xFFFFFFFF);
  603. mmio_write_32(cdns_params.reg_base + SDHC_CDNS_SRS23,
  604. (uint32_t)(desc_address >> 32 & 0xFFFFFFFF));
  605. }
  606. }
  607. int cdns_mmc_init(struct cdns_sdmmc_params *params,
  608. struct mmc_device_info *info)
  609. {
  610. int result = 0;
  611. assert((params != NULL) &&
  612. ((params->reg_base & MMC_BLOCK_MASK) == 0) &&
  613. ((params->desc_size & MMC_BLOCK_MASK) == 0) &&
  614. ((params->reg_pinmux & MMC_BLOCK_MASK) == 0) &&
  615. ((params->reg_phy & MMC_BLOCK_MASK) == 0) &&
  616. (params->desc_size > 0) &&
  617. (params->clk_rate > 0) &&
  618. ((params->bus_width == MMC_BUS_WIDTH_1) ||
  619. (params->bus_width == MMC_BUS_WIDTH_4) ||
  620. (params->bus_width == MMC_BUS_WIDTH_8)));
  621. memcpy(&cdns_params, params, sizeof(struct cdns_sdmmc_params));
  622. cdns_set_sdmmc_var(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
  623. result = cdns_sd_host_init(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
  624. if (result < 0) {
  625. return result;
  626. }
  627. cdns_params.cdn_sdmmc_dev_type = info->mmc_dev_type;
  628. cdns_params.cdn_sdmmc_dev_mode = SD_DS;
  629. result = mmc_init(&cdns_sdmmc_ops, params->clk_rate, params->bus_width,
  630. params->flags, info);
  631. return result;
  632. }