1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Freescale eSDHC i.MX controller driver for the platform bus. 4 * 5 * derived from the OF-version. 6 * 7 * Copyright (c) 2010 Pengutronix e.K. 8 * Author: Wolfram Sang <kernel@pengutronix.de> 9 */ 10 11 #include <linux/bitfield.h> 12 #include <linux/io.h> 13 #include <linux/iopoll.h> 14 #include <linux/delay.h> 15 #include <linux/err.h> 16 #include <linux/clk.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/pm_qos.h> 20 #include <linux/mmc/host.h> 21 #include <linux/mmc/mmc.h> 22 #include <linux/mmc/sdio.h> 23 #include <linux/mmc/slot-gpio.h> 24 #include <linux/of.h> 25 #include <linux/platform_device.h> 26 #include <linux/pinctrl/consumer.h> 27 #include <linux/pm_runtime.h> 28 #include "sdhci-cqhci.h" 29 #include "sdhci-pltfm.h" 30 #include "sdhci-esdhc.h" 31 #include "cqhci.h" 32 33 #define ESDHC_SYS_CTRL_DTOCV_MASK GENMASK(19, 16) 34 #define ESDHC_SYS_CTRL_RST_FIFO BIT(22) 35 #define ESDHC_SYS_CTRL_IPP_RST_N BIT(23) 36 #define ESDHC_SYS_CTRL_RESET_TUNING BIT(28) 37 #define ESDHC_CTRL_D3CD 0x08 38 #define ESDHC_BURST_LEN_EN_INCR (1 << 27) 39 /* VENDOR SPEC register */ 40 #define ESDHC_VENDOR_SPEC 0xc0 41 #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1) 42 #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1) 43 #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8) 44 #define ESDHC_DEBUG_SEL_AND_STATUS_REG 0xc2 45 #define ESDHC_DEBUG_SEL_REG 0xc3 46 #define ESDHC_DEBUG_SEL_MASK 0xf 47 #define ESDHC_DEBUG_SEL_CMD_STATE 1 48 #define ESDHC_DEBUG_SEL_DATA_STATE 2 49 #define ESDHC_DEBUG_SEL_TRANS_STATE 3 50 #define ESDHC_DEBUG_SEL_DMA_STATE 4 51 #define ESDHC_DEBUG_SEL_ADMA_STATE 5 52 #define ESDHC_DEBUG_SEL_FIFO_STATE 6 53 #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE 7 54 #define ESDHC_WTMK_LVL 0x44 55 #define ESDHC_WTMK_DEFAULT_VAL 0x10401040 56 #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF 57 #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0 58 #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000 59 #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16 60 #define ESDHC_WTMK_LVL_WML_VAL_DEF 64 61 #define ESDHC_WTMK_LVL_WML_VAL_MAX 128 62 #define ESDHC_MIX_CTRL 0x48 63 #define ESDHC_MIX_CTRL_DDREN (1 << 3) 64 #define ESDHC_MIX_CTRL_AC23EN (1 << 7) 65 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22) 66 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23) 67 #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24) 68 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25) 69 #define ESDHC_MIX_CTRL_HS400_EN (1 << 26) 70 #define ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27) 71 /* Bits 3 and 6 are not SDHCI standard definitions */ 72 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7 73 /* Tuning bits */ 74 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000 75 76 /* dll control register */ 77 #define ESDHC_DLL_CTRL 0x60 78 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9 79 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8 80 81 /* tune control register */ 82 #define ESDHC_TUNE_CTRL_STATUS 0x68 83 #define ESDHC_TUNE_CTRL_STEP 1 84 #define ESDHC_TUNE_CTRL_MIN 0 85 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1) 86 #define ESDHC_TUNE_CTRL_STATUS_TAP_SEL_MASK GENMASK(30, 16) 87 #define ESDHC_TUNE_CTRL_STATUS_TAP_SEL_PRE_MASK GENMASK(30, 24) 88 #define ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK GENMASK(14, 8) 89 #define ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_MASK GENMASK(7, 4) 90 #define ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_MASK GENMASK(3, 0) 91 /* strobe dll register */ 92 #define ESDHC_STROBE_DLL_CTRL 0x70 93 #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0) 94 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1) 95 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT 0x7 96 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3 97 #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT (4 << 20) 98 99 #define ESDHC_STROBE_DLL_STATUS 0x74 100 #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1) 101 #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1 102 103 #define ESDHC_VEND_SPEC2 0xc8 104 #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ (1 << 8) 105 #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN (1 << 4) 106 #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN (0 << 4) 107 #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN (2 << 4) 108 #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN (1 << 6) 109 #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK (7 << 4) 110 111 #define ESDHC_TUNING_CTRL 0xcc 112 #define ESDHC_STD_TUNING_EN (1 << 24) 113 #define ESDHC_TUNING_WINDOW_MASK GENMASK(22, 20) 114 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */ 115 #define ESDHC_TUNING_START_TAP_DEFAULT 0x1 116 #define ESDHC_TUNING_START_TAP_MASK 0x7f 117 #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE (1 << 7) 118 #define ESDHC_TUNING_STEP_DEFAULT 0x1 119 #define ESDHC_TUNING_STEP_MASK 0x00070000 120 #define ESDHC_TUNING_STEP_SHIFT 16 121 122 /* pinctrl state */ 123 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz" 124 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz" 125 126 /* 127 * Our interpretation of the SDHCI_HOST_CONTROL register 128 */ 129 #define ESDHC_CTRL_4BITBUS (0x1 << 1) 130 #define ESDHC_CTRL_8BITBUS (0x2 << 1) 131 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1) 132 #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK) 133 134 /* 135 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC: 136 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design, 137 * but bit28 is used as the INT DMA ERR in fsl eSDHC design. 138 * Define this macro DMA error INT for fsl eSDHC 139 */ 140 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28) 141 142 /* the address offset of CQHCI */ 143 #define ESDHC_CQHCI_ADDR_OFFSET 0x100 144 145 /* 146 * The CMDTYPE of the CMD register (offset 0xE) should be set to 147 * "11" when the STOP CMD12 is issued on imx53 to abort one 148 * open ended multi-blk IO. Otherwise the TC INT wouldn't 149 * be generated. 150 * In exact block transfer, the controller doesn't complete the 151 * operations automatically as required at the end of the 152 * transfer and remains on hold if the abort command is not sent. 153 * As a result, the TC flag is not asserted and SW received timeout 154 * exception. Bit1 of Vendor Spec register is used to fix it. 155 */ 156 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1) 157 /* 158 * The flag tells that the ESDHC controller is an USDHC block that is 159 * integrated on the i.MX6 series. 160 */ 161 #define ESDHC_FLAG_USDHC BIT(3) 162 /* The IP supports manual tuning process */ 163 #define ESDHC_FLAG_MAN_TUNING BIT(4) 164 /* The IP supports standard tuning process */ 165 #define ESDHC_FLAG_STD_TUNING BIT(5) 166 /* The IP has SDHCI_CAPABILITIES_1 register */ 167 #define ESDHC_FLAG_HAVE_CAP1 BIT(6) 168 /* 169 * The IP has erratum ERR004536 170 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow, 171 * when reading data from the card 172 * This flag is also set for i.MX25 and i.MX35 in order to get 173 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits). 174 */ 175 #define ESDHC_FLAG_ERR004536 BIT(7) 176 /* The IP supports HS200 mode */ 177 #define ESDHC_FLAG_HS200 BIT(8) 178 /* The IP supports HS400 mode */ 179 #define ESDHC_FLAG_HS400 BIT(9) 180 /* 181 * The IP has errata ERR010450 182 * uSDHC: At 1.8V due to the I/O timing limit, for SDR mode, SD card 183 * clock can't exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz. 184 */ 185 #define ESDHC_FLAG_ERR010450 BIT(10) 186 /* The IP supports HS400ES mode */ 187 #define ESDHC_FLAG_HS400_ES BIT(11) 188 /* The IP has Host Controller Interface for Command Queuing */ 189 #define ESDHC_FLAG_CQHCI BIT(12) 190 /* need request pmqos during low power */ 191 #define ESDHC_FLAG_PMQOS BIT(13) 192 /* The IP state got lost in low power mode */ 193 #define ESDHC_FLAG_STATE_LOST_IN_LPMODE BIT(14) 194 /* The IP lost clock rate in PM_RUNTIME */ 195 #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME BIT(15) 196 /* 197 * The IP do not support the ACMD23 feature completely when use ADMA mode. 198 * In ADMA mode, it only use the 16 bit block count of the register 0x4 199 * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will 200 * ignore the upper 16 bit of the CMD23's argument. This will block the reliable 201 * write operation in RPMB, because RPMB reliable write need to set the bit31 202 * of the CMD23's argument. 203 * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA 204 * do not has this limitation. so when these SoC use ADMA mode, it need to 205 * disable the ACMD23 feature. 206 */ 207 #define ESDHC_FLAG_BROKEN_AUTO_CMD23 BIT(16) 208 209 /* ERR004536 is not applicable for the IP */ 210 #define ESDHC_FLAG_SKIP_ERR004536 BIT(17) 211 212 /* The IP does not have GPIO CD wake capabilities */ 213 #define ESDHC_FLAG_SKIP_CD_WAKE BIT(18) 214 215 /* the controller has dummy pad for clock loopback */ 216 #define ESDHC_FLAG_DUMMY_PAD BIT(19) 217 218 #define ESDHC_AUTO_TUNING_WINDOW 3 219 220 enum wp_types { 221 ESDHC_WP_NONE, /* no WP, neither controller nor gpio */ 222 ESDHC_WP_CONTROLLER, /* mmc controller internal WP */ 223 ESDHC_WP_GPIO, /* external gpio pin for WP */ 224 }; 225 226 enum cd_types { 227 ESDHC_CD_NONE, /* no CD, neither controller nor gpio */ 228 ESDHC_CD_CONTROLLER, /* mmc controller internal CD */ 229 ESDHC_CD_GPIO, /* external gpio pin for CD */ 230 ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */ 231 }; 232 233 /* 234 * struct esdhc_platform_data - platform data for esdhc on i.MX 235 * 236 * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35. 237 * 238 * @wp_type: type of write_protect method (see wp_types enum above) 239 * @cd_type: type of card_detect method (see cd_types enum above) 240 */ 241 242 struct esdhc_platform_data { 243 enum wp_types wp_type; 244 enum cd_types cd_type; 245 int max_bus_width; 246 unsigned int delay_line; 247 unsigned int tuning_step; /* The delay cell steps in tuning procedure */ 248 unsigned int tuning_start_tap; /* The start delay cell point in tuning procedure */ 249 unsigned int strobe_dll_delay_target; /* The delay cell for strobe pad (read clock) */ 250 unsigned int saved_tuning_delay_cell; /* save the value of tuning delay cell */ 251 unsigned int saved_auto_tuning_window; /* save the auto tuning window width */ 252 }; 253 254 struct esdhc_soc_data { 255 u32 flags; 256 u32 quirks; 257 }; 258 259 static const struct esdhc_soc_data esdhc_imx25_data = { 260 .flags = ESDHC_FLAG_ERR004536, 261 }; 262 263 static const struct esdhc_soc_data esdhc_imx35_data = { 264 .flags = ESDHC_FLAG_ERR004536, 265 }; 266 267 static const struct esdhc_soc_data esdhc_imx51_data = { 268 .flags = 0, 269 }; 270 271 static const struct esdhc_soc_data esdhc_imx53_data = { 272 .flags = ESDHC_FLAG_MULTIBLK_NO_INT, 273 }; 274 275 static const struct esdhc_soc_data usdhc_imx6q_data = { 276 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 277 | ESDHC_FLAG_BROKEN_AUTO_CMD23, 278 }; 279 280 static const struct esdhc_soc_data usdhc_imx6sl_data = { 281 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 282 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536 283 | ESDHC_FLAG_HS200 284 | ESDHC_FLAG_BROKEN_AUTO_CMD23, 285 }; 286 287 static const struct esdhc_soc_data usdhc_imx6sll_data = { 288 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 289 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 290 | ESDHC_FLAG_HS400 291 | ESDHC_FLAG_STATE_LOST_IN_LPMODE, 292 }; 293 294 static const struct esdhc_soc_data usdhc_imx6sx_data = { 295 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 296 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 297 | ESDHC_FLAG_STATE_LOST_IN_LPMODE 298 | ESDHC_FLAG_BROKEN_AUTO_CMD23, 299 }; 300 301 static const struct esdhc_soc_data usdhc_imx6ull_data = { 302 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 303 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 304 | ESDHC_FLAG_ERR010450 305 | ESDHC_FLAG_STATE_LOST_IN_LPMODE, 306 }; 307 308 static const struct esdhc_soc_data usdhc_imx7d_data = { 309 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 310 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 311 | ESDHC_FLAG_HS400 312 | ESDHC_FLAG_STATE_LOST_IN_LPMODE 313 | ESDHC_FLAG_BROKEN_AUTO_CMD23, 314 }; 315 316 static struct esdhc_soc_data usdhc_s32g2_data = { 317 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 318 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 319 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES 320 | ESDHC_FLAG_SKIP_ERR004536 | ESDHC_FLAG_SKIP_CD_WAKE, 321 .quirks = SDHCI_QUIRK_NO_LED, 322 }; 323 324 static struct esdhc_soc_data usdhc_imx7ulp_data = { 325 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 326 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 327 | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400 328 | ESDHC_FLAG_STATE_LOST_IN_LPMODE, 329 .quirks = SDHCI_QUIRK_NO_LED, 330 }; 331 static struct esdhc_soc_data usdhc_imxrt1050_data = { 332 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING 333 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200, 334 .quirks = SDHCI_QUIRK_NO_LED, 335 }; 336 337 static struct esdhc_soc_data usdhc_imx8qxp_data = { 338 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 339 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 340 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES 341 | ESDHC_FLAG_STATE_LOST_IN_LPMODE 342 | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME, 343 .quirks = SDHCI_QUIRK_NO_LED, 344 }; 345 346 static struct esdhc_soc_data usdhc_imx8mm_data = { 347 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 348 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 349 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES 350 | ESDHC_FLAG_STATE_LOST_IN_LPMODE, 351 .quirks = SDHCI_QUIRK_NO_LED, 352 }; 353 354 static struct esdhc_soc_data usdhc_imx95_data = { 355 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING 356 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 357 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES 358 | ESDHC_FLAG_STATE_LOST_IN_LPMODE 359 | ESDHC_FLAG_DUMMY_PAD, 360 .quirks = SDHCI_QUIRK_NO_LED, 361 }; 362 363 struct pltfm_imx_data { 364 u32 scratchpad; 365 struct pinctrl *pinctrl; 366 struct pinctrl_state *pins_100mhz; 367 struct pinctrl_state *pins_200mhz; 368 const struct esdhc_soc_data *socdata; 369 struct esdhc_platform_data boarddata; 370 struct clk *clk_ipg; 371 struct clk *clk_ahb; 372 struct clk *clk_per; 373 unsigned int actual_clock; 374 375 /* 376 * USDHC has one limition, require the SDIO device a different 377 * register setting. Driver has to recognize card type during 378 * the card init, but at this stage, mmc_host->card is not 379 * available. So involve this field to save the card type 380 * during card init through usdhc_init_card(). 381 */ 382 unsigned int init_card_type; 383 384 enum { 385 NO_CMD_PENDING, /* no multiblock command pending */ 386 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */ 387 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */ 388 } multiblock_status; 389 u32 is_ddr; 390 struct pm_qos_request pm_qos_req; 391 }; 392 393 static const struct of_device_id imx_esdhc_dt_ids[] = { 394 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, }, 395 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, }, 396 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, }, 397 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, }, 398 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, }, 399 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, }, 400 { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, }, 401 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, }, 402 { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, }, 403 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, }, 404 { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, }, 405 { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, }, 406 { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, }, 407 { .compatible = "fsl,imx94-usdhc", .data = &usdhc_imx95_data, }, 408 { .compatible = "fsl,imx95-usdhc", .data = &usdhc_imx95_data, }, 409 { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, }, 410 { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, }, 411 { /* sentinel */ } 412 }; 413 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids); 414 415 static inline int is_imx25_esdhc(struct pltfm_imx_data *data) 416 { 417 return data->socdata == &esdhc_imx25_data; 418 } 419 420 static inline int is_imx53_esdhc(struct pltfm_imx_data *data) 421 { 422 return data->socdata == &esdhc_imx53_data; 423 } 424 425 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data) 426 { 427 return !!(data->socdata->flags & ESDHC_FLAG_USDHC); 428 } 429 430 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg) 431 { 432 void __iomem *base = host->ioaddr + (reg & ~0x3); 433 u32 shift = (reg & 0x3) * 8; 434 435 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base); 436 } 437 438 #define DRIVER_NAME "sdhci-esdhc-imx" 439 #define ESDHC_IMX_DUMP(f, x...) \ 440 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x) 441 static void esdhc_dump_debug_regs(struct sdhci_host *host) 442 { 443 int i; 444 char *debug_status[7] = { 445 "cmd debug status", 446 "data debug status", 447 "trans debug status", 448 "dma debug status", 449 "adma debug status", 450 "fifo debug status", 451 "async fifo debug status" 452 }; 453 454 ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n"); 455 for (i = 0; i < 7; i++) { 456 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 457 ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG); 458 ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i], 459 readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG)); 460 } 461 462 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG); 463 464 } 465 466 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host) 467 { 468 u32 present_state; 469 int ret; 470 471 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state, 472 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100); 473 if (ret == -ETIMEDOUT) 474 dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__); 475 } 476 477 /* Enable the auto tuning circuit to check the CMD line and BUS line */ 478 static inline void usdhc_auto_tuning_mode_sel_and_en(struct sdhci_host *host) 479 { 480 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 481 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 482 u32 buswidth, auto_tune_buswidth; 483 u32 reg; 484 485 buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL)); 486 487 switch (buswidth) { 488 case ESDHC_CTRL_8BITBUS: 489 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN; 490 break; 491 case ESDHC_CTRL_4BITBUS: 492 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN; 493 break; 494 default: /* 1BITBUS */ 495 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN; 496 break; 497 } 498 499 /* 500 * For USDHC, auto tuning circuit can not handle the async sdio 501 * device interrupt correctly. When sdio device use 4 data lines, 502 * async sdio interrupt will use the shared DAT[1], if enable auto 503 * tuning circuit check these 4 data lines, include the DAT[1], 504 * this circuit will detect this interrupt, take this as a data on 505 * DAT[1], and adjust the delay cell wrongly. 506 * This is the hardware design limitation, to avoid this, for sdio 507 * device, config the auto tuning circuit only check DAT[0] and CMD 508 * line. 509 */ 510 if (imx_data->init_card_type == MMC_TYPE_SDIO) 511 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN; 512 513 esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK, 514 auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN, 515 ESDHC_VEND_SPEC2); 516 517 reg = readl(host->ioaddr + ESDHC_MIX_CTRL); 518 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN; 519 writel(reg, host->ioaddr + ESDHC_MIX_CTRL); 520 } 521 522 static u32 esdhc_readl_le(struct sdhci_host *host, int reg) 523 { 524 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 525 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 526 u32 val = readl(host->ioaddr + reg); 527 528 if (unlikely(reg == SDHCI_PRESENT_STATE)) { 529 u32 fsl_prss = val; 530 /* save the least 20 bits */ 531 val = fsl_prss & 0x000FFFFF; 532 /* move dat[0-3] bits */ 533 val |= (fsl_prss & 0x0F000000) >> 4; 534 /* move cmd line bit */ 535 val |= (fsl_prss & 0x00800000) << 1; 536 } 537 538 if (unlikely(reg == SDHCI_CAPABILITIES)) { 539 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */ 540 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1) 541 val &= 0xffff0000; 542 543 /* In FSL esdhc IC module, only bit20 is used to indicate the 544 * ADMA2 capability of esdhc, but this bit is messed up on 545 * some SOCs (e.g. on MX25, MX35 this bit is set, but they 546 * don't actually support ADMA2). So set the BROKEN_ADMA 547 * quirk on MX25/35 platforms. 548 */ 549 550 if (val & SDHCI_CAN_DO_ADMA1) { 551 val &= ~SDHCI_CAN_DO_ADMA1; 552 val |= SDHCI_CAN_DO_ADMA2; 553 } 554 } 555 556 if (unlikely(reg == SDHCI_CAPABILITIES_1)) { 557 if (esdhc_is_usdhc(imx_data)) { 558 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1) 559 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF; 560 else 561 /* imx6q/dl does not have cap_1 register, fake one */ 562 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104 563 | SDHCI_SUPPORT_SDR50 564 | SDHCI_USE_SDR50_TUNING 565 | FIELD_PREP(SDHCI_RETUNING_MODE_MASK, 566 SDHCI_TUNING_MODE_3); 567 568 /* 569 * Do not advertise faster UHS modes if there are no 570 * pinctrl states for 100MHz/200MHz. 571 */ 572 if (IS_ERR_OR_NULL(imx_data->pins_100mhz)) 573 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50); 574 if (IS_ERR_OR_NULL(imx_data->pins_200mhz)) 575 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400); 576 } 577 } 578 579 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) { 580 val = 0; 581 val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF); 582 val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF); 583 val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF); 584 } 585 586 if (unlikely(reg == SDHCI_INT_STATUS)) { 587 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) { 588 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR; 589 val |= SDHCI_INT_ADMA_ERROR; 590 } 591 592 /* 593 * mask off the interrupt we get in response to the manually 594 * sent CMD12 595 */ 596 if ((imx_data->multiblock_status == WAIT_FOR_INT) && 597 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) { 598 val &= ~SDHCI_INT_RESPONSE; 599 writel(SDHCI_INT_RESPONSE, host->ioaddr + 600 SDHCI_INT_STATUS); 601 imx_data->multiblock_status = NO_CMD_PENDING; 602 } 603 } 604 605 return val; 606 } 607 608 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg) 609 { 610 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 611 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 612 u32 data; 613 614 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE || 615 reg == SDHCI_INT_STATUS)) { 616 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) { 617 /* 618 * Clear and then set D3CD bit to avoid missing the 619 * card interrupt. This is an eSDHC controller problem 620 * so we need to apply the following workaround: clear 621 * and set D3CD bit will make eSDHC re-sample the card 622 * interrupt. In case a card interrupt was lost, 623 * re-sample it by the following steps. 624 */ 625 data = readl(host->ioaddr + SDHCI_HOST_CONTROL); 626 data &= ~ESDHC_CTRL_D3CD; 627 writel(data, host->ioaddr + SDHCI_HOST_CONTROL); 628 data |= ESDHC_CTRL_D3CD; 629 writel(data, host->ioaddr + SDHCI_HOST_CONTROL); 630 } 631 632 if (val & SDHCI_INT_ADMA_ERROR) { 633 val &= ~SDHCI_INT_ADMA_ERROR; 634 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR; 635 } 636 } 637 638 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT) 639 && (reg == SDHCI_INT_STATUS) 640 && (val & SDHCI_INT_DATA_END))) { 641 u32 v; 642 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 643 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK; 644 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC); 645 646 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS) 647 { 648 /* send a manual CMD12 with RESPTYP=none */ 649 data = MMC_STOP_TRANSMISSION << 24 | 650 SDHCI_CMD_ABORTCMD << 16; 651 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE); 652 imx_data->multiblock_status = WAIT_FOR_INT; 653 } 654 } 655 656 writel(val, host->ioaddr + reg); 657 } 658 659 static u16 esdhc_readw_le(struct sdhci_host *host, int reg) 660 { 661 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 662 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 663 u16 ret = 0; 664 u32 val; 665 666 if (unlikely(reg == SDHCI_HOST_VERSION)) { 667 reg ^= 2; 668 if (esdhc_is_usdhc(imx_data)) { 669 /* 670 * The usdhc register returns a wrong host version. 671 * Correct it here. 672 */ 673 return SDHCI_SPEC_300; 674 } 675 } 676 677 if (unlikely(reg == SDHCI_HOST_CONTROL2)) { 678 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 679 if (val & ESDHC_VENDOR_SPEC_VSELECT) 680 ret |= SDHCI_CTRL_VDD_180; 681 682 if (esdhc_is_usdhc(imx_data)) { 683 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) 684 val = readl(host->ioaddr + ESDHC_MIX_CTRL); 685 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) 686 /* the std tuning bits is in ACMD12_ERR for imx6sl */ 687 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS); 688 } 689 690 if (val & ESDHC_MIX_CTRL_EXE_TUNE) 691 ret |= SDHCI_CTRL_EXEC_TUNING; 692 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL) 693 ret |= SDHCI_CTRL_TUNED_CLK; 694 695 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE; 696 697 return ret; 698 } 699 700 if (unlikely(reg == SDHCI_TRANSFER_MODE)) { 701 if (esdhc_is_usdhc(imx_data)) { 702 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL); 703 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK; 704 /* Swap AC23 bit */ 705 if (m & ESDHC_MIX_CTRL_AC23EN) { 706 ret &= ~ESDHC_MIX_CTRL_AC23EN; 707 ret |= SDHCI_TRNS_AUTO_CMD23; 708 } 709 } else { 710 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE); 711 } 712 713 return ret; 714 } 715 716 return readw(host->ioaddr + reg); 717 } 718 719 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg) 720 { 721 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 722 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 723 u32 new_val = 0; 724 725 switch (reg) { 726 case SDHCI_CLOCK_CONTROL: 727 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 728 if (val & SDHCI_CLOCK_CARD_EN) 729 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON; 730 else 731 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON; 732 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC); 733 if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON)) 734 esdhc_wait_for_card_clock_gate_off(host); 735 return; 736 case SDHCI_HOST_CONTROL2: 737 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 738 if (val & SDHCI_CTRL_VDD_180) 739 new_val |= ESDHC_VENDOR_SPEC_VSELECT; 740 else 741 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT; 742 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC); 743 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) { 744 u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS); 745 if (val & SDHCI_CTRL_TUNED_CLK) 746 v |= ESDHC_MIX_CTRL_SMPCLK_SEL; 747 else 748 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL; 749 750 if (val & SDHCI_CTRL_EXEC_TUNING) 751 v |= ESDHC_MIX_CTRL_EXE_TUNE; 752 else 753 v &= ~ESDHC_MIX_CTRL_EXE_TUNE; 754 755 writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS); 756 } 757 return; 758 case SDHCI_TRANSFER_MODE: 759 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT) 760 && (host->cmd->opcode == SD_IO_RW_EXTENDED) 761 && (host->cmd->data->blocks > 1) 762 && (host->cmd->data->flags & MMC_DATA_READ)) { 763 u32 v; 764 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 765 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK; 766 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC); 767 } 768 769 if (esdhc_is_usdhc(imx_data)) { 770 u32 wml; 771 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL); 772 /* Swap AC23 bit */ 773 if (val & SDHCI_TRNS_AUTO_CMD23) { 774 val &= ~SDHCI_TRNS_AUTO_CMD23; 775 val |= ESDHC_MIX_CTRL_AC23EN; 776 } 777 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK); 778 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 779 780 /* Set watermark levels for PIO access to maximum value 781 * (128 words) to accommodate full 512 bytes buffer. 782 * For DMA access restore the levels to default value. 783 */ 784 m = readl(host->ioaddr + ESDHC_WTMK_LVL); 785 if (val & SDHCI_TRNS_DMA) { 786 wml = ESDHC_WTMK_LVL_WML_VAL_DEF; 787 } else { 788 u8 ctrl; 789 wml = ESDHC_WTMK_LVL_WML_VAL_MAX; 790 791 /* 792 * Since already disable DMA mode, so also need 793 * to clear the DMASEL. Otherwise, for standard 794 * tuning, when send tuning command, usdhc will 795 * still prefetch the ADMA script from wrong 796 * DMA address, then we will see IOMMU report 797 * some error which show lack of TLB mapping. 798 */ 799 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 800 ctrl &= ~SDHCI_CTRL_DMA_MASK; 801 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 802 } 803 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK | 804 ESDHC_WTMK_LVL_WR_WML_MASK); 805 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) | 806 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT); 807 writel(m, host->ioaddr + ESDHC_WTMK_LVL); 808 } else { 809 /* 810 * Postpone this write, we must do it together with a 811 * command write that is down below. 812 */ 813 imx_data->scratchpad = val; 814 } 815 return; 816 case SDHCI_COMMAND: 817 if (host->cmd->opcode == MMC_STOP_TRANSMISSION) 818 val |= SDHCI_CMD_ABORTCMD; 819 820 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) && 821 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)) 822 imx_data->multiblock_status = MULTIBLK_IN_PROCESS; 823 824 if (esdhc_is_usdhc(imx_data)) 825 writel(val << 16, 826 host->ioaddr + SDHCI_TRANSFER_MODE); 827 else 828 writel(val << 16 | imx_data->scratchpad, 829 host->ioaddr + SDHCI_TRANSFER_MODE); 830 return; 831 case SDHCI_BLOCK_SIZE: 832 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0); 833 break; 834 } 835 esdhc_clrset_le(host, 0xffff, val, reg); 836 } 837 838 static u8 esdhc_readb_le(struct sdhci_host *host, int reg) 839 { 840 u8 ret; 841 u32 val; 842 843 switch (reg) { 844 case SDHCI_HOST_CONTROL: 845 val = readl(host->ioaddr + reg); 846 847 ret = val & SDHCI_CTRL_LED; 848 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK; 849 ret |= (val & ESDHC_CTRL_4BITBUS); 850 ret |= (val & ESDHC_CTRL_8BITBUS) << 3; 851 return ret; 852 } 853 854 return readb(host->ioaddr + reg); 855 } 856 857 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg) 858 { 859 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 860 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 861 u32 new_val = 0; 862 u32 mask; 863 864 switch (reg) { 865 case SDHCI_POWER_CONTROL: 866 /* 867 * FSL put some DMA bits here 868 * If your board has a regulator, code should be here 869 */ 870 return; 871 case SDHCI_HOST_CONTROL: 872 /* FSL messed up here, so we need to manually compose it. */ 873 new_val = val & SDHCI_CTRL_LED; 874 /* ensure the endianness */ 875 new_val |= ESDHC_HOST_CONTROL_LE; 876 /* bits 8&9 are reserved on mx25 */ 877 if (!is_imx25_esdhc(imx_data)) { 878 /* DMA mode bits are shifted */ 879 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5; 880 } 881 882 /* 883 * Do not touch buswidth bits here. This is done in 884 * esdhc_pltfm_bus_width. 885 * Do not touch the D3CD bit either which is used for the 886 * SDIO interrupt erratum workaround. 887 */ 888 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD); 889 890 esdhc_clrset_le(host, mask, new_val, reg); 891 return; 892 case SDHCI_TIMEOUT_CONTROL: 893 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK, 894 FIELD_PREP(ESDHC_SYS_CTRL_DTOCV_MASK, val), 895 ESDHC_SYSTEM_CONTROL); 896 return; 897 case SDHCI_SOFTWARE_RESET: 898 if (val & SDHCI_RESET_DATA) 899 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL); 900 break; 901 } 902 esdhc_clrset_le(host, 0xff, val, reg); 903 904 if (reg == SDHCI_SOFTWARE_RESET) { 905 if (val & SDHCI_RESET_ALL) { 906 /* 907 * The esdhc has a design violation to SDHC spec which 908 * tells that software reset should not affect card 909 * detection circuit. But esdhc clears its SYSCTL 910 * register bits [0..2] during the software reset. This 911 * will stop those clocks that card detection circuit 912 * relies on. To work around it, we turn the clocks on 913 * back to keep card detection circuit functional. 914 */ 915 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL); 916 /* 917 * The reset on usdhc fails to clear MIX_CTRL register. 918 * Do it manually here. 919 */ 920 if (esdhc_is_usdhc(imx_data)) { 921 /* 922 * the tuning bits should be kept during reset 923 */ 924 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL); 925 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK, 926 host->ioaddr + ESDHC_MIX_CTRL); 927 imx_data->is_ddr = 0; 928 } 929 } else if (val & SDHCI_RESET_DATA) { 930 /* 931 * The eSDHC DAT line software reset clears at least the 932 * data transfer width on i.MX25, so make sure that the 933 * Host Control register is unaffected. 934 */ 935 esdhc_clrset_le(host, 0xff, new_val, 936 SDHCI_HOST_CONTROL); 937 } 938 } 939 } 940 941 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host) 942 { 943 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 944 945 return pltfm_host->clock; 946 } 947 948 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host) 949 { 950 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 951 952 return pltfm_host->clock / 256 / 16; 953 } 954 955 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host, 956 unsigned int clock) 957 { 958 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 959 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 960 unsigned int host_clock = pltfm_host->clock; 961 int ddr_pre_div = imx_data->is_ddr ? 2 : 1; 962 int pre_div = 1; 963 int div = 1; 964 int ret; 965 u32 temp, val; 966 967 if (esdhc_is_usdhc(imx_data)) { 968 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 969 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON, 970 host->ioaddr + ESDHC_VENDOR_SPEC); 971 esdhc_wait_for_card_clock_gate_off(host); 972 } 973 974 if (clock == 0) { 975 host->mmc->actual_clock = 0; 976 return; 977 } 978 979 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */ 980 if (is_imx53_esdhc(imx_data)) { 981 /* 982 * According to the i.MX53 reference manual, if DLLCTRL[10] can 983 * be set, then the controller is eSDHCv3, else it is eSDHCv2. 984 */ 985 val = readl(host->ioaddr + ESDHC_DLL_CTRL); 986 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL); 987 temp = readl(host->ioaddr + ESDHC_DLL_CTRL); 988 writel(val, host->ioaddr + ESDHC_DLL_CTRL); 989 if (temp & BIT(10)) 990 pre_div = 2; 991 } 992 993 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 994 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 995 | ESDHC_CLOCK_MASK); 996 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 997 998 if ((imx_data->socdata->flags & ESDHC_FLAG_ERR010450) && 999 (!(host->quirks2 & SDHCI_QUIRK2_NO_1_8_V))) { 1000 unsigned int max_clock; 1001 1002 max_clock = imx_data->is_ddr ? 45000000 : 150000000; 1003 1004 clock = min(clock, max_clock); 1005 } 1006 1007 while (host_clock / (16 * pre_div * ddr_pre_div) > clock && 1008 pre_div < 256) 1009 pre_div *= 2; 1010 1011 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16) 1012 div++; 1013 1014 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div); 1015 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n", 1016 clock, host->mmc->actual_clock); 1017 1018 pre_div >>= 1; 1019 div--; 1020 1021 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL); 1022 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN 1023 | (div << ESDHC_DIVIDER_SHIFT) 1024 | (pre_div << ESDHC_PREDIV_SHIFT)); 1025 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL); 1026 1027 /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */ 1028 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp, 1029 (temp & ESDHC_CLOCK_STABLE), 2, 100); 1030 if (ret == -ETIMEDOUT) 1031 dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n"); 1032 1033 if (esdhc_is_usdhc(imx_data)) { 1034 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC); 1035 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON, 1036 host->ioaddr + ESDHC_VENDOR_SPEC); 1037 } 1038 1039 } 1040 1041 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host) 1042 { 1043 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1044 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1045 struct esdhc_platform_data *boarddata = &imx_data->boarddata; 1046 1047 switch (boarddata->wp_type) { 1048 case ESDHC_WP_GPIO: 1049 return mmc_gpio_get_ro(host->mmc); 1050 case ESDHC_WP_CONTROLLER: 1051 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) & 1052 SDHCI_WRITE_PROTECT); 1053 case ESDHC_WP_NONE: 1054 break; 1055 } 1056 1057 return -ENOSYS; 1058 } 1059 1060 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width) 1061 { 1062 u32 ctrl; 1063 1064 switch (width) { 1065 case MMC_BUS_WIDTH_8: 1066 ctrl = ESDHC_CTRL_8BITBUS; 1067 break; 1068 case MMC_BUS_WIDTH_4: 1069 ctrl = ESDHC_CTRL_4BITBUS; 1070 break; 1071 default: 1072 ctrl = 0; 1073 break; 1074 } 1075 1076 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl, 1077 SDHCI_HOST_CONTROL); 1078 } 1079 1080 static void esdhc_reset_tuning(struct sdhci_host *host) 1081 { 1082 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1083 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1084 u32 ctrl, tuning_ctrl, sys_ctrl; 1085 int ret; 1086 1087 /* Reset the tuning circuit */ 1088 if (esdhc_is_usdhc(imx_data)) { 1089 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL); 1090 ctrl &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN; 1091 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) { 1092 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL; 1093 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL); 1094 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1095 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) { 1096 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL); 1097 /* 1098 * enable the std tuning just in case it cleared in 1099 * sdhc_esdhc_tuning_restore. 1100 */ 1101 tuning_ctrl = readl(host->ioaddr + ESDHC_TUNING_CTRL); 1102 if (!(tuning_ctrl & ESDHC_STD_TUNING_EN)) { 1103 tuning_ctrl |= ESDHC_STD_TUNING_EN; 1104 writel(tuning_ctrl, host->ioaddr + ESDHC_TUNING_CTRL); 1105 } 1106 1107 /* set the reset tuning bit */ 1108 sys_ctrl = readl(host->ioaddr + ESDHC_SYSTEM_CONTROL); 1109 sys_ctrl |= ESDHC_SYS_CTRL_RESET_TUNING; 1110 writel(sys_ctrl, host->ioaddr + ESDHC_SYSTEM_CONTROL); 1111 1112 ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS); 1113 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL; 1114 ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE; 1115 writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS); 1116 /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */ 1117 ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS, 1118 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50); 1119 if (ret == -ETIMEDOUT) 1120 dev_warn(mmc_dev(host->mmc), 1121 "Warning! clear execute tuning bit failed\n"); 1122 /* 1123 * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the 1124 * usdhc IP internal logic flag execute_tuning_with_clr_buf, which 1125 * will finally make sure the normal data transfer logic correct. 1126 */ 1127 ctrl = readl(host->ioaddr + SDHCI_INT_STATUS); 1128 ctrl |= SDHCI_INT_DATA_AVAIL; 1129 writel(ctrl, host->ioaddr + SDHCI_INT_STATUS); 1130 } 1131 } 1132 } 1133 1134 static void usdhc_init_card(struct mmc_host *mmc, struct mmc_card *card) 1135 { 1136 struct sdhci_host *host = mmc_priv(mmc); 1137 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1138 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1139 1140 imx_data->init_card_type = card->type; 1141 } 1142 1143 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode) 1144 { 1145 struct sdhci_host *host = mmc_priv(mmc); 1146 int err; 1147 1148 /* 1149 * i.MX uSDHC internally already uses a fixed optimized timing for 1150 * DDR50, normally does not require tuning for DDR50 mode. 1151 */ 1152 if (host->timing == MMC_TIMING_UHS_DDR50) 1153 return 0; 1154 1155 /* 1156 * Reset tuning circuit logic. If not, the previous tuning result 1157 * will impact current tuning, make current tuning can't set the 1158 * correct delay cell. 1159 */ 1160 esdhc_reset_tuning(host); 1161 err = sdhci_execute_tuning(mmc, opcode); 1162 /* If tuning done, enable auto tuning */ 1163 if (!err && !host->tuning_err) 1164 usdhc_auto_tuning_mode_sel_and_en(host); 1165 1166 return err; 1167 } 1168 1169 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val) 1170 { 1171 u32 reg, sys_ctrl; 1172 u8 sw_rst; 1173 int ret; 1174 1175 /* FIXME: delay a bit for card to be ready for next tuning due to errors */ 1176 mdelay(1); 1177 1178 /* IC suggest to reset USDHC before every tuning command */ 1179 esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET); 1180 ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst, 1181 !(sw_rst & SDHCI_RESET_ALL), 10, 100); 1182 if (ret == -ETIMEDOUT) 1183 dev_warn(mmc_dev(host->mmc), 1184 "warning! RESET_ALL never complete before sending tuning command\n"); 1185 1186 reg = readl(host->ioaddr + ESDHC_MIX_CTRL); 1187 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL; 1188 writel(reg, host->ioaddr + ESDHC_MIX_CTRL); 1189 writel(FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK, val), 1190 host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1191 dev_dbg(mmc_dev(host->mmc), 1192 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n", 1193 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS)); 1194 1195 /* set RST_FIFO to reset the async FIFO, and wat it to self-clear */ 1196 sys_ctrl = readl(host->ioaddr + ESDHC_SYSTEM_CONTROL); 1197 sys_ctrl |= ESDHC_SYS_CTRL_RST_FIFO; 1198 writel(sys_ctrl, host->ioaddr + ESDHC_SYSTEM_CONTROL); 1199 ret = readl_poll_timeout(host->ioaddr + ESDHC_SYSTEM_CONTROL, sys_ctrl, 1200 !(sys_ctrl & ESDHC_SYS_CTRL_RST_FIFO), 10, 100); 1201 if (ret == -ETIMEDOUT) 1202 dev_warn(mmc_dev(host->mmc), 1203 "warning! RST_FIFO not clear in 100us\n"); 1204 } 1205 1206 static void esdhc_post_tuning(struct sdhci_host *host) 1207 { 1208 u32 reg; 1209 1210 reg = readl(host->ioaddr + ESDHC_MIX_CTRL); 1211 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE; 1212 writel(reg, host->ioaddr + ESDHC_MIX_CTRL); 1213 } 1214 1215 /* 1216 * find the largest pass window, and use the average delay of this 1217 * largest window to get the best timing. 1218 */ 1219 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode) 1220 { 1221 int min, max, avg, ret; 1222 int win_length, target_min, target_max, target_win_length; 1223 u32 clk_tune_ctrl_status, temp; 1224 1225 min = target_min = ESDHC_TUNE_CTRL_MIN; 1226 max = target_max = ESDHC_TUNE_CTRL_MIN; 1227 target_win_length = 0; 1228 while (max < ESDHC_TUNE_CTRL_MAX) { 1229 /* find the mininum delay first which can pass tuning */ 1230 while (min < ESDHC_TUNE_CTRL_MAX) { 1231 esdhc_prepare_tuning(host, min); 1232 if (!mmc_send_tuning(host->mmc, opcode, NULL)) 1233 break; 1234 min += ESDHC_TUNE_CTRL_STEP; 1235 } 1236 1237 /* find the maxinum delay which can not pass tuning */ 1238 max = min + ESDHC_TUNE_CTRL_STEP; 1239 while (max < ESDHC_TUNE_CTRL_MAX) { 1240 esdhc_prepare_tuning(host, max); 1241 if (mmc_send_tuning(host->mmc, opcode, NULL)) { 1242 max -= ESDHC_TUNE_CTRL_STEP; 1243 break; 1244 } 1245 max += ESDHC_TUNE_CTRL_STEP; 1246 } 1247 1248 win_length = max - min + 1; 1249 /* get the largest pass window */ 1250 if (win_length > target_win_length) { 1251 target_win_length = win_length; 1252 target_min = min; 1253 target_max = max; 1254 } 1255 1256 /* continue to find the next pass window */ 1257 min = max + ESDHC_TUNE_CTRL_STEP; 1258 } 1259 1260 /* use average delay to get the best timing */ 1261 avg = (target_min + target_max) / 2; 1262 esdhc_prepare_tuning(host, avg); 1263 1264 /* 1265 * adjust the delay according to tuning window, make preparation 1266 * for the auto-tuning logic. According to hardware suggest, need 1267 * to config the auto tuning window width to 3, to make the auto 1268 * tuning logic have enough space to handle the sample point shift 1269 * caused by temperature change. 1270 */ 1271 clk_tune_ctrl_status = FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK, 1272 avg - ESDHC_AUTO_TUNING_WINDOW) | 1273 FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_MASK, 1274 ESDHC_AUTO_TUNING_WINDOW) | 1275 FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_MASK, 1276 ESDHC_AUTO_TUNING_WINDOW); 1277 1278 writel(clk_tune_ctrl_status, host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1279 ret = readl_poll_timeout(host->ioaddr + ESDHC_TUNE_CTRL_STATUS, temp, 1280 clk_tune_ctrl_status == 1281 FIELD_GET(ESDHC_TUNE_CTRL_STATUS_TAP_SEL_MASK, temp), 1282 1, 10); 1283 if (ret == -ETIMEDOUT) 1284 dev_warn(mmc_dev(host->mmc), 1285 "clock tuning control status not set in 10us\n"); 1286 1287 ret = mmc_send_tuning(host->mmc, opcode, NULL); 1288 esdhc_post_tuning(host); 1289 1290 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n", 1291 ret ? "failed" : "passed", avg, ret); 1292 1293 return ret; 1294 } 1295 1296 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios) 1297 { 1298 struct sdhci_host *host = mmc_priv(mmc); 1299 u32 m; 1300 1301 m = readl(host->ioaddr + ESDHC_MIX_CTRL); 1302 if (ios->enhanced_strobe) 1303 m |= ESDHC_MIX_CTRL_HS400_ES_EN; 1304 else 1305 m &= ~ESDHC_MIX_CTRL_HS400_ES_EN; 1306 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 1307 } 1308 1309 static int esdhc_change_pinstate(struct sdhci_host *host, 1310 unsigned int uhs) 1311 { 1312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1313 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1314 struct pinctrl_state *pinctrl; 1315 1316 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs); 1317 1318 if (IS_ERR(imx_data->pinctrl) || 1319 IS_ERR(imx_data->pins_100mhz) || 1320 IS_ERR(imx_data->pins_200mhz)) 1321 return -EINVAL; 1322 1323 switch (uhs) { 1324 case MMC_TIMING_UHS_SDR50: 1325 case MMC_TIMING_UHS_DDR50: 1326 pinctrl = imx_data->pins_100mhz; 1327 break; 1328 case MMC_TIMING_UHS_SDR104: 1329 case MMC_TIMING_MMC_HS200: 1330 case MMC_TIMING_MMC_HS400: 1331 pinctrl = imx_data->pins_200mhz; 1332 break; 1333 default: 1334 /* back to default state for other legacy timing */ 1335 return pinctrl_select_default_state(mmc_dev(host->mmc)); 1336 } 1337 1338 return pinctrl_select_state(imx_data->pinctrl, pinctrl); 1339 } 1340 1341 /* 1342 * For HS400 eMMC, there is a data_strobe line. This signal is generated 1343 * by the device and used for data output and CRC status response output 1344 * in HS400 mode. The frequency of this signal follows the frequency of 1345 * CLK generated by host. The host receives the data which is aligned to the 1346 * edge of data_strobe line. Due to the time delay between CLK line and 1347 * data_strobe line, if the delay time is larger than one clock cycle, 1348 * then CLK and data_strobe line will be misaligned, read error shows up. 1349 */ 1350 static void esdhc_set_strobe_dll(struct sdhci_host *host) 1351 { 1352 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1353 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1354 u32 strobe_delay; 1355 u32 v; 1356 int ret; 1357 1358 /* disable clock before enabling strobe dll */ 1359 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) & 1360 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON, 1361 host->ioaddr + ESDHC_VENDOR_SPEC); 1362 esdhc_wait_for_card_clock_gate_off(host); 1363 1364 /* force a reset on strobe dll */ 1365 writel(ESDHC_STROBE_DLL_CTRL_RESET, 1366 host->ioaddr + ESDHC_STROBE_DLL_CTRL); 1367 /* clear the reset bit on strobe dll before any setting */ 1368 writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL); 1369 1370 /* 1371 * enable strobe dll ctrl and adjust the delay target 1372 * for the uSDHC loopback read clock 1373 */ 1374 if (imx_data->boarddata.strobe_dll_delay_target) 1375 strobe_delay = imx_data->boarddata.strobe_dll_delay_target; 1376 else 1377 strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT; 1378 v = ESDHC_STROBE_DLL_CTRL_ENABLE | 1379 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT | 1380 (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT); 1381 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL); 1382 1383 /* wait max 50us to get the REF/SLV lock */ 1384 ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v, 1385 ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50); 1386 if (ret == -ETIMEDOUT) 1387 dev_warn(mmc_dev(host->mmc), 1388 "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v); 1389 } 1390 1391 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing) 1392 { 1393 u32 m; 1394 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1395 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1396 struct esdhc_platform_data *boarddata = &imx_data->boarddata; 1397 1398 /* disable ddr mode and disable HS400 mode */ 1399 m = readl(host->ioaddr + ESDHC_MIX_CTRL); 1400 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN); 1401 imx_data->is_ddr = 0; 1402 1403 switch (timing) { 1404 case MMC_TIMING_UHS_SDR12: 1405 case MMC_TIMING_UHS_SDR25: 1406 case MMC_TIMING_UHS_SDR50: 1407 case MMC_TIMING_UHS_SDR104: 1408 case MMC_TIMING_MMC_HS: 1409 case MMC_TIMING_MMC_HS200: 1410 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 1411 break; 1412 case MMC_TIMING_UHS_DDR50: 1413 case MMC_TIMING_MMC_DDR52: 1414 m |= ESDHC_MIX_CTRL_DDREN; 1415 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 1416 imx_data->is_ddr = 1; 1417 if (boarddata->delay_line) { 1418 u32 v; 1419 v = boarddata->delay_line << 1420 ESDHC_DLL_OVERRIDE_VAL_SHIFT | 1421 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT); 1422 if (is_imx53_esdhc(imx_data)) 1423 v <<= 1; 1424 writel(v, host->ioaddr + ESDHC_DLL_CTRL); 1425 } 1426 break; 1427 case MMC_TIMING_MMC_HS400: 1428 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN; 1429 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 1430 imx_data->is_ddr = 1; 1431 /* update clock after enable DDR for strobe DLL lock */ 1432 host->ops->set_clock(host, host->clock); 1433 esdhc_set_strobe_dll(host); 1434 break; 1435 case MMC_TIMING_LEGACY: 1436 default: 1437 esdhc_reset_tuning(host); 1438 break; 1439 } 1440 1441 if (!(imx_data->socdata->flags & ESDHC_FLAG_DUMMY_PAD) && 1442 (timing == MMC_TIMING_UHS_SDR104 || 1443 timing == MMC_TIMING_MMC_HS200 || 1444 timing == MMC_TIMING_MMC_HS400)) 1445 m |= ESDHC_MIX_CTRL_FBCLK_SEL; 1446 else 1447 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL; 1448 1449 writel(m, host->ioaddr + ESDHC_MIX_CTRL); 1450 1451 esdhc_change_pinstate(host, timing); 1452 } 1453 1454 static void esdhc_reset(struct sdhci_host *host, u8 mask) 1455 { 1456 sdhci_and_cqhci_reset(host, mask); 1457 1458 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); 1459 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); 1460 } 1461 1462 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host) 1463 { 1464 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1465 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1466 1467 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */ 1468 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27; 1469 } 1470 1471 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask) 1472 { 1473 int cmd_error = 0; 1474 int data_error = 0; 1475 1476 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error)) 1477 return intmask; 1478 1479 cqhci_irq(host->mmc, intmask, cmd_error, data_error); 1480 1481 return 0; 1482 } 1483 1484 static void esdhc_hw_reset(struct sdhci_host *host) 1485 { 1486 esdhc_clrset_le(host, ESDHC_SYS_CTRL_IPP_RST_N, 0, ESDHC_SYSTEM_CONTROL); 1487 /* eMMC spec requires minimum 1us, here delay between 1-10us */ 1488 usleep_range(1, 10); 1489 esdhc_clrset_le(host, ESDHC_SYS_CTRL_IPP_RST_N, 1490 ESDHC_SYS_CTRL_IPP_RST_N, ESDHC_SYSTEM_CONTROL); 1491 /* eMMC spec requires minimum 200us, here delay between 200-300us */ 1492 usleep_range(200, 300); 1493 } 1494 1495 static struct sdhci_ops sdhci_esdhc_ops = { 1496 .read_l = esdhc_readl_le, 1497 .read_w = esdhc_readw_le, 1498 .read_b = esdhc_readb_le, 1499 .write_l = esdhc_writel_le, 1500 .write_w = esdhc_writew_le, 1501 .write_b = esdhc_writeb_le, 1502 .set_clock = esdhc_pltfm_set_clock, 1503 .get_max_clock = esdhc_pltfm_get_max_clock, 1504 .get_min_clock = esdhc_pltfm_get_min_clock, 1505 .get_max_timeout_count = esdhc_get_max_timeout_count, 1506 .get_ro = esdhc_pltfm_get_ro, 1507 .set_bus_width = esdhc_pltfm_set_bus_width, 1508 .set_uhs_signaling = esdhc_set_uhs_signaling, 1509 .reset = esdhc_reset, 1510 .irq = esdhc_cqhci_irq, 1511 .dump_vendor_regs = esdhc_dump_debug_regs, 1512 .hw_reset = esdhc_hw_reset, 1513 }; 1514 1515 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = { 1516 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT 1517 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC 1518 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC 1519 | SDHCI_QUIRK_BROKEN_CARD_DETECTION, 1520 .ops = &sdhci_esdhc_ops, 1521 }; 1522 1523 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host) 1524 { 1525 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1526 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1527 struct cqhci_host *cq_host = host->mmc->cqe_private; 1528 u32 tmp; 1529 1530 if (esdhc_is_usdhc(imx_data)) { 1531 /* 1532 * The imx6q ROM code will change the default watermark 1533 * level setting to something insane. Change it back here. 1534 */ 1535 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL); 1536 1537 /* 1538 * ROM code will change the bit burst_length_enable setting 1539 * to zero if this usdhc is chosen to boot system. Change 1540 * it back here, otherwise it will impact the performance a 1541 * lot. This bit is used to enable/disable the burst length 1542 * for the external AHB2AXI bridge. It's useful especially 1543 * for INCR transfer because without burst length indicator, 1544 * the AHB2AXI bridge does not know the burst length in 1545 * advance. And without burst length indicator, AHB INCR 1546 * transfer can only be converted to singles on the AXI side. 1547 */ 1548 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL) 1549 | ESDHC_BURST_LEN_EN_INCR, 1550 host->ioaddr + SDHCI_HOST_CONTROL); 1551 1552 /* 1553 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL 1554 * TO1.1, it's harmless for MX6SL 1555 */ 1556 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) { 1557 writel(readl(host->ioaddr + 0x6c) & ~BIT(7), 1558 host->ioaddr + 0x6c); 1559 } 1560 1561 /* disable DLL_CTRL delay line settings */ 1562 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL); 1563 1564 /* 1565 * For the case of command with busy, if set the bit 1566 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a 1567 * transfer complete interrupt when busy is deasserted. 1568 * When CQHCI use DCMD to send a CMD need R1b respons, 1569 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ, 1570 * otherwise DCMD will always meet timeout waiting for 1571 * hardware interrupt issue. 1572 */ 1573 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) { 1574 tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2); 1575 tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ; 1576 writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2); 1577 1578 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ; 1579 } 1580 1581 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) { 1582 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL); 1583 tmp |= ESDHC_STD_TUNING_EN; 1584 1585 /* 1586 * ROM code or bootloader may config the start tap 1587 * and step, unmask them first. 1588 */ 1589 tmp &= ~(ESDHC_TUNING_START_TAP_MASK | ESDHC_TUNING_STEP_MASK); 1590 if (imx_data->boarddata.tuning_start_tap) 1591 tmp |= imx_data->boarddata.tuning_start_tap; 1592 else 1593 tmp |= ESDHC_TUNING_START_TAP_DEFAULT; 1594 1595 if (imx_data->boarddata.tuning_step) { 1596 tmp |= imx_data->boarddata.tuning_step 1597 << ESDHC_TUNING_STEP_SHIFT; 1598 } else { 1599 tmp |= ESDHC_TUNING_STEP_DEFAULT 1600 << ESDHC_TUNING_STEP_SHIFT; 1601 } 1602 1603 /* 1604 * Config the tuning window to the hardware suggested value 3. 1605 * This tuning window is used for auto tuning logic. The default 1606 * tuning window is 2, here change to 3 make the window a bit 1607 * wider, give auto tuning enough space to handle the sample 1608 * point shift cause by temperature change. 1609 */ 1610 tmp &= ~ESDHC_TUNING_WINDOW_MASK; 1611 tmp |= FIELD_PREP(ESDHC_TUNING_WINDOW_MASK, ESDHC_AUTO_TUNING_WINDOW); 1612 1613 /* Disable the CMD CRC check for tuning, if not, need to 1614 * add some delay after every tuning command, because 1615 * hardware standard tuning logic will directly go to next 1616 * step once it detect the CMD CRC error, will not wait for 1617 * the card side to finally send out the tuning data, trigger 1618 * the buffer read ready interrupt immediately. If usdhc send 1619 * the next tuning command some eMMC card will stuck, can't 1620 * response, block the tuning procedure or the first command 1621 * after the whole tuning procedure always can't get any response. 1622 */ 1623 tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE; 1624 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL); 1625 } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) { 1626 /* 1627 * ESDHC_STD_TUNING_EN may be configured in bootloader 1628 * or ROM code, so clear this bit here to make sure 1629 * the manual tuning can work. 1630 */ 1631 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL); 1632 tmp &= ~ESDHC_STD_TUNING_EN; 1633 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL); 1634 } 1635 1636 /* 1637 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card 1638 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let 1639 * the 1st linux configure power/clock for the 2nd Linux. 1640 * 1641 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux 1642 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump. 1643 * After we clear the pending interrupt and halt CQCTL, issue gone. 1644 */ 1645 if (cq_host) { 1646 tmp = cqhci_readl(cq_host, CQHCI_IS); 1647 cqhci_writel(cq_host, tmp, CQHCI_IS); 1648 cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL); 1649 } 1650 } 1651 } 1652 1653 #ifdef CONFIG_PM_SLEEP 1654 static void sdhc_esdhc_tuning_save(struct sdhci_host *host) 1655 { 1656 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1657 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1658 u32 reg; 1659 1660 /* 1661 * SD/eMMC do not need this tuning save because it will re-init 1662 * after system resume back. 1663 * Here save the tuning delay value for SDIO device since it may 1664 * keep power during system PM. And for usdhc, only SDR50 and 1665 * SDR104 mode for SDIO device need to do tuning, and need to 1666 * save/restore. 1667 */ 1668 if (host->timing == MMC_TIMING_UHS_SDR50 || 1669 host->timing == MMC_TIMING_UHS_SDR104) { 1670 reg = readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1671 reg = FIELD_GET(ESDHC_TUNE_CTRL_STATUS_TAP_SEL_PRE_MASK, reg); 1672 imx_data->boarddata.saved_tuning_delay_cell = reg; 1673 } 1674 } 1675 1676 static void sdhc_esdhc_tuning_restore(struct sdhci_host *host) 1677 { 1678 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1679 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 1680 u32 reg; 1681 1682 if (host->timing == MMC_TIMING_UHS_SDR50 || 1683 host->timing == MMC_TIMING_UHS_SDR104) { 1684 /* 1685 * restore the tuning delay value actually is a 1686 * manual tuning method, so clear the standard 1687 * tuning enable bit here. Will set back this 1688 * ESDHC_STD_TUNING_EN in esdhc_reset_tuning() 1689 * when trigger re-tuning. 1690 */ 1691 reg = readl(host->ioaddr + ESDHC_TUNING_CTRL); 1692 reg &= ~ESDHC_STD_TUNING_EN; 1693 writel(reg, host->ioaddr + ESDHC_TUNING_CTRL); 1694 1695 reg = readl(host->ioaddr + ESDHC_MIX_CTRL); 1696 reg |= ESDHC_MIX_CTRL_SMPCLK_SEL; 1697 if (!(imx_data->socdata->flags & ESDHC_FLAG_DUMMY_PAD)) 1698 reg |= ESDHC_MIX_CTRL_FBCLK_SEL; 1699 writel(reg, host->ioaddr + ESDHC_MIX_CTRL); 1700 1701 writel(FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_PRE_MASK, 1702 imx_data->boarddata.saved_tuning_delay_cell) | 1703 FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_OUT_MASK, 1704 ESDHC_AUTO_TUNING_WINDOW) | 1705 FIELD_PREP(ESDHC_TUNE_CTRL_STATUS_DLY_CELL_SET_POST_MASK, 1706 ESDHC_AUTO_TUNING_WINDOW), 1707 host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1708 } 1709 } 1710 #endif 1711 1712 static void esdhc_cqe_enable(struct mmc_host *mmc) 1713 { 1714 struct sdhci_host *host = mmc_priv(mmc); 1715 struct cqhci_host *cq_host = mmc->cqe_private; 1716 u32 reg; 1717 u16 mode; 1718 int count = 10; 1719 1720 /* 1721 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be 1722 * the case after tuning, so ensure the buffer is drained. 1723 */ 1724 reg = sdhci_readl(host, SDHCI_PRESENT_STATE); 1725 while (reg & SDHCI_DATA_AVAILABLE) { 1726 sdhci_readl(host, SDHCI_BUFFER); 1727 reg = sdhci_readl(host, SDHCI_PRESENT_STATE); 1728 if (count-- == 0) { 1729 dev_warn(mmc_dev(host->mmc), 1730 "CQE may get stuck because the Buffer Read Enable bit is set\n"); 1731 break; 1732 } 1733 mdelay(1); 1734 } 1735 1736 /* 1737 * Runtime resume will reset the entire host controller, which 1738 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL. 1739 * Here set DMAEN and BCEN when enable CMDQ. 1740 */ 1741 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE); 1742 if (host->flags & SDHCI_REQ_USE_DMA) 1743 mode |= SDHCI_TRNS_DMA; 1744 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE)) 1745 mode |= SDHCI_TRNS_BLK_CNT_EN; 1746 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); 1747 1748 /* 1749 * Though Runtime resume reset the entire host controller, 1750 * but do not impact the CQHCI side, need to clear the 1751 * HALT bit, avoid CQHCI stuck in the first request when 1752 * system resume back. 1753 */ 1754 cqhci_writel(cq_host, 0, CQHCI_CTL); 1755 if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT) 1756 dev_err(mmc_dev(host->mmc), 1757 "failed to exit halt state when enable CQE\n"); 1758 1759 1760 sdhci_cqe_enable(mmc); 1761 } 1762 1763 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc) 1764 { 1765 sdhci_dumpregs(mmc_priv(mmc)); 1766 } 1767 1768 static const struct cqhci_host_ops esdhc_cqhci_ops = { 1769 .enable = esdhc_cqe_enable, 1770 .disable = sdhci_cqe_disable, 1771 .dumpregs = esdhc_sdhci_dumpregs, 1772 }; 1773 1774 static int 1775 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev, 1776 struct sdhci_host *host, 1777 struct pltfm_imx_data *imx_data) 1778 { 1779 struct device_node *np = pdev->dev.of_node; 1780 struct esdhc_platform_data *boarddata = &imx_data->boarddata; 1781 int ret; 1782 1783 if (of_property_read_bool(np, "fsl,wp-controller")) 1784 boarddata->wp_type = ESDHC_WP_CONTROLLER; 1785 1786 /* 1787 * If we have this property, then activate WP check. 1788 * Retrieving and requesting the actual WP GPIO will happen 1789 * in the call to mmc_of_parse(). 1790 */ 1791 if (of_property_present(np, "wp-gpios")) 1792 boarddata->wp_type = ESDHC_WP_GPIO; 1793 1794 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step); 1795 of_property_read_u32(np, "fsl,tuning-start-tap", 1796 &boarddata->tuning_start_tap); 1797 1798 of_property_read_u32(np, "fsl,strobe-dll-delay-target", 1799 &boarddata->strobe_dll_delay_target); 1800 if (of_property_read_bool(np, "no-1-8-v")) 1801 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V; 1802 1803 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line)) 1804 boarddata->delay_line = 0; 1805 1806 mmc_of_parse_voltage(host->mmc, &host->ocr_mask); 1807 1808 if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) { 1809 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl, 1810 ESDHC_PINCTRL_STATE_100MHZ); 1811 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl, 1812 ESDHC_PINCTRL_STATE_200MHZ); 1813 } 1814 1815 /* call to generic mmc_of_parse to support additional capabilities */ 1816 ret = mmc_of_parse(host->mmc); 1817 if (ret) 1818 return ret; 1819 1820 /* HS400/HS400ES require 8 bit bus */ 1821 if (!(host->mmc->caps & MMC_CAP_8_BIT_DATA)) 1822 host->mmc->caps2 &= ~(MMC_CAP2_HS400 | MMC_CAP2_HS400_ES); 1823 1824 if (mmc_gpio_get_cd(host->mmc) >= 0) 1825 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 1826 1827 return 0; 1828 } 1829 1830 static int sdhci_esdhc_imx_probe(struct platform_device *pdev) 1831 { 1832 struct sdhci_pltfm_host *pltfm_host; 1833 struct sdhci_host *host; 1834 struct cqhci_host *cq_host; 1835 int err; 1836 struct pltfm_imx_data *imx_data; 1837 1838 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata, 1839 sizeof(*imx_data)); 1840 if (IS_ERR(host)) 1841 return PTR_ERR(host); 1842 1843 pltfm_host = sdhci_priv(host); 1844 1845 imx_data = sdhci_pltfm_priv(pltfm_host); 1846 1847 imx_data->socdata = device_get_match_data(&pdev->dev); 1848 1849 host->quirks |= imx_data->socdata->quirks; 1850 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 1851 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0); 1852 1853 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 1854 if (IS_ERR(imx_data->clk_ipg)) { 1855 err = PTR_ERR(imx_data->clk_ipg); 1856 goto free_sdhci; 1857 } 1858 1859 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 1860 if (IS_ERR(imx_data->clk_ahb)) { 1861 err = PTR_ERR(imx_data->clk_ahb); 1862 goto free_sdhci; 1863 } 1864 1865 imx_data->clk_per = devm_clk_get(&pdev->dev, "per"); 1866 if (IS_ERR(imx_data->clk_per)) { 1867 err = PTR_ERR(imx_data->clk_per); 1868 goto free_sdhci; 1869 } 1870 1871 pltfm_host->clk = imx_data->clk_per; 1872 err = clk_prepare_enable(imx_data->clk_per); 1873 if (err) 1874 goto free_sdhci; 1875 err = clk_prepare_enable(imx_data->clk_ipg); 1876 if (err) 1877 goto disable_per_clk; 1878 err = clk_prepare_enable(imx_data->clk_ahb); 1879 if (err) 1880 goto disable_ipg_clk; 1881 1882 pltfm_host->clock = clk_get_rate(pltfm_host->clk); 1883 if (!pltfm_host->clock) { 1884 dev_err(mmc_dev(host->mmc), "could not get clk rate\n"); 1885 err = -EINVAL; 1886 goto disable_ahb_clk; 1887 } 1888 1889 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev); 1890 if (IS_ERR(imx_data->pinctrl)) 1891 dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n"); 1892 1893 if (esdhc_is_usdhc(imx_data)) { 1894 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN; 1895 host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR; 1896 1897 /* GPIO CD can be set as a wakeup source */ 1898 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_CD_WAKE)) 1899 host->mmc->caps |= MMC_CAP_CD_WAKE; 1900 1901 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200)) 1902 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200; 1903 1904 /* clear tuning bits in case ROM has set it already */ 1905 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL); 1906 writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS); 1907 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS); 1908 1909 /* 1910 * Link usdhc specific mmc_host_ops execute_tuning function, 1911 * to replace the standard one in sdhci_ops. 1912 */ 1913 host->mmc_host_ops.execute_tuning = usdhc_execute_tuning; 1914 1915 /* 1916 * Link usdhc specific mmc_host_ops init card function, 1917 * to distinguish the card type. 1918 */ 1919 host->mmc_host_ops.init_card = usdhc_init_card; 1920 1921 host->max_timeout_count = 0xF; 1922 } 1923 1924 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) 1925 sdhci_esdhc_ops.platform_execute_tuning = 1926 esdhc_executing_tuning; 1927 1928 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536) 1929 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA; 1930 1931 if (imx_data->socdata->flags & ESDHC_FLAG_HS400) 1932 host->mmc->caps2 |= MMC_CAP2_HS400; 1933 1934 if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23) 1935 host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN; 1936 1937 if (imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) { 1938 host->mmc->caps2 |= MMC_CAP2_HS400_ES; 1939 host->mmc_host_ops.hs400_enhanced_strobe = 1940 esdhc_hs400_enhanced_strobe; 1941 } 1942 1943 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) { 1944 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD; 1945 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL); 1946 if (!cq_host) { 1947 err = -ENOMEM; 1948 goto disable_ahb_clk; 1949 } 1950 1951 cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET; 1952 cq_host->ops = &esdhc_cqhci_ops; 1953 1954 err = cqhci_init(cq_host, host->mmc, false); 1955 if (err) 1956 goto disable_ahb_clk; 1957 } 1958 1959 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data); 1960 if (err) 1961 goto disable_ahb_clk; 1962 1963 sdhci_esdhc_imx_hwinit(host); 1964 1965 err = sdhci_add_host(host); 1966 if (err) 1967 goto disable_ahb_clk; 1968 1969 /* 1970 * Setup the wakeup capability here, let user to decide 1971 * whether need to enable this wakeup through sysfs interface. 1972 */ 1973 if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) && 1974 (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ)) 1975 device_set_wakeup_capable(&pdev->dev, true); 1976 1977 pm_runtime_set_active(&pdev->dev); 1978 pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 1979 pm_runtime_use_autosuspend(&pdev->dev); 1980 pm_suspend_ignore_children(&pdev->dev, 1); 1981 pm_runtime_enable(&pdev->dev); 1982 1983 return 0; 1984 1985 disable_ahb_clk: 1986 clk_disable_unprepare(imx_data->clk_ahb); 1987 disable_ipg_clk: 1988 clk_disable_unprepare(imx_data->clk_ipg); 1989 disable_per_clk: 1990 clk_disable_unprepare(imx_data->clk_per); 1991 free_sdhci: 1992 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 1993 cpu_latency_qos_remove_request(&imx_data->pm_qos_req); 1994 return err; 1995 } 1996 1997 static void sdhci_esdhc_imx_remove(struct platform_device *pdev) 1998 { 1999 struct sdhci_host *host = platform_get_drvdata(pdev); 2000 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 2001 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 2002 int dead; 2003 2004 pm_runtime_get_sync(&pdev->dev); 2005 dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); 2006 pm_runtime_disable(&pdev->dev); 2007 pm_runtime_put_noidle(&pdev->dev); 2008 2009 sdhci_remove_host(host, dead); 2010 2011 clk_disable_unprepare(imx_data->clk_per); 2012 clk_disable_unprepare(imx_data->clk_ipg); 2013 clk_disable_unprepare(imx_data->clk_ahb); 2014 2015 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 2016 cpu_latency_qos_remove_request(&imx_data->pm_qos_req); 2017 } 2018 2019 #ifdef CONFIG_PM_SLEEP 2020 static int sdhci_esdhc_suspend(struct device *dev) 2021 { 2022 struct sdhci_host *host = dev_get_drvdata(dev); 2023 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 2024 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 2025 int ret; 2026 2027 /* 2028 * Switch to runtime resume for two reasons: 2029 * 1, there is register access (e.g., wakeup control register), so 2030 * need to make sure gate on ipg clock. 2031 * 2, make sure the pm_runtime_force_resume() in sdhci_esdhc_resume() really 2032 * invoke its ->runtime_resume callback (needs_force_resume = 1). 2033 */ 2034 pm_runtime_get_sync(dev); 2035 2036 if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) && 2037 (host->tuning_mode != SDHCI_TUNING_MODE_1)) { 2038 mmc_retune_timer_stop(host->mmc); 2039 mmc_retune_needed(host->mmc); 2040 } 2041 2042 /* 2043 * For the device need to keep power during system PM, need 2044 * to save the tuning delay value just in case the usdhc 2045 * lost power during system PM. 2046 */ 2047 if (mmc_card_keep_power(host->mmc) && mmc_card_wake_sdio_irq(host->mmc) && 2048 esdhc_is_usdhc(imx_data)) 2049 sdhc_esdhc_tuning_save(host); 2050 2051 if (device_may_wakeup(dev)) { 2052 /* The irqs of imx are not shared. It is safe to disable */ 2053 disable_irq(host->irq); 2054 ret = sdhci_enable_irq_wakeups(host); 2055 if (!ret) 2056 dev_warn(dev, "Failed to enable irq wakeup\n"); 2057 } else { 2058 /* 2059 * For the device which works as wakeup source, no need 2060 * to change the pinctrl to sleep state. 2061 * e.g. For SDIO device, the interrupt share with data pin, 2062 * but the pinctrl sleep state may config the data pin to 2063 * other function like GPIO function to save power in PM, 2064 * which finally block the SDIO wakeup function. 2065 */ 2066 ret = pinctrl_pm_select_sleep_state(dev); 2067 if (ret) 2068 return ret; 2069 } 2070 2071 ret = mmc_gpio_set_cd_wake(host->mmc, true); 2072 2073 /* 2074 * Make sure invoke runtime_suspend to gate off clock. 2075 * uSDHC IP supports in-band SDIO wakeup even without clock. 2076 */ 2077 pm_runtime_force_suspend(dev); 2078 2079 return ret; 2080 } 2081 2082 static int sdhci_esdhc_resume(struct device *dev) 2083 { 2084 struct sdhci_host *host = dev_get_drvdata(dev); 2085 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 2086 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 2087 int ret; 2088 2089 pm_runtime_force_resume(dev); 2090 2091 ret = mmc_gpio_set_cd_wake(host->mmc, false); 2092 if (ret) 2093 return ret; 2094 2095 /* re-initialize hw state in case it's lost in low power mode */ 2096 sdhci_esdhc_imx_hwinit(host); 2097 2098 if (host->irq_wake_enabled) { 2099 sdhci_disable_irq_wakeups(host); 2100 enable_irq(host->irq); 2101 } 2102 2103 /* 2104 * restore the saved tuning delay value for the device which keep 2105 * power during system PM. 2106 */ 2107 if (mmc_card_keep_power(host->mmc) && mmc_card_wake_sdio_irq(host->mmc) && 2108 esdhc_is_usdhc(imx_data)) 2109 sdhc_esdhc_tuning_restore(host); 2110 2111 pm_runtime_put_autosuspend(dev); 2112 2113 return ret; 2114 } 2115 #endif 2116 2117 #ifdef CONFIG_PM 2118 static int sdhci_esdhc_runtime_suspend(struct device *dev) 2119 { 2120 struct sdhci_host *host = dev_get_drvdata(dev); 2121 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 2122 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 2123 int ret; 2124 2125 if (host->mmc->caps2 & MMC_CAP2_CQE) { 2126 ret = cqhci_suspend(host->mmc); 2127 if (ret) 2128 return ret; 2129 } 2130 2131 sdhci_runtime_suspend_host(host); 2132 2133 if (host->tuning_mode != SDHCI_TUNING_MODE_3) 2134 mmc_retune_needed(host->mmc); 2135 2136 imx_data->actual_clock = host->mmc->actual_clock; 2137 esdhc_pltfm_set_clock(host, 0); 2138 clk_disable_unprepare(imx_data->clk_per); 2139 clk_disable_unprepare(imx_data->clk_ipg); 2140 clk_disable_unprepare(imx_data->clk_ahb); 2141 2142 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 2143 cpu_latency_qos_remove_request(&imx_data->pm_qos_req); 2144 2145 return 0; 2146 } 2147 2148 static int sdhci_esdhc_runtime_resume(struct device *dev) 2149 { 2150 struct sdhci_host *host = dev_get_drvdata(dev); 2151 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 2152 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host); 2153 int err; 2154 2155 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 2156 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0); 2157 2158 if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME) 2159 clk_set_rate(imx_data->clk_per, pltfm_host->clock); 2160 2161 err = clk_prepare_enable(imx_data->clk_ahb); 2162 if (err) 2163 goto remove_pm_qos_request; 2164 2165 err = clk_prepare_enable(imx_data->clk_per); 2166 if (err) 2167 goto disable_ahb_clk; 2168 2169 err = clk_prepare_enable(imx_data->clk_ipg); 2170 if (err) 2171 goto disable_per_clk; 2172 2173 esdhc_pltfm_set_clock(host, imx_data->actual_clock); 2174 2175 sdhci_runtime_resume_host(host, 0); 2176 2177 if (host->mmc->caps2 & MMC_CAP2_CQE) 2178 err = cqhci_resume(host->mmc); 2179 2180 return err; 2181 2182 disable_per_clk: 2183 clk_disable_unprepare(imx_data->clk_per); 2184 disable_ahb_clk: 2185 clk_disable_unprepare(imx_data->clk_ahb); 2186 remove_pm_qos_request: 2187 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS) 2188 cpu_latency_qos_remove_request(&imx_data->pm_qos_req); 2189 return err; 2190 } 2191 #endif 2192 2193 static const struct dev_pm_ops sdhci_esdhc_pmops = { 2194 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume) 2195 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend, 2196 sdhci_esdhc_runtime_resume, NULL) 2197 }; 2198 2199 static struct platform_driver sdhci_esdhc_imx_driver = { 2200 .driver = { 2201 .name = "sdhci-esdhc-imx", 2202 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 2203 .of_match_table = imx_esdhc_dt_ids, 2204 .pm = &sdhci_esdhc_pmops, 2205 }, 2206 .probe = sdhci_esdhc_imx_probe, 2207 .remove = sdhci_esdhc_imx_remove, 2208 }; 2209 2210 module_platform_driver(sdhci_esdhc_imx_driver); 2211 2212 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC"); 2213 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>"); 2214 MODULE_LICENSE("GPL v2"); 2215