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