stm32_sdmmc2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  4. * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics.
  5. */
  6. #define LOG_CATEGORY UCLASS_MMC
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <cpu_func.h>
  10. #include <dm.h>
  11. #include <fdtdec.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <asm/bitops.h>
  15. #include <asm/cache.h>
  16. #include <dm/device_compat.h>
  17. #include <dm/pinctrl.h>
  18. #include <linux/bitops.h>
  19. #include <linux/delay.h>
  20. #include <linux/libfdt.h>
  21. #include <mmc.h>
  22. #include <reset.h>
  23. #include <asm/io.h>
  24. #include <asm/gpio.h>
  25. #include <linux/iopoll.h>
  26. #include <power/regulator.h>
  27. #include <watchdog.h>
  28. struct stm32_sdmmc2_plat {
  29. struct mmc_config cfg;
  30. struct mmc mmc;
  31. fdt_addr_t base;
  32. struct clk clk;
  33. struct reset_ctl reset_ctl;
  34. struct gpio_desc cd_gpio;
  35. u32 clk_reg_msk;
  36. u32 pwr_reg_msk;
  37. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  38. bool vqmmc_enabled;
  39. #endif
  40. };
  41. struct stm32_sdmmc2_ctx {
  42. u32 cache_start;
  43. u32 cache_end;
  44. u32 data_length;
  45. bool dpsm_abort;
  46. };
  47. /* SDMMC REGISTERS OFFSET */
  48. #define SDMMC_POWER 0x00 /* SDMMC power control */
  49. #define SDMMC_CLKCR 0x04 /* SDMMC clock control */
  50. #define SDMMC_ARG 0x08 /* SDMMC argument */
  51. #define SDMMC_CMD 0x0C /* SDMMC command */
  52. #define SDMMC_RESP1 0x14 /* SDMMC response 1 */
  53. #define SDMMC_RESP2 0x18 /* SDMMC response 2 */
  54. #define SDMMC_RESP3 0x1C /* SDMMC response 3 */
  55. #define SDMMC_RESP4 0x20 /* SDMMC response 4 */
  56. #define SDMMC_DTIMER 0x24 /* SDMMC data timer */
  57. #define SDMMC_DLEN 0x28 /* SDMMC data length */
  58. #define SDMMC_DCTRL 0x2C /* SDMMC data control */
  59. #define SDMMC_DCOUNT 0x30 /* SDMMC data counter */
  60. #define SDMMC_STA 0x34 /* SDMMC status */
  61. #define SDMMC_ICR 0x38 /* SDMMC interrupt clear */
  62. #define SDMMC_MASK 0x3C /* SDMMC mask */
  63. #define SDMMC_IDMACTRL 0x50 /* SDMMC DMA control */
  64. #define SDMMC_IDMABASE0 0x58 /* SDMMC DMA buffer 0 base address */
  65. /* SDMMC_POWER register */
  66. #define SDMMC_POWER_PWRCTRL_MASK GENMASK(1, 0)
  67. #define SDMMC_POWER_PWRCTRL_OFF 0
  68. #define SDMMC_POWER_PWRCTRL_CYCLE 2
  69. #define SDMMC_POWER_PWRCTRL_ON 3
  70. #define SDMMC_POWER_VSWITCH BIT(2)
  71. #define SDMMC_POWER_VSWITCHEN BIT(3)
  72. #define SDMMC_POWER_DIRPOL BIT(4)
  73. /* SDMMC_CLKCR register */
  74. #define SDMMC_CLKCR_CLKDIV GENMASK(9, 0)
  75. #define SDMMC_CLKCR_CLKDIV_MAX SDMMC_CLKCR_CLKDIV
  76. #define SDMMC_CLKCR_PWRSAV BIT(12)
  77. #define SDMMC_CLKCR_WIDBUS_4 BIT(14)
  78. #define SDMMC_CLKCR_WIDBUS_8 BIT(15)
  79. #define SDMMC_CLKCR_NEGEDGE BIT(16)
  80. #define SDMMC_CLKCR_HWFC_EN BIT(17)
  81. #define SDMMC_CLKCR_DDR BIT(18)
  82. #define SDMMC_CLKCR_BUSSPEED BIT(19)
  83. #define SDMMC_CLKCR_SELCLKRX_MASK GENMASK(21, 20)
  84. #define SDMMC_CLKCR_SELCLKRX_CK 0
  85. #define SDMMC_CLKCR_SELCLKRX_CKIN BIT(20)
  86. #define SDMMC_CLKCR_SELCLKRX_FBCK BIT(21)
  87. /* SDMMC_CMD register */
  88. #define SDMMC_CMD_CMDINDEX GENMASK(5, 0)
  89. #define SDMMC_CMD_CMDTRANS BIT(6)
  90. #define SDMMC_CMD_CMDSTOP BIT(7)
  91. #define SDMMC_CMD_WAITRESP GENMASK(9, 8)
  92. #define SDMMC_CMD_WAITRESP_0 BIT(8)
  93. #define SDMMC_CMD_WAITRESP_1 BIT(9)
  94. #define SDMMC_CMD_WAITINT BIT(10)
  95. #define SDMMC_CMD_WAITPEND BIT(11)
  96. #define SDMMC_CMD_CPSMEN BIT(12)
  97. #define SDMMC_CMD_DTHOLD BIT(13)
  98. #define SDMMC_CMD_BOOTMODE BIT(14)
  99. #define SDMMC_CMD_BOOTEN BIT(15)
  100. #define SDMMC_CMD_CMDSUSPEND BIT(16)
  101. /* SDMMC_DCTRL register */
  102. #define SDMMC_DCTRL_DTEN BIT(0)
  103. #define SDMMC_DCTRL_DTDIR BIT(1)
  104. #define SDMMC_DCTRL_DTMODE GENMASK(3, 2)
  105. #define SDMMC_DCTRL_DBLOCKSIZE GENMASK(7, 4)
  106. #define SDMMC_DCTRL_DBLOCKSIZE_SHIFT 4
  107. #define SDMMC_DCTRL_RWSTART BIT(8)
  108. #define SDMMC_DCTRL_RWSTOP BIT(9)
  109. #define SDMMC_DCTRL_RWMOD BIT(10)
  110. #define SDMMC_DCTRL_SDMMCEN BIT(11)
  111. #define SDMMC_DCTRL_BOOTACKEN BIT(12)
  112. #define SDMMC_DCTRL_FIFORST BIT(13)
  113. /* SDMMC_STA register */
  114. #define SDMMC_STA_CCRCFAIL BIT(0)
  115. #define SDMMC_STA_DCRCFAIL BIT(1)
  116. #define SDMMC_STA_CTIMEOUT BIT(2)
  117. #define SDMMC_STA_DTIMEOUT BIT(3)
  118. #define SDMMC_STA_TXUNDERR BIT(4)
  119. #define SDMMC_STA_RXOVERR BIT(5)
  120. #define SDMMC_STA_CMDREND BIT(6)
  121. #define SDMMC_STA_CMDSENT BIT(7)
  122. #define SDMMC_STA_DATAEND BIT(8)
  123. #define SDMMC_STA_DHOLD BIT(9)
  124. #define SDMMC_STA_DBCKEND BIT(10)
  125. #define SDMMC_STA_DABORT BIT(11)
  126. #define SDMMC_STA_DPSMACT BIT(12)
  127. #define SDMMC_STA_CPSMACT BIT(13)
  128. #define SDMMC_STA_TXFIFOHE BIT(14)
  129. #define SDMMC_STA_RXFIFOHF BIT(15)
  130. #define SDMMC_STA_TXFIFOF BIT(16)
  131. #define SDMMC_STA_RXFIFOF BIT(17)
  132. #define SDMMC_STA_TXFIFOE BIT(18)
  133. #define SDMMC_STA_RXFIFOE BIT(19)
  134. #define SDMMC_STA_BUSYD0 BIT(20)
  135. #define SDMMC_STA_BUSYD0END BIT(21)
  136. #define SDMMC_STA_SDMMCIT BIT(22)
  137. #define SDMMC_STA_ACKFAIL BIT(23)
  138. #define SDMMC_STA_ACKTIMEOUT BIT(24)
  139. #define SDMMC_STA_VSWEND BIT(25)
  140. #define SDMMC_STA_CKSTOP BIT(26)
  141. #define SDMMC_STA_IDMATE BIT(27)
  142. #define SDMMC_STA_IDMABTC BIT(28)
  143. /* SDMMC_ICR register */
  144. #define SDMMC_ICR_CCRCFAILC BIT(0)
  145. #define SDMMC_ICR_DCRCFAILC BIT(1)
  146. #define SDMMC_ICR_CTIMEOUTC BIT(2)
  147. #define SDMMC_ICR_DTIMEOUTC BIT(3)
  148. #define SDMMC_ICR_TXUNDERRC BIT(4)
  149. #define SDMMC_ICR_RXOVERRC BIT(5)
  150. #define SDMMC_ICR_CMDRENDC BIT(6)
  151. #define SDMMC_ICR_CMDSENTC BIT(7)
  152. #define SDMMC_ICR_DATAENDC BIT(8)
  153. #define SDMMC_ICR_DHOLDC BIT(9)
  154. #define SDMMC_ICR_DBCKENDC BIT(10)
  155. #define SDMMC_ICR_DABORTC BIT(11)
  156. #define SDMMC_ICR_BUSYD0ENDC BIT(21)
  157. #define SDMMC_ICR_SDMMCITC BIT(22)
  158. #define SDMMC_ICR_ACKFAILC BIT(23)
  159. #define SDMMC_ICR_ACKTIMEOUTC BIT(24)
  160. #define SDMMC_ICR_VSWENDC BIT(25)
  161. #define SDMMC_ICR_CKSTOPC BIT(26)
  162. #define SDMMC_ICR_IDMATEC BIT(27)
  163. #define SDMMC_ICR_IDMABTCC BIT(28)
  164. #define SDMMC_ICR_STATIC_FLAGS ((GENMASK(28, 21)) | (GENMASK(11, 0)))
  165. /* SDMMC_MASK register */
  166. #define SDMMC_MASK_CCRCFAILIE BIT(0)
  167. #define SDMMC_MASK_DCRCFAILIE BIT(1)
  168. #define SDMMC_MASK_CTIMEOUTIE BIT(2)
  169. #define SDMMC_MASK_DTIMEOUTIE BIT(3)
  170. #define SDMMC_MASK_TXUNDERRIE BIT(4)
  171. #define SDMMC_MASK_RXOVERRIE BIT(5)
  172. #define SDMMC_MASK_CMDRENDIE BIT(6)
  173. #define SDMMC_MASK_CMDSENTIE BIT(7)
  174. #define SDMMC_MASK_DATAENDIE BIT(8)
  175. #define SDMMC_MASK_DHOLDIE BIT(9)
  176. #define SDMMC_MASK_DBCKENDIE BIT(10)
  177. #define SDMMC_MASK_DABORTIE BIT(11)
  178. #define SDMMC_MASK_TXFIFOHEIE BIT(14)
  179. #define SDMMC_MASK_RXFIFOHFIE BIT(15)
  180. #define SDMMC_MASK_RXFIFOFIE BIT(17)
  181. #define SDMMC_MASK_TXFIFOEIE BIT(18)
  182. #define SDMMC_MASK_BUSYD0ENDIE BIT(21)
  183. #define SDMMC_MASK_SDMMCITIE BIT(22)
  184. #define SDMMC_MASK_ACKFAILIE BIT(23)
  185. #define SDMMC_MASK_ACKTIMEOUTIE BIT(24)
  186. #define SDMMC_MASK_VSWENDIE BIT(25)
  187. #define SDMMC_MASK_CKSTOPIE BIT(26)
  188. #define SDMMC_MASK_IDMABTCIE BIT(28)
  189. /* SDMMC_IDMACTRL register */
  190. #define SDMMC_IDMACTRL_IDMAEN BIT(0)
  191. #define SDMMC_CMD_TIMEOUT 0xFFFFFFFF
  192. #define SDMMC_BUSYD0END_TIMEOUT_US 2000000
  193. static void stm32_sdmmc2_start_data(struct udevice *dev,
  194. struct mmc_data *data,
  195. struct stm32_sdmmc2_ctx *ctx)
  196. {
  197. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  198. u32 data_ctrl, idmabase0;
  199. /* Configure the SDMMC DPSM (Data Path State Machine) */
  200. data_ctrl = (__ilog2(data->blocksize) <<
  201. SDMMC_DCTRL_DBLOCKSIZE_SHIFT) &
  202. SDMMC_DCTRL_DBLOCKSIZE;
  203. if (data->flags & MMC_DATA_READ) {
  204. data_ctrl |= SDMMC_DCTRL_DTDIR;
  205. idmabase0 = (u32)data->dest;
  206. } else {
  207. idmabase0 = (u32)data->src;
  208. }
  209. /* Set the SDMMC DataLength value */
  210. writel(ctx->data_length, plat->base + SDMMC_DLEN);
  211. /* Write to SDMMC DCTRL */
  212. writel(data_ctrl, plat->base + SDMMC_DCTRL);
  213. /* Cache align */
  214. ctx->cache_start = rounddown(idmabase0, ARCH_DMA_MINALIGN);
  215. ctx->cache_end = roundup(idmabase0 + ctx->data_length,
  216. ARCH_DMA_MINALIGN);
  217. /*
  218. * Flush data cache before DMA start (clean and invalidate)
  219. * Clean also needed for read
  220. * Avoid issue on buffer not cached-aligned
  221. */
  222. flush_dcache_range(ctx->cache_start, ctx->cache_end);
  223. /* Enable internal DMA */
  224. writel(idmabase0, plat->base + SDMMC_IDMABASE0);
  225. writel(SDMMC_IDMACTRL_IDMAEN, plat->base + SDMMC_IDMACTRL);
  226. }
  227. static void stm32_sdmmc2_start_cmd(struct udevice *dev,
  228. struct mmc_cmd *cmd, u32 cmd_param,
  229. struct stm32_sdmmc2_ctx *ctx)
  230. {
  231. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  232. u32 timeout = 0;
  233. if (readl(plat->base + SDMMC_CMD) & SDMMC_CMD_CPSMEN)
  234. writel(0, plat->base + SDMMC_CMD);
  235. cmd_param |= cmd->cmdidx | SDMMC_CMD_CPSMEN;
  236. if (cmd->resp_type & MMC_RSP_PRESENT) {
  237. if (cmd->resp_type & MMC_RSP_136)
  238. cmd_param |= SDMMC_CMD_WAITRESP;
  239. else if (cmd->resp_type & MMC_RSP_CRC)
  240. cmd_param |= SDMMC_CMD_WAITRESP_0;
  241. else
  242. cmd_param |= SDMMC_CMD_WAITRESP_1;
  243. }
  244. /*
  245. * SDMMC_DTIME must be set in two case:
  246. * - on data transfert.
  247. * - on busy request.
  248. * If not done or too short, the dtimeout flag occurs and DPSM stays
  249. * enabled/busy and waits for abort (stop transmission cmd).
  250. * Next data command is not possible whereas DPSM is activated.
  251. */
  252. if (ctx->data_length) {
  253. timeout = SDMMC_CMD_TIMEOUT;
  254. } else {
  255. writel(0, plat->base + SDMMC_DCTRL);
  256. if (cmd->resp_type & MMC_RSP_BUSY)
  257. timeout = SDMMC_CMD_TIMEOUT;
  258. }
  259. /* Set the SDMMC Data TimeOut value */
  260. writel(timeout, plat->base + SDMMC_DTIMER);
  261. /* Clear flags */
  262. writel(SDMMC_ICR_STATIC_FLAGS, plat->base + SDMMC_ICR);
  263. /* Set SDMMC argument value */
  264. writel(cmd->cmdarg, plat->base + SDMMC_ARG);
  265. /* Set SDMMC command parameters */
  266. writel(cmd_param, plat->base + SDMMC_CMD);
  267. }
  268. static int stm32_sdmmc2_end_cmd(struct udevice *dev,
  269. struct mmc_cmd *cmd,
  270. struct stm32_sdmmc2_ctx *ctx)
  271. {
  272. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  273. u32 mask = SDMMC_STA_CTIMEOUT;
  274. u32 status;
  275. int ret;
  276. if (cmd->resp_type & MMC_RSP_PRESENT) {
  277. mask |= SDMMC_STA_CMDREND;
  278. if (cmd->resp_type & MMC_RSP_CRC)
  279. mask |= SDMMC_STA_CCRCFAIL;
  280. } else {
  281. mask |= SDMMC_STA_CMDSENT;
  282. }
  283. /* Polling status register */
  284. ret = readl_poll_timeout(plat->base + SDMMC_STA, status, status & mask,
  285. 10000);
  286. if (ret < 0) {
  287. dev_dbg(dev, "timeout reading SDMMC_STA register\n");
  288. ctx->dpsm_abort = true;
  289. return ret;
  290. }
  291. /* Check status */
  292. if (status & SDMMC_STA_CTIMEOUT) {
  293. dev_dbg(dev, "error SDMMC_STA_CTIMEOUT (0x%x) for cmd %d\n",
  294. status, cmd->cmdidx);
  295. ctx->dpsm_abort = true;
  296. return -ETIMEDOUT;
  297. }
  298. if (status & SDMMC_STA_CCRCFAIL && cmd->resp_type & MMC_RSP_CRC) {
  299. dev_dbg(dev, "error SDMMC_STA_CCRCFAIL (0x%x) for cmd %d\n",
  300. status, cmd->cmdidx);
  301. ctx->dpsm_abort = true;
  302. return -EILSEQ;
  303. }
  304. if (status & SDMMC_STA_CMDREND && cmd->resp_type & MMC_RSP_PRESENT) {
  305. cmd->response[0] = readl(plat->base + SDMMC_RESP1);
  306. if (cmd->resp_type & MMC_RSP_136) {
  307. cmd->response[1] = readl(plat->base + SDMMC_RESP2);
  308. cmd->response[2] = readl(plat->base + SDMMC_RESP3);
  309. cmd->response[3] = readl(plat->base + SDMMC_RESP4);
  310. }
  311. /* Wait for BUSYD0END flag if busy status is detected */
  312. if (cmd->resp_type & MMC_RSP_BUSY &&
  313. status & SDMMC_STA_BUSYD0) {
  314. mask = SDMMC_STA_DTIMEOUT | SDMMC_STA_BUSYD0END;
  315. /* Polling status register */
  316. ret = readl_poll_timeout(plat->base + SDMMC_STA,
  317. status, status & mask,
  318. SDMMC_BUSYD0END_TIMEOUT_US);
  319. if (ret < 0) {
  320. dev_dbg(dev, "timeout reading SDMMC_STA\n");
  321. ctx->dpsm_abort = true;
  322. return ret;
  323. }
  324. if (status & SDMMC_STA_DTIMEOUT) {
  325. dev_dbg(dev,
  326. "error SDMMC_STA_DTIMEOUT (0x%x)\n",
  327. status);
  328. ctx->dpsm_abort = true;
  329. return -ETIMEDOUT;
  330. }
  331. }
  332. }
  333. return 0;
  334. }
  335. static int stm32_sdmmc2_end_data(struct udevice *dev,
  336. struct mmc_cmd *cmd,
  337. struct mmc_data *data,
  338. struct stm32_sdmmc2_ctx *ctx)
  339. {
  340. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  341. u32 mask = SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT |
  342. SDMMC_STA_IDMATE | SDMMC_STA_DATAEND;
  343. u32 status;
  344. if (data->flags & MMC_DATA_READ)
  345. mask |= SDMMC_STA_RXOVERR;
  346. else
  347. mask |= SDMMC_STA_TXUNDERR;
  348. status = readl(plat->base + SDMMC_STA);
  349. while (!(status & mask))
  350. status = readl(plat->base + SDMMC_STA);
  351. /*
  352. * Need invalidate the dcache again to avoid any
  353. * cache-refill during the DMA operations (pre-fetching)
  354. */
  355. if (data->flags & MMC_DATA_READ)
  356. invalidate_dcache_range(ctx->cache_start, ctx->cache_end);
  357. if (status & SDMMC_STA_DCRCFAIL) {
  358. dev_dbg(dev, "error SDMMC_STA_DCRCFAIL (0x%x) for cmd %d\n",
  359. status, cmd->cmdidx);
  360. if (readl(plat->base + SDMMC_DCOUNT))
  361. ctx->dpsm_abort = true;
  362. return -EILSEQ;
  363. }
  364. if (status & SDMMC_STA_DTIMEOUT) {
  365. dev_dbg(dev, "error SDMMC_STA_DTIMEOUT (0x%x) for cmd %d\n",
  366. status, cmd->cmdidx);
  367. ctx->dpsm_abort = true;
  368. return -ETIMEDOUT;
  369. }
  370. if (status & SDMMC_STA_TXUNDERR) {
  371. dev_dbg(dev, "error SDMMC_STA_TXUNDERR (0x%x) for cmd %d\n",
  372. status, cmd->cmdidx);
  373. ctx->dpsm_abort = true;
  374. return -EIO;
  375. }
  376. if (status & SDMMC_STA_RXOVERR) {
  377. dev_dbg(dev, "error SDMMC_STA_RXOVERR (0x%x) for cmd %d\n",
  378. status, cmd->cmdidx);
  379. ctx->dpsm_abort = true;
  380. return -EIO;
  381. }
  382. if (status & SDMMC_STA_IDMATE) {
  383. dev_dbg(dev, "error SDMMC_STA_IDMATE (0x%x) for cmd %d\n",
  384. status, cmd->cmdidx);
  385. ctx->dpsm_abort = true;
  386. return -EIO;
  387. }
  388. return 0;
  389. }
  390. static int stm32_sdmmc2_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  391. struct mmc_data *data)
  392. {
  393. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  394. struct stm32_sdmmc2_ctx ctx;
  395. u32 cmdat = data ? SDMMC_CMD_CMDTRANS : 0;
  396. int ret, retry = 3;
  397. schedule();
  398. retry_cmd:
  399. ctx.data_length = 0;
  400. ctx.dpsm_abort = false;
  401. if (data) {
  402. ctx.data_length = data->blocks * data->blocksize;
  403. stm32_sdmmc2_start_data(dev, data, &ctx);
  404. }
  405. stm32_sdmmc2_start_cmd(dev, cmd, cmdat, &ctx);
  406. dev_dbg(dev, "send cmd %d data: 0x%x @ 0x%x\n",
  407. cmd->cmdidx, data ? ctx.data_length : 0, (unsigned int)data);
  408. ret = stm32_sdmmc2_end_cmd(dev, cmd, &ctx);
  409. if (data && !ret)
  410. ret = stm32_sdmmc2_end_data(dev, cmd, data, &ctx);
  411. /* Clear flags */
  412. writel(SDMMC_ICR_STATIC_FLAGS, plat->base + SDMMC_ICR);
  413. if (data)
  414. writel(0x0, plat->base + SDMMC_IDMACTRL);
  415. /*
  416. * To stop Data Path State Machine, a stop_transmission command
  417. * shall be send on cmd or data errors.
  418. */
  419. if (ctx.dpsm_abort && (cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) {
  420. struct mmc_cmd stop_cmd;
  421. stop_cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  422. stop_cmd.cmdarg = 0;
  423. stop_cmd.resp_type = MMC_RSP_R1b;
  424. dev_dbg(dev, "send STOP command to abort dpsm treatments\n");
  425. ctx.data_length = 0;
  426. stm32_sdmmc2_start_cmd(dev, &stop_cmd,
  427. SDMMC_CMD_CMDSTOP, &ctx);
  428. stm32_sdmmc2_end_cmd(dev, &stop_cmd, &ctx);
  429. writel(SDMMC_ICR_STATIC_FLAGS, plat->base + SDMMC_ICR);
  430. }
  431. if ((ret != -ETIMEDOUT) && (ret != 0) && retry) {
  432. dev_err(dev, "cmd %d failed, retrying ...\n", cmd->cmdidx);
  433. retry--;
  434. goto retry_cmd;
  435. }
  436. dev_dbg(dev, "end for CMD %d, ret = %d\n", cmd->cmdidx, ret);
  437. return ret;
  438. }
  439. /*
  440. * Reset the SDMMC with the RCC.SDMMCxRST register bit.
  441. * This will reset the SDMMC to the reset state and the CPSM and DPSM
  442. * to the Idle state. SDMMC is disabled, Signals Hiz.
  443. */
  444. static void stm32_sdmmc2_reset(struct stm32_sdmmc2_plat *plat)
  445. {
  446. if (reset_valid(&plat->reset_ctl)) {
  447. /* Reset */
  448. reset_assert(&plat->reset_ctl);
  449. udelay(2);
  450. reset_deassert(&plat->reset_ctl);
  451. }
  452. /* init the needed SDMMC register after reset */
  453. writel(plat->pwr_reg_msk, plat->base + SDMMC_POWER);
  454. }
  455. /*
  456. * Set the SDMMC in power-cycle state.
  457. * This will make that the SDMMC_D[7:0],
  458. * SDMMC_CMD and SDMMC_CK are driven low, to prevent the card from being
  459. * supplied through the signal lines.
  460. */
  461. static void stm32_sdmmc2_pwrcycle(struct stm32_sdmmc2_plat *plat)
  462. {
  463. if ((readl(plat->base + SDMMC_POWER) & SDMMC_POWER_PWRCTRL_MASK) ==
  464. SDMMC_POWER_PWRCTRL_CYCLE)
  465. return;
  466. stm32_sdmmc2_reset(plat);
  467. }
  468. /*
  469. * set the SDMMC state Power-on: the card is clocked
  470. * manage the SDMMC state control:
  471. * Reset => Power-Cycle => Power-Off => Power
  472. * PWRCTRL=10 PWCTRL=00 PWCTRL=11
  473. */
  474. static void stm32_sdmmc2_pwron(struct stm32_sdmmc2_plat *plat)
  475. {
  476. u32 pwrctrl =
  477. readl(plat->base + SDMMC_POWER) & SDMMC_POWER_PWRCTRL_MASK;
  478. if (pwrctrl == SDMMC_POWER_PWRCTRL_ON)
  479. return;
  480. /* warning: same PWRCTRL value after reset and for power-off state
  481. * it is the reset state here = the only managed by the driver
  482. */
  483. if (pwrctrl == SDMMC_POWER_PWRCTRL_OFF) {
  484. writel(SDMMC_POWER_PWRCTRL_CYCLE | plat->pwr_reg_msk,
  485. plat->base + SDMMC_POWER);
  486. }
  487. /*
  488. * the remaining case is SDMMC_POWER_PWRCTRL_CYCLE
  489. * switch to Power-Off state: SDMCC disable, signals drive 1
  490. */
  491. writel(SDMMC_POWER_PWRCTRL_OFF | plat->pwr_reg_msk,
  492. plat->base + SDMMC_POWER);
  493. /* After the 1ms delay set the SDMMC to power-on */
  494. mdelay(1);
  495. writel(SDMMC_POWER_PWRCTRL_ON | plat->pwr_reg_msk,
  496. plat->base + SDMMC_POWER);
  497. /* during the first 74 SDMMC_CK cycles the SDMMC is still disabled. */
  498. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  499. if (plat->mmc.vqmmc_supply && !plat->vqmmc_enabled) {
  500. if (regulator_set_enable_if_allowed(plat->mmc.vqmmc_supply, true))
  501. dev_dbg(plat->mmc.dev, "failed to enable vqmmc-supply\n");
  502. else
  503. plat->vqmmc_enabled = true;
  504. }
  505. #endif
  506. }
  507. #define IS_RISING_EDGE(reg) (reg & SDMMC_CLKCR_NEGEDGE ? 0 : 1)
  508. static int stm32_sdmmc2_set_ios(struct udevice *dev)
  509. {
  510. struct mmc *mmc = mmc_get_mmc_dev(dev);
  511. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  512. u32 desired = mmc->clock;
  513. u32 sys_clock = clk_get_rate(&plat->clk);
  514. u32 clk = 0;
  515. dev_dbg(dev, "bus_with = %d, clock = %d\n",
  516. mmc->bus_width, mmc->clock);
  517. if (mmc->clk_disable)
  518. stm32_sdmmc2_pwrcycle(plat);
  519. else
  520. stm32_sdmmc2_pwron(plat);
  521. /*
  522. * clk_div = 0 => command and data generated on SDMMCCLK falling edge
  523. * clk_div > 0 and NEGEDGE = 0 => command and data generated on
  524. * SDMMCCLK rising edge
  525. * clk_div > 0 and NEGEDGE = 1 => command and data generated on
  526. * SDMMCCLK falling edge
  527. */
  528. if (desired && (sys_clock > desired || mmc->ddr_mode ||
  529. IS_RISING_EDGE(plat->clk_reg_msk))) {
  530. clk = DIV_ROUND_UP(sys_clock, 2 * desired);
  531. if (clk > SDMMC_CLKCR_CLKDIV_MAX)
  532. clk = SDMMC_CLKCR_CLKDIV_MAX;
  533. }
  534. if (mmc->ddr_mode)
  535. clk |= SDMMC_CLKCR_DDR;
  536. if (mmc->bus_width == 4)
  537. clk |= SDMMC_CLKCR_WIDBUS_4;
  538. if (mmc->bus_width == 8)
  539. clk |= SDMMC_CLKCR_WIDBUS_8;
  540. writel(clk | plat->clk_reg_msk | SDMMC_CLKCR_HWFC_EN,
  541. plat->base + SDMMC_CLKCR);
  542. return 0;
  543. }
  544. static int stm32_sdmmc2_getcd(struct udevice *dev)
  545. {
  546. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  547. dev_dbg(dev, "%s called\n", __func__);
  548. if (dm_gpio_is_valid(&plat->cd_gpio))
  549. return dm_gpio_get_value(&plat->cd_gpio);
  550. return 1;
  551. }
  552. static int stm32_sdmmc2_host_power_cycle(struct udevice *dev)
  553. {
  554. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  555. writel(SDMMC_POWER_PWRCTRL_CYCLE | plat->pwr_reg_msk,
  556. plat->base + SDMMC_POWER);
  557. return 0;
  558. }
  559. static const struct dm_mmc_ops stm32_sdmmc2_ops = {
  560. .send_cmd = stm32_sdmmc2_send_cmd,
  561. .set_ios = stm32_sdmmc2_set_ios,
  562. .get_cd = stm32_sdmmc2_getcd,
  563. .host_power_cycle = stm32_sdmmc2_host_power_cycle,
  564. };
  565. static int stm32_sdmmc2_of_to_plat(struct udevice *dev)
  566. {
  567. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  568. struct mmc_config *cfg = &plat->cfg;
  569. int ret;
  570. plat->base = dev_read_addr(dev);
  571. if (plat->base == FDT_ADDR_T_NONE)
  572. return -EINVAL;
  573. if (dev_read_bool(dev, "st,neg-edge"))
  574. plat->clk_reg_msk |= SDMMC_CLKCR_NEGEDGE;
  575. if (dev_read_bool(dev, "st,sig-dir"))
  576. plat->pwr_reg_msk |= SDMMC_POWER_DIRPOL;
  577. if (dev_read_bool(dev, "st,use-ckin"))
  578. plat->clk_reg_msk |= SDMMC_CLKCR_SELCLKRX_CKIN;
  579. cfg->f_min = 400000;
  580. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  581. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  582. cfg->name = "STM32 SD/MMC";
  583. cfg->host_caps = 0;
  584. cfg->f_max = 52000000;
  585. ret = mmc_of_parse(dev, cfg);
  586. if (ret)
  587. return ret;
  588. cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_HS400 | MMC_MODE_HS400_ES);
  589. ret = clk_get_by_index(dev, 0, &plat->clk);
  590. if (ret)
  591. return ret;
  592. ret = reset_get_by_index(dev, 0, &plat->reset_ctl);
  593. if (ret)
  594. dev_dbg(dev, "No reset provided\n");
  595. gpio_request_by_name(dev, "cd-gpios", 0, &plat->cd_gpio,
  596. GPIOD_IS_IN);
  597. return 0;
  598. }
  599. static int stm32_sdmmc2_probe_level_translator(struct udevice *dev)
  600. {
  601. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  602. struct gpio_desc cmd_gpio;
  603. struct gpio_desc ck_gpio;
  604. struct gpio_desc ckin_gpio;
  605. int clk_hi, clk_lo, ret;
  606. ret = gpio_request_by_name(dev, "st,cmd-gpios", 0, &cmd_gpio,
  607. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  608. if (ret)
  609. goto exit_cmd;
  610. ret = gpio_request_by_name(dev, "st,ck-gpios", 0, &ck_gpio,
  611. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  612. if (ret)
  613. goto exit_ck;
  614. ret = gpio_request_by_name(dev, "st,ckin-gpios", 0, &ckin_gpio,
  615. GPIOD_IS_IN);
  616. if (ret)
  617. goto exit_ckin;
  618. /* All GPIOs are valid, test whether level translator works */
  619. /* Sample CKIN */
  620. clk_hi = !!dm_gpio_get_value(&ckin_gpio);
  621. /* Set CK low */
  622. dm_gpio_set_value(&ck_gpio, 0);
  623. /* Sample CKIN */
  624. clk_lo = !!dm_gpio_get_value(&ckin_gpio);
  625. /* Tristate all */
  626. dm_gpio_set_dir_flags(&cmd_gpio, GPIOD_IS_IN);
  627. dm_gpio_set_dir_flags(&ck_gpio, GPIOD_IS_IN);
  628. /* Level translator is present if CK signal is propagated to CKIN */
  629. if (!clk_hi || clk_lo)
  630. plat->clk_reg_msk &= ~SDMMC_CLKCR_SELCLKRX_CKIN;
  631. dm_gpio_free(dev, &ckin_gpio);
  632. exit_ckin:
  633. dm_gpio_free(dev, &ck_gpio);
  634. exit_ck:
  635. dm_gpio_free(dev, &cmd_gpio);
  636. exit_cmd:
  637. pinctrl_select_state(dev, "default");
  638. return 0;
  639. }
  640. static int stm32_sdmmc2_probe(struct udevice *dev)
  641. {
  642. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  643. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  644. int ret;
  645. ret = clk_enable(&plat->clk);
  646. if (ret) {
  647. clk_free(&plat->clk);
  648. return ret;
  649. }
  650. upriv->mmc = &plat->mmc;
  651. if (plat->clk_reg_msk & SDMMC_CLKCR_SELCLKRX_CKIN)
  652. stm32_sdmmc2_probe_level_translator(dev);
  653. /* SDMMC init */
  654. stm32_sdmmc2_reset(plat);
  655. return 0;
  656. }
  657. static int stm32_sdmmc2_bind(struct udevice *dev)
  658. {
  659. struct stm32_sdmmc2_plat *plat = dev_get_plat(dev);
  660. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  661. }
  662. static const struct udevice_id stm32_sdmmc2_ids[] = {
  663. { .compatible = "st,stm32-sdmmc2" },
  664. { }
  665. };
  666. U_BOOT_DRIVER(stm32_sdmmc2) = {
  667. .name = "stm32_sdmmc2",
  668. .id = UCLASS_MMC,
  669. .of_match = stm32_sdmmc2_ids,
  670. .ops = &stm32_sdmmc2_ops,
  671. .probe = stm32_sdmmc2_probe,
  672. .bind = stm32_sdmmc2_bind,
  673. .of_to_plat = stm32_sdmmc2_of_to_plat,
  674. .plat_auto = sizeof(struct stm32_sdmmc2_plat),
  675. };