]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/fsl_esdhc.c
upgrade to upstream version 2013.07
[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         esdhc_write32(&regs->irqstat, -1);
278
279         sync();
280
281         start = get_timer_masked();
282         /* Wait for the bus to be idle */
283         while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
284                 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB)) {
285                 if (get_timer(start) > CONFIG_SYS_HZ) {
286                         printf("%s: Timeout waiting for bus idle\n", __func__);
287                         return TIMEOUT;
288                 }
289         }
290
291         start = get_timer_masked();
292         while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA) {
293                 if (get_timer(start) > CONFIG_SYS_HZ)
294                         return TIMEOUT;
295         }
296
297         /* Wait at least 8 SD clock cycles before the next command */
298         /*
299          * Note: This is way more than 8 cycles, but 1ms seems to
300          * resolve timing issues with some cards
301          */
302         udelay(1000);
303
304         /* Set up for a data transfer if we have one */
305         if (data) {
306                 int err;
307
308                 err = esdhc_setup_data(mmc, data);
309                 if (err)
310                         return err;
311         }
312
313         /* Figure out the transfer arguments */
314         xfertyp = esdhc_xfertyp(cmd, data);
315
316         /* Mask all irqs */
317         esdhc_write32(&regs->irqsigen, 0);
318
319         /* Send the command */
320         esdhc_write32(&regs->cmdarg, cmd->cmdarg);
321 #if defined(CONFIG_FSL_USDHC)
322         esdhc_write32(&regs->mixctrl,
323         (esdhc_read32(&regs->mixctrl) & ~0x7f) | (xfertyp & 0x7F));
324         esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
325 #else
326         esdhc_write32(&regs->xfertyp, xfertyp);
327 #endif
328
329         /* Mask all irqs */
330         esdhc_write32(&regs->irqsigen, 0);
331
332         start = get_timer_masked();
333         /* Wait for the command to complete */
334         while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) {
335                 if (get_timer(start) > CONFIG_SYS_HZ) {
336                         printf("%s: Timeout waiting for cmd completion\n", __func__);
337                         return TIMEOUT;
338                 }
339         }
340
341         if (data && (data->flags & MMC_DATA_READ))
342                 check_and_invalidate_dcache_range(cmd, data);
343
344         irqstat = esdhc_read32(&regs->irqstat);
345
346         /* Reset CMD and DATA portions on error */
347         if (irqstat & (CMD_ERR | IRQSTAT_CTOE)) {
348                 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
349                               SYSCTL_RSTC);
350                 start = get_timer_masked();
351                 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC) {
352                         if (get_timer(start) > CONFIG_SYS_HZ)
353                                 return TIMEOUT;
354                 }
355
356                 if (data) {
357                         esdhc_write32(&regs->sysctl,
358                                       esdhc_read32(&regs->sysctl) |
359                                       SYSCTL_RSTD);
360                         start = get_timer_masked();
361                         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD)) {
362                                 if (get_timer(start) > CONFIG_SYS_HZ)
363                                         return TIMEOUT;
364                         }
365                 }
366         }
367
368         if (irqstat & CMD_ERR)
369                 return COMM_ERR;
370
371         if (irqstat & IRQSTAT_CTOE)
372                 return TIMEOUT;
373
374         /* Workaround for ESDHC errata ENGcm03648 */
375         if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
376                 int timeout = 2500;
377
378                 /* Poll on DATA0 line for cmd with busy signal for 250 ms */
379                 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
380                                         PRSSTAT_DAT0)) {
381                         udelay(100);
382                         timeout--;
383                 }
384
385                 if (timeout <= 0) {
386                         printf("Timeout waiting for DAT0 to go high!\n");
387                         return TIMEOUT;
388                 }
389         }
390
391         /* Copy the response to the response buffer */
392         if (cmd->resp_type & MMC_RSP_136) {
393                 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
394
395                 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
396                 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
397                 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
398                 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
399                 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
400                 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
401                 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
402                 cmd->response[3] = (cmdrsp0 << 8);
403         } else
404                 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
405
406         /* Wait until all of the blocks are transferred */
407         if (data) {
408 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
409                 esdhc_pio_read_write(mmc, data);
410 #else
411                 unsigned long start = get_timer_masked();
412                 unsigned long data_timeout = data->blocks *
413                         (data->blocksize + 100) * 8 / mmc->bus_width /
414                         (mmc->tran_speed / CONFIG_SYS_HZ) + CONFIG_SYS_HZ;
415
416                 do {
417                         irqstat = esdhc_read32(&regs->irqstat);
418
419                         if (irqstat & IRQSTAT_DTOE) {
420                                 printf("MMC/SD data %s timeout\n",
421                                         data->flags & MMC_DATA_READ ?
422                                         "read" : "write");
423                                 return TIMEOUT;
424                         }
425
426                         if (irqstat & DATA_ERR) {
427                                 printf("MMC/SD data error\n");
428                                 return COMM_ERR;
429                         }
430
431                         if (get_timer(start) > data_timeout) {
432                                 printf("MMC/SD timeout waiting for %s xfer completion\n",
433                                                 data->flags & MMC_DATA_READ ?
434                                                 "read" : "write");
435                                 return TIMEOUT;
436                         }
437                 } while (!(irqstat & IRQSTAT_TC) &&
438                         (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
439
440                 check_and_invalidate_dcache_range(cmd, data);
441 #endif
442         }
443
444         esdhc_write32(&regs->irqstat, irqstat);
445
446         return 0;
447 }
448
449 static void set_sysctl(struct mmc *mmc, uint clock)
450 {
451         int div, pre_div;
452         struct fsl_esdhc_cfg *cfg = mmc->priv;
453         volatile struct fsl_esdhc *regs = cfg->esdhc_base;
454         int sdhc_clk = cfg->sdhc_clk;
455         uint clk;
456
457         if (clock < mmc->f_min)
458                 clock = mmc->f_min;
459
460         if (sdhc_clk / 16 > clock) {
461                 for (pre_div = 2; pre_div < 256; pre_div *= 2)
462                         if ((sdhc_clk / pre_div) <= (clock * 16))
463                                 break;
464         } else
465                 pre_div = 2;
466
467         for (div = 1; div <= 16; div++)
468                 if ((sdhc_clk / (div * pre_div)) <= clock)
469                         break;
470
471         pre_div >>= 1;
472         div -= 1;
473
474         clk = (pre_div << 8) | (div << 4);
475
476         esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
477
478         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
479
480         udelay(10000);
481
482         clk = SYSCTL_PEREN | SYSCTL_CKEN;
483
484         esdhc_setbits32(&regs->sysctl, clk);
485 }
486
487 static void esdhc_set_ios(struct mmc *mmc)
488 {
489         struct fsl_esdhc_cfg *cfg = mmc->priv;
490         struct fsl_esdhc *regs = cfg->esdhc_base;
491
492         /* Set the clock speed */
493         set_sysctl(mmc, mmc->clock);
494
495         /* Set the bus width */
496         esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
497
498         if (mmc->bus_width == 4)
499                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
500         else if (mmc->bus_width == 8)
501                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
502
503 }
504
505 static int esdhc_init(struct mmc *mmc)
506 {
507         struct fsl_esdhc_cfg *cfg = mmc->priv;
508         struct fsl_esdhc *regs = cfg->esdhc_base;
509         int timeout = 1000;
510
511         /* Reset the entire host controller */
512         esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
513
514         /* Wait until the controller is available */
515         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
516                 udelay(1000);
517
518 #ifndef ARCH_MXC
519         /* Enable cache snooping */
520         esdhc_write32(&regs->scr, 0x00000040);
521 #endif
522
523         esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
524
525         /* Set the initial clock speed */
526         mmc_set_clock(mmc, 400000);
527
528         /* Disable the BRR and BWR bits in IRQSTAT */
529         esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
530
531         /* Put the PROCTL reg back to the default */
532         esdhc_write32(&regs->proctl, PROCTL_INIT);
533
534         /* Set timout to the maximum value */
535         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
536
537         return 0;
538 }
539
540 static int esdhc_getcd(struct mmc *mmc)
541 {
542         struct fsl_esdhc_cfg *cfg = mmc->priv;
543         struct fsl_esdhc *regs = cfg->esdhc_base;
544         int timeout = 1000;
545
546         while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
547                 udelay(1000);
548
549         return timeout > 0;
550 }
551
552 static void esdhc_reset(struct fsl_esdhc *regs)
553 {
554         unsigned long timeout = 100; /* wait max 100 ms */
555
556         /* reset the controller */
557         esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
558
559         /* hardware clears the bit when it is done */
560         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
561                 udelay(1000);
562         if (!timeout)
563                 printf("MMC/SD: Reset never completed.\n");
564 }
565
566 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
567 {
568         struct fsl_esdhc *regs;
569         struct mmc *mmc;
570         u32 caps, voltage_caps;
571
572         if (!cfg)
573                 return -EINVAL;
574
575         mmc = kzalloc(sizeof(struct mmc), GFP_KERNEL);
576         if (!mmc)
577                 return -ENOMEM;
578
579         sprintf(mmc->name, "FSL_SDHC");
580         regs = cfg->esdhc_base;
581
582         /* First reset the eSDHC controller */
583         esdhc_reset(regs);
584
585         esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
586                                 | SYSCTL_IPGEN | SYSCTL_CKEN);
587
588         mmc->priv = cfg;
589         mmc->send_cmd = esdhc_send_cmd;
590         mmc->set_ios = esdhc_set_ios;
591         mmc->init = esdhc_init;
592         mmc->getcd = esdhc_getcd;
593         mmc->getwp = NULL;
594
595         voltage_caps = 0;
596         caps = regs->hostcapblt;
597
598 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
599         caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
600                         ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
601 #endif
602         if (caps & ESDHC_HOSTCAPBLT_VS18)
603                 voltage_caps |= MMC_VDD_165_195;
604         if (caps & ESDHC_HOSTCAPBLT_VS30)
605                 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
606         if (caps & ESDHC_HOSTCAPBLT_VS33)
607                 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
608
609 #ifdef CONFIG_SYS_SD_VOLTAGE
610         mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
611 #else
612         mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
613 #endif
614         if ((mmc->voltages & voltage_caps) == 0) {
615                 printf("voltage not supported by controller\n");
616                 return -EINVAL;
617         }
618
619         mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC;
620
621         if (cfg->max_bus_width > 0) {
622                 if (cfg->max_bus_width < 8)
623                         mmc->host_caps &= ~MMC_MODE_8BIT;
624                 if (cfg->max_bus_width < 4)
625                         mmc->host_caps &= ~MMC_MODE_4BIT;
626         }
627
628         if (caps & ESDHC_HOSTCAPBLT_HSS)
629                 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
630
631         mmc->f_min = 400000;
632         mmc->f_max = MIN(cfg->sdhc_clk, 52000000);
633
634         mmc->b_max = 0;
635         mmc_register(mmc);
636
637         return 0;
638 }
639
640 int fsl_esdhc_mmc_init(bd_t *bis)
641 {
642         struct fsl_esdhc_cfg *cfg;
643
644         cfg = kzalloc(sizeof(struct fsl_esdhc_cfg), GFP_KERNEL);
645         if (!cfg)
646                 return -ENOMEM;
647         cfg->esdhc_base = (void __iomem *)CONFIG_SYS_FSL_ESDHC_ADDR;
648         cfg->sdhc_clk = gd->arch.sdhc_clk;
649         return fsl_esdhc_initialize(bis, cfg);
650 }
651
652 #ifdef CONFIG_OF_LIBFDT
653 void fdt_fixup_esdhc(void *blob, bd_t *bd)
654 {
655         const char *compat = "fsl,esdhc";
656
657 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
658         if (!hwconfig("esdhc")) {
659                 do_fixup_by_compat(blob, compat, "status", "disabled",
660                                 8 + 1, 1);
661                 return;
662         }
663 #endif
664
665         do_fixup_by_compat_u32(blob, compat, "clock-frequency",
666                                gd->arch.sdhc_clk, 1);
667
668         do_fixup_by_compat(blob, compat, "status", "okay",
669                            4 + 1, 1);
670 }
671 #endif