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