wbsd: check for data opcode earlier
[linux-block.git] / drivers / mmc / core / core.c
CommitLineData
1da177e4 1/*
aaac1b47 2 * linux/drivers/mmc/core/core.c
1da177e4
LT
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
b855885e 6 * Copyright (C) 2005-2007 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>
da7fbe58
PO
26#include <linux/mmc/mmc.h>
27#include <linux/mmc/sd.h>
1da177e4 28
aaac1b47 29#include "core.h"
da7fbe58
PO
30#include "sysfs.h"
31
32#include "mmc_ops.h"
33#include "sd_ops.h"
1da177e4 34
1da177e4
LT
35#define CMD_RETRIES 3
36
37/*
38 * OCR Bit positions to 10s of Vdd mV.
39 */
40static const unsigned short mmc_ocr_bit_to_vdd[] = {
41 150, 155, 160, 165, 170, 180, 190, 200,
42 210, 220, 230, 240, 250, 260, 270, 280,
43 290, 300, 310, 320, 330, 340, 350, 360
44};
45
46static const unsigned int tran_exp[] = {
47 10000, 100000, 1000000, 10000000,
48 0, 0, 0, 0
49};
50
51static const unsigned char tran_mant[] = {
52 0, 10, 12, 13, 15, 20, 25, 30,
53 35, 40, 45, 50, 55, 60, 70, 80,
54};
55
56static const unsigned int tacc_exp[] = {
57 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
58};
59
60static const unsigned int tacc_mant[] = {
61 0, 10, 12, 13, 15, 20, 25, 30,
62 35, 40, 45, 50, 55, 60, 70, 80,
63};
64
65
66/**
fe10c6ab
RK
67 * mmc_request_done - finish processing an MMC request
68 * @host: MMC host which completed request
69 * @mrq: MMC request which request
1da177e4
LT
70 *
71 * MMC drivers should call this function when they have completed
fe10c6ab 72 * their processing of a request.
1da177e4
LT
73 */
74void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
75{
76 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
77 int err = cmd->error;
78
79 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
80 mmc_hostname(host), cmd->opcode, err,
81 mrq->data ? mrq->data->error : 0,
82 mrq->stop ? mrq->stop->error : 0,
83 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
1da177e4
LT
84
85 if (err && cmd->retries) {
86 cmd->retries--;
87 cmd->error = 0;
88 host->ops->request(host, mrq);
89 } else if (mrq->done) {
90 mrq->done(mrq);
91 }
92}
93
94EXPORT_SYMBOL(mmc_request_done);
95
96/**
97 * mmc_start_request - start a command on a host
98 * @host: MMC host to start command on
99 * @mrq: MMC request to start
100 *
101 * Queue a command on the specified host. We expect the
102 * caller to be holding the host lock with interrupts disabled.
103 */
104void
105mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
106{
976d9276
PO
107#ifdef CONFIG_MMC_DEBUG
108 unsigned int i, sz;
109#endif
110
920e70c5
RK
111 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
112 mmc_hostname(host), mrq->cmd->opcode,
113 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 114
f22ee4ed 115 WARN_ON(!host->claimed);
1da177e4
LT
116
117 mrq->cmd->error = 0;
118 mrq->cmd->mrq = mrq;
119 if (mrq->data) {
fe4a3c7a 120 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
121 BUG_ON(mrq->data->blocks > host->max_blk_count);
122 BUG_ON(mrq->data->blocks * mrq->data->blksz >
123 host->max_req_size);
fe4a3c7a 124
976d9276
PO
125#ifdef CONFIG_MMC_DEBUG
126 sz = 0;
127 for (i = 0;i < mrq->data->sg_len;i++)
128 sz += mrq->data->sg[i].length;
129 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
130#endif
131
1da177e4
LT
132 mrq->cmd->data = mrq->data;
133 mrq->data->error = 0;
134 mrq->data->mrq = mrq;
135 if (mrq->stop) {
136 mrq->data->stop = mrq->stop;
137 mrq->stop->error = 0;
138 mrq->stop->mrq = mrq;
139 }
140 }
141 host->ops->request(host, mrq);
142}
143
144EXPORT_SYMBOL(mmc_start_request);
145
146static void mmc_wait_done(struct mmc_request *mrq)
147{
148 complete(mrq->done_data);
149}
150
151int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
152{
0afffc72 153 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
154
155 mrq->done_data = &complete;
156 mrq->done = mmc_wait_done;
157
158 mmc_start_request(host, mrq);
159
160 wait_for_completion(&complete);
161
162 return 0;
163}
164
165EXPORT_SYMBOL(mmc_wait_for_req);
166
167/**
168 * mmc_wait_for_cmd - start a command and wait for completion
169 * @host: MMC host to start command
170 * @cmd: MMC command to start
171 * @retries: maximum number of retries
172 *
173 * Start a new MMC command for a host, and wait for the command
174 * to complete. Return any error that occurred while the command
175 * was executing. Do not attempt to parse the response.
176 */
177int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
178{
179 struct mmc_request mrq;
180
f22ee4ed 181 BUG_ON(!host->claimed);
1da177e4
LT
182
183 memset(&mrq, 0, sizeof(struct mmc_request));
184
185 memset(cmd->resp, 0, sizeof(cmd->resp));
186 cmd->retries = retries;
187
188 mrq.cmd = cmd;
189 cmd->data = NULL;
190
191 mmc_wait_for_req(host, &mrq);
192
193 return cmd->error;
194}
195
196EXPORT_SYMBOL(mmc_wait_for_cmd);
197
d773d725
RK
198/**
199 * mmc_set_data_timeout - set the timeout for a data command
200 * @data: data phase for command
201 * @card: the MMC card associated with the data transfer
202 * @write: flag to differentiate reads from writes
203 */
204void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
205 int write)
206{
207 unsigned int mult;
208
209 /*
210 * SD cards use a 100 multiplier rather than 10
211 */
212 mult = mmc_card_sd(card) ? 100 : 10;
213
214 /*
215 * Scale up the multiplier (and therefore the timeout) by
216 * the r2w factor for writes.
217 */
218 if (write)
219 mult <<= card->csd.r2w_factor;
220
221 data->timeout_ns = card->csd.tacc_ns * mult;
222 data->timeout_clks = card->csd.tacc_clks * mult;
223
224 /*
225 * SD cards also have an upper limit on the timeout.
226 */
227 if (mmc_card_sd(card)) {
228 unsigned int timeout_us, limit_us;
229
230 timeout_us = data->timeout_ns / 1000;
231 timeout_us += data->timeout_clks * 1000 /
232 (card->host->ios.clock / 1000);
233
234 if (write)
235 limit_us = 250000;
236 else
237 limit_us = 100000;
238
fba68bd2
PL
239 /*
240 * SDHC cards always use these fixed values.
241 */
242 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
243 data->timeout_ns = limit_us * 1000;
244 data->timeout_clks = 0;
245 }
246 }
247}
248EXPORT_SYMBOL(mmc_set_data_timeout);
249
1da177e4
LT
250/**
251 * __mmc_claim_host - exclusively claim a host
252 * @host: mmc host to claim
253 * @card: mmc card to claim host for
254 *
255 * Claim a host for a set of operations. If a valid card
256 * is passed and this wasn't the last card selected, select
257 * the card before returning.
258 *
259 * Note: you should use mmc_card_claim_host or mmc_claim_host.
260 */
b855885e 261void mmc_claim_host(struct mmc_host *host)
1da177e4
LT
262{
263 DECLARE_WAITQUEUE(wait, current);
264 unsigned long flags;
1da177e4
LT
265
266 add_wait_queue(&host->wq, &wait);
267 spin_lock_irqsave(&host->lock, flags);
268 while (1) {
269 set_current_state(TASK_UNINTERRUPTIBLE);
f22ee4ed 270 if (!host->claimed)
1da177e4
LT
271 break;
272 spin_unlock_irqrestore(&host->lock, flags);
273 schedule();
274 spin_lock_irqsave(&host->lock, flags);
275 }
276 set_current_state(TASK_RUNNING);
f22ee4ed 277 host->claimed = 1;
1da177e4
LT
278 spin_unlock_irqrestore(&host->lock, flags);
279 remove_wait_queue(&host->wq, &wait);
1da177e4
LT
280}
281
b855885e 282EXPORT_SYMBOL(mmc_claim_host);
1da177e4
LT
283
284/**
285 * mmc_release_host - release a host
286 * @host: mmc host to release
287 *
288 * Release a MMC host, allowing others to claim the host
289 * for their operations.
290 */
291void mmc_release_host(struct mmc_host *host)
292{
293 unsigned long flags;
294
f22ee4ed 295 BUG_ON(!host->claimed);
1da177e4
LT
296
297 spin_lock_irqsave(&host->lock, flags);
f22ee4ed 298 host->claimed = 0;
1da177e4
LT
299 spin_unlock_irqrestore(&host->lock, flags);
300
301 wake_up(&host->wq);
302}
303
304EXPORT_SYMBOL(mmc_release_host);
305
920e70c5
RK
306static inline void mmc_set_ios(struct mmc_host *host)
307{
308 struct mmc_ios *ios = &host->ios;
309
cd9277c0
PO
310 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
311 "width %u timing %u\n",
920e70c5
RK
312 mmc_hostname(host), ios->clock, ios->bus_mode,
313 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 314 ios->bus_width, ios->timing);
fba68bd2 315
920e70c5
RK
316 host->ops->set_ios(host, ios);
317}
318
da7fbe58 319void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 320{
da7fbe58
PO
321 host->ios.chip_select = mode;
322 mmc_set_ios(host);
1da177e4
LT
323}
324
325/*
326 * Mask off any voltages we don't support and select
327 * the lowest voltage
328 */
329static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
330{
331 int bit;
332
333 ocr &= host->ocr_avail;
334
335 bit = ffs(ocr);
336 if (bit) {
337 bit -= 1;
338
63ef731a 339 ocr &= 3 << bit;
1da177e4
LT
340
341 host->ios.vdd = bit;
920e70c5 342 mmc_set_ios(host);
1da177e4
LT
343 } else {
344 ocr = 0;
345 }
346
347 return ocr;
348}
349
350#define UNSTUFF_BITS(resp,start,size) \
351 ({ \
352 const int __size = size; \
353 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
354 const int __off = 3 - ((start) / 32); \
355 const int __shft = (start) & 31; \
356 u32 __res; \
357 \
358 __res = resp[__off] >> __shft; \
359 if (__size + __shft > 32) \
360 __res |= resp[__off-1] << ((32 - __shft) % 32); \
361 __res & __mask; \
362 })
363
364/*
365 * Given the decoded CSD structure, decode the raw CID to our CID structure.
366 */
367static void mmc_decode_cid(struct mmc_card *card)
368{
369 u32 *resp = card->raw_cid;
370
371 memset(&card->cid, 0, sizeof(struct mmc_cid));
372
335eadf2
PO
373 if (mmc_card_sd(card)) {
374 /*
375 * SD doesn't currently have a version field so we will
376 * have to assume we can parse this.
377 */
378 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
379 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
380 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
381 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
382 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
383 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
384 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
385 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
386 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
387 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
388 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
389 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
390
391 card->cid.year += 2000; /* SD cards year offset */
a00fc090 392 } else {
335eadf2
PO
393 /*
394 * The selection of the format here is based upon published
395 * specs from sandisk and from what people have reported.
396 */
397 switch (card->csd.mmca_vsn) {
398 case 0: /* MMC v1.0 - v1.2 */
399 case 1: /* MMC v1.4 */
400 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
401 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
402 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
403 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
404 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
405 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
406 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
407 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
408 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
409 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
410 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
411 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
412 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
413 break;
414
415 case 2: /* MMC v2.0 - v2.2 */
416 case 3: /* MMC v3.1 - v3.3 */
cb757b4e 417 case 4: /* MMC v4 */
335eadf2
PO
418 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
419 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
420 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
421 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
422 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
423 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
424 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
425 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
426 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
427 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
428 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
429 break;
430
431 default:
432 printk("%s: card has unknown MMCA version %d\n",
433 mmc_hostname(card->host), card->csd.mmca_vsn);
434 mmc_card_set_bad(card);
435 break;
436 }
1da177e4
LT
437 }
438}
439
440/*
441 * Given a 128-bit response, decode to our card CSD structure.
442 */
443static void mmc_decode_csd(struct mmc_card *card)
444{
445 struct mmc_csd *csd = &card->csd;
446 unsigned int e, m, csd_struct;
447 u32 *resp = card->raw_csd;
448
335eadf2
PO
449 if (mmc_card_sd(card)) {
450 csd_struct = UNSTUFF_BITS(resp, 126, 2);
fba68bd2
PL
451
452 switch (csd_struct) {
453 case 0:
454 m = UNSTUFF_BITS(resp, 115, 4);
455 e = UNSTUFF_BITS(resp, 112, 3);
456 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
457 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
458
459 m = UNSTUFF_BITS(resp, 99, 4);
460 e = UNSTUFF_BITS(resp, 96, 3);
461 csd->max_dtr = tran_exp[e] * tran_mant[m];
462 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
463
464 e = UNSTUFF_BITS(resp, 47, 3);
465 m = UNSTUFF_BITS(resp, 62, 12);
466 csd->capacity = (1 + m) << (e + 2);
467
468 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
469 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
470 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
471 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
472 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
473 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
474 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
475 break;
476 case 1:
477 /*
478 * This is a block-addressed SDHC card. Most
479 * interesting fields are unused and have fixed
480 * values. To avoid getting tripped by buggy cards,
481 * we assume those fixed values ourselves.
482 */
483 mmc_card_set_blockaddr(card);
484
485 csd->tacc_ns = 0; /* Unused */
486 csd->tacc_clks = 0; /* Unused */
487
488 m = UNSTUFF_BITS(resp, 99, 4);
489 e = UNSTUFF_BITS(resp, 96, 3);
490 csd->max_dtr = tran_exp[e] * tran_mant[m];
491 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
492
493 m = UNSTUFF_BITS(resp, 48, 22);
494 csd->capacity = (1 + m) << 10;
495
496 csd->read_blkbits = 9;
497 csd->read_partial = 0;
498 csd->write_misalign = 0;
499 csd->read_misalign = 0;
500 csd->r2w_factor = 4; /* Unused */
501 csd->write_blkbits = 9;
502 csd->write_partial = 0;
503 break;
504 default:
335eadf2
PO
505 printk("%s: unrecognised CSD structure version %d\n",
506 mmc_hostname(card->host), csd_struct);
507 mmc_card_set_bad(card);
508 return;
509 }
a00fc090 510 } else {
335eadf2
PO
511 /*
512 * We only understand CSD structure v1.1 and v1.2.
513 * v1.2 has extra information in bits 15, 11 and 10.
514 */
515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516 if (csd_struct != 1 && csd_struct != 2) {
517 printk("%s: unrecognised CSD structure version %d\n",
518 mmc_hostname(card->host), csd_struct);
519 mmc_card_set_bad(card);
520 return;
521 }
1da177e4 522
335eadf2
PO
523 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
524 m = UNSTUFF_BITS(resp, 115, 4);
525 e = UNSTUFF_BITS(resp, 112, 3);
526 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
527 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
1da177e4 528
335eadf2
PO
529 m = UNSTUFF_BITS(resp, 99, 4);
530 e = UNSTUFF_BITS(resp, 96, 3);
531 csd->max_dtr = tran_exp[e] * tran_mant[m];
532 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
1da177e4 533
335eadf2
PO
534 e = UNSTUFF_BITS(resp, 47, 3);
535 m = UNSTUFF_BITS(resp, 62, 12);
536 csd->capacity = (1 + m) << (e + 2);
1da177e4 537
335eadf2 538 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
a6f6c96b
RK
539 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
540 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
541 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
37be4e78 542 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
a6f6c96b
RK
543 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
544 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
335eadf2 545 }
1da177e4
LT
546}
547
b57c43ad
PO
548/*
549 * Given a 64-bit response, decode to our card SCR structure.
550 */
551static void mmc_decode_scr(struct mmc_card *card)
552{
553 struct sd_scr *scr = &card->scr;
554 unsigned int scr_struct;
555 u32 resp[4];
556
557 BUG_ON(!mmc_card_sd(card));
558
559 resp[3] = card->raw_scr[1];
560 resp[2] = card->raw_scr[0];
561
562 scr_struct = UNSTUFF_BITS(resp, 60, 4);
563 if (scr_struct != 0) {
564 printk("%s: unrecognised SCR structure version %d\n",
565 mmc_hostname(card->host), scr_struct);
566 mmc_card_set_bad(card);
567 return;
568 }
569
570 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
571 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
572}
573
1da177e4 574/*
b855885e 575 * Allocate a new MMC card
1da177e4
LT
576 */
577static struct mmc_card *
b855885e 578mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
1da177e4 579{
b855885e 580 struct mmc_card *card;
1da177e4
LT
581
582 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
583 if (!card)
584 return ERR_PTR(-ENOMEM);
585
586 mmc_init_card(card, host);
587 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
588
1da177e4
LT
589 return card;
590}
591
1da177e4 592/*
45f8245b
RK
593 * Apply power to the MMC stack. This is a two-stage process.
594 * First, we enable power to the card without the clock running.
595 * We then wait a bit for the power to stabilise. Finally,
596 * enable the bus drivers and clock to the card.
597 *
598 * We must _NOT_ enable the clock prior to power stablising.
599 *
600 * If a host does all the power sequencing itself, ignore the
601 * initial MMC_POWER_UP stage.
1da177e4
LT
602 */
603static void mmc_power_up(struct mmc_host *host)
604{
605 int bit = fls(host->ocr_avail) - 1;
606
607 host->ios.vdd = bit;
608 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 609 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 610 host->ios.power_mode = MMC_POWER_UP;
f218278a 611 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 612 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 613 mmc_set_ios(host);
1da177e4
LT
614
615 mmc_delay(1);
616
617 host->ios.clock = host->f_min;
618 host->ios.power_mode = MMC_POWER_ON;
920e70c5 619 mmc_set_ios(host);
1da177e4
LT
620
621 mmc_delay(2);
622}
623
624static void mmc_power_off(struct mmc_host *host)
625{
626 host->ios.clock = 0;
627 host->ios.vdd = 0;
628 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 629 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 630 host->ios.power_mode = MMC_POWER_OFF;
f218278a 631 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 632 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 633 mmc_set_ios(host);
1da177e4
LT
634}
635
1da177e4 636/*
b855885e 637 * Discover the card by requesting its CID.
1da177e4 638 *
b855885e 639 * Create a mmc_card entry for the discovered card, assigning
1da177e4
LT
640 * it an RCA, and save the raw CID for decoding later.
641 */
b855885e 642static void mmc_discover_card(struct mmc_host *host)
1da177e4 643{
b855885e 644 unsigned int err;
da7fbe58 645 u32 cid[4];
1da177e4 646
b855885e
PO
647 BUG_ON(host->card);
648
da7fbe58 649 err = mmc_all_send_cid(host, cid);
b855885e
PO
650 if (err != MMC_ERR_NONE) {
651 printk(KERN_ERR "%s: error requesting CID: %d\n",
652 mmc_hostname(host), err);
653 return;
654 }
655
da7fbe58 656 host->card = mmc_alloc_card(host, cid);
b855885e
PO
657 if (IS_ERR(host->card)) {
658 err = PTR_ERR(host->card);
659 host->card = NULL;
660 return;
661 }
662
663 if (host->mode == MMC_MODE_SD) {
664 host->card->type = MMC_TYPE_SD;
665
da7fbe58 666 err = mmc_send_relative_addr(host, &host->card->rca);
b855885e
PO
667 if (err != MMC_ERR_NONE)
668 mmc_card_set_dead(host->card);
669 else {
b855885e
PO
670 if (!host->ops->get_ro) {
671 printk(KERN_WARNING "%s: host does not "
672 "support reading read-only "
673 "switch. assuming write-enable.\n",
674 mmc_hostname(host));
675 } else {
676 if (host->ops->get_ro(host))
677 mmc_card_set_readonly(host->card);
1da177e4 678 }
1da177e4 679 }
b855885e
PO
680 } else {
681 host->card->type = MMC_TYPE_MMC;
682 host->card->rca = 1;
1da177e4 683
da7fbe58 684 err = mmc_set_relative_addr(host->card);
b855885e
PO
685 if (err != MMC_ERR_NONE)
686 mmc_card_set_dead(host->card);
1da177e4
LT
687 }
688}
689
b855885e 690static void mmc_read_csd(struct mmc_host *host)
1da177e4 691{
b855885e 692 int err;
1da177e4 693
b855885e
PO
694 if (!host->card)
695 return;
696 if (mmc_card_dead(host->card))
697 return;
1da177e4 698
da7fbe58 699 err = mmc_send_csd(host->card, host->card->raw_csd);
b855885e
PO
700 if (err != MMC_ERR_NONE) {
701 mmc_card_set_dead(host->card);
702 return;
703 }
1da177e4 704
b855885e
PO
705 mmc_decode_csd(host->card);
706 mmc_decode_cid(host->card);
1da177e4
LT
707}
708
b855885e 709static void mmc_process_ext_csd(struct mmc_host *host)
bce40a36
PL
710{
711 int err;
b855885e 712 u8 *ext_csd;
bce40a36 713
b855885e
PO
714 if (!host->card)
715 return;
716 if (mmc_card_dead(host->card))
717 return;
718 if (mmc_card_sd(host->card))
719 return;
720 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
721 return;
722
bce40a36
PL
723 /*
724 * As the ext_csd is so large and mostly unused, we don't store the
725 * raw block in mmc_card.
726 */
bce40a36
PL
727 ext_csd = kmalloc(512, GFP_KERNEL);
728 if (!ext_csd) {
729 printk("%s: could not allocate a buffer to receive the ext_csd."
730 "mmc v4 cards will be treated as v3.\n",
731 mmc_hostname(host));
732 return;
733 }
734
da7fbe58
PO
735 err = mmc_send_ext_csd(host->card, ext_csd);
736 if (err != MMC_ERR_NONE) {
b855885e
PO
737 if (host->card->csd.capacity == (4096 * 512)) {
738 printk(KERN_ERR "%s: unable to read EXT_CSD "
739 "on a possible high capacity card. "
740 "Card will be ignored.\n",
741 mmc_hostname(host));
742 mmc_card_set_dead(host->card);
743 } else {
744 printk(KERN_WARNING "%s: unable to read "
745 "EXT_CSD, performance might "
746 "suffer.\n",
747 mmc_hostname(host));
bce40a36 748 }
b855885e
PO
749 goto out;
750 }
bce40a36 751
b855885e
PO
752 host->card->ext_csd.sectors =
753 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
754 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
755 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
756 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
757 if (host->card->ext_csd.sectors)
758 mmc_card_set_blockaddr(host->card);
759
760 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
761 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
762 host->card->ext_csd.hs_max_dtr = 52000000;
763 break;
764 case EXT_CSD_CARD_TYPE_26:
765 host->card->ext_csd.hs_max_dtr = 26000000;
766 break;
767 default:
768 /* MMC v4 spec says this cannot happen */
769 printk("%s: card is mmc v4 but doesn't support "
770 "any high-speed modes.\n",
771 mmc_hostname(host));
772 goto out;
773 }
85a18ad9 774
b855885e
PO
775 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
776 /* Activate highspeed support. */
da7fbe58
PO
777 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
778 EXT_CSD_HS_TIMING, 1);
b855885e
PO
779 if (err != MMC_ERR_NONE) {
780 printk("%s: failed to switch card to mmc v4 "
781 "high-speed mode.\n",
782 mmc_hostname(host));
783 goto out;
784 }
bce40a36 785
b855885e 786 mmc_card_set_highspeed(host->card);
e45a1bd2 787
b855885e
PO
788 host->ios.timing = MMC_TIMING_MMC_HS;
789 mmc_set_ios(host);
790 }
e45a1bd2 791
b855885e
PO
792 /* Check for host support for wide-bus modes. */
793 if (host->caps & MMC_CAP_4_BIT_DATA) {
794 /* Activate 4-bit support. */
da7fbe58
PO
795 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
796 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
797 EXT_CSD_CMD_SET_NORMAL);
b855885e
PO
798 if (err != MMC_ERR_NONE) {
799 printk("%s: failed to switch card to "
800 "mmc v4 4-bit bus mode.\n",
801 mmc_hostname(host));
802 goto out;
cd9277c0 803 }
b855885e
PO
804
805 host->ios.bus_width = MMC_BUS_WIDTH_4;
806 mmc_set_ios(host);
bce40a36
PL
807 }
808
b855885e 809out:
bce40a36 810 kfree(ext_csd);
bce40a36
PL
811}
812
b855885e 813static void mmc_read_scr(struct mmc_host *host)
b57c43ad
PO
814{
815 int err;
b57c43ad 816
b855885e
PO
817 if (!host->card)
818 return;
819 if (mmc_card_dead(host->card))
820 return;
821 if (!mmc_card_sd(host->card))
822 return;
b57c43ad 823
da7fbe58
PO
824 err = mmc_app_send_scr(host->card, host->card->raw_scr);
825 if (err != MMC_ERR_NONE) {
b855885e
PO
826 mmc_card_set_dead(host->card);
827 return;
b57c43ad
PO
828 }
829
b855885e 830 mmc_decode_scr(host->card);
b57c43ad
PO
831}
832
7ccd266e
PO
833static void mmc_read_switch_caps(struct mmc_host *host)
834{
da7fbe58 835 int err;
7ccd266e 836 unsigned char *status;
7ccd266e 837
cd9277c0
PO
838 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
839 return;
840
b855885e
PO
841 if (!host->card)
842 return;
843 if (mmc_card_dead(host->card))
844 return;
845 if (!mmc_card_sd(host->card))
846 return;
847 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
848 return;
849
7ccd266e
PO
850 status = kmalloc(64, GFP_KERNEL);
851 if (!status) {
852 printk(KERN_WARNING "%s: Unable to allocate buffer for "
853 "reading switch capabilities.\n",
854 mmc_hostname(host));
855 return;
856 }
857
da7fbe58
PO
858 err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
859 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
860 if (err != MMC_ERR_NONE) {
b855885e
PO
861 printk("%s: unable to read switch capabilities, "
862 "performance might suffer.\n",
863 mmc_hostname(host));
864 goto out;
865 }
7ccd266e 866
b855885e
PO
867 if (status[13] & 0x02)
868 host->card->sw_caps.hs_max_dtr = 50000000;
7ccd266e 869
da7fbe58
PO
870 err = mmc_sd_switch(host->card, SD_SWITCH_SET,
871 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
872 if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
b855885e
PO
873 printk(KERN_WARNING "%s: Problem switching card "
874 "into high-speed mode!\n",
875 mmc_hostname(host));
876 goto out;
877 }
7ccd266e 878
b855885e 879 mmc_card_set_highspeed(host->card);
cd9277c0 880
b855885e
PO
881 host->ios.timing = MMC_TIMING_SD_HS;
882 mmc_set_ios(host);
7ccd266e 883
b855885e 884out:
7ccd266e 885 kfree(status);
7ccd266e
PO
886}
887
1da177e4
LT
888static unsigned int mmc_calculate_clock(struct mmc_host *host)
889{
1da177e4
LT
890 unsigned int max_dtr = host->f_max;
891
b855885e
PO
892 if (host->card && !mmc_card_dead(host->card)) {
893 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
894 if (max_dtr > host->card->sw_caps.hs_max_dtr)
895 max_dtr = host->card->sw_caps.hs_max_dtr;
896 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
897 if (max_dtr > host->card->ext_csd.hs_max_dtr)
898 max_dtr = host->card->ext_csd.hs_max_dtr;
899 } else if (max_dtr > host->card->csd.max_dtr) {
900 max_dtr = host->card->csd.max_dtr;
bce40a36 901 }
b855885e 902 }
1da177e4 903
920e70c5
RK
904 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
905 mmc_hostname(host),
c6563178 906 max_dtr / 1000000, (max_dtr / 1000) % 1000);
1da177e4
LT
907
908 return max_dtr;
909}
910
911/*
912 * Check whether cards we already know about are still present.
913 * We do this by requesting status, and checking whether a card
914 * responds.
915 *
916 * A request for status does not cause a state change in data
917 * transfer mode.
918 */
b855885e 919static void mmc_check_card(struct mmc_card *card)
1da177e4 920{
b855885e 921 int err;
1da177e4 922
b855885e 923 BUG_ON(!card);
1da177e4 924
da7fbe58 925 err = mmc_send_status(card, NULL);
b855885e
PO
926 if (err == MMC_ERR_NONE)
927 return;
1da177e4 928
b855885e 929 mmc_card_set_dead(card);
1da177e4
LT
930}
931
932static void mmc_setup(struct mmc_host *host)
933{
b855885e
PO
934 int err;
935 u32 ocr;
1da177e4 936
b855885e 937 host->mode = MMC_MODE_SD;
335eadf2 938
b855885e 939 mmc_power_up(host);
da7fbe58 940 mmc_go_idle(host);
1da177e4 941
da7fbe58 942 err = mmc_send_if_cond(host, host->ocr_avail);
b855885e
PO
943 if (err != MMC_ERR_NONE) {
944 return;
945 }
946 err = mmc_send_app_op_cond(host, 0, &ocr);
1da177e4 947
b855885e
PO
948 /*
949 * If we fail to detect any SD cards then try
950 * searching for MMC cards.
951 */
952 if (err != MMC_ERR_NONE) {
953 host->mode = MMC_MODE_MMC;
1da177e4 954
b855885e
PO
955 err = mmc_send_op_cond(host, 0, &ocr);
956 if (err != MMC_ERR_NONE)
957 return;
1da177e4
LT
958 }
959
b855885e
PO
960 host->ocr = mmc_select_voltage(host, ocr);
961
1da177e4
LT
962 if (host->ocr == 0)
963 return;
964
b855885e
PO
965 /*
966 * Since we're changing the OCR value, we seem to
967 * need to tell some cards to go back to the idle
968 * state. We wait 1ms to give cards time to
969 * respond.
970 */
da7fbe58 971 mmc_go_idle(host);
b855885e 972
1da177e4
LT
973 /*
974 * Send the selected OCR multiple times... until the cards
975 * all get the idea that they should be ready for CMD2.
976 * (My SanDisk card seems to need this.)
977 */
fba68bd2 978 if (host->mode == MMC_MODE_SD) {
da7fbe58
PO
979 /*
980 * If SD_SEND_IF_COND indicates an SD 2.0
981 * compliant card and we should set bit 30
982 * of the ocr to indicate that we can handle
983 * block-addressed SDHC cards.
984 */
985 err = mmc_send_if_cond(host, host->ocr);
986 if (err == MMC_ERR_NONE)
987 ocr = host->ocr | (1 << 30);
988
989 mmc_send_app_op_cond(host, ocr, NULL);
fba68bd2 990 } else {
85a18ad9
PO
991 /* The extra bit indicates that we support high capacity */
992 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
fba68bd2 993 }
1da177e4 994
b855885e 995 mmc_discover_card(host);
1da177e4
LT
996
997 /*
998 * Ok, now switch to push-pull mode.
999 */
1000 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
920e70c5 1001 mmc_set_ios(host);
1da177e4 1002
b855885e
PO
1003 mmc_read_csd(host);
1004
1005 if (host->card && !mmc_card_dead(host->card)) {
1006 err = mmc_select_card(host->card);
1007 if (err != MMC_ERR_NONE)
1008 mmc_card_set_dead(host->card);
1009 }
b57c43ad 1010
da7fbe58
PO
1011 /*
1012 * The card is in 1 bit mode by default so
1013 * we only need to change if it supports the
1014 * wider version.
1015 */
1016 if (host->card && !mmc_card_dead(host->card) &&
1017 mmc_card_sd(host->card) &&
1018 (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
1019 (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
1020 err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
1021 if (err != MMC_ERR_NONE)
1022 mmc_card_set_dead(host->card);
1023 else {
1024 host->ios.bus_width = MMC_BUS_WIDTH_4;
1025 mmc_set_ios(host);
1026 }
1027 }
1028
7ccd266e 1029 if (host->mode == MMC_MODE_SD) {
b855885e 1030 mmc_read_scr(host);
7ccd266e
PO
1031 mmc_read_switch_caps(host);
1032 } else
b855885e 1033 mmc_process_ext_csd(host);
1da177e4
LT
1034}
1035
1036
1037/**
1038 * mmc_detect_change - process change of state on a MMC socket
1039 * @host: host which changed state.
8dc00335 1040 * @delay: optional delay to wait before detection (jiffies)
1da177e4
LT
1041 *
1042 * All we know is that card(s) have been inserted or removed
1043 * from the socket(s). We don't know which socket or cards.
1044 */
8dc00335 1045void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1046{
3b91e550
PO
1047#ifdef CONFIG_MMC_DEBUG
1048 mmc_claim_host(host);
1049 BUG_ON(host->removed);
1050 mmc_release_host(host);
1051#endif
1052
c4028958 1053 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
1054}
1055
1056EXPORT_SYMBOL(mmc_detect_change);
1057
1058
c4028958 1059static void mmc_rescan(struct work_struct *work)
1da177e4 1060{
c4028958
DH
1061 struct mmc_host *host =
1062 container_of(work, struct mmc_host, detect.work);
1da177e4
LT
1063
1064 mmc_claim_host(host);
1065
25a122fd 1066 /*
b855885e 1067 * Check for removed card and newly inserted ones. We check for
25a122fd
TT
1068 * removed cards first so we can intelligently re-select the VDD.
1069 */
b855885e
PO
1070 if (host->card) {
1071 mmc_check_card(host->card);
1da177e4 1072
b855885e 1073 mmc_release_host(host);
1da177e4 1074
b855885e
PO
1075 if (mmc_card_dead(host->card)) {
1076 mmc_remove_card(host->card);
1077 host->card = NULL;
1078 }
1079
1080 goto out;
1081 }
25a122fd 1082
b855885e
PO
1083 mmc_setup(host);
1084
1085 if (host->card && !mmc_card_dead(host->card)) {
1da177e4
LT
1086 /*
1087 * (Re-)calculate the fastest clock rate which the
1088 * attached cards and the host support.
1089 */
1090 host->ios.clock = mmc_calculate_clock(host);
920e70c5 1091 mmc_set_ios(host);
1da177e4
LT
1092 }
1093
1094 mmc_release_host(host);
1095
b855885e
PO
1096 /*
1097 * If this is a new and good card, register it.
1098 */
1099 if (host->card && !mmc_card_dead(host->card)) {
1100 if (mmc_register_card(host->card))
1101 mmc_card_set_dead(host->card);
1102 }
1da177e4 1103
b855885e
PO
1104 /*
1105 * If this card is dead, destroy it.
1106 */
1107 if (host->card && mmc_card_dead(host->card)) {
1108 mmc_remove_card(host->card);
1109 host->card = NULL;
1da177e4
LT
1110 }
1111
b855885e 1112out:
1da177e4
LT
1113 /*
1114 * If we discover that there are no cards on the
1115 * bus, turn off the clock and power down.
1116 */
b855885e 1117 if (!host->card)
1da177e4
LT
1118 mmc_power_off(host);
1119}
1120
1121
1122/**
1123 * mmc_alloc_host - initialise the per-host structure.
1124 * @extra: sizeof private data structure
1125 * @dev: pointer to host device model structure
1126 *
1127 * Initialise the per-host structure.
1128 */
1129struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1130{
1131 struct mmc_host *host;
1132
00b137cf 1133 host = mmc_alloc_host_sysfs(extra, dev);
1da177e4 1134 if (host) {
1da177e4
LT
1135 spin_lock_init(&host->lock);
1136 init_waitqueue_head(&host->wq);
c4028958 1137 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1da177e4 1138
1da177e4
LT
1139 /*
1140 * By default, hosts do not support SGIO or large requests.
1141 * They have to set these according to their abilities.
1142 */
1143 host->max_hw_segs = 1;
1144 host->max_phys_segs = 1;
1da177e4 1145 host->max_seg_size = PAGE_CACHE_SIZE;
fe4a3c7a 1146
55db890a 1147 host->max_req_size = PAGE_CACHE_SIZE;
fe4a3c7a 1148 host->max_blk_size = 512;
55db890a 1149 host->max_blk_count = PAGE_CACHE_SIZE / 512;
1da177e4
LT
1150 }
1151
1152 return host;
1153}
1154
1155EXPORT_SYMBOL(mmc_alloc_host);
1156
1157/**
1158 * mmc_add_host - initialise host hardware
1159 * @host: mmc host
1160 */
1161int mmc_add_host(struct mmc_host *host)
1162{
00b137cf 1163 int ret;
1da177e4 1164
00b137cf
RK
1165 ret = mmc_add_host_sysfs(host);
1166 if (ret == 0) {
1167 mmc_power_off(host);
8dc00335 1168 mmc_detect_change(host, 0);
00b137cf 1169 }
1da177e4 1170
00b137cf 1171 return ret;
1da177e4
LT
1172}
1173
1174EXPORT_SYMBOL(mmc_add_host);
1175
1176/**
1177 * mmc_remove_host - remove host hardware
1178 * @host: mmc host
1179 *
1180 * Unregister and remove all cards associated with this host,
1181 * and power down the MMC bus.
1182 */
1183void mmc_remove_host(struct mmc_host *host)
1184{
3b91e550
PO
1185#ifdef CONFIG_MMC_DEBUG
1186 mmc_claim_host(host);
1187 host->removed = 1;
1188 mmc_release_host(host);
1189#endif
1190
1191 mmc_flush_scheduled_work();
1192
b855885e
PO
1193 if (host->card) {
1194 mmc_remove_card(host->card);
1195 host->card = NULL;
1da177e4
LT
1196 }
1197
1198 mmc_power_off(host);
00b137cf 1199 mmc_remove_host_sysfs(host);
1da177e4
LT
1200}
1201
1202EXPORT_SYMBOL(mmc_remove_host);
1203
1204/**
1205 * mmc_free_host - free the host structure
1206 * @host: mmc host
1207 *
1208 * Free the host once all references to it have been dropped.
1209 */
1210void mmc_free_host(struct mmc_host *host)
1211{
00b137cf 1212 mmc_free_host_sysfs(host);
1da177e4
LT
1213}
1214
1215EXPORT_SYMBOL(mmc_free_host);
1216
1217#ifdef CONFIG_PM
1218
1219/**
1220 * mmc_suspend_host - suspend a host
1221 * @host: mmc host
1222 * @state: suspend mode (PM_SUSPEND_xxx)
1223 */
e5378ca8 1224int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1da177e4 1225{
b5af25be
PO
1226 mmc_flush_scheduled_work();
1227
b855885e
PO
1228 if (host->card) {
1229 mmc_remove_card(host->card);
1230 host->card = NULL;
b5af25be
PO
1231 }
1232
1da177e4 1233 mmc_power_off(host);
1da177e4
LT
1234
1235 return 0;
1236}
1237
1238EXPORT_SYMBOL(mmc_suspend_host);
1239
1240/**
1241 * mmc_resume_host - resume a previously suspended host
1242 * @host: mmc host
1243 */
1244int mmc_resume_host(struct mmc_host *host)
1245{
c4028958 1246 mmc_rescan(&host->detect.work);
1da177e4
LT
1247
1248 return 0;
1249}
1250
1251EXPORT_SYMBOL(mmc_resume_host);
1252
1253#endif
1254
1255MODULE_LICENSE("GPL");