Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc.c revision 1.7
      1 /*	$NetBSD: sdmmc.c,v 1.7 2011/02/05 15:45:21 nonaka 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.7 2011/02/05 15:45:21 nonaka 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 #include <sys/callout.h>
     63 
     64 #include <machine/vmparam.h>
     65 
     66 #include <dev/sdmmc/sdmmc_ioreg.h>
     67 #include <dev/sdmmc/sdmmcchip.h>
     68 #include <dev/sdmmc/sdmmcreg.h>
     69 #include <dev/sdmmc/sdmmcvar.h>
     70 
     71 #ifdef SDMMC_DEBUG
     72 int sdmmcdebug = 1;
     73 static void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *);
     74 #define DPRINTF(n,s)	do { if ((n) <= sdmmcdebug) printf s; } while (0)
     75 #else
     76 #define	DPRINTF(n,s)	do {} while (0)
     77 #endif
     78 
     79 #define	DEVNAME(sc)	SDMMCDEVNAME(sc)
     80 
     81 static int sdmmc_match(device_t, cfdata_t, void *);
     82 static void sdmmc_attach(device_t, device_t, void *);
     83 static int sdmmc_detach(device_t, int);
     84 
     85 CFATTACH_DECL_NEW(sdmmc, sizeof(struct sdmmc_softc),
     86     sdmmc_match, sdmmc_attach, sdmmc_detach, NULL);
     87 
     88 static void sdmmc_doattach(device_t);
     89 static void sdmmc_task_thread(void *);
     90 static void sdmmc_discover_task(void *);
     91 static void sdmmc_polling_card(void *);
     92 static void sdmmc_card_attach(struct sdmmc_softc *);
     93 static void sdmmc_card_detach(struct sdmmc_softc *, int);
     94 static int sdmmc_print(void *, const char *);
     95 static int sdmmc_enable(struct sdmmc_softc *);
     96 static void sdmmc_disable(struct sdmmc_softc *);
     97 static int sdmmc_scan(struct sdmmc_softc *);
     98 static int sdmmc_init(struct sdmmc_softc *);
     99 
    100 static int
    101 sdmmc_match(device_t parent, cfdata_t cf, void *aux)
    102 {
    103 	struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
    104 
    105 	if (strcmp(saa->saa_busname, cf->cf_name) == 0)
    106 		return 1;
    107 	return 0;
    108 }
    109 
    110 static void
    111 sdmmc_attach(device_t parent, device_t self, void *aux)
    112 {
    113 	struct sdmmc_softc *sc = device_private(self);
    114 	struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
    115 	int error;
    116 
    117 	aprint_normal("\n");
    118 	aprint_naive("\n");
    119 
    120 	sc->sc_dev = self;
    121 	sc->sc_sct = saa->saa_sct;
    122 	sc->sc_spi_sct = saa->saa_spi_sct;
    123 	sc->sc_sch = saa->saa_sch;
    124 	sc->sc_dmat = saa->saa_dmat;
    125 	sc->sc_clkmin = saa->saa_clkmin;
    126 	sc->sc_clkmax = saa->saa_clkmax;
    127 	sc->sc_busclk = sc->sc_clkmax;
    128 	sc->sc_buswidth = 1;
    129 	sc->sc_caps = saa->saa_caps;
    130 
    131 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    132 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SDMMC_MAXNSEGS,
    133 		    MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->sc_dmap);
    134 		if (error) {
    135 			aprint_error_dev(sc->sc_dev,
    136 			    "couldn't create dma map. (error=%d)\n", error);
    137 			return;
    138 		}
    139 	}
    140 
    141 	if (ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
    142 		callout_init(&sc->sc_card_detect_ch, 0);
    143 		callout_reset(&sc->sc_card_detect_ch, hz,
    144 		    sdmmc_polling_card, sc);
    145 	}
    146 
    147 	SIMPLEQ_INIT(&sc->sf_head);
    148 	TAILQ_INIT(&sc->sc_tskq);
    149 	TAILQ_INIT(&sc->sc_intrq);
    150 
    151 	sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc);
    152 	sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc);
    153 
    154 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    155 	mutex_init(&sc->sc_tskq_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    156 	mutex_init(&sc->sc_discover_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    157 	mutex_init(&sc->sc_intr_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    158 	cv_init(&sc->sc_tskq_cv, "mmctaskq");
    159 
    160 	if (!pmf_device_register(self, NULL, NULL)) {
    161 		aprint_error_dev(self, "couldn't establish power handler\n");
    162 	}
    163 
    164 	SET(sc->sc_flags, SMF_INITED);
    165 
    166 	/*
    167 	 * Create the event thread that will attach and detach cards
    168 	 * and perform other lengthy operations.
    169 	 */
    170 	config_pending_incr();
    171 	config_interrupts(self, sdmmc_doattach);
    172 }
    173 
    174 static int
    175 sdmmc_detach(device_t self, int flags)
    176 {
    177 	struct sdmmc_softc *sc = device_private(self);
    178 	int error;
    179 
    180 	mutex_enter(&sc->sc_tskq_mtx);
    181 	sc->sc_dying = 1;
    182 	cv_signal(&sc->sc_tskq_cv);
    183 	while (sc->sc_tskq_lwp != NULL)
    184 		cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
    185 	mutex_exit(&sc->sc_tskq_mtx);
    186 
    187 	pmf_device_deregister(self);
    188 
    189 	error = config_detach_children(self, flags);
    190 	if (error)
    191 		return error;
    192 	return 0;
    193 }
    194 
    195 static void
    196 sdmmc_doattach(device_t dev)
    197 {
    198 	struct sdmmc_softc *sc = device_private(dev);
    199 
    200 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    201 	    sdmmc_task_thread, sc, &sc->sc_tskq_lwp, "%s", device_xname(dev))) {
    202 		aprint_error_dev(dev, "couldn't create task thread\n");
    203 	}
    204 }
    205 
    206 void
    207 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
    208 {
    209 
    210 	mutex_enter(&sc->sc_tskq_mtx);
    211 	task->onqueue = 1;
    212 	task->sc = sc;
    213 	TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
    214 	cv_broadcast(&sc->sc_tskq_cv);
    215 	mutex_exit(&sc->sc_tskq_mtx);
    216 }
    217 
    218 static inline void
    219 sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
    220 {
    221 
    222 	TAILQ_REMOVE(&sc->sc_tskq, task, next);
    223 	task->sc = NULL;
    224 	task->onqueue = 0;
    225 }
    226 
    227 void
    228 sdmmc_del_task(struct sdmmc_task *task)
    229 {
    230 	struct sdmmc_softc *sc = (struct sdmmc_softc *)task->sc;
    231 
    232 	if (sc != NULL) {
    233 		mutex_enter(&sc->sc_tskq_mtx);
    234 		sdmmc_del_task1(sc, task);
    235 		mutex_exit(&sc->sc_tskq_mtx);
    236 	}
    237 }
    238 
    239 static void
    240 sdmmc_task_thread(void *arg)
    241 {
    242 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    243 	struct sdmmc_task *task;
    244 
    245 	sdmmc_discover_task(sc);
    246 	config_pending_decr();
    247 
    248 	mutex_enter(&sc->sc_tskq_mtx);
    249 	for (;;) {
    250 		task = TAILQ_FIRST(&sc->sc_tskq);
    251 		if (task != NULL) {
    252 			sdmmc_del_task1(sc, task);
    253 			mutex_exit(&sc->sc_tskq_mtx);
    254 			(*task->func)(task->arg);
    255 			mutex_enter(&sc->sc_tskq_mtx);
    256 		} else {
    257 			/* Check for the exit condition. */
    258 			if (sc->sc_dying)
    259 				break;
    260 			cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
    261 		}
    262 	}
    263 	/* time to die. */
    264 	sc->sc_dying = 0;
    265 	if (ISSET(sc->sc_flags, SMF_CARD_PRESENT))
    266 		sdmmc_card_detach(sc, DETACH_FORCE);
    267 	sc->sc_tskq_lwp = NULL;
    268 	cv_broadcast(&sc->sc_tskq_cv);
    269 	mutex_exit(&sc->sc_tskq_mtx);
    270 	kthread_exit(0);
    271 }
    272 
    273 void
    274 sdmmc_needs_discover(device_t dev)
    275 {
    276 	struct sdmmc_softc *sc = device_private(dev);
    277 
    278 	if (!ISSET(sc->sc_flags, SMF_INITED))
    279 		return;
    280 
    281 	mutex_enter(&sc->sc_discover_task_mtx);
    282 	if (!sdmmc_task_pending(&sc->sc_discover_task))
    283 		sdmmc_add_task(sc, &sc->sc_discover_task);
    284 	mutex_exit(&sc->sc_discover_task_mtx);
    285 }
    286 
    287 static void
    288 sdmmc_discover_task(void *arg)
    289 {
    290 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    291 
    292 	if (sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch)) {
    293 		if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
    294 			SET(sc->sc_flags, SMF_CARD_PRESENT);
    295 			sdmmc_card_attach(sc);
    296 			if (!ISSET(sc->sc_flags, SMF_CARD_ATTACHED))
    297 				CLR(sc->sc_flags, SMF_CARD_PRESENT);
    298 		}
    299 	} else {
    300 		if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
    301 			CLR(sc->sc_flags, SMF_CARD_PRESENT);
    302 			sdmmc_card_detach(sc, DETACH_FORCE);
    303 		}
    304 	}
    305 }
    306 
    307 static void
    308 sdmmc_polling_card(void *arg)
    309 {
    310 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    311 	int card_detect;
    312 	int s;
    313 
    314 	s = splsdmmc();
    315 	card_detect = sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch);
    316 	if (card_detect) {
    317 		if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
    318 			sdmmc_needs_discover(sc->sc_dev);
    319 		}
    320 	} else {
    321 		if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
    322 			sdmmc_needs_discover(sc->sc_dev);
    323 		}
    324 	}
    325 	splx(s);
    326 
    327 	callout_schedule(&sc->sc_card_detect_ch, hz);
    328 }
    329 
    330 /*
    331  * Called from process context when a card is present.
    332  */
    333 static void
    334 sdmmc_card_attach(struct sdmmc_softc *sc)
    335 {
    336 	struct sdmmc_function *sf;
    337 	struct sdmmc_attach_args saa;
    338 	int error;
    339 
    340 	DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
    341 
    342 	CLR(sc->sc_flags, SMF_CARD_ATTACHED);
    343 
    344 	/*
    345 	 * Power up the card (or card stack).
    346 	 */
    347 	error = sdmmc_enable(sc);
    348 	if (error) {
    349 		aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
    350 		goto err;
    351 	}
    352 
    353 	/*
    354 	 * Scan for I/O functions and memory cards on the bus,
    355 	 * allocating a sdmmc_function structure for each.
    356 	 */
    357 	error = sdmmc_scan(sc);
    358 	if (error) {
    359 		aprint_error_dev(sc->sc_dev, "no functions\n");
    360 		goto err;
    361 	}
    362 
    363 	/*
    364 	 * Initialize the I/O functions and memory cards.
    365 	 */
    366 	error = sdmmc_init(sc);
    367 	if (error) {
    368 		aprint_error_dev(sc->sc_dev, "init failed\n");
    369 		goto err;
    370 	}
    371 
    372 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    373 		if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
    374 			continue;
    375 
    376 		memset(&saa, 0, sizeof saa);
    377 		saa.manufacturer = sf->cis.manufacturer;
    378 		saa.product = sf->cis.product;
    379 		saa.interface = sf->interface;
    380 		saa.sf = sf;
    381 
    382 		sf->child =
    383 		    config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
    384 	}
    385 
    386 	SET(sc->sc_flags, SMF_CARD_ATTACHED);
    387 	return;
    388 
    389 err:
    390 	sdmmc_card_detach(sc, DETACH_FORCE);
    391 }
    392 
    393 /*
    394  * Called from process context with DETACH_* flags from <sys/device.h>
    395  * when cards are gone.
    396  */
    397 static void
    398 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
    399 {
    400 	struct sdmmc_function *sf, *sfnext;
    401 
    402 	DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
    403 
    404 	if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
    405 		SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    406 			if (sf->child != NULL) {
    407 				config_detach(sf->child, DETACH_FORCE);
    408 				sf->child = NULL;
    409 			}
    410 		}
    411 
    412 		KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
    413 
    414 		CLR(sc->sc_flags, SMF_CARD_ATTACHED);
    415 	}
    416 
    417 	/* Power down. */
    418 	sdmmc_disable(sc);
    419 
    420 	/* Free all sdmmc_function structures. */
    421 	for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
    422 		sfnext = SIMPLEQ_NEXT(sf, sf_list);
    423 		sdmmc_function_free(sf);
    424 	}
    425 	SIMPLEQ_INIT(&sc->sf_head);
    426 	sc->sc_function_count = 0;
    427 	sc->sc_fn0 = NULL;
    428 }
    429 
    430 static int
    431 sdmmc_print(void *aux, const char *pnp)
    432 {
    433 	struct sdmmc_attach_args *sa = aux;
    434 	struct sdmmc_function *sf = sa->sf;
    435 	struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
    436 	int i;
    437 
    438 	if (pnp) {
    439 		if (sf->number == 0)
    440 			return QUIET;
    441 
    442 		for (i = 0; i < 4 && cis->cis1_info[i]; i++)
    443 			printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
    444 		if (i != 0)
    445 			printf("\"");
    446 
    447 		if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
    448 		    cis->product != SDMMC_PRODUCT_INVALID) {
    449 			printf("%s(", i ? " " : "");
    450 			if (cis->manufacturer != SDMMC_VENDOR_INVALID)
    451 				printf("manufacturer 0x%x%s",
    452 				    cis->manufacturer,
    453 				    cis->product == SDMMC_PRODUCT_INVALID ?
    454 				    "" : ", ");
    455 			if (cis->product != SDMMC_PRODUCT_INVALID)
    456 				printf("product 0x%x", cis->product);
    457 			printf(")");
    458 		}
    459 		printf("%sat %s", i ? " " : "", pnp);
    460 	}
    461 	if (sf->number > 0)
    462 		printf(" function %d", sf->number);
    463 
    464 	if (!pnp) {
    465 		for (i = 0; i < 3 && cis->cis1_info[i]; i++)
    466 			printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
    467 		if (i != 0)
    468 			printf("\"");
    469 	}
    470 	return UNCONF;
    471 }
    472 
    473 static int
    474 sdmmc_enable(struct sdmmc_softc *sc)
    475 {
    476 	int error;
    477 
    478 	/*
    479 	 * Calculate the equivalent of the card OCR from the host
    480 	 * capabilities and select the maximum supported bus voltage.
    481 	 */
    482 	error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
    483 	    sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
    484 	if (error) {
    485 		aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
    486 		goto out;
    487 	}
    488 
    489 	/*
    490 	 * Select the minimum clock frequency.
    491 	 */
    492 	error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
    493 	if (error) {
    494 		aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
    495 		goto out;
    496 	}
    497 
    498 	/* XXX wait for card to power up */
    499 	sdmmc_delay(100000);
    500 
    501 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    502 		/* Initialize SD I/O card function(s). */
    503 		error = sdmmc_io_enable(sc);
    504 		if (error)
    505 			goto out;
    506 	}
    507 
    508 		/* Initialize SD/MMC memory card(s). */
    509 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
    510 	    ISSET(sc->sc_flags, SMF_MEM_MODE))
    511 		error = sdmmc_mem_enable(sc);
    512 
    513 out:
    514 	if (error)
    515 		sdmmc_disable(sc);
    516 	return error;
    517 }
    518 
    519 static void
    520 sdmmc_disable(struct sdmmc_softc *sc)
    521 {
    522 	/* XXX complete commands if card is still present. */
    523 
    524 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    525 		/* Make sure no card is still selected. */
    526 		(void)sdmmc_select_card(sc, NULL);
    527 	}
    528 
    529 	/* Turn off bus power and clock. */
    530 	(void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
    531 	(void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF);
    532 	(void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
    533 	sc->sc_busclk = sc->sc_clkmax;
    534 }
    535 
    536 /*
    537  * Set the lowest bus voltage supported by the card and the host.
    538  */
    539 int
    540 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
    541     uint32_t card_ocr)
    542 {
    543 	uint32_t bit;
    544 
    545 	/* Mask off unsupported voltage levels and select the lowest. */
    546 	DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
    547 	host_ocr &= card_ocr;
    548 	for (bit = 4; bit < 23; bit++) {
    549 		if (ISSET(host_ocr, (1 << bit))) {
    550 			host_ocr &= (3 << bit);
    551 			break;
    552 		}
    553 	}
    554 	DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
    555 
    556 	if (host_ocr == 0 ||
    557 	    sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
    558 		return 1;
    559 	return 0;
    560 }
    561 
    562 struct sdmmc_function *
    563 sdmmc_function_alloc(struct sdmmc_softc *sc)
    564 {
    565 	struct sdmmc_function *sf;
    566 
    567 	sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
    568 	if (sf == NULL) {
    569 		aprint_error_dev(sc->sc_dev,
    570 		    "couldn't alloc memory (sdmmc function)\n");
    571 		return NULL;
    572 	}
    573 
    574 	sf->sc = sc;
    575 	sf->number = -1;
    576 	sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
    577 	sf->cis.product = SDMMC_PRODUCT_INVALID;
    578 	sf->cis.function = SDMMC_FUNCTION_INVALID;
    579 
    580 	if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    581 	    ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
    582 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
    583 		bus_dma_segment_t ds;
    584 		int rseg, error;
    585 
    586 		error = bus_dmamap_create(sc->sc_dmat, SDMMC_SECTOR_SIZE, 1,
    587 		    SDMMC_SECTOR_SIZE, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
    588 		if (error)
    589 			goto fail1;
    590 		error = bus_dmamem_alloc(sc->sc_dmat, SDMMC_SECTOR_SIZE,
    591 		    PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
    592 		if (error)
    593 			goto fail2;
    594 		error = bus_dmamem_map(sc->sc_dmat, &ds, 1, SDMMC_SECTOR_SIZE,
    595 		    &sf->bbuf, BUS_DMA_WAITOK);
    596 		if (error)
    597 			goto fail3;
    598 		error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
    599 		    sf->bbuf, SDMMC_SECTOR_SIZE, NULL,
    600 		    BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
    601 		if (!error)
    602 			goto out;
    603 
    604 		bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
    605 fail3:
    606 		bus_dmamem_free(sc->sc_dmat, &ds, 1);
    607 fail2:
    608 		bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
    609 fail1:
    610 		free(sf, M_DEVBUF);
    611 		sf = NULL;
    612 	}
    613 out:
    614 
    615 	return sf;
    616 }
    617 
    618 void
    619 sdmmc_function_free(struct sdmmc_function *sf)
    620 {
    621 	struct sdmmc_softc *sc = sf->sc;
    622 
    623 	if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    624 	    ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
    625 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
    626 		bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
    627 		bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
    628 		bus_dmamem_free(sc->sc_dmat,
    629 		    sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
    630 		bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
    631 	}
    632 
    633 	free(sf, M_DEVBUF);
    634 }
    635 
    636 /*
    637  * Scan for I/O functions and memory cards on the bus, allocating a
    638  * sdmmc_function structure for each.
    639  */
    640 static int
    641 sdmmc_scan(struct sdmmc_softc *sc)
    642 {
    643 
    644 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    645 		/* Scan for I/O functions. */
    646 		if (ISSET(sc->sc_flags, SMF_IO_MODE))
    647 			sdmmc_io_scan(sc);
    648 	}
    649 
    650 	/* Scan for memory cards on the bus. */
    651 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
    652 		sdmmc_mem_scan(sc);
    653 
    654 	/* There should be at least one function now. */
    655 	if (SIMPLEQ_EMPTY(&sc->sf_head)) {
    656 		aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
    657 		return 1;
    658 	}
    659 	return 0;
    660 }
    661 
    662 /*
    663  * Initialize all the distinguished functions of the card, be it I/O
    664  * or memory functions.
    665  */
    666 static int
    667 sdmmc_init(struct sdmmc_softc *sc)
    668 {
    669 	struct sdmmc_function *sf;
    670 
    671 	/* Initialize all identified card functions. */
    672 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    673 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    674 			if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
    675 			    sdmmc_io_init(sc, sf) != 0) {
    676 				aprint_error_dev(sc->sc_dev,
    677 				    "i/o init failed\n");
    678 			}
    679 		}
    680 
    681 		if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    682 		    sdmmc_mem_init(sc, sf) != 0) {
    683 			aprint_error_dev(sc->sc_dev, "mem init failed\n");
    684 		}
    685 	}
    686 
    687 	/* Any good functions left after initialization? */
    688 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    689 		if (!ISSET(sf->flags, SFF_ERROR))
    690 			return 0;
    691 	}
    692 
    693 	/* No, we should probably power down the card. */
    694 	return 1;
    695 }
    696 
    697 void
    698 sdmmc_delay(u_int usecs)
    699 {
    700 
    701 	delay(usecs);
    702 }
    703 
    704 int
    705 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
    706 {
    707 	struct sdmmc_command acmd;
    708 	int error;
    709 
    710 	DPRINTF(1,("sdmmc_app_command: start\n"));
    711 
    712 	/* Don't lock */
    713 
    714 	memset(&acmd, 0, sizeof(acmd));
    715 	acmd.c_opcode = MMC_APP_CMD;
    716 	if (sf != NULL) {
    717 		acmd.c_arg = sf->rca << 16;
    718 		acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    719 	} else {
    720 		acmd.c_arg = 0;
    721 		acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    722 	}
    723 
    724 	error = sdmmc_mmc_command(sc, &acmd);
    725 	if (error == 0) {
    726 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
    727 		    !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
    728 			/* Card does not support application commands. */
    729 			error = ENODEV;
    730 		} else {
    731 			error = sdmmc_mmc_command(sc, cmd);
    732 		}
    733 	}
    734 	DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
    735 	return error;
    736 }
    737 
    738 /*
    739  * Execute MMC command and data transfers.  All interactions with the
    740  * host controller to complete the command happen in the context of
    741  * the current process.
    742  */
    743 int
    744 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    745 {
    746 	int error;
    747 
    748 	DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
    749 	    cmd->c_opcode, cmd->c_arg, cmd->c_flags));
    750 
    751 	/* Don't lock */
    752 
    753 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
    754 	if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    755 		if (sc->sc_card == NULL)
    756 			panic("%s: deselected card\n", DEVNAME(sc));
    757 	}
    758 #endif
    759 
    760 	sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
    761 
    762 #ifdef SDMMC_DEBUG
    763 	sdmmc_dump_command(sc, cmd);
    764 #endif
    765 
    766 	error = cmd->c_error;
    767 
    768 	DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
    769 
    770 	return error;
    771 }
    772 
    773 /*
    774  * Send the "GO IDLE STATE" command.
    775  */
    776 void
    777 sdmmc_go_idle_state(struct sdmmc_softc *sc)
    778 {
    779 	struct sdmmc_command cmd;
    780 
    781 	DPRINTF(1,("sdmmc_go_idle_state\n"));
    782 
    783 	/* Don't lock */
    784 
    785 	memset(&cmd, 0, sizeof(cmd));
    786 	cmd.c_opcode = MMC_GO_IDLE_STATE;
    787 	cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
    788 
    789 	(void)sdmmc_mmc_command(sc, &cmd);
    790 }
    791 
    792 /*
    793  * Retrieve (SD) or set (MMC) the relative card address (RCA).
    794  */
    795 int
    796 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    797 {
    798 	struct sdmmc_command cmd;
    799 	int error;
    800 
    801 	/* Don't lock */
    802 
    803 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    804 		return EIO;
    805 
    806 	memset(&cmd, 0, sizeof(cmd));
    807 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    808 		cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
    809 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
    810 	} else {
    811 		cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
    812 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
    813 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
    814 	}
    815 	error = sdmmc_mmc_command(sc, &cmd);
    816 	if (error)
    817 		return error;
    818 
    819 	if (ISSET(sc->sc_flags, SMF_SD_MODE))
    820 		sf->rca = SD_R6_RCA(cmd.c_resp);
    821 
    822 	return 0;
    823 }
    824 
    825 int
    826 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    827 {
    828 	struct sdmmc_command cmd;
    829 	int error;
    830 
    831 	/* Don't lock */
    832 
    833 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    834 		return EIO;
    835 
    836 	if (sc->sc_card == sf
    837 	 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
    838 		sc->sc_card = sf;
    839 		return 0;
    840 	}
    841 
    842 	memset(&cmd, 0, sizeof(cmd));
    843 	cmd.c_opcode = MMC_SELECT_CARD;
    844 	cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
    845 	cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
    846 	error = sdmmc_mmc_command(sc, &cmd);
    847 	if (error == 0 || sf == NULL)
    848 		sc->sc_card = sf;
    849 
    850 	return error;
    851 }
    852 
    853 #ifdef SDMMC_DEBUG
    854 static void
    855 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    856 {
    857 	int i;
    858 
    859 	DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
    860 	    DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
    861 	    cmd->c_datalen, cmd->c_flags, cmd->c_error));
    862 
    863 	if (cmd->c_error || sdmmcdebug < 1)
    864 		return;
    865 
    866 	aprint_normal_dev(sc->sc_dev, "resp=");
    867 	if (ISSET(cmd->c_flags, SCF_RSP_136))
    868 		for (i = 0; i < sizeof cmd->c_resp; i++)
    869 			aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
    870 	else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
    871 		for (i = 0; i < 4; i++)
    872 			aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
    873 	else
    874 		aprint_normal("none");
    875 	aprint_normal("\n");
    876 }
    877 
    878 void
    879 sdmmc_dump_data(const char *title, void *ptr, size_t size)
    880 {
    881 	char buf[16];
    882 	uint8_t *p = ptr;
    883 	int i, j;
    884 
    885 	printf("sdmmc_dump_data: %s\n", title ? title : "");
    886 	printf("--------+--------------------------------------------------+------------------+\n");
    887 	printf("offset  | +0 +1 +2 +3 +4 +5 +6 +7  +8 +9 +a +b +c +d +e +f | data             |\n");
    888 	printf("--------+--------------------------------------------------+------------------+\n");
    889 	for (i = 0; i < (int)size; i++) {
    890 		if ((i % 16) == 0) {
    891 			printf("%08x| ", i);
    892 		} else if ((i % 16) == 8) {
    893 			printf(" ");
    894 		}
    895 
    896 		printf("%02x ", p[i]);
    897 		buf[i % 16] = p[i];
    898 
    899 		if ((i % 16) == 15) {
    900 			printf("| ");
    901 			for (j = 0; j < 16; j++) {
    902 				if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
    903 					printf("%c", buf[j]);
    904 				} else {
    905 					printf(".");
    906 				}
    907 			}
    908 			printf(" |\n");
    909 		}
    910 	}
    911 	if ((i % 16) != 0) {
    912 		j = (i % 16);
    913 		for (; j < 16; j++) {
    914 			printf("   ");
    915 			if ((j % 16) == 8) {
    916 				printf(" ");
    917 			}
    918 		}
    919 
    920 		printf("| ");
    921 		for (j = 0; j < (i % 16); j++) {
    922 			if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
    923 				printf("%c", buf[j]);
    924 			} else {
    925 				printf(".");
    926 			}
    927 		}
    928 		for (; j < 16; j++) {
    929 			printf(" ");
    930 		}
    931 		printf(" |\n");
    932 	}
    933 	printf("--------+--------------------------------------------------+------------------+\n");
    934 }
    935 #endif
    936