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