Lines Matching +full:spi +full:- +full:crc
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-direction.h>
20 #include <linux/crc-itu-t.h>
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
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!
71 /* Read and write blocks start with these tokens and end with crc;
98 /* "scratch" is per-{command,block} data exchanged with the card */
107 struct spi_device *spi; member
115 struct spi_transfer token, t, crc, early_status; member
136 * MMC-over-SPI protocol glue, used by the MMC stack interface
142 return spi_setup(host->spi); in mmc_cs_off()
147 if (len > sizeof(*host->data)) { in mmc_spi_readbytes()
149 return -EIO; in mmc_spi_readbytes()
152 host->status.len = len; in mmc_spi_readbytes()
154 return spi_sync_locked(host->spi, &host->readback); in mmc_spi_readbytes()
160 u8 *cp = host->data->status; in mmc_spi_skip()
186 return -ETIMEDOUT; in mmc_spi_skip()
192 return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); in mmc_spi_wait_unbusy()
202 * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
206 * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
221 /* return zero, else negative errno after setting cmd->error */
226 u8 *cp = host->data->status; in mmc_spi_response_get()
227 u8 *end = cp + host->t.len; in mmc_spi_response_get()
236 cmd->opcode, maptype(cmd)); in mmc_spi_response_get()
250 cp = host->data->status; in mmc_spi_response_get()
253 /* Card sends N(CR) (== 1..8) bytes of all-ones then one in mmc_spi_response_get()
256 * REVISIT block read paths use nasty byte-at-a-time I/O in mmc_spi_response_get()
271 value = -ETIMEDOUT; in mmc_spi_response_get()
278 /* Houston, we have an ugly card with a bit-shifted response */ in mmc_spi_response_get()
285 cp = host->data->status; in mmc_spi_response_get()
293 cmd->resp[0] = rotator >> 8; in mmc_spi_response_get()
296 cmd->resp[0] = *cp++; in mmc_spi_response_get()
298 cmd->error = 0; in mmc_spi_response_get()
300 /* Status byte: the entire seven-bit R1 response. */ in mmc_spi_response_get()
301 if (cmd->resp[0] != 0) { in mmc_spi_response_get()
303 & cmd->resp[0]) in mmc_spi_response_get()
304 value = -EFAULT; /* Bad address */ in mmc_spi_response_get()
305 else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0]) in mmc_spi_response_get()
306 value = -ENOSYS; /* Function not implemented */ in mmc_spi_response_get()
307 else if (R1_SPI_COM_CRC & cmd->resp[0]) in mmc_spi_response_get()
308 value = -EILSEQ; /* Illegal byte sequence */ in mmc_spi_response_get()
310 & cmd->resp[0]) in mmc_spi_response_get()
311 value = -EIO; /* I/O error */ in mmc_spi_response_get()
317 /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads) in mmc_spi_response_get()
318 * and less-common stuff like various erase operations. in mmc_spi_response_get()
325 timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : in mmc_spi_response_get()
331 /* SPI R2 == R1 + second status byte; SEND_STATUS in mmc_spi_response_get()
332 * SPI R5 == R1 + data byte; IO_RW_DIRECT in mmc_spi_response_get()
340 cp = host->data->status; in mmc_spi_response_get()
346 cmd->resp[0] |= (rotator & 0xFF00); in mmc_spi_response_get()
348 cmd->resp[0] |= *cp << 8; in mmc_spi_response_get()
352 /* SPI R3, R4, or R7 == R1 + 4 bytes */ in mmc_spi_response_get()
355 cmd->resp[1] = 0; in mmc_spi_response_get()
357 cmd->resp[1] <<= 8; in mmc_spi_response_get()
363 cp = host->data->status; in mmc_spi_response_get()
368 cmd->resp[1] |= (rotator >> 8); in mmc_spi_response_get()
371 cmd->resp[1] |= *cp++; in mmc_spi_response_get()
376 /* SPI R1 == just one status byte */ in mmc_spi_response_get()
381 dev_dbg(&host->spi->dev, "bad response type %04x\n", in mmc_spi_response_get()
384 value = -EINVAL; in mmc_spi_response_get()
389 dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n", in mmc_spi_response_get()
390 tag, cmd->resp[0], cmd->resp[1]); in mmc_spi_response_get()
397 cmd->error = value; in mmc_spi_response_get()
406 * means immediate low-level resubmit, which affects the bus lock...
413 struct scratch *data = host->data; in mmc_spi_command_send()
414 u8 *cp = data->status; in mmc_spi_command_send()
423 * - an all-ones byte to ensure the card is ready in mmc_spi_command_send()
424 * - opcode byte (plus start and transmission bits) in mmc_spi_command_send()
425 * - four bytes of big-endian argument in mmc_spi_command_send()
426 * - crc7 (plus end bit) ... always computed, it's cheap in mmc_spi_command_send()
428 * We init the whole buffer to all-ones, which is what we need in mmc_spi_command_send()
431 memset(cp, 0xff, sizeof(data->status)); in mmc_spi_command_send()
433 cp[1] = 0x40 | cmd->opcode; in mmc_spi_command_send()
434 put_unaligned_be32(cmd->arg, cp + 2); in mmc_spi_command_send()
438 /* Then, read up to 13 bytes (while writing all-ones): in mmc_spi_command_send()
439 * - N(CR) (== 1..8) bytes of all-ones in mmc_spi_command_send()
440 * - status byte (for all response types) in mmc_spi_command_send()
441 * - the rest of the response, either: in mmc_spi_command_send()
448 * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish in mmc_spi_command_send()
449 * - N(RC) (== 1..N) bytes of all-ones, before next command in mmc_spi_command_send()
450 * - N(WR) (== 1..N) bytes of all-ones, before data write in mmc_spi_command_send()
460 * - R1B responses need at least N(EC) bytes of all-zeroes. in mmc_spi_command_send()
465 * - Data block reads are more troublesome, since a variable in mmc_spi_command_send()
467 * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID in mmc_spi_command_send()
468 * + N(AC) (== 1..many) bytes of all-ones in mmc_spi_command_send()
473 if (cs_on && (mrq->data->flags & MMC_DATA_READ)) { in mmc_spi_command_send()
478 if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */ in mmc_spi_command_send()
480 else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */ in mmc_spi_command_send()
482 else if (cmd->flags & MMC_RSP_BUSY) /* R1B */ in mmc_spi_command_send()
483 cp = data->status + sizeof(data->status); in mmc_spi_command_send()
487 dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n", in mmc_spi_command_send()
488 cmd->opcode, maptype(cmd)); in mmc_spi_command_send()
491 spi_message_init(&host->m); in mmc_spi_command_send()
493 t = &host->t; in mmc_spi_command_send()
495 t->tx_buf = t->rx_buf = data->status; in mmc_spi_command_send()
496 t->len = cp - data->status; in mmc_spi_command_send()
497 t->cs_change = 1; in mmc_spi_command_send()
498 spi_message_add_tail(t, &host->m); in mmc_spi_command_send()
500 status = spi_sync_locked(host->spi, &host->m); in mmc_spi_command_send()
502 dev_dbg(&host->spi->dev, " ... write returned %d\n", status); in mmc_spi_command_send()
503 cmd->error = status; in mmc_spi_command_send()
507 /* after no-data commands and STOP_TRANSMISSION, chipselect off */ in mmc_spi_command_send()
515 * We always provide TX data for data and CRC. The MMC/SD protocol
526 struct scratch *scratch = host->data; in mmc_spi_setup_data_message()
528 spi_message_init(&host->m); in mmc_spi_setup_data_message()
534 t = &host->token; in mmc_spi_setup_data_message()
536 t->len = 1; in mmc_spi_setup_data_message()
538 scratch->data_token = SPI_TOKEN_MULTI_WRITE; in mmc_spi_setup_data_message()
540 scratch->data_token = SPI_TOKEN_SINGLE; in mmc_spi_setup_data_message()
541 t->tx_buf = &scratch->data_token; in mmc_spi_setup_data_message()
542 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
545 /* Body of transfer is buffer, then CRC ... in mmc_spi_setup_data_message()
546 * either TX-only, or RX with TX-ones. in mmc_spi_setup_data_message()
548 t = &host->t; in mmc_spi_setup_data_message()
550 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
552 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
554 t = &host->crc; in mmc_spi_setup_data_message()
556 t->len = 2; in mmc_spi_setup_data_message()
558 /* the actual CRC may get written later */ in mmc_spi_setup_data_message()
559 t->tx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
561 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
562 t->rx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
564 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
567 * A single block read is followed by N(EC) [0+] all-ones bytes in mmc_spi_setup_data_message()
570 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before in mmc_spi_setup_data_message()
574 * For a write, the one-byte data response follows immediately, then in mmc_spi_setup_data_message()
575 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes. in mmc_spi_setup_data_message()
578 * minimize I/O ops by using a single read to collect end-of-busy. in mmc_spi_setup_data_message()
581 t = &host->early_status; in mmc_spi_setup_data_message()
583 t->len = (direction == DMA_TO_DEVICE) ? sizeof(scratch->status) : 1; in mmc_spi_setup_data_message()
584 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
585 t->rx_buf = scratch->status; in mmc_spi_setup_data_message()
586 t->cs_change = 1; in mmc_spi_setup_data_message()
587 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
593 * - caller handled preceding N(WR) [1+] all-ones bytes
594 * - data block
598 * - an all-ones byte ... card writes a data-response byte
599 * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
607 struct spi_device *spi = host->spi; in mmc_spi_writeblock() local
609 struct scratch *scratch = host->data; in mmc_spi_writeblock()
612 if (host->mmc->use_spi_crc) in mmc_spi_writeblock()
613 scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len)); in mmc_spi_writeblock()
615 status = spi_sync_locked(spi, &host->m); in mmc_spi_writeblock()
617 dev_dbg(&spi->dev, "write error (%d)\n", status); in mmc_spi_writeblock()
622 * Get the transmission data-response reply. It must follow in mmc_spi_writeblock()
628 * In practice, there are (even modern SDHC-)cards which are late in mmc_spi_writeblock()
631 * bit-by-bit. Arggh!!! in mmc_spi_writeblock()
633 pattern = get_unaligned_be32(scratch->status); in mmc_spi_writeblock()
638 /* left-adjust to leading 0 bit */ in mmc_spi_writeblock()
641 /* right-adjust for pattern matching. Code is in bit 4..0 now. */ in mmc_spi_writeblock()
650 status = -EILSEQ; in mmc_spi_writeblock()
656 status = -EIO; in mmc_spi_writeblock()
659 status = -EPROTO; in mmc_spi_writeblock()
663 dev_dbg(&spi->dev, "write error %02x (%d)\n", in mmc_spi_writeblock()
664 scratch->status[0], status); in mmc_spi_writeblock()
668 t->tx_buf += t->len; in mmc_spi_writeblock()
673 for (i = 4; i < sizeof(scratch->status); i++) { in mmc_spi_writeblock()
674 /* card is non-busy if the most recent bit is 1 */ in mmc_spi_writeblock()
675 if (scratch->status[i] & 0x01) in mmc_spi_writeblock()
683 * - skip leading all-ones bytes ... either
686 * - data block
687 * + token ... if error token, no data or crc
691 * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
701 struct spi_device *spi = host->spi; in mmc_spi_readblock() local
703 struct scratch *scratch = host->data; in mmc_spi_readblock()
707 /* At least one SD card sends an all-zeroes byte when N(CX) in mmc_spi_readblock()
708 * applies, before the all-ones bytes ... just cope with that. in mmc_spi_readblock()
713 status = scratch->status[0]; in mmc_spi_readblock()
718 dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); in mmc_spi_readblock()
722 /* The token may be bit-shifted... in mmc_spi_readblock()
723 * the first 0-bit precedes the data stream. in mmc_spi_readblock()
728 bitshift--; in mmc_spi_readblock()
732 status = spi_sync_locked(spi, &host->m); in mmc_spi_readblock()
734 dev_dbg(&spi->dev, "read error %d\n", status); in mmc_spi_readblock()
739 /* Walk through the data and the crc and do in mmc_spi_readblock()
740 * all the magic to get byte-aligned data. in mmc_spi_readblock()
742 u8 *cp = t->rx_buf; in mmc_spi_readblock()
744 unsigned int bitright = 8 - bitshift; in mmc_spi_readblock()
746 for (len = t->len; len; len--) { in mmc_spi_readblock()
751 cp = (u8 *) &scratch->crc_val; in mmc_spi_readblock()
759 if (host->mmc->use_spi_crc) { in mmc_spi_readblock()
760 u16 crc = crc_itu_t(0, t->rx_buf, t->len); in mmc_spi_readblock() local
762 be16_to_cpus(&scratch->crc_val); in mmc_spi_readblock()
763 if (scratch->crc_val != crc) { in mmc_spi_readblock()
764 dev_dbg(&spi->dev, in mmc_spi_readblock()
765 "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n", in mmc_spi_readblock()
766 scratch->crc_val, crc, t->len); in mmc_spi_readblock()
767 return -EILSEQ; in mmc_spi_readblock()
771 t->rx_buf += t->len; in mmc_spi_readblock()
779 * other SPI protocol stacks.
785 struct spi_device *spi = host->spi; in mmc_spi_data_do() local
790 int multiple = (data->blocks > 1); in mmc_spi_data_do()
796 t = &host->t; in mmc_spi_data_do()
798 if (t->speed_hz) in mmc_spi_data_do()
799 clock_rate = t->speed_hz; in mmc_spi_data_do()
801 clock_rate = spi->max_speed_hz; in mmc_spi_data_do()
803 timeout = data->timeout_ns / 1000 + in mmc_spi_data_do()
804 data->timeout_clks * 1000000 / clock_rate; in mmc_spi_data_do()
808 * each 512-byte block in mmc_spi_data_do()
810 for_each_sg(data->sg, sg, data->sg_len, n_sg) { in mmc_spi_data_do()
813 unsigned length = sg->length; in mmc_spi_data_do()
818 t->tx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
820 t->rx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
824 t->len = min(length, blk_size); in mmc_spi_data_do()
826 dev_dbg(&host->spi->dev, in mmc_spi_data_do()
829 t->len); in mmc_spi_data_do()
838 data->bytes_xfered += t->len; in mmc_spi_data_do()
839 length -= t->len; in mmc_spi_data_do()
851 data->error = status; in mmc_spi_data_do()
852 dev_dbg(&spi->dev, "%s status %d\n", in mmc_spi_data_do()
859 /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that in mmc_spi_data_do()
866 struct scratch *scratch = host->data; in mmc_spi_data_do()
868 const unsigned statlen = sizeof(scratch->status); in mmc_spi_data_do()
870 dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n"); in mmc_spi_data_do()
872 /* Tweak the per-block message we set up earlier by morphing in mmc_spi_data_do()
874 * all-ones bytes ... skip N(BR) (0..1), scan the rest for in mmc_spi_data_do()
877 INIT_LIST_HEAD(&host->m.transfers); in mmc_spi_data_do()
878 list_add(&host->early_status.transfer_list, in mmc_spi_data_do()
879 &host->m.transfers); in mmc_spi_data_do()
881 memset(scratch->status, 0xff, statlen); in mmc_spi_data_do()
882 scratch->status[0] = SPI_TOKEN_STOP_TRAN; in mmc_spi_data_do()
884 host->early_status.tx_buf = host->early_status.rx_buf; in mmc_spi_data_do()
885 host->early_status.len = statlen; in mmc_spi_data_do()
887 tmp = spi_sync_locked(spi, &host->m); in mmc_spi_data_do()
889 if (!data->error) in mmc_spi_data_do()
890 data->error = tmp; in mmc_spi_data_do()
895 * avoiding wasteful byte-at-a-time scanning... but more in mmc_spi_data_do()
899 if (scratch->status[tmp] != 0) in mmc_spi_data_do()
903 if (tmp < 0 && !data->error) in mmc_spi_data_do()
904 data->error = tmp; in mmc_spi_data_do()
911 * MMC driver implementation -- the interface to the MMC stack
917 int status = -EINVAL; in mmc_spi_request()
922 /* MMC core and layered drivers *MUST* issue SPI-aware commands */ in mmc_spi_request()
927 cmd = mrq->cmd; in mmc_spi_request()
929 dev_dbg(&host->spi->dev, "bogus command\n"); in mmc_spi_request()
930 cmd->error = -EINVAL; in mmc_spi_request()
934 cmd = mrq->stop; in mmc_spi_request()
936 dev_dbg(&host->spi->dev, "bogus STOP command\n"); in mmc_spi_request()
937 cmd->error = -EINVAL; in mmc_spi_request()
943 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
950 spi_bus_lock(host->spi->master); in mmc_spi_request()
954 status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL); in mmc_spi_request()
955 if (status == 0 && mrq->data) { in mmc_spi_request()
956 mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz); in mmc_spi_request()
959 * The SPI bus is not always reliable for large data transfers. in mmc_spi_request()
960 * If an occasional crc error is reported by the SD device with in mmc_spi_request()
961 * data read/write over SPI, it may be recovered by repeating in mmc_spi_request()
965 if (mrq->data->error == -EILSEQ && crc_retry) { in mmc_spi_request()
970 crc_retry--; in mmc_spi_request()
971 mrq->data->error = 0; in mmc_spi_request()
975 if (mrq->stop) in mmc_spi_request()
976 status = mmc_spi_command_send(host, mrq, mrq->stop, 0); in mmc_spi_request()
982 spi_bus_unlock(host->spi->master); in mmc_spi_request()
984 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
992 * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
998 * wait till not-busy, skip debris from any old commands. in mmc_spi_initsequence()
1004 * Do a burst with chipselect active-high. We need to do this to in mmc_spi_initsequence()
1013 * SPI protocol. Another is that when chipselect is released while in mmc_spi_initsequence()
1022 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1023 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1025 dev_warn(&host->spi->dev, in mmc_spi_initsequence()
1026 "can't change chip-select polarity\n"); in mmc_spi_initsequence()
1027 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1031 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1032 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1034 dev_err(&host->spi->dev, in mmc_spi_initsequence()
1035 "can't restore chip-select polarity\n"); in mmc_spi_initsequence()
1054 if (host->power_mode != ios->power_mode) { in mmc_spi_set_ios()
1057 canpower = host->pdata && host->pdata->setpower; in mmc_spi_set_ios()
1059 dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n", in mmc_spi_set_ios()
1060 mmc_powerstring(ios->power_mode), in mmc_spi_set_ios()
1061 ios->vdd, in mmc_spi_set_ios()
1068 switch (ios->power_mode) { in mmc_spi_set_ios()
1071 host->pdata->setpower(&host->spi->dev, in mmc_spi_set_ios()
1072 ios->vdd); in mmc_spi_set_ios()
1073 if (ios->power_mode == MMC_POWER_UP) in mmc_spi_set_ios()
1074 msleep(host->powerup_msecs); in mmc_spi_set_ios()
1079 if (ios->power_mode == MMC_POWER_ON) in mmc_spi_set_ios()
1083 * delivery from data lines! On a shared SPI bus, this in mmc_spi_set_ios()
1087 * - Clock low means CPOL 0, e.g. mode 0 in mmc_spi_set_ios()
1088 * - MOSI low comes from writing zero in mmc_spi_set_ios()
1089 * - Chipselect is usually active low... in mmc_spi_set_ios()
1091 if (canpower && ios->power_mode == MMC_POWER_OFF) { in mmc_spi_set_ios()
1095 host->spi->mode &= ~(SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1096 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1098 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1099 "switch to SPI mode 0 failed\n"); in mmc_spi_set_ios()
1101 if (spi_write(host->spi, &nullbyte, 1) < 0) in mmc_spi_set_ios()
1102 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1103 "put spi signals to low failed\n"); in mmc_spi_set_ios()
1106 * Now clock should be low due to spi mode 0; in mmc_spi_set_ios()
1116 host->spi->mode |= (SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1117 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1119 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1120 "switch back to SPI mode 3 failed\n"); in mmc_spi_set_ios()
1124 host->power_mode = ios->power_mode; in mmc_spi_set_ios()
1127 if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) { in mmc_spi_set_ios()
1130 host->spi->max_speed_hz = ios->clock; in mmc_spi_set_ios()
1131 status = spi_setup(host->spi); in mmc_spi_set_ios()
1132 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1134 host->spi->max_speed_hz, status); in mmc_spi_set_ios()
1149 * SPI driver implementation
1156 u16 delay_msec = max(host->pdata->detect_delay, (u16)100); in mmc_spi_detect_irq()
1162 static int mmc_spi_probe(struct spi_device *spi) in mmc_spi_probe() argument
1171 * per-transfer overheads (by making fewer transfers). in mmc_spi_probe()
1173 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) in mmc_spi_probe()
1174 return -EINVAL; in mmc_spi_probe()
1177 * rising edge ... meaning SPI modes 0 or 3. So either SPI mode in mmc_spi_probe()
1182 if (spi->mode != SPI_MODE_3) in mmc_spi_probe()
1183 spi->mode = SPI_MODE_0; in mmc_spi_probe()
1184 spi->bits_per_word = 8; in mmc_spi_probe()
1186 status = spi_setup(spi); in mmc_spi_probe()
1188 dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n", in mmc_spi_probe()
1189 spi->mode, spi->max_speed_hz / 1000, in mmc_spi_probe()
1197 * NOTE if many systems use more than one MMC-over-SPI connector in mmc_spi_probe()
1200 status = -ENOMEM; in mmc_spi_probe()
1206 mmc = mmc_alloc_host(sizeof(*host), &spi->dev); in mmc_spi_probe()
1210 mmc->ops = &mmc_spi_ops; in mmc_spi_probe()
1211 mmc->max_blk_size = MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1212 mmc->max_segs = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1213 mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1214 mmc->max_blk_count = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1216 mmc->caps = MMC_CAP_SPI; in mmc_spi_probe()
1218 /* SPI doesn't need the lowspeed device identification thing for in mmc_spi_probe()
1220 * That's good; some SPI masters can't handle very low speeds! in mmc_spi_probe()
1226 mmc->f_min = 400000; in mmc_spi_probe()
1227 mmc->f_max = spi->max_speed_hz; in mmc_spi_probe()
1230 host->mmc = mmc; in mmc_spi_probe()
1231 host->spi = spi; in mmc_spi_probe()
1233 host->ones = ones; in mmc_spi_probe()
1238 host->pdata = mmc_spi_get_pdata(spi); in mmc_spi_probe()
1239 if (host->pdata) in mmc_spi_probe()
1240 mmc->ocr_avail = host->pdata->ocr_mask; in mmc_spi_probe()
1241 if (!mmc->ocr_avail) { in mmc_spi_probe()
1242 dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n"); in mmc_spi_probe()
1243 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34; in mmc_spi_probe()
1245 if (host->pdata && host->pdata->setpower) { in mmc_spi_probe()
1246 host->powerup_msecs = host->pdata->powerup_msecs; in mmc_spi_probe()
1247 if (!host->powerup_msecs || host->powerup_msecs > 250) in mmc_spi_probe()
1248 host->powerup_msecs = 250; in mmc_spi_probe()
1251 dev_set_drvdata(&spi->dev, mmc); in mmc_spi_probe()
1254 host->data = kmalloc(sizeof(*host->data), GFP_KERNEL); in mmc_spi_probe()
1255 if (!host->data) in mmc_spi_probe()
1259 spi_message_init(&host->readback); in mmc_spi_probe()
1261 spi_message_add_tail(&host->status, &host->readback); in mmc_spi_probe()
1262 host->status.tx_buf = host->ones; in mmc_spi_probe()
1263 host->status.rx_buf = &host->data->status; in mmc_spi_probe()
1264 host->status.cs_change = 1; in mmc_spi_probe()
1267 if (host->pdata && host->pdata->init) { in mmc_spi_probe()
1268 status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc); in mmc_spi_probe()
1274 if (host->pdata) { in mmc_spi_probe()
1275 mmc->caps |= host->pdata->caps; in mmc_spi_probe()
1276 mmc->caps2 |= host->pdata->caps2; in mmc_spi_probe()
1288 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1296 mmc->caps &= ~MMC_CAP_NEEDS_POLL; in mmc_spi_probe()
1303 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1308 dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n", in mmc_spi_probe()
1309 dev_name(&mmc->class_dev), in mmc_spi_probe()
1311 (host->pdata && host->pdata->setpower) in mmc_spi_probe()
1313 (mmc->caps & MMC_CAP_NEEDS_POLL) in mmc_spi_probe()
1320 kfree(host->data); in mmc_spi_probe()
1323 mmc_spi_put_pdata(spi); in mmc_spi_probe()
1330 static int mmc_spi_remove(struct spi_device *spi) in mmc_spi_remove() argument
1332 struct mmc_host *mmc = dev_get_drvdata(&spi->dev); in mmc_spi_remove()
1336 if (host->pdata && host->pdata->exit) in mmc_spi_remove()
1337 host->pdata->exit(&spi->dev, mmc); in mmc_spi_remove()
1341 kfree(host->data); in mmc_spi_remove()
1342 kfree(host->ones); in mmc_spi_remove()
1344 spi->max_speed_hz = mmc->f_max; in mmc_spi_remove()
1346 mmc_spi_put_pdata(spi); in mmc_spi_remove()
1351 { .compatible = "mmc-spi-slot", },
1367 MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
1368 MODULE_DESCRIPTION("SPI SD/MMC host driver");
1370 MODULE_ALIAS("spi:mmc_spi");