Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / sdmmc / sdmmc.c
blobb6f8cfdbe60646e23ae96e608fba8149b09798d9
1 /* $NetBSD$ */
2 /* $OpenBSD: sdmmc.c,v 1.18 2009/01/09 10:58:38 jsg Exp $ */
4 /*
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.
20 /*-
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
26 * are met:
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
43 * SUCH DAMAGE.
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>
60 #include <sys/proc.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>
68 #ifdef SDMMC_DEBUG
69 int sdmmcdebug = 1;
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)
72 #else
73 #define DPRINTF(n,s) do {} while (0)
74 #endif
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 *);
96 static int
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)
102 return 1;
103 return 0;
106 static void
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;
111 int error;
113 aprint_normal("\n");
114 aprint_naive("\n");
116 sc->sc_dev = self;
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;
123 sc->sc_buswidth = 1;
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);
129 if (error) {
130 aprint_error_dev(sc->sc_dev,
131 "couldn't create dma map. (error=%d)\n", error);
132 return;
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);
163 static int
164 sdmmc_detach(device_t self, int flags)
166 struct sdmmc_softc *sc = device_private(self);
167 int error;
169 mutex_enter(&sc->sc_tskq_mtx);
170 sc->sc_dying = 1;
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);
179 if (error)
180 return error;
181 return 0;
184 static void
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");
195 void
196 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
199 mutex_enter(&sc->sc_tskq_mtx);
200 task->onqueue = 1;
201 task->sc = sc;
202 TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
203 cv_broadcast(&sc->sc_tskq_cv);
204 mutex_exit(&sc->sc_tskq_mtx);
207 static inline void
208 sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
211 TAILQ_REMOVE(&sc->sc_tskq, task, next);
212 task->sc = NULL;
213 task->onqueue = 0;
216 void
217 sdmmc_del_task(struct sdmmc_task *task)
219 struct sdmmc_softc *sc = (struct sdmmc_softc *)task->sc;
221 if (sc != NULL) {
222 mutex_enter(&sc->sc_tskq_mtx);
223 sdmmc_del_task1(sc, task);
224 mutex_exit(&sc->sc_tskq_mtx);
228 static void
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);
238 for (;;) {
239 task = TAILQ_FIRST(&sc->sc_tskq);
240 if (task != NULL) {
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);
245 } else {
246 /* Check for the exit condition. */
247 if (sc->sc_dying)
248 break;
249 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
252 /* time to die. */
253 sc->sc_dying = 0;
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);
259 kthread_exit(0);
262 void
263 sdmmc_needs_discover(device_t dev)
265 struct sdmmc_softc *sc = device_private(dev);
267 if (!ISSET(sc->sc_flags, SMF_INITED))
268 return;
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);
276 static void
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);
286 } else {
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.
297 static void
298 sdmmc_card_attach(struct sdmmc_softc *sc)
300 struct sdmmc_function *sf;
301 struct sdmmc_attach_args saa;
302 int error;
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);
312 if (error) {
313 aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
314 goto err;
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);
322 if (error) {
323 aprint_error_dev(sc->sc_dev, "no functions\n");
324 goto err;
328 * Set SD/MMC bus clock.
330 #ifdef SDMMC_DEBUG
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));
334 } else {
335 DPRINTF(1,("%s: bus clock: %u KHz\n", DEVNAME(sc),
336 sc->sc_busclk % 1000));
338 #endif
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);
345 if (error) {
346 aprint_error_dev(sc->sc_dev, "init failed\n");
347 goto err;
350 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
351 if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
352 continue;
354 memset(&saa, 0, sizeof saa);
355 saa.manufacturer = sf->cis.manufacturer;
356 saa.product = sf->cis.product;
357 saa.sf = sf;
359 sf->child =
360 config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
363 SET(sc->sc_flags, SMF_CARD_ATTACHED);
364 return;
366 err:
367 sdmmc_card_detach(sc, DETACH_FORCE);
371 * Called from process context with DETACH_* flags from <sys/device.h>
372 * when cards are gone.
374 static void
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);
385 sf->child = NULL;
389 KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
391 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
394 /* Power down. */
395 sdmmc_disable(sc);
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;
404 sc->sc_fn0 = NULL;
407 static int
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;
413 int i;
415 if (pnp) {
416 if (sf->number == 0)
417 return QUIET;
419 for (i = 0; i < 4 && cis->cis1_info[i]; i++)
420 printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
421 if (i != 0)
422 printf("\"");
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",
429 cis->manufacturer,
430 cis->product == SDMMC_PRODUCT_INVALID ?
431 "" : ", ");
432 if (cis->product != SDMMC_PRODUCT_INVALID)
433 printf("product 0x%x", cis->product);
434 printf(")");
436 printf("%sat %s", i ? " " : "", pnp);
438 if (sf->number > 0)
439 printf(" function %d", sf->number);
441 if (!pnp) {
442 for (i = 0; i < 3 && cis->cis1_info[i]; i++)
443 printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
444 if (i != 0)
445 printf("\"");
447 return UNCONF;
450 static int
451 sdmmc_enable(struct sdmmc_softc *sc)
453 int error;
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));
461 if (error) {
462 aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
463 goto out;
467 * Select the minimum clock frequency.
469 error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
470 if (error) {
471 aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
472 goto out;
475 /* XXX wait for card to power up */
476 sdmmc_delay(100000);
478 /* Initialize SD I/O card function(s). */
479 error = sdmmc_io_enable(sc);
480 if (error)
481 goto out;
483 /* Initialize SD/MMC memory card(s). */
484 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
485 error = sdmmc_mem_enable(sc);
487 out:
488 if (error)
489 sdmmc_disable(sc);
490 return error;
493 static void
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,
512 uint32_t card_ocr)
514 uint32_t bit;
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);
522 break;
525 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
527 if (host_ocr == 0 ||
528 sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
529 return 1;
530 return 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);
539 if (sf == NULL) {
540 aprint_error_dev(sc->sc_dev,
541 "couldn't alloc memory (sdmmc function)\n");
542 return NULL;
545 sf->sc = sc;
546 sf->number = -1;
547 sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
548 sf->cis.product = SDMMC_PRODUCT_INVALID;
549 sf->cis.function = SDMMC_FUNCTION_INVALID;
551 return sf;
554 void
555 sdmmc_function_free(struct sdmmc_function *sf)
558 free(sf, M_DEVBUF);
562 * Scan for I/O functions and memory cards on the bus, allocating a
563 * sdmmc_function structure for each.
565 static int
566 sdmmc_scan(struct sdmmc_softc *sc)
569 /* Scan for I/O functions. */
570 if (ISSET(sc->sc_flags, SMF_IO_MODE))
571 sdmmc_io_scan(sc);
573 /* Scan for memory cards on the bus. */
574 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
575 sdmmc_mem_scan(sc);
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");
580 return 1;
582 return 0;
586 * Initialize all the distinguished functions of the card, be it I/O
587 * or memory functions.
589 static int
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))
610 return 0;
613 /* No, we should probably power down the card. */
614 return 1;
617 void
618 sdmmc_delay(u_int usecs)
621 delay(usecs);
625 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
627 struct sdmmc_command acmd;
628 int error;
630 DPRINTF(1,("sdmmc_app_command: start\n"));
632 /* Don't lock */
634 memset(&acmd, 0, sizeof(acmd));
635 acmd.c_opcode = MMC_APP_CMD;
636 acmd.c_arg = 0;
637 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
639 error = sdmmc_mmc_command(sc, &acmd);
640 if (error == 0) {
641 if (!ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
642 /* Card does not support application commands. */
643 error = ENODEV;
644 } else {
645 error = sdmmc_mmc_command(sc, cmd);
648 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
649 return 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)
660 int error;
662 DPRINTF(1,("sdmmc_mmc_command: cmd=%#x, arg=%#x, flags=%#x\n",
663 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
665 /* Don't lock */
667 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
668 if (cmd->c_data) {
669 if (sc->sc_card == NULL)
670 panic("%s: deselected card\n", DEVNAME(sc));
672 #endif
674 sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
676 #ifdef SDMMC_DEBUG
677 sdmmc_dump_command(sc, cmd);
678 #endif
680 error = cmd->c_error;
682 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
684 return error;
688 * Send the "GO IDLE STATE" command.
690 void
691 sdmmc_go_idle_state(struct sdmmc_softc *sc)
693 struct sdmmc_command cmd;
695 DPRINTF(1,("sdmmc_go_idle_state\n"));
697 /* Don't lock */
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;
713 int error;
715 /* Don't lock */
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;
721 } else {
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);
727 if (error)
728 return error;
730 if (ISSET(sc->sc_flags, SMF_SD_MODE))
731 sf->rca = SD_R6_RCA(cmd.c_resp);
733 return 0;
737 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
739 struct sdmmc_command cmd;
740 int error;
742 /* Don't lock */
744 if (sc->sc_card == sf
745 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
746 sc->sc_card = sf;
747 return 0;
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)
756 sc->sc_card = sf;
758 return error;
761 #ifdef SDMMC_DEBUG
762 static void
763 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
765 int i;
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 : "",
771 cmd->c_error));
773 if (cmd->c_error || sdmmcdebug < 1)
774 return;
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]);
783 aprint_normal("\n");
785 #endif