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