xref: /openbsd/sys/dev/sdmmc/sdmmc.c (revision 771fbea0)
1 /*	$OpenBSD: sdmmc.c,v 1.58 2020/08/24 15:06:10 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Host controller independent SD/MMC bus driver based on information
21  * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO
22  * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver.
23  */
24 
25 #include <sys/param.h>
26 #include <sys/device.h>
27 #include <sys/kernel.h>
28 #include <sys/kthread.h>
29 #include <sys/malloc.h>
30 #include <sys/rwlock.h>
31 #include <sys/systm.h>
32 #include <sys/time.h>
33 
34 #ifdef SDMMC_DEBUG
35 #include <sys/proc.h>
36 #endif
37 
38 #include <scsi/scsi_all.h>
39 #include <scsi/scsiconf.h>
40 
41 #include <dev/sdmmc/sdmmc_scsi.h>
42 #include <dev/sdmmc/sdmmcchip.h>
43 #include <dev/sdmmc/sdmmcreg.h>
44 #include <dev/sdmmc/sdmmcvar.h>
45 
46 #ifdef SDMMC_IOCTL
47 #include "bio.h"
48 #if NBIO < 1
49 #undef SDMMC_IOCTL
50 #endif
51 #include <dev/biovar.h>
52 #endif
53 
54 int	sdmmc_match(struct device *, void *, void *);
55 void	sdmmc_attach(struct device *, struct device *, void *);
56 int	sdmmc_detach(struct device *, int);
57 int	sdmmc_activate(struct device *, int);
58 
59 void	sdmmc_create_thread(void *);
60 void	sdmmc_task_thread(void *);
61 void	sdmmc_discover_task(void *);
62 void	sdmmc_card_attach(struct sdmmc_softc *);
63 void	sdmmc_card_detach(struct sdmmc_softc *, int);
64 int	sdmmc_enable(struct sdmmc_softc *);
65 void	sdmmc_disable(struct sdmmc_softc *);
66 int	sdmmc_scan(struct sdmmc_softc *);
67 int	sdmmc_init(struct sdmmc_softc *);
68 #ifdef SDMMC_IOCTL
69 int	sdmmc_ioctl(struct device *, u_long, caddr_t);
70 #endif
71 
72 #ifdef SDMMC_DEBUG
73 int sdmmcdebug = 0;
74 extern int sdhcdebug;	/* XXX should have a sdmmc_chip_debug() function */
75 void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *);
76 #define DPRINTF(n,s)	do { if ((n) <= sdmmcdebug) printf s; } while (0)
77 #else
78 #define DPRINTF(n,s)	do {} while (0)
79 #endif
80 
81 struct cfattach sdmmc_ca = {
82 	sizeof(struct sdmmc_softc), sdmmc_match, sdmmc_attach, sdmmc_detach,
83 	sdmmc_activate
84 };
85 
86 struct cfdriver sdmmc_cd = {
87 	NULL, "sdmmc", DV_DULL
88 };
89 
90 int
91 sdmmc_match(struct device *parent, void *match, void *aux)
92 {
93 	struct cfdata *cf = match;
94 	struct sdmmcbus_attach_args *saa = aux;
95 
96 	return strcmp(saa->saa_busname, cf->cf_driver->cd_name) == 0;
97 }
98 
99 void
100 sdmmc_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct sdmmc_softc *sc = (struct sdmmc_softc *)self;
103 	struct sdmmcbus_attach_args *saa = aux;
104 	int error;
105 
106 	if (ISSET(saa->caps, SMC_CAPS_8BIT_MODE))
107 		printf(": 8-bit");
108 	else if (ISSET(saa->caps, SMC_CAPS_4BIT_MODE))
109 		printf(": 4-bit");
110 	else
111 		printf(": 1-bit");
112 	if (ISSET(saa->caps, SMC_CAPS_SD_HIGHSPEED))
113 		printf(", sd high-speed");
114 	if (ISSET(saa->caps, SMC_CAPS_UHS_SDR50))
115 		printf(", sdr50");
116 	if (ISSET(saa->caps, SMC_CAPS_UHS_SDR104))
117 		printf(", sdr104");
118 	if (ISSET(saa->caps, SMC_CAPS_MMC_HIGHSPEED))
119 		printf(", mmc high-speed");
120 	if (ISSET(saa->caps, SMC_CAPS_MMC_DDR52))
121 		printf(", ddr52");
122 	if (ISSET(saa->caps, SMC_CAPS_MMC_HS200))
123 		printf(", hs200");
124 	if (ISSET(saa->caps, SMC_CAPS_DMA))
125 		printf(", dma");
126 	printf("\n");
127 
128 	sc->sct = saa->sct;
129 	sc->sch = saa->sch;
130 	sc->sc_dmat = saa->dmat;
131 	sc->sc_dmap = saa->dmap;
132 	sc->sc_flags = saa->flags;
133 	sc->sc_caps = saa->caps;
134 	sc->sc_max_seg = saa->max_seg ? saa->max_seg : MAXPHYS;
135 	sc->sc_max_xfer = saa->max_xfer;
136 	memcpy(&sc->sc_cookies, &saa->cookies, sizeof(sc->sc_cookies));
137 
138 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA) && sc->sc_dmap == NULL) {
139 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SDMMC_MAXNSEGS,
140 		    sc->sc_max_seg, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
141 		    &sc->sc_dmap);
142 		if (error) {
143 			printf("%s: can't create DMA map\n", DEVNAME(sc));
144 			return;
145 		}
146 	}
147 
148 	SIMPLEQ_INIT(&sc->sf_head);
149 	TAILQ_INIT(&sc->sc_tskq);
150 	TAILQ_INIT(&sc->sc_intrq);
151 	sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc);
152 	sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc);
153 	rw_init(&sc->sc_lock, DEVNAME(sc));
154 
155 #ifdef SDMMC_IOCTL
156 	if (bio_register(self, sdmmc_ioctl) != 0)
157 		printf("%s: unable to register ioctl\n", DEVNAME(sc));
158 #endif
159 
160 	/*
161 	 * Create the event thread that will attach and detach cards
162 	 * and perform other lengthy operations.  Enter config_pending
163 	 * state until the discovery task has run for the first time.
164 	 */
165 	SET(sc->sc_flags, SMF_CONFIG_PENDING);
166 	config_pending_incr();
167 	kthread_create_deferred(sdmmc_create_thread, sc);
168 }
169 
170 int
171 sdmmc_detach(struct device *self, int flags)
172 {
173 	struct sdmmc_softc *sc = (struct sdmmc_softc *)self;
174 
175 	sc->sc_dying = 1;
176 	while (sc->sc_task_thread != NULL) {
177 		wakeup(&sc->sc_tskq);
178 		tsleep_nsec(sc, PWAIT, "mmcdie", INFSLP);
179 	}
180 
181 	if (sc->sc_dmap)
182 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmap);
183 
184 	return 0;
185 }
186 
187 int
188 sdmmc_activate(struct device *self, int act)
189 {
190 	struct sdmmc_softc *sc = (struct sdmmc_softc *)self;
191 	int rv = 0;
192 
193 	switch (act) {
194 	case DVACT_SUSPEND:
195 		rv = config_activate_children(self, act);
196 		/* If card in slot, cause a detach/re-attach */
197 		if (ISSET(sc->sc_flags, SMF_CARD_PRESENT) &&
198 		    !ISSET(sc->sc_caps, SMC_CAPS_NONREMOVABLE))
199 			sc->sc_dying = -1;
200 		break;
201 	case DVACT_RESUME:
202 		rv = config_activate_children(self, act);
203 		wakeup(&sc->sc_tskq);
204 		break;
205 	default:
206 		rv = config_activate_children(self, act);
207 		break;
208 	}
209 	return (rv);
210 }
211 
212 void
213 sdmmc_create_thread(void *arg)
214 {
215 	struct sdmmc_softc *sc = arg;
216 
217 	if (kthread_create(sdmmc_task_thread, sc, &sc->sc_task_thread,
218 	    DEVNAME(sc)) != 0)
219 		printf("%s: can't create task thread\n", DEVNAME(sc));
220 
221 }
222 
223 void
224 sdmmc_task_thread(void *arg)
225 {
226 	struct sdmmc_softc *sc = arg;
227 	struct sdmmc_task *task;
228 	int s;
229 
230 restart:
231 	sdmmc_needs_discover(&sc->sc_dev);
232 
233 	s = splsdmmc();
234 	while (!sc->sc_dying) {
235 		for (task = TAILQ_FIRST(&sc->sc_tskq); task != NULL;
236 		     task = TAILQ_FIRST(&sc->sc_tskq)) {
237 			splx(s);
238 			sdmmc_del_task(task);
239 			task->func(task->arg);
240 			s = splsdmmc();
241 		}
242 		tsleep_nsec(&sc->sc_tskq, PWAIT, "mmctsk", INFSLP);
243 	}
244 	splx(s);
245 
246 	if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
247 		rw_enter_write(&sc->sc_lock);
248 		sdmmc_card_detach(sc, DETACH_FORCE);
249 		rw_exit(&sc->sc_lock);
250 	}
251 
252 	/*
253 	 * During a suspend, the card is detached since we do not know
254 	 * if it is the same upon wakeup.  Go re-discover the bus.
255 	 */
256 	if (sc->sc_dying == -1) {
257 		CLR(sc->sc_flags, SMF_CARD_PRESENT);
258 		sc->sc_dying = 0;
259 		goto restart;
260 	}
261 	sc->sc_task_thread = NULL;
262 	wakeup(sc);
263 	kthread_exit(0);
264 }
265 
266 void
267 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
268 {
269 	int s;
270 
271 	s = splsdmmc();
272 	TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
273 	task->onqueue = 1;
274 	task->sc = sc;
275 	wakeup(&sc->sc_tskq);
276 	splx(s);
277 }
278 
279 void
280 sdmmc_del_task(struct sdmmc_task *task)
281 {
282 	struct sdmmc_softc *sc = task->sc;
283 	int s;
284 
285 	if (sc == NULL)
286 		return;
287 
288 	s = splsdmmc();
289 	task->sc = NULL;
290 	task->onqueue = 0;
291 	TAILQ_REMOVE(&sc->sc_tskq, task, next);
292 	splx(s);
293 }
294 
295 void
296 sdmmc_needs_discover(struct device *self)
297 {
298 	struct sdmmc_softc *sc = (struct sdmmc_softc *)self;
299 
300 	if (!sdmmc_task_pending(&sc->sc_discover_task))
301 		sdmmc_add_task(sc, &sc->sc_discover_task);
302 }
303 
304 void
305 sdmmc_discover_task(void *arg)
306 {
307 	struct sdmmc_softc *sc = arg;
308 
309 	if (sdmmc_chip_card_detect(sc->sct, sc->sch)) {
310 		if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
311 			SET(sc->sc_flags, SMF_CARD_PRESENT);
312 			sdmmc_card_attach(sc);
313 		}
314 	} else {
315 		if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
316 			CLR(sc->sc_flags, SMF_CARD_PRESENT);
317 			rw_enter_write(&sc->sc_lock);
318 			sdmmc_card_detach(sc, DETACH_FORCE);
319 			rw_exit(&sc->sc_lock);
320 		}
321 	}
322 
323 	if (ISSET(sc->sc_flags, SMF_CONFIG_PENDING)) {
324 		CLR(sc->sc_flags, SMF_CONFIG_PENDING);
325 		config_pending_decr();
326 	}
327 }
328 
329 /*
330  * Called from process context when a card is present.
331  */
332 void
333 sdmmc_card_attach(struct sdmmc_softc *sc)
334 {
335 	DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
336 
337 	rw_enter_write(&sc->sc_lock);
338 	CLR(sc->sc_flags, SMF_CARD_ATTACHED);
339 
340 	/*
341 	 * Power up the card (or card stack).
342 	 */
343 	if (sdmmc_enable(sc) != 0) {
344 		printf("%s: can't enable card\n", DEVNAME(sc));
345 		goto err;
346 	}
347 
348 	/*
349 	 * Scan for I/O functions and memory cards on the bus,
350 	 * allocating a sdmmc_function structure for each.
351 	 */
352 	if (sdmmc_scan(sc) != 0) {
353 		printf("%s: no functions\n", DEVNAME(sc));
354 		goto err;
355 	}
356 
357 	/*
358 	 * Initialize the I/O functions and memory cards.
359 	 */
360 	if (sdmmc_init(sc) != 0) {
361 		printf("%s: init failed\n", DEVNAME(sc));
362 		goto err;
363 	}
364 
365 	/* Attach SCSI emulation for memory cards. */
366 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
367 		sdmmc_scsi_attach(sc);
368 
369 	/* Attach I/O function drivers. */
370 	if (ISSET(sc->sc_flags, SMF_IO_MODE))
371 		sdmmc_io_attach(sc);
372 
373 	SET(sc->sc_flags, SMF_CARD_ATTACHED);
374 	rw_exit(&sc->sc_lock);
375 	return;
376 err:
377 	sdmmc_card_detach(sc, DETACH_FORCE);
378 	rw_exit(&sc->sc_lock);
379 }
380 
381 /*
382  * Called from process context with DETACH_* flags from <sys/device.h>
383  * when cards are gone.
384  */
385 void
386 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
387 {
388 	struct sdmmc_function *sf, *sfnext;
389 
390 	rw_assert_wrlock(&sc->sc_lock);
391 
392 	DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
393 
394 	if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
395 		/* Detach I/O function drivers. */
396 		if (ISSET(sc->sc_flags, SMF_IO_MODE))
397 			sdmmc_io_detach(sc);
398 
399 		/* Detach the SCSI emulation for memory cards. */
400 		if (ISSET(sc->sc_flags, SMF_MEM_MODE))
401 			sdmmc_scsi_detach(sc);
402 
403 		CLR(sc->sc_flags, SMF_CARD_ATTACHED);
404 	}
405 
406 	/* Power down. */
407 	sdmmc_disable(sc);
408 
409 	/* Free all sdmmc_function structures. */
410 	for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
411 		sfnext = SIMPLEQ_NEXT(sf, sf_list);
412 		sdmmc_function_free(sf);
413 	}
414 	SIMPLEQ_INIT(&sc->sf_head);
415 	sc->sc_function_count = 0;
416 	sc->sc_fn0 = NULL;
417 }
418 
419 int
420 sdmmc_enable(struct sdmmc_softc *sc)
421 {
422 	u_int32_t host_ocr;
423 	int error;
424 
425 	rw_assert_wrlock(&sc->sc_lock);
426 
427 	/*
428 	 * Calculate the equivalent of the card OCR from the host
429 	 * capabilities and select the maximum supported bus voltage.
430 	 */
431 	host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch);
432 	error = sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr);
433 	if (error != 0) {
434 		printf("%s: can't supply bus power\n", DEVNAME(sc));
435 		goto err;
436 	}
437 
438 	/*
439 	 * Select the minimum clock frequency.
440 	 */
441 	error = sdmmc_chip_bus_clock(sc->sct, sc->sch,
442 	    SDMMC_SDCLK_400KHZ, SDMMC_TIMING_LEGACY);
443 	if (error != 0) {
444 		printf("%s: can't supply clock\n", DEVNAME(sc));
445 		goto err;
446 	}
447 
448 	/* XXX wait for card to power up */
449 	sdmmc_delay(250000);
450 
451 	/* Initialize SD I/O card function(s). */
452 	if ((error = sdmmc_io_enable(sc)) != 0)
453 		goto err;
454 
455 	/* Initialize SD/MMC memory card(s). */
456 	if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
457 	    (error = sdmmc_mem_enable(sc)) != 0)
458 		goto err;
459 
460  err:
461 	if (error != 0)
462 		sdmmc_disable(sc);
463 
464 	return error;
465 }
466 
467 void
468 sdmmc_disable(struct sdmmc_softc *sc)
469 {
470 	/* XXX complete commands if card is still present. */
471 
472 	rw_assert_wrlock(&sc->sc_lock);
473 
474 	/* Make sure no card is still selected. */
475 	(void)sdmmc_select_card(sc, NULL);
476 
477 	/* Turn off bus power and clock. */
478 	(void)sdmmc_chip_bus_clock(sc->sct, sc->sch,
479 	    SDMMC_SDCLK_OFF, SDMMC_TIMING_LEGACY);
480 	(void)sdmmc_chip_bus_power(sc->sct, sc->sch, 0);
481 }
482 
483 /*
484  * Set the lowest bus voltage supported by the card and the host.
485  */
486 int
487 sdmmc_set_bus_power(struct sdmmc_softc *sc, u_int32_t host_ocr,
488     u_int32_t card_ocr)
489 {
490 	u_int32_t bit;
491 
492 	rw_assert_wrlock(&sc->sc_lock);
493 
494 	/* Mask off unsupported voltage levels and select the lowest. */
495 	DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
496 	host_ocr &= card_ocr;
497 	for (bit = 4; bit < 23; bit++) {
498 		if (ISSET(host_ocr, 1<<bit)) {
499 			host_ocr &= 3<<bit;
500 			break;
501 		}
502 	}
503 	DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
504 
505 	if (host_ocr == 0 ||
506 	    sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr) != 0)
507 		return 1;
508 	return 0;
509 }
510 
511 struct sdmmc_function *
512 sdmmc_function_alloc(struct sdmmc_softc *sc)
513 {
514 	struct sdmmc_function *sf;
515 
516 	sf = (struct sdmmc_function *)malloc(sizeof *sf, M_DEVBUF,
517 	    M_WAITOK | M_ZERO);
518 	sf->sc = sc;
519 	sf->number = -1;
520 	sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
521 	sf->cis.product = SDMMC_PRODUCT_INVALID;
522 	sf->cis.function = SDMMC_FUNCTION_INVALID;
523 	sf->cur_blklen = sdmmc_chip_host_maxblklen(sc->sct, sc->sch);
524 	return sf;
525 }
526 
527 void
528 sdmmc_function_free(struct sdmmc_function *sf)
529 {
530 	free(sf, M_DEVBUF, sizeof *sf);
531 }
532 
533 /*
534  * Scan for I/O functions and memory cards on the bus, allocating a
535  * sdmmc_function structure for each.
536  */
537 int
538 sdmmc_scan(struct sdmmc_softc *sc)
539 {
540 
541 	rw_assert_wrlock(&sc->sc_lock);
542 
543 	/* Scan for I/O functions. */
544 	if (ISSET(sc->sc_flags, SMF_IO_MODE))
545 		sdmmc_io_scan(sc);
546 
547 	/* Scan for memory cards on the bus. */
548 	if (ISSET(sc->sc_flags, SMF_MEM_MODE))
549 		sdmmc_mem_scan(sc);
550 
551 	/* There should be at least one function now. */
552 	if (SIMPLEQ_EMPTY(&sc->sf_head)) {
553 		printf("%s: can't identify card\n", DEVNAME(sc));
554 		return 1;
555 	}
556 	return 0;
557 }
558 
559 /*
560  * Initialize all the distinguished functions of the card, be it I/O
561  * or memory functions.
562  */
563 int
564 sdmmc_init(struct sdmmc_softc *sc)
565 {
566 	struct sdmmc_function *sf;
567 
568 	rw_assert_wrlock(&sc->sc_lock);
569 
570 	/* Initialize all identified card functions. */
571 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
572 		if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
573 		    sdmmc_io_init(sc, sf) != 0)
574 			printf("%s: i/o init failed\n", DEVNAME(sc));
575 
576 		if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
577 		    sdmmc_mem_init(sc, sf) != 0)
578 			printf("%s: mem init failed\n", DEVNAME(sc));
579 	}
580 
581 	/* Any good functions left after initialization? */
582 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
583 		if (!ISSET(sf->flags, SFF_ERROR))
584 			return 0;
585 	}
586 	/* No, we should probably power down the card. */
587 	return 1;
588 }
589 
590 void
591 sdmmc_delay(u_int usecs)
592 {
593 	if (!cold && usecs > tick)
594 		tsleep_nsec(&sdmmc_delay, PWAIT, "mmcdly", USEC_TO_NSEC(usecs));
595 	else
596 		delay(usecs);
597 }
598 
599 int
600 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
601 {
602 	struct sdmmc_command acmd;
603 	int error;
604 
605 	rw_assert_wrlock(&sc->sc_lock);
606 
607 	bzero(&acmd, sizeof acmd);
608 	acmd.c_opcode = MMC_APP_CMD;
609 	acmd.c_arg = 0;
610 	if (sc->sc_card != NULL) {
611 		acmd.c_arg = sc->sc_card->rca << 16;
612 	}
613 	acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
614 
615 	error = sdmmc_mmc_command(sc, &acmd);
616 	if (error != 0) {
617 		return error;
618 	}
619 
620 	if (!ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
621 		/* Card does not support application commands. */
622 		return ENODEV;
623 	}
624 
625 	error = sdmmc_mmc_command(sc, cmd);
626 	return error;
627 }
628 
629 /*
630  * Execute MMC command and data transfers.  All interactions with the
631  * host controller to complete the command happen in the context of
632  * the current process.
633  */
634 int
635 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
636 {
637 	int error;
638 
639 	rw_assert_wrlock(&sc->sc_lock);
640 
641 	sdmmc_chip_exec_command(sc->sct, sc->sch, cmd);
642 
643 #ifdef SDMMC_DEBUG
644 	sdmmc_dump_command(sc, cmd);
645 #endif
646 
647 	error = cmd->c_error;
648 	if (!cold)
649 		wakeup(cmd);
650 
651 	return error;
652 }
653 
654 /*
655  * Send the "GO IDLE STATE" command.
656  */
657 void
658 sdmmc_go_idle_state(struct sdmmc_softc *sc)
659 {
660 	struct sdmmc_command cmd;
661 
662 	rw_assert_wrlock(&sc->sc_lock);
663 
664 	bzero(&cmd, sizeof cmd);
665 	cmd.c_opcode = MMC_GO_IDLE_STATE;
666 	cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0;
667 
668 	(void)sdmmc_mmc_command(sc, &cmd);
669 }
670 
671 /*
672  * Send the "SEND_IF_COND" command, to check operating condition
673  */
674 int
675 sdmmc_send_if_cond(struct sdmmc_softc *sc, uint32_t card_ocr)
676 {
677 	struct sdmmc_command cmd;
678 	uint8_t pat = 0x23;	/* any pattern will do here */
679 	uint8_t res;
680 
681 	rw_assert_wrlock(&sc->sc_lock);
682 
683 	bzero(&cmd, sizeof cmd);
684 
685 	cmd.c_opcode = SD_SEND_IF_COND;
686 	cmd.c_arg = ((card_ocr & SD_OCR_VOL_MASK) != 0) << 8 | pat;
687 	cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7;
688 
689 	if (sdmmc_mmc_command(sc, &cmd) != 0)
690 		return 1;
691 
692 	res = cmd.c_resp[0];
693 	if (res != pat)
694 		return 1;
695 	else
696 		return 0;
697 }
698 
699 /*
700  * Retrieve (SD) or set (MMC) the relative card address (RCA).
701  */
702 int
703 sdmmc_set_relative_addr(struct sdmmc_softc *sc,
704     struct sdmmc_function *sf)
705 {
706 	struct sdmmc_command cmd;
707 
708 	rw_assert_wrlock(&sc->sc_lock);
709 
710 	bzero(&cmd, sizeof cmd);
711 
712 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
713 		cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
714 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
715 	} else {
716 		cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
717 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
718 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
719 	}
720 
721 	if (sdmmc_mmc_command(sc, &cmd) != 0)
722 		return 1;
723 
724 	if (ISSET(sc->sc_flags, SMF_SD_MODE))
725 		sf->rca = SD_R6_RCA(cmd.c_resp);
726 	return 0;
727 }
728 
729 int
730 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
731 {
732 	struct sdmmc_command cmd;
733 	int error;
734 
735 	rw_assert_wrlock(&sc->sc_lock);
736 
737 	if (sc->sc_card == sf || (sf && sc->sc_card &&
738 	    sc->sc_card->rca == sf->rca)) {
739 		sc->sc_card = sf;
740 		return 0;
741 	}
742 
743 	bzero(&cmd, sizeof cmd);
744 	cmd.c_opcode = MMC_SELECT_CARD;
745 	cmd.c_arg = sf == NULL ? 0 : MMC_ARG_RCA(sf->rca);
746 	cmd.c_flags = SCF_CMD_AC | (sf == NULL ? SCF_RSP_R0 : SCF_RSP_R1);
747 	error = sdmmc_mmc_command(sc, &cmd);
748 	if (error == 0 || sf == NULL)
749 		sc->sc_card = sf;
750 	return error;
751 }
752 
753 #ifdef SDMMC_IOCTL
754 int
755 sdmmc_ioctl(struct device *self, u_long request, caddr_t addr)
756 {
757 	struct sdmmc_softc *sc = (struct sdmmc_softc *)self;
758 	struct sdmmc_command *ucmd;
759 	struct sdmmc_command cmd;
760 	void *data;
761 	int error = 0;
762 
763 	switch (request) {
764 #ifdef SDMMC_DEBUG
765 	case SDIOCSETDEBUG:
766 		sdmmcdebug = (((struct bio_sdmmc_debug *)addr)->debug) & 0xff;
767 		sdhcdebug = (((struct bio_sdmmc_debug *)addr)->debug >> 8) & 0xff;
768 		break;
769 #endif
770 
771 	case SDIOCEXECMMC:
772 	case SDIOCEXECAPP:
773 		ucmd = &((struct bio_sdmmc_command *)addr)->cmd;
774 
775 		/* Refuse to transfer more than 512K per command. */
776 		if (ucmd->c_datalen > 524288)
777 			return ENOMEM;
778 
779 		/* Verify that the data buffer is safe to copy. */
780 		if ((ucmd->c_datalen > 0 && ucmd->c_data == NULL) ||
781 		    (ucmd->c_datalen < 1 && ucmd->c_data != NULL) ||
782 		    ucmd->c_datalen < 0)
783 			return EINVAL;
784 
785 		bzero(&cmd, sizeof cmd);
786 		cmd.c_opcode = ucmd->c_opcode;
787 		cmd.c_arg = ucmd->c_arg;
788 		cmd.c_flags = ucmd->c_flags;
789 		cmd.c_blklen = ucmd->c_blklen;
790 
791 		if (ucmd->c_data) {
792 			data = malloc(ucmd->c_datalen, M_TEMP,
793 			    M_WAITOK | M_CANFAIL);
794 			if (data == NULL)
795 				return ENOMEM;
796 			error = copyin(ucmd->c_data, data, ucmd->c_datalen);
797 			if (error != 0)
798 				goto exec_done;
799 
800 			cmd.c_data = data;
801 			cmd.c_datalen = ucmd->c_datalen;
802 		}
803 
804 		rw_enter_write(&sc->sc_lock);
805 		if (request == SDIOCEXECMMC)
806 			error = sdmmc_mmc_command(sc, &cmd);
807 		else
808 			error = sdmmc_app_command(sc, &cmd);
809 		rw_exit(&sc->sc_lock);
810 		if (error && !cmd.c_error)
811 			cmd.c_error = error;
812 
813 		bcopy(&cmd.c_resp, ucmd->c_resp, sizeof cmd.c_resp);
814 		ucmd->c_flags = cmd.c_flags;
815 		ucmd->c_error = cmd.c_error;
816 
817 		if (ucmd->c_data)
818 			error = copyout(data, ucmd->c_data, ucmd->c_datalen);
819 		else
820 			error = 0;
821 
822 exec_done:
823 		if (ucmd->c_data)
824 			free(data, M_TEMP, ucmd->c_datalen);
825 		break;
826 
827 	default:
828 		return ENOTTY;
829 	}
830 	return error;
831 }
832 #endif
833 
834 #ifdef SDMMC_DEBUG
835 void
836 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
837 {
838 	int i;
839 
840 	rw_assert_wrlock(&sc->sc_lock);
841 
842 	DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x "
843 	    "proc=\"%s\" (error %d)\n", DEVNAME(sc), cmd->c_opcode,
844 	    cmd->c_arg, cmd->c_data, cmd->c_datalen, cmd->c_flags,
845 	    curproc ? curproc->p_p->ps_comm : "", cmd->c_error));
846 
847 	if (cmd->c_error || sdmmcdebug < 1)
848 		return;
849 
850 	printf("%s: resp=", DEVNAME(sc));
851 	if (ISSET(cmd->c_flags, SCF_RSP_136))
852 		for (i = 0; i < sizeof cmd->c_resp; i++)
853 			printf("%02x ", ((u_char *)cmd->c_resp)[i]);
854 	else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
855 		for (i = 0; i < 4; i++)
856 			printf("%02x ", ((u_char *)cmd->c_resp)[i]);
857 	printf("\n");
858 }
859 #endif
860