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