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