summaryrefslogtreecommitdiffstats
path: root/data/sysrom/sdcard.c
blob: a0ead30df677234d0994f37c9c5fd5924dc2d472 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2010 H. Peter Anvin - All Rights Reserved
 *
 *   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, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * sdcard.c
 *
 * SD card block driver
 *
 * Note: the handling of read operations is tricky, because they pick up
 * the results from the *previous* transaction.  Therefore there are some
 * serious subtleties, especially with which byte position in the shift
 * register the result ends up in and what the size flag should be set to.
 */

#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <ioreg.h>
#include "sysrom.h"
#include "ff.h"
#include "diskio.h"

#define SECTOR_SHIFT	9
#define SECTOR_SIZE	(1UL << SECTOR_SHIFT)

enum sdcard_cmd {
	CMD_GO_IDLE_STATE 		=  0,	/* a.k.a. reset */
	CMD_SEND_OP_COND		=  1,
	CMD_SWITCH_FUNC			=  6,
	CMD_SEND_IF_COND		=  8,
	CMD_SEND_CSD			=  9,
	CMD_SEND_CID			= 10,
	CMD_STOP_TRANSMISSION		= 12,
	CMD_SEND_STATUS			= 13,
	CMD_SET_BLOCKLEN		= 16,
	CMD_READ_SINGLE_BLOCK		= 17,
	CMD_READ_MULTIPLE_BLOCK		= 18,
	CMD_WRITE_BLOCK			= 24,
	CMD_WRITE_MULTIPLE_BLOCK	= 25,
	CMD_PROGRAM_CSD			= 27,
	CMD_SET_WRITE_PROT		= 28,
	CMD_CLR_WRITE_PROT		= 29,
	CMD_SEND_WRITE_PROT		= 30,
	CMD_ERASE_WR_BLK_START_ADDR	= 32,
	CMD_ERASE_WR_BLK_END_ADDR	= 33,
	CMD_ERASE			= 38,
	CMD_LOCK_UNLOCK			= 42,
	CMD_APP_CMD			= 55,
	CMD_GEN_CMD			= 56,
	CMD_READ_OCR			= 58,
	CMD_CRC_ON_OFF			= 59,
};
enum sdcard_acmd {
	ACMD_SD_STATUS			= 13,
	ACMD_SEND_NUM_WR_BLOCKS		= 22,
	ACMD_SET_WR_BLK_ERASE_COUNT	= 23,
	ACMD_SD_SEND_OP_COND		= 41,
	ACMD_SET_CLR_CARD_DETECT	= 42,
	ACMD_SEND_SCR			= 51,
};


struct sdcard_csd {
	uint32_t raw[4];
};

struct sdcard_cid {
	uint32_t raw[4];
};

struct sdcard_info {
	DSTATUS	 status;
	int8_t   card_type;
	unsigned long lbasize;
	uint32_t if_cond;
	uint32_t ocr;
	struct sdcard_csd csd;
	struct sdcard_cid cid;
};

struct sdcard_info sdc = {
	.status = STA_NOINIT
};

/* < 0 if it should be left on, 0 for off, > 0 for off after timeout */
static volatile int8_t sdcard_led;

/*
 * Called by the timer interrupt
 */
void sdcard_timer_tick(void)
{
	if (sdcard_led > 0) {
		if (--sdcard_led == 0)
			*IO_SYS_LED &= ~0x01;
	}
}

/*
 * Enable LED
 */
static void sdcard_led_on(void)
{
	sdcard_led = -1;
	*IO_SYS_LED |= 0x01;
}

/*
 * Disable LED after timeout
 */
static void sdcard_led_off(void)
{
	sdcard_led = 4;		/* 4 ticks @ 64 Hz = 62.5 ms */
}

static int sdcard_send_cmd(uint8_t opcode, uint32_t argument)
{
	int8_t crc7, status;
	int i;

	writeb(opcode | 0x40, IO_SDC_WR_B(SDC_CLEAR_CRC|SDC_CRC_OUT|
					  SDC_BYTE|SDC_START));
	writel(argument, IO_SDC_WR_L(SDC_CRC_OUT|SDC_START));
	crc7 = readb(IO_SDC_RD_CRC7(0));
	writeb(crc7, IO_SDC_WR_B(SDC_BYTE|SDC_START));

	/* The spec says a reply within 8 cycles, cut it some slack */
	for (i = 16; i; i--) {
		status = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START|SDC_CLEAR_CRC));
		if (status >= 0) /* Bit 7 = 0 for a valid reply */
			return status;
	}

	return -1;		/* Error */
}

static int sdcard_send_acmd(uint8_t opcode, uint32_t argument)
{
	int rv;

	/* CMD55 = application command follows */
	rv = sdcard_send_cmd(CMD_APP_CMD, 0);
	if (rv & 0x04)		/* Unknown command (very old card)? */
		return rv;
	return sdcard_send_cmd(opcode, argument);
}

/*
 * Read a data block of length len, with a timeout of (timeout)
 * byte-times.  Note that the minimum length is 4 by spec; under certain
 * circumstances this function will have a lower minimum.
 *
 * The input buffer is allowed to be unaligned.
 */
static int sdcard_read_block(void *buf, int len, int timeout, const void *xcmd)
{
	uint8_t tok;
	uint32_t *pl;
	uint16_t *pw;
	uint8_t  *pb = buf;
	uint16_t zcrc;
	const uint16_t *xcp = xcmd;
	int i;

	for (;;) {
		tok = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START|SDC_CLEAR_CRC));
		if (tok == 0xfe)
			break;
		if (tok < 0xfe) {
			printf("sdcard_read_block: bad token: %02x\n", tok);
			return -1; /* Bad token */
		}
		if (!--timeout) {
			printf("sdcard_read_block: reply timeout\n");
			return -1; /* Timeout */
		}
	}

	/* Shift in the next data byte */
	if ((unsigned long)pb & 1) {
		*pb++ = readb(IO_SDC_RD_B(SDC_START|SDC_BYTE));
		len--;
	} else {
		writeb(0xff, IO_SDC_WR_B(SDC_START|SDC_BYTE));
	}

	/*
	 * Are we supposed to send a command in parallel?
	 * This is unsafe for small blocks, but we only need to do this
	 * for full sectors (used to send STOP_TRANSMISSION).
	 */
	if (xcp)
		len -= 6;	/* Handle the last 6 bytes specially */

	pl = (uint32_t *)pb;

	{
		unsigned int dwords = len >> 2;
		uint16_t loops  = dwords >> 3;
		uint16_t skip   = (~dwords & 7) << 1;

		asm volatile(" jmp (2f,%%pc,%3:w)\n"
			     "1:\n"
			     " move.l (%2),(%0)+\n"
			     "2:\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " move.l (%2),(%0)+\n"
			     " dbf %1,1b\n"
			     : "+a" (pl), "+d" (loops)
			     : "a" (IO_SDC_RD_L(SDC_START)), "a" (skip)
			     : "memory");
	}

	pw = (uint16_t *)pl;
	if (len & 2)
		*pw++ = readw(IO_SDC_RD_W(SDC_START));

	pb = (uint8_t *)pw;
	if (len & 1)
		*pb++ = readb(IO_SDC_RD_BH(SDC_START|SDC_BYTE));

	/*
	 * If we're sending a command in parallel, then we have to
	 * read in the last 6 bytes in parallel with transmitting the
	 * command out.  Because pb may be misaligned at this point,
	 * do the reads as byte I/O on the host bus side.
	 */
	if (xcp) {
		for (i = 0; i < 3; i++) {
			writew(*xcp++, IO_SDC_WR_W(0));
			*pb++ = readb(IO_SDC_RD_BH(0));
			*pb++ = readb(IO_SDC_RD_B(SDC_START));
		}
	}

	/*
	 * Now the CRC is latched in the shift register, and the CRC
	 * in the CRC generator should be zero.  Shift in the first
	 * byte after the CRC for the next round.
	 */

	zcrc = readw(IO_SDC_RD_CRC16(SDC_START|SDC_BYTE));

	if (zcrc != 0x0000) {
		printf("sdcard_read_block: CRC error (zcrc = %04x)\n", zcrc);
		return -1;
	}

	return 0;
}

/*
 * Read a number of sectors; returns the number of sectors read.
 * The input buffer may be unaligned.
 */
int sdcard_read_sectors(void *buf, uint32_t lba, int count)
{
	int rv;
	int okcount = 0;
	static const uint16_t stop_transmission[3] =
		{ (0x40|CMD_STOP_TRANSMISSION) << 8, 0x0000, 0x0061 };
	uint8_t resp;
	uint8_t *p = buf;

	if (!count)
		return 0;

	sdcard_led_on();

	printf("sdcard: reading %d sectors at %u to %p\n", count, lba, buf);

	if (sdc.card_type == 1)
		lba <<= SECTOR_SHIFT;	/* Convert to a byte address */

	rv = sdcard_send_cmd(CMD_READ_MULTIPLE_BLOCK, lba);
	if (rv) {
		printf("sdcard: read_multiple error %02x\n", rv);
		goto out;
	}

	while (count--) {
		rv = sdcard_read_block(p, SECTOR_SIZE, 200000,
				       count ? NULL : stop_transmission);
		if (rv)
			goto out;

		okcount++;
		p += SECTOR_SIZE;
	}

	/* The first byte after the stop command is undefined */
	(void)readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));

	/* Wait for the response to the STOP TRANSMISSION command */
	do {
		resp = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));
	} while (resp & 0x80);

	if (resp) {
		printf("sdcard: read_sectors: terminate command error %02x\n",
		       resp);
	}

out:
	sdcard_led_off();
	return okcount;
}

DRESULT disk_read(BYTE drive, BYTE *buffer,
		  DWORD sectornumber, BYTE sectorcount)
{
	int rv;

	(void)drive;

	rv = sdcard_read_sectors(buffer, sectornumber, sectorcount);
	return (rv == sectorcount) ? RES_OK : RES_ERROR;
}

/*
 * Read CSD/CID
 */
static int sdcard_read_reg(uint8_t opcode, void *buf)
{
	int rv;

	rv = sdcard_send_cmd(opcode, 0);
	if (rv)
		return rv;

	return sdcard_read_block(buf, 16, 2000, NULL);
}

/*
 * Write a number of sectors; returns the number of sectors written.
 * The buffer may be unaligned.
 */
int sdcard_write_sectors(const void *buf, uint32_t lba, int count)
{
	int rv;
	int okcount = 0;
	const uint8_t *p = buf;
	uint16_t crc;
	uint8_t resp;

	if (!count)
		return 0;

	sdcard_led_on();

	printf("sdcard: writing %d sectors at %u from %p\n", count, lba, buf);

	if (sdc.card_type == 1)
		lba <<= SECTOR_SHIFT;	/* Convert to a byte address */

	rv = sdcard_send_cmd(CMD_WRITE_MULTIPLE_BLOCK, lba);
	if (rv) {
		printf("sdcard: write_multiple error %02x\n", rv);
		goto out;
	}

	while (count--) {
		uint16_t dbfloops = (SECTOR_SIZE >> 5) - 1;
		bool odd = (size_t)buf & 1;

		/* Start block token */
		writeb(0xfc, IO_SDC_WR_B(SDC_BYTE|SDC_START));

		/* Clear the CRC generator */
		writeb(0xff, IO_SDC_WR_B(SDC_CRC_OUT|SDC_CLEAR_CRC));

		if (odd) {
			writeb(*p++, IO_SDC_WR_B(SDC_CRC_OUT|
						 SDC_START|SDC_BYTE));
		}

		asm volatile(" jmp (1f,%%pc,%3:w)\n"
			     "1:\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " move.l (%1)+,(%2)\n"
			     " dbf %0,1b"
			     : "+d" (dbfloops), "+a" (p)
			     : "a" (IO_SDC_WR_L(SDC_CRC_OUT|SDC_START)),
			       "a" ((uint16_t)((uint16_t)odd << 1)));

		if (odd) {
			writeb(*p++, IO_SDC_WR_B(SDC_CRC_OUT|
						 SDC_START|SDC_BYTE));
		}

		crc = readw(IO_SDC_RD_CRC16(0));
		writew(crc, IO_SDC_WR_W(SDC_CRC_OUT|SDC_START));

		/* Wait for data response token */
		do {
			resp = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));
		} while ((resp & 0x11) != 0x01);

		resp &= ~0xe0;
		if (resp != 0x05) {
			/*
			 * Things are confusing here... the spec says
			 * that on error we are supposed to issue a
			 * STOP_TRANSMISSION command, which isn't the normal
			 * thing to do for a write... figure this out later.
			 */
			break;	/* Error */
		}

		/* Wait until the card is ready for the next block */
		do {
			resp = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));
		} while (resp == 0x00);

		okcount++;
	}

	/* Send stop transmission token */
	writeb(0xfd, IO_SDC_WR_B(SDC_BYTE|SDC_START));

	/* Wait for the card to go busy, then unbusy */
	do {
		resp = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));
	} while (resp != 0x00);
	do {
		resp = readb(IO_SDC_RD_B(SDC_BYTE|SDC_START));
	} while (resp == 0x00);

out:
	sdcard_led_off();
	return okcount;
}

DRESULT disk_write(BYTE drive, const BYTE *buffer, DWORD sectornumber, BYTE sectorcount)
{
	int rv;

	if (drive != 0)
		return STA_NOINIT;

	rv = sdcard_write_sectors(buffer, sectornumber, sectorcount);
	return (rv == sectorcount) ? RES_OK : RES_ERROR;
}

DRESULT disk_ioctl(BYTE drive, BYTE command, void *buffer)
{
	if (drive != 0)
		return STA_NOINIT;

	switch (command) {
	case CTRL_SYNC:
		return RES_OK;
	case GET_SECTOR_SIZE:
		*(WORD *)buffer = 512;
		return RES_OK;
	case GET_SECTOR_COUNT:
		*(DWORD *)buffer = sdc.lbasize;
		return RES_OK;
	case GET_BLOCK_SIZE:
		*(DWORD *)buffer = 1; /* XXX */
		return RES_OK;
	case CTRL_ERASE_SECTOR:
		return RES_OK;	/* XXX */
	default:
		return RES_PARERR;
	}
}

DWORD get_fattime(void)
{
	/* No RTC, return 2010-11-11 11:11:11 */
	return ((2010-1980) << 25) + (11 << 21) + (11 << 16) +
		(11 << 11) + (11 << 5) + (11 >> 1);
}

static unsigned long sdcard_compute_size(struct sdcard_info *sdi)
{
	unsigned int c_size;
	unsigned int c_size_mult;
	unsigned int read_bl_len;
	unsigned long lbasize;

	sdi->card_type = (sdi->csd.raw[0] >> 30)+1;
	switch (sdi->card_type) {
	case 1:			/* Classic SD/MMC card */
		c_size = ((sdi->csd.raw[2] & 0x3ff) << 2) +
			(sdi->csd.raw[3] >> 30);
		c_size_mult = (sdi->csd.raw[2] >> 15) & 7;
		read_bl_len = (sdi->csd.raw[1] >> 16) & 0xf;

		lbasize = (c_size + 1) << (c_size_mult + read_bl_len + 2 - 9);
		break;
	case 2:			/* SDHC/SDXC/eMMC card */
		c_size = ((sdi->csd.raw[1] & 0x3f) << 16) +
			(sdi->csd.raw[2] >> 16);

		lbasize = c_size << 10;
		break;
	default:
		sdi->card_type = 0;
		return 0;
	}

	return sdi->lbasize = lbasize;
}

static const char *sdcard_type_name(uint8_t type)
{
	static const char * const names[] = {
		"unknown",
		"SD/MMC",
		"SDHC/SDXC/eMMC"
	};


	if (type >= sizeof names/sizeof names[0])
		type = 0;

	return names[type];
}

DSTATUS disk_initialize(BYTE drive)
{
	uint16_t  status;
	uint32_t try_sdhc;
	int i, rv;

	if (drive != 0)
		return STA_NOINIT;

	memset(&sdc, 0, sizeof sdc);

	status = readw(IO_SDC_RD_STATUS(0));

	if (!(status & 0x04)) {
		printf("No memory card installed\n");
		return sdc.status = STA_NOINIT|STA_NODISK;
	}

	sdcard_led_on();

	/* Generate 256 clock cycles in slow mode, with CS# high */
	writew(0x03, IO_SDC_WR_CTL(0)); /* Slow mode, CS# high */
	for (i = 0; i < 16; i++)
		writew(0xffff, IO_SDC_WR_W(SDC_START));

	/* Assert CS# and send reset command */
	writew(0x01, IO_SDC_WR_CTL(0)); /* Slow mode, CS# low */
	rv = sdcard_send_cmd(CMD_GO_IDLE_STATE, 0);
	if (rv != 0x01) {
		printf("sdcard: CMD0 error %02x\n", rv);
		sdcard_led_off();
		return sdc.status = STA_NOINIT;
	}

	/* Switch to fast mode */
	writew(0x00, IO_SDC_WR_CTL(0));

	/* Enable command CRC checking (ignore result) */
	sdcard_send_cmd(CMD_CRC_ON_OFF, 0x0001);

	/* Probe for extended features */
	/*
	 * Bit  7:0 = check pattern
	 * Bit 11:8 = supply voltage (3.3 V)
	 */
	rv = sdcard_send_cmd(CMD_SEND_IF_COND, 0x01aa);
	if ((rv & 0x04) == 0) {
		/* CMD8 supported */
		if (rv & ~0x03) {
			printf("sdcard; CMD8 error %02x\n", rv);
			sdcard_led_off();
			return sdc.status = STA_NOINIT;
		}

		/* Shift in the second data byte */
		writeb(0xff, IO_SDC_WR_B(SDC_START|SDC_BYTE));

		sdc.if_cond = readl(IO_SDC_RD_L(SDC_START));

		printf("sdcard: CMD8 returned 0x%08x\n", sdc.if_cond);

		if ((sdc.if_cond & 0x1ff) != 0x1aa) {
			printf("sdcard: CMD8 reports unusable card (0x%x)\n",
			       sdc.if_cond);
			sdcard_led_off();
			return sdc.status = STA_NOINIT;
		}
		try_sdhc = 1 << 30;
	} else {
		try_sdhc = 0;
	}

	/* Initialize card */
	do {
		rv = sdcard_send_acmd(ACMD_SD_SEND_OP_COND, try_sdhc);
		if (rv & 0x04)
			break;

		if (rv & ~0x01) {
			printf("sdcard: ACMD41 error %02x\n", rv);
			sdcard_led_off();
			return sdc.status = STA_NOINIT;
		}
	} while (rv);

	if (!rv) {
		/* ACMD41 successful */
		rv = sdcard_send_cmd(CMD_READ_OCR, try_sdhc);
		if (rv) {
			printf("sdcard: CMD58 error %02x\n", rv);
			sdcard_led_off();
			return sdc.status = STA_NOINIT;
		}
		/* Shift in the second data byte */
		writeb(0xff, IO_SDC_WR_B(SDC_START|SDC_BYTE));
		sdc.ocr = readl(IO_SDC_RD_L(SDC_START));
	} else {
		/* ACMD41 unsupported, try CMD1 */
		do {
			rv = sdcard_send_cmd(CMD_SEND_OP_COND, 0);
			if (rv & ~0x01) {
				printf("sdcard: CMD1 error %02x\n", rv);
				sdcard_led_off();
				return sdc.status = STA_NOINIT;
			}
		} while (rv);
	}

	/*
	 * Set block length -- some cards power up to a larger block size
	 * than 512 bytes even though that violates the spec.
	 */
	rv = sdcard_send_cmd(CMD_SET_BLOCKLEN, 512);
	if (rv) {
		printf("sdcard: CMD16 error %02x\n", rv);
		sdcard_led_off();
		return sdc.status = STA_NOINIT;
	}

	/*
	 * Read CID and CSD
	 */

	if (sdcard_read_reg(CMD_SEND_CSD, &sdc.csd))
		memset(&sdc.csd, 0, sizeof sdc.csd);

	printf("sdcard: CSD: %08x %08x %08x %08x\n",
	       sdc.csd.raw[0], sdc.csd.raw[1], sdc.csd.raw[2], sdc.csd.raw[3]);

	if (sdcard_read_reg(CMD_SEND_CID, &sdc.cid))
		memset(&sdc.cid, 0, sizeof sdc.cid);

	printf("sdcard: CID: %08x %08x %08x %08x\n",
	       sdc.cid.raw[0], sdc.cid.raw[1], sdc.cid.raw[2], sdc.cid.raw[3]);

	sdcard_compute_size(&sdc);

	printf("sdcard: %s card found, capacity %u sectors\n",
	       sdcard_type_name(sdc.card_type), sdc.lbasize);

	status = readw(IO_SDC_RD_STATUS(0));

	sdc.status = 0;
	if (!(status & 0x04))
		sdc.status |= STA_NOINIT;
	if (!(status & 0x08))
		sdc.status |= STA_PROTECT;

	sdcard_led_off();
	return sdc.status;
}

DSTATUS disk_status(BYTE drive)
{
	if (drive != 0)
		return STA_NOINIT;

	return sdc.status;
}