linux/drivers/mmc/core/mmc_ops.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/mmc/core/mmc_ops.h
   3 *
   4 *  Copyright 2006-2007 Pierre Ossman
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or (at
   9 * your option) any later version.
  10 */
  11
  12#include <linux/types.h>
  13#include <linux/scatterlist.h>
  14
  15#include <linux/mmc/host.h>
  16#include <linux/mmc/card.h>
  17#include <linux/mmc/mmc.h>
  18
  19#include "core.h"
  20#include "mmc_ops.h"
  21
  22static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  23{
  24        int err;
  25        struct mmc_command cmd;
  26
  27        BUG_ON(!host);
  28
  29        memset(&cmd, 0, sizeof(struct mmc_command));
  30
  31        cmd.opcode = MMC_SELECT_CARD;
  32
  33        if (card) {
  34                cmd.arg = card->rca << 16;
  35                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  36        } else {
  37                cmd.arg = 0;
  38                cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  39        }
  40
  41        err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  42        if (err)
  43                return err;
  44
  45        return 0;
  46}
  47
  48int mmc_select_card(struct mmc_card *card)
  49{
  50        BUG_ON(!card);
  51
  52        return _mmc_select_card(card->host, card);
  53}
  54
  55int mmc_deselect_cards(struct mmc_host *host)
  56{
  57        return _mmc_select_card(host, NULL);
  58}
  59
  60int mmc_card_sleepawake(struct mmc_host *host, int sleep)
  61{
  62        struct mmc_command cmd;
  63        struct mmc_card *card = host->card;
  64        int err;
  65
  66        if (sleep)
  67                mmc_deselect_cards(host);
  68
  69        memset(&cmd, 0, sizeof(struct mmc_command));
  70
  71        cmd.opcode = MMC_SLEEP_AWAKE;
  72        cmd.arg = card->rca << 16;
  73        if (sleep)
  74                cmd.arg |= 1 << 15;
  75
  76        cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  77        err = mmc_wait_for_cmd(host, &cmd, 0);
  78        if (err)
  79                return err;
  80
  81        /*
  82         * If the host does not wait while the card signals busy, then we will
  83         * will have to wait the sleep/awake timeout.  Note, we cannot use the
  84         * SEND_STATUS command to poll the status because that command (and most
  85         * others) is invalid while the card sleeps.
  86         */
  87        if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
  88                mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
  89
  90        if (!sleep)
  91                err = mmc_select_card(card);
  92
  93        return err;
  94}
  95
  96int mmc_go_idle(struct mmc_host *host)
  97{
  98        int err;
  99        struct mmc_command cmd;
 100
 101        /*
 102         * Non-SPI hosts need to prevent chipselect going active during
 103         * GO_IDLE; that would put chips into SPI mode.  Remind them of
 104         * that in case of hardware that won't pull up DAT3/nCS otherwise.
 105         *
 106         * SPI hosts ignore ios.chip_select; it's managed according to
 107         * rules that must accomodate non-MMC slaves which this layer
 108         * won't even know about.
 109         */
 110        if (!mmc_host_is_spi(host)) {
 111                mmc_set_chip_select(host, MMC_CS_HIGH);
 112                mmc_delay(1);
 113        }
 114
 115        memset(&cmd, 0, sizeof(struct mmc_command));
 116
 117        cmd.opcode = MMC_GO_IDLE_STATE;
 118        cmd.arg = 0;
 119        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
 120
 121        err = mmc_wait_for_cmd(host, &cmd, 0);
 122
 123        mmc_delay(1);
 124
 125        if (!mmc_host_is_spi(host)) {
 126                mmc_set_chip_select(host, MMC_CS_DONTCARE);
 127                mmc_delay(1);
 128        }
 129
 130        host->use_spi_crc = 0;
 131
 132        return err;
 133}
 134
 135int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
 136{
 137        struct mmc_command cmd;
 138        int i, err = 0;
 139
 140        BUG_ON(!host);
 141
 142        memset(&cmd, 0, sizeof(struct mmc_command));
 143
 144        cmd.opcode = MMC_SEND_OP_COND;
 145        cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
 146        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
 147
 148        for (i = 100; i; i--) {
 149                err = mmc_wait_for_cmd(host, &cmd, 0);
 150                if (err)
 151                        break;
 152
 153                /* if we're just probing, do a single pass */
 154                if (ocr == 0)
 155                        break;
 156
 157                /* otherwise wait until reset completes */
 158                if (mmc_host_is_spi(host)) {
 159                        if (!(cmd.resp[0] & R1_SPI_IDLE))
 160                                break;
 161                } else {
 162                        if (cmd.resp[0] & MMC_CARD_BUSY)
 163                                break;
 164                }
 165
 166                err = -ETIMEDOUT;
 167
 168                mmc_delay(10);
 169        }
 170
 171        if (rocr && !mmc_host_is_spi(host))
 172                *rocr = cmd.resp[0];
 173
 174        return err;
 175}
 176
 177int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
 178{
 179        int err;
 180        struct mmc_command cmd;
 181
 182        BUG_ON(!host);
 183        BUG_ON(!cid);
 184
 185        memset(&cmd, 0, sizeof(struct mmc_command));
 186
 187        cmd.opcode = MMC_ALL_SEND_CID;
 188        cmd.arg = 0;
 189        cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
 190
 191        err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
 192        if (err)
 193                return err;
 194
 195        memcpy(cid, cmd.resp, sizeof(u32) * 4);
 196
 197        return 0;
 198}
 199
 200int mmc_set_relative_addr(struct mmc_card *card)
 201{
 202        int err;
 203        struct mmc_command cmd;
 204
 205        BUG_ON(!card);
 206        BUG_ON(!card->host);
 207
 208        memset(&cmd, 0, sizeof(struct mmc_command));
 209
 210        cmd.opcode = MMC_SET_RELATIVE_ADDR;
 211        cmd.arg = card->rca << 16;
 212        cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 213
 214        err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
 215        if (err)
 216                return err;
 217
 218        return 0;
 219}
 220
 221static int
 222mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
 223{
 224        int err;
 225        struct mmc_command cmd;
 226
 227        BUG_ON(!host);
 228        BUG_ON(!cxd);
 229
 230        memset(&cmd, 0, sizeof(struct mmc_command));
 231
 232        cmd.opcode = opcode;
 233        cmd.arg = arg;
 234        cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
 235
 236        err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
 237        if (err)
 238                return err;
 239
 240        memcpy(cxd, cmd.resp, sizeof(u32) * 4);
 241
 242        return 0;
 243}
 244
 245static int
 246mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
 247                u32 opcode, void *buf, unsigned len)
 248{
 249        struct mmc_request mrq;
 250        struct mmc_command cmd;
 251        struct mmc_data data;
 252        struct scatterlist sg;
 253        void *data_buf;
 254
 255        /* dma onto stack is unsafe/nonportable, but callers to this
 256         * routine normally provide temporary on-stack buffers ...
 257         */
 258        data_buf = kmalloc(len, GFP_KERNEL);
 259        if (data_buf == NULL)
 260                return -ENOMEM;
 261
 262        memset(&mrq, 0, sizeof(struct mmc_request));
 263        memset(&cmd, 0, sizeof(struct mmc_command));
 264        memset(&data, 0, sizeof(struct mmc_data));
 265
 266        mrq.cmd = &cmd;
 267        mrq.data = &data;
 268
 269        cmd.opcode = opcode;
 270        cmd.arg = 0;
 271
 272        /* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
 273         * rely on callers to never use this with "native" calls for reading
 274         * CSD or CID.  Native versions of those commands use the R2 type,
 275         * not R1 plus a data block.
 276         */
 277        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
 278
 279        data.blksz = len;
 280        data.blocks = 1;
 281        data.flags = MMC_DATA_READ;
 282        data.sg = &sg;
 283        data.sg_len = 1;
 284
 285        sg_init_one(&sg, data_buf, len);
 286
 287        if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) {
 288                /*
 289                 * The spec states that CSR and CID accesses have a timeout
 290                 * of 64 clock cycles.
 291                 */
 292                data.timeout_ns = 0;
 293                data.timeout_clks = 64;
 294        } else
 295                mmc_set_data_timeout(&data, card);
 296
 297        mmc_wait_for_req(host, &mrq);
 298
 299        memcpy(buf, data_buf, len);
 300        kfree(data_buf);
 301
 302        if (cmd.error)
 303                return cmd.error;
 304        if (data.error)
 305                return data.error;
 306
 307        return 0;
 308}
 309
 310int mmc_send_csd(struct mmc_card *card, u32 *csd)
 311{
 312        int ret, i;
 313
 314        if (!mmc_host_is_spi(card->host))
 315                return mmc_send_cxd_native(card->host, card->rca << 16,
 316                                csd, MMC_SEND_CSD);
 317
 318        ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
 319        if (ret)
 320                return ret;
 321
 322        for (i = 0;i < 4;i++)
 323                csd[i] = be32_to_cpu(csd[i]);
 324
 325        return 0;
 326}
 327
 328int mmc_send_cid(struct mmc_host *host, u32 *cid)
 329{
 330        int ret, i;
 331
 332        if (!mmc_host_is_spi(host)) {
 333                if (!host->card)
 334                        return -EINVAL;
 335                return mmc_send_cxd_native(host, host->card->rca << 16,
 336                                cid, MMC_SEND_CID);
 337        }
 338
 339        ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
 340        if (ret)
 341                return ret;
 342
 343        for (i = 0;i < 4;i++)
 344                cid[i] = be32_to_cpu(cid[i]);
 345
 346        return 0;
 347}
 348
 349int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
 350{
 351        return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
 352                        ext_csd, 512);
 353}
 354
 355int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
 356{
 357        struct mmc_command cmd;
 358        int err;
 359
 360        memset(&cmd, 0, sizeof(struct mmc_command));
 361
 362        cmd.opcode = MMC_SPI_READ_OCR;
 363        cmd.arg = highcap ? (1 << 30) : 0;
 364        cmd.flags = MMC_RSP_SPI_R3;
 365
 366        err = mmc_wait_for_cmd(host, &cmd, 0);
 367
 368        *ocrp = cmd.resp[1];
 369        return err;
 370}
 371
 372int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
 373{
 374        struct mmc_command cmd;
 375        int err;
 376
 377        memset(&cmd, 0, sizeof(struct mmc_command));
 378
 379        cmd.opcode = MMC_SPI_CRC_ON_OFF;
 380        cmd.flags = MMC_RSP_SPI_R1;
 381        cmd.arg = use_crc;
 382
 383        err = mmc_wait_for_cmd(host, &cmd, 0);
 384        if (!err)
 385                host->use_spi_crc = use_crc;
 386        return err;
 387}
 388
 389int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
 390{
 391        int err;
 392        struct mmc_command cmd;
 393        u32 status;
 394
 395        BUG_ON(!card);
 396        BUG_ON(!card->host);
 397
 398        memset(&cmd, 0, sizeof(struct mmc_command));
 399
 400        cmd.opcode = MMC_SWITCH;
 401        cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
 402                  (index << 16) |
 403                  (value << 8) |
 404                  set;
 405        cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
 406
 407        err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
 408        if (err)
 409                return err;
 410
 411        /* Must check status to be sure of no errors */
 412        do {
 413                err = mmc_send_status(card, &status);
 414                if (err)
 415                        return err;
 416                if (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)
 417                        break;
 418                if (mmc_host_is_spi(card->host))
 419                        break;
 420        } while (R1_CURRENT_STATE(status) == 7);
 421
 422        if (mmc_host_is_spi(card->host)) {
 423                if (status & R1_SPI_ILLEGAL_COMMAND)
 424                        return -EBADMSG;
 425        } else {
 426                if (status & 0xFDFFA000)
 427                        printk(KERN_WARNING "%s: unexpected status %#x after "
 428                               "switch", mmc_hostname(card->host), status);
 429                if (status & R1_SWITCH_ERROR)
 430                        return -EBADMSG;
 431        }
 432
 433        return 0;
 434}
 435
 436int mmc_send_status(struct mmc_card *card, u32 *status)
 437{
 438        int err;
 439        struct mmc_command cmd;
 440
 441        BUG_ON(!card);
 442        BUG_ON(!card->host);
 443
 444        memset(&cmd, 0, sizeof(struct mmc_command));
 445
 446        cmd.opcode = MMC_SEND_STATUS;
 447        if (!mmc_host_is_spi(card->host))
 448                cmd.arg = card->rca << 16;
 449        cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
 450
 451        err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
 452        if (err)
 453                return err;
 454
 455        /* NOTE: callers are required to understand the difference
 456         * between "native" and SPI format status words!
 457         */
 458        if (status)
 459                *status = cmd.resp[0];
 460
 461        return 0;
 462}
 463
 464