blob: ebd72178b595e46d1a86661bd593d6c03ead8250 [file] [log] [blame]
developerdc5a9aa2018-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>
developerdc5a9aa2018-11-15 10:08:04 +080010#include <dm.h>
11#include <mmc.h>
12#include <errno.h>
13#include <malloc.h>
developera2d3a6c2019-12-31 11:29:24 +080014#include <mapmem.h>
developerdc5a9aa2018-11-15 10:08:04 +080015#include <stdbool.h>
16#include <asm/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
developerdc5a9aa2018-11-15 10:08:04 +080018#include <dm/pinctrl.h>
19#include <linux/bitops.h>
20#include <linux/io.h>
21#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060022#include <linux/printk.h>
developerdc5a9aa2018-11-15 10:08:04 +080023
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
developer7295c892020-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
developerdc5a9aa2018-11-15 10:08:04 +0800158/* PAD_TUNE */
developer7295c892020-11-12 16:37:02 +0800159#define MSDC_PAD_TUNE_CLKTDLY_M 0xf8000000
160#define MSDC_PAD_TUNE_CLKTDLY_S 27
developerdc5a9aa2018-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
developer18f9fc72019-11-07 19:28:42 +0800173#define PAD_CMD_TUNE_RX_DLY3 0x3E
174#define PAD_CMD_TUNE_RX_DLY3_S 1
175
developer7295c892020-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
developerdc5a9aa2018-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
developera2d3a6c2019-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
developerdc5a9aa2018-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
developerc7310742020-11-12 16:36:57 +0800233#define SCLK_CYCLES_SHIFT 20
234
developer13b920a2021-04-20 16:37:10 +0800235#define MIN_BUS_CLK 200000
236
developerdc5a9aa2018-11-15 10:08:04 +0800237#define CMD_INTS_MASK \
238 (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO)
239
240#define DATA_INTS_MASK \
241 (MSDC_INT_XFER_COMPL | MSDC_INT_DATTMO | MSDC_INT_DATCRCERR)
242
243/* Register offset */
244struct mtk_sd_regs {
245 u32 msdc_cfg;
246 u32 msdc_iocon;
247 u32 msdc_ps;
248 u32 msdc_int;
249 u32 msdc_inten;
250 u32 msdc_fifocs;
251 u32 msdc_txdata;
252 u32 msdc_rxdata;
253 u32 reserved0[4];
254 u32 sdc_cfg;
255 u32 sdc_cmd;
256 u32 sdc_arg;
257 u32 sdc_sts;
258 u32 sdc_resp[4];
259 u32 sdc_blk_num;
260 u32 sdc_vol_chg;
261 u32 sdc_csts;
262 u32 sdc_csts_en;
263 u32 sdc_datcrc_sts;
264 u32 sdc_adv_cfg0;
265 u32 reserved1[2];
266 u32 emmc_cfg0;
267 u32 emmc_cfg1;
268 u32 emmc_sts;
269 u32 emmc_iocon;
270 u32 sd_acmd_resp;
271 u32 sd_acmd19_trg;
272 u32 sd_acmd19_sts;
273 u32 dma_sa_high4bit;
274 u32 dma_sa;
275 u32 dma_ca;
276 u32 dma_ctrl;
277 u32 dma_cfg;
278 u32 sw_dbg_sel;
279 u32 sw_dbg_out;
280 u32 dma_length;
281 u32 reserved2;
282 u32 patch_bit0;
283 u32 patch_bit1;
284 u32 patch_bit2;
285 u32 reserved3;
286 u32 dat0_tune_crc;
287 u32 dat1_tune_crc;
288 u32 dat2_tune_crc;
289 u32 dat3_tune_crc;
290 u32 cmd_tune_crc;
291 u32 sdio_tune_wind;
developer7295c892020-11-12 16:37:02 +0800292 u32 reserved4[2];
293 u32 pad_ctrl0;
294 u32 pad_ctrl1;
295 u32 pad_ctrl2;
developerdc5a9aa2018-11-15 10:08:04 +0800296 u32 pad_tune;
297 u32 pad_tune0;
298 u32 pad_tune1;
299 u32 dat_rd_dly[4];
300 u32 reserved5[2];
301 u32 hw_dbg_sel;
302 u32 main_ver;
303 u32 eco_ver;
304 u32 reserved6[27];
305 u32 pad_ds_tune;
developer18f9fc72019-11-07 19:28:42 +0800306 u32 pad_cmd_tune;
307 u32 reserved7[30];
developerdc5a9aa2018-11-15 10:08:04 +0800308 u32 emmc50_cfg0;
309 u32 reserved8[7];
310 u32 sdc_fifo_cfg;
311};
312
developera2d3a6c2019-12-31 11:29:24 +0800313struct msdc_top_regs {
314 u32 emmc_top_control;
315 u32 emmc_top_cmd;
316 u32 emmc50_pad_ctl0;
317 u32 emmc50_pad_ds_tune;
318 u32 emmc50_pad_dat0_tune;
319 u32 emmc50_pad_dat1_tune;
320 u32 emmc50_pad_dat2_tune;
321 u32 emmc50_pad_dat3_tune;
322 u32 emmc50_pad_dat4_tune;
323 u32 emmc50_pad_dat5_tune;
324 u32 emmc50_pad_dat6_tune;
325 u32 emmc50_pad_dat7_tune;
326};
327
developerdc5a9aa2018-11-15 10:08:04 +0800328struct msdc_compatible {
329 u8 clk_div_bits;
330 bool pad_tune0;
331 bool async_fifo;
332 bool data_tune;
333 bool busy_check;
334 bool stop_clk_fix;
335 bool enhance_rx;
developer7295c892020-11-12 16:37:02 +0800336 bool builtin_pad_ctrl;
337 bool default_pad_dly;
Christian Marangi2e43de22024-06-24 23:03:34 +0200338 bool use_internal_cd;
developerdc5a9aa2018-11-15 10:08:04 +0800339};
340
341struct msdc_delay_phase {
342 u8 maxlen;
343 u8 start;
344 u8 final_phase;
345};
346
347struct msdc_plat {
348 struct mmc_config cfg;
349 struct mmc mmc;
350};
351
352struct msdc_tune_para {
353 u32 iocon;
354 u32 pad_tune;
developer18f9fc72019-11-07 19:28:42 +0800355 u32 pad_cmd_tune;
developerdc5a9aa2018-11-15 10:08:04 +0800356};
357
358struct msdc_host {
359 struct mtk_sd_regs *base;
developera2d3a6c2019-12-31 11:29:24 +0800360 struct msdc_top_regs *top_base;
developerdc5a9aa2018-11-15 10:08:04 +0800361 struct mmc *mmc;
362
363 struct msdc_compatible *dev_comp;
364
365 struct clk src_clk; /* for SD/MMC bus clock */
Fabien Parent297fa1a2019-03-24 16:46:32 +0100366 struct clk src_clk_cg; /* optional, MSDC source clock control gate */
developerdc5a9aa2018-11-15 10:08:04 +0800367 struct clk h_clk; /* MSDC core clock */
368
Christian Marangi2e43de22024-06-24 23:03:34 +0200369 /* upstream linux clock */
370 struct clk axi_cg_clk; /* optional, AXI clock */
371 struct clk ahb_cg_clk; /* optional, AHB clock */
372
developerdc5a9aa2018-11-15 10:08:04 +0800373 u32 src_clk_freq; /* source clock */
374 u32 mclk; /* mmc framework required bus clock */
375 u32 sclk; /* actual calculated bus clock */
376
377 /* operation timeout clocks */
378 u32 timeout_ns;
379 u32 timeout_clks;
380
381 /* tuning options */
382 u32 hs400_ds_delay;
383 u32 hs200_cmd_int_delay;
384 u32 hs200_write_int_delay;
385 u32 latch_ck;
386 u32 r_smpl; /* sample edge */
387 bool hs400_mode;
388
389 /* whether to use gpio detection or built-in hw detection */
390 bool builtin_cd;
developer399e4af2019-09-25 17:45:38 +0800391 bool cd_active_high;
developerdc5a9aa2018-11-15 10:08:04 +0800392
393 /* card detection / write protection GPIOs */
Fabien Parent8ed608a2019-03-24 16:46:34 +0100394#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +0800395 struct gpio_desc gpio_wp;
396 struct gpio_desc gpio_cd;
397#endif
398
399 uint last_resp_type;
400 uint last_data_write;
401
402 enum bus_mode timing;
403
404 struct msdc_tune_para def_tune_para;
405 struct msdc_tune_para saved_tune_para;
406};
407
408static void msdc_reset_hw(struct msdc_host *host)
409{
410 u32 reg;
411
412 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_RST);
413
414 readl_poll_timeout(&host->base->msdc_cfg, reg,
415 !(reg & MSDC_CFG_RST), 1000000);
416}
417
418static void msdc_fifo_clr(struct msdc_host *host)
419{
420 u32 reg;
421
422 setbits_le32(&host->base->msdc_fifocs, MSDC_FIFOCS_CLR);
423
424 readl_poll_timeout(&host->base->msdc_fifocs, reg,
425 !(reg & MSDC_FIFOCS_CLR), 1000000);
426}
427
428static u32 msdc_fifo_rx_bytes(struct msdc_host *host)
429{
430 return (readl(&host->base->msdc_fifocs) &
431 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S;
432}
433
434static u32 msdc_fifo_tx_bytes(struct msdc_host *host)
435{
436 return (readl(&host->base->msdc_fifocs) &
437 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S;
438}
439
440static u32 msdc_cmd_find_resp(struct msdc_host *host, struct mmc_cmd *cmd)
441{
442 u32 resp;
443
444 switch (cmd->resp_type) {
445 /* Actually, R1, R5, R6, R7 are the same */
446 case MMC_RSP_R1:
447 resp = 0x1;
448 break;
449 case MMC_RSP_R1b:
450 resp = 0x7;
451 break;
452 case MMC_RSP_R2:
453 resp = 0x2;
454 break;
455 case MMC_RSP_R3:
456 resp = 0x3;
457 break;
458 case MMC_RSP_NONE:
459 default:
460 resp = 0x0;
461 break;
462 }
463
464 return resp;
465}
466
467static u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
468 struct mmc_cmd *cmd,
469 struct mmc_data *data)
470{
471 u32 opcode = cmd->cmdidx;
472 u32 resp_type = msdc_cmd_find_resp(host, cmd);
473 uint blocksize = 0;
474 u32 dtype = 0;
475 u32 rawcmd = 0;
476
477 switch (opcode) {
478 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
479 case MMC_CMD_READ_MULTIPLE_BLOCK:
480 dtype = 2;
481 break;
482 case MMC_CMD_WRITE_SINGLE_BLOCK:
483 case MMC_CMD_READ_SINGLE_BLOCK:
484 case SD_CMD_APP_SEND_SCR:
developer18f9fc72019-11-07 19:28:42 +0800485 case MMC_CMD_SEND_TUNING_BLOCK:
486 case MMC_CMD_SEND_TUNING_BLOCK_HS200:
developerdc5a9aa2018-11-15 10:08:04 +0800487 dtype = 1;
488 break;
489 case SD_CMD_SWITCH_FUNC: /* same as MMC_CMD_SWITCH */
490 case SD_CMD_SEND_IF_COND: /* same as MMC_CMD_SEND_EXT_CSD */
491 case SD_CMD_APP_SD_STATUS: /* same as MMC_CMD_SEND_STATUS */
492 if (data)
493 dtype = 1;
494 }
495
496 if (data) {
497 if (data->flags == MMC_DATA_WRITE)
498 rawcmd |= SDC_CMD_WR;
499
500 if (data->blocks > 1)
501 dtype = 2;
502
503 blocksize = data->blocksize;
504 }
505
506 rawcmd |= ((opcode << SDC_CMD_CMD_S) & SDC_CMD_CMD_M) |
507 ((resp_type << SDC_CMD_RSPTYP_S) & SDC_CMD_RSPTYP_M) |
508 ((blocksize << SDC_CMD_BLK_LEN_S) & SDC_CMD_BLK_LEN_M) |
509 ((dtype << SDC_CMD_DTYPE_S) & SDC_CMD_DTYPE_M);
510
511 if (opcode == MMC_CMD_STOP_TRANSMISSION)
512 rawcmd |= SDC_CMD_STOP;
513
514 return rawcmd;
515}
516
517static int msdc_cmd_done(struct msdc_host *host, int events,
518 struct mmc_cmd *cmd)
519{
520 u32 *rsp = cmd->response;
521 int ret = 0;
522
523 if (cmd->resp_type & MMC_RSP_PRESENT) {
524 if (cmd->resp_type & MMC_RSP_136) {
525 rsp[0] = readl(&host->base->sdc_resp[3]);
526 rsp[1] = readl(&host->base->sdc_resp[2]);
527 rsp[2] = readl(&host->base->sdc_resp[1]);
528 rsp[3] = readl(&host->base->sdc_resp[0]);
529 } else {
530 rsp[0] = readl(&host->base->sdc_resp[0]);
531 }
532 }
533
534 if (!(events & MSDC_INT_CMDRDY)) {
535 if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
536 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
537 /*
538 * should not clear fifo/interrupt as the tune data
539 * may have alreay come.
540 */
541 msdc_reset_hw(host);
542
543 if (events & MSDC_INT_CMDTMO)
544 ret = -ETIMEDOUT;
545 else
546 ret = -EIO;
547 }
548
549 return ret;
550}
551
552static bool msdc_cmd_is_ready(struct msdc_host *host)
553{
554 int ret;
555 u32 reg;
556
557 /* The max busy time we can endure is 20ms */
558 ret = readl_poll_timeout(&host->base->sdc_sts, reg,
559 !(reg & SDC_STS_CMDBUSY), 20000);
560
561 if (ret) {
562 pr_err("CMD bus busy detected\n");
563 msdc_reset_hw(host);
564 return false;
565 }
566
567 if (host->last_resp_type == MMC_RSP_R1b && host->last_data_write) {
568 ret = readl_poll_timeout(&host->base->msdc_ps, reg,
569 reg & MSDC_PS_DAT0, 1000000);
570
571 if (ret) {
572 pr_err("Card stuck in programming state!\n");
573 msdc_reset_hw(host);
574 return false;
575 }
576 }
577
578 return true;
579}
580
581static int msdc_start_command(struct msdc_host *host, struct mmc_cmd *cmd,
582 struct mmc_data *data)
583{
584 u32 rawcmd;
585 u32 status;
586 u32 blocks = 0;
587 int ret;
588
589 if (!msdc_cmd_is_ready(host))
590 return -EIO;
591
developer18f9fc72019-11-07 19:28:42 +0800592 if ((readl(&host->base->msdc_fifocs) &
593 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S ||
594 (readl(&host->base->msdc_fifocs) &
595 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S) {
596 pr_err("TX/RX FIFO non-empty before start of IO. Reset\n");
597 msdc_reset_hw(host);
598 }
599
developerdc5a9aa2018-11-15 10:08:04 +0800600 msdc_fifo_clr(host);
601
602 host->last_resp_type = cmd->resp_type;
603 host->last_data_write = 0;
604
605 rawcmd = msdc_cmd_prepare_raw_cmd(host, cmd, data);
606
607 if (data)
608 blocks = data->blocks;
609
610 writel(CMD_INTS_MASK, &host->base->msdc_int);
developer068cc652019-12-31 11:29:25 +0800611 writel(DATA_INTS_MASK, &host->base->msdc_int);
developerdc5a9aa2018-11-15 10:08:04 +0800612 writel(blocks, &host->base->sdc_blk_num);
613 writel(cmd->cmdarg, &host->base->sdc_arg);
614 writel(rawcmd, &host->base->sdc_cmd);
615
616 ret = readl_poll_timeout(&host->base->msdc_int, status,
617 status & CMD_INTS_MASK, 1000000);
618
619 if (ret)
620 status = MSDC_INT_CMDTMO;
621
622 return msdc_cmd_done(host, status, cmd);
623}
624
625static void msdc_fifo_read(struct msdc_host *host, u8 *buf, u32 size)
626{
627 u32 *wbuf;
628
629 while ((size_t)buf % 4) {
630 *buf++ = readb(&host->base->msdc_rxdata);
631 size--;
632 }
633
634 wbuf = (u32 *)buf;
635 while (size >= 4) {
636 *wbuf++ = readl(&host->base->msdc_rxdata);
637 size -= 4;
638 }
639
640 buf = (u8 *)wbuf;
641 while (size) {
642 *buf++ = readb(&host->base->msdc_rxdata);
643 size--;
644 }
645}
646
647static void msdc_fifo_write(struct msdc_host *host, const u8 *buf, u32 size)
648{
649 const u32 *wbuf;
650
651 while ((size_t)buf % 4) {
652 writeb(*buf++, &host->base->msdc_txdata);
653 size--;
654 }
655
656 wbuf = (const u32 *)buf;
657 while (size >= 4) {
658 writel(*wbuf++, &host->base->msdc_txdata);
659 size -= 4;
660 }
661
662 buf = (const u8 *)wbuf;
663 while (size) {
664 writeb(*buf++, &host->base->msdc_txdata);
665 size--;
666 }
667}
668
669static int msdc_pio_read(struct msdc_host *host, u8 *ptr, u32 size)
670{
671 u32 status;
672 u32 chksz;
673 int ret = 0;
674
675 while (1) {
676 status = readl(&host->base->msdc_int);
677 writel(status, &host->base->msdc_int);
678 status &= DATA_INTS_MASK;
679
680 if (status & MSDC_INT_DATCRCERR) {
681 ret = -EIO;
682 break;
683 }
684
685 if (status & MSDC_INT_DATTMO) {
686 ret = -ETIMEDOUT;
687 break;
688 }
689
Fabien Parent79a60732019-01-17 18:06:00 +0100690 chksz = min(size, (u32)MSDC_FIFO_SIZE);
691
692 if (msdc_fifo_rx_bytes(host) >= chksz) {
693 msdc_fifo_read(host, ptr, chksz);
694 ptr += chksz;
695 size -= chksz;
696 }
697
developerdc5a9aa2018-11-15 10:08:04 +0800698 if (status & MSDC_INT_XFER_COMPL) {
699 if (size) {
700 pr_err("data not fully read\n");
701 ret = -EIO;
702 }
703
704 break;
705 }
Fabien Parent79a60732019-01-17 18:06:00 +0100706}
developerdc5a9aa2018-11-15 10:08:04 +0800707
708 return ret;
709}
710
711static int msdc_pio_write(struct msdc_host *host, const u8 *ptr, u32 size)
712{
713 u32 status;
714 u32 chksz;
715 int ret = 0;
716
717 while (1) {
718 status = readl(&host->base->msdc_int);
719 writel(status, &host->base->msdc_int);
720 status &= DATA_INTS_MASK;
721
722 if (status & MSDC_INT_DATCRCERR) {
723 ret = -EIO;
724 break;
725 }
726
727 if (status & MSDC_INT_DATTMO) {
728 ret = -ETIMEDOUT;
729 break;
730 }
731
732 if (status & MSDC_INT_XFER_COMPL) {
733 if (size) {
734 pr_err("data not fully written\n");
735 ret = -EIO;
736 }
737
738 break;
739 }
740
741 chksz = min(size, (u32)MSDC_FIFO_SIZE);
742
743 if (MSDC_FIFO_SIZE - msdc_fifo_tx_bytes(host) >= chksz) {
744 msdc_fifo_write(host, ptr, chksz);
745 ptr += chksz;
746 size -= chksz;
747 }
748 }
749
750 return ret;
751}
752
753static int msdc_start_data(struct msdc_host *host, struct mmc_data *data)
754{
755 u32 size;
756 int ret;
757
758 if (data->flags == MMC_DATA_WRITE)
759 host->last_data_write = 1;
760
developerdc5a9aa2018-11-15 10:08:04 +0800761 size = data->blocks * data->blocksize;
762
763 if (data->flags == MMC_DATA_WRITE)
764 ret = msdc_pio_write(host, (const u8 *)data->src, size);
765 else
766 ret = msdc_pio_read(host, (u8 *)data->dest, size);
767
768 if (ret) {
769 msdc_reset_hw(host);
770 msdc_fifo_clr(host);
771 }
772
773 return ret;
774}
775
776static int msdc_ops_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
777 struct mmc_data *data)
778{
779 struct msdc_host *host = dev_get_priv(dev);
developer18f9fc72019-11-07 19:28:42 +0800780 int cmd_ret, data_ret;
developerdc5a9aa2018-11-15 10:08:04 +0800781
developer18f9fc72019-11-07 19:28:42 +0800782 cmd_ret = msdc_start_command(host, cmd, data);
783 if (cmd_ret &&
784 !(cmd_ret == -EIO &&
785 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
786 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)))
787 return cmd_ret;
developerdc5a9aa2018-11-15 10:08:04 +0800788
developer18f9fc72019-11-07 19:28:42 +0800789 if (data) {
790 data_ret = msdc_start_data(host, data);
791 if (cmd_ret)
792 return cmd_ret;
793 else
794 return data_ret;
795 }
developerdc5a9aa2018-11-15 10:08:04 +0800796
797 return 0;
798}
799
800static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
801{
developerc7310742020-11-12 16:36:57 +0800802 u32 timeout, clk_ns, shift = SCLK_CYCLES_SHIFT;
developerdc5a9aa2018-11-15 10:08:04 +0800803 u32 mode = 0;
804
805 host->timeout_ns = ns;
806 host->timeout_clks = clks;
807
808 if (host->sclk == 0) {
809 timeout = 0;
810 } else {
811 clk_ns = 1000000000UL / host->sclk;
812 timeout = (ns + clk_ns - 1) / clk_ns + clks;
813 /* unit is 1048576 sclk cycles */
developer607faf72019-09-25 17:45:37 +0800814 timeout = (timeout + (0x1 << shift) - 1) >> shift;
developerdc5a9aa2018-11-15 10:08:04 +0800815 if (host->dev_comp->clk_div_bits == 8)
816 mode = (readl(&host->base->msdc_cfg) &
817 MSDC_CFG_CKMOD_M) >> MSDC_CFG_CKMOD_S;
818 else
819 mode = (readl(&host->base->msdc_cfg) &
820 MSDC_CFG_CKMOD_EXT_M) >> MSDC_CFG_CKMOD_EXT_S;
821 /* DDR mode will double the clk cycles for data timeout */
822 timeout = mode >= 2 ? timeout * 2 : timeout;
823 timeout = timeout > 1 ? timeout - 1 : 0;
824 timeout = timeout > 255 ? 255 : timeout;
825 }
826
827 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
828 timeout << SDC_CFG_DTOC_S);
829}
830
831static void msdc_set_buswidth(struct msdc_host *host, u32 width)
832{
833 u32 val = readl(&host->base->sdc_cfg);
834
835 val &= ~SDC_CFG_BUSWIDTH_M;
836
837 switch (width) {
838 default:
839 case 1:
840 val |= (MSDC_BUS_1BITS << SDC_CFG_BUSWIDTH_S);
841 break;
842 case 4:
843 val |= (MSDC_BUS_4BITS << SDC_CFG_BUSWIDTH_S);
844 break;
845 case 8:
846 val |= (MSDC_BUS_8BITS << SDC_CFG_BUSWIDTH_S);
847 break;
848 }
849
850 writel(val, &host->base->sdc_cfg);
851}
852
Sean Anderson09aa57a2020-09-15 10:44:47 -0400853static void msdc_set_mclk(struct udevice *dev,
854 struct msdc_host *host, enum bus_mode timing, u32 hz)
developerdc5a9aa2018-11-15 10:08:04 +0800855{
856 u32 mode;
857 u32 div;
858 u32 sclk;
859 u32 reg;
860
861 if (!hz) {
862 host->mclk = 0;
863 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
864 return;
865 }
866
867 if (host->dev_comp->clk_div_bits == 8)
868 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_HS400_CK_MODE);
869 else
870 clrbits_le32(&host->base->msdc_cfg,
871 MSDC_CFG_HS400_CK_MODE_EXT);
872
873 if (timing == UHS_DDR50 || timing == MMC_DDR_52 ||
874 timing == MMC_HS_400) {
875 if (timing == MMC_HS_400)
876 mode = 0x3;
877 else
878 mode = 0x2; /* ddr mode and use divisor */
879
880 if (hz >= (host->src_clk_freq >> 2)) {
881 div = 0; /* mean div = 1/4 */
882 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
883 } else {
884 div = (host->src_clk_freq + ((hz << 2) - 1)) /
885 (hz << 2);
886 sclk = (host->src_clk_freq >> 2) / div;
887 div = (div >> 1);
888 }
889
890 if (timing == MMC_HS_400 && hz >= (host->src_clk_freq >> 1)) {
891 if (host->dev_comp->clk_div_bits == 8)
892 setbits_le32(&host->base->msdc_cfg,
893 MSDC_CFG_HS400_CK_MODE);
894 else
895 setbits_le32(&host->base->msdc_cfg,
896 MSDC_CFG_HS400_CK_MODE_EXT);
897
898 sclk = host->src_clk_freq >> 1;
899 div = 0; /* div is ignore when bit18 is set */
900 }
901 } else if (hz >= host->src_clk_freq) {
902 mode = 0x1; /* no divisor */
903 div = 0;
904 sclk = host->src_clk_freq;
905 } else {
906 mode = 0x0; /* use divisor */
907 if (hz >= (host->src_clk_freq >> 1)) {
908 div = 0; /* mean div = 1/2 */
909 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
910 } else {
911 div = (host->src_clk_freq + ((hz << 2) - 1)) /
912 (hz << 2);
913 sclk = (host->src_clk_freq >> 2) / div;
914 }
915 }
916
917 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
918
919 if (host->dev_comp->clk_div_bits == 8) {
920 div = min(div, (u32)(MSDC_CFG_CKDIV_M >> MSDC_CFG_CKDIV_S));
921 clrsetbits_le32(&host->base->msdc_cfg,
922 MSDC_CFG_CKMOD_M | MSDC_CFG_CKDIV_M,
923 (mode << MSDC_CFG_CKMOD_S) |
924 (div << MSDC_CFG_CKDIV_S));
925 } else {
926 div = min(div, (u32)(MSDC_CFG_CKDIV_EXT_M >>
927 MSDC_CFG_CKDIV_EXT_S));
928 clrsetbits_le32(&host->base->msdc_cfg,
929 MSDC_CFG_CKMOD_EXT_M | MSDC_CFG_CKDIV_EXT_M,
930 (mode << MSDC_CFG_CKMOD_EXT_S) |
931 (div << MSDC_CFG_CKDIV_EXT_S));
932 }
933
934 readl_poll_timeout(&host->base->msdc_cfg, reg,
935 reg & MSDC_CFG_CKSTB, 1000000);
936
937 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
938 host->sclk = sclk;
939 host->mclk = hz;
940 host->timing = timing;
941
942 /* needed because clk changed. */
943 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
944
945 /*
946 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
947 * tune result of hs200/200Mhz is not suitable for 50Mhz
948 */
949 if (host->sclk <= 52000000) {
950 writel(host->def_tune_para.iocon, &host->base->msdc_iocon);
951 writel(host->def_tune_para.pad_tune,
952 &host->base->pad_tune);
953 } else {
954 writel(host->saved_tune_para.iocon, &host->base->msdc_iocon);
955 writel(host->saved_tune_para.pad_tune,
956 &host->base->pad_tune);
957 }
958
959 dev_dbg(dev, "sclk: %d, timing: %d\n", host->sclk, timing);
960}
961
962static int msdc_ops_set_ios(struct udevice *dev)
963{
Simon Glassfa20e932020-12-03 16:55:20 -0700964 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +0800965 struct msdc_host *host = dev_get_priv(dev);
966 struct mmc *mmc = &plat->mmc;
967 uint clock = mmc->clock;
968
969 msdc_set_buswidth(host, mmc->bus_width);
970
971 if (mmc->clk_disable)
972 clock = 0;
973 else if (clock < mmc->cfg->f_min)
974 clock = mmc->cfg->f_min;
975
976 if (host->mclk != clock || host->timing != mmc->selected_mode)
Sean Anderson09aa57a2020-09-15 10:44:47 -0400977 msdc_set_mclk(dev, host, mmc->selected_mode, clock);
developerdc5a9aa2018-11-15 10:08:04 +0800978
979 return 0;
980}
981
982static int msdc_ops_get_cd(struct udevice *dev)
983{
984 struct msdc_host *host = dev_get_priv(dev);
985 u32 val;
986
987 if (host->builtin_cd) {
988 val = readl(&host->base->msdc_ps);
developer399e4af2019-09-25 17:45:38 +0800989 val = !!(val & MSDC_PS_CDSTS);
990
991 return !val ^ host->cd_active_high;
developerdc5a9aa2018-11-15 10:08:04 +0800992 }
993
Fabien Parent8ed608a2019-03-24 16:46:34 +0100994#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +0800995 if (!host->gpio_cd.dev)
996 return 1;
997
998 return dm_gpio_get_value(&host->gpio_cd);
999#else
1000 return 1;
1001#endif
1002}
1003
1004static int msdc_ops_get_wp(struct udevice *dev)
1005{
Fabien Parent8ed608a2019-03-24 16:46:34 +01001006#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +08001007 struct msdc_host *host = dev_get_priv(dev);
1008
developerdc5a9aa2018-11-15 10:08:04 +08001009 if (!host->gpio_wp.dev)
1010 return 0;
1011
1012 return !dm_gpio_get_value(&host->gpio_wp);
1013#else
1014 return 0;
1015#endif
1016}
1017
Tom Rinidec7ea02024-05-20 13:35:03 -06001018#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
developerdc5a9aa2018-11-15 10:08:04 +08001019static u32 test_delay_bit(u32 delay, u32 bit)
1020{
1021 bit %= PAD_DELAY_MAX;
1022 return delay & (1 << bit);
1023}
1024
1025static int get_delay_len(u32 delay, u32 start_bit)
1026{
1027 int i;
1028
1029 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
1030 if (test_delay_bit(delay, start_bit + i) == 0)
1031 return i;
1032 }
1033
1034 return PAD_DELAY_MAX - start_bit;
1035}
1036
Sean Anderson09aa57a2020-09-15 10:44:47 -04001037static struct msdc_delay_phase get_best_delay(struct udevice *dev,
1038 struct msdc_host *host, u32 delay)
developerdc5a9aa2018-11-15 10:08:04 +08001039{
1040 int start = 0, len = 0;
1041 int start_final = 0, len_final = 0;
1042 u8 final_phase = 0xff;
1043 struct msdc_delay_phase delay_phase = { 0, };
1044
1045 if (delay == 0) {
1046 dev_err(dev, "phase error: [map:%x]\n", delay);
1047 delay_phase.final_phase = final_phase;
1048 return delay_phase;
1049 }
1050
1051 while (start < PAD_DELAY_MAX) {
1052 len = get_delay_len(delay, start);
1053 if (len_final < len) {
1054 start_final = start;
1055 len_final = len;
1056 }
1057
1058 start += len ? len : 1;
1059 if (len >= 12 && start_final < 4)
1060 break;
1061 }
1062
1063 /* The rule is to find the smallest delay cell */
1064 if (start_final == 0)
1065 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
1066 else
1067 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
1068
1069 dev_info(dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
1070 delay, len_final, final_phase);
1071
1072 delay_phase.maxlen = len_final;
1073 delay_phase.start = start_final;
1074 delay_phase.final_phase = final_phase;
1075 return delay_phase;
1076}
1077
developera2d3a6c2019-12-31 11:29:24 +08001078static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value)
1079{
1080 void __iomem *tune_reg = &host->base->pad_tune;
1081
1082 if (host->dev_comp->pad_tune0)
1083 tune_reg = &host->base->pad_tune0;
1084
1085 if (host->top_base)
1086 clrsetbits_le32(&host->top_base->emmc_top_cmd, PAD_CMD_RXDLY,
1087 value << PAD_CMD_RXDLY_S);
1088 else
1089 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1090 value << MSDC_PAD_TUNE_CMDRDLY_S);
1091}
1092
1093static inline void msdc_set_data_delay(struct msdc_host *host, u32 value)
1094{
1095 void __iomem *tune_reg = &host->base->pad_tune;
1096
1097 if (host->dev_comp->pad_tune0)
1098 tune_reg = &host->base->pad_tune0;
1099
1100 if (host->top_base)
1101 clrsetbits_le32(&host->top_base->emmc_top_control,
1102 PAD_DAT_RD_RXDLY, value << PAD_DAT_RD_RXDLY_S);
1103 else
1104 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1105 value << MSDC_PAD_TUNE_DATRRDLY_S);
1106}
1107
developer18f9fc72019-11-07 19:28:42 +08001108static int hs400_tune_response(struct udevice *dev, u32 opcode)
1109{
Simon Glassfa20e932020-12-03 16:55:20 -07001110 struct msdc_plat *plat = dev_get_plat(dev);
developer18f9fc72019-11-07 19:28:42 +08001111 struct msdc_host *host = dev_get_priv(dev);
1112 struct mmc *mmc = &plat->mmc;
1113 u32 cmd_delay = 0;
1114 struct msdc_delay_phase final_cmd_delay = { 0, };
1115 u8 final_delay;
1116 void __iomem *tune_reg = &host->base->pad_cmd_tune;
1117 int cmd_err;
1118 int i, j;
1119
1120 setbits_le32(&host->base->pad_cmd_tune, BIT(0));
1121
1122 if (mmc->selected_mode == MMC_HS_200 ||
1123 mmc->selected_mode == UHS_SDR104)
1124 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1125 host->hs200_cmd_int_delay <<
1126 MSDC_PAD_TUNE_CMDRRDLY_S);
1127
1128 if (host->r_smpl)
1129 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1130 else
1131 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1132
1133 for (i = 0; i < PAD_DELAY_MAX; i++) {
1134 clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3,
1135 i << PAD_CMD_TUNE_RX_DLY3_S);
1136
1137 for (j = 0; j < 3; j++) {
Marek Vasutdad81fb2024-02-20 09:36:23 +01001138 cmd_err = mmc_send_tuning(mmc, opcode);
developer18f9fc72019-11-07 19:28:42 +08001139 if (!cmd_err) {
1140 cmd_delay |= (1 << i);
1141 } else {
1142 cmd_delay &= ~(1 << i);
1143 break;
1144 }
1145 }
1146 }
1147
Sean Anderson09aa57a2020-09-15 10:44:47 -04001148 final_cmd_delay = get_best_delay(dev, host, cmd_delay);
developer18f9fc72019-11-07 19:28:42 +08001149 clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3,
1150 final_cmd_delay.final_phase <<
1151 PAD_CMD_TUNE_RX_DLY3_S);
1152 final_delay = final_cmd_delay.final_phase;
1153
developera2d3a6c2019-12-31 11:29:24 +08001154 dev_info(dev, "Final cmd pad delay: %x\n", final_delay);
developer18f9fc72019-11-07 19:28:42 +08001155 return final_delay == 0xff ? -EIO : 0;
1156}
1157
developerdc5a9aa2018-11-15 10:08:04 +08001158static int msdc_tune_response(struct udevice *dev, u32 opcode)
1159{
Simon Glassfa20e932020-12-03 16:55:20 -07001160 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001161 struct msdc_host *host = dev_get_priv(dev);
1162 struct mmc *mmc = &plat->mmc;
1163 u32 rise_delay = 0, fall_delay = 0;
1164 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1165 struct msdc_delay_phase internal_delay_phase;
1166 u8 final_delay, final_maxlen;
1167 u32 internal_delay = 0;
1168 void __iomem *tune_reg = &host->base->pad_tune;
1169 int cmd_err;
1170 int i, j;
1171
1172 if (host->dev_comp->pad_tune0)
1173 tune_reg = &host->base->pad_tune0;
1174
1175 if (mmc->selected_mode == MMC_HS_200 ||
1176 mmc->selected_mode == UHS_SDR104)
1177 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1178 host->hs200_cmd_int_delay <<
1179 MSDC_PAD_TUNE_CMDRRDLY_S);
1180
1181 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1182
1183 for (i = 0; i < PAD_DELAY_MAX; i++) {
1184 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1185 i << MSDC_PAD_TUNE_CMDRDLY_S);
1186
1187 for (j = 0; j < 3; j++) {
Marek Vasutdad81fb2024-02-20 09:36:23 +01001188 cmd_err = mmc_send_tuning(mmc, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001189 if (!cmd_err) {
1190 rise_delay |= (1 << i);
1191 } else {
1192 rise_delay &= ~(1 << i);
1193 break;
1194 }
1195 }
1196 }
1197
Sean Anderson09aa57a2020-09-15 10:44:47 -04001198 final_rise_delay = get_best_delay(dev, host, rise_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001199 /* if rising edge has enough margin, do not scan falling edge */
1200 if (final_rise_delay.maxlen >= 12 ||
1201 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1202 goto skip_fall;
1203
1204 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1205 for (i = 0; i < PAD_DELAY_MAX; i++) {
1206 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1207 i << MSDC_PAD_TUNE_CMDRDLY_S);
1208
1209 for (j = 0; j < 3; j++) {
Marek Vasutdad81fb2024-02-20 09:36:23 +01001210 cmd_err = mmc_send_tuning(mmc, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001211 if (!cmd_err) {
1212 fall_delay |= (1 << i);
1213 } else {
1214 fall_delay &= ~(1 << i);
1215 break;
1216 }
1217 }
1218 }
1219
Sean Anderson09aa57a2020-09-15 10:44:47 -04001220 final_fall_delay = get_best_delay(dev, host, fall_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001221
1222skip_fall:
1223 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1224 if (final_maxlen == final_rise_delay.maxlen) {
1225 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1226 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1227 final_rise_delay.final_phase <<
1228 MSDC_PAD_TUNE_CMDRDLY_S);
1229 final_delay = final_rise_delay.final_phase;
1230 } else {
1231 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1232 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1233 final_fall_delay.final_phase <<
1234 MSDC_PAD_TUNE_CMDRDLY_S);
1235 final_delay = final_fall_delay.final_phase;
1236 }
1237
1238 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
1239 goto skip_internal;
1240
1241 for (i = 0; i < PAD_DELAY_MAX; i++) {
1242 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1243 i << MSDC_PAD_TUNE_CMDRRDLY_S);
1244
Marek Vasutdad81fb2024-02-20 09:36:23 +01001245 cmd_err = mmc_send_tuning(mmc, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001246 if (!cmd_err)
1247 internal_delay |= (1 << i);
1248 }
1249
Fabien Parentf9ca4672020-10-15 18:38:18 +02001250 dev_dbg(dev, "Final internal delay: 0x%x\n", internal_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001251
Sean Anderson09aa57a2020-09-15 10:44:47 -04001252 internal_delay_phase = get_best_delay(dev, host, internal_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001253 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1254 internal_delay_phase.final_phase <<
1255 MSDC_PAD_TUNE_CMDRRDLY_S);
1256
1257skip_internal:
Fabien Parentf9ca4672020-10-15 18:38:18 +02001258 dev_dbg(dev, "Final cmd pad delay: %x\n", final_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001259 return final_delay == 0xff ? -EIO : 0;
1260}
1261
1262static int msdc_tune_data(struct udevice *dev, u32 opcode)
1263{
Simon Glassfa20e932020-12-03 16:55:20 -07001264 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001265 struct msdc_host *host = dev_get_priv(dev);
1266 struct mmc *mmc = &plat->mmc;
1267 u32 rise_delay = 0, fall_delay = 0;
1268 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1269 u8 final_delay, final_maxlen;
1270 void __iomem *tune_reg = &host->base->pad_tune;
developerdc5a9aa2018-11-15 10:08:04 +08001271 int i, ret;
1272
1273 if (host->dev_comp->pad_tune0)
1274 tune_reg = &host->base->pad_tune0;
1275
1276 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1277 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1278
1279 for (i = 0; i < PAD_DELAY_MAX; i++) {
1280 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1281 i << MSDC_PAD_TUNE_DATRRDLY_S);
1282
Marek Vasutdad81fb2024-02-20 09:36:23 +01001283 ret = mmc_send_tuning(mmc, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001284 if (!ret) {
1285 rise_delay |= (1 << i);
Marek Vasutdad81fb2024-02-20 09:36:23 +01001286 } else {
developerdc5a9aa2018-11-15 10:08:04 +08001287 /* in this case, retune response is needed */
1288 ret = msdc_tune_response(dev, opcode);
1289 if (ret)
1290 break;
1291 }
1292 }
1293
Sean Anderson09aa57a2020-09-15 10:44:47 -04001294 final_rise_delay = get_best_delay(dev, host, rise_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001295 if (final_rise_delay.maxlen >= 12 ||
1296 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1297 goto skip_fall;
1298
1299 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1300 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1301
1302 for (i = 0; i < PAD_DELAY_MAX; i++) {
1303 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1304 i << MSDC_PAD_TUNE_DATRRDLY_S);
1305
Marek Vasutdad81fb2024-02-20 09:36:23 +01001306 ret = mmc_send_tuning(mmc, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001307 if (!ret) {
1308 fall_delay |= (1 << i);
Marek Vasutdad81fb2024-02-20 09:36:23 +01001309 } else {
developerdc5a9aa2018-11-15 10:08:04 +08001310 /* in this case, retune response is needed */
1311 ret = msdc_tune_response(dev, opcode);
1312 if (ret)
1313 break;
1314 }
1315 }
1316
Sean Anderson09aa57a2020-09-15 10:44:47 -04001317 final_fall_delay = get_best_delay(dev, host, fall_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001318
1319skip_fall:
1320 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1321 if (final_maxlen == final_rise_delay.maxlen) {
1322 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1323 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1324 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1325 final_rise_delay.final_phase <<
1326 MSDC_PAD_TUNE_DATRRDLY_S);
1327 final_delay = final_rise_delay.final_phase;
1328 } else {
1329 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1330 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1331 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1332 final_fall_delay.final_phase <<
1333 MSDC_PAD_TUNE_DATRRDLY_S);
1334 final_delay = final_fall_delay.final_phase;
1335 }
1336
1337 if (mmc->selected_mode == MMC_HS_200 ||
1338 mmc->selected_mode == UHS_SDR104)
1339 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATWRDLY_M,
1340 host->hs200_write_int_delay <<
1341 MSDC_PAD_TUNE_DATWRDLY_S);
1342
Fabien Parentf9ca4672020-10-15 18:38:18 +02001343 dev_dbg(dev, "Final data pad delay: %x\n", final_delay);
developerdc5a9aa2018-11-15 10:08:04 +08001344
1345 return final_delay == 0xff ? -EIO : 0;
1346}
1347
developer18f9fc72019-11-07 19:28:42 +08001348/*
1349 * MSDC IP which supports data tune + async fifo can do CMD/DAT tune
1350 * together, which can save the tuning time.
1351 */
1352static int msdc_tune_together(struct udevice *dev, u32 opcode)
1353{
Simon Glassfa20e932020-12-03 16:55:20 -07001354 struct msdc_plat *plat = dev_get_plat(dev);
developer18f9fc72019-11-07 19:28:42 +08001355 struct msdc_host *host = dev_get_priv(dev);
1356 struct mmc *mmc = &plat->mmc;
1357 u32 rise_delay = 0, fall_delay = 0;
1358 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1359 u8 final_delay, final_maxlen;
developer18f9fc72019-11-07 19:28:42 +08001360 int i, ret;
1361
developer18f9fc72019-11-07 19:28:42 +08001362 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1363 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1364
1365 for (i = 0; i < PAD_DELAY_MAX; i++) {
developera2d3a6c2019-12-31 11:29:24 +08001366 msdc_set_cmd_delay(host, i);
1367 msdc_set_data_delay(host, i);
Marek Vasutdad81fb2024-02-20 09:36:23 +01001368 ret = mmc_send_tuning(mmc, opcode);
developer18f9fc72019-11-07 19:28:42 +08001369 if (!ret)
1370 rise_delay |= (1 << i);
1371 }
1372
Sean Anderson09aa57a2020-09-15 10:44:47 -04001373 final_rise_delay = get_best_delay(dev, host, rise_delay);
developer18f9fc72019-11-07 19:28:42 +08001374 if (final_rise_delay.maxlen >= 12 ||
1375 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1376 goto skip_fall;
1377
1378 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1379 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1380
1381 for (i = 0; i < PAD_DELAY_MAX; i++) {
developera2d3a6c2019-12-31 11:29:24 +08001382 msdc_set_cmd_delay(host, i);
1383 msdc_set_data_delay(host, i);
Marek Vasutdad81fb2024-02-20 09:36:23 +01001384 ret = mmc_send_tuning(mmc, opcode);
developer18f9fc72019-11-07 19:28:42 +08001385 if (!ret)
1386 fall_delay |= (1 << i);
1387 }
1388
Sean Anderson09aa57a2020-09-15 10:44:47 -04001389 final_fall_delay = get_best_delay(dev, host, fall_delay);
developer18f9fc72019-11-07 19:28:42 +08001390
1391skip_fall:
1392 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1393 if (final_maxlen == final_rise_delay.maxlen) {
1394 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1395 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
developer18f9fc72019-11-07 19:28:42 +08001396 final_delay = final_rise_delay.final_phase;
1397 } else {
1398 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1399 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
developer18f9fc72019-11-07 19:28:42 +08001400 final_delay = final_fall_delay.final_phase;
1401 }
1402
developera2d3a6c2019-12-31 11:29:24 +08001403 msdc_set_cmd_delay(host, final_delay);
1404 msdc_set_data_delay(host, final_delay);
developer18f9fc72019-11-07 19:28:42 +08001405
developera2d3a6c2019-12-31 11:29:24 +08001406 dev_info(dev, "Final pad delay: %x\n", final_delay);
developer18f9fc72019-11-07 19:28:42 +08001407 return final_delay == 0xff ? -EIO : 0;
1408}
1409
developerdc5a9aa2018-11-15 10:08:04 +08001410static int msdc_execute_tuning(struct udevice *dev, uint opcode)
1411{
Simon Glassfa20e932020-12-03 16:55:20 -07001412 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001413 struct msdc_host *host = dev_get_priv(dev);
1414 struct mmc *mmc = &plat->mmc;
developer18f9fc72019-11-07 19:28:42 +08001415 int ret = 0;
1416
1417 if (host->dev_comp->data_tune && host->dev_comp->async_fifo) {
1418 ret = msdc_tune_together(dev, opcode);
1419 if (ret == -EIO) {
1420 dev_err(dev, "Tune fail!\n");
1421 return ret;
1422 }
1423
1424 if (mmc->selected_mode == MMC_HS_400) {
1425 clrbits_le32(&host->base->msdc_iocon,
1426 MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL);
1427 clrsetbits_le32(&host->base->pad_tune,
1428 MSDC_PAD_TUNE_DATRRDLY_M, 0);
developerdc5a9aa2018-11-15 10:08:04 +08001429
developer18f9fc72019-11-07 19:28:42 +08001430 writel(host->hs400_ds_delay, &host->base->pad_ds_tune);
1431 /* for hs400 mode it must be set to 0 */
1432 clrbits_le32(&host->base->patch_bit2,
1433 MSDC_PB2_CFGCRCSTS);
1434 host->hs400_mode = true;
1435 }
1436 goto tune_done;
developerdc5a9aa2018-11-15 10:08:04 +08001437 }
1438
developer18f9fc72019-11-07 19:28:42 +08001439 if (mmc->selected_mode == MMC_HS_400)
1440 ret = hs400_tune_response(dev, opcode);
1441 else
1442 ret = msdc_tune_response(dev, opcode);
developerdc5a9aa2018-11-15 10:08:04 +08001443 if (ret == -EIO) {
1444 dev_err(dev, "Tune response fail!\n");
1445 return ret;
1446 }
1447
developer18f9fc72019-11-07 19:28:42 +08001448 if (mmc->selected_mode != MMC_HS_400) {
developerdc5a9aa2018-11-15 10:08:04 +08001449 ret = msdc_tune_data(dev, opcode);
developer18f9fc72019-11-07 19:28:42 +08001450 if (ret == -EIO) {
developerdc5a9aa2018-11-15 10:08:04 +08001451 dev_err(dev, "Tune data fail!\n");
developer18f9fc72019-11-07 19:28:42 +08001452 return ret;
1453 }
developerdc5a9aa2018-11-15 10:08:04 +08001454 }
1455
developer18f9fc72019-11-07 19:28:42 +08001456tune_done:
developerdc5a9aa2018-11-15 10:08:04 +08001457 host->saved_tune_para.iocon = readl(&host->base->msdc_iocon);
1458 host->saved_tune_para.pad_tune = readl(&host->base->pad_tune);
developer18f9fc72019-11-07 19:28:42 +08001459 host->saved_tune_para.pad_cmd_tune = readl(&host->base->pad_cmd_tune);
developerdc5a9aa2018-11-15 10:08:04 +08001460
1461 return ret;
1462}
1463#endif
1464
1465static void msdc_init_hw(struct msdc_host *host)
1466{
1467 u32 val;
1468 void __iomem *tune_reg = &host->base->pad_tune;
developer7295c892020-11-12 16:37:02 +08001469 void __iomem *rd_dly0_reg = &host->base->pad_tune0;
1470 void __iomem *rd_dly1_reg = &host->base->pad_tune1;
developerdc5a9aa2018-11-15 10:08:04 +08001471
developer7295c892020-11-12 16:37:02 +08001472 if (host->dev_comp->pad_tune0) {
developerdc5a9aa2018-11-15 10:08:04 +08001473 tune_reg = &host->base->pad_tune0;
developer7295c892020-11-12 16:37:02 +08001474 rd_dly0_reg = &host->base->dat_rd_dly[0];
1475 rd_dly1_reg = &host->base->dat_rd_dly[1];
1476 }
developerdc5a9aa2018-11-15 10:08:04 +08001477
1478 /* Configure to MMC/SD mode, clock free running */
1479 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_MODE);
1480
1481 /* Use PIO mode */
1482 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_PIO);
1483
1484 /* Reset */
1485 msdc_reset_hw(host);
1486
1487 /* Enable/disable hw card detection according to fdt option */
1488 if (host->builtin_cd)
1489 clrsetbits_le32(&host->base->msdc_ps,
1490 MSDC_PS_CDDBCE_M,
1491 (DEFAULT_CD_DEBOUNCE << MSDC_PS_CDDBCE_S) |
1492 MSDC_PS_CDEN);
1493 else
1494 clrbits_le32(&host->base->msdc_ps, MSDC_PS_CDEN);
1495
1496 /* Clear all interrupts */
1497 val = readl(&host->base->msdc_int);
1498 writel(val, &host->base->msdc_int);
1499
1500 /* Enable data & cmd interrupts */
1501 writel(DATA_INTS_MASK | CMD_INTS_MASK, &host->base->msdc_inten);
1502
developer1b0c7ed2022-09-09 19:59:19 +08001503 if (host->top_base) {
1504 writel(0, &host->top_base->emmc_top_control);
1505 writel(0, &host->top_base->emmc_top_cmd);
1506 } else {
1507 writel(0, tune_reg);
1508 }
developerdc5a9aa2018-11-15 10:08:04 +08001509 writel(0, &host->base->msdc_iocon);
1510
1511 if (host->r_smpl)
1512 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1513 else
1514 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1515
1516 writel(0x403c0046, &host->base->patch_bit0);
1517 writel(0xffff4089, &host->base->patch_bit1);
1518
developer1b0c7ed2022-09-09 19:59:19 +08001519 if (host->dev_comp->stop_clk_fix) {
developerdc5a9aa2018-11-15 10:08:04 +08001520 clrsetbits_le32(&host->base->patch_bit1, MSDC_PB1_STOP_DLY_M,
1521 3 << MSDC_PB1_STOP_DLY_S);
developer1b0c7ed2022-09-09 19:59:19 +08001522 clrbits_le32(&host->base->sdc_fifo_cfg,
1523 SDC_FIFO_CFG_WRVALIDSEL);
1524 clrbits_le32(&host->base->sdc_fifo_cfg,
1525 SDC_FIFO_CFG_RDVALIDSEL);
1526 }
developerdc5a9aa2018-11-15 10:08:04 +08001527
1528 if (host->dev_comp->busy_check)
1529 clrbits_le32(&host->base->patch_bit1, (1 << 7));
1530
1531 setbits_le32(&host->base->emmc50_cfg0, EMMC50_CFG_CFCSTS_SEL);
1532
1533 if (host->dev_comp->async_fifo) {
1534 clrsetbits_le32(&host->base->patch_bit2, MSDC_PB2_RESPWAIT_M,
1535 3 << MSDC_PB2_RESPWAIT_S);
1536
1537 if (host->dev_comp->enhance_rx) {
developera2d3a6c2019-12-31 11:29:24 +08001538 if (host->top_base)
1539 setbits_le32(&host->top_base->emmc_top_control,
1540 SDC_RX_ENH_EN);
1541 else
1542 setbits_le32(&host->base->sdc_adv_cfg0,
1543 SDC_RX_ENHANCE_EN);
developerdc5a9aa2018-11-15 10:08:04 +08001544 } else {
1545 clrsetbits_le32(&host->base->patch_bit2,
1546 MSDC_PB2_RESPSTSENSEL_M,
1547 2 << MSDC_PB2_RESPSTSENSEL_S);
1548 clrsetbits_le32(&host->base->patch_bit2,
1549 MSDC_PB2_CRCSTSENSEL_M,
1550 2 << MSDC_PB2_CRCSTSENSEL_S);
1551 }
1552
1553 /* use async fifo to avoid tune internal delay */
1554 clrbits_le32(&host->base->patch_bit2,
1555 MSDC_PB2_CFGRESP);
1556 clrbits_le32(&host->base->patch_bit2,
1557 MSDC_PB2_CFGCRCSTS);
1558 }
1559
1560 if (host->dev_comp->data_tune) {
developer1b0c7ed2022-09-09 19:59:19 +08001561 if (host->top_base) {
1562 setbits_le32(&host->top_base->emmc_top_control,
1563 PAD_DAT_RD_RXDLY_SEL);
1564 clrbits_le32(&host->top_base->emmc_top_control,
1565 DATA_K_VALUE_SEL);
1566 setbits_le32(&host->top_base->emmc_top_cmd,
1567 PAD_CMD_RD_RXDLY_SEL);
1568 } else {
1569 setbits_le32(tune_reg,
1570 MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL);
1571 clrsetbits_le32(&host->base->patch_bit0,
1572 MSDC_INT_DAT_LATCH_CK_SEL_M,
1573 host->latch_ck <<
1574 MSDC_INT_DAT_LATCH_CK_SEL_S);
1575 }
developerdc5a9aa2018-11-15 10:08:04 +08001576 } else {
1577 /* choose clock tune */
developer1b0c7ed2022-09-09 19:59:19 +08001578 if (host->top_base)
1579 setbits_le32(&host->top_base->emmc_top_control,
1580 PAD_RXDLY_SEL);
1581 else
1582 setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
developerdc5a9aa2018-11-15 10:08:04 +08001583 }
1584
developer7295c892020-11-12 16:37:02 +08001585 if (host->dev_comp->builtin_pad_ctrl) {
1586 /* Set pins driving strength */
1587 writel(MSDC_PAD_CTRL0_CLKPD | MSDC_PAD_CTRL0_CLKSMT |
1588 MSDC_PAD_CTRL0_CLKIES | (4 << MSDC_PAD_CTRL0_CLKDRVN_S) |
1589 (4 << MSDC_PAD_CTRL0_CLKDRVP_S), &host->base->pad_ctrl0);
1590 writel(MSDC_PAD_CTRL1_CMDPU | MSDC_PAD_CTRL1_CMDSMT |
1591 MSDC_PAD_CTRL1_CMDIES | (4 << MSDC_PAD_CTRL1_CMDDRVN_S) |
1592 (4 << MSDC_PAD_CTRL1_CMDDRVP_S), &host->base->pad_ctrl1);
1593 writel(MSDC_PAD_CTRL2_DATPU | MSDC_PAD_CTRL2_DATSMT |
1594 MSDC_PAD_CTRL2_DATIES | (4 << MSDC_PAD_CTRL2_DATDRVN_S) |
1595 (4 << MSDC_PAD_CTRL2_DATDRVP_S), &host->base->pad_ctrl2);
1596 }
1597
1598 if (host->dev_comp->default_pad_dly) {
1599 /* Default pad delay may be needed if tuning not enabled */
1600 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CLKTDLY_M |
1601 MSDC_PAD_TUNE_CMDRRDLY_M |
1602 MSDC_PAD_TUNE_CMDRDLY_M |
1603 MSDC_PAD_TUNE_DATRRDLY_M |
1604 MSDC_PAD_TUNE_DATWRDLY_M,
1605 (0x10 << MSDC_PAD_TUNE_CLKTDLY_S) |
1606 (0x10 << MSDC_PAD_TUNE_CMDRRDLY_S) |
1607 (0x10 << MSDC_PAD_TUNE_CMDRDLY_S) |
1608 (0x10 << MSDC_PAD_TUNE_DATRRDLY_S) |
1609 (0x10 << MSDC_PAD_TUNE_DATWRDLY_S));
1610
1611 writel((0x10 << MSDC_PAD_TUNE0_DAT0RDDLY_S) |
1612 (0x10 << MSDC_PAD_TUNE0_DAT1RDDLY_S) |
1613 (0x10 << MSDC_PAD_TUNE0_DAT2RDDLY_S) |
1614 (0x10 << MSDC_PAD_TUNE0_DAT3RDDLY_S),
1615 rd_dly0_reg);
1616
1617 writel((0x10 << MSDC_PAD_TUNE1_DAT4RDDLY_S) |
1618 (0x10 << MSDC_PAD_TUNE1_DAT5RDDLY_S) |
1619 (0x10 << MSDC_PAD_TUNE1_DAT6RDDLY_S) |
1620 (0x10 << MSDC_PAD_TUNE1_DAT7RDDLY_S),
1621 rd_dly1_reg);
1622 }
1623
developerdc5a9aa2018-11-15 10:08:04 +08001624 /* Configure to enable SDIO mode otherwise sdio cmd5 won't work */
1625 setbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIO);
1626
1627 /* disable detecting SDIO device interrupt function */
1628 clrbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIOIDE);
1629
1630 /* Configure to default data timeout */
1631 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
1632 3 << SDC_CFG_DTOC_S);
1633
developerdc5a9aa2018-11-15 10:08:04 +08001634
1635 host->def_tune_para.iocon = readl(&host->base->msdc_iocon);
1636 host->def_tune_para.pad_tune = readl(&host->base->pad_tune);
1637}
1638
1639static void msdc_ungate_clock(struct msdc_host *host)
1640{
1641 clk_enable(&host->src_clk);
1642 clk_enable(&host->h_clk);
Fabien Parent297fa1a2019-03-24 16:46:32 +01001643 if (host->src_clk_cg.dev)
1644 clk_enable(&host->src_clk_cg);
Christian Marangi2e43de22024-06-24 23:03:34 +02001645
1646 if (host->axi_cg_clk.dev)
1647 clk_enable(&host->axi_cg_clk);
1648 if (host->ahb_cg_clk.dev)
1649 clk_enable(&host->ahb_cg_clk);
developerdc5a9aa2018-11-15 10:08:04 +08001650}
1651
1652static int msdc_drv_probe(struct udevice *dev)
1653{
1654 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001655 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001656 struct msdc_host *host = dev_get_priv(dev);
1657 struct mmc_config *cfg = &plat->cfg;
1658
1659 cfg->name = dev->name;
1660
1661 host->dev_comp = (struct msdc_compatible *)dev_get_driver_data(dev);
1662
Christian Marangi2e43de22024-06-24 23:03:34 +02001663 if (host->dev_comp->use_internal_cd)
1664 host->builtin_cd = 1;
1665
developerdc5a9aa2018-11-15 10:08:04 +08001666 host->src_clk_freq = clk_get_rate(&host->src_clk);
1667
1668 if (host->dev_comp->clk_div_bits == 8)
1669 cfg->f_min = host->src_clk_freq / (4 * 255);
1670 else
1671 cfg->f_min = host->src_clk_freq / (4 * 4095);
developerdc5a9aa2018-11-15 10:08:04 +08001672
developer13b920a2021-04-20 16:37:10 +08001673 if (cfg->f_min < MIN_BUS_CLK)
1674 cfg->f_min = MIN_BUS_CLK;
1675
Daniel Golle1bbd66a2021-03-15 15:31:11 +00001676 if (cfg->f_max < cfg->f_min || cfg->f_max > host->src_clk_freq)
1677 cfg->f_max = host->src_clk_freq;
developerdc462a82020-11-12 16:37:07 +08001678
Julien Masson7d6a56e2023-12-04 14:41:45 +01001679 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
developerdc5a9aa2018-11-15 10:08:04 +08001680 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1681
1682 host->mmc = &plat->mmc;
1683 host->timeout_ns = 100000000;
developerc7310742020-11-12 16:36:57 +08001684 host->timeout_clks = 3 * (1 << SCLK_CYCLES_SHIFT);
developerdc5a9aa2018-11-15 10:08:04 +08001685
1686#ifdef CONFIG_PINCTRL
1687 pinctrl_select_state(dev, "default");
1688#endif
1689
1690 msdc_ungate_clock(host);
1691 msdc_init_hw(host);
1692
1693 upriv->mmc = &plat->mmc;
1694
1695 return 0;
1696}
1697
Simon Glassaad29ae2020-12-03 16:55:21 -07001698static int msdc_of_to_plat(struct udevice *dev)
developerdc5a9aa2018-11-15 10:08:04 +08001699{
Simon Glassfa20e932020-12-03 16:55:20 -07001700 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001701 struct msdc_host *host = dev_get_priv(dev);
1702 struct mmc_config *cfg = &plat->cfg;
developera2d3a6c2019-12-31 11:29:24 +08001703 fdt_addr_t base, top_base;
developerdc5a9aa2018-11-15 10:08:04 +08001704 int ret;
1705
developera2d3a6c2019-12-31 11:29:24 +08001706 base = dev_read_addr(dev);
1707 if (base == FDT_ADDR_T_NONE)
developerdc5a9aa2018-11-15 10:08:04 +08001708 return -EINVAL;
developera2d3a6c2019-12-31 11:29:24 +08001709 host->base = map_sysmem(base, 0);
1710
1711 top_base = dev_read_addr_index(dev, 1);
1712 if (top_base == FDT_ADDR_T_NONE)
1713 host->top_base = NULL;
1714 else
1715 host->top_base = map_sysmem(top_base, 0);
developerdc5a9aa2018-11-15 10:08:04 +08001716
1717 ret = mmc_of_parse(dev, cfg);
1718 if (ret)
1719 return ret;
1720
1721 ret = clk_get_by_name(dev, "source", &host->src_clk);
1722 if (ret < 0)
1723 return ret;
1724
1725 ret = clk_get_by_name(dev, "hclk", &host->h_clk);
1726 if (ret < 0)
1727 return ret;
1728
Fabien Parent297fa1a2019-03-24 16:46:32 +01001729 clk_get_by_name(dev, "source_cg", &host->src_clk_cg); /* optional */
1730
Christian Marangi2e43de22024-06-24 23:03:34 +02001731 /* upstream linux clock */
1732 clk_get_by_name(dev, "axi_cg", &host->axi_cg_clk); /* optional */
1733 clk_get_by_name(dev, "ahb_cg", &host->ahb_cg_clk); /* optional */
1734
Fabien Parent8ed608a2019-03-24 16:46:34 +01001735#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +08001736 gpio_request_by_name(dev, "wp-gpios", 0, &host->gpio_wp, GPIOD_IS_IN);
1737 gpio_request_by_name(dev, "cd-gpios", 0, &host->gpio_cd, GPIOD_IS_IN);
1738#endif
1739
1740 host->hs400_ds_delay = dev_read_u32_default(dev, "hs400-ds-delay", 0);
Christian Marangi2e43de22024-06-24 23:03:34 +02001741 if (dev_read_u32(dev, "mediatek,hs200-cmd-int-delay",
1742 &host->hs200_cmd_int_delay))
1743 host->hs200_cmd_int_delay =
1744 dev_read_u32_default(dev, "cmd_int_delay", 0);
1745
developerdc5a9aa2018-11-15 10:08:04 +08001746 host->hs200_write_int_delay =
1747 dev_read_u32_default(dev, "write_int_delay", 0);
Christian Marangi2e43de22024-06-24 23:03:34 +02001748
1749 if (dev_read_u32(dev, "mediatek,latch-ck", &host->latch_ck))
1750 host->latch_ck = dev_read_u32_default(dev, "latch-ck", 0);
1751
developerdc5a9aa2018-11-15 10:08:04 +08001752 host->r_smpl = dev_read_u32_default(dev, "r_smpl", 0);
Christian Marangi2e43de22024-06-24 23:03:34 +02001753 if (dev_read_bool(dev, "mediatek,hs400-cmd-resp-sel-rising"))
1754 host->r_smpl = 1;
1755
developerdc5a9aa2018-11-15 10:08:04 +08001756 host->builtin_cd = dev_read_u32_default(dev, "builtin-cd", 0);
developer399e4af2019-09-25 17:45:38 +08001757 host->cd_active_high = dev_read_bool(dev, "cd-active-high");
developerdc5a9aa2018-11-15 10:08:04 +08001758
1759 return 0;
1760}
1761
1762static int msdc_drv_bind(struct udevice *dev)
1763{
Simon Glassfa20e932020-12-03 16:55:20 -07001764 struct msdc_plat *plat = dev_get_plat(dev);
developerdc5a9aa2018-11-15 10:08:04 +08001765
1766 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1767}
1768
Julien Massonff2d8bc2021-11-05 14:34:14 +01001769static int msdc_ops_wait_dat0(struct udevice *dev, int state, int timeout_us)
1770{
1771 struct msdc_host *host = dev_get_priv(dev);
1772 int ret;
1773 u32 reg;
1774
1775 ret = readl_poll_sleep_timeout(&host->base->msdc_ps, reg,
1776 !!(reg & MSDC_PS_DAT0) == !!state,
1777 1000, /* 1 ms */
1778 timeout_us);
1779
1780 return ret;
1781}
1782
developerdc5a9aa2018-11-15 10:08:04 +08001783static const struct dm_mmc_ops msdc_ops = {
1784 .send_cmd = msdc_ops_send_cmd,
1785 .set_ios = msdc_ops_set_ios,
1786 .get_cd = msdc_ops_get_cd,
1787 .get_wp = msdc_ops_get_wp,
Tom Rinidec7ea02024-05-20 13:35:03 -06001788#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
developerdc5a9aa2018-11-15 10:08:04 +08001789 .execute_tuning = msdc_execute_tuning,
1790#endif
Julien Massonff2d8bc2021-11-05 14:34:14 +01001791 .wait_dat0 = msdc_ops_wait_dat0,
developerdc5a9aa2018-11-15 10:08:04 +08001792};
1793
developer607faf72019-09-25 17:45:37 +08001794static const struct msdc_compatible mt7620_compat = {
1795 .clk_div_bits = 8,
developer607faf72019-09-25 17:45:37 +08001796 .pad_tune0 = false,
1797 .async_fifo = false,
1798 .data_tune = false,
1799 .busy_check = false,
1800 .stop_clk_fix = false,
developer7295c892020-11-12 16:37:02 +08001801 .enhance_rx = false,
1802 .builtin_pad_ctrl = true,
1803 .default_pad_dly = true,
Christian Marangi2e43de22024-06-24 23:03:34 +02001804 .use_internal_cd = true,
developer607faf72019-09-25 17:45:37 +08001805};
1806
developer56148242022-05-20 11:23:26 +08001807static const struct msdc_compatible mt7621_compat = {
1808 .clk_div_bits = 8,
1809 .pad_tune0 = false,
1810 .async_fifo = true,
1811 .data_tune = true,
1812 .busy_check = false,
1813 .stop_clk_fix = false,
1814 .enhance_rx = false,
1815 .builtin_pad_ctrl = true,
1816 .default_pad_dly = true,
1817};
1818
developer837d3342020-01-10 16:30:32 +08001819static const struct msdc_compatible mt7622_compat = {
1820 .clk_div_bits = 12,
1821 .pad_tune0 = true,
1822 .async_fifo = true,
1823 .data_tune = true,
1824 .busy_check = true,
1825 .stop_clk_fix = true,
1826};
1827
developerdc5a9aa2018-11-15 10:08:04 +08001828static const struct msdc_compatible mt7623_compat = {
1829 .clk_div_bits = 12,
1830 .pad_tune0 = true,
1831 .async_fifo = true,
1832 .data_tune = true,
1833 .busy_check = false,
1834 .stop_clk_fix = false,
Christian Marangi2e43de22024-06-24 23:03:34 +02001835 .enhance_rx = false,
developerdc5a9aa2018-11-15 10:08:04 +08001836};
1837
developer1b0c7ed2022-09-09 19:59:19 +08001838static const struct msdc_compatible mt7986_compat = {
1839 .clk_div_bits = 12,
1840 .pad_tune0 = true,
1841 .async_fifo = true,
1842 .data_tune = true,
1843 .busy_check = true,
1844 .stop_clk_fix = true,
1845 .enhance_rx = true,
1846};
1847
1848static const struct msdc_compatible mt7981_compat = {
1849 .clk_div_bits = 12,
1850 .pad_tune0 = true,
1851 .async_fifo = true,
1852 .data_tune = true,
1853 .busy_check = true,
1854 .stop_clk_fix = true,
1855};
1856
developera2d3a6c2019-12-31 11:29:24 +08001857static const struct msdc_compatible mt8512_compat = {
1858 .clk_div_bits = 12,
developera2d3a6c2019-12-31 11:29:24 +08001859 .pad_tune0 = true,
1860 .async_fifo = true,
1861 .data_tune = true,
1862 .busy_check = true,
1863 .stop_clk_fix = true,
1864};
1865
Fabien Parent1d520a42019-03-24 16:46:33 +01001866static const struct msdc_compatible mt8516_compat = {
1867 .clk_div_bits = 12,
1868 .pad_tune0 = true,
1869 .async_fifo = true,
1870 .data_tune = true,
1871 .busy_check = true,
1872 .stop_clk_fix = true,
1873};
1874
Fabien Parentc7da6982019-08-12 20:26:58 +02001875static const struct msdc_compatible mt8183_compat = {
1876 .clk_div_bits = 12,
1877 .pad_tune0 = true,
1878 .async_fifo = true,
1879 .data_tune = true,
1880 .busy_check = true,
1881 .stop_clk_fix = true,
1882};
1883
developerdc5a9aa2018-11-15 10:08:04 +08001884static const struct udevice_id msdc_ids[] = {
developer607faf72019-09-25 17:45:37 +08001885 { .compatible = "mediatek,mt7620-mmc", .data = (ulong)&mt7620_compat },
developer56148242022-05-20 11:23:26 +08001886 { .compatible = "mediatek,mt7621-mmc", .data = (ulong)&mt7621_compat },
developer837d3342020-01-10 16:30:32 +08001887 { .compatible = "mediatek,mt7622-mmc", .data = (ulong)&mt7622_compat },
developerdc5a9aa2018-11-15 10:08:04 +08001888 { .compatible = "mediatek,mt7623-mmc", .data = (ulong)&mt7623_compat },
developer1b0c7ed2022-09-09 19:59:19 +08001889 { .compatible = "mediatek,mt7986-mmc", .data = (ulong)&mt7986_compat },
1890 { .compatible = "mediatek,mt7981-mmc", .data = (ulong)&mt7981_compat },
developera2d3a6c2019-12-31 11:29:24 +08001891 { .compatible = "mediatek,mt8512-mmc", .data = (ulong)&mt8512_compat },
Fabien Parent1d520a42019-03-24 16:46:33 +01001892 { .compatible = "mediatek,mt8516-mmc", .data = (ulong)&mt8516_compat },
Fabien Parentc7da6982019-08-12 20:26:58 +02001893 { .compatible = "mediatek,mt8183-mmc", .data = (ulong)&mt8183_compat },
developerdc5a9aa2018-11-15 10:08:04 +08001894 {}
1895};
1896
1897U_BOOT_DRIVER(mtk_sd_drv) = {
1898 .name = "mtk_sd",
1899 .id = UCLASS_MMC,
1900 .of_match = msdc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001901 .of_to_plat = msdc_of_to_plat,
developerdc5a9aa2018-11-15 10:08:04 +08001902 .bind = msdc_drv_bind,
1903 .probe = msdc_drv_probe,
1904 .ops = &msdc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -07001905 .plat_auto = sizeof(struct msdc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -07001906 .priv_auto = sizeof(struct msdc_host),
developerdc5a9aa2018-11-15 10:08:04 +08001907};