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