13 #include <linux/module.h>
17 #include <linux/device.h>
28 #include <linux/random.h>
29 #include <linux/slab.h>
49 #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000)
52 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
68 #ifdef CONFIG_MMC_UNSAFE_RESUME
77 "MMC/SD cards are removable and may be removed during suspend");
91 static void mmc_flush_scheduled_work(
void)
96 #ifdef CONFIG_FAIL_MMC_REQUEST
102 static void mmc_should_fail_request(
struct mmc_host *
host,
107 static const int data_errors[] = {
126 static inline void mmc_should_fail_request(
struct mmc_host *host,
159 mmc_should_fail_request(host, mrq);
163 pr_debug(
"%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
169 pr_debug(
"%s: %d bytes transferred: %d\n",
171 mrq->
data->bytes_xfered, mrq->
data->error);
175 pr_debug(
"%s: (CMD%u): %d: %08x %08x %08x %08x\n",
178 mrq->
stop->resp[0], mrq->
stop->resp[1],
179 mrq->
stop->resp[2], mrq->
stop->resp[3]);
185 mmc_host_clk_release(host);
194 #ifdef CONFIG_MMC_DEBUG
200 pr_debug(
"<%s: starting CMD%u arg %08x flags %08x>\n",
202 mrq->
sbc->arg, mrq->
sbc->flags);
205 pr_debug(
"%s: starting CMD%u arg %08x flags %08x\n",
207 mrq->
cmd->arg, mrq->
cmd->flags);
210 pr_debug(
"%s: blksz %d blocks %d flags %08x "
211 "tsac %d ms nsac %d\n",
213 mrq->
data->blocks, mrq->
data->flags,
214 mrq->
data->timeout_ns / 1000000,
215 mrq->
data->timeout_clks);
219 pr_debug(
"%s: CMD%u arg %08x flags %08x\n",
234 #ifdef CONFIG_MMC_DEBUG
238 BUG_ON(sz != mrq->data->blocks * mrq->data->
blksz);
242 mrq->
data->error = 0;
243 mrq->
data->mrq = mrq;
246 mrq->
stop->error = 0;
247 mrq->
stop->mrq = mrq;
250 mmc_host_clk_hold(host);
252 host->
ops->request(host, mrq);
269 bool use_busy_signal;
278 pr_err(
"%s: Failed to read bkops status: %d\n",
283 if (!card->
ext_csd.raw_bkops_status)
290 mmc_claim_host(card->
host);
293 use_busy_signal =
true;
296 use_busy_signal =
false;
302 pr_warn(
"%s: Error %d starting bkops\n",
312 if (!use_busy_signal)
327 mrq->
done = mmc_wait_done;
333 mmc_start_request(host, mrq);
337 static void mmc_wait_for_req_done(
struct mmc_host *host,
350 pr_debug(
"%s: req failed (CMD%u): %d, retrying...\n",
354 host->
ops->request(host, mrq);
372 if (host->
ops->pre_req) {
373 mmc_host_clk_hold(host);
374 host->
ops->pre_req(host, mrq, is_first_req);
375 mmc_host_clk_release(host);
391 if (host->
ops->post_req) {
392 mmc_host_clk_hold(host);
393 host->
ops->post_req(host, mrq, err);
394 mmc_host_clk_release(host);
423 mmc_pre_req(host, areq->
mrq, !host->
areq);
426 mmc_wait_for_req_done(host, host->
areq->mrq);
439 start_err = __mmc_start_req(host, areq->
mrq);
442 mmc_post_req(host, host->
areq->mrq, 0);
445 if ((err || start_err) && areq)
470 __mmc_start_req(host, mrq);
471 mmc_wait_for_req_done(host, mrq);
486 unsigned long prg_wait;
495 mmc_claim_host(card->
host);
516 pr_debug(
"%s: HPI cannot be sent. Card state=%d\n",
591 if (!err || (err == -
EINVAL)) {
610 pr_err(
"%s: could not allocate buffer to receive the ext_csd.\n",
615 mmc_claim_host(card->
host);
660 mult <<= card->
csd.r2w_factor;
669 unsigned int timeout_us, limit_us;
672 if (mmc_host_clk_rate(card->
host))
674 (mmc_host_clk_rate(card->
host) / 1000);
748 sz = ((sz + 3) / 4) * 4;
779 spin_unlock_irqrestore(&host->
lock, flags);
790 spin_unlock_irqrestore(&host->
lock, flags);
793 host->
ops->enable(host);
807 int claimed_host = 0;
817 spin_unlock_irqrestore(&host->
lock, flags);
818 if (host->
ops->enable && claimed_host && host->
claim_cnt == 1)
819 host->
ops->enable(host);
838 host->
ops->disable(host);
843 spin_unlock_irqrestore(&host->
lock, flags);
847 spin_unlock_irqrestore(&host->
lock, flags);
857 static inline void mmc_set_ios(
struct mmc_host *host)
861 pr_debug(
"%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
862 "width %u timing %u\n",
869 host->
ops->set_ios(host, ios);
877 mmc_host_clk_hold(host);
880 mmc_host_clk_release(host);
887 static void __mmc_set_clock(
struct mmc_host *host,
unsigned int hz)
891 if (hz > host->
f_max)
894 host->
ios.clock = hz;
900 mmc_host_clk_hold(host);
901 __mmc_set_clock(host, hz);
902 mmc_host_clk_release(host);
905 #ifdef CONFIG_MMC_CLKGATE
914 host->clk_old = host->
ios.clock;
916 host->clk_gated =
true;
917 spin_unlock_irqrestore(&host->clk_lock, flags);
937 __mmc_set_clock(host, host->clk_old);
950 host->clk_gated =
false;
951 spin_unlock_irqrestore(&host->clk_lock, flags);
965 mmc_host_clk_hold(host);
968 mmc_host_clk_release(host);
976 mmc_host_clk_hold(host);
979 mmc_host_clk_release(host);
997 static int mmc_vdd_to_ocrbitnum(
int vdd,
bool low_bits)
1002 if (vdd < 1650 || vdd > 3600)
1005 if (vdd >= 1650 && vdd <= 1950)
1012 bit = (vdd - 2000) / 100 + 8;
1035 if (vdd_max < vdd_min)
1039 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max,
false);
1044 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min,
true);
1049 while (vdd_max >= vdd_min)
1050 mask |= 1 << vdd_max--;
1056 #ifdef CONFIG_REGULATOR
1067 int mmc_regulator_get_ocrmask(
struct regulator *supply)
1077 for (i = 0; i <
count; i++) {
1085 vdd_mV = vdd_uV / 1000;
1105 int mmc_regulator_set_ocr(
struct mmc_host *mmc,
1107 unsigned short vdd_bit)
1124 min_uV = 1650 * 1000;
1125 max_uV = 1950 * 1000;
1127 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1128 max_uV = min_uV + 100 * 1000;
1138 min_uV = max_uV = voltage;
1142 else if (voltage < min_uV || voltage > max_uV)
1147 if (result == 0 && !mmc->regulator_enabled) {
1150 mmc->regulator_enabled =
true;
1152 }
else if (mmc->regulator_enabled) {
1155 mmc->regulator_enabled =
false;
1160 "could not set regulator OCR (%d)\n", result);
1165 int mmc_regulator_get_supply(
struct mmc_host *mmc)
1172 mmc->
supply.vmmc = supply;
1176 return PTR_ERR(supply);
1178 ret = mmc_regulator_get_ocrmask(supply);
1206 mmc_host_clk_hold(host);
1209 mmc_host_clk_release(host);
1211 pr_warning(
"%s: host doesn't support card's voltages\n",
1243 host->
ios.signal_voltage = signal_voltage;
1245 if (host->
ops->start_signal_voltage_switch) {
1246 mmc_host_clk_hold(host);
1247 err = host->
ops->start_signal_voltage_switch(host, &host->
ios);
1248 mmc_host_clk_release(host);
1259 mmc_host_clk_hold(host);
1260 host->
ios.timing = timing;
1262 mmc_host_clk_release(host);
1270 mmc_host_clk_hold(host);
1271 host->
ios.drv_type = drv_type;
1273 mmc_host_clk_release(host);
1287 static void mmc_power_up(
struct mmc_host *host)
1294 mmc_host_clk_hold(host);
1298 bit =
ffs(host->
ocr) - 1;
1333 mmc_host_clk_release(host);
1341 mmc_host_clk_hold(host);
1343 host->
ios.clock = 0;
1369 mmc_host_clk_release(host);
1375 static void __mmc_release_bus(
struct mmc_host *host)
1387 static inline void mmc_bus_get(
struct mmc_host *host)
1389 unsigned long flags;
1393 spin_unlock_irqrestore(&host->
lock, flags);
1400 static inline void mmc_bus_put(
struct mmc_host *host)
1402 unsigned long flags;
1407 __mmc_release_bus(host);
1408 spin_unlock_irqrestore(&host->
lock, flags);
1417 unsigned long flags;
1433 spin_unlock_irqrestore(&host->
lock, flags);
1441 unsigned long flags;
1452 spin_unlock_irqrestore(&host->
lock, flags);
1469 #ifdef CONFIG_MMC_DEBUG
1470 unsigned long flags;
1473 spin_unlock_irqrestore(&host->
lock, flags);
1476 mmc_schedule_delayed_work(&host->
detect, delay);
1508 }
else if (card->
ext_csd.hc_erase_size) {
1511 sz = (card->
csd.capacity << (card->
csd.read_blkbits - 9)) >> 11;
1530 static unsigned int mmc_mmc_erase_timeout(
struct mmc_card *
card,
1531 unsigned int arg,
unsigned int qty)
1533 unsigned int erase_timeout;
1537 erase_timeout = card->
ext_csd.trim_timeout;
1538 }
else if (card->
ext_csd.erase_group_def & 1) {
1541 erase_timeout = card->
ext_csd.trim_timeout;
1543 erase_timeout = card->
ext_csd.hc_erase_timeout;
1546 unsigned int mult = (10 << card->
csd.r2w_factor);
1547 unsigned int timeout_clks = card->
csd.tacc_clks *
mult;
1548 unsigned int timeout_us;
1551 if (card->
csd.tacc_ns < 1000000)
1552 timeout_us = (card->
csd.tacc_ns *
mult) / 1000;
1554 timeout_us = (card->
csd.tacc_ns / 1000) * mult;
1561 timeout_us += (timeout_clks * 1000) /
1562 (mmc_host_clk_rate(card->
host) / 1000);
1564 erase_timeout = timeout_us / 1000;
1577 erase_timeout *= card->
ext_csd.sec_erase_mult;
1579 erase_timeout *= card->
ext_csd.sec_trim_mult;
1582 erase_timeout *= qty;
1589 erase_timeout = 1000;
1591 return erase_timeout;
1594 static unsigned int mmc_sd_erase_timeout(
struct mmc_card *card,
1598 unsigned int erase_timeout;
1600 if (card->
ssr.erase_timeout) {
1602 erase_timeout = card->
ssr.erase_timeout * qty +
1603 card->
ssr.erase_offset;
1609 erase_timeout = 250 * qty;
1613 if (erase_timeout < 1000)
1614 erase_timeout = 1000;
1616 return erase_timeout;
1619 static unsigned int mmc_erase_timeout(
struct mmc_card *card,
1624 return mmc_sd_erase_timeout(card, arg, qty);
1626 return mmc_mmc_erase_timeout(card, arg, qty);
1629 static int mmc_do_erase(
struct mmc_card *card,
unsigned int from,
1630 unsigned int to,
unsigned int arg)
1633 unsigned int qty = 0;
1656 qty += to - from + 1;
1674 pr_err(
"mmc_erase: group start error %d, "
1675 "status %#x\n", err, cmd.
resp[0]);
1689 pr_err(
"mmc_erase: group end error %d, status %#x\n",
1702 pr_err(
"mmc_erase: erase error %d, status %#x\n",
1714 cmd.
arg = card->
rca << 16;
1718 if (err || (cmd.
resp[0] & 0xFDF92000)) {
1719 pr_err(
"error %d requesting status %#x\n",
1742 unsigned int rem, to = from +
nr;
1754 if ((arg & MMC_SECURE_ARGS) &&
1793 return mmc_do_erase(card, from, to, arg);
1855 static unsigned int mmc_do_calc_max_discard(
struct mmc_card *card,
1859 unsigned int max_discard,
x,
y, qty = 0, max_qty,
timeout;
1860 unsigned int last_timeout = 0;
1872 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1873 timeout = mmc_erase_timeout(card, arg, qty + x);
1876 if (timeout < last_timeout)
1878 last_timeout = timeout;
1904 unsigned int max_discard, max_trim;
1919 max_trim = mmc_do_calc_max_discard(card,
MMC_TRIM_ARG);
1920 if (max_trim < max_discard)
1921 max_discard = max_trim;
1922 }
else if (max_discard < card->erase_size) {
1925 pr_debug(
"%s: calculated max. discard sectors %u for timeout %u ms\n",
1945 static void mmc_hw_reset_for_init(
struct mmc_host *host)
1949 mmc_host_clk_hold(host);
1950 host->
ops->hw_reset(host);
1951 mmc_host_clk_release(host);
1960 rst_n_function = card->
ext_csd.rst_n_function;
1967 static int mmc_do_hw_reset(
struct mmc_host *host,
int check)
1971 if (!host->
bus_ops->power_restore)
1983 mmc_host_clk_hold(host);
1986 host->
ops->hw_reset(host);
1995 cmd.
arg = card->
rca << 16;
1999 mmc_host_clk_release(host);
2016 mmc_host_clk_release(host);
2018 return host->
bus_ops->power_restore(host);
2023 return mmc_do_hw_reset(host, 0);
2029 return mmc_do_hw_reset(host, 1);
2033 static int mmc_rescan_try_freq(
struct mmc_host *host,
unsigned freq)
2037 #ifdef CONFIG_MMC_DEBUG
2038 pr_info(
"%s: %s: trying to init card at %u Hz\n",
2047 mmc_hw_reset_for_init(host);
2081 ret = host->
bus_ops->alive(host);
2171 if (host->
ops->get_cd && host->
ops->get_cd(host) == 0) {
2172 mmc_claim_host(host);
2178 mmc_claim_host(host);
2180 if (!mmc_rescan_try_freq(host,
max(freqs[i], host->
f_min)))
2182 if (freqs[i] <= host->
f_min)
2189 mmc_schedule_delayed_work(&host->
detect,
HZ);
2202 #ifdef CONFIG_MMC_DEBUG
2203 unsigned long flags;
2206 spin_unlock_irqrestore(&host->
lock, flags);
2211 mmc_flush_scheduled_work();
2222 mmc_claim_host(host);
2240 #ifdef CONFIG_MMC_DEBUG
2251 if (host->
bus_ops->power_save)
2252 ret = host->
bus_ops->power_save(host);
2266 #ifdef CONFIG_MMC_DEBUG
2278 ret = host->
bus_ops->power_restore(host);
2296 err = host->
bus_ops->awake(host);
2314 err = host->
bus_ops->sleep(host);
2344 (card->
ext_csd.cache_size > 0) &&
2345 (card->
ext_csd.cache_ctrl & 1)) {
2349 pr_err(
"%s: cache flush error %d\n",
2365 unsigned int timeout;
2369 mmc_card_is_removable(host))
2372 mmc_claim_host(host);
2374 (card->
ext_csd.cache_size > 0)) {
2377 if (card->
ext_csd.cache_ctrl ^ enable) {
2378 timeout = enable ? card->
ext_csd.generic_cmd6_time : 0;
2382 pr_err(
"%s: cache %s error %d\n",
2384 enable ?
"on" :
"off",
2407 mmc_flush_scheduled_work();
2421 err = host->
bus_ops->suspend(host);
2433 mmc_claim_host(host);
2443 if (!err && !mmc_card_keep_power(host))
2462 if (!mmc_card_keep_power(host)) {
2473 pm_runtime_disable(&host->
card->dev);
2474 pm_runtime_set_active(&host->
card->dev);
2479 err = host->
bus_ops->resume(host);
2482 "(card was removed?)\n",
2503 unsigned long flags;
2513 pr_err(
"%s: didn't stop bkops\n",
2522 spin_unlock_irqrestore(&host->
lock, flags);
2532 mmc_claim_host(host);
2545 spin_unlock_irqrestore(&host->
lock, flags);
2554 static int __init mmc_init(
void)
2568 goto unregister_bus;
2572 goto unregister_host_class;
2576 unregister_host_class:
2586 static void __exit mmc_exit(
void)