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