sdmmc.c revision 1.6 1 /* $NetBSD: sdmmc.c,v 1.6 2010/10/07 12:40:34 kiyohara 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 2010/10/07 12:40:34 kiyohara 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 }
534
535 /*
536 * Set the lowest bus voltage supported by the card and the host.
537 */
538 int
539 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
540 uint32_t card_ocr)
541 {
542 uint32_t bit;
543
544 /* Mask off unsupported voltage levels and select the lowest. */
545 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
546 host_ocr &= card_ocr;
547 for (bit = 4; bit < 23; bit++) {
548 if (ISSET(host_ocr, (1 << bit))) {
549 host_ocr &= (3 << bit);
550 break;
551 }
552 }
553 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
554
555 if (host_ocr == 0 ||
556 sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
557 return 1;
558 return 0;
559 }
560
561 struct sdmmc_function *
562 sdmmc_function_alloc(struct sdmmc_softc *sc)
563 {
564 struct sdmmc_function *sf;
565
566 sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
567 if (sf == NULL) {
568 aprint_error_dev(sc->sc_dev,
569 "couldn't alloc memory (sdmmc function)\n");
570 return NULL;
571 }
572
573 sf->sc = sc;
574 sf->number = -1;
575 sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
576 sf->cis.product = SDMMC_PRODUCT_INVALID;
577 sf->cis.function = SDMMC_FUNCTION_INVALID;
578
579 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
580 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
581 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
582 bus_dma_segment_t ds;
583 int rseg, error;
584
585 error = bus_dmamap_create(sc->sc_dmat, SDMMC_SECTOR_SIZE, 1,
586 SDMMC_SECTOR_SIZE, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
587 if (error)
588 goto fail1;
589 error = bus_dmamem_alloc(sc->sc_dmat, SDMMC_SECTOR_SIZE,
590 PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
591 if (error)
592 goto fail2;
593 error = bus_dmamem_map(sc->sc_dmat, &ds, 1, SDMMC_SECTOR_SIZE,
594 &sf->bbuf, BUS_DMA_WAITOK);
595 if (error)
596 goto fail3;
597 error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
598 sf->bbuf, SDMMC_SECTOR_SIZE, NULL,
599 BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
600 if (!error)
601 goto out;
602
603 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
604 fail3:
605 bus_dmamem_free(sc->sc_dmat, &ds, 1);
606 fail2:
607 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
608 fail1:
609 free(sf, M_DEVBUF);
610 sf = NULL;
611 }
612 out:
613
614 return sf;
615 }
616
617 void
618 sdmmc_function_free(struct sdmmc_function *sf)
619 {
620 struct sdmmc_softc *sc = sf->sc;
621
622 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
623 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
624 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
625 bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
626 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
627 bus_dmamem_free(sc->sc_dmat,
628 sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
629 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
630 }
631
632 free(sf, M_DEVBUF);
633 }
634
635 /*
636 * Scan for I/O functions and memory cards on the bus, allocating a
637 * sdmmc_function structure for each.
638 */
639 static int
640 sdmmc_scan(struct sdmmc_softc *sc)
641 {
642
643 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
644 /* Scan for I/O functions. */
645 if (ISSET(sc->sc_flags, SMF_IO_MODE))
646 sdmmc_io_scan(sc);
647 }
648
649 /* Scan for memory cards on the bus. */
650 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
651 sdmmc_mem_scan(sc);
652
653 /* There should be at least one function now. */
654 if (SIMPLEQ_EMPTY(&sc->sf_head)) {
655 aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
656 return 1;
657 }
658 return 0;
659 }
660
661 /*
662 * Initialize all the distinguished functions of the card, be it I/O
663 * or memory functions.
664 */
665 static int
666 sdmmc_init(struct sdmmc_softc *sc)
667 {
668 struct sdmmc_function *sf;
669
670 /* Initialize all identified card functions. */
671 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
672 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
673 if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
674 sdmmc_io_init(sc, sf) != 0) {
675 aprint_error_dev(sc->sc_dev,
676 "i/o init failed\n");
677 }
678 }
679
680 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
681 sdmmc_mem_init(sc, sf) != 0) {
682 aprint_error_dev(sc->sc_dev, "mem init failed\n");
683 }
684 }
685
686 /* Any good functions left after initialization? */
687 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
688 if (!ISSET(sf->flags, SFF_ERROR))
689 return 0;
690 }
691
692 /* No, we should probably power down the card. */
693 return 1;
694 }
695
696 void
697 sdmmc_delay(u_int usecs)
698 {
699
700 delay(usecs);
701 }
702
703 int
704 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
705 {
706 struct sdmmc_command acmd;
707 int error;
708
709 DPRINTF(1,("sdmmc_app_command: start\n"));
710
711 /* Don't lock */
712
713 memset(&acmd, 0, sizeof(acmd));
714 acmd.c_opcode = MMC_APP_CMD;
715 if (sf != NULL) {
716 acmd.c_arg = sf->rca << 16;
717 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
718 } else {
719 acmd.c_arg = 0;
720 acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
721 }
722
723 error = sdmmc_mmc_command(sc, &acmd);
724 if (error == 0) {
725 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
726 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
727 /* Card does not support application commands. */
728 error = ENODEV;
729 } else {
730 error = sdmmc_mmc_command(sc, cmd);
731 }
732 }
733 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
734 return error;
735 }
736
737 /*
738 * Execute MMC command and data transfers. All interactions with the
739 * host controller to complete the command happen in the context of
740 * the current process.
741 */
742 int
743 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
744 {
745 int error;
746
747 DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
748 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
749
750 /* Don't lock */
751
752 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
753 if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
754 if (sc->sc_card == NULL)
755 panic("%s: deselected card\n", DEVNAME(sc));
756 }
757 #endif
758
759 sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
760
761 #ifdef SDMMC_DEBUG
762 sdmmc_dump_command(sc, cmd);
763 #endif
764
765 error = cmd->c_error;
766
767 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
768
769 return error;
770 }
771
772 /*
773 * Send the "GO IDLE STATE" command.
774 */
775 void
776 sdmmc_go_idle_state(struct sdmmc_softc *sc)
777 {
778 struct sdmmc_command cmd;
779
780 DPRINTF(1,("sdmmc_go_idle_state\n"));
781
782 /* Don't lock */
783
784 memset(&cmd, 0, sizeof(cmd));
785 cmd.c_opcode = MMC_GO_IDLE_STATE;
786 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
787
788 (void)sdmmc_mmc_command(sc, &cmd);
789 }
790
791 /*
792 * Retrieve (SD) or set (MMC) the relative card address (RCA).
793 */
794 int
795 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
796 {
797 struct sdmmc_command cmd;
798 int error;
799
800 /* Don't lock */
801
802 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
803 return EIO;
804
805 memset(&cmd, 0, sizeof(cmd));
806 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
807 cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
808 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
809 } else {
810 cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
811 cmd.c_arg = MMC_ARG_RCA(sf->rca);
812 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
813 }
814 error = sdmmc_mmc_command(sc, &cmd);
815 if (error)
816 return error;
817
818 if (ISSET(sc->sc_flags, SMF_SD_MODE))
819 sf->rca = SD_R6_RCA(cmd.c_resp);
820
821 return 0;
822 }
823
824 int
825 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
826 {
827 struct sdmmc_command cmd;
828 int error;
829
830 /* Don't lock */
831
832 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
833 return EIO;
834
835 if (sc->sc_card == sf
836 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
837 sc->sc_card = sf;
838 return 0;
839 }
840
841 memset(&cmd, 0, sizeof(cmd));
842 cmd.c_opcode = MMC_SELECT_CARD;
843 cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
844 cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
845 error = sdmmc_mmc_command(sc, &cmd);
846 if (error == 0 || sf == NULL)
847 sc->sc_card = sf;
848
849 return error;
850 }
851
852 #ifdef SDMMC_DEBUG
853 static void
854 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
855 {
856 int i;
857
858 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
859 DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
860 cmd->c_datalen, cmd->c_flags, cmd->c_error));
861
862 if (cmd->c_error || sdmmcdebug < 1)
863 return;
864
865 aprint_normal_dev(sc->sc_dev, "resp=");
866 if (ISSET(cmd->c_flags, SCF_RSP_136))
867 for (i = 0; i < sizeof cmd->c_resp; i++)
868 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
869 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
870 for (i = 0; i < 4; i++)
871 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
872 else
873 aprint_normal("none");
874 aprint_normal("\n");
875 }
876
877 void
878 sdmmc_dump_data(const char *title, void *ptr, size_t size)
879 {
880 char buf[16];
881 uint8_t *p = ptr;
882 int i, j;
883
884 printf("sdmmc_dump_data: %s\n", title ? title : "");
885 printf("--------+--------------------------------------------------+------------------+\n");
886 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
887 printf("--------+--------------------------------------------------+------------------+\n");
888 for (i = 0; i < (int)size; i++) {
889 if ((i % 16) == 0) {
890 printf("%08x| ", i);
891 } else if ((i % 16) == 8) {
892 printf(" ");
893 }
894
895 printf("%02x ", p[i]);
896 buf[i % 16] = p[i];
897
898 if ((i % 16) == 15) {
899 printf("| ");
900 for (j = 0; j < 16; j++) {
901 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
902 printf("%c", buf[j]);
903 } else {
904 printf(".");
905 }
906 }
907 printf(" |\n");
908 }
909 }
910 if ((i % 16) != 0) {
911 j = (i % 16);
912 for (; j < 16; j++) {
913 printf(" ");
914 if ((j % 16) == 8) {
915 printf(" ");
916 }
917 }
918
919 printf("| ");
920 for (j = 0; j < (i % 16); j++) {
921 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
922 printf("%c", buf[j]);
923 } else {
924 printf(".");
925 }
926 }
927 for (; j < 16; j++) {
928 printf(" ");
929 }
930 printf(" |\n");
931 }
932 printf("--------+--------------------------------------------------+------------------+\n");
933 }
934 #endif
935