stm32_sdmmc2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright (c) 2018-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <arch.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <drivers/clk.h>
  13. #include <drivers/delay_timer.h>
  14. #include <drivers/mmc.h>
  15. #include <drivers/st/stm32_gpio.h>
  16. #include <drivers/st/stm32_sdmmc2.h>
  17. #include <drivers/st/stm32mp_reset.h>
  18. #include <lib/mmio.h>
  19. #include <lib/utils.h>
  20. #include <libfdt.h>
  21. #include <plat/common/platform.h>
  22. #include <platform_def.h>
  23. /* Registers offsets */
  24. #define SDMMC_POWER 0x00U
  25. #define SDMMC_CLKCR 0x04U
  26. #define SDMMC_ARGR 0x08U
  27. #define SDMMC_CMDR 0x0CU
  28. #define SDMMC_RESPCMDR 0x10U
  29. #define SDMMC_RESP1R 0x14U
  30. #define SDMMC_RESP2R 0x18U
  31. #define SDMMC_RESP3R 0x1CU
  32. #define SDMMC_RESP4R 0x20U
  33. #define SDMMC_DTIMER 0x24U
  34. #define SDMMC_DLENR 0x28U
  35. #define SDMMC_DCTRLR 0x2CU
  36. #define SDMMC_DCNTR 0x30U
  37. #define SDMMC_STAR 0x34U
  38. #define SDMMC_ICR 0x38U
  39. #define SDMMC_MASKR 0x3CU
  40. #define SDMMC_ACKTIMER 0x40U
  41. #define SDMMC_IDMACTRLR 0x50U
  42. #define SDMMC_IDMABSIZER 0x54U
  43. #define SDMMC_IDMABASE0R 0x58U
  44. #define SDMMC_IDMABASE1R 0x5CU
  45. #define SDMMC_FIFOR 0x80U
  46. /* SDMMC power control register */
  47. #define SDMMC_POWER_PWRCTRL GENMASK(1, 0)
  48. #define SDMMC_POWER_PWRCTRL_PWR_CYCLE BIT(1)
  49. #define SDMMC_POWER_DIRPOL BIT(4)
  50. /* SDMMC clock control register */
  51. #define SDMMC_CLKCR_WIDBUS_4 BIT(14)
  52. #define SDMMC_CLKCR_WIDBUS_8 BIT(15)
  53. #define SDMMC_CLKCR_NEGEDGE BIT(16)
  54. #define SDMMC_CLKCR_HWFC_EN BIT(17)
  55. #define SDMMC_CLKCR_SELCLKRX_0 BIT(20)
  56. /* SDMMC command register */
  57. #define SDMMC_CMDR_CMDTRANS BIT(6)
  58. #define SDMMC_CMDR_CMDSTOP BIT(7)
  59. #define SDMMC_CMDR_WAITRESP GENMASK(9, 8)
  60. #define SDMMC_CMDR_WAITRESP_SHORT BIT(8)
  61. #define SDMMC_CMDR_WAITRESP_SHORT_NOCRC BIT(9)
  62. #define SDMMC_CMDR_CPSMEN BIT(12)
  63. /* SDMMC data control register */
  64. #define SDMMC_DCTRLR_DTEN BIT(0)
  65. #define SDMMC_DCTRLR_DTDIR BIT(1)
  66. #define SDMMC_DCTRLR_DTMODE GENMASK(3, 2)
  67. #define SDMMC_DCTRLR_DBLOCKSIZE GENMASK(7, 4)
  68. #define SDMMC_DCTRLR_DBLOCKSIZE_SHIFT 4
  69. #define SDMMC_DCTRLR_FIFORST BIT(13)
  70. #define SDMMC_DCTRLR_CLEAR_MASK (SDMMC_DCTRLR_DTEN | \
  71. SDMMC_DCTRLR_DTDIR | \
  72. SDMMC_DCTRLR_DTMODE | \
  73. SDMMC_DCTRLR_DBLOCKSIZE)
  74. /* SDMMC status register */
  75. #define SDMMC_STAR_CCRCFAIL BIT(0)
  76. #define SDMMC_STAR_DCRCFAIL BIT(1)
  77. #define SDMMC_STAR_CTIMEOUT BIT(2)
  78. #define SDMMC_STAR_DTIMEOUT BIT(3)
  79. #define SDMMC_STAR_TXUNDERR BIT(4)
  80. #define SDMMC_STAR_RXOVERR BIT(5)
  81. #define SDMMC_STAR_CMDREND BIT(6)
  82. #define SDMMC_STAR_CMDSENT BIT(7)
  83. #define SDMMC_STAR_DATAEND BIT(8)
  84. #define SDMMC_STAR_DBCKEND BIT(10)
  85. #define SDMMC_STAR_DPSMACT BIT(12)
  86. #define SDMMC_STAR_RXFIFOHF BIT(15)
  87. #define SDMMC_STAR_RXFIFOE BIT(19)
  88. #define SDMMC_STAR_IDMATE BIT(27)
  89. #define SDMMC_STAR_IDMABTC BIT(28)
  90. /* SDMMC DMA control register */
  91. #define SDMMC_IDMACTRLR_IDMAEN BIT(0)
  92. #define SDMMC_STATIC_FLAGS (SDMMC_STAR_CCRCFAIL | \
  93. SDMMC_STAR_DCRCFAIL | \
  94. SDMMC_STAR_CTIMEOUT | \
  95. SDMMC_STAR_DTIMEOUT | \
  96. SDMMC_STAR_TXUNDERR | \
  97. SDMMC_STAR_RXOVERR | \
  98. SDMMC_STAR_CMDREND | \
  99. SDMMC_STAR_CMDSENT | \
  100. SDMMC_STAR_DATAEND | \
  101. SDMMC_STAR_DBCKEND | \
  102. SDMMC_STAR_IDMATE | \
  103. SDMMC_STAR_IDMABTC)
  104. #define TIMEOUT_US_1_MS 1000U
  105. #define TIMEOUT_US_10_MS 10000U
  106. #define TIMEOUT_US_1_S 1000000U
  107. /* Power cycle delays in ms */
  108. #define VCC_POWER_OFF_DELAY 2
  109. #define VCC_POWER_ON_DELAY 2
  110. #define POWER_CYCLE_DELAY 2
  111. #define POWER_OFF_DELAY 2
  112. #define POWER_ON_DELAY 1
  113. #ifndef DT_SDMMC2_COMPAT
  114. #define DT_SDMMC2_COMPAT "st,stm32-sdmmc2"
  115. #endif
  116. #if STM32MP13 || STM32MP15
  117. #define SDMMC_FIFO_SIZE 64U
  118. #else
  119. #define SDMMC_FIFO_SIZE 1024U
  120. #endif
  121. #define STM32MP_MMC_INIT_FREQ U(400000) /*400 KHz*/
  122. #define STM32MP_SD_NORMAL_SPEED_MAX_FREQ U(25000000) /*25 MHz*/
  123. #define STM32MP_SD_HIGH_SPEED_MAX_FREQ U(50000000) /*50 MHz*/
  124. #define STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ U(26000000) /*26 MHz*/
  125. #define STM32MP_EMMC_HIGH_SPEED_MAX_FREQ U(52000000) /*52 MHz*/
  126. static void stm32_sdmmc2_init(void);
  127. static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd);
  128. static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd);
  129. static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width);
  130. static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size);
  131. static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size);
  132. static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size);
  133. static const struct mmc_ops stm32_sdmmc2_ops = {
  134. .init = stm32_sdmmc2_init,
  135. .send_cmd = stm32_sdmmc2_send_cmd,
  136. .set_ios = stm32_sdmmc2_set_ios,
  137. .prepare = stm32_sdmmc2_prepare,
  138. .read = stm32_sdmmc2_read,
  139. .write = stm32_sdmmc2_write,
  140. };
  141. static struct stm32_sdmmc2_params sdmmc2_params;
  142. static bool next_cmd_is_acmd;
  143. #pragma weak plat_sdmmc2_use_dma
  144. bool plat_sdmmc2_use_dma(unsigned int instance, unsigned int memory)
  145. {
  146. return false;
  147. }
  148. static void stm32_sdmmc2_init(void)
  149. {
  150. uint32_t clock_div;
  151. uint32_t freq = STM32MP_MMC_INIT_FREQ;
  152. uintptr_t base = sdmmc2_params.reg_base;
  153. int ret;
  154. if (sdmmc2_params.max_freq != 0U) {
  155. freq = MIN(sdmmc2_params.max_freq, freq);
  156. }
  157. if (sdmmc2_params.vmmc_regu != NULL) {
  158. ret = regulator_disable(sdmmc2_params.vmmc_regu);
  159. if (ret < 0) {
  160. panic();
  161. }
  162. }
  163. mdelay(VCC_POWER_OFF_DELAY);
  164. mmio_write_32(base + SDMMC_POWER,
  165. SDMMC_POWER_PWRCTRL_PWR_CYCLE | sdmmc2_params.dirpol);
  166. mdelay(POWER_CYCLE_DELAY);
  167. if (sdmmc2_params.vmmc_regu != NULL) {
  168. ret = regulator_enable(sdmmc2_params.vmmc_regu);
  169. if (ret < 0) {
  170. panic();
  171. }
  172. }
  173. mdelay(VCC_POWER_ON_DELAY);
  174. mmio_write_32(base + SDMMC_POWER, sdmmc2_params.dirpol);
  175. mdelay(POWER_OFF_DELAY);
  176. clock_div = div_round_up(sdmmc2_params.clk_rate, freq * 2U);
  177. mmio_write_32(base + SDMMC_CLKCR, SDMMC_CLKCR_HWFC_EN | clock_div |
  178. sdmmc2_params.negedge |
  179. sdmmc2_params.pin_ckin);
  180. mmio_write_32(base + SDMMC_POWER,
  181. SDMMC_POWER_PWRCTRL | sdmmc2_params.dirpol);
  182. mdelay(POWER_ON_DELAY);
  183. }
  184. static int stm32_sdmmc2_stop_transfer(void)
  185. {
  186. struct mmc_cmd cmd_stop;
  187. zeromem(&cmd_stop, sizeof(struct mmc_cmd));
  188. cmd_stop.cmd_idx = MMC_CMD(12);
  189. cmd_stop.resp_type = MMC_RESPONSE_R1B;
  190. return stm32_sdmmc2_send_cmd(&cmd_stop);
  191. }
  192. static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd)
  193. {
  194. uint64_t timeout;
  195. uint32_t flags_cmd, status;
  196. uint32_t flags_data = 0;
  197. int err = 0;
  198. uintptr_t base = sdmmc2_params.reg_base;
  199. unsigned int cmd_reg, arg_reg;
  200. if (cmd == NULL) {
  201. return -EINVAL;
  202. }
  203. flags_cmd = SDMMC_STAR_CTIMEOUT;
  204. arg_reg = cmd->cmd_arg;
  205. if ((mmio_read_32(base + SDMMC_CMDR) & SDMMC_CMDR_CPSMEN) != 0U) {
  206. mmio_write_32(base + SDMMC_CMDR, 0);
  207. }
  208. cmd_reg = cmd->cmd_idx | SDMMC_CMDR_CPSMEN;
  209. if (cmd->resp_type == 0U) {
  210. flags_cmd |= SDMMC_STAR_CMDSENT;
  211. }
  212. if ((cmd->resp_type & MMC_RSP_48) != 0U) {
  213. if ((cmd->resp_type & MMC_RSP_136) != 0U) {
  214. flags_cmd |= SDMMC_STAR_CMDREND;
  215. cmd_reg |= SDMMC_CMDR_WAITRESP;
  216. } else if ((cmd->resp_type & MMC_RSP_CRC) != 0U) {
  217. flags_cmd |= SDMMC_STAR_CMDREND | SDMMC_STAR_CCRCFAIL;
  218. cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT;
  219. } else {
  220. flags_cmd |= SDMMC_STAR_CMDREND;
  221. cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT_NOCRC;
  222. }
  223. }
  224. switch (cmd->cmd_idx) {
  225. case MMC_CMD(1):
  226. arg_reg |= OCR_POWERUP;
  227. break;
  228. case MMC_CMD(6):
  229. if ((sdmmc2_params.device_info->mmc_dev_type == MMC_IS_SD_HC) &&
  230. (!next_cmd_is_acmd)) {
  231. cmd_reg |= SDMMC_CMDR_CMDTRANS;
  232. if (sdmmc2_params.use_dma) {
  233. flags_data |= SDMMC_STAR_DCRCFAIL |
  234. SDMMC_STAR_DTIMEOUT |
  235. SDMMC_STAR_DATAEND |
  236. SDMMC_STAR_RXOVERR |
  237. SDMMC_STAR_IDMATE |
  238. SDMMC_STAR_DBCKEND;
  239. }
  240. }
  241. break;
  242. case MMC_CMD(8):
  243. if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
  244. cmd_reg |= SDMMC_CMDR_CMDTRANS;
  245. }
  246. break;
  247. case MMC_CMD(12):
  248. cmd_reg |= SDMMC_CMDR_CMDSTOP;
  249. break;
  250. case MMC_CMD(17):
  251. case MMC_CMD(18):
  252. cmd_reg |= SDMMC_CMDR_CMDTRANS;
  253. if (sdmmc2_params.use_dma) {
  254. flags_data |= SDMMC_STAR_DCRCFAIL |
  255. SDMMC_STAR_DTIMEOUT |
  256. SDMMC_STAR_DATAEND |
  257. SDMMC_STAR_RXOVERR |
  258. SDMMC_STAR_IDMATE;
  259. }
  260. break;
  261. case MMC_ACMD(41):
  262. arg_reg |= OCR_3_2_3_3 | OCR_3_3_3_4;
  263. break;
  264. case MMC_ACMD(51):
  265. cmd_reg |= SDMMC_CMDR_CMDTRANS;
  266. if (sdmmc2_params.use_dma) {
  267. flags_data |= SDMMC_STAR_DCRCFAIL |
  268. SDMMC_STAR_DTIMEOUT |
  269. SDMMC_STAR_DATAEND |
  270. SDMMC_STAR_RXOVERR |
  271. SDMMC_STAR_IDMATE |
  272. SDMMC_STAR_DBCKEND;
  273. }
  274. break;
  275. default:
  276. break;
  277. }
  278. next_cmd_is_acmd = (cmd->cmd_idx == MMC_CMD(55));
  279. mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
  280. /*
  281. * Clear the SDMMC_DCTRLR if the command does not await data.
  282. * Skip CMD55 as the next command could be data related, and
  283. * the register could have been set in prepare function.
  284. */
  285. if (((cmd_reg & SDMMC_CMDR_CMDTRANS) == 0U) && !next_cmd_is_acmd) {
  286. mmio_write_32(base + SDMMC_DCTRLR, 0U);
  287. }
  288. if ((cmd->resp_type & MMC_RSP_BUSY) != 0U) {
  289. mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
  290. }
  291. mmio_write_32(base + SDMMC_ARGR, arg_reg);
  292. mmio_write_32(base + SDMMC_CMDR, cmd_reg);
  293. status = mmio_read_32(base + SDMMC_STAR);
  294. timeout = timeout_init_us(TIMEOUT_US_10_MS);
  295. while ((status & flags_cmd) == 0U) {
  296. if (timeout_elapsed(timeout)) {
  297. err = -ETIMEDOUT;
  298. ERROR("%s: timeout 10ms (cmd = %u,status = %x)\n",
  299. __func__, cmd->cmd_idx, status);
  300. goto err_exit;
  301. }
  302. status = mmio_read_32(base + SDMMC_STAR);
  303. }
  304. if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
  305. if ((status & SDMMC_STAR_CTIMEOUT) != 0U) {
  306. err = -ETIMEDOUT;
  307. /*
  308. * Those timeouts can occur, and framework will handle
  309. * the retries. CMD8 is expected to return this timeout
  310. * for eMMC
  311. */
  312. if (!((cmd->cmd_idx == MMC_CMD(1)) ||
  313. (cmd->cmd_idx == MMC_CMD(13)) ||
  314. ((cmd->cmd_idx == MMC_CMD(8)) &&
  315. (cmd->resp_type == MMC_RESPONSE_R7)))) {
  316. ERROR("%s: CTIMEOUT (cmd = %u,status = %x)\n",
  317. __func__, cmd->cmd_idx, status);
  318. }
  319. } else {
  320. err = -EIO;
  321. ERROR("%s: CRCFAIL (cmd = %u,status = %x)\n",
  322. __func__, cmd->cmd_idx, status);
  323. }
  324. goto err_exit;
  325. }
  326. if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
  327. if ((cmd->cmd_idx == MMC_CMD(9)) &&
  328. ((cmd_reg & SDMMC_CMDR_WAITRESP) == SDMMC_CMDR_WAITRESP)) {
  329. /* Need to invert response to match CSD structure */
  330. cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP4R);
  331. cmd->resp_data[1] = mmio_read_32(base + SDMMC_RESP3R);
  332. cmd->resp_data[2] = mmio_read_32(base + SDMMC_RESP2R);
  333. cmd->resp_data[3] = mmio_read_32(base + SDMMC_RESP1R);
  334. } else {
  335. cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP1R);
  336. if ((cmd_reg & SDMMC_CMDR_WAITRESP) ==
  337. SDMMC_CMDR_WAITRESP) {
  338. cmd->resp_data[1] = mmio_read_32(base +
  339. SDMMC_RESP2R);
  340. cmd->resp_data[2] = mmio_read_32(base +
  341. SDMMC_RESP3R);
  342. cmd->resp_data[3] = mmio_read_32(base +
  343. SDMMC_RESP4R);
  344. }
  345. }
  346. }
  347. if (flags_data == 0U) {
  348. mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
  349. return 0;
  350. }
  351. status = mmio_read_32(base + SDMMC_STAR);
  352. timeout = timeout_init_us(TIMEOUT_US_10_MS);
  353. while ((status & flags_data) == 0U) {
  354. if (timeout_elapsed(timeout)) {
  355. ERROR("%s: timeout 10ms (cmd = %u,status = %x)\n",
  356. __func__, cmd->cmd_idx, status);
  357. err = -ETIMEDOUT;
  358. goto err_exit;
  359. }
  360. status = mmio_read_32(base + SDMMC_STAR);
  361. };
  362. if ((status & (SDMMC_STAR_DTIMEOUT | SDMMC_STAR_DCRCFAIL |
  363. SDMMC_STAR_TXUNDERR | SDMMC_STAR_RXOVERR |
  364. SDMMC_STAR_IDMATE)) != 0U) {
  365. ERROR("%s: Error flag (cmd = %u,status = %x)\n", __func__,
  366. cmd->cmd_idx, status);
  367. err = -EIO;
  368. }
  369. err_exit:
  370. mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
  371. mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);
  372. if ((err != 0) && ((status & SDMMC_STAR_DPSMACT) != 0U)) {
  373. int ret_stop = stm32_sdmmc2_stop_transfer();
  374. if (ret_stop != 0) {
  375. return ret_stop;
  376. }
  377. }
  378. return err;
  379. }
  380. static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
  381. {
  382. uint8_t retry;
  383. int err;
  384. assert(cmd != NULL);
  385. for (retry = 0U; retry < 3U; retry++) {
  386. err = stm32_sdmmc2_send_cmd_req(cmd);
  387. if (err == 0) {
  388. return 0;
  389. }
  390. if ((cmd->cmd_idx == MMC_CMD(1)) ||
  391. (cmd->cmd_idx == MMC_CMD(13))) {
  392. return 0; /* Retry managed by framework */
  393. }
  394. /* Command 8 is expected to fail for eMMC */
  395. if (cmd->cmd_idx != MMC_CMD(8)) {
  396. WARN(" CMD%u, Retry: %u, Error: %d\n",
  397. cmd->cmd_idx, retry + 1U, err);
  398. }
  399. udelay(10U);
  400. }
  401. return err;
  402. }
  403. static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width)
  404. {
  405. uintptr_t base = sdmmc2_params.reg_base;
  406. uint32_t bus_cfg = 0;
  407. uint32_t clock_div, max_freq, freq;
  408. uint32_t clk_rate = sdmmc2_params.clk_rate;
  409. uint32_t max_bus_freq = sdmmc2_params.device_info->max_bus_freq;
  410. switch (width) {
  411. case MMC_BUS_WIDTH_1:
  412. break;
  413. case MMC_BUS_WIDTH_4:
  414. bus_cfg |= SDMMC_CLKCR_WIDBUS_4;
  415. break;
  416. case MMC_BUS_WIDTH_8:
  417. bus_cfg |= SDMMC_CLKCR_WIDBUS_8;
  418. break;
  419. default:
  420. panic();
  421. break;
  422. }
  423. if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
  424. if (max_bus_freq >= 52000000U) {
  425. max_freq = STM32MP_EMMC_HIGH_SPEED_MAX_FREQ;
  426. } else {
  427. max_freq = STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ;
  428. }
  429. } else {
  430. if (max_bus_freq >= 50000000U) {
  431. max_freq = STM32MP_SD_HIGH_SPEED_MAX_FREQ;
  432. } else {
  433. max_freq = STM32MP_SD_NORMAL_SPEED_MAX_FREQ;
  434. }
  435. }
  436. if (sdmmc2_params.max_freq != 0U) {
  437. freq = MIN(sdmmc2_params.max_freq, max_freq);
  438. } else {
  439. freq = max_freq;
  440. }
  441. clock_div = div_round_up(clk_rate, freq * 2U);
  442. mmio_write_32(base + SDMMC_CLKCR,
  443. SDMMC_CLKCR_HWFC_EN | clock_div | bus_cfg |
  444. sdmmc2_params.negedge |
  445. sdmmc2_params.pin_ckin);
  446. return 0;
  447. }
  448. static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size)
  449. {
  450. struct mmc_cmd cmd;
  451. int ret;
  452. uintptr_t base = sdmmc2_params.reg_base;
  453. uint32_t data_ctrl = SDMMC_DCTRLR_DTDIR;
  454. uint32_t arg_size;
  455. assert((size != 0U) && (size <= UINT32_MAX));
  456. if (size > MMC_BLOCK_SIZE) {
  457. arg_size = MMC_BLOCK_SIZE;
  458. } else {
  459. arg_size = (uint32_t)size;
  460. }
  461. sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);
  462. if (sdmmc2_params.use_dma) {
  463. inv_dcache_range(buf, size);
  464. }
  465. /* Prepare CMD 16*/
  466. mmio_write_32(base + SDMMC_DTIMER, 0);
  467. mmio_write_32(base + SDMMC_DLENR, 0);
  468. mmio_write_32(base + SDMMC_DCTRLR, 0);
  469. zeromem(&cmd, sizeof(struct mmc_cmd));
  470. cmd.cmd_idx = MMC_CMD(16);
  471. cmd.cmd_arg = arg_size;
  472. cmd.resp_type = MMC_RESPONSE_R1;
  473. ret = stm32_sdmmc2_send_cmd(&cmd);
  474. if (ret != 0) {
  475. ERROR("CMD16 failed\n");
  476. return ret;
  477. }
  478. /* Prepare data command */
  479. mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
  480. mmio_write_32(base + SDMMC_DLENR, size);
  481. if (sdmmc2_params.use_dma) {
  482. mmio_write_32(base + SDMMC_IDMACTRLR,
  483. SDMMC_IDMACTRLR_IDMAEN);
  484. mmio_write_32(base + SDMMC_IDMABASE0R, buf);
  485. flush_dcache_range(buf, size);
  486. }
  487. data_ctrl |= __builtin_ctz(arg_size) << SDMMC_DCTRLR_DBLOCKSIZE_SHIFT;
  488. mmio_clrsetbits_32(base + SDMMC_DCTRLR,
  489. SDMMC_DCTRLR_CLEAR_MASK,
  490. data_ctrl);
  491. return 0;
  492. }
  493. static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size)
  494. {
  495. uint32_t error_flags = SDMMC_STAR_RXOVERR | SDMMC_STAR_DCRCFAIL |
  496. SDMMC_STAR_DTIMEOUT;
  497. uint32_t flags = error_flags | SDMMC_STAR_DATAEND;
  498. uint32_t status;
  499. uint32_t *buffer;
  500. uintptr_t base = sdmmc2_params.reg_base;
  501. uintptr_t fifo_reg = base + SDMMC_FIFOR;
  502. uint64_t timeout;
  503. int ret;
  504. /* Assert buf is 4 bytes aligned */
  505. assert((buf & GENMASK(1, 0)) == 0U);
  506. buffer = (uint32_t *)buf;
  507. if (sdmmc2_params.use_dma) {
  508. inv_dcache_range(buf, size);
  509. return 0;
  510. }
  511. if (size <= MMC_BLOCK_SIZE) {
  512. flags |= SDMMC_STAR_DBCKEND;
  513. }
  514. timeout = timeout_init_us(TIMEOUT_US_1_S);
  515. do {
  516. status = mmio_read_32(base + SDMMC_STAR);
  517. if ((status & error_flags) != 0U) {
  518. ERROR("%s: Read error (status = %x)\n", __func__,
  519. status);
  520. mmio_write_32(base + SDMMC_DCTRLR,
  521. SDMMC_DCTRLR_FIFORST);
  522. mmio_write_32(base + SDMMC_ICR,
  523. SDMMC_STATIC_FLAGS);
  524. ret = stm32_sdmmc2_stop_transfer();
  525. if (ret != 0) {
  526. return ret;
  527. }
  528. return -EIO;
  529. }
  530. if (timeout_elapsed(timeout)) {
  531. ERROR("%s: timeout 1s (status = %x)\n",
  532. __func__, status);
  533. mmio_write_32(base + SDMMC_ICR,
  534. SDMMC_STATIC_FLAGS);
  535. ret = stm32_sdmmc2_stop_transfer();
  536. if (ret != 0) {
  537. return ret;
  538. }
  539. return -ETIMEDOUT;
  540. }
  541. if (size < (SDMMC_FIFO_SIZE / 2U)) {
  542. if ((mmio_read_32(base + SDMMC_DCNTR) > 0U) &&
  543. ((status & SDMMC_STAR_RXFIFOE) == 0U)) {
  544. *buffer = mmio_read_32(fifo_reg);
  545. buffer++;
  546. }
  547. } else if ((status & SDMMC_STAR_RXFIFOHF) != 0U) {
  548. uint32_t count;
  549. /* Read data from SDMMC Rx FIFO */
  550. for (count = 0; count < (SDMMC_FIFO_SIZE / 2U);
  551. count += sizeof(uint32_t)) {
  552. *buffer = mmio_read_32(fifo_reg);
  553. buffer++;
  554. }
  555. }
  556. } while ((status & flags) == 0U);
  557. mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
  558. if ((status & SDMMC_STAR_DPSMACT) != 0U) {
  559. WARN("%s: DPSMACT=1, send stop\n", __func__);
  560. return stm32_sdmmc2_stop_transfer();
  561. }
  562. return 0;
  563. }
  564. static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size)
  565. {
  566. return 0;
  567. }
  568. static int stm32_sdmmc2_dt_get_config(void)
  569. {
  570. int sdmmc_node;
  571. void *fdt = NULL;
  572. const fdt32_t *cuint;
  573. struct dt_node_info dt_info;
  574. if (fdt_get_address(&fdt) == 0) {
  575. return -FDT_ERR_NOTFOUND;
  576. }
  577. if (fdt == NULL) {
  578. return -FDT_ERR_NOTFOUND;
  579. }
  580. sdmmc_node = dt_match_instance_by_compatible(DT_SDMMC2_COMPAT,
  581. sdmmc2_params.reg_base);
  582. if (sdmmc_node == -FDT_ERR_NOTFOUND) {
  583. return -FDT_ERR_NOTFOUND;
  584. }
  585. dt_fill_device_info(&dt_info, sdmmc_node);
  586. if (dt_info.status == DT_DISABLED) {
  587. return -FDT_ERR_NOTFOUND;
  588. }
  589. if (dt_set_pinctrl_config(sdmmc_node) != 0) {
  590. return -FDT_ERR_BADVALUE;
  591. }
  592. sdmmc2_params.clock_id = dt_info.clock;
  593. sdmmc2_params.reset_id = dt_info.reset;
  594. if ((fdt_getprop(fdt, sdmmc_node, "st,use-ckin", NULL)) != NULL) {
  595. sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
  596. }
  597. if ((fdt_getprop(fdt, sdmmc_node, "st,sig-dir", NULL)) != NULL) {
  598. sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
  599. }
  600. if ((fdt_getprop(fdt, sdmmc_node, "st,neg-edge", NULL)) != NULL) {
  601. sdmmc2_params.negedge = SDMMC_CLKCR_NEGEDGE;
  602. }
  603. cuint = fdt_getprop(fdt, sdmmc_node, "bus-width", NULL);
  604. if (cuint != NULL) {
  605. switch (fdt32_to_cpu(*cuint)) {
  606. case 4:
  607. sdmmc2_params.bus_width = MMC_BUS_WIDTH_4;
  608. break;
  609. case 8:
  610. sdmmc2_params.bus_width = MMC_BUS_WIDTH_8;
  611. break;
  612. default:
  613. break;
  614. }
  615. }
  616. cuint = fdt_getprop(fdt, sdmmc_node, "max-frequency", NULL);
  617. if (cuint != NULL) {
  618. sdmmc2_params.max_freq = fdt32_to_cpu(*cuint);
  619. }
  620. sdmmc2_params.vmmc_regu = regulator_get_by_supply_name(fdt, sdmmc_node, "vmmc");
  621. return 0;
  622. }
  623. unsigned long long stm32_sdmmc2_mmc_get_device_size(void)
  624. {
  625. return sdmmc2_params.device_info->device_size;
  626. }
  627. int stm32_sdmmc2_mmc_init(struct stm32_sdmmc2_params *params)
  628. {
  629. assert((params != NULL) &&
  630. ((params->reg_base & MMC_BLOCK_MASK) == 0U) &&
  631. ((params->bus_width == MMC_BUS_WIDTH_1) ||
  632. (params->bus_width == MMC_BUS_WIDTH_4) ||
  633. (params->bus_width == MMC_BUS_WIDTH_8)));
  634. memcpy(&sdmmc2_params, params, sizeof(struct stm32_sdmmc2_params));
  635. sdmmc2_params.vmmc_regu = NULL;
  636. if (stm32_sdmmc2_dt_get_config() != 0) {
  637. ERROR("%s: DT error\n", __func__);
  638. return -ENOMEM;
  639. }
  640. clk_enable(sdmmc2_params.clock_id);
  641. if ((int)sdmmc2_params.reset_id >= 0) {
  642. int rc;
  643. rc = stm32mp_reset_assert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
  644. if (rc != 0) {
  645. panic();
  646. }
  647. udelay(2);
  648. rc = stm32mp_reset_deassert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
  649. if (rc != 0) {
  650. panic();
  651. }
  652. mdelay(1);
  653. }
  654. sdmmc2_params.clk_rate = clk_get_rate(sdmmc2_params.clock_id);
  655. sdmmc2_params.device_info->ocr_voltage = OCR_3_2_3_3 | OCR_3_3_3_4;
  656. return mmc_init(&stm32_sdmmc2_ops, sdmmc2_params.clk_rate,
  657. sdmmc2_params.bus_width, sdmmc2_params.flags,
  658. sdmmc2_params.device_info);
  659. }