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