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