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