| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* linux/drivers/mmc/core/core.c |
| 4 |
* |
| 5 |
* Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
| 6 |
* SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
| 7 |
* Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
| 8 |
* MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
| 9 |
*/ |
| 10 |
#include <linux/module.h> |
| 11 |
#include <linux/init.h> |
| 12 |
#include <linux/interrupt.h> |
| 13 |
#include <linux/completion.h> |
| 14 |
#include <linux/device.h> |
| 15 |
#include <linux/delay.h> |
| 16 |
#include <linux/pagemap.h> |
| 17 |
#include <linux/err.h> |
| 18 |
#include <linux/leds.h> |
| 19 |
#include <linux/scatterlist.h> |
| 20 |
#include <linux/log2.h> |
| 21 |
#include <linux/pm_runtime.h> |
| 22 |
#include <linux/suspend.h> |
| 23 |
#include <linux/fault-inject.h> |
| 24 |
#include <linux/random.h> |
| 25 |
#include <linux/slab.h> |
| 26 |
#include <linux/of.h> |
| 27 |
|
| 28 |
#include <linux/mmc/card.h> |
| 29 |
#include <linux/mmc/host.h> |
| 30 |
#include <linux/mmc/mmc.h> |
| 31 |
#include <linux/mmc/sd.h> |
| 32 |
#include <linux/mmc/slot-gpio.h> |
| 33 |
|
| 34 |
#define CREATE_TRACE_POINTS |
| 35 |
#include <trace/events/mmc.h> |
| 36 |
|
| 37 |
#include "core.h" |
| 38 |
#include "card.h" |
| 39 |
#include "crypto.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 |
/* The max erase timeout, used when host->max_busy_timeout isn't specified */ |
| 50 |
#define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */ |
| 51 |
#define SD_DISCARD_TIMEOUT_MS (250) |
| 52 |
|
| 53 |
static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
| 54 |
|
| 55 |
/* |
| 56 |
* Enabling software CRCs on the data blocks can be a significant (30%) |
| 57 |
* performance cost, and for other reasons may not always be desired. |
| 58 |
* So we allow it to be disabled. |
| 59 |
*/ |
| 60 |
bool use_spi_crc = 1; |
| 61 |
module_param(use_spi_crc, bool, 0); |
| 62 |
|
| 63 |
static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 64 |
unsigned long delay) |
| 65 |
{ |
| 66 |
/* |
| 67 |
* We use the system_freezable_wq, because of two reasons. |
| 68 |
* First, it allows several works (not the same work item) to be |
| 69 |
* executed simultaneously. Second, the queue becomes frozen when |
| 70 |
* userspace becomes frozen during system PM. |
| 71 |
*/ |
| 72 |
return queue_delayed_work(system_freezable_wq, work, delay); |
| 73 |
} |
| 74 |
|
| 75 |
#ifdef CONFIG_FAIL_MMC_REQUEST |
| 76 |
|
| 77 |
/* |
| 78 |
* Internal function. Inject random data errors. |
| 79 |
* If mmc_data is NULL no errors are injected. |
| 80 |
*/ |
| 81 |
static void mmc_should_fail_request(struct mmc_host *host, |
| 82 |
struct mmc_request *mrq) |
| 83 |
{ |
| 84 |
struct mmc_command *cmd = mrq->cmd; |
| 85 |
struct mmc_data *data = mrq->data; |
| 86 |
static const int data_errors[] = { |
| 87 |
-ETIMEDOUT, |
| 88 |
-EILSEQ, |
| 89 |
-EIO, |
| 90 |
}; |
| 91 |
|
| 92 |
if (!data) |
| 93 |
return; |
| 94 |
|
| 95 |
if ((cmd && cmd->error) || data->error || |
| 96 |
!should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) |
| 97 |
return; |
| 98 |
|
| 99 |
data->error = data_errors[get_random_u32_below(ARRAY_SIZE(data_errors))]; |
| 100 |
data->bytes_xfered = get_random_u32_below(data->bytes_xfered >> 9) << 9; |
| 101 |
} |
| 102 |
|
| 103 |
#else /* CONFIG_FAIL_MMC_REQUEST */ |
| 104 |
|
| 105 |
static inline void mmc_should_fail_request(struct mmc_host *host, |
| 106 |
struct mmc_request *mrq) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
#endif /* CONFIG_FAIL_MMC_REQUEST */ |
| 111 |
|
| 112 |
static inline void mmc_complete_cmd(struct mmc_request *mrq) |
| 113 |
{ |
| 114 |
if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion)) |
| 115 |
complete_all(&mrq->cmd_completion); |
| 116 |
} |
| 117 |
|
| 118 |
void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq) |
| 119 |
{ |
| 120 |
if (!mrq->cap_cmd_during_tfr) |
| 121 |
return; |
| 122 |
|
| 123 |
mmc_complete_cmd(mrq); |
| 124 |
|
| 125 |
pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n", |
| 126 |
mmc_hostname(host), mrq->cmd->opcode); |
| 127 |
} |
| 128 |
EXPORT_SYMBOL(mmc_command_done); |
| 129 |
|
| 130 |
/** |
| 131 |
* mmc_request_done - finish processing an MMC request |
| 132 |
* @host: MMC host which completed request |
| 133 |
* @mrq: MMC request which request |
| 134 |
* |
| 135 |
* MMC drivers should call this function when they have completed |
| 136 |
* their processing of a request. |
| 137 |
*/ |
| 138 |
void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 139 |
{ |
| 140 |
struct mmc_command *cmd = mrq->cmd; |
| 141 |
int err = cmd->error; |
| 142 |
|
| 143 |
/* Flag re-tuning needed on CRC errors */ |
| 144 |
if (!mmc_op_tuning(cmd->opcode) && |
| 145 |
!host->retune_crc_disable && |
| 146 |
(err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) || |
| 147 |
(mrq->data && mrq->data->error == -EILSEQ) || |
| 148 |
(mrq->stop && mrq->stop->error == -EILSEQ))) |
| 149 |
mmc_retune_needed(host); |
| 150 |
|
| 151 |
if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 152 |
if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 153 |
cmd->retries = 0; |
| 154 |
} |
| 155 |
|
| 156 |
if (host->ongoing_mrq == mrq) |
| 157 |
host->ongoing_mrq = NULL; |
| 158 |
|
| 159 |
mmc_complete_cmd(mrq); |
| 160 |
|
| 161 |
trace_mmc_request_done(host, mrq); |
| 162 |
|
| 163 |
/* |
| 164 |
* We list various conditions for the command to be considered |
| 165 |
* properly done: |
| 166 |
* |
| 167 |
* - There was no error, OK fine then |
| 168 |
* - We are not doing some kind of retry |
| 169 |
* - The card was removed (...so just complete everything no matter |
| 170 |
* if there are errors or retries) |
| 171 |
*/ |
| 172 |
if (!err || !cmd->retries || mmc_card_removed(host->card)) { |
| 173 |
mmc_should_fail_request(host, mrq); |
| 174 |
|
| 175 |
if (!host->ongoing_mrq) |
| 176 |
led_trigger_event(host->led, LED_OFF); |
| 177 |
|
| 178 |
if (mrq->sbc) { |
| 179 |
pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n", |
| 180 |
mmc_hostname(host), mrq->sbc->opcode, |
| 181 |
mrq->sbc->error, |
| 182 |
mrq->sbc->resp[0], mrq->sbc->resp[1], |
| 183 |
mrq->sbc->resp[2], mrq->sbc->resp[3]); |
| 184 |
} |
| 185 |
|
| 186 |
pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 187 |
mmc_hostname(host), cmd->opcode, err, |
| 188 |
cmd->resp[0], cmd->resp[1], |
| 189 |
cmd->resp[2], cmd->resp[3]); |
| 190 |
|
| 191 |
if (mrq->data) { |
| 192 |
pr_debug("%s: %d bytes transferred: %d\n", |
| 193 |
mmc_hostname(host), |
| 194 |
mrq->data->bytes_xfered, mrq->data->error); |
| 195 |
} |
| 196 |
|
| 197 |
if (mrq->stop) { |
| 198 |
pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 199 |
mmc_hostname(host), mrq->stop->opcode, |
| 200 |
mrq->stop->error, |
| 201 |
mrq->stop->resp[0], mrq->stop->resp[1], |
| 202 |
mrq->stop->resp[2], mrq->stop->resp[3]); |
| 203 |
} |
| 204 |
} |
| 205 |
/* |
| 206 |
* Request starter must handle retries - see |
| 207 |
* mmc_wait_for_req_done(). |
| 208 |
*/ |
| 209 |
if (mrq->done) |
| 210 |
mrq->done(mrq); |
| 211 |
} |
| 212 |
|
| 213 |
EXPORT_SYMBOL(mmc_request_done); |
| 214 |
|
| 215 |
static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 216 |
{ |
| 217 |
int err; |
| 218 |
|
| 219 |
/* Assumes host controller has been runtime resumed by mmc_claim_host */ |
| 220 |
err = mmc_retune(host); |
| 221 |
if (err) { |
| 222 |
mrq->cmd->error = err; |
| 223 |
mmc_request_done(host, mrq); |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* For sdio rw commands we must wait for card busy otherwise some |
| 229 |
* sdio devices won't work properly. |
| 230 |
* And bypass I/O abort, reset and bus suspend operations. |
| 231 |
*/ |
| 232 |
if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) && |
| 233 |
host->ops->card_busy) { |
| 234 |
int tries = 500; /* Wait aprox 500ms at maximum */ |
| 235 |
|
| 236 |
while (host->ops->card_busy(host) && --tries) |
| 237 |
mmc_delay(1); |
| 238 |
|
| 239 |
if (tries == 0) { |
| 240 |
mrq->cmd->error = -EBUSY; |
| 241 |
mmc_request_done(host, mrq); |
| 242 |
return; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if (mrq->cap_cmd_during_tfr) { |
| 247 |
host->ongoing_mrq = mrq; |
| 248 |
/* |
| 249 |
* Retry path could come through here without having waiting on |
| 250 |
* cmd_completion, so ensure it is reinitialised. |
| 251 |
*/ |
| 252 |
reinit_completion(&mrq->cmd_completion); |
| 253 |
} |
| 254 |
|
| 255 |
trace_mmc_request_start(host, mrq); |
| 256 |
|
| 257 |
if (host->cqe_on) |
| 258 |
host->cqe_ops->cqe_off(host); |
| 259 |
|
| 260 |
host->ops->request(host, mrq); |
| 261 |
} |
| 262 |
|
| 263 |
static void mmc_mrq_pr_debug(struct mmc_host *host, struct mmc_request *mrq, |
| 264 |
bool cqe) |
| 265 |
{ |
| 266 |
if (mrq->sbc) { |
| 267 |
pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n", |
| 268 |
mmc_hostname(host), mrq->sbc->opcode, |
| 269 |
mrq->sbc->arg, mrq->sbc->flags); |
| 270 |
} |
| 271 |
|
| 272 |
if (mrq->cmd) { |
| 273 |
pr_debug("%s: starting %sCMD%u arg %08x flags %08x\n", |
| 274 |
mmc_hostname(host), cqe ? "CQE direct " : "", |
| 275 |
mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags); |
| 276 |
} else if (cqe) { |
| 277 |
pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n", |
| 278 |
mmc_hostname(host), mrq->tag, mrq->data->blk_addr); |
| 279 |
} |
| 280 |
|
| 281 |
if (mrq->data) { |
| 282 |
pr_debug("%s: blksz %d blocks %d flags %08x " |
| 283 |
"tsac %d ms nsac %d\n", |
| 284 |
mmc_hostname(host), mrq->data->blksz, |
| 285 |
mrq->data->blocks, mrq->data->flags, |
| 286 |
mrq->data->timeout_ns / 1000000, |
| 287 |
mrq->data->timeout_clks); |
| 288 |
} |
| 289 |
|
| 290 |
if (mrq->stop) { |
| 291 |
pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 292 |
mmc_hostname(host), mrq->stop->opcode, |
| 293 |
mrq->stop->arg, mrq->stop->flags); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
static int mmc_mrq_prep(struct mmc_host *host, struct mmc_request *mrq) |
| 298 |
{ |
| 299 |
unsigned int i, sz = 0; |
| 300 |
struct scatterlist *sg; |
| 301 |
|
| 302 |
if (mrq->cmd) { |
| 303 |
mrq->cmd->error = 0; |
| 304 |
mrq->cmd->mrq = mrq; |
| 305 |
mrq->cmd->data = mrq->data; |
| 306 |
} |
| 307 |
if (mrq->sbc) { |
| 308 |
mrq->sbc->error = 0; |
| 309 |
mrq->sbc->mrq = mrq; |
| 310 |
} |
| 311 |
if (mrq->data) { |
| 312 |
if (mrq->data->blksz > host->max_blk_size || |
| 313 |
mrq->data->blocks > host->max_blk_count || |
| 314 |
mrq->data->blocks * mrq->data->blksz > host->max_req_size) |
| 315 |
return -EINVAL; |
| 316 |
|
| 317 |
for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 318 |
sz += sg->length; |
| 319 |
if (sz != mrq->data->blocks * mrq->data->blksz) |
| 320 |
return -EINVAL; |
| 321 |
|
| 322 |
mrq->data->error = 0; |
| 323 |
mrq->data->mrq = mrq; |
| 324 |
if (mrq->stop) { |
| 325 |
mrq->data->stop = mrq->stop; |
| 326 |
mrq->stop->error = 0; |
| 327 |
mrq->stop->mrq = mrq; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 335 |
{ |
| 336 |
int err; |
| 337 |
|
| 338 |
if (mrq->cmd->has_ext_addr) |
| 339 |
mmc_send_ext_addr(host, mrq->cmd->ext_addr); |
| 340 |
|
| 341 |
init_completion(&mrq->cmd_completion); |
| 342 |
|
| 343 |
mmc_retune_hold(host); |
| 344 |
|
| 345 |
if (mmc_card_removed(host->card)) |
| 346 |
return -ENOMEDIUM; |
| 347 |
|
| 348 |
mmc_mrq_pr_debug(host, mrq, false); |
| 349 |
|
| 350 |
WARN_ON(!host->claimed); |
| 351 |
|
| 352 |
err = mmc_mrq_prep(host, mrq); |
| 353 |
if (err) |
| 354 |
return err; |
| 355 |
|
| 356 |
if (host->uhs2_sd_tran) |
| 357 |
mmc_uhs2_prepare_cmd(host, mrq); |
| 358 |
|
| 359 |
led_trigger_event(host->led, LED_FULL); |
| 360 |
__mmc_start_request(host, mrq); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
EXPORT_SYMBOL(mmc_start_request); |
| 365 |
|
| 366 |
static void mmc_wait_done(struct mmc_request *mrq) |
| 367 |
{ |
| 368 |
complete(&mrq->completion); |
| 369 |
} |
| 370 |
|
| 371 |
static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host) |
| 372 |
{ |
| 373 |
struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq); |
| 374 |
|
| 375 |
/* |
| 376 |
* If there is an ongoing transfer, wait for the command line to become |
| 377 |
* available. |
| 378 |
*/ |
| 379 |
if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion)) |
| 380 |
wait_for_completion(&ongoing_mrq->cmd_completion); |
| 381 |
} |
| 382 |
|
| 383 |
static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) |
| 384 |
{ |
| 385 |
int err; |
| 386 |
|
| 387 |
mmc_wait_ongoing_tfr_cmd(host); |
| 388 |
|
| 389 |
init_completion(&mrq->completion); |
| 390 |
mrq->done = mmc_wait_done; |
| 391 |
|
| 392 |
err = mmc_start_request(host, mrq); |
| 393 |
if (err) { |
| 394 |
mrq->cmd->error = err; |
| 395 |
mmc_complete_cmd(mrq); |
| 396 |
complete(&mrq->completion); |
| 397 |
} |
| 398 |
|
| 399 |
return err; |
| 400 |
} |
| 401 |
|
| 402 |
void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq) |
| 403 |
{ |
| 404 |
struct mmc_command *cmd; |
| 405 |
|
| 406 |
while (1) { |
| 407 |
wait_for_completion(&mrq->completion); |
| 408 |
|
| 409 |
cmd = mrq->cmd; |
| 410 |
|
| 411 |
if (!cmd->error || !cmd->retries || |
| 412 |
mmc_card_removed(host->card)) |
| 413 |
break; |
| 414 |
|
| 415 |
mmc_retune_recheck(host); |
| 416 |
|
| 417 |
pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 418 |
mmc_hostname(host), cmd->opcode, cmd->error); |
| 419 |
cmd->retries--; |
| 420 |
cmd->error = 0; |
| 421 |
__mmc_start_request(host, mrq); |
| 422 |
} |
| 423 |
|
| 424 |
mmc_retune_release(host); |
| 425 |
} |
| 426 |
EXPORT_SYMBOL(mmc_wait_for_req_done); |
| 427 |
|
| 428 |
/* |
| 429 |
* mmc_cqe_start_req - Start a CQE request. |
| 430 |
* @host: MMC host to start the request |
| 431 |
* @mrq: request to start |
| 432 |
* |
| 433 |
* Start the request, re-tuning if needed and it is possible. Returns an error |
| 434 |
* code if the request fails to start or -EBUSY if CQE is busy. |
| 435 |
*/ |
| 436 |
int mmc_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) |
| 437 |
{ |
| 438 |
int err; |
| 439 |
|
| 440 |
/* |
| 441 |
* CQE cannot process re-tuning commands. Caller must hold retuning |
| 442 |
* while CQE is in use. Re-tuning can happen here only when CQE has no |
| 443 |
* active requests i.e. this is the first. Note, re-tuning will call |
| 444 |
* ->cqe_off(). |
| 445 |
*/ |
| 446 |
err = mmc_retune(host); |
| 447 |
if (err) |
| 448 |
goto out_err; |
| 449 |
|
| 450 |
mrq->host = host; |
| 451 |
|
| 452 |
mmc_mrq_pr_debug(host, mrq, true); |
| 453 |
|
| 454 |
err = mmc_mrq_prep(host, mrq); |
| 455 |
if (err) |
| 456 |
goto out_err; |
| 457 |
|
| 458 |
if (host->uhs2_sd_tran) |
| 459 |
mmc_uhs2_prepare_cmd(host, mrq); |
| 460 |
|
| 461 |
err = host->cqe_ops->cqe_request(host, mrq); |
| 462 |
if (err) |
| 463 |
goto out_err; |
| 464 |
|
| 465 |
trace_mmc_request_start(host, mrq); |
| 466 |
|
| 467 |
return 0; |
| 468 |
|
| 469 |
out_err: |
| 470 |
if (mrq->cmd) { |
| 471 |
pr_debug("%s: failed to start CQE direct CMD%u, error %d\n", |
| 472 |
mmc_hostname(host), mrq->cmd->opcode, err); |
| 473 |
} else { |
| 474 |
pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n", |
| 475 |
mmc_hostname(host), mrq->tag, err); |
| 476 |
} |
| 477 |
return err; |
| 478 |
} |
| 479 |
EXPORT_SYMBOL(mmc_cqe_start_req); |
| 480 |
|
| 481 |
/** |
| 482 |
* mmc_cqe_request_done - CQE has finished processing an MMC request |
| 483 |
* @host: MMC host which completed request |
| 484 |
* @mrq: MMC request which completed |
| 485 |
* |
| 486 |
* CQE drivers should call this function when they have completed |
| 487 |
* their processing of a request. |
| 488 |
*/ |
| 489 |
void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 490 |
{ |
| 491 |
mmc_should_fail_request(host, mrq); |
| 492 |
|
| 493 |
/* Flag re-tuning needed on CRC errors */ |
| 494 |
if ((mrq->cmd && mrq->cmd->error == -EILSEQ) || |
| 495 |
(mrq->data && mrq->data->error == -EILSEQ)) |
| 496 |
mmc_retune_needed(host); |
| 497 |
|
| 498 |
trace_mmc_request_done(host, mrq); |
| 499 |
|
| 500 |
if (mrq->cmd) { |
| 501 |
pr_debug("%s: CQE req done (direct CMD%u): %d\n", |
| 502 |
mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error); |
| 503 |
} else { |
| 504 |
pr_debug("%s: CQE transfer done tag %d\n", |
| 505 |
mmc_hostname(host), mrq->tag); |
| 506 |
} |
| 507 |
|
| 508 |
if (mrq->data) { |
| 509 |
pr_debug("%s: %d bytes transferred: %d\n", |
| 510 |
mmc_hostname(host), |
| 511 |
mrq->data->bytes_xfered, mrq->data->error); |
| 512 |
} |
| 513 |
|
| 514 |
mrq->done(mrq); |
| 515 |
} |
| 516 |
EXPORT_SYMBOL(mmc_cqe_request_done); |
| 517 |
|
| 518 |
/** |
| 519 |
* mmc_cqe_post_req - CQE post process of a completed MMC request |
| 520 |
* @host: MMC host |
| 521 |
* @mrq: MMC request to be processed |
| 522 |
*/ |
| 523 |
void mmc_cqe_post_req(struct mmc_host *host, struct mmc_request *mrq) |
| 524 |
{ |
| 525 |
if (host->cqe_ops->cqe_post_req) |
| 526 |
host->cqe_ops->cqe_post_req(host, mrq); |
| 527 |
} |
| 528 |
EXPORT_SYMBOL(mmc_cqe_post_req); |
| 529 |
|
| 530 |
/* Arbitrary 1 second timeout */ |
| 531 |
#define MMC_CQE_RECOVERY_TIMEOUT 1000 |
| 532 |
|
| 533 |
/* |
| 534 |
* mmc_cqe_recovery - Recover from CQE errors. |
| 535 |
* @host: MMC host to recover |
| 536 |
* |
| 537 |
* Recovery consists of stopping CQE, stopping eMMC, discarding the queue |
| 538 |
* in eMMC, and discarding the queue in CQE. CQE must call |
| 539 |
* mmc_cqe_request_done() on all requests. An error is returned if the eMMC |
| 540 |
* fails to discard its queue. |
| 541 |
*/ |
| 542 |
int mmc_cqe_recovery(struct mmc_host *host) |
| 543 |
{ |
| 544 |
struct mmc_command cmd; |
| 545 |
int err; |
| 546 |
|
| 547 |
mmc_retune_hold_now(host); |
| 548 |
|
| 549 |
/* |
| 550 |
* Recovery is expected seldom, if at all, but it reduces performance, |
| 551 |
* so make sure it is not completely silent. |
| 552 |
*/ |
| 553 |
pr_warn("%s: running CQE recovery\n", mmc_hostname(host)); |
| 554 |
|
| 555 |
host->cqe_ops->cqe_recovery_start(host); |
| 556 |
|
| 557 |
memset(&cmd, 0, sizeof(cmd)); |
| 558 |
cmd.opcode = MMC_STOP_TRANSMISSION; |
| 559 |
cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */ |
| 560 |
cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT; |
| 561 |
mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
| 562 |
|
| 563 |
mmc_poll_for_busy(host->card, MMC_CQE_RECOVERY_TIMEOUT, true, MMC_BUSY_IO); |
| 564 |
|
| 565 |
memset(&cmd, 0, sizeof(cmd)); |
| 566 |
cmd.opcode = MMC_CMDQ_TASK_MGMT; |
| 567 |
cmd.arg = 1; /* Discard entire queue */ |
| 568 |
cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */ |
| 569 |
cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT; |
| 570 |
err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
| 571 |
|
| 572 |
host->cqe_ops->cqe_recovery_finish(host); |
| 573 |
|
| 574 |
if (err) |
| 575 |
err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); |
| 576 |
|
| 577 |
mmc_retune_release(host); |
| 578 |
|
| 579 |
return err; |
| 580 |
} |
| 581 |
EXPORT_SYMBOL(mmc_cqe_recovery); |
| 582 |
|
| 583 |
/** |
| 584 |
* mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done |
| 585 |
* @host: MMC host |
| 586 |
* @mrq: MMC request |
| 587 |
* |
| 588 |
* mmc_is_req_done() is used with requests that have |
| 589 |
* mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after |
| 590 |
* starting a request and before waiting for it to complete. That is, |
| 591 |
* either in between calls to mmc_start_req(), or after mmc_wait_for_req() |
| 592 |
* and before mmc_wait_for_req_done(). If it is called at other times the |
| 593 |
* result is not meaningful. |
| 594 |
*/ |
| 595 |
bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq) |
| 596 |
{ |
| 597 |
return completion_done(&mrq->completion); |
| 598 |
} |
| 599 |
EXPORT_SYMBOL(mmc_is_req_done); |
| 600 |
|
| 601 |
/** |
| 602 |
* mmc_wait_for_req - start a request and wait for completion |
| 603 |
* @host: MMC host to start command |
| 604 |
* @mrq: MMC request to start |
| 605 |
* |
| 606 |
* Start a new MMC custom command request for a host, and wait |
| 607 |
* for the command to complete. In the case of 'cap_cmd_during_tfr' |
| 608 |
* requests, the transfer is ongoing and the caller can issue further |
| 609 |
* commands that do not use the data lines, and then wait by calling |
| 610 |
* mmc_wait_for_req_done(). |
| 611 |
* Does not attempt to parse the response. |
| 612 |
*/ |
| 613 |
void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
| 614 |
{ |
| 615 |
__mmc_start_req(host, mrq); |
| 616 |
|
| 617 |
if (!mrq->cap_cmd_during_tfr) |
| 618 |
mmc_wait_for_req_done(host, mrq); |
| 619 |
} |
| 620 |
EXPORT_SYMBOL(mmc_wait_for_req); |
| 621 |
|
| 622 |
/** |
| 623 |
* mmc_wait_for_cmd - start a command and wait for completion |
| 624 |
* @host: MMC host to start command |
| 625 |
* @cmd: MMC command to start |
| 626 |
* @retries: maximum number of retries |
| 627 |
* |
| 628 |
* Start a new MMC command for a host, and wait for the command |
| 629 |
* to complete. Return any error that occurred while the command |
| 630 |
* was executing. Do not attempt to parse the response. |
| 631 |
*/ |
| 632 |
int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 633 |
{ |
| 634 |
struct mmc_request mrq = {}; |
| 635 |
|
| 636 |
WARN_ON(!host->claimed); |
| 637 |
|
| 638 |
memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 639 |
cmd->retries = retries; |
| 640 |
|
| 641 |
mrq.cmd = cmd; |
| 642 |
cmd->data = NULL; |
| 643 |
|
| 644 |
mmc_wait_for_req(host, &mrq); |
| 645 |
|
| 646 |
return cmd->error; |
| 647 |
} |
| 648 |
|
| 649 |
EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 650 |
|
| 651 |
/** |
| 652 |
* mmc_set_data_timeout - set the timeout for a data command |
| 653 |
* @data: data phase for command |
| 654 |
* @card: the MMC card associated with the data transfer |
| 655 |
* |
| 656 |
* Computes the data timeout parameters according to the |
| 657 |
* correct algorithm given the card type. |
| 658 |
*/ |
| 659 |
void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) |
| 660 |
{ |
| 661 |
unsigned int mult; |
| 662 |
|
| 663 |
/* |
| 664 |
* SDIO cards only define an upper 1 s limit on access. |
| 665 |
*/ |
| 666 |
if (mmc_card_sdio(card)) { |
| 667 |
data->timeout_ns = 1000000000; |
| 668 |
data->timeout_clks = 0; |
| 669 |
return; |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* SD cards use a 100 multiplier rather than 10 |
| 674 |
*/ |
| 675 |
mult = mmc_card_sd(card) ? 100 : 10; |
| 676 |
|
| 677 |
/* |
| 678 |
* Scale up the multiplier (and therefore the timeout) by |
| 679 |
* the r2w factor for writes. |
| 680 |
*/ |
| 681 |
if (data->flags & MMC_DATA_WRITE) |
| 682 |
mult <<= card->csd.r2w_factor; |
| 683 |
|
| 684 |
data->timeout_ns = card->csd.taac_ns * mult; |
| 685 |
data->timeout_clks = card->csd.taac_clks * mult; |
| 686 |
|
| 687 |
/* |
| 688 |
* SD cards also have an upper limit on the timeout. |
| 689 |
*/ |
| 690 |
if (mmc_card_sd(card)) { |
| 691 |
unsigned int timeout_us, limit_us; |
| 692 |
|
| 693 |
timeout_us = data->timeout_ns / 1000; |
| 694 |
if (card->host->ios.clock) |
| 695 |
timeout_us += data->timeout_clks * 1000 / |
| 696 |
(card->host->ios.clock / 1000); |
| 697 |
|
| 698 |
if (data->flags & MMC_DATA_WRITE) |
| 699 |
/* |
| 700 |
* The MMC spec "It is strongly recommended |
| 701 |
* for hosts to implement more than 500ms |
| 702 |
* timeout value even if the card indicates |
| 703 |
* the 250ms maximum busy length." Even the |
| 704 |
* previous value of 300ms is known to be |
| 705 |
* insufficient for some cards. |
| 706 |
*/ |
| 707 |
limit_us = 3000000; |
| 708 |
else |
| 709 |
limit_us = 100000; |
| 710 |
|
| 711 |
/* |
| 712 |
* SDHC cards always use these fixed values. |
| 713 |
*/ |
| 714 |
if (timeout_us > limit_us) { |
| 715 |
data->timeout_ns = limit_us * 1000; |
| 716 |
data->timeout_clks = 0; |
| 717 |
} |
| 718 |
|
| 719 |
/* assign limit value if invalid */ |
| 720 |
if (timeout_us == 0) |
| 721 |
data->timeout_ns = limit_us * 1000; |
| 722 |
} |
| 723 |
|
| 724 |
/* |
| 725 |
* Some cards require longer data read timeout than indicated in CSD. |
| 726 |
* Address this by setting the read timeout to a "reasonably high" |
| 727 |
* value. For the cards tested, 600ms has proven enough. If necessary, |
| 728 |
* this value can be increased if other problematic cards require this. |
| 729 |
*/ |
| 730 |
if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) { |
| 731 |
data->timeout_ns = 600000000; |
| 732 |
data->timeout_clks = 0; |
| 733 |
} |
| 734 |
|
| 735 |
/* |
| 736 |
* Some cards need very high timeouts if driven in SPI mode. |
| 737 |
* The worst observed timeout was 900ms after writing a |
| 738 |
* continuous stream of data until the internal logic |
| 739 |
* overflowed. |
| 740 |
*/ |
| 741 |
if (mmc_host_is_spi(card->host)) { |
| 742 |
if (data->flags & MMC_DATA_WRITE) { |
| 743 |
if (data->timeout_ns < 1000000000) |
| 744 |
data->timeout_ns = 1000000000; /* 1s */ |
| 745 |
} else { |
| 746 |
if (data->timeout_ns < 100000000) |
| 747 |
data->timeout_ns = 100000000; /* 100ms */ |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
EXPORT_SYMBOL(mmc_set_data_timeout); |
| 752 |
|
| 753 |
/* |
| 754 |
* Allow claiming an already claimed host if the context is the same or there is |
| 755 |
* no context but the task is the same. |
| 756 |
*/ |
| 757 |
static inline bool mmc_ctx_matches(struct mmc_host *host, struct mmc_ctx *ctx, |
| 758 |
struct task_struct *task) |
| 759 |
{ |
| 760 |
return host->claimer == ctx || |
| 761 |
(!ctx && task && host->claimer->task == task); |
| 762 |
} |
| 763 |
|
| 764 |
static inline void mmc_ctx_set_claimer(struct mmc_host *host, |
| 765 |
struct mmc_ctx *ctx, |
| 766 |
struct task_struct *task) |
| 767 |
{ |
| 768 |
if (!host->claimer) { |
| 769 |
if (ctx) |
| 770 |
host->claimer = ctx; |
| 771 |
else |
| 772 |
host->claimer = &host->default_ctx; |
| 773 |
} |
| 774 |
if (task) |
| 775 |
host->claimer->task = task; |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* __mmc_claim_host - exclusively claim a host |
| 780 |
* @host: mmc host to claim |
| 781 |
* @ctx: context that claims the host or NULL in which case the default |
| 782 |
* context will be used |
| 783 |
* @abort: whether or not the operation should be aborted |
| 784 |
* |
| 785 |
* Claim a host for a set of operations. If @abort is non null and |
| 786 |
* dereference a non-zero value then this will return prematurely with |
| 787 |
* that non-zero value without acquiring the lock. Returns zero |
| 788 |
* with the lock held otherwise. |
| 789 |
*/ |
| 790 |
int __mmc_claim_host(struct mmc_host *host, struct mmc_ctx *ctx, |
| 791 |
atomic_t *abort) |
| 792 |
{ |
| 793 |
struct task_struct *task = ctx ? NULL : current; |
| 794 |
DECLARE_WAITQUEUE(wait, current); |
| 795 |
unsigned long flags; |
| 796 |
int stop; |
| 797 |
bool pm = false; |
| 798 |
|
| 799 |
might_sleep(); |
| 800 |
|
| 801 |
add_wait_queue(&host->wq, &wait); |
| 802 |
spin_lock_irqsave(&host->lock, flags); |
| 803 |
while (1) { |
| 804 |
set_current_state(TASK_UNINTERRUPTIBLE); |
| 805 |
stop = abort ? atomic_read(abort) : 0; |
| 806 |
if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task)) |
| 807 |
break; |
| 808 |
spin_unlock_irqrestore(&host->lock, flags); |
| 809 |
schedule(); |
| 810 |
spin_lock_irqsave(&host->lock, flags); |
| 811 |
} |
| 812 |
set_current_state(TASK_RUNNING); |
| 813 |
if (!stop) { |
| 814 |
host->claimed = 1; |
| 815 |
mmc_ctx_set_claimer(host, ctx, task); |
| 816 |
host->claim_cnt += 1; |
| 817 |
if (host->claim_cnt == 1) |
| 818 |
pm = true; |
| 819 |
} else |
| 820 |
wake_up(&host->wq); |
| 821 |
spin_unlock_irqrestore(&host->lock, flags); |
| 822 |
remove_wait_queue(&host->wq, &wait); |
| 823 |
|
| 824 |
if (pm) |
| 825 |
pm_runtime_get_sync(mmc_dev(host)); |
| 826 |
|
| 827 |
return stop; |
| 828 |
} |
| 829 |
EXPORT_SYMBOL(__mmc_claim_host); |
| 830 |
|
| 831 |
/** |
| 832 |
* mmc_release_host - release a host |
| 833 |
* @host: mmc host to release |
| 834 |
* |
| 835 |
* Release a MMC host, allowing others to claim the host |
| 836 |
* for their operations. |
| 837 |
*/ |
| 838 |
void mmc_release_host(struct mmc_host *host) |
| 839 |
{ |
| 840 |
unsigned long flags; |
| 841 |
|
| 842 |
WARN_ON(!host->claimed); |
| 843 |
|
| 844 |
spin_lock_irqsave(&host->lock, flags); |
| 845 |
if (--host->claim_cnt) { |
| 846 |
/* Release for nested claim */ |
| 847 |
spin_unlock_irqrestore(&host->lock, flags); |
| 848 |
} else { |
| 849 |
host->claimed = 0; |
| 850 |
host->claimer->task = NULL; |
| 851 |
host->claimer = NULL; |
| 852 |
spin_unlock_irqrestore(&host->lock, flags); |
| 853 |
wake_up(&host->wq); |
| 854 |
pm_runtime_mark_last_busy(mmc_dev(host)); |
| 855 |
if (host->caps & MMC_CAP_SYNC_RUNTIME_PM) |
| 856 |
pm_runtime_put_sync_suspend(mmc_dev(host)); |
| 857 |
else |
| 858 |
pm_runtime_put_autosuspend(mmc_dev(host)); |
| 859 |
} |
| 860 |
} |
| 861 |
EXPORT_SYMBOL(mmc_release_host); |
| 862 |
|
| 863 |
/* |
| 864 |
* This is a helper function, which fetches a runtime pm reference for the |
| 865 |
* card device and also claims the host. |
| 866 |
*/ |
| 867 |
void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx) |
| 868 |
{ |
| 869 |
pm_runtime_get_sync(&card->dev); |
| 870 |
__mmc_claim_host(card->host, ctx, NULL); |
| 871 |
} |
| 872 |
EXPORT_SYMBOL(mmc_get_card); |
| 873 |
|
| 874 |
/* |
| 875 |
* This is a helper function, which releases the host and drops the runtime |
| 876 |
* pm reference for the card device. |
| 877 |
*/ |
| 878 |
void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx) |
| 879 |
{ |
| 880 |
struct mmc_host *host = card->host; |
| 881 |
|
| 882 |
WARN_ON(ctx && host->claimer != ctx); |
| 883 |
|
| 884 |
mmc_release_host(host); |
| 885 |
pm_runtime_put_autosuspend(&card->dev); |
| 886 |
} |
| 887 |
EXPORT_SYMBOL(mmc_put_card); |
| 888 |
|
| 889 |
/* |
| 890 |
* Internal function that does the actual ios call to the host driver, |
| 891 |
* optionally printing some debug output. |
| 892 |
*/ |
| 893 |
static inline void mmc_set_ios(struct mmc_host *host) |
| 894 |
{ |
| 895 |
struct mmc_ios *ios = &host->ios; |
| 896 |
|
| 897 |
pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 898 |
"width %u timing %u\n", |
| 899 |
mmc_hostname(host), ios->clock, ios->bus_mode, |
| 900 |
ios->power_mode, ios->chip_select, ios->vdd, |
| 901 |
1 << ios->bus_width, ios->timing); |
| 902 |
|
| 903 |
host->ops->set_ios(host, ios); |
| 904 |
} |
| 905 |
|
| 906 |
/* |
| 907 |
* Control chip select pin on a host. |
| 908 |
*/ |
| 909 |
void mmc_set_chip_select(struct mmc_host *host, int mode) |
| 910 |
{ |
| 911 |
host->ios.chip_select = mode; |
| 912 |
mmc_set_ios(host); |
| 913 |
} |
| 914 |
|
| 915 |
/* |
| 916 |
* Sets the host clock to the highest possible frequency that |
| 917 |
* is below "hz". |
| 918 |
*/ |
| 919 |
void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 920 |
{ |
| 921 |
WARN_ON(hz && hz < host->f_min); |
| 922 |
|
| 923 |
if (hz > host->f_max) |
| 924 |
hz = host->f_max; |
| 925 |
|
| 926 |
host->ios.clock = hz; |
| 927 |
mmc_set_ios(host); |
| 928 |
} |
| 929 |
|
| 930 |
int mmc_execute_tuning(struct mmc_card *card) |
| 931 |
{ |
| 932 |
struct mmc_host *host = card->host; |
| 933 |
u32 opcode; |
| 934 |
int err; |
| 935 |
|
| 936 |
if (!host->ops->execute_tuning) |
| 937 |
return 0; |
| 938 |
|
| 939 |
if (host->cqe_on) |
| 940 |
host->cqe_ops->cqe_off(host); |
| 941 |
|
| 942 |
if (mmc_card_mmc(card)) |
| 943 |
opcode = MMC_SEND_TUNING_BLOCK_HS200; |
| 944 |
else |
| 945 |
opcode = MMC_SEND_TUNING_BLOCK; |
| 946 |
|
| 947 |
err = host->ops->execute_tuning(host, opcode); |
| 948 |
if (!err) { |
| 949 |
mmc_retune_clear(host); |
| 950 |
mmc_retune_enable(host); |
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
/* Only print error when we don't check for card removal */ |
| 955 |
if (!host->detect_change) { |
| 956 |
pr_err("%s: tuning execution failed: %d\n", |
| 957 |
mmc_hostname(host), err); |
| 958 |
mmc_debugfs_err_stats_inc(host, MMC_ERR_TUNING); |
| 959 |
} |
| 960 |
|
| 961 |
return err; |
| 962 |
} |
| 963 |
|
| 964 |
/* |
| 965 |
* Change the bus mode (open drain/push-pull) of a host. |
| 966 |
*/ |
| 967 |
void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 968 |
{ |
| 969 |
host->ios.bus_mode = mode; |
| 970 |
mmc_set_ios(host); |
| 971 |
} |
| 972 |
|
| 973 |
/* |
| 974 |
* Change data bus width of a host. |
| 975 |
*/ |
| 976 |
void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 977 |
{ |
| 978 |
host->ios.bus_width = width; |
| 979 |
mmc_set_ios(host); |
| 980 |
} |
| 981 |
|
| 982 |
/* |
| 983 |
* Set initial state after a power cycle or a hw_reset. |
| 984 |
*/ |
| 985 |
void mmc_set_initial_state(struct mmc_host *host) |
| 986 |
{ |
| 987 |
if (host->cqe_on) |
| 988 |
host->cqe_ops->cqe_off(host); |
| 989 |
|
| 990 |
mmc_retune_disable(host); |
| 991 |
|
| 992 |
if (mmc_host_is_spi(host)) |
| 993 |
host->ios.chip_select = MMC_CS_HIGH; |
| 994 |
else |
| 995 |
host->ios.chip_select = MMC_CS_DONTCARE; |
| 996 |
host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 997 |
host->ios.bus_width = MMC_BUS_WIDTH_1; |
| 998 |
host->ios.timing = MMC_TIMING_LEGACY; |
| 999 |
host->ios.drv_type = 0; |
| 1000 |
host->ios.enhanced_strobe = false; |
| 1001 |
|
| 1002 |
/* |
| 1003 |
* Make sure we are in non-enhanced strobe mode before we |
| 1004 |
* actually enable it in ext_csd. |
| 1005 |
*/ |
| 1006 |
if ((host->caps2 & MMC_CAP2_HS400_ES) && |
| 1007 |
host->ops->hs400_enhanced_strobe) |
| 1008 |
host->ops->hs400_enhanced_strobe(host, &host->ios); |
| 1009 |
|
| 1010 |
mmc_set_ios(host); |
| 1011 |
|
| 1012 |
mmc_crypto_set_initial_state(host); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** |
| 1016 |
* mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 1017 |
* @vdd: voltage (mV) |
| 1018 |
* @low_bits: prefer low bits in boundary cases |
| 1019 |
* |
| 1020 |
* This function returns the OCR bit number according to the provided @vdd |
| 1021 |
* value. If conversion is not possible a negative errno value returned. |
| 1022 |
* |
| 1023 |
* Depending on the @low_bits flag the function prefers low or high OCR bits |
| 1024 |
* on boundary voltages. For example, |
| 1025 |
* with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 1026 |
* with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 1027 |
* |
| 1028 |
* Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 1029 |
*/ |
| 1030 |
static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 1031 |
{ |
| 1032 |
const int max_bit = ilog2(MMC_VDD_35_36); |
| 1033 |
int bit; |
| 1034 |
|
| 1035 |
if (vdd < 1650 || vdd > 3600) |
| 1036 |
return -EINVAL; |
| 1037 |
|
| 1038 |
if (vdd >= 1650 && vdd <= 1950) |
| 1039 |
return ilog2(MMC_VDD_165_195); |
| 1040 |
|
| 1041 |
if (low_bits) |
| 1042 |
vdd -= 1; |
| 1043 |
|
| 1044 |
/* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 1045 |
bit = (vdd - 2000) / 100 + 8; |
| 1046 |
if (bit > max_bit) |
| 1047 |
return max_bit; |
| 1048 |
return bit; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/** |
| 1052 |
* mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 1053 |
* @vdd_min: minimum voltage value (mV) |
| 1054 |
* @vdd_max: maximum voltage value (mV) |
| 1055 |
* |
| 1056 |
* This function returns the OCR mask bits according to the provided @vdd_min |
| 1057 |
* and @vdd_max values. If conversion is not possible the function returns 0. |
| 1058 |
* |
| 1059 |
* Notes wrt boundary cases: |
| 1060 |
* This function sets the OCR bits for all boundary voltages, for example |
| 1061 |
* [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 1062 |
* MMC_VDD_34_35 mask. |
| 1063 |
*/ |
| 1064 |
u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 1065 |
{ |
| 1066 |
u32 mask = 0; |
| 1067 |
|
| 1068 |
if (vdd_max < vdd_min) |
| 1069 |
return 0; |
| 1070 |
|
| 1071 |
/* Prefer high bits for the boundary vdd_max values. */ |
| 1072 |
vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 1073 |
if (vdd_max < 0) |
| 1074 |
return 0; |
| 1075 |
|
| 1076 |
/* Prefer low bits for the boundary vdd_min values. */ |
| 1077 |
vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 1078 |
if (vdd_min < 0) |
| 1079 |
return 0; |
| 1080 |
|
| 1081 |
/* Fill the mask, from max bit to min bit. */ |
| 1082 |
while (vdd_max >= vdd_min) |
| 1083 |
mask |= 1 << vdd_max--; |
| 1084 |
|
| 1085 |
return mask; |
| 1086 |
} |
| 1087 |
|
| 1088 |
static int mmc_of_get_func_num(struct device_node *node) |
| 1089 |
{ |
| 1090 |
u32 reg; |
| 1091 |
int ret; |
| 1092 |
|
| 1093 |
ret = of_property_read_u32(node, "reg", ®); |
| 1094 |
if (ret < 0) |
| 1095 |
return ret; |
| 1096 |
|
| 1097 |
return reg; |
| 1098 |
} |
| 1099 |
|
| 1100 |
struct device_node *mmc_of_find_child_device(struct mmc_host *host, |
| 1101 |
unsigned func_num) |
| 1102 |
{ |
| 1103 |
struct device_node *node; |
| 1104 |
|
| 1105 |
if (!host->parent || !host->parent->of_node) |
| 1106 |
return NULL; |
| 1107 |
|
| 1108 |
for_each_child_of_node(host->parent->of_node, node) { |
| 1109 |
if (mmc_of_get_func_num(node) == func_num) |
| 1110 |
return node; |
| 1111 |
} |
| 1112 |
|
| 1113 |
return NULL; |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Mask off any voltages we don't support and select |
| 1118 |
* the lowest voltage |
| 1119 |
*/ |
| 1120 |
u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
| 1121 |
{ |
| 1122 |
int bit; |
| 1123 |
|
| 1124 |
/* |
| 1125 |
* Sanity check the voltages that the card claims to |
| 1126 |
* support. |
| 1127 |
*/ |
| 1128 |
if (ocr & 0x7F) { |
| 1129 |
dev_warn(mmc_dev(host), |
| 1130 |
"card claims to support voltages below defined range\n"); |
| 1131 |
ocr &= ~0x7F; |
| 1132 |
} |
| 1133 |
|
| 1134 |
ocr &= host->ocr_avail; |
| 1135 |
if (!ocr) { |
| 1136 |
dev_warn(mmc_dev(host), "no support for card's volts\n"); |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
if (!mmc_card_uhs2(host) && host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) { |
| 1141 |
bit = ffs(ocr) - 1; |
| 1142 |
ocr &= 3 << bit; |
| 1143 |
mmc_power_cycle(host, ocr); |
| 1144 |
} else { |
| 1145 |
bit = fls(ocr) - 1; |
| 1146 |
/* |
| 1147 |
* The bit variable represents the highest voltage bit set in |
| 1148 |
* the OCR register. |
| 1149 |
* To keep a range of 2 values (e.g. 3.2V/3.3V and 3.3V/3.4V), |
| 1150 |
* we must shift the mask '3' with (bit - 1). |
| 1151 |
*/ |
| 1152 |
ocr &= 3 << (bit - 1); |
| 1153 |
if (bit != host->ios.vdd) |
| 1154 |
dev_warn(mmc_dev(host), "exceeding card's volts\n"); |
| 1155 |
} |
| 1156 |
|
| 1157 |
return ocr; |
| 1158 |
} |
| 1159 |
|
| 1160 |
int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage) |
| 1161 |
{ |
| 1162 |
int err = 0; |
| 1163 |
int old_signal_voltage = host->ios.signal_voltage; |
| 1164 |
|
| 1165 |
host->ios.signal_voltage = signal_voltage; |
| 1166 |
if (host->ops->start_signal_voltage_switch) |
| 1167 |
err = host->ops->start_signal_voltage_switch(host, &host->ios); |
| 1168 |
|
| 1169 |
if (err) |
| 1170 |
host->ios.signal_voltage = old_signal_voltage; |
| 1171 |
|
| 1172 |
return err; |
| 1173 |
|
| 1174 |
} |
| 1175 |
|
| 1176 |
void mmc_set_initial_signal_voltage(struct mmc_host *host) |
| 1177 |
{ |
| 1178 |
/* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */ |
| 1179 |
if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330)) |
| 1180 |
dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n"); |
| 1181 |
else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180)) |
| 1182 |
dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n"); |
| 1183 |
else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120)) |
| 1184 |
dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n"); |
| 1185 |
} |
| 1186 |
|
| 1187 |
int mmc_host_set_uhs_voltage(struct mmc_host *host) |
| 1188 |
{ |
| 1189 |
u32 clock; |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* During a signal voltage level switch, the clock must be gated |
| 1193 |
* for 5 ms according to the SD spec |
| 1194 |
*/ |
| 1195 |
clock = host->ios.clock; |
| 1196 |
host->ios.clock = 0; |
| 1197 |
mmc_set_ios(host); |
| 1198 |
|
| 1199 |
if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180)) |
| 1200 |
return -EAGAIN; |
| 1201 |
|
| 1202 |
/* Keep clock gated for at least 10 ms, though spec only says 5 ms */ |
| 1203 |
mmc_delay(10); |
| 1204 |
host->ios.clock = clock; |
| 1205 |
mmc_set_ios(host); |
| 1206 |
|
| 1207 |
return 0; |
| 1208 |
} |
| 1209 |
|
| 1210 |
int mmc_set_uhs_voltage(struct mmc_host *host, u32 ocr) |
| 1211 |
{ |
| 1212 |
struct mmc_command cmd = {}; |
| 1213 |
int err = 0; |
| 1214 |
|
| 1215 |
/* |
| 1216 |
* If we cannot switch voltages, return failure so the caller |
| 1217 |
* can continue without UHS mode |
| 1218 |
*/ |
| 1219 |
if (!host->ops->start_signal_voltage_switch) |
| 1220 |
return -EPERM; |
| 1221 |
if (!host->ops->card_busy) |
| 1222 |
pr_warn("%s: cannot verify signal voltage switch\n", |
| 1223 |
mmc_hostname(host)); |
| 1224 |
|
| 1225 |
cmd.opcode = SD_SWITCH_VOLTAGE; |
| 1226 |
cmd.arg = 0; |
| 1227 |
cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1228 |
|
| 1229 |
err = mmc_wait_for_cmd(host, &cmd, 0); |
| 1230 |
if (err) |
| 1231 |
goto power_cycle; |
| 1232 |
|
| 1233 |
if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) |
| 1234 |
return -EIO; |
| 1235 |
|
| 1236 |
/* |
| 1237 |
* The card should drive cmd and dat[0:3] low immediately |
| 1238 |
* after the response of cmd11, but wait 1 ms to be sure |
| 1239 |
*/ |
| 1240 |
mmc_delay(1); |
| 1241 |
if (host->ops->card_busy && !host->ops->card_busy(host)) { |
| 1242 |
err = -EAGAIN; |
| 1243 |
goto power_cycle; |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (mmc_host_set_uhs_voltage(host)) { |
| 1247 |
/* |
| 1248 |
* Voltages may not have been switched, but we've already |
| 1249 |
* sent CMD11, so a power cycle is required anyway |
| 1250 |
*/ |
| 1251 |
err = -EAGAIN; |
| 1252 |
goto power_cycle; |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* Wait for at least 1 ms according to spec */ |
| 1256 |
mmc_delay(1); |
| 1257 |
|
| 1258 |
/* |
| 1259 |
* Failure to switch is indicated by the card holding |
| 1260 |
* dat[0:3] low |
| 1261 |
*/ |
| 1262 |
if (host->ops->card_busy && host->ops->card_busy(host)) |
| 1263 |
err = -EAGAIN; |
| 1264 |
|
| 1265 |
power_cycle: |
| 1266 |
if (err) { |
| 1267 |
pr_debug("%s: Signal voltage switch failed, " |
| 1268 |
"power cycling card\n", mmc_hostname(host)); |
| 1269 |
mmc_power_cycle(host, ocr); |
| 1270 |
} |
| 1271 |
|
| 1272 |
return err; |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* |
| 1276 |
* Select timing parameters for host. |
| 1277 |
*/ |
| 1278 |
void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
| 1279 |
{ |
| 1280 |
host->ios.timing = timing; |
| 1281 |
mmc_set_ios(host); |
| 1282 |
} |
| 1283 |
|
| 1284 |
/* |
| 1285 |
* Select appropriate driver type for host. |
| 1286 |
*/ |
| 1287 |
void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) |
| 1288 |
{ |
| 1289 |
host->ios.drv_type = drv_type; |
| 1290 |
mmc_set_ios(host); |
| 1291 |
} |
| 1292 |
|
| 1293 |
int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr, |
| 1294 |
int card_drv_type, int *drv_type) |
| 1295 |
{ |
| 1296 |
struct mmc_host *host = card->host; |
| 1297 |
int host_drv_type = SD_DRIVER_TYPE_B; |
| 1298 |
|
| 1299 |
*drv_type = 0; |
| 1300 |
|
| 1301 |
if (!host->ops->select_drive_strength) |
| 1302 |
return 0; |
| 1303 |
|
| 1304 |
/* Use SD definition of driver strength for hosts */ |
| 1305 |
if (host->caps & MMC_CAP_DRIVER_TYPE_A) |
| 1306 |
host_drv_type |= SD_DRIVER_TYPE_A; |
| 1307 |
|
| 1308 |
if (host->caps & MMC_CAP_DRIVER_TYPE_C) |
| 1309 |
host_drv_type |= SD_DRIVER_TYPE_C; |
| 1310 |
|
| 1311 |
if (host->caps & MMC_CAP_DRIVER_TYPE_D) |
| 1312 |
host_drv_type |= SD_DRIVER_TYPE_D; |
| 1313 |
|
| 1314 |
/* |
| 1315 |
* The drive strength that the hardware can support |
| 1316 |
* depends on the board design. Pass the appropriate |
| 1317 |
* information and let the hardware specific code |
| 1318 |
* return what is possible given the options |
| 1319 |
*/ |
| 1320 |
return host->ops->select_drive_strength(card, max_dtr, |
| 1321 |
host_drv_type, |
| 1322 |
card_drv_type, |
| 1323 |
drv_type); |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* Apply power to the MMC stack. This is a two-stage process. |
| 1328 |
* First, we enable power to the card without the clock running. |
| 1329 |
* We then wait a bit for the power to stabilise. Finally, |
| 1330 |
* enable the bus drivers and clock to the card. |
| 1331 |
* |
| 1332 |
* We must _NOT_ enable the clock prior to power stablising. |
| 1333 |
* |
| 1334 |
* If a host does all the power sequencing itself, ignore the |
| 1335 |
* initial MMC_POWER_UP stage. |
| 1336 |
*/ |
| 1337 |
void mmc_power_up(struct mmc_host *host, u32 ocr) |
| 1338 |
{ |
| 1339 |
if (host->ios.power_mode == MMC_POWER_ON) |
| 1340 |
return; |
| 1341 |
|
| 1342 |
mmc_pwrseq_pre_power_on(host); |
| 1343 |
|
| 1344 |
host->ios.vdd = fls(ocr) - 1; |
| 1345 |
host->ios.power_mode = MMC_POWER_UP; |
| 1346 |
/* Set initial state and call mmc_set_ios */ |
| 1347 |
mmc_set_initial_state(host); |
| 1348 |
|
| 1349 |
mmc_set_initial_signal_voltage(host); |
| 1350 |
|
| 1351 |
/* |
| 1352 |
* This delay should be sufficient to allow the power supply |
| 1353 |
* to reach the minimum voltage. |
| 1354 |
*/ |
| 1355 |
mmc_delay(host->ios.power_delay_ms); |
| 1356 |
|
| 1357 |
mmc_pwrseq_post_power_on(host); |
| 1358 |
|
| 1359 |
host->ios.clock = host->f_init; |
| 1360 |
|
| 1361 |
host->ios.power_mode = MMC_POWER_ON; |
| 1362 |
mmc_set_ios(host); |
| 1363 |
|
| 1364 |
/* |
| 1365 |
* This delay must be at least 74 clock sizes, or 1 ms, or the |
| 1366 |
* time required to reach a stable voltage. |
| 1367 |
*/ |
| 1368 |
mmc_delay(host->ios.power_delay_ms); |
| 1369 |
} |
| 1370 |
|
| 1371 |
void mmc_power_off(struct mmc_host *host) |
| 1372 |
{ |
| 1373 |
if (host->ios.power_mode == MMC_POWER_OFF) |
| 1374 |
return; |
| 1375 |
|
| 1376 |
mmc_pwrseq_power_off(host); |
| 1377 |
|
| 1378 |
host->ios.clock = 0; |
| 1379 |
host->ios.vdd = 0; |
| 1380 |
|
| 1381 |
host->ios.power_mode = MMC_POWER_OFF; |
| 1382 |
/* Set initial state and call mmc_set_ios */ |
| 1383 |
mmc_set_initial_state(host); |
| 1384 |
|
| 1385 |
/* |
| 1386 |
* Some configurations, such as the 802.11 SDIO card in the OLPC |
| 1387 |
* XO-1.5, require a short delay after poweroff before the card |
| 1388 |
* can be successfully turned on again. |
| 1389 |
*/ |
| 1390 |
mmc_delay(1); |
| 1391 |
} |
| 1392 |
|
| 1393 |
void mmc_power_cycle(struct mmc_host *host, u32 ocr) |
| 1394 |
{ |
| 1395 |
mmc_power_off(host); |
| 1396 |
/* Wait at least 1 ms according to SD spec */ |
| 1397 |
mmc_delay(1); |
| 1398 |
mmc_power_up(host, ocr); |
| 1399 |
} |
| 1400 |
|
| 1401 |
/** |
| 1402 |
* mmc_handle_undervoltage - Handle an undervoltage event on the MMC bus |
| 1403 |
* @host: The MMC host that detected the undervoltage condition |
| 1404 |
* |
| 1405 |
* This function is called when an undervoltage event is detected on one of |
| 1406 |
* the MMC regulators. |
| 1407 |
* |
| 1408 |
* Returns: 0 on success or a negative error code on failure. |
| 1409 |
*/ |
| 1410 |
int mmc_handle_undervoltage(struct mmc_host *host) |
| 1411 |
{ |
| 1412 |
/* Stop the host to prevent races with card removal */ |
| 1413 |
__mmc_stop_host(host); |
| 1414 |
|
| 1415 |
if (!host->bus_ops || !host->bus_ops->handle_undervoltage) |
| 1416 |
return 0; |
| 1417 |
|
| 1418 |
dev_warn(mmc_dev(host), "%s: Undervoltage detected, initiating emergency stop\n", |
| 1419 |
mmc_hostname(host)); |
| 1420 |
|
| 1421 |
return host->bus_ops->handle_undervoltage(host); |
| 1422 |
} |
| 1423 |
|
| 1424 |
/* |
| 1425 |
* Assign a mmc bus handler to a host. Only one bus handler may control a |
| 1426 |
* host at any given time. |
| 1427 |
*/ |
| 1428 |
void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) |
| 1429 |
{ |
| 1430 |
host->bus_ops = ops; |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* |
| 1434 |
* Remove the current bus handler from a host. |
| 1435 |
*/ |
| 1436 |
void mmc_detach_bus(struct mmc_host *host) |
| 1437 |
{ |
| 1438 |
host->bus_ops = NULL; |
| 1439 |
} |
| 1440 |
|
| 1441 |
void _mmc_detect_change(struct mmc_host *host, unsigned long delay, bool cd_irq) |
| 1442 |
{ |
| 1443 |
/* |
| 1444 |
* Prevent system sleep for 5s to allow user space to consume the |
| 1445 |
* corresponding uevent. This is especially useful, when CD irq is used |
| 1446 |
* as a system wakeup, but doesn't hurt in other cases. |
| 1447 |
*/ |
| 1448 |
if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL)) |
| 1449 |
__pm_wakeup_event(host->ws, 5000); |
| 1450 |
|
| 1451 |
host->detect_change = 1; |
| 1452 |
mmc_schedule_delayed_work(&host->detect, delay); |
| 1453 |
} |
| 1454 |
|
| 1455 |
/** |
| 1456 |
* mmc_detect_change - process change of state on a MMC socket |
| 1457 |
* @host: host which changed state. |
| 1458 |
* @delay: optional delay to wait before detection (jiffies) |
| 1459 |
* |
| 1460 |
* MMC drivers should call this when they detect a card has been |
| 1461 |
* inserted or removed. The MMC layer will confirm that any |
| 1462 |
* present card is still functional, and initialize any newly |
| 1463 |
* inserted. |
| 1464 |
*/ |
| 1465 |
void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
| 1466 |
{ |
| 1467 |
_mmc_detect_change(host, delay, true); |
| 1468 |
} |
| 1469 |
EXPORT_SYMBOL(mmc_detect_change); |
| 1470 |
|
| 1471 |
void mmc_init_erase(struct mmc_card *card) |
| 1472 |
{ |
| 1473 |
unsigned int sz; |
| 1474 |
|
| 1475 |
if (is_power_of_2(card->erase_size)) |
| 1476 |
card->erase_shift = ffs(card->erase_size) - 1; |
| 1477 |
else |
| 1478 |
card->erase_shift = 0; |
| 1479 |
|
| 1480 |
/* |
| 1481 |
* It is possible to erase an arbitrarily large area of an SD or MMC |
| 1482 |
* card. That is not desirable because it can take a long time |
| 1483 |
* (minutes) potentially delaying more important I/O, and also the |
| 1484 |
* timeout calculations become increasingly hugely over-estimated. |
| 1485 |
* Consequently, 'pref_erase' is defined as a guide to limit erases |
| 1486 |
* to that size and alignment. |
| 1487 |
* |
| 1488 |
* For SD cards that define Allocation Unit size, limit erases to one |
| 1489 |
* Allocation Unit at a time. |
| 1490 |
* For MMC, have a stab at ai good value and for modern cards it will |
| 1491 |
* end up being 4MiB. Note that if the value is too small, it can end |
| 1492 |
* up taking longer to erase. Also note, erase_size is already set to |
| 1493 |
* High Capacity Erase Size if available when this function is called. |
| 1494 |
*/ |
| 1495 |
if (mmc_card_sd(card) && card->ssr.au) { |
| 1496 |
card->pref_erase = card->ssr.au; |
| 1497 |
card->erase_shift = ffs(card->ssr.au) - 1; |
| 1498 |
} else if (card->erase_size) { |
| 1499 |
sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 1500 |
if (sz < 128) |
| 1501 |
card->pref_erase = 512 * 1024 / 512; |
| 1502 |
else if (sz < 512) |
| 1503 |
card->pref_erase = 1024 * 1024 / 512; |
| 1504 |
else if (sz < 1024) |
| 1505 |
card->pref_erase = 2 * 1024 * 1024 / 512; |
| 1506 |
else |
| 1507 |
card->pref_erase = 4 * 1024 * 1024 / 512; |
| 1508 |
if (card->pref_erase < card->erase_size) |
| 1509 |
card->pref_erase = card->erase_size; |
| 1510 |
else { |
| 1511 |
sz = card->pref_erase % card->erase_size; |
| 1512 |
if (sz) |
| 1513 |
card->pref_erase += card->erase_size - sz; |
| 1514 |
} |
| 1515 |
} else |
| 1516 |
card->pref_erase = 0; |
| 1517 |
} |
| 1518 |
|
| 1519 |
static bool is_trim_arg(unsigned int arg) |
| 1520 |
{ |
| 1521 |
return (arg & MMC_TRIM_OR_DISCARD_ARGS) && arg != MMC_DISCARD_ARG; |
| 1522 |
} |
| 1523 |
|
| 1524 |
static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, |
| 1525 |
unsigned int arg, unsigned int qty) |
| 1526 |
{ |
| 1527 |
unsigned int erase_timeout; |
| 1528 |
|
| 1529 |
if (arg == MMC_DISCARD_ARG || |
| 1530 |
(arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) { |
| 1531 |
erase_timeout = card->ext_csd.trim_timeout; |
| 1532 |
} else if (card->ext_csd.erase_group_def & 1) { |
| 1533 |
/* High Capacity Erase Group Size uses HC timeouts */ |
| 1534 |
if (arg == MMC_TRIM_ARG) |
| 1535 |
erase_timeout = card->ext_csd.trim_timeout; |
| 1536 |
else |
| 1537 |
erase_timeout = card->ext_csd.hc_erase_timeout; |
| 1538 |
} else { |
| 1539 |
/* CSD Erase Group Size uses write timeout */ |
| 1540 |
unsigned int mult = (10 << card->csd.r2w_factor); |
| 1541 |
unsigned int timeout_clks = card->csd.taac_clks * mult; |
| 1542 |
unsigned int timeout_us; |
| 1543 |
|
| 1544 |
/* Avoid overflow: e.g. taac_ns=80000000 mult=1280 */ |
| 1545 |
if (card->csd.taac_ns < 1000000) |
| 1546 |
timeout_us = (card->csd.taac_ns * mult) / 1000; |
| 1547 |
else |
| 1548 |
timeout_us = (card->csd.taac_ns / 1000) * mult; |
| 1549 |
|
| 1550 |
/* |
| 1551 |
* ios.clock is only a target. The real clock rate might be |
| 1552 |
* less but not that much less, so fudge it by multiplying by 2. |
| 1553 |
*/ |
| 1554 |
timeout_clks <<= 1; |
| 1555 |
timeout_us += (timeout_clks * 1000) / |
| 1556 |
(card->host->ios.clock / 1000); |
| 1557 |
|
| 1558 |
erase_timeout = timeout_us / 1000; |
| 1559 |
|
| 1560 |
/* |
| 1561 |
* Theoretically, the calculation could underflow so round up |
| 1562 |
* to 1ms in that case. |
| 1563 |
*/ |
| 1564 |
if (!erase_timeout) |
| 1565 |
erase_timeout = 1; |
| 1566 |
} |
| 1567 |
|
| 1568 |
/* Multiplier for secure operations */ |
| 1569 |
if (arg & MMC_SECURE_ARGS) { |
| 1570 |
if (arg == MMC_SECURE_ERASE_ARG) |
| 1571 |
erase_timeout *= card->ext_csd.sec_erase_mult; |
| 1572 |
else |
| 1573 |
erase_timeout *= card->ext_csd.sec_trim_mult; |
| 1574 |
} |
| 1575 |
|
| 1576 |
erase_timeout *= qty; |
| 1577 |
|
| 1578 |
/* |
| 1579 |
* Ensure at least a 1 second timeout for SPI as per |
| 1580 |
* 'mmc_set_data_timeout()' |
| 1581 |
*/ |
| 1582 |
if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 1583 |
erase_timeout = 1000; |
| 1584 |
|
| 1585 |
return erase_timeout; |
| 1586 |
} |
| 1587 |
|
| 1588 |
static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, |
| 1589 |
unsigned int arg, |
| 1590 |
unsigned int qty) |
| 1591 |
{ |
| 1592 |
unsigned int erase_timeout; |
| 1593 |
|
| 1594 |
/* for DISCARD none of the below calculation applies. |
| 1595 |
* the busy timeout is 250msec per discard command. |
| 1596 |
*/ |
| 1597 |
if (arg == SD_DISCARD_ARG) |
| 1598 |
return SD_DISCARD_TIMEOUT_MS; |
| 1599 |
|
| 1600 |
if (card->ssr.erase_timeout) { |
| 1601 |
/* Erase timeout specified in SD Status Register (SSR) */ |
| 1602 |
erase_timeout = card->ssr.erase_timeout * qty + |
| 1603 |
card->ssr.erase_offset; |
| 1604 |
} else { |
| 1605 |
/* |
| 1606 |
* Erase timeout not specified in SD Status Register (SSR) so |
| 1607 |
* use 250ms per write block. |
| 1608 |
*/ |
| 1609 |
erase_timeout = 250 * qty; |
| 1610 |
} |
| 1611 |
|
| 1612 |
/* Must not be less than 1 second */ |
| 1613 |
if (erase_timeout < 1000) |
| 1614 |
erase_timeout = 1000; |
| 1615 |
|
| 1616 |
return erase_timeout; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static unsigned int mmc_erase_timeout(struct mmc_card *card, |
| 1620 |
unsigned int arg, |
| 1621 |
unsigned int qty) |
| 1622 |
{ |
| 1623 |
if (mmc_card_sd(card)) |
| 1624 |
return mmc_sd_erase_timeout(card, arg, qty); |
| 1625 |
else |
| 1626 |
return mmc_mmc_erase_timeout(card, arg, qty); |
| 1627 |
} |
| 1628 |
|
| 1629 |
static int mmc_do_erase(struct mmc_card *card, sector_t from, |
| 1630 |
sector_t to, unsigned int arg) |
| 1631 |
{ |
| 1632 |
struct mmc_command cmd = {}; |
| 1633 |
unsigned int qty = 0, busy_timeout = 0; |
| 1634 |
bool use_r1b_resp; |
| 1635 |
int err; |
| 1636 |
|
| 1637 |
mmc_retune_hold(card->host); |
| 1638 |
|
| 1639 |
/* |
| 1640 |
* qty is used to calculate the erase timeout which depends on how many |
| 1641 |
* erase groups (or allocation units in SD terminology) are affected. |
| 1642 |
* We count erasing part of an erase group as one erase group. |
| 1643 |
* For SD, the allocation units are always a power of 2. For MMC, the |
| 1644 |
* erase group size is almost certainly also power of 2, but it does not |
| 1645 |
* seem to insist on that in the JEDEC standard, so we fall back to |
| 1646 |
* division in that case. SD may not specify an allocation unit size, |
| 1647 |
* in which case the timeout is based on the number of write blocks. |
| 1648 |
* |
| 1649 |
* Note that the timeout for secure trim 2 will only be correct if the |
| 1650 |
* number of erase groups specified is the same as the total of all |
| 1651 |
* preceding secure trim 1 commands. Since the power may have been |
| 1652 |
* lost since the secure trim 1 commands occurred, it is generally |
| 1653 |
* impossible to calculate the secure trim 2 timeout correctly. |
| 1654 |
*/ |
| 1655 |
if (card->erase_shift) |
| 1656 |
qty += ((to >> card->erase_shift) - |
| 1657 |
(from >> card->erase_shift)) + 1; |
| 1658 |
else if (mmc_card_sd(card)) |
| 1659 |
qty += to - from + 1; |
| 1660 |
else |
| 1661 |
qty += (mmc_sector_div(to, card->erase_size) - |
| 1662 |
mmc_sector_div(from, card->erase_size)) + 1; |
| 1663 |
|
| 1664 |
if (!mmc_card_blockaddr(card)) { |
| 1665 |
from <<= 9; |
| 1666 |
to <<= 9; |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (mmc_card_sd(card)) |
| 1670 |
cmd.opcode = SD_ERASE_WR_BLK_START; |
| 1671 |
else |
| 1672 |
cmd.opcode = MMC_ERASE_GROUP_START; |
| 1673 |
cmd.arg = from; |
| 1674 |
cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1675 |
|
| 1676 |
if (mmc_card_ult_capacity(card)) { |
| 1677 |
cmd.ext_addr = from >> 32; |
| 1678 |
cmd.has_ext_addr = true; |
| 1679 |
} |
| 1680 |
|
| 1681 |
err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1682 |
if (err) { |
| 1683 |
pr_err("mmc_erase: group start error %d, " |
| 1684 |
"status %#x\n", err, cmd.resp[0]); |
| 1685 |
err = -EIO; |
| 1686 |
goto out; |
| 1687 |
} |
| 1688 |
|
| 1689 |
memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1690 |
if (mmc_card_sd(card)) |
| 1691 |
cmd.opcode = SD_ERASE_WR_BLK_END; |
| 1692 |
else |
| 1693 |
cmd.opcode = MMC_ERASE_GROUP_END; |
| 1694 |
cmd.arg = to; |
| 1695 |
cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1696 |
|
| 1697 |
if (mmc_card_ult_capacity(card)) { |
| 1698 |
cmd.ext_addr = to >> 32; |
| 1699 |
cmd.has_ext_addr = true; |
| 1700 |
} |
| 1701 |
|
| 1702 |
err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1703 |
if (err) { |
| 1704 |
pr_err("mmc_erase: group end error %d, status %#x\n", |
| 1705 |
err, cmd.resp[0]); |
| 1706 |
err = -EIO; |
| 1707 |
goto out; |
| 1708 |
} |
| 1709 |
|
| 1710 |
memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1711 |
cmd.opcode = MMC_ERASE; |
| 1712 |
cmd.arg = arg; |
| 1713 |
busy_timeout = mmc_erase_timeout(card, arg, qty); |
| 1714 |
use_r1b_resp = mmc_prepare_busy_cmd(card->host, &cmd, busy_timeout); |
| 1715 |
|
| 1716 |
err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1717 |
if (err) { |
| 1718 |
pr_err("mmc_erase: erase error %d, status %#x\n", |
| 1719 |
err, cmd.resp[0]); |
| 1720 |
err = -EIO; |
| 1721 |
goto out; |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (mmc_host_is_spi(card->host)) |
| 1725 |
goto out; |
| 1726 |
|
| 1727 |
/* |
| 1728 |
* In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling |
| 1729 |
* shall be avoided. |
| 1730 |
*/ |
| 1731 |
if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) |
| 1732 |
goto out; |
| 1733 |
|
| 1734 |
/* Let's poll to find out when the erase operation completes. */ |
| 1735 |
err = mmc_poll_for_busy(card, busy_timeout, false, MMC_BUSY_ERASE); |
| 1736 |
|
| 1737 |
out: |
| 1738 |
mmc_retune_release(card->host); |
| 1739 |
return err; |
| 1740 |
} |
| 1741 |
|
| 1742 |
static unsigned int mmc_align_erase_size(struct mmc_card *card, |
| 1743 |
sector_t *from, |
| 1744 |
sector_t *to, |
| 1745 |
unsigned int nr) |
| 1746 |
{ |
| 1747 |
sector_t from_new = *from; |
| 1748 |
unsigned int nr_new = nr, rem; |
| 1749 |
|
| 1750 |
/* |
| 1751 |
* When the 'card->erase_size' is power of 2, we can use round_up/down() |
| 1752 |
* to align the erase size efficiently. |
| 1753 |
*/ |
| 1754 |
if (is_power_of_2(card->erase_size)) { |
| 1755 |
sector_t temp = from_new; |
| 1756 |
|
| 1757 |
from_new = round_up(temp, card->erase_size); |
| 1758 |
rem = from_new - temp; |
| 1759 |
|
| 1760 |
if (nr_new > rem) |
| 1761 |
nr_new -= rem; |
| 1762 |
else |
| 1763 |
return 0; |
| 1764 |
|
| 1765 |
nr_new = round_down(nr_new, card->erase_size); |
| 1766 |
} else { |
| 1767 |
rem = mmc_sector_mod(from_new, card->erase_size); |
| 1768 |
if (rem) { |
| 1769 |
rem = card->erase_size - rem; |
| 1770 |
from_new += rem; |
| 1771 |
if (nr_new > rem) |
| 1772 |
nr_new -= rem; |
| 1773 |
else |
| 1774 |
return 0; |
| 1775 |
} |
| 1776 |
|
| 1777 |
rem = nr_new % card->erase_size; |
| 1778 |
if (rem) |
| 1779 |
nr_new -= rem; |
| 1780 |
} |
| 1781 |
|
| 1782 |
if (nr_new == 0) |
| 1783 |
return 0; |
| 1784 |
|
| 1785 |
*to = from_new + nr_new; |
| 1786 |
*from = from_new; |
| 1787 |
|
| 1788 |
return nr_new; |
| 1789 |
} |
| 1790 |
|
| 1791 |
/** |
| 1792 |
* mmc_erase - erase sectors. |
| 1793 |
* @card: card to erase |
| 1794 |
* @from: first sector to erase |
| 1795 |
* @nr: number of sectors to erase |
| 1796 |
* @arg: erase command argument |
| 1797 |
* |
| 1798 |
* Caller must claim host before calling this function. |
| 1799 |
*/ |
| 1800 |
int mmc_erase(struct mmc_card *card, sector_t from, unsigned int nr, |
| 1801 |
unsigned int arg) |
| 1802 |
{ |
| 1803 |
unsigned int rem; |
| 1804 |
sector_t to = from + nr; |
| 1805 |
|
| 1806 |
int err; |
| 1807 |
|
| 1808 |
if (!(card->csd.cmdclass & CCC_ERASE)) |
| 1809 |
return -EOPNOTSUPP; |
| 1810 |
|
| 1811 |
if (!card->erase_size) |
| 1812 |
return -EOPNOTSUPP; |
| 1813 |
|
| 1814 |
if (mmc_card_sd(card) && arg != SD_ERASE_ARG && arg != SD_DISCARD_ARG) |
| 1815 |
return -EOPNOTSUPP; |
| 1816 |
|
| 1817 |
if (mmc_card_mmc(card) && (arg & MMC_SECURE_ARGS) && |
| 1818 |
!(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 1819 |
return -EOPNOTSUPP; |
| 1820 |
|
| 1821 |
if (mmc_card_mmc(card) && is_trim_arg(arg) && |
| 1822 |
!(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 1823 |
return -EOPNOTSUPP; |
| 1824 |
|
| 1825 |
if (arg == MMC_SECURE_ERASE_ARG) { |
| 1826 |
if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size) |
| 1827 |
return -EINVAL; |
| 1828 |
} |
| 1829 |
|
| 1830 |
if (arg == MMC_ERASE_ARG) |
| 1831 |
nr = mmc_align_erase_size(card, &from, &to, nr); |
| 1832 |
|
| 1833 |
if (nr == 0) |
| 1834 |
return 0; |
| 1835 |
|
| 1836 |
if (to <= from) |
| 1837 |
return -EINVAL; |
| 1838 |
|
| 1839 |
/* 'from' and 'to' are inclusive */ |
| 1840 |
to -= 1; |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Special case where only one erase-group fits in the timeout budget: |
| 1844 |
* If the region crosses an erase-group boundary on this particular |
| 1845 |
* case, we will be trimming more than one erase-group which, does not |
| 1846 |
* fit in the timeout budget of the controller, so we need to split it |
| 1847 |
* and call mmc_do_erase() twice if necessary. This special case is |
| 1848 |
* identified by the card->eg_boundary flag. |
| 1849 |
*/ |
| 1850 |
rem = card->erase_size - mmc_sector_mod(from, card->erase_size); |
| 1851 |
if ((arg & MMC_TRIM_OR_DISCARD_ARGS) && card->eg_boundary && nr > rem) { |
| 1852 |
err = mmc_do_erase(card, from, from + rem - 1, arg); |
| 1853 |
from += rem; |
| 1854 |
if ((err) || (to <= from)) |
| 1855 |
return err; |
| 1856 |
} |
| 1857 |
|
| 1858 |
return mmc_do_erase(card, from, to, arg); |
| 1859 |
} |
| 1860 |
EXPORT_SYMBOL(mmc_erase); |
| 1861 |
|
| 1862 |
bool mmc_card_can_erase(struct mmc_card *card) |
| 1863 |
{ |
| 1864 |
return (card->csd.cmdclass & CCC_ERASE && card->erase_size); |
| 1865 |
} |
| 1866 |
EXPORT_SYMBOL(mmc_card_can_erase); |
| 1867 |
|
| 1868 |
bool mmc_card_can_trim(struct mmc_card *card) |
| 1869 |
{ |
| 1870 |
return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) && |
| 1871 |
(!(card->quirks & MMC_QUIRK_TRIM_BROKEN))); |
| 1872 |
} |
| 1873 |
EXPORT_SYMBOL(mmc_card_can_trim); |
| 1874 |
|
| 1875 |
bool mmc_card_can_discard(struct mmc_card *card) |
| 1876 |
{ |
| 1877 |
/* |
| 1878 |
* As there's no way to detect the discard support bit at v4.5 |
| 1879 |
* use the s/w feature support filed. |
| 1880 |
*/ |
| 1881 |
return (card->ext_csd.feature_support & MMC_DISCARD_FEATURE); |
| 1882 |
} |
| 1883 |
EXPORT_SYMBOL(mmc_card_can_discard); |
| 1884 |
|
| 1885 |
bool mmc_card_can_sanitize(struct mmc_card *card) |
| 1886 |
{ |
| 1887 |
if (!mmc_card_can_trim(card) && !mmc_card_can_erase(card)) |
| 1888 |
return false; |
| 1889 |
if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) |
| 1890 |
return true; |
| 1891 |
return false; |
| 1892 |
} |
| 1893 |
|
| 1894 |
bool mmc_card_can_secure_erase_trim(struct mmc_card *card) |
| 1895 |
{ |
| 1896 |
return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) && |
| 1897 |
!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN)); |
| 1898 |
} |
| 1899 |
EXPORT_SYMBOL(mmc_card_can_secure_erase_trim); |
| 1900 |
|
| 1901 |
bool mmc_card_can_cmd23(struct mmc_card *card) |
| 1902 |
{ |
| 1903 |
return ((mmc_card_mmc(card) && |
| 1904 |
card->csd.mmca_vsn >= CSD_SPEC_VER_3) || |
| 1905 |
(mmc_card_sd(card) && !mmc_card_ult_capacity(card) && |
| 1906 |
card->scr.cmds & SD_SCR_CMD23_SUPPORT)); |
| 1907 |
} |
| 1908 |
EXPORT_SYMBOL(mmc_card_can_cmd23); |
| 1909 |
|
| 1910 |
int mmc_erase_group_aligned(struct mmc_card *card, sector_t from, |
| 1911 |
unsigned int nr) |
| 1912 |
{ |
| 1913 |
if (!card->erase_size) |
| 1914 |
return 0; |
| 1915 |
if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size) |
| 1916 |
return 0; |
| 1917 |
return 1; |
| 1918 |
} |
| 1919 |
EXPORT_SYMBOL(mmc_erase_group_aligned); |
| 1920 |
|
| 1921 |
static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, |
| 1922 |
unsigned int arg) |
| 1923 |
{ |
| 1924 |
struct mmc_host *host = card->host; |
| 1925 |
unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout; |
| 1926 |
unsigned int last_timeout = 0; |
| 1927 |
unsigned int max_busy_timeout = host->max_busy_timeout ? |
| 1928 |
host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS; |
| 1929 |
|
| 1930 |
if (card->erase_shift) { |
| 1931 |
max_qty = UINT_MAX >> card->erase_shift; |
| 1932 |
min_qty = card->pref_erase >> card->erase_shift; |
| 1933 |
} else if (mmc_card_sd(card)) { |
| 1934 |
max_qty = UINT_MAX; |
| 1935 |
min_qty = card->pref_erase; |
| 1936 |
} else { |
| 1937 |
max_qty = UINT_MAX / card->erase_size; |
| 1938 |
min_qty = card->pref_erase / card->erase_size; |
| 1939 |
} |
| 1940 |
|
| 1941 |
/* |
| 1942 |
* We should not only use 'host->max_busy_timeout' as the limitation |
| 1943 |
* when deciding the max discard sectors. We should set a balance value |
| 1944 |
* to improve the erase speed, and it can not get too long timeout at |
| 1945 |
* the same time. |
| 1946 |
* |
| 1947 |
* Here we set 'card->pref_erase' as the minimal discard sectors no |
| 1948 |
* matter what size of 'host->max_busy_timeout', but if the |
| 1949 |
* 'host->max_busy_timeout' is large enough for more discard sectors, |
| 1950 |
* then we can continue to increase the max discard sectors until we |
| 1951 |
* get a balance value. In cases when the 'host->max_busy_timeout' |
| 1952 |
* isn't specified, use the default max erase timeout. |
| 1953 |
*/ |
| 1954 |
do { |
| 1955 |
y = 0; |
| 1956 |
for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { |
| 1957 |
timeout = mmc_erase_timeout(card, arg, qty + x); |
| 1958 |
|
| 1959 |
if (qty + x > min_qty && timeout > max_busy_timeout) |
| 1960 |
break; |
| 1961 |
|
| 1962 |
if (timeout < last_timeout) |
| 1963 |
break; |
| 1964 |
last_timeout = timeout; |
| 1965 |
y = x; |
| 1966 |
} |
| 1967 |
qty += y; |
| 1968 |
} while (y); |
| 1969 |
|
| 1970 |
if (!qty) |
| 1971 |
return 0; |
| 1972 |
|
| 1973 |
/* |
| 1974 |
* When specifying a sector range to trim, chances are we might cross |
| 1975 |
* an erase-group boundary even if the amount of sectors is less than |
| 1976 |
* one erase-group. |
| 1977 |
* If we can only fit one erase-group in the controller timeout budget, |
| 1978 |
* we have to care that erase-group boundaries are not crossed by a |
| 1979 |
* single trim operation. We flag that special case with "eg_boundary". |
| 1980 |
* In all other cases we can just decrement qty and pretend that we |
| 1981 |
* always touch (qty + 1) erase-groups as a simple optimization. |
| 1982 |
*/ |
| 1983 |
if (qty == 1) |
| 1984 |
card->eg_boundary = 1; |
| 1985 |
else |
| 1986 |
qty--; |
| 1987 |
|
| 1988 |
/* Convert qty to sectors */ |
| 1989 |
if (card->erase_shift) |
| 1990 |
max_discard = qty << card->erase_shift; |
| 1991 |
else if (mmc_card_sd(card)) |
| 1992 |
max_discard = qty + 1; |
| 1993 |
else |
| 1994 |
max_discard = qty * card->erase_size; |
| 1995 |
|
| 1996 |
return max_discard; |
| 1997 |
} |
| 1998 |
|
| 1999 |
unsigned int mmc_calc_max_discard(struct mmc_card *card) |
| 2000 |
{ |
| 2001 |
struct mmc_host *host = card->host; |
| 2002 |
unsigned int max_discard, max_trim; |
| 2003 |
|
| 2004 |
/* |
| 2005 |
* Without erase_group_def set, MMC erase timeout depends on clock |
| 2006 |
* frequence which can change. In that case, the best choice is |
| 2007 |
* just the preferred erase size. |
| 2008 |
*/ |
| 2009 |
if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) |
| 2010 |
return card->pref_erase; |
| 2011 |
|
| 2012 |
max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); |
| 2013 |
if (mmc_card_can_trim(card)) { |
| 2014 |
max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); |
| 2015 |
if (max_trim < max_discard || max_discard == 0) |
| 2016 |
max_discard = max_trim; |
| 2017 |
} else if (max_discard < card->erase_size) { |
| 2018 |
max_discard = 0; |
| 2019 |
} |
| 2020 |
pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", |
| 2021 |
mmc_hostname(host), max_discard, host->max_busy_timeout ? |
| 2022 |
host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS); |
| 2023 |
return max_discard; |
| 2024 |
} |
| 2025 |
EXPORT_SYMBOL(mmc_calc_max_discard); |
| 2026 |
|
| 2027 |
bool mmc_card_is_blockaddr(struct mmc_card *card) |
| 2028 |
{ |
| 2029 |
return card ? mmc_card_blockaddr(card) : false; |
| 2030 |
} |
| 2031 |
EXPORT_SYMBOL(mmc_card_is_blockaddr); |
| 2032 |
|
| 2033 |
int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) |
| 2034 |
{ |
| 2035 |
struct mmc_command cmd = {}; |
| 2036 |
|
| 2037 |
if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) || |
| 2038 |
mmc_card_hs400(card) || mmc_card_hs400es(card)) |
| 2039 |
return 0; |
| 2040 |
|
| 2041 |
cmd.opcode = MMC_SET_BLOCKLEN; |
| 2042 |
cmd.arg = blocklen; |
| 2043 |
cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 2044 |
return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 2045 |
} |
| 2046 |
EXPORT_SYMBOL(mmc_set_blocklen); |
| 2047 |
|
| 2048 |
static void mmc_hw_reset_for_init(struct mmc_host *host) |
| 2049 |
{ |
| 2050 |
mmc_pwrseq_reset(host); |
| 2051 |
|
| 2052 |
if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->card_hw_reset) |
| 2053 |
return; |
| 2054 |
host->ops->card_hw_reset(host); |
| 2055 |
} |
| 2056 |
|
| 2057 |
/** |
| 2058 |
* mmc_hw_reset - reset the card in hardware |
| 2059 |
* @card: card to be reset |
| 2060 |
* |
| 2061 |
* Hard reset the card. This function is only for upper layers, like the |
| 2062 |
* block layer or card drivers. You cannot use it in host drivers (struct |
| 2063 |
* mmc_card might be gone then). |
| 2064 |
* |
| 2065 |
* Return: 0 on success, -errno on failure |
| 2066 |
*/ |
| 2067 |
int mmc_hw_reset(struct mmc_card *card) |
| 2068 |
{ |
| 2069 |
struct mmc_host *host = card->host; |
| 2070 |
int ret; |
| 2071 |
|
| 2072 |
ret = host->bus_ops->hw_reset(host); |
| 2073 |
if (ret < 0) |
| 2074 |
pr_warn("%s: tried to HW reset card, got error %d\n", |
| 2075 |
mmc_hostname(host), ret); |
| 2076 |
|
| 2077 |
return ret; |
| 2078 |
} |
| 2079 |
EXPORT_SYMBOL(mmc_hw_reset); |
| 2080 |
|
| 2081 |
int mmc_sw_reset(struct mmc_card *card) |
| 2082 |
{ |
| 2083 |
struct mmc_host *host = card->host; |
| 2084 |
int ret; |
| 2085 |
|
| 2086 |
if (!host->bus_ops->sw_reset) |
| 2087 |
return -EOPNOTSUPP; |
| 2088 |
|
| 2089 |
ret = host->bus_ops->sw_reset(host); |
| 2090 |
if (ret) |
| 2091 |
pr_warn("%s: tried to SW reset card, got error %d\n", |
| 2092 |
mmc_hostname(host), ret); |
| 2093 |
|
| 2094 |
return ret; |
| 2095 |
} |
| 2096 |
EXPORT_SYMBOL(mmc_sw_reset); |
| 2097 |
|
| 2098 |
static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) |
| 2099 |
{ |
| 2100 |
host->f_init = freq; |
| 2101 |
|
| 2102 |
pr_debug("%s: %s: trying to init card at %u Hz\n", |
| 2103 |
mmc_hostname(host), __func__, host->f_init); |
| 2104 |
|
| 2105 |
mmc_power_up(host, host->ocr_avail); |
| 2106 |
|
| 2107 |
/* |
| 2108 |
* Some eMMCs (with VCCQ always on) may not be reset after power up, so |
| 2109 |
* do a hardware reset if possible. |
| 2110 |
*/ |
| 2111 |
mmc_hw_reset_for_init(host); |
| 2112 |
|
| 2113 |
/* |
| 2114 |
* sdio_reset sends CMD52 to reset card. Since we do not know |
| 2115 |
* if the card is being re-initialized, just send it. CMD52 |
| 2116 |
* should be ignored by SD/eMMC cards. |
| 2117 |
* Skip it if we already know that we do not support SDIO commands |
| 2118 |
*/ |
| 2119 |
if (!(host->caps2 & MMC_CAP2_NO_SDIO)) |
| 2120 |
sdio_reset(host); |
| 2121 |
|
| 2122 |
mmc_go_idle(host); |
| 2123 |
|
| 2124 |
if (!(host->caps2 & MMC_CAP2_NO_SD)) { |
| 2125 |
if (mmc_send_if_cond_pcie(host, host->ocr_avail)) |
| 2126 |
goto out; |
| 2127 |
if (mmc_card_sd_express(host)) |
| 2128 |
return 0; |
| 2129 |
} |
| 2130 |
|
| 2131 |
/* Order's important: probe SDIO, then SD, then MMC */ |
| 2132 |
if (!(host->caps2 & MMC_CAP2_NO_SDIO)) |
| 2133 |
if (!mmc_attach_sdio(host)) |
| 2134 |
return 0; |
| 2135 |
|
| 2136 |
if (!(host->caps2 & MMC_CAP2_NO_SD)) |
| 2137 |
if (!mmc_attach_sd(host)) |
| 2138 |
return 0; |
| 2139 |
|
| 2140 |
if (!(host->caps2 & MMC_CAP2_NO_MMC)) |
| 2141 |
if (!mmc_attach_mmc(host)) |
| 2142 |
return 0; |
| 2143 |
|
| 2144 |
out: |
| 2145 |
mmc_power_off(host); |
| 2146 |
return -EIO; |
| 2147 |
} |
| 2148 |
|
| 2149 |
int _mmc_detect_card_removed(struct mmc_host *host) |
| 2150 |
{ |
| 2151 |
int ret; |
| 2152 |
|
| 2153 |
if (!host->card || mmc_card_removed(host->card)) |
| 2154 |
return 1; |
| 2155 |
|
| 2156 |
ret = host->bus_ops->alive(host); |
| 2157 |
|
| 2158 |
/* |
| 2159 |
* Card detect status and alive check may be out of sync if card is |
| 2160 |
* removed slowly, when card detect switch changes while card/slot |
| 2161 |
* pads are still contacted in hardware (refer to "SD Card Mechanical |
| 2162 |
* Addendum, Appendix C: Card Detection Switch"). So reschedule a |
| 2163 |
* detect work 200ms later for this case. |
| 2164 |
*/ |
| 2165 |
if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) { |
| 2166 |
mmc_detect_change(host, msecs_to_jiffies(200)); |
| 2167 |
pr_debug("%s: card removed too slowly\n", mmc_hostname(host)); |
| 2168 |
} |
| 2169 |
|
| 2170 |
if (ret) { |
| 2171 |
mmc_card_set_removed(host->card); |
| 2172 |
pr_debug("%s: card remove detected\n", mmc_hostname(host)); |
| 2173 |
} |
| 2174 |
|
| 2175 |
return ret; |
| 2176 |
} |
| 2177 |
|
| 2178 |
int mmc_detect_card_removed(struct mmc_host *host) |
| 2179 |
{ |
| 2180 |
struct mmc_card *card = host->card; |
| 2181 |
int ret; |
| 2182 |
|
| 2183 |
WARN_ON(!host->claimed); |
| 2184 |
|
| 2185 |
if (!card) |
| 2186 |
return 1; |
| 2187 |
|
| 2188 |
if (!mmc_card_is_removable(host)) |
| 2189 |
return 0; |
| 2190 |
|
| 2191 |
ret = mmc_card_removed(card); |
| 2192 |
/* |
| 2193 |
* The card will be considered unchanged unless we have been asked to |
| 2194 |
* detect a change or host requires polling to provide card detection. |
| 2195 |
*/ |
| 2196 |
if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL)) |
| 2197 |
return ret; |
| 2198 |
|
| 2199 |
host->detect_change = 0; |
| 2200 |
if (!ret) { |
| 2201 |
ret = _mmc_detect_card_removed(host); |
| 2202 |
if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) { |
| 2203 |
/* |
| 2204 |
* Schedule a detect work as soon as possible to let a |
| 2205 |
* rescan handle the card removal. |
| 2206 |
*/ |
| 2207 |
cancel_delayed_work(&host->detect); |
| 2208 |
_mmc_detect_change(host, 0, false); |
| 2209 |
} |
| 2210 |
} |
| 2211 |
|
| 2212 |
return ret; |
| 2213 |
} |
| 2214 |
EXPORT_SYMBOL(mmc_detect_card_removed); |
| 2215 |
|
| 2216 |
int mmc_card_alternative_gpt_sector(struct mmc_card *card, sector_t *gpt_sector) |
| 2217 |
{ |
| 2218 |
unsigned int boot_sectors_num; |
| 2219 |
|
| 2220 |
if ((!(card->host->caps2 & MMC_CAP2_ALT_GPT_TEGRA))) |
| 2221 |
return -EOPNOTSUPP; |
| 2222 |
|
| 2223 |
/* filter out unrelated cards */ |
| 2224 |
if (card->ext_csd.rev < 3 || |
| 2225 |
!mmc_card_mmc(card) || |
| 2226 |
!mmc_card_is_blockaddr(card) || |
| 2227 |
mmc_card_is_removable(card->host)) |
| 2228 |
return -ENOENT; |
| 2229 |
|
| 2230 |
/* |
| 2231 |
* eMMC storage has two special boot partitions in addition to the |
| 2232 |
* main one. NVIDIA's bootloader linearizes eMMC boot0->boot1->main |
| 2233 |
* accesses, this means that the partition table addresses are shifted |
| 2234 |
* by the size of boot partitions. In accordance with the eMMC |
| 2235 |
* specification, the boot partition size is calculated as follows: |
| 2236 |
* |
| 2237 |
* boot partition size = 128K byte x BOOT_SIZE_MULT |
| 2238 |
* |
| 2239 |
* Calculate number of sectors occupied by the both boot partitions. |
| 2240 |
*/ |
| 2241 |
boot_sectors_num = card->ext_csd.raw_boot_mult * SZ_128K / |
| 2242 |
SZ_512 * MMC_NUM_BOOT_PARTITION; |
| 2243 |
|
| 2244 |
/* Defined by NVIDIA and used by Android devices. */ |
| 2245 |
*gpt_sector = card->ext_csd.sectors - boot_sectors_num - 1; |
| 2246 |
|
| 2247 |
return 0; |
| 2248 |
} |
| 2249 |
EXPORT_SYMBOL(mmc_card_alternative_gpt_sector); |
| 2250 |
|
| 2251 |
void mmc_rescan(struct work_struct *work) |
| 2252 |
{ |
| 2253 |
struct mmc_host *host = |
| 2254 |
container_of(work, struct mmc_host, detect.work); |
| 2255 |
int i; |
| 2256 |
|
| 2257 |
if (host->rescan_disable) |
| 2258 |
return; |
| 2259 |
|
| 2260 |
/* If there is a non-removable card registered, only scan once */ |
| 2261 |
if (!mmc_card_is_removable(host) && host->rescan_entered) |
| 2262 |
return; |
| 2263 |
host->rescan_entered = 1; |
| 2264 |
|
| 2265 |
if (host->trigger_card_event && host->ops->card_event) { |
| 2266 |
mmc_claim_host(host); |
| 2267 |
host->ops->card_event(host); |
| 2268 |
mmc_release_host(host); |
| 2269 |
host->trigger_card_event = false; |
| 2270 |
} |
| 2271 |
|
| 2272 |
/* Verify a registered card to be functional, else remove it. */ |
| 2273 |
if (host->bus_ops) |
| 2274 |
host->bus_ops->detect(host); |
| 2275 |
|
| 2276 |
host->detect_change = 0; |
| 2277 |
|
| 2278 |
/* if there still is a card present, stop here */ |
| 2279 |
if (host->bus_ops != NULL) |
| 2280 |
goto out; |
| 2281 |
|
| 2282 |
mmc_claim_host(host); |
| 2283 |
if (mmc_card_is_removable(host) && host->ops->get_cd && |
| 2284 |
host->ops->get_cd(host) == 0) { |
| 2285 |
mmc_power_off(host); |
| 2286 |
mmc_release_host(host); |
| 2287 |
goto out; |
| 2288 |
} |
| 2289 |
|
| 2290 |
/* If an SD express card is present, then leave it as is. */ |
| 2291 |
if (mmc_card_sd_express(host)) { |
| 2292 |
mmc_release_host(host); |
| 2293 |
goto out; |
| 2294 |
} |
| 2295 |
|
| 2296 |
/* |
| 2297 |
* Ideally we should favor initialization of legacy SD cards and defer |
| 2298 |
* UHS-II enumeration. However, it seems like cards doesn't reliably |
| 2299 |
* announce their support for UHS-II in the response to the ACMD41, |
| 2300 |
* while initializing the legacy SD interface. Therefore, let's start |
| 2301 |
* with UHS-II for now. |
| 2302 |
*/ |
| 2303 |
if (!mmc_attach_sd_uhs2(host)) { |
| 2304 |
mmc_release_host(host); |
| 2305 |
goto out; |
| 2306 |
} |
| 2307 |
|
| 2308 |
for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
| 2309 |
unsigned int freq = freqs[i]; |
| 2310 |
if (freq > host->f_max) { |
| 2311 |
if (i + 1 < ARRAY_SIZE(freqs)) |
| 2312 |
continue; |
| 2313 |
freq = host->f_max; |
| 2314 |
} |
| 2315 |
if (!mmc_rescan_try_freq(host, max(freq, host->f_min))) |
| 2316 |
break; |
| 2317 |
if (freqs[i] <= host->f_min) |
| 2318 |
break; |
| 2319 |
} |
| 2320 |
|
| 2321 |
/* A non-removable card should have been detected by now. */ |
| 2322 |
if (!mmc_card_is_removable(host) && !host->bus_ops) |
| 2323 |
pr_info("%s: Failed to initialize a non-removable card", |
| 2324 |
mmc_hostname(host)); |
| 2325 |
|
| 2326 |
/* |
| 2327 |
* Ignore the command timeout errors observed during |
| 2328 |
* the card init as those are excepted. |
| 2329 |
*/ |
| 2330 |
host->err_stats[MMC_ERR_CMD_TIMEOUT] = 0; |
| 2331 |
mmc_release_host(host); |
| 2332 |
|
| 2333 |
out: |
| 2334 |
if (host->caps & MMC_CAP_NEEDS_POLL) |
| 2335 |
mmc_schedule_delayed_work(&host->detect, HZ); |
| 2336 |
} |
| 2337 |
|
| 2338 |
void mmc_start_host(struct mmc_host *host) |
| 2339 |
{ |
| 2340 |
bool power_up = !(host->caps2 & |
| 2341 |
(MMC_CAP2_NO_PRESCAN_POWERUP | MMC_CAP2_SD_UHS2)); |
| 2342 |
|
| 2343 |
host->f_init = max(min(freqs[0], host->f_max), host->f_min); |
| 2344 |
host->rescan_disable = 0; |
| 2345 |
|
| 2346 |
if (power_up) { |
| 2347 |
mmc_claim_host(host); |
| 2348 |
mmc_power_up(host, host->ocr_avail); |
| 2349 |
mmc_release_host(host); |
| 2350 |
} |
| 2351 |
|
| 2352 |
mmc_gpiod_request_cd_irq(host); |
| 2353 |
_mmc_detect_change(host, 0, false); |
| 2354 |
} |
| 2355 |
|
| 2356 |
void __mmc_stop_host(struct mmc_host *host) |
| 2357 |
{ |
| 2358 |
if (host->rescan_disable) |
| 2359 |
return; |
| 2360 |
|
| 2361 |
if (host->slot.cd_irq >= 0) { |
| 2362 |
mmc_gpio_set_cd_wake(host, false); |
| 2363 |
disable_irq(host->slot.cd_irq); |
| 2364 |
} |
| 2365 |
|
| 2366 |
host->rescan_disable = 1; |
| 2367 |
cancel_delayed_work_sync(&host->detect); |
| 2368 |
} |
| 2369 |
|
| 2370 |
void mmc_stop_host(struct mmc_host *host) |
| 2371 |
{ |
| 2372 |
__mmc_stop_host(host); |
| 2373 |
|
| 2374 |
/* clear pm flags now and let card drivers set them as needed */ |
| 2375 |
host->pm_flags = 0; |
| 2376 |
|
| 2377 |
if (host->bus_ops) { |
| 2378 |
/* Calling bus_ops->remove() with a claimed host can deadlock */ |
| 2379 |
host->bus_ops->remove(host); |
| 2380 |
mmc_claim_host(host); |
| 2381 |
mmc_detach_bus(host); |
| 2382 |
mmc_power_off(host); |
| 2383 |
mmc_release_host(host); |
| 2384 |
return; |
| 2385 |
} |
| 2386 |
|
| 2387 |
mmc_claim_host(host); |
| 2388 |
mmc_power_off(host); |
| 2389 |
mmc_release_host(host); |
| 2390 |
} |
| 2391 |
|
| 2392 |
static int __init mmc_init(void) |
| 2393 |
{ |
| 2394 |
int ret; |
| 2395 |
|
| 2396 |
ret = mmc_register_bus(); |
| 2397 |
if (ret) |
| 2398 |
return ret; |
| 2399 |
|
| 2400 |
ret = mmc_register_host_class(); |
| 2401 |
if (ret) |
| 2402 |
goto unregister_bus; |
| 2403 |
|
| 2404 |
ret = sdio_register_bus(); |
| 2405 |
if (ret) |
| 2406 |
goto unregister_host_class; |
| 2407 |
|
| 2408 |
return 0; |
| 2409 |
|
| 2410 |
unregister_host_class: |
| 2411 |
mmc_unregister_host_class(); |
| 2412 |
unregister_bus: |
| 2413 |
mmc_unregister_bus(); |
| 2414 |
return ret; |
| 2415 |
} |
| 2416 |
|
| 2417 |
static void __exit mmc_exit(void) |
| 2418 |
{ |
| 2419 |
sdio_unregister_bus(); |
| 2420 |
mmc_unregister_host_class(); |
| 2421 |
mmc_unregister_bus(); |
| 2422 |
} |
| 2423 |
|
| 2424 |
subsys_initcall(mmc_init); |
| 2425 |
module_exit(mmc_exit); |
| 2426 |
|
| 2427 |
MODULE_DESCRIPTION("MMC core driver"); |
| 2428 |
MODULE_LICENSE("GPL"); |