1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for Synopsys DesignWare Cores Mobile Storage Host Controller 4 * 5 * Copyright (C) 2018 Synaptics Incorporated 6 * 7 * Author: Jisheng Zhang <jszhang@kernel.org> 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/bitfield.h> 12 #include <linux/clk.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/iopoll.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/reset.h> 21 #include <linux/sizes.h> 22 23 #include "sdhci-pltfm.h" 24 #include "cqhci.h" 25 26 #define SDHCI_DWCMSHC_ARG2_STUFF GENMASK(31, 16) 27 28 /* DWCMSHC specific Mode Select value */ 29 #define DWCMSHC_CTRL_HS400 0x7 30 31 /* DWC IP vendor area 1 pointer */ 32 #define DWCMSHC_P_VENDOR_AREA1 0xe8 33 #define DWCMSHC_AREA1_MASK GENMASK(11, 0) 34 /* Offset inside the vendor area 1 */ 35 #define DWCMSHC_HOST_CTRL3 0x8 36 #define DWCMSHC_EMMC_CONTROL 0x2c 37 #define DWCMSHC_CARD_IS_EMMC BIT(0) 38 #define DWCMSHC_ENHANCED_STROBE BIT(8) 39 #define DWCMSHC_EMMC_ATCTRL 0x40 40 /* Tuning and auto-tuning fields in AT_CTRL_R control register */ 41 #define AT_CTRL_AT_EN BIT(0) /* autotuning is enabled */ 42 #define AT_CTRL_CI_SEL BIT(1) /* interval to drive center phase select */ 43 #define AT_CTRL_SWIN_TH_EN BIT(2) /* sampling window threshold enable */ 44 #define AT_CTRL_RPT_TUNE_ERR BIT(3) /* enable reporting framing errors */ 45 #define AT_CTRL_SW_TUNE_EN BIT(4) /* enable software managed tuning */ 46 #define AT_CTRL_WIN_EDGE_SEL_MASK GENMASK(11, 8) /* bits [11:8] */ 47 #define AT_CTRL_WIN_EDGE_SEL 0xf /* sampling window edge select */ 48 #define AT_CTRL_TUNE_CLK_STOP_EN BIT(16) /* clocks stopped during phase code change */ 49 #define AT_CTRL_PRE_CHANGE_DLY_MASK GENMASK(18, 17) /* bits [18:17] */ 50 #define AT_CTRL_PRE_CHANGE_DLY 0x1 /* 2-cycle latency */ 51 #define AT_CTRL_POST_CHANGE_DLY_MASK GENMASK(20, 19) /* bits [20:19] */ 52 #define AT_CTRL_POST_CHANGE_DLY 0x3 /* 4-cycle latency */ 53 #define AT_CTRL_SWIN_TH_VAL_MASK GENMASK(31, 24) /* bits [31:24] */ 54 #define AT_CTRL_SWIN_TH_VAL 0x9 /* sampling window threshold */ 55 56 /* DWC IP vendor area 2 pointer */ 57 #define DWCMSHC_P_VENDOR_AREA2 0xea 58 59 /* Sophgo CV18XX specific Registers */ 60 #define CV18XX_SDHCI_MSHC_CTRL 0x00 61 #define CV18XX_EMMC_FUNC_EN BIT(0) 62 #define CV18XX_LATANCY_1T BIT(1) 63 #define CV18XX_SDHCI_PHY_TX_RX_DLY 0x40 64 #define CV18XX_PHY_TX_DLY_MSK GENMASK(6, 0) 65 #define CV18XX_PHY_TX_SRC_MSK GENMASK(9, 8) 66 #define CV18XX_PHY_TX_SRC_INVERT_CLK_TX 0x1 67 #define CV18XX_PHY_RX_DLY_MSK GENMASK(22, 16) 68 #define CV18XX_PHY_RX_SRC_MSK GENMASK(25, 24) 69 #define CV18XX_PHY_RX_SRC_INVERT_RX_CLK 0x1 70 #define CV18XX_SDHCI_PHY_CONFIG 0x4c 71 #define CV18XX_PHY_TX_BPS BIT(0) 72 73 #define CV18XX_TUNE_MAX 128 74 #define CV18XX_TUNE_STEP 1 75 #define CV18XX_RETRY_TUNING_MAX 50 76 77 /* Rockchip specific Registers */ 78 #define DWCMSHC_EMMC_DLL_CTRL 0x800 79 #define DWCMSHC_EMMC_DLL_RXCLK 0x804 80 #define DWCMSHC_EMMC_DLL_TXCLK 0x808 81 #define DWCMSHC_EMMC_DLL_STRBIN 0x80c 82 #define DECMSHC_EMMC_DLL_CMDOUT 0x810 83 #define DWCMSHC_EMMC_DLL_STATUS0 0x840 84 #define DWCMSHC_EMMC_DLL_START BIT(0) 85 #define DWCMSHC_EMMC_DLL_LOCKED BIT(8) 86 #define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9) 87 #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29 88 #define DWCMSHC_EMMC_DLL_START_POINT 16 89 #define DWCMSHC_EMMC_DLL_INC 8 90 #define DWCMSHC_EMMC_DLL_BYPASS BIT(24) 91 #define DWCMSHC_EMMC_DLL_DLYENA BIT(27) 92 #define DLL_TXCLK_TAPNUM_DEFAULT 0x10 93 #define DLL_TXCLK_TAPNUM_90_DEGREES 0xA 94 #define DLL_TXCLK_TAPNUM_FROM_SW BIT(24) 95 #define DLL_STRBIN_TAPNUM_DEFAULT 0x8 96 #define DLL_STRBIN_TAPNUM_FROM_SW BIT(24) 97 #define DLL_STRBIN_DELAY_NUM_SEL BIT(26) 98 #define DLL_STRBIN_DELAY_NUM_OFFSET 16 99 #define DLL_STRBIN_DELAY_NUM_DEFAULT 0x16 100 #define DLL_RXCLK_NO_INVERTER 1 101 #define DLL_RXCLK_INVERTER 0 102 #define DLL_CMDOUT_TAPNUM_90_DEGREES 0x8 103 #define DLL_RXCLK_ORI_GATE BIT(31) 104 #define DLL_CMDOUT_TAPNUM_FROM_SW BIT(24) 105 #define DLL_CMDOUT_SRC_CLK_NEG BIT(28) 106 #define DLL_CMDOUT_EN_SRC_CLK_NEG BIT(29) 107 108 #define DLL_LOCK_WO_TMOUT(x) \ 109 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \ 110 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0)) 111 #define RK35xx_MAX_CLKS 3 112 113 /* PHY register area pointer */ 114 #define DWC_MSHC_PTR_PHY_R 0x300 115 116 /* PHY general configuration */ 117 #define PHY_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x00) 118 #define PHY_CNFG_RSTN_DEASSERT 0x1 /* Deassert PHY reset */ 119 #define PHY_CNFG_PAD_SP_MASK GENMASK(19, 16) /* bits [19:16] */ 120 #define PHY_CNFG_PAD_SP 0x0c /* PMOS TX drive strength */ 121 #define PHY_CNFG_PAD_SN_MASK GENMASK(23, 20) /* bits [23:20] */ 122 #define PHY_CNFG_PAD_SN 0x0c /* NMOS TX drive strength */ 123 124 /* PHY command/response pad settings */ 125 #define PHY_CMDPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x04) 126 127 /* PHY data pad settings */ 128 #define PHY_DATAPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x06) 129 130 /* PHY clock pad settings */ 131 #define PHY_CLKPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x08) 132 133 /* PHY strobe pad settings */ 134 #define PHY_STBPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0a) 135 136 /* PHY reset pad settings */ 137 #define PHY_RSTNPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0c) 138 139 /* Bitfields are common for all pad settings */ 140 #define PHY_PAD_RXSEL_1V8 0x1 /* Receiver type select for 1.8V */ 141 #define PHY_PAD_RXSEL_3V3 0x2 /* Receiver type select for 3.3V */ 142 143 #define PHY_PAD_WEAKPULL_MASK GENMASK(4, 3) /* bits [4:3] */ 144 #define PHY_PAD_WEAKPULL_PULLUP 0x1 /* Weak pull up enabled */ 145 #define PHY_PAD_WEAKPULL_PULLDOWN 0x2 /* Weak pull down enabled */ 146 147 #define PHY_PAD_TXSLEW_CTRL_P_MASK GENMASK(8, 5) /* bits [8:5] */ 148 #define PHY_PAD_TXSLEW_CTRL_P 0x3 /* Slew control for P-Type pad TX */ 149 #define PHY_PAD_TXSLEW_CTRL_N_MASK GENMASK(12, 9) /* bits [12:9] */ 150 #define PHY_PAD_TXSLEW_CTRL_N 0x3 /* Slew control for N-Type pad TX */ 151 152 /* PHY CLK delay line settings */ 153 #define PHY_SDCLKDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x1d) 154 #define PHY_SDCLKDL_CNFG_UPDATE BIT(4) /* set before writing to SDCLKDL_DC */ 155 156 /* PHY CLK delay line delay code */ 157 #define PHY_SDCLKDL_DC_R (DWC_MSHC_PTR_PHY_R + 0x1e) 158 #define PHY_SDCLKDL_DC_INITIAL 0x40 /* initial delay code */ 159 #define PHY_SDCLKDL_DC_DEFAULT 0x32 /* default delay code */ 160 #define PHY_SDCLKDL_DC_HS400 0x18 /* delay code for HS400 mode */ 161 162 /* PHY drift_cclk_rx delay line configuration setting */ 163 #define PHY_ATDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x21) 164 #define PHY_ATDL_CNFG_INPSEL_MASK GENMASK(3, 2) /* bits [3:2] */ 165 #define PHY_ATDL_CNFG_INPSEL 0x3 /* delay line input source */ 166 167 /* PHY DLL control settings */ 168 #define PHY_DLL_CTRL_R (DWC_MSHC_PTR_PHY_R + 0x24) 169 #define PHY_DLL_CTRL_DISABLE 0x0 /* PHY DLL is enabled */ 170 #define PHY_DLL_CTRL_ENABLE 0x1 /* PHY DLL is disabled */ 171 172 /* PHY DLL configuration register 1 */ 173 #define PHY_DLL_CNFG1_R (DWC_MSHC_PTR_PHY_R + 0x25) 174 #define PHY_DLL_CNFG1_SLVDLY_MASK GENMASK(5, 4) /* bits [5:4] */ 175 #define PHY_DLL_CNFG1_SLVDLY 0x2 /* DLL slave update delay input */ 176 #define PHY_DLL_CNFG1_WAITCYCLE 0x5 /* DLL wait cycle input */ 177 178 /* PHY DLL configuration register 2 */ 179 #define PHY_DLL_CNFG2_R (DWC_MSHC_PTR_PHY_R + 0x26) 180 #define PHY_DLL_CNFG2_JUMPSTEP 0xa /* DLL jump step input */ 181 182 /* PHY DLL master and slave delay line configuration settings */ 183 #define PHY_DLLDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x28) 184 #define PHY_DLLDL_CNFG_SLV_INPSEL_MASK GENMASK(6, 5) /* bits [6:5] */ 185 #define PHY_DLLDL_CNFG_SLV_INPSEL 0x3 /* clock source select for slave DL */ 186 187 #define FLAG_IO_FIXED_1V8 BIT(0) 188 189 #define BOUNDARY_OK(addr, len) \ 190 ((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1))) 191 192 #define DWCMSHC_SDHCI_CQE_TRNS_MODE (SDHCI_TRNS_MULTI | \ 193 SDHCI_TRNS_BLK_CNT_EN | \ 194 SDHCI_TRNS_DMA) 195 196 enum dwcmshc_rk_type { 197 DWCMSHC_RK3568, 198 DWCMSHC_RK3588, 199 }; 200 201 struct rk35xx_priv { 202 /* Rockchip specified optional clocks */ 203 struct clk_bulk_data rockchip_clks[RK35xx_MAX_CLKS]; 204 struct reset_control *reset; 205 enum dwcmshc_rk_type devtype; 206 u8 txclk_tapnum; 207 }; 208 209 struct dwcmshc_priv { 210 struct clk *bus_clk; 211 int vendor_specific_area1; /* P_VENDOR_SPECIFIC_AREA1 reg */ 212 int vendor_specific_area2; /* P_VENDOR_SPECIFIC_AREA2 reg */ 213 214 void *priv; /* pointer to SoC private stuff */ 215 u16 delay_line; 216 u16 flags; 217 }; 218 219 /* 220 * If DMA addr spans 128MB boundary, we split the DMA transfer into two 221 * so that each DMA transfer doesn't exceed the boundary. 222 */ 223 static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc, 224 dma_addr_t addr, int len, unsigned int cmd) 225 { 226 int tmplen, offset; 227 228 if (likely(!len || BOUNDARY_OK(addr, len))) { 229 sdhci_adma_write_desc(host, desc, addr, len, cmd); 230 return; 231 } 232 233 offset = addr & (SZ_128M - 1); 234 tmplen = SZ_128M - offset; 235 sdhci_adma_write_desc(host, desc, addr, tmplen, cmd); 236 237 addr += tmplen; 238 len -= tmplen; 239 sdhci_adma_write_desc(host, desc, addr, len, cmd); 240 } 241 242 static unsigned int dwcmshc_get_max_clock(struct sdhci_host *host) 243 { 244 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 245 246 if (pltfm_host->clk) 247 return sdhci_pltfm_clk_get_max_clock(host); 248 else 249 return pltfm_host->clock; 250 } 251 252 static unsigned int rk35xx_get_max_clock(struct sdhci_host *host) 253 { 254 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 255 256 return clk_round_rate(pltfm_host->clk, ULONG_MAX); 257 } 258 259 static void dwcmshc_check_auto_cmd23(struct mmc_host *mmc, 260 struct mmc_request *mrq) 261 { 262 struct sdhci_host *host = mmc_priv(mmc); 263 264 /* 265 * No matter V4 is enabled or not, ARGUMENT2 register is 32-bit 266 * block count register which doesn't support stuff bits of 267 * CMD23 argument on dwcmsch host controller. 268 */ 269 if (mrq->sbc && (mrq->sbc->arg & SDHCI_DWCMSHC_ARG2_STUFF)) 270 host->flags &= ~SDHCI_AUTO_CMD23; 271 else 272 host->flags |= SDHCI_AUTO_CMD23; 273 } 274 275 static void dwcmshc_request(struct mmc_host *mmc, struct mmc_request *mrq) 276 { 277 dwcmshc_check_auto_cmd23(mmc, mrq); 278 279 sdhci_request(mmc, mrq); 280 } 281 282 static void dwcmshc_phy_1_8v_init(struct sdhci_host *host) 283 { 284 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 285 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 286 u32 val; 287 288 /* deassert phy reset & set tx drive strength */ 289 val = PHY_CNFG_RSTN_DEASSERT; 290 val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP); 291 val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN); 292 sdhci_writel(host, val, PHY_CNFG_R); 293 294 /* disable delay line */ 295 sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R); 296 297 /* set delay line */ 298 sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R); 299 sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R); 300 301 /* enable delay lane */ 302 val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R); 303 val &= ~(PHY_SDCLKDL_CNFG_UPDATE); 304 sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R); 305 306 /* configure phy pads */ 307 val = PHY_PAD_RXSEL_1V8; 308 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP); 309 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 310 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 311 sdhci_writew(host, val, PHY_CMDPAD_CNFG_R); 312 sdhci_writew(host, val, PHY_DATAPAD_CNFG_R); 313 sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R); 314 315 val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 316 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 317 sdhci_writew(host, val, PHY_CLKPAD_CNFG_R); 318 319 val = PHY_PAD_RXSEL_1V8; 320 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN); 321 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 322 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 323 sdhci_writew(host, val, PHY_STBPAD_CNFG_R); 324 325 /* enable data strobe mode */ 326 sdhci_writeb(host, FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, PHY_DLLDL_CNFG_SLV_INPSEL), 327 PHY_DLLDL_CNFG_R); 328 329 /* enable phy dll */ 330 sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R); 331 } 332 333 static void dwcmshc_phy_3_3v_init(struct sdhci_host *host) 334 { 335 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 336 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 337 u32 val; 338 339 /* deassert phy reset & set tx drive strength */ 340 val = PHY_CNFG_RSTN_DEASSERT; 341 val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP); 342 val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN); 343 sdhci_writel(host, val, PHY_CNFG_R); 344 345 /* disable delay line */ 346 sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R); 347 348 /* set delay line */ 349 sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R); 350 sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R); 351 352 /* enable delay lane */ 353 val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R); 354 val &= ~(PHY_SDCLKDL_CNFG_UPDATE); 355 sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R); 356 357 /* configure phy pads */ 358 val = PHY_PAD_RXSEL_3V3; 359 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP); 360 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 361 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 362 sdhci_writew(host, val, PHY_CMDPAD_CNFG_R); 363 sdhci_writew(host, val, PHY_DATAPAD_CNFG_R); 364 sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R); 365 366 val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 367 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 368 sdhci_writew(host, val, PHY_CLKPAD_CNFG_R); 369 370 val = PHY_PAD_RXSEL_3V3; 371 val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN); 372 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); 373 val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); 374 sdhci_writew(host, val, PHY_STBPAD_CNFG_R); 375 376 /* enable phy dll */ 377 sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R); 378 } 379 380 static void th1520_sdhci_set_phy(struct sdhci_host *host) 381 { 382 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 383 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 384 u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; 385 u16 emmc_ctrl; 386 387 /* Before power on, set PHY configs */ 388 if (priv->flags & FLAG_IO_FIXED_1V8) 389 dwcmshc_phy_1_8v_init(host); 390 else 391 dwcmshc_phy_3_3v_init(host); 392 393 if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { 394 emmc_ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); 395 emmc_ctrl |= DWCMSHC_CARD_IS_EMMC; 396 sdhci_writew(host, emmc_ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); 397 } 398 399 sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) | 400 PHY_DLL_CNFG1_WAITCYCLE, PHY_DLL_CNFG1_R); 401 } 402 403 static void dwcmshc_set_uhs_signaling(struct sdhci_host *host, 404 unsigned int timing) 405 { 406 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 407 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 408 u16 ctrl, ctrl_2; 409 410 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); 411 /* Select Bus Speed Mode for host */ 412 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; 413 if ((timing == MMC_TIMING_MMC_HS200) || 414 (timing == MMC_TIMING_UHS_SDR104)) 415 ctrl_2 |= SDHCI_CTRL_UHS_SDR104; 416 else if (timing == MMC_TIMING_UHS_SDR12) 417 ctrl_2 |= SDHCI_CTRL_UHS_SDR12; 418 else if ((timing == MMC_TIMING_UHS_SDR25) || 419 (timing == MMC_TIMING_MMC_HS)) 420 ctrl_2 |= SDHCI_CTRL_UHS_SDR25; 421 else if (timing == MMC_TIMING_UHS_SDR50) 422 ctrl_2 |= SDHCI_CTRL_UHS_SDR50; 423 else if ((timing == MMC_TIMING_UHS_DDR50) || 424 (timing == MMC_TIMING_MMC_DDR52)) 425 ctrl_2 |= SDHCI_CTRL_UHS_DDR50; 426 else if (timing == MMC_TIMING_MMC_HS400) { 427 /* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */ 428 ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); 429 ctrl |= DWCMSHC_CARD_IS_EMMC; 430 sdhci_writew(host, ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); 431 432 ctrl_2 |= DWCMSHC_CTRL_HS400; 433 } 434 435 if (priv->flags & FLAG_IO_FIXED_1V8) 436 ctrl_2 |= SDHCI_CTRL_VDD_180; 437 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); 438 } 439 440 static void th1520_set_uhs_signaling(struct sdhci_host *host, 441 unsigned int timing) 442 { 443 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 444 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 445 446 dwcmshc_set_uhs_signaling(host, timing); 447 if (timing == MMC_TIMING_MMC_HS400) 448 priv->delay_line = PHY_SDCLKDL_DC_HS400; 449 else 450 sdhci_writeb(host, 0, PHY_DLLDL_CNFG_R); 451 th1520_sdhci_set_phy(host); 452 } 453 454 static void dwcmshc_hs400_enhanced_strobe(struct mmc_host *mmc, 455 struct mmc_ios *ios) 456 { 457 u32 vendor; 458 struct sdhci_host *host = mmc_priv(mmc); 459 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 460 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 461 int reg = priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL; 462 463 vendor = sdhci_readl(host, reg); 464 if (ios->enhanced_strobe) 465 vendor |= DWCMSHC_ENHANCED_STROBE; 466 else 467 vendor &= ~DWCMSHC_ENHANCED_STROBE; 468 469 sdhci_writel(host, vendor, reg); 470 } 471 472 static int dwcmshc_execute_tuning(struct mmc_host *mmc, u32 opcode) 473 { 474 int err = sdhci_execute_tuning(mmc, opcode); 475 struct sdhci_host *host = mmc_priv(mmc); 476 477 if (err) 478 return err; 479 480 /* 481 * Tuning can leave the IP in an active state (Buffer Read Enable bit 482 * set) which prevents the entry to low power states (i.e. S0i3). Data 483 * reset will clear it. 484 */ 485 sdhci_reset(host, SDHCI_RESET_DATA); 486 487 return 0; 488 } 489 490 static u32 dwcmshc_cqe_irq_handler(struct sdhci_host *host, u32 intmask) 491 { 492 int cmd_error = 0; 493 int data_error = 0; 494 495 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error)) 496 return intmask; 497 498 cqhci_irq(host->mmc, intmask, cmd_error, data_error); 499 500 return 0; 501 } 502 503 static void dwcmshc_sdhci_cqe_enable(struct mmc_host *mmc) 504 { 505 struct sdhci_host *host = mmc_priv(mmc); 506 u8 ctrl; 507 508 sdhci_writew(host, DWCMSHC_SDHCI_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE); 509 510 sdhci_cqe_enable(mmc); 511 512 /* 513 * The "DesignWare Cores Mobile Storage Host Controller 514 * DWC_mshc / DWC_mshc_lite Databook" says: 515 * when Host Version 4 Enable" is 1 in Host Control 2 register, 516 * SDHCI_CTRL_ADMA32 bit means ADMA2 is selected. 517 * Selection of 32-bit/64-bit System Addressing: 518 * either 32-bit or 64-bit system addressing is selected by 519 * 64-bit Addressing bit in Host Control 2 register. 520 * 521 * On the other hand the "DesignWare Cores Mobile Storage Host 522 * Controller DWC_mshc / DWC_mshc_lite User Guide" says, that we have to 523 * set DMA_SEL to ADMA2 _only_ mode in the Host Control 2 register. 524 */ 525 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 526 ctrl &= ~SDHCI_CTRL_DMA_MASK; 527 ctrl |= SDHCI_CTRL_ADMA32; 528 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 529 } 530 531 static void dwcmshc_set_tran_desc(struct cqhci_host *cq_host, u8 **desc, 532 dma_addr_t addr, int len, bool end, bool dma64) 533 { 534 int tmplen, offset; 535 536 if (likely(!len || BOUNDARY_OK(addr, len))) { 537 cqhci_set_tran_desc(*desc, addr, len, end, dma64); 538 return; 539 } 540 541 offset = addr & (SZ_128M - 1); 542 tmplen = SZ_128M - offset; 543 cqhci_set_tran_desc(*desc, addr, tmplen, false, dma64); 544 545 addr += tmplen; 546 len -= tmplen; 547 *desc += cq_host->trans_desc_len; 548 cqhci_set_tran_desc(*desc, addr, len, end, dma64); 549 } 550 551 static void dwcmshc_cqhci_dumpregs(struct mmc_host *mmc) 552 { 553 sdhci_dumpregs(mmc_priv(mmc)); 554 } 555 556 static void dwcmshc_rk3568_set_clock(struct sdhci_host *host, unsigned int clock) 557 { 558 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 559 struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); 560 struct rk35xx_priv *priv = dwc_priv->priv; 561 u8 txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT; 562 u32 extra, reg; 563 int err; 564 565 host->mmc->actual_clock = 0; 566 567 if (clock == 0) { 568 /* Disable interface clock at initial state. */ 569 sdhci_set_clock(host, clock); 570 return; 571 } 572 573 /* Rockchip platform only support 375KHz for identify mode */ 574 if (clock <= 400000) 575 clock = 375000; 576 577 err = clk_set_rate(pltfm_host->clk, clock); 578 if (err) 579 dev_err(mmc_dev(host->mmc), "fail to set clock %d", clock); 580 581 sdhci_set_clock(host, clock); 582 583 /* Disable cmd conflict check */ 584 reg = dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3; 585 extra = sdhci_readl(host, reg); 586 extra &= ~BIT(0); 587 sdhci_writel(host, extra, reg); 588 589 if (clock <= 52000000) { 590 /* 591 * Disable DLL and reset both of sample and drive clock. 592 * The bypass bit and start bit need to be set if DLL is not locked. 593 */ 594 sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START, DWCMSHC_EMMC_DLL_CTRL); 595 sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK); 596 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK); 597 sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT); 598 /* 599 * Before switching to hs400es mode, the driver will enable 600 * enhanced strobe first. PHY needs to configure the parameters 601 * of enhanced strobe first. 602 */ 603 extra = DWCMSHC_EMMC_DLL_DLYENA | 604 DLL_STRBIN_DELAY_NUM_SEL | 605 DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET; 606 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); 607 return; 608 } 609 610 /* Reset DLL */ 611 sdhci_writel(host, BIT(1), DWCMSHC_EMMC_DLL_CTRL); 612 udelay(1); 613 sdhci_writel(host, 0x0, DWCMSHC_EMMC_DLL_CTRL); 614 615 /* 616 * We shouldn't set DLL_RXCLK_NO_INVERTER for identify mode but 617 * we must set it in higher speed mode. 618 */ 619 extra = DWCMSHC_EMMC_DLL_DLYENA; 620 if (priv->devtype == DWCMSHC_RK3568) 621 extra |= DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL; 622 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK); 623 624 /* Init DLL settings */ 625 extra = 0x5 << DWCMSHC_EMMC_DLL_START_POINT | 626 0x2 << DWCMSHC_EMMC_DLL_INC | 627 DWCMSHC_EMMC_DLL_START; 628 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL); 629 err = readl_poll_timeout(host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0, 630 extra, DLL_LOCK_WO_TMOUT(extra), 1, 631 500 * USEC_PER_MSEC); 632 if (err) { 633 dev_err(mmc_dev(host->mmc), "DLL lock timeout!\n"); 634 return; 635 } 636 637 extra = 0x1 << 16 | /* tune clock stop en */ 638 0x3 << 17 | /* pre-change delay */ 639 0x3 << 19; /* post-change delay */ 640 sdhci_writel(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); 641 642 if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 || 643 host->mmc->ios.timing == MMC_TIMING_MMC_HS400) 644 txclk_tapnum = priv->txclk_tapnum; 645 646 if ((priv->devtype == DWCMSHC_RK3588) && host->mmc->ios.timing == MMC_TIMING_MMC_HS400) { 647 txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES; 648 649 extra = DLL_CMDOUT_SRC_CLK_NEG | 650 DLL_CMDOUT_EN_SRC_CLK_NEG | 651 DWCMSHC_EMMC_DLL_DLYENA | 652 DLL_CMDOUT_TAPNUM_90_DEGREES | 653 DLL_CMDOUT_TAPNUM_FROM_SW; 654 sdhci_writel(host, extra, DECMSHC_EMMC_DLL_CMDOUT); 655 } 656 657 extra = DWCMSHC_EMMC_DLL_DLYENA | 658 DLL_TXCLK_TAPNUM_FROM_SW | 659 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL | 660 txclk_tapnum; 661 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK); 662 663 extra = DWCMSHC_EMMC_DLL_DLYENA | 664 DLL_STRBIN_TAPNUM_DEFAULT | 665 DLL_STRBIN_TAPNUM_FROM_SW; 666 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); 667 } 668 669 static void rk35xx_sdhci_reset(struct sdhci_host *host, u8 mask) 670 { 671 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 672 struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); 673 struct rk35xx_priv *priv = dwc_priv->priv; 674 675 if (mask & SDHCI_RESET_ALL && priv->reset) { 676 reset_control_assert(priv->reset); 677 udelay(1); 678 reset_control_deassert(priv->reset); 679 } 680 681 sdhci_reset(host, mask); 682 } 683 684 static int th1520_execute_tuning(struct sdhci_host *host, u32 opcode) 685 { 686 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 687 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 688 u32 val = 0; 689 690 if (host->flags & SDHCI_HS400_TUNING) 691 return 0; 692 693 sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL), 694 PHY_ATDL_CNFG_R); 695 val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); 696 697 /* 698 * configure tuning settings: 699 * - center phase select code driven in block gap interval 700 * - disable reporting of framing errors 701 * - disable software managed tuning 702 * - disable user selection of sampling window edges, 703 * instead tuning calculated edges are used 704 */ 705 val &= ~(AT_CTRL_CI_SEL | AT_CTRL_RPT_TUNE_ERR | AT_CTRL_SW_TUNE_EN | 706 FIELD_PREP(AT_CTRL_WIN_EDGE_SEL_MASK, AT_CTRL_WIN_EDGE_SEL)); 707 708 /* 709 * configure tuning settings: 710 * - enable auto-tuning 711 * - enable sampling window threshold 712 * - stop clocks during phase code change 713 * - set max latency in cycles between tx and rx clocks 714 * - set max latency in cycles to switch output phase 715 * - set max sampling window threshold value 716 */ 717 val |= AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN; 718 val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY); 719 val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY); 720 val |= FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, AT_CTRL_SWIN_TH_VAL); 721 722 sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); 723 val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); 724 725 /* perform tuning */ 726 sdhci_start_tuning(host); 727 host->tuning_loop_count = 128; 728 host->tuning_err = __sdhci_execute_tuning(host, opcode); 729 if (host->tuning_err) { 730 /* disable auto-tuning upon tuning error */ 731 val &= ~AT_CTRL_AT_EN; 732 sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); 733 dev_err(mmc_dev(host->mmc), "tuning failed: %d\n", host->tuning_err); 734 return -EIO; 735 } 736 sdhci_end_tuning(host); 737 738 return 0; 739 } 740 741 static void th1520_sdhci_reset(struct sdhci_host *host, u8 mask) 742 { 743 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 744 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 745 u16 ctrl_2; 746 747 sdhci_reset(host, mask); 748 749 if (priv->flags & FLAG_IO_FIXED_1V8) { 750 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); 751 if (!(ctrl_2 & SDHCI_CTRL_VDD_180)) { 752 ctrl_2 |= SDHCI_CTRL_VDD_180; 753 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); 754 } 755 } 756 } 757 758 static void cv18xx_sdhci_reset(struct sdhci_host *host, u8 mask) 759 { 760 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 761 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 762 u32 val, emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; 763 764 sdhci_reset(host, mask); 765 766 if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { 767 val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 768 val |= CV18XX_EMMC_FUNC_EN; 769 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 770 } 771 772 val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 773 val |= CV18XX_LATANCY_1T; 774 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 775 776 val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); 777 val |= CV18XX_PHY_TX_BPS; 778 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); 779 780 val = (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) | 781 FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) | 782 FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, 0) | 783 FIELD_PREP(CV18XX_PHY_RX_SRC_MSK, CV18XX_PHY_RX_SRC_INVERT_RX_CLK)); 784 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY); 785 } 786 787 static void cv18xx_sdhci_set_tap(struct sdhci_host *host, int tap) 788 { 789 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 790 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 791 u16 clk; 792 u32 val; 793 794 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 795 clk &= ~SDHCI_CLOCK_CARD_EN; 796 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 797 798 val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 799 val &= ~CV18XX_LATANCY_1T; 800 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); 801 802 val = (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) | 803 FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) | 804 FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, tap)); 805 sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY); 806 807 sdhci_writel(host, 0, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); 808 809 clk |= SDHCI_CLOCK_CARD_EN; 810 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 811 usleep_range(1000, 2000); 812 } 813 814 static int cv18xx_retry_tuning(struct mmc_host *mmc, u32 opcode, int *cmd_error) 815 { 816 int ret, retry = 0; 817 818 while (retry < CV18XX_RETRY_TUNING_MAX) { 819 ret = mmc_send_tuning(mmc, opcode, NULL); 820 if (ret) 821 return ret; 822 retry++; 823 } 824 825 return 0; 826 } 827 828 static void cv18xx_sdhci_post_tuning(struct sdhci_host *host) 829 { 830 u32 val; 831 832 val = sdhci_readl(host, SDHCI_INT_STATUS); 833 val |= SDHCI_INT_DATA_AVAIL; 834 sdhci_writel(host, val, SDHCI_INT_STATUS); 835 836 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 837 } 838 839 static int cv18xx_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode) 840 { 841 int min, max, avg, ret; 842 int win_length, target_min, target_max, target_win_length; 843 844 min = max = 0; 845 target_win_length = 0; 846 847 sdhci_reset_tuning(host); 848 849 while (max < CV18XX_TUNE_MAX) { 850 /* find the mininum delay first which can pass tuning */ 851 while (min < CV18XX_TUNE_MAX) { 852 cv18xx_sdhci_set_tap(host, min); 853 if (!cv18xx_retry_tuning(host->mmc, opcode, NULL)) 854 break; 855 min += CV18XX_TUNE_STEP; 856 } 857 858 /* find the maxinum delay which can not pass tuning */ 859 max = min + CV18XX_TUNE_STEP; 860 while (max < CV18XX_TUNE_MAX) { 861 cv18xx_sdhci_set_tap(host, max); 862 if (cv18xx_retry_tuning(host->mmc, opcode, NULL)) { 863 max -= CV18XX_TUNE_STEP; 864 break; 865 } 866 max += CV18XX_TUNE_STEP; 867 } 868 869 win_length = max - min + 1; 870 /* get the largest pass window */ 871 if (win_length > target_win_length) { 872 target_win_length = win_length; 873 target_min = min; 874 target_max = max; 875 } 876 877 /* continue to find the next pass window */ 878 min = max + CV18XX_TUNE_STEP; 879 } 880 881 cv18xx_sdhci_post_tuning(host); 882 883 /* use average delay to get the best timing */ 884 avg = (target_min + target_max) / 2; 885 cv18xx_sdhci_set_tap(host, avg); 886 ret = mmc_send_tuning(host->mmc, opcode, NULL); 887 888 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n", 889 ret ? "failed" : "passed", avg, ret); 890 891 return ret; 892 } 893 894 static const struct sdhci_ops sdhci_dwcmshc_ops = { 895 .set_clock = sdhci_set_clock, 896 .set_bus_width = sdhci_set_bus_width, 897 .set_uhs_signaling = dwcmshc_set_uhs_signaling, 898 .get_max_clock = dwcmshc_get_max_clock, 899 .reset = sdhci_reset, 900 .adma_write_desc = dwcmshc_adma_write_desc, 901 .irq = dwcmshc_cqe_irq_handler, 902 }; 903 904 static const struct sdhci_ops sdhci_dwcmshc_rk35xx_ops = { 905 .set_clock = dwcmshc_rk3568_set_clock, 906 .set_bus_width = sdhci_set_bus_width, 907 .set_uhs_signaling = dwcmshc_set_uhs_signaling, 908 .get_max_clock = rk35xx_get_max_clock, 909 .reset = rk35xx_sdhci_reset, 910 .adma_write_desc = dwcmshc_adma_write_desc, 911 }; 912 913 static const struct sdhci_ops sdhci_dwcmshc_th1520_ops = { 914 .set_clock = sdhci_set_clock, 915 .set_bus_width = sdhci_set_bus_width, 916 .set_uhs_signaling = th1520_set_uhs_signaling, 917 .get_max_clock = dwcmshc_get_max_clock, 918 .reset = th1520_sdhci_reset, 919 .adma_write_desc = dwcmshc_adma_write_desc, 920 .voltage_switch = dwcmshc_phy_1_8v_init, 921 .platform_execute_tuning = th1520_execute_tuning, 922 }; 923 924 static const struct sdhci_ops sdhci_dwcmshc_cv18xx_ops = { 925 .set_clock = sdhci_set_clock, 926 .set_bus_width = sdhci_set_bus_width, 927 .set_uhs_signaling = dwcmshc_set_uhs_signaling, 928 .get_max_clock = dwcmshc_get_max_clock, 929 .reset = cv18xx_sdhci_reset, 930 .adma_write_desc = dwcmshc_adma_write_desc, 931 .platform_execute_tuning = cv18xx_sdhci_execute_tuning, 932 }; 933 934 static const struct sdhci_pltfm_data sdhci_dwcmshc_pdata = { 935 .ops = &sdhci_dwcmshc_ops, 936 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 937 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, 938 }; 939 940 #ifdef CONFIG_ACPI 941 static const struct sdhci_pltfm_data sdhci_dwcmshc_bf3_pdata = { 942 .ops = &sdhci_dwcmshc_ops, 943 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 944 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 945 SDHCI_QUIRK2_ACMD23_BROKEN, 946 }; 947 #endif 948 949 static const struct sdhci_pltfm_data sdhci_dwcmshc_rk35xx_pdata = { 950 .ops = &sdhci_dwcmshc_rk35xx_ops, 951 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | 952 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, 953 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 954 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, 955 }; 956 957 static const struct sdhci_pltfm_data sdhci_dwcmshc_th1520_pdata = { 958 .ops = &sdhci_dwcmshc_th1520_ops, 959 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 960 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, 961 }; 962 963 static const struct sdhci_pltfm_data sdhci_dwcmshc_cv18xx_pdata = { 964 .ops = &sdhci_dwcmshc_cv18xx_ops, 965 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 966 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, 967 }; 968 969 static const struct cqhci_host_ops dwcmshc_cqhci_ops = { 970 .enable = dwcmshc_sdhci_cqe_enable, 971 .disable = sdhci_cqe_disable, 972 .dumpregs = dwcmshc_cqhci_dumpregs, 973 .set_tran_desc = dwcmshc_set_tran_desc, 974 }; 975 976 static void dwcmshc_cqhci_init(struct sdhci_host *host, struct platform_device *pdev) 977 { 978 struct cqhci_host *cq_host; 979 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 980 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 981 bool dma64 = false; 982 u16 clk; 983 int err; 984 985 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD; 986 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL); 987 if (!cq_host) { 988 dev_err(mmc_dev(host->mmc), "Unable to setup CQE: not enough memory\n"); 989 goto dsbl_cqe_caps; 990 } 991 992 /* 993 * For dwcmshc host controller we have to enable internal clock 994 * before access to some registers from Vendor Specific Area 2. 995 */ 996 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 997 clk |= SDHCI_CLOCK_INT_EN; 998 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 999 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 1000 if (!(clk & SDHCI_CLOCK_INT_EN)) { 1001 dev_err(mmc_dev(host->mmc), "Unable to setup CQE: internal clock enable error\n"); 1002 goto free_cq_host; 1003 } 1004 1005 cq_host->mmio = host->ioaddr + priv->vendor_specific_area2; 1006 cq_host->ops = &dwcmshc_cqhci_ops; 1007 1008 /* Enable using of 128-bit task descriptors */ 1009 dma64 = host->flags & SDHCI_USE_64_BIT_DMA; 1010 if (dma64) { 1011 dev_dbg(mmc_dev(host->mmc), "128-bit task descriptors\n"); 1012 cq_host->caps |= CQHCI_TASK_DESC_SZ_128; 1013 } 1014 err = cqhci_init(cq_host, host->mmc, dma64); 1015 if (err) { 1016 dev_err(mmc_dev(host->mmc), "Unable to setup CQE: error %d\n", err); 1017 goto int_clock_disable; 1018 } 1019 1020 dev_dbg(mmc_dev(host->mmc), "CQE init done\n"); 1021 1022 return; 1023 1024 int_clock_disable: 1025 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 1026 clk &= ~SDHCI_CLOCK_INT_EN; 1027 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 1028 1029 free_cq_host: 1030 devm_kfree(&pdev->dev, cq_host); 1031 1032 dsbl_cqe_caps: 1033 host->mmc->caps2 &= ~(MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD); 1034 } 1035 1036 static int dwcmshc_rk35xx_init(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv) 1037 { 1038 int err; 1039 struct rk35xx_priv *priv = dwc_priv->priv; 1040 1041 priv->reset = devm_reset_control_array_get_optional_exclusive(mmc_dev(host->mmc)); 1042 if (IS_ERR(priv->reset)) { 1043 err = PTR_ERR(priv->reset); 1044 dev_err(mmc_dev(host->mmc), "failed to get reset control %d\n", err); 1045 return err; 1046 } 1047 1048 priv->rockchip_clks[0].id = "axi"; 1049 priv->rockchip_clks[1].id = "block"; 1050 priv->rockchip_clks[2].id = "timer"; 1051 err = devm_clk_bulk_get_optional(mmc_dev(host->mmc), RK35xx_MAX_CLKS, 1052 priv->rockchip_clks); 1053 if (err) { 1054 dev_err(mmc_dev(host->mmc), "failed to get clocks %d\n", err); 1055 return err; 1056 } 1057 1058 err = clk_bulk_prepare_enable(RK35xx_MAX_CLKS, priv->rockchip_clks); 1059 if (err) { 1060 dev_err(mmc_dev(host->mmc), "failed to enable clocks %d\n", err); 1061 return err; 1062 } 1063 1064 if (of_property_read_u8(mmc_dev(host->mmc)->of_node, "rockchip,txclk-tapnum", 1065 &priv->txclk_tapnum)) 1066 priv->txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT; 1067 1068 /* Disable cmd conflict check */ 1069 sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3); 1070 /* Reset previous settings */ 1071 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK); 1072 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN); 1073 1074 return 0; 1075 } 1076 1077 static void dwcmshc_rk35xx_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv) 1078 { 1079 /* 1080 * Don't support highspeed bus mode with low clk speed as we 1081 * cannot use DLL for this condition. 1082 */ 1083 if (host->mmc->f_max <= 52000000) { 1084 dev_info(mmc_dev(host->mmc), "Disabling HS200/HS400, frequency too low (%d)\n", 1085 host->mmc->f_max); 1086 host->mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400); 1087 host->mmc->caps &= ~(MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR); 1088 } 1089 } 1090 1091 static const struct of_device_id sdhci_dwcmshc_dt_ids[] = { 1092 { 1093 .compatible = "rockchip,rk3588-dwcmshc", 1094 .data = &sdhci_dwcmshc_rk35xx_pdata, 1095 }, 1096 { 1097 .compatible = "rockchip,rk3568-dwcmshc", 1098 .data = &sdhci_dwcmshc_rk35xx_pdata, 1099 }, 1100 { 1101 .compatible = "snps,dwcmshc-sdhci", 1102 .data = &sdhci_dwcmshc_pdata, 1103 }, 1104 { 1105 .compatible = "sophgo,cv1800b-dwcmshc", 1106 .data = &sdhci_dwcmshc_cv18xx_pdata, 1107 }, 1108 { 1109 .compatible = "sophgo,sg2002-dwcmshc", 1110 .data = &sdhci_dwcmshc_cv18xx_pdata, 1111 }, 1112 { 1113 .compatible = "thead,th1520-dwcmshc", 1114 .data = &sdhci_dwcmshc_th1520_pdata, 1115 }, 1116 {}, 1117 }; 1118 MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids); 1119 1120 #ifdef CONFIG_ACPI 1121 static const struct acpi_device_id sdhci_dwcmshc_acpi_ids[] = { 1122 { 1123 .id = "MLNXBF30", 1124 .driver_data = (kernel_ulong_t)&sdhci_dwcmshc_bf3_pdata, 1125 }, 1126 {} 1127 }; 1128 MODULE_DEVICE_TABLE(acpi, sdhci_dwcmshc_acpi_ids); 1129 #endif 1130 1131 static int dwcmshc_probe(struct platform_device *pdev) 1132 { 1133 struct device *dev = &pdev->dev; 1134 struct sdhci_pltfm_host *pltfm_host; 1135 struct sdhci_host *host; 1136 struct dwcmshc_priv *priv; 1137 struct rk35xx_priv *rk_priv = NULL; 1138 const struct sdhci_pltfm_data *pltfm_data; 1139 int err; 1140 u32 extra, caps; 1141 1142 pltfm_data = device_get_match_data(&pdev->dev); 1143 if (!pltfm_data) { 1144 dev_err(&pdev->dev, "Error: No device match data found\n"); 1145 return -ENODEV; 1146 } 1147 1148 host = sdhci_pltfm_init(pdev, pltfm_data, 1149 sizeof(struct dwcmshc_priv)); 1150 if (IS_ERR(host)) 1151 return PTR_ERR(host); 1152 1153 /* 1154 * extra adma table cnt for cross 128M boundary handling. 1155 */ 1156 extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M); 1157 if (extra > SDHCI_MAX_SEGS) 1158 extra = SDHCI_MAX_SEGS; 1159 host->adma_table_cnt += extra; 1160 1161 pltfm_host = sdhci_priv(host); 1162 priv = sdhci_pltfm_priv(pltfm_host); 1163 1164 if (dev->of_node) { 1165 pltfm_host->clk = devm_clk_get(dev, "core"); 1166 if (IS_ERR(pltfm_host->clk)) { 1167 err = PTR_ERR(pltfm_host->clk); 1168 dev_err(dev, "failed to get core clk: %d\n", err); 1169 goto free_pltfm; 1170 } 1171 err = clk_prepare_enable(pltfm_host->clk); 1172 if (err) 1173 goto free_pltfm; 1174 1175 priv->bus_clk = devm_clk_get(dev, "bus"); 1176 if (!IS_ERR(priv->bus_clk)) 1177 clk_prepare_enable(priv->bus_clk); 1178 } 1179 1180 err = mmc_of_parse(host->mmc); 1181 if (err) 1182 goto err_clk; 1183 1184 sdhci_get_of_property(pdev); 1185 1186 priv->vendor_specific_area1 = 1187 sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK; 1188 1189 host->mmc_host_ops.request = dwcmshc_request; 1190 host->mmc_host_ops.hs400_enhanced_strobe = dwcmshc_hs400_enhanced_strobe; 1191 host->mmc_host_ops.execute_tuning = dwcmshc_execute_tuning; 1192 1193 if (pltfm_data == &sdhci_dwcmshc_rk35xx_pdata) { 1194 rk_priv = devm_kzalloc(&pdev->dev, sizeof(struct rk35xx_priv), GFP_KERNEL); 1195 if (!rk_priv) { 1196 err = -ENOMEM; 1197 goto err_clk; 1198 } 1199 1200 if (of_device_is_compatible(pdev->dev.of_node, "rockchip,rk3588-dwcmshc")) 1201 rk_priv->devtype = DWCMSHC_RK3588; 1202 else 1203 rk_priv->devtype = DWCMSHC_RK3568; 1204 1205 priv->priv = rk_priv; 1206 1207 err = dwcmshc_rk35xx_init(host, priv); 1208 if (err) 1209 goto err_clk; 1210 } 1211 1212 if (pltfm_data == &sdhci_dwcmshc_th1520_pdata) { 1213 priv->delay_line = PHY_SDCLKDL_DC_DEFAULT; 1214 1215 if (device_property_read_bool(dev, "mmc-ddr-1_8v") || 1216 device_property_read_bool(dev, "mmc-hs200-1_8v") || 1217 device_property_read_bool(dev, "mmc-hs400-1_8v")) 1218 priv->flags |= FLAG_IO_FIXED_1V8; 1219 else 1220 priv->flags &= ~FLAG_IO_FIXED_1V8; 1221 1222 /* 1223 * start_signal_voltage_switch() will try 3.3V first 1224 * then 1.8V. Use SDHCI_SIGNALING_180 rather than 1225 * SDHCI_SIGNALING_330 to avoid setting voltage to 3.3V 1226 * in sdhci_start_signal_voltage_switch(). 1227 */ 1228 if (priv->flags & FLAG_IO_FIXED_1V8) { 1229 host->flags &= ~SDHCI_SIGNALING_330; 1230 host->flags |= SDHCI_SIGNALING_180; 1231 } 1232 1233 sdhci_enable_v4_mode(host); 1234 } 1235 1236 #ifdef CONFIG_ACPI 1237 if (pltfm_data == &sdhci_dwcmshc_bf3_pdata) 1238 sdhci_enable_v4_mode(host); 1239 #endif 1240 1241 caps = sdhci_readl(host, SDHCI_CAPABILITIES); 1242 if (caps & SDHCI_CAN_64BIT_V4) 1243 sdhci_enable_v4_mode(host); 1244 1245 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY; 1246 1247 pm_runtime_get_noresume(dev); 1248 pm_runtime_set_active(dev); 1249 pm_runtime_enable(dev); 1250 1251 err = sdhci_setup_host(host); 1252 if (err) 1253 goto err_rpm; 1254 1255 /* Setup Command Queue Engine if enabled */ 1256 if (device_property_read_bool(&pdev->dev, "supports-cqe")) { 1257 priv->vendor_specific_area2 = 1258 sdhci_readw(host, DWCMSHC_P_VENDOR_AREA2); 1259 1260 dwcmshc_cqhci_init(host, pdev); 1261 } 1262 1263 if (rk_priv) 1264 dwcmshc_rk35xx_postinit(host, priv); 1265 1266 err = __sdhci_add_host(host); 1267 if (err) 1268 goto err_setup_host; 1269 1270 pm_runtime_put(dev); 1271 1272 return 0; 1273 1274 err_setup_host: 1275 sdhci_cleanup_host(host); 1276 err_rpm: 1277 pm_runtime_disable(dev); 1278 pm_runtime_put_noidle(dev); 1279 err_clk: 1280 clk_disable_unprepare(pltfm_host->clk); 1281 clk_disable_unprepare(priv->bus_clk); 1282 if (rk_priv) 1283 clk_bulk_disable_unprepare(RK35xx_MAX_CLKS, 1284 rk_priv->rockchip_clks); 1285 free_pltfm: 1286 sdhci_pltfm_free(pdev); 1287 return err; 1288 } 1289 1290 static void dwcmshc_disable_card_clk(struct sdhci_host *host) 1291 { 1292 u16 ctrl; 1293 1294 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 1295 if (ctrl & SDHCI_CLOCK_CARD_EN) { 1296 ctrl &= ~SDHCI_CLOCK_CARD_EN; 1297 sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL); 1298 } 1299 } 1300 1301 static void dwcmshc_remove(struct platform_device *pdev) 1302 { 1303 struct sdhci_host *host = platform_get_drvdata(pdev); 1304 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1305 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 1306 struct rk35xx_priv *rk_priv = priv->priv; 1307 1308 pm_runtime_get_sync(&pdev->dev); 1309 pm_runtime_disable(&pdev->dev); 1310 pm_runtime_put_noidle(&pdev->dev); 1311 1312 sdhci_remove_host(host, 0); 1313 1314 dwcmshc_disable_card_clk(host); 1315 1316 clk_disable_unprepare(pltfm_host->clk); 1317 clk_disable_unprepare(priv->bus_clk); 1318 if (rk_priv) 1319 clk_bulk_disable_unprepare(RK35xx_MAX_CLKS, 1320 rk_priv->rockchip_clks); 1321 sdhci_pltfm_free(pdev); 1322 } 1323 1324 #ifdef CONFIG_PM_SLEEP 1325 static int dwcmshc_suspend(struct device *dev) 1326 { 1327 struct sdhci_host *host = dev_get_drvdata(dev); 1328 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1329 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 1330 struct rk35xx_priv *rk_priv = priv->priv; 1331 int ret; 1332 1333 pm_runtime_resume(dev); 1334 1335 if (host->mmc->caps2 & MMC_CAP2_CQE) { 1336 ret = cqhci_suspend(host->mmc); 1337 if (ret) 1338 return ret; 1339 } 1340 1341 ret = sdhci_suspend_host(host); 1342 if (ret) 1343 return ret; 1344 1345 clk_disable_unprepare(pltfm_host->clk); 1346 if (!IS_ERR(priv->bus_clk)) 1347 clk_disable_unprepare(priv->bus_clk); 1348 1349 if (rk_priv) 1350 clk_bulk_disable_unprepare(RK35xx_MAX_CLKS, 1351 rk_priv->rockchip_clks); 1352 1353 return ret; 1354 } 1355 1356 static int dwcmshc_resume(struct device *dev) 1357 { 1358 struct sdhci_host *host = dev_get_drvdata(dev); 1359 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1360 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); 1361 struct rk35xx_priv *rk_priv = priv->priv; 1362 int ret; 1363 1364 ret = clk_prepare_enable(pltfm_host->clk); 1365 if (ret) 1366 return ret; 1367 1368 if (!IS_ERR(priv->bus_clk)) { 1369 ret = clk_prepare_enable(priv->bus_clk); 1370 if (ret) 1371 goto disable_clk; 1372 } 1373 1374 if (rk_priv) { 1375 ret = clk_bulk_prepare_enable(RK35xx_MAX_CLKS, 1376 rk_priv->rockchip_clks); 1377 if (ret) 1378 goto disable_bus_clk; 1379 } 1380 1381 ret = sdhci_resume_host(host); 1382 if (ret) 1383 goto disable_rockchip_clks; 1384 1385 if (host->mmc->caps2 & MMC_CAP2_CQE) { 1386 ret = cqhci_resume(host->mmc); 1387 if (ret) 1388 goto disable_rockchip_clks; 1389 } 1390 1391 return 0; 1392 1393 disable_rockchip_clks: 1394 if (rk_priv) 1395 clk_bulk_disable_unprepare(RK35xx_MAX_CLKS, 1396 rk_priv->rockchip_clks); 1397 disable_bus_clk: 1398 if (!IS_ERR(priv->bus_clk)) 1399 clk_disable_unprepare(priv->bus_clk); 1400 disable_clk: 1401 clk_disable_unprepare(pltfm_host->clk); 1402 return ret; 1403 } 1404 #endif 1405 1406 #ifdef CONFIG_PM 1407 1408 static void dwcmshc_enable_card_clk(struct sdhci_host *host) 1409 { 1410 u16 ctrl; 1411 1412 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 1413 if ((ctrl & SDHCI_CLOCK_INT_EN) && !(ctrl & SDHCI_CLOCK_CARD_EN)) { 1414 ctrl |= SDHCI_CLOCK_CARD_EN; 1415 sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL); 1416 } 1417 } 1418 1419 static int dwcmshc_runtime_suspend(struct device *dev) 1420 { 1421 struct sdhci_host *host = dev_get_drvdata(dev); 1422 1423 dwcmshc_disable_card_clk(host); 1424 1425 return 0; 1426 } 1427 1428 static int dwcmshc_runtime_resume(struct device *dev) 1429 { 1430 struct sdhci_host *host = dev_get_drvdata(dev); 1431 1432 dwcmshc_enable_card_clk(host); 1433 1434 return 0; 1435 } 1436 1437 #endif 1438 1439 static const struct dev_pm_ops dwcmshc_pmops = { 1440 SET_SYSTEM_SLEEP_PM_OPS(dwcmshc_suspend, dwcmshc_resume) 1441 SET_RUNTIME_PM_OPS(dwcmshc_runtime_suspend, 1442 dwcmshc_runtime_resume, NULL) 1443 }; 1444 1445 static struct platform_driver sdhci_dwcmshc_driver = { 1446 .driver = { 1447 .name = "sdhci-dwcmshc", 1448 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1449 .of_match_table = sdhci_dwcmshc_dt_ids, 1450 .acpi_match_table = ACPI_PTR(sdhci_dwcmshc_acpi_ids), 1451 .pm = &dwcmshc_pmops, 1452 }, 1453 .probe = dwcmshc_probe, 1454 .remove_new = dwcmshc_remove, 1455 }; 1456 module_platform_driver(sdhci_dwcmshc_driver); 1457 1458 MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC"); 1459 MODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>"); 1460 MODULE_LICENSE("GPL v2"); 1461