linux/drivers/mmc/core/core.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/mmc/core/core.c
   3 *
   4 *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
   5 *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
   6 *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
   7 *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License version 2 as
  11 * published by the Free Software Foundation.
  12 */
  13#include <linux/module.h>
  14#include <linux/init.h>
  15#include <linux/interrupt.h>
  16#include <linux/completion.h>
  17#include <linux/device.h>
  18#include <linux/delay.h>
  19#include <linux/pagemap.h>
  20#include <linux/err.h>
  21#include <linux/leds.h>
  22#include <linux/scatterlist.h>
  23#include <linux/log2.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/pm_runtime.h>
  26#include <linux/pm_wakeup.h>
  27#include <linux/suspend.h>
  28#include <linux/fault-inject.h>
  29#include <linux/random.h>
  30#include <linux/slab.h>
  31#include <linux/of.h>
  32
  33#include <linux/mmc/card.h>
  34#include <linux/mmc/host.h>
  35#include <linux/mmc/mmc.h>
  36#include <linux/mmc/sd.h>
  37#include <linux/mmc/slot-gpio.h>
  38
  39#include "core.h"
  40#include "bus.h"
  41#include "host.h"
  42#include "sdio_bus.h"
  43#include "pwrseq.h"
  44
  45#include "mmc_ops.h"
  46#include "sd_ops.h"
  47#include "sdio_ops.h"
  48
  49/* If the device is not responding */
  50#define MMC_CORE_TIMEOUT_MS     (10 * 60 * 1000) /* 10 minute timeout */
  51
  52/*
  53 * Background operations can take a long time, depending on the housekeeping
  54 * operations the card has to perform.
  55 */
  56#define MMC_BKOPS_MAX_TIMEOUT   (4 * 60 * 1000) /* max time to wait in ms */
  57
  58static struct workqueue_struct *workqueue;
  59static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  60
  61/*
  62 * Enabling software CRCs on the data blocks can be a significant (30%)
  63 * performance cost, and for other reasons may not always be desired.
  64 * So we allow it it to be disabled.
  65 */
  66bool use_spi_crc = 1;
  67module_param(use_spi_crc, bool, 0);
  68
  69/*
  70 * Internal function. Schedule delayed work in the MMC work queue.
  71 */
  72static int mmc_schedule_delayed_work(struct delayed_work *work,
  73                                     unsigned long delay)
  74{
  75        return queue_delayed_work(workqueue, work, delay);
  76}
  77
  78/*
  79 * Internal function. Flush all scheduled work from the MMC work queue.
  80 */
  81static void mmc_flush_scheduled_work(void)
  82{
  83        flush_workqueue(workqueue);
  84}
  85
  86#ifdef CONFIG_FAIL_MMC_REQUEST
  87
  88/*
  89 * Internal function. Inject random data errors.
  90 * If mmc_data is NULL no errors are injected.
  91 */
  92static void mmc_should_fail_request(struct mmc_host *host,
  93                                    struct mmc_request *mrq)
  94{
  95        struct mmc_command *cmd = mrq->cmd;
  96        struct mmc_data *data = mrq->data;
  97        static const int data_errors[] = {
  98                -ETIMEDOUT,
  99                -EILSEQ,
 100                -EIO,
 101        };
 102
 103        if (!data)
 104                return;
 105
 106        if (cmd->error || data->error ||
 107            !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
 108                return;
 109
 110        data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
 111        data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
 112}
 113
 114#else /* CONFIG_FAIL_MMC_REQUEST */
 115
 116static inline void mmc_should_fail_request(struct mmc_host *host,
 117                                           struct mmc_request *mrq)
 118{
 119}
 120
 121#endif /* CONFIG_FAIL_MMC_REQUEST */
 122
 123/**
 124 *      mmc_request_done - finish processing an MMC request
 125 *      @host: MMC host which completed request
 126 *      @mrq: MMC request which request
 127 *
 128 *      MMC drivers should call this function when they have completed
 129 *      their processing of a request.
 130 */
 131void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
 132{
 133        struct mmc_command *cmd = mrq->cmd;
 134        int err = cmd->error;
 135
 136        /* Flag re-tuning needed on CRC errors */
 137        if ((cmd->opcode != MMC_SEND_TUNING_BLOCK &&
 138            cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
 139            (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
 140            (mrq->data && mrq->data->error == -EILSEQ) ||
 141            (mrq->stop && mrq->stop->error == -EILSEQ)))
 142                mmc_retune_needed(host);
 143
 144        if (err && cmd->retries && mmc_host_is_spi(host)) {
 145                if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
 146                        cmd->retries = 0;
 147        }
 148
 149        if (err && cmd->retries && !mmc_card_removed(host->card)) {
 150                /*
 151                 * Request starter must handle retries - see
 152                 * mmc_wait_for_req_done().
 153                 */
 154                if (mrq->done)
 155                        mrq->done(mrq);
 156        } else {
 157                mmc_should_fail_request(host, mrq);
 158
 159                led_trigger_event(host->led, LED_OFF);
 160
 161                if (mrq->sbc) {
 162                        pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
 163                                mmc_hostname(host), mrq->sbc->opcode,
 164                                mrq->sbc->error,
 165                                mrq->sbc->resp[0], mrq->sbc->resp[1],
 166                                mrq->sbc->resp[2], mrq->sbc->resp[3]);
 167                }
 168
 169                pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
 170                        mmc_hostname(host), cmd->opcode, err,
 171                        cmd->resp[0], cmd->resp[1],
 172                        cmd->resp[2], cmd->resp[3]);
 173
 174                if (mrq->data) {
 175                        pr_debug("%s:     %d bytes transferred: %d\n",
 176                                mmc_hostname(host),
 177                                mrq->data->bytes_xfered, mrq->data->error);
 178                }
 179
 180                if (mrq->stop) {
 181                        pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
 182                                mmc_hostname(host), mrq->stop->opcode,
 183                                mrq->stop->error,
 184                                mrq->stop->resp[0], mrq->stop->resp[1],
 185                                mrq->stop->resp[2], mrq->stop->resp[3]);
 186                }
 187
 188                if (mrq->done)
 189                        mrq->done(mrq);
 190
 191                mmc_host_clk_release(host);
 192        }
 193}
 194
 195EXPORT_SYMBOL(mmc_request_done);
 196
 197static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
 198{
 199        int err;
 200
 201        /* Assumes host controller has been runtime resumed by mmc_claim_host */
 202        err = mmc_retune(host);
 203        if (err) {
 204                mrq->cmd->error = err;
 205                mmc_request_done(host, mrq);
 206                return;
 207        }
 208
 209        host->ops->request(host, mrq);
 210}
 211
 212static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
 213{
 214#ifdef CONFIG_MMC_DEBUG
 215        unsigned int i, sz;
 216        struct scatterlist *sg;
 217#endif
 218        mmc_retune_hold(host);
 219
 220        if (mmc_card_removed(host->card))
 221                return -ENOMEDIUM;
 222
 223        if (mrq->sbc) {
 224                pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
 225                         mmc_hostname(host), mrq->sbc->opcode,
 226                         mrq->sbc->arg, mrq->sbc->flags);
 227        }
 228
 229        pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
 230                 mmc_hostname(host), mrq->cmd->opcode,
 231                 mrq->cmd->arg, mrq->cmd->flags);
 232
 233        if (mrq->data) {
 234                pr_debug("%s:     blksz %d blocks %d flags %08x "
 235                        "tsac %d ms nsac %d\n",
 236                        mmc_hostname(host), mrq->data->blksz,
 237                        mrq->data->blocks, mrq->data->flags,
 238                        mrq->data->timeout_ns / 1000000,
 239                        mrq->data->timeout_clks);
 240        }
 241
 242        if (mrq->stop) {
 243                pr_debug("%s:     CMD%u arg %08x flags %08x\n",
 244                         mmc_hostname(host), mrq->stop->opcode,
 245                         mrq->stop->arg, mrq->stop->flags);
 246        }
 247
 248        WARN_ON(!host->claimed);
 249
 250        mrq->cmd->error = 0;
 251        mrq->cmd->mrq = mrq;
 252        if (mrq->sbc) {
 253                mrq->sbc->error = 0;
 254                mrq->sbc->mrq = mrq;
 255        }
 256        if (mrq->data) {
 257                BUG_ON(mrq->data->blksz > host->max_blk_size);
 258                BUG_ON(mrq->data->blocks > host->max_blk_count);
 259                BUG_ON(mrq->data->blocks * mrq->data->blksz >
 260                        host->max_req_size);
 261
 262#ifdef CONFIG_MMC_DEBUG
 263                sz = 0;
 264                for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
 265                        sz += sg->length;
 266                BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
 267#endif
 268
 269                mrq->cmd->data = mrq->data;
 270                mrq->data->error = 0;
 271                mrq->data->mrq = mrq;
 272                if (mrq->stop) {
 273                        mrq->data->stop = mrq->stop;
 274                        mrq->stop->error = 0;
 275                        mrq->stop->mrq = mrq;
 276                }
 277        }
 278        mmc_host_clk_hold(host);
 279        led_trigger_event(host->led, LED_FULL);
 280        __mmc_start_request(host, mrq);
 281
 282        return 0;
 283}
 284
 285/**
 286 *      mmc_start_bkops - start BKOPS for supported cards
 287 *      @card: MMC card to start BKOPS
 288 *      @form_exception: A flag to indicate if this function was
 289 *                       called due to an exception raised by the card
 290 *
 291 *      Start background operations whenever requested.
 292 *      When the urgent BKOPS bit is set in a R1 command response
 293 *      then background operations should be started immediately.
 294*/
 295void mmc_start_bkops(struct mmc_card *card, bool from_exception)
 296{
 297        int err;
 298        int timeout;
 299        bool use_busy_signal;
 300
 301        BUG_ON(!card);
 302
 303        if (!card->ext_csd.man_bkops_en || mmc_card_doing_bkops(card))
 304                return;
 305
 306        err = mmc_read_bkops_status(card);
 307        if (err) {
 308                pr_err("%s: Failed to read bkops status: %d\n",
 309                       mmc_hostname(card->host), err);
 310                return;
 311        }
 312
 313        if (!card->ext_csd.raw_bkops_status)
 314                return;
 315
 316        if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
 317            from_exception)
 318                return;
 319
 320        mmc_claim_host(card->host);
 321        if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
 322                timeout = MMC_BKOPS_MAX_TIMEOUT;
 323                use_busy_signal = true;
 324        } else {
 325                timeout = 0;
 326                use_busy_signal = false;
 327        }
 328
 329        mmc_retune_hold(card->host);
 330
 331        err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
 332                        EXT_CSD_BKOPS_START, 1, timeout,
 333                        use_busy_signal, true, false);
 334        if (err) {
 335                pr_warn("%s: Error %d starting bkops\n",
 336                        mmc_hostname(card->host), err);
 337                mmc_retune_release(card->host);
 338                goto out;
 339        }
 340
 341        /*
 342         * For urgent bkops status (LEVEL_2 and more)
 343         * bkops executed synchronously, otherwise
 344         * the operation is in progress
 345         */
 346        if (!use_busy_signal)
 347                mmc_card_set_doing_bkops(card);
 348        else
 349                mmc_retune_release(card->host);
 350out:
 351        mmc_release_host(card->host);
 352}
 353EXPORT_SYMBOL(mmc_start_bkops);
 354
 355/*
 356 * mmc_wait_data_done() - done callback for data request
 357 * @mrq: done data request
 358 *
 359 * Wakes up mmc context, passed as a callback to host controller driver
 360 */
 361static void mmc_wait_data_done(struct mmc_request *mrq)
 362{
 363        struct mmc_context_info *context_info = &mrq->host->context_info;
 364
 365        context_info->is_done_rcv = true;
 366        wake_up_interruptible(&context_info->wait);
 367}
 368
 369static void mmc_wait_done(struct mmc_request *mrq)
 370{
 371        complete(&mrq->completion);
 372}
 373
 374/*
 375 *__mmc_start_data_req() - starts data request
 376 * @host: MMC host to start the request
 377 * @mrq: data request to start
 378 *
 379 * Sets the done callback to be called when request is completed by the card.
 380 * Starts data mmc request execution
 381 */
 382static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
 383{
 384        int err;
 385
 386        mrq->done = mmc_wait_data_done;
 387        mrq->host = host;
 388
 389        err = mmc_start_request(host, mrq);
 390        if (err) {
 391                mrq->cmd->error = err;
 392                mmc_wait_data_done(mrq);
 393        }
 394
 395        return err;
 396}
 397
 398static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
 399{
 400        int err;
 401
 402        init_completion(&mrq->completion);
 403        mrq->done = mmc_wait_done;
 404
 405        err = mmc_start_request(host, mrq);
 406        if (err) {
 407                mrq->cmd->error = err;
 408                complete(&mrq->completion);
 409        }
 410
 411        return err;
 412}
 413
 414/*
 415 * mmc_wait_for_data_req_done() - wait for request completed
 416 * @host: MMC host to prepare the command.
 417 * @mrq: MMC request to wait for
 418 *
 419 * Blocks MMC context till host controller will ack end of data request
 420 * execution or new request notification arrives from the block layer.
 421 * Handles command retries.
 422 *
 423 * Returns enum mmc_blk_status after checking errors.
 424 */
 425static int mmc_wait_for_data_req_done(struct mmc_host *host,
 426                                      struct mmc_request *mrq,
 427                                      struct mmc_async_req *next_req)
 428{
 429        struct mmc_command *cmd;
 430        struct mmc_context_info *context_info = &host->context_info;
 431        int err;
 432        unsigned long flags;
 433
 434        while (1) {
 435                wait_event_interruptible(context_info->wait,
 436                                (context_info->is_done_rcv ||
 437                                 context_info->is_new_req));
 438                spin_lock_irqsave(&context_info->lock, flags);
 439                context_info->is_waiting_last_req = false;
 440                spin_unlock_irqrestore(&context_info->lock, flags);
 441                if (context_info->is_done_rcv) {
 442                        context_info->is_done_rcv = false;
 443                        context_info->is_new_req = false;
 444                        cmd = mrq->cmd;
 445
 446                        if (!cmd->error || !cmd->retries ||
 447                            mmc_card_removed(host->card)) {
 448                                err = host->areq->err_check(host->card,
 449                                                            host->areq);
 450                                break; /* return err */
 451                        } else {
 452                                mmc_retune_recheck(host);
 453                                pr_info("%s: req failed (CMD%u): %d, retrying...\n",
 454                                        mmc_hostname(host),
 455                                        cmd->opcode, cmd->error);
 456                                cmd->retries--;
 457                                cmd->error = 0;
 458                                __mmc_start_request(host, mrq);
 459                                continue; /* wait for done/new event again */
 460                        }
 461                } else if (context_info->is_new_req) {
 462                        context_info->is_new_req = false;
 463                        if (!next_req)
 464                                return MMC_BLK_NEW_REQUEST;
 465                }
 466        }
 467        mmc_retune_release(host);
 468        return err;
 469}
 470
 471static void mmc_wait_for_req_done(struct mmc_host *host,
 472                                  struct mmc_request *mrq)
 473{
 474        struct mmc_command *cmd;
 475
 476        while (1) {
 477                wait_for_completion(&mrq->completion);
 478
 479                cmd = mrq->cmd;
 480
 481                /*
 482                 * If host has timed out waiting for the sanitize
 483                 * to complete, card might be still in programming state
 484                 * so let's try to bring the card out of programming
 485                 * state.
 486                 */
 487                if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
 488                        if (!mmc_interrupt_hpi(host->card)) {
 489                                pr_warn("%s: %s: Interrupted sanitize\n",
 490                                        mmc_hostname(host), __func__);
 491                                cmd->error = 0;
 492                                break;
 493                        } else {
 494                                pr_err("%s: %s: Failed to interrupt sanitize\n",
 495                                       mmc_hostname(host), __func__);
 496                        }
 497                }
 498                if (!cmd->error || !cmd->retries ||
 499                    mmc_card_removed(host->card))
 500                        break;
 501
 502                mmc_retune_recheck(host);
 503
 504                pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
 505                         mmc_hostname(host), cmd->opcode, cmd->error);
 506                cmd->retries--;
 507                cmd->error = 0;
 508                __mmc_start_request(host, mrq);
 509        }
 510
 511        mmc_retune_release(host);
 512}
 513
 514/**
 515 *      mmc_pre_req - Prepare for a new request
 516 *      @host: MMC host to prepare command
 517 *      @mrq: MMC request to prepare for
 518 *      @is_first_req: true if there is no previous started request
 519 *                     that may run in parellel to this call, otherwise false
 520 *
 521 *      mmc_pre_req() is called in prior to mmc_start_req() to let
 522 *      host prepare for the new request. Preparation of a request may be
 523 *      performed while another request is running on the host.
 524 */
 525static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
 526                 bool is_first_req)
 527{
 528        if (host->ops->pre_req) {
 529                mmc_host_clk_hold(host);
 530                host->ops->pre_req(host, mrq, is_first_req);
 531                mmc_host_clk_release(host);
 532        }
 533}
 534
 535/**
 536 *      mmc_post_req - Post process a completed request
 537 *      @host: MMC host to post process command
 538 *      @mrq: MMC request to post process for
 539 *      @err: Error, if non zero, clean up any resources made in pre_req
 540 *
 541 *      Let the host post process a completed request. Post processing of
 542 *      a request may be performed while another reuqest is running.
 543 */
 544static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
 545                         int err)
 546{
 547        if (host->ops->post_req) {
 548                mmc_host_clk_hold(host);
 549                host->ops->post_req(host, mrq, err);
 550                mmc_host_clk_release(host);
 551        }
 552}
 553
 554/**
 555 *      mmc_start_req - start a non-blocking request
 556 *      @host: MMC host to start command
 557 *      @areq: async request to start
 558 *      @error: out parameter returns 0 for success, otherwise non zero
 559 *
 560 *      Start a new MMC custom command request for a host.
 561 *      If there is on ongoing async request wait for completion
 562 *      of that request and start the new one and return.
 563 *      Does not wait for the new request to complete.
 564 *
 565 *      Returns the completed request, NULL in case of none completed.
 566 *      Wait for the an ongoing request (previoulsy started) to complete and
 567 *      return the completed request. If there is no ongoing request, NULL
 568 *      is returned without waiting. NULL is not an error condition.
 569 */
 570struct mmc_async_req *mmc_start_req(struct mmc_host *host,
 571                                    struct mmc_async_req *areq, int *error)
 572{
 573        int err = 0;
 574        int start_err = 0;
 575        struct mmc_async_req *data = host->areq;
 576
 577        /* Prepare a new request */
 578        if (areq)
 579                mmc_pre_req(host, areq->mrq, !host->areq);
 580
 581        if (host->areq) {
 582                err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq);
 583                if (err == MMC_BLK_NEW_REQUEST) {
 584                        if (error)
 585                                *error = err;
 586                        /*
 587                         * The previous request was not completed,
 588                         * nothing to return
 589                         */
 590                        return NULL;
 591                }
 592                /*
 593                 * Check BKOPS urgency for each R1 response
 594                 */
 595                if (host->card && mmc_card_mmc(host->card) &&
 596                    ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
 597                     (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
 598                    (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
 599
 600                        /* Cancel the prepared request */
 601                        if (areq)
 602                                mmc_post_req(host, areq->mrq, -EINVAL);
 603
 604                        mmc_start_bkops(host->card, true);
 605
 606                        /* prepare the request again */
 607                        if (areq)
 608                                mmc_pre_req(host, areq->mrq, !host->areq);
 609                }
 610        }
 611
 612        if (!err && areq)
 613                start_err = __mmc_start_data_req(host, areq->mrq);
 614
 615        if (host->areq)
 616                mmc_post_req(host, host->areq->mrq, 0);
 617
 618         /* Cancel a prepared request if it was not started. */
 619        if ((err || start_err) && areq)
 620                mmc_post_req(host, areq->mrq, -EINVAL);
 621
 622        if (err)
 623                host->areq = NULL;
 624        else
 625                host->areq = areq;
 626
 627        if (error)
 628                *error = err;
 629        return data;
 630}
 631EXPORT_SYMBOL(mmc_start_req);
 632
 633/**
 634 *      mmc_wait_for_req - start a request and wait for completion
 635 *      @host: MMC host to start command
 636 *      @mrq: MMC request to start
 637 *
 638 *      Start a new MMC custom command request for a host, and wait
 639 *      for the command to complete. Does not attempt to parse the
 640 *      response.
 641 */
 642void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
 643{
 644        __mmc_start_req(host, mrq);
 645        mmc_wait_for_req_done(host, mrq);
 646}
 647EXPORT_SYMBOL(mmc_wait_for_req);
 648
 649/**
 650 *      mmc_interrupt_hpi - Issue for High priority Interrupt
 651 *      @card: the MMC card associated with the HPI transfer
 652 *
 653 *      Issued High Priority Interrupt, and check for card status
 654 *      until out-of prg-state.
 655 */
 656int mmc_interrupt_hpi(struct mmc_card *card)
 657{
 658        int err;
 659        u32 status;
 660        unsigned long prg_wait;
 661
 662        BUG_ON(!card);
 663
 664        if (!card->ext_csd.hpi_en) {
 665                pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
 666                return 1;
 667        }
 668
 669        mmc_claim_host(card->host);
 670        err = mmc_send_status(card, &status);
 671        if (err) {
 672                pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
 673                goto out;
 674        }
 675
 676        switch (R1_CURRENT_STATE(status)) {
 677        case R1_STATE_IDLE:
 678        case R1_STATE_READY:
 679        case R1_STATE_STBY:
 680        case R1_STATE_TRAN:
 681                /*
 682                 * In idle and transfer states, HPI is not needed and the caller
 683                 * can issue the next intended command immediately
 684                 */
 685                goto out;
 686        case R1_STATE_PRG:
 687                break;
 688        default:
 689                /* In all other states, it's illegal to issue HPI */
 690                pr_debug("%s: HPI cannot be sent. Card state=%d\n",
 691                        mmc_hostname(card->host), R1_CURRENT_STATE(status));
 692                err = -EINVAL;
 693                goto out;
 694        }
 695
 696        err = mmc_send_hpi_cmd(card, &status);
 697        if (err)
 698                goto out;
 699
 700        prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
 701        do {
 702                err = mmc_send_status(card, &status);
 703
 704                if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
 705                        break;
 706                if (time_after(jiffies, prg_wait))
 707                        err = -ETIMEDOUT;
 708        } while (!err);
 709
 710out:
 711        mmc_release_host(card->host);
 712        return err;
 713}
 714EXPORT_SYMBOL(mmc_interrupt_hpi);
 715
 716/**
 717 *      mmc_wait_for_cmd - start a command and wait for completion
 718 *      @host: MMC host to start command
 719 *      @cmd: MMC command to start
 720 *      @retries: maximum number of retries
 721 *
 722 *      Start a new MMC command for a host, and wait for the command
 723 *      to complete.  Return any error that occurred while the command
 724 *      was executing.  Do not attempt to parse the response.
 725 */
 726int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
 727{
 728        struct mmc_request mrq = {NULL};
 729
 730        WARN_ON(!host->claimed);
 731
 732        memset(cmd->resp, 0, sizeof(cmd->resp));
 733        cmd->retries = retries;
 734
 735        mrq.cmd = cmd;
 736        cmd->data = NULL;
 737
 738        mmc_wait_for_req(host, &mrq);
 739
 740        return cmd->error;
 741}
 742
 743EXPORT_SYMBOL(mmc_wait_for_cmd);
 744
 745/**
 746 *      mmc_stop_bkops - stop ongoing BKOPS
 747 *      @card: MMC card to check BKOPS
 748 *
 749 *      Send HPI command to stop ongoing background operations to
 750 *      allow rapid servicing of foreground operations, e.g. read/
 751 *      writes. Wait until the card comes out of the programming state
 752 *      to avoid errors in servicing read/write requests.
 753 */
 754int mmc_stop_bkops(struct mmc_card *card)
 755{
 756        int err = 0;
 757
 758        BUG_ON(!card);
 759        err = mmc_interrupt_hpi(card);
 760
 761        /*
 762         * If err is EINVAL, we can't issue an HPI.
 763         * It should complete the BKOPS.
 764         */
 765        if (!err || (err == -EINVAL)) {
 766                mmc_card_clr_doing_bkops(card);
 767                mmc_retune_release(card->host);
 768                err = 0;
 769        }
 770
 771        return err;
 772}
 773EXPORT_SYMBOL(mmc_stop_bkops);
 774
 775int mmc_read_bkops_status(struct mmc_card *card)
 776{
 777        int err;
 778        u8 *ext_csd;
 779
 780        mmc_claim_host(card->host);
 781        err = mmc_get_ext_csd(card, &ext_csd);
 782        mmc_release_host(card->host);
 783        if (err)
 784                return err;
 785
 786        card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
 787        card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
 788        kfree(ext_csd);
 789        return 0;
 790}
 791EXPORT_SYMBOL(mmc_read_bkops_status);
 792
 793/**
 794 *      mmc_set_data_timeout - set the timeout for a data command
 795 *      @data: data phase for command
 796 *      @card: the MMC card associated with the data transfer
 797 *
 798 *      Computes the data timeout parameters according to the
 799 *      correct algorithm given the card type.
 800 */
 801void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
 802{
 803        unsigned int mult;
 804
 805        /*
 806         * SDIO cards only define an upper 1 s limit on access.
 807         */
 808        if (mmc_card_sdio(card)) {
 809                data->timeout_ns = 1000000000;
 810                data->timeout_clks = 0;
 811                return;
 812        }
 813
 814        /*
 815         * SD cards use a 100 multiplier rather than 10
 816         */
 817        mult = mmc_card_sd(card) ? 100 : 10;
 818
 819        /*
 820         * Scale up the multiplier (and therefore the timeout) by
 821         * the r2w factor for writes.
 822         */
 823        if (data->flags & MMC_DATA_WRITE)
 824                mult <<= card->csd.r2w_factor;
 825
 826        data->timeout_ns = card->csd.tacc_ns * mult;
 827        data->timeout_clks = card->csd.tacc_clks * mult;
 828
 829        /*
 830         * SD cards also have an upper limit on the timeout.
 831         */
 832        if (mmc_card_sd(card)) {
 833                unsigned int timeout_us, limit_us;
 834
 835                timeout_us = data->timeout_ns / 1000;
 836                if (mmc_host_clk_rate(card->host))
 837                        timeout_us += data->timeout_clks * 1000 /
 838                                (mmc_host_clk_rate(card->host) / 1000);
 839
 840                if (data->flags & MMC_DATA_WRITE)
 841                        /*
 842                         * The MMC spec "It is strongly recommended
 843                         * for hosts to implement more than 500ms
 844                         * timeout value even if the card indicates
 845                         * the 250ms maximum busy length."  Even the
 846                         * previous value of 300ms is known to be
 847                         * insufficient for some cards.
 848                         */
 849                        limit_us = 3000000;
 850                else
 851                        limit_us = 100000;
 852
 853                /*
 854                 * SDHC cards always use these fixed values.
 855                 */
 856                if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
 857                        data->timeout_ns = limit_us * 1000;
 858                        data->timeout_clks = 0;
 859                }
 860
 861                /* assign limit value if invalid */
 862                if (timeout_us == 0)
 863                        data->timeout_ns = limit_us * 1000;
 864        }
 865
 866        /*
 867         * Some cards require longer data read timeout than indicated in CSD.
 868         * Address this by setting the read timeout to a "reasonably high"
 869         * value. For the cards tested, 300ms has proven enough. If necessary,
 870         * this value can be increased if other problematic cards require this.
 871         */
 872        if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
 873                data->timeout_ns = 300000000;
 874                data->timeout_clks = 0;
 875        }
 876
 877        /*
 878         * Some cards need very high timeouts if driven in SPI mode.
 879         * The worst observed timeout was 900ms after writing a
 880         * continuous stream of data until the internal logic
 881         * overflowed.
 882         */
 883        if (mmc_host_is_spi(card->host)) {
 884                if (data->flags & MMC_DATA_WRITE) {
 885                        if (data->timeout_ns < 1000000000)
 886                                data->timeout_ns = 1000000000;  /* 1s */
 887                } else {
 888                        if (data->timeout_ns < 100000000)
 889                                data->timeout_ns =  100000000;  /* 100ms */
 890                }
 891        }
 892}
 893EXPORT_SYMBOL(mmc_set_data_timeout);
 894
 895/**
 896 *      mmc_align_data_size - pads a transfer size to a more optimal value
 897 *      @card: the MMC card associated with the data transfer
 898 *      @sz: original transfer size
 899 *
 900 *      Pads the original data size with a number of extra bytes in
 901 *      order to avoid controller bugs and/or performance hits
 902 *      (e.g. some controllers revert to PIO for certain sizes).
 903 *
 904 *      Returns the improved size, which might be unmodified.
 905 *
 906 *      Note that this function is only relevant when issuing a
 907 *      single scatter gather entry.
 908 */
 909unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
 910{
 911        /*
 912         * FIXME: We don't have a system for the controller to tell
 913         * the core about its problems yet, so for now we just 32-bit
 914         * align the size.
 915         */
 916        sz = ((sz + 3) / 4) * 4;
 917
 918        return sz;
 919}
 920EXPORT_SYMBOL(mmc_align_data_size);
 921
 922/**
 923 *      __mmc_claim_host - exclusively claim a host
 924 *      @host: mmc host to claim
 925 *      @abort: whether or not the operation should be aborted
 926 *
 927 *      Claim a host for a set of operations.  If @abort is non null and
 928 *      dereference a non-zero value then this will return prematurely with
 929 *      that non-zero value without acquiring the lock.  Returns zero
 930 *      with the lock held otherwise.
 931 */
 932int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
 933{
 934        DECLARE_WAITQUEUE(wait, current);
 935        unsigned long flags;
 936        int stop;
 937        bool pm = false;
 938
 939        might_sleep();
 940
 941        add_wait_queue(&host->wq, &wait);
 942        spin_lock_irqsave(&host->lock, flags);
 943        while (1) {
 944                set_current_state(TASK_UNINTERRUPTIBLE);
 945                stop = abort ? atomic_read(abort) : 0;
 946                if (stop || !host->claimed || host->claimer == current)
 947                        break;
 948                spin_unlock_irqrestore(&host->lock, flags);
 949                schedule();
 950                spin_lock_irqsave(&host->lock, flags);
 951        }
 952        set_current_state(TASK_RUNNING);
 953        if (!stop) {
 954                host->claimed = 1;
 955                host->claimer = current;
 956                host->claim_cnt += 1;
 957                if (host->claim_cnt == 1)
 958                        pm = true;
 959        } else
 960                wake_up(&host->wq);
 961        spin_unlock_irqrestore(&host->lock, flags);
 962        remove_wait_queue(&host->wq, &wait);
 963
 964        if (pm)
 965                pm_runtime_get_sync(mmc_dev(host));
 966
 967        return stop;
 968}
 969EXPORT_SYMBOL(__mmc_claim_host);
 970
 971/**
 972 *      mmc_release_host - release a host
 973 *      @host: mmc host to release
 974 *
 975 *      Release a MMC host, allowing others to claim the host
 976 *      for their operations.
 977 */
 978void mmc_release_host(struct mmc_host *host)
 979{
 980        unsigned long flags;
 981
 982        WARN_ON(!host->claimed);
 983
 984        spin_lock_irqsave(&host->lock, flags);
 985        if (--host->claim_cnt) {
 986                /* Release for nested claim */
 987                spin_unlock_irqrestore(&host->lock, flags);
 988        } else {
 989                host->claimed = 0;
 990                host->claimer = NULL;
 991                spin_unlock_irqrestore(&host->lock, flags);
 992                wake_up(&host->wq);
 993                pm_runtime_mark_last_busy(mmc_dev(host));
 994                pm_runtime_put_autosuspend(mmc_dev(host));
 995        }
 996}
 997EXPORT_SYMBOL(mmc_release_host);
 998
 999/*
1000 * This is a helper function, which fetches a runtime pm reference for the
1001 * card device and also claims the host.
1002 */
1003void mmc_get_card(struct mmc_card *card)
1004{
1005        pm_runtime_get_sync(&card->dev);
1006        mmc_claim_host(card->host);
1007}
1008EXPORT_SYMBOL(mmc_get_card);
1009
1010/*
1011 * This is a helper function, which releases the host and drops the runtime
1012 * pm reference for the card device.
1013 */
1014void mmc_put_card(struct mmc_card *card)
1015{
1016        mmc_release_host(card->host);
1017        pm_runtime_mark_last_busy(&card->dev);
1018        pm_runtime_put_autosuspend(&card->dev);
1019}
1020EXPORT_SYMBOL(mmc_put_card);
1021
1022/*
1023 * Internal function that does the actual ios call to the host driver,
1024 * optionally printing some debug output.
1025 */
1026static inline void mmc_set_ios(struct mmc_host *host)
1027{
1028        struct mmc_ios *ios = &host->ios;
1029
1030        pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1031                "width %u timing %u\n",
1032                 mmc_hostname(host), ios->clock, ios->bus_mode,
1033                 ios->power_mode, ios->chip_select, ios->vdd,
1034                 ios->bus_width, ios->timing);
1035
1036        if (ios->clock > 0)
1037                mmc_set_ungated(host);
1038        host->ops->set_ios(host, ios);
1039}
1040
1041/*
1042 * Control chip select pin on a host.
1043 */
1044void mmc_set_chip_select(struct mmc_host *host, int mode)
1045{
1046        mmc_host_clk_hold(host);
1047        host->ios.chip_select = mode;
1048        mmc_set_ios(host);
1049        mmc_host_clk_release(host);
1050}
1051
1052/*
1053 * Sets the host clock to the highest possible frequency that
1054 * is below "hz".
1055 */
1056static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
1057{
1058        WARN_ON(hz && hz < host->f_min);
1059
1060        if (hz > host->f_max)
1061                hz = host->f_max;
1062
1063        host->ios.clock = hz;
1064        mmc_set_ios(host);
1065}
1066
1067void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1068{
1069        mmc_host_clk_hold(host);
1070        __mmc_set_clock(host, hz);
1071        mmc_host_clk_release(host);
1072}
1073
1074#ifdef CONFIG_MMC_CLKGATE
1075/*
1076 * This gates the clock by setting it to 0 Hz.
1077 */
1078void mmc_gate_clock(struct mmc_host *host)
1079{
1080        unsigned long flags;
1081
1082        spin_lock_irqsave(&host->clk_lock, flags);
1083        host->clk_old = host->ios.clock;
1084        host->ios.clock = 0;
1085        host->clk_gated = true;
1086        spin_unlock_irqrestore(&host->clk_lock, flags);
1087        mmc_set_ios(host);
1088}
1089
1090/*
1091 * This restores the clock from gating by using the cached
1092 * clock value.
1093 */
1094void mmc_ungate_clock(struct mmc_host *host)
1095{
1096        /*
1097         * We should previously have gated the clock, so the clock shall
1098         * be 0 here! The clock may however be 0 during initialization,
1099         * when some request operations are performed before setting
1100         * the frequency. When ungate is requested in that situation
1101         * we just ignore the call.
1102         */
1103        if (host->clk_old) {
1104                BUG_ON(host->ios.clock);
1105                /* This call will also set host->clk_gated to false */
1106                __mmc_set_clock(host, host->clk_old);
1107        }
1108}
1109
1110void mmc_set_ungated(struct mmc_host *host)
1111{
1112        unsigned long flags;
1113
1114        /*
1115         * We've been given a new frequency while the clock is gated,
1116         * so make sure we regard this as ungating it.
1117         */
1118        spin_lock_irqsave(&host->clk_lock, flags);
1119        host->clk_gated = false;
1120        spin_unlock_irqrestore(&host->clk_lock, flags);
1121}
1122
1123#else
1124void mmc_set_ungated(struct mmc_host *host)
1125{
1126}
1127#endif
1128
1129int mmc_execute_tuning(struct mmc_card *card)
1130{
1131        struct mmc_host *host = card->host;
1132        u32 opcode;
1133        int err;
1134
1135        if (!host->ops->execute_tuning)
1136                return 0;
1137
1138        if (mmc_card_mmc(card))
1139                opcode = MMC_SEND_TUNING_BLOCK_HS200;
1140        else
1141                opcode = MMC_SEND_TUNING_BLOCK;
1142
1143        mmc_host_clk_hold(host);
1144        err = host->ops->execute_tuning(host, opcode);
1145        mmc_host_clk_release(host);
1146
1147        if (err)
1148                pr_err("%s: tuning execution failed\n", mmc_hostname(host));
1149        else
1150                mmc_retune_enable(host);
1151
1152        return err;
1153}
1154
1155/*
1156 * Change the bus mode (open drain/push-pull) of a host.
1157 */
1158void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1159{
1160        mmc_host_clk_hold(host);
1161        host->ios.bus_mode = mode;
1162        mmc_set_ios(host);
1163        mmc_host_clk_release(host);
1164}
1165
1166/*
1167 * Change data bus width of a host.
1168 */
1169void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1170{
1171        mmc_host_clk_hold(host);
1172        host->ios.bus_width = width;
1173        mmc_set_ios(host);
1174        mmc_host_clk_release(host);
1175}
1176
1177/*
1178 * Set initial state after a power cycle or a hw_reset.
1179 */
1180void mmc_set_initial_state(struct mmc_host *host)
1181{
1182        mmc_retune_disable(host);
1183
1184        if (mmc_host_is_spi(host))
1185                host->ios.chip_select = MMC_CS_HIGH;
1186        else
1187                host->ios.chip_select = MMC_CS_DONTCARE;
1188        host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1189        host->ios.bus_width = MMC_BUS_WIDTH_1;
1190        host->ios.timing = MMC_TIMING_LEGACY;
1191        host->ios.drv_type = 0;
1192
1193        mmc_set_ios(host);
1194}
1195
1196/**
1197 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1198 * @vdd:        voltage (mV)
1199 * @low_bits:   prefer low bits in boundary cases
1200 *
1201 * This function returns the OCR bit number according to the provided @vdd
1202 * value. If conversion is not possible a negative errno value returned.
1203 *
1204 * Depending on the @low_bits flag the function prefers low or high OCR bits
1205 * on boundary voltages. For example,
1206 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1207 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1208 *
1209 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1210 */
1211static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1212{
1213        const int max_bit = ilog2(MMC_VDD_35_36);
1214        int bit;
1215
1216        if (vdd < 1650 || vdd > 3600)
1217                return -EINVAL;
1218
1219        if (vdd >= 1650 && vdd <= 1950)
1220                return ilog2(MMC_VDD_165_195);
1221
1222        if (low_bits)
1223                vdd -= 1;
1224
1225        /* Base 2000 mV, step 100 mV, bit's base 8. */
1226        bit = (vdd - 2000) / 100 + 8;
1227        if (bit > max_bit)
1228                return max_bit;
1229        return bit;
1230}
1231
1232/**
1233 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1234 * @vdd_min:    minimum voltage value (mV)
1235 * @vdd_max:    maximum voltage value (mV)
1236 *
1237 * This function returns the OCR mask bits according to the provided @vdd_min
1238 * and @vdd_max values. If conversion is not possible the function returns 0.
1239 *
1240 * Notes wrt boundary cases:
1241 * This function sets the OCR bits for all boundary voltages, for example
1242 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1243 * MMC_VDD_34_35 mask.
1244 */
1245u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1246{
1247        u32 mask = 0;
1248
1249        if (vdd_max < vdd_min)
1250                return 0;
1251
1252        /* Prefer high bits for the boundary vdd_max values. */
1253        vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1254        if (vdd_max < 0)
1255                return 0;
1256
1257        /* Prefer low bits for the boundary vdd_min values. */
1258        vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1259        if (vdd_min < 0)
1260                return 0;
1261
1262        /* Fill the mask, from max bit to min bit. */
1263        while (vdd_max >= vdd_min)
1264                mask |= 1 << vdd_max--;
1265
1266        return mask;
1267}
1268EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1269
1270#ifdef CONFIG_OF
1271
1272/**
1273 * mmc_of_parse_voltage - return mask of supported voltages
1274 * @np: The device node need to be parsed.
1275 * @mask: mask of voltages available for MMC/SD/SDIO
1276 *
1277 * 1. Return zero on success.
1278 * 2. Return negative errno: voltage-range is invalid.
1279 */
1280int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1281{
1282        const u32 *voltage_ranges;
1283        int num_ranges, i;
1284
1285        voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1286        num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
1287        if (!voltage_ranges || !num_ranges) {
1288                pr_info("%s: voltage-ranges unspecified\n", np->full_name);
1289                return -EINVAL;
1290        }
1291
1292        for (i = 0; i < num_ranges; i++) {
1293                const int j = i * 2;
1294                u32 ocr_mask;
1295
1296                ocr_mask = mmc_vddrange_to_ocrmask(
1297                                be32_to_cpu(voltage_ranges[j]),
1298                                be32_to_cpu(voltage_ranges[j + 1]));
1299                if (!ocr_mask) {
1300                        pr_err("%s: voltage-range #%d is invalid\n",
1301                                np->full_name, i);
1302                        return -EINVAL;
1303                }
1304                *mask |= ocr_mask;
1305        }
1306
1307        return 0;
1308}
1309EXPORT_SYMBOL(mmc_of_parse_voltage);
1310
1311#endif /* CONFIG_OF */
1312
1313static int mmc_of_get_func_num(struct device_node *node)
1314{
1315        u32 reg;
1316        int ret;
1317
1318        ret = of_property_read_u32(node, "reg", &reg);
1319        if (ret < 0)
1320                return ret;
1321
1322        return reg;
1323}
1324
1325struct device_node *mmc_of_find_child_device(struct mmc_host *host,
1326                unsigned func_num)
1327{
1328        struct device_node *node;
1329
1330        if (!host->parent || !host->parent->of_node)
1331                return NULL;
1332
1333        for_each_child_of_node(host->parent->of_node, node) {
1334                if (mmc_of_get_func_num(node) == func_num)
1335                        return node;
1336        }
1337
1338        return NULL;
1339}
1340
1341#ifdef CONFIG_REGULATOR
1342
1343/**
1344 * mmc_regulator_get_ocrmask - return mask of supported voltages
1345 * @supply: regulator to use
1346 *
1347 * This returns either a negative errno, or a mask of voltages that
1348 * can be provided to MMC/SD/SDIO devices using the specified voltage
1349 * regulator.  This would normally be called before registering the
1350 * MMC host adapter.
1351 */
1352int mmc_regulator_get_ocrmask(struct regulator *supply)
1353{
1354        int                     result = 0;
1355        int                     count;
1356        int                     i;
1357        int                     vdd_uV;
1358        int                     vdd_mV;
1359
1360        count = regulator_count_voltages(supply);
1361        if (count < 0)
1362                return count;
1363
1364        for (i = 0; i < count; i++) {
1365                vdd_uV = regulator_list_voltage(supply, i);
1366                if (vdd_uV <= 0)
1367                        continue;
1368
1369                vdd_mV = vdd_uV / 1000;
1370                result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1371        }
1372
1373        if (!result) {
1374                vdd_uV = regulator_get_voltage(supply);
1375                if (vdd_uV <= 0)
1376                        return vdd_uV;
1377
1378                vdd_mV = vdd_uV / 1000;
1379                result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1380        }
1381
1382        return result;
1383}
1384EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
1385
1386/**
1387 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1388 * @mmc: the host to regulate
1389 * @supply: regulator to use
1390 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1391 *
1392 * Returns zero on success, else negative errno.
1393 *
1394 * MMC host drivers may use this to enable or disable a regulator using
1395 * a particular supply voltage.  This would normally be called from the
1396 * set_ios() method.
1397 */
1398int mmc_regulator_set_ocr(struct mmc_host *mmc,
1399                        struct regulator *supply,
1400                        unsigned short vdd_bit)
1401{
1402        int                     result = 0;
1403        int                     min_uV, max_uV;
1404
1405        if (vdd_bit) {
1406                int             tmp;
1407
1408                /*
1409                 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1410                 * bits this regulator doesn't quite support ... don't
1411                 * be too picky, most cards and regulators are OK with
1412                 * a 0.1V range goof (it's a small error percentage).
1413                 */
1414                tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1415                if (tmp == 0) {
1416                        min_uV = 1650 * 1000;
1417                        max_uV = 1950 * 1000;
1418                } else {
1419                        min_uV = 1900 * 1000 + tmp * 100 * 1000;
1420                        max_uV = min_uV + 100 * 1000;
1421                }
1422
1423                result = regulator_set_voltage(supply, min_uV, max_uV);
1424                if (result == 0 && !mmc->regulator_enabled) {
1425                        result = regulator_enable(supply);
1426                        if (!result)
1427                                mmc->regulator_enabled = true;
1428                }
1429        } else if (mmc->regulator_enabled) {
1430                result = regulator_disable(supply);
1431                if (result == 0)
1432                        mmc->regulator_enabled = false;
1433        }
1434
1435        if (result)
1436                dev_err(mmc_dev(mmc),
1437                        "could not set regulator OCR (%d)\n", result);
1438        return result;
1439}
1440EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
1441
1442#endif /* CONFIG_REGULATOR */
1443
1444int mmc_regulator_get_supply(struct mmc_host *mmc)
1445{
1446        struct device *dev = mmc_dev(mmc);
1447        int ret;
1448
1449        mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
1450        mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
1451
1452        if (IS_ERR(mmc->supply.vmmc)) {
1453                if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
1454                        return -EPROBE_DEFER;
1455                dev_info(dev, "No vmmc regulator found\n");
1456        } else {
1457                ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
1458                if (ret > 0)
1459                        mmc->ocr_avail = ret;
1460                else
1461                        dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
1462        }
1463
1464        if (IS_ERR(mmc->supply.vqmmc)) {
1465                if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
1466                        return -EPROBE_DEFER;
1467                dev_info(dev, "No vqmmc regulator found\n");
1468        }
1469
1470        return 0;
1471}
1472EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1473
1474/*
1475 * Mask off any voltages we don't support and select
1476 * the lowest voltage
1477 */
1478u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1479{
1480        int bit;
1481
1482        /*
1483         * Sanity check the voltages that the card claims to
1484         * support.
1485         */
1486        if (ocr & 0x7F) {
1487                dev_warn(mmc_dev(host),
1488                "card claims to support voltages below defined range\n");
1489                ocr &= ~0x7F;
1490        }
1491
1492        ocr &= host->ocr_avail;
1493        if (!ocr) {
1494                dev_warn(mmc_dev(host), "no support for card's volts\n");
1495                return 0;
1496        }
1497
1498        if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
1499                bit = ffs(ocr) - 1;
1500                ocr &= 3 << bit;
1501                mmc_power_cycle(host, ocr);
1502        } else {
1503                bit = fls(ocr) - 1;
1504                ocr &= 3 << bit;
1505                if (bit != host->ios.vdd)
1506                        dev_warn(mmc_dev(host), "exceeding card's volts\n");
1507        }
1508
1509        return ocr;
1510}
1511
1512int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1513{
1514        int err = 0;
1515        int old_signal_voltage = host->ios.signal_voltage;
1516
1517        host->ios.signal_voltage = signal_voltage;
1518        if (host->ops->start_signal_voltage_switch) {
1519                mmc_host_clk_hold(host);
1520                err = host->ops->start_signal_voltage_switch(host, &host->ios);
1521                mmc_host_clk_release(host);
1522        }
1523
1524        if (err)
1525                host->ios.signal_voltage = old_signal_voltage;
1526
1527        return err;
1528
1529}
1530
1531int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
1532{
1533        struct mmc_command cmd = {0};
1534        int err = 0;
1535        u32 clock;
1536
1537        BUG_ON(!host);
1538
1539        /*
1540         * Send CMD11 only if the request is to switch the card to
1541         * 1.8V signalling.
1542         */
1543        if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1544                return __mmc_set_signal_voltage(host, signal_voltage);
1545
1546        /*
1547         * If we cannot switch voltages, return failure so the caller
1548         * can continue without UHS mode
1549         */
1550        if (!host->ops->start_signal_voltage_switch)
1551                return -EPERM;
1552        if (!host->ops->card_busy)
1553                pr_warn("%s: cannot verify signal voltage switch\n",
1554                        mmc_hostname(host));
1555
1556        mmc_host_clk_hold(host);
1557
1558        cmd.opcode = SD_SWITCH_VOLTAGE;
1559        cmd.arg = 0;
1560        cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1561
1562        err = mmc_wait_for_cmd(host, &cmd, 0);
1563        if (err)
1564                goto err_command;
1565
1566        if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) {
1567                err = -EIO;
1568                goto err_command;
1569        }
1570        /*
1571         * The card should drive cmd and dat[0:3] low immediately
1572         * after the response of cmd11, but wait 1 ms to be sure
1573         */
1574        mmc_delay(1);
1575        if (host->ops->card_busy && !host->ops->card_busy(host)) {
1576                err = -EAGAIN;
1577                goto power_cycle;
1578        }
1579        /*
1580         * During a signal voltage level switch, the clock must be gated
1581         * for 5 ms according to the SD spec
1582         */
1583        clock = host->ios.clock;
1584        host->ios.clock = 0;
1585        mmc_set_ios(host);
1586
1587        if (__mmc_set_signal_voltage(host, signal_voltage)) {
1588                /*
1589                 * Voltages may not have been switched, but we've already
1590                 * sent CMD11, so a power cycle is required anyway
1591                 */
1592                err = -EAGAIN;
1593                goto power_cycle;
1594        }
1595
1596        /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
1597        mmc_delay(10);
1598        host->ios.clock = clock;
1599        mmc_set_ios(host);
1600
1601        /* Wait for at least 1 ms according to spec */
1602        mmc_delay(1);
1603
1604        /*
1605         * Failure to switch is indicated by the card holding
1606         * dat[0:3] low
1607         */
1608        if (host->ops->card_busy && host->ops->card_busy(host))
1609                err = -EAGAIN;
1610
1611power_cycle:
1612        if (err) {
1613                pr_debug("%s: Signal voltage switch failed, "
1614                        "power cycling card\n", mmc_hostname(host));
1615                mmc_power_cycle(host, ocr);
1616        }
1617
1618err_command:
1619        mmc_host_clk_release(host);
1620
1621        return err;
1622}
1623
1624/*
1625 * Select timing parameters for host.
1626 */
1627void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1628{
1629        mmc_host_clk_hold(host);
1630        host->ios.timing = timing;
1631        mmc_set_ios(host);
1632        mmc_host_clk_release(host);
1633}
1634
1635/*
1636 * Select appropriate driver type for host.
1637 */
1638void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1639{
1640        mmc_host_clk_hold(host);
1641        host->ios.drv_type = drv_type;
1642        mmc_set_ios(host);
1643        mmc_host_clk_release(host);
1644}
1645
1646int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
1647                              int card_drv_type, int *drv_type)
1648{
1649        struct mmc_host *host = card->host;
1650        int host_drv_type = SD_DRIVER_TYPE_B;
1651        int drive_strength;
1652
1653        *drv_type = 0;
1654
1655        if (!host->ops->select_drive_strength)
1656                return 0;
1657
1658        /* Use SD definition of driver strength for hosts */
1659        if (host->caps & MMC_CAP_DRIVER_TYPE_A)
1660                host_drv_type |= SD_DRIVER_TYPE_A;
1661
1662        if (host->caps & MMC_CAP_DRIVER_TYPE_C)
1663                host_drv_type |= SD_DRIVER_TYPE_C;
1664
1665        if (host->caps & MMC_CAP_DRIVER_TYPE_D)
1666                host_drv_type |= SD_DRIVER_TYPE_D;
1667
1668        /*
1669         * The drive strength that the hardware can support
1670         * depends on the board design.  Pass the appropriate
1671         * information and let the hardware specific code
1672         * return what is possible given the options
1673         */
1674        mmc_host_clk_hold(host);
1675        drive_strength = host->ops->select_drive_strength(card, max_dtr,
1676                                                          host_drv_type,
1677                                                          card_drv_type,
1678                                                          drv_type);
1679        mmc_host_clk_release(host);
1680
1681        return drive_strength;
1682}
1683
1684/*
1685 * Apply power to the MMC stack.  This is a two-stage process.
1686 * First, we enable power to the card without the clock running.
1687 * We then wait a bit for the power to stabilise.  Finally,
1688 * enable the bus drivers and clock to the card.
1689 *
1690 * We must _NOT_ enable the clock prior to power stablising.
1691 *
1692 * If a host does all the power sequencing itself, ignore the
1693 * initial MMC_POWER_UP stage.
1694 */
1695void mmc_power_up(struct mmc_host *host, u32 ocr)
1696{
1697        if (host->ios.power_mode == MMC_POWER_ON)
1698                return;
1699
1700        mmc_host_clk_hold(host);
1701
1702        mmc_pwrseq_pre_power_on(host);
1703
1704        host->ios.vdd = fls(ocr) - 1;
1705        host->ios.power_mode = MMC_POWER_UP;
1706        /* Set initial state and call mmc_set_ios */
1707        mmc_set_initial_state(host);
1708
1709        /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
1710        if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330) == 0)
1711                dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
1712        else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180) == 0)
1713                dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
1714        else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120) == 0)
1715                dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
1716
1717        /*
1718         * This delay should be sufficient to allow the power supply
1719         * to reach the minimum voltage.
1720         */
1721        mmc_delay(10);
1722
1723        mmc_pwrseq_post_power_on(host);
1724
1725        host->ios.clock = host->f_init;
1726
1727        host->ios.power_mode = MMC_POWER_ON;
1728        mmc_set_ios(host);
1729
1730        /*
1731         * This delay must be at least 74 clock sizes, or 1 ms, or the
1732         * time required to reach a stable voltage.
1733         */
1734        mmc_delay(10);
1735
1736        mmc_host_clk_release(host);
1737}
1738
1739void mmc_power_off(struct mmc_host *host)
1740{
1741        if (host->ios.power_mode == MMC_POWER_OFF)
1742                return;
1743
1744        mmc_host_clk_hold(host);
1745
1746        mmc_pwrseq_power_off(host);
1747
1748        host->ios.clock = 0;
1749        host->ios.vdd = 0;
1750
1751        host->ios.power_mode = MMC_POWER_OFF;
1752        /* Set initial state and call mmc_set_ios */
1753        mmc_set_initial_state(host);
1754
1755        /*
1756         * Some configurations, such as the 802.11 SDIO card in the OLPC
1757         * XO-1.5, require a short delay after poweroff before the card
1758         * can be successfully turned on again.
1759         */
1760        mmc_delay(1);
1761
1762        mmc_host_clk_release(host);
1763}
1764
1765void mmc_power_cycle(struct mmc_host *host, u32 ocr)
1766{
1767        mmc_power_off(host);
1768        /* Wait at least 1 ms according to SD spec */
1769        mmc_delay(1);
1770        mmc_power_up(host, ocr);
1771}
1772
1773/*
1774 * Cleanup when the last reference to the bus operator is dropped.
1775 */
1776static void __mmc_release_bus(struct mmc_host *host)
1777{
1778        BUG_ON(!host);
1779        BUG_ON(host->bus_refs);
1780        BUG_ON(!host->bus_dead);
1781
1782        host->bus_ops = NULL;
1783}
1784
1785/*
1786 * Increase reference count of bus operator
1787 */
1788static inline void mmc_bus_get(struct mmc_host *host)
1789{
1790        unsigned long flags;
1791
1792        spin_lock_irqsave(&host->lock, flags);
1793        host->bus_refs++;
1794        spin_unlock_irqrestore(&host->lock, flags);
1795}
1796
1797/*
1798 * Decrease reference count of bus operator and free it if
1799 * it is the last reference.
1800 */
1801static inline void mmc_bus_put(struct mmc_host *host)
1802{
1803        unsigned long flags;
1804
1805        spin_lock_irqsave(&host->lock, flags);
1806        host->bus_refs--;
1807        if ((host->bus_refs == 0) && host->bus_ops)
1808                __mmc_release_bus(host);
1809        spin_unlock_irqrestore(&host->lock, flags);
1810}
1811
1812/*
1813 * Assign a mmc bus handler to a host. Only one bus handler may control a
1814 * host at any given time.
1815 */
1816void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1817{
1818        unsigned long flags;
1819
1820        BUG_ON(!host);
1821        BUG_ON(!ops);
1822
1823        WARN_ON(!host->claimed);
1824
1825        spin_lock_irqsave(&host->lock, flags);
1826
1827        BUG_ON(host->bus_ops);
1828        BUG_ON(host->bus_refs);
1829
1830        host->bus_ops = ops;
1831        host->bus_refs = 1;
1832        host->bus_dead = 0;
1833
1834        spin_unlock_irqrestore(&host->lock, flags);
1835}
1836
1837/*
1838 * Remove the current bus handler from a host.
1839 */
1840void mmc_detach_bus(struct mmc_host *host)
1841{
1842        unsigned long flags;
1843
1844        BUG_ON(!host);
1845
1846        WARN_ON(!host->claimed);
1847        WARN_ON(!host->bus_ops);
1848
1849        spin_lock_irqsave(&host->lock, flags);
1850
1851        host->bus_dead = 1;
1852
1853        spin_unlock_irqrestore(&host->lock, flags);
1854
1855        mmc_bus_put(host);
1856}
1857
1858static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
1859                                bool cd_irq)
1860{
1861#ifdef CONFIG_MMC_DEBUG
1862        unsigned long flags;
1863        spin_lock_irqsave(&host->lock, flags);
1864        WARN_ON(host->removed);
1865        spin_unlock_irqrestore(&host->lock, flags);
1866#endif
1867
1868        /*
1869         * If the device is configured as wakeup, we prevent a new sleep for
1870         * 5 s to give provision for user space to consume the event.
1871         */
1872        if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
1873                device_can_wakeup(mmc_dev(host)))
1874                pm_wakeup_event(mmc_dev(host), 5000);
1875
1876        host->detect_change = 1;
1877        mmc_schedule_delayed_work(&host->detect, delay);
1878}
1879
1880/**
1881 *      mmc_detect_change - process change of state on a MMC socket
1882 *      @host: host which changed state.
1883 *      @delay: optional delay to wait before detection (jiffies)
1884 *
1885 *      MMC drivers should call this when they detect a card has been
1886 *      inserted or removed. The MMC layer will confirm that any
1887 *      present card is still functional, and initialize any newly
1888 *      inserted.
1889 */
1890void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1891{
1892        _mmc_detect_change(host, delay, true);
1893}
1894EXPORT_SYMBOL(mmc_detect_change);
1895
1896void mmc_init_erase(struct mmc_card *card)
1897{
1898        unsigned int sz;
1899
1900        if (is_power_of_2(card->erase_size))
1901                card->erase_shift = ffs(card->erase_size) - 1;
1902        else
1903                card->erase_shift = 0;
1904
1905        /*
1906         * It is possible to erase an arbitrarily large area of an SD or MMC
1907         * card.  That is not desirable because it can take a long time
1908         * (minutes) potentially delaying more important I/O, and also the
1909         * timeout calculations become increasingly hugely over-estimated.
1910         * Consequently, 'pref_erase' is defined as a guide to limit erases
1911         * to that size and alignment.
1912         *
1913         * For SD cards that define Allocation Unit size, limit erases to one
1914         * Allocation Unit at a time.  For MMC cards that define High Capacity
1915         * Erase Size, whether it is switched on or not, limit to that size.
1916         * Otherwise just have a stab at a good value.  For modern cards it
1917         * will end up being 4MiB.  Note that if the value is too small, it
1918         * can end up taking longer to erase.
1919         */
1920        if (mmc_card_sd(card) && card->ssr.au) {
1921                card->pref_erase = card->ssr.au;
1922                card->erase_shift = ffs(card->ssr.au) - 1;
1923        } else if (card->ext_csd.hc_erase_size) {
1924                card->pref_erase = card->ext_csd.hc_erase_size;
1925        } else if (card->erase_size) {
1926                sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1927                if (sz < 128)
1928                        card->pref_erase = 512 * 1024 / 512;
1929                else if (sz < 512)
1930                        card->pref_erase = 1024 * 1024 / 512;
1931                else if (sz < 1024)
1932                        card->pref_erase = 2 * 1024 * 1024 / 512;
1933                else
1934                        card->pref_erase = 4 * 1024 * 1024 / 512;
1935                if (card->pref_erase < card->erase_size)
1936                        card->pref_erase = card->erase_size;
1937                else {
1938                        sz = card->pref_erase % card->erase_size;
1939                        if (sz)
1940                                card->pref_erase += card->erase_size - sz;
1941                }
1942        } else
1943                card->pref_erase = 0;
1944}
1945
1946static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1947                                          unsigned int arg, unsigned int qty)
1948{
1949        unsigned int erase_timeout;
1950
1951        if (arg == MMC_DISCARD_ARG ||
1952            (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1953                erase_timeout = card->ext_csd.trim_timeout;
1954        } else if (card->ext_csd.erase_group_def & 1) {
1955                /* High Capacity Erase Group Size uses HC timeouts */
1956                if (arg == MMC_TRIM_ARG)
1957                        erase_timeout = card->ext_csd.trim_timeout;
1958                else
1959                        erase_timeout = card->ext_csd.hc_erase_timeout;
1960        } else {
1961                /* CSD Erase Group Size uses write timeout */
1962                unsigned int mult = (10 << card->csd.r2w_factor);
1963                unsigned int timeout_clks = card->csd.tacc_clks * mult;
1964                unsigned int timeout_us;
1965
1966                /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1967                if (card->csd.tacc_ns < 1000000)
1968                        timeout_us = (card->csd.tacc_ns * mult) / 1000;
1969                else
1970                        timeout_us = (card->csd.tacc_ns / 1000) * mult;
1971
1972                /*
1973                 * ios.clock is only a target.  The real clock rate might be
1974                 * less but not that much less, so fudge it by multiplying by 2.
1975                 */
1976                timeout_clks <<= 1;
1977                timeout_us += (timeout_clks * 1000) /
1978                              (mmc_host_clk_rate(card->host) / 1000);
1979
1980                erase_timeout = timeout_us / 1000;
1981
1982                /*
1983                 * Theoretically, the calculation could underflow so round up
1984                 * to 1ms in that case.
1985                 */
1986                if (!erase_timeout)
1987                        erase_timeout = 1;
1988        }
1989
1990        /* Multiplier for secure operations */
1991        if (arg & MMC_SECURE_ARGS) {
1992                if (arg == MMC_SECURE_ERASE_ARG)
1993                        erase_timeout *= card->ext_csd.sec_erase_mult;
1994                else
1995                        erase_timeout *= card->ext_csd.sec_trim_mult;
1996        }
1997
1998        erase_timeout *= qty;
1999
2000        /*
2001         * Ensure at least a 1 second timeout for SPI as per
2002         * 'mmc_set_data_timeout()'
2003         */
2004        if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
2005                erase_timeout = 1000;
2006
2007        return erase_timeout;
2008}
2009
2010static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
2011                                         unsigned int arg,
2012                                         unsigned int qty)
2013{
2014        unsigned int erase_timeout;
2015
2016        if (card->ssr.erase_timeout) {
2017                /* Erase timeout specified in SD Status Register (SSR) */
2018                erase_timeout = card->ssr.erase_timeout * qty +
2019                                card->ssr.erase_offset;
2020        } else {
2021                /*
2022                 * Erase timeout not specified in SD Status Register (SSR) so
2023                 * use 250ms per write block.
2024                 */
2025                erase_timeout = 250 * qty;
2026        }
2027
2028        /* Must not be less than 1 second */
2029        if (erase_timeout < 1000)
2030                erase_timeout = 1000;
2031
2032        return erase_timeout;
2033}
2034
2035static unsigned int mmc_erase_timeout(struct mmc_card *card,
2036                                      unsigned int arg,
2037                                      unsigned int qty)
2038{
2039        if (mmc_card_sd(card))
2040                return mmc_sd_erase_timeout(card, arg, qty);
2041        else
2042                return mmc_mmc_erase_timeout(card, arg, qty);
2043}
2044
2045static int mmc_do_erase(struct mmc_card *card, unsigned int from,
2046                        unsigned int to, unsigned int arg)
2047{
2048        struct mmc_command cmd = {0};
2049        unsigned int qty = 0;
2050        unsigned long timeout;
2051        int err;
2052
2053        mmc_retune_hold(card->host);
2054
2055        /*
2056         * qty is used to calculate the erase timeout which depends on how many
2057         * erase groups (or allocation units in SD terminology) are affected.
2058         * We count erasing part of an erase group as one erase group.
2059         * For SD, the allocation units are always a power of 2.  For MMC, the
2060         * erase group size is almost certainly also power of 2, but it does not
2061         * seem to insist on that in the JEDEC standard, so we fall back to
2062         * division in that case.  SD may not specify an allocation unit size,
2063         * in which case the timeout is based on the number of write blocks.
2064         *
2065         * Note that the timeout for secure trim 2 will only be correct if the
2066         * number of erase groups specified is the same as the total of all
2067         * preceding secure trim 1 commands.  Since the power may have been
2068         * lost since the secure trim 1 commands occurred, it is generally
2069         * impossible to calculate the secure trim 2 timeout correctly.
2070         */
2071        if (card->erase_shift)
2072                qty += ((to >> card->erase_shift) -
2073                        (from >> card->erase_shift)) + 1;
2074        else if (mmc_card_sd(card))
2075                qty += to - from + 1;
2076        else
2077                qty += ((to / card->erase_size) -
2078                        (from / card->erase_size)) + 1;
2079
2080        if (!mmc_card_blockaddr(card)) {
2081                from <<= 9;
2082                to <<= 9;
2083        }
2084
2085        if (mmc_card_sd(card))
2086                cmd.opcode = SD_ERASE_WR_BLK_START;
2087        else
2088                cmd.opcode = MMC_ERASE_GROUP_START;
2089        cmd.arg = from;
2090        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2091        err = mmc_wait_for_cmd(card->host, &cmd, 0);
2092        if (err) {
2093                pr_err("mmc_erase: group start error %d, "
2094                       "status %#x\n", err, cmd.resp[0]);
2095                err = -EIO;
2096                goto out;
2097        }
2098
2099        memset(&cmd, 0, sizeof(struct mmc_command));
2100        if (mmc_card_sd(card))
2101                cmd.opcode = SD_ERASE_WR_BLK_END;
2102        else
2103                cmd.opcode = MMC_ERASE_GROUP_END;
2104        cmd.arg = to;
2105        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2106        err = mmc_wait_for_cmd(card->host, &cmd, 0);
2107        if (err) {
2108                pr_err("mmc_erase: group end error %d, status %#x\n",
2109                       err, cmd.resp[0]);
2110                err = -EIO;
2111                goto out;
2112        }
2113
2114        memset(&cmd, 0, sizeof(struct mmc_command));
2115        cmd.opcode = MMC_ERASE;
2116        cmd.arg = arg;
2117        cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2118        cmd.busy_timeout = mmc_erase_timeout(card, arg, qty);
2119        err = mmc_wait_for_cmd(card->host, &cmd, 0);
2120        if (err) {
2121                pr_err("mmc_erase: erase error %d, status %#x\n",
2122                       err, cmd.resp[0]);
2123                err = -EIO;
2124                goto out;
2125        }
2126
2127        if (mmc_host_is_spi(card->host))
2128                goto out;
2129
2130        timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
2131        do {
2132                memset(&cmd, 0, sizeof(struct mmc_command));
2133                cmd.opcode = MMC_SEND_STATUS;
2134                cmd.arg = card->rca << 16;
2135                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2136                /* Do not retry else we can't see errors */
2137                err = mmc_wait_for_cmd(card->host, &cmd, 0);
2138                if (err || (cmd.resp[0] & 0xFDF92000)) {
2139                        pr_err("error %d requesting status %#x\n",
2140                                err, cmd.resp[0]);
2141                        err = -EIO;
2142                        goto out;
2143                }
2144
2145                /* Timeout if the device never becomes ready for data and
2146                 * never leaves the program state.
2147                 */
2148                if (time_after(jiffies, timeout)) {
2149                        pr_err("%s: Card stuck in programming state! %s\n",
2150                                mmc_hostname(card->host), __func__);
2151                        err =  -EIO;
2152                        goto out;
2153                }
2154
2155        } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
2156                 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
2157out:
2158        mmc_retune_release(card->host);
2159        return err;
2160}
2161
2162/**
2163 * mmc_erase - erase sectors.
2164 * @card: card to erase
2165 * @from: first sector to erase
2166 * @nr: number of sectors to erase
2167 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2168 *
2169 * Caller must claim host before calling this function.
2170 */
2171int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2172              unsigned int arg)
2173{
2174        unsigned int rem, to = from + nr;
2175        int err;
2176
2177        if (!(card->host->caps & MMC_CAP_ERASE) ||
2178            !(card->csd.cmdclass & CCC_ERASE))
2179                return -EOPNOTSUPP;
2180
2181        if (!card->erase_size)
2182                return -EOPNOTSUPP;
2183
2184        if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2185                return -EOPNOTSUPP;
2186
2187        if ((arg & MMC_SECURE_ARGS) &&
2188            !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2189                return -EOPNOTSUPP;
2190
2191        if ((arg & MMC_TRIM_ARGS) &&
2192            !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2193                return -EOPNOTSUPP;
2194
2195        if (arg == MMC_SECURE_ERASE_ARG) {
2196                if (from % card->erase_size || nr % card->erase_size)
2197                        return -EINVAL;
2198        }
2199
2200        if (arg == MMC_ERASE_ARG) {
2201                rem = from % card->erase_size;
2202                if (rem) {
2203                        rem = card->erase_size - rem;
2204                        from += rem;
2205                        if (nr > rem)
2206                                nr -= rem;
2207                        else
2208                                return 0;
2209                }
2210                rem = nr % card->erase_size;
2211                if (rem)
2212                        nr -= rem;
2213        }
2214
2215        if (nr == 0)
2216                return 0;
2217
2218        to = from + nr;
2219
2220        if (to <= from)
2221                return -EINVAL;
2222
2223        /* 'from' and 'to' are inclusive */
2224        to -= 1;
2225
2226        /*
2227         * Special case where only one erase-group fits in the timeout budget:
2228         * If the region crosses an erase-group boundary on this particular
2229         * case, we will be trimming more than one erase-group which, does not
2230         * fit in the timeout budget of the controller, so we need to split it
2231         * and call mmc_do_erase() twice if necessary. This special case is
2232         * identified by the card->eg_boundary flag.
2233         */
2234        rem = card->erase_size - (from % card->erase_size);
2235        if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
2236                err = mmc_do_erase(card, from, from + rem - 1, arg);
2237                from += rem;
2238                if ((err) || (to <= from))
2239                        return err;
2240        }
2241
2242        return mmc_do_erase(card, from, to, arg);
2243}
2244EXPORT_SYMBOL(mmc_erase);
2245
2246int mmc_can_erase(struct mmc_card *card)
2247{
2248        if ((card->host->caps & MMC_CAP_ERASE) &&
2249            (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2250                return 1;
2251        return 0;
2252}
2253EXPORT_SYMBOL(mmc_can_erase);
2254
2255int mmc_can_trim(struct mmc_card *card)
2256{
2257        if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
2258            (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
2259                return 1;
2260        return 0;
2261}
2262EXPORT_SYMBOL(mmc_can_trim);
2263
2264int mmc_can_discard(struct mmc_card *card)
2265{
2266        /*
2267         * As there's no way to detect the discard support bit at v4.5
2268         * use the s/w feature support filed.
2269         */
2270        if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2271                return 1;
2272        return 0;
2273}
2274EXPORT_SYMBOL(mmc_can_discard);
2275
2276int mmc_can_sanitize(struct mmc_card *card)
2277{
2278        if (!mmc_can_trim(card) && !mmc_can_erase(card))
2279                return 0;
2280        if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2281                return 1;
2282        return 0;
2283}
2284EXPORT_SYMBOL(mmc_can_sanitize);
2285
2286int mmc_can_secure_erase_trim(struct mmc_card *card)
2287{
2288        if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
2289            !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
2290                return 1;
2291        return 0;
2292}
2293EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2294
2295int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2296                            unsigned int nr)
2297{
2298        if (!card->erase_size)
2299                return 0;
2300        if (from % card->erase_size || nr % card->erase_size)
2301                return 0;
2302        return 1;
2303}
2304EXPORT_SYMBOL(mmc_erase_group_aligned);
2305
2306static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2307                                            unsigned int arg)
2308{
2309        struct mmc_host *host = card->host;
2310        unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2311        unsigned int last_timeout = 0;
2312
2313        if (card->erase_shift)
2314                max_qty = UINT_MAX >> card->erase_shift;
2315        else if (mmc_card_sd(card))
2316                max_qty = UINT_MAX;
2317        else
2318                max_qty = UINT_MAX / card->erase_size;
2319
2320        /* Find the largest qty with an OK timeout */
2321        do {
2322                y = 0;
2323                for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2324                        timeout = mmc_erase_timeout(card, arg, qty + x);
2325                        if (timeout > host->max_busy_timeout)
2326                                break;
2327                        if (timeout < last_timeout)
2328                                break;
2329                        last_timeout = timeout;
2330                        y = x;
2331                }
2332                qty += y;
2333        } while (y);
2334
2335        if (!qty)
2336                return 0;
2337
2338        /*
2339         * When specifying a sector range to trim, chances are we might cross
2340         * an erase-group boundary even if the amount of sectors is less than
2341         * one erase-group.
2342         * If we can only fit one erase-group in the controller timeout budget,
2343         * we have to care that erase-group boundaries are not crossed by a
2344         * single trim operation. We flag that special case with "eg_boundary".
2345         * In all other cases we can just decrement qty and pretend that we
2346         * always touch (qty + 1) erase-groups as a simple optimization.
2347         */
2348        if (qty == 1)
2349                card->eg_boundary = 1;
2350        else
2351                qty--;
2352
2353        /* Convert qty to sectors */
2354        if (card->erase_shift)
2355                max_discard = qty << card->erase_shift;
2356        else if (mmc_card_sd(card))
2357                max_discard = qty + 1;
2358        else
2359                max_discard = qty * card->erase_size;
2360
2361        return max_discard;
2362}
2363
2364unsigned int mmc_calc_max_discard(struct mmc_card *card)
2365{
2366        struct mmc_host *host = card->host;
2367        unsigned int max_discard, max_trim;
2368
2369        if (!host->max_busy_timeout)
2370                return UINT_MAX;
2371
2372        /*
2373         * Without erase_group_def set, MMC erase timeout depends on clock
2374         * frequence which can change.  In that case, the best choice is
2375         * just the preferred erase size.
2376         */
2377        if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2378                return card->pref_erase;
2379
2380        max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2381        if (mmc_can_trim(card)) {
2382                max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2383                if (max_trim < max_discard)
2384                        max_discard = max_trim;
2385        } else if (max_discard < card->erase_size) {
2386                max_discard = 0;
2387        }
2388        pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2389                 mmc_hostname(host), max_discard, host->max_busy_timeout);
2390        return max_discard;
2391}
2392EXPORT_SYMBOL(mmc_calc_max_discard);
2393
2394int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2395{
2396        struct mmc_command cmd = {0};
2397
2398        if (mmc_card_blockaddr(card) || mmc_card_ddr52(card))
2399                return 0;
2400
2401        cmd.opcode = MMC_SET_BLOCKLEN;
2402        cmd.arg = blocklen;
2403        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2404        return mmc_wait_for_cmd(card->host, &cmd, 5);
2405}
2406EXPORT_SYMBOL(mmc_set_blocklen);
2407
2408int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2409                        bool is_rel_write)
2410{
2411        struct mmc_command cmd = {0};
2412
2413        cmd.opcode = MMC_SET_BLOCK_COUNT;
2414        cmd.arg = blockcount & 0x0000FFFF;
2415        if (is_rel_write)
2416                cmd.arg |= 1 << 31;
2417        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2418        return mmc_wait_for_cmd(card->host, &cmd, 5);
2419}
2420EXPORT_SYMBOL(mmc_set_blockcount);
2421
2422static void mmc_hw_reset_for_init(struct mmc_host *host)
2423{
2424        if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2425                return;
2426        mmc_host_clk_hold(host);
2427        host->ops->hw_reset(host);
2428        mmc_host_clk_release(host);
2429}
2430
2431int mmc_hw_reset(struct mmc_host *host)
2432{
2433        int ret;
2434
2435        if (!host->card)
2436                return -EINVAL;
2437
2438        mmc_bus_get(host);
2439        if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
2440                mmc_bus_put(host);
2441                return -EOPNOTSUPP;
2442        }
2443
2444        ret = host->bus_ops->reset(host);
2445        mmc_bus_put(host);
2446
2447        if (ret != -EOPNOTSUPP)
2448                pr_warn("%s: tried to reset card\n", mmc_hostname(host));
2449
2450        return ret;
2451}
2452EXPORT_SYMBOL(mmc_hw_reset);
2453
2454static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2455{
2456        host->f_init = freq;
2457
2458#ifdef CONFIG_MMC_DEBUG
2459        pr_info("%s: %s: trying to init card at %u Hz\n",
2460                mmc_hostname(host), __func__, host->f_init);
2461#endif
2462        mmc_power_up(host, host->ocr_avail);
2463
2464        /*
2465         * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2466         * do a hardware reset if possible.
2467         */
2468        mmc_hw_reset_for_init(host);
2469
2470        /*
2471         * sdio_reset sends CMD52 to reset card.  Since we do not know
2472         * if the card is being re-initialized, just send it.  CMD52
2473         * should be ignored by SD/eMMC cards.
2474         */
2475        sdio_reset(host);
2476        mmc_go_idle(host);
2477
2478        mmc_send_if_cond(host, host->ocr_avail);
2479
2480        /* Order's important: probe SDIO, then SD, then MMC */
2481        if (!mmc_attach_sdio(host))
2482                return 0;
2483        if (!mmc_attach_sd(host))
2484                return 0;
2485        if (!mmc_attach_mmc(host))
2486                return 0;
2487
2488        mmc_power_off(host);
2489        return -EIO;
2490}
2491
2492int _mmc_detect_card_removed(struct mmc_host *host)
2493{
2494        int ret;
2495
2496        if (host->caps & MMC_CAP_NONREMOVABLE)
2497                return 0;
2498
2499        if (!host->card || mmc_card_removed(host->card))
2500                return 1;
2501
2502        ret = host->bus_ops->alive(host);
2503
2504        /*
2505         * Card detect status and alive check may be out of sync if card is
2506         * removed slowly, when card detect switch changes while card/slot
2507         * pads are still contacted in hardware (refer to "SD Card Mechanical
2508         * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2509         * detect work 200ms later for this case.
2510         */
2511        if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2512                mmc_detect_change(host, msecs_to_jiffies(200));
2513                pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2514        }
2515
2516        if (ret) {
2517                mmc_card_set_removed(host->card);
2518                pr_debug("%s: card remove detected\n", mmc_hostname(host));
2519        }
2520
2521        return ret;
2522}
2523
2524int mmc_detect_card_removed(struct mmc_host *host)
2525{
2526        struct mmc_card *card = host->card;
2527        int ret;
2528
2529        WARN_ON(!host->claimed);
2530
2531        if (!card)
2532                return 1;
2533
2534        ret = mmc_card_removed(card);
2535        /*
2536         * The card will be considered unchanged unless we have been asked to
2537         * detect a change or host requires polling to provide card detection.
2538         */
2539        if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2540                return ret;
2541
2542        host->detect_change = 0;
2543        if (!ret) {
2544                ret = _mmc_detect_card_removed(host);
2545                if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
2546                        /*
2547                         * Schedule a detect work as soon as possible to let a
2548                         * rescan handle the card removal.
2549                         */
2550                        cancel_delayed_work(&host->detect);
2551                        _mmc_detect_change(host, 0, false);
2552                }
2553        }
2554
2555        return ret;
2556}
2557EXPORT_SYMBOL(mmc_detect_card_removed);
2558
2559void mmc_rescan(struct work_struct *work)
2560{
2561        struct mmc_host *host =
2562                container_of(work, struct mmc_host, detect.work);
2563        int i;
2564
2565        if (host->trigger_card_event && host->ops->card_event) {
2566                host->ops->card_event(host);
2567                host->trigger_card_event = false;
2568        }
2569
2570        if (host->rescan_disable)
2571                return;
2572
2573        /* If there is a non-removable card registered, only scan once */
2574        if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2575                return;
2576        host->rescan_entered = 1;
2577
2578        mmc_bus_get(host);
2579
2580        /*
2581         * if there is a _removable_ card registered, check whether it is
2582         * still present
2583         */
2584        if (host->bus_ops && !host->bus_dead
2585            && !(host->caps & MMC_CAP_NONREMOVABLE))
2586                host->bus_ops->detect(host);
2587
2588        host->detect_change = 0;
2589
2590        /*
2591         * Let mmc_bus_put() free the bus/bus_ops if we've found that
2592         * the card is no longer present.
2593         */
2594        mmc_bus_put(host);
2595        mmc_bus_get(host);
2596
2597        /* if there still is a card present, stop here */
2598        if (host->bus_ops != NULL) {
2599                mmc_bus_put(host);
2600                goto out;
2601        }
2602
2603        /*
2604         * Only we can add a new handler, so it's safe to
2605         * release the lock here.
2606         */
2607        mmc_bus_put(host);
2608
2609        if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
2610                        host->ops->get_cd(host) == 0) {
2611                mmc_claim_host(host);
2612                mmc_power_off(host);
2613                mmc_release_host(host);
2614                goto out;
2615        }
2616
2617        mmc_claim_host(host);
2618        for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2619                if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2620                        break;
2621                if (freqs[i] <= host->f_min)
2622                        break;
2623        }
2624        mmc_release_host(host);
2625
2626 out:
2627        if (host->caps & MMC_CAP_NEEDS_POLL)
2628                mmc_schedule_delayed_work(&host->detect, HZ);
2629}
2630
2631void mmc_start_host(struct mmc_host *host)
2632{
2633        host->f_init = max(freqs[0], host->f_min);
2634        host->rescan_disable = 0;
2635        host->ios.power_mode = MMC_POWER_UNDEFINED;
2636        if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
2637                mmc_power_off(host);
2638        else
2639                mmc_power_up(host, host->ocr_avail);
2640        mmc_gpiod_request_cd_irq(host);
2641        _mmc_detect_change(host, 0, false);
2642}
2643
2644void mmc_stop_host(struct mmc_host *host)
2645{
2646#ifdef CONFIG_MMC_DEBUG
2647        unsigned long flags;
2648        spin_lock_irqsave(&host->lock, flags);
2649        host->removed = 1;
2650        spin_unlock_irqrestore(&host->lock, flags);
2651#endif
2652        if (host->slot.cd_irq >= 0)
2653                disable_irq(host->slot.cd_irq);
2654
2655        host->rescan_disable = 1;
2656        cancel_delayed_work_sync(&host->detect);
2657        mmc_flush_scheduled_work();
2658
2659        /* clear pm flags now and let card drivers set them as needed */
2660        host->pm_flags = 0;
2661
2662        mmc_bus_get(host);
2663        if (host->bus_ops && !host->bus_dead) {
2664                /* Calling bus_ops->remove() with a claimed host can deadlock */
2665                host->bus_ops->remove(host);
2666                mmc_claim_host(host);
2667                mmc_detach_bus(host);
2668                mmc_power_off(host);
2669                mmc_release_host(host);
2670                mmc_bus_put(host);
2671                return;
2672        }
2673        mmc_bus_put(host);
2674
2675        BUG_ON(host->card);
2676
2677        mmc_power_off(host);
2678}
2679
2680int mmc_power_save_host(struct mmc_host *host)
2681{
2682        int ret = 0;
2683
2684#ifdef CONFIG_MMC_DEBUG
2685        pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2686#endif
2687
2688        mmc_bus_get(host);
2689
2690        if (!host->bus_ops || host->bus_dead) {
2691                mmc_bus_put(host);
2692                return -EINVAL;
2693        }
2694
2695        if (host->bus_ops->power_save)
2696                ret = host->bus_ops->power_save(host);
2697
2698        mmc_bus_put(host);
2699
2700        mmc_power_off(host);
2701
2702        return ret;
2703}
2704EXPORT_SYMBOL(mmc_power_save_host);
2705
2706int mmc_power_restore_host(struct mmc_host *host)
2707{
2708        int ret;
2709
2710#ifdef CONFIG_MMC_DEBUG
2711        pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2712#endif
2713
2714        mmc_bus_get(host);
2715
2716        if (!host->bus_ops || host->bus_dead) {
2717                mmc_bus_put(host);
2718                return -EINVAL;
2719        }
2720
2721        mmc_power_up(host, host->card->ocr);
2722        ret = host->bus_ops->power_restore(host);
2723
2724        mmc_bus_put(host);
2725
2726        return ret;
2727}
2728EXPORT_SYMBOL(mmc_power_restore_host);
2729
2730/*
2731 * Flush the cache to the non-volatile storage.
2732 */
2733int mmc_flush_cache(struct mmc_card *card)
2734{
2735        int err = 0;
2736
2737        if (mmc_card_mmc(card) &&
2738                        (card->ext_csd.cache_size > 0) &&
2739                        (card->ext_csd.cache_ctrl & 1)) {
2740                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2741                                EXT_CSD_FLUSH_CACHE, 1, 0);
2742                if (err)
2743                        pr_err("%s: cache flush error %d\n",
2744                                        mmc_hostname(card->host), err);
2745        }
2746
2747        return err;
2748}
2749EXPORT_SYMBOL(mmc_flush_cache);
2750
2751#ifdef CONFIG_PM
2752
2753/* Do the card removal on suspend if card is assumed removeable
2754 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2755   to sync the card.
2756*/
2757int mmc_pm_notify(struct notifier_block *notify_block,
2758                                        unsigned long mode, void *unused)
2759{
2760        struct mmc_host *host = container_of(
2761                notify_block, struct mmc_host, pm_notify);
2762        unsigned long flags;
2763        int err = 0;
2764
2765        switch (mode) {
2766        case PM_HIBERNATION_PREPARE:
2767        case PM_SUSPEND_PREPARE:
2768        case PM_RESTORE_PREPARE:
2769                spin_lock_irqsave(&host->lock, flags);
2770                host->rescan_disable = 1;
2771                spin_unlock_irqrestore(&host->lock, flags);
2772                cancel_delayed_work_sync(&host->detect);
2773
2774                if (!host->bus_ops)
2775                        break;
2776
2777                /* Validate prerequisites for suspend */
2778                if (host->bus_ops->pre_suspend)
2779                        err = host->bus_ops->pre_suspend(host);
2780                if (!err)
2781                        break;
2782
2783                /* Calling bus_ops->remove() with a claimed host can deadlock */
2784                host->bus_ops->remove(host);
2785                mmc_claim_host(host);
2786                mmc_detach_bus(host);
2787                mmc_power_off(host);
2788                mmc_release_host(host);
2789                host->pm_flags = 0;
2790                break;
2791
2792        case PM_POST_SUSPEND:
2793        case PM_POST_HIBERNATION:
2794        case PM_POST_RESTORE:
2795
2796                spin_lock_irqsave(&host->lock, flags);
2797                host->rescan_disable = 0;
2798                spin_unlock_irqrestore(&host->lock, flags);
2799                _mmc_detect_change(host, 0, false);
2800
2801        }
2802
2803        return 0;
2804}
2805#endif
2806
2807/**
2808 * mmc_init_context_info() - init synchronization context
2809 * @host: mmc host
2810 *
2811 * Init struct context_info needed to implement asynchronous
2812 * request mechanism, used by mmc core, host driver and mmc requests
2813 * supplier.
2814 */
2815void mmc_init_context_info(struct mmc_host *host)
2816{
2817        spin_lock_init(&host->context_info.lock);
2818        host->context_info.is_new_req = false;
2819        host->context_info.is_done_rcv = false;
2820        host->context_info.is_waiting_last_req = false;
2821        init_waitqueue_head(&host->context_info.wait);
2822}
2823
2824static int __init mmc_init(void)
2825{
2826        int ret;
2827
2828        workqueue = alloc_ordered_workqueue("kmmcd", 0);
2829        if (!workqueue)
2830                return -ENOMEM;
2831
2832        ret = mmc_register_bus();
2833        if (ret)
2834                goto destroy_workqueue;
2835
2836        ret = mmc_register_host_class();
2837        if (ret)
2838                goto unregister_bus;
2839
2840        ret = sdio_register_bus();
2841        if (ret)
2842                goto unregister_host_class;
2843
2844        return 0;
2845
2846unregister_host_class:
2847        mmc_unregister_host_class();
2848unregister_bus:
2849        mmc_unregister_bus();
2850destroy_workqueue:
2851        destroy_workqueue(workqueue);
2852
2853        return ret;
2854}
2855
2856static void __exit mmc_exit(void)
2857{
2858        sdio_unregister_bus();
2859        mmc_unregister_host_class();
2860        mmc_unregister_bus();
2861        destroy_workqueue(workqueue);
2862}
2863
2864subsys_initcall(mmc_init);
2865module_exit(mmc_exit);
2866
2867MODULE_LICENSE("GPL");
2868