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