Lines Matching +full:ac +full:- +full:detect +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
25 #include <linux/mmc/slot-gpio.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
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 */
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()
206 return -ETIMEDOUT; in mmc_spi_skip()
212 return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0); in mmc_spi_wait_unbusy()
222 * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
226 * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
241 /* return zero, else negative errno after setting cmd->error */
246 u8 *cp = host->data->status; in mmc_spi_response_get()
247 u8 *end = cp + host->t.len; in mmc_spi_response_get()
256 cmd->opcode, maptype(cmd)); in mmc_spi_response_get()
270 cp = host->data->status; in mmc_spi_response_get()
273 /* Card sends N(CR) (== 1..8) bytes of all-ones then one in mmc_spi_response_get()
276 * REVISIT block read paths use nasty byte-at-a-time I/O in mmc_spi_response_get()
291 value = -ETIMEDOUT; in mmc_spi_response_get()
298 /* Houston, we have an ugly card with a bit-shifted response */ in mmc_spi_response_get()
305 cp = host->data->status; in mmc_spi_response_get()
313 cmd->resp[0] = rotator >> 8; in mmc_spi_response_get()
316 cmd->resp[0] = *cp++; in mmc_spi_response_get()
318 cmd->error = 0; in mmc_spi_response_get()
320 /* Status byte: the entire seven-bit R1 response. */ in mmc_spi_response_get()
321 if (cmd->resp[0] != 0) { in mmc_spi_response_get()
323 & cmd->resp[0]) in mmc_spi_response_get()
324 value = -EFAULT; /* Bad address */ in mmc_spi_response_get()
325 else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0]) in mmc_spi_response_get()
326 value = -ENOSYS; /* Function not implemented */ in mmc_spi_response_get()
327 else if (R1_SPI_COM_CRC & cmd->resp[0]) in mmc_spi_response_get()
328 value = -EILSEQ; /* Illegal byte sequence */ in mmc_spi_response_get()
330 & cmd->resp[0]) in mmc_spi_response_get()
331 value = -EIO; /* I/O error */ in mmc_spi_response_get()
338 * and less-common stuff like various erase operations. in mmc_spi_response_get()
345 timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : in mmc_spi_response_get()
360 cp = host->data->status; in mmc_spi_response_get()
366 cmd->resp[0] |= (rotator & 0xFF00); in mmc_spi_response_get()
368 cmd->resp[0] |= *cp << 8; in mmc_spi_response_get()
375 cmd->resp[1] = 0; in mmc_spi_response_get()
377 cmd->resp[1] <<= 8; in mmc_spi_response_get()
383 cp = host->data->status; in mmc_spi_response_get()
388 cmd->resp[1] |= (rotator >> 8); in mmc_spi_response_get()
391 cmd->resp[1] |= *cp++; in mmc_spi_response_get()
401 dev_dbg(&host->spi->dev, "bad response type %04x\n", in mmc_spi_response_get()
404 value = -EINVAL; in mmc_spi_response_get()
409 dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n", in mmc_spi_response_get()
410 tag, cmd->resp[0], cmd->resp[1]); in mmc_spi_response_get()
417 cmd->error = value; in mmc_spi_response_get()
426 * means immediate low-level resubmit, which affects the bus lock...
433 struct scratch *data = host->data; in mmc_spi_command_send()
434 u8 *cp = data->status; in mmc_spi_command_send()
443 * - an all-ones byte to ensure the card is ready in mmc_spi_command_send()
444 * - opcode byte (plus start and transmission bits) in mmc_spi_command_send()
445 * - four bytes of big-endian argument in mmc_spi_command_send()
446 * - crc7 (plus end bit) ... always computed, it's cheap in mmc_spi_command_send()
448 * We init the whole buffer to all-ones, which is what we need in mmc_spi_command_send()
451 memset(cp, 0xff, sizeof(data->status)); in mmc_spi_command_send()
453 cp[1] = 0x40 | cmd->opcode; in mmc_spi_command_send()
454 put_unaligned_be32(cmd->arg, cp + 2); in mmc_spi_command_send()
458 /* Then, read up to 13 bytes (while writing all-ones): in mmc_spi_command_send()
459 * - N(CR) (== 1..8) bytes of all-ones in mmc_spi_command_send()
460 * - status byte (for all response types) in mmc_spi_command_send()
461 * - the rest of the response, either: in mmc_spi_command_send()
468 * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish in mmc_spi_command_send()
469 * - N(RC) (== 1..N) bytes of all-ones, before next command in mmc_spi_command_send()
470 * - N(WR) (== 1..N) bytes of all-ones, before data write in mmc_spi_command_send()
480 * - R1B responses need at least N(EC) bytes of all-zeroes. in mmc_spi_command_send()
485 * - Data block reads are more troublesome, since a variable in mmc_spi_command_send()
487 * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID in mmc_spi_command_send()
488 * + N(AC) (== 1..many) bytes of all-ones in mmc_spi_command_send()
493 if (cs_on && (mrq->data->flags & MMC_DATA_READ)) { in mmc_spi_command_send()
498 if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */ in mmc_spi_command_send()
500 else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */ in mmc_spi_command_send()
502 else if (cmd->flags & MMC_RSP_BUSY) /* R1B */ in mmc_spi_command_send()
503 cp = data->status + sizeof(data->status); in mmc_spi_command_send()
507 dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n", in mmc_spi_command_send()
508 cmd->opcode, maptype(cmd)); in mmc_spi_command_send()
511 spi_message_init(&host->m); in mmc_spi_command_send()
513 t = &host->t; in mmc_spi_command_send()
515 t->tx_buf = t->rx_buf = data->status; in mmc_spi_command_send()
516 t->tx_dma = t->rx_dma = host->data_dma; in mmc_spi_command_send()
517 t->len = cp - data->status; in mmc_spi_command_send()
518 t->cs_change = 1; in mmc_spi_command_send()
519 spi_message_add_tail(t, &host->m); in mmc_spi_command_send()
521 if (host->dma_dev) { in mmc_spi_command_send()
522 host->m.is_dma_mapped = 1; in mmc_spi_command_send()
523 dma_sync_single_for_device(host->dma_dev, in mmc_spi_command_send()
524 host->data_dma, sizeof(*host->data), in mmc_spi_command_send()
527 status = spi_sync_locked(host->spi, &host->m); in mmc_spi_command_send()
529 if (host->dma_dev) in mmc_spi_command_send()
530 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_command_send()
531 host->data_dma, sizeof(*host->data), in mmc_spi_command_send()
534 dev_dbg(&host->spi->dev, " ... write returned %d\n", status); in mmc_spi_command_send()
535 cmd->error = status; in mmc_spi_command_send()
539 /* after no-data commands and STOP_TRANSMISSION, chipselect off */ in mmc_spi_command_send()
561 struct scratch *scratch = host->data; in mmc_spi_setup_data_message()
562 dma_addr_t dma = host->data_dma; in mmc_spi_setup_data_message()
564 spi_message_init(&host->m); in mmc_spi_setup_data_message()
566 host->m.is_dma_mapped = 1; in mmc_spi_setup_data_message()
572 t = &host->token; in mmc_spi_setup_data_message()
574 t->len = 1; in mmc_spi_setup_data_message()
576 scratch->data_token = SPI_TOKEN_MULTI_WRITE; in mmc_spi_setup_data_message()
578 scratch->data_token = SPI_TOKEN_SINGLE; in mmc_spi_setup_data_message()
579 t->tx_buf = &scratch->data_token; in mmc_spi_setup_data_message()
581 t->tx_dma = dma + offsetof(struct scratch, data_token); in mmc_spi_setup_data_message()
582 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
586 * either TX-only, or RX with TX-ones. in mmc_spi_setup_data_message()
588 t = &host->t; in mmc_spi_setup_data_message()
590 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
591 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
593 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
595 t = &host->crc; in mmc_spi_setup_data_message()
597 t->len = 2; in mmc_spi_setup_data_message()
600 t->tx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
602 t->tx_dma = dma + offsetof(struct scratch, crc_val); in mmc_spi_setup_data_message()
604 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
605 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
606 t->rx_buf = &scratch->crc_val; in mmc_spi_setup_data_message()
608 t->rx_dma = dma + offsetof(struct scratch, crc_val); in mmc_spi_setup_data_message()
610 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
613 * A single block read is followed by N(EC) [0+] all-ones bytes in mmc_spi_setup_data_message()
616 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before in mmc_spi_setup_data_message()
620 * For a write, the one-byte data response follows immediately, then in mmc_spi_setup_data_message()
621 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes. in mmc_spi_setup_data_message()
624 * minimize I/O ops by using a single read to collect end-of-busy. in mmc_spi_setup_data_message()
627 t = &host->early_status; in mmc_spi_setup_data_message()
629 t->len = (direction == DMA_TO_DEVICE) ? sizeof(scratch->status) : 1; in mmc_spi_setup_data_message()
630 t->tx_buf = host->ones; in mmc_spi_setup_data_message()
631 t->tx_dma = host->ones_dma; in mmc_spi_setup_data_message()
632 t->rx_buf = scratch->status; in mmc_spi_setup_data_message()
634 t->rx_dma = dma + offsetof(struct scratch, status); in mmc_spi_setup_data_message()
635 t->cs_change = 1; in mmc_spi_setup_data_message()
636 spi_message_add_tail(t, &host->m); in mmc_spi_setup_data_message()
642 * - caller handled preceding N(WR) [1+] all-ones bytes
643 * - data block
647 * - an all-ones byte ... card writes a data-response byte
648 * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
656 struct spi_device *spi = host->spi; in mmc_spi_writeblock()
658 struct scratch *scratch = host->data; in mmc_spi_writeblock()
661 if (host->mmc->use_spi_crc) in mmc_spi_writeblock()
662 scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len)); in mmc_spi_writeblock()
663 if (host->dma_dev) in mmc_spi_writeblock()
664 dma_sync_single_for_device(host->dma_dev, in mmc_spi_writeblock()
665 host->data_dma, sizeof(*scratch), in mmc_spi_writeblock()
668 status = spi_sync_locked(spi, &host->m); in mmc_spi_writeblock()
671 dev_dbg(&spi->dev, "write error (%d)\n", status); in mmc_spi_writeblock()
675 if (host->dma_dev) in mmc_spi_writeblock()
676 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_writeblock()
677 host->data_dma, sizeof(*scratch), in mmc_spi_writeblock()
681 * Get the transmission data-response reply. It must follow in mmc_spi_writeblock()
687 * In practice, there are (even modern SDHC-)cards which are late in mmc_spi_writeblock()
690 * bit-by-bit. Arggh!!! in mmc_spi_writeblock()
692 pattern = get_unaligned_be32(scratch->status); in mmc_spi_writeblock()
697 /* left-adjust to leading 0 bit */ in mmc_spi_writeblock()
700 /* right-adjust for pattern matching. Code is in bit 4..0 now. */ in mmc_spi_writeblock()
709 status = -EILSEQ; in mmc_spi_writeblock()
715 status = -EIO; in mmc_spi_writeblock()
718 status = -EPROTO; in mmc_spi_writeblock()
722 dev_dbg(&spi->dev, "write error %02x (%d)\n", in mmc_spi_writeblock()
723 scratch->status[0], status); in mmc_spi_writeblock()
727 t->tx_buf += t->len; in mmc_spi_writeblock()
728 if (host->dma_dev) in mmc_spi_writeblock()
729 t->tx_dma += t->len; in mmc_spi_writeblock()
734 for (i = 4; i < sizeof(scratch->status); i++) { in mmc_spi_writeblock()
735 /* card is non-busy if the most recent bit is 1 */ in mmc_spi_writeblock()
736 if (scratch->status[i] & 0x01) in mmc_spi_writeblock()
744 * - skip leading all-ones bytes ... either
745 * + N(AC) [1..f(clock,CSD)] usually, else
747 * - data block
752 * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
762 struct spi_device *spi = host->spi; in mmc_spi_readblock()
764 struct scratch *scratch = host->data; in mmc_spi_readblock()
768 /* At least one SD card sends an all-zeroes byte when N(CX) in mmc_spi_readblock()
769 * applies, before the all-ones bytes ... just cope with that. in mmc_spi_readblock()
774 status = scratch->status[0]; in mmc_spi_readblock()
779 dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status); in mmc_spi_readblock()
783 /* The token may be bit-shifted... in mmc_spi_readblock()
784 * the first 0-bit precedes the data stream. in mmc_spi_readblock()
789 bitshift--; in mmc_spi_readblock()
793 if (host->dma_dev) { in mmc_spi_readblock()
794 dma_sync_single_for_device(host->dma_dev, in mmc_spi_readblock()
795 host->data_dma, sizeof(*scratch), in mmc_spi_readblock()
797 dma_sync_single_for_device(host->dma_dev, in mmc_spi_readblock()
798 t->rx_dma, t->len, in mmc_spi_readblock()
802 status = spi_sync_locked(spi, &host->m); in mmc_spi_readblock()
804 dev_dbg(&spi->dev, "read error %d\n", status); in mmc_spi_readblock()
808 if (host->dma_dev) { in mmc_spi_readblock()
809 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_readblock()
810 host->data_dma, sizeof(*scratch), in mmc_spi_readblock()
812 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_readblock()
813 t->rx_dma, t->len, in mmc_spi_readblock()
819 * all the magic to get byte-aligned data. in mmc_spi_readblock()
821 u8 *cp = t->rx_buf; in mmc_spi_readblock()
823 unsigned int bitright = 8 - bitshift; in mmc_spi_readblock()
825 for (len = t->len; len; len--) { in mmc_spi_readblock()
830 cp = (u8 *) &scratch->crc_val; in mmc_spi_readblock()
838 if (host->mmc->use_spi_crc) { in mmc_spi_readblock()
839 u16 crc = crc_itu_t(0, t->rx_buf, t->len); in mmc_spi_readblock()
841 be16_to_cpus(&scratch->crc_val); in mmc_spi_readblock()
842 if (scratch->crc_val != crc) { in mmc_spi_readblock()
843 dev_dbg(&spi->dev, in mmc_spi_readblock()
844 "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n", in mmc_spi_readblock()
845 scratch->crc_val, crc, t->len); in mmc_spi_readblock()
846 return -EILSEQ; in mmc_spi_readblock()
850 t->rx_buf += t->len; in mmc_spi_readblock()
851 if (host->dma_dev) in mmc_spi_readblock()
852 t->rx_dma += t->len; in mmc_spi_readblock()
866 struct spi_device *spi = host->spi; in mmc_spi_data_do()
867 struct device *dma_dev = host->dma_dev; in mmc_spi_data_do()
872 int multiple = (data->blocks > 1); in mmc_spi_data_do()
878 t = &host->t; in mmc_spi_data_do()
880 if (t->speed_hz) in mmc_spi_data_do()
881 clock_rate = t->speed_hz; in mmc_spi_data_do()
883 clock_rate = spi->max_speed_hz; in mmc_spi_data_do()
885 timeout = data->timeout_ns / 1000 + in mmc_spi_data_do()
886 data->timeout_clks * 1000000 / clock_rate; in mmc_spi_data_do()
890 * each 512-byte block in mmc_spi_data_do()
892 for_each_sg(data->sg, sg, data->sg_len, n_sg) { in mmc_spi_data_do()
896 unsigned length = sg->length; in mmc_spi_data_do()
904 if ((sg->offset != 0 || length != PAGE_SIZE) in mmc_spi_data_do()
911 data->error = -EFAULT; in mmc_spi_data_do()
915 t->tx_dma = dma_addr + sg->offset; in mmc_spi_data_do()
917 t->rx_dma = dma_addr + sg->offset; in mmc_spi_data_do()
923 t->tx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
925 t->rx_buf = kmap_addr + sg->offset; in mmc_spi_data_do()
929 t->len = min(length, blk_size); in mmc_spi_data_do()
931 dev_dbg(&host->spi->dev, in mmc_spi_data_do()
934 t->len); in mmc_spi_data_do()
943 data->bytes_xfered += t->len; in mmc_spi_data_do()
944 length -= t->len; in mmc_spi_data_do()
958 data->error = status; in mmc_spi_data_do()
959 dev_dbg(&spi->dev, "%s status %d\n", in mmc_spi_data_do()
966 /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that in mmc_spi_data_do()
973 struct scratch *scratch = host->data; in mmc_spi_data_do()
975 const unsigned statlen = sizeof(scratch->status); in mmc_spi_data_do()
977 dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n"); in mmc_spi_data_do()
979 /* Tweak the per-block message we set up earlier by morphing in mmc_spi_data_do()
981 * all-ones bytes ... skip N(BR) (0..1), scan the rest for in mmc_spi_data_do()
984 INIT_LIST_HEAD(&host->m.transfers); in mmc_spi_data_do()
985 list_add(&host->early_status.transfer_list, in mmc_spi_data_do()
986 &host->m.transfers); in mmc_spi_data_do()
988 memset(scratch->status, 0xff, statlen); in mmc_spi_data_do()
989 scratch->status[0] = SPI_TOKEN_STOP_TRAN; in mmc_spi_data_do()
991 host->early_status.tx_buf = host->early_status.rx_buf; in mmc_spi_data_do()
992 host->early_status.tx_dma = host->early_status.rx_dma; in mmc_spi_data_do()
993 host->early_status.len = statlen; in mmc_spi_data_do()
995 if (host->dma_dev) in mmc_spi_data_do()
996 dma_sync_single_for_device(host->dma_dev, in mmc_spi_data_do()
997 host->data_dma, sizeof(*scratch), in mmc_spi_data_do()
1000 tmp = spi_sync_locked(spi, &host->m); in mmc_spi_data_do()
1002 if (host->dma_dev) in mmc_spi_data_do()
1003 dma_sync_single_for_cpu(host->dma_dev, in mmc_spi_data_do()
1004 host->data_dma, sizeof(*scratch), in mmc_spi_data_do()
1008 if (!data->error) in mmc_spi_data_do()
1009 data->error = tmp; in mmc_spi_data_do()
1014 * avoiding wasteful byte-at-a-time scanning... but more in mmc_spi_data_do()
1018 if (scratch->status[tmp] != 0) in mmc_spi_data_do()
1022 if (tmp < 0 && !data->error) in mmc_spi_data_do()
1023 data->error = tmp; in mmc_spi_data_do()
1030 * MMC driver implementation -- the interface to the MMC stack
1036 int status = -EINVAL; in mmc_spi_request()
1041 /* MMC core and layered drivers *MUST* issue SPI-aware commands */ in mmc_spi_request()
1046 cmd = mrq->cmd; in mmc_spi_request()
1048 dev_dbg(&host->spi->dev, "bogus command\n"); in mmc_spi_request()
1049 cmd->error = -EINVAL; in mmc_spi_request()
1053 cmd = mrq->stop; in mmc_spi_request()
1055 dev_dbg(&host->spi->dev, "bogus STOP command\n"); in mmc_spi_request()
1056 cmd->error = -EINVAL; in mmc_spi_request()
1062 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
1069 spi_bus_lock(host->spi->master); in mmc_spi_request()
1073 status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL); in mmc_spi_request()
1074 if (status == 0 && mrq->data) { in mmc_spi_request()
1075 mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz); in mmc_spi_request()
1084 if (mrq->data->error == -EILSEQ && crc_retry) { in mmc_spi_request()
1089 crc_retry--; in mmc_spi_request()
1090 mrq->data->error = 0; in mmc_spi_request()
1094 if (mrq->stop) in mmc_spi_request()
1095 status = mmc_spi_command_send(host, mrq, mrq->stop, 0); in mmc_spi_request()
1101 spi_bus_unlock(host->spi->master); in mmc_spi_request()
1103 mmc_request_done(host->mmc, mrq); in mmc_spi_request()
1117 * wait till not-busy, skip debris from any old commands. in mmc_spi_initsequence()
1123 * Do a burst with chipselect active-high. We need to do this to in mmc_spi_initsequence()
1137 * GPIOs for chip select, SPI_CS_HIGH is set but this will be logically in mmc_spi_initsequence()
1141 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1142 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1144 dev_warn(&host->spi->dev, in mmc_spi_initsequence()
1145 "can't change chip-select polarity\n"); in mmc_spi_initsequence()
1146 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1150 host->spi->mode ^= SPI_CS_HIGH; in mmc_spi_initsequence()
1151 if (spi_setup(host->spi) != 0) { in mmc_spi_initsequence()
1153 dev_err(&host->spi->dev, in mmc_spi_initsequence()
1154 "can't restore chip-select polarity\n"); in mmc_spi_initsequence()
1173 if (host->power_mode != ios->power_mode) { in mmc_spi_set_ios()
1176 canpower = host->pdata && host->pdata->setpower; in mmc_spi_set_ios()
1178 dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n", in mmc_spi_set_ios()
1179 mmc_powerstring(ios->power_mode), in mmc_spi_set_ios()
1180 ios->vdd, in mmc_spi_set_ios()
1187 switch (ios->power_mode) { in mmc_spi_set_ios()
1190 host->pdata->setpower(&host->spi->dev, in mmc_spi_set_ios()
1191 ios->vdd); in mmc_spi_set_ios()
1192 if (ios->power_mode == MMC_POWER_UP) in mmc_spi_set_ios()
1193 msleep(host->powerup_msecs); in mmc_spi_set_ios()
1198 if (ios->power_mode == MMC_POWER_ON) in mmc_spi_set_ios()
1206 * - Clock low means CPOL 0, e.g. mode 0 in mmc_spi_set_ios()
1207 * - MOSI low comes from writing zero in mmc_spi_set_ios()
1208 * - Chipselect is usually active low... in mmc_spi_set_ios()
1210 if (canpower && ios->power_mode == MMC_POWER_OFF) { in mmc_spi_set_ios()
1214 host->spi->mode &= ~(SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1215 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1217 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1220 if (spi_write(host->spi, &nullbyte, 1) < 0) in mmc_spi_set_ios()
1221 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1235 host->spi->mode |= (SPI_CPOL|SPI_CPHA); in mmc_spi_set_ios()
1236 mres = spi_setup(host->spi); in mmc_spi_set_ios()
1238 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1243 host->power_mode = ios->power_mode; in mmc_spi_set_ios()
1246 if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) { in mmc_spi_set_ios()
1249 host->spi->max_speed_hz = ios->clock; in mmc_spi_set_ios()
1250 status = spi_setup(host->spi); in mmc_spi_set_ios()
1251 dev_dbg(&host->spi->dev, in mmc_spi_set_ios()
1253 host->spi->max_speed_hz, status); in mmc_spi_set_ios()
1275 u16 delay_msec = max(host->pdata->detect_delay, (u16)100); in mmc_spi_detect_irq()
1284 struct spi_device *spi = host->spi; in mmc_spi_dma_alloc()
1287 if (!spi->master->dev.parent->dma_mask) in mmc_spi_dma_alloc()
1290 dev = spi->master->dev.parent; in mmc_spi_dma_alloc()
1292 host->ones_dma = dma_map_single(dev, host->ones, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_alloc()
1294 if (dma_mapping_error(dev, host->ones_dma)) in mmc_spi_dma_alloc()
1295 return -ENOMEM; in mmc_spi_dma_alloc()
1297 host->data_dma = dma_map_single(dev, host->data, sizeof(*host->data), in mmc_spi_dma_alloc()
1299 if (dma_mapping_error(dev, host->data_dma)) { in mmc_spi_dma_alloc()
1300 dma_unmap_single(dev, host->ones_dma, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_alloc()
1302 return -ENOMEM; in mmc_spi_dma_alloc()
1305 dma_sync_single_for_cpu(dev, host->data_dma, sizeof(*host->data), in mmc_spi_dma_alloc()
1308 host->dma_dev = dev; in mmc_spi_dma_alloc()
1314 if (!host->dma_dev) in mmc_spi_dma_free()
1317 dma_unmap_single(host->dma_dev, host->ones_dma, MMC_SPI_BLOCKSIZE, in mmc_spi_dma_free()
1319 dma_unmap_single(host->dma_dev, host->data_dma, sizeof(*host->data), in mmc_spi_dma_free()
1336 * per-transfer overheads (by making fewer transfers). in mmc_spi_probe()
1338 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX) in mmc_spi_probe()
1339 return -EINVAL; in mmc_spi_probe()
1347 if (spi->mode != SPI_MODE_3) in mmc_spi_probe()
1348 spi->mode = SPI_MODE_0; in mmc_spi_probe()
1349 spi->bits_per_word = 8; in mmc_spi_probe()
1353 dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n", in mmc_spi_probe()
1354 spi->mode, spi->max_speed_hz / 1000, in mmc_spi_probe()
1362 * NOTE if many systems use more than one MMC-over-SPI connector in mmc_spi_probe()
1365 status = -ENOMEM; in mmc_spi_probe()
1371 mmc = mmc_alloc_host(sizeof(*host), &spi->dev); in mmc_spi_probe()
1375 mmc->ops = &mmc_spi_ops; in mmc_spi_probe()
1376 mmc->max_blk_size = MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1377 mmc->max_segs = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1378 mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE; in mmc_spi_probe()
1379 mmc->max_blk_count = MMC_SPI_BLOCKSATONCE; in mmc_spi_probe()
1381 mmc->caps = MMC_CAP_SPI; in mmc_spi_probe()
1391 mmc->f_min = 400000; in mmc_spi_probe()
1392 mmc->f_max = spi->max_speed_hz; in mmc_spi_probe()
1395 host->mmc = mmc; in mmc_spi_probe()
1396 host->spi = spi; in mmc_spi_probe()
1398 host->ones = ones; in mmc_spi_probe()
1401 * and power switching gpios. in mmc_spi_probe()
1403 host->pdata = mmc_spi_get_pdata(spi); in mmc_spi_probe()
1404 if (host->pdata) in mmc_spi_probe()
1405 mmc->ocr_avail = host->pdata->ocr_mask; in mmc_spi_probe()
1406 if (!mmc->ocr_avail) { in mmc_spi_probe()
1407 dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n"); in mmc_spi_probe()
1408 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34; in mmc_spi_probe()
1410 if (host->pdata && host->pdata->setpower) { in mmc_spi_probe()
1411 host->powerup_msecs = host->pdata->powerup_msecs; in mmc_spi_probe()
1412 if (!host->powerup_msecs || host->powerup_msecs > 250) in mmc_spi_probe()
1413 host->powerup_msecs = 250; in mmc_spi_probe()
1416 dev_set_drvdata(&spi->dev, mmc); in mmc_spi_probe()
1419 host->data = kmalloc(sizeof(*host->data), GFP_KERNEL); in mmc_spi_probe()
1420 if (!host->data) in mmc_spi_probe()
1428 spi_message_init(&host->readback); in mmc_spi_probe()
1429 host->readback.is_dma_mapped = (host->dma_dev != NULL); in mmc_spi_probe()
1431 spi_message_add_tail(&host->status, &host->readback); in mmc_spi_probe()
1432 host->status.tx_buf = host->ones; in mmc_spi_probe()
1433 host->status.tx_dma = host->ones_dma; in mmc_spi_probe()
1434 host->status.rx_buf = &host->data->status; in mmc_spi_probe()
1435 host->status.rx_dma = host->data_dma + offsetof(struct scratch, status); in mmc_spi_probe()
1436 host->status.cs_change = 1; in mmc_spi_probe()
1438 /* register card detect irq */ in mmc_spi_probe()
1439 if (host->pdata && host->pdata->init) { in mmc_spi_probe()
1440 status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc); in mmc_spi_probe()
1446 if (host->pdata) { in mmc_spi_probe()
1447 mmc->caps |= host->pdata->caps; in mmc_spi_probe()
1448 mmc->caps2 |= host->pdata->caps2; in mmc_spi_probe()
1456 * Index 0 is card detect in mmc_spi_probe()
1460 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1468 mmc->caps &= ~MMC_CAP_NEEDS_POLL; in mmc_spi_probe()
1475 if (status == -EPROBE_DEFER) in mmc_spi_probe()
1480 dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n", in mmc_spi_probe()
1481 dev_name(&mmc->class_dev), in mmc_spi_probe()
1482 host->dma_dev ? "" : ", no DMA", in mmc_spi_probe()
1484 (host->pdata && host->pdata->setpower) in mmc_spi_probe()
1486 (mmc->caps & MMC_CAP_NEEDS_POLL) in mmc_spi_probe()
1495 kfree(host->data); in mmc_spi_probe()
1507 struct mmc_host *mmc = dev_get_drvdata(&spi->dev); in mmc_spi_remove()
1511 if (host->pdata && host->pdata->exit) in mmc_spi_remove()
1512 host->pdata->exit(&spi->dev, mmc); in mmc_spi_remove()
1517 kfree(host->data); in mmc_spi_remove()
1518 kfree(host->ones); in mmc_spi_remove()
1520 spi->max_speed_hz = mmc->f_max; in mmc_spi_remove()
1527 { .compatible = "mmc-spi-slot", },
1543 MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");