1/*
2 *  linux/drivers/mmc/mmc.c
3 *
4 *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 *  MMCv4 support Copyright (C) 2006 Philip Langdale, 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
23static const unsigned int tran_exp[] = {
24	10000,		100000,		1000000,	10000000,
25	0,		0,		0,		0
26};
27
28static const unsigned char tran_mant[] = {
29	0,	10,	12,	13,	15,	20,	25,	30,
30	35,	40,	45,	50,	55,	60,	70,	80,
31};
32
33static const unsigned int tacc_exp[] = {
34	1,	10,	100,	1000,	10000,	100000,	1000000, 10000000,
35};
36
37static const unsigned int tacc_mant[] = {
38	0,	10,	12,	13,	15,	20,	25,	30,
39	35,	40,	45,	50,	55,	60,	70,	80,
40};
41
42#define UNSTUFF_BITS(resp,start,size)					\
43	({								\
44		const int __size = size;				\
45		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
46		const int __off = 3 - ((start) / 32);			\
47		const int __shft = (start) & 31;			\
48		u32 __res;						\
49									\
50		__res = resp[__off] >> __shft;				\
51		if (__size + __shft > 32)				\
52			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
53		__res & __mask;						\
54	})
55
56/*
57 * Given the decoded CSD structure, decode the raw CID to our CID structure.
58 */
59static int mmc_decode_cid(struct mmc_card *card)
60{
61	u32 *resp = card->raw_cid;
62
63	/*
64	 * The selection of the format here is based upon published
65	 * specs from sandisk and from what people have reported.
66	 */
67	switch (card->csd.mmca_vsn) {
68	case 0: /* MMC v1.0 - v1.2 */
69	case 1: /* MMC v1.4 */
70		card->cid.manfid	= UNSTUFF_BITS(resp, 104, 24);
71		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
72		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
73		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
74		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
75		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
76		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
77		card->cid.prod_name[6]	= UNSTUFF_BITS(resp, 48, 8);
78		card->cid.hwrev		= UNSTUFF_BITS(resp, 44, 4);
79		card->cid.fwrev		= UNSTUFF_BITS(resp, 40, 4);
80		card->cid.serial	= UNSTUFF_BITS(resp, 16, 24);
81		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
82		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
83		break;
84
85	case 2: /* MMC v2.0 - v2.2 */
86	case 3: /* MMC v3.1 - v3.3 */
87	case 4: /* MMC v4 */
88		card->cid.manfid	= UNSTUFF_BITS(resp, 120, 8);
89		card->cid.oemid		= UNSTUFF_BITS(resp, 104, 16);
90		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
91		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
92		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
93		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
94		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
95		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
96		card->cid.serial	= UNSTUFF_BITS(resp, 16, 32);
97		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
98		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
99		break;
100
101	default:
102		printk("%s: card has unknown MMCA version %d\n",
103			mmc_hostname(card->host), card->csd.mmca_vsn);
104		return -EINVAL;
105	}
106
107	return 0;
108}
109
110/*
111 * Given a 128-bit response, decode to our card CSD structure.
112 */
113static int mmc_decode_csd(struct mmc_card *card)
114{
115	struct mmc_csd *csd = &card->csd;
116	unsigned int e, m, csd_struct;
117	u32 *resp = card->raw_csd;
118
119	/*
120	 * We only understand CSD structure v1.1 and v1.2.
121	 * v1.2 has extra information in bits 15, 11 and 10.
122	 */
123	csd_struct = UNSTUFF_BITS(resp, 126, 2);
124	if (csd_struct != 1 && csd_struct != 2) {
125		printk("%s: unrecognised CSD structure version %d\n",
126			mmc_hostname(card->host), csd_struct);
127		return -EINVAL;
128	}
129
130	csd->mmca_vsn	 = UNSTUFF_BITS(resp, 122, 4);
131	m = UNSTUFF_BITS(resp, 115, 4);
132	e = UNSTUFF_BITS(resp, 112, 3);
133	csd->tacc_ns	 = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
134	csd->tacc_clks	 = UNSTUFF_BITS(resp, 104, 8) * 100;
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	e = UNSTUFF_BITS(resp, 47, 3);
142	m = UNSTUFF_BITS(resp, 62, 12);
143	csd->capacity	  = (1 + m) << (e + 2);
144
145	csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
146	csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
147	csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
148	csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
149	csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
150	csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
151	csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
152
153	return 0;
154}
155
156/*
157 * Read and decode extended CSD.
158 */
159static int mmc_read_ext_csd(struct mmc_card *card)
160{
161	int err;
162	u8 *ext_csd;
163
164	BUG_ON(!card);
165
166	err = MMC_ERR_FAILED;
167
168	if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
169		return MMC_ERR_NONE;
170
171	/*
172	 * As the ext_csd is so large and mostly unused, we don't store the
173	 * raw block in mmc_card.
174	 */
175	ext_csd = kmalloc(512, GFP_KERNEL);
176	if (!ext_csd) {
177		printk(KERN_ERR "%s: could not allocate a buffer to "
178			"receive the ext_csd. mmc v4 cards will be "
179			"treated as v3.\n", mmc_hostname(card->host));
180		return MMC_ERR_FAILED;
181	}
182
183	err = mmc_send_ext_csd(card, ext_csd);
184	if (err != MMC_ERR_NONE) {
185		/*
186		 * High capacity cards should have this "magic" size
187		 * stored in their CSD.
188		 */
189		if (card->csd.capacity == (4096 * 512)) {
190			printk(KERN_ERR "%s: unable to read EXT_CSD "
191				"on a possible high capacity card. "
192				"Card will be ignored.\n",
193				mmc_hostname(card->host));
194		} else {
195			printk(KERN_WARNING "%s: unable to read "
196				"EXT_CSD, performance might "
197				"suffer.\n",
198				mmc_hostname(card->host));
199			err = MMC_ERR_NONE;
200		}
201		goto out;
202	}
203
204	card->ext_csd.sectors =
205		ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
206		ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
207		ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
208		ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
209	if (card->ext_csd.sectors)
210		mmc_card_set_blockaddr(card);
211
212	switch (ext_csd[EXT_CSD_CARD_TYPE]) {
213	case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
214		card->ext_csd.hs_max_dtr = 52000000;
215		break;
216	case EXT_CSD_CARD_TYPE_26:
217		card->ext_csd.hs_max_dtr = 26000000;
218		break;
219	default:
220		/* MMC v4 spec says this cannot happen */
221		printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
222			"support any high-speed modes.\n",
223			mmc_hostname(card->host));
224		goto out;
225	}
226
227out:
228	kfree(ext_csd);
229
230	return err;
231}
232
233/*
234 * Handle the detection and initialisation of a card.
235 *
236 * In the case of a resume, "curcard" will contain the card
237 * we're trying to reinitialise.
238 */
239static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
240	struct mmc_card *oldcard)
241{
242	struct mmc_card *card;
243	int err;
244	u32 cid[4];
245	unsigned int max_dtr;
246
247	BUG_ON(!host);
248	BUG_ON(!host->claimed);
249
250	/*
251	 * Since we're changing the OCR value, we seem to
252	 * need to tell some cards to go back to the idle
253	 * state.  We wait 1ms to give cards time to
254	 * respond.
255	 */
256	mmc_go_idle(host);
257
258	/* The extra bit indicates that we support high capacity */
259	err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
260	if (err != MMC_ERR_NONE)
261		goto err;
262
263	/*
264	 * Fetch CID from card.
265	 */
266	err = mmc_all_send_cid(host, cid);
267	if (err != MMC_ERR_NONE)
268		goto err;
269
270	if (oldcard) {
271		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
272			goto err;
273
274		card = oldcard;
275	} else {
276		/*
277		 * Allocate card structure.
278		 */
279		card = mmc_alloc_card(host);
280		if (IS_ERR(card))
281			goto err;
282
283		card->type = MMC_TYPE_MMC;
284		card->rca = 1;
285		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
286	}
287
288	/*
289	 * Set card RCA.
290	 */
291	err = mmc_set_relative_addr(card);
292	if (err != MMC_ERR_NONE)
293		goto free_card;
294
295	mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
296
297	if (!oldcard) {
298		/*
299		 * Fetch CSD from card.
300		 */
301		err = mmc_send_csd(card, card->raw_csd);
302		if (err != MMC_ERR_NONE)
303			goto free_card;
304
305		err = mmc_decode_csd(card);
306		if (err < 0)
307			goto free_card;
308		err = mmc_decode_cid(card);
309		if (err < 0)
310			goto free_card;
311	}
312
313	/*
314	 * Select card, as all following commands rely on that.
315	 */
316	err = mmc_select_card(card);
317	if (err != MMC_ERR_NONE)
318		goto free_card;
319
320	if (!oldcard) {
321		/*
322		 * Fetch and process extened CSD.
323		 */
324		err = mmc_read_ext_csd(card);
325		if (err != MMC_ERR_NONE)
326			goto free_card;
327	}
328
329	/*
330	 * Activate high speed (if supported)
331	 */
332	if ((card->ext_csd.hs_max_dtr != 0) &&
333		(host->caps & MMC_CAP_MMC_HIGHSPEED)) {
334		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
335			EXT_CSD_HS_TIMING, 1);
336		if (err != MMC_ERR_NONE)
337			goto free_card;
338
339		mmc_card_set_highspeed(card);
340
341		mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
342	}
343
344	/*
345	 * Compute bus speed.
346	 */
347	max_dtr = (unsigned int)-1;
348
349	if (mmc_card_highspeed(card)) {
350		if (max_dtr > card->ext_csd.hs_max_dtr)
351			max_dtr = card->ext_csd.hs_max_dtr;
352	} else if (max_dtr > card->csd.max_dtr) {
353		max_dtr = card->csd.max_dtr;
354	}
355
356	mmc_set_clock(host, max_dtr);
357
358	/*
359	 * Activate wide bus (if supported).
360	 */
361	if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
362		(host->caps & MMC_CAP_4_BIT_DATA)) {
363		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
364			EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
365		if (err != MMC_ERR_NONE)
366			goto free_card;
367
368		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
369	}
370
371	if (!oldcard)
372		host->card = card;
373
374	return MMC_ERR_NONE;
375
376free_card:
377	if (!oldcard)
378		mmc_remove_card(card);
379err:
380
381	return MMC_ERR_FAILED;
382}
383
384/*
385 * Host is being removed. Free up the current card.
386 */
387static void mmc_remove(struct mmc_host *host)
388{
389	BUG_ON(!host);
390	BUG_ON(!host->card);
391
392	mmc_remove_card(host->card);
393	host->card = NULL;
394}
395
396/*
397 * Card detection callback from host.
398 */
399static void mmc_detect(struct mmc_host *host)
400{
401	int err;
402
403	BUG_ON(!host);
404	BUG_ON(!host->card);
405
406	mmc_claim_host(host);
407
408	/*
409	 * Just check if our card has been removed.
410	 */
411	err = mmc_send_status(host->card, NULL);
412
413	mmc_release_host(host);
414
415	if (err != MMC_ERR_NONE) {
416		mmc_remove_card(host->card);
417		host->card = NULL;
418
419		mmc_claim_host(host);
420		mmc_detach_bus(host);
421		mmc_release_host(host);
422	}
423}
424
425#ifdef CONFIG_MMC_UNSAFE_RESUME
426
427/*
428 * Suspend callback from host.
429 */
430static void mmc_suspend(struct mmc_host *host)
431{
432	BUG_ON(!host);
433	BUG_ON(!host->card);
434
435	mmc_claim_host(host);
436	mmc_deselect_cards(host);
437	host->card->state &= ~MMC_STATE_HIGHSPEED;
438	mmc_release_host(host);
439}
440
441/*
442 * Resume callback from host.
443 *
444 * This function tries to determine if the same card is still present
445 * and, if so, restore all state to it.
446 */
447static void mmc_resume(struct mmc_host *host)
448{
449	int err;
450
451	BUG_ON(!host);
452	BUG_ON(!host->card);
453
454	mmc_claim_host(host);
455
456	err = mmc_sd_init_card(host, host->ocr, host->card);
457	if (err != MMC_ERR_NONE) {
458		mmc_remove_card(host->card);
459		host->card = NULL;
460
461		mmc_detach_bus(host);
462	}
463
464	mmc_release_host(host);
465}
466
467#else
468
469#define mmc_suspend NULL
470#define mmc_resume NULL
471
472#endif
473
474static const struct mmc_bus_ops mmc_ops = {
475	.remove = mmc_remove,
476	.detect = mmc_detect,
477	.suspend = mmc_suspend,
478	.resume = mmc_resume,
479};
480
481/*
482 * Starting point for MMC card init.
483 */
484int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
485{
486	int err;
487
488	BUG_ON(!host);
489	BUG_ON(!host->claimed);
490
491	mmc_attach_bus(host, &mmc_ops);
492
493	/*
494	 * Sanity check the voltages that the card claims to
495	 * support.
496	 */
497	if (ocr & 0x7F) {
498		printk(KERN_WARNING "%s: card claims to support voltages "
499		       "below the defined range. These will be ignored.\n",
500		       mmc_hostname(host));
501		ocr &= ~0x7F;
502	}
503
504	host->ocr = mmc_select_voltage(host, ocr);
505
506	/*
507	 * Can we support the voltage of the card?
508	 */
509	if (!host->ocr)
510		goto err;
511
512	/*
513	 * Detect and init the card.
514	 */
515	err = mmc_sd_init_card(host, host->ocr, NULL);
516	if (err != MMC_ERR_NONE)
517		goto err;
518
519	mmc_release_host(host);
520
521	err = mmc_register_card(host->card);
522	if (err)
523		goto reclaim_host;
524
525	return 0;
526
527reclaim_host:
528	mmc_claim_host(host);
529	mmc_remove_card(host->card);
530	host->card = NULL;
531err:
532	mmc_detach_bus(host);
533	mmc_release_host(host);
534
535	return 0;
536}
537