Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc.c revision 1.1.12.3
      1 /*	$NetBSD: sdmmc.c,v 1.1.12.3 2011/04/21 01:42:02 rmind 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.12.3 2011/04/21 01:42:02 rmind 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 		if (!ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
    350 			aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
    351 		}
    352 		goto err;
    353 	}
    354 
    355 	/*
    356 	 * Scan for I/O functions and memory cards on the bus,
    357 	 * allocating a sdmmc_function structure for each.
    358 	 */
    359 	error = sdmmc_scan(sc);
    360 	if (error) {
    361 		aprint_error_dev(sc->sc_dev, "no functions\n");
    362 		goto err;
    363 	}
    364 
    365 	/*
    366 	 * Initialize the I/O functions and memory cards.
    367 	 */
    368 	error = sdmmc_init(sc);
    369 	if (error) {
    370 		aprint_error_dev(sc->sc_dev, "init failed\n");
    371 		goto err;
    372 	}
    373 
    374 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    375 		if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
    376 			continue;
    377 
    378 		memset(&saa, 0, sizeof saa);
    379 		saa.manufacturer = sf->cis.manufacturer;
    380 		saa.product = sf->cis.product;
    381 		saa.interface = sf->interface;
    382 		saa.sf = sf;
    383 
    384 		sf->child =
    385 		    config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
    386 	}
    387 
    388 	SET(sc->sc_flags, SMF_CARD_ATTACHED);
    389 	return;
    390 
    391 err:
    392 	sdmmc_card_detach(sc, DETACH_FORCE);
    393 }
    394 
    395 /*
    396  * Called from process context with DETACH_* flags from <sys/device.h>
    397  * when cards are gone.
    398  */
    399 static void
    400 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
    401 {
    402 	struct sdmmc_function *sf, *sfnext;
    403 
    404 	DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
    405 
    406 	if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
    407 		SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    408 			if (sf->child != NULL) {
    409 				config_detach(sf->child, DETACH_FORCE);
    410 				sf->child = NULL;
    411 			}
    412 		}
    413 
    414 		KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
    415 
    416 		CLR(sc->sc_flags, SMF_CARD_ATTACHED);
    417 	}
    418 
    419 	/* Power down. */
    420 	sdmmc_disable(sc);
    421 
    422 	/* Free all sdmmc_function structures. */
    423 	for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
    424 		sfnext = SIMPLEQ_NEXT(sf, sf_list);
    425 		sdmmc_function_free(sf);
    426 	}
    427 	SIMPLEQ_INIT(&sc->sf_head);
    428 	sc->sc_function_count = 0;
    429 	sc->sc_fn0 = NULL;
    430 }
    431 
    432 static int
    433 sdmmc_print(void *aux, const char *pnp)
    434 {
    435 	struct sdmmc_attach_args *sa = aux;
    436 	struct sdmmc_function *sf = sa->sf;
    437 	struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
    438 	int i;
    439 
    440 	if (pnp) {
    441 		if (sf->number == 0)
    442 			return QUIET;
    443 
    444 		for (i = 0; i < 4 && cis->cis1_info[i]; i++)
    445 			printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
    446 		if (i != 0)
    447 			printf("\"");
    448 
    449 		if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
    450 		    cis->product != SDMMC_PRODUCT_INVALID) {
    451 			printf("%s(", i ? " " : "");
    452 			if (cis->manufacturer != SDMMC_VENDOR_INVALID)
    453 				printf("manufacturer 0x%x%s",
    454 				    cis->manufacturer,
    455 				    cis->product == SDMMC_PRODUCT_INVALID ?
    456 				    "" : ", ");
    457 			if (cis->product != SDMMC_PRODUCT_INVALID)
    458 				printf("product 0x%x", cis->product);
    459 			printf(")");
    460 		}
    461 		printf("%sat %s", i ? " " : "", pnp);
    462 	}
    463 	if (sf->number > 0)
    464 		printf(" function %d", sf->number);
    465 
    466 	if (!pnp) {
    467 		for (i = 0; i < 3 && cis->cis1_info[i]; i++)
    468 			printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
    469 		if (i != 0)
    470 			printf("\"");
    471 	}
    472 	return UNCONF;
    473 }
    474 
    475 static int
    476 sdmmc_enable(struct sdmmc_softc *sc)
    477 {
    478 	int error;
    479 
    480 	/*
    481 	 * Calculate the equivalent of the card OCR from the host
    482 	 * capabilities and select the maximum supported bus voltage.
    483 	 */
    484 	error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
    485 	    sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
    486 	if (error) {
    487 		aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
    488 		goto out;
    489 	}
    490 
    491 	/*
    492 	 * Select the minimum clock frequency.
    493 	 */
    494 	error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
    495 	if (error) {
    496 		aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
    497 		goto out;
    498 	}
    499 
    500 	/* XXX wait for card to power up */
    501 	sdmmc_delay(100000);
    502 
    503 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    504 		/* Initialize SD I/O card function(s). */
    505 		error = sdmmc_io_enable(sc);
    506 		if (error)
    507 			goto out;
    508 	}
    509 
    510 		/* Initialize SD/MMC memory card(s). */
    511 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
    512 	    ISSET(sc->sc_flags, SMF_MEM_MODE))
    513 		error = sdmmc_mem_enable(sc);
    514 
    515 out:
    516 	if (error)
    517 		sdmmc_disable(sc);
    518 	return error;
    519 }
    520 
    521 static void
    522 sdmmc_disable(struct sdmmc_softc *sc)
    523 {
    524 	/* XXX complete commands if card is still present. */
    525 
    526 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    527 		/* Make sure no card is still selected. */
    528 		(void)sdmmc_select_card(sc, NULL);
    529 	}
    530 
    531 	/* Turn off bus power and clock. */
    532 	(void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
    533 	(void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF);
    534 	(void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
    535 	sc->sc_busclk = sc->sc_clkmax;
    536 }
    537 
    538 /*
    539  * Set the lowest bus voltage supported by the card and the host.
    540  */
    541 int
    542 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
    543     uint32_t card_ocr)
    544 {
    545 	uint32_t bit;
    546 
    547 	/* Mask off unsupported voltage levels and select the lowest. */
    548 	DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
    549 	host_ocr &= card_ocr;
    550 	for (bit = 4; bit < 23; bit++) {
    551 		if (ISSET(host_ocr, (1 << bit))) {
    552 			host_ocr &= (3 << bit);
    553 			break;
    554 		}
    555 	}
    556 	DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
    557 
    558 	if (host_ocr == 0 ||
    559 	    sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
    560 		return 1;
    561 	return 0;
    562 }
    563 
    564 struct sdmmc_function *
    565 sdmmc_function_alloc(struct sdmmc_softc *sc)
    566 {
    567 	struct sdmmc_function *sf;
    568 
    569 	sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
    570 	if (sf == NULL) {
    571 		aprint_error_dev(sc->sc_dev,
    572 		    "couldn't alloc memory (sdmmc function)\n");
    573 		return NULL;
    574 	}
    575 
    576 	sf->sc = sc;
    577 	sf->number = -1;
    578 	sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
    579 	sf->cis.product = SDMMC_PRODUCT_INVALID;
    580 	sf->cis.function = SDMMC_FUNCTION_INVALID;
    581 	sf->width = 1;
    582 
    583 	if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    584 	    ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
    585 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
    586 		bus_dma_segment_t ds;
    587 		int rseg, error;
    588 
    589 		error = bus_dmamap_create(sc->sc_dmat, SDMMC_SECTOR_SIZE, 1,
    590 		    SDMMC_SECTOR_SIZE, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
    591 		if (error)
    592 			goto fail1;
    593 		error = bus_dmamem_alloc(sc->sc_dmat, SDMMC_SECTOR_SIZE,
    594 		    PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
    595 		if (error)
    596 			goto fail2;
    597 		error = bus_dmamem_map(sc->sc_dmat, &ds, 1, SDMMC_SECTOR_SIZE,
    598 		    &sf->bbuf, BUS_DMA_WAITOK);
    599 		if (error)
    600 			goto fail3;
    601 		error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
    602 		    sf->bbuf, SDMMC_SECTOR_SIZE, NULL,
    603 		    BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
    604 		if (!error)
    605 			goto out;
    606 
    607 		bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
    608 fail3:
    609 		bus_dmamem_free(sc->sc_dmat, &ds, 1);
    610 fail2:
    611 		bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
    612 fail1:
    613 		free(sf, M_DEVBUF);
    614 		sf = NULL;
    615 	}
    616 out:
    617 
    618 	return sf;
    619 }
    620 
    621 void
    622 sdmmc_function_free(struct sdmmc_function *sf)
    623 {
    624 	struct sdmmc_softc *sc = sf->sc;
    625 
    626 	if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    627 	    ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
    628 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
    629 		bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
    630 		bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
    631 		bus_dmamem_free(sc->sc_dmat,
    632 		    sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
    633 		bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
    634 	}
    635 
    636 	free(sf, M_DEVBUF);
    637 }
    638 
    639 /*
    640  * Scan for I/O functions and memory cards on the bus, allocating a
    641  * sdmmc_function structure for each.
    642  */
    643 static int
    644 sdmmc_scan(struct sdmmc_softc *sc)
    645 {
    646 
    647 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    648 		/* Scan for I/O functions. */
    649 		if (ISSET(sc->sc_flags, SMF_IO_MODE))
    650 			sdmmc_io_scan(sc);
    651 	}
    652 
    653 	/* Scan for memory cards on the bus. */
    654 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
    655 		sdmmc_mem_scan(sc);
    656 
    657 	/* There should be at least one function now. */
    658 	if (SIMPLEQ_EMPTY(&sc->sf_head)) {
    659 		aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
    660 		return 1;
    661 	}
    662 	return 0;
    663 }
    664 
    665 /*
    666  * Initialize all the distinguished functions of the card, be it I/O
    667  * or memory functions.
    668  */
    669 static int
    670 sdmmc_init(struct sdmmc_softc *sc)
    671 {
    672 	struct sdmmc_function *sf;
    673 
    674 	/* Initialize all identified card functions. */
    675 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    676 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    677 			if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
    678 			    sdmmc_io_init(sc, sf) != 0) {
    679 				aprint_error_dev(sc->sc_dev,
    680 				    "i/o init failed\n");
    681 			}
    682 		}
    683 
    684 		if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
    685 		    sdmmc_mem_init(sc, sf) != 0) {
    686 			aprint_error_dev(sc->sc_dev, "mem init failed\n");
    687 		}
    688 	}
    689 
    690 	/* Any good functions left after initialization? */
    691 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    692 		if (!ISSET(sf->flags, SFF_ERROR))
    693 			return 0;
    694 	}
    695 
    696 	/* No, we should probably power down the card. */
    697 	return 1;
    698 }
    699 
    700 void
    701 sdmmc_delay(u_int usecs)
    702 {
    703 
    704 	delay(usecs);
    705 }
    706 
    707 int
    708 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
    709 {
    710 	struct sdmmc_command acmd;
    711 	int error;
    712 
    713 	DPRINTF(1,("sdmmc_app_command: start\n"));
    714 
    715 	/* Don't lock */
    716 
    717 	memset(&acmd, 0, sizeof(acmd));
    718 	acmd.c_opcode = MMC_APP_CMD;
    719 	if (sf != NULL) {
    720 		acmd.c_arg = sf->rca << 16;
    721 		acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    722 	} else {
    723 		acmd.c_arg = 0;
    724 		acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    725 	}
    726 
    727 	error = sdmmc_mmc_command(sc, &acmd);
    728 	if (error == 0) {
    729 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
    730 		    !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
    731 			/* Card does not support application commands. */
    732 			error = ENODEV;
    733 		} else {
    734 			error = sdmmc_mmc_command(sc, cmd);
    735 		}
    736 	}
    737 	DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
    738 	return error;
    739 }
    740 
    741 /*
    742  * Execute MMC command and data transfers.  All interactions with the
    743  * host controller to complete the command happen in the context of
    744  * the current process.
    745  */
    746 int
    747 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    748 {
    749 	int error;
    750 
    751 	DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
    752 	    cmd->c_opcode, cmd->c_arg, cmd->c_flags));
    753 
    754 	/* Don't lock */
    755 
    756 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
    757 	if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    758 		if (sc->sc_card == NULL)
    759 			panic("%s: deselected card\n", DEVNAME(sc));
    760 	}
    761 #endif
    762 
    763 	sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
    764 
    765 #ifdef SDMMC_DEBUG
    766 	sdmmc_dump_command(sc, cmd);
    767 #endif
    768 
    769 	error = cmd->c_error;
    770 
    771 	DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
    772 
    773 	return error;
    774 }
    775 
    776 /*
    777  * Send the "GO IDLE STATE" command.
    778  */
    779 void
    780 sdmmc_go_idle_state(struct sdmmc_softc *sc)
    781 {
    782 	struct sdmmc_command cmd;
    783 
    784 	DPRINTF(1,("sdmmc_go_idle_state\n"));
    785 
    786 	/* Don't lock */
    787 
    788 	memset(&cmd, 0, sizeof(cmd));
    789 	cmd.c_opcode = MMC_GO_IDLE_STATE;
    790 	cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
    791 
    792 	(void)sdmmc_mmc_command(sc, &cmd);
    793 }
    794 
    795 /*
    796  * Retrieve (SD) or set (MMC) the relative card address (RCA).
    797  */
    798 int
    799 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    800 {
    801 	struct sdmmc_command cmd;
    802 	int error;
    803 
    804 	/* Don't lock */
    805 
    806 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    807 		return EIO;
    808 
    809 	memset(&cmd, 0, sizeof(cmd));
    810 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    811 		cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
    812 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
    813 	} else {
    814 		cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
    815 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
    816 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
    817 	}
    818 	error = sdmmc_mmc_command(sc, &cmd);
    819 	if (error)
    820 		return error;
    821 
    822 	if (ISSET(sc->sc_flags, SMF_SD_MODE))
    823 		sf->rca = SD_R6_RCA(cmd.c_resp);
    824 
    825 	return 0;
    826 }
    827 
    828 int
    829 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    830 {
    831 	struct sdmmc_command cmd;
    832 	int error;
    833 
    834 	/* Don't lock */
    835 
    836 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    837 		return EIO;
    838 
    839 	if (sc->sc_card == sf
    840 	 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
    841 		sc->sc_card = sf;
    842 		return 0;
    843 	}
    844 
    845 	memset(&cmd, 0, sizeof(cmd));
    846 	cmd.c_opcode = MMC_SELECT_CARD;
    847 	cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
    848 	cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
    849 	error = sdmmc_mmc_command(sc, &cmd);
    850 	if (error == 0 || sf == NULL)
    851 		sc->sc_card = sf;
    852 
    853 	return error;
    854 }
    855 
    856 #ifdef SDMMC_DEBUG
    857 static void
    858 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
    859 {
    860 	int i;
    861 
    862 	DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
    863 	    DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
    864 	    cmd->c_datalen, cmd->c_flags, cmd->c_error));
    865 
    866 	if (cmd->c_error || sdmmcdebug < 1)
    867 		return;
    868 
    869 	aprint_normal_dev(sc->sc_dev, "resp=");
    870 	if (ISSET(cmd->c_flags, SCF_RSP_136))
    871 		for (i = 0; i < sizeof cmd->c_resp; i++)
    872 			aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
    873 	else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
    874 		for (i = 0; i < 4; i++)
    875 			aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
    876 	else
    877 		aprint_normal("none");
    878 	aprint_normal("\n");
    879 }
    880 
    881 void
    882 sdmmc_dump_data(const char *title, void *ptr, size_t size)
    883 {
    884 	char buf[16];
    885 	uint8_t *p = ptr;
    886 	int i, j;
    887 
    888 	printf("sdmmc_dump_data: %s\n", title ? title : "");
    889 	printf("--------+--------------------------------------------------+------------------+\n");
    890 	printf("offset  | +0 +1 +2 +3 +4 +5 +6 +7  +8 +9 +a +b +c +d +e +f | data             |\n");
    891 	printf("--------+--------------------------------------------------+------------------+\n");
    892 	for (i = 0; i < (int)size; i++) {
    893 		if ((i % 16) == 0) {
    894 			printf("%08x| ", i);
    895 		} else if ((i % 16) == 8) {
    896 			printf(" ");
    897 		}
    898 
    899 		printf("%02x ", p[i]);
    900 		buf[i % 16] = p[i];
    901 
    902 		if ((i % 16) == 15) {
    903 			printf("| ");
    904 			for (j = 0; j < 16; j++) {
    905 				if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
    906 					printf("%c", buf[j]);
    907 				} else {
    908 					printf(".");
    909 				}
    910 			}
    911 			printf(" |\n");
    912 		}
    913 	}
    914 	if ((i % 16) != 0) {
    915 		j = (i % 16);
    916 		for (; j < 16; j++) {
    917 			printf("   ");
    918 			if ((j % 16) == 8) {
    919 				printf(" ");
    920 			}
    921 		}
    922 
    923 		printf("| ");
    924 		for (j = 0; j < (i % 16); j++) {
    925 			if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
    926 				printf("%c", buf[j]);
    927 			} else {
    928 				printf(".");
    929 			}
    930 		}
    931 		for (; j < 16; j++) {
    932 			printf(" ");
    933 		}
    934 		printf(" |\n");
    935 	}
    936 	printf("--------+--------------------------------------------------+------------------+\n");
    937 }
    938 #endif
    939