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