summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/sdcard/impl/sda_init.c
blob: 3e08c9e2cdae30844313fe5091b856139a3fdde8 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * SD card initialization support.
 */

#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sdcard/sda.h>
#include <sys/sdcard/sda_impl.h>


/*
 * Local Prototypes.
 */

static sda_err_t sda_init_mmc(sda_slot_t *);
static sda_err_t sda_init_sdio(sda_slot_t *);
static sda_err_t sda_init_sdmem(sda_slot_t *);
static sda_err_t sda_init_cmd(sda_slot_t *, sda_index_t, uint32_t,
    sda_rtype_t, uint32_t *);
static sda_err_t sda_init_acmd(sda_slot_t *, sda_index_t, uint32_t,
    sda_rtype_t, uint32_t *);
static sda_err_t sda_init_blocklen(sda_slot_t *);
static sda_err_t sda_init_width(sda_slot_t *);
static sda_err_t sda_init_rca(sda_slot_t *);
static sda_err_t sda_init_ifcond(sda_slot_t *);
static sda_err_t sda_init_highspeed(sda_slot_t *);
static sda_err_t sda_init_switch(sda_slot_t *, uint8_t, uint8_t, uint8_t,
    uint8_t *);
static void sda_init_clock(sda_slot_t *, uint32_t);

/*
 * Implementation.
 */
sda_err_t
sda_init_cmd(sda_slot_t *slot, sda_index_t cmd, uint32_t arg,
    sda_rtype_t rtype, uint32_t *resp)
{
	sda_cmd_t	*cmdp;
	sda_err_t	errno;

	cmdp = sda_cmd_alloc(slot, cmd, arg, rtype, NULL, KM_SLEEP);

	cmdp->sc_flags |= SDA_CMDF_INIT;

	errno = sda_cmd_exec(slot, cmdp, resp);

	sda_cmd_free(cmdp);

	return (errno);
}

sda_err_t
sda_init_acmd(sda_slot_t *slot, sda_index_t cmd, uint32_t arg,
    sda_rtype_t rtype, uint32_t *resp)
{
	sda_cmd_t	*cmdp;
	sda_err_t	errno;

	cmdp = sda_cmd_alloc_acmd(slot, cmd, arg, rtype, NULL, KM_SLEEP);

	cmdp->sc_flags |= SDA_CMDF_INIT;

	errno = sda_cmd_exec(slot, cmdp, resp);

	sda_cmd_free(cmdp);

	return (errno);
}

sda_err_t
sda_init_sdio(sda_slot_t *slot)
{
	slot->s_num_io = 0;

	/*
	 * TODO: SDIO: We need to initialize the SDIO OCR register using
	 * the special CMD_IO_SEND_OCR (CMD5) command.
	 */
	return (SDA_EOK);
}

sda_err_t
sda_init_sdmem(sda_slot_t *slot)
{
	uint32_t	ocr;
	int		count;

	slot->s_flags &= ~SLOTF_SDMEM;

	/*
	 * Try sending the ACMD41 to query the OCR (Op Cond Register).
	 */
	if (sda_init_acmd(slot, ACMD_SD_SEND_OCR, 0, R3, &ocr) != SDA_EOK) {
		/*
		 * Card failed to respond to query, not an SD card?
		 * We send GO_IDLE to clear any error status on the
		 * card.
		 */
		(void) sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
		return (SDA_EOK);
	}

	/*
	 * Now we have to send our OCR value, along with the HCS (High
	 * Capacity Support) bit.  The HCS bit is required, to
	 * activate high capacity cards.  We only set the HCS bit if
	 * the card responded to CMD8 (SEND_IFCOND), indicating that
	 * it supports the new protocol.
	 *
	 * Note that the HCS bit occupies the same location as the CCS bit
	 * in the response.
	 */
	if ((ocr & slot->s_cur_ocr) == 0) {
		sda_slot_err(slot, "SD card not compatible with host");
		return (SDA_ENOTSUP);
	}
	/* set the HCS bit if its a ver 2.00 card */
	if (slot->s_flags & SLOTF_IFCOND) {
		ocr |= OCR_CCS;
	}

	/* make sure card is powered up */
	for (count = 1000000; count != 0; count -= 10000) {
		uint32_t	r3;

		if (sda_init_acmd(slot, ACMD_SD_SEND_OCR, ocr, R3, &r3) != 0) {
			sda_slot_err(slot, "SD card failed to power up");
			return (SDA_ENOTSUP);
		}

		/* Now check the busy bit */
		if (r3 & OCR_POWER_UP) {
			slot->s_flags |= SLOTF_SDMEM;
			if ((slot->s_flags & SLOTF_IFCOND) &&
			    (r3 & OCR_CCS)) {
				slot->s_flags |= SLOTF_SDHC;
			} else {
				slot->s_flags &= ~SLOTF_SDHC;
			}
			return (0);
		}

		drv_usecwait(10000);
	}

	sda_slot_err(slot, "SD card timed out during power up");
	return (SDA_ETIME);
}

sda_err_t
sda_init_mmc(sda_slot_t *slot)
{
	uint32_t	ocr;
	int		count;

	slot->s_flags &= ~SLOTF_MMC;

	/*
	 * If the card has already been identified as an SD card, then
	 * cannot also be an MMC card, so don't probe it as such.
	 */
	if (slot->s_flags & SLOTF_SD) {
		return (SDA_EOK);
	}

	/*
	 * Try sending the CMD1 to query the OCR.
	 */
	if (sda_init_cmd(slot, CMD_SEND_OCR, 0, R3, &ocr) != 0) {
		/*
		 * Card failed to respond to query, not an MMC card?
		 * We send GO_IDLE to clear any error status on the
		 * card.
		 */
		(void) sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
		return (SDA_EOK);
	}

	if ((ocr & slot->s_cur_ocr) == 0) {
		sda_slot_err(slot, "MMC card not compatible with host");
		return (SDA_ENOTSUP);
	}

	/* make sure card is powered up */
	for (count = 1000000; count != 0; count -= 10000) {
		uint32_t	r3;

		if (sda_init_cmd(slot, CMD_SEND_OCR, ocr, R3, &r3) != 0) {
			sda_slot_err(slot, "MMC card failed to power up");
			return (SDA_ENOTSUP);
		}

		/* Now check the busy bit */
		if (r3 & OCR_POWER_UP) {
			slot->s_flags |= SLOTF_MMC;
			return (SDA_EOK);
		}

		drv_usecwait(10000);
	}

	sda_slot_err(slot, "MMC card timed out during power up");
	return (SDA_ETIME);
}

sda_err_t
sda_init_card(sda_slot_t *slot)
{
	int		rv;
	uint32_t	resp;
	uint32_t	val;

	/*
	 * Power off slot/card initially.
	 */
	sda_slot_power_off(slot);

	/*
	 * Apply initial power to the slot.
	 */
	if ((rv = sda_slot_power_on(slot)) != 0) {
		return (rv);
	}

	/*
	 * First enable the clock, but only at 400 kHz.  All cards are
	 * supposed to be able to operate between this speed and 100
	 * kHz, and all hosts must be able to pick a speed between 100
	 * kHz and 400 kHz.
	 *
	 * Once we know what the device can support, then we speed up.
	 */
	sda_init_clock(slot, 400000);

	if ((rv = sda_init_ifcond(slot)) != SDA_EOK) {
		goto done;
	}

	if (((rv = sda_init_sdio(slot)) != SDA_EOK) ||
	    ((rv = sda_init_sdmem(slot)) != SDA_EOK) ||
	    ((rv = sda_init_mmc(slot)) != SDA_EOK)) {

		/* message will already have been logged */
		goto done;
	}

	if ((slot->s_flags & (SLOTF_MEMORY | SLOTF_SDIO)) == 0) {
		sda_slot_err(slot, "Unidentified card type");
		rv = SDA_ENOTSUP;
		goto done;
	}

	/*
	 * Memory cards need to obtain their CID before getting their RCA.
	 * This is a requirement for the state transitions... they go thru
	 * the ident state, unlike SDIO cards.
	 */
	if (slot->s_flags & SLOTF_MEMORY) {
		rv = sda_init_cmd(slot, CMD_BCAST_CID, 0, R2, slot->s_rcid);
		if (rv != SDA_EOK) {
			sda_slot_err(slot, "Failed getting card CID (%d)", rv);
			goto done;
		}
	}

	if ((rv = sda_init_rca(slot)) != SDA_EOK) {
		goto done;
	}

	slot->s_maxclk = 0xffffffffU;	/* special sentinel */

	/*
	 * Figure out card supported bus width and speed.
	 *
	 * TODO: SDIO: For IO cards, we have to check what speed the card
	 * supports by looking in the CCCR_CAPAB register.  (SDIO cards
	 * can go low-speed only, full-speed, or high-speed.)
	 */
	if (slot->s_flags & SLOTF_MEMORY) {

		/*
		 * We need to obtain the CSD.
		 */
		rv = sda_init_cmd(slot, CMD_SEND_CSD, slot->s_rca << 16, R2,
		    slot->s_rcsd);
		if (rv != 0) {
			sda_slot_err(slot, "Failed getting card CSD (%d)", rv);
			goto done;
		}

		/*
		 * Calculate the maxclock.
		 */
		slot->s_maxclk = sda_mem_maxclk(slot);
	}
	if (((slot->s_flags & SLOTF_SDMEM) != 0) &&
	    ((slot->s_caps & SLOT_CAP_4BITS) != 0)) {
		slot->s_flags |= SLOTF_4BITS;
	}
	if (slot->s_flags & SLOTF_SDIO) {
		sda_slot_debug(slot, "Wide SDIO bus not yet supported");
		slot->s_flags &= ~SLOTF_4BITS;
	}

	/*
	 * Now select the card.
	 */
	if ((rv = sda_init_cmd(slot, CMD_SELECT_CARD, slot->s_rca << 16,
	    R1b, &resp)) != SDA_EOK) {
		sda_slot_err(slot, "Failed selecting card (%d, %x)", rv, resp);
		goto done;
	}

	if ((rv = sda_init_highspeed(slot)) != SDA_EOK) {
		goto done;
	}

	sda_init_clock(slot, slot->s_maxclk);

	/*
	 * Lets go to 4-bit bus mode, if possible.
	 */
	if ((rv = sda_init_width(slot)) != SDA_EOK) {
		goto done;
	}

	if ((rv = sda_init_blocklen(slot)) != SDA_EOK) {
		goto done;
	}

	/* note if a card is writable */
	if ((sda_getprop(slot, SDA_PROP_WPROTECT, &val) == SDA_EOK) &&
	    (val == 0)) {
		slot->s_flags |= SLOTF_WRITABLE;
	}

	rv = SDA_EOK;

done:

	sda_slot_enter(slot);
	slot->s_init = B_FALSE;
	sda_slot_exit(slot);

	sda_slot_wakeup(slot);

	return (rv);
}

sda_err_t
sda_init_blocklen(sda_slot_t *slot)
{
	int		rv;
	uint32_t	resp;

	if ((slot->s_flags & SLOTF_MEMORY) == 0)  {
		return (SDA_EOK);
	}

	/*
	 * All memory cards support block sizes of 512.  Full stop.
	 */
	rv = sda_init_cmd(slot, CMD_SET_BLOCKLEN, 512, R1, &resp);
	if (rv != SDA_EOK) {
		sda_slot_err(slot, "Unable to set block length (%d, %x)",
		    rv, resp);
	}
	return (rv);
}

void
sda_init_clock(sda_slot_t *slot, uint32_t hz)
{
	int		rv;
	uint32_t	act;

	/*
	 * Note that at no time is a failure programming the clock
	 * itself necessarily a fatal error.  Although if the clock
	 * wasn't programmed, other things will probably not work during
	 * initialization.
	 */

	if ((rv = sda_setprop(slot, SDA_PROP_CLOCK, hz)) != SDA_EOK) {
		sda_slot_err(slot, "Failed setting clock to %u Hz (%d)",
		    hz, rv);
		/* XXX: FMA fail the slot */
		return;
	}

	rv = sda_getprop(slot, SDA_PROP_CLOCK, &act);
	sda_slot_debug(slot,
	    rv == SDA_EOK ? "Clock set to %u Hz (requested %u Hz)" :
	    "Clock frequency unknown (good luck).", act, hz);

	/*
	 * For now, just wait 10msec for clocks to stabilize to the
	 * card.  (Is this really necessary?)
	 */
	delay(drv_usectohz(10000));
}

sda_err_t
sda_init_width(sda_slot_t *slot)
{
	int		rv;
	uint32_t	resp;

	/*
	 * Spec says we should command the card first.
	 */

	rv = sda_setprop(slot, SDA_PROP_BUSWIDTH, 1);
	if (rv != SDA_EOK) {
		sda_slot_err(slot, "Unable to set slot 1-bit mode (%d)", rv);
		return (rv);
	}

	if ((slot->s_flags & SLOTF_4BITS) == 0) {
		return (SDA_EOK);
	}

	/*
	 * TODO: SDIO: SDIO cards set the CCCR_BUS_WIDTH
	 * and CCCR_CD_DISABLE bits here.
	 */

	/*
	 * If we're going to use all 4 pins, we really need to disconnect
	 * the card pullup resistor.   A consquence of this, is that hosts
	 * which use that resistor for detection must not claim to support
	 * 4-bit bus mode.  This is a limitation of our implementation.
	 */
	rv = sda_init_acmd(slot, ACMD_SET_CLR_CARD_DETECT, 1, R1, &resp);
	if (rv != SDA_EOK) {
		sda_slot_err(slot,
		    "Unable disconnect DAT3 resistor on card (%d, %x)",
		    rv, resp);
		/* non-fatal error, muddle on */
		return (SDA_EOK);
	}

	rv = sda_init_acmd(slot, ACMD_SET_BUS_WIDTH, 2, R1, &resp);
	if (rv != SDA_EOK) {
		sda_slot_err(slot, "Unable to set card 4-bit mode (%d, %x)",
		    rv, resp);
		/* non-fatal error, muddle on */
		return (SDA_EOK);
	}

	rv = sda_setprop(slot, SDA_PROP_BUSWIDTH, 4);
	if (rv != SDA_EOK) {
		/*
		 * This is bad news.  We've already asked for the card to
		 * to use 4-bit mode, but the host is not complying.  It
		 * shouldn't ever happen, so we just error out.
		 */
		sda_slot_err(slot, "Unable to set slot 4-bit mode (%d)", rv);
	}

	return (rv);
}

sda_err_t
sda_init_ifcond(sda_slot_t *slot)
{
	int		rv;
	int		tries;
	uint32_t	vchk;
	uint32_t	resp;

	/*
	 * Try SEND_IF_COND.  Note that this assumes that the host is
	 * supplying 2.7 - 3.6 voltage range.  The standard is not
	 * defined for any other ranges.
	 */
	vchk = R7_VHS_27_36V | R7_PATTERN;

	/* we try this a few times, just to be sure */
	for (tries = 0; tries < 5; tries++) {
		rv = sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
		if (rv != SDA_EOK) {
			sda_slot_err(slot, "Failed to IDLE card");
			return (rv);
		}

		rv = sda_init_cmd(slot, CMD_SEND_IF_COND, vchk, R7, &resp);
		if (rv == SDA_EOK) {
			break;
		}
		delay(drv_usectohz(10000));
	}

	if (rv != SDA_EOK) {
		(void) sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
		slot->s_flags &= ~SLOTF_IFCOND;

	} else if (resp != vchk) {
		sda_slot_err(slot, "Card voltages incompatible! (%x)", resp);
		return (SDA_ENOTSUP);

	} else {
		/* SDHC compliant */
		slot->s_flags |= SLOTF_IFCOND;
	}

	return (SDA_EOK);
}

sda_err_t
sda_init_rca(sda_slot_t *slot)
{
	int		rv;
	int		tries;
	uint32_t	resp;

	/*
	 * Program the RCA.  Note that MMC has a different mechanism
	 * for this.
	 */
	for (tries = 0; tries < 10; tries++) {

		if (slot->s_flags & SLOTF_MMC) {
			/*
			 * For MMC, we push the RCA to the MMC.  We
			 * arbitrarily start at 0x100, and add from
			 * there.
			 */
			rv = sda_init_cmd(slot, CMD_SEND_RCA,
			    (0x100 + tries) << 16, R1, NULL);
			if (rv == SDA_EOK)
				slot->s_rca = 0x100 + tries;
		} else {
			/*
			 * For SDcard, we are basically asking the
			 * card to propose a value.  It *may* propose
			 * a value of zero, in which case we will have
			 * to ask again.
			 */
			rv = sda_init_cmd(slot, CMD_SEND_RCA, 0, R6, &resp);
			if (rv == SDA_EOK)
				slot->s_rca = resp >> 16;
		}
		if ((rv == SDA_EOK) && (slot->s_rca != 0)) {
			sda_slot_debug(slot, "Relative address (RCA) = %d",
			    slot->s_rca);
			return (SDA_EOK);
		}
	}

	sda_slot_err(slot, "Unable to negotiate a suitable RCA (%d)", rv);
	return ((rv != SDA_EOK) ? rv : SDA_EINVAL);
}

sda_err_t
sda_init_switch(sda_slot_t *slot, uint8_t mode, uint8_t grp, uint8_t val,
    uint8_t *data)
{
	sda_cmd_t	*cmdp;
	sda_err_t	errno;
	uint32_t	arg;

	/*
	 * The spec says we should leave unselected groups set to 0xf,
	 * to prevent inadvertent changes.
	 */
	arg = (mode << 31) | 0xffffff;
	arg &= ~(0xf << (grp << 2));
	arg |= (val << (grp << 2));

	cmdp = sda_cmd_alloc(slot, CMD_SWITCH_FUNC, arg, R1, NULL, KM_SLEEP);

	cmdp->sc_flags |= SDA_CMDF_INIT | SDA_CMDF_DAT | SDA_CMDF_READ;
	cmdp->sc_blksz = 64;
	cmdp->sc_nblks = 1;
	cmdp->sc_kvaddr = (void *)data;

	errno = sda_cmd_exec(slot, cmdp, NULL);

	sda_cmd_free(cmdp);

	return (errno);

}

sda_err_t
sda_init_highspeed(sda_slot_t *slot)
{
	uint32_t	ccc;
	uint8_t		data[64];
	sda_err_t	rv;

	if ((slot->s_caps & SLOT_CAP_HISPEED) == 0) {
		return (SDA_EOK);
	}
	if ((slot->s_flags & SLOTF_SDMEM) == 0) {
		return (SDA_EOK);
	}
	ccc = sda_mem_getbits(slot->s_rcsd, 95, 12);
	if ((ccc & (1 << 10)) == 0) {
		return (SDA_EOK);
	}

	rv = sda_init_switch(slot, 0, 0, 1, data);

	/* these are big-endian bits, bit 401 */
	if ((rv != SDA_EOK) || ((data[13] & (1 << 1)) == 0)) {
		return (SDA_EOK);
	}

	rv = sda_init_switch(slot, 1, 0, 1, data);
	if (rv != SDA_EOK) {
		return (SDA_EOK);
	}

	/* now program the card */
	rv = sda_setprop(slot, SDA_PROP_HISPEED, 1);
	if (rv != SDA_EOK) {
		sda_slot_err(slot, "Failed setting slot to high speed mode");
	} else {
		/* the card should now support 50 MHz */
		slot->s_maxclk = 50000000;
	}

	return (rv);
}