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