sdmmc.c revision 1.5 1 /* $NetBSD: sdmmc.c,v 1.5 2010/10/07 12:24:23 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.5 2010/10/07 12:24:23 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.sf = sf;
380
381 sf->child =
382 config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
383 }
384
385 SET(sc->sc_flags, SMF_CARD_ATTACHED);
386 return;
387
388 err:
389 sdmmc_card_detach(sc, DETACH_FORCE);
390 }
391
392 /*
393 * Called from process context with DETACH_* flags from <sys/device.h>
394 * when cards are gone.
395 */
396 static void
397 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
398 {
399 struct sdmmc_function *sf, *sfnext;
400
401 DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
402
403 if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
404 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
405 if (sf->child != NULL) {
406 config_detach(sf->child, DETACH_FORCE);
407 sf->child = NULL;
408 }
409 }
410
411 KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
412
413 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
414 }
415
416 /* Power down. */
417 sdmmc_disable(sc);
418
419 /* Free all sdmmc_function structures. */
420 for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
421 sfnext = SIMPLEQ_NEXT(sf, sf_list);
422 sdmmc_function_free(sf);
423 }
424 SIMPLEQ_INIT(&sc->sf_head);
425 sc->sc_function_count = 0;
426 sc->sc_fn0 = NULL;
427 }
428
429 static int
430 sdmmc_print(void *aux, const char *pnp)
431 {
432 struct sdmmc_attach_args *sa = aux;
433 struct sdmmc_function *sf = sa->sf;
434 struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
435 int i;
436
437 if (pnp) {
438 if (sf->number == 0)
439 return QUIET;
440
441 for (i = 0; i < 4 && cis->cis1_info[i]; i++)
442 printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
443 if (i != 0)
444 printf("\"");
445
446 if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
447 cis->product != SDMMC_PRODUCT_INVALID) {
448 printf("%s(", i ? " " : "");
449 if (cis->manufacturer != SDMMC_VENDOR_INVALID)
450 printf("manufacturer 0x%x%s",
451 cis->manufacturer,
452 cis->product == SDMMC_PRODUCT_INVALID ?
453 "" : ", ");
454 if (cis->product != SDMMC_PRODUCT_INVALID)
455 printf("product 0x%x", cis->product);
456 printf(")");
457 }
458 printf("%sat %s", i ? " " : "", pnp);
459 }
460 if (sf->number > 0)
461 printf(" function %d", sf->number);
462
463 if (!pnp) {
464 for (i = 0; i < 3 && cis->cis1_info[i]; i++)
465 printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
466 if (i != 0)
467 printf("\"");
468 }
469 return UNCONF;
470 }
471
472 static int
473 sdmmc_enable(struct sdmmc_softc *sc)
474 {
475 int error;
476
477 /*
478 * Calculate the equivalent of the card OCR from the host
479 * capabilities and select the maximum supported bus voltage.
480 */
481 error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
482 sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
483 if (error) {
484 aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
485 goto out;
486 }
487
488 /*
489 * Select the minimum clock frequency.
490 */
491 error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
492 if (error) {
493 aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
494 goto out;
495 }
496
497 /* XXX wait for card to power up */
498 sdmmc_delay(100000);
499
500 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
501 /* Initialize SD I/O card function(s). */
502 error = sdmmc_io_enable(sc);
503 if (error)
504 goto out;
505 }
506
507 /* Initialize SD/MMC memory card(s). */
508 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
509 ISSET(sc->sc_flags, SMF_MEM_MODE))
510 error = sdmmc_mem_enable(sc);
511
512 out:
513 if (error)
514 sdmmc_disable(sc);
515 return error;
516 }
517
518 static void
519 sdmmc_disable(struct sdmmc_softc *sc)
520 {
521 /* XXX complete commands if card is still present. */
522
523 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
524 /* Make sure no card is still selected. */
525 (void)sdmmc_select_card(sc, NULL);
526 }
527
528 /* Turn off bus power and clock. */
529 (void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
530 (void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF);
531 (void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
532 }
533
534 /*
535 * Set the lowest bus voltage supported by the card and the host.
536 */
537 int
538 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
539 uint32_t card_ocr)
540 {
541 uint32_t bit;
542
543 /* Mask off unsupported voltage levels and select the lowest. */
544 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
545 host_ocr &= card_ocr;
546 for (bit = 4; bit < 23; bit++) {
547 if (ISSET(host_ocr, (1 << bit))) {
548 host_ocr &= (3 << bit);
549 break;
550 }
551 }
552 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
553
554 if (host_ocr == 0 ||
555 sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
556 return 1;
557 return 0;
558 }
559
560 struct sdmmc_function *
561 sdmmc_function_alloc(struct sdmmc_softc *sc)
562 {
563 struct sdmmc_function *sf;
564
565 sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
566 if (sf == NULL) {
567 aprint_error_dev(sc->sc_dev,
568 "couldn't alloc memory (sdmmc function)\n");
569 return NULL;
570 }
571
572 sf->sc = sc;
573 sf->number = -1;
574 sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
575 sf->cis.product = SDMMC_PRODUCT_INVALID;
576 sf->cis.function = SDMMC_FUNCTION_INVALID;
577
578 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
579 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
580 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
581 bus_dma_segment_t ds;
582 int rseg, error;
583
584 error = bus_dmamap_create(sc->sc_dmat, SDMMC_SECTOR_SIZE, 1,
585 SDMMC_SECTOR_SIZE, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
586 if (error)
587 goto fail1;
588 error = bus_dmamem_alloc(sc->sc_dmat, SDMMC_SECTOR_SIZE,
589 PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
590 if (error)
591 goto fail2;
592 error = bus_dmamem_map(sc->sc_dmat, &ds, 1, SDMMC_SECTOR_SIZE,
593 &sf->bbuf, BUS_DMA_WAITOK);
594 if (error)
595 goto fail3;
596 error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
597 sf->bbuf, SDMMC_SECTOR_SIZE, NULL,
598 BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
599 if (!error)
600 goto out;
601
602 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
603 fail3:
604 bus_dmamem_free(sc->sc_dmat, &ds, 1);
605 fail2:
606 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
607 fail1:
608 free(sf, M_DEVBUF);
609 sf = NULL;
610 }
611 out:
612
613 return sf;
614 }
615
616 void
617 sdmmc_function_free(struct sdmmc_function *sf)
618 {
619 struct sdmmc_softc *sc = sf->sc;
620
621 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
622 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
623 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
624 bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
625 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, SDMMC_SECTOR_SIZE);
626 bus_dmamem_free(sc->sc_dmat,
627 sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
628 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
629 }
630
631 free(sf, M_DEVBUF);
632 }
633
634 /*
635 * Scan for I/O functions and memory cards on the bus, allocating a
636 * sdmmc_function structure for each.
637 */
638 static int
639 sdmmc_scan(struct sdmmc_softc *sc)
640 {
641
642 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
643 /* Scan for I/O functions. */
644 if (ISSET(sc->sc_flags, SMF_IO_MODE))
645 sdmmc_io_scan(sc);
646 }
647
648 /* Scan for memory cards on the bus. */
649 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
650 sdmmc_mem_scan(sc);
651
652 /* There should be at least one function now. */
653 if (SIMPLEQ_EMPTY(&sc->sf_head)) {
654 aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
655 return 1;
656 }
657 return 0;
658 }
659
660 /*
661 * Initialize all the distinguished functions of the card, be it I/O
662 * or memory functions.
663 */
664 static int
665 sdmmc_init(struct sdmmc_softc *sc)
666 {
667 struct sdmmc_function *sf;
668
669 /* Initialize all identified card functions. */
670 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
671 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
672 if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
673 sdmmc_io_init(sc, sf) != 0) {
674 aprint_error_dev(sc->sc_dev,
675 "i/o init failed\n");
676 }
677 }
678
679 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
680 sdmmc_mem_init(sc, sf) != 0) {
681 aprint_error_dev(sc->sc_dev, "mem init failed\n");
682 }
683 }
684
685 /* Any good functions left after initialization? */
686 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
687 if (!ISSET(sf->flags, SFF_ERROR))
688 return 0;
689 }
690
691 /* No, we should probably power down the card. */
692 return 1;
693 }
694
695 void
696 sdmmc_delay(u_int usecs)
697 {
698
699 delay(usecs);
700 }
701
702 int
703 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
704 {
705 struct sdmmc_command acmd;
706 int error;
707
708 DPRINTF(1,("sdmmc_app_command: start\n"));
709
710 /* Don't lock */
711
712 memset(&acmd, 0, sizeof(acmd));
713 acmd.c_opcode = MMC_APP_CMD;
714 if (sf != NULL) {
715 acmd.c_arg = sf->rca << 16;
716 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
717 } else {
718 acmd.c_arg = 0;
719 acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
720 }
721
722 error = sdmmc_mmc_command(sc, &acmd);
723 if (error == 0) {
724 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
725 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
726 /* Card does not support application commands. */
727 error = ENODEV;
728 } else {
729 error = sdmmc_mmc_command(sc, cmd);
730 }
731 }
732 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
733 return error;
734 }
735
736 /*
737 * Execute MMC command and data transfers. All interactions with the
738 * host controller to complete the command happen in the context of
739 * the current process.
740 */
741 int
742 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
743 {
744 int error;
745
746 DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
747 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
748
749 /* Don't lock */
750
751 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
752 if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
753 if (sc->sc_card == NULL)
754 panic("%s: deselected card\n", DEVNAME(sc));
755 }
756 #endif
757
758 sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
759
760 #ifdef SDMMC_DEBUG
761 sdmmc_dump_command(sc, cmd);
762 #endif
763
764 error = cmd->c_error;
765
766 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
767
768 return error;
769 }
770
771 /*
772 * Send the "GO IDLE STATE" command.
773 */
774 void
775 sdmmc_go_idle_state(struct sdmmc_softc *sc)
776 {
777 struct sdmmc_command cmd;
778
779 DPRINTF(1,("sdmmc_go_idle_state\n"));
780
781 /* Don't lock */
782
783 memset(&cmd, 0, sizeof(cmd));
784 cmd.c_opcode = MMC_GO_IDLE_STATE;
785 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
786
787 (void)sdmmc_mmc_command(sc, &cmd);
788 }
789
790 /*
791 * Retrieve (SD) or set (MMC) the relative card address (RCA).
792 */
793 int
794 sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
795 {
796 struct sdmmc_command cmd;
797 int error;
798
799 /* Don't lock */
800
801 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
802 return EIO;
803
804 memset(&cmd, 0, sizeof(cmd));
805 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
806 cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
807 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
808 } else {
809 cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
810 cmd.c_arg = MMC_ARG_RCA(sf->rca);
811 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
812 }
813 error = sdmmc_mmc_command(sc, &cmd);
814 if (error)
815 return error;
816
817 if (ISSET(sc->sc_flags, SMF_SD_MODE))
818 sf->rca = SD_R6_RCA(cmd.c_resp);
819
820 return 0;
821 }
822
823 int
824 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
825 {
826 struct sdmmc_command cmd;
827 int error;
828
829 /* Don't lock */
830
831 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
832 return EIO;
833
834 if (sc->sc_card == sf
835 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
836 sc->sc_card = sf;
837 return 0;
838 }
839
840 memset(&cmd, 0, sizeof(cmd));
841 cmd.c_opcode = MMC_SELECT_CARD;
842 cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
843 cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
844 error = sdmmc_mmc_command(sc, &cmd);
845 if (error == 0 || sf == NULL)
846 sc->sc_card = sf;
847
848 return error;
849 }
850
851 #ifdef SDMMC_DEBUG
852 static void
853 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
854 {
855 int i;
856
857 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
858 DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
859 cmd->c_datalen, cmd->c_flags, cmd->c_error));
860
861 if (cmd->c_error || sdmmcdebug < 1)
862 return;
863
864 aprint_normal_dev(sc->sc_dev, "resp=");
865 if (ISSET(cmd->c_flags, SCF_RSP_136))
866 for (i = 0; i < sizeof cmd->c_resp; i++)
867 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
868 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
869 for (i = 0; i < 4; i++)
870 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
871 else
872 aprint_normal("none");
873 aprint_normal("\n");
874 }
875
876 void
877 sdmmc_dump_data(const char *title, void *ptr, size_t size)
878 {
879 char buf[16];
880 uint8_t *p = ptr;
881 int i, j;
882
883 printf("sdmmc_dump_data: %s\n", title ? title : "");
884 printf("--------+--------------------------------------------------+------------------+\n");
885 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
886 printf("--------+--------------------------------------------------+------------------+\n");
887 for (i = 0; i < (int)size; i++) {
888 if ((i % 16) == 0) {
889 printf("%08x| ", i);
890 } else if ((i % 16) == 8) {
891 printf(" ");
892 }
893
894 printf("%02x ", p[i]);
895 buf[i % 16] = p[i];
896
897 if ((i % 16) == 15) {
898 printf("| ");
899 for (j = 0; j < 16; j++) {
900 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
901 printf("%c", buf[j]);
902 } else {
903 printf(".");
904 }
905 }
906 printf(" |\n");
907 }
908 }
909 if ((i % 16) != 0) {
910 j = (i % 16);
911 for (; j < 16; j++) {
912 printf(" ");
913 if ((j % 16) == 8) {
914 printf(" ");
915 }
916 }
917
918 printf("| ");
919 for (j = 0; j < (i % 16); j++) {
920 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
921 printf("%c", buf[j]);
922 } else {
923 printf(".");
924 }
925 }
926 for (; j < 16; j++) {
927 printf(" ");
928 }
929 printf(" |\n");
930 }
931 printf("--------+--------------------------------------------------+------------------+\n");
932 }
933 #endif
934