123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074 |
- /*
- * linux/drivers/mmc/core/core.c
- *
- * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
- * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
- * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
- * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/completion.h>
- #include <linux/device.h>
- #include <linux/delay.h>
- #include <linux/pagemap.h>
- #include <linux/err.h>
- #include <linux/leds.h>
- #include <linux/scatterlist.h>
- #include <linux/log2.h>
- #include <linux/regulator/consumer.h>
- #include <linux/mmc/card.h>
- #include <linux/mmc/host.h>
- #include <linux/mmc/mmc.h>
- #include <linux/mmc/sd.h>
- #include "core.h"
- #include "bus.h"
- #include "host.h"
- #include "sdio_bus.h"
- #include "mmc_ops.h"
- #include "sd_ops.h"
- #include "sdio_ops.h"
- static struct workqueue_struct *workqueue;
- /*
- * Enabling software CRCs on the data blocks can be a significant (30%)
- * performance cost, and for other reasons may not always be desired.
- * So we allow it it to be disabled.
- */
- int use_spi_crc = 1;
- module_param(use_spi_crc, bool, 0);
- /*
- * Internal function. Schedule delayed work in the MMC work queue.
- */
- static int mmc_schedule_delayed_work(struct delayed_work *work,
- unsigned long delay)
- {
- return queue_delayed_work(workqueue, work, delay);
- }
- /*
- * Internal function. Flush all scheduled work from the MMC work queue.
- */
- static void mmc_flush_scheduled_work(void)
- {
- flush_workqueue(workqueue);
- }
- /**
- * mmc_request_done - finish processing an MMC request
- * @host: MMC host which completed request
- * @mrq: MMC request which request
- *
- * MMC drivers should call this function when they have completed
- * their processing of a request.
- */
- void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
- {
- struct mmc_command *cmd = mrq->cmd;
- int err = cmd->error;
- if (err && cmd->retries && mmc_host_is_spi(host)) {
- if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
- cmd->retries = 0;
- }
- if (err && cmd->retries) {
- pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
- mmc_hostname(host), cmd->opcode, err);
- cmd->retries--;
- cmd->error = 0;
- host->ops->request(host, mrq);
- } else {
- led_trigger_event(host->led, LED_OFF);
- pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
- mmc_hostname(host), cmd->opcode, err,
- cmd->resp[0], cmd->resp[1],
- cmd->resp[2], cmd->resp[3]);
- if (mrq->data) {
- pr_debug("%s: %d bytes transferred: %d\n",
- mmc_hostname(host),
- mrq->data->bytes_xfered, mrq->data->error);
- }
- if (mrq->stop) {
- pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
- mmc_hostname(host), mrq->stop->opcode,
- mrq->stop->error,
- mrq->stop->resp[0], mrq->stop->resp[1],
- mrq->stop->resp[2], mrq->stop->resp[3]);
- }
- if (mrq->done)
- mrq->done(mrq);
- }
- }
- EXPORT_SYMBOL(mmc_request_done);
- static void
- mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
- {
- #ifdef CONFIG_MMC_DEBUG
- unsigned int i, sz;
- struct scatterlist *sg;
- #endif
- pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
- mmc_hostname(host), mrq->cmd->opcode,
- mrq->cmd->arg, mrq->cmd->flags);
- if (mrq->data) {
- pr_debug("%s: blksz %d blocks %d flags %08x "
- "tsac %d ms nsac %d\n",
- mmc_hostname(host), mrq->data->blksz,
- mrq->data->blocks, mrq->data->flags,
- mrq->data->timeout_ns / 1000000,
- mrq->data->timeout_clks);
- }
- if (mrq->stop) {
- pr_debug("%s: CMD%u arg %08x flags %08x\n",
- mmc_hostname(host), mrq->stop->opcode,
- mrq->stop->arg, mrq->stop->flags);
- }
- WARN_ON(!host->claimed);
- led_trigger_event(host->led, LED_FULL);
- mrq->cmd->error = 0;
- mrq->cmd->mrq = mrq;
- if (mrq->data) {
- BUG_ON(mrq->data->blksz > host->max_blk_size);
- BUG_ON(mrq->data->blocks > host->max_blk_count);
- BUG_ON(mrq->data->blocks * mrq->data->blksz >
- host->max_req_size);
- #ifdef CONFIG_MMC_DEBUG
- sz = 0;
- for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
- sz += sg->length;
- BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
- #endif
- mrq->cmd->data = mrq->data;
- mrq->data->error = 0;
- mrq->data->mrq = mrq;
- if (mrq->stop) {
- mrq->data->stop = mrq->stop;
- mrq->stop->error = 0;
- mrq->stop->mrq = mrq;
- }
- }
- host->ops->request(host, mrq);
- }
- static void mmc_wait_done(struct mmc_request *mrq)
- {
- complete(mrq->done_data);
- }
- /**
- * mmc_wait_for_req - start a request and wait for completion
- * @host: MMC host to start command
- * @mrq: MMC request to start
- *
- * Start a new MMC custom command request for a host, and wait
- * for the command to complete. Does not attempt to parse the
- * response.
- */
- void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
- {
- DECLARE_COMPLETION_ONSTACK(complete);
- mrq->done_data = &complete;
- mrq->done = mmc_wait_done;
- mmc_start_request(host, mrq);
- wait_for_completion(&complete);
- }
- EXPORT_SYMBOL(mmc_wait_for_req);
- /**
- * mmc_wait_for_cmd - start a command and wait for completion
- * @host: MMC host to start command
- * @cmd: MMC command to start
- * @retries: maximum number of retries
- *
- * Start a new MMC command for a host, and wait for the command
- * to complete. Return any error that occurred while the command
- * was executing. Do not attempt to parse the response.
- */
- int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
- {
- struct mmc_request mrq;
- WARN_ON(!host->claimed);
- memset(&mrq, 0, sizeof(struct mmc_request));
- memset(cmd->resp, 0, sizeof(cmd->resp));
- cmd->retries = retries;
- mrq.cmd = cmd;
- cmd->data = NULL;
- mmc_wait_for_req(host, &mrq);
- return cmd->error;
- }
- EXPORT_SYMBOL(mmc_wait_for_cmd);
- /**
- * mmc_set_data_timeout - set the timeout for a data command
- * @data: data phase for command
- * @card: the MMC card associated with the data transfer
- *
- * Computes the data timeout parameters according to the
- * correct algorithm given the card type.
- */
- void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
- {
- unsigned int mult;
- /*
- * SDIO cards only define an upper 1 s limit on access.
- */
- if (mmc_card_sdio(card)) {
- data->timeout_ns = 1000000000;
- data->timeout_clks = 0;
- return;
- }
- /*
- * SD cards use a 100 multiplier rather than 10
- */
- mult = mmc_card_sd(card) ? 100 : 10;
- /*
- * Scale up the multiplier (and therefore the timeout) by
- * the r2w factor for writes.
- */
- if (data->flags & MMC_DATA_WRITE)
- mult <<= card->csd.r2w_factor;
- data->timeout_ns = card->csd.tacc_ns * mult;
- data->timeout_clks = card->csd.tacc_clks * mult;
- /*
- * SD cards also have an upper limit on the timeout.
- */
- if (mmc_card_sd(card)) {
- unsigned int timeout_us, limit_us;
- timeout_us = data->timeout_ns / 1000;
- timeout_us += data->timeout_clks * 1000 /
- (card->host->ios.clock / 1000);
- if (data->flags & MMC_DATA_WRITE)
- /*
- * The limit is really 250 ms, but that is
- * insufficient for some crappy cards.
- */
- limit_us = 300000;
- else
- limit_us = 100000;
- /*
- * SDHC cards always use these fixed values.
- */
- if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
- data->timeout_ns = limit_us * 1000;
- data->timeout_clks = 0;
- }
- }
- /*
- * Some cards need very high timeouts if driven in SPI mode.
- * The worst observed timeout was 900ms after writing a
- * continuous stream of data until the internal logic
- * overflowed.
- */
- if (mmc_host_is_spi(card->host)) {
- if (data->flags & MMC_DATA_WRITE) {
- if (data->timeout_ns < 1000000000)
- data->timeout_ns = 1000000000; /* 1s */
- } else {
- if (data->timeout_ns < 100000000)
- data->timeout_ns = 100000000; /* 100ms */
- }
- }
- }
- EXPORT_SYMBOL(mmc_set_data_timeout);
- /**
- * mmc_align_data_size - pads a transfer size to a more optimal value
- * @card: the MMC card associated with the data transfer
- * @sz: original transfer size
- *
- * Pads the original data size with a number of extra bytes in
- * order to avoid controller bugs and/or performance hits
- * (e.g. some controllers revert to PIO for certain sizes).
- *
- * Returns the improved size, which might be unmodified.
- *
- * Note that this function is only relevant when issuing a
- * single scatter gather entry.
- */
- unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
- {
- /*
- * FIXME: We don't have a system for the controller to tell
- * the core about its problems yet, so for now we just 32-bit
- * align the size.
- */
- sz = ((sz + 3) / 4) * 4;
- return sz;
- }
- EXPORT_SYMBOL(mmc_align_data_size);
- /**
- * __mmc_claim_host - exclusively claim a host
- * @host: mmc host to claim
- * @abort: whether or not the operation should be aborted
- *
- * Claim a host for a set of operations. If @abort is non null and
- * dereference a non-zero value then this will return prematurely with
- * that non-zero value without acquiring the lock. Returns zero
- * with the lock held otherwise.
- */
- int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
- {
- DECLARE_WAITQUEUE(wait, current);
- unsigned long flags;
- int stop;
- might_sleep();
- add_wait_queue(&host->wq, &wait);
- spin_lock_irqsave(&host->lock, flags);
- while (1) {
- set_current_state(TASK_UNINTERRUPTIBLE);
- stop = abort ? atomic_read(abort) : 0;
- if (stop || !host->claimed)
- break;
- spin_unlock_irqrestore(&host->lock, flags);
- schedule();
- spin_lock_irqsave(&host->lock, flags);
- }
- set_current_state(TASK_RUNNING);
- if (!stop)
- host->claimed = 1;
- else
- wake_up(&host->wq);
- spin_unlock_irqrestore(&host->lock, flags);
- remove_wait_queue(&host->wq, &wait);
- return stop;
- }
- EXPORT_SYMBOL(__mmc_claim_host);
- /**
- * mmc_release_host - release a host
- * @host: mmc host to release
- *
- * Release a MMC host, allowing others to claim the host
- * for their operations.
- */
- void mmc_release_host(struct mmc_host *host)
- {
- unsigned long flags;
- WARN_ON(!host->claimed);
- spin_lock_irqsave(&host->lock, flags);
- host->claimed = 0;
- spin_unlock_irqrestore(&host->lock, flags);
- wake_up(&host->wq);
- }
- EXPORT_SYMBOL(mmc_release_host);
- /*
- * Internal function that does the actual ios call to the host driver,
- * optionally printing some debug output.
- */
- static inline void mmc_set_ios(struct mmc_host *host)
- {
- struct mmc_ios *ios = &host->ios;
- pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
- "width %u timing %u\n",
- mmc_hostname(host), ios->clock, ios->bus_mode,
- ios->power_mode, ios->chip_select, ios->vdd,
- ios->bus_width, ios->timing);
- host->ops->set_ios(host, ios);
- }
- /*
- * Control chip select pin on a host.
- */
- void mmc_set_chip_select(struct mmc_host *host, int mode)
- {
- host->ios.chip_select = mode;
- mmc_set_ios(host);
- }
- /*
- * Sets the host clock to the highest possible frequency that
- * is below "hz".
- */
- void mmc_set_clock(struct mmc_host *host, unsigned int hz)
- {
- WARN_ON(hz < host->f_min);
- if (hz > host->f_max)
- hz = host->f_max;
- host->ios.clock = hz;
- mmc_set_ios(host);
- }
- /*
- * Change the bus mode (open drain/push-pull) of a host.
- */
- void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
- {
- host->ios.bus_mode = mode;
- mmc_set_ios(host);
- }
- /*
- * Change data bus width of a host.
- */
- void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
- {
- host->ios.bus_width = width;
- mmc_set_ios(host);
- }
- /**
- * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
- * @vdd: voltage (mV)
- * @low_bits: prefer low bits in boundary cases
- *
- * This function returns the OCR bit number according to the provided @vdd
- * value. If conversion is not possible a negative errno value returned.
- *
- * Depending on the @low_bits flag the function prefers low or high OCR bits
- * on boundary voltages. For example,
- * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
- * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
- *
- * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
- */
- static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
- {
- const int max_bit = ilog2(MMC_VDD_35_36);
- int bit;
- if (vdd < 1650 || vdd > 3600)
- return -EINVAL;
- if (vdd >= 1650 && vdd <= 1950)
- return ilog2(MMC_VDD_165_195);
- if (low_bits)
- vdd -= 1;
- /* Base 2000 mV, step 100 mV, bit's base 8. */
- bit = (vdd - 2000) / 100 + 8;
- if (bit > max_bit)
- return max_bit;
- return bit;
- }
- /**
- * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
- * @vdd_min: minimum voltage value (mV)
- * @vdd_max: maximum voltage value (mV)
- *
- * This function returns the OCR mask bits according to the provided @vdd_min
- * and @vdd_max values. If conversion is not possible the function returns 0.
- *
- * Notes wrt boundary cases:
- * This function sets the OCR bits for all boundary voltages, for example
- * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
- * MMC_VDD_34_35 mask.
- */
- u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
- {
- u32 mask = 0;
- if (vdd_max < vdd_min)
- return 0;
- /* Prefer high bits for the boundary vdd_max values. */
- vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
- if (vdd_max < 0)
- return 0;
- /* Prefer low bits for the boundary vdd_min values. */
- vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
- if (vdd_min < 0)
- return 0;
- /* Fill the mask, from max bit to min bit. */
- while (vdd_max >= vdd_min)
- mask |= 1 << vdd_max--;
- return mask;
- }
- EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
- #ifdef CONFIG_REGULATOR
- /**
- * mmc_regulator_get_ocrmask - return mask of supported voltages
- * @supply: regulator to use
- *
- * This returns either a negative errno, or a mask of voltages that
- * can be provided to MMC/SD/SDIO devices using the specified voltage
- * regulator. This would normally be called before registering the
- * MMC host adapter.
- */
- int mmc_regulator_get_ocrmask(struct regulator *supply)
- {
- int result = 0;
- int count;
- int i;
- count = regulator_count_voltages(supply);
- if (count < 0)
- return count;
- for (i = 0; i < count; i++) {
- int vdd_uV;
- int vdd_mV;
- vdd_uV = regulator_list_voltage(supply, i);
- if (vdd_uV <= 0)
- continue;
- vdd_mV = vdd_uV / 1000;
- result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
- }
- return result;
- }
- EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
- /**
- * mmc_regulator_set_ocr - set regulator to match host->ios voltage
- * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
- * @supply: regulator to use
- *
- * Returns zero on success, else negative errno.
- *
- * MMC host drivers may use this to enable or disable a regulator using
- * a particular supply voltage. This would normally be called from the
- * set_ios() method.
- */
- int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
- {
- int result = 0;
- int min_uV, max_uV;
- int enabled;
- enabled = regulator_is_enabled(supply);
- if (enabled < 0)
- return enabled;
- if (vdd_bit) {
- int tmp;
- int voltage;
- /* REVISIT mmc_vddrange_to_ocrmask() may have set some
- * bits this regulator doesn't quite support ... don't
- * be too picky, most cards and regulators are OK with
- * a 0.1V range goof (it's a small error percentage).
- */
- tmp = vdd_bit - ilog2(MMC_VDD_165_195);
- if (tmp == 0) {
- min_uV = 1650 * 1000;
- max_uV = 1950 * 1000;
- } else {
- min_uV = 1900 * 1000 + tmp * 100 * 1000;
- max_uV = min_uV + 100 * 1000;
- }
- /* avoid needless changes to this voltage; the regulator
- * might not allow this operation
- */
- voltage = regulator_get_voltage(supply);
- if (voltage < 0)
- result = voltage;
- else if (voltage < min_uV || voltage > max_uV)
- result = regulator_set_voltage(supply, min_uV, max_uV);
- else
- result = 0;
- if (result == 0 && !enabled)
- result = regulator_enable(supply);
- } else if (enabled) {
- result = regulator_disable(supply);
- }
- return result;
- }
- EXPORT_SYMBOL(mmc_regulator_set_ocr);
- #endif
- /*
- * Mask off any voltages we don't support and select
- * the lowest voltage
- */
- u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
- {
- int bit;
- ocr &= host->ocr_avail;
- bit = ffs(ocr);
- if (bit) {
- bit -= 1;
- ocr &= 3 << bit;
- host->ios.vdd = bit;
- mmc_set_ios(host);
- } else {
- pr_warning("%s: host doesn't support card's voltages\n",
- mmc_hostname(host));
- ocr = 0;
- }
- return ocr;
- }
- /*
- * Select timing parameters for host.
- */
- void mmc_set_timing(struct mmc_host *host, unsigned int timing)
- {
- host->ios.timing = timing;
- mmc_set_ios(host);
- }
- /*
- * Apply power to the MMC stack. This is a two-stage process.
- * First, we enable power to the card without the clock running.
- * We then wait a bit for the power to stabilise. Finally,
- * enable the bus drivers and clock to the card.
- *
- * We must _NOT_ enable the clock prior to power stablising.
- *
- * If a host does all the power sequencing itself, ignore the
- * initial MMC_POWER_UP stage.
- */
- static void mmc_power_up(struct mmc_host *host)
- {
- int bit = fls(host->ocr_avail) - 1;
- host->ios.vdd = bit;
- if (mmc_host_is_spi(host)) {
- host->ios.chip_select = MMC_CS_HIGH;
- host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
- } else {
- host->ios.chip_select = MMC_CS_DONTCARE;
- host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
- }
- host->ios.power_mode = MMC_POWER_UP;
- host->ios.bus_width = MMC_BUS_WIDTH_1;
- host->ios.timing = MMC_TIMING_LEGACY;
- mmc_set_ios(host);
- /*
- * This delay should be sufficient to allow the power supply
- * to reach the minimum voltage.
- */
- mmc_delay(10);
- if (host->f_min > 400000) {
- pr_warning("%s: Minimum clock frequency too high for "
- "identification mode\n", mmc_hostname(host));
- host->ios.clock = host->f_min;
- } else
- host->ios.clock = 400000;
- host->ios.power_mode = MMC_POWER_ON;
- mmc_set_ios(host);
- /*
- * This delay must be at least 74 clock sizes, or 1 ms, or the
- * time required to reach a stable voltage.
- */
- mmc_delay(10);
- }
- static void mmc_power_off(struct mmc_host *host)
- {
- host->ios.clock = 0;
- host->ios.vdd = 0;
- if (!mmc_host_is_spi(host)) {
- host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
- host->ios.chip_select = MMC_CS_DONTCARE;
- }
- host->ios.power_mode = MMC_POWER_OFF;
- host->ios.bus_width = MMC_BUS_WIDTH_1;
- host->ios.timing = MMC_TIMING_LEGACY;
- mmc_set_ios(host);
- }
- /*
- * Cleanup when the last reference to the bus operator is dropped.
- */
- static void __mmc_release_bus(struct mmc_host *host)
- {
- BUG_ON(!host);
- BUG_ON(host->bus_refs);
- BUG_ON(!host->bus_dead);
- host->bus_ops = NULL;
- }
- /*
- * Increase reference count of bus operator
- */
- static inline void mmc_bus_get(struct mmc_host *host)
- {
- unsigned long flags;
- spin_lock_irqsave(&host->lock, flags);
- host->bus_refs++;
- spin_unlock_irqrestore(&host->lock, flags);
- }
- /*
- * Decrease reference count of bus operator and free it if
- * it is the last reference.
- */
- static inline void mmc_bus_put(struct mmc_host *host)
- {
- unsigned long flags;
- spin_lock_irqsave(&host->lock, flags);
- host->bus_refs--;
- if ((host->bus_refs == 0) && host->bus_ops)
- __mmc_release_bus(host);
- spin_unlock_irqrestore(&host->lock, flags);
- }
- /*
- * Assign a mmc bus handler to a host. Only one bus handler may control a
- * host at any given time.
- */
- void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
- {
- unsigned long flags;
- BUG_ON(!host);
- BUG_ON(!ops);
- WARN_ON(!host->claimed);
- spin_lock_irqsave(&host->lock, flags);
- BUG_ON(host->bus_ops);
- BUG_ON(host->bus_refs);
- host->bus_ops = ops;
- host->bus_refs = 1;
- host->bus_dead = 0;
- spin_unlock_irqrestore(&host->lock, flags);
- }
- /*
- * Remove the current bus handler from a host. Assumes that there are
- * no interesting cards left, so the bus is powered down.
- */
- void mmc_detach_bus(struct mmc_host *host)
- {
- unsigned long flags;
- BUG_ON(!host);
- WARN_ON(!host->claimed);
- WARN_ON(!host->bus_ops);
- spin_lock_irqsave(&host->lock, flags);
- host->bus_dead = 1;
- spin_unlock_irqrestore(&host->lock, flags);
- mmc_power_off(host);
- mmc_bus_put(host);
- }
- /**
- * mmc_detect_change - process change of state on a MMC socket
- * @host: host which changed state.
- * @delay: optional delay to wait before detection (jiffies)
- *
- * MMC drivers should call this when they detect a card has been
- * inserted or removed. The MMC layer will confirm that any
- * present card is still functional, and initialize any newly
- * inserted.
- */
- void mmc_detect_change(struct mmc_host *host, unsigned long delay)
- {
- #ifdef CONFIG_MMC_DEBUG
- unsigned long flags;
- spin_lock_irqsave(&host->lock, flags);
- WARN_ON(host->removed);
- spin_unlock_irqrestore(&host->lock, flags);
- #endif
- mmc_schedule_delayed_work(&host->detect, delay);
- }
- EXPORT_SYMBOL(mmc_detect_change);
- void mmc_rescan(struct work_struct *work)
- {
- struct mmc_host *host =
- container_of(work, struct mmc_host, detect.work);
- u32 ocr;
- int err;
- mmc_bus_get(host);
- /* if there is a card registered, check whether it is still present */
- if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
- host->bus_ops->detect(host);
- mmc_bus_put(host);
- mmc_bus_get(host);
- /* if there still is a card present, stop here */
- if (host->bus_ops != NULL) {
- mmc_bus_put(host);
- goto out;
- }
- /* detect a newly inserted card */
- /*
- * Only we can add a new handler, so it's safe to
- * release the lock here.
- */
- mmc_bus_put(host);
- if (host->ops->get_cd && host->ops->get_cd(host) == 0)
- goto out;
- mmc_claim_host(host);
- mmc_power_up(host);
- mmc_go_idle(host);
- mmc_send_if_cond(host, host->ocr_avail);
- /*
- * First we search for SDIO...
- */
- err = mmc_send_io_op_cond(host, 0, &ocr);
- if (!err) {
- if (mmc_attach_sdio(host, ocr))
- mmc_power_off(host);
- goto out;
- }
- /*
- * ...then normal SD...
- */
- err = mmc_send_app_op_cond(host, 0, &ocr);
- if (!err) {
- if (mmc_attach_sd(host, ocr))
- mmc_power_off(host);
- goto out;
- }
- /*
- * ...and finally MMC.
- */
- err = mmc_send_op_cond(host, 0, &ocr);
- if (!err) {
- if (mmc_attach_mmc(host, ocr))
- mmc_power_off(host);
- goto out;
- }
- mmc_release_host(host);
- mmc_power_off(host);
- out:
- if (host->caps & MMC_CAP_NEEDS_POLL)
- mmc_schedule_delayed_work(&host->detect, HZ);
- }
- void mmc_start_host(struct mmc_host *host)
- {
- mmc_power_off(host);
- mmc_detect_change(host, 0);
- }
- void mmc_stop_host(struct mmc_host *host)
- {
- #ifdef CONFIG_MMC_DEBUG
- unsigned long flags;
- spin_lock_irqsave(&host->lock, flags);
- host->removed = 1;
- spin_unlock_irqrestore(&host->lock, flags);
- #endif
- cancel_delayed_work(&host->detect);
- mmc_flush_scheduled_work();
- mmc_bus_get(host);
- if (host->bus_ops && !host->bus_dead) {
- if (host->bus_ops->remove)
- host->bus_ops->remove(host);
- mmc_claim_host(host);
- mmc_detach_bus(host);
- mmc_release_host(host);
- }
- mmc_bus_put(host);
- BUG_ON(host->card);
- mmc_power_off(host);
- }
- #ifdef CONFIG_PM
- /**
- * mmc_suspend_host - suspend a host
- * @host: mmc host
- * @state: suspend mode (PM_SUSPEND_xxx)
- */
- int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
- {
- cancel_delayed_work(&host->detect);
- mmc_flush_scheduled_work();
- mmc_bus_get(host);
- if (host->bus_ops && !host->bus_dead) {
- if (host->bus_ops->suspend)
- host->bus_ops->suspend(host);
- if (!host->bus_ops->resume) {
- if (host->bus_ops->remove)
- host->bus_ops->remove(host);
- mmc_claim_host(host);
- mmc_detach_bus(host);
- mmc_release_host(host);
- }
- }
- mmc_bus_put(host);
- mmc_power_off(host);
- return 0;
- }
- EXPORT_SYMBOL(mmc_suspend_host);
- /**
- * mmc_resume_host - resume a previously suspended host
- * @host: mmc host
- */
- int mmc_resume_host(struct mmc_host *host)
- {
- mmc_bus_get(host);
- if (host->bus_ops && !host->bus_dead) {
- mmc_power_up(host);
- mmc_select_voltage(host, host->ocr);
- BUG_ON(!host->bus_ops->resume);
- host->bus_ops->resume(host);
- }
- mmc_bus_put(host);
- /*
- * We add a slight delay here so that resume can progress
- * in parallel.
- */
- mmc_detect_change(host, 1);
- return 0;
- }
- EXPORT_SYMBOL(mmc_resume_host);
- #endif
- static int __init mmc_init(void)
- {
- int ret;
- workqueue = create_singlethread_workqueue("kmmcd");
- if (!workqueue)
- return -ENOMEM;
- ret = mmc_register_bus();
- if (ret)
- goto destroy_workqueue;
- ret = mmc_register_host_class();
- if (ret)
- goto unregister_bus;
- ret = sdio_register_bus();
- if (ret)
- goto unregister_host_class;
- return 0;
- unregister_host_class:
- mmc_unregister_host_class();
- unregister_bus:
- mmc_unregister_bus();
- destroy_workqueue:
- destroy_workqueue(workqueue);
- return ret;
- }
- static void __exit mmc_exit(void)
- {
- sdio_unregister_bus();
- mmc_unregister_host_class();
- mmc_unregister_bus();
- destroy_workqueue(workqueue);
- }
- subsys_initcall(mmc_init);
- module_exit(mmc_exit);
- MODULE_LICENSE("GPL");
|