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