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