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