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