| 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 <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/acpi.h> |
| 11 |
#include <linux/arm-smccc.h> |
| 12 |
#include <linux/bitfield.h> |
| 13 |
#include <linux/clk.h> |
| 14 |
#include <linux/clk-provider.h> |
| 15 |
#include <linux/dma-mapping.h> |
| 16 |
#include <linux/iopoll.h> |
| 17 |
#include <linux/kernel.h> |
| 18 |
#include <linux/module.h> |
| 19 |
#include <linux/of.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/pm_domain.h> |
| 22 |
#include <linux/pm_runtime.h> |
| 23 |
#include <linux/regmap.h> |
| 24 |
#include <linux/reset.h> |
| 25 |
#include <linux/sizes.h> |
| 26 |
#include <linux/mfd/syscon.h> |
| 27 |
#include <linux/units.h> |
| 28 |
|
| 29 |
#include "sdhci-pltfm.h" |
| 30 |
#include "cqhci.h" |
| 31 |
#include "sdhci-cqhci.h" |
| 32 |
|
| 33 |
#define SDHCI_DWCMSHC_ARG2_STUFF GENMASK(31, 16) |
| 34 |
|
| 35 |
/* DWCMSHC specific Mode Select value */ |
| 36 |
#define DWCMSHC_CTRL_HS400 0x7 |
| 37 |
|
| 38 |
/* DWC IP vendor area 1 pointer */ |
| 39 |
#define DWCMSHC_P_VENDOR_AREA1 0xe8 |
| 40 |
#define DWCMSHC_AREA1_MASK GENMASK(11, 0) |
| 41 |
/* Offset inside the vendor area 1 */ |
| 42 |
#define DWCMSHC_HOST_CTRL3 0x8 |
| 43 |
#define DWCMSHC_EMMC_CONTROL 0x2c |
| 44 |
#define DWCMSHC_CARD_IS_EMMC BIT(0) |
| 45 |
#define DWCMSHC_ENHANCED_STROBE BIT(8) |
| 46 |
#define DWCMSHC_EMMC_ATCTRL 0x40 |
| 47 |
#define DWCMSHC_AT_STAT 0x44 |
| 48 |
/* Tuning and auto-tuning fields in AT_CTRL_R control register */ |
| 49 |
#define AT_CTRL_AT_EN BIT(0) /* autotuning is enabled */ |
| 50 |
#define AT_CTRL_CI_SEL BIT(1) /* interval to drive center phase select */ |
| 51 |
#define AT_CTRL_SWIN_TH_EN BIT(2) /* sampling window threshold enable */ |
| 52 |
#define AT_CTRL_RPT_TUNE_ERR BIT(3) /* enable reporting framing errors */ |
| 53 |
#define AT_CTRL_SW_TUNE_EN BIT(4) /* enable software managed tuning */ |
| 54 |
#define AT_CTRL_WIN_EDGE_SEL_MASK GENMASK(11, 8) /* bits [11:8] */ |
| 55 |
#define AT_CTRL_WIN_EDGE_SEL 0xf /* sampling window edge select */ |
| 56 |
#define AT_CTRL_TUNE_CLK_STOP_EN BIT(16) /* clocks stopped during phase code change */ |
| 57 |
#define AT_CTRL_PRE_CHANGE_DLY_MASK GENMASK(18, 17) /* bits [18:17] */ |
| 58 |
#define AT_CTRL_PRE_CHANGE_DLY 0x1 /* 2-cycle latency */ |
| 59 |
#define AT_CTRL_POST_CHANGE_DLY_MASK GENMASK(20, 19) /* bits [20:19] */ |
| 60 |
#define AT_CTRL_POST_CHANGE_DLY 0x3 /* 4-cycle latency */ |
| 61 |
#define AT_CTRL_SWIN_TH_VAL_MASK GENMASK(31, 24) /* bits [31:24] */ |
| 62 |
#define AT_CTRL_SWIN_TH_VAL 0x9 /* sampling window threshold */ |
| 63 |
|
| 64 |
/* DWC IP vendor area 2 pointer */ |
| 65 |
#define DWCMSHC_P_VENDOR_AREA2 0xea |
| 66 |
|
| 67 |
/* Sophgo CV18XX specific Registers */ |
| 68 |
#define CV18XX_SDHCI_MSHC_CTRL 0x00 |
| 69 |
#define CV18XX_EMMC_FUNC_EN BIT(0) |
| 70 |
#define CV18XX_LATANCY_1T BIT(1) |
| 71 |
#define CV18XX_SDHCI_PHY_TX_RX_DLY 0x40 |
| 72 |
#define CV18XX_PHY_TX_DLY_MSK GENMASK(6, 0) |
| 73 |
#define CV18XX_PHY_TX_SRC_MSK GENMASK(9, 8) |
| 74 |
#define CV18XX_PHY_TX_SRC_INVERT_CLK_TX 0x1 |
| 75 |
#define CV18XX_PHY_RX_DLY_MSK GENMASK(22, 16) |
| 76 |
#define CV18XX_PHY_RX_SRC_MSK GENMASK(25, 24) |
| 77 |
#define CV18XX_PHY_RX_SRC_INVERT_RX_CLK 0x1 |
| 78 |
#define CV18XX_SDHCI_PHY_CONFIG 0x4c |
| 79 |
#define CV18XX_PHY_TX_BPS BIT(0) |
| 80 |
|
| 81 |
#define CV18XX_TUNE_MAX 128 |
| 82 |
#define CV18XX_TUNE_STEP 1 |
| 83 |
#define CV18XX_RETRY_TUNING_MAX 50 |
| 84 |
|
| 85 |
/* Rockchip specific Registers */ |
| 86 |
#define DWCMSHC_EMMC_DLL_CTRL 0x800 |
| 87 |
#define DWCMSHC_EMMC_DLL_RXCLK 0x804 |
| 88 |
#define DWCMSHC_EMMC_DLL_TXCLK 0x808 |
| 89 |
#define DWCMSHC_EMMC_DLL_STRBIN 0x80c |
| 90 |
#define DECMSHC_EMMC_DLL_CMDOUT 0x810 |
| 91 |
#define DECMSHC_EMMC_MISC_CON 0x81C |
| 92 |
#define MISC_INTCLK_EN BIT(1) |
| 93 |
#define DWCMSHC_EMMC_DLL_STATUS0 0x840 |
| 94 |
#define DWCMSHC_EMMC_DLL_START BIT(0) |
| 95 |
#define DWCMSHC_EMMC_DLL_LOCKED BIT(8) |
| 96 |
#define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9) |
| 97 |
#define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29 |
| 98 |
#define DWCMSHC_EMMC_DLL_START_POINT 16 |
| 99 |
#define DWCMSHC_EMMC_DLL_INC 8 |
| 100 |
#define DWCMSHC_EMMC_DLL_BYPASS BIT(24) |
| 101 |
#define DWCMSHC_EMMC_DLL_DLYENA BIT(27) |
| 102 |
#define DLL_TXCLK_TAPNUM_DEFAULT 0x10 |
| 103 |
#define DLL_TXCLK_TAPNUM_90_DEGREES 0xA |
| 104 |
#define DLL_TXCLK_TAPNUM_FROM_SW BIT(24) |
| 105 |
#define DLL_STRBIN_TAPNUM_DEFAULT 0x4 |
| 106 |
#define DLL_STRBIN_TAPNUM_FROM_SW BIT(24) |
| 107 |
#define DLL_STRBIN_DELAY_NUM_SEL BIT(26) |
| 108 |
#define DLL_STRBIN_DELAY_NUM_OFFSET 16 |
| 109 |
#define DLL_STRBIN_DELAY_NUM_DEFAULT 0x16 |
| 110 |
#define DLL_RXCLK_NO_INVERTER 1 |
| 111 |
#define DLL_RXCLK_INVERTER 0 |
| 112 |
#define DLL_CMDOUT_TAPNUM_90_DEGREES 0x8 |
| 113 |
#define DLL_RXCLK_ORI_GATE BIT(31) |
| 114 |
#define DLL_CMDOUT_TAPNUM_FROM_SW BIT(24) |
| 115 |
#define DLL_CMDOUT_SRC_CLK_NEG BIT(28) |
| 116 |
#define DLL_CMDOUT_EN_SRC_CLK_NEG BIT(29) |
| 117 |
|
| 118 |
#define DLL_LOCK_WO_TMOUT(x) \ |
| 119 |
((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \ |
| 120 |
(((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0)) |
| 121 |
|
| 122 |
/* PHY register area pointer */ |
| 123 |
#define DWC_MSHC_PTR_PHY_R 0x300 |
| 124 |
|
| 125 |
/* PHY general configuration */ |
| 126 |
#define PHY_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x00) |
| 127 |
#define PHY_CNFG_RSTN_DEASSERT 0x1 /* Deassert PHY reset */ |
| 128 |
#define PHY_CNFG_PHY_PWRGOOD_MASK BIT_MASK(1) /* bit [1] */ |
| 129 |
#define PHY_CNFG_PAD_SP_MASK GENMASK(19, 16) /* bits [19:16] */ |
| 130 |
#define PHY_CNFG_PAD_SP 0x0c /* PMOS TX drive strength */ |
| 131 |
#define PHY_CNFG_PAD_SP_SG2042 0x09 /* PMOS TX drive strength for SG2042 */ |
| 132 |
#define PHY_CNFG_PAD_SN_MASK GENMASK(23, 20) /* bits [23:20] */ |
| 133 |
#define PHY_CNFG_PAD_SN 0x0c /* NMOS TX drive strength */ |
| 134 |
#define PHY_CNFG_PAD_SN_SG2042 0x08 /* NMOS TX drive strength for SG2042 */ |
| 135 |
|
| 136 |
/* PHY command/response pad settings */ |
| 137 |
#define PHY_CMDPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x04) |
| 138 |
|
| 139 |
/* PHY data pad settings */ |
| 140 |
#define PHY_DATAPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x06) |
| 141 |
|
| 142 |
/* PHY clock pad settings */ |
| 143 |
#define PHY_CLKPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x08) |
| 144 |
|
| 145 |
/* PHY strobe pad settings */ |
| 146 |
#define PHY_STBPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0a) |
| 147 |
|
| 148 |
/* PHY reset pad settings */ |
| 149 |
#define PHY_RSTNPAD_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x0c) |
| 150 |
|
| 151 |
/* Bitfields are common for all pad settings */ |
| 152 |
#define PHY_PAD_RXSEL_1V8 0x1 /* Receiver type select for 1.8V */ |
| 153 |
#define PHY_PAD_RXSEL_3V3 0x2 /* Receiver type select for 3.3V */ |
| 154 |
|
| 155 |
#define PHY_PAD_WEAKPULL_MASK GENMASK(4, 3) /* bits [4:3] */ |
| 156 |
#define PHY_PAD_WEAKPULL_PULLUP 0x1 /* Weak pull up enabled */ |
| 157 |
#define PHY_PAD_WEAKPULL_PULLDOWN 0x2 /* Weak pull down enabled */ |
| 158 |
|
| 159 |
#define PHY_PAD_TXSLEW_CTRL_P_MASK GENMASK(8, 5) /* bits [8:5] */ |
| 160 |
#define PHY_PAD_TXSLEW_CTRL_P 0x3 /* Slew control for P-Type pad TX */ |
| 161 |
#define PHY_PAD_TXSLEW_CTRL_N_MASK GENMASK(12, 9) /* bits [12:9] */ |
| 162 |
#define PHY_PAD_TXSLEW_CTRL_N 0x3 /* Slew control for N-Type pad TX */ |
| 163 |
#define PHY_PAD_TXSLEW_CTRL_N_SG2042 0x2 /* Slew control for N-Type pad TX for SG2042 */ |
| 164 |
|
| 165 |
/* PHY CLK delay line settings */ |
| 166 |
#define PHY_SDCLKDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x1d) |
| 167 |
#define PHY_SDCLKDL_CNFG_EXTDLY_EN BIT(0) |
| 168 |
#define PHY_SDCLKDL_CNFG_UPDATE BIT(4) /* set before writing to SDCLKDL_DC */ |
| 169 |
|
| 170 |
/* PHY CLK delay line delay code */ |
| 171 |
#define PHY_SDCLKDL_DC_R (DWC_MSHC_PTR_PHY_R + 0x1e) |
| 172 |
#define PHY_SDCLKDL_DC_INITIAL 0x40 /* initial delay code */ |
| 173 |
#define PHY_SDCLKDL_DC_DEFAULT 0x32 /* default delay code */ |
| 174 |
#define PHY_SDCLKDL_DC_HS400 0x18 /* delay code for HS400 mode */ |
| 175 |
|
| 176 |
#define PHY_SMPLDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x20) |
| 177 |
#define PHY_SMPLDL_CNFG_BYPASS_EN BIT(1) |
| 178 |
|
| 179 |
/* PHY drift_cclk_rx delay line configuration setting */ |
| 180 |
#define PHY_ATDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x21) |
| 181 |
#define PHY_ATDL_CNFG_INPSEL_MASK GENMASK(3, 2) /* bits [3:2] */ |
| 182 |
#define PHY_ATDL_CNFG_INPSEL 0x3 /* delay line input source */ |
| 183 |
#define PHY_ATDL_CNFG_INPSEL_SG2042 0x2 /* delay line input source for SG2042 */ |
| 184 |
|
| 185 |
/* PHY DLL control settings */ |
| 186 |
#define PHY_DLL_CTRL_R (DWC_MSHC_PTR_PHY_R + 0x24) |
| 187 |
#define PHY_DLL_CTRL_DISABLE 0x0 /* PHY DLL is enabled */ |
| 188 |
#define PHY_DLL_CTRL_ENABLE 0x1 /* PHY DLL is disabled */ |
| 189 |
|
| 190 |
/* PHY DLL configuration register 1 */ |
| 191 |
#define PHY_DLL_CNFG1_R (DWC_MSHC_PTR_PHY_R + 0x25) |
| 192 |
#define PHY_DLL_CNFG1_SLVDLY_MASK GENMASK(5, 4) /* bits [5:4] */ |
| 193 |
#define PHY_DLL_CNFG1_SLVDLY 0x2 /* DLL slave update delay input */ |
| 194 |
#define PHY_DLL_CNFG1_WAITCYCLE 0x5 /* DLL wait cycle input */ |
| 195 |
|
| 196 |
/* PHY DLL configuration register 2 */ |
| 197 |
#define PHY_DLL_CNFG2_R (DWC_MSHC_PTR_PHY_R + 0x26) |
| 198 |
#define PHY_DLL_CNFG2_JUMPSTEP 0xa /* DLL jump step input */ |
| 199 |
|
| 200 |
/* PHY DLL master and slave delay line configuration settings */ |
| 201 |
#define PHY_DLLDL_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x28) |
| 202 |
#define PHY_DLLDL_CNFG_SLV_INPSEL_MASK GENMASK(6, 5) /* bits [6:5] */ |
| 203 |
#define PHY_DLLDL_CNFG_SLV_INPSEL 0x3 /* clock source select for slave DL */ |
| 204 |
|
| 205 |
/* PHY DLL offset setting register */ |
| 206 |
#define PHY_DLL_OFFST_R (DWC_MSHC_PTR_PHY_R + 0x29) |
| 207 |
/* DLL LBT setting register */ |
| 208 |
#define PHY_DLLBT_CNFG_R (DWC_MSHC_PTR_PHY_R + 0x2c) |
| 209 |
/* DLL Status register */ |
| 210 |
#define PHY_DLL_STATUS_R (DWC_MSHC_PTR_PHY_R + 0x2e) |
| 211 |
#define DLL_LOCK_STS BIT(0)/* DLL is locked and ready */ |
| 212 |
/* |
| 213 |
* Captures the value of DLL's lock error status information. Value is valid |
| 214 |
* only when LOCK_STS is set. |
| 215 |
*/ |
| 216 |
#define DLL_ERROR_STS BIT(1) |
| 217 |
|
| 218 |
#define FLAG_IO_FIXED_1V8 BIT(0) |
| 219 |
|
| 220 |
#define BOUNDARY_OK(addr, len) \ |
| 221 |
((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1))) |
| 222 |
|
| 223 |
#define DWCMSHC_SDHCI_CQE_TRNS_MODE (SDHCI_TRNS_MULTI | \ |
| 224 |
SDHCI_TRNS_BLK_CNT_EN | \ |
| 225 |
SDHCI_TRNS_DMA) |
| 226 |
|
| 227 |
/* SMC call for BlueField-3 eMMC RST_N */ |
| 228 |
#define BLUEFIELD_SMC_SET_EMMC_RST_N 0x82000007 |
| 229 |
|
| 230 |
/* Eswin specific Registers */ |
| 231 |
#define EIC7700_CARD_CLK_STABLE BIT(28) |
| 232 |
#define EIC7700_INT_BCLK_STABLE BIT(16) |
| 233 |
#define EIC7700_INT_ACLK_STABLE BIT(8) |
| 234 |
#define EIC7700_INT_TMCLK_STABLE BIT(0) |
| 235 |
#define EIC7700_INT_CLK_STABLE (EIC7700_CARD_CLK_STABLE | \ |
| 236 |
EIC7700_INT_ACLK_STABLE | \ |
| 237 |
EIC7700_INT_BCLK_STABLE | \ |
| 238 |
EIC7700_INT_TMCLK_STABLE) |
| 239 |
#define EIC7700_HOST_VAL_STABLE BIT(0) |
| 240 |
|
| 241 |
/* strength definition */ |
| 242 |
#define PHYCTRL_DR_33OHM 0xee |
| 243 |
#define PHYCTRL_DR_40OHM 0xcc |
| 244 |
#define PHYCTRL_DR_50OHM 0x88 |
| 245 |
#define PHYCTRL_DR_66OHM 0x44 |
| 246 |
#define PHYCTRL_DR_100OHM 0x00 |
| 247 |
|
| 248 |
#define MAX_PHASE_CODE 0xff |
| 249 |
#define TUNING_RANGE_THRESHOLD 40 |
| 250 |
#define PHY_CLK_MAX_DELAY_MASK 0x7f |
| 251 |
#define PHY_DELAY_CODE_MAX 0x7f |
| 252 |
#define PHY_DELAY_CODE_EMMC 0x17 |
| 253 |
#define PHY_DELAY_CODE_SD 0x55 |
| 254 |
|
| 255 |
enum dwcmshc_rk_type { |
| 256 |
DWCMSHC_RK3568, |
| 257 |
DWCMSHC_RK3588, |
| 258 |
}; |
| 259 |
|
| 260 |
struct rk35xx_priv { |
| 261 |
struct reset_control *reset; |
| 262 |
enum dwcmshc_rk_type devtype; |
| 263 |
u8 txclk_tapnum; |
| 264 |
}; |
| 265 |
|
| 266 |
struct eic7700_priv { |
| 267 |
struct reset_control *reset; |
| 268 |
unsigned int drive_impedance; |
| 269 |
}; |
| 270 |
|
| 271 |
#define DWCMSHC_MAX_OTHER_CLKS 3 |
| 272 |
|
| 273 |
struct dwcmshc_priv { |
| 274 |
struct clk *bus_clk; |
| 275 |
int vendor_specific_area1; /* P_VENDOR_SPECIFIC_AREA1 reg */ |
| 276 |
int vendor_specific_area2; /* P_VENDOR_SPECIFIC_AREA2 reg */ |
| 277 |
|
| 278 |
int num_other_clks; |
| 279 |
struct clk_bulk_data other_clks[DWCMSHC_MAX_OTHER_CLKS]; |
| 280 |
|
| 281 |
void *priv; /* pointer to SoC private stuff */ |
| 282 |
u16 delay_line; |
| 283 |
u16 flags; |
| 284 |
}; |
| 285 |
|
| 286 |
struct dwcmshc_pltfm_data { |
| 287 |
const struct sdhci_pltfm_data pdata; |
| 288 |
const struct cqhci_host_ops *cqhci_host_ops; |
| 289 |
int (*init)(struct device *dev, struct sdhci_host *host, struct dwcmshc_priv *dwc_priv); |
| 290 |
void (*postinit)(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv); |
| 291 |
}; |
| 292 |
|
| 293 |
static void dwcmshc_enable_card_clk(struct sdhci_host *host) |
| 294 |
{ |
| 295 |
u16 ctrl; |
| 296 |
|
| 297 |
ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 298 |
if ((ctrl & SDHCI_CLOCK_INT_EN) && !(ctrl & SDHCI_CLOCK_CARD_EN)) { |
| 299 |
ctrl |= SDHCI_CLOCK_CARD_EN; |
| 300 |
sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
static int dwcmshc_get_enable_other_clks(struct device *dev, |
| 305 |
struct dwcmshc_priv *priv, |
| 306 |
int num_clks, |
| 307 |
const char * const clk_ids[]) |
| 308 |
{ |
| 309 |
int err; |
| 310 |
|
| 311 |
if (num_clks > DWCMSHC_MAX_OTHER_CLKS) |
| 312 |
return -EINVAL; |
| 313 |
|
| 314 |
for (int i = 0; i < num_clks; i++) |
| 315 |
priv->other_clks[i].id = clk_ids[i]; |
| 316 |
|
| 317 |
err = devm_clk_bulk_get_optional(dev, num_clks, priv->other_clks); |
| 318 |
if (err) { |
| 319 |
dev_err(dev, "failed to get clocks %d\n", err); |
| 320 |
return err; |
| 321 |
} |
| 322 |
|
| 323 |
err = clk_bulk_prepare_enable(num_clks, priv->other_clks); |
| 324 |
if (err) |
| 325 |
dev_err(dev, "failed to enable clocks %d\n", err); |
| 326 |
|
| 327 |
priv->num_other_clks = num_clks; |
| 328 |
|
| 329 |
return err; |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
* If DMA addr spans 128MB boundary, we split the DMA transfer into two |
| 334 |
* so that each DMA transfer doesn't exceed the boundary. |
| 335 |
*/ |
| 336 |
static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc, |
| 337 |
dma_addr_t addr, int len, unsigned int cmd) |
| 338 |
{ |
| 339 |
int tmplen, offset; |
| 340 |
|
| 341 |
if (likely(!len || BOUNDARY_OK(addr, len))) { |
| 342 |
sdhci_adma_write_desc(host, desc, addr, len, cmd); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
offset = addr & (SZ_128M - 1); |
| 347 |
tmplen = SZ_128M - offset; |
| 348 |
sdhci_adma_write_desc(host, desc, addr, tmplen, cmd); |
| 349 |
|
| 350 |
addr += tmplen; |
| 351 |
len -= tmplen; |
| 352 |
sdhci_adma_write_desc(host, desc, addr, len, cmd); |
| 353 |
} |
| 354 |
|
| 355 |
static void dwcmshc_reset(struct sdhci_host *host, u8 mask) |
| 356 |
{ |
| 357 |
sdhci_reset(host, mask); |
| 358 |
|
| 359 |
/* The dwcmshc does not comply with the SDHCI specification |
| 360 |
* regarding the "Software Reset for CMD line should clear 'Command |
| 361 |
* Complete' in the Normal Interrupt Status Register." Clear the bit |
| 362 |
* here to compensate for this quirk. |
| 363 |
*/ |
| 364 |
if (mask & SDHCI_RESET_CMD) |
| 365 |
sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS); |
| 366 |
} |
| 367 |
|
| 368 |
static unsigned int dwcmshc_get_max_clock(struct sdhci_host *host) |
| 369 |
{ |
| 370 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 371 |
|
| 372 |
if (pltfm_host->clk) |
| 373 |
return sdhci_pltfm_clk_get_max_clock(host); |
| 374 |
else |
| 375 |
return pltfm_host->clock; |
| 376 |
} |
| 377 |
|
| 378 |
static unsigned int rk35xx_get_max_clock(struct sdhci_host *host) |
| 379 |
{ |
| 380 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 381 |
|
| 382 |
return clk_round_rate(pltfm_host->clk, ULONG_MAX); |
| 383 |
} |
| 384 |
|
| 385 |
static void dwcmshc_check_auto_cmd23(struct mmc_host *mmc, |
| 386 |
struct mmc_request *mrq) |
| 387 |
{ |
| 388 |
struct sdhci_host *host = mmc_priv(mmc); |
| 389 |
|
| 390 |
/* |
| 391 |
* No matter V4 is enabled or not, ARGUMENT2 register is 32-bit |
| 392 |
* block count register which doesn't support stuff bits of |
| 393 |
* CMD23 argument on dwcmsch host controller. |
| 394 |
*/ |
| 395 |
if (mrq->sbc && (mrq->sbc->arg & SDHCI_DWCMSHC_ARG2_STUFF)) |
| 396 |
host->flags &= ~SDHCI_AUTO_CMD23; |
| 397 |
else |
| 398 |
host->flags |= SDHCI_AUTO_CMD23; |
| 399 |
} |
| 400 |
|
| 401 |
static void dwcmshc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 402 |
{ |
| 403 |
dwcmshc_check_auto_cmd23(mmc, mrq); |
| 404 |
|
| 405 |
sdhci_request(mmc, mrq); |
| 406 |
} |
| 407 |
|
| 408 |
static void dwcmshc_phy_init(struct sdhci_host *host) |
| 409 |
{ |
| 410 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 411 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 412 |
u32 rxsel = PHY_PAD_RXSEL_3V3; |
| 413 |
u32 val; |
| 414 |
|
| 415 |
if (priv->flags & FLAG_IO_FIXED_1V8 || |
| 416 |
host->mmc->ios.timing & MMC_SIGNAL_VOLTAGE_180) |
| 417 |
rxsel = PHY_PAD_RXSEL_1V8; |
| 418 |
|
| 419 |
/* deassert phy reset & set tx drive strength */ |
| 420 |
val = PHY_CNFG_RSTN_DEASSERT; |
| 421 |
val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP); |
| 422 |
val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN); |
| 423 |
sdhci_writel(host, val, PHY_CNFG_R); |
| 424 |
|
| 425 |
/* disable delay line */ |
| 426 |
sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R); |
| 427 |
|
| 428 |
/* set delay line */ |
| 429 |
sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R); |
| 430 |
sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R); |
| 431 |
|
| 432 |
/* enable delay lane */ |
| 433 |
val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R); |
| 434 |
val &= ~(PHY_SDCLKDL_CNFG_UPDATE); |
| 435 |
sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R); |
| 436 |
|
| 437 |
/* configure phy pads */ |
| 438 |
val = rxsel; |
| 439 |
val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP); |
| 440 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 441 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); |
| 442 |
sdhci_writew(host, val, PHY_CMDPAD_CNFG_R); |
| 443 |
sdhci_writew(host, val, PHY_DATAPAD_CNFG_R); |
| 444 |
sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R); |
| 445 |
|
| 446 |
val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 447 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); |
| 448 |
sdhci_writew(host, val, PHY_CLKPAD_CNFG_R); |
| 449 |
|
| 450 |
val = rxsel; |
| 451 |
val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN); |
| 452 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 453 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N); |
| 454 |
sdhci_writew(host, val, PHY_STBPAD_CNFG_R); |
| 455 |
|
| 456 |
/* enable data strobe mode */ |
| 457 |
if (rxsel == PHY_PAD_RXSEL_1V8) { |
| 458 |
u8 sel = FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, PHY_DLLDL_CNFG_SLV_INPSEL); |
| 459 |
|
| 460 |
sdhci_writeb(host, sel, PHY_DLLDL_CNFG_R); |
| 461 |
} |
| 462 |
|
| 463 |
/* enable phy dll */ |
| 464 |
sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R); |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
static void th1520_sdhci_set_phy(struct sdhci_host *host) |
| 469 |
{ |
| 470 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 471 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 472 |
u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; |
| 473 |
u16 emmc_ctrl; |
| 474 |
|
| 475 |
dwcmshc_phy_init(host); |
| 476 |
|
| 477 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { |
| 478 |
emmc_ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 479 |
emmc_ctrl |= DWCMSHC_CARD_IS_EMMC; |
| 480 |
sdhci_writew(host, emmc_ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 481 |
} |
| 482 |
|
| 483 |
sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) | |
| 484 |
PHY_DLL_CNFG1_WAITCYCLE, PHY_DLL_CNFG1_R); |
| 485 |
} |
| 486 |
|
| 487 |
static void dwcmshc_set_uhs_signaling(struct sdhci_host *host, |
| 488 |
unsigned int timing) |
| 489 |
{ |
| 490 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 491 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 492 |
u16 ctrl, ctrl_2; |
| 493 |
|
| 494 |
ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 495 |
/* Select Bus Speed Mode for host */ |
| 496 |
ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
| 497 |
if ((timing == MMC_TIMING_MMC_HS200) || |
| 498 |
(timing == MMC_TIMING_UHS_SDR104)) |
| 499 |
ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 500 |
else if (timing == MMC_TIMING_UHS_SDR12) |
| 501 |
ctrl_2 |= SDHCI_CTRL_UHS_SDR12; |
| 502 |
else if ((timing == MMC_TIMING_UHS_SDR25) || |
| 503 |
(timing == MMC_TIMING_MMC_HS)) |
| 504 |
ctrl_2 |= SDHCI_CTRL_UHS_SDR25; |
| 505 |
else if (timing == MMC_TIMING_UHS_SDR50) |
| 506 |
ctrl_2 |= SDHCI_CTRL_UHS_SDR50; |
| 507 |
else if ((timing == MMC_TIMING_UHS_DDR50) || |
| 508 |
(timing == MMC_TIMING_MMC_DDR52)) |
| 509 |
ctrl_2 |= SDHCI_CTRL_UHS_DDR50; |
| 510 |
else if (timing == MMC_TIMING_MMC_HS400) { |
| 511 |
/* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */ |
| 512 |
ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 513 |
ctrl |= DWCMSHC_CARD_IS_EMMC; |
| 514 |
sdhci_writew(host, ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 515 |
|
| 516 |
ctrl_2 |= DWCMSHC_CTRL_HS400; |
| 517 |
} |
| 518 |
|
| 519 |
if (priv->flags & FLAG_IO_FIXED_1V8) |
| 520 |
ctrl_2 |= SDHCI_CTRL_VDD_180; |
| 521 |
sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 522 |
} |
| 523 |
|
| 524 |
static void th1520_set_uhs_signaling(struct sdhci_host *host, |
| 525 |
unsigned int timing) |
| 526 |
{ |
| 527 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 528 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 529 |
|
| 530 |
dwcmshc_set_uhs_signaling(host, timing); |
| 531 |
if (timing == MMC_TIMING_MMC_HS400) |
| 532 |
priv->delay_line = PHY_SDCLKDL_DC_HS400; |
| 533 |
else |
| 534 |
sdhci_writeb(host, 0, PHY_DLLDL_CNFG_R); |
| 535 |
th1520_sdhci_set_phy(host); |
| 536 |
} |
| 537 |
|
| 538 |
static void dwcmshc_hs400_enhanced_strobe(struct mmc_host *mmc, |
| 539 |
struct mmc_ios *ios) |
| 540 |
{ |
| 541 |
u32 vendor; |
| 542 |
struct sdhci_host *host = mmc_priv(mmc); |
| 543 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 544 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 545 |
int reg = priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL; |
| 546 |
|
| 547 |
vendor = sdhci_readl(host, reg); |
| 548 |
if (ios->enhanced_strobe) |
| 549 |
vendor |= DWCMSHC_ENHANCED_STROBE; |
| 550 |
else |
| 551 |
vendor &= ~DWCMSHC_ENHANCED_STROBE; |
| 552 |
|
| 553 |
sdhci_writel(host, vendor, reg); |
| 554 |
} |
| 555 |
|
| 556 |
static int dwcmshc_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 557 |
{ |
| 558 |
int err = sdhci_execute_tuning(mmc, opcode); |
| 559 |
struct sdhci_host *host = mmc_priv(mmc); |
| 560 |
|
| 561 |
if (err) |
| 562 |
return err; |
| 563 |
|
| 564 |
/* |
| 565 |
* Tuning can leave the IP in an active state (Buffer Read Enable bit |
| 566 |
* set) which prevents the entry to low power states (i.e. S0i3). Data |
| 567 |
* reset will clear it. |
| 568 |
*/ |
| 569 |
sdhci_reset(host, SDHCI_RESET_DATA); |
| 570 |
|
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
static u32 dwcmshc_cqe_irq_handler(struct sdhci_host *host, u32 intmask) |
| 575 |
{ |
| 576 |
int cmd_error = 0; |
| 577 |
int data_error = 0; |
| 578 |
|
| 579 |
if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error)) |
| 580 |
return intmask; |
| 581 |
|
| 582 |
cqhci_irq(host->mmc, intmask, cmd_error, data_error); |
| 583 |
|
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
static void dwcmshc_sdhci_cqe_enable(struct mmc_host *mmc) |
| 588 |
{ |
| 589 |
struct sdhci_host *host = mmc_priv(mmc); |
| 590 |
u8 ctrl; |
| 591 |
|
| 592 |
sdhci_writew(host, DWCMSHC_SDHCI_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE); |
| 593 |
|
| 594 |
sdhci_cqe_enable(mmc); |
| 595 |
|
| 596 |
/* |
| 597 |
* The "DesignWare Cores Mobile Storage Host Controller |
| 598 |
* DWC_mshc / DWC_mshc_lite Databook" says: |
| 599 |
* when Host Version 4 Enable" is 1 in Host Control 2 register, |
| 600 |
* SDHCI_CTRL_ADMA32 bit means ADMA2 is selected. |
| 601 |
* Selection of 32-bit/64-bit System Addressing: |
| 602 |
* either 32-bit or 64-bit system addressing is selected by |
| 603 |
* 64-bit Addressing bit in Host Control 2 register. |
| 604 |
* |
| 605 |
* On the other hand the "DesignWare Cores Mobile Storage Host |
| 606 |
* Controller DWC_mshc / DWC_mshc_lite User Guide" says, that we have to |
| 607 |
* set DMA_SEL to ADMA2 _only_ mode in the Host Control 2 register. |
| 608 |
*/ |
| 609 |
ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 610 |
ctrl &= ~SDHCI_CTRL_DMA_MASK; |
| 611 |
ctrl |= SDHCI_CTRL_ADMA32; |
| 612 |
sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 613 |
} |
| 614 |
|
| 615 |
static void dwcmshc_set_tran_desc(struct cqhci_host *cq_host, u8 **desc, |
| 616 |
dma_addr_t addr, int len, bool end, bool dma64) |
| 617 |
{ |
| 618 |
int tmplen, offset; |
| 619 |
|
| 620 |
if (likely(!len || BOUNDARY_OK(addr, len))) { |
| 621 |
cqhci_set_tran_desc(*desc, addr, len, end, dma64); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
offset = addr & (SZ_128M - 1); |
| 626 |
tmplen = SZ_128M - offset; |
| 627 |
cqhci_set_tran_desc(*desc, addr, tmplen, false, dma64); |
| 628 |
|
| 629 |
addr += tmplen; |
| 630 |
len -= tmplen; |
| 631 |
*desc += cq_host->trans_desc_len; |
| 632 |
cqhci_set_tran_desc(*desc, addr, len, end, dma64); |
| 633 |
} |
| 634 |
|
| 635 |
static void dwcmshc_cqhci_dumpregs(struct mmc_host *mmc) |
| 636 |
{ |
| 637 |
sdhci_dumpregs(mmc_priv(mmc)); |
| 638 |
} |
| 639 |
|
| 640 |
static void rk35xx_sdhci_cqe_pre_enable(struct mmc_host *mmc) |
| 641 |
{ |
| 642 |
struct sdhci_host *host = mmc_priv(mmc); |
| 643 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 644 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 645 |
u32 reg; |
| 646 |
|
| 647 |
/* Set Send Status Command Idle Timer to 10.66us (256 * 1 / 24) */ |
| 648 |
reg = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_SSC1); |
| 649 |
reg = (reg & ~CQHCI_SSC1_CIT_MASK) | 0x0100; |
| 650 |
sdhci_writel(host, reg, dwc_priv->vendor_specific_area2 + CQHCI_SSC1); |
| 651 |
|
| 652 |
reg = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_CFG); |
| 653 |
reg |= CQHCI_ENABLE; |
| 654 |
sdhci_writel(host, reg, dwc_priv->vendor_specific_area2 + CQHCI_CFG); |
| 655 |
} |
| 656 |
|
| 657 |
static void rk35xx_sdhci_cqe_enable(struct mmc_host *mmc) |
| 658 |
{ |
| 659 |
struct sdhci_host *host = mmc_priv(mmc); |
| 660 |
u32 reg; |
| 661 |
|
| 662 |
reg = sdhci_readl(host, SDHCI_PRESENT_STATE); |
| 663 |
while (reg & SDHCI_DATA_AVAILABLE) { |
| 664 |
sdhci_readl(host, SDHCI_BUFFER); |
| 665 |
reg = sdhci_readl(host, SDHCI_PRESENT_STATE); |
| 666 |
} |
| 667 |
|
| 668 |
sdhci_writew(host, DWCMSHC_SDHCI_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE); |
| 669 |
|
| 670 |
sdhci_cqe_enable(mmc); |
| 671 |
} |
| 672 |
|
| 673 |
static void rk35xx_sdhci_cqe_disable(struct mmc_host *mmc, bool recovery) |
| 674 |
{ |
| 675 |
struct sdhci_host *host = mmc_priv(mmc); |
| 676 |
unsigned long flags; |
| 677 |
u32 ctrl; |
| 678 |
|
| 679 |
/* |
| 680 |
* During CQE command transfers, command complete bit gets latched. |
| 681 |
* So s/w should clear command complete interrupt status when CQE is |
| 682 |
* either halted or disabled. Otherwise unexpected SDCHI legacy |
| 683 |
* interrupt gets triggered when CQE is halted/disabled. |
| 684 |
*/ |
| 685 |
spin_lock_irqsave(&host->lock, flags); |
| 686 |
ctrl = sdhci_readl(host, SDHCI_INT_ENABLE); |
| 687 |
ctrl |= SDHCI_INT_RESPONSE; |
| 688 |
sdhci_writel(host, ctrl, SDHCI_INT_ENABLE); |
| 689 |
sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS); |
| 690 |
spin_unlock_irqrestore(&host->lock, flags); |
| 691 |
|
| 692 |
sdhci_cqe_disable(mmc, recovery); |
| 693 |
} |
| 694 |
|
| 695 |
static void rk35xx_sdhci_cqe_post_disable(struct mmc_host *mmc) |
| 696 |
{ |
| 697 |
struct sdhci_host *host = mmc_priv(mmc); |
| 698 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 699 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 700 |
u32 ctrl; |
| 701 |
|
| 702 |
ctrl = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_CFG); |
| 703 |
ctrl &= ~CQHCI_ENABLE; |
| 704 |
sdhci_writel(host, ctrl, dwc_priv->vendor_specific_area2 + CQHCI_CFG); |
| 705 |
} |
| 706 |
|
| 707 |
static void dwcmshc_rk3568_set_clock(struct sdhci_host *host, unsigned int clock) |
| 708 |
{ |
| 709 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 710 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 711 |
struct rk35xx_priv *priv = dwc_priv->priv; |
| 712 |
u8 txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT; |
| 713 |
u32 extra, reg; |
| 714 |
int err; |
| 715 |
|
| 716 |
host->mmc->actual_clock = 0; |
| 717 |
|
| 718 |
if (clock == 0) { |
| 719 |
/* Disable interface clock at initial state. */ |
| 720 |
sdhci_set_clock(host, clock); |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
/* Rockchip platform only support 375KHz for identify mode */ |
| 725 |
if (clock <= 400000) |
| 726 |
clock = 375000; |
| 727 |
|
| 728 |
err = clk_set_rate(pltfm_host->clk, clock); |
| 729 |
if (err) |
| 730 |
dev_err(mmc_dev(host->mmc), "fail to set clock %d", clock); |
| 731 |
|
| 732 |
sdhci_set_clock(host, clock); |
| 733 |
|
| 734 |
/* Disable cmd conflict check and internal clock gate */ |
| 735 |
reg = dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3; |
| 736 |
extra = sdhci_readl(host, reg); |
| 737 |
extra &= ~BIT(0); |
| 738 |
extra |= BIT(4); |
| 739 |
sdhci_writel(host, extra, reg); |
| 740 |
|
| 741 |
/* Disable clock while config DLL */ |
| 742 |
sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
| 743 |
|
| 744 |
if (clock <= 52000000) { |
| 745 |
if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 746 |
host->mmc->ios.timing == MMC_TIMING_MMC_HS400) { |
| 747 |
dev_err(mmc_dev(host->mmc), |
| 748 |
"Can't reduce the clock below 52MHz in HS200/HS400 mode"); |
| 749 |
goto enable_clk; |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
* Disable DLL and reset both of sample and drive clock. |
| 754 |
* The bypass bit and start bit need to be set if DLL is not locked. |
| 755 |
*/ |
| 756 |
sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START, DWCMSHC_EMMC_DLL_CTRL); |
| 757 |
sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK); |
| 758 |
sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK); |
| 759 |
sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT); |
| 760 |
/* |
| 761 |
* Before switching to hs400es mode, the driver will enable |
| 762 |
* enhanced strobe first. PHY needs to configure the parameters |
| 763 |
* of enhanced strobe first. |
| 764 |
*/ |
| 765 |
extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 766 |
DLL_STRBIN_DELAY_NUM_SEL | |
| 767 |
DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET; |
| 768 |
sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); |
| 769 |
goto enable_clk; |
| 770 |
} |
| 771 |
|
| 772 |
/* Reset DLL */ |
| 773 |
sdhci_writel(host, BIT(1), DWCMSHC_EMMC_DLL_CTRL); |
| 774 |
udelay(1); |
| 775 |
sdhci_writel(host, 0x0, DWCMSHC_EMMC_DLL_CTRL); |
| 776 |
|
| 777 |
/* |
| 778 |
* We shouldn't set DLL_RXCLK_NO_INVERTER for identify mode but |
| 779 |
* we must set it in higher speed mode. |
| 780 |
*/ |
| 781 |
extra = DWCMSHC_EMMC_DLL_DLYENA; |
| 782 |
if (priv->devtype == DWCMSHC_RK3568) |
| 783 |
extra |= DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL; |
| 784 |
sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK); |
| 785 |
|
| 786 |
/* Init DLL settings */ |
| 787 |
extra = 0x5 << DWCMSHC_EMMC_DLL_START_POINT | |
| 788 |
0x2 << DWCMSHC_EMMC_DLL_INC | |
| 789 |
DWCMSHC_EMMC_DLL_START; |
| 790 |
sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL); |
| 791 |
err = readl_poll_timeout(host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0, |
| 792 |
extra, DLL_LOCK_WO_TMOUT(extra), 1, |
| 793 |
500 * USEC_PER_MSEC); |
| 794 |
if (err) { |
| 795 |
dev_err(mmc_dev(host->mmc), "DLL lock timeout!\n"); |
| 796 |
goto enable_clk; |
| 797 |
} |
| 798 |
|
| 799 |
extra = 0x1 << 16 | /* tune clock stop en */ |
| 800 |
0x3 << 17 | /* pre-change delay */ |
| 801 |
0x3 << 19; /* post-change delay */ |
| 802 |
sdhci_writel(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 803 |
|
| 804 |
if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 805 |
host->mmc->ios.timing == MMC_TIMING_MMC_HS400) |
| 806 |
txclk_tapnum = priv->txclk_tapnum; |
| 807 |
|
| 808 |
if ((priv->devtype == DWCMSHC_RK3588) && host->mmc->ios.timing == MMC_TIMING_MMC_HS400) { |
| 809 |
txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES; |
| 810 |
|
| 811 |
extra = DLL_CMDOUT_SRC_CLK_NEG | |
| 812 |
DLL_CMDOUT_EN_SRC_CLK_NEG | |
| 813 |
DWCMSHC_EMMC_DLL_DLYENA | |
| 814 |
DLL_CMDOUT_TAPNUM_90_DEGREES | |
| 815 |
DLL_CMDOUT_TAPNUM_FROM_SW; |
| 816 |
sdhci_writel(host, extra, DECMSHC_EMMC_DLL_CMDOUT); |
| 817 |
} |
| 818 |
|
| 819 |
extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 820 |
DLL_TXCLK_TAPNUM_FROM_SW | |
| 821 |
DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL | |
| 822 |
txclk_tapnum; |
| 823 |
sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK); |
| 824 |
|
| 825 |
extra = DWCMSHC_EMMC_DLL_DLYENA | |
| 826 |
DLL_STRBIN_TAPNUM_DEFAULT | |
| 827 |
DLL_STRBIN_TAPNUM_FROM_SW; |
| 828 |
sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN); |
| 829 |
|
| 830 |
enable_clk: |
| 831 |
/* |
| 832 |
* The sdclk frequency select bits in SDHCI_CLOCK_CONTROL are not functional |
| 833 |
* on Rockchip's SDHCI implementation. Instead, the clock frequency is fully |
| 834 |
* controlled via external clk provider by calling clk_set_rate(). Consequently, |
| 835 |
* passing 0 to sdhci_enable_clk() only re-enables the already-configured clock, |
| 836 |
* which matches the hardware's actual behavior. |
| 837 |
*/ |
| 838 |
sdhci_enable_clk(host, 0); |
| 839 |
} |
| 840 |
|
| 841 |
static void rk35xx_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 842 |
{ |
| 843 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 844 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 845 |
struct rk35xx_priv *priv = dwc_priv->priv; |
| 846 |
u32 extra = sdhci_readl(host, DECMSHC_EMMC_MISC_CON); |
| 847 |
|
| 848 |
if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL)) |
| 849 |
cqhci_deactivate(host->mmc); |
| 850 |
|
| 851 |
if (mask & SDHCI_RESET_ALL && priv->reset) { |
| 852 |
reset_control_assert(priv->reset); |
| 853 |
udelay(1); |
| 854 |
reset_control_deassert(priv->reset); |
| 855 |
} |
| 856 |
|
| 857 |
sdhci_reset(host, mask); |
| 858 |
|
| 859 |
/* Enable INTERNAL CLOCK */ |
| 860 |
sdhci_writel(host, MISC_INTCLK_EN | extra, DECMSHC_EMMC_MISC_CON); |
| 861 |
} |
| 862 |
|
| 863 |
static int dwcmshc_rk35xx_init(struct device *dev, struct sdhci_host *host, |
| 864 |
struct dwcmshc_priv *dwc_priv) |
| 865 |
{ |
| 866 |
static const char * const clk_ids[] = {"axi", "block", "timer"}; |
| 867 |
struct rk35xx_priv *priv; |
| 868 |
int err; |
| 869 |
|
| 870 |
priv = devm_kzalloc(dev, sizeof(struct rk35xx_priv), GFP_KERNEL); |
| 871 |
if (!priv) |
| 872 |
return -ENOMEM; |
| 873 |
|
| 874 |
if (of_device_is_compatible(dev->of_node, "rockchip,rk3588-dwcmshc")) |
| 875 |
priv->devtype = DWCMSHC_RK3588; |
| 876 |
else |
| 877 |
priv->devtype = DWCMSHC_RK3568; |
| 878 |
|
| 879 |
priv->reset = devm_reset_control_array_get_optional_exclusive(mmc_dev(host->mmc)); |
| 880 |
if (IS_ERR(priv->reset)) { |
| 881 |
err = PTR_ERR(priv->reset); |
| 882 |
dev_err(mmc_dev(host->mmc), "failed to get reset control %d\n", err); |
| 883 |
return err; |
| 884 |
} |
| 885 |
|
| 886 |
err = dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv, |
| 887 |
ARRAY_SIZE(clk_ids), clk_ids); |
| 888 |
if (err) |
| 889 |
return err; |
| 890 |
|
| 891 |
if (of_property_read_u8(mmc_dev(host->mmc)->of_node, "rockchip,txclk-tapnum", |
| 892 |
&priv->txclk_tapnum)) |
| 893 |
priv->txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT; |
| 894 |
|
| 895 |
/* Disable cmd conflict check */ |
| 896 |
sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3); |
| 897 |
/* Reset previous settings */ |
| 898 |
sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK); |
| 899 |
sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN); |
| 900 |
|
| 901 |
dwc_priv->priv = priv; |
| 902 |
|
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
static void dwcmshc_rk35xx_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv) |
| 907 |
{ |
| 908 |
/* |
| 909 |
* Don't support highspeed bus mode with low clk speed as we |
| 910 |
* cannot use DLL for this condition. |
| 911 |
*/ |
| 912 |
if (host->mmc->f_max <= 52000000) { |
| 913 |
dev_info(mmc_dev(host->mmc), "Disabling HS200/HS400, frequency too low (%d)\n", |
| 914 |
host->mmc->f_max); |
| 915 |
host->mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400); |
| 916 |
host->mmc->caps &= ~(MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
static void dwcmshc_rk3576_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv) |
| 921 |
{ |
| 922 |
struct device *dev = mmc_dev(host->mmc); |
| 923 |
int ret; |
| 924 |
|
| 925 |
/* |
| 926 |
* This works around the design of the RK3576's power domains, which |
| 927 |
* makes the PD_NVM power domain, which the sdhci controller on the |
| 928 |
* RK3576 is in, never come back the same way once it's run-time |
| 929 |
* suspended once. This can happen during early kernel boot if no driver |
| 930 |
* is using either PD_NVM or its child power domain PD_SDGMAC for a |
| 931 |
* short moment, leading to it being turned off to save power. By |
| 932 |
* keeping it on, sdhci suspending won't lead to PD_NVM becoming a |
| 933 |
* candidate for getting turned off. |
| 934 |
*/ |
| 935 |
ret = dev_pm_genpd_rpm_always_on(dev, true); |
| 936 |
if (ret && ret != -EOPNOTSUPP) |
| 937 |
dev_warn(dev, "failed to set PD rpm always on, SoC may hang later: %pe\n", |
| 938 |
ERR_PTR(ret)); |
| 939 |
|
| 940 |
dwcmshc_rk35xx_postinit(host, dwc_priv); |
| 941 |
} |
| 942 |
|
| 943 |
static int th1520_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 944 |
{ |
| 945 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 946 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 947 |
u32 val = 0; |
| 948 |
|
| 949 |
if (host->flags & SDHCI_HS400_TUNING) |
| 950 |
return 0; |
| 951 |
|
| 952 |
sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL), |
| 953 |
PHY_ATDL_CNFG_R); |
| 954 |
val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 955 |
|
| 956 |
/* |
| 957 |
* configure tuning settings: |
| 958 |
* - center phase select code driven in block gap interval |
| 959 |
* - disable reporting of framing errors |
| 960 |
* - disable software managed tuning |
| 961 |
* - disable user selection of sampling window edges, |
| 962 |
* instead tuning calculated edges are used |
| 963 |
*/ |
| 964 |
val &= ~(AT_CTRL_CI_SEL | AT_CTRL_RPT_TUNE_ERR | AT_CTRL_SW_TUNE_EN | |
| 965 |
FIELD_PREP(AT_CTRL_WIN_EDGE_SEL_MASK, AT_CTRL_WIN_EDGE_SEL)); |
| 966 |
|
| 967 |
/* |
| 968 |
* configure tuning settings: |
| 969 |
* - enable auto-tuning |
| 970 |
* - enable sampling window threshold |
| 971 |
* - stop clocks during phase code change |
| 972 |
* - set max latency in cycles between tx and rx clocks |
| 973 |
* - set max latency in cycles to switch output phase |
| 974 |
* - set max sampling window threshold value |
| 975 |
*/ |
| 976 |
val |= AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN; |
| 977 |
val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY); |
| 978 |
val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY); |
| 979 |
val |= FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, AT_CTRL_SWIN_TH_VAL); |
| 980 |
|
| 981 |
sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 982 |
val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 983 |
|
| 984 |
/* perform tuning */ |
| 985 |
sdhci_start_tuning(host); |
| 986 |
host->tuning_loop_count = 128; |
| 987 |
host->tuning_err = __sdhci_execute_tuning(host, opcode); |
| 988 |
if (host->tuning_err) { |
| 989 |
/* disable auto-tuning upon tuning error */ |
| 990 |
val &= ~AT_CTRL_AT_EN; |
| 991 |
sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 992 |
dev_err(mmc_dev(host->mmc), "tuning failed: %d\n", host->tuning_err); |
| 993 |
return -EIO; |
| 994 |
} |
| 995 |
sdhci_end_tuning(host); |
| 996 |
|
| 997 |
return 0; |
| 998 |
} |
| 999 |
|
| 1000 |
static void th1520_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 1001 |
{ |
| 1002 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1003 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1004 |
u16 ctrl_2; |
| 1005 |
|
| 1006 |
dwcmshc_reset(host, mask); |
| 1007 |
|
| 1008 |
if (priv->flags & FLAG_IO_FIXED_1V8) { |
| 1009 |
ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 1010 |
if (!(ctrl_2 & SDHCI_CTRL_VDD_180)) { |
| 1011 |
ctrl_2 |= SDHCI_CTRL_VDD_180; |
| 1012 |
sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
static int th1520_init(struct device *dev, |
| 1018 |
struct sdhci_host *host, |
| 1019 |
struct dwcmshc_priv *dwc_priv) |
| 1020 |
{ |
| 1021 |
dwc_priv->delay_line = PHY_SDCLKDL_DC_DEFAULT; |
| 1022 |
|
| 1023 |
if (device_property_read_bool(dev, "mmc-ddr-1_8v") || |
| 1024 |
device_property_read_bool(dev, "mmc-hs200-1_8v") || |
| 1025 |
device_property_read_bool(dev, "mmc-hs400-1_8v")) |
| 1026 |
dwc_priv->flags |= FLAG_IO_FIXED_1V8; |
| 1027 |
else |
| 1028 |
dwc_priv->flags &= ~FLAG_IO_FIXED_1V8; |
| 1029 |
|
| 1030 |
/* |
| 1031 |
* start_signal_voltage_switch() will try 3.3V first |
| 1032 |
* then 1.8V. Use SDHCI_SIGNALING_180 rather than |
| 1033 |
* SDHCI_SIGNALING_330 to avoid setting voltage to 3.3V |
| 1034 |
* in sdhci_start_signal_voltage_switch(). |
| 1035 |
*/ |
| 1036 |
if (dwc_priv->flags & FLAG_IO_FIXED_1V8) { |
| 1037 |
host->flags &= ~SDHCI_SIGNALING_330; |
| 1038 |
host->flags |= SDHCI_SIGNALING_180; |
| 1039 |
} |
| 1040 |
|
| 1041 |
sdhci_enable_v4_mode(host); |
| 1042 |
|
| 1043 |
return 0; |
| 1044 |
} |
| 1045 |
|
| 1046 |
static void cv18xx_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 1047 |
{ |
| 1048 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1049 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1050 |
u32 val, emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; |
| 1051 |
|
| 1052 |
dwcmshc_reset(host, mask); |
| 1053 |
|
| 1054 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { |
| 1055 |
val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1056 |
val |= CV18XX_EMMC_FUNC_EN; |
| 1057 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1058 |
} |
| 1059 |
|
| 1060 |
val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1061 |
val |= CV18XX_LATANCY_1T; |
| 1062 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1063 |
|
| 1064 |
val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); |
| 1065 |
val |= CV18XX_PHY_TX_BPS; |
| 1066 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); |
| 1067 |
|
| 1068 |
val = (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) | |
| 1069 |
FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) | |
| 1070 |
FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, 0) | |
| 1071 |
FIELD_PREP(CV18XX_PHY_RX_SRC_MSK, CV18XX_PHY_RX_SRC_INVERT_RX_CLK)); |
| 1072 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY); |
| 1073 |
} |
| 1074 |
|
| 1075 |
static void cv18xx_sdhci_set_tap(struct sdhci_host *host, int tap) |
| 1076 |
{ |
| 1077 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1078 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1079 |
u16 clk; |
| 1080 |
u32 val; |
| 1081 |
|
| 1082 |
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1083 |
clk &= ~SDHCI_CLOCK_CARD_EN; |
| 1084 |
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1085 |
|
| 1086 |
val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1087 |
val &= ~CV18XX_LATANCY_1T; |
| 1088 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL); |
| 1089 |
|
| 1090 |
val = (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) | |
| 1091 |
FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) | |
| 1092 |
FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, tap)); |
| 1093 |
sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY); |
| 1094 |
|
| 1095 |
sdhci_writel(host, 0, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG); |
| 1096 |
|
| 1097 |
clk |= SDHCI_CLOCK_CARD_EN; |
| 1098 |
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1099 |
usleep_range(1000, 2000); |
| 1100 |
} |
| 1101 |
|
| 1102 |
static int cv18xx_retry_tuning(struct mmc_host *mmc, u32 opcode, int *cmd_error) |
| 1103 |
{ |
| 1104 |
int ret, retry = 0; |
| 1105 |
|
| 1106 |
while (retry < CV18XX_RETRY_TUNING_MAX) { |
| 1107 |
ret = mmc_send_tuning(mmc, opcode, NULL); |
| 1108 |
if (ret) |
| 1109 |
return ret; |
| 1110 |
retry++; |
| 1111 |
} |
| 1112 |
|
| 1113 |
return 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
static void cv18xx_sdhci_post_tuning(struct sdhci_host *host) |
| 1117 |
{ |
| 1118 |
u32 val; |
| 1119 |
|
| 1120 |
val = sdhci_readl(host, SDHCI_INT_STATUS); |
| 1121 |
val |= SDHCI_INT_DATA_AVAIL; |
| 1122 |
sdhci_writel(host, val, SDHCI_INT_STATUS); |
| 1123 |
|
| 1124 |
dwcmshc_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static int cv18xx_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 1128 |
{ |
| 1129 |
int min, max, avg, ret; |
| 1130 |
int win_length, target_min, target_max, target_win_length; |
| 1131 |
|
| 1132 |
min = max = 0; |
| 1133 |
target_win_length = 0; |
| 1134 |
|
| 1135 |
sdhci_reset_tuning(host); |
| 1136 |
|
| 1137 |
while (max < CV18XX_TUNE_MAX) { |
| 1138 |
/* find the mininum delay first which can pass tuning */ |
| 1139 |
while (min < CV18XX_TUNE_MAX) { |
| 1140 |
cv18xx_sdhci_set_tap(host, min); |
| 1141 |
if (!cv18xx_retry_tuning(host->mmc, opcode, NULL)) |
| 1142 |
break; |
| 1143 |
min += CV18XX_TUNE_STEP; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* find the maxinum delay which can not pass tuning */ |
| 1147 |
max = min + CV18XX_TUNE_STEP; |
| 1148 |
while (max < CV18XX_TUNE_MAX) { |
| 1149 |
cv18xx_sdhci_set_tap(host, max); |
| 1150 |
if (cv18xx_retry_tuning(host->mmc, opcode, NULL)) { |
| 1151 |
max -= CV18XX_TUNE_STEP; |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
max += CV18XX_TUNE_STEP; |
| 1155 |
} |
| 1156 |
|
| 1157 |
win_length = max - min + 1; |
| 1158 |
/* get the largest pass window */ |
| 1159 |
if (win_length > target_win_length) { |
| 1160 |
target_win_length = win_length; |
| 1161 |
target_min = min; |
| 1162 |
target_max = max; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* continue to find the next pass window */ |
| 1166 |
min = max + CV18XX_TUNE_STEP; |
| 1167 |
} |
| 1168 |
|
| 1169 |
cv18xx_sdhci_post_tuning(host); |
| 1170 |
|
| 1171 |
/* use average delay to get the best timing */ |
| 1172 |
avg = (target_min + target_max) / 2; |
| 1173 |
cv18xx_sdhci_set_tap(host, avg); |
| 1174 |
ret = mmc_send_tuning(host->mmc, opcode, NULL); |
| 1175 |
|
| 1176 |
dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n", |
| 1177 |
ret ? "failed" : "passed", avg, ret); |
| 1178 |
|
| 1179 |
return ret; |
| 1180 |
} |
| 1181 |
|
| 1182 |
static inline void sg2042_sdhci_phy_init(struct sdhci_host *host) |
| 1183 |
{ |
| 1184 |
u32 val; |
| 1185 |
|
| 1186 |
/* Asset phy reset & set tx drive strength */ |
| 1187 |
val = sdhci_readl(host, PHY_CNFG_R); |
| 1188 |
val &= ~PHY_CNFG_RSTN_DEASSERT; |
| 1189 |
val |= FIELD_PREP(PHY_CNFG_PHY_PWRGOOD_MASK, 1); |
| 1190 |
val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP_SG2042); |
| 1191 |
val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN_SG2042); |
| 1192 |
sdhci_writel(host, val, PHY_CNFG_R); |
| 1193 |
|
| 1194 |
/* Configure phy pads */ |
| 1195 |
val = PHY_PAD_RXSEL_3V3; |
| 1196 |
val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP); |
| 1197 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 1198 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1199 |
sdhci_writew(host, val, PHY_CMDPAD_CNFG_R); |
| 1200 |
sdhci_writew(host, val, PHY_DATAPAD_CNFG_R); |
| 1201 |
sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R); |
| 1202 |
|
| 1203 |
val = PHY_PAD_RXSEL_3V3; |
| 1204 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 1205 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1206 |
sdhci_writew(host, val, PHY_CLKPAD_CNFG_R); |
| 1207 |
|
| 1208 |
val = PHY_PAD_RXSEL_3V3; |
| 1209 |
val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN); |
| 1210 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P); |
| 1211 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1212 |
sdhci_writew(host, val, PHY_STBPAD_CNFG_R); |
| 1213 |
|
| 1214 |
/* Configure delay line */ |
| 1215 |
/* Enable fixed delay */ |
| 1216 |
sdhci_writeb(host, PHY_SDCLKDL_CNFG_EXTDLY_EN, PHY_SDCLKDL_CNFG_R); |
| 1217 |
/* |
| 1218 |
* Set delay line. |
| 1219 |
* Its recommended that bit UPDATE_DC[4] is 1 when SDCLKDL_DC is being written. |
| 1220 |
* Ensure UPDATE_DC[4] is '0' when not updating code. |
| 1221 |
*/ |
| 1222 |
val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R); |
| 1223 |
val |= PHY_SDCLKDL_CNFG_UPDATE; |
| 1224 |
sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R); |
| 1225 |
/* Add 10 * 70ps = 0.7ns for output delay */ |
| 1226 |
sdhci_writeb(host, 10, PHY_SDCLKDL_DC_R); |
| 1227 |
val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R); |
| 1228 |
val &= ~(PHY_SDCLKDL_CNFG_UPDATE); |
| 1229 |
sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R); |
| 1230 |
|
| 1231 |
/* Set SMPLDL_CNFG, Bypass */ |
| 1232 |
sdhci_writeb(host, PHY_SMPLDL_CNFG_BYPASS_EN, PHY_SMPLDL_CNFG_R); |
| 1233 |
|
| 1234 |
/* Set ATDL_CNFG, tuning clk not use for init */ |
| 1235 |
val = FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL_SG2042); |
| 1236 |
sdhci_writeb(host, val, PHY_ATDL_CNFG_R); |
| 1237 |
|
| 1238 |
/* Deasset phy reset */ |
| 1239 |
val = sdhci_readl(host, PHY_CNFG_R); |
| 1240 |
val |= PHY_CNFG_RSTN_DEASSERT; |
| 1241 |
sdhci_writel(host, val, PHY_CNFG_R); |
| 1242 |
} |
| 1243 |
|
| 1244 |
static void sg2042_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 1245 |
{ |
| 1246 |
sdhci_reset(host, mask); |
| 1247 |
|
| 1248 |
if (mask & SDHCI_RESET_ALL) |
| 1249 |
sg2042_sdhci_phy_init(host); |
| 1250 |
} |
| 1251 |
|
| 1252 |
static int sg2042_init(struct device *dev, struct sdhci_host *host, |
| 1253 |
struct dwcmshc_priv *dwc_priv) |
| 1254 |
{ |
| 1255 |
static const char * const clk_ids[] = {"timer"}; |
| 1256 |
|
| 1257 |
return dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv, |
| 1258 |
ARRAY_SIZE(clk_ids), clk_ids); |
| 1259 |
} |
| 1260 |
|
| 1261 |
static void sdhci_eic7700_set_clock(struct sdhci_host *host, unsigned int clock) |
| 1262 |
{ |
| 1263 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1264 |
u16 clk; |
| 1265 |
|
| 1266 |
host->mmc->actual_clock = clock; |
| 1267 |
|
| 1268 |
if (clock == 0) { |
| 1269 |
sdhci_set_clock(host, clock); |
| 1270 |
return; |
| 1271 |
} |
| 1272 |
|
| 1273 |
clk_set_rate(pltfm_host->clk, clock); |
| 1274 |
|
| 1275 |
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1276 |
clk |= SDHCI_CLOCK_INT_EN; |
| 1277 |
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1278 |
|
| 1279 |
dwcmshc_enable_card_clk(host); |
| 1280 |
} |
| 1281 |
|
| 1282 |
static void sdhci_eic7700_config_phy_delay(struct sdhci_host *host, int delay) |
| 1283 |
{ |
| 1284 |
delay &= PHY_CLK_MAX_DELAY_MASK; |
| 1285 |
|
| 1286 |
/* phy clk delay line config */ |
| 1287 |
sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R); |
| 1288 |
sdhci_writeb(host, delay, PHY_SDCLKDL_DC_R); |
| 1289 |
sdhci_writeb(host, 0x0, PHY_SDCLKDL_CNFG_R); |
| 1290 |
} |
| 1291 |
|
| 1292 |
static void sdhci_eic7700_config_phy(struct sdhci_host *host) |
| 1293 |
{ |
| 1294 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1295 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 1296 |
u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; |
| 1297 |
struct eic7700_priv *priv = dwc_priv->priv; |
| 1298 |
unsigned int val, drv; |
| 1299 |
|
| 1300 |
drv = FIELD_PREP(PHY_CNFG_PAD_SP_MASK, priv->drive_impedance & 0xF); |
| 1301 |
drv |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, (priv->drive_impedance >> 4) & 0xF); |
| 1302 |
|
| 1303 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { |
| 1304 |
val = sdhci_readw(host, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 1305 |
val |= DWCMSHC_CARD_IS_EMMC; |
| 1306 |
sdhci_writew(host, val, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL); |
| 1307 |
} |
| 1308 |
|
| 1309 |
/* reset phy, config phy's pad */ |
| 1310 |
sdhci_writel(host, drv | ~PHY_CNFG_RSTN_DEASSERT, PHY_CNFG_R); |
| 1311 |
|
| 1312 |
/* configure phy pads */ |
| 1313 |
val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1314 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1315 |
val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP); |
| 1316 |
val |= PHY_PAD_RXSEL_1V8; |
| 1317 |
sdhci_writew(host, val, PHY_CMDPAD_CNFG_R); |
| 1318 |
sdhci_writew(host, val, PHY_DATAPAD_CNFG_R); |
| 1319 |
sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R); |
| 1320 |
|
| 1321 |
/* Clock PAD Setting */ |
| 1322 |
val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1323 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1324 |
sdhci_writew(host, val, PHY_CLKPAD_CNFG_R); |
| 1325 |
|
| 1326 |
/* PHY strobe PAD setting (EMMC only) */ |
| 1327 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { |
| 1328 |
val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1329 |
val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042); |
| 1330 |
val |= PHY_PAD_RXSEL_1V8; |
| 1331 |
sdhci_writew(host, val, PHY_STBPAD_CNFG_R); |
| 1332 |
} |
| 1333 |
usleep_range(2000, 3000); |
| 1334 |
sdhci_writel(host, drv | PHY_CNFG_RSTN_DEASSERT, PHY_CNFG_R); |
| 1335 |
sdhci_eic7700_config_phy_delay(host, dwc_priv->delay_line); |
| 1336 |
} |
| 1337 |
|
| 1338 |
static void sdhci_eic7700_reset(struct sdhci_host *host, u8 mask) |
| 1339 |
{ |
| 1340 |
sdhci_reset(host, mask); |
| 1341 |
|
| 1342 |
/* after reset all, the phy's config will be clear */ |
| 1343 |
if (mask == SDHCI_RESET_ALL) |
| 1344 |
sdhci_eic7700_config_phy(host); |
| 1345 |
} |
| 1346 |
|
| 1347 |
static int sdhci_eic7700_reset_init(struct device *dev, struct eic7700_priv *priv) |
| 1348 |
{ |
| 1349 |
int ret; |
| 1350 |
|
| 1351 |
priv->reset = devm_reset_control_array_get_optional_exclusive(dev); |
| 1352 |
if (IS_ERR(priv->reset)) { |
| 1353 |
ret = PTR_ERR(priv->reset); |
| 1354 |
dev_err(dev, "failed to get reset control %d\n", ret); |
| 1355 |
return ret; |
| 1356 |
} |
| 1357 |
|
| 1358 |
ret = reset_control_assert(priv->reset); |
| 1359 |
if (ret) { |
| 1360 |
dev_err(dev, "Failed to assert reset signals: %d\n", ret); |
| 1361 |
return ret; |
| 1362 |
} |
| 1363 |
usleep_range(2000, 2100); |
| 1364 |
ret = reset_control_deassert(priv->reset); |
| 1365 |
if (ret) { |
| 1366 |
dev_err(dev, "Failed to deassert reset signals: %d\n", ret); |
| 1367 |
return ret; |
| 1368 |
} |
| 1369 |
|
| 1370 |
return ret; |
| 1371 |
} |
| 1372 |
|
| 1373 |
static unsigned int eic7700_convert_drive_impedance_ohm(struct device *dev, unsigned int dr_ohm) |
| 1374 |
{ |
| 1375 |
switch (dr_ohm) { |
| 1376 |
case 100: |
| 1377 |
return PHYCTRL_DR_100OHM; |
| 1378 |
case 66: |
| 1379 |
return PHYCTRL_DR_66OHM; |
| 1380 |
case 50: |
| 1381 |
return PHYCTRL_DR_50OHM; |
| 1382 |
case 40: |
| 1383 |
return PHYCTRL_DR_40OHM; |
| 1384 |
case 33: |
| 1385 |
return PHYCTRL_DR_33OHM; |
| 1386 |
} |
| 1387 |
|
| 1388 |
dev_warn(dev, "Invalid value %u for drive-impedance-ohms.\n", dr_ohm); |
| 1389 |
return PHYCTRL_DR_50OHM; |
| 1390 |
} |
| 1391 |
|
| 1392 |
static int sdhci_eic7700_delay_tuning(struct sdhci_host *host, u32 opcode) |
| 1393 |
{ |
| 1394 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1395 |
struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host); |
| 1396 |
int delay_min = -1; |
| 1397 |
int delay_max = -1; |
| 1398 |
int cmd_error = 0; |
| 1399 |
int delay = 0; |
| 1400 |
int i = 0; |
| 1401 |
int ret; |
| 1402 |
|
| 1403 |
for (i = 0; i <= PHY_DELAY_CODE_MAX; i++) { |
| 1404 |
sdhci_eic7700_config_phy_delay(host, i); |
| 1405 |
ret = mmc_send_tuning(host->mmc, opcode, &cmd_error); |
| 1406 |
if (ret) { |
| 1407 |
host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 1408 |
usleep_range(200, 210); |
| 1409 |
if (delay_min != -1 && delay_max != -1) |
| 1410 |
break; |
| 1411 |
} else { |
| 1412 |
if (delay_min == -1) { |
| 1413 |
delay_min = i; |
| 1414 |
continue; |
| 1415 |
} else { |
| 1416 |
delay_max = i; |
| 1417 |
continue; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
} |
| 1421 |
if (delay_min == -1 && delay_max == -1) { |
| 1422 |
pr_err("%s: delay code tuning failed!\n", mmc_hostname(host->mmc)); |
| 1423 |
sdhci_eic7700_config_phy_delay(host, dwc_priv->delay_line); |
| 1424 |
return ret; |
| 1425 |
} |
| 1426 |
|
| 1427 |
delay = (delay_min + delay_max) / 2; |
| 1428 |
sdhci_eic7700_config_phy_delay(host, delay); |
| 1429 |
|
| 1430 |
return 0; |
| 1431 |
} |
| 1432 |
|
| 1433 |
static int sdhci_eic7700_phase_code_tuning(struct sdhci_host *host, u32 opcode) |
| 1434 |
{ |
| 1435 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1436 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1437 |
u32 sd_caps = MMC_CAP2_NO_MMC | MMC_CAP2_NO_SDIO; |
| 1438 |
int phase_code = -1; |
| 1439 |
int code_range = -1; |
| 1440 |
bool is_sd = false; |
| 1441 |
int code_min = -1; |
| 1442 |
int code_max = -1; |
| 1443 |
int cmd_error = 0; |
| 1444 |
int ret = 0; |
| 1445 |
int i = 0; |
| 1446 |
|
| 1447 |
if ((host->mmc->caps2 & sd_caps) == sd_caps) |
| 1448 |
is_sd = true; |
| 1449 |
|
| 1450 |
for (i = 0; i <= MAX_PHASE_CODE; i++) { |
| 1451 |
/* Centered Phase code */ |
| 1452 |
sdhci_writew(host, i, priv->vendor_specific_area1 + DWCMSHC_AT_STAT); |
| 1453 |
ret = mmc_send_tuning(host->mmc, opcode, &cmd_error); |
| 1454 |
host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 1455 |
|
| 1456 |
if (ret) { |
| 1457 |
/* SD specific range tracking */ |
| 1458 |
if (is_sd && code_min != -1 && code_max != -1) { |
| 1459 |
if (code_max - code_min > code_range) { |
| 1460 |
code_range = code_max - code_min; |
| 1461 |
phase_code = (code_min + code_max) / 2; |
| 1462 |
if (code_range > TUNING_RANGE_THRESHOLD) |
| 1463 |
break; |
| 1464 |
} |
| 1465 |
code_min = -1; |
| 1466 |
code_max = -1; |
| 1467 |
} |
| 1468 |
/* EMMC breaks after first valid range */ |
| 1469 |
if (!is_sd && code_min != -1 && code_max != -1) |
| 1470 |
break; |
| 1471 |
} else { |
| 1472 |
/* Track valid phase code range */ |
| 1473 |
if (code_min == -1) { |
| 1474 |
code_min = i; |
| 1475 |
if (!is_sd) |
| 1476 |
continue; |
| 1477 |
} |
| 1478 |
code_max = i; |
| 1479 |
if (is_sd && i == MAX_PHASE_CODE) { |
| 1480 |
if (code_max - code_min > code_range) { |
| 1481 |
code_range = code_max - code_min; |
| 1482 |
phase_code = (code_min + code_max) / 2; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* Handle tuning failure case */ |
| 1489 |
if ((is_sd && phase_code == -1) || |
| 1490 |
(!is_sd && code_min == -1 && code_max == -1)) { |
| 1491 |
pr_err("%s: phase code tuning failed!\n", mmc_hostname(host->mmc)); |
| 1492 |
sdhci_writew(host, 0, priv->vendor_specific_area1 + DWCMSHC_AT_STAT); |
| 1493 |
return -EIO; |
| 1494 |
} |
| 1495 |
if (!is_sd) |
| 1496 |
phase_code = (code_min + code_max) / 2; |
| 1497 |
|
| 1498 |
sdhci_writew(host, phase_code, priv->vendor_specific_area1 + DWCMSHC_AT_STAT); |
| 1499 |
|
| 1500 |
/* SD specific final verification */ |
| 1501 |
if (is_sd) { |
| 1502 |
ret = mmc_send_tuning(host->mmc, opcode, &cmd_error); |
| 1503 |
host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
| 1504 |
if (ret) { |
| 1505 |
pr_err("%s: Final phase code 0x%x verification failed!\n", |
| 1506 |
mmc_hostname(host->mmc), phase_code); |
| 1507 |
return ret; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
return 0; |
| 1512 |
} |
| 1513 |
|
| 1514 |
static int sdhci_eic7700_executing_tuning(struct sdhci_host *host, u32 opcode) |
| 1515 |
{ |
| 1516 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1517 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1518 |
u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; |
| 1519 |
int ret = 0; |
| 1520 |
u16 ctrl; |
| 1521 |
u32 val; |
| 1522 |
|
| 1523 |
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 1524 |
ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 1525 |
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 1526 |
|
| 1527 |
val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 1528 |
val |= AT_CTRL_SW_TUNE_EN; |
| 1529 |
sdhci_writew(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 1530 |
|
| 1531 |
sdhci_writew(host, 0, priv->vendor_specific_area1 + DWCMSHC_AT_STAT); |
| 1532 |
sdhci_writew(host, 0x0, SDHCI_CMD_DATA); |
| 1533 |
|
| 1534 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) { |
| 1535 |
ret = sdhci_eic7700_delay_tuning(host, opcode); |
| 1536 |
if (ret) |
| 1537 |
return ret; |
| 1538 |
} |
| 1539 |
|
| 1540 |
ret = sdhci_eic7700_phase_code_tuning(host, opcode); |
| 1541 |
if (ret) |
| 1542 |
return ret; |
| 1543 |
|
| 1544 |
return 0; |
| 1545 |
} |
| 1546 |
|
| 1547 |
static void sdhci_eic7700_set_uhs_signaling(struct sdhci_host *host, unsigned int timing) |
| 1548 |
{ |
| 1549 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1550 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1551 |
u8 status; |
| 1552 |
u32 val; |
| 1553 |
int ret; |
| 1554 |
|
| 1555 |
dwcmshc_set_uhs_signaling(host, timing); |
| 1556 |
|
| 1557 |
/* here need make dll locked when in hs400 at 200MHz */ |
| 1558 |
if (timing == MMC_TIMING_MMC_HS400 && host->clock == 200000000) { |
| 1559 |
val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 1560 |
val &= ~(FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY)); |
| 1561 |
/* 2-cycle latency */ |
| 1562 |
val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, 0x2); |
| 1563 |
sdhci_writew(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL); |
| 1564 |
|
| 1565 |
sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) | |
| 1566 |
0x3, PHY_DLL_CNFG1_R);/* DLL wait cycle input */ |
| 1567 |
/* DLL jump step input */ |
| 1568 |
sdhci_writeb(host, 0x02, PHY_DLL_CNFG2_R); |
| 1569 |
sdhci_writeb(host, FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, |
| 1570 |
PHY_DLLDL_CNFG_SLV_INPSEL), PHY_DLLDL_CNFG_R); |
| 1571 |
/* Sets the value of DLL's offset input */ |
| 1572 |
sdhci_writeb(host, 0x00, PHY_DLL_OFFST_R); |
| 1573 |
/* |
| 1574 |
* Sets the value of DLL's olbt loadval input. Controls the Ibt |
| 1575 |
* timer's timeout value at which DLL runs a revalidation cycle. |
| 1576 |
*/ |
| 1577 |
sdhci_writew(host, 0xffff, PHY_DLLBT_CNFG_R); |
| 1578 |
sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R); |
| 1579 |
usleep_range(100, 110); |
| 1580 |
|
| 1581 |
ret = read_poll_timeout(sdhci_readb, status, status & DLL_LOCK_STS, 100, 1000000, |
| 1582 |
false, host, PHY_DLL_STATUS_R); |
| 1583 |
if (ret) { |
| 1584 |
pr_err("%s: DLL lock timeout! status: 0x%x\n", |
| 1585 |
mmc_hostname(host->mmc), status); |
| 1586 |
return; |
| 1587 |
} |
| 1588 |
|
| 1589 |
status = sdhci_readb(host, PHY_DLL_STATUS_R); |
| 1590 |
if (status & DLL_ERROR_STS) { |
| 1591 |
pr_err("%s: DLL lock failed!err_status:0x%x\n", |
| 1592 |
mmc_hostname(host->mmc), status); |
| 1593 |
} |
| 1594 |
} |
| 1595 |
} |
| 1596 |
|
| 1597 |
static void sdhci_eic7700_set_uhs_wrapper(struct sdhci_host *host, unsigned int timing) |
| 1598 |
{ |
| 1599 |
u32 sd_caps = MMC_CAP2_NO_MMC | MMC_CAP2_NO_SDIO; |
| 1600 |
|
| 1601 |
if ((host->mmc->caps2 & sd_caps) == sd_caps) |
| 1602 |
sdhci_set_uhs_signaling(host, timing); |
| 1603 |
else |
| 1604 |
sdhci_eic7700_set_uhs_signaling(host, timing); |
| 1605 |
} |
| 1606 |
|
| 1607 |
static int eic7700_init(struct device *dev, struct sdhci_host *host, struct dwcmshc_priv *dwc_priv) |
| 1608 |
{ |
| 1609 |
u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO; |
| 1610 |
unsigned int val, hsp_int_status, hsp_pwr_ctrl; |
| 1611 |
static const char * const clk_ids[] = {"axi"}; |
| 1612 |
struct of_phandle_args args; |
| 1613 |
struct eic7700_priv *priv; |
| 1614 |
struct regmap *hsp_regmap; |
| 1615 |
int ret; |
| 1616 |
|
| 1617 |
priv = devm_kzalloc(dev, sizeof(struct eic7700_priv), GFP_KERNEL); |
| 1618 |
if (!priv) |
| 1619 |
return -ENOMEM; |
| 1620 |
|
| 1621 |
dwc_priv->priv = priv; |
| 1622 |
|
| 1623 |
ret = sdhci_eic7700_reset_init(dev, dwc_priv->priv); |
| 1624 |
if (ret) { |
| 1625 |
dev_err(dev, "failed to reset\n"); |
| 1626 |
return ret; |
| 1627 |
} |
| 1628 |
|
| 1629 |
ret = dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv, |
| 1630 |
ARRAY_SIZE(clk_ids), clk_ids); |
| 1631 |
if (ret) |
| 1632 |
return ret; |
| 1633 |
|
| 1634 |
ret = of_parse_phandle_with_fixed_args(dev->of_node, "eswin,hsp-sp-csr", 2, 0, &args); |
| 1635 |
if (ret) { |
| 1636 |
dev_err(dev, "Fail to parse 'eswin,hsp-sp-csr' phandle (%d)\n", ret); |
| 1637 |
return ret; |
| 1638 |
} |
| 1639 |
|
| 1640 |
hsp_regmap = syscon_node_to_regmap(args.np); |
| 1641 |
if (IS_ERR(hsp_regmap)) { |
| 1642 |
dev_err(dev, "Failed to get regmap for 'eswin,hsp-sp-csr'\n"); |
| 1643 |
of_node_put(args.np); |
| 1644 |
return PTR_ERR(hsp_regmap); |
| 1645 |
} |
| 1646 |
hsp_int_status = args.args[0]; |
| 1647 |
hsp_pwr_ctrl = args.args[1]; |
| 1648 |
of_node_put(args.np); |
| 1649 |
/* |
| 1650 |
* Assert clock stability: write EIC7700_INT_CLK_STABLE to hsp_int_status. |
| 1651 |
* This signals to the eMMC controller that platform clocks (card, ACLK, |
| 1652 |
* BCLK, TMCLK) are enabled and stable. |
| 1653 |
*/ |
| 1654 |
regmap_write(hsp_regmap, hsp_int_status, EIC7700_INT_CLK_STABLE); |
| 1655 |
/* |
| 1656 |
* Assert voltage stability: write EIC7700_HOST_VAL_STABLE to hsp_pwr_ctrl. |
| 1657 |
* This signals that VDD is stable and permits transition to high-speed |
| 1658 |
* modes (e.g., UHS-I). |
| 1659 |
*/ |
| 1660 |
regmap_write(hsp_regmap, hsp_pwr_ctrl, EIC7700_HOST_VAL_STABLE); |
| 1661 |
|
| 1662 |
if ((host->mmc->caps2 & emmc_caps) == emmc_caps) |
| 1663 |
dwc_priv->delay_line = PHY_DELAY_CODE_EMMC; |
| 1664 |
else |
| 1665 |
dwc_priv->delay_line = PHY_DELAY_CODE_SD; |
| 1666 |
|
| 1667 |
if (!of_property_read_u32(dev->of_node, "eswin,drive-impedance-ohms", &val)) |
| 1668 |
priv->drive_impedance = eic7700_convert_drive_impedance_ohm(dev, val); |
| 1669 |
return 0; |
| 1670 |
} |
| 1671 |
|
| 1672 |
static const struct sdhci_ops sdhci_dwcmshc_ops = { |
| 1673 |
.set_clock = sdhci_set_clock, |
| 1674 |
.set_bus_width = sdhci_set_bus_width, |
| 1675 |
.set_uhs_signaling = dwcmshc_set_uhs_signaling, |
| 1676 |
.get_max_clock = dwcmshc_get_max_clock, |
| 1677 |
.reset = dwcmshc_reset, |
| 1678 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1679 |
.irq = dwcmshc_cqe_irq_handler, |
| 1680 |
}; |
| 1681 |
|
| 1682 |
#ifdef CONFIG_ACPI |
| 1683 |
static void dwcmshc_bf3_hw_reset(struct sdhci_host *host) |
| 1684 |
{ |
| 1685 |
struct arm_smccc_res res = { 0 }; |
| 1686 |
|
| 1687 |
arm_smccc_smc(BLUEFIELD_SMC_SET_EMMC_RST_N, 0, 0, 0, 0, 0, 0, 0, &res); |
| 1688 |
|
| 1689 |
if (res.a0) |
| 1690 |
pr_err("%s: RST_N failed.\n", mmc_hostname(host->mmc)); |
| 1691 |
} |
| 1692 |
|
| 1693 |
static const struct sdhci_ops sdhci_dwcmshc_bf3_ops = { |
| 1694 |
.set_clock = sdhci_set_clock, |
| 1695 |
.set_bus_width = sdhci_set_bus_width, |
| 1696 |
.set_uhs_signaling = dwcmshc_set_uhs_signaling, |
| 1697 |
.get_max_clock = dwcmshc_get_max_clock, |
| 1698 |
.reset = sdhci_reset, |
| 1699 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1700 |
.irq = dwcmshc_cqe_irq_handler, |
| 1701 |
.hw_reset = dwcmshc_bf3_hw_reset, |
| 1702 |
}; |
| 1703 |
#endif |
| 1704 |
|
| 1705 |
static const struct sdhci_ops sdhci_dwcmshc_rk35xx_ops = { |
| 1706 |
.set_clock = dwcmshc_rk3568_set_clock, |
| 1707 |
.set_bus_width = sdhci_set_bus_width, |
| 1708 |
.set_uhs_signaling = dwcmshc_set_uhs_signaling, |
| 1709 |
.get_max_clock = rk35xx_get_max_clock, |
| 1710 |
.reset = rk35xx_sdhci_reset, |
| 1711 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1712 |
.irq = dwcmshc_cqe_irq_handler, |
| 1713 |
}; |
| 1714 |
|
| 1715 |
static const struct sdhci_ops sdhci_dwcmshc_th1520_ops = { |
| 1716 |
.set_clock = sdhci_set_clock, |
| 1717 |
.set_bus_width = sdhci_set_bus_width, |
| 1718 |
.set_uhs_signaling = th1520_set_uhs_signaling, |
| 1719 |
.get_max_clock = dwcmshc_get_max_clock, |
| 1720 |
.reset = th1520_sdhci_reset, |
| 1721 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1722 |
.voltage_switch = dwcmshc_phy_init, |
| 1723 |
.platform_execute_tuning = th1520_execute_tuning, |
| 1724 |
}; |
| 1725 |
|
| 1726 |
static const struct sdhci_ops sdhci_dwcmshc_cv18xx_ops = { |
| 1727 |
.set_clock = sdhci_set_clock, |
| 1728 |
.set_bus_width = sdhci_set_bus_width, |
| 1729 |
.set_uhs_signaling = dwcmshc_set_uhs_signaling, |
| 1730 |
.get_max_clock = dwcmshc_get_max_clock, |
| 1731 |
.reset = cv18xx_sdhci_reset, |
| 1732 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1733 |
.platform_execute_tuning = cv18xx_sdhci_execute_tuning, |
| 1734 |
}; |
| 1735 |
|
| 1736 |
static const struct sdhci_ops sdhci_dwcmshc_sg2042_ops = { |
| 1737 |
.set_clock = sdhci_set_clock, |
| 1738 |
.set_bus_width = sdhci_set_bus_width, |
| 1739 |
.set_uhs_signaling = dwcmshc_set_uhs_signaling, |
| 1740 |
.get_max_clock = dwcmshc_get_max_clock, |
| 1741 |
.reset = sg2042_sdhci_reset, |
| 1742 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1743 |
.platform_execute_tuning = th1520_execute_tuning, |
| 1744 |
}; |
| 1745 |
|
| 1746 |
static const struct sdhci_ops sdhci_dwcmshc_eic7700_ops = { |
| 1747 |
.set_clock = sdhci_eic7700_set_clock, |
| 1748 |
.get_max_clock = sdhci_pltfm_clk_get_max_clock, |
| 1749 |
.get_timeout_clock = sdhci_pltfm_clk_get_max_clock, |
| 1750 |
.set_bus_width = sdhci_set_bus_width, |
| 1751 |
.reset = sdhci_eic7700_reset, |
| 1752 |
.set_uhs_signaling = sdhci_eic7700_set_uhs_wrapper, |
| 1753 |
.set_power = sdhci_set_power_and_bus_voltage, |
| 1754 |
.irq = dwcmshc_cqe_irq_handler, |
| 1755 |
.adma_write_desc = dwcmshc_adma_write_desc, |
| 1756 |
.platform_execute_tuning = sdhci_eic7700_executing_tuning, |
| 1757 |
}; |
| 1758 |
|
| 1759 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_pdata = { |
| 1760 |
.pdata = { |
| 1761 |
.ops = &sdhci_dwcmshc_ops, |
| 1762 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 1763 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 1764 |
}, |
| 1765 |
}; |
| 1766 |
|
| 1767 |
#ifdef CONFIG_ACPI |
| 1768 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_bf3_pdata = { |
| 1769 |
.pdata = { |
| 1770 |
.ops = &sdhci_dwcmshc_bf3_ops, |
| 1771 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 1772 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 1773 |
SDHCI_QUIRK2_ACMD23_BROKEN, |
| 1774 |
}, |
| 1775 |
}; |
| 1776 |
#endif |
| 1777 |
|
| 1778 |
static const struct cqhci_host_ops rk35xx_cqhci_ops = { |
| 1779 |
.pre_enable = rk35xx_sdhci_cqe_pre_enable, |
| 1780 |
.enable = rk35xx_sdhci_cqe_enable, |
| 1781 |
.disable = rk35xx_sdhci_cqe_disable, |
| 1782 |
.post_disable = rk35xx_sdhci_cqe_post_disable, |
| 1783 |
.dumpregs = dwcmshc_cqhci_dumpregs, |
| 1784 |
.set_tran_desc = dwcmshc_set_tran_desc, |
| 1785 |
}; |
| 1786 |
|
| 1787 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_rk35xx_pdata = { |
| 1788 |
.pdata = { |
| 1789 |
.ops = &sdhci_dwcmshc_rk35xx_ops, |
| 1790 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 1791 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 1792 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 1793 |
SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, |
| 1794 |
}, |
| 1795 |
.cqhci_host_ops = &rk35xx_cqhci_ops, |
| 1796 |
.init = dwcmshc_rk35xx_init, |
| 1797 |
.postinit = dwcmshc_rk35xx_postinit, |
| 1798 |
}; |
| 1799 |
|
| 1800 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_rk3576_pdata = { |
| 1801 |
.pdata = { |
| 1802 |
.ops = &sdhci_dwcmshc_rk35xx_ops, |
| 1803 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 1804 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 1805 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 1806 |
SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, |
| 1807 |
}, |
| 1808 |
.cqhci_host_ops = &rk35xx_cqhci_ops, |
| 1809 |
.init = dwcmshc_rk35xx_init, |
| 1810 |
.postinit = dwcmshc_rk3576_postinit, |
| 1811 |
}; |
| 1812 |
|
| 1813 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_th1520_pdata = { |
| 1814 |
.pdata = { |
| 1815 |
.ops = &sdhci_dwcmshc_th1520_ops, |
| 1816 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 1817 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 1818 |
}, |
| 1819 |
.init = th1520_init, |
| 1820 |
}; |
| 1821 |
|
| 1822 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_cv18xx_pdata = { |
| 1823 |
.pdata = { |
| 1824 |
.ops = &sdhci_dwcmshc_cv18xx_ops, |
| 1825 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 1826 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 1827 |
}, |
| 1828 |
}; |
| 1829 |
|
| 1830 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_sg2042_pdata = { |
| 1831 |
.pdata = { |
| 1832 |
.ops = &sdhci_dwcmshc_sg2042_ops, |
| 1833 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, |
| 1834 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 1835 |
}, |
| 1836 |
.init = sg2042_init, |
| 1837 |
}; |
| 1838 |
|
| 1839 |
static const struct dwcmshc_pltfm_data sdhci_dwcmshc_eic7700_pdata = { |
| 1840 |
.pdata = { |
| 1841 |
.ops = &sdhci_dwcmshc_eic7700_ops, |
| 1842 |
.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 1843 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 1844 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 1845 |
SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, |
| 1846 |
}, |
| 1847 |
.init = eic7700_init, |
| 1848 |
}; |
| 1849 |
|
| 1850 |
static const struct cqhci_host_ops dwcmshc_cqhci_ops = { |
| 1851 |
.enable = dwcmshc_sdhci_cqe_enable, |
| 1852 |
.disable = sdhci_cqe_disable, |
| 1853 |
.dumpregs = dwcmshc_cqhci_dumpregs, |
| 1854 |
.set_tran_desc = dwcmshc_set_tran_desc, |
| 1855 |
}; |
| 1856 |
|
| 1857 |
static void dwcmshc_cqhci_init(struct sdhci_host *host, struct platform_device *pdev, |
| 1858 |
const struct dwcmshc_pltfm_data *pltfm_data) |
| 1859 |
{ |
| 1860 |
struct cqhci_host *cq_host; |
| 1861 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1862 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 1863 |
bool dma64 = false; |
| 1864 |
u16 clk; |
| 1865 |
int err; |
| 1866 |
|
| 1867 |
host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD; |
| 1868 |
cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL); |
| 1869 |
if (!cq_host) { |
| 1870 |
dev_err(mmc_dev(host->mmc), "Unable to setup CQE: not enough memory\n"); |
| 1871 |
goto dsbl_cqe_caps; |
| 1872 |
} |
| 1873 |
|
| 1874 |
/* |
| 1875 |
* For dwcmshc host controller we have to enable internal clock |
| 1876 |
* before access to some registers from Vendor Specific Area 2. |
| 1877 |
*/ |
| 1878 |
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1879 |
clk |= SDHCI_CLOCK_INT_EN; |
| 1880 |
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1881 |
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1882 |
if (!(clk & SDHCI_CLOCK_INT_EN)) { |
| 1883 |
dev_err(mmc_dev(host->mmc), "Unable to setup CQE: internal clock enable error\n"); |
| 1884 |
goto free_cq_host; |
| 1885 |
} |
| 1886 |
|
| 1887 |
cq_host->mmio = host->ioaddr + priv->vendor_specific_area2; |
| 1888 |
if (pltfm_data->cqhci_host_ops) |
| 1889 |
cq_host->ops = pltfm_data->cqhci_host_ops; |
| 1890 |
else |
| 1891 |
cq_host->ops = &dwcmshc_cqhci_ops; |
| 1892 |
|
| 1893 |
/* Enable using of 128-bit task descriptors */ |
| 1894 |
dma64 = host->flags & SDHCI_USE_64_BIT_DMA; |
| 1895 |
if (dma64) { |
| 1896 |
dev_dbg(mmc_dev(host->mmc), "128-bit task descriptors\n"); |
| 1897 |
cq_host->caps |= CQHCI_TASK_DESC_SZ_128; |
| 1898 |
} |
| 1899 |
err = cqhci_init(cq_host, host->mmc, dma64); |
| 1900 |
if (err) { |
| 1901 |
dev_err(mmc_dev(host->mmc), "Unable to setup CQE: error %d\n", err); |
| 1902 |
goto int_clock_disable; |
| 1903 |
} |
| 1904 |
|
| 1905 |
dev_dbg(mmc_dev(host->mmc), "CQE init done\n"); |
| 1906 |
|
| 1907 |
return; |
| 1908 |
|
| 1909 |
int_clock_disable: |
| 1910 |
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1911 |
clk &= ~SDHCI_CLOCK_INT_EN; |
| 1912 |
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1913 |
|
| 1914 |
free_cq_host: |
| 1915 |
devm_kfree(&pdev->dev, cq_host); |
| 1916 |
|
| 1917 |
dsbl_cqe_caps: |
| 1918 |
host->mmc->caps2 &= ~(MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD); |
| 1919 |
} |
| 1920 |
|
| 1921 |
static const struct of_device_id sdhci_dwcmshc_dt_ids[] = { |
| 1922 |
{ |
| 1923 |
.compatible = "rockchip,rk3588-dwcmshc", |
| 1924 |
.data = &sdhci_dwcmshc_rk35xx_pdata, |
| 1925 |
}, |
| 1926 |
{ |
| 1927 |
.compatible = "rockchip,rk3576-dwcmshc", |
| 1928 |
.data = &sdhci_dwcmshc_rk3576_pdata, |
| 1929 |
}, |
| 1930 |
{ |
| 1931 |
.compatible = "rockchip,rk3568-dwcmshc", |
| 1932 |
.data = &sdhci_dwcmshc_rk35xx_pdata, |
| 1933 |
}, |
| 1934 |
{ |
| 1935 |
.compatible = "snps,dwcmshc-sdhci", |
| 1936 |
.data = &sdhci_dwcmshc_pdata, |
| 1937 |
}, |
| 1938 |
{ |
| 1939 |
.compatible = "sophgo,cv1800b-dwcmshc", |
| 1940 |
.data = &sdhci_dwcmshc_cv18xx_pdata, |
| 1941 |
}, |
| 1942 |
{ |
| 1943 |
.compatible = "sophgo,sg2002-dwcmshc", |
| 1944 |
.data = &sdhci_dwcmshc_cv18xx_pdata, |
| 1945 |
}, |
| 1946 |
{ |
| 1947 |
.compatible = "thead,th1520-dwcmshc", |
| 1948 |
.data = &sdhci_dwcmshc_th1520_pdata, |
| 1949 |
}, |
| 1950 |
{ |
| 1951 |
.compatible = "sophgo,sg2042-dwcmshc", |
| 1952 |
.data = &sdhci_dwcmshc_sg2042_pdata, |
| 1953 |
}, |
| 1954 |
{ |
| 1955 |
.compatible = "eswin,eic7700-dwcmshc", |
| 1956 |
.data = &sdhci_dwcmshc_eic7700_pdata, |
| 1957 |
}, |
| 1958 |
{}, |
| 1959 |
}; |
| 1960 |
MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids); |
| 1961 |
|
| 1962 |
#ifdef CONFIG_ACPI |
| 1963 |
static const struct acpi_device_id sdhci_dwcmshc_acpi_ids[] = { |
| 1964 |
{ |
| 1965 |
.id = "MLNXBF30", |
| 1966 |
.driver_data = (kernel_ulong_t)&sdhci_dwcmshc_bf3_pdata, |
| 1967 |
}, |
| 1968 |
{} |
| 1969 |
}; |
| 1970 |
MODULE_DEVICE_TABLE(acpi, sdhci_dwcmshc_acpi_ids); |
| 1971 |
#endif |
| 1972 |
|
| 1973 |
static int dwcmshc_probe(struct platform_device *pdev) |
| 1974 |
{ |
| 1975 |
struct device *dev = &pdev->dev; |
| 1976 |
struct sdhci_pltfm_host *pltfm_host; |
| 1977 |
struct sdhci_host *host; |
| 1978 |
struct dwcmshc_priv *priv; |
| 1979 |
const struct dwcmshc_pltfm_data *pltfm_data; |
| 1980 |
int err; |
| 1981 |
u32 extra, caps; |
| 1982 |
|
| 1983 |
pltfm_data = device_get_match_data(&pdev->dev); |
| 1984 |
if (!pltfm_data) { |
| 1985 |
dev_err(&pdev->dev, "Error: No device match data found\n"); |
| 1986 |
return -ENODEV; |
| 1987 |
} |
| 1988 |
|
| 1989 |
host = sdhci_pltfm_init(pdev, &pltfm_data->pdata, |
| 1990 |
sizeof(struct dwcmshc_priv)); |
| 1991 |
if (IS_ERR(host)) |
| 1992 |
return PTR_ERR(host); |
| 1993 |
|
| 1994 |
/* |
| 1995 |
* extra adma table cnt for cross 128M boundary handling. |
| 1996 |
*/ |
| 1997 |
extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M); |
| 1998 |
if (extra > SDHCI_MAX_SEGS) |
| 1999 |
extra = SDHCI_MAX_SEGS; |
| 2000 |
host->adma_table_cnt += extra; |
| 2001 |
|
| 2002 |
pltfm_host = sdhci_priv(host); |
| 2003 |
priv = sdhci_pltfm_priv(pltfm_host); |
| 2004 |
|
| 2005 |
if (dev->of_node) { |
| 2006 |
pltfm_host->clk = devm_clk_get(dev, "core"); |
| 2007 |
if (IS_ERR(pltfm_host->clk)) |
| 2008 |
return dev_err_probe(dev, PTR_ERR(pltfm_host->clk), |
| 2009 |
"failed to get core clk\n"); |
| 2010 |
|
| 2011 |
err = clk_prepare_enable(pltfm_host->clk); |
| 2012 |
if (err) |
| 2013 |
return err; |
| 2014 |
|
| 2015 |
priv->bus_clk = devm_clk_get(dev, "bus"); |
| 2016 |
if (!IS_ERR(priv->bus_clk)) |
| 2017 |
clk_prepare_enable(priv->bus_clk); |
| 2018 |
} |
| 2019 |
|
| 2020 |
err = mmc_of_parse(host->mmc); |
| 2021 |
if (err) |
| 2022 |
goto err_clk; |
| 2023 |
|
| 2024 |
sdhci_get_of_property(pdev); |
| 2025 |
|
| 2026 |
priv->vendor_specific_area1 = |
| 2027 |
sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK; |
| 2028 |
|
| 2029 |
host->mmc_host_ops.request = dwcmshc_request; |
| 2030 |
host->mmc_host_ops.hs400_enhanced_strobe = dwcmshc_hs400_enhanced_strobe; |
| 2031 |
host->mmc_host_ops.execute_tuning = dwcmshc_execute_tuning; |
| 2032 |
|
| 2033 |
if (pltfm_data->init) { |
| 2034 |
err = pltfm_data->init(&pdev->dev, host, priv); |
| 2035 |
if (err) |
| 2036 |
goto err_clk; |
| 2037 |
} |
| 2038 |
|
| 2039 |
#ifdef CONFIG_ACPI |
| 2040 |
if (pltfm_data == &sdhci_dwcmshc_bf3_pdata) |
| 2041 |
sdhci_enable_v4_mode(host); |
| 2042 |
#endif |
| 2043 |
|
| 2044 |
caps = sdhci_readl(host, SDHCI_CAPABILITIES); |
| 2045 |
if (caps & SDHCI_CAN_64BIT_V4) |
| 2046 |
sdhci_enable_v4_mode(host); |
| 2047 |
|
| 2048 |
host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY; |
| 2049 |
|
| 2050 |
pm_runtime_get_noresume(dev); |
| 2051 |
pm_runtime_set_active(dev); |
| 2052 |
pm_runtime_enable(dev); |
| 2053 |
|
| 2054 |
err = sdhci_setup_host(host); |
| 2055 |
if (err) |
| 2056 |
goto err_rpm; |
| 2057 |
|
| 2058 |
/* Setup Command Queue Engine if enabled */ |
| 2059 |
if (device_property_read_bool(&pdev->dev, "supports-cqe")) { |
| 2060 |
priv->vendor_specific_area2 = |
| 2061 |
sdhci_readw(host, DWCMSHC_P_VENDOR_AREA2); |
| 2062 |
|
| 2063 |
dwcmshc_cqhci_init(host, pdev, pltfm_data); |
| 2064 |
} |
| 2065 |
|
| 2066 |
if (pltfm_data->postinit) |
| 2067 |
pltfm_data->postinit(host, priv); |
| 2068 |
|
| 2069 |
err = __sdhci_add_host(host); |
| 2070 |
if (err) |
| 2071 |
goto err_setup_host; |
| 2072 |
|
| 2073 |
pm_runtime_put(dev); |
| 2074 |
|
| 2075 |
return 0; |
| 2076 |
|
| 2077 |
err_setup_host: |
| 2078 |
sdhci_cleanup_host(host); |
| 2079 |
err_rpm: |
| 2080 |
pm_runtime_disable(dev); |
| 2081 |
pm_runtime_put_noidle(dev); |
| 2082 |
err_clk: |
| 2083 |
clk_disable_unprepare(pltfm_host->clk); |
| 2084 |
clk_disable_unprepare(priv->bus_clk); |
| 2085 |
clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks); |
| 2086 |
return err; |
| 2087 |
} |
| 2088 |
|
| 2089 |
static void dwcmshc_disable_card_clk(struct sdhci_host *host) |
| 2090 |
{ |
| 2091 |
u16 ctrl; |
| 2092 |
|
| 2093 |
ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 2094 |
if (ctrl & SDHCI_CLOCK_CARD_EN) { |
| 2095 |
ctrl &= ~SDHCI_CLOCK_CARD_EN; |
| 2096 |
sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL); |
| 2097 |
} |
| 2098 |
} |
| 2099 |
|
| 2100 |
static void dwcmshc_remove(struct platform_device *pdev) |
| 2101 |
{ |
| 2102 |
struct sdhci_host *host = platform_get_drvdata(pdev); |
| 2103 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2104 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 2105 |
|
| 2106 |
pm_runtime_get_sync(&pdev->dev); |
| 2107 |
pm_runtime_disable(&pdev->dev); |
| 2108 |
pm_runtime_put_noidle(&pdev->dev); |
| 2109 |
|
| 2110 |
sdhci_remove_host(host, 0); |
| 2111 |
|
| 2112 |
dwcmshc_disable_card_clk(host); |
| 2113 |
|
| 2114 |
clk_disable_unprepare(pltfm_host->clk); |
| 2115 |
clk_disable_unprepare(priv->bus_clk); |
| 2116 |
clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks); |
| 2117 |
} |
| 2118 |
|
| 2119 |
static int dwcmshc_suspend(struct device *dev) |
| 2120 |
{ |
| 2121 |
struct sdhci_host *host = dev_get_drvdata(dev); |
| 2122 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2123 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 2124 |
int ret; |
| 2125 |
|
| 2126 |
pm_runtime_resume(dev); |
| 2127 |
|
| 2128 |
if (host->mmc->caps2 & MMC_CAP2_CQE) { |
| 2129 |
ret = cqhci_suspend(host->mmc); |
| 2130 |
if (ret) |
| 2131 |
return ret; |
| 2132 |
} |
| 2133 |
|
| 2134 |
ret = sdhci_suspend_host(host); |
| 2135 |
if (ret) |
| 2136 |
return ret; |
| 2137 |
|
| 2138 |
clk_disable_unprepare(pltfm_host->clk); |
| 2139 |
if (!IS_ERR(priv->bus_clk)) |
| 2140 |
clk_disable_unprepare(priv->bus_clk); |
| 2141 |
|
| 2142 |
clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks); |
| 2143 |
|
| 2144 |
return ret; |
| 2145 |
} |
| 2146 |
|
| 2147 |
static int dwcmshc_resume(struct device *dev) |
| 2148 |
{ |
| 2149 |
struct sdhci_host *host = dev_get_drvdata(dev); |
| 2150 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2151 |
struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 2152 |
int ret; |
| 2153 |
|
| 2154 |
ret = clk_prepare_enable(pltfm_host->clk); |
| 2155 |
if (ret) |
| 2156 |
return ret; |
| 2157 |
|
| 2158 |
if (!IS_ERR(priv->bus_clk)) { |
| 2159 |
ret = clk_prepare_enable(priv->bus_clk); |
| 2160 |
if (ret) |
| 2161 |
goto disable_clk; |
| 2162 |
} |
| 2163 |
|
| 2164 |
ret = clk_bulk_prepare_enable(priv->num_other_clks, priv->other_clks); |
| 2165 |
if (ret) |
| 2166 |
goto disable_bus_clk; |
| 2167 |
|
| 2168 |
ret = sdhci_resume_host(host); |
| 2169 |
if (ret) |
| 2170 |
goto disable_other_clks; |
| 2171 |
|
| 2172 |
if (host->mmc->caps2 & MMC_CAP2_CQE) { |
| 2173 |
ret = cqhci_resume(host->mmc); |
| 2174 |
if (ret) |
| 2175 |
goto disable_other_clks; |
| 2176 |
} |
| 2177 |
|
| 2178 |
return 0; |
| 2179 |
|
| 2180 |
disable_other_clks: |
| 2181 |
clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks); |
| 2182 |
disable_bus_clk: |
| 2183 |
if (!IS_ERR(priv->bus_clk)) |
| 2184 |
clk_disable_unprepare(priv->bus_clk); |
| 2185 |
disable_clk: |
| 2186 |
clk_disable_unprepare(pltfm_host->clk); |
| 2187 |
return ret; |
| 2188 |
} |
| 2189 |
|
| 2190 |
static int dwcmshc_runtime_suspend(struct device *dev) |
| 2191 |
{ |
| 2192 |
struct sdhci_host *host = dev_get_drvdata(dev); |
| 2193 |
|
| 2194 |
dwcmshc_disable_card_clk(host); |
| 2195 |
|
| 2196 |
return 0; |
| 2197 |
} |
| 2198 |
|
| 2199 |
static int dwcmshc_runtime_resume(struct device *dev) |
| 2200 |
{ |
| 2201 |
struct sdhci_host *host = dev_get_drvdata(dev); |
| 2202 |
|
| 2203 |
dwcmshc_enable_card_clk(host); |
| 2204 |
|
| 2205 |
return 0; |
| 2206 |
} |
| 2207 |
|
| 2208 |
static const struct dev_pm_ops dwcmshc_pmops = { |
| 2209 |
SYSTEM_SLEEP_PM_OPS(dwcmshc_suspend, dwcmshc_resume) |
| 2210 |
RUNTIME_PM_OPS(dwcmshc_runtime_suspend, dwcmshc_runtime_resume, NULL) |
| 2211 |
}; |
| 2212 |
|
| 2213 |
static struct platform_driver sdhci_dwcmshc_driver = { |
| 2214 |
.driver = { |
| 2215 |
.name = "sdhci-dwcmshc", |
| 2216 |
.probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 2217 |
.of_match_table = sdhci_dwcmshc_dt_ids, |
| 2218 |
.acpi_match_table = ACPI_PTR(sdhci_dwcmshc_acpi_ids), |
| 2219 |
.pm = pm_ptr(&dwcmshc_pmops), |
| 2220 |
}, |
| 2221 |
.probe = dwcmshc_probe, |
| 2222 |
.remove = dwcmshc_remove, |
| 2223 |
}; |
| 2224 |
module_platform_driver(sdhci_dwcmshc_driver); |
| 2225 |
|
| 2226 |
MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC"); |
| 2227 |
MODULE_AUTHOR("Jisheng Zhang <[email protected]>"); |
| 2228 |
MODULE_LICENSE("GPL v2"); |