4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * SD card initialization support.
30 #include <sys/types.h>
32 #include <sys/sunddi.h>
33 #include <sys/sdcard/sda.h>
34 #include <sys/sdcard/sda_impl.h>
41 static sda_err_t
sda_init_mmc(sda_slot_t
*);
42 static sda_err_t
sda_init_sdio(sda_slot_t
*);
43 static sda_err_t
sda_init_sdmem(sda_slot_t
*);
44 static sda_err_t
sda_init_cmd(sda_slot_t
*, sda_index_t
, uint32_t,
45 sda_rtype_t
, uint32_t *);
46 static sda_err_t
sda_init_acmd(sda_slot_t
*, sda_index_t
, uint32_t,
47 sda_rtype_t
, uint32_t *);
48 static sda_err_t
sda_init_blocklen(sda_slot_t
*);
49 static sda_err_t
sda_init_width(sda_slot_t
*);
50 static sda_err_t
sda_init_rca(sda_slot_t
*);
51 static sda_err_t
sda_init_ifcond(sda_slot_t
*);
52 static sda_err_t
sda_init_highspeed(sda_slot_t
*);
53 static sda_err_t
sda_init_switch(sda_slot_t
*, uint8_t, uint8_t, uint8_t,
55 static void sda_init_clock(sda_slot_t
*, uint32_t);
61 sda_init_cmd(sda_slot_t
*slot
, sda_index_t cmd
, uint32_t arg
,
62 sda_rtype_t rtype
, uint32_t *resp
)
67 cmdp
= sda_cmd_alloc(slot
, cmd
, arg
, rtype
, NULL
, KM_SLEEP
);
69 cmdp
->sc_flags
|= SDA_CMDF_INIT
;
71 errno
= sda_cmd_exec(slot
, cmdp
, resp
);
79 sda_init_acmd(sda_slot_t
*slot
, sda_index_t cmd
, uint32_t arg
,
80 sda_rtype_t rtype
, uint32_t *resp
)
85 cmdp
= sda_cmd_alloc_acmd(slot
, cmd
, arg
, rtype
, NULL
, KM_SLEEP
);
87 cmdp
->sc_flags
|= SDA_CMDF_INIT
;
89 errno
= sda_cmd_exec(slot
, cmdp
, resp
);
97 sda_init_sdio(sda_slot_t
*slot
)
102 * TODO: SDIO: We need to initialize the SDIO OCR register using
103 * the special CMD_IO_SEND_OCR (CMD5) command.
109 sda_init_sdmem(sda_slot_t
*slot
)
114 slot
->s_flags
&= ~SLOTF_SDMEM
;
117 * Try sending the ACMD41 to query the OCR (Op Cond Register).
119 if (sda_init_acmd(slot
, ACMD_SD_SEND_OCR
, 0, R3
, &ocr
) != SDA_EOK
) {
121 * Card failed to respond to query, not an SD card?
122 * We send GO_IDLE to clear any error status on the
125 (void) sda_init_cmd(slot
, CMD_GO_IDLE
, 0, R0
, NULL
);
130 * Now we have to send our OCR value, along with the HCS (High
131 * Capacity Support) bit. The HCS bit is required, to
132 * activate high capacity cards. We only set the HCS bit if
133 * the card responded to CMD8 (SEND_IFCOND), indicating that
134 * it supports the new protocol.
136 * Note that the HCS bit occupies the same location as the CCS bit
139 if ((ocr
& slot
->s_cur_ocr
) == 0) {
140 sda_slot_err(slot
, "SD card not compatible with host");
141 return (SDA_ENOTSUP
);
143 /* set the HCS bit if its a ver 2.00 card */
144 if (slot
->s_flags
& SLOTF_IFCOND
) {
148 /* make sure card is powered up */
149 for (count
= 1000000; count
!= 0; count
-= 10000) {
152 if (sda_init_acmd(slot
, ACMD_SD_SEND_OCR
, ocr
, R3
, &r3
) != 0) {
153 sda_slot_err(slot
, "SD card failed to power up");
154 return (SDA_ENOTSUP
);
157 /* Now check the busy bit */
158 if (r3
& OCR_POWER_UP
) {
159 slot
->s_flags
|= SLOTF_SDMEM
;
160 if ((slot
->s_flags
& SLOTF_IFCOND
) &&
162 slot
->s_flags
|= SLOTF_SDHC
;
164 slot
->s_flags
&= ~SLOTF_SDHC
;
172 sda_slot_err(slot
, "SD card timed out during power up");
177 sda_init_mmc(sda_slot_t
*slot
)
182 slot
->s_flags
&= ~SLOTF_MMC
;
185 * If the card has already been identified as an SD card, then
186 * cannot also be an MMC card, so don't probe it as such.
188 if (slot
->s_flags
& SLOTF_SD
) {
193 * Try sending the CMD1 to query the OCR.
195 if (sda_init_cmd(slot
, CMD_SEND_OCR
, 0, R3
, &ocr
) != 0) {
197 * Card failed to respond to query, not an MMC card?
198 * We send GO_IDLE to clear any error status on the
201 (void) sda_init_cmd(slot
, CMD_GO_IDLE
, 0, R0
, NULL
);
205 if ((ocr
& slot
->s_cur_ocr
) == 0) {
206 sda_slot_err(slot
, "MMC card not compatible with host");
207 return (SDA_ENOTSUP
);
210 /* make sure card is powered up */
211 for (count
= 1000000; count
!= 0; count
-= 10000) {
214 if (sda_init_cmd(slot
, CMD_SEND_OCR
, ocr
, R3
, &r3
) != 0) {
215 sda_slot_err(slot
, "MMC card failed to power up");
216 return (SDA_ENOTSUP
);
219 /* Now check the busy bit */
220 if (r3
& OCR_POWER_UP
) {
221 slot
->s_flags
|= SLOTF_MMC
;
228 sda_slot_err(slot
, "MMC card timed out during power up");
233 sda_init_card(sda_slot_t
*slot
)
240 * Power off slot/card initially.
242 sda_slot_power_off(slot
);
245 * Apply initial power to the slot.
247 if ((rv
= sda_slot_power_on(slot
)) != 0) {
252 * First enable the clock, but only at 400 kHz. All cards are
253 * supposed to be able to operate between this speed and 100
254 * kHz, and all hosts must be able to pick a speed between 100
257 * Once we know what the device can support, then we speed up.
259 sda_init_clock(slot
, 400000);
261 if ((rv
= sda_init_ifcond(slot
)) != SDA_EOK
) {
265 if (((rv
= sda_init_sdio(slot
)) != SDA_EOK
) ||
266 ((rv
= sda_init_sdmem(slot
)) != SDA_EOK
) ||
267 ((rv
= sda_init_mmc(slot
)) != SDA_EOK
)) {
269 /* message will already have been logged */
273 if ((slot
->s_flags
& (SLOTF_MEMORY
| SLOTF_SDIO
)) == 0) {
274 sda_slot_err(slot
, "Unidentified card type");
280 * Memory cards need to obtain their CID before getting their RCA.
281 * This is a requirement for the state transitions... they go thru
282 * the ident state, unlike SDIO cards.
284 if (slot
->s_flags
& SLOTF_MEMORY
) {
285 rv
= sda_init_cmd(slot
, CMD_BCAST_CID
, 0, R2
, slot
->s_rcid
);
287 sda_slot_err(slot
, "Failed getting card CID (%d)", rv
);
292 if ((rv
= sda_init_rca(slot
)) != SDA_EOK
) {
296 slot
->s_maxclk
= 0xffffffffU
; /* special sentinel */
299 * Figure out card supported bus width and speed.
301 * TODO: SDIO: For IO cards, we have to check what speed the card
302 * supports by looking in the CCCR_CAPAB register. (SDIO cards
303 * can go low-speed only, full-speed, or high-speed.)
305 if (slot
->s_flags
& SLOTF_MEMORY
) {
308 * We need to obtain the CSD.
310 rv
= sda_init_cmd(slot
, CMD_SEND_CSD
, slot
->s_rca
<< 16, R2
,
313 sda_slot_err(slot
, "Failed getting card CSD (%d)", rv
);
318 * Calculate the maxclock.
320 slot
->s_maxclk
= sda_mem_maxclk(slot
);
322 if (((slot
->s_flags
& SLOTF_SDMEM
) != 0) &&
323 ((slot
->s_caps
& SLOT_CAP_4BITS
) != 0)) {
324 slot
->s_flags
|= SLOTF_4BITS
;
326 if (slot
->s_flags
& SLOTF_SDIO
) {
327 sda_slot_debug(slot
, "Wide SDIO bus not yet supported");
328 slot
->s_flags
&= ~SLOTF_4BITS
;
332 * Now select the card.
334 if ((rv
= sda_init_cmd(slot
, CMD_SELECT_CARD
, slot
->s_rca
<< 16,
335 R1b
, &resp
)) != SDA_EOK
) {
336 sda_slot_err(slot
, "Failed selecting card (%d, %x)", rv
, resp
);
340 if ((rv
= sda_init_highspeed(slot
)) != SDA_EOK
) {
344 sda_init_clock(slot
, slot
->s_maxclk
);
347 * Lets go to 4-bit bus mode, if possible.
349 if ((rv
= sda_init_width(slot
)) != SDA_EOK
) {
353 if ((rv
= sda_init_blocklen(slot
)) != SDA_EOK
) {
357 /* note if a card is writable */
358 if ((sda_getprop(slot
, SDA_PROP_WPROTECT
, &val
) == SDA_EOK
) &&
360 slot
->s_flags
|= SLOTF_WRITABLE
;
367 sda_slot_enter(slot
);
368 slot
->s_init
= B_FALSE
;
371 sda_slot_wakeup(slot
);
377 sda_init_blocklen(sda_slot_t
*slot
)
382 if ((slot
->s_flags
& SLOTF_MEMORY
) == 0) {
387 * All memory cards support block sizes of 512. Full stop.
389 rv
= sda_init_cmd(slot
, CMD_SET_BLOCKLEN
, 512, R1
, &resp
);
391 sda_slot_err(slot
, "Unable to set block length (%d, %x)",
398 sda_init_clock(sda_slot_t
*slot
, uint32_t hz
)
404 * Note that at no time is a failure programming the clock
405 * itself necessarily a fatal error. Although if the clock
406 * wasn't programmed, other things will probably not work during
410 if ((rv
= sda_setprop(slot
, SDA_PROP_CLOCK
, hz
)) != SDA_EOK
) {
411 sda_slot_err(slot
, "Failed setting clock to %u Hz (%d)",
413 /* XXX: FMA fail the slot */
417 rv
= sda_getprop(slot
, SDA_PROP_CLOCK
, &act
);
419 rv
== SDA_EOK
? "Clock set to %u Hz (requested %u Hz)" :
420 "Clock frequency unknown (good luck).", act
, hz
);
423 * For now, just wait 10msec for clocks to stabilize to the
424 * card. (Is this really necessary?)
426 delay(drv_usectohz(10000));
430 sda_init_width(sda_slot_t
*slot
)
436 * Spec says we should command the card first.
439 rv
= sda_setprop(slot
, SDA_PROP_BUSWIDTH
, 1);
441 sda_slot_err(slot
, "Unable to set slot 1-bit mode (%d)", rv
);
445 if ((slot
->s_flags
& SLOTF_4BITS
) == 0) {
450 * TODO: SDIO: SDIO cards set the CCCR_BUS_WIDTH
451 * and CCCR_CD_DISABLE bits here.
455 * If we're going to use all 4 pins, we really need to disconnect
456 * the card pullup resistor. A consquence of this, is that hosts
457 * which use that resistor for detection must not claim to support
458 * 4-bit bus mode. This is a limitation of our implementation.
460 rv
= sda_init_acmd(slot
, ACMD_SET_CLR_CARD_DETECT
, 1, R1
, &resp
);
463 "Unable disconnect DAT3 resistor on card (%d, %x)",
465 /* non-fatal error, muddle on */
469 rv
= sda_init_acmd(slot
, ACMD_SET_BUS_WIDTH
, 2, R1
, &resp
);
471 sda_slot_err(slot
, "Unable to set card 4-bit mode (%d, %x)",
473 /* non-fatal error, muddle on */
477 rv
= sda_setprop(slot
, SDA_PROP_BUSWIDTH
, 4);
480 * This is bad news. We've already asked for the card to
481 * to use 4-bit mode, but the host is not complying. It
482 * shouldn't ever happen, so we just error out.
484 sda_slot_err(slot
, "Unable to set slot 4-bit mode (%d)", rv
);
491 sda_init_ifcond(sda_slot_t
*slot
)
499 * Try SEND_IF_COND. Note that this assumes that the host is
500 * supplying 2.7 - 3.6 voltage range. The standard is not
501 * defined for any other ranges.
503 vchk
= R7_VHS_27_36V
| R7_PATTERN
;
505 /* we try this a few times, just to be sure */
506 for (tries
= 0; tries
< 5; tries
++) {
507 rv
= sda_init_cmd(slot
, CMD_GO_IDLE
, 0, R0
, NULL
);
509 sda_slot_err(slot
, "Failed to IDLE card");
513 rv
= sda_init_cmd(slot
, CMD_SEND_IF_COND
, vchk
, R7
, &resp
);
517 delay(drv_usectohz(10000));
521 (void) sda_init_cmd(slot
, CMD_GO_IDLE
, 0, R0
, NULL
);
522 slot
->s_flags
&= ~SLOTF_IFCOND
;
524 } else if (resp
!= vchk
) {
525 sda_slot_err(slot
, "Card voltages incompatible! (%x)", resp
);
526 return (SDA_ENOTSUP
);
530 slot
->s_flags
|= SLOTF_IFCOND
;
537 sda_init_rca(sda_slot_t
*slot
)
544 * Program the RCA. Note that MMC has a different mechanism
547 for (tries
= 0; tries
< 10; tries
++) {
549 if (slot
->s_flags
& SLOTF_MMC
) {
551 * For MMC, we push the RCA to the MMC. We
552 * arbitrarily start at 0x100, and add from
555 rv
= sda_init_cmd(slot
, CMD_SEND_RCA
,
556 (0x100 + tries
) << 16, R1
, NULL
);
558 slot
->s_rca
= 0x100 + tries
;
561 * For SDcard, we are basically asking the
562 * card to propose a value. It *may* propose
563 * a value of zero, in which case we will have
566 rv
= sda_init_cmd(slot
, CMD_SEND_RCA
, 0, R6
, &resp
);
568 slot
->s_rca
= resp
>> 16;
570 if ((rv
== SDA_EOK
) && (slot
->s_rca
!= 0)) {
571 sda_slot_debug(slot
, "Relative address (RCA) = %d",
577 sda_slot_err(slot
, "Unable to negotiate a suitable RCA (%d)", rv
);
578 return ((rv
!= SDA_EOK
) ? rv
: SDA_EINVAL
);
582 sda_init_switch(sda_slot_t
*slot
, uint8_t mode
, uint8_t grp
, uint8_t val
,
590 * The spec says we should leave unselected groups set to 0xf,
591 * to prevent inadvertent changes.
593 arg
= (mode
<< 31) | 0xffffff;
594 arg
&= ~(0xf << (grp
<< 2));
595 arg
|= (val
<< (grp
<< 2));
597 cmdp
= sda_cmd_alloc(slot
, CMD_SWITCH_FUNC
, arg
, R1
, NULL
, KM_SLEEP
);
599 cmdp
->sc_flags
|= SDA_CMDF_INIT
| SDA_CMDF_DAT
| SDA_CMDF_READ
;
602 cmdp
->sc_kvaddr
= (void *)data
;
604 errno
= sda_cmd_exec(slot
, cmdp
, NULL
);
613 sda_init_highspeed(sda_slot_t
*slot
)
619 if ((slot
->s_caps
& SLOT_CAP_HISPEED
) == 0) {
622 if ((slot
->s_flags
& SLOTF_SDMEM
) == 0) {
625 ccc
= sda_mem_getbits(slot
->s_rcsd
, 95, 12);
626 if ((ccc
& (1 << 10)) == 0) {
630 rv
= sda_init_switch(slot
, 0, 0, 1, data
);
632 /* these are big-endian bits, bit 401 */
633 if ((rv
!= SDA_EOK
) || ((data
[13] & (1 << 1)) == 0)) {
637 rv
= sda_init_switch(slot
, 1, 0, 1, data
);
642 /* now program the card */
643 rv
= sda_setprop(slot
, SDA_PROP_HISPEED
, 1);
645 sda_slot_err(slot
, "Failed setting slot to high speed mode");
647 /* the card should now support 50 MHz */
648 slot
->s_maxclk
= 50000000;