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