xref: /linux/drivers/mmc/core/sd.c (revision eb2bce7f5e7ac1ca6da434461217fadf3c688d2c)
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 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("%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 	BUG_ON(!mmc_card_sd(card));
170 
171 	resp[3] = card->raw_scr[1];
172 	resp[2] = card->raw_scr[0];
173 
174 	scr_struct = UNSTUFF_BITS(resp, 60, 4);
175 	if (scr_struct != 0) {
176 		printk("%s: unrecognised SCR structure version %d\n",
177 			mmc_hostname(card->host), scr_struct);
178 		return -EINVAL;
179 	}
180 
181 	scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
182 	scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
183 
184 	return 0;
185 }
186 
187 /*
188  * Fetches and decodes switch information
189  */
190 static int mmc_read_switch(struct mmc_card *card)
191 {
192 	int err;
193 	u8 *status;
194 
195 	err = MMC_ERR_FAILED;
196 
197 	status = kmalloc(64, GFP_KERNEL);
198 	if (!status) {
199 		printk("%s: could not allocate a buffer for switch "
200 		       "capabilities.\n",
201 			mmc_hostname(card->host));
202 		return err;
203 	}
204 
205 	err = mmc_sd_switch(card, 0, 0, 1, status);
206 	if (err != MMC_ERR_NONE) {
207 		/*
208 		 * Card not supporting high-speed will ignore the
209 		 * command.
210 		 */
211 		err = MMC_ERR_NONE;
212 		goto out;
213 	}
214 
215 	if (status[13] & 0x02)
216 		card->sw_caps.hs_max_dtr = 50000000;
217 
218 out:
219 	kfree(status);
220 
221 	return err;
222 }
223 
224 /*
225  * Test if the card supports high-speed mode and, if so, switch to it.
226  */
227 static int mmc_switch_hs(struct mmc_card *card)
228 {
229 	int err;
230 	u8 *status;
231 
232 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
233 		return MMC_ERR_NONE;
234 
235 	if (card->sw_caps.hs_max_dtr == 0)
236 		return MMC_ERR_NONE;
237 
238 	err = MMC_ERR_FAILED;
239 
240 	status = kmalloc(64, GFP_KERNEL);
241 	if (!status) {
242 		printk("%s: could not allocate a buffer for switch "
243 		       "capabilities.\n",
244 			mmc_hostname(card->host));
245 		return err;
246 	}
247 
248 	err = mmc_sd_switch(card, 1, 0, 1, status);
249 	if (err != MMC_ERR_NONE)
250 		goto out;
251 
252 	if ((status[16] & 0xF) != 1) {
253 		printk(KERN_WARNING "%s: Problem switching card "
254 			"into high-speed mode!\n",
255 			mmc_hostname(card->host));
256 	} else {
257 		mmc_card_set_highspeed(card);
258 		mmc_set_timing(card->host, MMC_TIMING_SD_HS);
259 	}
260 
261 out:
262 	kfree(status);
263 
264 	return err;
265 }
266 
267 /*
268  * Handle the detection and initialisation of a card.
269  *
270  * In the case of a resume, "curcard" will contain the card
271  * we're trying to reinitialise.
272  */
273 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
274 	struct mmc_card *oldcard)
275 {
276 	struct mmc_card *card;
277 	int err;
278 	u32 cid[4];
279 	unsigned int max_dtr;
280 
281 	BUG_ON(!host);
282 	BUG_ON(!host->claimed);
283 
284 	/*
285 	 * Since we're changing the OCR value, we seem to
286 	 * need to tell some cards to go back to the idle
287 	 * state.  We wait 1ms to give cards time to
288 	 * respond.
289 	 */
290 	mmc_go_idle(host);
291 
292 	/*
293 	 * If SD_SEND_IF_COND indicates an SD 2.0
294 	 * compliant card and we should set bit 30
295 	 * of the ocr to indicate that we can handle
296 	 * block-addressed SDHC cards.
297 	 */
298 	err = mmc_send_if_cond(host, ocr);
299 	if (err == MMC_ERR_NONE)
300 		ocr |= 1 << 30;
301 
302 	err = mmc_send_app_op_cond(host, ocr, NULL);
303 	if (err != MMC_ERR_NONE)
304 		goto err;
305 
306 	/*
307 	 * Fetch CID from card.
308 	 */
309 	err = mmc_all_send_cid(host, cid);
310 	if (err != MMC_ERR_NONE)
311 		goto err;
312 
313 	if (oldcard) {
314 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
315 			goto err;
316 
317 		card = oldcard;
318 	} else {
319 		/*
320 		 * Allocate card structure.
321 		 */
322 		card = mmc_alloc_card(host);
323 		if (IS_ERR(card))
324 			goto err;
325 
326 		card->type = MMC_TYPE_SD;
327 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
328 	}
329 
330 	/*
331 	 * Set card RCA.
332 	 */
333 	err = mmc_send_relative_addr(host, &card->rca);
334 	if (err != MMC_ERR_NONE)
335 		goto free_card;
336 
337 	mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
338 
339 	if (!oldcard) {
340 		/*
341 		 * Fetch CSD from card.
342 		 */
343 		err = mmc_send_csd(card, card->raw_csd);
344 		if (err != MMC_ERR_NONE)
345 			goto free_card;
346 
347 		err = mmc_decode_csd(card);
348 		if (err < 0)
349 			goto free_card;
350 
351 		mmc_decode_cid(card);
352 	}
353 
354 	/*
355 	 * Select card, as all following commands rely on that.
356 	 */
357 	err = mmc_select_card(card);
358 	if (err != MMC_ERR_NONE)
359 		goto free_card;
360 
361 	if (!oldcard) {
362 		/*
363 		 * Fetch SCR from card.
364 		 */
365 		err = mmc_app_send_scr(card, card->raw_scr);
366 		if (err != MMC_ERR_NONE)
367 			goto free_card;
368 
369 		err = mmc_decode_scr(card);
370 		if (err < 0)
371 			goto free_card;
372 
373 		/*
374 		 * Fetch switch information from card.
375 		 */
376 		err = mmc_read_switch(card);
377 		if (err != MMC_ERR_NONE)
378 			goto free_card;
379 	}
380 
381 	/*
382 	 * Attempt to change to high-speed (if supported)
383 	 */
384 	err = mmc_switch_hs(card);
385 	if (err != MMC_ERR_NONE)
386 		goto free_card;
387 
388 	/*
389 	 * Compute bus speed.
390 	 */
391 	max_dtr = (unsigned int)-1;
392 
393 	if (mmc_card_highspeed(card)) {
394 		if (max_dtr > card->sw_caps.hs_max_dtr)
395 			max_dtr = card->sw_caps.hs_max_dtr;
396 	} else if (max_dtr > card->csd.max_dtr) {
397 		max_dtr = card->csd.max_dtr;
398 	}
399 
400 	mmc_set_clock(host, max_dtr);
401 
402 	/*
403 	 * Switch to wider bus (if supported).
404 	 */
405 	if ((host->caps && MMC_CAP_4_BIT_DATA) &&
406 		(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
407 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
408 		if (err != MMC_ERR_NONE)
409 			goto free_card;
410 
411 		mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
412 	}
413 
414 	if (!oldcard)
415 		host->card = card;
416 
417 	return MMC_ERR_NONE;
418 
419 free_card:
420 	if (!oldcard)
421 		mmc_remove_card(card);
422 err:
423 
424 	return MMC_ERR_FAILED;
425 }
426 
427 /*
428  * Host is being removed. Free up the current card.
429  */
430 static void mmc_sd_remove(struct mmc_host *host)
431 {
432 	BUG_ON(!host);
433 	BUG_ON(!host->card);
434 
435 	mmc_remove_card(host->card);
436 	host->card = NULL;
437 }
438 
439 /*
440  * Card detection callback from host.
441  */
442 static void mmc_sd_detect(struct mmc_host *host)
443 {
444 	int err;
445 
446 	BUG_ON(!host);
447 	BUG_ON(!host->card);
448 
449 	mmc_claim_host(host);
450 
451 	/*
452 	 * Just check if our card has been removed.
453 	 */
454 	err = mmc_send_status(host->card, NULL);
455 
456 	mmc_release_host(host);
457 
458 	if (err != MMC_ERR_NONE) {
459 		mmc_remove_card(host->card);
460 		host->card = NULL;
461 
462 		mmc_claim_host(host);
463 		mmc_detach_bus(host);
464 		mmc_release_host(host);
465 	}
466 }
467 
468 #ifdef CONFIG_MMC_UNSAFE_RESUME
469 
470 /*
471  * Suspend callback from host.
472  */
473 static void mmc_sd_suspend(struct mmc_host *host)
474 {
475 	BUG_ON(!host);
476 	BUG_ON(!host->card);
477 
478 	mmc_claim_host(host);
479 	mmc_deselect_cards(host);
480 	host->card->state &= ~MMC_STATE_HIGHSPEED;
481 	mmc_release_host(host);
482 }
483 
484 /*
485  * Resume callback from host.
486  *
487  * This function tries to determine if the same card is still present
488  * and, if so, restore all state to it.
489  */
490 static void mmc_sd_resume(struct mmc_host *host)
491 {
492 	int err;
493 
494 	BUG_ON(!host);
495 	BUG_ON(!host->card);
496 
497 	mmc_claim_host(host);
498 
499 	err = mmc_sd_init_card(host, host->ocr, host->card);
500 	if (err != MMC_ERR_NONE) {
501 		mmc_remove_card(host->card);
502 		host->card = NULL;
503 
504 		mmc_detach_bus(host);
505 	}
506 
507 	mmc_release_host(host);
508 }
509 
510 #else
511 
512 #define mmc_sd_suspend NULL
513 #define mmc_sd_resume NULL
514 
515 #endif
516 
517 static const struct mmc_bus_ops mmc_sd_ops = {
518 	.remove = mmc_sd_remove,
519 	.detect = mmc_sd_detect,
520 	.suspend = mmc_sd_suspend,
521 	.resume = mmc_sd_resume,
522 };
523 
524 /*
525  * Starting point for SD card init.
526  */
527 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
528 {
529 	int err;
530 
531 	BUG_ON(!host);
532 	BUG_ON(!host->claimed);
533 
534 	mmc_attach_bus(host, &mmc_sd_ops);
535 
536 	/*
537 	 * Sanity check the voltages that the card claims to
538 	 * support.
539 	 */
540 	if (ocr & 0x7F) {
541 		printk(KERN_WARNING "%s: card claims to support voltages "
542 		       "below the defined range. These will be ignored.\n",
543 		       mmc_hostname(host));
544 		ocr &= ~0x7F;
545 	}
546 
547 	if (ocr & MMC_VDD_165_195) {
548 		printk(KERN_WARNING "%s: SD card claims to support the "
549 		       "incompletely defined 'low voltage range'. This "
550 		       "will be ignored.\n", mmc_hostname(host));
551 		ocr &= ~MMC_VDD_165_195;
552 	}
553 
554 	host->ocr = mmc_select_voltage(host, ocr);
555 
556 	/*
557 	 * Can we support the voltage(s) of the card(s)?
558 	 */
559 	if (!host->ocr)
560 		goto err;
561 
562 	/*
563 	 * Detect and init the card.
564 	 */
565 	err = mmc_sd_init_card(host, host->ocr, NULL);
566 	if (err != MMC_ERR_NONE)
567 		goto err;
568 
569 	mmc_release_host(host);
570 
571 	err = mmc_register_card(host->card);
572 	if (err)
573 		goto reclaim_host;
574 
575 	return 0;
576 
577 reclaim_host:
578 	mmc_claim_host(host);
579 	mmc_remove_card(host->card);
580 	host->card = NULL;
581 err:
582 	mmc_detach_bus(host);
583 	mmc_release_host(host);
584 
585 	return 0;
586 }
587 
588