1 This is a port of the MMC-SPI driver from 2.6.24.3
5 --- a/drivers/mmc/host/Kconfig
6 +++ b/drivers/mmc/host/Kconfig
7 @@ -100,3 +100,16 @@ config MMC_TIFM_SD
8 To compile this driver as a module, choose M here: the
9 module will be called tifm_sd.
12 + tristate "MMC/SD over SPI (EXPERIMENTAL)"
13 + depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
17 + Some systems accss MMC/SD cards using a SPI controller instead of
18 + using a "native" MMC/SD controller. This has a disadvantage of
19 + being relatively high overhead, but a compensating advantage of
20 + working on many systems without dedicated MMC/SD controllers.
22 + If unsure, or if your system has no SPI master driver, say N.
24 --- a/drivers/mmc/host/Makefile
25 +++ b/drivers/mmc/host/Makefile
26 @@ -15,4 +15,5 @@ obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
27 obj-$(CONFIG_MMC_OMAP) += omap.o
28 obj-$(CONFIG_MMC_AT91) += at91_mci.o
29 obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
30 +obj-$(CONFIG_MMC_SPI) += mmc_spi.o
33 +++ b/drivers/mmc/host/mmc_spi.c
36 + * mmc_spi.c - Access SD/MMC cards through SPI master controllers
38 + * (C) Copyright 2005, Intec Automation,
39 + * Mike Lavender (mike@steroidmicros)
40 + * (C) Copyright 2006-2007, David Brownell
41 + * (C) Copyright 2007, Axis Communications,
42 + * Hans-Peter Nilsson (hp@axis.com)
43 + * (C) Copyright 2007, ATRON electronic GmbH,
44 + * Jan Nikitenko <jan.nikitenko@gmail.com>
47 + * This program is free software; you can redistribute it and/or modify
48 + * it under the terms of the GNU General Public License as published by
49 + * the Free Software Foundation; either version 2 of the License, or
50 + * (at your option) any later version.
52 + * This program is distributed in the hope that it will be useful,
53 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 + * GNU General Public License for more details.
57 + * You should have received a copy of the GNU General Public License
58 + * along with this program; if not, write to the Free Software
59 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
61 +#include <linux/hrtimer.h>
62 +#include <linux/delay.h>
63 +#include <linux/bio.h>
64 +#include <linux/dma-mapping.h>
65 +#include <linux/crc7.h>
66 +#include <linux/crc-itu-t.h>
67 +#include <linux/scatterlist.h>
69 +#include <linux/mmc/host.h>
70 +#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
72 +#include <linux/spi/spi.h>
73 +#include <linux/spi/mmc_spi.h>
75 +#include <asm/unaligned.h>
78 +#define sg_page(sg) (sg)->page
83 + * - For now, we won't try to interoperate with a real mmc/sd/sdio
84 + * controller, although some of them do have hardware support for
85 + * SPI protocol. The main reason for such configs would be mmc-ish
86 + * cards like DataFlash, which don't support that "native" protocol.
88 + * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
89 + * switch between driver stacks, and in any case if "native" mode
90 + * is available, it will be faster and hence preferable.
92 + * - MMC depends on a different chipselect management policy than the
93 + * SPI interface currently supports for shared bus segments: it needs
94 + * to issue multiple spi_message requests with the chipselect active,
95 + * using the results of one message to decide the next one to issue.
97 + * Pending updates to the programming interface, this driver expects
98 + * that it not share the bus with other drivers (precluding conflicts).
100 + * - We tell the controller to keep the chipselect active from the
101 + * beginning of an mmc_host_ops.request until the end. So beware
102 + * of SPI controller drivers that mis-handle the cs_change flag!
104 + * However, many cards seem OK with chipselect flapping up/down
105 + * during that time ... at least on unshared bus segments.
110 + * Local protocol constants, internal to data block protocols.
113 +/* Response tokens used to ack each block written: */
114 +#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
115 +#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
116 +#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
117 +#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
119 +/* Read and write blocks start with these tokens and end with crc;
120 + * on error, read tokens act like a subset of R2_SPI_* values.
122 +#define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
123 +#define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
124 +#define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
126 +#define MMC_SPI_BLOCKSIZE 512
129 +/* These fixed timeouts come from the latest SD specs, which say to ignore
130 + * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
131 + * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
132 + * reads which takes nowhere near that long. Older cards may be able to use
133 + * shorter timeouts ... but why bother?
135 +#define readblock_timeout ktime_set(0, 100 * 1000 * 1000)
136 +#define writeblock_timeout ktime_set(0, 250 * 1000 * 1000)
137 +#define r1b_timeout ktime_set(3, 0)
140 +/****************************************************************************/
143 + * Local Data Structures
146 +/* "scratch" is per-{command,block} data exchanged with the card */
153 +struct mmc_spi_host {
154 + struct mmc_host *mmc;
155 + struct spi_device *spi;
157 + unsigned char power_mode;
160 + struct mmc_spi_platform_data *pdata;
162 + /* for bulk data transfers */
163 + struct spi_transfer token, t, crc, early_status;
164 + struct spi_message m;
166 + /* for status readback */
167 + struct spi_transfer status;
168 + struct spi_message readback;
170 + /* underlying DMA-aware controller, or null */
171 + struct device *dma_dev;
173 + /* buffer used for commands and for message "overhead" */
174 + struct scratch *data;
175 + dma_addr_t data_dma;
177 + /* Specs say to write ones most of the time, even when the card
178 + * has no need to read its input data; and many cards won't care.
179 + * This is our source of those ones.
182 + dma_addr_t ones_dma;
186 +/****************************************************************************/
189 + * MMC-over-SPI protocol glue, used by the MMC stack interface
192 +static inline int mmc_cs_off(struct mmc_spi_host *host)
194 + /* chipselect will always be inactive after setup() */
195 + return spi_setup(host->spi);
199 +mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
203 + if (len > sizeof(*host->data)) {
208 + host->status.len = len;
211 + dma_sync_single_for_device(host->dma_dev,
212 + host->data_dma, sizeof(*host->data),
215 + status = spi_sync(host->spi, &host->readback);
218 + dma_sync_single_for_cpu(host->dma_dev,
219 + host->data_dma, sizeof(*host->data),
226 +mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
228 + u8 *cp = host->data->status;
230 + timeout = ktime_add(timeout, ktime_get());
236 + status = mmc_spi_readbytes(host, n);
240 + for (i = 0; i < n; i++) {
245 + /* REVISIT investigate msleep() to avoid busy-wait I/O
246 + * in at least some cases.
248 + if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
255 +mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
257 + return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
260 +static int mmc_spi_readtoken(struct mmc_spi_host *host)
262 + return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
267 + * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
268 + * hosts return! The low byte holds R1_SPI bits. The next byte may hold
269 + * R2_SPI bits ... for SEND_STATUS, or after data read errors.
271 + * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
272 + * newer cards R7 (IF_COND).
275 +static char *maptype(struct mmc_command *cmd)
277 + switch (mmc_spi_resp_type(cmd)) {
278 + case MMC_RSP_SPI_R1: return "R1";
279 + case MMC_RSP_SPI_R1B: return "R1B";
280 + case MMC_RSP_SPI_R2: return "R2/R5";
281 + case MMC_RSP_SPI_R3: return "R3/R4/R7";
282 + default: return "?";
286 +/* return zero, else negative errno after setting cmd->error */
287 +static int mmc_spi_response_get(struct mmc_spi_host *host,
288 + struct mmc_command *cmd, int cs_on)
290 + u8 *cp = host->data->status;
291 + u8 *end = cp + host->t.len;
295 + snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
296 + cmd->opcode, maptype(cmd));
298 + /* Except for data block reads, the whole response will already
299 + * be stored in the scratch buffer. It's somewhere after the
300 + * command and the first byte we read after it. We ignore that
301 + * first byte. After STOP_TRANSMISSION command it may include
302 + * two data bits, but otherwise it's all ones.
305 + while (cp < end && *cp == 0xff)
308 + /* Data block reads (R1 response types) may need more data... */
312 + cp = host->data->status;
314 + /* Card sends N(CR) (== 1..8) bytes of all-ones then one
315 + * status byte ... and we already scanned 2 bytes.
317 + * REVISIT block read paths use nasty byte-at-a-time I/O
318 + * so it can always DMA directly into the target buffer.
319 + * It'd probably be better to memcpy() the first chunk and
320 + * avoid extra i/o calls...
322 + for (i = 2; i < 9; i++) {
323 + value = mmc_spi_readbytes(host, 1);
329 + value = -ETIMEDOUT;
335 + dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
341 + cmd->resp[0] = *cp++;
344 + /* Status byte: the entire seven-bit R1 response. */
345 + if (cmd->resp[0] != 0) {
346 + if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS
347 + | R1_SPI_ILLEGAL_COMMAND)
350 + else if (R1_SPI_COM_CRC & cmd->resp[0])
352 + else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
355 + /* else R1_SPI_IDLE, "it's resetting" */
358 + switch (mmc_spi_resp_type(cmd)) {
360 + /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
361 + * and less-common stuff like various erase operations.
363 + case MMC_RSP_SPI_R1B:
364 + /* maybe we read all the busy tokens already */
365 + while (cp < end && *cp == 0)
368 + mmc_spi_wait_unbusy(host, r1b_timeout);
371 + /* SPI R2 == R1 + second status byte; SEND_STATUS
372 + * SPI R5 == R1 + data byte; IO_RW_DIRECT
374 + case MMC_RSP_SPI_R2:
375 + cmd->resp[0] |= *cp << 8;
378 + /* SPI R3, R4, or R7 == R1 + 4 bytes */
379 + case MMC_RSP_SPI_R3:
380 + cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
383 + /* SPI R1 == just one status byte */
384 + case MMC_RSP_SPI_R1:
388 + dev_dbg(&host->spi->dev, "bad response type %04x\n",
389 + mmc_spi_resp_type(cmd));
396 + dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
397 + tag, cmd->resp[0], cmd->resp[1]);
399 + /* disable chipselect on errors and some success cases */
400 + if (value >= 0 && cs_on)
404 + cmd->error = value;
409 +/* Issue command and read its response.
410 + * Returns zero on success, negative for error.
412 + * On error, caller must cope with mmc core retry mechanism. That
413 + * means immediate low-level resubmit, which affects the bus lock...
416 +mmc_spi_command_send(struct mmc_spi_host *host,
417 + struct mmc_request *mrq,
418 + struct mmc_command *cmd, int cs_on)
420 + struct scratch *data = host->data;
421 + u8 *cp = data->status;
422 + u32 arg = cmd->arg;
424 + struct spi_transfer *t;
426 + /* We can handle most commands (except block reads) in one full
427 + * duplex I/O operation before either starting the next transfer
428 + * (data block or command) or else deselecting the card.
430 + * First, write 7 bytes:
431 + * - an all-ones byte to ensure the card is ready
432 + * - opcode byte (plus start and transmission bits)
433 + * - four bytes of big-endian argument
434 + * - crc7 (plus end bit) ... always computed, it's cheap
436 + * We init the whole buffer to all-ones, which is what we need
437 + * to write while we're reading (later) response data.
439 + memset(cp++, 0xff, sizeof(data->status));
441 + *cp++ = 0x40 | cmd->opcode;
442 + *cp++ = (u8)(arg >> 24);
443 + *cp++ = (u8)(arg >> 16);
444 + *cp++ = (u8)(arg >> 8);
446 + *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
448 + /* Then, read up to 13 bytes (while writing all-ones):
449 + * - N(CR) (== 1..8) bytes of all-ones
450 + * - status byte (for all response types)
451 + * - the rest of the response, either:
452 + * + nothing, for R1 or R1B responses
453 + * + second status byte, for R2 responses
454 + * + four data bytes, for R3 and R7 responses
456 + * Finally, read some more bytes ... in the nice cases we know in
457 + * advance how many, and reading 1 more is always OK:
458 + * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
459 + * - N(RC) (== 1..N) bytes of all-ones, before next command
460 + * - N(WR) (== 1..N) bytes of all-ones, before data write
462 + * So in those cases one full duplex I/O of at most 21 bytes will
463 + * handle the whole command, leaving the card ready to receive a
464 + * data block or new command. We do that whenever we can, shaving
465 + * CPU and IRQ costs (especially when using DMA or FIFOs).
467 + * There are two other cases, where it's not generally practical
468 + * to rely on a single I/O:
470 + * - R1B responses need at least N(EC) bytes of all-zeroes.
472 + * In this case we can *try* to fit it into one I/O, then
473 + * maybe read more data later.
475 + * - Data block reads are more troublesome, since a variable
476 + * number of padding bytes precede the token and data.
477 + * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
478 + * + N(AC) (== 1..many) bytes of all-ones
480 + * In this case we currently only have minimal speedups here:
481 + * when N(CR) == 1 we can avoid I/O in response_get().
483 + if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
484 + cp += 2; /* min(N(CR)) + status */
487 + cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
488 + if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
490 + else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
492 + else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
493 + cp = data->status + sizeof(data->status);
494 + /* else: R1 (most commands) */
497 + dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
498 + cmd->opcode, maptype(cmd));
500 + /* send command, leaving chipselect active */
501 + spi_message_init(&host->m);
504 + memset(t, 0, sizeof(*t));
505 + t->tx_buf = t->rx_buf = data->status;
506 + t->tx_dma = t->rx_dma = host->data_dma;
507 + t->len = cp - data->status;
509 + spi_message_add_tail(t, &host->m);
511 + if (host->dma_dev) {
512 + host->m.is_dma_mapped = 1;
513 + dma_sync_single_for_device(host->dma_dev,
514 + host->data_dma, sizeof(*host->data),
515 + DMA_BIDIRECTIONAL);
517 + status = spi_sync(host->spi, &host->m);
520 + dma_sync_single_for_cpu(host->dma_dev,
521 + host->data_dma, sizeof(*host->data),
522 + DMA_BIDIRECTIONAL);
524 + dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
525 + cmd->error = status;
529 + /* after no-data commands and STOP_TRANSMISSION, chipselect off */
530 + return mmc_spi_response_get(host, cmd, cs_on);
533 +/* Build data message with up to four separate transfers. For TX, we
534 + * start by writing the data token. And in most cases, we finish with
535 + * a status transfer.
537 + * We always provide TX data for data and CRC. The MMC/SD protocol
538 + * requires us to write ones; but Linux defaults to writing zeroes;
539 + * so we explicitly initialize it to all ones on RX paths.
541 + * We also handle DMA mapping, so the underlying SPI controller does
542 + * not need to (re)do it for each message.
545 +mmc_spi_setup_data_message(
546 + struct mmc_spi_host *host,
548 + enum dma_data_direction direction)
550 + struct spi_transfer *t;
551 + struct scratch *scratch = host->data;
552 + dma_addr_t dma = host->data_dma;
554 + spi_message_init(&host->m);
556 + host->m.is_dma_mapped = 1;
558 + /* for reads, readblock() skips 0xff bytes before finding
559 + * the token; for writes, this transfer issues that token.
561 + if (direction == DMA_TO_DEVICE) {
563 + memset(t, 0, sizeof(*t));
566 + scratch->data_token = SPI_TOKEN_MULTI_WRITE;
568 + scratch->data_token = SPI_TOKEN_SINGLE;
569 + t->tx_buf = &scratch->data_token;
571 + t->tx_dma = dma + offsetof(struct scratch, data_token);
572 + spi_message_add_tail(t, &host->m);
575 + /* Body of transfer is buffer, then CRC ...
576 + * either TX-only, or RX with TX-ones.
579 + memset(t, 0, sizeof(*t));
580 + t->tx_buf = host->ones;
581 + t->tx_dma = host->ones_dma;
582 + /* length and actual buffer info are written later */
583 + spi_message_add_tail(t, &host->m);
586 + memset(t, 0, sizeof(*t));
588 + if (direction == DMA_TO_DEVICE) {
589 + /* the actual CRC may get written later */
590 + t->tx_buf = &scratch->crc_val;
592 + t->tx_dma = dma + offsetof(struct scratch, crc_val);
594 + t->tx_buf = host->ones;
595 + t->tx_dma = host->ones_dma;
596 + t->rx_buf = &scratch->crc_val;
598 + t->rx_dma = dma + offsetof(struct scratch, crc_val);
600 + spi_message_add_tail(t, &host->m);
603 + * A single block read is followed by N(EC) [0+] all-ones bytes
604 + * before deselect ... don't bother.
606 + * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
607 + * the next block is read, or a STOP_TRANSMISSION is issued. We'll
608 + * collect that single byte, so readblock() doesn't need to.
610 + * For a write, the one-byte data response follows immediately, then
611 + * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
612 + * Then single block reads may deselect, and multiblock ones issue
613 + * the next token (next data block, or STOP_TRAN). We can try to
614 + * minimize I/O ops by using a single read to collect end-of-busy.
616 + if (multiple || direction == DMA_TO_DEVICE) {
617 + t = &host->early_status;
618 + memset(t, 0, sizeof(*t));
619 + t->len = (direction == DMA_TO_DEVICE)
620 + ? sizeof(scratch->status)
622 + t->tx_buf = host->ones;
623 + t->tx_dma = host->ones_dma;
624 + t->rx_buf = scratch->status;
626 + t->rx_dma = dma + offsetof(struct scratch, status);
628 + spi_message_add_tail(t, &host->m);
634 + * - caller handled preceding N(WR) [1+] all-ones bytes
639 + * - an all-ones byte ... card writes a data-response byte
640 + * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
642 + * Return negative errno, else success.
645 +mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
647 + struct spi_device *spi = host->spi;
649 + struct scratch *scratch = host->data;
651 + if (host->mmc->use_spi_crc)
652 + scratch->crc_val = cpu_to_be16(
653 + crc_itu_t(0, t->tx_buf, t->len));
655 + dma_sync_single_for_device(host->dma_dev,
656 + host->data_dma, sizeof(*scratch),
657 + DMA_BIDIRECTIONAL);
659 + status = spi_sync(spi, &host->m);
662 + dev_dbg(&spi->dev, "write error (%d)\n", status);
667 + dma_sync_single_for_cpu(host->dma_dev,
668 + host->data_dma, sizeof(*scratch),
669 + DMA_BIDIRECTIONAL);
672 + * Get the transmission data-response reply. It must follow
673 + * immediately after the data block we transferred. This reply
674 + * doesn't necessarily tell whether the write operation succeeded;
675 + * it just says if the transmission was ok and whether *earlier*
676 + * writes succeeded; see the standard.
678 + switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
679 + case SPI_RESPONSE_ACCEPTED:
682 + case SPI_RESPONSE_CRC_ERR:
683 + /* host shall then issue MMC_STOP_TRANSMISSION */
686 + case SPI_RESPONSE_WRITE_ERR:
687 + /* host shall then issue MMC_STOP_TRANSMISSION,
688 + * and should MMC_SEND_STATUS to sort it out
697 + dev_dbg(&spi->dev, "write error %02x (%d)\n",
698 + scratch->status[0], status);
702 + t->tx_buf += t->len;
704 + t->tx_dma += t->len;
706 + /* Return when not busy. If we didn't collect that status yet,
707 + * we'll need some more I/O.
709 + for (i = 1; i < sizeof(scratch->status); i++) {
710 + if (scratch->status[i] != 0)
713 + return mmc_spi_wait_unbusy(host, writeblock_timeout);
718 + * - skip leading all-ones bytes ... either
719 + * + N(AC) [1..f(clock,CSD)] usually, else
720 + * + N(CX) [0..8] when reading CSD or CID
722 + * + token ... if error token, no data or crc
726 + * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
727 + * before dropping chipselect.
729 + * For multiblock reads, caller either reads the next block or issues a
730 + * STOP_TRANSMISSION command.
733 +mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
735 + struct spi_device *spi = host->spi;
737 + struct scratch *scratch = host->data;
739 + /* At least one SD card sends an all-zeroes byte when N(CX)
740 + * applies, before the all-ones bytes ... just cope with that.
742 + status = mmc_spi_readbytes(host, 1);
745 + status = scratch->status[0];
746 + if (status == 0xff || status == 0)
747 + status = mmc_spi_readtoken(host);
749 + if (status == SPI_TOKEN_SINGLE) {
750 + if (host->dma_dev) {
751 + dma_sync_single_for_device(host->dma_dev,
752 + host->data_dma, sizeof(*scratch),
753 + DMA_BIDIRECTIONAL);
754 + dma_sync_single_for_device(host->dma_dev,
759 + status = spi_sync(spi, &host->m);
761 + if (host->dma_dev) {
762 + dma_sync_single_for_cpu(host->dma_dev,
763 + host->data_dma, sizeof(*scratch),
764 + DMA_BIDIRECTIONAL);
765 + dma_sync_single_for_cpu(host->dma_dev,
771 + dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
773 + /* we've read extra garbage, timed out, etc */
777 + /* low four bits are an R2 subset, fifth seems to be
778 + * vendor specific ... map them all to generic error..
783 + if (host->mmc->use_spi_crc) {
784 + u16 crc = crc_itu_t(0, t->rx_buf, t->len);
786 + be16_to_cpus(&scratch->crc_val);
787 + if (scratch->crc_val != crc) {
788 + dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
789 + "computed=0x%04x len=%d\n",
790 + scratch->crc_val, crc, t->len);
795 + t->rx_buf += t->len;
797 + t->rx_dma += t->len;
803 + * An MMC/SD data stage includes one or more blocks, optional CRCs,
804 + * and inline handshaking. That handhaking makes it unlike most
805 + * other SPI protocol stacks.
808 +mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
809 + struct mmc_data *data, u32 blk_size)
811 + struct spi_device *spi = host->spi;
812 + struct device *dma_dev = host->dma_dev;
813 + struct spi_transfer *t;
814 + enum dma_data_direction direction;
815 + struct scatterlist *sg;
817 + int multiple = (data->blocks > 1);
819 + if (data->flags & MMC_DATA_READ)
820 + direction = DMA_FROM_DEVICE;
822 + direction = DMA_TO_DEVICE;
823 + mmc_spi_setup_data_message(host, multiple, direction);
826 + /* Handle scatterlist segments one at a time, with synch for
827 + * each 512-byte block
829 + for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
831 + dma_addr_t dma_addr = 0;
833 + unsigned length = sg->length;
834 + enum dma_data_direction dir = direction;
836 + /* set up dma mapping for controller drivers that might
837 + * use DMA ... though they may fall back to PIO
840 + /* never invalidate whole *shared* pages ... */
841 + if ((sg->offset != 0 || length != PAGE_SIZE)
842 + && dir == DMA_FROM_DEVICE)
843 + dir = DMA_BIDIRECTIONAL;
845 + dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
847 + if (direction == DMA_TO_DEVICE)
848 + t->tx_dma = dma_addr + sg->offset;
850 + t->rx_dma = dma_addr + sg->offset;
853 + /* allow pio too; we don't allow highmem */
854 + kmap_addr = kmap(sg_page(sg));
855 + if (direction == DMA_TO_DEVICE)
856 + t->tx_buf = kmap_addr + sg->offset;
858 + t->rx_buf = kmap_addr + sg->offset;
860 + /* transfer each block, and update request status */
862 + t->len = min(length, blk_size);
864 + dev_dbg(&host->spi->dev,
865 + " mmc_spi: %s block, %d bytes\n",
866 + (direction == DMA_TO_DEVICE)
871 + if (direction == DMA_TO_DEVICE)
872 + status = mmc_spi_writeblock(host, t);
874 + status = mmc_spi_readblock(host, t);
878 + data->bytes_xfered += t->len;
885 + /* discard mappings */
886 + if (direction == DMA_FROM_DEVICE)
887 + flush_kernel_dcache_page(sg_page(sg));
888 + kunmap(sg_page(sg));
890 + dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
893 + data->error = status;
894 + dev_dbg(&spi->dev, "%s status %d\n",
895 + (direction == DMA_TO_DEVICE)
896 + ? "write" : "read",
902 + /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
903 + * can be issued before multiblock writes. Unlike its more widely
904 + * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
905 + * that can affect the STOP_TRAN logic. Complete (and current)
906 + * MMC specs should sort that out before Linux starts using CMD23.
908 + if (direction == DMA_TO_DEVICE && multiple) {
909 + struct scratch *scratch = host->data;
911 + const unsigned statlen = sizeof(scratch->status);
913 + dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
915 + /* Tweak the per-block message we set up earlier by morphing
916 + * it to hold single buffer with the token followed by some
917 + * all-ones bytes ... skip N(BR) (0..1), scan the rest for
918 + * "not busy any longer" status, and leave chip selected.
920 + INIT_LIST_HEAD(&host->m.transfers);
921 + list_add(&host->early_status.transfer_list,
922 + &host->m.transfers);
924 + memset(scratch->status, 0xff, statlen);
925 + scratch->status[0] = SPI_TOKEN_STOP_TRAN;
927 + host->early_status.tx_buf = host->early_status.rx_buf;
928 + host->early_status.tx_dma = host->early_status.rx_dma;
929 + host->early_status.len = statlen;
932 + dma_sync_single_for_device(host->dma_dev,
933 + host->data_dma, sizeof(*scratch),
934 + DMA_BIDIRECTIONAL);
936 + tmp = spi_sync(spi, &host->m);
939 + dma_sync_single_for_cpu(host->dma_dev,
940 + host->data_dma, sizeof(*scratch),
941 + DMA_BIDIRECTIONAL);
949 + /* Ideally we collected "not busy" status with one I/O,
950 + * avoiding wasteful byte-at-a-time scanning... but more
951 + * I/O is often needed.
953 + for (tmp = 2; tmp < statlen; tmp++) {
954 + if (scratch->status[tmp] != 0)
957 + tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
958 + if (tmp < 0 && !data->error)
963 +/****************************************************************************/
966 + * MMC driver implementation -- the interface to the MMC stack
969 +static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
971 + struct mmc_spi_host *host = mmc_priv(mmc);
972 + int status = -EINVAL;
975 + /* MMC core and layered drivers *MUST* issue SPI-aware commands */
977 + struct mmc_command *cmd;
981 + if (!mmc_spi_resp_type(cmd)) {
982 + dev_dbg(&host->spi->dev, "bogus command\n");
983 + cmd->error = -EINVAL;
988 + if (cmd && !mmc_spi_resp_type(cmd)) {
989 + dev_dbg(&host->spi->dev, "bogus STOP command\n");
990 + cmd->error = -EINVAL;
996 + mmc_request_done(host->mmc, mrq);
1002 + /* issue command; then optionally data and stop */
1003 + status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
1004 + if (status == 0 && mrq->data) {
1005 + mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
1007 + status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1012 + mmc_request_done(host->mmc, mrq);
1015 +/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1017 + * NOTE that here we can't know that the card has just been powered up;
1018 + * not all MMC/SD sockets support power switching.
1020 + * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1021 + * this doesn't seem to do the right thing at all...
1023 +static void mmc_spi_initsequence(struct mmc_spi_host *host)
1025 + /* Try to be very sure any previous command has completed;
1026 + * wait till not-busy, skip debris from any old commands.
1028 + mmc_spi_wait_unbusy(host, r1b_timeout);
1029 + mmc_spi_readbytes(host, 10);
1032 + * Do a burst with chipselect active-high. We need to do this to
1033 + * meet the requirement of 74 clock cycles with both chipselect
1034 + * and CMD (MOSI) high before CMD0 ... after the card has been
1035 + * powered up to Vdd(min), and so is ready to take commands.
1037 + * Some cards are particularly needy of this (e.g. Viking "SD256")
1038 + * while most others don't seem to care.
1040 + * Note that this is one of the places MMC/SD plays games with the
1041 + * SPI protocol. Another is that when chipselect is released while
1042 + * the card returns BUSY status, the clock must issue several cycles
1043 + * with chipselect high before the card will stop driving its output.
1045 + host->spi->mode |= SPI_CS_HIGH;
1046 + if (spi_setup(host->spi) != 0) {
1047 + /* Just warn; most cards work without it. */
1048 + dev_warn(&host->spi->dev,
1049 + "can't change chip-select polarity\n");
1050 + host->spi->mode &= ~SPI_CS_HIGH;
1052 + mmc_spi_readbytes(host, 18);
1054 + host->spi->mode &= ~SPI_CS_HIGH;
1055 + if (spi_setup(host->spi) != 0) {
1056 + /* Wot, we can't get the same setup we had before? */
1057 + dev_err(&host->spi->dev,
1058 + "can't restore chip-select polarity\n");
1063 +static char *mmc_powerstring(u8 power_mode)
1065 + switch (power_mode) {
1066 + case MMC_POWER_OFF: return "off";
1067 + case MMC_POWER_UP: return "up";
1068 + case MMC_POWER_ON: return "on";
1073 +static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1075 + struct mmc_spi_host *host = mmc_priv(mmc);
1077 + if (host->power_mode != ios->power_mode) {
1080 + canpower = host->pdata && host->pdata->setpower;
1082 + dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1083 + mmc_powerstring(ios->power_mode),
1085 + canpower ? ", can switch" : "");
1087 + /* switch power on/off if possible, accounting for
1088 + * max 250msec powerup time if needed.
1091 + switch (ios->power_mode) {
1092 + case MMC_POWER_OFF:
1093 + case MMC_POWER_UP:
1094 + host->pdata->setpower(&host->spi->dev,
1096 + if (ios->power_mode == MMC_POWER_UP)
1097 + msleep(host->powerup_msecs);
1101 + /* See 6.4.1 in the simplified SD card physical spec 2.0 */
1102 + if (ios->power_mode == MMC_POWER_ON)
1103 + mmc_spi_initsequence(host);
1105 + /* If powering down, ground all card inputs to avoid power
1106 + * delivery from data lines! On a shared SPI bus, this
1107 + * will probably be temporary; 6.4.2 of the simplified SD
1108 + * spec says this must last at least 1msec.
1110 + * - Clock low means CPOL 0, e.g. mode 0
1111 + * - MOSI low comes from writing zero
1112 + * - Chipselect is usually active low...
1114 + if (canpower && ios->power_mode == MMC_POWER_OFF) {
1117 + host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1118 + mres = spi_setup(host->spi);
1120 + dev_dbg(&host->spi->dev,
1121 + "switch to SPI mode 0 failed\n");
1123 + if (spi_w8r8(host->spi, 0x00) < 0)
1124 + dev_dbg(&host->spi->dev,
1125 + "put spi signals to low failed\n");
1128 + * Now clock should be low due to spi mode 0;
1129 + * MOSI should be low because of written 0x00;
1130 + * chipselect should be low (it is active low)
1131 + * power supply is off, so now MMC is off too!
1133 + * FIXME no, chipselect can be high since the
1134 + * device is inactive and SPI_CS_HIGH is clear...
1138 + host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1139 + mres = spi_setup(host->spi);
1141 + dev_dbg(&host->spi->dev,
1142 + "switch back to SPI mode 3"
1147 + host->power_mode = ios->power_mode;
1150 + if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1153 + host->spi->max_speed_hz = ios->clock;
1154 + status = spi_setup(host->spi);
1155 + dev_dbg(&host->spi->dev,
1156 + "mmc_spi: clock to %d Hz, %d\n",
1157 + host->spi->max_speed_hz, status);
1161 +static int mmc_spi_get_ro(struct mmc_host *mmc)
1163 + struct mmc_spi_host *host = mmc_priv(mmc);
1165 + if (host->pdata && host->pdata->get_ro)
1166 + return host->pdata->get_ro(mmc->parent);
1167 + /* board doesn't support read only detection; assume writeable */
1172 +static const struct mmc_host_ops mmc_spi_ops = {
1173 + .request = mmc_spi_request,
1174 + .set_ios = mmc_spi_set_ios,
1175 + .get_ro = mmc_spi_get_ro,
1179 +/****************************************************************************/
1182 + * SPI driver implementation
1186 +mmc_spi_detect_irq(int irq, void *mmc)
1188 + struct mmc_spi_host *host = mmc_priv(mmc);
1189 + u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1191 + mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1192 + return IRQ_HANDLED;
1195 +struct count_children {
1197 + struct bus_type *bus;
1200 +static int maybe_count_child(struct device *dev, void *c)
1202 + struct count_children *ccp = c;
1204 + if (dev->bus == ccp->bus) {
1212 +static int mmc_spi_probe(struct spi_device *spi)
1215 + struct mmc_host *mmc;
1216 + struct mmc_spi_host *host;
1219 + /* MMC and SD specs only seem to care that sampling is on the
1220 + * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
1221 + * should be legit. We'll use mode 0 since it seems to be a
1222 + * bit less troublesome on some hardware ... unclear why.
1224 + spi->mode = SPI_MODE_0;
1225 + spi->bits_per_word = 8;
1227 + status = spi_setup(spi);
1229 + dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1230 + spi->mode, spi->max_speed_hz / 1000,
1235 + /* We can use the bus safely iff nobody else will interfere with us.
1236 + * Most commands consist of one SPI message to issue a command, then
1237 + * several more to collect its response, then possibly more for data
1238 + * transfer. Clocking access to other devices during that period will
1239 + * corrupt the command execution.
1241 + * Until we have software primitives which guarantee non-interference,
1242 + * we'll aim for a hardware-level guarantee.
1244 + * REVISIT we can't guarantee another device won't be added later...
1246 + if (spi->master->num_chipselect > 1) {
1247 + struct count_children cc;
1250 + cc.bus = spi->dev.bus;
1251 + status = device_for_each_child(spi->dev.parent, &cc,
1252 + maybe_count_child);
1254 + dev_err(&spi->dev, "can't share SPI bus\n");
1258 + dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
1261 + /* We need a supply of ones to transmit. This is the only time
1262 + * the CPU touches these, so cache coherency isn't a concern.
1264 + * NOTE if many systems use more than one MMC-over-SPI connector
1265 + * it'd save some memory to share this. That's evidently rare.
1268 + ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1271 + memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1273 + mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1277 + mmc->ops = &mmc_spi_ops;
1278 + mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1280 + /* As long as we keep track of the number of successfully
1281 + * transmitted blocks, we're good for multiwrite.
1283 + mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1285 + /* SPI doesn't need the lowspeed device identification thing for
1286 + * MMC or SD cards, since it never comes up in open drain mode.
1287 + * That's good; some SPI masters can't handle very low speeds!
1289 + * However, low speed SDIO cards need not handle over 400 KHz;
1290 + * that's the only reason not to use a few MHz for f_min (until
1291 + * the upper layer reads the target frequency from the CSD).
1293 + mmc->f_min = 400000;
1294 + mmc->f_max = spi->max_speed_hz;
1296 + host = mmc_priv(mmc);
1300 + host->ones = ones;
1302 + /* Platform data is used to hook up things like card sensing
1303 + * and power switching gpios.
1305 + host->pdata = spi->dev.platform_data;
1307 + mmc->ocr_avail = host->pdata->ocr_mask;
1308 + if (!mmc->ocr_avail) {
1309 + dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1310 + mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1312 + if (host->pdata && host->pdata->setpower) {
1313 + host->powerup_msecs = host->pdata->powerup_msecs;
1314 + if (!host->powerup_msecs || host->powerup_msecs > 250)
1315 + host->powerup_msecs = 250;
1318 + dev_set_drvdata(&spi->dev, mmc);
1320 + /* preallocate dma buffers */
1321 + host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1327 + if (spi->master->dev.parent->dma_mask) {
1328 + struct device *dev = spi->master->dev.parent;
1330 + host->dma_dev = dev;
1331 + host->ones_dma = dma_map_single(dev, ones,
1332 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1333 + host->data_dma = dma_map_single(dev, host->data,
1334 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1336 + /* REVISIT in theory those map operations can fail... */
1338 + dma_sync_single_for_cpu(host->dma_dev,
1339 + host->data_dma, sizeof(*host->data),
1340 + DMA_BIDIRECTIONAL);
1344 + /* setup message for status/busy readback */
1345 + spi_message_init(&host->readback);
1346 + host->readback.is_dma_mapped = (host->dma_dev != NULL);
1348 + spi_message_add_tail(&host->status, &host->readback);
1349 + host->status.tx_buf = host->ones;
1350 + host->status.tx_dma = host->ones_dma;
1351 + host->status.rx_buf = &host->data->status;
1352 + host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1353 + host->status.cs_change = 1;
1355 + /* register card detect irq */
1356 + if (host->pdata && host->pdata->init) {
1357 + status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1359 + goto fail_glue_init;
1362 + status = mmc_add_host(mmc);
1364 + goto fail_add_host;
1366 + dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
1367 + mmc->class_dev.bus_id,
1368 + host->dma_dev ? "" : ", no DMA",
1369 + (host->pdata && host->pdata->get_ro)
1371 + (host->pdata && host->pdata->setpower)
1372 + ? "" : ", no poweroff");
1376 + mmc_remove_host (mmc);
1378 + if (host->dma_dev)
1379 + dma_unmap_single(host->dma_dev, host->data_dma,
1380 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1381 + kfree(host->data);
1384 + mmc_free_host(mmc);
1385 + dev_set_drvdata(&spi->dev, NULL);
1393 +static int __devexit mmc_spi_remove(struct spi_device *spi)
1395 + struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1396 + struct mmc_spi_host *host;
1399 + host = mmc_priv(mmc);
1401 + /* prevent new mmc_detect_change() calls */
1402 + if (host->pdata && host->pdata->exit)
1403 + host->pdata->exit(&spi->dev, mmc);
1405 + mmc_remove_host(mmc);
1407 + if (host->dma_dev) {
1408 + dma_unmap_single(host->dma_dev, host->ones_dma,
1409 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1410 + dma_unmap_single(host->dma_dev, host->data_dma,
1411 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1414 + kfree(host->data);
1415 + kfree(host->ones);
1417 + spi->max_speed_hz = mmc->f_max;
1418 + mmc_free_host(mmc);
1419 + dev_set_drvdata(&spi->dev, NULL);
1425 +static struct spi_driver mmc_spi_driver = {
1427 + .name = "mmc_spi",
1428 + .bus = &spi_bus_type,
1429 + .owner = THIS_MODULE,
1431 + .probe = mmc_spi_probe,
1432 + .remove = __devexit_p(mmc_spi_remove),
1436 +static int __init mmc_spi_init(void)
1438 + return spi_register_driver(&mmc_spi_driver);
1440 +module_init(mmc_spi_init);
1443 +static void __exit mmc_spi_exit(void)
1445 + spi_unregister_driver(&mmc_spi_driver);
1447 +module_exit(mmc_spi_exit);
1450 +MODULE_AUTHOR("Mike Lavender, David Brownell, "
1451 + "Hans-Peter Nilsson, Jan Nikitenko");
1452 +MODULE_DESCRIPTION("SPI SD/MMC host driver");
1453 +MODULE_LICENSE("GPL");
1455 +++ b/include/linux/spi/mmc_spi.h
1457 +#ifndef __LINUX_SPI_MMC_SPI_H
1458 +#define __LINUX_SPI_MMC_SPI_H
1463 +/* Put this in platform_data of a device being used to manage an MMC/SD
1464 + * card slot. (Modeled after PXA mmc glue; see that for usage examples.)
1466 + * REVISIT This is not a spi-specific notion. Any card slot should be
1467 + * able to handle it. If the MMC core doesn't adopt this kind of notion,
1468 + * switch the "struct device *" parameters over to "struct spi_device *".
1470 +struct mmc_spi_platform_data {
1471 + /* driver activation and (optional) card detect irq hookup */
1472 + int (*init)(struct device *,
1473 + irqreturn_t (*)(int, void *),
1475 + void (*exit)(struct device *, void *);
1477 + /* sense switch on sd cards */
1478 + int (*get_ro)(struct device *);
1480 + /* how long to debounce card detect, in msecs */
1483 + /* power management */
1484 + u16 powerup_msecs; /* delay of up to 250 msec */
1485 + u32 ocr_mask; /* available voltages */
1486 + void (*setpower)(struct device *, unsigned int maskval);
1489 +#endif /* __LINUX_SPI_MMC_SPI_H */
1490 --- a/drivers/mmc/core/bus.c
1491 +++ b/drivers/mmc/core/bus.c
1496 +#include "sdio_cis.h"
1499 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
1500 @@ -34,6 +35,8 @@ static ssize_t mmc_type_show(struct devi
1501 return sprintf(buf, "MMC\n");
1503 return sprintf(buf, "SD\n");
1504 + case MMC_TYPE_SDIO:
1505 + return sprintf(buf, "SDIO\n");
1509 @@ -55,36 +58,37 @@ static int mmc_bus_match(struct device *
1513 -mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
1515 +mmc_bus_uevent(struct device *dev, char **envp,
1516 + int num_envp, char *buffer, int buffer_size)
1518 struct mmc_card *card = dev_to_mmc_card(dev);
1519 - int retval = 0, i = 0, length = 0;
1521 -#define add_env(fmt,val) do { \
1522 - retval = add_uevent_var(envp, num_envp, &i, \
1523 - buf, buf_size, &length, \
1530 + int i = 0, len = 0;
1532 switch (card->type) {
1534 - add_env("MMC_TYPE=%s", "MMC");
1538 - add_env("MMC_TYPE=%s", "SD");
1541 + case MMC_TYPE_SDIO:
1548 - add_env("MMC_NAME=%s", mmc_card_name(card));
1552 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_TYPE=%s", type);
1558 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_NAME=%s", mmc_card_name(card));
1564 static int mmc_bus_probe(struct device *dev)
1565 @@ -176,6 +180,11 @@ static void mmc_release_card(struct devi
1567 struct mmc_card *card = dev_to_mmc_card(dev);
1569 + sdio_free_common_cis(card);
1572 + kfree(card->info);
1577 @@ -221,15 +230,25 @@ int mmc_add_card(struct mmc_card *card)
1578 if (mmc_card_blockaddr(card))
1581 + case MMC_TYPE_SDIO:
1589 - printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1590 - mmc_hostname(card->host),
1591 - mmc_card_highspeed(card) ? "high speed " : "",
1593 + if (mmc_host_is_spi(card->host)) {
1594 + printk(KERN_INFO "%s: new %s%s card on SPI\n",
1595 + mmc_hostname(card->host),
1596 + mmc_card_highspeed(card) ? "high speed " : "",
1599 + printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1600 + mmc_hostname(card->host),
1601 + mmc_card_highspeed(card) ? "high speed " : "",
1605 card->dev.uevent_suppress = 1;
1607 @@ -261,8 +280,13 @@ int mmc_add_card(struct mmc_card *card)
1608 void mmc_remove_card(struct mmc_card *card)
1610 if (mmc_card_present(card)) {
1611 - printk(KERN_INFO "%s: card %04x removed\n",
1612 - mmc_hostname(card->host), card->rca);
1613 + if (mmc_host_is_spi(card->host)) {
1614 + printk(KERN_INFO "%s: SPI card removed\n",
1615 + mmc_hostname(card->host));
1617 + printk(KERN_INFO "%s: card %04x removed\n",
1618 + mmc_hostname(card->host), card->rca);
1621 if (card->host->bus_ops->sysfs_remove)
1622 card->host->bus_ops->sysfs_remove(card->host, card);
1623 --- a/drivers/mmc/core/core.c
1624 +++ b/drivers/mmc/core/core.c
1626 #include <linux/delay.h>
1627 #include <linux/pagemap.h>
1628 #include <linux/err.h>
1629 -#include <asm/scatterlist.h>
1630 +#include <linux/leds.h>
1631 #include <linux/scatterlist.h>
1633 #include <linux/mmc/card.h>
1638 +#include "sdio_bus.h"
1640 #include "mmc_ops.h"
1642 +#include "sdio_ops.h"
1644 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
1645 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
1646 +extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1648 static struct workqueue_struct *workqueue;
1651 + * Enabling software CRCs on the data blocks can be a significant (30%)
1652 + * performance cost, and for other reasons may not always be desired.
1653 + * So we allow it it to be disabled.
1655 +int use_spi_crc = 1;
1656 +module_param(use_spi_crc, bool, 0);
1659 * Internal function. Schedule delayed work in the MMC work queue.
1661 static int mmc_schedule_delayed_work(struct delayed_work *work,
1662 @@ -68,6 +79,11 @@ void mmc_request_done(struct mmc_host *h
1663 struct mmc_command *cmd = mrq->cmd;
1664 int err = cmd->error;
1666 + if (err && cmd->retries && mmc_host_is_spi(host)) {
1667 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
1671 if (err && cmd->retries) {
1672 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
1673 mmc_hostname(host), cmd->opcode, err);
1674 @@ -76,6 +92,8 @@ void mmc_request_done(struct mmc_host *h
1676 host->ops->request(host, mrq);
1678 + led_trigger_event(host->led, LED_OFF);
1680 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
1681 mmc_hostname(host), cmd->opcode, err,
1682 cmd->resp[0], cmd->resp[1],
1683 @@ -118,7 +136,7 @@ mmc_start_request(struct mmc_host *host,
1684 "tsac %d ms nsac %d\n",
1685 mmc_hostname(host), mrq->data->blksz,
1686 mrq->data->blocks, mrq->data->flags,
1687 - mrq->data->timeout_ns / 10000000,
1688 + mrq->data->timeout_ns / 1000000,
1689 mrq->data->timeout_clks);
1692 @@ -130,6 +148,8 @@ mmc_start_request(struct mmc_host *host,
1694 WARN_ON(!host->claimed);
1696 + led_trigger_event(host->led, LED_FULL);
1698 mrq->cmd->error = 0;
1699 mrq->cmd->mrq = mrq;
1701 @@ -199,7 +219,7 @@ int mmc_wait_for_cmd(struct mmc_host *ho
1703 struct mmc_request mrq;
1705 - BUG_ON(!host->claimed);
1706 + WARN_ON(!host->claimed);
1708 memset(&mrq, 0, sizeof(struct mmc_request));
1710 @@ -220,17 +240,24 @@ EXPORT_SYMBOL(mmc_wait_for_cmd);
1711 * mmc_set_data_timeout - set the timeout for a data command
1712 * @data: data phase for command
1713 * @card: the MMC card associated with the data transfer
1714 - * @write: flag to differentiate reads from writes
1716 * Computes the data timeout parameters according to the
1717 * correct algorithm given the card type.
1719 -void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
1721 +void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
1726 + * SDIO cards only define an upper 1 s limit on access.
1728 + if (mmc_card_sdio(card)) {
1729 + data->timeout_ns = 1000000000;
1730 + data->timeout_clks = 0;
1735 * SD cards use a 100 multiplier rather than 10
1737 mult = mmc_card_sd(card) ? 100 : 10;
1738 @@ -239,7 +266,7 @@ void mmc_set_data_timeout(struct mmc_dat
1739 * Scale up the multiplier (and therefore the timeout) by
1740 * the r2w factor for writes.
1743 + if (data->flags & MMC_DATA_WRITE)
1744 mult <<= card->csd.r2w_factor;
1746 data->timeout_ns = card->csd.tacc_ns * mult;
1747 @@ -255,7 +282,7 @@ void mmc_set_data_timeout(struct mmc_dat
1748 timeout_us += data->timeout_clks * 1000 /
1749 (card->host->ios.clock / 1000);
1752 + if (data->flags & MMC_DATA_WRITE)
1756 @@ -272,15 +299,20 @@ void mmc_set_data_timeout(struct mmc_dat
1757 EXPORT_SYMBOL(mmc_set_data_timeout);
1760 - * mmc_claim_host - exclusively claim a host
1761 + * __mmc_claim_host - exclusively claim a host
1762 * @host: mmc host to claim
1763 + * @abort: whether or not the operation should be aborted
1765 - * Claim a host for a set of operations.
1766 + * Claim a host for a set of operations. If @abort is non null and
1767 + * dereference a non-zero value then this will return prematurely with
1768 + * that non-zero value without acquiring the lock. Returns zero
1769 + * with the lock held otherwise.
1771 -void mmc_claim_host(struct mmc_host *host)
1772 +int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1774 DECLARE_WAITQUEUE(wait, current);
1775 unsigned long flags;
1780 @@ -288,19 +320,24 @@ void mmc_claim_host(struct mmc_host *hos
1781 spin_lock_irqsave(&host->lock, flags);
1783 set_current_state(TASK_UNINTERRUPTIBLE);
1784 - if (!host->claimed)
1785 + stop = abort ? atomic_read(abort) : 0;
1786 + if (stop || !host->claimed)
1788 spin_unlock_irqrestore(&host->lock, flags);
1790 spin_lock_irqsave(&host->lock, flags);
1792 set_current_state(TASK_RUNNING);
1793 - host->claimed = 1;
1795 + host->claimed = 1;
1797 + wake_up(&host->wq);
1798 spin_unlock_irqrestore(&host->lock, flags);
1799 remove_wait_queue(&host->wq, &wait);
1803 -EXPORT_SYMBOL(mmc_claim_host);
1804 +EXPORT_SYMBOL(__mmc_claim_host);
1807 * mmc_release_host - release a host
1808 @@ -313,7 +350,7 @@ void mmc_release_host(struct mmc_host *h
1810 unsigned long flags;
1812 - BUG_ON(!host->claimed);
1813 + WARN_ON(!host->claimed);
1815 spin_lock_irqsave(&host->lock, flags);
1817 @@ -433,19 +470,32 @@ static void mmc_power_up(struct mmc_host
1818 int bit = fls(host->ocr_avail) - 1;
1820 host->ios.vdd = bit;
1821 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1822 - host->ios.chip_select = MMC_CS_DONTCARE;
1823 + if (mmc_host_is_spi(host)) {
1824 + host->ios.chip_select = MMC_CS_HIGH;
1825 + host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1827 + host->ios.chip_select = MMC_CS_DONTCARE;
1828 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1830 host->ios.power_mode = MMC_POWER_UP;
1831 host->ios.bus_width = MMC_BUS_WIDTH_1;
1832 host->ios.timing = MMC_TIMING_LEGACY;
1837 + * This delay should be sufficient to allow the power supply
1838 + * to reach the minimum voltage.
1842 host->ios.clock = host->f_min;
1843 host->ios.power_mode = MMC_POWER_ON;
1847 + * This delay must be at least 74 clock sizes, or 1 ms, or the
1848 + * time required to reach a stable voltage.
1853 @@ -453,8 +503,10 @@ static void mmc_power_off(struct mmc_hos
1855 host->ios.clock = 0;
1857 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1858 - host->ios.chip_select = MMC_CS_DONTCARE;
1859 + if (!mmc_host_is_spi(host)) {
1860 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1861 + host->ios.chip_select = MMC_CS_DONTCARE;
1863 host->ios.power_mode = MMC_POWER_OFF;
1864 host->ios.bus_width = MMC_BUS_WIDTH_1;
1865 host->ios.timing = MMC_TIMING_LEGACY;
1866 @@ -511,7 +563,7 @@ void mmc_attach_bus(struct mmc_host *hos
1870 - BUG_ON(!host->claimed);
1871 + WARN_ON(!host->claimed);
1873 spin_lock_irqsave(&host->lock, flags);
1875 @@ -535,8 +587,8 @@ void mmc_detach_bus(struct mmc_host *hos
1879 - BUG_ON(!host->claimed);
1880 - BUG_ON(!host->bus_ops);
1881 + WARN_ON(!host->claimed);
1882 + WARN_ON(!host->bus_ops);
1884 spin_lock_irqsave(&host->lock, flags);
1886 @@ -564,7 +616,7 @@ void mmc_detect_change(struct mmc_host *
1887 #ifdef CONFIG_MMC_DEBUG
1888 unsigned long flags;
1889 spin_lock_irqsave(&host->lock, flags);
1890 - BUG_ON(host->removed);
1891 + WARN_ON(host->removed);
1892 spin_unlock_irqrestore(&host->lock, flags);
1895 @@ -597,24 +649,38 @@ void mmc_rescan(struct work_struct *work
1897 mmc_send_if_cond(host, host->ocr_avail);
1900 + * First we search for SDIO...
1902 + err = mmc_send_io_op_cond(host, 0, &ocr);
1904 + if (mmc_attach_sdio(host, ocr))
1905 + mmc_power_off(host);
1910 + * ...then normal SD...
1912 err = mmc_send_app_op_cond(host, 0, &ocr);
1913 - if (err == MMC_ERR_NONE) {
1915 if (mmc_attach_sd(host, ocr))
1916 mmc_power_off(host);
1919 - * If we fail to detect any SD cards then try
1920 - * searching for MMC cards.
1922 - err = mmc_send_op_cond(host, 0, &ocr);
1923 - if (err == MMC_ERR_NONE) {
1924 - if (mmc_attach_mmc(host, ocr))
1925 - mmc_power_off(host);
1931 + * ...and finally MMC.
1933 + err = mmc_send_op_cond(host, 0, &ocr);
1935 + if (mmc_attach_mmc(host, ocr))
1936 mmc_power_off(host);
1937 - mmc_release_host(host);
1942 + mmc_release_host(host);
1943 + mmc_power_off(host);
1945 if (host->bus_ops->detect && !host->bus_dead)
1946 host->bus_ops->detect(host);
1947 @@ -725,22 +791,38 @@ static int __init mmc_init(void)
1950 ret = mmc_register_bus();
1952 - ret = mmc_register_host_class();
1954 - mmc_unregister_bus();
1957 + goto destroy_workqueue;
1959 + ret = mmc_register_host_class();
1961 + goto unregister_bus;
1963 + ret = sdio_register_bus();
1965 + goto unregister_host_class;
1969 +unregister_host_class:
1970 + mmc_unregister_host_class();
1972 + mmc_unregister_bus();
1974 + destroy_workqueue(workqueue);
1979 static void __exit mmc_exit(void)
1981 + sdio_unregister_bus();
1982 mmc_unregister_host_class();
1983 mmc_unregister_bus();
1984 destroy_workqueue(workqueue);
1987 -module_init(mmc_init);
1988 +subsys_initcall(mmc_init);
1989 module_exit(mmc_exit);
1991 MODULE_LICENSE("GPL");
1992 --- a/drivers/mmc/core/core.h
1993 +++ b/drivers/mmc/core/core.h
1994 @@ -48,5 +48,7 @@ void mmc_rescan(struct work_struct *work
1995 void mmc_start_host(struct mmc_host *host);
1996 void mmc_stop_host(struct mmc_host *host);
1998 +extern int use_spi_crc;
2002 --- a/drivers/mmc/core/host.c
2003 +++ b/drivers/mmc/core/host.c
2005 #include <linux/err.h>
2006 #include <linux/idr.h>
2007 #include <linux/pagemap.h>
2008 +#include <linux/leds.h>
2010 #include <linux/mmc/host.h>
2012 @@ -100,6 +101,9 @@ int mmc_add_host(struct mmc_host *host)
2016 + WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
2017 + !host->ops->enable_sdio_irq);
2019 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
2022 @@ -112,6 +116,8 @@ int mmc_add_host(struct mmc_host *host)
2023 snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
2024 "mmc%d", host->index);
2026 + led_trigger_register_simple(host->class_dev.bus_id, &host->led);
2028 err = device_add(&host->class_dev);
2031 @@ -137,6 +143,8 @@ void mmc_remove_host(struct mmc_host *ho
2033 device_del(&host->class_dev);
2035 + led_trigger_unregister_simple(host->led);
2037 spin_lock(&mmc_host_lock);
2038 idr_remove(&mmc_host_idr, host->index);
2039 spin_unlock(&mmc_host_lock);
2040 --- a/drivers/mmc/core/mmc.c
2041 +++ b/drivers/mmc/core/mmc.c
2042 @@ -161,13 +161,12 @@ static int mmc_read_ext_csd(struct mmc_c
2046 + unsigned int ext_csd_struct;
2050 - err = MMC_ERR_FAILED;
2052 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
2053 - return MMC_ERR_NONE;
2057 * As the ext_csd is so large and mostly unused, we don't store the
2058 @@ -176,13 +175,19 @@ static int mmc_read_ext_csd(struct mmc_c
2059 ext_csd = kmalloc(512, GFP_KERNEL);
2061 printk(KERN_ERR "%s: could not allocate a buffer to "
2062 - "receive the ext_csd. mmc v4 cards will be "
2063 - "treated as v3.\n", mmc_hostname(card->host));
2064 - return MMC_ERR_FAILED;
2065 + "receive the ext_csd.\n", mmc_hostname(card->host));
2069 err = mmc_send_ext_csd(card, ext_csd);
2070 - if (err != MMC_ERR_NONE) {
2073 + * We all hosts that cannot perform the command
2074 + * to fail more gracefully
2076 + if (err != -EINVAL)
2080 * High capacity cards should have this "magic" size
2081 * stored in their CSD.
2082 @@ -197,18 +202,30 @@ static int mmc_read_ext_csd(struct mmc_c
2083 "EXT_CSD, performance might "
2085 mmc_hostname(card->host));
2086 - err = MMC_ERR_NONE;
2093 - card->ext_csd.sectors =
2094 - ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2095 - ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2096 - ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2097 - ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2098 - if (card->ext_csd.sectors)
2099 - mmc_card_set_blockaddr(card);
2100 + ext_csd_struct = ext_csd[EXT_CSD_REV];
2101 + if (ext_csd_struct > 2) {
2102 + printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
2103 + "version %d\n", mmc_hostname(card->host),
2109 + if (ext_csd_struct >= 2) {
2110 + card->ext_csd.sectors =
2111 + ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2112 + ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2113 + ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2114 + ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2115 + if (card->ext_csd.sectors)
2116 + mmc_card_set_blockaddr(card);
2119 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
2120 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
2121 @@ -246,7 +263,7 @@ static int mmc_init_card(struct mmc_host
2122 unsigned int max_dtr;
2125 - BUG_ON(!host->claimed);
2126 + WARN_ON(!host->claimed);
2129 * Since we're changing the OCR value, we seem to
2130 @@ -258,19 +275,33 @@ static int mmc_init_card(struct mmc_host
2132 /* The extra bit indicates that we support high capacity */
2133 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
2134 - if (err != MMC_ERR_NONE)
2139 + * For SPI, enable CRC as appropriate.
2141 + if (mmc_host_is_spi(host)) {
2142 + err = mmc_spi_set_crc(host, use_spi_crc);
2148 * Fetch CID from card.
2150 - err = mmc_all_send_cid(host, cid);
2151 - if (err != MMC_ERR_NONE)
2152 + if (mmc_host_is_spi(host))
2153 + err = mmc_send_cid(host, cid);
2155 + err = mmc_all_send_cid(host, cid);
2160 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2161 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2168 @@ -278,8 +309,10 @@ static int mmc_init_card(struct mmc_host
2169 * Allocate card structure.
2171 card = mmc_alloc_card(host);
2173 + if (IS_ERR(card)) {
2174 + err = PTR_ERR(card);
2178 card->type = MMC_TYPE_MMC;
2180 @@ -287,43 +320,47 @@ static int mmc_init_card(struct mmc_host
2185 + * For native busses: set card RCA and quit open drain mode.
2187 - err = mmc_set_relative_addr(card);
2188 - if (err != MMC_ERR_NONE)
2190 + if (!mmc_host_is_spi(host)) {
2191 + err = mmc_set_relative_addr(card);
2195 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2196 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2201 * Fetch CSD from card.
2203 err = mmc_send_csd(card, card->raw_csd);
2204 - if (err != MMC_ERR_NONE)
2208 err = mmc_decode_csd(card);
2212 err = mmc_decode_cid(card);
2219 * Select card, as all following commands rely on that.
2221 - err = mmc_select_card(card);
2222 - if (err != MMC_ERR_NONE)
2224 + if (!mmc_host_is_spi(host)) {
2225 + err = mmc_select_card(card);
2232 - * Fetch and process extened CSD.
2233 + * Fetch and process extended CSD.
2235 err = mmc_read_ext_csd(card);
2236 - if (err != MMC_ERR_NONE)
2241 @@ -334,7 +371,7 @@ static int mmc_init_card(struct mmc_host
2242 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
2243 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2244 EXT_CSD_HS_TIMING, 1);
2245 - if (err != MMC_ERR_NONE)
2249 mmc_card_set_highspeed(card);
2250 @@ -363,7 +400,7 @@ static int mmc_init_card(struct mmc_host
2251 (host->caps & MMC_CAP_4_BIT_DATA)) {
2252 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2253 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
2254 - if (err != MMC_ERR_NONE)
2258 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
2259 @@ -372,14 +409,14 @@ static int mmc_init_card(struct mmc_host
2263 - return MMC_ERR_NONE;
2268 mmc_remove_card(card);
2271 - return MMC_ERR_FAILED;
2276 @@ -413,7 +450,7 @@ static void mmc_detect(struct mmc_host *
2278 mmc_release_host(host);
2280 - if (err != MMC_ERR_NONE) {
2284 mmc_claim_host(host);
2285 @@ -480,7 +517,8 @@ static void mmc_suspend(struct mmc_host
2286 BUG_ON(!host->card);
2288 mmc_claim_host(host);
2289 - mmc_deselect_cards(host);
2290 + if (!mmc_host_is_spi(host))
2291 + mmc_deselect_cards(host);
2292 host->card->state &= ~MMC_STATE_HIGHSPEED;
2293 mmc_release_host(host);
2295 @@ -502,7 +540,7 @@ static void mmc_resume(struct mmc_host *
2296 err = mmc_init_card(host, host->ocr, host->card);
2297 mmc_release_host(host);
2299 - if (err != MMC_ERR_NONE) {
2303 mmc_claim_host(host);
2304 @@ -536,11 +574,20 @@ int mmc_attach_mmc(struct mmc_host *host
2308 - BUG_ON(!host->claimed);
2309 + WARN_ON(!host->claimed);
2311 mmc_attach_bus(host, &mmc_ops);
2314 + * We need to get OCR a different way for SPI.
2316 + if (mmc_host_is_spi(host)) {
2317 + err = mmc_spi_read_ocr(host, 1, &ocr);
2323 * Sanity check the voltages that the card claims to
2326 @@ -565,7 +612,7 @@ int mmc_attach_mmc(struct mmc_host *host
2327 * Detect and init the card.
2329 err = mmc_init_card(host, host->ocr, NULL);
2330 - if (err != MMC_ERR_NONE)
2334 mmc_release_host(host);
2335 @@ -587,6 +634,6 @@ err:
2336 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
2337 mmc_hostname(host), err);
2343 --- a/drivers/mmc/core/mmc_ops.c
2344 +++ b/drivers/mmc/core/mmc_ops.c
2348 #include <linux/types.h>
2349 -#include <asm/scatterlist.h>
2350 #include <linux/scatterlist.h>
2352 #include <linux/mmc/host.h>
2353 @@ -40,10 +39,10 @@ static int _mmc_select_card(struct mmc_h
2356 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2357 - if (err != MMC_ERR_NONE)
2361 - return MMC_ERR_NONE;
2365 int mmc_select_card(struct mmc_card *card)
2366 @@ -63,23 +62,36 @@ int mmc_go_idle(struct mmc_host *host)
2368 struct mmc_command cmd;
2370 - mmc_set_chip_select(host, MMC_CS_HIGH);
2374 + * Non-SPI hosts need to prevent chipselect going active during
2375 + * GO_IDLE; that would put chips into SPI mode. Remind them of
2376 + * that in case of hardware that won't pull up DAT3/nCS otherwise.
2378 + * SPI hosts ignore ios.chip_select; it's managed according to
2379 + * rules that must accomodate non-MMC slaves which this layer
2380 + * won't even know about.
2382 + if (!mmc_host_is_spi(host)) {
2383 + mmc_set_chip_select(host, MMC_CS_HIGH);
2387 memset(&cmd, 0, sizeof(struct mmc_command));
2389 cmd.opcode = MMC_GO_IDLE_STATE;
2391 - cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
2392 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
2394 err = mmc_wait_for_cmd(host, &cmd, 0);
2398 - mmc_set_chip_select(host, MMC_CS_DONTCARE);
2399 + if (!mmc_host_is_spi(host)) {
2400 + mmc_set_chip_select(host, MMC_CS_DONTCARE);
2405 + host->use_spi_crc = 0;
2409 @@ -94,23 +106,33 @@ int mmc_send_op_cond(struct mmc_host *ho
2410 memset(&cmd, 0, sizeof(struct mmc_command));
2412 cmd.opcode = MMC_SEND_OP_COND;
2414 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
2415 + cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
2416 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
2418 for (i = 100; i; i--) {
2419 err = mmc_wait_for_cmd(host, &cmd, 0);
2420 - if (err != MMC_ERR_NONE)
2424 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
2425 + /* if we're just probing, do a single pass */
2429 - err = MMC_ERR_TIMEOUT;
2430 + /* otherwise wait until reset completes */
2431 + if (mmc_host_is_spi(host)) {
2432 + if (!(cmd.resp[0] & R1_SPI_IDLE))
2435 + if (cmd.resp[0] & MMC_CARD_BUSY)
2445 + if (rocr && !mmc_host_is_spi(host))
2446 *rocr = cmd.resp[0];
2449 @@ -131,12 +153,12 @@ int mmc_all_send_cid(struct mmc_host *ho
2450 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
2452 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2453 - if (err != MMC_ERR_NONE)
2457 memcpy(cid, cmd.resp, sizeof(u32) * 4);
2459 - return MMC_ERR_NONE;
2463 int mmc_set_relative_addr(struct mmc_card *card)
2464 @@ -154,46 +176,52 @@ int mmc_set_relative_addr(struct mmc_car
2465 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2467 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2468 - if (err != MMC_ERR_NONE)
2472 - return MMC_ERR_NONE;
2476 -int mmc_send_csd(struct mmc_card *card, u32 *csd)
2478 +mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
2481 struct mmc_command cmd;
2484 - BUG_ON(!card->host);
2489 memset(&cmd, 0, sizeof(struct mmc_command));
2491 - cmd.opcode = MMC_SEND_CSD;
2492 - cmd.arg = card->rca << 16;
2493 + cmd.opcode = opcode;
2495 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
2497 - err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2498 - if (err != MMC_ERR_NONE)
2499 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2503 - memcpy(csd, cmd.resp, sizeof(u32) * 4);
2504 + memcpy(cxd, cmd.resp, sizeof(u32) * 4);
2506 - return MMC_ERR_NONE;
2510 -int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2512 +mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
2513 + u32 opcode, void *buf, unsigned len)
2515 struct mmc_request mrq;
2516 struct mmc_command cmd;
2517 struct mmc_data data;
2518 struct scatterlist sg;
2522 - BUG_ON(!card->host);
2524 + /* dma onto stack is unsafe/nonportable, but callers to this
2525 + * routine normally provide temporary on-stack buffers ...
2527 + data_buf = kmalloc(len, GFP_KERNEL);
2528 + if (data_buf == NULL)
2531 memset(&mrq, 0, sizeof(struct mmc_request));
2532 memset(&cmd, 0, sizeof(struct mmc_command));
2533 @@ -202,28 +230,117 @@ int mmc_send_ext_csd(struct mmc_card *ca
2537 - cmd.opcode = MMC_SEND_EXT_CSD;
2538 + cmd.opcode = opcode;
2540 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2543 + /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
2544 + * rely on callers to never use this with "native" calls for reading
2545 + * CSD or CID. Native versions of those commands use the R2 type,
2546 + * not R1 plus a data block.
2548 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2552 data.flags = MMC_DATA_READ;
2556 - sg_init_one(&sg, ext_csd, 512);
2557 + sg_init_one(&sg, data_buf, len);
2560 + mmc_set_data_timeout(&data, card);
2562 - mmc_set_data_timeout(&data, card, 0);
2563 + mmc_wait_for_req(host, &mrq);
2565 - mmc_wait_for_req(card->host, &mrq);
2566 + memcpy(buf, data_buf, len);
2569 - if (cmd.error != MMC_ERR_NONE)
2572 - if (data.error != MMC_ERR_NONE)
2576 - return MMC_ERR_NONE;
2580 +int mmc_send_csd(struct mmc_card *card, u32 *csd)
2584 + if (!mmc_host_is_spi(card->host))
2585 + return mmc_send_cxd_native(card->host, card->rca << 16,
2586 + csd, MMC_SEND_CSD);
2588 + ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
2592 + for (i = 0;i < 4;i++)
2593 + csd[i] = be32_to_cpu(csd[i]);
2598 +int mmc_send_cid(struct mmc_host *host, u32 *cid)
2602 + if (!mmc_host_is_spi(host)) {
2605 + return mmc_send_cxd_native(host, host->card->rca << 16,
2606 + cid, MMC_SEND_CID);
2609 + ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
2613 + for (i = 0;i < 4;i++)
2614 + cid[i] = be32_to_cpu(cid[i]);
2619 +int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2621 + return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
2625 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
2627 + struct mmc_command cmd;
2630 + memset(&cmd, 0, sizeof(struct mmc_command));
2632 + cmd.opcode = MMC_SPI_READ_OCR;
2633 + cmd.arg = highcap ? (1 << 30) : 0;
2634 + cmd.flags = MMC_RSP_SPI_R3;
2636 + err = mmc_wait_for_cmd(host, &cmd, 0);
2638 + *ocrp = cmd.resp[1];
2642 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
2644 + struct mmc_command cmd;
2647 + memset(&cmd, 0, sizeof(struct mmc_command));
2649 + cmd.opcode = MMC_SPI_CRC_ON_OFF;
2650 + cmd.flags = MMC_RSP_SPI_R1;
2651 + cmd.arg = use_crc;
2653 + err = mmc_wait_for_cmd(host, &cmd, 0);
2655 + host->use_spi_crc = use_crc;
2659 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
2660 @@ -241,13 +358,13 @@ int mmc_switch(struct mmc_card *card, u8
2664 - cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
2665 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2667 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2668 - if (err != MMC_ERR_NONE)
2672 - return MMC_ERR_NONE;
2676 int mmc_send_status(struct mmc_card *card, u32 *status)
2677 @@ -261,16 +378,20 @@ int mmc_send_status(struct mmc_card *car
2678 memset(&cmd, 0, sizeof(struct mmc_command));
2680 cmd.opcode = MMC_SEND_STATUS;
2681 - cmd.arg = card->rca << 16;
2682 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2683 + if (!mmc_host_is_spi(card->host))
2684 + cmd.arg = card->rca << 16;
2685 + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2687 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2688 - if (err != MMC_ERR_NONE)
2692 + /* NOTE: callers are required to understand the difference
2693 + * between "native" and SPI format status words!
2696 *status = cmd.resp[0];
2698 - return MMC_ERR_NONE;
2702 --- a/drivers/mmc/core/mmc_ops.h
2703 +++ b/drivers/mmc/core/mmc_ops.h
2704 @@ -22,6 +22,9 @@ int mmc_send_csd(struct mmc_card *card,
2705 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
2706 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
2707 int mmc_send_status(struct mmc_card *card, u32 *status);
2708 +int mmc_send_cid(struct mmc_host *host, u32 *cid);
2709 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
2710 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
2714 --- a/drivers/mmc/core/sd.c
2715 +++ b/drivers/mmc/core/sd.c
2716 @@ -166,8 +166,6 @@ static int mmc_decode_scr(struct mmc_car
2717 unsigned int scr_struct;
2720 - BUG_ON(!mmc_card_sd(card));
2722 resp[3] = card->raw_scr[1];
2723 resp[2] = card->raw_scr[0];
2725 @@ -193,30 +191,38 @@ static int mmc_read_switch(struct mmc_ca
2728 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2729 - return MMC_ERR_NONE;
2732 if (!(card->csd.cmdclass & CCC_SWITCH)) {
2733 printk(KERN_WARNING "%s: card lacks mandatory switch "
2734 "function, performance might suffer.\n",
2735 mmc_hostname(card->host));
2736 - return MMC_ERR_NONE;
2740 - err = MMC_ERR_FAILED;
2743 status = kmalloc(64, GFP_KERNEL);
2745 printk(KERN_ERR "%s: could not allocate a buffer for "
2746 "switch capabilities.\n", mmc_hostname(card->host));
2751 err = mmc_sd_switch(card, 0, 0, 1, status);
2752 - if (err != MMC_ERR_NONE) {
2755 + * We all hosts that cannot perform the command
2756 + * to fail more gracefully
2758 + if (err != -EINVAL)
2761 printk(KERN_WARNING "%s: problem reading switch "
2762 "capabilities, performance might suffer.\n",
2763 mmc_hostname(card->host));
2764 - err = MMC_ERR_NONE;
2770 @@ -238,28 +244,28 @@ static int mmc_switch_hs(struct mmc_card
2773 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2774 - return MMC_ERR_NONE;
2777 if (!(card->csd.cmdclass & CCC_SWITCH))
2778 - return MMC_ERR_NONE;
2781 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
2782 - return MMC_ERR_NONE;
2785 if (card->sw_caps.hs_max_dtr == 0)
2786 - return MMC_ERR_NONE;
2789 - err = MMC_ERR_FAILED;
2792 status = kmalloc(64, GFP_KERNEL);
2794 printk(KERN_ERR "%s: could not allocate a buffer for "
2795 "switch capabilities.\n", mmc_hostname(card->host));
2800 err = mmc_sd_switch(card, 1, 0, 1, status);
2801 - if (err != MMC_ERR_NONE)
2805 if ((status[16] & 0xF) != 1) {
2806 @@ -292,7 +298,7 @@ static int mmc_sd_init_card(struct mmc_h
2807 unsigned int max_dtr;
2810 - BUG_ON(!host->claimed);
2811 + WARN_ON(!host->claimed);
2814 * Since we're changing the OCR value, we seem to
2815 @@ -309,23 +315,37 @@ static int mmc_sd_init_card(struct mmc_h
2816 * block-addressed SDHC cards.
2818 err = mmc_send_if_cond(host, ocr);
2819 - if (err == MMC_ERR_NONE)
2823 err = mmc_send_app_op_cond(host, ocr, NULL);
2824 - if (err != MMC_ERR_NONE)
2829 + * For SPI, enable CRC as appropriate.
2831 + if (mmc_host_is_spi(host)) {
2832 + err = mmc_spi_set_crc(host, use_spi_crc);
2838 * Fetch CID from card.
2840 - err = mmc_all_send_cid(host, cid);
2841 - if (err != MMC_ERR_NONE)
2842 + if (mmc_host_is_spi(host))
2843 + err = mmc_send_cid(host, cid);
2845 + err = mmc_all_send_cid(host, cid);
2850 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2851 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2858 @@ -333,32 +353,36 @@ static int mmc_sd_init_card(struct mmc_h
2859 * Allocate card structure.
2861 card = mmc_alloc_card(host);
2863 + if (IS_ERR(card)) {
2864 + err = PTR_ERR(card);
2868 card->type = MMC_TYPE_SD;
2869 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
2874 + * For native busses: get card RCA and quit open drain mode.
2876 - err = mmc_send_relative_addr(host, &card->rca);
2877 - if (err != MMC_ERR_NONE)
2879 + if (!mmc_host_is_spi(host)) {
2880 + err = mmc_send_relative_addr(host, &card->rca);
2884 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2885 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2890 * Fetch CSD from card.
2892 err = mmc_send_csd(card, card->raw_csd);
2893 - if (err != MMC_ERR_NONE)
2897 err = mmc_decode_csd(card);
2902 mmc_decode_cid(card);
2903 @@ -367,16 +391,18 @@ static int mmc_sd_init_card(struct mmc_h
2905 * Select card, as all following commands rely on that.
2907 - err = mmc_select_card(card);
2908 - if (err != MMC_ERR_NONE)
2910 + if (!mmc_host_is_spi(host)) {
2911 + err = mmc_select_card(card);
2918 * Fetch SCR from card.
2920 err = mmc_app_send_scr(card, card->raw_scr);
2921 - if (err != MMC_ERR_NONE)
2925 err = mmc_decode_scr(card);
2926 @@ -387,7 +413,7 @@ static int mmc_sd_init_card(struct mmc_h
2927 * Fetch switch information from card.
2929 err = mmc_read_switch(card);
2930 - if (err != MMC_ERR_NONE)
2935 @@ -395,7 +421,7 @@ static int mmc_sd_init_card(struct mmc_h
2936 * Attempt to change to high-speed (if supported)
2938 err = mmc_switch_hs(card);
2939 - if (err != MMC_ERR_NONE)
2944 @@ -418,7 +444,7 @@ static int mmc_sd_init_card(struct mmc_h
2945 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
2946 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
2947 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
2948 - if (err != MMC_ERR_NONE)
2952 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
2953 @@ -442,14 +468,14 @@ static int mmc_sd_init_card(struct mmc_h
2957 - return MMC_ERR_NONE;
2962 mmc_remove_card(card);
2965 - return MMC_ERR_FAILED;
2970 @@ -483,7 +509,7 @@ static void mmc_sd_detect(struct mmc_hos
2972 mmc_release_host(host);
2974 - if (err != MMC_ERR_NONE) {
2976 mmc_sd_remove(host);
2978 mmc_claim_host(host);
2979 @@ -552,7 +578,8 @@ static void mmc_sd_suspend(struct mmc_ho
2980 BUG_ON(!host->card);
2982 mmc_claim_host(host);
2983 - mmc_deselect_cards(host);
2984 + if (!mmc_host_is_spi(host))
2985 + mmc_deselect_cards(host);
2986 host->card->state &= ~MMC_STATE_HIGHSPEED;
2987 mmc_release_host(host);
2989 @@ -574,7 +601,7 @@ static void mmc_sd_resume(struct mmc_hos
2990 err = mmc_sd_init_card(host, host->ocr, host->card);
2991 mmc_release_host(host);
2993 - if (err != MMC_ERR_NONE) {
2995 mmc_sd_remove(host);
2997 mmc_claim_host(host);
2998 @@ -608,11 +635,22 @@ int mmc_attach_sd(struct mmc_host *host,
3002 - BUG_ON(!host->claimed);
3003 + WARN_ON(!host->claimed);
3005 mmc_attach_bus(host, &mmc_sd_ops);
3008 + * We need to get OCR a different way for SPI.
3010 + if (mmc_host_is_spi(host)) {
3011 + mmc_go_idle(host);
3013 + err = mmc_spi_read_ocr(host, 0, &ocr);
3019 * Sanity check the voltages that the card claims to
3022 @@ -644,7 +682,7 @@ int mmc_attach_sd(struct mmc_host *host,
3023 * Detect and init the card.
3025 err = mmc_sd_init_card(host, host->ocr, NULL);
3026 - if (err != MMC_ERR_NONE)
3030 mmc_release_host(host);
3031 @@ -666,6 +704,6 @@ err:
3032 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
3033 mmc_hostname(host), err);
3039 --- a/drivers/mmc/core/sd_ops.c
3040 +++ b/drivers/mmc/core/sd_ops.c
3044 #include <linux/types.h>
3045 -#include <asm/scatterlist.h>
3046 #include <linux/scatterlist.h>
3048 #include <linux/mmc/host.h>
3049 @@ -33,21 +32,21 @@ static int mmc_app_cmd(struct mmc_host *
3052 cmd.arg = card->rca << 16;
3053 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3054 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
3057 - cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
3058 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
3061 err = mmc_wait_for_cmd(host, &cmd, 0);
3062 - if (err != MMC_ERR_NONE)
3066 /* Check that card supported application commands */
3067 - if (!(cmd.resp[0] & R1_APP_CMD))
3068 - return MMC_ERR_FAILED;
3069 + if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
3070 + return -EOPNOTSUPP;
3072 - return MMC_ERR_NONE;
3077 @@ -73,7 +72,7 @@ int mmc_wait_for_app_cmd(struct mmc_host
3079 BUG_ON(retries < 0);
3081 - err = MMC_ERR_INVALID;
3085 * We have to resend MMC_APP_CMD for each attempt so
3086 @@ -83,8 +82,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3087 memset(&mrq, 0, sizeof(struct mmc_request));
3089 err = mmc_app_cmd(host, card);
3090 - if (err != MMC_ERR_NONE)
3092 + /* no point in retrying; no APP commands allowed */
3093 + if (mmc_host_is_spi(host)) {
3094 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3100 memset(&mrq, 0, sizeof(struct mmc_request));
3102 @@ -97,8 +102,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3103 mmc_wait_for_req(host, &mrq);
3106 - if (cmd->error == MMC_ERR_NONE)
3110 + /* no point in retrying illegal APP commands */
3111 + if (mmc_host_is_spi(host)) {
3112 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3118 @@ -127,14 +138,14 @@ int mmc_app_set_bus_width(struct mmc_car
3119 cmd.arg = SD_BUS_WIDTH_4;
3122 - return MMC_ERR_INVALID;
3126 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
3127 - if (err != MMC_ERR_NONE)
3131 - return MMC_ERR_NONE;
3135 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
3136 @@ -147,23 +158,36 @@ int mmc_send_app_op_cond(struct mmc_host
3137 memset(&cmd, 0, sizeof(struct mmc_command));
3139 cmd.opcode = SD_APP_OP_COND;
3141 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
3142 + if (mmc_host_is_spi(host))
3143 + cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
3146 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
3148 for (i = 100; i; i--) {
3149 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
3150 - if (err != MMC_ERR_NONE)
3154 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
3155 + /* if we're just probing, do a single pass */
3159 - err = MMC_ERR_TIMEOUT;
3160 + /* otherwise wait until reset completes */
3161 + if (mmc_host_is_spi(host)) {
3162 + if (!(cmd.resp[0] & R1_SPI_IDLE))
3165 + if (cmd.resp[0] & MMC_CARD_BUSY)
3175 + if (rocr && !mmc_host_is_spi(host))
3176 *rocr = cmd.resp[0];
3179 @@ -174,6 +198,7 @@ int mmc_send_if_cond(struct mmc_host *ho
3180 struct mmc_command cmd;
3182 static const u8 test_pattern = 0xAA;
3183 + u8 result_pattern;
3186 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
3187 @@ -182,16 +207,21 @@ int mmc_send_if_cond(struct mmc_host *ho
3189 cmd.opcode = SD_SEND_IF_COND;
3190 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
3191 - cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
3192 + cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
3194 err = mmc_wait_for_cmd(host, &cmd, 0);
3195 - if (err != MMC_ERR_NONE)
3199 - if ((cmd.resp[0] & 0xFF) != test_pattern)
3200 - return MMC_ERR_FAILED;
3201 + if (mmc_host_is_spi(host))
3202 + result_pattern = cmd.resp[1] & 0xFF;
3204 + result_pattern = cmd.resp[0] & 0xFF;
3206 + if (result_pattern != test_pattern)
3209 - return MMC_ERR_NONE;
3213 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
3214 @@ -209,12 +239,12 @@ int mmc_send_relative_addr(struct mmc_ho
3215 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
3217 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
3218 - if (err != MMC_ERR_NONE)
3222 *rca = cmd.resp[0] >> 16;
3224 - return MMC_ERR_NONE;
3228 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
3229 @@ -229,8 +259,10 @@ int mmc_app_send_scr(struct mmc_card *ca
3230 BUG_ON(!card->host);
3233 + /* NOTE: caller guarantees scr is heap-allocated */
3235 err = mmc_app_cmd(card->host, card);
3236 - if (err != MMC_ERR_NONE)
3240 memset(&mrq, 0, sizeof(struct mmc_request));
3241 @@ -242,7 +274,7 @@ int mmc_app_send_scr(struct mmc_card *ca
3243 cmd.opcode = SD_APP_SEND_SCR;
3245 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3246 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3250 @@ -252,19 +284,19 @@ int mmc_app_send_scr(struct mmc_card *ca
3252 sg_init_one(&sg, scr, 8);
3254 - mmc_set_data_timeout(&data, card, 0);
3255 + mmc_set_data_timeout(&data, card);
3257 mmc_wait_for_req(card->host, &mrq);
3259 - if (cmd.error != MMC_ERR_NONE)
3262 - if (data.error != MMC_ERR_NONE)
3266 - scr[0] = ntohl(scr[0]);
3267 - scr[1] = ntohl(scr[1]);
3268 + scr[0] = be32_to_cpu(scr[0]);
3269 + scr[1] = be32_to_cpu(scr[1]);
3271 - return MMC_ERR_NONE;
3275 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
3276 @@ -278,6 +310,8 @@ int mmc_sd_switch(struct mmc_card *card,
3278 BUG_ON(!card->host);
3280 + /* NOTE: caller guarantees resp is heap-allocated */
3285 @@ -292,7 +326,7 @@ int mmc_sd_switch(struct mmc_card *card,
3286 cmd.arg = mode << 31 | 0x00FFFFFF;
3287 cmd.arg &= ~(0xF << (group * 4));
3288 cmd.arg |= value << (group * 4);
3289 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3290 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3294 @@ -302,15 +336,15 @@ int mmc_sd_switch(struct mmc_card *card,
3296 sg_init_one(&sg, resp, 64);
3298 - mmc_set_data_timeout(&data, card, 0);
3299 + mmc_set_data_timeout(&data, card);
3301 mmc_wait_for_req(card->host, &mrq);
3303 - if (cmd.error != MMC_ERR_NONE)
3306 - if (data.error != MMC_ERR_NONE)
3310 - return MMC_ERR_NONE;
3315 +++ b/drivers/mmc/core/sdio.c
3318 + * linux/drivers/mmc/sdio.c
3320 + * Copyright 2006-2007 Pierre Ossman
3322 + * This program is free software; you can redistribute it and/or modify
3323 + * it under the terms of the GNU General Public License as published by
3324 + * the Free Software Foundation; either version 2 of the License, or (at
3325 + * your option) any later version.
3328 +#include <linux/err.h>
3330 +#include <linux/mmc/host.h>
3331 +#include <linux/mmc/card.h>
3332 +#include <linux/mmc/sdio.h>
3333 +#include <linux/mmc/sdio_func.h>
3337 +#include "sdio_bus.h"
3338 +#include "mmc_ops.h"
3339 +#include "sd_ops.h"
3340 +#include "sdio_ops.h"
3341 +#include "sdio_cis.h"
3343 +static int sdio_read_fbr(struct sdio_func *func)
3346 + unsigned char data;
3348 + ret = mmc_io_rw_direct(func->card, 0, 0,
3349 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
3355 + if (data == 0x0f) {
3356 + ret = mmc_io_rw_direct(func->card, 0, 0,
3357 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
3362 + func->class = data;
3368 +static int sdio_init_func(struct mmc_card *card, unsigned int fn)
3371 + struct sdio_func *func;
3373 + BUG_ON(fn > SDIO_MAX_FUNCS);
3375 + func = sdio_alloc_func(card);
3377 + return PTR_ERR(func);
3381 + ret = sdio_read_fbr(func);
3385 + ret = sdio_read_func_cis(func);
3389 + card->sdio_func[fn - 1] = func;
3395 + * It is okay to remove the function here even though we hold
3396 + * the host lock as we haven't registered the device yet.
3398 + sdio_remove_func(func);
3402 +static int sdio_read_cccr(struct mmc_card *card)
3406 + unsigned char data;
3408 + memset(&card->cccr, 0, sizeof(struct sdio_cccr));
3410 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
3414 + cccr_vsn = data & 0x0f;
3416 + if (cccr_vsn > SDIO_CCCR_REV_1_20) {
3417 + printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
3418 + mmc_hostname(card->host), cccr_vsn);
3422 + card->cccr.sdio_vsn = (data & 0xf0) >> 4;
3424 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
3428 + if (data & SDIO_CCCR_CAP_SMB)
3429 + card->cccr.multi_block = 1;
3430 + if (data & SDIO_CCCR_CAP_LSC)
3431 + card->cccr.low_speed = 1;
3432 + if (data & SDIO_CCCR_CAP_4BLS)
3433 + card->cccr.wide_bus = 1;
3435 + if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
3436 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
3440 + if (data & SDIO_POWER_SMPC)
3441 + card->cccr.high_power = 1;
3444 + if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
3445 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
3449 + if (data & SDIO_SPEED_SHS)
3450 + card->cccr.high_speed = 1;
3457 +static int sdio_enable_wide(struct mmc_card *card)
3462 + if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
3465 + if (card->cccr.low_speed && !card->cccr.wide_bus)
3468 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
3472 + ctrl |= SDIO_BUS_WIDTH_4BIT;
3474 + ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
3478 + mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
3484 + * Host is being removed. Free up the current card.
3486 +static void mmc_sdio_remove(struct mmc_host *host)
3491 + BUG_ON(!host->card);
3493 + for (i = 0;i < host->card->sdio_funcs;i++) {
3494 + if (host->card->sdio_func[i]) {
3495 + sdio_remove_func(host->card->sdio_func[i]);
3496 + host->card->sdio_func[i] = NULL;
3500 + mmc_remove_card(host->card);
3501 + host->card = NULL;
3505 + * Card detection callback from host.
3507 +static void mmc_sdio_detect(struct mmc_host *host)
3512 + BUG_ON(!host->card);
3514 + mmc_claim_host(host);
3517 + * Just check if our card has been removed.
3519 + err = mmc_select_card(host->card);
3521 + mmc_release_host(host);
3524 + mmc_sdio_remove(host);
3526 + mmc_claim_host(host);
3527 + mmc_detach_bus(host);
3528 + mmc_release_host(host);
3533 +static const struct mmc_bus_ops mmc_sdio_ops = {
3534 + .remove = mmc_sdio_remove,
3535 + .detect = mmc_sdio_detect,
3540 + * Starting point for SDIO card init.
3542 +int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
3546 + struct mmc_card *card;
3549 + WARN_ON(!host->claimed);
3551 + mmc_attach_bus(host, &mmc_sdio_ops);
3554 + * Sanity check the voltages that the card claims to
3558 + printk(KERN_WARNING "%s: card claims to support voltages "
3559 + "below the defined range. These will be ignored.\n",
3560 + mmc_hostname(host));
3564 + if (ocr & MMC_VDD_165_195) {
3565 + printk(KERN_WARNING "%s: SDIO card claims to support the "
3566 + "incompletely defined 'low voltage range'. This "
3567 + "will be ignored.\n", mmc_hostname(host));
3568 + ocr &= ~MMC_VDD_165_195;
3571 + host->ocr = mmc_select_voltage(host, ocr);
3574 + * Can we support the voltage(s) of the card(s)?
3582 + * Inform the card of the voltage
3584 + err = mmc_send_io_op_cond(host, host->ocr, &ocr);
3589 + * For SPI, enable CRC as appropriate.
3591 + if (mmc_host_is_spi(host)) {
3592 + err = mmc_spi_set_crc(host, use_spi_crc);
3598 + * The number of functions on the card is encoded inside
3601 + funcs = (ocr & 0x70000000) >> 28;
3604 + * Allocate card structure.
3606 + card = mmc_alloc_card(host);
3607 + if (IS_ERR(card)) {
3608 + err = PTR_ERR(card);
3612 + card->type = MMC_TYPE_SDIO;
3613 + card->sdio_funcs = funcs;
3615 + host->card = card;
3618 + * For native busses: set card RCA and quit open drain mode.
3620 + if (!mmc_host_is_spi(host)) {
3621 + err = mmc_send_relative_addr(host, &card->rca);
3625 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
3629 + * Select card, as all following commands rely on that.
3631 + if (!mmc_host_is_spi(host)) {
3632 + err = mmc_select_card(card);
3638 + * Read the common registers.
3640 + err = sdio_read_cccr(card);
3645 + * Read the common CIS tuples.
3647 + err = sdio_read_common_cis(card);
3652 + * No support for high-speed yet, so just set
3653 + * the card's maximum speed.
3655 + mmc_set_clock(host, card->cis.max_dtr);
3658 + * Switch to wider bus (if supported).
3660 + err = sdio_enable_wide(card);
3665 + * Initialize (but don't add) all present functions.
3667 + for (i = 0;i < funcs;i++) {
3668 + err = sdio_init_func(host->card, i + 1);
3673 + mmc_release_host(host);
3676 + * First add the card to the driver model...
3678 + err = mmc_add_card(host->card);
3680 + goto remove_added;
3683 + * ...then the SDIO functions.
3685 + for (i = 0;i < funcs;i++) {
3686 + err = sdio_add_func(host->card->sdio_func[i]);
3688 + goto remove_added;
3695 + /* Remove without lock if the device has been added. */
3696 + mmc_sdio_remove(host);
3697 + mmc_claim_host(host);
3699 + /* And with lock if it hasn't been added. */
3701 + mmc_sdio_remove(host);
3703 + mmc_detach_bus(host);
3704 + mmc_release_host(host);
3706 + printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
3707 + mmc_hostname(host), err);
3713 +++ b/drivers/mmc/core/sdio_bus.c
3716 + * linux/drivers/mmc/core/sdio_bus.c
3718 + * Copyright 2007 Pierre Ossman
3720 + * This program is free software; you can redistribute it and/or modify
3721 + * it under the terms of the GNU General Public License as published by
3722 + * the Free Software Foundation; either version 2 of the License, or (at
3723 + * your option) any later version.
3725 + * SDIO function driver model
3728 +#include <linux/device.h>
3729 +#include <linux/err.h>
3731 +#include <linux/mmc/card.h>
3732 +#include <linux/mmc/sdio_func.h>
3734 +#include "sdio_cis.h"
3735 +#include "sdio_bus.h"
3737 +#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
3738 +#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
3740 +/* show configuration fields */
3741 +#define sdio_config_attr(field, format_string) \
3743 +field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
3745 + struct sdio_func *func; \
3747 + func = dev_to_sdio_func (dev); \
3748 + return sprintf (buf, format_string, func->field); \
3751 +sdio_config_attr(class, "0x%02x\n");
3752 +sdio_config_attr(vendor, "0x%04x\n");
3753 +sdio_config_attr(device, "0x%04x\n");
3755 +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3757 + struct sdio_func *func = dev_to_sdio_func (dev);
3759 + return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
3760 + func->class, func->vendor, func->device);
3763 +static struct device_attribute sdio_dev_attrs[] = {
3765 + __ATTR_RO(vendor),
3766 + __ATTR_RO(device),
3767 + __ATTR_RO(modalias),
3771 +static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
3772 + const struct sdio_device_id *id)
3774 + if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
3776 + if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
3778 + if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
3783 +static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
3784 + struct sdio_driver *sdrv)
3786 + const struct sdio_device_id *ids;
3788 + ids = sdrv->id_table;
3791 + while (ids->class || ids->vendor || ids->device) {
3792 + if (sdio_match_one(func, ids))
3801 +static int sdio_bus_match(struct device *dev, struct device_driver *drv)
3803 + struct sdio_func *func = dev_to_sdio_func(dev);
3804 + struct sdio_driver *sdrv = to_sdio_driver(drv);
3806 + if (sdio_match_device(func, sdrv))
3813 +sdio_bus_uevent(struct device *dev, char **envp,
3814 + int num_envp, char *buffer, int buffer_size)
3816 + struct sdio_func *func = dev_to_sdio_func(dev);
3817 + int i = 0, len = 0;
3819 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3820 + "SDIO_CLASS=%02X", func->class))
3823 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3824 + "SDIO_ID=%04X:%04X", func->vendor, func->device))
3827 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3828 + "MODALIAS=sdio:c%02Xv%04Xd%04X",
3829 + func->class, func->vendor, func->device))
3835 +static int sdio_bus_probe(struct device *dev)
3837 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3838 + struct sdio_func *func = dev_to_sdio_func(dev);
3839 + const struct sdio_device_id *id;
3842 + id = sdio_match_device(func, drv);
3846 + /* Set the default block size so the driver is sure it's something
3848 + sdio_claim_host(func);
3849 + ret = sdio_set_block_size(func, 0);
3850 + sdio_release_host(func);
3854 + return drv->probe(func, id);
3857 +static int sdio_bus_remove(struct device *dev)
3859 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3860 + struct sdio_func *func = dev_to_sdio_func(dev);
3862 + drv->remove(func);
3864 + if (func->irq_handler) {
3865 + printk(KERN_WARNING "WARNING: driver %s did not remove "
3866 + "its interrupt handler!\n", drv->name);
3867 + sdio_claim_host(func);
3868 + sdio_release_irq(func);
3869 + sdio_release_host(func);
3875 +static struct bus_type sdio_bus_type = {
3877 + .dev_attrs = sdio_dev_attrs,
3878 + .match = sdio_bus_match,
3879 + .uevent = sdio_bus_uevent,
3880 + .probe = sdio_bus_probe,
3881 + .remove = sdio_bus_remove,
3884 +int sdio_register_bus(void)
3886 + return bus_register(&sdio_bus_type);
3889 +void sdio_unregister_bus(void)
3891 + bus_unregister(&sdio_bus_type);
3895 + * sdio_register_driver - register a function driver
3896 + * @drv: SDIO function driver
3898 +int sdio_register_driver(struct sdio_driver *drv)
3900 + drv->drv.name = drv->name;
3901 + drv->drv.bus = &sdio_bus_type;
3902 + return driver_register(&drv->drv);
3904 +EXPORT_SYMBOL_GPL(sdio_register_driver);
3907 + * sdio_unregister_driver - unregister a function driver
3908 + * @drv: SDIO function driver
3910 +void sdio_unregister_driver(struct sdio_driver *drv)
3912 + drv->drv.bus = &sdio_bus_type;
3913 + driver_unregister(&drv->drv);
3915 +EXPORT_SYMBOL_GPL(sdio_unregister_driver);
3917 +static void sdio_release_func(struct device *dev)
3919 + struct sdio_func *func = dev_to_sdio_func(dev);
3921 + sdio_free_func_cis(func);
3924 + kfree(func->info);
3930 + * Allocate and initialise a new SDIO function structure.
3932 +struct sdio_func *sdio_alloc_func(struct mmc_card *card)
3934 + struct sdio_func *func;
3936 + func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
3938 + return ERR_PTR(-ENOMEM);
3940 + func->card = card;
3942 + device_initialize(&func->dev);
3944 + func->dev.parent = &card->dev;
3945 + func->dev.bus = &sdio_bus_type;
3946 + func->dev.release = sdio_release_func;
3952 + * Register a new SDIO function with the driver model.
3954 +int sdio_add_func(struct sdio_func *func)
3958 + snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
3959 + "%s:%d", mmc_card_id(func->card), func->num);
3961 + ret = device_add(&func->dev);
3963 + sdio_func_set_present(func);
3969 + * Unregister a SDIO function with the driver model, and
3970 + * (eventually) free it.
3972 +void sdio_remove_func(struct sdio_func *func)
3974 + if (sdio_func_present(func))
3975 + device_del(&func->dev);
3977 + put_device(&func->dev);
3981 +++ b/drivers/mmc/core/sdio_bus.h
3984 + * linux/drivers/mmc/core/sdio_bus.h
3986 + * Copyright 2007 Pierre Ossman
3988 + * This program is free software; you can redistribute it and/or modify
3989 + * it under the terms of the GNU General Public License as published by
3990 + * the Free Software Foundation; either version 2 of the License, or (at
3991 + * your option) any later version.
3993 +#ifndef _MMC_CORE_SDIO_BUS_H
3994 +#define _MMC_CORE_SDIO_BUS_H
3996 +struct sdio_func *sdio_alloc_func(struct mmc_card *card);
3997 +int sdio_add_func(struct sdio_func *func);
3998 +void sdio_remove_func(struct sdio_func *func);
4000 +int sdio_register_bus(void);
4001 +void sdio_unregister_bus(void);
4006 +++ b/drivers/mmc/core/sdio_cis.c
4009 + * linux/drivers/mmc/core/sdio_cis.c
4011 + * Author: Nicolas Pitre
4012 + * Created: June 11, 2007
4013 + * Copyright: MontaVista Software Inc.
4015 + * Copyright 2007 Pierre Ossman
4017 + * This program is free software; you can redistribute it and/or modify
4018 + * it under the terms of the GNU General Public License as published by
4019 + * the Free Software Foundation; either version 2 of the License, or (at
4020 + * your option) any later version.
4023 +#include <linux/kernel.h>
4025 +#include <linux/mmc/host.h>
4026 +#include <linux/mmc/card.h>
4027 +#include <linux/mmc/sdio.h>
4028 +#include <linux/mmc/sdio_func.h>
4030 +#include "sdio_cis.h"
4031 +#include "sdio_ops.h"
4033 +static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
4034 + const unsigned char *buf, unsigned size)
4036 + unsigned i, nr_strings;
4037 + char **buffer, *string;
4043 + for (i = 0; i < size; i++) {
4044 + if (buf[i] == 0xff)
4050 + if (buf[i-1] != '\0') {
4051 + printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
4057 + buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
4061 + string = (char*)(buffer + nr_strings);
4063 + for (i = 0; i < nr_strings; i++) {
4064 + buffer[i] = string;
4065 + strcpy(string, buf);
4066 + string += strlen(string) + 1;
4067 + buf += strlen(buf) + 1;
4071 + func->num_info = nr_strings;
4072 + func->info = (const char**)buffer;
4074 + card->num_info = nr_strings;
4075 + card->info = (const char**)buffer;
4081 +static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
4082 + const unsigned char *buf, unsigned size)
4084 + unsigned int vendor, device;
4087 + vendor = buf[0] | (buf[1] << 8);
4090 + device = buf[2] | (buf[3] << 8);
4093 + func->vendor = vendor;
4094 + func->device = device;
4096 + card->cis.vendor = vendor;
4097 + card->cis.device = device;
4103 +static const unsigned char speed_val[16] =
4104 + { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
4105 +static const unsigned int speed_unit[8] =
4106 + { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
4108 +static int cistpl_funce_common(struct mmc_card *card,
4109 + const unsigned char *buf, unsigned size)
4111 + if (size < 0x04 || buf[0] != 0)
4114 + /* TPLFE_FN0_BLK_SIZE */
4115 + card->cis.blksize = buf[1] | (buf[2] << 8);
4117 + /* TPLFE_MAX_TRAN_SPEED */
4118 + card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
4119 + speed_unit[buf[3] & 7];
4124 +static int cistpl_funce_func(struct sdio_func *func,
4125 + const unsigned char *buf, unsigned size)
4128 + unsigned min_size;
4130 + vsn = func->card->cccr.sdio_vsn;
4131 + min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
4133 + if (size < min_size || buf[0] != 1)
4136 + /* TPLFE_MAX_BLK_SIZE */
4137 + func->max_blksize = buf[12] | (buf[13] << 8);
4142 +static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
4143 + const unsigned char *buf, unsigned size)
4148 + * There should be two versions of the CISTPL_FUNCE tuple,
4149 + * one for the common CIS (function 0) and a version used by
4150 + * the individual function's CIS (1-7). Yet, the later has a
4151 + * different length depending on the SDIO spec version.
4154 + ret = cistpl_funce_func(func, buf, size);
4156 + ret = cistpl_funce_common(card, buf, size);
4159 + printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
4160 + "type %u\n", mmc_hostname(card->host), size, buf[0]);
4167 +typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
4168 + const unsigned char *, unsigned);
4171 + unsigned char code;
4172 + unsigned char min_size;
4173 + tpl_parse_t *parse;
4176 +static const struct cis_tpl cis_tpl_list[] = {
4177 + { 0x15, 3, cistpl_vers_1 },
4178 + { 0x20, 4, cistpl_manfid },
4179 + { 0x21, 2, /* cistpl_funcid */ },
4180 + { 0x22, 0, cistpl_funce },
4183 +static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
4186 + struct sdio_func_tuple *this, **prev;
4187 + unsigned i, ptr = 0;
4190 + * Note that this works for the common CIS (function number 0) as
4191 + * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
4192 + * have the same offset.
4194 + for (i = 0; i < 3; i++) {
4195 + unsigned char x, fn;
4202 + ret = mmc_io_rw_direct(card, 0, 0,
4203 + SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
4206 + ptr |= x << (i * 8);
4210 + prev = &func->tuples;
4212 + prev = &card->tuples;
4217 + unsigned char tpl_code, tpl_link;
4219 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
4223 + /* 0xff means we're done */
4224 + if (tpl_code == 0xff)
4227 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
4231 + this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
4235 + for (i = 0; i < tpl_link; i++) {
4236 + ret = mmc_io_rw_direct(card, 0, 0,
4237 + ptr + i, 0, &this->data[i]);
4246 + for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
4247 + if (cis_tpl_list[i].code == tpl_code)
4249 + if (i >= ARRAY_SIZE(cis_tpl_list)) {
4250 + /* this tuple is unknown to the core */
4251 + this->next = NULL;
4252 + this->code = tpl_code;
4253 + this->size = tpl_link;
4255 + prev = &this->next;
4257 + "%s: queuing CIS tuple 0x%02x length %u\n",
4258 + mmc_hostname(card->host), tpl_code, tpl_link);
4260 + const struct cis_tpl *tpl = cis_tpl_list + i;
4261 + if (tpl_link < tpl->min_size) {
4263 + "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
4264 + mmc_hostname(card->host),
4265 + tpl_code, tpl_link, tpl->min_size);
4267 + } else if (tpl->parse) {
4268 + ret = tpl->parse(card, func,
4269 + this->data, tpl_link);
4278 + * Link in all unknown tuples found in the common CIS so that
4279 + * drivers don't have to go digging in two places.
4282 + *prev = card->tuples;
4287 +int sdio_read_common_cis(struct mmc_card *card)
4289 + return sdio_read_cis(card, NULL);
4292 +void sdio_free_common_cis(struct mmc_card *card)
4294 + struct sdio_func_tuple *tuple, *victim;
4296 + tuple = card->tuples;
4300 + tuple = tuple->next;
4304 + card->tuples = NULL;
4307 +int sdio_read_func_cis(struct sdio_func *func)
4311 + ret = sdio_read_cis(func->card, func);
4316 + * Since we've linked to tuples in the card structure,
4317 + * we must make sure we have a reference to it.
4319 + get_device(&func->card->dev);
4322 + * Vendor/device id is optional for function CIS, so
4323 + * copy it from the card structure as needed.
4325 + if (func->vendor == 0) {
4326 + func->vendor = func->card->cis.vendor;
4327 + func->device = func->card->cis.device;
4333 +void sdio_free_func_cis(struct sdio_func *func)
4335 + struct sdio_func_tuple *tuple, *victim;
4337 + tuple = func->tuples;
4339 + while (tuple && tuple != func->card->tuples) {
4341 + tuple = tuple->next;
4345 + func->tuples = NULL;
4348 + * We have now removed the link to the tuples in the
4349 + * card structure, so remove the reference.
4351 + put_device(&func->card->dev);
4355 +++ b/drivers/mmc/core/sdio_cis.h
4358 + * linux/drivers/mmc/core/sdio_cis.h
4360 + * Author: Nicolas Pitre
4361 + * Created: June 11, 2007
4362 + * Copyright: MontaVista Software Inc.
4364 + * This program is free software; you can redistribute it and/or modify
4365 + * it under the terms of the GNU General Public License as published by
4366 + * the Free Software Foundation; either version 2 of the License, or (at
4367 + * your option) any later version.
4370 +#ifndef _MMC_SDIO_CIS_H
4371 +#define _MMC_SDIO_CIS_H
4373 +int sdio_read_common_cis(struct mmc_card *card);
4374 +void sdio_free_common_cis(struct mmc_card *card);
4376 +int sdio_read_func_cis(struct sdio_func *func);
4377 +void sdio_free_func_cis(struct sdio_func *func);
4381 +++ b/drivers/mmc/core/sdio_io.c
4384 + * linux/drivers/mmc/core/sdio_io.c
4386 + * Copyright 2007 Pierre Ossman
4388 + * This program is free software; you can redistribute it and/or modify
4389 + * it under the terms of the GNU General Public License as published by
4390 + * the Free Software Foundation; either version 2 of the License, or (at
4391 + * your option) any later version.
4394 +#include <linux/mmc/host.h>
4395 +#include <linux/mmc/card.h>
4396 +#include <linux/mmc/sdio.h>
4397 +#include <linux/mmc/sdio_func.h>
4399 +#include "sdio_ops.h"
4402 + * sdio_claim_host - exclusively claim a bus for a certain SDIO function
4403 + * @func: SDIO function that will be accessed
4405 + * Claim a bus for a set of operations. The SDIO function given
4406 + * is used to figure out which bus is relevant.
4408 +void sdio_claim_host(struct sdio_func *func)
4411 + BUG_ON(!func->card);
4413 + mmc_claim_host(func->card->host);
4415 +EXPORT_SYMBOL_GPL(sdio_claim_host);
4418 + * sdio_release_host - release a bus for a certain SDIO function
4419 + * @func: SDIO function that was accessed
4421 + * Release a bus, allowing others to claim the bus for their
4424 +void sdio_release_host(struct sdio_func *func)
4427 + BUG_ON(!func->card);
4429 + mmc_release_host(func->card->host);
4431 +EXPORT_SYMBOL_GPL(sdio_release_host);
4434 + * sdio_enable_func - enables a SDIO function for usage
4435 + * @func: SDIO function to enable
4437 + * Powers up and activates a SDIO function so that register
4438 + * access is possible.
4440 +int sdio_enable_func(struct sdio_func *func)
4443 + unsigned char reg;
4444 + unsigned long timeout;
4447 + BUG_ON(!func->card);
4449 + pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
4451 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
4455 + reg |= 1 << func->num;
4457 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4462 + * FIXME: This should timeout based on information in the CIS,
4463 + * but we don't have card to parse that yet.
4465 + timeout = jiffies + HZ;
4468 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®);
4471 + if (reg & (1 << func->num))
4474 + if (time_after(jiffies, timeout))
4478 + pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
4483 + pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
4486 +EXPORT_SYMBOL_GPL(sdio_enable_func);
4489 + * sdio_disable_func - disable a SDIO function
4490 + * @func: SDIO function to disable
4492 + * Powers down and deactivates a SDIO function. Register access
4493 + * to this function will fail until the function is reenabled.
4495 +int sdio_disable_func(struct sdio_func *func)
4498 + unsigned char reg;
4501 + BUG_ON(!func->card);
4503 + pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
4505 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
4509 + reg &= ~(1 << func->num);
4511 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4515 + pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
4520 + pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
4523 +EXPORT_SYMBOL_GPL(sdio_disable_func);
4526 + * sdio_set_block_size - set the block size of an SDIO function
4527 + * @func: SDIO function to change
4528 + * @blksz: new block size or 0 to use the default.
4530 + * The default block size is the largest supported by both the function
4531 + * and the host, with a maximum of 512 to ensure that arbitrarily sized
4532 + * data transfer use the optimal (least) number of commands.
4534 + * A driver may call this to override the default block size set by the
4535 + * core. This can be used to set a block size greater than the maximum
4536 + * that reported by the card; it is the driver's responsibility to ensure
4537 + * it uses a value that the card supports.
4539 + * Returns 0 on success, -EINVAL if the host does not support the
4540 + * requested block size, or -EIO (etc.) if one of the resultant FBR block
4541 + * size register writes failed.
4544 +int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
4548 + if (blksz > func->card->host->max_blk_size)
4553 + func->max_blksize,
4554 + func->card->host->max_blk_size),
4558 + ret = mmc_io_rw_direct(func->card, 1, 0,
4559 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
4560 + blksz & 0xff, NULL);
4563 + ret = mmc_io_rw_direct(func->card, 1, 0,
4564 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
4565 + (blksz >> 8) & 0xff, NULL);
4568 + func->cur_blksize = blksz;
4572 +EXPORT_SYMBOL_GPL(sdio_set_block_size);
4574 +/* Split an arbitrarily sized data transfer into several
4575 + * IO_RW_EXTENDED commands. */
4576 +static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
4577 + unsigned addr, int incr_addr, u8 *buf, unsigned size)
4579 + unsigned remainder = size;
4580 + unsigned max_blocks;
4583 + /* Do the bulk of the transfer using block mode (if supported). */
4584 + if (func->card->cccr.multi_block) {
4585 + /* Blocks per command is limited by host count, host transfer
4586 + * size (we only use a single sg entry) and the maximum for
4587 + * IO_RW_EXTENDED of 511 blocks. */
4588 + max_blocks = min(min(
4589 + func->card->host->max_blk_count,
4590 + func->card->host->max_seg_size / func->cur_blksize),
4593 + while (remainder > func->cur_blksize) {
4596 + blocks = remainder / func->cur_blksize;
4597 + if (blocks > max_blocks)
4598 + blocks = max_blocks;
4599 + size = blocks * func->cur_blksize;
4601 + ret = mmc_io_rw_extended(func->card, write,
4602 + func->num, addr, incr_addr, buf,
4603 + blocks, func->cur_blksize);
4607 + remainder -= size;
4614 + /* Write the remainder using byte mode. */
4615 + while (remainder > 0) {
4617 + if (size > func->cur_blksize)
4618 + size = func->cur_blksize;
4620 + size = 512; /* maximum size for byte mode */
4622 + ret = mmc_io_rw_extended(func->card, write, func->num, addr,
4623 + incr_addr, buf, 1, size);
4627 + remainder -= size;
4636 + * sdio_readb - read a single byte from a SDIO function
4637 + * @func: SDIO function to access
4638 + * @addr: address to read
4639 + * @err_ret: optional status value from transfer
4641 + * Reads a single byte from the address space of a given SDIO
4642 + * function. If there is a problem reading the address, 0xff
4643 + * is returned and @err_ret will contain the error code.
4645 +unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
4649 + unsigned char val;
4656 + ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
4665 +EXPORT_SYMBOL_GPL(sdio_readb);
4668 + * sdio_writeb - write a single byte to a SDIO function
4669 + * @func: SDIO function to access
4670 + * @b: byte to write
4671 + * @addr: address to write to
4672 + * @err_ret: optional status value from transfer
4674 + * Writes a single byte to the address space of a given SDIO
4675 + * function. @err_ret will contain the status of the actual
4678 +void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4685 + ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
4689 +EXPORT_SYMBOL_GPL(sdio_writeb);
4692 + * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
4693 + * @func: SDIO function to access
4694 + * @dst: buffer to store the data
4695 + * @addr: address to begin reading from
4696 + * @count: number of bytes to read
4698 + * Reads from the address space of a given SDIO function. Return
4699 + * value indicates if the transfer succeeded or not.
4701 +int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
4702 + unsigned int addr, int count)
4704 + return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
4706 +EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
4709 + * sdio_memcpy_toio - write a chunk of memory to a SDIO function
4710 + * @func: SDIO function to access
4711 + * @addr: address to start writing to
4712 + * @src: buffer that contains the data to write
4713 + * @count: number of bytes to write
4715 + * Writes to the address space of a given SDIO function. Return
4716 + * value indicates if the transfer succeeded or not.
4718 +int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
4719 + void *src, int count)
4721 + return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
4723 +EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
4726 + * sdio_readsb - read from a FIFO on a SDIO function
4727 + * @func: SDIO function to access
4728 + * @dst: buffer to store the data
4729 + * @addr: address of (single byte) FIFO
4730 + * @count: number of bytes to read
4732 + * Reads from the specified FIFO of a given SDIO function. Return
4733 + * value indicates if the transfer succeeded or not.
4735 +int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
4738 + return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
4741 +EXPORT_SYMBOL_GPL(sdio_readsb);
4744 + * sdio_writesb - write to a FIFO of a SDIO function
4745 + * @func: SDIO function to access
4746 + * @addr: address of (single byte) FIFO
4747 + * @src: buffer that contains the data to write
4748 + * @count: number of bytes to write
4750 + * Writes to the specified FIFO of a given SDIO function. Return
4751 + * value indicates if the transfer succeeded or not.
4753 +int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
4756 + return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
4758 +EXPORT_SYMBOL_GPL(sdio_writesb);
4761 + * sdio_readw - read a 16 bit integer from a SDIO function
4762 + * @func: SDIO function to access
4763 + * @addr: address to read
4764 + * @err_ret: optional status value from transfer
4766 + * Reads a 16 bit integer from the address space of a given SDIO
4767 + * function. If there is a problem reading the address, 0xffff
4768 + * is returned and @err_ret will contain the error code.
4770 +unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
4778 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
4785 + return le16_to_cpu(*(u16*)func->tmpbuf);
4787 +EXPORT_SYMBOL_GPL(sdio_readw);
4790 + * sdio_writew - write a 16 bit integer to a SDIO function
4791 + * @func: SDIO function to access
4792 + * @b: integer to write
4793 + * @addr: address to write to
4794 + * @err_ret: optional status value from transfer
4796 + * Writes a 16 bit integer to the address space of a given SDIO
4797 + * function. @err_ret will contain the status of the actual
4800 +void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
4805 + *(u16*)func->tmpbuf = cpu_to_le16(b);
4807 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
4811 +EXPORT_SYMBOL_GPL(sdio_writew);
4814 + * sdio_readl - read a 32 bit integer from a SDIO function
4815 + * @func: SDIO function to access
4816 + * @addr: address to read
4817 + * @err_ret: optional status value from transfer
4819 + * Reads a 32 bit integer from the address space of a given SDIO
4820 + * function. If there is a problem reading the address,
4821 + * 0xffffffff is returned and @err_ret will contain the error
4824 +unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
4832 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
4836 + return 0xFFFFFFFF;
4839 + return le32_to_cpu(*(u32*)func->tmpbuf);
4841 +EXPORT_SYMBOL_GPL(sdio_readl);
4844 + * sdio_writel - write a 32 bit integer to a SDIO function
4845 + * @func: SDIO function to access
4846 + * @b: integer to write
4847 + * @addr: address to write to
4848 + * @err_ret: optional status value from transfer
4850 + * Writes a 32 bit integer to the address space of a given SDIO
4851 + * function. @err_ret will contain the status of the actual
4854 +void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
4859 + *(u32*)func->tmpbuf = cpu_to_le32(b);
4861 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
4865 +EXPORT_SYMBOL_GPL(sdio_writel);
4868 + * sdio_f0_readb - read a single byte from SDIO function 0
4869 + * @func: an SDIO function of the card
4870 + * @addr: address to read
4871 + * @err_ret: optional status value from transfer
4873 + * Reads a single byte from the address space of SDIO function 0.
4874 + * If there is a problem reading the address, 0xff is returned
4875 + * and @err_ret will contain the error code.
4877 +unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
4881 + unsigned char val;
4888 + ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
4897 +EXPORT_SYMBOL_GPL(sdio_f0_readb);
4900 + * sdio_f0_writeb - write a single byte to SDIO function 0
4901 + * @func: an SDIO function of the card
4902 + * @b: byte to write
4903 + * @addr: address to write to
4904 + * @err_ret: optional status value from transfer
4906 + * Writes a single byte to the address space of SDIO function 0.
4907 + * @err_ret will contain the status of the actual transfer.
4909 + * Only writes to the vendor specific CCCR registers (0xF0 -
4910 + * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
4911 + * writes outside this range.
4913 +void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4920 + if (addr < 0xF0 || addr > 0xFF) {
4922 + *err_ret = -EINVAL;
4926 + ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
4930 +EXPORT_SYMBOL_GPL(sdio_f0_writeb);
4932 +++ b/drivers/mmc/core/sdio_irq.c
4935 + * linux/drivers/mmc/core/sdio_irq.c
4937 + * Author: Nicolas Pitre
4938 + * Created: June 18, 2007
4939 + * Copyright: MontaVista Software Inc.
4941 + * This program is free software; you can redistribute it and/or modify
4942 + * it under the terms of the GNU General Public License as published by
4943 + * the Free Software Foundation; either version 2 of the License, or (at
4944 + * your option) any later version.
4947 +#include <linux/kernel.h>
4948 +#include <linux/sched.h>
4949 +#include <linux/kthread.h>
4950 +#include <linux/wait.h>
4951 +#include <linux/delay.h>
4953 +#include <linux/mmc/core.h>
4954 +#include <linux/mmc/host.h>
4955 +#include <linux/mmc/card.h>
4956 +#include <linux/mmc/sdio.h>
4957 +#include <linux/mmc/sdio_func.h>
4959 +#include "sdio_ops.h"
4961 +static int process_sdio_pending_irqs(struct mmc_card *card)
4963 + int i, ret, count;
4964 + unsigned char pending;
4966 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
4968 + printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
4969 + mmc_card_id(card), ret);
4974 + for (i = 1; i <= 7; i++) {
4975 + if (pending & (1 << i)) {
4976 + struct sdio_func *func = card->sdio_func[i - 1];
4978 + printk(KERN_WARNING "%s: pending IRQ for "
4979 + "non-existant function\n",
4980 + mmc_card_id(card));
4982 + } else if (func->irq_handler) {
4983 + func->irq_handler(func);
4986 + printk(KERN_WARNING "%s: pending IRQ with no handler\n",
4987 + sdio_func_id(func));
4999 +static int sdio_irq_thread(void *_host)
5001 + struct mmc_host *host = _host;
5002 + struct sched_param param = { .sched_priority = 1 };
5003 + unsigned long period, idle_period;
5006 + sched_setscheduler(current, SCHED_FIFO, ¶m);
5009 + * We want to allow for SDIO cards to work even on non SDIO
5010 + * aware hosts. One thing that non SDIO host cannot do is
5011 + * asynchronous notification of pending SDIO card interrupts
5012 + * hence we poll for them in that case.
5014 + idle_period = msecs_to_jiffies(10);
5015 + period = (host->caps & MMC_CAP_SDIO_IRQ) ?
5016 + MAX_SCHEDULE_TIMEOUT : idle_period;
5018 + pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
5019 + mmc_hostname(host), period);
5023 + * We claim the host here on drivers behalf for a couple
5026 + * 1) it is already needed to retrieve the CCCR_INTx;
5027 + * 2) we want the driver(s) to clear the IRQ condition ASAP;
5028 + * 3) we need to control the abort condition locally.
5030 + * Just like traditional hard IRQ handlers, we expect SDIO
5031 + * IRQ handlers to be quick and to the point, so that the
5032 + * holding of the host lock does not cover too much work
5033 + * that doesn't require that lock to be held.
5035 + ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
5038 + ret = process_sdio_pending_irqs(host->card);
5039 + mmc_release_host(host);
5042 + * Give other threads a chance to run in the presence of
5043 + * errors. FIXME: determine if due to card removal and
5044 + * possibly exit this thread if so.
5050 + * Adaptive polling frequency based on the assumption
5051 + * that an interrupt will be closely followed by more.
5052 + * This has a substantial benefit for network devices.
5054 + if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
5059 + if (period > idle_period)
5060 + period = idle_period;
5064 + set_task_state(current, TASK_INTERRUPTIBLE);
5065 + if (host->caps & MMC_CAP_SDIO_IRQ)
5066 + host->ops->enable_sdio_irq(host, 1);
5067 + if (!kthread_should_stop())
5068 + schedule_timeout(period);
5069 + set_task_state(current, TASK_RUNNING);
5070 + } while (!kthread_should_stop());
5072 + if (host->caps & MMC_CAP_SDIO_IRQ)
5073 + host->ops->enable_sdio_irq(host, 0);
5075 + pr_debug("%s: IRQ thread exiting with code %d\n",
5076 + mmc_hostname(host), ret);
5081 +static int sdio_card_irq_get(struct mmc_card *card)
5083 + struct mmc_host *host = card->host;
5085 + WARN_ON(!host->claimed);
5087 + if (!host->sdio_irqs++) {
5088 + atomic_set(&host->sdio_irq_thread_abort, 0);
5089 + host->sdio_irq_thread =
5090 + kthread_run(sdio_irq_thread, host, "ksdiorqd");
5091 + if (IS_ERR(host->sdio_irq_thread)) {
5092 + int err = PTR_ERR(host->sdio_irq_thread);
5093 + host->sdio_irqs--;
5101 +static int sdio_card_irq_put(struct mmc_card *card)
5103 + struct mmc_host *host = card->host;
5105 + WARN_ON(!host->claimed);
5106 + BUG_ON(host->sdio_irqs < 1);
5108 + if (!--host->sdio_irqs) {
5109 + atomic_set(&host->sdio_irq_thread_abort, 1);
5110 + kthread_stop(host->sdio_irq_thread);
5117 + * sdio_claim_irq - claim the IRQ for a SDIO function
5118 + * @func: SDIO function
5119 + * @handler: IRQ handler callback
5121 + * Claim and activate the IRQ for the given SDIO function. The provided
5122 + * handler will be called when that IRQ is asserted. The host is always
5123 + * claimed already when the handler is called so the handler must not
5124 + * call sdio_claim_host() nor sdio_release_host().
5126 +int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
5129 + unsigned char reg;
5132 + BUG_ON(!func->card);
5134 + pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
5136 + if (func->irq_handler) {
5137 + pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
5141 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
5145 + reg |= 1 << func->num;
5147 + reg |= 1; /* Master interrupt enable */
5149 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5153 + func->irq_handler = handler;
5154 + ret = sdio_card_irq_get(func->card);
5156 + func->irq_handler = NULL;
5160 +EXPORT_SYMBOL_GPL(sdio_claim_irq);
5163 + * sdio_release_irq - release the IRQ for a SDIO function
5164 + * @func: SDIO function
5166 + * Disable and release the IRQ for the given SDIO function.
5168 +int sdio_release_irq(struct sdio_func *func)
5171 + unsigned char reg;
5174 + BUG_ON(!func->card);
5176 + pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
5178 + if (func->irq_handler) {
5179 + func->irq_handler = NULL;
5180 + sdio_card_irq_put(func->card);
5183 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
5187 + reg &= ~(1 << func->num);
5189 + /* Disable master interrupt with the last function interrupt */
5190 + if (!(reg & 0xFE))
5193 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5199 +EXPORT_SYMBOL_GPL(sdio_release_irq);
5202 +++ b/drivers/mmc/core/sdio_ops.c
5205 + * linux/drivers/mmc/sdio_ops.c
5207 + * Copyright 2006-2007 Pierre Ossman
5209 + * This program is free software; you can redistribute it and/or modify
5210 + * it under the terms of the GNU General Public License as published by
5211 + * the Free Software Foundation; either version 2 of the License, or (at
5212 + * your option) any later version.
5215 +#include <linux/scatterlist.h>
5217 +#include <linux/mmc/host.h>
5218 +#include <linux/mmc/card.h>
5219 +#include <linux/mmc/mmc.h>
5220 +#include <linux/mmc/sdio.h>
5224 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
5226 + struct mmc_command cmd;
5231 + memset(&cmd, 0, sizeof(struct mmc_command));
5233 + cmd.opcode = SD_IO_SEND_OP_COND;
5235 + cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
5237 + for (i = 100; i; i--) {
5238 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
5242 + /* if we're just probing, do a single pass */
5246 + /* otherwise wait until reset completes */
5247 + if (mmc_host_is_spi(host)) {
5249 + * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
5250 + * an initialized card under SPI, but some cards
5251 + * (Marvell's) only behave when looking at this
5254 + if (cmd.resp[1] & MMC_CARD_BUSY)
5257 + if (cmd.resp[0] & MMC_CARD_BUSY)
5267 + *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
5272 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5273 + unsigned addr, u8 in, u8* out)
5275 + struct mmc_command cmd;
5281 + memset(&cmd, 0, sizeof(struct mmc_command));
5283 + cmd.opcode = SD_IO_RW_DIRECT;
5284 + cmd.arg = write ? 0x80000000 : 0x00000000;
5285 + cmd.arg |= fn << 28;
5286 + cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
5287 + cmd.arg |= addr << 9;
5289 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
5291 + err = mmc_wait_for_cmd(card->host, &cmd, 0);
5295 + if (mmc_host_is_spi(card->host)) {
5296 + /* host driver already reported errors */
5298 + if (cmd.resp[0] & R5_ERROR)
5300 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5302 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5307 + if (mmc_host_is_spi(card->host))
5308 + *out = (cmd.resp[0] >> 8) & 0xFF;
5310 + *out = cmd.resp[0] & 0xFF;
5316 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5317 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
5319 + struct mmc_request mrq;
5320 + struct mmc_command cmd;
5321 + struct mmc_data data;
5322 + struct scatterlist sg;
5326 + BUG_ON(blocks == 1 && blksz > 512);
5327 + WARN_ON(blocks == 0);
5328 + WARN_ON(blksz == 0);
5330 + memset(&mrq, 0, sizeof(struct mmc_request));
5331 + memset(&cmd, 0, sizeof(struct mmc_command));
5332 + memset(&data, 0, sizeof(struct mmc_data));
5337 + cmd.opcode = SD_IO_RW_EXTENDED;
5338 + cmd.arg = write ? 0x80000000 : 0x00000000;
5339 + cmd.arg |= fn << 28;
5340 + cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
5341 + cmd.arg |= addr << 9;
5342 + if (blocks == 1 && blksz <= 512)
5343 + cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
5345 + cmd.arg |= 0x08000000 | blocks; /* block mode */
5346 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
5348 + data.blksz = blksz;
5349 + data.blocks = blocks;
5350 + data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
5354 + sg_init_one(&sg, buf, blksz * blocks);
5356 + mmc_set_data_timeout(&data, card);
5358 + mmc_wait_for_req(card->host, &mrq);
5363 + return data.error;
5365 + if (mmc_host_is_spi(card->host)) {
5366 + /* host driver already reported errors */
5368 + if (cmd.resp[0] & R5_ERROR)
5370 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5372 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5380 +++ b/drivers/mmc/core/sdio_ops.h
5383 + * linux/drivers/mmc/sdio_ops.c
5385 + * Copyright 2006-2007 Pierre Ossman
5387 + * This program is free software; you can redistribute it and/or modify
5388 + * it under the terms of the GNU General Public License as published by
5389 + * the Free Software Foundation; either version 2 of the License, or (at
5390 + * your option) any later version.
5393 +#ifndef _MMC_SDIO_OPS_H
5394 +#define _MMC_SDIO_OPS_H
5396 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
5397 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5398 + unsigned addr, u8 in, u8* out);
5399 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5400 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz);
5404 --- a/include/linux/mmc/card.h
5405 +++ b/include/linux/mmc/card.h
5406 @@ -55,7 +55,28 @@ struct sd_switch_caps {
5407 unsigned int hs_max_dtr;
5411 + unsigned int sdio_vsn;
5412 + unsigned int sd_vsn;
5413 + unsigned int multi_block:1,
5421 + unsigned short vendor;
5422 + unsigned short device;
5423 + unsigned short blksize;
5424 + unsigned int max_dtr;
5429 +struct sdio_func_tuple;
5431 +#define SDIO_MAX_FUNCS 7
5435 @@ -67,11 +88,13 @@ struct mmc_card {
5436 unsigned int type; /* card type */
5437 #define MMC_TYPE_MMC 0 /* MMC card */
5438 #define MMC_TYPE_SD 1 /* SD card */
5439 +#define MMC_TYPE_SDIO 2 /* SDIO card */
5440 unsigned int state; /* (our) card state */
5441 #define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
5442 #define MMC_STATE_READONLY (1<<1) /* card is read-only */
5443 #define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
5444 #define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
5446 u32 raw_cid[4]; /* raw card CID */
5447 u32 raw_csd[4]; /* raw card CSD */
5448 u32 raw_scr[2]; /* raw card SCR */
5449 @@ -80,10 +103,19 @@ struct mmc_card {
5450 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
5451 struct sd_scr scr; /* extra SD information */
5452 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
5454 + unsigned int sdio_funcs; /* number of SDIO functions */
5455 + struct sdio_cccr cccr; /* common card info */
5456 + struct sdio_cis cis; /* common tuple info */
5457 + struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
5458 + unsigned num_info; /* number of info strings */
5459 + const char **info; /* info strings */
5460 + struct sdio_func_tuple *tuples; /* unknown common tuples */
5463 #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
5464 #define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
5465 +#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
5467 #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
5468 #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
5469 --- a/include/linux/mmc/core.h
5470 +++ b/include/linux/mmc/core.h
5471 @@ -25,14 +25,20 @@ struct mmc_command {
5472 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
5473 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
5474 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
5475 -#define MMC_CMD_MASK (3 << 5) /* command type */
5477 +#define MMC_CMD_MASK (3 << 5) /* non-SPI command type */
5478 #define MMC_CMD_AC (0 << 5)
5479 #define MMC_CMD_ADTC (1 << 5)
5480 #define MMC_CMD_BC (2 << 5)
5481 #define MMC_CMD_BCR (3 << 5)
5483 +#define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */
5484 +#define MMC_RSP_SPI_S2 (1 << 8) /* second byte */
5485 +#define MMC_RSP_SPI_B4 (1 << 9) /* four data bytes */
5486 +#define MMC_RSP_SPI_BUSY (1 << 10) /* card may send busy */
5489 - * These are the response types, and correspond to valid bit
5490 + * These are the native response types, and correspond to valid bit
5491 * patterns of the above flags. One additional valid pattern
5492 * is all zeros, which means we don't expect a response.
5494 @@ -41,12 +47,30 @@ struct mmc_command {
5495 #define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
5496 #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
5497 #define MMC_RSP_R3 (MMC_RSP_PRESENT)
5498 +#define MMC_RSP_R4 (MMC_RSP_PRESENT)
5499 +#define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5500 #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5501 #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5503 #define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
5506 + * These are the SPI response types for MMC, SD, and SDIO cards.
5507 + * Commands return R1, with maybe more info. Zero is an error type;
5508 + * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
5510 +#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
5511 +#define MMC_RSP_SPI_R1B (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
5512 +#define MMC_RSP_SPI_R2 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5513 +#define MMC_RSP_SPI_R3 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5514 +#define MMC_RSP_SPI_R4 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5515 +#define MMC_RSP_SPI_R5 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5516 +#define MMC_RSP_SPI_R7 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5518 +#define mmc_spi_resp_type(cmd) ((cmd)->flags & \
5519 + (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
5522 * These are the command types.
5524 #define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK)
5525 @@ -54,12 +78,19 @@ struct mmc_command {
5526 unsigned int retries; /* max number of retries */
5527 unsigned int error; /* command error */
5529 -#define MMC_ERR_NONE 0
5530 -#define MMC_ERR_TIMEOUT 1
5531 -#define MMC_ERR_BADCRC 2
5532 -#define MMC_ERR_FIFO 3
5533 -#define MMC_ERR_FAILED 4
5534 -#define MMC_ERR_INVALID 5
5536 + * Standard errno values are used for errors, but some have specific
5537 + * meaning in the MMC layer:
5539 + * ETIMEDOUT Card took too long to respond
5540 + * EILSEQ Basic format problem with the received or sent data
5541 + * (e.g. CRC check failed, incorrect opcode in response
5543 + * EINVAL Request cannot be performed because of restrictions
5544 + * in hardware and/or the driver
5545 + * ENOMEDIUM Host can determine that the slot is empty and is
5546 + * actively failing requests
5549 struct mmc_data *data; /* data segment associated with cmd */
5550 struct mmc_request *mrq; /* associated request */
5551 @@ -76,7 +107,6 @@ struct mmc_data {
5552 #define MMC_DATA_WRITE (1 << 8)
5553 #define MMC_DATA_READ (1 << 9)
5554 #define MMC_DATA_STREAM (1 << 10)
5555 -#define MMC_DATA_MULTI (1 << 11)
5557 unsigned int bytes_xfered;
5559 @@ -104,9 +134,20 @@ extern int mmc_wait_for_cmd(struct mmc_h
5560 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
5561 struct mmc_command *, int);
5563 -extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int);
5564 +extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
5566 -extern void mmc_claim_host(struct mmc_host *host);
5567 +extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
5568 extern void mmc_release_host(struct mmc_host *host);
5571 + * mmc_claim_host - exclusively claim a host
5572 + * @host: mmc host to claim
5574 + * Claim a host for a set of operations.
5576 +static inline void mmc_claim_host(struct mmc_host *host)
5578 + __mmc_claim_host(host, NULL);
5582 --- a/include/linux/mmc/host.h
5583 +++ b/include/linux/mmc/host.h
5585 #ifndef LINUX_MMC_HOST_H
5586 #define LINUX_MMC_HOST_H
5588 +#include <linux/leds.h>
5590 #include <linux/mmc/core.h>
5593 @@ -51,6 +53,7 @@ struct mmc_host_ops {
5594 void (*request)(struct mmc_host *host, struct mmc_request *req);
5595 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
5596 int (*get_ro)(struct mmc_host *host);
5597 + void (*enable_sdio_irq)(struct mmc_host *host, int enable);
5601 @@ -87,9 +90,10 @@ struct mmc_host {
5603 #define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
5604 #define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */
5605 -#define MMC_CAP_BYTEBLOCK (1 << 2) /* Can do non-log2 block sizes */
5606 -#define MMC_CAP_MMC_HIGHSPEED (1 << 3) /* Can do MMC high-speed timing */
5607 -#define MMC_CAP_SD_HIGHSPEED (1 << 4) /* Can do SD high-speed timing */
5608 +#define MMC_CAP_MMC_HIGHSPEED (1 << 2) /* Can do MMC high-speed timing */
5609 +#define MMC_CAP_SD_HIGHSPEED (1 << 3) /* Can do SD high-speed timing */
5610 +#define MMC_CAP_SDIO_IRQ (1 << 4) /* Can signal pending SDIO IRQs */
5611 +#define MMC_CAP_SPI (1 << 5) /* Talks only SPI protocols */
5613 /* host specific block data */
5614 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
5615 @@ -106,23 +110,30 @@ struct mmc_host {
5616 struct mmc_ios ios; /* current io bus settings */
5617 u32 ocr; /* the current OCR setting */
5619 - unsigned int mode; /* current card mode of host */
5620 -#define MMC_MODE_MMC 0
5621 -#define MMC_MODE_SD 1
5622 + /* group bitfields together to minimize padding */
5623 + unsigned int use_spi_crc:1;
5624 + unsigned int claimed:1; /* host exclusively claimed */
5625 + unsigned int bus_dead:1; /* bus has been released */
5626 +#ifdef CONFIG_MMC_DEBUG
5627 + unsigned int removed:1; /* host is being removed */
5630 struct mmc_card *card; /* device attached to this host */
5632 wait_queue_head_t wq;
5633 - unsigned int claimed:1; /* host exclusively claimed */
5635 struct delayed_work detect;
5636 -#ifdef CONFIG_MMC_DEBUG
5637 - unsigned int removed:1; /* host is being removed */
5640 const struct mmc_bus_ops *bus_ops; /* current bus driver */
5641 unsigned int bus_refs; /* reference counter */
5642 - unsigned int bus_dead:1; /* bus has been released */
5644 + unsigned int sdio_irqs;
5645 + struct task_struct *sdio_irq_thread;
5646 + atomic_t sdio_irq_thread_abort;
5648 +#ifdef CONFIG_LEDS_TRIGGERS
5649 + struct led_trigger *led; /* activity led */
5652 unsigned long private[0] ____cacheline_aligned;
5654 @@ -137,6 +148,8 @@ static inline void *mmc_priv(struct mmc_
5655 return (void *)host->private;
5658 +#define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
5660 #define mmc_dev(x) ((x)->parent)
5661 #define mmc_classdev(x) (&(x)->class_dev)
5662 #define mmc_hostname(x) ((x)->class_dev.bus_id)
5663 @@ -147,5 +160,11 @@ extern int mmc_resume_host(struct mmc_ho
5664 extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
5665 extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
5667 +static inline void mmc_signal_sdio_irq(struct mmc_host *host)
5669 + host->ops->enable_sdio_irq(host, 0);
5670 + wake_up_process(host->sdio_irq_thread);
5675 --- a/include/linux/mmc/mmc.h
5676 +++ b/include/linux/mmc/mmc.h
5679 /* Standard MMC commands (4.1) type argument response */
5681 -#define MMC_GO_IDLE_STATE 0 /* bc */
5682 +#define MMC_GO_IDLE_STATE 0 /* bc */
5683 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
5684 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
5685 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
5687 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
5688 #define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
5689 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
5690 -#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5691 +#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5692 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
5693 +#define MMC_SPI_READ_OCR 58 /* spi spi_R3 */
5694 +#define MMC_SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
5697 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
5703 + MMC status in R1, for native mode (SPI bits are different)
5708 r : detected and set for the actual command response
5709 x : detected and set during command execution. the host must poll
5710 the card by sending status command in order to read these bits.
5712 - a : according to the card state
5713 + a : according to the card state
5714 b : always related to the previous command. Reception of
5715 a valid command will clear it (with a delay of one command)
5717 @@ -124,10 +126,33 @@
5718 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
5719 #define R1_ERASE_RESET (1 << 13) /* sr, c */
5720 #define R1_STATUS(x) (x & 0xFFFFE000)
5721 -#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5722 +#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5723 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
5724 #define R1_APP_CMD (1 << 5) /* sr, c */
5727 + * MMC/SD in SPI mode reports R1 status always, and R2 for SEND_STATUS
5728 + * R1 is the low order byte; R2 is the next highest byte, when present.
5730 +#define R1_SPI_IDLE (1 << 0)
5731 +#define R1_SPI_ERASE_RESET (1 << 1)
5732 +#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
5733 +#define R1_SPI_COM_CRC (1 << 3)
5734 +#define R1_SPI_ERASE_SEQ (1 << 4)
5735 +#define R1_SPI_ADDRESS (1 << 5)
5736 +#define R1_SPI_PARAMETER (1 << 6)
5737 +/* R1 bit 7 is always zero */
5738 +#define R2_SPI_CARD_LOCKED (1 << 8)
5739 +#define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
5740 +#define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
5741 +#define R2_SPI_ERROR (1 << 10)
5742 +#define R2_SPI_CC_ERROR (1 << 11)
5743 +#define R2_SPI_CARD_ECC_ERROR (1 << 12)
5744 +#define R2_SPI_WP_VIOLATION (1 << 13)
5745 +#define R2_SPI_ERASE_PARAM (1 << 14)
5746 +#define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
5747 +#define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
5749 /* These are unpacked versions of the actual responses */
5752 @@ -182,6 +207,7 @@ struct _mmc_csd {
5754 #define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
5755 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
5756 + /* (and for SPI, CMD58,59) */
5757 #define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
5759 #define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
5760 @@ -227,6 +253,7 @@ struct _mmc_csd {
5761 #define EXT_CSD_BUS_WIDTH 183 /* R/W */
5762 #define EXT_CSD_HS_TIMING 185 /* R/W */
5763 #define EXT_CSD_CARD_TYPE 196 /* RO */
5764 +#define EXT_CSD_REV 192 /* RO */
5765 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
5769 +++ b/include/linux/mmc/sdio.h
5772 + * include/linux/mmc/sdio.h
5774 + * Copyright 2006-2007 Pierre Ossman
5776 + * This program is free software; you can redistribute it and/or modify
5777 + * it under the terms of the GNU General Public License as published by
5778 + * the Free Software Foundation; either version 2 of the License, or (at
5779 + * your option) any later version.
5785 +/* SDIO commands type argument response */
5786 +#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
5787 +#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
5788 +#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
5791 + * SD_IO_RW_DIRECT argument format:
5794 + * [30:28] Function number
5796 + * [25:9] Register address
5801 + * SD_IO_RW_EXTENDED argument format:
5804 + * [30:28] Function number
5806 + * [26] Increment address
5807 + * [25:9] Register address
5808 + * [8:0] Byte/block count
5816 + r : detected and set for the actual command response
5817 + x : detected and set during command execution. the host must poll
5818 + the card by sending status command in order to read these bits.
5820 + a : according to the card state
5821 + b : always related to the previous command. Reception of
5822 + a valid command will clear it (with a delay of one command)
5826 +#define R5_COM_CRC_ERROR (1 << 15) /* er, b */
5827 +#define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
5828 +#define R5_ERROR (1 << 11) /* erx, c */
5829 +#define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
5830 +#define R5_OUT_OF_RANGE (1 << 8) /* er, c */
5831 +#define R5_STATUS(x) (x & 0xCB00)
5832 +#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
5835 + * Card Common Control Registers (CCCR)
5838 +#define SDIO_CCCR_CCCR 0x00
5840 +#define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
5841 +#define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
5842 +#define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
5844 +#define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
5845 +#define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
5846 +#define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
5847 +#define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
5849 +#define SDIO_CCCR_SD 0x01
5851 +#define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
5852 +#define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
5853 +#define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
5855 +#define SDIO_CCCR_IOEx 0x02
5856 +#define SDIO_CCCR_IORx 0x03
5858 +#define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
5859 +#define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
5861 +#define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
5863 +#define SDIO_CCCR_IF 0x07 /* bus interface controls */
5865 +#define SDIO_BUS_WIDTH_1BIT 0x00
5866 +#define SDIO_BUS_WIDTH_4BIT 0x02
5868 +#define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
5870 +#define SDIO_CCCR_CAPS 0x08
5872 +#define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
5873 +#define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
5874 +#define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
5875 +#define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
5876 +#define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
5877 +#define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
5878 +#define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
5879 +#define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
5881 +#define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
5883 +/* Following 4 regs are valid only if SBS is set */
5884 +#define SDIO_CCCR_SUSPEND 0x0c
5885 +#define SDIO_CCCR_SELx 0x0d
5886 +#define SDIO_CCCR_EXECx 0x0e
5887 +#define SDIO_CCCR_READYx 0x0f
5889 +#define SDIO_CCCR_BLKSIZE 0x10
5891 +#define SDIO_CCCR_POWER 0x12
5893 +#define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
5894 +#define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
5896 +#define SDIO_CCCR_SPEED 0x13
5898 +#define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
5899 +#define SDIO_SPEED_EHS 0x02 /* Enable High-Speed mode */
5902 + * Function Basic Registers (FBR)
5905 +#define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
5907 +#define SDIO_FBR_STD_IF 0x00
5909 +#define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
5910 +#define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
5912 +#define SDIO_FBR_STD_IF_EXT 0x01
5914 +#define SDIO_FBR_POWER 0x02
5916 +#define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
5917 +#define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
5919 +#define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
5922 +#define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
5924 +#define SDIO_FBR_CSA_DATA 0x0F
5926 +#define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
5931 +++ b/include/linux/mmc/sdio_func.h
5934 + * include/linux/mmc/sdio_func.h
5936 + * Copyright 2007 Pierre Ossman
5938 + * This program is free software; you can redistribute it and/or modify
5939 + * it under the terms of the GNU General Public License as published by
5940 + * the Free Software Foundation; either version 2 of the License, or (at
5941 + * your option) any later version.
5944 +#ifndef MMC_SDIO_FUNC_H
5945 +#define MMC_SDIO_FUNC_H
5947 +#include <linux/device.h>
5948 +#include <linux/mod_devicetable.h>
5953 +typedef void (sdio_irq_handler_t)(struct sdio_func *);
5956 + * SDIO function CIS tuple (unknown to the core)
5958 +struct sdio_func_tuple {
5959 + struct sdio_func_tuple *next;
5960 + unsigned char code;
5961 + unsigned char size;
5962 + unsigned char data[0];
5966 + * SDIO function devices
5969 + struct mmc_card *card; /* the card this device belongs to */
5970 + struct device dev; /* the device */
5971 + sdio_irq_handler_t *irq_handler; /* IRQ callback */
5972 + unsigned int num; /* function number */
5974 + unsigned char class; /* standard interface class */
5975 + unsigned short vendor; /* vendor id */
5976 + unsigned short device; /* device id */
5978 + unsigned max_blksize; /* maximum block size */
5979 + unsigned cur_blksize; /* current block size */
5981 + unsigned int state; /* function state */
5982 +#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
5984 + u8 tmpbuf[4]; /* DMA:able scratch buffer */
5986 + unsigned num_info; /* number of info strings */
5987 + const char **info; /* info strings */
5989 + struct sdio_func_tuple *tuples;
5992 +#define sdio_func_present(f) ((f)->state & SDIO_STATE_PRESENT)
5994 +#define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
5996 +#define sdio_func_id(f) ((f)->dev.bus_id)
5998 +#define sdio_get_drvdata(f) dev_get_drvdata(&(f)->dev)
5999 +#define sdio_set_drvdata(f,d) dev_set_drvdata(&(f)->dev, d)
6002 + * SDIO function device driver
6004 +struct sdio_driver {
6006 + const struct sdio_device_id *id_table;
6008 + int (*probe)(struct sdio_func *, const struct sdio_device_id *);
6009 + void (*remove)(struct sdio_func *);
6011 + struct device_driver drv;
6015 + * SDIO_DEVICE - macro used to describe a specific SDIO device
6016 + * @vend: the 16 bit manufacturer code
6017 + * @dev: the 16 bit function id
6019 + * This macro is used to create a struct sdio_device_id that matches a
6020 + * specific device. The class field will be set to SDIO_ANY_ID.
6022 +#define SDIO_DEVICE(vend,dev) \
6023 + .class = SDIO_ANY_ID, \
6024 + .vendor = (vend), .device = (dev)
6027 + * SDIO_DEVICE_CLASS - macro used to describe a specific SDIO device class
6028 + * @dev_class: the 8 bit standard interface code
6030 + * This macro is used to create a struct sdio_device_id that matches a
6031 + * specific standard SDIO function type. The vendor and device fields will
6032 + * be set to SDIO_ANY_ID.
6034 +#define SDIO_DEVICE_CLASS(dev_class) \
6035 + .class = (dev_class), \
6036 + .vendor = SDIO_ANY_ID, .device = SDIO_ANY_ID
6038 +extern int sdio_register_driver(struct sdio_driver *);
6039 +extern void sdio_unregister_driver(struct sdio_driver *);
6042 + * SDIO I/O operations
6044 +extern void sdio_claim_host(struct sdio_func *func);
6045 +extern void sdio_release_host(struct sdio_func *func);
6047 +extern int sdio_enable_func(struct sdio_func *func);
6048 +extern int sdio_disable_func(struct sdio_func *func);
6050 +extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
6052 +extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
6053 +extern int sdio_release_irq(struct sdio_func *func);
6055 +extern unsigned char sdio_readb(struct sdio_func *func,
6056 + unsigned int addr, int *err_ret);
6057 +extern unsigned short sdio_readw(struct sdio_func *func,
6058 + unsigned int addr, int *err_ret);
6059 +extern unsigned long sdio_readl(struct sdio_func *func,
6060 + unsigned int addr, int *err_ret);
6062 +extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
6063 + unsigned int addr, int count);
6064 +extern int sdio_readsb(struct sdio_func *func, void *dst,
6065 + unsigned int addr, int count);
6067 +extern void sdio_writeb(struct sdio_func *func, unsigned char b,
6068 + unsigned int addr, int *err_ret);
6069 +extern void sdio_writew(struct sdio_func *func, unsigned short b,
6070 + unsigned int addr, int *err_ret);
6071 +extern void sdio_writel(struct sdio_func *func, unsigned long b,
6072 + unsigned int addr, int *err_ret);
6074 +extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
6075 + void *src, int count);
6076 +extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
6077 + void *src, int count);
6079 +extern unsigned char sdio_f0_readb(struct sdio_func *func,
6080 + unsigned int addr, int *err_ret);
6081 +extern void sdio_f0_writeb(struct sdio_func *func, unsigned char b,
6082 + unsigned int addr, int *err_ret);
6087 +++ b/include/linux/mmc/sdio_ids.h
6090 + * SDIO Classes, Interface Types, Manufacturer IDs, etc.
6093 +#ifndef MMC_SDIO_IDS_H
6094 +#define MMC_SDIO_IDS_H
6097 + * Standard SDIO Function Interfaces
6100 +#define SDIO_CLASS_NONE 0x00 /* Not a SDIO standard interface */
6101 +#define SDIO_CLASS_UART 0x01 /* standard UART interface */
6102 +#define SDIO_CLASS_BT_A 0x02 /* Type-A BlueTooth std interface */
6103 +#define SDIO_CLASS_BT_B 0x03 /* Type-B BlueTooth std interface */
6104 +#define SDIO_CLASS_GPS 0x04 /* GPS standard interface */
6105 +#define SDIO_CLASS_CAMERA 0x05 /* Camera standard interface */
6106 +#define SDIO_CLASS_PHS 0x06 /* PHS standard interface */
6107 +#define SDIO_CLASS_WLAN 0x07 /* WLAN interface */
6108 +#define SDIO_CLASS_ATA 0x08 /* Embedded SDIO-ATA std interface */
6111 + * Vendors and devices. Sort key: vendor first, device next.
6114 +#define SDIO_VENDOR_ID_MARVELL 0x02df
6115 +#define SDIO_DEVICE_ID_MARVELL_LIBERTAS 0x9103
6118 --- a/include/linux/mod_devicetable.h
6119 +++ b/include/linux/mod_devicetable.h
6120 @@ -22,6 +22,18 @@ struct pci_device_id {
6126 +#define SDIO_ANY_ID (~0)
6128 +struct sdio_device_id {
6129 + __u8 class; /* Standard interface or SDIO_ANY_ID */
6130 + __u16 vendor; /* Vendor or SDIO_ANY_ID */
6131 + __u16 device; /* Device ID or SDIO_ANY_ID */
6132 + kernel_ulong_t driver_data; /* Data private to the driver */
6136 #define IEEE1394_MATCH_VENDOR_ID 0x0001
6137 #define IEEE1394_MATCH_MODEL_ID 0x0002
6138 #define IEEE1394_MATCH_SPECIFIER_ID 0x0004
6139 --- a/drivers/mmc/card/Kconfig
6140 +++ b/drivers/mmc/card/Kconfig
6141 @@ -32,3 +32,10 @@ config MMC_BLOCK_BOUNCE
6143 If unsure, say Y here.
6146 + tristate "SDIO UART/GPS class support"
6149 + SDIO function driver for SDIO cards that implements the UART
6150 + class, as well as the GPS class which appears like a UART.
6152 --- a/drivers/mmc/card/Makefile
6153 +++ b/drivers/mmc/card/Makefile
6154 @@ -9,3 +9,5 @@ endif
6155 obj-$(CONFIG_MMC_BLOCK) += mmc_block.o
6156 mmc_block-objs := block.o queue.o
6158 +obj-$(CONFIG_SDIO_UART) += sdio_uart.o
6160 --- a/drivers/mmc/card/block.c
6161 +++ b/drivers/mmc/card/block.c
6163 * max 8 partitions per card
6166 +#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6168 +static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6171 * There is one mmc_blk_data per slot.
6172 @@ -80,6 +83,9 @@ static void mmc_blk_put(struct mmc_blk_d
6173 mutex_lock(&open_lock);
6175 if (md->usage == 0) {
6176 + int devidx = md->disk->first_minor >> MMC_SHIFT;
6177 + __clear_bit(devidx, dev_use);
6182 @@ -151,17 +157,19 @@ static u32 mmc_sd_num_wr_blocks(struct m
6184 cmd.opcode = MMC_APP_CMD;
6185 cmd.arg = card->rca << 16;
6186 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6187 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6189 err = mmc_wait_for_cmd(card->host, &cmd, 0);
6190 - if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
6193 + if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
6196 memset(&cmd, 0, sizeof(struct mmc_command));
6198 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
6200 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6201 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6203 memset(&data, 0, sizeof(struct mmc_data));
6205 @@ -192,7 +200,7 @@ static u32 mmc_sd_num_wr_blocks(struct m
6207 mmc_wait_for_req(card->host, &mrq);
6209 - if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
6210 + if (cmd.error || data.error)
6213 blocks = ntohl(blocks);
6214 @@ -220,17 +228,15 @@ static int mmc_blk_issue_rq(struct mmc_q
6215 brq.cmd.arg = req->sector;
6216 if (!mmc_card_blockaddr(card))
6218 - brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6219 + brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6220 brq.data.blksz = 1 << md->block_bits;
6221 brq.stop.opcode = MMC_STOP_TRANSMISSION;
6223 - brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
6224 + brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
6225 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
6226 if (brq.data.blocks > card->host->max_blk_count)
6227 brq.data.blocks = card->host->max_blk_count;
6229 - mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
6232 * If the host doesn't support multiple block writes, force
6233 * block writes to single block. SD cards are excepted from
6234 @@ -243,8 +249,12 @@ static int mmc_blk_issue_rq(struct mmc_q
6235 brq.data.blocks = 1;
6237 if (brq.data.blocks > 1) {
6238 - brq.data.flags |= MMC_DATA_MULTI;
6239 - brq.mrq.stop = &brq.stop;
6240 + /* SPI multiblock writes terminate using a special
6241 + * token, not a STOP_TRANSMISSION request.
6243 + if (!mmc_host_is_spi(card->host)
6244 + || rq_data_dir(req) == READ)
6245 + brq.mrq.stop = &brq.stop;
6246 readcmd = MMC_READ_MULTIPLE_BLOCK;
6247 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
6249 @@ -261,6 +271,8 @@ static int mmc_blk_issue_rq(struct mmc_q
6250 brq.data.flags |= MMC_DATA_WRITE;
6253 + mmc_set_data_timeout(&brq.data, card);
6255 brq.data.sg = mq->sg;
6256 brq.data.sg_len = mmc_queue_map_sg(mq);
6258 @@ -302,7 +314,7 @@ static int mmc_blk_issue_rq(struct mmc_q
6262 - if (rq_data_dir(req) != READ) {
6263 + if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
6267 @@ -315,7 +327,13 @@ static int mmc_blk_issue_rq(struct mmc_q
6268 req->rq_disk->disk_name, err);
6271 - } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
6273 + * Some cards mishandle the status bits,
6274 + * so make sure to check both the busy
6275 + * indication and the card state.
6277 + } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
6278 + (R1_CURRENT_STATE(cmd.resp[0]) == 7));
6281 if (cmd.resp[0] & ~0x00000900)
6282 @@ -394,9 +412,6 @@ static int mmc_blk_issue_rq(struct mmc_q
6286 -#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6288 -static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6290 static inline int mmc_blk_readonly(struct mmc_card *card)
6292 @@ -510,7 +525,7 @@ mmc_blk_set_blksize(struct mmc_blk_data
6293 mmc_claim_host(card->host);
6294 cmd.opcode = MMC_SET_BLOCKLEN;
6295 cmd.arg = 1 << md->block_bits;
6296 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6297 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6298 err = mmc_wait_for_cmd(card->host, &cmd, 5);
6299 mmc_release_host(card->host);
6301 @@ -562,17 +577,12 @@ static void mmc_blk_remove(struct mmc_ca
6302 struct mmc_blk_data *md = mmc_get_drvdata(card);
6307 /* Stop new requests from getting into the queue */
6308 del_gendisk(md->disk);
6310 /* Then flush out any already in there */
6311 mmc_cleanup_queue(&md->queue);
6313 - devidx = md->disk->first_minor >> MMC_SHIFT;
6314 - __clear_bit(devidx, dev_use);
6318 mmc_set_drvdata(card, NULL);
6319 --- a/drivers/mmc/card/queue.c
6320 +++ b/drivers/mmc/card/queue.c
6322 #include <linux/blkdev.h>
6323 #include <linux/freezer.h>
6324 #include <linux/kthread.h>
6325 +#include <linux/scatterlist.h>
6327 #include <linux/mmc/card.h>
6328 #include <linux/mmc/host.h>
6331 #define MMC_QUEUE_SUSPENDED (1 << 0)
6333 +#define sg_init_table(sg, n) do { \
6334 + memset(sg, 0, sizeof(*(sg)) * (n)); \
6337 +#define sg_virt(sg) (page_address((sg)->page) + (sg)->offset)
6340 * Prepare a MMC request. This just filters out odd stuff.
6342 @@ -159,6 +166,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6346 + sg_init_table(mq->sg, 1);
6348 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
6349 bouncesz / 512, GFP_KERNEL);
6350 @@ -166,6 +174,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6354 + sg_init_table(mq->bounce_sg, bouncesz / 512);
6358 @@ -183,6 +192,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6362 + sg_init_table(mq->sg, host->max_phys_segs);
6365 init_MUTEX(&mq->thread_sem);
6366 @@ -302,12 +312,12 @@ static void copy_sg(struct scatterlist *
6367 BUG_ON(dst_len == 0);
6369 if (dst_size == 0) {
6370 - dst_buf = page_address(dst->page) + dst->offset;
6371 + dst_buf = sg_virt(dst);
6372 dst_size = dst->length;
6375 if (src_size == 0) {
6376 - src_buf = page_address(src->page) + src->offset;
6377 + src_buf = sg_virt(src);
6378 src_size = src->length;
6381 @@ -353,9 +363,7 @@ unsigned int mmc_queue_map_sg(struct mmc
6385 - mq->sg[0].page = virt_to_page(mq->bounce_buf);
6386 - mq->sg[0].offset = offset_in_page(mq->bounce_buf);
6387 - mq->sg[0].length = 0;
6388 + sg_init_one(mq->sg, mq->bounce_buf, 0);
6391 mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
6393 +++ b/drivers/mmc/card/sdio_uart.c
6396 + * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
6398 + * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
6399 + * by Russell King.
6401 + * Author: Nicolas Pitre
6402 + * Created: June 15, 2007
6403 + * Copyright: MontaVista Software, Inc.
6405 + * This program is free software; you can redistribute it and/or modify
6406 + * it under the terms of the GNU General Public License as published by
6407 + * the Free Software Foundation; either version 2 of the License, or (at
6408 + * your option) any later version.
6412 + * Note: Although this driver assumes a 16550A-like UART implementation,
6413 + * it is not possible to leverage the common 8250/16550 driver, nor the
6414 + * core UART infrastructure, as they assumes direct access to the hardware
6415 + * registers, often under a spinlock. This is not possible in the SDIO
6416 + * context as SDIO access functions must be able to sleep.
6418 + * Because we need to lock the SDIO host to ensure an exclusive access to
6419 + * the card, we simply rely on that lock to also prevent and serialize
6420 + * concurrent access to the same port.
6423 +#include <linux/module.h>
6424 +#include <linux/init.h>
6425 +#include <linux/kernel.h>
6426 +#include <linux/mutex.h>
6427 +#include <linux/serial_reg.h>
6428 +#include <linux/circ_buf.h>
6429 +#include <linux/gfp.h>
6430 +#include <linux/tty.h>
6431 +#include <linux/tty_flip.h>
6433 +#include <linux/mmc/core.h>
6434 +#include <linux/mmc/card.h>
6435 +#include <linux/mmc/sdio_func.h>
6436 +#include <linux/mmc/sdio_ids.h>
6439 +#define UART_NR 8 /* Number of UARTs this driver can handle */
6442 +#define UART_XMIT_SIZE PAGE_SIZE
6443 +#define WAKEUP_CHARS 256
6445 +#define circ_empty(circ) ((circ)->head == (circ)->tail)
6446 +#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
6448 +#define circ_chars_pending(circ) \
6449 + (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6451 +#define circ_chars_free(circ) \
6452 + (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6455 +struct uart_icount {
6468 +struct sdio_uart_port {
6470 + struct tty_struct *tty;
6471 + unsigned int index;
6472 + unsigned int opened;
6473 + struct mutex open_lock;
6474 + struct sdio_func *func;
6475 + struct mutex func_lock;
6476 + struct task_struct *in_sdio_uart_irq;
6477 + unsigned int regs_offset;
6478 + struct circ_buf xmit;
6479 + spinlock_t write_lock;
6480 + struct uart_icount icount;
6481 + unsigned int uartclk;
6482 + unsigned int mctrl;
6483 + unsigned int read_status_mask;
6484 + unsigned int ignore_status_mask;
6485 + unsigned char x_char;
6486 + unsigned char ier;
6487 + unsigned char lcr;
6490 +static struct sdio_uart_port *sdio_uart_table[UART_NR];
6491 +static DEFINE_SPINLOCK(sdio_uart_table_lock);
6493 +static int sdio_uart_add_port(struct sdio_uart_port *port)
6495 + int index, ret = -EBUSY;
6497 + kref_init(&port->kref);
6498 + mutex_init(&port->open_lock);
6499 + mutex_init(&port->func_lock);
6500 + spin_lock_init(&port->write_lock);
6502 + spin_lock(&sdio_uart_table_lock);
6503 + for (index = 0; index < UART_NR; index++) {
6504 + if (!sdio_uart_table[index]) {
6505 + port->index = index;
6506 + sdio_uart_table[index] = port;
6511 + spin_unlock(&sdio_uart_table_lock);
6516 +static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
6518 + struct sdio_uart_port *port;
6520 + if (index >= UART_NR)
6523 + spin_lock(&sdio_uart_table_lock);
6524 + port = sdio_uart_table[index];
6526 + kref_get(&port->kref);
6527 + spin_unlock(&sdio_uart_table_lock);
6532 +static void sdio_uart_port_destroy(struct kref *kref)
6534 + struct sdio_uart_port *port =
6535 + container_of(kref, struct sdio_uart_port, kref);
6539 +static void sdio_uart_port_put(struct sdio_uart_port *port)
6541 + kref_put(&port->kref, sdio_uart_port_destroy);
6544 +static void sdio_uart_port_remove(struct sdio_uart_port *port)
6546 + struct sdio_func *func;
6548 + BUG_ON(sdio_uart_table[port->index] != port);
6550 + spin_lock(&sdio_uart_table_lock);
6551 + sdio_uart_table[port->index] = NULL;
6552 + spin_unlock(&sdio_uart_table_lock);
6555 + * We're killing a port that potentially still is in use by
6556 + * the tty layer. Be careful to prevent any further access
6557 + * to the SDIO function and arrange for the tty layer to
6558 + * give up on that port ASAP.
6559 + * Beware: the lock ordering is critical.
6561 + mutex_lock(&port->open_lock);
6562 + mutex_lock(&port->func_lock);
6563 + func = port->func;
6564 + sdio_claim_host(func);
6565 + port->func = NULL;
6566 + mutex_unlock(&port->func_lock);
6568 + tty_hangup(port->tty);
6569 + mutex_unlock(&port->open_lock);
6570 + sdio_release_irq(func);
6571 + sdio_disable_func(func);
6572 + sdio_release_host(func);
6574 + sdio_uart_port_put(port);
6577 +static int sdio_uart_claim_func(struct sdio_uart_port *port)
6579 + mutex_lock(&port->func_lock);
6580 + if (unlikely(!port->func)) {
6581 + mutex_unlock(&port->func_lock);
6584 + if (likely(port->in_sdio_uart_irq != current))
6585 + sdio_claim_host(port->func);
6586 + mutex_unlock(&port->func_lock);
6590 +static inline void sdio_uart_release_func(struct sdio_uart_port *port)
6592 + if (likely(port->in_sdio_uart_irq != current))
6593 + sdio_release_host(port->func);
6596 +static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
6599 + c = sdio_readb(port->func, port->regs_offset + offset, NULL);
6603 +static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
6605 + sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
6608 +static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
6610 + unsigned char status;
6613 + status = sdio_in(port, UART_MSR);
6616 + if (status & UART_MSR_DCD)
6618 + if (status & UART_MSR_RI)
6620 + if (status & UART_MSR_DSR)
6622 + if (status & UART_MSR_CTS)
6627 +static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
6629 + unsigned char mcr = 0;
6631 + if (mctrl & TIOCM_RTS)
6632 + mcr |= UART_MCR_RTS;
6633 + if (mctrl & TIOCM_DTR)
6634 + mcr |= UART_MCR_DTR;
6635 + if (mctrl & TIOCM_OUT1)
6636 + mcr |= UART_MCR_OUT1;
6637 + if (mctrl & TIOCM_OUT2)
6638 + mcr |= UART_MCR_OUT2;
6639 + if (mctrl & TIOCM_LOOP)
6640 + mcr |= UART_MCR_LOOP;
6642 + sdio_out(port, UART_MCR, mcr);
6645 +static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
6646 + unsigned int set, unsigned int clear)
6650 + old = port->mctrl;
6651 + port->mctrl = (old & ~clear) | set;
6652 + if (old != port->mctrl)
6653 + sdio_uart_write_mctrl(port, port->mctrl);
6656 +#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
6657 +#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
6659 +static void sdio_uart_change_speed(struct sdio_uart_port *port,
6660 + struct ktermios *termios,
6661 + struct ktermios *old)
6663 + unsigned char cval, fcr = 0;
6664 + unsigned int baud, quot;
6666 + switch (termios->c_cflag & CSIZE) {
6668 + cval = UART_LCR_WLEN5;
6671 + cval = UART_LCR_WLEN6;
6674 + cval = UART_LCR_WLEN7;
6678 + cval = UART_LCR_WLEN8;
6682 + if (termios->c_cflag & CSTOPB)
6683 + cval |= UART_LCR_STOP;
6684 + if (termios->c_cflag & PARENB)
6685 + cval |= UART_LCR_PARITY;
6686 + if (!(termios->c_cflag & PARODD))
6687 + cval |= UART_LCR_EPAR;
6690 + baud = tty_termios_baud_rate(termios);
6692 + baud = 9600; /* Special case: B0 rate. */
6693 + if (baud <= port->uartclk)
6696 + * Oops, the quotient was zero. Try again with the old
6697 + * baud rate if possible, otherwise default to 9600.
6699 + termios->c_cflag &= ~CBAUD;
6701 + termios->c_cflag |= old->c_cflag & CBAUD;
6704 + termios->c_cflag |= B9600;
6706 + quot = (2 * port->uartclk + baud) / (2 * baud);
6709 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
6711 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
6713 + port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
6714 + if (termios->c_iflag & INPCK)
6715 + port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
6716 + if (termios->c_iflag & (BRKINT | PARMRK))
6717 + port->read_status_mask |= UART_LSR_BI;
6720 + * Characters to ignore
6722 + port->ignore_status_mask = 0;
6723 + if (termios->c_iflag & IGNPAR)
6724 + port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
6725 + if (termios->c_iflag & IGNBRK) {
6726 + port->ignore_status_mask |= UART_LSR_BI;
6728 + * If we're ignoring parity and break indicators,
6729 + * ignore overruns too (for real raw support).
6731 + if (termios->c_iflag & IGNPAR)
6732 + port->ignore_status_mask |= UART_LSR_OE;
6736 + * ignore all characters if CREAD is not set
6738 + if ((termios->c_cflag & CREAD) == 0)
6739 + port->ignore_status_mask |= UART_LSR_DR;
6742 + * CTS flow control flag and modem status interrupts
6744 + port->ier &= ~UART_IER_MSI;
6745 + if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
6746 + port->ier |= UART_IER_MSI;
6750 + sdio_out(port, UART_IER, port->ier);
6751 + sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
6752 + sdio_out(port, UART_DLL, quot & 0xff);
6753 + sdio_out(port, UART_DLM, quot >> 8);
6754 + sdio_out(port, UART_LCR, cval);
6755 + sdio_out(port, UART_FCR, fcr);
6757 + sdio_uart_write_mctrl(port, port->mctrl);
6760 +static void sdio_uart_start_tx(struct sdio_uart_port *port)
6762 + if (!(port->ier & UART_IER_THRI)) {
6763 + port->ier |= UART_IER_THRI;
6764 + sdio_out(port, UART_IER, port->ier);
6768 +static void sdio_uart_stop_tx(struct sdio_uart_port *port)
6770 + if (port->ier & UART_IER_THRI) {
6771 + port->ier &= ~UART_IER_THRI;
6772 + sdio_out(port, UART_IER, port->ier);
6776 +static void sdio_uart_stop_rx(struct sdio_uart_port *port)
6778 + port->ier &= ~UART_IER_RLSI;
6779 + port->read_status_mask &= ~UART_LSR_DR;
6780 + sdio_out(port, UART_IER, port->ier);
6783 +static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
6785 + struct tty_struct *tty = port->tty;
6786 + unsigned int ch, flag;
6787 + int max_count = 256;
6790 + ch = sdio_in(port, UART_RX);
6791 + flag = TTY_NORMAL;
6792 + port->icount.rx++;
6794 + if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
6795 + UART_LSR_FE | UART_LSR_OE))) {
6797 + * For statistics only
6799 + if (*status & UART_LSR_BI) {
6800 + *status &= ~(UART_LSR_FE | UART_LSR_PE);
6801 + port->icount.brk++;
6802 + } else if (*status & UART_LSR_PE)
6803 + port->icount.parity++;
6804 + else if (*status & UART_LSR_FE)
6805 + port->icount.frame++;
6806 + if (*status & UART_LSR_OE)
6807 + port->icount.overrun++;
6810 + * Mask off conditions which should be ignored.
6812 + *status &= port->read_status_mask;
6813 + if (*status & UART_LSR_BI) {
6815 + } else if (*status & UART_LSR_PE)
6816 + flag = TTY_PARITY;
6817 + else if (*status & UART_LSR_FE)
6821 + if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
6822 + tty_insert_flip_char(tty, ch, flag);
6825 + * Overrun is special. Since it's reported immediately,
6826 + * it doesn't affect the current character.
6828 + if (*status & ~port->ignore_status_mask & UART_LSR_OE)
6829 + tty_insert_flip_char(tty, 0, TTY_OVERRUN);
6831 + *status = sdio_in(port, UART_LSR);
6832 + } while ((*status & UART_LSR_DR) && (max_count-- > 0));
6833 + tty_flip_buffer_push(tty);
6836 +static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
6838 + struct circ_buf *xmit = &port->xmit;
6841 + if (port->x_char) {
6842 + sdio_out(port, UART_TX, port->x_char);
6843 + port->icount.tx++;
6847 + if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
6848 + sdio_uart_stop_tx(port);
6854 + sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
6855 + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
6856 + port->icount.tx++;
6857 + if (circ_empty(xmit))
6859 + } while (--count > 0);
6861 + if (circ_chars_pending(xmit) < WAKEUP_CHARS)
6862 + tty_wakeup(port->tty);
6864 + if (circ_empty(xmit))
6865 + sdio_uart_stop_tx(port);
6868 +static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
6872 + status = sdio_in(port, UART_MSR);
6874 + if ((status & UART_MSR_ANY_DELTA) == 0)
6877 + if (status & UART_MSR_TERI)
6878 + port->icount.rng++;
6879 + if (status & UART_MSR_DDSR)
6880 + port->icount.dsr++;
6881 + if (status & UART_MSR_DDCD)
6882 + port->icount.dcd++;
6883 + if (status & UART_MSR_DCTS) {
6884 + port->icount.cts++;
6885 + if (port->tty->termios->c_cflag & CRTSCTS) {
6886 + int cts = (status & UART_MSR_CTS);
6887 + if (port->tty->hw_stopped) {
6889 + port->tty->hw_stopped = 0;
6890 + sdio_uart_start_tx(port);
6891 + tty_wakeup(port->tty);
6895 + port->tty->hw_stopped = 1;
6896 + sdio_uart_stop_tx(port);
6904 + * This handles the interrupt from one port.
6906 +static void sdio_uart_irq(struct sdio_func *func)
6908 + struct sdio_uart_port *port = sdio_get_drvdata(func);
6909 + unsigned int iir, lsr;
6912 + * In a few places sdio_uart_irq() is called directly instead of
6913 + * waiting for the actual interrupt to be raised and the SDIO IRQ
6914 + * thread scheduled in order to reduce latency. However, some
6915 + * interaction with the tty core may end up calling us back
6916 + * (serial echo, flow control, etc.) through those same places
6917 + * causing undesirable effects. Let's stop the recursion here.
6919 + if (unlikely(port->in_sdio_uart_irq == current))
6922 + iir = sdio_in(port, UART_IIR);
6923 + if (iir & UART_IIR_NO_INT)
6926 + port->in_sdio_uart_irq = current;
6927 + lsr = sdio_in(port, UART_LSR);
6928 + if (lsr & UART_LSR_DR)
6929 + sdio_uart_receive_chars(port, &lsr);
6930 + sdio_uart_check_modem_status(port);
6931 + if (lsr & UART_LSR_THRE)
6932 + sdio_uart_transmit_chars(port);
6933 + port->in_sdio_uart_irq = NULL;
6936 +static int sdio_uart_startup(struct sdio_uart_port *port)
6938 + unsigned long page;
6942 + * Set the TTY IO error marker - we will only clear this
6943 + * once we have successfully opened the port.
6945 + set_bit(TTY_IO_ERROR, &port->tty->flags);
6947 + /* Initialise and allocate the transmit buffer. */
6948 + page = __get_free_page(GFP_KERNEL);
6951 + port->xmit.buf = (unsigned char *)page;
6952 + circ_clear(&port->xmit);
6954 + ret = sdio_uart_claim_func(port);
6957 + ret = sdio_enable_func(port->func);
6960 + ret = sdio_claim_irq(port->func, sdio_uart_irq);
6965 + * Clear the FIFO buffers and disable them.
6966 + * (they will be reenabled in sdio_change_speed())
6968 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
6969 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
6970 + UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
6971 + sdio_out(port, UART_FCR, 0);
6974 + * Clear the interrupt registers.
6976 + (void) sdio_in(port, UART_LSR);
6977 + (void) sdio_in(port, UART_RX);
6978 + (void) sdio_in(port, UART_IIR);
6979 + (void) sdio_in(port, UART_MSR);
6982 + * Now, initialize the UART
6984 + sdio_out(port, UART_LCR, UART_LCR_WLEN8);
6986 + port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
6987 + port->mctrl = TIOCM_OUT2;
6989 + sdio_uart_change_speed(port, port->tty->termios, NULL);
6991 + if (port->tty->termios->c_cflag & CBAUD)
6992 + sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
6994 + if (port->tty->termios->c_cflag & CRTSCTS)
6995 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
6996 + port->tty->hw_stopped = 1;
6998 + clear_bit(TTY_IO_ERROR, &port->tty->flags);
7000 + /* Kick the IRQ handler once while we're still holding the host lock */
7001 + sdio_uart_irq(port->func);
7003 + sdio_uart_release_func(port);
7007 + sdio_disable_func(port->func);
7009 + sdio_uart_release_func(port);
7011 + free_page((unsigned long)port->xmit.buf);
7015 +static void sdio_uart_shutdown(struct sdio_uart_port *port)
7019 + ret = sdio_uart_claim_func(port);
7023 + sdio_uart_stop_rx(port);
7025 + /* TODO: wait here for TX FIFO to drain */
7027 + /* Turn off DTR and RTS early. */
7028 + if (port->tty->termios->c_cflag & HUPCL)
7029 + sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
7031 + /* Disable interrupts from this port */
7032 + sdio_release_irq(port->func);
7034 + sdio_out(port, UART_IER, 0);
7036 + sdio_uart_clear_mctrl(port, TIOCM_OUT2);
7038 + /* Disable break condition and FIFOs. */
7039 + port->lcr &= ~UART_LCR_SBC;
7040 + sdio_out(port, UART_LCR, port->lcr);
7041 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7042 + UART_FCR_CLEAR_RCVR |
7043 + UART_FCR_CLEAR_XMIT);
7044 + sdio_out(port, UART_FCR, 0);
7046 + sdio_disable_func(port->func);
7048 + sdio_uart_release_func(port);
7051 + /* Free the transmit buffer page. */
7052 + free_page((unsigned long)port->xmit.buf);
7055 +static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
7057 + struct sdio_uart_port *port;
7060 + port = sdio_uart_port_get(tty->index);
7064 + mutex_lock(&port->open_lock);
7067 + * Make sure not to mess up with a dead port
7068 + * which has not been closed yet.
7070 + if (tty->driver_data && tty->driver_data != port) {
7071 + mutex_unlock(&port->open_lock);
7072 + sdio_uart_port_put(port);
7076 + if (!port->opened) {
7077 + tty->driver_data = port;
7079 + ret = sdio_uart_startup(port);
7081 + tty->driver_data = NULL;
7083 + mutex_unlock(&port->open_lock);
7084 + sdio_uart_port_put(port);
7089 + mutex_unlock(&port->open_lock);
7093 +static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
7095 + struct sdio_uart_port *port = tty->driver_data;
7100 + mutex_lock(&port->open_lock);
7101 + BUG_ON(!port->opened);
7104 + * This is messy. The tty layer calls us even when open()
7105 + * returned an error. Ignore this close request if tty->count
7106 + * is larger than port->count.
7108 + if (tty->count > port->opened) {
7109 + mutex_unlock(&port->open_lock);
7113 + if (--port->opened == 0) {
7115 + sdio_uart_shutdown(port);
7116 + tty_ldisc_flush(tty);
7118 + tty->driver_data = NULL;
7121 + mutex_unlock(&port->open_lock);
7122 + sdio_uart_port_put(port);
7125 +static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
7128 + struct sdio_uart_port *port = tty->driver_data;
7129 + struct circ_buf *circ = &port->xmit;
7135 + spin_lock(&port->write_lock);
7137 + c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
7142 + memcpy(circ->buf + circ->head, buf, c);
7143 + circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
7148 + spin_unlock(&port->write_lock);
7150 + if ( !(port->ier & UART_IER_THRI)) {
7151 + int err = sdio_uart_claim_func(port);
7153 + sdio_uart_start_tx(port);
7154 + sdio_uart_irq(port->func);
7155 + sdio_uart_release_func(port);
7163 +static int sdio_uart_write_room(struct tty_struct *tty)
7165 + struct sdio_uart_port *port = tty->driver_data;
7166 + return port ? circ_chars_free(&port->xmit) : 0;
7169 +static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
7171 + struct sdio_uart_port *port = tty->driver_data;
7172 + return port ? circ_chars_pending(&port->xmit) : 0;
7175 +static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
7177 + struct sdio_uart_port *port = tty->driver_data;
7179 + port->x_char = ch;
7180 + if (ch && !(port->ier & UART_IER_THRI)) {
7181 + if (sdio_uart_claim_func(port) != 0)
7183 + sdio_uart_start_tx(port);
7184 + sdio_uart_irq(port->func);
7185 + sdio_uart_release_func(port);
7189 +static void sdio_uart_throttle(struct tty_struct *tty)
7191 + struct sdio_uart_port *port = tty->driver_data;
7193 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7196 + if (sdio_uart_claim_func(port) != 0)
7199 + if (I_IXOFF(tty)) {
7200 + port->x_char = STOP_CHAR(tty);
7201 + sdio_uart_start_tx(port);
7204 + if (tty->termios->c_cflag & CRTSCTS)
7205 + sdio_uart_clear_mctrl(port, TIOCM_RTS);
7207 + sdio_uart_irq(port->func);
7208 + sdio_uart_release_func(port);
7211 +static void sdio_uart_unthrottle(struct tty_struct *tty)
7213 + struct sdio_uart_port *port = tty->driver_data;
7215 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7218 + if (sdio_uart_claim_func(port) != 0)
7221 + if (I_IXOFF(tty)) {
7222 + if (port->x_char) {
7225 + port->x_char = START_CHAR(tty);
7226 + sdio_uart_start_tx(port);
7230 + if (tty->termios->c_cflag & CRTSCTS)
7231 + sdio_uart_set_mctrl(port, TIOCM_RTS);
7233 + sdio_uart_irq(port->func);
7234 + sdio_uart_release_func(port);
7237 +static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
7239 + struct sdio_uart_port *port = tty->driver_data;
7240 + unsigned int cflag = tty->termios->c_cflag;
7242 +#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
7244 + if ((cflag ^ old_termios->c_cflag) == 0 &&
7245 + RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
7248 + if (sdio_uart_claim_func(port) != 0)
7251 + sdio_uart_change_speed(port, tty->termios, old_termios);
7253 + /* Handle transition to B0 status */
7254 + if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
7255 + sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7257 + /* Handle transition away from B0 status */
7258 + if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
7259 + unsigned int mask = TIOCM_DTR;
7260 + if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
7261 + mask |= TIOCM_RTS;
7262 + sdio_uart_set_mctrl(port, mask);
7265 + /* Handle turning off CRTSCTS */
7266 + if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
7267 + tty->hw_stopped = 0;
7268 + sdio_uart_start_tx(port);
7271 + /* Handle turning on CRTSCTS */
7272 + if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
7273 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
7274 + tty->hw_stopped = 1;
7275 + sdio_uart_stop_tx(port);
7279 + sdio_uart_release_func(port);
7282 +static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
7284 + struct sdio_uart_port *port = tty->driver_data;
7286 + if (sdio_uart_claim_func(port) != 0)
7289 + if (break_state == -1)
7290 + port->lcr |= UART_LCR_SBC;
7292 + port->lcr &= ~UART_LCR_SBC;
7293 + sdio_out(port, UART_LCR, port->lcr);
7295 + sdio_uart_release_func(port);
7298 +static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
7300 + struct sdio_uart_port *port = tty->driver_data;
7303 + result = sdio_uart_claim_func(port);
7305 + result = port->mctrl | sdio_uart_get_mctrl(port);
7306 + sdio_uart_release_func(port);
7312 +static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
7313 + unsigned int set, unsigned int clear)
7315 + struct sdio_uart_port *port = tty->driver_data;
7318 + result =sdio_uart_claim_func(port);
7320 + sdio_uart_update_mctrl(port, set, clear);
7321 + sdio_uart_release_func(port);
7327 +static int sdio_uart_read_proc(char *page, char **start, off_t off,
7328 + int count, int *eof, void *data)
7333 + len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
7335 + for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
7336 + struct sdio_uart_port *port = sdio_uart_port_get(i);
7338 + len += sprintf(page+len, "%d: uart:SDIO", i);
7339 + if(capable(CAP_SYS_ADMIN)) {
7340 + len += sprintf(page + len, " tx:%d rx:%d",
7341 + port->icount.tx, port->icount.rx);
7342 + if (port->icount.frame)
7343 + len += sprintf(page + len, " fe:%d",
7344 + port->icount.frame);
7345 + if (port->icount.parity)
7346 + len += sprintf(page + len, " pe:%d",
7347 + port->icount.parity);
7348 + if (port->icount.brk)
7349 + len += sprintf(page + len, " brk:%d",
7350 + port->icount.brk);
7351 + if (port->icount.overrun)
7352 + len += sprintf(page + len, " oe:%d",
7353 + port->icount.overrun);
7354 + if (port->icount.cts)
7355 + len += sprintf(page + len, " cts:%d",
7356 + port->icount.cts);
7357 + if (port->icount.dsr)
7358 + len += sprintf(page + len, " dsr:%d",
7359 + port->icount.dsr);
7360 + if (port->icount.rng)
7361 + len += sprintf(page + len, " rng:%d",
7362 + port->icount.rng);
7363 + if (port->icount.dcd)
7364 + len += sprintf(page + len, " dcd:%d",
7365 + port->icount.dcd);
7367 + strcat(page, "\n");
7369 + sdio_uart_port_put(port);
7372 + if (len + begin > off + count)
7374 + if (len + begin < off) {
7382 + if (off >= len + begin)
7384 + *start = page + (off - begin);
7385 + return (count < begin + len - off) ? count : (begin + len - off);
7388 +static const struct tty_operations sdio_uart_ops = {
7389 + .open = sdio_uart_open,
7390 + .close = sdio_uart_close,
7391 + .write = sdio_uart_write,
7392 + .write_room = sdio_uart_write_room,
7393 + .chars_in_buffer = sdio_uart_chars_in_buffer,
7394 + .send_xchar = sdio_uart_send_xchar,
7395 + .throttle = sdio_uart_throttle,
7396 + .unthrottle = sdio_uart_unthrottle,
7397 + .set_termios = sdio_uart_set_termios,
7398 + .break_ctl = sdio_uart_break_ctl,
7399 + .tiocmget = sdio_uart_tiocmget,
7400 + .tiocmset = sdio_uart_tiocmset,
7401 + .read_proc = sdio_uart_read_proc,
7404 +static struct tty_driver *sdio_uart_tty_driver;
7406 +static int sdio_uart_probe(struct sdio_func *func,
7407 + const struct sdio_device_id *id)
7409 + struct sdio_uart_port *port;
7412 + port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
7416 + if (func->class == SDIO_CLASS_UART) {
7417 + printk(KERN_WARNING "%s: need info on UART class basic setup\n",
7418 + sdio_func_id(func));
7421 + } else if (func->class == SDIO_CLASS_GPS) {
7423 + * We need tuple 0x91. It contains SUBTPL_SIOREG
7424 + * and SUBTPL_RCVCAPS.
7426 + struct sdio_func_tuple *tpl;
7427 + for (tpl = func->tuples; tpl; tpl = tpl->next) {
7428 + if (tpl->code != 0x91)
7430 + if (tpl->size < 10)
7432 + if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
7436 + printk(KERN_WARNING
7437 + "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
7438 + sdio_func_id(func));
7442 + printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
7443 + sdio_func_id(func), tpl->data[2], tpl->data[3]);
7444 + port->regs_offset = (tpl->data[4] << 0) |
7445 + (tpl->data[5] << 8) |
7446 + (tpl->data[6] << 16);
7447 + printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
7448 + sdio_func_id(func), port->regs_offset);
7449 + port->uartclk = tpl->data[7] * 115200;
7450 + if (port->uartclk == 0)
7451 + port->uartclk = 115200;
7452 + printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
7453 + sdio_func_id(func), port->uartclk,
7454 + tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
7460 + port->func = func;
7461 + sdio_set_drvdata(func, port);
7463 + ret = sdio_uart_add_port(port);
7467 + struct device *dev;
7468 + dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
7469 + if (IS_ERR(dev)) {
7470 + sdio_uart_port_remove(port);
7471 + ret = PTR_ERR(dev);
7478 +static void sdio_uart_remove(struct sdio_func *func)
7480 + struct sdio_uart_port *port = sdio_get_drvdata(func);
7482 + tty_unregister_device(sdio_uart_tty_driver, port->index);
7483 + sdio_uart_port_remove(port);
7486 +static const struct sdio_device_id sdio_uart_ids[] = {
7487 + { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
7488 + { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
7489 + { /* end: all zeroes */ },
7492 +MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
7494 +static struct sdio_driver sdio_uart_driver = {
7495 + .probe = sdio_uart_probe,
7496 + .remove = sdio_uart_remove,
7497 + .name = "sdio_uart",
7498 + .id_table = sdio_uart_ids,
7501 +static int __init sdio_uart_init(void)
7504 + struct tty_driver *tty_drv;
7506 + sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
7510 + tty_drv->owner = THIS_MODULE;
7511 + tty_drv->driver_name = "sdio_uart";
7512 + tty_drv->name = "ttySDIO";
7513 + tty_drv->major = 0; /* dynamically allocated */
7514 + tty_drv->minor_start = 0;
7515 + tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
7516 + tty_drv->subtype = SERIAL_TYPE_NORMAL;
7517 + tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
7518 + tty_drv->init_termios = tty_std_termios;
7519 + tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
7520 + tty_drv->init_termios.c_ispeed = 4800;
7521 + tty_drv->init_termios.c_ospeed = 4800;
7522 + tty_set_operations(tty_drv, &sdio_uart_ops);
7524 + ret = tty_register_driver(tty_drv);
7528 + ret = sdio_register_driver(&sdio_uart_driver);
7535 + tty_unregister_driver(tty_drv);
7537 + put_tty_driver(tty_drv);
7541 +static void __exit sdio_uart_exit(void)
7543 + sdio_unregister_driver(&sdio_uart_driver);
7544 + tty_unregister_driver(sdio_uart_tty_driver);
7545 + put_tty_driver(sdio_uart_tty_driver);
7548 +module_init(sdio_uart_init);
7549 +module_exit(sdio_uart_exit);
7551 +MODULE_AUTHOR("Nicolas Pitre");
7552 +MODULE_LICENSE("GPL");
7553 --- a/drivers/mmc/core/Makefile
7554 +++ b/drivers/mmc/core/Makefile
7555 @@ -8,5 +8,7 @@ endif
7557 obj-$(CONFIG_MMC) += mmc_core.o
7558 mmc_core-y := core.o sysfs.o bus.o host.o \
7559 - mmc.o mmc_ops.o sd.o sd_ops.o
7560 + mmc.o mmc_ops.o sd.o sd_ops.o \
7561 + sdio.o sdio_ops.o sdio_bus.o \
7562 + sdio_cis.o sdio_io.o sdio_irq.o