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