6597e778f70e28687eab42cfa81a0f00e94ddafc
[safe/jmp/linux-2.6] / drivers / mmc / core / sd.c
1 /*
2  *  linux/drivers/mmc/sd.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  *
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
13 #include <linux/err.h>
14
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/mmc.h>
18
19 #include "core.h"
20 #include "sysfs.h"
21 #include "mmc_ops.h"
22 #include "sd_ops.h"
23
24 #include "core.h"
25
26 static const unsigned int tran_exp[] = {
27         10000,          100000,         1000000,        10000000,
28         0,              0,              0,              0
29 };
30
31 static const unsigned char tran_mant[] = {
32         0,      10,     12,     13,     15,     20,     25,     30,
33         35,     40,     45,     50,     55,     60,     70,     80,
34 };
35
36 static const unsigned int tacc_exp[] = {
37         1,      10,     100,    1000,   10000,  100000, 1000000, 10000000,
38 };
39
40 static const unsigned int tacc_mant[] = {
41         0,      10,     12,     13,     15,     20,     25,     30,
42         35,     40,     45,     50,     55,     60,     70,     80,
43 };
44
45 #define UNSTUFF_BITS(resp,start,size)                                   \
46         ({                                                              \
47                 const int __size = size;                                \
48                 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
49                 const int __off = 3 - ((start) / 32);                   \
50                 const int __shft = (start) & 31;                        \
51                 u32 __res;                                              \
52                                                                         \
53                 __res = resp[__off] >> __shft;                          \
54                 if (__size + __shft > 32)                               \
55                         __res |= resp[__off-1] << ((32 - __shft) % 32); \
56                 __res & __mask;                                         \
57         })
58
59 /*
60  * Given the decoded CSD structure, decode the raw CID to our CID structure.
61  */
62 static void mmc_decode_cid(struct mmc_card *card)
63 {
64         u32 *resp = card->raw_cid;
65
66         memset(&card->cid, 0, sizeof(struct mmc_cid));
67
68         /*
69          * SD doesn't currently have a version field so we will
70          * have to assume we can parse this.
71          */
72         card->cid.manfid                = UNSTUFF_BITS(resp, 120, 8);
73         card->cid.oemid                 = UNSTUFF_BITS(resp, 104, 16);
74         card->cid.prod_name[0]          = UNSTUFF_BITS(resp, 96, 8);
75         card->cid.prod_name[1]          = UNSTUFF_BITS(resp, 88, 8);
76         card->cid.prod_name[2]          = UNSTUFF_BITS(resp, 80, 8);
77         card->cid.prod_name[3]          = UNSTUFF_BITS(resp, 72, 8);
78         card->cid.prod_name[4]          = UNSTUFF_BITS(resp, 64, 8);
79         card->cid.hwrev                 = UNSTUFF_BITS(resp, 60, 4);
80         card->cid.fwrev                 = UNSTUFF_BITS(resp, 56, 4);
81         card->cid.serial                = UNSTUFF_BITS(resp, 24, 32);
82         card->cid.year                  = UNSTUFF_BITS(resp, 12, 8);
83         card->cid.month                 = UNSTUFF_BITS(resp, 8, 4);
84
85         card->cid.year += 2000; /* SD cards year offset */
86 }
87
88 /*
89  * Given a 128-bit response, decode to our card CSD structure.
90  */
91 static void mmc_decode_csd(struct mmc_card *card)
92 {
93         struct mmc_csd *csd = &card->csd;
94         unsigned int e, m, csd_struct;
95         u32 *resp = card->raw_csd;
96
97         csd_struct = UNSTUFF_BITS(resp, 126, 2);
98
99         switch (csd_struct) {
100         case 0:
101                 m = UNSTUFF_BITS(resp, 115, 4);
102                 e = UNSTUFF_BITS(resp, 112, 3);
103                 csd->tacc_ns     = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
104                 csd->tacc_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
105
106                 m = UNSTUFF_BITS(resp, 99, 4);
107                 e = UNSTUFF_BITS(resp, 96, 3);
108                 csd->max_dtr      = tran_exp[e] * tran_mant[m];
109                 csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
110
111                 e = UNSTUFF_BITS(resp, 47, 3);
112                 m = UNSTUFF_BITS(resp, 62, 12);
113                 csd->capacity     = (1 + m) << (e + 2);
114
115                 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
116                 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
117                 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
118                 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
119                 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
120                 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
121                 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
122                 break;
123         case 1:
124                 /*
125                  * This is a block-addressed SDHC card. Most
126                  * interesting fields are unused and have fixed
127                  * values. To avoid getting tripped by buggy cards,
128                  * we assume those fixed values ourselves.
129                  */
130                 mmc_card_set_blockaddr(card);
131
132                 csd->tacc_ns     = 0; /* Unused */
133                 csd->tacc_clks   = 0; /* Unused */
134
135                 m = UNSTUFF_BITS(resp, 99, 4);
136                 e = UNSTUFF_BITS(resp, 96, 3);
137                 csd->max_dtr      = tran_exp[e] * tran_mant[m];
138                 csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
139
140                 m = UNSTUFF_BITS(resp, 48, 22);
141                 csd->capacity     = (1 + m) << 10;
142
143                 csd->read_blkbits = 9;
144                 csd->read_partial = 0;
145                 csd->write_misalign = 0;
146                 csd->read_misalign = 0;
147                 csd->r2w_factor = 4; /* Unused */
148                 csd->write_blkbits = 9;
149                 csd->write_partial = 0;
150                 break;
151         default:
152                 printk("%s: unrecognised CSD structure version %d\n",
153                         mmc_hostname(card->host), csd_struct);
154                 mmc_card_set_bad(card);
155                 return;
156         }
157 }
158
159 /*
160  * Given a 64-bit response, decode to our card SCR structure.
161  */
162 static void mmc_decode_scr(struct mmc_card *card)
163 {
164         struct sd_scr *scr = &card->scr;
165         unsigned int scr_struct;
166         u32 resp[4];
167
168         BUG_ON(!mmc_card_sd(card));
169
170         resp[3] = card->raw_scr[1];
171         resp[2] = card->raw_scr[0];
172
173         scr_struct = UNSTUFF_BITS(resp, 60, 4);
174         if (scr_struct != 0) {
175                 printk("%s: unrecognised SCR structure version %d\n",
176                         mmc_hostname(card->host), scr_struct);
177                 mmc_card_set_bad(card);
178                 return;
179         }
180
181         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
182         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
183 }
184
185 /*
186  * Fetches and decodes switch information
187  */
188 static int mmc_read_switch(struct mmc_card *card)
189 {
190         int err;
191         u8 *status;
192
193         err = MMC_ERR_FAILED;
194
195         status = kmalloc(64, GFP_KERNEL);
196         if (!status) {
197                 printk("%s: could not allocate a buffer for switch "
198                        "capabilities.\n",
199                         mmc_hostname(card->host));
200                 return err;
201         }
202
203         err = mmc_sd_switch(card, 0, 0, 1, status);
204         if (err != MMC_ERR_NONE) {
205                 /*
206                  * Card not supporting high-speed will ignore the
207                  * command.
208                  */
209                 err = MMC_ERR_NONE;
210                 goto out;
211         }
212
213         if (status[13] & 0x02)
214                 card->sw_caps.hs_max_dtr = 50000000;
215
216 out:
217         kfree(status);
218
219         return err;
220 }
221
222 /*
223  * Test if the card supports high-speed mode and, if so, switch to it.
224  */
225 static int mmc_switch_hs(struct mmc_card *card)
226 {
227         int err;
228         u8 *status;
229
230         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
231                 return MMC_ERR_NONE;
232
233         if (card->sw_caps.hs_max_dtr == 0)
234                 return MMC_ERR_NONE;
235
236         err = MMC_ERR_FAILED;
237
238         status = kmalloc(64, GFP_KERNEL);
239         if (!status) {
240                 printk("%s: could not allocate a buffer for switch "
241                        "capabilities.\n",
242                         mmc_hostname(card->host));
243                 return err;
244         }
245
246         err = mmc_sd_switch(card, 1, 0, 1, status);
247         if (err != MMC_ERR_NONE)
248                 goto out;
249
250         if ((status[16] & 0xF) != 1) {
251                 printk(KERN_WARNING "%s: Problem switching card "
252                         "into high-speed mode!\n",
253                         mmc_hostname(card->host));
254         } else {
255                 mmc_card_set_highspeed(card);
256                 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
257         }
258
259 out:
260         kfree(status);
261
262         return err;
263 }
264
265 /*
266  * Handle the detection and initialisation of a card.
267  *
268  * In the case of a resume, "curcard" will contain the card
269  * we're trying to reinitialise.
270  */
271 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
272         struct mmc_card *oldcard)
273 {
274         struct mmc_card *card;
275         int err;
276         u32 cid[4];
277         unsigned int max_dtr;
278
279         BUG_ON(!host);
280         BUG_ON(!host->claimed);
281
282         /*
283          * Since we're changing the OCR value, we seem to
284          * need to tell some cards to go back to the idle
285          * state.  We wait 1ms to give cards time to
286          * respond.
287          */
288         mmc_go_idle(host);
289
290         /*
291          * If SD_SEND_IF_COND indicates an SD 2.0
292          * compliant card and we should set bit 30
293          * of the ocr to indicate that we can handle
294          * block-addressed SDHC cards.
295          */
296         err = mmc_send_if_cond(host, ocr);
297         if (err == MMC_ERR_NONE)
298                 ocr |= 1 << 30;
299
300         err = mmc_send_app_op_cond(host, ocr, NULL);
301         if (err != MMC_ERR_NONE)
302                 goto err;
303
304         /*
305          * Fetch CID from card.
306          */
307         err = mmc_all_send_cid(host, cid);
308         if (err != MMC_ERR_NONE)
309                 goto err;
310
311         if (oldcard) {
312                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
313                         goto err;
314
315                 card = oldcard;
316         } else {
317                 /*
318                  * Allocate card structure.
319                  */
320                 card = mmc_alloc_card(host);
321                 if (IS_ERR(card))
322                         goto err;
323
324                 card->type = MMC_TYPE_SD;
325                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
326         }
327
328         /*
329          * Set card RCA.
330          */
331         err = mmc_send_relative_addr(host, &card->rca);
332         if (err != MMC_ERR_NONE)
333                 goto free_card;
334
335         mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
336
337         if (!oldcard) {
338                 /*
339                  * Fetch CSD from card.
340                  */
341                 err = mmc_send_csd(card, card->raw_csd);
342                 if (err != MMC_ERR_NONE)
343                         goto free_card;
344
345                 mmc_decode_csd(card);
346                 mmc_decode_cid(card);
347         }
348
349         /*
350          * Select card, as all following commands rely on that.
351          */
352         err = mmc_select_card(card);
353         if (err != MMC_ERR_NONE)
354                 goto free_card;
355
356         if (!oldcard) {
357                 /*
358                  * Fetch SCR from card.
359                  */
360                 err = mmc_app_send_scr(card, card->raw_scr);
361                 if (err != MMC_ERR_NONE)
362                         goto free_card;
363
364                 mmc_decode_scr(card);
365
366                 /*
367                  * Fetch switch information from card.
368                  */
369                 err = mmc_read_switch(card);
370                 if (err != MMC_ERR_NONE)
371                         goto free_card;
372         }
373
374         /*
375          * Attempt to change to high-speed (if supported)
376          */
377         err = mmc_switch_hs(card);
378         if (err != MMC_ERR_NONE)
379                 goto free_card;
380
381         /*
382          * Compute bus speed.
383          */
384         max_dtr = (unsigned int)-1;
385
386         if (mmc_card_highspeed(card)) {
387                 if (max_dtr > card->sw_caps.hs_max_dtr)
388                         max_dtr = card->sw_caps.hs_max_dtr;
389         } else if (max_dtr > card->csd.max_dtr) {
390                 max_dtr = card->csd.max_dtr;
391         }
392
393         mmc_set_clock(host, max_dtr);
394
395         /*
396          * Switch to wider bus (if supported).
397          */
398         if ((host->caps && MMC_CAP_4_BIT_DATA) &&
399                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
400                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
401                 if (err != MMC_ERR_NONE)
402                         goto free_card;
403
404                 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
405         }
406
407         if (!oldcard)
408                 host->card = card;
409
410         return MMC_ERR_NONE;
411
412 free_card:
413         if (!oldcard)
414                 mmc_remove_card(card);
415 err:
416
417         return MMC_ERR_FAILED;
418 }
419
420 /*
421  * Host is being removed. Free up the current card.
422  */
423 static void mmc_sd_remove(struct mmc_host *host)
424 {
425         BUG_ON(!host);
426         BUG_ON(!host->card);
427
428         mmc_remove_card(host->card);
429         host->card = NULL;
430 }
431
432 /*
433  * Card detection callback from host.
434  */
435 static void mmc_sd_detect(struct mmc_host *host)
436 {
437         int err;
438
439         BUG_ON(!host);
440         BUG_ON(!host->card);
441
442         mmc_claim_host(host);
443
444         /*
445          * Just check if our card has been removed.
446          */
447         err = mmc_send_status(host->card, NULL);
448
449         mmc_release_host(host);
450
451         if (err != MMC_ERR_NONE) {
452                 mmc_remove_card(host->card);
453                 host->card = NULL;
454
455                 mmc_claim_host(host);
456                 mmc_detach_bus(host);
457                 mmc_release_host(host);
458         }
459 }
460
461 #ifdef CONFIG_MMC_UNSAFE_RESUME
462
463 /*
464  * Suspend callback from host.
465  */
466 static void mmc_sd_suspend(struct mmc_host *host)
467 {
468         BUG_ON(!host);
469         BUG_ON(!host->card);
470
471         mmc_claim_host(host);
472         mmc_deselect_cards(host);
473         host->card->state &= ~MMC_STATE_HIGHSPEED;
474         mmc_release_host(host);
475 }
476
477 /*
478  * Resume callback from host.
479  *
480  * This function tries to determine if the same card is still present
481  * and, if so, restore all state to it.
482  */
483 static void mmc_sd_resume(struct mmc_host *host)
484 {
485         int err;
486
487         BUG_ON(!host);
488         BUG_ON(!host->card);
489
490         mmc_claim_host(host);
491
492         err = mmc_sd_init_card(host, host->ocr, host->card);
493         if (err != MMC_ERR_NONE) {
494                 mmc_remove_card(host->card);
495                 host->card = NULL;
496
497                 mmc_detach_bus(host);
498         }
499
500         mmc_release_host(host);
501 }
502
503 #else
504
505 #define mmc_sd_suspend NULL
506 #define mmc_sd_resume NULL
507
508 #endif
509
510 static const struct mmc_bus_ops mmc_sd_ops = {
511         .remove = mmc_sd_remove,
512         .detect = mmc_sd_detect,
513         .suspend = mmc_sd_suspend,
514         .resume = mmc_sd_resume,
515 };
516
517 /*
518  * Starting point for SD card init.
519  */
520 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
521 {
522         int err;
523
524         BUG_ON(!host);
525         BUG_ON(!host->claimed);
526
527         mmc_attach_bus(host, &mmc_sd_ops);
528
529         /*
530          * Sanity check the voltages that the card claims to
531          * support.
532          */
533         if (ocr & 0x7F) {
534                 printk(KERN_WARNING "%s: card claims to support voltages "
535                        "below the defined range. These will be ignored.\n",
536                        mmc_hostname(host));
537                 ocr &= ~0x7F;
538         }
539
540         if (ocr & MMC_VDD_165_195) {
541                 printk(KERN_WARNING "%s: SD card claims to support the "
542                        "incompletely defined 'low voltage range'. This "
543                        "will be ignored.\n", mmc_hostname(host));
544                 ocr &= ~MMC_VDD_165_195;
545         }
546
547         host->ocr = mmc_select_voltage(host, ocr);
548
549         /*
550          * Can we support the voltage(s) of the card(s)?
551          */
552         if (!host->ocr)
553                 goto err;
554
555         /*
556          * Detect and init the card.
557          */
558         err = mmc_sd_init_card(host, host->ocr, NULL);
559         if (err != MMC_ERR_NONE)
560                 goto err;
561
562         mmc_release_host(host);
563
564         err = mmc_register_card(host->card);
565         if (err)
566                 goto reclaim_host;
567
568         return 0;
569
570 reclaim_host:
571         mmc_claim_host(host);
572         mmc_remove_card(host->card);
573         host->card = NULL;
574 err:
575         mmc_detach_bus(host);
576         mmc_release_host(host);
577
578         return 0;
579 }
580