1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
6 #include <linux/bitfield.h>
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/iopoll.h>
10 #include <linux/mmc/host.h>
11 #include <linux/mmc/card.h>
12 #include <linux/of_address.h>
13 #include <linux/reset.h>
14 #include <linux/scatterlist.h>
17 #define SDMMC_LLI_BUF_LEN PAGE_SIZE
18 #define SDMMC_IDMA_BURST BIT(MMCI_STM32_IDMABNDT_SHIFT)
21 #define DLYB_CR_DEN BIT(0)
22 #define DLYB_CR_SEN BIT(1)
25 #define DLYB_CFGR_SEL_MASK GENMASK(3, 0)
26 #define DLYB_CFGR_UNIT_MASK GENMASK(14, 8)
27 #define DLYB_CFGR_LNG_MASK GENMASK(27, 16)
28 #define DLYB_CFGR_LNGF BIT(31)
30 #define DLYB_NB_DELAY 11
31 #define DLYB_CFGR_SEL_MAX (DLYB_NB_DELAY + 1)
32 #define DLYB_CFGR_UNIT_MAX 127
34 #define DLYB_LNG_TIMEOUT_US 1000
35 #define SDMMC_VSWEND_TIMEOUT_US 10000
37 struct sdmmc_lli_desc {
46 dma_addr_t bounce_dma_addr;
48 bool use_bounce_buffer;
57 static int sdmmc_idma_validate_data(struct mmci_host *host,
58 struct mmc_data *data)
60 struct sdmmc_idma *idma = host->dma_priv;
61 struct device *dev = mmc_dev(host->mmc);
62 struct scatterlist *sg;
66 * idma has constraints on idmabase & idmasize for each element
67 * excepted the last element which has no constraint on idmasize
69 idma->use_bounce_buffer = false;
70 for_each_sg(data->sg, sg, data->sg_len - 1, i) {
71 if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
72 !IS_ALIGNED(sg->length, SDMMC_IDMA_BURST)) {
73 dev_dbg(mmc_dev(host->mmc),
74 "unaligned scatterlist: ofst:%x length:%d\n",
75 data->sg->offset, data->sg->length);
76 goto use_bounce_buffer;
80 if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
81 dev_dbg(mmc_dev(host->mmc),
82 "unaligned last scatterlist: ofst:%x length:%d\n",
83 data->sg->offset, data->sg->length);
84 goto use_bounce_buffer;
90 if (!idma->bounce_buf) {
91 idma->bounce_buf = dmam_alloc_coherent(dev,
92 host->mmc->max_req_size,
93 &idma->bounce_dma_addr,
95 if (!idma->bounce_buf) {
96 dev_err(dev, "Unable to map allocate DMA bounce buffer.\n");
101 idma->use_bounce_buffer = true;
106 static int _sdmmc_idma_prep_data(struct mmci_host *host,
107 struct mmc_data *data)
109 struct sdmmc_idma *idma = host->dma_priv;
111 if (idma->use_bounce_buffer) {
112 if (data->flags & MMC_DATA_WRITE) {
113 unsigned int xfer_bytes = data->blksz * data->blocks;
115 sg_copy_to_buffer(data->sg, data->sg_len,
116 idma->bounce_buf, xfer_bytes);
122 n_elem = dma_map_sg(mmc_dev(host->mmc),
125 mmc_get_dma_dir(data));
128 dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
135 static int sdmmc_idma_prep_data(struct mmci_host *host,
136 struct mmc_data *data, bool next)
138 /* Check if job is already prepared. */
139 if (!next && data->host_cookie == host->next_cookie)
142 return _sdmmc_idma_prep_data(host, data);
145 static void sdmmc_idma_unprep_data(struct mmci_host *host,
146 struct mmc_data *data, int err)
148 struct sdmmc_idma *idma = host->dma_priv;
150 if (idma->use_bounce_buffer) {
151 if (data->flags & MMC_DATA_READ) {
152 unsigned int xfer_bytes = data->blksz * data->blocks;
154 sg_copy_from_buffer(data->sg, data->sg_len,
155 idma->bounce_buf, xfer_bytes);
158 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
159 mmc_get_dma_dir(data));
163 static int sdmmc_idma_setup(struct mmci_host *host)
165 struct sdmmc_idma *idma;
166 struct device *dev = mmc_dev(host->mmc);
168 idma = devm_kzalloc(dev, sizeof(*idma), GFP_KERNEL);
172 host->dma_priv = idma;
174 if (host->variant->dma_lli) {
175 idma->sg_cpu = dmam_alloc_coherent(dev, SDMMC_LLI_BUF_LEN,
176 &idma->sg_dma, GFP_KERNEL);
178 dev_err(dev, "Failed to alloc IDMA descriptor\n");
181 host->mmc->max_segs = SDMMC_LLI_BUF_LEN /
182 sizeof(struct sdmmc_lli_desc);
183 host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask;
185 host->mmc->max_req_size = SZ_1M;
187 host->mmc->max_segs = 1;
188 host->mmc->max_seg_size = host->mmc->max_req_size;
191 return dma_set_max_seg_size(dev, host->mmc->max_seg_size);
194 static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
197 struct sdmmc_idma *idma = host->dma_priv;
198 struct sdmmc_lli_desc *desc = (struct sdmmc_lli_desc *)idma->sg_cpu;
199 struct mmc_data *data = host->data;
200 struct scatterlist *sg;
203 if (!host->variant->dma_lli || data->sg_len == 1 ||
204 idma->use_bounce_buffer) {
207 if (idma->use_bounce_buffer)
208 dma_addr = idma->bounce_dma_addr;
210 dma_addr = sg_dma_address(data->sg);
212 writel_relaxed(dma_addr,
213 host->base + MMCI_STM32_IDMABASE0R);
214 writel_relaxed(MMCI_STM32_IDMAEN,
215 host->base + MMCI_STM32_IDMACTRLR);
219 for_each_sg(data->sg, sg, data->sg_len, i) {
220 desc[i].idmalar = (i + 1) * sizeof(struct sdmmc_lli_desc);
221 desc[i].idmalar |= MMCI_STM32_ULA | MMCI_STM32_ULS
223 desc[i].idmabase = sg_dma_address(sg);
224 desc[i].idmasize = sg_dma_len(sg);
227 /* notice the end of link list */
228 desc[data->sg_len - 1].idmalar &= ~MMCI_STM32_ULA;
231 writel_relaxed(idma->sg_dma, host->base + MMCI_STM32_IDMABAR);
232 writel_relaxed(desc[0].idmalar, host->base + MMCI_STM32_IDMALAR);
233 writel_relaxed(desc[0].idmabase, host->base + MMCI_STM32_IDMABASE0R);
234 writel_relaxed(desc[0].idmasize, host->base + MMCI_STM32_IDMABSIZER);
235 writel_relaxed(MMCI_STM32_IDMAEN | MMCI_STM32_IDMALLIEN,
236 host->base + MMCI_STM32_IDMACTRLR);
241 static void sdmmc_idma_finalize(struct mmci_host *host, struct mmc_data *data)
243 writel_relaxed(0, host->base + MMCI_STM32_IDMACTRLR);
245 if (!data->host_cookie)
246 sdmmc_idma_unprep_data(host, data, 0);
249 static void mmci_sdmmc_set_clkreg(struct mmci_host *host, unsigned int desired)
251 unsigned int clk = 0, ddr = 0;
253 if (host->mmc->ios.timing == MMC_TIMING_MMC_DDR52 ||
254 host->mmc->ios.timing == MMC_TIMING_UHS_DDR50)
255 ddr = MCI_STM32_CLK_DDR;
258 * cclk = mclk / (2 * clkdiv)
260 * in ddr mode bypass is not possible
263 if (desired >= host->mclk && !ddr) {
264 host->cclk = host->mclk;
266 clk = DIV_ROUND_UP(host->mclk, 2 * desired);
267 if (clk > MCI_STM32_CLK_CLKDIV_MSK)
268 clk = MCI_STM32_CLK_CLKDIV_MSK;
269 host->cclk = host->mclk / (2 * clk);
273 * while power-on phase the clock can't be define to 0,
274 * Only power-off and power-cyc deactivate the clock.
275 * if desired clock is 0, set max divider
277 clk = MCI_STM32_CLK_CLKDIV_MSK;
278 host->cclk = host->mclk / (2 * clk);
281 /* Set actual clock for debug */
282 if (host->mmc->ios.power_mode == MMC_POWER_ON)
283 host->mmc->actual_clock = host->cclk;
285 host->mmc->actual_clock = 0;
287 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
288 clk |= MCI_STM32_CLK_WIDEBUS_4;
289 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
290 clk |= MCI_STM32_CLK_WIDEBUS_8;
292 clk |= MCI_STM32_CLK_HWFCEN;
293 clk |= host->clk_reg_add;
297 * SDMMC_FBCK is selected when an external Delay Block is needed
298 * with SDR104 or HS200.
300 if (host->mmc->ios.timing >= MMC_TIMING_UHS_SDR50) {
301 clk |= MCI_STM32_CLK_BUSSPEED;
302 if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104 ||
303 host->mmc->ios.timing == MMC_TIMING_MMC_HS200) {
304 clk &= ~MCI_STM32_CLK_SEL_MSK;
305 clk |= MCI_STM32_CLK_SELFBCK;
309 mmci_write_clkreg(host, clk);
312 static void sdmmc_dlyb_input_ck(struct sdmmc_dlyb *dlyb)
314 if (!dlyb || !dlyb->base)
317 /* Output clock = Input clock */
318 writel_relaxed(0, dlyb->base + DLYB_CR);
321 static void mmci_sdmmc_set_pwrreg(struct mmci_host *host, unsigned int pwr)
323 struct mmc_ios ios = host->mmc->ios;
324 struct sdmmc_dlyb *dlyb = host->variant_priv;
326 /* adds OF options */
327 pwr = host->pwr_reg_add;
329 sdmmc_dlyb_input_ck(dlyb);
331 if (ios.power_mode == MMC_POWER_OFF) {
332 /* Only a reset could power-off sdmmc */
333 reset_control_assert(host->rst);
335 reset_control_deassert(host->rst);
338 * Set the SDMMC in Power-cycle state.
339 * This will make that the SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK
340 * are driven low, to prevent the Card from being supplied
341 * through the signal lines.
343 mmci_write_pwrreg(host, MCI_STM32_PWR_CYC | pwr);
344 } else if (ios.power_mode == MMC_POWER_ON) {
346 * After power-off (reset): the irq mask defined in probe
348 * ault irq mask (probe) must be activated
350 writel(MCI_IRQENABLE | host->variant->start_err,
351 host->base + MMCIMASK0);
353 /* preserves voltage switch bits */
354 pwr |= host->pwr_reg & (MCI_STM32_VSWITCHEN |
358 * After a power-cycle state, we must set the SDMMC in
359 * Power-off. The SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK are
360 * driven high. Then we can set the SDMMC to Power-on state
362 mmci_write_pwrreg(host, MCI_PWR_OFF | pwr);
364 mmci_write_pwrreg(host, MCI_PWR_ON | pwr);
368 static u32 sdmmc_get_dctrl_cfg(struct mmci_host *host)
372 datactrl = mmci_dctrl_blksz(host);
374 if (host->mmc->card && mmc_card_sdio(host->mmc->card) &&
375 host->data->blocks == 1)
376 datactrl |= MCI_DPSM_STM32_MODE_SDIO;
377 else if (host->data->stop && !host->mrq->sbc)
378 datactrl |= MCI_DPSM_STM32_MODE_BLOCK_STOP;
380 datactrl |= MCI_DPSM_STM32_MODE_BLOCK;
385 static bool sdmmc_busy_complete(struct mmci_host *host, u32 status, u32 err_msk)
387 void __iomem *base = host->base;
388 u32 busy_d0, busy_d0end, mask, sdmmc_status;
390 mask = readl_relaxed(base + MMCIMASK0);
391 sdmmc_status = readl_relaxed(base + MMCISTATUS);
392 busy_d0end = sdmmc_status & MCI_STM32_BUSYD0END;
393 busy_d0 = sdmmc_status & MCI_STM32_BUSYD0;
395 /* complete if there is an error or busy_d0end */
396 if ((status & err_msk) || busy_d0end)
400 * On response the busy signaling is reflected in the BUSYD0 flag.
401 * if busy_d0 is in-progress we must activate busyd0end interrupt
402 * to wait this completion. Else this request has no busy step.
405 if (!host->busy_status) {
406 writel_relaxed(mask | host->variant->busy_detect_mask,
408 host->busy_status = status &
409 (MCI_CMDSENT | MCI_CMDRESPEND);
415 if (host->busy_status) {
416 writel_relaxed(mask & ~host->variant->busy_detect_mask,
418 host->busy_status = 0;
421 writel_relaxed(host->variant->busy_detect_mask, base + MMCICLEAR);
426 static void sdmmc_dlyb_set_cfgr(struct sdmmc_dlyb *dlyb,
427 int unit, int phase, bool sampler)
431 writel_relaxed(DLYB_CR_SEN | DLYB_CR_DEN, dlyb->base + DLYB_CR);
433 cfgr = FIELD_PREP(DLYB_CFGR_UNIT_MASK, unit) |
434 FIELD_PREP(DLYB_CFGR_SEL_MASK, phase);
435 writel_relaxed(cfgr, dlyb->base + DLYB_CFGR);
438 writel_relaxed(DLYB_CR_DEN, dlyb->base + DLYB_CR);
441 static int sdmmc_dlyb_lng_tuning(struct mmci_host *host)
443 struct sdmmc_dlyb *dlyb = host->variant_priv;
447 for (i = 0; i <= DLYB_CFGR_UNIT_MAX; i++) {
448 sdmmc_dlyb_set_cfgr(dlyb, i, DLYB_CFGR_SEL_MAX, true);
450 ret = readl_relaxed_poll_timeout(dlyb->base + DLYB_CFGR, cfgr,
451 (cfgr & DLYB_CFGR_LNGF),
452 1, DLYB_LNG_TIMEOUT_US);
454 dev_warn(mmc_dev(host->mmc),
455 "delay line cfg timeout unit:%d cfgr:%d\n",
460 lng = FIELD_GET(DLYB_CFGR_LNG_MASK, cfgr);
461 if (lng < BIT(DLYB_NB_DELAY) && lng > 0)
465 if (i > DLYB_CFGR_UNIT_MAX)
469 dlyb->max = __fls(lng);
474 static int sdmmc_dlyb_phase_tuning(struct mmci_host *host, u32 opcode)
476 struct sdmmc_dlyb *dlyb = host->variant_priv;
477 int cur_len = 0, max_len = 0, end_of_len = 0;
480 for (phase = 0; phase <= dlyb->max; phase++) {
481 sdmmc_dlyb_set_cfgr(dlyb, dlyb->unit, phase, false);
483 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
487 if (cur_len > max_len) {
495 dev_err(mmc_dev(host->mmc), "no tuning point found\n");
499 writel_relaxed(0, dlyb->base + DLYB_CR);
501 phase = end_of_len - max_len / 2;
502 sdmmc_dlyb_set_cfgr(dlyb, dlyb->unit, phase, false);
504 dev_dbg(mmc_dev(host->mmc), "unit:%d max_dly:%d phase:%d\n",
505 dlyb->unit, dlyb->max, phase);
510 static int sdmmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
512 struct mmci_host *host = mmc_priv(mmc);
513 struct sdmmc_dlyb *dlyb = host->variant_priv;
515 if (!dlyb || !dlyb->base)
518 if (sdmmc_dlyb_lng_tuning(host))
521 return sdmmc_dlyb_phase_tuning(host, opcode);
524 static void sdmmc_pre_sig_volt_vswitch(struct mmci_host *host)
526 /* clear the voltage switch completion flag */
527 writel_relaxed(MCI_STM32_VSWENDC, host->base + MMCICLEAR);
528 /* enable Voltage switch procedure */
529 mmci_write_pwrreg(host, host->pwr_reg | MCI_STM32_VSWITCHEN);
532 static int sdmmc_post_sig_volt_switch(struct mmci_host *host,
539 spin_lock_irqsave(&host->lock, flags);
540 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180 &&
541 host->pwr_reg & MCI_STM32_VSWITCHEN) {
542 mmci_write_pwrreg(host, host->pwr_reg | MCI_STM32_VSWITCH);
543 spin_unlock_irqrestore(&host->lock, flags);
545 /* wait voltage switch completion while 10ms */
546 ret = readl_relaxed_poll_timeout(host->base + MMCISTATUS,
548 (status & MCI_STM32_VSWEND),
549 10, SDMMC_VSWEND_TIMEOUT_US);
551 writel_relaxed(MCI_STM32_VSWENDC | MCI_STM32_CKSTOPC,
552 host->base + MMCICLEAR);
553 spin_lock_irqsave(&host->lock, flags);
554 mmci_write_pwrreg(host, host->pwr_reg &
555 ~(MCI_STM32_VSWITCHEN | MCI_STM32_VSWITCH));
557 spin_unlock_irqrestore(&host->lock, flags);
562 static struct mmci_host_ops sdmmc_variant_ops = {
563 .validate_data = sdmmc_idma_validate_data,
564 .prep_data = sdmmc_idma_prep_data,
565 .unprep_data = sdmmc_idma_unprep_data,
566 .get_datactrl_cfg = sdmmc_get_dctrl_cfg,
567 .dma_setup = sdmmc_idma_setup,
568 .dma_start = sdmmc_idma_start,
569 .dma_finalize = sdmmc_idma_finalize,
570 .set_clkreg = mmci_sdmmc_set_clkreg,
571 .set_pwrreg = mmci_sdmmc_set_pwrreg,
572 .busy_complete = sdmmc_busy_complete,
573 .pre_sig_volt_switch = sdmmc_pre_sig_volt_vswitch,
574 .post_sig_volt_switch = sdmmc_post_sig_volt_switch,
577 void sdmmc_variant_init(struct mmci_host *host)
579 struct device_node *np = host->mmc->parent->of_node;
580 void __iomem *base_dlyb;
581 struct sdmmc_dlyb *dlyb;
583 host->ops = &sdmmc_variant_ops;
584 host->pwr_reg = readl_relaxed(host->base + MMCIPOWER);
586 base_dlyb = devm_of_iomap(mmc_dev(host->mmc), np, 1, NULL);
587 if (IS_ERR(base_dlyb))
590 dlyb = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dlyb), GFP_KERNEL);
594 dlyb->base = base_dlyb;
595 host->variant_priv = dlyb;
596 host->mmc_ops->execute_tuning = sdmmc_execute_tuning;