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