update iptables to 1.4.0 (2.6 kernels only), refresh kernel patches
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.23 / 300-add-mmc-spi-driver.patch
1 This is a port of the MMC-SPI driver from 2.6.24.3
2 --mb
3
4
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.
12
13 +config MMC_SPI
14 + tristate "MMC/SD over SPI (EXPERIMENTAL)"
15 + depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
16 + select CRC7
17 + select CRC_ITU_T
18 + help
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.
23 +
24 + If unsure, or if your system has no SPI master driver, say N.
25 +
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
35
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
40 @@ -0,0 +1,1419 @@
41 +/*
42 + * mmc_spi.c - Access SD/MMC cards through SPI master controllers
43 + *
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>
51 + *
52 + *
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.
57 + *
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.
62 + *
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.
66 + */
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>
74 +
75 +#include <linux/mmc/host.h>
76 +#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
77 +
78 +#include <linux/spi/spi.h>
79 +#include <linux/spi/mmc_spi.h>
80 +
81 +#include <asm/unaligned.h>
82 +
83 +
84 +#define sg_page(sg) (sg)->page
85 +
86 +
87 +/* NOTES:
88 + *
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.
93 + *
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.
97 + *
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.
102 + *
103 + * Pending updates to the programming interface, this driver expects
104 + * that it not share the bus with other drivers (precluding conflicts).
105 + *
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!
109 + *
110 + * However, many cards seem OK with chipselect flapping up/down
111 + * during that time ... at least on unshared bus segments.
112 + */
113 +
114 +
115 +/*
116 + * Local protocol constants, internal to data block protocols.
117 + */
118 +
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)
124 +
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.
127 + */
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 */
131 +
132 +#define MMC_SPI_BLOCKSIZE 512
133 +
134 +
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?
140 + */
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)
144 +
145 +
146 +/****************************************************************************/
147 +
148 +/*
149 + * Local Data Structures
150 + */
151 +
152 +/* "scratch" is per-{command,block} data exchanged with the card */
153 +struct scratch {
154 + u8 status[29];
155 + u8 data_token;
156 + __be16 crc_val;
157 +};
158 +
159 +struct mmc_spi_host {
160 + struct mmc_host *mmc;
161 + struct spi_device *spi;
162 +
163 + unsigned char power_mode;
164 + u16 powerup_msecs;
165 +
166 + struct mmc_spi_platform_data *pdata;
167 +
168 + /* for bulk data transfers */
169 + struct spi_transfer token, t, crc, early_status;
170 + struct spi_message m;
171 +
172 + /* for status readback */
173 + struct spi_transfer status;
174 + struct spi_message readback;
175 +
176 + /* underlying DMA-aware controller, or null */
177 + struct device *dma_dev;
178 +
179 + /* buffer used for commands and for message "overhead" */
180 + struct scratch *data;
181 + dma_addr_t data_dma;
182 +
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.
186 + */
187 + void *ones;
188 + dma_addr_t ones_dma;
189 +};
190 +
191 +
192 +/****************************************************************************/
193 +
194 +/*
195 + * MMC-over-SPI protocol glue, used by the MMC stack interface
196 + */
197 +
198 +static inline int mmc_cs_off(struct mmc_spi_host *host)
199 +{
200 + /* chipselect will always be inactive after setup() */
201 + return spi_setup(host->spi);
202 +}
203 +
204 +static int
205 +mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
206 +{
207 + int status;
208 +
209 + if (len > sizeof(*host->data)) {
210 + WARN_ON(1);
211 + return -EIO;
212 + }
213 +
214 + host->status.len = len;
215 +
216 + if (host->dma_dev)
217 + dma_sync_single_for_device(host->dma_dev,
218 + host->data_dma, sizeof(*host->data),
219 + DMA_FROM_DEVICE);
220 +
221 + status = spi_sync(host->spi, &host->readback);
222 +
223 + if (host->dma_dev)
224 + dma_sync_single_for_cpu(host->dma_dev,
225 + host->data_dma, sizeof(*host->data),
226 + DMA_FROM_DEVICE);
227 +
228 + return status;
229 +}
230 +
231 +static int
232 +mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
233 +{
234 + u8 *cp = host->data->status;
235 +
236 + timeout = ktime_add(timeout, ktime_get());
237 +
238 + while (1) {
239 + int status;
240 + unsigned i;
241 +
242 + status = mmc_spi_readbytes(host, n);
243 + if (status < 0)
244 + return status;
245 +
246 + for (i = 0; i < n; i++) {
247 + if (cp[i] != byte)
248 + return cp[i];
249 + }
250 +
251 + /* REVISIT investigate msleep() to avoid busy-wait I/O
252 + * in at least some cases.
253 + */
254 + if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
255 + break;
256 + }
257 + return -ETIMEDOUT;
258 +}
259 +
260 +static inline int
261 +mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
262 +{
263 + return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
264 +}
265 +
266 +static int mmc_spi_readtoken(struct mmc_spi_host *host)
267 +{
268 + return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
269 +}
270 +
271 +
272 +/*
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.
276 + *
277 + * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
278 + * newer cards R7 (IF_COND).
279 + */
280 +
281 +static char *maptype(struct mmc_command *cmd)
282 +{
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 "?";
289 + }
290 +}
291 +
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)
295 +{
296 + u8 *cp = host->data->status;
297 + u8 *end = cp + host->t.len;
298 + int value = 0;
299 + char tag[32];
300 +
301 + snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
302 + cmd->opcode, maptype(cmd));
303 +
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.
309 + */
310 + cp += 8;
311 + while (cp < end && *cp == 0xff)
312 + cp++;
313 +
314 + /* Data block reads (R1 response types) may need more data... */
315 + if (cp == end) {
316 + unsigned i;
317 +
318 + cp = host->data->status;
319 +
320 + /* Card sends N(CR) (== 1..8) bytes of all-ones then one
321 + * status byte ... and we already scanned 2 bytes.
322 + *
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...
327 + */
328 + for (i = 2; i < 9; i++) {
329 + value = mmc_spi_readbytes(host, 1);
330 + if (value < 0)
331 + goto done;
332 + if (*cp != 0xff)
333 + goto checkstatus;
334 + }
335 + value = -ETIMEDOUT;
336 + goto done;
337 + }
338 +
339 +checkstatus:
340 + if (*cp & 0x80) {
341 + dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
342 + tag, *cp);
343 + value = -EBADR;
344 + goto done;
345 + }
346 +
347 + cmd->resp[0] = *cp++;
348 + cmd->error = 0;
349 +
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)
354 + & cmd->resp[0])
355 + value = -EINVAL;
356 + else if (R1_SPI_COM_CRC & cmd->resp[0])
357 + value = -EILSEQ;
358 + else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
359 + & cmd->resp[0])
360 + value = -EIO;
361 + /* else R1_SPI_IDLE, "it's resetting" */
362 + }
363 +
364 + switch (mmc_spi_resp_type(cmd)) {
365 +
366 + /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
367 + * and less-common stuff like various erase operations.
368 + */
369 + case MMC_RSP_SPI_R1B:
370 + /* maybe we read all the busy tokens already */
371 + while (cp < end && *cp == 0)
372 + cp++;
373 + if (cp == end)
374 + mmc_spi_wait_unbusy(host, r1b_timeout);
375 + break;
376 +
377 + /* SPI R2 == R1 + second status byte; SEND_STATUS
378 + * SPI R5 == R1 + data byte; IO_RW_DIRECT
379 + */
380 + case MMC_RSP_SPI_R2:
381 + cmd->resp[0] |= *cp << 8;
382 + break;
383 +
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));
387 + break;
388 +
389 + /* SPI R1 == just one status byte */
390 + case MMC_RSP_SPI_R1:
391 + break;
392 +
393 + default:
394 + dev_dbg(&host->spi->dev, "bad response type %04x\n",
395 + mmc_spi_resp_type(cmd));
396 + if (value >= 0)
397 + value = -EINVAL;
398 + goto done;
399 + }
400 +
401 + if (value < 0)
402 + dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
403 + tag, cmd->resp[0], cmd->resp[1]);
404 +
405 + /* disable chipselect on errors and some success cases */
406 + if (value >= 0 && cs_on)
407 + return value;
408 +done:
409 + if (value < 0)
410 + cmd->error = value;
411 + mmc_cs_off(host);
412 + return value;
413 +}
414 +
415 +/* Issue command and read its response.
416 + * Returns zero on success, negative for error.
417 + *
418 + * On error, caller must cope with mmc core retry mechanism. That
419 + * means immediate low-level resubmit, which affects the bus lock...
420 + */
421 +static int
422 +mmc_spi_command_send(struct mmc_spi_host *host,
423 + struct mmc_request *mrq,
424 + struct mmc_command *cmd, int cs_on)
425 +{
426 + struct scratch *data = host->data;
427 + u8 *cp = data->status;
428 + u32 arg = cmd->arg;
429 + int status;
430 + struct spi_transfer *t;
431 +
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.
435 + *
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
441 + *
442 + * We init the whole buffer to all-ones, which is what we need
443 + * to write while we're reading (later) response data.
444 + */
445 + memset(cp++, 0xff, sizeof(data->status));
446 +
447 + *cp++ = 0x40 | cmd->opcode;
448 + *cp++ = (u8)(arg >> 24);
449 + *cp++ = (u8)(arg >> 16);
450 + *cp++ = (u8)(arg >> 8);
451 + *cp++ = (u8)arg;
452 + *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
453 +
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
461 + *
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
467 + *
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).
472 + *
473 + * There are two other cases, where it's not generally practical
474 + * to rely on a single I/O:
475 + *
476 + * - R1B responses need at least N(EC) bytes of all-zeroes.
477 + *
478 + * In this case we can *try* to fit it into one I/O, then
479 + * maybe read more data later.
480 + *
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
485 + *
486 + * In this case we currently only have minimal speedups here:
487 + * when N(CR) == 1 we can avoid I/O in response_get().
488 + */
489 + if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
490 + cp += 2; /* min(N(CR)) + status */
491 + /* R1 */
492 + } else {
493 + cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
494 + if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
495 + cp++;
496 + else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
497 + cp += 4;
498 + else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
499 + cp = data->status + sizeof(data->status);
500 + /* else: R1 (most commands) */
501 + }
502 +
503 + dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
504 + cmd->opcode, maptype(cmd));
505 +
506 + /* send command, leaving chipselect active */
507 + spi_message_init(&host->m);
508 +
509 + t = &host->t;
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;
514 + t->cs_change = 1;
515 + spi_message_add_tail(t, &host->m);
516 +
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);
522 + }
523 + status = spi_sync(host->spi, &host->m);
524 +
525 + if (host->dma_dev)
526 + dma_sync_single_for_cpu(host->dma_dev,
527 + host->data_dma, sizeof(*host->data),
528 + DMA_BIDIRECTIONAL);
529 + if (status < 0) {
530 + dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
531 + cmd->error = status;
532 + return status;
533 + }
534 +
535 + /* after no-data commands and STOP_TRANSMISSION, chipselect off */
536 + return mmc_spi_response_get(host, cmd, cs_on);
537 +}
538 +
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.
542 + *
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.
546 + *
547 + * We also handle DMA mapping, so the underlying SPI controller does
548 + * not need to (re)do it for each message.
549 + */
550 +static void
551 +mmc_spi_setup_data_message(
552 + struct mmc_spi_host *host,
553 + int multiple,
554 + enum dma_data_direction direction)
555 +{
556 + struct spi_transfer *t;
557 + struct scratch *scratch = host->data;
558 + dma_addr_t dma = host->data_dma;
559 +
560 + spi_message_init(&host->m);
561 + if (dma)
562 + host->m.is_dma_mapped = 1;
563 +
564 + /* for reads, readblock() skips 0xff bytes before finding
565 + * the token; for writes, this transfer issues that token.
566 + */
567 + if (direction == DMA_TO_DEVICE) {
568 + t = &host->token;
569 + memset(t, 0, sizeof(*t));
570 + t->len = 1;
571 + if (multiple)
572 + scratch->data_token = SPI_TOKEN_MULTI_WRITE;
573 + else
574 + scratch->data_token = SPI_TOKEN_SINGLE;
575 + t->tx_buf = &scratch->data_token;
576 + if (dma)
577 + t->tx_dma = dma + offsetof(struct scratch, data_token);
578 + spi_message_add_tail(t, &host->m);
579 + }
580 +
581 + /* Body of transfer is buffer, then CRC ...
582 + * either TX-only, or RX with TX-ones.
583 + */
584 + t = &host->t;
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);
590 +
591 + t = &host->crc;
592 + memset(t, 0, sizeof(*t));
593 + t->len = 2;
594 + if (direction == DMA_TO_DEVICE) {
595 + /* the actual CRC may get written later */
596 + t->tx_buf = &scratch->crc_val;
597 + if (dma)
598 + t->tx_dma = dma + offsetof(struct scratch, crc_val);
599 + } else {
600 + t->tx_buf = host->ones;
601 + t->tx_dma = host->ones_dma;
602 + t->rx_buf = &scratch->crc_val;
603 + if (dma)
604 + t->rx_dma = dma + offsetof(struct scratch, crc_val);
605 + }
606 + spi_message_add_tail(t, &host->m);
607 +
608 + /*
609 + * A single block read is followed by N(EC) [0+] all-ones bytes
610 + * before deselect ... don't bother.
611 + *
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.
615 + *
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.
621 + */
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)
627 + : 1;
628 + t->tx_buf = host->ones;
629 + t->tx_dma = host->ones_dma;
630 + t->rx_buf = scratch->status;
631 + if (dma)
632 + t->rx_dma = dma + offsetof(struct scratch, status);
633 + t->cs_change = 1;
634 + spi_message_add_tail(t, &host->m);
635 + }
636 +}
637 +
638 +/*
639 + * Write one block:
640 + * - caller handled preceding N(WR) [1+] all-ones bytes
641 + * - data block
642 + * + token
643 + * + data bytes
644 + * + crc16
645 + * - an all-ones byte ... card writes a data-response byte
646 + * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
647 + *
648 + * Return negative errno, else success.
649 + */
650 +static int
651 +mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
652 +{
653 + struct spi_device *spi = host->spi;
654 + int status, i;
655 + struct scratch *scratch = host->data;
656 +
657 + if (host->mmc->use_spi_crc)
658 + scratch->crc_val = cpu_to_be16(
659 + crc_itu_t(0, t->tx_buf, t->len));
660 + if (host->dma_dev)
661 + dma_sync_single_for_device(host->dma_dev,
662 + host->data_dma, sizeof(*scratch),
663 + DMA_BIDIRECTIONAL);
664 +
665 + status = spi_sync(spi, &host->m);
666 +
667 + if (status != 0) {
668 + dev_dbg(&spi->dev, "write error (%d)\n", status);
669 + return status;
670 + }
671 +
672 + if (host->dma_dev)
673 + dma_sync_single_for_cpu(host->dma_dev,
674 + host->data_dma, sizeof(*scratch),
675 + DMA_BIDIRECTIONAL);
676 +
677 + /*
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.
683 + */
684 + switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
685 + case SPI_RESPONSE_ACCEPTED:
686 + status = 0;
687 + break;
688 + case SPI_RESPONSE_CRC_ERR:
689 + /* host shall then issue MMC_STOP_TRANSMISSION */
690 + status = -EILSEQ;
691 + break;
692 + case SPI_RESPONSE_WRITE_ERR:
693 + /* host shall then issue MMC_STOP_TRANSMISSION,
694 + * and should MMC_SEND_STATUS to sort it out
695 + */
696 + status = -EIO;
697 + break;
698 + default:
699 + status = -EPROTO;
700 + break;
701 + }
702 + if (status != 0) {
703 + dev_dbg(&spi->dev, "write error %02x (%d)\n",
704 + scratch->status[0], status);
705 + return status;
706 + }
707 +
708 + t->tx_buf += t->len;
709 + if (host->dma_dev)
710 + t->tx_dma += t->len;
711 +
712 + /* Return when not busy. If we didn't collect that status yet,
713 + * we'll need some more I/O.
714 + */
715 + for (i = 1; i < sizeof(scratch->status); i++) {
716 + if (scratch->status[i] != 0)
717 + return 0;
718 + }
719 + return mmc_spi_wait_unbusy(host, writeblock_timeout);
720 +}
721 +
722 +/*
723 + * Read one block:
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
727 + * - data block
728 + * + token ... if error token, no data or crc
729 + * + data bytes
730 + * + crc16
731 + *
732 + * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
733 + * before dropping chipselect.
734 + *
735 + * For multiblock reads, caller either reads the next block or issues a
736 + * STOP_TRANSMISSION command.
737 + */
738 +static int
739 +mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
740 +{
741 + struct spi_device *spi = host->spi;
742 + int status;
743 + struct scratch *scratch = host->data;
744 +
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.
747 + */
748 + status = mmc_spi_readbytes(host, 1);
749 + if (status < 0)
750 + return status;
751 + status = scratch->status[0];
752 + if (status == 0xff || status == 0)
753 + status = mmc_spi_readtoken(host);
754 +
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,
761 + t->rx_dma, t->len,
762 + DMA_FROM_DEVICE);
763 + }
764 +
765 + status = spi_sync(spi, &host->m);
766 +
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,
772 + t->rx_dma, t->len,
773 + DMA_FROM_DEVICE);
774 + }
775 +
776 + } else {
777 + dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
778 +
779 + /* we've read extra garbage, timed out, etc */
780 + if (status < 0)
781 + return status;
782 +
783 + /* low four bits are an R2 subset, fifth seems to be
784 + * vendor specific ... map them all to generic error..
785 + */
786 + return -EIO;
787 + }
788 +
789 + if (host->mmc->use_spi_crc) {
790 + u16 crc = crc_itu_t(0, t->rx_buf, t->len);
791 +
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);
797 + return -EILSEQ;
798 + }
799 + }
800 +
801 + t->rx_buf += t->len;
802 + if (host->dma_dev)
803 + t->rx_dma += t->len;
804 +
805 + return 0;
806 +}
807 +
808 +/*
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.
812 + */
813 +static void
814 +mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
815 + struct mmc_data *data, u32 blk_size)
816 +{
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;
822 + unsigned n_sg;
823 + int multiple = (data->blocks > 1);
824 +
825 + if (data->flags & MMC_DATA_READ)
826 + direction = DMA_FROM_DEVICE;
827 + else
828 + direction = DMA_TO_DEVICE;
829 + mmc_spi_setup_data_message(host, multiple, direction);
830 + t = &host->t;
831 +
832 + /* Handle scatterlist segments one at a time, with synch for
833 + * each 512-byte block
834 + */
835 + for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
836 + int status = 0;
837 + dma_addr_t dma_addr = 0;
838 + void *kmap_addr;
839 + unsigned length = sg->length;
840 + enum dma_data_direction dir = direction;
841 +
842 + /* set up dma mapping for controller drivers that might
843 + * use DMA ... though they may fall back to PIO
844 + */
845 + if (dma_dev) {
846 + /* never invalidate whole *shared* pages ... */
847 + if ((sg->offset != 0 || length != PAGE_SIZE)
848 + && dir == DMA_FROM_DEVICE)
849 + dir = DMA_BIDIRECTIONAL;
850 +
851 + dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
852 + PAGE_SIZE, dir);
853 + if (direction == DMA_TO_DEVICE)
854 + t->tx_dma = dma_addr + sg->offset;
855 + else
856 + t->rx_dma = dma_addr + sg->offset;
857 + }
858 +
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;
863 + else
864 + t->rx_buf = kmap_addr + sg->offset;
865 +
866 + /* transfer each block, and update request status */
867 + while (length) {
868 + t->len = min(length, blk_size);
869 +
870 + dev_dbg(&host->spi->dev,
871 + " mmc_spi: %s block, %d bytes\n",
872 + (direction == DMA_TO_DEVICE)
873 + ? "write"
874 + : "read",
875 + t->len);
876 +
877 + if (direction == DMA_TO_DEVICE)
878 + status = mmc_spi_writeblock(host, t);
879 + else
880 + status = mmc_spi_readblock(host, t);
881 + if (status < 0)
882 + break;
883 +
884 + data->bytes_xfered += t->len;
885 + length -= t->len;
886 +
887 + if (!multiple)
888 + break;
889 + }
890 +
891 + /* discard mappings */
892 + if (direction == DMA_FROM_DEVICE)
893 + flush_kernel_dcache_page(sg_page(sg));
894 + kunmap(sg_page(sg));
895 + if (dma_dev)
896 + dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
897 +
898 + if (status < 0) {
899 + data->error = status;
900 + dev_dbg(&spi->dev, "%s status %d\n",
901 + (direction == DMA_TO_DEVICE)
902 + ? "write" : "read",
903 + status);
904 + break;
905 + }
906 + }
907 +
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.
913 + */
914 + if (direction == DMA_TO_DEVICE && multiple) {
915 + struct scratch *scratch = host->data;
916 + int tmp;
917 + const unsigned statlen = sizeof(scratch->status);
918 +
919 + dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
920 +
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.
925 + */
926 + INIT_LIST_HEAD(&host->m.transfers);
927 + list_add(&host->early_status.transfer_list,
928 + &host->m.transfers);
929 +
930 + memset(scratch->status, 0xff, statlen);
931 + scratch->status[0] = SPI_TOKEN_STOP_TRAN;
932 +
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;
936 +
937 + if (host->dma_dev)
938 + dma_sync_single_for_device(host->dma_dev,
939 + host->data_dma, sizeof(*scratch),
940 + DMA_BIDIRECTIONAL);
941 +
942 + tmp = spi_sync(spi, &host->m);
943 +
944 + if (host->dma_dev)
945 + dma_sync_single_for_cpu(host->dma_dev,
946 + host->data_dma, sizeof(*scratch),
947 + DMA_BIDIRECTIONAL);
948 +
949 + if (tmp < 0) {
950 + if (!data->error)
951 + data->error = tmp;
952 + return;
953 + }
954 +
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.
958 + */
959 + for (tmp = 2; tmp < statlen; tmp++) {
960 + if (scratch->status[tmp] != 0)
961 + return;
962 + }
963 + tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
964 + if (tmp < 0 && !data->error)
965 + data->error = tmp;
966 + }
967 +}
968 +
969 +/****************************************************************************/
970 +
971 +/*
972 + * MMC driver implementation -- the interface to the MMC stack
973 + */
974 +
975 +static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
976 +{
977 + struct mmc_spi_host *host = mmc_priv(mmc);
978 + int status = -EINVAL;
979 +
980 +#ifdef DEBUG
981 + /* MMC core and layered drivers *MUST* issue SPI-aware commands */
982 + {
983 + struct mmc_command *cmd;
984 + int invalid = 0;
985 +
986 + cmd = mrq->cmd;
987 + if (!mmc_spi_resp_type(cmd)) {
988 + dev_dbg(&host->spi->dev, "bogus command\n");
989 + cmd->error = -EINVAL;
990 + invalid = 1;
991 + }
992 +
993 + cmd = mrq->stop;
994 + if (cmd && !mmc_spi_resp_type(cmd)) {
995 + dev_dbg(&host->spi->dev, "bogus STOP command\n");
996 + cmd->error = -EINVAL;
997 + invalid = 1;
998 + }
999 +
1000 + if (invalid) {
1001 + dump_stack();
1002 + mmc_request_done(host->mmc, mrq);
1003 + return;
1004 + }
1005 + }
1006 +#endif
1007 +
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);
1012 + if (mrq->stop)
1013 + status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1014 + else
1015 + mmc_cs_off(host);
1016 + }
1017 +
1018 + mmc_request_done(host->mmc, mrq);
1019 +}
1020 +
1021 +/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1022 + *
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.
1025 + *
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...
1028 + */
1029 +static void mmc_spi_initsequence(struct mmc_spi_host *host)
1030 +{
1031 + /* Try to be very sure any previous command has completed;
1032 + * wait till not-busy, skip debris from any old commands.
1033 + */
1034 + mmc_spi_wait_unbusy(host, r1b_timeout);
1035 + mmc_spi_readbytes(host, 10);
1036 +
1037 + /*
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.
1042 + *
1043 + * Some cards are particularly needy of this (e.g. Viking "SD256")
1044 + * while most others don't seem to care.
1045 + *
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.
1050 + */
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;
1057 + } else {
1058 + mmc_spi_readbytes(host, 18);
1059 +
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");
1065 + }
1066 + }
1067 +}
1068 +
1069 +static char *mmc_powerstring(u8 power_mode)
1070 +{
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";
1075 + }
1076 + return "?";
1077 +}
1078 +
1079 +static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1080 +{
1081 + struct mmc_spi_host *host = mmc_priv(mmc);
1082 +
1083 + if (host->power_mode != ios->power_mode) {
1084 + int canpower;
1085 +
1086 + canpower = host->pdata && host->pdata->setpower;
1087 +
1088 + dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1089 + mmc_powerstring(ios->power_mode),
1090 + ios->vdd,
1091 + canpower ? ", can switch" : "");
1092 +
1093 + /* switch power on/off if possible, accounting for
1094 + * max 250msec powerup time if needed.
1095 + */
1096 + if (canpower) {
1097 + switch (ios->power_mode) {
1098 + case MMC_POWER_OFF:
1099 + case MMC_POWER_UP:
1100 + host->pdata->setpower(&host->spi->dev,
1101 + ios->vdd);
1102 + if (ios->power_mode == MMC_POWER_UP)
1103 + msleep(host->powerup_msecs);
1104 + }
1105 + }
1106 +
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);
1110 +
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.
1115 + *
1116 + * - Clock low means CPOL 0, e.g. mode 0
1117 + * - MOSI low comes from writing zero
1118 + * - Chipselect is usually active low...
1119 + */
1120 + if (canpower && ios->power_mode == MMC_POWER_OFF) {
1121 + int mres;
1122 +
1123 + host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1124 + mres = spi_setup(host->spi);
1125 + if (mres < 0)
1126 + dev_dbg(&host->spi->dev,
1127 + "switch to SPI mode 0 failed\n");
1128 +
1129 + if (spi_w8r8(host->spi, 0x00) < 0)
1130 + dev_dbg(&host->spi->dev,
1131 + "put spi signals to low failed\n");
1132 +
1133 + /*
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!
1138 + *
1139 + * FIXME no, chipselect can be high since the
1140 + * device is inactive and SPI_CS_HIGH is clear...
1141 + */
1142 + msleep(10);
1143 + if (mres == 0) {
1144 + host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1145 + mres = spi_setup(host->spi);
1146 + if (mres < 0)
1147 + dev_dbg(&host->spi->dev,
1148 + "switch back to SPI mode 3"
1149 + " failed\n");
1150 + }
1151 + }
1152 +
1153 + host->power_mode = ios->power_mode;
1154 + }
1155 +
1156 + if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1157 + int status;
1158 +
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);
1164 + }
1165 +}
1166 +
1167 +static int mmc_spi_get_ro(struct mmc_host *mmc)
1168 +{
1169 + struct mmc_spi_host *host = mmc_priv(mmc);
1170 +
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 */
1174 + return 0;
1175 +}
1176 +
1177 +
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,
1182 +};
1183 +
1184 +
1185 +/****************************************************************************/
1186 +
1187 +/*
1188 + * SPI driver implementation
1189 + */
1190 +
1191 +static irqreturn_t
1192 +mmc_spi_detect_irq(int irq, void *mmc)
1193 +{
1194 + struct mmc_spi_host *host = mmc_priv(mmc);
1195 + u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1196 +
1197 + mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1198 + return IRQ_HANDLED;
1199 +}
1200 +
1201 +struct count_children {
1202 + unsigned n;
1203 + struct bus_type *bus;
1204 +};
1205 +
1206 +static int maybe_count_child(struct device *dev, void *c)
1207 +{
1208 + struct count_children *ccp = c;
1209 +
1210 + if (dev->bus == ccp->bus) {
1211 + if (ccp->n)
1212 + return -EBUSY;
1213 + ccp->n++;
1214 + }
1215 + return 0;
1216 +}
1217 +
1218 +static int mmc_spi_probe(struct spi_device *spi)
1219 +{
1220 + void *ones;
1221 + struct mmc_host *mmc;
1222 + struct mmc_spi_host *host;
1223 + int status;
1224 +
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.
1229 + */
1230 + spi->mode = SPI_MODE_0;
1231 + spi->bits_per_word = 8;
1232 +
1233 + status = spi_setup(spi);
1234 + if (status < 0) {
1235 + dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1236 + spi->mode, spi->max_speed_hz / 1000,
1237 + status);
1238 + return status;
1239 + }
1240 +
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.
1246 + *
1247 + * Until we have software primitives which guarantee non-interference,
1248 + * we'll aim for a hardware-level guarantee.
1249 + *
1250 + * REVISIT we can't guarantee another device won't be added later...
1251 + */
1252 + if (spi->master->num_chipselect > 1) {
1253 + struct count_children cc;
1254 +
1255 + cc.n = 0;
1256 + cc.bus = spi->dev.bus;
1257 + status = device_for_each_child(spi->dev.parent, &cc,
1258 + maybe_count_child);
1259 + if (status < 0) {
1260 + dev_err(&spi->dev, "can't share SPI bus\n");
1261 + return status;
1262 + }
1263 +
1264 + dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
1265 + }
1266 +
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.
1269 + *
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.
1272 + */
1273 + status = -ENOMEM;
1274 + ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1275 + if (!ones)
1276 + goto nomem;
1277 + memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1278 +
1279 + mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1280 + if (!mmc)
1281 + goto nomem;
1282 +
1283 + mmc->ops = &mmc_spi_ops;
1284 + mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1285 +
1286 + /* As long as we keep track of the number of successfully
1287 + * transmitted blocks, we're good for multiwrite.
1288 + */
1289 + mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1290 +
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!
1294 + *
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).
1298 + */
1299 + mmc->f_min = 400000;
1300 + mmc->f_max = spi->max_speed_hz;
1301 +
1302 + host = mmc_priv(mmc);
1303 + host->mmc = mmc;
1304 + host->spi = spi;
1305 +
1306 + host->ones = ones;
1307 +
1308 + /* Platform data is used to hook up things like card sensing
1309 + * and power switching gpios.
1310 + */
1311 + host->pdata = spi->dev.platform_data;
1312 + if (host->pdata)
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;
1317 + }
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;
1322 + }
1323 +
1324 + dev_set_drvdata(&spi->dev, mmc);
1325 +
1326 + /* preallocate dma buffers */
1327 + host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1328 + if (!host->data)
1329 + goto fail_nobuf1;
1330 +
1331 +//FIXME
1332 +#if 0
1333 + if (spi->master->dev.parent->dma_mask) {
1334 + struct device *dev = spi->master->dev.parent;
1335 +
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);
1341 +
1342 + /* REVISIT in theory those map operations can fail... */
1343 +
1344 + dma_sync_single_for_cpu(host->dma_dev,
1345 + host->data_dma, sizeof(*host->data),
1346 + DMA_BIDIRECTIONAL);
1347 + }
1348 +#endif
1349 +
1350 + /* setup message for status/busy readback */
1351 + spi_message_init(&host->readback);
1352 + host->readback.is_dma_mapped = (host->dma_dev != NULL);
1353 +
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;
1360 +
1361 + /* register card detect irq */
1362 + if (host->pdata && host->pdata->init) {
1363 + status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1364 + if (status != 0)
1365 + goto fail_glue_init;
1366 + }
1367 +
1368 + status = mmc_add_host(mmc);
1369 + if (status != 0)
1370 + goto fail_add_host;
1371 +
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)
1376 + ? "" : ", no WP",
1377 + (host->pdata && host->pdata->setpower)
1378 + ? "" : ", no poweroff");
1379 + return 0;
1380 +
1381 +fail_add_host:
1382 + mmc_remove_host (mmc);
1383 +fail_glue_init:
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);
1388 +
1389 +fail_nobuf1:
1390 + mmc_free_host(mmc);
1391 + dev_set_drvdata(&spi->dev, NULL);
1392 +
1393 +nomem:
1394 + kfree(ones);
1395 + return status;
1396 +}
1397 +
1398 +
1399 +static int __devexit mmc_spi_remove(struct spi_device *spi)
1400 +{
1401 + struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1402 + struct mmc_spi_host *host;
1403 +
1404 + if (mmc) {
1405 + host = mmc_priv(mmc);
1406 +
1407 + /* prevent new mmc_detect_change() calls */
1408 + if (host->pdata && host->pdata->exit)
1409 + host->pdata->exit(&spi->dev, mmc);
1410 +
1411 + mmc_remove_host(mmc);
1412 +
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);
1418 + }
1419 +
1420 + kfree(host->data);
1421 + kfree(host->ones);
1422 +
1423 + spi->max_speed_hz = mmc->f_max;
1424 + mmc_free_host(mmc);
1425 + dev_set_drvdata(&spi->dev, NULL);
1426 + }
1427 + return 0;
1428 +}
1429 +
1430 +
1431 +static struct spi_driver mmc_spi_driver = {
1432 + .driver = {
1433 + .name = "mmc_spi",
1434 + .bus = &spi_bus_type,
1435 + .owner = THIS_MODULE,
1436 + },
1437 + .probe = mmc_spi_probe,
1438 + .remove = __devexit_p(mmc_spi_remove),
1439 +};
1440 +
1441 +
1442 +static int __init mmc_spi_init(void)
1443 +{
1444 + return spi_register_driver(&mmc_spi_driver);
1445 +}
1446 +module_init(mmc_spi_init);
1447 +
1448 +
1449 +static void __exit mmc_spi_exit(void)
1450 +{
1451 + spi_unregister_driver(&mmc_spi_driver);
1452 +}
1453 +module_exit(mmc_spi_exit);
1454 +
1455 +
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
1464 @@ -0,0 +1,33 @@
1465 +#ifndef __LINUX_SPI_MMC_SPI_H
1466 +#define __LINUX_SPI_MMC_SPI_H
1467 +
1468 +struct device;
1469 +struct mmc_host;
1470 +
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.)
1473 + *
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 *".
1477 + */
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 *),
1482 + void *);
1483 + void (*exit)(struct device *, void *);
1484 +
1485 + /* sense switch on sd cards */
1486 + int (*get_ro)(struct device *);
1487 +
1488 + /* how long to debounce card detect, in msecs */
1489 + u16 detect_delay;
1490 +
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);
1495 +};
1496 +
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
1502 @@ -19,6 +19,7 @@
1503
1504 #include "sysfs.h"
1505 #include "core.h"
1506 +#include "sdio_cis.h"
1507 #include "bus.h"
1508
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");
1512 case MMC_TYPE_SD:
1513 return sprintf(buf, "SD\n");
1514 + case MMC_TYPE_SDIO:
1515 + return sprintf(buf, "SDIO\n");
1516 default:
1517 return -EFAULT;
1518 }
1519 @@ -55,36 +58,37 @@ static int mmc_bus_match(struct device *
1520 }
1521
1522 static int
1523 -mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
1524 - int buf_size)
1525 +mmc_bus_uevent(struct device *dev, char **envp,
1526 + int num_envp, char *buffer, int buffer_size)
1527 {
1528 struct mmc_card *card = dev_to_mmc_card(dev);
1529 - int retval = 0, i = 0, length = 0;
1530 -
1531 -#define add_env(fmt,val) do { \
1532 - retval = add_uevent_var(envp, num_envp, &i, \
1533 - buf, buf_size, &length, \
1534 - fmt, val); \
1535 - if (retval) \
1536 - return retval; \
1537 -} while (0);
1538 + const char *type;
1539 + int retval = 0;
1540 + int i = 0, len = 0;
1541
1542 switch (card->type) {
1543 case MMC_TYPE_MMC:
1544 - add_env("MMC_TYPE=%s", "MMC");
1545 + type = "MMC";
1546 break;
1547 case MMC_TYPE_SD:
1548 - add_env("MMC_TYPE=%s", "SD");
1549 + type = "SD";
1550 break;
1551 + case MMC_TYPE_SDIO:
1552 + type = "SDIO";
1553 + break;
1554 + default:
1555 + type = NULL;
1556 }
1557
1558 - add_env("MMC_NAME=%s", mmc_card_name(card));
1559 -
1560 -#undef add_env
1561 + if (type) {
1562 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_TYPE=%s", type);
1563 + if (retval)
1564 + return retval;
1565 + }
1566
1567 - envp[i] = NULL;
1568 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_NAME=%s", mmc_card_name(card));
1569
1570 - return 0;
1571 + return retval;
1572 }
1573
1574 static int mmc_bus_probe(struct device *dev)
1575 @@ -176,6 +180,11 @@ static void mmc_release_card(struct devi
1576 {
1577 struct mmc_card *card = dev_to_mmc_card(dev);
1578
1579 + sdio_free_common_cis(card);
1580 +
1581 + if (card->info)
1582 + kfree(card->info);
1583 +
1584 kfree(card);
1585 }
1586
1587 @@ -221,15 +230,25 @@ int mmc_add_card(struct mmc_card *card)
1588 if (mmc_card_blockaddr(card))
1589 type = "SDHC";
1590 break;
1591 + case MMC_TYPE_SDIO:
1592 + type = "SDIO";
1593 + break;
1594 default:
1595 type = "?";
1596 break;
1597 }
1598
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 " : "",
1602 - type, card->rca);
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 " : "",
1607 + type);
1608 + } else {
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 " : "",
1612 + type, card->rca);
1613 + }
1614
1615 card->dev.uevent_suppress = 1;
1616
1617 @@ -261,8 +280,13 @@ int mmc_add_card(struct mmc_card *card)
1618 void mmc_remove_card(struct mmc_card *card)
1619 {
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));
1626 + } else {
1627 + printk(KERN_INFO "%s: card %04x removed\n",
1628 + mmc_hostname(card->host), card->rca);
1629 + }
1630
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
1637 @@ -18,7 +18,7 @@
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>
1644
1645 #include <linux/mmc/card.h>
1646 @@ -29,16 +29,27 @@
1647 #include "core.h"
1648 #include "bus.h"
1649 #include "host.h"
1650 +#include "sdio_bus.h"
1651
1652 #include "mmc_ops.h"
1653 #include "sd_ops.h"
1654 +#include "sdio_ops.h"
1655
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);
1659
1660 static struct workqueue_struct *workqueue;
1661
1662 /*
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.
1666 + */
1667 +int use_spi_crc = 1;
1668 +module_param(use_spi_crc, bool, 0);
1669 +
1670 +/*
1671 * Internal function. Schedule delayed work in the MMC work queue.
1672 */
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;
1677
1678 + if (err && cmd->retries && mmc_host_is_spi(host)) {
1679 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
1680 + cmd->retries = 0;
1681 + }
1682 +
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
1687 cmd->error = 0;
1688 host->ops->request(host, mrq);
1689 } else {
1690 + led_trigger_event(host->led, LED_OFF);
1691 +
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);
1702 }
1703
1704 @@ -130,6 +148,8 @@ mmc_start_request(struct mmc_host *host,
1705
1706 WARN_ON(!host->claimed);
1707
1708 + led_trigger_event(host->led, LED_FULL);
1709 +
1710 mrq->cmd->error = 0;
1711 mrq->cmd->mrq = mrq;
1712 if (mrq->data) {
1713 @@ -199,7 +219,7 @@ int mmc_wait_for_cmd(struct mmc_host *ho
1714 {
1715 struct mmc_request mrq;
1716
1717 - BUG_ON(!host->claimed);
1718 + WARN_ON(!host->claimed);
1719
1720 memset(&mrq, 0, sizeof(struct mmc_request));
1721
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
1727 *
1728 * Computes the data timeout parameters according to the
1729 * correct algorithm given the card type.
1730 */
1731 -void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
1732 - int write)
1733 +void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
1734 {
1735 unsigned int mult;
1736
1737 /*
1738 + * SDIO cards only define an upper 1 s limit on access.
1739 + */
1740 + if (mmc_card_sdio(card)) {
1741 + data->timeout_ns = 1000000000;
1742 + data->timeout_clks = 0;
1743 + return;
1744 + }
1745 +
1746 + /*
1747 * SD cards use a 100 multiplier rather than 10
1748 */
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.
1753 */
1754 - if (write)
1755 + if (data->flags & MMC_DATA_WRITE)
1756 mult <<= card->csd.r2w_factor;
1757
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);
1762
1763 - if (write)
1764 + if (data->flags & MMC_DATA_WRITE)
1765 limit_us = 250000;
1766 else
1767 limit_us = 100000;
1768 @@ -272,15 +299,20 @@ void mmc_set_data_timeout(struct mmc_dat
1769 EXPORT_SYMBOL(mmc_set_data_timeout);
1770
1771 /**
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
1776 *
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.
1782 */
1783 -void mmc_claim_host(struct mmc_host *host)
1784 +int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1785 {
1786 DECLARE_WAITQUEUE(wait, current);
1787 unsigned long flags;
1788 + int stop;
1789
1790 might_sleep();
1791
1792 @@ -288,19 +320,24 @@ void mmc_claim_host(struct mmc_host *hos
1793 spin_lock_irqsave(&host->lock, flags);
1794 while (1) {
1795 set_current_state(TASK_UNINTERRUPTIBLE);
1796 - if (!host->claimed)
1797 + stop = abort ? atomic_read(abort) : 0;
1798 + if (stop || !host->claimed)
1799 break;
1800 spin_unlock_irqrestore(&host->lock, flags);
1801 schedule();
1802 spin_lock_irqsave(&host->lock, flags);
1803 }
1804 set_current_state(TASK_RUNNING);
1805 - host->claimed = 1;
1806 + if (!stop)
1807 + host->claimed = 1;
1808 + else
1809 + wake_up(&host->wq);
1810 spin_unlock_irqrestore(&host->lock, flags);
1811 remove_wait_queue(&host->wq, &wait);
1812 + return stop;
1813 }
1814
1815 -EXPORT_SYMBOL(mmc_claim_host);
1816 +EXPORT_SYMBOL(__mmc_claim_host);
1817
1818 /**
1819 * mmc_release_host - release a host
1820 @@ -313,7 +350,7 @@ void mmc_release_host(struct mmc_host *h
1821 {
1822 unsigned long flags;
1823
1824 - BUG_ON(!host->claimed);
1825 + WARN_ON(!host->claimed);
1826
1827 spin_lock_irqsave(&host->lock, flags);
1828 host->claimed = 0;
1829 @@ -433,19 +470,32 @@ static void mmc_power_up(struct mmc_host
1830 int bit = fls(host->ocr_avail) - 1;
1831
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;
1838 + } else {
1839 + host->ios.chip_select = MMC_CS_DONTCARE;
1840 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1841 + }
1842 host->ios.power_mode = MMC_POWER_UP;
1843 host->ios.bus_width = MMC_BUS_WIDTH_1;
1844 host->ios.timing = MMC_TIMING_LEGACY;
1845 mmc_set_ios(host);
1846
1847 - mmc_delay(1);
1848 + /*
1849 + * This delay should be sufficient to allow the power supply
1850 + * to reach the minimum voltage.
1851 + */
1852 + mmc_delay(2);
1853
1854 host->ios.clock = host->f_min;
1855 host->ios.power_mode = MMC_POWER_ON;
1856 mmc_set_ios(host);
1857
1858 + /*
1859 + * This delay must be at least 74 clock sizes, or 1 ms, or the
1860 + * time required to reach a stable voltage.
1861 + */
1862 mmc_delay(2);
1863 }
1864
1865 @@ -453,8 +503,10 @@ static void mmc_power_off(struct mmc_hos
1866 {
1867 host->ios.clock = 0;
1868 host->ios.vdd = 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;
1874 + }
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
1879 BUG_ON(!host);
1880 BUG_ON(!ops);
1881
1882 - BUG_ON(!host->claimed);
1883 + WARN_ON(!host->claimed);
1884
1885 spin_lock_irqsave(&host->lock, flags);
1886
1887 @@ -535,8 +587,8 @@ void mmc_detach_bus(struct mmc_host *hos
1888
1889 BUG_ON(!host);
1890
1891 - BUG_ON(!host->claimed);
1892 - BUG_ON(!host->bus_ops);
1893 + WARN_ON(!host->claimed);
1894 + WARN_ON(!host->bus_ops);
1895
1896 spin_lock_irqsave(&host->lock, flags);
1897
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);
1905 #endif
1906
1907 @@ -597,24 +649,38 @@ void mmc_rescan(struct work_struct *work
1908
1909 mmc_send_if_cond(host, host->ocr_avail);
1910
1911 + /*
1912 + * First we search for SDIO...
1913 + */
1914 + err = mmc_send_io_op_cond(host, 0, &ocr);
1915 + if (!err) {
1916 + if (mmc_attach_sdio(host, ocr))
1917 + mmc_power_off(host);
1918 + return;
1919 + }
1920 +
1921 + /*
1922 + * ...then normal SD...
1923 + */
1924 err = mmc_send_app_op_cond(host, 0, &ocr);
1925 - if (err == MMC_ERR_NONE) {
1926 + if (!err) {
1927 if (mmc_attach_sd(host, ocr))
1928 mmc_power_off(host);
1929 - } else {
1930 - /*
1931 - * If we fail to detect any SD cards then try
1932 - * searching for MMC cards.
1933 - */
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);
1938 - } else {
1939 + return;
1940 + }
1941 +
1942 + /*
1943 + * ...and finally MMC.
1944 + */
1945 + err = mmc_send_op_cond(host, 0, &ocr);
1946 + if (!err) {
1947 + if (mmc_attach_mmc(host, ocr))
1948 mmc_power_off(host);
1949 - mmc_release_host(host);
1950 - }
1951 + return;
1952 }
1953 +
1954 + mmc_release_host(host);
1955 + mmc_power_off(host);
1956 } else {
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)
1960 return -ENOMEM;
1961
1962 ret = mmc_register_bus();
1963 - if (ret == 0) {
1964 - ret = mmc_register_host_class();
1965 - if (ret)
1966 - mmc_unregister_bus();
1967 - }
1968 + if (ret)
1969 + goto destroy_workqueue;
1970 +
1971 + ret = mmc_register_host_class();
1972 + if (ret)
1973 + goto unregister_bus;
1974 +
1975 + ret = sdio_register_bus();
1976 + if (ret)
1977 + goto unregister_host_class;
1978 +
1979 + return 0;
1980 +
1981 +unregister_host_class:
1982 + mmc_unregister_host_class();
1983 +unregister_bus:
1984 + mmc_unregister_bus();
1985 +destroy_workqueue:
1986 + destroy_workqueue(workqueue);
1987 +
1988 return ret;
1989 }
1990
1991 static void __exit mmc_exit(void)
1992 {
1993 + sdio_unregister_bus();
1994 mmc_unregister_host_class();
1995 mmc_unregister_bus();
1996 destroy_workqueue(workqueue);
1997 }
1998
1999 -module_init(mmc_init);
2000 +subsys_initcall(mmc_init);
2001 module_exit(mmc_exit);
2002
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);
2011
2012 +extern int use_spi_crc;
2013 +
2014 #endif
2015
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
2020 @@ -15,6 +15,7 @@
2021 #include <linux/err.h>
2022 #include <linux/idr.h>
2023 #include <linux/pagemap.h>
2024 +#include <linux/leds.h>
2025
2026 #include <linux/mmc/host.h>
2027
2028 @@ -100,6 +101,9 @@ int mmc_add_host(struct mmc_host *host)
2029 {
2030 int err;
2031
2032 + WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
2033 + !host->ops->enable_sdio_irq);
2034 +
2035 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
2036 return -ENOMEM;
2037
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);
2041
2042 + led_trigger_register_simple(host->class_dev.bus_id, &host->led);
2043 +
2044 err = device_add(&host->class_dev);
2045 if (err)
2046 return err;
2047 @@ -137,6 +143,8 @@ void mmc_remove_host(struct mmc_host *ho
2048
2049 device_del(&host->class_dev);
2050
2051 + led_trigger_unregister_simple(host->led);
2052 +
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
2061 {
2062 int err;
2063 u8 *ext_csd;
2064 + unsigned int ext_csd_struct;
2065
2066 BUG_ON(!card);
2067
2068 - err = MMC_ERR_FAILED;
2069 -
2070 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
2071 - return MMC_ERR_NONE;
2072 + return 0;
2073
2074 /*
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);
2078 if (!ext_csd) {
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));
2084 + return -ENOMEM;
2085 }
2086
2087 err = mmc_send_ext_csd(card, ext_csd);
2088 - if (err != MMC_ERR_NONE) {
2089 + if (err) {
2090 + /*
2091 + * We all hosts that cannot perform the command
2092 + * to fail more gracefully
2093 + */
2094 + if (err != -EINVAL)
2095 + goto out;
2096 +
2097 /*
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 "
2102 "suffer.\n",
2103 mmc_hostname(card->host));
2104 - err = MMC_ERR_NONE;
2105 + err = 0;
2106 }
2107 +
2108 goto out;
2109 }
2110
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),
2122 + ext_csd_struct);
2123 + err = -EINVAL;
2124 + goto out;
2125 + }
2126 +
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);
2135 + }
2136
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;
2141
2142 BUG_ON(!host);
2143 - BUG_ON(!host->claimed);
2144 + WARN_ON(!host->claimed);
2145
2146 /*
2147 * Since we're changing the OCR value, we seem to
2148 @@ -258,19 +275,33 @@ static int mmc_init_card(struct mmc_host
2149
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)
2153 + if (err)
2154 goto err;
2155
2156 /*
2157 + * For SPI, enable CRC as appropriate.
2158 + */
2159 + if (mmc_host_is_spi(host)) {
2160 + err = mmc_spi_set_crc(host, use_spi_crc);
2161 + if (err)
2162 + goto err;
2163 + }
2164 +
2165 + /*
2166 * Fetch CID from card.
2167 */
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);
2172 + else
2173 + err = mmc_all_send_cid(host, cid);
2174 + if (err)
2175 goto err;
2176
2177 if (oldcard) {
2178 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2179 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2180 + err = -ENOENT;
2181 goto err;
2182 + }
2183
2184 card = oldcard;
2185 } else {
2186 @@ -278,8 +309,10 @@ static int mmc_init_card(struct mmc_host
2187 * Allocate card structure.
2188 */
2189 card = mmc_alloc_card(host);
2190 - if (IS_ERR(card))
2191 + if (IS_ERR(card)) {
2192 + err = PTR_ERR(card);
2193 goto err;
2194 + }
2195
2196 card->type = MMC_TYPE_MMC;
2197 card->rca = 1;
2198 @@ -287,43 +320,47 @@ static int mmc_init_card(struct mmc_host
2199 }
2200
2201 /*
2202 - * Set card RCA.
2203 + * For native busses: set card RCA and quit open drain mode.
2204 */
2205 - err = mmc_set_relative_addr(card);
2206 - if (err != MMC_ERR_NONE)
2207 - goto free_card;
2208 + if (!mmc_host_is_spi(host)) {
2209 + err = mmc_set_relative_addr(card);
2210 + if (err)
2211 + goto free_card;
2212
2213 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2214 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2215 + }
2216
2217 if (!oldcard) {
2218 /*
2219 * Fetch CSD from card.
2220 */
2221 err = mmc_send_csd(card, card->raw_csd);
2222 - if (err != MMC_ERR_NONE)
2223 + if (err)
2224 goto free_card;
2225
2226 err = mmc_decode_csd(card);
2227 - if (err < 0)
2228 + if (err)
2229 goto free_card;
2230 err = mmc_decode_cid(card);
2231 - if (err < 0)
2232 + if (err)
2233 goto free_card;
2234 }
2235
2236 /*
2237 * Select card, as all following commands rely on that.
2238 */
2239 - err = mmc_select_card(card);
2240 - if (err != MMC_ERR_NONE)
2241 - goto free_card;
2242 + if (!mmc_host_is_spi(host)) {
2243 + err = mmc_select_card(card);
2244 + if (err)
2245 + goto free_card;
2246 + }
2247
2248 if (!oldcard) {
2249 /*
2250 - * Fetch and process extened CSD.
2251 + * Fetch and process extended CSD.
2252 */
2253 err = mmc_read_ext_csd(card);
2254 - if (err != MMC_ERR_NONE)
2255 + if (err)
2256 goto free_card;
2257 }
2258
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)
2264 + if (err)
2265 goto free_card;
2266
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)
2273 + if (err)
2274 goto free_card;
2275
2276 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
2277 @@ -372,14 +409,14 @@ static int mmc_init_card(struct mmc_host
2278 if (!oldcard)
2279 host->card = card;
2280
2281 - return MMC_ERR_NONE;
2282 + return 0;
2283
2284 free_card:
2285 if (!oldcard)
2286 mmc_remove_card(card);
2287 err:
2288
2289 - return MMC_ERR_FAILED;
2290 + return err;
2291 }
2292
2293 /*
2294 @@ -413,7 +450,7 @@ static void mmc_detect(struct mmc_host *
2295
2296 mmc_release_host(host);
2297
2298 - if (err != MMC_ERR_NONE) {
2299 + if (err) {
2300 mmc_remove(host);
2301
2302 mmc_claim_host(host);
2303 @@ -480,7 +517,8 @@ static void mmc_suspend(struct mmc_host
2304 BUG_ON(!host->card);
2305
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);
2312 }
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);
2316
2317 - if (err != MMC_ERR_NONE) {
2318 + if (err) {
2319 mmc_remove(host);
2320
2321 mmc_claim_host(host);
2322 @@ -536,11 +574,20 @@ int mmc_attach_mmc(struct mmc_host *host
2323 int err;
2324
2325 BUG_ON(!host);
2326 - BUG_ON(!host->claimed);
2327 + WARN_ON(!host->claimed);
2328
2329 mmc_attach_bus(host, &mmc_ops);
2330
2331 /*
2332 + * We need to get OCR a different way for SPI.
2333 + */
2334 + if (mmc_host_is_spi(host)) {
2335 + err = mmc_spi_read_ocr(host, 1, &ocr);
2336 + if (err)
2337 + goto err;
2338 + }
2339 +
2340 + /*
2341 * Sanity check the voltages that the card claims to
2342 * support.
2343 */
2344 @@ -565,7 +612,7 @@ int mmc_attach_mmc(struct mmc_host *host
2345 * Detect and init the card.
2346 */
2347 err = mmc_init_card(host, host->ocr, NULL);
2348 - if (err != MMC_ERR_NONE)
2349 + if (err)
2350 goto err;
2351
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);
2356
2357 - return 0;
2358 + return err;
2359 }
2360
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
2365 @@ -10,7 +10,6 @@
2366 */
2367
2368 #include <linux/types.h>
2369 -#include <asm/scatterlist.h>
2370 #include <linux/scatterlist.h>
2371
2372 #include <linux/mmc/host.h>
2373 @@ -40,10 +39,10 @@ static int _mmc_select_card(struct mmc_h
2374 }
2375
2376 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2377 - if (err != MMC_ERR_NONE)
2378 + if (err)
2379 return err;
2380
2381 - return MMC_ERR_NONE;
2382 + return 0;
2383 }
2384
2385 int mmc_select_card(struct mmc_card *card)
2386 @@ -63,23 +62,36 @@ int mmc_go_idle(struct mmc_host *host)
2387 int err;
2388 struct mmc_command cmd;
2389
2390 - mmc_set_chip_select(host, MMC_CS_HIGH);
2391 -
2392 - mmc_delay(1);
2393 + /*
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.
2397 + *
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.
2401 + */
2402 + if (!mmc_host_is_spi(host)) {
2403 + mmc_set_chip_select(host, MMC_CS_HIGH);
2404 + mmc_delay(1);
2405 + }
2406
2407 memset(&cmd, 0, sizeof(struct mmc_command));
2408
2409 cmd.opcode = MMC_GO_IDLE_STATE;
2410 cmd.arg = 0;
2411 - cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
2412 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
2413
2414 err = mmc_wait_for_cmd(host, &cmd, 0);
2415
2416 mmc_delay(1);
2417
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);
2421 + mmc_delay(1);
2422 + }
2423
2424 - mmc_delay(1);
2425 + host->use_spi_crc = 0;
2426
2427 return err;
2428 }
2429 @@ -94,23 +106,33 @@ int mmc_send_op_cond(struct mmc_host *ho
2430 memset(&cmd, 0, sizeof(struct mmc_command));
2431
2432 cmd.opcode = MMC_SEND_OP_COND;
2433 - cmd.arg = ocr;
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;
2437
2438 for (i = 100; i; i--) {
2439 err = mmc_wait_for_cmd(host, &cmd, 0);
2440 - if (err != MMC_ERR_NONE)
2441 + if (err)
2442 break;
2443
2444 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
2445 + /* if we're just probing, do a single pass */
2446 + if (ocr == 0)
2447 break;
2448
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))
2453 + break;
2454 + } else {
2455 + if (cmd.resp[0] & MMC_CARD_BUSY)
2456 + break;
2457 + }
2458 +
2459 + err = -ETIMEDOUT;
2460
2461 mmc_delay(10);
2462 }
2463
2464 - if (rocr)
2465 + if (rocr && !mmc_host_is_spi(host))
2466 *rocr = cmd.resp[0];
2467
2468 return err;
2469 @@ -131,12 +153,12 @@ int mmc_all_send_cid(struct mmc_host *ho
2470 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
2471
2472 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2473 - if (err != MMC_ERR_NONE)
2474 + if (err)
2475 return err;
2476
2477 memcpy(cid, cmd.resp, sizeof(u32) * 4);
2478
2479 - return MMC_ERR_NONE;
2480 + return 0;
2481 }
2482
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;
2486
2487 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2488 - if (err != MMC_ERR_NONE)
2489 + if (err)
2490 return err;
2491
2492 - return MMC_ERR_NONE;
2493 + return 0;
2494 }
2495
2496 -int mmc_send_csd(struct mmc_card *card, u32 *csd)
2497 +static int
2498 +mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
2499 {
2500 int err;
2501 struct mmc_command cmd;
2502
2503 - BUG_ON(!card);
2504 - BUG_ON(!card->host);
2505 - BUG_ON(!csd);
2506 + BUG_ON(!host);
2507 + BUG_ON(!cxd);
2508
2509 memset(&cmd, 0, sizeof(struct mmc_command));
2510
2511 - cmd.opcode = MMC_SEND_CSD;
2512 - cmd.arg = card->rca << 16;
2513 + cmd.opcode = opcode;
2514 + cmd.arg = arg;
2515 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
2516
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);
2520 + if (err)
2521 return err;
2522
2523 - memcpy(csd, cmd.resp, sizeof(u32) * 4);
2524 + memcpy(cxd, cmd.resp, sizeof(u32) * 4);
2525
2526 - return MMC_ERR_NONE;
2527 + return 0;
2528 }
2529
2530 -int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2531 +static int
2532 +mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
2533 + u32 opcode, void *buf, unsigned len)
2534 {
2535 struct mmc_request mrq;
2536 struct mmc_command cmd;
2537 struct mmc_data data;
2538 struct scatterlist sg;
2539 + void *data_buf;
2540
2541 - BUG_ON(!card);
2542 - BUG_ON(!card->host);
2543 - BUG_ON(!ext_csd);
2544 + /* dma onto stack is unsafe/nonportable, but callers to this
2545 + * routine normally provide temporary on-stack buffers ...
2546 + */
2547 + data_buf = kmalloc(len, GFP_KERNEL);
2548 + if (data_buf == NULL)
2549 + return -ENOMEM;
2550
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
2554 mrq.cmd = &cmd;
2555 mrq.data = &data;
2556
2557 - cmd.opcode = MMC_SEND_EXT_CSD;
2558 + cmd.opcode = opcode;
2559 cmd.arg = 0;
2560 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2561
2562 - data.blksz = 512;
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.
2567 + */
2568 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2569 +
2570 + data.blksz = len;
2571 data.blocks = 1;
2572 data.flags = MMC_DATA_READ;
2573 data.sg = &sg;
2574 data.sg_len = 1;
2575
2576 - sg_init_one(&sg, ext_csd, 512);
2577 + sg_init_one(&sg, data_buf, len);
2578 +
2579 + if (card)
2580 + mmc_set_data_timeout(&data, card);
2581
2582 - mmc_set_data_timeout(&data, card, 0);
2583 + mmc_wait_for_req(host, &mrq);
2584
2585 - mmc_wait_for_req(card->host, &mrq);
2586 + memcpy(buf, data_buf, len);
2587 + kfree(data_buf);
2588
2589 - if (cmd.error != MMC_ERR_NONE)
2590 + if (cmd.error)
2591 return cmd.error;
2592 - if (data.error != MMC_ERR_NONE)
2593 + if (data.error)
2594 return data.error;
2595
2596 - return MMC_ERR_NONE;
2597 + return 0;
2598 +}
2599 +
2600 +int mmc_send_csd(struct mmc_card *card, u32 *csd)
2601 +{
2602 + int ret, i;
2603 +
2604 + if (!mmc_host_is_spi(card->host))
2605 + return mmc_send_cxd_native(card->host, card->rca << 16,
2606 + csd, MMC_SEND_CSD);
2607 +
2608 + ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
2609 + if (ret)
2610 + return ret;
2611 +
2612 + for (i = 0;i < 4;i++)
2613 + csd[i] = be32_to_cpu(csd[i]);
2614 +
2615 + return 0;
2616 +}
2617 +
2618 +int mmc_send_cid(struct mmc_host *host, u32 *cid)
2619 +{
2620 + int ret, i;
2621 +
2622 + if (!mmc_host_is_spi(host)) {
2623 + if (!host->card)
2624 + return -EINVAL;
2625 + return mmc_send_cxd_native(host, host->card->rca << 16,
2626 + cid, MMC_SEND_CID);
2627 + }
2628 +
2629 + ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
2630 + if (ret)
2631 + return ret;
2632 +
2633 + for (i = 0;i < 4;i++)
2634 + cid[i] = be32_to_cpu(cid[i]);
2635 +
2636 + return 0;
2637 +}
2638 +
2639 +int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2640 +{
2641 + return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
2642 + ext_csd, 512);
2643 +}
2644 +
2645 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
2646 +{
2647 + struct mmc_command cmd;
2648 + int err;
2649 +
2650 + memset(&cmd, 0, sizeof(struct mmc_command));
2651 +
2652 + cmd.opcode = MMC_SPI_READ_OCR;
2653 + cmd.arg = highcap ? (1 << 30) : 0;
2654 + cmd.flags = MMC_RSP_SPI_R3;
2655 +
2656 + err = mmc_wait_for_cmd(host, &cmd, 0);
2657 +
2658 + *ocrp = cmd.resp[1];
2659 + return err;
2660 +}
2661 +
2662 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
2663 +{
2664 + struct mmc_command cmd;
2665 + int err;
2666 +
2667 + memset(&cmd, 0, sizeof(struct mmc_command));
2668 +
2669 + cmd.opcode = MMC_SPI_CRC_ON_OFF;
2670 + cmd.flags = MMC_RSP_SPI_R1;
2671 + cmd.arg = use_crc;
2672 +
2673 + err = mmc_wait_for_cmd(host, &cmd, 0);
2674 + if (!err)
2675 + host->use_spi_crc = use_crc;
2676 + return err;
2677 }
2678
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
2681 (index << 16) |
2682 (value << 8) |
2683 set;
2684 - cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
2685 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2686
2687 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2688 - if (err != MMC_ERR_NONE)
2689 + if (err)
2690 return err;
2691
2692 - return MMC_ERR_NONE;
2693 + return 0;
2694 }
2695
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));
2699
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;
2706
2707 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2708 - if (err != MMC_ERR_NONE)
2709 + if (err)
2710 return err;
2711
2712 + /* NOTE: callers are required to understand the difference
2713 + * between "native" and SPI format status words!
2714 + */
2715 if (status)
2716 *status = cmd.resp[0];
2717
2718 - return MMC_ERR_NONE;
2719 + return 0;
2720 }
2721
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);
2733
2734 #endif
2735
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;
2742 u32 resp[4];
2743
2744 - BUG_ON(!mmc_card_sd(card));
2745 -
2746 resp[3] = card->raw_scr[1];
2747 resp[2] = card->raw_scr[0];
2748
2749 @@ -193,30 +191,38 @@ static int mmc_read_switch(struct mmc_ca
2750 u8 *status;
2751
2752 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2753 - return MMC_ERR_NONE;
2754 + return 0;
2755
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;
2761 + return 0;
2762 }
2763
2764 - err = MMC_ERR_FAILED;
2765 + err = -EIO;
2766
2767 status = kmalloc(64, GFP_KERNEL);
2768 if (!status) {
2769 printk(KERN_ERR "%s: could not allocate a buffer for "
2770 "switch capabilities.\n", mmc_hostname(card->host));
2771 - return err;
2772 + return -ENOMEM;
2773 }
2774
2775 err = mmc_sd_switch(card, 0, 0, 1, status);
2776 - if (err != MMC_ERR_NONE) {
2777 + if (err) {
2778 + /*
2779 + * We all hosts that cannot perform the command
2780 + * to fail more gracefully
2781 + */
2782 + if (err != -EINVAL)
2783 + goto out;
2784 +
2785 printk(KERN_WARNING "%s: problem reading switch "
2786 "capabilities, performance might suffer.\n",
2787 mmc_hostname(card->host));
2788 - err = MMC_ERR_NONE;
2789 + err = 0;
2790 +
2791 goto out;
2792 }
2793
2794 @@ -238,28 +244,28 @@ static int mmc_switch_hs(struct mmc_card
2795 u8 *status;
2796
2797 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2798 - return MMC_ERR_NONE;
2799 + return 0;
2800
2801 if (!(card->csd.cmdclass & CCC_SWITCH))
2802 - return MMC_ERR_NONE;
2803 + return 0;
2804
2805 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
2806 - return MMC_ERR_NONE;
2807 + return 0;
2808
2809 if (card->sw_caps.hs_max_dtr == 0)
2810 - return MMC_ERR_NONE;
2811 + return 0;
2812
2813 - err = MMC_ERR_FAILED;
2814 + err = -EIO;
2815
2816 status = kmalloc(64, GFP_KERNEL);
2817 if (!status) {
2818 printk(KERN_ERR "%s: could not allocate a buffer for "
2819 "switch capabilities.\n", mmc_hostname(card->host));
2820 - return err;
2821 + return -ENOMEM;
2822 }
2823
2824 err = mmc_sd_switch(card, 1, 0, 1, status);
2825 - if (err != MMC_ERR_NONE)
2826 + if (err)
2827 goto out;
2828
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;
2832
2833 BUG_ON(!host);
2834 - BUG_ON(!host->claimed);
2835 + WARN_ON(!host->claimed);
2836
2837 /*
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.
2841 */
2842 err = mmc_send_if_cond(host, ocr);
2843 - if (err == MMC_ERR_NONE)
2844 + if (!err)
2845 ocr |= 1 << 30;
2846
2847 err = mmc_send_app_op_cond(host, ocr, NULL);
2848 - if (err != MMC_ERR_NONE)
2849 + if (err)
2850 goto err;
2851
2852 /*
2853 + * For SPI, enable CRC as appropriate.
2854 + */
2855 + if (mmc_host_is_spi(host)) {
2856 + err = mmc_spi_set_crc(host, use_spi_crc);
2857 + if (err)
2858 + goto err;
2859 + }
2860 +
2861 + /*
2862 * Fetch CID from card.
2863 */
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);
2868 + else
2869 + err = mmc_all_send_cid(host, cid);
2870 + if (err)
2871 goto err;
2872
2873 if (oldcard) {
2874 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2875 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2876 + err = -ENOENT;
2877 goto err;
2878 + }
2879
2880 card = oldcard;
2881 } else {
2882 @@ -333,32 +353,36 @@ static int mmc_sd_init_card(struct mmc_h
2883 * Allocate card structure.
2884 */
2885 card = mmc_alloc_card(host);
2886 - if (IS_ERR(card))
2887 + if (IS_ERR(card)) {
2888 + err = PTR_ERR(card);
2889 goto err;
2890 + }
2891
2892 card->type = MMC_TYPE_SD;
2893 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
2894 }
2895
2896 /*
2897 - * Set card RCA.
2898 + * For native busses: get card RCA and quit open drain mode.
2899 */
2900 - err = mmc_send_relative_addr(host, &card->rca);
2901 - if (err != MMC_ERR_NONE)
2902 - goto free_card;
2903 + if (!mmc_host_is_spi(host)) {
2904 + err = mmc_send_relative_addr(host, &card->rca);
2905 + if (err)
2906 + goto free_card;
2907
2908 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2909 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2910 + }
2911
2912 if (!oldcard) {
2913 /*
2914 * Fetch CSD from card.
2915 */
2916 err = mmc_send_csd(card, card->raw_csd);
2917 - if (err != MMC_ERR_NONE)
2918 + if (err)
2919 goto free_card;
2920
2921 err = mmc_decode_csd(card);
2922 - if (err < 0)
2923 + if (err)
2924 goto free_card;
2925
2926 mmc_decode_cid(card);
2927 @@ -367,16 +391,18 @@ static int mmc_sd_init_card(struct mmc_h
2928 /*
2929 * Select card, as all following commands rely on that.
2930 */
2931 - err = mmc_select_card(card);
2932 - if (err != MMC_ERR_NONE)
2933 - goto free_card;
2934 + if (!mmc_host_is_spi(host)) {
2935 + err = mmc_select_card(card);
2936 + if (err)
2937 + goto free_card;
2938 + }
2939
2940 if (!oldcard) {
2941 /*
2942 * Fetch SCR from card.
2943 */
2944 err = mmc_app_send_scr(card, card->raw_scr);
2945 - if (err != MMC_ERR_NONE)
2946 + if (err)
2947 goto free_card;
2948
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.
2952 */
2953 err = mmc_read_switch(card);
2954 - if (err != MMC_ERR_NONE)
2955 + if (err)
2956 goto free_card;
2957 }
2958
2959 @@ -395,7 +421,7 @@ static int mmc_sd_init_card(struct mmc_h
2960 * Attempt to change to high-speed (if supported)
2961 */
2962 err = mmc_switch_hs(card);
2963 - if (err != MMC_ERR_NONE)
2964 + if (err)
2965 goto free_card;
2966
2967 /*
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)
2973 + if (err)
2974 goto free_card;
2975
2976 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
2977 @@ -442,14 +468,14 @@ static int mmc_sd_init_card(struct mmc_h
2978 if (!oldcard)
2979 host->card = card;
2980
2981 - return MMC_ERR_NONE;
2982 + return 0;
2983
2984 free_card:
2985 if (!oldcard)
2986 mmc_remove_card(card);
2987 err:
2988
2989 - return MMC_ERR_FAILED;
2990 + return err;
2991 }
2992
2993 /*
2994 @@ -483,7 +509,7 @@ static void mmc_sd_detect(struct mmc_hos
2995
2996 mmc_release_host(host);
2997
2998 - if (err != MMC_ERR_NONE) {
2999 + if (err) {
3000 mmc_sd_remove(host);
3001
3002 mmc_claim_host(host);
3003 @@ -552,7 +578,8 @@ static void mmc_sd_suspend(struct mmc_ho
3004 BUG_ON(!host->card);
3005
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);
3012 }
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);
3016
3017 - if (err != MMC_ERR_NONE) {
3018 + if (err) {
3019 mmc_sd_remove(host);
3020
3021 mmc_claim_host(host);
3022 @@ -608,11 +635,22 @@ int mmc_attach_sd(struct mmc_host *host,
3023 int err;
3024
3025 BUG_ON(!host);
3026 - BUG_ON(!host->claimed);
3027 + WARN_ON(!host->claimed);
3028
3029 mmc_attach_bus(host, &mmc_sd_ops);
3030
3031 /*
3032 + * We need to get OCR a different way for SPI.
3033 + */
3034 + if (mmc_host_is_spi(host)) {
3035 + mmc_go_idle(host);
3036 +
3037 + err = mmc_spi_read_ocr(host, 0, &ocr);
3038 + if (err)
3039 + goto err;
3040 + }
3041 +
3042 + /*
3043 * Sanity check the voltages that the card claims to
3044 * support.
3045 */
3046 @@ -644,7 +682,7 @@ int mmc_attach_sd(struct mmc_host *host,
3047 * Detect and init the card.
3048 */
3049 err = mmc_sd_init_card(host, host->ocr, NULL);
3050 - if (err != MMC_ERR_NONE)
3051 + if (err)
3052 goto err;
3053
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);
3058
3059 - return 0;
3060 + return err;
3061 }
3062
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
3067 @@ -10,7 +10,6 @@
3068 */
3069
3070 #include <linux/types.h>
3071 -#include <asm/scatterlist.h>
3072 #include <linux/scatterlist.h>
3073
3074 #include <linux/mmc/host.h>
3075 @@ -33,21 +32,21 @@ static int mmc_app_cmd(struct mmc_host *
3076
3077 if (card) {
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;
3081 } else {
3082 cmd.arg = 0;
3083 - cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
3084 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
3085 }
3086
3087 err = mmc_wait_for_cmd(host, &cmd, 0);
3088 - if (err != MMC_ERR_NONE)
3089 + if (err)
3090 return err;
3091
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;
3097
3098 - return MMC_ERR_NONE;
3099 + return 0;
3100 }
3101
3102 /**
3103 @@ -73,7 +72,7 @@ int mmc_wait_for_app_cmd(struct mmc_host
3104 BUG_ON(!cmd);
3105 BUG_ON(retries < 0);
3106
3107 - err = MMC_ERR_INVALID;
3108 + err = -EIO;
3109
3110 /*
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));
3114
3115 err = mmc_app_cmd(host, card);
3116 - if (err != MMC_ERR_NONE)
3117 + if (err) {
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)
3121 + break;
3122 + }
3123 continue;
3124 + }
3125
3126 memset(&mrq, 0, sizeof(struct mmc_request));
3127
3128 @@ -97,8 +102,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3129 mmc_wait_for_req(host, &mrq);
3130
3131 err = cmd->error;
3132 - if (cmd->error == MMC_ERR_NONE)
3133 + if (!cmd->error)
3134 break;
3135 +
3136 + /* no point in retrying illegal APP commands */
3137 + if (mmc_host_is_spi(host)) {
3138 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3139 + break;
3140 + }
3141 }
3142
3143 return err;
3144 @@ -127,14 +138,14 @@ int mmc_app_set_bus_width(struct mmc_car
3145 cmd.arg = SD_BUS_WIDTH_4;
3146 break;
3147 default:
3148 - return MMC_ERR_INVALID;
3149 + return -EINVAL;
3150 }
3151
3152 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
3153 - if (err != MMC_ERR_NONE)
3154 + if (err)
3155 return err;
3156
3157 - return MMC_ERR_NONE;
3158 + return 0;
3159 }
3160
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));
3164
3165 cmd.opcode = SD_APP_OP_COND;
3166 - cmd.arg = ocr;
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 */
3170 + else
3171 + cmd.arg = ocr;
3172 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
3173
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)
3177 + if (err)
3178 break;
3179
3180 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
3181 + /* if we're just probing, do a single pass */
3182 + if (ocr == 0)
3183 break;
3184
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))
3189 + break;
3190 + } else {
3191 + if (cmd.resp[0] & MMC_CARD_BUSY)
3192 + break;
3193 + }
3194 +
3195 + err = -ETIMEDOUT;
3196
3197 mmc_delay(10);
3198 }
3199
3200 - if (rocr)
3201 + if (rocr && !mmc_host_is_spi(host))
3202 *rocr = cmd.resp[0];
3203
3204 return err;
3205 @@ -174,6 +198,7 @@ int mmc_send_if_cond(struct mmc_host *ho
3206 struct mmc_command cmd;
3207 int err;
3208 static const u8 test_pattern = 0xAA;
3209 + u8 result_pattern;
3210
3211 /*
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
3214 */
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;
3219
3220 err = mmc_wait_for_cmd(host, &cmd, 0);
3221 - if (err != MMC_ERR_NONE)
3222 + if (err)
3223 return err;
3224
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;
3229 + else
3230 + result_pattern = cmd.resp[0] & 0xFF;
3231 +
3232 + if (result_pattern != test_pattern)
3233 + return -EIO;
3234
3235 - return MMC_ERR_NONE;
3236 + return 0;
3237 }
3238
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;
3242
3243 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
3244 - if (err != MMC_ERR_NONE)
3245 + if (err)
3246 return err;
3247
3248 *rca = cmd.resp[0] >> 16;
3249
3250 - return MMC_ERR_NONE;
3251 + return 0;
3252 }
3253
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);
3257 BUG_ON(!scr);
3258
3259 + /* NOTE: caller guarantees scr is heap-allocated */
3260 +
3261 err = mmc_app_cmd(card->host, card);
3262 - if (err != MMC_ERR_NONE)
3263 + if (err)
3264 return err;
3265
3266 memset(&mrq, 0, sizeof(struct mmc_request));
3267 @@ -242,7 +274,7 @@ int mmc_app_send_scr(struct mmc_card *ca
3268
3269 cmd.opcode = SD_APP_SEND_SCR;
3270 cmd.arg = 0;
3271 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3272 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3273
3274 data.blksz = 8;
3275 data.blocks = 1;
3276 @@ -252,19 +284,19 @@ int mmc_app_send_scr(struct mmc_card *ca
3277
3278 sg_init_one(&sg, scr, 8);
3279
3280 - mmc_set_data_timeout(&data, card, 0);
3281 + mmc_set_data_timeout(&data, card);
3282
3283 mmc_wait_for_req(card->host, &mrq);
3284
3285 - if (cmd.error != MMC_ERR_NONE)
3286 + if (cmd.error)
3287 return cmd.error;
3288 - if (data.error != MMC_ERR_NONE)
3289 + if (data.error)
3290 return data.error;
3291
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]);
3296
3297 - return MMC_ERR_NONE;
3298 + return 0;
3299 }
3300
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,
3303 BUG_ON(!card);
3304 BUG_ON(!card->host);
3305
3306 + /* NOTE: caller guarantees resp is heap-allocated */
3307 +
3308 mode = !!mode;
3309 value &= 0xF;
3310
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;
3317
3318 data.blksz = 64;
3319 data.blocks = 1;
3320 @@ -302,15 +336,15 @@ int mmc_sd_switch(struct mmc_card *card,
3321
3322 sg_init_one(&sg, resp, 64);
3323
3324 - mmc_set_data_timeout(&data, card, 0);
3325 + mmc_set_data_timeout(&data, card);
3326
3327 mmc_wait_for_req(card->host, &mrq);
3328
3329 - if (cmd.error != MMC_ERR_NONE)
3330 + if (cmd.error)
3331 return cmd.error;
3332 - if (data.error != MMC_ERR_NONE)
3333 + if (data.error)
3334 return data.error;
3335
3336 - return MMC_ERR_NONE;
3337 + return 0;
3338 }
3339
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
3344 @@ -0,0 +1,395 @@
3345 +/*
3346 + * linux/drivers/mmc/sdio.c
3347 + *
3348 + * Copyright 2006-2007 Pierre Ossman
3349 + *
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.
3354 + */
3355 +
3356 +#include <linux/err.h>
3357 +
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>
3362 +
3363 +#include "core.h"
3364 +#include "bus.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"
3370 +