[adm5120] load the mac address from flash where it is possible
[openwrt.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 --- a/drivers/mmc/host/Kconfig
6 +++ b/drivers/mmc/host/Kconfig
7 @@ -100,3 +100,16 @@
8 To compile this driver as a module, choose M here: the
9 module will be called tifm_sd.
10
11 +config MMC_SPI
12 + tristate "MMC/SD over SPI (EXPERIMENTAL)"
13 + depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
14 + select CRC7
15 + select CRC_ITU_T
16 + help
17 + Some systems accss MMC/SD cards using a SPI controller instead of
18 + using a "native" MMC/SD controller. This has a disadvantage of
19 + being relatively high overhead, but a compensating advantage of
20 + working on many systems without dedicated MMC/SD controllers.
21 +
22 + If unsure, or if your system has no SPI master driver, say N.
23 +
24 --- a/drivers/mmc/host/Makefile
25 +++ b/drivers/mmc/host/Makefile
26 @@ -15,4 +15,5 @@
27 obj-$(CONFIG_MMC_OMAP) += omap.o
28 obj-$(CONFIG_MMC_AT91) += at91_mci.o
29 obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
30 +obj-$(CONFIG_MMC_SPI) += mmc_spi.o
31
32 --- /dev/null
33 +++ b/drivers/mmc/host/mmc_spi.c
34 @@ -0,0 +1,1419 @@
35 +/*
36 + * mmc_spi.c - Access SD/MMC cards through SPI master controllers
37 + *
38 + * (C) Copyright 2005, Intec Automation,
39 + * Mike Lavender (mike@steroidmicros)
40 + * (C) Copyright 2006-2007, David Brownell
41 + * (C) Copyright 2007, Axis Communications,
42 + * Hans-Peter Nilsson (hp@axis.com)
43 + * (C) Copyright 2007, ATRON electronic GmbH,
44 + * Jan Nikitenko <jan.nikitenko@gmail.com>
45 + *
46 + *
47 + * This program is free software; you can redistribute it and/or modify
48 + * it under the terms of the GNU General Public License as published by
49 + * the Free Software Foundation; either version 2 of the License, or
50 + * (at your option) any later version.
51 + *
52 + * This program is distributed in the hope that it will be useful,
53 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 + * GNU General Public License for more details.
56 + *
57 + * You should have received a copy of the GNU General Public License
58 + * along with this program; if not, write to the Free Software
59 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
60 + */
61 +#include <linux/hrtimer.h>
62 +#include <linux/delay.h>
63 +#include <linux/bio.h>
64 +#include <linux/dma-mapping.h>
65 +#include <linux/crc7.h>
66 +#include <linux/crc-itu-t.h>
67 +#include <linux/scatterlist.h>
68 +
69 +#include <linux/mmc/host.h>
70 +#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
71 +
72 +#include <linux/spi/spi.h>
73 +#include <linux/spi/mmc_spi.h>
74 +
75 +#include <asm/unaligned.h>
76 +
77 +
78 +#define sg_page(sg) (sg)->page
79 +
80 +
81 +/* NOTES:
82 + *
83 + * - For now, we won't try to interoperate with a real mmc/sd/sdio
84 + * controller, although some of them do have hardware support for
85 + * SPI protocol. The main reason for such configs would be mmc-ish
86 + * cards like DataFlash, which don't support that "native" protocol.
87 + *
88 + * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
89 + * switch between driver stacks, and in any case if "native" mode
90 + * is available, it will be faster and hence preferable.
91 + *
92 + * - MMC depends on a different chipselect management policy than the
93 + * SPI interface currently supports for shared bus segments: it needs
94 + * to issue multiple spi_message requests with the chipselect active,
95 + * using the results of one message to decide the next one to issue.
96 + *
97 + * Pending updates to the programming interface, this driver expects
98 + * that it not share the bus with other drivers (precluding conflicts).
99 + *
100 + * - We tell the controller to keep the chipselect active from the
101 + * beginning of an mmc_host_ops.request until the end. So beware
102 + * of SPI controller drivers that mis-handle the cs_change flag!
103 + *
104 + * However, many cards seem OK with chipselect flapping up/down
105 + * during that time ... at least on unshared bus segments.
106 + */
107 +
108 +
109 +/*
110 + * Local protocol constants, internal to data block protocols.
111 + */
112 +
113 +/* Response tokens used to ack each block written: */
114 +#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
115 +#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
116 +#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
117 +#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
118 +
119 +/* Read and write blocks start with these tokens and end with crc;
120 + * on error, read tokens act like a subset of R2_SPI_* values.
121 + */
122 +#define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
123 +#define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
124 +#define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
125 +
126 +#define MMC_SPI_BLOCKSIZE 512
127 +
128 +
129 +/* These fixed timeouts come from the latest SD specs, which say to ignore
130 + * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
131 + * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
132 + * reads which takes nowhere near that long. Older cards may be able to use
133 + * shorter timeouts ... but why bother?
134 + */
135 +#define readblock_timeout ktime_set(0, 100 * 1000 * 1000)
136 +#define writeblock_timeout ktime_set(0, 250 * 1000 * 1000)
137 +#define r1b_timeout ktime_set(3, 0)
138 +
139 +
140 +/****************************************************************************/
141 +
142 +/*
143 + * Local Data Structures
144 + */
145 +
146 +/* "scratch" is per-{command,block} data exchanged with the card */
147 +struct scratch {
148 + u8 status[29];
149 + u8 data_token;
150 + __be16 crc_val;
151 +};
152 +
153 +struct mmc_spi_host {
154 + struct mmc_host *mmc;
155 + struct spi_device *spi;
156 +
157 + unsigned char power_mode;
158 + u16 powerup_msecs;
159 +
160 + struct mmc_spi_platform_data *pdata;
161 +
162 + /* for bulk data transfers */
163 + struct spi_transfer token, t, crc, early_status;
164 + struct spi_message m;
165 +
166 + /* for status readback */
167 + struct spi_transfer status;
168 + struct spi_message readback;
169 +
170 + /* underlying DMA-aware controller, or null */
171 + struct device *dma_dev;
172 +
173 + /* buffer used for commands and for message "overhead" */
174 + struct scratch *data;
175 + dma_addr_t data_dma;
176 +
177 + /* Specs say to write ones most of the time, even when the card
178 + * has no need to read its input data; and many cards won't care.
179 + * This is our source of those ones.
180 + */
181 + void *ones;
182 + dma_addr_t ones_dma;
183 +};
184 +
185 +
186 +/****************************************************************************/
187 +
188 +/*
189 + * MMC-over-SPI protocol glue, used by the MMC stack interface
190 + */
191 +
192 +static inline int mmc_cs_off(struct mmc_spi_host *host)
193 +{
194 + /* chipselect will always be inactive after setup() */
195 + return spi_setup(host->spi);
196 +}
197 +
198 +static int
199 +mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
200 +{
201 + int status;
202 +
203 + if (len > sizeof(*host->data)) {
204 + WARN_ON(1);
205 + return -EIO;
206 + }
207 +
208 + host->status.len = len;
209 +
210 + if (host->dma_dev)
211 + dma_sync_single_for_device(host->dma_dev,
212 + host->data_dma, sizeof(*host->data),
213 + DMA_FROM_DEVICE);
214 +
215 + status = spi_sync(host->spi, &host->readback);
216 +
217 + if (host->dma_dev)
218 + dma_sync_single_for_cpu(host->dma_dev,
219 + host->data_dma, sizeof(*host->data),
220 + DMA_FROM_DEVICE);
221 +
222 + return status;
223 +}
224 +
225 +static int
226 +mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
227 +{
228 + u8 *cp = host->data->status;
229 +
230 + timeout = ktime_add(timeout, ktime_get());
231 +
232 + while (1) {
233 + int status;
234 + unsigned i;
235 +
236 + status = mmc_spi_readbytes(host, n);
237 + if (status < 0)
238 + return status;
239 +
240 + for (i = 0; i < n; i++) {
241 + if (cp[i] != byte)
242 + return cp[i];
243 + }
244 +
245 + /* REVISIT investigate msleep() to avoid busy-wait I/O
246 + * in at least some cases.
247 + */
248 + if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
249 + break;
250 + }
251 + return -ETIMEDOUT;
252 +}
253 +
254 +static inline int
255 +mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
256 +{
257 + return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
258 +}
259 +
260 +static int mmc_spi_readtoken(struct mmc_spi_host *host)
261 +{
262 + return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
263 +}
264 +
265 +
266 +/*
267 + * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
268 + * hosts return! The low byte holds R1_SPI bits. The next byte may hold
269 + * R2_SPI bits ... for SEND_STATUS, or after data read errors.
270 + *
271 + * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
272 + * newer cards R7 (IF_COND).
273 + */
274 +
275 +static char *maptype(struct mmc_command *cmd)
276 +{
277 + switch (mmc_spi_resp_type(cmd)) {
278 + case MMC_RSP_SPI_R1: return "R1";
279 + case MMC_RSP_SPI_R1B: return "R1B";
280 + case MMC_RSP_SPI_R2: return "R2/R5";
281 + case MMC_RSP_SPI_R3: return "R3/R4/R7";
282 + default: return "?";
283 + }
284 +}
285 +
286 +/* return zero, else negative errno after setting cmd->error */
287 +static int mmc_spi_response_get(struct mmc_spi_host *host,
288 + struct mmc_command *cmd, int cs_on)
289 +{
290 + u8 *cp = host->data->status;
291 + u8 *end = cp + host->t.len;
292 + int value = 0;
293 + char tag[32];
294 +
295 + snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
296 + cmd->opcode, maptype(cmd));
297 +
298 + /* Except for data block reads, the whole response will already
299 + * be stored in the scratch buffer. It's somewhere after the
300 + * command and the first byte we read after it. We ignore that
301 + * first byte. After STOP_TRANSMISSION command it may include
302 + * two data bits, but otherwise it's all ones.
303 + */
304 + cp += 8;
305 + while (cp < end && *cp == 0xff)
306 + cp++;
307 +
308 + /* Data block reads (R1 response types) may need more data... */
309 + if (cp == end) {
310 + unsigned i;
311 +
312 + cp = host->data->status;
313 +
314 + /* Card sends N(CR) (== 1..8) bytes of all-ones then one
315 + * status byte ... and we already scanned 2 bytes.
316 + *
317 + * REVISIT block read paths use nasty byte-at-a-time I/O
318 + * so it can always DMA directly into the target buffer.
319 + * It'd probably be better to memcpy() the first chunk and
320 + * avoid extra i/o calls...
321 + */
322 + for (i = 2; i < 9; i++) {
323 + value = mmc_spi_readbytes(host, 1);
324 + if (value < 0)
325 + goto done;
326 + if (*cp != 0xff)
327 + goto checkstatus;
328 + }
329 + value = -ETIMEDOUT;
330 + goto done;
331 + }
332 +
333 +checkstatus:
334 + if (*cp & 0x80) {
335 + dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
336 + tag, *cp);
337 + value = -EBADR;
338 + goto done;
339 + }
340 +
341 + cmd->resp[0] = *cp++;
342 + cmd->error = 0;
343 +
344 + /* Status byte: the entire seven-bit R1 response. */
345 + if (cmd->resp[0] != 0) {
346 + if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS
347 + | R1_SPI_ILLEGAL_COMMAND)
348 + & cmd->resp[0])
349 + value = -EINVAL;
350 + else if (R1_SPI_COM_CRC & cmd->resp[0])
351 + value = -EILSEQ;
352 + else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
353 + & cmd->resp[0])
354 + value = -EIO;
355 + /* else R1_SPI_IDLE, "it's resetting" */
356 + }
357 +
358 + switch (mmc_spi_resp_type(cmd)) {
359 +
360 + /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
361 + * and less-common stuff like various erase operations.
362 + */
363 + case MMC_RSP_SPI_R1B:
364 + /* maybe we read all the busy tokens already */
365 + while (cp < end && *cp == 0)
366 + cp++;
367 + if (cp == end)
368 + mmc_spi_wait_unbusy(host, r1b_timeout);
369 + break;
370 +
371 + /* SPI R2 == R1 + second status byte; SEND_STATUS
372 + * SPI R5 == R1 + data byte; IO_RW_DIRECT
373 + */
374 + case MMC_RSP_SPI_R2:
375 + cmd->resp[0] |= *cp << 8;
376 + break;
377 +
378 + /* SPI R3, R4, or R7 == R1 + 4 bytes */
379 + case MMC_RSP_SPI_R3:
380 + cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
381 + break;
382 +
383 + /* SPI R1 == just one status byte */
384 + case MMC_RSP_SPI_R1:
385 + break;
386 +
387 + default:
388 + dev_dbg(&host->spi->dev, "bad response type %04x\n",
389 + mmc_spi_resp_type(cmd));
390 + if (value >= 0)
391 + value = -EINVAL;
392 + goto done;
393 + }
394 +
395 + if (value < 0)
396 + dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
397 + tag, cmd->resp[0], cmd->resp[1]);
398 +
399 + /* disable chipselect on errors and some success cases */
400 + if (value >= 0 && cs_on)
401 + return value;
402 +done:
403 + if (value < 0)
404 + cmd->error = value;
405 + mmc_cs_off(host);
406 + return value;
407 +}
408 +
409 +/* Issue command and read its response.
410 + * Returns zero on success, negative for error.
411 + *
412 + * On error, caller must cope with mmc core retry mechanism. That
413 + * means immediate low-level resubmit, which affects the bus lock...
414 + */
415 +static int
416 +mmc_spi_command_send(struct mmc_spi_host *host,
417 + struct mmc_request *mrq,
418 + struct mmc_command *cmd, int cs_on)
419 +{
420 + struct scratch *data = host->data;
421 + u8 *cp = data->status;
422 + u32 arg = cmd->arg;
423 + int status;
424 + struct spi_transfer *t;
425 +
426 + /* We can handle most commands (except block reads) in one full
427 + * duplex I/O operation before either starting the next transfer
428 + * (data block or command) or else deselecting the card.
429 + *
430 + * First, write 7 bytes:
431 + * - an all-ones byte to ensure the card is ready
432 + * - opcode byte (plus start and transmission bits)
433 + * - four bytes of big-endian argument
434 + * - crc7 (plus end bit) ... always computed, it's cheap
435 + *
436 + * We init the whole buffer to all-ones, which is what we need
437 + * to write while we're reading (later) response data.
438 + */
439 + memset(cp++, 0xff, sizeof(data->status));
440 +
441 + *cp++ = 0x40 | cmd->opcode;
442 + *cp++ = (u8)(arg >> 24);
443 + *cp++ = (u8)(arg >> 16);
444 + *cp++ = (u8)(arg >> 8);
445 + *cp++ = (u8)arg;
446 + *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
447 +
448 + /* Then, read up to 13 bytes (while writing all-ones):
449 + * - N(CR) (== 1..8) bytes of all-ones
450 + * - status byte (for all response types)
451 + * - the rest of the response, either:
452 + * + nothing, for R1 or R1B responses
453 + * + second status byte, for R2 responses
454 + * + four data bytes, for R3 and R7 responses
455 + *
456 + * Finally, read some more bytes ... in the nice cases we know in
457 + * advance how many, and reading 1 more is always OK:
458 + * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
459 + * - N(RC) (== 1..N) bytes of all-ones, before next command
460 + * - N(WR) (== 1..N) bytes of all-ones, before data write
461 + *
462 + * So in those cases one full duplex I/O of at most 21 bytes will
463 + * handle the whole command, leaving the card ready to receive a
464 + * data block or new command. We do that whenever we can, shaving
465 + * CPU and IRQ costs (especially when using DMA or FIFOs).
466 + *
467 + * There are two other cases, where it's not generally practical
468 + * to rely on a single I/O:
469 + *
470 + * - R1B responses need at least N(EC) bytes of all-zeroes.
471 + *
472 + * In this case we can *try* to fit it into one I/O, then
473 + * maybe read more data later.
474 + *
475 + * - Data block reads are more troublesome, since a variable
476 + * number of padding bytes precede the token and data.
477 + * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
478 + * + N(AC) (== 1..many) bytes of all-ones
479 + *
480 + * In this case we currently only have minimal speedups here:
481 + * when N(CR) == 1 we can avoid I/O in response_get().
482 + */
483 + if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
484 + cp += 2; /* min(N(CR)) + status */
485 + /* R1 */
486 + } else {
487 + cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
488 + if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
489 + cp++;
490 + else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
491 + cp += 4;
492 + else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
493 + cp = data->status + sizeof(data->status);
494 + /* else: R1 (most commands) */
495 + }
496 +
497 + dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
498 + cmd->opcode, maptype(cmd));
499 +
500 + /* send command, leaving chipselect active */
501 + spi_message_init(&host->m);
502 +
503 + t = &host->t;
504 + memset(t, 0, sizeof(*t));
505 + t->tx_buf = t->rx_buf = data->status;
506 + t->tx_dma = t->rx_dma = host->data_dma;
507 + t->len = cp - data->status;
508 + t->cs_change = 1;
509 + spi_message_add_tail(t, &host->m);
510 +
511 + if (host->dma_dev) {
512 + host->m.is_dma_mapped = 1;
513 + dma_sync_single_for_device(host->dma_dev,
514 + host->data_dma, sizeof(*host->data),
515 + DMA_BIDIRECTIONAL);
516 + }
517 + status = spi_sync(host->spi, &host->m);
518 +
519 + if (host->dma_dev)
520 + dma_sync_single_for_cpu(host->dma_dev,
521 + host->data_dma, sizeof(*host->data),
522 + DMA_BIDIRECTIONAL);
523 + if (status < 0) {
524 + dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
525 + cmd->error = status;
526 + return status;
527 + }
528 +
529 + /* after no-data commands and STOP_TRANSMISSION, chipselect off */
530 + return mmc_spi_response_get(host, cmd, cs_on);
531 +}
532 +
533 +/* Build data message with up to four separate transfers. For TX, we
534 + * start by writing the data token. And in most cases, we finish with
535 + * a status transfer.
536 + *
537 + * We always provide TX data for data and CRC. The MMC/SD protocol
538 + * requires us to write ones; but Linux defaults to writing zeroes;
539 + * so we explicitly initialize it to all ones on RX paths.
540 + *
541 + * We also handle DMA mapping, so the underlying SPI controller does
542 + * not need to (re)do it for each message.
543 + */
544 +static void
545 +mmc_spi_setup_data_message(
546 + struct mmc_spi_host *host,
547 + int multiple,
548 + enum dma_data_direction direction)
549 +{
550 + struct spi_transfer *t;
551 + struct scratch *scratch = host->data;
552 + dma_addr_t dma = host->data_dma;
553 +
554 + spi_message_init(&host->m);
555 + if (dma)
556 + host->m.is_dma_mapped = 1;
557 +
558 + /* for reads, readblock() skips 0xff bytes before finding
559 + * the token; for writes, this transfer issues that token.
560 + */
561 + if (direction == DMA_TO_DEVICE) {
562 + t = &host->token;
563 + memset(t, 0, sizeof(*t));
564 + t->len = 1;
565 + if (multiple)
566 + scratch->data_token = SPI_TOKEN_MULTI_WRITE;
567 + else
568 + scratch->data_token = SPI_TOKEN_SINGLE;
569 + t->tx_buf = &scratch->data_token;
570 + if (dma)
571 + t->tx_dma = dma + offsetof(struct scratch, data_token);
572 + spi_message_add_tail(t, &host->m);
573 + }
574 +
575 + /* Body of transfer is buffer, then CRC ...
576 + * either TX-only, or RX with TX-ones.
577 + */
578 + t = &host->t;
579 + memset(t, 0, sizeof(*t));
580 + t->tx_buf = host->ones;
581 + t->tx_dma = host->ones_dma;
582 + /* length and actual buffer info are written later */
583 + spi_message_add_tail(t, &host->m);
584 +
585 + t = &host->crc;
586 + memset(t, 0, sizeof(*t));
587 + t->len = 2;
588 + if (direction == DMA_TO_DEVICE) {
589 + /* the actual CRC may get written later */
590 + t->tx_buf = &scratch->crc_val;
591 + if (dma)
592 + t->tx_dma = dma + offsetof(struct scratch, crc_val);
593 + } else {
594 + t->tx_buf = host->ones;
595 + t->tx_dma = host->ones_dma;
596 + t->rx_buf = &scratch->crc_val;
597 + if (dma)
598 + t->rx_dma = dma + offsetof(struct scratch, crc_val);
599 + }
600 + spi_message_add_tail(t, &host->m);
601 +
602 + /*
603 + * A single block read is followed by N(EC) [0+] all-ones bytes
604 + * before deselect ... don't bother.
605 + *
606 + * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
607 + * the next block is read, or a STOP_TRANSMISSION is issued. We'll
608 + * collect that single byte, so readblock() doesn't need to.
609 + *
610 + * For a write, the one-byte data response follows immediately, then
611 + * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
612 + * Then single block reads may deselect, and multiblock ones issue
613 + * the next token (next data block, or STOP_TRAN). We can try to
614 + * minimize I/O ops by using a single read to collect end-of-busy.
615 + */
616 + if (multiple || direction == DMA_TO_DEVICE) {
617 + t = &host->early_status;
618 + memset(t, 0, sizeof(*t));
619 + t->len = (direction == DMA_TO_DEVICE)
620 + ? sizeof(scratch->status)
621 + : 1;
622 + t->tx_buf = host->ones;
623 + t->tx_dma = host->ones_dma;
624 + t->rx_buf = scratch->status;
625 + if (dma)
626 + t->rx_dma = dma + offsetof(struct scratch, status);
627 + t->cs_change = 1;
628 + spi_message_add_tail(t, &host->m);
629 + }
630 +}
631 +
632 +/*
633 + * Write one block:
634 + * - caller handled preceding N(WR) [1+] all-ones bytes
635 + * - data block
636 + * + token
637 + * + data bytes
638 + * + crc16
639 + * - an all-ones byte ... card writes a data-response byte
640 + * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
641 + *
642 + * Return negative errno, else success.
643 + */
644 +static int
645 +mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
646 +{
647 + struct spi_device *spi = host->spi;
648 + int status, i;
649 + struct scratch *scratch = host->data;
650 +
651 + if (host->mmc->use_spi_crc)
652 + scratch->crc_val = cpu_to_be16(
653 + crc_itu_t(0, t->tx_buf, t->len));
654 + if (host->dma_dev)
655 + dma_sync_single_for_device(host->dma_dev,
656 + host->data_dma, sizeof(*scratch),
657 + DMA_BIDIRECTIONAL);
658 +
659 + status = spi_sync(spi, &host->m);
660 +
661 + if (status != 0) {
662 + dev_dbg(&spi->dev, "write error (%d)\n", status);
663 + return status;
664 + }
665 +
666 + if (host->dma_dev)
667 + dma_sync_single_for_cpu(host->dma_dev,
668 + host->data_dma, sizeof(*scratch),
669 + DMA_BIDIRECTIONAL);
670 +
671 + /*
672 + * Get the transmission data-response reply. It must follow
673 + * immediately after the data block we transferred. This reply
674 + * doesn't necessarily tell whether the write operation succeeded;
675 + * it just says if the transmission was ok and whether *earlier*
676 + * writes succeeded; see the standard.
677 + */
678 + switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
679 + case SPI_RESPONSE_ACCEPTED:
680 + status = 0;
681 + break;
682 + case SPI_RESPONSE_CRC_ERR:
683 + /* host shall then issue MMC_STOP_TRANSMISSION */
684 + status = -EILSEQ;
685 + break;
686 + case SPI_RESPONSE_WRITE_ERR:
687 + /* host shall then issue MMC_STOP_TRANSMISSION,
688 + * and should MMC_SEND_STATUS to sort it out
689 + */
690 + status = -EIO;
691 + break;
692 + default:
693 + status = -EPROTO;
694 + break;
695 + }
696 + if (status != 0) {
697 + dev_dbg(&spi->dev, "write error %02x (%d)\n",
698 + scratch->status[0], status);
699 + return status;
700 + }
701 +
702 + t->tx_buf += t->len;
703 + if (host->dma_dev)
704 + t->tx_dma += t->len;
705 +
706 + /* Return when not busy. If we didn't collect that status yet,
707 + * we'll need some more I/O.
708 + */
709 + for (i = 1; i < sizeof(scratch->status); i++) {
710 + if (scratch->status[i] != 0)
711 + return 0;
712 + }
713 + return mmc_spi_wait_unbusy(host, writeblock_timeout);
714 +}
715 +
716 +/*
717 + * Read one block:
718 + * - skip leading all-ones bytes ... either
719 + * + N(AC) [1..f(clock,CSD)] usually, else
720 + * + N(CX) [0..8] when reading CSD or CID
721 + * - data block
722 + * + token ... if error token, no data or crc
723 + * + data bytes
724 + * + crc16
725 + *
726 + * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
727 + * before dropping chipselect.
728 + *
729 + * For multiblock reads, caller either reads the next block or issues a
730 + * STOP_TRANSMISSION command.
731 + */
732 +static int
733 +mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
734 +{
735 + struct spi_device *spi = host->spi;
736 + int status;
737 + struct scratch *scratch = host->data;
738 +
739 + /* At least one SD card sends an all-zeroes byte when N(CX)
740 + * applies, before the all-ones bytes ... just cope with that.
741 + */
742 + status = mmc_spi_readbytes(host, 1);
743 + if (status < 0)
744 + return status;
745 + status = scratch->status[0];
746 + if (status == 0xff || status == 0)
747 + status = mmc_spi_readtoken(host);
748 +
749 + if (status == SPI_TOKEN_SINGLE) {
750 + if (host->dma_dev) {
751 + dma_sync_single_for_device(host->dma_dev,
752 + host->data_dma, sizeof(*scratch),
753 + DMA_BIDIRECTIONAL);
754 + dma_sync_single_for_device(host->dma_dev,
755 + t->rx_dma, t->len,
756 + DMA_FROM_DEVICE);
757 + }
758 +
759 + status = spi_sync(spi, &host->m);
760 +
761 + if (host->dma_dev) {
762 + dma_sync_single_for_cpu(host->dma_dev,
763 + host->data_dma, sizeof(*scratch),
764 + DMA_BIDIRECTIONAL);
765 + dma_sync_single_for_cpu(host->dma_dev,
766 + t->rx_dma, t->len,
767 + DMA_FROM_DEVICE);
768 + }
769 +
770 + } else {
771 + dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
772 +
773 + /* we've read extra garbage, timed out, etc */
774 + if (status < 0)
775 + return status;
776 +
777 + /* low four bits are an R2 subset, fifth seems to be
778 + * vendor specific ... map them all to generic error..
779 + */
780 + return -EIO;
781 + }
782 +
783 + if (host->mmc->use_spi_crc) {
784 + u16 crc = crc_itu_t(0, t->rx_buf, t->len);
785 +
786 + be16_to_cpus(&scratch->crc_val);
787 + if (scratch->crc_val != crc) {
788 + dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
789 + "computed=0x%04x len=%d\n",
790 + scratch->crc_val, crc, t->len);
791 + return -EILSEQ;
792 + }
793 + }
794 +
795 + t->rx_buf += t->len;
796 + if (host->dma_dev)
797 + t->rx_dma += t->len;
798 +
799 + return 0;
800 +}
801 +
802 +/*
803 + * An MMC/SD data stage includes one or more blocks, optional CRCs,
804 + * and inline handshaking. That handhaking makes it unlike most
805 + * other SPI protocol stacks.
806 + */
807 +static void
808 +mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
809 + struct mmc_data *data, u32 blk_size)
810 +{
811 + struct spi_device *spi = host->spi;
812 + struct device *dma_dev = host->dma_dev;
813 + struct spi_transfer *t;
814 + enum dma_data_direction direction;
815 + struct scatterlist *sg;
816 + unsigned n_sg;
817 + int multiple = (data->blocks > 1);
818 +
819 + if (data->flags & MMC_DATA_READ)
820 + direction = DMA_FROM_DEVICE;
821 + else
822 + direction = DMA_TO_DEVICE;
823 + mmc_spi_setup_data_message(host, multiple, direction);
824 + t = &host->t;
825 +
826 + /* Handle scatterlist segments one at a time, with synch for
827 + * each 512-byte block
828 + */
829 + for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
830 + int status = 0;
831 + dma_addr_t dma_addr = 0;
832 + void *kmap_addr;
833 + unsigned length = sg->length;
834 + enum dma_data_direction dir = direction;
835 +
836 + /* set up dma mapping for controller drivers that might
837 + * use DMA ... though they may fall back to PIO
838 + */
839 + if (dma_dev) {
840 + /* never invalidate whole *shared* pages ... */
841 + if ((sg->offset != 0 || length != PAGE_SIZE)
842 + && dir == DMA_FROM_DEVICE)
843 + dir = DMA_BIDIRECTIONAL;
844 +
845 + dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
846 + PAGE_SIZE, dir);
847 + if (direction == DMA_TO_DEVICE)
848 + t->tx_dma = dma_addr + sg->offset;
849 + else
850 + t->rx_dma = dma_addr + sg->offset;
851 + }
852 +
853 + /* allow pio too; we don't allow highmem */
854 + kmap_addr = kmap(sg_page(sg));
855 + if (direction == DMA_TO_DEVICE)
856 + t->tx_buf = kmap_addr + sg->offset;
857 + else
858 + t->rx_buf = kmap_addr + sg->offset;
859 +
860 + /* transfer each block, and update request status */
861 + while (length) {
862 + t->len = min(length, blk_size);
863 +
864 + dev_dbg(&host->spi->dev,
865 + " mmc_spi: %s block, %d bytes\n",
866 + (direction == DMA_TO_DEVICE)
867 + ? "write"
868 + : "read",
869 + t->len);
870 +
871 + if (direction == DMA_TO_DEVICE)
872 + status = mmc_spi_writeblock(host, t);
873 + else
874 + status = mmc_spi_readblock(host, t);
875 + if (status < 0)
876 + break;
877 +
878 + data->bytes_xfered += t->len;
879 + length -= t->len;
880 +
881 + if (!multiple)
882 + break;
883 + }
884 +
885 + /* discard mappings */
886 + if (direction == DMA_FROM_DEVICE)
887 + flush_kernel_dcache_page(sg_page(sg));
888 + kunmap(sg_page(sg));
889 + if (dma_dev)
890 + dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
891 +
892 + if (status < 0) {
893 + data->error = status;
894 + dev_dbg(&spi->dev, "%s status %d\n",
895 + (direction == DMA_TO_DEVICE)
896 + ? "write" : "read",
897 + status);
898 + break;
899 + }
900 + }
901 +
902 + /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
903 + * can be issued before multiblock writes. Unlike its more widely
904 + * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
905 + * that can affect the STOP_TRAN logic. Complete (and current)
906 + * MMC specs should sort that out before Linux starts using CMD23.
907 + */
908 + if (direction == DMA_TO_DEVICE && multiple) {
909 + struct scratch *scratch = host->data;
910 + int tmp;
911 + const unsigned statlen = sizeof(scratch->status);
912 +
913 + dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
914 +
915 + /* Tweak the per-block message we set up earlier by morphing
916 + * it to hold single buffer with the token followed by some
917 + * all-ones bytes ... skip N(BR) (0..1), scan the rest for
918 + * "not busy any longer" status, and leave chip selected.
919 + */
920 + INIT_LIST_HEAD(&host->m.transfers);
921 + list_add(&host->early_status.transfer_list,
922 + &host->m.transfers);
923 +
924 + memset(scratch->status, 0xff, statlen);
925 + scratch->status[0] = SPI_TOKEN_STOP_TRAN;
926 +
927 + host->early_status.tx_buf = host->early_status.rx_buf;
928 + host->early_status.tx_dma = host->early_status.rx_dma;
929 + host->early_status.len = statlen;
930 +
931 + if (host->dma_dev)
932 + dma_sync_single_for_device(host->dma_dev,
933 + host->data_dma, sizeof(*scratch),
934 + DMA_BIDIRECTIONAL);
935 +
936 + tmp = spi_sync(spi, &host->m);
937 +
938 + if (host->dma_dev)
939 + dma_sync_single_for_cpu(host->dma_dev,
940 + host->data_dma, sizeof(*scratch),
941 + DMA_BIDIRECTIONAL);
942 +
943 + if (tmp < 0) {
944 + if (!data->error)
945 + data->error = tmp;
946 + return;
947 + }
948 +
949 + /* Ideally we collected "not busy" status with one I/O,
950 + * avoiding wasteful byte-at-a-time scanning... but more
951 + * I/O is often needed.
952 + */
953 + for (tmp = 2; tmp < statlen; tmp++) {
954 + if (scratch->status[tmp] != 0)
955 + return;
956 + }
957 + tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
958 + if (tmp < 0 && !data->error)
959 + data->error = tmp;
960 + }
961 +}
962 +
963 +/****************************************************************************/
964 +
965 +/*
966 + * MMC driver implementation -- the interface to the MMC stack
967 + */
968 +
969 +static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
970 +{
971 + struct mmc_spi_host *host = mmc_priv(mmc);
972 + int status = -EINVAL;
973 +
974 +#ifdef DEBUG
975 + /* MMC core and layered drivers *MUST* issue SPI-aware commands */
976 + {
977 + struct mmc_command *cmd;
978 + int invalid = 0;
979 +
980 + cmd = mrq->cmd;
981 + if (!mmc_spi_resp_type(cmd)) {
982 + dev_dbg(&host->spi->dev, "bogus command\n");
983 + cmd->error = -EINVAL;
984 + invalid = 1;
985 + }
986 +
987 + cmd = mrq->stop;
988 + if (cmd && !mmc_spi_resp_type(cmd)) {
989 + dev_dbg(&host->spi->dev, "bogus STOP command\n");
990 + cmd->error = -EINVAL;
991 + invalid = 1;
992 + }
993 +
994 + if (invalid) {
995 + dump_stack();
996 + mmc_request_done(host->mmc, mrq);
997 + return;
998 + }
999 + }
1000 +#endif
1001 +
1002 + /* issue command; then optionally data and stop */
1003 + status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
1004 + if (status == 0 && mrq->data) {
1005 + mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
1006 + if (mrq->stop)
1007 + status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1008 + else
1009 + mmc_cs_off(host);
1010 + }
1011 +
1012 + mmc_request_done(host->mmc, mrq);
1013 +}
1014 +
1015 +/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1016 + *
1017 + * NOTE that here we can't know that the card has just been powered up;
1018 + * not all MMC/SD sockets support power switching.
1019 + *
1020 + * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1021 + * this doesn't seem to do the right thing at all...
1022 + */
1023 +static void mmc_spi_initsequence(struct mmc_spi_host *host)
1024 +{
1025 + /* Try to be very sure any previous command has completed;
1026 + * wait till not-busy, skip debris from any old commands.
1027 + */
1028 + mmc_spi_wait_unbusy(host, r1b_timeout);
1029 + mmc_spi_readbytes(host, 10);
1030 +
1031 + /*
1032 + * Do a burst with chipselect active-high. We need to do this to
1033 + * meet the requirement of 74 clock cycles with both chipselect
1034 + * and CMD (MOSI) high before CMD0 ... after the card has been
1035 + * powered up to Vdd(min), and so is ready to take commands.
1036 + *
1037 + * Some cards are particularly needy of this (e.g. Viking "SD256")
1038 + * while most others don't seem to care.
1039 + *
1040 + * Note that this is one of the places MMC/SD plays games with the
1041 + * SPI protocol. Another is that when chipselect is released while
1042 + * the card returns BUSY status, the clock must issue several cycles
1043 + * with chipselect high before the card will stop driving its output.
1044 + */
1045 + host->spi->mode |= SPI_CS_HIGH;
1046 + if (spi_setup(host->spi) != 0) {
1047 + /* Just warn; most cards work without it. */
1048 + dev_warn(&host->spi->dev,
1049 + "can't change chip-select polarity\n");
1050 + host->spi->mode &= ~SPI_CS_HIGH;
1051 + } else {
1052 + mmc_spi_readbytes(host, 18);
1053 +
1054 + host->spi->mode &= ~SPI_CS_HIGH;
1055 + if (spi_setup(host->spi) != 0) {
1056 + /* Wot, we can't get the same setup we had before? */
1057 + dev_err(&host->spi->dev,
1058 + "can't restore chip-select polarity\n");
1059 + }
1060 + }
1061 +}
1062 +
1063 +static char *mmc_powerstring(u8 power_mode)
1064 +{
1065 + switch (power_mode) {
1066 + case MMC_POWER_OFF: return "off";
1067 + case MMC_POWER_UP: return "up";
1068 + case MMC_POWER_ON: return "on";
1069 + }
1070 + return "?";
1071 +}
1072 +
1073 +static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1074 +{
1075 + struct mmc_spi_host *host = mmc_priv(mmc);
1076 +
1077 + if (host->power_mode != ios->power_mode) {
1078 + int canpower;
1079 +
1080 + canpower = host->pdata && host->pdata->setpower;
1081 +
1082 + dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1083 + mmc_powerstring(ios->power_mode),
1084 + ios->vdd,
1085 + canpower ? ", can switch" : "");
1086 +
1087 + /* switch power on/off if possible, accounting for
1088 + * max 250msec powerup time if needed.
1089 + */
1090 + if (canpower) {
1091 + switch (ios->power_mode) {
1092 + case MMC_POWER_OFF:
1093 + case MMC_POWER_UP:
1094 + host->pdata->setpower(&host->spi->dev,
1095 + ios->vdd);
1096 + if (ios->power_mode == MMC_POWER_UP)
1097 + msleep(host->powerup_msecs);
1098 + }
1099 + }
1100 +
1101 + /* See 6.4.1 in the simplified SD card physical spec 2.0 */
1102 + if (ios->power_mode == MMC_POWER_ON)
1103 + mmc_spi_initsequence(host);
1104 +
1105 + /* If powering down, ground all card inputs to avoid power
1106 + * delivery from data lines! On a shared SPI bus, this
1107 + * will probably be temporary; 6.4.2 of the simplified SD
1108 + * spec says this must last at least 1msec.
1109 + *
1110 + * - Clock low means CPOL 0, e.g. mode 0
1111 + * - MOSI low comes from writing zero
1112 + * - Chipselect is usually active low...
1113 + */
1114 + if (canpower && ios->power_mode == MMC_POWER_OFF) {
1115 + int mres;
1116 +
1117 + host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1118 + mres = spi_setup(host->spi);
1119 + if (mres < 0)
1120 + dev_dbg(&host->spi->dev,
1121 + "switch to SPI mode 0 failed\n");
1122 +
1123 + if (spi_w8r8(host->spi, 0x00) < 0)
1124 + dev_dbg(&host->spi->dev,
1125 + "put spi signals to low failed\n");
1126 +
1127 + /*
1128 + * Now clock should be low due to spi mode 0;
1129 + * MOSI should be low because of written 0x00;
1130 + * chipselect should be low (it is active low)
1131 + * power supply is off, so now MMC is off too!
1132 + *
1133 + * FIXME no, chipselect can be high since the
1134 + * device is inactive and SPI_CS_HIGH is clear...
1135 + */
1136 + msleep(10);
1137 + if (mres == 0) {
1138 + host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1139 + mres = spi_setup(host->spi);
1140 + if (mres < 0)
1141 + dev_dbg(&host->spi->dev,
1142 + "switch back to SPI mode 3"
1143 + " failed\n");
1144 + }
1145 + }
1146 +
1147 + host->power_mode = ios->power_mode;
1148 + }
1149 +
1150 + if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1151 + int status;
1152 +
1153 + host->spi->max_speed_hz = ios->clock;
1154 + status = spi_setup(host->spi);
1155 + dev_dbg(&host->spi->dev,
1156 + "mmc_spi: clock to %d Hz, %d\n",
1157 + host->spi->max_speed_hz, status);
1158 + }
1159 +}
1160 +
1161 +static int mmc_spi_get_ro(struct mmc_host *mmc)
1162 +{
1163 + struct mmc_spi_host *host = mmc_priv(mmc);
1164 +
1165 + if (host->pdata && host->pdata->get_ro)
1166 + return host->pdata->get_ro(mmc->parent);
1167 + /* board doesn't support read only detection; assume writeable */
1168 + return 0;
1169 +}
1170 +
1171 +
1172 +static const struct mmc_host_ops mmc_spi_ops = {
1173 + .request = mmc_spi_request,
1174 + .set_ios = mmc_spi_set_ios,
1175 + .get_ro = mmc_spi_get_ro,
1176 +};
1177 +
1178 +
1179 +/****************************************************************************/
1180 +
1181 +/*
1182 + * SPI driver implementation
1183 + */
1184 +
1185 +static irqreturn_t
1186 +mmc_spi_detect_irq(int irq, void *mmc)
1187 +{
1188 + struct mmc_spi_host *host = mmc_priv(mmc);
1189 + u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1190 +
1191 + mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1192 + return IRQ_HANDLED;
1193 +}
1194 +
1195 +struct count_children {
1196 + unsigned n;
1197 + struct bus_type *bus;
1198 +};
1199 +
1200 +static int maybe_count_child(struct device *dev, void *c)
1201 +{
1202 + struct count_children *ccp = c;
1203 +
1204 + if (dev->bus == ccp->bus) {
1205 + if (ccp->n)
1206 + return -EBUSY;
1207 + ccp->n++;
1208 + }
1209 + return 0;
1210 +}
1211 +
1212 +static int mmc_spi_probe(struct spi_device *spi)
1213 +{
1214 + void *ones;
1215 + struct mmc_host *mmc;
1216 + struct mmc_spi_host *host;
1217 + int status;
1218 +
1219 + /* MMC and SD specs only seem to care that sampling is on the
1220 + * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
1221 + * should be legit. We'll use mode 0 since it seems to be a
1222 + * bit less troublesome on some hardware ... unclear why.
1223 + */
1224 + spi->mode = SPI_MODE_0;
1225 + spi->bits_per_word = 8;
1226 +
1227 + status = spi_setup(spi);
1228 + if (status < 0) {
1229 + dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1230 + spi->mode, spi->max_speed_hz / 1000,
1231 + status);
1232 + return status;
1233 + }
1234 +
1235 + /* We can use the bus safely iff nobody else will interfere with us.
1236 + * Most commands consist of one SPI message to issue a command, then
1237 + * several more to collect its response, then possibly more for data
1238 + * transfer. Clocking access to other devices during that period will
1239 + * corrupt the command execution.
1240 + *
1241 + * Until we have software primitives which guarantee non-interference,
1242 + * we'll aim for a hardware-level guarantee.
1243 + *
1244 + * REVISIT we can't guarantee another device won't be added later...
1245 + */
1246 + if (spi->master->num_chipselect > 1) {
1247 + struct count_children cc;
1248 +
1249 + cc.n = 0;
1250 + cc.bus = spi->dev.bus;
1251 + status = device_for_each_child(spi->dev.parent, &cc,
1252 + maybe_count_child);
1253 + if (status < 0) {
1254 + dev_err(&spi->dev, "can't share SPI bus\n");
1255 + return status;
1256 + }
1257 +
1258 + dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
1259 + }
1260 +
1261 + /* We need a supply of ones to transmit. This is the only time
1262 + * the CPU touches these, so cache coherency isn't a concern.
1263 + *
1264 + * NOTE if many systems use more than one MMC-over-SPI connector
1265 + * it'd save some memory to share this. That's evidently rare.
1266 + */
1267 + status = -ENOMEM;
1268 + ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1269 + if (!ones)
1270 + goto nomem;
1271 + memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1272 +
1273 + mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1274 + if (!mmc)
1275 + goto nomem;
1276 +
1277 + mmc->ops = &mmc_spi_ops;
1278 + mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1279 +
1280 + /* As long as we keep track of the number of successfully
1281 + * transmitted blocks, we're good for multiwrite.
1282 + */
1283 + mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1284 +
1285 + /* SPI doesn't need the lowspeed device identification thing for
1286 + * MMC or SD cards, since it never comes up in open drain mode.
1287 + * That's good; some SPI masters can't handle very low speeds!
1288 + *
1289 + * However, low speed SDIO cards need not handle over 400 KHz;
1290 + * that's the only reason not to use a few MHz for f_min (until
1291 + * the upper layer reads the target frequency from the CSD).
1292 + */
1293 + mmc->f_min = 400000;
1294 + mmc->f_max = spi->max_speed_hz;
1295 +
1296 + host = mmc_priv(mmc);
1297 + host->mmc = mmc;
1298 + host->spi = spi;
1299 +
1300 + host->ones = ones;
1301 +
1302 + /* Platform data is used to hook up things like card sensing
1303 + * and power switching gpios.
1304 + */
1305 + host->pdata = spi->dev.platform_data;
1306 + if (host->pdata)
1307 + mmc->ocr_avail = host->pdata->ocr_mask;
1308 + if (!mmc->ocr_avail) {
1309 + dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1310 + mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1311 + }
1312 + if (host->pdata && host->pdata->setpower) {
1313 + host->powerup_msecs = host->pdata->powerup_msecs;
1314 + if (!host->powerup_msecs || host->powerup_msecs > 250)
1315 + host->powerup_msecs = 250;
1316 + }
1317 +
1318 + dev_set_drvdata(&spi->dev, mmc);
1319 +
1320 + /* preallocate dma buffers */
1321 + host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1322 + if (!host->data)
1323 + goto fail_nobuf1;
1324 +
1325 +//FIXME
1326 +#if 0
1327 + if (spi->master->dev.parent->dma_mask) {
1328 + struct device *dev = spi->master->dev.parent;
1329 +
1330 + host->dma_dev = dev;
1331 + host->ones_dma = dma_map_single(dev, ones,
1332 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1333 + host->data_dma = dma_map_single(dev, host->data,
1334 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1335 +
1336 + /* REVISIT in theory those map operations can fail... */
1337 +
1338 + dma_sync_single_for_cpu(host->dma_dev,
1339 + host->data_dma, sizeof(*host->data),
1340 + DMA_BIDIRECTIONAL);
1341 + }
1342 +#endif
1343 +
1344 + /* setup message for status/busy readback */
1345 + spi_message_init(&host->readback);
1346 + host->readback.is_dma_mapped = (host->dma_dev != NULL);
1347 +
1348 + spi_message_add_tail(&host->status, &host->readback);
1349 + host->status.tx_buf = host->ones;
1350 + host->status.tx_dma = host->ones_dma;
1351 + host->status.rx_buf = &host->data->status;
1352 + host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1353 + host->status.cs_change = 1;
1354 +
1355 + /* register card detect irq */
1356 + if (host->pdata && host->pdata->init) {
1357 + status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1358 + if (status != 0)
1359 + goto fail_glue_init;
1360 + }
1361 +
1362 + status = mmc_add_host(mmc);
1363 + if (status != 0)
1364 + goto fail_add_host;
1365 +
1366 + dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
1367 + mmc->class_dev.bus_id,
1368 + host->dma_dev ? "" : ", no DMA",
1369 + (host->pdata && host->pdata->get_ro)
1370 + ? "" : ", no WP",
1371 + (host->pdata && host->pdata->setpower)
1372 + ? "" : ", no poweroff");
1373 + return 0;
1374 +
1375 +fail_add_host:
1376 + mmc_remove_host (mmc);
1377 +fail_glue_init:
1378 + if (host->dma_dev)
1379 + dma_unmap_single(host->dma_dev, host->data_dma,
1380 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1381 + kfree(host->data);
1382 +
1383 +fail_nobuf1:
1384 + mmc_free_host(mmc);
1385 + dev_set_drvdata(&spi->dev, NULL);
1386 +
1387 +nomem:
1388 + kfree(ones);
1389 + return status;
1390 +}
1391 +
1392 +
1393 +static int __devexit mmc_spi_remove(struct spi_device *spi)
1394 +{
1395 + struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1396 + struct mmc_spi_host *host;
1397 +
1398 + if (mmc) {
1399 + host = mmc_priv(mmc);
1400 +
1401 + /* prevent new mmc_detect_change() calls */
1402 + if (host->pdata && host->pdata->exit)
1403 + host->pdata->exit(&spi->dev, mmc);
1404 +
1405 + mmc_remove_host(mmc);
1406 +
1407 + if (host->dma_dev) {
1408 + dma_unmap_single(host->dma_dev, host->ones_dma,
1409 + MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1410 + dma_unmap_single(host->dma_dev, host->data_dma,
1411 + sizeof(*host->data), DMA_BIDIRECTIONAL);
1412 + }
1413 +
1414 + kfree(host->data);
1415 + kfree(host->ones);
1416 +
1417 + spi->max_speed_hz = mmc->f_max;
1418 + mmc_free_host(mmc);
1419 + dev_set_drvdata(&spi->dev, NULL);
1420 + }
1421 + return 0;
1422 +}
1423 +
1424 +
1425 +static struct spi_driver mmc_spi_driver = {
1426 + .driver = {
1427 + .name = "mmc_spi",
1428 + .bus = &spi_bus_type,
1429 + .owner = THIS_MODULE,
1430 + },
1431 + .probe = mmc_spi_probe,
1432 + .remove = __devexit_p(mmc_spi_remove),
1433 +};
1434 +
1435 +
1436 +static int __init mmc_spi_init(void)
1437 +{
1438 + return spi_register_driver(&mmc_spi_driver);
1439 +}
1440 +module_init(mmc_spi_init);
1441 +
1442 +
1443 +static void __exit mmc_spi_exit(void)
1444 +{
1445 + spi_unregister_driver(&mmc_spi_driver);
1446 +}
1447 +module_exit(mmc_spi_exit);
1448 +
1449 +
1450 +MODULE_AUTHOR("Mike Lavender, David Brownell, "
1451 + "Hans-Peter Nilsson, Jan Nikitenko");
1452 +MODULE_DESCRIPTION("SPI SD/MMC host driver");
1453 +MODULE_LICENSE("GPL");
1454 --- /dev/null
1455 +++ b/include/linux/spi/mmc_spi.h
1456 @@ -0,0 +1,33 @@
1457 +#ifndef __LINUX_SPI_MMC_SPI_H
1458 +#define __LINUX_SPI_MMC_SPI_H
1459 +
1460 +struct device;
1461 +struct mmc_host;
1462 +
1463 +/* Put this in platform_data of a device being used to manage an MMC/SD
1464 + * card slot. (Modeled after PXA mmc glue; see that for usage examples.)
1465 + *
1466 + * REVISIT This is not a spi-specific notion. Any card slot should be
1467 + * able to handle it. If the MMC core doesn't adopt this kind of notion,
1468 + * switch the "struct device *" parameters over to "struct spi_device *".
1469 + */
1470 +struct mmc_spi_platform_data {
1471 + /* driver activation and (optional) card detect irq hookup */
1472 + int (*init)(struct device *,
1473 + irqreturn_t (*)(int, void *),
1474 + void *);
1475 + void (*exit)(struct device *, void *);
1476 +
1477 + /* sense switch on sd cards */
1478 + int (*get_ro)(struct device *);
1479 +
1480 + /* how long to debounce card detect, in msecs */
1481 + u16 detect_delay;
1482 +
1483 + /* power management */
1484 + u16 powerup_msecs; /* delay of up to 250 msec */
1485 + u32 ocr_mask; /* available voltages */
1486 + void (*setpower)(struct device *, unsigned int maskval);
1487 +};
1488 +
1489 +#endif /* __LINUX_SPI_MMC_SPI_H */
1490 --- a/drivers/mmc/core/bus.c
1491 +++ b/drivers/mmc/core/bus.c
1492 @@ -19,6 +19,7 @@
1493
1494 #include "sysfs.h"
1495 #include "core.h"
1496 +#include "sdio_cis.h"
1497 #include "bus.h"
1498
1499 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
1500 @@ -34,6 +35,8 @@
1501 return sprintf(buf, "MMC\n");
1502 case MMC_TYPE_SD:
1503 return sprintf(buf, "SD\n");
1504 + case MMC_TYPE_SDIO:
1505 + return sprintf(buf, "SDIO\n");
1506 default:
1507 return -EFAULT;
1508 }
1509 @@ -55,36 +58,37 @@
1510 }
1511
1512 static int
1513 -mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
1514 - int buf_size)
1515 +mmc_bus_uevent(struct device *dev, char **envp,
1516 + int num_envp, char *buffer, int buffer_size)
1517 {
1518 struct mmc_card *card = dev_to_mmc_card(dev);
1519 - int retval = 0, i = 0, length = 0;
1520 -
1521 -#define add_env(fmt,val) do { \
1522 - retval = add_uevent_var(envp, num_envp, &i, \
1523 - buf, buf_size, &length, \
1524 - fmt, val); \
1525 - if (retval) \
1526 - return retval; \
1527 -} while (0);
1528 + const char *type;
1529 + int retval = 0;
1530 + int i = 0, len = 0;
1531
1532 switch (card->type) {
1533 case MMC_TYPE_MMC:
1534 - add_env("MMC_TYPE=%s", "MMC");
1535 + type = "MMC";
1536 break;
1537 case MMC_TYPE_SD:
1538 - add_env("MMC_TYPE=%s", "SD");
1539 + type = "SD";
1540 break;
1541 + case MMC_TYPE_SDIO:
1542 + type = "SDIO";
1543 + break;
1544 + default:
1545 + type = NULL;
1546 }
1547
1548 - add_env("MMC_NAME=%s", mmc_card_name(card));
1549 -
1550 -#undef add_env
1551 + if (type) {
1552 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_TYPE=%s", type);
1553 + if (retval)
1554 + return retval;
1555 + }
1556
1557 - envp[i] = NULL;
1558 + retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_NAME=%s", mmc_card_name(card));
1559
1560 - return 0;
1561 + return retval;
1562 }
1563
1564 static int mmc_bus_probe(struct device *dev)
1565 @@ -176,6 +180,11 @@
1566 {
1567 struct mmc_card *card = dev_to_mmc_card(dev);
1568
1569 + sdio_free_common_cis(card);
1570 +
1571 + if (card->info)
1572 + kfree(card->info);
1573 +
1574 kfree(card);
1575 }
1576
1577 @@ -221,15 +230,25 @@
1578 if (mmc_card_blockaddr(card))
1579 type = "SDHC";
1580 break;
1581 + case MMC_TYPE_SDIO:
1582 + type = "SDIO";
1583 + break;
1584 default:
1585 type = "?";
1586 break;
1587 }
1588
1589 - printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1590 - mmc_hostname(card->host),
1591 - mmc_card_highspeed(card) ? "high speed " : "",
1592 - type, card->rca);
1593 + if (mmc_host_is_spi(card->host)) {
1594 + printk(KERN_INFO "%s: new %s%s card on SPI\n",
1595 + mmc_hostname(card->host),
1596 + mmc_card_highspeed(card) ? "high speed " : "",
1597 + type);
1598 + } else {
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 + }
1604
1605 card->dev.uevent_suppress = 1;
1606
1607 @@ -261,8 +280,13 @@
1608 void mmc_remove_card(struct mmc_card *card)
1609 {
1610 if (mmc_card_present(card)) {
1611 - printk(KERN_INFO "%s: card %04x removed\n",
1612 - mmc_hostname(card->host), card->rca);
1613 + if (mmc_host_is_spi(card->host)) {
1614 + printk(KERN_INFO "%s: SPI card removed\n",
1615 + mmc_hostname(card->host));
1616 + } else {
1617 + printk(KERN_INFO "%s: card %04x removed\n",
1618 + mmc_hostname(card->host), card->rca);
1619 + }
1620
1621 if (card->host->bus_ops->sysfs_remove)
1622 card->host->bus_ops->sysfs_remove(card->host, card);
1623 --- a/drivers/mmc/core/core.c
1624 +++ b/drivers/mmc/core/core.c
1625 @@ -18,7 +18,7 @@
1626 #include <linux/delay.h>
1627 #include <linux/pagemap.h>
1628 #include <linux/err.h>
1629 -#include <asm/scatterlist.h>
1630 +#include <linux/leds.h>
1631 #include <linux/scatterlist.h>
1632
1633 #include <linux/mmc/card.h>
1634 @@ -29,16 +29,27 @@
1635 #include "core.h"
1636 #include "bus.h"
1637 #include "host.h"
1638 +#include "sdio_bus.h"
1639
1640 #include "mmc_ops.h"
1641 #include "sd_ops.h"
1642 +#include "sdio_ops.h"
1643
1644 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
1645 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
1646 +extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1647
1648 static struct workqueue_struct *workqueue;
1649
1650 /*
1651 + * Enabling software CRCs on the data blocks can be a significant (30%)
1652 + * performance cost, and for other reasons may not always be desired.
1653 + * So we allow it it to be disabled.
1654 + */
1655 +int use_spi_crc = 1;
1656 +module_param(use_spi_crc, bool, 0);
1657 +
1658 +/*
1659 * Internal function. Schedule delayed work in the MMC work queue.
1660 */
1661 static int mmc_schedule_delayed_work(struct delayed_work *work,
1662 @@ -68,6 +79,11 @@
1663 struct mmc_command *cmd = mrq->cmd;
1664 int err = cmd->error;
1665
1666 + if (err && cmd->retries && mmc_host_is_spi(host)) {
1667 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
1668 + cmd->retries = 0;
1669 + }
1670 +
1671 if (err && cmd->retries) {
1672 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
1673 mmc_hostname(host), cmd->opcode, err);
1674 @@ -76,6 +92,8 @@
1675 cmd->error = 0;
1676 host->ops->request(host, mrq);
1677 } else {
1678 + led_trigger_event(host->led, LED_OFF);
1679 +
1680 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
1681 mmc_hostname(host), cmd->opcode, err,
1682 cmd->resp[0], cmd->resp[1],
1683 @@ -118,7 +136,7 @@
1684 "tsac %d ms nsac %d\n",
1685 mmc_hostname(host), mrq->data->blksz,
1686 mrq->data->blocks, mrq->data->flags,
1687 - mrq->data->timeout_ns / 10000000,
1688 + mrq->data->timeout_ns / 1000000,
1689 mrq->data->timeout_clks);
1690 }
1691
1692 @@ -130,6 +148,8 @@
1693
1694 WARN_ON(!host->claimed);
1695
1696 + led_trigger_event(host->led, LED_FULL);
1697 +
1698 mrq->cmd->error = 0;
1699 mrq->cmd->mrq = mrq;
1700 if (mrq->data) {
1701 @@ -199,7 +219,7 @@
1702 {
1703 struct mmc_request mrq;
1704
1705 - BUG_ON(!host->claimed);
1706 + WARN_ON(!host->claimed);
1707
1708 memset(&mrq, 0, sizeof(struct mmc_request));
1709
1710 @@ -220,17 +240,24 @@
1711 * mmc_set_data_timeout - set the timeout for a data command
1712 * @data: data phase for command
1713 * @card: the MMC card associated with the data transfer
1714 - * @write: flag to differentiate reads from writes
1715 *
1716 * Computes the data timeout parameters according to the
1717 * correct algorithm given the card type.
1718 */
1719 -void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
1720 - int write)
1721 +void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
1722 {
1723 unsigned int mult;
1724
1725 /*
1726 + * SDIO cards only define an upper 1 s limit on access.
1727 + */
1728 + if (mmc_card_sdio(card)) {
1729 + data->timeout_ns = 1000000000;
1730 + data->timeout_clks = 0;
1731 + return;
1732 + }
1733 +
1734 + /*
1735 * SD cards use a 100 multiplier rather than 10
1736 */
1737 mult = mmc_card_sd(card) ? 100 : 10;
1738 @@ -239,7 +266,7 @@
1739 * Scale up the multiplier (and therefore the timeout) by
1740 * the r2w factor for writes.
1741 */
1742 - if (write)
1743 + if (data->flags & MMC_DATA_WRITE)
1744 mult <<= card->csd.r2w_factor;
1745
1746 data->timeout_ns = card->csd.tacc_ns * mult;
1747 @@ -255,7 +282,7 @@
1748 timeout_us += data->timeout_clks * 1000 /
1749 (card->host->ios.clock / 1000);
1750
1751 - if (write)
1752 + if (data->flags & MMC_DATA_WRITE)
1753 limit_us = 250000;
1754 else
1755 limit_us = 100000;
1756 @@ -272,15 +299,20 @@
1757 EXPORT_SYMBOL(mmc_set_data_timeout);
1758
1759 /**
1760 - * mmc_claim_host - exclusively claim a host
1761 + * __mmc_claim_host - exclusively claim a host
1762 * @host: mmc host to claim
1763 + * @abort: whether or not the operation should be aborted
1764 *
1765 - * Claim a host for a set of operations.
1766 + * Claim a host for a set of operations. If @abort is non null and
1767 + * dereference a non-zero value then this will return prematurely with
1768 + * that non-zero value without acquiring the lock. Returns zero
1769 + * with the lock held otherwise.
1770 */
1771 -void mmc_claim_host(struct mmc_host *host)
1772 +int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1773 {
1774 DECLARE_WAITQUEUE(wait, current);
1775 unsigned long flags;
1776 + int stop;
1777
1778 might_sleep();
1779
1780 @@ -288,19 +320,24 @@
1781 spin_lock_irqsave(&host->lock, flags);
1782 while (1) {
1783 set_current_state(TASK_UNINTERRUPTIBLE);
1784 - if (!host->claimed)
1785 + stop = abort ? atomic_read(abort) : 0;
1786 + if (stop || !host->claimed)
1787 break;
1788 spin_unlock_irqrestore(&host->lock, flags);
1789 schedule();
1790 spin_lock_irqsave(&host->lock, flags);
1791 }
1792 set_current_state(TASK_RUNNING);
1793 - host->claimed = 1;
1794 + if (!stop)
1795 + host->claimed = 1;
1796 + else
1797 + wake_up(&host->wq);
1798 spin_unlock_irqrestore(&host->lock, flags);
1799 remove_wait_queue(&host->wq, &wait);
1800 + return stop;
1801 }
1802
1803 -EXPORT_SYMBOL(mmc_claim_host);
1804 +EXPORT_SYMBOL(__mmc_claim_host);
1805
1806 /**
1807 * mmc_release_host - release a host
1808 @@ -313,7 +350,7 @@
1809 {
1810 unsigned long flags;
1811
1812 - BUG_ON(!host->claimed);
1813 + WARN_ON(!host->claimed);
1814
1815 spin_lock_irqsave(&host->lock, flags);
1816 host->claimed = 0;
1817 @@ -433,19 +470,32 @@
1818 int bit = fls(host->ocr_avail) - 1;
1819
1820 host->ios.vdd = bit;
1821 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1822 - host->ios.chip_select = MMC_CS_DONTCARE;
1823 + if (mmc_host_is_spi(host)) {
1824 + host->ios.chip_select = MMC_CS_HIGH;
1825 + host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1826 + } else {
1827 + host->ios.chip_select = MMC_CS_DONTCARE;
1828 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1829 + }
1830 host->ios.power_mode = MMC_POWER_UP;
1831 host->ios.bus_width = MMC_BUS_WIDTH_1;
1832 host->ios.timing = MMC_TIMING_LEGACY;
1833 mmc_set_ios(host);
1834
1835 - mmc_delay(1);
1836 + /*
1837 + * This delay should be sufficient to allow the power supply
1838 + * to reach the minimum voltage.
1839 + */
1840 + mmc_delay(2);
1841
1842 host->ios.clock = host->f_min;
1843 host->ios.power_mode = MMC_POWER_ON;
1844 mmc_set_ios(host);
1845
1846 + /*
1847 + * This delay must be at least 74 clock sizes, or 1 ms, or the
1848 + * time required to reach a stable voltage.
1849 + */
1850 mmc_delay(2);
1851 }
1852
1853 @@ -453,8 +503,10 @@
1854 {
1855 host->ios.clock = 0;
1856 host->ios.vdd = 0;
1857 - host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1858 - host->ios.chip_select = MMC_CS_DONTCARE;
1859 + if (!mmc_host_is_spi(host)) {
1860 + host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1861 + host->ios.chip_select = MMC_CS_DONTCARE;
1862 + }
1863 host->ios.power_mode = MMC_POWER_OFF;
1864 host->ios.bus_width = MMC_BUS_WIDTH_1;
1865 host->ios.timing = MMC_TIMING_LEGACY;
1866 @@ -511,7 +563,7 @@
1867 BUG_ON(!host);
1868 BUG_ON(!ops);
1869
1870 - BUG_ON(!host->claimed);
1871 + WARN_ON(!host->claimed);
1872
1873 spin_lock_irqsave(&host->lock, flags);
1874
1875 @@ -535,8 +587,8 @@
1876
1877 BUG_ON(!host);
1878
1879 - BUG_ON(!host->claimed);
1880 - BUG_ON(!host->bus_ops);
1881 + WARN_ON(!host->claimed);
1882 + WARN_ON(!host->bus_ops);
1883
1884 spin_lock_irqsave(&host->lock, flags);
1885
1886 @@ -564,7 +616,7 @@
1887 #ifdef CONFIG_MMC_DEBUG
1888 unsigned long flags;
1889 spin_lock_irqsave(&host->lock, flags);
1890 - BUG_ON(host->removed);
1891 + WARN_ON(host->removed);
1892 spin_unlock_irqrestore(&host->lock, flags);
1893 #endif
1894
1895 @@ -597,24 +649,38 @@
1896
1897 mmc_send_if_cond(host, host->ocr_avail);
1898
1899 + /*
1900 + * First we search for SDIO...
1901 + */
1902 + err = mmc_send_io_op_cond(host, 0, &ocr);
1903 + if (!err) {
1904 + if (mmc_attach_sdio(host, ocr))
1905 + mmc_power_off(host);
1906 + return;
1907 + }
1908 +
1909 + /*
1910 + * ...then normal SD...
1911 + */
1912 err = mmc_send_app_op_cond(host, 0, &ocr);
1913 - if (err == MMC_ERR_NONE) {
1914 + if (!err) {
1915 if (mmc_attach_sd(host, ocr))
1916 mmc_power_off(host);
1917 - } else {
1918 - /*
1919 - * If we fail to detect any SD cards then try
1920 - * searching for MMC cards.
1921 - */
1922 - err = mmc_send_op_cond(host, 0, &ocr);
1923 - if (err == MMC_ERR_NONE) {
1924 - if (mmc_attach_mmc(host, ocr))
1925 - mmc_power_off(host);
1926 - } else {
1927 + return;
1928 + }
1929 +
1930 + /*
1931 + * ...and finally MMC.
1932 + */
1933 + err = mmc_send_op_cond(host, 0, &ocr);
1934 + if (!err) {
1935 + if (mmc_attach_mmc(host, ocr))
1936 mmc_power_off(host);
1937 - mmc_release_host(host);
1938 - }
1939 + return;
1940 }
1941 +
1942 + mmc_release_host(host);
1943 + mmc_power_off(host);
1944 } else {
1945 if (host->bus_ops->detect && !host->bus_dead)
1946 host->bus_ops->detect(host);
1947 @@ -725,22 +791,38 @@
1948 return -ENOMEM;
1949
1950 ret = mmc_register_bus();
1951 - if (ret == 0) {
1952 - ret = mmc_register_host_class();
1953 - if (ret)
1954 - mmc_unregister_bus();
1955 - }
1956 + if (ret)
1957 + goto destroy_workqueue;
1958 +
1959 + ret = mmc_register_host_class();
1960 + if (ret)
1961 + goto unregister_bus;
1962 +
1963 + ret = sdio_register_bus();
1964 + if (ret)
1965 + goto unregister_host_class;
1966 +
1967 + return 0;
1968 +
1969 +unregister_host_class:
1970 + mmc_unregister_host_class();
1971 +unregister_bus:
1972 + mmc_unregister_bus();
1973 +destroy_workqueue:
1974 + destroy_workqueue(workqueue);
1975 +
1976 return ret;
1977 }
1978
1979 static void __exit mmc_exit(void)
1980 {
1981 + sdio_unregister_bus();
1982 mmc_unregister_host_class();
1983 mmc_unregister_bus();
1984 destroy_workqueue(workqueue);
1985 }
1986
1987 -module_init(mmc_init);
1988 +subsys_initcall(mmc_init);
1989 module_exit(mmc_exit);
1990
1991 MODULE_LICENSE("GPL");
1992 --- a/drivers/mmc/core/core.h
1993 +++ b/drivers/mmc/core/core.h
1994 @@ -48,5 +48,7 @@
1995 void mmc_start_host(struct mmc_host *host);
1996 void mmc_stop_host(struct mmc_host *host);
1997
1998 +extern int use_spi_crc;
1999 +
2000 #endif
2001
2002 --- a/drivers/mmc/core/host.c
2003 +++ b/drivers/mmc/core/host.c
2004 @@ -15,6 +15,7 @@
2005 #include <linux/err.h>
2006 #include <linux/idr.h>
2007 #include <linux/pagemap.h>
2008 +#include <linux/leds.h>
2009
2010 #include <linux/mmc/host.h>
2011
2012 @@ -100,6 +101,9 @@
2013 {
2014 int err;
2015
2016 + WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
2017 + !host->ops->enable_sdio_irq);
2018 +
2019 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
2020 return -ENOMEM;
2021
2022 @@ -112,6 +116,8 @@
2023 snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
2024 "mmc%d", host->index);
2025
2026 + led_trigger_register_simple(host->class_dev.bus_id, &host->led);
2027 +
2028 err = device_add(&host->class_dev);
2029 if (err)
2030 return err;
2031 @@ -137,6 +143,8 @@
2032
2033 device_del(&host->class_dev);
2034
2035 + led_trigger_unregister_simple(host->led);
2036 +
2037 spin_lock(&mmc_host_lock);
2038 idr_remove(&mmc_host_idr, host->index);
2039 spin_unlock(&mmc_host_lock);
2040 --- a/drivers/mmc/core/mmc.c
2041 +++ b/drivers/mmc/core/mmc.c
2042 @@ -161,13 +161,12 @@
2043 {
2044 int err;
2045 u8 *ext_csd;
2046 + unsigned int ext_csd_struct;
2047
2048 BUG_ON(!card);
2049
2050 - err = MMC_ERR_FAILED;
2051 -
2052 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
2053 - return MMC_ERR_NONE;
2054 + return 0;
2055
2056 /*
2057 * As the ext_csd is so large and mostly unused, we don't store the
2058 @@ -176,13 +175,19 @@
2059 ext_csd = kmalloc(512, GFP_KERNEL);
2060 if (!ext_csd) {
2061 printk(KERN_ERR "%s: could not allocate a buffer to "
2062 - "receive the ext_csd. mmc v4 cards will be "
2063 - "treated as v3.\n", mmc_hostname(card->host));
2064 - return MMC_ERR_FAILED;
2065 + "receive the ext_csd.\n", mmc_hostname(card->host));
2066 + return -ENOMEM;
2067 }
2068
2069 err = mmc_send_ext_csd(card, ext_csd);
2070 - if (err != MMC_ERR_NONE) {
2071 + if (err) {
2072 + /*
2073 + * We all hosts that cannot perform the command
2074 + * to fail more gracefully
2075 + */
2076 + if (err != -EINVAL)
2077 + goto out;
2078 +
2079 /*
2080 * High capacity cards should have this "magic" size
2081 * stored in their CSD.
2082 @@ -197,18 +202,30 @@
2083 "EXT_CSD, performance might "
2084 "suffer.\n",
2085 mmc_hostname(card->host));
2086 - err = MMC_ERR_NONE;
2087 + err = 0;
2088 }
2089 +
2090 goto out;
2091 }
2092
2093 - card->ext_csd.sectors =
2094 - ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2095 - ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2096 - ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2097 - ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2098 - if (card->ext_csd.sectors)
2099 - mmc_card_set_blockaddr(card);
2100 + ext_csd_struct = ext_csd[EXT_CSD_REV];
2101 + if (ext_csd_struct > 2) {
2102 + printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
2103 + "version %d\n", mmc_hostname(card->host),
2104 + ext_csd_struct);
2105 + err = -EINVAL;
2106 + goto out;
2107 + }
2108 +
2109 + if (ext_csd_struct >= 2) {
2110 + card->ext_csd.sectors =
2111 + ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2112 + ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2113 + ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2114 + ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2115 + if (card->ext_csd.sectors)
2116 + mmc_card_set_blockaddr(card);
2117 + }
2118
2119 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
2120 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
2121 @@ -246,7 +263,7 @@
2122 unsigned int max_dtr;
2123
2124 BUG_ON(!host);
2125 - BUG_ON(!host->claimed);
2126 + WARN_ON(!host->claimed);
2127
2128 /*
2129 * Since we're changing the OCR value, we seem to
2130 @@ -258,19 +275,33 @@
2131
2132 /* The extra bit indicates that we support high capacity */
2133 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
2134 - if (err != MMC_ERR_NONE)
2135 + if (err)
2136 goto err;
2137
2138 /*
2139 + * For SPI, enable CRC as appropriate.
2140 + */
2141 + if (mmc_host_is_spi(host)) {
2142 + err = mmc_spi_set_crc(host, use_spi_crc);
2143 + if (err)
2144 + goto err;
2145 + }
2146 +
2147 + /*
2148 * Fetch CID from card.
2149 */
2150 - err = mmc_all_send_cid(host, cid);
2151 - if (err != MMC_ERR_NONE)
2152 + if (mmc_host_is_spi(host))
2153 + err = mmc_send_cid(host, cid);
2154 + else
2155 + err = mmc_all_send_cid(host, cid);
2156 + if (err)
2157 goto err;
2158
2159 if (oldcard) {
2160 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2161 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2162 + err = -ENOENT;
2163 goto err;
2164 + }
2165
2166 card = oldcard;
2167 } else {
2168 @@ -278,8 +309,10 @@
2169 * Allocate card structure.
2170 */
2171 card = mmc_alloc_card(host);
2172 - if (IS_ERR(card))
2173 + if (IS_ERR(card)) {
2174 + err = PTR_ERR(card);
2175 goto err;
2176 + }
2177
2178 card->type = MMC_TYPE_MMC;
2179 card->rca = 1;
2180 @@ -287,43 +320,47 @@
2181 }
2182
2183 /*
2184 - * Set card RCA.
2185 + * For native busses: set card RCA and quit open drain mode.
2186 */
2187 - err = mmc_set_relative_addr(card);
2188 - if (err != MMC_ERR_NONE)
2189 - goto free_card;
2190 + if (!mmc_host_is_spi(host)) {
2191 + err = mmc_set_relative_addr(card);
2192 + if (err)
2193 + goto free_card;
2194
2195 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2196 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2197 + }
2198
2199 if (!oldcard) {
2200 /*
2201 * Fetch CSD from card.
2202 */
2203 err = mmc_send_csd(card, card->raw_csd);
2204 - if (err != MMC_ERR_NONE)
2205 + if (err)
2206 goto free_card;
2207
2208 err = mmc_decode_csd(card);
2209 - if (err < 0)
2210 + if (err)
2211 goto free_card;
2212 err = mmc_decode_cid(card);
2213 - if (err < 0)
2214 + if (err)
2215 goto free_card;
2216 }
2217
2218 /*
2219 * Select card, as all following commands rely on that.
2220 */
2221 - err = mmc_select_card(card);
2222 - if (err != MMC_ERR_NONE)
2223 - goto free_card;
2224 + if (!mmc_host_is_spi(host)) {
2225 + err = mmc_select_card(card);
2226 + if (err)
2227 + goto free_card;
2228 + }
2229
2230 if (!oldcard) {
2231 /*
2232 - * Fetch and process extened CSD.
2233 + * Fetch and process extended CSD.
2234 */
2235 err = mmc_read_ext_csd(card);
2236 - if (err != MMC_ERR_NONE)
2237 + if (err)
2238 goto free_card;
2239 }
2240
2241 @@ -334,7 +371,7 @@
2242 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
2243 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2244 EXT_CSD_HS_TIMING, 1);
2245 - if (err != MMC_ERR_NONE)
2246 + if (err)
2247 goto free_card;
2248
2249 mmc_card_set_highspeed(card);
2250 @@ -363,7 +400,7 @@
2251 (host->caps & MMC_CAP_4_BIT_DATA)) {
2252 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2253 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
2254 - if (err != MMC_ERR_NONE)
2255 + if (err)
2256 goto free_card;
2257
2258 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
2259 @@ -372,14 +409,14 @@
2260 if (!oldcard)
2261 host->card = card;
2262
2263 - return MMC_ERR_NONE;
2264 + return 0;
2265
2266 free_card:
2267 if (!oldcard)
2268 mmc_remove_card(card);
2269 err:
2270
2271 - return MMC_ERR_FAILED;
2272 + return err;
2273 }
2274
2275 /*
2276 @@ -413,7 +450,7 @@
2277
2278 mmc_release_host(host);
2279
2280 - if (err != MMC_ERR_NONE) {
2281 + if (err) {
2282 mmc_remove(host);
2283
2284 mmc_claim_host(host);
2285 @@ -480,7 +517,8 @@
2286 BUG_ON(!host->card);
2287
2288 mmc_claim_host(host);
2289 - mmc_deselect_cards(host);
2290 + if (!mmc_host_is_spi(host))
2291 + mmc_deselect_cards(host);
2292 host->card->state &= ~MMC_STATE_HIGHSPEED;
2293 mmc_release_host(host);
2294 }
2295 @@ -502,7 +540,7 @@
2296 err = mmc_init_card(host, host->ocr, host->card);
2297 mmc_release_host(host);
2298
2299 - if (err != MMC_ERR_NONE) {
2300 + if (err) {
2301 mmc_remove(host);
2302
2303 mmc_claim_host(host);
2304 @@ -536,11 +574,20 @@
2305 int err;
2306
2307 BUG_ON(!host);
2308 - BUG_ON(!host->claimed);
2309 + WARN_ON(!host->claimed);
2310
2311 mmc_attach_bus(host, &mmc_ops);
2312
2313 /*
2314 + * We need to get OCR a different way for SPI.
2315 + */
2316 + if (mmc_host_is_spi(host)) {
2317 + err = mmc_spi_read_ocr(host, 1, &ocr);
2318 + if (err)
2319 + goto err;
2320 + }
2321 +
2322 + /*
2323 * Sanity check the voltages that the card claims to
2324 * support.
2325 */
2326 @@ -565,7 +612,7 @@
2327 * Detect and init the card.
2328 */
2329 err = mmc_init_card(host, host->ocr, NULL);
2330 - if (err != MMC_ERR_NONE)
2331 + if (err)
2332 goto err;
2333
2334 mmc_release_host(host);
2335 @@ -587,6 +634,6 @@
2336 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
2337 mmc_hostname(host), err);
2338
2339 - return 0;
2340 + return err;
2341 }
2342
2343 --- a/drivers/mmc/core/mmc_ops.c
2344 +++ b/drivers/mmc/core/mmc_ops.c
2345 @@ -10,7 +10,6 @@
2346 */
2347
2348 #include <linux/types.h>
2349 -#include <asm/scatterlist.h>
2350 #include <linux/scatterlist.h>
2351
2352 #include <linux/mmc/host.h>
2353 @@ -40,10 +39,10 @@
2354 }
2355
2356 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2357 - if (err != MMC_ERR_NONE)
2358 + if (err)
2359 return err;
2360
2361 - return MMC_ERR_NONE;
2362 + return 0;
2363 }
2364
2365 int mmc_select_card(struct mmc_card *card)
2366 @@ -63,23 +62,36 @@
2367 int err;
2368 struct mmc_command cmd;
2369
2370 - mmc_set_chip_select(host, MMC_CS_HIGH);
2371 -
2372 - mmc_delay(1);
2373 + /*
2374 + * Non-SPI hosts need to prevent chipselect going active during
2375 + * GO_IDLE; that would put chips into SPI mode. Remind them of
2376 + * that in case of hardware that won't pull up DAT3/nCS otherwise.
2377 + *
2378 + * SPI hosts ignore ios.chip_select; it's managed according to
2379 + * rules that must accomodate non-MMC slaves which this layer
2380 + * won't even know about.
2381 + */
2382 + if (!mmc_host_is_spi(host)) {
2383 + mmc_set_chip_select(host, MMC_CS_HIGH);
2384 + mmc_delay(1);
2385 + }
2386
2387 memset(&cmd, 0, sizeof(struct mmc_command));
2388
2389 cmd.opcode = MMC_GO_IDLE_STATE;
2390 cmd.arg = 0;
2391 - cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
2392 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
2393
2394 err = mmc_wait_for_cmd(host, &cmd, 0);
2395
2396 mmc_delay(1);
2397
2398 - mmc_set_chip_select(host, MMC_CS_DONTCARE);
2399 + if (!mmc_host_is_spi(host)) {
2400 + mmc_set_chip_select(host, MMC_CS_DONTCARE);
2401 + mmc_delay(1);
2402 + }
2403
2404 - mmc_delay(1);
2405 + host->use_spi_crc = 0;
2406
2407 return err;
2408 }
2409 @@ -94,23 +106,33 @@
2410 memset(&cmd, 0, sizeof(struct mmc_command));
2411
2412 cmd.opcode = MMC_SEND_OP_COND;
2413 - cmd.arg = ocr;
2414 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
2415 + cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
2416 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
2417
2418 for (i = 100; i; i--) {
2419 err = mmc_wait_for_cmd(host, &cmd, 0);
2420 - if (err != MMC_ERR_NONE)
2421 + if (err)
2422 break;
2423
2424 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
2425 + /* if we're just probing, do a single pass */
2426 + if (ocr == 0)
2427 break;
2428
2429 - err = MMC_ERR_TIMEOUT;
2430 + /* otherwise wait until reset completes */
2431 + if (mmc_host_is_spi(host)) {
2432 + if (!(cmd.resp[0] & R1_SPI_IDLE))
2433 + break;
2434 + } else {
2435 + if (cmd.resp[0] & MMC_CARD_BUSY)
2436 + break;
2437 + }
2438 +
2439 + err = -ETIMEDOUT;
2440
2441 mmc_delay(10);
2442 }
2443
2444 - if (rocr)
2445 + if (rocr && !mmc_host_is_spi(host))
2446 *rocr = cmd.resp[0];
2447
2448 return err;
2449 @@ -131,12 +153,12 @@
2450 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
2451
2452 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2453 - if (err != MMC_ERR_NONE)
2454 + if (err)
2455 return err;
2456
2457 memcpy(cid, cmd.resp, sizeof(u32) * 4);
2458
2459 - return MMC_ERR_NONE;
2460 + return 0;
2461 }
2462
2463 int mmc_set_relative_addr(struct mmc_card *card)
2464 @@ -154,46 +176,52 @@
2465 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2466
2467 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2468 - if (err != MMC_ERR_NONE)
2469 + if (err)
2470 return err;
2471
2472 - return MMC_ERR_NONE;
2473 + return 0;
2474 }
2475
2476 -int mmc_send_csd(struct mmc_card *card, u32 *csd)
2477 +static int
2478 +mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
2479 {
2480 int err;
2481 struct mmc_command cmd;
2482
2483 - BUG_ON(!card);
2484 - BUG_ON(!card->host);
2485 - BUG_ON(!csd);
2486 + BUG_ON(!host);
2487 + BUG_ON(!cxd);
2488
2489 memset(&cmd, 0, sizeof(struct mmc_command));
2490
2491 - cmd.opcode = MMC_SEND_CSD;
2492 - cmd.arg = card->rca << 16;
2493 + cmd.opcode = opcode;
2494 + cmd.arg = arg;
2495 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
2496
2497 - err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2498 - if (err != MMC_ERR_NONE)
2499 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2500 + if (err)
2501 return err;
2502
2503 - memcpy(csd, cmd.resp, sizeof(u32) * 4);
2504 + memcpy(cxd, cmd.resp, sizeof(u32) * 4);
2505
2506 - return MMC_ERR_NONE;
2507 + return 0;
2508 }
2509
2510 -int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2511 +static int
2512 +mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
2513 + u32 opcode, void *buf, unsigned len)
2514 {
2515 struct mmc_request mrq;
2516 struct mmc_command cmd;
2517 struct mmc_data data;
2518 struct scatterlist sg;
2519 + void *data_buf;
2520
2521 - BUG_ON(!card);
2522 - BUG_ON(!card->host);
2523 - BUG_ON(!ext_csd);
2524 + /* dma onto stack is unsafe/nonportable, but callers to this
2525 + * routine normally provide temporary on-stack buffers ...
2526 + */
2527 + data_buf = kmalloc(len, GFP_KERNEL);
2528 + if (data_buf == NULL)
2529 + return -ENOMEM;
2530
2531 memset(&mrq, 0, sizeof(struct mmc_request));
2532 memset(&cmd, 0, sizeof(struct mmc_command));
2533 @@ -202,28 +230,117 @@
2534 mrq.cmd = &cmd;
2535 mrq.data = &data;
2536
2537 - cmd.opcode = MMC_SEND_EXT_CSD;
2538 + cmd.opcode = opcode;
2539 cmd.arg = 0;
2540 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2541
2542 - data.blksz = 512;
2543 + /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
2544 + * rely on callers to never use this with "native" calls for reading
2545 + * CSD or CID. Native versions of those commands use the R2 type,
2546 + * not R1 plus a data block.
2547 + */
2548 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2549 +
2550 + data.blksz = len;
2551 data.blocks = 1;
2552 data.flags = MMC_DATA_READ;
2553 data.sg = &sg;
2554 data.sg_len = 1;
2555
2556 - sg_init_one(&sg, ext_csd, 512);
2557 + sg_init_one(&sg, data_buf, len);
2558 +
2559 + if (card)
2560 + mmc_set_data_timeout(&data, card);
2561
2562 - mmc_set_data_timeout(&data, card, 0);
2563 + mmc_wait_for_req(host, &mrq);
2564
2565 - mmc_wait_for_req(card->host, &mrq);
2566 + memcpy(buf, data_buf, len);
2567 + kfree(data_buf);
2568
2569 - if (cmd.error != MMC_ERR_NONE)
2570 + if (cmd.error)
2571 return cmd.error;
2572 - if (data.error != MMC_ERR_NONE)
2573 + if (data.error)
2574 return data.error;
2575
2576 - return MMC_ERR_NONE;
2577 + return 0;
2578 +}
2579 +
2580 +int mmc_send_csd(struct mmc_card *card, u32 *csd)
2581 +{
2582 + int ret, i;
2583 +
2584 + if (!mmc_host_is_spi(card->host))
2585 + return mmc_send_cxd_native(card->host, card->rca << 16,
2586 + csd, MMC_SEND_CSD);
2587 +
2588 + ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
2589 + if (ret)
2590 + return ret;
2591 +
2592 + for (i = 0;i < 4;i++)
2593 + csd[i] = be32_to_cpu(csd[i]);
2594 +
2595 + return 0;
2596 +}
2597 +
2598 +int mmc_send_cid(struct mmc_host *host, u32 *cid)
2599 +{
2600 + int ret, i;
2601 +
2602 + if (!mmc_host_is_spi(host)) {
2603 + if (!host->card)
2604 + return -EINVAL;
2605 + return mmc_send_cxd_native(host, host->card->rca << 16,
2606 + cid, MMC_SEND_CID);
2607 + }
2608 +
2609 + ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
2610 + if (ret)
2611 + return ret;
2612 +
2613 + for (i = 0;i < 4;i++)
2614 + cid[i] = be32_to_cpu(cid[i]);
2615 +
2616 + return 0;
2617 +}
2618 +
2619 +int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2620 +{
2621 + return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
2622 + ext_csd, 512);
2623 +}
2624 +
2625 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
2626 +{
2627 + struct mmc_command cmd;
2628 + int err;
2629 +
2630 + memset(&cmd, 0, sizeof(struct mmc_command));
2631 +
2632 + cmd.opcode = MMC_SPI_READ_OCR;
2633 + cmd.arg = highcap ? (1 << 30) : 0;
2634 + cmd.flags = MMC_RSP_SPI_R3;
2635 +
2636 + err = mmc_wait_for_cmd(host, &cmd, 0);
2637 +
2638 + *ocrp = cmd.resp[1];
2639 + return err;
2640 +}
2641 +
2642 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
2643 +{
2644 + struct mmc_command cmd;
2645 + int err;
2646 +
2647 + memset(&cmd, 0, sizeof(struct mmc_command));
2648 +
2649 + cmd.opcode = MMC_SPI_CRC_ON_OFF;
2650 + cmd.flags = MMC_RSP_SPI_R1;
2651 + cmd.arg = use_crc;
2652 +
2653 + err = mmc_wait_for_cmd(host, &cmd, 0);
2654 + if (!err)
2655 + host->use_spi_crc = use_crc;
2656 + return err;
2657 }
2658
2659 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
2660 @@ -241,13 +358,13 @@
2661 (index << 16) |
2662 (value << 8) |
2663 set;
2664 - cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
2665 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2666
2667 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2668 - if (err != MMC_ERR_NONE)
2669 + if (err)
2670 return err;
2671
2672 - return MMC_ERR_NONE;
2673 + return 0;
2674 }
2675
2676 int mmc_send_status(struct mmc_card *card, u32 *status)
2677 @@ -261,16 +378,20 @@
2678 memset(&cmd, 0, sizeof(struct mmc_command));
2679
2680 cmd.opcode = MMC_SEND_STATUS;
2681 - cmd.arg = card->rca << 16;
2682 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2683 + if (!mmc_host_is_spi(card->host))
2684 + cmd.arg = card->rca << 16;
2685 + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
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 + /* NOTE: callers are required to understand the difference
2693 + * between "native" and SPI format status words!
2694 + */
2695 if (status)
2696 *status = cmd.resp[0];
2697
2698 - return MMC_ERR_NONE;
2699 + return 0;
2700 }
2701
2702 --- a/drivers/mmc/core/mmc_ops.h
2703 +++ b/drivers/mmc/core/mmc_ops.h
2704 @@ -22,6 +22,9 @@
2705 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
2706 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
2707 int mmc_send_status(struct mmc_card *card, u32 *status);
2708 +int mmc_send_cid(struct mmc_host *host, u32 *cid);
2709 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
2710 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
2711
2712 #endif
2713
2714 --- a/drivers/mmc/core/sd.c
2715 +++ b/drivers/mmc/core/sd.c
2716 @@ -166,8 +166,6 @@
2717 unsigned int scr_struct;
2718 u32 resp[4];
2719
2720 - BUG_ON(!mmc_card_sd(card));
2721 -
2722 resp[3] = card->raw_scr[1];
2723 resp[2] = card->raw_scr[0];
2724
2725 @@ -193,30 +191,38 @@
2726 u8 *status;
2727
2728 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2729 - return MMC_ERR_NONE;
2730 + return 0;
2731
2732 if (!(card->csd.cmdclass & CCC_SWITCH)) {
2733 printk(KERN_WARNING "%s: card lacks mandatory switch "
2734 "function, performance might suffer.\n",
2735 mmc_hostname(card->host));
2736 - return MMC_ERR_NONE;
2737 + return 0;
2738 }
2739
2740 - err = MMC_ERR_FAILED;
2741 + err = -EIO;
2742
2743 status = kmalloc(64, GFP_KERNEL);
2744 if (!status) {
2745 printk(KERN_ERR "%s: could not allocate a buffer for "
2746 "switch capabilities.\n", mmc_hostname(card->host));
2747 - return err;
2748 + return -ENOMEM;
2749 }
2750
2751 err = mmc_sd_switch(card, 0, 0, 1, status);
2752 - if (err != MMC_ERR_NONE) {
2753 + if (err) {
2754 + /*
2755 + * We all hosts that cannot perform the command
2756 + * to fail more gracefully
2757 + */
2758 + if (err != -EINVAL)
2759 + goto out;
2760 +
2761 printk(KERN_WARNING "%s: problem reading switch "
2762 "capabilities, performance might suffer.\n",
2763 mmc_hostname(card->host));
2764 - err = MMC_ERR_NONE;
2765 + err = 0;
2766 +
2767 goto out;
2768 }
2769
2770 @@ -238,28 +244,28 @@
2771 u8 *status;
2772
2773 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2774 - return MMC_ERR_NONE;
2775 + return 0;
2776
2777 if (!(card->csd.cmdclass & CCC_SWITCH))
2778 - return MMC_ERR_NONE;
2779 + return 0;
2780
2781 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
2782 - return MMC_ERR_NONE;
2783 + return 0;
2784
2785 if (card->sw_caps.hs_max_dtr == 0)
2786 - return MMC_ERR_NONE;
2787 + return 0;
2788
2789 - err = MMC_ERR_FAILED;
2790 + err = -EIO;
2791
2792 status = kmalloc(64, GFP_KERNEL);
2793 if (!status) {
2794 printk(KERN_ERR "%s: could not allocate a buffer for "
2795 "switch capabilities.\n", mmc_hostname(card->host));
2796 - return err;
2797 + return -ENOMEM;
2798 }
2799
2800 err = mmc_sd_switch(card, 1, 0, 1, status);
2801 - if (err != MMC_ERR_NONE)
2802 + if (err)
2803 goto out;
2804
2805 if ((status[16] & 0xF) != 1) {
2806 @@ -292,7 +298,7 @@
2807 unsigned int max_dtr;
2808
2809 BUG_ON(!host);
2810 - BUG_ON(!host->claimed);
2811 + WARN_ON(!host->claimed);
2812
2813 /*
2814 * Since we're changing the OCR value, we seem to
2815 @@ -309,23 +315,37 @@
2816 * block-addressed SDHC cards.
2817 */
2818 err = mmc_send_if_cond(host, ocr);
2819 - if (err == MMC_ERR_NONE)
2820 + if (!err)
2821 ocr |= 1 << 30;
2822
2823 err = mmc_send_app_op_cond(host, ocr, NULL);
2824 - if (err != MMC_ERR_NONE)
2825 + if (err)
2826 goto err;
2827
2828 /*
2829 + * For SPI, enable CRC as appropriate.
2830 + */
2831 + if (mmc_host_is_spi(host)) {
2832 + err = mmc_spi_set_crc(host, use_spi_crc);
2833 + if (err)
2834 + goto err;
2835 + }
2836 +
2837 + /*
2838 * Fetch CID from card.
2839 */
2840 - err = mmc_all_send_cid(host, cid);
2841 - if (err != MMC_ERR_NONE)
2842 + if (mmc_host_is_spi(host))
2843 + err = mmc_send_cid(host, cid);
2844 + else
2845 + err = mmc_all_send_cid(host, cid);
2846 + if (err)
2847 goto err;
2848
2849 if (oldcard) {
2850 - if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2851 + if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2852 + err = -ENOENT;
2853 goto err;
2854 + }
2855
2856 card = oldcard;
2857 } else {
2858 @@ -333,32 +353,36 @@
2859 * Allocate card structure.
2860 */
2861 card = mmc_alloc_card(host);
2862 - if (IS_ERR(card))
2863 + if (IS_ERR(card)) {
2864 + err = PTR_ERR(card);
2865 goto err;
2866 + }
2867
2868 card->type = MMC_TYPE_SD;
2869 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
2870 }
2871
2872 /*
2873 - * Set card RCA.
2874 + * For native busses: get card RCA and quit open drain mode.
2875 */
2876 - err = mmc_send_relative_addr(host, &card->rca);
2877 - if (err != MMC_ERR_NONE)
2878 - goto free_card;
2879 + if (!mmc_host_is_spi(host)) {
2880 + err = mmc_send_relative_addr(host, &card->rca);
2881 + if (err)
2882 + goto free_card;
2883
2884 - mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2885 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2886 + }
2887
2888 if (!oldcard) {
2889 /*
2890 * Fetch CSD from card.
2891 */
2892 err = mmc_send_csd(card, card->raw_csd);
2893 - if (err != MMC_ERR_NONE)
2894 + if (err)
2895 goto free_card;
2896
2897 err = mmc_decode_csd(card);
2898 - if (err < 0)
2899 + if (err)
2900 goto free_card;
2901
2902 mmc_decode_cid(card);
2903 @@ -367,16 +391,18 @@
2904 /*
2905 * Select card, as all following commands rely on that.
2906 */
2907 - err = mmc_select_card(card);
2908 - if (err != MMC_ERR_NONE)
2909 - goto free_card;
2910 + if (!mmc_host_is_spi(host)) {
2911 + err = mmc_select_card(card);
2912 + if (err)
2913 + goto free_card;
2914 + }
2915
2916 if (!oldcard) {
2917 /*
2918 * Fetch SCR from card.
2919 */
2920 err = mmc_app_send_scr(card, card->raw_scr);
2921 - if (err != MMC_ERR_NONE)
2922 + if (err)
2923 goto free_card;
2924
2925 err = mmc_decode_scr(card);
2926 @@ -387,7 +413,7 @@
2927 * Fetch switch information from card.
2928 */
2929 err = mmc_read_switch(card);
2930 - if (err != MMC_ERR_NONE)
2931 + if (err)
2932 goto free_card;
2933 }
2934
2935 @@ -395,7 +421,7 @@
2936 * Attempt to change to high-speed (if supported)
2937 */
2938 err = mmc_switch_hs(card);
2939 - if (err != MMC_ERR_NONE)
2940 + if (err)
2941 goto free_card;
2942
2943 /*
2944 @@ -418,7 +444,7 @@
2945 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
2946 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
2947 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
2948 - if (err != MMC_ERR_NONE)
2949 + if (err)
2950 goto free_card;
2951
2952 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
2953 @@ -442,14 +468,14 @@
2954 if (!oldcard)
2955 host->card = card;
2956
2957 - return MMC_ERR_NONE;
2958 + return 0;
2959
2960 free_card:
2961 if (!oldcard)
2962 mmc_remove_card(card);
2963 err:
2964
2965 - return MMC_ERR_FAILED;
2966 + return err;
2967 }
2968
2969 /*
2970 @@ -483,7 +509,7 @@
2971
2972 mmc_release_host(host);
2973
2974 - if (err != MMC_ERR_NONE) {
2975 + if (err) {
2976 mmc_sd_remove(host);
2977
2978 mmc_claim_host(host);
2979 @@ -552,7 +578,8 @@
2980 BUG_ON(!host->card);
2981
2982 mmc_claim_host(host);
2983 - mmc_deselect_cards(host);
2984 + if (!mmc_host_is_spi(host))
2985 + mmc_deselect_cards(host);
2986 host->card->state &= ~MMC_STATE_HIGHSPEED;
2987 mmc_release_host(host);
2988 }
2989 @@ -574,7 +601,7 @@
2990 err = mmc_sd_init_card(host, host->ocr, host->card);
2991 mmc_release_host(host);
2992
2993 - if (err != MMC_ERR_NONE) {
2994 + if (err) {
2995 mmc_sd_remove(host);
2996
2997 mmc_claim_host(host);
2998 @@ -608,11 +635,22 @@
2999 int err;
3000
3001 BUG_ON(!host);
3002 - BUG_ON(!host->claimed);
3003 + WARN_ON(!host->claimed);
3004
3005 mmc_attach_bus(host, &mmc_sd_ops);
3006
3007 /*
3008 + * We need to get OCR a different way for SPI.
3009 + */
3010 + if (mmc_host_is_spi(host)) {
3011 + mmc_go_idle(host);
3012 +
3013 + err = mmc_spi_read_ocr(host, 0, &ocr);
3014 + if (err)
3015 + goto err;
3016 + }
3017 +
3018 + /*
3019 * Sanity check the voltages that the card claims to
3020 * support.
3021 */
3022 @@ -644,7 +682,7 @@
3023 * Detect and init the card.
3024 */
3025 err = mmc_sd_init_card(host, host->ocr, NULL);
3026 - if (err != MMC_ERR_NONE)
3027 + if (err)
3028 goto err;
3029
3030 mmc_release_host(host);
3031 @@ -666,6 +704,6 @@
3032 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
3033 mmc_hostname(host), err);
3034
3035 - return 0;
3036 + return err;
3037 }
3038
3039 --- a/drivers/mmc/core/sd_ops.c
3040 +++ b/drivers/mmc/core/sd_ops.c
3041 @@ -10,7 +10,6 @@
3042 */
3043
3044 #include <linux/types.h>
3045 -#include <asm/scatterlist.h>
3046 #include <linux/scatterlist.h>
3047
3048 #include <linux/mmc/host.h>
3049 @@ -33,21 +32,21 @@
3050
3051 if (card) {
3052 cmd.arg = card->rca << 16;
3053 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3054 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
3055 } else {
3056 cmd.arg = 0;
3057 - cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
3058 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
3059 }
3060
3061 err = mmc_wait_for_cmd(host, &cmd, 0);
3062 - if (err != MMC_ERR_NONE)
3063 + if (err)
3064 return err;
3065
3066 /* Check that card supported application commands */
3067 - if (!(cmd.resp[0] & R1_APP_CMD))
3068 - return MMC_ERR_FAILED;
3069 + if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
3070 + return -EOPNOTSUPP;
3071
3072 - return MMC_ERR_NONE;
3073 + return 0;
3074 }
3075
3076 /**
3077 @@ -73,7 +72,7 @@
3078 BUG_ON(!cmd);
3079 BUG_ON(retries < 0);
3080
3081 - err = MMC_ERR_INVALID;
3082 + err = -EIO;
3083
3084 /*
3085 * We have to resend MMC_APP_CMD for each attempt so
3086 @@ -83,8 +82,14 @@
3087 memset(&mrq, 0, sizeof(struct mmc_request));
3088
3089 err = mmc_app_cmd(host, card);
3090 - if (err != MMC_ERR_NONE)
3091 + if (err) {
3092 + /* no point in retrying; no APP commands allowed */
3093 + if (mmc_host_is_spi(host)) {
3094 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3095 + break;
3096 + }
3097 continue;
3098 + }
3099
3100 memset(&mrq, 0, sizeof(struct mmc_request));
3101
3102 @@ -97,8 +102,14 @@
3103 mmc_wait_for_req(host, &mrq);
3104
3105 err = cmd->error;
3106 - if (cmd->error == MMC_ERR_NONE)
3107 + if (!cmd->error)
3108 break;
3109 +
3110 + /* no point in retrying illegal APP commands */
3111 + if (mmc_host_is_spi(host)) {
3112 + if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3113 + break;
3114 + }
3115 }
3116
3117 return err;
3118 @@ -127,14 +138,14 @@
3119 cmd.arg = SD_BUS_WIDTH_4;
3120 break;
3121 default:
3122 - return MMC_ERR_INVALID;
3123 + return -EINVAL;
3124 }
3125
3126 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
3127 - if (err != MMC_ERR_NONE)
3128 + if (err)
3129 return err;
3130
3131 - return MMC_ERR_NONE;
3132 + return 0;
3133 }
3134
3135 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
3136 @@ -147,23 +158,36 @@
3137 memset(&cmd, 0, sizeof(struct mmc_command));
3138
3139 cmd.opcode = SD_APP_OP_COND;
3140 - cmd.arg = ocr;
3141 - cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
3142 + if (mmc_host_is_spi(host))
3143 + cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
3144 + else
3145 + cmd.arg = ocr;
3146 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
3147
3148 for (i = 100; i; i--) {
3149 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
3150 - if (err != MMC_ERR_NONE)
3151 + if (err)
3152 break;
3153
3154 - if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
3155 + /* if we're just probing, do a single pass */
3156 + if (ocr == 0)
3157 break;
3158
3159 - err = MMC_ERR_TIMEOUT;
3160 + /* otherwise wait until reset completes */
3161 + if (mmc_host_is_spi(host)) {
3162 + if (!(cmd.resp[0] & R1_SPI_IDLE))
3163 + break;
3164 + } else {
3165 + if (cmd.resp[0] & MMC_CARD_BUSY)
3166 + break;
3167 + }
3168 +
3169 + err = -ETIMEDOUT;
3170
3171 mmc_delay(10);
3172 }
3173
3174 - if (rocr)
3175 + if (rocr && !mmc_host_is_spi(host))
3176 *rocr = cmd.resp[0];
3177
3178 return err;
3179 @@ -174,6 +198,7 @@
3180 struct mmc_command cmd;
3181 int err;
3182 static const u8 test_pattern = 0xAA;
3183 + u8 result_pattern;
3184
3185 /*
3186 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
3187 @@ -182,16 +207,21 @@
3188 */
3189 cmd.opcode = SD_SEND_IF_COND;
3190 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
3191 - cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
3192 + cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
3193
3194 err = mmc_wait_for_cmd(host, &cmd, 0);
3195 - if (err != MMC_ERR_NONE)
3196 + if (err)
3197 return err;
3198
3199 - if ((cmd.resp[0] & 0xFF) != test_pattern)
3200 - return MMC_ERR_FAILED;
3201 + if (mmc_host_is_spi(host))
3202 + result_pattern = cmd.resp[1] & 0xFF;
3203 + else
3204 + result_pattern = cmd.resp[0] & 0xFF;
3205 +
3206 + if (result_pattern != test_pattern)
3207 + return -EIO;
3208
3209 - return MMC_ERR_NONE;
3210 + return 0;
3211 }
3212
3213 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
3214 @@ -209,12 +239,12 @@
3215 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
3216
3217 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
3218 - if (err != MMC_ERR_NONE)
3219 + if (err)
3220 return err;
3221
3222 *rca = cmd.resp[0] >> 16;
3223
3224 - return MMC_ERR_NONE;
3225 + return 0;
3226 }
3227
3228 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
3229 @@ -229,8 +259,10 @@
3230 BUG_ON(!card->host);
3231 BUG_ON(!scr);
3232
3233 + /* NOTE: caller guarantees scr is heap-allocated */
3234 +
3235 err = mmc_app_cmd(card->host, card);
3236 - if (err != MMC_ERR_NONE)
3237 + if (err)
3238 return err;
3239
3240 memset(&mrq, 0, sizeof(struct mmc_request));
3241 @@ -242,7 +274,7 @@
3242
3243 cmd.opcode = SD_APP_SEND_SCR;
3244 cmd.arg = 0;
3245 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3246 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3247
3248 data.blksz = 8;
3249 data.blocks = 1;
3250 @@ -252,19 +284,19 @@
3251
3252 sg_init_one(&sg, scr, 8);
3253
3254 - mmc_set_data_timeout(&data, card, 0);
3255 + mmc_set_data_timeout(&data, card);
3256
3257 mmc_wait_for_req(card->host, &mrq);
3258
3259 - if (cmd.error != MMC_ERR_NONE)
3260 + if (cmd.error)
3261 return cmd.error;
3262 - if (data.error != MMC_ERR_NONE)
3263 + if (data.error)
3264 return data.error;
3265
3266 - scr[0] = ntohl(scr[0]);
3267 - scr[1] = ntohl(scr[1]);
3268 + scr[0] = be32_to_cpu(scr[0]);
3269 + scr[1] = be32_to_cpu(scr[1]);
3270
3271 - return MMC_ERR_NONE;
3272 + return 0;
3273 }
3274
3275 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
3276 @@ -278,6 +310,8 @@
3277 BUG_ON(!card);
3278 BUG_ON(!card->host);
3279
3280 + /* NOTE: caller guarantees resp is heap-allocated */
3281 +
3282 mode = !!mode;
3283 value &= 0xF;
3284
3285 @@ -292,7 +326,7 @@
3286 cmd.arg = mode << 31 | 0x00FFFFFF;
3287 cmd.arg &= ~(0xF << (group * 4));
3288 cmd.arg |= value << (group * 4);
3289 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3290 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3291
3292 data.blksz = 64;
3293 data.blocks = 1;
3294 @@ -302,15 +336,15 @@
3295
3296 sg_init_one(&sg, resp, 64);
3297
3298 - mmc_set_data_timeout(&data, card, 0);
3299 + mmc_set_data_timeout(&data, card);
3300
3301 mmc_wait_for_req(card->host, &mrq);
3302
3303 - if (cmd.error != MMC_ERR_NONE)
3304 + if (cmd.error)
3305 return cmd.error;
3306 - if (data.error != MMC_ERR_NONE)
3307 + if (data.error)
3308 return data.error;
3309
3310 - return MMC_ERR_NONE;
3311 + return 0;
3312 }
3313
3314 --- /dev/null
3315 +++ b/drivers/mmc/core/sdio.c
3316 @@ -0,0 +1,395 @@
3317 +/*
3318 + * linux/drivers/mmc/sdio.c
3319 + *
3320 + * Copyright 2006-2007 Pierre Ossman
3321 + *
3322 + * This program is free software; you can redistribute it and/or modify
3323 + * it under the terms of the GNU General Public License as published by
3324 + * the Free Software Foundation; either version 2 of the License, or (at
3325 + * your option) any later version.
3326 + */
3327 +
3328 +#include <linux/err.h>
3329 +
3330 +#include <linux/mmc/host.h>
3331 +#include <linux/mmc/card.h>
3332 +#include <linux/mmc/sdio.h>
3333 +#include <linux/mmc/sdio_func.h>
3334 +
3335 +#include "core.h"
3336 +#include "bus.h"
3337 +#include "sdio_bus.h"
3338 +#include "mmc_ops.h"
3339 +#include "sd_ops.h"
3340 +#include "sdio_ops.h"
3341 +#include "sdio_cis.h"
3342 +
3343 +static int sdio_read_fbr(struct sdio_func *func)
3344 +{
3345 + int ret;
3346 + unsigned char data;
3347 +
3348 + ret = mmc_io_rw_direct(func->card, 0, 0,
3349 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
3350 + if (ret)
3351 + goto out;
3352 +
3353 + data &= 0x0f;
3354 +
3355 + if (data == 0x0f) {
3356 + ret = mmc_io_rw_direct(func->card, 0, 0,
3357 + SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
3358 + if (ret)
3359 + goto out;
3360 + }
3361 +
3362 + func->class = data;
3363 +
3364 +out:
3365 + return ret;
3366 +}
3367 +
3368 +static int sdio_init_func(struct mmc_card *card, unsigned int fn)
3369 +{
3370 + int ret;
3371 + struct sdio_func *func;
3372 +
3373 + BUG_ON(fn > SDIO_MAX_FUNCS);
3374 +
3375 + func = sdio_alloc_func(card);
3376 + if (IS_ERR(func))
3377 + return PTR_ERR(func);
3378 +
3379 + func->num = fn;
3380 +
3381 + ret = sdio_read_fbr(func);
3382 + if (ret)
3383 + goto fail;
3384 +
3385 + ret = sdio_read_func_cis(func);
3386 + if (ret)
3387 + goto fail;
3388 +
3389 + card->sdio_func[fn - 1] = func;
3390 +
3391 + return 0;
3392 +
3393 +fail:
3394 + /*
3395 + * It is okay to remove the function here even though we hold
3396 + * the host lock as we haven't registered the device yet.
3397 + */
3398 + sdio_remove_func(func);
3399 + return ret;
3400 +}
3401 +
3402 +static int sdio_read_cccr(struct mmc_card *card)
3403 +{
3404 + int ret;
3405 + int cccr_vsn;
3406 + unsigned char data;
3407 +
3408 + memset(&card->cccr, 0, sizeof(struct sdio_cccr));
3409 +
3410 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
3411 + if (ret)
3412 + goto out;
3413 +
3414 + cccr_vsn = data & 0x0f;
3415 +
3416 + if (cccr_vsn > SDIO_CCCR_REV_1_20) {
3417 + printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
3418 + mmc_hostname(card->host), cccr_vsn);
3419 + return -EINVAL;
3420 + }
3421 +
3422 + card->cccr.sdio_vsn = (data & 0xf0) >> 4;
3423 +
3424 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
3425 + if (ret)
3426 + goto out;
3427 +
3428 + if (data & SDIO_CCCR_CAP_SMB)
3429 + card->cccr.multi_block = 1;
3430 + if (data & SDIO_CCCR_CAP_LSC)
3431 + card->cccr.low_speed = 1;
3432 + if (data & SDIO_CCCR_CAP_4BLS)
3433 + card->cccr.wide_bus = 1;
3434 +
3435 + if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
3436 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
3437 + if (ret)
3438 + goto out;
3439 +
3440 + if (data & SDIO_POWER_SMPC)
3441 + card->cccr.high_power = 1;
3442 + }
3443 +
3444 + if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
3445 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
3446 + if (ret)
3447 + goto out;
3448 +
3449 + if (data & SDIO_SPEED_SHS)
3450 + card->cccr.high_speed = 1;
3451 + }
3452 +
3453 +out:
3454 + return ret;
3455 +}
3456 +
3457 +static int sdio_enable_wide(struct mmc_card *card)
3458 +{
3459 + int ret;
3460 + u8 ctrl;
3461 +
3462 + if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
3463 + return 0;
3464 +
3465 + if (card->cccr.low_speed && !card->cccr.wide_bus)
3466 + return 0;
3467 +
3468 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
3469 + if (ret)
3470 + return ret;
3471 +
3472 + ctrl |= SDIO_BUS_WIDTH_4BIT;
3473 +
3474 + ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
3475 + if (ret)
3476 + return ret;
3477 +
3478 + mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
3479 +
3480 + return 0;
3481 +}
3482 +
3483 +/*
3484 + * Host is being removed. Free up the current card.
3485 + */
3486 +static void mmc_sdio_remove(struct mmc_host *host)
3487 +{
3488 + int i;
3489 +
3490 + BUG_ON(!host);
3491 + BUG_ON(!host->card);
3492 +
3493 + for (i = 0;i < host->card->sdio_funcs;i++) {
3494 + if (host->card->sdio_func[i]) {
3495 + sdio_remove_func(host->card->sdio_func[i]);
3496 + host->card->sdio_func[i] = NULL;
3497 + }
3498 + }
3499 +
3500 + mmc_remove_card(host->card);
3501 + host->card = NULL;
3502 +}
3503 +
3504 +/*
3505 + * Card detection callback from host.
3506 + */
3507 +static void mmc_sdio_detect(struct mmc_host *host)
3508 +{
3509 + int err;
3510 +
3511 + BUG_ON(!host);
3512 + BUG_ON(!host->card);
3513 +
3514 + mmc_claim_host(host);
3515 +
3516 + /*
3517 + * Just check if our card has been removed.
3518 + */
3519 + err = mmc_select_card(host->card);
3520 +
3521 + mmc_release_host(host);
3522 +
3523 + if (err) {
3524 + mmc_sdio_remove(host);
3525 +
3526 + mmc_claim_host(host);
3527 + mmc_detach_bus(host);
3528 + mmc_release_host(host);
3529 + }
3530 +}
3531 +
3532 +
3533 +static const struct mmc_bus_ops mmc_sdio_ops = {
3534 + .remove = mmc_sdio_remove,
3535 + .detect = mmc_sdio_detect,
3536 +};
3537 +
3538 +
3539 +/*
3540 + * Starting point for SDIO card init.
3541 + */
3542 +int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
3543 +{
3544 + int err;
3545 + int i, funcs;
3546 + struct mmc_card *card;
3547 +
3548 + BUG_ON(!host);
3549 + WARN_ON(!host->claimed);
3550 +
3551 + mmc_attach_bus(host, &mmc_sdio_ops);
3552 +
3553 + /*
3554 + * Sanity check the voltages that the card claims to
3555 + * support.
3556 + */
3557 + if (ocr & 0x7F) {
3558 + printk(KERN_WARNING "%s: card claims to support voltages "
3559 + "below the defined range. These will be ignored.\n",
3560 + mmc_hostname(host));
3561 + ocr &= ~0x7F;
3562 + }
3563 +
3564 + if (ocr & MMC_VDD_165_195) {
3565 + printk(KERN_WARNING "%s: SDIO card claims to support the "
3566 + "incompletely defined 'low voltage range'. This "
3567 + "will be ignored.\n", mmc_hostname(host));
3568 + ocr &= ~MMC_VDD_165_195;
3569 + }
3570 +
3571 + host->ocr = mmc_select_voltage(host, ocr);
3572 +
3573 + /*
3574 + * Can we support the voltage(s) of the card(s)?
3575 + */
3576 + if (!host->ocr) {
3577 + err = -EINVAL;
3578 + goto err;
3579 + }
3580 +
3581 + /*
3582 + * Inform the card of the voltage
3583 + */
3584 + err = mmc_send_io_op_cond(host, host->ocr, &ocr);
3585 + if (err)
3586 + goto err;
3587 +
3588 + /*
3589 + * For SPI, enable CRC as appropriate.
3590 + */
3591 + if (mmc_host_is_spi(host)) {
3592 + err = mmc_spi_set_crc(host, use_spi_crc);
3593 + if (err)
3594 + goto err;
3595 + }
3596 +
3597 + /*
3598 + * The number of functions on the card is encoded inside
3599 + * the ocr.
3600 + */
3601 + funcs = (ocr & 0x70000000) >> 28;
3602 +
3603 + /*
3604 + * Allocate card structure.
3605 + */
3606 + card = mmc_alloc_card(host);
3607 + if (IS_ERR(card)) {
3608 + err = PTR_ERR(card);
3609 + goto err;
3610 + }
3611 +
3612 + card->type = MMC_TYPE_SDIO;
3613 + card->sdio_funcs = funcs;
3614 +
3615 + host->card = card;
3616 +
3617 + /*
3618 + * For native busses: set card RCA and quit open drain mode.
3619 + */
3620 + if (!mmc_host_is_spi(host)) {
3621 + err = mmc_send_relative_addr(host, &card->rca);
3622 + if (err)
3623 + goto remove;
3624 +
3625 + mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
3626 + }
3627 +
3628 + /*
3629 + * Select card, as all following commands rely on that.
3630 + */
3631 + if (!mmc_host_is_spi(host)) {
3632 + err = mmc_select_card(card);
3633 + if (err)
3634 + goto remove;
3635 + }
3636 +
3637 + /*
3638 + * Read the common registers.
3639 + */
3640 + err = sdio_read_cccr(card);
3641 + if (err)
3642 + goto remove;
3643 +
3644 + /*
3645 + * Read the common CIS tuples.
3646 + */
3647 + err = sdio_read_common_cis(card);
3648 + if (err)
3649 + goto remove;
3650 +
3651 + /*
3652 + * No support for high-speed yet, so just set
3653 + * the card's maximum speed.
3654 + */
3655 + mmc_set_clock(host, card->cis.max_dtr);
3656 +
3657 + /*
3658 + * Switch to wider bus (if supported).
3659 + */
3660 + err = sdio_enable_wide(card);
3661 + if (err)
3662 + goto remove;
3663 +
3664 + /*
3665 + * Initialize (but don't add) all present functions.
3666 + */
3667 + for (i = 0;i < funcs;i++) {
3668 + err = sdio_init_func(host->card, i + 1);
3669 + if (err)
3670 + goto remove;
3671 + }
3672 +
3673 + mmc_release_host(host);
3674 +
3675 + /*
3676 + * First add the card to the driver model...
3677 + */
3678 + err = mmc_add_card(host->card);
3679 + if (err)
3680 + goto remove_added;
3681 +
3682 + /*
3683 + * ...then the SDIO functions.
3684 + */
3685 + for (i = 0;i < funcs;i++) {
3686 + err = sdio_add_func(host->card->sdio_func[i]);
3687 + if (err)
3688 + goto remove_added;
3689 + }
3690 +
3691 + return 0;
3692 +
3693 +
3694 +remove_added:
3695 + /* Remove without lock if the device has been added. */
3696 + mmc_sdio_remove(host);
3697 + mmc_claim_host(host);
3698 +remove:
3699 + /* And with lock if it hasn't been added. */
3700 + if (host->card)
3701 + mmc_sdio_remove(host);
3702 +err:
3703 + mmc_detach_bus(host);
3704 + mmc_release_host(host);
3705 +
3706 + printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
3707 + mmc_hostname(host), err);
3708 +
3709 + return err;
3710 +}
3711 +
3712 --- /dev/null
3713 +++ b/drivers/mmc/core/sdio_bus.c
3714 @@ -0,0 +1,265 @@
3715 +/*
3716 + * linux/drivers/mmc/core/sdio_bus.c
3717 + *
3718 + * Copyright 2007 Pierre Ossman
3719 + *
3720 + * This program is free software; you can redistribute it and/or modify
3721 + * it under the terms of the GNU General Public License as published by
3722 + * the Free Software Foundation; either version 2 of the License, or (at
3723 + * your option) any later version.
3724 + *
3725 + * SDIO function driver model
3726 + */
3727 +
3728 +#include <linux/device.h>
3729 +#include <linux/err.h>
3730 +
3731 +#include <linux/mmc/card.h>
3732 +#include <linux/mmc/sdio_func.h>
3733 +
3734 +#include "sdio_cis.h"
3735 +#include "sdio_bus.h"
3736 +
3737 +#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
3738 +#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
3739 +
3740 +/* show configuration fields */
3741 +#define sdio_config_attr(field, format_string) \
3742 +static ssize_t \
3743 +field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
3744 +{ \
3745 + struct sdio_func *func; \
3746 + \
3747 + func = dev_to_sdio_func (dev); \
3748 + return sprintf (buf, format_string, func->field); \
3749 +}
3750 +
3751 +sdio_config_attr(class, "0x%02x\n");
3752 +sdio_config_attr(vendor, "0x%04x\n");
3753 +sdio_config_attr(device, "0x%04x\n");
3754 +
3755 +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3756 +{
3757 + struct sdio_func *func = dev_to_sdio_func (dev);
3758 +
3759 + return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
3760 + func->class, func->vendor, func->device);
3761 +}
3762 +
3763 +static struct device_attribute sdio_dev_attrs[] = {
3764 + __ATTR_RO(class),
3765 + __ATTR_RO(vendor),
3766 + __ATTR_RO(device),
3767 + __ATTR_RO(modalias),
3768 + __ATTR_NULL,
3769 +};
3770 +
3771 +static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
3772 + const struct sdio_device_id *id)
3773 +{
3774 + if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
3775 + return NULL;
3776 + if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
3777 + return NULL;
3778 + if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
3779 + return NULL;
3780 + return id;
3781 +}
3782 +
3783 +static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
3784 + struct sdio_driver *sdrv)
3785 +{
3786 + const struct sdio_device_id *ids;
3787 +
3788 + ids = sdrv->id_table;
3789 +
3790 + if (ids) {
3791 + while (ids->class || ids->vendor || ids->device) {
3792 + if (sdio_match_one(func, ids))
3793 + return ids;
3794 + ids++;
3795 + }
3796 + }
3797 +
3798 + return NULL;
3799 +}
3800 +
3801 +static int sdio_bus_match(struct device *dev, struct device_driver *drv)
3802 +{
3803 + struct sdio_func *func = dev_to_sdio_func(dev);
3804 + struct sdio_driver *sdrv = to_sdio_driver(drv);
3805 +
3806 + if (sdio_match_device(func, sdrv))
3807 + return 1;
3808 +
3809 + return 0;
3810 +}
3811 +
3812 +static int
3813 +sdio_bus_uevent(struct device *dev, char **envp,
3814 + int num_envp, char *buffer, int buffer_size)
3815 +{
3816 + struct sdio_func *func = dev_to_sdio_func(dev);
3817 + int i = 0, len = 0;
3818 +
3819 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3820 + "SDIO_CLASS=%02X", func->class))
3821 + return -ENOMEM;
3822 +
3823 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3824 + "SDIO_ID=%04X:%04X", func->vendor, func->device))
3825 + return -ENOMEM;
3826 +
3827 + if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3828 + "MODALIAS=sdio:c%02Xv%04Xd%04X",
3829 + func->class, func->vendor, func->device))
3830 + return -ENOMEM;
3831 +
3832 + return 0;
3833 +}
3834 +
3835 +static int sdio_bus_probe(struct device *dev)
3836 +{
3837 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3838 + struct sdio_func *func = dev_to_sdio_func(dev);
3839 + const struct sdio_device_id *id;
3840 + int ret;
3841 +
3842 + id = sdio_match_device(func, drv);
3843 + if (!id)
3844 + return -ENODEV;
3845 +
3846 + /* Set the default block size so the driver is sure it's something
3847 + * sensible. */
3848 + sdio_claim_host(func);
3849 + ret = sdio_set_block_size(func, 0);
3850 + sdio_release_host(func);
3851 + if (ret)
3852 + return ret;
3853 +
3854 + return drv->probe(func, id);
3855 +}
3856 +
3857 +static int sdio_bus_remove(struct device *dev)
3858 +{
3859 + struct sdio_driver *drv = to_sdio_driver(dev->driver);
3860 + struct sdio_func *func = dev_to_sdio_func(dev);
3861 +
3862 + drv->remove(func);
3863 +
3864 + if (func->irq_handler) {
3865 + printk(KERN_WARNING "WARNING: driver %s did not remove "
3866 + "its interrupt handler!\n", drv->name);
3867 + sdio_claim_host(func);
3868 + sdio_release_irq(func);
3869 + sdio_release_host(func);
3870 + }
3871 +
3872 + return 0;
3873 +}
3874 +
3875 +static struct bus_type sdio_bus_type = {
3876 + .name = "sdio",
3877 + .dev_attrs = sdio_dev_attrs,
3878 + .match = sdio_bus_match,
3879 + .uevent = sdio_bus_uevent,
3880 + .probe = sdio_bus_probe,
3881 + .remove = sdio_bus_remove,
3882 +};
3883 +
3884 +int sdio_register_bus(void)
3885 +{
3886 + return bus_register(&sdio_bus_type);
3887 +}
3888 +
3889 +void sdio_unregister_bus(void)
3890 +{
3891 + bus_unregister(&sdio_bus_type);
3892 +}
3893 +
3894 +/**
3895 + * sdio_register_driver - register a function driver
3896 + * @drv: SDIO function driver
3897 + */
3898 +int sdio_register_driver(struct sdio_driver *drv)
3899 +{
3900 + drv->drv.name = drv->name;
3901 + drv->drv.bus = &sdio_bus_type;
3902 + return driver_register(&drv->drv);
3903 +}
3904 +EXPORT_SYMBOL_GPL(sdio_register_driver);
3905 +
3906 +/**
3907 + * sdio_unregister_driver - unregister a function driver
3908 + * @drv: SDIO function driver
3909 + */
3910 +void sdio_unregister_driver(struct sdio_driver *drv)
3911 +{
3912 + drv->drv.bus = &sdio_bus_type;
3913 + driver_unregister(&drv->drv);
3914 +}
3915 +EXPORT_SYMBOL_GPL(sdio_unregister_driver);
3916 +
3917 +static void sdio_release_func(struct device *dev)
3918 +{
3919 + struct sdio_func *func = dev_to_sdio_func(dev);
3920 +
3921 + sdio_free_func_cis(func);
3922 +
3923 + if (func->info)
3924 + kfree(func->info);
3925 +
3926 + kfree(func);
3927 +}
3928 +
3929 +/*
3930 + * Allocate and initialise a new SDIO function structure.
3931 + */
3932 +struct sdio_func *sdio_alloc_func(struct mmc_card *card)
3933 +{
3934 + struct sdio_func *func;
3935 +
3936 + func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
3937 + if (!func)
3938 + return ERR_PTR(-ENOMEM);
3939 +
3940 + func->card = card;
3941 +
3942 + device_initialize(&func->dev);
3943 +
3944 + func->dev.parent = &card->dev;
3945 + func->dev.bus = &sdio_bus_type;
3946 + func->dev.release = sdio_release_func;
3947 +
3948 + return func;
3949 +}
3950 +
3951 +/*
3952 + * Register a new SDIO function with the driver model.
3953 + */
3954 +int sdio_add_func(struct sdio_func *func)
3955 +{
3956 + int ret;
3957 +
3958 + snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
3959 + "%s:%d", mmc_card_id(func->card), func->num);
3960 +
3961 + ret = device_add(&func->dev);
3962 + if (ret == 0)
3963 + sdio_func_set_present(func);
3964 +
3965 + return ret;
3966 +}
3967 +
3968 +/*
3969 + * Unregister a SDIO function with the driver model, and
3970 + * (eventually) free it.
3971 + */
3972 +void sdio_remove_func(struct sdio_func *func)
3973 +{
3974 + if (sdio_func_present(func))
3975 + device_del(&func->dev);
3976 +
3977 + put_device(&func->dev);
3978 +}
3979 +
3980 --- /dev/null
3981 +++ b/drivers/mmc/core/sdio_bus.h
3982 @@ -0,0 +1,22 @@
3983 +/*
3984 + * linux/drivers/mmc/core/sdio_bus.h
3985 + *
3986 + * Copyright 2007 Pierre Ossman
3987 + *
3988 + * This program is free software; you can redistribute it and/or modify
3989 + * it under the terms of the GNU General Public License as published by
3990 + * the Free Software Foundation; either version 2 of the License, or (at
3991 + * your option) any later version.
3992 + */
3993 +#ifndef _MMC_CORE_SDIO_BUS_H
3994 +#define _MMC_CORE_SDIO_BUS_H
3995 +
3996 +struct sdio_func *sdio_alloc_func(struct mmc_card *card);
3997 +int sdio_add_func(struct sdio_func *func);
3998 +void sdio_remove_func(struct sdio_func *func);
3999 +
4000 +int sdio_register_bus(void);
4001 +void sdio_unregister_bus(void);
4002 +
4003 +#endif
4004 +
4005 --- /dev/null
4006 +++ b/drivers/mmc/core/sdio_cis.c
4007 @@ -0,0 +1,346 @@
4008 +/*
4009 + * linux/drivers/mmc/core/sdio_cis.c
4010 + *
4011 + * Author: Nicolas Pitre
4012 + * Created: June 11, 2007
4013 + * Copyright: MontaVista Software Inc.
4014 + *
4015 + * Copyright 2007 Pierre Ossman
4016 + *
4017 + * This program is free software; you can redistribute it and/or modify
4018 + * it under the terms of the GNU General Public License as published by
4019 + * the Free Software Foundation; either version 2 of the License, or (at
4020 + * your option) any later version.
4021 + */
4022 +
4023 +#include <linux/kernel.h>
4024 +
4025 +#include <linux/mmc/host.h>
4026 +#include <linux/mmc/card.h>
4027 +#include <linux/mmc/sdio.h>
4028 +#include <linux/mmc/sdio_func.h>
4029 +
4030 +#include "sdio_cis.h"
4031 +#include "sdio_ops.h"
4032 +
4033 +static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
4034 + const unsigned char *buf, unsigned size)
4035 +{
4036 + unsigned i, nr_strings;
4037 + char **buffer, *string;
4038 +
4039 + buf += 2;
4040 + size -= 2;
4041 +
4042 + nr_strings = 0;
4043 + for (i = 0; i < size; i++) {
4044 + if (buf[i] == 0xff)
4045 + break;
4046 + if (buf[i] == 0)
4047 + nr_strings++;
4048 + }
4049 +
4050 + if (buf[i-1] != '\0') {
4051 + printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
4052 + return 0;
4053 + }
4054 +
4055 + size = i;
4056 +
4057 + buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
4058 + if (!buffer)
4059 + return -ENOMEM;
4060 +
4061 + string = (char*)(buffer + nr_strings);
4062 +
4063 + for (i = 0; i < nr_strings; i++) {
4064 + buffer[i] = string;
4065 + strcpy(string, buf);
4066 + string += strlen(string) + 1;
4067 + buf += strlen(buf) + 1;
4068 + }
4069 +
4070 + if (func) {
4071 + func->num_info = nr_strings;
4072 + func->info = (const char**)buffer;
4073 + } else {
4074 + card->num_info = nr_strings;
4075 + card->info = (const char**)buffer;
4076 + }
4077 +
4078 + return 0;
4079 +}
4080 +
4081 +static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
4082 + const unsigned char *buf, unsigned size)
4083 +{
4084 + unsigned int vendor, device;
4085 +
4086 + /* TPLMID_MANF */
4087 + vendor = buf[0] | (buf[1] << 8);
4088 +
4089 + /* TPLMID_CARD */
4090 + device = buf[2] | (buf[3] << 8);
4091 +
4092 + if (func) {
4093 + func->vendor = vendor;
4094 + func->device = device;
4095 + } else {
4096 + card->cis.vendor = vendor;
4097 + card->cis.device = device;
4098 + }
4099 +
4100 + return 0;
4101 +}
4102 +
4103 +static const unsigned char speed_val[16] =
4104 + { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
4105 +static const unsigned int speed_unit[8] =
4106 + { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
4107 +
4108 +static int cistpl_funce_common(struct mmc_card *card,
4109 + const unsigned char *buf, unsigned size)
4110 +{
4111 + if (size < 0x04 || buf[0] != 0)
4112 + return -EINVAL;
4113 +
4114 + /* TPLFE_FN0_BLK_SIZE */
4115 + card->cis.blksize = buf[1] | (buf[2] << 8);
4116 +
4117 + /* TPLFE_MAX_TRAN_SPEED */
4118 + card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
4119 + speed_unit[buf[3] & 7];
4120 +
4121 + return 0;
4122 +}
4123 +
4124 +static int cistpl_funce_func(struct sdio_func *func,
4125 + const unsigned char *buf, unsigned size)
4126 +{
4127 + unsigned vsn;
4128 + unsigned min_size;
4129 +
4130 + vsn = func->card->cccr.sdio_vsn;
4131 + min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
4132 +
4133 + if (size < min_size || buf[0] != 1)
4134 + return -EINVAL;
4135 +
4136 + /* TPLFE_MAX_BLK_SIZE */
4137 + func->max_blksize = buf[12] | (buf[13] << 8);
4138 +
4139 + return 0;
4140 +}
4141 +
4142 +static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
4143 + const unsigned char *buf, unsigned size)
4144 +{
4145 + int ret;
4146 +
4147 + /*
4148 + * There should be two versions of the CISTPL_FUNCE tuple,
4149 + * one for the common CIS (function 0) and a version used by
4150 + * the individual function's CIS (1-7). Yet, the later has a
4151 + * different length depending on the SDIO spec version.
4152 + */
4153 + if (func)
4154 + ret = cistpl_funce_func(func, buf, size);
4155 + else
4156 + ret = cistpl_funce_common(card, buf, size);
4157 +
4158 + if (ret) {
4159 + printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
4160 + "type %u\n", mmc_hostname(card->host), size, buf[0]);
4161 + return ret;
4162 + }
4163 +
4164 + return 0;
4165 +}
4166 +
4167 +typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
4168 + const unsigned char *, unsigned);
4169 +
4170 +struct cis_tpl {
4171 + unsigned char code;
4172 + unsigned char min_size;
4173 + tpl_parse_t *parse;
4174 +};
4175 +
4176 +static const struct cis_tpl cis_tpl_list[] = {
4177 + { 0x15, 3, cistpl_vers_1 },
4178 + { 0x20, 4, cistpl_manfid },
4179 + { 0x21, 2, /* cistpl_funcid */ },
4180 + { 0x22, 0, cistpl_funce },
4181 +};
4182 +
4183 +static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
4184 +{
4185 + int ret;
4186 + struct sdio_func_tuple *this, **prev;
4187 + unsigned i, ptr = 0;
4188 +
4189 + /*
4190 + * Note that this works for the common CIS (function number 0) as
4191 + * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
4192 + * have the same offset.
4193 + */
4194 + for (i = 0; i < 3; i++) {
4195 + unsigned char x, fn;
4196 +
4197 + if (func)
4198 + fn = func->num;
4199 + else
4200 + fn = 0;
4201 +
4202 + ret = mmc_io_rw_direct(card, 0, 0,
4203 + SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
4204 + if (ret)
4205 + return ret;
4206 + ptr |= x << (i * 8);
4207 + }
4208 +
4209 + if (func)
4210 + prev = &func->tuples;
4211 + else
4212 + prev = &card->tuples;
4213 +
4214 + BUG_ON(*prev);
4215 +
4216 + do {
4217 + unsigned char tpl_code, tpl_link;
4218 +
4219 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
4220 + if (ret)
4221 + break;
4222 +
4223 + /* 0xff means we're done */
4224 + if (tpl_code == 0xff)
4225 + break;
4226 +
4227 + ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
4228 + if (ret)
4229 + break;
4230 +
4231 + this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
4232 + if (!this)
4233 + return -ENOMEM;
4234 +
4235 + for (i = 0; i < tpl_link; i++) {
4236 + ret = mmc_io_rw_direct(card, 0, 0,
4237 + ptr + i, 0, &this->data[i]);
4238 + if (ret)
4239 + break;
4240 + }
4241 + if (ret) {
4242 + kfree(this);
4243 + break;
4244 + }
4245 +
4246 + for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
4247 + if (cis_tpl_list[i].code == tpl_code)
4248 + break;
4249 + if (i >= ARRAY_SIZE(cis_tpl_list)) {
4250 + /* this tuple is unknown to the core */
4251 + this->next = NULL;
4252 + this->code = tpl_code;
4253 + this->size = tpl_link;
4254 + *prev = this;
4255 + prev = &this->next;
4256 + printk(KERN_DEBUG
4257 + "%s: queuing CIS tuple 0x%02x length %u\n",
4258 + mmc_hostname(card->host), tpl_code, tpl_link);
4259 + } else {
4260 + const struct cis_tpl *tpl = cis_tpl_list + i;
4261 + if (tpl_link < tpl->min_size) {
4262 + printk(KERN_ERR
4263 + "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
4264 + mmc_hostname(card->host),
4265 + tpl_code, tpl_link, tpl->min_size);
4266 + ret = -EINVAL;
4267 + } else if (tpl->parse) {
4268 + ret = tpl->parse(card, func,
4269 + this->data, tpl_link);
4270 + }
4271 + kfree(this);
4272 + }
4273 +
4274 + ptr += tpl_link;
4275 + } while (!ret);
4276 +
4277 + /*
4278 + * Link in all unknown tuples found in the common CIS so that
4279 + * drivers don't have to go digging in two places.
4280 + */
4281 + if (func)
4282 + *prev = card->tuples;
4283 +
4284 + return ret;
4285 +}
4286 +
4287 +int sdio_read_common_cis(struct mmc_card *card)
4288 +{
4289 + return sdio_read_cis(card, NULL);
4290 +}
4291 +
4292 +void sdio_free_common_cis(struct mmc_card *card)
4293 +{
4294 + struct sdio_func_tuple *tuple, *victim;
4295 +
4296 + tuple = card->tuples;
4297 +
4298 + while (tuple) {
4299 + victim = tuple;
4300 + tuple = tuple->next;
4301 + kfree(victim);
4302 + }
4303 +
4304 + card->tuples = NULL;
4305 +}
4306 +
4307 +int sdio_read_func_cis(struct sdio_func *func)
4308 +{
4309 + int ret;
4310 +
4311 + ret = sdio_read_cis(func->card, func);
4312 + if (ret)
4313 + return ret;
4314 +
4315 + /*
4316 + * Since we've linked to tuples in the card structure,
4317 + * we must make sure we have a reference to it.
4318 + */
4319 + get_device(&func->card->dev);
4320 +
4321 + /*
4322 + * Vendor/device id is optional for function CIS, so
4323 + * copy it from the card structure as needed.
4324 + */
4325 + if (func->vendor == 0) {
4326 + func->vendor = func->card->cis.vendor;
4327 + func->device = func->card->cis.device;
4328 + }
4329 +
4330 + return 0;
4331 +}
4332 +
4333 +void sdio_free_func_cis(struct sdio_func *func)
4334 +{
4335 + struct sdio_func_tuple *tuple, *victim;
4336 +
4337 + tuple = func->tuples;
4338 +
4339 + while (tuple && tuple != func->card->tuples) {
4340 + victim = tuple;
4341 + tuple = tuple->next;
4342 + kfree(victim);
4343 + }
4344 +
4345 + func->tuples = NULL;
4346 +
4347 + /*
4348 + * We have now removed the link to the tuples in the
4349 + * card structure, so remove the reference.
4350 + */
4351 + put_device(&func->card->dev);
4352 +}
4353 +
4354 --- /dev/null
4355 +++ b/drivers/mmc/core/sdio_cis.h
4356 @@ -0,0 +1,23 @@
4357 +/*
4358 + * linux/drivers/mmc/core/sdio_cis.h
4359 + *
4360 + * Author: Nicolas Pitre
4361 + * Created: June 11, 2007
4362 + * Copyright: MontaVista Software Inc.
4363 + *
4364 + * This program is free software; you can redistribute it and/or modify
4365 + * it under the terms of the GNU General Public License as published by
4366 + * the Free Software Foundation; either version 2 of the License, or (at
4367 + * your option) any later version.
4368 + */
4369 +
4370 +#ifndef _MMC_SDIO_CIS_H
4371 +#define _MMC_SDIO_CIS_H
4372 +
4373 +int sdio_read_common_cis(struct mmc_card *card);
4374 +void sdio_free_common_cis(struct mmc_card *card);
4375 +
4376 +int sdio_read_func_cis(struct sdio_func *func);
4377 +void sdio_free_func_cis(struct sdio_func *func);
4378 +
4379 +#endif
4380 --- /dev/null
4381 +++ b/drivers/mmc/core/sdio_io.c
4382 @@ -0,0 +1,548 @@
4383 +/*
4384 + * linux/drivers/mmc/core/sdio_io.c
4385 + *
4386 + * Copyright 2007 Pierre Ossman
4387 + *
4388 + * This program is free software; you can redistribute it and/or modify
4389 + * it under the terms of the GNU General Public License as published by
4390 + * the Free Software Foundation; either version 2 of the License, or (at
4391 + * your option) any later version.
4392 + */
4393 +
4394 +#include <linux/mmc/host.h>
4395 +#include <linux/mmc/card.h>
4396 +#include <linux/mmc/sdio.h>
4397 +#include <linux/mmc/sdio_func.h>
4398 +
4399 +#include "sdio_ops.h"
4400 +
4401 +/**
4402 + * sdio_claim_host - exclusively claim a bus for a certain SDIO function
4403 + * @func: SDIO function that will be accessed
4404 + *
4405 + * Claim a bus for a set of operations. The SDIO function given
4406 + * is used to figure out which bus is relevant.
4407 + */
4408 +void sdio_claim_host(struct sdio_func *func)
4409 +{
4410 + BUG_ON(!func);
4411 + BUG_ON(!func->card);
4412 +
4413 + mmc_claim_host(func->card->host);
4414 +}
4415 +EXPORT_SYMBOL_GPL(sdio_claim_host);
4416 +
4417 +/**
4418 + * sdio_release_host - release a bus for a certain SDIO function
4419 + * @func: SDIO function that was accessed
4420 + *
4421 + * Release a bus, allowing others to claim the bus for their
4422 + * operations.
4423 + */
4424 +void sdio_release_host(struct sdio_func *func)
4425 +{
4426 + BUG_ON(!func);
4427 + BUG_ON(!func->card);
4428 +
4429 + mmc_release_host(func->card->host);
4430 +}
4431 +EXPORT_SYMBOL_GPL(sdio_release_host);
4432 +
4433 +/**
4434 + * sdio_enable_func - enables a SDIO function for usage
4435 + * @func: SDIO function to enable
4436 + *
4437 + * Powers up and activates a SDIO function so that register
4438 + * access is possible.
4439 + */
4440 +int sdio_enable_func(struct sdio_func *func)
4441 +{
4442 + int ret;
4443 + unsigned char reg;
4444 + unsigned long timeout;
4445 +
4446 + BUG_ON(!func);
4447 + BUG_ON(!func->card);
4448 +
4449 + pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
4450 +
4451 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
4452 + if (ret)
4453 + goto err;
4454 +
4455 + reg |= 1 << func->num;
4456 +
4457 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4458 + if (ret)
4459 + goto err;
4460 +
4461 + /*
4462 + * FIXME: This should timeout based on information in the CIS,
4463 + * but we don't have card to parse that yet.
4464 + */
4465 + timeout = jiffies + HZ;
4466 +
4467 + while (1) {
4468 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
4469 + if (ret)
4470 + goto err;
4471 + if (reg & (1 << func->num))
4472 + break;
4473 + ret = -ETIME;
4474 + if (time_after(jiffies, timeout))
4475 + goto err;
4476 + }
4477 +
4478 + pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
4479 +
4480 + return 0;
4481 +
4482 +err:
4483 + pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
4484 + return ret;
4485 +}
4486 +EXPORT_SYMBOL_GPL(sdio_enable_func);
4487 +
4488 +/**
4489 + * sdio_disable_func - disable a SDIO function
4490 + * @func: SDIO function to disable
4491 + *
4492 + * Powers down and deactivates a SDIO function. Register access
4493 + * to this function will fail until the function is reenabled.
4494 + */
4495 +int sdio_disable_func(struct sdio_func *func)
4496 +{
4497 + int ret;
4498 + unsigned char reg;
4499 +
4500 + BUG_ON(!func);
4501 + BUG_ON(!func->card);
4502 +
4503 + pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
4504 +
4505 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
4506 + if (ret)
4507 + goto err;
4508 +
4509 + reg &= ~(1 << func->num);
4510 +
4511 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4512 + if (ret)
4513 + goto err;
4514 +
4515 + pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
4516 +
4517 + return 0;
4518 +
4519 +err:
4520 + pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
4521 + return -EIO;
4522 +}
4523 +EXPORT_SYMBOL_GPL(sdio_disable_func);
4524 +
4525 +/**
4526 + * sdio_set_block_size - set the block size of an SDIO function
4527 + * @func: SDIO function to change
4528 + * @blksz: new block size or 0 to use the default.
4529 + *
4530 + * The default block size is the largest supported by both the function
4531 + * and the host, with a maximum of 512 to ensure that arbitrarily sized
4532 + * data transfer use the optimal (least) number of commands.
4533 + *
4534 + * A driver may call this to override the default block size set by the
4535 + * core. This can be used to set a block size greater than the maximum
4536 + * that reported by the card; it is the driver's responsibility to ensure
4537 + * it uses a value that the card supports.
4538 + *
4539 + * Returns 0 on success, -EINVAL if the host does not support the
4540 + * requested block size, or -EIO (etc.) if one of the resultant FBR block
4541 + * size register writes failed.
4542 + *
4543 + */
4544 +int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
4545 +{
4546 + int ret;
4547 +
4548 + if (blksz > func->card->host->max_blk_size)
4549 + return -EINVAL;
4550 +
4551 + if (blksz == 0) {
4552 + blksz = min(min(
4553 + func->max_blksize,
4554 + func->card->host->max_blk_size),
4555 + 512u);
4556 + }
4557 +
4558 + ret = mmc_io_rw_direct(func->card, 1, 0,
4559 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
4560 + blksz & 0xff, NULL);
4561 + if (ret)
4562 + return ret;
4563 + ret = mmc_io_rw_direct(func->card, 1, 0,
4564 + SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
4565 + (blksz >> 8) & 0xff, NULL);
4566 + if (ret)
4567 + return ret;
4568 + func->cur_blksize = blksz;
4569 + return 0;
4570 +}
4571 +
4572 +EXPORT_SYMBOL_GPL(sdio_set_block_size);
4573 +
4574 +/* Split an arbitrarily sized data transfer into several
4575 + * IO_RW_EXTENDED commands. */
4576 +static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
4577 + unsigned addr, int incr_addr, u8 *buf, unsigned size)
4578 +{
4579 + unsigned remainder = size;
4580 + unsigned max_blocks;
4581 + int ret;
4582 +
4583 + /* Do the bulk of the transfer using block mode (if supported). */
4584 + if (func->card->cccr.multi_block) {
4585 + /* Blocks per command is limited by host count, host transfer
4586 + * size (we only use a single sg entry) and the maximum for
4587 + * IO_RW_EXTENDED of 511 blocks. */
4588 + max_blocks = min(min(
4589 + func->card->host->max_blk_count,
4590 + func->card->host->max_seg_size / func->cur_blksize),
4591 + 511u);
4592 +
4593 + while (remainder > func->cur_blksize) {
4594 + unsigned blocks;
4595 +
4596 + blocks = remainder / func->cur_blksize;
4597 + if (blocks > max_blocks)
4598 + blocks = max_blocks;
4599 + size = blocks * func->cur_blksize;
4600 +
4601 + ret = mmc_io_rw_extended(func->card, write,
4602 + func->num, addr, incr_addr, buf,
4603 + blocks, func->cur_blksize);
4604 + if (ret)
4605 + return ret;
4606 +
4607 + remainder -= size;
4608 + buf += size;
4609 + if (incr_addr)
4610 + addr += size;
4611 + }
4612 + }
4613 +
4614 + /* Write the remainder using byte mode. */
4615 + while (remainder > 0) {
4616 + size = remainder;
4617 + if (size > func->cur_blksize)
4618 + size = func->cur_blksize;
4619 + if (size > 512)
4620 + size = 512; /* maximum size for byte mode */
4621 +
4622 + ret = mmc_io_rw_extended(func->card, write, func->num, addr,
4623 + incr_addr, buf, 1, size);
4624 + if (ret)
4625 + return ret;
4626 +
4627 + remainder -= size;
4628 + buf += size;
4629 + if (incr_addr)
4630 + addr += size;
4631 + }
4632 + return 0;
4633 +}
4634 +
4635 +/**
4636 + * sdio_readb - read a single byte from a SDIO function
4637 + * @func: SDIO function to access
4638 + * @addr: address to read
4639 + * @err_ret: optional status value from transfer
4640 + *
4641 + * Reads a single byte from the address space of a given SDIO
4642 + * function. If there is a problem reading the address, 0xff
4643 + * is returned and @err_ret will contain the error code.
4644 + */
4645 +unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
4646 + int *err_ret)
4647 +{
4648 + int ret;
4649 + unsigned char val;
4650 +
4651 + BUG_ON(!func);
4652 +
4653 + if (err_ret)
4654 + *err_ret = 0;
4655 +
4656 + ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
4657 + if (ret) {
4658 + if (err_ret)
4659 + *err_ret = ret;
4660 + return 0xFF;
4661 + }
4662 +
4663 + return val;
4664 +}
4665 +EXPORT_SYMBOL_GPL(sdio_readb);
4666 +
4667 +/**
4668 + * sdio_writeb - write a single byte to a SDIO function
4669 + * @func: SDIO function to access
4670 + * @b: byte to write
4671 + * @addr: address to write to
4672 + * @err_ret: optional status value from transfer
4673 + *
4674 + * Writes a single byte to the address space of a given SDIO
4675 + * function. @err_ret will contain the status of the actual
4676 + * transfer.
4677 + */
4678 +void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4679 + int *err_ret)
4680 +{
4681 + int ret;
4682 +
4683 + BUG_ON(!func);
4684 +
4685 + ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
4686 + if (err_ret)
4687 + *err_ret = ret;
4688 +}
4689 +EXPORT_SYMBOL_GPL(sdio_writeb);
4690 +
4691 +/**
4692 + * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
4693 + * @func: SDIO function to access
4694 + * @dst: buffer to store the data
4695 + * @addr: address to begin reading from
4696 + * @count: number of bytes to read
4697 + *
4698 + * Reads from the address space of a given SDIO function. Return
4699 + * value indicates if the transfer succeeded or not.
4700 + */
4701 +int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
4702 + unsigned int addr, int count)
4703 +{
4704 + return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
4705 +}
4706 +EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
4707 +
4708 +/**
4709 + * sdio_memcpy_toio - write a chunk of memory to a SDIO function
4710 + * @func: SDIO function to access
4711 + * @addr: address to start writing to
4712 + * @src: buffer that contains the data to write
4713 + * @count: number of bytes to write
4714 + *
4715 + * Writes to the address space of a given SDIO function. Return
4716 + * value indicates if the transfer succeeded or not.
4717 + */
4718 +int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
4719 + void *src, int count)
4720 +{
4721 + return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
4722 +}
4723 +EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
4724 +
4725 +/**
4726 + * sdio_readsb - read from a FIFO on a SDIO function
4727 + * @func: SDIO function to access
4728 + * @dst: buffer to store the data
4729 + * @addr: address of (single byte) FIFO
4730 + * @count: number of bytes to read
4731 + *
4732 + * Reads from the specified FIFO of a given SDIO function. Return
4733 + * value indicates if the transfer succeeded or not.
4734 + */
4735 +int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
4736 + int count)
4737 +{
4738 + return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
4739 +}
4740 +
4741 +EXPORT_SYMBOL_GPL(sdio_readsb);
4742 +
4743 +/**
4744 + * sdio_writesb - write to a FIFO of a SDIO function
4745 + * @func: SDIO function to access
4746 + * @addr: address of (single byte) FIFO
4747 + * @src: buffer that contains the data to write
4748 + * @count: number of bytes to write
4749 + *
4750 + * Writes to the specified FIFO of a given SDIO function. Return
4751 + * value indicates if the transfer succeeded or not.
4752 + */
4753 +int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
4754 + int count)
4755 +{
4756 + return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
4757 +}
4758 +EXPORT_SYMBOL_GPL(sdio_writesb);
4759 +
4760 +/**
4761 + * sdio_readw - read a 16 bit integer from a SDIO function
4762 + * @func: SDIO function to access
4763 + * @addr: address to read
4764 + * @err_ret: optional status value from transfer
4765 + *
4766 + * Reads a 16 bit integer from the address space of a given SDIO
4767 + * function. If there is a problem reading the address, 0xffff
4768 + * is returned and @err_ret will contain the error code.
4769 + */
4770 +unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
4771 + int *err_ret)
4772 +{
4773 + int ret;
4774 +
4775 + if (err_ret)
4776 + *err_ret = 0;
4777 +
4778 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
4779 + if (ret) {
4780 + if (err_ret)
4781 + *err_ret = ret;
4782 + return 0xFFFF;
4783 + }
4784 +
4785 + return le16_to_cpu(*(u16*)func->tmpbuf);
4786 +}
4787 +EXPORT_SYMBOL_GPL(sdio_readw);
4788 +
4789 +/**
4790 + * sdio_writew - write a 16 bit integer to a SDIO function
4791 + * @func: SDIO function to access
4792 + * @b: integer to write
4793 + * @addr: address to write to
4794 + * @err_ret: optional status value from transfer
4795 + *
4796 + * Writes a 16 bit integer to the address space of a given SDIO
4797 + * function. @err_ret will contain the status of the actual
4798 + * transfer.
4799 + */
4800 +void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
4801 + int *err_ret)
4802 +{
4803 + int ret;
4804 +
4805 + *(u16*)func->tmpbuf = cpu_to_le16(b);
4806 +
4807 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
4808 + if (err_ret)
4809 + *err_ret = ret;
4810 +}
4811 +EXPORT_SYMBOL_GPL(sdio_writew);
4812 +
4813 +/**
4814 + * sdio_readl - read a 32 bit integer from a SDIO function
4815 + * @func: SDIO function to access
4816 + * @addr: address to read
4817 + * @err_ret: optional status value from transfer
4818 + *
4819 + * Reads a 32 bit integer from the address space of a given SDIO
4820 + * function. If there is a problem reading the address,
4821 + * 0xffffffff is returned and @err_ret will contain the error
4822 + * code.
4823 + */
4824 +unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
4825 + int *err_ret)
4826 +{
4827 + int ret;
4828 +
4829 + if (err_ret)
4830 + *err_ret = 0;
4831 +
4832 + ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
4833 + if (ret) {
4834 + if (err_ret)
4835 + *err_ret = ret;
4836 + return 0xFFFFFFFF;
4837 + }
4838 +
4839 + return le32_to_cpu(*(u32*)func->tmpbuf);
4840 +}
4841 +EXPORT_SYMBOL_GPL(sdio_readl);
4842 +
4843 +/**
4844 + * sdio_writel - write a 32 bit integer to a SDIO function
4845 + * @func: SDIO function to access
4846 + * @b: integer to write
4847 + * @addr: address to write to
4848 + * @err_ret: optional status value from transfer
4849 + *
4850 + * Writes a 32 bit integer to the address space of a given SDIO
4851 + * function. @err_ret will contain the status of the actual
4852 + * transfer.
4853 + */
4854 +void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
4855 + int *err_ret)
4856 +{
4857 + int ret;
4858 +
4859 + *(u32*)func->tmpbuf = cpu_to_le32(b);
4860 +
4861 + ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
4862 + if (err_ret)
4863 + *err_ret = ret;
4864 +}
4865 +EXPORT_SYMBOL_GPL(sdio_writel);
4866 +
4867 +/**
4868 + * sdio_f0_readb - read a single byte from SDIO function 0
4869 + * @func: an SDIO function of the card
4870 + * @addr: address to read
4871 + * @err_ret: optional status value from transfer
4872 + *
4873 + * Reads a single byte from the address space of SDIO function 0.
4874 + * If there is a problem reading the address, 0xff is returned
4875 + * and @err_ret will contain the error code.
4876 + */
4877 +unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
4878 + int *err_ret)
4879 +{
4880 + int ret;
4881 + unsigned char val;
4882 +
4883 + BUG_ON(!func);
4884 +
4885 + if (err_ret)
4886 + *err_ret = 0;
4887 +
4888 + ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
4889 + if (ret) {
4890 + if (err_ret)
4891 + *err_ret = ret;
4892 + return 0xFF;
4893 + }
4894 +
4895 + return val;
4896 +}
4897 +EXPORT_SYMBOL_GPL(sdio_f0_readb);
4898 +
4899 +/**
4900 + * sdio_f0_writeb - write a single byte to SDIO function 0
4901 + * @func: an SDIO function of the card
4902 + * @b: byte to write
4903 + * @addr: address to write to
4904 + * @err_ret: optional status value from transfer
4905 + *
4906 + * Writes a single byte to the address space of SDIO function 0.
4907 + * @err_ret will contain the status of the actual transfer.
4908 + *
4909 + * Only writes to the vendor specific CCCR registers (0xF0 -
4910 + * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
4911 + * writes outside this range.
4912 + */
4913 +void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4914 + int *err_ret)
4915 +{
4916 + int ret;
4917 +
4918 + BUG_ON(!func);
4919 +
4920 + if (addr < 0xF0 || addr > 0xFF) {
4921 + if (err_ret)
4922 + *err_ret = -EINVAL;
4923 + return;
4924 + }
4925 +
4926 + ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
4927 + if (err_ret)
4928 + *err_ret = ret;
4929 +}
4930 +EXPORT_SYMBOL_GPL(sdio_f0_writeb);
4931 --- /dev/null
4932 +++ b/drivers/mmc/core/sdio_irq.c
4933 @@ -0,0 +1,267 @@
4934 +/*
4935 + * linux/drivers/mmc/core/sdio_irq.c
4936 + *
4937 + * Author: Nicolas Pitre
4938 + * Created: June 18, 2007
4939 + * Copyright: MontaVista Software Inc.
4940 + *
4941 + * This program is free software; you can redistribute it and/or modify
4942 + * it under the terms of the GNU General Public License as published by
4943 + * the Free Software Foundation; either version 2 of the License, or (at
4944 + * your option) any later version.
4945 + */
4946 +
4947 +#include <linux/kernel.h>
4948 +#include <linux/sched.h>
4949 +#include <linux/kthread.h>
4950 +#include <linux/wait.h>
4951 +#include <linux/delay.h>
4952 +
4953 +#include <linux/mmc/core.h>
4954 +#include <linux/mmc/host.h>
4955 +#include <linux/mmc/card.h>
4956 +#include <linux/mmc/sdio.h>
4957 +#include <linux/mmc/sdio_func.h>
4958 +
4959 +#include "sdio_ops.h"
4960 +
4961 +static int process_sdio_pending_irqs(struct mmc_card *card)
4962 +{
4963 + int i, ret, count;
4964 + unsigned char pending;
4965 +
4966 + ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
4967 + if (ret) {
4968 + printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
4969 + mmc_card_id(card), ret);
4970 + return ret;
4971 + }
4972 +
4973 + count = 0;
4974 + for (i = 1; i <= 7; i++) {
4975 + if (pending & (1 << i)) {
4976 + struct sdio_func *func = card->sdio_func[i - 1];
4977 + if (!func) {
4978 + printk(KERN_WARNING "%s: pending IRQ for "
4979 + "non-existant function\n",
4980 + mmc_card_id(card));
4981 + ret = -EINVAL;
4982 + } else if (func->irq_handler) {
4983 + func->irq_handler(func);
4984 + count++;
4985 + } else {
4986 + printk(KERN_WARNING "%s: pending IRQ with no handler\n",
4987 + sdio_func_id(func));
4988 + ret = -EINVAL;
4989 + }
4990 + }
4991 + }
4992 +
4993 + if (count)
4994 + return count;
4995 +
4996 + return ret;
4997 +}
4998 +
4999 +static int sdio_irq_thread(void *_host)
5000 +{
5001 + struct mmc_host *host = _host;
5002 + struct sched_param param = { .sched_priority = 1 };
5003 + unsigned long period, idle_period;
5004 + int ret;
5005 +
5006 + sched_setscheduler(current, SCHED_FIFO, &param);
5007 +
5008 + /*
5009 + * We want to allow for SDIO cards to work even on non SDIO
5010 + * aware hosts. One thing that non SDIO host cannot do is
5011 + * asynchronous notification of pending SDIO card interrupts
5012 + * hence we poll for them in that case.
5013 + */
5014 + idle_period = msecs_to_jiffies(10);
5015 + period = (host->caps & MMC_CAP_SDIO_IRQ) ?
5016 + MAX_SCHEDULE_TIMEOUT : idle_period;
5017 +
5018 + pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
5019 + mmc_hostname(host), period);
5020 +
5021 + do {
5022 + /*
5023 + * We claim the host here on drivers behalf for a couple
5024 + * reasons:
5025 + *
5026 + * 1) it is already needed to retrieve the CCCR_INTx;
5027 + * 2) we want the driver(s) to clear the IRQ condition ASAP;
5028 + * 3) we need to control the abort condition locally.
5029 + *
5030 + * Just like traditional hard IRQ handlers, we expect SDIO
5031 + * IRQ handlers to be quick and to the point, so that the
5032 + * holding of the host lock does not cover too much work
5033 + * that doesn't require that lock to be held.
5034 + */
5035 + ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
5036 + if (ret)
5037 + break;
5038 + ret = process_sdio_pending_irqs(host->card);
5039 + mmc_release_host(host);
5040 +
5041 + /*
5042 + * Give other threads a chance to run in the presence of
5043 + * errors. FIXME: determine if due to card removal and
5044 + * possibly exit this thread if so.
5045 + */
5046 + if (ret < 0)
5047 + ssleep(1);
5048 +
5049 + /*
5050 + * Adaptive polling frequency based on the assumption
5051 + * that an interrupt will be closely followed by more.
5052 + * This has a substantial benefit for network devices.
5053 + */
5054 + if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
5055 + if (ret > 0)
5056 + period /= 2;
5057 + else {
5058 + period++;
5059 + if (period > idle_period)
5060 + period = idle_period;
5061 + }
5062 + }
5063 +
5064 + set_task_state(current, TASK_INTERRUPTIBLE);
5065 + if (host->caps & MMC_CAP_SDIO_IRQ)
5066 + host->ops->enable_sdio_irq(host, 1);
5067 + if (!kthread_should_stop())
5068 + schedule_timeout(period);
5069 + set_task_state(current, TASK_RUNNING);
5070 + } while (!kthread_should_stop());
5071 +
5072 + if (host->caps & MMC_CAP_SDIO_IRQ)
5073 + host->ops->enable_sdio_irq(host, 0);
5074 +
5075 + pr_debug("%s: IRQ thread exiting with code %d\n",
5076 + mmc_hostname(host), ret);
5077 +
5078 + return ret;
5079 +}
5080 +
5081 +static int sdio_card_irq_get(struct mmc_card *card)
5082 +{
5083 + struct mmc_host *host = card->host;
5084 +
5085 + WARN_ON(!host->claimed);
5086 +
5087 + if (!host->sdio_irqs++) {
5088 + atomic_set(&host->sdio_irq_thread_abort, 0);
5089 + host->sdio_irq_thread =
5090 + kthread_run(sdio_irq_thread, host, "ksdiorqd");
5091 + if (IS_ERR(host->sdio_irq_thread)) {
5092 + int err = PTR_ERR(host->sdio_irq_thread);
5093 + host->sdio_irqs--;
5094 + return err;
5095 + }
5096 + }
5097 +
5098 + return 0;
5099 +}
5100 +
5101 +static int sdio_card_irq_put(struct mmc_card *card)
5102 +{
5103 + struct mmc_host *host = card->host;
5104 +
5105 + WARN_ON(!host->claimed);
5106 + BUG_ON(host->sdio_irqs < 1);
5107 +
5108 + if (!--host->sdio_irqs) {
5109 + atomic_set(&host->sdio_irq_thread_abort, 1);
5110 + kthread_stop(host->sdio_irq_thread);
5111 + }
5112 +
5113 + return 0;
5114 +}
5115 +
5116 +/**
5117 + * sdio_claim_irq - claim the IRQ for a SDIO function
5118 + * @func: SDIO function
5119 + * @handler: IRQ handler callback
5120 + *
5121 + * Claim and activate the IRQ for the given SDIO function. The provided
5122 + * handler will be called when that IRQ is asserted. The host is always
5123 + * claimed already when the handler is called so the handler must not
5124 + * call sdio_claim_host() nor sdio_release_host().
5125 + */
5126 +int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
5127 +{
5128 + int ret;
5129 + unsigned char reg;
5130 +
5131 + BUG_ON(!func);
5132 + BUG_ON(!func->card);
5133 +
5134 + pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
5135 +
5136 + if (func->irq_handler) {
5137 + pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
5138 + return -EBUSY;
5139 + }
5140 +
5141 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
5142 + if (ret)
5143 + return ret;
5144 +
5145 + reg |= 1 << func->num;
5146 +
5147 + reg |= 1; /* Master interrupt enable */
5148 +
5149 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5150 + if (ret)
5151 + return ret;
5152 +
5153 + func->irq_handler = handler;
5154 + ret = sdio_card_irq_get(func->card);
5155 + if (ret)
5156 + func->irq_handler = NULL;
5157 +
5158 + return ret;
5159 +}
5160 +EXPORT_SYMBOL_GPL(sdio_claim_irq);
5161 +
5162 +/**
5163 + * sdio_release_irq - release the IRQ for a SDIO function
5164 + * @func: SDIO function
5165 + *
5166 + * Disable and release the IRQ for the given SDIO function.
5167 + */
5168 +int sdio_release_irq(struct sdio_func *func)
5169 +{
5170 + int ret;
5171 + unsigned char reg;
5172 +
5173 + BUG_ON(!func);
5174 + BUG_ON(!func->card);
5175 +
5176 + pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
5177 +
5178 + if (func->irq_handler) {
5179 + func->irq_handler = NULL;
5180 + sdio_card_irq_put(func->card);
5181 + }
5182 +
5183 + ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
5184 + if (ret)
5185 + return ret;
5186 +
5187 + reg &= ~(1 << func->num);
5188 +
5189 + /* Disable master interrupt with the last function interrupt */
5190 + if (!(reg & 0xFE))
5191 + reg = 0;
5192 +
5193 + ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5194 + if (ret)
5195 + return ret;
5196 +
5197 + return 0;
5198 +}
5199 +EXPORT_SYMBOL_GPL(sdio_release_irq);
5200 +
5201 --- /dev/null
5202 +++ b/drivers/mmc/core/sdio_ops.c
5203 @@ -0,0 +1,175 @@
5204 +/*
5205 + * linux/drivers/mmc/sdio_ops.c
5206 + *
5207 + * Copyright 2006-2007 Pierre Ossman
5208 + *
5209 + * This program is free software; you can redistribute it and/or modify
5210 + * it under the terms of the GNU General Public License as published by
5211 + * the Free Software Foundation; either version 2 of the License, or (at
5212 + * your option) any later version.
5213 + */
5214 +
5215 +#include <linux/scatterlist.h>
5216 +
5217 +#include <linux/mmc/host.h>
5218 +#include <linux/mmc/card.h>
5219 +#include <linux/mmc/mmc.h>
5220 +#include <linux/mmc/sdio.h>
5221 +
5222 +#include "core.h"
5223 +
5224 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
5225 +{
5226 + struct mmc_command cmd;
5227 + int i, err = 0;
5228 +
5229 + BUG_ON(!host);
5230 +
5231 + memset(&cmd, 0, sizeof(struct mmc_command));
5232 +
5233 + cmd.opcode = SD_IO_SEND_OP_COND;
5234 + cmd.arg = ocr;
5235 + cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
5236 +
5237 + for (i = 100; i; i--) {
5238 + err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
5239 + if (err)
5240 + break;
5241 +
5242 + /* if we're just probing, do a single pass */
5243 + if (ocr == 0)
5244 + break;
5245 +
5246 + /* otherwise wait until reset completes */
5247 + if (mmc_host_is_spi(host)) {
5248 + /*
5249 + * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
5250 + * an initialized card under SPI, but some cards
5251 + * (Marvell's) only behave when looking at this
5252 + * one.
5253 + */
5254 + if (cmd.resp[1] & MMC_CARD_BUSY)
5255 + break;
5256 + } else {
5257 + if (cmd.resp[0] & MMC_CARD_BUSY)
5258 + break;
5259 + }
5260 +
5261 + err = -ETIMEDOUT;
5262 +
5263 + mmc_delay(10);
5264 + }
5265 +
5266 + if (rocr)
5267 + *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
5268 +
5269 + return err;
5270 +}
5271 +
5272 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5273 + unsigned addr, u8 in, u8* out)
5274 +{
5275 + struct mmc_command cmd;
5276 + int err;
5277 +
5278 + BUG_ON(!card);
5279 + BUG_ON(fn > 7);
5280 +
5281 + memset(&cmd, 0, sizeof(struct mmc_command));
5282 +
5283 + cmd.opcode = SD_IO_RW_DIRECT;
5284 + cmd.arg = write ? 0x80000000 : 0x00000000;
5285 + cmd.arg |= fn << 28;
5286 + cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
5287 + cmd.arg |= addr << 9;
5288 + cmd.arg |= in;
5289 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
5290 +
5291 + err = mmc_wait_for_cmd(card->host, &cmd, 0);
5292 + if (err)
5293 + return err;
5294 +
5295 + if (mmc_host_is_spi(card->host)) {
5296 + /* host driver already reported errors */
5297 + } else {
5298 + if (cmd.resp[0] & R5_ERROR)
5299 + return -EIO;
5300 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5301 + return -EINVAL;
5302 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5303 + return -ERANGE;
5304 + }
5305 +
5306 + if (out) {
5307 + if (mmc_host_is_spi(card->host))
5308 + *out = (cmd.resp[0] >> 8) & 0xFF;
5309 + else
5310 + *out = cmd.resp[0] & 0xFF;
5311 + }
5312 +
5313 + return 0;
5314 +}
5315 +
5316 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5317 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
5318 +{
5319 + struct mmc_request mrq;
5320 + struct mmc_command cmd;
5321 + struct mmc_data data;
5322 + struct scatterlist sg;
5323 +
5324 + BUG_ON(!card);
5325 + BUG_ON(fn > 7);
5326 + BUG_ON(blocks == 1 && blksz > 512);
5327 + WARN_ON(blocks == 0);
5328 + WARN_ON(blksz == 0);
5329 +
5330 + memset(&mrq, 0, sizeof(struct mmc_request));
5331 + memset(&cmd, 0, sizeof(struct mmc_command));
5332 + memset(&data, 0, sizeof(struct mmc_data));
5333 +
5334 + mrq.cmd = &cmd;
5335 + mrq.data = &data;
5336 +
5337 + cmd.opcode = SD_IO_RW_EXTENDED;
5338 + cmd.arg = write ? 0x80000000 : 0x00000000;
5339 + cmd.arg |= fn << 28;
5340 + cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
5341 + cmd.arg |= addr << 9;
5342 + if (blocks == 1 && blksz <= 512)
5343 + cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
5344 + else
5345 + cmd.arg |= 0x08000000 | blocks; /* block mode */
5346 + cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
5347 +
5348 + data.blksz = blksz;
5349 + data.blocks = blocks;
5350 + data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
5351 + data.sg = &sg;
5352 + data.sg_len = 1;
5353 +
5354 + sg_init_one(&sg, buf, blksz * blocks);
5355 +
5356 + mmc_set_data_timeout(&data, card);
5357 +
5358 + mmc_wait_for_req(card->host, &mrq);
5359 +
5360 + if (cmd.error)
5361 + return cmd.error;
5362 + if (data.error)
5363 + return data.error;
5364 +
5365 + if (mmc_host_is_spi(card->host)) {
5366 + /* host driver already reported errors */
5367 + } else {
5368 + if (cmd.resp[0] & R5_ERROR)
5369 + return -EIO;
5370 + if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5371 + return -EINVAL;
5372 + if (cmd.resp[0] & R5_OUT_OF_RANGE)
5373 + return -ERANGE;
5374 + }
5375 +
5376 + return 0;
5377 +}
5378 +
5379 --- /dev/null
5380 +++ b/drivers/mmc/core/sdio_ops.h
5381 @@ -0,0 +1,22 @@
5382 +/*
5383 + * linux/drivers/mmc/sdio_ops.c
5384 + *
5385 + * Copyright 2006-2007 Pierre Ossman
5386 + *
5387 + * This program is free software; you can redistribute it and/or modify
5388 + * it under the terms of the GNU General Public License as published by
5389 + * the Free Software Foundation; either version 2 of the License, or (at
5390 + * your option) any later version.
5391 + */
5392 +
5393 +#ifndef _MMC_SDIO_OPS_H
5394 +#define _MMC_SDIO_OPS_H
5395 +
5396 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
5397 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5398 + unsigned addr, u8 in, u8* out);
5399 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5400 + unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz);
5401 +
5402 +#endif
5403 +
5404 --- a/include/linux/mmc/card.h
5405 +++ b/include/linux/mmc/card.h
5406 @@ -55,7 +55,28 @@
5407 unsigned int hs_max_dtr;
5408 };
5409
5410 +struct sdio_cccr {
5411 + unsigned int sdio_vsn;
5412 + unsigned int sd_vsn;
5413 + unsigned int multi_block:1,
5414 + low_speed:1,
5415 + wide_bus:1,
5416 + high_power:1,
5417 + high_speed:1;
5418 +};
5419 +
5420 +struct sdio_cis {
5421 + unsigned short vendor;
5422 + unsigned short device;
5423 + unsigned short blksize;
5424 + unsigned int max_dtr;
5425 +};
5426 +
5427 struct mmc_host;
5428 +struct sdio_func;
5429 +struct sdio_func_tuple;
5430 +
5431 +#define SDIO_MAX_FUNCS 7
5432
5433 /*
5434 * MMC device
5435 @@ -67,11 +88,13 @@
5436 unsigned int type; /* card type */
5437 #define MMC_TYPE_MMC 0 /* MMC card */
5438 #define MMC_TYPE_SD 1 /* SD card */
5439 +#define MMC_TYPE_SDIO 2 /* SDIO card */
5440 unsigned int state; /* (our) card state */
5441 #define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
5442 #define MMC_STATE_READONLY (1<<1) /* card is read-only */
5443 #define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
5444 #define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
5445 +
5446 u32 raw_cid[4]; /* raw card CID */
5447 u32 raw_csd[4]; /* raw card CSD */
5448 u32 raw_scr[2]; /* raw card SCR */
5449 @@ -80,10 +103,19 @@
5450 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
5451 struct sd_scr scr; /* extra SD information */
5452 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
5453 +
5454 + unsigned int sdio_funcs; /* number of SDIO functions */
5455 + struct sdio_cccr cccr; /* common card info */
5456 + struct sdio_cis cis; /* common tuple info */
5457 + struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
5458 + unsigned num_info; /* number of info strings */
5459 + const char **info; /* info strings */
5460 + struct sdio_func_tuple *tuples; /* unknown common tuples */
5461 };
5462
5463 #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
5464 #define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
5465 +#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
5466
5467 #define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
5468 #define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
5469 --- a/include/linux/mmc/core.h
5470 +++ b/include/linux/mmc/core.h
5471 @@ -25,14 +25,20 @@
5472 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
5473 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
5474 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
5475 -#define MMC_CMD_MASK (3 << 5) /* command type */
5476 +
5477 +#define MMC_CMD_MASK (3 << 5) /* non-SPI command type */
5478 #define MMC_CMD_AC (0 << 5)
5479 #define MMC_CMD_ADTC (1 << 5)
5480 #define MMC_CMD_BC (2 << 5)
5481 #define MMC_CMD_BCR (3 << 5)
5482
5483 +#define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */
5484 +#define MMC_RSP_SPI_S2 (1 << 8) /* second byte */
5485 +#define MMC_RSP_SPI_B4 (1 << 9) /* four data bytes */
5486 +#define MMC_RSP_SPI_BUSY (1 << 10) /* card may send busy */
5487 +
5488 /*
5489 - * These are the response types, and correspond to valid bit
5490 + * These are the native response types, and correspond to valid bit
5491 * patterns of the above flags. One additional valid pattern
5492 * is all zeros, which means we don't expect a response.
5493 */
5494 @@ -41,12 +47,30 @@
5495 #define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
5496 #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
5497 #define MMC_RSP_R3 (MMC_RSP_PRESENT)
5498 +#define MMC_RSP_R4 (MMC_RSP_PRESENT)
5499 +#define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5500 #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5501 #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5502
5503 #define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
5504
5505 /*
5506 + * These are the SPI response types for MMC, SD, and SDIO cards.
5507 + * Commands return R1, with maybe more info. Zero is an error type;
5508 + * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
5509 + */
5510 +#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
5511 +#define MMC_RSP_SPI_R1B (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
5512 +#define MMC_RSP_SPI_R2 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5513 +#define MMC_RSP_SPI_R3 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5514 +#define MMC_RSP_SPI_R4 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5515 +#define MMC_RSP_SPI_R5 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5516 +#define MMC_RSP_SPI_R7 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5517 +
5518 +#define mmc_spi_resp_type(cmd) ((cmd)->flags & \
5519 + (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
5520 +
5521 +/*
5522 * These are the command types.
5523 */
5524 #define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK)
5525 @@ -54,12 +78,19 @@
5526 unsigned int retries; /* max number of retries */
5527 unsigned int error; /* command error */
5528
5529 -#define MMC_ERR_NONE 0
5530 -#define MMC_ERR_TIMEOUT 1
5531 -#define MMC_ERR_BADCRC 2
5532 -#define MMC_ERR_FIFO 3
5533 -#define MMC_ERR_FAILED 4
5534 -#define MMC_ERR_INVALID 5
5535 +/*
5536 + * Standard errno values are used for errors, but some have specific
5537 + * meaning in the MMC layer:
5538 + *
5539 + * ETIMEDOUT Card took too long to respond
5540 + * EILSEQ Basic format problem with the received or sent data
5541 + * (e.g. CRC check failed, incorrect opcode in response
5542 + * or bad end bit)
5543 + * EINVAL Request cannot be performed because of restrictions
5544 + * in hardware and/or the driver
5545 + * ENOMEDIUM Host can determine that the slot is empty and is
5546 + * actively failing requests
5547 + */
5548
5549 struct mmc_data *data; /* data segment associated with cmd */
5550 struct mmc_request *mrq; /* associated request */
5551 @@ -76,7 +107,6 @@
5552 #define MMC_DATA_WRITE (1 << 8)
5553 #define MMC_DATA_READ (1 << 9)
5554 #define MMC_DATA_STREAM (1 << 10)
5555 -#define MMC_DATA_MULTI (1 << 11)
5556
5557 unsigned int bytes_xfered;
5558
5559 @@ -104,9 +134,20 @@
5560 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
5561 struct mmc_command *, int);
5562
5563 -extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int);
5564 +extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
5565
5566 -extern void mmc_claim_host(struct mmc_host *host);
5567 +extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
5568 extern void mmc_release_host(struct mmc_host *host);
5569
5570 +/**
5571 + * mmc_claim_host - exclusively claim a host
5572 + * @host: mmc host to claim
5573 + *
5574 + * Claim a host for a set of operations.
5575 + */
5576 +static inline void mmc_claim_host(struct mmc_host *host)
5577 +{
5578 + __mmc_claim_host(host, NULL);
5579 +}
5580 +
5581 #endif
5582 --- a/include/linux/mmc/host.h
5583 +++ b/include/linux/mmc/host.h
5584 @@ -10,6 +10,8 @@
5585 #ifndef LINUX_MMC_HOST_H
5586 #define LINUX_MMC_HOST_H
5587
5588 +#include <linux/leds.h>
5589 +
5590 #include <linux/mmc/core.h>
5591
5592 struct mmc_ios {
5593 @@ -51,6 +53,7 @@
5594 void (*request)(struct mmc_host *host, struct mmc_request *req);
5595 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
5596 int (*get_ro)(struct mmc_host *host);
5597 + void (*enable_sdio_irq)(struct mmc_host *host, int enable);
5598 };
5599
5600 struct mmc_card;
5601 @@ -87,9 +90,10 @@
5602
5603 #define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
5604 #define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */
5605 -#define MMC_CAP_BYTEBLOCK (1 << 2) /* Can do non-log2 block sizes */
5606 -#define MMC_CAP_MMC_HIGHSPEED (1 << 3) /* Can do MMC high-speed timing */
5607 -#define MMC_CAP_SD_HIGHSPEED (1 << 4) /* Can do SD high-speed timing */
5608 +#define MMC_CAP_MMC_HIGHSPEED (1 << 2) /* Can do MMC high-speed timing */
5609 +#define MMC_CAP_SD_HIGHSPEED (1 << 3) /* Can do SD high-speed timing */
5610 +#define MMC_CAP_SDIO_IRQ (1 << 4) /* Can signal pending SDIO IRQs */
5611 +#define MMC_CAP_SPI (1 << 5) /* Talks only SPI protocols */
5612
5613 /* host specific block data */
5614 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
5615 @@ -106,23 +110,30 @@
5616 struct mmc_ios ios; /* current io bus settings */
5617 u32 ocr; /* the current OCR setting */
5618
5619 - unsigned int mode; /* current card mode of host */
5620 -#define MMC_MODE_MMC 0
5621 -#define MMC_MODE_SD 1
5622 + /* group bitfields together to minimize padding */
5623 + unsigned int use_spi_crc:1;
5624 + unsigned int claimed:1; /* host exclusively claimed */
5625 + unsigned int bus_dead:1; /* bus has been released */
5626 +#ifdef CONFIG_MMC_DEBUG
5627 + unsigned int removed:1; /* host is being removed */
5628 +#endif
5629
5630 struct mmc_card *card; /* device attached to this host */
5631
5632 wait_queue_head_t wq;
5633 - unsigned int claimed:1; /* host exclusively claimed */
5634
5635 struct delayed_work detect;
5636 -#ifdef CONFIG_MMC_DEBUG
5637 - unsigned int removed:1; /* host is being removed */
5638 -#endif
5639
5640 const struct mmc_bus_ops *bus_ops; /* current bus driver */
5641 unsigned int bus_refs; /* reference counter */
5642 - unsigned int bus_dead:1; /* bus has been released */
5643 +
5644 + unsigned int sdio_irqs;
5645 + struct task_struct *sdio_irq_thread;
5646 + atomic_t sdio_irq_thread_abort;
5647 +
5648 +#ifdef CONFIG_LEDS_TRIGGERS
5649 + struct led_trigger *led; /* activity led */
5650 +#endif
5651
5652 unsigned long private[0] ____cacheline_aligned;
5653 };
5654 @@ -137,6 +148,8 @@
5655 return (void *)host->private;
5656 }
5657
5658 +#define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
5659 +
5660 #define mmc_dev(x) ((x)->parent)
5661 #define mmc_classdev(x) (&(x)->class_dev)
5662 #define mmc_hostname(x) ((x)->class_dev.bus_id)
5663 @@ -147,5 +160,11 @@
5664 extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
5665 extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
5666
5667 +static inline void mmc_signal_sdio_irq(struct mmc_host *host)
5668 +{
5669 + host->ops->enable_sdio_irq(host, 0);
5670 + wake_up_process(host->sdio_irq_thread);
5671 +}
5672 +
5673 #endif
5674
5675 --- a/include/linux/mmc/mmc.h
5676 +++ b/include/linux/mmc/mmc.h
5677 @@ -27,7 +27,7 @@
5678
5679 /* Standard MMC commands (4.1) type argument response */
5680 /* class 1 */
5681 -#define MMC_GO_IDLE_STATE 0 /* bc */
5682 +#define MMC_GO_IDLE_STATE 0 /* bc */
5683 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
5684 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
5685 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
5686 @@ -39,8 +39,10 @@
5687 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
5688 #define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
5689 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
5690 -#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5691 +#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
5692 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
5693 +#define MMC_SPI_READ_OCR 58 /* spi spi_R3 */
5694 +#define MMC_SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
5695
5696 /* class 2 */
5697 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
5698 @@ -90,15 +92,15 @@
5699 */
5700
5701 /*
5702 - MMC status in R1
5703 + MMC status in R1, for native mode (SPI bits are different)
5704 Type
5705 - e : error bit
5706 + e : error bit
5707 s : status bit
5708 r : detected and set for the actual command response
5709 x : detected and set during command execution. the host must poll
5710 the card by sending status command in order to read these bits.
5711 Clear condition
5712 - a : according to the card state
5713 + a : according to the card state
5714 b : always related to the previous command. Reception of
5715 a valid command will clear it (with a delay of one command)
5716 c : clear by read
5717 @@ -124,10 +126,33 @@
5718 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
5719 #define R1_ERASE_RESET (1 << 13) /* sr, c */
5720 #define R1_STATUS(x) (x & 0xFFFFE000)
5721 -#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5722 +#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5723 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
5724 #define R1_APP_CMD (1 << 5) /* sr, c */
5725
5726 +/*
5727 + * MMC/SD in SPI mode reports R1 status always, and R2 for SEND_STATUS
5728 + * R1 is the low order byte; R2 is the next highest byte, when present.
5729 + */
5730 +#define R1_SPI_IDLE (1 << 0)
5731 +#define R1_SPI_ERASE_RESET (1 << 1)
5732 +#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
5733 +#define R1_SPI_COM_CRC (1 << 3)
5734 +#define R1_SPI_ERASE_SEQ (1 << 4)
5735 +#define R1_SPI_ADDRESS (1 << 5)
5736 +#define R1_SPI_PARAMETER (1 << 6)
5737 +/* R1 bit 7 is always zero */
5738 +#define R2_SPI_CARD_LOCKED (1 << 8)
5739 +#define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
5740 +#define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
5741 +#define R2_SPI_ERROR (1 << 10)
5742 +#define R2_SPI_CC_ERROR (1 << 11)
5743 +#define R2_SPI_CARD_ECC_ERROR (1 << 12)
5744 +#define R2_SPI_WP_VIOLATION (1 << 13)
5745 +#define R2_SPI_ERASE_PARAM (1 << 14)
5746 +#define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
5747 +#define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
5748 +
5749 /* These are unpacked versions of the actual responses */
5750
5751 struct _mmc_csd {
5752 @@ -182,6 +207,7 @@
5753 */
5754 #define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
5755 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
5756 + /* (and for SPI, CMD58,59) */
5757 #define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
5758 /* (CMD11) */
5759 #define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
5760 @@ -227,6 +253,7 @@
5761 #define EXT_CSD_BUS_WIDTH 183 /* R/W */
5762 #define EXT_CSD_HS_TIMING 185 /* R/W */
5763 #define EXT_CSD_CARD_TYPE 196 /* RO */
5764 +#define EXT_CSD_REV 192 /* RO */
5765 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
5766
5767 /*
5768 --- /dev/null
5769 +++ b/include/linux/mmc/sdio.h
5770 @@ -0,0 +1,159 @@
5771 +/*
5772 + * include/linux/mmc/sdio.h
5773 + *
5774 + * Copyright 2006-2007 Pierre Ossman
5775 + *
5776 + * This program is free software; you can redistribute it and/or modify
5777 + * it under the terms of the GNU General Public License as published by
5778 + * the Free Software Foundation; either version 2 of the License, or (at
5779 + * your option) any later version.
5780 + */
5781 +
5782 +#ifndef MMC_SDIO_H
5783 +#define MMC_SDIO_H
5784 +
5785 +/* SDIO commands type argument response */
5786 +#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
5787 +#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
5788 +#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
5789 +
5790 +/*
5791 + * SD_IO_RW_DIRECT argument format:
5792 + *
5793 + * [31] R/W flag
5794 + * [30:28] Function number
5795 + * [27] RAW flag
5796 + * [25:9] Register address
5797 + * [7:0] Data
5798 + */
5799 +
5800 +/*
5801 + * SD_IO_RW_EXTENDED argument format:
5802 + *
5803 + * [31] R/W flag
5804 + * [30:28] Function number
5805 + * [27] Block mode
5806 + * [26] Increment address
5807 + * [25:9] Register address
5808 + * [8:0] Byte/block count
5809 + */
5810 +
5811 +/*
5812 + SDIO status in R5
5813 + Type
5814 + e : error bit
5815 + s : status bit
5816 + r : detected and set for the actual command response
5817 + x : detected and set during command execution. the host must poll
5818 + the card by sending status command in order to read these bits.
5819 + Clear condition
5820 + a : according to the card state
5821 + b : always related to the previous command. Reception of
5822 + a valid command will clear it (with a delay of one command)
5823 + c : clear by read
5824 + */
5825 +
5826 +#define R5_COM_CRC_ERROR (1 << 15) /* er, b */
5827 +#define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
5828 +#define R5_ERROR (1 << 11) /* erx, c */
5829 +#define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
5830 +#define R5_OUT_OF_RANGE (1 << 8) /* er, c */
5831 +#define R5_STATUS(x) (x & 0xCB00)
5832 +#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
5833 +
5834 +/*
5835 + * Card Common Control Registers (CCCR)
5836 + */
5837 +
5838 +#define SDIO_CCCR_CCCR 0x00
5839 +
5840 +#define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
5841 +#define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
5842 +#define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
5843 +
5844 +#define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
5845 +#define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
5846 +#define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
5847 +#define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
5848 +
5849 +#define SDIO_CCCR_SD 0x01
5850 +
5851 +#define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
5852 +#define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
5853 +#define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
5854 +
5855 +#define SDIO_CCCR_IOEx 0x02
5856 +#define SDIO_CCCR_IORx 0x03
5857 +
5858 +#define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
5859 +#define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
5860 +
5861 +#define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
5862 +
5863 +#define SDIO_CCCR_IF 0x07 /* bus interface controls */
5864 +
5865 +#define SDIO_BUS_WIDTH_1BIT 0x00
5866 +#define SDIO_BUS_WIDTH_4BIT 0x02
5867 +
5868 +#define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
5869 +
5870 +#define SDIO_CCCR_CAPS 0x08
5871 +
5872 +#define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
5873 +#define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
5874 +#define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
5875 +#define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
5876 +#define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
5877 +#define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
5878 +#define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
5879 +#define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
5880 +
5881 +#define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
5882 +
5883 +/* Following 4 regs are valid only if SBS is set */
5884 +#define SDIO_CCCR_SUSPEND 0x0c
5885 +#define SDIO_CCCR_SELx 0x0d
5886 +#define SDIO_CCCR_EXECx 0x0e
5887 +#define SDIO_CCCR_READYx 0x0f
5888 +
5889 +#define SDIO_CCCR_BLKSIZE 0x10
5890 +
5891 +#define SDIO_CCCR_POWER 0x12
5892 +
5893 +#define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
5894 +#define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
5895 +
5896 +#define SDIO_CCCR_SPEED 0x13
5897 +
5898 +#define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
5899 +#define SDIO_SPEED_EHS 0x02 /* Enable High-Speed mode */
5900 +
5901 +/*
5902 + * Function Basic Registers (FBR)
5903 + */
5904 +
5905 +#define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
5906 +
5907 +#define SDIO_FBR_STD_IF 0x00
5908 +
5909 +#define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
5910 +#define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
5911 +
5912 +#define SDIO_FBR_STD_IF_EXT 0x01
5913 +
5914 +#define SDIO_FBR_POWER 0x02
5915 +
5916 +#define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
5917 +#define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
5918 +
5919 +#define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
5920 +
5921 +
5922 +#define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
5923 +
5924 +#define SDIO_FBR_CSA_DATA 0x0F
5925 +
5926 +#define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
5927 +
5928 +#endif
5929 +
5930 --- /dev/null
5931 +++ b/include/linux/mmc/sdio_func.h
5932 @@ -0,0 +1,153 @@
5933 +/*
5934 + * include/linux/mmc/sdio_func.h
5935 + *
5936 + * Copyright 2007 Pierre Ossman
5937 + *
5938 + * This program is free software; you can redistribute it and/or modify
5939 + * it under the terms of the GNU General Public License as published by
5940 + * the Free Software Foundation; either version 2 of the License, or (at
5941 + * your option) any later version.
5942 + */
5943 +
5944 +#ifndef MMC_SDIO_FUNC_H
5945 +#define MMC_SDIO_FUNC_H
5946 +
5947 +#include <linux/device.h>
5948 +#include <linux/mod_devicetable.h>
5949 +
5950 +struct mmc_card;
5951 +struct sdio_func;
5952 +
5953 +typedef void (sdio_irq_handler_t)(struct sdio_func *);
5954 +
5955 +/*
5956 + * SDIO function CIS tuple (unknown to the core)
5957 + */
5958 +struct sdio_func_tuple {
5959 + struct sdio_func_tuple *next;
5960 + unsigned char code;
5961 + unsigned char size;
5962 + unsigned char data[0];
5963 +};
5964 +
5965 +/*
5966 + * SDIO function devices
5967 + */
5968 +struct sdio_func {
5969 + struct mmc_card *card; /* the card this device belongs to */
5970 + struct device dev; /* the device */
5971 + sdio_irq_handler_t *irq_handler; /* IRQ callback */
5972 + unsigned int num; /* function number */
5973 +
5974 + unsigned char class; /* standard interface class */
5975 + unsigned short vendor; /* vendor id */
5976 + unsigned short device; /* device id */
5977 +
5978 + unsigned max_blksize; /* maximum block size */
5979 + unsigned cur_blksize; /* current block size */
5980 +
5981 + unsigned int state; /* function state */
5982 +#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
5983 +
5984 + u8 tmpbuf[4]; /* DMA:able scratch buffer */
5985 +
5986 + unsigned num_info; /* number of info strings */
5987 + const char **info; /* info strings */
5988 +
5989 + struct sdio_func_tuple *tuples;
5990 +};
5991 +
5992 +#define sdio_func_present(f) ((f)->state & SDIO_STATE_PRESENT)
5993 +
5994 +#define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
5995 +
5996 +#define sdio_func_id(f) ((f)->dev.bus_id)
5997 +
5998 +#define sdio_get_drvdata(f) dev_get_drvdata(&(f)->dev)
5999 +#define sdio_set_drvdata(f,d) dev_set_drvdata(&(f)->dev, d)
6000 +
6001 +/*
6002 + * SDIO function device driver
6003 + */
6004 +struct sdio_driver {
6005 + char *name;
6006 + const struct sdio_device_id *id_table;
6007 +
6008 + int (*probe)(struct sdio_func *, const struct sdio_device_id *);
6009 + void (*remove)(struct sdio_func *);
6010 +
6011 + struct device_driver drv;
6012 +};
6013 +
6014 +/**
6015 + * SDIO_DEVICE - macro used to describe a specific SDIO device
6016 + * @vend: the 16 bit manufacturer code
6017 + * @dev: the 16 bit function id
6018 + *
6019 + * This macro is used to create a struct sdio_device_id that matches a
6020 + * specific device. The class field will be set to SDIO_ANY_ID.
6021 + */
6022 +#define SDIO_DEVICE(vend,dev) \
6023 + .class = SDIO_ANY_ID, \
6024 + .vendor = (vend), .device = (dev)
6025 +
6026 +/**
6027 + * SDIO_DEVICE_CLASS - macro used to describe a specific SDIO device class
6028 + * @dev_class: the 8 bit standard interface code
6029 + *
6030 + * This macro is used to create a struct sdio_device_id that matches a
6031 + * specific standard SDIO function type. The vendor and device fields will
6032 + * be set to SDIO_ANY_ID.
6033 + */
6034 +#define SDIO_DEVICE_CLASS(dev_class) \
6035 + .class = (dev_class), \
6036 + .vendor = SDIO_ANY_ID, .device = SDIO_ANY_ID
6037 +
6038 +extern int sdio_register_driver(struct sdio_driver *);
6039 +extern void sdio_unregister_driver(struct sdio_driver *);
6040 +
6041 +/*
6042 + * SDIO I/O operations
6043 + */
6044 +extern void sdio_claim_host(struct sdio_func *func);
6045 +extern void sdio_release_host(struct sdio_func *func);
6046 +
6047 +extern int sdio_enable_func(struct sdio_func *func);
6048 +extern int sdio_disable_func(struct sdio_func *func);
6049 +
6050 +extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
6051 +
6052 +extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
6053 +extern int sdio_release_irq(struct sdio_func *func);
6054 +
6055 +extern unsigned char sdio_readb(struct sdio_func *func,
6056 + unsigned int addr, int *err_ret);
6057 +extern unsigned short sdio_readw(struct sdio_func *func,
6058 + unsigned int addr, int *err_ret);
6059 +extern unsigned long sdio_readl(struct sdio_func *func,
6060 + unsigned int addr, int *err_ret);
6061 +
6062 +extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
6063 + unsigned int addr, int count);
6064 +extern int sdio_readsb(struct sdio_func *func, void *dst,
6065 + unsigned int addr, int count);
6066 +
6067 +extern void sdio_writeb(struct sdio_func *func, unsigned char b,
6068 + unsigned int addr, int *err_ret);
6069 +extern void sdio_writew(struct sdio_func *func, unsigned short b,
6070 + unsigned int addr, int *err_ret);
6071 +extern void sdio_writel(struct sdio_func *func, unsigned long b,
6072 + unsigned int addr, int *err_ret);
6073 +
6074 +extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
6075 + void *src, int count);
6076 +extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
6077 + void *src, int count);
6078 +
6079 +extern unsigned char sdio_f0_readb(struct sdio_func *func,
6080 + unsigned int addr, int *err_ret);
6081 +extern void sdio_f0_writeb(struct sdio_func *func, unsigned char b,
6082 + unsigned int addr, int *err_ret);
6083 +
6084 +#endif
6085 +
6086 --- /dev/null
6087 +++ b/include/linux/mmc/sdio_ids.h
6088 @@ -0,0 +1,29 @@
6089 +/*
6090 + * SDIO Classes, Interface Types, Manufacturer IDs, etc.
6091 + */
6092 +
6093 +#ifndef MMC_SDIO_IDS_H
6094 +#define MMC_SDIO_IDS_H
6095 +
6096 +/*
6097 + * Standard SDIO Function Interfaces
6098 + */
6099 +
6100 +#define SDIO_CLASS_NONE 0x00 /* Not a SDIO standard interface */
6101 +#define SDIO_CLASS_UART 0x01 /* standard UART interface */
6102 +#define SDIO_CLASS_BT_A 0x02 /* Type-A BlueTooth std interface */
6103 +#define SDIO_CLASS_BT_B 0x03 /* Type-B BlueTooth std interface */
6104 +#define SDIO_CLASS_GPS 0x04 /* GPS standard interface */
6105 +#define SDIO_CLASS_CAMERA 0x05 /* Camera standard interface */
6106 +#define SDIO_CLASS_PHS 0x06 /* PHS standard interface */
6107 +#define SDIO_CLASS_WLAN 0x07 /* WLAN interface */
6108 +#define SDIO_CLASS_ATA 0x08 /* Embedded SDIO-ATA std interface */
6109 +
6110 +/*
6111 + * Vendors and devices. Sort key: vendor first, device next.
6112 + */
6113 +
6114 +#define SDIO_VENDOR_ID_MARVELL 0x02df
6115 +#define SDIO_DEVICE_ID_MARVELL_LIBERTAS 0x9103
6116 +
6117 +#endif
6118 --- a/include/linux/mod_devicetable.h
6119 +++ b/include/linux/mod_devicetable.h
6120 @@ -22,6 +22,18 @@
6121 };
6122
6123
6124 +/* SDIO */
6125 +
6126 +#define SDIO_ANY_ID (~0)
6127 +
6128 +struct sdio_device_id {
6129 + __u8 class; /* Standard interface or SDIO_ANY_ID */
6130 + __u16 vendor; /* Vendor or SDIO_ANY_ID */
6131 + __u16 device; /* Device ID or SDIO_ANY_ID */
6132 + kernel_ulong_t driver_data; /* Data private to the driver */
6133 +};
6134 +
6135 +
6136 #define IEEE1394_MATCH_VENDOR_ID 0x0001
6137 #define IEEE1394_MATCH_MODEL_ID 0x0002
6138 #define IEEE1394_MATCH_SPECIFIER_ID 0x0004
6139 --- a/drivers/mmc/card/Kconfig
6140 +++ b/drivers/mmc/card/Kconfig
6141 @@ -32,3 +32,10 @@
6142
6143 If unsure, say Y here.
6144
6145 +config SDIO_UART
6146 + tristate "SDIO UART/GPS class support"
6147 + depends on MMC
6148 + help
6149 + SDIO function driver for SDIO cards that implements the UART
6150 + class, as well as the GPS class which appears like a UART.
6151 +
6152 --- a/drivers/mmc/card/Makefile
6153 +++ b/drivers/mmc/card/Makefile
6154 @@ -9,3 +9,5 @@
6155 obj-$(CONFIG_MMC_BLOCK) += mmc_block.o
6156 mmc_block-objs := block.o queue.o
6157
6158 +obj-$(CONFIG_SDIO_UART) += sdio_uart.o
6159 +
6160 --- a/drivers/mmc/card/block.c
6161 +++ b/drivers/mmc/card/block.c
6162 @@ -44,6 +44,9 @@
6163 * max 8 partitions per card
6164 */
6165 #define MMC_SHIFT 3
6166 +#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6167 +
6168 +static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6169
6170 /*
6171 * There is one mmc_blk_data per slot.
6172 @@ -80,6 +83,9 @@
6173 mutex_lock(&open_lock);
6174 md->usage--;
6175 if (md->usage == 0) {
6176 + int devidx = md->disk->first_minor >> MMC_SHIFT;
6177 + __clear_bit(devidx, dev_use);
6178 +
6179 put_disk(md->disk);
6180 kfree(md);
6181 }
6182 @@ -151,17 +157,19 @@
6183
6184 cmd.opcode = MMC_APP_CMD;
6185 cmd.arg = card->rca << 16;
6186 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6187 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6188
6189 err = mmc_wait_for_cmd(card->host, &cmd, 0);
6190 - if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
6191 + if (err)
6192 + return (u32)-1;
6193 + if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
6194 return (u32)-1;
6195
6196 memset(&cmd, 0, sizeof(struct mmc_command));
6197
6198 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
6199 cmd.arg = 0;
6200 - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6201 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6202
6203 memset(&data, 0, sizeof(struct mmc_data));
6204
6205 @@ -192,7 +200,7 @@
6206
6207 mmc_wait_for_req(card->host, &mrq);
6208
6209 - if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
6210 + if (cmd.error || data.error)
6211 return (u32)-1;
6212
6213 blocks = ntohl(blocks);
6214 @@ -220,17 +228,15 @@
6215 brq.cmd.arg = req->sector;
6216 if (!mmc_card_blockaddr(card))
6217 brq.cmd.arg <<= 9;
6218 - brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6219 + brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6220 brq.data.blksz = 1 << md->block_bits;
6221 brq.stop.opcode = MMC_STOP_TRANSMISSION;
6222 brq.stop.arg = 0;
6223 - brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
6224 + brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
6225 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
6226 if (brq.data.blocks > card->host->max_blk_count)
6227 brq.data.blocks = card->host->max_blk_count;
6228
6229 - mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
6230 -
6231 /*
6232 * If the host doesn't support multiple block writes, force
6233 * block writes to single block. SD cards are excepted from
6234 @@ -243,8 +249,12 @@
6235 brq.data.blocks = 1;
6236
6237 if (brq.data.blocks > 1) {
6238 - brq.data.flags |= MMC_DATA_MULTI;
6239 - brq.mrq.stop = &brq.stop;
6240 + /* SPI multiblock writes terminate using a special
6241 + * token, not a STOP_TRANSMISSION request.
6242 + */
6243 + if (!mmc_host_is_spi(card->host)
6244 + || rq_data_dir(req) == READ)
6245 + brq.mrq.stop = &brq.stop;
6246 readcmd = MMC_READ_MULTIPLE_BLOCK;
6247 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
6248 } else {
6249 @@ -261,6 +271,8 @@
6250 brq.data.flags |= MMC_DATA_WRITE;
6251 }
6252
6253 + mmc_set_data_timeout(&brq.data, card);
6254 +
6255 brq.data.sg = mq->sg;
6256 brq.data.sg_len = mmc_queue_map_sg(mq);
6257
6258 @@ -302,7 +314,7 @@
6259 goto cmd_err;
6260 }
6261
6262 - if (rq_data_dir(req) != READ) {
6263 + if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
6264 do {
6265 int err;
6266
6267 @@ -315,7 +327,13 @@
6268 req->rq_disk->disk_name, err);
6269 goto cmd_err;
6270 }
6271 - } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
6272 + /*
6273 + * Some cards mishandle the status bits,
6274 + * so make sure to check both the busy
6275 + * indication and the card state.
6276 + */
6277 + } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
6278 + (R1_CURRENT_STATE(cmd.resp[0]) == 7));
6279
6280 #if 0
6281 if (cmd.resp[0] & ~0x00000900)
6282 @@ -394,9 +412,6 @@
6283 return 0;
6284 }
6285
6286 -#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6287 -
6288 -static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6289
6290 static inline int mmc_blk_readonly(struct mmc_card *card)
6291 {
6292 @@ -510,7 +525,7 @@
6293 mmc_claim_host(card->host);
6294 cmd.opcode = MMC_SET_BLOCKLEN;
6295 cmd.arg = 1 << md->block_bits;
6296 - cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6297 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6298 err = mmc_wait_for_cmd(card->host, &cmd, 5);
6299 mmc_release_host(card->host);
6300
6301 @@ -562,17 +577,12 @@
6302 struct mmc_blk_data *md = mmc_get_drvdata(card);
6303
6304 if (md) {
6305 - int devidx;
6306 -
6307 /* Stop new requests from getting into the queue */
6308 del_gendisk(md->disk);
6309
6310 /* Then flush out any already in there */
6311 mmc_cleanup_queue(&md->queue);
6312
6313 - devidx = md->disk->first_minor >> MMC_SHIFT;
6314 - __clear_bit(devidx, dev_use);
6315 -
6316 mmc_blk_put(md);
6317 }
6318 mmc_set_drvdata(card, NULL);
6319 --- a/drivers/mmc/card/queue.c
6320 +++ b/drivers/mmc/card/queue.c
6321 @@ -13,6 +13,7 @@
6322 #include <linux/blkdev.h>
6323 #include <linux/freezer.h>
6324 #include <linux/kthread.h>
6325 +#include <linux/scatterlist.h>
6326
6327 #include <linux/mmc/card.h>
6328 #include <linux/mmc/host.h>
6329 @@ -22,6 +23,12 @@
6330
6331 #define MMC_QUEUE_SUSPENDED (1 << 0)
6332
6333 +#define sg_init_table(sg, n) do { \
6334 + memset(sg, 0, sizeof(*(sg)) * (n)); \
6335 +} while (0)
6336 +
6337 +#define sg_virt(sg) (page_address((sg)->page) + (sg)->offset)
6338 +
6339 /*
6340 * Prepare a MMC request. This just filters out odd stuff.
6341 */
6342 @@ -159,6 +166,7 @@
6343 ret = -ENOMEM;
6344 goto cleanup_queue;
6345 }
6346 + sg_init_table(mq->sg, 1);
6347
6348 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
6349 bouncesz / 512, GFP_KERNEL);
6350 @@ -166,6 +174,7 @@
6351 ret = -ENOMEM;
6352 goto cleanup_queue;
6353 }
6354 + sg_init_table(mq->bounce_sg, bouncesz / 512);
6355 }
6356 }
6357 #endif
6358 @@ -183,6 +192,7 @@
6359 ret = -ENOMEM;
6360 goto cleanup_queue;
6361 }
6362 + sg_init_table(mq->sg, host->max_phys_segs);
6363 }
6364
6365 init_MUTEX(&mq->thread_sem);
6366 @@ -302,12 +312,12 @@
6367 BUG_ON(dst_len == 0);
6368
6369 if (dst_size == 0) {
6370 - dst_buf = page_address(dst->page) + dst->offset;
6371 + dst_buf = sg_virt(dst);
6372 dst_size = dst->length;
6373 }
6374
6375 if (src_size == 0) {
6376 - src_buf = page_address(src->page) + src->offset;
6377 + src_buf = sg_virt(src);
6378 src_size = src->length;
6379 }
6380
6381 @@ -353,9 +363,7 @@
6382 return 1;
6383 }
6384
6385 - mq->sg[0].page = virt_to_page(mq->bounce_buf);
6386 - mq->sg[0].offset = offset_in_page(mq->bounce_buf);
6387 - mq->sg[0].length = 0;
6388 + sg_init_one(mq->sg, mq->bounce_buf, 0);
6389
6390 while (sg_len) {
6391 mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
6392 --- /dev/null
6393 +++ b/drivers/mmc/card/sdio_uart.c
6394 @@ -0,0 +1,1158 @@
6395 +/*
6396 + * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
6397 + *
6398 + * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
6399 + * by Russell King.
6400 + *
6401 + * Author: Nicolas Pitre
6402 + * Created: June 15, 2007
6403 + * Copyright: MontaVista Software, Inc.
6404 + *
6405 + * This program is free software; you can redistribute it and/or modify
6406 + * it under the terms of the GNU General Public License as published by
6407 + * the Free Software Foundation; either version 2 of the License, or (at
6408 + * your option) any later version.
6409 + */
6410 +
6411 +/*
6412 + * Note: Although this driver assumes a 16550A-like UART implementation,
6413 + * it is not possible to leverage the common 8250/16550 driver, nor the
6414 + * core UART infrastructure, as they assumes direct access to the hardware
6415 + * registers, often under a spinlock. This is not possible in the SDIO
6416 + * context as SDIO access functions must be able to sleep.
6417 + *
6418 + * Because we need to lock the SDIO host to ensure an exclusive access to
6419 + * the card, we simply rely on that lock to also prevent and serialize
6420 + * concurrent access to the same port.
6421 + */
6422 +
6423 +#include <linux/module.h>
6424 +#include <linux/init.h>
6425 +#include <linux/kernel.h>
6426 +#include <linux/mutex.h>
6427 +#include <linux/serial_reg.h>
6428 +#include <linux/circ_buf.h>
6429 +#include <linux/gfp.h>
6430 +#include <linux/tty.h>
6431 +#include <linux/tty_flip.h>
6432 +
6433 +#include <linux/mmc/core.h>
6434 +#include <linux/mmc/card.h>
6435 +#include <linux/mmc/sdio_func.h>
6436 +#include <linux/mmc/sdio_ids.h>
6437 +
6438 +
6439 +#define UART_NR 8 /* Number of UARTs this driver can handle */
6440 +
6441 +
6442 +#define UART_XMIT_SIZE PAGE_SIZE
6443 +#define WAKEUP_CHARS 256
6444 +
6445 +#define circ_empty(circ) ((circ)->head == (circ)->tail)
6446 +#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
6447 +
6448 +#define circ_chars_pending(circ) \
6449 + (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6450 +
6451 +#define circ_chars_free(circ) \
6452 + (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6453 +
6454 +
6455 +struct uart_icount {
6456 + __u32 cts;
6457 + __u32 dsr;
6458 + __u32 rng;
6459 + __u32 dcd;
6460 + __u32 rx;
6461 + __u32 tx;
6462 + __u32 frame;
6463 + __u32 overrun;
6464 + __u32 parity;
6465 + __u32 brk;
6466 +};
6467 +
6468 +struct sdio_uart_port {
6469 + struct kref kref;
6470 + struct tty_struct *tty;
6471 + unsigned int index;
6472 + unsigned int opened;
6473 + struct mutex open_lock;
6474 + struct sdio_func *func;
6475 + struct mutex func_lock;
6476 + struct task_struct *in_sdio_uart_irq;
6477 + unsigned int regs_offset;
6478 + struct circ_buf xmit;
6479 + spinlock_t write_lock;
6480 + struct uart_icount icount;
6481 + unsigned int uartclk;
6482 + unsigned int mctrl;
6483 + unsigned int read_status_mask;
6484 + unsigned int ignore_status_mask;
6485 + unsigned char x_char;
6486 + unsigned char ier;
6487 + unsigned char lcr;
6488 +};
6489 +
6490 +static struct sdio_uart_port *sdio_uart_table[UART_NR];
6491 +static DEFINE_SPINLOCK(sdio_uart_table_lock);
6492 +
6493 +static int sdio_uart_add_port(struct sdio_uart_port *port)
6494 +{
6495 + int index, ret = -EBUSY;
6496 +
6497 + kref_init(&port->kref);
6498 + mutex_init(&port->open_lock);
6499 + mutex_init(&port->func_lock);
6500 + spin_lock_init(&port->write_lock);
6501 +
6502 + spin_lock(&sdio_uart_table_lock);
6503 + for (index = 0; index < UART_NR; index++) {
6504 + if (!sdio_uart_table[index]) {
6505 + port->index = index;
6506 + sdio_uart_table[index] = port;
6507 + ret = 0;
6508 + break;
6509 + }
6510 + }
6511 + spin_unlock(&sdio_uart_table_lock);
6512 +
6513 + return ret;
6514 +}
6515 +
6516 +static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
6517 +{
6518 + struct sdio_uart_port *port;
6519 +
6520 + if (index >= UART_NR)
6521 + return NULL;
6522 +
6523 + spin_lock(&sdio_uart_table_lock);
6524 + port = sdio_uart_table[index];
6525 + if (port)
6526 + kref_get(&port->kref);
6527 + spin_unlock(&sdio_uart_table_lock);
6528 +
6529 + return port;
6530 +}
6531 +
6532 +static void sdio_uart_port_destroy(struct kref *kref)
6533 +{
6534 + struct sdio_uart_port *port =
6535 + container_of(kref, struct sdio_uart_port, kref);
6536 + kfree(port);
6537 +}
6538 +
6539 +static void sdio_uart_port_put(struct sdio_uart_port *port)
6540 +{
6541 + kref_put(&port->kref, sdio_uart_port_destroy);
6542 +}
6543 +
6544 +static void sdio_uart_port_remove(struct sdio_uart_port *port)
6545 +{
6546 + struct sdio_func *func;
6547 +
6548 + BUG_ON(sdio_uart_table[port->index] != port);
6549 +
6550 + spin_lock(&sdio_uart_table_lock);
6551 + sdio_uart_table[port->index] = NULL;
6552 + spin_unlock(&sdio_uart_table_lock);
6553 +
6554 + /*
6555 + * We're killing a port that potentially still is in use by
6556 + * the tty layer. Be careful to prevent any further access
6557 + * to the SDIO function and arrange for the tty layer to
6558 + * give up on that port ASAP.
6559 + * Beware: the lock ordering is critical.
6560 + */
6561 + mutex_lock(&port->open_lock);
6562 + mutex_lock(&port->func_lock);
6563 + func = port->func;
6564 + sdio_claim_host(func);
6565 + port->func = NULL;
6566 + mutex_unlock(&port->func_lock);
6567 + if (port->opened)
6568 + tty_hangup(port->tty);
6569 + mutex_unlock(&port->open_lock);
6570 + sdio_release_irq(func);
6571 + sdio_disable_func(func);
6572 + sdio_release_host(func);
6573 +
6574 + sdio_uart_port_put(port);
6575 +}
6576 +
6577 +static int sdio_uart_claim_func(struct sdio_uart_port *port)
6578 +{
6579 + mutex_lock(&port->func_lock);
6580 + if (unlikely(!port->func)) {
6581 + mutex_unlock(&port->func_lock);
6582 + return -ENODEV;
6583 + }
6584 + if (likely(port->in_sdio_uart_irq != current))
6585 + sdio_claim_host(port->func);
6586 + mutex_unlock(&port->func_lock);
6587 + return 0;
6588 +}
6589 +
6590 +static inline void sdio_uart_release_func(struct sdio_uart_port *port)
6591 +{
6592 + if (likely(port->in_sdio_uart_irq != current))
6593 + sdio_release_host(port->func);
6594 +}
6595 +
6596 +static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
6597 +{
6598 + unsigned char c;
6599 + c = sdio_readb(port->func, port->regs_offset + offset, NULL);
6600 + return c;
6601 +}
6602 +
6603 +static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
6604 +{
6605 + sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
6606 +}
6607 +
6608 +static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
6609 +{
6610 + unsigned char status;
6611 + unsigned int ret;
6612 +
6613 + status = sdio_in(port, UART_MSR);
6614 +
6615 + ret = 0;
6616 + if (status & UART_MSR_DCD)
6617 + ret |= TIOCM_CAR;
6618 + if (status & UART_MSR_RI)
6619 + ret |= TIOCM_RNG;
6620 + if (status & UART_MSR_DSR)
6621 + ret |= TIOCM_DSR;
6622 + if (status & UART_MSR_CTS)
6623 + ret |= TIOCM_CTS;
6624 + return ret;
6625 +}
6626 +
6627 +static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
6628 +{
6629 + unsigned char mcr = 0;
6630 +
6631 + if (mctrl & TIOCM_RTS)
6632 + mcr |= UART_MCR_RTS;
6633 + if (mctrl & TIOCM_DTR)
6634 + mcr |= UART_MCR_DTR;
6635 + if (mctrl & TIOCM_OUT1)
6636 + mcr |= UART_MCR_OUT1;
6637 + if (mctrl & TIOCM_OUT2)
6638 + mcr |= UART_MCR_OUT2;
6639 + if (mctrl & TIOCM_LOOP)
6640 + mcr |= UART_MCR_LOOP;
6641 +
6642 + sdio_out(port, UART_MCR, mcr);
6643 +}
6644 +
6645 +static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
6646 + unsigned int set, unsigned int clear)
6647 +{
6648 + unsigned int old;
6649 +
6650 + old = port->mctrl;
6651 + port->mctrl = (old & ~clear) | set;
6652 + if (old != port->mctrl)
6653 + sdio_uart_write_mctrl(port, port->mctrl);
6654 +}
6655 +
6656 +#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
6657 +#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
6658 +
6659 +static void sdio_uart_change_speed(struct sdio_uart_port *port,
6660 + struct ktermios *termios,
6661 + struct ktermios *old)
6662 +{
6663 + unsigned char cval, fcr = 0;
6664 + unsigned int baud, quot;
6665 +
6666 + switch (termios->c_cflag & CSIZE) {
6667 + case CS5:
6668 + cval = UART_LCR_WLEN5;
6669 + break;
6670 + case CS6:
6671 + cval = UART_LCR_WLEN6;
6672 + break;
6673 + case CS7:
6674 + cval = UART_LCR_WLEN7;
6675 + break;
6676 + default:
6677 + case CS8:
6678 + cval = UART_LCR_WLEN8;
6679 + break;
6680 + }
6681 +
6682 + if (termios->c_cflag & CSTOPB)
6683 + cval |= UART_LCR_STOP;
6684 + if (termios->c_cflag & PARENB)
6685 + cval |= UART_LCR_PARITY;
6686 + if (!(termios->c_cflag & PARODD))
6687 + cval |= UART_LCR_EPAR;
6688 +
6689 + for (;;) {
6690 + baud = tty_termios_baud_rate(termios);
6691 + if (baud == 0)
6692 + baud = 9600; /* Special case: B0 rate. */
6693 + if (baud <= port->uartclk)
6694 + break;
6695 + /*
6696 + * Oops, the quotient was zero. Try again with the old
6697 + * baud rate if possible, otherwise default to 9600.
6698 + */
6699 + termios->c_cflag &= ~CBAUD;
6700 + if (old) {
6701 + termios->c_cflag |= old->c_cflag & CBAUD;
6702 + old = NULL;
6703 + } else
6704 + termios->c_cflag |= B9600;
6705 + }
6706 + quot = (2 * port->uartclk + baud) / (2 * baud);
6707 +
6708 + if (baud < 2400)
6709 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
6710 + else
6711 + fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
6712 +
6713 + port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
6714 + if (termios->c_iflag & INPCK)
6715 + port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
6716 + if (termios->c_iflag & (BRKINT | PARMRK))
6717 + port->read_status_mask |= UART_LSR_BI;
6718 +
6719 + /*
6720 + * Characters to ignore
6721 + */
6722 + port->ignore_status_mask = 0;
6723 + if (termios->c_iflag & IGNPAR)
6724 + port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
6725 + if (termios->c_iflag & IGNBRK) {
6726 + port->ignore_status_mask |= UART_LSR_BI;
6727 + /*
6728 + * If we're ignoring parity and break indicators,
6729 + * ignore overruns too (for real raw support).
6730 + */
6731 + if (termios->c_iflag & IGNPAR)
6732 + port->ignore_status_mask |= UART_LSR_OE;
6733 + }
6734 +
6735 + /*
6736 + * ignore all characters if CREAD is not set
6737 + */
6738 + if ((termios->c_cflag & CREAD) == 0)
6739 + port->ignore_status_mask |= UART_LSR_DR;
6740 +
6741 + /*
6742 + * CTS flow control flag and modem status interrupts
6743 + */
6744 + port->ier &= ~UART_IER_MSI;
6745 + if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
6746 + port->ier |= UART_IER_MSI;
6747 +
6748 + port->lcr = cval;
6749 +
6750 + sdio_out(port, UART_IER, port->ier);
6751 + sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
6752 + sdio_out(port, UART_DLL, quot & 0xff);
6753 + sdio_out(port, UART_DLM, quot >> 8);
6754 + sdio_out(port, UART_LCR, cval);
6755 + sdio_out(port, UART_FCR, fcr);
6756 +
6757 + sdio_uart_write_mctrl(port, port->mctrl);
6758 +}
6759 +
6760 +static void sdio_uart_start_tx(struct sdio_uart_port *port)
6761 +{
6762 + if (!(port->ier & UART_IER_THRI)) {
6763 + port->ier |= UART_IER_THRI;
6764 + sdio_out(port, UART_IER, port->ier);
6765 + }
6766 +}
6767 +
6768 +static void sdio_uart_stop_tx(struct sdio_uart_port *port)
6769 +{
6770 + if (port->ier & UART_IER_THRI) {
6771 + port->ier &= ~UART_IER_THRI;
6772 + sdio_out(port, UART_IER, port->ier);
6773 + }
6774 +}
6775 +
6776 +static void sdio_uart_stop_rx(struct sdio_uart_port *port)
6777 +{
6778 + port->ier &= ~UART_IER_RLSI;
6779 + port->read_status_mask &= ~UART_LSR_DR;
6780 + sdio_out(port, UART_IER, port->ier);
6781 +}
6782 +
6783 +static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
6784 +{
6785 + struct tty_struct *tty = port->tty;
6786 + unsigned int ch, flag;
6787 + int max_count = 256;
6788 +
6789 + do {
6790 + ch = sdio_in(port, UART_RX);
6791 + flag = TTY_NORMAL;
6792 + port->icount.rx++;
6793 +
6794 + if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
6795 + UART_LSR_FE | UART_LSR_OE))) {
6796 + /*
6797 + * For statistics only
6798 + */
6799 + if (*status & UART_LSR_BI) {
6800 + *status &= ~(UART_LSR_FE | UART_LSR_PE);
6801 + port->icount.brk++;
6802 + } else if (*status & UART_LSR_PE)
6803 + port->icount.parity++;
6804 + else if (*status & UART_LSR_FE)
6805 + port->icount.frame++;
6806 + if (*status & UART_LSR_OE)
6807 + port->icount.overrun++;
6808 +
6809 + /*
6810 + * Mask off conditions which should be ignored.
6811 + */
6812 + *status &= port->read_status_mask;
6813 + if (*status & UART_LSR_BI) {
6814 + flag = TTY_BREAK;
6815 + } else if (*status & UART_LSR_PE)
6816 + flag = TTY_PARITY;
6817 + else if (*status & UART_LSR_FE)
6818 + flag = TTY_FRAME;
6819 + }
6820 +
6821 + if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
6822 + tty_insert_flip_char(tty, ch, flag);
6823 +
6824 + /*
6825 + * Overrun is special. Since it's reported immediately,
6826 + * it doesn't affect the current character.
6827 + */
6828 + if (*status & ~port->ignore_status_mask & UART_LSR_OE)
6829 + tty_insert_flip_char(tty, 0, TTY_OVERRUN);
6830 +
6831 + *status = sdio_in(port, UART_LSR);
6832 + } while ((*status & UART_LSR_DR) && (max_count-- > 0));
6833 + tty_flip_buffer_push(tty);
6834 +}
6835 +
6836 +static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
6837 +{
6838 + struct circ_buf *xmit = &port->xmit;
6839 + int count;
6840 +
6841 + if (port->x_char) {
6842 + sdio_out(port, UART_TX, port->x_char);
6843 + port->icount.tx++;
6844 + port->x_char = 0;
6845 + return;
6846 + }
6847 + if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
6848 + sdio_uart_stop_tx(port);
6849 + return;
6850 + }
6851 +
6852 + count = 16;
6853 + do {
6854 + sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
6855 + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
6856 + port->icount.tx++;
6857 + if (circ_empty(xmit))
6858 + break;
6859 + } while (--count > 0);
6860 +
6861 + if (circ_chars_pending(xmit) < WAKEUP_CHARS)
6862 + tty_wakeup(port->tty);
6863 +
6864 + if (circ_empty(xmit))
6865 + sdio_uart_stop_tx(port);
6866 +}
6867 +
6868 +static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
6869 +{
6870 + int status;
6871 +
6872 + status = sdio_in(port, UART_MSR);
6873 +
6874 + if ((status & UART_MSR_ANY_DELTA) == 0)
6875 + return;
6876 +
6877 + if (status & UART_MSR_TERI)
6878 + port->icount.rng++;
6879 + if (status & UART_MSR_DDSR)
6880 + port->icount.dsr++;
6881 + if (status & UART_MSR_DDCD)
6882 + port->icount.dcd++;
6883 + if (status & UART_MSR_DCTS) {
6884 + port->icount.cts++;
6885 + if (port->tty->termios->c_cflag & CRTSCTS) {
6886 + int cts = (status & UART_MSR_CTS);
6887 + if (port->tty->hw_stopped) {
6888 + if (cts) {
6889 + port->tty->hw_stopped = 0;
6890 + sdio_uart_start_tx(port);
6891 + tty_wakeup(port->tty);
6892 + }
6893 + } else {
6894 + if (!cts) {
6895 + port->tty->hw_stopped = 1;
6896 + sdio_uart_stop_tx(port);
6897 + }
6898 + }
6899 + }
6900 + }
6901 +}
6902 +
6903 +/*
6904 + * This handles the interrupt from one port.
6905 + */
6906 +static void sdio_uart_irq(struct sdio_func *func)
6907 +{
6908 + struct sdio_uart_port *port = sdio_get_drvdata(func);
6909 + unsigned int iir, lsr;
6910 +
6911 + /*
6912 + * In a few places sdio_uart_irq() is called directly instead of
6913 + * waiting for the actual interrupt to be raised and the SDIO IRQ
6914 + * thread scheduled in order to reduce latency. However, some
6915 + * interaction with the tty core may end up calling us back
6916 + * (serial echo, flow control, etc.) through those same places
6917 + * causing undesirable effects. Let's stop the recursion here.
6918 + */
6919 + if (unlikely(port->in_sdio_uart_irq == current))
6920 + return;
6921 +
6922 + iir = sdio_in(port, UART_IIR);
6923 + if (iir & UART_IIR_NO_INT)
6924 + return;
6925 +
6926 + port->in_sdio_uart_irq = current;
6927 + lsr = sdio_in(port, UART_LSR);
6928 + if (lsr & UART_LSR_DR)
6929 + sdio_uart_receive_chars(port, &lsr);
6930 + sdio_uart_check_modem_status(port);
6931 + if (lsr & UART_LSR_THRE)
6932 + sdio_uart_transmit_chars(port);
6933 + port->in_sdio_uart_irq = NULL;
6934 +}
6935 +
6936 +static int sdio_uart_startup(struct sdio_uart_port *port)
6937 +{
6938 + unsigned long page;
6939 + int ret;
6940 +
6941 + /*
6942 + * Set the TTY IO error marker - we will only clear this
6943 + * once we have successfully opened the port.
6944 + */
6945 + set_bit(TTY_IO_ERROR, &port->tty->flags);
6946 +
6947 + /* Initialise and allocate the transmit buffer. */
6948 + page = __get_free_page(GFP_KERNEL);
6949 + if (!page)
6950 + return -ENOMEM;
6951 + port->xmit.buf = (unsigned char *)page;
6952 + circ_clear(&port->xmit);
6953 +
6954 + ret = sdio_uart_claim_func(port);
6955 + if (ret)
6956 + goto err1;
6957 + ret = sdio_enable_func(port->func);
6958 + if (ret)
6959 + goto err2;
6960 + ret = sdio_claim_irq(port->func, sdio_uart_irq);
6961 + if (ret)
6962 + goto err3;
6963 +
6964 + /*
6965 + * Clear the FIFO buffers and disable them.
6966 + * (they will be reenabled in sdio_change_speed())
6967 + */
6968 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
6969 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
6970 + UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
6971 + sdio_out(port, UART_FCR, 0);
6972 +
6973 + /*
6974 + * Clear the interrupt registers.
6975 + */
6976 + (void) sdio_in(port, UART_LSR);
6977 + (void) sdio_in(port, UART_RX);
6978 + (void) sdio_in(port, UART_IIR);
6979 + (void) sdio_in(port, UART_MSR);
6980 +
6981 + /*
6982 + * Now, initialize the UART
6983 + */
6984 + sdio_out(port, UART_LCR, UART_LCR_WLEN8);
6985 +
6986 + port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
6987 + port->mctrl = TIOCM_OUT2;
6988 +
6989 + sdio_uart_change_speed(port, port->tty->termios, NULL);
6990 +
6991 + if (port->tty->termios->c_cflag & CBAUD)
6992 + sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
6993 +
6994 + if (port->tty->termios->c_cflag & CRTSCTS)
6995 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
6996 + port->tty->hw_stopped = 1;
6997 +
6998 + clear_bit(TTY_IO_ERROR, &port->tty->flags);
6999 +
7000 + /* Kick the IRQ handler once while we're still holding the host lock */
7001 + sdio_uart_irq(port->func);
7002 +
7003 + sdio_uart_release_func(port);
7004 + return 0;
7005 +
7006 +err3:
7007 + sdio_disable_func(port->func);
7008 +err2:
7009 + sdio_uart_release_func(port);
7010 +err1:
7011 + free_page((unsigned long)port->xmit.buf);
7012 + return ret;
7013 +}
7014 +
7015 +static void sdio_uart_shutdown(struct sdio_uart_port *port)
7016 +{
7017 + int ret;
7018 +
7019 + ret = sdio_uart_claim_func(port);
7020 + if (ret)
7021 + goto skip;
7022 +
7023 + sdio_uart_stop_rx(port);
7024 +
7025 + /* TODO: wait here for TX FIFO to drain */
7026 +
7027 + /* Turn off DTR and RTS early. */
7028 + if (port->tty->termios->c_cflag & HUPCL)
7029 + sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
7030 +
7031 + /* Disable interrupts from this port */
7032 + sdio_release_irq(port->func);
7033 + port->ier = 0;
7034 + sdio_out(port, UART_IER, 0);
7035 +
7036 + sdio_uart_clear_mctrl(port, TIOCM_OUT2);
7037 +
7038 + /* Disable break condition and FIFOs. */
7039 + port->lcr &= ~UART_LCR_SBC;
7040 + sdio_out(port, UART_LCR, port->lcr);
7041 + sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7042 + UART_FCR_CLEAR_RCVR |
7043 + UART_FCR_CLEAR_XMIT);
7044 + sdio_out(port, UART_FCR, 0);
7045 +
7046 + sdio_disable_func(port->func);
7047 +
7048 + sdio_uart_release_func(port);
7049 +
7050 +skip:
7051 + /* Free the transmit buffer page. */
7052 + free_page((unsigned long)port->xmit.buf);
7053 +}
7054 +
7055 +static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
7056 +{
7057 + struct sdio_uart_port *port;
7058 + int ret;
7059 +
7060 + port = sdio_uart_port_get(tty->index);
7061 + if (!port)
7062 + return -ENODEV;
7063 +
7064 + mutex_lock(&port->open_lock);
7065 +
7066 + /*
7067 + * Make sure not to mess up with a dead port
7068 + * which has not been closed yet.
7069 + */
7070 + if (tty->driver_data && tty->driver_data != port) {
7071 + mutex_unlock(&port->open_lock);
7072 + sdio_uart_port_put(port);
7073 + return -EBUSY;
7074 + }
7075 +
7076 + if (!port->opened) {
7077 + tty->driver_data = port;
7078 + port->tty = tty;
7079 + ret = sdio_uart_startup(port);
7080 + if (ret) {
7081 + tty->driver_data = NULL;
7082 + port->tty = NULL;
7083 + mutex_unlock(&port->open_lock);
7084 + sdio_uart_port_put(port);
7085 + return ret;
7086 + }
7087 + }
7088 + port->opened++;
7089 + mutex_unlock(&port->open_lock);
7090 + return 0;
7091 +}
7092 +
7093 +static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
7094 +{
7095 + struct sdio_uart_port *port = tty->driver_data;
7096 +
7097 + if (!port)
7098 + return;
7099 +
7100 + mutex_lock(&port->open_lock);
7101 + BUG_ON(!port->opened);
7102 +
7103 + /*
7104 + * This is messy. The tty layer calls us even when open()
7105 + * returned an error. Ignore this close request if tty->count
7106 + * is larger than port->count.
7107 + */
7108 + if (tty->count > port->opened) {
7109 + mutex_unlock(&port->open_lock);
7110 + return;
7111 + }
7112 +
7113 + if (--port->opened == 0) {
7114 + tty->closing = 1;
7115 + sdio_uart_shutdown(port);
7116 + tty_ldisc_flush(tty);
7117 + port->tty = NULL;
7118 + tty->driver_data = NULL;
7119 + tty->closing = 0;
7120 + }
7121 + mutex_unlock(&port->open_lock);
7122 + sdio_uart_port_put(port);
7123 +}
7124 +
7125 +static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
7126 + int count)
7127 +{
7128 + struct sdio_uart_port *port = tty->driver_data;
7129 + struct circ_buf *circ = &port->xmit;
7130 + int c, ret = 0;
7131 +
7132 + if (!port->func)
7133 + return -ENODEV;
7134 +
7135 + spin_lock(&port->write_lock);
7136 + while (1) {
7137 + c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
7138 + if (count < c)
7139 + c = count;
7140 + if (c <= 0)
7141 + break;
7142 + memcpy(circ->buf + circ->head, buf, c);
7143 + circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
7144 + buf += c;
7145 + count -= c;
7146 + ret += c;
7147 + }
7148 + spin_unlock(&port->write_lock);
7149 +
7150 + if ( !(port->ier & UART_IER_THRI)) {
7151 + int err = sdio_uart_claim_func(port);
7152 + if (!err) {
7153 + sdio_uart_start_tx(port);
7154 + sdio_uart_irq(port->func);
7155 + sdio_uart_release_func(port);
7156 + } else
7157 + ret = err;
7158 + }
7159 +
7160 + return ret;
7161 +}
7162 +
7163 +static int sdio_uart_write_room(struct tty_struct *tty)
7164 +{
7165 + struct sdio_uart_port *port = tty->driver_data;
7166 + return port ? circ_chars_free(&port->xmit) : 0;
7167 +}
7168 +
7169 +static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
7170 +{
7171 + struct sdio_uart_port *port = tty->driver_data;
7172 + return port ? circ_chars_pending(&port->xmit) : 0;
7173 +}
7174 +
7175 +static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
7176 +{
7177 + struct sdio_uart_port *port = tty->driver_data;
7178 +
7179 + port->x_char = ch;
7180 + if (ch && !(port->ier & UART_IER_THRI)) {
7181 + if (sdio_uart_claim_func(port) != 0)
7182 + return;
7183 + sdio_uart_start_tx(port);
7184 + sdio_uart_irq(port->func);
7185 + sdio_uart_release_func(port);
7186 + }
7187 +}
7188 +
7189 +static void sdio_uart_throttle(struct tty_struct *tty)
7190 +{
7191 + struct sdio_uart_port *port = tty->driver_data;
7192 +
7193 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7194 + return;
7195 +
7196 + if (sdio_uart_claim_func(port) != 0)
7197 + return;
7198 +
7199 + if (I_IXOFF(tty)) {
7200 + port->x_char = STOP_CHAR(tty);
7201 + sdio_uart_start_tx(port);
7202 + }
7203 +
7204 + if (tty->termios->c_cflag & CRTSCTS)
7205 + sdio_uart_clear_mctrl(port, TIOCM_RTS);
7206 +
7207 + sdio_uart_irq(port->func);
7208 + sdio_uart_release_func(port);
7209 +}
7210 +
7211 +static void sdio_uart_unthrottle(struct tty_struct *tty)
7212 +{
7213 + struct sdio_uart_port *port = tty->driver_data;
7214 +
7215 + if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7216 + return;
7217 +
7218 + if (sdio_uart_claim_func(port) != 0)
7219 + return;
7220 +
7221 + if (I_IXOFF(tty)) {
7222 + if (port->x_char) {
7223 + port->x_char = 0;
7224 + } else {
7225 + port->x_char = START_CHAR(tty);
7226 + sdio_uart_start_tx(port);
7227 + }
7228 + }
7229 +
7230 + if (tty->termios->c_cflag & CRTSCTS)
7231 + sdio_uart_set_mctrl(port, TIOCM_RTS);
7232 +
7233 + sdio_uart_irq(port->func);
7234 + sdio_uart_release_func(port);
7235 +}
7236 +
7237 +static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
7238 +{
7239 + struct sdio_uart_port *port = tty->driver_data;
7240 + unsigned int cflag = tty->termios->c_cflag;
7241 +
7242 +#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
7243 +
7244 + if ((cflag ^ old_termios->c_cflag) == 0 &&
7245 + RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
7246 + return;
7247 +
7248 + if (sdio_uart_claim_func(port) != 0)
7249 + return;
7250 +
7251 + sdio_uart_change_speed(port, tty->termios, old_termios);
7252 +
7253 + /* Handle transition to B0 status */
7254 + if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
7255 + sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7256 +
7257 + /* Handle transition away from B0 status */
7258 + if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
7259 + unsigned int mask = TIOCM_DTR;
7260 + if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
7261 + mask |= TIOCM_RTS;
7262 + sdio_uart_set_mctrl(port, mask);
7263 + }
7264 +
7265 + /* Handle turning off CRTSCTS */
7266 + if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
7267 + tty->hw_stopped = 0;
7268 + sdio_uart_start_tx(port);
7269 + }
7270 +
7271 + /* Handle turning on CRTSCTS */
7272 + if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
7273 + if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
7274 + tty->hw_stopped = 1;
7275 + sdio_uart_stop_tx(port);
7276 + }
7277 + }
7278 +
7279 + sdio_uart_release_func(port);
7280 +}
7281 +
7282 +static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
7283 +{
7284 + struct sdio_uart_port *port = tty->driver_data;
7285 +
7286 + if (sdio_uart_claim_func(port) != 0)
7287 + return;
7288 +
7289 + if (break_state == -1)
7290 + port->lcr |= UART_LCR_SBC;
7291 + else
7292 + port->lcr &= ~UART_LCR_SBC;
7293 + sdio_out(port, UART_LCR, port->lcr);
7294 +
7295 + sdio_uart_release_func(port);
7296 +}
7297 +
7298 +static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
7299 +{
7300 + struct sdio_uart_port *port = tty->driver_data;
7301 + int result;
7302 +
7303 + result = sdio_uart_claim_func(port);
7304 + if (!result) {
7305 + result = port->mctrl | sdio_uart_get_mctrl(port);
7306 + sdio_uart_release_func(port);
7307 + }
7308 +
7309 + return result;
7310 +}
7311 +
7312 +static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
7313 + unsigned int set, unsigned int clear)
7314 +{
7315 + struct sdio_uart_port *port = tty->driver_data;
7316 + int result;
7317 +
7318 + result =sdio_uart_claim_func(port);
7319 + if(!result) {
7320 + sdio_uart_update_mctrl(port, set, clear);
7321 + sdio_uart_release_func(port);
7322 + }
7323 +
7324 + return result;
7325 +}
7326 +
7327 +static int sdio_uart_read_proc(char *page, char **start, off_t off,
7328 + int count, int *eof, void *data)
7329 +{
7330 + int i, len = 0;
7331 + off_t begin = 0;
7332 +
7333 + len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
7334 + "", "", "");
7335 + for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
7336 + struct sdio_uart_port *port = sdio_uart_port_get(i);
7337 + if (port) {
7338 + len += sprintf(page+len, "%d: uart:SDIO", i);
7339 + if(capable(CAP_SYS_ADMIN)) {
7340 + len += sprintf(page + len, " tx:%d rx:%d",
7341 + port->icount.tx, port->icount.rx);
7342 + if (port->icount.frame)
7343 + len += sprintf(page + len, " fe:%d",
7344 + port->icount.frame);
7345 + if (port->icount.parity)
7346 + len += sprintf(page + len, " pe:%d",
7347 + port->icount.parity);
7348 + if (port->icount.brk)
7349 + len += sprintf(page + len, " brk:%d",
7350 + port->icount.brk);
7351 + if (port->icount.overrun)
7352 + len += sprintf(page + len, " oe:%d",
7353 + port->icount.overrun);
7354 + if (port->icount.cts)
7355 + len += sprintf(page + len, " cts:%d",
7356 + port->icount.cts);
7357 + if (port->icount.dsr)
7358 + len += sprintf(page + len, " dsr:%d",
7359 + port->icount.dsr);
7360 + if (port->icount.rng)
7361 + len += sprintf(page + len, " rng:%d",
7362 + port->icount.rng);
7363 + if (port->icount.dcd)
7364 + len += sprintf(page + len, " dcd:%d",
7365 + port->icount.dcd);
7366 + }
7367 + strcat(page, "\n");
7368 + len++;
7369 + sdio_uart_port_put(port);
7370 + }
7371 +
7372 + if (len + begin > off + count)
7373 + goto done;
7374 + if (len + begin < off) {
7375 + begin += len;
7376 + len = 0;
7377 + }
7378 + }
7379 + *eof = 1;
7380 +
7381 +done:
7382 + if (off >= len + begin)
7383 + return 0;
7384 + *start = page + (off - begin);
7385 + return (count < begin + len - off) ? count : (begin + len - off);
7386 +}
7387 +
7388 +static const struct tty_operations sdio_uart_ops = {
7389 + .open = sdio_uart_open,
7390 + .close = sdio_uart_close,
7391 + .write = sdio_uart_write,
7392 + .write_room = sdio_uart_write_room,
7393 + .chars_in_buffer = sdio_uart_chars_in_buffer,
7394 + .send_xchar = sdio_uart_send_xchar,
7395 + .throttle = sdio_uart_throttle,
7396 + .unthrottle = sdio_uart_unthrottle,
7397 + .set_termios = sdio_uart_set_termios,
7398 + .break_ctl = sdio_uart_break_ctl,
7399 + .tiocmget = sdio_uart_tiocmget,
7400 + .tiocmset = sdio_uart_tiocmset,
7401 + .read_proc = sdio_uart_read_proc,
7402 +};
7403 +
7404 +static struct tty_driver *sdio_uart_tty_driver;
7405 +
7406 +static int sdio_uart_probe(struct sdio_func *func,
7407 + const struct sdio_device_id *id)
7408 +{
7409 + struct sdio_uart_port *port;
7410 + int ret;
7411 +
7412 + port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
7413 + if (!port)
7414 + return -ENOMEM;
7415 +
7416 + if (func->class == SDIO_CLASS_UART) {
7417 + printk(KERN_WARNING "%s: need info on UART class basic setup\n",
7418 + sdio_func_id(func));
7419 + kfree(port);
7420 + return -ENOSYS;
7421 + } else if (func->class == SDIO_CLASS_GPS) {
7422 + /*
7423 + * We need tuple 0x91. It contains SUBTPL_SIOREG
7424 + * and SUBTPL_RCVCAPS.
7425 + */
7426 + struct sdio_func_tuple *tpl;
7427 + for (tpl = func->tuples; tpl; tpl = tpl->next) {
7428 + if (tpl->code != 0x91)
7429 + continue;
7430 + if (tpl->size < 10)
7431 + continue;
7432 + if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
7433 + break;
7434 + }
7435 + if (!tpl) {
7436 + printk(KERN_WARNING
7437 + "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
7438 + sdio_func_id(func));
7439 + kfree(port);
7440 + return -EINVAL;
7441 + }
7442 + printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
7443 + sdio_func_id(func), tpl->data[2], tpl->data[3]);
7444 + port->regs_offset = (tpl->data[4] << 0) |
7445 + (tpl->data[5] << 8) |
7446 + (tpl->data[6] << 16);
7447 + printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
7448 + sdio_func_id(func), port->regs_offset);
7449 + port->uartclk = tpl->data[7] * 115200;
7450 + if (port->uartclk == 0)
7451 + port->uartclk = 115200;
7452 + printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
7453 + sdio_func_id(func), port->uartclk,
7454 + tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
7455 + } else {
7456 + kfree(port);
7457 + return -EINVAL;
7458 + }
7459 +
7460 + port->func = func;
7461 + sdio_set_drvdata(func, port);
7462 +
7463 + ret = sdio_uart_add_port(port);
7464 + if (ret) {
7465 + kfree(port);
7466 + } else {
7467 + struct device *dev;
7468 + dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
7469 + if (IS_ERR(dev)) {
7470 + sdio_uart_port_remove(port);
7471 + ret = PTR_ERR(dev);
7472 + }
7473 + }
7474 +
7475 + return ret;
7476 +}
7477 +
7478 +static void sdio_uart_remove(struct sdio_func *func)
7479 +{
7480 + struct sdio_uart_port *port = sdio_get_drvdata(func);
7481 +
7482 + tty_unregister_device(sdio_uart_tty_driver, port->index);
7483 + sdio_uart_port_remove(port);
7484 +}
7485 +
7486 +static const struct sdio_device_id sdio_uart_ids[] = {
7487 + { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
7488 + { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
7489 + { /* end: all zeroes */ },
7490 +};
7491 +
7492 +MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
7493 +
7494 +static struct sdio_driver sdio_uart_driver = {
7495 + .probe = sdio_uart_probe,
7496 + .remove = sdio_uart_remove,
7497 + .name = "sdio_uart",
7498 + .id_table = sdio_uart_ids,
7499 +};
7500 +
7501 +static int __init sdio_uart_init(void)
7502 +{
7503 + int ret;
7504 + struct tty_driver *tty_drv;
7505 +
7506 + sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
7507 + if (!tty_drv)
7508 + return -ENOMEM;
7509 +
7510 + tty_drv->owner = THIS_MODULE;
7511 + tty_drv->driver_name = "sdio_uart";
7512 + tty_drv->name = "ttySDIO";
7513 + tty_drv->major = 0; /* dynamically allocated */
7514 + tty_drv->minor_start = 0;
7515 + tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
7516 + tty_drv->subtype = SERIAL_TYPE_NORMAL;
7517 + tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
7518 + tty_drv->init_termios = tty_std_termios;
7519 + tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
7520 + tty_drv->init_termios.c_ispeed = 4800;
7521 + tty_drv->init_termios.c_ospeed = 4800;
7522 + tty_set_operations(tty_drv, &sdio_uart_ops);
7523 +
7524 + ret = tty_register_driver(tty_drv);
7525 + if (ret)
7526 + goto err1;
7527 +
7528 + ret = sdio_register_driver(&sdio_uart_driver);
7529 + if (ret)
7530 + goto err2;
7531 +
7532 + return 0;
7533 +
7534 +err2:
7535 + tty_unregister_driver(tty_drv);
7536 +err1:
7537 + put_tty_driver(tty_drv);
7538 + return ret;
7539 +}
7540 +
7541 +static void __exit sdio_uart_exit(void)
7542 +{
7543 + sdio_unregister_driver(&sdio_uart_driver);
7544 + tty_unregister_driver(sdio_uart_tty_driver);
7545 + put_tty_driver(sdio_uart_tty_driver);
7546 +}
7547 +
7548 +module_init(sdio_uart_init);
7549 +module_exit(sdio_uart_exit);
7550 +
7551 +MODULE_AUTHOR("Nicolas Pitre");
7552 +MODULE_LICENSE("GPL");
7553 --- a/drivers/mmc/core/Makefile
7554 +++ b/drivers/mmc/core/Makefile
7555 @@ -8,5 +8,7 @@
7556
7557 obj-$(CONFIG_MMC) += mmc_core.o
7558 mmc_core-y := core.o sysfs.o bus.o host.o \
7559 - mmc.o mmc_ops.o sd.o sd_ops.o
7560 + mmc.o mmc_ops.o sd.o sd_ops.o \
7561 + sdio.o sdio_ops.o sdio_bus.o \
7562 + sdio_cis.o sdio_io.o sdio_irq.o
7563
This page took 0.372623 seconds and 5 git commands to generate.