1/* 2 * linux/drivers/mmc/core/mmc_ops.h 3 * 4 * Copyright 2006-2007 Pierre Ossman 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or (at 9 * your option) any later version. 10 */ 11 12#include <linux/slab.h> 13#include <linux/export.h> 14#include <linux/types.h> 15#include <linux/scatterlist.h> 16 17#include <linux/mmc/host.h> 18#include <linux/mmc/card.h> 19#include <linux/mmc/mmc.h> 20 21#include "core.h" 22#include "mmc_ops.h" 23 24#define MMC_OPS_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ 25 26static const u8 tuning_blk_pattern_4bit[] = { 27 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, 28 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef, 29 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, 30 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef, 31 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, 32 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee, 33 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, 34 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde, 35}; 36 37static const u8 tuning_blk_pattern_8bit[] = { 38 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 39 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc, 40 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff, 41 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff, 42 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd, 43 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 44 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff, 45 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff, 46 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 47 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 48 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 49 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 50 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 51 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 52 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 53 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 54}; 55 56static inline int __mmc_send_status(struct mmc_card *card, u32 *status, 57 bool ignore_crc) 58{ 59 int err; 60 struct mmc_command cmd = {0}; 61 62 BUG_ON(!card); 63 BUG_ON(!card->host); 64 65 cmd.opcode = MMC_SEND_STATUS; 66 if (!mmc_host_is_spi(card->host)) 67 cmd.arg = card->rca << 16; 68 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; 69 if (ignore_crc) 70 cmd.flags &= ~MMC_RSP_CRC; 71 72 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 73 if (err) 74 return err; 75 76 /* NOTE: callers are required to understand the difference 77 * between "native" and SPI format status words! 78 */ 79 if (status) 80 *status = cmd.resp[0]; 81 82 return 0; 83} 84 85int mmc_send_status(struct mmc_card *card, u32 *status) 86{ 87 return __mmc_send_status(card, status, false); 88} 89 90static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card) 91{ 92 int err; 93 struct mmc_command cmd = {0}; 94 95 BUG_ON(!host); 96 97 cmd.opcode = MMC_SELECT_CARD; 98 99 if (card) { 100 cmd.arg = card->rca << 16; 101 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 102 } else { 103 cmd.arg = 0; 104 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; 105 } 106 107 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 108 if (err) 109 return err; 110 111 return 0; 112} 113 114int mmc_select_card(struct mmc_card *card) 115{ 116 BUG_ON(!card); 117 118 return _mmc_select_card(card->host, card); 119} 120 121int mmc_deselect_cards(struct mmc_host *host) 122{ 123 return _mmc_select_card(host, NULL); 124} 125 126/* 127 * Write the value specified in the device tree or board code into the optional 128 * 16 bit Driver Stage Register. This can be used to tune raise/fall times and 129 * drive strength of the DAT and CMD outputs. The actual meaning of a given 130 * value is hardware dependant. 131 * The presence of the DSR register can be determined from the CSD register, 132 * bit 76. 133 */ 134int mmc_set_dsr(struct mmc_host *host) 135{ 136 struct mmc_command cmd = {0}; 137 138 cmd.opcode = MMC_SET_DSR; 139 140 cmd.arg = (host->dsr << 16) | 0xffff; 141 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; 142 143 return mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 144} 145 146int mmc_go_idle(struct mmc_host *host) 147{ 148 int err; 149 struct mmc_command cmd = {0}; 150 151 /* 152 * Non-SPI hosts need to prevent chipselect going active during 153 * GO_IDLE; that would put chips into SPI mode. Remind them of 154 * that in case of hardware that won't pull up DAT3/nCS otherwise. 155 * 156 * SPI hosts ignore ios.chip_select; it's managed according to 157 * rules that must accommodate non-MMC slaves which this layer 158 * won't even know about. 159 */ 160 if (!mmc_host_is_spi(host)) { 161 mmc_set_chip_select(host, MMC_CS_HIGH); 162 mmc_delay(1); 163 } 164 165 cmd.opcode = MMC_GO_IDLE_STATE; 166 cmd.arg = 0; 167 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC; 168 169 err = mmc_wait_for_cmd(host, &cmd, 0); 170 171 mmc_delay(1); 172 173 if (!mmc_host_is_spi(host)) { 174 mmc_set_chip_select(host, MMC_CS_DONTCARE); 175 mmc_delay(1); 176 } 177 178 host->use_spi_crc = 0; 179 180 return err; 181} 182 183int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 184{ 185 struct mmc_command cmd = {0}; 186 int i, err = 0; 187 188 BUG_ON(!host); 189 190 cmd.opcode = MMC_SEND_OP_COND; 191 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr; 192 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; 193 194 for (i = 100; i; i--) { 195 err = mmc_wait_for_cmd(host, &cmd, 0); 196 if (err) 197 break; 198 199 /* if we're just probing, do a single pass */ 200 if (ocr == 0) 201 break; 202 203 /* otherwise wait until reset completes */ 204 if (mmc_host_is_spi(host)) { 205 if (!(cmd.resp[0] & R1_SPI_IDLE)) 206 break; 207 } else { 208 if (cmd.resp[0] & MMC_CARD_BUSY) 209 break; 210 } 211 212 err = -ETIMEDOUT; 213 214 mmc_delay(10); 215 } 216 217 if (rocr && !mmc_host_is_spi(host)) 218 *rocr = cmd.resp[0]; 219 220 return err; 221} 222 223int mmc_all_send_cid(struct mmc_host *host, u32 *cid) 224{ 225 int err; 226 struct mmc_command cmd = {0}; 227 228 BUG_ON(!host); 229 BUG_ON(!cid); 230 231 cmd.opcode = MMC_ALL_SEND_CID; 232 cmd.arg = 0; 233 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 234 235 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 236 if (err) 237 return err; 238 239 memcpy(cid, cmd.resp, sizeof(u32) * 4); 240 241 return 0; 242} 243 244int mmc_set_relative_addr(struct mmc_card *card) 245{ 246 int err; 247 struct mmc_command cmd = {0}; 248 249 BUG_ON(!card); 250 BUG_ON(!card->host); 251 252 cmd.opcode = MMC_SET_RELATIVE_ADDR; 253 cmd.arg = card->rca << 16; 254 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 255 256 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 257 if (err) 258 return err; 259 260 return 0; 261} 262 263static int 264mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode) 265{ 266 int err; 267 struct mmc_command cmd = {0}; 268 269 BUG_ON(!host); 270 BUG_ON(!cxd); 271 272 cmd.opcode = opcode; 273 cmd.arg = arg; 274 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 275 276 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 277 if (err) 278 return err; 279 280 memcpy(cxd, cmd.resp, sizeof(u32) * 4); 281 282 return 0; 283} 284 285/* 286 * NOTE: void *buf, caller for the buf is required to use DMA-capable 287 * buffer or on-stack buffer (with some overhead in callee). 288 */ 289static int 290mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host, 291 u32 opcode, void *buf, unsigned len) 292{ 293 struct mmc_request mrq = {NULL}; 294 struct mmc_command cmd = {0}; 295 struct mmc_data data = {0}; 296 struct scatterlist sg; 297 298 mrq.cmd = &cmd; 299 mrq.data = &data; 300 301 cmd.opcode = opcode; 302 cmd.arg = 0; 303 304 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we 305 * rely on callers to never use this with "native" calls for reading 306 * CSD or CID. Native versions of those commands use the R2 type, 307 * not R1 plus a data block. 308 */ 309 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 310 311 data.blksz = len; 312 data.blocks = 1; 313 data.flags = MMC_DATA_READ; 314 data.sg = &sg; 315 data.sg_len = 1; 316 317 sg_init_one(&sg, buf, len); 318 319 if (opcode == MMC_SEND_CSD || opcode == MMC_SEND_CID) { 320 /* 321 * The spec states that CSR and CID accesses have a timeout 322 * of 64 clock cycles. 323 */ 324 data.timeout_ns = 0; 325 data.timeout_clks = 64; 326 } else 327 mmc_set_data_timeout(&data, card); 328 329 mmc_wait_for_req(host, &mrq); 330 331 if (cmd.error) 332 return cmd.error; 333 if (data.error) 334 return data.error; 335 336 return 0; 337} 338 339int mmc_send_csd(struct mmc_card *card, u32 *csd) 340{ 341 int ret, i; 342 u32 *csd_tmp; 343 344 if (!mmc_host_is_spi(card->host)) 345 return mmc_send_cxd_native(card->host, card->rca << 16, 346 csd, MMC_SEND_CSD); 347 348 csd_tmp = kzalloc(16, GFP_KERNEL); 349 if (!csd_tmp) 350 return -ENOMEM; 351 352 ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd_tmp, 16); 353 if (ret) 354 goto err; 355 356 for (i = 0;i < 4;i++) 357 csd[i] = be32_to_cpu(csd_tmp[i]); 358 359err: 360 kfree(csd_tmp); 361 return ret; 362} 363 364int mmc_send_cid(struct mmc_host *host, u32 *cid) 365{ 366 int ret, i; 367 u32 *cid_tmp; 368 369 if (!mmc_host_is_spi(host)) { 370 if (!host->card) 371 return -EINVAL; 372 return mmc_send_cxd_native(host, host->card->rca << 16, 373 cid, MMC_SEND_CID); 374 } 375 376 cid_tmp = kzalloc(16, GFP_KERNEL); 377 if (!cid_tmp) 378 return -ENOMEM; 379 380 ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid_tmp, 16); 381 if (ret) 382 goto err; 383 384 for (i = 0;i < 4;i++) 385 cid[i] = be32_to_cpu(cid_tmp[i]); 386 387err: 388 kfree(cid_tmp); 389 return ret; 390} 391 392int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd) 393{ 394 int err; 395 u8 *ext_csd; 396 397 if (!card || !new_ext_csd) 398 return -EINVAL; 399 400 if (!mmc_can_ext_csd(card)) 401 return -EOPNOTSUPP; 402 403 /* 404 * As the ext_csd is so large and mostly unused, we don't store the 405 * raw block in mmc_card. 406 */ 407 ext_csd = kzalloc(512, GFP_KERNEL); 408 if (!ext_csd) 409 return -ENOMEM; 410 411 err = mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, ext_csd, 412 512); 413 if (err) 414 kfree(ext_csd); 415 else 416 *new_ext_csd = ext_csd; 417 418 return err; 419} 420EXPORT_SYMBOL_GPL(mmc_get_ext_csd); 421 422int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp) 423{ 424 struct mmc_command cmd = {0}; 425 int err; 426 427 cmd.opcode = MMC_SPI_READ_OCR; 428 cmd.arg = highcap ? (1 << 30) : 0; 429 cmd.flags = MMC_RSP_SPI_R3; 430 431 err = mmc_wait_for_cmd(host, &cmd, 0); 432 433 *ocrp = cmd.resp[1]; 434 return err; 435} 436 437int mmc_spi_set_crc(struct mmc_host *host, int use_crc) 438{ 439 struct mmc_command cmd = {0}; 440 int err; 441 442 cmd.opcode = MMC_SPI_CRC_ON_OFF; 443 cmd.flags = MMC_RSP_SPI_R1; 444 cmd.arg = use_crc; 445 446 err = mmc_wait_for_cmd(host, &cmd, 0); 447 if (!err) 448 host->use_spi_crc = use_crc; 449 return err; 450} 451 452/** 453 * __mmc_switch - modify EXT_CSD register 454 * @card: the MMC card associated with the data transfer 455 * @set: cmd set values 456 * @index: EXT_CSD register index 457 * @value: value to program into EXT_CSD register 458 * @timeout_ms: timeout (ms) for operation performed by register write, 459 * timeout of zero implies maximum possible timeout 460 * @use_busy_signal: use the busy signal as response type 461 * @send_status: send status cmd to poll for busy 462 * @ignore_crc: ignore CRC errors when sending status cmd to poll for busy 463 * 464 * Modifies the EXT_CSD register for selected card. 465 */ 466int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, 467 unsigned int timeout_ms, bool use_busy_signal, bool send_status, 468 bool ignore_crc) 469{ 470 struct mmc_host *host = card->host; 471 int err; 472 struct mmc_command cmd = {0}; 473 unsigned long timeout; 474 u32 status = 0; 475 bool use_r1b_resp = use_busy_signal; 476 477 /* 478 * If the cmd timeout and the max_busy_timeout of the host are both 479 * specified, let's validate them. A failure means we need to prevent 480 * the host from doing hw busy detection, which is done by converting 481 * to a R1 response instead of a R1B. 482 */ 483 if (timeout_ms && host->max_busy_timeout && 484 (timeout_ms > host->max_busy_timeout)) 485 use_r1b_resp = false; 486 487 cmd.opcode = MMC_SWITCH; 488 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 489 (index << 16) | 490 (value << 8) | 491 set; 492 cmd.flags = MMC_CMD_AC; 493 if (use_r1b_resp) { 494 cmd.flags |= MMC_RSP_SPI_R1B | MMC_RSP_R1B; 495 /* 496 * A busy_timeout of zero means the host can decide to use 497 * whatever value it finds suitable. 498 */ 499 cmd.busy_timeout = timeout_ms; 500 } else { 501 cmd.flags |= MMC_RSP_SPI_R1 | MMC_RSP_R1; 502 } 503 504 if (index == EXT_CSD_SANITIZE_START) 505 cmd.sanitize_busy = true; 506 507 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 508 if (err) 509 return err; 510 511 /* No need to check card status in case of unblocking command */ 512 if (!use_busy_signal) 513 return 0; 514 515 /* 516 * CRC errors shall only be ignored in cases were CMD13 is used to poll 517 * to detect busy completion. 518 */ 519 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) 520 ignore_crc = false; 521 522 /* We have an unspecified cmd timeout, use the fallback value. */ 523 if (!timeout_ms) 524 timeout_ms = MMC_OPS_TIMEOUT_MS; 525 526 /* Must check status to be sure of no errors. */ 527 timeout = jiffies + msecs_to_jiffies(timeout_ms); 528 do { 529 if (send_status) { 530 err = __mmc_send_status(card, &status, ignore_crc); 531 if (err) 532 return err; 533 } 534 if ((host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) 535 break; 536 if (mmc_host_is_spi(host)) 537 break; 538 539 /* 540 * We are not allowed to issue a status command and the host 541 * does'nt support MMC_CAP_WAIT_WHILE_BUSY, then we can only 542 * rely on waiting for the stated timeout to be sufficient. 543 */ 544 if (!send_status) { 545 mmc_delay(timeout_ms); 546 return 0; 547 } 548 549 /* Timeout if the device never leaves the program state. */ 550 if (time_after(jiffies, timeout)) { 551 pr_err("%s: Card stuck in programming state! %s\n", 552 mmc_hostname(host), __func__); 553 return -ETIMEDOUT; 554 } 555 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG); 556 557 if (mmc_host_is_spi(host)) { 558 if (status & R1_SPI_ILLEGAL_COMMAND) 559 return -EBADMSG; 560 } else { 561 if (status & 0xFDFFA000) 562 pr_warn("%s: unexpected status %#x after switch\n", 563 mmc_hostname(host), status); 564 if (status & R1_SWITCH_ERROR) 565 return -EBADMSG; 566 } 567 568 return 0; 569} 570EXPORT_SYMBOL_GPL(__mmc_switch); 571 572int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, 573 unsigned int timeout_ms) 574{ 575 return __mmc_switch(card, set, index, value, timeout_ms, true, true, 576 false); 577} 578EXPORT_SYMBOL_GPL(mmc_switch); 579 580int mmc_send_tuning(struct mmc_host *host) 581{ 582 struct mmc_request mrq = {NULL}; 583 struct mmc_command cmd = {0}; 584 struct mmc_data data = {0}; 585 struct scatterlist sg; 586 struct mmc_ios *ios = &host->ios; 587 const u8 *tuning_block_pattern; 588 int size, err = 0; 589 u8 *data_buf; 590 u32 opcode; 591 592 if (ios->bus_width == MMC_BUS_WIDTH_8) { 593 tuning_block_pattern = tuning_blk_pattern_8bit; 594 size = sizeof(tuning_blk_pattern_8bit); 595 opcode = MMC_SEND_TUNING_BLOCK_HS200; 596 } else if (ios->bus_width == MMC_BUS_WIDTH_4) { 597 tuning_block_pattern = tuning_blk_pattern_4bit; 598 size = sizeof(tuning_blk_pattern_4bit); 599 opcode = MMC_SEND_TUNING_BLOCK; 600 } else 601 return -EINVAL; 602 603 data_buf = kzalloc(size, GFP_KERNEL); 604 if (!data_buf) 605 return -ENOMEM; 606 607 mrq.cmd = &cmd; 608 mrq.data = &data; 609 610 cmd.opcode = opcode; 611 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 612 613 data.blksz = size; 614 data.blocks = 1; 615 data.flags = MMC_DATA_READ; 616 617 /* 618 * According to the tuning specs, Tuning process 619 * is normally shorter 40 executions of CMD19, 620 * and timeout value should be shorter than 150 ms 621 */ 622 data.timeout_ns = 150 * NSEC_PER_MSEC; 623 624 data.sg = &sg; 625 data.sg_len = 1; 626 sg_init_one(&sg, data_buf, size); 627 628 mmc_wait_for_req(host, &mrq); 629 630 if (cmd.error) { 631 err = cmd.error; 632 goto out; 633 } 634 635 if (data.error) { 636 err = data.error; 637 goto out; 638 } 639 640 if (memcmp(data_buf, tuning_block_pattern, size)) 641 err = -EIO; 642 643out: 644 kfree(data_buf); 645 return err; 646} 647EXPORT_SYMBOL_GPL(mmc_send_tuning); 648 649static int 650mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode, 651 u8 len) 652{ 653 struct mmc_request mrq = {NULL}; 654 struct mmc_command cmd = {0}; 655 struct mmc_data data = {0}; 656 struct scatterlist sg; 657 u8 *data_buf; 658 u8 *test_buf; 659 int i, err; 660 static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 }; 661 static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 }; 662 663 /* dma onto stack is unsafe/nonportable, but callers to this 664 * routine normally provide temporary on-stack buffers ... 665 */ 666 data_buf = kmalloc(len, GFP_KERNEL); 667 if (!data_buf) 668 return -ENOMEM; 669 670 if (len == 8) 671 test_buf = testdata_8bit; 672 else if (len == 4) 673 test_buf = testdata_4bit; 674 else { 675 pr_err("%s: Invalid bus_width %d\n", 676 mmc_hostname(host), len); 677 kfree(data_buf); 678 return -EINVAL; 679 } 680 681 if (opcode == MMC_BUS_TEST_W) 682 memcpy(data_buf, test_buf, len); 683 684 mrq.cmd = &cmd; 685 mrq.data = &data; 686 cmd.opcode = opcode; 687 cmd.arg = 0; 688 689 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we 690 * rely on callers to never use this with "native" calls for reading 691 * CSD or CID. Native versions of those commands use the R2 type, 692 * not R1 plus a data block. 693 */ 694 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 695 696 data.blksz = len; 697 data.blocks = 1; 698 if (opcode == MMC_BUS_TEST_R) 699 data.flags = MMC_DATA_READ; 700 else 701 data.flags = MMC_DATA_WRITE; 702 703 data.sg = &sg; 704 data.sg_len = 1; 705 mmc_set_data_timeout(&data, card); 706 sg_init_one(&sg, data_buf, len); 707 mmc_wait_for_req(host, &mrq); 708 err = 0; 709 if (opcode == MMC_BUS_TEST_R) { 710 for (i = 0; i < len / 4; i++) 711 if ((test_buf[i] ^ data_buf[i]) != 0xff) { 712 err = -EIO; 713 break; 714 } 715 } 716 kfree(data_buf); 717 718 if (cmd.error) 719 return cmd.error; 720 if (data.error) 721 return data.error; 722 723 return err; 724} 725 726int mmc_bus_test(struct mmc_card *card, u8 bus_width) 727{ 728 int err, width; 729 730 if (bus_width == MMC_BUS_WIDTH_8) 731 width = 8; 732 else if (bus_width == MMC_BUS_WIDTH_4) 733 width = 4; 734 else if (bus_width == MMC_BUS_WIDTH_1) 735 return 0; /* no need for test */ 736 else 737 return -EINVAL; 738 739 /* 740 * Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there 741 * is a problem. This improves chances that the test will work. 742 */ 743 mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width); 744 err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width); 745 return err; 746} 747 748int mmc_send_hpi_cmd(struct mmc_card *card, u32 *status) 749{ 750 struct mmc_command cmd = {0}; 751 unsigned int opcode; 752 int err; 753 754 if (!card->ext_csd.hpi) { 755 pr_warn("%s: Card didn't support HPI command\n", 756 mmc_hostname(card->host)); 757 return -EINVAL; 758 } 759 760 opcode = card->ext_csd.hpi_cmd; 761 if (opcode == MMC_STOP_TRANSMISSION) 762 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 763 else if (opcode == MMC_SEND_STATUS) 764 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 765 766 cmd.opcode = opcode; 767 cmd.arg = card->rca << 16 | 1; 768 769 err = mmc_wait_for_cmd(card->host, &cmd, 0); 770 if (err) { 771 pr_warn("%s: error %d interrupting operation. " 772 "HPI command response %#x\n", mmc_hostname(card->host), 773 err, cmd.resp[0]); 774 return err; 775 } 776 if (status) 777 *status = cmd.resp[0]; 778 779 return 0; 780} 781 782int mmc_can_ext_csd(struct mmc_card *card) 783{ 784 return (card && card->csd.mmca_vsn > CSD_SPEC_VER_3); 785} 786