1
2
3
4
5
6
7
8
9
10
11
12
13#include <config.h>
14#include <common.h>
15#include <command.h>
16#include <clk.h>
17#include <cpu_func.h>
18#include <errno.h>
19#include <hwconfig.h>
20#include <log.h>
21#include <mmc.h>
22#include <part.h>
23#include <asm/cache.h>
24#include <dm/device_compat.h>
25#include <linux/bitops.h>
26#include <linux/delay.h>
27#include <linux/err.h>
28#include <power/regulator.h>
29#include <malloc.h>
30#include <fsl_esdhc_imx.h>
31#include <fdt_support.h>
32#include <asm/io.h>
33#include <dm.h>
34#include <asm-generic/gpio.h>
35#include <dm/pinctrl.h>
36#include <dt-structs.h>
37#include <mapmem.h>
38#include <dm/ofnode.h>
39#include <linux/iopoll.h>
40
41#if !CONFIG_IS_ENABLED(BLK)
42#include "mmc_private.h"
43#endif
44
45DECLARE_GLOBAL_DATA_PTR;
46
47#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
48 IRQSTATEN_CINT | \
49 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
50 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
51 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
52 IRQSTATEN_DINT)
53#define MAX_TUNING_LOOP 40
54#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
55
56struct fsl_esdhc {
57 uint dsaddr;
58 uint blkattr;
59 uint cmdarg;
60 uint xfertyp;
61 uint cmdrsp0;
62 uint cmdrsp1;
63 uint cmdrsp2;
64 uint cmdrsp3;
65 uint datport;
66 uint prsstat;
67 uint proctl;
68 uint sysctl;
69 uint irqstat;
70 uint irqstaten;
71 uint irqsigen;
72 uint autoc12err;
73 uint hostcapblt;
74 uint wml;
75 uint mixctrl;
76 char reserved1[4];
77 uint fevt;
78 uint admaes;
79 uint adsaddr;
80 char reserved2[4];
81 uint dllctrl;
82 uint dllstat;
83 uint clktunectrlstatus;
84 char reserved3[4];
85 uint strobe_dllctrl;
86 uint strobe_dllstat;
87 char reserved4[72];
88 uint vendorspec;
89 uint mmcboot;
90 uint vendorspec2;
91 uint tuning_ctrl;
92 char reserved5[44];
93 uint hostver;
94 char reserved6[4];
95 uint dmaerraddr;
96 char reserved7[4];
97 uint dmaerrattr;
98 char reserved8[4];
99 uint hostcapblt2;
100 char reserved9[8];
101 uint tcr;
102 char reserved10[28];
103 uint sddirctl;
104 char reserved11[712];
105 uint scr;
106};
107
108struct fsl_esdhc_plat {
109#if CONFIG_IS_ENABLED(OF_PLATDATA)
110
111 struct dtd_fsl_esdhc dtplat;
112#endif
113
114 struct mmc_config cfg;
115 struct mmc mmc;
116};
117
118struct esdhc_soc_data {
119 u32 flags;
120};
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145struct fsl_esdhc_priv {
146 struct fsl_esdhc *esdhc_regs;
147 unsigned int sdhc_clk;
148 struct clk per_clk;
149 unsigned int clock;
150 unsigned int mode;
151 unsigned int bus_width;
152#if !CONFIG_IS_ENABLED(BLK)
153 struct mmc *mmc;
154#endif
155 struct udevice *dev;
156 int non_removable;
157 int broken_cd;
158 int wp_enable;
159 int vs18_enable;
160 u32 flags;
161 u32 caps;
162 u32 tuning_step;
163 u32 tuning_start_tap;
164 u32 strobe_dll_delay_target;
165 u32 signal_voltage;
166#if CONFIG_IS_ENABLED(DM_REGULATOR)
167 struct udevice *vqmmc_dev;
168 struct udevice *vmmc_dev;
169#endif
170#if CONFIG_IS_ENABLED(DM_GPIO)
171 struct gpio_desc cd_gpio;
172 struct gpio_desc wp_gpio;
173#endif
174};
175
176
177static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
178{
179 uint xfertyp = 0;
180
181 if (data) {
182 xfertyp |= XFERTYP_DPSEL;
183#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
184 xfertyp |= XFERTYP_DMAEN;
185#endif
186 if (data->blocks > 1) {
187 xfertyp |= XFERTYP_MSBSEL;
188 xfertyp |= XFERTYP_BCEN;
189#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
190 xfertyp |= XFERTYP_AC12EN;
191#endif
192 }
193
194 if (data->flags & MMC_DATA_READ)
195 xfertyp |= XFERTYP_DTDSEL;
196 }
197
198 if (cmd->resp_type & MMC_RSP_CRC)
199 xfertyp |= XFERTYP_CCCEN;
200 if (cmd->resp_type & MMC_RSP_OPCODE)
201 xfertyp |= XFERTYP_CICEN;
202 if (cmd->resp_type & MMC_RSP_136)
203 xfertyp |= XFERTYP_RSPTYP_136;
204 else if (cmd->resp_type & MMC_RSP_BUSY)
205 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
206 else if (cmd->resp_type & MMC_RSP_PRESENT)
207 xfertyp |= XFERTYP_RSPTYP_48;
208
209 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
210 xfertyp |= XFERTYP_CMDTYP_ABORT;
211
212 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
213}
214
215#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
216
217
218
219static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
220 struct mmc_data *data)
221{
222 struct fsl_esdhc *regs = priv->esdhc_regs;
223 uint blocks;
224 char *buffer;
225 uint databuf;
226 uint size;
227 uint irqstat;
228 ulong start;
229
230 if (data->flags & MMC_DATA_READ) {
231 blocks = data->blocks;
232 buffer = data->dest;
233 while (blocks) {
234 start = get_timer(0);
235 size = data->blocksize;
236 irqstat = esdhc_read32(®s->irqstat);
237 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) {
238 if (get_timer(start) > PIO_TIMEOUT) {
239 printf("\nData Read Failed in PIO Mode.");
240 return;
241 }
242 }
243 while (size && (!(irqstat & IRQSTAT_TC))) {
244 udelay(100);
245 irqstat = esdhc_read32(®s->irqstat);
246 databuf = in_le32(®s->datport);
247 *((uint *)buffer) = databuf;
248 buffer += 4;
249 size -= 4;
250 }
251 blocks--;
252 }
253 } else {
254 blocks = data->blocks;
255 buffer = (char *)data->src;
256 while (blocks) {
257 start = get_timer(0);
258 size = data->blocksize;
259 irqstat = esdhc_read32(®s->irqstat);
260 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) {
261 if (get_timer(start) > PIO_TIMEOUT) {
262 printf("\nData Write Failed in PIO Mode.");
263 return;
264 }
265 }
266 while (size && (!(irqstat & IRQSTAT_TC))) {
267 udelay(100);
268 databuf = *((uint *)buffer);
269 buffer += 4;
270 size -= 4;
271 irqstat = esdhc_read32(®s->irqstat);
272 out_le32(®s->datport, databuf);
273 }
274 blocks--;
275 }
276 }
277}
278#endif
279
280static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
281 struct mmc_data *data)
282{
283 int timeout;
284 struct fsl_esdhc *regs = priv->esdhc_regs;
285#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
286 dma_addr_t addr;
287#endif
288 uint wml_value;
289
290 wml_value = data->blocksize/4;
291
292 if (data->flags & MMC_DATA_READ) {
293 if (wml_value > WML_RD_WML_MAX)
294 wml_value = WML_RD_WML_MAX_VAL;
295
296 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value);
297#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
298#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
299 addr = virt_to_phys((void *)(data->dest));
300 if (upper_32_bits(addr))
301 printf("Error found for upper 32 bits\n");
302 else
303 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
304#else
305 esdhc_write32(®s->dsaddr, (u32)data->dest);
306#endif
307#endif
308 } else {
309#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
310 flush_dcache_range((ulong)data->src,
311 (ulong)data->src+data->blocks
312 *data->blocksize);
313#endif
314 if (wml_value > WML_WR_WML_MAX)
315 wml_value = WML_WR_WML_MAX_VAL;
316 if (priv->wp_enable) {
317 if ((esdhc_read32(®s->prsstat) &
318 PRSSTAT_WPSPL) == 0) {
319 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
320 return -ETIMEDOUT;
321 }
322 } else {
323#if CONFIG_IS_ENABLED(DM_GPIO)
324 if (dm_gpio_is_valid(&priv->wp_gpio) &&
325 dm_gpio_get_value(&priv->wp_gpio)) {
326 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
327 return -ETIMEDOUT;
328 }
329#endif
330 }
331
332 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK,
333 wml_value << 16);
334#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
335#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
336 addr = virt_to_phys((void *)(data->src));
337 if (upper_32_bits(addr))
338 printf("Error found for upper 32 bits\n");
339 else
340 esdhc_write32(®s->dsaddr, lower_32_bits(addr));
341#else
342 esdhc_write32(®s->dsaddr, (u32)data->src);
343#endif
344#endif
345 }
346
347 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 timeout = fls(mmc->clock/2);
372 timeout -= 13;
373
374 if (timeout > 14)
375 timeout = 14;
376
377 if (timeout < 0)
378 timeout = 0;
379
380#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
381 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
382 timeout++;
383#endif
384
385#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
386 timeout = 0xE;
387#endif
388 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
389
390 return 0;
391}
392
393static void check_and_invalidate_dcache_range
394 (struct mmc_cmd *cmd,
395 struct mmc_data *data) {
396 unsigned start = 0;
397 unsigned end = 0;
398 unsigned size = roundup(ARCH_DMA_MINALIGN,
399 data->blocks*data->blocksize);
400#if defined(CONFIG_S32V234) || defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
401 dma_addr_t addr;
402
403 addr = virt_to_phys((void *)(data->dest));
404 if (upper_32_bits(addr))
405 printf("Error found for upper 32 bits\n");
406 else
407 start = lower_32_bits(addr);
408#else
409 start = (unsigned)data->dest;
410#endif
411 end = start + size;
412 invalidate_dcache_range(start, end);
413}
414
415#ifdef CONFIG_MCF5441x
416
417
418
419static inline void sd_swap_dma_buff(struct mmc_data *data)
420{
421 int i, size = data->blocksize >> 2;
422 u32 *buffer = (u32 *)data->dest;
423 u32 sw;
424
425 while (data->blocks--) {
426 for (i = 0; i < size; i++) {
427 sw = __sw32(*buffer);
428 *buffer++ = sw;
429 }
430 }
431}
432#endif
433
434
435
436
437
438static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
439 struct mmc_cmd *cmd, struct mmc_data *data)
440{
441 int err = 0;
442 uint xfertyp;
443 uint irqstat;
444 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
445 struct fsl_esdhc *regs = priv->esdhc_regs;
446 unsigned long start;
447
448#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
449 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
450 return 0;
451#endif
452
453 esdhc_write32(®s->irqstat, -1);
454
455 sync();
456
457
458 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) ||
459 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB))
460 ;
461
462 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)
463 ;
464
465
466 if (data) {
467 err = esdhc_setup_data(priv, mmc, data);
468 if(err)
469 return err;
470
471 if (data->flags & MMC_DATA_READ)
472 check_and_invalidate_dcache_range(cmd, data);
473 }
474
475
476 xfertyp = esdhc_xfertyp(cmd, data);
477
478
479 esdhc_write32(®s->irqsigen, 0);
480
481
482 esdhc_write32(®s->cmdarg, cmd->cmdarg);
483#if defined(CONFIG_FSL_USDHC)
484 esdhc_write32(®s->mixctrl,
485 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
486 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
487 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000);
488#else
489 esdhc_write32(®s->xfertyp, xfertyp);
490#endif
491
492 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
493 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
494 flags = IRQSTAT_BRR;
495
496
497 start = get_timer(0);
498 while (!(esdhc_read32(®s->irqstat) & flags)) {
499 if (get_timer(start) > 1000) {
500 err = -ETIMEDOUT;
501 goto out;
502 }
503 }
504
505 irqstat = esdhc_read32(®s->irqstat);
506
507 if (irqstat & CMD_ERR) {
508 err = -ECOMM;
509 goto out;
510 }
511
512 if (irqstat & IRQSTAT_CTOE) {
513 err = -ETIMEDOUT;
514 goto out;
515 }
516
517
518 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
519 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
520
521 printf("Run CMD11 1.8V switch\n");
522
523 udelay(5000);
524 }
525
526
527 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
528 int timeout = 50000;
529
530
531 while (timeout > 0 && !(esdhc_read32(®s->prsstat) &
532 PRSSTAT_DAT0)) {
533 udelay(100);
534 timeout--;
535 }
536
537 if (timeout <= 0) {
538 printf("Timeout waiting for DAT0 to go high!\n");
539 err = -ETIMEDOUT;
540 goto out;
541 }
542 }
543
544
545 if (cmd->resp_type & MMC_RSP_136) {
546 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
547
548 cmdrsp3 = esdhc_read32(®s->cmdrsp3);
549 cmdrsp2 = esdhc_read32(®s->cmdrsp2);
550 cmdrsp1 = esdhc_read32(®s->cmdrsp1);
551 cmdrsp0 = esdhc_read32(®s->cmdrsp0);
552 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
553 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
554 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
555 cmd->response[3] = (cmdrsp0 << 8);
556 } else
557 cmd->response[0] = esdhc_read32(®s->cmdrsp0);
558
559
560 if (data) {
561#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
562 esdhc_pio_read_write(priv, data);
563#else
564 flags = DATA_COMPLETE;
565 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
566 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
567 flags = IRQSTAT_BRR;
568 }
569
570 do {
571 irqstat = esdhc_read32(®s->irqstat);
572
573 if (irqstat & IRQSTAT_DTOE) {
574 err = -ETIMEDOUT;
575 goto out;
576 }
577
578 if (irqstat & DATA_ERR) {
579 err = -ECOMM;
580 goto out;
581 }
582 } while ((irqstat & flags) != flags);
583
584
585
586
587
588
589 if (data->flags & MMC_DATA_READ) {
590 check_and_invalidate_dcache_range(cmd, data);
591#ifdef CONFIG_MCF5441x
592 sd_swap_dma_buff(data);
593#endif
594 }
595#endif
596 }
597
598out:
599
600 if (err) {
601 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) |
602 SYSCTL_RSTC);
603 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC)
604 ;
605
606 if (data) {
607 esdhc_write32(®s->sysctl,
608 esdhc_read32(®s->sysctl) |
609 SYSCTL_RSTD);
610 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD))
611 ;
612 }
613
614
615 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
616 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
617 }
618
619 esdhc_write32(®s->irqstat, -1);
620
621 return err;
622}
623
624static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
625{
626 struct fsl_esdhc *regs = priv->esdhc_regs;
627 int div = 1;
628 u32 tmp;
629 int ret;
630#ifdef ARCH_MXC
631#ifdef CONFIG_MX53
632
633 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
634#else
635 int pre_div = 1;
636#endif
637#else
638 int pre_div = 2;
639#endif
640 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
641 int sdhc_clk = priv->sdhc_clk;
642 uint clk;
643
644 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
645 pre_div *= 2;
646
647 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
648 div++;
649
650 pre_div >>= 1;
651 div -= 1;
652
653 clk = (pre_div << 8) | (div << 4);
654
655#ifdef CONFIG_FSL_USDHC
656 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN);
657#else
658 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
659#endif
660
661 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk);
662
663 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, tmp & PRSSTAT_SDSTB, 100);
664 if (ret)
665 pr_warn("fsl_esdhc_imx: Internal clock never stabilised.\n");
666
667#ifdef CONFIG_FSL_USDHC
668 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
669#else
670 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
671#endif
672
673 priv->clock = clock;
674}
675
676#ifdef MMC_SUPPORTS_TUNING
677static int esdhc_change_pinstate(struct udevice *dev)
678{
679 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
680 int ret;
681
682 switch (priv->mode) {
683 case UHS_SDR50:
684 case UHS_DDR50:
685 ret = pinctrl_select_state(dev, "state_100mhz");
686 break;
687 case UHS_SDR104:
688 case MMC_HS_200:
689 case MMC_HS_400:
690 case MMC_HS_400_ES:
691 ret = pinctrl_select_state(dev, "state_200mhz");
692 break;
693 default:
694 ret = pinctrl_select_state(dev, "default");
695 break;
696 }
697
698 if (ret)
699 printf("%s %d error\n", __func__, priv->mode);
700
701 return ret;
702}
703
704static void esdhc_reset_tuning(struct mmc *mmc)
705{
706 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
707 struct fsl_esdhc *regs = priv->esdhc_regs;
708
709 if (priv->flags & ESDHC_FLAG_USDHC) {
710 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
711 esdhc_clrbits32(®s->autoc12err,
712 MIX_CTRL_SMPCLK_SEL |
713 MIX_CTRL_EXE_TUNE);
714 }
715 }
716}
717
718static void esdhc_set_strobe_dll(struct mmc *mmc)
719{
720 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
721 struct fsl_esdhc *regs = priv->esdhc_regs;
722 u32 val;
723
724 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
725 esdhc_write32(®s->strobe_dllctrl, ESDHC_STROBE_DLL_CTRL_RESET);
726
727
728
729
730
731 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
732 (priv->strobe_dll_delay_target <<
733 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
734 esdhc_write32(®s->strobe_dllctrl, val);
735
736 mdelay(1);
737 val = esdhc_read32(®s->strobe_dllstat);
738 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
739 pr_warn("HS400 strobe DLL status REF not lock!\n");
740 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
741 pr_warn("HS400 strobe DLL status SLV not lock!\n");
742 }
743}
744
745static int esdhc_set_timing(struct mmc *mmc)
746{
747 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
748 struct fsl_esdhc *regs = priv->esdhc_regs;
749 u32 mixctrl;
750
751 mixctrl = esdhc_read32(®s->mixctrl);
752 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
753
754 switch (mmc->selected_mode) {
755 case MMC_LEGACY:
756 esdhc_reset_tuning(mmc);
757 esdhc_write32(®s->mixctrl, mixctrl);
758 break;
759 case MMC_HS_400:
760 case MMC_HS_400_ES:
761 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
762 esdhc_write32(®s->mixctrl, mixctrl);
763 break;
764 case MMC_HS:
765 case MMC_HS_52:
766 case MMC_HS_200:
767 case SD_HS:
768 case UHS_SDR12:
769 case UHS_SDR25:
770 case UHS_SDR50:
771 case UHS_SDR104:
772 esdhc_write32(®s->mixctrl, mixctrl);
773 break;
774 case UHS_DDR50:
775 case MMC_DDR_52:
776 mixctrl |= MIX_CTRL_DDREN;
777 esdhc_write32(®s->mixctrl, mixctrl);
778 break;
779 default:
780 printf("Not supported %d\n", mmc->selected_mode);
781 return -EINVAL;
782 }
783
784 priv->mode = mmc->selected_mode;
785
786 return esdhc_change_pinstate(mmc->dev);
787}
788
789static int esdhc_set_voltage(struct mmc *mmc)
790{
791 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
792 struct fsl_esdhc *regs = priv->esdhc_regs;
793 int ret;
794
795 priv->signal_voltage = mmc->signal_voltage;
796 switch (mmc->signal_voltage) {
797 case MMC_SIGNAL_VOLTAGE_330:
798 if (priv->vs18_enable)
799 return -ENOTSUPP;
800#if CONFIG_IS_ENABLED(DM_REGULATOR)
801 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
802 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
803 if (ret) {
804 printf("Setting to 3.3V error");
805 return -EIO;
806 }
807
808 mdelay(5);
809 }
810#endif
811
812 esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
813 if (!(esdhc_read32(®s->vendorspec) &
814 ESDHC_VENDORSPEC_VSELECT))
815 return 0;
816
817 return -EAGAIN;
818 case MMC_SIGNAL_VOLTAGE_180:
819#if CONFIG_IS_ENABLED(DM_REGULATOR)
820 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
821 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
822 if (ret) {
823 printf("Setting to 1.8V error");
824 return -EIO;
825 }
826 }
827#endif
828 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
829 if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
830 return 0;
831
832 return -EAGAIN;
833 case MMC_SIGNAL_VOLTAGE_120:
834 return -ENOTSUPP;
835 default:
836 return 0;
837 }
838}
839
840static void esdhc_stop_tuning(struct mmc *mmc)
841{
842 struct mmc_cmd cmd;
843
844 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
845 cmd.cmdarg = 0;
846 cmd.resp_type = MMC_RSP_R1b;
847
848 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
849}
850
851static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
852{
853 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
854 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
855 struct fsl_esdhc *regs = priv->esdhc_regs;
856 struct mmc *mmc = &plat->mmc;
857 u32 irqstaten = esdhc_read32(®s->irqstaten);
858 u32 irqsigen = esdhc_read32(®s->irqsigen);
859 int i, ret = -ETIMEDOUT;
860 u32 val, mixctrl;
861
862
863 if (mmc->clock <= 52000000)
864 return 0;
865
866
867 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
868 val = esdhc_read32(®s->autoc12err);
869 mixctrl = esdhc_read32(®s->mixctrl);
870 val &= ~MIX_CTRL_SMPCLK_SEL;
871 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
872
873 val |= MIX_CTRL_EXE_TUNE;
874 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
875
876 esdhc_write32(®s->autoc12err, val);
877 esdhc_write32(®s->mixctrl, mixctrl);
878 }
879
880
881 mixctrl = esdhc_read32(®s->mixctrl);
882 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
883 esdhc_write32(®s->mixctrl, mixctrl);
884
885 esdhc_write32(®s->irqstaten, IRQSTATEN_BRR);
886 esdhc_write32(®s->irqsigen, IRQSTATEN_BRR);
887
888
889
890
891
892 for (i = 0; i < MAX_TUNING_LOOP; i++) {
893 u32 ctrl;
894
895 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
896 if (mmc->bus_width == 8)
897 esdhc_write32(®s->blkattr, 0x7080);
898 else if (mmc->bus_width == 4)
899 esdhc_write32(®s->blkattr, 0x7040);
900 } else {
901 esdhc_write32(®s->blkattr, 0x7040);
902 }
903
904
905 val = esdhc_read32(®s->mixctrl);
906 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
907 esdhc_write32(®s->mixctrl, val);
908
909
910 mmc_send_tuning(mmc, opcode, NULL);
911
912 ctrl = esdhc_read32(®s->autoc12err);
913 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
914 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
915 ret = 0;
916 break;
917 }
918 }
919
920 esdhc_write32(®s->irqstaten, irqstaten);
921 esdhc_write32(®s->irqsigen, irqsigen);
922
923 esdhc_stop_tuning(mmc);
924
925 return ret;
926}
927#endif
928
929static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
930{
931 struct fsl_esdhc *regs = priv->esdhc_regs;
932 int ret __maybe_unused;
933 u32 clock;
934
935#ifdef MMC_SUPPORTS_TUNING
936
937
938
939
940
941
942
943 if (priv->mode != mmc->selected_mode) {
944 ret = esdhc_set_timing(mmc);
945 if (ret) {
946 printf("esdhc_set_timing error %d\n", ret);
947 return ret;
948 }
949 }
950#endif
951
952
953 clock = mmc->clock;
954 if (clock < mmc->cfg->f_min)
955 clock = mmc->cfg->f_min;
956
957 if (priv->clock != clock)
958 set_sysctl(priv, mmc, clock);
959
960#ifdef MMC_SUPPORTS_TUNING
961 if (mmc->clk_disable) {
962#ifdef CONFIG_FSL_USDHC
963 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN);
964#else
965 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN);
966#endif
967 } else {
968#ifdef CONFIG_FSL_USDHC
969 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN |
970 VENDORSPEC_CKEN);
971#else
972 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
973#endif
974 }
975
976
977
978
979
980
981 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES)
982 esdhc_set_strobe_dll(mmc);
983
984 if (priv->signal_voltage != mmc->signal_voltage) {
985 ret = esdhc_set_voltage(mmc);
986 if (ret) {
987 if (ret != -ENOTSUPP)
988 printf("esdhc_set_voltage error %d\n", ret);
989 return ret;
990 }
991 }
992#endif
993
994
995 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
996
997 if (mmc->bus_width == 4)
998 esdhc_setbits32(®s->proctl, PROCTL_DTW_4);
999 else if (mmc->bus_width == 8)
1000 esdhc_setbits32(®s->proctl, PROCTL_DTW_8);
1001
1002 return 0;
1003}
1004
1005static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
1006{
1007 struct fsl_esdhc *regs = priv->esdhc_regs;
1008 ulong start;
1009
1010
1011 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1012
1013
1014 start = get_timer(0);
1015 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1016 if (get_timer(start) > 1000)
1017 return -ETIMEDOUT;
1018 }
1019
1020#if defined(CONFIG_FSL_USDHC)
1021
1022 esdhc_write32(®s->mmcboot, 0x0);
1023
1024 esdhc_write32(®s->mixctrl, 0x0);
1025 esdhc_write32(®s->clktunectrlstatus, 0x0);
1026
1027
1028 if (priv->vs18_enable)
1029 esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT |
1030 ESDHC_VENDORSPEC_VSELECT));
1031 else
1032 esdhc_write32(®s->vendorspec, VENDORSPEC_INIT);
1033
1034
1035 esdhc_write32(®s->dllctrl, 0x0);
1036#endif
1037
1038#ifndef ARCH_MXC
1039
1040 esdhc_write32(®s->scr, 0x00000040);
1041#endif
1042
1043#ifndef CONFIG_FSL_USDHC
1044 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
1045#else
1046 esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
1047#endif
1048
1049
1050 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
1051
1052
1053 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
1054
1055#ifdef CONFIG_MCF5441x
1056 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1057#else
1058
1059 esdhc_write32(®s->proctl, PROCTL_INIT);
1060#endif
1061
1062
1063 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
1064
1065 return 0;
1066}
1067
1068static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
1069{
1070 struct fsl_esdhc *regs = priv->esdhc_regs;
1071 int timeout = 1000;
1072
1073#ifdef CONFIG_ESDHC_DETECT_QUIRK
1074 if (CONFIG_ESDHC_DETECT_QUIRK)
1075 return 1;
1076#endif
1077
1078#if CONFIG_IS_ENABLED(DM_MMC)
1079 if (priv->non_removable)
1080 return 1;
1081
1082 if (priv->broken_cd)
1083 return 1;
1084#if CONFIG_IS_ENABLED(DM_GPIO)
1085 if (dm_gpio_is_valid(&priv->cd_gpio))
1086 return dm_gpio_get_value(&priv->cd_gpio);
1087#endif
1088#endif
1089
1090 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout)
1091 udelay(1000);
1092
1093 return timeout > 0;
1094}
1095
1096static int esdhc_reset(struct fsl_esdhc *regs)
1097{
1098 ulong start;
1099
1100
1101 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA);
1102
1103
1104 start = get_timer(0);
1105 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) {
1106 if (get_timer(start) > 100) {
1107 printf("MMC/SD: Reset never completed.\n");
1108 return -ETIMEDOUT;
1109 }
1110 }
1111
1112 return 0;
1113}
1114
1115#if !CONFIG_IS_ENABLED(DM_MMC)
1116static int esdhc_getcd(struct mmc *mmc)
1117{
1118 struct fsl_esdhc_priv *priv = mmc->priv;
1119
1120 return esdhc_getcd_common(priv);
1121}
1122
1123static int esdhc_init(struct mmc *mmc)
1124{
1125 struct fsl_esdhc_priv *priv = mmc->priv;
1126
1127 return esdhc_init_common(priv, mmc);
1128}
1129
1130static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1131 struct mmc_data *data)
1132{
1133 struct fsl_esdhc_priv *priv = mmc->priv;
1134
1135 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1136}
1137
1138static int esdhc_set_ios(struct mmc *mmc)
1139{
1140 struct fsl_esdhc_priv *priv = mmc->priv;
1141
1142 return esdhc_set_ios_common(priv, mmc);
1143}
1144
1145static const struct mmc_ops esdhc_ops = {
1146 .getcd = esdhc_getcd,
1147 .init = esdhc_init,
1148 .send_cmd = esdhc_send_cmd,
1149 .set_ios = esdhc_set_ios,
1150};
1151#endif
1152
1153static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1154 struct fsl_esdhc_plat *plat)
1155{
1156 struct mmc_config *cfg;
1157 struct fsl_esdhc *regs;
1158 u32 caps, voltage_caps;
1159 int ret;
1160
1161 if (!priv)
1162 return -EINVAL;
1163
1164 regs = priv->esdhc_regs;
1165
1166
1167 ret = esdhc_reset(regs);
1168 if (ret)
1169 return ret;
1170
1171#ifdef CONFIG_MCF5441x
1172
1173 esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD);
1174#endif
1175
1176#ifndef CONFIG_FSL_USDHC
1177 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1178 | SYSCTL_IPGEN | SYSCTL_CKEN);
1179
1180 esdhc_write32(®s->mixctrl, 0);
1181 esdhc_write32(®s->autoc12err, 0);
1182 esdhc_write32(®s->clktunectrlstatus, 0);
1183#else
1184 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN |
1185 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
1186#endif
1187
1188 if (priv->vs18_enable)
1189 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1190
1191 esdhc_write32(®s->irqstaten, SDHCI_IRQ_EN_BITS);
1192 cfg = &plat->cfg;
1193#ifndef CONFIG_DM_MMC
1194 memset(cfg, '\0', sizeof(*cfg));
1195#endif
1196
1197 voltage_caps = 0;
1198 caps = esdhc_read32(®s->hostcapblt);
1199
1200#ifdef CONFIG_MCF5441x
1201
1202
1203
1204
1205
1206 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1207#endif
1208
1209#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1210 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1211 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1212#endif
1213
1214
1215#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1216 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1217#endif
1218
1219 if (caps & ESDHC_HOSTCAPBLT_VS18)
1220 voltage_caps |= MMC_VDD_165_195;
1221 if (caps & ESDHC_HOSTCAPBLT_VS30)
1222 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
1223 if (caps & ESDHC_HOSTCAPBLT_VS33)
1224 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1225
1226 cfg->name = "FSL_SDHC";
1227#if !CONFIG_IS_ENABLED(DM_MMC)
1228 cfg->ops = &esdhc_ops;
1229#endif
1230#ifdef CONFIG_SYS_SD_VOLTAGE
1231 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
1232#else
1233 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1234#endif
1235 if ((cfg->voltages & voltage_caps) == 0) {
1236 printf("voltage not supported by controller\n");
1237 return -1;
1238 }
1239
1240 if (priv->bus_width == 8)
1241 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1242 else if (priv->bus_width == 4)
1243 cfg->host_caps = MMC_MODE_4BIT;
1244
1245 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
1246#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
1247 cfg->host_caps |= MMC_MODE_DDR_52MHz;
1248#endif
1249
1250 if (priv->bus_width > 0) {
1251 if (priv->bus_width < 8)
1252 cfg->host_caps &= ~MMC_MODE_8BIT;
1253 if (priv->bus_width < 4)
1254 cfg->host_caps &= ~MMC_MODE_4BIT;
1255 }
1256
1257 if (caps & ESDHC_HOSTCAPBLT_HSS)
1258 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1259
1260#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1261 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
1262 cfg->host_caps &= ~MMC_MODE_8BIT;
1263#endif
1264
1265 cfg->host_caps |= priv->caps;
1266
1267 cfg->f_min = 400000;
1268 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
1269
1270 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1271
1272 esdhc_write32(®s->dllctrl, 0);
1273 if (priv->flags & ESDHC_FLAG_USDHC) {
1274 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1275 u32 val = esdhc_read32(®s->tuning_ctrl);
1276
1277 val |= ESDHC_STD_TUNING_EN;
1278 val &= ~ESDHC_TUNING_START_TAP_MASK;
1279 val |= priv->tuning_start_tap;
1280 val &= ~ESDHC_TUNING_STEP_MASK;
1281 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293 val |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1294 esdhc_write32(®s->tuning_ctrl, val);
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301#if !CONFIG_IS_ENABLED(DM_MMC)
1302static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1303 struct fsl_esdhc_priv *priv)
1304{
1305 if (!cfg || !priv)
1306 return -EINVAL;
1307
1308 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1309 priv->bus_width = cfg->max_bus_width;
1310 priv->sdhc_clk = cfg->sdhc_clk;
1311 priv->wp_enable = cfg->wp_enable;
1312 priv->vs18_enable = cfg->vs18_enable;
1313
1314 return 0;
1315};
1316
1317int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
1318{
1319 struct fsl_esdhc_plat *plat;
1320 struct fsl_esdhc_priv *priv;
1321 struct mmc *mmc;
1322 int ret;
1323
1324 if (!cfg)
1325 return -EINVAL;
1326
1327 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1328 if (!priv)
1329 return -ENOMEM;
1330 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1331 if (!plat) {
1332 free(priv);
1333 return -ENOMEM;
1334 }
1335
1336 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1337 if (ret) {
1338 debug("%s xlate failure\n", __func__);
1339 free(plat);
1340 free(priv);
1341 return ret;
1342 }
1343
1344 ret = fsl_esdhc_init(priv, plat);
1345 if (ret) {
1346 debug("%s init failure\n", __func__);
1347 free(plat);
1348 free(priv);
1349 return ret;
1350 }
1351
1352 mmc = mmc_create(&plat->cfg, priv);
1353 if (!mmc)
1354 return -EIO;
1355
1356 priv->mmc = mmc;
1357
1358 return 0;
1359}
1360
1361int fsl_esdhc_mmc_init(struct bd_info *bis)
1362{
1363 struct fsl_esdhc_cfg *cfg;
1364
1365 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
1366 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
1367 cfg->sdhc_clk = gd->arch.sdhc_clk;
1368 return fsl_esdhc_initialize(bis, cfg);
1369}
1370#endif
1371
1372#ifdef CONFIG_OF_LIBFDT
1373__weak int esdhc_status_fixup(void *blob, const char *compat)
1374{
1375#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1376 if (!hwconfig("esdhc")) {
1377 do_fixup_by_compat(blob, compat, "status", "disabled",
1378 sizeof("disabled"), 1);
1379 return 1;
1380 }
1381#endif
1382 return 0;
1383}
1384
1385void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
1386{
1387 const char *compat = "fsl,esdhc";
1388
1389 if (esdhc_status_fixup(blob, compat))
1390 return;
1391
1392 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
1393 gd->arch.sdhc_clk, 1);
1394}
1395#endif
1396
1397#if CONFIG_IS_ENABLED(DM_MMC)
1398#include <asm/arch/clock.h>
1399__weak void init_clk_usdhc(u32 index)
1400{
1401}
1402
1403static int fsl_esdhc_ofdata_to_platdata(struct udevice *dev)
1404{
1405#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1406 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1407#if CONFIG_IS_ENABLED(DM_REGULATOR)
1408 struct udevice *vqmmc_dev;
1409 int ret;
1410#endif
1411 const void *fdt = gd->fdt_blob;
1412 int node = dev_of_offset(dev);
1413
1414 fdt_addr_t addr;
1415 unsigned int val;
1416
1417 addr = dev_read_addr(dev);
1418 if (addr == FDT_ADDR_T_NONE)
1419 return -EINVAL;
1420 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1421 priv->dev = dev;
1422 priv->mode = -1;
1423
1424 val = dev_read_u32_default(dev, "bus-width", -1);
1425 if (val == 8)
1426 priv->bus_width = 8;
1427 else if (val == 4)
1428 priv->bus_width = 4;
1429 else
1430 priv->bus_width = 1;
1431
1432 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1433 priv->tuning_step = val;
1434 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1435 ESDHC_TUNING_START_TAP_DEFAULT);
1436 priv->tuning_start_tap = val;
1437 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1438 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1439 priv->strobe_dll_delay_target = val;
1440
1441 if (dev_read_bool(dev, "broken-cd"))
1442 priv->broken_cd = 1;
1443
1444 if (dev_read_bool(dev, "non-removable")) {
1445 priv->non_removable = 1;
1446 } else {
1447 priv->non_removable = 0;
1448#if CONFIG_IS_ENABLED(DM_GPIO)
1449 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1450 GPIOD_IS_IN);
1451#endif
1452 }
1453
1454 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1455 priv->wp_enable = 1;
1456 } else {
1457 priv->wp_enable = 0;
1458#if CONFIG_IS_ENABLED(DM_GPIO)
1459 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1460 GPIOD_IS_IN);
1461#endif
1462 }
1463
1464 priv->vs18_enable = 0;
1465
1466#if CONFIG_IS_ENABLED(DM_REGULATOR)
1467
1468
1469
1470
1471 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1472 if (ret) {
1473 dev_dbg(dev, "no vqmmc-supply\n");
1474 } else {
1475 priv->vqmmc_dev = vqmmc_dev;
1476 ret = regulator_set_enable(vqmmc_dev, true);
1477 if (ret) {
1478 dev_err(dev, "fail to enable vqmmc-supply\n");
1479 return ret;
1480 }
1481
1482 if (regulator_get_value(vqmmc_dev) == 1800000)
1483 priv->vs18_enable = 1;
1484 }
1485#endif
1486#endif
1487 return 0;
1488}
1489
1490static int fsl_esdhc_probe(struct udevice *dev)
1491{
1492 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1493 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1494 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1495 struct esdhc_soc_data *data =
1496 (struct esdhc_soc_data *)dev_get_driver_data(dev);
1497 struct mmc *mmc;
1498#if !CONFIG_IS_ENABLED(BLK)
1499 struct blk_desc *bdesc;
1500#endif
1501 int ret;
1502
1503#if CONFIG_IS_ENABLED(OF_PLATDATA)
1504 struct dtd_fsl_esdhc *dtplat = &plat->dtplat;
1505 unsigned int val;
1506
1507 priv->esdhc_regs = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
1508 val = plat->dtplat.bus_width;
1509 if (val == 8)
1510 priv->bus_width = 8;
1511 else if (val == 4)
1512 priv->bus_width = 4;
1513 else
1514 priv->bus_width = 1;
1515
1516 if (dtplat->non_removable)
1517 priv->non_removable = 1;
1518 else
1519 priv->non_removable = 0;
1520
1521 if (CONFIG_IS_ENABLED(DM_GPIO) && !priv->non_removable) {
1522 struct udevice *gpiodev;
1523
1524 ret = device_get_by_driver_info_idx(dtplat->cd_gpios->idx,
1525 &gpiodev);
1526 if (ret)
1527 return ret;
1528
1529 ret = gpio_dev_request_index(gpiodev, gpiodev->name, "cd-gpios",
1530 dtplat->cd_gpios->arg[0], GPIOD_IS_IN,
1531 dtplat->cd_gpios->arg[1], &priv->cd_gpio);
1532
1533 if (ret)
1534 return ret;
1535 }
1536#endif
1537
1538 if (data)
1539 priv->flags = data->flags;
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561 init_clk_usdhc(dev->seq);
1562
1563#if CONFIG_IS_ENABLED(CLK)
1564
1565 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1566 if (ret) {
1567 printf("Failed to get per_clk\n");
1568 return ret;
1569 }
1570 ret = clk_enable(&priv->per_clk);
1571 if (ret) {
1572 printf("Failed to enable per_clk\n");
1573 return ret;
1574 }
1575
1576 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1577#else
1578 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1579 if (priv->sdhc_clk <= 0) {
1580 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1581 return -EINVAL;
1582 }
1583#endif
1584
1585 ret = fsl_esdhc_init(priv, plat);
1586 if (ret) {
1587 dev_err(dev, "fsl_esdhc_init failure\n");
1588 return ret;
1589 }
1590
1591#if !CONFIG_IS_ENABLED(OF_PLATDATA)
1592 ret = mmc_of_parse(dev, &plat->cfg);
1593 if (ret)
1594 return ret;
1595#endif
1596
1597 mmc = &plat->mmc;
1598 mmc->cfg = &plat->cfg;
1599 mmc->dev = dev;
1600#if !CONFIG_IS_ENABLED(BLK)
1601 mmc->priv = priv;
1602
1603
1604 mmc->dsr_imp = 0;
1605 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1606
1607 bdesc = mmc_get_blk_desc(mmc);
1608 bdesc->if_type = IF_TYPE_MMC;
1609 bdesc->removable = 1;
1610 bdesc->devnum = mmc_get_next_devnum();
1611 bdesc->block_read = mmc_bread;
1612 bdesc->block_write = mmc_bwrite;
1613 bdesc->block_erase = mmc_berase;
1614
1615
1616 bdesc->part_type = mmc->cfg->part_type;
1617 mmc_list_add(mmc);
1618#endif
1619
1620 upriv->mmc = mmc;
1621
1622 return esdhc_init_common(priv, mmc);
1623}
1624
1625#if CONFIG_IS_ENABLED(DM_MMC)
1626static int fsl_esdhc_get_cd(struct udevice *dev)
1627{
1628 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1629
1630 return esdhc_getcd_common(priv);
1631}
1632
1633static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1634 struct mmc_data *data)
1635{
1636 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1637 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1638
1639 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1640}
1641
1642static int fsl_esdhc_set_ios(struct udevice *dev)
1643{
1644 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1645 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1646
1647 return esdhc_set_ios_common(priv, &plat->mmc);
1648}
1649
1650#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1651static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev)
1652{
1653 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1654 struct fsl_esdhc *regs = priv->esdhc_regs;
1655 u32 m;
1656
1657 m = esdhc_read32(®s->mixctrl);
1658 m |= MIX_CTRL_HS400_ES;
1659 esdhc_write32(®s->mixctrl, m);
1660
1661 return 0;
1662}
1663#endif
1664
1665static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
1666 int timeout_us)
1667{
1668 int ret;
1669 u32 tmp;
1670 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1671 struct fsl_esdhc *regs = priv->esdhc_regs;
1672
1673 ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp,
1674 !!(tmp & PRSSTAT_DAT0) == !!state,
1675 timeout_us);
1676 return ret;
1677}
1678
1679static const struct dm_mmc_ops fsl_esdhc_ops = {
1680 .get_cd = fsl_esdhc_get_cd,
1681 .send_cmd = fsl_esdhc_send_cmd,
1682 .set_ios = fsl_esdhc_set_ios,
1683#ifdef MMC_SUPPORTS_TUNING
1684 .execute_tuning = fsl_esdhc_execute_tuning,
1685#endif
1686#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
1687 .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe,
1688#endif
1689 .wait_dat0 = fsl_esdhc_wait_dat0,
1690};
1691#endif
1692
1693static struct esdhc_soc_data usdhc_imx7d_data = {
1694 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1695 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1696 | ESDHC_FLAG_HS400,
1697};
1698
1699static struct esdhc_soc_data usdhc_imx8qm_data = {
1700 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
1701 ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
1702 ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES,
1703};
1704
1705static const struct udevice_id fsl_esdhc_ids[] = {
1706 { .compatible = "fsl,imx53-esdhc", },
1707 { .compatible = "fsl,imx6ul-usdhc", },
1708 { .compatible = "fsl,imx6sx-usdhc", },
1709 { .compatible = "fsl,imx6sl-usdhc", },
1710 { .compatible = "fsl,imx6q-usdhc", },
1711 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
1712 { .compatible = "fsl,imx7ulp-usdhc", },
1713 { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1714 { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1715 { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1716 { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,},
1717 { .compatible = "fsl,imxrt-usdhc", },
1718 { .compatible = "fsl,esdhc", },
1719 { }
1720};
1721
1722#if CONFIG_IS_ENABLED(BLK)
1723static int fsl_esdhc_bind(struct udevice *dev)
1724{
1725 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1726
1727 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1728}
1729#endif
1730
1731U_BOOT_DRIVER(fsl_esdhc) = {
1732 .name = "fsl_esdhc",
1733 .id = UCLASS_MMC,
1734 .of_match = fsl_esdhc_ids,
1735 .ofdata_to_platdata = fsl_esdhc_ofdata_to_platdata,
1736 .ops = &fsl_esdhc_ops,
1737#if CONFIG_IS_ENABLED(BLK)
1738 .bind = fsl_esdhc_bind,
1739#endif
1740 .probe = fsl_esdhc_probe,
1741 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
1742 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1743};
1744
1745U_BOOT_DRIVER_ALIAS(fsl_esdhc, fsl_imx6q_usdhc)
1746#endif
1747