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