1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014-2015, 2022 MediaTek Inc. 4 * Author: Chaotian.Jing <chaotian.jing@mediatek.com> 5 */ 6 7 #include <linux/module.h> 8 #include <linux/bitops.h> 9 #include <linux/clk.h> 10 #include <linux/delay.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/iopoll.h> 13 #include <linux/ioport.h> 14 #include <linux/irq.h> 15 #include <linux/of_address.h> 16 #include <linux/of_device.h> 17 #include <linux/of_irq.h> 18 #include <linux/of_gpio.h> 19 #include <linux/pinctrl/consumer.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm.h> 22 #include <linux/pm_runtime.h> 23 #include <linux/pm_wakeirq.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/slab.h> 26 #include <linux/spinlock.h> 27 #include <linux/interrupt.h> 28 #include <linux/reset.h> 29 30 #include <linux/mmc/card.h> 31 #include <linux/mmc/core.h> 32 #include <linux/mmc/host.h> 33 #include <linux/mmc/mmc.h> 34 #include <linux/mmc/sd.h> 35 #include <linux/mmc/sdio.h> 36 #include <linux/mmc/slot-gpio.h> 37 38 #include "cqhci.h" 39 40 #define MAX_BD_NUM 1024 41 #define MSDC_NR_CLOCKS 3 42 43 /*--------------------------------------------------------------------------*/ 44 /* Common Definition */ 45 /*--------------------------------------------------------------------------*/ 46 #define MSDC_BUS_1BITS 0x0 47 #define MSDC_BUS_4BITS 0x1 48 #define MSDC_BUS_8BITS 0x2 49 50 #define MSDC_BURST_64B 0x6 51 52 /*--------------------------------------------------------------------------*/ 53 /* Register Offset */ 54 /*--------------------------------------------------------------------------*/ 55 #define MSDC_CFG 0x0 56 #define MSDC_IOCON 0x04 57 #define MSDC_PS 0x08 58 #define MSDC_INT 0x0c 59 #define MSDC_INTEN 0x10 60 #define MSDC_FIFOCS 0x14 61 #define SDC_CFG 0x30 62 #define SDC_CMD 0x34 63 #define SDC_ARG 0x38 64 #define SDC_STS 0x3c 65 #define SDC_RESP0 0x40 66 #define SDC_RESP1 0x44 67 #define SDC_RESP2 0x48 68 #define SDC_RESP3 0x4c 69 #define SDC_BLK_NUM 0x50 70 #define SDC_ADV_CFG0 0x64 71 #define EMMC_IOCON 0x7c 72 #define SDC_ACMD_RESP 0x80 73 #define DMA_SA_H4BIT 0x8c 74 #define MSDC_DMA_SA 0x90 75 #define MSDC_DMA_CTRL 0x98 76 #define MSDC_DMA_CFG 0x9c 77 #define MSDC_PATCH_BIT 0xb0 78 #define MSDC_PATCH_BIT1 0xb4 79 #define MSDC_PATCH_BIT2 0xb8 80 #define MSDC_PAD_TUNE 0xec 81 #define MSDC_PAD_TUNE0 0xf0 82 #define PAD_DS_TUNE 0x188 83 #define PAD_CMD_TUNE 0x18c 84 #define EMMC51_CFG0 0x204 85 #define EMMC50_CFG0 0x208 86 #define EMMC50_CFG1 0x20c 87 #define EMMC50_CFG3 0x220 88 #define SDC_FIFO_CFG 0x228 89 #define CQHCI_SETTING 0x7fc 90 91 /*--------------------------------------------------------------------------*/ 92 /* Top Pad Register Offset */ 93 /*--------------------------------------------------------------------------*/ 94 #define EMMC_TOP_CONTROL 0x00 95 #define EMMC_TOP_CMD 0x04 96 #define EMMC50_PAD_DS_TUNE 0x0c 97 98 /*--------------------------------------------------------------------------*/ 99 /* Register Mask */ 100 /*--------------------------------------------------------------------------*/ 101 102 /* MSDC_CFG mask */ 103 #define MSDC_CFG_MODE BIT(0) /* RW */ 104 #define MSDC_CFG_CKPDN BIT(1) /* RW */ 105 #define MSDC_CFG_RST BIT(2) /* RW */ 106 #define MSDC_CFG_PIO BIT(3) /* RW */ 107 #define MSDC_CFG_CKDRVEN BIT(4) /* RW */ 108 #define MSDC_CFG_BV18SDT BIT(5) /* RW */ 109 #define MSDC_CFG_BV18PSS BIT(6) /* R */ 110 #define MSDC_CFG_CKSTB BIT(7) /* R */ 111 #define MSDC_CFG_CKDIV GENMASK(15, 8) /* RW */ 112 #define MSDC_CFG_CKMOD GENMASK(17, 16) /* RW */ 113 #define MSDC_CFG_HS400_CK_MODE BIT(18) /* RW */ 114 #define MSDC_CFG_HS400_CK_MODE_EXTRA BIT(22) /* RW */ 115 #define MSDC_CFG_CKDIV_EXTRA GENMASK(19, 8) /* RW */ 116 #define MSDC_CFG_CKMOD_EXTRA GENMASK(21, 20) /* RW */ 117 118 /* MSDC_IOCON mask */ 119 #define MSDC_IOCON_SDR104CKS BIT(0) /* RW */ 120 #define MSDC_IOCON_RSPL BIT(1) /* RW */ 121 #define MSDC_IOCON_DSPL BIT(2) /* RW */ 122 #define MSDC_IOCON_DDLSEL BIT(3) /* RW */ 123 #define MSDC_IOCON_DDR50CKD BIT(4) /* RW */ 124 #define MSDC_IOCON_DSPLSEL BIT(5) /* RW */ 125 #define MSDC_IOCON_W_DSPL BIT(8) /* RW */ 126 #define MSDC_IOCON_D0SPL BIT(16) /* RW */ 127 #define MSDC_IOCON_D1SPL BIT(17) /* RW */ 128 #define MSDC_IOCON_D2SPL BIT(18) /* RW */ 129 #define MSDC_IOCON_D3SPL BIT(19) /* RW */ 130 #define MSDC_IOCON_D4SPL BIT(20) /* RW */ 131 #define MSDC_IOCON_D5SPL BIT(21) /* RW */ 132 #define MSDC_IOCON_D6SPL BIT(22) /* RW */ 133 #define MSDC_IOCON_D7SPL BIT(23) /* RW */ 134 #define MSDC_IOCON_RISCSZ GENMASK(25, 24) /* RW */ 135 136 /* MSDC_PS mask */ 137 #define MSDC_PS_CDEN BIT(0) /* RW */ 138 #define MSDC_PS_CDSTS BIT(1) /* R */ 139 #define MSDC_PS_CDDEBOUNCE GENMASK(15, 12) /* RW */ 140 #define MSDC_PS_DAT GENMASK(23, 16) /* R */ 141 #define MSDC_PS_DATA1 BIT(17) /* R */ 142 #define MSDC_PS_CMD BIT(24) /* R */ 143 #define MSDC_PS_WP BIT(31) /* R */ 144 145 /* MSDC_INT mask */ 146 #define MSDC_INT_MMCIRQ BIT(0) /* W1C */ 147 #define MSDC_INT_CDSC BIT(1) /* W1C */ 148 #define MSDC_INT_ACMDRDY BIT(3) /* W1C */ 149 #define MSDC_INT_ACMDTMO BIT(4) /* W1C */ 150 #define MSDC_INT_ACMDCRCERR BIT(5) /* W1C */ 151 #define MSDC_INT_DMAQ_EMPTY BIT(6) /* W1C */ 152 #define MSDC_INT_SDIOIRQ BIT(7) /* W1C */ 153 #define MSDC_INT_CMDRDY BIT(8) /* W1C */ 154 #define MSDC_INT_CMDTMO BIT(9) /* W1C */ 155 #define MSDC_INT_RSPCRCERR BIT(10) /* W1C */ 156 #define MSDC_INT_CSTA BIT(11) /* R */ 157 #define MSDC_INT_XFER_COMPL BIT(12) /* W1C */ 158 #define MSDC_INT_DXFER_DONE BIT(13) /* W1C */ 159 #define MSDC_INT_DATTMO BIT(14) /* W1C */ 160 #define MSDC_INT_DATCRCERR BIT(15) /* W1C */ 161 #define MSDC_INT_ACMD19_DONE BIT(16) /* W1C */ 162 #define MSDC_INT_DMA_BDCSERR BIT(17) /* W1C */ 163 #define MSDC_INT_DMA_GPDCSERR BIT(18) /* W1C */ 164 #define MSDC_INT_DMA_PROTECT BIT(19) /* W1C */ 165 #define MSDC_INT_CMDQ BIT(28) /* W1C */ 166 167 /* MSDC_INTEN mask */ 168 #define MSDC_INTEN_MMCIRQ BIT(0) /* RW */ 169 #define MSDC_INTEN_CDSC BIT(1) /* RW */ 170 #define MSDC_INTEN_ACMDRDY BIT(3) /* RW */ 171 #define MSDC_INTEN_ACMDTMO BIT(4) /* RW */ 172 #define MSDC_INTEN_ACMDCRCERR BIT(5) /* RW */ 173 #define MSDC_INTEN_DMAQ_EMPTY BIT(6) /* RW */ 174 #define MSDC_INTEN_SDIOIRQ BIT(7) /* RW */ 175 #define MSDC_INTEN_CMDRDY BIT(8) /* RW */ 176 #define MSDC_INTEN_CMDTMO BIT(9) /* RW */ 177 #define MSDC_INTEN_RSPCRCERR BIT(10) /* RW */ 178 #define MSDC_INTEN_CSTA BIT(11) /* RW */ 179 #define MSDC_INTEN_XFER_COMPL BIT(12) /* RW */ 180 #define MSDC_INTEN_DXFER_DONE BIT(13) /* RW */ 181 #define MSDC_INTEN_DATTMO BIT(14) /* RW */ 182 #define MSDC_INTEN_DATCRCERR BIT(15) /* RW */ 183 #define MSDC_INTEN_ACMD19_DONE BIT(16) /* RW */ 184 #define MSDC_INTEN_DMA_BDCSERR BIT(17) /* RW */ 185 #define MSDC_INTEN_DMA_GPDCSERR BIT(18) /* RW */ 186 #define MSDC_INTEN_DMA_PROTECT BIT(19) /* RW */ 187 188 /* MSDC_FIFOCS mask */ 189 #define MSDC_FIFOCS_RXCNT GENMASK(7, 0) /* R */ 190 #define MSDC_FIFOCS_TXCNT GENMASK(23, 16) /* R */ 191 #define MSDC_FIFOCS_CLR BIT(31) /* RW */ 192 193 /* SDC_CFG mask */ 194 #define SDC_CFG_SDIOINTWKUP BIT(0) /* RW */ 195 #define SDC_CFG_INSWKUP BIT(1) /* RW */ 196 #define SDC_CFG_WRDTOC GENMASK(14, 2) /* RW */ 197 #define SDC_CFG_BUSWIDTH GENMASK(17, 16) /* RW */ 198 #define SDC_CFG_SDIO BIT(19) /* RW */ 199 #define SDC_CFG_SDIOIDE BIT(20) /* RW */ 200 #define SDC_CFG_INTATGAP BIT(21) /* RW */ 201 #define SDC_CFG_DTOC GENMASK(31, 24) /* RW */ 202 203 /* SDC_STS mask */ 204 #define SDC_STS_SDCBUSY BIT(0) /* RW */ 205 #define SDC_STS_CMDBUSY BIT(1) /* RW */ 206 #define SDC_STS_SWR_COMPL BIT(31) /* RW */ 207 208 #define SDC_DAT1_IRQ_TRIGGER BIT(19) /* RW */ 209 /* SDC_ADV_CFG0 mask */ 210 #define SDC_RX_ENHANCE_EN BIT(20) /* RW */ 211 212 /* DMA_SA_H4BIT mask */ 213 #define DMA_ADDR_HIGH_4BIT GENMASK(3, 0) /* RW */ 214 215 /* MSDC_DMA_CTRL mask */ 216 #define MSDC_DMA_CTRL_START BIT(0) /* W */ 217 #define MSDC_DMA_CTRL_STOP BIT(1) /* W */ 218 #define MSDC_DMA_CTRL_RESUME BIT(2) /* W */ 219 #define MSDC_DMA_CTRL_MODE BIT(8) /* RW */ 220 #define MSDC_DMA_CTRL_LASTBUF BIT(10) /* RW */ 221 #define MSDC_DMA_CTRL_BRUSTSZ GENMASK(14, 12) /* RW */ 222 223 /* MSDC_DMA_CFG mask */ 224 #define MSDC_DMA_CFG_STS BIT(0) /* R */ 225 #define MSDC_DMA_CFG_DECSEN BIT(1) /* RW */ 226 #define MSDC_DMA_CFG_AHBHPROT2 BIT(9) /* RW */ 227 #define MSDC_DMA_CFG_ACTIVEEN BIT(13) /* RW */ 228 #define MSDC_DMA_CFG_CS12B16B BIT(16) /* RW */ 229 230 /* MSDC_PATCH_BIT mask */ 231 #define MSDC_PATCH_BIT_ODDSUPP BIT(1) /* RW */ 232 #define MSDC_INT_DAT_LATCH_CK_SEL GENMASK(9, 7) 233 #define MSDC_CKGEN_MSDC_DLY_SEL GENMASK(14, 10) 234 #define MSDC_PATCH_BIT_IODSSEL BIT(16) /* RW */ 235 #define MSDC_PATCH_BIT_IOINTSEL BIT(17) /* RW */ 236 #define MSDC_PATCH_BIT_BUSYDLY GENMASK(21, 18) /* RW */ 237 #define MSDC_PATCH_BIT_WDOD GENMASK(25, 22) /* RW */ 238 #define MSDC_PATCH_BIT_IDRTSEL BIT(26) /* RW */ 239 #define MSDC_PATCH_BIT_CMDFSEL BIT(27) /* RW */ 240 #define MSDC_PATCH_BIT_INTDLSEL BIT(28) /* RW */ 241 #define MSDC_PATCH_BIT_SPCPUSH BIT(29) /* RW */ 242 #define MSDC_PATCH_BIT_DECRCTMO BIT(30) /* RW */ 243 244 #define MSDC_PATCH_BIT1_CMDTA GENMASK(5, 3) /* RW */ 245 #define MSDC_PB1_BUSY_CHECK_SEL BIT(7) /* RW */ 246 #define MSDC_PATCH_BIT1_STOP_DLY GENMASK(11, 8) /* RW */ 247 248 #define MSDC_PATCH_BIT2_CFGRESP BIT(15) /* RW */ 249 #define MSDC_PATCH_BIT2_CFGCRCSTS BIT(28) /* RW */ 250 #define MSDC_PB2_SUPPORT_64G BIT(1) /* RW */ 251 #define MSDC_PB2_RESPWAIT GENMASK(3, 2) /* RW */ 252 #define MSDC_PB2_RESPSTSENSEL GENMASK(18, 16) /* RW */ 253 #define MSDC_PB2_CRCSTSENSEL GENMASK(31, 29) /* RW */ 254 255 #define MSDC_PAD_TUNE_DATWRDLY GENMASK(4, 0) /* RW */ 256 #define MSDC_PAD_TUNE_DATRRDLY GENMASK(12, 8) /* RW */ 257 #define MSDC_PAD_TUNE_CMDRDLY GENMASK(20, 16) /* RW */ 258 #define MSDC_PAD_TUNE_CMDRRDLY GENMASK(26, 22) /* RW */ 259 #define MSDC_PAD_TUNE_CLKTDLY GENMASK(31, 27) /* RW */ 260 #define MSDC_PAD_TUNE_RXDLYSEL BIT(15) /* RW */ 261 #define MSDC_PAD_TUNE_RD_SEL BIT(13) /* RW */ 262 #define MSDC_PAD_TUNE_CMD_SEL BIT(21) /* RW */ 263 264 #define PAD_DS_TUNE_DLY_SEL BIT(0) /* RW */ 265 #define PAD_DS_TUNE_DLY1 GENMASK(6, 2) /* RW */ 266 #define PAD_DS_TUNE_DLY2 GENMASK(11, 7) /* RW */ 267 #define PAD_DS_TUNE_DLY3 GENMASK(16, 12) /* RW */ 268 269 #define PAD_CMD_TUNE_RX_DLY3 GENMASK(5, 1) /* RW */ 270 271 /* EMMC51_CFG0 mask */ 272 #define CMDQ_RDAT_CNT GENMASK(21, 12) /* RW */ 273 274 #define EMMC50_CFG_PADCMD_LATCHCK BIT(0) /* RW */ 275 #define EMMC50_CFG_CRCSTS_EDGE BIT(3) /* RW */ 276 #define EMMC50_CFG_CFCSTS_SEL BIT(4) /* RW */ 277 #define EMMC50_CFG_CMD_RESP_SEL BIT(9) /* RW */ 278 279 /* EMMC50_CFG1 mask */ 280 #define EMMC50_CFG1_DS_CFG BIT(28) /* RW */ 281 282 #define EMMC50_CFG3_OUTS_WR GENMASK(4, 0) /* RW */ 283 284 #define SDC_FIFO_CFG_WRVALIDSEL BIT(24) /* RW */ 285 #define SDC_FIFO_CFG_RDVALIDSEL BIT(25) /* RW */ 286 287 /* CQHCI_SETTING */ 288 #define CQHCI_RD_CMD_WND_SEL BIT(14) /* RW */ 289 #define CQHCI_WR_CMD_WND_SEL BIT(15) /* RW */ 290 291 /* EMMC_TOP_CONTROL mask */ 292 #define PAD_RXDLY_SEL BIT(0) /* RW */ 293 #define DELAY_EN BIT(1) /* RW */ 294 #define PAD_DAT_RD_RXDLY2 GENMASK(6, 2) /* RW */ 295 #define PAD_DAT_RD_RXDLY GENMASK(11, 7) /* RW */ 296 #define PAD_DAT_RD_RXDLY2_SEL BIT(12) /* RW */ 297 #define PAD_DAT_RD_RXDLY_SEL BIT(13) /* RW */ 298 #define DATA_K_VALUE_SEL BIT(14) /* RW */ 299 #define SDC_RX_ENH_EN BIT(15) /* TW */ 300 301 /* EMMC_TOP_CMD mask */ 302 #define PAD_CMD_RXDLY2 GENMASK(4, 0) /* RW */ 303 #define PAD_CMD_RXDLY GENMASK(9, 5) /* RW */ 304 #define PAD_CMD_RD_RXDLY2_SEL BIT(10) /* RW */ 305 #define PAD_CMD_RD_RXDLY_SEL BIT(11) /* RW */ 306 #define PAD_CMD_TX_DLY GENMASK(16, 12) /* RW */ 307 308 /* EMMC50_PAD_DS_TUNE mask */ 309 #define PAD_DS_DLY_SEL BIT(16) /* RW */ 310 #define PAD_DS_DLY1 GENMASK(14, 10) /* RW */ 311 #define PAD_DS_DLY3 GENMASK(4, 0) /* RW */ 312 313 #define REQ_CMD_EIO BIT(0) 314 #define REQ_CMD_TMO BIT(1) 315 #define REQ_DAT_ERR BIT(2) 316 #define REQ_STOP_EIO BIT(3) 317 #define REQ_STOP_TMO BIT(4) 318 #define REQ_CMD_BUSY BIT(5) 319 320 #define MSDC_PREPARE_FLAG BIT(0) 321 #define MSDC_ASYNC_FLAG BIT(1) 322 #define MSDC_MMAP_FLAG BIT(2) 323 324 #define MTK_MMC_AUTOSUSPEND_DELAY 50 325 #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */ 326 #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */ 327 328 #define DEFAULT_DEBOUNCE (8) /* 8 cycles CD debounce */ 329 330 #define PAD_DELAY_MAX 32 /* PAD delay cells */ 331 /*--------------------------------------------------------------------------*/ 332 /* Descriptor Structure */ 333 /*--------------------------------------------------------------------------*/ 334 struct mt_gpdma_desc { 335 u32 gpd_info; 336 #define GPDMA_DESC_HWO BIT(0) 337 #define GPDMA_DESC_BDP BIT(1) 338 #define GPDMA_DESC_CHECKSUM GENMASK(15, 8) 339 #define GPDMA_DESC_INT BIT(16) 340 #define GPDMA_DESC_NEXT_H4 GENMASK(27, 24) 341 #define GPDMA_DESC_PTR_H4 GENMASK(31, 28) 342 u32 next; 343 u32 ptr; 344 u32 gpd_data_len; 345 #define GPDMA_DESC_BUFLEN GENMASK(15, 0) 346 #define GPDMA_DESC_EXTLEN GENMASK(23, 16) 347 u32 arg; 348 u32 blknum; 349 u32 cmd; 350 }; 351 352 struct mt_bdma_desc { 353 u32 bd_info; 354 #define BDMA_DESC_EOL BIT(0) 355 #define BDMA_DESC_CHECKSUM GENMASK(15, 8) 356 #define BDMA_DESC_BLKPAD BIT(17) 357 #define BDMA_DESC_DWPAD BIT(18) 358 #define BDMA_DESC_NEXT_H4 GENMASK(27, 24) 359 #define BDMA_DESC_PTR_H4 GENMASK(31, 28) 360 u32 next; 361 u32 ptr; 362 u32 bd_data_len; 363 #define BDMA_DESC_BUFLEN GENMASK(15, 0) 364 #define BDMA_DESC_BUFLEN_EXT GENMASK(23, 0) 365 }; 366 367 struct msdc_dma { 368 struct scatterlist *sg; /* I/O scatter list */ 369 struct mt_gpdma_desc *gpd; /* pointer to gpd array */ 370 struct mt_bdma_desc *bd; /* pointer to bd array */ 371 dma_addr_t gpd_addr; /* the physical address of gpd array */ 372 dma_addr_t bd_addr; /* the physical address of bd array */ 373 }; 374 375 struct msdc_save_para { 376 u32 msdc_cfg; 377 u32 iocon; 378 u32 sdc_cfg; 379 u32 pad_tune; 380 u32 patch_bit0; 381 u32 patch_bit1; 382 u32 patch_bit2; 383 u32 pad_ds_tune; 384 u32 pad_cmd_tune; 385 u32 emmc50_cfg0; 386 u32 emmc50_cfg3; 387 u32 sdc_fifo_cfg; 388 u32 emmc_top_control; 389 u32 emmc_top_cmd; 390 u32 emmc50_pad_ds_tune; 391 }; 392 393 struct mtk_mmc_compatible { 394 u8 clk_div_bits; 395 bool recheck_sdio_irq; 396 bool hs400_tune; /* only used for MT8173 */ 397 u32 pad_tune_reg; 398 bool async_fifo; 399 bool data_tune; 400 bool busy_check; 401 bool stop_clk_fix; 402 bool enhance_rx; 403 bool support_64g; 404 bool use_internal_cd; 405 }; 406 407 struct msdc_tune_para { 408 u32 iocon; 409 u32 pad_tune; 410 u32 pad_cmd_tune; 411 u32 emmc_top_control; 412 u32 emmc_top_cmd; 413 }; 414 415 struct msdc_delay_phase { 416 u8 maxlen; 417 u8 start; 418 u8 final_phase; 419 }; 420 421 struct msdc_host { 422 struct device *dev; 423 const struct mtk_mmc_compatible *dev_comp; 424 int cmd_rsp; 425 426 spinlock_t lock; 427 struct mmc_request *mrq; 428 struct mmc_command *cmd; 429 struct mmc_data *data; 430 int error; 431 432 void __iomem *base; /* host base address */ 433 void __iomem *top_base; /* host top register base address */ 434 435 struct msdc_dma dma; /* dma channel */ 436 u64 dma_mask; 437 438 u32 timeout_ns; /* data timeout ns */ 439 u32 timeout_clks; /* data timeout clks */ 440 441 struct pinctrl *pinctrl; 442 struct pinctrl_state *pins_default; 443 struct pinctrl_state *pins_uhs; 444 struct pinctrl_state *pins_eint; 445 struct delayed_work req_timeout; 446 int irq; /* host interrupt */ 447 int eint_irq; /* interrupt from sdio device for waking up system */ 448 struct reset_control *reset; 449 450 struct clk *src_clk; /* msdc source clock */ 451 struct clk *h_clk; /* msdc h_clk */ 452 struct clk *bus_clk; /* bus clock which used to access register */ 453 struct clk *src_clk_cg; /* msdc source clock control gate */ 454 struct clk *sys_clk_cg; /* msdc subsys clock control gate */ 455 struct clk_bulk_data bulk_clks[MSDC_NR_CLOCKS]; 456 u32 mclk; /* mmc subsystem clock frequency */ 457 u32 src_clk_freq; /* source clock frequency */ 458 unsigned char timing; 459 bool vqmmc_enabled; 460 u32 latch_ck; 461 u32 hs400_ds_delay; 462 u32 hs400_ds_dly3; 463 u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */ 464 u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */ 465 bool hs400_cmd_resp_sel_rising; 466 /* cmd response sample selection for HS400 */ 467 bool hs400_mode; /* current eMMC will run at hs400 mode */ 468 bool hs400_tuning; /* hs400 mode online tuning */ 469 bool internal_cd; /* Use internal card-detect logic */ 470 bool cqhci; /* support eMMC hw cmdq */ 471 struct msdc_save_para save_para; /* used when gate HCLK */ 472 struct msdc_tune_para def_tune_para; /* default tune setting */ 473 struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */ 474 struct cqhci_host *cq_host; 475 }; 476 477 static const struct mtk_mmc_compatible mt2701_compat = { 478 .clk_div_bits = 12, 479 .recheck_sdio_irq = true, 480 .hs400_tune = false, 481 .pad_tune_reg = MSDC_PAD_TUNE0, 482 .async_fifo = true, 483 .data_tune = true, 484 .busy_check = false, 485 .stop_clk_fix = false, 486 .enhance_rx = false, 487 .support_64g = false, 488 }; 489 490 static const struct mtk_mmc_compatible mt2712_compat = { 491 .clk_div_bits = 12, 492 .recheck_sdio_irq = false, 493 .hs400_tune = false, 494 .pad_tune_reg = MSDC_PAD_TUNE0, 495 .async_fifo = true, 496 .data_tune = true, 497 .busy_check = true, 498 .stop_clk_fix = true, 499 .enhance_rx = true, 500 .support_64g = true, 501 }; 502 503 static const struct mtk_mmc_compatible mt6779_compat = { 504 .clk_div_bits = 12, 505 .recheck_sdio_irq = false, 506 .hs400_tune = false, 507 .pad_tune_reg = MSDC_PAD_TUNE0, 508 .async_fifo = true, 509 .data_tune = true, 510 .busy_check = true, 511 .stop_clk_fix = true, 512 .enhance_rx = true, 513 .support_64g = true, 514 }; 515 516 static const struct mtk_mmc_compatible mt6795_compat = { 517 .clk_div_bits = 8, 518 .recheck_sdio_irq = false, 519 .hs400_tune = true, 520 .pad_tune_reg = MSDC_PAD_TUNE, 521 .async_fifo = false, 522 .data_tune = false, 523 .busy_check = false, 524 .stop_clk_fix = false, 525 .enhance_rx = false, 526 .support_64g = false, 527 }; 528 529 static const struct mtk_mmc_compatible mt7620_compat = { 530 .clk_div_bits = 8, 531 .recheck_sdio_irq = true, 532 .hs400_tune = false, 533 .pad_tune_reg = MSDC_PAD_TUNE, 534 .async_fifo = false, 535 .data_tune = false, 536 .busy_check = false, 537 .stop_clk_fix = false, 538 .enhance_rx = false, 539 .use_internal_cd = true, 540 }; 541 542 static const struct mtk_mmc_compatible mt7622_compat = { 543 .clk_div_bits = 12, 544 .recheck_sdio_irq = true, 545 .hs400_tune = false, 546 .pad_tune_reg = MSDC_PAD_TUNE0, 547 .async_fifo = true, 548 .data_tune = true, 549 .busy_check = true, 550 .stop_clk_fix = true, 551 .enhance_rx = true, 552 .support_64g = false, 553 }; 554 555 static const struct mtk_mmc_compatible mt7986_compat = { 556 .clk_div_bits = 12, 557 .recheck_sdio_irq = true, 558 .hs400_tune = false, 559 .pad_tune_reg = MSDC_PAD_TUNE0, 560 .async_fifo = true, 561 .data_tune = true, 562 .busy_check = true, 563 .stop_clk_fix = true, 564 .enhance_rx = true, 565 .support_64g = true, 566 }; 567 568 static const struct mtk_mmc_compatible mt8135_compat = { 569 .clk_div_bits = 8, 570 .recheck_sdio_irq = true, 571 .hs400_tune = false, 572 .pad_tune_reg = MSDC_PAD_TUNE, 573 .async_fifo = false, 574 .data_tune = false, 575 .busy_check = false, 576 .stop_clk_fix = false, 577 .enhance_rx = false, 578 .support_64g = false, 579 }; 580 581 static const struct mtk_mmc_compatible mt8173_compat = { 582 .clk_div_bits = 8, 583 .recheck_sdio_irq = true, 584 .hs400_tune = true, 585 .pad_tune_reg = MSDC_PAD_TUNE, 586 .async_fifo = false, 587 .data_tune = false, 588 .busy_check = false, 589 .stop_clk_fix = false, 590 .enhance_rx = false, 591 .support_64g = false, 592 }; 593 594 static const struct mtk_mmc_compatible mt8183_compat = { 595 .clk_div_bits = 12, 596 .recheck_sdio_irq = false, 597 .hs400_tune = false, 598 .pad_tune_reg = MSDC_PAD_TUNE0, 599 .async_fifo = true, 600 .data_tune = true, 601 .busy_check = true, 602 .stop_clk_fix = true, 603 .enhance_rx = true, 604 .support_64g = true, 605 }; 606 607 static const struct mtk_mmc_compatible mt8516_compat = { 608 .clk_div_bits = 12, 609 .recheck_sdio_irq = true, 610 .hs400_tune = false, 611 .pad_tune_reg = MSDC_PAD_TUNE0, 612 .async_fifo = true, 613 .data_tune = true, 614 .busy_check = true, 615 .stop_clk_fix = true, 616 }; 617 618 static const struct of_device_id msdc_of_ids[] = { 619 { .compatible = "mediatek,mt2701-mmc", .data = &mt2701_compat}, 620 { .compatible = "mediatek,mt2712-mmc", .data = &mt2712_compat}, 621 { .compatible = "mediatek,mt6779-mmc", .data = &mt6779_compat}, 622 { .compatible = "mediatek,mt6795-mmc", .data = &mt6795_compat}, 623 { .compatible = "mediatek,mt7620-mmc", .data = &mt7620_compat}, 624 { .compatible = "mediatek,mt7622-mmc", .data = &mt7622_compat}, 625 { .compatible = "mediatek,mt7986-mmc", .data = &mt7986_compat}, 626 { .compatible = "mediatek,mt8135-mmc", .data = &mt8135_compat}, 627 { .compatible = "mediatek,mt8173-mmc", .data = &mt8173_compat}, 628 { .compatible = "mediatek,mt8183-mmc", .data = &mt8183_compat}, 629 { .compatible = "mediatek,mt8516-mmc", .data = &mt8516_compat}, 630 631 {} 632 }; 633 MODULE_DEVICE_TABLE(of, msdc_of_ids); 634 635 static void sdr_set_bits(void __iomem *reg, u32 bs) 636 { 637 u32 val = readl(reg); 638 639 val |= bs; 640 writel(val, reg); 641 } 642 643 static void sdr_clr_bits(void __iomem *reg, u32 bs) 644 { 645 u32 val = readl(reg); 646 647 val &= ~bs; 648 writel(val, reg); 649 } 650 651 static void sdr_set_field(void __iomem *reg, u32 field, u32 val) 652 { 653 unsigned int tv = readl(reg); 654 655 tv &= ~field; 656 tv |= ((val) << (ffs((unsigned int)field) - 1)); 657 writel(tv, reg); 658 } 659 660 static void sdr_get_field(void __iomem *reg, u32 field, u32 *val) 661 { 662 unsigned int tv = readl(reg); 663 664 *val = ((tv & field) >> (ffs((unsigned int)field) - 1)); 665 } 666 667 static void msdc_reset_hw(struct msdc_host *host) 668 { 669 u32 val; 670 671 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST); 672 readl_poll_timeout(host->base + MSDC_CFG, val, !(val & MSDC_CFG_RST), 0, 0); 673 674 sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR); 675 readl_poll_timeout(host->base + MSDC_FIFOCS, val, 676 !(val & MSDC_FIFOCS_CLR), 0, 0); 677 678 val = readl(host->base + MSDC_INT); 679 writel(val, host->base + MSDC_INT); 680 } 681 682 static void msdc_cmd_next(struct msdc_host *host, 683 struct mmc_request *mrq, struct mmc_command *cmd); 684 static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb); 685 686 static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR | 687 MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY | 688 MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO; 689 static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | 690 MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR | 691 MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT; 692 693 static u8 msdc_dma_calcs(u8 *buf, u32 len) 694 { 695 u32 i, sum = 0; 696 697 for (i = 0; i < len; i++) 698 sum += buf[i]; 699 return 0xff - (u8) sum; 700 } 701 702 static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma, 703 struct mmc_data *data) 704 { 705 unsigned int j, dma_len; 706 dma_addr_t dma_address; 707 u32 dma_ctrl; 708 struct scatterlist *sg; 709 struct mt_gpdma_desc *gpd; 710 struct mt_bdma_desc *bd; 711 712 sg = data->sg; 713 714 gpd = dma->gpd; 715 bd = dma->bd; 716 717 /* modify gpd */ 718 gpd->gpd_info |= GPDMA_DESC_HWO; 719 gpd->gpd_info |= GPDMA_DESC_BDP; 720 /* need to clear first. use these bits to calc checksum */ 721 gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM; 722 gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8; 723 724 /* modify bd */ 725 for_each_sg(data->sg, sg, data->sg_count, j) { 726 dma_address = sg_dma_address(sg); 727 dma_len = sg_dma_len(sg); 728 729 /* init bd */ 730 bd[j].bd_info &= ~BDMA_DESC_BLKPAD; 731 bd[j].bd_info &= ~BDMA_DESC_DWPAD; 732 bd[j].ptr = lower_32_bits(dma_address); 733 if (host->dev_comp->support_64g) { 734 bd[j].bd_info &= ~BDMA_DESC_PTR_H4; 735 bd[j].bd_info |= (upper_32_bits(dma_address) & 0xf) 736 << 28; 737 } 738 739 if (host->dev_comp->support_64g) { 740 bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN_EXT; 741 bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN_EXT); 742 } else { 743 bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN; 744 bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN); 745 } 746 747 if (j == data->sg_count - 1) /* the last bd */ 748 bd[j].bd_info |= BDMA_DESC_EOL; 749 else 750 bd[j].bd_info &= ~BDMA_DESC_EOL; 751 752 /* checksume need to clear first */ 753 bd[j].bd_info &= ~BDMA_DESC_CHECKSUM; 754 bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8; 755 } 756 757 sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1); 758 dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL); 759 dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE); 760 dma_ctrl |= (MSDC_BURST_64B << 12 | BIT(8)); 761 writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL); 762 if (host->dev_comp->support_64g) 763 sdr_set_field(host->base + DMA_SA_H4BIT, DMA_ADDR_HIGH_4BIT, 764 upper_32_bits(dma->gpd_addr) & 0xf); 765 writel(lower_32_bits(dma->gpd_addr), host->base + MSDC_DMA_SA); 766 } 767 768 static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data) 769 { 770 if (!(data->host_cookie & MSDC_PREPARE_FLAG)) { 771 data->host_cookie |= MSDC_PREPARE_FLAG; 772 data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len, 773 mmc_get_dma_dir(data)); 774 } 775 } 776 777 static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data) 778 { 779 if (data->host_cookie & MSDC_ASYNC_FLAG) 780 return; 781 782 if (data->host_cookie & MSDC_PREPARE_FLAG) { 783 dma_unmap_sg(host->dev, data->sg, data->sg_len, 784 mmc_get_dma_dir(data)); 785 data->host_cookie &= ~MSDC_PREPARE_FLAG; 786 } 787 } 788 789 static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks) 790 { 791 struct mmc_host *mmc = mmc_from_priv(host); 792 u64 timeout, clk_ns; 793 u32 mode = 0; 794 795 if (mmc->actual_clock == 0) { 796 timeout = 0; 797 } else { 798 clk_ns = 1000000000ULL; 799 do_div(clk_ns, mmc->actual_clock); 800 timeout = ns + clk_ns - 1; 801 do_div(timeout, clk_ns); 802 timeout += clks; 803 /* in 1048576 sclk cycle unit */ 804 timeout = DIV_ROUND_UP(timeout, BIT(20)); 805 if (host->dev_comp->clk_div_bits == 8) 806 sdr_get_field(host->base + MSDC_CFG, 807 MSDC_CFG_CKMOD, &mode); 808 else 809 sdr_get_field(host->base + MSDC_CFG, 810 MSDC_CFG_CKMOD_EXTRA, &mode); 811 /*DDR mode will double the clk cycles for data timeout */ 812 timeout = mode >= 2 ? timeout * 2 : timeout; 813 timeout = timeout > 1 ? timeout - 1 : 0; 814 } 815 return timeout; 816 } 817 818 /* clock control primitives */ 819 static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks) 820 { 821 u64 timeout; 822 823 host->timeout_ns = ns; 824 host->timeout_clks = clks; 825 826 timeout = msdc_timeout_cal(host, ns, clks); 827 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 828 (u32)(timeout > 255 ? 255 : timeout)); 829 } 830 831 static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks) 832 { 833 u64 timeout; 834 835 timeout = msdc_timeout_cal(host, ns, clks); 836 sdr_set_field(host->base + SDC_CFG, SDC_CFG_WRDTOC, 837 (u32)(timeout > 8191 ? 8191 : timeout)); 838 } 839 840 static void msdc_gate_clock(struct msdc_host *host) 841 { 842 clk_bulk_disable_unprepare(MSDC_NR_CLOCKS, host->bulk_clks); 843 clk_disable_unprepare(host->src_clk_cg); 844 clk_disable_unprepare(host->src_clk); 845 clk_disable_unprepare(host->bus_clk); 846 clk_disable_unprepare(host->h_clk); 847 } 848 849 static int msdc_ungate_clock(struct msdc_host *host) 850 { 851 u32 val; 852 int ret; 853 854 clk_prepare_enable(host->h_clk); 855 clk_prepare_enable(host->bus_clk); 856 clk_prepare_enable(host->src_clk); 857 clk_prepare_enable(host->src_clk_cg); 858 ret = clk_bulk_prepare_enable(MSDC_NR_CLOCKS, host->bulk_clks); 859 if (ret) { 860 dev_err(host->dev, "Cannot enable pclk/axi/ahb clock gates\n"); 861 return ret; 862 } 863 864 return readl_poll_timeout(host->base + MSDC_CFG, val, 865 (val & MSDC_CFG_CKSTB), 1, 20000); 866 } 867 868 static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz) 869 { 870 struct mmc_host *mmc = mmc_from_priv(host); 871 u32 mode; 872 u32 flags; 873 u32 div; 874 u32 sclk; 875 u32 tune_reg = host->dev_comp->pad_tune_reg; 876 u32 val; 877 878 if (!hz) { 879 dev_dbg(host->dev, "set mclk to 0\n"); 880 host->mclk = 0; 881 mmc->actual_clock = 0; 882 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 883 return; 884 } 885 886 flags = readl(host->base + MSDC_INTEN); 887 sdr_clr_bits(host->base + MSDC_INTEN, flags); 888 if (host->dev_comp->clk_div_bits == 8) 889 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE); 890 else 891 sdr_clr_bits(host->base + MSDC_CFG, 892 MSDC_CFG_HS400_CK_MODE_EXTRA); 893 if (timing == MMC_TIMING_UHS_DDR50 || 894 timing == MMC_TIMING_MMC_DDR52 || 895 timing == MMC_TIMING_MMC_HS400) { 896 if (timing == MMC_TIMING_MMC_HS400) 897 mode = 0x3; 898 else 899 mode = 0x2; /* ddr mode and use divisor */ 900 901 if (hz >= (host->src_clk_freq >> 2)) { 902 div = 0; /* mean div = 1/4 */ 903 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */ 904 } else { 905 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); 906 sclk = (host->src_clk_freq >> 2) / div; 907 div = (div >> 1); 908 } 909 910 if (timing == MMC_TIMING_MMC_HS400 && 911 hz >= (host->src_clk_freq >> 1)) { 912 if (host->dev_comp->clk_div_bits == 8) 913 sdr_set_bits(host->base + MSDC_CFG, 914 MSDC_CFG_HS400_CK_MODE); 915 else 916 sdr_set_bits(host->base + MSDC_CFG, 917 MSDC_CFG_HS400_CK_MODE_EXTRA); 918 sclk = host->src_clk_freq >> 1; 919 div = 0; /* div is ignore when bit18 is set */ 920 } 921 } else if (hz >= host->src_clk_freq) { 922 mode = 0x1; /* no divisor */ 923 div = 0; 924 sclk = host->src_clk_freq; 925 } else { 926 mode = 0x0; /* use divisor */ 927 if (hz >= (host->src_clk_freq >> 1)) { 928 div = 0; /* mean div = 1/2 */ 929 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */ 930 } else { 931 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); 932 sclk = (host->src_clk_freq >> 2) / div; 933 } 934 } 935 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 936 937 clk_disable_unprepare(host->src_clk_cg); 938 if (host->dev_comp->clk_div_bits == 8) 939 sdr_set_field(host->base + MSDC_CFG, 940 MSDC_CFG_CKMOD | MSDC_CFG_CKDIV, 941 (mode << 8) | div); 942 else 943 sdr_set_field(host->base + MSDC_CFG, 944 MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA, 945 (mode << 12) | div); 946 947 clk_prepare_enable(host->src_clk_cg); 948 readl_poll_timeout(host->base + MSDC_CFG, val, (val & MSDC_CFG_CKSTB), 0, 0); 949 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 950 mmc->actual_clock = sclk; 951 host->mclk = hz; 952 host->timing = timing; 953 /* need because clk changed. */ 954 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); 955 sdr_set_bits(host->base + MSDC_INTEN, flags); 956 957 /* 958 * mmc_select_hs400() will drop to 50Mhz and High speed mode, 959 * tune result of hs200/200Mhz is not suitable for 50Mhz 960 */ 961 if (mmc->actual_clock <= 52000000) { 962 writel(host->def_tune_para.iocon, host->base + MSDC_IOCON); 963 if (host->top_base) { 964 writel(host->def_tune_para.emmc_top_control, 965 host->top_base + EMMC_TOP_CONTROL); 966 writel(host->def_tune_para.emmc_top_cmd, 967 host->top_base + EMMC_TOP_CMD); 968 } else { 969 writel(host->def_tune_para.pad_tune, 970 host->base + tune_reg); 971 } 972 } else { 973 writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON); 974 writel(host->saved_tune_para.pad_cmd_tune, 975 host->base + PAD_CMD_TUNE); 976 if (host->top_base) { 977 writel(host->saved_tune_para.emmc_top_control, 978 host->top_base + EMMC_TOP_CONTROL); 979 writel(host->saved_tune_para.emmc_top_cmd, 980 host->top_base + EMMC_TOP_CMD); 981 } else { 982 writel(host->saved_tune_para.pad_tune, 983 host->base + tune_reg); 984 } 985 } 986 987 if (timing == MMC_TIMING_MMC_HS400 && 988 host->dev_comp->hs400_tune) 989 sdr_set_field(host->base + tune_reg, 990 MSDC_PAD_TUNE_CMDRRDLY, 991 host->hs400_cmd_int_delay); 992 dev_dbg(host->dev, "sclk: %d, timing: %d\n", mmc->actual_clock, 993 timing); 994 } 995 996 static inline u32 msdc_cmd_find_resp(struct msdc_host *host, 997 struct mmc_command *cmd) 998 { 999 u32 resp; 1000 1001 switch (mmc_resp_type(cmd)) { 1002 /* Actually, R1, R5, R6, R7 are the same */ 1003 case MMC_RSP_R1: 1004 resp = 0x1; 1005 break; 1006 case MMC_RSP_R1B: 1007 resp = 0x7; 1008 break; 1009 case MMC_RSP_R2: 1010 resp = 0x2; 1011 break; 1012 case MMC_RSP_R3: 1013 resp = 0x3; 1014 break; 1015 case MMC_RSP_NONE: 1016 default: 1017 resp = 0x0; 1018 break; 1019 } 1020 1021 return resp; 1022 } 1023 1024 static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host, 1025 struct mmc_request *mrq, struct mmc_command *cmd) 1026 { 1027 struct mmc_host *mmc = mmc_from_priv(host); 1028 /* rawcmd : 1029 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 | 1030 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode 1031 */ 1032 u32 opcode = cmd->opcode; 1033 u32 resp = msdc_cmd_find_resp(host, cmd); 1034 u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7); 1035 1036 host->cmd_rsp = resp; 1037 1038 if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) || 1039 opcode == MMC_STOP_TRANSMISSION) 1040 rawcmd |= BIT(14); 1041 else if (opcode == SD_SWITCH_VOLTAGE) 1042 rawcmd |= BIT(30); 1043 else if (opcode == SD_APP_SEND_SCR || 1044 opcode == SD_APP_SEND_NUM_WR_BLKS || 1045 (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || 1046 (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || 1047 (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC)) 1048 rawcmd |= BIT(11); 1049 1050 if (cmd->data) { 1051 struct mmc_data *data = cmd->data; 1052 1053 if (mmc_op_multi(opcode)) { 1054 if (mmc_card_mmc(mmc->card) && mrq->sbc && 1055 !(mrq->sbc->arg & 0xFFFF0000)) 1056 rawcmd |= BIT(29); /* AutoCMD23 */ 1057 } 1058 1059 rawcmd |= ((data->blksz & 0xFFF) << 16); 1060 if (data->flags & MMC_DATA_WRITE) 1061 rawcmd |= BIT(13); 1062 if (data->blocks > 1) 1063 rawcmd |= BIT(12); 1064 else 1065 rawcmd |= BIT(11); 1066 /* Always use dma mode */ 1067 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO); 1068 1069 if (host->timeout_ns != data->timeout_ns || 1070 host->timeout_clks != data->timeout_clks) 1071 msdc_set_timeout(host, data->timeout_ns, 1072 data->timeout_clks); 1073 1074 writel(data->blocks, host->base + SDC_BLK_NUM); 1075 } 1076 return rawcmd; 1077 } 1078 1079 static void msdc_start_data(struct msdc_host *host, struct mmc_command *cmd, 1080 struct mmc_data *data) 1081 { 1082 bool read; 1083 1084 WARN_ON(host->data); 1085 host->data = data; 1086 read = data->flags & MMC_DATA_READ; 1087 1088 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); 1089 msdc_dma_setup(host, &host->dma, data); 1090 sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask); 1091 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1); 1092 dev_dbg(host->dev, "DMA start\n"); 1093 dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n", 1094 __func__, cmd->opcode, data->blocks, read); 1095 } 1096 1097 static int msdc_auto_cmd_done(struct msdc_host *host, int events, 1098 struct mmc_command *cmd) 1099 { 1100 u32 *rsp = cmd->resp; 1101 1102 rsp[0] = readl(host->base + SDC_ACMD_RESP); 1103 1104 if (events & MSDC_INT_ACMDRDY) { 1105 cmd->error = 0; 1106 } else { 1107 msdc_reset_hw(host); 1108 if (events & MSDC_INT_ACMDCRCERR) { 1109 cmd->error = -EILSEQ; 1110 host->error |= REQ_STOP_EIO; 1111 } else if (events & MSDC_INT_ACMDTMO) { 1112 cmd->error = -ETIMEDOUT; 1113 host->error |= REQ_STOP_TMO; 1114 } 1115 dev_err(host->dev, 1116 "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n", 1117 __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error); 1118 } 1119 return cmd->error; 1120 } 1121 1122 /* 1123 * msdc_recheck_sdio_irq - recheck whether the SDIO irq is lost 1124 * 1125 * Host controller may lost interrupt in some special case. 1126 * Add SDIO irq recheck mechanism to make sure all interrupts 1127 * can be processed immediately 1128 */ 1129 static void msdc_recheck_sdio_irq(struct msdc_host *host) 1130 { 1131 struct mmc_host *mmc = mmc_from_priv(host); 1132 u32 reg_int, reg_inten, reg_ps; 1133 1134 if (mmc->caps & MMC_CAP_SDIO_IRQ) { 1135 reg_inten = readl(host->base + MSDC_INTEN); 1136 if (reg_inten & MSDC_INTEN_SDIOIRQ) { 1137 reg_int = readl(host->base + MSDC_INT); 1138 reg_ps = readl(host->base + MSDC_PS); 1139 if (!(reg_int & MSDC_INT_SDIOIRQ || 1140 reg_ps & MSDC_PS_DATA1)) { 1141 __msdc_enable_sdio_irq(host, 0); 1142 sdio_signal_irq(mmc); 1143 } 1144 } 1145 } 1146 } 1147 1148 static void msdc_track_cmd_data(struct msdc_host *host, struct mmc_command *cmd) 1149 { 1150 if (host->error) 1151 dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n", 1152 __func__, cmd->opcode, cmd->arg, host->error); 1153 } 1154 1155 static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq) 1156 { 1157 unsigned long flags; 1158 1159 /* 1160 * No need check the return value of cancel_delayed_work, as only ONE 1161 * path will go here! 1162 */ 1163 cancel_delayed_work(&host->req_timeout); 1164 1165 spin_lock_irqsave(&host->lock, flags); 1166 host->mrq = NULL; 1167 spin_unlock_irqrestore(&host->lock, flags); 1168 1169 msdc_track_cmd_data(host, mrq->cmd); 1170 if (mrq->data) 1171 msdc_unprepare_data(host, mrq->data); 1172 if (host->error) 1173 msdc_reset_hw(host); 1174 mmc_request_done(mmc_from_priv(host), mrq); 1175 if (host->dev_comp->recheck_sdio_irq) 1176 msdc_recheck_sdio_irq(host); 1177 } 1178 1179 /* returns true if command is fully handled; returns false otherwise */ 1180 static bool msdc_cmd_done(struct msdc_host *host, int events, 1181 struct mmc_request *mrq, struct mmc_command *cmd) 1182 { 1183 bool done = false; 1184 bool sbc_error; 1185 unsigned long flags; 1186 u32 *rsp; 1187 1188 if (mrq->sbc && cmd == mrq->cmd && 1189 (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR 1190 | MSDC_INT_ACMDTMO))) 1191 msdc_auto_cmd_done(host, events, mrq->sbc); 1192 1193 sbc_error = mrq->sbc && mrq->sbc->error; 1194 1195 if (!sbc_error && !(events & (MSDC_INT_CMDRDY 1196 | MSDC_INT_RSPCRCERR 1197 | MSDC_INT_CMDTMO))) 1198 return done; 1199 1200 spin_lock_irqsave(&host->lock, flags); 1201 done = !host->cmd; 1202 host->cmd = NULL; 1203 spin_unlock_irqrestore(&host->lock, flags); 1204 1205 if (done) 1206 return true; 1207 rsp = cmd->resp; 1208 1209 sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask); 1210 1211 if (cmd->flags & MMC_RSP_PRESENT) { 1212 if (cmd->flags & MMC_RSP_136) { 1213 rsp[0] = readl(host->base + SDC_RESP3); 1214 rsp[1] = readl(host->base + SDC_RESP2); 1215 rsp[2] = readl(host->base + SDC_RESP1); 1216 rsp[3] = readl(host->base + SDC_RESP0); 1217 } else { 1218 rsp[0] = readl(host->base + SDC_RESP0); 1219 } 1220 } 1221 1222 if (!sbc_error && !(events & MSDC_INT_CMDRDY)) { 1223 if (events & MSDC_INT_CMDTMO || 1224 (cmd->opcode != MMC_SEND_TUNING_BLOCK && 1225 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200 && 1226 !host->hs400_tuning)) 1227 /* 1228 * should not clear fifo/interrupt as the tune data 1229 * may have alreay come when cmd19/cmd21 gets response 1230 * CRC error. 1231 */ 1232 msdc_reset_hw(host); 1233 if (events & MSDC_INT_RSPCRCERR) { 1234 cmd->error = -EILSEQ; 1235 host->error |= REQ_CMD_EIO; 1236 } else if (events & MSDC_INT_CMDTMO) { 1237 cmd->error = -ETIMEDOUT; 1238 host->error |= REQ_CMD_TMO; 1239 } 1240 } 1241 if (cmd->error) 1242 dev_dbg(host->dev, 1243 "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n", 1244 __func__, cmd->opcode, cmd->arg, rsp[0], 1245 cmd->error); 1246 1247 msdc_cmd_next(host, mrq, cmd); 1248 return true; 1249 } 1250 1251 /* It is the core layer's responsibility to ensure card status 1252 * is correct before issue a request. but host design do below 1253 * checks recommended. 1254 */ 1255 static inline bool msdc_cmd_is_ready(struct msdc_host *host, 1256 struct mmc_request *mrq, struct mmc_command *cmd) 1257 { 1258 u32 val; 1259 int ret; 1260 1261 /* The max busy time we can endure is 20ms */ 1262 ret = readl_poll_timeout_atomic(host->base + SDC_STS, val, 1263 !(val & SDC_STS_CMDBUSY), 1, 20000); 1264 if (ret) { 1265 dev_err(host->dev, "CMD bus busy detected\n"); 1266 host->error |= REQ_CMD_BUSY; 1267 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); 1268 return false; 1269 } 1270 1271 if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) { 1272 /* R1B or with data, should check SDCBUSY */ 1273 ret = readl_poll_timeout_atomic(host->base + SDC_STS, val, 1274 !(val & SDC_STS_SDCBUSY), 1, 20000); 1275 if (ret) { 1276 dev_err(host->dev, "Controller busy detected\n"); 1277 host->error |= REQ_CMD_BUSY; 1278 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); 1279 return false; 1280 } 1281 } 1282 return true; 1283 } 1284 1285 static void msdc_start_command(struct msdc_host *host, 1286 struct mmc_request *mrq, struct mmc_command *cmd) 1287 { 1288 u32 rawcmd; 1289 unsigned long flags; 1290 1291 WARN_ON(host->cmd); 1292 host->cmd = cmd; 1293 1294 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); 1295 if (!msdc_cmd_is_ready(host, mrq, cmd)) 1296 return; 1297 1298 if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 || 1299 readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) { 1300 dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n"); 1301 msdc_reset_hw(host); 1302 } 1303 1304 cmd->error = 0; 1305 rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd); 1306 1307 spin_lock_irqsave(&host->lock, flags); 1308 sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask); 1309 spin_unlock_irqrestore(&host->lock, flags); 1310 1311 writel(cmd->arg, host->base + SDC_ARG); 1312 writel(rawcmd, host->base + SDC_CMD); 1313 } 1314 1315 static void msdc_cmd_next(struct msdc_host *host, 1316 struct mmc_request *mrq, struct mmc_command *cmd) 1317 { 1318 if ((cmd->error && 1319 !(cmd->error == -EILSEQ && 1320 (cmd->opcode == MMC_SEND_TUNING_BLOCK || 1321 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200 || 1322 host->hs400_tuning))) || 1323 (mrq->sbc && mrq->sbc->error)) 1324 msdc_request_done(host, mrq); 1325 else if (cmd == mrq->sbc) 1326 msdc_start_command(host, mrq, mrq->cmd); 1327 else if (!cmd->data) 1328 msdc_request_done(host, mrq); 1329 else 1330 msdc_start_data(host, cmd, cmd->data); 1331 } 1332 1333 static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq) 1334 { 1335 struct msdc_host *host = mmc_priv(mmc); 1336 1337 host->error = 0; 1338 WARN_ON(host->mrq); 1339 host->mrq = mrq; 1340 1341 if (mrq->data) 1342 msdc_prepare_data(host, mrq->data); 1343 1344 /* if SBC is required, we have HW option and SW option. 1345 * if HW option is enabled, and SBC does not have "special" flags, 1346 * use HW option, otherwise use SW option 1347 */ 1348 if (mrq->sbc && (!mmc_card_mmc(mmc->card) || 1349 (mrq->sbc->arg & 0xFFFF0000))) 1350 msdc_start_command(host, mrq, mrq->sbc); 1351 else 1352 msdc_start_command(host, mrq, mrq->cmd); 1353 } 1354 1355 static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) 1356 { 1357 struct msdc_host *host = mmc_priv(mmc); 1358 struct mmc_data *data = mrq->data; 1359 1360 if (!data) 1361 return; 1362 1363 msdc_prepare_data(host, data); 1364 data->host_cookie |= MSDC_ASYNC_FLAG; 1365 } 1366 1367 static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, 1368 int err) 1369 { 1370 struct msdc_host *host = mmc_priv(mmc); 1371 struct mmc_data *data = mrq->data; 1372 1373 if (!data) 1374 return; 1375 1376 if (data->host_cookie) { 1377 data->host_cookie &= ~MSDC_ASYNC_FLAG; 1378 msdc_unprepare_data(host, data); 1379 } 1380 } 1381 1382 static void msdc_data_xfer_next(struct msdc_host *host, struct mmc_request *mrq) 1383 { 1384 if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error && 1385 !mrq->sbc) 1386 msdc_start_command(host, mrq, mrq->stop); 1387 else 1388 msdc_request_done(host, mrq); 1389 } 1390 1391 static void msdc_data_xfer_done(struct msdc_host *host, u32 events, 1392 struct mmc_request *mrq, struct mmc_data *data) 1393 { 1394 struct mmc_command *stop; 1395 unsigned long flags; 1396 bool done; 1397 unsigned int check_data = events & 1398 (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO 1399 | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR 1400 | MSDC_INT_DMA_PROTECT); 1401 u32 val; 1402 int ret; 1403 1404 spin_lock_irqsave(&host->lock, flags); 1405 done = !host->data; 1406 if (check_data) 1407 host->data = NULL; 1408 spin_unlock_irqrestore(&host->lock, flags); 1409 1410 if (done) 1411 return; 1412 stop = data->stop; 1413 1414 if (check_data || (stop && stop->error)) { 1415 dev_dbg(host->dev, "DMA status: 0x%8X\n", 1416 readl(host->base + MSDC_DMA_CFG)); 1417 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1418 1); 1419 1420 ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CTRL, val, 1421 !(val & MSDC_DMA_CTRL_STOP), 1, 20000); 1422 if (ret) 1423 dev_dbg(host->dev, "DMA stop timed out\n"); 1424 1425 ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CFG, val, 1426 !(val & MSDC_DMA_CFG_STS), 1, 20000); 1427 if (ret) 1428 dev_dbg(host->dev, "DMA inactive timed out\n"); 1429 1430 sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask); 1431 dev_dbg(host->dev, "DMA stop\n"); 1432 1433 if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) { 1434 data->bytes_xfered = data->blocks * data->blksz; 1435 } else { 1436 dev_dbg(host->dev, "interrupt events: %x\n", events); 1437 msdc_reset_hw(host); 1438 host->error |= REQ_DAT_ERR; 1439 data->bytes_xfered = 0; 1440 1441 if (events & MSDC_INT_DATTMO) 1442 data->error = -ETIMEDOUT; 1443 else if (events & MSDC_INT_DATCRCERR) 1444 data->error = -EILSEQ; 1445 1446 dev_dbg(host->dev, "%s: cmd=%d; blocks=%d", 1447 __func__, mrq->cmd->opcode, data->blocks); 1448 dev_dbg(host->dev, "data_error=%d xfer_size=%d\n", 1449 (int)data->error, data->bytes_xfered); 1450 } 1451 1452 msdc_data_xfer_next(host, mrq); 1453 } 1454 } 1455 1456 static void msdc_set_buswidth(struct msdc_host *host, u32 width) 1457 { 1458 u32 val = readl(host->base + SDC_CFG); 1459 1460 val &= ~SDC_CFG_BUSWIDTH; 1461 1462 switch (width) { 1463 default: 1464 case MMC_BUS_WIDTH_1: 1465 val |= (MSDC_BUS_1BITS << 16); 1466 break; 1467 case MMC_BUS_WIDTH_4: 1468 val |= (MSDC_BUS_4BITS << 16); 1469 break; 1470 case MMC_BUS_WIDTH_8: 1471 val |= (MSDC_BUS_8BITS << 16); 1472 break; 1473 } 1474 1475 writel(val, host->base + SDC_CFG); 1476 dev_dbg(host->dev, "Bus Width = %d", width); 1477 } 1478 1479 static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios) 1480 { 1481 struct msdc_host *host = mmc_priv(mmc); 1482 int ret; 1483 1484 if (!IS_ERR(mmc->supply.vqmmc)) { 1485 if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 && 1486 ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) { 1487 dev_err(host->dev, "Unsupported signal voltage!\n"); 1488 return -EINVAL; 1489 } 1490 1491 ret = mmc_regulator_set_vqmmc(mmc, ios); 1492 if (ret < 0) { 1493 dev_dbg(host->dev, "Regulator set error %d (%d)\n", 1494 ret, ios->signal_voltage); 1495 return ret; 1496 } 1497 1498 /* Apply different pinctrl settings for different signal voltage */ 1499 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) 1500 pinctrl_select_state(host->pinctrl, host->pins_uhs); 1501 else 1502 pinctrl_select_state(host->pinctrl, host->pins_default); 1503 } 1504 return 0; 1505 } 1506 1507 static int msdc_card_busy(struct mmc_host *mmc) 1508 { 1509 struct msdc_host *host = mmc_priv(mmc); 1510 u32 status = readl(host->base + MSDC_PS); 1511 1512 /* only check if data0 is low */ 1513 return !(status & BIT(16)); 1514 } 1515 1516 static void msdc_request_timeout(struct work_struct *work) 1517 { 1518 struct msdc_host *host = container_of(work, struct msdc_host, 1519 req_timeout.work); 1520 1521 /* simulate HW timeout status */ 1522 dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__); 1523 if (host->mrq) { 1524 dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__, 1525 host->mrq, host->mrq->cmd->opcode); 1526 if (host->cmd) { 1527 dev_err(host->dev, "%s: aborting cmd=%d\n", 1528 __func__, host->cmd->opcode); 1529 msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq, 1530 host->cmd); 1531 } else if (host->data) { 1532 dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n", 1533 __func__, host->mrq->cmd->opcode, 1534 host->data->blocks); 1535 msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq, 1536 host->data); 1537 } 1538 } 1539 } 1540 1541 static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb) 1542 { 1543 if (enb) { 1544 sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 1545 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 1546 if (host->dev_comp->recheck_sdio_irq) 1547 msdc_recheck_sdio_irq(host); 1548 } else { 1549 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 1550 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 1551 } 1552 } 1553 1554 static void msdc_enable_sdio_irq(struct mmc_host *mmc, int enb) 1555 { 1556 struct msdc_host *host = mmc_priv(mmc); 1557 unsigned long flags; 1558 int ret; 1559 1560 spin_lock_irqsave(&host->lock, flags); 1561 __msdc_enable_sdio_irq(host, enb); 1562 spin_unlock_irqrestore(&host->lock, flags); 1563 1564 if (mmc_card_enable_async_irq(mmc->card) && host->pins_eint) { 1565 if (enb) { 1566 /* 1567 * In dev_pm_set_dedicated_wake_irq_reverse(), eint pin will be set to 1568 * GPIO mode. We need to restore it to SDIO DAT1 mode after that. 1569 * Since the current pinstate is pins_uhs, to ensure pinctrl select take 1570 * affect successfully, we change the pinstate to pins_eint firstly. 1571 */ 1572 pinctrl_select_state(host->pinctrl, host->pins_eint); 1573 ret = dev_pm_set_dedicated_wake_irq_reverse(host->dev, host->eint_irq); 1574 1575 if (ret) { 1576 dev_err(host->dev, "Failed to register SDIO wakeup irq!\n"); 1577 host->pins_eint = NULL; 1578 pm_runtime_get_noresume(host->dev); 1579 } else { 1580 dev_dbg(host->dev, "SDIO eint irq: %d!\n", host->eint_irq); 1581 } 1582 1583 pinctrl_select_state(host->pinctrl, host->pins_uhs); 1584 } else { 1585 dev_pm_clear_wake_irq(host->dev); 1586 } 1587 } else { 1588 if (enb) { 1589 /* Ensure host->pins_eint is NULL */ 1590 host->pins_eint = NULL; 1591 pm_runtime_get_noresume(host->dev); 1592 } else { 1593 pm_runtime_put_noidle(host->dev); 1594 } 1595 } 1596 } 1597 1598 static irqreturn_t msdc_cmdq_irq(struct msdc_host *host, u32 intsts) 1599 { 1600 struct mmc_host *mmc = mmc_from_priv(host); 1601 int cmd_err = 0, dat_err = 0; 1602 1603 if (intsts & MSDC_INT_RSPCRCERR) { 1604 cmd_err = -EILSEQ; 1605 dev_err(host->dev, "%s: CMD CRC ERR", __func__); 1606 } else if (intsts & MSDC_INT_CMDTMO) { 1607 cmd_err = -ETIMEDOUT; 1608 dev_err(host->dev, "%s: CMD TIMEOUT ERR", __func__); 1609 } 1610 1611 if (intsts & MSDC_INT_DATCRCERR) { 1612 dat_err = -EILSEQ; 1613 dev_err(host->dev, "%s: DATA CRC ERR", __func__); 1614 } else if (intsts & MSDC_INT_DATTMO) { 1615 dat_err = -ETIMEDOUT; 1616 dev_err(host->dev, "%s: DATA TIMEOUT ERR", __func__); 1617 } 1618 1619 if (cmd_err || dat_err) { 1620 dev_err(host->dev, "cmd_err = %d, dat_err =%d, intsts = 0x%x", 1621 cmd_err, dat_err, intsts); 1622 } 1623 1624 return cqhci_irq(mmc, 0, cmd_err, dat_err); 1625 } 1626 1627 static irqreturn_t msdc_irq(int irq, void *dev_id) 1628 { 1629 struct msdc_host *host = (struct msdc_host *) dev_id; 1630 struct mmc_host *mmc = mmc_from_priv(host); 1631 1632 while (true) { 1633 struct mmc_request *mrq; 1634 struct mmc_command *cmd; 1635 struct mmc_data *data; 1636 u32 events, event_mask; 1637 1638 spin_lock(&host->lock); 1639 events = readl(host->base + MSDC_INT); 1640 event_mask = readl(host->base + MSDC_INTEN); 1641 if ((events & event_mask) & MSDC_INT_SDIOIRQ) 1642 __msdc_enable_sdio_irq(host, 0); 1643 /* clear interrupts */ 1644 writel(events & event_mask, host->base + MSDC_INT); 1645 1646 mrq = host->mrq; 1647 cmd = host->cmd; 1648 data = host->data; 1649 spin_unlock(&host->lock); 1650 1651 if ((events & event_mask) & MSDC_INT_SDIOIRQ) 1652 sdio_signal_irq(mmc); 1653 1654 if ((events & event_mask) & MSDC_INT_CDSC) { 1655 if (host->internal_cd) 1656 mmc_detect_change(mmc, msecs_to_jiffies(20)); 1657 events &= ~MSDC_INT_CDSC; 1658 } 1659 1660 if (!(events & (event_mask & ~MSDC_INT_SDIOIRQ))) 1661 break; 1662 1663 if ((mmc->caps2 & MMC_CAP2_CQE) && 1664 (events & MSDC_INT_CMDQ)) { 1665 msdc_cmdq_irq(host, events); 1666 /* clear interrupts */ 1667 writel(events, host->base + MSDC_INT); 1668 return IRQ_HANDLED; 1669 } 1670 1671 if (!mrq) { 1672 dev_err(host->dev, 1673 "%s: MRQ=NULL; events=%08X; event_mask=%08X\n", 1674 __func__, events, event_mask); 1675 WARN_ON(1); 1676 break; 1677 } 1678 1679 dev_dbg(host->dev, "%s: events=%08X\n", __func__, events); 1680 1681 if (cmd) 1682 msdc_cmd_done(host, events, mrq, cmd); 1683 else if (data) 1684 msdc_data_xfer_done(host, events, mrq, data); 1685 } 1686 1687 return IRQ_HANDLED; 1688 } 1689 1690 static void msdc_init_hw(struct msdc_host *host) 1691 { 1692 u32 val; 1693 u32 tune_reg = host->dev_comp->pad_tune_reg; 1694 struct mmc_host *mmc = mmc_from_priv(host); 1695 1696 if (host->reset) { 1697 reset_control_assert(host->reset); 1698 usleep_range(10, 50); 1699 reset_control_deassert(host->reset); 1700 } 1701 1702 /* Configure to MMC/SD mode, clock free running */ 1703 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN); 1704 1705 /* Reset */ 1706 msdc_reset_hw(host); 1707 1708 /* Disable and clear all interrupts */ 1709 writel(0, host->base + MSDC_INTEN); 1710 val = readl(host->base + MSDC_INT); 1711 writel(val, host->base + MSDC_INT); 1712 1713 /* Configure card detection */ 1714 if (host->internal_cd) { 1715 sdr_set_field(host->base + MSDC_PS, MSDC_PS_CDDEBOUNCE, 1716 DEFAULT_DEBOUNCE); 1717 sdr_set_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1718 sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC); 1719 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1720 } else { 1721 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1722 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1723 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC); 1724 } 1725 1726 if (host->top_base) { 1727 writel(0, host->top_base + EMMC_TOP_CONTROL); 1728 writel(0, host->top_base + EMMC_TOP_CMD); 1729 } else { 1730 writel(0, host->base + tune_reg); 1731 } 1732 writel(0, host->base + MSDC_IOCON); 1733 sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0); 1734 writel(0x403c0046, host->base + MSDC_PATCH_BIT); 1735 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1); 1736 writel(0xffff4089, host->base + MSDC_PATCH_BIT1); 1737 sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL); 1738 1739 if (host->dev_comp->stop_clk_fix) { 1740 sdr_set_field(host->base + MSDC_PATCH_BIT1, 1741 MSDC_PATCH_BIT1_STOP_DLY, 3); 1742 sdr_clr_bits(host->base + SDC_FIFO_CFG, 1743 SDC_FIFO_CFG_WRVALIDSEL); 1744 sdr_clr_bits(host->base + SDC_FIFO_CFG, 1745 SDC_FIFO_CFG_RDVALIDSEL); 1746 } 1747 1748 if (host->dev_comp->busy_check) 1749 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, BIT(7)); 1750 1751 if (host->dev_comp->async_fifo) { 1752 sdr_set_field(host->base + MSDC_PATCH_BIT2, 1753 MSDC_PB2_RESPWAIT, 3); 1754 if (host->dev_comp->enhance_rx) { 1755 if (host->top_base) 1756 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1757 SDC_RX_ENH_EN); 1758 else 1759 sdr_set_bits(host->base + SDC_ADV_CFG0, 1760 SDC_RX_ENHANCE_EN); 1761 } else { 1762 sdr_set_field(host->base + MSDC_PATCH_BIT2, 1763 MSDC_PB2_RESPSTSENSEL, 2); 1764 sdr_set_field(host->base + MSDC_PATCH_BIT2, 1765 MSDC_PB2_CRCSTSENSEL, 2); 1766 } 1767 /* use async fifo, then no need tune internal delay */ 1768 sdr_clr_bits(host->base + MSDC_PATCH_BIT2, 1769 MSDC_PATCH_BIT2_CFGRESP); 1770 sdr_set_bits(host->base + MSDC_PATCH_BIT2, 1771 MSDC_PATCH_BIT2_CFGCRCSTS); 1772 } 1773 1774 if (host->dev_comp->support_64g) 1775 sdr_set_bits(host->base + MSDC_PATCH_BIT2, 1776 MSDC_PB2_SUPPORT_64G); 1777 if (host->dev_comp->data_tune) { 1778 if (host->top_base) { 1779 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1780 PAD_DAT_RD_RXDLY_SEL); 1781 sdr_clr_bits(host->top_base + EMMC_TOP_CONTROL, 1782 DATA_K_VALUE_SEL); 1783 sdr_set_bits(host->top_base + EMMC_TOP_CMD, 1784 PAD_CMD_RD_RXDLY_SEL); 1785 } else { 1786 sdr_set_bits(host->base + tune_reg, 1787 MSDC_PAD_TUNE_RD_SEL | 1788 MSDC_PAD_TUNE_CMD_SEL); 1789 } 1790 } else { 1791 /* choose clock tune */ 1792 if (host->top_base) 1793 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1794 PAD_RXDLY_SEL); 1795 else 1796 sdr_set_bits(host->base + tune_reg, 1797 MSDC_PAD_TUNE_RXDLYSEL); 1798 } 1799 1800 if (mmc->caps2 & MMC_CAP2_NO_SDIO) { 1801 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIO); 1802 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 1803 sdr_clr_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER); 1804 } else { 1805 /* Configure to enable SDIO mode, otherwise SDIO CMD5 fails */ 1806 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO); 1807 1808 /* Config SDIO device detect interrupt function */ 1809 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 1810 sdr_set_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER); 1811 } 1812 1813 /* Configure to default data timeout */ 1814 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3); 1815 1816 host->def_tune_para.iocon = readl(host->base + MSDC_IOCON); 1817 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); 1818 if (host->top_base) { 1819 host->def_tune_para.emmc_top_control = 1820 readl(host->top_base + EMMC_TOP_CONTROL); 1821 host->def_tune_para.emmc_top_cmd = 1822 readl(host->top_base + EMMC_TOP_CMD); 1823 host->saved_tune_para.emmc_top_control = 1824 readl(host->top_base + EMMC_TOP_CONTROL); 1825 host->saved_tune_para.emmc_top_cmd = 1826 readl(host->top_base + EMMC_TOP_CMD); 1827 } else { 1828 host->def_tune_para.pad_tune = readl(host->base + tune_reg); 1829 host->saved_tune_para.pad_tune = readl(host->base + tune_reg); 1830 } 1831 dev_dbg(host->dev, "init hardware done!"); 1832 } 1833 1834 static void msdc_deinit_hw(struct msdc_host *host) 1835 { 1836 u32 val; 1837 1838 if (host->internal_cd) { 1839 /* Disabled card-detect */ 1840 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1841 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1842 } 1843 1844 /* Disable and clear all interrupts */ 1845 writel(0, host->base + MSDC_INTEN); 1846 1847 val = readl(host->base + MSDC_INT); 1848 writel(val, host->base + MSDC_INT); 1849 } 1850 1851 /* init gpd and bd list in msdc_drv_probe */ 1852 static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma) 1853 { 1854 struct mt_gpdma_desc *gpd = dma->gpd; 1855 struct mt_bdma_desc *bd = dma->bd; 1856 dma_addr_t dma_addr; 1857 int i; 1858 1859 memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2); 1860 1861 dma_addr = dma->gpd_addr + sizeof(struct mt_gpdma_desc); 1862 gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */ 1863 /* gpd->next is must set for desc DMA 1864 * That's why must alloc 2 gpd structure. 1865 */ 1866 gpd->next = lower_32_bits(dma_addr); 1867 if (host->dev_comp->support_64g) 1868 gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; 1869 1870 dma_addr = dma->bd_addr; 1871 gpd->ptr = lower_32_bits(dma->bd_addr); /* physical address */ 1872 if (host->dev_comp->support_64g) 1873 gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 28; 1874 1875 memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM); 1876 for (i = 0; i < (MAX_BD_NUM - 1); i++) { 1877 dma_addr = dma->bd_addr + sizeof(*bd) * (i + 1); 1878 bd[i].next = lower_32_bits(dma_addr); 1879 if (host->dev_comp->support_64g) 1880 bd[i].bd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; 1881 } 1882 } 1883 1884 static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 1885 { 1886 struct msdc_host *host = mmc_priv(mmc); 1887 int ret; 1888 1889 msdc_set_buswidth(host, ios->bus_width); 1890 1891 /* Suspend/Resume will do power off/on */ 1892 switch (ios->power_mode) { 1893 case MMC_POWER_UP: 1894 if (!IS_ERR(mmc->supply.vmmc)) { 1895 msdc_init_hw(host); 1896 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 1897 ios->vdd); 1898 if (ret) { 1899 dev_err(host->dev, "Failed to set vmmc power!\n"); 1900 return; 1901 } 1902 } 1903 break; 1904 case MMC_POWER_ON: 1905 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { 1906 ret = regulator_enable(mmc->supply.vqmmc); 1907 if (ret) 1908 dev_err(host->dev, "Failed to set vqmmc power!\n"); 1909 else 1910 host->vqmmc_enabled = true; 1911 } 1912 break; 1913 case MMC_POWER_OFF: 1914 if (!IS_ERR(mmc->supply.vmmc)) 1915 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); 1916 1917 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { 1918 regulator_disable(mmc->supply.vqmmc); 1919 host->vqmmc_enabled = false; 1920 } 1921 break; 1922 default: 1923 break; 1924 } 1925 1926 if (host->mclk != ios->clock || host->timing != ios->timing) 1927 msdc_set_mclk(host, ios->timing, ios->clock); 1928 } 1929 1930 static u32 test_delay_bit(u32 delay, u32 bit) 1931 { 1932 bit %= PAD_DELAY_MAX; 1933 return delay & BIT(bit); 1934 } 1935 1936 static int get_delay_len(u32 delay, u32 start_bit) 1937 { 1938 int i; 1939 1940 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) { 1941 if (test_delay_bit(delay, start_bit + i) == 0) 1942 return i; 1943 } 1944 return PAD_DELAY_MAX - start_bit; 1945 } 1946 1947 static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay) 1948 { 1949 int start = 0, len = 0; 1950 int start_final = 0, len_final = 0; 1951 u8 final_phase = 0xff; 1952 struct msdc_delay_phase delay_phase = { 0, }; 1953 1954 if (delay == 0) { 1955 dev_err(host->dev, "phase error: [map:%x]\n", delay); 1956 delay_phase.final_phase = final_phase; 1957 return delay_phase; 1958 } 1959 1960 while (start < PAD_DELAY_MAX) { 1961 len = get_delay_len(delay, start); 1962 if (len_final < len) { 1963 start_final = start; 1964 len_final = len; 1965 } 1966 start += len ? len : 1; 1967 if (len >= 12 && start_final < 4) 1968 break; 1969 } 1970 1971 /* The rule is that to find the smallest delay cell */ 1972 if (start_final == 0) 1973 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX; 1974 else 1975 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX; 1976 dev_dbg(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n", 1977 delay, len_final, final_phase); 1978 1979 delay_phase.maxlen = len_final; 1980 delay_phase.start = start_final; 1981 delay_phase.final_phase = final_phase; 1982 return delay_phase; 1983 } 1984 1985 static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value) 1986 { 1987 u32 tune_reg = host->dev_comp->pad_tune_reg; 1988 1989 if (host->top_base) 1990 sdr_set_field(host->top_base + EMMC_TOP_CMD, PAD_CMD_RXDLY, 1991 value); 1992 else 1993 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY, 1994 value); 1995 } 1996 1997 static inline void msdc_set_data_delay(struct msdc_host *host, u32 value) 1998 { 1999 u32 tune_reg = host->dev_comp->pad_tune_reg; 2000 2001 if (host->top_base) 2002 sdr_set_field(host->top_base + EMMC_TOP_CONTROL, 2003 PAD_DAT_RD_RXDLY, value); 2004 else 2005 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_DATRRDLY, 2006 value); 2007 } 2008 2009 static int msdc_tune_response(struct mmc_host *mmc, u32 opcode) 2010 { 2011 struct msdc_host *host = mmc_priv(mmc); 2012 u32 rise_delay = 0, fall_delay = 0; 2013 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 2014 struct msdc_delay_phase internal_delay_phase; 2015 u8 final_delay, final_maxlen; 2016 u32 internal_delay = 0; 2017 u32 tune_reg = host->dev_comp->pad_tune_reg; 2018 int cmd_err; 2019 int i, j; 2020 2021 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || 2022 mmc->ios.timing == MMC_TIMING_UHS_SDR104) 2023 sdr_set_field(host->base + tune_reg, 2024 MSDC_PAD_TUNE_CMDRRDLY, 2025 host->hs200_cmd_int_delay); 2026 2027 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2028 for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2029 msdc_set_cmd_delay(host, i); 2030 /* 2031 * Using the same parameters, it may sometimes pass the test, 2032 * but sometimes it may fail. To make sure the parameters are 2033 * more stable, we test each set of parameters 3 times. 2034 */ 2035 for (j = 0; j < 3; j++) { 2036 mmc_send_tuning(mmc, opcode, &cmd_err); 2037 if (!cmd_err) { 2038 rise_delay |= BIT(i); 2039 } else { 2040 rise_delay &= ~BIT(i); 2041 break; 2042 } 2043 } 2044 } 2045 final_rise_delay = get_best_delay(host, rise_delay); 2046 /* if rising edge has enough margin, then do not scan falling edge */ 2047 if (final_rise_delay.maxlen >= 12 || 2048 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 2049 goto skip_fall; 2050 2051 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2052 for (i = 0; i < PAD_DELAY_MAX; i++) { 2053 msdc_set_cmd_delay(host, i); 2054 /* 2055 * Using the same parameters, it may sometimes pass the test, 2056 * but sometimes it may fail. To make sure the parameters are 2057 * more stable, we test each set of parameters 3 times. 2058 */ 2059 for (j = 0; j < 3; j++) { 2060 mmc_send_tuning(mmc, opcode, &cmd_err); 2061 if (!cmd_err) { 2062 fall_delay |= BIT(i); 2063 } else { 2064 fall_delay &= ~BIT(i); 2065 break; 2066 } 2067 } 2068 } 2069 final_fall_delay = get_best_delay(host, fall_delay); 2070 2071 skip_fall: 2072 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 2073 if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4) 2074 final_maxlen = final_fall_delay.maxlen; 2075 if (final_maxlen == final_rise_delay.maxlen) { 2076 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2077 final_delay = final_rise_delay.final_phase; 2078 } else { 2079 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2080 final_delay = final_fall_delay.final_phase; 2081 } 2082 msdc_set_cmd_delay(host, final_delay); 2083 2084 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay) 2085 goto skip_internal; 2086 2087 for (i = 0; i < PAD_DELAY_MAX; i++) { 2088 sdr_set_field(host->base + tune_reg, 2089 MSDC_PAD_TUNE_CMDRRDLY, i); 2090 mmc_send_tuning(mmc, opcode, &cmd_err); 2091 if (!cmd_err) 2092 internal_delay |= BIT(i); 2093 } 2094 dev_dbg(host->dev, "Final internal delay: 0x%x\n", internal_delay); 2095 internal_delay_phase = get_best_delay(host, internal_delay); 2096 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY, 2097 internal_delay_phase.final_phase); 2098 skip_internal: 2099 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); 2100 return final_delay == 0xff ? -EIO : 0; 2101 } 2102 2103 static int hs400_tune_response(struct mmc_host *mmc, u32 opcode) 2104 { 2105 struct msdc_host *host = mmc_priv(mmc); 2106 u32 cmd_delay = 0; 2107 struct msdc_delay_phase final_cmd_delay = { 0,}; 2108 u8 final_delay; 2109 int cmd_err; 2110 int i, j; 2111 2112 /* select EMMC50 PAD CMD tune */ 2113 sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0)); 2114 sdr_set_field(host->base + MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMDTA, 2); 2115 2116 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || 2117 mmc->ios.timing == MMC_TIMING_UHS_SDR104) 2118 sdr_set_field(host->base + MSDC_PAD_TUNE, 2119 MSDC_PAD_TUNE_CMDRRDLY, 2120 host->hs200_cmd_int_delay); 2121 2122 if (host->hs400_cmd_resp_sel_rising) 2123 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2124 else 2125 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2126 for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2127 sdr_set_field(host->base + PAD_CMD_TUNE, 2128 PAD_CMD_TUNE_RX_DLY3, i); 2129 /* 2130 * Using the same parameters, it may sometimes pass the test, 2131 * but sometimes it may fail. To make sure the parameters are 2132 * more stable, we test each set of parameters 3 times. 2133 */ 2134 for (j = 0; j < 3; j++) { 2135 mmc_send_tuning(mmc, opcode, &cmd_err); 2136 if (!cmd_err) { 2137 cmd_delay |= BIT(i); 2138 } else { 2139 cmd_delay &= ~BIT(i); 2140 break; 2141 } 2142 } 2143 } 2144 final_cmd_delay = get_best_delay(host, cmd_delay); 2145 sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3, 2146 final_cmd_delay.final_phase); 2147 final_delay = final_cmd_delay.final_phase; 2148 2149 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); 2150 return final_delay == 0xff ? -EIO : 0; 2151 } 2152 2153 static int msdc_tune_data(struct mmc_host *mmc, u32 opcode) 2154 { 2155 struct msdc_host *host = mmc_priv(mmc); 2156 u32 rise_delay = 0, fall_delay = 0; 2157 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 2158 u8 final_delay, final_maxlen; 2159 int i, ret; 2160 2161 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, 2162 host->latch_ck); 2163 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 2164 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 2165 for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2166 msdc_set_data_delay(host, i); 2167 ret = mmc_send_tuning(mmc, opcode, NULL); 2168 if (!ret) 2169 rise_delay |= BIT(i); 2170 } 2171 final_rise_delay = get_best_delay(host, rise_delay); 2172 /* if rising edge has enough margin, then do not scan falling edge */ 2173 if (final_rise_delay.maxlen >= 12 || 2174 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 2175 goto skip_fall; 2176 2177 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 2178 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 2179 for (i = 0; i < PAD_DELAY_MAX; i++) { 2180 msdc_set_data_delay(host, i); 2181 ret = mmc_send_tuning(mmc, opcode, NULL); 2182 if (!ret) 2183 fall_delay |= BIT(i); 2184 } 2185 final_fall_delay = get_best_delay(host, fall_delay); 2186 2187 skip_fall: 2188 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 2189 if (final_maxlen == final_rise_delay.maxlen) { 2190 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 2191 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 2192 final_delay = final_rise_delay.final_phase; 2193 } else { 2194 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 2195 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 2196 final_delay = final_fall_delay.final_phase; 2197 } 2198 msdc_set_data_delay(host, final_delay); 2199 2200 dev_dbg(host->dev, "Final data pad delay: %x\n", final_delay); 2201 return final_delay == 0xff ? -EIO : 0; 2202 } 2203 2204 /* 2205 * MSDC IP which supports data tune + async fifo can do CMD/DAT tune 2206 * together, which can save the tuning time. 2207 */ 2208 static int msdc_tune_together(struct mmc_host *mmc, u32 opcode) 2209 { 2210 struct msdc_host *host = mmc_priv(mmc); 2211 u32 rise_delay = 0, fall_delay = 0; 2212 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 2213 u8 final_delay, final_maxlen; 2214 int i, ret; 2215 2216 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, 2217 host->latch_ck); 2218 2219 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2220 sdr_clr_bits(host->base + MSDC_IOCON, 2221 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2222 for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2223 msdc_set_cmd_delay(host, i); 2224 msdc_set_data_delay(host, i); 2225 ret = mmc_send_tuning(mmc, opcode, NULL); 2226 if (!ret) 2227 rise_delay |= BIT(i); 2228 } 2229 final_rise_delay = get_best_delay(host, rise_delay); 2230 /* if rising edge has enough margin, then do not scan falling edge */ 2231 if (final_rise_delay.maxlen >= 12 || 2232 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 2233 goto skip_fall; 2234 2235 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2236 sdr_set_bits(host->base + MSDC_IOCON, 2237 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2238 for (i = 0; i < PAD_DELAY_MAX; i++) { 2239 msdc_set_cmd_delay(host, i); 2240 msdc_set_data_delay(host, i); 2241 ret = mmc_send_tuning(mmc, opcode, NULL); 2242 if (!ret) 2243 fall_delay |= BIT(i); 2244 } 2245 final_fall_delay = get_best_delay(host, fall_delay); 2246 2247 skip_fall: 2248 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 2249 if (final_maxlen == final_rise_delay.maxlen) { 2250 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2251 sdr_clr_bits(host->base + MSDC_IOCON, 2252 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2253 final_delay = final_rise_delay.final_phase; 2254 } else { 2255 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 2256 sdr_set_bits(host->base + MSDC_IOCON, 2257 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2258 final_delay = final_fall_delay.final_phase; 2259 } 2260 2261 msdc_set_cmd_delay(host, final_delay); 2262 msdc_set_data_delay(host, final_delay); 2263 2264 dev_dbg(host->dev, "Final pad delay: %x\n", final_delay); 2265 return final_delay == 0xff ? -EIO : 0; 2266 } 2267 2268 static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode) 2269 { 2270 struct msdc_host *host = mmc_priv(mmc); 2271 int ret; 2272 u32 tune_reg = host->dev_comp->pad_tune_reg; 2273 2274 if (host->dev_comp->data_tune && host->dev_comp->async_fifo) { 2275 ret = msdc_tune_together(mmc, opcode); 2276 if (host->hs400_mode) { 2277 sdr_clr_bits(host->base + MSDC_IOCON, 2278 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2279 msdc_set_data_delay(host, 0); 2280 } 2281 goto tune_done; 2282 } 2283 if (host->hs400_mode && 2284 host->dev_comp->hs400_tune) 2285 ret = hs400_tune_response(mmc, opcode); 2286 else 2287 ret = msdc_tune_response(mmc, opcode); 2288 if (ret == -EIO) { 2289 dev_err(host->dev, "Tune response fail!\n"); 2290 return ret; 2291 } 2292 if (host->hs400_mode == false) { 2293 ret = msdc_tune_data(mmc, opcode); 2294 if (ret == -EIO) 2295 dev_err(host->dev, "Tune data fail!\n"); 2296 } 2297 2298 tune_done: 2299 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); 2300 host->saved_tune_para.pad_tune = readl(host->base + tune_reg); 2301 host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); 2302 if (host->top_base) { 2303 host->saved_tune_para.emmc_top_control = readl(host->top_base + 2304 EMMC_TOP_CONTROL); 2305 host->saved_tune_para.emmc_top_cmd = readl(host->top_base + 2306 EMMC_TOP_CMD); 2307 } 2308 return ret; 2309 } 2310 2311 static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) 2312 { 2313 struct msdc_host *host = mmc_priv(mmc); 2314 host->hs400_mode = true; 2315 2316 if (host->top_base) 2317 writel(host->hs400_ds_delay, 2318 host->top_base + EMMC50_PAD_DS_TUNE); 2319 else 2320 writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE); 2321 /* hs400 mode must set it to 0 */ 2322 sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS); 2323 /* to improve read performance, set outstanding to 2 */ 2324 sdr_set_field(host->base + EMMC50_CFG3, EMMC50_CFG3_OUTS_WR, 2); 2325 2326 return 0; 2327 } 2328 2329 static int msdc_execute_hs400_tuning(struct mmc_host *mmc, struct mmc_card *card) 2330 { 2331 struct msdc_host *host = mmc_priv(mmc); 2332 struct msdc_delay_phase dly1_delay; 2333 u32 val, result_dly1 = 0; 2334 u8 *ext_csd; 2335 int i, ret; 2336 2337 if (host->top_base) { 2338 sdr_set_bits(host->top_base + EMMC50_PAD_DS_TUNE, 2339 PAD_DS_DLY_SEL); 2340 if (host->hs400_ds_dly3) 2341 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2342 PAD_DS_DLY3, host->hs400_ds_dly3); 2343 } else { 2344 sdr_set_bits(host->base + PAD_DS_TUNE, PAD_DS_TUNE_DLY_SEL); 2345 if (host->hs400_ds_dly3) 2346 sdr_set_field(host->base + PAD_DS_TUNE, 2347 PAD_DS_TUNE_DLY3, host->hs400_ds_dly3); 2348 } 2349 2350 host->hs400_tuning = true; 2351 for (i = 0; i < PAD_DELAY_MAX; i++) { 2352 if (host->top_base) 2353 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2354 PAD_DS_DLY1, i); 2355 else 2356 sdr_set_field(host->base + PAD_DS_TUNE, 2357 PAD_DS_TUNE_DLY1, i); 2358 ret = mmc_get_ext_csd(card, &ext_csd); 2359 if (!ret) { 2360 result_dly1 |= BIT(i); 2361 kfree(ext_csd); 2362 } 2363 } 2364 host->hs400_tuning = false; 2365 2366 dly1_delay = get_best_delay(host, result_dly1); 2367 if (dly1_delay.maxlen == 0) { 2368 dev_err(host->dev, "Failed to get DLY1 delay!\n"); 2369 goto fail; 2370 } 2371 if (host->top_base) 2372 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2373 PAD_DS_DLY1, dly1_delay.final_phase); 2374 else 2375 sdr_set_field(host->base + PAD_DS_TUNE, 2376 PAD_DS_TUNE_DLY1, dly1_delay.final_phase); 2377 2378 if (host->top_base) 2379 val = readl(host->top_base + EMMC50_PAD_DS_TUNE); 2380 else 2381 val = readl(host->base + PAD_DS_TUNE); 2382 2383 dev_info(host->dev, "Final PAD_DS_TUNE: 0x%x\n", val); 2384 2385 return 0; 2386 2387 fail: 2388 dev_err(host->dev, "Failed to tuning DS pin delay!\n"); 2389 return -EIO; 2390 } 2391 2392 static void msdc_hw_reset(struct mmc_host *mmc) 2393 { 2394 struct msdc_host *host = mmc_priv(mmc); 2395 2396 sdr_set_bits(host->base + EMMC_IOCON, 1); 2397 udelay(10); /* 10us is enough */ 2398 sdr_clr_bits(host->base + EMMC_IOCON, 1); 2399 } 2400 2401 static void msdc_ack_sdio_irq(struct mmc_host *mmc) 2402 { 2403 unsigned long flags; 2404 struct msdc_host *host = mmc_priv(mmc); 2405 2406 spin_lock_irqsave(&host->lock, flags); 2407 __msdc_enable_sdio_irq(host, 1); 2408 spin_unlock_irqrestore(&host->lock, flags); 2409 } 2410 2411 static int msdc_get_cd(struct mmc_host *mmc) 2412 { 2413 struct msdc_host *host = mmc_priv(mmc); 2414 int val; 2415 2416 if (mmc->caps & MMC_CAP_NONREMOVABLE) 2417 return 1; 2418 2419 if (!host->internal_cd) 2420 return mmc_gpio_get_cd(mmc); 2421 2422 val = readl(host->base + MSDC_PS) & MSDC_PS_CDSTS; 2423 if (mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) 2424 return !!val; 2425 else 2426 return !val; 2427 } 2428 2429 static void msdc_hs400_enhanced_strobe(struct mmc_host *mmc, 2430 struct mmc_ios *ios) 2431 { 2432 struct msdc_host *host = mmc_priv(mmc); 2433 2434 if (ios->enhanced_strobe) { 2435 msdc_prepare_hs400_tuning(mmc, ios); 2436 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 1); 2437 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 1); 2438 sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 1); 2439 2440 sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL); 2441 sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL); 2442 sdr_clr_bits(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT); 2443 } else { 2444 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 0); 2445 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 0); 2446 sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 0); 2447 2448 sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL); 2449 sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL); 2450 sdr_set_field(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT, 0xb4); 2451 } 2452 } 2453 2454 static void msdc_cqe_enable(struct mmc_host *mmc) 2455 { 2456 struct msdc_host *host = mmc_priv(mmc); 2457 2458 /* enable cmdq irq */ 2459 writel(MSDC_INT_CMDQ, host->base + MSDC_INTEN); 2460 /* enable busy check */ 2461 sdr_set_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL); 2462 /* default write data / busy timeout 20s */ 2463 msdc_set_busy_timeout(host, 20 * 1000000000ULL, 0); 2464 /* default read data timeout 1s */ 2465 msdc_set_timeout(host, 1000000000ULL, 0); 2466 } 2467 2468 static void msdc_cqe_disable(struct mmc_host *mmc, bool recovery) 2469 { 2470 struct msdc_host *host = mmc_priv(mmc); 2471 unsigned int val = 0; 2472 2473 /* disable cmdq irq */ 2474 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INT_CMDQ); 2475 /* disable busy check */ 2476 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL); 2477 2478 val = readl(host->base + MSDC_INT); 2479 writel(val, host->base + MSDC_INT); 2480 2481 if (recovery) { 2482 sdr_set_field(host->base + MSDC_DMA_CTRL, 2483 MSDC_DMA_CTRL_STOP, 1); 2484 if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CTRL, val, 2485 !(val & MSDC_DMA_CTRL_STOP), 1, 3000))) 2486 return; 2487 if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CFG, val, 2488 !(val & MSDC_DMA_CFG_STS), 1, 3000))) 2489 return; 2490 msdc_reset_hw(host); 2491 } 2492 } 2493 2494 static void msdc_cqe_pre_enable(struct mmc_host *mmc) 2495 { 2496 struct cqhci_host *cq_host = mmc->cqe_private; 2497 u32 reg; 2498 2499 reg = cqhci_readl(cq_host, CQHCI_CFG); 2500 reg |= CQHCI_ENABLE; 2501 cqhci_writel(cq_host, reg, CQHCI_CFG); 2502 } 2503 2504 static void msdc_cqe_post_disable(struct mmc_host *mmc) 2505 { 2506 struct cqhci_host *cq_host = mmc->cqe_private; 2507 u32 reg; 2508 2509 reg = cqhci_readl(cq_host, CQHCI_CFG); 2510 reg &= ~CQHCI_ENABLE; 2511 cqhci_writel(cq_host, reg, CQHCI_CFG); 2512 } 2513 2514 static const struct mmc_host_ops mt_msdc_ops = { 2515 .post_req = msdc_post_req, 2516 .pre_req = msdc_pre_req, 2517 .request = msdc_ops_request, 2518 .set_ios = msdc_ops_set_ios, 2519 .get_ro = mmc_gpio_get_ro, 2520 .get_cd = msdc_get_cd, 2521 .hs400_enhanced_strobe = msdc_hs400_enhanced_strobe, 2522 .enable_sdio_irq = msdc_enable_sdio_irq, 2523 .ack_sdio_irq = msdc_ack_sdio_irq, 2524 .start_signal_voltage_switch = msdc_ops_switch_volt, 2525 .card_busy = msdc_card_busy, 2526 .execute_tuning = msdc_execute_tuning, 2527 .prepare_hs400_tuning = msdc_prepare_hs400_tuning, 2528 .execute_hs400_tuning = msdc_execute_hs400_tuning, 2529 .card_hw_reset = msdc_hw_reset, 2530 }; 2531 2532 static const struct cqhci_host_ops msdc_cmdq_ops = { 2533 .enable = msdc_cqe_enable, 2534 .disable = msdc_cqe_disable, 2535 .pre_enable = msdc_cqe_pre_enable, 2536 .post_disable = msdc_cqe_post_disable, 2537 }; 2538 2539 static void msdc_of_property_parse(struct platform_device *pdev, 2540 struct msdc_host *host) 2541 { 2542 of_property_read_u32(pdev->dev.of_node, "mediatek,latch-ck", 2543 &host->latch_ck); 2544 2545 of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay", 2546 &host->hs400_ds_delay); 2547 2548 of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-ds-dly3", 2549 &host->hs400_ds_dly3); 2550 2551 of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay", 2552 &host->hs200_cmd_int_delay); 2553 2554 of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay", 2555 &host->hs400_cmd_int_delay); 2556 2557 if (of_property_read_bool(pdev->dev.of_node, 2558 "mediatek,hs400-cmd-resp-sel-rising")) 2559 host->hs400_cmd_resp_sel_rising = true; 2560 else 2561 host->hs400_cmd_resp_sel_rising = false; 2562 2563 if (of_property_read_bool(pdev->dev.of_node, 2564 "supports-cqe")) 2565 host->cqhci = true; 2566 else 2567 host->cqhci = false; 2568 } 2569 2570 static int msdc_of_clock_parse(struct platform_device *pdev, 2571 struct msdc_host *host) 2572 { 2573 int ret; 2574 2575 host->src_clk = devm_clk_get(&pdev->dev, "source"); 2576 if (IS_ERR(host->src_clk)) 2577 return PTR_ERR(host->src_clk); 2578 2579 host->h_clk = devm_clk_get(&pdev->dev, "hclk"); 2580 if (IS_ERR(host->h_clk)) 2581 return PTR_ERR(host->h_clk); 2582 2583 host->bus_clk = devm_clk_get_optional(&pdev->dev, "bus_clk"); 2584 if (IS_ERR(host->bus_clk)) 2585 host->bus_clk = NULL; 2586 2587 /*source clock control gate is optional clock*/ 2588 host->src_clk_cg = devm_clk_get_optional(&pdev->dev, "source_cg"); 2589 if (IS_ERR(host->src_clk_cg)) 2590 return PTR_ERR(host->src_clk_cg); 2591 2592 /* 2593 * Fallback for legacy device-trees: src_clk and HCLK use the same 2594 * bit to control gating but they are parented to a different mux, 2595 * hence if our intention is to gate only the source, required 2596 * during a clk mode switch to avoid hw hangs, we need to gate 2597 * its parent (specified as a different clock only on new DTs). 2598 */ 2599 if (!host->src_clk_cg) { 2600 host->src_clk_cg = clk_get_parent(host->src_clk); 2601 if (IS_ERR(host->src_clk_cg)) 2602 return PTR_ERR(host->src_clk_cg); 2603 } 2604 2605 /* If present, always enable for this clock gate */ 2606 host->sys_clk_cg = devm_clk_get_optional_enabled(&pdev->dev, "sys_cg"); 2607 if (IS_ERR(host->sys_clk_cg)) 2608 host->sys_clk_cg = NULL; 2609 2610 host->bulk_clks[0].id = "pclk_cg"; 2611 host->bulk_clks[1].id = "axi_cg"; 2612 host->bulk_clks[2].id = "ahb_cg"; 2613 ret = devm_clk_bulk_get_optional(&pdev->dev, MSDC_NR_CLOCKS, 2614 host->bulk_clks); 2615 if (ret) { 2616 dev_err(&pdev->dev, "Cannot get pclk/axi/ahb clock gates\n"); 2617 return ret; 2618 } 2619 2620 return 0; 2621 } 2622 2623 static int msdc_drv_probe(struct platform_device *pdev) 2624 { 2625 struct mmc_host *mmc; 2626 struct msdc_host *host; 2627 struct resource *res; 2628 int ret; 2629 2630 if (!pdev->dev.of_node) { 2631 dev_err(&pdev->dev, "No DT found\n"); 2632 return -EINVAL; 2633 } 2634 2635 /* Allocate MMC host for this device */ 2636 mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev); 2637 if (!mmc) 2638 return -ENOMEM; 2639 2640 host = mmc_priv(mmc); 2641 ret = mmc_of_parse(mmc); 2642 if (ret) 2643 goto host_free; 2644 2645 host->base = devm_platform_ioremap_resource(pdev, 0); 2646 if (IS_ERR(host->base)) { 2647 ret = PTR_ERR(host->base); 2648 goto host_free; 2649 } 2650 2651 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 2652 if (res) { 2653 host->top_base = devm_ioremap_resource(&pdev->dev, res); 2654 if (IS_ERR(host->top_base)) 2655 host->top_base = NULL; 2656 } 2657 2658 ret = mmc_regulator_get_supply(mmc); 2659 if (ret) 2660 goto host_free; 2661 2662 ret = msdc_of_clock_parse(pdev, host); 2663 if (ret) 2664 goto host_free; 2665 2666 host->reset = devm_reset_control_get_optional_exclusive(&pdev->dev, 2667 "hrst"); 2668 if (IS_ERR(host->reset)) { 2669 ret = PTR_ERR(host->reset); 2670 goto host_free; 2671 } 2672 2673 host->irq = platform_get_irq(pdev, 0); 2674 if (host->irq < 0) { 2675 ret = -EINVAL; 2676 goto host_free; 2677 } 2678 2679 host->pinctrl = devm_pinctrl_get(&pdev->dev); 2680 if (IS_ERR(host->pinctrl)) { 2681 ret = PTR_ERR(host->pinctrl); 2682 dev_err(&pdev->dev, "Cannot find pinctrl!\n"); 2683 goto host_free; 2684 } 2685 2686 host->pins_default = pinctrl_lookup_state(host->pinctrl, "default"); 2687 if (IS_ERR(host->pins_default)) { 2688 ret = PTR_ERR(host->pins_default); 2689 dev_err(&pdev->dev, "Cannot find pinctrl default!\n"); 2690 goto host_free; 2691 } 2692 2693 host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs"); 2694 if (IS_ERR(host->pins_uhs)) { 2695 ret = PTR_ERR(host->pins_uhs); 2696 dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n"); 2697 goto host_free; 2698 } 2699 2700 /* Support for SDIO eint irq ? */ 2701 if ((mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ) && (mmc->pm_caps & MMC_PM_KEEP_POWER)) { 2702 host->eint_irq = platform_get_irq_byname(pdev, "sdio_wakeup"); 2703 if (host->eint_irq > 0) { 2704 host->pins_eint = pinctrl_lookup_state(host->pinctrl, "state_eint"); 2705 if (IS_ERR(host->pins_eint)) { 2706 dev_err(&pdev->dev, "Cannot find pinctrl eint!\n"); 2707 host->pins_eint = NULL; 2708 } else { 2709 device_init_wakeup(&pdev->dev, true); 2710 } 2711 } 2712 } 2713 2714 msdc_of_property_parse(pdev, host); 2715 2716 host->dev = &pdev->dev; 2717 host->dev_comp = of_device_get_match_data(&pdev->dev); 2718 host->src_clk_freq = clk_get_rate(host->src_clk); 2719 /* Set host parameters to mmc */ 2720 mmc->ops = &mt_msdc_ops; 2721 if (host->dev_comp->clk_div_bits == 8) 2722 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255); 2723 else 2724 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095); 2725 2726 if (!(mmc->caps & MMC_CAP_NONREMOVABLE) && 2727 !mmc_can_gpio_cd(mmc) && 2728 host->dev_comp->use_internal_cd) { 2729 /* 2730 * Is removable but no GPIO declared, so 2731 * use internal functionality. 2732 */ 2733 host->internal_cd = true; 2734 } 2735 2736 if (mmc->caps & MMC_CAP_SDIO_IRQ) 2737 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; 2738 2739 mmc->caps |= MMC_CAP_CMD23; 2740 if (host->cqhci) 2741 mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD; 2742 /* MMC core transfer sizes tunable parameters */ 2743 mmc->max_segs = MAX_BD_NUM; 2744 if (host->dev_comp->support_64g) 2745 mmc->max_seg_size = BDMA_DESC_BUFLEN_EXT; 2746 else 2747 mmc->max_seg_size = BDMA_DESC_BUFLEN; 2748 mmc->max_blk_size = 2048; 2749 mmc->max_req_size = 512 * 1024; 2750 mmc->max_blk_count = mmc->max_req_size / 512; 2751 if (host->dev_comp->support_64g) 2752 host->dma_mask = DMA_BIT_MASK(36); 2753 else 2754 host->dma_mask = DMA_BIT_MASK(32); 2755 mmc_dev(mmc)->dma_mask = &host->dma_mask; 2756 2757 host->timeout_clks = 3 * 1048576; 2758 host->dma.gpd = dma_alloc_coherent(&pdev->dev, 2759 2 * sizeof(struct mt_gpdma_desc), 2760 &host->dma.gpd_addr, GFP_KERNEL); 2761 host->dma.bd = dma_alloc_coherent(&pdev->dev, 2762 MAX_BD_NUM * sizeof(struct mt_bdma_desc), 2763 &host->dma.bd_addr, GFP_KERNEL); 2764 if (!host->dma.gpd || !host->dma.bd) { 2765 ret = -ENOMEM; 2766 goto release_mem; 2767 } 2768 msdc_init_gpd_bd(host, &host->dma); 2769 INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout); 2770 spin_lock_init(&host->lock); 2771 2772 platform_set_drvdata(pdev, mmc); 2773 ret = msdc_ungate_clock(host); 2774 if (ret) { 2775 dev_err(&pdev->dev, "Cannot ungate clocks!\n"); 2776 goto release_mem; 2777 } 2778 msdc_init_hw(host); 2779 2780 if (mmc->caps2 & MMC_CAP2_CQE) { 2781 host->cq_host = devm_kzalloc(mmc->parent, 2782 sizeof(*host->cq_host), 2783 GFP_KERNEL); 2784 if (!host->cq_host) { 2785 ret = -ENOMEM; 2786 goto host_free; 2787 } 2788 host->cq_host->caps |= CQHCI_TASK_DESC_SZ_128; 2789 host->cq_host->mmio = host->base + 0x800; 2790 host->cq_host->ops = &msdc_cmdq_ops; 2791 ret = cqhci_init(host->cq_host, mmc, true); 2792 if (ret) 2793 goto host_free; 2794 mmc->max_segs = 128; 2795 /* cqhci 16bit length */ 2796 /* 0 size, means 65536 so we don't have to -1 here */ 2797 mmc->max_seg_size = 64 * 1024; 2798 } 2799 2800 ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, 2801 IRQF_TRIGGER_NONE, pdev->name, host); 2802 if (ret) 2803 goto release; 2804 2805 pm_runtime_set_active(host->dev); 2806 pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY); 2807 pm_runtime_use_autosuspend(host->dev); 2808 pm_runtime_enable(host->dev); 2809 ret = mmc_add_host(mmc); 2810 2811 if (ret) 2812 goto end; 2813 2814 return 0; 2815 end: 2816 pm_runtime_disable(host->dev); 2817 release: 2818 platform_set_drvdata(pdev, NULL); 2819 msdc_deinit_hw(host); 2820 msdc_gate_clock(host); 2821 release_mem: 2822 if (host->dma.gpd) 2823 dma_free_coherent(&pdev->dev, 2824 2 * sizeof(struct mt_gpdma_desc), 2825 host->dma.gpd, host->dma.gpd_addr); 2826 if (host->dma.bd) 2827 dma_free_coherent(&pdev->dev, 2828 MAX_BD_NUM * sizeof(struct mt_bdma_desc), 2829 host->dma.bd, host->dma.bd_addr); 2830 host_free: 2831 mmc_free_host(mmc); 2832 2833 return ret; 2834 } 2835 2836 static int msdc_drv_remove(struct platform_device *pdev) 2837 { 2838 struct mmc_host *mmc; 2839 struct msdc_host *host; 2840 2841 mmc = platform_get_drvdata(pdev); 2842 host = mmc_priv(mmc); 2843 2844 pm_runtime_get_sync(host->dev); 2845 2846 platform_set_drvdata(pdev, NULL); 2847 mmc_remove_host(mmc); 2848 msdc_deinit_hw(host); 2849 msdc_gate_clock(host); 2850 2851 pm_runtime_disable(host->dev); 2852 pm_runtime_put_noidle(host->dev); 2853 dma_free_coherent(&pdev->dev, 2854 2 * sizeof(struct mt_gpdma_desc), 2855 host->dma.gpd, host->dma.gpd_addr); 2856 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc), 2857 host->dma.bd, host->dma.bd_addr); 2858 2859 mmc_free_host(mmc); 2860 2861 return 0; 2862 } 2863 2864 static void msdc_save_reg(struct msdc_host *host) 2865 { 2866 u32 tune_reg = host->dev_comp->pad_tune_reg; 2867 2868 host->save_para.msdc_cfg = readl(host->base + MSDC_CFG); 2869 host->save_para.iocon = readl(host->base + MSDC_IOCON); 2870 host->save_para.sdc_cfg = readl(host->base + SDC_CFG); 2871 host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT); 2872 host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1); 2873 host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2); 2874 host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE); 2875 host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); 2876 host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0); 2877 host->save_para.emmc50_cfg3 = readl(host->base + EMMC50_CFG3); 2878 host->save_para.sdc_fifo_cfg = readl(host->base + SDC_FIFO_CFG); 2879 if (host->top_base) { 2880 host->save_para.emmc_top_control = 2881 readl(host->top_base + EMMC_TOP_CONTROL); 2882 host->save_para.emmc_top_cmd = 2883 readl(host->top_base + EMMC_TOP_CMD); 2884 host->save_para.emmc50_pad_ds_tune = 2885 readl(host->top_base + EMMC50_PAD_DS_TUNE); 2886 } else { 2887 host->save_para.pad_tune = readl(host->base + tune_reg); 2888 } 2889 } 2890 2891 static void msdc_restore_reg(struct msdc_host *host) 2892 { 2893 struct mmc_host *mmc = mmc_from_priv(host); 2894 u32 tune_reg = host->dev_comp->pad_tune_reg; 2895 2896 writel(host->save_para.msdc_cfg, host->base + MSDC_CFG); 2897 writel(host->save_para.iocon, host->base + MSDC_IOCON); 2898 writel(host->save_para.sdc_cfg, host->base + SDC_CFG); 2899 writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT); 2900 writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1); 2901 writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2); 2902 writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE); 2903 writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE); 2904 writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0); 2905 writel(host->save_para.emmc50_cfg3, host->base + EMMC50_CFG3); 2906 writel(host->save_para.sdc_fifo_cfg, host->base + SDC_FIFO_CFG); 2907 if (host->top_base) { 2908 writel(host->save_para.emmc_top_control, 2909 host->top_base + EMMC_TOP_CONTROL); 2910 writel(host->save_para.emmc_top_cmd, 2911 host->top_base + EMMC_TOP_CMD); 2912 writel(host->save_para.emmc50_pad_ds_tune, 2913 host->top_base + EMMC50_PAD_DS_TUNE); 2914 } else { 2915 writel(host->save_para.pad_tune, host->base + tune_reg); 2916 } 2917 2918 if (sdio_irq_claimed(mmc)) 2919 __msdc_enable_sdio_irq(host, 1); 2920 } 2921 2922 static int __maybe_unused msdc_runtime_suspend(struct device *dev) 2923 { 2924 struct mmc_host *mmc = dev_get_drvdata(dev); 2925 struct msdc_host *host = mmc_priv(mmc); 2926 2927 msdc_save_reg(host); 2928 2929 if (sdio_irq_claimed(mmc)) { 2930 if (host->pins_eint) { 2931 disable_irq(host->irq); 2932 pinctrl_select_state(host->pinctrl, host->pins_eint); 2933 } 2934 2935 __msdc_enable_sdio_irq(host, 0); 2936 } 2937 msdc_gate_clock(host); 2938 return 0; 2939 } 2940 2941 static int __maybe_unused msdc_runtime_resume(struct device *dev) 2942 { 2943 struct mmc_host *mmc = dev_get_drvdata(dev); 2944 struct msdc_host *host = mmc_priv(mmc); 2945 int ret; 2946 2947 ret = msdc_ungate_clock(host); 2948 if (ret) 2949 return ret; 2950 2951 msdc_restore_reg(host); 2952 2953 if (sdio_irq_claimed(mmc) && host->pins_eint) { 2954 pinctrl_select_state(host->pinctrl, host->pins_uhs); 2955 enable_irq(host->irq); 2956 } 2957 return 0; 2958 } 2959 2960 static int __maybe_unused msdc_suspend(struct device *dev) 2961 { 2962 struct mmc_host *mmc = dev_get_drvdata(dev); 2963 struct msdc_host *host = mmc_priv(mmc); 2964 int ret; 2965 u32 val; 2966 2967 if (mmc->caps2 & MMC_CAP2_CQE) { 2968 ret = cqhci_suspend(mmc); 2969 if (ret) 2970 return ret; 2971 val = readl(host->base + MSDC_INT); 2972 writel(val, host->base + MSDC_INT); 2973 } 2974 2975 /* 2976 * Bump up runtime PM usage counter otherwise dev->power.needs_force_resume will 2977 * not be marked as 1, pm_runtime_force_resume() will go out directly. 2978 */ 2979 if (sdio_irq_claimed(mmc) && host->pins_eint) 2980 pm_runtime_get_noresume(dev); 2981 2982 return pm_runtime_force_suspend(dev); 2983 } 2984 2985 static int __maybe_unused msdc_resume(struct device *dev) 2986 { 2987 struct mmc_host *mmc = dev_get_drvdata(dev); 2988 struct msdc_host *host = mmc_priv(mmc); 2989 2990 if (sdio_irq_claimed(mmc) && host->pins_eint) 2991 pm_runtime_put_noidle(dev); 2992 2993 return pm_runtime_force_resume(dev); 2994 } 2995 2996 static const struct dev_pm_ops msdc_dev_pm_ops = { 2997 SET_SYSTEM_SLEEP_PM_OPS(msdc_suspend, msdc_resume) 2998 SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL) 2999 }; 3000 3001 static struct platform_driver mt_msdc_driver = { 3002 .probe = msdc_drv_probe, 3003 .remove = msdc_drv_remove, 3004 .driver = { 3005 .name = "mtk-msdc", 3006 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 3007 .of_match_table = msdc_of_ids, 3008 .pm = &msdc_dev_pm_ops, 3009 }, 3010 }; 3011 3012 module_platform_driver(mt_msdc_driver); 3013 MODULE_LICENSE("GPL v2"); 3014 MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver"); 3015