Lines Matching +full:mmc +full:- +full:spi +full:- +full:slot
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Access SD/MMC cards through SPI master controllers
7 * (C) Copyright 2006-2007, David Brownell
9 * Hans-Peter Nilsson (hp@axis.com)
18 #include <linux/dma-mapping.h>
20 #include <linux/crc-itu-t.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
25 #include <linux/mmc/slot-gpio.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/mmc_spi.h>
35 * - For now, we won't try to interoperate with a real mmc/sd/sdio
37 * SPI protocol. The main reason for such configs would be mmc-ish
40 * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
44 * - MMC depends on a different chipselect management policy than the
45 * SPI interface currently supports for shared bus segments: it needs
52 * - We tell the controller to keep the chipselect active from the
54 * of SPI controller drivers that mis-handle the cs_change flag!
98 /* "scratch" is per-{command,block} data exchanged with the card */
106 struct mmc_host *mmc; member
107 struct spi_device *spi; member
122 /* underlying DMA-aware controller, or null */
141 * MMC-over-SPI protocol glue, used by the MMC stack interface
147 return spi_setup(host->spi); in mmc_cs_off()
155 if (len > sizeof(*host->data)) { in mmc_spi_readbytes()
157 return -EIO; in mmc_spi_readbytes()
160 host->status.len = len; in mmc_spi_readbytes()
162 if (host->dma_dev) in mmc_spi_readbytes()
163 dma_sync_single_for_device(host->dma_dev, in mmc_spi_readbytes()
164 host->data_dma, sizeof(*host->data), in mmc_spi_readbytes()
167 status = spi_sync_locked(host->spi, &host->readback); in mmc_spi_readbytes()
169 if (host->dma_dev) in mmc_spi_readbytes()
170 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_readbytes()
171 host->data_dma, sizeof(*host->data), in mmc_spi_readbytes()
180 u8 *cp = host->data->status; in mmc_spi_skip()
199 return -ETIMEDOUT; in mmc_spi_skip()
205 return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); in mmc_spi_wait_unbusy()
215 * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
219 * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
234 /* return zero, else negative errno after setting cmd->error */
239 u8 *cp = host->data->status; in mmc_spi_response_get()
240 u8 *end = cp + host->t.len; in mmc_spi_response_get()
249 cmd->opcode, maptype(cmd)); in mmc_spi_response_get()
263 cp = host->data->status; in mmc_spi_response_get()
266 /* Card sends N(CR) (== 1..8) bytes of all-ones then one in mmc_spi_response_get()
269 * REVISIT block read paths use nasty byte-at-a-time I/O in mmc_spi_response_get()
284 value = -ETIMEDOUT; in mmc_spi_response_get()
291 /* Houston, we have an ugly card with a bit-shifted response */ in mmc_spi_response_get()
298 cp = host->data->status; in mmc_spi_response_get()
306 cmd->resp[0] = rotator >> 8; in mmc_spi_response_get()
309 cmd->resp[0] = *cp++; in mmc_spi_response_get()
311 cmd->error = 0; in mmc_spi_response_get()
313 /* Status byte: the entire seven-bit R1 response. */ in mmc_spi_response_get()
314 if (cmd->resp[0] != 0) { in mmc_spi_response_get()
316 & cmd->resp[0]) in mmc_spi_response_get()
317 value = -EFAULT; /* Bad address */ in mmc_spi_response_get()
318 else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0]) in mmc_spi_response_get()
319 value = -ENOSYS; /* Function not implemented */ in mmc_spi_response_get()
320 else if (R1_SPI_COM_CRC & cmd->resp[0]) in mmc_spi_response_get()
321 value = -EILSEQ; /* Illegal byte sequence */ in mmc_spi_response_get()
323 & cmd->resp[0]) in mmc_spi_response_get()
324 value = -EIO; /* I/O error */ in mmc_spi_response_get()
330 /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads) in mmc_spi_response_get()
331 * and less-common stuff like various erase operations. in mmc_spi_response_get()
338 timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : in mmc_spi_response_get()
344 /* SPI R2 == R1 + second status byte; SEND_STATUS in mmc_spi_response_get()
345 * SPI R5 == R1 + data byte; IO_RW_DIRECT in mmc_spi_response_get()
353 cp = host->data->status; in mmc_spi_response_get()
359 cmd->resp[0] |= (rotator & 0xFF00); in mmc_spi_response_get()
361 cmd->resp[0] |= *cp << 8; in mmc_spi_response_get()
365 /* SPI R3, R4, or R7 == R1 + 4 bytes */ in mmc_spi_response_get()
368 cmd->resp[1] = 0; in mmc_spi_response_get()
370 cmd->resp[1] <<= 8; in mmc_spi_response_get()
376 cp = host->data->status; in mmc_spi_response_get()
381 cmd->resp[1] |= (rotator >> 8); in mmc_spi_response_get()
384 cmd->resp[1] |= *cp++; in mmc_spi_response_get()
389 /* SPI R1 == just one status byte */ in mmc_spi_response_get()
394 dev_dbg(&host->spi->dev, "bad response type %04x\n", in mmc_spi_response_get()
397 value = -EINVAL; in mmc_spi_response_get()
402 dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n", in mmc_spi_response_get()
403 tag, cmd->resp[0], cmd->resp[1]); in mmc_spi_response_get()
410 cmd->error = value; in mmc_spi_response_get()
418 * On error, caller must cope with mmc core retry mechanism. That
419 * means immediate low-level resubmit, which affects the bus lock...
426 struct scratch *data = host->data; in mmc_spi_command_send()
427 u8 *cp = data->status; in mmc_spi_command_send()
436 * - an all-ones byte to ensure the card is ready in mmc_spi_command_send()
437 * - opcode byte (plus start and transmission bits) in mmc_spi_command_send()
438 * - four bytes of big-endian argument in mmc_spi_command_send()
439 * - crc7 (plus end bit) ... always computed, it's cheap in mmc_spi_command_send()
441 * We init the whole buffer to all-ones, which is what we need in mmc_spi_command_send()
444 memset(cp, 0xff, sizeof(data->status)); in mmc_spi_command_send()
446 cp[1] = 0x40 | cmd->opcode; in mmc_spi_command_send()
447 put_unaligned_be32(cmd->arg, cp + 2); in mmc_spi_command_send()
451 /* Then, read up to 13 bytes (while writing all-ones): in mmc_spi_command_send()
452 * - N(CR) (== 1..8) bytes of all-ones in mmc_spi_command_send()
453 * - status byte (for all response types) in mmc_spi_command_send()
454 * - the rest of the response, either: in mmc_spi_command_send()
461 * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish in mmc_spi_command_send()
462 * - N(RC) (== 1..N) bytes of all-ones, before next command in mmc_spi_command_send()
463 * - N(WR) (== 1..N) bytes of all-ones, before data write in mmc_spi_command_send()
473 * - R1B responses need at least N(EC) bytes of all-zeroes. in mmc_spi_command_send()
478 * - Data block reads are more troublesome, since a variable in mmc_spi_command_send()
480 * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID in mmc_spi_command_send()
481 * + N(AC) (== 1..many) bytes of all-ones in mmc_spi_command_send()
486 if (cs_on && (mrq->data->flags & MMC_DATA_READ)) { in mmc_spi_command_send()
491 if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */ in mmc_spi_command_send()
493 else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */ in mmc_spi_command_send()
495 else if (cmd->flags & MMC_RSP_BUSY) /* R1B */ in mmc_spi_command_send()
496 cp = data->status + sizeof(data->status); in mmc_spi_command_send()
500 dev_dbg(&host->spi->dev, " CMD%d, resp %s\n", in mmc_spi_command_send()
501 cmd->opcode, maptype(cmd)); in mmc_spi_command_send()
504 spi_message_init(&host->m); in mmc_spi_command_send()
506 t = &host->t; in mmc_spi_command_send()
508 t->tx_buf = t->rx_buf = data->status; in mmc_spi_command_send()
509 t->tx_dma = t->rx_dma = host->data_dma; in mmc_spi_command_send()
510 t->len = cp - data->status; in mmc_spi_command_send()
511 t->cs_change = 1; in mmc_spi_command_send()
512 spi_message_add_tail(t, &host->m); in mmc_spi_command_send()
514 if (host->dma_dev) { in mmc_spi_command_send()
515 host->m.is_dma_mapped = 1; in mmc_spi_command_send()
516 dma_sync_single_for_device(host->dma_dev, in mmc_spi_command_send()
517 host->data_dma, sizeof(*host->data), in mmc_spi_command_send()
520 status = spi_sync_locked(host->spi, &host->m); in mmc_spi_command_send()
522 if (host->dma_dev) in mmc_spi_command_send()
523 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_command_send()
524 host->data_dma, sizeof(*host->data), in mmc_spi_command_send()
527 dev_dbg(&host->spi->dev, " ... write returned %d\n", status); in mmc_spi_command_send()
528 cmd->error = status; in mmc_spi_command_send()
532 /* after no-data commands and STOP_TRANSMISSION, chipselect off */ in mmc_spi_command_send()
540 * We always provide TX data for data and CRC. The MMC/SD protocol
544 * We also handle DMA mapping, so the underlying SPI controller does
554 struct scratch *scratch = host->data; in mmc_spi_setup_data_message()
555 dma_addr_t dma = host->data_dma; in mmc_spi_setup_data_message()
557 spi_message_init(&host->m); in mmc_spi_setup_data_message()
559 host->m.is_dma_mapped = 1; in mmc_spi_setup_data_message()
565 t = &host->token; in mmc_spi_setup_data_message()
567 t->len = 1; in mmc_spi_setup_data_message()
569 scratch->data_token = SPI_TOKEN_MULTI_WRITE; in mmc_spi_setup_data_message()
571 scratch->data_token = SPI_TOKEN_SINGLE; in mmc_spi_setup_data_message()
572 t->tx_buf = &scratch->data_token; in mmc_spi_setup_data_message()
574 t->tx_dma = dma + offsetof(struct scratch, data_token); in mmc_spi_setup_data_message()
575 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
579 * either TX-only, or RX with TX-ones. in mmc_spi_setup_data_message()
581 t = &host->t; in mmc_spi_setup_data_message()
583 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
584 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
586 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
588 t = &host->crc; in mmc_spi_setup_data_message()
590 t->len = 2; in mmc_spi_setup_data_message()
593 t->tx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
595 t->tx_dma = dma + offsetof(struct scratch, crc_val); in mmc_spi_setup_data_message()
597 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
598 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
599 t->rx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
601 t->rx_dma = dma + offsetof(struct scratch, crc_val); in mmc_spi_setup_data_message()
603 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
606 * A single block read is followed by N(EC) [0+] all-ones bytes in mmc_spi_setup_data_message()
609 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before in mmc_spi_setup_data_message()
613 * For a write, the one-byte data response follows immediately, then in mmc_spi_setup_data_message()
614 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes. in mmc_spi_setup_data_message()
617 * minimize I/O ops by using a single read to collect end-of-busy. in mmc_spi_setup_data_message()
620 t = &host->early_status; in mmc_spi_setup_data_message()
622 t->len = (direction == DMA_TO_DEVICE) ? sizeof(scratch->status) : 1; in mmc_spi_setup_data_message()
623 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
624 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
625 t->rx_buf = scratch->status; in mmc_spi_setup_data_message()
627 t->rx_dma = dma + offsetof(struct scratch, status); in mmc_spi_setup_data_message()
628 t->cs_change = 1; in mmc_spi_setup_data_message()
629 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
635 * - caller handled preceding N(WR) [1+] all-ones bytes
636 * - data block
640 * - an all-ones byte ... card writes a data-response byte
641 * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
649 struct spi_device *spi = host->spi; in mmc_spi_writeblock() local
651 struct scratch *scratch = host->data; in mmc_spi_writeblock()
654 if (host->mmc->use_spi_crc) in mmc_spi_writeblock()
655 scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len)); in mmc_spi_writeblock()
656 if (host->dma_dev) in mmc_spi_writeblock()
657 dma_sync_single_for_device(host->dma_dev, in mmc_spi_writeblock()
658 host->data_dma, sizeof(*scratch), in mmc_spi_writeblock()
661 status = spi_sync_locked(spi, &host->m); in mmc_spi_writeblock()
664 dev_dbg(&spi->dev, "write error (%d)\n", status); in mmc_spi_writeblock()
668 if (host->dma_dev) in mmc_spi_writeblock()
669 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_writeblock()
670 host->data_dma, sizeof(*scratch), in mmc_spi_writeblock()
674 * Get the transmission data-response reply. It must follow in mmc_spi_writeblock()
680 * In practice, there are (even modern SDHC-)cards which are late in mmc_spi_writeblock()
683 * bit-by-bit. Arggh!!! in mmc_spi_writeblock()
685 pattern = get_unaligned_be32(scratch->status); in mmc_spi_writeblock()
690 /* left-adjust to leading 0 bit */ in mmc_spi_writeblock()
693 /* right-adjust for pattern matching. Code is in bit 4..0 now. */ in mmc_spi_writeblock()
702 status = -EILSEQ; in mmc_spi_writeblock()
708 status = -EIO; in mmc_spi_writeblock()
711 status = -EPROTO; in mmc_spi_writeblock()
715 dev_dbg(&spi->dev, "write error %02x (%d)\n", in mmc_spi_writeblock()
716 scratch->status[0], status); in mmc_spi_writeblock()
720 t->tx_buf += t->len; in mmc_spi_writeblock()
721 if (host->dma_dev) in mmc_spi_writeblock()
722 t->tx_dma += t->len; in mmc_spi_writeblock()
727 for (i = 4; i < sizeof(scratch->status); i++) { in mmc_spi_writeblock()
728 /* card is non-busy if the most recent bit is 1 */ in mmc_spi_writeblock()
729 if (scratch->status[i] & 0x01) in mmc_spi_writeblock()
737 * - skip leading all-ones bytes ... either
740 * - data block
745 * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
755 struct spi_device *spi = host->spi; in mmc_spi_readblock() local
757 struct scratch *scratch = host->data; in mmc_spi_readblock()
761 /* At least one SD card sends an all-zeroes byte when N(CX) in mmc_spi_readblock()
762 * applies, before the all-ones bytes ... just cope with that. in mmc_spi_readblock()
767 status = scratch->status[0]; in mmc_spi_readblock()
772 dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); in mmc_spi_readblock()
776 /* The token may be bit-shifted... in mmc_spi_readblock()
777 * the first 0-bit precedes the data stream. in mmc_spi_readblock()
782 bitshift--; in mmc_spi_readblock()
786 if (host->dma_dev) { in mmc_spi_readblock()
787 dma_sync_single_for_device(host->dma_dev, in mmc_spi_readblock()
788 host->data_dma, sizeof(*scratch), in mmc_spi_readblock()
790 dma_sync_single_for_device(host->dma_dev, in mmc_spi_readblock()
791 t->rx_dma, t->len, in mmc_spi_readblock()
795 status = spi_sync_locked(spi, &host->m); in mmc_spi_readblock()
797 dev_dbg(&spi->dev, "read error %d\n", status); in mmc_spi_readblock()
801 if (host->dma_dev) { in mmc_spi_readblock()
802 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_readblock()
803 host->data_dma, sizeof(*scratch), in mmc_spi_readblock()
805 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_readblock()
806 t->rx_dma, t->len, in mmc_spi_readblock()
812 * all the magic to get byte-aligned data. in mmc_spi_readblock()
814 u8 *cp = t->rx_buf; in mmc_spi_readblock()
816 unsigned int bitright = 8 - bitshift; in mmc_spi_readblock()
818 for (len = t->len; len; len--) { in mmc_spi_readblock()
823 cp = (u8 *) &scratch->crc_val; in mmc_spi_readblock()
831 if (host->mmc->use_spi_crc) { in mmc_spi_readblock()
832 u16 crc = crc_itu_t(0, t->rx_buf, t->len); in mmc_spi_readblock()
834 be16_to_cpus(&scratch->crc_val); in mmc_spi_readblock()
835 if (scratch->crc_val != crc) { in mmc_spi_readblock()
836 dev_dbg(&spi->dev, in mmc_spi_readblock()
837 "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n", in mmc_spi_readblock()
838 scratch->crc_val, crc, t->len); in mmc_spi_readblock()
839 return -EILSEQ; in mmc_spi_readblock()
843 t->rx_buf += t->len; in mmc_spi_readblock()
844 if (host->dma_dev) in mmc_spi_readblock()
845 t->rx_dma += t->len; in mmc_spi_readblock()
851 * An MMC/SD data stage includes one or more blocks, optional CRCs,
853 * other SPI protocol stacks.
859 struct spi_device *spi = host->spi; in mmc_spi_data_do() local
860 struct device *dma_dev = host->dma_dev; in mmc_spi_data_do()
865 bool multiple = (data->blocks > 1); in mmc_spi_data_do()
871 t = &host->t; in mmc_spi_data_do()
873 if (t->speed_hz) in mmc_spi_data_do()
874 clock_rate = t->speed_hz; in mmc_spi_data_do()
876 clock_rate = spi->max_speed_hz; in mmc_spi_data_do()
878 timeout = data->timeout_ns / 1000 + in mmc_spi_data_do()
879 data->timeout_clks * 1000000 / clock_rate; in mmc_spi_data_do()
883 * each 512-byte block in mmc_spi_data_do()
885 for_each_sg(data->sg, sg, data->sg_len, n_sg) { in mmc_spi_data_do()
889 unsigned length = sg->length; in mmc_spi_data_do()
897 if ((sg->offset != 0 || length != PAGE_SIZE) in mmc_spi_data_do()
904 data->error = -EFAULT; in mmc_spi_data_do()
908 t->tx_dma = dma_addr + sg->offset; in mmc_spi_data_do()
910 t->rx_dma = dma_addr + sg->offset; in mmc_spi_data_do()
916 t->tx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
918 t->rx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
922 t->len = min(length, blk_size); in mmc_spi_data_do()
924 dev_dbg(&spi->dev, " %s block, %d bytes\n", write_or_read, t->len); in mmc_spi_data_do()
933 data->bytes_xfered += t->len; in mmc_spi_data_do()
934 length -= t->len; in mmc_spi_data_do()
948 data->error = status; in mmc_spi_data_do()
949 dev_dbg(&spi->dev, "%s status %d\n", write_or_read, status); in mmc_spi_data_do()
954 /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that in mmc_spi_data_do()
958 * MMC specs should sort that out before Linux starts using CMD23. in mmc_spi_data_do()
961 struct scratch *scratch = host->data; in mmc_spi_data_do()
963 const unsigned statlen = sizeof(scratch->status); in mmc_spi_data_do()
965 dev_dbg(&spi->dev, " STOP_TRAN\n"); in mmc_spi_data_do()
967 /* Tweak the per-block message we set up earlier by morphing in mmc_spi_data_do()
969 * all-ones bytes ... skip N(BR) (0..1), scan the rest for in mmc_spi_data_do()
972 INIT_LIST_HEAD(&host->m.transfers); in mmc_spi_data_do()
973 list_add(&host->early_status.transfer_list, in mmc_spi_data_do()
974 &host->m.transfers); in mmc_spi_data_do()
976 memset(scratch->status, 0xff, statlen); in mmc_spi_data_do()
977 scratch->status[0] = SPI_TOKEN_STOP_TRAN; in mmc_spi_data_do()
979 host->early_status.tx_buf = host->early_status.rx_buf; in mmc_spi_data_do()
980 host->early_status.tx_dma = host->early_status.rx_dma; in mmc_spi_data_do()
981 host->early_status.len = statlen; in mmc_spi_data_do()
983 if (host->dma_dev) in mmc_spi_data_do()
984 dma_sync_single_for_device(host->dma_dev, in mmc_spi_data_do()
985 host->data_dma, sizeof(*scratch), in mmc_spi_data_do()
988 tmp = spi_sync_locked(spi, &host->m); in mmc_spi_data_do()
990 if (host->dma_dev) in mmc_spi_data_do()
991 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_data_do()
992 host->data_dma, sizeof(*scratch), in mmc_spi_data_do()
996 if (!data->error) in mmc_spi_data_do()
997 data->error = tmp; in mmc_spi_data_do()
1002 * avoiding wasteful byte-at-a-time scanning... but more in mmc_spi_data_do()
1006 if (scratch->status[tmp] != 0) in mmc_spi_data_do()
1010 if (tmp < 0 && !data->error) in mmc_spi_data_do()
1011 data->error = tmp; in mmc_spi_data_do()
1018 * MMC driver implementation -- the interface to the MMC stack
1021 static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq) in mmc_spi_request() argument
1023 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_request()
1024 int status = -EINVAL; in mmc_spi_request()
1029 /* MMC core and layered drivers *MUST* issue SPI-aware commands */ in mmc_spi_request()
1034 cmd = mrq->cmd; in mmc_spi_request()
1036 dev_dbg(&host->spi->dev, "bogus command\n"); in mmc_spi_request()
1037 cmd->error = -EINVAL; in mmc_spi_request()
1041 cmd = mrq->stop; in mmc_spi_request()
1043 dev_dbg(&host->spi->dev, "bogus STOP command\n"); in mmc_spi_request()
1044 cmd->error = -EINVAL; in mmc_spi_request()
1050 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
1057 spi_bus_lock(host->spi->master); in mmc_spi_request()
1061 status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL); in mmc_spi_request()
1062 if (status == 0 && mrq->data) { in mmc_spi_request()
1063 mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz); in mmc_spi_request()
1066 * The SPI bus is not always reliable for large data transfers. in mmc_spi_request()
1068 * data read/write over SPI, it may be recovered by repeating in mmc_spi_request()
1072 if (mrq->data->error == -EILSEQ && crc_retry) { in mmc_spi_request()
1077 crc_retry--; in mmc_spi_request()
1078 mrq->data->error = 0; in mmc_spi_request()
1082 if (mrq->stop) in mmc_spi_request()
1083 status = mmc_spi_command_send(host, mrq, mrq->stop, 0); in mmc_spi_request()
1089 spi_bus_unlock(host->spi->master); in mmc_spi_request()
1091 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
1097 * not all MMC/SD sockets support power switching.
1099 * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1105 * wait till not-busy, skip debris from any old commands. in mmc_spi_initsequence()
1111 * Do a burst with chipselect active-high. We need to do this to in mmc_spi_initsequence()
1119 * Note that this is one of the places MMC/SD plays games with the in mmc_spi_initsequence()
1120 * SPI protocol. Another is that when chipselect is released while in mmc_spi_initsequence()
1129 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1130 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1132 dev_warn(&host->spi->dev, in mmc_spi_initsequence()
1133 "can't change chip-select polarity\n"); in mmc_spi_initsequence()
1134 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1138 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1139 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1141 dev_err(&host->spi->dev, in mmc_spi_initsequence()
1142 "can't restore chip-select polarity\n"); in mmc_spi_initsequence()
1157 static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) in mmc_spi_set_ios() argument
1159 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_set_ios()
1161 if (host->power_mode != ios->power_mode) { in mmc_spi_set_ios()
1164 canpower = host->pdata && host->pdata->setpower; in mmc_spi_set_ios()
1166 dev_dbg(&host->spi->dev, "power %s (%d)%s\n", in mmc_spi_set_ios()
1167 mmc_powerstring(ios->power_mode), in mmc_spi_set_ios()
1168 ios->vdd, in mmc_spi_set_ios()
1175 switch (ios->power_mode) { in mmc_spi_set_ios()
1178 host->pdata->setpower(&host->spi->dev, in mmc_spi_set_ios()
1179 ios->vdd); in mmc_spi_set_ios()
1180 if (ios->power_mode == MMC_POWER_UP) in mmc_spi_set_ios()
1181 msleep(host->powerup_msecs); in mmc_spi_set_ios()
1186 if (ios->power_mode == MMC_POWER_ON) in mmc_spi_set_ios()
1190 * delivery from data lines! On a shared SPI bus, this in mmc_spi_set_ios()
1194 * - Clock low means CPOL 0, e.g. mode 0 in mmc_spi_set_ios()
1195 * - MOSI low comes from writing zero in mmc_spi_set_ios()
1196 * - Chipselect is usually active low... in mmc_spi_set_ios()
1198 if (canpower && ios->power_mode == MMC_POWER_OFF) { in mmc_spi_set_ios()
1202 host->spi->mode &= ~(SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1203 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1205 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1206 "switch to SPI mode 0 failed\n"); in mmc_spi_set_ios()
1208 if (spi_write(host->spi, &nullbyte, 1) < 0) in mmc_spi_set_ios()
1209 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1210 "put spi signals to low failed\n"); in mmc_spi_set_ios()
1213 * Now clock should be low due to spi mode 0; in mmc_spi_set_ios()
1216 * power supply is off, so now MMC is off too! in mmc_spi_set_ios()
1223 host->spi->mode |= (SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1224 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1226 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1227 "switch back to SPI mode 3 failed\n"); in mmc_spi_set_ios()
1231 host->power_mode = ios->power_mode; in mmc_spi_set_ios()
1234 if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) { in mmc_spi_set_ios()
1237 host->spi->max_speed_hz = ios->clock; in mmc_spi_set_ios()
1238 status = spi_setup(host->spi); in mmc_spi_set_ios()
1239 dev_dbg(&host->spi->dev, " clock to %d Hz, %d\n", in mmc_spi_set_ios()
1240 host->spi->max_speed_hz, status); in mmc_spi_set_ios()
1255 * SPI driver implementation
1259 mmc_spi_detect_irq(int irq, void *mmc) in mmc_spi_detect_irq() argument
1261 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_detect_irq()
1262 u16 delay_msec = max(host->pdata->detect_delay, (u16)100); in mmc_spi_detect_irq()
1264 mmc_detect_change(mmc, msecs_to_jiffies(delay_msec)); in mmc_spi_detect_irq()
1271 struct spi_device *spi = host->spi; in mmc_spi_dma_alloc() local
1274 if (!spi->master->dev.parent->dma_mask) in mmc_spi_dma_alloc()
1277 dev = spi->master->dev.parent; in mmc_spi_dma_alloc()
1279 host->ones_dma = dma_map_single(dev, host->ones, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_alloc()
1281 if (dma_mapping_error(dev, host->ones_dma)) in mmc_spi_dma_alloc()
1282 return -ENOMEM; in mmc_spi_dma_alloc()
1284 host->data_dma = dma_map_single(dev, host->data, sizeof(*host->data), in mmc_spi_dma_alloc()
1286 if (dma_mapping_error(dev, host->data_dma)) { in mmc_spi_dma_alloc()
1287 dma_unmap_single(dev, host->ones_dma, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_alloc()
1289 return -ENOMEM; in mmc_spi_dma_alloc()
1292 dma_sync_single_for_cpu(dev, host->data_dma, sizeof(*host->data), in mmc_spi_dma_alloc()
1295 host->dma_dev = dev; in mmc_spi_dma_alloc()
1301 if (!host->dma_dev) in mmc_spi_dma_free()
1304 dma_unmap_single(host->dma_dev, host->ones_dma, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_free()
1306 dma_unmap_single(host->dma_dev, host->data_dma, sizeof(*host->data), in mmc_spi_dma_free()
1314 static int mmc_spi_probe(struct spi_device *spi) in mmc_spi_probe() argument
1317 struct mmc_host *mmc; in mmc_spi_probe() local
1323 * per-transfer overheads (by making fewer transfers). in mmc_spi_probe()
1325 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) in mmc_spi_probe()
1326 return -EINVAL; in mmc_spi_probe()
1328 /* MMC and SD specs only seem to care that sampling is on the in mmc_spi_probe()
1329 * rising edge ... meaning SPI modes 0 or 3. So either SPI mode in mmc_spi_probe()
1334 if (spi->mode != SPI_MODE_3) in mmc_spi_probe()
1335 spi->mode = SPI_MODE_0; in mmc_spi_probe()
1336 spi->bits_per_word = 8; in mmc_spi_probe()
1338 status = spi_setup(spi); in mmc_spi_probe()
1340 dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n", in mmc_spi_probe()
1341 spi->mode, spi->max_speed_hz / 1000, in mmc_spi_probe()
1349 * NOTE if many systems use more than one MMC-over-SPI connector in mmc_spi_probe()
1352 status = -ENOMEM; in mmc_spi_probe()
1358 mmc = mmc_alloc_host(sizeof(*host), &spi->dev); in mmc_spi_probe()
1359 if (!mmc) in mmc_spi_probe()
1362 mmc->ops = &mmc_spi_ops; in mmc_spi_probe()
1363 mmc->max_blk_size = MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1364 mmc->max_segs = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1365 mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1366 mmc->max_blk_count = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1368 mmc->caps = MMC_CAP_SPI; in mmc_spi_probe()
1370 /* SPI doesn't need the lowspeed device identification thing for in mmc_spi_probe()
1371 * MMC or SD cards, since it never comes up in open drain mode. in mmc_spi_probe()
1372 * That's good; some SPI masters can't handle very low speeds! in mmc_spi_probe()
1378 mmc->f_min = 400000; in mmc_spi_probe()
1379 mmc->f_max = spi->max_speed_hz; in mmc_spi_probe()
1381 host = mmc_priv(mmc); in mmc_spi_probe()
1382 host->mmc = mmc; in mmc_spi_probe()
1383 host->spi = spi; in mmc_spi_probe()
1385 host->ones = ones; in mmc_spi_probe()
1387 dev_set_drvdata(&spi->dev, mmc); in mmc_spi_probe()
1392 host->pdata = mmc_spi_get_pdata(spi); in mmc_spi_probe()
1393 if (host->pdata) in mmc_spi_probe()
1394 mmc->ocr_avail = host->pdata->ocr_mask; in mmc_spi_probe()
1395 if (!mmc->ocr_avail) { in mmc_spi_probe()
1396 dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n"); in mmc_spi_probe()
1397 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34; in mmc_spi_probe()
1399 if (host->pdata && host->pdata->setpower) { in mmc_spi_probe()
1400 host->powerup_msecs = host->pdata->powerup_msecs; in mmc_spi_probe()
1401 if (!host->powerup_msecs || host->powerup_msecs > 250) in mmc_spi_probe()
1402 host->powerup_msecs = 250; in mmc_spi_probe()
1406 host->data = kmalloc(sizeof(*host->data), GFP_KERNEL); in mmc_spi_probe()
1407 if (!host->data) in mmc_spi_probe()
1415 spi_message_init(&host->readback); in mmc_spi_probe()
1416 host->readback.is_dma_mapped = (host->dma_dev != NULL); in mmc_spi_probe()
1418 spi_message_add_tail(&host->status, &host->readback); in mmc_spi_probe()
1419 host->status.tx_buf = host->ones; in mmc_spi_probe()
1420 host->status.tx_dma = host->ones_dma; in mmc_spi_probe()
1421 host->status.rx_buf = &host->data->status; in mmc_spi_probe()
1422 host->status.rx_dma = host->data_dma + offsetof(struct scratch, status); in mmc_spi_probe()
1423 host->status.cs_change = 1; in mmc_spi_probe()
1426 if (host->pdata && host->pdata->init) { in mmc_spi_probe()
1427 status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc); in mmc_spi_probe()
1433 if (host->pdata) { in mmc_spi_probe()
1434 mmc->caps |= host->pdata->caps; in mmc_spi_probe()
1435 mmc->caps2 |= host->pdata->caps2; in mmc_spi_probe()
1438 status = mmc_add_host(mmc); in mmc_spi_probe()
1446 status = mmc_gpiod_request_cd(mmc, NULL, 0, false, 1000); in mmc_spi_probe()
1447 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1455 mmc->caps &= ~MMC_CAP_NEEDS_POLL; in mmc_spi_probe()
1456 mmc_gpiod_request_cd_irq(mmc); in mmc_spi_probe()
1458 mmc_detect_change(mmc, 0); in mmc_spi_probe()
1461 status = mmc_gpiod_request_ro(mmc, NULL, 1, 0); in mmc_spi_probe()
1462 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1467 dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n", in mmc_spi_probe()
1468 dev_name(&mmc->class_dev), in mmc_spi_probe()
1469 host->dma_dev ? "" : ", no DMA", in mmc_spi_probe()
1471 (host->pdata && host->pdata->setpower) in mmc_spi_probe()
1473 (mmc->caps & MMC_CAP_NEEDS_POLL) in mmc_spi_probe()
1478 mmc_remove_host(mmc); in mmc_spi_probe()
1482 kfree(host->data); in mmc_spi_probe()
1484 mmc_spi_put_pdata(spi); in mmc_spi_probe()
1485 mmc_free_host(mmc); in mmc_spi_probe()
1492 static void mmc_spi_remove(struct spi_device *spi) in mmc_spi_remove() argument
1494 struct mmc_host *mmc = dev_get_drvdata(&spi->dev); in mmc_spi_remove() local
1495 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_remove()
1498 if (host->pdata && host->pdata->exit) in mmc_spi_remove()
1499 host->pdata->exit(&spi->dev, mmc); in mmc_spi_remove()
1501 mmc_remove_host(mmc); in mmc_spi_remove()
1504 kfree(host->data); in mmc_spi_remove()
1505 kfree(host->ones); in mmc_spi_remove()
1507 spi->max_speed_hz = mmc->f_max; in mmc_spi_remove()
1508 mmc_spi_put_pdata(spi); in mmc_spi_remove()
1509 mmc_free_host(mmc); in mmc_spi_remove()
1513 { "mmc-spi-slot"},
1516 MODULE_DEVICE_TABLE(spi, mmc_spi_dev_ids);
1519 { .compatible = "mmc-spi-slot", },
1536 MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
1537 MODULE_DESCRIPTION("SPI SD/MMC host driver");
1539 MODULE_ALIAS("spi:mmc_spi");