1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2015, 2022 MediaTek Inc.
4 * Author: Chaotian.Jing <chaotian.jing@mediatek.com>
7 #include <linux/module.h>
8 #include <linux/bitops.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>
16 #include <linux/of_gpio.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/pm_wakeirq.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/reset.h>
28 #include <linux/mmc/card.h>
29 #include <linux/mmc/core.h>
30 #include <linux/mmc/host.h>
31 #include <linux/mmc/mmc.h>
32 #include <linux/mmc/sd.h>
33 #include <linux/mmc/sdio.h>
34 #include <linux/mmc/slot-gpio.h>
38 #define MAX_BD_NUM 1024
39 #define MSDC_NR_CLOCKS 3
41 /*--------------------------------------------------------------------------*/
42 /* Common Definition */
43 /*--------------------------------------------------------------------------*/
44 #define MSDC_BUS_1BITS 0x0
45 #define MSDC_BUS_4BITS 0x1
46 #define MSDC_BUS_8BITS 0x2
48 #define MSDC_BURST_64B 0x6
50 /*--------------------------------------------------------------------------*/
52 /*--------------------------------------------------------------------------*/
54 #define MSDC_IOCON 0x04
57 #define MSDC_INTEN 0x10
58 #define MSDC_FIFOCS 0x14
63 #define SDC_RESP0 0x40
64 #define SDC_RESP1 0x44
65 #define SDC_RESP2 0x48
66 #define SDC_RESP3 0x4c
67 #define SDC_BLK_NUM 0x50
68 #define SDC_ADV_CFG0 0x64
69 #define EMMC_IOCON 0x7c
70 #define SDC_ACMD_RESP 0x80
71 #define DMA_SA_H4BIT 0x8c
72 #define MSDC_DMA_SA 0x90
73 #define MSDC_DMA_CTRL 0x98
74 #define MSDC_DMA_CFG 0x9c
75 #define MSDC_PATCH_BIT 0xb0
76 #define MSDC_PATCH_BIT1 0xb4
77 #define MSDC_PATCH_BIT2 0xb8
78 #define MSDC_PAD_TUNE 0xec
79 #define MSDC_PAD_TUNE0 0xf0
80 #define PAD_DS_TUNE 0x188
81 #define PAD_CMD_TUNE 0x18c
82 #define EMMC51_CFG0 0x204
83 #define EMMC50_CFG0 0x208
84 #define EMMC50_CFG1 0x20c
85 #define EMMC50_CFG3 0x220
86 #define SDC_FIFO_CFG 0x228
87 #define CQHCI_SETTING 0x7fc
89 /*--------------------------------------------------------------------------*/
90 /* Top Pad Register Offset */
91 /*--------------------------------------------------------------------------*/
92 #define EMMC_TOP_CONTROL 0x00
93 #define EMMC_TOP_CMD 0x04
94 #define EMMC50_PAD_DS_TUNE 0x0c
96 /*--------------------------------------------------------------------------*/
98 /*--------------------------------------------------------------------------*/
101 #define MSDC_CFG_MODE BIT(0) /* RW */
102 #define MSDC_CFG_CKPDN BIT(1) /* RW */
103 #define MSDC_CFG_RST BIT(2) /* RW */
104 #define MSDC_CFG_PIO BIT(3) /* RW */
105 #define MSDC_CFG_CKDRVEN BIT(4) /* RW */
106 #define MSDC_CFG_BV18SDT BIT(5) /* RW */
107 #define MSDC_CFG_BV18PSS BIT(6) /* R */
108 #define MSDC_CFG_CKSTB BIT(7) /* R */
109 #define MSDC_CFG_CKDIV GENMASK(15, 8) /* RW */
110 #define MSDC_CFG_CKMOD GENMASK(17, 16) /* RW */
111 #define MSDC_CFG_HS400_CK_MODE BIT(18) /* RW */
112 #define MSDC_CFG_HS400_CK_MODE_EXTRA BIT(22) /* RW */
113 #define MSDC_CFG_CKDIV_EXTRA GENMASK(19, 8) /* RW */
114 #define MSDC_CFG_CKMOD_EXTRA GENMASK(21, 20) /* RW */
116 /* MSDC_IOCON mask */
117 #define MSDC_IOCON_SDR104CKS BIT(0) /* RW */
118 #define MSDC_IOCON_RSPL BIT(1) /* RW */
119 #define MSDC_IOCON_DSPL BIT(2) /* RW */
120 #define MSDC_IOCON_DDLSEL BIT(3) /* RW */
121 #define MSDC_IOCON_DDR50CKD BIT(4) /* RW */
122 #define MSDC_IOCON_DSPLSEL BIT(5) /* RW */
123 #define MSDC_IOCON_W_DSPL BIT(8) /* RW */
124 #define MSDC_IOCON_D0SPL BIT(16) /* RW */
125 #define MSDC_IOCON_D1SPL BIT(17) /* RW */
126 #define MSDC_IOCON_D2SPL BIT(18) /* RW */
127 #define MSDC_IOCON_D3SPL BIT(19) /* RW */
128 #define MSDC_IOCON_D4SPL BIT(20) /* RW */
129 #define MSDC_IOCON_D5SPL BIT(21) /* RW */
130 #define MSDC_IOCON_D6SPL BIT(22) /* RW */
131 #define MSDC_IOCON_D7SPL BIT(23) /* RW */
132 #define MSDC_IOCON_RISCSZ GENMASK(25, 24) /* RW */
135 #define MSDC_PS_CDEN BIT(0) /* RW */
136 #define MSDC_PS_CDSTS BIT(1) /* R */
137 #define MSDC_PS_CDDEBOUNCE GENMASK(15, 12) /* RW */
138 #define MSDC_PS_DAT GENMASK(23, 16) /* R */
139 #define MSDC_PS_DATA1 BIT(17) /* R */
140 #define MSDC_PS_CMD BIT(24) /* R */
141 #define MSDC_PS_WP BIT(31) /* R */
144 #define MSDC_INT_MMCIRQ BIT(0) /* W1C */
145 #define MSDC_INT_CDSC BIT(1) /* W1C */
146 #define MSDC_INT_ACMDRDY BIT(3) /* W1C */
147 #define MSDC_INT_ACMDTMO BIT(4) /* W1C */
148 #define MSDC_INT_ACMDCRCERR BIT(5) /* W1C */
149 #define MSDC_INT_DMAQ_EMPTY BIT(6) /* W1C */
150 #define MSDC_INT_SDIOIRQ BIT(7) /* W1C */
151 #define MSDC_INT_CMDRDY BIT(8) /* W1C */
152 #define MSDC_INT_CMDTMO BIT(9) /* W1C */
153 #define MSDC_INT_RSPCRCERR BIT(10) /* W1C */
154 #define MSDC_INT_CSTA BIT(11) /* R */
155 #define MSDC_INT_XFER_COMPL BIT(12) /* W1C */
156 #define MSDC_INT_DXFER_DONE BIT(13) /* W1C */
157 #define MSDC_INT_DATTMO BIT(14) /* W1C */
158 #define MSDC_INT_DATCRCERR BIT(15) /* W1C */
159 #define MSDC_INT_ACMD19_DONE BIT(16) /* W1C */
160 #define MSDC_INT_DMA_BDCSERR BIT(17) /* W1C */
161 #define MSDC_INT_DMA_GPDCSERR BIT(18) /* W1C */
162 #define MSDC_INT_DMA_PROTECT BIT(19) /* W1C */
163 #define MSDC_INT_CMDQ BIT(28) /* W1C */
165 /* MSDC_INTEN mask */
166 #define MSDC_INTEN_MMCIRQ BIT(0) /* RW */
167 #define MSDC_INTEN_CDSC BIT(1) /* RW */
168 #define MSDC_INTEN_ACMDRDY BIT(3) /* RW */
169 #define MSDC_INTEN_ACMDTMO BIT(4) /* RW */
170 #define MSDC_INTEN_ACMDCRCERR BIT(5) /* RW */
171 #define MSDC_INTEN_DMAQ_EMPTY BIT(6) /* RW */
172 #define MSDC_INTEN_SDIOIRQ BIT(7) /* RW */
173 #define MSDC_INTEN_CMDRDY BIT(8) /* RW */
174 #define MSDC_INTEN_CMDTMO BIT(9) /* RW */
175 #define MSDC_INTEN_RSPCRCERR BIT(10) /* RW */
176 #define MSDC_INTEN_CSTA BIT(11) /* RW */
177 #define MSDC_INTEN_XFER_COMPL BIT(12) /* RW */
178 #define MSDC_INTEN_DXFER_DONE BIT(13) /* RW */
179 #define MSDC_INTEN_DATTMO BIT(14) /* RW */
180 #define MSDC_INTEN_DATCRCERR BIT(15) /* RW */
181 #define MSDC_INTEN_ACMD19_DONE BIT(16) /* RW */
182 #define MSDC_INTEN_DMA_BDCSERR BIT(17) /* RW */
183 #define MSDC_INTEN_DMA_GPDCSERR BIT(18) /* RW */
184 #define MSDC_INTEN_DMA_PROTECT BIT(19) /* RW */
186 /* MSDC_FIFOCS mask */
187 #define MSDC_FIFOCS_RXCNT GENMASK(7, 0) /* R */
188 #define MSDC_FIFOCS_TXCNT GENMASK(23, 16) /* R */
189 #define MSDC_FIFOCS_CLR BIT(31) /* RW */
192 #define SDC_CFG_SDIOINTWKUP BIT(0) /* RW */
193 #define SDC_CFG_INSWKUP BIT(1) /* RW */
194 #define SDC_CFG_WRDTOC GENMASK(14, 2) /* RW */
195 #define SDC_CFG_BUSWIDTH GENMASK(17, 16) /* RW */
196 #define SDC_CFG_SDIO BIT(19) /* RW */
197 #define SDC_CFG_SDIOIDE BIT(20) /* RW */
198 #define SDC_CFG_INTATGAP BIT(21) /* RW */
199 #define SDC_CFG_DTOC GENMASK(31, 24) /* RW */
202 #define SDC_STS_SDCBUSY BIT(0) /* RW */
203 #define SDC_STS_CMDBUSY BIT(1) /* RW */
204 #define SDC_STS_SWR_COMPL BIT(31) /* RW */
206 #define SDC_DAT1_IRQ_TRIGGER BIT(19) /* RW */
207 /* SDC_ADV_CFG0 mask */
208 #define SDC_RX_ENHANCE_EN BIT(20) /* RW */
210 /* DMA_SA_H4BIT mask */
211 #define DMA_ADDR_HIGH_4BIT GENMASK(3, 0) /* RW */
213 /* MSDC_DMA_CTRL mask */
214 #define MSDC_DMA_CTRL_START BIT(0) /* W */
215 #define MSDC_DMA_CTRL_STOP BIT(1) /* W */
216 #define MSDC_DMA_CTRL_RESUME BIT(2) /* W */
217 #define MSDC_DMA_CTRL_MODE BIT(8) /* RW */
218 #define MSDC_DMA_CTRL_LASTBUF BIT(10) /* RW */
219 #define MSDC_DMA_CTRL_BRUSTSZ GENMASK(14, 12) /* RW */
221 /* MSDC_DMA_CFG mask */
222 #define MSDC_DMA_CFG_STS BIT(0) /* R */
223 #define MSDC_DMA_CFG_DECSEN BIT(1) /* RW */
224 #define MSDC_DMA_CFG_AHBHPROT2 BIT(9) /* RW */
225 #define MSDC_DMA_CFG_ACTIVEEN BIT(13) /* RW */
226 #define MSDC_DMA_CFG_CS12B16B BIT(16) /* RW */
228 /* MSDC_PATCH_BIT mask */
229 #define MSDC_PATCH_BIT_ODDSUPP BIT(1) /* RW */
230 #define MSDC_INT_DAT_LATCH_CK_SEL GENMASK(9, 7)
231 #define MSDC_CKGEN_MSDC_DLY_SEL GENMASK(14, 10)
232 #define MSDC_PATCH_BIT_IODSSEL BIT(16) /* RW */
233 #define MSDC_PATCH_BIT_IOINTSEL BIT(17) /* RW */
234 #define MSDC_PATCH_BIT_BUSYDLY GENMASK(21, 18) /* RW */
235 #define MSDC_PATCH_BIT_WDOD GENMASK(25, 22) /* RW */
236 #define MSDC_PATCH_BIT_IDRTSEL BIT(26) /* RW */
237 #define MSDC_PATCH_BIT_CMDFSEL BIT(27) /* RW */
238 #define MSDC_PATCH_BIT_INTDLSEL BIT(28) /* RW */
239 #define MSDC_PATCH_BIT_SPCPUSH BIT(29) /* RW */
240 #define MSDC_PATCH_BIT_DECRCTMO BIT(30) /* RW */
242 #define MSDC_PATCH_BIT1_CMDTA GENMASK(5, 3) /* RW */
243 #define MSDC_PB1_BUSY_CHECK_SEL BIT(7) /* RW */
244 #define MSDC_PATCH_BIT1_STOP_DLY GENMASK(11, 8) /* RW */
246 #define MSDC_PATCH_BIT2_CFGRESP BIT(15) /* RW */
247 #define MSDC_PATCH_BIT2_CFGCRCSTS BIT(28) /* RW */
248 #define MSDC_PB2_SUPPORT_64G BIT(1) /* RW */
249 #define MSDC_PB2_RESPWAIT GENMASK(3, 2) /* RW */
250 #define MSDC_PB2_RESPSTSENSEL GENMASK(18, 16) /* RW */
251 #define MSDC_PB2_CRCSTSENSEL GENMASK(31, 29) /* RW */
253 #define MSDC_PAD_TUNE_DATWRDLY GENMASK(4, 0) /* RW */
254 #define MSDC_PAD_TUNE_DATRRDLY GENMASK(12, 8) /* RW */
255 #define MSDC_PAD_TUNE_CMDRDLY GENMASK(20, 16) /* RW */
256 #define MSDC_PAD_TUNE_CMDRRDLY GENMASK(26, 22) /* RW */
257 #define MSDC_PAD_TUNE_CLKTDLY GENMASK(31, 27) /* RW */
258 #define MSDC_PAD_TUNE_RXDLYSEL BIT(15) /* RW */
259 #define MSDC_PAD_TUNE_RD_SEL BIT(13) /* RW */
260 #define MSDC_PAD_TUNE_CMD_SEL BIT(21) /* RW */
262 #define PAD_DS_TUNE_DLY_SEL BIT(0) /* RW */
263 #define PAD_DS_TUNE_DLY1 GENMASK(6, 2) /* RW */
264 #define PAD_DS_TUNE_DLY2 GENMASK(11, 7) /* RW */
265 #define PAD_DS_TUNE_DLY3 GENMASK(16, 12) /* RW */
267 #define PAD_CMD_TUNE_RX_DLY3 GENMASK(5, 1) /* RW */
269 /* EMMC51_CFG0 mask */
270 #define CMDQ_RDAT_CNT GENMASK(21, 12) /* RW */
272 #define EMMC50_CFG_PADCMD_LATCHCK BIT(0) /* RW */
273 #define EMMC50_CFG_CRCSTS_EDGE BIT(3) /* RW */
274 #define EMMC50_CFG_CFCSTS_SEL BIT(4) /* RW */
275 #define EMMC50_CFG_CMD_RESP_SEL BIT(9) /* RW */
277 /* EMMC50_CFG1 mask */
278 #define EMMC50_CFG1_DS_CFG BIT(28) /* RW */
280 #define EMMC50_CFG3_OUTS_WR GENMASK(4, 0) /* RW */
282 #define SDC_FIFO_CFG_WRVALIDSEL BIT(24) /* RW */
283 #define SDC_FIFO_CFG_RDVALIDSEL BIT(25) /* RW */
286 #define CQHCI_RD_CMD_WND_SEL BIT(14) /* RW */
287 #define CQHCI_WR_CMD_WND_SEL BIT(15) /* RW */
289 /* EMMC_TOP_CONTROL mask */
290 #define PAD_RXDLY_SEL BIT(0) /* RW */
291 #define DELAY_EN BIT(1) /* RW */
292 #define PAD_DAT_RD_RXDLY2 GENMASK(6, 2) /* RW */
293 #define PAD_DAT_RD_RXDLY GENMASK(11, 7) /* RW */
294 #define PAD_DAT_RD_RXDLY2_SEL BIT(12) /* RW */
295 #define PAD_DAT_RD_RXDLY_SEL BIT(13) /* RW */
296 #define DATA_K_VALUE_SEL BIT(14) /* RW */
297 #define SDC_RX_ENH_EN BIT(15) /* TW */
299 /* EMMC_TOP_CMD mask */
300 #define PAD_CMD_RXDLY2 GENMASK(4, 0) /* RW */
301 #define PAD_CMD_RXDLY GENMASK(9, 5) /* RW */
302 #define PAD_CMD_RD_RXDLY2_SEL BIT(10) /* RW */
303 #define PAD_CMD_RD_RXDLY_SEL BIT(11) /* RW */
304 #define PAD_CMD_TX_DLY GENMASK(16, 12) /* RW */
306 /* EMMC50_PAD_DS_TUNE mask */
307 #define PAD_DS_DLY_SEL BIT(16) /* RW */
308 #define PAD_DS_DLY1 GENMASK(14, 10) /* RW */
309 #define PAD_DS_DLY3 GENMASK(4, 0) /* RW */
311 #define REQ_CMD_EIO BIT(0)
312 #define REQ_CMD_TMO BIT(1)
313 #define REQ_DAT_ERR BIT(2)
314 #define REQ_STOP_EIO BIT(3)
315 #define REQ_STOP_TMO BIT(4)
316 #define REQ_CMD_BUSY BIT(5)
318 #define MSDC_PREPARE_FLAG BIT(0)
319 #define MSDC_ASYNC_FLAG BIT(1)
320 #define MSDC_MMAP_FLAG BIT(2)
322 #define MTK_MMC_AUTOSUSPEND_DELAY 50
323 #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
324 #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */
326 #define DEFAULT_DEBOUNCE (8) /* 8 cycles CD debounce */
328 #define PAD_DELAY_MAX 32 /* PAD delay cells */
329 /*--------------------------------------------------------------------------*/
330 /* Descriptor Structure */
331 /*--------------------------------------------------------------------------*/
332 struct mt_gpdma_desc {
334 #define GPDMA_DESC_HWO BIT(0)
335 #define GPDMA_DESC_BDP BIT(1)
336 #define GPDMA_DESC_CHECKSUM GENMASK(15, 8)
337 #define GPDMA_DESC_INT BIT(16)
338 #define GPDMA_DESC_NEXT_H4 GENMASK(27, 24)
339 #define GPDMA_DESC_PTR_H4 GENMASK(31, 28)
343 #define GPDMA_DESC_BUFLEN GENMASK(15, 0)
344 #define GPDMA_DESC_EXTLEN GENMASK(23, 16)
350 struct mt_bdma_desc {
352 #define BDMA_DESC_EOL BIT(0)
353 #define BDMA_DESC_CHECKSUM GENMASK(15, 8)
354 #define BDMA_DESC_BLKPAD BIT(17)
355 #define BDMA_DESC_DWPAD BIT(18)
356 #define BDMA_DESC_NEXT_H4 GENMASK(27, 24)
357 #define BDMA_DESC_PTR_H4 GENMASK(31, 28)
361 #define BDMA_DESC_BUFLEN GENMASK(15, 0)
362 #define BDMA_DESC_BUFLEN_EXT GENMASK(23, 0)
366 struct scatterlist *sg; /* I/O scatter list */
367 struct mt_gpdma_desc *gpd; /* pointer to gpd array */
368 struct mt_bdma_desc *bd; /* pointer to bd array */
369 dma_addr_t gpd_addr; /* the physical address of gpd array */
370 dma_addr_t bd_addr; /* the physical address of bd array */
373 struct msdc_save_para {
386 u32 emmc_top_control;
388 u32 emmc50_pad_ds_tune;
391 struct mtk_mmc_compatible {
393 bool recheck_sdio_irq;
394 bool hs400_tune; /* only used for MT8173 */
402 bool use_internal_cd;
405 struct msdc_tune_para {
409 u32 emmc_top_control;
413 struct msdc_delay_phase {
421 const struct mtk_mmc_compatible *dev_comp;
425 struct mmc_request *mrq;
426 struct mmc_command *cmd;
427 struct mmc_data *data;
430 void __iomem *base; /* host base address */
431 void __iomem *top_base; /* host top register base address */
433 struct msdc_dma dma; /* dma channel */
436 u32 timeout_ns; /* data timeout ns */
437 u32 timeout_clks; /* data timeout clks */
439 struct pinctrl *pinctrl;
440 struct pinctrl_state *pins_default;
441 struct pinctrl_state *pins_uhs;
442 struct pinctrl_state *pins_eint;
443 struct delayed_work req_timeout;
444 int irq; /* host interrupt */
445 int eint_irq; /* interrupt from sdio device for waking up system */
446 struct reset_control *reset;
448 struct clk *src_clk; /* msdc source clock */
449 struct clk *h_clk; /* msdc h_clk */
450 struct clk *bus_clk; /* bus clock which used to access register */
451 struct clk *src_clk_cg; /* msdc source clock control gate */
452 struct clk *sys_clk_cg; /* msdc subsys clock control gate */
453 struct clk *crypto_clk; /* msdc crypto clock control gate */
454 struct clk_bulk_data bulk_clks[MSDC_NR_CLOCKS];
455 u32 mclk; /* mmc subsystem clock frequency */
456 u32 src_clk_freq; /* source clock frequency */
457 unsigned char timing;
462 u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */
463 u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */
464 bool hs400_cmd_resp_sel_rising;
465 /* cmd response sample selection for HS400 */
466 bool hs400_mode; /* current eMMC will run at hs400 mode */
467 bool hs400_tuning; /* hs400 mode online tuning */
468 bool internal_cd; /* Use internal card-detect logic */
469 bool cqhci; /* support eMMC hw cmdq */
470 struct msdc_save_para save_para; /* used when gate HCLK */
471 struct msdc_tune_para def_tune_para; /* default tune setting */
472 struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */
473 struct cqhci_host *cq_host;
477 static const struct mtk_mmc_compatible mt2701_compat = {
479 .recheck_sdio_irq = true,
481 .pad_tune_reg = MSDC_PAD_TUNE0,
485 .stop_clk_fix = false,
487 .support_64g = false,
490 static const struct mtk_mmc_compatible mt2712_compat = {
492 .recheck_sdio_irq = false,
494 .pad_tune_reg = MSDC_PAD_TUNE0,
498 .stop_clk_fix = true,
503 static const struct mtk_mmc_compatible mt6779_compat = {
505 .recheck_sdio_irq = false,
507 .pad_tune_reg = MSDC_PAD_TUNE0,
511 .stop_clk_fix = true,
516 static const struct mtk_mmc_compatible mt6795_compat = {
518 .recheck_sdio_irq = false,
520 .pad_tune_reg = MSDC_PAD_TUNE,
524 .stop_clk_fix = false,
526 .support_64g = false,
529 static const struct mtk_mmc_compatible mt7620_compat = {
531 .recheck_sdio_irq = true,
533 .pad_tune_reg = MSDC_PAD_TUNE,
537 .stop_clk_fix = false,
539 .use_internal_cd = true,
542 static const struct mtk_mmc_compatible mt7622_compat = {
544 .recheck_sdio_irq = true,
546 .pad_tune_reg = MSDC_PAD_TUNE0,
550 .stop_clk_fix = true,
552 .support_64g = false,
555 static const struct mtk_mmc_compatible mt7986_compat = {
557 .recheck_sdio_irq = true,
559 .pad_tune_reg = MSDC_PAD_TUNE0,
563 .stop_clk_fix = true,
568 static const struct mtk_mmc_compatible mt8135_compat = {
570 .recheck_sdio_irq = true,
572 .pad_tune_reg = MSDC_PAD_TUNE,
576 .stop_clk_fix = false,
578 .support_64g = false,
581 static const struct mtk_mmc_compatible mt8173_compat = {
583 .recheck_sdio_irq = true,
585 .pad_tune_reg = MSDC_PAD_TUNE,
589 .stop_clk_fix = false,
591 .support_64g = false,
594 static const struct mtk_mmc_compatible mt8183_compat = {
596 .recheck_sdio_irq = false,
598 .pad_tune_reg = MSDC_PAD_TUNE0,
602 .stop_clk_fix = true,
607 static const struct mtk_mmc_compatible mt8516_compat = {
609 .recheck_sdio_irq = true,
611 .pad_tune_reg = MSDC_PAD_TUNE0,
615 .stop_clk_fix = true,
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},
633 MODULE_DEVICE_TABLE(of, msdc_of_ids);
635 static void sdr_set_bits(void __iomem *reg, u32 bs)
637 u32 val = readl(reg);
643 static void sdr_clr_bits(void __iomem *reg, u32 bs)
645 u32 val = readl(reg);
651 static void sdr_set_field(void __iomem *reg, u32 field, u32 val)
653 unsigned int tv = readl(reg);
656 tv |= ((val) << (ffs((unsigned int)field) - 1));
660 static void sdr_get_field(void __iomem *reg, u32 field, u32 *val)
662 unsigned int tv = readl(reg);
664 *val = ((tv & field) >> (ffs((unsigned int)field) - 1));
667 static void msdc_reset_hw(struct msdc_host *host)
671 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST);
672 readl_poll_timeout_atomic(host->base + MSDC_CFG, val, !(val & MSDC_CFG_RST), 0, 0);
674 sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR);
675 readl_poll_timeout_atomic(host->base + MSDC_FIFOCS, val,
676 !(val & MSDC_FIFOCS_CLR), 0, 0);
678 val = readl(host->base + MSDC_INT);
679 writel(val, host->base + MSDC_INT);
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);
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;
693 static u8 msdc_dma_calcs(u8 *buf, u32 len)
697 for (i = 0; i < len; i++)
699 return 0xff - (u8) sum;
702 static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
703 struct mmc_data *data)
705 unsigned int j, dma_len;
706 dma_addr_t dma_address;
708 struct scatterlist *sg;
709 struct mt_gpdma_desc *gpd;
710 struct mt_bdma_desc *bd;
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;
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);
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)
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);
743 bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN;
744 bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN);
747 if (j == data->sg_count - 1) /* the last bd */
748 bd[j].bd_info |= BDMA_DESC_EOL;
750 bd[j].bd_info &= ~BDMA_DESC_EOL;
752 /* checksum 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;
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);
768 static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
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));
777 static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
779 if (data->host_cookie & MSDC_ASYNC_FLAG)
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;
789 static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks)
791 struct mmc_host *mmc = mmc_from_priv(host);
795 if (mmc->actual_clock == 0) {
798 clk_ns = 1000000000ULL;
799 do_div(clk_ns, mmc->actual_clock);
800 timeout = ns + clk_ns - 1;
801 do_div(timeout, clk_ns);
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);
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;
818 /* clock control primitives */
819 static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks)
823 host->timeout_ns = ns;
824 host->timeout_clks = clks;
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));
831 static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
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));
840 static void msdc_gate_clock(struct msdc_host *host)
842 clk_bulk_disable_unprepare(MSDC_NR_CLOCKS, host->bulk_clks);
843 clk_disable_unprepare(host->crypto_clk);
844 clk_disable_unprepare(host->src_clk_cg);
845 clk_disable_unprepare(host->src_clk);
846 clk_disable_unprepare(host->bus_clk);
847 clk_disable_unprepare(host->h_clk);
850 static int msdc_ungate_clock(struct msdc_host *host)
855 clk_prepare_enable(host->h_clk);
856 clk_prepare_enable(host->bus_clk);
857 clk_prepare_enable(host->src_clk);
858 clk_prepare_enable(host->src_clk_cg);
859 clk_prepare_enable(host->crypto_clk);
860 ret = clk_bulk_prepare_enable(MSDC_NR_CLOCKS, host->bulk_clks);
862 dev_err(host->dev, "Cannot enable pclk/axi/ahb clock gates\n");
866 return readl_poll_timeout(host->base + MSDC_CFG, val,
867 (val & MSDC_CFG_CKSTB), 1, 20000);
870 static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz)
872 struct mmc_host *mmc = mmc_from_priv(host);
877 u32 tune_reg = host->dev_comp->pad_tune_reg;
881 dev_dbg(host->dev, "set mclk to 0\n");
883 mmc->actual_clock = 0;
884 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
888 flags = readl(host->base + MSDC_INTEN);
889 sdr_clr_bits(host->base + MSDC_INTEN, flags);
890 if (host->dev_comp->clk_div_bits == 8)
891 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE);
893 sdr_clr_bits(host->base + MSDC_CFG,
894 MSDC_CFG_HS400_CK_MODE_EXTRA);
895 if (timing == MMC_TIMING_UHS_DDR50 ||
896 timing == MMC_TIMING_MMC_DDR52 ||
897 timing == MMC_TIMING_MMC_HS400) {
898 if (timing == MMC_TIMING_MMC_HS400)
901 mode = 0x2; /* ddr mode and use divisor */
903 if (hz >= (host->src_clk_freq >> 2)) {
904 div = 0; /* mean div = 1/4 */
905 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
907 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
908 sclk = (host->src_clk_freq >> 2) / div;
912 if (timing == MMC_TIMING_MMC_HS400 &&
913 hz >= (host->src_clk_freq >> 1)) {
914 if (host->dev_comp->clk_div_bits == 8)
915 sdr_set_bits(host->base + MSDC_CFG,
916 MSDC_CFG_HS400_CK_MODE);
918 sdr_set_bits(host->base + MSDC_CFG,
919 MSDC_CFG_HS400_CK_MODE_EXTRA);
920 sclk = host->src_clk_freq >> 1;
921 div = 0; /* div is ignore when bit18 is set */
923 } else if (hz >= host->src_clk_freq) {
924 mode = 0x1; /* no divisor */
926 sclk = host->src_clk_freq;
928 mode = 0x0; /* use divisor */
929 if (hz >= (host->src_clk_freq >> 1)) {
930 div = 0; /* mean div = 1/2 */
931 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
933 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
934 sclk = (host->src_clk_freq >> 2) / div;
937 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
939 clk_disable_unprepare(host->src_clk_cg);
940 if (host->dev_comp->clk_div_bits == 8)
941 sdr_set_field(host->base + MSDC_CFG,
942 MSDC_CFG_CKMOD | MSDC_CFG_CKDIV,
945 sdr_set_field(host->base + MSDC_CFG,
946 MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA,
949 clk_prepare_enable(host->src_clk_cg);
950 readl_poll_timeout(host->base + MSDC_CFG, val, (val & MSDC_CFG_CKSTB), 0, 0);
951 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
952 mmc->actual_clock = sclk;
954 host->timing = timing;
955 /* need because clk changed. */
956 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
957 sdr_set_bits(host->base + MSDC_INTEN, flags);
960 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
961 * tune result of hs200/200Mhz is not suitable for 50Mhz
963 if (mmc->actual_clock <= 52000000) {
964 writel(host->def_tune_para.iocon, host->base + MSDC_IOCON);
965 if (host->top_base) {
966 writel(host->def_tune_para.emmc_top_control,
967 host->top_base + EMMC_TOP_CONTROL);
968 writel(host->def_tune_para.emmc_top_cmd,
969 host->top_base + EMMC_TOP_CMD);
971 writel(host->def_tune_para.pad_tune,
972 host->base + tune_reg);
975 writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON);
976 writel(host->saved_tune_para.pad_cmd_tune,
977 host->base + PAD_CMD_TUNE);
978 if (host->top_base) {
979 writel(host->saved_tune_para.emmc_top_control,
980 host->top_base + EMMC_TOP_CONTROL);
981 writel(host->saved_tune_para.emmc_top_cmd,
982 host->top_base + EMMC_TOP_CMD);
984 writel(host->saved_tune_para.pad_tune,
985 host->base + tune_reg);
989 if (timing == MMC_TIMING_MMC_HS400 &&
990 host->dev_comp->hs400_tune)
991 sdr_set_field(host->base + tune_reg,
992 MSDC_PAD_TUNE_CMDRRDLY,
993 host->hs400_cmd_int_delay);
994 dev_dbg(host->dev, "sclk: %d, timing: %d\n", mmc->actual_clock,
998 static inline u32 msdc_cmd_find_resp(struct msdc_host *host,
999 struct mmc_command *cmd)
1003 switch (mmc_resp_type(cmd)) {
1004 /* Actually, R1, R5, R6, R7 are the same */
1026 static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
1027 struct mmc_request *mrq, struct mmc_command *cmd)
1029 struct mmc_host *mmc = mmc_from_priv(host);
1031 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
1032 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
1034 u32 opcode = cmd->opcode;
1035 u32 resp = msdc_cmd_find_resp(host, cmd);
1036 u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7);
1038 host->cmd_rsp = resp;
1040 if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) ||
1041 opcode == MMC_STOP_TRANSMISSION)
1043 else if (opcode == SD_SWITCH_VOLTAGE)
1045 else if (opcode == SD_APP_SEND_SCR ||
1046 opcode == SD_APP_SEND_NUM_WR_BLKS ||
1047 (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
1048 (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
1049 (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC))
1053 struct mmc_data *data = cmd->data;
1055 if (mmc_op_multi(opcode)) {
1056 if (mmc_card_mmc(mmc->card) && mrq->sbc &&
1057 !(mrq->sbc->arg & 0xFFFF0000))
1058 rawcmd |= BIT(29); /* AutoCMD23 */
1061 rawcmd |= ((data->blksz & 0xFFF) << 16);
1062 if (data->flags & MMC_DATA_WRITE)
1064 if (data->blocks > 1)
1068 /* Always use dma mode */
1069 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO);
1071 if (host->timeout_ns != data->timeout_ns ||
1072 host->timeout_clks != data->timeout_clks)
1073 msdc_set_timeout(host, data->timeout_ns,
1074 data->timeout_clks);
1076 writel(data->blocks, host->base + SDC_BLK_NUM);
1081 static void msdc_start_data(struct msdc_host *host, struct mmc_command *cmd,
1082 struct mmc_data *data)
1086 WARN_ON(host->data);
1088 read = data->flags & MMC_DATA_READ;
1090 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
1091 msdc_dma_setup(host, &host->dma, data);
1092 sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask);
1093 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
1094 dev_dbg(host->dev, "DMA start\n");
1095 dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n",
1096 __func__, cmd->opcode, data->blocks, read);
1099 static int msdc_auto_cmd_done(struct msdc_host *host, int events,
1100 struct mmc_command *cmd)
1102 u32 *rsp = cmd->resp;
1104 rsp[0] = readl(host->base + SDC_ACMD_RESP);
1106 if (events & MSDC_INT_ACMDRDY) {
1109 msdc_reset_hw(host);
1110 if (events & MSDC_INT_ACMDCRCERR) {
1111 cmd->error = -EILSEQ;
1112 host->error |= REQ_STOP_EIO;
1113 } else if (events & MSDC_INT_ACMDTMO) {
1114 cmd->error = -ETIMEDOUT;
1115 host->error |= REQ_STOP_TMO;
1118 "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n",
1119 __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error);
1125 * msdc_recheck_sdio_irq - recheck whether the SDIO irq is lost
1127 * Host controller may lost interrupt in some special case.
1128 * Add SDIO irq recheck mechanism to make sure all interrupts
1129 * can be processed immediately
1131 static void msdc_recheck_sdio_irq(struct msdc_host *host)
1133 struct mmc_host *mmc = mmc_from_priv(host);
1134 u32 reg_int, reg_inten, reg_ps;
1136 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1137 reg_inten = readl(host->base + MSDC_INTEN);
1138 if (reg_inten & MSDC_INTEN_SDIOIRQ) {
1139 reg_int = readl(host->base + MSDC_INT);
1140 reg_ps = readl(host->base + MSDC_PS);
1141 if (!(reg_int & MSDC_INT_SDIOIRQ ||
1142 reg_ps & MSDC_PS_DATA1)) {
1143 __msdc_enable_sdio_irq(host, 0);
1144 sdio_signal_irq(mmc);
1150 static void msdc_track_cmd_data(struct msdc_host *host, struct mmc_command *cmd)
1153 dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n",
1154 __func__, cmd->opcode, cmd->arg, host->error);
1157 static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq)
1159 unsigned long flags;
1162 * No need check the return value of cancel_delayed_work, as only ONE
1163 * path will go here!
1165 cancel_delayed_work(&host->req_timeout);
1167 spin_lock_irqsave(&host->lock, flags);
1169 spin_unlock_irqrestore(&host->lock, flags);
1171 msdc_track_cmd_data(host, mrq->cmd);
1173 msdc_unprepare_data(host, mrq->data);
1175 msdc_reset_hw(host);
1176 mmc_request_done(mmc_from_priv(host), mrq);
1177 if (host->dev_comp->recheck_sdio_irq)
1178 msdc_recheck_sdio_irq(host);
1181 /* returns true if command is fully handled; returns false otherwise */
1182 static bool msdc_cmd_done(struct msdc_host *host, int events,
1183 struct mmc_request *mrq, struct mmc_command *cmd)
1187 unsigned long flags;
1190 if (mrq->sbc && cmd == mrq->cmd &&
1191 (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR
1192 | MSDC_INT_ACMDTMO)))
1193 msdc_auto_cmd_done(host, events, mrq->sbc);
1195 sbc_error = mrq->sbc && mrq->sbc->error;
1197 if (!sbc_error && !(events & (MSDC_INT_CMDRDY
1198 | MSDC_INT_RSPCRCERR
1199 | MSDC_INT_CMDTMO)))
1202 spin_lock_irqsave(&host->lock, flags);
1205 spin_unlock_irqrestore(&host->lock, flags);
1211 sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask);
1213 if (cmd->flags & MMC_RSP_PRESENT) {
1214 if (cmd->flags & MMC_RSP_136) {
1215 rsp[0] = readl(host->base + SDC_RESP3);
1216 rsp[1] = readl(host->base + SDC_RESP2);
1217 rsp[2] = readl(host->base + SDC_RESP1);
1218 rsp[3] = readl(host->base + SDC_RESP0);
1220 rsp[0] = readl(host->base + SDC_RESP0);
1224 if (!sbc_error && !(events & MSDC_INT_CMDRDY)) {
1225 if (events & MSDC_INT_CMDTMO ||
1226 (!mmc_op_tuning(cmd->opcode) && !host->hs400_tuning))
1228 * should not clear fifo/interrupt as the tune data
1229 * may have already come when cmd19/cmd21 gets response
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;
1243 "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n",
1244 __func__, cmd->opcode, cmd->arg, rsp[0],
1247 msdc_cmd_next(host, mrq, cmd);
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.
1255 static inline bool msdc_cmd_is_ready(struct msdc_host *host,
1256 struct mmc_request *mrq, struct mmc_command *cmd)
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);
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);
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);
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);
1285 static void msdc_start_command(struct msdc_host *host,
1286 struct mmc_request *mrq, struct mmc_command *cmd)
1289 unsigned long flags;
1294 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
1295 if (!msdc_cmd_is_ready(host, mrq, cmd))
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);
1305 rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd);
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);
1311 writel(cmd->arg, host->base + SDC_ARG);
1312 writel(rawcmd, host->base + SDC_CMD);
1315 static void msdc_cmd_next(struct msdc_host *host,
1316 struct mmc_request *mrq, struct mmc_command *cmd)
1319 !(cmd->error == -EILSEQ &&
1320 (mmc_op_tuning(cmd->opcode) || host->hs400_tuning))) ||
1321 (mrq->sbc && mrq->sbc->error))
1322 msdc_request_done(host, mrq);
1323 else if (cmd == mrq->sbc)
1324 msdc_start_command(host, mrq, mrq->cmd);
1325 else if (!cmd->data)
1326 msdc_request_done(host, mrq);
1328 msdc_start_data(host, cmd, cmd->data);
1331 static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
1333 struct msdc_host *host = mmc_priv(mmc);
1340 msdc_prepare_data(host, mrq->data);
1342 /* if SBC is required, we have HW option and SW option.
1343 * if HW option is enabled, and SBC does not have "special" flags,
1344 * use HW option, otherwise use SW option
1346 if (mrq->sbc && (!mmc_card_mmc(mmc->card) ||
1347 (mrq->sbc->arg & 0xFFFF0000)))
1348 msdc_start_command(host, mrq, mrq->sbc);
1350 msdc_start_command(host, mrq, mrq->cmd);
1353 static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
1355 struct msdc_host *host = mmc_priv(mmc);
1356 struct mmc_data *data = mrq->data;
1361 msdc_prepare_data(host, data);
1362 data->host_cookie |= MSDC_ASYNC_FLAG;
1365 static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1368 struct msdc_host *host = mmc_priv(mmc);
1369 struct mmc_data *data = mrq->data;
1374 if (data->host_cookie) {
1375 data->host_cookie &= ~MSDC_ASYNC_FLAG;
1376 msdc_unprepare_data(host, data);
1380 static void msdc_data_xfer_next(struct msdc_host *host, struct mmc_request *mrq)
1382 if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error &&
1384 msdc_start_command(host, mrq, mrq->stop);
1386 msdc_request_done(host, mrq);
1389 static void msdc_data_xfer_done(struct msdc_host *host, u32 events,
1390 struct mmc_request *mrq, struct mmc_data *data)
1392 struct mmc_command *stop;
1393 unsigned long flags;
1395 unsigned int check_data = events &
1396 (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO
1397 | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR
1398 | MSDC_INT_DMA_PROTECT);
1402 spin_lock_irqsave(&host->lock, flags);
1406 spin_unlock_irqrestore(&host->lock, flags);
1412 if (check_data || (stop && stop->error)) {
1413 dev_dbg(host->dev, "DMA status: 0x%8X\n",
1414 readl(host->base + MSDC_DMA_CFG));
1415 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP,
1418 ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CTRL, val,
1419 !(val & MSDC_DMA_CTRL_STOP), 1, 20000);
1421 dev_dbg(host->dev, "DMA stop timed out\n");
1423 ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CFG, val,
1424 !(val & MSDC_DMA_CFG_STS), 1, 20000);
1426 dev_dbg(host->dev, "DMA inactive timed out\n");
1428 sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask);
1429 dev_dbg(host->dev, "DMA stop\n");
1431 if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) {
1432 data->bytes_xfered = data->blocks * data->blksz;
1434 dev_dbg(host->dev, "interrupt events: %x\n", events);
1435 msdc_reset_hw(host);
1436 host->error |= REQ_DAT_ERR;
1437 data->bytes_xfered = 0;
1439 if (events & MSDC_INT_DATTMO)
1440 data->error = -ETIMEDOUT;
1441 else if (events & MSDC_INT_DATCRCERR)
1442 data->error = -EILSEQ;
1444 dev_dbg(host->dev, "%s: cmd=%d; blocks=%d",
1445 __func__, mrq->cmd->opcode, data->blocks);
1446 dev_dbg(host->dev, "data_error=%d xfer_size=%d\n",
1447 (int)data->error, data->bytes_xfered);
1450 msdc_data_xfer_next(host, mrq);
1454 static void msdc_set_buswidth(struct msdc_host *host, u32 width)
1456 u32 val = readl(host->base + SDC_CFG);
1458 val &= ~SDC_CFG_BUSWIDTH;
1462 case MMC_BUS_WIDTH_1:
1463 val |= (MSDC_BUS_1BITS << 16);
1465 case MMC_BUS_WIDTH_4:
1466 val |= (MSDC_BUS_4BITS << 16);
1468 case MMC_BUS_WIDTH_8:
1469 val |= (MSDC_BUS_8BITS << 16);
1473 writel(val, host->base + SDC_CFG);
1474 dev_dbg(host->dev, "Bus Width = %d", width);
1477 static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios)
1479 struct msdc_host *host = mmc_priv(mmc);
1482 if (!IS_ERR(mmc->supply.vqmmc)) {
1483 if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 &&
1484 ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1485 dev_err(host->dev, "Unsupported signal voltage!\n");
1489 ret = mmc_regulator_set_vqmmc(mmc, ios);
1491 dev_dbg(host->dev, "Regulator set error %d (%d)\n",
1492 ret, ios->signal_voltage);
1496 /* Apply different pinctrl settings for different signal voltage */
1497 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
1498 pinctrl_select_state(host->pinctrl, host->pins_uhs);
1500 pinctrl_select_state(host->pinctrl, host->pins_default);
1505 static int msdc_card_busy(struct mmc_host *mmc)
1507 struct msdc_host *host = mmc_priv(mmc);
1508 u32 status = readl(host->base + MSDC_PS);
1510 /* only check if data0 is low */
1511 return !(status & BIT(16));
1514 static void msdc_request_timeout(struct work_struct *work)
1516 struct msdc_host *host = container_of(work, struct msdc_host,
1519 /* simulate HW timeout status */
1520 dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__);
1522 dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__,
1523 host->mrq, host->mrq->cmd->opcode);
1525 dev_err(host->dev, "%s: aborting cmd=%d\n",
1526 __func__, host->cmd->opcode);
1527 msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq,
1529 } else if (host->data) {
1530 dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n",
1531 __func__, host->mrq->cmd->opcode,
1532 host->data->blocks);
1533 msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq,
1539 static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb)
1542 sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ);
1543 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1544 if (host->dev_comp->recheck_sdio_irq)
1545 msdc_recheck_sdio_irq(host);
1547 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ);
1548 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1552 static void msdc_enable_sdio_irq(struct mmc_host *mmc, int enb)
1554 struct msdc_host *host = mmc_priv(mmc);
1555 unsigned long flags;
1558 spin_lock_irqsave(&host->lock, flags);
1559 __msdc_enable_sdio_irq(host, enb);
1560 spin_unlock_irqrestore(&host->lock, flags);
1562 if (mmc_card_enable_async_irq(mmc->card) && host->pins_eint) {
1565 * In dev_pm_set_dedicated_wake_irq_reverse(), eint pin will be set to
1566 * GPIO mode. We need to restore it to SDIO DAT1 mode after that.
1567 * Since the current pinstate is pins_uhs, to ensure pinctrl select take
1568 * affect successfully, we change the pinstate to pins_eint firstly.
1570 pinctrl_select_state(host->pinctrl, host->pins_eint);
1571 ret = dev_pm_set_dedicated_wake_irq_reverse(host->dev, host->eint_irq);
1574 dev_err(host->dev, "Failed to register SDIO wakeup irq!\n");
1575 host->pins_eint = NULL;
1576 pm_runtime_get_noresume(host->dev);
1578 dev_dbg(host->dev, "SDIO eint irq: %d!\n", host->eint_irq);
1581 pinctrl_select_state(host->pinctrl, host->pins_uhs);
1583 dev_pm_clear_wake_irq(host->dev);
1587 /* Ensure host->pins_eint is NULL */
1588 host->pins_eint = NULL;
1589 pm_runtime_get_noresume(host->dev);
1591 pm_runtime_put_noidle(host->dev);
1596 static irqreturn_t msdc_cmdq_irq(struct msdc_host *host, u32 intsts)
1598 struct mmc_host *mmc = mmc_from_priv(host);
1599 int cmd_err = 0, dat_err = 0;
1601 if (intsts & MSDC_INT_RSPCRCERR) {
1603 dev_err(host->dev, "%s: CMD CRC ERR", __func__);
1604 } else if (intsts & MSDC_INT_CMDTMO) {
1605 cmd_err = -ETIMEDOUT;
1606 dev_err(host->dev, "%s: CMD TIMEOUT ERR", __func__);
1609 if (intsts & MSDC_INT_DATCRCERR) {
1611 dev_err(host->dev, "%s: DATA CRC ERR", __func__);
1612 } else if (intsts & MSDC_INT_DATTMO) {
1613 dat_err = -ETIMEDOUT;
1614 dev_err(host->dev, "%s: DATA TIMEOUT ERR", __func__);
1617 if (cmd_err || dat_err) {
1618 dev_err(host->dev, "cmd_err = %d, dat_err =%d, intsts = 0x%x",
1619 cmd_err, dat_err, intsts);
1622 return cqhci_irq(mmc, 0, cmd_err, dat_err);
1625 static irqreturn_t msdc_irq(int irq, void *dev_id)
1627 struct msdc_host *host = (struct msdc_host *) dev_id;
1628 struct mmc_host *mmc = mmc_from_priv(host);
1631 struct mmc_request *mrq;
1632 struct mmc_command *cmd;
1633 struct mmc_data *data;
1634 u32 events, event_mask;
1636 spin_lock(&host->lock);
1637 events = readl(host->base + MSDC_INT);
1638 event_mask = readl(host->base + MSDC_INTEN);
1639 if ((events & event_mask) & MSDC_INT_SDIOIRQ)
1640 __msdc_enable_sdio_irq(host, 0);
1641 /* clear interrupts */
1642 writel(events & event_mask, host->base + MSDC_INT);
1647 spin_unlock(&host->lock);
1649 if ((events & event_mask) & MSDC_INT_SDIOIRQ)
1650 sdio_signal_irq(mmc);
1652 if ((events & event_mask) & MSDC_INT_CDSC) {
1653 if (host->internal_cd)
1654 mmc_detect_change(mmc, msecs_to_jiffies(20));
1655 events &= ~MSDC_INT_CDSC;
1658 if (!(events & (event_mask & ~MSDC_INT_SDIOIRQ)))
1661 if ((mmc->caps2 & MMC_CAP2_CQE) &&
1662 (events & MSDC_INT_CMDQ)) {
1663 msdc_cmdq_irq(host, events);
1664 /* clear interrupts */
1665 writel(events, host->base + MSDC_INT);
1671 "%s: MRQ=NULL; events=%08X; event_mask=%08X\n",
1672 __func__, events, event_mask);
1677 dev_dbg(host->dev, "%s: events=%08X\n", __func__, events);
1680 msdc_cmd_done(host, events, mrq, cmd);
1682 msdc_data_xfer_done(host, events, mrq, data);
1688 static void msdc_init_hw(struct msdc_host *host)
1691 u32 tune_reg = host->dev_comp->pad_tune_reg;
1692 struct mmc_host *mmc = mmc_from_priv(host);
1695 reset_control_assert(host->reset);
1696 usleep_range(10, 50);
1697 reset_control_deassert(host->reset);
1700 /* Configure to MMC/SD mode, clock free running */
1701 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN);
1704 msdc_reset_hw(host);
1706 /* Disable and clear all interrupts */
1707 writel(0, host->base + MSDC_INTEN);
1708 val = readl(host->base + MSDC_INT);
1709 writel(val, host->base + MSDC_INT);
1711 /* Configure card detection */
1712 if (host->internal_cd) {
1713 sdr_set_field(host->base + MSDC_PS, MSDC_PS_CDDEBOUNCE,
1715 sdr_set_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
1716 sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC);
1717 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP);
1719 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP);
1720 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
1721 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC);
1724 if (host->top_base) {
1725 writel(0, host->top_base + EMMC_TOP_CONTROL);
1726 writel(0, host->top_base + EMMC_TOP_CMD);
1728 writel(0, host->base + tune_reg);
1730 writel(0, host->base + MSDC_IOCON);
1731 sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0);
1732 writel(0x403c0046, host->base + MSDC_PATCH_BIT);
1733 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1);
1734 writel(0xffff4089, host->base + MSDC_PATCH_BIT1);
1735 sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL);
1737 if (host->dev_comp->stop_clk_fix) {
1738 sdr_set_field(host->base + MSDC_PATCH_BIT1,
1739 MSDC_PATCH_BIT1_STOP_DLY, 3);
1740 sdr_clr_bits(host->base + SDC_FIFO_CFG,
1741 SDC_FIFO_CFG_WRVALIDSEL);
1742 sdr_clr_bits(host->base + SDC_FIFO_CFG,
1743 SDC_FIFO_CFG_RDVALIDSEL);
1746 if (host->dev_comp->busy_check)
1747 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, BIT(7));
1749 if (host->dev_comp->async_fifo) {
1750 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1751 MSDC_PB2_RESPWAIT, 3);
1752 if (host->dev_comp->enhance_rx) {
1754 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1757 sdr_set_bits(host->base + SDC_ADV_CFG0,
1760 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1761 MSDC_PB2_RESPSTSENSEL, 2);
1762 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1763 MSDC_PB2_CRCSTSENSEL, 2);
1765 /* use async fifo, then no need tune internal delay */
1766 sdr_clr_bits(host->base + MSDC_PATCH_BIT2,
1767 MSDC_PATCH_BIT2_CFGRESP);
1768 sdr_set_bits(host->base + MSDC_PATCH_BIT2,
1769 MSDC_PATCH_BIT2_CFGCRCSTS);
1772 if (host->dev_comp->support_64g)
1773 sdr_set_bits(host->base + MSDC_PATCH_BIT2,
1774 MSDC_PB2_SUPPORT_64G);
1775 if (host->dev_comp->data_tune) {
1776 if (host->top_base) {
1777 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1778 PAD_DAT_RD_RXDLY_SEL);
1779 sdr_clr_bits(host->top_base + EMMC_TOP_CONTROL,
1781 sdr_set_bits(host->top_base + EMMC_TOP_CMD,
1782 PAD_CMD_RD_RXDLY_SEL);
1784 sdr_set_bits(host->base + tune_reg,
1785 MSDC_PAD_TUNE_RD_SEL |
1786 MSDC_PAD_TUNE_CMD_SEL);
1789 /* choose clock tune */
1791 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1794 sdr_set_bits(host->base + tune_reg,
1795 MSDC_PAD_TUNE_RXDLYSEL);
1798 if (mmc->caps2 & MMC_CAP2_NO_SDIO) {
1799 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIO);
1800 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ);
1801 sdr_clr_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER);
1803 /* Configure to enable SDIO mode, otherwise SDIO CMD5 fails */
1804 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO);
1806 /* Config SDIO device detect interrupt function */
1807 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1808 sdr_set_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER);
1811 /* Configure to default data timeout */
1812 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3);
1814 host->def_tune_para.iocon = readl(host->base + MSDC_IOCON);
1815 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
1816 if (host->top_base) {
1817 host->def_tune_para.emmc_top_control =
1818 readl(host->top_base + EMMC_TOP_CONTROL);
1819 host->def_tune_para.emmc_top_cmd =
1820 readl(host->top_base + EMMC_TOP_CMD);
1821 host->saved_tune_para.emmc_top_control =
1822 readl(host->top_base + EMMC_TOP_CONTROL);
1823 host->saved_tune_para.emmc_top_cmd =
1824 readl(host->top_base + EMMC_TOP_CMD);
1826 host->def_tune_para.pad_tune = readl(host->base + tune_reg);
1827 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
1829 dev_dbg(host->dev, "init hardware done!");
1832 static void msdc_deinit_hw(struct msdc_host *host)
1836 if (host->internal_cd) {
1837 /* Disabled card-detect */
1838 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
1839 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP);
1842 /* Disable and clear all interrupts */
1843 writel(0, host->base + MSDC_INTEN);
1845 val = readl(host->base + MSDC_INT);
1846 writel(val, host->base + MSDC_INT);
1849 /* init gpd and bd list in msdc_drv_probe */
1850 static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
1852 struct mt_gpdma_desc *gpd = dma->gpd;
1853 struct mt_bdma_desc *bd = dma->bd;
1854 dma_addr_t dma_addr;
1857 memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2);
1859 dma_addr = dma->gpd_addr + sizeof(struct mt_gpdma_desc);
1860 gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */
1861 /* gpd->next is must set for desc DMA
1862 * That's why must alloc 2 gpd structure.
1864 gpd->next = lower_32_bits(dma_addr);
1865 if (host->dev_comp->support_64g)
1866 gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 24;
1868 dma_addr = dma->bd_addr;
1869 gpd->ptr = lower_32_bits(dma->bd_addr); /* physical address */
1870 if (host->dev_comp->support_64g)
1871 gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 28;
1873 memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM);
1874 for (i = 0; i < (MAX_BD_NUM - 1); i++) {
1875 dma_addr = dma->bd_addr + sizeof(*bd) * (i + 1);
1876 bd[i].next = lower_32_bits(dma_addr);
1877 if (host->dev_comp->support_64g)
1878 bd[i].bd_info |= (upper_32_bits(dma_addr) & 0xf) << 24;
1882 static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1884 struct msdc_host *host = mmc_priv(mmc);
1887 msdc_set_buswidth(host, ios->bus_width);
1889 /* Suspend/Resume will do power off/on */
1890 switch (ios->power_mode) {
1892 if (!IS_ERR(mmc->supply.vmmc)) {
1894 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1897 dev_err(host->dev, "Failed to set vmmc power!\n");
1903 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
1904 ret = regulator_enable(mmc->supply.vqmmc);
1906 dev_err(host->dev, "Failed to set vqmmc power!\n");
1908 host->vqmmc_enabled = true;
1912 if (!IS_ERR(mmc->supply.vmmc))
1913 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1915 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
1916 regulator_disable(mmc->supply.vqmmc);
1917 host->vqmmc_enabled = false;
1924 if (host->mclk != ios->clock || host->timing != ios->timing)
1925 msdc_set_mclk(host, ios->timing, ios->clock);
1928 static u32 test_delay_bit(u32 delay, u32 bit)
1930 bit %= PAD_DELAY_MAX;
1931 return delay & BIT(bit);
1934 static int get_delay_len(u32 delay, u32 start_bit)
1938 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
1939 if (test_delay_bit(delay, start_bit + i) == 0)
1942 return PAD_DELAY_MAX - start_bit;
1945 static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay)
1947 int start = 0, len = 0;
1948 int start_final = 0, len_final = 0;
1949 u8 final_phase = 0xff;
1950 struct msdc_delay_phase delay_phase = { 0, };
1953 dev_err(host->dev, "phase error: [map:%x]\n", delay);
1954 delay_phase.final_phase = final_phase;
1958 while (start < PAD_DELAY_MAX) {
1959 len = get_delay_len(delay, start);
1960 if (len_final < len) {
1961 start_final = start;
1964 start += len ? len : 1;
1965 if (len >= 12 && start_final < 4)
1969 /* The rule is that to find the smallest delay cell */
1970 if (start_final == 0)
1971 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
1973 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
1974 dev_dbg(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
1975 delay, len_final, final_phase);
1977 delay_phase.maxlen = len_final;
1978 delay_phase.start = start_final;
1979 delay_phase.final_phase = final_phase;
1983 static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value)
1985 u32 tune_reg = host->dev_comp->pad_tune_reg;
1988 sdr_set_field(host->top_base + EMMC_TOP_CMD, PAD_CMD_RXDLY,
1991 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY,
1995 static inline void msdc_set_data_delay(struct msdc_host *host, u32 value)
1997 u32 tune_reg = host->dev_comp->pad_tune_reg;
2000 sdr_set_field(host->top_base + EMMC_TOP_CONTROL,
2001 PAD_DAT_RD_RXDLY, value);
2003 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_DATRRDLY,
2007 static int msdc_tune_response(struct mmc_host *mmc, u32 opcode)
2009 struct msdc_host *host = mmc_priv(mmc);
2010 u32 rise_delay = 0, fall_delay = 0;
2011 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
2012 struct msdc_delay_phase internal_delay_phase;
2013 u8 final_delay, final_maxlen;
2014 u32 internal_delay = 0;
2015 u32 tune_reg = host->dev_comp->pad_tune_reg;
2019 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
2020 mmc->ios.timing == MMC_TIMING_UHS_SDR104)
2021 sdr_set_field(host->base + tune_reg,
2022 MSDC_PAD_TUNE_CMDRRDLY,
2023 host->hs200_cmd_int_delay);
2025 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2026 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
2027 msdc_set_cmd_delay(host, i);
2029 * Using the same parameters, it may sometimes pass the test,
2030 * but sometimes it may fail. To make sure the parameters are
2031 * more stable, we test each set of parameters 3 times.
2033 for (j = 0; j < 3; j++) {
2034 mmc_send_tuning(mmc, opcode, &cmd_err);
2036 rise_delay |= BIT(i);
2038 rise_delay &= ~BIT(i);
2043 final_rise_delay = get_best_delay(host, rise_delay);
2044 /* if rising edge has enough margin, then do not scan falling edge */
2045 if (final_rise_delay.maxlen >= 12 ||
2046 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
2049 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2050 for (i = 0; i < PAD_DELAY_MAX; i++) {
2051 msdc_set_cmd_delay(host, i);
2053 * Using the same parameters, it may sometimes pass the test,
2054 * but sometimes it may fail. To make sure the parameters are
2055 * more stable, we test each set of parameters 3 times.
2057 for (j = 0; j < 3; j++) {
2058 mmc_send_tuning(mmc, opcode, &cmd_err);
2060 fall_delay |= BIT(i);
2062 fall_delay &= ~BIT(i);
2067 final_fall_delay = get_best_delay(host, fall_delay);
2070 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
2071 if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4)
2072 final_maxlen = final_fall_delay.maxlen;
2073 if (final_maxlen == final_rise_delay.maxlen) {
2074 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2075 final_delay = final_rise_delay.final_phase;
2077 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2078 final_delay = final_fall_delay.final_phase;
2080 msdc_set_cmd_delay(host, final_delay);
2082 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
2085 for (i = 0; i < PAD_DELAY_MAX; i++) {
2086 sdr_set_field(host->base + tune_reg,
2087 MSDC_PAD_TUNE_CMDRRDLY, i);
2088 mmc_send_tuning(mmc, opcode, &cmd_err);
2090 internal_delay |= BIT(i);
2092 dev_dbg(host->dev, "Final internal delay: 0x%x\n", internal_delay);
2093 internal_delay_phase = get_best_delay(host, internal_delay);
2094 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY,
2095 internal_delay_phase.final_phase);
2097 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay);
2098 return final_delay == 0xff ? -EIO : 0;
2101 static int hs400_tune_response(struct mmc_host *mmc, u32 opcode)
2103 struct msdc_host *host = mmc_priv(mmc);
2105 struct msdc_delay_phase final_cmd_delay = { 0,};
2110 /* select EMMC50 PAD CMD tune */
2111 sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0));
2112 sdr_set_field(host->base + MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMDTA, 2);
2114 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
2115 mmc->ios.timing == MMC_TIMING_UHS_SDR104)
2116 sdr_set_field(host->base + MSDC_PAD_TUNE,
2117 MSDC_PAD_TUNE_CMDRRDLY,
2118 host->hs200_cmd_int_delay);
2120 if (host->hs400_cmd_resp_sel_rising)
2121 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2123 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2124 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
2125 sdr_set_field(host->base + PAD_CMD_TUNE,
2126 PAD_CMD_TUNE_RX_DLY3, i);
2128 * Using the same parameters, it may sometimes pass the test,
2129 * but sometimes it may fail. To make sure the parameters are
2130 * more stable, we test each set of parameters 3 times.
2132 for (j = 0; j < 3; j++) {
2133 mmc_send_tuning(mmc, opcode, &cmd_err);
2135 cmd_delay |= BIT(i);
2137 cmd_delay &= ~BIT(i);
2142 final_cmd_delay = get_best_delay(host, cmd_delay);
2143 sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3,
2144 final_cmd_delay.final_phase);
2145 final_delay = final_cmd_delay.final_phase;
2147 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay);
2148 return final_delay == 0xff ? -EIO : 0;
2151 static int msdc_tune_data(struct mmc_host *mmc, u32 opcode)
2153 struct msdc_host *host = mmc_priv(mmc);
2154 u32 rise_delay = 0, fall_delay = 0;
2155 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
2156 u8 final_delay, final_maxlen;
2159 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL,
2161 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
2162 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
2163 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
2164 msdc_set_data_delay(host, i);
2165 ret = mmc_send_tuning(mmc, opcode, NULL);
2167 rise_delay |= BIT(i);
2169 final_rise_delay = get_best_delay(host, rise_delay);
2170 /* if rising edge has enough margin, then do not scan falling edge */
2171 if (final_rise_delay.maxlen >= 12 ||
2172 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
2175 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
2176 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
2177 for (i = 0; i < PAD_DELAY_MAX; i++) {
2178 msdc_set_data_delay(host, i);
2179 ret = mmc_send_tuning(mmc, opcode, NULL);
2181 fall_delay |= BIT(i);
2183 final_fall_delay = get_best_delay(host, fall_delay);
2186 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
2187 if (final_maxlen == final_rise_delay.maxlen) {
2188 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
2189 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
2190 final_delay = final_rise_delay.final_phase;
2192 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
2193 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
2194 final_delay = final_fall_delay.final_phase;
2196 msdc_set_data_delay(host, final_delay);
2198 dev_dbg(host->dev, "Final data pad delay: %x\n", final_delay);
2199 return final_delay == 0xff ? -EIO : 0;
2203 * MSDC IP which supports data tune + async fifo can do CMD/DAT tune
2204 * together, which can save the tuning time.
2206 static int msdc_tune_together(struct mmc_host *mmc, u32 opcode)
2208 struct msdc_host *host = mmc_priv(mmc);
2209 u32 rise_delay = 0, fall_delay = 0;
2210 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
2211 u8 final_delay, final_maxlen;
2214 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL,
2217 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2218 sdr_clr_bits(host->base + MSDC_IOCON,
2219 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
2220 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
2221 msdc_set_cmd_delay(host, i);
2222 msdc_set_data_delay(host, i);
2223 ret = mmc_send_tuning(mmc, opcode, NULL);
2225 rise_delay |= BIT(i);
2227 final_rise_delay = get_best_delay(host, rise_delay);
2228 /* if rising edge has enough margin, then do not scan falling edge */
2229 if (final_rise_delay.maxlen >= 12 ||
2230 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
2233 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2234 sdr_set_bits(host->base + MSDC_IOCON,
2235 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
2236 for (i = 0; i < PAD_DELAY_MAX; i++) {
2237 msdc_set_cmd_delay(host, i);
2238 msdc_set_data_delay(host, i);
2239 ret = mmc_send_tuning(mmc, opcode, NULL);
2241 fall_delay |= BIT(i);
2243 final_fall_delay = get_best_delay(host, fall_delay);
2246 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
2247 if (final_maxlen == final_rise_delay.maxlen) {
2248 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2249 sdr_clr_bits(host->base + MSDC_IOCON,
2250 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
2251 final_delay = final_rise_delay.final_phase;
2253 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
2254 sdr_set_bits(host->base + MSDC_IOCON,
2255 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
2256 final_delay = final_fall_delay.final_phase;
2259 msdc_set_cmd_delay(host, final_delay);
2260 msdc_set_data_delay(host, final_delay);
2262 dev_dbg(host->dev, "Final pad delay: %x\n", final_delay);
2263 return final_delay == 0xff ? -EIO : 0;
2266 static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode)
2268 struct msdc_host *host = mmc_priv(mmc);
2270 u32 tune_reg = host->dev_comp->pad_tune_reg;
2272 if (host->dev_comp->data_tune && host->dev_comp->async_fifo) {
2273 ret = msdc_tune_together(mmc, opcode);
2274 if (host->hs400_mode) {
2275 sdr_clr_bits(host->base + MSDC_IOCON,
2276 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
2277 msdc_set_data_delay(host, 0);
2281 if (host->hs400_mode &&
2282 host->dev_comp->hs400_tune)
2283 ret = hs400_tune_response(mmc, opcode);
2285 ret = msdc_tune_response(mmc, opcode);
2287 dev_err(host->dev, "Tune response fail!\n");
2290 if (host->hs400_mode == false) {
2291 ret = msdc_tune_data(mmc, opcode);
2293 dev_err(host->dev, "Tune data fail!\n");
2297 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
2298 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
2299 host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE);
2300 if (host->top_base) {
2301 host->saved_tune_para.emmc_top_control = readl(host->top_base +
2303 host->saved_tune_para.emmc_top_cmd = readl(host->top_base +
2309 static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
2311 struct msdc_host *host = mmc_priv(mmc);
2312 host->hs400_mode = true;
2315 writel(host->hs400_ds_delay,
2316 host->top_base + EMMC50_PAD_DS_TUNE);
2318 writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE);
2319 /* hs400 mode must set it to 0 */
2320 sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS);
2321 /* to improve read performance, set outstanding to 2 */
2322 sdr_set_field(host->base + EMMC50_CFG3, EMMC50_CFG3_OUTS_WR, 2);
2327 static int msdc_execute_hs400_tuning(struct mmc_host *mmc, struct mmc_card *card)
2329 struct msdc_host *host = mmc_priv(mmc);
2330 struct msdc_delay_phase dly1_delay;
2331 u32 val, result_dly1 = 0;
2335 if (host->top_base) {
2336 sdr_set_bits(host->top_base + EMMC50_PAD_DS_TUNE,
2338 if (host->hs400_ds_dly3)
2339 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE,
2340 PAD_DS_DLY3, host->hs400_ds_dly3);
2342 sdr_set_bits(host->base + PAD_DS_TUNE, PAD_DS_TUNE_DLY_SEL);
2343 if (host->hs400_ds_dly3)
2344 sdr_set_field(host->base + PAD_DS_TUNE,
2345 PAD_DS_TUNE_DLY3, host->hs400_ds_dly3);
2348 host->hs400_tuning = true;
2349 for (i = 0; i < PAD_DELAY_MAX; i++) {
2351 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE,
2354 sdr_set_field(host->base + PAD_DS_TUNE,
2355 PAD_DS_TUNE_DLY1, i);
2356 ret = mmc_get_ext_csd(card, &ext_csd);
2358 result_dly1 |= BIT(i);
2362 host->hs400_tuning = false;
2364 dly1_delay = get_best_delay(host, result_dly1);
2365 if (dly1_delay.maxlen == 0) {
2366 dev_err(host->dev, "Failed to get DLY1 delay!\n");
2370 sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE,
2371 PAD_DS_DLY1, dly1_delay.final_phase);
2373 sdr_set_field(host->base + PAD_DS_TUNE,
2374 PAD_DS_TUNE_DLY1, dly1_delay.final_phase);
2377 val = readl(host->top_base + EMMC50_PAD_DS_TUNE);
2379 val = readl(host->base + PAD_DS_TUNE);
2381 dev_info(host->dev, "Final PAD_DS_TUNE: 0x%x\n", val);
2386 dev_err(host->dev, "Failed to tuning DS pin delay!\n");
2390 static void msdc_hw_reset(struct mmc_host *mmc)
2392 struct msdc_host *host = mmc_priv(mmc);
2394 sdr_set_bits(host->base + EMMC_IOCON, 1);
2395 udelay(10); /* 10us is enough */
2396 sdr_clr_bits(host->base + EMMC_IOCON, 1);
2399 static void msdc_ack_sdio_irq(struct mmc_host *mmc)
2401 unsigned long flags;
2402 struct msdc_host *host = mmc_priv(mmc);
2404 spin_lock_irqsave(&host->lock, flags);
2405 __msdc_enable_sdio_irq(host, 1);
2406 spin_unlock_irqrestore(&host->lock, flags);
2409 static int msdc_get_cd(struct mmc_host *mmc)
2411 struct msdc_host *host = mmc_priv(mmc);
2414 if (mmc->caps & MMC_CAP_NONREMOVABLE)
2417 if (!host->internal_cd)
2418 return mmc_gpio_get_cd(mmc);
2420 val = readl(host->base + MSDC_PS) & MSDC_PS_CDSTS;
2421 if (mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
2427 static void msdc_hs400_enhanced_strobe(struct mmc_host *mmc,
2428 struct mmc_ios *ios)
2430 struct msdc_host *host = mmc_priv(mmc);
2432 if (ios->enhanced_strobe) {
2433 msdc_prepare_hs400_tuning(mmc, ios);
2434 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 1);
2435 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 1);
2436 sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 1);
2438 sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL);
2439 sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL);
2440 sdr_clr_bits(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT);
2442 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 0);
2443 sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 0);
2444 sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 0);
2446 sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL);
2447 sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL);
2448 sdr_set_field(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT, 0xb4);
2452 static void msdc_cqe_cit_cal(struct msdc_host *host, u64 timer_ns)
2454 struct mmc_host *mmc = mmc_from_priv(host);
2455 struct cqhci_host *cq_host = mmc->cqe_private;
2457 u64 hclk_freq, value;
2460 * On MediaTek SoCs the MSDC controller's CQE uses msdc_hclk as ITCFVAL
2461 * so we multiply/divide the HCLK frequency by ITCFMUL to calculate the
2462 * Send Status Command Idle Timer (CIT) value.
2464 hclk_freq = (u64)clk_get_rate(host->h_clk);
2465 itcfmul = CQHCI_ITCFMUL(cqhci_readl(cq_host, CQHCI_CAP));
2468 do_div(hclk_freq, 1000);
2471 do_div(hclk_freq, 100);
2474 do_div(hclk_freq, 10);
2479 hclk_freq = hclk_freq * 10;
2482 host->cq_ssc1_time = 0x40;
2486 value = hclk_freq * timer_ns;
2487 do_div(value, 1000000000);
2488 host->cq_ssc1_time = value;
2491 static void msdc_cqe_enable(struct mmc_host *mmc)
2493 struct msdc_host *host = mmc_priv(mmc);
2494 struct cqhci_host *cq_host = mmc->cqe_private;
2496 /* enable cmdq irq */
2497 writel(MSDC_INT_CMDQ, host->base + MSDC_INTEN);
2498 /* enable busy check */
2499 sdr_set_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL);
2500 /* default write data / busy timeout 20s */
2501 msdc_set_busy_timeout(host, 20 * 1000000000ULL, 0);
2502 /* default read data timeout 1s */
2503 msdc_set_timeout(host, 1000000000ULL, 0);
2505 /* Set the send status command idle timer */
2506 cqhci_writel(cq_host, host->cq_ssc1_time, CQHCI_SSC1);
2509 static void msdc_cqe_disable(struct mmc_host *mmc, bool recovery)
2511 struct msdc_host *host = mmc_priv(mmc);
2512 unsigned int val = 0;
2514 /* disable cmdq irq */
2515 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INT_CMDQ);
2516 /* disable busy check */
2517 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL);
2519 val = readl(host->base + MSDC_INT);
2520 writel(val, host->base + MSDC_INT);
2523 sdr_set_field(host->base + MSDC_DMA_CTRL,
2524 MSDC_DMA_CTRL_STOP, 1);
2525 if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CTRL, val,
2526 !(val & MSDC_DMA_CTRL_STOP), 1, 3000)))
2528 if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CFG, val,
2529 !(val & MSDC_DMA_CFG_STS), 1, 3000)))
2531 msdc_reset_hw(host);
2535 static void msdc_cqe_pre_enable(struct mmc_host *mmc)
2537 struct cqhci_host *cq_host = mmc->cqe_private;
2540 reg = cqhci_readl(cq_host, CQHCI_CFG);
2541 reg |= CQHCI_ENABLE;
2542 cqhci_writel(cq_host, reg, CQHCI_CFG);
2545 static void msdc_cqe_post_disable(struct mmc_host *mmc)
2547 struct cqhci_host *cq_host = mmc->cqe_private;
2550 reg = cqhci_readl(cq_host, CQHCI_CFG);
2551 reg &= ~CQHCI_ENABLE;
2552 cqhci_writel(cq_host, reg, CQHCI_CFG);
2555 static const struct mmc_host_ops mt_msdc_ops = {
2556 .post_req = msdc_post_req,
2557 .pre_req = msdc_pre_req,
2558 .request = msdc_ops_request,
2559 .set_ios = msdc_ops_set_ios,
2560 .get_ro = mmc_gpio_get_ro,
2561 .get_cd = msdc_get_cd,
2562 .hs400_enhanced_strobe = msdc_hs400_enhanced_strobe,
2563 .enable_sdio_irq = msdc_enable_sdio_irq,
2564 .ack_sdio_irq = msdc_ack_sdio_irq,
2565 .start_signal_voltage_switch = msdc_ops_switch_volt,
2566 .card_busy = msdc_card_busy,
2567 .execute_tuning = msdc_execute_tuning,
2568 .prepare_hs400_tuning = msdc_prepare_hs400_tuning,
2569 .execute_hs400_tuning = msdc_execute_hs400_tuning,
2570 .card_hw_reset = msdc_hw_reset,
2573 static const struct cqhci_host_ops msdc_cmdq_ops = {
2574 .enable = msdc_cqe_enable,
2575 .disable = msdc_cqe_disable,
2576 .pre_enable = msdc_cqe_pre_enable,
2577 .post_disable = msdc_cqe_post_disable,
2580 static void msdc_of_property_parse(struct platform_device *pdev,
2581 struct msdc_host *host)
2583 of_property_read_u32(pdev->dev.of_node, "mediatek,latch-ck",
2586 of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay",
2587 &host->hs400_ds_delay);
2589 of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-ds-dly3",
2590 &host->hs400_ds_dly3);
2592 of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay",
2593 &host->hs200_cmd_int_delay);
2595 of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay",
2596 &host->hs400_cmd_int_delay);
2598 if (of_property_read_bool(pdev->dev.of_node,
2599 "mediatek,hs400-cmd-resp-sel-rising"))
2600 host->hs400_cmd_resp_sel_rising = true;
2602 host->hs400_cmd_resp_sel_rising = false;
2604 if (of_property_read_bool(pdev->dev.of_node,
2608 host->cqhci = false;
2611 static int msdc_of_clock_parse(struct platform_device *pdev,
2612 struct msdc_host *host)
2616 host->src_clk = devm_clk_get(&pdev->dev, "source");
2617 if (IS_ERR(host->src_clk))
2618 return PTR_ERR(host->src_clk);
2620 host->h_clk = devm_clk_get(&pdev->dev, "hclk");
2621 if (IS_ERR(host->h_clk))
2622 return PTR_ERR(host->h_clk);
2624 host->bus_clk = devm_clk_get_optional(&pdev->dev, "bus_clk");
2625 if (IS_ERR(host->bus_clk))
2626 host->bus_clk = NULL;
2628 /*source clock control gate is optional clock*/
2629 host->src_clk_cg = devm_clk_get_optional(&pdev->dev, "source_cg");
2630 if (IS_ERR(host->src_clk_cg))
2631 return PTR_ERR(host->src_clk_cg);
2634 * Fallback for legacy device-trees: src_clk and HCLK use the same
2635 * bit to control gating but they are parented to a different mux,
2636 * hence if our intention is to gate only the source, required
2637 * during a clk mode switch to avoid hw hangs, we need to gate
2638 * its parent (specified as a different clock only on new DTs).
2640 if (!host->src_clk_cg) {
2641 host->src_clk_cg = clk_get_parent(host->src_clk);
2642 if (IS_ERR(host->src_clk_cg))
2643 return PTR_ERR(host->src_clk_cg);
2646 /* If present, always enable for this clock gate */
2647 host->sys_clk_cg = devm_clk_get_optional_enabled(&pdev->dev, "sys_cg");
2648 if (IS_ERR(host->sys_clk_cg))
2649 host->sys_clk_cg = NULL;
2651 host->bulk_clks[0].id = "pclk_cg";
2652 host->bulk_clks[1].id = "axi_cg";
2653 host->bulk_clks[2].id = "ahb_cg";
2654 ret = devm_clk_bulk_get_optional(&pdev->dev, MSDC_NR_CLOCKS,
2657 dev_err(&pdev->dev, "Cannot get pclk/axi/ahb clock gates\n");
2664 static int msdc_drv_probe(struct platform_device *pdev)
2666 struct mmc_host *mmc;
2667 struct msdc_host *host;
2668 struct resource *res;
2671 if (!pdev->dev.of_node) {
2672 dev_err(&pdev->dev, "No DT found\n");
2676 /* Allocate MMC host for this device */
2677 mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
2681 host = mmc_priv(mmc);
2682 ret = mmc_of_parse(mmc);
2686 host->base = devm_platform_ioremap_resource(pdev, 0);
2687 if (IS_ERR(host->base)) {
2688 ret = PTR_ERR(host->base);
2692 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2694 host->top_base = devm_ioremap_resource(&pdev->dev, res);
2695 if (IS_ERR(host->top_base))
2696 host->top_base = NULL;
2699 ret = mmc_regulator_get_supply(mmc);
2703 ret = msdc_of_clock_parse(pdev, host);
2707 host->reset = devm_reset_control_get_optional_exclusive(&pdev->dev,
2709 if (IS_ERR(host->reset)) {
2710 ret = PTR_ERR(host->reset);
2714 /* only eMMC has crypto property */
2715 if (!(mmc->caps2 & MMC_CAP2_NO_MMC)) {
2716 host->crypto_clk = devm_clk_get_optional(&pdev->dev, "crypto");
2717 if (IS_ERR(host->crypto_clk))
2718 host->crypto_clk = NULL;
2720 mmc->caps2 |= MMC_CAP2_CRYPTO;
2723 host->irq = platform_get_irq(pdev, 0);
2724 if (host->irq < 0) {
2729 host->pinctrl = devm_pinctrl_get(&pdev->dev);
2730 if (IS_ERR(host->pinctrl)) {
2731 ret = PTR_ERR(host->pinctrl);
2732 dev_err(&pdev->dev, "Cannot find pinctrl!\n");
2736 host->pins_default = pinctrl_lookup_state(host->pinctrl, "default");
2737 if (IS_ERR(host->pins_default)) {
2738 ret = PTR_ERR(host->pins_default);
2739 dev_err(&pdev->dev, "Cannot find pinctrl default!\n");
2743 host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs");
2744 if (IS_ERR(host->pins_uhs)) {
2745 ret = PTR_ERR(host->pins_uhs);
2746 dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n");
2750 /* Support for SDIO eint irq ? */
2751 if ((mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ) && (mmc->pm_caps & MMC_PM_KEEP_POWER)) {
2752 host->eint_irq = platform_get_irq_byname_optional(pdev, "sdio_wakeup");
2753 if (host->eint_irq > 0) {
2754 host->pins_eint = pinctrl_lookup_state(host->pinctrl, "state_eint");
2755 if (IS_ERR(host->pins_eint)) {
2756 dev_err(&pdev->dev, "Cannot find pinctrl eint!\n");
2757 host->pins_eint = NULL;
2759 device_init_wakeup(&pdev->dev, true);
2764 msdc_of_property_parse(pdev, host);
2766 host->dev = &pdev->dev;
2767 host->dev_comp = of_device_get_match_data(&pdev->dev);
2768 host->src_clk_freq = clk_get_rate(host->src_clk);
2769 /* Set host parameters to mmc */
2770 mmc->ops = &mt_msdc_ops;
2771 if (host->dev_comp->clk_div_bits == 8)
2772 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255);
2774 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095);
2776 if (!(mmc->caps & MMC_CAP_NONREMOVABLE) &&
2777 !mmc_can_gpio_cd(mmc) &&
2778 host->dev_comp->use_internal_cd) {
2780 * Is removable but no GPIO declared, so
2781 * use internal functionality.
2783 host->internal_cd = true;
2786 if (mmc->caps & MMC_CAP_SDIO_IRQ)
2787 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2789 mmc->caps |= MMC_CAP_CMD23;
2791 mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
2792 /* MMC core transfer sizes tunable parameters */
2793 mmc->max_segs = MAX_BD_NUM;
2794 if (host->dev_comp->support_64g)
2795 mmc->max_seg_size = BDMA_DESC_BUFLEN_EXT;
2797 mmc->max_seg_size = BDMA_DESC_BUFLEN;
2798 mmc->max_blk_size = 2048;
2799 mmc->max_req_size = 512 * 1024;
2800 mmc->max_blk_count = mmc->max_req_size / 512;
2801 if (host->dev_comp->support_64g)
2802 host->dma_mask = DMA_BIT_MASK(36);
2804 host->dma_mask = DMA_BIT_MASK(32);
2805 mmc_dev(mmc)->dma_mask = &host->dma_mask;
2807 host->timeout_clks = 3 * 1048576;
2808 host->dma.gpd = dma_alloc_coherent(&pdev->dev,
2809 2 * sizeof(struct mt_gpdma_desc),
2810 &host->dma.gpd_addr, GFP_KERNEL);
2811 host->dma.bd = dma_alloc_coherent(&pdev->dev,
2812 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
2813 &host->dma.bd_addr, GFP_KERNEL);
2814 if (!host->dma.gpd || !host->dma.bd) {
2818 msdc_init_gpd_bd(host, &host->dma);
2819 INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout);
2820 spin_lock_init(&host->lock);
2822 platform_set_drvdata(pdev, mmc);
2823 ret = msdc_ungate_clock(host);
2825 dev_err(&pdev->dev, "Cannot ungate clocks!\n");
2830 if (mmc->caps2 & MMC_CAP2_CQE) {
2831 host->cq_host = devm_kzalloc(mmc->parent,
2832 sizeof(*host->cq_host),
2834 if (!host->cq_host) {
2838 host->cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
2839 host->cq_host->mmio = host->base + 0x800;
2840 host->cq_host->ops = &msdc_cmdq_ops;
2841 ret = cqhci_init(host->cq_host, mmc, true);
2844 mmc->max_segs = 128;
2845 /* cqhci 16bit length */
2846 /* 0 size, means 65536 so we don't have to -1 here */
2847 mmc->max_seg_size = 64 * 1024;
2848 /* Reduce CIT to 0x40 that corresponds to 2.35us */
2849 msdc_cqe_cit_cal(host, 2350);
2852 ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq,
2853 IRQF_TRIGGER_NONE, pdev->name, host);
2857 pm_runtime_set_active(host->dev);
2858 pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY);
2859 pm_runtime_use_autosuspend(host->dev);
2860 pm_runtime_enable(host->dev);
2861 ret = mmc_add_host(mmc);
2868 pm_runtime_disable(host->dev);
2870 platform_set_drvdata(pdev, NULL);
2871 msdc_deinit_hw(host);
2872 msdc_gate_clock(host);
2875 dma_free_coherent(&pdev->dev,
2876 2 * sizeof(struct mt_gpdma_desc),
2877 host->dma.gpd, host->dma.gpd_addr);
2879 dma_free_coherent(&pdev->dev,
2880 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
2881 host->dma.bd, host->dma.bd_addr);
2888 static void msdc_drv_remove(struct platform_device *pdev)
2890 struct mmc_host *mmc;
2891 struct msdc_host *host;
2893 mmc = platform_get_drvdata(pdev);
2894 host = mmc_priv(mmc);
2896 pm_runtime_get_sync(host->dev);
2898 platform_set_drvdata(pdev, NULL);
2899 mmc_remove_host(mmc);
2900 msdc_deinit_hw(host);
2901 msdc_gate_clock(host);
2903 pm_runtime_disable(host->dev);
2904 pm_runtime_put_noidle(host->dev);
2905 dma_free_coherent(&pdev->dev,
2906 2 * sizeof(struct mt_gpdma_desc),
2907 host->dma.gpd, host->dma.gpd_addr);
2908 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc),
2909 host->dma.bd, host->dma.bd_addr);
2914 static void msdc_save_reg(struct msdc_host *host)
2916 u32 tune_reg = host->dev_comp->pad_tune_reg;
2918 host->save_para.msdc_cfg = readl(host->base + MSDC_CFG);
2919 host->save_para.iocon = readl(host->base + MSDC_IOCON);
2920 host->save_para.sdc_cfg = readl(host->base + SDC_CFG);
2921 host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT);
2922 host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1);
2923 host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2);
2924 host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE);
2925 host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE);
2926 host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0);
2927 host->save_para.emmc50_cfg3 = readl(host->base + EMMC50_CFG3);
2928 host->save_para.sdc_fifo_cfg = readl(host->base + SDC_FIFO_CFG);
2929 if (host->top_base) {
2930 host->save_para.emmc_top_control =
2931 readl(host->top_base + EMMC_TOP_CONTROL);
2932 host->save_para.emmc_top_cmd =
2933 readl(host->top_base + EMMC_TOP_CMD);
2934 host->save_para.emmc50_pad_ds_tune =
2935 readl(host->top_base + EMMC50_PAD_DS_TUNE);
2937 host->save_para.pad_tune = readl(host->base + tune_reg);
2941 static void msdc_restore_reg(struct msdc_host *host)
2943 struct mmc_host *mmc = mmc_from_priv(host);
2944 u32 tune_reg = host->dev_comp->pad_tune_reg;
2946 writel(host->save_para.msdc_cfg, host->base + MSDC_CFG);
2947 writel(host->save_para.iocon, host->base + MSDC_IOCON);
2948 writel(host->save_para.sdc_cfg, host->base + SDC_CFG);
2949 writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT);
2950 writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1);
2951 writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2);
2952 writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE);
2953 writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE);
2954 writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0);
2955 writel(host->save_para.emmc50_cfg3, host->base + EMMC50_CFG3);
2956 writel(host->save_para.sdc_fifo_cfg, host->base + SDC_FIFO_CFG);
2957 if (host->top_base) {
2958 writel(host->save_para.emmc_top_control,
2959 host->top_base + EMMC_TOP_CONTROL);
2960 writel(host->save_para.emmc_top_cmd,
2961 host->top_base + EMMC_TOP_CMD);
2962 writel(host->save_para.emmc50_pad_ds_tune,
2963 host->top_base + EMMC50_PAD_DS_TUNE);
2965 writel(host->save_para.pad_tune, host->base + tune_reg);
2968 if (sdio_irq_claimed(mmc))
2969 __msdc_enable_sdio_irq(host, 1);
2972 static int __maybe_unused msdc_runtime_suspend(struct device *dev)
2974 struct mmc_host *mmc = dev_get_drvdata(dev);
2975 struct msdc_host *host = mmc_priv(mmc);
2977 msdc_save_reg(host);
2979 if (sdio_irq_claimed(mmc)) {
2980 if (host->pins_eint) {
2981 disable_irq(host->irq);
2982 pinctrl_select_state(host->pinctrl, host->pins_eint);
2985 __msdc_enable_sdio_irq(host, 0);
2987 msdc_gate_clock(host);
2991 static int __maybe_unused msdc_runtime_resume(struct device *dev)
2993 struct mmc_host *mmc = dev_get_drvdata(dev);
2994 struct msdc_host *host = mmc_priv(mmc);
2997 ret = msdc_ungate_clock(host);
3001 msdc_restore_reg(host);
3003 if (sdio_irq_claimed(mmc) && host->pins_eint) {
3004 pinctrl_select_state(host->pinctrl, host->pins_uhs);
3005 enable_irq(host->irq);
3010 static int __maybe_unused msdc_suspend(struct device *dev)
3012 struct mmc_host *mmc = dev_get_drvdata(dev);
3013 struct msdc_host *host = mmc_priv(mmc);
3017 if (mmc->caps2 & MMC_CAP2_CQE) {
3018 ret = cqhci_suspend(mmc);
3021 val = readl(host->base + MSDC_INT);
3022 writel(val, host->base + MSDC_INT);
3026 * Bump up runtime PM usage counter otherwise dev->power.needs_force_resume will
3027 * not be marked as 1, pm_runtime_force_resume() will go out directly.
3029 if (sdio_irq_claimed(mmc) && host->pins_eint)
3030 pm_runtime_get_noresume(dev);
3032 return pm_runtime_force_suspend(dev);
3035 static int __maybe_unused msdc_resume(struct device *dev)
3037 struct mmc_host *mmc = dev_get_drvdata(dev);
3038 struct msdc_host *host = mmc_priv(mmc);
3040 if (sdio_irq_claimed(mmc) && host->pins_eint)
3041 pm_runtime_put_noidle(dev);
3043 return pm_runtime_force_resume(dev);
3046 static const struct dev_pm_ops msdc_dev_pm_ops = {
3047 SET_SYSTEM_SLEEP_PM_OPS(msdc_suspend, msdc_resume)
3048 SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL)
3051 static struct platform_driver mt_msdc_driver = {
3052 .probe = msdc_drv_probe,
3053 .remove_new = msdc_drv_remove,
3056 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
3057 .of_match_table = msdc_of_ids,
3058 .pm = &msdc_dev_pm_ops,
3062 module_platform_driver(mt_msdc_driver);
3063 MODULE_LICENSE("GPL v2");
3064 MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver");