]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/core.c
mmc: Separate out protocol ops
[linux-2.6-omap-h63xx.git] / drivers / mmc / core / core.c
1 /*
2  *  linux/drivers/mmc/core/core.c
3  *
4  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6  *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7  *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/completion.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/pagemap.h>
20 #include <linux/err.h>
21 #include <asm/scatterlist.h>
22 #include <linux/scatterlist.h>
23
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/sd.h>
28
29 #include "core.h"
30 #include "sysfs.h"
31
32 #include "mmc_ops.h"
33 #include "sd_ops.h"
34
35 #define CMD_RETRIES     3
36
37 /*
38  * OCR Bit positions to 10s of Vdd mV.
39  */
40 static const unsigned short mmc_ocr_bit_to_vdd[] = {
41         150,    155,    160,    165,    170,    180,    190,    200,
42         210,    220,    230,    240,    250,    260,    270,    280,
43         290,    300,    310,    320,    330,    340,    350,    360
44 };
45
46 static const unsigned int tran_exp[] = {
47         10000,          100000,         1000000,        10000000,
48         0,              0,              0,              0
49 };
50
51 static const unsigned char tran_mant[] = {
52         0,      10,     12,     13,     15,     20,     25,     30,
53         35,     40,     45,     50,     55,     60,     70,     80,
54 };
55
56 static const unsigned int tacc_exp[] = {
57         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
58 };
59
60 static const unsigned int tacc_mant[] = {
61         0,      10,     12,     13,     15,     20,     25,     30,
62         35,     40,     45,     50,     55,     60,     70,     80,
63 };
64
65
66 /**
67  *      mmc_request_done - finish processing an MMC request
68  *      @host: MMC host which completed request
69  *      @mrq: MMC request which request
70  *
71  *      MMC drivers should call this function when they have completed
72  *      their processing of a request.
73  */
74 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
75 {
76         struct mmc_command *cmd = mrq->cmd;
77         int err = cmd->error;
78
79         pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
80                  mmc_hostname(host), cmd->opcode, err,
81                  mrq->data ? mrq->data->error : 0,
82                  mrq->stop ? mrq->stop->error : 0,
83                  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 #ifdef CONFIG_MMC_DEBUG
108         unsigned int i, sz;
109 #endif
110
111         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
112                  mmc_hostname(host), mrq->cmd->opcode,
113                  mrq->cmd->arg, mrq->cmd->flags);
114
115         WARN_ON(!host->claimed);
116
117         mrq->cmd->error = 0;
118         mrq->cmd->mrq = mrq;
119         if (mrq->data) {
120                 BUG_ON(mrq->data->blksz > host->max_blk_size);
121                 BUG_ON(mrq->data->blocks > host->max_blk_count);
122                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
123                         host->max_req_size);
124
125 #ifdef CONFIG_MMC_DEBUG
126                 sz = 0;
127                 for (i = 0;i < mrq->data->sg_len;i++)
128                         sz += mrq->data->sg[i].length;
129                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
130 #endif
131
132                 mrq->cmd->data = mrq->data;
133                 mrq->data->error = 0;
134                 mrq->data->mrq = mrq;
135                 if (mrq->stop) {
136                         mrq->data->stop = mrq->stop;
137                         mrq->stop->error = 0;
138                         mrq->stop->mrq = mrq;
139                 }
140         }
141         host->ops->request(host, mrq);
142 }
143
144 EXPORT_SYMBOL(mmc_start_request);
145
146 static void mmc_wait_done(struct mmc_request *mrq)
147 {
148         complete(mrq->done_data);
149 }
150
151 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
152 {
153         DECLARE_COMPLETION_ONSTACK(complete);
154
155         mrq->done_data = &complete;
156         mrq->done = mmc_wait_done;
157
158         mmc_start_request(host, mrq);
159
160         wait_for_completion(&complete);
161
162         return 0;
163 }
164
165 EXPORT_SYMBOL(mmc_wait_for_req);
166
167 /**
168  *      mmc_wait_for_cmd - start a command and wait for completion
169  *      @host: MMC host to start command
170  *      @cmd: MMC command to start
171  *      @retries: maximum number of retries
172  *
173  *      Start a new MMC command for a host, and wait for the command
174  *      to complete.  Return any error that occurred while the command
175  *      was executing.  Do not attempt to parse the response.
176  */
177 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
178 {
179         struct mmc_request mrq;
180
181         BUG_ON(!host->claimed);
182
183         memset(&mrq, 0, sizeof(struct mmc_request));
184
185         memset(cmd->resp, 0, sizeof(cmd->resp));
186         cmd->retries = retries;
187
188         mrq.cmd = cmd;
189         cmd->data = NULL;
190
191         mmc_wait_for_req(host, &mrq);
192
193         return cmd->error;
194 }
195
196 EXPORT_SYMBOL(mmc_wait_for_cmd);
197
198 /**
199  *      mmc_set_data_timeout - set the timeout for a data command
200  *      @data: data phase for command
201  *      @card: the MMC card associated with the data transfer
202  *      @write: flag to differentiate reads from writes
203  */
204 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
205                           int write)
206 {
207         unsigned int mult;
208
209         /*
210          * SD cards use a 100 multiplier rather than 10
211          */
212         mult = mmc_card_sd(card) ? 100 : 10;
213
214         /*
215          * Scale up the multiplier (and therefore the timeout) by
216          * the r2w factor for writes.
217          */
218         if (write)
219                 mult <<= card->csd.r2w_factor;
220
221         data->timeout_ns = card->csd.tacc_ns * mult;
222         data->timeout_clks = card->csd.tacc_clks * mult;
223
224         /*
225          * SD cards also have an upper limit on the timeout.
226          */
227         if (mmc_card_sd(card)) {
228                 unsigned int timeout_us, limit_us;
229
230                 timeout_us = data->timeout_ns / 1000;
231                 timeout_us += data->timeout_clks * 1000 /
232                         (card->host->ios.clock / 1000);
233
234                 if (write)
235                         limit_us = 250000;
236                 else
237                         limit_us = 100000;
238
239                 /*
240                  * SDHC cards always use these fixed values.
241                  */
242                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
243                         data->timeout_ns = limit_us * 1000;
244                         data->timeout_clks = 0;
245                 }
246         }
247 }
248 EXPORT_SYMBOL(mmc_set_data_timeout);
249
250 /**
251  *      __mmc_claim_host - exclusively claim a host
252  *      @host: mmc host to claim
253  *      @card: mmc card to claim host for
254  *
255  *      Claim a host for a set of operations.  If a valid card
256  *      is passed and this wasn't the last card selected, select
257  *      the card before returning.
258  *
259  *      Note: you should use mmc_card_claim_host or mmc_claim_host.
260  */
261 void mmc_claim_host(struct mmc_host *host)
262 {
263         DECLARE_WAITQUEUE(wait, current);
264         unsigned long flags;
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->claimed)
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->claimed = 1;
278         spin_unlock_irqrestore(&host->lock, flags);
279         remove_wait_queue(&host->wq, &wait);
280 }
281
282 EXPORT_SYMBOL(mmc_claim_host);
283
284 /**
285  *      mmc_release_host - release a host
286  *      @host: mmc host to release
287  *
288  *      Release a MMC host, allowing others to claim the host
289  *      for their operations.
290  */
291 void mmc_release_host(struct mmc_host *host)
292 {
293         unsigned long flags;
294
295         BUG_ON(!host->claimed);
296
297         spin_lock_irqsave(&host->lock, flags);
298         host->claimed = 0;
299         spin_unlock_irqrestore(&host->lock, flags);
300
301         wake_up(&host->wq);
302 }
303
304 EXPORT_SYMBOL(mmc_release_host);
305
306 static inline void mmc_set_ios(struct mmc_host *host)
307 {
308         struct mmc_ios *ios = &host->ios;
309
310         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
311                 "width %u timing %u\n",
312                  mmc_hostname(host), ios->clock, ios->bus_mode,
313                  ios->power_mode, ios->chip_select, ios->vdd,
314                  ios->bus_width, ios->timing);
315
316         host->ops->set_ios(host, ios);
317 }
318
319 void mmc_set_chip_select(struct mmc_host *host, int mode)
320 {
321         host->ios.chip_select = mode;
322         mmc_set_ios(host);
323 }
324
325 /*
326  * Mask off any voltages we don't support and select
327  * the lowest voltage
328  */
329 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
330 {
331         int bit;
332
333         ocr &= host->ocr_avail;
334
335         bit = ffs(ocr);
336         if (bit) {
337                 bit -= 1;
338
339                 ocr &= 3 << bit;
340
341                 host->ios.vdd = bit;
342                 mmc_set_ios(host);
343         } else {
344                 ocr = 0;
345         }
346
347         return ocr;
348 }
349
350 #define UNSTUFF_BITS(resp,start,size)                                   \
351         ({                                                              \
352                 const int __size = size;                                \
353                 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
354                 const int __off = 3 - ((start) / 32);                   \
355                 const int __shft = (start) & 31;                        \
356                 u32 __res;                                              \
357                                                                         \
358                 __res = resp[__off] >> __shft;                          \
359                 if (__size + __shft > 32)                               \
360                         __res |= resp[__off-1] << ((32 - __shft) % 32); \
361                 __res & __mask;                                         \
362         })
363
364 /*
365  * Given the decoded CSD structure, decode the raw CID to our CID structure.
366  */
367 static void mmc_decode_cid(struct mmc_card *card)
368 {
369         u32 *resp = card->raw_cid;
370
371         memset(&card->cid, 0, sizeof(struct mmc_cid));
372
373         if (mmc_card_sd(card)) {
374                 /*
375                  * SD doesn't currently have a version field so we will
376                  * have to assume we can parse this.
377                  */
378                 card->cid.manfid                = UNSTUFF_BITS(resp, 120, 8);
379                 card->cid.oemid                 = UNSTUFF_BITS(resp, 104, 16);
380                 card->cid.prod_name[0]          = UNSTUFF_BITS(resp, 96, 8);
381                 card->cid.prod_name[1]          = UNSTUFF_BITS(resp, 88, 8);
382                 card->cid.prod_name[2]          = UNSTUFF_BITS(resp, 80, 8);
383                 card->cid.prod_name[3]          = UNSTUFF_BITS(resp, 72, 8);
384                 card->cid.prod_name[4]          = UNSTUFF_BITS(resp, 64, 8);
385                 card->cid.hwrev                 = UNSTUFF_BITS(resp, 60, 4);
386                 card->cid.fwrev                 = UNSTUFF_BITS(resp, 56, 4);
387                 card->cid.serial                = UNSTUFF_BITS(resp, 24, 32);
388                 card->cid.year                  = UNSTUFF_BITS(resp, 12, 8);
389                 card->cid.month                 = UNSTUFF_BITS(resp, 8, 4);
390
391                 card->cid.year += 2000; /* SD cards year offset */
392         } else {
393                 /*
394                  * The selection of the format here is based upon published
395                  * specs from sandisk and from what people have reported.
396                  */
397                 switch (card->csd.mmca_vsn) {
398                 case 0: /* MMC v1.0 - v1.2 */
399                 case 1: /* MMC v1.4 */
400                         card->cid.manfid        = UNSTUFF_BITS(resp, 104, 24);
401                         card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
402                         card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
403                         card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
404                         card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
405                         card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
406                         card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
407                         card->cid.prod_name[6]  = UNSTUFF_BITS(resp, 48, 8);
408                         card->cid.hwrev         = UNSTUFF_BITS(resp, 44, 4);
409                         card->cid.fwrev         = UNSTUFF_BITS(resp, 40, 4);
410                         card->cid.serial        = UNSTUFF_BITS(resp, 16, 24);
411                         card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
412                         card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
413                         break;
414
415                 case 2: /* MMC v2.0 - v2.2 */
416                 case 3: /* MMC v3.1 - v3.3 */
417                 case 4: /* MMC v4 */
418                         card->cid.manfid        = UNSTUFF_BITS(resp, 120, 8);
419                         card->cid.oemid         = UNSTUFF_BITS(resp, 104, 16);
420                         card->cid.prod_name[0]  = UNSTUFF_BITS(resp, 96, 8);
421                         card->cid.prod_name[1]  = UNSTUFF_BITS(resp, 88, 8);
422                         card->cid.prod_name[2]  = UNSTUFF_BITS(resp, 80, 8);
423                         card->cid.prod_name[3]  = UNSTUFF_BITS(resp, 72, 8);
424                         card->cid.prod_name[4]  = UNSTUFF_BITS(resp, 64, 8);
425                         card->cid.prod_name[5]  = UNSTUFF_BITS(resp, 56, 8);
426                         card->cid.serial        = UNSTUFF_BITS(resp, 16, 32);
427                         card->cid.month         = UNSTUFF_BITS(resp, 12, 4);
428                         card->cid.year          = UNSTUFF_BITS(resp, 8, 4) + 1997;
429                         break;
430
431                 default:
432                         printk("%s: card has unknown MMCA version %d\n",
433                                 mmc_hostname(card->host), card->csd.mmca_vsn);
434                         mmc_card_set_bad(card);
435                         break;
436                 }
437         }
438 }
439
440 /*
441  * Given a 128-bit response, decode to our card CSD structure.
442  */
443 static void mmc_decode_csd(struct mmc_card *card)
444 {
445         struct mmc_csd *csd = &card->csd;
446         unsigned int e, m, csd_struct;
447         u32 *resp = card->raw_csd;
448
449         if (mmc_card_sd(card)) {
450                 csd_struct = UNSTUFF_BITS(resp, 126, 2);
451
452                 switch (csd_struct) {
453                 case 0:
454                         m = UNSTUFF_BITS(resp, 115, 4);
455                         e = UNSTUFF_BITS(resp, 112, 3);
456                         csd->tacc_ns     = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
457                         csd->tacc_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
458
459                         m = UNSTUFF_BITS(resp, 99, 4);
460                         e = UNSTUFF_BITS(resp, 96, 3);
461                         csd->max_dtr      = tran_exp[e] * tran_mant[m];
462                         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
463
464                         e = UNSTUFF_BITS(resp, 47, 3);
465                         m = UNSTUFF_BITS(resp, 62, 12);
466                         csd->capacity     = (1 + m) << (e + 2);
467
468                         csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
469                         csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
470                         csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
471                         csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
472                         csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
473                         csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
474                         csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
475                         break;
476                 case 1:
477                         /*
478                          * This is a block-addressed SDHC card. Most
479                          * interesting fields are unused and have fixed
480                          * values. To avoid getting tripped by buggy cards,
481                          * we assume those fixed values ourselves.
482                          */
483                         mmc_card_set_blockaddr(card);
484
485                         csd->tacc_ns     = 0; /* Unused */
486                         csd->tacc_clks   = 0; /* Unused */
487
488                         m = UNSTUFF_BITS(resp, 99, 4);
489                         e = UNSTUFF_BITS(resp, 96, 3);
490                         csd->max_dtr      = tran_exp[e] * tran_mant[m];
491                         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
492
493                         m = UNSTUFF_BITS(resp, 48, 22);
494                         csd->capacity     = (1 + m) << 10;
495
496                         csd->read_blkbits = 9;
497                         csd->read_partial = 0;
498                         csd->write_misalign = 0;
499                         csd->read_misalign = 0;
500                         csd->r2w_factor = 4; /* Unused */
501                         csd->write_blkbits = 9;
502                         csd->write_partial = 0;
503                         break;
504                 default:
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         } else {
511                 /*
512                  * We only understand CSD structure v1.1 and v1.2.
513                  * v1.2 has extra information in bits 15, 11 and 10.
514                  */
515                 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516                 if (csd_struct != 1 && csd_struct != 2) {
517                         printk("%s: unrecognised CSD structure version %d\n",
518                                 mmc_hostname(card->host), csd_struct);
519                         mmc_card_set_bad(card);
520                         return;
521                 }
522
523                 csd->mmca_vsn    = UNSTUFF_BITS(resp, 122, 4);
524                 m = UNSTUFF_BITS(resp, 115, 4);
525                 e = UNSTUFF_BITS(resp, 112, 3);
526                 csd->tacc_ns     = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
527                 csd->tacc_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
528
529                 m = UNSTUFF_BITS(resp, 99, 4);
530                 e = UNSTUFF_BITS(resp, 96, 3);
531                 csd->max_dtr      = tran_exp[e] * tran_mant[m];
532                 csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
533
534                 e = UNSTUFF_BITS(resp, 47, 3);
535                 m = UNSTUFF_BITS(resp, 62, 12);
536                 csd->capacity     = (1 + m) << (e + 2);
537
538                 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
539                 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
540                 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
541                 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
542                 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
543                 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
544                 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
545         }
546 }
547
548 /*
549  * Given a 64-bit response, decode to our card SCR structure.
550  */
551 static void mmc_decode_scr(struct mmc_card *card)
552 {
553         struct sd_scr *scr = &card->scr;
554         unsigned int scr_struct;
555         u32 resp[4];
556
557         BUG_ON(!mmc_card_sd(card));
558
559         resp[3] = card->raw_scr[1];
560         resp[2] = card->raw_scr[0];
561
562         scr_struct = UNSTUFF_BITS(resp, 60, 4);
563         if (scr_struct != 0) {
564                 printk("%s: unrecognised SCR structure version %d\n",
565                         mmc_hostname(card->host), scr_struct);
566                 mmc_card_set_bad(card);
567                 return;
568         }
569
570         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
571         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
572 }
573
574 /*
575  * Allocate a new MMC card
576  */
577 static struct mmc_card *
578 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
579 {
580         struct mmc_card *card;
581
582         card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
583         if (!card)
584                 return ERR_PTR(-ENOMEM);
585
586         mmc_init_card(card, host);
587         memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
588
589         return card;
590 }
591
592 /*
593  * Apply power to the MMC stack.  This is a two-stage process.
594  * First, we enable power to the card without the clock running.
595  * We then wait a bit for the power to stabilise.  Finally,
596  * enable the bus drivers and clock to the card.
597  *
598  * We must _NOT_ enable the clock prior to power stablising.
599  *
600  * If a host does all the power sequencing itself, ignore the
601  * initial MMC_POWER_UP stage.
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->ios.bus_width = MMC_BUS_WIDTH_1;
612         host->ios.timing = MMC_TIMING_LEGACY;
613         mmc_set_ios(host);
614
615         mmc_delay(1);
616
617         host->ios.clock = host->f_min;
618         host->ios.power_mode = MMC_POWER_ON;
619         mmc_set_ios(host);
620
621         mmc_delay(2);
622 }
623
624 static void mmc_power_off(struct mmc_host *host)
625 {
626         host->ios.clock = 0;
627         host->ios.vdd = 0;
628         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
629         host->ios.chip_select = MMC_CS_DONTCARE;
630         host->ios.power_mode = MMC_POWER_OFF;
631         host->ios.bus_width = MMC_BUS_WIDTH_1;
632         host->ios.timing = MMC_TIMING_LEGACY;
633         mmc_set_ios(host);
634 }
635
636 /*
637  * Discover the card by requesting its CID.
638  *
639  * Create a mmc_card entry for the discovered card, assigning
640  * it an RCA, and save the raw CID for decoding later.
641  */
642 static void mmc_discover_card(struct mmc_host *host)
643 {
644         unsigned int err;
645         u32 cid[4];
646
647         BUG_ON(host->card);
648
649         err = mmc_all_send_cid(host, cid);
650         if (err != MMC_ERR_NONE) {
651                 printk(KERN_ERR "%s: error requesting CID: %d\n",
652                         mmc_hostname(host), err);
653                 return;
654         }
655
656         host->card = mmc_alloc_card(host, cid);
657         if (IS_ERR(host->card)) {
658                 err = PTR_ERR(host->card);
659                 host->card = NULL;
660                 return;
661         }
662
663         if (host->mode == MMC_MODE_SD) {
664                 host->card->type = MMC_TYPE_SD;
665
666                 err = mmc_send_relative_addr(host, &host->card->rca);
667                 if (err != MMC_ERR_NONE)
668                         mmc_card_set_dead(host->card);
669                 else {
670                         if (!host->ops->get_ro) {
671                                 printk(KERN_WARNING "%s: host does not "
672                                         "support reading read-only "
673                                         "switch. assuming write-enable.\n",
674                                         mmc_hostname(host));
675                         } else {
676                                 if (host->ops->get_ro(host))
677                                         mmc_card_set_readonly(host->card);
678                         }
679                 }
680         } else {
681                 host->card->type = MMC_TYPE_MMC;
682                 host->card->rca = 1;
683
684                 err = mmc_set_relative_addr(host->card);
685                 if (err != MMC_ERR_NONE)
686                         mmc_card_set_dead(host->card);
687         }
688 }
689
690 static void mmc_read_csd(struct mmc_host *host)
691 {
692         int err;
693
694         if (!host->card)
695                 return;
696         if (mmc_card_dead(host->card))
697                 return;
698
699         err = mmc_send_csd(host->card, host->card->raw_csd);
700         if (err != MMC_ERR_NONE) {
701                 mmc_card_set_dead(host->card);
702                 return;
703         }
704
705         mmc_decode_csd(host->card);
706         mmc_decode_cid(host->card);
707 }
708
709 static void mmc_process_ext_csd(struct mmc_host *host)
710 {
711         int err;
712         u8 *ext_csd;
713
714         if (!host->card)
715                 return;
716         if (mmc_card_dead(host->card))
717                 return;
718         if (mmc_card_sd(host->card))
719                 return;
720         if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
721                 return;
722
723         /*
724          * As the ext_csd is so large and mostly unused, we don't store the
725          * raw block in mmc_card.
726          */
727         ext_csd = kmalloc(512, GFP_KERNEL);
728         if (!ext_csd) {
729                 printk("%s: could not allocate a buffer to receive the ext_csd."
730                        "mmc v4 cards will be treated as v3.\n",
731                         mmc_hostname(host));
732                 return;
733         }
734
735         err = mmc_send_ext_csd(host->card, ext_csd);
736         if (err != MMC_ERR_NONE) {
737                 if (host->card->csd.capacity == (4096 * 512)) {
738                         printk(KERN_ERR "%s: unable to read EXT_CSD "
739                                 "on a possible high capacity card. "
740                                 "Card will be ignored.\n",
741                                 mmc_hostname(host));
742                         mmc_card_set_dead(host->card);
743                 } else {
744                         printk(KERN_WARNING "%s: unable to read "
745                                 "EXT_CSD, performance might "
746                                 "suffer.\n",
747                                 mmc_hostname(host));
748                 }
749                 goto out;
750         }
751
752         host->card->ext_csd.sectors =
753                 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
754                 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
755                 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
756                 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
757         if (host->card->ext_csd.sectors)
758                 mmc_card_set_blockaddr(host->card);
759
760         switch (ext_csd[EXT_CSD_CARD_TYPE]) {
761         case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
762                 host->card->ext_csd.hs_max_dtr = 52000000;
763                 break;
764         case EXT_CSD_CARD_TYPE_26:
765                 host->card->ext_csd.hs_max_dtr = 26000000;
766                 break;
767         default:
768                 /* MMC v4 spec says this cannot happen */
769                 printk("%s: card is mmc v4 but doesn't support "
770                        "any high-speed modes.\n",
771                         mmc_hostname(host));
772                 goto out;
773         }
774
775         if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
776                 /* Activate highspeed support. */
777                 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
778                         EXT_CSD_HS_TIMING, 1);
779                 if (err != MMC_ERR_NONE) {
780                         printk("%s: failed to switch card to mmc v4 "
781                                "high-speed mode.\n",
782                                mmc_hostname(host));
783                         goto out;
784                 }
785
786                 mmc_card_set_highspeed(host->card);
787
788                 host->ios.timing = MMC_TIMING_MMC_HS;
789                 mmc_set_ios(host);
790         }
791
792         /* Check for host support for wide-bus modes. */
793         if (host->caps & MMC_CAP_4_BIT_DATA) {
794                 /* Activate 4-bit support. */
795                 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
796                         EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
797                         EXT_CSD_CMD_SET_NORMAL);
798                 if (err != MMC_ERR_NONE) {
799                         printk("%s: failed to switch card to "
800                                "mmc v4 4-bit bus mode.\n",
801                                mmc_hostname(host));
802                         goto out;
803                 }
804
805                 host->ios.bus_width = MMC_BUS_WIDTH_4;
806                 mmc_set_ios(host);
807         }
808
809 out:
810         kfree(ext_csd);
811 }
812
813 static void mmc_read_scr(struct mmc_host *host)
814 {
815         int err;
816
817         if (!host->card)
818                 return;
819         if (mmc_card_dead(host->card))
820                 return;
821         if (!mmc_card_sd(host->card))
822                 return;
823
824         err = mmc_app_send_scr(host->card, host->card->raw_scr);
825         if (err != MMC_ERR_NONE) {
826                 mmc_card_set_dead(host->card);
827                 return;
828         }
829
830         mmc_decode_scr(host->card);
831 }
832
833 static void mmc_read_switch_caps(struct mmc_host *host)
834 {
835         int err;
836         unsigned char *status;
837
838         if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
839                 return;
840
841         if (!host->card)
842                 return;
843         if (mmc_card_dead(host->card))
844                 return;
845         if (!mmc_card_sd(host->card))
846                 return;
847         if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
848                 return;
849
850         status = kmalloc(64, GFP_KERNEL);
851         if (!status) {
852                 printk(KERN_WARNING "%s: Unable to allocate buffer for "
853                         "reading switch capabilities.\n",
854                         mmc_hostname(host));
855                 return;
856         }
857
858         err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
859                 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
860         if (err != MMC_ERR_NONE) {
861                 printk("%s: unable to read switch capabilities, "
862                         "performance might suffer.\n",
863                         mmc_hostname(host));
864                 goto out;
865         }
866
867         if (status[13] & 0x02)
868                 host->card->sw_caps.hs_max_dtr = 50000000;
869
870         err = mmc_sd_switch(host->card, SD_SWITCH_SET,
871                 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
872         if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
873                 printk(KERN_WARNING "%s: Problem switching card "
874                         "into high-speed mode!\n",
875                         mmc_hostname(host));
876                 goto out;
877         }
878
879         mmc_card_set_highspeed(host->card);
880
881         host->ios.timing = MMC_TIMING_SD_HS;
882         mmc_set_ios(host);
883
884 out:
885         kfree(status);
886 }
887
888 static unsigned int mmc_calculate_clock(struct mmc_host *host)
889 {
890         unsigned int max_dtr = host->f_max;
891
892         if (host->card && !mmc_card_dead(host->card)) {
893                 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
894                         if (max_dtr > host->card->sw_caps.hs_max_dtr)
895                                 max_dtr = host->card->sw_caps.hs_max_dtr;
896                 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
897                         if (max_dtr > host->card->ext_csd.hs_max_dtr)
898                                 max_dtr = host->card->ext_csd.hs_max_dtr;
899                 } else if (max_dtr > host->card->csd.max_dtr) {
900                         max_dtr = host->card->csd.max_dtr;
901                 }
902         }
903
904         pr_debug("%s: selected %d.%03dMHz transfer rate\n",
905                  mmc_hostname(host),
906                  max_dtr / 1000000, (max_dtr / 1000) % 1000);
907
908         return max_dtr;
909 }
910
911 /*
912  * Check whether cards we already know about are still present.
913  * We do this by requesting status, and checking whether a card
914  * responds.
915  *
916  * A request for status does not cause a state change in data
917  * transfer mode.
918  */
919 static void mmc_check_card(struct mmc_card *card)
920 {
921         int err;
922
923         BUG_ON(!card);
924
925         err = mmc_send_status(card, NULL);
926         if (err == MMC_ERR_NONE)
927                 return;
928
929         mmc_card_set_dead(card);
930 }
931
932 static void mmc_setup(struct mmc_host *host)
933 {
934         int err;
935         u32 ocr;
936
937         host->mode = MMC_MODE_SD;
938
939         mmc_power_up(host);
940         mmc_go_idle(host);
941
942         err = mmc_send_if_cond(host, host->ocr_avail);
943         if (err != MMC_ERR_NONE) {
944                 return;
945         }
946         err = mmc_send_app_op_cond(host, 0, &ocr);
947
948         /*
949          * If we fail to detect any SD cards then try
950          * searching for MMC cards.
951          */
952         if (err != MMC_ERR_NONE) {
953                 host->mode = MMC_MODE_MMC;
954
955                 err = mmc_send_op_cond(host, 0, &ocr);
956                 if (err != MMC_ERR_NONE)
957                         return;
958         }
959
960         host->ocr = mmc_select_voltage(host, ocr);
961
962         if (host->ocr == 0)
963                 return;
964
965         /*
966          * Since we're changing the OCR value, we seem to
967          * need to tell some cards to go back to the idle
968          * state.  We wait 1ms to give cards time to
969          * respond.
970          */
971         mmc_go_idle(host);
972
973         /*
974          * Send the selected OCR multiple times... until the cards
975          * all get the idea that they should be ready for CMD2.
976          * (My SanDisk card seems to need this.)
977          */
978         if (host->mode == MMC_MODE_SD) {
979                 /*
980                  * If SD_SEND_IF_COND indicates an SD 2.0
981                  * compliant card and we should set bit 30
982                  * of the ocr to indicate that we can handle
983                  * block-addressed SDHC cards.
984                  */
985                 err = mmc_send_if_cond(host, host->ocr);
986                 if (err == MMC_ERR_NONE)
987                         ocr = host->ocr | (1 << 30);
988
989                 mmc_send_app_op_cond(host, ocr, NULL);
990         } else {
991                 /* The extra bit indicates that we support high capacity */
992                 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
993         }
994
995         mmc_discover_card(host);
996
997         /*
998          * Ok, now switch to push-pull mode.
999          */
1000         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1001         mmc_set_ios(host);
1002
1003         mmc_read_csd(host);
1004
1005         if (host->card && !mmc_card_dead(host->card)) {
1006                 err = mmc_select_card(host->card);
1007                 if (err != MMC_ERR_NONE)
1008                         mmc_card_set_dead(host->card);
1009         }
1010
1011         /*
1012          * The card is in 1 bit mode by default so
1013          * we only need to change if it supports the
1014          * wider version.
1015          */
1016         if (host->card && !mmc_card_dead(host->card) && 
1017                 mmc_card_sd(host->card) &&
1018                 (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
1019                 (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
1020                 err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
1021                 if (err != MMC_ERR_NONE)
1022                         mmc_card_set_dead(host->card);
1023                 else {
1024                         host->ios.bus_width = MMC_BUS_WIDTH_4;
1025                         mmc_set_ios(host);
1026                 }
1027         }
1028
1029         if (host->mode == MMC_MODE_SD) {
1030                 mmc_read_scr(host);
1031                 mmc_read_switch_caps(host);
1032         } else
1033                 mmc_process_ext_csd(host);
1034 }
1035
1036
1037 /**
1038  *      mmc_detect_change - process change of state on a MMC socket
1039  *      @host: host which changed state.
1040  *      @delay: optional delay to wait before detection (jiffies)
1041  *
1042  *      All we know is that card(s) have been inserted or removed
1043  *      from the socket(s).  We don't know which socket or cards.
1044  */
1045 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1046 {
1047 #ifdef CONFIG_MMC_DEBUG
1048         mmc_claim_host(host);
1049         BUG_ON(host->removed);
1050         mmc_release_host(host);
1051 #endif
1052
1053         mmc_schedule_delayed_work(&host->detect, delay);
1054 }
1055
1056 EXPORT_SYMBOL(mmc_detect_change);
1057
1058
1059 static void mmc_rescan(struct work_struct *work)
1060 {
1061         struct mmc_host *host =
1062                 container_of(work, struct mmc_host, detect.work);
1063
1064         mmc_claim_host(host);
1065
1066         /*
1067          * Check for removed card and newly inserted ones. We check for
1068          * removed cards first so we can intelligently re-select the VDD.
1069          */
1070         if (host->card) {
1071                 mmc_check_card(host->card);
1072
1073                 mmc_release_host(host);
1074
1075                 if (mmc_card_dead(host->card)) {
1076                         mmc_remove_card(host->card);
1077                         host->card = NULL;
1078                 }
1079
1080                 goto out;
1081         }
1082
1083         mmc_setup(host);
1084
1085         if (host->card && !mmc_card_dead(host->card)) {
1086                 /*
1087                  * (Re-)calculate the fastest clock rate which the
1088                  * attached cards and the host support.
1089                  */
1090                 host->ios.clock = mmc_calculate_clock(host);
1091                 mmc_set_ios(host);
1092         }
1093
1094         mmc_release_host(host);
1095
1096         /*
1097          * If this is a new and good card, register it.
1098          */
1099         if (host->card && !mmc_card_dead(host->card)) {
1100                 if (mmc_register_card(host->card))
1101                         mmc_card_set_dead(host->card);
1102         }
1103
1104         /*
1105          * If this card is dead, destroy it.
1106          */
1107         if (host->card && mmc_card_dead(host->card)) {
1108                 mmc_remove_card(host->card);
1109                 host->card = NULL;
1110         }
1111
1112 out:
1113         /*
1114          * If we discover that there are no cards on the
1115          * bus, turn off the clock and power down.
1116          */
1117         if (!host->card)
1118                 mmc_power_off(host);
1119 }
1120
1121
1122 /**
1123  *      mmc_alloc_host - initialise the per-host structure.
1124  *      @extra: sizeof private data structure
1125  *      @dev: pointer to host device model structure
1126  *
1127  *      Initialise the per-host structure.
1128  */
1129 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1130 {
1131         struct mmc_host *host;
1132
1133         host = mmc_alloc_host_sysfs(extra, dev);
1134         if (host) {
1135                 spin_lock_init(&host->lock);
1136                 init_waitqueue_head(&host->wq);
1137                 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1138
1139                 /*
1140                  * By default, hosts do not support SGIO or large requests.
1141                  * They have to set these according to their abilities.
1142                  */
1143                 host->max_hw_segs = 1;
1144                 host->max_phys_segs = 1;
1145                 host->max_seg_size = PAGE_CACHE_SIZE;
1146
1147                 host->max_req_size = PAGE_CACHE_SIZE;
1148                 host->max_blk_size = 512;
1149                 host->max_blk_count = PAGE_CACHE_SIZE / 512;
1150         }
1151
1152         return host;
1153 }
1154
1155 EXPORT_SYMBOL(mmc_alloc_host);
1156
1157 /**
1158  *      mmc_add_host - initialise host hardware
1159  *      @host: mmc host
1160  */
1161 int mmc_add_host(struct mmc_host *host)
1162 {
1163         int ret;
1164
1165         ret = mmc_add_host_sysfs(host);
1166         if (ret == 0) {
1167                 mmc_power_off(host);
1168                 mmc_detect_change(host, 0);
1169         }
1170
1171         return ret;
1172 }
1173
1174 EXPORT_SYMBOL(mmc_add_host);
1175
1176 /**
1177  *      mmc_remove_host - remove host hardware
1178  *      @host: mmc host
1179  *
1180  *      Unregister and remove all cards associated with this host,
1181  *      and power down the MMC bus.
1182  */
1183 void mmc_remove_host(struct mmc_host *host)
1184 {
1185 #ifdef CONFIG_MMC_DEBUG
1186         mmc_claim_host(host);
1187         host->removed = 1;
1188         mmc_release_host(host);
1189 #endif
1190
1191         mmc_flush_scheduled_work();
1192
1193         if (host->card) {
1194                 mmc_remove_card(host->card);
1195                 host->card = NULL;
1196         }
1197
1198         mmc_power_off(host);
1199         mmc_remove_host_sysfs(host);
1200 }
1201
1202 EXPORT_SYMBOL(mmc_remove_host);
1203
1204 /**
1205  *      mmc_free_host - free the host structure
1206  *      @host: mmc host
1207  *
1208  *      Free the host once all references to it have been dropped.
1209  */
1210 void mmc_free_host(struct mmc_host *host)
1211 {
1212         mmc_free_host_sysfs(host);
1213 }
1214
1215 EXPORT_SYMBOL(mmc_free_host);
1216
1217 #ifdef CONFIG_PM
1218
1219 /**
1220  *      mmc_suspend_host - suspend a host
1221  *      @host: mmc host
1222  *      @state: suspend mode (PM_SUSPEND_xxx)
1223  */
1224 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1225 {
1226         mmc_flush_scheduled_work();
1227
1228         if (host->card) {
1229                 mmc_remove_card(host->card);
1230                 host->card = NULL;
1231         }
1232
1233         mmc_power_off(host);
1234
1235         return 0;
1236 }
1237
1238 EXPORT_SYMBOL(mmc_suspend_host);
1239
1240 /**
1241  *      mmc_resume_host - resume a previously suspended host
1242  *      @host: mmc host
1243  */
1244 int mmc_resume_host(struct mmc_host *host)
1245 {
1246         mmc_rescan(&host->detect.work);
1247
1248         return 0;
1249 }
1250
1251 EXPORT_SYMBOL(mmc_resume_host);
1252
1253 #endif
1254
1255 MODULE_LICENSE("GPL");