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)
19 #include <linux/crc-itu-t.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
24 #include <linux/mmc/slot-gpio.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/mmc_spi.h>
34 * - For now, we won't try to interoperate with a real mmc/sd/sdio
36 * SPI protocol. The main reason for such configs would be mmc-ish
39 * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
43 * - MMC depends on a different chipselect management policy than the
44 * SPI interface currently supports for shared bus segments: it needs
51 * - We tell the controller to keep the chipselect active from the
53 * of SPI controller drivers that mis-handle the cs_change flag!
97 /* "scratch" is per-{command,block} data exchanged with the card */
105 struct mmc_host *mmc; member
106 struct spi_device *spi; member
135 * MMC-over-SPI protocol glue, used by the MMC stack interface
141 return spi_setup(host->spi); in mmc_cs_off()
146 if (len > sizeof(*host->data)) { in mmc_spi_readbytes()
148 return -EIO; in mmc_spi_readbytes()
151 host->status.len = len; in mmc_spi_readbytes()
153 return spi_sync_locked(host->spi, &host->readback); in mmc_spi_readbytes()
159 u8 *cp = host->data->status; in mmc_spi_skip()
178 return -ETIMEDOUT; in mmc_spi_skip()
184 return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); in mmc_spi_wait_unbusy()
194 * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
198 * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
213 /* return zero, else negative errno after setting cmd->error */
218 u8 *cp = host->data->status; in mmc_spi_response_get()
219 u8 *end = cp + host->t.len; in mmc_spi_response_get()
228 cmd->opcode, maptype(cmd)); in mmc_spi_response_get()
242 cp = host->data->status; in mmc_spi_response_get()
245 /* Card sends N(CR) (== 1..8) bytes of all-ones then one in mmc_spi_response_get()
248 * REVISIT block read paths use nasty byte-at-a-time I/O in mmc_spi_response_get()
263 value = -ETIMEDOUT; in mmc_spi_response_get()
270 /* Houston, we have an ugly card with a bit-shifted response */ in mmc_spi_response_get()
277 cp = host->data->status; in mmc_spi_response_get()
285 cmd->resp[0] = rotator >> 8; in mmc_spi_response_get()
288 cmd->resp[0] = *cp++; in mmc_spi_response_get()
290 cmd->error = 0; in mmc_spi_response_get()
292 /* Status byte: the entire seven-bit R1 response. */ in mmc_spi_response_get()
293 if (cmd->resp[0] != 0) { in mmc_spi_response_get()
295 & cmd->resp[0]) in mmc_spi_response_get()
296 value = -EFAULT; /* Bad address */ in mmc_spi_response_get()
297 else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0]) in mmc_spi_response_get()
298 value = -ENOSYS; /* Function not implemented */ in mmc_spi_response_get()
299 else if (R1_SPI_COM_CRC & cmd->resp[0]) in mmc_spi_response_get()
300 value = -EILSEQ; /* Illegal byte sequence */ in mmc_spi_response_get()
302 & cmd->resp[0]) in mmc_spi_response_get()
303 value = -EIO; /* I/O error */ in mmc_spi_response_get()
309 /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads) in mmc_spi_response_get()
310 * and less-common stuff like various erase operations. in mmc_spi_response_get()
317 timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : in mmc_spi_response_get()
323 /* SPI R2 == R1 + second status byte; SEND_STATUS in mmc_spi_response_get()
324 * SPI R5 == R1 + data byte; IO_RW_DIRECT in mmc_spi_response_get()
332 cp = host->data->status; in mmc_spi_response_get()
338 cmd->resp[0] |= (rotator & 0xFF00); in mmc_spi_response_get()
340 cmd->resp[0] |= *cp << 8; in mmc_spi_response_get()
344 /* SPI R3, R4, or R7 == R1 + 4 bytes */ in mmc_spi_response_get()
347 cmd->resp[1] = 0; in mmc_spi_response_get()
349 cmd->resp[1] <<= 8; in mmc_spi_response_get()
355 cp = host->data->status; in mmc_spi_response_get()
360 cmd->resp[1] |= (rotator >> 8); in mmc_spi_response_get()
363 cmd->resp[1] |= *cp++; in mmc_spi_response_get()
368 /* SPI R1 == just one status byte */ in mmc_spi_response_get()
373 dev_dbg(&host->spi->dev, "bad response type %04x\n", in mmc_spi_response_get()
376 value = -EINVAL; in mmc_spi_response_get()
381 dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n", in mmc_spi_response_get()
382 tag, cmd->resp[0], cmd->resp[1]); in mmc_spi_response_get()
389 cmd->error = value; in mmc_spi_response_get()
397 * On error, caller must cope with mmc core retry mechanism. That
398 * means immediate low-level resubmit, which affects the bus lock...
405 struct scratch *data = host->data; in mmc_spi_command_send()
406 u8 *cp = data->status; in mmc_spi_command_send()
415 * - an all-ones byte to ensure the card is ready in mmc_spi_command_send()
416 * - opcode byte (plus start and transmission bits) in mmc_spi_command_send()
417 * - four bytes of big-endian argument in mmc_spi_command_send()
418 * - crc7 (plus end bit) ... always computed, it's cheap in mmc_spi_command_send()
420 * We init the whole buffer to all-ones, which is what we need in mmc_spi_command_send()
423 memset(cp, 0xff, sizeof(data->status)); in mmc_spi_command_send()
425 cp[1] = 0x40 | cmd->opcode; in mmc_spi_command_send()
426 put_unaligned_be32(cmd->arg, cp + 2); in mmc_spi_command_send()
430 /* Then, read up to 13 bytes (while writing all-ones): in mmc_spi_command_send()
431 * - N(CR) (== 1..8) bytes of all-ones in mmc_spi_command_send()
432 * - status byte (for all response types) in mmc_spi_command_send()
433 * - the rest of the response, either: in mmc_spi_command_send()
440 * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish in mmc_spi_command_send()
441 * - N(RC) (== 1..N) bytes of all-ones, before next command in mmc_spi_command_send()
442 * - N(WR) (== 1..N) bytes of all-ones, before data write in mmc_spi_command_send()
452 * - R1B responses need at least N(EC) bytes of all-zeroes. in mmc_spi_command_send()
457 * - Data block reads are more troublesome, since a variable in mmc_spi_command_send()
459 * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID in mmc_spi_command_send()
460 * + N(AC) (== 1..many) bytes of all-ones in mmc_spi_command_send()
465 if (cs_on && (mrq->data->flags & MMC_DATA_READ)) { in mmc_spi_command_send()
470 if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */ in mmc_spi_command_send()
472 else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */ in mmc_spi_command_send()
474 else if (cmd->flags & MMC_RSP_BUSY) /* R1B */ in mmc_spi_command_send()
475 cp = data->status + sizeof(data->status); in mmc_spi_command_send()
479 dev_dbg(&host->spi->dev, " CMD%d, resp %s\n", in mmc_spi_command_send()
480 cmd->opcode, maptype(cmd)); in mmc_spi_command_send()
483 spi_message_init(&host->m); in mmc_spi_command_send()
485 t = &host->t; in mmc_spi_command_send()
487 t->tx_buf = t->rx_buf = data->status; in mmc_spi_command_send()
488 t->len = cp - data->status; in mmc_spi_command_send()
489 t->cs_change = 1; in mmc_spi_command_send()
490 spi_message_add_tail(t, &host->m); in mmc_spi_command_send()
492 status = spi_sync_locked(host->spi, &host->m); in mmc_spi_command_send()
494 dev_dbg(&host->spi->dev, " ... write returned %d\n", status); in mmc_spi_command_send()
495 cmd->error = status; in mmc_spi_command_send()
499 /* after no-data commands and STOP_TRANSMISSION, chipselect off */ in mmc_spi_command_send()
507 * We always provide TX data for data and CRC. The MMC/SD protocol
515 struct scratch *scratch = host->data; in mmc_spi_setup_data_message()
517 spi_message_init(&host->m); in mmc_spi_setup_data_message()
523 t = &host->token; in mmc_spi_setup_data_message()
525 t->len = 1; in mmc_spi_setup_data_message()
527 scratch->data_token = SPI_TOKEN_MULTI_WRITE; in mmc_spi_setup_data_message()
529 scratch->data_token = SPI_TOKEN_SINGLE; in mmc_spi_setup_data_message()
530 t->tx_buf = &scratch->data_token; in mmc_spi_setup_data_message()
531 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
535 * either TX-only, or RX with TX-ones. in mmc_spi_setup_data_message()
537 t = &host->t; in mmc_spi_setup_data_message()
539 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
541 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
543 t = &host->crc; in mmc_spi_setup_data_message()
545 t->len = 2; in mmc_spi_setup_data_message()
548 t->tx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
550 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
551 t->rx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
553 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
556 * A single block read is followed by N(EC) [0+] all-ones bytes in mmc_spi_setup_data_message()
559 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before in mmc_spi_setup_data_message()
563 * For a write, the one-byte data response follows immediately, then in mmc_spi_setup_data_message()
564 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes. in mmc_spi_setup_data_message()
567 * minimize I/O ops by using a single read to collect end-of-busy. in mmc_spi_setup_data_message()
570 t = &host->early_status; in mmc_spi_setup_data_message()
572 t->len = write ? sizeof(scratch->status) : 1; in mmc_spi_setup_data_message()
573 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
574 t->rx_buf = scratch->status; in mmc_spi_setup_data_message()
575 t->cs_change = 1; in mmc_spi_setup_data_message()
576 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
582 * - caller handled preceding N(WR) [1+] all-ones bytes
583 * - data block
587 * - an all-ones byte ... card writes a data-response byte
588 * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
596 struct spi_device *spi = host->spi; in mmc_spi_writeblock() local
598 struct scratch *scratch = host->data; in mmc_spi_writeblock()
601 if (host->mmc->use_spi_crc) in mmc_spi_writeblock()
602 scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len)); in mmc_spi_writeblock()
604 status = spi_sync_locked(spi, &host->m); in mmc_spi_writeblock()
606 dev_dbg(&spi->dev, "write error (%d)\n", status); in mmc_spi_writeblock()
611 * Get the transmission data-response reply. It must follow in mmc_spi_writeblock()
617 * In practice, there are (even modern SDHC-)cards which are late in mmc_spi_writeblock()
620 * bit-by-bit. Arggh!!! in mmc_spi_writeblock()
622 pattern = get_unaligned_be32(scratch->status); in mmc_spi_writeblock()
627 /* left-adjust to leading 0 bit */ in mmc_spi_writeblock()
630 /* right-adjust for pattern matching. Code is in bit 4..0 now. */ in mmc_spi_writeblock()
639 status = -EILSEQ; in mmc_spi_writeblock()
645 status = -EIO; in mmc_spi_writeblock()
648 status = -EPROTO; in mmc_spi_writeblock()
652 dev_dbg(&spi->dev, "write error %02x (%d)\n", in mmc_spi_writeblock()
653 scratch->status[0], status); in mmc_spi_writeblock()
657 t->tx_buf += t->len; in mmc_spi_writeblock()
662 for (i = 4; i < sizeof(scratch->status); i++) { in mmc_spi_writeblock()
663 /* card is non-busy if the most recent bit is 1 */ in mmc_spi_writeblock()
664 if (scratch->status[i] & 0x01) in mmc_spi_writeblock()
672 * - skip leading all-ones bytes ... either
675 * - data block
680 * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
690 struct spi_device *spi = host->spi; in mmc_spi_readblock() local
692 struct scratch *scratch = host->data; in mmc_spi_readblock()
696 /* At least one SD card sends an all-zeroes byte when N(CX) in mmc_spi_readblock()
697 * applies, before the all-ones bytes ... just cope with that. in mmc_spi_readblock()
702 status = scratch->status[0]; in mmc_spi_readblock()
707 dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); in mmc_spi_readblock()
711 /* The token may be bit-shifted... in mmc_spi_readblock()
712 * the first 0-bit precedes the data stream. in mmc_spi_readblock()
717 bitshift--; in mmc_spi_readblock()
721 status = spi_sync_locked(spi, &host->m); in mmc_spi_readblock()
723 dev_dbg(&spi->dev, "read error %d\n", status); in mmc_spi_readblock()
729 * all the magic to get byte-aligned data. in mmc_spi_readblock()
731 u8 *cp = t->rx_buf; in mmc_spi_readblock()
733 unsigned int bitright = 8 - bitshift; in mmc_spi_readblock()
735 for (len = t->len; len; len--) { in mmc_spi_readblock()
740 cp = (u8 *) &scratch->crc_val; in mmc_spi_readblock()
748 if (host->mmc->use_spi_crc) { in mmc_spi_readblock()
749 u16 crc = crc_itu_t(0, t->rx_buf, t->len); in mmc_spi_readblock()
751 be16_to_cpus(&scratch->crc_val); in mmc_spi_readblock()
752 if (scratch->crc_val != crc) { in mmc_spi_readblock()
753 dev_dbg(&spi->dev, in mmc_spi_readblock()
754 "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n", in mmc_spi_readblock()
755 scratch->crc_val, crc, t->len); in mmc_spi_readblock()
756 return -EILSEQ; in mmc_spi_readblock()
760 t->rx_buf += t->len; in mmc_spi_readblock()
766 * An MMC/SD data stage includes one or more blocks, optional CRCs,
768 * other SPI protocol stacks.
774 struct spi_device *spi = host->spi; in mmc_spi_data_do() local
778 bool multiple = (data->blocks > 1); in mmc_spi_data_do()
779 bool write = (data->flags & MMC_DATA_WRITE); in mmc_spi_data_do()
785 t = &host->t; in mmc_spi_data_do()
787 if (t->speed_hz) in mmc_spi_data_do()
788 clock_rate = t->speed_hz; in mmc_spi_data_do()
790 clock_rate = spi->max_speed_hz; in mmc_spi_data_do()
792 timeout = data->timeout_ns / 1000 + in mmc_spi_data_do()
793 data->timeout_clks * 1000000 / clock_rate; in mmc_spi_data_do()
797 * each 512-byte block in mmc_spi_data_do()
799 for_each_sg(data->sg, sg, data->sg_len, n_sg) { in mmc_spi_data_do()
802 unsigned length = sg->length; in mmc_spi_data_do()
807 t->tx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
809 t->rx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
813 t->len = min(length, blk_size); in mmc_spi_data_do()
815 dev_dbg(&spi->dev, " %s block, %d bytes\n", write_or_read, t->len); in mmc_spi_data_do()
824 data->bytes_xfered += t->len; in mmc_spi_data_do()
825 length -= t->len; in mmc_spi_data_do()
839 data->error = status; in mmc_spi_data_do()
840 dev_dbg(&spi->dev, "%s status %d\n", write_or_read, status); in mmc_spi_data_do()
845 /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that in mmc_spi_data_do()
849 * MMC specs should sort that out before Linux starts using CMD23. in mmc_spi_data_do()
852 struct scratch *scratch = host->data; in mmc_spi_data_do()
854 const unsigned statlen = sizeof(scratch->status); in mmc_spi_data_do()
856 dev_dbg(&spi->dev, " STOP_TRAN\n"); in mmc_spi_data_do()
858 /* Tweak the per-block message we set up earlier by morphing in mmc_spi_data_do()
860 * all-ones bytes ... skip N(BR) (0..1), scan the rest for in mmc_spi_data_do()
863 INIT_LIST_HEAD(&host->m.transfers); in mmc_spi_data_do()
864 list_add(&host->early_status.transfer_list, in mmc_spi_data_do()
865 &host->m.transfers); in mmc_spi_data_do()
867 memset(scratch->status, 0xff, statlen); in mmc_spi_data_do()
868 scratch->status[0] = SPI_TOKEN_STOP_TRAN; in mmc_spi_data_do()
870 host->early_status.tx_buf = host->early_status.rx_buf; in mmc_spi_data_do()
871 host->early_status.len = statlen; in mmc_spi_data_do()
873 tmp = spi_sync_locked(spi, &host->m); in mmc_spi_data_do()
875 if (!data->error) in mmc_spi_data_do()
876 data->error = tmp; in mmc_spi_data_do()
881 * avoiding wasteful byte-at-a-time scanning... but more in mmc_spi_data_do()
885 if (scratch->status[tmp] != 0) in mmc_spi_data_do()
889 if (tmp < 0 && !data->error) in mmc_spi_data_do()
890 data->error = tmp; in mmc_spi_data_do()
897 * MMC driver implementation -- the interface to the MMC stack
900 static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq) in mmc_spi_request() argument
902 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_request()
903 int status = -EINVAL; in mmc_spi_request()
908 /* MMC core and layered drivers *MUST* issue SPI-aware commands */ in mmc_spi_request()
913 cmd = mrq->cmd; in mmc_spi_request()
915 dev_dbg(&host->spi->dev, "bogus command\n"); in mmc_spi_request()
916 cmd->error = -EINVAL; in mmc_spi_request()
920 cmd = mrq->stop; in mmc_spi_request()
922 dev_dbg(&host->spi->dev, "bogus STOP command\n"); in mmc_spi_request()
923 cmd->error = -EINVAL; in mmc_spi_request()
929 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
936 spi_bus_lock(host->spi->controller); in mmc_spi_request()
940 status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL); in mmc_spi_request()
941 if (status == 0 && mrq->data) { in mmc_spi_request()
942 mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz); in mmc_spi_request()
945 * The SPI bus is not always reliable for large data transfers. in mmc_spi_request()
947 * data read/write over SPI, it may be recovered by repeating in mmc_spi_request()
951 if (mrq->data->error == -EILSEQ && crc_retry) { in mmc_spi_request()
956 crc_retry--; in mmc_spi_request()
957 mrq->data->error = 0; in mmc_spi_request()
961 if (mrq->stop) in mmc_spi_request()
962 status = mmc_spi_command_send(host, mrq, mrq->stop, 0); in mmc_spi_request()
968 spi_bus_unlock(host->spi->controller); in mmc_spi_request()
970 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
976 * not all MMC/SD sockets support power switching.
978 * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
984 * wait till not-busy, skip debris from any old commands. in mmc_spi_initsequence()
990 * Do a burst with chipselect active-high. We need to do this to in mmc_spi_initsequence()
998 * Note that this is one of the places MMC/SD plays games with the in mmc_spi_initsequence()
999 * SPI protocol. Another is that when chipselect is released while in mmc_spi_initsequence()
1008 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1009 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1011 dev_warn(&host->spi->dev, in mmc_spi_initsequence()
1012 "can't change chip-select polarity\n"); in mmc_spi_initsequence()
1013 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1017 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1018 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1020 dev_err(&host->spi->dev, in mmc_spi_initsequence()
1021 "can't restore chip-select polarity\n"); in mmc_spi_initsequence()
1036 static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) in mmc_spi_set_ios() argument
1038 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_set_ios()
1040 if (host->power_mode != ios->power_mode) { in mmc_spi_set_ios()
1043 canpower = host->pdata && host->pdata->setpower; in mmc_spi_set_ios()
1045 dev_dbg(&host->spi->dev, "power %s (%d)%s\n", in mmc_spi_set_ios()
1046 mmc_powerstring(ios->power_mode), in mmc_spi_set_ios()
1047 ios->vdd, in mmc_spi_set_ios()
1054 switch (ios->power_mode) { in mmc_spi_set_ios()
1057 host->pdata->setpower(&host->spi->dev, in mmc_spi_set_ios()
1058 ios->vdd); in mmc_spi_set_ios()
1059 if (ios->power_mode == MMC_POWER_UP) in mmc_spi_set_ios()
1060 msleep(host->powerup_msecs); in mmc_spi_set_ios()
1065 if (ios->power_mode == MMC_POWER_ON) in mmc_spi_set_ios()
1069 * delivery from data lines! On a shared SPI bus, this in mmc_spi_set_ios()
1073 * - Clock low means CPOL 0, e.g. mode 0 in mmc_spi_set_ios()
1074 * - MOSI low comes from writing zero in mmc_spi_set_ios()
1075 * - Chipselect is usually active low... in mmc_spi_set_ios()
1077 if (canpower && ios->power_mode == MMC_POWER_OFF) { in mmc_spi_set_ios()
1081 host->spi->mode &= ~(SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1082 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1084 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1085 "switch to SPI mode 0 failed\n"); in mmc_spi_set_ios()
1087 if (spi_write(host->spi, &nullbyte, 1) < 0) in mmc_spi_set_ios()
1088 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1089 "put spi signals to low failed\n"); in mmc_spi_set_ios()
1092 * Now clock should be low due to spi mode 0; in mmc_spi_set_ios()
1095 * power supply is off, so now MMC is off too! in mmc_spi_set_ios()
1102 host->spi->mode |= (SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1103 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1105 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1106 "switch back to SPI mode 3 failed\n"); in mmc_spi_set_ios()
1110 host->power_mode = ios->power_mode; in mmc_spi_set_ios()
1113 if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) { in mmc_spi_set_ios()
1116 host->spi->max_speed_hz = ios->clock; in mmc_spi_set_ios()
1117 status = spi_setup(host->spi); in mmc_spi_set_ios()
1118 dev_dbg(&host->spi->dev, " clock to %d Hz, %d\n", in mmc_spi_set_ios()
1119 host->spi->max_speed_hz, status); in mmc_spi_set_ios()
1134 * SPI driver implementation
1138 mmc_spi_detect_irq(int irq, void *mmc) in mmc_spi_detect_irq() argument
1140 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_detect_irq()
1141 u16 delay_msec = max(host->pdata->detect_delay, (u16)100); in mmc_spi_detect_irq()
1143 mmc_detect_change(mmc, msecs_to_jiffies(delay_msec)); in mmc_spi_detect_irq()
1147 static int mmc_spi_probe(struct spi_device *spi) in mmc_spi_probe() argument
1150 struct mmc_host *mmc; in mmc_spi_probe() local
1156 * per-transfer overheads (by making fewer transfers). in mmc_spi_probe()
1158 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) in mmc_spi_probe()
1159 return -EINVAL; in mmc_spi_probe()
1161 /* MMC and SD specs only seem to care that sampling is on the in mmc_spi_probe()
1162 * rising edge ... meaning SPI modes 0 or 3. So either SPI mode in mmc_spi_probe()
1167 if (spi->mode != SPI_MODE_3) in mmc_spi_probe()
1168 spi->mode = SPI_MODE_0; in mmc_spi_probe()
1169 spi->bits_per_word = 8; in mmc_spi_probe()
1171 status = spi_setup(spi); in mmc_spi_probe()
1173 dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n", in mmc_spi_probe()
1174 spi->mode, spi->max_speed_hz / 1000, in mmc_spi_probe()
1182 * NOTE if many systems use more than one MMC-over-SPI connector in mmc_spi_probe()
1185 status = -ENOMEM; in mmc_spi_probe()
1191 mmc = mmc_alloc_host(sizeof(*host), &spi->dev); in mmc_spi_probe()
1192 if (!mmc) in mmc_spi_probe()
1195 mmc->ops = &mmc_spi_ops; in mmc_spi_probe()
1196 mmc->max_blk_size = MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1197 mmc->max_segs = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1198 mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1199 mmc->max_blk_count = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1201 mmc->caps = MMC_CAP_SPI; in mmc_spi_probe()
1203 /* SPI doesn't need the lowspeed device identification thing for in mmc_spi_probe()
1204 * MMC or SD cards, since it never comes up in open drain mode. in mmc_spi_probe()
1205 * That's good; some SPI masters can't handle very low speeds! in mmc_spi_probe()
1211 if (spi->controller->min_speed_hz > 400000) in mmc_spi_probe()
1212 dev_warn(&spi->dev,"Controller unable to reduce bus clock to 400 KHz\n"); in mmc_spi_probe()
1214 mmc->f_min = max(spi->controller->min_speed_hz, 400000); in mmc_spi_probe()
1215 mmc->f_max = spi->max_speed_hz; in mmc_spi_probe()
1217 host = mmc_priv(mmc); in mmc_spi_probe()
1218 host->mmc = mmc; in mmc_spi_probe()
1219 host->spi = spi; in mmc_spi_probe()
1221 host->ones = ones; in mmc_spi_probe()
1223 dev_set_drvdata(&spi->dev, mmc); in mmc_spi_probe()
1228 host->pdata = mmc_spi_get_pdata(spi); in mmc_spi_probe()
1229 if (host->pdata) in mmc_spi_probe()
1230 mmc->ocr_avail = host->pdata->ocr_mask; in mmc_spi_probe()
1231 if (!mmc->ocr_avail) { in mmc_spi_probe()
1232 dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n"); in mmc_spi_probe()
1233 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34; in mmc_spi_probe()
1235 if (host->pdata && host->pdata->setpower) { in mmc_spi_probe()
1236 host->powerup_msecs = host->pdata->powerup_msecs; in mmc_spi_probe()
1237 if (!host->powerup_msecs || host->powerup_msecs > 250) in mmc_spi_probe()
1238 host->powerup_msecs = 250; in mmc_spi_probe()
1242 host->data = kmalloc(sizeof(*host->data), GFP_KERNEL); in mmc_spi_probe()
1243 if (!host->data) in mmc_spi_probe()
1247 spi_message_init(&host->readback); in mmc_spi_probe()
1249 spi_message_add_tail(&host->status, &host->readback); in mmc_spi_probe()
1250 host->status.tx_buf = host->ones; in mmc_spi_probe()
1251 host->status.rx_buf = &host->data->status; in mmc_spi_probe()
1252 host->status.cs_change = 1; in mmc_spi_probe()
1255 if (host->pdata && host->pdata->init) { in mmc_spi_probe()
1256 status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc); in mmc_spi_probe()
1262 if (host->pdata) { in mmc_spi_probe()
1263 mmc->caps |= host->pdata->caps; in mmc_spi_probe()
1264 mmc->caps2 |= host->pdata->caps2; in mmc_spi_probe()
1267 status = mmc_add_host(mmc); in mmc_spi_probe()
1275 status = mmc_gpiod_request_cd(mmc, NULL, 0, false, 1000); in mmc_spi_probe()
1276 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1284 mmc->caps &= ~MMC_CAP_NEEDS_POLL; in mmc_spi_probe()
1285 mmc_gpiod_request_cd_irq(mmc); in mmc_spi_probe()
1287 mmc_detect_change(mmc, 0); in mmc_spi_probe()
1290 status = mmc_gpiod_request_ro(mmc, NULL, 1, 0); in mmc_spi_probe()
1291 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1296 dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n", in mmc_spi_probe()
1297 dev_name(&mmc->class_dev), in mmc_spi_probe()
1299 (host->pdata && host->pdata->setpower) in mmc_spi_probe()
1301 (mmc->caps & MMC_CAP_NEEDS_POLL) in mmc_spi_probe()
1306 mmc_remove_host(mmc); in mmc_spi_probe()
1308 kfree(host->data); in mmc_spi_probe()
1310 mmc_spi_put_pdata(spi); in mmc_spi_probe()
1311 mmc_free_host(mmc); in mmc_spi_probe()
1318 static void mmc_spi_remove(struct spi_device *spi) in mmc_spi_remove() argument
1320 struct mmc_host *mmc = dev_get_drvdata(&spi->dev); in mmc_spi_remove() local
1321 struct mmc_spi_host *host = mmc_priv(mmc); in mmc_spi_remove()
1324 if (host->pdata && host->pdata->exit) in mmc_spi_remove()
1325 host->pdata->exit(&spi->dev, mmc); in mmc_spi_remove()
1327 mmc_remove_host(mmc); in mmc_spi_remove()
1329 kfree(host->data); in mmc_spi_remove()
1330 kfree(host->ones); in mmc_spi_remove()
1332 spi->max_speed_hz = mmc->f_max; in mmc_spi_remove()
1333 mmc_spi_put_pdata(spi); in mmc_spi_remove()
1334 mmc_free_host(mmc); in mmc_spi_remove()
1338 { "mmc-spi-slot"},
1341 MODULE_DEVICE_TABLE(spi, mmc_spi_dev_ids);
1344 { .compatible = "mmc-spi-slot", },
1361 MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
1362 MODULE_DESCRIPTION("SPI SD/MMC host driver");
1364 MODULE_ALIAS("spi:mmc_spi");