stm32_sdmmc2.c 19 KB

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