mmc: Allow host drivers to specify a max block size
[linux-2.6-block.git] / drivers / mmc / mmc.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/mmc/mmc.c
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae
PO
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * SD support Copyright (C) 2005 Pierre Ossman, All Rights Reserved.
bce40a36 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
1da177e4
LT
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/completion.h>
17#include <linux/device.h>
18#include <linux/delay.h>
19#include <linux/pagemap.h>
20#include <linux/err.h>
b57c43ad
PO
21#include <asm/scatterlist.h>
22#include <linux/scatterlist.h>
1da177e4
LT
23
24#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
26#include <linux/mmc/protocol.h>
27
28#include "mmc.h"
29
1da177e4
LT
30#define CMD_RETRIES 3
31
32/*
33 * OCR Bit positions to 10s of Vdd mV.
34 */
35static const unsigned short mmc_ocr_bit_to_vdd[] = {
36 150, 155, 160, 165, 170, 180, 190, 200,
37 210, 220, 230, 240, 250, 260, 270, 280,
38 290, 300, 310, 320, 330, 340, 350, 360
39};
40
41static const unsigned int tran_exp[] = {
42 10000, 100000, 1000000, 10000000,
43 0, 0, 0, 0
44};
45
46static const unsigned char tran_mant[] = {
47 0, 10, 12, 13, 15, 20, 25, 30,
48 35, 40, 45, 50, 55, 60, 70, 80,
49};
50
51static const unsigned int tacc_exp[] = {
52 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
53};
54
55static const unsigned int tacc_mant[] = {
56 0, 10, 12, 13, 15, 20, 25, 30,
57 35, 40, 45, 50, 55, 60, 70, 80,
58};
59
60
61/**
fe10c6ab
RK
62 * mmc_request_done - finish processing an MMC request
63 * @host: MMC host which completed request
64 * @mrq: MMC request which request
1da177e4
LT
65 *
66 * MMC drivers should call this function when they have completed
fe10c6ab 67 * their processing of a request.
1da177e4
LT
68 */
69void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70{
71 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
72 int err = cmd->error;
73
74 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
75 mmc_hostname(host), cmd->opcode, err,
76 mrq->data ? mrq->data->error : 0,
77 mrq->stop ? mrq->stop->error : 0,
78 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
1da177e4
LT
79
80 if (err && cmd->retries) {
81 cmd->retries--;
82 cmd->error = 0;
83 host->ops->request(host, mrq);
84 } else if (mrq->done) {
85 mrq->done(mrq);
86 }
87}
88
89EXPORT_SYMBOL(mmc_request_done);
90
91/**
92 * mmc_start_request - start a command on a host
93 * @host: MMC host to start command on
94 * @mrq: MMC request to start
95 *
96 * Queue a command on the specified host. We expect the
97 * caller to be holding the host lock with interrupts disabled.
98 */
99void
100mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
101{
920e70c5
RK
102 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
103 mmc_hostname(host), mrq->cmd->opcode,
104 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 105
f22ee4ed 106 WARN_ON(!host->claimed);
1da177e4
LT
107
108 mrq->cmd->error = 0;
109 mrq->cmd->mrq = mrq;
110 if (mrq->data) {
fe4a3c7a
PO
111 BUG_ON(mrq->data->blksz > host->max_blk_size);
112
1da177e4
LT
113 mrq->cmd->data = mrq->data;
114 mrq->data->error = 0;
115 mrq->data->mrq = mrq;
116 if (mrq->stop) {
117 mrq->data->stop = mrq->stop;
118 mrq->stop->error = 0;
119 mrq->stop->mrq = mrq;
120 }
121 }
122 host->ops->request(host, mrq);
123}
124
125EXPORT_SYMBOL(mmc_start_request);
126
127static void mmc_wait_done(struct mmc_request *mrq)
128{
129 complete(mrq->done_data);
130}
131
132int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
133{
0afffc72 134 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
135
136 mrq->done_data = &complete;
137 mrq->done = mmc_wait_done;
138
139 mmc_start_request(host, mrq);
140
141 wait_for_completion(&complete);
142
143 return 0;
144}
145
146EXPORT_SYMBOL(mmc_wait_for_req);
147
148/**
149 * mmc_wait_for_cmd - start a command and wait for completion
150 * @host: MMC host to start command
151 * @cmd: MMC command to start
152 * @retries: maximum number of retries
153 *
154 * Start a new MMC command for a host, and wait for the command
155 * to complete. Return any error that occurred while the command
156 * was executing. Do not attempt to parse the response.
157 */
158int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
159{
160 struct mmc_request mrq;
161
f22ee4ed 162 BUG_ON(!host->claimed);
1da177e4
LT
163
164 memset(&mrq, 0, sizeof(struct mmc_request));
165
166 memset(cmd->resp, 0, sizeof(cmd->resp));
167 cmd->retries = retries;
168
169 mrq.cmd = cmd;
170 cmd->data = NULL;
171
172 mmc_wait_for_req(host, &mrq);
173
174 return cmd->error;
175}
176
177EXPORT_SYMBOL(mmc_wait_for_cmd);
178
335eadf2
PO
179/**
180 * mmc_wait_for_app_cmd - start an application command and wait for
181 completion
182 * @host: MMC host to start command
183 * @rca: RCA to send MMC_APP_CMD to
184 * @cmd: MMC command to start
185 * @retries: maximum number of retries
186 *
187 * Sends a MMC_APP_CMD, checks the card response, sends the command
188 * in the parameter and waits for it to complete. Return any error
189 * that occurred while the command was executing. Do not attempt to
190 * parse the response.
191 */
192int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
193 struct mmc_command *cmd, int retries)
194{
195 struct mmc_request mrq;
196 struct mmc_command appcmd;
197
198 int i, err;
199
f22ee4ed 200 BUG_ON(!host->claimed);
335eadf2
PO
201 BUG_ON(retries < 0);
202
203 err = MMC_ERR_INVALID;
204
205 /*
206 * We have to resend MMC_APP_CMD for each attempt so
207 * we cannot use the retries field in mmc_command.
208 */
209 for (i = 0;i <= retries;i++) {
210 memset(&mrq, 0, sizeof(struct mmc_request));
211
212 appcmd.opcode = MMC_APP_CMD;
213 appcmd.arg = rca << 16;
e9225176 214 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
335eadf2
PO
215 appcmd.retries = 0;
216 memset(appcmd.resp, 0, sizeof(appcmd.resp));
217 appcmd.data = NULL;
218
219 mrq.cmd = &appcmd;
220 appcmd.data = NULL;
221
222 mmc_wait_for_req(host, &mrq);
223
224 if (appcmd.error) {
225 err = appcmd.error;
226 continue;
227 }
228
229 /* Check that card supported application commands */
230 if (!(appcmd.resp[0] & R1_APP_CMD))
231 return MMC_ERR_FAILED;
232
233 memset(&mrq, 0, sizeof(struct mmc_request));
234
235 memset(cmd->resp, 0, sizeof(cmd->resp));
236 cmd->retries = 0;
237
238 mrq.cmd = cmd;
239 cmd->data = NULL;
1da177e4 240
335eadf2
PO
241 mmc_wait_for_req(host, &mrq);
242
243 err = cmd->error;
244 if (cmd->error == MMC_ERR_NONE)
245 break;
246 }
247
248 return err;
249}
250
251EXPORT_SYMBOL(mmc_wait_for_app_cmd);
1da177e4 252
d773d725
RK
253/**
254 * mmc_set_data_timeout - set the timeout for a data command
255 * @data: data phase for command
256 * @card: the MMC card associated with the data transfer
257 * @write: flag to differentiate reads from writes
258 */
259void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
260 int write)
261{
262 unsigned int mult;
263
264 /*
265 * SD cards use a 100 multiplier rather than 10
266 */
267 mult = mmc_card_sd(card) ? 100 : 10;
268
269 /*
270 * Scale up the multiplier (and therefore the timeout) by
271 * the r2w factor for writes.
272 */
273 if (write)
274 mult <<= card->csd.r2w_factor;
275
276 data->timeout_ns = card->csd.tacc_ns * mult;
277 data->timeout_clks = card->csd.tacc_clks * mult;
278
279 /*
280 * SD cards also have an upper limit on the timeout.
281 */
282 if (mmc_card_sd(card)) {
283 unsigned int timeout_us, limit_us;
284
285 timeout_us = data->timeout_ns / 1000;
286 timeout_us += data->timeout_clks * 1000 /
287 (card->host->ios.clock / 1000);
288
289 if (write)
290 limit_us = 250000;
291 else
292 limit_us = 100000;
293
fba68bd2
PL
294 /*
295 * SDHC cards always use these fixed values.
296 */
297 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
298 data->timeout_ns = limit_us * 1000;
299 data->timeout_clks = 0;
300 }
301 }
302}
303EXPORT_SYMBOL(mmc_set_data_timeout);
304
b57c43ad
PO
305static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
306
1da177e4
LT
307/**
308 * __mmc_claim_host - exclusively claim a host
309 * @host: mmc host to claim
310 * @card: mmc card to claim host for
311 *
312 * Claim a host for a set of operations. If a valid card
313 * is passed and this wasn't the last card selected, select
314 * the card before returning.
315 *
316 * Note: you should use mmc_card_claim_host or mmc_claim_host.
317 */
318int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
319{
320 DECLARE_WAITQUEUE(wait, current);
321 unsigned long flags;
322 int err = 0;
323
324 add_wait_queue(&host->wq, &wait);
325 spin_lock_irqsave(&host->lock, flags);
326 while (1) {
327 set_current_state(TASK_UNINTERRUPTIBLE);
f22ee4ed 328 if (!host->claimed)
1da177e4
LT
329 break;
330 spin_unlock_irqrestore(&host->lock, flags);
331 schedule();
332 spin_lock_irqsave(&host->lock, flags);
333 }
334 set_current_state(TASK_RUNNING);
f22ee4ed 335 host->claimed = 1;
1da177e4
LT
336 spin_unlock_irqrestore(&host->lock, flags);
337 remove_wait_queue(&host->wq, &wait);
338
b57c43ad
PO
339 if (card != (void *)-1) {
340 err = mmc_select_card(host, card);
341 if (err != MMC_ERR_NONE)
342 return err;
1da177e4
LT
343 }
344
345 return err;
346}
347
348EXPORT_SYMBOL(__mmc_claim_host);
349
350/**
351 * mmc_release_host - release a host
352 * @host: mmc host to release
353 *
354 * Release a MMC host, allowing others to claim the host
355 * for their operations.
356 */
357void mmc_release_host(struct mmc_host *host)
358{
359 unsigned long flags;
360
f22ee4ed 361 BUG_ON(!host->claimed);
1da177e4
LT
362
363 spin_lock_irqsave(&host->lock, flags);
f22ee4ed 364 host->claimed = 0;
1da177e4
LT
365 spin_unlock_irqrestore(&host->lock, flags);
366
367 wake_up(&host->wq);
368}
369
370EXPORT_SYMBOL(mmc_release_host);
371
920e70c5
RK
372static inline void mmc_set_ios(struct mmc_host *host)
373{
374 struct mmc_ios *ios = &host->ios;
375
376 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
377 mmc_hostname(host), ios->clock, ios->bus_mode,
378 ios->power_mode, ios->chip_select, ios->vdd,
379 ios->bus_width);
fba68bd2 380
920e70c5
RK
381 host->ops->set_ios(host, ios);
382}
383
b57c43ad
PO
384static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
385{
386 int err;
387 struct mmc_command cmd;
388
f22ee4ed 389 BUG_ON(!host->claimed);
b57c43ad
PO
390
391 if (host->card_selected == card)
392 return MMC_ERR_NONE;
393
394 host->card_selected = card;
395
396 cmd.opcode = MMC_SELECT_CARD;
397 cmd.arg = card->rca << 16;
e9225176 398 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
b57c43ad
PO
399
400 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
401 if (err != MMC_ERR_NONE)
402 return err;
403
f218278a 404 /*
e45a1bd2
PL
405 * We can only change the bus width of SD cards when
406 * they are selected so we have to put the handling
f218278a 407 * here.
e45a1bd2
PL
408 *
409 * The card is in 1 bit mode by default so
410 * we only need to change if it supports the
411 * wider version.
f218278a 412 */
e45a1bd2
PL
413 if (mmc_card_sd(card) &&
414 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
415
f218278a 416 /*
e45a1bd2
PL
417 * Default bus width is 1 bit.
418 */
419 host->ios.bus_width = MMC_BUS_WIDTH_1;
420
421 if (host->caps & MMC_CAP_4_BIT_DATA) {
f218278a
PO
422 struct mmc_command cmd;
423 cmd.opcode = SD_APP_SET_BUS_WIDTH;
424 cmd.arg = SD_BUS_WIDTH_4;
e9225176 425 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
f218278a
PO
426
427 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
428 CMD_RETRIES);
429 if (err != MMC_ERR_NONE)
430 return err;
431
432 host->ios.bus_width = MMC_BUS_WIDTH_4;
433 }
434 }
435
920e70c5 436 mmc_set_ios(host);
f218278a 437
b57c43ad
PO
438 return MMC_ERR_NONE;
439}
440
1da177e4
LT
441/*
442 * Ensure that no card is selected.
443 */
444static void mmc_deselect_cards(struct mmc_host *host)
445{
446 struct mmc_command cmd;
447
448 if (host->card_selected) {
449 host->card_selected = NULL;
450
451 cmd.opcode = MMC_SELECT_CARD;
452 cmd.arg = 0;
e9225176 453 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
1da177e4
LT
454
455 mmc_wait_for_cmd(host, &cmd, 0);
456 }
457}
458
459
460static inline void mmc_delay(unsigned int ms)
461{
73778120
PO
462 if (ms < 1000 / HZ) {
463 cond_resched();
1da177e4
LT
464 mdelay(ms);
465 } else {
73778120 466 msleep(ms);
1da177e4
LT
467 }
468}
469
470/*
471 * Mask off any voltages we don't support and select
472 * the lowest voltage
473 */
474static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
475{
476 int bit;
477
478 ocr &= host->ocr_avail;
479
480 bit = ffs(ocr);
481 if (bit) {
482 bit -= 1;
483
63ef731a 484 ocr &= 3 << bit;
1da177e4
LT
485
486 host->ios.vdd = bit;
920e70c5 487 mmc_set_ios(host);
1da177e4
LT
488 } else {
489 ocr = 0;
490 }
491
492 return ocr;
493}
494
495#define UNSTUFF_BITS(resp,start,size) \
496 ({ \
497 const int __size = size; \
498 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
499 const int __off = 3 - ((start) / 32); \
500 const int __shft = (start) & 31; \
501 u32 __res; \
502 \
503 __res = resp[__off] >> __shft; \
504 if (__size + __shft > 32) \
505 __res |= resp[__off-1] << ((32 - __shft) % 32); \
506 __res & __mask; \
507 })
508
509/*
510 * Given the decoded CSD structure, decode the raw CID to our CID structure.
511 */
512static void mmc_decode_cid(struct mmc_card *card)
513{
514 u32 *resp = card->raw_cid;
515
516 memset(&card->cid, 0, sizeof(struct mmc_cid));
517
335eadf2
PO
518 if (mmc_card_sd(card)) {
519 /*
520 * SD doesn't currently have a version field so we will
521 * have to assume we can parse this.
522 */
523 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
524 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
525 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
526 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
527 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
528 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
529 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
530 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
531 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
532 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
533 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
534 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
535
536 card->cid.year += 2000; /* SD cards year offset */
a00fc090 537 } else {
335eadf2
PO
538 /*
539 * The selection of the format here is based upon published
540 * specs from sandisk and from what people have reported.
541 */
542 switch (card->csd.mmca_vsn) {
543 case 0: /* MMC v1.0 - v1.2 */
544 case 1: /* MMC v1.4 */
545 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
546 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
547 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
548 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
549 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
550 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
551 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
552 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
553 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
554 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
555 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
556 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
557 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
558 break;
559
560 case 2: /* MMC v2.0 - v2.2 */
561 case 3: /* MMC v3.1 - v3.3 */
cb757b4e 562 case 4: /* MMC v4 */
335eadf2
PO
563 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
564 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
565 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
566 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
567 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
568 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
569 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
570 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
571 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
572 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
573 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
574 break;
575
576 default:
577 printk("%s: card has unknown MMCA version %d\n",
578 mmc_hostname(card->host), card->csd.mmca_vsn);
579 mmc_card_set_bad(card);
580 break;
581 }
1da177e4
LT
582 }
583}
584
585/*
586 * Given a 128-bit response, decode to our card CSD structure.
587 */
588static void mmc_decode_csd(struct mmc_card *card)
589{
590 struct mmc_csd *csd = &card->csd;
591 unsigned int e, m, csd_struct;
592 u32 *resp = card->raw_csd;
593
335eadf2
PO
594 if (mmc_card_sd(card)) {
595 csd_struct = UNSTUFF_BITS(resp, 126, 2);
fba68bd2
PL
596
597 switch (csd_struct) {
598 case 0:
599 m = UNSTUFF_BITS(resp, 115, 4);
600 e = UNSTUFF_BITS(resp, 112, 3);
601 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
602 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
603
604 m = UNSTUFF_BITS(resp, 99, 4);
605 e = UNSTUFF_BITS(resp, 96, 3);
606 csd->max_dtr = tran_exp[e] * tran_mant[m];
607 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
608
609 e = UNSTUFF_BITS(resp, 47, 3);
610 m = UNSTUFF_BITS(resp, 62, 12);
611 csd->capacity = (1 + m) << (e + 2);
612
613 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
614 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
615 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
616 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
617 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
618 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
619 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
620 break;
621 case 1:
622 /*
623 * This is a block-addressed SDHC card. Most
624 * interesting fields are unused and have fixed
625 * values. To avoid getting tripped by buggy cards,
626 * we assume those fixed values ourselves.
627 */
628 mmc_card_set_blockaddr(card);
629
630 csd->tacc_ns = 0; /* Unused */
631 csd->tacc_clks = 0; /* Unused */
632
633 m = UNSTUFF_BITS(resp, 99, 4);
634 e = UNSTUFF_BITS(resp, 96, 3);
635 csd->max_dtr = tran_exp[e] * tran_mant[m];
636 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
637
638 m = UNSTUFF_BITS(resp, 48, 22);
639 csd->capacity = (1 + m) << 10;
640
641 csd->read_blkbits = 9;
642 csd->read_partial = 0;
643 csd->write_misalign = 0;
644 csd->read_misalign = 0;
645 csd->r2w_factor = 4; /* Unused */
646 csd->write_blkbits = 9;
647 csd->write_partial = 0;
648 break;
649 default:
335eadf2
PO
650 printk("%s: unrecognised CSD structure version %d\n",
651 mmc_hostname(card->host), csd_struct);
652 mmc_card_set_bad(card);
653 return;
654 }
a00fc090 655 } else {
335eadf2
PO
656 /*
657 * We only understand CSD structure v1.1 and v1.2.
658 * v1.2 has extra information in bits 15, 11 and 10.
659 */
660 csd_struct = UNSTUFF_BITS(resp, 126, 2);
661 if (csd_struct != 1 && csd_struct != 2) {
662 printk("%s: unrecognised CSD structure version %d\n",
663 mmc_hostname(card->host), csd_struct);
664 mmc_card_set_bad(card);
665 return;
666 }
1da177e4 667
335eadf2
PO
668 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
669 m = UNSTUFF_BITS(resp, 115, 4);
670 e = UNSTUFF_BITS(resp, 112, 3);
671 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
672 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
1da177e4 673
335eadf2
PO
674 m = UNSTUFF_BITS(resp, 99, 4);
675 e = UNSTUFF_BITS(resp, 96, 3);
676 csd->max_dtr = tran_exp[e] * tran_mant[m];
677 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
1da177e4 678
335eadf2
PO
679 e = UNSTUFF_BITS(resp, 47, 3);
680 m = UNSTUFF_BITS(resp, 62, 12);
681 csd->capacity = (1 + m) << (e + 2);
1da177e4 682
335eadf2 683 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
a6f6c96b
RK
684 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
685 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
686 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
37be4e78 687 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
a6f6c96b
RK
688 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
689 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
335eadf2 690 }
1da177e4
LT
691}
692
b57c43ad
PO
693/*
694 * Given a 64-bit response, decode to our card SCR structure.
695 */
696static void mmc_decode_scr(struct mmc_card *card)
697{
698 struct sd_scr *scr = &card->scr;
699 unsigned int scr_struct;
700 u32 resp[4];
701
702 BUG_ON(!mmc_card_sd(card));
703
704 resp[3] = card->raw_scr[1];
705 resp[2] = card->raw_scr[0];
706
707 scr_struct = UNSTUFF_BITS(resp, 60, 4);
708 if (scr_struct != 0) {
709 printk("%s: unrecognised SCR structure version %d\n",
710 mmc_hostname(card->host), scr_struct);
711 mmc_card_set_bad(card);
712 return;
713 }
714
715 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
716 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
717}
718
1da177e4
LT
719/*
720 * Locate a MMC card on this MMC host given a raw CID.
721 */
722static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
723{
724 struct mmc_card *card;
725
726 list_for_each_entry(card, &host->cards, node) {
727 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
728 return card;
729 }
730 return NULL;
731}
732
733/*
734 * Allocate a new MMC card, and assign a unique RCA.
735 */
736static struct mmc_card *
737mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
738{
739 struct mmc_card *card, *c;
740 unsigned int rca = *frca;
741
742 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
743 if (!card)
744 return ERR_PTR(-ENOMEM);
745
746 mmc_init_card(card, host);
747 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
748
749 again:
750 list_for_each_entry(c, &host->cards, node)
751 if (c->rca == rca) {
752 rca++;
753 goto again;
754 }
755
756 card->rca = rca;
757
758 *frca = rca;
759
760 return card;
761}
762
763/*
764 * Tell attached cards to go to IDLE state
765 */
766static void mmc_idle_cards(struct mmc_host *host)
767{
768 struct mmc_command cmd;
769
865e9f13 770 host->ios.chip_select = MMC_CS_HIGH;
920e70c5 771 mmc_set_ios(host);
865e9f13
PO
772
773 mmc_delay(1);
774
1da177e4
LT
775 cmd.opcode = MMC_GO_IDLE_STATE;
776 cmd.arg = 0;
e9225176 777 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
1da177e4
LT
778
779 mmc_wait_for_cmd(host, &cmd, 0);
780
781 mmc_delay(1);
865e9f13
PO
782
783 host->ios.chip_select = MMC_CS_DONTCARE;
920e70c5 784 mmc_set_ios(host);
865e9f13
PO
785
786 mmc_delay(1);
1da177e4
LT
787}
788
789/*
45f8245b
RK
790 * Apply power to the MMC stack. This is a two-stage process.
791 * First, we enable power to the card without the clock running.
792 * We then wait a bit for the power to stabilise. Finally,
793 * enable the bus drivers and clock to the card.
794 *
795 * We must _NOT_ enable the clock prior to power stablising.
796 *
797 * If a host does all the power sequencing itself, ignore the
798 * initial MMC_POWER_UP stage.
1da177e4
LT
799 */
800static void mmc_power_up(struct mmc_host *host)
801{
802 int bit = fls(host->ocr_avail) - 1;
803
804 host->ios.vdd = bit;
805 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 806 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 807 host->ios.power_mode = MMC_POWER_UP;
f218278a 808 host->ios.bus_width = MMC_BUS_WIDTH_1;
920e70c5 809 mmc_set_ios(host);
1da177e4
LT
810
811 mmc_delay(1);
812
813 host->ios.clock = host->f_min;
814 host->ios.power_mode = MMC_POWER_ON;
920e70c5 815 mmc_set_ios(host);
1da177e4
LT
816
817 mmc_delay(2);
818}
819
820static void mmc_power_off(struct mmc_host *host)
821{
822 host->ios.clock = 0;
823 host->ios.vdd = 0;
824 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 825 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 826 host->ios.power_mode = MMC_POWER_OFF;
f218278a 827 host->ios.bus_width = MMC_BUS_WIDTH_1;
920e70c5 828 mmc_set_ios(host);
1da177e4
LT
829}
830
831static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
832{
833 struct mmc_command cmd;
834 int i, err = 0;
835
836 cmd.opcode = MMC_SEND_OP_COND;
837 cmd.arg = ocr;
e9225176 838 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
1da177e4
LT
839
840 for (i = 100; i; i--) {
841 err = mmc_wait_for_cmd(host, &cmd, 0);
842 if (err != MMC_ERR_NONE)
843 break;
844
845 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
846 break;
847
848 err = MMC_ERR_TIMEOUT;
849
850 mmc_delay(10);
851 }
852
853 if (rocr)
854 *rocr = cmd.resp[0];
855
856 return err;
857}
858
335eadf2
PO
859static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
860{
861 struct mmc_command cmd;
862 int i, err = 0;
863
864 cmd.opcode = SD_APP_OP_COND;
865 cmd.arg = ocr;
e9225176 866 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
335eadf2
PO
867
868 for (i = 100; i; i--) {
869 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
870 if (err != MMC_ERR_NONE)
871 break;
872
873 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
874 break;
875
876 err = MMC_ERR_TIMEOUT;
877
878 mmc_delay(10);
879 }
880
881 if (rocr)
882 *rocr = cmd.resp[0];
883
884 return err;
885}
886
fba68bd2
PL
887static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
888{
889 struct mmc_command cmd;
890 int err, sd2;
891 static const u8 test_pattern = 0xAA;
892
893 /*
894 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
895 * before SD_APP_OP_COND. This command will harmlessly fail for
896 * SD 1.0 cards.
897 */
898 cmd.opcode = SD_SEND_IF_COND;
899 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
900 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
901
902 err = mmc_wait_for_cmd(host, &cmd, 0);
903 if (err == MMC_ERR_NONE) {
904 if ((cmd.resp[0] & 0xFF) == test_pattern) {
905 sd2 = 1;
906 } else {
907 sd2 = 0;
908 err = MMC_ERR_FAILED;
909 }
910 } else {
911 /*
912 * Treat errors as SD 1.0 card.
913 */
914 sd2 = 0;
915 err = MMC_ERR_NONE;
916 }
917 if (rsd2)
918 *rsd2 = sd2;
919 return err;
920}
921
1da177e4
LT
922/*
923 * Discover cards by requesting their CID. If this command
924 * times out, it is not an error; there are no further cards
925 * to be discovered. Add new cards to the list.
926 *
927 * Create a mmc_card entry for each discovered card, assigning
928 * it an RCA, and save the raw CID for decoding later.
929 */
930static void mmc_discover_cards(struct mmc_host *host)
931{
932 struct mmc_card *card;
933 unsigned int first_rca = 1, err;
934
935 while (1) {
936 struct mmc_command cmd;
937
938 cmd.opcode = MMC_ALL_SEND_CID;
939 cmd.arg = 0;
e9225176 940 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1da177e4
LT
941
942 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
943 if (err == MMC_ERR_TIMEOUT) {
944 err = MMC_ERR_NONE;
945 break;
946 }
947 if (err != MMC_ERR_NONE) {
948 printk(KERN_ERR "%s: error requesting CID: %d\n",
d366b643 949 mmc_hostname(host), err);
1da177e4
LT
950 break;
951 }
952
953 card = mmc_find_card(host, cmd.resp);
954 if (!card) {
955 card = mmc_alloc_card(host, cmd.resp, &first_rca);
956 if (IS_ERR(card)) {
957 err = PTR_ERR(card);
958 break;
959 }
960 list_add(&card->node, &host->cards);
961 }
962
963 card->state &= ~MMC_STATE_DEAD;
964
335eadf2
PO
965 if (host->mode == MMC_MODE_SD) {
966 mmc_card_set_sd(card);
1da177e4 967
335eadf2
PO
968 cmd.opcode = SD_SEND_RELATIVE_ADDR;
969 cmd.arg = 0;
e9225176 970 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
335eadf2
PO
971
972 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
973 if (err != MMC_ERR_NONE)
974 mmc_card_set_dead(card);
a00fc090 975 else {
335eadf2 976 card->rca = cmd.resp[0] >> 16;
a00fc090
PO
977
978 if (!host->ops->get_ro) {
979 printk(KERN_WARNING "%s: host does not "
980 "support reading read-only "
981 "switch. assuming write-enable.\n",
982 mmc_hostname(host));
983 } else {
984 if (host->ops->get_ro(host))
985 mmc_card_set_readonly(card);
986 }
987 }
988 } else {
335eadf2
PO
989 cmd.opcode = MMC_SET_RELATIVE_ADDR;
990 cmd.arg = card->rca << 16;
e9225176 991 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
335eadf2
PO
992
993 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
994 if (err != MMC_ERR_NONE)
995 mmc_card_set_dead(card);
996 }
1da177e4
LT
997 }
998}
999
1000static void mmc_read_csds(struct mmc_host *host)
1001{
1002 struct mmc_card *card;
1003
1004 list_for_each_entry(card, &host->cards, node) {
1005 struct mmc_command cmd;
1006 int err;
1007
1008 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1009 continue;
1010
1011 cmd.opcode = MMC_SEND_CSD;
1012 cmd.arg = card->rca << 16;
e9225176 1013 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
1da177e4
LT
1014
1015 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1016 if (err != MMC_ERR_NONE) {
1017 mmc_card_set_dead(card);
1018 continue;
1019 }
1020
1021 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
1022
1023 mmc_decode_csd(card);
1024 mmc_decode_cid(card);
1025 }
1026}
1027
bce40a36
PL
1028static void mmc_process_ext_csds(struct mmc_host *host)
1029{
1030 int err;
1031 struct mmc_card *card;
1032
1033 struct mmc_request mrq;
1034 struct mmc_command cmd;
1035 struct mmc_data data;
1036
1037 struct scatterlist sg;
1038
1039 /*
1040 * As the ext_csd is so large and mostly unused, we don't store the
1041 * raw block in mmc_card.
1042 */
1043 u8 *ext_csd;
1044 ext_csd = kmalloc(512, GFP_KERNEL);
1045 if (!ext_csd) {
1046 printk("%s: could not allocate a buffer to receive the ext_csd."
1047 "mmc v4 cards will be treated as v3.\n",
1048 mmc_hostname(host));
1049 return;
1050 }
1051
1052 list_for_each_entry(card, &host->cards, node) {
1053 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1054 continue;
1055 if (mmc_card_sd(card))
1056 continue;
1057 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
1058 continue;
1059
1060 err = mmc_select_card(host, card);
1061 if (err != MMC_ERR_NONE) {
1062 mmc_card_set_dead(card);
1063 continue;
1064 }
1065
1066 memset(&cmd, 0, sizeof(struct mmc_command));
1067
1068 cmd.opcode = MMC_SEND_EXT_CSD;
1069 cmd.arg = 0;
1070 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1071
1072 memset(&data, 0, sizeof(struct mmc_data));
1073
1074 mmc_set_data_timeout(&data, card, 0);
1075
1076 data.blksz = 512;
1077 data.blocks = 1;
1078 data.flags = MMC_DATA_READ;
1079 data.sg = &sg;
1080 data.sg_len = 1;
1081
1082 memset(&mrq, 0, sizeof(struct mmc_request));
1083
1084 mrq.cmd = &cmd;
1085 mrq.data = &data;
1086
1087 sg_init_one(&sg, ext_csd, 512);
1088
1089 mmc_wait_for_req(host, &mrq);
1090
1091 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1092 mmc_card_set_dead(card);
1093 continue;
1094 }
1095
1096 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1097 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1098 card->ext_csd.hs_max_dtr = 52000000;
1099 break;
1100 case EXT_CSD_CARD_TYPE_26:
1101 card->ext_csd.hs_max_dtr = 26000000;
1102 break;
1103 default:
1104 /* MMC v4 spec says this cannot happen */
1105 printk("%s: card is mmc v4 but doesn't support "
1106 "any high-speed modes.\n",
1107 mmc_hostname(card->host));
1108 mmc_card_set_bad(card);
1109 continue;
1110 }
1111
1112 /* Activate highspeed support. */
1113 cmd.opcode = MMC_SWITCH;
1114 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1115 (EXT_CSD_HS_TIMING << 16) |
1116 (1 << 8) |
1117 EXT_CSD_CMD_SET_NORMAL;
1118 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1119
1120 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1121 if (err != MMC_ERR_NONE) {
1122 printk("%s: failed to switch card to mmc v4 "
1123 "high-speed mode.\n",
1124 mmc_hostname(card->host));
1125 continue;
1126 }
1127
1128 mmc_card_set_highspeed(card);
e45a1bd2
PL
1129
1130 /* Check for host support for wide-bus modes. */
1131 if (!(host->caps & MMC_CAP_4_BIT_DATA)) {
1132 continue;
1133 }
1134
1135 /* Activate 4-bit support. */
1136 cmd.opcode = MMC_SWITCH;
1137 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1138 (EXT_CSD_BUS_WIDTH << 16) |
1139 (EXT_CSD_BUS_WIDTH_4 << 8) |
1140 EXT_CSD_CMD_SET_NORMAL;
1141 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1142
1143 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1144 if (err != MMC_ERR_NONE) {
1145 printk("%s: failed to switch card to "
1146 "mmc v4 4-bit bus mode.\n",
1147 mmc_hostname(card->host));
1148 continue;
1149 }
1150
1151 host->ios.bus_width = MMC_BUS_WIDTH_4;
bce40a36
PL
1152 }
1153
1154 kfree(ext_csd);
1155
1156 mmc_deselect_cards(host);
1157}
1158
b57c43ad
PO
1159static void mmc_read_scrs(struct mmc_host *host)
1160{
1161 int err;
1162 struct mmc_card *card;
b57c43ad
PO
1163 struct mmc_request mrq;
1164 struct mmc_command cmd;
1165 struct mmc_data data;
b57c43ad
PO
1166 struct scatterlist sg;
1167
1168 list_for_each_entry(card, &host->cards, node) {
1169 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1170 continue;
1171 if (!mmc_card_sd(card))
1172 continue;
1173
1174 err = mmc_select_card(host, card);
1175 if (err != MMC_ERR_NONE) {
1176 mmc_card_set_dead(card);
1177 continue;
1178 }
1179
1180 memset(&cmd, 0, sizeof(struct mmc_command));
1181
1182 cmd.opcode = MMC_APP_CMD;
1183 cmd.arg = card->rca << 16;
e9225176 1184 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
b57c43ad
PO
1185
1186 err = mmc_wait_for_cmd(host, &cmd, 0);
1187 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1188 mmc_card_set_dead(card);
1189 continue;
1190 }
1191
1192 memset(&cmd, 0, sizeof(struct mmc_command));
1193
1194 cmd.opcode = SD_APP_SEND_SCR;
1195 cmd.arg = 0;
e9225176 1196 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
b57c43ad
PO
1197
1198 memset(&data, 0, sizeof(struct mmc_data));
1199
d773d725 1200 mmc_set_data_timeout(&data, card, 0);
385e3227 1201
2c171bf1 1202 data.blksz = 1 << 3;
b57c43ad
PO
1203 data.blocks = 1;
1204 data.flags = MMC_DATA_READ;
1205 data.sg = &sg;
1206 data.sg_len = 1;
1207
1208 memset(&mrq, 0, sizeof(struct mmc_request));
1209
1210 mrq.cmd = &cmd;
1211 mrq.data = &data;
1212
1213 sg_init_one(&sg, (u8*)card->raw_scr, 8);
1214
e781de44
PO
1215 mmc_wait_for_req(host, &mrq);
1216
1217 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
b57c43ad
PO
1218 mmc_card_set_dead(card);
1219 continue;
1220 }
1221
1222 card->raw_scr[0] = ntohl(card->raw_scr[0]);
1223 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1224
1225 mmc_decode_scr(card);
1226 }
1227
1228 mmc_deselect_cards(host);
1229}
1230
7ccd266e
PO
1231static void mmc_read_switch_caps(struct mmc_host *host)
1232{
1233 int err;
1234 struct mmc_card *card;
1235 struct mmc_request mrq;
1236 struct mmc_command cmd;
1237 struct mmc_data data;
1238 unsigned char *status;
1239 struct scatterlist sg;
1240
1241 status = kmalloc(64, GFP_KERNEL);
1242 if (!status) {
1243 printk(KERN_WARNING "%s: Unable to allocate buffer for "
1244 "reading switch capabilities.\n",
1245 mmc_hostname(host));
1246 return;
1247 }
1248
1249 list_for_each_entry(card, &host->cards, node) {
1250 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1251 continue;
1252 if (!mmc_card_sd(card))
1253 continue;
1254 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
1255 continue;
1256
1257 err = mmc_select_card(host, card);
1258 if (err != MMC_ERR_NONE) {
1259 mmc_card_set_dead(card);
1260 continue;
1261 }
1262
1263 memset(&cmd, 0, sizeof(struct mmc_command));
1264
1265 cmd.opcode = SD_SWITCH;
1266 cmd.arg = 0x00FFFFF1;
1267 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1268
1269 memset(&data, 0, sizeof(struct mmc_data));
1270
1271 mmc_set_data_timeout(&data, card, 0);
1272
1273 data.blksz = 64;
1274 data.blocks = 1;
1275 data.flags = MMC_DATA_READ;
1276 data.sg = &sg;
1277 data.sg_len = 1;
1278
1279 memset(&mrq, 0, sizeof(struct mmc_request));
1280
1281 mrq.cmd = &cmd;
1282 mrq.data = &data;
1283
1284 sg_init_one(&sg, status, 64);
1285
1286 mmc_wait_for_req(host, &mrq);
1287
1288 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1289 mmc_card_set_dead(card);
1290 continue;
1291 }
1292
1293 if (status[13] & 0x02)
1294 card->sw_caps.hs_max_dtr = 50000000;
1295
1296 memset(&cmd, 0, sizeof(struct mmc_command));
1297
1298 cmd.opcode = SD_SWITCH;
1299 cmd.arg = 0x80FFFFF1;
1300 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1301
1302 memset(&data, 0, sizeof(struct mmc_data));
1303
1304 mmc_set_data_timeout(&data, card, 0);
1305
1306 data.blksz = 64;
1307 data.blocks = 1;
1308 data.flags = MMC_DATA_READ;
1309 data.sg = &sg;
1310 data.sg_len = 1;
1311
1312 memset(&mrq, 0, sizeof(struct mmc_request));
1313
1314 mrq.cmd = &cmd;
1315 mrq.data = &data;
1316
1317 sg_init_one(&sg, status, 64);
1318
1319 mmc_wait_for_req(host, &mrq);
1320
1321 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1322 mmc_card_set_dead(card);
1323 continue;
1324 }
1325
1326 if ((status[16] & 0xF) != 1) {
1327 printk(KERN_WARNING "%s: Problem switching card "
1328 "into high-speed mode!\n",
1329 mmc_hostname(host));
1330 continue;
1331 }
1332
1333 mmc_card_set_highspeed(card);
1334 }
1335
1336 kfree(status);
1337
1338 mmc_deselect_cards(host);
1339}
1340
1da177e4
LT
1341static unsigned int mmc_calculate_clock(struct mmc_host *host)
1342{
1343 struct mmc_card *card;
1344 unsigned int max_dtr = host->f_max;
1345
1346 list_for_each_entry(card, &host->cards, node)
bce40a36 1347 if (!mmc_card_dead(card)) {
7ccd266e
PO
1348 if (mmc_card_highspeed(card) && mmc_card_sd(card)) {
1349 if (max_dtr > card->sw_caps.hs_max_dtr)
1350 max_dtr = card->sw_caps.hs_max_dtr;
1351 } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) {
bce40a36 1352 if (max_dtr > card->ext_csd.hs_max_dtr)
7ccd266e 1353 max_dtr = card->ext_csd.hs_max_dtr;
bce40a36
PL
1354 } else if (max_dtr > card->csd.max_dtr) {
1355 max_dtr = card->csd.max_dtr;
1356 }
1357 }
1da177e4 1358
920e70c5
RK
1359 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1360 mmc_hostname(host),
c6563178 1361 max_dtr / 1000000, (max_dtr / 1000) % 1000);
1da177e4
LT
1362
1363 return max_dtr;
1364}
1365
1366/*
1367 * Check whether cards we already know about are still present.
1368 * We do this by requesting status, and checking whether a card
1369 * responds.
1370 *
1371 * A request for status does not cause a state change in data
1372 * transfer mode.
1373 */
1374static void mmc_check_cards(struct mmc_host *host)
1375{
1376 struct list_head *l, *n;
1377
1378 mmc_deselect_cards(host);
1379
1380 list_for_each_safe(l, n, &host->cards) {
1381 struct mmc_card *card = mmc_list_to_card(l);
1382 struct mmc_command cmd;
1383 int err;
1384
1385 cmd.opcode = MMC_SEND_STATUS;
1386 cmd.arg = card->rca << 16;
e9225176 1387 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1da177e4
LT
1388
1389 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1390 if (err == MMC_ERR_NONE)
1391 continue;
1392
1393 mmc_card_set_dead(card);
1394 }
1395}
1396
1397static void mmc_setup(struct mmc_host *host)
1398{
1399 if (host->ios.power_mode != MMC_POWER_ON) {
1400 int err;
1401 u32 ocr;
1402
a00fc090 1403 host->mode = MMC_MODE_SD;
335eadf2 1404
1da177e4
LT
1405 mmc_power_up(host);
1406 mmc_idle_cards(host);
1407
fba68bd2
PL
1408 err = mmc_send_if_cond(host, host->ocr_avail, NULL);
1409 if (err != MMC_ERR_NONE) {
1410 return;
1411 }
a00fc090 1412 err = mmc_send_app_op_cond(host, 0, &ocr);
335eadf2
PO
1413
1414 /*
a00fc090
PO
1415 * If we fail to detect any SD cards then try
1416 * searching for MMC cards.
335eadf2 1417 */
a00fc090
PO
1418 if (err != MMC_ERR_NONE) {
1419 host->mode = MMC_MODE_MMC;
1420
1421 err = mmc_send_op_cond(host, 0, &ocr);
335eadf2
PO
1422 if (err != MMC_ERR_NONE)
1423 return;
335eadf2 1424 }
1da177e4
LT
1425
1426 host->ocr = mmc_select_voltage(host, ocr);
1427
1428 /*
1429 * Since we're changing the OCR value, we seem to
1430 * need to tell some cards to go back to the idle
1431 * state. We wait 1ms to give cards time to
1432 * respond.
1433 */
1434 if (host->ocr)
1435 mmc_idle_cards(host);
1436 } else {
1437 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1438 host->ios.clock = host->f_min;
920e70c5 1439 mmc_set_ios(host);
1da177e4
LT
1440
1441 /*
1442 * We should remember the OCR mask from the existing
1443 * cards, and detect the new cards OCR mask, combine
1444 * the two and re-select the VDD. However, if we do
1445 * change VDD, we should do an idle, and then do a
1446 * full re-initialisation. We would need to notify
1447 * drivers so that they can re-setup the cards as
1448 * well, while keeping their queues at bay.
1449 *
1450 * For the moment, we take the easy way out - if the
1451 * new cards don't like our currently selected VDD,
1452 * they drop off the bus.
1453 */
1454 }
1455
1456 if (host->ocr == 0)
1457 return;
1458
1459 /*
1460 * Send the selected OCR multiple times... until the cards
1461 * all get the idea that they should be ready for CMD2.
1462 * (My SanDisk card seems to need this.)
1463 */
fba68bd2
PL
1464 if (host->mode == MMC_MODE_SD) {
1465 int err, sd2;
1466 err = mmc_send_if_cond(host, host->ocr, &sd2);
1467 if (err == MMC_ERR_NONE) {
1468 /*
1469 * If SD_SEND_IF_COND indicates an SD 2.0
1470 * compliant card and we should set bit 30
1471 * of the ocr to indicate that we can handle
1472 * block-addressed SDHC cards.
1473 */
1474 mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);
1475 }
1476 } else {
335eadf2 1477 mmc_send_op_cond(host, host->ocr, NULL);
fba68bd2 1478 }
1da177e4
LT
1479
1480 mmc_discover_cards(host);
1481
1482 /*
1483 * Ok, now switch to push-pull mode.
1484 */
1485 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
920e70c5 1486 mmc_set_ios(host);
1da177e4
LT
1487
1488 mmc_read_csds(host);
b57c43ad 1489
7ccd266e 1490 if (host->mode == MMC_MODE_SD) {
b57c43ad 1491 mmc_read_scrs(host);
7ccd266e
PO
1492 mmc_read_switch_caps(host);
1493 } else
bce40a36 1494 mmc_process_ext_csds(host);
1da177e4
LT
1495}
1496
1497
1498/**
1499 * mmc_detect_change - process change of state on a MMC socket
1500 * @host: host which changed state.
8dc00335 1501 * @delay: optional delay to wait before detection (jiffies)
1da177e4
LT
1502 *
1503 * All we know is that card(s) have been inserted or removed
1504 * from the socket(s). We don't know which socket or cards.
1505 */
8dc00335 1506void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1507{
c4028958 1508 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
1509}
1510
1511EXPORT_SYMBOL(mmc_detect_change);
1512
1513
c4028958 1514static void mmc_rescan(struct work_struct *work)
1da177e4 1515{
c4028958
DH
1516 struct mmc_host *host =
1517 container_of(work, struct mmc_host, detect.work);
1da177e4 1518 struct list_head *l, *n;
25a122fd 1519 unsigned char power_mode;
1da177e4
LT
1520
1521 mmc_claim_host(host);
1522
25a122fd
TT
1523 /*
1524 * Check for removed cards and newly inserted ones. We check for
1525 * removed cards first so we can intelligently re-select the VDD.
1526 */
1527 power_mode = host->ios.power_mode;
1528 if (power_mode == MMC_POWER_ON)
1da177e4
LT
1529 mmc_check_cards(host);
1530
1531 mmc_setup(host);
1532
25a122fd
TT
1533 /*
1534 * Some broken cards process CMD1 even in stand-by state. There is
1535 * no reply, but an ILLEGAL_COMMAND error is cached and returned
1536 * after next command. We poll for card status here to clear any
1537 * possibly pending error.
1538 */
1539 if (power_mode == MMC_POWER_ON)
1540 mmc_check_cards(host);
1541
1da177e4
LT
1542 if (!list_empty(&host->cards)) {
1543 /*
1544 * (Re-)calculate the fastest clock rate which the
1545 * attached cards and the host support.
1546 */
1547 host->ios.clock = mmc_calculate_clock(host);
920e70c5 1548 mmc_set_ios(host);
1da177e4
LT
1549 }
1550
1551 mmc_release_host(host);
1552
1553 list_for_each_safe(l, n, &host->cards) {
1554 struct mmc_card *card = mmc_list_to_card(l);
1555
1556 /*
1557 * If this is a new and good card, register it.
1558 */
1559 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1560 if (mmc_register_card(card))
1561 mmc_card_set_dead(card);
1562 else
1563 mmc_card_set_present(card);
1564 }
1565
1566 /*
1567 * If this card is dead, destroy it.
1568 */
1569 if (mmc_card_dead(card)) {
1570 list_del(&card->node);
1571 mmc_remove_card(card);
1572 }
1573 }
1574
1575 /*
1576 * If we discover that there are no cards on the
1577 * bus, turn off the clock and power down.
1578 */
1579 if (list_empty(&host->cards))
1580 mmc_power_off(host);
1581}
1582
1583
1584/**
1585 * mmc_alloc_host - initialise the per-host structure.
1586 * @extra: sizeof private data structure
1587 * @dev: pointer to host device model structure
1588 *
1589 * Initialise the per-host structure.
1590 */
1591struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1592{
1593 struct mmc_host *host;
1594
00b137cf 1595 host = mmc_alloc_host_sysfs(extra, dev);
1da177e4 1596 if (host) {
1da177e4
LT
1597 spin_lock_init(&host->lock);
1598 init_waitqueue_head(&host->wq);
1599 INIT_LIST_HEAD(&host->cards);
c4028958 1600 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1da177e4 1601
1da177e4
LT
1602 /*
1603 * By default, hosts do not support SGIO or large requests.
1604 * They have to set these according to their abilities.
1605 */
1606 host->max_hw_segs = 1;
1607 host->max_phys_segs = 1;
1608 host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
1609 host->max_seg_size = PAGE_CACHE_SIZE;
fe4a3c7a
PO
1610
1611 host->max_blk_size = 512;
1da177e4
LT
1612 }
1613
1614 return host;
1615}
1616
1617EXPORT_SYMBOL(mmc_alloc_host);
1618
1619/**
1620 * mmc_add_host - initialise host hardware
1621 * @host: mmc host
1622 */
1623int mmc_add_host(struct mmc_host *host)
1624{
00b137cf 1625 int ret;
1da177e4 1626
00b137cf
RK
1627 ret = mmc_add_host_sysfs(host);
1628 if (ret == 0) {
1629 mmc_power_off(host);
8dc00335 1630 mmc_detect_change(host, 0);
00b137cf 1631 }
1da177e4 1632
00b137cf 1633 return ret;
1da177e4
LT
1634}
1635
1636EXPORT_SYMBOL(mmc_add_host);
1637
1638/**
1639 * mmc_remove_host - remove host hardware
1640 * @host: mmc host
1641 *
1642 * Unregister and remove all cards associated with this host,
1643 * and power down the MMC bus.
1644 */
1645void mmc_remove_host(struct mmc_host *host)
1646{
1647 struct list_head *l, *n;
1648
1649 list_for_each_safe(l, n, &host->cards) {
1650 struct mmc_card *card = mmc_list_to_card(l);
1651
1652 mmc_remove_card(card);
1653 }
1654
1655 mmc_power_off(host);
00b137cf 1656 mmc_remove_host_sysfs(host);
1da177e4
LT
1657}
1658
1659EXPORT_SYMBOL(mmc_remove_host);
1660
1661/**
1662 * mmc_free_host - free the host structure
1663 * @host: mmc host
1664 *
1665 * Free the host once all references to it have been dropped.
1666 */
1667void mmc_free_host(struct mmc_host *host)
1668{
7104e2d5 1669 mmc_flush_scheduled_work();
00b137cf 1670 mmc_free_host_sysfs(host);
1da177e4
LT
1671}
1672
1673EXPORT_SYMBOL(mmc_free_host);
1674
1675#ifdef CONFIG_PM
1676
1677/**
1678 * mmc_suspend_host - suspend a host
1679 * @host: mmc host
1680 * @state: suspend mode (PM_SUSPEND_xxx)
1681 */
e5378ca8 1682int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1da177e4
LT
1683{
1684 mmc_claim_host(host);
1685 mmc_deselect_cards(host);
1686 mmc_power_off(host);
1687 mmc_release_host(host);
1688
1689 return 0;
1690}
1691
1692EXPORT_SYMBOL(mmc_suspend_host);
1693
1694/**
1695 * mmc_resume_host - resume a previously suspended host
1696 * @host: mmc host
1697 */
1698int mmc_resume_host(struct mmc_host *host)
1699{
c4028958 1700 mmc_rescan(&host->detect.work);
1da177e4
LT
1701
1702 return 0;
1703}
1704
1705EXPORT_SYMBOL(mmc_resume_host);
1706
1707#endif
1708
1709MODULE_LICENSE("GPL");