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/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/pm_wakeirq.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/reset.h>
27 #include <linux/mmc/card.h>
28 #include <linux/mmc/core.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/mmc.h>
31 #include <linux/mmc/sd.h>
32 #include <linux/mmc/sdio.h>
33 #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 MSDC_NEW_RX_CFG 0x68
70 #define EMMC_IOCON 0x7c
71 #define SDC_ACMD_RESP 0x80
72 #define DMA_SA_H4BIT 0x8c
73 #define MSDC_DMA_SA 0x90
74 #define MSDC_DMA_CTRL 0x98
75 #define MSDC_DMA_CFG 0x9c
76 #define MSDC_PATCH_BIT 0xb0
77 #define MSDC_PATCH_BIT1 0xb4
78 #define MSDC_PATCH_BIT2 0xb8
79 #define MSDC_PAD_TUNE 0xec
80 #define MSDC_PAD_TUNE0 0xf0
81 #define PAD_DS_TUNE 0x188
82 #define PAD_CMD_TUNE 0x18c
83 #define EMMC51_CFG0 0x204
84 #define EMMC50_CFG0 0x208
85 #define EMMC50_CFG1 0x20c
86 #define EMMC50_CFG3 0x220
87 #define SDC_FIFO_CFG 0x228
88 #define CQHCI_SETTING 0x7fc
90 /*--------------------------------------------------------------------------*/
91 /* Top Pad Register Offset */
92 /*--------------------------------------------------------------------------*/
93 #define EMMC_TOP_CONTROL 0x00
94 #define EMMC_TOP_CMD 0x04
95 #define EMMC50_PAD_DS_TUNE 0x0c
96 #define LOOP_TEST_CONTROL 0x30
98 /*--------------------------------------------------------------------------*/
100 /*--------------------------------------------------------------------------*/
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
208 /* SDC_ADV_CFG0 mask */
209 #define SDC_DAT1_IRQ_TRIGGER BIT(19) /* RW */
210 #define SDC_RX_ENHANCE_EN BIT(20) /* RW */
211 #define SDC_NEW_TX_EN BIT(31) /* RW */
213 /* MSDC_NEW_RX_CFG mask */
214 #define MSDC_NEW_RX_PATH_SEL BIT(0) /* RW */
216 /* DMA_SA_H4BIT mask */
217 #define DMA_ADDR_HIGH_4BIT GENMASK(3, 0) /* RW */
219 /* MSDC_DMA_CTRL mask */
220 #define MSDC_DMA_CTRL_START BIT(0) /* W */
221 #define MSDC_DMA_CTRL_STOP BIT(1) /* W */
222 #define MSDC_DMA_CTRL_RESUME BIT(2) /* W */
223 #define MSDC_DMA_CTRL_MODE BIT(8) /* RW */
224 #define MSDC_DMA_CTRL_LASTBUF BIT(10) /* RW */
225 #define MSDC_DMA_CTRL_BRUSTSZ GENMASK(14, 12) /* RW */
227 /* MSDC_DMA_CFG mask */
228 #define MSDC_DMA_CFG_STS BIT(0) /* R */
229 #define MSDC_DMA_CFG_DECSEN BIT(1) /* RW */
230 #define MSDC_DMA_CFG_AHBHPROT2 BIT(9) /* RW */
231 #define MSDC_DMA_CFG_ACTIVEEN BIT(13) /* RW */
232 #define MSDC_DMA_CFG_CS12B16B BIT(16) /* RW */
234 /* MSDC_PATCH_BIT mask */
235 #define MSDC_PATCH_BIT_ODDSUPP BIT(1) /* RW */
236 #define MSDC_PATCH_BIT_RD_DAT_SEL BIT(3) /* RW */
237 #define MSDC_INT_DAT_LATCH_CK_SEL GENMASK(9, 7)
238 #define MSDC_CKGEN_MSDC_DLY_SEL GENMASK(14, 10)
239 #define MSDC_PATCH_BIT_IODSSEL BIT(16) /* RW */
240 #define MSDC_PATCH_BIT_IOINTSEL BIT(17) /* RW */
241 #define MSDC_PATCH_BIT_BUSYDLY GENMASK(21, 18) /* RW */
242 #define MSDC_PATCH_BIT_WDOD GENMASK(25, 22) /* RW */
243 #define MSDC_PATCH_BIT_IDRTSEL BIT(26) /* RW */
244 #define MSDC_PATCH_BIT_CMDFSEL BIT(27) /* RW */
245 #define MSDC_PATCH_BIT_INTDLSEL BIT(28) /* RW */
246 #define MSDC_PATCH_BIT_SPCPUSH BIT(29) /* RW */
247 #define MSDC_PATCH_BIT_DECRCTMO BIT(30) /* RW */
249 #define MSDC_PATCH_BIT1_CMDTA GENMASK(5, 3) /* RW */
250 #define MSDC_PB1_BUSY_CHECK_SEL BIT(7) /* RW */
251 #define MSDC_PATCH_BIT1_STOP_DLY GENMASK(11, 8) /* RW */
253 #define MSDC_PATCH_BIT2_CFGRESP BIT(15) /* RW */
254 #define MSDC_PATCH_BIT2_CFGCRCSTS BIT(28) /* RW */
255 #define MSDC_PB2_SUPPORT_64G BIT(1) /* RW */
256 #define MSDC_PB2_RESPWAIT GENMASK(3, 2) /* RW */
257 #define MSDC_PB2_RESPSTSENSEL GENMASK(18, 16) /* RW */
258 #define MSDC_PB2_POP_EN_CNT GENMASK(23, 20) /* RW */
259 #define MSDC_PB2_CFGCRCSTSEDGE BIT(25) /* RW */
260 #define MSDC_PB2_CRCSTSENSEL GENMASK(31, 29) /* RW */
262 #define MSDC_PAD_TUNE_DATWRDLY GENMASK(4, 0) /* RW */
263 #define MSDC_PAD_TUNE_DATRRDLY GENMASK(12, 8) /* RW */
264 #define MSDC_PAD_TUNE_DATRRDLY2 GENMASK(12, 8) /* RW */
265 #define MSDC_PAD_TUNE_CMDRDLY GENMASK(20, 16) /* RW */
266 #define MSDC_PAD_TUNE_CMDRDLY2 GENMASK(20, 16) /* RW */
267 #define MSDC_PAD_TUNE_CMDRRDLY GENMASK(26, 22) /* RW */
268 #define MSDC_PAD_TUNE_CLKTDLY GENMASK(31, 27) /* RW */
269 #define MSDC_PAD_TUNE_RXDLYSEL BIT(15) /* RW */
270 #define MSDC_PAD_TUNE_RD_SEL BIT(13) /* RW */
271 #define MSDC_PAD_TUNE_CMD_SEL BIT(21) /* RW */
272 #define MSDC_PAD_TUNE_RD2_SEL BIT(13) /* RW */
273 #define MSDC_PAD_TUNE_CMD2_SEL BIT(21) /* RW */
275 #define PAD_DS_TUNE_DLY_SEL BIT(0) /* RW */
276 #define PAD_DS_TUNE_DLY1 GENMASK(6, 2) /* RW */
277 #define PAD_DS_TUNE_DLY2 GENMASK(11, 7) /* RW */
278 #define PAD_DS_TUNE_DLY3 GENMASK(16, 12) /* RW */
280 #define PAD_CMD_TUNE_RX_DLY3 GENMASK(5, 1) /* RW */
282 /* EMMC51_CFG0 mask */
283 #define CMDQ_RDAT_CNT GENMASK(21, 12) /* RW */
285 #define EMMC50_CFG_PADCMD_LATCHCK BIT(0) /* RW */
286 #define EMMC50_CFG_CRCSTS_EDGE BIT(3) /* RW */
287 #define EMMC50_CFG_CFCSTS_SEL BIT(4) /* RW */
288 #define EMMC50_CFG_CMD_RESP_SEL BIT(9) /* RW */
290 /* EMMC50_CFG1 mask */
291 #define EMMC50_CFG1_DS_CFG BIT(28) /* RW */
293 #define EMMC50_CFG3_OUTS_WR GENMASK(4, 0) /* RW */
295 #define SDC_FIFO_CFG_WRVALIDSEL BIT(24) /* RW */
296 #define SDC_FIFO_CFG_RDVALIDSEL BIT(25) /* RW */
299 #define CQHCI_RD_CMD_WND_SEL BIT(14) /* RW */
300 #define CQHCI_WR_CMD_WND_SEL BIT(15) /* RW */
302 /* EMMC_TOP_CONTROL mask */
303 #define PAD_RXDLY_SEL BIT(0) /* RW */
304 #define DELAY_EN BIT(1) /* RW */
305 #define PAD_DAT_RD_RXDLY2 GENMASK(6, 2) /* RW */
306 #define PAD_DAT_RD_RXDLY GENMASK(11, 7) /* RW */
307 #define PAD_DAT_RD_RXDLY2_SEL BIT(12) /* RW */
308 #define PAD_DAT_RD_RXDLY_SEL BIT(13) /* RW */
309 #define DATA_K_VALUE_SEL BIT(14) /* RW */
310 #define SDC_RX_ENH_EN BIT(15) /* TW */
312 /* EMMC_TOP_CMD mask */
313 #define PAD_CMD_RXDLY2 GENMASK(4, 0) /* RW */
314 #define PAD_CMD_RXDLY GENMASK(9, 5) /* RW */
315 #define PAD_CMD_RD_RXDLY2_SEL BIT(10) /* RW */
316 #define PAD_CMD_RD_RXDLY_SEL BIT(11) /* RW */
317 #define PAD_CMD_TX_DLY GENMASK(16, 12) /* RW */
319 /* EMMC50_PAD_DS_TUNE mask */
320 #define PAD_DS_DLY_SEL BIT(16) /* RW */
321 #define PAD_DS_DLY1 GENMASK(14, 10) /* RW */
322 #define PAD_DS_DLY3 GENMASK(4, 0) /* RW */
324 /* LOOP_TEST_CONTROL mask */
325 #define TEST_LOOP_DSCLK_MUX_SEL BIT(0) /* RW */
326 #define TEST_LOOP_LATCH_MUX_SEL BIT(1) /* RW */
327 #define LOOP_EN_SEL_CLK BIT(20) /* RW */
328 #define TEST_HS400_CMD_LOOP_MUX_SEL BIT(31) /* RW */
330 #define REQ_CMD_EIO BIT(0)
331 #define REQ_CMD_TMO BIT(1)
332 #define REQ_DAT_ERR BIT(2)
333 #define REQ_STOP_EIO BIT(3)
334 #define REQ_STOP_TMO BIT(4)
335 #define REQ_CMD_BUSY BIT(5)
337 #define MSDC_PREPARE_FLAG BIT(0)
338 #define MSDC_ASYNC_FLAG BIT(1)
339 #define MSDC_MMAP_FLAG BIT(2)
341 #define MTK_MMC_AUTOSUSPEND_DELAY 50
342 #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
343 #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */
345 #define DEFAULT_DEBOUNCE (8) /* 8 cycles CD debounce */
347 #define TUNING_REG2_FIXED_OFFEST 4
348 #define PAD_DELAY_HALF 32 /* PAD delay cells */
349 #define PAD_DELAY_FULL 64
350 /*--------------------------------------------------------------------------*/
351 /* Descriptor Structure */
352 /*--------------------------------------------------------------------------*/
353 struct mt_gpdma_desc
{
355 #define GPDMA_DESC_HWO BIT(0)
356 #define GPDMA_DESC_BDP BIT(1)
357 #define GPDMA_DESC_CHECKSUM GENMASK(15, 8)
358 #define GPDMA_DESC_INT BIT(16)
359 #define GPDMA_DESC_NEXT_H4 GENMASK(27, 24)
360 #define GPDMA_DESC_PTR_H4 GENMASK(31, 28)
364 #define GPDMA_DESC_BUFLEN GENMASK(15, 0)
365 #define GPDMA_DESC_EXTLEN GENMASK(23, 16)
371 struct mt_bdma_desc
{
373 #define BDMA_DESC_EOL BIT(0)
374 #define BDMA_DESC_CHECKSUM GENMASK(15, 8)
375 #define BDMA_DESC_BLKPAD BIT(17)
376 #define BDMA_DESC_DWPAD BIT(18)
377 #define BDMA_DESC_NEXT_H4 GENMASK(27, 24)
378 #define BDMA_DESC_PTR_H4 GENMASK(31, 28)
382 #define BDMA_DESC_BUFLEN GENMASK(15, 0)
383 #define BDMA_DESC_BUFLEN_EXT GENMASK(23, 0)
387 struct scatterlist
*sg
; /* I/O scatter list */
388 struct mt_gpdma_desc
*gpd
; /* pointer to gpd array */
389 struct mt_bdma_desc
*bd
; /* pointer to bd array */
390 dma_addr_t gpd_addr
; /* the physical address of gpd array */
391 dma_addr_t bd_addr
; /* the physical address of bd array */
394 struct msdc_save_para
{
407 u32 emmc_top_control
;
409 u32 emmc50_pad_ds_tune
;
410 u32 loop_test_control
;
413 struct mtk_mmc_compatible
{
415 bool recheck_sdio_irq
;
416 bool hs400_tune
; /* only used for MT8173 */
426 bool use_internal_cd
;
431 struct msdc_tune_para
{
435 u32 emmc_top_control
;
439 struct msdc_delay_phase
{
447 const struct mtk_mmc_compatible
*dev_comp
;
451 struct mmc_request
*mrq
;
452 struct mmc_command
*cmd
;
453 struct mmc_data
*data
;
456 void __iomem
*base
; /* host base address */
457 void __iomem
*top_base
; /* host top register base address */
459 struct msdc_dma dma
; /* dma channel */
462 u32 timeout_ns
; /* data timeout ns */
463 u32 timeout_clks
; /* data timeout clks */
465 struct pinctrl
*pinctrl
;
466 struct pinctrl_state
*pins_default
;
467 struct pinctrl_state
*pins_uhs
;
468 struct pinctrl_state
*pins_eint
;
469 struct delayed_work req_timeout
;
470 int irq
; /* host interrupt */
471 int eint_irq
; /* interrupt from sdio device for waking up system */
472 struct reset_control
*reset
;
474 struct clk
*src_clk
; /* msdc source clock */
475 struct clk
*h_clk
; /* msdc h_clk */
476 struct clk
*bus_clk
; /* bus clock which used to access register */
477 struct clk
*src_clk_cg
; /* msdc source clock control gate */
478 struct clk
*sys_clk_cg
; /* msdc subsys clock control gate */
479 struct clk
*crypto_clk
; /* msdc crypto clock control gate */
480 struct clk_bulk_data bulk_clks
[MSDC_NR_CLOCKS
];
481 u32 mclk
; /* mmc subsystem clock frequency */
482 u32 src_clk_freq
; /* source clock frequency */
483 unsigned char timing
;
488 u32 hs200_cmd_int_delay
; /* cmd internal delay for HS200/SDR104 */
489 u32 hs400_cmd_int_delay
; /* cmd internal delay for HS400 */
491 bool hs400_cmd_resp_sel_rising
;
492 /* cmd response sample selection for HS400 */
493 bool hs400_mode
; /* current eMMC will run at hs400 mode */
494 bool hs400_tuning
; /* hs400 mode online tuning */
495 bool internal_cd
; /* Use internal card-detect logic */
496 bool cqhci
; /* support eMMC hw cmdq */
497 bool hsq_en
; /* Host Software Queue is enabled */
498 struct msdc_save_para save_para
; /* used when gate HCLK */
499 struct msdc_tune_para def_tune_para
; /* default tune setting */
500 struct msdc_tune_para saved_tune_para
; /* tune result of CMD21/CMD19 */
501 struct cqhci_host
*cq_host
;
505 static const struct mtk_mmc_compatible mt2701_compat
= {
507 .recheck_sdio_irq
= true,
509 .pad_tune_reg
= MSDC_PAD_TUNE0
,
513 .stop_clk_fix
= false,
515 .support_64g
= false,
518 static const struct mtk_mmc_compatible mt2712_compat
= {
520 .recheck_sdio_irq
= false,
522 .pad_tune_reg
= MSDC_PAD_TUNE0
,
526 .stop_clk_fix
= true,
532 static const struct mtk_mmc_compatible mt6779_compat
= {
534 .recheck_sdio_irq
= false,
536 .pad_tune_reg
= MSDC_PAD_TUNE0
,
540 .stop_clk_fix
= true,
546 static const struct mtk_mmc_compatible mt6795_compat
= {
548 .recheck_sdio_irq
= false,
550 .pad_tune_reg
= MSDC_PAD_TUNE
,
554 .stop_clk_fix
= false,
556 .support_64g
= false,
559 static const struct mtk_mmc_compatible mt7620_compat
= {
561 .recheck_sdio_irq
= true,
563 .pad_tune_reg
= MSDC_PAD_TUNE
,
567 .stop_clk_fix
= false,
569 .use_internal_cd
= true,
572 static const struct mtk_mmc_compatible mt7622_compat
= {
574 .recheck_sdio_irq
= true,
576 .pad_tune_reg
= MSDC_PAD_TUNE0
,
580 .stop_clk_fix
= true,
583 .support_64g
= false,
586 static const struct mtk_mmc_compatible mt7986_compat
= {
588 .recheck_sdio_irq
= true,
590 .pad_tune_reg
= MSDC_PAD_TUNE0
,
594 .stop_clk_fix
= true,
600 static const struct mtk_mmc_compatible mt8135_compat
= {
602 .recheck_sdio_irq
= true,
604 .pad_tune_reg
= MSDC_PAD_TUNE
,
608 .stop_clk_fix
= false,
610 .support_64g
= false,
613 static const struct mtk_mmc_compatible mt8173_compat
= {
615 .recheck_sdio_irq
= true,
617 .pad_tune_reg
= MSDC_PAD_TUNE
,
621 .stop_clk_fix
= false,
623 .support_64g
= false,
626 static const struct mtk_mmc_compatible mt8183_compat
= {
628 .recheck_sdio_irq
= false,
630 .pad_tune_reg
= MSDC_PAD_TUNE0
,
634 .stop_clk_fix
= true,
640 static const struct mtk_mmc_compatible mt8516_compat
= {
642 .recheck_sdio_irq
= true,
644 .pad_tune_reg
= MSDC_PAD_TUNE0
,
648 .stop_clk_fix
= true,
652 static const struct mtk_mmc_compatible mt8196_compat
= {
654 .recheck_sdio_irq
= false,
656 .pad_tune_reg
= MSDC_PAD_TUNE0
,
660 .stop_clk_fix
= true,
665 .support_new_tx
= true,
666 .support_new_rx
= true,
669 static const struct of_device_id msdc_of_ids
[] = {
670 { .compatible
= "mediatek,mt2701-mmc", .data
= &mt2701_compat
},
671 { .compatible
= "mediatek,mt2712-mmc", .data
= &mt2712_compat
},
672 { .compatible
= "mediatek,mt6779-mmc", .data
= &mt6779_compat
},
673 { .compatible
= "mediatek,mt6795-mmc", .data
= &mt6795_compat
},
674 { .compatible
= "mediatek,mt7620-mmc", .data
= &mt7620_compat
},
675 { .compatible
= "mediatek,mt7622-mmc", .data
= &mt7622_compat
},
676 { .compatible
= "mediatek,mt7986-mmc", .data
= &mt7986_compat
},
677 { .compatible
= "mediatek,mt7988-mmc", .data
= &mt7986_compat
},
678 { .compatible
= "mediatek,mt8135-mmc", .data
= &mt8135_compat
},
679 { .compatible
= "mediatek,mt8173-mmc", .data
= &mt8173_compat
},
680 { .compatible
= "mediatek,mt8183-mmc", .data
= &mt8183_compat
},
681 { .compatible
= "mediatek,mt8196-mmc", .data
= &mt8196_compat
},
682 { .compatible
= "mediatek,mt8516-mmc", .data
= &mt8516_compat
},
686 MODULE_DEVICE_TABLE(of
, msdc_of_ids
);
688 static void sdr_set_bits(void __iomem
*reg
, u32 bs
)
690 u32 val
= readl(reg
);
696 static void sdr_clr_bits(void __iomem
*reg
, u32 bs
)
698 u32 val
= readl(reg
);
704 static void sdr_set_field(void __iomem
*reg
, u32 field
, u32 val
)
706 unsigned int tv
= readl(reg
);
709 tv
|= ((val
) << (ffs((unsigned int)field
) - 1));
713 static void sdr_get_field(void __iomem
*reg
, u32 field
, u32
*val
)
715 unsigned int tv
= readl(reg
);
717 *val
= ((tv
& field
) >> (ffs((unsigned int)field
) - 1));
720 static void msdc_reset_hw(struct msdc_host
*host
)
724 sdr_set_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_RST
);
725 readl_poll_timeout_atomic(host
->base
+ MSDC_CFG
, val
, !(val
& MSDC_CFG_RST
), 0, 0);
727 sdr_set_bits(host
->base
+ MSDC_FIFOCS
, MSDC_FIFOCS_CLR
);
728 readl_poll_timeout_atomic(host
->base
+ MSDC_FIFOCS
, val
,
729 !(val
& MSDC_FIFOCS_CLR
), 0, 0);
731 val
= readl(host
->base
+ MSDC_INT
);
732 writel(val
, host
->base
+ MSDC_INT
);
735 static void msdc_cmd_next(struct msdc_host
*host
,
736 struct mmc_request
*mrq
, struct mmc_command
*cmd
);
737 static void __msdc_enable_sdio_irq(struct msdc_host
*host
, int enb
);
739 static const u32 cmd_ints_mask
= MSDC_INTEN_CMDRDY
| MSDC_INTEN_RSPCRCERR
|
740 MSDC_INTEN_CMDTMO
| MSDC_INTEN_ACMDRDY
|
741 MSDC_INTEN_ACMDCRCERR
| MSDC_INTEN_ACMDTMO
;
742 static const u32 data_ints_mask
= MSDC_INTEN_XFER_COMPL
| MSDC_INTEN_DATTMO
|
743 MSDC_INTEN_DATCRCERR
| MSDC_INTEN_DMA_BDCSERR
|
744 MSDC_INTEN_DMA_GPDCSERR
| MSDC_INTEN_DMA_PROTECT
;
746 static u8
msdc_dma_calcs(u8
*buf
, u32 len
)
750 for (i
= 0; i
< len
; i
++)
752 return 0xff - (u8
) sum
;
755 static inline void msdc_dma_setup(struct msdc_host
*host
, struct msdc_dma
*dma
,
756 struct mmc_data
*data
)
758 unsigned int j
, dma_len
;
759 dma_addr_t dma_address
;
761 struct scatterlist
*sg
;
762 struct mt_gpdma_desc
*gpd
;
763 struct mt_bdma_desc
*bd
;
771 gpd
->gpd_info
|= GPDMA_DESC_HWO
;
772 gpd
->gpd_info
|= GPDMA_DESC_BDP
;
773 /* need to clear first. use these bits to calc checksum */
774 gpd
->gpd_info
&= ~GPDMA_DESC_CHECKSUM
;
775 gpd
->gpd_info
|= msdc_dma_calcs((u8
*) gpd
, 16) << 8;
778 for_each_sg(data
->sg
, sg
, data
->sg_count
, j
) {
779 dma_address
= sg_dma_address(sg
);
780 dma_len
= sg_dma_len(sg
);
783 bd
[j
].bd_info
&= ~BDMA_DESC_BLKPAD
;
784 bd
[j
].bd_info
&= ~BDMA_DESC_DWPAD
;
785 bd
[j
].ptr
= lower_32_bits(dma_address
);
786 if (host
->dev_comp
->support_64g
) {
787 bd
[j
].bd_info
&= ~BDMA_DESC_PTR_H4
;
788 bd
[j
].bd_info
|= (upper_32_bits(dma_address
) & 0xf)
792 if (host
->dev_comp
->support_64g
) {
793 bd
[j
].bd_data_len
&= ~BDMA_DESC_BUFLEN_EXT
;
794 bd
[j
].bd_data_len
|= (dma_len
& BDMA_DESC_BUFLEN_EXT
);
796 bd
[j
].bd_data_len
&= ~BDMA_DESC_BUFLEN
;
797 bd
[j
].bd_data_len
|= (dma_len
& BDMA_DESC_BUFLEN
);
800 if (j
== data
->sg_count
- 1) /* the last bd */
801 bd
[j
].bd_info
|= BDMA_DESC_EOL
;
803 bd
[j
].bd_info
&= ~BDMA_DESC_EOL
;
805 /* checksum need to clear first */
806 bd
[j
].bd_info
&= ~BDMA_DESC_CHECKSUM
;
807 bd
[j
].bd_info
|= msdc_dma_calcs((u8
*)(&bd
[j
]), 16) << 8;
810 sdr_set_field(host
->base
+ MSDC_DMA_CFG
, MSDC_DMA_CFG_DECSEN
, 1);
811 dma_ctrl
= readl_relaxed(host
->base
+ MSDC_DMA_CTRL
);
812 dma_ctrl
&= ~(MSDC_DMA_CTRL_BRUSTSZ
| MSDC_DMA_CTRL_MODE
);
813 dma_ctrl
|= (MSDC_BURST_64B
<< 12 | BIT(8));
814 writel_relaxed(dma_ctrl
, host
->base
+ MSDC_DMA_CTRL
);
815 if (host
->dev_comp
->support_64g
)
816 sdr_set_field(host
->base
+ DMA_SA_H4BIT
, DMA_ADDR_HIGH_4BIT
,
817 upper_32_bits(dma
->gpd_addr
) & 0xf);
818 writel(lower_32_bits(dma
->gpd_addr
), host
->base
+ MSDC_DMA_SA
);
821 static void msdc_prepare_data(struct msdc_host
*host
, struct mmc_data
*data
)
823 if (!(data
->host_cookie
& MSDC_PREPARE_FLAG
)) {
824 data
->host_cookie
|= MSDC_PREPARE_FLAG
;
825 data
->sg_count
= dma_map_sg(host
->dev
, data
->sg
, data
->sg_len
,
826 mmc_get_dma_dir(data
));
830 static void msdc_unprepare_data(struct msdc_host
*host
, struct mmc_data
*data
)
832 if (data
->host_cookie
& MSDC_ASYNC_FLAG
)
835 if (data
->host_cookie
& MSDC_PREPARE_FLAG
) {
836 dma_unmap_sg(host
->dev
, data
->sg
, data
->sg_len
,
837 mmc_get_dma_dir(data
));
838 data
->host_cookie
&= ~MSDC_PREPARE_FLAG
;
842 static u64
msdc_timeout_cal(struct msdc_host
*host
, u64 ns
, u64 clks
)
844 struct mmc_host
*mmc
= mmc_from_priv(host
);
846 u32 clk_ns
, mode
= 0;
848 if (mmc
->actual_clock
== 0) {
851 clk_ns
= 1000000000U / mmc
->actual_clock
;
852 timeout
= ns
+ clk_ns
- 1;
853 do_div(timeout
, clk_ns
);
855 /* in 1048576 sclk cycle unit */
856 timeout
= DIV_ROUND_UP(timeout
, BIT(20));
857 if (host
->dev_comp
->clk_div_bits
== 8)
858 sdr_get_field(host
->base
+ MSDC_CFG
,
859 MSDC_CFG_CKMOD
, &mode
);
861 sdr_get_field(host
->base
+ MSDC_CFG
,
862 MSDC_CFG_CKMOD_EXTRA
, &mode
);
863 /*DDR mode will double the clk cycles for data timeout */
864 timeout
= mode
>= 2 ? timeout
* 2 : timeout
;
865 timeout
= timeout
> 1 ? timeout
- 1 : 0;
870 /* clock control primitives */
871 static void msdc_set_timeout(struct msdc_host
*host
, u64 ns
, u64 clks
)
875 host
->timeout_ns
= ns
;
876 host
->timeout_clks
= clks
;
878 timeout
= msdc_timeout_cal(host
, ns
, clks
);
879 sdr_set_field(host
->base
+ SDC_CFG
, SDC_CFG_DTOC
,
880 min_t(u32
, timeout
, 255));
883 static void msdc_set_busy_timeout(struct msdc_host
*host
, u64 ns
, u64 clks
)
887 timeout
= msdc_timeout_cal(host
, ns
, clks
);
888 sdr_set_field(host
->base
+ SDC_CFG
, SDC_CFG_WRDTOC
,
889 min_t(u32
, timeout
, 8191));
892 static void msdc_gate_clock(struct msdc_host
*host
)
894 clk_bulk_disable_unprepare(MSDC_NR_CLOCKS
, host
->bulk_clks
);
895 clk_disable_unprepare(host
->crypto_clk
);
896 clk_disable_unprepare(host
->src_clk_cg
);
897 clk_disable_unprepare(host
->src_clk
);
898 clk_disable_unprepare(host
->bus_clk
);
899 clk_disable_unprepare(host
->h_clk
);
902 static int msdc_ungate_clock(struct msdc_host
*host
)
907 clk_prepare_enable(host
->h_clk
);
908 clk_prepare_enable(host
->bus_clk
);
909 clk_prepare_enable(host
->src_clk
);
910 clk_prepare_enable(host
->src_clk_cg
);
911 clk_prepare_enable(host
->crypto_clk
);
912 ret
= clk_bulk_prepare_enable(MSDC_NR_CLOCKS
, host
->bulk_clks
);
914 dev_err(host
->dev
, "Cannot enable pclk/axi/ahb clock gates\n");
918 return readl_poll_timeout(host
->base
+ MSDC_CFG
, val
,
919 (val
& MSDC_CFG_CKSTB
), 1, 20000);
922 static void msdc_new_tx_setting(struct msdc_host
*host
)
927 sdr_set_bits(host
->top_base
+ LOOP_TEST_CONTROL
,
928 TEST_LOOP_DSCLK_MUX_SEL
);
929 sdr_set_bits(host
->top_base
+ LOOP_TEST_CONTROL
,
930 TEST_LOOP_LATCH_MUX_SEL
);
931 sdr_clr_bits(host
->top_base
+ LOOP_TEST_CONTROL
,
932 TEST_HS400_CMD_LOOP_MUX_SEL
);
934 switch (host
->timing
) {
935 case MMC_TIMING_LEGACY
:
936 case MMC_TIMING_MMC_HS
:
937 case MMC_TIMING_SD_HS
:
938 case MMC_TIMING_UHS_SDR12
:
939 case MMC_TIMING_UHS_SDR25
:
940 case MMC_TIMING_UHS_DDR50
:
941 case MMC_TIMING_MMC_DDR52
:
942 sdr_clr_bits(host
->top_base
+ LOOP_TEST_CONTROL
,
945 case MMC_TIMING_UHS_SDR50
:
946 case MMC_TIMING_UHS_SDR104
:
947 case MMC_TIMING_MMC_HS200
:
948 case MMC_TIMING_MMC_HS400
:
949 sdr_set_bits(host
->top_base
+ LOOP_TEST_CONTROL
,
957 static void msdc_set_mclk(struct msdc_host
*host
, unsigned char timing
, u32 hz
)
959 struct mmc_host
*mmc
= mmc_from_priv(host
);
964 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
969 dev_dbg(host
->dev
, "set mclk to 0\n");
971 mmc
->actual_clock
= 0;
972 sdr_clr_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_CKPDN
);
976 if (host
->timing
!= timing
)
977 timing_changed
= true;
979 timing_changed
= false;
981 flags
= readl(host
->base
+ MSDC_INTEN
);
982 sdr_clr_bits(host
->base
+ MSDC_INTEN
, flags
);
983 if (host
->dev_comp
->clk_div_bits
== 8)
984 sdr_clr_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_HS400_CK_MODE
);
986 sdr_clr_bits(host
->base
+ MSDC_CFG
,
987 MSDC_CFG_HS400_CK_MODE_EXTRA
);
988 if (timing
== MMC_TIMING_UHS_DDR50
||
989 timing
== MMC_TIMING_MMC_DDR52
||
990 timing
== MMC_TIMING_MMC_HS400
) {
991 if (timing
== MMC_TIMING_MMC_HS400
)
994 mode
= 0x2; /* ddr mode and use divisor */
996 if (hz
>= (host
->src_clk_freq
>> 2)) {
997 div
= 0; /* mean div = 1/4 */
998 sclk
= host
->src_clk_freq
>> 2; /* sclk = clk / 4 */
1000 div
= (host
->src_clk_freq
+ ((hz
<< 2) - 1)) / (hz
<< 2);
1001 sclk
= (host
->src_clk_freq
>> 2) / div
;
1005 if (timing
== MMC_TIMING_MMC_HS400
&&
1006 hz
>= (host
->src_clk_freq
>> 1)) {
1007 if (host
->dev_comp
->clk_div_bits
== 8)
1008 sdr_set_bits(host
->base
+ MSDC_CFG
,
1009 MSDC_CFG_HS400_CK_MODE
);
1011 sdr_set_bits(host
->base
+ MSDC_CFG
,
1012 MSDC_CFG_HS400_CK_MODE_EXTRA
);
1013 sclk
= host
->src_clk_freq
>> 1;
1014 div
= 0; /* div is ignore when bit18 is set */
1016 } else if (hz
>= host
->src_clk_freq
) {
1017 mode
= 0x1; /* no divisor */
1019 sclk
= host
->src_clk_freq
;
1021 mode
= 0x0; /* use divisor */
1022 if (hz
>= (host
->src_clk_freq
>> 1)) {
1023 div
= 0; /* mean div = 1/2 */
1024 sclk
= host
->src_clk_freq
>> 1; /* sclk = clk / 2 */
1026 div
= (host
->src_clk_freq
+ ((hz
<< 2) - 1)) / (hz
<< 2);
1027 sclk
= (host
->src_clk_freq
>> 2) / div
;
1030 sdr_clr_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_CKPDN
);
1032 clk_disable_unprepare(host
->src_clk_cg
);
1033 if (host
->dev_comp
->clk_div_bits
== 8)
1034 sdr_set_field(host
->base
+ MSDC_CFG
,
1035 MSDC_CFG_CKMOD
| MSDC_CFG_CKDIV
,
1038 sdr_set_field(host
->base
+ MSDC_CFG
,
1039 MSDC_CFG_CKMOD_EXTRA
| MSDC_CFG_CKDIV_EXTRA
,
1040 (mode
<< 12) | div
);
1042 clk_prepare_enable(host
->src_clk_cg
);
1043 readl_poll_timeout(host
->base
+ MSDC_CFG
, val
, (val
& MSDC_CFG_CKSTB
), 0, 0);
1044 sdr_set_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_CKPDN
);
1045 mmc
->actual_clock
= sclk
;
1047 host
->timing
= timing
;
1048 /* need because clk changed. */
1049 msdc_set_timeout(host
, host
->timeout_ns
, host
->timeout_clks
);
1050 sdr_set_bits(host
->base
+ MSDC_INTEN
, flags
);
1053 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
1054 * tune result of hs200/200Mhz is not suitable for 50Mhz
1056 if (mmc
->actual_clock
<= 52000000) {
1057 writel(host
->def_tune_para
.iocon
, host
->base
+ MSDC_IOCON
);
1058 if (host
->top_base
) {
1059 writel(host
->def_tune_para
.emmc_top_control
,
1060 host
->top_base
+ EMMC_TOP_CONTROL
);
1061 writel(host
->def_tune_para
.emmc_top_cmd
,
1062 host
->top_base
+ EMMC_TOP_CMD
);
1064 writel(host
->def_tune_para
.pad_tune
,
1065 host
->base
+ tune_reg
);
1068 writel(host
->saved_tune_para
.iocon
, host
->base
+ MSDC_IOCON
);
1069 writel(host
->saved_tune_para
.pad_cmd_tune
,
1070 host
->base
+ PAD_CMD_TUNE
);
1071 if (host
->top_base
) {
1072 writel(host
->saved_tune_para
.emmc_top_control
,
1073 host
->top_base
+ EMMC_TOP_CONTROL
);
1074 writel(host
->saved_tune_para
.emmc_top_cmd
,
1075 host
->top_base
+ EMMC_TOP_CMD
);
1077 writel(host
->saved_tune_para
.pad_tune
,
1078 host
->base
+ tune_reg
);
1082 if (timing
== MMC_TIMING_MMC_HS400
&&
1083 host
->dev_comp
->hs400_tune
)
1084 sdr_set_field(host
->base
+ tune_reg
,
1085 MSDC_PAD_TUNE_CMDRRDLY
,
1086 host
->hs400_cmd_int_delay
);
1087 if (host
->dev_comp
->support_new_tx
&& timing_changed
)
1088 msdc_new_tx_setting(host
);
1090 dev_dbg(host
->dev
, "sclk: %d, timing: %d\n", mmc
->actual_clock
,
1094 static inline u32
msdc_cmd_find_resp(struct msdc_host
*host
,
1095 struct mmc_command
*cmd
)
1099 switch (mmc_resp_type(cmd
)) {
1100 /* Actually, R1, R5, R6, R7 are the same */
1122 static inline u32
msdc_cmd_prepare_raw_cmd(struct msdc_host
*host
,
1123 struct mmc_request
*mrq
, struct mmc_command
*cmd
)
1125 struct mmc_host
*mmc
= mmc_from_priv(host
);
1127 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
1128 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
1130 u32 opcode
= cmd
->opcode
;
1131 u32 resp
= msdc_cmd_find_resp(host
, cmd
);
1132 u32 rawcmd
= (opcode
& 0x3f) | ((resp
& 0x7) << 7);
1134 host
->cmd_rsp
= resp
;
1136 if ((opcode
== SD_IO_RW_DIRECT
&& cmd
->flags
== (unsigned int) -1) ||
1137 opcode
== MMC_STOP_TRANSMISSION
)
1139 else if (opcode
== SD_SWITCH_VOLTAGE
)
1141 else if (opcode
== SD_APP_SEND_SCR
||
1142 opcode
== SD_APP_SEND_NUM_WR_BLKS
||
1143 (opcode
== SD_SWITCH
&& mmc_cmd_type(cmd
) == MMC_CMD_ADTC
) ||
1144 (opcode
== SD_APP_SD_STATUS
&& mmc_cmd_type(cmd
) == MMC_CMD_ADTC
) ||
1145 (opcode
== MMC_SEND_EXT_CSD
&& mmc_cmd_type(cmd
) == MMC_CMD_ADTC
))
1149 struct mmc_data
*data
= cmd
->data
;
1151 if (mmc_op_multi(opcode
)) {
1152 if (mmc_card_mmc(mmc
->card
) && mrq
->sbc
&&
1153 !(mrq
->sbc
->arg
& 0xFFFF0000))
1154 rawcmd
|= BIT(29); /* AutoCMD23 */
1157 rawcmd
|= ((data
->blksz
& 0xFFF) << 16);
1158 if (data
->flags
& MMC_DATA_WRITE
)
1160 if (data
->blocks
> 1)
1164 /* Always use dma mode */
1165 sdr_clr_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_PIO
);
1167 if (host
->timeout_ns
!= data
->timeout_ns
||
1168 host
->timeout_clks
!= data
->timeout_clks
)
1169 msdc_set_timeout(host
, data
->timeout_ns
,
1170 data
->timeout_clks
);
1172 writel(data
->blocks
, host
->base
+ SDC_BLK_NUM
);
1177 static void msdc_start_data(struct msdc_host
*host
, struct mmc_command
*cmd
,
1178 struct mmc_data
*data
)
1182 WARN_ON(host
->data
);
1184 read
= data
->flags
& MMC_DATA_READ
;
1186 mod_delayed_work(system_wq
, &host
->req_timeout
, DAT_TIMEOUT
);
1187 msdc_dma_setup(host
, &host
->dma
, data
);
1188 sdr_set_bits(host
->base
+ MSDC_INTEN
, data_ints_mask
);
1189 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_START
, 1);
1190 dev_dbg(host
->dev
, "DMA start\n");
1191 dev_dbg(host
->dev
, "%s: cmd=%d DMA data: %d blocks; read=%d\n",
1192 __func__
, cmd
->opcode
, data
->blocks
, read
);
1195 static int msdc_auto_cmd_done(struct msdc_host
*host
, int events
,
1196 struct mmc_command
*cmd
)
1198 u32
*rsp
= cmd
->resp
;
1200 rsp
[0] = readl(host
->base
+ SDC_ACMD_RESP
);
1202 if (events
& MSDC_INT_ACMDRDY
) {
1205 msdc_reset_hw(host
);
1206 if (events
& MSDC_INT_ACMDCRCERR
) {
1207 cmd
->error
= -EILSEQ
;
1208 host
->error
|= REQ_STOP_EIO
;
1209 } else if (events
& MSDC_INT_ACMDTMO
) {
1210 cmd
->error
= -ETIMEDOUT
;
1211 host
->error
|= REQ_STOP_TMO
;
1214 "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n",
1215 __func__
, cmd
->opcode
, cmd
->arg
, rsp
[0], cmd
->error
);
1221 * msdc_recheck_sdio_irq - recheck whether the SDIO irq is lost
1223 * Host controller may lost interrupt in some special case.
1224 * Add SDIO irq recheck mechanism to make sure all interrupts
1225 * can be processed immediately
1227 static void msdc_recheck_sdio_irq(struct msdc_host
*host
)
1229 struct mmc_host
*mmc
= mmc_from_priv(host
);
1230 u32 reg_int
, reg_inten
, reg_ps
;
1232 if (mmc
->caps
& MMC_CAP_SDIO_IRQ
) {
1233 reg_inten
= readl(host
->base
+ MSDC_INTEN
);
1234 if (reg_inten
& MSDC_INTEN_SDIOIRQ
) {
1235 reg_int
= readl(host
->base
+ MSDC_INT
);
1236 reg_ps
= readl(host
->base
+ MSDC_PS
);
1237 if (!(reg_int
& MSDC_INT_SDIOIRQ
||
1238 reg_ps
& MSDC_PS_DATA1
)) {
1239 __msdc_enable_sdio_irq(host
, 0);
1240 sdio_signal_irq(mmc
);
1246 static void msdc_track_cmd_data(struct msdc_host
*host
, struct mmc_command
*cmd
)
1249 ((!mmc_op_tuning(cmd
->opcode
) && !host
->hs400_tuning
) ||
1250 cmd
->error
== -ETIMEDOUT
))
1251 dev_warn(host
->dev
, "%s: cmd=%d arg=%08X; host->error=0x%08X\n",
1252 __func__
, cmd
->opcode
, cmd
->arg
, host
->error
);
1255 static void msdc_request_done(struct msdc_host
*host
, struct mmc_request
*mrq
)
1257 struct mmc_host
*mmc
= mmc_from_priv(host
);
1258 unsigned long flags
;
1262 * No need check the return value of cancel_delayed_work, as only ONE
1263 * path will go here!
1265 cancel_delayed_work(&host
->req_timeout
);
1268 * If the request was handled from Host Software Queue, there's almost
1269 * nothing to do here, and we also don't need to reset mrq as any race
1270 * condition would not have any room to happen, since HSQ stores the
1271 * "scheduled" mrqs in an internal array of mrq slots anyway.
1272 * However, if the controller experienced an error, we still want to
1273 * reset it as soon as possible.
1275 * Note that non-HSQ requests will still be happening at times, even
1276 * though it is enabled, and that's what is going to reset host->mrq.
1277 * Also, msdc_unprepare_data() is going to be called by HSQ when needed
1278 * as HSQ request finalization will eventually call the .post_req()
1279 * callback of this driver which, in turn, unprepares the data.
1281 hsq_req_done
= host
->hsq_en
? mmc_hsq_finalize_request(mmc
, mrq
) : false;
1284 msdc_reset_hw(host
);
1288 spin_lock_irqsave(&host
->lock
, flags
);
1290 spin_unlock_irqrestore(&host
->lock
, flags
);
1292 msdc_track_cmd_data(host
, mrq
->cmd
);
1294 msdc_unprepare_data(host
, mrq
->data
);
1296 msdc_reset_hw(host
);
1297 mmc_request_done(mmc
, mrq
);
1298 if (host
->dev_comp
->recheck_sdio_irq
)
1299 msdc_recheck_sdio_irq(host
);
1302 /* returns true if command is fully handled; returns false otherwise */
1303 static bool msdc_cmd_done(struct msdc_host
*host
, int events
,
1304 struct mmc_request
*mrq
, struct mmc_command
*cmd
)
1308 unsigned long flags
;
1311 if (mrq
->sbc
&& cmd
== mrq
->cmd
&&
1312 (events
& (MSDC_INT_ACMDRDY
| MSDC_INT_ACMDCRCERR
1313 | MSDC_INT_ACMDTMO
)))
1314 msdc_auto_cmd_done(host
, events
, mrq
->sbc
);
1316 sbc_error
= mrq
->sbc
&& mrq
->sbc
->error
;
1318 if (!sbc_error
&& !(events
& (MSDC_INT_CMDRDY
1319 | MSDC_INT_RSPCRCERR
1320 | MSDC_INT_CMDTMO
)))
1323 spin_lock_irqsave(&host
->lock
, flags
);
1326 spin_unlock_irqrestore(&host
->lock
, flags
);
1332 sdr_clr_bits(host
->base
+ MSDC_INTEN
, cmd_ints_mask
);
1334 if (cmd
->flags
& MMC_RSP_PRESENT
) {
1335 if (cmd
->flags
& MMC_RSP_136
) {
1336 rsp
[0] = readl(host
->base
+ SDC_RESP3
);
1337 rsp
[1] = readl(host
->base
+ SDC_RESP2
);
1338 rsp
[2] = readl(host
->base
+ SDC_RESP1
);
1339 rsp
[3] = readl(host
->base
+ SDC_RESP0
);
1341 rsp
[0] = readl(host
->base
+ SDC_RESP0
);
1345 if (!sbc_error
&& !(events
& MSDC_INT_CMDRDY
)) {
1346 if ((events
& MSDC_INT_CMDTMO
&& !host
->hs400_tuning
) ||
1347 (!mmc_op_tuning(cmd
->opcode
) && !host
->hs400_tuning
))
1349 * should not clear fifo/interrupt as the tune data
1350 * may have already come when cmd19/cmd21 gets response
1353 msdc_reset_hw(host
);
1354 if (events
& MSDC_INT_RSPCRCERR
) {
1355 cmd
->error
= -EILSEQ
;
1356 host
->error
|= REQ_CMD_EIO
;
1357 } else if (events
& MSDC_INT_CMDTMO
) {
1358 cmd
->error
= -ETIMEDOUT
;
1359 host
->error
|= REQ_CMD_TMO
;
1364 "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n",
1365 __func__
, cmd
->opcode
, cmd
->arg
, rsp
[0],
1368 msdc_cmd_next(host
, mrq
, cmd
);
1372 /* It is the core layer's responsibility to ensure card status
1373 * is correct before issue a request. but host design do below
1374 * checks recommended.
1376 static inline bool msdc_cmd_is_ready(struct msdc_host
*host
,
1377 struct mmc_request
*mrq
, struct mmc_command
*cmd
)
1382 /* The max busy time we can endure is 20ms */
1383 ret
= readl_poll_timeout_atomic(host
->base
+ SDC_STS
, val
,
1384 !(val
& SDC_STS_CMDBUSY
), 1, 20000);
1386 dev_err(host
->dev
, "CMD bus busy detected\n");
1387 host
->error
|= REQ_CMD_BUSY
;
1388 msdc_cmd_done(host
, MSDC_INT_CMDTMO
, mrq
, cmd
);
1392 if (mmc_resp_type(cmd
) == MMC_RSP_R1B
|| cmd
->data
) {
1393 /* R1B or with data, should check SDCBUSY */
1394 ret
= readl_poll_timeout_atomic(host
->base
+ SDC_STS
, val
,
1395 !(val
& SDC_STS_SDCBUSY
), 1, 20000);
1397 dev_err(host
->dev
, "Controller busy detected\n");
1398 host
->error
|= REQ_CMD_BUSY
;
1399 msdc_cmd_done(host
, MSDC_INT_CMDTMO
, mrq
, cmd
);
1406 static void msdc_start_command(struct msdc_host
*host
,
1407 struct mmc_request
*mrq
, struct mmc_command
*cmd
)
1410 unsigned long flags
;
1415 mod_delayed_work(system_wq
, &host
->req_timeout
, DAT_TIMEOUT
);
1416 if (!msdc_cmd_is_ready(host
, mrq
, cmd
))
1419 if ((readl(host
->base
+ MSDC_FIFOCS
) & MSDC_FIFOCS_TXCNT
) >> 16 ||
1420 readl(host
->base
+ MSDC_FIFOCS
) & MSDC_FIFOCS_RXCNT
) {
1421 dev_err(host
->dev
, "TX/RX FIFO non-empty before start of IO. Reset\n");
1422 msdc_reset_hw(host
);
1426 rawcmd
= msdc_cmd_prepare_raw_cmd(host
, mrq
, cmd
);
1428 spin_lock_irqsave(&host
->lock
, flags
);
1429 sdr_set_bits(host
->base
+ MSDC_INTEN
, cmd_ints_mask
);
1430 spin_unlock_irqrestore(&host
->lock
, flags
);
1432 writel(cmd
->arg
, host
->base
+ SDC_ARG
);
1433 writel(rawcmd
, host
->base
+ SDC_CMD
);
1436 static void msdc_cmd_next(struct msdc_host
*host
,
1437 struct mmc_request
*mrq
, struct mmc_command
*cmd
)
1439 if ((cmd
->error
&& !host
->hs400_tuning
&&
1440 !(cmd
->error
== -EILSEQ
&&
1441 mmc_op_tuning(cmd
->opcode
))) ||
1442 (mrq
->sbc
&& mrq
->sbc
->error
))
1443 msdc_request_done(host
, mrq
);
1444 else if (cmd
== mrq
->sbc
)
1445 msdc_start_command(host
, mrq
, mrq
->cmd
);
1446 else if (!cmd
->data
)
1447 msdc_request_done(host
, mrq
);
1449 msdc_start_data(host
, cmd
, cmd
->data
);
1452 static void msdc_ops_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1454 struct msdc_host
*host
= mmc_priv(mmc
);
1457 WARN_ON(!host
->hsq_en
&& host
->mrq
);
1461 msdc_prepare_data(host
, mrq
->data
);
1463 /* if SBC is required, we have HW option and SW option.
1464 * if HW option is enabled, and SBC does not have "special" flags,
1465 * use HW option, otherwise use SW option
1467 if (mrq
->sbc
&& (!mmc_card_mmc(mmc
->card
) ||
1468 (mrq
->sbc
->arg
& 0xFFFF0000)))
1469 msdc_start_command(host
, mrq
, mrq
->sbc
);
1471 msdc_start_command(host
, mrq
, mrq
->cmd
);
1474 static void msdc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1476 struct msdc_host
*host
= mmc_priv(mmc
);
1477 struct mmc_data
*data
= mrq
->data
;
1482 msdc_prepare_data(host
, data
);
1483 data
->host_cookie
|= MSDC_ASYNC_FLAG
;
1486 static void msdc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
1489 struct msdc_host
*host
= mmc_priv(mmc
);
1490 struct mmc_data
*data
= mrq
->data
;
1495 if (data
->host_cookie
) {
1496 data
->host_cookie
&= ~MSDC_ASYNC_FLAG
;
1497 msdc_unprepare_data(host
, data
);
1501 static void msdc_data_xfer_next(struct msdc_host
*host
, struct mmc_request
*mrq
)
1503 if (mmc_op_multi(mrq
->cmd
->opcode
) && mrq
->stop
&& !mrq
->stop
->error
&&
1505 msdc_start_command(host
, mrq
, mrq
->stop
);
1507 msdc_request_done(host
, mrq
);
1510 static void msdc_data_xfer_done(struct msdc_host
*host
, u32 events
,
1511 struct mmc_request
*mrq
, struct mmc_data
*data
)
1513 struct mmc_command
*stop
;
1514 unsigned long flags
;
1516 unsigned int check_data
= events
&
1517 (MSDC_INT_XFER_COMPL
| MSDC_INT_DATCRCERR
| MSDC_INT_DATTMO
1518 | MSDC_INT_DMA_BDCSERR
| MSDC_INT_DMA_GPDCSERR
1519 | MSDC_INT_DMA_PROTECT
);
1523 spin_lock_irqsave(&host
->lock
, flags
);
1527 spin_unlock_irqrestore(&host
->lock
, flags
);
1533 if (check_data
|| (stop
&& stop
->error
)) {
1534 dev_dbg(host
->dev
, "DMA status: 0x%8X\n",
1535 readl(host
->base
+ MSDC_DMA_CFG
));
1536 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_STOP
,
1539 ret
= readl_poll_timeout_atomic(host
->base
+ MSDC_DMA_CTRL
, val
,
1540 !(val
& MSDC_DMA_CTRL_STOP
), 1, 20000);
1542 dev_dbg(host
->dev
, "DMA stop timed out\n");
1544 ret
= readl_poll_timeout_atomic(host
->base
+ MSDC_DMA_CFG
, val
,
1545 !(val
& MSDC_DMA_CFG_STS
), 1, 20000);
1547 dev_dbg(host
->dev
, "DMA inactive timed out\n");
1549 sdr_clr_bits(host
->base
+ MSDC_INTEN
, data_ints_mask
);
1550 dev_dbg(host
->dev
, "DMA stop\n");
1552 if ((events
& MSDC_INT_XFER_COMPL
) && (!stop
|| !stop
->error
)) {
1553 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
1555 dev_dbg(host
->dev
, "interrupt events: %x\n", events
);
1556 msdc_reset_hw(host
);
1557 host
->error
|= REQ_DAT_ERR
;
1558 data
->bytes_xfered
= 0;
1560 if (events
& MSDC_INT_DATTMO
)
1561 data
->error
= -ETIMEDOUT
;
1562 else if (events
& MSDC_INT_DATCRCERR
)
1563 data
->error
= -EILSEQ
;
1565 dev_dbg(host
->dev
, "%s: cmd=%d; blocks=%d",
1566 __func__
, mrq
->cmd
->opcode
, data
->blocks
);
1567 dev_dbg(host
->dev
, "data_error=%d xfer_size=%d\n",
1568 (int)data
->error
, data
->bytes_xfered
);
1571 msdc_data_xfer_next(host
, mrq
);
1575 static void msdc_set_buswidth(struct msdc_host
*host
, u32 width
)
1577 u32 val
= readl(host
->base
+ SDC_CFG
);
1579 val
&= ~SDC_CFG_BUSWIDTH
;
1583 case MMC_BUS_WIDTH_1
:
1584 val
|= (MSDC_BUS_1BITS
<< 16);
1586 case MMC_BUS_WIDTH_4
:
1587 val
|= (MSDC_BUS_4BITS
<< 16);
1589 case MMC_BUS_WIDTH_8
:
1590 val
|= (MSDC_BUS_8BITS
<< 16);
1594 writel(val
, host
->base
+ SDC_CFG
);
1595 dev_dbg(host
->dev
, "Bus Width = %d", width
);
1598 static int msdc_ops_switch_volt(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1600 struct msdc_host
*host
= mmc_priv(mmc
);
1603 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1604 if (ios
->signal_voltage
!= MMC_SIGNAL_VOLTAGE_330
&&
1605 ios
->signal_voltage
!= MMC_SIGNAL_VOLTAGE_180
) {
1606 dev_err(host
->dev
, "Unsupported signal voltage!\n");
1610 ret
= mmc_regulator_set_vqmmc(mmc
, ios
);
1612 dev_dbg(host
->dev
, "Regulator set error %d (%d)\n",
1613 ret
, ios
->signal_voltage
);
1617 /* Apply different pinctrl settings for different signal voltage */
1618 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_180
)
1619 pinctrl_select_state(host
->pinctrl
, host
->pins_uhs
);
1621 pinctrl_select_state(host
->pinctrl
, host
->pins_default
);
1626 static int msdc_card_busy(struct mmc_host
*mmc
)
1628 struct msdc_host
*host
= mmc_priv(mmc
);
1629 u32 status
= readl(host
->base
+ MSDC_PS
);
1631 /* only check if data0 is low */
1632 return !(status
& BIT(16));
1635 static void msdc_request_timeout(struct work_struct
*work
)
1637 struct msdc_host
*host
= container_of(work
, struct msdc_host
,
1640 /* simulate HW timeout status */
1641 dev_err(host
->dev
, "%s: aborting cmd/data/mrq\n", __func__
);
1643 dev_err(host
->dev
, "%s: aborting mrq=%p cmd=%d\n", __func__
,
1644 host
->mrq
, host
->mrq
->cmd
->opcode
);
1646 dev_err(host
->dev
, "%s: aborting cmd=%d\n",
1647 __func__
, host
->cmd
->opcode
);
1648 msdc_cmd_done(host
, MSDC_INT_CMDTMO
, host
->mrq
,
1650 } else if (host
->data
) {
1651 dev_err(host
->dev
, "%s: abort data: cmd%d; %d blocks\n",
1652 __func__
, host
->mrq
->cmd
->opcode
,
1653 host
->data
->blocks
);
1654 msdc_data_xfer_done(host
, MSDC_INT_DATTMO
, host
->mrq
,
1660 static void __msdc_enable_sdio_irq(struct msdc_host
*host
, int enb
)
1663 sdr_set_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_SDIOIRQ
);
1664 sdr_set_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIOIDE
);
1665 if (host
->dev_comp
->recheck_sdio_irq
)
1666 msdc_recheck_sdio_irq(host
);
1668 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_SDIOIRQ
);
1669 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIOIDE
);
1673 static void msdc_enable_sdio_irq(struct mmc_host
*mmc
, int enb
)
1675 struct msdc_host
*host
= mmc_priv(mmc
);
1676 unsigned long flags
;
1679 spin_lock_irqsave(&host
->lock
, flags
);
1680 __msdc_enable_sdio_irq(host
, enb
);
1681 spin_unlock_irqrestore(&host
->lock
, flags
);
1683 if (mmc_card_enable_async_irq(mmc
->card
) && host
->pins_eint
) {
1686 * In dev_pm_set_dedicated_wake_irq_reverse(), eint pin will be set to
1687 * GPIO mode. We need to restore it to SDIO DAT1 mode after that.
1688 * Since the current pinstate is pins_uhs, to ensure pinctrl select take
1689 * affect successfully, we change the pinstate to pins_eint firstly.
1691 pinctrl_select_state(host
->pinctrl
, host
->pins_eint
);
1692 ret
= dev_pm_set_dedicated_wake_irq_reverse(host
->dev
, host
->eint_irq
);
1695 dev_err(host
->dev
, "Failed to register SDIO wakeup irq!\n");
1696 host
->pins_eint
= NULL
;
1697 pm_runtime_get_noresume(host
->dev
);
1699 dev_dbg(host
->dev
, "SDIO eint irq: %d!\n", host
->eint_irq
);
1702 pinctrl_select_state(host
->pinctrl
, host
->pins_uhs
);
1704 dev_pm_clear_wake_irq(host
->dev
);
1708 /* Ensure host->pins_eint is NULL */
1709 host
->pins_eint
= NULL
;
1710 pm_runtime_get_noresume(host
->dev
);
1712 pm_runtime_put_noidle(host
->dev
);
1717 static irqreturn_t
msdc_cmdq_irq(struct msdc_host
*host
, u32 intsts
)
1719 struct mmc_host
*mmc
= mmc_from_priv(host
);
1720 int cmd_err
= 0, dat_err
= 0;
1722 if (intsts
& MSDC_INT_RSPCRCERR
) {
1724 dev_err(host
->dev
, "%s: CMD CRC ERR", __func__
);
1725 } else if (intsts
& MSDC_INT_CMDTMO
) {
1726 cmd_err
= -ETIMEDOUT
;
1727 dev_err(host
->dev
, "%s: CMD TIMEOUT ERR", __func__
);
1730 if (intsts
& MSDC_INT_DATCRCERR
) {
1732 dev_err(host
->dev
, "%s: DATA CRC ERR", __func__
);
1733 } else if (intsts
& MSDC_INT_DATTMO
) {
1734 dat_err
= -ETIMEDOUT
;
1735 dev_err(host
->dev
, "%s: DATA TIMEOUT ERR", __func__
);
1738 if (cmd_err
|| dat_err
) {
1739 dev_err(host
->dev
, "cmd_err = %d, dat_err = %d, intsts = 0x%x",
1740 cmd_err
, dat_err
, intsts
);
1743 return cqhci_irq(mmc
, 0, cmd_err
, dat_err
);
1746 static irqreturn_t
msdc_irq(int irq
, void *dev_id
)
1748 struct msdc_host
*host
= (struct msdc_host
*) dev_id
;
1749 struct mmc_host
*mmc
= mmc_from_priv(host
);
1752 struct mmc_request
*mrq
;
1753 struct mmc_command
*cmd
;
1754 struct mmc_data
*data
;
1755 u32 events
, event_mask
;
1757 spin_lock(&host
->lock
);
1758 events
= readl(host
->base
+ MSDC_INT
);
1759 event_mask
= readl(host
->base
+ MSDC_INTEN
);
1760 if ((events
& event_mask
) & MSDC_INT_SDIOIRQ
)
1761 __msdc_enable_sdio_irq(host
, 0);
1762 /* clear interrupts */
1763 writel(events
& event_mask
, host
->base
+ MSDC_INT
);
1768 spin_unlock(&host
->lock
);
1770 if ((events
& event_mask
) & MSDC_INT_SDIOIRQ
)
1771 sdio_signal_irq(mmc
);
1773 if ((events
& event_mask
) & MSDC_INT_CDSC
) {
1774 if (host
->internal_cd
)
1775 mmc_detect_change(mmc
, msecs_to_jiffies(20));
1776 events
&= ~MSDC_INT_CDSC
;
1779 if (!(events
& (event_mask
& ~MSDC_INT_SDIOIRQ
)))
1782 if ((mmc
->caps2
& MMC_CAP2_CQE
) &&
1783 (events
& MSDC_INT_CMDQ
)) {
1784 msdc_cmdq_irq(host
, events
);
1785 /* clear interrupts */
1786 writel(events
, host
->base
+ MSDC_INT
);
1792 "%s: MRQ=NULL; events=%08X; event_mask=%08X\n",
1793 __func__
, events
, event_mask
);
1798 dev_dbg(host
->dev
, "%s: events=%08X\n", __func__
, events
);
1801 msdc_cmd_done(host
, events
, mrq
, cmd
);
1803 msdc_data_xfer_done(host
, events
, mrq
, data
);
1809 static void msdc_init_hw(struct msdc_host
*host
)
1812 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
1813 struct mmc_host
*mmc
= mmc_from_priv(host
);
1816 reset_control_assert(host
->reset
);
1817 usleep_range(10, 50);
1818 reset_control_deassert(host
->reset
);
1821 /* New tx/rx enable bit need to be 0->1 for hardware check */
1822 if (host
->dev_comp
->support_new_tx
) {
1823 sdr_clr_bits(host
->base
+ SDC_ADV_CFG0
, SDC_NEW_TX_EN
);
1824 sdr_set_bits(host
->base
+ SDC_ADV_CFG0
, SDC_NEW_TX_EN
);
1825 msdc_new_tx_setting(host
);
1827 if (host
->dev_comp
->support_new_rx
) {
1828 sdr_clr_bits(host
->base
+ MSDC_NEW_RX_CFG
, MSDC_NEW_RX_PATH_SEL
);
1829 sdr_set_bits(host
->base
+ MSDC_NEW_RX_CFG
, MSDC_NEW_RX_PATH_SEL
);
1832 /* Configure to MMC/SD mode, clock free running */
1833 sdr_set_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_MODE
| MSDC_CFG_CKPDN
);
1836 msdc_reset_hw(host
);
1838 /* Disable and clear all interrupts */
1839 writel(0, host
->base
+ MSDC_INTEN
);
1840 val
= readl(host
->base
+ MSDC_INT
);
1841 writel(val
, host
->base
+ MSDC_INT
);
1843 /* Configure card detection */
1844 if (host
->internal_cd
) {
1845 sdr_set_field(host
->base
+ MSDC_PS
, MSDC_PS_CDDEBOUNCE
,
1847 sdr_set_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1848 sdr_set_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_CDSC
);
1849 sdr_set_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1851 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1852 sdr_clr_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1853 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_CDSC
);
1856 if (host
->top_base
) {
1857 writel(0, host
->top_base
+ EMMC_TOP_CONTROL
);
1858 writel(0, host
->top_base
+ EMMC_TOP_CMD
);
1860 writel(0, host
->base
+ tune_reg
);
1862 writel(0, host
->base
+ MSDC_IOCON
);
1863 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DDLSEL
, 0);
1864 writel(0x403c0046, host
->base
+ MSDC_PATCH_BIT
);
1865 sdr_set_field(host
->base
+ MSDC_PATCH_BIT
, MSDC_CKGEN_MSDC_DLY_SEL
, 1);
1866 writel(0xffff4089, host
->base
+ MSDC_PATCH_BIT1
);
1867 sdr_set_bits(host
->base
+ EMMC50_CFG0
, EMMC50_CFG_CFCSTS_SEL
);
1869 if (host
->dev_comp
->stop_clk_fix
) {
1870 if (host
->dev_comp
->stop_dly_sel
)
1871 sdr_set_field(host
->base
+ MSDC_PATCH_BIT1
,
1872 MSDC_PATCH_BIT1_STOP_DLY
,
1873 host
->dev_comp
->stop_dly_sel
);
1875 if (host
->dev_comp
->pop_en_cnt
)
1876 sdr_set_field(host
->base
+ MSDC_PATCH_BIT2
,
1877 MSDC_PB2_POP_EN_CNT
,
1878 host
->dev_comp
->pop_en_cnt
);
1880 sdr_clr_bits(host
->base
+ SDC_FIFO_CFG
,
1881 SDC_FIFO_CFG_WRVALIDSEL
);
1882 sdr_clr_bits(host
->base
+ SDC_FIFO_CFG
,
1883 SDC_FIFO_CFG_RDVALIDSEL
);
1886 if (host
->dev_comp
->busy_check
)
1887 sdr_clr_bits(host
->base
+ MSDC_PATCH_BIT1
, BIT(7));
1889 if (host
->dev_comp
->async_fifo
) {
1890 sdr_set_field(host
->base
+ MSDC_PATCH_BIT2
,
1891 MSDC_PB2_RESPWAIT
, 3);
1892 if (host
->dev_comp
->enhance_rx
) {
1894 sdr_set_bits(host
->top_base
+ EMMC_TOP_CONTROL
,
1897 sdr_set_bits(host
->base
+ SDC_ADV_CFG0
,
1900 sdr_set_field(host
->base
+ MSDC_PATCH_BIT2
,
1901 MSDC_PB2_RESPSTSENSEL
, 2);
1902 sdr_set_field(host
->base
+ MSDC_PATCH_BIT2
,
1903 MSDC_PB2_CRCSTSENSEL
, 2);
1905 /* use async fifo, then no need tune internal delay */
1906 sdr_clr_bits(host
->base
+ MSDC_PATCH_BIT2
,
1907 MSDC_PATCH_BIT2_CFGRESP
);
1908 sdr_set_bits(host
->base
+ MSDC_PATCH_BIT2
,
1909 MSDC_PATCH_BIT2_CFGCRCSTS
);
1912 if (host
->dev_comp
->support_64g
)
1913 sdr_set_bits(host
->base
+ MSDC_PATCH_BIT2
,
1914 MSDC_PB2_SUPPORT_64G
);
1915 if (host
->dev_comp
->data_tune
) {
1916 if (host
->top_base
) {
1917 sdr_set_bits(host
->top_base
+ EMMC_TOP_CONTROL
,
1918 PAD_DAT_RD_RXDLY_SEL
);
1919 sdr_clr_bits(host
->top_base
+ EMMC_TOP_CONTROL
,
1921 sdr_set_bits(host
->top_base
+ EMMC_TOP_CMD
,
1922 PAD_CMD_RD_RXDLY_SEL
);
1923 if (host
->tuning_step
> PAD_DELAY_HALF
) {
1924 sdr_set_bits(host
->top_base
+ EMMC_TOP_CONTROL
,
1925 PAD_DAT_RD_RXDLY2_SEL
);
1926 sdr_set_bits(host
->top_base
+ EMMC_TOP_CMD
,
1927 PAD_CMD_RD_RXDLY2_SEL
);
1930 sdr_set_bits(host
->base
+ tune_reg
,
1931 MSDC_PAD_TUNE_RD_SEL
|
1932 MSDC_PAD_TUNE_CMD_SEL
);
1933 if (host
->tuning_step
> PAD_DELAY_HALF
)
1934 sdr_set_bits(host
->base
+ tune_reg
+ TUNING_REG2_FIXED_OFFEST
,
1935 MSDC_PAD_TUNE_RD2_SEL
|
1936 MSDC_PAD_TUNE_CMD2_SEL
);
1939 /* choose clock tune */
1941 sdr_set_bits(host
->top_base
+ EMMC_TOP_CONTROL
,
1944 sdr_set_bits(host
->base
+ tune_reg
,
1945 MSDC_PAD_TUNE_RXDLYSEL
);
1948 if (mmc
->caps2
& MMC_CAP2_NO_SDIO
) {
1949 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIO
);
1950 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_SDIOIRQ
);
1951 sdr_clr_bits(host
->base
+ SDC_ADV_CFG0
, SDC_DAT1_IRQ_TRIGGER
);
1953 /* Configure to enable SDIO mode, otherwise SDIO CMD5 fails */
1954 sdr_set_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIO
);
1956 /* Config SDIO device detect interrupt function */
1957 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIOIDE
);
1958 sdr_set_bits(host
->base
+ SDC_ADV_CFG0
, SDC_DAT1_IRQ_TRIGGER
);
1961 /* Configure to default data timeout */
1962 sdr_set_field(host
->base
+ SDC_CFG
, SDC_CFG_DTOC
, 3);
1964 host
->def_tune_para
.iocon
= readl(host
->base
+ MSDC_IOCON
);
1965 host
->saved_tune_para
.iocon
= readl(host
->base
+ MSDC_IOCON
);
1966 if (host
->top_base
) {
1967 host
->def_tune_para
.emmc_top_control
=
1968 readl(host
->top_base
+ EMMC_TOP_CONTROL
);
1969 host
->def_tune_para
.emmc_top_cmd
=
1970 readl(host
->top_base
+ EMMC_TOP_CMD
);
1971 host
->saved_tune_para
.emmc_top_control
=
1972 readl(host
->top_base
+ EMMC_TOP_CONTROL
);
1973 host
->saved_tune_para
.emmc_top_cmd
=
1974 readl(host
->top_base
+ EMMC_TOP_CMD
);
1976 host
->def_tune_para
.pad_tune
= readl(host
->base
+ tune_reg
);
1977 host
->saved_tune_para
.pad_tune
= readl(host
->base
+ tune_reg
);
1979 dev_dbg(host
->dev
, "init hardware done!");
1982 static void msdc_deinit_hw(struct msdc_host
*host
)
1986 if (host
->internal_cd
) {
1987 /* Disabled card-detect */
1988 sdr_clr_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1989 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1992 /* Disable and clear all interrupts */
1993 writel(0, host
->base
+ MSDC_INTEN
);
1995 val
= readl(host
->base
+ MSDC_INT
);
1996 writel(val
, host
->base
+ MSDC_INT
);
1999 /* init gpd and bd list in msdc_drv_probe */
2000 static void msdc_init_gpd_bd(struct msdc_host
*host
, struct msdc_dma
*dma
)
2002 struct mt_gpdma_desc
*gpd
= dma
->gpd
;
2003 struct mt_bdma_desc
*bd
= dma
->bd
;
2004 dma_addr_t dma_addr
;
2007 memset(gpd
, 0, sizeof(struct mt_gpdma_desc
) * 2);
2009 dma_addr
= dma
->gpd_addr
+ sizeof(struct mt_gpdma_desc
);
2010 gpd
->gpd_info
= GPDMA_DESC_BDP
; /* hwo, cs, bd pointer */
2011 /* gpd->next is must set for desc DMA
2012 * That's why must alloc 2 gpd structure.
2014 gpd
->next
= lower_32_bits(dma_addr
);
2015 if (host
->dev_comp
->support_64g
)
2016 gpd
->gpd_info
|= (upper_32_bits(dma_addr
) & 0xf) << 24;
2018 dma_addr
= dma
->bd_addr
;
2019 gpd
->ptr
= lower_32_bits(dma
->bd_addr
); /* physical address */
2020 if (host
->dev_comp
->support_64g
)
2021 gpd
->gpd_info
|= (upper_32_bits(dma_addr
) & 0xf) << 28;
2023 memset(bd
, 0, sizeof(struct mt_bdma_desc
) * MAX_BD_NUM
);
2024 for (i
= 0; i
< (MAX_BD_NUM
- 1); i
++) {
2025 dma_addr
= dma
->bd_addr
+ sizeof(*bd
) * (i
+ 1);
2026 bd
[i
].next
= lower_32_bits(dma_addr
);
2027 if (host
->dev_comp
->support_64g
)
2028 bd
[i
].bd_info
|= (upper_32_bits(dma_addr
) & 0xf) << 24;
2032 static void msdc_ops_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
2034 struct msdc_host
*host
= mmc_priv(mmc
);
2037 msdc_set_buswidth(host
, ios
->bus_width
);
2039 /* Suspend/Resume will do power off/on */
2040 switch (ios
->power_mode
) {
2042 if (!IS_ERR(mmc
->supply
.vmmc
)) {
2044 ret
= mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
,
2047 dev_err(host
->dev
, "Failed to set vmmc power!\n");
2053 if (!IS_ERR(mmc
->supply
.vqmmc
) && !host
->vqmmc_enabled
) {
2054 ret
= regulator_enable(mmc
->supply
.vqmmc
);
2056 dev_err(host
->dev
, "Failed to set vqmmc power!\n");
2058 host
->vqmmc_enabled
= true;
2062 if (!IS_ERR(mmc
->supply
.vmmc
))
2063 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, 0);
2065 if (!IS_ERR(mmc
->supply
.vqmmc
) && host
->vqmmc_enabled
) {
2066 regulator_disable(mmc
->supply
.vqmmc
);
2067 host
->vqmmc_enabled
= false;
2074 if (host
->mclk
!= ios
->clock
|| host
->timing
!= ios
->timing
)
2075 msdc_set_mclk(host
, ios
->timing
, ios
->clock
);
2078 static u64
test_delay_bit(u64 delay
, u32 bit
)
2080 bit
%= PAD_DELAY_FULL
;
2081 return delay
& BIT_ULL(bit
);
2084 static int get_delay_len(u64 delay
, u32 start_bit
)
2088 for (i
= 0; i
< (PAD_DELAY_FULL
- start_bit
); i
++) {
2089 if (test_delay_bit(delay
, start_bit
+ i
) == 0)
2092 return PAD_DELAY_FULL
- start_bit
;
2095 static struct msdc_delay_phase
get_best_delay(struct msdc_host
*host
, u64 delay
)
2097 int start
= 0, len
= 0;
2098 int start_final
= 0, len_final
= 0;
2099 u8 final_phase
= 0xff;
2100 struct msdc_delay_phase delay_phase
= { 0, };
2103 dev_err(host
->dev
, "phase error: [map:%016llx]\n", delay
);
2104 delay_phase
.final_phase
= final_phase
;
2108 while (start
< PAD_DELAY_FULL
) {
2109 len
= get_delay_len(delay
, start
);
2110 if (len_final
< len
) {
2111 start_final
= start
;
2114 start
+= len
? len
: 1;
2115 if (!upper_32_bits(delay
) && len
>= 12 && start_final
< 4)
2119 /* The rule is that to find the smallest delay cell */
2120 if (start_final
== 0)
2121 final_phase
= (start_final
+ len_final
/ 3) % PAD_DELAY_FULL
;
2123 final_phase
= (start_final
+ len_final
/ 2) % PAD_DELAY_FULL
;
2124 dev_dbg(host
->dev
, "phase: [map:%016llx] [maxlen:%d] [final:%d]\n",
2125 delay
, len_final
, final_phase
);
2127 delay_phase
.maxlen
= len_final
;
2128 delay_phase
.start
= start_final
;
2129 delay_phase
.final_phase
= final_phase
;
2133 static inline void msdc_set_cmd_delay(struct msdc_host
*host
, u32 value
)
2135 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
2137 if (host
->top_base
) {
2138 if (value
< PAD_DELAY_HALF
) {
2139 sdr_set_field(host
->top_base
+ EMMC_TOP_CMD
, PAD_CMD_RXDLY
, value
);
2140 sdr_set_field(host
->top_base
+ EMMC_TOP_CMD
, PAD_CMD_RXDLY2
, 0);
2142 sdr_set_field(host
->top_base
+ EMMC_TOP_CMD
, PAD_CMD_RXDLY
,
2143 PAD_DELAY_HALF
- 1);
2144 sdr_set_field(host
->top_base
+ EMMC_TOP_CMD
, PAD_CMD_RXDLY2
,
2145 value
- PAD_DELAY_HALF
);
2148 if (value
< PAD_DELAY_HALF
) {
2149 sdr_set_field(host
->base
+ tune_reg
, MSDC_PAD_TUNE_CMDRDLY
, value
);
2150 sdr_set_field(host
->base
+ tune_reg
+ TUNING_REG2_FIXED_OFFEST
,
2151 MSDC_PAD_TUNE_CMDRDLY2
, 0);
2153 sdr_set_field(host
->base
+ tune_reg
, MSDC_PAD_TUNE_CMDRDLY
,
2154 PAD_DELAY_HALF
- 1);
2155 sdr_set_field(host
->base
+ tune_reg
+ TUNING_REG2_FIXED_OFFEST
,
2156 MSDC_PAD_TUNE_CMDRDLY2
, value
- PAD_DELAY_HALF
);
2161 static inline void msdc_set_data_delay(struct msdc_host
*host
, u32 value
)
2163 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
2165 if (host
->top_base
) {
2166 if (value
< PAD_DELAY_HALF
) {
2167 sdr_set_field(host
->top_base
+ EMMC_TOP_CONTROL
,
2168 PAD_DAT_RD_RXDLY
, value
);
2169 sdr_set_field(host
->top_base
+ EMMC_TOP_CONTROL
,
2170 PAD_DAT_RD_RXDLY2
, 0);
2172 sdr_set_field(host
->top_base
+ EMMC_TOP_CONTROL
,
2173 PAD_DAT_RD_RXDLY
, PAD_DELAY_HALF
- 1);
2174 sdr_set_field(host
->top_base
+ EMMC_TOP_CONTROL
,
2175 PAD_DAT_RD_RXDLY2
, value
- PAD_DELAY_HALF
);
2178 if (value
< PAD_DELAY_HALF
) {
2179 sdr_set_field(host
->base
+ tune_reg
, MSDC_PAD_TUNE_DATRRDLY
, value
);
2180 sdr_set_field(host
->base
+ tune_reg
+ TUNING_REG2_FIXED_OFFEST
,
2181 MSDC_PAD_TUNE_DATRRDLY2
, 0);
2183 sdr_set_field(host
->base
+ tune_reg
, MSDC_PAD_TUNE_DATRRDLY
,
2184 PAD_DELAY_HALF
- 1);
2185 sdr_set_field(host
->base
+ tune_reg
+ TUNING_REG2_FIXED_OFFEST
,
2186 MSDC_PAD_TUNE_DATRRDLY2
, value
- PAD_DELAY_HALF
);
2191 static inline void msdc_set_data_sample_edge(struct msdc_host
*host
, bool rising
)
2193 u32 value
= rising
? 0 : 1;
2195 if (host
->dev_comp
->support_new_rx
) {
2196 sdr_set_field(host
->base
+ MSDC_PATCH_BIT
, MSDC_PATCH_BIT_RD_DAT_SEL
, value
);
2197 sdr_set_field(host
->base
+ MSDC_PATCH_BIT2
, MSDC_PB2_CFGCRCSTSEDGE
, value
);
2199 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DSPL
, value
);
2200 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_W_DSPL
, value
);
2204 static int msdc_tune_response(struct mmc_host
*mmc
, u32 opcode
)
2206 struct msdc_host
*host
= mmc_priv(mmc
);
2207 u64 rise_delay
= 0, fall_delay
= 0;
2208 struct msdc_delay_phase final_rise_delay
, final_fall_delay
= { 0,};
2209 struct msdc_delay_phase internal_delay_phase
;
2210 u8 final_delay
, final_maxlen
;
2211 u32 internal_delay
= 0;
2212 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
2216 if (mmc
->ios
.timing
== MMC_TIMING_MMC_HS200
||
2217 mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
)
2218 sdr_set_field(host
->base
+ tune_reg
,
2219 MSDC_PAD_TUNE_CMDRRDLY
,
2220 host
->hs200_cmd_int_delay
);
2222 sdr_clr_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2223 for (i
= 0; i
< host
->tuning_step
; i
++) {
2224 msdc_set_cmd_delay(host
, i
);
2226 * Using the same parameters, it may sometimes pass the test,
2227 * but sometimes it may fail. To make sure the parameters are
2228 * more stable, we test each set of parameters 3 times.
2230 for (j
= 0; j
< 3; j
++) {
2231 mmc_send_tuning(mmc
, opcode
, &cmd_err
);
2233 rise_delay
|= BIT_ULL(i
);
2235 rise_delay
&= ~BIT_ULL(i
);
2240 final_rise_delay
= get_best_delay(host
, rise_delay
);
2241 /* if rising edge has enough margin, then do not scan falling edge */
2242 if (final_rise_delay
.maxlen
>= 12 ||
2243 (final_rise_delay
.start
== 0 && final_rise_delay
.maxlen
>= 4))
2246 sdr_set_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2247 for (i
= 0; i
< host
->tuning_step
; i
++) {
2248 msdc_set_cmd_delay(host
, i
);
2250 * Using the same parameters, it may sometimes pass the test,
2251 * but sometimes it may fail. To make sure the parameters are
2252 * more stable, we test each set of parameters 3 times.
2254 for (j
= 0; j
< 3; j
++) {
2255 mmc_send_tuning(mmc
, opcode
, &cmd_err
);
2257 fall_delay
|= BIT_ULL(i
);
2259 fall_delay
&= ~BIT_ULL(i
);
2264 final_fall_delay
= get_best_delay(host
, fall_delay
);
2267 final_maxlen
= max(final_rise_delay
.maxlen
, final_fall_delay
.maxlen
);
2268 if (final_fall_delay
.maxlen
>= 12 && final_fall_delay
.start
< 4)
2269 final_maxlen
= final_fall_delay
.maxlen
;
2270 if (final_maxlen
== final_rise_delay
.maxlen
) {
2271 sdr_clr_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2272 final_delay
= final_rise_delay
.final_phase
;
2274 sdr_set_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2275 final_delay
= final_fall_delay
.final_phase
;
2277 msdc_set_cmd_delay(host
, final_delay
);
2279 if (host
->dev_comp
->async_fifo
|| host
->hs200_cmd_int_delay
)
2282 for (i
= 0; i
< host
->tuning_step
; i
++) {
2283 sdr_set_field(host
->base
+ tune_reg
,
2284 MSDC_PAD_TUNE_CMDRRDLY
, i
);
2285 mmc_send_tuning(mmc
, opcode
, &cmd_err
);
2287 internal_delay
|= BIT_ULL(i
);
2289 dev_dbg(host
->dev
, "Final internal delay: 0x%x\n", internal_delay
);
2290 internal_delay_phase
= get_best_delay(host
, internal_delay
);
2291 sdr_set_field(host
->base
+ tune_reg
, MSDC_PAD_TUNE_CMDRRDLY
,
2292 internal_delay_phase
.final_phase
);
2294 dev_dbg(host
->dev
, "Final cmd pad delay: %x\n", final_delay
);
2295 return final_delay
== 0xff ? -EIO
: 0;
2298 static int hs400_tune_response(struct mmc_host
*mmc
, u32 opcode
)
2300 struct msdc_host
*host
= mmc_priv(mmc
);
2302 struct msdc_delay_phase final_cmd_delay
= { 0,};
2307 /* select EMMC50 PAD CMD tune */
2308 sdr_set_bits(host
->base
+ PAD_CMD_TUNE
, BIT(0));
2309 sdr_set_field(host
->base
+ MSDC_PATCH_BIT1
, MSDC_PATCH_BIT1_CMDTA
, 2);
2311 if (mmc
->ios
.timing
== MMC_TIMING_MMC_HS200
||
2312 mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
)
2313 sdr_set_field(host
->base
+ MSDC_PAD_TUNE
,
2314 MSDC_PAD_TUNE_CMDRRDLY
,
2315 host
->hs200_cmd_int_delay
);
2317 if (host
->hs400_cmd_resp_sel_rising
)
2318 sdr_clr_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2320 sdr_set_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2322 for (i
= 0; i
< PAD_DELAY_HALF
; i
++) {
2323 sdr_set_field(host
->base
+ PAD_CMD_TUNE
,
2324 PAD_CMD_TUNE_RX_DLY3
, i
);
2326 * Using the same parameters, it may sometimes pass the test,
2327 * but sometimes it may fail. To make sure the parameters are
2328 * more stable, we test each set of parameters 3 times.
2330 for (j
= 0; j
< 3; j
++) {
2331 mmc_send_tuning(mmc
, opcode
, &cmd_err
);
2333 cmd_delay
|= BIT(i
);
2335 cmd_delay
&= ~BIT(i
);
2340 final_cmd_delay
= get_best_delay(host
, cmd_delay
);
2341 sdr_set_field(host
->base
+ PAD_CMD_TUNE
, PAD_CMD_TUNE_RX_DLY3
,
2342 final_cmd_delay
.final_phase
);
2343 final_delay
= final_cmd_delay
.final_phase
;
2345 dev_dbg(host
->dev
, "Final cmd pad delay: %x\n", final_delay
);
2346 return final_delay
== 0xff ? -EIO
: 0;
2349 static int msdc_tune_data(struct mmc_host
*mmc
, u32 opcode
)
2351 struct msdc_host
*host
= mmc_priv(mmc
);
2352 u64 rise_delay
= 0, fall_delay
= 0;
2353 struct msdc_delay_phase final_rise_delay
, final_fall_delay
= { 0,};
2354 u8 final_delay
, final_maxlen
;
2357 sdr_set_field(host
->base
+ MSDC_PATCH_BIT
, MSDC_INT_DAT_LATCH_CK_SEL
,
2359 msdc_set_data_sample_edge(host
, true);
2360 for (i
= 0; i
< host
->tuning_step
; i
++) {
2361 msdc_set_data_delay(host
, i
);
2362 ret
= mmc_send_tuning(mmc
, opcode
, NULL
);
2364 rise_delay
|= BIT_ULL(i
);
2366 final_rise_delay
= get_best_delay(host
, rise_delay
);
2367 /* if rising edge has enough margin, then do not scan falling edge */
2368 if (final_rise_delay
.maxlen
>= 12 ||
2369 (final_rise_delay
.start
== 0 && final_rise_delay
.maxlen
>= 4))
2372 msdc_set_data_sample_edge(host
, false);
2373 for (i
= 0; i
< host
->tuning_step
; i
++) {
2374 msdc_set_data_delay(host
, i
);
2375 ret
= mmc_send_tuning(mmc
, opcode
, NULL
);
2377 fall_delay
|= BIT_ULL(i
);
2379 final_fall_delay
= get_best_delay(host
, fall_delay
);
2382 final_maxlen
= max(final_rise_delay
.maxlen
, final_fall_delay
.maxlen
);
2383 if (final_maxlen
== final_rise_delay
.maxlen
) {
2384 msdc_set_data_sample_edge(host
, true);
2385 final_delay
= final_rise_delay
.final_phase
;
2387 msdc_set_data_sample_edge(host
, false);
2388 final_delay
= final_fall_delay
.final_phase
;
2390 msdc_set_data_delay(host
, final_delay
);
2392 dev_dbg(host
->dev
, "Final data pad delay: %x\n", final_delay
);
2393 return final_delay
== 0xff ? -EIO
: 0;
2397 * MSDC IP which supports data tune + async fifo can do CMD/DAT tune
2398 * together, which can save the tuning time.
2400 static int msdc_tune_together(struct mmc_host
*mmc
, u32 opcode
)
2402 struct msdc_host
*host
= mmc_priv(mmc
);
2403 u64 rise_delay
= 0, fall_delay
= 0;
2404 struct msdc_delay_phase final_rise_delay
, final_fall_delay
= { 0,};
2405 u8 final_delay
, final_maxlen
;
2408 sdr_set_field(host
->base
+ MSDC_PATCH_BIT
, MSDC_INT_DAT_LATCH_CK_SEL
,
2411 sdr_clr_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2412 msdc_set_data_sample_edge(host
, true);
2413 for (i
= 0; i
< host
->tuning_step
; i
++) {
2414 msdc_set_cmd_delay(host
, i
);
2415 msdc_set_data_delay(host
, i
);
2416 ret
= mmc_send_tuning(mmc
, opcode
, NULL
);
2418 rise_delay
|= BIT_ULL(i
);
2420 final_rise_delay
= get_best_delay(host
, rise_delay
);
2421 /* if rising edge has enough margin, then do not scan falling edge */
2422 if (final_rise_delay
.maxlen
>= 12 ||
2423 (final_rise_delay
.start
== 0 && final_rise_delay
.maxlen
>= 4))
2426 sdr_set_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2427 msdc_set_data_sample_edge(host
, false);
2428 for (i
= 0; i
< host
->tuning_step
; i
++) {
2429 msdc_set_cmd_delay(host
, i
);
2430 msdc_set_data_delay(host
, i
);
2431 ret
= mmc_send_tuning(mmc
, opcode
, NULL
);
2433 fall_delay
|= BIT_ULL(i
);
2435 final_fall_delay
= get_best_delay(host
, fall_delay
);
2438 final_maxlen
= max(final_rise_delay
.maxlen
, final_fall_delay
.maxlen
);
2439 if (final_maxlen
== final_rise_delay
.maxlen
) {
2440 sdr_clr_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2441 msdc_set_data_sample_edge(host
, true);
2442 final_delay
= final_rise_delay
.final_phase
;
2444 sdr_set_bits(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
);
2445 msdc_set_data_sample_edge(host
, false);
2446 final_delay
= final_fall_delay
.final_phase
;
2449 msdc_set_cmd_delay(host
, final_delay
);
2450 msdc_set_data_delay(host
, final_delay
);
2452 dev_dbg(host
->dev
, "Final pad delay: %x\n", final_delay
);
2453 return final_delay
== 0xff ? -EIO
: 0;
2456 static int msdc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
2458 struct msdc_host
*host
= mmc_priv(mmc
);
2460 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
2462 if (host
->dev_comp
->data_tune
&& host
->dev_comp
->async_fifo
) {
2463 ret
= msdc_tune_together(mmc
, opcode
);
2464 if (host
->hs400_mode
) {
2465 msdc_set_data_sample_edge(host
, true);
2466 msdc_set_data_delay(host
, 0);
2470 if (host
->hs400_mode
&&
2471 host
->dev_comp
->hs400_tune
)
2472 ret
= hs400_tune_response(mmc
, opcode
);
2474 ret
= msdc_tune_response(mmc
, opcode
);
2476 dev_err(host
->dev
, "Tune response fail!\n");
2479 if (host
->hs400_mode
== false) {
2480 ret
= msdc_tune_data(mmc
, opcode
);
2482 dev_err(host
->dev
, "Tune data fail!\n");
2486 host
->saved_tune_para
.iocon
= readl(host
->base
+ MSDC_IOCON
);
2487 host
->saved_tune_para
.pad_tune
= readl(host
->base
+ tune_reg
);
2488 host
->saved_tune_para
.pad_cmd_tune
= readl(host
->base
+ PAD_CMD_TUNE
);
2489 if (host
->top_base
) {
2490 host
->saved_tune_para
.emmc_top_control
= readl(host
->top_base
+
2492 host
->saved_tune_para
.emmc_top_cmd
= readl(host
->top_base
+
2498 static int msdc_prepare_hs400_tuning(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
2500 struct msdc_host
*host
= mmc_priv(mmc
);
2501 host
->hs400_mode
= true;
2504 writel(host
->hs400_ds_delay
,
2505 host
->top_base
+ EMMC50_PAD_DS_TUNE
);
2507 writel(host
->hs400_ds_delay
, host
->base
+ PAD_DS_TUNE
);
2508 /* hs400 mode must set it to 0 */
2509 sdr_clr_bits(host
->base
+ MSDC_PATCH_BIT2
, MSDC_PATCH_BIT2_CFGCRCSTS
);
2510 /* to improve read performance, set outstanding to 2 */
2511 sdr_set_field(host
->base
+ EMMC50_CFG3
, EMMC50_CFG3_OUTS_WR
, 2);
2516 static int msdc_execute_hs400_tuning(struct mmc_host
*mmc
, struct mmc_card
*card
)
2518 struct msdc_host
*host
= mmc_priv(mmc
);
2519 struct msdc_delay_phase dly1_delay
;
2520 u32 val
, result_dly1
= 0;
2524 if (host
->top_base
) {
2525 sdr_set_bits(host
->top_base
+ EMMC50_PAD_DS_TUNE
,
2527 if (host
->hs400_ds_dly3
)
2528 sdr_set_field(host
->top_base
+ EMMC50_PAD_DS_TUNE
,
2529 PAD_DS_DLY3
, host
->hs400_ds_dly3
);
2531 sdr_set_bits(host
->base
+ PAD_DS_TUNE
, PAD_DS_TUNE_DLY_SEL
);
2532 if (host
->hs400_ds_dly3
)
2533 sdr_set_field(host
->base
+ PAD_DS_TUNE
,
2534 PAD_DS_TUNE_DLY3
, host
->hs400_ds_dly3
);
2537 host
->hs400_tuning
= true;
2538 for (i
= 0; i
< PAD_DELAY_HALF
; i
++) {
2540 sdr_set_field(host
->top_base
+ EMMC50_PAD_DS_TUNE
,
2543 sdr_set_field(host
->base
+ PAD_DS_TUNE
,
2544 PAD_DS_TUNE_DLY1
, i
);
2545 ret
= mmc_get_ext_csd(card
, &ext_csd
);
2547 result_dly1
|= BIT(i
);
2551 host
->hs400_tuning
= false;
2553 dly1_delay
= get_best_delay(host
, result_dly1
);
2554 if (dly1_delay
.maxlen
== 0) {
2555 dev_err(host
->dev
, "Failed to get DLY1 delay!\n");
2559 sdr_set_field(host
->top_base
+ EMMC50_PAD_DS_TUNE
,
2560 PAD_DS_DLY1
, dly1_delay
.final_phase
);
2562 sdr_set_field(host
->base
+ PAD_DS_TUNE
,
2563 PAD_DS_TUNE_DLY1
, dly1_delay
.final_phase
);
2566 val
= readl(host
->top_base
+ EMMC50_PAD_DS_TUNE
);
2568 val
= readl(host
->base
+ PAD_DS_TUNE
);
2570 dev_info(host
->dev
, "Final PAD_DS_TUNE: 0x%x\n", val
);
2575 dev_err(host
->dev
, "Failed to tuning DS pin delay!\n");
2579 static void msdc_hw_reset(struct mmc_host
*mmc
)
2581 struct msdc_host
*host
= mmc_priv(mmc
);
2583 sdr_set_bits(host
->base
+ EMMC_IOCON
, 1);
2584 udelay(10); /* 10us is enough */
2585 sdr_clr_bits(host
->base
+ EMMC_IOCON
, 1);
2588 static void msdc_ack_sdio_irq(struct mmc_host
*mmc
)
2590 unsigned long flags
;
2591 struct msdc_host
*host
= mmc_priv(mmc
);
2593 spin_lock_irqsave(&host
->lock
, flags
);
2594 __msdc_enable_sdio_irq(host
, 1);
2595 spin_unlock_irqrestore(&host
->lock
, flags
);
2598 static int msdc_get_cd(struct mmc_host
*mmc
)
2600 struct msdc_host
*host
= mmc_priv(mmc
);
2603 if (mmc
->caps
& MMC_CAP_NONREMOVABLE
)
2606 if (!host
->internal_cd
)
2607 return mmc_gpio_get_cd(mmc
);
2609 val
= readl(host
->base
+ MSDC_PS
) & MSDC_PS_CDSTS
;
2610 if (mmc
->caps2
& MMC_CAP2_CD_ACTIVE_HIGH
)
2616 static void msdc_hs400_enhanced_strobe(struct mmc_host
*mmc
,
2617 struct mmc_ios
*ios
)
2619 struct msdc_host
*host
= mmc_priv(mmc
);
2621 if (ios
->enhanced_strobe
) {
2622 msdc_prepare_hs400_tuning(mmc
, ios
);
2623 sdr_set_field(host
->base
+ EMMC50_CFG0
, EMMC50_CFG_PADCMD_LATCHCK
, 1);
2624 sdr_set_field(host
->base
+ EMMC50_CFG0
, EMMC50_CFG_CMD_RESP_SEL
, 1);
2625 sdr_set_field(host
->base
+ EMMC50_CFG1
, EMMC50_CFG1_DS_CFG
, 1);
2627 sdr_clr_bits(host
->base
+ CQHCI_SETTING
, CQHCI_RD_CMD_WND_SEL
);
2628 sdr_clr_bits(host
->base
+ CQHCI_SETTING
, CQHCI_WR_CMD_WND_SEL
);
2629 sdr_clr_bits(host
->base
+ EMMC51_CFG0
, CMDQ_RDAT_CNT
);
2631 sdr_set_field(host
->base
+ EMMC50_CFG0
, EMMC50_CFG_PADCMD_LATCHCK
, 0);
2632 sdr_set_field(host
->base
+ EMMC50_CFG0
, EMMC50_CFG_CMD_RESP_SEL
, 0);
2633 sdr_set_field(host
->base
+ EMMC50_CFG1
, EMMC50_CFG1_DS_CFG
, 0);
2635 sdr_set_bits(host
->base
+ CQHCI_SETTING
, CQHCI_RD_CMD_WND_SEL
);
2636 sdr_set_bits(host
->base
+ CQHCI_SETTING
, CQHCI_WR_CMD_WND_SEL
);
2637 sdr_set_field(host
->base
+ EMMC51_CFG0
, CMDQ_RDAT_CNT
, 0xb4);
2641 static void msdc_cqe_cit_cal(struct msdc_host
*host
, u64 timer_ns
)
2643 struct mmc_host
*mmc
= mmc_from_priv(host
);
2644 struct cqhci_host
*cq_host
= mmc
->cqe_private
;
2646 u64 hclk_freq
, value
;
2649 * On MediaTek SoCs the MSDC controller's CQE uses msdc_hclk as ITCFVAL
2650 * so we multiply/divide the HCLK frequency by ITCFMUL to calculate the
2651 * Send Status Command Idle Timer (CIT) value.
2653 hclk_freq
= (u64
)clk_get_rate(host
->h_clk
);
2654 itcfmul
= CQHCI_ITCFMUL(cqhci_readl(cq_host
, CQHCI_CAP
));
2657 do_div(hclk_freq
, 1000);
2660 do_div(hclk_freq
, 100);
2663 do_div(hclk_freq
, 10);
2668 hclk_freq
= hclk_freq
* 10;
2671 host
->cq_ssc1_time
= 0x40;
2675 value
= hclk_freq
* timer_ns
;
2676 do_div(value
, 1000000000);
2677 host
->cq_ssc1_time
= value
;
2680 static void msdc_cqe_enable(struct mmc_host
*mmc
)
2682 struct msdc_host
*host
= mmc_priv(mmc
);
2683 struct cqhci_host
*cq_host
= mmc
->cqe_private
;
2685 /* enable cmdq irq */
2686 writel(MSDC_INT_CMDQ
, host
->base
+ MSDC_INTEN
);
2687 /* enable busy check */
2688 sdr_set_bits(host
->base
+ MSDC_PATCH_BIT1
, MSDC_PB1_BUSY_CHECK_SEL
);
2689 /* default write data / busy timeout 20s */
2690 msdc_set_busy_timeout(host
, 20 * 1000000000ULL, 0);
2691 /* default read data timeout 1s */
2692 msdc_set_timeout(host
, 1000000000ULL, 0);
2694 /* Set the send status command idle timer */
2695 cqhci_writel(cq_host
, host
->cq_ssc1_time
, CQHCI_SSC1
);
2698 static void msdc_cqe_disable(struct mmc_host
*mmc
, bool recovery
)
2700 struct msdc_host
*host
= mmc_priv(mmc
);
2701 unsigned int val
= 0;
2703 /* disable cmdq irq */
2704 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INT_CMDQ
);
2705 /* disable busy check */
2706 sdr_clr_bits(host
->base
+ MSDC_PATCH_BIT1
, MSDC_PB1_BUSY_CHECK_SEL
);
2708 val
= readl(host
->base
+ MSDC_INT
);
2709 writel(val
, host
->base
+ MSDC_INT
);
2712 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
,
2713 MSDC_DMA_CTRL_STOP
, 1);
2714 if (WARN_ON(readl_poll_timeout(host
->base
+ MSDC_DMA_CTRL
, val
,
2715 !(val
& MSDC_DMA_CTRL_STOP
), 1, 3000)))
2717 if (WARN_ON(readl_poll_timeout(host
->base
+ MSDC_DMA_CFG
, val
,
2718 !(val
& MSDC_DMA_CFG_STS
), 1, 3000)))
2720 msdc_reset_hw(host
);
2724 static void msdc_cqe_pre_enable(struct mmc_host
*mmc
)
2726 struct cqhci_host
*cq_host
= mmc
->cqe_private
;
2729 reg
= cqhci_readl(cq_host
, CQHCI_CFG
);
2730 reg
|= CQHCI_ENABLE
;
2731 cqhci_writel(cq_host
, reg
, CQHCI_CFG
);
2734 static void msdc_cqe_post_disable(struct mmc_host
*mmc
)
2736 struct cqhci_host
*cq_host
= mmc
->cqe_private
;
2739 reg
= cqhci_readl(cq_host
, CQHCI_CFG
);
2740 reg
&= ~CQHCI_ENABLE
;
2741 cqhci_writel(cq_host
, reg
, CQHCI_CFG
);
2744 static const struct mmc_host_ops mt_msdc_ops
= {
2745 .post_req
= msdc_post_req
,
2746 .pre_req
= msdc_pre_req
,
2747 .request
= msdc_ops_request
,
2748 .set_ios
= msdc_ops_set_ios
,
2749 .get_ro
= mmc_gpio_get_ro
,
2750 .get_cd
= msdc_get_cd
,
2751 .hs400_enhanced_strobe
= msdc_hs400_enhanced_strobe
,
2752 .enable_sdio_irq
= msdc_enable_sdio_irq
,
2753 .ack_sdio_irq
= msdc_ack_sdio_irq
,
2754 .start_signal_voltage_switch
= msdc_ops_switch_volt
,
2755 .card_busy
= msdc_card_busy
,
2756 .execute_tuning
= msdc_execute_tuning
,
2757 .prepare_hs400_tuning
= msdc_prepare_hs400_tuning
,
2758 .execute_hs400_tuning
= msdc_execute_hs400_tuning
,
2759 .card_hw_reset
= msdc_hw_reset
,
2762 static const struct cqhci_host_ops msdc_cmdq_ops
= {
2763 .enable
= msdc_cqe_enable
,
2764 .disable
= msdc_cqe_disable
,
2765 .pre_enable
= msdc_cqe_pre_enable
,
2766 .post_disable
= msdc_cqe_post_disable
,
2769 static void msdc_of_property_parse(struct platform_device
*pdev
,
2770 struct msdc_host
*host
)
2772 struct mmc_host
*mmc
= mmc_from_priv(host
);
2774 of_property_read_u32(pdev
->dev
.of_node
, "mediatek,latch-ck",
2777 of_property_read_u32(pdev
->dev
.of_node
, "hs400-ds-delay",
2778 &host
->hs400_ds_delay
);
2780 of_property_read_u32(pdev
->dev
.of_node
, "mediatek,hs400-ds-dly3",
2781 &host
->hs400_ds_dly3
);
2783 of_property_read_u32(pdev
->dev
.of_node
, "mediatek,hs200-cmd-int-delay",
2784 &host
->hs200_cmd_int_delay
);
2786 of_property_read_u32(pdev
->dev
.of_node
, "mediatek,hs400-cmd-int-delay",
2787 &host
->hs400_cmd_int_delay
);
2789 if (of_property_read_bool(pdev
->dev
.of_node
,
2790 "mediatek,hs400-cmd-resp-sel-rising"))
2791 host
->hs400_cmd_resp_sel_rising
= true;
2793 host
->hs400_cmd_resp_sel_rising
= false;
2795 if (of_property_read_u32(pdev
->dev
.of_node
, "mediatek,tuning-step",
2796 &host
->tuning_step
)) {
2797 if (mmc
->caps2
& MMC_CAP2_NO_MMC
)
2798 host
->tuning_step
= PAD_DELAY_FULL
;
2800 host
->tuning_step
= PAD_DELAY_HALF
;
2803 if (of_property_read_bool(pdev
->dev
.of_node
,
2807 host
->cqhci
= false;
2810 static int msdc_of_clock_parse(struct platform_device
*pdev
,
2811 struct msdc_host
*host
)
2815 host
->src_clk
= devm_clk_get(&pdev
->dev
, "source");
2816 if (IS_ERR(host
->src_clk
))
2817 return PTR_ERR(host
->src_clk
);
2819 host
->h_clk
= devm_clk_get(&pdev
->dev
, "hclk");
2820 if (IS_ERR(host
->h_clk
))
2821 return PTR_ERR(host
->h_clk
);
2823 host
->bus_clk
= devm_clk_get_optional(&pdev
->dev
, "bus_clk");
2824 if (IS_ERR(host
->bus_clk
))
2825 host
->bus_clk
= NULL
;
2827 /*source clock control gate is optional clock*/
2828 host
->src_clk_cg
= devm_clk_get_optional(&pdev
->dev
, "source_cg");
2829 if (IS_ERR(host
->src_clk_cg
))
2830 return PTR_ERR(host
->src_clk_cg
);
2833 * Fallback for legacy device-trees: src_clk and HCLK use the same
2834 * bit to control gating but they are parented to a different mux,
2835 * hence if our intention is to gate only the source, required
2836 * during a clk mode switch to avoid hw hangs, we need to gate
2837 * its parent (specified as a different clock only on new DTs).
2839 if (!host
->src_clk_cg
) {
2840 host
->src_clk_cg
= clk_get_parent(host
->src_clk
);
2841 if (IS_ERR(host
->src_clk_cg
))
2842 return PTR_ERR(host
->src_clk_cg
);
2845 /* If present, always enable for this clock gate */
2846 host
->sys_clk_cg
= devm_clk_get_optional_enabled(&pdev
->dev
, "sys_cg");
2847 if (IS_ERR(host
->sys_clk_cg
))
2848 host
->sys_clk_cg
= NULL
;
2850 host
->bulk_clks
[0].id
= "pclk_cg";
2851 host
->bulk_clks
[1].id
= "axi_cg";
2852 host
->bulk_clks
[2].id
= "ahb_cg";
2853 ret
= devm_clk_bulk_get_optional(&pdev
->dev
, MSDC_NR_CLOCKS
,
2856 dev_err(&pdev
->dev
, "Cannot get pclk/axi/ahb clock gates\n");
2863 static int msdc_drv_probe(struct platform_device
*pdev
)
2865 struct mmc_host
*mmc
;
2866 struct msdc_host
*host
;
2869 if (!pdev
->dev
.of_node
) {
2870 dev_err(&pdev
->dev
, "No DT found\n");
2874 /* Allocate MMC host for this device */
2875 mmc
= devm_mmc_alloc_host(&pdev
->dev
, sizeof(struct msdc_host
));
2879 host
= mmc_priv(mmc
);
2880 ret
= mmc_of_parse(mmc
);
2884 host
->base
= devm_platform_ioremap_resource(pdev
, 0);
2885 if (IS_ERR(host
->base
))
2886 return PTR_ERR(host
->base
);
2888 host
->top_base
= devm_platform_ioremap_resource(pdev
, 1);
2889 if (IS_ERR(host
->top_base
))
2890 host
->top_base
= NULL
;
2892 ret
= mmc_regulator_get_supply(mmc
);
2896 ret
= msdc_of_clock_parse(pdev
, host
);
2900 host
->reset
= devm_reset_control_get_optional_exclusive(&pdev
->dev
,
2902 if (IS_ERR(host
->reset
))
2903 return PTR_ERR(host
->reset
);
2905 /* only eMMC has crypto property */
2906 if (!(mmc
->caps2
& MMC_CAP2_NO_MMC
)) {
2907 host
->crypto_clk
= devm_clk_get_optional(&pdev
->dev
, "crypto");
2908 if (IS_ERR(host
->crypto_clk
))
2909 return PTR_ERR(host
->crypto_clk
);
2910 else if (host
->crypto_clk
)
2911 mmc
->caps2
|= MMC_CAP2_CRYPTO
;
2914 host
->irq
= platform_get_irq(pdev
, 0);
2918 host
->pinctrl
= devm_pinctrl_get(&pdev
->dev
);
2919 if (IS_ERR(host
->pinctrl
))
2920 return dev_err_probe(&pdev
->dev
, PTR_ERR(host
->pinctrl
),
2921 "Cannot find pinctrl");
2923 host
->pins_default
= pinctrl_lookup_state(host
->pinctrl
, "default");
2924 if (IS_ERR(host
->pins_default
)) {
2925 dev_err(&pdev
->dev
, "Cannot find pinctrl default!\n");
2926 return PTR_ERR(host
->pins_default
);
2929 host
->pins_uhs
= pinctrl_lookup_state(host
->pinctrl
, "state_uhs");
2930 if (IS_ERR(host
->pins_uhs
)) {
2931 dev_err(&pdev
->dev
, "Cannot find pinctrl uhs!\n");
2932 return PTR_ERR(host
->pins_uhs
);
2935 /* Support for SDIO eint irq ? */
2936 if ((mmc
->pm_caps
& MMC_PM_WAKE_SDIO_IRQ
) && (mmc
->pm_caps
& MMC_PM_KEEP_POWER
)) {
2937 host
->eint_irq
= platform_get_irq_byname_optional(pdev
, "sdio_wakeup");
2938 if (host
->eint_irq
> 0) {
2939 host
->pins_eint
= pinctrl_lookup_state(host
->pinctrl
, "state_eint");
2940 if (IS_ERR(host
->pins_eint
)) {
2941 dev_err(&pdev
->dev
, "Cannot find pinctrl eint!\n");
2942 host
->pins_eint
= NULL
;
2944 device_init_wakeup(&pdev
->dev
, true);
2949 msdc_of_property_parse(pdev
, host
);
2951 host
->dev
= &pdev
->dev
;
2952 host
->dev_comp
= of_device_get_match_data(&pdev
->dev
);
2953 host
->src_clk_freq
= clk_get_rate(host
->src_clk
);
2954 /* Set host parameters to mmc */
2955 mmc
->ops
= &mt_msdc_ops
;
2956 if (host
->dev_comp
->clk_div_bits
== 8)
2957 mmc
->f_min
= DIV_ROUND_UP(host
->src_clk_freq
, 4 * 255);
2959 mmc
->f_min
= DIV_ROUND_UP(host
->src_clk_freq
, 4 * 4095);
2961 if (!(mmc
->caps
& MMC_CAP_NONREMOVABLE
) &&
2962 !mmc_can_gpio_cd(mmc
) &&
2963 host
->dev_comp
->use_internal_cd
) {
2965 * Is removable but no GPIO declared, so
2966 * use internal functionality.
2968 host
->internal_cd
= true;
2971 if (mmc
->caps
& MMC_CAP_SDIO_IRQ
)
2972 mmc
->caps2
|= MMC_CAP2_SDIO_IRQ_NOTHREAD
;
2974 mmc
->caps
|= MMC_CAP_CMD23
;
2976 mmc
->caps2
|= MMC_CAP2_CQE
| MMC_CAP2_CQE_DCMD
;
2977 /* MMC core transfer sizes tunable parameters */
2978 mmc
->max_segs
= MAX_BD_NUM
;
2979 if (host
->dev_comp
->support_64g
)
2980 mmc
->max_seg_size
= BDMA_DESC_BUFLEN_EXT
;
2982 mmc
->max_seg_size
= BDMA_DESC_BUFLEN
;
2983 mmc
->max_blk_size
= 2048;
2984 mmc
->max_req_size
= 512 * 1024;
2985 mmc
->max_blk_count
= mmc
->max_req_size
/ 512;
2986 if (host
->dev_comp
->support_64g
)
2987 host
->dma_mask
= DMA_BIT_MASK(36);
2989 host
->dma_mask
= DMA_BIT_MASK(32);
2990 mmc_dev(mmc
)->dma_mask
= &host
->dma_mask
;
2992 host
->timeout_clks
= 3 * 1048576;
2993 host
->dma
.gpd
= dma_alloc_coherent(&pdev
->dev
,
2994 2 * sizeof(struct mt_gpdma_desc
),
2995 &host
->dma
.gpd_addr
, GFP_KERNEL
);
2996 host
->dma
.bd
= dma_alloc_coherent(&pdev
->dev
,
2997 MAX_BD_NUM
* sizeof(struct mt_bdma_desc
),
2998 &host
->dma
.bd_addr
, GFP_KERNEL
);
2999 if (!host
->dma
.gpd
|| !host
->dma
.bd
) {
3003 msdc_init_gpd_bd(host
, &host
->dma
);
3004 INIT_DELAYED_WORK(&host
->req_timeout
, msdc_request_timeout
);
3005 spin_lock_init(&host
->lock
);
3007 platform_set_drvdata(pdev
, mmc
);
3008 ret
= msdc_ungate_clock(host
);
3010 dev_err(&pdev
->dev
, "Cannot ungate clocks!\n");
3015 if (mmc
->caps2
& MMC_CAP2_CQE
) {
3016 host
->cq_host
= devm_kzalloc(mmc
->parent
,
3017 sizeof(*host
->cq_host
),
3019 if (!host
->cq_host
) {
3023 host
->cq_host
->caps
|= CQHCI_TASK_DESC_SZ_128
;
3024 host
->cq_host
->mmio
= host
->base
+ 0x800;
3025 host
->cq_host
->ops
= &msdc_cmdq_ops
;
3026 ret
= cqhci_init(host
->cq_host
, mmc
, true);
3029 mmc
->max_segs
= 128;
3030 /* cqhci 16bit length */
3031 /* 0 size, means 65536 so we don't have to -1 here */
3032 mmc
->max_seg_size
= 64 * 1024;
3033 /* Reduce CIT to 0x40 that corresponds to 2.35us */
3034 msdc_cqe_cit_cal(host
, 2350);
3035 } else if (mmc
->caps2
& MMC_CAP2_NO_SDIO
) {
3036 /* Use HSQ on eMMC/SD (but not on SDIO) if HW CQE not supported */
3037 struct mmc_hsq
*hsq
= devm_kzalloc(&pdev
->dev
, sizeof(*hsq
), GFP_KERNEL
);
3043 ret
= mmc_hsq_init(hsq
, mmc
);
3047 host
->hsq_en
= true;
3050 ret
= devm_request_irq(&pdev
->dev
, host
->irq
, msdc_irq
,
3051 IRQF_TRIGGER_NONE
, pdev
->name
, host
);
3055 pm_runtime_set_active(host
->dev
);
3056 pm_runtime_set_autosuspend_delay(host
->dev
, MTK_MMC_AUTOSUSPEND_DELAY
);
3057 pm_runtime_use_autosuspend(host
->dev
);
3058 pm_runtime_enable(host
->dev
);
3059 ret
= mmc_add_host(mmc
);
3066 pm_runtime_disable(host
->dev
);
3068 msdc_deinit_hw(host
);
3070 msdc_gate_clock(host
);
3071 platform_set_drvdata(pdev
, NULL
);
3074 dma_free_coherent(&pdev
->dev
,
3075 2 * sizeof(struct mt_gpdma_desc
),
3076 host
->dma
.gpd
, host
->dma
.gpd_addr
);
3078 dma_free_coherent(&pdev
->dev
,
3079 MAX_BD_NUM
* sizeof(struct mt_bdma_desc
),
3080 host
->dma
.bd
, host
->dma
.bd_addr
);
3084 static void msdc_drv_remove(struct platform_device
*pdev
)
3086 struct mmc_host
*mmc
;
3087 struct msdc_host
*host
;
3089 mmc
= platform_get_drvdata(pdev
);
3090 host
= mmc_priv(mmc
);
3092 pm_runtime_get_sync(host
->dev
);
3094 platform_set_drvdata(pdev
, NULL
);
3095 mmc_remove_host(mmc
);
3096 msdc_deinit_hw(host
);
3097 msdc_gate_clock(host
);
3099 pm_runtime_disable(host
->dev
);
3100 pm_runtime_put_noidle(host
->dev
);
3101 dma_free_coherent(&pdev
->dev
,
3102 2 * sizeof(struct mt_gpdma_desc
),
3103 host
->dma
.gpd
, host
->dma
.gpd_addr
);
3104 dma_free_coherent(&pdev
->dev
, MAX_BD_NUM
* sizeof(struct mt_bdma_desc
),
3105 host
->dma
.bd
, host
->dma
.bd_addr
);
3108 static void msdc_save_reg(struct msdc_host
*host
)
3110 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
3112 host
->save_para
.msdc_cfg
= readl(host
->base
+ MSDC_CFG
);
3113 host
->save_para
.iocon
= readl(host
->base
+ MSDC_IOCON
);
3114 host
->save_para
.sdc_cfg
= readl(host
->base
+ SDC_CFG
);
3115 host
->save_para
.patch_bit0
= readl(host
->base
+ MSDC_PATCH_BIT
);
3116 host
->save_para
.patch_bit1
= readl(host
->base
+ MSDC_PATCH_BIT1
);
3117 host
->save_para
.patch_bit2
= readl(host
->base
+ MSDC_PATCH_BIT2
);
3118 host
->save_para
.pad_ds_tune
= readl(host
->base
+ PAD_DS_TUNE
);
3119 host
->save_para
.pad_cmd_tune
= readl(host
->base
+ PAD_CMD_TUNE
);
3120 host
->save_para
.emmc50_cfg0
= readl(host
->base
+ EMMC50_CFG0
);
3121 host
->save_para
.emmc50_cfg3
= readl(host
->base
+ EMMC50_CFG3
);
3122 host
->save_para
.sdc_fifo_cfg
= readl(host
->base
+ SDC_FIFO_CFG
);
3123 if (host
->top_base
) {
3124 host
->save_para
.emmc_top_control
=
3125 readl(host
->top_base
+ EMMC_TOP_CONTROL
);
3126 host
->save_para
.emmc_top_cmd
=
3127 readl(host
->top_base
+ EMMC_TOP_CMD
);
3128 host
->save_para
.emmc50_pad_ds_tune
=
3129 readl(host
->top_base
+ EMMC50_PAD_DS_TUNE
);
3130 host
->save_para
.loop_test_control
=
3131 readl(host
->top_base
+ LOOP_TEST_CONTROL
);
3133 host
->save_para
.pad_tune
= readl(host
->base
+ tune_reg
);
3137 static void msdc_restore_reg(struct msdc_host
*host
)
3139 struct mmc_host
*mmc
= mmc_from_priv(host
);
3140 u32 tune_reg
= host
->dev_comp
->pad_tune_reg
;
3142 if (host
->dev_comp
->support_new_tx
) {
3143 sdr_clr_bits(host
->base
+ SDC_ADV_CFG0
, SDC_NEW_TX_EN
);
3144 sdr_set_bits(host
->base
+ SDC_ADV_CFG0
, SDC_NEW_TX_EN
);
3146 if (host
->dev_comp
->support_new_rx
) {
3147 sdr_clr_bits(host
->base
+ MSDC_NEW_RX_CFG
, MSDC_NEW_RX_PATH_SEL
);
3148 sdr_set_bits(host
->base
+ MSDC_NEW_RX_CFG
, MSDC_NEW_RX_PATH_SEL
);
3151 writel(host
->save_para
.msdc_cfg
, host
->base
+ MSDC_CFG
);
3152 writel(host
->save_para
.iocon
, host
->base
+ MSDC_IOCON
);
3153 writel(host
->save_para
.sdc_cfg
, host
->base
+ SDC_CFG
);
3154 writel(host
->save_para
.patch_bit0
, host
->base
+ MSDC_PATCH_BIT
);
3155 writel(host
->save_para
.patch_bit1
, host
->base
+ MSDC_PATCH_BIT1
);
3156 writel(host
->save_para
.patch_bit2
, host
->base
+ MSDC_PATCH_BIT2
);
3157 writel(host
->save_para
.pad_ds_tune
, host
->base
+ PAD_DS_TUNE
);
3158 writel(host
->save_para
.pad_cmd_tune
, host
->base
+ PAD_CMD_TUNE
);
3159 writel(host
->save_para
.emmc50_cfg0
, host
->base
+ EMMC50_CFG0
);
3160 writel(host
->save_para
.emmc50_cfg3
, host
->base
+ EMMC50_CFG3
);
3161 writel(host
->save_para
.sdc_fifo_cfg
, host
->base
+ SDC_FIFO_CFG
);
3162 if (host
->top_base
) {
3163 writel(host
->save_para
.emmc_top_control
,
3164 host
->top_base
+ EMMC_TOP_CONTROL
);
3165 writel(host
->save_para
.emmc_top_cmd
,
3166 host
->top_base
+ EMMC_TOP_CMD
);
3167 writel(host
->save_para
.emmc50_pad_ds_tune
,
3168 host
->top_base
+ EMMC50_PAD_DS_TUNE
);
3169 writel(host
->save_para
.loop_test_control
,
3170 host
->top_base
+ LOOP_TEST_CONTROL
);
3172 writel(host
->save_para
.pad_tune
, host
->base
+ tune_reg
);
3175 if (sdio_irq_claimed(mmc
))
3176 __msdc_enable_sdio_irq(host
, 1);
3179 static int __maybe_unused
msdc_runtime_suspend(struct device
*dev
)
3181 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
3182 struct msdc_host
*host
= mmc_priv(mmc
);
3185 mmc_hsq_suspend(mmc
);
3187 msdc_save_reg(host
);
3189 if (sdio_irq_claimed(mmc
)) {
3190 if (host
->pins_eint
) {
3191 disable_irq(host
->irq
);
3192 pinctrl_select_state(host
->pinctrl
, host
->pins_eint
);
3195 __msdc_enable_sdio_irq(host
, 0);
3197 msdc_gate_clock(host
);
3201 static int __maybe_unused
msdc_runtime_resume(struct device
*dev
)
3203 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
3204 struct msdc_host
*host
= mmc_priv(mmc
);
3207 ret
= msdc_ungate_clock(host
);
3211 msdc_restore_reg(host
);
3213 if (sdio_irq_claimed(mmc
) && host
->pins_eint
) {
3214 pinctrl_select_state(host
->pinctrl
, host
->pins_uhs
);
3215 enable_irq(host
->irq
);
3219 mmc_hsq_resume(mmc
);
3224 static int __maybe_unused
msdc_suspend(struct device
*dev
)
3226 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
3227 struct msdc_host
*host
= mmc_priv(mmc
);
3231 if (mmc
->caps2
& MMC_CAP2_CQE
) {
3232 ret
= cqhci_suspend(mmc
);
3235 val
= readl(host
->base
+ MSDC_INT
);
3236 writel(val
, host
->base
+ MSDC_INT
);
3240 * Bump up runtime PM usage counter otherwise dev->power.needs_force_resume will
3241 * not be marked as 1, pm_runtime_force_resume() will go out directly.
3243 if (sdio_irq_claimed(mmc
) && host
->pins_eint
)
3244 pm_runtime_get_noresume(dev
);
3246 return pm_runtime_force_suspend(dev
);
3249 static int __maybe_unused
msdc_resume(struct device
*dev
)
3251 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
3252 struct msdc_host
*host
= mmc_priv(mmc
);
3254 if (sdio_irq_claimed(mmc
) && host
->pins_eint
)
3255 pm_runtime_put_noidle(dev
);
3257 return pm_runtime_force_resume(dev
);
3260 static const struct dev_pm_ops msdc_dev_pm_ops
= {
3261 SET_SYSTEM_SLEEP_PM_OPS(msdc_suspend
, msdc_resume
)
3262 SET_RUNTIME_PM_OPS(msdc_runtime_suspend
, msdc_runtime_resume
, NULL
)
3265 static struct platform_driver mt_msdc_driver
= {
3266 .probe
= msdc_drv_probe
,
3267 .remove
= msdc_drv_remove
,
3270 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
3271 .of_match_table
= msdc_of_ids
,
3272 .pm
= &msdc_dev_pm_ops
,
3276 module_platform_driver(mt_msdc_driver
);
3277 MODULE_LICENSE("GPL v2");
3278 MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver");