aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--MAINTAINERS8
-rw-r--r--arch/arm/mach-imx/mx1ads.c2
-rw-r--r--drivers/mmc/card/Kconfig7
-rw-r--r--drivers/mmc/card/Makefile2
-rw-r--r--drivers/mmc/card/block.c30
-rw-r--r--drivers/mmc/card/sdio_uart.c1158
-rw-r--r--drivers/mmc/core/Makefile4
-rw-r--r--drivers/mmc/core/bus.c67
-rw-r--r--drivers/mmc/core/core.c167
-rw-r--r--drivers/mmc/core/core.h2
-rw-r--r--drivers/mmc/core/host.c8
-rw-r--r--drivers/mmc/core/mmc.c134
-rw-r--r--drivers/mmc/core/mmc_ops.c200
-rw-r--r--drivers/mmc/core/mmc_ops.h3
-rw-r--r--drivers/mmc/core/sd.c126
-rw-r--r--drivers/mmc/core/sd_ops.c107
-rw-r--r--drivers/mmc/core/sdio.c395
-rw-r--r--drivers/mmc/core/sdio_bus.c270
-rw-r--r--drivers/mmc/core/sdio_bus.h22
-rw-r--r--drivers/mmc/core/sdio_cis.c346
-rw-r--r--drivers/mmc/core/sdio_cis.h23
-rw-r--r--drivers/mmc/core/sdio_io.c548
-rw-r--r--drivers/mmc/core/sdio_irq.c267
-rw-r--r--drivers/mmc/core/sdio_ops.c176
-rw-r--r--drivers/mmc/core/sdio_ops.h22
-rw-r--r--drivers/mmc/host/Kconfig30
-rw-r--r--drivers/mmc/host/Makefile2
-rw-r--r--drivers/mmc/host/at91_mci.c25
-rw-r--r--drivers/mmc/host/au1xmmc.c58
-rw-r--r--drivers/mmc/host/imxmmc.c32
-rw-r--r--drivers/mmc/host/mmc_spi.c1408
-rw-r--r--drivers/mmc/host/mmci.c21
-rw-r--r--drivers/mmc/host/omap.c12
-rw-r--r--drivers/mmc/host/pxamci.c71
-rw-r--r--drivers/mmc/host/pxamci.h2
-rw-r--r--drivers/mmc/host/ricoh_mmc.c151
-rw-r--r--drivers/mmc/host/sdhci.c127
-rw-r--r--drivers/mmc/host/sdhci.h2
-rw-r--r--drivers/mmc/host/tifm_sd.c32
-rw-r--r--drivers/mmc/host/wbsd.c43
-rw-r--r--include/asm-arm/arch-imx/mmc.h5
-rw-r--r--include/linux/mmc/card.h32
-rw-r--r--include/linux/mmc/core.h63
-rw-r--r--include/linux/mmc/host.h39
-rw-r--r--include/linux/mmc/mmc.h39
-rw-r--r--include/linux/mmc/sdio.h159
-rw-r--r--include/linux/mmc/sdio_func.h153
-rw-r--r--include/linux/mmc/sdio_ids.h23
-rw-r--r--include/linux/mod_devicetable.h11
-rw-r--r--include/linux/pci_ids.h2
-rw-r--r--include/linux/spi/mmc_spi.h33
-rw-r--r--scripts/mod/file2alias.c20
52 files changed, 6259 insertions, 430 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 9a91d9e3f1f2..0df29ce05e5a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2561,12 +2561,18 @@ L: linux-kernel@vger.kernel.org
2561W: http://www.atnf.csiro.au/~rgooch/linux/kernel-patches.html 2561W: http://www.atnf.csiro.au/~rgooch/linux/kernel-patches.html
2562S: Maintained 2562S: Maintained
2563 2563
2564MULTIMEDIA CARD (MMC) AND SECURE DIGITAL (SD) SUBSYSTEM 2564MULTIMEDIA CARD (MMC), SECURE DIGITAL (SD) AND SDIO SUBSYSTEM
2565P: Pierre Ossman 2565P: Pierre Ossman
2566M: drzeus-mmc@drzeus.cx 2566M: drzeus-mmc@drzeus.cx
2567L: linux-kernel@vger.kernel.org 2567L: linux-kernel@vger.kernel.org
2568S: Maintained 2568S: Maintained
2569 2569
2570MULTIMEDIA CARD (MMC) ETC. OVER SPI
2571P: David Brownell
2572M: dbrownell@users.sourceforge.net
2573L: linux-kernel@vger.kernel.org
2574S: Odd fixes
2575
2570MULTISOUND SOUND DRIVER 2576MULTISOUND SOUND DRIVER
2571P: Andrew Veliath 2577P: Andrew Veliath
2572M: andrewtv@usa.net 2578M: andrewtv@usa.net
diff --git a/arch/arm/mach-imx/mx1ads.c b/arch/arm/mach-imx/mx1ads.c
index da893c80d471..a9778c1587ab 100644
--- a/arch/arm/mach-imx/mx1ads.c
+++ b/arch/arm/mach-imx/mx1ads.c
@@ -116,7 +116,7 @@ static struct platform_device *devices[] __initdata = {
116}; 116};
117 117
118#ifdef CONFIG_MMC_IMX 118#ifdef CONFIG_MMC_IMX
119static int mx1ads_mmc_card_present(void) 119static int mx1ads_mmc_card_present(struct device *dev)
120{ 120{
121 /* MMC/SD Card Detect is PB 20 on MX1ADS V1.0.7 */ 121 /* MMC/SD Card Detect is PB 20 on MX1ADS V1.0.7 */
122 return (SSR(1) & (1 << 20) ? 0 : 1); 122 return (SSR(1) & (1 << 20) ? 0 : 1);
diff --git a/drivers/mmc/card/Kconfig b/drivers/mmc/card/Kconfig
index a49cb9737cd8..aa8a4e461942 100644
--- a/drivers/mmc/card/Kconfig
+++ b/drivers/mmc/card/Kconfig
@@ -32,3 +32,10 @@ config MMC_BLOCK_BOUNCE
32 32
33 If unsure, say Y here. 33 If unsure, say Y here.
34 34
35config SDIO_UART
36 tristate "SDIO UART/GPS class support"
37 depends on MMC
38 help
39 SDIO function driver for SDIO cards that implements the UART
40 class, as well as the GPS class which appears like a UART.
41
diff --git a/drivers/mmc/card/Makefile b/drivers/mmc/card/Makefile
index cf8c939867f5..fc5a784cfa1a 100644
--- a/drivers/mmc/card/Makefile
+++ b/drivers/mmc/card/Makefile
@@ -9,3 +9,5 @@ endif
9obj-$(CONFIG_MMC_BLOCK) += mmc_block.o 9obj-$(CONFIG_MMC_BLOCK) += mmc_block.o
10mmc_block-objs := block.o queue.o 10mmc_block-objs := block.o queue.o
11 11
12obj-$(CONFIG_SDIO_UART) += sdio_uart.o
13
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 93fe2e5dd616..e38d5a3b2a89 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -151,17 +151,19 @@ static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
151 151
152 cmd.opcode = MMC_APP_CMD; 152 cmd.opcode = MMC_APP_CMD;
153 cmd.arg = card->rca << 16; 153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 154 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
155 155
156 err = mmc_wait_for_cmd(card->host, &cmd, 0); 156 err = mmc_wait_for_cmd(card->host, &cmd, 0);
157 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) 157 if (err)
158 return (u32)-1;
159 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
158 return (u32)-1; 160 return (u32)-1;
159 161
160 memset(&cmd, 0, sizeof(struct mmc_command)); 162 memset(&cmd, 0, sizeof(struct mmc_command));
161 163
162 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; 164 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
163 cmd.arg = 0; 165 cmd.arg = 0;
164 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 166 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
165 167
166 memset(&data, 0, sizeof(struct mmc_data)); 168 memset(&data, 0, sizeof(struct mmc_data));
167 169
@@ -192,7 +194,7 @@ static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
192 194
193 mmc_wait_for_req(card->host, &mrq); 195 mmc_wait_for_req(card->host, &mrq);
194 196
195 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) 197 if (cmd.error || data.error)
196 return (u32)-1; 198 return (u32)-1;
197 199
198 blocks = ntohl(blocks); 200 blocks = ntohl(blocks);
@@ -220,17 +222,15 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
220 brq.cmd.arg = req->sector; 222 brq.cmd.arg = req->sector;
221 if (!mmc_card_blockaddr(card)) 223 if (!mmc_card_blockaddr(card))
222 brq.cmd.arg <<= 9; 224 brq.cmd.arg <<= 9;
223 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 225 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
224 brq.data.blksz = 1 << md->block_bits; 226 brq.data.blksz = 1 << md->block_bits;
225 brq.stop.opcode = MMC_STOP_TRANSMISSION; 227 brq.stop.opcode = MMC_STOP_TRANSMISSION;
226 brq.stop.arg = 0; 228 brq.stop.arg = 0;
227 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 229 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
228 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9); 230 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
229 if (brq.data.blocks > card->host->max_blk_count) 231 if (brq.data.blocks > card->host->max_blk_count)
230 brq.data.blocks = card->host->max_blk_count; 232 brq.data.blocks = card->host->max_blk_count;
231 233
232 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
233
234 /* 234 /*
235 * If the host doesn't support multiple block writes, force 235 * If the host doesn't support multiple block writes, force
236 * block writes to single block. SD cards are excepted from 236 * block writes to single block. SD cards are excepted from
@@ -243,8 +243,12 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
243 brq.data.blocks = 1; 243 brq.data.blocks = 1;
244 244
245 if (brq.data.blocks > 1) { 245 if (brq.data.blocks > 1) {
246 brq.data.flags |= MMC_DATA_MULTI; 246 /* SPI multiblock writes terminate using a special
247 brq.mrq.stop = &brq.stop; 247 * token, not a STOP_TRANSMISSION request.
248 */
249 if (!mmc_host_is_spi(card->host)
250 || rq_data_dir(req) == READ)
251 brq.mrq.stop = &brq.stop;
248 readcmd = MMC_READ_MULTIPLE_BLOCK; 252 readcmd = MMC_READ_MULTIPLE_BLOCK;
249 writecmd = MMC_WRITE_MULTIPLE_BLOCK; 253 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
250 } else { 254 } else {
@@ -261,6 +265,8 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
261 brq.data.flags |= MMC_DATA_WRITE; 265 brq.data.flags |= MMC_DATA_WRITE;
262 } 266 }
263 267
268 mmc_set_data_timeout(&brq.data, card);
269
264 brq.data.sg = mq->sg; 270 brq.data.sg = mq->sg;
265 brq.data.sg_len = mmc_queue_map_sg(mq); 271 brq.data.sg_len = mmc_queue_map_sg(mq);
266 272
@@ -302,7 +308,7 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
302 goto cmd_err; 308 goto cmd_err;
303 } 309 }
304 310
305 if (rq_data_dir(req) != READ) { 311 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
306 do { 312 do {
307 int err; 313 int err;
308 314
@@ -510,7 +516,7 @@ mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
510 mmc_claim_host(card->host); 516 mmc_claim_host(card->host);
511 cmd.opcode = MMC_SET_BLOCKLEN; 517 cmd.opcode = MMC_SET_BLOCKLEN;
512 cmd.arg = 1 << md->block_bits; 518 cmd.arg = 1 << md->block_bits;
513 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 519 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
514 err = mmc_wait_for_cmd(card->host, &cmd, 5); 520 err = mmc_wait_for_cmd(card->host, &cmd, 5);
515 mmc_release_host(card->host); 521 mmc_release_host(card->host);
516 522
diff --git a/drivers/mmc/card/sdio_uart.c b/drivers/mmc/card/sdio_uart.c
new file mode 100644
index 000000000000..d552de683110
--- /dev/null
+++ b/drivers/mmc/card/sdio_uart.c
@@ -0,0 +1,1158 @@
1/*
2 * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
3 *
4 * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
5 * by Russell King.
6 *
7 * Author: Nicolas Pitre
8 * Created: June 15, 2007
9 * Copyright: MontaVista Software, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 */
16
17/*
18 * Note: Although this driver assumes a 16550A-like UART implementation,
19 * it is not possible to leverage the common 8250/16550 driver, nor the
20 * core UART infrastructure, as they assumes direct access to the hardware
21 * registers, often under a spinlock. This is not possible in the SDIO
22 * context as SDIO access functions must be able to sleep.
23 *
24 * Because we need to lock the SDIO host to ensure an exclusive access to
25 * the card, we simply rely on that lock to also prevent and serialize
26 * concurrent access to the same port.
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32#include <linux/mutex.h>
33#include <linux/serial_reg.h>
34#include <linux/circ_buf.h>
35#include <linux/gfp.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38
39#include <linux/mmc/core.h>
40#include <linux/mmc/card.h>
41#include <linux/mmc/sdio_func.h>
42#include <linux/mmc/sdio_ids.h>
43
44
45#define UART_NR 8 /* Number of UARTs this driver can handle */
46
47
48#define UART_XMIT_SIZE PAGE_SIZE
49#define WAKEUP_CHARS 256
50
51#define circ_empty(circ) ((circ)->head == (circ)->tail)
52#define circ_clear(circ) ((circ)->head = (circ)->tail = 0)
53
54#define circ_chars_pending(circ) \
55 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
56
57#define circ_chars_free(circ) \
58 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
59
60
61struct uart_icount {
62 __u32 cts;
63 __u32 dsr;
64 __u32 rng;
65 __u32 dcd;
66 __u32 rx;
67 __u32 tx;
68 __u32 frame;
69 __u32 overrun;
70 __u32 parity;
71 __u32 brk;
72};
73
74struct sdio_uart_port {
75 struct kref kref;
76 struct tty_struct *tty;
77 unsigned int index;
78 unsigned int opened;
79 struct mutex open_lock;
80 struct sdio_func *func;
81 struct mutex func_lock;
82 struct task_struct *in_sdio_uart_irq;
83 unsigned int regs_offset;
84 struct circ_buf xmit;
85 spinlock_t write_lock;
86 struct uart_icount icount;
87 unsigned int uartclk;
88 unsigned int mctrl;
89 unsigned int read_status_mask;
90 unsigned int ignore_status_mask;
91 unsigned char x_char;
92 unsigned char ier;
93 unsigned char lcr;
94};
95
96static struct sdio_uart_port *sdio_uart_table[UART_NR];
97static DEFINE_SPINLOCK(sdio_uart_table_lock);
98
99static int sdio_uart_add_port(struct sdio_uart_port *port)
100{
101 int index, ret = -EBUSY;
102
103 kref_init(&port->kref);
104 mutex_init(&port->open_lock);
105 mutex_init(&port->func_lock);
106 spin_lock_init(&port->write_lock);
107
108 spin_lock(&sdio_uart_table_lock);
109 for (index = 0; index < UART_NR; index++) {
110 if (!sdio_uart_table[index]) {
111 port->index = index;
112 sdio_uart_table[index] = port;
113 ret = 0;
114 break;
115 }
116 }
117 spin_unlock(&sdio_uart_table_lock);
118
119 return ret;
120}
121
122static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
123{
124 struct sdio_uart_port *port;
125
126 if (index >= UART_NR)
127 return NULL;
128
129 spin_lock(&sdio_uart_table_lock);
130 port = sdio_uart_table[index];
131 if (port)
132 kref_get(&port->kref);
133 spin_unlock(&sdio_uart_table_lock);
134
135 return port;
136}
137
138static void sdio_uart_port_destroy(struct kref *kref)
139{
140 struct sdio_uart_port *port =
141 container_of(kref, struct sdio_uart_port, kref);
142 kfree(port);
143}
144
145static void sdio_uart_port_put(struct sdio_uart_port *port)
146{
147 kref_put(&port->kref, sdio_uart_port_destroy);
148}
149
150static void sdio_uart_port_remove(struct sdio_uart_port *port)
151{
152 struct sdio_func *func;
153
154 BUG_ON(sdio_uart_table[port->index] != port);
155
156 spin_lock(&sdio_uart_table_lock);
157 sdio_uart_table[port->index] = NULL;
158 spin_unlock(&sdio_uart_table_lock);
159
160 /*
161 * We're killing a port that potentially still is in use by
162 * the tty layer. Be careful to prevent any further access
163 * to the SDIO function and arrange for the tty layer to
164 * give up on that port ASAP.
165 * Beware: the lock ordering is critical.
166 */
167 mutex_lock(&port->open_lock);
168 mutex_lock(&port->func_lock);
169 func = port->func;
170 sdio_claim_host(func);
171 port->func = NULL;
172 mutex_unlock(&port->func_lock);
173 if (port->opened)
174 tty_hangup(port->tty);
175 mutex_unlock(&port->open_lock);
176 sdio_release_irq(func);
177 sdio_disable_func(func);
178 sdio_release_host(func);
179
180 sdio_uart_port_put(port);
181}
182
183static int sdio_uart_claim_func(struct sdio_uart_port *port)
184{
185 mutex_lock(&port->func_lock);
186 if (unlikely(!port->func)) {
187 mutex_unlock(&port->func_lock);
188 return -ENODEV;
189 }
190 if (likely(port->in_sdio_uart_irq != current))
191 sdio_claim_host(port->func);
192 mutex_unlock(&port->func_lock);
193 return 0;
194}
195
196static inline void sdio_uart_release_func(struct sdio_uart_port *port)
197{
198 if (likely(port->in_sdio_uart_irq != current))
199 sdio_release_host(port->func);
200}
201
202static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
203{
204 unsigned char c;
205 c = sdio_readb(port->func, port->regs_offset + offset, NULL);
206 return c;
207}
208
209static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
210{
211 sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
212}
213
214static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
215{
216 unsigned char status;
217 unsigned int ret;
218
219 status = sdio_in(port, UART_MSR);
220
221 ret = 0;
222 if (status & UART_MSR_DCD)
223 ret |= TIOCM_CAR;
224 if (status & UART_MSR_RI)
225 ret |= TIOCM_RNG;
226 if (status & UART_MSR_DSR)
227 ret |= TIOCM_DSR;
228 if (status & UART_MSR_CTS)
229 ret |= TIOCM_CTS;
230 return ret;
231}
232
233static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
234{
235 unsigned char mcr = 0;
236
237 if (mctrl & TIOCM_RTS)
238 mcr |= UART_MCR_RTS;
239 if (mctrl & TIOCM_DTR)
240 mcr |= UART_MCR_DTR;
241 if (mctrl & TIOCM_OUT1)
242 mcr |= UART_MCR_OUT1;
243 if (mctrl & TIOCM_OUT2)
244 mcr |= UART_MCR_OUT2;
245 if (mctrl & TIOCM_LOOP)
246 mcr |= UART_MCR_LOOP;
247
248 sdio_out(port, UART_MCR, mcr);
249}
250
251static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
252 unsigned int set, unsigned int clear)
253{
254 unsigned int old;
255
256 old = port->mctrl;
257 port->mctrl = (old & ~clear) | set;
258 if (old != port->mctrl)
259 sdio_uart_write_mctrl(port, port->mctrl);
260}
261
262#define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
263#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
264
265static void sdio_uart_change_speed(struct sdio_uart_port *port,
266 struct ktermios *termios,
267 struct ktermios *old)
268{
269 unsigned char cval, fcr = 0;
270 unsigned int baud, quot;
271
272 switch (termios->c_cflag & CSIZE) {
273 case CS5:
274 cval = UART_LCR_WLEN5;
275 break;
276 case CS6:
277 cval = UART_LCR_WLEN6;
278 break;
279 case CS7:
280 cval = UART_LCR_WLEN7;
281 break;
282 default:
283 case CS8:
284 cval = UART_LCR_WLEN8;
285 break;
286 }
287
288 if (termios->c_cflag & CSTOPB)
289 cval |= UART_LCR_STOP;
290 if (termios->c_cflag & PARENB)
291 cval |= UART_LCR_PARITY;
292 if (!(termios->c_cflag & PARODD))
293 cval |= UART_LCR_EPAR;
294
295 for (;;) {
296 baud = tty_termios_baud_rate(termios);
297 if (baud == 0)
298 baud = 9600; /* Special case: B0 rate. */
299 if (baud <= port->uartclk)
300 break;
301 /*
302 * Oops, the quotient was zero. Try again with the old
303 * baud rate if possible, otherwise default to 9600.
304 */
305 termios->c_cflag &= ~CBAUD;
306 if (old) {
307 termios->c_cflag |= old->c_cflag & CBAUD;
308 old = NULL;
309 } else
310 termios->c_cflag |= B9600;
311 }
312 quot = (2 * port->uartclk + baud) / (2 * baud);
313
314 if (baud < 2400)
315 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
316 else
317 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
318
319 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
320 if (termios->c_iflag & INPCK)
321 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
322 if (termios->c_iflag & (BRKINT | PARMRK))
323 port->read_status_mask |= UART_LSR_BI;
324
325 /*
326 * Characters to ignore
327 */
328 port->ignore_status_mask = 0;
329 if (termios->c_iflag & IGNPAR)
330 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
331 if (termios->c_iflag & IGNBRK) {
332 port->ignore_status_mask |= UART_LSR_BI;
333 /*
334 * If we're ignoring parity and break indicators,
335 * ignore overruns too (for real raw support).
336 */
337 if (termios->c_iflag & IGNPAR)
338 port->ignore_status_mask |= UART_LSR_OE;
339 }
340
341 /*
342 * ignore all characters if CREAD is not set
343 */
344 if ((termios->c_cflag & CREAD) == 0)
345 port->ignore_status_mask |= UART_LSR_DR;
346
347 /*
348 * CTS flow control flag and modem status interrupts
349 */
350 port->ier &= ~UART_IER_MSI;
351 if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
352 port->ier |= UART_IER_MSI;
353
354 port->lcr = cval;
355
356 sdio_out(port, UART_IER, port->ier);
357 sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
358 sdio_out(port, UART_DLL, quot & 0xff);
359 sdio_out(port, UART_DLM, quot >> 8);
360 sdio_out(port, UART_LCR, cval);
361 sdio_out(port, UART_FCR, fcr);
362
363 sdio_uart_write_mctrl(port, port->mctrl);
364}
365
366static void sdio_uart_start_tx(struct sdio_uart_port *port)
367{
368 if (!(port->ier & UART_IER_THRI)) {
369 port->ier |= UART_IER_THRI;
370 sdio_out(port, UART_IER, port->ier);
371 }
372}
373
374static void sdio_uart_stop_tx(struct sdio_uart_port *port)
375{
376 if (port->ier & UART_IER_THRI) {
377 port->ier &= ~UART_IER_THRI;
378 sdio_out(port, UART_IER, port->ier);
379 }
380}
381
382static void sdio_uart_stop_rx(struct sdio_uart_port *port)
383{
384 port->ier &= ~UART_IER_RLSI;
385 port->read_status_mask &= ~UART_LSR_DR;
386 sdio_out(port, UART_IER, port->ier);
387}
388
389static void sdio_uart_receive_chars(struct sdio_uart_port *port, int *status)
390{
391 struct tty_struct *tty = port->tty;
392 unsigned int ch, flag;
393 int max_count = 256;
394
395 do {
396 ch = sdio_in(port, UART_RX);
397 flag = TTY_NORMAL;
398 port->icount.rx++;
399
400 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
401 UART_LSR_FE | UART_LSR_OE))) {
402 /*
403 * For statistics only
404 */
405 if (*status & UART_LSR_BI) {
406 *status &= ~(UART_LSR_FE | UART_LSR_PE);
407 port->icount.brk++;
408 } else if (*status & UART_LSR_PE)
409 port->icount.parity++;
410 else if (*status & UART_LSR_FE)
411 port->icount.frame++;
412 if (*status & UART_LSR_OE)
413 port->icount.overrun++;
414
415 /*
416 * Mask off conditions which should be ignored.
417 */
418 *status &= port->read_status_mask;
419 if (*status & UART_LSR_BI) {
420 flag = TTY_BREAK;
421 } else if (*status & UART_LSR_PE)
422 flag = TTY_PARITY;
423 else if (*status & UART_LSR_FE)
424 flag = TTY_FRAME;
425 }
426
427 if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
428 tty_insert_flip_char(tty, ch, flag);
429
430 /*
431 * Overrun is special. Since it's reported immediately,
432 * it doesn't affect the current character.
433 */
434 if (*status & ~port->ignore_status_mask & UART_LSR_OE)
435 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
436
437 *status = sdio_in(port, UART_LSR);
438 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
439 tty_flip_buffer_push(tty);
440}
441
442static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
443{
444 struct circ_buf *xmit = &port->xmit;
445 int count;
446
447 if (port->x_char) {
448 sdio_out(port, UART_TX, port->x_char);
449 port->icount.tx++;
450 port->x_char = 0;
451 return;
452 }
453 if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
454 sdio_uart_stop_tx(port);
455 return;
456 }
457
458 count = 16;
459 do {
460 sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
461 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
462 port->icount.tx++;
463 if (circ_empty(xmit))
464 break;
465 } while (--count > 0);
466
467 if (circ_chars_pending(xmit) < WAKEUP_CHARS)
468 tty_wakeup(port->tty);
469
470 if (circ_empty(xmit))
471 sdio_uart_stop_tx(port);
472}
473
474static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
475{
476 int status;
477
478 status = sdio_in(port, UART_MSR);
479
480 if ((status & UART_MSR_ANY_DELTA) == 0)
481 return;
482
483 if (status & UART_MSR_TERI)
484 port->icount.rng++;
485 if (status & UART_MSR_DDSR)
486 port->icount.dsr++;
487 if (status & UART_MSR_DDCD)
488 port->icount.dcd++;
489 if (status & UART_MSR_DCTS) {
490 port->icount.cts++;
491 if (port->tty->termios->c_cflag & CRTSCTS) {
492 int cts = (status & UART_MSR_CTS);
493 if (port->tty->hw_stopped) {
494 if (cts) {
495 port->tty->hw_stopped = 0;
496 sdio_uart_start_tx(port);
497 tty_wakeup(port->tty);
498 }
499 } else {
500 if (!cts) {
501 port->tty->hw_stopped = 1;
502 sdio_uart_stop_tx(port);
503 }
504 }
505 }
506 }
507}
508
509/*
510 * This handles the interrupt from one port.
511 */
512static void sdio_uart_irq(struct sdio_func *func)
513{
514 struct sdio_uart_port *port = sdio_get_drvdata(func);
515 unsigned int iir, lsr;
516
517 /*
518 * In a few places sdio_uart_irq() is called directly instead of
519 * waiting for the actual interrupt to be raised and the SDIO IRQ
520 * thread scheduled in order to reduce latency. However, some
521 * interaction with the tty core may end up calling us back
522 * (serial echo, flow control, etc.) through those same places
523 * causing undesirable effects. Let's stop the recursion here.
524 */
525 if (unlikely(port->in_sdio_uart_irq == current))
526 return;
527
528 iir = sdio_in(port, UART_IIR);
529 if (iir & UART_IIR_NO_INT)
530 return;
531
532 port->in_sdio_uart_irq = current;
533 lsr = sdio_in(port, UART_LSR);
534 if (lsr & UART_LSR_DR)
535 sdio_uart_receive_chars(port, &lsr);
536 sdio_uart_check_modem_status(port);
537 if (lsr & UART_LSR_THRE)
538 sdio_uart_transmit_chars(port);
539 port->in_sdio_uart_irq = NULL;
540}
541
542static int sdio_uart_startup(struct sdio_uart_port *port)
543{
544 unsigned long page;
545 int ret;
546
547 /*
548 * Set the TTY IO error marker - we will only clear this
549 * once we have successfully opened the port.
550 */
551 set_bit(TTY_IO_ERROR, &port->tty->flags);
552
553 /* Initialise and allocate the transmit buffer. */
554 page = __get_free_page(GFP_KERNEL);
555 if (!page)
556 return -ENOMEM;
557 port->xmit.buf = (unsigned char *)page;
558 circ_clear(&port->xmit);
559
560 ret = sdio_uart_claim_func(port);
561 if (ret)
562 goto err1;
563 ret = sdio_enable_func(port->func);
564 if (ret)
565 goto err2;
566 ret = sdio_claim_irq(port->func, sdio_uart_irq);
567 if (ret)
568 goto err3;
569
570 /*
571 * Clear the FIFO buffers and disable them.
572 * (they will be reenabled in sdio_change_speed())
573 */
574 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
575 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
576 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
577 sdio_out(port, UART_FCR, 0);
578
579 /*
580 * Clear the interrupt registers.
581 */
582 (void) sdio_in(port, UART_LSR);
583 (void) sdio_in(port, UART_RX);
584 (void) sdio_in(port, UART_IIR);
585 (void) sdio_in(port, UART_MSR);
586
587 /*
588 * Now, initialize the UART
589 */
590 sdio_out(port, UART_LCR, UART_LCR_WLEN8);
591
592 port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
593 port->mctrl = TIOCM_OUT2;
594
595 sdio_uart_change_speed(port, port->tty->termios, NULL);
596
597 if (port->tty->termios->c_cflag & CBAUD)
598 sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
599
600 if (port->tty->termios->c_cflag & CRTSCTS)
601 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
602 port->tty->hw_stopped = 1;
603
604 clear_bit(TTY_IO_ERROR, &port->tty->flags);
605
606 /* Kick the IRQ handler once while we're still holding the host lock */
607 sdio_uart_irq(port->func);
608
609 sdio_uart_release_func(port);
610 return 0;
611
612err3:
613 sdio_disable_func(port->func);
614err2:
615 sdio_uart_release_func(port);
616err1:
617 free_page((unsigned long)port->xmit.buf);
618 return ret;
619}
620
621static void sdio_uart_shutdown(struct sdio_uart_port *port)
622{
623 int ret;
624
625 ret = sdio_uart_claim_func(port);
626 if (ret)
627 goto skip;
628
629 sdio_uart_stop_rx(port);
630
631 /* TODO: wait here for TX FIFO to drain */
632
633 /* Turn off DTR and RTS early. */
634 if (port->tty->termios->c_cflag & HUPCL)
635 sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
636
637 /* Disable interrupts from this port */
638 sdio_release_irq(port->func);
639 port->ier = 0;
640 sdio_out(port, UART_IER, 0);
641
642 sdio_uart_clear_mctrl(port, TIOCM_OUT2);
643
644 /* Disable break condition and FIFOs. */
645 port->lcr &= ~UART_LCR_SBC;
646 sdio_out(port, UART_LCR, port->lcr);
647 sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
648 UART_FCR_CLEAR_RCVR |
649 UART_FCR_CLEAR_XMIT);
650 sdio_out(port, UART_FCR, 0);
651
652 sdio_disable_func(port->func);
653
654 sdio_uart_release_func(port);
655
656skip:
657 /* Free the transmit buffer page. */
658 free_page((unsigned long)port->xmit.buf);
659}
660
661static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
662{
663 struct sdio_uart_port *port;
664 int ret;
665
666 port = sdio_uart_port_get(tty->index);
667 if (!port)
668 return -ENODEV;
669
670 mutex_lock(&port->open_lock);
671
672 /*
673 * Make sure not to mess up with a dead port
674 * which has not been closed yet.
675 */
676 if (tty->driver_data && tty->driver_data != port) {
677 mutex_unlock(&port->open_lock);
678 sdio_uart_port_put(port);
679 return -EBUSY;
680 }
681
682 if (!port->opened) {
683 tty->driver_data = port;
684 port->tty = tty;
685 ret = sdio_uart_startup(port);
686 if (ret) {
687 tty->driver_data = NULL;
688 port->tty = NULL;
689 mutex_unlock(&port->open_lock);
690 sdio_uart_port_put(port);
691 return ret;
692 }
693 }
694 port->opened++;
695 mutex_unlock(&port->open_lock);
696 return 0;
697}
698
699static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
700{
701 struct sdio_uart_port *port = tty->driver_data;
702
703 if (!port)
704 return;
705
706 mutex_lock(&port->open_lock);
707 BUG_ON(!port->opened);
708
709 /*
710 * This is messy. The tty layer calls us even when open()
711 * returned an error. Ignore this close request if tty->count
712 * is larger than port->count.
713 */
714 if (tty->count > port->opened) {
715 mutex_unlock(&port->open_lock);
716 return;
717 }
718
719 if (--port->opened == 0) {
720 tty->closing = 1;
721 sdio_uart_shutdown(port);
722 tty_ldisc_flush(tty);
723 port->tty = NULL;
724 tty->driver_data = NULL;
725 tty->closing = 0;
726 }
727 mutex_unlock(&port->open_lock);
728 sdio_uart_port_put(port);
729}
730
731static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
732 int count)
733{
734 struct sdio_uart_port *port = tty->driver_data;
735 struct circ_buf *circ = &port->xmit;
736 int c, ret = 0;
737
738 if (!port->func)
739 return -ENODEV;
740
741 spin_lock(&port->write_lock);
742 while (1) {
743 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
744 if (count < c)
745 c = count;
746 if (c <= 0)
747 break;
748 memcpy(circ->buf + circ->head, buf, c);
749 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
750 buf += c;
751 count -= c;
752 ret += c;
753 }
754 spin_unlock(&port->write_lock);
755
756 if ( !(port->ier & UART_IER_THRI)) {
757 int err = sdio_uart_claim_func(port);
758 if (!err) {
759 sdio_uart_start_tx(port);
760 sdio_uart_irq(port->func);
761 sdio_uart_release_func(port);
762 } else
763 ret = err;
764 }
765
766 return ret;
767}
768
769static int sdio_uart_write_room(struct tty_struct *tty)
770{
771 struct sdio_uart_port *port = tty->driver_data;
772 return port ? circ_chars_free(&port->xmit) : 0;
773}
774
775static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
776{
777 struct sdio_uart_port *port = tty->driver_data;
778 return port ? circ_chars_pending(&port->xmit) : 0;
779}
780
781static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
782{
783 struct sdio_uart_port *port = tty->driver_data;
784
785 port->x_char = ch;
786 if (ch && !(port->ier & UART_IER_THRI)) {
787 if (sdio_uart_claim_func(port) != 0)
788 return;
789 sdio_uart_start_tx(port);
790 sdio_uart_irq(port->func);
791 sdio_uart_release_func(port);
792 }
793}
794
795static void sdio_uart_throttle(struct tty_struct *tty)
796{
797 struct sdio_uart_port *port = tty->driver_data;
798
799 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
800 return;
801
802 if (sdio_uart_claim_func(port) != 0)
803 return;
804
805 if (I_IXOFF(tty)) {
806 port->x_char = STOP_CHAR(tty);
807 sdio_uart_start_tx(port);
808 }
809
810 if (tty->termios->c_cflag & CRTSCTS)
811 sdio_uart_clear_mctrl(port, TIOCM_RTS);
812
813 sdio_uart_irq(port->func);
814 sdio_uart_release_func(port);
815}
816
817static void sdio_uart_unthrottle(struct tty_struct *tty)
818{
819 struct sdio_uart_port *port = tty->driver_data;
820
821 if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
822 return;
823
824 if (sdio_uart_claim_func(port) != 0)
825 return;
826
827 if (I_IXOFF(tty)) {
828 if (port->x_char) {
829 port->x_char = 0;
830 } else {
831 port->x_char = START_CHAR(tty);
832 sdio_uart_start_tx(port);
833 }
834 }
835
836 if (tty->termios->c_cflag & CRTSCTS)
837 sdio_uart_set_mctrl(port, TIOCM_RTS);
838
839 sdio_uart_irq(port->func);
840 sdio_uart_release_func(port);
841}
842
843static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
844{
845 struct sdio_uart_port *port = tty->driver_data;
846 unsigned int cflag = tty->termios->c_cflag;
847
848#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
849
850 if ((cflag ^ old_termios->c_cflag) == 0 &&
851 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
852 return;
853
854 if (sdio_uart_claim_func(port) != 0)
855 return;
856
857 sdio_uart_change_speed(port, tty->termios, old_termios);
858
859 /* Handle transition to B0 status */
860 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
861 sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
862
863 /* Handle transition away from B0 status */
864 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
865 unsigned int mask = TIOCM_DTR;
866 if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
867 mask |= TIOCM_RTS;
868 sdio_uart_set_mctrl(port, mask);
869 }
870
871 /* Handle turning off CRTSCTS */
872 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
873 tty->hw_stopped = 0;
874 sdio_uart_start_tx(port);
875 }
876
877 /* Handle turning on CRTSCTS */
878 if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
879 if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
880 tty->hw_stopped = 1;
881 sdio_uart_stop_tx(port);
882 }
883 }
884
885 sdio_uart_release_func(port);
886}
887
888static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
889{
890 struct sdio_uart_port *port = tty->driver_data;
891
892 if (sdio_uart_claim_func(port) != 0)
893 return;
894
895 if (break_state == -1)
896 port->lcr |= UART_LCR_SBC;
897 else
898 port->lcr &= ~UART_LCR_SBC;
899 sdio_out(port, UART_LCR, port->lcr);
900
901 sdio_uart_release_func(port);
902}
903
904static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
905{
906 struct sdio_uart_port *port = tty->driver_data;
907 int result;
908
909 result = sdio_uart_claim_func(port);
910 if (!result) {
911 result = port->mctrl | sdio_uart_get_mctrl(port);
912 sdio_uart_release_func(port);
913 }
914
915 return result;
916}
917
918static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
919 unsigned int set, unsigned int clear)
920{
921 struct sdio_uart_port *port = tty->driver_data;
922 int result;
923
924 result =sdio_uart_claim_func(port);
925 if(!result) {
926 sdio_uart_update_mctrl(port, set, clear);
927 sdio_uart_release_func(port);
928 }
929
930 return result;
931}
932
933static int sdio_uart_read_proc(char *page, char **start, off_t off,
934 int count, int *eof, void *data)
935{
936 int i, len = 0;
937 off_t begin = 0;
938
939 len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
940 "", "", "");
941 for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
942 struct sdio_uart_port *port = sdio_uart_port_get(i);
943 if (port) {
944 len += sprintf(page+len, "%d: uart:SDIO", i);
945 if(capable(CAP_SYS_ADMIN)) {
946 len += sprintf(page + len, " tx:%d rx:%d",
947 port->icount.tx, port->icount.rx);
948 if (port->icount.frame)
949 len += sprintf(page + len, " fe:%d",
950 port->icount.frame);
951 if (port->icount.parity)
952 len += sprintf(page + len, " pe:%d",
953 port->icount.parity);
954 if (port->icount.brk)
955 len += sprintf(page + len, " brk:%d",
956 port->icount.brk);
957 if (port->icount.overrun)
958 len += sprintf(page + len, " oe:%d",
959 port->icount.overrun);
960 if (port->icount.cts)
961 len += sprintf(page + len, " cts:%d",
962 port->icount.cts);
963 if (port->icount.dsr)
964 len += sprintf(page + len, " dsr:%d",
965 port->icount.dsr);
966 if (port->icount.rng)
967 len += sprintf(page + len, " rng:%d",
968 port->icount.rng);
969 if (port->icount.dcd)
970 len += sprintf(page + len, " dcd:%d",
971 port->icount.dcd);
972 }
973 strcat(page, "\n");
974 len++;
975 sdio_uart_port_put(port);
976 }
977
978 if (len + begin > off + count)
979 goto done;
980 if (len + begin < off) {
981 begin += len;
982 len = 0;
983 }
984 }
985 *eof = 1;
986
987done:
988 if (off >= len + begin)
989 return 0;
990 *start = page + (off - begin);
991 return (count < begin + len - off) ? count : (begin + len - off);
992}
993
994static const struct tty_operations sdio_uart_ops = {
995 .open = sdio_uart_open,
996 .close = sdio_uart_close,
997 .write = sdio_uart_write,
998 .write_room = sdio_uart_write_room,
999 .chars_in_buffer = sdio_uart_chars_in_buffer,
1000 .send_xchar = sdio_uart_send_xchar,
1001 .throttle = sdio_uart_throttle,
1002 .unthrottle = sdio_uart_unthrottle,
1003 .set_termios = sdio_uart_set_termios,
1004 .break_ctl = sdio_uart_break_ctl,
1005 .tiocmget = sdio_uart_tiocmget,
1006 .tiocmset = sdio_uart_tiocmset,
1007 .read_proc = sdio_uart_read_proc,
1008};
1009
1010static struct tty_driver *sdio_uart_tty_driver;
1011
1012static int sdio_uart_probe(struct sdio_func *func,
1013 const struct sdio_device_id *id)
1014{
1015 struct sdio_uart_port *port;
1016 int ret;
1017
1018 port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
1019 if (!port)
1020 return -ENOMEM;
1021
1022 if (func->class == SDIO_CLASS_UART) {
1023 printk(KERN_WARNING "%s: need info on UART class basic setup\n",
1024 sdio_func_id(func));
1025 kfree(port);
1026 return -ENOSYS;
1027 } else if (func->class == SDIO_CLASS_GPS) {
1028 /*
1029 * We need tuple 0x91. It contains SUBTPL_SIOREG
1030 * and SUBTPL_RCVCAPS.
1031 */
1032 struct sdio_func_tuple *tpl;
1033 for (tpl = func->tuples; tpl; tpl = tpl->next) {
1034 if (tpl->code != 0x91)
1035 continue;
1036 if (tpl->size < 10)
1037 continue;
1038 if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
1039 break;
1040 }
1041 if (!tpl) {
1042 printk(KERN_WARNING
1043 "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
1044 sdio_func_id(func));
1045 kfree(port);
1046 return -EINVAL;
1047 }
1048 printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
1049 sdio_func_id(func), tpl->data[2], tpl->data[3]);
1050 port->regs_offset = (tpl->data[4] << 0) |
1051 (tpl->data[5] << 8) |
1052 (tpl->data[6] << 16);
1053 printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
1054 sdio_func_id(func), port->regs_offset);
1055 port->uartclk = tpl->data[7] * 115200;
1056 if (port->uartclk == 0)
1057 port->uartclk = 115200;
1058 printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
1059 sdio_func_id(func), port->uartclk,
1060 tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
1061 } else {
1062 kfree(port);
1063 return -EINVAL;
1064 }
1065
1066 port->func = func;
1067 sdio_set_drvdata(func, port);
1068
1069 ret = sdio_uart_add_port(port);
1070 if (ret) {
1071 kfree(port);
1072 } else {
1073 struct device *dev;
1074 dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
1075 if (IS_ERR(dev)) {
1076 sdio_uart_port_remove(port);
1077 ret = PTR_ERR(dev);
1078 }
1079 }
1080
1081 return ret;
1082}
1083
1084static void sdio_uart_remove(struct sdio_func *func)
1085{
1086 struct sdio_uart_port *port = sdio_get_drvdata(func);
1087
1088 tty_unregister_device(sdio_uart_tty_driver, port->index);
1089 sdio_uart_port_remove(port);
1090}
1091
1092static const struct sdio_device_id sdio_uart_ids[] = {
1093 { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
1094 { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
1095 { /* end: all zeroes */ },
1096};
1097
1098MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
1099
1100static struct sdio_driver sdio_uart_driver = {
1101 .probe = sdio_uart_probe,
1102 .remove = sdio_uart_remove,
1103 .name = "sdio_uart",
1104 .id_table = sdio_uart_ids,
1105};
1106
1107static int __init sdio_uart_init(void)
1108{
1109 int ret;
1110 struct tty_driver *tty_drv;
1111
1112 sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
1113 if (!tty_drv)
1114 return -ENOMEM;
1115
1116 tty_drv->owner = THIS_MODULE;
1117 tty_drv->driver_name = "sdio_uart";
1118 tty_drv->name = "ttySDIO";
1119 tty_drv->major = 0; /* dynamically allocated */
1120 tty_drv->minor_start = 0;
1121 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1122 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1123 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1124 tty_drv->init_termios = tty_std_termios;
1125 tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
1126 tty_drv->init_termios.c_ispeed = 4800;
1127 tty_drv->init_termios.c_ospeed = 4800;
1128 tty_set_operations(tty_drv, &sdio_uart_ops);
1129
1130 ret = tty_register_driver(tty_drv);
1131 if (ret)
1132 goto err1;
1133
1134 ret = sdio_register_driver(&sdio_uart_driver);
1135 if (ret)
1136 goto err2;
1137
1138 return 0;
1139
1140err2:
1141 tty_unregister_driver(tty_drv);
1142err1:
1143 put_tty_driver(tty_drv);
1144 return ret;
1145}
1146
1147static void __exit sdio_uart_exit(void)
1148{
1149 sdio_unregister_driver(&sdio_uart_driver);
1150 tty_unregister_driver(sdio_uart_tty_driver);
1151 put_tty_driver(sdio_uart_tty_driver);
1152}
1153
1154module_init(sdio_uart_init);
1155module_exit(sdio_uart_exit);
1156
1157MODULE_AUTHOR("Nicolas Pitre");
1158MODULE_LICENSE("GPL");
diff --git a/drivers/mmc/core/Makefile b/drivers/mmc/core/Makefile
index 3fdd08c7f143..4985807257a8 100644
--- a/drivers/mmc/core/Makefile
+++ b/drivers/mmc/core/Makefile
@@ -8,5 +8,7 @@ endif
8 8
9obj-$(CONFIG_MMC) += mmc_core.o 9obj-$(CONFIG_MMC) += mmc_core.o
10mmc_core-y := core.o sysfs.o bus.o host.o \ 10mmc_core-y := core.o sysfs.o bus.o host.o \
11 mmc.o mmc_ops.o sd.o sd_ops.o 11 mmc.o mmc_ops.o sd.o sd_ops.o \
12 sdio.o sdio_ops.o sdio_bus.o \
13 sdio_cis.o sdio_io.o sdio_irq.o
12 14
diff --git a/drivers/mmc/core/bus.c b/drivers/mmc/core/bus.c
index 817a79462b3d..8d6f6014870f 100644
--- a/drivers/mmc/core/bus.c
+++ b/drivers/mmc/core/bus.c
@@ -19,6 +19,7 @@
19 19
20#include "sysfs.h" 20#include "sysfs.h"
21#include "core.h" 21#include "core.h"
22#include "sdio_cis.h"
22#include "bus.h" 23#include "bus.h"
23 24
24#define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev) 25#define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
@@ -34,6 +35,8 @@ static ssize_t mmc_type_show(struct device *dev,
34 return sprintf(buf, "MMC\n"); 35 return sprintf(buf, "MMC\n");
35 case MMC_TYPE_SD: 36 case MMC_TYPE_SD:
36 return sprintf(buf, "SD\n"); 37 return sprintf(buf, "SD\n");
38 case MMC_TYPE_SDIO:
39 return sprintf(buf, "SDIO\n");
37 default: 40 default:
38 return -EFAULT; 41 return -EFAULT;
39 } 42 }
@@ -59,28 +62,34 @@ mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
59 int buf_size) 62 int buf_size)
60{ 63{
61 struct mmc_card *card = dev_to_mmc_card(dev); 64 struct mmc_card *card = dev_to_mmc_card(dev);
62 int retval = 0, i = 0, length = 0; 65 const char *type;
63 66 int i = 0, length = 0;
64#define add_env(fmt,val) do { \
65 retval = add_uevent_var(envp, num_envp, &i, \
66 buf, buf_size, &length, \
67 fmt, val); \
68 if (retval) \
69 return retval; \
70} while (0);
71 67
72 switch (card->type) { 68 switch (card->type) {
73 case MMC_TYPE_MMC: 69 case MMC_TYPE_MMC:
74 add_env("MMC_TYPE=%s", "MMC"); 70 type = "MMC";
75 break; 71 break;
76 case MMC_TYPE_SD: 72 case MMC_TYPE_SD:
77 add_env("MMC_TYPE=%s", "SD"); 73 type = "SD";
74 break;
75 case MMC_TYPE_SDIO:
76 type = "SDIO";
78 break; 77 break;
78 default:
79 type = NULL;
79 } 80 }
80 81
81 add_env("MMC_NAME=%s", mmc_card_name(card)); 82 if (type) {
83 if (add_uevent_var(envp, num_envp, &i,
84 buf, buf_size, &length,
85 "MMC_TYPE=%s", type))
86 return -ENOMEM;
87 }
82 88
83#undef add_env 89 if (add_uevent_var(envp, num_envp, &i,
90 buf, buf_size, &length,
91 "MMC_NAME=%s", mmc_card_name(card)))
92 return -ENOMEM;
84 93
85 envp[i] = NULL; 94 envp[i] = NULL;
86 95
@@ -176,6 +185,11 @@ static void mmc_release_card(struct device *dev)
176{ 185{
177 struct mmc_card *card = dev_to_mmc_card(dev); 186 struct mmc_card *card = dev_to_mmc_card(dev);
178 187
188 sdio_free_common_cis(card);
189
190 if (card->info)
191 kfree(card->info);
192
179 kfree(card); 193 kfree(card);
180} 194}
181 195
@@ -221,15 +235,25 @@ int mmc_add_card(struct mmc_card *card)
221 if (mmc_card_blockaddr(card)) 235 if (mmc_card_blockaddr(card))
222 type = "SDHC"; 236 type = "SDHC";
223 break; 237 break;
238 case MMC_TYPE_SDIO:
239 type = "SDIO";
240 break;
224 default: 241 default:
225 type = "?"; 242 type = "?";
226 break; 243 break;
227 } 244 }
228 245
229 printk(KERN_INFO "%s: new %s%s card at address %04x\n", 246 if (mmc_host_is_spi(card->host)) {
230 mmc_hostname(card->host), 247 printk(KERN_INFO "%s: new %s%s card on SPI\n",
231 mmc_card_highspeed(card) ? "high speed " : "", 248 mmc_hostname(card->host),
232 type, card->rca); 249 mmc_card_highspeed(card) ? "high speed " : "",
250 type);
251 } else {
252 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
253 mmc_hostname(card->host),
254 mmc_card_highspeed(card) ? "high speed " : "",
255 type, card->rca);
256 }
233 257
234 card->dev.uevent_suppress = 1; 258 card->dev.uevent_suppress = 1;
235 259
@@ -261,8 +285,13 @@ int mmc_add_card(struct mmc_card *card)
261void mmc_remove_card(struct mmc_card *card) 285void mmc_remove_card(struct mmc_card *card)
262{ 286{
263 if (mmc_card_present(card)) { 287 if (mmc_card_present(card)) {
264 printk(KERN_INFO "%s: card %04x removed\n", 288 if (mmc_host_is_spi(card->host)) {
265 mmc_hostname(card->host), card->rca); 289 printk(KERN_INFO "%s: SPI card removed\n",
290 mmc_hostname(card->host));
291 } else {
292 printk(KERN_INFO "%s: card %04x removed\n",
293 mmc_hostname(card->host), card->rca);
294 }
266 295
267 if (card->host->bus_ops->sysfs_remove) 296 if (card->host->bus_ops->sysfs_remove)
268 card->host->bus_ops->sysfs_remove(card->host, card); 297 card->host->bus_ops->sysfs_remove(card->host, card);
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index bfd2ae5bd669..09435e0ec680 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -18,6 +18,7 @@
18#include <linux/delay.h> 18#include <linux/delay.h>
19#include <linux/pagemap.h> 19#include <linux/pagemap.h>
20#include <linux/err.h> 20#include <linux/err.h>
21#include <linux/leds.h>
21#include <asm/scatterlist.h> 22#include <asm/scatterlist.h>
22#include <linux/scatterlist.h> 23#include <linux/scatterlist.h>
23 24
@@ -29,16 +30,27 @@
29#include "core.h" 30#include "core.h"
30#include "bus.h" 31#include "bus.h"
31#include "host.h" 32#include "host.h"
33#include "sdio_bus.h"
32 34
33#include "mmc_ops.h" 35#include "mmc_ops.h"
34#include "sd_ops.h" 36#include "sd_ops.h"
37#include "sdio_ops.h"
35 38
36extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr); 39extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
37extern int mmc_attach_sd(struct mmc_host *host, u32 ocr); 40extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
41extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
38 42
39static struct workqueue_struct *workqueue; 43static struct workqueue_struct *workqueue;
40 44
41/* 45/*
46 * Enabling software CRCs on the data blocks can be a significant (30%)
47 * performance cost, and for other reasons may not always be desired.
48 * So we allow it it to be disabled.
49 */
50int use_spi_crc = 1;
51module_param(use_spi_crc, bool, 0);
52
53/*
42 * Internal function. Schedule delayed work in the MMC work queue. 54 * Internal function. Schedule delayed work in the MMC work queue.
43 */ 55 */
44static int mmc_schedule_delayed_work(struct delayed_work *work, 56static int mmc_schedule_delayed_work(struct delayed_work *work,
@@ -68,6 +80,11 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
68 struct mmc_command *cmd = mrq->cmd; 80 struct mmc_command *cmd = mrq->cmd;
69 int err = cmd->error; 81 int err = cmd->error;
70 82
83 if (err && cmd->retries && mmc_host_is_spi(host)) {
84 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
85 cmd->retries = 0;
86 }
87
71 if (err && cmd->retries) { 88 if (err && cmd->retries) {
72 pr_debug("%s: req failed (CMD%u): %d, retrying...\n", 89 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
73 mmc_hostname(host), cmd->opcode, err); 90 mmc_hostname(host), cmd->opcode, err);
@@ -76,6 +93,8 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
76 cmd->error = 0; 93 cmd->error = 0;
77 host->ops->request(host, mrq); 94 host->ops->request(host, mrq);
78 } else { 95 } else {
96 led_trigger_event(host->led, LED_OFF);
97
79 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", 98 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
80 mmc_hostname(host), cmd->opcode, err, 99 mmc_hostname(host), cmd->opcode, err,
81 cmd->resp[0], cmd->resp[1], 100 cmd->resp[0], cmd->resp[1],
@@ -118,7 +137,7 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
118 "tsac %d ms nsac %d\n", 137 "tsac %d ms nsac %d\n",
119 mmc_hostname(host), mrq->data->blksz, 138 mmc_hostname(host), mrq->data->blksz,
120 mrq->data->blocks, mrq->data->flags, 139 mrq->data->blocks, mrq->data->flags,
121 mrq->data->timeout_ns / 10000000, 140 mrq->data->timeout_ns / 1000000,
122 mrq->data->timeout_clks); 141 mrq->data->timeout_clks);
123 } 142 }
124 143
@@ -130,6 +149,8 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
130 149
131 WARN_ON(!host->claimed); 150 WARN_ON(!host->claimed);
132 151
152 led_trigger_event(host->led, LED_FULL);
153
133 mrq->cmd->error = 0; 154 mrq->cmd->error = 0;
134 mrq->cmd->mrq = mrq; 155 mrq->cmd->mrq = mrq;
135 if (mrq->data) { 156 if (mrq->data) {
@@ -199,7 +220,7 @@ int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries
199{ 220{
200 struct mmc_request mrq; 221 struct mmc_request mrq;
201 222
202 BUG_ON(!host->claimed); 223 WARN_ON(!host->claimed);
203 224
204 memset(&mrq, 0, sizeof(struct mmc_request)); 225 memset(&mrq, 0, sizeof(struct mmc_request));
205 226
@@ -220,17 +241,24 @@ EXPORT_SYMBOL(mmc_wait_for_cmd);
220 * mmc_set_data_timeout - set the timeout for a data command 241 * mmc_set_data_timeout - set the timeout for a data command
221 * @data: data phase for command 242 * @data: data phase for command
222 * @card: the MMC card associated with the data transfer 243 * @card: the MMC card associated with the data transfer
223 * @write: flag to differentiate reads from writes
224 * 244 *
225 * Computes the data timeout parameters according to the 245 * Computes the data timeout parameters according to the
226 * correct algorithm given the card type. 246 * correct algorithm given the card type.
227 */ 247 */
228void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card, 248void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
229 int write)
230{ 249{
231 unsigned int mult; 250 unsigned int mult;
232 251
233 /* 252 /*
253 * SDIO cards only define an upper 1 s limit on access.
254 */
255 if (mmc_card_sdio(card)) {
256 data->timeout_ns = 1000000000;
257 data->timeout_clks = 0;
258 return;
259 }
260
261 /*
234 * SD cards use a 100 multiplier rather than 10 262 * SD cards use a 100 multiplier rather than 10
235 */ 263 */
236 mult = mmc_card_sd(card) ? 100 : 10; 264 mult = mmc_card_sd(card) ? 100 : 10;
@@ -239,7 +267,7 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
239 * Scale up the multiplier (and therefore the timeout) by 267 * Scale up the multiplier (and therefore the timeout) by
240 * the r2w factor for writes. 268 * the r2w factor for writes.
241 */ 269 */
242 if (write) 270 if (data->flags & MMC_DATA_WRITE)
243 mult <<= card->csd.r2w_factor; 271 mult <<= card->csd.r2w_factor;
244 272
245 data->timeout_ns = card->csd.tacc_ns * mult; 273 data->timeout_ns = card->csd.tacc_ns * mult;
@@ -255,7 +283,7 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
255 timeout_us += data->timeout_clks * 1000 / 283 timeout_us += data->timeout_clks * 1000 /
256 (card->host->ios.clock / 1000); 284 (card->host->ios.clock / 1000);
257 285
258 if (write) 286 if (data->flags & MMC_DATA_WRITE)
259 limit_us = 250000; 287 limit_us = 250000;
260 else 288 else
261 limit_us = 100000; 289 limit_us = 100000;
@@ -272,15 +300,20 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
272EXPORT_SYMBOL(mmc_set_data_timeout); 300EXPORT_SYMBOL(mmc_set_data_timeout);
273 301
274/** 302/**
275 * mmc_claim_host - exclusively claim a host 303 * __mmc_claim_host - exclusively claim a host
276 * @host: mmc host to claim 304 * @host: mmc host to claim
305 * @abort: whether or not the operation should be aborted
277 * 306 *
278 * Claim a host for a set of operations. 307 * Claim a host for a set of operations. If @abort is non null and
308 * dereference a non-zero value then this will return prematurely with
309 * that non-zero value without acquiring the lock. Returns zero
310 * with the lock held otherwise.
279 */ 311 */
280void mmc_claim_host(struct mmc_host *host) 312int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
281{ 313{
282 DECLARE_WAITQUEUE(wait, current); 314 DECLARE_WAITQUEUE(wait, current);
283 unsigned long flags; 315 unsigned long flags;
316 int stop;
284 317
285 might_sleep(); 318 might_sleep();
286 319
@@ -288,19 +321,24 @@ void mmc_claim_host(struct mmc_host *host)
288 spin_lock_irqsave(&host->lock, flags); 321 spin_lock_irqsave(&host->lock, flags);
289 while (1) { 322 while (1) {
290 set_current_state(TASK_UNINTERRUPTIBLE); 323 set_current_state(TASK_UNINTERRUPTIBLE);
291 if (!host->claimed) 324 stop = abort ? atomic_read(abort) : 0;
325 if (stop || !host->claimed)
292 break; 326 break;
293 spin_unlock_irqrestore(&host->lock, flags); 327 spin_unlock_irqrestore(&host->lock, flags);
294 schedule(); 328 schedule();
295 spin_lock_irqsave(&host->lock, flags); 329 spin_lock_irqsave(&host->lock, flags);
296 } 330 }
297 set_current_state(TASK_RUNNING); 331 set_current_state(TASK_RUNNING);
298 host->claimed = 1; 332 if (!stop)
333 host->claimed = 1;
334 else
335 wake_up(&host->wq);
299 spin_unlock_irqrestore(&host->lock, flags); 336 spin_unlock_irqrestore(&host->lock, flags);
300 remove_wait_queue(&host->wq, &wait); 337 remove_wait_queue(&host->wq, &wait);
338 return stop;
301} 339}
302 340
303EXPORT_SYMBOL(mmc_claim_host); 341EXPORT_SYMBOL(__mmc_claim_host);
304 342
305/** 343/**
306 * mmc_release_host - release a host 344 * mmc_release_host - release a host
@@ -313,7 +351,7 @@ void mmc_release_host(struct mmc_host *host)
313{ 351{
314 unsigned long flags; 352 unsigned long flags;
315 353
316 BUG_ON(!host->claimed); 354 WARN_ON(!host->claimed);
317 355
318 spin_lock_irqsave(&host->lock, flags); 356 spin_lock_irqsave(&host->lock, flags);
319 host->claimed = 0; 357 host->claimed = 0;
@@ -433,19 +471,32 @@ static void mmc_power_up(struct mmc_host *host)
433 int bit = fls(host->ocr_avail) - 1; 471 int bit = fls(host->ocr_avail) - 1;
434 472
435 host->ios.vdd = bit; 473 host->ios.vdd = bit;
436 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 474 if (mmc_host_is_spi(host)) {
437 host->ios.chip_select = MMC_CS_DONTCARE; 475 host->ios.chip_select = MMC_CS_HIGH;
476 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
477 } else {
478 host->ios.chip_select = MMC_CS_DONTCARE;
479 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
480 }
438 host->ios.power_mode = MMC_POWER_UP; 481 host->ios.power_mode = MMC_POWER_UP;
439 host->ios.bus_width = MMC_BUS_WIDTH_1; 482 host->ios.bus_width = MMC_BUS_WIDTH_1;
440 host->ios.timing = MMC_TIMING_LEGACY; 483 host->ios.timing = MMC_TIMING_LEGACY;
441 mmc_set_ios(host); 484 mmc_set_ios(host);
442 485
443 mmc_delay(1); 486 /*
487 * This delay should be sufficient to allow the power supply
488 * to reach the minimum voltage.
489 */
490 mmc_delay(2);
444 491
445 host->ios.clock = host->f_min; 492 host->ios.clock = host->f_min;
446 host->ios.power_mode = MMC_POWER_ON; 493 host->ios.power_mode = MMC_POWER_ON;
447 mmc_set_ios(host); 494 mmc_set_ios(host);
448 495
496 /*
497 * This delay must be at least 74 clock sizes, or 1 ms, or the
498 * time required to reach a stable voltage.
499 */
449 mmc_delay(2); 500 mmc_delay(2);
450} 501}
451 502
@@ -453,8 +504,10 @@ static void mmc_power_off(struct mmc_host *host)
453{ 504{
454 host->ios.clock = 0; 505 host->ios.clock = 0;
455 host->ios.vdd = 0; 506 host->ios.vdd = 0;
456 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 507 if (!mmc_host_is_spi(host)) {
457 host->ios.chip_select = MMC_CS_DONTCARE; 508 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
509 host->ios.chip_select = MMC_CS_DONTCARE;
510 }
458 host->ios.power_mode = MMC_POWER_OFF; 511 host->ios.power_mode = MMC_POWER_OFF;
459 host->ios.bus_width = MMC_BUS_WIDTH_1; 512 host->ios.bus_width = MMC_BUS_WIDTH_1;
460 host->ios.timing = MMC_TIMING_LEGACY; 513 host->ios.timing = MMC_TIMING_LEGACY;
@@ -511,7 +564,7 @@ void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
511 BUG_ON(!host); 564 BUG_ON(!host);
512 BUG_ON(!ops); 565 BUG_ON(!ops);
513 566
514 BUG_ON(!host->claimed); 567 WARN_ON(!host->claimed);
515 568
516 spin_lock_irqsave(&host->lock, flags); 569 spin_lock_irqsave(&host->lock, flags);
517 570
@@ -535,8 +588,8 @@ void mmc_detach_bus(struct mmc_host *host)
535 588
536 BUG_ON(!host); 589 BUG_ON(!host);
537 590
538 BUG_ON(!host->claimed); 591 WARN_ON(!host->claimed);
539 BUG_ON(!host->bus_ops); 592 WARN_ON(!host->bus_ops);
540 593
541 spin_lock_irqsave(&host->lock, flags); 594 spin_lock_irqsave(&host->lock, flags);
542 595
@@ -564,7 +617,7 @@ void mmc_detect_change(struct mmc_host *host, unsigned long delay)
564#ifdef CONFIG_MMC_DEBUG 617#ifdef CONFIG_MMC_DEBUG
565 unsigned long flags; 618 unsigned long flags;
566 spin_lock_irqsave(&host->lock, flags); 619 spin_lock_irqsave(&host->lock, flags);
567 BUG_ON(host->removed); 620 WARN_ON(host->removed);
568 spin_unlock_irqrestore(&host->lock, flags); 621 spin_unlock_irqrestore(&host->lock, flags);
569#endif 622#endif
570 623
@@ -597,24 +650,38 @@ void mmc_rescan(struct work_struct *work)
597 650
598 mmc_send_if_cond(host, host->ocr_avail); 651 mmc_send_if_cond(host, host->ocr_avail);
599 652
653 /*
654 * First we search for SDIO...
655 */
656 err = mmc_send_io_op_cond(host, 0, &ocr);
657 if (!err) {
658 if (mmc_attach_sdio(host, ocr))
659 mmc_power_off(host);
660 return;
661 }
662
663 /*
664 * ...then normal SD...
665 */
600 err = mmc_send_app_op_cond(host, 0, &ocr); 666 err = mmc_send_app_op_cond(host, 0, &ocr);
601 if (err == MMC_ERR_NONE) { 667 if (!err) {
602 if (mmc_attach_sd(host, ocr)) 668 if (mmc_attach_sd(host, ocr))
603 mmc_power_off(host); 669 mmc_power_off(host);
604 } else { 670 return;
605 /* 671 }
606 * If we fail to detect any SD cards then try 672
607 * searching for MMC cards. 673 /*
608 */ 674 * ...and finally MMC.
609 err = mmc_send_op_cond(host, 0, &ocr); 675 */
610 if (err == MMC_ERR_NONE) { 676 err = mmc_send_op_cond(host, 0, &ocr);
611 if (mmc_attach_mmc(host, ocr)) 677 if (!err) {
612 mmc_power_off(host); 678 if (mmc_attach_mmc(host, ocr))
613 } else {
614 mmc_power_off(host); 679 mmc_power_off(host);
615 mmc_release_host(host); 680 return;
616 }
617 } 681 }
682
683 mmc_release_host(host);
684 mmc_power_off(host);
618 } else { 685 } else {
619 if (host->bus_ops->detect && !host->bus_dead) 686 if (host->bus_ops->detect && !host->bus_dead)
620 host->bus_ops->detect(host); 687 host->bus_ops->detect(host);
@@ -725,22 +792,38 @@ static int __init mmc_init(void)
725 return -ENOMEM; 792 return -ENOMEM;
726 793
727 ret = mmc_register_bus(); 794 ret = mmc_register_bus();
728 if (ret == 0) { 795 if (ret)
729 ret = mmc_register_host_class(); 796 goto destroy_workqueue;
730 if (ret) 797
731 mmc_unregister_bus(); 798 ret = mmc_register_host_class();
732 } 799 if (ret)
800 goto unregister_bus;
801
802 ret = sdio_register_bus();
803 if (ret)
804 goto unregister_host_class;
805
806 return 0;
807
808unregister_host_class:
809 mmc_unregister_host_class();
810unregister_bus:
811 mmc_unregister_bus();
812destroy_workqueue:
813 destroy_workqueue(workqueue);
814
733 return ret; 815 return ret;
734} 816}
735 817
736static void __exit mmc_exit(void) 818static void __exit mmc_exit(void)
737{ 819{
820 sdio_unregister_bus();
738 mmc_unregister_host_class(); 821 mmc_unregister_host_class();
739 mmc_unregister_bus(); 822 mmc_unregister_bus();
740 destroy_workqueue(workqueue); 823 destroy_workqueue(workqueue);
741} 824}
742 825
743module_init(mmc_init); 826subsys_initcall(mmc_init);
744module_exit(mmc_exit); 827module_exit(mmc_exit);
745 828
746MODULE_LICENSE("GPL"); 829MODULE_LICENSE("GPL");
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index bb2774af9ea9..39daf2fb5dc4 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -48,5 +48,7 @@ void mmc_rescan(struct work_struct *work);
48void mmc_start_host(struct mmc_host *host); 48void mmc_start_host(struct mmc_host *host);
49void mmc_stop_host(struct mmc_host *host); 49void mmc_stop_host(struct mmc_host *host);
50 50
51extern int use_spi_crc;
52
51#endif 53#endif
52 54
diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
index 2c7ce8f43a9a..64fbc9759a30 100644
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -15,6 +15,7 @@
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/idr.h> 16#include <linux/idr.h>
17#include <linux/pagemap.h> 17#include <linux/pagemap.h>
18#include <linux/leds.h>
18 19
19#include <linux/mmc/host.h> 20#include <linux/mmc/host.h>
20 21
@@ -100,6 +101,9 @@ int mmc_add_host(struct mmc_host *host)
100{ 101{
101 int err; 102 int err;
102 103
104 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
105 !host->ops->enable_sdio_irq);
106
103 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL)) 107 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
104 return -ENOMEM; 108 return -ENOMEM;
105 109
@@ -112,6 +116,8 @@ int mmc_add_host(struct mmc_host *host)
112 snprintf(host->class_dev.bus_id, BUS_ID_SIZE, 116 snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
113 "mmc%d", host->index); 117 "mmc%d", host->index);
114 118
119 led_trigger_register_simple(host->class_dev.bus_id, &host->led);
120
115 err = device_add(&host->class_dev); 121 err = device_add(&host->class_dev);
116 if (err) 122 if (err)
117 return err; 123 return err;
@@ -137,6 +143,8 @@ void mmc_remove_host(struct mmc_host *host)
137 143
138 device_del(&host->class_dev); 144 device_del(&host->class_dev);
139 145
146 led_trigger_unregister(host->led);
147
140 spin_lock(&mmc_host_lock); 148 spin_lock(&mmc_host_lock);
141 idr_remove(&mmc_host_idr, host->index); 149 idr_remove(&mmc_host_idr, host->index);
142 spin_unlock(&mmc_host_lock); 150 spin_unlock(&mmc_host_lock);
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 21d7f48e1d4e..65fe28860f54 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -161,13 +161,12 @@ static int mmc_read_ext_csd(struct mmc_card *card)
161{ 161{
162 int err; 162 int err;
163 u8 *ext_csd; 163 u8 *ext_csd;
164 unsigned int ext_csd_struct;
164 165
165 BUG_ON(!card); 166 BUG_ON(!card);
166 167
167 err = MMC_ERR_FAILED;
168
169 if (card->csd.mmca_vsn < CSD_SPEC_VER_4) 168 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
170 return MMC_ERR_NONE; 169 return 0;
171 170
172 /* 171 /*
173 * As the ext_csd is so large and mostly unused, we don't store the 172 * As the ext_csd is so large and mostly unused, we don't store the
@@ -176,13 +175,19 @@ static int mmc_read_ext_csd(struct mmc_card *card)
176 ext_csd = kmalloc(512, GFP_KERNEL); 175 ext_csd = kmalloc(512, GFP_KERNEL);
177 if (!ext_csd) { 176 if (!ext_csd) {
178 printk(KERN_ERR "%s: could not allocate a buffer to " 177 printk(KERN_ERR "%s: could not allocate a buffer to "
179 "receive the ext_csd. mmc v4 cards will be " 178 "receive the ext_csd.\n", mmc_hostname(card->host));
180 "treated as v3.\n", mmc_hostname(card->host)); 179 return -ENOMEM;
181 return MMC_ERR_FAILED;
182 } 180 }
183 181
184 err = mmc_send_ext_csd(card, ext_csd); 182 err = mmc_send_ext_csd(card, ext_csd);
185 if (err != MMC_ERR_NONE) { 183 if (err) {
184 /*
185 * We all hosts that cannot perform the command
186 * to fail more gracefully
187 */
188 if (err != -EINVAL)
189 goto out;
190
186 /* 191 /*
187 * High capacity cards should have this "magic" size 192 * High capacity cards should have this "magic" size
188 * stored in their CSD. 193 * stored in their CSD.
@@ -197,18 +202,29 @@ static int mmc_read_ext_csd(struct mmc_card *card)
197 "EXT_CSD, performance might " 202 "EXT_CSD, performance might "
198 "suffer.\n", 203 "suffer.\n",
199 mmc_hostname(card->host)); 204 mmc_hostname(card->host));
200 err = MMC_ERR_NONE; 205 err = 0;
201 } 206 }
207
202 goto out; 208 goto out;
203 } 209 }
204 210
205 card->ext_csd.sectors = 211 ext_csd_struct = ext_csd[EXT_CSD_REV];
206 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 212 if (ext_csd_struct > 2) {
207 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 213 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
208 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 214 "version %d\n", mmc_hostname(card->host),
209 ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 215 ext_csd_struct);
210 if (card->ext_csd.sectors) 216 return -EINVAL;
211 mmc_card_set_blockaddr(card); 217 }
218
219 if (ext_csd_struct >= 2) {
220 card->ext_csd.sectors =
221 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
222 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
223 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
224 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
225 if (card->ext_csd.sectors)
226 mmc_card_set_blockaddr(card);
227 }
212 228
213 switch (ext_csd[EXT_CSD_CARD_TYPE]) { 229 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
214 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 230 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
@@ -246,7 +262,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
246 unsigned int max_dtr; 262 unsigned int max_dtr;
247 263
248 BUG_ON(!host); 264 BUG_ON(!host);
249 BUG_ON(!host->claimed); 265 WARN_ON(!host->claimed);
250 266
251 /* 267 /*
252 * Since we're changing the OCR value, we seem to 268 * Since we're changing the OCR value, we seem to
@@ -258,19 +274,33 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
258 274
259 /* The extra bit indicates that we support high capacity */ 275 /* The extra bit indicates that we support high capacity */
260 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL); 276 err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
261 if (err != MMC_ERR_NONE) 277 if (err)
262 goto err; 278 goto err;
263 279
264 /* 280 /*
281 * For SPI, enable CRC as appropriate.
282 */
283 if (mmc_host_is_spi(host)) {
284 err = mmc_spi_set_crc(host, use_spi_crc);
285 if (err)
286 goto err;
287 }
288
289 /*
265 * Fetch CID from card. 290 * Fetch CID from card.
266 */ 291 */
267 err = mmc_all_send_cid(host, cid); 292 if (mmc_host_is_spi(host))
268 if (err != MMC_ERR_NONE) 293 err = mmc_send_cid(host, cid);
294 else
295 err = mmc_all_send_cid(host, cid);
296 if (err)
269 goto err; 297 goto err;
270 298
271 if (oldcard) { 299 if (oldcard) {
272 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 300 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
301 err = -ENOENT;
273 goto err; 302 goto err;
303 }
274 304
275 card = oldcard; 305 card = oldcard;
276 } else { 306 } else {
@@ -278,8 +308,10 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
278 * Allocate card structure. 308 * Allocate card structure.
279 */ 309 */
280 card = mmc_alloc_card(host); 310 card = mmc_alloc_card(host);
281 if (IS_ERR(card)) 311 if (IS_ERR(card)) {
312 err = PTR_ERR(card);
282 goto err; 313 goto err;
314 }
283 315
284 card->type = MMC_TYPE_MMC; 316 card->type = MMC_TYPE_MMC;
285 card->rca = 1; 317 card->rca = 1;
@@ -287,43 +319,47 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
287 } 319 }
288 320
289 /* 321 /*
290 * Set card RCA. 322 * For native busses: set card RCA and quit open drain mode.
291 */ 323 */
292 err = mmc_set_relative_addr(card); 324 if (!mmc_host_is_spi(host)) {
293 if (err != MMC_ERR_NONE) 325 err = mmc_set_relative_addr(card);
294 goto free_card; 326 if (err)
327 goto free_card;
295 328
296 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 329 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
330 }
297 331
298 if (!oldcard) { 332 if (!oldcard) {
299 /* 333 /*
300 * Fetch CSD from card. 334 * Fetch CSD from card.
301 */ 335 */
302 err = mmc_send_csd(card, card->raw_csd); 336 err = mmc_send_csd(card, card->raw_csd);
303 if (err != MMC_ERR_NONE) 337 if (err)
304 goto free_card; 338 goto free_card;
305 339
306 err = mmc_decode_csd(card); 340 err = mmc_decode_csd(card);
307 if (err < 0) 341 if (err)
308 goto free_card; 342 goto free_card;
309 err = mmc_decode_cid(card); 343 err = mmc_decode_cid(card);
310 if (err < 0) 344 if (err)
311 goto free_card; 345 goto free_card;
312 } 346 }
313 347
314 /* 348 /*
315 * Select card, as all following commands rely on that. 349 * Select card, as all following commands rely on that.
316 */ 350 */
317 err = mmc_select_card(card); 351 if (!mmc_host_is_spi(host)) {
318 if (err != MMC_ERR_NONE) 352 err = mmc_select_card(card);
319 goto free_card; 353 if (err)
354 goto free_card;
355 }
320 356
321 if (!oldcard) { 357 if (!oldcard) {
322 /* 358 /*
323 * Fetch and process extened CSD. 359 * Fetch and process extended CSD.
324 */ 360 */
325 err = mmc_read_ext_csd(card); 361 err = mmc_read_ext_csd(card);
326 if (err != MMC_ERR_NONE) 362 if (err)
327 goto free_card; 363 goto free_card;
328 } 364 }
329 365
@@ -334,7 +370,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
334 (host->caps & MMC_CAP_MMC_HIGHSPEED)) { 370 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
335 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 371 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
336 EXT_CSD_HS_TIMING, 1); 372 EXT_CSD_HS_TIMING, 1);
337 if (err != MMC_ERR_NONE) 373 if (err)
338 goto free_card; 374 goto free_card;
339 375
340 mmc_card_set_highspeed(card); 376 mmc_card_set_highspeed(card);
@@ -363,7 +399,7 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
363 (host->caps & MMC_CAP_4_BIT_DATA)) { 399 (host->caps & MMC_CAP_4_BIT_DATA)) {
364 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 400 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
365 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4); 401 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
366 if (err != MMC_ERR_NONE) 402 if (err)
367 goto free_card; 403 goto free_card;
368 404
369 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 405 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
@@ -372,14 +408,14 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
372 if (!oldcard) 408 if (!oldcard)
373 host->card = card; 409 host->card = card;
374 410
375 return MMC_ERR_NONE; 411 return 0;
376 412
377free_card: 413free_card:
378 if (!oldcard) 414 if (!oldcard)
379 mmc_remove_card(card); 415 mmc_remove_card(card);
380err: 416err:
381 417
382 return MMC_ERR_FAILED; 418 return err;
383} 419}
384 420
385/* 421/*
@@ -413,7 +449,7 @@ static void mmc_detect(struct mmc_host *host)
413 449
414 mmc_release_host(host); 450 mmc_release_host(host);
415 451
416 if (err != MMC_ERR_NONE) { 452 if (err) {
417 mmc_remove(host); 453 mmc_remove(host);
418 454
419 mmc_claim_host(host); 455 mmc_claim_host(host);
@@ -480,7 +516,8 @@ static void mmc_suspend(struct mmc_host *host)
480 BUG_ON(!host->card); 516 BUG_ON(!host->card);
481 517
482 mmc_claim_host(host); 518 mmc_claim_host(host);
483 mmc_deselect_cards(host); 519 if (!mmc_host_is_spi(host))
520 mmc_deselect_cards(host);
484 host->card->state &= ~MMC_STATE_HIGHSPEED; 521 host->card->state &= ~MMC_STATE_HIGHSPEED;
485 mmc_release_host(host); 522 mmc_release_host(host);
486} 523}
@@ -502,7 +539,7 @@ static void mmc_resume(struct mmc_host *host)
502 err = mmc_init_card(host, host->ocr, host->card); 539 err = mmc_init_card(host, host->ocr, host->card);
503 mmc_release_host(host); 540 mmc_release_host(host);
504 541
505 if (err != MMC_ERR_NONE) { 542 if (err) {
506 mmc_remove(host); 543 mmc_remove(host);
507 544
508 mmc_claim_host(host); 545 mmc_claim_host(host);
@@ -536,11 +573,20 @@ int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
536 int err; 573 int err;
537 574
538 BUG_ON(!host); 575 BUG_ON(!host);
539 BUG_ON(!host->claimed); 576 WARN_ON(!host->claimed);
540 577
541 mmc_attach_bus(host, &mmc_ops); 578 mmc_attach_bus(host, &mmc_ops);
542 579
543 /* 580 /*
581 * We need to get OCR a different way for SPI.
582 */
583 if (mmc_host_is_spi(host)) {
584 err = mmc_spi_read_ocr(host, 1, &ocr);
585 if (err)
586 goto err;
587 }
588
589 /*
544 * Sanity check the voltages that the card claims to 590 * Sanity check the voltages that the card claims to
545 * support. 591 * support.
546 */ 592 */
@@ -565,7 +611,7 @@ int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
565 * Detect and init the card. 611 * Detect and init the card.
566 */ 612 */
567 err = mmc_init_card(host, host->ocr, NULL); 613 err = mmc_init_card(host, host->ocr, NULL);
568 if (err != MMC_ERR_NONE) 614 if (err)
569 goto err; 615 goto err;
570 616
571 mmc_release_host(host); 617 mmc_release_host(host);
@@ -587,6 +633,6 @@ err:
587 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n", 633 printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
588 mmc_hostname(host), err); 634 mmc_hostname(host), err);
589 635
590 return 0; 636 return err;
591} 637}
592 638
diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c
index 913e75f00843..bf4bc6adcfef 100644
--- a/drivers/mmc/core/mmc_ops.c
+++ b/drivers/mmc/core/mmc_ops.c
@@ -40,10 +40,10 @@ static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
40 } 40 }
41 41
42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
43 if (err != MMC_ERR_NONE) 43 if (err)
44 return err; 44 return err;
45 45
46 return MMC_ERR_NONE; 46 return 0;
47} 47}
48 48
49int mmc_select_card(struct mmc_card *card) 49int mmc_select_card(struct mmc_card *card)
@@ -63,23 +63,36 @@ int mmc_go_idle(struct mmc_host *host)
63 int err; 63 int err;
64 struct mmc_command cmd; 64 struct mmc_command cmd;
65 65
66 mmc_set_chip_select(host, MMC_CS_HIGH); 66 /*
67 67 * Non-SPI hosts need to prevent chipselect going active during
68 mmc_delay(1); 68 * GO_IDLE; that would put chips into SPI mode. Remind them of
69 * that in case of hardware that won't pull up DAT3/nCS otherwise.
70 *
71 * SPI hosts ignore ios.chip_select; it's managed according to
72 * rules that must accomodate non-MMC slaves which this layer
73 * won't even know about.
74 */
75 if (!mmc_host_is_spi(host)) {
76 mmc_set_chip_select(host, MMC_CS_HIGH);
77 mmc_delay(1);
78 }
69 79
70 memset(&cmd, 0, sizeof(struct mmc_command)); 80 memset(&cmd, 0, sizeof(struct mmc_command));
71 81
72 cmd.opcode = MMC_GO_IDLE_STATE; 82 cmd.opcode = MMC_GO_IDLE_STATE;
73 cmd.arg = 0; 83 cmd.arg = 0;
74 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 84 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
75 85
76 err = mmc_wait_for_cmd(host, &cmd, 0); 86 err = mmc_wait_for_cmd(host, &cmd, 0);
77 87
78 mmc_delay(1); 88 mmc_delay(1);
79 89
80 mmc_set_chip_select(host, MMC_CS_DONTCARE); 90 if (!mmc_host_is_spi(host)) {
91 mmc_set_chip_select(host, MMC_CS_DONTCARE);
92 mmc_delay(1);
93 }
81 94
82 mmc_delay(1); 95 host->use_spi_crc = 0;
83 96
84 return err; 97 return err;
85} 98}
@@ -94,23 +107,33 @@ int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
94 memset(&cmd, 0, sizeof(struct mmc_command)); 107 memset(&cmd, 0, sizeof(struct mmc_command));
95 108
96 cmd.opcode = MMC_SEND_OP_COND; 109 cmd.opcode = MMC_SEND_OP_COND;
97 cmd.arg = ocr; 110 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
98 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 111 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
99 112
100 for (i = 100; i; i--) { 113 for (i = 100; i; i--) {
101 err = mmc_wait_for_cmd(host, &cmd, 0); 114 err = mmc_wait_for_cmd(host, &cmd, 0);
102 if (err != MMC_ERR_NONE) 115 if (err)
103 break; 116 break;
104 117
105 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) 118 /* if we're just probing, do a single pass */
119 if (ocr == 0)
106 break; 120 break;
107 121
108 err = MMC_ERR_TIMEOUT; 122 /* otherwise wait until reset completes */
123 if (mmc_host_is_spi(host)) {
124 if (!(cmd.resp[0] & R1_SPI_IDLE))
125 break;
126 } else {
127 if (cmd.resp[0] & MMC_CARD_BUSY)
128 break;
129 }
130
131 err = -ETIMEDOUT;
109 132
110 mmc_delay(10); 133 mmc_delay(10);
111 } 134 }
112 135
113 if (rocr) 136 if (rocr && !mmc_host_is_spi(host))
114 *rocr = cmd.resp[0]; 137 *rocr = cmd.resp[0];
115 138
116 return err; 139 return err;
@@ -131,12 +154,12 @@ int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
131 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 154 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
132 155
133 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 156 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE) 157 if (err)
135 return err; 158 return err;
136 159
137 memcpy(cid, cmd.resp, sizeof(u32) * 4); 160 memcpy(cid, cmd.resp, sizeof(u32) * 4);
138 161
139 return MMC_ERR_NONE; 162 return 0;
140} 163}
141 164
142int mmc_set_relative_addr(struct mmc_card *card) 165int mmc_set_relative_addr(struct mmc_card *card)
@@ -154,46 +177,52 @@ int mmc_set_relative_addr(struct mmc_card *card)
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 177 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
155 178
156 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 179 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
157 if (err != MMC_ERR_NONE) 180 if (err)
158 return err; 181 return err;
159 182
160 return MMC_ERR_NONE; 183 return 0;
161} 184}
162 185
163int mmc_send_csd(struct mmc_card *card, u32 *csd) 186static int
187mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
164{ 188{
165 int err; 189 int err;
166 struct mmc_command cmd; 190 struct mmc_command cmd;
167 191
168 BUG_ON(!card); 192 BUG_ON(!host);
169 BUG_ON(!card->host); 193 BUG_ON(!cxd);
170 BUG_ON(!csd);
171 194
172 memset(&cmd, 0, sizeof(struct mmc_command)); 195 memset(&cmd, 0, sizeof(struct mmc_command));
173 196
174 cmd.opcode = MMC_SEND_CSD; 197 cmd.opcode = opcode;
175 cmd.arg = card->rca << 16; 198 cmd.arg = arg;
176 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 199 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
177 200
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 201 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
179 if (err != MMC_ERR_NONE) 202 if (err)
180 return err; 203 return err;
181 204
182 memcpy(csd, cmd.resp, sizeof(u32) * 4); 205 memcpy(cxd, cmd.resp, sizeof(u32) * 4);
183 206
184 return MMC_ERR_NONE; 207 return 0;
185} 208}
186 209
187int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd) 210static int
211mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
212 u32 opcode, void *buf, unsigned len)
188{ 213{
189 struct mmc_request mrq; 214 struct mmc_request mrq;
190 struct mmc_command cmd; 215 struct mmc_command cmd;
191 struct mmc_data data; 216 struct mmc_data data;
192 struct scatterlist sg; 217 struct scatterlist sg;
218 void *data_buf;
193 219
194 BUG_ON(!card); 220 /* dma onto stack is unsafe/nonportable, but callers to this
195 BUG_ON(!card->host); 221 * routine normally provide temporary on-stack buffers ...
196 BUG_ON(!ext_csd); 222 */
223 data_buf = kmalloc(len, GFP_KERNEL);
224 if (data_buf == NULL)
225 return -ENOMEM;
197 226
198 memset(&mrq, 0, sizeof(struct mmc_request)); 227 memset(&mrq, 0, sizeof(struct mmc_request));
199 memset(&cmd, 0, sizeof(struct mmc_command)); 228 memset(&cmd, 0, sizeof(struct mmc_command));
@@ -202,28 +231,99 @@ int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
202 mrq.cmd = &cmd; 231 mrq.cmd = &cmd;
203 mrq.data = &data; 232 mrq.data = &data;
204 233
205 cmd.opcode = MMC_SEND_EXT_CSD; 234 cmd.opcode = opcode;
206 cmd.arg = 0; 235 cmd.arg = 0;
207 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
208 236
209 data.blksz = 512; 237 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
238 * rely on callers to never use this with "native" calls for reading
239 * CSD or CID. Native versions of those commands use the R2 type,
240 * not R1 plus a data block.
241 */
242 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
243
244 data.blksz = len;
210 data.blocks = 1; 245 data.blocks = 1;
211 data.flags = MMC_DATA_READ; 246 data.flags = MMC_DATA_READ;
212 data.sg = &sg; 247 data.sg = &sg;
213 data.sg_len = 1; 248 data.sg_len = 1;
214 249
215 sg_init_one(&sg, ext_csd, 512); 250 sg_init_one(&sg, data_buf, len);
216 251
217 mmc_set_data_timeout(&data, card, 0); 252 if (card)
253 mmc_set_data_timeout(&data, card);
218 254
219 mmc_wait_for_req(card->host, &mrq); 255 mmc_wait_for_req(host, &mrq);
220 256
221 if (cmd.error != MMC_ERR_NONE) 257 memcpy(buf, data_buf, len);
258 kfree(data_buf);
259
260 if (cmd.error)
222 return cmd.error; 261 return cmd.error;
223 if (data.error != MMC_ERR_NONE) 262 if (data.error)
224 return data.error; 263 return data.error;
225 264
226 return MMC_ERR_NONE; 265 return 0;
266}
267
268int mmc_send_csd(struct mmc_card *card, u32 *csd)
269{
270 if (!mmc_host_is_spi(card->host))
271 return mmc_send_cxd_native(card->host, card->rca << 16,
272 csd, MMC_SEND_CSD);
273
274 return mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
275}
276
277int mmc_send_cid(struct mmc_host *host, u32 *cid)
278{
279 if (!mmc_host_is_spi(host)) {
280 if (!host->card)
281 return -EINVAL;
282 return mmc_send_cxd_native(host, host->card->rca << 16,
283 cid, MMC_SEND_CID);
284 }
285
286 return mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
287}
288
289int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
290{
291 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
292 ext_csd, 512);
293}
294
295int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
296{
297 struct mmc_command cmd;
298 int err;
299
300 memset(&cmd, 0, sizeof(struct mmc_command));
301
302 cmd.opcode = MMC_SPI_READ_OCR;
303 cmd.arg = highcap ? (1 << 30) : 0;
304 cmd.flags = MMC_RSP_SPI_R3;
305
306 err = mmc_wait_for_cmd(host, &cmd, 0);
307
308 *ocrp = cmd.resp[1];
309 return err;
310}
311
312int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
313{
314 struct mmc_command cmd;
315 int err;
316
317 memset(&cmd, 0, sizeof(struct mmc_command));
318
319 cmd.opcode = MMC_SPI_CRC_ON_OFF;
320 cmd.flags = MMC_RSP_SPI_R1;
321 cmd.arg = use_crc;
322
323 err = mmc_wait_for_cmd(host, &cmd, 0);
324 if (!err)
325 host->use_spi_crc = use_crc;
326 return err;
227} 327}
228 328
229int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value) 329int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
@@ -241,13 +341,13 @@ int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
241 (index << 16) | 341 (index << 16) |
242 (value << 8) | 342 (value << 8) |
243 set; 343 set;
244 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 344 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
245 345
246 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 346 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
247 if (err != MMC_ERR_NONE) 347 if (err)
248 return err; 348 return err;
249 349
250 return MMC_ERR_NONE; 350 return 0;
251} 351}
252 352
253int mmc_send_status(struct mmc_card *card, u32 *status) 353int mmc_send_status(struct mmc_card *card, u32 *status)
@@ -261,16 +361,20 @@ int mmc_send_status(struct mmc_card *card, u32 *status)
261 memset(&cmd, 0, sizeof(struct mmc_command)); 361 memset(&cmd, 0, sizeof(struct mmc_command));
262 362
263 cmd.opcode = MMC_SEND_STATUS; 363 cmd.opcode = MMC_SEND_STATUS;
264 cmd.arg = card->rca << 16; 364 if (!mmc_host_is_spi(card->host))
265 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 365 cmd.arg = card->rca << 16;
366 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
266 367
267 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 368 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
268 if (err != MMC_ERR_NONE) 369 if (err)
269 return err; 370 return err;
270 371
372 /* NOTE: callers are required to understand the difference
373 * between "native" and SPI format status words!
374 */
271 if (status) 375 if (status)
272 *status = cmd.resp[0]; 376 *status = cmd.resp[0];
273 377
274 return MMC_ERR_NONE; 378 return 0;
275} 379}
276 380
diff --git a/drivers/mmc/core/mmc_ops.h b/drivers/mmc/core/mmc_ops.h
index 76d09a93c5d6..17854bf7cf0d 100644
--- a/drivers/mmc/core/mmc_ops.h
+++ b/drivers/mmc/core/mmc_ops.h
@@ -22,6 +22,9 @@ int mmc_send_csd(struct mmc_card *card, u32 *csd);
22int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd); 22int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
23int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value); 23int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
24int mmc_send_status(struct mmc_card *card, u32 *status); 24int mmc_send_status(struct mmc_card *card, u32 *status);
25int mmc_send_cid(struct mmc_host *host, u32 *cid);
26int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
27int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
25 28
26#endif 29#endif
27 30
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 1edc62b1e5c6..d1c1e0f592f1 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -166,8 +166,6 @@ static int mmc_decode_scr(struct mmc_card *card)
166 unsigned int scr_struct; 166 unsigned int scr_struct;
167 u32 resp[4]; 167 u32 resp[4];
168 168
169 BUG_ON(!mmc_card_sd(card));
170
171 resp[3] = card->raw_scr[1]; 169 resp[3] = card->raw_scr[1];
172 resp[2] = card->raw_scr[0]; 170 resp[2] = card->raw_scr[0];
173 171
@@ -193,30 +191,38 @@ static int mmc_read_switch(struct mmc_card *card)
193 u8 *status; 191 u8 *status;
194 192
195 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 193 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
196 return MMC_ERR_NONE; 194 return 0;
197 195
198 if (!(card->csd.cmdclass & CCC_SWITCH)) { 196 if (!(card->csd.cmdclass & CCC_SWITCH)) {
199 printk(KERN_WARNING "%s: card lacks mandatory switch " 197 printk(KERN_WARNING "%s: card lacks mandatory switch "
200 "function, performance might suffer.\n", 198 "function, performance might suffer.\n",
201 mmc_hostname(card->host)); 199 mmc_hostname(card->host));
202 return MMC_ERR_NONE; 200 return 0;
203 } 201 }
204 202
205 err = MMC_ERR_FAILED; 203 err = -EIO;
206 204
207 status = kmalloc(64, GFP_KERNEL); 205 status = kmalloc(64, GFP_KERNEL);
208 if (!status) { 206 if (!status) {
209 printk(KERN_ERR "%s: could not allocate a buffer for " 207 printk(KERN_ERR "%s: could not allocate a buffer for "
210 "switch capabilities.\n", mmc_hostname(card->host)); 208 "switch capabilities.\n", mmc_hostname(card->host));
211 return err; 209 return -ENOMEM;
212 } 210 }
213 211
214 err = mmc_sd_switch(card, 0, 0, 1, status); 212 err = mmc_sd_switch(card, 0, 0, 1, status);
215 if (err != MMC_ERR_NONE) { 213 if (err) {
214 /*
215 * We all hosts that cannot perform the command
216 * to fail more gracefully
217 */
218 if (err != -EINVAL)
219 goto out;
220
216 printk(KERN_WARNING "%s: problem reading switch " 221 printk(KERN_WARNING "%s: problem reading switch "
217 "capabilities, performance might suffer.\n", 222 "capabilities, performance might suffer.\n",
218 mmc_hostname(card->host)); 223 mmc_hostname(card->host));
219 err = MMC_ERR_NONE; 224 err = 0;
225
220 goto out; 226 goto out;
221 } 227 }
222 228
@@ -238,28 +244,28 @@ static int mmc_switch_hs(struct mmc_card *card)
238 u8 *status; 244 u8 *status;
239 245
240 if (card->scr.sda_vsn < SCR_SPEC_VER_1) 246 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
241 return MMC_ERR_NONE; 247 return 0;
242 248
243 if (!(card->csd.cmdclass & CCC_SWITCH)) 249 if (!(card->csd.cmdclass & CCC_SWITCH))
244 return MMC_ERR_NONE; 250 return 0;
245 251
246 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 252 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
247 return MMC_ERR_NONE; 253 return 0;
248 254
249 if (card->sw_caps.hs_max_dtr == 0) 255 if (card->sw_caps.hs_max_dtr == 0)
250 return MMC_ERR_NONE; 256 return 0;
251 257
252 err = MMC_ERR_FAILED; 258 err = -EIO;
253 259
254 status = kmalloc(64, GFP_KERNEL); 260 status = kmalloc(64, GFP_KERNEL);
255 if (!status) { 261 if (!status) {
256 printk(KERN_ERR "%s: could not allocate a buffer for " 262 printk(KERN_ERR "%s: could not allocate a buffer for "
257 "switch capabilities.\n", mmc_hostname(card->host)); 263 "switch capabilities.\n", mmc_hostname(card->host));
258 return err; 264 return -ENOMEM;
259 } 265 }
260 266
261 err = mmc_sd_switch(card, 1, 0, 1, status); 267 err = mmc_sd_switch(card, 1, 0, 1, status);
262 if (err != MMC_ERR_NONE) 268 if (err)
263 goto out; 269 goto out;
264 270
265 if ((status[16] & 0xF) != 1) { 271 if ((status[16] & 0xF) != 1) {
@@ -292,7 +298,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
292 unsigned int max_dtr; 298 unsigned int max_dtr;
293 299
294 BUG_ON(!host); 300 BUG_ON(!host);
295 BUG_ON(!host->claimed); 301 WARN_ON(!host->claimed);
296 302
297 /* 303 /*
298 * Since we're changing the OCR value, we seem to 304 * Since we're changing the OCR value, we seem to
@@ -309,23 +315,37 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
309 * block-addressed SDHC cards. 315 * block-addressed SDHC cards.
310 */ 316 */
311 err = mmc_send_if_cond(host, ocr); 317 err = mmc_send_if_cond(host, ocr);
312 if (err == MMC_ERR_NONE) 318 if (!err)
313 ocr |= 1 << 30; 319 ocr |= 1 << 30;
314 320
315 err = mmc_send_app_op_cond(host, ocr, NULL); 321 err = mmc_send_app_op_cond(host, ocr, NULL);
316 if (err != MMC_ERR_NONE) 322 if (err)
317 goto err; 323 goto err;
318 324
319 /* 325 /*
326 * For SPI, enable CRC as appropriate.
327 */
328 if (mmc_host_is_spi(host)) {
329 err = mmc_spi_set_crc(host, use_spi_crc);
330 if (err)
331 goto err;
332 }
333
334 /*
320 * Fetch CID from card. 335 * Fetch CID from card.
321 */ 336 */
322 err = mmc_all_send_cid(host, cid); 337 if (mmc_host_is_spi(host))
323 if (err != MMC_ERR_NONE) 338 err = mmc_send_cid(host, cid);
339 else
340 err = mmc_all_send_cid(host, cid);
341 if (err)
324 goto err; 342 goto err;
325 343
326 if (oldcard) { 344 if (oldcard) {
327 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) 345 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
346 err = -ENOENT;
328 goto err; 347 goto err;
348 }
329 349
330 card = oldcard; 350 card = oldcard;
331 } else { 351 } else {
@@ -333,32 +353,36 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
333 * Allocate card structure. 353 * Allocate card structure.
334 */ 354 */
335 card = mmc_alloc_card(host); 355 card = mmc_alloc_card(host);
336 if (IS_ERR(card)) 356 if (IS_ERR(card)) {
357 err = PTR_ERR(card);
337 goto err; 358 goto err;
359 }
338 360
339 card->type = MMC_TYPE_SD; 361 card->type = MMC_TYPE_SD;
340 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 362 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
341 } 363 }
342 364
343 /* 365 /*
344 * Set card RCA. 366 * For native busses: get card RCA and quit open drain mode.
345 */ 367 */
346 err = mmc_send_relative_addr(host, &card->rca); 368 if (!mmc_host_is_spi(host)) {
347 if (err != MMC_ERR_NONE) 369 err = mmc_send_relative_addr(host, &card->rca);
348 goto free_card; 370 if (err)
371 goto free_card;
349 372
350 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 373 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
374 }
351 375
352 if (!oldcard) { 376 if (!oldcard) {
353 /* 377 /*
354 * Fetch CSD from card. 378 * Fetch CSD from card.
355 */ 379 */
356 err = mmc_send_csd(card, card->raw_csd); 380 err = mmc_send_csd(card, card->raw_csd);
357 if (err != MMC_ERR_NONE) 381 if (err)
358 goto free_card; 382 goto free_card;
359 383
360 err = mmc_decode_csd(card); 384 err = mmc_decode_csd(card);
361 if (err < 0) 385 if (err)
362 goto free_card; 386 goto free_card;
363 387
364 mmc_decode_cid(card); 388 mmc_decode_cid(card);
@@ -367,16 +391,18 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
367 /* 391 /*
368 * Select card, as all following commands rely on that. 392 * Select card, as all following commands rely on that.
369 */ 393 */
370 err = mmc_select_card(card); 394 if (!mmc_host_is_spi(host)) {
371 if (err != MMC_ERR_NONE) 395 err = mmc_select_card(card);
372 goto free_card; 396 if (err)
397 goto free_card;
398 }
373 399
374 if (!oldcard) { 400 if (!oldcard) {
375 /* 401 /*
376 * Fetch SCR from card. 402 * Fetch SCR from card.
377 */ 403 */
378 err = mmc_app_send_scr(card, card->raw_scr); 404 err = mmc_app_send_scr(card, card->raw_scr);
379 if (err != MMC_ERR_NONE) 405 if (err)
380 goto free_card; 406 goto free_card;
381 407
382 err = mmc_decode_scr(card); 408 err = mmc_decode_scr(card);
@@ -387,7 +413,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
387 * Fetch switch information from card. 413 * Fetch switch information from card.
388 */ 414 */
389 err = mmc_read_switch(card); 415 err = mmc_read_switch(card);
390 if (err != MMC_ERR_NONE) 416 if (err)
391 goto free_card; 417 goto free_card;
392 } 418 }
393 419
@@ -395,7 +421,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
395 * Attempt to change to high-speed (if supported) 421 * Attempt to change to high-speed (if supported)
396 */ 422 */
397 err = mmc_switch_hs(card); 423 err = mmc_switch_hs(card);
398 if (err != MMC_ERR_NONE) 424 if (err)
399 goto free_card; 425 goto free_card;
400 426
401 /* 427 /*
@@ -418,7 +444,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
418 if ((host->caps & MMC_CAP_4_BIT_DATA) && 444 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
419 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 445 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
420 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 446 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
421 if (err != MMC_ERR_NONE) 447 if (err)
422 goto free_card; 448 goto free_card;
423 449
424 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 450 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
@@ -442,14 +468,14 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
442 if (!oldcard) 468 if (!oldcard)
443 host->card = card; 469 host->card = card;
444 470
445 return MMC_ERR_NONE; 471 return 0;
446 472
447free_card: 473free_card:
448 if (!oldcard) 474 if (!oldcard)
449 mmc_remove_card(card); 475 mmc_remove_card(card);
450err: 476err:
451 477
452 return MMC_ERR_FAILED; 478 return err;
453} 479}
454 480
455/* 481/*
@@ -483,7 +509,7 @@ static void mmc_sd_detect(struct mmc_host *host)
483 509
484 mmc_release_host(host); 510 mmc_release_host(host);
485 511
486 if (err != MMC_ERR_NONE) { 512 if (err) {
487 mmc_sd_remove(host); 513 mmc_sd_remove(host);
488 514
489 mmc_claim_host(host); 515 mmc_claim_host(host);
@@ -552,7 +578,8 @@ static void mmc_sd_suspend(struct mmc_host *host)
552 BUG_ON(!host->card); 578 BUG_ON(!host->card);
553 579
554 mmc_claim_host(host); 580 mmc_claim_host(host);
555 mmc_deselect_cards(host); 581 if (!mmc_host_is_spi(host))
582 mmc_deselect_cards(host);
556 host->card->state &= ~MMC_STATE_HIGHSPEED; 583 host->card->state &= ~MMC_STATE_HIGHSPEED;
557 mmc_release_host(host); 584 mmc_release_host(host);
558} 585}
@@ -574,7 +601,7 @@ static void mmc_sd_resume(struct mmc_host *host)
574 err = mmc_sd_init_card(host, host->ocr, host->card); 601 err = mmc_sd_init_card(host, host->ocr, host->card);
575 mmc_release_host(host); 602 mmc_release_host(host);
576 603
577 if (err != MMC_ERR_NONE) { 604 if (err) {
578 mmc_sd_remove(host); 605 mmc_sd_remove(host);
579 606
580 mmc_claim_host(host); 607 mmc_claim_host(host);
@@ -608,11 +635,22 @@ int mmc_attach_sd(struct mmc_host *host, u32 ocr)
608 int err; 635 int err;
609 636
610 BUG_ON(!host); 637 BUG_ON(!host);
611 BUG_ON(!host->claimed); 638 WARN_ON(!host->claimed);
612 639
613 mmc_attach_bus(host, &mmc_sd_ops); 640 mmc_attach_bus(host, &mmc_sd_ops);
614 641
615 /* 642 /*
643 * We need to get OCR a different way for SPI.
644 */
645 if (mmc_host_is_spi(host)) {
646 mmc_go_idle(host);
647
648 err = mmc_spi_read_ocr(host, 0, &ocr);
649 if (err)
650 goto err;
651 }
652
653 /*
616 * Sanity check the voltages that the card claims to 654 * Sanity check the voltages that the card claims to
617 * support. 655 * support.
618 */ 656 */
@@ -644,7 +682,7 @@ int mmc_attach_sd(struct mmc_host *host, u32 ocr)
644 * Detect and init the card. 682 * Detect and init the card.
645 */ 683 */
646 err = mmc_sd_init_card(host, host->ocr, NULL); 684 err = mmc_sd_init_card(host, host->ocr, NULL);
647 if (err != MMC_ERR_NONE) 685 if (err)
648 goto err; 686 goto err;
649 687
650 mmc_release_host(host); 688 mmc_release_host(host);
@@ -666,6 +704,6 @@ err:
666 printk(KERN_ERR "%s: error %d whilst initialising SD card\n", 704 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
667 mmc_hostname(host), err); 705 mmc_hostname(host), err);
668 706
669 return 0; 707 return err;
670} 708}
671 709
diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c
index 342f340ebc25..ee4029a24efd 100644
--- a/drivers/mmc/core/sd_ops.c
+++ b/drivers/mmc/core/sd_ops.c
@@ -33,21 +33,21 @@ static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
33 33
34 if (card) { 34 if (card) {
35 cmd.arg = card->rca << 16; 35 cmd.arg = card->rca << 16;
36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 36 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
37 } else { 37 } else {
38 cmd.arg = 0; 38 cmd.arg = 0;
39 cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR; 39 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
40 } 40 }
41 41
42 err = mmc_wait_for_cmd(host, &cmd, 0); 42 err = mmc_wait_for_cmd(host, &cmd, 0);
43 if (err != MMC_ERR_NONE) 43 if (err)
44 return err; 44 return err;
45 45
46 /* Check that card supported application commands */ 46 /* Check that card supported application commands */
47 if (!(cmd.resp[0] & R1_APP_CMD)) 47 if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
48 return MMC_ERR_FAILED; 48 return -EOPNOTSUPP;
49 49
50 return MMC_ERR_NONE; 50 return 0;
51} 51}
52 52
53/** 53/**
@@ -73,7 +73,7 @@ int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
73 BUG_ON(!cmd); 73 BUG_ON(!cmd);
74 BUG_ON(retries < 0); 74 BUG_ON(retries < 0);
75 75
76 err = MMC_ERR_INVALID; 76 err = -EIO;
77 77
78 /* 78 /*
79 * We have to resend MMC_APP_CMD for each attempt so 79 * We have to resend MMC_APP_CMD for each attempt so
@@ -83,8 +83,14 @@ int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
83 memset(&mrq, 0, sizeof(struct mmc_request)); 83 memset(&mrq, 0, sizeof(struct mmc_request));
84 84
85 err = mmc_app_cmd(host, card); 85 err = mmc_app_cmd(host, card);
86 if (err != MMC_ERR_NONE) 86 if (err) {
87 /* no point in retrying; no APP commands allowed */
88 if (mmc_host_is_spi(host)) {
89 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
90 break;
91 }
87 continue; 92 continue;
93 }
88 94
89 memset(&mrq, 0, sizeof(struct mmc_request)); 95 memset(&mrq, 0, sizeof(struct mmc_request));
90 96
@@ -97,8 +103,14 @@ int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
97 mmc_wait_for_req(host, &mrq); 103 mmc_wait_for_req(host, &mrq);
98 104
99 err = cmd->error; 105 err = cmd->error;
100 if (cmd->error == MMC_ERR_NONE) 106 if (!cmd->error)
101 break; 107 break;
108
109 /* no point in retrying illegal APP commands */
110 if (mmc_host_is_spi(host)) {
111 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
112 break;
113 }
102 } 114 }
103 115
104 return err; 116 return err;
@@ -127,14 +139,14 @@ int mmc_app_set_bus_width(struct mmc_card *card, int width)
127 cmd.arg = SD_BUS_WIDTH_4; 139 cmd.arg = SD_BUS_WIDTH_4;
128 break; 140 break;
129 default: 141 default:
130 return MMC_ERR_INVALID; 142 return -EINVAL;
131 } 143 }
132 144
133 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES); 145 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE) 146 if (err)
135 return err; 147 return err;
136 148
137 return MMC_ERR_NONE; 149 return 0;
138} 150}
139 151
140int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 152int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
@@ -147,23 +159,36 @@ int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
147 memset(&cmd, 0, sizeof(struct mmc_command)); 159 memset(&cmd, 0, sizeof(struct mmc_command));
148 160
149 cmd.opcode = SD_APP_OP_COND; 161 cmd.opcode = SD_APP_OP_COND;
150 cmd.arg = ocr; 162 if (mmc_host_is_spi(host))
151 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 163 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
164 else
165 cmd.arg = ocr;
166 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
152 167
153 for (i = 100; i; i--) { 168 for (i = 100; i; i--) {
154 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES); 169 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
155 if (err != MMC_ERR_NONE) 170 if (err)
156 break; 171 break;
157 172
158 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) 173 /* if we're just probing, do a single pass */
174 if (ocr == 0)
159 break; 175 break;
160 176
161 err = MMC_ERR_TIMEOUT; 177 /* otherwise wait until reset completes */
178 if (mmc_host_is_spi(host)) {
179 if (!(cmd.resp[0] & R1_SPI_IDLE))
180 break;
181 } else {
182 if (cmd.resp[0] & MMC_CARD_BUSY)
183 break;
184 }
185
186 err = -ETIMEDOUT;
162 187
163 mmc_delay(10); 188 mmc_delay(10);
164 } 189 }
165 190
166 if (rocr) 191 if (rocr && !mmc_host_is_spi(host))
167 *rocr = cmd.resp[0]; 192 *rocr = cmd.resp[0];
168 193
169 return err; 194 return err;
@@ -174,6 +199,7 @@ int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
174 struct mmc_command cmd; 199 struct mmc_command cmd;
175 int err; 200 int err;
176 static const u8 test_pattern = 0xAA; 201 static const u8 test_pattern = 0xAA;
202 u8 result_pattern;
177 203
178 /* 204 /*
179 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND 205 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
@@ -182,16 +208,21 @@ int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
182 */ 208 */
183 cmd.opcode = SD_SEND_IF_COND; 209 cmd.opcode = SD_SEND_IF_COND;
184 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern; 210 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
185 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 211 cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
186 212
187 err = mmc_wait_for_cmd(host, &cmd, 0); 213 err = mmc_wait_for_cmd(host, &cmd, 0);
188 if (err != MMC_ERR_NONE) 214 if (err)
189 return err; 215 return err;
190 216
191 if ((cmd.resp[0] & 0xFF) != test_pattern) 217 if (mmc_host_is_spi(host))
192 return MMC_ERR_FAILED; 218 result_pattern = cmd.resp[1] & 0xFF;
219 else
220 result_pattern = cmd.resp[0] & 0xFF;
221
222 if (result_pattern != test_pattern)
223 return -EIO;
193 224
194 return MMC_ERR_NONE; 225 return 0;
195} 226}
196 227
197int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca) 228int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
@@ -209,12 +240,12 @@ int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
209 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 240 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
210 241
211 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 242 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
212 if (err != MMC_ERR_NONE) 243 if (err)
213 return err; 244 return err;
214 245
215 *rca = cmd.resp[0] >> 16; 246 *rca = cmd.resp[0] >> 16;
216 247
217 return MMC_ERR_NONE; 248 return 0;
218} 249}
219 250
220int mmc_app_send_scr(struct mmc_card *card, u32 *scr) 251int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
@@ -229,8 +260,10 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
229 BUG_ON(!card->host); 260 BUG_ON(!card->host);
230 BUG_ON(!scr); 261 BUG_ON(!scr);
231 262
263 /* NOTE: caller guarantees scr is heap-allocated */
264
232 err = mmc_app_cmd(card->host, card); 265 err = mmc_app_cmd(card->host, card);
233 if (err != MMC_ERR_NONE) 266 if (err)
234 return err; 267 return err;
235 268
236 memset(&mrq, 0, sizeof(struct mmc_request)); 269 memset(&mrq, 0, sizeof(struct mmc_request));
@@ -242,7 +275,7 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
242 275
243 cmd.opcode = SD_APP_SEND_SCR; 276 cmd.opcode = SD_APP_SEND_SCR;
244 cmd.arg = 0; 277 cmd.arg = 0;
245 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 278 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
246 279
247 data.blksz = 8; 280 data.blksz = 8;
248 data.blocks = 1; 281 data.blocks = 1;
@@ -252,19 +285,19 @@ int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
252 285
253 sg_init_one(&sg, scr, 8); 286 sg_init_one(&sg, scr, 8);
254 287
255 mmc_set_data_timeout(&data, card, 0); 288 mmc_set_data_timeout(&data, card);
256 289
257 mmc_wait_for_req(card->host, &mrq); 290 mmc_wait_for_req(card->host, &mrq);
258 291
259 if (cmd.error != MMC_ERR_NONE) 292 if (cmd.error)
260 return cmd.error; 293 return cmd.error;
261 if (data.error != MMC_ERR_NONE) 294 if (data.error)
262 return data.error; 295 return data.error;
263 296
264 scr[0] = ntohl(scr[0]); 297 scr[0] = ntohl(scr[0]);
265 scr[1] = ntohl(scr[1]); 298 scr[1] = ntohl(scr[1]);
266 299
267 return MMC_ERR_NONE; 300 return 0;
268} 301}
269 302
270int mmc_sd_switch(struct mmc_card *card, int mode, int group, 303int mmc_sd_switch(struct mmc_card *card, int mode, int group,
@@ -278,6 +311,8 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
278 BUG_ON(!card); 311 BUG_ON(!card);
279 BUG_ON(!card->host); 312 BUG_ON(!card->host);
280 313
314 /* NOTE: caller guarantees resp is heap-allocated */
315
281 mode = !!mode; 316 mode = !!mode;
282 value &= 0xF; 317 value &= 0xF;
283 318
@@ -292,7 +327,7 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
292 cmd.arg = mode << 31 | 0x00FFFFFF; 327 cmd.arg = mode << 31 | 0x00FFFFFF;
293 cmd.arg &= ~(0xF << (group * 4)); 328 cmd.arg &= ~(0xF << (group * 4));
294 cmd.arg |= value << (group * 4); 329 cmd.arg |= value << (group * 4);
295 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 330 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
296 331
297 data.blksz = 64; 332 data.blksz = 64;
298 data.blocks = 1; 333 data.blocks = 1;
@@ -302,15 +337,15 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
302 337
303 sg_init_one(&sg, resp, 64); 338 sg_init_one(&sg, resp, 64);
304 339
305 mmc_set_data_timeout(&data, card, 0); 340 mmc_set_data_timeout(&data, card);
306 341
307 mmc_wait_for_req(card->host, &mrq); 342 mmc_wait_for_req(card->host, &mrq);
308 343
309 if (cmd.error != MMC_ERR_NONE) 344 if (cmd.error)
310 return cmd.error; 345 return cmd.error;
311 if (data.error != MMC_ERR_NONE) 346 if (data.error)
312 return data.error; 347 return data.error;
313 348
314 return MMC_ERR_NONE; 349 return 0;
315} 350}
316 351
diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c
new file mode 100644
index 000000000000..87a50f456efc
--- /dev/null
+++ b/drivers/mmc/core/sdio.c
@@ -0,0 +1,395 @@
1/*
2 * linux/drivers/mmc/sdio.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#include <linux/err.h>
13
14#include <linux/mmc/host.h>
15#include <linux/mmc/card.h>
16#include <linux/mmc/sdio.h>
17#include <linux/mmc/sdio_func.h>
18
19#include "core.h"
20#include "bus.h"
21#include "sdio_bus.h"
22#include "mmc_ops.h"
23#include "sd_ops.h"
24#include "sdio_ops.h"
25#include "sdio_cis.h"
26
27static int sdio_read_fbr(struct sdio_func *func)
28{
29 int ret;
30 unsigned char data;
31
32 ret = mmc_io_rw_direct(func->card, 0, 0,
33 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
34 if (ret)
35 goto out;
36
37 data &= 0x0f;
38
39 if (data == 0x0f) {
40 ret = mmc_io_rw_direct(func->card, 0, 0,
41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
42 if (ret)
43 goto out;
44 }
45
46 func->class = data;
47
48out:
49 return ret;
50}
51
52static int sdio_init_func(struct mmc_card *card, unsigned int fn)
53{
54 int ret;
55 struct sdio_func *func;
56
57 BUG_ON(fn > SDIO_MAX_FUNCS);
58
59 func = sdio_alloc_func(card);
60 if (IS_ERR(func))
61 return PTR_ERR(func);
62
63 func->num = fn;
64
65 ret = sdio_read_fbr(func);
66 if (ret)
67 goto fail;
68
69 ret = sdio_read_func_cis(func);
70 if (ret)
71 goto fail;
72
73 card->sdio_func[fn - 1] = func;
74
75 return 0;
76
77fail:
78 /*
79 * It is okay to remove the function here even though we hold
80 * the host lock as we haven't registered the device yet.
81 */
82 sdio_remove_func(func);
83 return ret;
84}
85
86static int sdio_read_cccr(struct mmc_card *card)
87{
88 int ret;
89 int cccr_vsn;
90 unsigned char data;
91
92 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
93
94 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
95 if (ret)
96 goto out;
97
98 cccr_vsn = data & 0x0f;
99
100 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
101 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
102 mmc_hostname(card->host), cccr_vsn);
103 return -EINVAL;
104 }
105
106 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
107
108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
109 if (ret)
110 goto out;
111
112 if (data & SDIO_CCCR_CAP_SMB)
113 card->cccr.multi_block = 1;
114 if (data & SDIO_CCCR_CAP_LSC)
115 card->cccr.low_speed = 1;
116 if (data & SDIO_CCCR_CAP_4BLS)
117 card->cccr.wide_bus = 1;
118
119 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
120 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
121 if (ret)
122 goto out;
123
124 if (data & SDIO_POWER_SMPC)
125 card->cccr.high_power = 1;
126 }
127
128 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
129 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
130 if (ret)
131 goto out;
132
133 if (data & SDIO_SPEED_SHS)
134 card->cccr.high_speed = 1;
135 }
136
137out:
138 return ret;
139}
140
141static int sdio_enable_wide(struct mmc_card *card)
142{
143 int ret;
144 u8 ctrl;
145
146 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
147 return 0;
148
149 if (card->cccr.low_speed && !card->cccr.wide_bus)
150 return 0;
151
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
153 if (ret)
154 return ret;
155
156 ctrl |= SDIO_BUS_WIDTH_4BIT;
157
158 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
159 if (ret)
160 return ret;
161
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
163
164 return 0;
165}
166
167/*
168 * Host is being removed. Free up the current card.
169 */
170static void mmc_sdio_remove(struct mmc_host *host)
171{
172 int i;
173
174 BUG_ON(!host);
175 BUG_ON(!host->card);
176
177 for (i = 0;i < host->card->sdio_funcs;i++) {
178 if (host->card->sdio_func[i]) {
179 sdio_remove_func(host->card->sdio_func[i]);
180 host->card->sdio_func[i] = NULL;
181 }
182 }
183
184 mmc_remove_card(host->card);
185 host->card = NULL;
186}
187
188/*
189 * Card detection callback from host.
190 */
191static void mmc_sdio_detect(struct mmc_host *host)
192{
193 int err;
194
195 BUG_ON(!host);
196 BUG_ON(!host->card);
197
198 mmc_claim_host(host);
199
200 /*
201 * Just check if our card has been removed.
202 */
203 err = mmc_select_card(host->card);
204
205 mmc_release_host(host);
206
207 if (err) {
208 mmc_sdio_remove(host);
209
210 mmc_claim_host(host);
211 mmc_detach_bus(host);
212 mmc_release_host(host);
213 }
214}
215
216
217static const struct mmc_bus_ops mmc_sdio_ops = {
218 .remove = mmc_sdio_remove,
219 .detect = mmc_sdio_detect,
220};
221
222
223/*
224 * Starting point for SDIO card init.
225 */
226int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
227{
228 int err;
229 int i, funcs;
230 struct mmc_card *card;
231
232 BUG_ON(!host);
233 WARN_ON(!host->claimed);
234
235 mmc_attach_bus(host, &mmc_sdio_ops);
236
237 /*
238 * Sanity check the voltages that the card claims to
239 * support.
240 */
241 if (ocr & 0x7F) {
242 printk(KERN_WARNING "%s: card claims to support voltages "
243 "below the defined range. These will be ignored.\n",
244 mmc_hostname(host));
245 ocr &= ~0x7F;
246 }
247
248 if (ocr & MMC_VDD_165_195) {
249 printk(KERN_WARNING "%s: SDIO card claims to support the "
250 "incompletely defined 'low voltage range'. This "
251 "will be ignored.\n", mmc_hostname(host));
252 ocr &= ~MMC_VDD_165_195;
253 }
254
255 host->ocr = mmc_select_voltage(host, ocr);
256
257 /*
258 * Can we support the voltage(s) of the card(s)?
259 */
260 if (!host->ocr) {
261 err = -EINVAL;
262 goto err;
263 }
264
265 /*
266 * Inform the card of the voltage
267 */
268 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
269 if (err)
270 goto err;
271
272 /*
273 * For SPI, enable CRC as appropriate.
274 */
275 if (mmc_host_is_spi(host)) {
276 err = mmc_spi_set_crc(host, use_spi_crc);
277 if (err)
278 goto err;
279 }
280
281 /*
282 * The number of functions on the card is encoded inside
283 * the ocr.
284 */
285 funcs = (ocr & 0x70000000) >> 28;
286
287 /*
288 * Allocate card structure.
289 */
290 card = mmc_alloc_card(host);
291 if (IS_ERR(card)) {
292 err = PTR_ERR(card);
293 goto err;
294 }
295
296 card->type = MMC_TYPE_SDIO;
297 card->sdio_funcs = funcs;
298
299 host->card = card;
300
301 /*
302 * For native busses: set card RCA and quit open drain mode.
303 */
304 if (!mmc_host_is_spi(host)) {
305 err = mmc_send_relative_addr(host, &card->rca);
306 if (err)
307 goto remove;
308
309 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
310 }
311
312 /*
313 * Select card, as all following commands rely on that.
314 */
315 if (!mmc_host_is_spi(host)) {
316 err = mmc_select_card(card);
317 if (err)
318 goto remove;
319 }
320
321 /*
322 * Read the common registers.
323 */
324 err = sdio_read_cccr(card);
325 if (err)
326 goto remove;
327
328 /*
329 * Read the common CIS tuples.
330 */
331 err = sdio_read_common_cis(card);
332 if (err)
333 goto remove;
334
335 /*
336 * No support for high-speed yet, so just set
337 * the card's maximum speed.
338 */
339 mmc_set_clock(host, card->cis.max_dtr);
340
341 /*
342 * Switch to wider bus (if supported).
343 */
344 err = sdio_enable_wide(card);
345 if (err)
346 goto remove;
347
348 /*
349 * Initialize (but don't add) all present functions.
350 */
351 for (i = 0;i < funcs;i++) {
352 err = sdio_init_func(host->card, i + 1);
353 if (err)
354 goto remove;
355 }
356
357 mmc_release_host(host);
358
359 /*
360 * First add the card to the driver model...
361 */
362 err = mmc_add_card(host->card);
363 if (err)
364 goto remove_added;
365
366 /*
367 * ...then the SDIO functions.
368 */
369 for (i = 0;i < funcs;i++) {
370 err = sdio_add_func(host->card->sdio_func[i]);
371 if (err)
372 goto remove_added;
373 }
374
375 return 0;
376
377
378remove_added:
379 /* Remove without lock if the device has been added. */
380 mmc_sdio_remove(host);
381 mmc_claim_host(host);
382remove:
383 /* And with lock if it hasn't been added. */
384 if (host->card)
385 mmc_sdio_remove(host);
386err:
387 mmc_detach_bus(host);
388 mmc_release_host(host);
389
390 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
391 mmc_hostname(host), err);
392
393 return err;
394}
395
diff --git a/drivers/mmc/core/sdio_bus.c b/drivers/mmc/core/sdio_bus.c
new file mode 100644
index 000000000000..0713a8c71e54
--- /dev/null
+++ b/drivers/mmc/core/sdio_bus.c
@@ -0,0 +1,270 @@
1/*
2 * linux/drivers/mmc/core/sdio_bus.c
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * SDIO function driver model
12 */
13
14#include <linux/device.h>
15#include <linux/err.h>
16
17#include <linux/mmc/card.h>
18#include <linux/mmc/sdio_func.h>
19
20#include "sdio_cis.h"
21#include "sdio_bus.h"
22
23#define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
24#define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
25
26/* show configuration fields */
27#define sdio_config_attr(field, format_string) \
28static ssize_t \
29field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
30{ \
31 struct sdio_func *func; \
32 \
33 func = dev_to_sdio_func (dev); \
34 return sprintf (buf, format_string, func->field); \
35}
36
37sdio_config_attr(class, "0x%02x\n");
38sdio_config_attr(vendor, "0x%04x\n");
39sdio_config_attr(device, "0x%04x\n");
40
41static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
42{
43 struct sdio_func *func = dev_to_sdio_func (dev);
44
45 return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
46 func->class, func->vendor, func->device);
47}
48
49static struct device_attribute sdio_dev_attrs[] = {
50 __ATTR_RO(class),
51 __ATTR_RO(vendor),
52 __ATTR_RO(device),
53 __ATTR_RO(modalias),
54 __ATTR_NULL,
55};
56
57static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
58 const struct sdio_device_id *id)
59{
60 if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
61 return NULL;
62 if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
63 return NULL;
64 if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
65 return NULL;
66 return id;
67}
68
69static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
70 struct sdio_driver *sdrv)
71{
72 const struct sdio_device_id *ids;
73
74 ids = sdrv->id_table;
75
76 if (ids) {
77 while (ids->class || ids->vendor || ids->device) {
78 if (sdio_match_one(func, ids))
79 return ids;
80 ids++;
81 }
82 }
83
84 return NULL;
85}
86
87static int sdio_bus_match(struct device *dev, struct device_driver *drv)
88{
89 struct sdio_func *func = dev_to_sdio_func(dev);
90 struct sdio_driver *sdrv = to_sdio_driver(drv);
91
92 if (sdio_match_device(func, sdrv))
93 return 1;
94
95 return 0;
96}
97
98static int
99sdio_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
100 int buf_size)
101{
102 struct sdio_func *func = dev_to_sdio_func(dev);
103 int i = 0, length = 0;
104
105 if (add_uevent_var(envp, num_envp, &i,
106 buf, buf_size, &length,
107 "SDIO_CLASS=%02X", func->class))
108 return -ENOMEM;
109
110 if (add_uevent_var(envp, num_envp, &i,
111 buf, buf_size, &length,
112 "SDIO_ID=%04X:%04X", func->vendor, func->device))
113 return -ENOMEM;
114
115 if (add_uevent_var(envp, num_envp, &i,
116 buf, buf_size, &length,
117 "MODALIAS=sdio:c%02Xv%04Xd%04X",
118 func->class, func->vendor, func->device))
119 return -ENOMEM;
120
121 envp[i] = NULL;
122
123 return 0;
124}
125
126static int sdio_bus_probe(struct device *dev)
127{
128 struct sdio_driver *drv = to_sdio_driver(dev->driver);
129 struct sdio_func *func = dev_to_sdio_func(dev);
130 const struct sdio_device_id *id;
131 int ret;
132
133 id = sdio_match_device(func, drv);
134 if (!id)
135 return -ENODEV;
136
137 /* Set the default block size so the driver is sure it's something
138 * sensible. */
139 sdio_claim_host(func);
140 ret = sdio_set_block_size(func, 0);
141 sdio_release_host(func);
142 if (ret)
143 return ret;
144
145 return drv->probe(func, id);
146}
147
148static int sdio_bus_remove(struct device *dev)
149{
150 struct sdio_driver *drv = to_sdio_driver(dev->driver);
151 struct sdio_func *func = dev_to_sdio_func(dev);
152
153 drv->remove(func);
154
155 if (func->irq_handler) {
156 printk(KERN_WARNING "WARNING: driver %s did not remove "
157 "its interrupt handler!\n", drv->name);
158 sdio_claim_host(func);
159 sdio_release_irq(func);
160 sdio_release_host(func);
161 }
162
163 return 0;
164}
165
166static struct bus_type sdio_bus_type = {
167 .name = "sdio",
168 .dev_attrs = sdio_dev_attrs,
169 .match = sdio_bus_match,
170 .uevent = sdio_bus_uevent,
171 .probe = sdio_bus_probe,
172 .remove = sdio_bus_remove,
173};
174
175int sdio_register_bus(void)
176{
177 return bus_register(&sdio_bus_type);
178}
179
180void sdio_unregister_bus(void)
181{
182 bus_unregister(&sdio_bus_type);
183}
184
185/**
186 * sdio_register_driver - register a function driver
187 * @drv: SDIO function driver
188 */
189int sdio_register_driver(struct sdio_driver *drv)
190{
191 drv->drv.name = drv->name;
192 drv->drv.bus = &sdio_bus_type;
193 return driver_register(&drv->drv);
194}
195EXPORT_SYMBOL_GPL(sdio_register_driver);
196
197/**
198 * sdio_unregister_driver - unregister a function driver
199 * @drv: SDIO function driver
200 */
201void sdio_unregister_driver(struct sdio_driver *drv)
202{
203 drv->drv.bus = &sdio_bus_type;
204 driver_unregister(&drv->drv);
205}
206EXPORT_SYMBOL_GPL(sdio_unregister_driver);
207
208static void sdio_release_func(struct device *dev)
209{
210 struct sdio_func *func = dev_to_sdio_func(dev);
211
212 sdio_free_func_cis(func);
213
214 if (func->info)
215 kfree(func->info);
216
217 kfree(func);
218}
219
220/*
221 * Allocate and initialise a new SDIO function structure.
222 */
223struct sdio_func *sdio_alloc_func(struct mmc_card *card)
224{
225 struct sdio_func *func;
226
227 func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
228 if (!func)
229 return ERR_PTR(-ENOMEM);
230
231 func->card = card;
232
233 device_initialize(&func->dev);
234
235 func->dev.parent = &card->dev;
236 func->dev.bus = &sdio_bus_type;
237 func->dev.release = sdio_release_func;
238
239 return func;
240}
241
242/*
243 * Register a new SDIO function with the driver model.
244 */
245int sdio_add_func(struct sdio_func *func)
246{
247 int ret;
248
249 snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
250 "%s:%d", mmc_card_id(func->card), func->num);
251
252 ret = device_add(&func->dev);
253 if (ret == 0)
254 sdio_func_set_present(func);
255
256 return ret;
257}
258
259/*
260 * Unregister a SDIO function with the driver model, and
261 * (eventually) free it.
262 */
263void sdio_remove_func(struct sdio_func *func)
264{
265 if (sdio_func_present(func))
266 device_del(&func->dev);
267
268 put_device(&func->dev);
269}
270
diff --git a/drivers/mmc/core/sdio_bus.h b/drivers/mmc/core/sdio_bus.h
new file mode 100644
index 000000000000..567a76821ba7
--- /dev/null
+++ b/drivers/mmc/core/sdio_bus.h
@@ -0,0 +1,22 @@
1/*
2 * linux/drivers/mmc/core/sdio_bus.h
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11#ifndef _MMC_CORE_SDIO_BUS_H
12#define _MMC_CORE_SDIO_BUS_H
13
14struct sdio_func *sdio_alloc_func(struct mmc_card *card);
15int sdio_add_func(struct sdio_func *func);
16void sdio_remove_func(struct sdio_func *func);
17
18int sdio_register_bus(void);
19void sdio_unregister_bus(void);
20
21#endif
22
diff --git a/drivers/mmc/core/sdio_cis.c b/drivers/mmc/core/sdio_cis.c
new file mode 100644
index 000000000000..d5e51b1c7b3f
--- /dev/null
+++ b/drivers/mmc/core/sdio_cis.c
@@ -0,0 +1,346 @@
1/*
2 * linux/drivers/mmc/core/sdio_cis.c
3 *
4 * Author: Nicolas Pitre
5 * Created: June 11, 2007
6 * Copyright: MontaVista Software Inc.
7 *
8 * Copyright 2007 Pierre Ossman
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 */
15
16#include <linux/kernel.h>
17
18#include <linux/mmc/host.h>
19#include <linux/mmc/card.h>
20#include <linux/mmc/sdio.h>
21#include <linux/mmc/sdio_func.h>
22
23#include "sdio_cis.h"
24#include "sdio_ops.h"
25
26static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
27 const unsigned char *buf, unsigned size)
28{
29 unsigned i, nr_strings;
30 char **buffer, *string;
31
32 buf += 2;
33 size -= 2;
34
35 nr_strings = 0;
36 for (i = 0; i < size; i++) {
37 if (buf[i] == 0xff)
38 break;
39 if (buf[i] == 0)
40 nr_strings++;
41 }
42
43 if (buf[i-1] != '\0') {
44 printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
45 return 0;
46 }
47
48 size = i;
49
50 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
51 if (!buffer)
52 return -ENOMEM;
53
54 string = (char*)(buffer + nr_strings);
55
56 for (i = 0; i < nr_strings; i++) {
57 buffer[i] = string;
58 strcpy(string, buf);
59 string += strlen(string) + 1;
60 buf += strlen(buf) + 1;
61 }
62
63 if (func) {
64 func->num_info = nr_strings;
65 func->info = (const char**)buffer;
66 } else {
67 card->num_info = nr_strings;
68 card->info = (const char**)buffer;
69 }
70
71 return 0;
72}
73
74static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
75 const unsigned char *buf, unsigned size)
76{
77 unsigned int vendor, device;
78
79 /* TPLMID_MANF */
80 vendor = buf[0] | (buf[1] << 8);
81
82 /* TPLMID_CARD */
83 device = buf[2] | (buf[3] << 8);
84
85 if (func) {
86 func->vendor = vendor;
87 func->device = device;
88 } else {
89 card->cis.vendor = vendor;
90 card->cis.device = device;
91 }
92
93 return 0;
94}
95
96static const unsigned char speed_val[16] =
97 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
98static const unsigned int speed_unit[8] =
99 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
100
101static int cistpl_funce_common(struct mmc_card *card,
102 const unsigned char *buf, unsigned size)
103{
104 if (size < 0x04 || buf[0] != 0)
105 return -EINVAL;
106
107 /* TPLFE_FN0_BLK_SIZE */
108 card->cis.blksize = buf[1] | (buf[2] << 8);
109
110 /* TPLFE_MAX_TRAN_SPEED */
111 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
112 speed_unit[buf[3] & 7];
113
114 return 0;
115}
116
117static int cistpl_funce_func(struct sdio_func *func,
118 const unsigned char *buf, unsigned size)
119{
120 unsigned vsn;
121 unsigned min_size;
122
123 vsn = func->card->cccr.sdio_vsn;
124 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
125
126 if (size < min_size || buf[0] != 1)
127 return -EINVAL;
128
129 /* TPLFE_MAX_BLK_SIZE */
130 func->max_blksize = buf[12] | (buf[13] << 8);
131
132 return 0;
133}
134
135static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
136 const unsigned char *buf, unsigned size)
137{
138 int ret;
139
140 /*
141 * There should be two versions of the CISTPL_FUNCE tuple,
142 * one for the common CIS (function 0) and a version used by
143 * the individual function's CIS (1-7). Yet, the later has a
144 * different length depending on the SDIO spec version.
145 */
146 if (func)
147 ret = cistpl_funce_func(func, buf, size);
148 else
149 ret = cistpl_funce_common(card, buf, size);
150
151 if (ret) {
152 printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
153 "type %u\n", mmc_hostname(card->host), size, buf[0]);
154 return ret;
155 }
156
157 return 0;
158}
159
160typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
161 const unsigned char *, unsigned);
162
163struct cis_tpl {
164 unsigned char code;
165 unsigned char min_size;
166 tpl_parse_t *parse;
167};
168
169static const struct cis_tpl cis_tpl_list[] = {
170 { 0x15, 3, cistpl_vers_1 },
171 { 0x20, 4, cistpl_manfid },
172 { 0x21, 2, /* cistpl_funcid */ },
173 { 0x22, 0, cistpl_funce },
174};
175
176static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
177{
178 int ret;
179 struct sdio_func_tuple *this, **prev;
180 unsigned i, ptr = 0;
181
182 /*
183 * Note that this works for the common CIS (function number 0) as
184 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
185 * have the same offset.
186 */
187 for (i = 0; i < 3; i++) {
188 unsigned char x, fn;
189
190 if (func)
191 fn = func->num;
192 else
193 fn = 0;
194
195 ret = mmc_io_rw_direct(card, 0, 0,
196 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
197 if (ret)
198 return ret;
199 ptr |= x << (i * 8);
200 }
201
202 if (func)
203 prev = &func->tuples;
204 else
205 prev = &card->tuples;
206
207 BUG_ON(*prev);
208
209 do {
210 unsigned char tpl_code, tpl_link;
211
212 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
213 if (ret)
214 break;
215
216 /* 0xff means we're done */
217 if (tpl_code == 0xff)
218 break;
219
220 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
221 if (ret)
222 break;
223
224 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
225 if (!this)
226 return -ENOMEM;
227
228 for (i = 0; i < tpl_link; i++) {
229 ret = mmc_io_rw_direct(card, 0, 0,
230 ptr + i, 0, &this->data[i]);
231 if (ret)
232 break;
233 }
234 if (ret) {
235 kfree(this);
236 break;
237 }
238
239 for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
240 if (cis_tpl_list[i].code == tpl_code)
241 break;
242 if (i >= ARRAY_SIZE(cis_tpl_list)) {
243 /* this tuple is unknown to the core */
244 this->next = NULL;
245 this->code = tpl_code;
246 this->size = tpl_link;
247 *prev = this;
248 prev = &this->next;
249 printk(KERN_DEBUG
250 "%s: queuing CIS tuple 0x%02x length %u\n",
251 mmc_hostname(card->host), tpl_code, tpl_link);
252 } else {
253 const struct cis_tpl *tpl = cis_tpl_list + i;
254 if (tpl_link < tpl->min_size) {
255 printk(KERN_ERR
256 "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
257 mmc_hostname(card->host),
258 tpl_code, tpl_link, tpl->min_size);
259 ret = -EINVAL;
260 } else if (tpl->parse) {
261 ret = tpl->parse(card, func,
262 this->data, tpl_link);
263 }
264 kfree(this);
265 }
266
267 ptr += tpl_link;
268 } while (!ret);
269
270 /*
271 * Link in all unknown tuples found in the common CIS so that
272 * drivers don't have to go digging in two places.
273 */
274 if (func)
275 *prev = card->tuples;
276
277 return ret;
278}
279
280int sdio_read_common_cis(struct mmc_card *card)
281{
282 return sdio_read_cis(card, NULL);
283}
284
285void sdio_free_common_cis(struct mmc_card *card)
286{
287 struct sdio_func_tuple *tuple, *victim;
288
289 tuple = card->tuples;
290
291 while (tuple) {
292 victim = tuple;
293 tuple = tuple->next;
294 kfree(victim);
295 }
296
297 card->tuples = NULL;
298}
299
300int sdio_read_func_cis(struct sdio_func *func)
301{
302 int ret;
303
304 ret = sdio_read_cis(func->card, func);
305 if (ret)
306 return ret;
307
308 /*
309 * Since we've linked to tuples in the card structure,
310 * we must make sure we have a reference to it.
311 */
312 get_device(&func->card->dev);
313
314 /*
315 * Vendor/device id is optional for function CIS, so
316 * copy it from the card structure as needed.
317 */
318 if (func->vendor == 0) {
319 func->vendor = func->card->cis.vendor;
320 func->device = func->card->cis.device;
321 }
322
323 return 0;
324}
325
326void sdio_free_func_cis(struct sdio_func *func)
327{
328 struct sdio_func_tuple *tuple, *victim;
329
330 tuple = func->tuples;
331
332 while (tuple && tuple != func->card->tuples) {
333 victim = tuple;
334 tuple = tuple->next;
335 kfree(victim);
336 }
337
338 func->tuples = NULL;
339
340 /*
341 * We have now removed the link to the tuples in the
342 * card structure, so remove the reference.
343 */
344 put_device(&func->card->dev);
345}
346
diff --git a/drivers/mmc/core/sdio_cis.h b/drivers/mmc/core/sdio_cis.h
new file mode 100644
index 000000000000..4d903c2e425e
--- /dev/null
+++ b/drivers/mmc/core/sdio_cis.h
@@ -0,0 +1,23 @@
1/*
2 * linux/drivers/mmc/core/sdio_cis.h
3 *
4 * Author: Nicolas Pitre
5 * Created: June 11, 2007
6 * Copyright: MontaVista Software Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 */
13
14#ifndef _MMC_SDIO_CIS_H
15#define _MMC_SDIO_CIS_H
16
17int sdio_read_common_cis(struct mmc_card *card);
18void sdio_free_common_cis(struct mmc_card *card);
19
20int sdio_read_func_cis(struct sdio_func *func);
21void sdio_free_func_cis(struct sdio_func *func);
22
23#endif
diff --git a/drivers/mmc/core/sdio_io.c b/drivers/mmc/core/sdio_io.c
new file mode 100644
index 000000000000..625b92ce9cef
--- /dev/null
+++ b/drivers/mmc/core/sdio_io.c
@@ -0,0 +1,548 @@
1/*
2 * linux/drivers/mmc/core/sdio_io.c
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#include <linux/mmc/host.h>
13#include <linux/mmc/card.h>
14#include <linux/mmc/sdio.h>
15#include <linux/mmc/sdio_func.h>
16
17#include "sdio_ops.h"
18
19/**
20 * sdio_claim_host - exclusively claim a bus for a certain SDIO function
21 * @func: SDIO function that will be accessed
22 *
23 * Claim a bus for a set of operations. The SDIO function given
24 * is used to figure out which bus is relevant.
25 */
26void sdio_claim_host(struct sdio_func *func)
27{
28 BUG_ON(!func);
29 BUG_ON(!func->card);
30
31 mmc_claim_host(func->card->host);
32}
33EXPORT_SYMBOL_GPL(sdio_claim_host);
34
35/**
36 * sdio_release_host - release a bus for a certain SDIO function
37 * @func: SDIO function that was accessed
38 *
39 * Release a bus, allowing others to claim the bus for their
40 * operations.
41 */
42void sdio_release_host(struct sdio_func *func)
43{
44 BUG_ON(!func);
45 BUG_ON(!func->card);
46
47 mmc_release_host(func->card->host);
48}
49EXPORT_SYMBOL_GPL(sdio_release_host);
50
51/**
52 * sdio_enable_func - enables a SDIO function for usage
53 * @func: SDIO function to enable
54 *
55 * Powers up and activates a SDIO function so that register
56 * access is possible.
57 */
58int sdio_enable_func(struct sdio_func *func)
59{
60 int ret;
61 unsigned char reg;
62 unsigned long timeout;
63
64 BUG_ON(!func);
65 BUG_ON(!func->card);
66
67 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
68
69 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
70 if (ret)
71 goto err;
72
73 reg |= 1 << func->num;
74
75 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
76 if (ret)
77 goto err;
78
79 /*
80 * FIXME: This should timeout based on information in the CIS,
81 * but we don't have card to parse that yet.
82 */
83 timeout = jiffies + HZ;
84
85 while (1) {
86 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
87 if (ret)
88 goto err;
89 if (reg & (1 << func->num))
90 break;
91 ret = -ETIME;
92 if (time_after(jiffies, timeout))
93 goto err;
94 }
95
96 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
97
98 return 0;
99
100err:
101 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
102 return ret;
103}
104EXPORT_SYMBOL_GPL(sdio_enable_func);
105
106/**
107 * sdio_disable_func - disable a SDIO function
108 * @func: SDIO function to disable
109 *
110 * Powers down and deactivates a SDIO function. Register access
111 * to this function will fail until the function is reenabled.
112 */
113int sdio_disable_func(struct sdio_func *func)
114{
115 int ret;
116 unsigned char reg;
117
118 BUG_ON(!func);
119 BUG_ON(!func->card);
120
121 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
122
123 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
124 if (ret)
125 goto err;
126
127 reg &= ~(1 << func->num);
128
129 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
130 if (ret)
131 goto err;
132
133 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
134
135 return 0;
136
137err:
138 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
139 return -EIO;
140}
141EXPORT_SYMBOL_GPL(sdio_disable_func);
142
143/**
144 * sdio_set_block_size - set the block size of an SDIO function
145 * @func: SDIO function to change
146 * @blksz: new block size or 0 to use the default.
147 *
148 * The default block size is the largest supported by both the function
149 * and the host, with a maximum of 512 to ensure that arbitrarily sized
150 * data transfer use the optimal (least) number of commands.
151 *
152 * A driver may call this to override the default block size set by the
153 * core. This can be used to set a block size greater than the maximum
154 * that reported by the card; it is the driver's responsibility to ensure
155 * it uses a value that the card supports.
156 *
157 * Returns 0 on success, -EINVAL if the host does not support the
158 * requested block size, or -EIO (etc.) if one of the resultant FBR block
159 * size register writes failed.
160 *
161 */
162int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
163{
164 int ret;
165
166 if (blksz > func->card->host->max_blk_size)
167 return -EINVAL;
168
169 if (blksz == 0) {
170 blksz = min(min(
171 func->max_blksize,
172 func->card->host->max_blk_size),
173 512u);
174 }
175
176 ret = mmc_io_rw_direct(func->card, 1, 0,
177 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
178 blksz & 0xff, NULL);
179 if (ret)
180 return ret;
181 ret = mmc_io_rw_direct(func->card, 1, 0,
182 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
183 (blksz >> 8) & 0xff, NULL);
184 if (ret)
185 return ret;
186 func->cur_blksize = blksz;
187 return 0;
188}
189
190EXPORT_SYMBOL_GPL(sdio_set_block_size);
191
192/* Split an arbitrarily sized data transfer into several
193 * IO_RW_EXTENDED commands. */
194static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
195 unsigned addr, int incr_addr, u8 *buf, unsigned size)
196{
197 unsigned remainder = size;
198 unsigned max_blocks;
199 int ret;
200
201 /* Do the bulk of the transfer using block mode (if supported). */
202 if (func->card->cccr.multi_block) {
203 /* Blocks per command is limited by host count, host transfer
204 * size (we only use a single sg entry) and the maximum for
205 * IO_RW_EXTENDED of 511 blocks. */
206 max_blocks = min(min(
207 func->card->host->max_blk_count,
208 func->card->host->max_seg_size / func->cur_blksize),
209 511u);
210
211 while (remainder > func->cur_blksize) {
212 unsigned blocks;
213
214 blocks = remainder / func->cur_blksize;
215 if (blocks > max_blocks)
216 blocks = max_blocks;
217 size = blocks * func->cur_blksize;
218
219 ret = mmc_io_rw_extended(func->card, write,
220 func->num, addr, incr_addr, buf,
221 blocks, func->cur_blksize);
222 if (ret)
223 return ret;
224
225 remainder -= size;
226 buf += size;
227 if (incr_addr)
228 addr += size;
229 }
230 }
231
232 /* Write the remainder using byte mode. */
233 while (remainder > 0) {
234 size = remainder;
235 if (size > func->cur_blksize)
236 size = func->cur_blksize;
237 if (size > 512)
238 size = 512; /* maximum size for byte mode */
239
240 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
241 incr_addr, buf, 1, size);
242 if (ret)
243 return ret;
244
245 remainder -= size;
246 buf += size;
247 if (incr_addr)
248 addr += size;
249 }
250 return 0;
251}
252
253/**
254 * sdio_readb - read a single byte from a SDIO function
255 * @func: SDIO function to access
256 * @addr: address to read
257 * @err_ret: optional status value from transfer
258 *
259 * Reads a single byte from the address space of a given SDIO
260 * function. If there is a problem reading the address, 0xff
261 * is returned and @err_ret will contain the error code.
262 */
263unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
264 int *err_ret)
265{
266 int ret;
267 unsigned char val;
268
269 BUG_ON(!func);
270
271 if (err_ret)
272 *err_ret = 0;
273
274 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
275 if (ret) {
276 if (err_ret)
277 *err_ret = ret;
278 return 0xFF;
279 }
280
281 return val;
282}
283EXPORT_SYMBOL_GPL(sdio_readb);
284
285/**
286 * sdio_writeb - write a single byte to a SDIO function
287 * @func: SDIO function to access
288 * @b: byte to write
289 * @addr: address to write to
290 * @err_ret: optional status value from transfer
291 *
292 * Writes a single byte to the address space of a given SDIO
293 * function. @err_ret will contain the status of the actual
294 * transfer.
295 */
296void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
297 int *err_ret)
298{
299 int ret;
300
301 BUG_ON(!func);
302
303 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
304 if (err_ret)
305 *err_ret = ret;
306}
307EXPORT_SYMBOL_GPL(sdio_writeb);
308
309/**
310 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
311 * @func: SDIO function to access
312 * @dst: buffer to store the data
313 * @addr: address to begin reading from
314 * @count: number of bytes to read
315 *
316 * Reads from the address space of a given SDIO function. Return
317 * value indicates if the transfer succeeded or not.
318 */
319int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
320 unsigned int addr, int count)
321{
322 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
323}
324EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
325
326/**
327 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
328 * @func: SDIO function to access
329 * @addr: address to start writing to
330 * @src: buffer that contains the data to write
331 * @count: number of bytes to write
332 *
333 * Writes to the address space of a given SDIO function. Return
334 * value indicates if the transfer succeeded or not.
335 */
336int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
337 void *src, int count)
338{
339 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
340}
341EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
342
343/**
344 * sdio_readsb - read from a FIFO on a SDIO function
345 * @func: SDIO function to access
346 * @dst: buffer to store the data
347 * @addr: address of (single byte) FIFO
348 * @count: number of bytes to read
349 *
350 * Reads from the specified FIFO of a given SDIO function. Return
351 * value indicates if the transfer succeeded or not.
352 */
353int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
354 int count)
355{
356 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
357}
358
359EXPORT_SYMBOL_GPL(sdio_readsb);
360
361/**
362 * sdio_writesb - write to a FIFO of a SDIO function
363 * @func: SDIO function to access
364 * @addr: address of (single byte) FIFO
365 * @src: buffer that contains the data to write
366 * @count: number of bytes to write
367 *
368 * Writes to the specified FIFO of a given SDIO function. Return
369 * value indicates if the transfer succeeded or not.
370 */
371int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
372 int count)
373{
374 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
375}
376EXPORT_SYMBOL_GPL(sdio_writesb);
377
378/**
379 * sdio_readw - read a 16 bit integer from a SDIO function
380 * @func: SDIO function to access
381 * @addr: address to read
382 * @err_ret: optional status value from transfer
383 *
384 * Reads a 16 bit integer from the address space of a given SDIO
385 * function. If there is a problem reading the address, 0xffff
386 * is returned and @err_ret will contain the error code.
387 */
388unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
389 int *err_ret)
390{
391 int ret;
392
393 if (err_ret)
394 *err_ret = 0;
395
396 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
397 if (ret) {
398 if (err_ret)
399 *err_ret = ret;
400 return 0xFFFF;
401 }
402
403 return le16_to_cpu(*(u16*)func->tmpbuf);
404}
405EXPORT_SYMBOL_GPL(sdio_readw);
406
407/**
408 * sdio_writew - write a 16 bit integer to a SDIO function
409 * @func: SDIO function to access
410 * @b: integer to write
411 * @addr: address to write to
412 * @err_ret: optional status value from transfer
413 *
414 * Writes a 16 bit integer to the address space of a given SDIO
415 * function. @err_ret will contain the status of the actual
416 * transfer.
417 */
418void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
419 int *err_ret)
420{
421 int ret;
422
423 *(u16*)func->tmpbuf = cpu_to_le16(b);
424
425 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
426 if (err_ret)
427 *err_ret = ret;
428}
429EXPORT_SYMBOL_GPL(sdio_writew);
430
431/**
432 * sdio_readl - read a 32 bit integer from a SDIO function
433 * @func: SDIO function to access
434 * @addr: address to read
435 * @err_ret: optional status value from transfer
436 *
437 * Reads a 32 bit integer from the address space of a given SDIO
438 * function. If there is a problem reading the address,
439 * 0xffffffff is returned and @err_ret will contain the error
440 * code.
441 */
442unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
443 int *err_ret)
444{
445 int ret;
446
447 if (err_ret)
448 *err_ret = 0;
449
450 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
451 if (ret) {
452 if (err_ret)
453 *err_ret = ret;
454 return 0xFFFFFFFF;
455 }
456
457 return le32_to_cpu(*(u32*)func->tmpbuf);
458}
459EXPORT_SYMBOL_GPL(sdio_readl);
460
461/**
462 * sdio_writel - write a 32 bit integer to a SDIO function
463 * @func: SDIO function to access
464 * @b: integer to write
465 * @addr: address to write to
466 * @err_ret: optional status value from transfer
467 *
468 * Writes a 32 bit integer to the address space of a given SDIO
469 * function. @err_ret will contain the status of the actual
470 * transfer.
471 */
472void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
473 int *err_ret)
474{
475 int ret;
476
477 *(u32*)func->tmpbuf = cpu_to_le32(b);
478
479 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
480 if (err_ret)
481 *err_ret = ret;
482}
483EXPORT_SYMBOL_GPL(sdio_writel);
484
485/**
486 * sdio_f0_readb - read a single byte from SDIO function 0
487 * @func: an SDIO function of the card
488 * @addr: address to read
489 * @err_ret: optional status value from transfer
490 *
491 * Reads a single byte from the address space of SDIO function 0.
492 * If there is a problem reading the address, 0xff is returned
493 * and @err_ret will contain the error code.
494 */
495unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
496 int *err_ret)
497{
498 int ret;
499 unsigned char val;
500
501 BUG_ON(!func);
502
503 if (err_ret)
504 *err_ret = 0;
505
506 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
507 if (ret) {
508 if (err_ret)
509 *err_ret = ret;
510 return 0xFF;
511 }
512
513 return val;
514}
515EXPORT_SYMBOL_GPL(sdio_f0_readb);
516
517/**
518 * sdio_f0_writeb - write a single byte to SDIO function 0
519 * @func: an SDIO function of the card
520 * @b: byte to write
521 * @addr: address to write to
522 * @err_ret: optional status value from transfer
523 *
524 * Writes a single byte to the address space of SDIO function 0.
525 * @err_ret will contain the status of the actual transfer.
526 *
527 * Only writes to the vendor specific CCCR registers (0xF0 -
528 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
529 * writes outside this range.
530 */
531void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
532 int *err_ret)
533{
534 int ret;
535
536 BUG_ON(!func);
537
538 if (addr < 0xF0 || addr > 0xFF) {
539 if (err_ret)
540 *err_ret = -EINVAL;
541 return;
542 }
543
544 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
545 if (err_ret)
546 *err_ret = ret;
547}
548EXPORT_SYMBOL_GPL(sdio_f0_writeb);
diff --git a/drivers/mmc/core/sdio_irq.c b/drivers/mmc/core/sdio_irq.c
new file mode 100644
index 000000000000..3bd3021f5e80
--- /dev/null
+++ b/drivers/mmc/core/sdio_irq.c
@@ -0,0 +1,267 @@
1/*
2 * linux/drivers/mmc/core/sdio_irq.c
3 *
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/kthread.h>
17#include <linux/wait.h>
18#include <linux/delay.h>
19
20#include <linux/mmc/core.h>
21#include <linux/mmc/host.h>
22#include <linux/mmc/card.h>
23#include <linux/mmc/sdio.h>
24#include <linux/mmc/sdio_func.h>
25
26#include "sdio_ops.h"
27
28static int process_sdio_pending_irqs(struct mmc_card *card)
29{
30 int i, ret, count;
31 unsigned char pending;
32
33 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
34 if (ret) {
35 printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
36 mmc_card_id(card), ret);
37 return ret;
38 }
39
40 count = 0;
41 for (i = 1; i <= 7; i++) {
42 if (pending & (1 << i)) {
43 struct sdio_func *func = card->sdio_func[i - 1];
44 if (!func) {
45 printk(KERN_WARNING "%s: pending IRQ for "
46 "non-existant function\n",
47 mmc_card_id(card));
48 ret = -EINVAL;
49 } else if (func->irq_handler) {
50 func->irq_handler(func);
51 count++;
52 } else {
53 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
54 sdio_func_id(func));
55 ret = -EINVAL;
56 }
57 }
58 }
59
60 if (count)
61 return count;
62
63 return ret;
64}
65
66static int sdio_irq_thread(void *_host)
67{
68 struct mmc_host *host = _host;
69 struct sched_param param = { .sched_priority = 1 };
70 unsigned long period, idle_period;
71 int ret;
72
73 sched_setscheduler(current, SCHED_FIFO, &param);
74
75 /*
76 * We want to allow for SDIO cards to work even on non SDIO
77 * aware hosts. One thing that non SDIO host cannot do is
78 * asynchronous notification of pending SDIO card interrupts
79 * hence we poll for them in that case.
80 */
81 idle_period = msecs_to_jiffies(10);
82 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
83 MAX_SCHEDULE_TIMEOUT : idle_period;
84
85 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
86 mmc_hostname(host), period);
87
88 do {
89 /*
90 * We claim the host here on drivers behalf for a couple
91 * reasons:
92 *
93 * 1) it is already needed to retrieve the CCCR_INTx;
94 * 2) we want the driver(s) to clear the IRQ condition ASAP;
95 * 3) we need to control the abort condition locally.
96 *
97 * Just like traditional hard IRQ handlers, we expect SDIO
98 * IRQ handlers to be quick and to the point, so that the
99 * holding of the host lock does not cover too much work
100 * that doesn't require that lock to be held.
101 */
102 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
103 if (ret)
104 break;
105 ret = process_sdio_pending_irqs(host->card);
106 mmc_release_host(host);
107
108 /*
109 * Give other threads a chance to run in the presence of
110 * errors. FIXME: determine if due to card removal and
111 * possibly exit this thread if so.
112 */
113 if (ret < 0)
114 ssleep(1);
115
116 /*
117 * Adaptive polling frequency based on the assumption
118 * that an interrupt will be closely followed by more.
119 * This has a substantial benefit for network devices.
120 */
121 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
122 if (ret > 0)
123 period /= 2;
124 else {
125 period++;
126 if (period > idle_period)
127 period = idle_period;
128 }
129 }
130
131 set_task_state(current, TASK_INTERRUPTIBLE);
132 if (host->caps & MMC_CAP_SDIO_IRQ)
133 host->ops->enable_sdio_irq(host, 1);
134 if (!kthread_should_stop())
135 schedule_timeout(period);
136 set_task_state(current, TASK_RUNNING);
137 } while (!kthread_should_stop());
138
139 if (host->caps & MMC_CAP_SDIO_IRQ)
140 host->ops->enable_sdio_irq(host, 0);
141
142 pr_debug("%s: IRQ thread exiting with code %d\n",
143 mmc_hostname(host), ret);
144
145 return ret;
146}
147
148static int sdio_card_irq_get(struct mmc_card *card)
149{
150 struct mmc_host *host = card->host;
151
152 WARN_ON(!host->claimed);
153
154 if (!host->sdio_irqs++) {
155 atomic_set(&host->sdio_irq_thread_abort, 0);
156 host->sdio_irq_thread =
157 kthread_run(sdio_irq_thread, host, "ksdiorqd");
158 if (IS_ERR(host->sdio_irq_thread)) {
159 int err = PTR_ERR(host->sdio_irq_thread);
160 host->sdio_irqs--;
161 return err;
162 }
163 }
164
165 return 0;
166}
167
168static int sdio_card_irq_put(struct mmc_card *card)
169{
170 struct mmc_host *host = card->host;
171
172 WARN_ON(!host->claimed);
173 BUG_ON(host->sdio_irqs < 1);
174
175 if (!--host->sdio_irqs) {
176 atomic_set(&host->sdio_irq_thread_abort, 1);
177 kthread_stop(host->sdio_irq_thread);
178 }
179
180 return 0;
181}
182
183/**
184 * sdio_claim_irq - claim the IRQ for a SDIO function
185 * @func: SDIO function
186 * @handler: IRQ handler callback
187 *
188 * Claim and activate the IRQ for the given SDIO function. The provided
189 * handler will be called when that IRQ is asserted. The host is always
190 * claimed already when the handler is called so the handler must not
191 * call sdio_claim_host() nor sdio_release_host().
192 */
193int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
194{
195 int ret;
196 unsigned char reg;
197
198 BUG_ON(!func);
199 BUG_ON(!func->card);
200
201 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
202
203 if (func->irq_handler) {
204 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
205 return -EBUSY;
206 }
207
208 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
209 if (ret)
210 return ret;
211
212 reg |= 1 << func->num;
213
214 reg |= 1; /* Master interrupt enable */
215
216 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
217 if (ret)
218 return ret;
219
220 func->irq_handler = handler;
221 ret = sdio_card_irq_get(func->card);
222 if (ret)
223 func->irq_handler = NULL;
224
225 return ret;
226}
227EXPORT_SYMBOL_GPL(sdio_claim_irq);
228
229/**
230 * sdio_release_irq - release the IRQ for a SDIO function
231 * @func: SDIO function
232 *
233 * Disable and release the IRQ for the given SDIO function.
234 */
235int sdio_release_irq(struct sdio_func *func)
236{
237 int ret;
238 unsigned char reg;
239
240 BUG_ON(!func);
241 BUG_ON(!func->card);
242
243 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
244
245 if (func->irq_handler) {
246 func->irq_handler = NULL;
247 sdio_card_irq_put(func->card);
248 }
249
250 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
251 if (ret)
252 return ret;
253
254 reg &= ~(1 << func->num);
255
256 /* Disable master interrupt with the last function interrupt */
257 if (!(reg & 0xFE))
258 reg = 0;
259
260 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
261 if (ret)
262 return ret;
263
264 return 0;
265}
266EXPORT_SYMBOL_GPL(sdio_release_irq);
267
diff --git a/drivers/mmc/core/sdio_ops.c b/drivers/mmc/core/sdio_ops.c
new file mode 100644
index 000000000000..4d289b275031
--- /dev/null
+++ b/drivers/mmc/core/sdio_ops.c
@@ -0,0 +1,176 @@
1/*
2 * linux/drivers/mmc/sdio_ops.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#include <asm/scatterlist.h>
13#include <linux/scatterlist.h>
14
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
17#include <linux/mmc/mmc.h>
18#include <linux/mmc/sdio.h>
19
20#include "core.h"
21
22int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
23{
24 struct mmc_command cmd;
25 int i, err = 0;
26
27 BUG_ON(!host);
28
29 memset(&cmd, 0, sizeof(struct mmc_command));
30
31 cmd.opcode = SD_IO_SEND_OP_COND;
32 cmd.arg = ocr;
33 cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
34
35 for (i = 100; i; i--) {
36 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
37 if (err)
38 break;
39
40 /* if we're just probing, do a single pass */
41 if (ocr == 0)
42 break;
43
44 /* otherwise wait until reset completes */
45 if (mmc_host_is_spi(host)) {
46 /*
47 * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
48 * an initialized card under SPI, but some cards
49 * (Marvell's) only behave when looking at this
50 * one.
51 */
52 if (cmd.resp[1] & MMC_CARD_BUSY)
53 break;
54 } else {
55 if (cmd.resp[0] & MMC_CARD_BUSY)
56 break;
57 }
58
59 err = -ETIMEDOUT;
60
61 mmc_delay(10);
62 }
63
64 if (rocr)
65 *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
66
67 return err;
68}
69
70int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
71 unsigned addr, u8 in, u8* out)
72{
73 struct mmc_command cmd;
74 int err;
75
76 BUG_ON(!card);
77 BUG_ON(fn > 7);
78
79 memset(&cmd, 0, sizeof(struct mmc_command));
80
81 cmd.opcode = SD_IO_RW_DIRECT;
82 cmd.arg = write ? 0x80000000 : 0x00000000;
83 cmd.arg |= fn << 28;
84 cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
85 cmd.arg |= addr << 9;
86 cmd.arg |= in;
87 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
88
89 err = mmc_wait_for_cmd(card->host, &cmd, 0);
90 if (err)
91 return err;
92
93 if (mmc_host_is_spi(card->host)) {
94 /* host driver already reported errors */
95 } else {
96 if (cmd.resp[0] & R5_ERROR)
97 return -EIO;
98 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
99 return -EINVAL;
100 if (cmd.resp[0] & R5_OUT_OF_RANGE)
101 return -ERANGE;
102 }
103
104 if (out) {
105 if (mmc_host_is_spi(card->host))
106 *out = (cmd.resp[0] >> 8) & 0xFF;
107 else
108 *out = cmd.resp[0] & 0xFF;
109 }
110
111 return 0;
112}
113
114int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
115 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
116{
117 struct mmc_request mrq;
118 struct mmc_command cmd;
119 struct mmc_data data;
120 struct scatterlist sg;
121
122 BUG_ON(!card);
123 BUG_ON(fn > 7);
124 BUG_ON(blocks == 1 && blksz > 512);
125 WARN_ON(blocks == 0);
126 WARN_ON(blksz == 0);
127
128 memset(&mrq, 0, sizeof(struct mmc_request));
129 memset(&cmd, 0, sizeof(struct mmc_command));
130 memset(&data, 0, sizeof(struct mmc_data));
131
132 mrq.cmd = &cmd;
133 mrq.data = &data;
134
135 cmd.opcode = SD_IO_RW_EXTENDED;
136 cmd.arg = write ? 0x80000000 : 0x00000000;
137 cmd.arg |= fn << 28;
138 cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
139 cmd.arg |= addr << 9;
140 if (blocks == 1 && blksz <= 512)
141 cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
142 else
143 cmd.arg |= 0x08000000 | blocks; /* block mode */
144 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
145
146 data.blksz = blksz;
147 data.blocks = blocks;
148 data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
149 data.sg = &sg;
150 data.sg_len = 1;
151
152 sg_init_one(&sg, buf, blksz * blocks);
153
154 mmc_set_data_timeout(&data, card);
155
156 mmc_wait_for_req(card->host, &mrq);
157
158 if (cmd.error)
159 return cmd.error;
160 if (data.error)
161 return data.error;
162
163 if (mmc_host_is_spi(card->host)) {
164 /* host driver already reported errors */
165 } else {
166 if (cmd.resp[0] & R5_ERROR)
167 return -EIO;
168 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
169 return -EINVAL;
170 if (cmd.resp[0] & R5_OUT_OF_RANGE)
171 return -ERANGE;
172 }
173
174 return 0;
175}
176
diff --git a/drivers/mmc/core/sdio_ops.h b/drivers/mmc/core/sdio_ops.h
new file mode 100644
index 000000000000..e2e74b0d17d8
--- /dev/null
+++ b/drivers/mmc/core/sdio_ops.h
@@ -0,0 +1,22 @@
1/*
2 * linux/drivers/mmc/sdio_ops.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#ifndef _MMC_SDIO_OPS_H
13#define _MMC_SDIO_OPS_H
14
15int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
16int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
17 unsigned addr, u8 in, u8* out);
18int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
19 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz);
20
21#endif
22
diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
index e23082fe88d0..5fef6783c716 100644
--- a/drivers/mmc/host/Kconfig
+++ b/drivers/mmc/host/Kconfig
@@ -35,6 +35,23 @@ config MMC_SDHCI
35 35
36 If unsure, say N. 36 If unsure, say N.
37 37
38config MMC_RICOH_MMC
39 tristate "Ricoh MMC Controller Disabler (EXPERIMENTAL)"
40 depends on PCI && EXPERIMENTAL && MMC_SDHCI
41 help
42 This selects the disabler for the Ricoh MMC Controller. This
43 proprietary controller is unnecessary because the SDHCI driver
44 supports MMC cards on the SD controller, but if it is not
45 disabled, it will steal the MMC cards away - rendering them
46 useless. It is safe to select this driver even if you don't
47 have a Ricoh based card reader.
48
49
50 To compile this driver as a module, choose M here:
51 the module will be called ricoh_mmc.
52
53 If unsure, say Y.
54
38config MMC_OMAP 55config MMC_OMAP
39 tristate "TI OMAP Multimedia Card Interface support" 56 tristate "TI OMAP Multimedia Card Interface support"
40 depends on ARCH_OMAP 57 depends on ARCH_OMAP
@@ -100,3 +117,16 @@ config MMC_TIFM_SD
100 To compile this driver as a module, choose M here: the 117 To compile this driver as a module, choose M here: the
101 module will be called tifm_sd. 118 module will be called tifm_sd.
102 119
120config MMC_SPI
121 tristate "MMC/SD over SPI (EXPERIMENTAL)"
122 depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
123 select CRC7
124 select CRC_ITU_T
125 help
126 Some systems accss MMC/SD cards using a SPI controller instead of
127 using a "native" MMC/SD controller. This has a disadvantage of
128 being relatively high overhead, but a compensating advantage of
129 working on many systems without dedicated MMC/SD controllers.
130
131 If unsure, or if your system has no SPI master driver, say N.
132
diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
index 6685f64345b4..3877c87e6da2 100644
--- a/drivers/mmc/host/Makefile
+++ b/drivers/mmc/host/Makefile
@@ -10,9 +10,11 @@ obj-$(CONFIG_MMC_ARMMMCI) += mmci.o
10obj-$(CONFIG_MMC_PXA) += pxamci.o 10obj-$(CONFIG_MMC_PXA) += pxamci.o
11obj-$(CONFIG_MMC_IMX) += imxmmc.o 11obj-$(CONFIG_MMC_IMX) += imxmmc.o
12obj-$(CONFIG_MMC_SDHCI) += sdhci.o 12obj-$(CONFIG_MMC_SDHCI) += sdhci.o
13obj-$(CONFIG_MMC_RICOH_MMC) += ricoh_mmc.o
13obj-$(CONFIG_MMC_WBSD) += wbsd.o 14obj-$(CONFIG_MMC_WBSD) += wbsd.o
14obj-$(CONFIG_MMC_AU1X) += au1xmmc.o 15obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
15obj-$(CONFIG_MMC_OMAP) += omap.o 16obj-$(CONFIG_MMC_OMAP) += omap.o
16obj-$(CONFIG_MMC_AT91) += at91_mci.o 17obj-$(CONFIG_MMC_AT91) += at91_mci.o
17obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o 18obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
19obj-$(CONFIG_MMC_SPI) += mmc_spi.o
18 20
diff --git a/drivers/mmc/host/at91_mci.c b/drivers/mmc/host/at91_mci.c
index 955ea60583b5..6ba98a49612d 100644
--- a/drivers/mmc/host/at91_mci.c
+++ b/drivers/mmc/host/at91_mci.c
@@ -328,7 +328,7 @@ static void at91_mci_handle_transmitted(struct at91mci_host *host)
328 data = cmd->data; 328 data = cmd->data;
329 if (!data) return; 329 if (!data) return;
330 330
331 if (cmd->data->flags & MMC_DATA_MULTI) { 331 if (cmd->data->blocks > 1) {
332 pr_debug("multiple write : wait for BLKE...\n"); 332 pr_debug("multiple write : wait for BLKE...\n");
333 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE); 333 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
334 } else 334 } else
@@ -428,6 +428,14 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
428 } 428 }
429 429
430 if (data) { 430 if (data) {
431
432 if ( data->blksz & 0x3 ) {
433 pr_debug("Unsupported block size\n");
434 cmd->error = -EINVAL;
435 mmc_request_done(host->mmc, host->request);
436 return;
437 }
438
431 block_length = data->blksz; 439 block_length = data->blksz;
432 blocks = data->blocks; 440 blocks = data->blocks;
433 441
@@ -439,7 +447,7 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
439 447
440 if (data->flags & MMC_DATA_STREAM) 448 if (data->flags & MMC_DATA_STREAM)
441 cmdr |= AT91_MCI_TRTYP_STREAM; 449 cmdr |= AT91_MCI_TRTYP_STREAM;
442 if (data->flags & MMC_DATA_MULTI) 450 if (data->blocks > 1)
443 cmdr |= AT91_MCI_TRTYP_MULTIPLE; 451 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
444 } 452 }
445 else { 453 else {
@@ -577,24 +585,22 @@ static void at91_mci_completed_command(struct at91mci_host *host)
577 AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE | 585 AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE |
578 AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) { 586 AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)) {
579 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) { 587 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
580 cmd->error = MMC_ERR_NONE; 588 cmd->error = 0;
581 } 589 }
582 else { 590 else {
583 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE)) 591 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
584 cmd->error = MMC_ERR_TIMEOUT; 592 cmd->error = -ETIMEDOUT;
585 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE)) 593 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
586 cmd->error = MMC_ERR_BADCRC; 594 cmd->error = -EILSEQ;
587 else if (status & (AT91_MCI_OVRE | AT91_MCI_UNRE))
588 cmd->error = MMC_ERR_FIFO;
589 else 595 else
590 cmd->error = MMC_ERR_FAILED; 596 cmd->error = -EIO;
591 597
592 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n", 598 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
593 cmd->error, cmd->opcode, cmd->retries); 599 cmd->error, cmd->opcode, cmd->retries);
594 } 600 }
595 } 601 }
596 else 602 else
597 cmd->error = MMC_ERR_NONE; 603 cmd->error = 0;
598 604
599 at91_mci_process_next(host); 605 at91_mci_process_next(host);
600} 606}
@@ -836,7 +842,6 @@ static int __init at91_mci_probe(struct platform_device *pdev)
836 mmc->f_min = 375000; 842 mmc->f_min = 375000;
837 mmc->f_max = 25000000; 843 mmc->f_max = 25000000;
838 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 844 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
839 mmc->caps = MMC_CAP_BYTEBLOCK;
840 845
841 mmc->max_blk_size = 4095; 846 mmc->max_blk_size = 4095;
842 mmc->max_blk_count = mmc->max_req_size; 847 mmc->max_blk_count = mmc->max_req_size;
diff --git a/drivers/mmc/host/au1xmmc.c b/drivers/mmc/host/au1xmmc.c
index 34c99d4ea041..92c4d0dfee43 100644
--- a/drivers/mmc/host/au1xmmc.c
+++ b/drivers/mmc/host/au1xmmc.c
@@ -186,7 +186,7 @@ static void au1xmmc_tasklet_finish(unsigned long param)
186} 186}
187 187
188static int au1xmmc_send_command(struct au1xmmc_host *host, int wait, 188static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
189 struct mmc_command *cmd, unsigned int flags) 189 struct mmc_command *cmd, struct mmc_data *data)
190{ 190{
191 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT); 191 u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
192 192
@@ -208,19 +208,21 @@ static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
208 default: 208 default:
209 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n", 209 printk(KERN_INFO "au1xmmc: unhandled response type %02x\n",
210 mmc_resp_type(cmd)); 210 mmc_resp_type(cmd));
211 return MMC_ERR_INVALID; 211 return -EINVAL;
212 } 212 }
213 213
214 if (flags & MMC_DATA_READ) { 214 if (data) {
215 if (flags & MMC_DATA_MULTI) 215 if (flags & MMC_DATA_READ) {
216 mmccmd |= SD_CMD_CT_4; 216 if (data->blocks > 1)
217 else 217 mmccmd |= SD_CMD_CT_4;
218 mmccmd |= SD_CMD_CT_2; 218 else
219 } else if (flags & MMC_DATA_WRITE) { 219 mmccmd |= SD_CMD_CT_2;
220 if (flags & MMC_DATA_MULTI) 220 } else if (flags & MMC_DATA_WRITE) {
221 mmccmd |= SD_CMD_CT_3; 221 if (data->blocks > 1)
222 else 222 mmccmd |= SD_CMD_CT_3;
223 mmccmd |= SD_CMD_CT_1; 223 else
224 mmccmd |= SD_CMD_CT_1;
225 }
224 } 226 }
225 227
226 au_writel(cmd->arg, HOST_CMDARG(host)); 228 au_writel(cmd->arg, HOST_CMDARG(host));
@@ -253,7 +255,7 @@ static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
253 IRQ_ON(host, SD_CONFIG_CR); 255 IRQ_ON(host, SD_CONFIG_CR);
254 } 256 }
255 257
256 return MMC_ERR_NONE; 258 return 0;
257} 259}
258 260
259static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status) 261static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
@@ -278,7 +280,7 @@ static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
278 while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB)) 280 while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
279 status = au_readl(HOST_STATUS(host)); 281 status = au_readl(HOST_STATUS(host));
280 282
281 data->error = MMC_ERR_NONE; 283 data->error = 0;
282 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir); 284 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
283 285
284 /* Process any errors */ 286 /* Process any errors */
@@ -288,14 +290,14 @@ static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
288 crc |= ((status & 0x07) == 0x02) ? 0 : 1; 290 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
289 291
290 if (crc) 292 if (crc)
291 data->error = MMC_ERR_BADCRC; 293 data->error = -EILSEQ;
292 294
293 /* Clear the CRC bits */ 295 /* Clear the CRC bits */
294 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host)); 296 au_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
295 297
296 data->bytes_xfered = 0; 298 data->bytes_xfered = 0;
297 299
298 if (data->error == MMC_ERR_NONE) { 300 if (!data->error) {
299 if (host->flags & HOST_F_DMA) { 301 if (host->flags & HOST_F_DMA) {
300 u32 chan = DMA_CHANNEL(host); 302 u32 chan = DMA_CHANNEL(host);
301 303
@@ -475,7 +477,7 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
475 return; 477 return;
476 478
477 cmd = mrq->cmd; 479 cmd = mrq->cmd;
478 cmd->error = MMC_ERR_NONE; 480 cmd->error = 0;
479 481
480 if (cmd->flags & MMC_RSP_PRESENT) { 482 if (cmd->flags & MMC_RSP_PRESENT) {
481 if (cmd->flags & MMC_RSP_136) { 483 if (cmd->flags & MMC_RSP_136) {
@@ -512,11 +514,11 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
512 /* Figure out errors */ 514 /* Figure out errors */
513 515
514 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC)) 516 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
515 cmd->error = MMC_ERR_BADCRC; 517 cmd->error = -EILSEQ;
516 518
517 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV); 519 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
518 520
519 if (!trans || cmd->error != MMC_ERR_NONE) { 521 if (!trans || cmd->error) {
520 522
521 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF); 523 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
522 tasklet_schedule(&host->finish_task); 524 tasklet_schedule(&host->finish_task);
@@ -589,7 +591,7 @@ au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
589 data->sg_len, host->dma.dir); 591 data->sg_len, host->dma.dir);
590 592
591 if (host->dma.len == 0) 593 if (host->dma.len == 0)
592 return MMC_ERR_TIMEOUT; 594 return -ETIMEDOUT;
593 595
594 au_writel(data->blksz - 1, HOST_BLKSIZE(host)); 596 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
595 597
@@ -640,11 +642,11 @@ au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
640 //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF); 642 //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF);
641 } 643 }
642 644
643 return MMC_ERR_NONE; 645 return 0;
644 646
645 dataerr: 647 dataerr:
646 dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir); 648 dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir);
647 return MMC_ERR_TIMEOUT; 649 return -ETIMEDOUT;
648} 650}
649 651
650/* static void au1xmmc_request 652/* static void au1xmmc_request
@@ -656,7 +658,7 @@ static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
656 658
657 struct au1xmmc_host *host = mmc_priv(mmc); 659 struct au1xmmc_host *host = mmc_priv(mmc);
658 unsigned int flags = 0; 660 unsigned int flags = 0;
659 int ret = MMC_ERR_NONE; 661 int ret = 0;
660 662
661 WARN_ON(irqs_disabled()); 663 WARN_ON(irqs_disabled());
662 WARN_ON(host->status != HOST_S_IDLE); 664 WARN_ON(host->status != HOST_S_IDLE);
@@ -672,10 +674,10 @@ static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
672 ret = au1xmmc_prepare_data(host, mrq->data); 674 ret = au1xmmc_prepare_data(host, mrq->data);
673 } 675 }
674 676
675 if (ret == MMC_ERR_NONE) 677 if (!ret)
676 ret = au1xmmc_send_command(host, 0, mrq->cmd, flags); 678 ret = au1xmmc_send_command(host, 0, mrq->cmd, mrq->data);
677 679
678 if (ret != MMC_ERR_NONE) { 680 if (ret) {
679 mrq->cmd->error = ret; 681 mrq->cmd->error = ret;
680 au1xmmc_finish_request(host); 682 au1xmmc_finish_request(host);
681 } 683 }
@@ -764,10 +766,10 @@ static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
764 766
765 if (host->mrq && (status & STATUS_TIMEOUT)) { 767 if (host->mrq && (status & STATUS_TIMEOUT)) {
766 if (status & SD_STATUS_RAT) 768 if (status & SD_STATUS_RAT)
767 host->mrq->cmd->error = MMC_ERR_TIMEOUT; 769 host->mrq->cmd->error = -ETIMEDOUT;
768 770
769 else if (status & SD_STATUS_DT) 771 else if (status & SD_STATUS_DT)
770 host->mrq->data->error = MMC_ERR_TIMEOUT; 772 host->mrq->data->error = -ETIMEDOUT;
771 773
772 /* In PIO mode, interrupts might still be enabled */ 774 /* In PIO mode, interrupts might still be enabled */
773 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH); 775 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
diff --git a/drivers/mmc/host/imxmmc.c b/drivers/mmc/host/imxmmc.c
index 54bfc9f25596..6ebc41e7592c 100644
--- a/drivers/mmc/host/imxmmc.c
+++ b/drivers/mmc/host/imxmmc.c
@@ -428,11 +428,11 @@ static int imxmci_finish_data(struct imxmci_host *host, unsigned int stat)
428 if ( stat & STATUS_ERR_MASK ) { 428 if ( stat & STATUS_ERR_MASK ) {
429 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",stat); 429 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",stat);
430 if(stat & (STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR)) 430 if(stat & (STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR))
431 data->error = MMC_ERR_BADCRC; 431 data->error = -EILSEQ;
432 else if(stat & STATUS_TIME_OUT_READ) 432 else if(stat & STATUS_TIME_OUT_READ)
433 data->error = MMC_ERR_TIMEOUT; 433 data->error = -ETIMEDOUT;
434 else 434 else
435 data->error = MMC_ERR_FAILED; 435 data->error = -EIO;
436 } else { 436 } else {
437 data->bytes_xfered = host->dma_size; 437 data->bytes_xfered = host->dma_size;
438 } 438 }
@@ -458,10 +458,10 @@ static int imxmci_cmd_done(struct imxmci_host *host, unsigned int stat)
458 458
459 if (stat & STATUS_TIME_OUT_RESP) { 459 if (stat & STATUS_TIME_OUT_RESP) {
460 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n"); 460 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
461 cmd->error = MMC_ERR_TIMEOUT; 461 cmd->error = -ETIMEDOUT;
462 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) { 462 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
463 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n"); 463 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
464 cmd->error = MMC_ERR_BADCRC; 464 cmd->error = -EILSEQ;
465 } 465 }
466 466
467 if(cmd->flags & MMC_RSP_PRESENT) { 467 if(cmd->flags & MMC_RSP_PRESENT) {
@@ -482,7 +482,7 @@ static int imxmci_cmd_done(struct imxmci_host *host, unsigned int stat)
482 dev_dbg(mmc_dev(host->mmc), "RESP 0x%08x, 0x%08x, 0x%08x, 0x%08x, error %d\n", 482 dev_dbg(mmc_dev(host->mmc), "RESP 0x%08x, 0x%08x, 0x%08x, 0x%08x, error %d\n",
483 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3], cmd->error); 483 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3], cmd->error);
484 484
485 if (data && (cmd->error == MMC_ERR_NONE) && !(stat & STATUS_ERR_MASK)) { 485 if (data && !cmd->error && !(stat & STATUS_ERR_MASK)) {
486 if (host->req->data->flags & MMC_DATA_WRITE) { 486 if (host->req->data->flags & MMC_DATA_WRITE) {
487 487
488 /* Wait for FIFO to be empty before starting DMA write */ 488 /* Wait for FIFO to be empty before starting DMA write */
@@ -491,7 +491,7 @@ static int imxmci_cmd_done(struct imxmci_host *host, unsigned int stat)
491 if(imxmci_busy_wait_for_status(host, &stat, 491 if(imxmci_busy_wait_for_status(host, &stat,
492 STATUS_APPL_BUFF_FE, 492 STATUS_APPL_BUFF_FE,
493 40, "imxmci_cmd_done DMA WR") < 0) { 493 40, "imxmci_cmd_done DMA WR") < 0) {
494 cmd->error = MMC_ERR_FIFO; 494 cmd->error = -EIO;
495 imxmci_finish_data(host, stat); 495 imxmci_finish_data(host, stat);
496 if(host->req) 496 if(host->req)
497 imxmci_finish_request(host, host->req); 497 imxmci_finish_request(host, host->req);
@@ -884,9 +884,21 @@ static void imxmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
884 } 884 }
885} 885}
886 886
887static int imxmci_get_ro(struct mmc_host *mmc)
888{
889 struct imxmci_host *host = mmc_priv(mmc);
890
891 if (host->pdata && host->pdata->get_ro)
892 return host->pdata->get_ro(mmc_dev(mmc));
893 /* Host doesn't support read only detection so assume writeable */
894 return 0;
895}
896
897
887static const struct mmc_host_ops imxmci_ops = { 898static const struct mmc_host_ops imxmci_ops = {
888 .request = imxmci_request, 899 .request = imxmci_request,
889 .set_ios = imxmci_set_ios, 900 .set_ios = imxmci_set_ios,
901 .get_ro = imxmci_get_ro,
890}; 902};
891 903
892static struct resource *platform_device_resource(struct platform_device *dev, unsigned int mask, int nr) 904static struct resource *platform_device_resource(struct platform_device *dev, unsigned int mask, int nr)
@@ -913,7 +925,7 @@ static void imxmci_check_status(unsigned long data)
913{ 925{
914 struct imxmci_host *host = (struct imxmci_host *)data; 926 struct imxmci_host *host = (struct imxmci_host *)data;
915 927
916 if( host->pdata->card_present() != host->present ) { 928 if( host->pdata->card_present(mmc_dev(host->mmc)) != host->present ) {
917 host->present ^= 1; 929 host->present ^= 1;
918 dev_info(mmc_dev(host->mmc), "card %s\n", 930 dev_info(mmc_dev(host->mmc), "card %s\n",
919 host->present ? "inserted" : "removed"); 931 host->present ? "inserted" : "removed");
@@ -963,7 +975,7 @@ static int imxmci_probe(struct platform_device *pdev)
963 mmc->f_min = 150000; 975 mmc->f_min = 150000;
964 mmc->f_max = CLK_RATE/2; 976 mmc->f_max = CLK_RATE/2;
965 mmc->ocr_avail = MMC_VDD_32_33; 977 mmc->ocr_avail = MMC_VDD_32_33;
966 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_BYTEBLOCK; 978 mmc->caps = MMC_CAP_4_BIT_DATA;
967 979
968 /* MMC core transfer sizes tunable parameters */ 980 /* MMC core transfer sizes tunable parameters */
969 mmc->max_hw_segs = 64; 981 mmc->max_hw_segs = 64;
@@ -1022,7 +1034,7 @@ static int imxmci_probe(struct platform_device *pdev)
1022 if (ret) 1034 if (ret)
1023 goto out; 1035 goto out;
1024 1036
1025 host->present = host->pdata->card_present(); 1037 host->present = host->pdata->card_present(mmc_dev(mmc));
1026 init_timer(&host->timer); 1038 init_timer(&host->timer);
1027 host->timer.data = (unsigned long)host; 1039 host->timer.data = (unsigned long)host;
1028 host->timer.function = imxmci_check_status; 1040 host->timer.function = imxmci_check_status;
diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
new file mode 100644
index 000000000000..f30327bba6f6
--- /dev/null
+++ b/drivers/mmc/host/mmc_spi.c
@@ -0,0 +1,1408 @@
1/*
2 * mmc_spi.c - Access SD/MMC cards through SPI master controllers
3 *
4 * (C) Copyright 2005, Intec Automation,
5 * Mike Lavender (mike@steroidmicros)
6 * (C) Copyright 2006-2007, David Brownell
7 * (C) Copyright 2007, Axis Communications,
8 * Hans-Peter Nilsson (hp@axis.com)
9 * (C) Copyright 2007, ATRON electronic GmbH,
10 * Jan Nikitenko <jan.nikitenko@gmail.com>
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27#include <linux/hrtimer.h>
28#include <linux/delay.h>
29#include <linux/blkdev.h>
30#include <linux/dma-mapping.h>
31#include <linux/crc7.h>
32#include <linux/crc-itu-t.h>
33
34#include <linux/mmc/host.h>
35#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
36
37#include <linux/spi/spi.h>
38#include <linux/spi/mmc_spi.h>
39
40#include <asm/unaligned.h>
41
42
43/* NOTES:
44 *
45 * - For now, we won't try to interoperate with a real mmc/sd/sdio
46 * controller, although some of them do have hardware support for
47 * SPI protocol. The main reason for such configs would be mmc-ish
48 * cards like DataFlash, which don't support that "native" protocol.
49 *
50 * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
51 * switch between driver stacks, and in any case if "native" mode
52 * is available, it will be faster and hence preferable.
53 *
54 * - MMC depends on a different chipselect management policy than the
55 * SPI interface currently supports for shared bus segments: it needs
56 * to issue multiple spi_message requests with the chipselect active,
57 * using the results of one message to decide the next one to issue.
58 *
59 * Pending updates to the programming interface, this driver expects
60 * that it not share the bus with other drivers (precluding conflicts).
61 *
62 * - We tell the controller to keep the chipselect active from the
63 * beginning of an mmc_host_ops.request until the end. So beware
64 * of SPI controller drivers that mis-handle the cs_change flag!
65 *
66 * However, many cards seem OK with chipselect flapping up/down
67 * during that time ... at least on unshared bus segments.
68 */
69
70
71/*
72 * Local protocol constants, internal to data block protocols.
73 */
74
75/* Response tokens used to ack each block written: */
76#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
77#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
78#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
79#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
80
81/* Read and write blocks start with these tokens and end with crc;
82 * on error, read tokens act like a subset of R2_SPI_* values.
83 */
84#define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
85#define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
86#define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
87
88#define MMC_SPI_BLOCKSIZE 512
89
90
91/* These fixed timeouts come from the latest SD specs, which say to ignore
92 * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
93 * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
94 * reads which takes nowhere near that long. Older cards may be able to use
95 * shorter timeouts ... but why bother?
96 */
97#define readblock_timeout ktime_set(0, 100 * 1000 * 1000)
98#define writeblock_timeout ktime_set(0, 250 * 1000 * 1000)
99#define r1b_timeout ktime_set(3, 0)
100
101
102/****************************************************************************/
103
104/*
105 * Local Data Structures
106 */
107
108/* "scratch" is per-{command,block} data exchanged with the card */
109struct scratch {
110 u8 status[29];
111 u8 data_token;
112 __be16 crc_val;
113};
114
115struct mmc_spi_host {
116 struct mmc_host *mmc;
117 struct spi_device *spi;
118
119 unsigned char power_mode;
120 u16 powerup_msecs;
121
122 struct mmc_spi_platform_data *pdata;
123
124 /* for bulk data transfers */
125 struct spi_transfer token, t, crc, early_status;
126 struct spi_message m;
127
128 /* for status readback */
129 struct spi_transfer status;
130 struct spi_message readback;
131
132 /* underlying DMA-aware controller, or null */
133 struct device *dma_dev;
134
135 /* buffer used for commands and for message "overhead" */
136 struct scratch *data;
137 dma_addr_t data_dma;
138
139 /* Specs say to write ones most of the time, even when the card
140 * has no need to read its input data; and many cards won't care.
141 * This is our source of those ones.
142 */
143 void *ones;
144 dma_addr_t ones_dma;
145};
146
147
148/****************************************************************************/
149
150/*
151 * MMC-over-SPI protocol glue, used by the MMC stack interface
152 */
153
154static inline int mmc_cs_off(struct mmc_spi_host *host)
155{
156 /* chipselect will always be inactive after setup() */
157 return spi_setup(host->spi);
158}
159
160static int
161mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
162{
163 int status;
164
165 if (len > sizeof(*host->data)) {
166 WARN_ON(1);
167 return -EIO;
168 }
169
170 host->status.len = len;
171
172 if (host->dma_dev)
173 dma_sync_single_for_device(host->dma_dev,
174 host->data_dma, sizeof(*host->data),
175 DMA_FROM_DEVICE);
176
177 status = spi_sync(host->spi, &host->readback);
178 if (status == 0)
179 status = host->readback.status;
180
181 if (host->dma_dev)
182 dma_sync_single_for_cpu(host->dma_dev,
183 host->data_dma, sizeof(*host->data),
184 DMA_FROM_DEVICE);
185
186 return status;
187}
188
189static int
190mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
191{
192 u8 *cp = host->data->status;
193
194 timeout = ktime_add(timeout, ktime_get());
195
196 while (1) {
197 int status;
198 unsigned i;
199
200 status = mmc_spi_readbytes(host, n);
201 if (status < 0)
202 return status;
203
204 for (i = 0; i < n; i++) {
205 if (cp[i] != byte)
206 return cp[i];
207 }
208
209 /* REVISIT investigate msleep() to avoid busy-wait I/O
210 * in at least some cases.
211 */
212 if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
213 break;
214 }
215 return -ETIMEDOUT;
216}
217
218static inline int
219mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
220{
221 return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
222}
223
224static int mmc_spi_readtoken(struct mmc_spi_host *host)
225{
226 return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
227}
228
229
230/*
231 * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
232 * hosts return! The low byte holds R1_SPI bits. The next byte may hold
233 * R2_SPI bits ... for SEND_STATUS, or after data read errors.
234 *
235 * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
236 * newer cards R7 (IF_COND).
237 */
238
239static char *maptype(struct mmc_command *cmd)
240{
241 switch (mmc_spi_resp_type(cmd)) {
242 case MMC_RSP_SPI_R1: return "R1";
243 case MMC_RSP_SPI_R1B: return "R1B";
244 case MMC_RSP_SPI_R2: return "R2/R5";
245 case MMC_RSP_SPI_R3: return "R3/R4/R7";
246 default: return "?";
247 }
248}
249
250/* return zero, else negative errno after setting cmd->error */
251static int mmc_spi_response_get(struct mmc_spi_host *host,
252 struct mmc_command *cmd, int cs_on)
253{
254 u8 *cp = host->data->status;
255 u8 *end = cp + host->t.len;
256 int value = 0;
257 char tag[32];
258
259 snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
260 cmd->opcode, maptype(cmd));
261
262 /* Except for data block reads, the whole response will already
263 * be stored in the scratch buffer. It's somewhere after the
264 * command and the first byte we read after it. We ignore that
265 * first byte. After STOP_TRANSMISSION command it may include
266 * two data bits, but otherwise it's all ones.
267 */
268 cp += 8;
269 while (cp < end && *cp == 0xff)
270 cp++;
271
272 /* Data block reads (R1 response types) may need more data... */
273 if (cp == end) {
274 unsigned i;
275
276 cp = host->data->status;
277
278 /* Card sends N(CR) (== 1..8) bytes of all-ones then one
279 * status byte ... and we already scanned 2 bytes.
280 *
281 * REVISIT block read paths use nasty byte-at-a-time I/O
282 * so it can always DMA directly into the target buffer.
283 * It'd probably be better to memcpy() the first chunk and
284 * avoid extra i/o calls...
285 */
286 for (i = 2; i < 9; i++) {
287 value = mmc_spi_readbytes(host, 1);
288 if (value < 0)
289 goto done;
290 if (*cp != 0xff)
291 goto checkstatus;
292 }
293 value = -ETIMEDOUT;
294 goto done;
295 }
296
297checkstatus:
298 if (*cp & 0x80) {
299 dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
300 tag, *cp);
301 value = -EBADR;
302 goto done;
303 }
304
305 cmd->resp[0] = *cp++;
306 cmd->error = 0;
307
308 /* Status byte: the entire seven-bit R1 response. */
309 if (cmd->resp[0] != 0) {
310 if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS
311 | R1_SPI_ILLEGAL_COMMAND)
312 & cmd->resp[0])
313 value = -EINVAL;
314 else if (R1_SPI_COM_CRC & cmd->resp[0])
315 value = -EILSEQ;
316 else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
317 & cmd->resp[0])
318 value = -EIO;
319 /* else R1_SPI_IDLE, "it's resetting" */
320 }
321
322 switch (mmc_spi_resp_type(cmd)) {
323
324 /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
325 * and less-common stuff like various erase operations.
326 */
327 case MMC_RSP_SPI_R1B:
328 /* maybe we read all the busy tokens already */
329 while (cp < end && *cp == 0)
330 cp++;
331 if (cp == end)
332 mmc_spi_wait_unbusy(host, r1b_timeout);
333 break;
334
335 /* SPI R2 == R1 + second status byte; SEND_STATUS
336 * SPI R5 == R1 + data byte; IO_RW_DIRECT
337 */
338 case MMC_RSP_SPI_R2:
339 cmd->resp[0] |= *cp << 8;
340 break;
341
342 /* SPI R3, R4, or R7 == R1 + 4 bytes */
343 case MMC_RSP_SPI_R3:
344 cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
345 break;
346
347 /* SPI R1 == just one status byte */
348 case MMC_RSP_SPI_R1:
349 break;
350
351 default:
352 dev_dbg(&host->spi->dev, "bad response type %04x\n",
353 mmc_spi_resp_type(cmd));
354 if (value >= 0)
355 value = -EINVAL;
356 goto done;
357 }
358
359 if (value < 0)
360 dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
361 tag, cmd->resp[0], cmd->resp[1]);
362
363 /* disable chipselect on errors and some success cases */
364 if (value >= 0 && cs_on)
365 return value;
366done:
367 if (value < 0)
368 cmd->error = value;
369 mmc_cs_off(host);
370 return value;
371}
372
373/* Issue command and read its response.
374 * Returns zero on success, negative for error.
375 *
376 * On error, caller must cope with mmc core retry mechanism. That
377 * means immediate low-level resubmit, which affects the bus lock...
378 */
379static int
380mmc_spi_command_send(struct mmc_spi_host *host,
381 struct mmc_request *mrq,
382 struct mmc_command *cmd, int cs_on)
383{
384 struct scratch *data = host->data;
385 u8 *cp = data->status;
386 u32 arg = cmd->arg;
387 int status;
388 struct spi_transfer *t;
389
390 /* We can handle most commands (except block reads) in one full
391 * duplex I/O operation before either starting the next transfer
392 * (data block or command) or else deselecting the card.
393 *
394 * First, write 7 bytes:
395 * - an all-ones byte to ensure the card is ready
396 * - opcode byte (plus start and transmission bits)
397 * - four bytes of big-endian argument
398 * - crc7 (plus end bit) ... always computed, it's cheap
399 *
400 * We init the whole buffer to all-ones, which is what we need
401 * to write while we're reading (later) response data.
402 */
403 memset(cp++, 0xff, sizeof(data->status));
404
405 *cp++ = 0x40 | cmd->opcode;
406 *cp++ = (u8)(arg >> 24);
407 *cp++ = (u8)(arg >> 16);
408 *cp++ = (u8)(arg >> 8);
409 *cp++ = (u8)arg;
410 *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
411
412 /* Then, read up to 13 bytes (while writing all-ones):
413 * - N(CR) (== 1..8) bytes of all-ones
414 * - status byte (for all response types)
415 * - the rest of the response, either:
416 * + nothing, for R1 or R1B responses
417 * + second status byte, for R2 responses
418 * + four data bytes, for R3 and R7 responses
419 *
420 * Finally, read some more bytes ... in the nice cases we know in
421 * advance how many, and reading 1 more is always OK:
422 * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
423 * - N(RC) (== 1..N) bytes of all-ones, before next command
424 * - N(WR) (== 1..N) bytes of all-ones, before data write
425 *
426 * So in those cases one full duplex I/O of at most 21 bytes will
427 * handle the whole command, leaving the card ready to receive a
428 * data block or new command. We do that whenever we can, shaving
429 * CPU and IRQ costs (especially when using DMA or FIFOs).
430 *
431 * There are two other cases, where it's not generally practical
432 * to rely on a single I/O:
433 *
434 * - R1B responses need at least N(EC) bytes of all-zeroes.
435 *
436 * In this case we can *try* to fit it into one I/O, then
437 * maybe read more data later.
438 *
439 * - Data block reads are more troublesome, since a variable
440 * number of padding bytes precede the token and data.
441 * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
442 * + N(AC) (== 1..many) bytes of all-ones
443 *
444 * In this case we currently only have minimal speedups here:
445 * when N(CR) == 1 we can avoid I/O in response_get().
446 */
447 if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
448 cp += 2; /* min(N(CR)) + status */
449 /* R1 */
450 } else {
451 cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
452 if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
453 cp++;
454 else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
455 cp += 4;
456 else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
457 cp = data->status + sizeof(data->status);
458 /* else: R1 (most commands) */
459 }
460
461 dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
462 cmd->opcode, maptype(cmd));
463
464 /* send command, leaving chipselect active */
465 spi_message_init(&host->m);
466
467 t = &host->t;
468 memset(t, 0, sizeof(*t));
469 t->tx_buf = t->rx_buf = data->status;
470 t->tx_dma = t->rx_dma = host->data_dma;
471 t->len = cp - data->status;
472 t->cs_change = 1;
473 spi_message_add_tail(t, &host->m);
474
475 if (host->dma_dev) {
476 host->m.is_dma_mapped = 1;
477 dma_sync_single_for_device(host->dma_dev,
478 host->data_dma, sizeof(*host->data),
479 DMA_BIDIRECTIONAL);
480 }
481 status = spi_sync(host->spi, &host->m);
482 if (status == 0)
483 status = host->m.status;
484
485 if (host->dma_dev)
486 dma_sync_single_for_cpu(host->dma_dev,
487 host->data_dma, sizeof(*host->data),
488 DMA_BIDIRECTIONAL);
489 if (status < 0) {
490 dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
491 cmd->error = status;
492 return status;
493 }
494
495 /* after no-data commands and STOP_TRANSMISSION, chipselect off */
496 return mmc_spi_response_get(host, cmd, cs_on);
497}
498
499/* Build data message with up to four separate transfers. For TX, we
500 * start by writing the data token. And in most cases, we finish with
501 * a status transfer.
502 *
503 * We always provide TX data for data and CRC. The MMC/SD protocol
504 * requires us to write ones; but Linux defaults to writing zeroes;
505 * so we explicitly initialize it to all ones on RX paths.
506 *
507 * We also handle DMA mapping, so the underlying SPI controller does
508 * not need to (re)do it for each message.
509 */
510static void
511mmc_spi_setup_data_message(
512 struct mmc_spi_host *host,
513 int multiple,
514 enum dma_data_direction direction)
515{
516 struct spi_transfer *t;
517 struct scratch *scratch = host->data;
518 dma_addr_t dma = host->data_dma;
519
520 spi_message_init(&host->m);
521 if (dma)
522 host->m.is_dma_mapped = 1;
523
524 /* for reads, readblock() skips 0xff bytes before finding
525 * the token; for writes, this transfer issues that token.
526 */
527 if (direction == DMA_TO_DEVICE) {
528 t = &host->token;
529 memset(t, 0, sizeof(*t));
530 t->len = 1;
531 if (multiple)
532 scratch->data_token = SPI_TOKEN_MULTI_WRITE;
533 else
534 scratch->data_token = SPI_TOKEN_SINGLE;
535 t->tx_buf = &scratch->data_token;
536 if (dma)
537 t->tx_dma = dma + offsetof(struct scratch, data_token);
538 spi_message_add_tail(t, &host->m);
539 }
540
541 /* Body of transfer is buffer, then CRC ...
542 * either TX-only, or RX with TX-ones.
543 */
544 t = &host->t;
545 memset(t, 0, sizeof(*t));
546 t->tx_buf = host->ones;
547 t->tx_dma = host->ones_dma;
548 /* length and actual buffer info are written later */
549 spi_message_add_tail(t, &host->m);
550
551 t = &host->crc;
552 memset(t, 0, sizeof(*t));
553 t->len = 2;
554 if (direction == DMA_TO_DEVICE) {
555 /* the actual CRC may get written later */
556 t->tx_buf = &scratch->crc_val;
557 if (dma)
558 t->tx_dma = dma + offsetof(struct scratch, crc_val);
559 } else {
560 t->tx_buf = host->ones;
561 t->tx_dma = host->ones_dma;
562 t->rx_buf = &scratch->crc_val;
563 if (dma)
564 t->rx_dma = dma + offsetof(struct scratch, crc_val);
565 }
566 spi_message_add_tail(t, &host->m);
567
568 /*
569 * A single block read is followed by N(EC) [0+] all-ones bytes
570 * before deselect ... don't bother.
571 *
572 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
573 * the next block is read, or a STOP_TRANSMISSION is issued. We'll
574 * collect that single byte, so readblock() doesn't need to.
575 *
576 * For a write, the one-byte data response follows immediately, then
577 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
578 * Then single block reads may deselect, and multiblock ones issue
579 * the next token (next data block, or STOP_TRAN). We can try to
580 * minimize I/O ops by using a single read to collect end-of-busy.
581 */
582 if (multiple || direction == DMA_TO_DEVICE) {
583 t = &host->early_status;
584 memset(t, 0, sizeof(*t));
585 t->len = (direction == DMA_TO_DEVICE)
586 ? sizeof(scratch->status)
587 : 1;
588 t->tx_buf = host->ones;
589 t->tx_dma = host->ones_dma;
590 t->rx_buf = scratch->status;
591 if (dma)
592 t->rx_dma = dma + offsetof(struct scratch, status);
593 t->cs_change = 1;
594 spi_message_add_tail(t, &host->m);
595 }
596}
597
598/*
599 * Write one block:
600 * - caller handled preceding N(WR) [1+] all-ones bytes
601 * - data block
602 * + token
603 * + data bytes
604 * + crc16
605 * - an all-ones byte ... card writes a data-response byte
606 * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
607 *
608 * Return negative errno, else success.
609 */
610static int
611mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
612{
613 struct spi_device *spi = host->spi;
614 int status, i;
615 struct scratch *scratch = host->data;
616
617 if (host->mmc->use_spi_crc)
618 scratch->crc_val = cpu_to_be16(
619 crc_itu_t(0, t->tx_buf, t->len));
620 if (host->dma_dev)
621 dma_sync_single_for_device(host->dma_dev,
622 host->data_dma, sizeof(*scratch),
623 DMA_BIDIRECTIONAL);
624
625 status = spi_sync(spi, &host->m);
626 if (status == 0)
627 status = host->m.status;
628
629 if (status != 0) {
630 dev_dbg(&spi->dev, "write error (%d)\n", status);
631 return status;
632 }
633
634 if (host->dma_dev)
635 dma_sync_single_for_cpu(host->dma_dev,
636 host->data_dma, sizeof(*scratch),
637 DMA_BIDIRECTIONAL);
638
639 /*
640 * Get the transmission data-response reply. It must follow
641 * immediately after the data block we transferred. This reply
642 * doesn't necessarily tell whether the write operation succeeded;
643 * it just says if the transmission was ok and whether *earlier*
644 * writes succeeded; see the standard.
645 */
646 switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
647 case SPI_RESPONSE_ACCEPTED:
648 status = 0;
649 break;
650 case SPI_RESPONSE_CRC_ERR:
651 /* host shall then issue MMC_STOP_TRANSMISSION */
652 status = -EILSEQ;
653 break;
654 case SPI_RESPONSE_WRITE_ERR:
655 /* host shall then issue MMC_STOP_TRANSMISSION,
656 * and should MMC_SEND_STATUS to sort it out
657 */
658 status = -EIO;
659 break;
660 default:
661 status = -EPROTO;
662 break;
663 }
664 if (status != 0) {
665 dev_dbg(&spi->dev, "write error %02x (%d)\n",
666 scratch->status[0], status);
667 return status;
668 }
669
670 t->tx_buf += t->len;
671 if (host->dma_dev)
672 t->tx_dma += t->len;
673
674 /* Return when not busy. If we didn't collect that status yet,
675 * we'll need some more I/O.
676 */
677 for (i = 1; i < sizeof(scratch->status); i++) {
678 if (scratch->status[i] != 0)
679 return 0;
680 }
681 return mmc_spi_wait_unbusy(host, writeblock_timeout);
682}
683
684/*
685 * Read one block:
686 * - skip leading all-ones bytes ... either
687 * + N(AC) [1..f(clock,CSD)] usually, else
688 * + N(CX) [0..8] when reading CSD or CID
689 * - data block
690 * + token ... if error token, no data or crc
691 * + data bytes
692 * + crc16
693 *
694 * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
695 * before dropping chipselect.
696 *
697 * For multiblock reads, caller either reads the next block or issues a
698 * STOP_TRANSMISSION command.
699 */
700static int
701mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
702{
703 struct spi_device *spi = host->spi;
704 int status;
705 struct scratch *scratch = host->data;
706
707 /* At least one SD card sends an all-zeroes byte when N(CX)
708 * applies, before the all-ones bytes ... just cope with that.
709 */
710 status = mmc_spi_readbytes(host, 1);
711 if (status < 0)
712 return status;
713 status = scratch->status[0];
714 if (status == 0xff || status == 0)
715 status = mmc_spi_readtoken(host);
716
717 if (status == SPI_TOKEN_SINGLE) {
718 if (host->dma_dev) {
719 dma_sync_single_for_device(host->dma_dev,
720 host->data_dma, sizeof(*scratch),
721 DMA_BIDIRECTIONAL);
722 dma_sync_single_for_device(host->dma_dev,
723 t->rx_dma, t->len,
724 DMA_FROM_DEVICE);
725 }
726
727 status = spi_sync(spi, &host->m);
728 if (status == 0)
729 status = host->m.status;
730
731 if (host->dma_dev) {
732 dma_sync_single_for_cpu(host->dma_dev,
733 host->data_dma, sizeof(*scratch),
734 DMA_BIDIRECTIONAL);
735 dma_sync_single_for_cpu(host->dma_dev,
736 t->rx_dma, t->len,
737 DMA_FROM_DEVICE);
738 }
739
740 } else {
741 dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
742
743 /* we've read extra garbage, timed out, etc */
744 if (status < 0)
745 return status;
746
747 /* low four bits are an R2 subset, fifth seems to be
748 * vendor specific ... map them all to generic error..
749 */
750 return -EIO;
751 }
752
753 if (host->mmc->use_spi_crc) {
754 u16 crc = crc_itu_t(0, t->rx_buf, t->len);
755
756 be16_to_cpus(&scratch->crc_val);
757 if (scratch->crc_val != crc) {
758 dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
759 "computed=0x%04x len=%d\n",
760 scratch->crc_val, crc, t->len);
761 return -EILSEQ;
762 }
763 }
764
765 t->rx_buf += t->len;
766 if (host->dma_dev)
767 t->rx_dma += t->len;
768
769 return 0;
770}
771
772/*
773 * An MMC/SD data stage includes one or more blocks, optional CRCs,
774 * and inline handshaking. That handhaking makes it unlike most
775 * other SPI protocol stacks.
776 */
777static void
778mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
779 struct mmc_data *data, u32 blk_size)
780{
781 struct spi_device *spi = host->spi;
782 struct device *dma_dev = host->dma_dev;
783 struct spi_transfer *t;
784 enum dma_data_direction direction;
785 struct scatterlist *sg;
786 unsigned n_sg;
787 int multiple = (data->blocks > 1);
788
789 if (data->flags & MMC_DATA_READ)
790 direction = DMA_FROM_DEVICE;
791 else
792 direction = DMA_TO_DEVICE;
793 mmc_spi_setup_data_message(host, multiple, direction);
794 t = &host->t;
795
796 /* Handle scatterlist segments one at a time, with synch for
797 * each 512-byte block
798 */
799 for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
800 int status = 0;
801 dma_addr_t dma_addr = 0;
802 void *kmap_addr;
803 unsigned length = sg->length;
804 enum dma_data_direction dir = direction;
805
806 /* set up dma mapping for controller drivers that might
807 * use DMA ... though they may fall back to PIO
808 */
809 if (dma_dev) {
810 /* never invalidate whole *shared* pages ... */
811 if ((sg->offset != 0 || length != PAGE_SIZE)
812 && dir == DMA_FROM_DEVICE)
813 dir = DMA_BIDIRECTIONAL;
814
815 dma_addr = dma_map_page(dma_dev, sg->page, 0,
816 PAGE_SIZE, dir);
817 if (direction == DMA_TO_DEVICE)
818 t->tx_dma = dma_addr + sg->offset;
819 else
820 t->rx_dma = dma_addr + sg->offset;
821 }
822
823 /* allow pio too; we don't allow highmem */
824 kmap_addr = kmap(sg->page);
825 if (direction == DMA_TO_DEVICE)
826 t->tx_buf = kmap_addr + sg->offset;
827 else
828 t->rx_buf = kmap_addr + sg->offset;
829
830 /* transfer each block, and update request status */
831 while (length) {
832 t->len = min(length, blk_size);
833
834 dev_dbg(&host->spi->dev,
835 " mmc_spi: %s block, %d bytes\n",
836 (direction == DMA_TO_DEVICE)
837 ? "write"
838 : "read",
839 t->len);
840
841 if (direction == DMA_TO_DEVICE)
842 status = mmc_spi_writeblock(host, t);
843 else
844 status = mmc_spi_readblock(host, t);
845 if (status < 0)
846 break;
847
848 data->bytes_xfered += t->len;
849 length -= t->len;
850
851 if (!multiple)
852 break;
853 }
854
855 /* discard mappings */
856 if (direction == DMA_FROM_DEVICE)
857 flush_kernel_dcache_page(sg->page);
858 kunmap(sg->page);
859 if (dma_dev)
860 dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
861
862 if (status < 0) {
863 data->error = status;
864 dev_dbg(&spi->dev, "%s status %d\n",
865 (direction == DMA_TO_DEVICE)
866 ? "write" : "read",
867 status);
868 break;
869 }
870 }
871
872 /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
873 * can be issued before multiblock writes. Unlike its more widely
874 * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
875 * that can affect the STOP_TRAN logic. Complete (and current)
876 * MMC specs should sort that out before Linux starts using CMD23.
877 */
878 if (direction == DMA_TO_DEVICE && multiple) {
879 struct scratch *scratch = host->data;
880 int tmp;
881 const unsigned statlen = sizeof(scratch->status);
882
883 dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
884
885 /* Tweak the per-block message we set up earlier by morphing
886 * it to hold single buffer with the token followed by some
887 * all-ones bytes ... skip N(BR) (0..1), scan the rest for
888 * "not busy any longer" status, and leave chip selected.
889 */
890 INIT_LIST_HEAD(&host->m.transfers);
891 list_add(&host->early_status.transfer_list,
892 &host->m.transfers);
893
894 memset(scratch->status, 0xff, statlen);
895 scratch->status[0] = SPI_TOKEN_STOP_TRAN;
896
897 host->early_status.tx_buf = host->early_status.rx_buf;
898 host->early_status.tx_dma = host->early_status.rx_dma;
899 host->early_status.len = statlen;
900
901 if (host->dma_dev)
902 dma_sync_single_for_device(host->dma_dev,
903 host->data_dma, sizeof(*scratch),
904 DMA_BIDIRECTIONAL);
905
906 tmp = spi_sync(spi, &host->m);
907 if (tmp == 0)
908 tmp = host->m.status;
909
910 if (host->dma_dev)
911 dma_sync_single_for_cpu(host->dma_dev,
912 host->data_dma, sizeof(*scratch),
913 DMA_BIDIRECTIONAL);
914
915 if (tmp < 0) {
916 if (!data->error)
917 data->error = tmp;
918 return;
919 }
920
921 /* Ideally we collected "not busy" status with one I/O,
922 * avoiding wasteful byte-at-a-time scanning... but more
923 * I/O is often needed.
924 */
925 for (tmp = 2; tmp < statlen; tmp++) {
926 if (scratch->status[tmp] != 0)
927 return;
928 }
929 tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
930 if (tmp < 0 && !data->error)
931 data->error = tmp;
932 }
933}
934
935/****************************************************************************/
936
937/*
938 * MMC driver implementation -- the interface to the MMC stack
939 */
940
941static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
942{
943 struct mmc_spi_host *host = mmc_priv(mmc);
944 int status = -EINVAL;
945
946#ifdef DEBUG
947 /* MMC core and layered drivers *MUST* issue SPI-aware commands */
948 {
949 struct mmc_command *cmd;
950 int invalid = 0;
951
952 cmd = mrq->cmd;
953 if (!mmc_spi_resp_type(cmd)) {
954 dev_dbg(&host->spi->dev, "bogus command\n");
955 cmd->error = -EINVAL;
956 invalid = 1;
957 }
958
959 cmd = mrq->stop;
960 if (cmd && !mmc_spi_resp_type(cmd)) {
961 dev_dbg(&host->spi->dev, "bogus STOP command\n");
962 cmd->error = -EINVAL;
963 invalid = 1;
964 }
965
966 if (invalid) {
967 dump_stack();
968 mmc_request_done(host->mmc, mrq);
969 return;
970 }
971 }
972#endif
973
974 /* issue command; then optionally data and stop */
975 status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
976 if (status == 0 && mrq->data) {
977 mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
978 if (mrq->stop)
979 status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
980 else
981 mmc_cs_off(host);
982 }
983
984 mmc_request_done(host->mmc, mrq);
985}
986
987/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
988 *
989 * NOTE that here we can't know that the card has just been powered up;
990 * not all MMC/SD sockets support power switching.
991 *
992 * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
993 * this doesn't seem to do the right thing at all...
994 */
995static void mmc_spi_initsequence(struct mmc_spi_host *host)
996{
997 /* Try to be very sure any previous command has completed;
998 * wait till not-busy, skip debris from any old commands.
999 */
1000 mmc_spi_wait_unbusy(host, r1b_timeout);
1001 mmc_spi_readbytes(host, 10);
1002
1003 /*
1004 * Do a burst with chipselect active-high. We need to do this to
1005 * meet the requirement of 74 clock cycles with both chipselect
1006 * and CMD (MOSI) high before CMD0 ... after the card has been
1007 * powered up to Vdd(min), and so is ready to take commands.
1008 *
1009 * Some cards are particularly needy of this (e.g. Viking "SD256")
1010 * while most others don't seem to care.
1011 *
1012 * Note that this is one of the places MMC/SD plays games with the
1013 * SPI protocol. Another is that when chipselect is released while
1014 * the card returns BUSY status, the clock must issue several cycles
1015 * with chipselect high before the card will stop driving its output.
1016 */
1017 host->spi->mode |= SPI_CS_HIGH;
1018 if (spi_setup(host->spi) != 0) {
1019 /* Just warn; most cards work without it. */
1020 dev_warn(&host->spi->dev,
1021 "can't change chip-select polarity\n");
1022 host->spi->mode &= ~SPI_CS_HIGH;
1023 } else {
1024 mmc_spi_readbytes(host, 18);
1025
1026 host->spi->mode &= ~SPI_CS_HIGH;
1027 if (spi_setup(host->spi) != 0) {
1028 /* Wot, we can't get the same setup we had before? */
1029 dev_err(&host->spi->dev,
1030 "can't restore chip-select polarity\n");
1031 }
1032 }
1033}
1034
1035static char *mmc_powerstring(u8 power_mode)
1036{
1037 switch (power_mode) {
1038 case MMC_POWER_OFF: return "off";
1039 case MMC_POWER_UP: return "up";
1040 case MMC_POWER_ON: return "on";
1041 }
1042 return "?";
1043}
1044
1045static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1046{
1047 struct mmc_spi_host *host = mmc_priv(mmc);
1048
1049 if (host->power_mode != ios->power_mode) {
1050 int canpower;
1051
1052 canpower = host->pdata && host->pdata->setpower;
1053
1054 dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1055 mmc_powerstring(ios->power_mode),
1056 ios->vdd,
1057 canpower ? ", can switch" : "");
1058
1059 /* switch power on/off if possible, accounting for
1060 * max 250msec powerup time if needed.
1061 */
1062 if (canpower) {
1063 switch (ios->power_mode) {
1064 case MMC_POWER_OFF:
1065 case MMC_POWER_UP:
1066 host->pdata->setpower(&host->spi->dev,
1067 ios->vdd);
1068 if (ios->power_mode == MMC_POWER_UP)
1069 msleep(host->powerup_msecs);
1070 }
1071 }
1072
1073 /* See 6.4.1 in the simplified SD card physical spec 2.0 */
1074 if (ios->power_mode == MMC_POWER_ON)
1075 mmc_spi_initsequence(host);
1076
1077 /* If powering down, ground all card inputs to avoid power
1078 * delivery from data lines! On a shared SPI bus, this
1079 * will probably be temporary; 6.4.2 of the simplified SD
1080 * spec says this must last at least 1msec.
1081 *
1082 * - Clock low means CPOL 0, e.g. mode 0
1083 * - MOSI low comes from writing zero
1084 * - Chipselect is usually active low...
1085 */
1086 if (canpower && ios->power_mode == MMC_POWER_OFF) {
1087 int mres;
1088
1089 host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1090 mres = spi_setup(host->spi);
1091 if (mres < 0)
1092 dev_dbg(&host->spi->dev,
1093 "switch to SPI mode 0 failed\n");
1094
1095 if (spi_w8r8(host->spi, 0x00) < 0)
1096 dev_dbg(&host->spi->dev,
1097 "put spi signals to low failed\n");
1098
1099 /*
1100 * Now clock should be low due to spi mode 0;
1101 * MOSI should be low because of written 0x00;
1102 * chipselect should be low (it is active low)
1103 * power supply is off, so now MMC is off too!
1104 *
1105 * FIXME no, chipselect can be high since the
1106 * device is inactive and SPI_CS_HIGH is clear...
1107 */
1108 msleep(10);
1109 if (mres == 0) {
1110 host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1111 mres = spi_setup(host->spi);
1112 if (mres < 0)
1113 dev_dbg(&host->spi->dev,
1114 "switch back to SPI mode 3"
1115 " failed\n");
1116 }
1117 }
1118
1119 host->power_mode = ios->power_mode;
1120 }
1121
1122 if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1123 int status;
1124
1125 host->spi->max_speed_hz = ios->clock;
1126 status = spi_setup(host->spi);
1127 dev_dbg(&host->spi->dev,
1128 "mmc_spi: clock to %d Hz, %d\n",
1129 host->spi->max_speed_hz, status);
1130 }
1131}
1132
1133static int mmc_spi_get_ro(struct mmc_host *mmc)
1134{
1135 struct mmc_spi_host *host = mmc_priv(mmc);
1136
1137 if (host->pdata && host->pdata->get_ro)
1138 return host->pdata->get_ro(mmc->parent);
1139 /* board doesn't support read only detection; assume writeable */
1140 return 0;
1141}
1142
1143
1144static const struct mmc_host_ops mmc_spi_ops = {
1145 .request = mmc_spi_request,
1146 .set_ios = mmc_spi_set_ios,
1147 .get_ro = mmc_spi_get_ro,
1148};
1149
1150
1151/****************************************************************************/
1152
1153/*
1154 * SPI driver implementation
1155 */
1156
1157static irqreturn_t
1158mmc_spi_detect_irq(int irq, void *mmc)
1159{
1160 struct mmc_spi_host *host = mmc_priv(mmc);
1161 u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1162
1163 mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1164 return IRQ_HANDLED;
1165}
1166
1167static int mmc_spi_probe(struct spi_device *spi)
1168{
1169 void *ones;
1170 struct mmc_host *mmc;
1171 struct mmc_spi_host *host;
1172 int status;
1173
1174 /* MMC and SD specs only seem to care that sampling is on the
1175 * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
1176 * should be legit. We'll use mode 0 since it seems to be a
1177 * bit less troublesome on some hardware ... unclear why.
1178 */
1179 spi->mode = SPI_MODE_0;
1180 spi->bits_per_word = 8;
1181
1182 status = spi_setup(spi);
1183 if (status < 0) {
1184 dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1185 spi->mode, spi->max_speed_hz / 1000,
1186 status);
1187 return status;
1188 }
1189
1190 /* We can use the bus safely iff nobody else will interfere with
1191 * us. That is, either we have the experimental exclusive access
1192 * primitives ... or else there's nobody to share it with.
1193 */
1194 if (spi->master->num_chipselect > 1) {
1195 struct device *parent = spi->dev.parent;
1196
1197 /* If there are multiple devices on this bus, we
1198 * can't proceed.
1199 */
1200 spin_lock(&parent->klist_children.k_lock);
1201 if (parent->klist_children.k_list.next
1202 != parent->klist_children.k_list.prev)
1203 status = -EMLINK;
1204 else
1205 status = 0;
1206 spin_unlock(&parent->klist_children.k_lock);
1207 if (status < 0) {
1208 dev_err(&spi->dev, "can't share SPI bus\n");
1209 return status;
1210 }
1211
1212 /* REVISIT we can't guarantee another device won't
1213 * be added later. It's uncommon though ... for now,
1214 * work as if this is safe.
1215 */
1216 dev_warn(&spi->dev, "ASSUMING unshared SPI bus!\n");
1217 }
1218
1219 /* We need a supply of ones to transmit. This is the only time
1220 * the CPU touches these, so cache coherency isn't a concern.
1221 *
1222 * NOTE if many systems use more than one MMC-over-SPI connector
1223 * it'd save some memory to share this. That's evidently rare.
1224 */
1225 status = -ENOMEM;
1226 ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1227 if (!ones)
1228 goto nomem;
1229 memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1230
1231 mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1232 if (!mmc)
1233 goto nomem;
1234
1235 mmc->ops = &mmc_spi_ops;
1236 mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1237
1238 /* As long as we keep track of the number of successfully
1239 * transmitted blocks, we're good for multiwrite.
1240 */
1241 mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1242
1243 /* SPI doesn't need the lowspeed device identification thing for
1244 * MMC or SD cards, since it never comes up in open drain mode.
1245 * That's good; some SPI masters can't handle very low speeds!
1246 *
1247 * However, low speed SDIO cards need not handle over 400 KHz;
1248 * that's the only reason not to use a few MHz for f_min (until
1249 * the upper layer reads the target frequency from the CSD).
1250 */
1251 mmc->f_min = 400000;
1252 mmc->f_max = spi->max_speed_hz;
1253
1254 host = mmc_priv(mmc);
1255 host->mmc = mmc;
1256 host->spi = spi;
1257
1258 host->ones = ones;
1259
1260 /* Platform data is used to hook up things like card sensing
1261 * and power switching gpios.
1262 */
1263 host->pdata = spi->dev.platform_data;
1264 if (host->pdata)
1265 mmc->ocr_avail = host->pdata->ocr_mask;
1266 if (!mmc->ocr_avail) {
1267 dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1268 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1269 }
1270 if (host->pdata && host->pdata->setpower) {
1271 host->powerup_msecs = host->pdata->powerup_msecs;
1272 if (!host->powerup_msecs || host->powerup_msecs > 250)
1273 host->powerup_msecs = 250;
1274 }
1275
1276 dev_set_drvdata(&spi->dev, mmc);
1277
1278 /* preallocate dma buffers */
1279 host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1280 if (!host->data)
1281 goto fail_nobuf1;
1282
1283 if (spi->master->cdev.dev->dma_mask) {
1284 struct device *dev = spi->master->cdev.dev;
1285
1286 host->dma_dev = dev;
1287 host->ones_dma = dma_map_single(dev, ones,
1288 MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1289 host->data_dma = dma_map_single(dev, host->data,
1290 sizeof(*host->data), DMA_BIDIRECTIONAL);
1291
1292 /* REVISIT in theory those map operations can fail... */
1293
1294 dma_sync_single_for_cpu(host->dma_dev,
1295 host->data_dma, sizeof(*host->data),
1296 DMA_BIDIRECTIONAL);
1297 }
1298
1299 /* setup message for status/busy readback */
1300 spi_message_init(&host->readback);
1301 host->readback.is_dma_mapped = (host->dma_dev != NULL);
1302
1303 spi_message_add_tail(&host->status, &host->readback);
1304 host->status.tx_buf = host->ones;
1305 host->status.tx_dma = host->ones_dma;
1306 host->status.rx_buf = &host->data->status;
1307 host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1308 host->status.cs_change = 1;
1309
1310 /* register card detect irq */
1311 if (host->pdata && host->pdata->init) {
1312 status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1313 if (status != 0)
1314 goto fail_glue_init;
1315 }
1316
1317 status = mmc_add_host(mmc);
1318 if (status != 0)
1319 goto fail_add_host;
1320
1321 dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
1322 mmc->class_dev.bus_id,
1323 host->dma_dev ? "" : ", no DMA",
1324 (host->pdata && host->pdata->get_ro)
1325 ? "" : ", no WP",
1326 (host->pdata && host->pdata->setpower)
1327 ? "" : ", no poweroff");
1328 return 0;
1329
1330fail_add_host:
1331 mmc_remove_host (mmc);
1332fail_glue_init:
1333 if (host->dma_dev)
1334 dma_unmap_single(host->dma_dev, host->data_dma,
1335 sizeof(*host->data), DMA_BIDIRECTIONAL);
1336 kfree(host->data);
1337
1338fail_nobuf1:
1339 mmc_free_host(mmc);
1340 dev_set_drvdata(&spi->dev, NULL);
1341
1342nomem:
1343 kfree(ones);
1344 return status;
1345}
1346
1347
1348static int __devexit mmc_spi_remove(struct spi_device *spi)
1349{
1350 struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
1351 struct mmc_spi_host *host;
1352
1353 if (mmc) {
1354 host = mmc_priv(mmc);
1355
1356 /* prevent new mmc_detect_change() calls */
1357 if (host->pdata && host->pdata->exit)
1358 host->pdata->exit(&spi->dev, mmc);
1359
1360 mmc_remove_host(mmc);
1361
1362 if (host->dma_dev) {
1363 dma_unmap_single(host->dma_dev, host->ones_dma,
1364 MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1365 dma_unmap_single(host->dma_dev, host->data_dma,
1366 sizeof(*host->data), DMA_BIDIRECTIONAL);
1367 }
1368
1369 kfree(host->data);
1370 kfree(host->ones);
1371
1372 spi->max_speed_hz = mmc->f_max;
1373 mmc_free_host(mmc);
1374 dev_set_drvdata(&spi->dev, NULL);
1375 }
1376 return 0;
1377}
1378
1379
1380static struct spi_driver mmc_spi_driver = {
1381 .driver = {
1382 .name = "mmc_spi",
1383 .bus = &spi_bus_type,
1384 .owner = THIS_MODULE,
1385 },
1386 .probe = mmc_spi_probe,
1387 .remove = __devexit_p(mmc_spi_remove),
1388};
1389
1390
1391static int __init mmc_spi_init(void)
1392{
1393 return spi_register_driver(&mmc_spi_driver);
1394}
1395module_init(mmc_spi_init);
1396
1397
1398static void __exit mmc_spi_exit(void)
1399{
1400 spi_unregister_driver(&mmc_spi_driver);
1401}
1402module_exit(mmc_spi_exit);
1403
1404
1405MODULE_AUTHOR("Mike Lavender, David Brownell, "
1406 "Hans-Peter Nilsson, Jan Nikitenko");
1407MODULE_DESCRIPTION("SPI SD/MMC host driver");
1408MODULE_LICENSE("GPL");
diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
index be730c0a0352..d0eb0a2abf4d 100644
--- a/drivers/mmc/host/mmci.c
+++ b/drivers/mmc/host/mmci.c
@@ -16,6 +16,7 @@
16#include <linux/delay.h> 16#include <linux/delay.h>
17#include <linux/err.h> 17#include <linux/err.h>
18#include <linux/highmem.h> 18#include <linux/highmem.h>
19#include <linux/log2.h>
19#include <linux/mmc/host.h> 20#include <linux/mmc/host.h>
20#include <linux/amba/bus.h> 21#include <linux/amba/bus.h>
21#include <linux/clk.h> 22#include <linux/clk.h>
@@ -154,11 +155,11 @@ mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
154 } 155 }
155 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) { 156 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
156 if (status & MCI_DATACRCFAIL) 157 if (status & MCI_DATACRCFAIL)
157 data->error = MMC_ERR_BADCRC; 158 data->error = -EILSEQ;
158 else if (status & MCI_DATATIMEOUT) 159 else if (status & MCI_DATATIMEOUT)
159 data->error = MMC_ERR_TIMEOUT; 160 data->error = -ETIMEDOUT;
160 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN)) 161 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
161 data->error = MMC_ERR_FIFO; 162 data->error = -EIO;
162 status |= MCI_DATAEND; 163 status |= MCI_DATAEND;
163 164
164 /* 165 /*
@@ -193,12 +194,12 @@ mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
193 cmd->resp[3] = readl(base + MMCIRESPONSE3); 194 cmd->resp[3] = readl(base + MMCIRESPONSE3);
194 195
195 if (status & MCI_CMDTIMEOUT) { 196 if (status & MCI_CMDTIMEOUT) {
196 cmd->error = MMC_ERR_TIMEOUT; 197 cmd->error = -ETIMEDOUT;
197 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) { 198 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
198 cmd->error = MMC_ERR_BADCRC; 199 cmd->error = -EILSEQ;
199 } 200 }
200 201
201 if (!cmd->data || cmd->error != MMC_ERR_NONE) { 202 if (!cmd->data || cmd->error) {
202 if (host->data) 203 if (host->data)
203 mmci_stop_data(host); 204 mmci_stop_data(host);
204 mmci_request_end(host, cmd->mrq); 205 mmci_request_end(host, cmd->mrq);
@@ -391,6 +392,14 @@ static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
391 392
392 WARN_ON(host->mrq != NULL); 393 WARN_ON(host->mrq != NULL);
393 394
395 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
396 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
397 mmc_hostname(mmc), mrq->data->blksz);
398 mrq->cmd->error = -EINVAL;
399 mmc_request_done(mmc, mrq);
400 return;
401 }
402
394 spin_lock_irq(&host->lock); 403 spin_lock_irq(&host->lock);
395 404
396 host->mrq = mrq; 405 host->mrq = mrq;
diff --git a/drivers/mmc/host/omap.c b/drivers/mmc/host/omap.c
index 0cf97edc5f58..60a67dfcda6a 100644
--- a/drivers/mmc/host/omap.c
+++ b/drivers/mmc/host/omap.c
@@ -263,7 +263,7 @@ mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
263 enum dma_data_direction dma_data_dir; 263 enum dma_data_direction dma_data_dir;
264 264
265 BUG_ON(host->dma_ch < 0); 265 BUG_ON(host->dma_ch < 0);
266 if (data->error != MMC_ERR_NONE) 266 if (data->error)
267 omap_stop_dma(host->dma_ch); 267 omap_stop_dma(host->dma_ch);
268 /* Release DMA channel lazily */ 268 /* Release DMA channel lazily */
269 mod_timer(&host->dma_timer, jiffies + HZ); 269 mod_timer(&host->dma_timer, jiffies + HZ);
@@ -368,7 +368,7 @@ mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
368 } 368 }
369 } 369 }
370 370
371 if (host->data == NULL || cmd->error != MMC_ERR_NONE) { 371 if (host->data == NULL || cmd->error) {
372 host->mrq = NULL; 372 host->mrq = NULL;
373 clk_disable(host->fclk); 373 clk_disable(host->fclk);
374 mmc_request_done(host->mmc, cmd->mrq); 374 mmc_request_done(host->mmc, cmd->mrq);
@@ -475,14 +475,14 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
475 if (status & OMAP_MMC_STAT_DATA_TOUT) { 475 if (status & OMAP_MMC_STAT_DATA_TOUT) {
476 dev_dbg(mmc_dev(host->mmc), "data timeout\n"); 476 dev_dbg(mmc_dev(host->mmc), "data timeout\n");
477 if (host->data) { 477 if (host->data) {
478 host->data->error |= MMC_ERR_TIMEOUT; 478 host->data->error = -ETIMEDOUT;
479 transfer_error = 1; 479 transfer_error = 1;
480 } 480 }
481 } 481 }
482 482
483 if (status & OMAP_MMC_STAT_DATA_CRC) { 483 if (status & OMAP_MMC_STAT_DATA_CRC) {
484 if (host->data) { 484 if (host->data) {
485 host->data->error |= MMC_ERR_BADCRC; 485 host->data->error = -EILSEQ;
486 dev_dbg(mmc_dev(host->mmc), 486 dev_dbg(mmc_dev(host->mmc),
487 "data CRC error, bytes left %d\n", 487 "data CRC error, bytes left %d\n",
488 host->total_bytes_left); 488 host->total_bytes_left);
@@ -504,7 +504,7 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
504 dev_err(mmc_dev(host->mmc), 504 dev_err(mmc_dev(host->mmc),
505 "command timeout, CMD %d\n", 505 "command timeout, CMD %d\n",
506 host->cmd->opcode); 506 host->cmd->opcode);
507 host->cmd->error = MMC_ERR_TIMEOUT; 507 host->cmd->error = -ETIMEDOUT;
508 end_command = 1; 508 end_command = 1;
509 } 509 }
510 } 510 }
@@ -514,7 +514,7 @@ static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
514 dev_err(mmc_dev(host->mmc), 514 dev_err(mmc_dev(host->mmc),
515 "command CRC error (CMD%d, arg 0x%08x)\n", 515 "command CRC error (CMD%d, arg 0x%08x)\n",
516 host->cmd->opcode, host->cmd->arg); 516 host->cmd->opcode, host->cmd->arg);
517 host->cmd->error = MMC_ERR_BADCRC; 517 host->cmd->error = -EILSEQ;
518 end_command = 1; 518 end_command = 1;
519 } else 519 } else
520 dev_err(mmc_dev(host->mmc), 520 dev_err(mmc_dev(host->mmc),
diff --git a/drivers/mmc/host/pxamci.c b/drivers/mmc/host/pxamci.c
index ff960334b337..657901eecfce 100644
--- a/drivers/mmc/host/pxamci.c
+++ b/drivers/mmc/host/pxamci.c
@@ -142,6 +142,10 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
142 host->dma_dir); 142 host->dma_dir);
143 143
144 for (i = 0; i < host->dma_len; i++) { 144 for (i = 0; i < host->dma_len; i++) {
145 unsigned int length = sg_dma_len(&data->sg[i]);
146 host->sg_cpu[i].dcmd = dcmd | length;
147 if (length & 31 && !(data->flags & MMC_DATA_READ))
148 host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
145 if (data->flags & MMC_DATA_READ) { 149 if (data->flags & MMC_DATA_READ) {
146 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO; 150 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
147 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]); 151 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
@@ -149,7 +153,6 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
149 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]); 153 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
150 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO; 154 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
151 } 155 }
152 host->sg_cpu[i].dcmd = dcmd | sg_dma_len(&data->sg[i]);
153 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) * 156 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
154 sizeof(struct pxa_dma_desc); 157 sizeof(struct pxa_dma_desc);
155 } 158 }
@@ -226,7 +229,7 @@ static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
226 } 229 }
227 230
228 if (stat & STAT_TIME_OUT_RESPONSE) { 231 if (stat & STAT_TIME_OUT_RESPONSE) {
229 cmd->error = MMC_ERR_TIMEOUT; 232 cmd->error = -ETIMEDOUT;
230 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) { 233 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
231#ifdef CONFIG_PXA27x 234#ifdef CONFIG_PXA27x
232 /* 235 /*
@@ -239,11 +242,11 @@ static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
239 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode); 242 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
240 } else 243 } else
241#endif 244#endif
242 cmd->error = MMC_ERR_BADCRC; 245 cmd->error = -EILSEQ;
243 } 246 }
244 247
245 pxamci_disable_irq(host, END_CMD_RES); 248 pxamci_disable_irq(host, END_CMD_RES);
246 if (host->data && cmd->error == MMC_ERR_NONE) { 249 if (host->data && !cmd->error) {
247 pxamci_enable_irq(host, DATA_TRAN_DONE); 250 pxamci_enable_irq(host, DATA_TRAN_DONE);
248 } else { 251 } else {
249 pxamci_finish_request(host, host->mrq); 252 pxamci_finish_request(host, host->mrq);
@@ -264,9 +267,9 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
264 host->dma_dir); 267 host->dma_dir);
265 268
266 if (stat & STAT_READ_TIME_OUT) 269 if (stat & STAT_READ_TIME_OUT)
267 data->error = MMC_ERR_TIMEOUT; 270 data->error = -ETIMEDOUT;
268 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR)) 271 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
269 data->error = MMC_ERR_BADCRC; 272 data->error = -EILSEQ;
270 273
271 /* 274 /*
272 * There appears to be a hardware design bug here. There seems to 275 * There appears to be a hardware design bug here. There seems to
@@ -274,7 +277,7 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
274 * This means that if there was an error on any block, we mark all 277 * This means that if there was an error on any block, we mark all
275 * data blocks as being in error. 278 * data blocks as being in error.
276 */ 279 */
277 if (data->error == MMC_ERR_NONE) 280 if (!data->error)
278 data->bytes_xfered = data->blocks * data->blksz; 281 data->bytes_xfered = data->blocks * data->blksz;
279 else 282 else
280 data->bytes_xfered = 0; 283 data->bytes_xfered = 0;
@@ -284,7 +287,7 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
284 host->data = NULL; 287 host->data = NULL;
285 if (host->mrq->stop) { 288 if (host->mrq->stop) {
286 pxamci_stop_clock(host); 289 pxamci_stop_clock(host);
287 pxamci_start_cmd(host, host->mrq->stop, 0); 290 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
288 } else { 291 } else {
289 pxamci_finish_request(host, host->mrq); 292 pxamci_finish_request(host, host->mrq);
290 } 293 }
@@ -298,7 +301,7 @@ static irqreturn_t pxamci_irq(int irq, void *devid)
298 unsigned int ireg; 301 unsigned int ireg;
299 int handled = 0; 302 int handled = 0;
300 303
301 ireg = readl(host->base + MMC_I_REG); 304 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
302 305
303 if (ireg) { 306 if (ireg) {
304 unsigned stat = readl(host->base + MMC_STAT); 307 unsigned stat = readl(host->base + MMC_STAT);
@@ -309,6 +312,10 @@ static irqreturn_t pxamci_irq(int irq, void *devid)
309 handled |= pxamci_cmd_done(host, stat); 312 handled |= pxamci_cmd_done(host, stat);
310 if (ireg & DATA_TRAN_DONE) 313 if (ireg & DATA_TRAN_DONE)
311 handled |= pxamci_data_done(host, stat); 314 handled |= pxamci_data_done(host, stat);
315 if (ireg & SDIO_INT) {
316 mmc_signal_sdio_irq(host->mmc);
317 handled = 1;
318 }
312 } 319 }
313 320
314 return IRQ_RETVAL(handled); 321 return IRQ_RETVAL(handled);
@@ -382,20 +389,46 @@ static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
382 host->cmdat |= CMDAT_INIT; 389 host->cmdat |= CMDAT_INIT;
383 } 390 }
384 391
392 if (ios->bus_width == MMC_BUS_WIDTH_4)
393 host->cmdat |= CMDAT_SD_4DAT;
394 else
395 host->cmdat &= ~CMDAT_SD_4DAT;
396
385 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n", 397 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
386 host->clkrt, host->cmdat); 398 host->clkrt, host->cmdat);
387} 399}
388 400
401static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
402{
403 struct pxamci_host *pxa_host = mmc_priv(host);
404
405 if (enable)
406 pxamci_enable_irq(pxa_host, SDIO_INT);
407 else
408 pxamci_disable_irq(pxa_host, SDIO_INT);
409}
410
389static const struct mmc_host_ops pxamci_ops = { 411static const struct mmc_host_ops pxamci_ops = {
390 .request = pxamci_request, 412 .request = pxamci_request,
391 .get_ro = pxamci_get_ro, 413 .get_ro = pxamci_get_ro,
392 .set_ios = pxamci_set_ios, 414 .set_ios = pxamci_set_ios,
415 .enable_sdio_irq = pxamci_enable_sdio_irq,
393}; 416};
394 417
395static void pxamci_dma_irq(int dma, void *devid) 418static void pxamci_dma_irq(int dma, void *devid)
396{ 419{
397 printk(KERN_ERR "DMA%d: IRQ???\n", dma); 420 struct pxamci_host *host = devid;
398 DCSR(dma) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR; 421 int dcsr = DCSR(dma);
422 DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
423
424 if (dcsr & DCSR_ENDINTR) {
425 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
426 } else {
427 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
428 mmc_hostname(host->mmc), dma, dcsr);
429 host->data->error = -EIO;
430 pxamci_data_done(host, 0);
431 }
399} 432}
400 433
401static irqreturn_t pxamci_detect_irq(int irq, void *devid) 434static irqreturn_t pxamci_detect_irq(int irq, void *devid)
@@ -444,9 +477,9 @@ static int pxamci_probe(struct platform_device *pdev)
444 mmc->max_seg_size = PAGE_SIZE; 477 mmc->max_seg_size = PAGE_SIZE;
445 478
446 /* 479 /*
447 * Block length register is 10 bits. 480 * Block length register is only 10 bits before PXA27x.
448 */ 481 */
449 mmc->max_blk_size = 1023; 482 mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048;
450 483
451 /* 484 /*
452 * Block count register is 16 bits. 485 * Block count register is 16 bits.
@@ -460,6 +493,12 @@ static int pxamci_probe(struct platform_device *pdev)
460 mmc->ocr_avail = host->pdata ? 493 mmc->ocr_avail = host->pdata ?
461 host->pdata->ocr_mask : 494 host->pdata->ocr_mask :
462 MMC_VDD_32_33|MMC_VDD_33_34; 495 MMC_VDD_32_33|MMC_VDD_33_34;
496 mmc->caps = 0;
497 host->cmdat = 0;
498 if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
499 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
500 host->cmdat |= CMDAT_SDIO_INT_EN;
501 }
463 502
464 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL); 503 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
465 if (!host->sg_cpu) { 504 if (!host->sg_cpu) {
diff --git a/drivers/mmc/host/pxamci.h b/drivers/mmc/host/pxamci.h
index df17c281278a..3153e779d46a 100644
--- a/drivers/mmc/host/pxamci.h
+++ b/drivers/mmc/host/pxamci.h
@@ -25,6 +25,8 @@
25#define SPI_EN (1 << 0) 25#define SPI_EN (1 << 0)
26 26
27#define MMC_CMDAT 0x0010 27#define MMC_CMDAT 0x0010
28#define CMDAT_SDIO_INT_EN (1 << 11)
29#define CMDAT_SD_4DAT (1 << 8)
28#define CMDAT_DMAEN (1 << 7) 30#define CMDAT_DMAEN (1 << 7)
29#define CMDAT_INIT (1 << 6) 31#define CMDAT_INIT (1 << 6)
30#define CMDAT_BUSY (1 << 5) 32#define CMDAT_BUSY (1 << 5)
diff --git a/drivers/mmc/host/ricoh_mmc.c b/drivers/mmc/host/ricoh_mmc.c
new file mode 100644
index 000000000000..1e8704533bc5
--- /dev/null
+++ b/drivers/mmc/host/ricoh_mmc.c
@@ -0,0 +1,151 @@
1/*
2 * ricoh_mmc.c - Dummy driver to disable the Rioch MMC controller.
3 *
4 * Copyright (C) 2007 Philip Langdale, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12/*
13 * This is a conceptually ridiculous driver, but it is required by the way
14 * the Ricoh multi-function R5C832 works. This chip implements firewire
15 * and four different memory card controllers. Two of those controllers are
16 * an SDHCI controller and a proprietary MMC controller. The linux SDHCI
17 * driver supports MMC cards but the chip detects MMC cards in hardware
18 * and directs them to the MMC controller - so the SDHCI driver never sees
19 * them. To get around this, we must disable the useless MMC controller.
20 * At that point, the SDHCI controller will start seeing them. As a bonus,
21 * a detection event occurs immediately, even if the MMC card is already
22 * in the reader.
23 *
24 * The relevant registers live on the firewire function, so this is unavoidably
25 * ugly. Such is life.
26 */
27
28#include <linux/pci.h>
29
30#define DRIVER_NAME "ricoh-mmc"
31
32static const struct pci_device_id pci_ids[] __devinitdata = {
33 {
34 .vendor = PCI_VENDOR_ID_RICOH,
35 .device = PCI_DEVICE_ID_RICOH_R5C843,
36 .subvendor = PCI_ANY_ID,
37 .subdevice = PCI_ANY_ID,
38 },
39 { /* end: all zeroes */ },
40};
41
42MODULE_DEVICE_TABLE(pci, pci_ids);
43
44static int __devinit ricoh_mmc_probe(struct pci_dev *pdev,
45 const struct pci_device_id *ent)
46{
47 u8 rev;
48
49 struct pci_dev *fw_dev = NULL;
50
51 BUG_ON(pdev == NULL);
52 BUG_ON(ent == NULL);
53
54 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
55
56 printk(KERN_INFO DRIVER_NAME
57 ": Ricoh MMC controller found at %s [%04x:%04x] (rev %x)\n",
58 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
59 (int)rev);
60
61 while ((fw_dev = pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5C832, fw_dev))) {
62 if (PCI_SLOT(pdev->devfn) == PCI_SLOT(fw_dev->devfn) &&
63 pdev->bus == fw_dev->bus) {
64 u8 write_enable;
65 u8 disable;
66
67 pci_read_config_byte(fw_dev, 0xCB, &disable);
68 if (disable & 0x02) {
69 printk(KERN_INFO DRIVER_NAME
70 ": Controller already disabled. Nothing to do.\n");
71 return -ENODEV;
72 }
73
74 pci_read_config_byte(fw_dev, 0xCA, &write_enable);
75 pci_write_config_byte(fw_dev, 0xCA, 0x57);
76 pci_write_config_byte(fw_dev, 0xCB, disable | 0x02);
77 pci_write_config_byte(fw_dev, 0xCA, write_enable);
78
79 pci_set_drvdata(pdev, fw_dev);
80
81 printk(KERN_INFO DRIVER_NAME
82 ": Controller is now disabled.\n");
83
84 break;
85 }
86 }
87
88 if (pci_get_drvdata(pdev) == NULL) {
89 printk(KERN_WARNING DRIVER_NAME
90 ": Main firewire function not found. Cannot disable controller.\n");
91 return -ENODEV;
92 }
93
94 return 0;
95}
96
97static void __devexit ricoh_mmc_remove(struct pci_dev *pdev)
98{
99 u8 write_enable;
100 u8 disable;
101 struct pci_dev *fw_dev = NULL;
102
103 fw_dev = pci_get_drvdata(pdev);
104 BUG_ON(fw_dev == NULL);
105
106 pci_read_config_byte(fw_dev, 0xCA, &write_enable);
107 pci_read_config_byte(fw_dev, 0xCB, &disable);
108 pci_write_config_byte(fw_dev, 0xCA, 0x57);
109 pci_write_config_byte(fw_dev, 0xCB, disable & ~0x02);
110 pci_write_config_byte(fw_dev, 0xCA, write_enable);
111
112 printk(KERN_INFO DRIVER_NAME
113 ": Controller is now re-enabled.\n");
114
115 pci_set_drvdata(pdev, NULL);
116}
117
118static struct pci_driver ricoh_mmc_driver = {
119 .name = DRIVER_NAME,
120 .id_table = pci_ids,
121 .probe = ricoh_mmc_probe,
122 .remove = __devexit_p(ricoh_mmc_remove),
123};
124
125/*****************************************************************************\
126 * *
127 * Driver init/exit *
128 * *
129\*****************************************************************************/
130
131static int __init ricoh_mmc_drv_init(void)
132{
133 printk(KERN_INFO DRIVER_NAME
134 ": Ricoh MMC Controller disabling driver\n");
135 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Philip Langdale\n");
136
137 return pci_register_driver(&ricoh_mmc_driver);
138}
139
140static void __exit ricoh_mmc_drv_exit(void)
141{
142 pci_unregister_driver(&ricoh_mmc_driver);
143}
144
145module_init(ricoh_mmc_drv_init);
146module_exit(ricoh_mmc_drv_exit);
147
148MODULE_AUTHOR("Philip Langdale <philipl@alumni.utexas.net>");
149MODULE_DESCRIPTION("Ricoh MMC Controller disabling driver");
150MODULE_LICENSE("GPL");
151
diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index 20a7d89e01ba..b397121b947d 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -25,8 +25,6 @@
25#define DBG(f, x...) \ 25#define DBG(f, x...) \
26 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) 26 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
27 27
28static unsigned int debug_nodma = 0;
29static unsigned int debug_forcedma = 0;
30static unsigned int debug_quirks = 0; 28static unsigned int debug_quirks = 0;
31 29
32#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0) 30#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
@@ -35,6 +33,7 @@ static unsigned int debug_quirks = 0;
35#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2) 33#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
36#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3) 34#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
37#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4) 35#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
36#define SDHCI_QUIRK_BROKEN_DMA (1<<5)
38 37
39static const struct pci_device_id pci_ids[] __devinitdata = { 38static const struct pci_device_id pci_ids[] __devinitdata = {
40 { 39 {
@@ -68,7 +67,8 @@ static const struct pci_device_id pci_ids[] __devinitdata = {
68 .device = PCI_DEVICE_ID_ENE_CB712_SD, 67 .device = PCI_DEVICE_ID_ENE_CB712_SD,
69 .subvendor = PCI_ANY_ID, 68 .subvendor = PCI_ANY_ID,
70 .subdevice = PCI_ANY_ID, 69 .subdevice = PCI_ANY_ID,
71 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE, 70 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
71 SDHCI_QUIRK_BROKEN_DMA,
72 }, 72 },
73 73
74 { 74 {
@@ -76,7 +76,8 @@ static const struct pci_device_id pci_ids[] __devinitdata = {
76 .device = PCI_DEVICE_ID_ENE_CB712_SD_2, 76 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
77 .subvendor = PCI_ANY_ID, 77 .subvendor = PCI_ANY_ID,
78 .subdevice = PCI_ANY_ID, 78 .subdevice = PCI_ANY_ID,
79 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE, 79 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
80 SDHCI_QUIRK_BROKEN_DMA,
80 }, 81 },
81 82
82 { 83 {
@@ -132,7 +133,7 @@ static void sdhci_dumpregs(struct sdhci_host *host)
132 readb(host->ioaddr + SDHCI_POWER_CONTROL), 133 readb(host->ioaddr + SDHCI_POWER_CONTROL),
133 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL)); 134 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
134 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n", 135 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
135 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL), 136 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
136 readw(host->ioaddr + SDHCI_CLOCK_CONTROL)); 137 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
137 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", 138 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
138 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL), 139 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
@@ -481,16 +482,16 @@ static void sdhci_finish_data(struct sdhci_host *host)
481 * Controller doesn't count down when in single block mode. 482 * Controller doesn't count down when in single block mode.
482 */ 483 */
483 if (data->blocks == 1) 484 if (data->blocks == 1)
484 blocks = (data->error == MMC_ERR_NONE) ? 0 : 1; 485 blocks = (data->error == 0) ? 0 : 1;
485 else 486 else
486 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT); 487 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
487 data->bytes_xfered = data->blksz * (data->blocks - blocks); 488 data->bytes_xfered = data->blksz * (data->blocks - blocks);
488 489
489 if ((data->error == MMC_ERR_NONE) && blocks) { 490 if (!data->error && blocks) {
490 printk(KERN_ERR "%s: Controller signalled completion even " 491 printk(KERN_ERR "%s: Controller signalled completion even "
491 "though there were blocks left.\n", 492 "though there were blocks left.\n",
492 mmc_hostname(host->mmc)); 493 mmc_hostname(host->mmc));
493 data->error = MMC_ERR_FAILED; 494 data->error = -EIO;
494 } 495 }
495 496
496 if (data->stop) { 497 if (data->stop) {
@@ -498,7 +499,7 @@ static void sdhci_finish_data(struct sdhci_host *host)
498 * The controller needs a reset of internal state machines 499 * The controller needs a reset of internal state machines
499 * upon error conditions. 500 * upon error conditions.
500 */ 501 */
501 if (data->error != MMC_ERR_NONE) { 502 if (data->error) {
502 sdhci_reset(host, SDHCI_RESET_CMD); 503 sdhci_reset(host, SDHCI_RESET_CMD);
503 sdhci_reset(host, SDHCI_RESET_DATA); 504 sdhci_reset(host, SDHCI_RESET_DATA);
504 } 505 }
@@ -533,7 +534,7 @@ static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
533 printk(KERN_ERR "%s: Controller never released " 534 printk(KERN_ERR "%s: Controller never released "
534 "inhibit bit(s).\n", mmc_hostname(host->mmc)); 535 "inhibit bit(s).\n", mmc_hostname(host->mmc));
535 sdhci_dumpregs(host); 536 sdhci_dumpregs(host);
536 cmd->error = MMC_ERR_FAILED; 537 cmd->error = -EIO;
537 tasklet_schedule(&host->finish_tasklet); 538 tasklet_schedule(&host->finish_tasklet);
538 return; 539 return;
539 } 540 }
@@ -554,7 +555,7 @@ static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
554 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 555 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
555 printk(KERN_ERR "%s: Unsupported response type!\n", 556 printk(KERN_ERR "%s: Unsupported response type!\n",
556 mmc_hostname(host->mmc)); 557 mmc_hostname(host->mmc));
557 cmd->error = MMC_ERR_INVALID; 558 cmd->error = -EINVAL;
558 tasklet_schedule(&host->finish_tasklet); 559 tasklet_schedule(&host->finish_tasklet);
559 return; 560 return;
560 } 561 }
@@ -601,7 +602,7 @@ static void sdhci_finish_command(struct sdhci_host *host)
601 } 602 }
602 } 603 }
603 604
604 host->cmd->error = MMC_ERR_NONE; 605 host->cmd->error = 0;
605 606
606 if (host->data && host->data_early) 607 if (host->data && host->data_early)
607 sdhci_finish_data(host); 608 sdhci_finish_data(host);
@@ -722,7 +723,7 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
722 host->mrq = mrq; 723 host->mrq = mrq;
723 724
724 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { 725 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
725 host->mrq->cmd->error = MMC_ERR_TIMEOUT; 726 host->mrq->cmd->error = -ENOMEDIUM;
726 tasklet_schedule(&host->finish_tasklet); 727 tasklet_schedule(&host->finish_tasklet);
727 } else 728 } else
728 sdhci_send_command(host, mrq->cmd); 729 sdhci_send_command(host, mrq->cmd);
@@ -800,10 +801,35 @@ static int sdhci_get_ro(struct mmc_host *mmc)
800 return !(present & SDHCI_WRITE_PROTECT); 801 return !(present & SDHCI_WRITE_PROTECT);
801} 802}
802 803
804static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
805{
806 struct sdhci_host *host;
807 unsigned long flags;
808 u32 ier;
809
810 host = mmc_priv(mmc);
811
812 spin_lock_irqsave(&host->lock, flags);
813
814 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
815
816 ier &= ~SDHCI_INT_CARD_INT;
817 if (enable)
818 ier |= SDHCI_INT_CARD_INT;
819
820 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
821 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
822
823 mmiowb();
824
825 spin_unlock_irqrestore(&host->lock, flags);
826}
827
803static const struct mmc_host_ops sdhci_ops = { 828static const struct mmc_host_ops sdhci_ops = {
804 .request = sdhci_request, 829 .request = sdhci_request,
805 .set_ios = sdhci_set_ios, 830 .set_ios = sdhci_set_ios,
806 .get_ro = sdhci_get_ro, 831 .get_ro = sdhci_get_ro,
832 .enable_sdio_irq = sdhci_enable_sdio_irq,
807}; 833};
808 834
809/*****************************************************************************\ 835/*****************************************************************************\
@@ -831,7 +857,7 @@ static void sdhci_tasklet_card(unsigned long param)
831 sdhci_reset(host, SDHCI_RESET_CMD); 857 sdhci_reset(host, SDHCI_RESET_CMD);
832 sdhci_reset(host, SDHCI_RESET_DATA); 858 sdhci_reset(host, SDHCI_RESET_DATA);
833 859
834 host->mrq->cmd->error = MMC_ERR_FAILED; 860 host->mrq->cmd->error = -ENOMEDIUM;
835 tasklet_schedule(&host->finish_tasklet); 861 tasklet_schedule(&host->finish_tasklet);
836 } 862 }
837 } 863 }
@@ -859,9 +885,9 @@ static void sdhci_tasklet_finish(unsigned long param)
859 * The controller needs a reset of internal state machines 885 * The controller needs a reset of internal state machines
860 * upon error conditions. 886 * upon error conditions.
861 */ 887 */
862 if ((mrq->cmd->error != MMC_ERR_NONE) || 888 if (mrq->cmd->error ||
863 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) || 889 (mrq->data && (mrq->data->error ||
864 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) { 890 (mrq->data->stop && mrq->data->stop->error)))) {
865 891
866 /* Some controllers need this kick or reset won't work here */ 892 /* Some controllers need this kick or reset won't work here */
867 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) { 893 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
@@ -906,13 +932,13 @@ static void sdhci_timeout_timer(unsigned long data)
906 sdhci_dumpregs(host); 932 sdhci_dumpregs(host);
907 933
908 if (host->data) { 934 if (host->data) {
909 host->data->error = MMC_ERR_TIMEOUT; 935 host->data->error = -ETIMEDOUT;
910 sdhci_finish_data(host); 936 sdhci_finish_data(host);
911 } else { 937 } else {
912 if (host->cmd) 938 if (host->cmd)
913 host->cmd->error = MMC_ERR_TIMEOUT; 939 host->cmd->error = -ETIMEDOUT;
914 else 940 else
915 host->mrq->cmd->error = MMC_ERR_TIMEOUT; 941 host->mrq->cmd->error = -ETIMEDOUT;
916 942
917 tasklet_schedule(&host->finish_tasklet); 943 tasklet_schedule(&host->finish_tasklet);
918 } 944 }
@@ -941,13 +967,12 @@ static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
941 } 967 }
942 968
943 if (intmask & SDHCI_INT_TIMEOUT) 969 if (intmask & SDHCI_INT_TIMEOUT)
944 host->cmd->error = MMC_ERR_TIMEOUT; 970 host->cmd->error = -ETIMEDOUT;
945 else if (intmask & SDHCI_INT_CRC) 971 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
946 host->cmd->error = MMC_ERR_BADCRC; 972 SDHCI_INT_INDEX))
947 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 973 host->cmd->error = -EILSEQ;
948 host->cmd->error = MMC_ERR_FAILED;
949 974
950 if (host->cmd->error != MMC_ERR_NONE) 975 if (host->cmd->error)
951 tasklet_schedule(&host->finish_tasklet); 976 tasklet_schedule(&host->finish_tasklet);
952 else if (intmask & SDHCI_INT_RESPONSE) 977 else if (intmask & SDHCI_INT_RESPONSE)
953 sdhci_finish_command(host); 978 sdhci_finish_command(host);
@@ -974,13 +999,11 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
974 } 999 }
975 1000
976 if (intmask & SDHCI_INT_DATA_TIMEOUT) 1001 if (intmask & SDHCI_INT_DATA_TIMEOUT)
977 host->data->error = MMC_ERR_TIMEOUT; 1002 host->data->error = -ETIMEDOUT;
978 else if (intmask & SDHCI_INT_DATA_CRC) 1003 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
979 host->data->error = MMC_ERR_BADCRC; 1004 host->data->error = -EILSEQ;
980 else if (intmask & SDHCI_INT_DATA_END_BIT)
981 host->data->error = MMC_ERR_FAILED;
982 1005
983 if (host->data->error != MMC_ERR_NONE) 1006 if (host->data->error)
984 sdhci_finish_data(host); 1007 sdhci_finish_data(host);
985 else { 1008 else {
986 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) 1009 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
@@ -1015,6 +1038,7 @@ static irqreturn_t sdhci_irq(int irq, void *dev_id)
1015 irqreturn_t result; 1038 irqreturn_t result;
1016 struct sdhci_host* host = dev_id; 1039 struct sdhci_host* host = dev_id;
1017 u32 intmask; 1040 u32 intmask;
1041 int cardint = 0;
1018 1042
1019 spin_lock(&host->lock); 1043 spin_lock(&host->lock);
1020 1044
@@ -1059,6 +1083,11 @@ static irqreturn_t sdhci_irq(int irq, void *dev_id)
1059 1083
1060 intmask &= ~SDHCI_INT_BUS_POWER; 1084 intmask &= ~SDHCI_INT_BUS_POWER;
1061 1085
1086 if (intmask & SDHCI_INT_CARD_INT)
1087 cardint = 1;
1088
1089 intmask &= ~SDHCI_INT_CARD_INT;
1090
1062 if (intmask) { 1091 if (intmask) {
1063 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n", 1092 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
1064 mmc_hostname(host->mmc), intmask); 1093 mmc_hostname(host->mmc), intmask);
@@ -1073,6 +1102,12 @@ static irqreturn_t sdhci_irq(int irq, void *dev_id)
1073out: 1102out:
1074 spin_unlock(&host->lock); 1103 spin_unlock(&host->lock);
1075 1104
1105 /*
1106 * We have to delay this as it calls back into the driver.
1107 */
1108 if (cardint)
1109 mmc_signal_sdio_irq(host->mmc);
1110
1076 return result; 1111 return result;
1077} 1112}
1078 1113
@@ -1258,20 +1293,26 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1258 1293
1259 caps = readl(host->ioaddr + SDHCI_CAPABILITIES); 1294 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1260 1295
1261 if (debug_nodma) 1296 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1262 DBG("DMA forced off\n");
1263 else if (debug_forcedma) {
1264 DBG("DMA forced on\n");
1265 host->flags |= SDHCI_USE_DMA;
1266 } else if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1267 host->flags |= SDHCI_USE_DMA; 1297 host->flags |= SDHCI_USE_DMA;
1268 else if ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA)
1269 DBG("Controller doesn't have DMA interface\n");
1270 else if (!(caps & SDHCI_CAN_DO_DMA)) 1298 else if (!(caps & SDHCI_CAN_DO_DMA))
1271 DBG("Controller doesn't have DMA capability\n"); 1299 DBG("Controller doesn't have DMA capability\n");
1272 else 1300 else
1273 host->flags |= SDHCI_USE_DMA; 1301 host->flags |= SDHCI_USE_DMA;
1274 1302
1303 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1304 (host->flags & SDHCI_USE_DMA)) {
1305 DBG("Disabling DMA as it is marked broken");
1306 host->flags &= ~SDHCI_USE_DMA;
1307 }
1308
1309 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1310 (host->flags & SDHCI_USE_DMA)) {
1311 printk(KERN_WARNING "%s: Will use DMA "
1312 "mode even though HW doesn't fully "
1313 "claim to support it.\n", host->slot_descr);
1314 }
1315
1275 if (host->flags & SDHCI_USE_DMA) { 1316 if (host->flags & SDHCI_USE_DMA) {
1276 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { 1317 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1277 printk(KERN_WARNING "%s: No suitable DMA available. " 1318 printk(KERN_WARNING "%s: No suitable DMA available. "
@@ -1312,7 +1353,7 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1312 mmc->ops = &sdhci_ops; 1353 mmc->ops = &sdhci_ops;
1313 mmc->f_min = host->max_clk / 256; 1354 mmc->f_min = host->max_clk / 256;
1314 mmc->f_max = host->max_clk; 1355 mmc->f_max = host->max_clk;
1315 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK; 1356 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
1316 1357
1317 if (caps & SDHCI_CAN_DO_HISPD) 1358 if (caps & SDHCI_CAN_DO_HISPD)
1318 mmc->caps |= MMC_CAP_SD_HIGHSPEED; 1359 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
@@ -1565,14 +1606,10 @@ static void __exit sdhci_drv_exit(void)
1565module_init(sdhci_drv_init); 1606module_init(sdhci_drv_init);
1566module_exit(sdhci_drv_exit); 1607module_exit(sdhci_drv_exit);
1567 1608
1568module_param(debug_nodma, uint, 0444);
1569module_param(debug_forcedma, uint, 0444);
1570module_param(debug_quirks, uint, 0444); 1609module_param(debug_quirks, uint, 0444);
1571 1610
1572MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); 1611MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1573MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver"); 1612MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1574MODULE_LICENSE("GPL"); 1613MODULE_LICENSE("GPL");
1575 1614
1576MODULE_PARM_DESC(debug_nodma, "Forcefully disable DMA transfers. (default 0)");
1577MODULE_PARM_DESC(debug_forcedma, "Forcefully enable DMA transfers. (default 0)");
1578MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); 1615MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
diff --git a/drivers/mmc/host/sdhci.h b/drivers/mmc/host/sdhci.h
index e28987d6d2eb..05195ea900f4 100644
--- a/drivers/mmc/host/sdhci.h
+++ b/drivers/mmc/host/sdhci.h
@@ -81,7 +81,7 @@
81 81
82#define SDHCI_BLOCK_GAP_CONTROL 0x2A 82#define SDHCI_BLOCK_GAP_CONTROL 0x2A
83 83
84#define SDHCI_WALK_UP_CONTROL 0x2B 84#define SDHCI_WAKE_UP_CONTROL 0x2B
85 85
86#define SDHCI_CLOCK_CONTROL 0x2C 86#define SDHCI_CLOCK_CONTROL 0x2C
87#define SDHCI_DIVIDER_SHIFT 8 87#define SDHCI_DIVIDER_SHIFT 8
diff --git a/drivers/mmc/host/tifm_sd.c b/drivers/mmc/host/tifm_sd.c
index 8b736e968447..9b904795eb77 100644
--- a/drivers/mmc/host/tifm_sd.c
+++ b/drivers/mmc/host/tifm_sd.c
@@ -16,6 +16,7 @@
16#include <linux/mmc/host.h> 16#include <linux/mmc/host.h>
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/scatterlist.h> 18#include <linux/scatterlist.h>
19#include <linux/log2.h>
19#include <asm/io.h> 20#include <asm/io.h>
20 21
21#define DRIVER_NAME "tifm_sd" 22#define DRIVER_NAME "tifm_sd"
@@ -404,14 +405,14 @@ static void tifm_sd_check_status(struct tifm_sd *host)
404 struct tifm_dev *sock = host->dev; 405 struct tifm_dev *sock = host->dev;
405 struct mmc_command *cmd = host->req->cmd; 406 struct mmc_command *cmd = host->req->cmd;
406 407
407 if (cmd->error != MMC_ERR_NONE) 408 if (cmd->error)
408 goto finish_request; 409 goto finish_request;
409 410
410 if (!(host->cmd_flags & CMD_READY)) 411 if (!(host->cmd_flags & CMD_READY))
411 return; 412 return;
412 413
413 if (cmd->data) { 414 if (cmd->data) {
414 if (cmd->data->error != MMC_ERR_NONE) { 415 if (cmd->data->error) {
415 if ((host->cmd_flags & SCMD_ACTIVE) 416 if ((host->cmd_flags & SCMD_ACTIVE)
416 && !(host->cmd_flags & SCMD_READY)) 417 && !(host->cmd_flags & SCMD_READY))
417 return; 418 return;
@@ -504,7 +505,7 @@ static void tifm_sd_card_event(struct tifm_dev *sock)
504{ 505{
505 struct tifm_sd *host; 506 struct tifm_sd *host;
506 unsigned int host_status = 0; 507 unsigned int host_status = 0;
507 int cmd_error = MMC_ERR_NONE; 508 int cmd_error = 0;
508 struct mmc_command *cmd = NULL; 509 struct mmc_command *cmd = NULL;
509 unsigned long flags; 510 unsigned long flags;
510 511
@@ -521,15 +522,15 @@ static void tifm_sd_card_event(struct tifm_dev *sock)
521 writel(host_status & TIFM_MMCSD_ERRMASK, 522 writel(host_status & TIFM_MMCSD_ERRMASK,
522 sock->addr + SOCK_MMCSD_STATUS); 523 sock->addr + SOCK_MMCSD_STATUS);
523 if (host_status & TIFM_MMCSD_CTO) 524 if (host_status & TIFM_MMCSD_CTO)
524 cmd_error = MMC_ERR_TIMEOUT; 525 cmd_error = -ETIMEDOUT;
525 else if (host_status & TIFM_MMCSD_CCRC) 526 else if (host_status & TIFM_MMCSD_CCRC)
526 cmd_error = MMC_ERR_BADCRC; 527 cmd_error = -EILSEQ;
527 528
528 if (cmd->data) { 529 if (cmd->data) {
529 if (host_status & TIFM_MMCSD_DTO) 530 if (host_status & TIFM_MMCSD_DTO)
530 cmd->data->error = MMC_ERR_TIMEOUT; 531 cmd->data->error = -ETIMEDOUT;
531 else if (host_status & TIFM_MMCSD_DCRC) 532 else if (host_status & TIFM_MMCSD_DCRC)
532 cmd->data->error = MMC_ERR_BADCRC; 533 cmd->data->error = -EILSEQ;
533 } 534 }
534 535
535 writel(TIFM_FIFO_INT_SETALL, 536 writel(TIFM_FIFO_INT_SETALL,
@@ -626,14 +627,21 @@ static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
626 627
627 spin_lock_irqsave(&sock->lock, flags); 628 spin_lock_irqsave(&sock->lock, flags);
628 if (host->eject) { 629 if (host->eject) {
629 spin_unlock_irqrestore(&sock->lock, flags); 630 mrq->cmd->error = -ENOMEDIUM;
630 goto err_out; 631 goto err_out;
631 } 632 }
632 633
633 if (host->req) { 634 if (host->req) {
634 printk(KERN_ERR "%s : unfinished request detected\n", 635 printk(KERN_ERR "%s : unfinished request detected\n",
635 sock->dev.bus_id); 636 sock->dev.bus_id);
636 spin_unlock_irqrestore(&sock->lock, flags); 637 mrq->cmd->error = -ETIMEDOUT;
638 goto err_out;
639 }
640
641 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
642 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
643 sock->dev.bus_id, mrq->data->blksz);
644 mrq->cmd->error = -EINVAL;
637 goto err_out; 645 goto err_out;
638 } 646 }
639 647
@@ -722,7 +730,7 @@ static void tifm_sd_request(struct mmc_host *mmc, struct mmc_request *mrq)
722 return; 730 return;
723 731
724err_out: 732err_out:
725 mrq->cmd->error = MMC_ERR_TIMEOUT; 733 spin_unlock_irqrestore(&sock->lock, flags);
726 mmc_request_done(mmc, mrq); 734 mmc_request_done(mmc, mrq);
727} 735}
728 736
@@ -1012,9 +1020,9 @@ static void tifm_sd_remove(struct tifm_dev *sock)
1012 writel(TIFM_FIFO_INT_SETALL, 1020 writel(TIFM_FIFO_INT_SETALL,
1013 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR); 1021 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
1014 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET); 1022 writel(0, sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
1015 host->req->cmd->error = MMC_ERR_TIMEOUT; 1023 host->req->cmd->error = -ENOMEDIUM;
1016 if (host->req->stop) 1024 if (host->req->stop)
1017 host->req->stop->error = MMC_ERR_TIMEOUT; 1025 host->req->stop->error = -ENOMEDIUM;
1018 tasklet_schedule(&host->finish_tasklet); 1026 tasklet_schedule(&host->finish_tasklet);
1019 } 1027 }
1020 spin_unlock_irqrestore(&sock->lock, flags); 1028 spin_unlock_irqrestore(&sock->lock, flags);
diff --git a/drivers/mmc/host/wbsd.c b/drivers/mmc/host/wbsd.c
index 9bf2a877113b..80db11c05f2a 100644
--- a/drivers/mmc/host/wbsd.c
+++ b/drivers/mmc/host/wbsd.c
@@ -317,7 +317,7 @@ static inline void wbsd_get_short_reply(struct wbsd_host *host,
317 * Correct response type? 317 * Correct response type?
318 */ 318 */
319 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) { 319 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_SHORT) {
320 cmd->error = MMC_ERR_INVALID; 320 cmd->error = -EILSEQ;
321 return; 321 return;
322 } 322 }
323 323
@@ -337,7 +337,7 @@ static inline void wbsd_get_long_reply(struct wbsd_host *host,
337 * Correct response type? 337 * Correct response type?
338 */ 338 */
339 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) { 339 if (wbsd_read_index(host, WBSD_IDX_RSPLEN) != WBSD_RSP_LONG) {
340 cmd->error = MMC_ERR_INVALID; 340 cmd->error = -EILSEQ;
341 return; 341 return;
342 } 342 }
343 343
@@ -372,7 +372,7 @@ static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
372 for (i = 3; i >= 0; i--) 372 for (i = 3; i >= 0; i--)
373 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR); 373 outb((cmd->arg >> (i * 8)) & 0xff, host->base + WBSD_CMDR);
374 374
375 cmd->error = MMC_ERR_NONE; 375 cmd->error = 0;
376 376
377 /* 377 /*
378 * Wait for the request to complete. 378 * Wait for the request to complete.
@@ -392,13 +392,13 @@ static void wbsd_send_command(struct wbsd_host *host, struct mmc_command *cmd)
392 392
393 /* Card removed? */ 393 /* Card removed? */
394 if (isr & WBSD_INT_CARD) 394 if (isr & WBSD_INT_CARD)
395 cmd->error = MMC_ERR_TIMEOUT; 395 cmd->error = -ENOMEDIUM;
396 /* Timeout? */ 396 /* Timeout? */
397 else if (isr & WBSD_INT_TIMEOUT) 397 else if (isr & WBSD_INT_TIMEOUT)
398 cmd->error = MMC_ERR_TIMEOUT; 398 cmd->error = -ETIMEDOUT;
399 /* CRC? */ 399 /* CRC? */
400 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC)) 400 else if ((cmd->flags & MMC_RSP_CRC) && (isr & WBSD_INT_CRC))
401 cmd->error = MMC_ERR_BADCRC; 401 cmd->error = -EILSEQ;
402 /* All ok */ 402 /* All ok */
403 else { 403 else {
404 if (cmd->flags & MMC_RSP_136) 404 if (cmd->flags & MMC_RSP_136)
@@ -585,7 +585,7 @@ static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
585 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH); 585 ((blksize >> 4) & 0xF0) | WBSD_DATA_WIDTH);
586 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF); 586 wbsd_write_index(host, WBSD_IDX_PBSLSB, blksize & 0xFF);
587 } else { 587 } else {
588 data->error = MMC_ERR_INVALID; 588 data->error = -EINVAL;
589 return; 589 return;
590 } 590 }
591 591
@@ -607,7 +607,7 @@ static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
607 */ 607 */
608 BUG_ON(size > 0x10000); 608 BUG_ON(size > 0x10000);
609 if (size > 0x10000) { 609 if (size > 0x10000) {
610 data->error = MMC_ERR_INVALID; 610 data->error = -EINVAL;
611 return; 611 return;
612 } 612 }
613 613
@@ -669,7 +669,7 @@ static void wbsd_prepare_data(struct wbsd_host *host, struct mmc_data *data)
669 } 669 }
670 } 670 }
671 671
672 data->error = MMC_ERR_NONE; 672 data->error = 0;
673} 673}
674 674
675static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data) 675static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
@@ -724,8 +724,8 @@ static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
724 "%d bytes left.\n", 724 "%d bytes left.\n",
725 mmc_hostname(host->mmc), count); 725 mmc_hostname(host->mmc), count);
726 726
727 if (data->error == MMC_ERR_NONE) 727 if (!data->error)
728 data->error = MMC_ERR_FAILED; 728 data->error = -EIO;
729 } else { 729 } else {
730 /* 730 /*
731 * Transfer data from DMA buffer to 731 * Transfer data from DMA buffer to
@@ -735,7 +735,7 @@ static void wbsd_finish_data(struct wbsd_host *host, struct mmc_data *data)
735 wbsd_dma_to_sg(host, data); 735 wbsd_dma_to_sg(host, data);
736 } 736 }
737 737
738 if (data->error != MMC_ERR_NONE) { 738 if (data->error) {
739 if (data->bytes_xfered) 739 if (data->bytes_xfered)
740 data->bytes_xfered -= data->blksz; 740 data->bytes_xfered -= data->blksz;
741 } 741 }
@@ -767,11 +767,10 @@ static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
767 host->mrq = mrq; 767 host->mrq = mrq;
768 768
769 /* 769 /*
770 * If there is no card in the slot then 770 * Check that there is actually a card in the slot.
771 * timeout immediatly.
772 */ 771 */
773 if (!(host->flags & WBSD_FCARD_PRESENT)) { 772 if (!(host->flags & WBSD_FCARD_PRESENT)) {
774 cmd->error = MMC_ERR_TIMEOUT; 773 cmd->error = -ENOMEDIUM;
775 goto done; 774 goto done;
776 } 775 }
777 776
@@ -807,7 +806,7 @@ static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
807 "supported by this controller.\n", 806 "supported by this controller.\n",
808 mmc_hostname(host->mmc), cmd->opcode); 807 mmc_hostname(host->mmc), cmd->opcode);
809#endif 808#endif
810 cmd->error = MMC_ERR_INVALID; 809 cmd->error = -EINVAL;
811 810
812 goto done; 811 goto done;
813 }; 812 };
@@ -819,7 +818,7 @@ static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
819 if (cmd->data) { 818 if (cmd->data) {
820 wbsd_prepare_data(host, cmd->data); 819 wbsd_prepare_data(host, cmd->data);
821 820
822 if (cmd->data->error != MMC_ERR_NONE) 821 if (cmd->data->error)
823 goto done; 822 goto done;
824 } 823 }
825 824
@@ -830,7 +829,7 @@ static void wbsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
830 * will be finished after the data has 829 * will be finished after the data has
831 * transfered. 830 * transfered.
832 */ 831 */
833 if (cmd->data && (cmd->error == MMC_ERR_NONE)) { 832 if (cmd->data && !cmd->error) {
834 /* 833 /*
835 * Dirty fix for hardware bug. 834 * Dirty fix for hardware bug.
836 */ 835 */
@@ -1033,7 +1032,7 @@ static void wbsd_tasklet_card(unsigned long param)
1033 mmc_hostname(host->mmc)); 1032 mmc_hostname(host->mmc));
1034 wbsd_reset(host); 1033 wbsd_reset(host);
1035 1034
1036 host->mrq->cmd->error = MMC_ERR_FAILED; 1035 host->mrq->cmd->error = -ENOMEDIUM;
1037 tasklet_schedule(&host->finish_tasklet); 1036 tasklet_schedule(&host->finish_tasklet);
1038 } 1037 }
1039 1038
@@ -1097,7 +1096,7 @@ static void wbsd_tasklet_crc(unsigned long param)
1097 1096
1098 DBGF("CRC error\n"); 1097 DBGF("CRC error\n");
1099 1098
1100 data->error = MMC_ERR_BADCRC; 1099 data->error = -EILSEQ;
1101 1100
1102 tasklet_schedule(&host->finish_tasklet); 1101 tasklet_schedule(&host->finish_tasklet);
1103 1102
@@ -1121,7 +1120,7 @@ static void wbsd_tasklet_timeout(unsigned long param)
1121 1120
1122 DBGF("Timeout\n"); 1121 DBGF("Timeout\n");
1123 1122
1124 data->error = MMC_ERR_TIMEOUT; 1123 data->error = -ETIMEDOUT;
1125 1124
1126 tasklet_schedule(&host->finish_tasklet); 1125 tasklet_schedule(&host->finish_tasklet);
1127 1126
@@ -1220,7 +1219,7 @@ static int __devinit wbsd_alloc_mmc(struct device *dev)
1220 mmc->f_min = 375000; 1219 mmc->f_min = 375000;
1221 mmc->f_max = 24000000; 1220 mmc->f_max = 24000000;
1222 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 1221 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1223 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK; 1222 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE;
1224 1223
1225 spin_lock_init(&host->lock); 1224 spin_lock_init(&host->lock);
1226 1225
diff --git a/include/asm-arm/arch-imx/mmc.h b/include/asm-arm/arch-imx/mmc.h
index 84c726934ace..4712f354dcca 100644
--- a/include/asm-arm/arch-imx/mmc.h
+++ b/include/asm-arm/arch-imx/mmc.h
@@ -3,8 +3,11 @@
3 3
4#include <linux/mmc/host.h> 4#include <linux/mmc/host.h>
5 5
6struct device;
7
6struct imxmmc_platform_data { 8struct imxmmc_platform_data {
7 int (*card_present)(void); 9 int (*card_present)(struct device *);
10 int (*get_ro)(struct device *);
8}; 11};
9 12
10extern void imx_set_mmc_info(struct imxmmc_platform_data *info); 13extern void imx_set_mmc_info(struct imxmmc_platform_data *info);
diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
index badf702fcff4..0d508ac17d64 100644
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -55,7 +55,28 @@ struct sd_switch_caps {
55 unsigned int hs_max_dtr; 55 unsigned int hs_max_dtr;
56}; 56};
57 57
58struct sdio_cccr {
59 unsigned int sdio_vsn;
60 unsigned int sd_vsn;
61 unsigned int multi_block:1,
62 low_speed:1,
63 wide_bus:1,
64 high_power:1,
65 high_speed:1;
66};
67
68struct sdio_cis {
69 unsigned short vendor;
70 unsigned short device;
71 unsigned short blksize;
72 unsigned int max_dtr;
73};
74
58struct mmc_host; 75struct mmc_host;
76struct sdio_func;
77struct sdio_func_tuple;
78
79#define SDIO_MAX_FUNCS 7
59 80
60/* 81/*
61 * MMC device 82 * MMC device
@@ -67,11 +88,13 @@ struct mmc_card {
67 unsigned int type; /* card type */ 88 unsigned int type; /* card type */
68#define MMC_TYPE_MMC 0 /* MMC card */ 89#define MMC_TYPE_MMC 0 /* MMC card */
69#define MMC_TYPE_SD 1 /* SD card */ 90#define MMC_TYPE_SD 1 /* SD card */
91#define MMC_TYPE_SDIO 2 /* SDIO card */
70 unsigned int state; /* (our) card state */ 92 unsigned int state; /* (our) card state */
71#define MMC_STATE_PRESENT (1<<0) /* present in sysfs */ 93#define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
72#define MMC_STATE_READONLY (1<<1) /* card is read-only */ 94#define MMC_STATE_READONLY (1<<1) /* card is read-only */
73#define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */ 95#define MMC_STATE_HIGHSPEED (1<<2) /* card is in high speed mode */
74#define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */ 96#define MMC_STATE_BLOCKADDR (1<<3) /* card uses block-addressing */
97
75 u32 raw_cid[4]; /* raw card CID */ 98 u32 raw_cid[4]; /* raw card CID */
76 u32 raw_csd[4]; /* raw card CSD */ 99 u32 raw_csd[4]; /* raw card CSD */
77 u32 raw_scr[2]; /* raw card SCR */ 100 u32 raw_scr[2]; /* raw card SCR */
@@ -80,10 +103,19 @@ struct mmc_card {
80 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */ 103 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
81 struct sd_scr scr; /* extra SD information */ 104 struct sd_scr scr; /* extra SD information */
82 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */ 105 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
106
107 unsigned int sdio_funcs; /* number of SDIO functions */
108 struct sdio_cccr cccr; /* common card info */
109 struct sdio_cis cis; /* common tuple info */
110 struct sdio_func *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
111 unsigned num_info; /* number of info strings */
112 const char **info; /* info strings */
113 struct sdio_func_tuple *tuples; /* unknown common tuples */
83}; 114};
84 115
85#define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC) 116#define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
86#define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD) 117#define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
118#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
87 119
88#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT) 120#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
89#define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY) 121#define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)
diff --git a/include/linux/mmc/core.h b/include/linux/mmc/core.h
index 63a80ea61124..d0c3abed74c2 100644
--- a/include/linux/mmc/core.h
+++ b/include/linux/mmc/core.h
@@ -25,14 +25,20 @@ struct mmc_command {
25#define MMC_RSP_CRC (1 << 2) /* expect valid crc */ 25#define MMC_RSP_CRC (1 << 2) /* expect valid crc */
26#define MMC_RSP_BUSY (1 << 3) /* card may send busy */ 26#define MMC_RSP_BUSY (1 << 3) /* card may send busy */
27#define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */ 27#define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
28#define MMC_CMD_MASK (3 << 5) /* command type */ 28
29#define MMC_CMD_MASK (3 << 5) /* non-SPI command type */
29#define MMC_CMD_AC (0 << 5) 30#define MMC_CMD_AC (0 << 5)
30#define MMC_CMD_ADTC (1 << 5) 31#define MMC_CMD_ADTC (1 << 5)
31#define MMC_CMD_BC (2 << 5) 32#define MMC_CMD_BC (2 << 5)
32#define MMC_CMD_BCR (3 << 5) 33#define MMC_CMD_BCR (3 << 5)
33 34
35#define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */
36#define MMC_RSP_SPI_S2 (1 << 8) /* second byte */
37#define MMC_RSP_SPI_B4 (1 << 9) /* four data bytes */
38#define MMC_RSP_SPI_BUSY (1 << 10) /* card may send busy */
39
34/* 40/*
35 * These are the response types, and correspond to valid bit 41 * These are the native response types, and correspond to valid bit
36 * patterns of the above flags. One additional valid pattern 42 * patterns of the above flags. One additional valid pattern
37 * is all zeros, which means we don't expect a response. 43 * is all zeros, which means we don't expect a response.
38 */ 44 */
@@ -41,12 +47,30 @@ struct mmc_command {
41#define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY) 47#define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
42#define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC) 48#define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
43#define MMC_RSP_R3 (MMC_RSP_PRESENT) 49#define MMC_RSP_R3 (MMC_RSP_PRESENT)
50#define MMC_RSP_R4 (MMC_RSP_PRESENT)
51#define MMC_RSP_R5 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
44#define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) 52#define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
45#define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) 53#define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
46 54
47#define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE)) 55#define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
48 56
49/* 57/*
58 * These are the SPI response types for MMC, SD, and SDIO cards.
59 * Commands return R1, with maybe more info. Zero is an error type;
60 * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
61 */
62#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
63#define MMC_RSP_SPI_R1B (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
64#define MMC_RSP_SPI_R2 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
65#define MMC_RSP_SPI_R3 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
66#define MMC_RSP_SPI_R4 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
67#define MMC_RSP_SPI_R5 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
68#define MMC_RSP_SPI_R7 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
69
70#define mmc_spi_resp_type(cmd) ((cmd)->flags & \
71 (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
72
73/*
50 * These are the command types. 74 * These are the command types.
51 */ 75 */
52#define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK) 76#define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK)
@@ -54,12 +78,19 @@ struct mmc_command {
54 unsigned int retries; /* max number of retries */ 78 unsigned int retries; /* max number of retries */
55 unsigned int error; /* command error */ 79 unsigned int error; /* command error */
56 80
57#define MMC_ERR_NONE 0 81/*
58#define MMC_ERR_TIMEOUT 1 82 * Standard errno values are used for errors, but some have specific
59#define MMC_ERR_BADCRC 2 83 * meaning in the MMC layer:
60#define MMC_ERR_FIFO 3 84 *
61#define MMC_ERR_FAILED 4 85 * ETIMEDOUT Card took too long to respond
62#define MMC_ERR_INVALID 5 86 * EILSEQ Basic format problem with the received or sent data
87 * (e.g. CRC check failed, incorrect opcode in response
88 * or bad end bit)
89 * EINVAL Request cannot be performed because of restrictions
90 * in hardware and/or the driver
91 * ENOMEDIUM Host can determine that the slot is empty and is
92 * actively failing requests
93 */
63 94
64 struct mmc_data *data; /* data segment associated with cmd */ 95 struct mmc_data *data; /* data segment associated with cmd */
65 struct mmc_request *mrq; /* associated request */ 96 struct mmc_request *mrq; /* associated request */
@@ -76,7 +107,6 @@ struct mmc_data {
76#define MMC_DATA_WRITE (1 << 8) 107#define MMC_DATA_WRITE (1 << 8)
77#define MMC_DATA_READ (1 << 9) 108#define MMC_DATA_READ (1 << 9)
78#define MMC_DATA_STREAM (1 << 10) 109#define MMC_DATA_STREAM (1 << 10)
79#define MMC_DATA_MULTI (1 << 11)
80 110
81 unsigned int bytes_xfered; 111 unsigned int bytes_xfered;
82 112
@@ -104,9 +134,20 @@ extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
104extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *, 134extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
105 struct mmc_command *, int); 135 struct mmc_command *, int);
106 136
107extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int); 137extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
108 138
109extern void mmc_claim_host(struct mmc_host *host); 139extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
110extern void mmc_release_host(struct mmc_host *host); 140extern void mmc_release_host(struct mmc_host *host);
111 141
142/**
143 * mmc_claim_host - exclusively claim a host
144 * @host: mmc host to claim
145 *
146 * Claim a host for a set of operations.
147 */
148static inline void mmc_claim_host(struct mmc_host *host)
149{
150 __mmc_claim_host(host, NULL);
151}
152
112#endif 153#endif
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index b1350dfd3e91..125eee1407ff 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -10,6 +10,8 @@
10#ifndef LINUX_MMC_HOST_H 10#ifndef LINUX_MMC_HOST_H
11#define LINUX_MMC_HOST_H 11#define LINUX_MMC_HOST_H
12 12
13#include <linux/leds.h>
14
13#include <linux/mmc/core.h> 15#include <linux/mmc/core.h>
14 16
15struct mmc_ios { 17struct mmc_ios {
@@ -51,6 +53,7 @@ struct mmc_host_ops {
51 void (*request)(struct mmc_host *host, struct mmc_request *req); 53 void (*request)(struct mmc_host *host, struct mmc_request *req);
52 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios); 54 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
53 int (*get_ro)(struct mmc_host *host); 55 int (*get_ro)(struct mmc_host *host);
56 void (*enable_sdio_irq)(struct mmc_host *host, int enable);
54}; 57};
55 58
56struct mmc_card; 59struct mmc_card;
@@ -87,9 +90,10 @@ struct mmc_host {
87 90
88#define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */ 91#define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
89#define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */ 92#define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */
90#define MMC_CAP_BYTEBLOCK (1 << 2) /* Can do non-log2 block sizes */ 93#define MMC_CAP_MMC_HIGHSPEED (1 << 2) /* Can do MMC high-speed timing */
91#define MMC_CAP_MMC_HIGHSPEED (1 << 3) /* Can do MMC high-speed timing */ 94#define MMC_CAP_SD_HIGHSPEED (1 << 3) /* Can do SD high-speed timing */
92#define MMC_CAP_SD_HIGHSPEED (1 << 4) /* Can do SD high-speed timing */ 95#define MMC_CAP_SDIO_IRQ (1 << 4) /* Can signal pending SDIO IRQs */
96#define MMC_CAP_SPI (1 << 5) /* Talks only SPI protocols */
93 97
94 /* host specific block data */ 98 /* host specific block data */
95 unsigned int max_seg_size; /* see blk_queue_max_segment_size */ 99 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
@@ -106,6 +110,14 @@ struct mmc_host {
106 struct mmc_ios ios; /* current io bus settings */ 110 struct mmc_ios ios; /* current io bus settings */
107 u32 ocr; /* the current OCR setting */ 111 u32 ocr; /* the current OCR setting */
108 112
113 /* group bitfields together to minimize padding */
114 unsigned int use_spi_crc:1;
115 unsigned int claimed:1; /* host exclusively claimed */
116 unsigned int bus_dead:1; /* bus has been released */
117#ifdef CONFIG_MMC_DEBUG
118 unsigned int removed:1; /* host is being removed */
119#endif
120
109 unsigned int mode; /* current card mode of host */ 121 unsigned int mode; /* current card mode of host */
110#define MMC_MODE_MMC 0 122#define MMC_MODE_MMC 0
111#define MMC_MODE_SD 1 123#define MMC_MODE_SD 1
@@ -113,16 +125,19 @@ struct mmc_host {
113 struct mmc_card *card; /* device attached to this host */ 125 struct mmc_card *card; /* device attached to this host */
114 126
115 wait_queue_head_t wq; 127 wait_queue_head_t wq;
116 unsigned int claimed:1; /* host exclusively claimed */
117 128
118 struct delayed_work detect; 129 struct delayed_work detect;
119#ifdef CONFIG_MMC_DEBUG
120 unsigned int removed:1; /* host is being removed */
121#endif
122 130
123 const struct mmc_bus_ops *bus_ops; /* current bus driver */ 131 const struct mmc_bus_ops *bus_ops; /* current bus driver */
124 unsigned int bus_refs; /* reference counter */ 132 unsigned int bus_refs; /* reference counter */
125 unsigned int bus_dead:1; /* bus has been released */ 133
134 unsigned int sdio_irqs;
135 struct task_struct *sdio_irq_thread;
136 atomic_t sdio_irq_thread_abort;
137
138#ifdef CONFIG_LEDS_TRIGGERS
139 struct led_trigger *led; /* activity led */
140#endif
126 141
127 unsigned long private[0] ____cacheline_aligned; 142 unsigned long private[0] ____cacheline_aligned;
128}; 143};
@@ -137,6 +152,8 @@ static inline void *mmc_priv(struct mmc_host *host)
137 return (void *)host->private; 152 return (void *)host->private;
138} 153}
139 154
155#define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
156
140#define mmc_dev(x) ((x)->parent) 157#define mmc_dev(x) ((x)->parent)
141#define mmc_classdev(x) (&(x)->class_dev) 158#define mmc_classdev(x) (&(x)->class_dev)
142#define mmc_hostname(x) ((x)->class_dev.bus_id) 159#define mmc_hostname(x) ((x)->class_dev.bus_id)
@@ -147,5 +164,11 @@ extern int mmc_resume_host(struct mmc_host *);
147extern void mmc_detect_change(struct mmc_host *, unsigned long delay); 164extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
148extern void mmc_request_done(struct mmc_host *, struct mmc_request *); 165extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
149 166
167static inline void mmc_signal_sdio_irq(struct mmc_host *host)
168{
169 host->ops->enable_sdio_irq(host, 0);
170 wake_up_process(host->sdio_irq_thread);
171}
172
150#endif 173#endif
151 174
diff --git a/include/linux/mmc/mmc.h b/include/linux/mmc/mmc.h
index e3ed9b95040e..4236fbf0b6fb 100644
--- a/include/linux/mmc/mmc.h
+++ b/include/linux/mmc/mmc.h
@@ -27,7 +27,7 @@
27 27
28/* Standard MMC commands (4.1) type argument response */ 28/* Standard MMC commands (4.1) type argument response */
29 /* class 1 */ 29 /* class 1 */
30#define MMC_GO_IDLE_STATE 0 /* bc */ 30#define MMC_GO_IDLE_STATE 0 /* bc */
31#define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */ 31#define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
32#define MMC_ALL_SEND_CID 2 /* bcr R2 */ 32#define MMC_ALL_SEND_CID 2 /* bcr R2 */
33#define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */ 33#define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
@@ -39,8 +39,10 @@
39#define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */ 39#define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
40#define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */ 40#define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
41#define MMC_STOP_TRANSMISSION 12 /* ac R1b */ 41#define MMC_STOP_TRANSMISSION 12 /* ac R1b */
42#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */ 42#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
43#define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */ 43#define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
44#define MMC_SPI_READ_OCR 58 /* spi spi_R3 */
45#define MMC_SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
44 46
45 /* class 2 */ 47 /* class 2 */
46#define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */ 48#define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
@@ -90,15 +92,15 @@
90 */ 92 */
91 93
92/* 94/*
93 MMC status in R1 95 MMC status in R1, for native mode (SPI bits are different)
94 Type 96 Type
95 e : error bit 97 e : error bit
96 s : status bit 98 s : status bit
97 r : detected and set for the actual command response 99 r : detected and set for the actual command response
98 x : detected and set during command execution. the host must poll 100 x : detected and set during command execution. the host must poll
99 the card by sending status command in order to read these bits. 101 the card by sending status command in order to read these bits.
100 Clear condition 102 Clear condition
101 a : according to the card state 103 a : according to the card state
102 b : always related to the previous command. Reception of 104 b : always related to the previous command. Reception of
103 a valid command will clear it (with a delay of one command) 105 a valid command will clear it (with a delay of one command)
104 c : clear by read 106 c : clear by read
@@ -124,10 +126,33 @@
124#define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */ 126#define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
125#define R1_ERASE_RESET (1 << 13) /* sr, c */ 127#define R1_ERASE_RESET (1 << 13) /* sr, c */
126#define R1_STATUS(x) (x & 0xFFFFE000) 128#define R1_STATUS(x) (x & 0xFFFFE000)
127#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */ 129#define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
128#define R1_READY_FOR_DATA (1 << 8) /* sx, a */ 130#define R1_READY_FOR_DATA (1 << 8) /* sx, a */
129#define R1_APP_CMD (1 << 5) /* sr, c */ 131#define R1_APP_CMD (1 << 5) /* sr, c */
130 132
133/*
134 * MMC/SD in SPI mode reports R1 status always, and R2 for SEND_STATUS
135 * R1 is the low order byte; R2 is the next highest byte, when present.
136 */
137#define R1_SPI_IDLE (1 << 0)
138#define R1_SPI_ERASE_RESET (1 << 1)
139#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
140#define R1_SPI_COM_CRC (1 << 3)
141#define R1_SPI_ERASE_SEQ (1 << 4)
142#define R1_SPI_ADDRESS (1 << 5)
143#define R1_SPI_PARAMETER (1 << 6)
144/* R1 bit 7 is always zero */
145#define R2_SPI_CARD_LOCKED (1 << 8)
146#define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
147#define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
148#define R2_SPI_ERROR (1 << 10)
149#define R2_SPI_CC_ERROR (1 << 11)
150#define R2_SPI_CARD_ECC_ERROR (1 << 12)
151#define R2_SPI_WP_VIOLATION (1 << 13)
152#define R2_SPI_ERASE_PARAM (1 << 14)
153#define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
154#define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
155
131/* These are unpacked versions of the actual responses */ 156/* These are unpacked versions of the actual responses */
132 157
133struct _mmc_csd { 158struct _mmc_csd {
@@ -182,6 +207,7 @@ struct _mmc_csd {
182 */ 207 */
183#define CCC_BASIC (1<<0) /* (0) Basic protocol functions */ 208#define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
184 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */ 209 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
210 /* (and for SPI, CMD58,59) */
185#define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */ 211#define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
186 /* (CMD11) */ 212 /* (CMD11) */
187#define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */ 213#define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
@@ -227,6 +253,7 @@ struct _mmc_csd {
227#define EXT_CSD_BUS_WIDTH 183 /* R/W */ 253#define EXT_CSD_BUS_WIDTH 183 /* R/W */
228#define EXT_CSD_HS_TIMING 185 /* R/W */ 254#define EXT_CSD_HS_TIMING 185 /* R/W */
229#define EXT_CSD_CARD_TYPE 196 /* RO */ 255#define EXT_CSD_CARD_TYPE 196 /* RO */
256#define EXT_CSD_REV 192 /* RO */
230#define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */ 257#define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
231 258
232/* 259/*
diff --git a/include/linux/mmc/sdio.h b/include/linux/mmc/sdio.h
new file mode 100644
index 000000000000..47ba464f5170
--- /dev/null
+++ b/include/linux/mmc/sdio.h
@@ -0,0 +1,159 @@
1/*
2 * include/linux/mmc/sdio.h
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#ifndef MMC_SDIO_H
13#define MMC_SDIO_H
14
15/* SDIO commands type argument response */
16#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
17#define SD_IO_RW_DIRECT 52 /* ac [31:0] See below R5 */
18#define SD_IO_RW_EXTENDED 53 /* adtc [31:0] See below R5 */
19
20/*
21 * SD_IO_RW_DIRECT argument format:
22 *
23 * [31] R/W flag
24 * [30:28] Function number
25 * [27] RAW flag
26 * [25:9] Register address
27 * [7:0] Data
28 */
29
30/*
31 * SD_IO_RW_EXTENDED argument format:
32 *
33 * [31] R/W flag
34 * [30:28] Function number
35 * [27] Block mode
36 * [26] Increment address
37 * [25:9] Register address
38 * [8:0] Byte/block count
39 */
40
41/*
42 SDIO status in R5
43 Type
44 e : error bit
45 s : status bit
46 r : detected and set for the actual command response
47 x : detected and set during command execution. the host must poll
48 the card by sending status command in order to read these bits.
49 Clear condition
50 a : according to the card state
51 b : always related to the previous command. Reception of
52 a valid command will clear it (with a delay of one command)
53 c : clear by read
54 */
55
56#define R5_COM_CRC_ERROR (1 << 15) /* er, b */
57#define R5_ILLEGAL_COMMAND (1 << 14) /* er, b */
58#define R5_ERROR (1 << 11) /* erx, c */
59#define R5_FUNCTION_NUMBER (1 << 9) /* er, c */
60#define R5_OUT_OF_RANGE (1 << 8) /* er, c */
61#define R5_STATUS(x) (x & 0xCB00)
62#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
63
64/*
65 * Card Common Control Registers (CCCR)
66 */
67
68#define SDIO_CCCR_CCCR 0x00
69
70#define SDIO_CCCR_REV_1_00 0 /* CCCR/FBR Version 1.00 */
71#define SDIO_CCCR_REV_1_10 1 /* CCCR/FBR Version 1.10 */
72#define SDIO_CCCR_REV_1_20 2 /* CCCR/FBR Version 1.20 */
73
74#define SDIO_SDIO_REV_1_00 0 /* SDIO Spec Version 1.00 */
75#define SDIO_SDIO_REV_1_10 1 /* SDIO Spec Version 1.10 */
76#define SDIO_SDIO_REV_1_20 2 /* SDIO Spec Version 1.20 */
77#define SDIO_SDIO_REV_2_00 3 /* SDIO Spec Version 2.00 */
78
79#define SDIO_CCCR_SD 0x01
80
81#define SDIO_SD_REV_1_01 0 /* SD Physical Spec Version 1.01 */
82#define SDIO_SD_REV_1_10 1 /* SD Physical Spec Version 1.10 */
83#define SDIO_SD_REV_2_00 2 /* SD Physical Spec Version 2.00 */
84
85#define SDIO_CCCR_IOEx 0x02
86#define SDIO_CCCR_IORx 0x03
87
88#define SDIO_CCCR_IENx 0x04 /* Function/Master Interrupt Enable */
89#define SDIO_CCCR_INTx 0x05 /* Function Interrupt Pending */
90
91#define SDIO_CCCR_ABORT 0x06 /* function abort/card reset */
92
93#define SDIO_CCCR_IF 0x07 /* bus interface controls */
94
95#define SDIO_BUS_WIDTH_1BIT 0x00
96#define SDIO_BUS_WIDTH_4BIT 0x02
97
98#define SDIO_BUS_CD_DISABLE 0x80 /* disable pull-up on DAT3 (pin 1) */
99
100#define SDIO_CCCR_CAPS 0x08
101
102#define SDIO_CCCR_CAP_SDC 0x01 /* can do CMD52 while data transfer */
103#define SDIO_CCCR_CAP_SMB 0x02 /* can do multi-block xfers (CMD53) */
104#define SDIO_CCCR_CAP_SRW 0x04 /* supports read-wait protocol */
105#define SDIO_CCCR_CAP_SBS 0x08 /* supports suspend/resume */
106#define SDIO_CCCR_CAP_S4MI 0x10 /* interrupt during 4-bit CMD53 */
107#define SDIO_CCCR_CAP_E4MI 0x20 /* enable ints during 4-bit CMD53 */
108#define SDIO_CCCR_CAP_LSC 0x40 /* low speed card */
109#define SDIO_CCCR_CAP_4BLS 0x80 /* 4 bit low speed card */
110
111#define SDIO_CCCR_CIS 0x09 /* common CIS pointer (3 bytes) */
112
113/* Following 4 regs are valid only if SBS is set */
114#define SDIO_CCCR_SUSPEND 0x0c
115#define SDIO_CCCR_SELx 0x0d
116#define SDIO_CCCR_EXECx 0x0e
117#define SDIO_CCCR_READYx 0x0f
118
119#define SDIO_CCCR_BLKSIZE 0x10
120
121#define SDIO_CCCR_POWER 0x12
122
123#define SDIO_POWER_SMPC 0x01 /* Supports Master Power Control */
124#define SDIO_POWER_EMPC 0x02 /* Enable Master Power Control */
125
126#define SDIO_CCCR_SPEED 0x13
127
128#define SDIO_SPEED_SHS 0x01 /* Supports High-Speed mode */
129#define SDIO_SPEED_EHS 0x02 /* Enable High-Speed mode */
130
131/*
132 * Function Basic Registers (FBR)
133 */
134
135#define SDIO_FBR_BASE(f) ((f) * 0x100) /* base of function f's FBRs */
136
137#define SDIO_FBR_STD_IF 0x00
138
139#define SDIO_FBR_SUPPORTS_CSA 0x40 /* supports Code Storage Area */
140#define SDIO_FBR_ENABLE_CSA 0x80 /* enable Code Storage Area */
141
142#define SDIO_FBR_STD_IF_EXT 0x01
143
144#define SDIO_FBR_POWER 0x02
145
146#define SDIO_FBR_POWER_SPS 0x01 /* Supports Power Selection */
147#define SDIO_FBR_POWER_EPS 0x02 /* Enable (low) Power Selection */
148
149#define SDIO_FBR_CIS 0x09 /* CIS pointer (3 bytes) */
150
151
152#define SDIO_FBR_CSA 0x0C /* CSA pointer (3 bytes) */
153
154#define SDIO_FBR_CSA_DATA 0x0F
155
156#define SDIO_FBR_BLKSIZE 0x10 /* block size (2 bytes) */
157
158#endif
159
diff --git a/include/linux/mmc/sdio_func.h b/include/linux/mmc/sdio_func.h
new file mode 100644
index 000000000000..b050f4d7b41f
--- /dev/null
+++ b/include/linux/mmc/sdio_func.h
@@ -0,0 +1,153 @@
1/*
2 * include/linux/mmc/sdio_func.h
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#ifndef MMC_SDIO_FUNC_H
13#define MMC_SDIO_FUNC_H
14
15#include <linux/device.h>
16#include <linux/mod_devicetable.h>
17
18struct mmc_card;
19struct sdio_func;
20
21typedef void (sdio_irq_handler_t)(struct sdio_func *);
22
23/*
24 * SDIO function CIS tuple (unknown to the core)
25 */
26struct sdio_func_tuple {
27 struct sdio_func_tuple *next;
28 unsigned char code;
29 unsigned char size;
30 unsigned char data[0];
31};
32
33/*
34 * SDIO function devices
35 */
36struct sdio_func {
37 struct mmc_card *card; /* the card this device belongs to */
38 struct device dev; /* the device */
39 sdio_irq_handler_t *irq_handler; /* IRQ callback */
40 unsigned int num; /* function number */
41
42 unsigned char class; /* standard interface class */
43 unsigned short vendor; /* vendor id */
44 unsigned short device; /* device id */
45
46 unsigned max_blksize; /* maximum block size */
47 unsigned cur_blksize; /* current block size */
48
49 unsigned int state; /* function state */
50#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
51
52 u8 tmpbuf[4]; /* DMA:able scratch buffer */
53
54 unsigned num_info; /* number of info strings */
55 const char **info; /* info strings */
56
57 struct sdio_func_tuple *tuples;
58};
59
60#define sdio_func_present(f) ((f)->state & SDIO_STATE_PRESENT)
61
62#define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
63
64#define sdio_func_id(f) ((f)->dev.bus_id)
65
66#define sdio_get_drvdata(f) dev_get_drvdata(&(f)->dev)
67#define sdio_set_drvdata(f,d) dev_set_drvdata(&(f)->dev, d)
68
69/*
70 * SDIO function device driver
71 */
72struct sdio_driver {
73 char *name;
74 const struct sdio_device_id *id_table;
75
76 int (*probe)(struct sdio_func *, const struct sdio_device_id *);
77 void (*remove)(struct sdio_func *);
78
79 struct device_driver drv;
80};
81
82/**
83 * SDIO_DEVICE - macro used to describe a specific SDIO device
84 * @vend: the 16 bit manufacturer code
85 * @dev: the 16 bit function id
86 *
87 * This macro is used to create a struct sdio_device_id that matches a
88 * specific device. The class field will be set to SDIO_ANY_ID.
89 */
90#define SDIO_DEVICE(vend,dev) \
91 .class = SDIO_ANY_ID, \
92 .vendor = (vend), .device = (dev)
93
94/**
95 * SDIO_DEVICE_CLASS - macro used to describe a specific SDIO device class
96 * @dev_class: the 8 bit standard interface code
97 *
98 * This macro is used to create a struct sdio_device_id that matches a
99 * specific standard SDIO function type. The vendor and device fields will
100 * be set to SDIO_ANY_ID.
101 */
102#define SDIO_DEVICE_CLASS(dev_class) \
103 .class = (dev_class), \
104 .vendor = SDIO_ANY_ID, .device = SDIO_ANY_ID
105
106extern int sdio_register_driver(struct sdio_driver *);
107extern void sdio_unregister_driver(struct sdio_driver *);
108
109/*
110 * SDIO I/O operations
111 */
112extern void sdio_claim_host(struct sdio_func *func);
113extern void sdio_release_host(struct sdio_func *func);
114
115extern int sdio_enable_func(struct sdio_func *func);
116extern int sdio_disable_func(struct sdio_func *func);
117
118extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
119
120extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
121extern int sdio_release_irq(struct sdio_func *func);
122
123extern unsigned char sdio_readb(struct sdio_func *func,
124 unsigned int addr, int *err_ret);
125extern unsigned short sdio_readw(struct sdio_func *func,
126 unsigned int addr, int *err_ret);
127extern unsigned long sdio_readl(struct sdio_func *func,
128 unsigned int addr, int *err_ret);
129
130extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
131 unsigned int addr, int count);
132extern int sdio_readsb(struct sdio_func *func, void *dst,
133 unsigned int addr, int count);
134
135extern void sdio_writeb(struct sdio_func *func, unsigned char b,
136 unsigned int addr, int *err_ret);
137extern void sdio_writew(struct sdio_func *func, unsigned short b,
138 unsigned int addr, int *err_ret);
139extern void sdio_writel(struct sdio_func *func, unsigned long b,
140 unsigned int addr, int *err_ret);
141
142extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
143 void *src, int count);
144extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
145 void *src, int count);
146
147extern unsigned char sdio_f0_readb(struct sdio_func *func,
148 unsigned int addr, int *err_ret);
149extern void sdio_f0_writeb(struct sdio_func *func, unsigned char b,
150 unsigned int addr, int *err_ret);
151
152#endif
153
diff --git a/include/linux/mmc/sdio_ids.h b/include/linux/mmc/sdio_ids.h
new file mode 100644
index 000000000000..09306d47ff5e
--- /dev/null
+++ b/include/linux/mmc/sdio_ids.h
@@ -0,0 +1,23 @@
1/*
2 * SDIO Classes, Interface Types, Manufacturer IDs, etc.
3 */
4
5#ifndef MMC_SDIO_IDS_H
6#define MMC_SDIO_IDS_H
7
8/*
9 * Standard SDIO Function Interfaces
10 */
11
12#define SDIO_CLASS_NONE 0x00 /* Not a SDIO standard interface */
13#define SDIO_CLASS_UART 0x01 /* standard UART interface */
14#define SDIO_CLASS_BT_A 0x02 /* Type-A BlueTooth std interface */
15#define SDIO_CLASS_BT_B 0x03 /* Type-B BlueTooth std interface */
16#define SDIO_CLASS_GPS 0x04 /* GPS standard interface */
17#define SDIO_CLASS_CAMERA 0x05 /* Camera standard interface */
18#define SDIO_CLASS_PHS 0x06 /* PHS standard interface */
19#define SDIO_CLASS_WLAN 0x07 /* WLAN interface */
20#define SDIO_CLASS_ATA 0x08 /* Embedded SDIO-ATA std interface */
21
22
23#endif
diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
index 4dc5fa8be781..e47e5951058b 100644
--- a/include/linux/mod_devicetable.h
+++ b/include/linux/mod_devicetable.h
@@ -340,4 +340,15 @@ struct parisc_device_id {
340#define PA_HVERSION_ANY_ID 0xffff 340#define PA_HVERSION_ANY_ID 0xffff
341#define PA_SVERSION_ANY_ID 0xffffffff 341#define PA_SVERSION_ANY_ID 0xffffffff
342 342
343/* SDIO */
344
345#define SDIO_ANY_ID (~0)
346
347struct sdio_device_id {
348 __u8 class; /* Standard interface or SDIO_ANY_ID */
349 __u16 vendor; /* Vendor or SDIO_ANY_ID */
350 __u16 device; /* Device ID or SDIO_ANY_ID */
351 kernel_ulong_t driver_data; /* Data private to the driver */
352};
353
343#endif /* LINUX_MOD_DEVICETABLE_H */ 354#endif /* LINUX_MOD_DEVICETABLE_H */
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 55f307ffbf96..c8636bb3e3a7 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -1471,6 +1471,8 @@
1471#define PCI_DEVICE_ID_RICOH_RL5C476 0x0476 1471#define PCI_DEVICE_ID_RICOH_RL5C476 0x0476
1472#define PCI_DEVICE_ID_RICOH_RL5C478 0x0478 1472#define PCI_DEVICE_ID_RICOH_RL5C478 0x0478
1473#define PCI_DEVICE_ID_RICOH_R5C822 0x0822 1473#define PCI_DEVICE_ID_RICOH_R5C822 0x0822
1474#define PCI_DEVICE_ID_RICOH_R5C832 0x0832
1475#define PCI_DEVICE_ID_RICOH_R5C843 0x0843
1474 1476
1475#define PCI_VENDOR_ID_DLINK 0x1186 1477#define PCI_VENDOR_ID_DLINK 0x1186
1476#define PCI_DEVICE_ID_DLINK_DGE510T 0x4c00 1478#define PCI_DEVICE_ID_DLINK_DGE510T 0x4c00
diff --git a/include/linux/spi/mmc_spi.h b/include/linux/spi/mmc_spi.h
new file mode 100644
index 000000000000..e9bbe3ebd721
--- /dev/null
+++ b/include/linux/spi/mmc_spi.h
@@ -0,0 +1,33 @@
1#ifndef __LINUX_SPI_MMC_SPI_H
2#define __LINUX_SPI_MMC_SPI_H
3
4struct device;
5struct mmc_host;
6
7/* Put this in platform_data of a device being used to manage an MMC/SD
8 * card slot. (Modeled after PXA mmc glue; see that for usage examples.)
9 *
10 * REVISIT This is not a spi-specific notion. Any card slot should be
11 * able to handle it. If the MMC core doesn't adopt this kind of notion,
12 * switch the "struct device *" parameters over to "struct spi_device *".
13 */
14struct mmc_spi_platform_data {
15 /* driver activation and (optional) card detect irq hookup */
16 int (*init)(struct device *,
17 irqreturn_t (*)(int, void *),
18 void *);
19 void (*exit)(struct device *, void *);
20
21 /* sense switch on sd cards */
22 int (*get_ro)(struct device *);
23
24 /* how long to debounce card detect, in msecs */
25 u16 detect_delay;
26
27 /* power management */
28 u16 powerup_msecs; /* delay of up to 250 msec */
29 u32 ocr_mask; /* available voltages */
30 void (*setpower)(struct device *, unsigned int maskval);
31};
32
33#endif /* __LINUX_SPI_MMC_SPI_H */
diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 8a09021d8c59..1e5d4d693195 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -484,6 +484,22 @@ static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
484 return 1; 484 return 1;
485} 485}
486 486
487/* Looks like: sdio:cNvNdN. */
488static int do_sdio_entry(const char *filename,
489 struct sdio_device_id *id, char *alias)
490{
491 id->class = TO_NATIVE(id->class);
492 id->vendor = TO_NATIVE(id->vendor);
493 id->device = TO_NATIVE(id->device);
494
495 strcpy(alias, "sdio:");
496 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
497 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
498 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
499
500 return 1;
501}
502
487/* Ignore any prefix, eg. v850 prepends _ */ 503/* Ignore any prefix, eg. v850 prepends _ */
488static inline int sym_is(const char *symbol, const char *name) 504static inline int sym_is(const char *symbol, const char *name)
489{ 505{
@@ -599,6 +615,10 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
599 do_table(symval, sym->st_size, 615 do_table(symval, sym->st_size,
600 sizeof(struct parisc_device_id), "parisc", 616 sizeof(struct parisc_device_id), "parisc",
601 do_parisc_entry, mod); 617 do_parisc_entry, mod);
618 else if (sym_is(symname, "__mod_sdio_device_table"))
619 do_table(symval, sym->st_size,
620 sizeof(struct sdio_device_id), "sdio",
621 do_sdio_entry, mod);
602} 622}
603 623
604/* Now add out buffered information to the generated C source */ 624/* Now add out buffered information to the generated C source */