2 /* $OpenBSD: sdmmc.c,v 1.18 2009/01/09 10:58:38 jsg Exp $ */
5 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 2007-2009 NONAKA Kimihiro <nonaka@netbsd.org>
22 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * Host controller independent SD/MMC bus driver based on information
48 * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO
49 * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver.
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD$");
55 #include <sys/param.h>
56 #include <sys/device.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/systm.h>
63 #include <dev/sdmmc/sdmmc_ioreg.h>
64 #include <dev/sdmmc/sdmmcchip.h>
65 #include <dev/sdmmc/sdmmcreg.h>
66 #include <dev/sdmmc/sdmmcvar.h>
70 static void sdmmc_dump_command(struct sdmmc_softc
*, struct sdmmc_command
*);
71 #define DPRINTF(n,s) do { if ((n) <= sdmmcdebug) printf s; } while (0)
73 #define DPRINTF(n,s) do {} while (0)
76 #define DEVNAME(sc) SDMMCDEVNAME(sc)
78 static int sdmmc_match(device_t
, cfdata_t
, void *);
79 static void sdmmc_attach(device_t
, device_t
, void *);
80 static int sdmmc_detach(device_t
, int);
82 CFATTACH_DECL_NEW(sdmmc
, sizeof(struct sdmmc_softc
),
83 sdmmc_match
, sdmmc_attach
, sdmmc_detach
, NULL
);
85 static void sdmmc_doattach(device_t
);
86 static void sdmmc_task_thread(void *);
87 static void sdmmc_discover_task(void *);
88 static void sdmmc_card_attach(struct sdmmc_softc
*);
89 static void sdmmc_card_detach(struct sdmmc_softc
*, int);
90 static int sdmmc_print(void *, const char *);
91 static int sdmmc_enable(struct sdmmc_softc
*);
92 static void sdmmc_disable(struct sdmmc_softc
*);
93 static int sdmmc_scan(struct sdmmc_softc
*);
94 static int sdmmc_init(struct sdmmc_softc
*);
97 sdmmc_match(device_t parent
, cfdata_t cf
, void *aux
)
99 struct sdmmcbus_attach_args
*saa
= (struct sdmmcbus_attach_args
*)aux
;
101 if (strcmp(saa
->saa_busname
, cf
->cf_name
) == 0)
107 sdmmc_attach(device_t parent
, device_t self
, void *aux
)
109 struct sdmmc_softc
*sc
= device_private(self
);
110 struct sdmmcbus_attach_args
*saa
= (struct sdmmcbus_attach_args
*)aux
;
117 sc
->sc_sct
= saa
->saa_sct
;
118 sc
->sc_sch
= saa
->saa_sch
;
119 sc
->sc_dmat
= saa
->saa_dmat
;
120 sc
->sc_clkmin
= saa
->saa_clkmin
;
121 sc
->sc_clkmax
= saa
->saa_clkmax
;
122 sc
->sc_busclk
= sc
->sc_clkmax
;
124 sc
->sc_caps
= saa
->saa_caps
;
126 if (ISSET(sc
->sc_caps
, SMC_CAPS_DMA
)) {
127 error
= bus_dmamap_create(sc
->sc_dmat
, MAXPHYS
, SDMMC_MAXNSEGS
,
128 MAXPHYS
, 0, BUS_DMA_NOWAIT
|BUS_DMA_ALLOCNOW
, &sc
->sc_dmap
);
130 aprint_error_dev(sc
->sc_dev
,
131 "couldn't create dma map. (error=%d)\n", error
);
136 SIMPLEQ_INIT(&sc
->sf_head
);
137 TAILQ_INIT(&sc
->sc_tskq
);
138 TAILQ_INIT(&sc
->sc_intrq
);
140 sdmmc_init_task(&sc
->sc_discover_task
, sdmmc_discover_task
, sc
);
141 sdmmc_init_task(&sc
->sc_intr_task
, sdmmc_intr_task
, sc
);
143 mutex_init(&sc
->sc_mtx
, MUTEX_DEFAULT
, IPL_SDMMC
);
144 mutex_init(&sc
->sc_tskq_mtx
, MUTEX_DEFAULT
, IPL_SDMMC
);
145 mutex_init(&sc
->sc_discover_task_mtx
, MUTEX_DEFAULT
, IPL_SDMMC
);
146 mutex_init(&sc
->sc_intr_task_mtx
, MUTEX_DEFAULT
, IPL_SDMMC
);
147 cv_init(&sc
->sc_tskq_cv
, "mmctaskq");
149 if (!pmf_device_register(self
, NULL
, NULL
)) {
150 aprint_error_dev(self
, "couldn't establish power handler\n");
153 SET(sc
->sc_flags
, SMF_INITED
);
156 * Create the event thread that will attach and detach cards
157 * and perform other lengthy operations.
159 config_pending_incr();
160 config_interrupts(self
, sdmmc_doattach
);
164 sdmmc_detach(device_t self
, int flags
)
166 struct sdmmc_softc
*sc
= device_private(self
);
169 mutex_enter(&sc
->sc_tskq_mtx
);
171 cv_signal(&sc
->sc_tskq_cv
);
172 while (sc
->sc_tskq_lwp
!= NULL
)
173 cv_wait(&sc
->sc_tskq_cv
, &sc
->sc_tskq_mtx
);
174 mutex_exit(&sc
->sc_tskq_mtx
);
176 pmf_device_deregister(self
);
178 error
= config_detach_children(self
, flags
);
185 sdmmc_doattach(device_t dev
)
187 struct sdmmc_softc
*sc
= device_private(dev
);
189 if (kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
,
190 sdmmc_task_thread
, sc
, &sc
->sc_tskq_lwp
, "%s", device_xname(dev
))) {
191 aprint_error_dev(dev
, "couldn't create task thread\n");
196 sdmmc_add_task(struct sdmmc_softc
*sc
, struct sdmmc_task
*task
)
199 mutex_enter(&sc
->sc_tskq_mtx
);
202 TAILQ_INSERT_TAIL(&sc
->sc_tskq
, task
, next
);
203 cv_broadcast(&sc
->sc_tskq_cv
);
204 mutex_exit(&sc
->sc_tskq_mtx
);
208 sdmmc_del_task1(struct sdmmc_softc
*sc
, struct sdmmc_task
*task
)
211 TAILQ_REMOVE(&sc
->sc_tskq
, task
, next
);
217 sdmmc_del_task(struct sdmmc_task
*task
)
219 struct sdmmc_softc
*sc
= (struct sdmmc_softc
*)task
->sc
;
222 mutex_enter(&sc
->sc_tskq_mtx
);
223 sdmmc_del_task1(sc
, task
);
224 mutex_exit(&sc
->sc_tskq_mtx
);
229 sdmmc_task_thread(void *arg
)
231 struct sdmmc_softc
*sc
= (struct sdmmc_softc
*)arg
;
232 struct sdmmc_task
*task
;
234 sdmmc_discover_task(sc
);
235 config_pending_decr();
237 mutex_enter(&sc
->sc_tskq_mtx
);
239 task
= TAILQ_FIRST(&sc
->sc_tskq
);
241 sdmmc_del_task1(sc
, task
);
242 mutex_exit(&sc
->sc_tskq_mtx
);
243 (*task
->func
)(task
->arg
);
244 mutex_enter(&sc
->sc_tskq_mtx
);
246 /* Check for the exit condition. */
249 cv_wait(&sc
->sc_tskq_cv
, &sc
->sc_tskq_mtx
);
254 if (ISSET(sc
->sc_flags
, SMF_CARD_PRESENT
))
255 sdmmc_card_detach(sc
, DETACH_FORCE
);
256 sc
->sc_tskq_lwp
= NULL
;
257 cv_broadcast(&sc
->sc_tskq_cv
);
258 mutex_exit(&sc
->sc_tskq_mtx
);
263 sdmmc_needs_discover(device_t dev
)
265 struct sdmmc_softc
*sc
= device_private(dev
);
267 if (!ISSET(sc
->sc_flags
, SMF_INITED
))
270 mutex_enter(&sc
->sc_discover_task_mtx
);
271 if (!sdmmc_task_pending(&sc
->sc_discover_task
))
272 sdmmc_add_task(sc
, &sc
->sc_discover_task
);
273 mutex_exit(&sc
->sc_discover_task_mtx
);
277 sdmmc_discover_task(void *arg
)
279 struct sdmmc_softc
*sc
= (struct sdmmc_softc
*)arg
;
281 if (sdmmc_chip_card_detect(sc
->sc_sct
, sc
->sc_sch
)) {
282 if (!ISSET(sc
->sc_flags
, SMF_CARD_PRESENT
)) {
283 SET(sc
->sc_flags
, SMF_CARD_PRESENT
);
284 sdmmc_card_attach(sc
);
287 if (ISSET(sc
->sc_flags
, SMF_CARD_PRESENT
)) {
288 CLR(sc
->sc_flags
, SMF_CARD_PRESENT
);
289 sdmmc_card_detach(sc
, DETACH_FORCE
);
295 * Called from process context when a card is present.
298 sdmmc_card_attach(struct sdmmc_softc
*sc
)
300 struct sdmmc_function
*sf
;
301 struct sdmmc_attach_args saa
;
304 DPRINTF(1,("%s: attach card\n", DEVNAME(sc
)));
306 CLR(sc
->sc_flags
, SMF_CARD_ATTACHED
);
309 * Power up the card (or card stack).
311 error
= sdmmc_enable(sc
);
313 aprint_error_dev(sc
->sc_dev
, "couldn't enable card\n");
318 * Scan for I/O functions and memory cards on the bus,
319 * allocating a sdmmc_function structure for each.
321 error
= sdmmc_scan(sc
);
323 aprint_error_dev(sc
->sc_dev
, "no functions\n");
328 * Set SD/MMC bus clock.
331 if ((sc
->sc_busclk
/ 1000) != 0) {
332 DPRINTF(1,("%s: bus clock: %u.%03u MHz\n", DEVNAME(sc
),
333 sc
->sc_busclk
/ 1000, sc
->sc_busclk
% 1000));
335 DPRINTF(1,("%s: bus clock: %u KHz\n", DEVNAME(sc
),
336 sc
->sc_busclk
% 1000));
339 (void)sdmmc_chip_bus_clock(sc
->sc_sct
, sc
->sc_sch
, sc
->sc_busclk
);
342 * Initialize the I/O functions and memory cards.
344 error
= sdmmc_init(sc
);
346 aprint_error_dev(sc
->sc_dev
, "init failed\n");
350 SIMPLEQ_FOREACH(sf
, &sc
->sf_head
, sf_list
) {
351 if (ISSET(sc
->sc_flags
, SMF_IO_MODE
) && sf
->number
< 1)
354 memset(&saa
, 0, sizeof saa
);
355 saa
.manufacturer
= sf
->cis
.manufacturer
;
356 saa
.product
= sf
->cis
.product
;
360 config_found_ia(sc
->sc_dev
, "sdmmc", &saa
, sdmmc_print
);
363 SET(sc
->sc_flags
, SMF_CARD_ATTACHED
);
367 sdmmc_card_detach(sc
, DETACH_FORCE
);
371 * Called from process context with DETACH_* flags from <sys/device.h>
372 * when cards are gone.
375 sdmmc_card_detach(struct sdmmc_softc
*sc
, int flags
)
377 struct sdmmc_function
*sf
, *sfnext
;
379 DPRINTF(1,("%s: detach card\n", DEVNAME(sc
)));
381 if (ISSET(sc
->sc_flags
, SMF_CARD_ATTACHED
)) {
382 SIMPLEQ_FOREACH(sf
, &sc
->sf_head
, sf_list
) {
383 if (sf
->child
!= NULL
) {
384 config_detach(sf
->child
, DETACH_FORCE
);
389 KASSERT(TAILQ_EMPTY(&sc
->sc_intrq
));
391 CLR(sc
->sc_flags
, SMF_CARD_ATTACHED
);
397 /* Free all sdmmc_function structures. */
398 for (sf
= SIMPLEQ_FIRST(&sc
->sf_head
); sf
!= NULL
; sf
= sfnext
) {
399 sfnext
= SIMPLEQ_NEXT(sf
, sf_list
);
400 sdmmc_function_free(sf
);
402 SIMPLEQ_INIT(&sc
->sf_head
);
403 sc
->sc_function_count
= 0;
408 sdmmc_print(void *aux
, const char *pnp
)
410 struct sdmmc_attach_args
*sa
= aux
;
411 struct sdmmc_function
*sf
= sa
->sf
;
412 struct sdmmc_cis
*cis
= &sf
->sc
->sc_fn0
->cis
;
419 for (i
= 0; i
< 4 && cis
->cis1_info
[i
]; i
++)
420 printf("%s%s", i
? ", " : "\"", cis
->cis1_info
[i
]);
424 if (cis
->manufacturer
!= SDMMC_VENDOR_INVALID
&&
425 cis
->product
!= SDMMC_PRODUCT_INVALID
) {
426 printf("%s(", i
? " " : "");
427 if (cis
->manufacturer
!= SDMMC_VENDOR_INVALID
)
428 printf("manufacturer 0x%x%s",
430 cis
->product
== SDMMC_PRODUCT_INVALID
?
432 if (cis
->product
!= SDMMC_PRODUCT_INVALID
)
433 printf("product 0x%x", cis
->product
);
436 printf("%sat %s", i
? " " : "", pnp
);
439 printf(" function %d", sf
->number
);
442 for (i
= 0; i
< 3 && cis
->cis1_info
[i
]; i
++)
443 printf("%s%s", i
? ", " : " \"", cis
->cis1_info
[i
]);
451 sdmmc_enable(struct sdmmc_softc
*sc
)
456 * Calculate the equivalent of the card OCR from the host
457 * capabilities and select the maximum supported bus voltage.
459 error
= sdmmc_chip_bus_power(sc
->sc_sct
, sc
->sc_sch
,
460 sdmmc_chip_host_ocr(sc
->sc_sct
, sc
->sc_sch
));
462 aprint_error_dev(sc
->sc_dev
, "couldn't supply bus power\n");
467 * Select the minimum clock frequency.
469 error
= sdmmc_chip_bus_clock(sc
->sc_sct
, sc
->sc_sch
, SDMMC_SDCLK_400K
);
471 aprint_error_dev(sc
->sc_dev
, "couldn't supply clock\n");
475 /* XXX wait for card to power up */
478 /* Initialize SD I/O card function(s). */
479 error
= sdmmc_io_enable(sc
);
483 /* Initialize SD/MMC memory card(s). */
484 if (ISSET(sc
->sc_flags
, SMF_MEM_MODE
))
485 error
= sdmmc_mem_enable(sc
);
494 sdmmc_disable(struct sdmmc_softc
*sc
)
496 /* XXX complete commands if card is still present. */
498 /* Make sure no card is still selected. */
499 (void)sdmmc_select_card(sc
, NULL
);
501 /* Turn off bus power and clock. */
502 (void)sdmmc_chip_bus_width(sc
->sc_sct
, sc
->sc_sch
, 1);
503 (void)sdmmc_chip_bus_clock(sc
->sc_sct
, sc
->sc_sch
, SDMMC_SDCLK_OFF
);
504 (void)sdmmc_chip_bus_power(sc
->sc_sct
, sc
->sc_sch
, 0);
508 * Set the lowest bus voltage supported by the card and the host.
511 sdmmc_set_bus_power(struct sdmmc_softc
*sc
, uint32_t host_ocr
,
516 /* Mask off unsupported voltage levels and select the lowest. */
517 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc
), host_ocr
));
518 host_ocr
&= card_ocr
;
519 for (bit
= 4; bit
< 23; bit
++) {
520 if (ISSET(host_ocr
, (1 << bit
))) {
521 host_ocr
&= (3 << bit
);
525 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr
, host_ocr
));
528 sdmmc_chip_bus_power(sc
->sc_sct
, sc
->sc_sch
, host_ocr
) != 0)
533 struct sdmmc_function
*
534 sdmmc_function_alloc(struct sdmmc_softc
*sc
)
536 struct sdmmc_function
*sf
;
538 sf
= malloc(sizeof *sf
, M_DEVBUF
, M_WAITOK
|M_ZERO
);
540 aprint_error_dev(sc
->sc_dev
,
541 "couldn't alloc memory (sdmmc function)\n");
547 sf
->cis
.manufacturer
= SDMMC_VENDOR_INVALID
;
548 sf
->cis
.product
= SDMMC_PRODUCT_INVALID
;
549 sf
->cis
.function
= SDMMC_FUNCTION_INVALID
;
555 sdmmc_function_free(struct sdmmc_function
*sf
)
562 * Scan for I/O functions and memory cards on the bus, allocating a
563 * sdmmc_function structure for each.
566 sdmmc_scan(struct sdmmc_softc
*sc
)
569 /* Scan for I/O functions. */
570 if (ISSET(sc
->sc_flags
, SMF_IO_MODE
))
573 /* Scan for memory cards on the bus. */
574 if (ISSET(sc
->sc_flags
, SMF_MEM_MODE
))
577 /* There should be at least one function now. */
578 if (SIMPLEQ_EMPTY(&sc
->sf_head
)) {
579 aprint_error_dev(sc
->sc_dev
, "couldn't identify card\n");
586 * Initialize all the distinguished functions of the card, be it I/O
587 * or memory functions.
590 sdmmc_init(struct sdmmc_softc
*sc
)
592 struct sdmmc_function
*sf
;
594 /* Initialize all identified card functions. */
595 SIMPLEQ_FOREACH(sf
, &sc
->sf_head
, sf_list
) {
596 if (ISSET(sc
->sc_flags
, SMF_IO_MODE
) &&
597 sdmmc_io_init(sc
, sf
) != 0) {
598 aprint_error_dev(sc
->sc_dev
, "i/o init failed\n");
601 if (ISSET(sc
->sc_flags
, SMF_MEM_MODE
) &&
602 sdmmc_mem_init(sc
, sf
) != 0) {
603 aprint_error_dev(sc
->sc_dev
, "mem init failed\n");
607 /* Any good functions left after initialization? */
608 SIMPLEQ_FOREACH(sf
, &sc
->sf_head
, sf_list
) {
609 if (!ISSET(sf
->flags
, SFF_ERROR
))
613 /* No, we should probably power down the card. */
618 sdmmc_delay(u_int usecs
)
625 sdmmc_app_command(struct sdmmc_softc
*sc
, struct sdmmc_command
*cmd
)
627 struct sdmmc_command acmd
;
630 DPRINTF(1,("sdmmc_app_command: start\n"));
634 memset(&acmd
, 0, sizeof(acmd
));
635 acmd
.c_opcode
= MMC_APP_CMD
;
637 acmd
.c_flags
= SCF_CMD_AC
| SCF_RSP_R1
;
639 error
= sdmmc_mmc_command(sc
, &acmd
);
641 if (!ISSET(MMC_R1(acmd
.c_resp
), MMC_R1_APP_CMD
)) {
642 /* Card does not support application commands. */
645 error
= sdmmc_mmc_command(sc
, cmd
);
648 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error
));
653 * Execute MMC command and data transfers. All interactions with the
654 * host controller to complete the command happen in the context of
655 * the current process.
658 sdmmc_mmc_command(struct sdmmc_softc
*sc
, struct sdmmc_command
*cmd
)
662 DPRINTF(1,("sdmmc_mmc_command: cmd=%#x, arg=%#x, flags=%#x\n",
663 cmd
->c_opcode
, cmd
->c_arg
, cmd
->c_flags
));
667 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
669 if (sc
->sc_card
== NULL
)
670 panic("%s: deselected card\n", DEVNAME(sc
));
674 sdmmc_chip_exec_command(sc
->sc_sct
, sc
->sc_sch
, cmd
);
677 sdmmc_dump_command(sc
, cmd
);
680 error
= cmd
->c_error
;
682 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error
));
688 * Send the "GO IDLE STATE" command.
691 sdmmc_go_idle_state(struct sdmmc_softc
*sc
)
693 struct sdmmc_command cmd
;
695 DPRINTF(1,("sdmmc_go_idle_state\n"));
699 memset(&cmd
, 0, sizeof(cmd
));
700 cmd
.c_opcode
= MMC_GO_IDLE_STATE
;
701 cmd
.c_flags
= SCF_CMD_BC
| SCF_RSP_R0
;
703 (void)sdmmc_mmc_command(sc
, &cmd
);
707 * Retrieve (SD) or set (MMC) the relative card address (RCA).
710 sdmmc_set_relative_addr(struct sdmmc_softc
*sc
, struct sdmmc_function
*sf
)
712 struct sdmmc_command cmd
;
717 memset(&cmd
, 0, sizeof(cmd
));
718 if (ISSET(sc
->sc_flags
, SMF_SD_MODE
)) {
719 cmd
.c_opcode
= SD_SEND_RELATIVE_ADDR
;
720 cmd
.c_flags
= SCF_CMD_BCR
| SCF_RSP_R6
;
722 cmd
.c_opcode
= MMC_SET_RELATIVE_ADDR
;
723 cmd
.c_arg
= MMC_ARG_RCA(sf
->rca
);
724 cmd
.c_flags
= SCF_CMD_AC
| SCF_RSP_R1
;
726 error
= sdmmc_mmc_command(sc
, &cmd
);
730 if (ISSET(sc
->sc_flags
, SMF_SD_MODE
))
731 sf
->rca
= SD_R6_RCA(cmd
.c_resp
);
737 sdmmc_select_card(struct sdmmc_softc
*sc
, struct sdmmc_function
*sf
)
739 struct sdmmc_command cmd
;
744 if (sc
->sc_card
== sf
745 || (sf
&& sc
->sc_card
&& sc
->sc_card
->rca
== sf
->rca
)) {
750 memset(&cmd
, 0, sizeof(cmd
));
751 cmd
.c_opcode
= MMC_SELECT_CARD
;
752 cmd
.c_arg
= (sf
== NULL
) ? 0 : MMC_ARG_RCA(sf
->rca
);
753 cmd
.c_flags
= SCF_CMD_AC
| ((sf
== NULL
) ? SCF_RSP_R0
: SCF_RSP_R1
);
754 error
= sdmmc_mmc_command(sc
, &cmd
);
755 if (error
== 0 || sf
== NULL
)
763 sdmmc_dump_command(struct sdmmc_softc
*sc
, struct sdmmc_command
*cmd
)
767 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x "
768 "proc=\"%s\" (error %d)\n",
769 DEVNAME(sc
), cmd
->c_opcode
, cmd
->c_arg
, cmd
->c_data
,
770 cmd
->c_datalen
, cmd
->c_flags
, curproc
? curproc
->p_comm
: "",
773 if (cmd
->c_error
|| sdmmcdebug
< 1)
776 aprint_normal_dev(sc
->sc_dev
, "resp=");
777 if (ISSET(cmd
->c_flags
, SCF_RSP_136
))
778 for (i
= 0; i
< sizeof cmd
->c_resp
; i
++)
779 aprint_normal("%02x ", ((uint8_t *)cmd
->c_resp
)[i
]);
780 else if (ISSET(cmd
->c_flags
, SCF_RSP_PRESENT
))
781 for (i
= 0; i
< 4; i
++)
782 aprint_normal("%02x ", ((uint8_t *)cmd
->c_resp
)[i
]);