1 This is a port of the MMC-SPI driver from 2.6.24.3
5 Index: linux-2.6.23.16/drivers/mmc/host/Kconfig
6 ===================================================================
7 --- linux-2.6.23.16.orig/drivers/mmc/host/Kconfig 2008-03-21 17:28:26.000000000 +0100
8 +++ linux-2.6.23.16/drivers/mmc/host/Kconfig 2008-03-21 17:30:25.000000000 +0100
9 @@ -100,3 +100,16 @@ config MMC_TIFM_SD
10 To compile this driver as a module, choose M here: the
11 module will be called tifm_sd.
14 + tristate "MMC/SD over SPI (EXPERIMENTAL)"
15 + depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
19 + Some systems accss MMC/SD cards using a SPI controller instead of
20 + using a "native" MMC/SD controller. This has a disadvantage of
21 + being relatively high overhead, but a compensating advantage of
22 + working on many systems without dedicated MMC/SD controllers.
24 + If unsure, or if your system has no SPI master driver, say N.
26 Index: linux-2.6.23.16/drivers/mmc/host/Makefile
27 ===================================================================
28 --- linux-2.6.23.16.orig/drivers/mmc/host/Makefile 2008-03-21 17:28:26.000000000 +0100
29 +++ linux-2.6.23.16/drivers/mmc/host/Makefile 2008-03-21 17:30:25.000000000 +0100
30 @@ -15,4 +15,5 @@ obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
31 obj-$(CONFIG_MMC_OMAP) += omap.o
32 obj-$(CONFIG_MMC_AT91) += at91_mci.o
33 obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
34 +obj-$(CONFIG_MMC_SPI) += mmc_spi.o
36 Index: linux-2.6.23.16/drivers/mmc/host/mmc_spi.c
37 ===================================================================
38 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
39 +++ linux-2.6.23.16/drivers/mmc/host/mmc_spi.c 2008-03-21 17:30:25.000000000 +0100
42 + * mmc_spi.c - Access SD/MMC cards through SPI master controllers
44 + * (C) Copyright 2005, Intec Automation,
45 + * Mike Lavender (mike@steroidmicros)
46 + * (C) Copyright 2006-2007, David Brownell
47 + * (C) Copyright 2007, Axis Communications,
48 + * Hans-Peter Nilsson (hp@axis.com)
49 + * (C) Copyright 2007, ATRON electronic GmbH,
50 + * Jan Nikitenko <jan.nikitenko@gmail.com>
53 + * This program is free software; you can redistribute it and/or modify
54 + * it under the terms of the GNU General Public License as published by
55 + * the Free Software Foundation; either version 2 of the License, or
56 + * (at your option) any later version.
58 + * This program is distributed in the hope that it will be useful,
59 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 + * GNU General Public License for more details.
63 + * You should have received a copy of the GNU General Public License
64 + * along with this program; if not, write to the Free Software
65 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
67 +#include <linux/hrtimer.h>
68 +#include <linux/delay.h>
69 +#include <linux/bio.h>
70 +#include <linux/dma-mapping.h>
71 +#include <linux/crc7.h>
72 +#include <linux/crc-itu-t.h>
73 +#include <linux/scatterlist.h>
75 +#include <linux/mmc/host.h>
76 +#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
78 +#include <linux/spi/spi.h>
79 +#include <linux/spi/mmc_spi.h>
81 +#include <asm/unaligned.h>
84 +#define sg_page(sg) (sg)->page
89 + * - For now, we won't try to interoperate with a real mmc/sd/sdio
90 + * controller, although some of them do have hardware support for
91 + * SPI protocol. The main reason for such configs would be mmc-ish
92 + * cards like DataFlash, which don't support that "native" protocol.
94 + * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
95 + * switch between driver stacks, and in any case if "native" mode
96 + * is available, it will be faster and hence preferable.
98 + * - MMC depends on a different chipselect management policy than the
99 + * SPI interface currently supports for shared bus segments: it needs
100 + * to issue multiple spi_message requests with the chipselect active,
101 + * using the results of one message to decide the next one to issue.
103 + * Pending updates to the programming interface, this driver expects
104 + * that it not share the bus with other drivers (precluding conflicts).
106 + * - We tell the controller to keep the chipselect active from the
107 + * beginning of an mmc_host_ops.request until the end. So beware
108 + * of SPI controller drivers that mis-handle the cs_change flag!
110 + * However, many cards seem OK with chipselect flapping up/down
111 + * during that time ... at least on unshared bus segments.
116 + * Local protocol constants, internal to data block protocols.
119 +/* Response tokens used to ack each block written: */
120 +#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
121 +#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
122 +#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
123 +#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
125 +/* Read and write blocks start with these tokens and end with crc;
126 + * on error, read tokens act like a subset of R2_SPI_* values.
128 +#define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
129 +#define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
130 +#define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
132 +#define MMC_SPI_BLOCKSIZE 512
135 +/* These fixed timeouts come from the latest SD specs, which say to ignore
136 + * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
137 + * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
138 + * reads which takes nowhere near that long. Older cards may be able to use
139 + * shorter timeouts ... but why bother?
141 +#define readblock_timeout ktime_set(0, 100 * 1000 * 1000)
142 +#define writeblock_timeout ktime_set(0, 250 * 1000 * 1000)
143 +#define r1b_timeout ktime_set(3, 0)
146 +/****************************************************************************/
149 + * Local Data Structures
152 +/* "scratch" is per-{command,block} data exchanged with the card */
159 +struct mmc_spi_host {
160 + struct mmc_host *mmc;
161 + struct spi_device *spi;
163 + unsigned char power_mode;
166 + struct mmc_spi_platform_data *pdata;
168 + /* for bulk data transfers */
169 + struct spi_transfer token, t, crc, early_status;
170 + struct spi_message m;
172 + /* for status readback */
173 + struct spi_transfer status;
174 + struct spi_message readback;
176 + /* underlying DMA-aware controller, or null */
177 + struct device *dma_dev;
179 + /* buffer used for commands and for message "overhead" */
180 + struct scratch *data;
181 + dma_addr_t data_dma;
183 + /* Specs say to write ones most of the time, even when the card
184 + * has no need to read its input data; and many cards won't care.
185 + * This is our source of those ones.
188 + dma_addr_t ones_dma;
192 +/****************************************************************************/
195 + * MMC-over-SPI protocol glue, used by the MMC stack interface
198 +static inline int mmc_cs_off(struct mmc_spi_host *host)
200 + /* chipselect will always be inactive after setup() */
201 + return spi_setup(host->spi);
205 +mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
209 + if (len > sizeof(*host->data)) {
214 + host->status.len = len;
217 + dma_sync_single_for_device(host->dma_dev,
218 + host->data_dma, sizeof(*host->data),
221 + status = spi_sync(host->spi, &host->readback);
224 + dma_sync_single_for_cpu(host->dma_dev,
225 + host->data_dma, sizeof(*host->data),
232 +mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
234 + u8 *cp = host->data->status;
236 + timeout = ktime_add(timeout, ktime_get());
242 + status = mmc_spi_readbytes(host, n);
246 + for (i = 0; i < n; i++) {
251 + /* REVISIT investigate msleep() to avoid busy-wait I/O
252 + * in at least some cases.
254 + if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
261 +mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
263 + return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
266 +static int mmc_spi_readtoken(struct mmc_spi_host *host)
268 + return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
273 + * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
274 + * hosts return! The low byte holds R1_SPI bits. The next byte may hold
275 + * R2_SPI bits ... for SEND_STATUS, or after data read errors.
277 + * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
278 + * newer cards R7 (IF_COND).
281 +static char *maptype(struct mmc_command *cmd)
283 + switch (mmc_spi_resp_type(cmd)) {
284 + case MMC_RSP_SPI_R1: return "R1";
285 + case MMC_RSP_SPI_R1B: return "R1B";
286 + case MMC_RSP_SPI_R2: return "R2/R5";
287 + case MMC_RSP_SPI_R3: return "R3/R4/R7";
288 + default: return "?";
292 +/* return zero, else negative errno after setting cmd->error */
293 +static int mmc_spi_response_get(struct mmc_spi_host *host,
294 + struct mmc_command *cmd, int cs_on)
296 + u8 *cp = host->data->status;
297 + u8 *end = cp + host->t.len;
301 + snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
302 + cmd->opcode, maptype(cmd));
304 + /* Except for data block reads, the whole response will already
305 + * be stored in the scratch buffer. It's somewhere after the
306 + * command and the first byte we read after it. We ignore that
307 + * first byte. After STOP_TRANSMISSION command it may include
308 + * two data bits, but otherwise it's all ones.
311 + while (cp < end && *cp == 0xff)
314 + /* Data block reads (R1 response types) may need more data... */
318 + cp = host->data->status;
320 + /* Card sends N(CR) (== 1..8) bytes of all-ones then one
321 + * status byte ... and we already scanned 2 bytes.
323 + * REVISIT block read paths use nasty byte-at-a-time I/O
324 + * so it can always DMA directly into the target buffer.
325 + * It'd probably be better to memcpy() the first chunk and
326 + * avoid extra i/o calls...
328 + for (i = 2; i < 9; i++) {
329 + value = mmc_spi_readbytes(host, 1);
335 + value = -ETIMEDOUT;
341 + dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
347 + cmd->resp[0] = *cp++;
350 + /* Status byte: the entire seven-bit R1 response. */
351 + if (cmd->resp[0] != 0) {
352 + if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS
353 + | R1_SPI_ILLEGAL_COMMAND)
356 + else if (R1_SPI_COM_CRC & cmd->resp[0])
358 + else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
361 + /* else R1_SPI_IDLE, "it's resetting" */
364 + switch (mmc_spi_resp_type(cmd)) {
366 + /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
367 + * and less-common stuff like various erase operations.
369 + case MMC_RSP_SPI_R1B:
370 + /* maybe we read all the busy tokens already */
371 + while (cp < end && *cp == 0)
374 + mmc_spi_wait_unbusy(host, r1b_timeout);
377 + /* SPI R2 == R1 + second status byte; SEND_STATUS
378 + * SPI R5 == R1 + data byte; IO_RW_DIRECT
380 + case MMC_RSP_SPI_R2:
381 + cmd->resp[0] |= *cp << 8;
384 + /* SPI R3, R4, or R7 == R1 + 4 bytes */
385 + case MMC_RSP_SPI_R3:
386 + cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
389 + /* SPI R1 == just one status byte */
390 + case MMC_RSP_SPI_R1:
394 + dev_dbg(&host->spi->dev, "bad response type %04x\n",
395 + mmc_spi_resp_type(cmd));
402 + dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
403 + tag, cmd->resp[0], cmd->resp[1]);
405 + /* disable chipselect on errors and some success cases */
406 + if (value >= 0 && cs_on)
410 + cmd->error = value;
415 +/* Issue command and read its response.
416 + * Returns zero on success, negative for error.
418 + * On error, caller must cope with mmc core retry mechanism. That
419 + * means immediate low-level resubmit, which affects the bus lock...
422 +mmc_spi_command_send(struct mmc_spi_host *host,
423 + struct mmc_request *mrq,
424 + struct mmc_command *cmd, int cs_on)
426 + struct scratch *data = host->data;
427 + u8 *cp = data->status;
428 + u32 arg = cmd->arg;
430 + struct spi_transfer *t;
432 + /* We can handle most commands (except block reads) in one full
433 + * duplex I/O operation before either starting the next transfer
434 + * (data block or command) or else deselecting the card.
436 + * First, write 7 bytes:
437 + * - an all-ones byte to ensure the card is ready
438 + * - opcode byte (plus start and transmission bits)
439 + * - four bytes of big-endian argument
440 + * - crc7 (plus end bit) ... always computed, it's cheap
442 + * We init the whole buffer to all-ones, which is what we need
443 + * to write while we're reading (later) response data.
445 + memset(cp++, 0xff, sizeof(data->status));
447 + *cp++ = 0x40 | cmd->opcode;
448 + *cp++ = (u8)(arg >> 24);
449 + *cp++ = (u8)(arg >> 16);
450 + *cp++ = (u8)(arg >> 8);
452 + *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
454 + /* Then, read up to 13 bytes (while writing all-ones):
455 + * - N(CR) (== 1..8) bytes of all-ones
456 + * - status byte (for all response types)
457 + * - the rest of the response, either:
458 + * + nothing, for R1 or R1B responses
459 + * + second status byte, for R2 responses
460 + * + four data bytes, for R3 and R7 responses
462 + * Finally, read some more bytes ... in the nice cases we know in
463 + * advance how many, and reading 1 more is always OK:
464 + * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
465 + * - N(RC) (== 1..N) bytes of all-ones, before next command
466 + * - N(WR) (== 1..N) bytes of all-ones, before data write
468 + * So in those cases one full duplex I/O of at most 21 bytes will
469 + * handle the whole command, leaving the card ready to receive a
470 + * data block or new command. We do that whenever we can, shaving
471 + * CPU and IRQ costs (especially when using DMA or FIFOs).
473 + * There are two other cases, where it's not generally practical
474 + * to rely on a single I/O:
476 + * - R1B responses need at least N(EC) bytes of all-zeroes.
478 + * In this case we can *try* to fit it into one I/O, then
479 + * maybe read more data later.
481 + * - Data block reads are more troublesome, since a variable
482 + * number of padding bytes precede the token and data.
483 + * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
484 + * + N(AC) (== 1..many) bytes of all-ones
486 + * In this case we currently only have minimal speedups here:
487 + * when N(CR) == 1 we can avoid I/O in response_get().
489 + if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
490 + cp += 2; /* min(N(CR)) + status */
493 + cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
494 + if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
496 + else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
498 + else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
499 + cp = data->status + sizeof(data->status);
500 + /* else: R1 (most commands) */
503 + dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
504 + cmd->opcode, maptype(cmd));
506 + /* send command, leaving chipselect active */
507 + spi_message_init(&host->m);
510 + memset(t, 0, sizeof(*t));
511 + t->tx_buf = t->rx_buf = data->status;
512 + t->tx_dma = t->rx_dma = host->data_dma;
513 + t->len = cp - data->status;
515 + spi_message_add_tail(t, &host->m);
517 + if (host->dma_dev) {
518 + host->m.is_dma_mapped = 1;
519 + dma_sync_single_for_device(host->dma_dev,
520 + host->data_dma, sizeof(*host->data),
521 + DMA_BIDIRECTIONAL);
523 + status = spi_sync(host->spi, &host->m);
526 + dma_sync_single_for_cpu(host->dma_dev,
527 + host->data_dma, sizeof(*host->data),
528 + DMA_BIDIRECTIONAL);
530 + dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
531 + cmd->error = status;
535 + /* after no-data commands and STOP_TRANSMISSION, chipselect off */
536 + return mmc_spi_response_get(host, cmd, cs_on);
539 +/* Build data message with up to four separate transfers. For TX, we
540 + * start by writing the data token. And in most cases, we finish with
541 + * a status transfer.
543 + * We always provide TX data for data and CRC. The MMC/SD protocol
544 + * requires us to write ones; but Linux defaults to writing zeroes;
545 + * so we explicitly initialize it to all ones on RX paths.
547 + * We also handle DMA mapping, so the underlying SPI controller does
548 + * not need to (re)do it for each message.
551 +mmc_spi_setup_data_message(
552 + struct mmc_spi_host *host,
554 + enum dma_data_direction direction)
556 + struct spi_transfer *t;
557 + struct scratch *scratch = host->data;
558 + dma_addr_t dma = host->data_dma;
560 + spi_message_init(&host->m);
562 + host->m.is_dma_mapped = 1;
564 + /* for reads, readblock() skips 0xff bytes before finding
565 + * the token; for writes, this transfer issues that token.
567 + if (direction == DMA_TO_DEVICE) {
569 + memset(t, 0, sizeof(*t));
572 + scratch->data_token = SPI_TOKEN_MULTI_WRITE;
574 + scratch->data_token = SPI_TOKEN_SINGLE;
575 + t->tx_buf = &scratch->data_token;
577 + t->tx_dma = dma + offsetof(struct scratch, data_token);
578 + spi_message_add_tail(t, &host->m);
581 + /* Body of transfer is buffer, then CRC ...
582 + * either TX-only, or RX with TX-ones.
585 + memset(t, 0, sizeof(*t));
586 + t->tx_buf = host->ones;
587 + t->tx_dma = host->ones_dma;
588 + /* length and actual buffer info are written later */
589 + spi_message_add_tail(t, &host->m);
592 + memset(t, 0, sizeof(*t));
594 + if (direction == DMA_TO_DEVICE) {
595 + /* the actual CRC may get written later */
596 + t->tx_buf = &scratch->crc_val;
598 + t->tx_dma = dma + offsetof(struct scratch, crc_val);
600 + t->tx_buf = host->ones;
601 + t->tx_dma = host->ones_dma;
602 + t->rx_buf = &scratch->crc_val;
604 + t->rx_dma = dma + offsetof(struct scratch, crc_val);
606 + spi_message_add_tail(t, &host->m);
609 + * A single block read is followed by N(EC) [0+] all-ones bytes
610 + * before deselect ... don't bother.
612 + * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
613 + * the next block is read, or a STOP_TRANSMISSION is issued. We'll
614 + * collect that single byte, so readblock() doesn't need to.
616 + * For a write, the one-byte data response follows immediately, then
617 + * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
618 + * Then single block reads may deselect, and multiblock ones issue
619 + * the next token (next data block, or STOP_TRAN). We can try to
620 + * minimize I/O ops by using a single read to collect end-of-busy.
622 + if (multiple || direction == DMA_TO_DEVICE) {
623 + t = &host->early_status;
624 + memset(t, 0, sizeof(*t));
625 + t->len = (direction == DMA_TO_DEVICE)
626 + ? sizeof(scratch->status)
628 + t->tx_buf = host->ones;
629 + t->tx_dma = host->ones_dma;
630 + t->rx_buf = scratch->status;
632 + t->rx_dma = dma + offsetof(struct scratch, status);
634 + spi_message_add_tail(t, &host->m);
640 + * - caller handled preceding N(WR) [1+] all-ones bytes
645 + * - an all-ones byte ... card writes a data-response byte
646 + * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
648 + * Return negative errno, else success.
651 +mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
653 + struct spi_device *spi = host->spi;
655 + struct scratch *scratch = host->data;
657 + if (host->mmc->use_spi_crc)
658 + scratch->crc_val = cpu_to_be16(
659 + crc_itu_t(0, t->tx_buf, t->len));
661 + dma_sync_single_for_device(host->dma_dev,
662 + host->data_dma, sizeof(*scratch),
663 + DMA_BIDIRECTIONAL);
665 + status = spi_sync(spi, &host->m);
668 + dev_dbg(&spi->dev, "write error (%d)\n", status);
673 + dma_sync_single_for_cpu(host->dma_dev,
674 + host->data_dma, sizeof(*scratch),
675 + DMA_BIDIRECTIONAL);
678 + * Get the transmission data-response reply. It must follow
679 + * immediately after the data block we transferred. This reply
680 + * doesn't necessarily tell whether the write operation succeeded;
681 + * it just says if the transmission was ok and whether *earlier*
682 + * writes succeeded; see the standard.
684 + switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
685 + case SPI_RESPONSE_ACCEPTED:
688 + case SPI_RESPONSE_CRC_ERR:
689 + /* host shall then issue MMC_STOP_TRANSMISSION */
692 + case SPI_RESPONSE_WRITE_ERR:
693 + /* host shall then issue MMC_STOP_TRANSMISSION,
694 + * and should MMC_SEND_STATUS to sort it out
703 + dev_dbg(&spi->dev, "write error %02x (%d)\n",
704 + scratch->status[0], status);
708 + t->tx_buf += t->len;
710 + t->tx_dma += t->len;
712 + /* Return when not busy. If we didn't collect that status yet,
713 + * we'll need some more I/O.
715 + for (i = 1; i < sizeof(scratch->status); i++) {
716 + if (scratch->status[i] != 0)
719 + return mmc_spi_wait_unbusy(host, writeblock_timeout);
724 + * - skip leading all-ones bytes ... either
725 + * + N(AC) [1..f(clock,CSD)] usually, else
726 + * + N(CX) [0..8] when reading CSD or CID
728 + * + token ... if error token, no data or crc
732 + * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
733 + * before dropping chipselect.
735 + * For multiblock reads, caller either reads the next block or issues a
736 + * STOP_TRANSMISSION command.
739 +mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
741 + struct spi_device *spi = host->spi;
743 + struct scratch *scratch = host->data;
745 + /* At least one SD card sends an all-zeroes byte when N(CX)
746 + * applies, before the all-ones bytes ... just cope with that.
748 + status = mmc_spi_readbytes(host, 1);
751 + status = scratch->status[0];
752 + if (status == 0xff || status == 0)
753 + status = mmc_spi_readtoken(host);
755 + if (status == SPI_TOKEN_SINGLE) {
756 + if (host->dma_dev) {
757 + dma_sync_single_for_device(host->dma_dev,
758 + host->data_dma, sizeof(*scratch),
759 + DMA_BIDIRECTIONAL);
760 + dma_sync_single_for_device(host->dma_dev,
765 + status = spi_sync(spi, &host->m);
767 + if (host->dma_dev) {
768 + dma_sync_single_for_cpu(host->dma_dev,
769 + host->data_dma, sizeof(*scratch),
770 + DMA_BIDIRECTIONAL);
771 + dma_sync_single_for_cpu(host->dma_dev,
777 + dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
779 + /* we've read extra garbage, timed out, etc */
783 + /* low four bits are an R2 subset, fifth seems to be
784 + * vendor specific ... map them all to generic error..
789 + if (host->mmc->use_spi_crc) {
790 + u16 crc = crc_itu_t(0, t->rx_buf, t->len);
792 + be16_to_cpus(&scratch->crc_val);
793 + if (scratch->crc_val != crc) {
794 + dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
795 + "computed=0x%04x len=%d\n",
796 + scratch->crc_val, crc, t->len);
801 + t->rx_buf += t->len;
803 + t->rx_dma += t->len;
809 + * An MMC/SD data stage includes one or more blocks, optional CRCs,
810 + * and inline handshaking. That handhaking makes it unlike most
811 + * other SPI protocol stacks.
814 +mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
815 + struct mmc_data *data, u32 blk_size)
817 + struct spi_device *spi = host->spi;
818 + struct device *dma_dev = host->dma_dev;
819 + struct spi_transfer *t;
820 + enum dma_data_direction direction;
821 + struct scatterlist *sg;
823 + int multiple = (data->blocks > 1);
825 + if (data->flags & MMC_DATA_READ)
826 + direction = DMA_FROM_DEVICE;
828 + direction = DMA_TO_DEVICE;
829 + mmc_spi_setup_data_message(host, multiple, direction);
832 + /* Handle scatterlist segments one at a time, with synch for
833 + * each 512-byte block
835 + for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
837 + dma_addr_t dma_addr = 0;
839 + unsigned length = sg->length;
840 + enum dma_data_direction dir = direction;
842 + /* set up dma mapping for controller drivers that might
843 + * use DMA ... though they may fall back to PIO
846 + /* never invalidate whole *shared* pages ... */
847 + if ((sg->offset != 0 || length != PAGE_SIZE)
848 + && dir == DMA_FROM_DEVICE)
849 + dir = DMA_BIDIRECTIONAL;
851 + dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
853 + if (direction == DMA_TO_DEVICE)
854 + t->tx_dma = dma_addr + sg->offset;
856 + t->rx_dma = dma_addr + sg->offset;
859 + /* allow pio too; we don't allow highmem */
860 + kmap_addr = kmap(sg_page(sg));
861 + if (direction == DMA_TO_DEVICE)
862 + t->tx_buf = kmap_addr + sg->offset;
864 + t->rx_buf = kmap_addr + sg->offset;
866 + /* transfer each block, and update request status */
868 + t->len = min(length, blk_size);
870 + dev_dbg(&host->spi->dev,
871 + " mmc_spi: %s block, %d bytes\n",
872 + (direction == DMA_TO_DEVICE)
877 + if (direction == DMA_TO_DEVICE)
878 + status = mmc_spi_writeblock(host, t);
880 + status = mmc_spi_readblock(host, t);
884 + data->bytes_xfered += t->len;
891 + /* discard mappings */
892 + if (direction == DMA_FROM_DEVICE)
893 + flush_kernel_dcache_page(sg_page(sg));
894 + kunmap(sg_page(sg));
896 + dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
899 + data->error = status;
900 + dev_dbg(&spi->dev, "%s status %d\n",
901 + (direction == DMA_TO_DEVICE)
902 + ? "write" : "read",
908 + /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
909 + * can be issued before multiblock writes. Unlike its more widely
910 + * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
911 + * that can affect the STOP_TRAN logic. Complete (and current)
912 + * MMC specs should sort that out before Linux starts using CMD23.
914 + if (direction == DMA_TO_DEVICE && multiple) {
915 + struct scratch *scratch = host->data;
917 + const unsigned statlen = sizeof(scratch->status);
919 + dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
921 + /* Tweak the per-block message we set up earlier by morphing
922 + * it to hold single buffer with the token followed by some
923 + * all-ones bytes ... skip N(BR) (0..1), scan the rest for
924 + * "not busy any longer" status, and leave chip selected.
926 + INIT_LIST_HEAD(&host->m.transfers);
927 + list_add(&host->early_status.transfer_list,
928 + &host->m.transfers);
930 + memset(scratch->status, 0xff, statlen);
931 + scratch->status[0] = SPI_TOKEN_STOP_TRAN;
933 + host->early_status.tx_buf = host->early_status.rx_buf;
934 + host->early_status.tx_dma = host->early_status.rx_dma;
935 + host->early_status.len = statlen;
938 + dma_sync_single_for_device(host->dma_dev,
939 + host->data_dma, sizeof(*scratch),
940 + DMA_BIDIRECTIONAL);
942 + tmp = spi_sync(spi, &host->m);
945 + dma_sync_single_for_cpu(host->dma_dev,
946 + host->data_dma, sizeof(*scratch),
947 + DMA_BIDIRECTIONAL);
955 + /* Ideally we collected "not busy" status with one I/O,
956 + * avoiding wasteful byte-at-a-time scanning... but more
957 + * I/O is often needed.
959 + for (tmp = 2; tmp < statlen; tmp++) {
960 + if (scratch->status[tmp] != 0)
963 + tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
964 + if (tmp < 0 && !data->error)
969 +/****************************************************************************/
972 + * MMC driver implementation -- the interface to the MMC stack
975 +static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
977 + struct mmc_spi_host *host = mmc_priv(mmc);
978 + int status = -EINVAL;
981 + /* MMC core and layered drivers *MUST* issue SPI-aware commands */
983 + struct mmc_command *cmd;
987 + if (!mmc_spi_resp_type(cmd)) {
988 + dev_dbg(&host->spi->dev, "bogus command\n");
989 + cmd->error = -EINVAL;
994 + if (cmd && !mmc_spi_resp_type(cmd)) {
995 + dev_dbg(&host->spi->dev, "bogus STOP command\n");
996 + cmd->error = -EINVAL;
1002 + mmc_request_done(host->mmc, mrq);
1008 + /* issue command; then optionally data and stop */
1009 + status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
1010 + if (status == 0 && mrq->data) {
1011 + mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
1013 + status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1018 + mmc_request_done(host->mmc, mrq);
1021 +/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1023 + * NOTE that here we can't know that the card has just been powered up;
1024 + * not all MMC/SD sockets support power switching.
1026 + * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1027 + * this doesn't seem to do the right thing at all...
1029 +static void mmc_spi_initsequence(struct mmc_spi_host *host)
1031 + /* Try to be very sure any previous command has completed;
1032 + * wait till not-busy, skip debris from any old commands.
1034 + mmc_spi_wait_unbusy(host, r1b_timeout);
1035 + mmc_spi_readbytes(host, 10);
1038 + * Do a burst with chipselect active-high. We need to do this to
1039 + * meet the requirement of 74 clock cycles with both chipselect
1040 + * and CMD (MOSI) high before CMD0 ... after the card has been
1041 + * powered up to Vdd(min), and so is ready to take commands.
1043 + * Some cards are particularly needy of this (e.g. Viking "SD256")
1044 + * while most others don't seem to care.
1046 + * Note that this is one of the places MMC/SD plays games with the
1047 + * SPI protocol. Another is that when chipselect is released while
1048 + * the card returns BUSY status, the clock must issue several cycles
1049 + * with chipselect high before the card will stop driving its output.
1051 + host->spi->mode |= SPI_CS_HIGH;
1052 + if (spi_setup(host->spi) != 0) {
1053 + /* Just warn; most cards work without it. */
1054 + dev_warn(&host->spi->dev,
1055 + "can't change chip-select polarity\n");
1056 + host->spi->mode &= ~SPI_CS_HIGH;
1058 + mmc_spi_readbytes(host, 18);
1060 + host->spi->mode &= ~SPI_CS_HIGH;
1061 + if (spi_setup(host->spi) != 0) {
1062 + /* Wot, we can't get the same setup we had before? */
1063 + dev_err(&host->spi->dev,
1064 + "can't restore chip-select polarity\n");
1069 +static char *mmc_powerstring(u8 power_mode)
1071 + switch (power_mode) {
1072 + case MMC_POWER_OFF: return "off";
1073 + case MMC_POWER_UP: return "up";
1074 + case MMC_POWER_ON: return "on";
1079 +static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1081 + struct mmc_spi_host *host = mmc_priv(mmc);
1083 + if (host->power_mode != ios->power_mode) {
1086 + canpower = host->pdata && host->pdata->setpower;
1088 + dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1089 + mmc_powerstring(ios->power_mode),
1091 + canpower ? ", can switch" : "");
1093 + /* switch power on/off if possible, accounting for
1094 + * max 250msec powerup time if needed.
1097 + switch (ios->power_mode) {
1098 + case MMC_POWER_OFF:
1099 + case MMC_POWER_UP:
1100 + host->pdata->setpower(&host->spi->dev,
1102 + if (ios->power_mode == MMC_POWER_UP)
1103 + msleep(host->powerup_msecs);
1107 + /* See 6.4.1 in the simplified SD card physical spec 2.0 */
1108 + if (ios->power_mode == MMC_POWER_ON)
1109 + mmc_spi_initsequence(host);
1111 + /* If powering down, ground all card inputs to avoid power
1112 + * delivery from data lines! On a shared SPI bus, this
1113 + * will probably be temporary; 6.4.2 of the simplified SD
1114 + * spec says this must last at least 1msec.
1116 + * - Clock low means CPOL 0, e.g. mode 0
1117 + * - MOSI low comes from writing zero
1118 + * - Chipselect is usually active low...
1120 + if (canpower && ios->power_mode == MMC_POWER_OFF) {
1123 + host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1124 + mres = spi_setup(host->spi);
1126 + dev_dbg(&host->spi->dev,
1127 + "switch to SPI mode 0 failed\n");
1129 + if (spi_w8r8(host->spi, 0x00) < 0)
1130 + dev_dbg(&host->spi->dev,
1131 + "put spi signals to low failed\n");
1134 + * Now clock should be low due to spi mode 0;
1135 + * MOSI should be low because of written 0x00;
1136 + * chipselect should be low (it is active low)
1137 + * power supply is off, so now MMC is off too!
1139 + * FIXME no, chipselect can be high since the
1140 + * device is inactive and SPI_CS_HIGH is clear...
1144 + host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1145 + mres = spi_setup(host->spi);
1147 + dev_dbg(&host->spi->dev,
1148 + "switch back to SPI mode 3"
1153 + host->power_mode = ios->power_mode;
1156 + if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1159 + host->spi->max_speed_hz = ios->clock;
1160 + status = spi_setup(host->spi);
1161 + dev_dbg(&host->spi->dev,
1162 + "mmc_spi: clock to %d Hz, %d\n",
1163 + host->spi->max_speed_hz, status);
1167 +static int mmc_spi_get_ro(struct mmc_host *mmc)
1169 + struct mmc_spi_host *host = mmc_priv(mmc);
1171 + if (host->pdata && host->pdata->get_ro)
1172 + return host->pdata->get_ro(mmc->parent);
1173 + /* board doesn't support read only detection; assume writeable */
1178 +static const struct mmc_host_ops mmc_spi_ops = {
1179 + .request = mmc_spi_request,
1180 + .set_ios = mmc_spi_set_ios,
1181 + .get_ro = mmc_spi_get_ro,
1185 +/****************************************************************************/
1188 + * SPI driver implementation
1192 +mmc_spi_detect_irq(int irq, void *mmc)
1194 + struct mmc_spi_host *host = mmc_priv(mmc);
1195 + u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1197 + mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1198 + return IRQ_HANDLED;
1201 +struct count_children {
1203 + struct bus_type *bus;
1206 +static int maybe_count_child(struct device *dev, void *c)
1208 + struct count_children *ccp = c;
1210 + if (dev->bus == ccp->bus) {
1218 +static int mmc_spi_probe(struct spi_device *spi)
1221 + struct mmc_host *mmc;
1222 + struct mmc_spi_host *host;
1225 + /* MMC and SD specs only seem to care that sampling is on the
1226 + * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
1227 + * should be legit. We'll use mode 0 since it seems to be a
1228 + * bit less troublesome on some hardware ... unclear why.
1230 + spi->mode = SPI_MODE_0;
1231 + spi->bits_per_word = 8;
1233 + status = spi_setup(spi);
1235 + dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1236 + spi->mode, spi->max_speed_hz / 1000,
1241 + /* We can use the bus safely iff nobody else will interfere with us.
1242 + * Most commands consist of one SPI message to issue a command, then
1243 + * several more to collect its response, then possibly more for data
1244 + * transfer. Clocking access to other devices during that period will
1245 + * corrupt the command execution.
1247 + * Until we have software primitives which guarantee non-interference,
1248 + * we'll aim for a hardware-level guarantee.
1250 + * REVISIT we can't guarantee another device won't be added later...
1252 + if (spi->master->num_chipselect > 1) {
1253 + struct count_children cc;
1256 + cc.bus = spi->dev.bus;
1257 + status = device_for_each_child(spi->dev.parent, &cc,
1258 + maybe_count_child);
1260 + dev_err(&spi->dev, "can't share SPI bus\n");
1264 + dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
1267 + /* We need a supply of ones to transmit. This is the only time
1268 + * the CPU touches these, so cache coherency isn't a concern.
1270 + * NOTE if many systems use more than one MMC-over-SPI connector
1271 + * it'd save some memory to share this. That's evidently rare.
1274 + ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1277 + memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1279 + mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1283 + mmc->ops = &mmc_spi_ops;
1284 + mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1286 + /* As long as we keep track of the number of successfully
1287 + * transmitted blocks, we're good for multiwrite.
1289 + mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1291 + /* SPI doesn't need the lowspeed device identification thing for
1292 + * MMC or SD cards, since it never comes up in open drain mode.
1293 + * That's good; some SPI masters can't handle very low speeds!
1295 + * However, low speed SDIO cards need not handle over 400 KHz;
1296 + * that's the only reason not to use a few MHz for f_min (until
1297 + * the upper layer reads the target frequency from the CSD).
1299 + mmc->f_min = 400000;
1300 + mmc->f_max = spi->max_speed_hz;
1302 + host = mmc_priv(mmc);
1306 + host->ones = ones;
1308 + /* Platform data is used to hook up things like card sensing
1309 + * and power switching gpios.
1311 + host->pdata = spi->dev.platform_data;
1313 + mmc->ocr_avail = host->pdata->ocr_mask;
1314 + if (!mmc->ocr_avail) {
1315 + dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1316 + mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1318 + if (host->pdata && host->pdata->setpower) {
1319 + host->powerup_msecs = host->pdata->powerup_msecs;
1320 + if (!host->powerup_msecs || host->powerup_msecs > 250)
1321 + host->powerup_msecs = 250;
1324 + dev_set_drvdata(&spi->dev, mmc);
1326 + /* preallocate dma buffers */
1327 + host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1333 + if (spi->master->dev.parent->dma_mask) {
1334 + struct device *dev = spi->master->dev.parent;
1336 + host->dma_dev = dev;
1337 + host->ones_dma = dma_map_single(dev, ones,
1338 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1339 + host->data_dma = dma_map_single(dev, host->data,
1340 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1342 + /* REVISIT in theory those map operations can fail... */
1344 + dma_sync_single_for_cpu(host->dma_dev,
1345 + host->data_dma, sizeof(*host->data),
1346 + DMA_BIDIRECTIONAL);
1350 + /* setup message for status/busy readback */
1351 + spi_message_init(&host->readback);
1352 + host->readback.is_dma_mapped = (host->dma_dev != NULL);
1354 + spi_message_add_tail(&host->status, &host->readback);
1355 + host->status.tx_buf = host->ones;
1356 + host->status.tx_dma = host->ones_dma;
1357 + host->status.rx_buf = &host->data->status;
1358 + host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1359 + host->status.cs_change = 1;
1361 + /* register card detect irq */
1362 + if (host->pdata && host->pdata->init) {
1363 + status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1365 + goto fail_glue_init;
1368 + status = mmc_add_host(mmc);
1370 + goto fail_add_host;
1372 + dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
1373 + mmc->class_dev.bus_id,
1374 + host->dma_dev ? "" : ", no DMA",
1375 + (host->pdata && host->pdata->get_ro)
1377 + (host->pdata && host->pdata->setpower)
1378 + ? "" : ", no poweroff");
1382 + mmc_remove_host (mmc);
1384 + if (host->dma_dev)
1385 + dma_unmap_single(host->dma_dev, host->data_dma,
1386 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1387 + kfree(host->data);
1390 + mmc_free_host(mmc);
1391 + dev_set_drvdata(&spi->dev, NULL);
1399 +static int __devexit mmc_spi_remove(struct spi_device *spi)
1401 + struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1402 + struct mmc_spi_host *host;
1405 + host = mmc_priv(mmc);
1407 + /* prevent new mmc_detect_change() calls */
1408 + if (host->pdata && host->pdata->exit)
1409 + host->pdata->exit(&spi->dev, mmc);
1411 + mmc_remove_host(mmc);
1413 + if (host->dma_dev) {
1414 + dma_unmap_single(host->dma_dev, host->ones_dma,
1415 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1416 + dma_unmap_single(host->dma_dev, host->data_dma,
1417 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1420 + kfree(host->data);
1421 + kfree(host->ones);
1423 + spi->max_speed_hz = mmc->f_max;
1424 + mmc_free_host(mmc);
1425 + dev_set_drvdata(&spi->dev, NULL);
1431 +static struct spi_driver mmc_spi_driver = {
1433 + .name = "mmc_spi",
1434 + .bus = &spi_bus_type,
1435 + .owner = THIS_MODULE,
1437 + .probe = mmc_spi_probe,
1438 + .remove = __devexit_p(mmc_spi_remove),
1442 +static int __init mmc_spi_init(void)
1444 + return spi_register_driver(&mmc_spi_driver);
1446 +module_init(mmc_spi_init);
1449 +static void __exit mmc_spi_exit(void)
1451 + spi_unregister_driver(&mmc_spi_driver);
1453 +module_exit(mmc_spi_exit);
1456 +MODULE_AUTHOR("Mike Lavender, David Brownell, "
1457 + "Hans-Peter Nilsson, Jan Nikitenko");
1458 +MODULE_DESCRIPTION("SPI SD/MMC host driver");
1459 +MODULE_LICENSE("GPL");
1460 Index: linux-2.6.23.16/include/linux/spi/mmc_spi.h
1461 ===================================================================
1462 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1463 +++ linux-2.6.23.16/include/linux/spi/mmc_spi.h 2008-03-21 17:30:25.000000000 +0100
1465 +#ifndef __LINUX_SPI_MMC_SPI_H
1466 +#define __LINUX_SPI_MMC_SPI_H
1471 +/* Put this in platform_data of a device being used to manage an MMC/SD
1472 + * card slot. (Modeled after PXA mmc glue; see that for usage examples.)
1474 + * REVISIT This is not a spi-specific notion. Any card slot should be
1475 + * able to handle it. If the MMC core doesn't adopt this kind of notion,
1476 + * switch the "struct device *" parameters over to "struct spi_device *".
1478 +struct mmc_spi_platform_data {
1479 + /* driver activation and (optional) card detect irq hookup */
1480 + int (*init)(struct device *,
1481 + irqreturn_t (*)(int, void *),
1483 + void (*exit)(struct device *, void *);
1485 + /* sense switch on sd cards */
1486 + int (*get_ro)(struct device *);
1488 + /* how long to debounce card detect, in msecs */
1491 + /* power management */
1492 + u16 powerup_msecs; /* delay of up to 250 msec */
1493 + u32 ocr_mask; /* available voltages */
1494 + void (*setpower)(struct device *, unsigned int maskval);
1497 +#endif /* __LINUX_SPI_MMC_SPI_H */
1498 Index: linux-2.6.23.16/drivers/mmc/core/bus.c
1499 ===================================================================
1500 --- linux-2.6.23.16.orig/drivers/mmc/core/bus.c 2008-03-21 17:28:26.000000000 +0100
1501 +++ linux-2.6.23.16/drivers/mmc/core/bus.c 2008-03-21 17:30:25.000000000 +0100
1506 +#include "sdio_cis.h"
1509 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
1510 @@ -34,6 +35,8 @@ static ssize_t mmc_type_show(struct devi
1511 return sprintf(buf, "MMC\n");
1513 return sprintf(buf, "SD\n");
1514 + case MMC_TYPE_SDIO:
1515 + return sprintf(buf, "SDIO\n");
1519 @@ -55,36 +58,37 @@ static int mmc_bus_match(struct device *
1523 -mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
1525 +mmc_bus_uevent(struct device *dev, char **envp,
1526 + int num_envp, char *buffer, int buffer_size)
1528 struct mmc_card *card = dev_to_mmc_card(dev);
1529 - int retval = 0, i = 0, length = 0;
1531 -#define add_env(fmt,val) do { \
1532 - retval = add_uevent_var(envp, num_envp, &i, \
1533 - buf, buf_size, &length, \
1540 + int i = 0, len = 0;
1542 switch (card->type) {
1544 - add_env("MMC_TYPE=%s", "MMC");
1548 - add_env("MMC_TYPE=%s", "SD");
1551 + case MMC_TYPE_SDIO:
1558 - add_env("MMC_NAME=%s", mmc_card_name(card));
1562 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_TYPE=%s", type);
1568 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_NAME=%s", mmc_card_name(card));
1574 static int mmc_bus_probe(struct device *dev)
1575 @@ -176,6 +180,11 @@ static void mmc_release_card(struct devi
1577 struct mmc_card *card = dev_to_mmc_card(dev);
1579 + sdio_free_common_cis(card);
1582 + kfree(card->info);
1587 @@ -221,15 +230,25 @@ int mmc_add_card(struct mmc_card *card)
1588 if (mmc_card_blockaddr(card))
1591 + case MMC_TYPE_SDIO:
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 " : "",
1603 + if (mmc_host_is_spi(card->host)) {
1604 + printk(KERN_INFO "%s: new %s%s card on SPI\n",
1605 + mmc_hostname(card->host),
1606 + mmc_card_highspeed(card) ? "high speed " : "",
1609 + printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1610 + mmc_hostname(card->host),
1611 + mmc_card_highspeed(card) ? "high speed " : "",
1615 card->dev.uevent_suppress = 1;
1617 @@ -261,8 +280,13 @@ int mmc_add_card(struct mmc_card *card)
1618 void mmc_remove_card(struct mmc_card *card)
1620 if (mmc_card_present(card)) {
1621 - printk(KERN_INFO "%s: card %04x removed\n",
1622 - mmc_hostname(card->host), card->rca);
1623 + if (mmc_host_is_spi(card->host)) {
1624 + printk(KERN_INFO "%s: SPI card removed\n",
1625 + mmc_hostname(card->host));
1627 + printk(KERN_INFO "%s: card %04x removed\n",
1628 + mmc_hostname(card->host), card->rca);
1631 if (card->host->bus_ops->sysfs_remove)
1632 card->host->bus_ops->sysfs_remove(card->host, card);
1633 Index: linux-2.6.23.16/drivers/mmc/core/core.c
1634 ===================================================================
1635 --- linux-2.6.23.16.orig/drivers/mmc/core/core.c 2008-03-21 17:28:26.000000000 +0100
1636 +++ linux-2.6.23.16/drivers/mmc/core/core.c 2008-03-21 17:30:25.000000000 +0100
1638 #include <linux/delay.h>
1639 #include <linux/pagemap.h>
1640 #include <linux/err.h>
1641 -#include <asm/scatterlist.h>
1642 +#include <linux/leds.h>
1643 #include <linux/scatterlist.h>
1645 #include <linux/mmc/card.h>
1650 +#include "sdio_bus.h"
1652 #include "mmc_ops.h"
1654 +#include "sdio_ops.h"
1656 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
1657 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
1658 +extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1660 static struct workqueue_struct *workqueue;
1663 + * Enabling software CRCs on the data blocks can be a significant (30%)
1664 + * performance cost, and for other reasons may not always be desired.
1665 + * So we allow it it to be disabled.
1667 +int use_spi_crc = 1;
1668 +module_param(use_spi_crc, bool, 0);
1671 * Internal function. Schedule delayed work in the MMC work queue.
1673 static int mmc_schedule_delayed_work(struct delayed_work *work,
1674 @@ -68,6 +79,11 @@ void mmc_request_done(struct mmc_host *h
1675 struct mmc_command *cmd = mrq->cmd;
1676 int err = cmd->error;
1678 + if (err && cmd->retries && mmc_host_is_spi(host)) {
1679 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
1683 if (err && cmd->retries) {
1684 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
1685 mmc_hostname(host), cmd->opcode, err);
1686 @@ -76,6 +92,8 @@ void mmc_request_done(struct mmc_host *h
1688 host->ops->request(host, mrq);
1690 + led_trigger_event(host->led, LED_OFF);
1692 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
1693 mmc_hostname(host), cmd->opcode, err,
1694 cmd->resp[0], cmd->resp[1],
1695 @@ -118,7 +136,7 @@ mmc_start_request(struct mmc_host *host,
1696 "tsac %d ms nsac %d\n",
1697 mmc_hostname(host), mrq->data->blksz,
1698 mrq->data->blocks, mrq->data->flags,
1699 - mrq->data->timeout_ns / 10000000,
1700 + mrq->data->timeout_ns / 1000000,
1701 mrq->data->timeout_clks);
1704 @@ -130,6 +148,8 @@ mmc_start_request(struct mmc_host *host,
1706 WARN_ON(!host->claimed);
1708 + led_trigger_event(host->led, LED_FULL);
1710 mrq->cmd->error = 0;
1711 mrq->cmd->mrq = mrq;
1713 @@ -199,7 +219,7 @@ int mmc_wait_for_cmd(struct mmc_host *ho
1715 struct mmc_request mrq;
1717 - BUG_ON(!host->claimed);
1718 + WARN_ON(!host->claimed);
1720 memset(&mrq, 0, sizeof(struct mmc_request));
1722 @@ -220,17 +240,24 @@ EXPORT_SYMBOL(mmc_wait_for_cmd);
1723 * mmc_set_data_timeout - set the timeout for a data command
1724 * @data: data phase for command
1725 * @card: the MMC card associated with the data transfer
1726 - * @write: flag to differentiate reads from writes
1728 * Computes the data timeout parameters according to the
1729 * correct algorithm given the card type.
1731 -void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
1733 +void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
1738 + * SDIO cards only define an upper 1 s limit on access.
1740 + if (mmc_card_sdio(card)) {
1741 + data->timeout_ns = 1000000000;
1742 + data->timeout_clks = 0;
1747 * SD cards use a 100 multiplier rather than 10
1749 mult = mmc_card_sd(card) ? 100 : 10;
1750 @@ -239,7 +266,7 @@ void mmc_set_data_timeout(struct mmc_dat
1751 * Scale up the multiplier (and therefore the timeout) by
1752 * the r2w factor for writes.
1755 + if (data->flags & MMC_DATA_WRITE)
1756 mult <<= card->csd.r2w_factor;
1758 data->timeout_ns = card->csd.tacc_ns * mult;
1759 @@ -255,7 +282,7 @@ void mmc_set_data_timeout(struct mmc_dat
1760 timeout_us += data->timeout_clks * 1000 /
1761 (card->host->ios.clock / 1000);
1764 + if (data->flags & MMC_DATA_WRITE)
1768 @@ -272,15 +299,20 @@ void mmc_set_data_timeout(struct mmc_dat
1769 EXPORT_SYMBOL(mmc_set_data_timeout);
1772 - * mmc_claim_host - exclusively claim a host
1773 + * __mmc_claim_host - exclusively claim a host
1774 * @host: mmc host to claim
1775 + * @abort: whether or not the operation should be aborted
1777 - * Claim a host for a set of operations.
1778 + * Claim a host for a set of operations. If @abort is non null and
1779 + * dereference a non-zero value then this will return prematurely with
1780 + * that non-zero value without acquiring the lock. Returns zero
1781 + * with the lock held otherwise.
1783 -void mmc_claim_host(struct mmc_host *host)
1784 +int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1786 DECLARE_WAITQUEUE(wait, current);
1787 unsigned long flags;
1792 @@ -288,19 +320,24 @@ void mmc_claim_host(struct mmc_host *hos
1793 spin_lock_irqsave(&host->lock, flags);
1795 set_current_state(TASK_UNINTERRUPTIBLE);
1796 - if (!host->claimed)
1797 + stop = abort ? atomic_read(abort) : 0;
1798 + if (stop || !host->claimed)
1800 spin_unlock_irqrestore(&host->lock, flags);
1802 spin_lock_irqsave(&host->lock, flags);
1804 set_current_state(TASK_RUNNING);
1805 - host->claimed = 1;
1807 + host->claimed = 1;
1809 + wake_up(&host->wq);
1810 spin_unlock_irqrestore(&host->lock, flags);
1811 remove_wait_queue(&host->wq, &wait);
1815 -EXPORT_SYMBOL(mmc_claim_host);
1816 +EXPORT_SYMBOL(__mmc_claim_host);
1819 * mmc_release_host - release a host
1820 @@ -313,7 +350,7 @@ void mmc_release_host(struct mmc_host *h
1822 unsigned long flags;
1824 - BUG_ON(!host->claimed);
1825 + WARN_ON(!host->claimed);
1827 spin_lock_irqsave(&host->lock, flags);
1829 @@ -433,19 +470,32 @@ static void mmc_power_up(struct mmc_host
1830 int bit = fls(host->ocr_avail) - 1;
1832 host->ios.vdd = bit;
1833 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1834 - host->ios.chip_select = MMC_CS_DONTCARE;
1835 + if (mmc_host_is_spi(host)) {
1836 + host->ios.chip_select = MMC_CS_HIGH;
1837 + host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1839 + host->ios.chip_select = MMC_CS_DONTCARE;
1840 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1842 host->ios.power_mode = MMC_POWER_UP;
1843 host->ios.bus_width = MMC_BUS_WIDTH_1;
1844 host->ios.timing = MMC_TIMING_LEGACY;
1849 + * This delay should be sufficient to allow the power supply
1850 + * to reach the minimum voltage.
1854 host->ios.clock = host->f_min;
1855 host->ios.power_mode = MMC_POWER_ON;
1859 + * This delay must be at least 74 clock sizes, or 1 ms, or the
1860 + * time required to reach a stable voltage.
1865 @@ -453,8 +503,10 @@ static void mmc_power_off(struct mmc_hos
1867 host->ios.clock = 0;
1869 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1870 - host->ios.chip_select = MMC_CS_DONTCARE;
1871 + if (!mmc_host_is_spi(host)) {
1872 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1873 + host->ios.chip_select = MMC_CS_DONTCARE;
1875 host->ios.power_mode = MMC_POWER_OFF;
1876 host->ios.bus_width = MMC_BUS_WIDTH_1;
1877 host->ios.timing = MMC_TIMING_LEGACY;
1878 @@ -511,7 +563,7 @@ void mmc_attach_bus(struct mmc_host *hos
1882 - BUG_ON(!host->claimed);
1883 + WARN_ON(!host->claimed);
1885 spin_lock_irqsave(&host->lock, flags);
1887 @@ -535,8 +587,8 @@ void mmc_detach_bus(struct mmc_host *hos
1891 - BUG_ON(!host->claimed);
1892 - BUG_ON(!host->bus_ops);
1893 + WARN_ON(!host->claimed);
1894 + WARN_ON(!host->bus_ops);
1896 spin_lock_irqsave(&host->lock, flags);
1898 @@ -564,7 +616,7 @@ void mmc_detect_change(struct mmc_host *
1899 #ifdef CONFIG_MMC_DEBUG
1900 unsigned long flags;
1901 spin_lock_irqsave(&host->lock, flags);
1902 - BUG_ON(host->removed);
1903 + WARN_ON(host->removed);
1904 spin_unlock_irqrestore(&host->lock, flags);
1907 @@ -597,24 +649,38 @@ void mmc_rescan(struct work_struct *work
1909 mmc_send_if_cond(host, host->ocr_avail);
1912 + * First we search for SDIO...
1914 + err = mmc_send_io_op_cond(host, 0, &ocr);
1916 + if (mmc_attach_sdio(host, ocr))
1917 + mmc_power_off(host);
1922 + * ...then normal SD...
1924 err = mmc_send_app_op_cond(host, 0, &ocr);
1925 - if (err == MMC_ERR_NONE) {
1927 if (mmc_attach_sd(host, ocr))
1928 mmc_power_off(host);
1931 - * If we fail to detect any SD cards then try
1932 - * searching for MMC cards.
1934 - err = mmc_send_op_cond(host, 0, &ocr);
1935 - if (err == MMC_ERR_NONE) {
1936 - if (mmc_attach_mmc(host, ocr))
1937 - mmc_power_off(host);
1943 + * ...and finally MMC.
1945 + err = mmc_send_op_cond(host, 0, &ocr);
1947 + if (mmc_attach_mmc(host, ocr))
1948 mmc_power_off(host);
1949 - mmc_release_host(host);
1954 + mmc_release_host(host);
1955 + mmc_power_off(host);
1957 if (host->bus_ops->detect && !host->bus_dead)
1958 host->bus_ops->detect(host);
1959 @@ -725,22 +791,38 @@ static int __init mmc_init(void)
1962 ret = mmc_register_bus();
1964 - ret = mmc_register_host_class();
1966 - mmc_unregister_bus();
1969 + goto destroy_workqueue;
1971 + ret = mmc_register_host_class();
1973 + goto unregister_bus;
1975 + ret = sdio_register_bus();
1977 + goto unregister_host_class;
1981 +unregister_host_class:
1982 + mmc_unregister_host_class();
1984 + mmc_unregister_bus();
1986 + destroy_workqueue(workqueue);
1991 static void __exit mmc_exit(void)
1993 + sdio_unregister_bus();
1994 mmc_unregister_host_class();
1995 mmc_unregister_bus();
1996 destroy_workqueue(workqueue);
1999 -module_init(mmc_init);
2000 +subsys_initcall(mmc_init);
2001 module_exit(mmc_exit);
2003 MODULE_LICENSE("GPL");
2004 Index: linux-2.6.23.16/drivers/mmc/core/core.h
2005 ===================================================================
2006 --- linux-2.6.23.16.orig/drivers/mmc/core/core.h 2008-03-21 17:28:26.000000000 +0100
2007 +++ linux-2.6.23.16/drivers/mmc/core/core.h 2008-03-21 17:30:25.000000000 +0100
2008 @@ -48,5 +48,7 @@ void mmc_rescan(struct work_struct *work
2009 void mmc_start_host(struct mmc_host *host);
2010 void mmc_stop_host(struct mmc_host *host);
2012 +extern int use_spi_crc;
2016 Index: linux-2.6.23.16/drivers/mmc/core/host.c
2017 ===================================================================
2018 --- linux-2.6.23.16.orig/drivers/mmc/core/host.c 2008-03-21 17:28:26.000000000 +0100
2019 +++ linux-2.6.23.16/drivers/mmc/core/host.c 2008-03-21 17:30:25.000000000 +0100
2021 #include <linux/err.h>
2022 #include <linux/idr.h>
2023 #include <linux/pagemap.h>
2024 +#include <linux/leds.h>
2026 #include <linux/mmc/host.h>
2028 @@ -100,6 +101,9 @@ int mmc_add_host(struct mmc_host *host)
2032 + WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
2033 + !host->ops->enable_sdio_irq);
2035 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
2038 @@ -112,6 +116,8 @@ int mmc_add_host(struct mmc_host *host)
2039 snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
2040 "mmc%d", host->index);
2042 + led_trigger_register_simple(host->class_dev.bus_id, &host->led);
2044 err = device_add(&host->class_dev);
2047 @@ -137,6 +143,8 @@ void mmc_remove_host(struct mmc_host *ho
2049 device_del(&host->class_dev);
2051 + led_trigger_unregister_simple(host->led);
2053 spin_lock(&mmc_host_lock);
2054 idr_remove(&mmc_host_idr, host->index);
2055 spin_unlock(&mmc_host_lock);
2056 Index: linux-2.6.23.16/drivers/mmc/core/mmc.c
2057 ===================================================================
2058 --- linux-2.6.23.16.orig/drivers/mmc/core/mmc.c 2008-03-21 17:28:26.000000000 +0100
2059 +++ linux-2.6.23.16/drivers/mmc/core/mmc.c 2008-03-21 17:30:25.000000000 +0100
2060 @@ -161,13 +161,12 @@ static int mmc_read_ext_csd(struct mmc_c
2064 + unsigned int ext_csd_struct;
2068 - err = MMC_ERR_FAILED;
2070 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
2071 - return MMC_ERR_NONE;
2075 * As the ext_csd is so large and mostly unused, we don't store the
2076 @@ -176,13 +175,19 @@ static int mmc_read_ext_csd(struct mmc_c
2077 ext_csd = kmalloc(512, GFP_KERNEL);
2079 printk(KERN_ERR "%s: could not allocate a buffer to "
2080 - "receive the ext_csd. mmc v4 cards will be "
2081 - "treated as v3.\n", mmc_hostname(card->host));
2082 - return MMC_ERR_FAILED;
2083 + "receive the ext_csd.\n", mmc_hostname(card->host));
2087 err = mmc_send_ext_csd(card, ext_csd);
2088 - if (err != MMC_ERR_NONE) {
2091 + * We all hosts that cannot perform the command
2092 + * to fail more gracefully
2094 + if (err != -EINVAL)
2098 * High capacity cards should have this "magic" size
2099 * stored in their CSD.
2100 @@ -197,18 +202,30 @@ static int mmc_read_ext_csd(struct mmc_c
2101 "EXT_CSD, performance might "
2103 mmc_hostname(card->host));
2104 - err = MMC_ERR_NONE;
2111 - card->ext_csd.sectors =
2112 - ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2113 - ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2114 - ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2115 - ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2116 - if (card->ext_csd.sectors)
2117 - mmc_card_set_blockaddr(card);
2118 + ext_csd_struct = ext_csd[EXT_CSD_REV];
2119 + if (ext_csd_struct > 2) {
2120 + printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
2121 + "version %d\n", mmc_hostname(card->host),
2127 + if (ext_csd_struct >= 2) {
2128 + card->ext_csd.sectors =
2129 + ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2130 + ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2131 + ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2132 + ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2133 + if (card->ext_csd.sectors)
2134 + mmc_card_set_blockaddr(card);
2137 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
2138 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
2139 @@ -246,7 +263,7 @@ static int mmc_init_card(struct mmc_host
2140 unsigned int max_dtr;
2143 - BUG_ON(!host->claimed);
2144 + WARN_ON(!host->claimed);
2147 * Since we're changing the OCR value, we seem to
2148 @@ -258,19 +275,33 @@ static int mmc_init_card(struct mmc_host
2150 /* The extra bit indicates that we support high capacity */
2151 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
2152 - if (err != MMC_ERR_NONE)
2157 + * For SPI, enable CRC as appropriate.
2159 + if (mmc_host_is_spi(host)) {
2160 + err = mmc_spi_set_crc(host, use_spi_crc);
2166 * Fetch CID from card.
2168 - err = mmc_all_send_cid(host, cid);
2169 - if (err != MMC_ERR_NONE)
2170 + if (mmc_host_is_spi(host))
2171 + err = mmc_send_cid(host, cid);
2173 + err = mmc_all_send_cid(host, cid);
2178 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2179 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2186 @@ -278,8 +309,10 @@ static int mmc_init_card(struct mmc_host
2187 * Allocate card structure.
2189 card = mmc_alloc_card(host);
2191 + if (IS_ERR(card)) {
2192 + err = PTR_ERR(card);
2196 card->type = MMC_TYPE_MMC;
2198 @@ -287,43 +320,47 @@ static int mmc_init_card(struct mmc_host
2203 + * For native busses: set card RCA and quit open drain mode.
2205 - err = mmc_set_relative_addr(card);
2206 - if (err != MMC_ERR_NONE)
2208 + if (!mmc_host_is_spi(host)) {
2209 + err = mmc_set_relative_addr(card);
2213 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2214 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2219 * Fetch CSD from card.
2221 err = mmc_send_csd(card, card->raw_csd);
2222 - if (err != MMC_ERR_NONE)
2226 err = mmc_decode_csd(card);
2230 err = mmc_decode_cid(card);
2237 * Select card, as all following commands rely on that.
2239 - err = mmc_select_card(card);
2240 - if (err != MMC_ERR_NONE)
2242 + if (!mmc_host_is_spi(host)) {
2243 + err = mmc_select_card(card);
2250 - * Fetch and process extened CSD.
2251 + * Fetch and process extended CSD.
2253 err = mmc_read_ext_csd(card);
2254 - if (err != MMC_ERR_NONE)
2259 @@ -334,7 +371,7 @@ static int mmc_init_card(struct mmc_host
2260 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
2261 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2262 EXT_CSD_HS_TIMING, 1);
2263 - if (err != MMC_ERR_NONE)
2267 mmc_card_set_highspeed(card);
2268 @@ -363,7 +400,7 @@ static int mmc_init_card(struct mmc_host
2269 (host->caps & MMC_CAP_4_BIT_DATA)) {
2270 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2271 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
2272 - if (err != MMC_ERR_NONE)
2276 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
2277 @@ -372,14 +409,14 @@ static int mmc_init_card(struct mmc_host
2281 - return MMC_ERR_NONE;
2286 mmc_remove_card(card);
2289 - return MMC_ERR_FAILED;
2294 @@ -413,7 +450,7 @@ static void mmc_detect(struct mmc_host *
2296 mmc_release_host(host);
2298 - if (err != MMC_ERR_NONE) {
2302 mmc_claim_host(host);
2303 @@ -480,7 +517,8 @@ static void mmc_suspend(struct mmc_host
2304 BUG_ON(!host->card);
2306 mmc_claim_host(host);
2307 - mmc_deselect_cards(host);
2308 + if (!mmc_host_is_spi(host))
2309 + mmc_deselect_cards(host);
2310 host->card->state &= ~MMC_STATE_HIGHSPEED;
2311 mmc_release_host(host);
2313 @@ -502,7 +540,7 @@ static void mmc_resume(struct mmc_host *
2314 err = mmc_init_card(host, host->ocr, host->card);
2315 mmc_release_host(host);
2317 - if (err != MMC_ERR_NONE) {
2321 mmc_claim_host(host);
2322 @@ -536,11 +574,20 @@ int mmc_attach_mmc(struct mmc_host *host
2326 - BUG_ON(!host->claimed);
2327 + WARN_ON(!host->claimed);
2329 mmc_attach_bus(host, &mmc_ops);
2332 + * We need to get OCR a different way for SPI.
2334 + if (mmc_host_is_spi(host)) {
2335 + err = mmc_spi_read_ocr(host, 1, &ocr);
2341 * Sanity check the voltages that the card claims to
2344 @@ -565,7 +612,7 @@ int mmc_attach_mmc(struct mmc_host *host
2345 * Detect and init the card.
2347 err = mmc_init_card(host, host->ocr, NULL);
2348 - if (err != MMC_ERR_NONE)
2352 mmc_release_host(host);
2353 @@ -587,6 +634,6 @@ err:
2354 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
2355 mmc_hostname(host), err);
2361 Index: linux-2.6.23.16/drivers/mmc/core/mmc_ops.c
2362 ===================================================================
2363 --- linux-2.6.23.16.orig/drivers/mmc/core/mmc_ops.c 2008-03-21 17:28:26.000000000 +0100
2364 +++ linux-2.6.23.16/drivers/mmc/core/mmc_ops.c 2008-03-21 17:30:25.000000000 +0100
2368 #include <linux/types.h>
2369 -#include <asm/scatterlist.h>
2370 #include <linux/scatterlist.h>
2372 #include <linux/mmc/host.h>
2373 @@ -40,10 +39,10 @@ static int _mmc_select_card(struct mmc_h
2376 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2377 - if (err != MMC_ERR_NONE)
2381 - return MMC_ERR_NONE;
2385 int mmc_select_card(struct mmc_card *card)
2386 @@ -63,23 +62,36 @@ int mmc_go_idle(struct mmc_host *host)
2388 struct mmc_command cmd;
2390 - mmc_set_chip_select(host, MMC_CS_HIGH);
2394 + * Non-SPI hosts need to prevent chipselect going active during
2395 + * GO_IDLE; that would put chips into SPI mode. Remind them of
2396 + * that in case of hardware that won't pull up DAT3/nCS otherwise.
2398 + * SPI hosts ignore ios.chip_select; it's managed according to
2399 + * rules that must accomodate non-MMC slaves which this layer
2400 + * won't even know about.
2402 + if (!mmc_host_is_spi(host)) {
2403 + mmc_set_chip_select(host, MMC_CS_HIGH);
2407 memset(&cmd, 0, sizeof(struct mmc_command));
2409 cmd.opcode = MMC_GO_IDLE_STATE;
2411 - cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
2412 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
2414 err = mmc_wait_for_cmd(host, &cmd, 0);
2418 - mmc_set_chip_select(host, MMC_CS_DONTCARE);
2419 + if (!mmc_host_is_spi(host)) {
2420 + mmc_set_chip_select(host, MMC_CS_DONTCARE);
2425 + host->use_spi_crc = 0;
2429 @@ -94,23 +106,33 @@ int mmc_send_op_cond(struct mmc_host *ho
2430 memset(&cmd, 0, sizeof(struct mmc_command));
2432 cmd.opcode = MMC_SEND_OP_COND;
2434 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
2435 + cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
2436 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
2438 for (i = 100; i; i--) {
2439 err = mmc_wait_for_cmd(host, &cmd, 0);
2440 - if (err != MMC_ERR_NONE)
2444 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
2445 + /* if we're just probing, do a single pass */
2449 - err = MMC_ERR_TIMEOUT;
2450 + /* otherwise wait until reset completes */
2451 + if (mmc_host_is_spi(host)) {
2452 + if (!(cmd.resp[0] & R1_SPI_IDLE))
2455 + if (cmd.resp[0] & MMC_CARD_BUSY)
2465 + if (rocr && !mmc_host_is_spi(host))
2466 *rocr = cmd.resp[0];
2469 @@ -131,12 +153,12 @@ int mmc_all_send_cid(struct mmc_host *ho
2470 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
2472 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2473 - if (err != MMC_ERR_NONE)
2477 memcpy(cid, cmd.resp, sizeof(u32) * 4);
2479 - return MMC_ERR_NONE;
2483 int mmc_set_relative_addr(struct mmc_card *card)
2484 @@ -154,46 +176,52 @@ int mmc_set_relative_addr(struct mmc_car
2485 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2487 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2488 - if (err != MMC_ERR_NONE)
2492 - return MMC_ERR_NONE;
2496 -int mmc_send_csd(struct mmc_card *card, u32 *csd)
2498 +mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
2501 struct mmc_command cmd;
2504 - BUG_ON(!card->host);
2509 memset(&cmd, 0, sizeof(struct mmc_command));
2511 - cmd.opcode = MMC_SEND_CSD;
2512 - cmd.arg = card->rca << 16;
2513 + cmd.opcode = opcode;
2515 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
2517 - err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2518 - if (err != MMC_ERR_NONE)
2519 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2523 - memcpy(csd, cmd.resp, sizeof(u32) * 4);
2524 + memcpy(cxd, cmd.resp, sizeof(u32) * 4);
2526 - return MMC_ERR_NONE;
2530 -int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2532 +mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
2533 + u32 opcode, void *buf, unsigned len)
2535 struct mmc_request mrq;
2536 struct mmc_command cmd;
2537 struct mmc_data data;
2538 struct scatterlist sg;
2542 - BUG_ON(!card->host);
2544 + /* dma onto stack is unsafe/nonportable, but callers to this
2545 + * routine normally provide temporary on-stack buffers ...
2547 + data_buf = kmalloc(len, GFP_KERNEL);
2548 + if (data_buf == NULL)
2551 memset(&mrq, 0, sizeof(struct mmc_request));
2552 memset(&cmd, 0, sizeof(struct mmc_command));
2553 @@ -202,28 +230,117 @@ int mmc_send_ext_csd(struct mmc_card *ca
2557 - cmd.opcode = MMC_SEND_EXT_CSD;
2558 + cmd.opcode = opcode;
2560 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2563 + /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
2564 + * rely on callers to never use this with "native" calls for reading
2565 + * CSD or CID. Native versions of those commands use the R2 type,
2566 + * not R1 plus a data block.
2568 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2572 data.flags = MMC_DATA_READ;
2576 - sg_init_one(&sg, ext_csd, 512);
2577 + sg_init_one(&sg, data_buf, len);
2580 + mmc_set_data_timeout(&data, card);
2582 - mmc_set_data_timeout(&data, card, 0);
2583 + mmc_wait_for_req(host, &mrq);
2585 - mmc_wait_for_req(card->host, &mrq);
2586 + memcpy(buf, data_buf, len);
2589 - if (cmd.error != MMC_ERR_NONE)
2592 - if (data.error != MMC_ERR_NONE)
2596 - return MMC_ERR_NONE;
2600 +int mmc_send_csd(struct mmc_card *card, u32 *csd)
2604 + if (!mmc_host_is_spi(card->host))
2605 + return mmc_send_cxd_native(card->host, card->rca << 16,
2606 + csd, MMC_SEND_CSD);
2608 + ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
2612 + for (i = 0;i < 4;i++)
2613 + csd[i] = be32_to_cpu(csd[i]);
2618 +int mmc_send_cid(struct mmc_host *host, u32 *cid)
2622 + if (!mmc_host_is_spi(host)) {
2625 + return mmc_send_cxd_native(host, host->card->rca << 16,
2626 + cid, MMC_SEND_CID);
2629 + ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
2633 + for (i = 0;i < 4;i++)
2634 + cid[i] = be32_to_cpu(cid[i]);
2639 +int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2641 + return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
2645 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
2647 + struct mmc_command cmd;
2650 + memset(&cmd, 0, sizeof(struct mmc_command));
2652 + cmd.opcode = MMC_SPI_READ_OCR;
2653 + cmd.arg = highcap ? (1 << 30) : 0;
2654 + cmd.flags = MMC_RSP_SPI_R3;
2656 + err = mmc_wait_for_cmd(host, &cmd, 0);
2658 + *ocrp = cmd.resp[1];
2662 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
2664 + struct mmc_command cmd;
2667 + memset(&cmd, 0, sizeof(struct mmc_command));
2669 + cmd.opcode = MMC_SPI_CRC_ON_OFF;
2670 + cmd.flags = MMC_RSP_SPI_R1;
2671 + cmd.arg = use_crc;
2673 + err = mmc_wait_for_cmd(host, &cmd, 0);
2675 + host->use_spi_crc = use_crc;
2679 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
2680 @@ -241,13 +358,13 @@ int mmc_switch(struct mmc_card *card, u8
2684 - cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
2685 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2687 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2688 - if (err != MMC_ERR_NONE)
2692 - return MMC_ERR_NONE;
2696 int mmc_send_status(struct mmc_card *card, u32 *status)
2697 @@ -261,16 +378,20 @@ int mmc_send_status(struct mmc_card *car
2698 memset(&cmd, 0, sizeof(struct mmc_command));
2700 cmd.opcode = MMC_SEND_STATUS;
2701 - cmd.arg = card->rca << 16;
2702 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2703 + if (!mmc_host_is_spi(card->host))
2704 + cmd.arg = card->rca << 16;
2705 + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2707 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2708 - if (err != MMC_ERR_NONE)
2712 + /* NOTE: callers are required to understand the difference
2713 + * between "native" and SPI format status words!
2716 *status = cmd.resp[0];
2718 - return MMC_ERR_NONE;
2722 Index: linux-2.6.23.16/drivers/mmc/core/mmc_ops.h
2723 ===================================================================
2724 --- linux-2.6.23.16.orig/drivers/mmc/core/mmc_ops.h 2008-03-21 17:28:26.000000000 +0100
2725 +++ linux-2.6.23.16/drivers/mmc/core/mmc_ops.h 2008-03-21 17:30:25.000000000 +0100
2726 @@ -22,6 +22,9 @@ int mmc_send_csd(struct mmc_card *card,
2727 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
2728 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
2729 int mmc_send_status(struct mmc_card *card, u32 *status);
2730 +int mmc_send_cid(struct mmc_host *host, u32 *cid);
2731 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
2732 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
2736 Index: linux-2.6.23.16/drivers/mmc/core/sd.c
2737 ===================================================================
2738 --- linux-2.6.23.16.orig/drivers/mmc/core/sd.c 2008-03-21 17:28:26.000000000 +0100
2739 +++ linux-2.6.23.16/drivers/mmc/core/sd.c 2008-03-21 17:30:25.000000000 +0100
2740 @@ -166,8 +166,6 @@ static int mmc_decode_scr(struct mmc_car
2741 unsigned int scr_struct;
2744 - BUG_ON(!mmc_card_sd(card));
2746 resp[3] = card->raw_scr[1];
2747 resp[2] = card->raw_scr[0];
2749 @@ -193,30 +191,38 @@ static int mmc_read_switch(struct mmc_ca
2752 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2753 - return MMC_ERR_NONE;
2756 if (!(card->csd.cmdclass & CCC_SWITCH)) {
2757 printk(KERN_WARNING "%s: card lacks mandatory switch "
2758 "function, performance might suffer.\n",
2759 mmc_hostname(card->host));
2760 - return MMC_ERR_NONE;
2764 - err = MMC_ERR_FAILED;
2767 status = kmalloc(64, GFP_KERNEL);
2769 printk(KERN_ERR "%s: could not allocate a buffer for "
2770 "switch capabilities.\n", mmc_hostname(card->host));
2775 err = mmc_sd_switch(card, 0, 0, 1, status);
2776 - if (err != MMC_ERR_NONE) {
2779 + * We all hosts that cannot perform the command
2780 + * to fail more gracefully
2782 + if (err != -EINVAL)
2785 printk(KERN_WARNING "%s: problem reading switch "
2786 "capabilities, performance might suffer.\n",
2787 mmc_hostname(card->host));
2788 - err = MMC_ERR_NONE;
2794 @@ -238,28 +244,28 @@ static int mmc_switch_hs(struct mmc_card
2797 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2798 - return MMC_ERR_NONE;
2801 if (!(card->csd.cmdclass & CCC_SWITCH))
2802 - return MMC_ERR_NONE;
2805 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
2806 - return MMC_ERR_NONE;
2809 if (card->sw_caps.hs_max_dtr == 0)
2810 - return MMC_ERR_NONE;
2813 - err = MMC_ERR_FAILED;
2816 status = kmalloc(64, GFP_KERNEL);
2818 printk(KERN_ERR "%s: could not allocate a buffer for "
2819 "switch capabilities.\n", mmc_hostname(card->host));
2824 err = mmc_sd_switch(card, 1, 0, 1, status);
2825 - if (err != MMC_ERR_NONE)
2829 if ((status[16] & 0xF) != 1) {
2830 @@ -292,7 +298,7 @@ static int mmc_sd_init_card(struct mmc_h
2831 unsigned int max_dtr;
2834 - BUG_ON(!host->claimed);
2835 + WARN_ON(!host->claimed);
2838 * Since we're changing the OCR value, we seem to
2839 @@ -309,23 +315,37 @@ static int mmc_sd_init_card(struct mmc_h
2840 * block-addressed SDHC cards.
2842 err = mmc_send_if_cond(host, ocr);
2843 - if (err == MMC_ERR_NONE)
2847 err = mmc_send_app_op_cond(host, ocr, NULL);
2848 - if (err != MMC_ERR_NONE)
2853 + * For SPI, enable CRC as appropriate.
2855 + if (mmc_host_is_spi(host)) {
2856 + err = mmc_spi_set_crc(host, use_spi_crc);
2862 * Fetch CID from card.
2864 - err = mmc_all_send_cid(host, cid);
2865 - if (err != MMC_ERR_NONE)
2866 + if (mmc_host_is_spi(host))
2867 + err = mmc_send_cid(host, cid);
2869 + err = mmc_all_send_cid(host, cid);
2874 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2875 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2882 @@ -333,32 +353,36 @@ static int mmc_sd_init_card(struct mmc_h
2883 * Allocate card structure.
2885 card = mmc_alloc_card(host);
2887 + if (IS_ERR(card)) {
2888 + err = PTR_ERR(card);
2892 card->type = MMC_TYPE_SD;
2893 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
2898 + * For native busses: get card RCA and quit open drain mode.
2900 - err = mmc_send_relative_addr(host, &card->rca);
2901 - if (err != MMC_ERR_NONE)
2903 + if (!mmc_host_is_spi(host)) {
2904 + err = mmc_send_relative_addr(host, &card->rca);
2908 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2909 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2914 * Fetch CSD from card.
2916 err = mmc_send_csd(card, card->raw_csd);
2917 - if (err != MMC_ERR_NONE)
2921 err = mmc_decode_csd(card);
2926 mmc_decode_cid(card);
2927 @@ -367,16 +391,18 @@ static int mmc_sd_init_card(struct mmc_h
2929 * Select card, as all following commands rely on that.
2931 - err = mmc_select_card(card);
2932 - if (err != MMC_ERR_NONE)
2934 + if (!mmc_host_is_spi(host)) {
2935 + err = mmc_select_card(card);
2942 * Fetch SCR from card.
2944 err = mmc_app_send_scr(card, card->raw_scr);
2945 - if (err != MMC_ERR_NONE)
2949 err = mmc_decode_scr(card);
2950 @@ -387,7 +413,7 @@ static int mmc_sd_init_card(struct mmc_h
2951 * Fetch switch information from card.
2953 err = mmc_read_switch(card);
2954 - if (err != MMC_ERR_NONE)
2959 @@ -395,7 +421,7 @@ static int mmc_sd_init_card(struct mmc_h
2960 * Attempt to change to high-speed (if supported)
2962 err = mmc_switch_hs(card);
2963 - if (err != MMC_ERR_NONE)
2968 @@ -418,7 +444,7 @@ static int mmc_sd_init_card(struct mmc_h
2969 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
2970 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
2971 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
2972 - if (err != MMC_ERR_NONE)
2976 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
2977 @@ -442,14 +468,14 @@ static int mmc_sd_init_card(struct mmc_h
2981 - return MMC_ERR_NONE;
2986 mmc_remove_card(card);
2989 - return MMC_ERR_FAILED;
2994 @@ -483,7 +509,7 @@ static void mmc_sd_detect(struct mmc_hos
2996 mmc_release_host(host);
2998 - if (err != MMC_ERR_NONE) {
3000 mmc_sd_remove(host);
3002 mmc_claim_host(host);
3003 @@ -552,7 +578,8 @@ static void mmc_sd_suspend(struct mmc_ho
3004 BUG_ON(!host->card);
3006 mmc_claim_host(host);
3007 - mmc_deselect_cards(host);
3008 + if (!mmc_host_is_spi(host))
3009 + mmc_deselect_cards(host);
3010 host->card->state &= ~MMC_STATE_HIGHSPEED;
3011 mmc_release_host(host);
3013 @@ -574,7 +601,7 @@ static void mmc_sd_resume(struct mmc_hos
3014 err = mmc_sd_init_card(host, host->ocr, host->card);
3015 mmc_release_host(host);
3017 - if (err != MMC_ERR_NONE) {
3019 mmc_sd_remove(host);
3021 mmc_claim_host(host);
3022 @@ -608,11 +635,22 @@ int mmc_attach_sd(struct mmc_host *host,
3026 - BUG_ON(!host->claimed);
3027 + WARN_ON(!host->claimed);
3029 mmc_attach_bus(host, &mmc_sd_ops);
3032 + * We need to get OCR a different way for SPI.
3034 + if (mmc_host_is_spi(host)) {
3035 + mmc_go_idle(host);
3037 + err = mmc_spi_read_ocr(host, 0, &ocr);
3043 * Sanity check the voltages that the card claims to
3046 @@ -644,7 +682,7 @@ int mmc_attach_sd(struct mmc_host *host,
3047 * Detect and init the card.
3049 err = mmc_sd_init_card(host, host->ocr, NULL);
3050 - if (err != MMC_ERR_NONE)
3054 mmc_release_host(host);
3055 @@ -666,6 +704,6 @@ err:
3056 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
3057 mmc_hostname(host), err);
3063 Index: linux-2.6.23.16/drivers/mmc/core/sd_ops.c
3064 ===================================================================
3065 --- linux-2.6.23.16.orig/drivers/mmc/core/sd_ops.c 2008-03-21 17:28:26.000000000 +0100
3066 +++ linux-2.6.23.16/drivers/mmc/core/sd_ops.c 2008-03-21 17:30:25.000000000 +0100
3070 #include <linux/types.h>
3071 -#include <asm/scatterlist.h>
3072 #include <linux/scatterlist.h>
3074 #include <linux/mmc/host.h>
3075 @@ -33,21 +32,21 @@ static int mmc_app_cmd(struct mmc_host *
3078 cmd.arg = card->rca << 16;
3079 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3080 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
3083 - cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
3084 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
3087 err = mmc_wait_for_cmd(host, &cmd, 0);
3088 - if (err != MMC_ERR_NONE)
3092 /* Check that card supported application commands */
3093 - if (!(cmd.resp[0] & R1_APP_CMD))
3094 - return MMC_ERR_FAILED;
3095 + if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
3096 + return -EOPNOTSUPP;
3098 - return MMC_ERR_NONE;
3103 @@ -73,7 +72,7 @@ int mmc_wait_for_app_cmd(struct mmc_host
3105 BUG_ON(retries < 0);
3107 - err = MMC_ERR_INVALID;
3111 * We have to resend MMC_APP_CMD for each attempt so
3112 @@ -83,8 +82,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3113 memset(&mrq, 0, sizeof(struct mmc_request));
3115 err = mmc_app_cmd(host, card);
3116 - if (err != MMC_ERR_NONE)
3118 + /* no point in retrying; no APP commands allowed */
3119 + if (mmc_host_is_spi(host)) {
3120 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3126 memset(&mrq, 0, sizeof(struct mmc_request));
3128 @@ -97,8 +102,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3129 mmc_wait_for_req(host, &mrq);
3132 - if (cmd->error == MMC_ERR_NONE)
3136 + /* no point in retrying illegal APP commands */
3137 + if (mmc_host_is_spi(host)) {
3138 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3144 @@ -127,14 +138,14 @@ int mmc_app_set_bus_width(struct mmc_car
3145 cmd.arg = SD_BUS_WIDTH_4;
3148 - return MMC_ERR_INVALID;
3152 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
3153 - if (err != MMC_ERR_NONE)
3157 - return MMC_ERR_NONE;
3161 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
3162 @@ -147,23 +158,36 @@ int mmc_send_app_op_cond(struct mmc_host
3163 memset(&cmd, 0, sizeof(struct mmc_command));
3165 cmd.opcode = SD_APP_OP_COND;
3167 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
3168 + if (mmc_host_is_spi(host))
3169 + cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
3172 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
3174 for (i = 100; i; i--) {
3175 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
3176 - if (err != MMC_ERR_NONE)
3180 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
3181 + /* if we're just probing, do a single pass */
3185 - err = MMC_ERR_TIMEOUT;
3186 + /* otherwise wait until reset completes */
3187 + if (mmc_host_is_spi(host)) {
3188 + if (!(cmd.resp[0] & R1_SPI_IDLE))
3191 + if (cmd.resp[0] & MMC_CARD_BUSY)
3201 + if (rocr && !mmc_host_is_spi(host))
3202 *rocr = cmd.resp[0];
3205 @@ -174,6 +198,7 @@ int mmc_send_if_cond(struct mmc_host *ho
3206 struct mmc_command cmd;
3208 static const u8 test_pattern = 0xAA;
3209 + u8 result_pattern;
3212 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
3213 @@ -182,16 +207,21 @@ int mmc_send_if_cond(struct mmc_host *ho
3215 cmd.opcode = SD_SEND_IF_COND;
3216 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
3217 - cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
3218 + cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
3220 err = mmc_wait_for_cmd(host, &cmd, 0);
3221 - if (err != MMC_ERR_NONE)
3225 - if ((cmd.resp[0] & 0xFF) != test_pattern)
3226 - return MMC_ERR_FAILED;
3227 + if (mmc_host_is_spi(host))
3228 + result_pattern = cmd.resp[1] & 0xFF;
3230 + result_pattern = cmd.resp[0] & 0xFF;
3232 + if (result_pattern != test_pattern)
3235 - return MMC_ERR_NONE;
3239 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
3240 @@ -209,12 +239,12 @@ int mmc_send_relative_addr(struct mmc_ho
3241 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
3243 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
3244 - if (err != MMC_ERR_NONE)
3248 *rca = cmd.resp[0] >> 16;
3250 - return MMC_ERR_NONE;
3254 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
3255 @@ -229,8 +259,10 @@ int mmc_app_send_scr(struct mmc_card *ca
3256 BUG_ON(!card->host);
3259 + /* NOTE: caller guarantees scr is heap-allocated */
3261 err = mmc_app_cmd(card->host, card);
3262 - if (err != MMC_ERR_NONE)
3266 memset(&mrq, 0, sizeof(struct mmc_request));
3267 @@ -242,7 +274,7 @@ int mmc_app_send_scr(struct mmc_card *ca
3269 cmd.opcode = SD_APP_SEND_SCR;
3271 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3272 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3276 @@ -252,19 +284,19 @@ int mmc_app_send_scr(struct mmc_card *ca
3278 sg_init_one(&sg, scr, 8);
3280 - mmc_set_data_timeout(&data, card, 0);
3281 + mmc_set_data_timeout(&data, card);
3283 mmc_wait_for_req(card->host, &mrq);
3285 - if (cmd.error != MMC_ERR_NONE)
3288 - if (data.error != MMC_ERR_NONE)
3292 - scr[0] = ntohl(scr[0]);
3293 - scr[1] = ntohl(scr[1]);
3294 + scr[0] = be32_to_cpu(scr[0]);
3295 + scr[1] = be32_to_cpu(scr[1]);
3297 - return MMC_ERR_NONE;
3301 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
3302 @@ -278,6 +310,8 @@ int mmc_sd_switch(struct mmc_card *card,
3304 BUG_ON(!card->host);
3306 + /* NOTE: caller guarantees resp is heap-allocated */
3311 @@ -292,7 +326,7 @@ int mmc_sd_switch(struct mmc_card *card,
3312 cmd.arg = mode << 31 | 0x00FFFFFF;
3313 cmd.arg &= ~(0xF << (group * 4));
3314 cmd.arg |= value << (group * 4);
3315 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3316 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3320 @@ -302,15 +336,15 @@ int mmc_sd_switch(struct mmc_card *card,
3322 sg_init_one(&sg, resp, 64);
3324 - mmc_set_data_timeout(&data, card, 0);
3325 + mmc_set_data_timeout(&data, card);
3327 mmc_wait_for_req(card->host, &mrq);
3329 - if (cmd.error != MMC_ERR_NONE)
3332 - if (data.error != MMC_ERR_NONE)
3336 - return MMC_ERR_NONE;
3340 Index: linux-2.6.23.16/drivers/mmc/core/sdio.c
3341 ===================================================================
3342 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3343 +++ linux-2.6.23.16/drivers/mmc/core/sdio.c 2008-03-21 17:30:25.000000000 +0100
3346 + * linux/drivers/mmc/sdio.c
3348 + * Copyright 2006-2007 Pierre Ossman
3350 + * This program is free software; you can redistribute it and/or modify
3351 + * it under the terms of the GNU General Public License as published by
3352 + * the Free Software Foundation; either version 2 of the License, or (at
3353 + * your option) any later version.
3356 +#include <linux/err.h>
3358 +#include <linux/mmc/host.h>
3359 +#include <linux/mmc/card.h>
3360 +#include <linux/mmc/sdio.h>
3361 +#include <linux/mmc/sdio_func.h>
3365 +#include "sdio_bus.h"
3366 +#include "mmc_ops.h"
3367 +#include "sd_ops.h"
3368 +#include "sdio_ops.h"
3369 +#include "sdio_cis.h"
3371 +static int sdio_read_fbr(struct sdio_func *func)
3374 + unsigned char data;
3376 + ret = mmc_io_rw_direct(func->card, 0, 0,
3377 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
3383 + if (data == 0x0f) {
3384 + ret = mmc_io_rw_direct(func->card, 0, 0,
3385 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
3390 + func->class = data;
3396 +static int sdio_init_func(struct mmc_card *card, unsigned int fn)
3399 + struct sdio_func *func;
3401 + BUG_ON(fn > SDIO_MAX_FUNCS);
3403 + func = sdio_alloc_func(card);
3405 + return PTR_ERR(func);
3409 + ret = sdio_read_fbr(func);
3413 + ret = sdio_read_func_cis(func);
3417 + card->sdio_func[fn - 1] = func;
3423 + * It is okay to remove the function here even though we hold
3424 + * the host lock as we haven't registered the device yet.
3426 + sdio_remove_func(func);
3430 +static int sdio_read_cccr(struct mmc_card *card)
3434 + unsigned char data;
3436 + memset(&card->cccr, 0, sizeof(struct sdio_cccr));
3438 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
3442 + cccr_vsn = data & 0x0f;
3444 + if (cccr_vsn > SDIO_CCCR_REV_1_20) {
3445 + printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
3446 + mmc_hostname(card->host), cccr_vsn);
3450 + card->cccr.sdio_vsn = (data & 0xf0) >> 4;
3452 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
3456 + if (data & SDIO_CCCR_CAP_SMB)
3457 + card->cccr.multi_block = 1;
3458 + if (data & SDIO_CCCR_CAP_LSC)
3459 + card->cccr.low_speed = 1;
3460 + if (data & SDIO_CCCR_CAP_4BLS)
3461 + card->cccr.wide_bus = 1;
3463 + if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
3464 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
3468 + if (data & SDIO_POWER_SMPC)
3469 + card->cccr.high_power = 1;
3472 + if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
3473 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
3477 + if (data & SDIO_SPEED_SHS)
3478 + card->cccr.high_speed = 1;
3485 +static int sdio_enable_wide(struct mmc_card *card)
3490 + if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
3493 + if (card->cccr.low_speed && !card->cccr.wide_bus)
3496 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
3500 + ctrl |= SDIO_BUS_WIDTH_4BIT;
3502 + ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
3506 + mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
3512 + * Host is being removed. Free up the current card.
3514 +static void mmc_sdio_remove(struct mmc_host *host)
3519 + BUG_ON(!host->card);
3521 + for (i = 0;i < host->card->sdio_funcs;i++) {
3522 + if (host->card->sdio_func[i]) {
3523 + sdio_remove_func(host->card->sdio_func[i]);
3524 + host->card->sdio_func[i] = NULL;
3528 + mmc_remove_card(host->card);
3529 + host->card = NULL;
3533 + * Card detection callback from host.
3535 +static void mmc_sdio_detect(struct mmc_host *host)
3540 + BUG_ON(!host->card);
3542 + mmc_claim_host(host);
3545 + * Just check if our card has been removed.
3547 + err = mmc_select_card(host->card);
3549 + mmc_release_host(host);
3552 + mmc_sdio_remove(host);
3554 + mmc_claim_host(host);
3555 + mmc_detach_bus(host);
3556 + mmc_release_host(host);
3561 +static const struct mmc_bus_ops mmc_sdio_ops = {
3562 + .remove = mmc_sdio_remove,
3563 + .detect = mmc_sdio_detect,
3568 + * Starting point for SDIO card init.
3570 +int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
3574 + struct mmc_card *card;
3577 + WARN_ON(!host->claimed);
3579 + mmc_attach_bus(host, &mmc_sdio_ops);
3582 + * Sanity check the voltages that the card claims to
3586 + printk(KERN_WARNING "%s: card claims to support voltages "
3587 + "below the defined range. These will be ignored.\n",
3588 + mmc_hostname(host));
3592 + if (ocr & MMC_VDD_165_195) {
3593 + printk(KERN_WARNING "%s: SDIO card claims to support the "
3594 + "incompletely defined 'low voltage range'. This "
3595 + "will be ignored.\n", mmc_hostname(host));
3596 + ocr &= ~MMC_VDD_165_195;
3599 + host->ocr = mmc_select_voltage(host, ocr);
3602 + * Can we support the voltage(s) of the card(s)?
3610 + * Inform the card of the voltage
3612 + err = mmc_send_io_op_cond(host, host->ocr, &ocr);
3617 + * For SPI, enable CRC as appropriate.
3619 + if (mmc_host_is_spi(host)) {
3620 + err = mmc_spi_set_crc(host, use_spi_crc);
3626 + * The number of functions on the card is encoded inside
3629 + funcs = (ocr & 0x70000000) >> 28;
3632 + * Allocate card structure.
3634 + card = mmc_alloc_card(host);
3635 + if (IS_ERR(card)) {
3636 + err = PTR_ERR(card);
3640 + card->type = MMC_TYPE_SDIO;
3641 + card->sdio_funcs = funcs;
3643 + host->card = card;
3646 + * For native busses: set card RCA and quit open drain mode.
3648 + if (!mmc_host_is_spi(host)) {
3649 + err = mmc_send_relative_addr(host, &card->rca);
3653 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
3657 + * Select card, as all following commands rely on that.
3659 + if (!mmc_host_is_spi(host)) {
3660 + err = mmc_select_card(card);
3666 + * Read the common registers.
3668 + err = sdio_read_cccr(card);
3673 + * Read the common CIS tuples.
3675 + err = sdio_read_common_cis(card);
3680 + * No support for high-speed yet, so just set
3681 + * the card's maximum speed.
3683 + mmc_set_clock(host, card->cis.max_dtr);
3686 + * Switch to wider bus (if supported).
3688 + err = sdio_enable_wide(card);
3693 + * Initialize (but don't add) all present functions.
3695 + for (i = 0;i < funcs;i++) {
3696 + err = sdio_init_func(host->card, i + 1);
3701 + mmc_release_host(host);
3704 + * First add the card to the driver model...
3706 + err = mmc_add_card(host->card);
3708 + goto remove_added;
3711 + * ...then the SDIO functions.
3713 + for (i = 0;i < funcs;i++) {
3714 + err = sdio_add_func(host->card->sdio_func[i]);
3716 + goto remove_added;
3723 + /* Remove without lock if the device has been added. */
3724 + mmc_sdio_remove(host);
3725 + mmc_claim_host(host);
3727 + /* And with lock if it hasn't been added. */
3729 + mmc_sdio_remove(host);
3731 + mmc_detach_bus(host);
3732 + mmc_release_host(host);
3734 + printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
3735 + mmc_hostname(host), err);
3740 Index: linux-2.6.23.16/drivers/mmc/core/sdio_bus.c
3741 ===================================================================
3742 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3743 +++ linux-2.6.23.16/drivers/mmc/core/sdio_bus.c 2008-03-21 17:30:25.000000000 +0100
3746 + * linux/drivers/mmc/core/sdio_bus.c
3748 + * Copyright 2007 Pierre Ossman
3750 + * This program is free software; you can redistribute it and/or modify
3751 + * it under the terms of the GNU General Public License as published by
3752 + * the Free Software Foundation; either version 2 of the License, or (at
3753 + * your option) any later version.
3755 + * SDIO function driver model
3758 +#include <linux/device.h>
3759 +#include <linux/err.h>
3761 +#include <linux/mmc/card.h>
3762 +#include <linux/mmc/sdio_func.h>
3764 +#include "sdio_cis.h"
3765 +#include "sdio_bus.h"
3767 +#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
3768 +#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
3770 +/* show configuration fields */
3771 +#define sdio_config_attr(field, format_string) \
3773 +field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
3775 + struct sdio_func *func; \
3777 + func = dev_to_sdio_func (dev); \
3778 + return sprintf (buf, format_string, func->field); \
3781 +sdio_config_attr(class, "0x%02x\n");
3782 +sdio_config_attr(vendor, "0x%04x\n");
3783 +sdio_config_attr(device, "0x%04x\n");
3785 +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3787 + struct sdio_func *func = dev_to_sdio_func (dev);
3789 + return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
3790 + func->class, func->vendor, func->device);
3793 +static struct device_attribute sdio_dev_attrs[] = {
3795 + __ATTR_RO(vendor),
3796 + __ATTR_RO(device),
3797 + __ATTR_RO(modalias),
3801 +static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
3802 + const struct sdio_device_id *id)
3804 + if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
3806 + if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
3808 + if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
3813 +static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
3814 + struct sdio_driver *sdrv)
3816 + const struct sdio_device_id *ids;
3818 + ids = sdrv->id_table;
3821 + while (ids->class || ids->vendor || ids->device) {
3822 + if (sdio_match_one(func, ids))
3831 +static int sdio_bus_match(struct device *dev, struct device_driver *drv)
3833 + struct sdio_func *func = dev_to_sdio_func(dev);
3834 + struct sdio_driver *sdrv = to_sdio_driver(drv);
3836 + if (sdio_match_device(func, sdrv))
3843 +sdio_bus_uevent(struct device *dev, char **envp,
3844 + int num_envp, char *buffer, int buffer_size)
3846 + struct sdio_func *func = dev_to_sdio_func(dev);
3847 + int i = 0, len = 0;
3849 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3850 + "SDIO_CLASS=%02X", func->class))
3853 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3854 + "SDIO_ID=%04X:%04X", func->vendor, func->device))
3857 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3858 + "MODALIAS=sdio:c%02Xv%04Xd%04X",
3859 + func->class, func->vendor, func->device))
3865 +static int sdio_bus_probe(struct device *dev)
3867 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3868 + struct sdio_func *func = dev_to_sdio_func(dev);
3869 + const struct sdio_device_id *id;
3872 + id = sdio_match_device(func, drv);
3876 + /* Set the default block size so the driver is sure it's something
3878 + sdio_claim_host(func);
3879 + ret = sdio_set_block_size(func, 0);
3880 + sdio_release_host(func);
3884 + return drv->probe(func, id);
3887 +static int sdio_bus_remove(struct device *dev)
3889 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3890 + struct sdio_func *func = dev_to_sdio_func(dev);
3892 + drv->remove(func);
3894 + if (func->irq_handler) {
3895 + printk(KERN_WARNING "WARNING: driver %s did not remove "
3896 + "its interrupt handler!\n", drv->name);
3897 + sdio_claim_host(func);
3898 + sdio_release_irq(func);
3899 + sdio_release_host(func);
3905 +static struct bus_type sdio_bus_type = {
3907 + .dev_attrs = sdio_dev_attrs,
3908 + .match = sdio_bus_match,
3909 + .uevent = sdio_bus_uevent,
3910 + .probe = sdio_bus_probe,
3911 + .remove = sdio_bus_remove,
3914 +int sdio_register_bus(void)
3916 + return bus_register(&sdio_bus_type);
3919 +void sdio_unregister_bus(void)
3921 + bus_unregister(&sdio_bus_type);
3925 + * sdio_register_driver - register a function driver
3926 + * @drv: SDIO function driver
3928 +int sdio_register_driver(struct sdio_driver *drv)
3930 + drv->drv.name = drv->name;
3931 + drv->drv.bus = &sdio_bus_type;
3932 + return driver_register(&drv->drv);
3934 +EXPORT_SYMBOL_GPL(sdio_register_driver);
3937 + * sdio_unregister_driver - unregister a function driver
3938 + * @drv: SDIO function driver
3940 +void sdio_unregister_driver(struct sdio_driver *drv)
3942 + drv->drv.bus = &sdio_bus_type;
3943 + driver_unregister(&drv->drv);
3945 +EXPORT_SYMBOL_GPL(sdio_unregister_driver);
3947 +static void sdio_release_func(struct device *dev)
3949 + struct sdio_func *func = dev_to_sdio_func(dev);
3951 + sdio_free_func_cis(func);
3954 + kfree(func->info);
3960 + * Allocate and initialise a new SDIO function structure.
3962 +struct sdio_func *sdio_alloc_func(struct mmc_card *card)
3964 + struct sdio_func *func;
3966 + func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
3968 + return ERR_PTR(-ENOMEM);
3970 + func->card = card;
3972 + device_initialize(&func->dev);
3974 + func->dev.parent = &card->dev;
3975 + func->dev.bus = &sdio_bus_type;
3976 + func->dev.release = sdio_release_func;
3982 + * Register a new SDIO function with the driver model.
3984 +int sdio_add_func(struct sdio_func *func)
3988 + snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
3989 + "%s:%d", mmc_card_id(func->card), func->num);
3991 + ret = device_add(&func->dev);
3993 + sdio_func_set_present(func);
3999 + * Unregister a SDIO function with the driver model, and
4000 + * (eventually) free it.
4002 +void sdio_remove_func(struct sdio_func *func)
4004 + if (sdio_func_present(func))
4005 + device_del(&func->dev);
4007 + put_device(&func->dev);
4010 Index: linux-2.6.23.16/drivers/mmc/core/sdio_bus.h
4011 ===================================================================
4012 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4013 +++ linux-2.6.23.16/drivers/mmc/core/sdio_bus.h 2008-03-21 17:30:25.000000000 +0100
4016 + * linux/drivers/mmc/core/sdio_bus.h
4018 + * Copyright 2007 Pierre Ossman
4020 + * This program is free software; you can redistribute it and/or modify
4021 + * it under the terms of the GNU General Public License as published by
4022 + * the Free Software Foundation; either version 2 of the License, or (at
4023 + * your option) any later version.
4025 +#ifndef _MMC_CORE_SDIO_BUS_H
4026 +#define _MMC_CORE_SDIO_BUS_H
4028 +struct sdio_func *sdio_alloc_func(struct mmc_card *card);
4029 +int sdio_add_func(struct sdio_func *func);
4030 +void sdio_remove_func(struct sdio_func *func);
4032 +int sdio_register_bus(void);
4033 +void sdio_unregister_bus(void);
4037 Index: linux-2.6.23.16/drivers/mmc/core/sdio_cis.c
4038 ===================================================================
4039 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4040 +++ linux-2.6.23.16/drivers/mmc/core/sdio_cis.c 2008-03-21 17:30:25.000000000 +0100
4043 + * linux/drivers/mmc/core/sdio_cis.c
4045 + * Author: Nicolas Pitre
4046 + * Created: June 11, 2007
4047 + * Copyright: MontaVista Software Inc.
4049 + * Copyright 2007 Pierre Ossman
4051 + * This program is free software; you can redistribute it and/or modify
4052 + * it under the terms of the GNU General Public License as published by
4053 + * the Free Software Foundation; either version 2 of the License, or (at
4054 + * your option) any later version.
4057 +#include <linux/kernel.h>
4059 +#include <linux/mmc/host.h>
4060 +#include <linux/mmc/card.h>
4061 +#include <linux/mmc/sdio.h>
4062 +#include <linux/mmc/sdio_func.h>
4064 +#include "sdio_cis.h"
4065 +#include "sdio_ops.h"
4067 +static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
4068 + const unsigned char *buf, unsigned size)
4070 + unsigned i, nr_strings;
4071 + char **buffer, *string;
4077 + for (i = 0; i < size; i++) {
4078 + if (buf[i] == 0xff)
4084 + if (buf[i-1] != '\0') {
4085 + printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
4091 + buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
4095 + string = (char*)(buffer + nr_strings);
4097 + for (i = 0; i < nr_strings; i++) {
4098 + buffer[i] = string;
4099 + strcpy(string, buf);
4100 + string += strlen(string) + 1;
4101 + buf += strlen(buf) + 1;
4105 + func->num_info = nr_strings;
4106 + func->info = (const char**)buffer;
4108 + card->num_info = nr_strings;
4109 + card->info = (const char**)buffer;
4115 +static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
4116 + const unsigned char *buf, unsigned size)
4118 + unsigned int vendor, device;
4121 + vendor = buf[0] | (buf[1] << 8);
4124 + device = buf[2] | (buf[3] << 8);
4127 + func->vendor = vendor;
4128 + func->device = device;
4130 + card->cis.vendor = vendor;
4131 + card->cis.device = device;
4137 +static const unsigned char speed_val[16] =
4138 + { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
4139 +static const unsigned int speed_unit[8] =
4140 + { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
4142 +static int cistpl_funce_common(struct mmc_card *card,
4143 + const unsigned char *buf, unsigned size)
4145 + if (size < 0x04 || buf[0] != 0)
4148 + /* TPLFE_FN0_BLK_SIZE */
4149 + card->cis.blksize = buf[1] | (buf[2] << 8);
4151 + /* TPLFE_MAX_TRAN_SPEED */
4152 + card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
4153 + speed_unit[buf[3] & 7];
4158 +static int cistpl_funce_func(struct sdio_func *func,
4159 + const unsigned char *buf, unsigned size)
4162 + unsigned min_size;
4164 + vsn = func->card->cccr.sdio_vsn;
4165 + min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
4167 + if (size < min_size || buf[0] != 1)
4170 + /* TPLFE_MAX_BLK_SIZE */
4171 + func->max_blksize = buf[12] | (buf[13] << 8);
4176 +static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
4177 + const unsigned char *buf, unsigned size)
4182 + * There should be two versions of the CISTPL_FUNCE tuple,
4183 + * one for the common CIS (function 0) and a version used by
4184 + * the individual function's CIS (1-7). Yet, the later has a
4185 + * different length depending on the SDIO spec version.
4188 + ret = cistpl_funce_func(func, buf, size);
4190 + ret = cistpl_funce_common(card, buf, size);
4193 + printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
4194 + "type %u\n", mmc_hostname(card->host), size, buf[0]);
4201 +typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
4202 + const unsigned char *, unsigned);
4205 + unsigned char code;
4206 + unsigned char min_size;
4207 + tpl_parse_t *parse;
4210 +static const struct cis_tpl cis_tpl_list[] = {
4211 + { 0x15, 3, cistpl_vers_1 },
4212 + { 0x20, 4, cistpl_manfid },
4213 + { 0x21, 2, /* cistpl_funcid */ },
4214 + { 0x22, 0, cistpl_funce },
4217 +static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
4220 + struct sdio_func_tuple *this, **prev;
4221 + unsigned i, ptr = 0;
4224 + * Note that this works for the common CIS (function number 0) as
4225 + * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
4226 + * have the same offset.
4228 + for (i = 0; i < 3; i++) {
4229 + unsigned char x, fn;
4236 + ret = mmc_io_rw_direct(card, 0, 0,
4237 + SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
4240 + ptr |= x << (i * 8);
4244 + prev = &func->tuples;
4246 + prev = &card->tuples;
4251 + unsigned char tpl_code, tpl_link;
4253 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
4257 + /* 0xff means we're done */
4258 + if (tpl_code == 0xff)
4261 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
4265 + this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
4269 + for (i = 0; i < tpl_link; i++) {
4270 + ret = mmc_io_rw_direct(card, 0, 0,
4271 + ptr + i, 0, &this->data[i]);
4280 + for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
4281 + if (cis_tpl_list[i].code == tpl_code)
4283 + if (i >= ARRAY_SIZE(cis_tpl_list)) {
4284 + /* this tuple is unknown to the core */
4285 + this->next = NULL;
4286 + this->code = tpl_code;
4287 + this->size = tpl_link;
4289 + prev = &this->next;
4291 + "%s: queuing CIS tuple 0x%02x length %u\n",
4292 + mmc_hostname(card->host), tpl_code, tpl_link);
4294 + const struct cis_tpl *tpl = cis_tpl_list + i;
4295 + if (tpl_link < tpl->min_size) {
4297 + "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
4298 + mmc_hostname(card->host),
4299 + tpl_code, tpl_link, tpl->min_size);
4301 + } else if (tpl->parse) {
4302 + ret = tpl->parse(card, func,
4303 + this->data, tpl_link);
4312 + * Link in all unknown tuples found in the common CIS so that
4313 + * drivers don't have to go digging in two places.
4316 + *prev = card->tuples;
4321 +int sdio_read_common_cis(struct mmc_card *card)
4323 + return sdio_read_cis(card, NULL);
4326 +void sdio_free_common_cis(struct mmc_card *card)
4328 + struct sdio_func_tuple *tuple, *victim;
4330 + tuple = card->tuples;
4334 + tuple = tuple->next;
4338 + card->tuples = NULL;
4341 +int sdio_read_func_cis(struct sdio_func *func)
4345 + ret = sdio_read_cis(func->card, func);
4350 + * Since we've linked to tuples in the card structure,
4351 + * we must make sure we have a reference to it.
4353 + get_device(&func->card->dev);
4356 + * Vendor/device id is optional for function CIS, so
4357 + * copy it from the card structure as needed.
4359 + if (func->vendor == 0) {
4360 + func->vendor = func->card->cis.vendor;
4361 + func->device = func->card->cis.device;
4367 +void sdio_free_func_cis(struct sdio_func *func)
4369 + struct sdio_func_tuple *tuple, *victim;
4371 + tuple = func->tuples;
4373 + while (tuple && tuple != func->card->tuples) {
4375 + tuple = tuple->next;
4379 + func->tuples = NULL;
4382 + * We have now removed the link to the tuples in the
4383 + * card structure, so remove the reference.
4385 + put_device(&func->card->dev);
4388 Index: linux-2.6.23.16/drivers/mmc/core/sdio_cis.h
4389 ===================================================================
4390 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4391 +++ linux-2.6.23.16/drivers/mmc/core/sdio_cis.h 2008-03-21 17:30:25.000000000 +0100
4394 + * linux/drivers/mmc/core/sdio_cis.h
4396 + * Author: Nicolas Pitre
4397 + * Created: June 11, 2007
4398 + * Copyright: MontaVista Software Inc.
4400 + * This program is free software; you can redistribute it and/or modify
4401 + * it under the terms of the GNU General Public License as published by
4402 + * the Free Software Foundation; either version 2 of the License, or (at
4403 + * your option) any later version.
4406 +#ifndef _MMC_SDIO_CIS_H
4407 +#define _MMC_SDIO_CIS_H
4409 +int sdio_read_common_cis(struct mmc_card *card);
4410 +void sdio_free_common_cis(struct mmc_card *card);
4412 +int sdio_read_func_cis(struct sdio_func *func);
4413 +void sdio_free_func_cis(struct sdio_func *func);
4416 Index: linux-2.6.23.16/drivers/mmc/core/sdio_io.c
4417 ===================================================================
4418 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4419 +++ linux-2.6.23.16/drivers/mmc/core/sdio_io.c 2008-03-21 17:30:25.000000000 +0100
4422 + * linux/drivers/mmc/core/sdio_io.c
4424 + * Copyright 2007 Pierre Ossman
4426 + * This program is free software; you can redistribute it and/or modify
4427 + * it under the terms of the GNU General Public License as published by
4428 + * the Free Software Foundation; either version 2 of the License, or (at
4429 + * your option) any later version.
4432 +#include <linux/mmc/host.h>
4433 +#include <linux/mmc/card.h>
4434 +#include <linux/mmc/sdio.h>
4435 +#include <linux/mmc/sdio_func.h>
4437 +#include "sdio_ops.h"
4440 + * sdio_claim_host - exclusively claim a bus for a certain SDIO function
4441 + * @func: SDIO function that will be accessed
4443 + * Claim a bus for a set of operations. The SDIO function given
4444 + * is used to figure out which bus is relevant.
4446 +void sdio_claim_host(struct sdio_func *func)
4449 + BUG_ON(!func->card);
4451 + mmc_claim_host(func->card->host);
4453 +EXPORT_SYMBOL_GPL(sdio_claim_host);
4456 + * sdio_release_host - release a bus for a certain SDIO function
4457 + * @func: SDIO function that was accessed
4459 + * Release a bus, allowing others to claim the bus for their
4462 +void sdio_release_host(struct sdio_func *func)
4465 + BUG_ON(!func->card);
4467 + mmc_release_host(func->card->host);
4469 +EXPORT_SYMBOL_GPL(sdio_release_host);
4472 + * sdio_enable_func - enables a SDIO function for usage
4473 + * @func: SDIO function to enable
4475 + * Powers up and activates a SDIO function so that register
4476 + * access is possible.
4478 +int sdio_enable_func(struct sdio_func *func)
4481 + unsigned char reg;
4482 + unsigned long timeout;
4485 + BUG_ON(!func->card);
4487 + pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
4489 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
4493 + reg |= 1 << func->num;
4495 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4500 + * FIXME: This should timeout based on information in the CIS,
4501 + * but we don't have card to parse that yet.
4503 + timeout = jiffies + HZ;
4506 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®);
4509 + if (reg & (1 << func->num))
4512 + if (time_after(jiffies, timeout))
4516 + pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
4521 + pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
4524 +EXPORT_SYMBOL_GPL(sdio_enable_func);
4527 + * sdio_disable_func - disable a SDIO function
4528 + * @func: SDIO function to disable
4530 + * Powers down and deactivates a SDIO function. Register access
4531 + * to this function will fail until the function is reenabled.
4533 +int sdio_disable_func(struct sdio_func *func)
4536 + unsigned char reg;
4539 + BUG_ON(!func->card);
4541 + pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
4543 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
4547 + reg &= ~(1 << func->num);
4549 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4553 + pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
4558 + pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
4561 +EXPORT_SYMBOL_GPL(sdio_disable_func);
4564 + * sdio_set_block_size - set the block size of an SDIO function
4565 + * @func: SDIO function to change
4566 + * @blksz: new block size or 0 to use the default.
4568 + * The default block size is the largest supported by both the function
4569 + * and the host, with a maximum of 512 to ensure that arbitrarily sized
4570 + * data transfer use the optimal (least) number of commands.
4572 + * A driver may call this to override the default block size set by the
4573 + * core. This can be used to set a block size greater than the maximum
4574 + * that reported by the card; it is the driver's responsibility to ensure
4575 + * it uses a value that the card supports.
4577 + * Returns 0 on success, -EINVAL if the host does not support the
4578 + * requested block size, or -EIO (etc.) if one of the resultant FBR block
4579 + * size register writes failed.
4582 +int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
4586 + if (blksz > func->card->host->max_blk_size)
4591 + func->max_blksize,
4592 + func->card->host->max_blk_size),
4596 + ret = mmc_io_rw_direct(func->card, 1, 0,
4597 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
4598 + blksz & 0xff, NULL);
4601 + ret = mmc_io_rw_direct(func->card, 1, 0,
4602 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
4603 + (blksz >> 8) & 0xff, NULL);
4606 + func->cur_blksize = blksz;
4610 +EXPORT_SYMBOL_GPL(sdio_set_block_size);
4612 +/* Split an arbitrarily sized data transfer into several
4613 + * IO_RW_EXTENDED commands. */
4614 +static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
4615 + unsigned addr, int incr_addr, u8 *buf, unsigned size)
4617 + unsigned remainder = size;
4618 + unsigned max_blocks;
4621 + /* Do the bulk of the transfer using block mode (if supported). */
4622 + if (func->card->cccr.multi_block) {
4623 + /* Blocks per command is limited by host count, host transfer
4624 + * size (we only use a single sg entry) and the maximum for
4625 + * IO_RW_EXTENDED of 511 blocks. */
4626 + max_blocks = min(min(
4627 + func->card->host->max_blk_count,
4628 + func->card->host->max_seg_size / func->cur_blksize),
4631 + while (remainder > func->cur_blksize) {
4634 + blocks = remainder / func->cur_blksize;
4635 + if (blocks > max_blocks)
4636 + blocks = max_blocks;
4637 + size = blocks * func->cur_blksize;
4639 + ret = mmc_io_rw_extended(func->card, write,
4640 + func->num, addr, incr_addr, buf,
4641 + blocks, func->cur_blksize);
4645 + remainder -= size;
4652 + /* Write the remainder using byte mode. */
4653 + while (remainder > 0) {
4655 + if (size > func->cur_blksize)
4656 + size = func->cur_blksize;
4658 + size = 512; /* maximum size for byte mode */
4660 + ret = mmc_io_rw_extended(func->card, write, func->num, addr,
4661 + incr_addr, buf, 1, size);
4665 + remainder -= size;
4674 + * sdio_readb - read a single byte from a SDIO function
4675 + * @func: SDIO function to access
4676 + * @addr: address to read
4677 + * @err_ret: optional status value from transfer
4679 + * Reads a single byte from the address space of a given SDIO
4680 + * function. If there is a problem reading the address, 0xff
4681 + * is returned and @err_ret will contain the error code.
4683 +unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
4687 + unsigned char val;
4694 + ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
4703 +EXPORT_SYMBOL_GPL(sdio_readb);
4706 + * sdio_writeb - write a single byte to a SDIO function
4707 + * @func: SDIO function to access
4708 + * @b: byte to write
4709 + * @addr: address to write to
4710 + * @err_ret: optional status value from transfer
4712 + * Writes a single byte to the address space of a given SDIO
4713 + * function. @err_ret will contain the status of the actual
4716 +void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4723 + ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
4727 +EXPORT_SYMBOL_GPL(sdio_writeb);
4730 + * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
4731 + * @func: SDIO function to access
4732 + * @dst: buffer to store the data
4733 + * @addr: address to begin reading from
4734 + * @count: number of bytes to read
4736 + * Reads from the address space of a given SDIO function. Return
4737 + * value indicates if the transfer succeeded or not.
4739 +int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
4740 + unsigned int addr, int count)
4742 + return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
4744 +EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
4747 + * sdio_memcpy_toio - write a chunk of memory to a SDIO function
4748 + * @func: SDIO function to access
4749 + * @addr: address to start writing to
4750 + * @src: buffer that contains the data to write
4751 + * @count: number of bytes to write
4753 + * Writes to the address space of a given SDIO function. Return
4754 + * value indicates if the transfer succeeded or not.
4756 +int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
4757 + void *src, int count)
4759 + return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
4761 +EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
4764 + * sdio_readsb - read from a FIFO on a SDIO function
4765 + * @func: SDIO function to access
4766 + * @dst: buffer to store the data
4767 + * @addr: address of (single byte) FIFO
4768 + * @count: number of bytes to read
4770 + * Reads from the specified FIFO of a given SDIO function. Return
4771 + * value indicates if the transfer succeeded or not.
4773 +int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
4776 + return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
4779 +EXPORT_SYMBOL_GPL(sdio_readsb);
4782 + * sdio_writesb - write to a FIFO of a SDIO function
4783 + * @func: SDIO function to access
4784 + * @addr: address of (single byte) FIFO
4785 + * @src: buffer that contains the data to write
4786 + * @count: number of bytes to write
4788 + * Writes to the specified FIFO of a given SDIO function. Return
4789 + * value indicates if the transfer succeeded or not.
4791 +int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
4794 + return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
4796 +EXPORT_SYMBOL_GPL(sdio_writesb);
4799 + * sdio_readw - read a 16 bit integer from a SDIO function
4800 + * @func: SDIO function to access
4801 + * @addr: address to read
4802 + * @err_ret: optional status value from transfer
4804 + * Reads a 16 bit integer from the address space of a given SDIO
4805 + * function. If there is a problem reading the address, 0xffff
4806 + * is returned and @err_ret will contain the error code.
4808 +unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
4816 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
4823 + return le16_to_cpu(*(u16*)func->tmpbuf);
4825 +EXPORT_SYMBOL_GPL(sdio_readw);
4828 + * sdio_writew - write a 16 bit integer to a SDIO function
4829 + * @func: SDIO function to access
4830 + * @b: integer to write
4831 + * @addr: address to write to
4832 + * @err_ret: optional status value from transfer
4834 + * Writes a 16 bit integer to the address space of a given SDIO
4835 + * function. @err_ret will contain the status of the actual
4838 +void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
4843 + *(u16*)func->tmpbuf = cpu_to_le16(b);
4845 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
4849 +EXPORT_SYMBOL_GPL(sdio_writew);
4852 + * sdio_readl - read a 32 bit integer from a SDIO function
4853 + * @func: SDIO function to access
4854 + * @addr: address to read
4855 + * @err_ret: optional status value from transfer
4857 + * Reads a 32 bit integer from the address space of a given SDIO
4858 + * function. If there is a problem reading the address,
4859 + * 0xffffffff is returned and @err_ret will contain the error
4862 +unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
4870 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
4874 + return 0xFFFFFFFF;
4877 + return le32_to_cpu(*(u32*)func->tmpbuf);
4879 +EXPORT_SYMBOL_GPL(sdio_readl);
4882 + * sdio_writel - write a 32 bit integer to a SDIO function
4883 + * @func: SDIO function to access
4884 + * @b: integer to write
4885 + * @addr: address to write to
4886 + * @err_ret: optional status value from transfer
4888 + * Writes a 32 bit integer to the address space of a given SDIO
4889 + * function. @err_ret will contain the status of the actual
4892 +void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
4897 + *(u32*)func->tmpbuf = cpu_to_le32(b);
4899 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
4903 +EXPORT_SYMBOL_GPL(sdio_writel);
4906 + * sdio_f0_readb - read a single byte from SDIO function 0
4907 + * @func: an SDIO function of the card
4908 + * @addr: address to read
4909 + * @err_ret: optional status value from transfer
4911 + * Reads a single byte from the address space of SDIO function 0.
4912 + * If there is a problem reading the address, 0xff is returned
4913 + * and @err_ret will contain the error code.
4915 +unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
4919 + unsigned char val;
4926 + ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
4935 +EXPORT_SYMBOL_GPL(sdio_f0_readb);
4938 + * sdio_f0_writeb - write a single byte to SDIO function 0
4939 + * @func: an SDIO function of the card
4940 + * @b: byte to write
4941 + * @addr: address to write to
4942 + * @err_ret: optional status value from transfer
4944 + * Writes a single byte to the address space of SDIO function 0.
4945 + * @err_ret will contain the status of the actual transfer.
4947 + * Only writes to the vendor specific CCCR registers (0xF0 -
4948 + * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
4949 + * writes outside this range.
4951 +void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4958 + if (addr < 0xF0 || addr > 0xFF) {
4960 + *err_ret = -EINVAL;
4964 + ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
4968 +EXPORT_SYMBOL_GPL(sdio_f0_writeb);
4969 Index: linux-2.6.23.16/drivers/mmc/core/sdio_irq.c
4970 ===================================================================
4971 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4972 +++ linux-2.6.23.16/drivers/mmc/core/sdio_irq.c 2008-03-21 17:30:25.000000000 +0100
4975 + * linux/drivers/mmc/core/sdio_irq.c
4977 + * Author: Nicolas Pitre
4978 + * Created: June 18, 2007
4979 + * Copyright: MontaVista Software Inc.
4981 + * This program is free software; you can redistribute it and/or modify
4982 + * it under the terms of the GNU General Public License as published by
4983 + * the Free Software Foundation; either version 2 of the License, or (at
4984 + * your option) any later version.
4987 +#include <linux/kernel.h>
4988 +#include <linux/sched.h>
4989 +#include <linux/kthread.h>
4990 +#include <linux/wait.h>
4991 +#include <linux/delay.h>
4993 +#include <linux/mmc/core.h>
4994 +#include <linux/mmc/host.h>
4995 +#include <linux/mmc/card.h>
4996 +#include <linux/mmc/sdio.h>
4997 +#include <linux/mmc/sdio_func.h>
4999 +#include "sdio_ops.h"
5001 +static int process_sdio_pending_irqs(struct mmc_card *card)
5003 + int i, ret, count;
5004 + unsigned char pending;
5006 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
5008 + printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
5009 + mmc_card_id(card), ret);
5014 + for (i = 1; i <= 7; i++) {
5015 + if (pending & (1 << i)) {
5016 + struct sdio_func *func = card->sdio_func[i - 1];
5018 + printk(KERN_WARNING "%s: pending IRQ for "
5019 + "non-existant function\n",
5020 + mmc_card_id(card));
5022 + } else if (func->irq_handler) {
5023 + func->irq_handler(func);
5026 + printk(KERN_WARNING "%s: pending IRQ with no handler\n",
5027 + sdio_func_id(func));
5039 +static int sdio_irq_thread(void *_host)
5041 + struct mmc_host *host = _host;
5042 + struct sched_param param = { .sched_priority = 1 };
5043 + unsigned long period, idle_period;
5046 + sched_setscheduler(current, SCHED_FIFO, ¶m);
5049 + * We want to allow for SDIO cards to work even on non SDIO
5050 + * aware hosts. One thing that non SDIO host cannot do is
5051 + * asynchronous notification of pending SDIO card interrupts
5052 + * hence we poll for them in that case.
5054 + idle_period = msecs_to_jiffies(10);
5055 + period = (host->caps & MMC_CAP_SDIO_IRQ) ?
5056 + MAX_SCHEDULE_TIMEOUT : idle_period;
5058 + pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
5059 + mmc_hostname(host), period);
5063 + * We claim the host here on drivers behalf for a couple
5066 + * 1) it is already needed to retrieve the CCCR_INTx;
5067 + * 2) we want the driver(s) to clear the IRQ condition ASAP;
5068 + * 3) we need to control the abort condition locally.
5070 + * Just like traditional hard IRQ handlers, we expect SDIO
5071 + * IRQ handlers to be quick and to the point, so that the
5072 + * holding of the host lock does not cover too much work
5073 + * that doesn't require that lock to be held.
5075 + ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
5078 + ret = process_sdio_pending_irqs(host->card);
5079 + mmc_release_host(host);
5082 + * Give other threads a chance to run in the presence of
5083 + * errors. FIXME: determine if due to card removal and
5084 + * possibly exit this thread if so.
5090 + * Adaptive polling frequency based on the assumption
5091 + * that an interrupt will be closely followed by more.
5092 + * This has a substantial benefit for network devices.
5094 + if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
5099 + if (period > idle_period)
5100 + period = idle_period;
5104 + set_task_state(current, TASK_INTERRUPTIBLE);
5105 + if (host->caps & MMC_CAP_SDIO_IRQ)
5106 + host->ops->enable_sdio_irq(host, 1);
5107 + if (!kthread_should_stop())
5108 + schedule_timeout(period);
5109 + set_task_state(current, TASK_RUNNING);
5110 + } while (!kthread_should_stop());
5112 + if (host->caps & MMC_CAP_SDIO_IRQ)
5113 + host->ops->enable_sdio_irq(host, 0);
5115 + pr_debug("%s: IRQ thread exiting with code %d\n",
5116 + mmc_hostname(host), ret);
5121 +static int sdio_card_irq_get(struct mmc_card *card)
5123 + struct mmc_host *host = card->host;
5125 + WARN_ON(!host->claimed);
5127 + if (!host->sdio_irqs++) {
5128 + atomic_set(&host->sdio_irq_thread_abort, 0);
5129 + host->sdio_irq_thread =
5130 + kthread_run(sdio_irq_thread, host, "ksdiorqd");
5131 + if (IS_ERR(host->sdio_irq_thread)) {
5132 + int err = PTR_ERR(host->sdio_irq_thread);
5133 + host->sdio_irqs--;
5141 +static int sdio_card_irq_put(struct mmc_card *card)
5143 + struct mmc_host *host = card->host;
5145 + WARN_ON(!host->claimed);
5146 + BUG_ON(host->sdio_irqs < 1);
5148 + if (!--host->sdio_irqs) {
5149 + atomic_set(&host->sdio_irq_thread_abort, 1);
5150 + kthread_stop(host->sdio_irq_thread);
5157 + * sdio_claim_irq - claim the IRQ for a SDIO function
5158 + * @func: SDIO function
5159 + * @handler: IRQ handler callback
5161 + * Claim and activate the IRQ for the given SDIO function. The provided
5162 + * handler will be called when that IRQ is asserted. The host is always
5163 + * claimed already when the handler is called so the handler must not
5164 + * call sdio_claim_host() nor sdio_release_host().
5166 +int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
5169 + unsigned char reg;
5172 + BUG_ON(!func->card);
5174 + pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
5176 + if (func->irq_handler) {
5177 + pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
5181 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
5185 + reg |= 1 << func->num;
5187 + reg |= 1; /* Master interrupt enable */
5189 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5193 + func->irq_handler = handler;
5194 + ret = sdio_card_irq_get(func->card);
5196 + func->irq_handler = NULL;
5200 +EXPORT_SYMBOL_GPL(sdio_claim_irq);
5203 + * sdio_release_irq - release the IRQ for a SDIO function
5204 + * @func: SDIO function
5206 + * Disable and release the IRQ for the given SDIO function.
5208 +int sdio_release_irq(struct sdio_func *func)
5211 + unsigned char reg;
5214 + BUG_ON(!func->card);
5216 + pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
5218 + if (func->irq_handler) {
5219 + func->irq_handler = NULL;
5220 + sdio_card_irq_put(func->card);
5223 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
5227 + reg &= ~(1 << func->num);
5229 + /* Disable master interrupt with the last function interrupt */
5230 + if (!(reg & 0xFE))
5233 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5239 +EXPORT_SYMBOL_GPL(sdio_release_irq);
5241 Index: linux-2.6.23.16/drivers/mmc/core/sdio_ops.c
5242 ===================================================================
5243 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5244 +++ linux-2.6.23.16/drivers/mmc/core/sdio_ops.c 2008-03-21 17:30:25.000000000 +0100
5247 + * linux/drivers/mmc/sdio_ops.c
5249 + * Copyright 2006-2007 Pierre Ossman
5251 + * This program is free software; you can redistribute it and/or modify
5252 + * it under the terms of the GNU General Public License as published by
5253 + * the Free Software Foundation; either version 2 of the License, or (at
5254 + * your option) any later version.
5257 +#include <linux/scatterlist.h>
5259 +#include <linux/mmc/host.h>
5260 +#include <linux/mmc/card.h>
5261 +#include <linux/mmc/mmc.h>
5262 +#include <linux/mmc/sdio.h>
5266 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
5268 + struct mmc_command cmd;
5273 + memset(&cmd, 0, sizeof(struct mmc_command));
5275 + cmd.opcode = SD_IO_SEND_OP_COND;
5277 + cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
5279 + for (i = 100; i; i--) {
5280 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
5284 + /* if we're just probing, do a single pass */
5288 + /* otherwise wait until reset completes */
5289 + if (mmc_host_is_spi(host)) {
5291 + * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
5292 + * an initialized card under SPI, but some cards
5293 + * (Marvell's) only behave when looking at this
5296 + if (cmd.resp[1] & MMC_CARD_BUSY)
5299 + if (cmd.resp[0] & MMC_CARD_BUSY)
5309 + *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
5314 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5315 + unsigned addr, u8 in, u8* out)
5317 + struct mmc_command cmd;
5323 + memset(&cmd, 0, sizeof(struct mmc_command));
5325 + cmd.opcode = SD_IO_RW_DIRECT;
5326 + cmd.arg = write ? 0x80000000 : 0x00000000;
5327 + cmd.arg |= fn << 28;
5328 + cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
5329 + cmd.arg |= addr << 9;
5331 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
5333 + err = mmc_wait_for_cmd(card->host, &cmd, 0);
5337 + if (mmc_host_is_spi(card->host)) {
5338 + /* host driver already reported errors */
5340 + if (cmd.resp[0] & R5_ERROR)
5342 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5344 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5349 + if (mmc_host_is_spi(card->host))
5350 + *out = (cmd.resp[0] >> 8) & 0xFF;
5352 + *out = cmd.resp[0] & 0xFF;
5358 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5359 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
5361 + struct mmc_request mrq;
5362 + struct mmc_command cmd;
5363 + struct mmc_data data;
5364 + struct scatterlist sg;
5368 + BUG_ON(blocks == 1 && blksz > 512);
5369 + WARN_ON(blocks == 0);
5370 + WARN_ON(blksz == 0);
5372 + memset(&mrq, 0, sizeof(struct mmc_request));
5373 + memset(&cmd, 0, sizeof(struct mmc_command));
5374 + memset(&data, 0, sizeof(struct mmc_data));
5379 + cmd.opcode = SD_IO_RW_EXTENDED;
5380 + cmd.arg = write ? 0x80000000 : 0x00000000;
5381 + cmd.arg |= fn << 28;
5382 + cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
5383 + cmd.arg |= addr << 9;
5384 + if (blocks == 1 && blksz <= 512)
5385 + cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
5387 + cmd.arg |= 0x08000000 | blocks; /* block mode */
5388 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
5390 + data.blksz = blksz;
5391 + data.blocks = blocks;
5392 + data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
5396 + sg_init_one(&sg, buf, blksz * blocks);
5398 + mmc_set_data_timeout(&data, card);
5400 + mmc_wait_for_req(card->host, &mrq);
5405 + return data.error;
5407 + if (mmc_host_is_spi(card->host)) {
5408 + /* host driver already reported errors */
5410 + if (cmd.resp[0] & R5_ERROR)
5412 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5414 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5421 Index: linux-2.6.23.16/drivers/mmc/core/sdio_ops.h
5422 ===================================================================
5423 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5424 +++ linux-2.6.23.16/drivers/mmc/core/sdio_ops.h 2008-03-21 17:30:25.000000000 +0100
5427 + * linux/drivers/mmc/sdio_ops.c
5429 + * Copyright 2006-2007 Pierre Ossman
5431 + * This program is free software; you can redistribute it and/or modify
5432 + * it under the terms of the GNU General Public License as published by
5433 + * the Free Software Foundation; either version 2 of the License, or (at
5434 + * your option) any later version.
5437 +#ifndef _MMC_SDIO_OPS_H
5438 +#define _MMC_SDIO_OPS_H
5440 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
5441 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5442 + unsigned addr, u8 in, u8* out);
5443 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5444 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz);
5448 Index: linux-2.6.23.16/include/linux/mmc/card.h
5449 ===================================================================
5450 --- linux-2.6.23.16.orig/include/linux/mmc/card.h 2008-03-21 17:28:26.000000000 +0100
5451 +++ linux-2.6.23.16/include/linux/mmc/card.h 2008-03-21 17:30:25.000000000 +0100
5452 @@ -55,7 +55,28 @@ struct sd_switch_caps {
5453 unsigned int hs_max_dtr;
5457 + unsigned int sdio_vsn;
5458 + unsigned int sd_vsn;
5459 + unsigned int multi_block:1,
5467 + unsigned short vendor;
5468 + unsigned short device;
5469 + unsigned short blksize;
5470 + unsigned int max_dtr;
5475 +struct sdio_func_tuple;
5477 +#define SDIO_MAX_FUNCS 7
5481 @@ -67,11 +88,13 @@ struct mmc_card {
5482 unsigned int type; /* card type */
5483 #define MMC_TYPE_MMC 0 /* MMC card */
5484 #define MMC_TYPE_SD 1 /* SD card */
5485 +#define MMC_TYPE_SDIO 2 /* SDIO card */
5486 unsigned int state; /* (our) card state */
5487 #define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
5488 #define MMC_STATE_READONLY (1<<1) /* card is read-only */
5489 #define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
5490 #define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
5492 u32 raw_cid[4]; /* raw card CID */
5493 u32 raw_csd[4]; /* raw card CSD */
5494 u32 raw_scr[2]; /* raw card SCR */
5495 @@ -80,10 +103,19 @@ struct mmc_card {
5496 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
5497 struct sd_scr scr; /* extra SD information */
5498 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
5500 + unsigned int sdio_funcs; /* number of SDIO functions */
5501 + struct sdio_cccr cccr; /* common card info */
5502 + struct sdio_cis cis; /* common tuple info */
5503 + struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
5504 + unsigned num_info; /* number of info strings */
5505 + const char **info; /* info strings */
5506 + struct sdio_func_tuple *tuples; /* unknown common tuples */
5509 #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
5510 #define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
5511 +#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
5513 #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
5514 #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
5515 Index: linux-2.6.23.16/include/linux/mmc/core.h
5516 ===================================================================
5517 --- linux-2.6.23.16.orig/include/linux/mmc/core.h 2008-03-21 17:28:26.000000000 +0100
5518 +++ linux-2.6.23.16/include/linux/mmc/core.h 2008-03-21 17:30:25.000000000 +0100
5519 @@ -25,14 +25,20 @@ struct mmc_command {
5520 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
5521 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
5522 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
5523 -#define MMC_CMD_MASK (3 << 5) /* command type */
5525 +#define MMC_CMD_MASK (3 << 5) /* non-SPI command type */
5526 #define MMC_CMD_AC (0 << 5)
5527 #define MMC_CMD_ADTC (1 << 5)
5528 #define MMC_CMD_BC (2 << 5)
5529 #define MMC_CMD_BCR (3 << 5)
5531 +#define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */
5532 +#define MMC_RSP_SPI_S2 (1 << 8) /* second byte */
5533 +#define MMC_RSP_SPI_B4 (1 << 9) /* four data bytes */
5534 +#define MMC_RSP_SPI_BUSY (1 << 10) /* card may send busy */
5537 - * These are the response types, and correspond to valid bit
5538 + * These are the native response types, and correspond to valid bit
5539 * patterns of the above flags. One additional valid pattern
5540 * is all zeros, which means we don't expect a response.
5542 @@ -41,12 +47,30 @@ struct mmc_command {
5543 #define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
5544 #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
5545 #define MMC_RSP_R3 (MMC_RSP_PRESENT)
5546 +#define MMC_RSP_R4 (MMC_RSP_PRESENT)
5547 +#define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5548 #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5549 #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5551 #define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
5554 + * These are the SPI response types for MMC, SD, and SDIO cards.
5555 + * Commands return R1, with maybe more info. Zero is an error type;
5556 + * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
5558 +#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
5559 +#define MMC_RSP_SPI_R1B (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
5560 +#define MMC_RSP_SPI_R2 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5561 +#define MMC_RSP_SPI_R3 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5562 +#define MMC_RSP_SPI_R4 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5563 +#define MMC_RSP_SPI_R5 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5564 +#define MMC_RSP_SPI_R7 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5566 +#define mmc_spi_resp_type(cmd) ((cmd)->flags & \
5567 + (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
5570 * These are the command types.
5572 #define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK)
5573 @@ -54,12 +78,19 @@ struct mmc_command {
5574 unsigned int retries; /* max number of retries */
5575 unsigned int error; /* command error */
5577 -#define MMC_ERR_NONE 0
5578 -#define MMC_ERR_TIMEOUT 1
5579 -#define MMC_ERR_BADCRC 2
5580 -#define MMC_ERR_FIFO 3
5581 -#define MMC_ERR_FAILED 4
5582 -#define MMC_ERR_INVALID 5
5584 + * Standard errno values are used for errors, but some have specific
5585 + * meaning in the MMC layer:
5587 + * ETIMEDOUT Card took too long to respond
5588 + * EILSEQ Basic format problem with the received or sent data
5589 + * (e.g. CRC check failed, incorrect opcode in response
5591 + * EINVAL Request cannot be performed because of restrictions
5592 + * in hardware and/or the driver
5593 + * ENOMEDIUM Host can determine that the slot is empty and is
5594 + * actively failing requests
5597 struct mmc_data *data; /* data segment associated with cmd */
5598 struct mmc_request *mrq; /* associated request */
5599 @@ -76,7 +107,6 @@ struct mmc_data {
5600 #define MMC_DATA_WRITE (1 << 8)
5601 #define MMC_DATA_READ (1 << 9)
5602 #define MMC_DATA_STREAM (1 << 10)
5603 -#define MMC_DATA_MULTI (1 << 11)
5605 unsigned int bytes_xfered;
5607 @@ -104,9 +134,20 @@ extern int mmc_wait_for_cmd(struct mmc_h
5608 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
5609 struct mmc_command *, int);
5611 -extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int);
5612 +extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
5614 -extern void mmc_claim_host(struct mmc_host *host);
5615 +extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
5616 extern void mmc_release_host(struct mmc_host *host);
5619 + * mmc_claim_host - exclusively claim a host
5620 + * @host: mmc host to claim
5622 + * Claim a host for a set of operations.
5624 +static inline void mmc_claim_host(struct mmc_host *host)
5626 + __mmc_claim_host(host, NULL);
5630 Index: linux-2.6.23.16/include/linux/mmc/host.h
5631 ===================================================================
5632 --- linux-2.6.23.16.orig/include/linux/mmc/host.h 2008-03-21 17:28:26.000000000 +0100
5633 +++ linux-2.6.23.16/include/linux/mmc/host.h 2008-03-21 17:30:25.000000000 +0100
5635 #ifndef LINUX_MMC_HOST_H
5636 #define LINUX_MMC_HOST_H
5638 +#include <linux/leds.h>
5640 #include <linux/mmc/core.h>
5643 @@ -51,6 +53,7 @@ struct mmc_host_ops {
5644 void (*request)(struct mmc_host *host, struct mmc_request *req);
5645 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
5646 int (*get_ro)(struct mmc_host *host);
5647 + void (*enable_sdio_irq)(struct mmc_host *host, int enable);
5651 @@ -87,9 +90,10 @@ struct mmc_host {
5653 #define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
5654 #define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */
5655 -#define MMC_CAP_BYTEBLOCK (1 << 2) /* Can do non-log2 block sizes */
5656 -#define MMC_CAP_MMC_HIGHSPEED (1 << 3) /* Can do MMC high-speed timing */
5657 -#define MMC_CAP_SD_HIGHSPEED (1 << 4) /* Can do SD high-speed timing */
5658 +#define MMC_CAP_MMC_HIGHSPEED (1 << 2) /* Can do MMC high-speed timing */
5659 +#define MMC_CAP_SD_HIGHSPEED (1 << 3) /* Can do SD high-speed timing */
5660 +#define MMC_CAP_SDIO_IRQ (1 << 4) /* Can signal pending SDIO IRQs */
5661 +#define MMC_CAP_SPI (1 << 5) /* Talks only SPI protocols */
5663 /* host specific block data */
5664 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
5665 @@ -106,23 +110,30 @@ struct mmc_host {
5666 struct mmc_ios ios; /* current io bus settings */
5667 u32 ocr; /* the current OCR setting */
5669 - unsigned int mode; /* current card mode of host */
5670 -#define MMC_MODE_MMC 0
5671 -#define MMC_MODE_SD 1
5672 + /* group bitfields together to minimize padding */
5673 + unsigned int use_spi_crc:1;
5674 + unsigned int claimed:1; /* host exclusively claimed */
5675 + unsigned int bus_dead:1; /* bus has been released */
5676 +#ifdef CONFIG_MMC_DEBUG
5677 + unsigned int removed:1; /* host is being removed */
5680 struct mmc_card *card; /* device attached to this host */
5682 wait_queue_head_t wq;
5683 - unsigned int claimed:1; /* host exclusively claimed */
5685 struct delayed_work detect;
5686 -#ifdef CONFIG_MMC_DEBUG
5687 - unsigned int removed:1; /* host is being removed */
5690 const struct mmc_bus_ops *bus_ops; /* current bus driver */
5691 unsigned int bus_refs; /* reference counter */
5692 - unsigned int bus_dead:1; /* bus has been released */
5694 + unsigned int sdio_irqs;
5695 + struct task_struct *sdio_irq_thread;
5696 + atomic_t sdio_irq_thread_abort;
5698 +#ifdef CONFIG_LEDS_TRIGGERS
5699 + struct led_trigger *led; /* activity led */
5702 unsigned long private[0] ____cacheline_aligned;
5704 @@ -137,6 +148,8 @@ static inline void *mmc_priv(struct mmc_
5705 return (void *)host->private;
5708 +#define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
5710 #define mmc_dev(x) ((x)->parent)
5711 #define mmc_classdev(x) (&(x)->class_dev)
5712 #define mmc_hostname(x) ((x)->class_dev.bus_id)
5713 @@ -147,5 +160,11 @@ extern int mmc_resume_host(struct mmc_ho
5714 extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
5715 extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
5717 +static inline void mmc_signal_sdio_irq(struct mmc_host *host)
5719 + host->ops->enable_sdio_irq(host, 0);
5720 + wake_up_process(host->sdio_irq_thread);
5725 Index: linux-2.6.23.16/include/linux/mmc/mmc.h
5726 ===================================================================
5727 --- linux-2.6.23.16.orig/include/linux/mmc/mmc.h 2008-03-21 17:28:26.000000000 +0100
5728 +++ linux-2.6.23.16/include/linux/mmc/mmc.h 2008-03-21 17:30:25.000000000 +0100
5731 /* Standard MMC commands (4.1) type argument response */
5733 -#define MMC_GO_IDLE_STATE 0 /* bc */
5734 +#define MMC_GO_IDLE_STATE 0 /* bc */
5735 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
5736 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
5737 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
5739 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
5740 #define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
5741 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
5742 -#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5743 +#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5744 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
5745 +#define MMC_SPI_READ_OCR 58 /* spi spi_R3 */
5746 +#define MMC_SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
5749 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
5755 + MMC status in R1, for native mode (SPI bits are different)
5760 r : detected and set for the actual command response
5761 x : detected and set during command execution. the host must poll
5762 the card by sending status command in order to read these bits.
5764 - a : according to the card state
5765 + a : according to the card state
5766 b : always related to the previous command. Reception of
5767 a valid command will clear it (with a delay of one command)
5769 @@ -124,10 +126,33 @@
5770 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
5771 #define R1_ERASE_RESET (1 << 13) /* sr, c */
5772 #define R1_STATUS(x) (x & 0xFFFFE000)
5773 -#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5774 +#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5775 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
5776 #define R1_APP_CMD (1 << 5) /* sr, c */
5779 + * MMC/SD in SPI mode reports R1 status always, and R2 for SEND_STATUS
5780 + * R1 is the low order byte; R2 is the next highest byte, when present.
5782 +#define R1_SPI_IDLE (1 << 0)
5783 +#define R1_SPI_ERASE_RESET (1 << 1)
5784 +#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
5785 +#define R1_SPI_COM_CRC (1 << 3)
5786 +#define R1_SPI_ERASE_SEQ (1 << 4)
5787 +#define R1_SPI_ADDRESS (1 << 5)
5788 +#define R1_SPI_PARAMETER (1 << 6)
5789 +/* R1 bit 7 is always zero */
5790 +#define R2_SPI_CARD_LOCKED (1 << 8)
5791 +#define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
5792 +#define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
5793 +#define R2_SPI_ERROR (1 << 10)
5794 +#define R2_SPI_CC_ERROR (1 << 11)
5795 +#define R2_SPI_CARD_ECC_ERROR (1 << 12)
5796 +#define R2_SPI_WP_VIOLATION (1 << 13)
5797 +#define R2_SPI_ERASE_PARAM (1 << 14)
5798 +#define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
5799 +#define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
5801 /* These are unpacked versions of the actual responses */
5804 @@ -182,6 +207,7 @@ struct _mmc_csd {
5806 #define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
5807 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
5808 + /* (and for SPI, CMD58,59) */
5809 #define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
5811 #define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
5812 @@ -227,6 +253,7 @@ struct _mmc_csd {
5813 #define EXT_CSD_BUS_WIDTH 183 /* R/W */
5814 #define EXT_CSD_HS_TIMING 185 /* R/W */
5815 #define EXT_CSD_CARD_TYPE 196 /* RO */
5816 +#define EXT_CSD_REV 192 /* RO */
5817 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
5820 Index: linux-2.6.23.16/include/linux/mmc/sdio.h
5821 ===================================================================
5822 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5823 +++ linux-2.6.23.16/include/linux/mmc/sdio.h 2008-03-21 17:30:25.000000000 +0100
5826 + * include/linux/mmc/sdio.h
5828 + * Copyright 2006-2007 Pierre Ossman
5830 + * This program is free software; you can redistribute it and/or modify
5831 + * it under the terms of the GNU General Public License as published by
5832 + * the Free Software Foundation; either version 2 of the License, or (at
5833 + * your option) any later version.
5839 +/* SDIO commands type argument response */
5840 +#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
5841 +#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
5842 +#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
5845 + * SD_IO_RW_DIRECT argument format:
5848 + * [30:28] Function number
5850 + * [25:9] Register address
5855 + * SD_IO_RW_EXTENDED argument format:
5858 + * [30:28] Function number
5860 + * [26] Increment address
5861 + * [25:9] Register address
5862 + * [8:0] Byte/block count
5870 + r : detected and set for the actual command response
5871 + x : detected and set during command execution. the host must poll
5872 + the card by sending status command in order to read these bits.
5874 + a : according to the card state
5875 + b : always related to the previous command. Reception of
5876 + a valid command will clear it (with a delay of one command)
5880 +#define R5_COM_CRC_ERROR (1 << 15) /* er, b */
5881 +#define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
5882 +#define R5_ERROR (1 << 11) /* erx, c */
5883 +#define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
5884 +#define R5_OUT_OF_RANGE (1 << 8) /* er, c */
5885 +#define R5_STATUS(x) (x & 0xCB00)
5886 +#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
5889 + * Card Common Control Registers (CCCR)
5892 +#define SDIO_CCCR_CCCR 0x00
5894 +#define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
5895 +#define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
5896 +#define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
5898 +#define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
5899 +#define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
5900 +#define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
5901 +#define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
5903 +#define SDIO_CCCR_SD 0x01
5905 +#define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
5906 +#define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
5907 +#define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
5909 +#define SDIO_CCCR_IOEx 0x02
5910 +#define SDIO_CCCR_IORx 0x03
5912 +#define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
5913 +#define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
5915 +#define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
5917 +#define SDIO_CCCR_IF 0x07 /* bus interface controls */
5919 +#define SDIO_BUS_WIDTH_1BIT 0x00
5920 +#define SDIO_BUS_WIDTH_4BIT 0x02
5922 +#define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
5924 +#define SDIO_CCCR_CAPS 0x08
5926 +#define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
5927 +#define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
5928 +#define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
5929 +#define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
5930 +#define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
5931 +#define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
5932 +#define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
5933 +#define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
5935 +#define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
5937 +/* Following 4 regs are valid only if SBS is set */
5938 +#define SDIO_CCCR_SUSPEND 0x0c
5939 +#define SDIO_CCCR_SELx 0x0d
5940 +#define SDIO_CCCR_EXECx 0x0e
5941 +#define SDIO_CCCR_READYx 0x0f
5943 +#define SDIO_CCCR_BLKSIZE 0x10
5945 +#define SDIO_CCCR_POWER 0x12
5947 +#define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
5948 +#define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
5950 +#define SDIO_CCCR_SPEED 0x13
5952 +#define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
5953 +#define SDIO_SPEED_EHS 0x02 /* Enable High-Speed mode */
5956 + * Function Basic Registers (FBR)
5959 +#define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
5961 +#define SDIO_FBR_STD_IF 0x00
5963 +#define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
5964 +#define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
5966 +#define SDIO_FBR_STD_IF_EXT 0x01
5968 +#define SDIO_FBR_POWER 0x02
5970 +#define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
5971 +#define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
5973 +#define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
5976 +#define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
5978 +#define SDIO_FBR_CSA_DATA 0x0F
5980 +#define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
5984 Index: linux-2.6.23.16/include/linux/mmc/sdio_func.h
5985 ===================================================================
5986 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5987 +++ linux-2.6.23.16/include/linux/mmc/sdio_func.h 2008-03-21 17:30:25.000000000 +0100
5990 + * include/linux/mmc/sdio_func.h
5992 + * Copyright 2007 Pierre Ossman
5994 + * This program is free software; you can redistribute it and/or modify
5995 + * it under the terms of the GNU General Public License as published by
5996 + * the Free Software Foundation; either version 2 of the License, or (at
5997 + * your option) any later version.
6000 +#ifndef MMC_SDIO_FUNC_H
6001 +#define MMC_SDIO_FUNC_H
6003 +#include <linux/device.h>
6004 +#include <linux/mod_devicetable.h>
6009 +typedef void (sdio_irq_handler_t)(struct sdio_func *);
6012 + * SDIO function CIS tuple (unknown to the core)
6014 +struct sdio_func_tuple {
6015 + struct sdio_func_tuple *next;
6016 + unsigned char code;
6017 + unsigned char size;
6018 + unsigned char data[0];
6022 + * SDIO function devices
6025 + struct mmc_card *card; /* the card this device belongs to */
6026 + struct device dev; /* the device */
6027 + sdio_irq_handler_t *irq_handler; /* IRQ callback */
6028 + unsigned int num; /* function number */
6030 + unsigned char class; /* standard interface class */
6031 + unsigned short vendor; /* vendor id */
6032 + unsigned short device; /* device id */
6034 + unsigned max_blksize; /* maximum block size */
6035 + unsigned cur_blksize; /* current block size */
6037 + unsigned int state; /* function state */
6038 +#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
6040 + u8 tmpbuf[4]; /* DMA:able scratch buffer */
6042 + unsigned num_info; /* number of info strings */
6043 + const char **info; /* info strings */
6045 + struct sdio_func_tuple *tuples;
6048 +#define sdio_func_present(f) ((f)->state & SDIO_STATE_PRESENT)
6050 +#define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
6052 +#define sdio_func_id(f) ((f)->dev.bus_id)
6054 +#define sdio_get_drvdata(f) dev_get_drvdata(&(f)->dev)
6055 +#define sdio_set_drvdata(f,d) dev_set_drvdata(&(f)->dev, d)
6058 + * SDIO function device driver
6060 +struct sdio_driver {
6062 + const struct sdio_device_id *id_table;
6064 + int (*probe)(struct sdio_func *, const struct sdio_device_id *);
6065 + void (*remove)(struct sdio_func *);
6067 + struct device_driver drv;
6071 + * SDIO_DEVICE - macro used to describe a specific SDIO device
6072 + * @vend: the 16 bit manufacturer code
6073 + * @dev: the 16 bit function id
6075 + * This macro is used to create a struct sdio_device_id that matches a
6076 + * specific device. The class field will be set to SDIO_ANY_ID.
6078 +#define SDIO_DEVICE(vend,dev) \
6079 + .class = SDIO_ANY_ID, \
6080 + .vendor = (vend), .device = (dev)
6083 + * SDIO_DEVICE_CLASS - macro used to describe a specific SDIO device class
6084 + * @dev_class: the 8 bit standard interface code
6086 + * This macro is used to create a struct sdio_device_id that matches a
6087 + * specific standard SDIO function type. The vendor and device fields will
6088 + * be set to SDIO_ANY_ID.
6090 +#define SDIO_DEVICE_CLASS(dev_class) \
6091 + .class = (dev_class), \
6092 + .vendor = SDIO_ANY_ID, .device = SDIO_ANY_ID
6094 +extern int sdio_register_driver(struct sdio_driver *);
6095 +extern void sdio_unregister_driver(struct sdio_driver *);
6098 + * SDIO I/O operations
6100 +extern void sdio_claim_host(struct sdio_func *func);
6101 +extern void sdio_release_host(struct sdio_func *func);
6103 +extern int sdio_enable_func(struct sdio_func *func);
6104 +extern int sdio_disable_func(struct sdio_func *func);
6106 +extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
6108 +extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
6109 +extern int sdio_release_irq(struct sdio_func *func);
6111 +extern unsigned char sdio_readb(struct sdio_func *func,
6112 + unsigned int addr, int *err_ret);
6113 +extern unsigned short sdio_readw(struct sdio_func *func,
6114 + unsigned int addr, int *err_ret);
6115 +extern unsigned long sdio_readl(struct sdio_func *func,
6116 + unsigned int addr, int *err_ret);
6118 +extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
6119 + unsigned int addr, int count);
6120 +extern int sdio_readsb(struct sdio_func *func, void *dst,
6121 + unsigned int addr, int count);
6123 +extern void sdio_writeb(struct sdio_func *func, unsigned char b,
6124 + unsigned int addr, int *err_ret);
6125 +extern void sdio_writew(struct sdio_func *func, unsigned short b,
6126 + unsigned int addr, int *err_ret);
6127 +extern void sdio_writel(struct sdio_func *func, unsigned long b,
6128 + unsigned int addr, int *err_ret);
6130 +extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
6131 + void *src, int count);
6132 +extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
6133 + void *src, int count);
6135 +extern unsigned char sdio_f0_readb(struct sdio_func *func,
6136 + unsigned int addr, int *err_ret);
6137 +extern void sdio_f0_writeb(struct sdio_func *func, unsigned char b,
6138 + unsigned int addr, int *err_ret);
6142 Index: linux-2.6.23.16/include/linux/mmc/sdio_ids.h
6143 ===================================================================
6144 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
6145 +++ linux-2.6.23.16/include/linux/mmc/sdio_ids.h 2008-03-21 17:30:25.000000000 +0100
6148 + * SDIO Classes, Interface Types, Manufacturer IDs, etc.
6151 +#ifndef MMC_SDIO_IDS_H
6152 +#define MMC_SDIO_IDS_H
6155 + * Standard SDIO Function Interfaces
6158 +#define SDIO_CLASS_NONE 0x00 /* Not a SDIO standard interface */
6159 +#define SDIO_CLASS_UART 0x01 /* standard UART interface */
6160 +#define SDIO_CLASS_BT_A 0x02 /* Type-A BlueTooth std interface */
6161 +#define SDIO_CLASS_BT_B 0x03 /* Type-B BlueTooth std interface */
6162 +#define SDIO_CLASS_GPS 0x04 /* GPS standard interface */
6163 +#define SDIO_CLASS_CAMERA 0x05 /* Camera standard interface */
6164 +#define SDIO_CLASS_PHS 0x06 /* PHS standard interface */
6165 +#define SDIO_CLASS_WLAN 0x07 /* WLAN interface */
6166 +#define SDIO_CLASS_ATA 0x08 /* Embedded SDIO-ATA std interface */
6169 + * Vendors and devices. Sort key: vendor first, device next.
6172 +#define SDIO_VENDOR_ID_MARVELL 0x02df
6173 +#define SDIO_DEVICE_ID_MARVELL_LIBERTAS 0x9103
6176 Index: linux-2.6.23.16/include/linux/mod_devicetable.h
6177 ===================================================================
6178 --- linux-2.6.23.16.orig/include/linux/mod_devicetable.h 2008-03-21 17:28:37.000000000 +0100
6179 +++ linux-2.6.23.16/include/linux/mod_devicetable.h 2008-03-21 17:32:50.000000000 +0100
6180 @@ -22,6 +22,18 @@ struct pci_device_id {
6186 +#define SDIO_ANY_ID (~0)
6188 +struct sdio_device_id {
6189 + __u8 class; /* Standard interface or SDIO_ANY_ID */
6190 + __u16 vendor; /* Vendor or SDIO_ANY_ID */
6191 + __u16 device; /* Device ID or SDIO_ANY_ID */
6192 + kernel_ulong_t driver_data; /* Data private to the driver */
6196 #define IEEE1394_MATCH_VENDOR_ID 0x0001
6197 #define IEEE1394_MATCH_MODEL_ID 0x0002
6198 #define IEEE1394_MATCH_SPECIFIER_ID 0x0004
6199 Index: linux-2.6.23.16/drivers/mmc/card/Kconfig
6200 ===================================================================
6201 --- linux-2.6.23.16.orig/drivers/mmc/card/Kconfig 2008-03-21 17:28:26.000000000 +0100
6202 +++ linux-2.6.23.16/drivers/mmc/card/Kconfig 2008-03-21 17:30:25.000000000 +0100
6203 @@ -32,3 +32,10 @@ config MMC_BLOCK_BOUNCE
6205 If unsure, say Y here.
6208 + tristate "SDIO UART/GPS class support"
6211 + SDIO function driver for SDIO cards that implements the UART
6212 + class, as well as the GPS class which appears like a UART.
6214 Index: linux-2.6.23.16/drivers/mmc/card/Makefile
6215 ===================================================================
6216 --- linux-2.6.23.16.orig/drivers/mmc/card/Makefile 2008-03-21 17:28:26.000000000 +0100
6217 +++ linux-2.6.23.16/drivers/mmc/card/Makefile 2008-03-21 17:30:25.000000000 +0100
6218 @@ -9,3 +9,5 @@ endif
6219 obj-$(CONFIG_MMC_BLOCK) += mmc_block.o
6220 mmc_block-objs := block.o queue.o
6222 +obj-$(CONFIG_SDIO_UART) += sdio_uart.o
6224 Index: linux-2.6.23.16/drivers/mmc/card/block.c
6225 ===================================================================
6226 --- linux-2.6.23.16.orig/drivers/mmc/card/block.c 2008-03-21 17:28:26.000000000 +0100
6227 +++ linux-2.6.23.16/drivers/mmc/card/block.c 2008-03-21 17:30:25.000000000 +0100
6229 * max 8 partitions per card
6232 +#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6234 +static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6237 * There is one mmc_blk_data per slot.
6238 @@ -80,6 +83,9 @@ static void mmc_blk_put(struct mmc_blk_d
6239 mutex_lock(&open_lock);
6241 if (md->usage == 0) {
6242 + int devidx = md->disk->first_minor >> MMC_SHIFT;
6243 + __clear_bit(devidx, dev_use);
6248 @@ -151,17 +157,19 @@ static u32 mmc_sd_num_wr_blocks(struct m
6250 cmd.opcode = MMC_APP_CMD;
6251 cmd.arg = card->rca << 16;
6252 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6253 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6255 err = mmc_wait_for_cmd(card->host, &cmd, 0);
6256 - if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
6259 + if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
6262 memset(&cmd, 0, sizeof(struct mmc_command));
6264 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
6266 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6267 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6269 memset(&data, 0, sizeof(struct mmc_data));
6271 @@ -192,7 +200,7 @@ static u32 mmc_sd_num_wr_blocks(struct m
6273 mmc_wait_for_req(card->host, &mrq);
6275 - if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
6276 + if (cmd.error || data.error)
6279 blocks = ntohl(blocks);
6280 @@ -220,17 +228,15 @@ static int mmc_blk_issue_rq(struct mmc_q
6281 brq.cmd.arg = req->sector;
6282 if (!mmc_card_blockaddr(card))
6284 - brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6285 + brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6286 brq.data.blksz = 1 << md->block_bits;
6287 brq.stop.opcode = MMC_STOP_TRANSMISSION;
6289 - brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
6290 + brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
6291 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
6292 if (brq.data.blocks > card->host->max_blk_count)
6293 brq.data.blocks = card->host->max_blk_count;
6295 - mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
6298 * If the host doesn't support multiple block writes, force
6299 * block writes to single block. SD cards are excepted from
6300 @@ -243,8 +249,12 @@ static int mmc_blk_issue_rq(struct mmc_q
6301 brq.data.blocks = 1;
6303 if (brq.data.blocks > 1) {
6304 - brq.data.flags |= MMC_DATA_MULTI;
6305 - brq.mrq.stop = &brq.stop;
6306 + /* SPI multiblock writes terminate using a special
6307 + * token, not a STOP_TRANSMISSION request.
6309 + if (!mmc_host_is_spi(card->host)
6310 + || rq_data_dir(req) == READ)
6311 + brq.mrq.stop = &brq.stop;
6312 readcmd = MMC_READ_MULTIPLE_BLOCK;
6313 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
6315 @@ -261,6 +271,8 @@ static int mmc_blk_issue_rq(struct mmc_q
6316 brq.data.flags |= MMC_DATA_WRITE;
6319 + mmc_set_data_timeout(&brq.data, card);
6321 brq.data.sg = mq->sg;
6322 brq.data.sg_len = mmc_queue_map_sg(mq);
6324 @@ -302,7 +314,7 @@ static int mmc_blk_issue_rq(struct mmc_q
6328 - if (rq_data_dir(req) != READ) {
6329 + if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
6333 @@ -315,7 +327,13 @@ static int mmc_blk_issue_rq(struct mmc_q
6334 req->rq_disk->disk_name, err);
6337 - } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
6339 + * Some cards mishandle the status bits,
6340 + * so make sure to check both the busy
6341 + * indication and the card state.
6343 + } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
6344 + (R1_CURRENT_STATE(cmd.resp[0]) == 7));
6347 if (cmd.resp[0] & ~0x00000900)
6348 @@ -394,9 +412,6 @@ static int mmc_blk_issue_rq(struct mmc_q
6352 -#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6354 -static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6356 static inline int mmc_blk_readonly(struct mmc_card *card)
6358 @@ -510,7 +525,7 @@ mmc_blk_set_blksize(struct mmc_blk_data
6359 mmc_claim_host(card->host);
6360 cmd.opcode = MMC_SET_BLOCKLEN;
6361 cmd.arg = 1 << md->block_bits;
6362 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6363 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6364 err = mmc_wait_for_cmd(card->host, &cmd, 5);
6365 mmc_release_host(card->host);
6367 @@ -562,17 +577,12 @@ static void mmc_blk_remove(struct mmc_ca
6368 struct mmc_blk_data *md = mmc_get_drvdata(card);
6373 /* Stop new requests from getting into the queue */
6374 del_gendisk(md->disk);
6376 /* Then flush out any already in there */
6377 mmc_cleanup_queue(&md->queue);
6379 - devidx = md->disk->first_minor >> MMC_SHIFT;
6380 - __clear_bit(devidx, dev_use);
6384 mmc_set_drvdata(card, NULL);
6385 Index: linux-2.6.23.16/drivers/mmc/card/queue.c
6386 ===================================================================
6387 --- linux-2.6.23.16.orig/drivers/mmc/card/queue.c 2008-03-21 17:28:26.000000000 +0100
6388 +++ linux-2.6.23.16/drivers/mmc/card/queue.c 2008-03-21 17:30:25.000000000 +0100
6390 #include <linux/blkdev.h>
6391 #include <linux/freezer.h>
6392 #include <linux/kthread.h>
6393 +#include <linux/scatterlist.h>
6395 #include <linux/mmc/card.h>
6396 #include <linux/mmc/host.h>
6399 #define MMC_QUEUE_SUSPENDED (1 << 0)
6401 +#define sg_init_table(sg, n) do { \
6402 + memset(sg, 0, sizeof(*(sg)) * (n)); \
6405 +#define sg_virt(sg) (page_address((sg)->page) + (sg)->offset)
6408 * Prepare a MMC request. This just filters out odd stuff.
6410 @@ -159,6 +166,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6414 + sg_init_table(mq->sg, 1);
6416 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
6417 bouncesz / 512, GFP_KERNEL);
6418 @@ -166,6 +174,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6422 + sg_init_table(mq->bounce_sg, bouncesz / 512);
6426 @@ -183,6 +192,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6430 + sg_init_table(mq->sg, host->max_phys_segs);
6433 init_MUTEX(&mq->thread_sem);
6434 @@ -302,12 +312,12 @@ static void copy_sg(struct scatterlist *
6435 BUG_ON(dst_len == 0);
6437 if (dst_size == 0) {
6438 - dst_buf = page_address(dst->page) + dst->offset;
6439 + dst_buf = sg_virt(dst);
6440 dst_size = dst->length;
6443 if (src_size == 0) {
6444 - src_buf = page_address(src->page) + src->offset;
6445 + src_buf = sg_virt(src);
6446 src_size = src->length;
6449 @@ -353,9 +363,7 @@ unsigned int mmc_queue_map_sg(struct mmc
6453 - mq->sg[0].page = virt_to_page(mq->bounce_buf);
6454 - mq->sg[0].offset = offset_in_page(mq->bounce_buf);
6455 - mq->sg[0].length = 0;
6456 + sg_init_one(mq->sg, mq->bounce_buf, 0);
6459 mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
6460 Index: linux-2.6.23.16/drivers/mmc/card/sdio_uart.c
6461 ===================================================================
6462 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
6463 +++ linux-2.6.23.16/drivers/mmc/card/sdio_uart.c 2008-03-21 17:30:25.000000000 +0100
6466 + * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
6468 + * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
6469 + * by Russell King.
6471 + * Author: Nicolas Pitre
6472 + * Created: June 15, 2007
6473 + * Copyright: MontaVista Software, Inc.
6475 + * This program is free software; you can redistribute it and/or modify
6476 + * it under the terms of the GNU General Public License as published by
6477 + * the Free Software Foundation; either version 2 of the License, or (at
6478 + * your option) any later version.
6482 + * Note: Although this driver assumes a 16550A-like UART implementation,
6483 + * it is not possible to leverage the common 8250/16550 driver, nor the
6484 + * core UART infrastructure, as they assumes direct access to the hardware
6485 + * registers, often under a spinlock. This is not possible in the SDIO
6486 + * context as SDIO access functions must be able to sleep.
6488 + * Because we need to lock the SDIO host to ensure an exclusive access to
6489 + * the card, we simply rely on that lock to also prevent and serialize
6490 + * concurrent access to the same port.
6493 +#include <linux/module.h>
6494 +#include <linux/init.h>
6495 +#include <linux/kernel.h>
6496 +#include <linux/mutex.h>
6497 +#include <linux/serial_reg.h>
6498 +#include <linux/circ_buf.h>
6499 +#include <linux/gfp.h>
6500 +#include <linux/tty.h>
6501 +#include <linux/tty_flip.h>
6503 +#include <linux/mmc/core.h>
6504 +#include <linux/mmc/card.h>
6505 +#include <linux/mmc/sdio_func.h>
6506 +#include <linux/mmc/sdio_ids.h>
6509 +#define UART_NR 8 /* Number of UARTs this driver can handle */
6512 +#define UART_XMIT_SIZE PAGE_SIZE
6513 +#define WAKEUP_CHARS 256
6515 +#define circ_empty(circ) ((circ)->head == (circ)->tail)
6516 +#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
6518 +#define circ_chars_pending(circ) \
6519 + (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6521 +#define circ_chars_free(circ) \
6522 + (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6525 +struct uart_icount {
6538 +struct sdio_uart_port {
6540 + struct tty_struct *tty;
6541 + unsigned int index;
6542 + unsigned int opened;
6543 + struct mutex open_lock;
6544 + struct sdio_func *func;
6545 + struct mutex func_lock;
6546 + struct task_struct *in_sdio_uart_irq;
6547 + unsigned int regs_offset;
6548 + struct circ_buf xmit;
6549 + spinlock_t write_lock;
6550 + struct uart_icount icount;
6551 + unsigned int uartclk;
6552 + unsigned int mctrl;
6553 + unsigned int read_status_mask;
6554 + unsigned int ignore_status_mask;
6555 + unsigned char x_char;
6556 + unsigned char ier;
6557 + unsigned char lcr;
6560 +static struct sdio_uart_port *sdio_uart_table[UART_NR];
6561 +static DEFINE_SPINLOCK(sdio_uart_table_lock);
6563 +static int sdio_uart_add_port(struct sdio_uart_port *port)
6565 + int index, ret = -EBUSY;
6567 + kref_init(&port->kref);
6568 + mutex_init(&port->open_lock);
6569 + mutex_init(&port->func_lock);
6570 + spin_lock_init(&port->write_lock);
6572 + spin_lock(&sdio_uart_table_lock);
6573 + for (index = 0; index < UART_NR; index++) {
6574 + if (!sdio_uart_table[index]) {
6575 + port->index = index;
6576 + sdio_uart_table[index] = port;
6581 + spin_unlock(&sdio_uart_table_lock);
6586 +static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
6588 + struct sdio_uart_port *port;
6590 + if (index >= UART_NR)
6593 + spin_lock(&sdio_uart_table_lock);
6594 + port = sdio_uart_table[index];
6596 + kref_get(&port->kref);
6597 + spin_unlock(&sdio_uart_table_lock);
6602 +static void sdio_uart_port_destroy(struct kref *kref)
6604 + struct sdio_uart_port *port =
6605 + container_of(kref, struct sdio_uart_port, kref);
6609 +static void sdio_uart_port_put(struct sdio_uart_port *port)
6611 + kref_put(&port->kref, sdio_uart_port_destroy);
6614 +static void sdio_uart_port_remove(struct sdio_uart_port *port)
6616 + struct sdio_func *func;
6618 + BUG_ON(sdio_uart_table[port->index] != port);
6620 + spin_lock(&sdio_uart_table_lock);
6621 + sdio_uart_table[port->index] = NULL;
6622 + spin_unlock(&sdio_uart_table_lock);
6625 + * We're killing a port that potentially still is in use by
6626 + * the tty layer. Be careful to prevent any further access
6627 + * to the SDIO function and arrange for the tty layer to
6628 + * give up on that port ASAP.
6629 + * Beware: the lock ordering is critical.
6631 + mutex_lock(&port->open_lock);
6632 + mutex_lock(&port->func_lock);
6633 + func = port->func;
6634 + sdio_claim_host(func);
6635 + port->func = NULL;
6636 + mutex_unlock(&port->func_lock);
6638 + tty_hangup(port->tty);
6639 + mutex_unlock(&port->open_lock);
6640 + sdio_release_irq(func);
6641 + sdio_disable_func(func);
6642 + sdio_release_host(func);
6644 + sdio_uart_port_put(port);
6647 +static int sdio_uart_claim_func(struct sdio_uart_port *port)
6649 + mutex_lock(&port->func_lock);
6650 + if (unlikely(!port->func)) {
6651 + mutex_unlock(&port->func_lock);
6654 + if (likely(port->in_sdio_uart_irq != current))
6655 + sdio_claim_host(port->func);
6656 + mutex_unlock(&port->func_lock);
6660 +static inline void sdio_uart_release_func(struct sdio_uart_port *port)
6662 + if (likely(port->in_sdio_uart_irq != current))
6663 + sdio_release_host(port->func);
6666 +static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
6669 + c = sdio_readb(port->func, port->regs_offset + offset, NULL);
6673 +static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
6675 + sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
6678 +static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
6680 + unsigned char status;
6683 + status = sdio_in(port, UART_MSR);
6686 + if (status & UART_MSR_DCD)
6688 + if (status & UART_MSR_RI)
6690 + if (status & UART_MSR_DSR)
6692 + if (status & UART_MSR_CTS)
6697 +static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
6699 + unsigned char mcr = 0;
6701 + if (mctrl & TIOCM_RTS)
6702 + mcr |= UART_MCR_RTS;
6703 + if (mctrl & TIOCM_DTR)
6704 + mcr |= UART_MCR_DTR;
6705 + if (mctrl & TIOCM_OUT1)
6706 + mcr |= UART_MCR_OUT1;
6707 + if (mctrl & TIOCM_OUT2)
6708 + mcr |= UART_MCR_OUT2;
6709 + if (mctrl & TIOCM_LOOP)
6710 + mcr |= UART_MCR_LOOP;
6712 + sdio_out(port, UART_MCR, mcr);
6715 +static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
6716 + unsigned int set, unsigned int clear)
6720 + old = port->mctrl;
6721 + port->mctrl = (old & ~clear) | set;
6722 + if (old != port->mctrl)
6723 + sdio_uart_write_mctrl(port, port->mctrl);
6726 +#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
6727 +#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
6729 +static void sdio_uart_change_speed(struct sdio_uart_port *port,
6730 + struct ktermios *termios,
6731 + struct ktermios *old)
6733 + unsigned char cval, fcr = 0;
6734 + unsigned int baud, quot;
6736 + switch (termios->c_cflag & CSIZE) {
6738 + cval = UART_LCR_WLEN5;
6741 + cval = UART_LCR_WLEN6;
6744 + cval = UART_LCR_WLEN7;
6748 + cval = UART_LCR_WLEN8;
6752 + if (termios->c_cflag & CSTOPB)
6753 + cval |= UART_LCR_STOP;
6754 + if (termios->c_cflag & PARENB)
6755 + cval |= UART_LCR_PARITY;
6756 + if (!(termios->c_cflag & PARODD))
6757 + cval |= UART_LCR_EPAR;
6760 + baud = tty_termios_baud_rate(termios);
6762 + baud = 9600; /* Special case: B0 rate. */
6763 + if (baud <= port->uartclk)
6766 + * Oops, the quotient was zero. Try again with the old
6767 + * baud rate if possible, otherwise default to 9600.
6769 + termios->c_cflag &= ~CBAUD;
6771 + termios->c_cflag |= old->c_cflag & CBAUD;
6774 + termios->c_cflag |= B9600;
6776 + quot = (2 * port->uartclk + baud) / (2 * baud);
6779 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
6781 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
6783 + port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
6784 + if (termios->c_iflag & INPCK)
6785 + port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
6786 + if (termios->c_iflag & (BRKINT | PARMRK))
6787 + port->read_status_mask |= UART_LSR_BI;
6790 + * Characters to ignore
6792 + port->ignore_status_mask = 0;
6793 + if (termios->c_iflag & IGNPAR)
6794 + port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
6795 + if (termios->c_iflag & IGNBRK) {
6796 + port->ignore_status_mask |= UART_LSR_BI;
6798 + * If we're ignoring parity and break indicators,
6799 + * ignore overruns too (for real raw support).
6801 + if (termios->c_iflag & IGNPAR)
6802 + port->ignore_status_mask |= UART_LSR_OE;
6806 + * ignore all characters if CREAD is not set
6808 + if ((termios->c_cflag & CREAD) == 0)
6809 + port->ignore_status_mask |= UART_LSR_DR;
6812 + * CTS flow control flag and modem status interrupts
6814 + port->ier &= ~UART_IER_MSI;
6815 + if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
6816 + port->ier |= UART_IER_MSI;
6820 + sdio_out(port, UART_IER, port->ier);
6821 + sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
6822 + sdio_out(port, UART_DLL, quot & 0xff);
6823 + sdio_out(port, UART_DLM, quot >> 8);
6824 + sdio_out(port, UART_LCR, cval);
6825 + sdio_out(port, UART_FCR, fcr);
6827 + sdio_uart_write_mctrl(port, port->mctrl);
6830 +static void sdio_uart_start_tx(struct sdio_uart_port *port)
6832 + if (!(port->ier & UART_IER_THRI)) {
6833 + port->ier |= UART_IER_THRI;
6834 + sdio_out(port, UART_IER, port->ier);
6838 +static void sdio_uart_stop_tx(struct sdio_uart_port *port)
6840 + if (port->ier & UART_IER_THRI) {
6841 + port->ier &= ~UART_IER_THRI;
6842 + sdio_out(port, UART_IER, port->ier);
6846 +static void sdio_uart_stop_rx(struct sdio_uart_port *port)
6848 + port->ier &= ~UART_IER_RLSI;
6849 + port->read_status_mask &= ~UART_LSR_DR;
6850 + sdio_out(port, UART_IER, port->ier);
6853 +static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
6855 + struct tty_struct *tty = port->tty;
6856 + unsigned int ch, flag;
6857 + int max_count = 256;
6860 + ch = sdio_in(port, UART_RX);
6861 + flag = TTY_NORMAL;
6862 + port->icount.rx++;
6864 + if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
6865 + UART_LSR_FE | UART_LSR_OE))) {
6867 + * For statistics only
6869 + if (*status & UART_LSR_BI) {
6870 + *status &= ~(UART_LSR_FE | UART_LSR_PE);
6871 + port->icount.brk++;
6872 + } else if (*status & UART_LSR_PE)
6873 + port->icount.parity++;
6874 + else if (*status & UART_LSR_FE)
6875 + port->icount.frame++;
6876 + if (*status & UART_LSR_OE)
6877 + port->icount.overrun++;
6880 + * Mask off conditions which should be ignored.
6882 + *status &= port->read_status_mask;
6883 + if (*status & UART_LSR_BI) {
6885 + } else if (*status & UART_LSR_PE)
6886 + flag = TTY_PARITY;
6887 + else if (*status & UART_LSR_FE)
6891 + if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
6892 + tty_insert_flip_char(tty, ch, flag);
6895 + * Overrun is special. Since it's reported immediately,
6896 + * it doesn't affect the current character.
6898 + if (*status & ~port->ignore_status_mask & UART_LSR_OE)
6899 + tty_insert_flip_char(tty, 0, TTY_OVERRUN);
6901 + *status = sdio_in(port, UART_LSR);
6902 + } while ((*status & UART_LSR_DR) && (max_count-- > 0));
6903 + tty_flip_buffer_push(tty);
6906 +static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
6908 + struct circ_buf *xmit = &port->xmit;
6911 + if (port->x_char) {
6912 + sdio_out(port, UART_TX, port->x_char);
6913 + port->icount.tx++;
6917 + if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
6918 + sdio_uart_stop_tx(port);
6924 + sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
6925 + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
6926 + port->icount.tx++;
6927 + if (circ_empty(xmit))
6929 + } while (--count > 0);
6931 + if (circ_chars_pending(xmit) < WAKEUP_CHARS)
6932 + tty_wakeup(port->tty);
6934 + if (circ_empty(xmit))
6935 + sdio_uart_stop_tx(port);
6938 +static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
6942 + status = sdio_in(port, UART_MSR);
6944 + if ((status & UART_MSR_ANY_DELTA) == 0)
6947 + if (status & UART_MSR_TERI)
6948 + port->icount.rng++;
6949 + if (status & UART_MSR_DDSR)
6950 + port->icount.dsr++;
6951 + if (status & UART_MSR_DDCD)
6952 + port->icount.dcd++;
6953 + if (status & UART_MSR_DCTS) {
6954 + port->icount.cts++;
6955 + if (port->tty->termios->c_cflag & CRTSCTS) {
6956 + int cts = (status & UART_MSR_CTS);
6957 + if (port->tty->hw_stopped) {
6959 + port->tty->hw_stopped = 0;
6960 + sdio_uart_start_tx(port);
6961 + tty_wakeup(port->tty);
6965 + port->tty->hw_stopped = 1;
6966 + sdio_uart_stop_tx(port);
6974 + * This handles the interrupt from one port.
6976 +static void sdio_uart_irq(struct sdio_func *func)
6978 + struct sdio_uart_port *port = sdio_get_drvdata(func);
6979 + unsigned int iir, lsr;
6982 + * In a few places sdio_uart_irq() is called directly instead of
6983 + * waiting for the actual interrupt to be raised and the SDIO IRQ
6984 + * thread scheduled in order to reduce latency. However, some
6985 + * interaction with the tty core may end up calling us back
6986 + * (serial echo, flow control, etc.) through those same places
6987 + * causing undesirable effects. Let's stop the recursion here.
6989 + if (unlikely(port->in_sdio_uart_irq == current))
6992 + iir = sdio_in(port, UART_IIR);
6993 + if (iir & UART_IIR_NO_INT)
6996 + port->in_sdio_uart_irq = current;
6997 + lsr = sdio_in(port, UART_LSR);
6998 + if (lsr & UART_LSR_DR)
6999 + sdio_uart_receive_chars(port, &lsr);
7000 + sdio_uart_check_modem_status(port);
7001 + if (lsr & UART_LSR_THRE)
7002 + sdio_uart_transmit_chars(port);
7003 + port->in_sdio_uart_irq = NULL;
7006 +static int sdio_uart_startup(struct sdio_uart_port *port)
7008 + unsigned long page;
7012 + * Set the TTY IO error marker - we will only clear this
7013 + * once we have successfully opened the port.
7015 + set_bit(TTY_IO_ERROR, &port->tty->flags);
7017 + /* Initialise and allocate the transmit buffer. */
7018 + page = __get_free_page(GFP_KERNEL);
7021 + port->xmit.buf = (unsigned char *)page;
7022 + circ_clear(&port->xmit);
7024 + ret = sdio_uart_claim_func(port);
7027 + ret = sdio_enable_func(port->func);
7030 + ret = sdio_claim_irq(port->func, sdio_uart_irq);
7035 + * Clear the FIFO buffers and disable them.
7036 + * (they will be reenabled in sdio_change_speed())
7038 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
7039 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7040 + UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
7041 + sdio_out(port, UART_FCR, 0);
7044 + * Clear the interrupt registers.
7046 + (void) sdio_in(port, UART_LSR);
7047 + (void) sdio_in(port, UART_RX);
7048 + (void) sdio_in(port, UART_IIR);
7049 + (void) sdio_in(port, UART_MSR);
7052 + * Now, initialize the UART
7054 + sdio_out(port, UART_LCR, UART_LCR_WLEN8);
7056 + port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
7057 + port->mctrl = TIOCM_OUT2;
7059 + sdio_uart_change_speed(port, port->tty->termios, NULL);
7061 + if (port->tty->termios->c_cflag & CBAUD)
7062 + sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7064 + if (port->tty->termios->c_cflag & CRTSCTS)
7065 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
7066 + port->tty->hw_stopped = 1;
7068 + clear_bit(TTY_IO_ERROR, &port->tty->flags);
7070 + /* Kick the IRQ handler once while we're still holding the host lock */
7071 + sdio_uart_irq(port->func);
7073 + sdio_uart_release_func(port);
7077 + sdio_disable_func(port->func);
7079 + sdio_uart_release_func(port);
7081 + free_page((unsigned long)port->xmit.buf);
7085 +static void sdio_uart_shutdown(struct sdio_uart_port *port)
7089 + ret = sdio_uart_claim_func(port);
7093 + sdio_uart_stop_rx(port);
7095 + /* TODO: wait here for TX FIFO to drain */
7097 + /* Turn off DTR and RTS early. */
7098 + if (port->tty->termios->c_cflag & HUPCL)
7099 + sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
7101 + /* Disable interrupts from this port */
7102 + sdio_release_irq(port->func);
7104 + sdio_out(port, UART_IER, 0);
7106 + sdio_uart_clear_mctrl(port, TIOCM_OUT2);
7108 + /* Disable break condition and FIFOs. */
7109 + port->lcr &= ~UART_LCR_SBC;
7110 + sdio_out(port, UART_LCR, port->lcr);
7111 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7112 + UART_FCR_CLEAR_RCVR |
7113 + UART_FCR_CLEAR_XMIT);
7114 + sdio_out(port, UART_FCR, 0);
7116 + sdio_disable_func(port->func);
7118 + sdio_uart_release_func(port);
7121 + /* Free the transmit buffer page. */
7122 + free_page((unsigned long)port->xmit.buf);
7125 +static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
7127 + struct sdio_uart_port *port;
7130 + port = sdio_uart_port_get(tty->index);
7134 + mutex_lock(&port->open_lock);
7137 + * Make sure not to mess up with a dead port
7138 + * which has not been closed yet.
7140 + if (tty->driver_data && tty->driver_data != port) {
7141 + mutex_unlock(&port->open_lock);
7142 + sdio_uart_port_put(port);
7146 + if (!port->opened) {
7147 + tty->driver_data = port;
7149 + ret = sdio_uart_startup(port);
7151 + tty->driver_data = NULL;
7153 + mutex_unlock(&port->open_lock);
7154 + sdio_uart_port_put(port);
7159 + mutex_unlock(&port->open_lock);
7163 +static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
7165 + struct sdio_uart_port *port = tty->driver_data;
7170 + mutex_lock(&port->open_lock);
7171 + BUG_ON(!port->opened);
7174 + * This is messy. The tty layer calls us even when open()
7175 + * returned an error. Ignore this close request if tty->count
7176 + * is larger than port->count.
7178 + if (tty->count > port->opened) {
7179 + mutex_unlock(&port->open_lock);
7183 + if (--port->opened == 0) {
7185 + sdio_uart_shutdown(port);
7186 + tty_ldisc_flush(tty);
7188 + tty->driver_data = NULL;
7191 + mutex_unlock(&port->open_lock);
7192 + sdio_uart_port_put(port);
7195 +static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
7198 + struct sdio_uart_port *port = tty->driver_data;
7199 + struct circ_buf *circ = &port->xmit;
7205 + spin_lock(&port->write_lock);
7207 + c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
7212 + memcpy(circ->buf + circ->head, buf, c);
7213 + circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
7218 + spin_unlock(&port->write_lock);
7220 + if ( !(port->ier & UART_IER_THRI)) {
7221 + int err = sdio_uart_claim_func(port);
7223 + sdio_uart_start_tx(port);
7224 + sdio_uart_irq(port->func);
7225 + sdio_uart_release_func(port);
7233 +static int sdio_uart_write_room(struct tty_struct *tty)
7235 + struct sdio_uart_port *port = tty->driver_data;
7236 + return port ? circ_chars_free(&port->xmit) : 0;
7239 +static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
7241 + struct sdio_uart_port *port = tty->driver_data;
7242 + return port ? circ_chars_pending(&port->xmit) : 0;
7245 +static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
7247 + struct sdio_uart_port *port = tty->driver_data;
7249 + port->x_char = ch;
7250 + if (ch && !(port->ier & UART_IER_THRI)) {
7251 + if (sdio_uart_claim_func(port) != 0)
7253 + sdio_uart_start_tx(port);
7254 + sdio_uart_irq(port->func);
7255 + sdio_uart_release_func(port);
7259 +static void sdio_uart_throttle(struct tty_struct *tty)
7261 + struct sdio_uart_port *port = tty->driver_data;
7263 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7266 + if (sdio_uart_claim_func(port) != 0)
7269 + if (I_IXOFF(tty)) {
7270 + port->x_char = STOP_CHAR(tty);
7271 + sdio_uart_start_tx(port);
7274 + if (tty->termios->c_cflag & CRTSCTS)
7275 + sdio_uart_clear_mctrl(port, TIOCM_RTS);
7277 + sdio_uart_irq(port->func);
7278 + sdio_uart_release_func(port);
7281 +static void sdio_uart_unthrottle(struct tty_struct *tty)
7283 + struct sdio_uart_port *port = tty->driver_data;
7285 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7288 + if (sdio_uart_claim_func(port) != 0)
7291 + if (I_IXOFF(tty)) {
7292 + if (port->x_char) {
7295 + port->x_char = START_CHAR(tty);
7296 + sdio_uart_start_tx(port);
7300 + if (tty->termios->c_cflag & CRTSCTS)
7301 + sdio_uart_set_mctrl(port, TIOCM_RTS);
7303 + sdio_uart_irq(port->func);
7304 + sdio_uart_release_func(port);
7307 +static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
7309 + struct sdio_uart_port *port = tty->driver_data;
7310 + unsigned int cflag = tty->termios->c_cflag;
7312 +#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
7314 + if ((cflag ^ old_termios->c_cflag) == 0 &&
7315 + RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
7318 + if (sdio_uart_claim_func(port) != 0)
7321 + sdio_uart_change_speed(port, tty->termios, old_termios);
7323 + /* Handle transition to B0 status */
7324 + if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
7325 + sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7327 + /* Handle transition away from B0 status */
7328 + if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
7329 + unsigned int mask = TIOCM_DTR;
7330 + if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
7331 + mask |= TIOCM_RTS;
7332 + sdio_uart_set_mctrl(port, mask);
7335 + /* Handle turning off CRTSCTS */
7336 + if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
7337 + tty->hw_stopped = 0;
7338 + sdio_uart_start_tx(port);
7341 + /* Handle turning on CRTSCTS */
7342 + if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
7343 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
7344 + tty->hw_stopped = 1;
7345 + sdio_uart_stop_tx(port);
7349 + sdio_uart_release_func(port);
7352 +static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
7354 + struct sdio_uart_port *port = tty->driver_data;
7356 + if (sdio_uart_claim_func(port) != 0)
7359 + if (break_state == -1)
7360 + port->lcr |= UART_LCR_SBC;
7362 + port->lcr &= ~UART_LCR_SBC;
7363 + sdio_out(port, UART_LCR, port->lcr);
7365 + sdio_uart_release_func(port);
7368 +static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
7370 + struct sdio_uart_port *port = tty->driver_data;
7373 + result = sdio_uart_claim_func(port);
7375 + result = port->mctrl | sdio_uart_get_mctrl(port);
7376 + sdio_uart_release_func(port);
7382 +static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
7383 + unsigned int set, unsigned int clear)
7385 + struct sdio_uart_port *port = tty->driver_data;
7388 + result =sdio_uart_claim_func(port);
7390 + sdio_uart_update_mctrl(port, set, clear);
7391 + sdio_uart_release_func(port);
7397 +static int sdio_uart_read_proc(char *page, char **start, off_t off,
7398 + int count, int *eof, void *data)
7403 + len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
7405 + for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
7406 + struct sdio_uart_port *port = sdio_uart_port_get(i);
7408 + len += sprintf(page+len, "%d: uart:SDIO", i);
7409 + if(capable(CAP_SYS_ADMIN)) {
7410 + len += sprintf(page + len, " tx:%d rx:%d",
7411 + port->icount.tx, port->icount.rx);
7412 + if (port->icount.frame)
7413 + len += sprintf(page + len, " fe:%d",
7414 + port->icount.frame);
7415 + if (port->icount.parity)
7416 + len += sprintf(page + len, " pe:%d",
7417 + port->icount.parity);
7418 + if (port->icount.brk)
7419 + len += sprintf(page + len, " brk:%d",
7420 + port->icount.brk);
7421 + if (port->icount.overrun)
7422 + len += sprintf(page + len, " oe:%d",
7423 + port->icount.overrun);
7424 + if (port->icount.cts)
7425 + len += sprintf(page + len, " cts:%d",
7426 + port->icount.cts);
7427 + if (port->icount.dsr)
7428 + len += sprintf(page + len, " dsr:%d",
7429 + port->icount.dsr);
7430 + if (port->icount.rng)
7431 + len += sprintf(page + len, " rng:%d",
7432 + port->icount.rng);
7433 + if (port->icount.dcd)
7434 + len += sprintf(page + len, " dcd:%d",
7435 + port->icount.dcd);
7437 + strcat(page, "\n");
7439 + sdio_uart_port_put(port);
7442 + if (len + begin > off + count)
7444 + if (len + begin < off) {
7452 + if (off >= len + begin)
7454 + *start = page + (off - begin);
7455 + return (count < begin + len - off) ? count : (begin + len - off);
7458 +static const struct tty_operations sdio_uart_ops = {
7459 + .open = sdio_uart_open,
7460 + .close = sdio_uart_close,
7461 + .write = sdio_uart_write,
7462 + .write_room = sdio_uart_write_room,
7463 + .chars_in_buffer = sdio_uart_chars_in_buffer,
7464 + .send_xchar = sdio_uart_send_xchar,
7465 + .throttle = sdio_uart_throttle,
7466 + .unthrottle = sdio_uart_unthrottle,
7467 + .set_termios = sdio_uart_set_termios,
7468 + .break_ctl = sdio_uart_break_ctl,
7469 + .tiocmget = sdio_uart_tiocmget,
7470 + .tiocmset = sdio_uart_tiocmset,
7471 + .read_proc = sdio_uart_read_proc,
7474 +static struct tty_driver *sdio_uart_tty_driver;
7476 +static int sdio_uart_probe(struct sdio_func *func,
7477 + const struct sdio_device_id *id)
7479 + struct sdio_uart_port *port;
7482 + port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
7486 + if (func->class == SDIO_CLASS_UART) {
7487 + printk(KERN_WARNING "%s: need info on UART class basic setup\n",
7488 + sdio_func_id(func));
7491 + } else if (func->class == SDIO_CLASS_GPS) {
7493 + * We need tuple 0x91. It contains SUBTPL_SIOREG
7494 + * and SUBTPL_RCVCAPS.
7496 + struct sdio_func_tuple *tpl;
7497 + for (tpl = func->tuples; tpl; tpl = tpl->next) {
7498 + if (tpl->code != 0x91)
7500 + if (tpl->size < 10)
7502 + if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
7506 + printk(KERN_WARNING
7507 + "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
7508 + sdio_func_id(func));
7512 + printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
7513 + sdio_func_id(func), tpl->data[2], tpl->data[3]);
7514 + port->regs_offset = (tpl->data[4] << 0) |
7515 + (tpl->data[5] << 8) |
7516 + (tpl->data[6] << 16);
7517 + printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
7518 + sdio_func_id(func), port->regs_offset);
7519 + port->uartclk = tpl->data[7] * 115200;
7520 + if (port->uartclk == 0)
7521 + port->uartclk = 115200;
7522 + printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
7523 + sdio_func_id(func), port->uartclk,
7524 + tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
7530 + port->func = func;
7531 + sdio_set_drvdata(func, port);
7533 + ret = sdio_uart_add_port(port);
7537 + struct device *dev;
7538 + dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
7539 + if (IS_ERR(dev)) {
7540 + sdio_uart_port_remove(port);
7541 + ret = PTR_ERR(dev);
7548 +static void sdio_uart_remove(struct sdio_func *func)
7550 + struct sdio_uart_port *port = sdio_get_drvdata(func);
7552 + tty_unregister_device(sdio_uart_tty_driver, port->index);
7553 + sdio_uart_port_remove(port);
7556 +static const struct sdio_device_id sdio_uart_ids[] = {
7557 + { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
7558 + { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
7559 + { /* end: all zeroes */ },
7562 +MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
7564 +static struct sdio_driver sdio_uart_driver = {
7565 + .probe = sdio_uart_probe,
7566 + .remove = sdio_uart_remove,
7567 + .name = "sdio_uart",
7568 + .id_table = sdio_uart_ids,
7571 +static int __init sdio_uart_init(void)
7574 + struct tty_driver *tty_drv;
7576 + sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
7580 + tty_drv->owner = THIS_MODULE;
7581 + tty_drv->driver_name = "sdio_uart";
7582 + tty_drv->name = "ttySDIO";
7583 + tty_drv->major = 0; /* dynamically allocated */
7584 + tty_drv->minor_start = 0;
7585 + tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
7586 + tty_drv->subtype = SERIAL_TYPE_NORMAL;
7587 + tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
7588 + tty_drv->init_termios = tty_std_termios;
7589 + tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
7590 + tty_drv->init_termios.c_ispeed = 4800;
7591 + tty_drv->init_termios.c_ospeed = 4800;
7592 + tty_set_operations(tty_drv, &sdio_uart_ops);
7594 + ret = tty_register_driver(tty_drv);
7598 + ret = sdio_register_driver(&sdio_uart_driver);
7605 + tty_unregister_driver(tty_drv);
7607 + put_tty_driver(tty_drv);
7611 +static void __exit sdio_uart_exit(void)
7613 + sdio_unregister_driver(&sdio_uart_driver);
7614 + tty_unregister_driver(sdio_uart_tty_driver);
7615 + put_tty_driver(sdio_uart_tty_driver);
7618 +module_init(sdio_uart_init);
7619 +module_exit(sdio_uart_exit);
7621 +MODULE_AUTHOR("Nicolas Pitre");
7622 +MODULE_LICENSE("GPL");
7623 Index: linux-2.6.23.16/drivers/mmc/core/Makefile
7624 ===================================================================
7625 --- linux-2.6.23.16.orig/drivers/mmc/core/Makefile 2008-03-21 17:28:26.000000000 +0100
7626 +++ linux-2.6.23.16/drivers/mmc/core/Makefile 2008-03-21 17:30:25.000000000 +0100
7627 @@ -8,5 +8,7 @@ endif
7629 obj-$(CONFIG_MMC) += mmc_core.o
7630 mmc_core-y := core.o sysfs.o bus.o host.o \
7631 - mmc.o mmc_ops.o sd.o sd_ops.o
7632 + mmc.o mmc_ops.o sd.o sd_ops.o \
7633 + sdio.o sdio_ops.o sdio_bus.o \
7634 + sdio_cis.o sdio_io.o sdio_irq.o