perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / mmc / host / mmci_stm32_sdmmc.c
blobcfbfc6f1048f563f8891e4888af373ee21ecba81
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"
14 #define SDMMC_LLI_BUF_LEN PAGE_SIZE
15 #define SDMMC_IDMA_BURST BIT(MMCI_STM32_IDMABNDT_SHIFT)
17 struct sdmmc_lli_desc {
18 u32 idmalar;
19 u32 idmabase;
20 u32 idmasize;
23 struct sdmmc_priv {
24 dma_addr_t sg_dma;
25 void *sg_cpu;
28 int sdmmc_idma_validate_data(struct mmci_host *host,
29 struct mmc_data *data)
31 struct scatterlist *sg;
32 int i;
35 * idma has constraints on idmabase & idmasize for each element
36 * excepted the last element which has no constraint on idmasize
38 for_each_sg(data->sg, sg, data->sg_len - 1, i) {
39 if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32)) ||
40 !IS_ALIGNED(sg_dma_len(data->sg), SDMMC_IDMA_BURST)) {
41 dev_err(mmc_dev(host->mmc),
42 "unaligned scatterlist: ofst:%x length:%d\n",
43 data->sg->offset, data->sg->length);
44 return -EINVAL;
48 if (!IS_ALIGNED(sg_dma_address(data->sg), sizeof(u32))) {
49 dev_err(mmc_dev(host->mmc),
50 "unaligned last scatterlist: ofst:%x length:%d\n",
51 data->sg->offset, data->sg->length);
52 return -EINVAL;
55 return 0;
58 static int _sdmmc_idma_prep_data(struct mmci_host *host,
59 struct mmc_data *data)
61 int n_elem;
63 n_elem = dma_map_sg(mmc_dev(host->mmc),
64 data->sg,
65 data->sg_len,
66 mmc_get_dma_dir(data));
68 if (!n_elem) {
69 dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
70 return -EINVAL;
73 return 0;
76 static int sdmmc_idma_prep_data(struct mmci_host *host,
77 struct mmc_data *data, bool next)
79 /* Check if job is already prepared. */
80 if (!next && data->host_cookie == host->next_cookie)
81 return 0;
83 return _sdmmc_idma_prep_data(host, data);
86 static void sdmmc_idma_unprep_data(struct mmci_host *host,
87 struct mmc_data *data, int err)
89 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
90 mmc_get_dma_dir(data));
93 static int sdmmc_idma_setup(struct mmci_host *host)
95 struct sdmmc_priv *idma;
97 idma = devm_kzalloc(mmc_dev(host->mmc), sizeof(*idma), GFP_KERNEL);
98 if (!idma)
99 return -ENOMEM;
101 host->dma_priv = idma;
103 if (host->variant->dma_lli) {
104 idma->sg_cpu = dmam_alloc_coherent(mmc_dev(host->mmc),
105 SDMMC_LLI_BUF_LEN,
106 &idma->sg_dma, GFP_KERNEL);
107 if (!idma->sg_cpu) {
108 dev_err(mmc_dev(host->mmc),
109 "Failed to alloc IDMA descriptor\n");
110 return -ENOMEM;
112 host->mmc->max_segs = SDMMC_LLI_BUF_LEN /
113 sizeof(struct sdmmc_lli_desc);
114 host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask;
115 } else {
116 host->mmc->max_segs = 1;
117 host->mmc->max_seg_size = host->mmc->max_req_size;
120 return 0;
123 static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
126 struct sdmmc_priv *idma = host->dma_priv;
127 struct sdmmc_lli_desc *desc = (struct sdmmc_lli_desc *)idma->sg_cpu;
128 struct mmc_data *data = host->data;
129 struct scatterlist *sg;
130 int i;
132 if (!host->variant->dma_lli || data->sg_len == 1) {
133 writel_relaxed(sg_dma_address(data->sg),
134 host->base + MMCI_STM32_IDMABASE0R);
135 writel_relaxed(MMCI_STM32_IDMAEN,
136 host->base + MMCI_STM32_IDMACTRLR);
137 return 0;
140 for_each_sg(data->sg, sg, data->sg_len, i) {
141 desc[i].idmalar = (i + 1) * sizeof(struct sdmmc_lli_desc);
142 desc[i].idmalar |= MMCI_STM32_ULA | MMCI_STM32_ULS
143 | MMCI_STM32_ABR;
144 desc[i].idmabase = sg_dma_address(sg);
145 desc[i].idmasize = sg_dma_len(sg);
148 /* notice the end of link list */
149 desc[data->sg_len - 1].idmalar &= ~MMCI_STM32_ULA;
151 dma_wmb();
152 writel_relaxed(idma->sg_dma, host->base + MMCI_STM32_IDMABAR);
153 writel_relaxed(desc[0].idmalar, host->base + MMCI_STM32_IDMALAR);
154 writel_relaxed(desc[0].idmabase, host->base + MMCI_STM32_IDMABASE0R);
155 writel_relaxed(desc[0].idmasize, host->base + MMCI_STM32_IDMABSIZER);
156 writel_relaxed(MMCI_STM32_IDMAEN | MMCI_STM32_IDMALLIEN,
157 host->base + MMCI_STM32_IDMACTRLR);
159 return 0;
162 static void sdmmc_idma_finalize(struct mmci_host *host, struct mmc_data *data)
164 writel_relaxed(0, host->base + MMCI_STM32_IDMACTRLR);
167 static void mmci_sdmmc_set_clkreg(struct mmci_host *host, unsigned int desired)
169 unsigned int clk = 0, ddr = 0;
171 if (host->mmc->ios.timing == MMC_TIMING_MMC_DDR52 ||
172 host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
173 ddr = MCI_STM32_CLK_DDR;
176 * cclk = mclk / (2 * clkdiv)
177 * clkdiv 0 => bypass
178 * in ddr mode bypass is not possible
180 if (desired) {
181 if (desired >= host->mclk && !ddr) {
182 host->cclk = host->mclk;
183 } else {
184 clk = DIV_ROUND_UP(host->mclk, 2 * desired);
185 if (clk > MCI_STM32_CLK_CLKDIV_MSK)
186 clk = MCI_STM32_CLK_CLKDIV_MSK;
187 host->cclk = host->mclk / (2 * clk);
189 } else {
191 * while power-on phase the clock can't be define to 0,
192 * Only power-off and power-cyc deactivate the clock.
193 * if desired clock is 0, set max divider
195 clk = MCI_STM32_CLK_CLKDIV_MSK;
196 host->cclk = host->mclk / (2 * clk);
199 /* Set actual clock for debug */
200 if (host->mmc->ios.power_mode == MMC_POWER_ON)
201 host->mmc->actual_clock = host->cclk;
202 else
203 host->mmc->actual_clock = 0;
205 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
206 clk |= MCI_STM32_CLK_WIDEBUS_4;
207 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
208 clk |= MCI_STM32_CLK_WIDEBUS_8;
210 clk |= MCI_STM32_CLK_HWFCEN;
211 clk |= host->clk_reg_add;
212 clk |= ddr;
215 * SDMMC_FBCK is selected when an external Delay Block is needed
216 * with SDR104.
218 if (host->mmc->ios.timing >= MMC_TIMING_UHS_SDR50) {
219 clk |= MCI_STM32_CLK_BUSSPEED;
220 if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) {
221 clk &= ~MCI_STM32_CLK_SEL_MSK;
222 clk |= MCI_STM32_CLK_SELFBCK;
226 mmci_write_clkreg(host, clk);
229 static void mmci_sdmmc_set_pwrreg(struct mmci_host *host, unsigned int pwr)
231 struct mmc_ios ios = host->mmc->ios;
233 pwr = host->pwr_reg_add;
235 if (ios.power_mode == MMC_POWER_OFF) {
236 /* Only a reset could power-off sdmmc */
237 reset_control_assert(host->rst);
238 udelay(2);
239 reset_control_deassert(host->rst);
242 * Set the SDMMC in Power-cycle state.
243 * This will make that the SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK
244 * are driven low, to prevent the Card from being supplied
245 * through the signal lines.
247 mmci_write_pwrreg(host, MCI_STM32_PWR_CYC | pwr);
248 } else if (ios.power_mode == MMC_POWER_ON) {
250 * After power-off (reset): the irq mask defined in probe
251 * functionis lost
252 * ault irq mask (probe) must be activated
254 writel(MCI_IRQENABLE | host->variant->start_err,
255 host->base + MMCIMASK0);
258 * After a power-cycle state, we must set the SDMMC in
259 * Power-off. The SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK are
260 * driven high. Then we can set the SDMMC to Power-on state
262 mmci_write_pwrreg(host, MCI_PWR_OFF | pwr);
263 mdelay(1);
264 mmci_write_pwrreg(host, MCI_PWR_ON | pwr);
268 static struct mmci_host_ops sdmmc_variant_ops = {
269 .validate_data = sdmmc_idma_validate_data,
270 .prep_data = sdmmc_idma_prep_data,
271 .unprep_data = sdmmc_idma_unprep_data,
272 .dma_setup = sdmmc_idma_setup,
273 .dma_start = sdmmc_idma_start,
274 .dma_finalize = sdmmc_idma_finalize,
275 .set_clkreg = mmci_sdmmc_set_clkreg,
276 .set_pwrreg = mmci_sdmmc_set_pwrreg,
279 void sdmmc_variant_init(struct mmci_host *host)
281 host->ops = &sdmmc_variant_ops;