sdmmc.c revision 1.12 1 /* $NetBSD: sdmmc.c,v 1.12 2012/02/01 22:34:42 matt Exp $ */
2 /* $OpenBSD: sdmmc.c,v 1.18 2009/01/09 10:58:38 jsg Exp $ */
3
4 /*
5 * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*-
21 * Copyright (C) 2007, 2008, 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 /*
46 * Host controller independent SD/MMC bus driver based on information
47 * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO
48 * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver.
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: sdmmc.c,v 1.12 2012/02/01 22:34:42 matt Exp $");
53
54 #ifdef _KERNEL_OPT
55 #include "opt_sdmmc.h"
56 #endif
57
58 #include <sys/param.h>
59 #include <sys/device.h>
60 #include <sys/kernel.h>
61 #include <sys/kthread.h>
62 #include <sys/malloc.h>
63 #include <sys/proc.h>
64 #include <sys/systm.h>
65 #include <sys/callout.h>
66
67 #include <machine/vmparam.h>
68
69 #include <dev/sdmmc/sdmmc_ioreg.h>
70 #include <dev/sdmmc/sdmmcchip.h>
71 #include <dev/sdmmc/sdmmcreg.h>
72 #include <dev/sdmmc/sdmmcvar.h>
73
74 #ifdef SDMMC_DEBUG
75 int sdmmcdebug = 1;
76 static void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *);
77 #define DPRINTF(n,s) do { if ((n) <= sdmmcdebug) printf s; } while (0)
78 #else
79 #define DPRINTF(n,s) do {} while (0)
80 #endif
81
82 #define DEVNAME(sc) SDMMCDEVNAME(sc)
83
84 static int sdmmc_match(device_t, cfdata_t, void *);
85 static void sdmmc_attach(device_t, device_t, void *);
86 static int sdmmc_detach(device_t, int);
87
88 CFATTACH_DECL_NEW(sdmmc, sizeof(struct sdmmc_softc),
89 sdmmc_match, sdmmc_attach, sdmmc_detach, NULL);
90
91 static void sdmmc_doattach(device_t);
92 static void sdmmc_task_thread(void *);
93 static void sdmmc_discover_task(void *);
94 static void sdmmc_polling_card(void *);
95 static void sdmmc_card_attach(struct sdmmc_softc *);
96 static void sdmmc_card_detach(struct sdmmc_softc *, int);
97 static int sdmmc_print(void *, const char *);
98 static int sdmmc_enable(struct sdmmc_softc *);
99 static void sdmmc_disable(struct sdmmc_softc *);
100 static int sdmmc_scan(struct sdmmc_softc *);
101 static int sdmmc_init(struct sdmmc_softc *);
102
103 static int
104 sdmmc_match(device_t parent, cfdata_t cf, void *aux)
105 {
106 struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
107
108 if (strcmp(saa->saa_busname, cf->cf_name) == 0)
109 return 1;
110 return 0;
111 }
112
113 static void
114 sdmmc_attach(device_t parent, device_t self, void *aux)
115 {
116 struct sdmmc_softc *sc = device_private(self);
117 struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
118 int error;
119
120 aprint_normal("\n");
121 aprint_naive("\n");
122
123 sc->sc_dev = self;
124 sc->sc_sct = saa->saa_sct;
125 sc->sc_spi_sct = saa->saa_spi_sct;
126 sc->sc_sch = saa->saa_sch;
127 sc->sc_dmat = saa->saa_dmat;
128 sc->sc_clkmin = saa->saa_clkmin;
129 sc->sc_clkmax = saa->saa_clkmax;
130 sc->sc_busclk = sc->sc_clkmax;
131 sc->sc_buswidth = 1;
132 sc->sc_caps = saa->saa_caps;
133
134 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
135 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SDMMC_MAXNSEGS,
136 MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->sc_dmap);
137 if (error) {
138 aprint_error_dev(sc->sc_dev,
139 "couldn't create dma map. (error=%d)\n", error);
140 return;
141 }
142 }
143
144 if (ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
145 callout_init(&sc->sc_card_detect_ch, 0);
146 callout_reset(&sc->sc_card_detect_ch, hz,
147 sdmmc_polling_card, sc);
148 }
149
150 SIMPLEQ_INIT(&sc->sf_head);
151 TAILQ_INIT(&sc->sc_tskq);
152 TAILQ_INIT(&sc->sc_intrq);
153
154 sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc);
155 sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc);
156
157 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_SDMMC);
158 mutex_init(&sc->sc_tskq_mtx, MUTEX_DEFAULT, IPL_SDMMC);
159 mutex_init(&sc->sc_discover_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
160 mutex_init(&sc->sc_intr_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
161 cv_init(&sc->sc_tskq_cv, "mmctaskq");
162
163 if (!pmf_device_register(self, NULL, NULL)) {
164 aprint_error_dev(self, "couldn't establish power handler\n");
165 }
166
167 SET(sc->sc_flags, SMF_INITED);
168
169 /*
170 * Create the event thread that will attach and detach cards
171 * and perform other lengthy operations.
172 */
173 config_pending_incr();
174 config_interrupts(self, sdmmc_doattach);
175 }
176
177 static int
178 sdmmc_detach(device_t self, int flags)
179 {
180 struct sdmmc_softc *sc = device_private(self);
181 int error;
182
183 mutex_enter(&sc->sc_tskq_mtx);
184 sc->sc_dying = 1;
185 cv_signal(&sc->sc_tskq_cv);
186 while (sc->sc_tskq_lwp != NULL)
187 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
188 mutex_exit(&sc->sc_tskq_mtx);
189
190 pmf_device_deregister(self);
191
192 error = config_detach_children(self, flags);
193 if (error)
194 return error;
195 return 0;
196 }
197
198 static void
199 sdmmc_doattach(device_t dev)
200 {
201 struct sdmmc_softc *sc = device_private(dev);
202
203 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
204 sdmmc_task_thread, sc, &sc->sc_tskq_lwp, "%s", device_xname(dev))) {
205 aprint_error_dev(dev, "couldn't create task thread\n");
206 }
207 }
208
209 void
210 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
211 {
212
213 mutex_enter(&sc->sc_tskq_mtx);
214 task->onqueue = 1;
215 task->sc = sc;
216 TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
217 cv_broadcast(&sc->sc_tskq_cv);
218 mutex_exit(&sc->sc_tskq_mtx);
219 }
220
221 static inline void
222 sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
223 {
224
225 TAILQ_REMOVE(&sc->sc_tskq, task, next);
226 task->sc = NULL;
227 task->onqueue = 0;
228 }
229
230 void
231 sdmmc_del_task(struct sdmmc_task *task)
232 {
233 struct sdmmc_softc *sc = (struct sdmmc_softc *)task->sc;
234
235 if (sc != NULL) {
236 mutex_enter(&sc->sc_tskq_mtx);
237 sdmmc_del_task1(sc, task);
238 mutex_exit(&sc->sc_tskq_mtx);
239 }
240 }
241
242 static void
243 sdmmc_task_thread(void *arg)
244 {
245 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
246 struct sdmmc_task *task;
247
248 sdmmc_discover_task(sc);
249 config_pending_decr();
250
251 mutex_enter(&sc->sc_tskq_mtx);
252 for (;;) {
253 task = TAILQ_FIRST(&sc->sc_tskq);
254 if (task != NULL) {
255 sdmmc_del_task1(sc, task);
256 mutex_exit(&sc->sc_tskq_mtx);
257 (*task->func)(task->arg);
258 mutex_enter(&sc->sc_tskq_mtx);
259 } else {
260 /* Check for the exit condition. */
261 if (sc->sc_dying)
262 break;
263 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
264 }
265 }
266 /* time to die. */
267 sc->sc_dying = 0;
268 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT))
269 sdmmc_card_detach(sc, DETACH_FORCE);
270 sc->sc_tskq_lwp = NULL;
271 cv_broadcast(&sc->sc_tskq_cv);
272 mutex_exit(&sc->sc_tskq_mtx);
273 kthread_exit(0);
274 }
275
276 void
277 sdmmc_needs_discover(device_t dev)
278 {
279 struct sdmmc_softc *sc = device_private(dev);
280
281 if (!ISSET(sc->sc_flags, SMF_INITED))
282 return;
283
284 mutex_enter(&sc->sc_discover_task_mtx);
285 if (!sdmmc_task_pending(&sc->sc_discover_task))
286 sdmmc_add_task(sc, &sc->sc_discover_task);
287 mutex_exit(&sc->sc_discover_task_mtx);
288 }
289
290 static void
291 sdmmc_discover_task(void *arg)
292 {
293 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
294
295 if (sdmmc_chip_card_detect(sc->sc_sct, sc->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 if (!ISSET(sc->sc_flags, SMF_CARD_ATTACHED))
300 CLR(sc->sc_flags, SMF_CARD_PRESENT);
301 }
302 } else {
303 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
304 CLR(sc->sc_flags, SMF_CARD_PRESENT);
305 sdmmc_card_detach(sc, DETACH_FORCE);
306 }
307 }
308 }
309
310 static void
311 sdmmc_polling_card(void *arg)
312 {
313 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
314 int card_detect;
315 int s;
316
317 s = splsdmmc();
318 card_detect = sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch);
319 if (card_detect) {
320 if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
321 sdmmc_needs_discover(sc->sc_dev);
322 }
323 } else {
324 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
325 sdmmc_needs_discover(sc->sc_dev);
326 }
327 }
328 splx(s);
329
330 callout_schedule(&sc->sc_card_detect_ch, hz);
331 }
332
333 /*
334 * Called from process context when a card is present.
335 */
336 static void
337 sdmmc_card_attach(struct sdmmc_softc *sc)
338 {
339 struct sdmmc_function *sf;
340 struct sdmmc_attach_args saa;
341 int error;
342
343 DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
344
345 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
346
347 /*
348 * Power up the card (or card stack).
349 */
350 error = sdmmc_enable(sc);
351 if (error) {
352 if (!ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
353 aprint_error_dev(sc->sc_dev, "couldn't enable card: %d\n", error);
354 }
355 goto err;
356 }
357
358 /*
359 * Scan for I/O functions and memory cards on the bus,
360 * allocating a sdmmc_function structure for each.
361 */
362 error = sdmmc_scan(sc);
363 if (error) {
364 aprint_error_dev(sc->sc_dev, "no functions\n");
365 goto err;
366 }
367
368 /*
369 * Initialize the I/O functions and memory cards.
370 */
371 error = sdmmc_init(sc);
372 if (error) {
373 aprint_error_dev(sc->sc_dev, "init failed\n");
374 goto err;
375 }
376
377 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
378 if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
379 continue;
380
381 memset(&saa, 0, sizeof saa);
382 saa.manufacturer = sf->cis.manufacturer;
383 saa.product = sf->cis.product;
384 saa.interface = sf->interface;
385 saa.sf = sf;
386
387 sf->child =
388 config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
389 }
390
391 SET(sc->sc_flags, SMF_CARD_ATTACHED);
392 return;
393
394 err:
395 sdmmc_card_detach(sc, DETACH_FORCE);
396 }
397
398 /*
399 * Called from process context with DETACH_* flags from <sys/device.h>
400 * when cards are gone.
401 */
402 static void
403 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
404 {
405 struct sdmmc_function *sf, *sfnext;
406
407 DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
408
409 if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
410 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
411 if (sf->child != NULL) {
412 config_detach(sf->child, DETACH_FORCE);
413 sf->child = NULL;
414 }
415 }
416
417 KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
418
419 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
420 }
421
422 /* Power down. */
423 sdmmc_disable(sc);
424
425 /* Free all sdmmc_function structures. */
426 for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
427 sfnext = SIMPLEQ_NEXT(sf, sf_list);
428 sdmmc_function_free(sf);
429 }
430 SIMPLEQ_INIT(&sc->sf_head);
431 sc->sc_function_count = 0;
432 sc->sc_fn0 = NULL;
433 }
434
435 static int
436 sdmmc_print(void *aux, const char *pnp)
437 {
438 struct sdmmc_attach_args *sa = aux;
439 struct sdmmc_function *sf = sa->sf;
440 struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
441 int i;
442
443 if (pnp) {
444 if (sf->number == 0)
445 return QUIET;
446
447 for (i = 0; i < 4 && cis->cis1_info[i]; i++)
448 printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
449 if (i != 0)
450 printf("\"");
451
452 if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
453 cis->product != SDMMC_PRODUCT_INVALID) {
454 printf("%s(", i ? " " : "");
455 if (cis->manufacturer != SDMMC_VENDOR_INVALID)
456 printf("manufacturer 0x%x%s",
457 cis->manufacturer,
458 cis->product == SDMMC_PRODUCT_INVALID ?
459 "" : ", ");
460 if (cis->product != SDMMC_PRODUCT_INVALID)
461 printf("product 0x%x", cis->product);
462 printf(")");
463 }
464 printf("%sat %s", i ? " " : "", pnp);
465 }
466 if (sf->number > 0)
467 printf(" function %d", sf->number);
468
469 if (!pnp) {
470 for (i = 0; i < 3 && cis->cis1_info[i]; i++)
471 printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
472 if (i != 0)
473 printf("\"");
474 }
475 return UNCONF;
476 }
477
478 static int
479 sdmmc_enable(struct sdmmc_softc *sc)
480 {
481 int error;
482
483 /*
484 * Calculate the equivalent of the card OCR from the host
485 * capabilities and select the maximum supported bus voltage.
486 */
487 error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
488 sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
489 if (error) {
490 aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
491 goto out;
492 }
493
494 /*
495 * Select the minimum clock frequency.
496 */
497 error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
498 if (error) {
499 aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
500 goto out;
501 }
502
503 /* XXX wait for card to power up */
504 sdmmc_delay(100000);
505
506 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
507 /* Initialize SD I/O card function(s). */
508 error = sdmmc_io_enable(sc);
509 if (error)
510 goto out;
511 }
512
513 /* Initialize SD/MMC memory card(s). */
514 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
515 ISSET(sc->sc_flags, SMF_MEM_MODE))
516 error = sdmmc_mem_enable(sc);
517
518 out:
519 if (error)
520 sdmmc_disable(sc);
521 return error;
522 }
523
524 static void
525 sdmmc_disable(struct sdmmc_softc *sc)
526 {
527 /* XXX complete commands if card is still present. */
528
529 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
530 /* Make sure no card is still selected. */
531 (void)sdmmc_select_card(sc, NULL);
532 }
533
534 /* Turn off bus power and clock. */
535 (void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
536 (void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF);
537 (void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
538 sc->sc_busclk = sc->sc_clkmax;
539 }
540
541 /*
542 * Set the lowest bus voltage supported by the card and the host.
543 */
544 int
545 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
546 uint32_t card_ocr)
547 {
548 uint32_t bit;
549
550 /* Mask off unsupported voltage levels and select the lowest. */
551 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
552 host_ocr &= card_ocr;
553 for (bit = 4; bit < 23; bit++) {
554 if (ISSET(host_ocr, (1 << bit))) {
555 host_ocr &= (3 << bit);
556 break;
557 }
558 }
559 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
560
561 if (host_ocr == 0 ||
562 sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
563 return 1;
564 return 0;
565 }
566
567 struct sdmmc_function *
568 sdmmc_function_alloc(struct sdmmc_softc *sc)
569 {
570 struct sdmmc_function *sf;
571
572 sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
573 if (sf == NULL) {
574 aprint_error_dev(sc->sc_dev,
575 "couldn't alloc memory (sdmmc function)\n");
576 return NULL;
577 }
578
579 sf->sc = sc;
580 sf->number = -1;
581 sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
582 sf->cis.product = SDMMC_PRODUCT_INVALID;
583 sf->cis.function = SDMMC_FUNCTION_INVALID;
584 sf->width = 1;
585
586 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
587 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
588 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
589 bus_dma_segment_t ds;
590 int rseg, error;
591
592 error = bus_dmamap_create(sc->sc_dmat, SDMMC_SECTOR_SIZE, 1,
593 SDMMC_SECTOR_SIZE, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
594 if (error)
595 goto fail1;
596 error = bus_dmamem_alloc(sc->sc_dmat, SDMMC_SECTOR_SIZE,
597 PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
598 if (error)
599 goto fail2;
600 error = bus_dmamem_map(sc->sc_dmat, &ds, 1, SDMMC_SECTOR_SIZE,
601 &sf->bbuf, BUS_DMA_WAITOK);
602 if (error)
603 goto fail3;
604 error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
605 sf->bbuf, SDMMC_SECTOR_SIZE, NULL,
606 BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
607 if (!error)
608 goto out;
609
610 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
611 fail3:
612 bus_dmamem_free(sc->sc_dmat, &ds, 1);
613 fail2:
614 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
615 fail1:
616 free(sf, M_DEVBUF);
617 sf = NULL;
618 }
619 out:
620
621 return sf;
622 }
623
624 void
625 sdmmc_function_free(struct sdmmc_function *sf)
626 {
627 struct sdmmc_softc *sc = sf->sc;
628
629 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
630 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
631 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
632 bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
633 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
634 bus_dmamem_free(sc->sc_dmat,
635 sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
636 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
637 }
638
639 free(sf, M_DEVBUF);
640 }
641
642 /*
643 * Scan for I/O functions and memory cards on the bus, allocating a
644 * sdmmc_function structure for each.
645 */
646 static int
647 sdmmc_scan(struct sdmmc_softc *sc)
648 {
649
650 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
651 /* Scan for I/O functions. */
652 if (ISSET(sc->sc_flags, SMF_IO_MODE))
653 sdmmc_io_scan(sc);
654 }
655
656 /* Scan for memory cards on the bus. */
657 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
658 sdmmc_mem_scan(sc);
659
660 /* There should be at least one function now. */
661 if (SIMPLEQ_EMPTY(&sc->sf_head)) {
662 aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
663 return 1;
664 }
665 return 0;
666 }
667
668 /*
669 * Initialize all the distinguished functions of the card, be it I/O
670 * or memory functions.
671 */
672 static int
673 sdmmc_init(struct sdmmc_softc *sc)
674 {
675 struct sdmmc_function *sf;
676
677 /* Initialize all identified card functions. */
678 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
679 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
680 if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
681 sdmmc_io_init(sc, sf) != 0) {
682 aprint_error_dev(sc->sc_dev,
683 "i/o init failed\n");
684 }
685 }
686
687 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
688 sdmmc_mem_init(sc, sf) != 0) {
689 aprint_error_dev(sc->sc_dev, "mem init failed\n");
690 }
691 }
692
693 /* Any good functions left after initialization? */
694 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
695 if (!ISSET(sf->flags, SFF_ERROR))
696 return 0;
697 }
698
699 /* No, we should probably power down the card. */
700 return 1;
701 }
702
703 void
704 sdmmc_delay(u_int usecs)
705 {
706
707 delay(usecs);
708 }
709
710 int
711 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
712 {
713 struct sdmmc_command acmd;
714 int error;
715
716 DPRINTF(1,("sdmmc_app_command: start\n"));
717
718 /* Don't lock */
719
720 memset(&acmd, 0, sizeof(acmd));
721 acmd.c_opcode = MMC_APP_CMD;
722 if (sf != NULL) {
723 acmd.c_arg = sf->rca << 16;
724 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
725 } else {
726 acmd.c_arg = 0;
727 acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
728 }
729
730 error = sdmmc_mmc_command(sc, &acmd);
731 if (error == 0) {
732 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
733 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
734 /* Card does not support application commands. */
735 error = ENODEV;
736 } else {
737 error = sdmmc_mmc_command(sc, cmd);
738 }
739 }
740 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
741 return error;
742 }
743
744 /*
745 * Execute MMC command and data transfers. All interactions with the
746 * host controller to complete the command happen in the context of
747 * the current process.
748 */
749 int
750 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
751 {
752 int error;
753
754 DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
755 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
756
757 /* Don't lock */
758
759 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
760 if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
761 if (sc->sc_card == NULL)
762 panic("%s: deselected card\n", DEVNAME(sc));
763 }
764 #endif
765
766 sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
767
768 #ifdef SDMMC_DEBUG
769 sdmmc_dump_command(sc, cmd);
770 #endif
771
772 error = cmd->c_error;
773
774 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
775
776 return error;
777 }
778
779 /*
780 * Send the "GO IDLE STATE" command.
781 */
782 void
783 sdmmc_go_idle_state(struct sdmmc_softc *sc)
784 {
785 struct sdmmc_command cmd;
786
787 DPRINTF(1,("sdmmc_go_idle_state\n"));
788
789 /* Don't lock */
790
791 memset(&cmd, 0, sizeof(cmd));
792 cmd.c_opcode = MMC_GO_IDLE_STATE;
793 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
794
795 (void)sdmmc_mmc_command(sc, &cmd);
796 }
797
798 /*
799 * Retrieve (SD) or set (MMC) the relative card address (RCA).
800 */
801 int
802 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
803 {
804 struct sdmmc_command cmd;
805 int error;
806
807 /* Don't lock */
808
809 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
810 return EIO;
811
812 memset(&cmd, 0, sizeof(cmd));
813 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
814 cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
815 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
816 } else {
817 cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
818 cmd.c_arg = MMC_ARG_RCA(sf->rca);
819 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
820 }
821 error = sdmmc_mmc_command(sc, &cmd);
822 if (error)
823 return error;
824
825 if (ISSET(sc->sc_flags, SMF_SD_MODE))
826 sf->rca = SD_R6_RCA(cmd.c_resp);
827
828 return 0;
829 }
830
831 int
832 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
833 {
834 struct sdmmc_command cmd;
835 int error;
836
837 /* Don't lock */
838
839 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
840 return EIO;
841
842 if (sc->sc_card == sf
843 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
844 sc->sc_card = sf;
845 return 0;
846 }
847
848 memset(&cmd, 0, sizeof(cmd));
849 cmd.c_opcode = MMC_SELECT_CARD;
850 cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
851 cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
852 error = sdmmc_mmc_command(sc, &cmd);
853 if (error == 0 || sf == NULL)
854 sc->sc_card = sf;
855
856 return error;
857 }
858
859 #ifdef SDMMC_DEBUG
860 static void
861 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
862 {
863 int i;
864
865 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
866 DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
867 cmd->c_datalen, cmd->c_flags, cmd->c_error));
868
869 if (cmd->c_error || sdmmcdebug < 1)
870 return;
871
872 aprint_normal_dev(sc->sc_dev, "resp=");
873 if (ISSET(cmd->c_flags, SCF_RSP_136))
874 for (i = 0; i < sizeof cmd->c_resp; i++)
875 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
876 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
877 for (i = 0; i < 4; i++)
878 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
879 else
880 aprint_normal("none");
881 aprint_normal("\n");
882 }
883
884 void
885 sdmmc_dump_data(const char *title, void *ptr, size_t size)
886 {
887 char buf[16];
888 uint8_t *p = ptr;
889 int i, j;
890
891 printf("sdmmc_dump_data: %s\n", title ? title : "");
892 printf("--------+--------------------------------------------------+------------------+\n");
893 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
894 printf("--------+--------------------------------------------------+------------------+\n");
895 for (i = 0; i < (int)size; i++) {
896 if ((i % 16) == 0) {
897 printf("%08x| ", i);
898 } else if ((i % 16) == 8) {
899 printf(" ");
900 }
901
902 printf("%02x ", p[i]);
903 buf[i % 16] = p[i];
904
905 if ((i % 16) == 15) {
906 printf("| ");
907 for (j = 0; j < 16; j++) {
908 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
909 printf("%c", buf[j]);
910 } else {
911 printf(".");
912 }
913 }
914 printf(" |\n");
915 }
916 }
917 if ((i % 16) != 0) {
918 j = (i % 16);
919 for (; j < 16; j++) {
920 printf(" ");
921 if ((j % 16) == 8) {
922 printf(" ");
923 }
924 }
925
926 printf("| ");
927 for (j = 0; j < (i % 16); j++) {
928 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
929 printf("%c", buf[j]);
930 } else {
931 printf(".");
932 }
933 }
934 for (; j < 16; j++) {
935 printf(" ");
936 }
937 printf(" |\n");
938 }
939 printf("--------+--------------------------------------------------+------------------+\n");
940 }
941 #endif
942