]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/fsl_esdhc.c
55482dd33654445b41fc2a39bab444f6a3791bd9
[karo-tx-uboot.git] / drivers / mmc / fsl_esdhc.c
1 /*
2  * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
3  * Andy Fleming
4  *
5  * Based vaguely on the pxa mmc code:
6  * (C) Copyright 2003
7  * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <config.h>
13 #include <common.h>
14 #include <command.h>
15 #include <hwconfig.h>
16 #include <mmc.h>
17 #include <part.h>
18 #include <malloc.h>
19 #include <mmc.h>
20 #include <fsl_esdhc.h>
21 #include <fdt_support.h>
22 #include <asm/io.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 struct fsl_esdhc {
27         uint    dsaddr;
28         uint    blkattr;
29         uint    cmdarg;
30         uint    xfertyp;
31         uint    cmdrsp0;
32         uint    cmdrsp1;
33         uint    cmdrsp2;
34         uint    cmdrsp3;
35         uint    datport;
36         uint    prsstat;
37         uint    proctl;
38         uint    sysctl;
39         uint    irqstat;
40         uint    irqstaten;
41         uint    irqsigen;
42         uint    autoc12err;
43         uint    hostcapblt;
44         uint    wml;
45         uint    mixctrl;
46         char    reserved1[4];
47         uint    fevt;
48         char    reserved2[168];
49         uint    hostver;
50         char    reserved3[780];
51         uint    scr;
52 };
53
54 /* Return the XFERTYP flags for a given command and data packet */
55 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
56 {
57         uint xfertyp = 0;
58
59         if (data) {
60                 xfertyp |= XFERTYP_DPSEL;
61 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
62                 xfertyp |= XFERTYP_DMAEN;
63 #endif
64                 if (data->blocks > 1) {
65                         xfertyp |= XFERTYP_MSBSEL;
66                         xfertyp |= XFERTYP_BCEN;
67 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
68                         xfertyp |= XFERTYP_AC12EN;
69 #endif
70                 }
71
72                 if (data->flags & MMC_DATA_READ)
73                         xfertyp |= XFERTYP_DTDSEL;
74         }
75
76         if (cmd->resp_type & MMC_RSP_CRC)
77                 xfertyp |= XFERTYP_CCCEN;
78         if (cmd->resp_type & MMC_RSP_OPCODE)
79                 xfertyp |= XFERTYP_CICEN;
80         if (cmd->resp_type & MMC_RSP_136)
81                 xfertyp |= XFERTYP_RSPTYP_136;
82         else if (cmd->resp_type & MMC_RSP_BUSY)
83                 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
84         else if (cmd->resp_type & MMC_RSP_PRESENT)
85                 xfertyp |= XFERTYP_RSPTYP_48;
86
87 #if defined(CONFIG_MX53) || defined(CONFIG_T4240QDS)
88         if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
89                 xfertyp |= XFERTYP_CMDTYP_ABORT;
90 #endif
91         return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
92 }
93
94 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
95 /*
96  * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
97  */
98 static void
99 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
100 {
101         struct fsl_esdhc_cfg *cfg = mmc->priv;
102         struct fsl_esdhc *regs = cfg->esdhc_base;
103         uint blocks;
104         char *buffer;
105         uint databuf;
106         uint size;
107         uint timeout;
108         int wml = esdhc_read32(&regs->wml);
109
110         if (data->flags & MMC_DATA_READ) {
111                 wml &= WML_RD_WML_MASK;
112                 blocks = data->blocks;
113                 buffer = data->dest;
114                 while (blocks) {
115                         timeout = PIO_TIMEOUT;
116                         size = data->blocksize;
117                         while (size &&
118                                 !(esdhc_read32(&regs->irqstat) & IRQSTAT_TC)) {
119                                 int i;
120                                 u32 prsstat;
121
122                                 while (!((prsstat = esdhc_read32(&regs->prsstat)) &
123                                                 PRSSTAT_BREN) && --timeout)
124                                         /* NOP */;
125                                 if (!(prsstat & PRSSTAT_BREN)) {
126                                         printf("%s: Data Read Failed in PIO Mode\n",
127                                                 __func__);
128                                         return;
129                                 }
130                                 for (i = 0; i < wml && size; i++) {
131                                         databuf = in_le32(&regs->datport);
132                                         memcpy(buffer, &databuf, sizeof(databuf));
133                                         buffer += 4;
134                                         size -= 4;
135                                 }
136                         }
137                         blocks--;
138                 }
139         } else {
140                 wml = (wml & WML_WR_WML_MASK) >> 16;
141                 blocks = data->blocks;
142                 buffer = (char *)data->src; /* cast away 'const' */
143                 while (blocks) {
144                         timeout = PIO_TIMEOUT;
145                         size = data->blocksize;
146                         while (size &&
147                                 !(esdhc_read32(&regs->irqstat) & IRQSTAT_TC)) {
148                                 int i;
149                                 u32 prsstat;
150
151                                 while (!((prsstat = esdhc_read32(&regs->prsstat)) &
152                                                 PRSSTAT_BWEN) && --timeout)
153                                         /* NOP */;
154                                 if (!(prsstat & PRSSTAT_BWEN)) {
155                                         printf("%s: Data Write Failed in PIO Mode\n",
156                                                 __func__);
157                                         return;
158                                 }
159                                 for (i = 0; i < wml && size; i++) {
160                                         memcpy(&databuf, buffer, sizeof(databuf));
161                                         out_le32(&regs->datport, databuf);
162                                         buffer += 4;
163                                         size -= 4;
164                                 }
165                         }
166                         blocks--;
167                 }
168         }
169 }
170 #endif
171
172 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
173 {
174         int timeout;
175         struct fsl_esdhc_cfg *cfg = mmc->priv;
176         struct fsl_esdhc *regs = cfg->esdhc_base;
177 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
178         uint wml_value;
179
180         wml_value = data->blocksize / 4;
181
182         if (data->flags & MMC_DATA_READ) {
183                 if (wml_value > WML_RD_WML_MAX)
184                         wml_value = WML_RD_WML_MAX_VAL;
185
186                 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
187                 esdhc_write32(&regs->dsaddr, (u32)data->dest);
188         } else {
189                 if (wml_value > WML_WR_WML_MAX)
190                         wml_value = WML_WR_WML_MAX_VAL;
191                 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
192                         printf("The SD card is locked. Can not write to a locked card.\n");
193                         return UNUSABLE_ERR;
194                 }
195
196                 flush_dcache_range((unsigned long)data->src,
197                                 (unsigned long)data->src + data->blocks * data->blocksize);
198                 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
199                                         wml_value << 16);
200                 esdhc_write32(&regs->dsaddr, (u32)data->src);
201         }
202 #else   /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
203         if (!(data->flags & MMC_DATA_READ)) {
204                 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
205                         printf("The SD card is locked. Can not write to a locked card.\n");
206                         return UNUSABLE_ERR;
207                 }
208                 esdhc_write32(&regs->dsaddr, (u32)data->src);
209         } else {
210                 esdhc_write32(&regs->dsaddr, (u32)data->dest);
211         }
212 #endif  /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
213
214         esdhc_write32(&regs->blkattr, (data->blocks << 16) | data->blocksize);
215
216         /* Calculate the timeout period for data transactions */
217         /*
218          * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
219          * 2)Timeout period should be minimum 0.250sec as per SD Card spec
220          *  So, Number of SD Clock cycles for 0.25sec should be minimum
221          *              (SD Clock/sec * 0.25 sec) SD Clock cycles
222          *              = (mmc->tran_speed * 1/4) SD Clock cycles
223          * As 1) >=  2)
224          * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
225          * Taking log2 both the sides
226          * => timeout + 13 >= log2(mmc->tran_speed/4)
227          * Rounding up to next power of 2
228          * => timeout + 13 = log2(mmc->tran_speed/4) + 1
229          * => timeout + 13 = fls(mmc->tran_speed/4)
230          */
231         timeout = fls(mmc->tran_speed / 4);
232         timeout -= 13;
233
234         if (timeout > 14)
235                 timeout = 14;
236         else if (timeout < 0)
237                 timeout = 0;
238
239 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
240         if ((timeout == 4) || (timeout == 8) || (timeout == 12))
241                 timeout++;
242 #endif
243         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
244
245         return 0;
246 }
247
248 static inline void check_and_invalidate_dcache_range(struct mmc_cmd *cmd,
249                                         struct mmc_data *data)
250 {
251         unsigned long start = (unsigned long)data->dest;
252         size_t start_ofs = start & (ARCH_DMA_MINALIGN - 1);
253         unsigned long size = data->blocks * data->blocksize;
254         unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
255
256         start -= start_ofs;
257         invalidate_dcache_range(start, end);
258 }
259
260 /*
261  * Sends a command out on the bus.  Takes the mmc pointer,
262  * a command pointer, and an optional data pointer.
263  */
264 static int
265 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
266 {
267         uint    xfertyp;
268         uint    irqstat;
269         struct fsl_esdhc_cfg *cfg = mmc->priv;
270         volatile struct fsl_esdhc *regs = cfg->esdhc_base;
271         unsigned long start;
272
273 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
274         if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
275                 return 0;
276 #endif
277
278         esdhc_write32(&regs->irqstat, -1);
279
280         sync();
281
282         start = get_timer_masked();
283         /* Wait for the bus to be idle */
284         while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
285                 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB)) {
286                 if (get_timer(start) > CONFIG_SYS_HZ) {
287                         printf("%s: Timeout waiting for bus idle\n", __func__);
288                         return TIMEOUT;
289                 }
290         }
291
292         start = get_timer_masked();
293         while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA) {
294                 if (get_timer(start) > CONFIG_SYS_HZ)
295                         return TIMEOUT;
296         }
297
298         /* Wait at least 8 SD clock cycles before the next command */
299         /*
300          * Note: This is way more than 8 cycles, but 1ms seems to
301          * resolve timing issues with some cards
302          */
303         udelay(1000);
304
305         /* Set up for a data transfer if we have one */
306         if (data) {
307                 int err;
308
309                 err = esdhc_setup_data(mmc, data);
310                 if (err)
311                         return err;
312         }
313
314         /* Figure out the transfer arguments */
315         xfertyp = esdhc_xfertyp(cmd, data);
316
317         /* Mask all irqs */
318         esdhc_write32(&regs->irqsigen, 0);
319
320         /* Send the command */
321         esdhc_write32(&regs->cmdarg, cmd->cmdarg);
322 #if defined(CONFIG_FSL_USDHC)
323         esdhc_write32(&regs->mixctrl,
324         (esdhc_read32(&regs->mixctrl) & ~0x7f) | (xfertyp & 0x7F));
325         esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
326 #else
327         esdhc_write32(&regs->xfertyp, xfertyp);
328 #endif
329
330         /* Mask all irqs */
331         esdhc_write32(&regs->irqsigen, 0);
332
333         start = get_timer_masked();
334         /* Wait for the command to complete */
335         while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) {
336                 if (get_timer(start) > CONFIG_SYS_HZ) {
337                         printf("%s: Timeout waiting for cmd completion\n", __func__);
338                         return TIMEOUT;
339                 }
340         }
341
342         if (data && (data->flags & MMC_DATA_READ))
343                 check_and_invalidate_dcache_range(cmd, data);
344
345         irqstat = esdhc_read32(&regs->irqstat);
346
347         /* Reset CMD and DATA portions on error */
348         if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
349                 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
350                               SYSCTL_RSTC);
351                 start = get_timer_masked();
352                 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC) {
353                         if (get_timer(start) > CONFIG_SYS_HZ)
354                                 return TIMEOUT;
355                 }
356
357                 if (data) {
358                         esdhc_write32(&regs->sysctl,
359                                       esdhc_read32(&regs->sysctl) |
360                                       SYSCTL_RSTD);
361                         start = get_timer_masked();
362                         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD)) {
363                                 if (get_timer(start) > CONFIG_SYS_HZ)
364                                         return TIMEOUT;
365                         }
366                 }
367         }
368
369         if (irqstat & CMD_ERR)
370                 return COMM_ERR;
371
372         if (irqstat & IRQSTAT_CTOE)
373                 return TIMEOUT;
374
375         /* Workaround for ESDHC errata ENGcm03648 */
376         if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
377                 int timeout = 2500;
378
379                 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
380                 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
381                                         PRSSTAT_DAT0)) {
382                         udelay(100);
383                         timeout--;
384                 }
385
386                 if (timeout <= 0) {
387                         printf("Timeout waiting for DAT0 to go high!\n");
388                         return TIMEOUT;
389                 }
390         }
391
392         /* Copy the response to the response buffer */
393         if (cmd->resp_type & MMC_RSP_136) {
394                 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
395
396                 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
397                 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
398                 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
399                 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
400                 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
401                 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
402                 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
403                 cmd->response[3] = (cmdrsp0 << 8);
404         } else
405                 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
406
407         /* Wait until all of the blocks are transferred */
408         if (data) {
409 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
410                 esdhc_pio_read_write(mmc, data);
411 #else
412                 unsigned long start = get_timer_masked();
413                 unsigned long data_timeout = data->blocks *
414                         (data->blocksize + 100) * 8 / mmc->bus_width /
415                         (mmc->tran_speed / CONFIG_SYS_HZ) + CONFIG_SYS_HZ;
416
417                 do {
418                         irqstat = esdhc_read32(&regs->irqstat);
419
420                         if (irqstat & IRQSTAT_DTOE) {
421                                 printf("MMC/SD data %s timeout\n",
422                                         data->flags & MMC_DATA_READ ?
423                                         "read" : "write");
424                                 return TIMEOUT;
425                         }
426
427                         if (irqstat & DATA_ERR) {
428                                 printf("MMC/SD data error\n");
429                                 return COMM_ERR;
430                         }
431
432                         if (get_timer(start) > data_timeout) {
433                                 printf("MMC/SD timeout waiting for %s xfer completion\n",
434                                                 data->flags & MMC_DATA_READ ?
435                                                 "read" : "write");
436                                 return TIMEOUT;
437                         }
438                 } while (!(irqstat & IRQSTAT_TC) &&
439                         (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
440
441                 check_and_invalidate_dcache_range(cmd, data);
442 #endif
443         }
444
445         esdhc_write32(&regs->irqstat, irqstat);
446
447         return 0;
448 }
449
450 static void set_sysctl(struct mmc *mmc, uint clock)
451 {
452         int div, pre_div;
453         struct fsl_esdhc_cfg *cfg = mmc->priv;
454         volatile struct fsl_esdhc *regs = cfg->esdhc_base;
455         int sdhc_clk = cfg->sdhc_clk;
456         uint clk;
457
458         if (clock < mmc->f_min)
459                 clock = mmc->f_min;
460
461         if (sdhc_clk / 16 > clock) {
462                 for (pre_div = 2; pre_div < 256; pre_div *= 2)
463                         if ((sdhc_clk / pre_div) <= (clock * 16))
464                                 break;
465         } else
466                 pre_div = 2;
467
468         for (div = 1; div <= 16; div++)
469                 if ((sdhc_clk / (div * pre_div)) <= clock)
470                         break;
471
472         pre_div >>= 1;
473         div -= 1;
474
475         clk = (pre_div << 8) | (div << 4);
476
477         esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
478
479         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
480
481         udelay(10000);
482
483         clk = SYSCTL_PEREN | SYSCTL_CKEN;
484
485         esdhc_setbits32(&regs->sysctl, clk);
486 }
487
488 static void esdhc_set_ios(struct mmc *mmc)
489 {
490         struct fsl_esdhc_cfg *cfg = mmc->priv;
491         struct fsl_esdhc *regs = cfg->esdhc_base;
492
493         /* Set the clock speed */
494         set_sysctl(mmc, mmc->clock);
495
496         /* Set the bus width */
497         esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
498
499         if (mmc->bus_width == 4)
500                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
501         else if (mmc->bus_width == 8)
502                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
503
504 }
505
506 static int esdhc_init(struct mmc *mmc)
507 {
508         struct fsl_esdhc_cfg *cfg = mmc->priv;
509         struct fsl_esdhc *regs = cfg->esdhc_base;
510         int timeout = 1000;
511
512         /* Reset the entire host controller */
513         esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
514
515         /* Wait until the controller is available */
516         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
517                 udelay(1000);
518
519 #ifndef ARCH_MXC
520         /* Enable cache snooping */
521         esdhc_write32(&regs->scr, 0x00000040);
522 #endif
523
524         esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
525
526         /* Set the initial clock speed */
527         mmc_set_clock(mmc, 400000);
528
529         /* Disable the BRR and BWR bits in IRQSTAT */
530         esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
531
532         /* Put the PROCTL reg back to the default */
533         esdhc_write32(&regs->proctl, PROCTL_INIT);
534
535         /* Set timout to the maximum value */
536         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
537
538         return 0;
539 }
540
541 static int esdhc_getcd(struct mmc *mmc)
542 {
543         struct fsl_esdhc_cfg *cfg = mmc->priv;
544         struct fsl_esdhc *regs = cfg->esdhc_base;
545         int timeout = 1000;
546
547         while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
548                 udelay(1000);
549
550         return timeout > 0;
551 }
552
553 static void esdhc_reset(struct fsl_esdhc *regs)
554 {
555         unsigned long timeout = 100; /* wait max 100 ms */
556
557         /* reset the controller */
558         esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
559
560         /* hardware clears the bit when it is done */
561         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
562                 udelay(1000);
563         if (!timeout)
564                 printf("MMC/SD: Reset never completed.\n");
565 }
566
567 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
568 {
569         struct fsl_esdhc *regs;
570         struct mmc *mmc;
571         u32 caps, voltage_caps;
572
573         if (!cfg)
574                 return -EINVAL;
575
576         mmc = kzalloc(sizeof(struct mmc), GFP_KERNEL);
577         if (!mmc)
578                 return -ENOMEM;
579
580         sprintf(mmc->name, "FSL_SDHC");
581         regs = cfg->esdhc_base;
582
583         /* First reset the eSDHC controller */
584         esdhc_reset(regs);
585
586         esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
587                                 | SYSCTL_IPGEN | SYSCTL_CKEN);
588
589         mmc->priv = cfg;
590         mmc->send_cmd = esdhc_send_cmd;
591         mmc->set_ios = esdhc_set_ios;
592         mmc->init = esdhc_init;
593         mmc->getcd = esdhc_getcd;
594         mmc->getwp = NULL;
595
596         voltage_caps = 0;
597         caps = regs->hostcapblt;
598
599 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
600         caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
601                         ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
602 #endif
603         if (caps & ESDHC_HOSTCAPBLT_VS18)
604                 voltage_caps |= MMC_VDD_165_195;
605         if (caps & ESDHC_HOSTCAPBLT_VS30)
606                 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
607         if (caps & ESDHC_HOSTCAPBLT_VS33)
608                 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
609
610 #ifdef CONFIG_SYS_SD_VOLTAGE
611         mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
612 #else
613         mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
614 #endif
615         if ((mmc->voltages & voltage_caps) == 0) {
616                 printf("voltage not supported by controller\n");
617                 return -EINVAL;
618         }
619
620         mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
621
622         if (cfg->max_bus_width > 0) {
623                 if (cfg->max_bus_width < 8)
624                         mmc->host_caps &= ~MMC_MODE_8BIT;
625                 if (cfg->max_bus_width < 4)
626                         mmc->host_caps &= ~MMC_MODE_4BIT;
627         }
628
629         if (caps & ESDHC_HOSTCAPBLT_HSS)
630                 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
631
632         mmc->f_min = 400000;
633         mmc->f_max = MIN(cfg->sdhc_clk, 52000000);
634
635         mmc->b_max = 0;
636         mmc_register(mmc);
637
638         return 0;
639 }
640
641 int fsl_esdhc_mmc_init(bd_t *bis)
642 {
643         struct fsl_esdhc_cfg *cfg;
644
645         cfg = kzalloc(sizeof(struct fsl_esdhc_cfg), GFP_KERNEL);
646         if (!cfg)
647                 return -ENOMEM;
648         cfg->esdhc_base = (void __iomem *)CONFIG_SYS_FSL_ESDHC_ADDR;
649         cfg->sdhc_clk = gd->arch.sdhc_clk;
650         return fsl_esdhc_initialize(bis, cfg);
651 }
652
653 #ifdef CONFIG_OF_LIBFDT
654 void fdt_fixup_esdhc(void *blob, bd_t *bd)
655 {
656         const char *compat = "fsl,esdhc";
657
658 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
659         if (!hwconfig("esdhc")) {
660                 do_fixup_by_compat(blob, compat, "status", "disabled",
661                                 8 + 1, 1);
662                 return;
663         }
664 #endif
665
666         do_fixup_by_compat_u32(blob, compat, "clock-frequency",
667                                gd->arch.sdhc_clk, 1);
668
669         do_fixup_by_compat(blob, compat, "status", "okay",
670                            4 + 1, 1);
671 }
672 #endif