mmc.c 17.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * (C) Copyright 2003
 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <config.h>
#include <common.h>
#include <mmc.h>
#include <asm/errno.h>
#include <asm/arch/hardware.h>
wdenk's avatar
wdenk committed
29
#include <part.h>
30
31
32

#ifdef CONFIG_MMC

stefano babic's avatar
stefano babic committed
33
extern int fat_register_device(block_dev_desc_t * dev_desc, int part_no);
wdenk's avatar
wdenk committed
34
35
36

static block_dev_desc_t mmc_dev;

stefano babic's avatar
stefano babic committed
37
block_dev_desc_t *mmc_get_dev(int dev)
wdenk's avatar
wdenk committed
38
{
stefano babic's avatar
stefano babic committed
39
	return ((block_dev_desc_t *) & mmc_dev);
wdenk's avatar
wdenk committed
40
}
41

wdenk's avatar
wdenk committed
42
/*
43
44
45
46
 * FIXME needs to read cid and csd info to determine block size
 * and other parameters
 */
static uchar mmc_buf[MMC_BLOCK_SIZE];
stefano babic's avatar
stefano babic committed
47
static uchar spec_ver;
48
static int mmc_ready = 0;
stefano babic's avatar
stefano babic committed
49
static int wide = 0;
50

stefano babic's avatar
stefano babic committed
51
static uint32_t *
52
53
54
55
/****************************************************/
mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
/****************************************************/
{
stefano babic's avatar
stefano babic committed
56
	static uint32_t resp[4], a, b, c;
57
	ulong status;
stefano babic's avatar
stefano babic committed
58
	int i;
59

stefano babic's avatar
stefano babic committed
60
61
	debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd, argh, argl,
	      cmdat | wide);
62
63
	MMC_STRPCL = MMC_STRPCL_STOP_CLK;
	MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
stefano babic's avatar
stefano babic committed
64
65
66
67
68
	while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF)) ;
	MMC_CMD = cmd;
	MMC_ARGH = argh;
	MMC_ARGL = argl;
	MMC_CMDAT = cmdat | wide;
69
70
	MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
	MMC_STRPCL = MMC_STRPCL_START_CLK;
stefano babic's avatar
stefano babic committed
71
	while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES)) ;
72
73

	status = MMC_STAT;
stefano babic's avatar
stefano babic committed
74
	debug("MMC status 0x%08x\n", status);
75
	if (status & MMC_STAT_TIME_OUT_RESPONSE) {
76
77
78
		return 0;
	}

stefano babic's avatar
stefano babic committed
79
80
81
82
83
84
85
86
87
88
89
	/* Linux says:
	 * Did I mention this is Sick.  We always need to
	 * discard the upper 8 bits of the first 16-bit word.
	 */
	a = (MMC_RES & 0xffff);
	for (i = 0; i < 4; i++) {
		b = (MMC_RES & 0xffff);
		c = (MMC_RES & 0xffff);
		resp[i] = (a << 24) | (b << 8) | (c >> 8);
		a = c;
		debug("MMC resp[%d] = %#08x\n", i, resp[i]);
90
91
92
93
94
95
96
	}

	return resp;
}

int
/****************************************************/
stefano babic's avatar
stefano babic committed
97
mmc_block_read(uchar * dst, ulong src, ulong len)
98
99
100
101
102
/****************************************************/
{
	ushort argh, argl;
	ulong status;

103
	if (len == 0) {
104
105
106
		return 0;
	}

stefano babic's avatar
stefano babic committed
107
	debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong) dst, src, len);
108
109
110
111
112

	argh = len >> 16;
	argl = len & 0xffff;

	/* set block len */
stefano babic's avatar
stefano babic committed
113
	mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
114
115
116
117
118
119
120
121

	/* send read command */
	argh = src >> 16;
	argl = src & 0xffff;
	MMC_STRPCL = MMC_STRPCL_STOP_CLK;
	MMC_RDTO = 0xffff;
	MMC_NOB = 1;
	MMC_BLKLEN = len;
122
	mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK, argh, argl,
stefano babic's avatar
stefano babic committed
123
124
		MMC_CMDAT_R1 | MMC_CMDAT_READ | MMC_CMDAT_BLOCK |
		MMC_CMDAT_DATA_EN);
wdenk's avatar
wdenk committed
125

126
	MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
127
128
129
130
	while (len) {
		if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
#ifdef CONFIG_PXA27X
			int i;
stefano babic's avatar
stefano babic committed
131
132
			for (i = min(len, 32); i; i--) {
				*dst++ = *((volatile uchar *)&MMC_RXFIFO);
133
134
135
				len--;
			}
#else
136
137
			*dst++ = MMC_RXFIFO;
			len--;
138
#endif
139
140
		}
		status = MMC_STAT;
141
		if (status & MMC_STAT_ERRORS) {
142
143
144
145
146
			printf("MMC_STAT error %lx\n", status);
			return -1;
		}
	}
	MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
stefano babic's avatar
stefano babic committed
147
	while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
148
	status = MMC_STAT;
149
	if (status & MMC_STAT_ERRORS) {
150
151
152
153
154
155
156
157
		printf("MMC_STAT error %lx\n", status);
		return -1;
	}
	return 0;
}

int
/****************************************************/
stefano babic's avatar
stefano babic committed
158
mmc_block_write(ulong dst, uchar * src, int len)
159
160
161
162
163
/****************************************************/
{
	ushort argh, argl;
	ulong status;

164
	if (len == 0) {
165
166
167
		return 0;
	}

stefano babic's avatar
stefano babic committed
168
	debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong) src, len);
169
170
171
172
173

	argh = len >> 16;
	argl = len & 0xffff;

	/* set block len */
stefano babic's avatar
stefano babic committed
174
	mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
175
176
177
178
179
180
181

	/* send write command */
	argh = dst >> 16;
	argl = dst & 0xffff;
	MMC_STRPCL = MMC_STRPCL_STOP_CLK;
	MMC_NOB = 1;
	MMC_BLKLEN = len;
stefano babic's avatar
stefano babic committed
182
183
184
	mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
		MMC_CMDAT_R1 | MMC_CMDAT_WRITE | MMC_CMDAT_BLOCK |
		MMC_CMDAT_DATA_EN);
wdenk's avatar
wdenk committed
185

186
	MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
187
188
	while (len) {
		if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
stefano babic's avatar
stefano babic committed
189
			int i, bytes = min(32, len);
190

stefano babic's avatar
stefano babic committed
191
			for (i = 0; i < bytes; i++) {
192
193
				MMC_TXFIFO = *src++;
			}
194
			if (bytes < 32) {
195
196
197
198
199
				MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
			}
			len -= bytes;
		}
		status = MMC_STAT;
200
		if (status & MMC_STAT_ERRORS) {
201
202
203
204
205
			printf("MMC_STAT error %lx\n", status);
			return -1;
		}
	}
	MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
stefano babic's avatar
stefano babic committed
206
	while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
207
	MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
stefano babic's avatar
stefano babic committed
208
	while (!(MMC_I_REG & MMC_I_REG_PRG_DONE)) ;
209
	status = MMC_STAT;
210
	if (status & MMC_STAT_ERRORS) {
211
212
213
214
215
216
217
218
		printf("MMC_STAT error %lx\n", status);
		return -1;
	}
	return 0;
}

int
/****************************************************/
stefano babic's avatar
stefano babic committed
219
mmc_read(ulong src, uchar * dst, int size)
220
221
222
223
224
/****************************************************/
{
	ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
	ulong mmc_block_size, mmc_block_address;

225
	if (size == 0) {
226
227
228
		return 0;
	}

229
	if (!mmc_ready) {
230
231
232
233
234
235
236
		printf("Please initial the MMC first\n");
		return -1;
	}

	mmc_block_size = MMC_BLOCK_SIZE;
	mmc_block_address = ~(mmc_block_size - 1);

237
	src -= CONFIG_SYS_MMC_BASE;
238
239
240
241
242
243
244
	end = src + size;
	part_start = ~mmc_block_address & src;
	part_end = ~mmc_block_address & end;
	aligned_start = mmc_block_address & src;
	aligned_end = mmc_block_address & end;

	/* all block aligned accesses */
stefano babic's avatar
stefano babic committed
245
246
247
248
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
249
	if (part_start) {
250
		part_len = mmc_block_size - part_start;
stefano babic's avatar
stefano babic committed
251
252
253
254
255
256
		debug
		    ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     src, (ulong) dst, end, part_start, part_end, aligned_start,
		     aligned_end);
		if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
		    0) {
257
258
			return -1;
		}
stefano babic's avatar
stefano babic committed
259
		memcpy(dst, mmc_buf + part_start, part_len);
260
261
262
		dst += part_len;
		src += part_len;
	}
stefano babic's avatar
stefano babic committed
263
264
265
266
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
267
	for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
stefano babic's avatar
stefano babic committed
268
269
270
271
272
		debug
		    ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     src, (ulong) dst, end, part_start, part_end, aligned_start,
		     aligned_end);
		if ((mmc_block_read((uchar *) (dst), src, mmc_block_size)) < 0) {
273
274
275
			return -1;
		}
	}
stefano babic's avatar
stefano babic committed
276
277
278
279
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
280
	if (part_end && src < end) {
stefano babic's avatar
stefano babic committed
281
282
283
284
		debug
		    ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     src, (ulong) dst, end, part_start, part_end, aligned_start,
		     aligned_end);
285
		if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
286
287
288
289
290
291
292
293
294
			return -1;
		}
		memcpy(dst, mmc_buf, part_end);
	}
	return 0;
}

int
/****************************************************/
stefano babic's avatar
stefano babic committed
295
mmc_write(uchar * src, ulong dst, int size)
296
297
298
299
300
/****************************************************/
{
	ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
	ulong mmc_block_size, mmc_block_address;

301
	if (size == 0) {
302
303
304
		return 0;
	}

305
	if (!mmc_ready) {
306
307
308
309
310
311
312
		printf("Please initial the MMC first\n");
		return -1;
	}

	mmc_block_size = MMC_BLOCK_SIZE;
	mmc_block_address = ~(mmc_block_size - 1);

313
	dst -= CONFIG_SYS_MMC_BASE;
314
315
316
317
318
319
320
	end = dst + size;
	part_start = ~mmc_block_address & dst;
	part_end = ~mmc_block_address & end;
	aligned_start = mmc_block_address & dst;
	aligned_end = mmc_block_address & end;

	/* all block aligned accesses */
stefano babic's avatar
stefano babic committed
321
322
323
324
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
325
	if (part_start) {
326
		part_len = mmc_block_size - part_start;
stefano babic's avatar
stefano babic committed
327
328
329
330
331
332
		debug
		    ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     (ulong) src, dst, end, part_start, part_end, aligned_start,
		     aligned_end);
		if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
		    0) {
333
334
			return -1;
		}
stefano babic's avatar
stefano babic committed
335
336
337
		memcpy(mmc_buf + part_start, src, part_len);
		if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) <
		    0) {
338
339
340
341
342
			return -1;
		}
		dst += part_len;
		src += part_len;
	}
stefano babic's avatar
stefano babic committed
343
344
345
346
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
347
	for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
stefano babic's avatar
stefano babic committed
348
349
350
351
352
		debug
		    ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     src, (ulong) dst, end, part_start, part_end, aligned_start,
		     aligned_end);
		if ((mmc_block_write(dst, (uchar *) src, mmc_block_size)) < 0) {
353
354
355
			return -1;
		}
	}
stefano babic's avatar
stefano babic committed
356
357
358
359
	debug
	    ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
	     src, (ulong) dst, end, part_start, part_end, aligned_start,
	     aligned_end);
360
	if (part_end && dst < end) {
stefano babic's avatar
stefano babic committed
361
362
363
364
		debug
		    ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
		     src, (ulong) dst, end, part_start, part_end, aligned_start,
		     aligned_end);
365
		if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
366
367
368
			return -1;
		}
		memcpy(mmc_buf, src, part_end);
369
		if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
370
371
372
373
374
375
			return -1;
		}
	}
	return 0;
}

wdenk's avatar
wdenk committed
376
ulong
377
/****************************************************/
378
mmc_bread(int dev_num, ulong blknr, lbaint_t blkcnt, void *dst)
379
380
381
/****************************************************/
{
	int mmc_block_size = MMC_BLOCK_SIZE;
382
	ulong src = blknr * mmc_block_size + CONFIG_SYS_MMC_BASE;
383

stefano babic's avatar
stefano babic committed
384
	mmc_read(src, (uchar *) dst, blkcnt * mmc_block_size);
385
386
387
	return blkcnt;
}

stefano babic's avatar
stefano babic committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#ifdef __GNUC__
#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)
#else
#define likely(x)	(x)
#define unlikely(x)	(x)
#endif

#define UNSTUFF_BITS(resp,start,size)					\
	({								\
		const int __size = size;				\
		const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
		const int32_t __off = 3 - ((start) / 32);			\
		const int32_t __shft = (start) & 31;			\
		uint32_t __res;						\
									\
		__res = resp[__off] >> __shft;				\
		if (__size + __shft > 32)				\
			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
		__res & __mask;						\
	})

/*
 * Given the decoded CSD structure, decode the raw CID to our CID structure.
 */
static void mmc_decode_cid(uint32_t * resp)
{
	if (IF_TYPE_SD == mmc_dev.if_type) {
		/*
		 * SD doesn't currently have a version field so we will
		 * have to assume we can parse this.
		 */
		sprintf((char *)mmc_dev.vendor,
			"Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
			UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp, 112, 8),
			UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp, 96, 8),
			UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp, 80, 8),
			UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp, 64, 8),
			UNSTUFF_BITS(resp, 8, 4), UNSTUFF_BITS(resp, 12,
							       8) + 2000);
		sprintf((char *)mmc_dev.revision, "%d.%d",
			UNSTUFF_BITS(resp, 60, 4), UNSTUFF_BITS(resp, 56, 4));
		sprintf((char *)mmc_dev.product, "%u",
			UNSTUFF_BITS(resp, 24, 32));
	} else {
		/*
		 * The selection of the format here is based upon published
		 * specs from sandisk and from what people have reported.
		 */
		switch (spec_ver) {
		case 0:	/* MMC v1.0 - v1.2 */
		case 1:	/* MMC v1.4 */
			sprintf((char *)mmc_dev.vendor,
				"Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
				UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
									 112,
									 8),
				UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp,
									 96, 8),
				UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp,
									80, 8),
				UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp,
									64, 8),
				UNSTUFF_BITS(resp, 56, 8), UNSTUFF_BITS(resp,
									48, 8),
				UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
									4) +
				1997);
			sprintf((char *)mmc_dev.revision, "%d.%d",
				UNSTUFF_BITS(resp, 44, 4), UNSTUFF_BITS(resp,
									40, 4));
			sprintf((char *)mmc_dev.product, "%u",
				UNSTUFF_BITS(resp, 16, 24));
			break;

		case 2:	/* MMC v2.0 - v2.2 */
		case 3:	/* MMC v3.1 - v3.3 */
		case 4:	/* MMC v4 */
			sprintf((char *)mmc_dev.vendor,
				"Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
				UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
									 104,
									 16),
				UNSTUFF_BITS(resp, 96, 8), UNSTUFF_BITS(resp,
									88, 8),
				UNSTUFF_BITS(resp, 80, 8), UNSTUFF_BITS(resp,
									72, 8),
				UNSTUFF_BITS(resp, 64, 8), UNSTUFF_BITS(resp,
									56, 8),
				UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
									4) +
				1997);
			sprintf((char *)mmc_dev.product, "%u",
				UNSTUFF_BITS(resp, 16, 32));
			sprintf((char *)mmc_dev.revision, "N/A");
			break;

		default:
			printf("MMC card has unknown MMCA version %d\n",
			       spec_ver);
			break;
		}
	}
	printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
	       (IF_TYPE_SD == mmc_dev.if_type) ? "SD" : "MMC", mmc_dev.vendor,
	       mmc_dev.product, mmc_dev.revision);
}

/*
 * Given a 128-bit response, decode to our card CSD structure.
 */
static void mmc_decode_csd(uint32_t * resp)
{
	unsigned int mult, csd_struct;

	if (IF_TYPE_SD == mmc_dev.if_type) {
		csd_struct = UNSTUFF_BITS(resp, 126, 2);
		if (csd_struct != 0) {
			printf("SD: unrecognised CSD structure version %d\n",
			       csd_struct);
			return;
		}
	} else {
		/*
		 * We only understand CSD structure v1.1 and v1.2.
		 * v1.2 has extra information in bits 15, 11 and 10.
		 */
		csd_struct = UNSTUFF_BITS(resp, 126, 2);
		if (csd_struct != 1 && csd_struct != 2) {
			printf("MMC: unrecognised CSD structure version %d\n",
			       csd_struct);
			return;
		}

		spec_ver = UNSTUFF_BITS(resp, 122, 4);
		mmc_dev.if_type = IF_TYPE_MMC;
	}

	mult = 1 << (UNSTUFF_BITS(resp, 47, 3) + 2);
	mmc_dev.lba = (1 + UNSTUFF_BITS(resp, 62, 12)) * mult;
	mmc_dev.blksz = 1 << UNSTUFF_BITS(resp, 80, 4);

	/* FIXME: The following just makes assumes that's the partition type -- should really read it */
	mmc_dev.part_type = PART_TYPE_DOS;
	mmc_dev.dev = 0;
	mmc_dev.lun = 0;
	mmc_dev.type = DEV_TYPE_HARDDISK;
	mmc_dev.removable = 0;
	mmc_dev.block_read = mmc_bread;

538
539
540
541
	printf("Detected: %lu blocks of %lu bytes (%luMB) ",
		mmc_dev.lba,
		mmc_dev.blksz,
		mmc_dev.lba * mmc_dev.blksz / (1024 * 1024));
stefano babic's avatar
stefano babic committed
542
543
}

544
545
int
/****************************************************/
546
mmc_legacy_init(int verbose)
547
548
/****************************************************/
{
stefano babic's avatar
stefano babic committed
549
550
551
552
553
554
555
	int retries, rc = -ENODEV;
	uint32_t cid_resp[4];
	uint32_t *resp;
	uint16_t rca = 0;

	/* Reset device interface type */
	mmc_dev.if_type = IF_TYPE_UNKNOWN;
556

stefano babic's avatar
stefano babic committed
557
558
559
#if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
	set_GPIO_mode(GPIO6_MMCCLK_MD);
	set_GPIO_mode(GPIO8_MMCCS0_MD);
560
#endif
stefano babic's avatar
stefano babic committed
561
	CKEN |= CKEN12_MMC;	/* enable MMC unit clock */
562

stefano babic's avatar
stefano babic committed
563
564
565
	MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
	MMC_RESTO = MMC_RES_TO_MAX;
	MMC_SPI = MMC_SPI_DISABLE;
566
567

	/* reset */
568
	mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, 0, MMC_CMDAT_INIT | MMC_CMDAT_R0);
stefano babic's avatar
stefano babic committed
569
570
571
572
573
574
575
576
577
	udelay(200000);
	retries = 3;
	while (retries--) {
		resp = mmc_cmd(MMC_CMD_APP_CMD, 0, 0, MMC_CMDAT_R1);
		if (!(resp[0] & 0x00000020)) {	/* Card does not support APP_CMD */
			debug("Card does not support APP_CMD\n");
			break;
		}

578
579
		/* Select 3.2-3.3V and 3.3-3.4V */
		resp = mmc_cmd(SD_CMD_APP_SEND_OP_COND, 0x0030, 0x0000,
580
581
				MMC_CMDAT_R3 | (retries < 2 ? 0
					: MMC_CMDAT_INIT));
stefano babic's avatar
stefano babic committed
582
583
584
585
586
		if (resp[0] & 0x80000000) {
			mmc_dev.if_type = IF_TYPE_SD;
			debug("Detected SD card\n");
			break;
		}
587
588
589
#ifdef CONFIG_PXA27X
		udelay(10000);
#else
stefano babic's avatar
stefano babic committed
590
		udelay(200000);
591
#endif
stefano babic's avatar
stefano babic committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
	}

	if (retries <= 0 || !(IF_TYPE_SD == mmc_dev.if_type)) {
		debug("Failed to detect SD Card, trying MMC\n");
		resp =
		    mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000, MMC_CMDAT_R3);

		retries = 10;
		while (retries-- && resp && !(resp[0] & 0x80000000)) {
#ifdef CONFIG_PXA27X
			udelay(10000);
#else
			udelay(200000);
#endif
			resp =
			    mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000,
				    MMC_CMDAT_R3);
		}
610
611
612
	}

	/* try to get card id */
stefano babic's avatar
stefano babic committed
613
614
	resp =
	    mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, 0, MMC_CMDAT_R2 | MMC_CMDAT_BUSY);
615
	if (resp) {
stefano babic's avatar
stefano babic committed
616
		memcpy(cid_resp, resp, sizeof(cid_resp));
617
618

		/* MMC exists, get CSD too */
619
		resp = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, 0, 0, MMC_CMDAT_R1);
stefano babic's avatar
stefano babic committed
620
621
622
		if (IF_TYPE_SD == mmc_dev.if_type)
			rca = ((resp[0] & 0xffff0000) >> 16);
		resp = mmc_cmd(MMC_CMD_SEND_CSD, rca, 0, MMC_CMDAT_R2);
623
		if (resp) {
stefano babic's avatar
stefano babic committed
624
			mmc_decode_csd(resp);
625
626
627
			rc = 0;
			mmc_ready = 1;
		}
stefano babic's avatar
stefano babic committed
628
629

		mmc_decode_cid(cid_resp);
630
631
	}

stefano babic's avatar
stefano babic committed
632
633
634
	MMC_CLKRT = 0;		/* 20 MHz */
	resp = mmc_cmd(MMC_CMD_SELECT_CARD, rca, 0, MMC_CMDAT_R1);

635
#ifdef CONFIG_PXA27X
stefano babic's avatar
stefano babic committed
636
637
638
639
640
	if (IF_TYPE_SD == mmc_dev.if_type) {
		resp = mmc_cmd(MMC_CMD_APP_CMD, rca, 0, MMC_CMDAT_R1);
		resp = mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH, 0, 2, MMC_CMDAT_R1);
		wide = MMC_CMDAT_SD_4DAT;
	}
641
#endif
642

stefano babic's avatar
stefano babic committed
643
	fat_register_device(&mmc_dev, 1);	/* partitions start counting with 1 */
644
645
646
647

	return rc;
}

stefano babic's avatar
stefano babic committed
648
#endif /* CONFIG_MMC */