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