]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/fsl_esdhc.c
db5278b64d13a4385a129054ecb01d1aa8996ac3
[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  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <config.h>
29 #include <common.h>
30 #include <command.h>
31 #include <hwconfig.h>
32 #include <mmc.h>
33 #include <part.h>
34 #include <malloc.h>
35 #include <mmc.h>
36 #include <fsl_esdhc.h>
37 #include <fdt_support.h>
38 #include <asm/io.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 struct fsl_esdhc {
43         uint    dsaddr;
44         uint    blkattr;
45         uint    cmdarg;
46         uint    xfertyp;
47         uint    cmdrsp0;
48         uint    cmdrsp1;
49         uint    cmdrsp2;
50         uint    cmdrsp3;
51         uint    datport;
52         uint    prsstat;
53         uint    proctl;
54         uint    sysctl;
55         uint    irqstat;
56         uint    irqstaten;
57         uint    irqsigen;
58         uint    autoc12err;
59         uint    hostcapblt;
60         uint    wml;
61         uint    mixctrl;
62         char    reserved1[4];
63         uint    fevt;
64         char    reserved2[168];
65         uint    hostver;
66         char    reserved3[780];
67         uint    scr;
68 };
69
70 /* Return the XFERTYP flags for a given command and data packet */
71 uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
72 {
73         uint xfertyp = 0;
74
75         if (data) {
76                 xfertyp |= XFERTYP_DPSEL;
77 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
78                 xfertyp |= XFERTYP_DMAEN;
79 #endif
80                 if (data->blocks > 1) {
81                         xfertyp |= XFERTYP_MSBSEL;
82                         xfertyp |= XFERTYP_BCEN;
83 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
84                         xfertyp |= XFERTYP_AC12EN;
85 #endif
86                 }
87
88                 if (data->flags & MMC_DATA_READ)
89                         xfertyp |= XFERTYP_DTDSEL;
90         }
91
92         if (cmd->resp_type & MMC_RSP_CRC)
93                 xfertyp |= XFERTYP_CCCEN;
94         if (cmd->resp_type & MMC_RSP_OPCODE)
95                 xfertyp |= XFERTYP_CICEN;
96         if (cmd->resp_type & MMC_RSP_136)
97                 xfertyp |= XFERTYP_RSPTYP_136;
98         else if (cmd->resp_type & MMC_RSP_BUSY)
99                 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
100         else if (cmd->resp_type & MMC_RSP_PRESENT)
101                 xfertyp |= XFERTYP_RSPTYP_48;
102
103 #ifdef CONFIG_MX53
104         if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
105                 xfertyp |= XFERTYP_CMDTYP_ABORT;
106 #endif
107         return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
108 }
109
110 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
111 /*
112  * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
113  */
114 static void
115 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
116 {
117         struct fsl_esdhc_cfg *cfg = mmc->priv;
118         struct fsl_esdhc *regs = cfg->esdhc_base;
119         uint blocks;
120         char *buffer;
121         uint databuf;
122         uint size;
123         uint timeout;
124         int wml = esdhc_read32(&regs->wml);
125
126         if (data->flags & MMC_DATA_READ) {
127                 wml &= WML_RD_WML_MASK;
128                 blocks = data->blocks;
129                 buffer = data->dest;
130                 while (blocks) {
131                         timeout = PIO_TIMEOUT;
132                         size = data->blocksize;
133                         while (size &&
134                                 !(esdhc_read32(&regs->irqstat) & IRQSTAT_TC)) {
135                                 int i;
136                                 u32 prsstat;
137
138                                 while (!((prsstat = esdhc_read32(&regs->prsstat)) &
139                                                 PRSSTAT_BREN) && --timeout)
140                                         /* NOP */;
141                                 if (!(prsstat & PRSSTAT_BREN)) {
142                                         printf("%s: Data Read Failed in PIO Mode\n",
143                                                 __func__);
144                                         return;
145                                 }
146                                 for (i = 0; i < wml && size; i++) {
147                                         databuf = in_le32(&regs->datport);
148                                         memcpy(buffer, &databuf, sizeof(databuf));
149                                         buffer += 4;
150                                         size -= 4;
151                                 }
152                         }
153                         blocks--;
154                 }
155         } else {
156                 wml = (wml & WML_WR_WML_MASK) >> 16;
157                 blocks = data->blocks;
158                 buffer = (char *)data->src; /* cast away 'const' */
159                 while (blocks) {
160                         timeout = PIO_TIMEOUT;
161                         size = data->blocksize;
162                         while (size &&
163                                 !(esdhc_read32(&regs->irqstat) & IRQSTAT_TC)) {
164                                 int i;
165                                 u32 prsstat;
166
167                                 while (!((prsstat = esdhc_read32(&regs->prsstat)) &
168                                                 PRSSTAT_BWEN) && --timeout)
169                                         /* NOP */;
170                                 if (!(prsstat & PRSSTAT_BWEN)) {
171                                         printf("%s: Data Write Failed in PIO Mode\n",
172                                                 __func__);
173                                         return;
174                                 }
175                                 for (i = 0; i < wml && size; i++) {
176                                         memcpy(&databuf, buffer, sizeof(databuf));
177                                         out_le32(&regs->datport, databuf);
178                                         buffer += 4;
179                                         size -= 4;
180                                 }
181                         }
182                         blocks--;
183                 }
184         }
185 }
186 #endif
187
188 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
189 {
190         int timeout;
191         struct fsl_esdhc_cfg *cfg = mmc->priv;
192         struct fsl_esdhc *regs = cfg->esdhc_base;
193 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
194         uint wml_value;
195
196         wml_value = data->blocksize/4;
197
198         if (data->flags & MMC_DATA_READ) {
199                 if (wml_value > WML_RD_WML_MAX)
200                         wml_value = WML_RD_WML_MAX_VAL;
201
202                 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
203                 esdhc_write32(&regs->dsaddr, (u32)data->dest);
204         } else {
205                 if (wml_value > WML_WR_WML_MAX)
206                         wml_value = WML_WR_WML_MAX_VAL;
207                 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
208                         printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
209                         return TIMEOUT;
210                 }
211
212                 flush_dcache_range((unsigned long)data->src,
213                                 (unsigned long)data->src + data->blocks * data->blocksize);
214                 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
215                                         wml_value << 16);
216                 esdhc_write32(&regs->dsaddr, (u32)data->src);
217         }
218 #else   /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
219         if (!(data->flags & MMC_DATA_READ)) {
220                 if ((esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL) == 0) {
221                         printf("\nThe SD card is locked. "
222                                 "Can not write to a locked card.\n\n");
223                         return TIMEOUT;
224                 }
225                 esdhc_write32(&regs->dsaddr, (u32)data->src);
226         } else {
227                 esdhc_write32(&regs->dsaddr, (u32)data->dest);
228         }
229 #endif  /* CONFIG_SYS_FSL_ESDHC_USE_PIO */
230
231         esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
232
233         /* Calculate the timeout period for data transactions */
234         /*
235          * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
236          * 2)Timeout period should be minimum 0.250sec as per SD Card spec
237          *  So, Number of SD Clock cycles for 0.25sec should be minimum
238          *              (SD Clock/sec * 0.25 sec) SD Clock cycles
239          *              = (mmc->tran_speed * 1/4) SD Clock cycles
240          * As 1) >=  2)
241          * => (2^(timeout+13)) >= mmc->tran_speed * 1/4
242          * Taking log2 both the sides
243          * => timeout + 13 >= log2(mmc->tran_speed/4)
244          * Rounding up to next power of 2
245          * => timeout + 13 = log2(mmc->tran_speed/4) + 1
246          * => timeout + 13 = fls(mmc->tran_speed/4)
247          */
248         timeout = fls(mmc->tran_speed/4);
249         timeout -= 13;
250
251         if (timeout > 14)
252                 timeout = 14;
253
254         if (timeout < 0)
255                 timeout = 0;
256
257 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
258         if ((timeout == 4) || (timeout == 8) || (timeout == 12))
259                 timeout++;
260 #endif
261
262         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
263
264         return 0;
265 }
266
267
268 /*
269  * Sends a command out on the bus.  Takes the mmc pointer,
270  * a command pointer, and an optional data pointer.
271  */
272 static int
273 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
274 {
275         uint    xfertyp;
276         uint    irqstat;
277         struct fsl_esdhc_cfg *cfg = mmc->priv;
278         volatile struct fsl_esdhc *regs = cfg->esdhc_base;
279
280 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
281         if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
282                 return 0;
283 #endif
284
285         esdhc_write32(&regs->irqstat, -1);
286
287         sync();
288
289         /* Wait for the bus to be idle */
290         while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
291                         (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
292                 ;
293
294         while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
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         /* Send the command */
317         esdhc_write32(&regs->cmdarg, cmd->cmdarg);
318 #if defined(CONFIG_FSL_USDHC)
319         esdhc_write32(&regs->mixctrl,
320         (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F));
321         esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
322 #else
323         esdhc_write32(&regs->xfertyp, xfertyp);
324 #endif
325         /* Wait for the command to complete */
326         while (!(esdhc_read32(&regs->irqstat) & IRQSTAT_CC))
327                 ;
328
329         irqstat = esdhc_read32(&regs->irqstat);
330         esdhc_write32(&regs->irqstat, irqstat);
331
332         if (irqstat & CMD_ERR)
333                 return COMM_ERR;
334
335         if (irqstat & IRQSTAT_CTOE)
336                 return TIMEOUT;
337
338         /* Copy the response to the response buffer */
339         if (cmd->resp_type & MMC_RSP_136) {
340                 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
341
342                 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
343                 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
344                 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
345                 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
346                 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
347                 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
348                 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
349                 cmd->response[3] = (cmdrsp0 << 8);
350         } else
351                 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
352
353         /* Wait until all of the blocks are transferred */
354         if (data) {
355 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
356                 esdhc_pio_read_write(mmc, data);
357 #else
358                 do {
359                         irqstat = esdhc_read32(&regs->irqstat);
360
361                         if (irqstat & IRQSTAT_DTOE)
362                                 return TIMEOUT;
363
364                         if (irqstat & DATA_ERR)
365                                 return COMM_ERR;
366                 } while (!(irqstat & IRQSTAT_TC) &&
367                                 (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA));
368                 invalidate_dcache_range((unsigned long)data->dest,
369                         (unsigned long)data->dest + data->blocks * data->blocksize);
370 #endif
371         }
372
373         esdhc_write32(&regs->irqstat, -1);
374
375         return 0;
376 }
377
378 void set_sysctl(struct mmc *mmc, uint clock)
379 {
380         int sdhc_clk = gd->sdhc_clk;
381         int div, pre_div;
382         struct fsl_esdhc_cfg *cfg = mmc->priv;
383         volatile struct fsl_esdhc *regs = cfg->esdhc_base;
384         uint clk;
385
386         if (clock < mmc->f_min)
387                 clock = mmc->f_min;
388
389         if (sdhc_clk / 16 > clock) {
390                 for (pre_div = 2; pre_div < 256; pre_div *= 2)
391                         if ((sdhc_clk / pre_div) <= (clock * 16))
392                                 break;
393         } else
394                 pre_div = 2;
395
396         for (div = 1; div <= 16; div++)
397                 if ((sdhc_clk / (div * pre_div)) <= clock)
398                         break;
399
400         pre_div >>= 1;
401         div -= 1;
402
403         clk = (pre_div << 8) | (div << 4);
404
405         esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
406
407         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
408
409         udelay(10000);
410
411         clk = SYSCTL_PEREN | SYSCTL_CKEN;
412
413         esdhc_setbits32(&regs->sysctl, clk);
414 }
415
416 static void esdhc_set_ios(struct mmc *mmc)
417 {
418         struct fsl_esdhc_cfg *cfg = mmc->priv;
419         struct fsl_esdhc *regs = cfg->esdhc_base;
420
421         /* Set the clock speed */
422         set_sysctl(mmc, mmc->clock);
423
424         /* Set the bus width */
425         esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
426
427         if (mmc->bus_width == 4)
428                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
429         else if (mmc->bus_width == 8)
430                 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
431
432 }
433
434 static int esdhc_init(struct mmc *mmc)
435 {
436         struct fsl_esdhc_cfg *cfg = mmc->priv;
437         struct fsl_esdhc *regs = cfg->esdhc_base;
438         int timeout = 1000;
439
440         /* Reset the entire host controller */
441         esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
442
443         /* Wait until the controller is available */
444         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
445                 udelay(1000);
446
447         /* Enable cache snooping */
448         if (cfg && !cfg->no_snoop)
449                 esdhc_write32(&regs->scr, 0x00000040);
450
451         esdhc_write32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
452
453         /* Set the initial clock speed */
454         mmc_set_clock(mmc, 400000);
455
456         /* Disable the BRR and BWR bits in IRQSTAT */
457         esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
458
459         /* Put the PROCTL reg back to the default */
460         esdhc_write32(&regs->proctl, PROCTL_INIT);
461
462         /* Set timout to the maximum value */
463         esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
464
465         return 0;
466 }
467
468 static int esdhc_getcd(struct mmc *mmc)
469 {
470         struct fsl_esdhc_cfg *cfg = mmc->priv;
471         struct fsl_esdhc *regs = cfg->esdhc_base;
472         int timeout = 1000;
473
474         while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
475                 udelay(1000);
476
477         return timeout > 0;
478 }
479
480 static void esdhc_reset(struct fsl_esdhc *regs)
481 {
482         unsigned long timeout = 100; /* wait max 100 ms */
483
484         /* reset the controller */
485         esdhc_write32(&regs->sysctl, SYSCTL_RSTA);
486
487         /* hardware clears the bit when it is done */
488         while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
489                 udelay(1000);
490         if (!timeout)
491                 printf("MMC/SD: Reset never completed.\n");
492 }
493
494 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
495 {
496         struct fsl_esdhc *regs;
497         struct mmc *mmc;
498         u32 caps, voltage_caps;
499
500         if (!cfg)
501                 return -1;
502
503         mmc = kzalloc(sizeof(struct mmc), GFP_KERNEL);
504
505         sprintf(mmc->name, "FSL_SDHC");
506         regs = cfg->esdhc_base;
507
508         /* First reset the eSDHC controller */
509         esdhc_reset(regs);
510
511         mmc->priv = cfg;
512         mmc->send_cmd = esdhc_send_cmd;
513         mmc->set_ios = esdhc_set_ios;
514         mmc->init = esdhc_init;
515         mmc->getcd = esdhc_getcd;
516
517         voltage_caps = 0;
518         caps = regs->hostcapblt;
519
520 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
521         caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
522                         ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
523 #endif
524         if (caps & ESDHC_HOSTCAPBLT_VS18)
525                 voltage_caps |= MMC_VDD_165_195;
526         if (caps & ESDHC_HOSTCAPBLT_VS30)
527                 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
528         if (caps & ESDHC_HOSTCAPBLT_VS33)
529                 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
530
531 #ifdef CONFIG_SYS_SD_VOLTAGE
532         mmc->voltages = CONFIG_SYS_SD_VOLTAGE;
533 #else
534         mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
535 #endif
536         if ((mmc->voltages & voltage_caps) == 0) {
537                 printf("voltage not supported by controller\n");
538                 return -1;
539         }
540
541         mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
542
543         if (caps & ESDHC_HOSTCAPBLT_HSS)
544                 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
545
546         mmc->f_min = 400000;
547         mmc->f_max = MIN(gd->sdhc_clk, 52000000);
548
549         mmc->b_max = 0;
550         mmc_register(mmc);
551
552         return 0;
553 }
554
555 int fsl_esdhc_mmc_init(bd_t *bis)
556 {
557         struct fsl_esdhc_cfg *cfg;
558
559         cfg = kzalloc(sizeof(struct fsl_esdhc_cfg), GFP_KERNEL);
560
561         cfg->esdhc_base = (void __iomem *)CONFIG_SYS_FSL_ESDHC_ADDR;
562         return fsl_esdhc_initialize(bis, cfg);
563 }
564
565 #ifdef CONFIG_OF_LIBFDT
566 void fdt_fixup_esdhc(void *blob, bd_t *bd)
567 {
568         const char *compat = "fsl,esdhc";
569
570 #ifdef CONFIG_FSL_ESDHC_PIN_MUX
571         if (!hwconfig("esdhc")) {
572                 do_fixup_by_compat(blob, compat, "status", "disabled",
573                                 8 + 1, 1);
574                 return;
575         }
576 #endif
577
578         do_fixup_by_compat_u32(blob, compat, "clock-frequency",
579                                gd->sdhc_clk, 1);
580
581         do_fixup_by_compat(blob, compat, "status", "okay",
582                            4 + 1, 1);
583 }
584 #endif