146b723ddSLudovic Barre // SPDX-License-Identifier: GPL-2.0 246b723ddSLudovic Barre /* 346b723ddSLudovic Barre * Copyright (C) STMicroelectronics 2018 - All Rights Reserved 446b723ddSLudovic Barre * Author: Ludovic.barre@st.com for STMicroelectronics. 546b723ddSLudovic Barre */ 61103f807SLudovic Barre #include <linux/bitfield.h> 746b723ddSLudovic Barre #include <linux/delay.h> 846b723ddSLudovic Barre #include <linux/dma-mapping.h> 91103f807SLudovic Barre #include <linux/iopoll.h> 1046b723ddSLudovic Barre #include <linux/mmc/host.h> 1146b723ddSLudovic Barre #include <linux/mmc/card.h> 121103f807SLudovic Barre #include <linux/of_address.h> 1346b723ddSLudovic Barre #include <linux/reset.h> 1446b723ddSLudovic Barre #include <linux/scatterlist.h> 1546b723ddSLudovic Barre #include "mmci.h" 1646b723ddSLudovic Barre 1746b723ddSLudovic Barre #define SDMMC_LLI_BUF_LEN PAGE_SIZE 1846b723ddSLudovic Barre #define SDMMC_IDMA_BURST BIT(MMCI_STM32_IDMABNDT_SHIFT) 1946b723ddSLudovic Barre 201103f807SLudovic Barre #define DLYB_CR 0x0 211103f807SLudovic Barre #define DLYB_CR_DEN BIT(0) 221103f807SLudovic Barre #define DLYB_CR_SEN BIT(1) 231103f807SLudovic Barre 241103f807SLudovic Barre #define DLYB_CFGR 0x4 251103f807SLudovic Barre #define DLYB_CFGR_SEL_MASK GENMASK(3, 0) 261103f807SLudovic Barre #define DLYB_CFGR_UNIT_MASK GENMASK(14, 8) 271103f807SLudovic Barre #define DLYB_CFGR_LNG_MASK GENMASK(27, 16) 281103f807SLudovic Barre #define DLYB_CFGR_LNGF BIT(31) 291103f807SLudovic Barre 301103f807SLudovic Barre #define DLYB_NB_DELAY 11 311103f807SLudovic Barre #define DLYB_CFGR_SEL_MAX (DLYB_NB_DELAY + 1) 321103f807SLudovic Barre #define DLYB_CFGR_UNIT_MAX 127 331103f807SLudovic Barre 341103f807SLudovic Barre #define DLYB_LNG_TIMEOUT_US 1000 351103f807SLudovic Barre 3646b723ddSLudovic Barre struct sdmmc_lli_desc { 3746b723ddSLudovic Barre u32 idmalar; 3846b723ddSLudovic Barre u32 idmabase; 3946b723ddSLudovic Barre u32 idmasize; 4046b723ddSLudovic Barre }; 4146b723ddSLudovic Barre 42bdbf9fafSLudovic Barre struct sdmmc_idma { 4346b723ddSLudovic Barre dma_addr_t sg_dma; 4446b723ddSLudovic Barre void *sg_cpu; 4546b723ddSLudovic Barre }; 4646b723ddSLudovic Barre 471103f807SLudovic Barre struct sdmmc_dlyb { 481103f807SLudovic Barre void __iomem *base; 491103f807SLudovic Barre u32 unit; 501103f807SLudovic Barre u32 max; 511103f807SLudovic Barre }; 521103f807SLudovic Barre 5361a14e52SBen Dooks static int sdmmc_idma_validate_data(struct mmci_host *host, 5446b723ddSLudovic Barre struct mmc_data *data) 5546b723ddSLudovic Barre { 5646b723ddSLudovic Barre struct scatterlist *sg; 5746b723ddSLudovic Barre int i; 5846b723ddSLudovic Barre 5946b723ddSLudovic Barre /* 6046b723ddSLudovic Barre * idma has constraints on idmabase & idmasize for each element 6146b723ddSLudovic Barre * excepted the last element which has no constraint on idmasize 6246b723ddSLudovic Barre */ 6346b723ddSLudovic Barre for_each_sg(data->sg, sg, data->sg_len - 1, i) { 64127e6e98SLudovic Barre if (!IS_ALIGNED(data->sg->offset, sizeof(u32)) || 65127e6e98SLudovic Barre !IS_ALIGNED(data->sg->length, SDMMC_IDMA_BURST)) { 6646b723ddSLudovic Barre dev_err(mmc_dev(host->mmc), 6746b723ddSLudovic Barre "unaligned scatterlist: ofst:%x length:%d\n", 6846b723ddSLudovic Barre data->sg->offset, data->sg->length); 6946b723ddSLudovic Barre return -EINVAL; 7046b723ddSLudovic Barre } 7146b723ddSLudovic Barre } 7246b723ddSLudovic Barre 73127e6e98SLudovic Barre if (!IS_ALIGNED(data->sg->offset, sizeof(u32))) { 7446b723ddSLudovic Barre dev_err(mmc_dev(host->mmc), 7546b723ddSLudovic Barre "unaligned last scatterlist: ofst:%x length:%d\n", 7646b723ddSLudovic Barre data->sg->offset, data->sg->length); 7746b723ddSLudovic Barre return -EINVAL; 7846b723ddSLudovic Barre } 7946b723ddSLudovic Barre 8046b723ddSLudovic Barre return 0; 8146b723ddSLudovic Barre } 8246b723ddSLudovic Barre 8346b723ddSLudovic Barre static int _sdmmc_idma_prep_data(struct mmci_host *host, 8446b723ddSLudovic Barre struct mmc_data *data) 8546b723ddSLudovic Barre { 8646b723ddSLudovic Barre int n_elem; 8746b723ddSLudovic Barre 8846b723ddSLudovic Barre n_elem = dma_map_sg(mmc_dev(host->mmc), 8946b723ddSLudovic Barre data->sg, 9046b723ddSLudovic Barre data->sg_len, 9146b723ddSLudovic Barre mmc_get_dma_dir(data)); 9246b723ddSLudovic Barre 9346b723ddSLudovic Barre if (!n_elem) { 9446b723ddSLudovic Barre dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n"); 9546b723ddSLudovic Barre return -EINVAL; 9646b723ddSLudovic Barre } 9746b723ddSLudovic Barre 9846b723ddSLudovic Barre return 0; 9946b723ddSLudovic Barre } 10046b723ddSLudovic Barre 10146b723ddSLudovic Barre static int sdmmc_idma_prep_data(struct mmci_host *host, 10246b723ddSLudovic Barre struct mmc_data *data, bool next) 10346b723ddSLudovic Barre { 10446b723ddSLudovic Barre /* Check if job is already prepared. */ 10546b723ddSLudovic Barre if (!next && data->host_cookie == host->next_cookie) 10646b723ddSLudovic Barre return 0; 10746b723ddSLudovic Barre 10846b723ddSLudovic Barre return _sdmmc_idma_prep_data(host, data); 10946b723ddSLudovic Barre } 11046b723ddSLudovic Barre 11146b723ddSLudovic Barre static void sdmmc_idma_unprep_data(struct mmci_host *host, 11246b723ddSLudovic Barre struct mmc_data *data, int err) 11346b723ddSLudovic Barre { 11446b723ddSLudovic Barre dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, 11546b723ddSLudovic Barre mmc_get_dma_dir(data)); 11646b723ddSLudovic Barre } 11746b723ddSLudovic Barre 11846b723ddSLudovic Barre static int sdmmc_idma_setup(struct mmci_host *host) 11946b723ddSLudovic Barre { 120bdbf9fafSLudovic Barre struct sdmmc_idma *idma; 12146b723ddSLudovic Barre 12246b723ddSLudovic Barre idma = devm_kzalloc(mmc_dev(host->mmc), sizeof(*idma), GFP_KERNEL); 12346b723ddSLudovic Barre if (!idma) 12446b723ddSLudovic Barre return -ENOMEM; 12546b723ddSLudovic Barre 12646b723ddSLudovic Barre host->dma_priv = idma; 12746b723ddSLudovic Barre 12846b723ddSLudovic Barre if (host->variant->dma_lli) { 12946b723ddSLudovic Barre idma->sg_cpu = dmam_alloc_coherent(mmc_dev(host->mmc), 13046b723ddSLudovic Barre SDMMC_LLI_BUF_LEN, 13146b723ddSLudovic Barre &idma->sg_dma, GFP_KERNEL); 13246b723ddSLudovic Barre if (!idma->sg_cpu) { 13346b723ddSLudovic Barre dev_err(mmc_dev(host->mmc), 13446b723ddSLudovic Barre "Failed to alloc IDMA descriptor\n"); 13546b723ddSLudovic Barre return -ENOMEM; 13646b723ddSLudovic Barre } 13746b723ddSLudovic Barre host->mmc->max_segs = SDMMC_LLI_BUF_LEN / 13846b723ddSLudovic Barre sizeof(struct sdmmc_lli_desc); 13946b723ddSLudovic Barre host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask; 14046b723ddSLudovic Barre } else { 14146b723ddSLudovic Barre host->mmc->max_segs = 1; 14246b723ddSLudovic Barre host->mmc->max_seg_size = host->mmc->max_req_size; 14346b723ddSLudovic Barre } 14446b723ddSLudovic Barre 14546b723ddSLudovic Barre return 0; 14646b723ddSLudovic Barre } 14746b723ddSLudovic Barre 14846b723ddSLudovic Barre static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl) 14946b723ddSLudovic Barre 15046b723ddSLudovic Barre { 151bdbf9fafSLudovic Barre struct sdmmc_idma *idma = host->dma_priv; 15246b723ddSLudovic Barre struct sdmmc_lli_desc *desc = (struct sdmmc_lli_desc *)idma->sg_cpu; 15346b723ddSLudovic Barre struct mmc_data *data = host->data; 15446b723ddSLudovic Barre struct scatterlist *sg; 15546b723ddSLudovic Barre int i; 15646b723ddSLudovic Barre 15746b723ddSLudovic Barre if (!host->variant->dma_lli || data->sg_len == 1) { 15846b723ddSLudovic Barre writel_relaxed(sg_dma_address(data->sg), 15946b723ddSLudovic Barre host->base + MMCI_STM32_IDMABASE0R); 16046b723ddSLudovic Barre writel_relaxed(MMCI_STM32_IDMAEN, 16146b723ddSLudovic Barre host->base + MMCI_STM32_IDMACTRLR); 16246b723ddSLudovic Barre return 0; 16346b723ddSLudovic Barre } 16446b723ddSLudovic Barre 16546b723ddSLudovic Barre for_each_sg(data->sg, sg, data->sg_len, i) { 16646b723ddSLudovic Barre desc[i].idmalar = (i + 1) * sizeof(struct sdmmc_lli_desc); 16746b723ddSLudovic Barre desc[i].idmalar |= MMCI_STM32_ULA | MMCI_STM32_ULS 16846b723ddSLudovic Barre | MMCI_STM32_ABR; 16946b723ddSLudovic Barre desc[i].idmabase = sg_dma_address(sg); 17046b723ddSLudovic Barre desc[i].idmasize = sg_dma_len(sg); 17146b723ddSLudovic Barre } 17246b723ddSLudovic Barre 17346b723ddSLudovic Barre /* notice the end of link list */ 17446b723ddSLudovic Barre desc[data->sg_len - 1].idmalar &= ~MMCI_STM32_ULA; 17546b723ddSLudovic Barre 17646b723ddSLudovic Barre dma_wmb(); 17746b723ddSLudovic Barre writel_relaxed(idma->sg_dma, host->base + MMCI_STM32_IDMABAR); 17846b723ddSLudovic Barre writel_relaxed(desc[0].idmalar, host->base + MMCI_STM32_IDMALAR); 17946b723ddSLudovic Barre writel_relaxed(desc[0].idmabase, host->base + MMCI_STM32_IDMABASE0R); 18046b723ddSLudovic Barre writel_relaxed(desc[0].idmasize, host->base + MMCI_STM32_IDMABSIZER); 18146b723ddSLudovic Barre writel_relaxed(MMCI_STM32_IDMAEN | MMCI_STM32_IDMALLIEN, 18246b723ddSLudovic Barre host->base + MMCI_STM32_IDMACTRLR); 18346b723ddSLudovic Barre 18446b723ddSLudovic Barre return 0; 18546b723ddSLudovic Barre } 18646b723ddSLudovic Barre 18746b723ddSLudovic Barre static void sdmmc_idma_finalize(struct mmci_host *host, struct mmc_data *data) 18846b723ddSLudovic Barre { 18946b723ddSLudovic Barre writel_relaxed(0, host->base + MMCI_STM32_IDMACTRLR); 19046b723ddSLudovic Barre } 19146b723ddSLudovic Barre 19246b723ddSLudovic Barre static void mmci_sdmmc_set_clkreg(struct mmci_host *host, unsigned int desired) 19346b723ddSLudovic Barre { 19446b723ddSLudovic Barre unsigned int clk = 0, ddr = 0; 19546b723ddSLudovic Barre 19646b723ddSLudovic Barre if (host->mmc->ios.timing == MMC_TIMING_MMC_DDR52 || 19746b723ddSLudovic Barre host->mmc->ios.timing == MMC_TIMING_UHS_DDR50) 19846b723ddSLudovic Barre ddr = MCI_STM32_CLK_DDR; 19946b723ddSLudovic Barre 20046b723ddSLudovic Barre /* 20146b723ddSLudovic Barre * cclk = mclk / (2 * clkdiv) 20246b723ddSLudovic Barre * clkdiv 0 => bypass 20346b723ddSLudovic Barre * in ddr mode bypass is not possible 20446b723ddSLudovic Barre */ 20546b723ddSLudovic Barre if (desired) { 20646b723ddSLudovic Barre if (desired >= host->mclk && !ddr) { 20746b723ddSLudovic Barre host->cclk = host->mclk; 20846b723ddSLudovic Barre } else { 20946b723ddSLudovic Barre clk = DIV_ROUND_UP(host->mclk, 2 * desired); 21046b723ddSLudovic Barre if (clk > MCI_STM32_CLK_CLKDIV_MSK) 21146b723ddSLudovic Barre clk = MCI_STM32_CLK_CLKDIV_MSK; 21246b723ddSLudovic Barre host->cclk = host->mclk / (2 * clk); 21346b723ddSLudovic Barre } 21446b723ddSLudovic Barre } else { 21546b723ddSLudovic Barre /* 21646b723ddSLudovic Barre * while power-on phase the clock can't be define to 0, 21746b723ddSLudovic Barre * Only power-off and power-cyc deactivate the clock. 21846b723ddSLudovic Barre * if desired clock is 0, set max divider 21946b723ddSLudovic Barre */ 22046b723ddSLudovic Barre clk = MCI_STM32_CLK_CLKDIV_MSK; 22146b723ddSLudovic Barre host->cclk = host->mclk / (2 * clk); 22246b723ddSLudovic Barre } 22346b723ddSLudovic Barre 22446b723ddSLudovic Barre /* Set actual clock for debug */ 22546b723ddSLudovic Barre if (host->mmc->ios.power_mode == MMC_POWER_ON) 22646b723ddSLudovic Barre host->mmc->actual_clock = host->cclk; 22746b723ddSLudovic Barre else 22846b723ddSLudovic Barre host->mmc->actual_clock = 0; 22946b723ddSLudovic Barre 23046b723ddSLudovic Barre if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) 23146b723ddSLudovic Barre clk |= MCI_STM32_CLK_WIDEBUS_4; 23246b723ddSLudovic Barre if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) 23346b723ddSLudovic Barre clk |= MCI_STM32_CLK_WIDEBUS_8; 23446b723ddSLudovic Barre 23546b723ddSLudovic Barre clk |= MCI_STM32_CLK_HWFCEN; 23646b723ddSLudovic Barre clk |= host->clk_reg_add; 23746b723ddSLudovic Barre clk |= ddr; 23846b723ddSLudovic Barre 23946b723ddSLudovic Barre /* 24046b723ddSLudovic Barre * SDMMC_FBCK is selected when an external Delay Block is needed 24146b723ddSLudovic Barre * with SDR104. 24246b723ddSLudovic Barre */ 24346b723ddSLudovic Barre if (host->mmc->ios.timing >= MMC_TIMING_UHS_SDR50) { 24446b723ddSLudovic Barre clk |= MCI_STM32_CLK_BUSSPEED; 24546b723ddSLudovic Barre if (host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) { 24646b723ddSLudovic Barre clk &= ~MCI_STM32_CLK_SEL_MSK; 24746b723ddSLudovic Barre clk |= MCI_STM32_CLK_SELFBCK; 24846b723ddSLudovic Barre } 24946b723ddSLudovic Barre } 25046b723ddSLudovic Barre 25146b723ddSLudovic Barre mmci_write_clkreg(host, clk); 25246b723ddSLudovic Barre } 25346b723ddSLudovic Barre 2541103f807SLudovic Barre static void sdmmc_dlyb_input_ck(struct sdmmc_dlyb *dlyb) 2551103f807SLudovic Barre { 2561103f807SLudovic Barre if (!dlyb || !dlyb->base) 2571103f807SLudovic Barre return; 2581103f807SLudovic Barre 2591103f807SLudovic Barre /* Output clock = Input clock */ 2601103f807SLudovic Barre writel_relaxed(0, dlyb->base + DLYB_CR); 2611103f807SLudovic Barre } 2621103f807SLudovic Barre 26346b723ddSLudovic Barre static void mmci_sdmmc_set_pwrreg(struct mmci_host *host, unsigned int pwr) 26446b723ddSLudovic Barre { 26546b723ddSLudovic Barre struct mmc_ios ios = host->mmc->ios; 2661103f807SLudovic Barre struct sdmmc_dlyb *dlyb = host->variant_priv; 26746b723ddSLudovic Barre 26846b723ddSLudovic Barre pwr = host->pwr_reg_add; 26946b723ddSLudovic Barre 2701103f807SLudovic Barre sdmmc_dlyb_input_ck(dlyb); 2711103f807SLudovic Barre 27246b723ddSLudovic Barre if (ios.power_mode == MMC_POWER_OFF) { 27346b723ddSLudovic Barre /* Only a reset could power-off sdmmc */ 27446b723ddSLudovic Barre reset_control_assert(host->rst); 27546b723ddSLudovic Barre udelay(2); 27646b723ddSLudovic Barre reset_control_deassert(host->rst); 27746b723ddSLudovic Barre 27846b723ddSLudovic Barre /* 27946b723ddSLudovic Barre * Set the SDMMC in Power-cycle state. 28046b723ddSLudovic Barre * This will make that the SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK 28146b723ddSLudovic Barre * are driven low, to prevent the Card from being supplied 28246b723ddSLudovic Barre * through the signal lines. 28346b723ddSLudovic Barre */ 28446b723ddSLudovic Barre mmci_write_pwrreg(host, MCI_STM32_PWR_CYC | pwr); 28546b723ddSLudovic Barre } else if (ios.power_mode == MMC_POWER_ON) { 28646b723ddSLudovic Barre /* 28746b723ddSLudovic Barre * After power-off (reset): the irq mask defined in probe 28846b723ddSLudovic Barre * functionis lost 28946b723ddSLudovic Barre * ault irq mask (probe) must be activated 29046b723ddSLudovic Barre */ 29146b723ddSLudovic Barre writel(MCI_IRQENABLE | host->variant->start_err, 29246b723ddSLudovic Barre host->base + MMCIMASK0); 29346b723ddSLudovic Barre 29446b723ddSLudovic Barre /* 29546b723ddSLudovic Barre * After a power-cycle state, we must set the SDMMC in 29646b723ddSLudovic Barre * Power-off. The SDMMC_D[7:0], SDMMC_CMD and SDMMC_CK are 29746b723ddSLudovic Barre * driven high. Then we can set the SDMMC to Power-on state 29846b723ddSLudovic Barre */ 29946b723ddSLudovic Barre mmci_write_pwrreg(host, MCI_PWR_OFF | pwr); 30046b723ddSLudovic Barre mdelay(1); 30146b723ddSLudovic Barre mmci_write_pwrreg(host, MCI_PWR_ON | pwr); 30246b723ddSLudovic Barre } 30346b723ddSLudovic Barre } 30446b723ddSLudovic Barre 3058372f9d0SLudovic Barre static u32 sdmmc_get_dctrl_cfg(struct mmci_host *host) 3068372f9d0SLudovic Barre { 3078372f9d0SLudovic Barre u32 datactrl; 3088372f9d0SLudovic Barre 3098372f9d0SLudovic Barre datactrl = mmci_dctrl_blksz(host); 3108372f9d0SLudovic Barre 3118372f9d0SLudovic Barre if (host->mmc->card && mmc_card_sdio(host->mmc->card) && 3128372f9d0SLudovic Barre host->data->blocks == 1) 3138372f9d0SLudovic Barre datactrl |= MCI_DPSM_STM32_MODE_SDIO; 3148372f9d0SLudovic Barre else if (host->data->stop && !host->mrq->sbc) 3158372f9d0SLudovic Barre datactrl |= MCI_DPSM_STM32_MODE_BLOCK_STOP; 3168372f9d0SLudovic Barre else 3178372f9d0SLudovic Barre datactrl |= MCI_DPSM_STM32_MODE_BLOCK; 3188372f9d0SLudovic Barre 3198372f9d0SLudovic Barre return datactrl; 3208372f9d0SLudovic Barre } 3218372f9d0SLudovic Barre 3220e68de6aSLudovic Barre static bool sdmmc_busy_complete(struct mmci_host *host, u32 status, u32 err_msk) 3230e68de6aSLudovic Barre { 3240e68de6aSLudovic Barre void __iomem *base = host->base; 3250e68de6aSLudovic Barre u32 busy_d0, busy_d0end, mask, sdmmc_status; 3260e68de6aSLudovic Barre 3270e68de6aSLudovic Barre mask = readl_relaxed(base + MMCIMASK0); 3280e68de6aSLudovic Barre sdmmc_status = readl_relaxed(base + MMCISTATUS); 3290e68de6aSLudovic Barre busy_d0end = sdmmc_status & MCI_STM32_BUSYD0END; 3300e68de6aSLudovic Barre busy_d0 = sdmmc_status & MCI_STM32_BUSYD0; 3310e68de6aSLudovic Barre 3320e68de6aSLudovic Barre /* complete if there is an error or busy_d0end */ 3330e68de6aSLudovic Barre if ((status & err_msk) || busy_d0end) 3340e68de6aSLudovic Barre goto complete; 3350e68de6aSLudovic Barre 3360e68de6aSLudovic Barre /* 3370e68de6aSLudovic Barre * On response the busy signaling is reflected in the BUSYD0 flag. 3380e68de6aSLudovic Barre * if busy_d0 is in-progress we must activate busyd0end interrupt 3390e68de6aSLudovic Barre * to wait this completion. Else this request has no busy step. 3400e68de6aSLudovic Barre */ 3410e68de6aSLudovic Barre if (busy_d0) { 3420e68de6aSLudovic Barre if (!host->busy_status) { 3430e68de6aSLudovic Barre writel_relaxed(mask | host->variant->busy_detect_mask, 3440e68de6aSLudovic Barre base + MMCIMASK0); 3450e68de6aSLudovic Barre host->busy_status = status & 3460e68de6aSLudovic Barre (MCI_CMDSENT | MCI_CMDRESPEND); 3470e68de6aSLudovic Barre } 3480e68de6aSLudovic Barre return false; 3490e68de6aSLudovic Barre } 3500e68de6aSLudovic Barre 3510e68de6aSLudovic Barre complete: 3520e68de6aSLudovic Barre if (host->busy_status) { 3530e68de6aSLudovic Barre writel_relaxed(mask & ~host->variant->busy_detect_mask, 3540e68de6aSLudovic Barre base + MMCIMASK0); 3550e68de6aSLudovic Barre writel_relaxed(host->variant->busy_detect_mask, 3560e68de6aSLudovic Barre base + MMCICLEAR); 3570e68de6aSLudovic Barre host->busy_status = 0; 3580e68de6aSLudovic Barre } 3590e68de6aSLudovic Barre 3600e68de6aSLudovic Barre return true; 3610e68de6aSLudovic Barre } 3620e68de6aSLudovic Barre 3631103f807SLudovic Barre static void sdmmc_dlyb_set_cfgr(struct sdmmc_dlyb *dlyb, 3641103f807SLudovic Barre int unit, int phase, bool sampler) 3651103f807SLudovic Barre { 3661103f807SLudovic Barre u32 cfgr; 3671103f807SLudovic Barre 3681103f807SLudovic Barre writel_relaxed(DLYB_CR_SEN | DLYB_CR_DEN, dlyb->base + DLYB_CR); 3691103f807SLudovic Barre 3701103f807SLudovic Barre cfgr = FIELD_PREP(DLYB_CFGR_UNIT_MASK, unit) | 3711103f807SLudovic Barre FIELD_PREP(DLYB_CFGR_SEL_MASK, phase); 3721103f807SLudovic Barre writel_relaxed(cfgr, dlyb->base + DLYB_CFGR); 3731103f807SLudovic Barre 3741103f807SLudovic Barre if (!sampler) 3751103f807SLudovic Barre writel_relaxed(DLYB_CR_DEN, dlyb->base + DLYB_CR); 3761103f807SLudovic Barre } 3771103f807SLudovic Barre 3781103f807SLudovic Barre static int sdmmc_dlyb_lng_tuning(struct mmci_host *host) 3791103f807SLudovic Barre { 3801103f807SLudovic Barre struct sdmmc_dlyb *dlyb = host->variant_priv; 3811103f807SLudovic Barre u32 cfgr; 3821103f807SLudovic Barre int i, lng, ret; 3831103f807SLudovic Barre 3841103f807SLudovic Barre for (i = 0; i <= DLYB_CFGR_UNIT_MAX; i++) { 3851103f807SLudovic Barre sdmmc_dlyb_set_cfgr(dlyb, i, DLYB_CFGR_SEL_MAX, true); 3861103f807SLudovic Barre 3871103f807SLudovic Barre ret = readl_relaxed_poll_timeout(dlyb->base + DLYB_CFGR, cfgr, 3881103f807SLudovic Barre (cfgr & DLYB_CFGR_LNGF), 3891103f807SLudovic Barre 1, DLYB_LNG_TIMEOUT_US); 3901103f807SLudovic Barre if (ret) { 3911103f807SLudovic Barre dev_warn(mmc_dev(host->mmc), 3921103f807SLudovic Barre "delay line cfg timeout unit:%d cfgr:%d\n", 3931103f807SLudovic Barre i, cfgr); 3941103f807SLudovic Barre continue; 3951103f807SLudovic Barre } 3961103f807SLudovic Barre 3971103f807SLudovic Barre lng = FIELD_GET(DLYB_CFGR_LNG_MASK, cfgr); 3981103f807SLudovic Barre if (lng < BIT(DLYB_NB_DELAY) && lng > 0) 3991103f807SLudovic Barre break; 4001103f807SLudovic Barre } 4011103f807SLudovic Barre 4021103f807SLudovic Barre if (i > DLYB_CFGR_UNIT_MAX) 4031103f807SLudovic Barre return -EINVAL; 4041103f807SLudovic Barre 4051103f807SLudovic Barre dlyb->unit = i; 4061103f807SLudovic Barre dlyb->max = __fls(lng); 4071103f807SLudovic Barre 4081103f807SLudovic Barre return 0; 4091103f807SLudovic Barre } 4101103f807SLudovic Barre 4111103f807SLudovic Barre static int sdmmc_dlyb_phase_tuning(struct mmci_host *host, u32 opcode) 4121103f807SLudovic Barre { 4131103f807SLudovic Barre struct sdmmc_dlyb *dlyb = host->variant_priv; 4141103f807SLudovic Barre int cur_len = 0, max_len = 0, end_of_len = 0; 4151103f807SLudovic Barre int phase; 4161103f807SLudovic Barre 4171103f807SLudovic Barre for (phase = 0; phase <= dlyb->max; phase++) { 4181103f807SLudovic Barre sdmmc_dlyb_set_cfgr(dlyb, dlyb->unit, phase, false); 4191103f807SLudovic Barre 4201103f807SLudovic Barre if (mmc_send_tuning(host->mmc, opcode, NULL)) { 4211103f807SLudovic Barre cur_len = 0; 4221103f807SLudovic Barre } else { 4231103f807SLudovic Barre cur_len++; 4241103f807SLudovic Barre if (cur_len > max_len) { 4251103f807SLudovic Barre max_len = cur_len; 4261103f807SLudovic Barre end_of_len = phase; 4271103f807SLudovic Barre } 4281103f807SLudovic Barre } 4291103f807SLudovic Barre } 4301103f807SLudovic Barre 4311103f807SLudovic Barre if (!max_len) { 4321103f807SLudovic Barre dev_err(mmc_dev(host->mmc), "no tuning point found\n"); 4331103f807SLudovic Barre return -EINVAL; 4341103f807SLudovic Barre } 4351103f807SLudovic Barre 4361103f807SLudovic Barre phase = end_of_len - max_len / 2; 4371103f807SLudovic Barre sdmmc_dlyb_set_cfgr(dlyb, dlyb->unit, phase, false); 4381103f807SLudovic Barre 4391103f807SLudovic Barre dev_dbg(mmc_dev(host->mmc), "unit:%d max_dly:%d phase:%d\n", 4401103f807SLudovic Barre dlyb->unit, dlyb->max, phase); 4411103f807SLudovic Barre 4421103f807SLudovic Barre return 0; 4431103f807SLudovic Barre } 4441103f807SLudovic Barre 4451103f807SLudovic Barre static int sdmmc_execute_tuning(struct mmc_host *mmc, u32 opcode) 4461103f807SLudovic Barre { 4471103f807SLudovic Barre struct mmci_host *host = mmc_priv(mmc); 4481103f807SLudovic Barre struct sdmmc_dlyb *dlyb = host->variant_priv; 4491103f807SLudovic Barre 4501103f807SLudovic Barre if (!dlyb || !dlyb->base) 4511103f807SLudovic Barre return -EINVAL; 4521103f807SLudovic Barre 4531103f807SLudovic Barre if (sdmmc_dlyb_lng_tuning(host)) 4541103f807SLudovic Barre return -EINVAL; 4551103f807SLudovic Barre 4561103f807SLudovic Barre return sdmmc_dlyb_phase_tuning(host, opcode); 4571103f807SLudovic Barre } 4581103f807SLudovic Barre 45946b723ddSLudovic Barre static struct mmci_host_ops sdmmc_variant_ops = { 46046b723ddSLudovic Barre .validate_data = sdmmc_idma_validate_data, 46146b723ddSLudovic Barre .prep_data = sdmmc_idma_prep_data, 46246b723ddSLudovic Barre .unprep_data = sdmmc_idma_unprep_data, 4638372f9d0SLudovic Barre .get_datactrl_cfg = sdmmc_get_dctrl_cfg, 46446b723ddSLudovic Barre .dma_setup = sdmmc_idma_setup, 46546b723ddSLudovic Barre .dma_start = sdmmc_idma_start, 46646b723ddSLudovic Barre .dma_finalize = sdmmc_idma_finalize, 46746b723ddSLudovic Barre .set_clkreg = mmci_sdmmc_set_clkreg, 46846b723ddSLudovic Barre .set_pwrreg = mmci_sdmmc_set_pwrreg, 4690e68de6aSLudovic Barre .busy_complete = sdmmc_busy_complete, 47046b723ddSLudovic Barre }; 47146b723ddSLudovic Barre 47246b723ddSLudovic Barre void sdmmc_variant_init(struct mmci_host *host) 47346b723ddSLudovic Barre { 4741103f807SLudovic Barre struct device_node *np = host->mmc->parent->of_node; 4751103f807SLudovic Barre void __iomem *base_dlyb; 4761103f807SLudovic Barre struct sdmmc_dlyb *dlyb; 4771103f807SLudovic Barre 47846b723ddSLudovic Barre host->ops = &sdmmc_variant_ops; 4791103f807SLudovic Barre 4801103f807SLudovic Barre base_dlyb = devm_of_iomap(mmc_dev(host->mmc), np, 1, NULL); 4811103f807SLudovic Barre if (IS_ERR(base_dlyb)) 4821103f807SLudovic Barre return; 4831103f807SLudovic Barre 4841103f807SLudovic Barre dlyb = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dlyb), GFP_KERNEL); 4851103f807SLudovic Barre if (!dlyb) 4861103f807SLudovic Barre return; 4871103f807SLudovic Barre 4881103f807SLudovic Barre dlyb->base = base_dlyb; 4891103f807SLudovic Barre host->variant_priv = dlyb; 4901103f807SLudovic Barre host->mmc_ops->execute_tuning = sdmmc_execute_tuning; 49146b723ddSLudovic Barre } 492