]> err.no Git - linux-2.6/blob - drivers/mmc/core/sd.c
mmc: replace BUG_ON with WARN_ON
[linux-2.6] / drivers / mmc / core / sd.c
1 /*
2  *  linux/drivers/mmc/core/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 "bus.h"
23 #include "mmc_ops.h"
24 #include "sd_ops.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 int 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(KERN_ERR "%s: unrecognised CSD structure version %d\n",
153                         mmc_hostname(card->host), csd_struct);
154                 return -EINVAL;
155         }
156
157         return 0;
158 }
159
160 /*
161  * Given a 64-bit response, decode to our card SCR structure.
162  */
163 static int mmc_decode_scr(struct mmc_card *card)
164 {
165         struct sd_scr *scr = &card->scr;
166         unsigned int scr_struct;
167         u32 resp[4];
168
169         resp[3] = card->raw_scr[1];
170         resp[2] = card->raw_scr[0];
171
172         scr_struct = UNSTUFF_BITS(resp, 60, 4);
173         if (scr_struct != 0) {
174                 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
175                         mmc_hostname(card->host), scr_struct);
176                 return -EINVAL;
177         }
178
179         scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
180         scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
181
182         return 0;
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         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
194                 return 0;
195
196         if (!(card->csd.cmdclass & CCC_SWITCH)) {
197                 printk(KERN_WARNING "%s: card lacks mandatory switch "
198                         "function, performance might suffer.\n",
199                         mmc_hostname(card->host));
200                 return 0;
201         }
202
203         err = -EIO;
204
205         status = kmalloc(64, GFP_KERNEL);
206         if (!status) {
207                 printk(KERN_ERR "%s: could not allocate a buffer for "
208                         "switch capabilities.\n", mmc_hostname(card->host));
209                 return -ENOMEM;
210         }
211
212         err = mmc_sd_switch(card, 0, 0, 1, status);
213         if (err) {
214                 /*
215                  * We all hosts that cannot perform the command
216                  * to fail more gracefully
217                  */
218                 if (err != -EINVAL)
219                         goto out;
220
221                 printk(KERN_WARNING "%s: problem reading switch "
222                         "capabilities, performance might suffer.\n",
223                         mmc_hostname(card->host));
224                 err = 0;
225
226                 goto out;
227         }
228
229         if (status[13] & 0x02)
230                 card->sw_caps.hs_max_dtr = 50000000;
231
232 out:
233         kfree(status);
234
235         return err;
236 }
237
238 /*
239  * Test if the card supports high-speed mode and, if so, switch to it.
240  */
241 static int mmc_switch_hs(struct mmc_card *card)
242 {
243         int err;
244         u8 *status;
245
246         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
247                 return 0;
248
249         if (!(card->csd.cmdclass & CCC_SWITCH))
250                 return 0;
251
252         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
253                 return 0;
254
255         if (card->sw_caps.hs_max_dtr == 0)
256                 return 0;
257
258         err = -EIO;
259
260         status = kmalloc(64, GFP_KERNEL);
261         if (!status) {
262                 printk(KERN_ERR "%s: could not allocate a buffer for "
263                         "switch capabilities.\n", mmc_hostname(card->host));
264                 return -ENOMEM;
265         }
266
267         err = mmc_sd_switch(card, 1, 0, 1, status);
268         if (err)
269                 goto out;
270
271         if ((status[16] & 0xF) != 1) {
272                 printk(KERN_WARNING "%s: Problem switching card "
273                         "into high-speed mode!\n",
274                         mmc_hostname(card->host));
275         } else {
276                 mmc_card_set_highspeed(card);
277                 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
278         }
279
280 out:
281         kfree(status);
282
283         return err;
284 }
285
286 /*
287  * Handle the detection and initialisation of a card.
288  *
289  * In the case of a resume, "curcard" will contain the card
290  * we're trying to reinitialise.
291  */
292 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
293         struct mmc_card *oldcard)
294 {
295         struct mmc_card *card;
296         int err;
297         u32 cid[4];
298         unsigned int max_dtr;
299
300         BUG_ON(!host);
301         WARN_ON(!host->claimed);
302
303         /*
304          * Since we're changing the OCR value, we seem to
305          * need to tell some cards to go back to the idle
306          * state.  We wait 1ms to give cards time to
307          * respond.
308          */
309         mmc_go_idle(host);
310
311         /*
312          * If SD_SEND_IF_COND indicates an SD 2.0
313          * compliant card and we should set bit 30
314          * of the ocr to indicate that we can handle
315          * block-addressed SDHC cards.
316          */
317         err = mmc_send_if_cond(host, ocr);
318         if (!err)
319                 ocr |= 1 << 30;
320
321         err = mmc_send_app_op_cond(host, ocr, NULL);
322         if (err)
323                 goto err;
324
325         /*
326          * Fetch CID from card.
327          */
328         err = mmc_all_send_cid(host, cid);
329         if (err)
330                 goto err;
331
332         if (oldcard) {
333                 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
334                         err = -ENOENT;
335                         goto err;
336                 }
337
338                 card = oldcard;
339         } else {
340                 /*
341                  * Allocate card structure.
342                  */
343                 card = mmc_alloc_card(host);
344                 if (IS_ERR(card)) {
345                         err = PTR_ERR(card);
346                         goto err;
347                 }
348
349                 card->type = MMC_TYPE_SD;
350                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
351         }
352
353         /*
354          * Set card RCA.
355          */
356         err = mmc_send_relative_addr(host, &card->rca);
357         if (err)
358                 goto free_card;
359
360         mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
361
362         if (!oldcard) {
363                 /*
364                  * Fetch CSD from card.
365                  */
366                 err = mmc_send_csd(card, card->raw_csd);
367                 if (err)
368                         goto free_card;
369
370                 err = mmc_decode_csd(card);
371                 if (err)
372                         goto free_card;
373
374                 mmc_decode_cid(card);
375         }
376
377         /*
378          * Select card, as all following commands rely on that.
379          */
380         err = mmc_select_card(card);
381         if (err)
382                 goto free_card;
383
384         if (!oldcard) {
385                 /*
386                  * Fetch SCR from card.
387                  */
388                 err = mmc_app_send_scr(card, card->raw_scr);
389                 if (err)
390                         goto free_card;
391
392                 err = mmc_decode_scr(card);
393                 if (err < 0)
394                         goto free_card;
395
396                 /*
397                  * Fetch switch information from card.
398                  */
399                 err = mmc_read_switch(card);
400                 if (err)
401                         goto free_card;
402         }
403
404         /*
405          * Attempt to change to high-speed (if supported)
406          */
407         err = mmc_switch_hs(card);
408         if (err)
409                 goto free_card;
410
411         /*
412          * Compute bus speed.
413          */
414         max_dtr = (unsigned int)-1;
415
416         if (mmc_card_highspeed(card)) {
417                 if (max_dtr > card->sw_caps.hs_max_dtr)
418                         max_dtr = card->sw_caps.hs_max_dtr;
419         } else if (max_dtr > card->csd.max_dtr) {
420                 max_dtr = card->csd.max_dtr;
421         }
422
423         mmc_set_clock(host, max_dtr);
424
425         /*
426          * Switch to wider bus (if supported).
427          */
428         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
429                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
430                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
431                 if (err)
432                         goto free_card;
433
434                 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
435         }
436
437         /*
438          * Check if read-only switch is active.
439          */
440         if (!oldcard) {
441                 if (!host->ops->get_ro) {
442                         printk(KERN_WARNING "%s: host does not "
443                                 "support reading read-only "
444                                 "switch. assuming write-enable.\n",
445                                 mmc_hostname(host));
446                 } else {
447                         if (host->ops->get_ro(host))
448                                 mmc_card_set_readonly(card);
449                 }
450         }
451
452         if (!oldcard)
453                 host->card = card;
454
455         return 0;
456
457 free_card:
458         if (!oldcard)
459                 mmc_remove_card(card);
460 err:
461
462         return err;
463 }
464
465 /*
466  * Host is being removed. Free up the current card.
467  */
468 static void mmc_sd_remove(struct mmc_host *host)
469 {
470         BUG_ON(!host);
471         BUG_ON(!host->card);
472
473         mmc_remove_card(host->card);
474         host->card = NULL;
475 }
476
477 /*
478  * Card detection callback from host.
479  */
480 static void mmc_sd_detect(struct mmc_host *host)
481 {
482         int err;
483
484         BUG_ON(!host);
485         BUG_ON(!host->card);
486
487         mmc_claim_host(host);
488
489         /*
490          * Just check if our card has been removed.
491          */
492         err = mmc_send_status(host->card, NULL);
493
494         mmc_release_host(host);
495
496         if (err) {
497                 mmc_sd_remove(host);
498
499                 mmc_claim_host(host);
500                 mmc_detach_bus(host);
501                 mmc_release_host(host);
502         }
503 }
504
505 MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
506         card->raw_cid[2], card->raw_cid[3]);
507 MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
508         card->raw_csd[2], card->raw_csd[3]);
509 MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
510 MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year);
511 MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev);
512 MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev);
513 MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid);
514 MMC_ATTR_FN(name, "%s\n", card->cid.prod_name);
515 MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid);
516 MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial);
517
518 static struct device_attribute mmc_sd_dev_attrs[] = {
519         MMC_ATTR_RO(cid),
520         MMC_ATTR_RO(csd),
521         MMC_ATTR_RO(scr),
522         MMC_ATTR_RO(date),
523         MMC_ATTR_RO(fwrev),
524         MMC_ATTR_RO(hwrev),
525         MMC_ATTR_RO(manfid),
526         MMC_ATTR_RO(name),
527         MMC_ATTR_RO(oemid),
528         MMC_ATTR_RO(serial),
529         __ATTR_NULL,
530 };
531
532 /*
533  * Adds sysfs entries as relevant.
534  */
535 static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card)
536 {
537         int ret;
538
539         ret = mmc_add_attrs(card, mmc_sd_dev_attrs);
540         if (ret < 0)
541                 return ret;
542
543         return 0;
544 }
545
546 /*
547  * Removes the sysfs entries added by mmc_sysfs_add().
548  */
549 static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card)
550 {
551         mmc_remove_attrs(card, mmc_sd_dev_attrs);
552 }
553
554 #ifdef CONFIG_MMC_UNSAFE_RESUME
555
556 /*
557  * Suspend callback from host.
558  */
559 static void mmc_sd_suspend(struct mmc_host *host)
560 {
561         BUG_ON(!host);
562         BUG_ON(!host->card);
563
564         mmc_claim_host(host);
565         mmc_deselect_cards(host);
566         host->card->state &= ~MMC_STATE_HIGHSPEED;
567         mmc_release_host(host);
568 }
569
570 /*
571  * Resume callback from host.
572  *
573  * This function tries to determine if the same card is still present
574  * and, if so, restore all state to it.
575  */
576 static void mmc_sd_resume(struct mmc_host *host)
577 {
578         int err;
579
580         BUG_ON(!host);
581         BUG_ON(!host->card);
582
583         mmc_claim_host(host);
584         err = mmc_sd_init_card(host, host->ocr, host->card);
585         mmc_release_host(host);
586
587         if (err) {
588                 mmc_sd_remove(host);
589
590                 mmc_claim_host(host);
591                 mmc_detach_bus(host);
592                 mmc_release_host(host);
593         }
594
595 }
596
597 #else
598
599 #define mmc_sd_suspend NULL
600 #define mmc_sd_resume NULL
601
602 #endif
603
604 static const struct mmc_bus_ops mmc_sd_ops = {
605         .remove = mmc_sd_remove,
606         .detect = mmc_sd_detect,
607         .sysfs_add = mmc_sd_sysfs_add,
608         .sysfs_remove = mmc_sd_sysfs_remove,
609         .suspend = mmc_sd_suspend,
610         .resume = mmc_sd_resume,
611 };
612
613 /*
614  * Starting point for SD card init.
615  */
616 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
617 {
618         int err;
619
620         BUG_ON(!host);
621         WARN_ON(!host->claimed);
622
623         mmc_attach_bus(host, &mmc_sd_ops);
624
625         /*
626          * Sanity check the voltages that the card claims to
627          * support.
628          */
629         if (ocr & 0x7F) {
630                 printk(KERN_WARNING "%s: card claims to support voltages "
631                        "below the defined range. These will be ignored.\n",
632                        mmc_hostname(host));
633                 ocr &= ~0x7F;
634         }
635
636         if (ocr & MMC_VDD_165_195) {
637                 printk(KERN_WARNING "%s: SD card claims to support the "
638                        "incompletely defined 'low voltage range'. This "
639                        "will be ignored.\n", mmc_hostname(host));
640                 ocr &= ~MMC_VDD_165_195;
641         }
642
643         host->ocr = mmc_select_voltage(host, ocr);
644
645         /*
646          * Can we support the voltage(s) of the card(s)?
647          */
648         if (!host->ocr) {
649                 err = -EINVAL;
650                 goto err;
651         }
652
653         /*
654          * Detect and init the card.
655          */
656         err = mmc_sd_init_card(host, host->ocr, NULL);
657         if (err)
658                 goto err;
659
660         mmc_release_host(host);
661
662         err = mmc_add_card(host->card);
663         if (err)
664                 goto remove_card;
665
666         return 0;
667
668 remove_card:
669         mmc_remove_card(host->card);
670         host->card = NULL;
671         mmc_claim_host(host);
672 err:
673         mmc_detach_bus(host);
674         mmc_release_host(host);
675
676         printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
677                 mmc_hostname(host), err);
678
679         return err;
680 }
681