Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / sdcard / impl / sda_init.c
blob3e08c9e2cdae30844313fe5091b856139a3fdde8
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/sdcard/sda.h>
34 #include <sys/sdcard/sda_impl.h>
38 * Local Prototypes.
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,
54 uint8_t *);
55 static void sda_init_clock(sda_slot_t *, uint32_t);
58 * Implementation.
60 sda_err_t
61 sda_init_cmd(sda_slot_t *slot, sda_index_t cmd, uint32_t arg,
62 sda_rtype_t rtype, uint32_t *resp)
64 sda_cmd_t *cmdp;
65 sda_err_t errno;
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);
73 sda_cmd_free(cmdp);
75 return (errno);
78 sda_err_t
79 sda_init_acmd(sda_slot_t *slot, sda_index_t cmd, uint32_t arg,
80 sda_rtype_t rtype, uint32_t *resp)
82 sda_cmd_t *cmdp;
83 sda_err_t errno;
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);
91 sda_cmd_free(cmdp);
93 return (errno);
96 sda_err_t
97 sda_init_sdio(sda_slot_t *slot)
99 slot->s_num_io = 0;
102 * TODO: SDIO: We need to initialize the SDIO OCR register using
103 * the special CMD_IO_SEND_OCR (CMD5) command.
105 return (SDA_EOK);
108 sda_err_t
109 sda_init_sdmem(sda_slot_t *slot)
111 uint32_t ocr;
112 int count;
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
123 * card.
125 (void) sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
126 return (SDA_EOK);
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
137 * in the response.
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) {
145 ocr |= OCR_CCS;
148 /* make sure card is powered up */
149 for (count = 1000000; count != 0; count -= 10000) {
150 uint32_t r3;
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) &&
161 (r3 & OCR_CCS)) {
162 slot->s_flags |= SLOTF_SDHC;
163 } else {
164 slot->s_flags &= ~SLOTF_SDHC;
166 return (0);
169 drv_usecwait(10000);
172 sda_slot_err(slot, "SD card timed out during power up");
173 return (SDA_ETIME);
176 sda_err_t
177 sda_init_mmc(sda_slot_t *slot)
179 uint32_t ocr;
180 int count;
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) {
189 return (SDA_EOK);
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
199 * card.
201 (void) sda_init_cmd(slot, CMD_GO_IDLE, 0, R0, NULL);
202 return (SDA_EOK);
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) {
212 uint32_t r3;
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;
222 return (SDA_EOK);
225 drv_usecwait(10000);
228 sda_slot_err(slot, "MMC card timed out during power up");
229 return (SDA_ETIME);
232 sda_err_t
233 sda_init_card(sda_slot_t *slot)
235 int rv;
236 uint32_t resp;
237 uint32_t val;
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) {
248 return (rv);
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
255 * kHz and 400 kHz.
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) {
262 goto done;
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 */
270 goto done;
273 if ((slot->s_flags & (SLOTF_MEMORY | SLOTF_SDIO)) == 0) {
274 sda_slot_err(slot, "Unidentified card type");
275 rv = SDA_ENOTSUP;
276 goto done;
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);
286 if (rv != SDA_EOK) {
287 sda_slot_err(slot, "Failed getting card CID (%d)", rv);
288 goto done;
292 if ((rv = sda_init_rca(slot)) != SDA_EOK) {
293 goto done;
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,
311 slot->s_rcsd);
312 if (rv != 0) {
313 sda_slot_err(slot, "Failed getting card CSD (%d)", rv);
314 goto done;
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);
337 goto done;
340 if ((rv = sda_init_highspeed(slot)) != SDA_EOK) {
341 goto done;
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) {
350 goto done;
353 if ((rv = sda_init_blocklen(slot)) != SDA_EOK) {
354 goto done;
357 /* note if a card is writable */
358 if ((sda_getprop(slot, SDA_PROP_WPROTECT, &val) == SDA_EOK) &&
359 (val == 0)) {
360 slot->s_flags |= SLOTF_WRITABLE;
363 rv = SDA_EOK;
365 done:
367 sda_slot_enter(slot);
368 slot->s_init = B_FALSE;
369 sda_slot_exit(slot);
371 sda_slot_wakeup(slot);
373 return (rv);
376 sda_err_t
377 sda_init_blocklen(sda_slot_t *slot)
379 int rv;
380 uint32_t resp;
382 if ((slot->s_flags & SLOTF_MEMORY) == 0) {
383 return (SDA_EOK);
387 * All memory cards support block sizes of 512. Full stop.
389 rv = sda_init_cmd(slot, CMD_SET_BLOCKLEN, 512, R1, &resp);
390 if (rv != SDA_EOK) {
391 sda_slot_err(slot, "Unable to set block length (%d, %x)",
392 rv, resp);
394 return (rv);
397 void
398 sda_init_clock(sda_slot_t *slot, uint32_t hz)
400 int rv;
401 uint32_t act;
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
407 * initialization.
410 if ((rv = sda_setprop(slot, SDA_PROP_CLOCK, hz)) != SDA_EOK) {
411 sda_slot_err(slot, "Failed setting clock to %u Hz (%d)",
412 hz, rv);
413 /* XXX: FMA fail the slot */
414 return;
417 rv = sda_getprop(slot, SDA_PROP_CLOCK, &act);
418 sda_slot_debug(slot,
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));
429 sda_err_t
430 sda_init_width(sda_slot_t *slot)
432 int rv;
433 uint32_t resp;
436 * Spec says we should command the card first.
439 rv = sda_setprop(slot, SDA_PROP_BUSWIDTH, 1);
440 if (rv != SDA_EOK) {
441 sda_slot_err(slot, "Unable to set slot 1-bit mode (%d)", rv);
442 return (rv);
445 if ((slot->s_flags & SLOTF_4BITS) == 0) {
446 return (SDA_EOK);
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);
461 if (rv != SDA_EOK) {
462 sda_slot_err(slot,
463 "Unable disconnect DAT3 resistor on card (%d, %x)",
464 rv, resp);
465 /* non-fatal error, muddle on */
466 return (SDA_EOK);
469 rv = sda_init_acmd(slot, ACMD_SET_BUS_WIDTH, 2, R1, &resp);
470 if (rv != SDA_EOK) {
471 sda_slot_err(slot, "Unable to set card 4-bit mode (%d, %x)",
472 rv, resp);
473 /* non-fatal error, muddle on */
474 return (SDA_EOK);
477 rv = sda_setprop(slot, SDA_PROP_BUSWIDTH, 4);
478 if (rv != SDA_EOK) {
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);
487 return (rv);
490 sda_err_t
491 sda_init_ifcond(sda_slot_t *slot)
493 int rv;
494 int tries;
495 uint32_t vchk;
496 uint32_t resp;
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);
508 if (rv != SDA_EOK) {
509 sda_slot_err(slot, "Failed to IDLE card");
510 return (rv);
513 rv = sda_init_cmd(slot, CMD_SEND_IF_COND, vchk, R7, &resp);
514 if (rv == SDA_EOK) {
515 break;
517 delay(drv_usectohz(10000));
520 if (rv != SDA_EOK) {
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);
528 } else {
529 /* SDHC compliant */
530 slot->s_flags |= SLOTF_IFCOND;
533 return (SDA_EOK);
536 sda_err_t
537 sda_init_rca(sda_slot_t *slot)
539 int rv;
540 int tries;
541 uint32_t resp;
544 * Program the RCA. Note that MMC has a different mechanism
545 * for this.
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
553 * there.
555 rv = sda_init_cmd(slot, CMD_SEND_RCA,
556 (0x100 + tries) << 16, R1, NULL);
557 if (rv == SDA_EOK)
558 slot->s_rca = 0x100 + tries;
559 } else {
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
564 * to ask again.
566 rv = sda_init_cmd(slot, CMD_SEND_RCA, 0, R6, &resp);
567 if (rv == SDA_EOK)
568 slot->s_rca = resp >> 16;
570 if ((rv == SDA_EOK) && (slot->s_rca != 0)) {
571 sda_slot_debug(slot, "Relative address (RCA) = %d",
572 slot->s_rca);
573 return (SDA_EOK);
577 sda_slot_err(slot, "Unable to negotiate a suitable RCA (%d)", rv);
578 return ((rv != SDA_EOK) ? rv : SDA_EINVAL);
581 sda_err_t
582 sda_init_switch(sda_slot_t *slot, uint8_t mode, uint8_t grp, uint8_t val,
583 uint8_t *data)
585 sda_cmd_t *cmdp;
586 sda_err_t errno;
587 uint32_t arg;
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;
600 cmdp->sc_blksz = 64;
601 cmdp->sc_nblks = 1;
602 cmdp->sc_kvaddr = (void *)data;
604 errno = sda_cmd_exec(slot, cmdp, NULL);
606 sda_cmd_free(cmdp);
608 return (errno);
612 sda_err_t
613 sda_init_highspeed(sda_slot_t *slot)
615 uint32_t ccc;
616 uint8_t data[64];
617 sda_err_t rv;
619 if ((slot->s_caps & SLOT_CAP_HISPEED) == 0) {
620 return (SDA_EOK);
622 if ((slot->s_flags & SLOTF_SDMEM) == 0) {
623 return (SDA_EOK);
625 ccc = sda_mem_getbits(slot->s_rcsd, 95, 12);
626 if ((ccc & (1 << 10)) == 0) {
627 return (SDA_EOK);
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)) {
634 return (SDA_EOK);
637 rv = sda_init_switch(slot, 1, 0, 1, data);
638 if (rv != SDA_EOK) {
639 return (SDA_EOK);
642 /* now program the card */
643 rv = sda_setprop(slot, SDA_PROP_HISPEED, 1);
644 if (rv != SDA_EOK) {
645 sda_slot_err(slot, "Failed setting slot to high speed mode");
646 } else {
647 /* the card should now support 50 MHz */
648 slot->s_maxclk = 50000000;
651 return (rv);