blob: ffd647fac7a60a40cf67f4f83129d6face136774 [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>
10#include <common.h>
11#include <dm.h>
12#include <mmc.h>
13#include <errno.h>
14#include <malloc.h>
15#include <stdbool.h>
developer7462c842019-07-19 11:04:47 +080016#include <watchdog.h>
developerdc5a9aa2018-11-15 10:08:04 +080017#include <asm/gpio.h>
18#include <dm/pinctrl.h>
19#include <linux/bitops.h>
20#include <linux/io.h>
21#include <linux/iopoll.h>
22
23/* MSDC_CFG */
24#define MSDC_CFG_HS400_CK_MODE_EXT BIT(22)
25#define MSDC_CFG_CKMOD_EXT_M 0x300000
26#define MSDC_CFG_CKMOD_EXT_S 20
27#define MSDC_CFG_CKDIV_EXT_M 0xfff00
28#define MSDC_CFG_CKDIV_EXT_S 8
29#define MSDC_CFG_HS400_CK_MODE BIT(18)
30#define MSDC_CFG_CKMOD_M 0x30000
31#define MSDC_CFG_CKMOD_S 16
32#define MSDC_CFG_CKDIV_M 0xff00
33#define MSDC_CFG_CKDIV_S 8
34#define MSDC_CFG_CKSTB BIT(7)
35#define MSDC_CFG_PIO BIT(3)
36#define MSDC_CFG_RST BIT(2)
37#define MSDC_CFG_CKPDN BIT(1)
38#define MSDC_CFG_MODE BIT(0)
39
40/* MSDC_IOCON */
41#define MSDC_IOCON_W_DSPL BIT(8)
42#define MSDC_IOCON_DSPL BIT(2)
43#define MSDC_IOCON_RSPL BIT(1)
44
45/* MSDC_PS */
46#define MSDC_PS_DAT0 BIT(16)
47#define MSDC_PS_CDDBCE_M 0xf000
48#define MSDC_PS_CDDBCE_S 12
49#define MSDC_PS_CDSTS BIT(1)
50#define MSDC_PS_CDEN BIT(0)
51
52/* #define MSDC_INT(EN) */
53#define MSDC_INT_ACMDRDY BIT(3)
54#define MSDC_INT_ACMDTMO BIT(4)
55#define MSDC_INT_ACMDCRCERR BIT(5)
56#define MSDC_INT_CMDRDY BIT(8)
57#define MSDC_INT_CMDTMO BIT(9)
58#define MSDC_INT_RSPCRCERR BIT(10)
59#define MSDC_INT_XFER_COMPL BIT(12)
60#define MSDC_INT_DATTMO BIT(14)
61#define MSDC_INT_DATCRCERR BIT(15)
62
63/* MSDC_FIFOCS */
64#define MSDC_FIFOCS_CLR BIT(31)
65#define MSDC_FIFOCS_TXCNT_M 0xff0000
66#define MSDC_FIFOCS_TXCNT_S 16
67#define MSDC_FIFOCS_RXCNT_M 0xff
68#define MSDC_FIFOCS_RXCNT_S 0
69
70/* #define SDC_CFG */
71#define SDC_CFG_DTOC_M 0xff000000
72#define SDC_CFG_DTOC_S 24
73#define SDC_CFG_SDIOIDE BIT(20)
74#define SDC_CFG_SDIO BIT(19)
75#define SDC_CFG_BUSWIDTH_M 0x30000
76#define SDC_CFG_BUSWIDTH_S 16
77
78/* SDC_CMD */
79#define SDC_CMD_BLK_LEN_M 0xfff0000
80#define SDC_CMD_BLK_LEN_S 16
81#define SDC_CMD_STOP BIT(14)
82#define SDC_CMD_WR BIT(13)
83#define SDC_CMD_DTYPE_M 0x1800
84#define SDC_CMD_DTYPE_S 11
85#define SDC_CMD_RSPTYP_M 0x380
86#define SDC_CMD_RSPTYP_S 7
87#define SDC_CMD_CMD_M 0x3f
88#define SDC_CMD_CMD_S 0
89
90/* SDC_STS */
91#define SDC_STS_CMDBUSY BIT(1)
92#define SDC_STS_SDCBUSY BIT(0)
93
94/* SDC_ADV_CFG0 */
95#define SDC_RX_ENHANCE_EN BIT(20)
96
97/* PATCH_BIT0 */
98#define MSDC_INT_DAT_LATCH_CK_SEL_M 0x380
99#define MSDC_INT_DAT_LATCH_CK_SEL_S 7
100
101/* PATCH_BIT1 */
102#define MSDC_PB1_STOP_DLY_M 0xf00
103#define MSDC_PB1_STOP_DLY_S 8
104
105/* PATCH_BIT2 */
106#define MSDC_PB2_CRCSTSENSEL_M 0xe0000000
107#define MSDC_PB2_CRCSTSENSEL_S 29
108#define MSDC_PB2_CFGCRCSTS BIT(28)
109#define MSDC_PB2_RESPSTSENSEL_M 0x70000
110#define MSDC_PB2_RESPSTSENSEL_S 16
111#define MSDC_PB2_CFGRESP BIT(15)
112#define MSDC_PB2_RESPWAIT_M 0x0c
113#define MSDC_PB2_RESPWAIT_S 2
114
115/* PAD_TUNE */
116#define MSDC_PAD_TUNE_CMDRRDLY_M 0x7c00000
117#define MSDC_PAD_TUNE_CMDRRDLY_S 22
118#define MSDC_PAD_TUNE_CMD_SEL BIT(21)
119#define MSDC_PAD_TUNE_CMDRDLY_M 0x1f0000
120#define MSDC_PAD_TUNE_CMDRDLY_S 16
121#define MSDC_PAD_TUNE_RXDLYSEL BIT(15)
122#define MSDC_PAD_TUNE_RD_SEL BIT(13)
123#define MSDC_PAD_TUNE_DATRRDLY_M 0x1f00
124#define MSDC_PAD_TUNE_DATRRDLY_S 8
125#define MSDC_PAD_TUNE_DATWRDLY_M 0x1f
126#define MSDC_PAD_TUNE_DATWRDLY_S 0
127
128/* EMMC50_CFG0 */
129#define EMMC50_CFG_CFCSTS_SEL BIT(4)
130
131/* SDC_FIFO_CFG */
132#define SDC_FIFO_CFG_WRVALIDSEL BIT(24)
133#define SDC_FIFO_CFG_RDVALIDSEL BIT(25)
134
135/* SDC_CFG_BUSWIDTH */
136#define MSDC_BUS_1BITS 0x0
137#define MSDC_BUS_4BITS 0x1
138#define MSDC_BUS_8BITS 0x2
139
140#define MSDC_FIFO_SIZE 128
141
142#define PAD_DELAY_MAX 32
143
144#define DEFAULT_CD_DEBOUNCE 8
145
146#define CMD_INTS_MASK \
147 (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO)
148
149#define DATA_INTS_MASK \
150 (MSDC_INT_XFER_COMPL | MSDC_INT_DATTMO | MSDC_INT_DATCRCERR)
151
152/* Register offset */
153struct mtk_sd_regs {
154 u32 msdc_cfg;
155 u32 msdc_iocon;
156 u32 msdc_ps;
157 u32 msdc_int;
158 u32 msdc_inten;
159 u32 msdc_fifocs;
160 u32 msdc_txdata;
161 u32 msdc_rxdata;
162 u32 reserved0[4];
163 u32 sdc_cfg;
164 u32 sdc_cmd;
165 u32 sdc_arg;
166 u32 sdc_sts;
167 u32 sdc_resp[4];
168 u32 sdc_blk_num;
169 u32 sdc_vol_chg;
170 u32 sdc_csts;
171 u32 sdc_csts_en;
172 u32 sdc_datcrc_sts;
173 u32 sdc_adv_cfg0;
174 u32 reserved1[2];
175 u32 emmc_cfg0;
176 u32 emmc_cfg1;
177 u32 emmc_sts;
178 u32 emmc_iocon;
179 u32 sd_acmd_resp;
180 u32 sd_acmd19_trg;
181 u32 sd_acmd19_sts;
182 u32 dma_sa_high4bit;
183 u32 dma_sa;
184 u32 dma_ca;
185 u32 dma_ctrl;
186 u32 dma_cfg;
187 u32 sw_dbg_sel;
188 u32 sw_dbg_out;
189 u32 dma_length;
190 u32 reserved2;
191 u32 patch_bit0;
192 u32 patch_bit1;
193 u32 patch_bit2;
194 u32 reserved3;
195 u32 dat0_tune_crc;
196 u32 dat1_tune_crc;
197 u32 dat2_tune_crc;
198 u32 dat3_tune_crc;
199 u32 cmd_tune_crc;
200 u32 sdio_tune_wind;
201 u32 reserved4[5];
202 u32 pad_tune;
203 u32 pad_tune0;
204 u32 pad_tune1;
205 u32 dat_rd_dly[4];
206 u32 reserved5[2];
207 u32 hw_dbg_sel;
208 u32 main_ver;
209 u32 eco_ver;
210 u32 reserved6[27];
211 u32 pad_ds_tune;
212 u32 reserved7[31];
213 u32 emmc50_cfg0;
214 u32 reserved8[7];
215 u32 sdc_fifo_cfg;
216};
217
218struct msdc_compatible {
219 u8 clk_div_bits;
developer607faf72019-09-25 17:45:37 +0800220 u8 sclk_cycle_shift;
developerdc5a9aa2018-11-15 10:08:04 +0800221 bool pad_tune0;
222 bool async_fifo;
223 bool data_tune;
224 bool busy_check;
225 bool stop_clk_fix;
226 bool enhance_rx;
227};
228
229struct msdc_delay_phase {
230 u8 maxlen;
231 u8 start;
232 u8 final_phase;
233};
234
235struct msdc_plat {
236 struct mmc_config cfg;
237 struct mmc mmc;
238};
239
240struct msdc_tune_para {
241 u32 iocon;
242 u32 pad_tune;
243};
244
245struct msdc_host {
246 struct mtk_sd_regs *base;
247 struct mmc *mmc;
248
249 struct msdc_compatible *dev_comp;
250
251 struct clk src_clk; /* for SD/MMC bus clock */
Fabien Parent297fa1a2019-03-24 16:46:32 +0100252 struct clk src_clk_cg; /* optional, MSDC source clock control gate */
developerdc5a9aa2018-11-15 10:08:04 +0800253 struct clk h_clk; /* MSDC core clock */
254
255 u32 src_clk_freq; /* source clock */
256 u32 mclk; /* mmc framework required bus clock */
257 u32 sclk; /* actual calculated bus clock */
258
259 /* operation timeout clocks */
260 u32 timeout_ns;
261 u32 timeout_clks;
262
263 /* tuning options */
264 u32 hs400_ds_delay;
265 u32 hs200_cmd_int_delay;
266 u32 hs200_write_int_delay;
267 u32 latch_ck;
268 u32 r_smpl; /* sample edge */
269 bool hs400_mode;
270
271 /* whether to use gpio detection or built-in hw detection */
272 bool builtin_cd;
developer399e4af2019-09-25 17:45:38 +0800273 bool cd_active_high;
developerdc5a9aa2018-11-15 10:08:04 +0800274
275 /* card detection / write protection GPIOs */
Fabien Parent8ed608a2019-03-24 16:46:34 +0100276#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +0800277 struct gpio_desc gpio_wp;
278 struct gpio_desc gpio_cd;
279#endif
280
281 uint last_resp_type;
282 uint last_data_write;
283
284 enum bus_mode timing;
285
286 struct msdc_tune_para def_tune_para;
287 struct msdc_tune_para saved_tune_para;
288};
289
290static void msdc_reset_hw(struct msdc_host *host)
291{
292 u32 reg;
293
294 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_RST);
295
296 readl_poll_timeout(&host->base->msdc_cfg, reg,
297 !(reg & MSDC_CFG_RST), 1000000);
298}
299
300static void msdc_fifo_clr(struct msdc_host *host)
301{
302 u32 reg;
303
304 setbits_le32(&host->base->msdc_fifocs, MSDC_FIFOCS_CLR);
305
306 readl_poll_timeout(&host->base->msdc_fifocs, reg,
307 !(reg & MSDC_FIFOCS_CLR), 1000000);
308}
309
310static u32 msdc_fifo_rx_bytes(struct msdc_host *host)
311{
312 return (readl(&host->base->msdc_fifocs) &
313 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S;
314}
315
316static u32 msdc_fifo_tx_bytes(struct msdc_host *host)
317{
318 return (readl(&host->base->msdc_fifocs) &
319 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S;
320}
321
322static u32 msdc_cmd_find_resp(struct msdc_host *host, struct mmc_cmd *cmd)
323{
324 u32 resp;
325
326 switch (cmd->resp_type) {
327 /* Actually, R1, R5, R6, R7 are the same */
328 case MMC_RSP_R1:
329 resp = 0x1;
330 break;
331 case MMC_RSP_R1b:
332 resp = 0x7;
333 break;
334 case MMC_RSP_R2:
335 resp = 0x2;
336 break;
337 case MMC_RSP_R3:
338 resp = 0x3;
339 break;
340 case MMC_RSP_NONE:
341 default:
342 resp = 0x0;
343 break;
344 }
345
346 return resp;
347}
348
349static u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
350 struct mmc_cmd *cmd,
351 struct mmc_data *data)
352{
353 u32 opcode = cmd->cmdidx;
354 u32 resp_type = msdc_cmd_find_resp(host, cmd);
355 uint blocksize = 0;
356 u32 dtype = 0;
357 u32 rawcmd = 0;
358
359 switch (opcode) {
360 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
361 case MMC_CMD_READ_MULTIPLE_BLOCK:
362 dtype = 2;
363 break;
364 case MMC_CMD_WRITE_SINGLE_BLOCK:
365 case MMC_CMD_READ_SINGLE_BLOCK:
366 case SD_CMD_APP_SEND_SCR:
367 dtype = 1;
368 break;
369 case SD_CMD_SWITCH_FUNC: /* same as MMC_CMD_SWITCH */
370 case SD_CMD_SEND_IF_COND: /* same as MMC_CMD_SEND_EXT_CSD */
371 case SD_CMD_APP_SD_STATUS: /* same as MMC_CMD_SEND_STATUS */
372 if (data)
373 dtype = 1;
374 }
375
376 if (data) {
377 if (data->flags == MMC_DATA_WRITE)
378 rawcmd |= SDC_CMD_WR;
379
380 if (data->blocks > 1)
381 dtype = 2;
382
383 blocksize = data->blocksize;
384 }
385
386 rawcmd |= ((opcode << SDC_CMD_CMD_S) & SDC_CMD_CMD_M) |
387 ((resp_type << SDC_CMD_RSPTYP_S) & SDC_CMD_RSPTYP_M) |
388 ((blocksize << SDC_CMD_BLK_LEN_S) & SDC_CMD_BLK_LEN_M) |
389 ((dtype << SDC_CMD_DTYPE_S) & SDC_CMD_DTYPE_M);
390
391 if (opcode == MMC_CMD_STOP_TRANSMISSION)
392 rawcmd |= SDC_CMD_STOP;
393
394 return rawcmd;
395}
396
397static int msdc_cmd_done(struct msdc_host *host, int events,
398 struct mmc_cmd *cmd)
399{
400 u32 *rsp = cmd->response;
401 int ret = 0;
402
403 if (cmd->resp_type & MMC_RSP_PRESENT) {
404 if (cmd->resp_type & MMC_RSP_136) {
405 rsp[0] = readl(&host->base->sdc_resp[3]);
406 rsp[1] = readl(&host->base->sdc_resp[2]);
407 rsp[2] = readl(&host->base->sdc_resp[1]);
408 rsp[3] = readl(&host->base->sdc_resp[0]);
409 } else {
410 rsp[0] = readl(&host->base->sdc_resp[0]);
411 }
412 }
413
414 if (!(events & MSDC_INT_CMDRDY)) {
415 if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
416 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
417 /*
418 * should not clear fifo/interrupt as the tune data
419 * may have alreay come.
420 */
421 msdc_reset_hw(host);
422
423 if (events & MSDC_INT_CMDTMO)
424 ret = -ETIMEDOUT;
425 else
426 ret = -EIO;
427 }
428
429 return ret;
430}
431
432static bool msdc_cmd_is_ready(struct msdc_host *host)
433{
434 int ret;
435 u32 reg;
436
437 /* The max busy time we can endure is 20ms */
438 ret = readl_poll_timeout(&host->base->sdc_sts, reg,
439 !(reg & SDC_STS_CMDBUSY), 20000);
440
441 if (ret) {
442 pr_err("CMD bus busy detected\n");
443 msdc_reset_hw(host);
444 return false;
445 }
446
447 if (host->last_resp_type == MMC_RSP_R1b && host->last_data_write) {
448 ret = readl_poll_timeout(&host->base->msdc_ps, reg,
449 reg & MSDC_PS_DAT0, 1000000);
450
451 if (ret) {
452 pr_err("Card stuck in programming state!\n");
453 msdc_reset_hw(host);
454 return false;
455 }
456 }
457
458 return true;
459}
460
461static int msdc_start_command(struct msdc_host *host, struct mmc_cmd *cmd,
462 struct mmc_data *data)
463{
464 u32 rawcmd;
465 u32 status;
466 u32 blocks = 0;
467 int ret;
468
469 if (!msdc_cmd_is_ready(host))
470 return -EIO;
471
472 msdc_fifo_clr(host);
473
474 host->last_resp_type = cmd->resp_type;
475 host->last_data_write = 0;
476
477 rawcmd = msdc_cmd_prepare_raw_cmd(host, cmd, data);
478
479 if (data)
480 blocks = data->blocks;
481
482 writel(CMD_INTS_MASK, &host->base->msdc_int);
483 writel(blocks, &host->base->sdc_blk_num);
484 writel(cmd->cmdarg, &host->base->sdc_arg);
485 writel(rawcmd, &host->base->sdc_cmd);
486
487 ret = readl_poll_timeout(&host->base->msdc_int, status,
488 status & CMD_INTS_MASK, 1000000);
489
490 if (ret)
491 status = MSDC_INT_CMDTMO;
492
493 return msdc_cmd_done(host, status, cmd);
494}
495
496static void msdc_fifo_read(struct msdc_host *host, u8 *buf, u32 size)
497{
498 u32 *wbuf;
499
500 while ((size_t)buf % 4) {
501 *buf++ = readb(&host->base->msdc_rxdata);
502 size--;
503 }
504
505 wbuf = (u32 *)buf;
506 while (size >= 4) {
507 *wbuf++ = readl(&host->base->msdc_rxdata);
508 size -= 4;
509 }
510
511 buf = (u8 *)wbuf;
512 while (size) {
513 *buf++ = readb(&host->base->msdc_rxdata);
514 size--;
515 }
516}
517
518static void msdc_fifo_write(struct msdc_host *host, const u8 *buf, u32 size)
519{
520 const u32 *wbuf;
521
522 while ((size_t)buf % 4) {
523 writeb(*buf++, &host->base->msdc_txdata);
524 size--;
525 }
526
527 wbuf = (const u32 *)buf;
528 while (size >= 4) {
529 writel(*wbuf++, &host->base->msdc_txdata);
530 size -= 4;
531 }
532
533 buf = (const u8 *)wbuf;
534 while (size) {
535 writeb(*buf++, &host->base->msdc_txdata);
536 size--;
537 }
538}
539
540static int msdc_pio_read(struct msdc_host *host, u8 *ptr, u32 size)
541{
542 u32 status;
543 u32 chksz;
544 int ret = 0;
545
546 while (1) {
547 status = readl(&host->base->msdc_int);
548 writel(status, &host->base->msdc_int);
549 status &= DATA_INTS_MASK;
550
551 if (status & MSDC_INT_DATCRCERR) {
552 ret = -EIO;
553 break;
554 }
555
556 if (status & MSDC_INT_DATTMO) {
557 ret = -ETIMEDOUT;
558 break;
559 }
560
Fabien Parent79a60732019-01-17 18:06:00 +0100561 chksz = min(size, (u32)MSDC_FIFO_SIZE);
562
563 if (msdc_fifo_rx_bytes(host) >= chksz) {
564 msdc_fifo_read(host, ptr, chksz);
565 ptr += chksz;
566 size -= chksz;
567 }
568
developerdc5a9aa2018-11-15 10:08:04 +0800569 if (status & MSDC_INT_XFER_COMPL) {
570 if (size) {
571 pr_err("data not fully read\n");
572 ret = -EIO;
573 }
574
575 break;
576 }
Fabien Parent79a60732019-01-17 18:06:00 +0100577}
developerdc5a9aa2018-11-15 10:08:04 +0800578
579 return ret;
580}
581
582static int msdc_pio_write(struct msdc_host *host, const u8 *ptr, u32 size)
583{
584 u32 status;
585 u32 chksz;
586 int ret = 0;
587
588 while (1) {
589 status = readl(&host->base->msdc_int);
590 writel(status, &host->base->msdc_int);
591 status &= DATA_INTS_MASK;
592
593 if (status & MSDC_INT_DATCRCERR) {
594 ret = -EIO;
595 break;
596 }
597
598 if (status & MSDC_INT_DATTMO) {
599 ret = -ETIMEDOUT;
600 break;
601 }
602
603 if (status & MSDC_INT_XFER_COMPL) {
604 if (size) {
605 pr_err("data not fully written\n");
606 ret = -EIO;
607 }
608
609 break;
610 }
611
612 chksz = min(size, (u32)MSDC_FIFO_SIZE);
613
614 if (MSDC_FIFO_SIZE - msdc_fifo_tx_bytes(host) >= chksz) {
615 msdc_fifo_write(host, ptr, chksz);
616 ptr += chksz;
617 size -= chksz;
618 }
619 }
620
621 return ret;
622}
623
624static int msdc_start_data(struct msdc_host *host, struct mmc_data *data)
625{
626 u32 size;
627 int ret;
628
developer7462c842019-07-19 11:04:47 +0800629 WATCHDOG_RESET();
630
developerdc5a9aa2018-11-15 10:08:04 +0800631 if (data->flags == MMC_DATA_WRITE)
632 host->last_data_write = 1;
633
634 writel(DATA_INTS_MASK, &host->base->msdc_int);
635
636 size = data->blocks * data->blocksize;
637
638 if (data->flags == MMC_DATA_WRITE)
639 ret = msdc_pio_write(host, (const u8 *)data->src, size);
640 else
641 ret = msdc_pio_read(host, (u8 *)data->dest, size);
642
643 if (ret) {
644 msdc_reset_hw(host);
645 msdc_fifo_clr(host);
646 }
647
648 return ret;
649}
650
651static int msdc_ops_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
652 struct mmc_data *data)
653{
654 struct msdc_host *host = dev_get_priv(dev);
655 int ret;
656
657 ret = msdc_start_command(host, cmd, data);
658 if (ret)
659 return ret;
660
661 if (data)
662 return msdc_start_data(host, data);
663
664 return 0;
665}
666
667static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
668{
developer607faf72019-09-25 17:45:37 +0800669 u32 timeout, clk_ns, shift;
developerdc5a9aa2018-11-15 10:08:04 +0800670 u32 mode = 0;
671
672 host->timeout_ns = ns;
673 host->timeout_clks = clks;
674
675 if (host->sclk == 0) {
676 timeout = 0;
677 } else {
developer607faf72019-09-25 17:45:37 +0800678 shift = host->dev_comp->sclk_cycle_shift;
developerdc5a9aa2018-11-15 10:08:04 +0800679 clk_ns = 1000000000UL / host->sclk;
680 timeout = (ns + clk_ns - 1) / clk_ns + clks;
681 /* unit is 1048576 sclk cycles */
developer607faf72019-09-25 17:45:37 +0800682 timeout = (timeout + (0x1 << shift) - 1) >> shift;
developerdc5a9aa2018-11-15 10:08:04 +0800683 if (host->dev_comp->clk_div_bits == 8)
684 mode = (readl(&host->base->msdc_cfg) &
685 MSDC_CFG_CKMOD_M) >> MSDC_CFG_CKMOD_S;
686 else
687 mode = (readl(&host->base->msdc_cfg) &
688 MSDC_CFG_CKMOD_EXT_M) >> MSDC_CFG_CKMOD_EXT_S;
689 /* DDR mode will double the clk cycles for data timeout */
690 timeout = mode >= 2 ? timeout * 2 : timeout;
691 timeout = timeout > 1 ? timeout - 1 : 0;
692 timeout = timeout > 255 ? 255 : timeout;
693 }
694
695 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
696 timeout << SDC_CFG_DTOC_S);
697}
698
699static void msdc_set_buswidth(struct msdc_host *host, u32 width)
700{
701 u32 val = readl(&host->base->sdc_cfg);
702
703 val &= ~SDC_CFG_BUSWIDTH_M;
704
705 switch (width) {
706 default:
707 case 1:
708 val |= (MSDC_BUS_1BITS << SDC_CFG_BUSWIDTH_S);
709 break;
710 case 4:
711 val |= (MSDC_BUS_4BITS << SDC_CFG_BUSWIDTH_S);
712 break;
713 case 8:
714 val |= (MSDC_BUS_8BITS << SDC_CFG_BUSWIDTH_S);
715 break;
716 }
717
718 writel(val, &host->base->sdc_cfg);
719}
720
721static void msdc_set_mclk(struct msdc_host *host, enum bus_mode timing, u32 hz)
722{
723 u32 mode;
724 u32 div;
725 u32 sclk;
726 u32 reg;
727
728 if (!hz) {
729 host->mclk = 0;
730 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
731 return;
732 }
733
734 if (host->dev_comp->clk_div_bits == 8)
735 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_HS400_CK_MODE);
736 else
737 clrbits_le32(&host->base->msdc_cfg,
738 MSDC_CFG_HS400_CK_MODE_EXT);
739
740 if (timing == UHS_DDR50 || timing == MMC_DDR_52 ||
741 timing == MMC_HS_400) {
742 if (timing == MMC_HS_400)
743 mode = 0x3;
744 else
745 mode = 0x2; /* ddr mode and use divisor */
746
747 if (hz >= (host->src_clk_freq >> 2)) {
748 div = 0; /* mean div = 1/4 */
749 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
750 } else {
751 div = (host->src_clk_freq + ((hz << 2) - 1)) /
752 (hz << 2);
753 sclk = (host->src_clk_freq >> 2) / div;
754 div = (div >> 1);
755 }
756
757 if (timing == MMC_HS_400 && hz >= (host->src_clk_freq >> 1)) {
758 if (host->dev_comp->clk_div_bits == 8)
759 setbits_le32(&host->base->msdc_cfg,
760 MSDC_CFG_HS400_CK_MODE);
761 else
762 setbits_le32(&host->base->msdc_cfg,
763 MSDC_CFG_HS400_CK_MODE_EXT);
764
765 sclk = host->src_clk_freq >> 1;
766 div = 0; /* div is ignore when bit18 is set */
767 }
768 } else if (hz >= host->src_clk_freq) {
769 mode = 0x1; /* no divisor */
770 div = 0;
771 sclk = host->src_clk_freq;
772 } else {
773 mode = 0x0; /* use divisor */
774 if (hz >= (host->src_clk_freq >> 1)) {
775 div = 0; /* mean div = 1/2 */
776 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
777 } else {
778 div = (host->src_clk_freq + ((hz << 2) - 1)) /
779 (hz << 2);
780 sclk = (host->src_clk_freq >> 2) / div;
781 }
782 }
783
784 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
785
786 if (host->dev_comp->clk_div_bits == 8) {
787 div = min(div, (u32)(MSDC_CFG_CKDIV_M >> MSDC_CFG_CKDIV_S));
788 clrsetbits_le32(&host->base->msdc_cfg,
789 MSDC_CFG_CKMOD_M | MSDC_CFG_CKDIV_M,
790 (mode << MSDC_CFG_CKMOD_S) |
791 (div << MSDC_CFG_CKDIV_S));
792 } else {
793 div = min(div, (u32)(MSDC_CFG_CKDIV_EXT_M >>
794 MSDC_CFG_CKDIV_EXT_S));
795 clrsetbits_le32(&host->base->msdc_cfg,
796 MSDC_CFG_CKMOD_EXT_M | MSDC_CFG_CKDIV_EXT_M,
797 (mode << MSDC_CFG_CKMOD_EXT_S) |
798 (div << MSDC_CFG_CKDIV_EXT_S));
799 }
800
801 readl_poll_timeout(&host->base->msdc_cfg, reg,
802 reg & MSDC_CFG_CKSTB, 1000000);
803
804 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
805 host->sclk = sclk;
806 host->mclk = hz;
807 host->timing = timing;
808
809 /* needed because clk changed. */
810 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
811
812 /*
813 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
814 * tune result of hs200/200Mhz is not suitable for 50Mhz
815 */
816 if (host->sclk <= 52000000) {
817 writel(host->def_tune_para.iocon, &host->base->msdc_iocon);
818 writel(host->def_tune_para.pad_tune,
819 &host->base->pad_tune);
820 } else {
821 writel(host->saved_tune_para.iocon, &host->base->msdc_iocon);
822 writel(host->saved_tune_para.pad_tune,
823 &host->base->pad_tune);
824 }
825
826 dev_dbg(dev, "sclk: %d, timing: %d\n", host->sclk, timing);
827}
828
829static int msdc_ops_set_ios(struct udevice *dev)
830{
831 struct msdc_plat *plat = dev_get_platdata(dev);
832 struct msdc_host *host = dev_get_priv(dev);
833 struct mmc *mmc = &plat->mmc;
834 uint clock = mmc->clock;
835
836 msdc_set_buswidth(host, mmc->bus_width);
837
838 if (mmc->clk_disable)
839 clock = 0;
840 else if (clock < mmc->cfg->f_min)
841 clock = mmc->cfg->f_min;
842
843 if (host->mclk != clock || host->timing != mmc->selected_mode)
844 msdc_set_mclk(host, mmc->selected_mode, clock);
845
846 return 0;
847}
848
849static int msdc_ops_get_cd(struct udevice *dev)
850{
851 struct msdc_host *host = dev_get_priv(dev);
852 u32 val;
853
854 if (host->builtin_cd) {
855 val = readl(&host->base->msdc_ps);
developer399e4af2019-09-25 17:45:38 +0800856 val = !!(val & MSDC_PS_CDSTS);
857
858 return !val ^ host->cd_active_high;
developerdc5a9aa2018-11-15 10:08:04 +0800859 }
860
Fabien Parent8ed608a2019-03-24 16:46:34 +0100861#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +0800862 if (!host->gpio_cd.dev)
863 return 1;
864
865 return dm_gpio_get_value(&host->gpio_cd);
866#else
867 return 1;
868#endif
869}
870
871static int msdc_ops_get_wp(struct udevice *dev)
872{
Fabien Parent8ed608a2019-03-24 16:46:34 +0100873#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +0800874 struct msdc_host *host = dev_get_priv(dev);
875
developerdc5a9aa2018-11-15 10:08:04 +0800876 if (!host->gpio_wp.dev)
877 return 0;
878
879 return !dm_gpio_get_value(&host->gpio_wp);
880#else
881 return 0;
882#endif
883}
884
885#ifdef MMC_SUPPORTS_TUNING
886static u32 test_delay_bit(u32 delay, u32 bit)
887{
888 bit %= PAD_DELAY_MAX;
889 return delay & (1 << bit);
890}
891
892static int get_delay_len(u32 delay, u32 start_bit)
893{
894 int i;
895
896 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
897 if (test_delay_bit(delay, start_bit + i) == 0)
898 return i;
899 }
900
901 return PAD_DELAY_MAX - start_bit;
902}
903
904static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay)
905{
906 int start = 0, len = 0;
907 int start_final = 0, len_final = 0;
908 u8 final_phase = 0xff;
909 struct msdc_delay_phase delay_phase = { 0, };
910
911 if (delay == 0) {
912 dev_err(dev, "phase error: [map:%x]\n", delay);
913 delay_phase.final_phase = final_phase;
914 return delay_phase;
915 }
916
917 while (start < PAD_DELAY_MAX) {
918 len = get_delay_len(delay, start);
919 if (len_final < len) {
920 start_final = start;
921 len_final = len;
922 }
923
924 start += len ? len : 1;
925 if (len >= 12 && start_final < 4)
926 break;
927 }
928
929 /* The rule is to find the smallest delay cell */
930 if (start_final == 0)
931 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
932 else
933 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
934
935 dev_info(dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
936 delay, len_final, final_phase);
937
938 delay_phase.maxlen = len_final;
939 delay_phase.start = start_final;
940 delay_phase.final_phase = final_phase;
941 return delay_phase;
942}
943
944static int msdc_tune_response(struct udevice *dev, u32 opcode)
945{
946 struct msdc_plat *plat = dev_get_platdata(dev);
947 struct msdc_host *host = dev_get_priv(dev);
948 struct mmc *mmc = &plat->mmc;
949 u32 rise_delay = 0, fall_delay = 0;
950 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
951 struct msdc_delay_phase internal_delay_phase;
952 u8 final_delay, final_maxlen;
953 u32 internal_delay = 0;
954 void __iomem *tune_reg = &host->base->pad_tune;
955 int cmd_err;
956 int i, j;
957
958 if (host->dev_comp->pad_tune0)
959 tune_reg = &host->base->pad_tune0;
960
961 if (mmc->selected_mode == MMC_HS_200 ||
962 mmc->selected_mode == UHS_SDR104)
963 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
964 host->hs200_cmd_int_delay <<
965 MSDC_PAD_TUNE_CMDRRDLY_S);
966
967 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
968
969 for (i = 0; i < PAD_DELAY_MAX; i++) {
970 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
971 i << MSDC_PAD_TUNE_CMDRDLY_S);
972
973 for (j = 0; j < 3; j++) {
974 mmc_send_tuning(mmc, opcode, &cmd_err);
975 if (!cmd_err) {
976 rise_delay |= (1 << i);
977 } else {
978 rise_delay &= ~(1 << i);
979 break;
980 }
981 }
982 }
983
984 final_rise_delay = get_best_delay(host, rise_delay);
985 /* if rising edge has enough margin, do not scan falling edge */
986 if (final_rise_delay.maxlen >= 12 ||
987 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
988 goto skip_fall;
989
990 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
991 for (i = 0; i < PAD_DELAY_MAX; i++) {
992 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
993 i << MSDC_PAD_TUNE_CMDRDLY_S);
994
995 for (j = 0; j < 3; j++) {
996 mmc_send_tuning(mmc, opcode, &cmd_err);
997 if (!cmd_err) {
998 fall_delay |= (1 << i);
999 } else {
1000 fall_delay &= ~(1 << i);
1001 break;
1002 }
1003 }
1004 }
1005
1006 final_fall_delay = get_best_delay(host, fall_delay);
1007
1008skip_fall:
1009 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1010 if (final_maxlen == final_rise_delay.maxlen) {
1011 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1012 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1013 final_rise_delay.final_phase <<
1014 MSDC_PAD_TUNE_CMDRDLY_S);
1015 final_delay = final_rise_delay.final_phase;
1016 } else {
1017 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1018 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1019 final_fall_delay.final_phase <<
1020 MSDC_PAD_TUNE_CMDRDLY_S);
1021 final_delay = final_fall_delay.final_phase;
1022 }
1023
1024 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
1025 goto skip_internal;
1026
1027 for (i = 0; i < PAD_DELAY_MAX; i++) {
1028 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1029 i << MSDC_PAD_TUNE_CMDRRDLY_S);
1030
1031 mmc_send_tuning(mmc, opcode, &cmd_err);
1032 if (!cmd_err)
1033 internal_delay |= (1 << i);
1034 }
1035
1036 dev_err(dev, "Final internal delay: 0x%x\n", internal_delay);
1037
1038 internal_delay_phase = get_best_delay(host, internal_delay);
1039 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1040 internal_delay_phase.final_phase <<
1041 MSDC_PAD_TUNE_CMDRRDLY_S);
1042
1043skip_internal:
1044 dev_err(dev, "Final cmd pad delay: %x\n", final_delay);
1045 return final_delay == 0xff ? -EIO : 0;
1046}
1047
1048static int msdc_tune_data(struct udevice *dev, u32 opcode)
1049{
1050 struct msdc_plat *plat = dev_get_platdata(dev);
1051 struct msdc_host *host = dev_get_priv(dev);
1052 struct mmc *mmc = &plat->mmc;
1053 u32 rise_delay = 0, fall_delay = 0;
1054 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1055 u8 final_delay, final_maxlen;
1056 void __iomem *tune_reg = &host->base->pad_tune;
1057 int cmd_err;
1058 int i, ret;
1059
1060 if (host->dev_comp->pad_tune0)
1061 tune_reg = &host->base->pad_tune0;
1062
1063 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1064 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1065
1066 for (i = 0; i < PAD_DELAY_MAX; i++) {
1067 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1068 i << MSDC_PAD_TUNE_DATRRDLY_S);
1069
1070 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1071 if (!ret) {
1072 rise_delay |= (1 << i);
1073 } else if (cmd_err) {
1074 /* in this case, retune response is needed */
1075 ret = msdc_tune_response(dev, opcode);
1076 if (ret)
1077 break;
1078 }
1079 }
1080
1081 final_rise_delay = get_best_delay(host, rise_delay);
1082 if (final_rise_delay.maxlen >= 12 ||
1083 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1084 goto skip_fall;
1085
1086 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1087 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1088
1089 for (i = 0; i < PAD_DELAY_MAX; i++) {
1090 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1091 i << MSDC_PAD_TUNE_DATRRDLY_S);
1092
1093 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1094 if (!ret) {
1095 fall_delay |= (1 << i);
1096 } else if (cmd_err) {
1097 /* in this case, retune response is needed */
1098 ret = msdc_tune_response(dev, opcode);
1099 if (ret)
1100 break;
1101 }
1102 }
1103
1104 final_fall_delay = get_best_delay(host, fall_delay);
1105
1106skip_fall:
1107 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1108 if (final_maxlen == final_rise_delay.maxlen) {
1109 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1110 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1111 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1112 final_rise_delay.final_phase <<
1113 MSDC_PAD_TUNE_DATRRDLY_S);
1114 final_delay = final_rise_delay.final_phase;
1115 } else {
1116 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1117 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1118 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1119 final_fall_delay.final_phase <<
1120 MSDC_PAD_TUNE_DATRRDLY_S);
1121 final_delay = final_fall_delay.final_phase;
1122 }
1123
1124 if (mmc->selected_mode == MMC_HS_200 ||
1125 mmc->selected_mode == UHS_SDR104)
1126 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATWRDLY_M,
1127 host->hs200_write_int_delay <<
1128 MSDC_PAD_TUNE_DATWRDLY_S);
1129
1130 dev_err(dev, "Final data pad delay: %x\n", final_delay);
1131
1132 return final_delay == 0xff ? -EIO : 0;
1133}
1134
1135static int msdc_execute_tuning(struct udevice *dev, uint opcode)
1136{
1137 struct msdc_plat *plat = dev_get_platdata(dev);
1138 struct msdc_host *host = dev_get_priv(dev);
1139 struct mmc *mmc = &plat->mmc;
1140 int ret;
1141
1142 if (mmc->selected_mode == MMC_HS_400) {
1143 writel(host->hs400_ds_delay, &host->base->pad_ds_tune);
1144 /* for hs400 mode it must be set to 0 */
1145 clrbits_le32(&host->base->patch_bit2, MSDC_PB2_CFGCRCSTS);
1146 host->hs400_mode = true;
1147 }
1148
1149 ret = msdc_tune_response(dev, opcode);
1150 if (ret == -EIO) {
1151 dev_err(dev, "Tune response fail!\n");
1152 return ret;
1153 }
1154
1155 if (!host->hs400_mode) {
1156 ret = msdc_tune_data(dev, opcode);
1157 if (ret == -EIO)
1158 dev_err(dev, "Tune data fail!\n");
1159 }
1160
1161 host->saved_tune_para.iocon = readl(&host->base->msdc_iocon);
1162 host->saved_tune_para.pad_tune = readl(&host->base->pad_tune);
1163
1164 return ret;
1165}
1166#endif
1167
1168static void msdc_init_hw(struct msdc_host *host)
1169{
1170 u32 val;
1171 void __iomem *tune_reg = &host->base->pad_tune;
1172
1173 if (host->dev_comp->pad_tune0)
1174 tune_reg = &host->base->pad_tune0;
1175
1176 /* Configure to MMC/SD mode, clock free running */
1177 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_MODE);
1178
1179 /* Use PIO mode */
1180 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_PIO);
1181
1182 /* Reset */
1183 msdc_reset_hw(host);
1184
1185 /* Enable/disable hw card detection according to fdt option */
1186 if (host->builtin_cd)
1187 clrsetbits_le32(&host->base->msdc_ps,
1188 MSDC_PS_CDDBCE_M,
1189 (DEFAULT_CD_DEBOUNCE << MSDC_PS_CDDBCE_S) |
1190 MSDC_PS_CDEN);
1191 else
1192 clrbits_le32(&host->base->msdc_ps, MSDC_PS_CDEN);
1193
1194 /* Clear all interrupts */
1195 val = readl(&host->base->msdc_int);
1196 writel(val, &host->base->msdc_int);
1197
1198 /* Enable data & cmd interrupts */
1199 writel(DATA_INTS_MASK | CMD_INTS_MASK, &host->base->msdc_inten);
1200
1201 writel(0, tune_reg);
1202 writel(0, &host->base->msdc_iocon);
1203
1204 if (host->r_smpl)
1205 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1206 else
1207 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1208
1209 writel(0x403c0046, &host->base->patch_bit0);
1210 writel(0xffff4089, &host->base->patch_bit1);
1211
1212 if (host->dev_comp->stop_clk_fix)
1213 clrsetbits_le32(&host->base->patch_bit1, MSDC_PB1_STOP_DLY_M,
1214 3 << MSDC_PB1_STOP_DLY_S);
1215
1216 if (host->dev_comp->busy_check)
1217 clrbits_le32(&host->base->patch_bit1, (1 << 7));
1218
1219 setbits_le32(&host->base->emmc50_cfg0, EMMC50_CFG_CFCSTS_SEL);
1220
1221 if (host->dev_comp->async_fifo) {
1222 clrsetbits_le32(&host->base->patch_bit2, MSDC_PB2_RESPWAIT_M,
1223 3 << MSDC_PB2_RESPWAIT_S);
1224
1225 if (host->dev_comp->enhance_rx) {
1226 setbits_le32(&host->base->sdc_adv_cfg0,
1227 SDC_RX_ENHANCE_EN);
1228 } else {
1229 clrsetbits_le32(&host->base->patch_bit2,
1230 MSDC_PB2_RESPSTSENSEL_M,
1231 2 << MSDC_PB2_RESPSTSENSEL_S);
1232 clrsetbits_le32(&host->base->patch_bit2,
1233 MSDC_PB2_CRCSTSENSEL_M,
1234 2 << MSDC_PB2_CRCSTSENSEL_S);
1235 }
1236
1237 /* use async fifo to avoid tune internal delay */
1238 clrbits_le32(&host->base->patch_bit2,
1239 MSDC_PB2_CFGRESP);
1240 clrbits_le32(&host->base->patch_bit2,
1241 MSDC_PB2_CFGCRCSTS);
1242 }
1243
1244 if (host->dev_comp->data_tune) {
1245 setbits_le32(tune_reg,
1246 MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL);
1247 clrsetbits_le32(&host->base->patch_bit0,
1248 MSDC_INT_DAT_LATCH_CK_SEL_M,
1249 host->latch_ck <<
1250 MSDC_INT_DAT_LATCH_CK_SEL_S);
1251 } else {
1252 /* choose clock tune */
1253 setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
1254 }
1255
1256 /* Configure to enable SDIO mode otherwise sdio cmd5 won't work */
1257 setbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIO);
1258
1259 /* disable detecting SDIO device interrupt function */
1260 clrbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIOIDE);
1261
1262 /* Configure to default data timeout */
1263 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
1264 3 << SDC_CFG_DTOC_S);
1265
1266 if (host->dev_comp->stop_clk_fix) {
1267 clrbits_le32(&host->base->sdc_fifo_cfg,
1268 SDC_FIFO_CFG_WRVALIDSEL);
1269 clrbits_le32(&host->base->sdc_fifo_cfg,
1270 SDC_FIFO_CFG_RDVALIDSEL);
1271 }
1272
1273 host->def_tune_para.iocon = readl(&host->base->msdc_iocon);
1274 host->def_tune_para.pad_tune = readl(&host->base->pad_tune);
1275}
1276
1277static void msdc_ungate_clock(struct msdc_host *host)
1278{
1279 clk_enable(&host->src_clk);
1280 clk_enable(&host->h_clk);
Fabien Parent297fa1a2019-03-24 16:46:32 +01001281 if (host->src_clk_cg.dev)
1282 clk_enable(&host->src_clk_cg);
developerdc5a9aa2018-11-15 10:08:04 +08001283}
1284
1285static int msdc_drv_probe(struct udevice *dev)
1286{
1287 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1288 struct msdc_plat *plat = dev_get_platdata(dev);
1289 struct msdc_host *host = dev_get_priv(dev);
1290 struct mmc_config *cfg = &plat->cfg;
1291
1292 cfg->name = dev->name;
1293
1294 host->dev_comp = (struct msdc_compatible *)dev_get_driver_data(dev);
1295
1296 host->src_clk_freq = clk_get_rate(&host->src_clk);
1297
1298 if (host->dev_comp->clk_div_bits == 8)
1299 cfg->f_min = host->src_clk_freq / (4 * 255);
1300 else
1301 cfg->f_min = host->src_clk_freq / (4 * 4095);
1302 cfg->f_max = host->src_clk_freq / 2;
1303
1304 cfg->b_max = 1024;
1305 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1306
1307 host->mmc = &plat->mmc;
1308 host->timeout_ns = 100000000;
developer607faf72019-09-25 17:45:37 +08001309 host->timeout_clks = 3 * (1 << host->dev_comp->sclk_cycle_shift);
developerdc5a9aa2018-11-15 10:08:04 +08001310
1311#ifdef CONFIG_PINCTRL
1312 pinctrl_select_state(dev, "default");
1313#endif
1314
1315 msdc_ungate_clock(host);
1316 msdc_init_hw(host);
1317
1318 upriv->mmc = &plat->mmc;
1319
1320 return 0;
1321}
1322
1323static int msdc_ofdata_to_platdata(struct udevice *dev)
1324{
1325 struct msdc_plat *plat = dev_get_platdata(dev);
1326 struct msdc_host *host = dev_get_priv(dev);
1327 struct mmc_config *cfg = &plat->cfg;
1328 int ret;
1329
1330 host->base = (void *)dev_read_addr(dev);
1331 if (!host->base)
1332 return -EINVAL;
1333
1334 ret = mmc_of_parse(dev, cfg);
1335 if (ret)
1336 return ret;
1337
1338 ret = clk_get_by_name(dev, "source", &host->src_clk);
1339 if (ret < 0)
1340 return ret;
1341
1342 ret = clk_get_by_name(dev, "hclk", &host->h_clk);
1343 if (ret < 0)
1344 return ret;
1345
Fabien Parent297fa1a2019-03-24 16:46:32 +01001346 clk_get_by_name(dev, "source_cg", &host->src_clk_cg); /* optional */
1347
Fabien Parent8ed608a2019-03-24 16:46:34 +01001348#if CONFIG_IS_ENABLED(DM_GPIO)
developerdc5a9aa2018-11-15 10:08:04 +08001349 gpio_request_by_name(dev, "wp-gpios", 0, &host->gpio_wp, GPIOD_IS_IN);
1350 gpio_request_by_name(dev, "cd-gpios", 0, &host->gpio_cd, GPIOD_IS_IN);
1351#endif
1352
1353 host->hs400_ds_delay = dev_read_u32_default(dev, "hs400-ds-delay", 0);
1354 host->hs200_cmd_int_delay =
1355 dev_read_u32_default(dev, "cmd_int_delay", 0);
1356 host->hs200_write_int_delay =
1357 dev_read_u32_default(dev, "write_int_delay", 0);
1358 host->latch_ck = dev_read_u32_default(dev, "latch-ck", 0);
1359 host->r_smpl = dev_read_u32_default(dev, "r_smpl", 0);
1360 host->builtin_cd = dev_read_u32_default(dev, "builtin-cd", 0);
developer399e4af2019-09-25 17:45:38 +08001361 host->cd_active_high = dev_read_bool(dev, "cd-active-high");
developerdc5a9aa2018-11-15 10:08:04 +08001362
1363 return 0;
1364}
1365
1366static int msdc_drv_bind(struct udevice *dev)
1367{
1368 struct msdc_plat *plat = dev_get_platdata(dev);
1369
1370 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1371}
1372
1373static const struct dm_mmc_ops msdc_ops = {
1374 .send_cmd = msdc_ops_send_cmd,
1375 .set_ios = msdc_ops_set_ios,
1376 .get_cd = msdc_ops_get_cd,
1377 .get_wp = msdc_ops_get_wp,
1378#ifdef MMC_SUPPORTS_TUNING
1379 .execute_tuning = msdc_execute_tuning,
1380#endif
1381};
1382
developer607faf72019-09-25 17:45:37 +08001383static const struct msdc_compatible mt7620_compat = {
1384 .clk_div_bits = 8,
1385 .sclk_cycle_shift = 16,
1386 .pad_tune0 = false,
1387 .async_fifo = false,
1388 .data_tune = false,
1389 .busy_check = false,
1390 .stop_clk_fix = false,
1391 .enhance_rx = false
1392};
1393
developerdc5a9aa2018-11-15 10:08:04 +08001394static const struct msdc_compatible mt7623_compat = {
1395 .clk_div_bits = 12,
developer607faf72019-09-25 17:45:37 +08001396 .sclk_cycle_shift = 20,
developerdc5a9aa2018-11-15 10:08:04 +08001397 .pad_tune0 = true,
1398 .async_fifo = true,
1399 .data_tune = true,
1400 .busy_check = false,
1401 .stop_clk_fix = false,
1402 .enhance_rx = false
1403};
1404
Fabien Parent1d520a42019-03-24 16:46:33 +01001405static const struct msdc_compatible mt8516_compat = {
1406 .clk_div_bits = 12,
developer607faf72019-09-25 17:45:37 +08001407 .sclk_cycle_shift = 20,
Fabien Parent1d520a42019-03-24 16:46:33 +01001408 .pad_tune0 = true,
1409 .async_fifo = true,
1410 .data_tune = true,
1411 .busy_check = true,
1412 .stop_clk_fix = true,
1413};
1414
Fabien Parentc7da6982019-08-12 20:26:58 +02001415static const struct msdc_compatible mt8183_compat = {
1416 .clk_div_bits = 12,
developer607faf72019-09-25 17:45:37 +08001417 .sclk_cycle_shift = 20,
Fabien Parentc7da6982019-08-12 20:26:58 +02001418 .pad_tune0 = true,
1419 .async_fifo = true,
1420 .data_tune = true,
1421 .busy_check = true,
1422 .stop_clk_fix = true,
1423};
1424
developerdc5a9aa2018-11-15 10:08:04 +08001425static const struct udevice_id msdc_ids[] = {
developer607faf72019-09-25 17:45:37 +08001426 { .compatible = "mediatek,mt7620-mmc", .data = (ulong)&mt7620_compat },
developerdc5a9aa2018-11-15 10:08:04 +08001427 { .compatible = "mediatek,mt7623-mmc", .data = (ulong)&mt7623_compat },
Fabien Parent1d520a42019-03-24 16:46:33 +01001428 { .compatible = "mediatek,mt8516-mmc", .data = (ulong)&mt8516_compat },
Fabien Parentc7da6982019-08-12 20:26:58 +02001429 { .compatible = "mediatek,mt8183-mmc", .data = (ulong)&mt8183_compat },
developerdc5a9aa2018-11-15 10:08:04 +08001430 {}
1431};
1432
1433U_BOOT_DRIVER(mtk_sd_drv) = {
1434 .name = "mtk_sd",
1435 .id = UCLASS_MMC,
1436 .of_match = msdc_ids,
1437 .ofdata_to_platdata = msdc_ofdata_to_platdata,
1438 .bind = msdc_drv_bind,
1439 .probe = msdc_drv_probe,
1440 .ops = &msdc_ops,
1441 .platdata_auto_alloc_size = sizeof(struct msdc_plat),
1442 .priv_auto_alloc_size = sizeof(struct msdc_host),
1443};