blob: 5e2ece0499bff04a8341af8fe2c87e43b6b58f87 [file] [log] [blame]
Weijie Gaod24b6932018-11-15 10:08:04 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek SD/MMC Card Interface driver
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Weijie Gao <weijie.gao@mediatek.com>
7 */
8
9#include <clk.h>
10#include <common.h>
11#include <dm.h>
12#include <mmc.h>
13#include <errno.h>
14#include <malloc.h>
mingming lee3b0397d2019-12-31 11:29:24 +080015#include <mapmem.h>
Weijie Gaod24b6932018-11-15 10:08:04 +080016#include <stdbool.h>
17#include <asm/gpio.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Weijie Gaod24b6932018-11-15 10:08:04 +080019#include <dm/pinctrl.h>
20#include <linux/bitops.h>
21#include <linux/io.h>
22#include <linux/iopoll.h>
23
24/* MSDC_CFG */
25#define MSDC_CFG_HS400_CK_MODE_EXT BIT(22)
26#define MSDC_CFG_CKMOD_EXT_M 0x300000
27#define MSDC_CFG_CKMOD_EXT_S 20
28#define MSDC_CFG_CKDIV_EXT_M 0xfff00
29#define MSDC_CFG_CKDIV_EXT_S 8
30#define MSDC_CFG_HS400_CK_MODE BIT(18)
31#define MSDC_CFG_CKMOD_M 0x30000
32#define MSDC_CFG_CKMOD_S 16
33#define MSDC_CFG_CKDIV_M 0xff00
34#define MSDC_CFG_CKDIV_S 8
35#define MSDC_CFG_CKSTB BIT(7)
36#define MSDC_CFG_PIO BIT(3)
37#define MSDC_CFG_RST BIT(2)
38#define MSDC_CFG_CKPDN BIT(1)
39#define MSDC_CFG_MODE BIT(0)
40
41/* MSDC_IOCON */
42#define MSDC_IOCON_W_DSPL BIT(8)
43#define MSDC_IOCON_DSPL BIT(2)
44#define MSDC_IOCON_RSPL BIT(1)
45
46/* MSDC_PS */
47#define MSDC_PS_DAT0 BIT(16)
48#define MSDC_PS_CDDBCE_M 0xf000
49#define MSDC_PS_CDDBCE_S 12
50#define MSDC_PS_CDSTS BIT(1)
51#define MSDC_PS_CDEN BIT(0)
52
53/* #define MSDC_INT(EN) */
54#define MSDC_INT_ACMDRDY BIT(3)
55#define MSDC_INT_ACMDTMO BIT(4)
56#define MSDC_INT_ACMDCRCERR BIT(5)
57#define MSDC_INT_CMDRDY BIT(8)
58#define MSDC_INT_CMDTMO BIT(9)
59#define MSDC_INT_RSPCRCERR BIT(10)
60#define MSDC_INT_XFER_COMPL BIT(12)
61#define MSDC_INT_DATTMO BIT(14)
62#define MSDC_INT_DATCRCERR BIT(15)
63
64/* MSDC_FIFOCS */
65#define MSDC_FIFOCS_CLR BIT(31)
66#define MSDC_FIFOCS_TXCNT_M 0xff0000
67#define MSDC_FIFOCS_TXCNT_S 16
68#define MSDC_FIFOCS_RXCNT_M 0xff
69#define MSDC_FIFOCS_RXCNT_S 0
70
71/* #define SDC_CFG */
72#define SDC_CFG_DTOC_M 0xff000000
73#define SDC_CFG_DTOC_S 24
74#define SDC_CFG_SDIOIDE BIT(20)
75#define SDC_CFG_SDIO BIT(19)
76#define SDC_CFG_BUSWIDTH_M 0x30000
77#define SDC_CFG_BUSWIDTH_S 16
78
79/* SDC_CMD */
80#define SDC_CMD_BLK_LEN_M 0xfff0000
81#define SDC_CMD_BLK_LEN_S 16
82#define SDC_CMD_STOP BIT(14)
83#define SDC_CMD_WR BIT(13)
84#define SDC_CMD_DTYPE_M 0x1800
85#define SDC_CMD_DTYPE_S 11
86#define SDC_CMD_RSPTYP_M 0x380
87#define SDC_CMD_RSPTYP_S 7
88#define SDC_CMD_CMD_M 0x3f
89#define SDC_CMD_CMD_S 0
90
91/* SDC_STS */
92#define SDC_STS_CMDBUSY BIT(1)
93#define SDC_STS_SDCBUSY BIT(0)
94
95/* SDC_ADV_CFG0 */
96#define SDC_RX_ENHANCE_EN BIT(20)
97
98/* PATCH_BIT0 */
99#define MSDC_INT_DAT_LATCH_CK_SEL_M 0x380
100#define MSDC_INT_DAT_LATCH_CK_SEL_S 7
101
102/* PATCH_BIT1 */
103#define MSDC_PB1_STOP_DLY_M 0xf00
104#define MSDC_PB1_STOP_DLY_S 8
105
106/* PATCH_BIT2 */
107#define MSDC_PB2_CRCSTSENSEL_M 0xe0000000
108#define MSDC_PB2_CRCSTSENSEL_S 29
109#define MSDC_PB2_CFGCRCSTS BIT(28)
110#define MSDC_PB2_RESPSTSENSEL_M 0x70000
111#define MSDC_PB2_RESPSTSENSEL_S 16
112#define MSDC_PB2_CFGRESP BIT(15)
113#define MSDC_PB2_RESPWAIT_M 0x0c
114#define MSDC_PB2_RESPWAIT_S 2
115
Weijie Gao2a505cd2020-11-12 16:37:02 +0800116/* MSDC_PAD_CTRL0 */
117#define MSDC_PAD_CTRL0_CLKRDSEL_M 0xff000000
118#define MSDC_PAD_CTRL0_CLKRDSEL_S 24
119#define MSDC_PAD_CTRL0_CLKTDSEL BIT(20)
120#define MSDC_PAD_CTRL0_CLKIES BIT(19)
121#define MSDC_PAD_CTRL0_CLKSMT BIT(18)
122#define MSDC_PAD_CTRL0_CLKPU BIT(17)
123#define MSDC_PAD_CTRL0_CLKPD BIT(16)
124#define MSDC_PAD_CTRL0_CLKSR BIT(8)
125#define MSDC_PAD_CTRL0_CLKDRVP_M 0x70
126#define MSDC_PAD_CTRL0_CLKDRVP_S 4
127#define MSDC_PAD_CTRL0_CLKDRVN_M 0x7
128#define MSDC_PAD_CTRL0_CLKDRVN_S 0
129
130/* MSDC_PAD_CTRL1 */
131#define MSDC_PAD_CTRL1_CMDRDSEL_M 0xff000000
132#define MSDC_PAD_CTRL1_CMDRDSEL_S 24
133#define MSDC_PAD_CTRL1_CMDTDSEL BIT(20)
134#define MSDC_PAD_CTRL1_CMDIES BIT(19)
135#define MSDC_PAD_CTRL1_CMDSMT BIT(18)
136#define MSDC_PAD_CTRL1_CMDPU BIT(17)
137#define MSDC_PAD_CTRL1_CMDPD BIT(16)
138#define MSDC_PAD_CTRL1_CMDSR BIT(8)
139#define MSDC_PAD_CTRL1_CMDDRVP_M 0x70
140#define MSDC_PAD_CTRL1_CMDDRVP_S 4
141#define MSDC_PAD_CTRL1_CMDDRVN_M 0x7
142#define MSDC_PAD_CTRL1_CMDDRVN_S 0
143
144/* MSDC_PAD_CTRL2 */
145#define MSDC_PAD_CTRL2_DATRDSEL_M 0xff000000
146#define MSDC_PAD_CTRL2_DATRDSEL_S 24
147#define MSDC_PAD_CTRL2_DATTDSEL BIT(20)
148#define MSDC_PAD_CTRL2_DATIES BIT(19)
149#define MSDC_PAD_CTRL2_DATSMT BIT(18)
150#define MSDC_PAD_CTRL2_DATPU BIT(17)
151#define MSDC_PAD_CTRL2_DATPD BIT(16)
152#define MSDC_PAD_CTRL2_DATSR BIT(8)
153#define MSDC_PAD_CTRL2_DATDRVP_M 0x70
154#define MSDC_PAD_CTRL2_DATDRVP_S 4
155#define MSDC_PAD_CTRL2_DATDRVN_M 0x7
156#define MSDC_PAD_CTRL2_DATDRVN_S 0
157
Weijie Gaod24b6932018-11-15 10:08:04 +0800158/* PAD_TUNE */
Weijie Gao2a505cd2020-11-12 16:37:02 +0800159#define MSDC_PAD_TUNE_CLKTDLY_M 0xf8000000
160#define MSDC_PAD_TUNE_CLKTDLY_S 27
Weijie Gaod24b6932018-11-15 10:08:04 +0800161#define MSDC_PAD_TUNE_CMDRRDLY_M 0x7c00000
162#define MSDC_PAD_TUNE_CMDRRDLY_S 22
163#define MSDC_PAD_TUNE_CMD_SEL BIT(21)
164#define MSDC_PAD_TUNE_CMDRDLY_M 0x1f0000
165#define MSDC_PAD_TUNE_CMDRDLY_S 16
166#define MSDC_PAD_TUNE_RXDLYSEL BIT(15)
167#define MSDC_PAD_TUNE_RD_SEL BIT(13)
168#define MSDC_PAD_TUNE_DATRRDLY_M 0x1f00
169#define MSDC_PAD_TUNE_DATRRDLY_S 8
170#define MSDC_PAD_TUNE_DATWRDLY_M 0x1f
171#define MSDC_PAD_TUNE_DATWRDLY_S 0
172
mingming lee69373bd2019-11-07 19:28:42 +0800173#define PAD_CMD_TUNE_RX_DLY3 0x3E
174#define PAD_CMD_TUNE_RX_DLY3_S 1
175
Weijie Gao2a505cd2020-11-12 16:37:02 +0800176/* PAD_TUNE0 */
177#define MSDC_PAD_TUNE0_DAT0RDDLY_M 0x1f000000
178#define MSDC_PAD_TUNE0_DAT0RDDLY_S 24
179#define MSDC_PAD_TUNE0_DAT1RDDLY_M 0x1f0000
180#define MSDC_PAD_TUNE0_DAT1RDDLY_S 16
181#define MSDC_PAD_TUNE0_DAT2RDDLY_M 0x1f00
182#define MSDC_PAD_TUNE0_DAT2RDDLY_S 8
183#define MSDC_PAD_TUNE0_DAT3RDDLY_M 0x1f
184#define MSDC_PAD_TUNE0_DAT3RDDLY_S 0
185
186/* PAD_TUNE1 */
187#define MSDC_PAD_TUNE1_DAT4RDDLY_M 0x1f000000
188#define MSDC_PAD_TUNE1_DAT4RDDLY_S 24
189#define MSDC_PAD_TUNE1_DAT5RDDLY_M 0x1f0000
190#define MSDC_PAD_TUNE1_DAT5RDDLY_S 16
191#define MSDC_PAD_TUNE1_DAT6RDDLY_M 0x1f00
192#define MSDC_PAD_TUNE1_DAT6RDDLY_S 8
193#define MSDC_PAD_TUNE1_DAT7RDDLY_M 0x1f
194#define MSDC_PAD_TUNE1_DAT7RDDLY_S 0
195
Weijie Gaod24b6932018-11-15 10:08:04 +0800196/* EMMC50_CFG0 */
197#define EMMC50_CFG_CFCSTS_SEL BIT(4)
198
199/* SDC_FIFO_CFG */
200#define SDC_FIFO_CFG_WRVALIDSEL BIT(24)
201#define SDC_FIFO_CFG_RDVALIDSEL BIT(25)
202
mingming lee3b0397d2019-12-31 11:29:24 +0800203/* EMMC_TOP_CONTROL mask */
204#define PAD_RXDLY_SEL BIT(0)
205#define DELAY_EN BIT(1)
206#define PAD_DAT_RD_RXDLY2 (0x1f << 2)
207#define PAD_DAT_RD_RXDLY (0x1f << 7)
208#define PAD_DAT_RD_RXDLY_S 7
209#define PAD_DAT_RD_RXDLY2_SEL BIT(12)
210#define PAD_DAT_RD_RXDLY_SEL BIT(13)
211#define DATA_K_VALUE_SEL BIT(14)
212#define SDC_RX_ENH_EN BIT(15)
213
214/* EMMC_TOP_CMD mask */
215#define PAD_CMD_RXDLY2 (0x1f << 0)
216#define PAD_CMD_RXDLY (0x1f << 5)
217#define PAD_CMD_RXDLY_S 5
218#define PAD_CMD_RD_RXDLY2_SEL BIT(10)
219#define PAD_CMD_RD_RXDLY_SEL BIT(11)
220#define PAD_CMD_TX_DLY (0x1f << 12)
221
Weijie Gaod24b6932018-11-15 10:08:04 +0800222/* SDC_CFG_BUSWIDTH */
223#define MSDC_BUS_1BITS 0x0
224#define MSDC_BUS_4BITS 0x1
225#define MSDC_BUS_8BITS 0x2
226
227#define MSDC_FIFO_SIZE 128
228
229#define PAD_DELAY_MAX 32
230
231#define DEFAULT_CD_DEBOUNCE 8
232
Weijie Gaof875a122020-11-12 16:36:57 +0800233#define SCLK_CYCLES_SHIFT 20
234
Weijie Gaod24b6932018-11-15 10:08:04 +0800235#define CMD_INTS_MASK \
236 (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO)
237
238#define DATA_INTS_MASK \
239 (MSDC_INT_XFER_COMPL | MSDC_INT_DATTMO | MSDC_INT_DATCRCERR)
240
241/* Register offset */
242struct mtk_sd_regs {
243 u32 msdc_cfg;
244 u32 msdc_iocon;
245 u32 msdc_ps;
246 u32 msdc_int;
247 u32 msdc_inten;
248 u32 msdc_fifocs;
249 u32 msdc_txdata;
250 u32 msdc_rxdata;
251 u32 reserved0[4];
252 u32 sdc_cfg;
253 u32 sdc_cmd;
254 u32 sdc_arg;
255 u32 sdc_sts;
256 u32 sdc_resp[4];
257 u32 sdc_blk_num;
258 u32 sdc_vol_chg;
259 u32 sdc_csts;
260 u32 sdc_csts_en;
261 u32 sdc_datcrc_sts;
262 u32 sdc_adv_cfg0;
263 u32 reserved1[2];
264 u32 emmc_cfg0;
265 u32 emmc_cfg1;
266 u32 emmc_sts;
267 u32 emmc_iocon;
268 u32 sd_acmd_resp;
269 u32 sd_acmd19_trg;
270 u32 sd_acmd19_sts;
271 u32 dma_sa_high4bit;
272 u32 dma_sa;
273 u32 dma_ca;
274 u32 dma_ctrl;
275 u32 dma_cfg;
276 u32 sw_dbg_sel;
277 u32 sw_dbg_out;
278 u32 dma_length;
279 u32 reserved2;
280 u32 patch_bit0;
281 u32 patch_bit1;
282 u32 patch_bit2;
283 u32 reserved3;
284 u32 dat0_tune_crc;
285 u32 dat1_tune_crc;
286 u32 dat2_tune_crc;
287 u32 dat3_tune_crc;
288 u32 cmd_tune_crc;
289 u32 sdio_tune_wind;
Weijie Gao2a505cd2020-11-12 16:37:02 +0800290 u32 reserved4[2];
291 u32 pad_ctrl0;
292 u32 pad_ctrl1;
293 u32 pad_ctrl2;
Weijie Gaod24b6932018-11-15 10:08:04 +0800294 u32 pad_tune;
295 u32 pad_tune0;
296 u32 pad_tune1;
297 u32 dat_rd_dly[4];
298 u32 reserved5[2];
299 u32 hw_dbg_sel;
300 u32 main_ver;
301 u32 eco_ver;
302 u32 reserved6[27];
303 u32 pad_ds_tune;
mingming lee69373bd2019-11-07 19:28:42 +0800304 u32 pad_cmd_tune;
305 u32 reserved7[30];
Weijie Gaod24b6932018-11-15 10:08:04 +0800306 u32 emmc50_cfg0;
307 u32 reserved8[7];
308 u32 sdc_fifo_cfg;
309};
310
mingming lee3b0397d2019-12-31 11:29:24 +0800311struct msdc_top_regs {
312 u32 emmc_top_control;
313 u32 emmc_top_cmd;
314 u32 emmc50_pad_ctl0;
315 u32 emmc50_pad_ds_tune;
316 u32 emmc50_pad_dat0_tune;
317 u32 emmc50_pad_dat1_tune;
318 u32 emmc50_pad_dat2_tune;
319 u32 emmc50_pad_dat3_tune;
320 u32 emmc50_pad_dat4_tune;
321 u32 emmc50_pad_dat5_tune;
322 u32 emmc50_pad_dat6_tune;
323 u32 emmc50_pad_dat7_tune;
324};
325
Weijie Gaod24b6932018-11-15 10:08:04 +0800326struct msdc_compatible {
327 u8 clk_div_bits;
328 bool pad_tune0;
329 bool async_fifo;
330 bool data_tune;
331 bool busy_check;
332 bool stop_clk_fix;
333 bool enhance_rx;
Weijie Gao2a505cd2020-11-12 16:37:02 +0800334 bool builtin_pad_ctrl;
335 bool default_pad_dly;
Weijie Gaod24b6932018-11-15 10:08:04 +0800336};
337
338struct msdc_delay_phase {
339 u8 maxlen;
340 u8 start;
341 u8 final_phase;
342};
343
344struct msdc_plat {
345 struct mmc_config cfg;
346 struct mmc mmc;
347};
348
349struct msdc_tune_para {
350 u32 iocon;
351 u32 pad_tune;
mingming lee69373bd2019-11-07 19:28:42 +0800352 u32 pad_cmd_tune;
Weijie Gaod24b6932018-11-15 10:08:04 +0800353};
354
355struct msdc_host {
356 struct mtk_sd_regs *base;
mingming lee3b0397d2019-12-31 11:29:24 +0800357 struct msdc_top_regs *top_base;
Weijie Gaod24b6932018-11-15 10:08:04 +0800358 struct mmc *mmc;
359
360 struct msdc_compatible *dev_comp;
361
362 struct clk src_clk; /* for SD/MMC bus clock */
Fabien Parentba077e52019-03-24 16:46:32 +0100363 struct clk src_clk_cg; /* optional, MSDC source clock control gate */
Weijie Gaod24b6932018-11-15 10:08:04 +0800364 struct clk h_clk; /* MSDC core clock */
365
366 u32 src_clk_freq; /* source clock */
367 u32 mclk; /* mmc framework required bus clock */
368 u32 sclk; /* actual calculated bus clock */
369
370 /* operation timeout clocks */
371 u32 timeout_ns;
372 u32 timeout_clks;
373
374 /* tuning options */
375 u32 hs400_ds_delay;
376 u32 hs200_cmd_int_delay;
377 u32 hs200_write_int_delay;
378 u32 latch_ck;
379 u32 r_smpl; /* sample edge */
380 bool hs400_mode;
381
382 /* whether to use gpio detection or built-in hw detection */
383 bool builtin_cd;
Weijie Gao77ebea22019-09-25 17:45:38 +0800384 bool cd_active_high;
Weijie Gaod24b6932018-11-15 10:08:04 +0800385
386 /* card detection / write protection GPIOs */
Fabien Parentb5096f12019-03-24 16:46:34 +0100387#if CONFIG_IS_ENABLED(DM_GPIO)
Weijie Gaod24b6932018-11-15 10:08:04 +0800388 struct gpio_desc gpio_wp;
389 struct gpio_desc gpio_cd;
390#endif
391
392 uint last_resp_type;
393 uint last_data_write;
394
395 enum bus_mode timing;
396
397 struct msdc_tune_para def_tune_para;
398 struct msdc_tune_para saved_tune_para;
399};
400
401static void msdc_reset_hw(struct msdc_host *host)
402{
403 u32 reg;
404
405 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_RST);
406
407 readl_poll_timeout(&host->base->msdc_cfg, reg,
408 !(reg & MSDC_CFG_RST), 1000000);
409}
410
411static void msdc_fifo_clr(struct msdc_host *host)
412{
413 u32 reg;
414
415 setbits_le32(&host->base->msdc_fifocs, MSDC_FIFOCS_CLR);
416
417 readl_poll_timeout(&host->base->msdc_fifocs, reg,
418 !(reg & MSDC_FIFOCS_CLR), 1000000);
419}
420
421static u32 msdc_fifo_rx_bytes(struct msdc_host *host)
422{
423 return (readl(&host->base->msdc_fifocs) &
424 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S;
425}
426
427static u32 msdc_fifo_tx_bytes(struct msdc_host *host)
428{
429 return (readl(&host->base->msdc_fifocs) &
430 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S;
431}
432
433static u32 msdc_cmd_find_resp(struct msdc_host *host, struct mmc_cmd *cmd)
434{
435 u32 resp;
436
437 switch (cmd->resp_type) {
438 /* Actually, R1, R5, R6, R7 are the same */
439 case MMC_RSP_R1:
440 resp = 0x1;
441 break;
442 case MMC_RSP_R1b:
443 resp = 0x7;
444 break;
445 case MMC_RSP_R2:
446 resp = 0x2;
447 break;
448 case MMC_RSP_R3:
449 resp = 0x3;
450 break;
451 case MMC_RSP_NONE:
452 default:
453 resp = 0x0;
454 break;
455 }
456
457 return resp;
458}
459
460static u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
461 struct mmc_cmd *cmd,
462 struct mmc_data *data)
463{
464 u32 opcode = cmd->cmdidx;
465 u32 resp_type = msdc_cmd_find_resp(host, cmd);
466 uint blocksize = 0;
467 u32 dtype = 0;
468 u32 rawcmd = 0;
469
470 switch (opcode) {
471 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
472 case MMC_CMD_READ_MULTIPLE_BLOCK:
473 dtype = 2;
474 break;
475 case MMC_CMD_WRITE_SINGLE_BLOCK:
476 case MMC_CMD_READ_SINGLE_BLOCK:
477 case SD_CMD_APP_SEND_SCR:
mingming lee69373bd2019-11-07 19:28:42 +0800478 case MMC_CMD_SEND_TUNING_BLOCK:
479 case MMC_CMD_SEND_TUNING_BLOCK_HS200:
Weijie Gaod24b6932018-11-15 10:08:04 +0800480 dtype = 1;
481 break;
482 case SD_CMD_SWITCH_FUNC: /* same as MMC_CMD_SWITCH */
483 case SD_CMD_SEND_IF_COND: /* same as MMC_CMD_SEND_EXT_CSD */
484 case SD_CMD_APP_SD_STATUS: /* same as MMC_CMD_SEND_STATUS */
485 if (data)
486 dtype = 1;
487 }
488
489 if (data) {
490 if (data->flags == MMC_DATA_WRITE)
491 rawcmd |= SDC_CMD_WR;
492
493 if (data->blocks > 1)
494 dtype = 2;
495
496 blocksize = data->blocksize;
497 }
498
499 rawcmd |= ((opcode << SDC_CMD_CMD_S) & SDC_CMD_CMD_M) |
500 ((resp_type << SDC_CMD_RSPTYP_S) & SDC_CMD_RSPTYP_M) |
501 ((blocksize << SDC_CMD_BLK_LEN_S) & SDC_CMD_BLK_LEN_M) |
502 ((dtype << SDC_CMD_DTYPE_S) & SDC_CMD_DTYPE_M);
503
504 if (opcode == MMC_CMD_STOP_TRANSMISSION)
505 rawcmd |= SDC_CMD_STOP;
506
507 return rawcmd;
508}
509
510static int msdc_cmd_done(struct msdc_host *host, int events,
511 struct mmc_cmd *cmd)
512{
513 u32 *rsp = cmd->response;
514 int ret = 0;
515
516 if (cmd->resp_type & MMC_RSP_PRESENT) {
517 if (cmd->resp_type & MMC_RSP_136) {
518 rsp[0] = readl(&host->base->sdc_resp[3]);
519 rsp[1] = readl(&host->base->sdc_resp[2]);
520 rsp[2] = readl(&host->base->sdc_resp[1]);
521 rsp[3] = readl(&host->base->sdc_resp[0]);
522 } else {
523 rsp[0] = readl(&host->base->sdc_resp[0]);
524 }
525 }
526
527 if (!(events & MSDC_INT_CMDRDY)) {
528 if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
529 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
530 /*
531 * should not clear fifo/interrupt as the tune data
532 * may have alreay come.
533 */
534 msdc_reset_hw(host);
535
536 if (events & MSDC_INT_CMDTMO)
537 ret = -ETIMEDOUT;
538 else
539 ret = -EIO;
540 }
541
542 return ret;
543}
544
545static bool msdc_cmd_is_ready(struct msdc_host *host)
546{
547 int ret;
548 u32 reg;
549
550 /* The max busy time we can endure is 20ms */
551 ret = readl_poll_timeout(&host->base->sdc_sts, reg,
552 !(reg & SDC_STS_CMDBUSY), 20000);
553
554 if (ret) {
555 pr_err("CMD bus busy detected\n");
556 msdc_reset_hw(host);
557 return false;
558 }
559
560 if (host->last_resp_type == MMC_RSP_R1b && host->last_data_write) {
561 ret = readl_poll_timeout(&host->base->msdc_ps, reg,
562 reg & MSDC_PS_DAT0, 1000000);
563
564 if (ret) {
565 pr_err("Card stuck in programming state!\n");
566 msdc_reset_hw(host);
567 return false;
568 }
569 }
570
571 return true;
572}
573
574static int msdc_start_command(struct msdc_host *host, struct mmc_cmd *cmd,
575 struct mmc_data *data)
576{
577 u32 rawcmd;
578 u32 status;
579 u32 blocks = 0;
580 int ret;
581
582 if (!msdc_cmd_is_ready(host))
583 return -EIO;
584
mingming lee69373bd2019-11-07 19:28:42 +0800585 if ((readl(&host->base->msdc_fifocs) &
586 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S ||
587 (readl(&host->base->msdc_fifocs) &
588 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S) {
589 pr_err("TX/RX FIFO non-empty before start of IO. Reset\n");
590 msdc_reset_hw(host);
591 }
592
Weijie Gaod24b6932018-11-15 10:08:04 +0800593 msdc_fifo_clr(host);
594
595 host->last_resp_type = cmd->resp_type;
596 host->last_data_write = 0;
597
598 rawcmd = msdc_cmd_prepare_raw_cmd(host, cmd, data);
599
600 if (data)
601 blocks = data->blocks;
602
603 writel(CMD_INTS_MASK, &host->base->msdc_int);
mingming leefb80eaa2019-12-31 11:29:25 +0800604 writel(DATA_INTS_MASK, &host->base->msdc_int);
Weijie Gaod24b6932018-11-15 10:08:04 +0800605 writel(blocks, &host->base->sdc_blk_num);
606 writel(cmd->cmdarg, &host->base->sdc_arg);
607 writel(rawcmd, &host->base->sdc_cmd);
608
609 ret = readl_poll_timeout(&host->base->msdc_int, status,
610 status & CMD_INTS_MASK, 1000000);
611
612 if (ret)
613 status = MSDC_INT_CMDTMO;
614
615 return msdc_cmd_done(host, status, cmd);
616}
617
618static void msdc_fifo_read(struct msdc_host *host, u8 *buf, u32 size)
619{
620 u32 *wbuf;
621
622 while ((size_t)buf % 4) {
623 *buf++ = readb(&host->base->msdc_rxdata);
624 size--;
625 }
626
627 wbuf = (u32 *)buf;
628 while (size >= 4) {
629 *wbuf++ = readl(&host->base->msdc_rxdata);
630 size -= 4;
631 }
632
633 buf = (u8 *)wbuf;
634 while (size) {
635 *buf++ = readb(&host->base->msdc_rxdata);
636 size--;
637 }
638}
639
640static void msdc_fifo_write(struct msdc_host *host, const u8 *buf, u32 size)
641{
642 const u32 *wbuf;
643
644 while ((size_t)buf % 4) {
645 writeb(*buf++, &host->base->msdc_txdata);
646 size--;
647 }
648
649 wbuf = (const u32 *)buf;
650 while (size >= 4) {
651 writel(*wbuf++, &host->base->msdc_txdata);
652 size -= 4;
653 }
654
655 buf = (const u8 *)wbuf;
656 while (size) {
657 writeb(*buf++, &host->base->msdc_txdata);
658 size--;
659 }
660}
661
662static int msdc_pio_read(struct msdc_host *host, u8 *ptr, u32 size)
663{
664 u32 status;
665 u32 chksz;
666 int ret = 0;
667
668 while (1) {
669 status = readl(&host->base->msdc_int);
670 writel(status, &host->base->msdc_int);
671 status &= DATA_INTS_MASK;
672
673 if (status & MSDC_INT_DATCRCERR) {
674 ret = -EIO;
675 break;
676 }
677
678 if (status & MSDC_INT_DATTMO) {
679 ret = -ETIMEDOUT;
680 break;
681 }
682
Fabien Parent924ed342019-01-17 18:06:00 +0100683 chksz = min(size, (u32)MSDC_FIFO_SIZE);
684
685 if (msdc_fifo_rx_bytes(host) >= chksz) {
686 msdc_fifo_read(host, ptr, chksz);
687 ptr += chksz;
688 size -= chksz;
689 }
690
Weijie Gaod24b6932018-11-15 10:08:04 +0800691 if (status & MSDC_INT_XFER_COMPL) {
692 if (size) {
693 pr_err("data not fully read\n");
694 ret = -EIO;
695 }
696
697 break;
698 }
Fabien Parent924ed342019-01-17 18:06:00 +0100699}
Weijie Gaod24b6932018-11-15 10:08:04 +0800700
701 return ret;
702}
703
704static int msdc_pio_write(struct msdc_host *host, const u8 *ptr, u32 size)
705{
706 u32 status;
707 u32 chksz;
708 int ret = 0;
709
710 while (1) {
711 status = readl(&host->base->msdc_int);
712 writel(status, &host->base->msdc_int);
713 status &= DATA_INTS_MASK;
714
715 if (status & MSDC_INT_DATCRCERR) {
716 ret = -EIO;
717 break;
718 }
719
720 if (status & MSDC_INT_DATTMO) {
721 ret = -ETIMEDOUT;
722 break;
723 }
724
725 if (status & MSDC_INT_XFER_COMPL) {
726 if (size) {
727 pr_err("data not fully written\n");
728 ret = -EIO;
729 }
730
731 break;
732 }
733
734 chksz = min(size, (u32)MSDC_FIFO_SIZE);
735
736 if (MSDC_FIFO_SIZE - msdc_fifo_tx_bytes(host) >= chksz) {
737 msdc_fifo_write(host, ptr, chksz);
738 ptr += chksz;
739 size -= chksz;
740 }
741 }
742
743 return ret;
744}
745
746static int msdc_start_data(struct msdc_host *host, struct mmc_data *data)
747{
748 u32 size;
749 int ret;
750
751 if (data->flags == MMC_DATA_WRITE)
752 host->last_data_write = 1;
753
Weijie Gaod24b6932018-11-15 10:08:04 +0800754 size = data->blocks * data->blocksize;
755
756 if (data->flags == MMC_DATA_WRITE)
757 ret = msdc_pio_write(host, (const u8 *)data->src, size);
758 else
759 ret = msdc_pio_read(host, (u8 *)data->dest, size);
760
761 if (ret) {
762 msdc_reset_hw(host);
763 msdc_fifo_clr(host);
764 }
765
766 return ret;
767}
768
769static int msdc_ops_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
770 struct mmc_data *data)
771{
772 struct msdc_host *host = dev_get_priv(dev);
mingming lee69373bd2019-11-07 19:28:42 +0800773 int cmd_ret, data_ret;
Weijie Gaod24b6932018-11-15 10:08:04 +0800774
mingming lee69373bd2019-11-07 19:28:42 +0800775 cmd_ret = msdc_start_command(host, cmd, data);
776 if (cmd_ret &&
777 !(cmd_ret == -EIO &&
778 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
779 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)))
780 return cmd_ret;
Weijie Gaod24b6932018-11-15 10:08:04 +0800781
mingming lee69373bd2019-11-07 19:28:42 +0800782 if (data) {
783 data_ret = msdc_start_data(host, data);
784 if (cmd_ret)
785 return cmd_ret;
786 else
787 return data_ret;
788 }
Weijie Gaod24b6932018-11-15 10:08:04 +0800789
790 return 0;
791}
792
793static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
794{
Weijie Gaof875a122020-11-12 16:36:57 +0800795 u32 timeout, clk_ns, shift = SCLK_CYCLES_SHIFT;
Weijie Gaod24b6932018-11-15 10:08:04 +0800796 u32 mode = 0;
797
798 host->timeout_ns = ns;
799 host->timeout_clks = clks;
800
801 if (host->sclk == 0) {
802 timeout = 0;
803 } else {
804 clk_ns = 1000000000UL / host->sclk;
805 timeout = (ns + clk_ns - 1) / clk_ns + clks;
806 /* unit is 1048576 sclk cycles */
Weijie Gao3c92a952019-09-25 17:45:37 +0800807 timeout = (timeout + (0x1 << shift) - 1) >> shift;
Weijie Gaod24b6932018-11-15 10:08:04 +0800808 if (host->dev_comp->clk_div_bits == 8)
809 mode = (readl(&host->base->msdc_cfg) &
810 MSDC_CFG_CKMOD_M) >> MSDC_CFG_CKMOD_S;
811 else
812 mode = (readl(&host->base->msdc_cfg) &
813 MSDC_CFG_CKMOD_EXT_M) >> MSDC_CFG_CKMOD_EXT_S;
814 /* DDR mode will double the clk cycles for data timeout */
815 timeout = mode >= 2 ? timeout * 2 : timeout;
816 timeout = timeout > 1 ? timeout - 1 : 0;
817 timeout = timeout > 255 ? 255 : timeout;
818 }
819
820 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
821 timeout << SDC_CFG_DTOC_S);
822}
823
824static void msdc_set_buswidth(struct msdc_host *host, u32 width)
825{
826 u32 val = readl(&host->base->sdc_cfg);
827
828 val &= ~SDC_CFG_BUSWIDTH_M;
829
830 switch (width) {
831 default:
832 case 1:
833 val |= (MSDC_BUS_1BITS << SDC_CFG_BUSWIDTH_S);
834 break;
835 case 4:
836 val |= (MSDC_BUS_4BITS << SDC_CFG_BUSWIDTH_S);
837 break;
838 case 8:
839 val |= (MSDC_BUS_8BITS << SDC_CFG_BUSWIDTH_S);
840 break;
841 }
842
843 writel(val, &host->base->sdc_cfg);
844}
845
Sean Andersone1ce7902020-09-15 10:44:47 -0400846static void msdc_set_mclk(struct udevice *dev,
847 struct msdc_host *host, enum bus_mode timing, u32 hz)
Weijie Gaod24b6932018-11-15 10:08:04 +0800848{
849 u32 mode;
850 u32 div;
851 u32 sclk;
852 u32 reg;
853
854 if (!hz) {
855 host->mclk = 0;
856 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
857 return;
858 }
859
860 if (host->dev_comp->clk_div_bits == 8)
861 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_HS400_CK_MODE);
862 else
863 clrbits_le32(&host->base->msdc_cfg,
864 MSDC_CFG_HS400_CK_MODE_EXT);
865
866 if (timing == UHS_DDR50 || timing == MMC_DDR_52 ||
867 timing == MMC_HS_400) {
868 if (timing == MMC_HS_400)
869 mode = 0x3;
870 else
871 mode = 0x2; /* ddr mode and use divisor */
872
873 if (hz >= (host->src_clk_freq >> 2)) {
874 div = 0; /* mean div = 1/4 */
875 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
876 } else {
877 div = (host->src_clk_freq + ((hz << 2) - 1)) /
878 (hz << 2);
879 sclk = (host->src_clk_freq >> 2) / div;
880 div = (div >> 1);
881 }
882
883 if (timing == MMC_HS_400 && hz >= (host->src_clk_freq >> 1)) {
884 if (host->dev_comp->clk_div_bits == 8)
885 setbits_le32(&host->base->msdc_cfg,
886 MSDC_CFG_HS400_CK_MODE);
887 else
888 setbits_le32(&host->base->msdc_cfg,
889 MSDC_CFG_HS400_CK_MODE_EXT);
890
891 sclk = host->src_clk_freq >> 1;
892 div = 0; /* div is ignore when bit18 is set */
893 }
894 } else if (hz >= host->src_clk_freq) {
895 mode = 0x1; /* no divisor */
896 div = 0;
897 sclk = host->src_clk_freq;
898 } else {
899 mode = 0x0; /* use divisor */
900 if (hz >= (host->src_clk_freq >> 1)) {
901 div = 0; /* mean div = 1/2 */
902 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
903 } else {
904 div = (host->src_clk_freq + ((hz << 2) - 1)) /
905 (hz << 2);
906 sclk = (host->src_clk_freq >> 2) / div;
907 }
908 }
909
910 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
911
912 if (host->dev_comp->clk_div_bits == 8) {
913 div = min(div, (u32)(MSDC_CFG_CKDIV_M >> MSDC_CFG_CKDIV_S));
914 clrsetbits_le32(&host->base->msdc_cfg,
915 MSDC_CFG_CKMOD_M | MSDC_CFG_CKDIV_M,
916 (mode << MSDC_CFG_CKMOD_S) |
917 (div << MSDC_CFG_CKDIV_S));
918 } else {
919 div = min(div, (u32)(MSDC_CFG_CKDIV_EXT_M >>
920 MSDC_CFG_CKDIV_EXT_S));
921 clrsetbits_le32(&host->base->msdc_cfg,
922 MSDC_CFG_CKMOD_EXT_M | MSDC_CFG_CKDIV_EXT_M,
923 (mode << MSDC_CFG_CKMOD_EXT_S) |
924 (div << MSDC_CFG_CKDIV_EXT_S));
925 }
926
927 readl_poll_timeout(&host->base->msdc_cfg, reg,
928 reg & MSDC_CFG_CKSTB, 1000000);
929
930 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
931 host->sclk = sclk;
932 host->mclk = hz;
933 host->timing = timing;
934
935 /* needed because clk changed. */
936 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
937
938 /*
939 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
940 * tune result of hs200/200Mhz is not suitable for 50Mhz
941 */
942 if (host->sclk <= 52000000) {
943 writel(host->def_tune_para.iocon, &host->base->msdc_iocon);
944 writel(host->def_tune_para.pad_tune,
945 &host->base->pad_tune);
946 } else {
947 writel(host->saved_tune_para.iocon, &host->base->msdc_iocon);
948 writel(host->saved_tune_para.pad_tune,
949 &host->base->pad_tune);
950 }
951
952 dev_dbg(dev, "sclk: %d, timing: %d\n", host->sclk, timing);
953}
954
955static int msdc_ops_set_ios(struct udevice *dev)
956{
Simon Glassc69cda22020-12-03 16:55:20 -0700957 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +0800958 struct msdc_host *host = dev_get_priv(dev);
959 struct mmc *mmc = &plat->mmc;
960 uint clock = mmc->clock;
961
962 msdc_set_buswidth(host, mmc->bus_width);
963
964 if (mmc->clk_disable)
965 clock = 0;
966 else if (clock < mmc->cfg->f_min)
967 clock = mmc->cfg->f_min;
968
969 if (host->mclk != clock || host->timing != mmc->selected_mode)
Sean Andersone1ce7902020-09-15 10:44:47 -0400970 msdc_set_mclk(dev, host, mmc->selected_mode, clock);
Weijie Gaod24b6932018-11-15 10:08:04 +0800971
972 return 0;
973}
974
975static int msdc_ops_get_cd(struct udevice *dev)
976{
977 struct msdc_host *host = dev_get_priv(dev);
978 u32 val;
979
980 if (host->builtin_cd) {
981 val = readl(&host->base->msdc_ps);
Weijie Gao77ebea22019-09-25 17:45:38 +0800982 val = !!(val & MSDC_PS_CDSTS);
983
984 return !val ^ host->cd_active_high;
Weijie Gaod24b6932018-11-15 10:08:04 +0800985 }
986
Fabien Parentb5096f12019-03-24 16:46:34 +0100987#if CONFIG_IS_ENABLED(DM_GPIO)
Weijie Gaod24b6932018-11-15 10:08:04 +0800988 if (!host->gpio_cd.dev)
989 return 1;
990
991 return dm_gpio_get_value(&host->gpio_cd);
992#else
993 return 1;
994#endif
995}
996
997static int msdc_ops_get_wp(struct udevice *dev)
998{
Fabien Parentb5096f12019-03-24 16:46:34 +0100999#if CONFIG_IS_ENABLED(DM_GPIO)
Weijie Gaod24b6932018-11-15 10:08:04 +08001000 struct msdc_host *host = dev_get_priv(dev);
1001
Weijie Gaod24b6932018-11-15 10:08:04 +08001002 if (!host->gpio_wp.dev)
1003 return 0;
1004
1005 return !dm_gpio_get_value(&host->gpio_wp);
1006#else
1007 return 0;
1008#endif
1009}
1010
1011#ifdef MMC_SUPPORTS_TUNING
1012static u32 test_delay_bit(u32 delay, u32 bit)
1013{
1014 bit %= PAD_DELAY_MAX;
1015 return delay & (1 << bit);
1016}
1017
1018static int get_delay_len(u32 delay, u32 start_bit)
1019{
1020 int i;
1021
1022 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
1023 if (test_delay_bit(delay, start_bit + i) == 0)
1024 return i;
1025 }
1026
1027 return PAD_DELAY_MAX - start_bit;
1028}
1029
Sean Andersone1ce7902020-09-15 10:44:47 -04001030static struct msdc_delay_phase get_best_delay(struct udevice *dev,
1031 struct msdc_host *host, u32 delay)
Weijie Gaod24b6932018-11-15 10:08:04 +08001032{
1033 int start = 0, len = 0;
1034 int start_final = 0, len_final = 0;
1035 u8 final_phase = 0xff;
1036 struct msdc_delay_phase delay_phase = { 0, };
1037
1038 if (delay == 0) {
1039 dev_err(dev, "phase error: [map:%x]\n", delay);
1040 delay_phase.final_phase = final_phase;
1041 return delay_phase;
1042 }
1043
1044 while (start < PAD_DELAY_MAX) {
1045 len = get_delay_len(delay, start);
1046 if (len_final < len) {
1047 start_final = start;
1048 len_final = len;
1049 }
1050
1051 start += len ? len : 1;
1052 if (len >= 12 && start_final < 4)
1053 break;
1054 }
1055
1056 /* The rule is to find the smallest delay cell */
1057 if (start_final == 0)
1058 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
1059 else
1060 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
1061
1062 dev_info(dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
1063 delay, len_final, final_phase);
1064
1065 delay_phase.maxlen = len_final;
1066 delay_phase.start = start_final;
1067 delay_phase.final_phase = final_phase;
1068 return delay_phase;
1069}
1070
mingming lee3b0397d2019-12-31 11:29:24 +08001071static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value)
1072{
1073 void __iomem *tune_reg = &host->base->pad_tune;
1074
1075 if (host->dev_comp->pad_tune0)
1076 tune_reg = &host->base->pad_tune0;
1077
1078 if (host->top_base)
1079 clrsetbits_le32(&host->top_base->emmc_top_cmd, PAD_CMD_RXDLY,
1080 value << PAD_CMD_RXDLY_S);
1081 else
1082 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1083 value << MSDC_PAD_TUNE_CMDRDLY_S);
1084}
1085
1086static inline void msdc_set_data_delay(struct msdc_host *host, u32 value)
1087{
1088 void __iomem *tune_reg = &host->base->pad_tune;
1089
1090 if (host->dev_comp->pad_tune0)
1091 tune_reg = &host->base->pad_tune0;
1092
1093 if (host->top_base)
1094 clrsetbits_le32(&host->top_base->emmc_top_control,
1095 PAD_DAT_RD_RXDLY, value << PAD_DAT_RD_RXDLY_S);
1096 else
1097 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1098 value << MSDC_PAD_TUNE_DATRRDLY_S);
1099}
1100
mingming lee69373bd2019-11-07 19:28:42 +08001101static int hs400_tune_response(struct udevice *dev, u32 opcode)
1102{
Simon Glassc69cda22020-12-03 16:55:20 -07001103 struct msdc_plat *plat = dev_get_plat(dev);
mingming lee69373bd2019-11-07 19:28:42 +08001104 struct msdc_host *host = dev_get_priv(dev);
1105 struct mmc *mmc = &plat->mmc;
1106 u32 cmd_delay = 0;
1107 struct msdc_delay_phase final_cmd_delay = { 0, };
1108 u8 final_delay;
1109 void __iomem *tune_reg = &host->base->pad_cmd_tune;
1110 int cmd_err;
1111 int i, j;
1112
1113 setbits_le32(&host->base->pad_cmd_tune, BIT(0));
1114
1115 if (mmc->selected_mode == MMC_HS_200 ||
1116 mmc->selected_mode == UHS_SDR104)
1117 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1118 host->hs200_cmd_int_delay <<
1119 MSDC_PAD_TUNE_CMDRRDLY_S);
1120
1121 if (host->r_smpl)
1122 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1123 else
1124 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1125
1126 for (i = 0; i < PAD_DELAY_MAX; i++) {
1127 clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3,
1128 i << PAD_CMD_TUNE_RX_DLY3_S);
1129
1130 for (j = 0; j < 3; j++) {
1131 mmc_send_tuning(mmc, opcode, &cmd_err);
1132 if (!cmd_err) {
1133 cmd_delay |= (1 << i);
1134 } else {
1135 cmd_delay &= ~(1 << i);
1136 break;
1137 }
1138 }
1139 }
1140
Sean Andersone1ce7902020-09-15 10:44:47 -04001141 final_cmd_delay = get_best_delay(dev, host, cmd_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001142 clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3,
1143 final_cmd_delay.final_phase <<
1144 PAD_CMD_TUNE_RX_DLY3_S);
1145 final_delay = final_cmd_delay.final_phase;
1146
mingming lee3b0397d2019-12-31 11:29:24 +08001147 dev_info(dev, "Final cmd pad delay: %x\n", final_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001148 return final_delay == 0xff ? -EIO : 0;
1149}
1150
Weijie Gaod24b6932018-11-15 10:08:04 +08001151static int msdc_tune_response(struct udevice *dev, u32 opcode)
1152{
Simon Glassc69cda22020-12-03 16:55:20 -07001153 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001154 struct msdc_host *host = dev_get_priv(dev);
1155 struct mmc *mmc = &plat->mmc;
1156 u32 rise_delay = 0, fall_delay = 0;
1157 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1158 struct msdc_delay_phase internal_delay_phase;
1159 u8 final_delay, final_maxlen;
1160 u32 internal_delay = 0;
1161 void __iomem *tune_reg = &host->base->pad_tune;
1162 int cmd_err;
1163 int i, j;
1164
1165 if (host->dev_comp->pad_tune0)
1166 tune_reg = &host->base->pad_tune0;
1167
1168 if (mmc->selected_mode == MMC_HS_200 ||
1169 mmc->selected_mode == UHS_SDR104)
1170 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1171 host->hs200_cmd_int_delay <<
1172 MSDC_PAD_TUNE_CMDRRDLY_S);
1173
1174 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1175
1176 for (i = 0; i < PAD_DELAY_MAX; i++) {
1177 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1178 i << MSDC_PAD_TUNE_CMDRDLY_S);
1179
1180 for (j = 0; j < 3; j++) {
1181 mmc_send_tuning(mmc, opcode, &cmd_err);
1182 if (!cmd_err) {
1183 rise_delay |= (1 << i);
1184 } else {
1185 rise_delay &= ~(1 << i);
1186 break;
1187 }
1188 }
1189 }
1190
Sean Andersone1ce7902020-09-15 10:44:47 -04001191 final_rise_delay = get_best_delay(dev, host, rise_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001192 /* if rising edge has enough margin, do not scan falling edge */
1193 if (final_rise_delay.maxlen >= 12 ||
1194 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1195 goto skip_fall;
1196
1197 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1198 for (i = 0; i < PAD_DELAY_MAX; i++) {
1199 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1200 i << MSDC_PAD_TUNE_CMDRDLY_S);
1201
1202 for (j = 0; j < 3; j++) {
1203 mmc_send_tuning(mmc, opcode, &cmd_err);
1204 if (!cmd_err) {
1205 fall_delay |= (1 << i);
1206 } else {
1207 fall_delay &= ~(1 << i);
1208 break;
1209 }
1210 }
1211 }
1212
Sean Andersone1ce7902020-09-15 10:44:47 -04001213 final_fall_delay = get_best_delay(dev, host, fall_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001214
1215skip_fall:
1216 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1217 if (final_maxlen == final_rise_delay.maxlen) {
1218 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1219 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1220 final_rise_delay.final_phase <<
1221 MSDC_PAD_TUNE_CMDRDLY_S);
1222 final_delay = final_rise_delay.final_phase;
1223 } else {
1224 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1225 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1226 final_fall_delay.final_phase <<
1227 MSDC_PAD_TUNE_CMDRDLY_S);
1228 final_delay = final_fall_delay.final_phase;
1229 }
1230
1231 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
1232 goto skip_internal;
1233
1234 for (i = 0; i < PAD_DELAY_MAX; i++) {
1235 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1236 i << MSDC_PAD_TUNE_CMDRRDLY_S);
1237
1238 mmc_send_tuning(mmc, opcode, &cmd_err);
1239 if (!cmd_err)
1240 internal_delay |= (1 << i);
1241 }
1242
Fabien Parent0d675762020-10-15 18:38:18 +02001243 dev_dbg(dev, "Final internal delay: 0x%x\n", internal_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001244
Sean Andersone1ce7902020-09-15 10:44:47 -04001245 internal_delay_phase = get_best_delay(dev, host, internal_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001246 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1247 internal_delay_phase.final_phase <<
1248 MSDC_PAD_TUNE_CMDRRDLY_S);
1249
1250skip_internal:
Fabien Parent0d675762020-10-15 18:38:18 +02001251 dev_dbg(dev, "Final cmd pad delay: %x\n", final_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001252 return final_delay == 0xff ? -EIO : 0;
1253}
1254
1255static int msdc_tune_data(struct udevice *dev, u32 opcode)
1256{
Simon Glassc69cda22020-12-03 16:55:20 -07001257 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001258 struct msdc_host *host = dev_get_priv(dev);
1259 struct mmc *mmc = &plat->mmc;
1260 u32 rise_delay = 0, fall_delay = 0;
1261 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1262 u8 final_delay, final_maxlen;
1263 void __iomem *tune_reg = &host->base->pad_tune;
1264 int cmd_err;
1265 int i, ret;
1266
1267 if (host->dev_comp->pad_tune0)
1268 tune_reg = &host->base->pad_tune0;
1269
1270 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1271 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1272
1273 for (i = 0; i < PAD_DELAY_MAX; i++) {
1274 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1275 i << MSDC_PAD_TUNE_DATRRDLY_S);
1276
1277 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1278 if (!ret) {
1279 rise_delay |= (1 << i);
1280 } else if (cmd_err) {
1281 /* in this case, retune response is needed */
1282 ret = msdc_tune_response(dev, opcode);
1283 if (ret)
1284 break;
1285 }
1286 }
1287
Sean Andersone1ce7902020-09-15 10:44:47 -04001288 final_rise_delay = get_best_delay(dev, host, rise_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001289 if (final_rise_delay.maxlen >= 12 ||
1290 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1291 goto skip_fall;
1292
1293 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1294 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1295
1296 for (i = 0; i < PAD_DELAY_MAX; i++) {
1297 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1298 i << MSDC_PAD_TUNE_DATRRDLY_S);
1299
1300 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1301 if (!ret) {
1302 fall_delay |= (1 << i);
1303 } else if (cmd_err) {
1304 /* in this case, retune response is needed */
1305 ret = msdc_tune_response(dev, opcode);
1306 if (ret)
1307 break;
1308 }
1309 }
1310
Sean Andersone1ce7902020-09-15 10:44:47 -04001311 final_fall_delay = get_best_delay(dev, host, fall_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001312
1313skip_fall:
1314 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1315 if (final_maxlen == final_rise_delay.maxlen) {
1316 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1317 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1318 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1319 final_rise_delay.final_phase <<
1320 MSDC_PAD_TUNE_DATRRDLY_S);
1321 final_delay = final_rise_delay.final_phase;
1322 } else {
1323 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1324 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1325 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1326 final_fall_delay.final_phase <<
1327 MSDC_PAD_TUNE_DATRRDLY_S);
1328 final_delay = final_fall_delay.final_phase;
1329 }
1330
1331 if (mmc->selected_mode == MMC_HS_200 ||
1332 mmc->selected_mode == UHS_SDR104)
1333 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATWRDLY_M,
1334 host->hs200_write_int_delay <<
1335 MSDC_PAD_TUNE_DATWRDLY_S);
1336
Fabien Parent0d675762020-10-15 18:38:18 +02001337 dev_dbg(dev, "Final data pad delay: %x\n", final_delay);
Weijie Gaod24b6932018-11-15 10:08:04 +08001338
1339 return final_delay == 0xff ? -EIO : 0;
1340}
1341
mingming lee69373bd2019-11-07 19:28:42 +08001342/*
1343 * MSDC IP which supports data tune + async fifo can do CMD/DAT tune
1344 * together, which can save the tuning time.
1345 */
1346static int msdc_tune_together(struct udevice *dev, u32 opcode)
1347{
Simon Glassc69cda22020-12-03 16:55:20 -07001348 struct msdc_plat *plat = dev_get_plat(dev);
mingming lee69373bd2019-11-07 19:28:42 +08001349 struct msdc_host *host = dev_get_priv(dev);
1350 struct mmc *mmc = &plat->mmc;
1351 u32 rise_delay = 0, fall_delay = 0;
1352 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1353 u8 final_delay, final_maxlen;
mingming lee69373bd2019-11-07 19:28:42 +08001354 int i, ret;
1355
mingming lee69373bd2019-11-07 19:28:42 +08001356 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1357 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1358
1359 for (i = 0; i < PAD_DELAY_MAX; i++) {
mingming lee3b0397d2019-12-31 11:29:24 +08001360 msdc_set_cmd_delay(host, i);
1361 msdc_set_data_delay(host, i);
mingming lee69373bd2019-11-07 19:28:42 +08001362 ret = mmc_send_tuning(mmc, opcode, NULL);
1363 if (!ret)
1364 rise_delay |= (1 << i);
1365 }
1366
Sean Andersone1ce7902020-09-15 10:44:47 -04001367 final_rise_delay = get_best_delay(dev, host, rise_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001368 if (final_rise_delay.maxlen >= 12 ||
1369 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1370 goto skip_fall;
1371
1372 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1373 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1374
1375 for (i = 0; i < PAD_DELAY_MAX; i++) {
mingming lee3b0397d2019-12-31 11:29:24 +08001376 msdc_set_cmd_delay(host, i);
1377 msdc_set_data_delay(host, i);
mingming lee69373bd2019-11-07 19:28:42 +08001378 ret = mmc_send_tuning(mmc, opcode, NULL);
1379 if (!ret)
1380 fall_delay |= (1 << i);
1381 }
1382
Sean Andersone1ce7902020-09-15 10:44:47 -04001383 final_fall_delay = get_best_delay(dev, host, fall_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001384
1385skip_fall:
1386 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1387 if (final_maxlen == final_rise_delay.maxlen) {
1388 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1389 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
mingming lee69373bd2019-11-07 19:28:42 +08001390 final_delay = final_rise_delay.final_phase;
1391 } else {
1392 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1393 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
mingming lee69373bd2019-11-07 19:28:42 +08001394 final_delay = final_fall_delay.final_phase;
1395 }
1396
mingming lee3b0397d2019-12-31 11:29:24 +08001397 msdc_set_cmd_delay(host, final_delay);
1398 msdc_set_data_delay(host, final_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001399
mingming lee3b0397d2019-12-31 11:29:24 +08001400 dev_info(dev, "Final pad delay: %x\n", final_delay);
mingming lee69373bd2019-11-07 19:28:42 +08001401 return final_delay == 0xff ? -EIO : 0;
1402}
1403
Weijie Gaod24b6932018-11-15 10:08:04 +08001404static int msdc_execute_tuning(struct udevice *dev, uint opcode)
1405{
Simon Glassc69cda22020-12-03 16:55:20 -07001406 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001407 struct msdc_host *host = dev_get_priv(dev);
1408 struct mmc *mmc = &plat->mmc;
mingming lee69373bd2019-11-07 19:28:42 +08001409 int ret = 0;
Weijie Gaod24b6932018-11-15 10:08:04 +08001410
mingming lee69373bd2019-11-07 19:28:42 +08001411 if (host->dev_comp->data_tune && host->dev_comp->async_fifo) {
1412 ret = msdc_tune_together(dev, opcode);
1413 if (ret == -EIO) {
1414 dev_err(dev, "Tune fail!\n");
1415 return ret;
1416 }
1417
1418 if (mmc->selected_mode == MMC_HS_400) {
1419 clrbits_le32(&host->base->msdc_iocon,
1420 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
1421 clrsetbits_le32(&host->base->pad_tune,
1422 MSDC_PAD_TUNE_DATRRDLY_M, 0);
1423
1424 writel(host->hs400_ds_delay, &host->base->pad_ds_tune);
1425 /* for hs400 mode it must be set to 0 */
1426 clrbits_le32(&host->base->patch_bit2,
1427 MSDC_PB2_CFGCRCSTS);
1428 host->hs400_mode = true;
1429 }
1430 goto tune_done;
Weijie Gaod24b6932018-11-15 10:08:04 +08001431 }
1432
mingming lee69373bd2019-11-07 19:28:42 +08001433 if (mmc->selected_mode == MMC_HS_400)
1434 ret = hs400_tune_response(dev, opcode);
1435 else
1436 ret = msdc_tune_response(dev, opcode);
Weijie Gaod24b6932018-11-15 10:08:04 +08001437 if (ret == -EIO) {
1438 dev_err(dev, "Tune response fail!\n");
1439 return ret;
1440 }
1441
mingming lee69373bd2019-11-07 19:28:42 +08001442 if (mmc->selected_mode != MMC_HS_400) {
Weijie Gaod24b6932018-11-15 10:08:04 +08001443 ret = msdc_tune_data(dev, opcode);
mingming lee69373bd2019-11-07 19:28:42 +08001444 if (ret == -EIO) {
Weijie Gaod24b6932018-11-15 10:08:04 +08001445 dev_err(dev, "Tune data fail!\n");
mingming lee69373bd2019-11-07 19:28:42 +08001446 return ret;
1447 }
Weijie Gaod24b6932018-11-15 10:08:04 +08001448 }
1449
mingming lee69373bd2019-11-07 19:28:42 +08001450tune_done:
Weijie Gaod24b6932018-11-15 10:08:04 +08001451 host->saved_tune_para.iocon = readl(&host->base->msdc_iocon);
1452 host->saved_tune_para.pad_tune = readl(&host->base->pad_tune);
mingming lee69373bd2019-11-07 19:28:42 +08001453 host->saved_tune_para.pad_cmd_tune = readl(&host->base->pad_cmd_tune);
Weijie Gaod24b6932018-11-15 10:08:04 +08001454
1455 return ret;
1456}
1457#endif
1458
1459static void msdc_init_hw(struct msdc_host *host)
1460{
1461 u32 val;
1462 void __iomem *tune_reg = &host->base->pad_tune;
Weijie Gao2a505cd2020-11-12 16:37:02 +08001463 void __iomem *rd_dly0_reg = &host->base->pad_tune0;
1464 void __iomem *rd_dly1_reg = &host->base->pad_tune1;
Weijie Gaod24b6932018-11-15 10:08:04 +08001465
Weijie Gao2a505cd2020-11-12 16:37:02 +08001466 if (host->dev_comp->pad_tune0) {
Weijie Gaod24b6932018-11-15 10:08:04 +08001467 tune_reg = &host->base->pad_tune0;
Weijie Gao2a505cd2020-11-12 16:37:02 +08001468 rd_dly0_reg = &host->base->dat_rd_dly[0];
1469 rd_dly1_reg = &host->base->dat_rd_dly[1];
1470 }
Weijie Gaod24b6932018-11-15 10:08:04 +08001471
1472 /* Configure to MMC/SD mode, clock free running */
1473 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_MODE);
1474
1475 /* Use PIO mode */
1476 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_PIO);
1477
1478 /* Reset */
1479 msdc_reset_hw(host);
1480
1481 /* Enable/disable hw card detection according to fdt option */
1482 if (host->builtin_cd)
1483 clrsetbits_le32(&host->base->msdc_ps,
1484 MSDC_PS_CDDBCE_M,
1485 (DEFAULT_CD_DEBOUNCE << MSDC_PS_CDDBCE_S) |
1486 MSDC_PS_CDEN);
1487 else
1488 clrbits_le32(&host->base->msdc_ps, MSDC_PS_CDEN);
1489
1490 /* Clear all interrupts */
1491 val = readl(&host->base->msdc_int);
1492 writel(val, &host->base->msdc_int);
1493
1494 /* Enable data & cmd interrupts */
1495 writel(DATA_INTS_MASK | CMD_INTS_MASK, &host->base->msdc_inten);
1496
1497 writel(0, tune_reg);
1498 writel(0, &host->base->msdc_iocon);
1499
1500 if (host->r_smpl)
1501 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1502 else
1503 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1504
1505 writel(0x403c0046, &host->base->patch_bit0);
1506 writel(0xffff4089, &host->base->patch_bit1);
1507
1508 if (host->dev_comp->stop_clk_fix)
1509 clrsetbits_le32(&host->base->patch_bit1, MSDC_PB1_STOP_DLY_M,
1510 3 << MSDC_PB1_STOP_DLY_S);
1511
1512 if (host->dev_comp->busy_check)
1513 clrbits_le32(&host->base->patch_bit1, (1 << 7));
1514
1515 setbits_le32(&host->base->emmc50_cfg0, EMMC50_CFG_CFCSTS_SEL);
1516
1517 if (host->dev_comp->async_fifo) {
1518 clrsetbits_le32(&host->base->patch_bit2, MSDC_PB2_RESPWAIT_M,
1519 3 << MSDC_PB2_RESPWAIT_S);
1520
1521 if (host->dev_comp->enhance_rx) {
mingming lee3b0397d2019-12-31 11:29:24 +08001522 if (host->top_base)
1523 setbits_le32(&host->top_base->emmc_top_control,
1524 SDC_RX_ENH_EN);
1525 else
1526 setbits_le32(&host->base->sdc_adv_cfg0,
1527 SDC_RX_ENHANCE_EN);
Weijie Gaod24b6932018-11-15 10:08:04 +08001528 } else {
1529 clrsetbits_le32(&host->base->patch_bit2,
1530 MSDC_PB2_RESPSTSENSEL_M,
1531 2 << MSDC_PB2_RESPSTSENSEL_S);
1532 clrsetbits_le32(&host->base->patch_bit2,
1533 MSDC_PB2_CRCSTSENSEL_M,
1534 2 << MSDC_PB2_CRCSTSENSEL_S);
1535 }
1536
1537 /* use async fifo to avoid tune internal delay */
1538 clrbits_le32(&host->base->patch_bit2,
1539 MSDC_PB2_CFGRESP);
1540 clrbits_le32(&host->base->patch_bit2,
1541 MSDC_PB2_CFGCRCSTS);
1542 }
1543
1544 if (host->dev_comp->data_tune) {
1545 setbits_le32(tune_reg,
1546 MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL);
1547 clrsetbits_le32(&host->base->patch_bit0,
1548 MSDC_INT_DAT_LATCH_CK_SEL_M,
1549 host->latch_ck <<
1550 MSDC_INT_DAT_LATCH_CK_SEL_S);
1551 } else {
1552 /* choose clock tune */
1553 setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
1554 }
1555
Weijie Gao2a505cd2020-11-12 16:37:02 +08001556 if (host->dev_comp->builtin_pad_ctrl) {
1557 /* Set pins driving strength */
1558 writel(MSDC_PAD_CTRL0_CLKPD | MSDC_PAD_CTRL0_CLKSMT |
1559 MSDC_PAD_CTRL0_CLKIES | (4 << MSDC_PAD_CTRL0_CLKDRVN_S) |
1560 (4 << MSDC_PAD_CTRL0_CLKDRVP_S), &host->base->pad_ctrl0);
1561 writel(MSDC_PAD_CTRL1_CMDPU | MSDC_PAD_CTRL1_CMDSMT |
1562 MSDC_PAD_CTRL1_CMDIES | (4 << MSDC_PAD_CTRL1_CMDDRVN_S) |
1563 (4 << MSDC_PAD_CTRL1_CMDDRVP_S), &host->base->pad_ctrl1);
1564 writel(MSDC_PAD_CTRL2_DATPU | MSDC_PAD_CTRL2_DATSMT |
1565 MSDC_PAD_CTRL2_DATIES | (4 << MSDC_PAD_CTRL2_DATDRVN_S) |
1566 (4 << MSDC_PAD_CTRL2_DATDRVP_S), &host->base->pad_ctrl2);
1567 }
1568
1569 if (host->dev_comp->default_pad_dly) {
1570 /* Default pad delay may be needed if tuning not enabled */
1571 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CLKTDLY_M |
1572 MSDC_PAD_TUNE_CMDRRDLY_M |
1573 MSDC_PAD_TUNE_CMDRDLY_M |
1574 MSDC_PAD_TUNE_DATRRDLY_M |
1575 MSDC_PAD_TUNE_DATWRDLY_M,
1576 (0x10 << MSDC_PAD_TUNE_CLKTDLY_S) |
1577 (0x10 << MSDC_PAD_TUNE_CMDRRDLY_S) |
1578 (0x10 << MSDC_PAD_TUNE_CMDRDLY_S) |
1579 (0x10 << MSDC_PAD_TUNE_DATRRDLY_S) |
1580 (0x10 << MSDC_PAD_TUNE_DATWRDLY_S));
1581
1582 writel((0x10 << MSDC_PAD_TUNE0_DAT0RDDLY_S) |
1583 (0x10 << MSDC_PAD_TUNE0_DAT1RDDLY_S) |
1584 (0x10 << MSDC_PAD_TUNE0_DAT2RDDLY_S) |
1585 (0x10 << MSDC_PAD_TUNE0_DAT3RDDLY_S),
1586 rd_dly0_reg);
1587
1588 writel((0x10 << MSDC_PAD_TUNE1_DAT4RDDLY_S) |
1589 (0x10 << MSDC_PAD_TUNE1_DAT5RDDLY_S) |
1590 (0x10 << MSDC_PAD_TUNE1_DAT6RDDLY_S) |
1591 (0x10 << MSDC_PAD_TUNE1_DAT7RDDLY_S),
1592 rd_dly1_reg);
1593 }
1594
Weijie Gaod24b6932018-11-15 10:08:04 +08001595 /* Configure to enable SDIO mode otherwise sdio cmd5 won't work */
1596 setbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIO);
1597
1598 /* disable detecting SDIO device interrupt function */
1599 clrbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIOIDE);
1600
1601 /* Configure to default data timeout */
1602 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
1603 3 << SDC_CFG_DTOC_S);
1604
1605 if (host->dev_comp->stop_clk_fix) {
1606 clrbits_le32(&host->base->sdc_fifo_cfg,
1607 SDC_FIFO_CFG_WRVALIDSEL);
1608 clrbits_le32(&host->base->sdc_fifo_cfg,
1609 SDC_FIFO_CFG_RDVALIDSEL);
1610 }
1611
1612 host->def_tune_para.iocon = readl(&host->base->msdc_iocon);
1613 host->def_tune_para.pad_tune = readl(&host->base->pad_tune);
1614}
1615
1616static void msdc_ungate_clock(struct msdc_host *host)
1617{
1618 clk_enable(&host->src_clk);
1619 clk_enable(&host->h_clk);
Fabien Parentba077e52019-03-24 16:46:32 +01001620 if (host->src_clk_cg.dev)
1621 clk_enable(&host->src_clk_cg);
Weijie Gaod24b6932018-11-15 10:08:04 +08001622}
1623
1624static int msdc_drv_probe(struct udevice *dev)
1625{
1626 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -07001627 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001628 struct msdc_host *host = dev_get_priv(dev);
1629 struct mmc_config *cfg = &plat->cfg;
1630
1631 cfg->name = dev->name;
1632
1633 host->dev_comp = (struct msdc_compatible *)dev_get_driver_data(dev);
1634
1635 host->src_clk_freq = clk_get_rate(&host->src_clk);
1636
1637 if (host->dev_comp->clk_div_bits == 8)
1638 cfg->f_min = host->src_clk_freq / (4 * 255);
1639 else
1640 cfg->f_min = host->src_clk_freq / (4 * 4095);
Weijie Gaod24b6932018-11-15 10:08:04 +08001641
1642 cfg->b_max = 1024;
1643 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1644
1645 host->mmc = &plat->mmc;
1646 host->timeout_ns = 100000000;
Weijie Gaof875a122020-11-12 16:36:57 +08001647 host->timeout_clks = 3 * (1 << SCLK_CYCLES_SHIFT);
Weijie Gaod24b6932018-11-15 10:08:04 +08001648
1649#ifdef CONFIG_PINCTRL
1650 pinctrl_select_state(dev, "default");
1651#endif
1652
1653 msdc_ungate_clock(host);
1654 msdc_init_hw(host);
1655
1656 upriv->mmc = &plat->mmc;
1657
1658 return 0;
1659}
1660
Simon Glassd1998a92020-12-03 16:55:21 -07001661static int msdc_of_to_plat(struct udevice *dev)
Weijie Gaod24b6932018-11-15 10:08:04 +08001662{
Simon Glassc69cda22020-12-03 16:55:20 -07001663 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001664 struct msdc_host *host = dev_get_priv(dev);
1665 struct mmc_config *cfg = &plat->cfg;
mingming lee3b0397d2019-12-31 11:29:24 +08001666 fdt_addr_t base, top_base;
Weijie Gaod24b6932018-11-15 10:08:04 +08001667 int ret;
1668
mingming lee3b0397d2019-12-31 11:29:24 +08001669 base = dev_read_addr(dev);
1670 if (base == FDT_ADDR_T_NONE)
Weijie Gaod24b6932018-11-15 10:08:04 +08001671 return -EINVAL;
mingming lee3b0397d2019-12-31 11:29:24 +08001672 host->base = map_sysmem(base, 0);
1673
1674 top_base = dev_read_addr_index(dev, 1);
1675 if (top_base == FDT_ADDR_T_NONE)
1676 host->top_base = NULL;
1677 else
1678 host->top_base = map_sysmem(top_base, 0);
Weijie Gaod24b6932018-11-15 10:08:04 +08001679
1680 ret = mmc_of_parse(dev, cfg);
1681 if (ret)
1682 return ret;
1683
1684 ret = clk_get_by_name(dev, "source", &host->src_clk);
1685 if (ret < 0)
1686 return ret;
1687
1688 ret = clk_get_by_name(dev, "hclk", &host->h_clk);
1689 if (ret < 0)
1690 return ret;
1691
Fabien Parentba077e52019-03-24 16:46:32 +01001692 clk_get_by_name(dev, "source_cg", &host->src_clk_cg); /* optional */
1693
Fabien Parentb5096f12019-03-24 16:46:34 +01001694#if CONFIG_IS_ENABLED(DM_GPIO)
Weijie Gaod24b6932018-11-15 10:08:04 +08001695 gpio_request_by_name(dev, "wp-gpios", 0, &host->gpio_wp, GPIOD_IS_IN);
1696 gpio_request_by_name(dev, "cd-gpios", 0, &host->gpio_cd, GPIOD_IS_IN);
1697#endif
1698
1699 host->hs400_ds_delay = dev_read_u32_default(dev, "hs400-ds-delay", 0);
1700 host->hs200_cmd_int_delay =
1701 dev_read_u32_default(dev, "cmd_int_delay", 0);
1702 host->hs200_write_int_delay =
1703 dev_read_u32_default(dev, "write_int_delay", 0);
1704 host->latch_ck = dev_read_u32_default(dev, "latch-ck", 0);
1705 host->r_smpl = dev_read_u32_default(dev, "r_smpl", 0);
1706 host->builtin_cd = dev_read_u32_default(dev, "builtin-cd", 0);
Weijie Gao77ebea22019-09-25 17:45:38 +08001707 host->cd_active_high = dev_read_bool(dev, "cd-active-high");
Weijie Gaod24b6932018-11-15 10:08:04 +08001708
1709 return 0;
1710}
1711
1712static int msdc_drv_bind(struct udevice *dev)
1713{
Simon Glassc69cda22020-12-03 16:55:20 -07001714 struct msdc_plat *plat = dev_get_plat(dev);
Weijie Gaod24b6932018-11-15 10:08:04 +08001715
1716 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1717}
1718
1719static const struct dm_mmc_ops msdc_ops = {
1720 .send_cmd = msdc_ops_send_cmd,
1721 .set_ios = msdc_ops_set_ios,
1722 .get_cd = msdc_ops_get_cd,
1723 .get_wp = msdc_ops_get_wp,
1724#ifdef MMC_SUPPORTS_TUNING
1725 .execute_tuning = msdc_execute_tuning,
1726#endif
1727};
1728
Weijie Gao3c92a952019-09-25 17:45:37 +08001729static const struct msdc_compatible mt7620_compat = {
1730 .clk_div_bits = 8,
Weijie Gao3c92a952019-09-25 17:45:37 +08001731 .pad_tune0 = false,
1732 .async_fifo = false,
1733 .data_tune = false,
1734 .busy_check = false,
1735 .stop_clk_fix = false,
Weijie Gao2a505cd2020-11-12 16:37:02 +08001736 .enhance_rx = false,
1737 .builtin_pad_ctrl = true,
1738 .default_pad_dly = true,
Weijie Gao3c92a952019-09-25 17:45:37 +08001739};
1740
Sam Shih25273db2020-01-10 16:30:32 +08001741static const struct msdc_compatible mt7622_compat = {
1742 .clk_div_bits = 12,
1743 .pad_tune0 = true,
1744 .async_fifo = true,
1745 .data_tune = true,
1746 .busy_check = true,
1747 .stop_clk_fix = true,
1748};
1749
Weijie Gaod24b6932018-11-15 10:08:04 +08001750static const struct msdc_compatible mt7623_compat = {
1751 .clk_div_bits = 12,
1752 .pad_tune0 = true,
1753 .async_fifo = true,
1754 .data_tune = true,
1755 .busy_check = false,
1756 .stop_clk_fix = false,
1757 .enhance_rx = false
1758};
1759
mingming lee3b0397d2019-12-31 11:29:24 +08001760static const struct msdc_compatible mt8512_compat = {
1761 .clk_div_bits = 12,
mingming lee3b0397d2019-12-31 11:29:24 +08001762 .pad_tune0 = true,
1763 .async_fifo = true,
1764 .data_tune = true,
1765 .busy_check = true,
1766 .stop_clk_fix = true,
1767};
1768
Fabien Parenta9332662019-03-24 16:46:33 +01001769static const struct msdc_compatible mt8516_compat = {
1770 .clk_div_bits = 12,
1771 .pad_tune0 = true,
1772 .async_fifo = true,
1773 .data_tune = true,
1774 .busy_check = true,
1775 .stop_clk_fix = true,
1776};
1777
Fabien Parent908d0062019-08-12 20:26:58 +02001778static const struct msdc_compatible mt8183_compat = {
1779 .clk_div_bits = 12,
1780 .pad_tune0 = true,
1781 .async_fifo = true,
1782 .data_tune = true,
1783 .busy_check = true,
1784 .stop_clk_fix = true,
1785};
1786
Weijie Gaod24b6932018-11-15 10:08:04 +08001787static const struct udevice_id msdc_ids[] = {
Weijie Gao3c92a952019-09-25 17:45:37 +08001788 { .compatible = "mediatek,mt7620-mmc", .data = (ulong)&mt7620_compat },
Sam Shih25273db2020-01-10 16:30:32 +08001789 { .compatible = "mediatek,mt7622-mmc", .data = (ulong)&mt7622_compat },
Weijie Gaod24b6932018-11-15 10:08:04 +08001790 { .compatible = "mediatek,mt7623-mmc", .data = (ulong)&mt7623_compat },
mingming lee3b0397d2019-12-31 11:29:24 +08001791 { .compatible = "mediatek,mt8512-mmc", .data = (ulong)&mt8512_compat },
Fabien Parenta9332662019-03-24 16:46:33 +01001792 { .compatible = "mediatek,mt8516-mmc", .data = (ulong)&mt8516_compat },
Fabien Parent908d0062019-08-12 20:26:58 +02001793 { .compatible = "mediatek,mt8183-mmc", .data = (ulong)&mt8183_compat },
Weijie Gaod24b6932018-11-15 10:08:04 +08001794 {}
1795};
1796
1797U_BOOT_DRIVER(mtk_sd_drv) = {
1798 .name = "mtk_sd",
1799 .id = UCLASS_MMC,
1800 .of_match = msdc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07001801 .of_to_plat = msdc_of_to_plat,
Weijie Gaod24b6932018-11-15 10:08:04 +08001802 .bind = msdc_drv_bind,
1803 .probe = msdc_drv_probe,
1804 .ops = &msdc_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -07001805 .plat_auto = sizeof(struct msdc_plat),
Simon Glass41575d82020-12-03 16:55:17 -07001806 .priv_auto = sizeof(struct msdc_host),
Weijie Gaod24b6932018-11-15 10:08:04 +08001807};