uboot/drivers/mmc/tegra2_mmc.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2009 SAMSUNG Electronics
   3 * Minkyu Kang <mk7.kang@samsung.com>
   4 * Jaehoon Chung <jh80.chung@samsung.com>
   5 * Portions Copyright 2011 NVIDIA Corporation
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 */
  21
  22#include <common.h>
  23#include <mmc.h>
  24#include <asm/io.h>
  25#include <asm/arch/clk_rst.h>
  26#include "tegra2_mmc.h"
  27
  28/* support 4 mmc hosts */
  29struct mmc mmc_dev[4];
  30struct mmc_host mmc_host[4];
  31
  32static inline struct tegra2_mmc *tegra2_get_base_mmc(int dev_index)
  33{
  34        unsigned long offset;
  35        debug("tegra2_get_base_mmc: dev_index = %d\n", dev_index);
  36
  37        switch (dev_index) {
  38        case 0:
  39                offset = TEGRA2_SDMMC4_BASE;
  40                break;
  41        case 1:
  42                offset = TEGRA2_SDMMC3_BASE;
  43                break;
  44        case 2:
  45                offset = TEGRA2_SDMMC2_BASE;
  46                break;
  47        case 3:
  48                offset = TEGRA2_SDMMC1_BASE;
  49                break;
  50        default:
  51                offset = TEGRA2_SDMMC4_BASE;
  52                break;
  53        }
  54
  55        return (struct tegra2_mmc *)(offset);
  56}
  57
  58static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
  59{
  60        unsigned char ctrl;
  61
  62        debug("data->dest: %08X, data->blocks: %u, data->blocksize: %u\n",
  63        (u32)data->dest, data->blocks, data->blocksize);
  64
  65        writel((u32)data->dest, &host->reg->sysad);
  66        /*
  67         * DMASEL[4:3]
  68         * 00 = Selects SDMA
  69         * 01 = Reserved
  70         * 10 = Selects 32-bit Address ADMA2
  71         * 11 = Selects 64-bit Address ADMA2
  72         */
  73        ctrl = readb(&host->reg->hostctl);
  74        ctrl &= ~(3 << 3);                      /* SDMA */
  75        writeb(ctrl, &host->reg->hostctl);
  76
  77        /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  78        writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
  79        writew(data->blocks, &host->reg->blkcnt);
  80}
  81
  82static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
  83{
  84        unsigned short mode;
  85        debug(" mmc_set_transfer_mode called\n");
  86        /*
  87         * TRNMOD
  88         * MUL1SIN0[5]  : Multi/Single Block Select
  89         * RD1WT0[4]    : Data Transfer Direction Select
  90         *      1 = read
  91         *      0 = write
  92         * ENACMD12[2]  : Auto CMD12 Enable
  93         * ENBLKCNT[1]  : Block Count Enable
  94         * ENDMA[0]     : DMA Enable
  95         */
  96        mode = (1 << 1) | (1 << 0);
  97        if (data->blocks > 1)
  98                mode |= (1 << 5);
  99        if (data->flags & MMC_DATA_READ)
 100                mode |= (1 << 4);
 101
 102        writew(mode, &host->reg->trnmod);
 103}
 104
 105static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
 106                        struct mmc_data *data)
 107{
 108        struct mmc_host *host = (struct mmc_host *)mmc->priv;
 109        int flags, i;
 110        unsigned int timeout;
 111        unsigned int mask;
 112        unsigned int retry = 0x100000;
 113        debug(" mmc_send_cmd called\n");
 114
 115        /* Wait max 10 ms */
 116        timeout = 10;
 117
 118        /*
 119         * PRNSTS
 120         * CMDINHDAT[1] : Command Inhibit (DAT)
 121         * CMDINHCMD[0] : Command Inhibit (CMD)
 122         */
 123        mask = (1 << 0);
 124        if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
 125                mask |= (1 << 1);
 126
 127        /*
 128         * We shouldn't wait for data inhibit for stop commands, even
 129         * though they might use busy signaling
 130         */
 131        if (data)
 132                mask &= ~(1 << 1);
 133
 134        while (readl(&host->reg->prnsts) & mask) {
 135                if (timeout == 0) {
 136                        printf("%s: timeout error\n", __func__);
 137                        return -1;
 138                }
 139                timeout--;
 140                udelay(1000);
 141        }
 142
 143        if (data)
 144                mmc_prepare_data(host, data);
 145
 146        debug("cmd->arg: %08x\n", cmd->cmdarg);
 147        writel(cmd->cmdarg, &host->reg->argument);
 148
 149        if (data)
 150                mmc_set_transfer_mode(host, data);
 151
 152        if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
 153                return -1;
 154
 155        /*
 156         * CMDREG
 157         * CMDIDX[13:8] : Command index
 158         * DATAPRNT[5]  : Data Present Select
 159         * ENCMDIDX[4]  : Command Index Check Enable
 160         * ENCMDCRC[3]  : Command CRC Check Enable
 161         * RSPTYP[1:0]
 162         *      00 = No Response
 163         *      01 = Length 136
 164         *      10 = Length 48
 165         *      11 = Length 48 Check busy after response
 166         */
 167        if (!(cmd->resp_type & MMC_RSP_PRESENT))
 168                flags = 0;
 169        else if (cmd->resp_type & MMC_RSP_136)
 170                flags = (1 << 0);
 171        else if (cmd->resp_type & MMC_RSP_BUSY)
 172                flags = (3 << 0);
 173        else
 174                flags = (2 << 0);
 175
 176        if (cmd->resp_type & MMC_RSP_CRC)
 177                flags |= (1 << 3);
 178        if (cmd->resp_type & MMC_RSP_OPCODE)
 179                flags |= (1 << 4);
 180        if (data)
 181                flags |= (1 << 5);
 182
 183        debug("cmd: %d\n", cmd->cmdidx);
 184
 185        writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
 186
 187        for (i = 0; i < retry; i++) {
 188                mask = readl(&host->reg->norintsts);
 189                /* Command Complete */
 190                if (mask & (1 << 0)) {
 191                        if (!data)
 192                                writel(mask, &host->reg->norintsts);
 193                        break;
 194                }
 195        }
 196
 197        if (i == retry) {
 198                printf("%s: waiting for status update\n", __func__);
 199                return TIMEOUT;
 200        }
 201
 202        if (mask & (1 << 16)) {
 203                /* Timeout Error */
 204                debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
 205                return TIMEOUT;
 206        } else if (mask & (1 << 15)) {
 207                /* Error Interrupt */
 208                debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
 209                return -1;
 210        }
 211
 212        if (cmd->resp_type & MMC_RSP_PRESENT) {
 213                if (cmd->resp_type & MMC_RSP_136) {
 214                        /* CRC is stripped so we need to do some shifting. */
 215                        for (i = 0; i < 4; i++) {
 216                                unsigned int offset =
 217                                        (unsigned int)(&host->reg->rspreg3 - i);
 218                                cmd->response[i] = readl(offset) << 8;
 219
 220                                if (i != 3) {
 221                                        cmd->response[i] |=
 222                                                readb(offset - 1);
 223                                }
 224                                debug("cmd->resp[%d]: %08x\n",
 225                                                i, cmd->response[i]);
 226                        }
 227                } else if (cmd->resp_type & MMC_RSP_BUSY) {
 228                        for (i = 0; i < retry; i++) {
 229                                /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
 230                                if (readl(&host->reg->prnsts)
 231                                        & (1 << 20))    /* DAT[0] */
 232                                        break;
 233                        }
 234
 235                        if (i == retry) {
 236                                printf("%s: card is still busy\n", __func__);
 237                                return TIMEOUT;
 238                        }
 239
 240                        cmd->response[0] = readl(&host->reg->rspreg0);
 241                        debug("cmd->resp[0]: %08x\n", cmd->response[0]);
 242                } else {
 243                        cmd->response[0] = readl(&host->reg->rspreg0);
 244                        debug("cmd->resp[0]: %08x\n", cmd->response[0]);
 245                }
 246        }
 247
 248        if (data) {
 249                while (1) {
 250                        mask = readl(&host->reg->norintsts);
 251
 252                        if (mask & (1 << 15)) {
 253                                /* Error Interrupt */
 254                                writel(mask, &host->reg->norintsts);
 255                                printf("%s: error during transfer: 0x%08x\n",
 256                                                __func__, mask);
 257                                return -1;
 258                        } else if (mask & (1 << 3)) {
 259                                /* DMA Interrupt */
 260                                debug("DMA end\n");
 261                                break;
 262                        } else if (mask & (1 << 1)) {
 263                                /* Transfer Complete */
 264                                debug("r/w is done\n");
 265                                break;
 266                        }
 267                }
 268                writel(mask, &host->reg->norintsts);
 269        }
 270
 271        udelay(1000);
 272        return 0;
 273}
 274
 275static void mmc_change_clock(struct mmc_host *host, uint clock)
 276{
 277        int div, hw_div;
 278        unsigned short clk;
 279        unsigned long timeout;
 280        unsigned int reg, hostbase;
 281        struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
 282        debug(" mmc_change_clock called\n");
 283
 284        /* Change Tegra2 SDMMCx clock divisor here */
 285        /* Source is 216MHz, PLLP_OUT0 */
 286        if (clock == 0)
 287                goto out;
 288
 289        div = 1;
 290        if (clock <= 400000) {
 291                hw_div = ((9-1)<<1);            /* Best match is 375KHz */
 292                div = 64;
 293        } else if (clock <= 20000000)
 294                hw_div = ((11-1)<<1);           /* Best match is 19.6MHz */
 295        else if (clock <= 26000000)
 296                hw_div = ((9-1)<<1);            /* Use 24MHz */
 297        else
 298                hw_div = ((4-1)<<1) + 1;        /* 4.5 divisor for 48MHz */
 299
 300        debug("mmc_change_clock: hw_div = %d, card clock div = %d\n",
 301                hw_div, div);
 302
 303        /* Change SDMMCx divisor */
 304
 305        hostbase = readl(&host->base);
 306        debug("mmc_change_clock: hostbase = %08X\n", hostbase);
 307
 308        if (hostbase == TEGRA2_SDMMC1_BASE) {
 309                reg = readl(&clkrst->crc_clk_src_sdmmc1);
 310                reg &= 0xFFFFFF00;      /* divisor (7.1) = 00 */
 311                reg |= hw_div;          /* n-1 */
 312                writel(reg, &clkrst->crc_clk_src_sdmmc1);
 313        } else if (hostbase == TEGRA2_SDMMC2_BASE) {
 314                reg = readl(&clkrst->crc_clk_src_sdmmc2);
 315                reg &= 0xFFFFFF00;      /* divisor (7.1) = 00 */
 316                reg |= hw_div;          /* n-1 */
 317                writel(reg, &clkrst->crc_clk_src_sdmmc2);
 318        } else if (hostbase == TEGRA2_SDMMC3_BASE) {
 319                reg = readl(&clkrst->crc_clk_src_sdmmc3);
 320                reg &= 0xFFFFFF00;      /* divisor (7.1) = 00 */
 321                reg |= hw_div;          /* n-1 */
 322                writel(reg, &clkrst->crc_clk_src_sdmmc3);
 323        } else {
 324                reg = readl(&clkrst->crc_clk_src_sdmmc4);
 325                reg &= 0xFFFFFF00;      /* divisor (7.1) = 00 */
 326                reg |= hw_div;          /* n-1 */
 327                writel(reg, &clkrst->crc_clk_src_sdmmc4);
 328        }
 329
 330        writew(0, &host->reg->clkcon);
 331
 332        div >>= 1;
 333        /*
 334         * CLKCON
 335         * SELFREQ[15:8]        : base clock divided by value
 336         * ENSDCLK[2]           : SD Clock Enable
 337         * STBLINTCLK[1]        : Internal Clock Stable
 338         * ENINTCLK[0]          : Internal Clock Enable
 339         */
 340        clk = (div << 8) | (1 << 0);
 341        writew(clk, &host->reg->clkcon);
 342
 343        /* Wait max 10 ms */
 344        timeout = 10;
 345        while (!(readw(&host->reg->clkcon) & (1 << 1))) {
 346                if (timeout == 0) {
 347                        printf("%s: timeout error\n", __func__);
 348                        return;
 349                }
 350                timeout--;
 351                udelay(1000);
 352        }
 353
 354        clk |= (1 << 2);
 355        writew(clk, &host->reg->clkcon);
 356
 357        debug("mmc_change_clock: clkcon = %08X\n", clk);
 358        debug("mmc_change_clock: CLK_SOURCE_SDMMCx = %08X\n", reg);
 359
 360out:
 361        host->clock = clock;
 362}
 363
 364static void mmc_set_ios(struct mmc *mmc)
 365{
 366        struct mmc_host *host = mmc->priv;
 367        unsigned char ctrl;
 368        debug(" mmc_set_ios called\n");
 369
 370        debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
 371
 372        /* Change clock first */
 373
 374        mmc_change_clock(host, mmc->clock);
 375
 376        ctrl = readb(&host->reg->hostctl);
 377
 378        /*
 379         * WIDE8[5]
 380         * 0 = Depend on WIDE4
 381         * 1 = 8-bit mode
 382         * WIDE4[1]
 383         * 1 = 4-bit mode
 384         * 0 = 1-bit mode
 385         */
 386        if (mmc->bus_width == 8)
 387                ctrl |= (1 << 5);
 388        else if (mmc->bus_width == 4)
 389                ctrl |= (1 << 1);
 390        else
 391                ctrl &= ~(1 << 1);
 392
 393        writeb(ctrl, &host->reg->hostctl);
 394        debug("mmc_set_ios: hostctl = %08X\n", ctrl);
 395}
 396
 397static void mmc_reset(struct mmc_host *host)
 398{
 399        unsigned int timeout;
 400        debug(" mmc_reset called\n");
 401
 402        /*
 403         * RSTALL[0] : Software reset for all
 404         * 1 = reset
 405         * 0 = work
 406         */
 407        writeb((1 << 0), &host->reg->swrst);
 408
 409        host->clock = 0;
 410
 411        /* Wait max 100 ms */
 412        timeout = 100;
 413
 414        /* hw clears the bit when it's done */
 415        while (readb(&host->reg->swrst) & (1 << 0)) {
 416                if (timeout == 0) {
 417                        printf("%s: timeout error\n", __func__);
 418                        return;
 419                }
 420                timeout--;
 421                udelay(1000);
 422        }
 423}
 424
 425static int mmc_core_init(struct mmc *mmc)
 426{
 427        struct mmc_host *host = (struct mmc_host *)mmc->priv;
 428        unsigned int mask;
 429        debug(" mmc_core_init called\n");
 430
 431        mmc_reset(host);
 432
 433        host->version = readw(&host->reg->hcver);
 434        debug("host version = %x\n", host->version);
 435
 436        /* mask all */
 437        writel(0xffffffff, &host->reg->norintstsen);
 438        writel(0xffffffff, &host->reg->norintsigen);
 439
 440        writeb(0xe, &host->reg->timeoutcon);    /* TMCLK * 2^27 */
 441        /*
 442         * NORMAL Interrupt Status Enable Register init
 443         * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
 444         * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
 445         * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
 446         * [0] ENSTACMDCMPLT : Command Complete Status Enable
 447        */
 448        mask = readl(&host->reg->norintstsen);
 449        mask &= ~(0xffff);
 450        mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
 451        writel(mask, &host->reg->norintstsen);
 452
 453        /*
 454         * NORMAL Interrupt Signal Enable Register init
 455         * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
 456         */
 457        mask = readl(&host->reg->norintsigen);
 458        mask &= ~(0xffff);
 459        mask |= (1 << 1);
 460        writel(mask, &host->reg->norintsigen);
 461
 462        return 0;
 463}
 464
 465static int tegra2_mmc_initialize(int dev_index, int bus_width)
 466{
 467        struct mmc *mmc;
 468
 469        debug(" mmc_initialize called\n");
 470
 471        mmc = &mmc_dev[dev_index];
 472
 473        sprintf(mmc->name, "Tegra2 SD/MMC");
 474        mmc->priv = &mmc_host[dev_index];
 475        mmc->send_cmd = mmc_send_cmd;
 476        mmc->set_ios = mmc_set_ios;
 477        mmc->init = mmc_core_init;
 478
 479        mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
 480        if (bus_width == 8)
 481                mmc->host_caps = MMC_MODE_8BIT;
 482        else
 483                mmc->host_caps = MMC_MODE_4BIT;
 484        mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
 485
 486        /*
 487         * min freq is for card identification, and is the highest
 488         *  low-speed SDIO card frequency (actually 400KHz)
 489         * max freq is highest HS eMMC clock as per the SD/MMC spec
 490         *  (actually 52MHz)
 491         * Both of these are the closest equivalents w/216MHz source
 492         *  clock and Tegra2 SDMMC divisors.
 493         */
 494        mmc->f_min = 375000;
 495        mmc->f_max = 48000000;
 496
 497        mmc_host[dev_index].clock = 0;
 498        mmc_host[dev_index].reg = tegra2_get_base_mmc(dev_index);
 499        mmc_host[dev_index].base = (unsigned int)mmc_host[dev_index].reg;
 500        mmc_register(mmc);
 501
 502        return 0;
 503}
 504
 505int tegra2_mmc_init(int dev_index, int bus_width)
 506{
 507        debug(" tegra2_mmc_init: index %d, bus width %d\n",
 508                dev_index, bus_width);
 509        return tegra2_mmc_initialize(dev_index, bus_width);
 510}
 511