Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

MMC core learns about SPI

Teach the MMC/SD/SDIO core about using SPI mode.

- Use mmc_host_is_spi() so enumeration works through SPI signaling
and protocols, not just the native versions.

- Provide the SPI response type flags with each request issued,
including requests from the new lock/unlock code.

- Understand that cmd->resp[0] and mmc_get_status() results for SPI
return different values than for "native" MMC/SD protocol; this
affects resetting, checking card lock status, and some others.

- Understand that some commands act a bit differently ... notably:
* OP_COND command doesn't return the OCR
* APP_CMD status doesn't have an R1_APP_CMD analogue

Those changes required some new and updated primitives:

- Provide utilities to access two SPI-only requests, and one
request that wasn't previously needed:
* mmc_spi_read_ocr() ... SPI only
* mmc_spi_set_crc() ... SPI only (override by module parm)
* mmc_send_cid() ... for use without broadcast mode

- Updated internal routines:
* Previous mmc_send_csd() modified into mmc_send_cxd_native();
it uses native "R2" responses, which include 16 bytes of data.
* Previous mmc_send_ext_csd() becomes new mmc_send_cxd_data()
helper for command-and-data access
* Bugfix to that mmc_send_cxd_data() code: dma-to-stack is
unsafe/nonportable, so kmalloc a bounce buffer instead.

- Modified mmc_send_ext_csd() now uses mmc_send_cxd_data() helper

- Modified mmc_send_csd(), and new mmc_spi_send_cid(), routines use
those helper routines based on whether they're native or SPI

The newest categories of cards supported by the MMC stack aren't expected
to work yet with SPI: MMC or SD cards with over 4GB data, and SDIO.
All those cards support SPI mode, so eventually they should work too.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>

authored by

David Brownell and committed by
Pierre Ossman
af517150 7213d175

+372 -103
+18 -6
drivers/mmc/core/bus.c
··· 243 243 break; 244 244 } 245 245 246 - printk(KERN_INFO "%s: new %s%s card at address %04x\n", 247 - mmc_hostname(card->host), 248 - mmc_card_highspeed(card) ? "high speed " : "", 249 - type, card->rca); 246 + if (mmc_host_is_spi(card->host)) { 247 + printk(KERN_INFO "%s: new %s%s card on SPI\n", 248 + mmc_hostname(card->host), 249 + mmc_card_highspeed(card) ? "high speed " : "", 250 + type); 251 + } else { 252 + printk(KERN_INFO "%s: new %s%s card at address %04x\n", 253 + mmc_hostname(card->host), 254 + mmc_card_highspeed(card) ? "high speed " : "", 255 + type, card->rca); 256 + } 250 257 251 258 card->dev.uevent_suppress = 1; 252 259 ··· 285 278 void mmc_remove_card(struct mmc_card *card) 286 279 { 287 280 if (mmc_card_present(card)) { 288 - printk(KERN_INFO "%s: card %04x removed\n", 289 - mmc_hostname(card->host), card->rca); 281 + if (mmc_host_is_spi(card->host)) { 282 + printk(KERN_INFO "%s: SPI card removed\n", 283 + mmc_hostname(card->host)); 284 + } else { 285 + printk(KERN_INFO "%s: card %04x removed\n", 286 + mmc_hostname(card->host), card->rca); 287 + } 290 288 291 289 if (card->host->bus_ops->sysfs_remove) 292 290 card->host->bus_ops->sysfs_remove(card->host, card);
+24 -4
drivers/mmc/core/core.c
··· 42 42 static struct workqueue_struct *workqueue; 43 43 44 44 /* 45 + * Enabling software CRCs on the data blocks can be a significant (30%) 46 + * performance cost, and for other reasons may not always be desired. 47 + * So we allow it it to be disabled. 48 + */ 49 + int use_spi_crc = 1; 50 + module_param(use_spi_crc, bool, 0); 51 + 52 + /* 45 53 * Internal function. Schedule delayed work in the MMC work queue. 46 54 */ 47 55 static int mmc_schedule_delayed_work(struct delayed_work *work, ··· 78 70 { 79 71 struct mmc_command *cmd = mrq->cmd; 80 72 int err = cmd->error; 73 + 74 + if (err && cmd->retries && mmc_host_is_spi(host)) { 75 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) 76 + cmd->retries = 0; 77 + } 81 78 82 79 if (err && cmd->retries) { 83 80 pr_debug("%s: req failed (CMD%u): %d, retrying...\n", ··· 466 453 int bit = fls(host->ocr_avail) - 1; 467 454 468 455 host->ios.vdd = bit; 469 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 470 - host->ios.chip_select = MMC_CS_DONTCARE; 456 + if (mmc_host_is_spi(host)) { 457 + host->ios.chip_select = MMC_CS_HIGH; 458 + host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 459 + } else { 460 + host->ios.chip_select = MMC_CS_DONTCARE; 461 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 462 + } 471 463 host->ios.power_mode = MMC_POWER_UP; 472 464 host->ios.bus_width = MMC_BUS_WIDTH_1; 473 465 host->ios.timing = MMC_TIMING_LEGACY; ··· 499 481 { 500 482 host->ios.clock = 0; 501 483 host->ios.vdd = 0; 502 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 503 - host->ios.chip_select = MMC_CS_DONTCARE; 484 + if (!mmc_host_is_spi(host)) { 485 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 486 + host->ios.chip_select = MMC_CS_DONTCARE; 487 + } 504 488 host->ios.power_mode = MMC_POWER_OFF; 505 489 host->ios.bus_width = MMC_BUS_WIDTH_1; 506 490 host->ios.timing = MMC_TIMING_LEGACY;
+2
drivers/mmc/core/core.h
··· 48 48 void mmc_start_host(struct mmc_host *host); 49 49 void mmc_stop_host(struct mmc_host *host); 50 50 51 + extern int use_spi_crc; 52 + 51 53 #endif 52 54
+37 -13
drivers/mmc/core/mmc.c
··· 165 165 166 166 BUG_ON(!card); 167 167 168 - err = -EIO; 169 - 170 168 if (card->csd.mmca_vsn < CSD_SPEC_VER_4) 171 169 return 0; 172 170 ··· 278 280 goto err; 279 281 280 282 /* 283 + * For SPI, enable CRC as appropriate. 284 + */ 285 + if (mmc_host_is_spi(host)) { 286 + err = mmc_spi_set_crc(host, use_spi_crc); 287 + if (err) 288 + goto err; 289 + } 290 + 291 + /* 281 292 * Fetch CID from card. 282 293 */ 283 - err = mmc_all_send_cid(host, cid); 294 + if (mmc_host_is_spi(host)) 295 + err = mmc_send_cid(host, cid); 296 + else 297 + err = mmc_all_send_cid(host, cid); 284 298 if (err) 285 299 goto err; 286 300 ··· 319 309 } 320 310 321 311 /* 322 - * Set card RCA. 312 + * For native busses: set card RCA and quit open drain mode. 323 313 */ 324 - err = mmc_set_relative_addr(card); 325 - if (err) 326 - goto free_card; 314 + if (!mmc_host_is_spi(host)) { 315 + err = mmc_set_relative_addr(card); 316 + if (err) 317 + goto free_card; 327 318 328 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 319 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 320 + } 329 321 330 322 if (!oldcard) { 331 323 /* ··· 348 336 /* 349 337 * Select card, as all following commands rely on that. 350 338 */ 351 - err = mmc_select_card(card); 352 - if (err) 353 - goto free_card; 339 + if (!mmc_host_is_spi(host)) { 340 + err = mmc_select_card(card); 341 + if (err) 342 + goto free_card; 343 + } 354 344 355 345 if (!oldcard) { 356 346 /* 357 - * Fetch and process extened CSD. 347 + * Fetch and process extended CSD. 358 348 */ 359 349 err = mmc_read_ext_csd(card); 360 350 if (err) ··· 516 502 BUG_ON(!host->card); 517 503 518 504 mmc_claim_host(host); 519 - mmc_deselect_cards(host); 505 + if (!mmc_host_is_spi(host)) 506 + mmc_deselect_cards(host); 520 507 host->card->state &= ~MMC_STATE_HIGHSPEED; 521 508 mmc_release_host(host); 522 509 } ··· 576 561 WARN_ON(!host->claimed); 577 562 578 563 mmc_attach_bus(host, &mmc_ops); 564 + 565 + /* 566 + * We need to get OCR a different way for SPI. 567 + */ 568 + if (mmc_host_is_spi(host)) { 569 + err = mmc_spi_read_ocr(host, 1, &ocr); 570 + if (err) 571 + goto err; 572 + } 579 573 580 574 /* 581 575 * Sanity check the voltages that the card claims to
+135 -31
drivers/mmc/core/mmc_ops.c
··· 63 63 int err; 64 64 struct mmc_command cmd; 65 65 66 - mmc_set_chip_select(host, MMC_CS_HIGH); 67 - 68 - mmc_delay(1); 66 + /* 67 + * Non-SPI hosts need to prevent chipselect going active during 68 + * GO_IDLE; that would put chips into SPI mode. Remind them of 69 + * that in case of hardware that won't pull up DAT3/nCS otherwise. 70 + * 71 + * SPI hosts ignore ios.chip_select; it's managed according to 72 + * rules that must accomodate non-MMC slaves which this layer 73 + * won't even know about. 74 + */ 75 + if (!mmc_host_is_spi(host)) { 76 + mmc_set_chip_select(host, MMC_CS_HIGH); 77 + mmc_delay(1); 78 + } 69 79 70 80 memset(&cmd, 0, sizeof(struct mmc_command)); 71 81 72 82 cmd.opcode = MMC_GO_IDLE_STATE; 73 83 cmd.arg = 0; 74 - cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 84 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC; 75 85 76 86 err = mmc_wait_for_cmd(host, &cmd, 0); 77 87 78 88 mmc_delay(1); 79 89 80 - mmc_set_chip_select(host, MMC_CS_DONTCARE); 90 + if (!mmc_host_is_spi(host)) { 91 + mmc_set_chip_select(host, MMC_CS_DONTCARE); 92 + mmc_delay(1); 93 + } 81 94 82 - mmc_delay(1); 95 + host->use_spi_crc = 0; 83 96 84 97 return err; 85 98 } ··· 107 94 memset(&cmd, 0, sizeof(struct mmc_command)); 108 95 109 96 cmd.opcode = MMC_SEND_OP_COND; 110 - cmd.arg = ocr; 111 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 97 + cmd.arg = mmc_host_is_spi(host) ? 0 : ocr; 98 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; 112 99 113 100 for (i = 100; i; i--) { 114 101 err = mmc_wait_for_cmd(host, &cmd, 0); 115 102 if (err) 116 103 break; 117 104 118 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) 105 + /* if we're just probing, do a single pass */ 106 + if (ocr == 0) 119 107 break; 108 + 109 + /* otherwise wait until reset completes */ 110 + if (mmc_host_is_spi(host)) { 111 + if (!(cmd.resp[0] & R1_SPI_IDLE)) 112 + break; 113 + } else { 114 + if (cmd.resp[0] & MMC_CARD_BUSY) 115 + break; 116 + } 120 117 121 118 err = -ETIMEDOUT; 122 119 123 120 mmc_delay(10); 124 121 } 125 122 126 - if (rocr) 123 + if (rocr && !mmc_host_is_spi(host)) 127 124 *rocr = cmd.resp[0]; 128 125 129 126 return err; ··· 183 160 return 0; 184 161 } 185 162 186 - int mmc_send_csd(struct mmc_card *card, u32 *csd) 163 + static int 164 + mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode) 187 165 { 188 166 int err; 189 167 struct mmc_command cmd; 190 168 191 - BUG_ON(!card); 192 - BUG_ON(!card->host); 193 - BUG_ON(!csd); 169 + BUG_ON(!host); 170 + BUG_ON(!cxd); 194 171 195 172 memset(&cmd, 0, sizeof(struct mmc_command)); 196 173 197 - cmd.opcode = MMC_SEND_CSD; 198 - cmd.arg = card->rca << 16; 174 + cmd.opcode = opcode; 175 + cmd.arg = arg; 199 176 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 200 177 201 - err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 178 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 202 179 if (err) 203 180 return err; 204 181 205 - memcpy(csd, cmd.resp, sizeof(u32) * 4); 182 + memcpy(cxd, cmd.resp, sizeof(u32) * 4); 206 183 207 184 return 0; 208 185 } 209 186 210 - int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd) 187 + static int 188 + mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host, 189 + u32 opcode, void *buf, unsigned len) 211 190 { 212 191 struct mmc_request mrq; 213 192 struct mmc_command cmd; 214 193 struct mmc_data data; 215 194 struct scatterlist sg; 195 + void *data_buf; 216 196 217 - BUG_ON(!card); 218 - BUG_ON(!card->host); 219 - BUG_ON(!ext_csd); 197 + /* dma onto stack is unsafe/nonportable, but callers to this 198 + * routine normally provide temporary on-stack buffers ... 199 + */ 200 + data_buf = kmalloc(len, GFP_KERNEL); 201 + if (data_buf == NULL) 202 + return -ENOMEM; 220 203 221 204 memset(&mrq, 0, sizeof(struct mmc_request)); 222 205 memset(&cmd, 0, sizeof(struct mmc_command)); ··· 231 202 mrq.cmd = &cmd; 232 203 mrq.data = &data; 233 204 234 - cmd.opcode = MMC_SEND_EXT_CSD; 205 + cmd.opcode = opcode; 235 206 cmd.arg = 0; 236 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 237 207 238 - data.blksz = 512; 208 + /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we 209 + * rely on callers to never use this with "native" calls for reading 210 + * CSD or CID. Native versions of those commands use the R2 type, 211 + * not R1 plus a data block. 212 + */ 213 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 214 + 215 + data.blksz = len; 239 216 data.blocks = 1; 240 217 data.flags = MMC_DATA_READ; 241 218 data.sg = &sg; 242 219 data.sg_len = 1; 243 220 244 - sg_init_one(&sg, ext_csd, 512); 221 + sg_init_one(&sg, data_buf, len); 245 222 246 - mmc_set_data_timeout(&data, card); 223 + if (card) 224 + mmc_set_data_timeout(&data, card); 247 225 248 - mmc_wait_for_req(card->host, &mrq); 226 + mmc_wait_for_req(host, &mrq); 227 + 228 + memcpy(buf, data_buf, len); 229 + kfree(data_buf); 249 230 250 231 if (cmd.error) 251 232 return cmd.error; ··· 263 224 return data.error; 264 225 265 226 return 0; 227 + } 228 + 229 + int mmc_send_csd(struct mmc_card *card, u32 *csd) 230 + { 231 + if (!mmc_host_is_spi(card->host)) 232 + return mmc_send_cxd_native(card->host, card->rca << 16, 233 + csd, MMC_SEND_CSD); 234 + 235 + return mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16); 236 + } 237 + 238 + int mmc_send_cid(struct mmc_host *host, u32 *cid) 239 + { 240 + if (!mmc_host_is_spi(host)) { 241 + if (!host->card) 242 + return -EINVAL; 243 + return mmc_send_cxd_native(host, host->card->rca << 16, 244 + cid, MMC_SEND_CID); 245 + } 246 + 247 + return mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16); 248 + } 249 + 250 + int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd) 251 + { 252 + return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, 253 + ext_csd, 512); 254 + } 255 + 256 + int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp) 257 + { 258 + struct mmc_command cmd; 259 + int err; 260 + 261 + memset(&cmd, 0, sizeof(struct mmc_command)); 262 + 263 + cmd.opcode = MMC_SPI_READ_OCR; 264 + cmd.arg = highcap ? (1 << 30) : 0; 265 + cmd.flags = MMC_RSP_SPI_R3; 266 + 267 + err = mmc_wait_for_cmd(host, &cmd, 0); 268 + 269 + *ocrp = cmd.resp[1]; 270 + return err; 271 + } 272 + 273 + int mmc_spi_set_crc(struct mmc_host *host, int use_crc) 274 + { 275 + struct mmc_command cmd; 276 + int err; 277 + 278 + memset(&cmd, 0, sizeof(struct mmc_command)); 279 + 280 + cmd.opcode = MMC_SPI_CRC_ON_OFF; 281 + cmd.flags = MMC_RSP_SPI_R1; 282 + cmd.arg = use_crc; 283 + 284 + err = mmc_wait_for_cmd(host, &cmd, 0); 285 + if (!err) 286 + host->use_spi_crc = use_crc; 287 + return err; 266 288 } 267 289 268 290 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value) ··· 341 241 (index << 16) | 342 242 (value << 8) | 343 243 set; 344 - cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 244 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 345 245 346 246 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 347 247 if (err) ··· 361 261 memset(&cmd, 0, sizeof(struct mmc_command)); 362 262 363 263 cmd.opcode = MMC_SEND_STATUS; 364 - cmd.arg = card->rca << 16; 365 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 264 + if (!mmc_host_is_spi(card->host)) 265 + cmd.arg = card->rca << 16; 266 + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; 366 267 367 268 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 368 269 if (err) 369 270 return err; 370 271 272 + /* NOTE: callers are required to understand the difference 273 + * between "native" and SPI format status words! 274 + */ 371 275 if (status) 372 276 *status = cmd.resp[0]; 373 277
+3
drivers/mmc/core/mmc_ops.h
··· 22 22 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd); 23 23 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value); 24 24 int mmc_send_status(struct mmc_card *card, u32 *status); 25 + int mmc_send_cid(struct mmc_host *host, u32 *cid); 26 + int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp); 27 + int mmc_spi_set_crc(struct mmc_host *host, int use_crc); 25 28 26 29 #endif 27 30
+38 -10
drivers/mmc/core/sd.c
··· 323 323 goto err; 324 324 325 325 /* 326 + * For SPI, enable CRC as appropriate. 327 + */ 328 + if (mmc_host_is_spi(host)) { 329 + err = mmc_spi_set_crc(host, use_spi_crc); 330 + if (err) 331 + goto err; 332 + } 333 + 334 + /* 326 335 * Fetch CID from card. 327 336 */ 328 - err = mmc_all_send_cid(host, cid); 337 + if (mmc_host_is_spi(host)) 338 + err = mmc_send_cid(host, cid); 339 + else 340 + err = mmc_all_send_cid(host, cid); 329 341 if (err) 330 342 goto err; 331 343 ··· 363 351 } 364 352 365 353 /* 366 - * Set card RCA. 354 + * For native busses: get card RCA and quit open drain mode. 367 355 */ 368 - err = mmc_send_relative_addr(host, &card->rca); 369 - if (err) 370 - goto free_card; 356 + if (!mmc_host_is_spi(host)) { 357 + err = mmc_send_relative_addr(host, &card->rca); 358 + if (err) 359 + goto free_card; 371 360 372 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 361 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 362 + } 373 363 374 364 if (!oldcard) { 375 365 /* ··· 391 377 /* 392 378 * Select card, as all following commands rely on that. 393 379 */ 394 - err = mmc_select_card(card); 395 - if (err) 396 - goto free_card; 380 + if (!mmc_host_is_spi(host)) { 381 + err = mmc_select_card(card); 382 + if (err) 383 + goto free_card; 384 + } 397 385 398 386 if (!oldcard) { 399 387 /* ··· 578 562 BUG_ON(!host->card); 579 563 580 564 mmc_claim_host(host); 581 - mmc_deselect_cards(host); 565 + if (!mmc_host_is_spi(host)) 566 + mmc_deselect_cards(host); 582 567 host->card->state &= ~MMC_STATE_HIGHSPEED; 583 568 mmc_release_host(host); 584 569 } ··· 638 621 WARN_ON(!host->claimed); 639 622 640 623 mmc_attach_bus(host, &mmc_sd_ops); 624 + 625 + /* 626 + * We need to get OCR a different way for SPI. 627 + */ 628 + if (mmc_host_is_spi(host)) { 629 + mmc_go_idle(host); 630 + 631 + err = mmc_spi_read_ocr(host, 0, &ocr); 632 + if (err) 633 + goto err; 634 + } 641 635 642 636 /* 643 637 * Sanity check the voltages that the card claims to
+47 -12
drivers/mmc/core/sd_ops.c
··· 33 33 34 34 if (card) { 35 35 cmd.arg = card->rca << 16; 36 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 36 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 37 37 } else { 38 38 cmd.arg = 0; 39 - cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR; 39 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR; 40 40 } 41 41 42 42 err = mmc_wait_for_cmd(host, &cmd, 0); ··· 44 44 return err; 45 45 46 46 /* Check that card supported application commands */ 47 - if (!(cmd.resp[0] & R1_APP_CMD)) 47 + if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD)) 48 48 return -EOPNOTSUPP; 49 49 50 50 return 0; ··· 83 83 memset(&mrq, 0, sizeof(struct mmc_request)); 84 84 85 85 err = mmc_app_cmd(host, card); 86 - if (err) 86 + if (err) { 87 + /* no point in retrying; no APP commands allowed */ 88 + if (mmc_host_is_spi(host)) { 89 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) 90 + break; 91 + } 87 92 continue; 93 + } 88 94 89 95 memset(&mrq, 0, sizeof(struct mmc_request)); 90 96 ··· 105 99 err = cmd->error; 106 100 if (!cmd->error) 107 101 break; 102 + 103 + /* no point in retrying illegal APP commands */ 104 + if (mmc_host_is_spi(host)) { 105 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) 106 + break; 107 + } 108 108 } 109 109 110 110 return err; ··· 159 147 memset(&cmd, 0, sizeof(struct mmc_command)); 160 148 161 149 cmd.opcode = SD_APP_OP_COND; 162 - cmd.arg = ocr; 163 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 150 + if (mmc_host_is_spi(host)) 151 + cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */ 152 + else 153 + cmd.arg = ocr; 154 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; 164 155 165 156 for (i = 100; i; i--) { 166 157 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES); 167 158 if (err) 168 159 break; 169 160 170 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) 161 + /* if we're just probing, do a single pass */ 162 + if (ocr == 0) 171 163 break; 164 + 165 + /* otherwise wait until reset completes */ 166 + if (mmc_host_is_spi(host)) { 167 + if (!(cmd.resp[0] & R1_SPI_IDLE)) 168 + break; 169 + } else { 170 + if (cmd.resp[0] & MMC_CARD_BUSY) 171 + break; 172 + } 172 173 173 174 err = -ETIMEDOUT; 174 175 175 176 mmc_delay(10); 176 177 } 177 178 178 - if (rocr) 179 + if (rocr && !mmc_host_is_spi(host)) 179 180 *rocr = cmd.resp[0]; 180 181 181 182 return err; ··· 199 174 struct mmc_command cmd; 200 175 int err; 201 176 static const u8 test_pattern = 0xAA; 177 + u8 result_pattern; 202 178 203 179 /* 204 180 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND ··· 208 182 */ 209 183 cmd.opcode = SD_SEND_IF_COND; 210 184 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern; 211 - cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 185 + cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR; 212 186 213 187 err = mmc_wait_for_cmd(host, &cmd, 0); 214 188 if (err) 215 189 return err; 216 190 217 - if ((cmd.resp[0] & 0xFF) != test_pattern) 191 + if (mmc_host_is_spi(host)) 192 + result_pattern = cmd.resp[1] & 0xFF; 193 + else 194 + result_pattern = cmd.resp[0] & 0xFF; 195 + 196 + if (result_pattern != test_pattern) 218 197 return -EIO; 219 198 220 199 return 0; ··· 260 229 BUG_ON(!card->host); 261 230 BUG_ON(!scr); 262 231 232 + /* NOTE: caller guarantees scr is heap-allocated */ 233 + 263 234 err = mmc_app_cmd(card->host, card); 264 235 if (err) 265 236 return err; ··· 275 242 276 243 cmd.opcode = SD_APP_SEND_SCR; 277 244 cmd.arg = 0; 278 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 245 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 279 246 280 247 data.blksz = 8; 281 248 data.blocks = 1; ··· 311 278 BUG_ON(!card); 312 279 BUG_ON(!card->host); 313 280 281 + /* NOTE: caller guarantees resp is heap-allocated */ 282 + 314 283 mode = !!mode; 315 284 value &= 0xF; 316 285 ··· 327 292 cmd.arg = mode << 31 | 0x00FFFFFF; 328 293 cmd.arg &= ~(0xF << (group * 4)); 329 294 cmd.arg |= value << (group * 4); 330 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 295 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 331 296 332 297 data.blksz = 64; 333 298 data.blocks = 1;
+21 -8
drivers/mmc/core/sdio.c
··· 270 270 goto err; 271 271 272 272 /* 273 + * For SPI, enable CRC as appropriate. 274 + */ 275 + if (mmc_host_is_spi(host)) { 276 + err = mmc_spi_set_crc(host, use_spi_crc); 277 + if (err) 278 + goto err; 279 + } 280 + 281 + /* 273 282 * The number of functions on the card is encoded inside 274 283 * the ocr. 275 284 */ ··· 299 290 host->card = card; 300 291 301 292 /* 302 - * Set card RCA. 293 + * For native busses: set card RCA and quit open drain mode. 303 294 */ 304 - err = mmc_send_relative_addr(host, &card->rca); 305 - if (err) 306 - goto remove; 295 + if (!mmc_host_is_spi(host)) { 296 + err = mmc_send_relative_addr(host, &card->rca); 297 + if (err) 298 + goto remove; 307 299 308 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 300 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 301 + } 309 302 310 303 /* 311 304 * Select card, as all following commands rely on that. 312 305 */ 313 - err = mmc_select_card(card); 314 - if (err) 315 - goto remove; 306 + if (!mmc_host_is_spi(host)) { 307 + err = mmc_select_card(card); 308 + if (err) 309 + goto remove; 310 + } 316 311 317 312 /* 318 313 * Read the common registers.
+47 -19
drivers/mmc/core/sdio_ops.c
··· 30 30 31 31 cmd.opcode = SD_IO_SEND_OP_COND; 32 32 cmd.arg = ocr; 33 - cmd.flags = MMC_RSP_R4 | MMC_CMD_BCR; 33 + cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR; 34 34 35 35 for (i = 100; i; i--) { 36 36 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 37 37 if (err) 38 38 break; 39 39 40 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) 40 + /* if we're just probing, do a single pass */ 41 + if (ocr == 0) 41 42 break; 43 + 44 + /* otherwise wait until reset completes */ 45 + if (mmc_host_is_spi(host)) { 46 + /* 47 + * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate 48 + * an initialized card under SPI, but some cards 49 + * (Marvell's) only behave when looking at this 50 + * one. 51 + */ 52 + if (cmd.resp[1] & MMC_CARD_BUSY) 53 + break; 54 + } else { 55 + if (cmd.resp[0] & MMC_CARD_BUSY) 56 + break; 57 + } 42 58 43 59 err = -ETIMEDOUT; 44 60 ··· 62 46 } 63 47 64 48 if (rocr) 65 - *rocr = cmd.resp[0]; 49 + *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0]; 66 50 67 51 return err; 68 52 } ··· 84 68 cmd.arg |= (write && out) ? 0x08000000 : 0x00000000; 85 69 cmd.arg |= addr << 9; 86 70 cmd.arg |= in; 87 - cmd.flags = MMC_RSP_R5 | MMC_CMD_AC; 71 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC; 88 72 89 73 err = mmc_wait_for_cmd(card->host, &cmd, 0); 90 74 if (err) 91 75 return err; 92 76 93 - if (cmd.resp[0] & R5_ERROR) 94 - return -EIO; 95 - if (cmd.resp[0] & R5_FUNCTION_NUMBER) 96 - return -EINVAL; 97 - if (cmd.resp[0] & R5_OUT_OF_RANGE) 98 - return -ERANGE; 77 + if (mmc_host_is_spi(card->host)) { 78 + /* host driver already reported errors */ 79 + } else { 80 + if (cmd.resp[0] & R5_ERROR) 81 + return -EIO; 82 + if (cmd.resp[0] & R5_FUNCTION_NUMBER) 83 + return -EINVAL; 84 + if (cmd.resp[0] & R5_OUT_OF_RANGE) 85 + return -ERANGE; 86 + } 99 87 100 - if (out) 101 - *out = cmd.resp[0] & 0xFF; 88 + if (out) { 89 + if (mmc_host_is_spi(card->host)) 90 + *out = (cmd.resp[0] >> 8) & 0xFF; 91 + else 92 + *out = cmd.resp[0] & 0xFF; 93 + } 102 94 103 95 return 0; 104 96 } ··· 141 117 cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */ 142 118 else 143 119 cmd.arg |= 0x08000000 | blocks; /* block mode */ 144 - cmd.flags = MMC_RSP_R5 | MMC_CMD_ADTC; 120 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC; 145 121 146 122 data.blksz = blksz; 147 123 data.blocks = blocks; ··· 160 136 if (data.error) 161 137 return data.error; 162 138 163 - if (cmd.resp[0] & R5_ERROR) 164 - return -EIO; 165 - if (cmd.resp[0] & R5_FUNCTION_NUMBER) 166 - return -EINVAL; 167 - if (cmd.resp[0] & R5_OUT_OF_RANGE) 168 - return -ERANGE; 139 + if (mmc_host_is_spi(card->host)) { 140 + /* host driver already reported errors */ 141 + } else { 142 + if (cmd.resp[0] & R5_ERROR) 143 + return -EIO; 144 + if (cmd.resp[0] & R5_FUNCTION_NUMBER) 145 + return -EINVAL; 146 + if (cmd.resp[0] & R5_OUT_OF_RANGE) 147 + return -ERANGE; 148 + } 169 149 170 150 return 0; 171 151 }