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