Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc.c revision 1.1.6.2
      1 /*	$NetBSD: sdmmc.c,v 1.1.6.2 2009/05/13 17:21:29 jym Exp $	*/
      2 /*	$OpenBSD: sdmmc.c,v 1.18 2009/01/09 10:58:38 jsg Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
      6  *
      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.
     10  *
     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.
     18  */
     19 
     20 /*-
     21  * Copyright (c) 2007-2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
     22  * All rights reserved.
     23  *
     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.
     32  *
     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.
     44  */
     45 
     46 /*
     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.
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: sdmmc.c,v 1.1.6.2 2009/05/13 17:21:29 jym Exp $");
     54 
     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>
     62 
     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>
     67 
     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
     75 
     76 #define	DEVNAME(sc)	SDMMCDEVNAME(sc)
     77 
     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);
     81 
     82 CFATTACH_DECL_NEW(sdmmc, sizeof(struct sdmmc_softc),
     83     sdmmc_match, sdmmc_attach, sdmmc_detach, NULL);
     84 
     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 *);
     95 
     96 static int
     97 sdmmc_match(device_t parent, cfdata_t cf, void *aux)
     98 {
     99 	struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
    100 
    101 	if (strcmp(saa->saa_busname, cf->cf_name) == 0)
    102 		return 1;
    103 	return 0;
    104 }
    105 
    106 static void
    107 sdmmc_attach(device_t parent, device_t self, void *aux)
    108 {
    109 	struct sdmmc_softc *sc = device_private(self);
    110 	struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
    111 	int error;
    112 
    113 	aprint_normal("\n");
    114 	aprint_naive("\n");
    115 
    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;
    125 
    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;
    133 		}
    134 	}
    135 
    136 	SIMPLEQ_INIT(&sc->sf_head);
    137 	TAILQ_INIT(&sc->sc_tskq);
    138 	TAILQ_INIT(&sc->sc_intrq);
    139 
    140 	sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc);
    141 	sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc);
    142 
    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");
    148 
    149 	if (!pmf_device_register(self, NULL, NULL)) {
    150 		aprint_error_dev(self, "couldn't establish power handler\n");
    151 	}
    152 
    153 	SET(sc->sc_flags, SMF_INITED);
    154 
    155 	/*
    156 	 * Create the event thread that will attach and detach cards
    157 	 * and perform other lengthy operations.
    158 	 */
    159 	config_pending_incr();
    160 	config_interrupts(self, sdmmc_doattach);
    161 }
    162 
    163 static int
    164 sdmmc_detach(device_t self, int flags)
    165 {
    166 	struct sdmmc_softc *sc = device_private(self);
    167 	int error;
    168 
    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);
    175 
    176 	pmf_device_deregister(self);
    177 
    178 	error = config_detach_children(self, flags);
    179 	if (error)
    180 		return error;
    181 	return 0;
    182 }
    183 
    184 static void
    185 sdmmc_doattach(device_t dev)
    186 {
    187 	struct sdmmc_softc *sc = device_private(dev);
    188 
    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");
    192 	}
    193 }
    194 
    195 void
    196 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
    197 {
    198 
    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);
    205 }
    206 
    207 static inline void
    208 sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
    209 {
    210 
    211 	TAILQ_REMOVE(&sc->sc_tskq, task, next);
    212 	task->sc = NULL;
    213 	task->onqueue = 0;
    214 }
    215 
    216 void
    217 sdmmc_del_task(struct sdmmc_task *task)
    218 {
    219 	struct sdmmc_softc *sc = (struct sdmmc_softc *)task->sc;
    220 
    221 	if (sc != NULL) {
    222 		mutex_enter(&sc->sc_tskq_mtx);
    223 		sdmmc_del_task1(sc, task);
    224 		mutex_exit(&sc->sc_tskq_mtx);
    225 	}
    226 }
    227 
    228 static void
    229 sdmmc_task_thread(void *arg)
    230 {
    231 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    232 	struct sdmmc_task *task;
    233 
    234 	sdmmc_discover_task(sc);
    235 	config_pending_decr();
    236 
    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);
    250 		}
    251 	}
    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);
    260 }
    261 
    262 void
    263 sdmmc_needs_discover(device_t dev)
    264 {
    265 	struct sdmmc_softc *sc = device_private(dev);
    266 
    267 	if (!ISSET(sc->sc_flags, SMF_INITED))
    268 		return;
    269 
    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);
    274 }
    275 
    276 static void
    277 sdmmc_discover_task(void *arg)
    278 {
    279 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    280 
    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);
    285 		}
    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);
    290 		}
    291 	}
    292 }
    293 
    294 /*
    295  * Called from process context when a card is present.
    296  */
    297 static void
    298 sdmmc_card_attach(struct sdmmc_softc *sc)
    299 {
    300 	struct sdmmc_function *sf;
    301 	struct sdmmc_attach_args saa;
    302 	int error;
    303 
    304 	DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
    305 
    306 	CLR(sc->sc_flags, SMF_CARD_ATTACHED);
    307 
    308 	/*
    309 	 * Power up the card (or card stack).
    310 	 */
    311 	error = sdmmc_enable(sc);
    312 	if (error) {
    313 		aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
    314 		goto err;
    315 	}
    316 
    317 	/*
    318 	 * Scan for I/O functions and memory cards on the bus,
    319 	 * allocating a sdmmc_function structure for each.
    320 	 */
    321 	error = sdmmc_scan(sc);
    322 	if (error) {
    323 		aprint_error_dev(sc->sc_dev, "no functions\n");
    324 		goto err;
    325 	}
    326 
    327 	/*
    328 	 * Set SD/MMC bus clock.
    329 	 */
    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));
    337 	}
    338 #endif
    339 	(void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
    340 
    341 	/*
    342 	 * Initialize the I/O functions and memory cards.
    343 	 */
    344 	error = sdmmc_init(sc);
    345 	if (error) {
    346 		aprint_error_dev(sc->sc_dev, "init failed\n");
    347 		goto err;
    348 	}
    349 
    350 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    351 		if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
    352 			continue;
    353 
    354 		memset(&saa, 0, sizeof saa);
    355 		saa.manufacturer = sf->cis.manufacturer;
    356 		saa.product = sf->cis.product;
    357 		saa.sf = sf;
    358 
    359 		sf->child =
    360 		    config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
    361 	}
    362 
    363 	SET(sc->sc_flags, SMF_CARD_ATTACHED);
    364 	return;
    365 
    366 err:
    367 	sdmmc_card_detach(sc, DETACH_FORCE);
    368 }
    369 
    370 /*
    371  * Called from process context with DETACH_* flags from <sys/device.h>
    372  * when cards are gone.
    373  */
    374 static void
    375 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
    376 {
    377 	struct sdmmc_function *sf, *sfnext;
    378 
    379 	DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
    380 
    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;
    386 			}
    387 		}
    388 
    389 		KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
    390 
    391 		CLR(sc->sc_flags, SMF_CARD_ATTACHED);
    392 	}
    393 
    394 	/* Power down. */
    395 	sdmmc_disable(sc);
    396 
    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);
    401 	}
    402 	SIMPLEQ_INIT(&sc->sf_head);
    403 	sc->sc_function_count = 0;
    404 	sc->sc_fn0 = NULL;
    405 }
    406 
    407 static int
    408 sdmmc_print(void *aux, const char *pnp)
    409 {
    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;
    414 
    415 	if (pnp) {
    416 		if (sf->number == 0)
    417 			return QUIET;
    418 
    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("\"");
    423 
    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(")");
    435 		}
    436 		printf("%sat %s", i ? " " : "", pnp);
    437 	}
    438 	if (sf->number > 0)
    439 		printf(" function %d", sf->number);
    440 
    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("\"");
    446 	}
    447 	return UNCONF;
    448 }
    449 
    450 static int
    451 sdmmc_enable(struct sdmmc_softc *sc)
    452 {
    453 	int error;
    454 
    455 	/*
    456 	 * Calculate the equivalent of the card OCR from the host
    457 	 * capabilities and select the maximum supported bus voltage.
    458 	 */
    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;
    464 	}
    465 
    466 	/*
    467 	 * Select the minimum clock frequency.
    468 	 */
    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;
    473 	}
    474 
    475 	/* XXX wait for card to power up */
    476 	sdmmc_delay(100000);
    477 
    478 	/* Initialize SD I/O card function(s). */
    479 	error = sdmmc_io_enable(sc);
    480 	if (error)
    481 		goto out;
    482 
    483 	/* Initialize SD/MMC memory card(s). */
    484 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
    485 		error = sdmmc_mem_enable(sc);
    486 
    487 out:
    488 	if (error)
    489 		sdmmc_disable(sc);
    490 	return error;
    491 }
    492 
    493 static void
    494 sdmmc_disable(struct sdmmc_softc *sc)
    495 {
    496 	/* XXX complete commands if card is still present. */
    497 
    498 	/* Make sure no card is still selected. */
    499 	(void)sdmmc_select_card(sc, NULL);
    500 
    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);
    505 }
    506 
    507 /*
    508  * Set the lowest bus voltage supported by the card and the host.
    509  */
    510 int
    511 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
    512     uint32_t card_ocr)
    513 {
    514 	uint32_t bit;
    515 
    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;
    523 		}
    524 	}
    525 	DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
    526 
    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;
    531 }
    532 
    533 struct sdmmc_function *
    534 sdmmc_function_alloc(struct sdmmc_softc *sc)
    535 {
    536 	struct sdmmc_function *sf;
    537 
    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;
    543 	}
    544 
    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;
    550 
    551 	return sf;
    552 }
    553 
    554 void
    555 sdmmc_function_free(struct sdmmc_function *sf)
    556 {
    557 
    558 	free(sf, M_DEVBUF);
    559 }
    560 
    561 /*
    562  * Scan for I/O functions and memory cards on the bus, allocating a
    563  * sdmmc_function structure for each.
    564  */
    565 static int
    566 sdmmc_scan(struct sdmmc_softc *sc)
    567 {
    568 
    569 	/* Scan for I/O functions. */
    570 	if (ISSET(sc->sc_flags, SMF_IO_MODE))
    571 		sdmmc_io_scan(sc);
    572 
    573 	/* Scan for memory cards on the bus. */
    574 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
    575 		sdmmc_mem_scan(sc);
    576 
    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;
    581 	}
    582 	return 0;
    583 }
    584 
    585 /*
    586  * Initialize all the distinguished functions of the card, be it I/O
    587  * or memory functions.
    588  */
    589 static int
    590 sdmmc_init(struct sdmmc_softc *sc)
    591 {
    592 	struct sdmmc_function *sf;
    593 
    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");
    599 		}
    600 
    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");
    604 		}
    605 	}
    606 
    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;
    611 	}
    612 
    613 	/* No, we should probably power down the card. */
    614 	return 1;
    615 }
    616 
    617 void
    618 sdmmc_delay(u_int usecs)
    619 {
    620 
    621 	delay(usecs);
    622 }
    623 
    624 int
    625 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    626 {
    627 	struct sdmmc_command acmd;
    628 	int error;
    629 
    630 	DPRINTF(1,("sdmmc_app_command: start\n"));
    631 
    632 	/* Don't lock */
    633 
    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;
    638 
    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);
    646 		}
    647 	}
    648 	DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
    649 	return error;
    650 }
    651 
    652 /*
    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.
    656  */
    657 int
    658 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    659 {
    660 	int error;
    661 
    662 	DPRINTF(1,("sdmmc_mmc_command: cmd=%#x, arg=%#x, flags=%#x\n",
    663 	    cmd->c_opcode, cmd->c_arg, cmd->c_flags));
    664 
    665 	/* Don't lock */
    666 
    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));
    671 	}
    672 #endif
    673 
    674 	sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
    675 
    676 #ifdef SDMMC_DEBUG
    677 	sdmmc_dump_command(sc, cmd);
    678 #endif
    679 
    680 	error = cmd->c_error;
    681 
    682 	DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
    683 
    684 	return error;
    685 }
    686 
    687 /*
    688  * Send the "GO IDLE STATE" command.
    689  */
    690 void
    691 sdmmc_go_idle_state(struct sdmmc_softc *sc)
    692 {
    693 	struct sdmmc_command cmd;
    694 
    695 	DPRINTF(1,("sdmmc_go_idle_state\n"));
    696 
    697 	/* Don't lock */
    698 
    699 	memset(&cmd, 0, sizeof(cmd));
    700 	cmd.c_opcode = MMC_GO_IDLE_STATE;
    701 	cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0;
    702 
    703 	(void)sdmmc_mmc_command(sc, &cmd);
    704 }
    705 
    706 /*
    707  * Retrieve (SD) or set (MMC) the relative card address (RCA).
    708  */
    709 int
    710 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    711 {
    712 	struct sdmmc_command cmd;
    713 	int error;
    714 
    715 	/* Don't lock */
    716 
    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;
    725 	}
    726 	error = sdmmc_mmc_command(sc, &cmd);
    727 	if (error)
    728 		return error;
    729 
    730 	if (ISSET(sc->sc_flags, SMF_SD_MODE))
    731 		sf->rca = SD_R6_RCA(cmd.c_resp);
    732 
    733 	return 0;
    734 }
    735 
    736 int
    737 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    738 {
    739 	struct sdmmc_command cmd;
    740 	int error;
    741 
    742 	/* Don't lock */
    743 
    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;
    748 	}
    749 
    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;
    757 
    758 	return error;
    759 }
    760 
    761 #ifdef SDMMC_DEBUG
    762 static void
    763 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    764 {
    765 	int i;
    766 
    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));
    772 
    773 	if (cmd->c_error || sdmmcdebug < 1)
    774 		return;
    775 
    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");
    784 }
    785 #endif
    786