mmci_stm32_sdmmc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
  4. * Author: Ludovic.barre@st.com for STMicroelectronics.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/mmc/host.h>
  9. #include <linux/mmc/card.h>
  10. #include <linux/reset.h>
  11. #include <linux/scatterlist.h>
  12. #include "mmci.h"
  13. #define SDMMC_LLI_BUF_LEN PAGE_SIZE
  14. #define SDMMC_IDMA_BURST BIT(MMCI_STM32_IDMABNDT_SHIFT)
  15. struct sdmmc_lli_desc {
  16. u32 idmalar;
  17. u32 idmabase;
  18. u32 idmasize;
  19. };
  20. struct sdmmc_priv {
  21. dma_addr_t sg_dma;
  22. void *sg_cpu;
  23. };
  24. int sdmmc_idma_validate_data(struct mmci_host *host,
  25. struct mmc_data *data)
  26. {
  27. struct scatterlist *sg;
  28. int i;
  29. /*
  30. * idma has constraints on idmabase & idmasize for each element
  31. * excepted the last element which has no constraint on idmasize
  32. */
  33. for_each_sg(data->sg, sg, data->sg_len - 1, i) {
  34. if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32)) ||
  35. !IS_ALIGNED(sg_dma_len(data->sg), SDMMC_IDMA_BURST)) {
  36. dev_err(mmc_dev(host->mmc),
  37. "unaligned scatterlist: ofst:%x length:%d\n",
  38. data->sg->offset, data->sg->length);
  39. return -EINVAL;
  40. }
  41. }
  42. if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32))) {
  43. dev_err(mmc_dev(host->mmc),
  44. "unaligned last scatterlist: ofst:%x length:%d\n",
  45. data->sg->offset, data->sg->length);
  46. return -EINVAL;
  47. }
  48. return 0;
  49. }
  50. static int _sdmmc_idma_prep_data(struct mmci_host *host,
  51. struct mmc_data *data)
  52. {
  53. int n_elem;
  54. n_elem = dma_map_sg(mmc_dev(host->mmc),
  55. data->sg,
  56. data->sg_len,
  57. mmc_get_dma_dir(data));
  58. if (!n_elem) {
  59. dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
  60. return -EINVAL;
  61. }
  62. return 0;
  63. }
  64. static int sdmmc_idma_prep_data(struct mmci_host *host,
  65. struct mmc_data *data, bool next)
  66. {
  67. /* Check if job is already prepared. */
  68. if (!next && data->host_cookie == host->next_cookie)
  69. return 0;
  70. return _sdmmc_idma_prep_data(host, data);
  71. }
  72. static void sdmmc_idma_unprep_data(struct mmci_host *host,
  73. struct mmc_data *data, int err)
  74. {
  75. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  76. mmc_get_dma_dir(data));
  77. }
  78. static int sdmmc_idma_setup(struct mmci_host *host)
  79. {
  80. struct sdmmc_priv *idma;
  81. idma = devm_kzalloc(mmc_dev(host->mmc), sizeof(*idma), GFP_KERNEL);
  82. if (!idma)
  83. return -ENOMEM;
  84. host->dma_priv = idma;
  85. if (host->variant->dma_lli) {
  86. idma->sg_cpu = dmam_alloc_coherent(mmc_dev(host->mmc),
  87. SDMMC_LLI_BUF_LEN,
  88. &idma->sg_dma, GFP_KERNEL);
  89. if (!idma->sg_cpu) {
  90. dev_err(mmc_dev(host->mmc),
  91. "Failed to alloc IDMA descriptor\n");
  92. return -ENOMEM;
  93. }
  94. host->mmc->max_segs = SDMMC_LLI_BUF_LEN /
  95. sizeof(struct sdmmc_lli_desc);
  96. host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask;
  97. } else {
  98. host->mmc->max_segs = 1;
  99. host->mmc->max_seg_size = host->mmc->max_req_size;
  100. }
  101. return 0;
  102. }
  103. static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
  104. {
  105. struct sdmmc_priv *idma = host->dma_priv;
  106. struct sdmmc_lli_desc *desc = (struct sdmmc_lli_desc *)idma->sg_cpu;
  107. struct mmc_data *data = host->data;
  108. struct scatterlist *sg;
  109. int i;
  110. if (!host->variant->dma_lli || data->sg_len == 1) {
  111. writel_relaxed(sg_dma_address(data->sg),
  112. host->base + MMCI_STM32_IDMABASE0R);
  113. writel_relaxed(MMCI_STM32_IDMAEN,
  114. host->base + MMCI_STM32_IDMACTRLR);
  115. return 0;
  116. }
  117. for_each_sg(data->sg, sg, data->sg_len, i) {
  118. desc[i].idmalar = (i + 1) * sizeof(struct sdmmc_lli_desc);
  119. desc[i].idmalar |= MMCI_STM32_ULA | MMCI_STM32_ULS
  120. | MMCI_STM32_ABR;
  121. desc[i].idmabase = sg_dma_address(sg);
  122. desc[i].idmasize = sg_dma_len(sg);
  123. }
  124. /* notice the end of link list */
  125. desc[data->sg_len - 1].idmalar &= ~MMCI_STM32_ULA;
  126. dma_wmb();
  127. writel_relaxed(idma->sg_dma, host->base + MMCI_STM32_IDMABAR);
  128. writel_relaxed(desc[0].idmalar, host->base + MMCI_STM32_IDMALAR);
  129. writel_relaxed(desc[0].idmabase, host->base + MMCI_STM32_IDMABASE0R);
  130. writel_relaxed(desc[0].idmasize, host->base + MMCI_STM32_IDMABSIZER);
  131. writel_relaxed(MMCI_STM32_IDMAEN | MMCI_STM32_IDMALLIEN,
  132. host->base + MMCI_STM32_IDMACTRLR);
  133. return 0;
  134. }
  135. static void sdmmc_idma_finalize(struct mmci_host *host, struct mmc_data *data)
  136. {
  137. writel_relaxed(0, host->base + MMCI_STM32_IDMACTRLR);
  138. }
  139. static void mmci_sdmmc_set_clkreg(struct mmci_host *host, unsigned int desired)
  140. {
  141. unsigned int clk = 0, ddr = 0;
  142. if (host->mmc->ios.timing == MMC_TIMING_MMC_DDR52 ||
  143. host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
  144. ddr = MCI_STM32_CLK_DDR;
  145. /*
  146. * cclk = mclk / (2 * clkdiv)
  147. * clkdiv 0 => bypass
  148. * in ddr mode bypass is not possible
  149. */
  150. if (desired) {
  151. if (desired >= host->mclk && !ddr) {
  152. host->cclk = host->mclk;
  153. } else {
  154. clk = DIV_ROUND_UP(host->mclk, 2 * desired);
  155. if (clk > MCI_STM32_CLK_CLKDIV_MSK)
  156. clk = MCI_STM32_CLK_CLKDIV_MSK;
  157. host->cclk = host->mclk / (2 * clk);
  158. }
  159. } else {
  160. /*
  161. * while power-on phase the clock can't be define to 0,
  162. * Only power-off and power-cyc deactivate the clock.
  163. * if desired clock is 0, set max divider
  164. */
  165. clk = MCI_STM32_CLK_CLKDIV_MSK;
  166. host->cclk = host->mclk / (2 * clk);
  167. }
  168. /* Set actual clock for debug */
  169. if (host->mmc->ios.power_mode == MMC_POWER_ON)
  170. host->mmc->actual_clock = host->cclk;
  171. else
  172. host->mmc->actual_clock = 0;
  173. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  174. clk |= MCI_STM32_CLK_WIDEBUS_4;
  175. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  176. clk |= MCI_STM32_CLK_WIDEBUS_8;
  177. clk |= MCI_STM32_CLK_HWFCEN;
  178. clk |= host->clk_reg_add;
  179. clk |= ddr;
  180. /*
  181. * SDMMC_FBCK is selected when an external Delay Block is needed
  182. * with SDR104.
  183. */
  184. if (host->mmc->ios.timing >= MMC_TIMING_UHS_SDR50) {
  185. clk |= MCI_STM32_CLK_BUSSPEED;
  186. if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) {
  187. clk &= ~MCI_STM32_CLK_SEL_MSK;
  188. clk |= MCI_STM32_CLK_SELFBCK;
  189. }
  190. }
  191. mmci_write_clkreg(host, clk);
  192. }
  193. static void mmci_sdmmc_set_pwrreg(struct mmci_host *host, unsigned int pwr)
  194. {
  195. struct mmc_ios ios = host->mmc->ios;
  196. pwr = host->pwr_reg_add;
  197. if (ios.power_mode == MMC_POWER_OFF) {
  198. /* Only a reset could power-off sdmmc */
  199. reset_control_assert(host->rst);
  200. udelay(2);
  201. reset_control_deassert(host->rst);
  202. /*
  203. * Set the SDMMC in Power-cycle state.
  204. * This will make that the SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK
  205. * are driven low, to prevent the Card from being supplied
  206. * through the signal lines.
  207. */
  208. mmci_write_pwrreg(host, MCI_STM32_PWR_CYC | pwr);
  209. } else if (ios.power_mode == MMC_POWER_ON) {
  210. /*
  211. * After power-off (reset): the irq mask defined in probe
  212. * functionis lost
  213. * ault irq mask (probe) must be activated
  214. */
  215. writel(MCI_IRQENABLE | host->variant->start_err,
  216. host->base + MMCIMASK0);
  217. /*
  218. * After a power-cycle state, we must set the SDMMC in
  219. * Power-off. The SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK are
  220. * driven high. Then we can set the SDMMC to Power-on state
  221. */
  222. mmci_write_pwrreg(host, MCI_PWR_OFF | pwr);
  223. mdelay(1);
  224. mmci_write_pwrreg(host, MCI_PWR_ON | pwr);
  225. }
  226. }
  227. static struct mmci_host_ops sdmmc_variant_ops = {
  228. .validate_data = sdmmc_idma_validate_data,
  229. .prep_data = sdmmc_idma_prep_data,
  230. .unprep_data = sdmmc_idma_unprep_data,
  231. .dma_setup = sdmmc_idma_setup,
  232. .dma_start = sdmmc_idma_start,
  233. .dma_finalize = sdmmc_idma_finalize,
  234. .set_clkreg = mmci_sdmmc_set_clkreg,
  235. .set_pwrreg = mmci_sdmmc_set_pwrreg,
  236. };
  237. void sdmmc_variant_init(struct mmci_host *host)
  238. {
  239. host->ops = &sdmmc_variant_ops;
  240. }