Lines Matching +full:transfer +full:- +full:function
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
12 * - Lutz Bichler <Lutz.Bichler@gmail.com>
73 /* The softc holds our per-instance data. */
89 /* function to call if transfer succeed */
91 /* function to call if transfer fail */
102 bus_dma_tag_t rtsx_cmd_dma_tag; /* DMA tag for command transfer */
103 bus_dmamap_t rtsx_cmd_dmamap; /* DMA map for command transfer */
104 void *rtsx_cmd_dmamem; /* DMA mem for command transfer */
108 bus_dma_tag_t rtsx_data_dma_tag; /* DMA tag for data transfer */
109 bus_dmamap_t rtsx_data_dmamap; /* DMA map for data transfer */
110 void *rtsx_data_dmamem; /* DMA mem for data transfer */
136 uint8_t rtsx_cam_status; /* CAM status - 1 if card in use */
143 bool rtsx_vpclk; /* voltage at Pulse-width Modulation(PWM) clock? */
292 #define RTSX_LOCK_INIT(_sc) mtx_init(&(_sc)->rtsx_mtx, \
293 device_get_nameunit(sc->rtsx_dev), "rtsx", MTX_DEF)
294 #define RTSX_LOCK(_sc) mtx_lock(&(_sc)->rtsx_mtx)
295 #define RTSX_UNLOCK(_sc) mtx_unlock(&(_sc)->rtsx_mtx)
296 #define RTSX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->rtsx_mtx)
319 (bus_space_read_4((sc)->rtsx_mem_btag, (sc)->rtsx_mem_bhandle, (reg)))
321 (bus_space_write_4((sc)->rtsx_mem_btag, (sc)->rtsx_mem_bhandle, (reg), (val)))
366 * and a data bit-mask and value.
367 * SD/MMC commands which do not transfer any data from/to the card only use
370 * The data buffer is used for transfer longer than 512. Data transfer is
387 * CMD13 Send status (32 bits - bit 25: card password protected)
389 * ACMD51 Send SCR (SD CARD Configuration Register - [51:48]: Bus widths supported)
390 * CMD6 SD switch function
411 error = bus_dma_tag_create(bus_get_dma_tag(sc->rtsx_dev), /* inherit from parent */ in rtsx_dma_alloc()
420 &sc->rtsx_cmd_dma_tag); in rtsx_dma_alloc()
422 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
426 error = bus_dmamem_alloc(sc->rtsx_cmd_dma_tag, /* DMA tag */ in rtsx_dma_alloc()
427 &sc->rtsx_cmd_dmamem, /* will hold the KVA pointer */ in rtsx_dma_alloc()
429 &sc->rtsx_cmd_dmamap); /* DMA map */ in rtsx_dma_alloc()
431 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
432 "Can't create DMA map for command transfer\n"); in rtsx_dma_alloc()
436 error = bus_dmamap_load(sc->rtsx_cmd_dma_tag, /* DMA tag */ in rtsx_dma_alloc()
437 sc->rtsx_cmd_dmamap, /* DMA map */ in rtsx_dma_alloc()
438 sc->rtsx_cmd_dmamem, /* KVA pointer to be mapped */ in rtsx_dma_alloc()
441 &sc->rtsx_cmd_buffer, /* first arg of callback */ in rtsx_dma_alloc()
443 if (error || sc->rtsx_cmd_buffer == 0) { in rtsx_dma_alloc()
444 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
445 "Can't load DMA memory for command transfer\n"); in rtsx_dma_alloc()
450 error = bus_dma_tag_create(bus_get_dma_tag(sc->rtsx_dev), /* inherit from parent */ in rtsx_dma_alloc()
459 &sc->rtsx_data_dma_tag); in rtsx_dma_alloc()
461 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
465 error = bus_dmamem_alloc(sc->rtsx_data_dma_tag, /* DMA tag */ in rtsx_dma_alloc()
466 &sc->rtsx_data_dmamem, /* will hold the KVA pointer */ in rtsx_dma_alloc()
468 &sc->rtsx_data_dmamap); /* DMA map */ in rtsx_dma_alloc()
470 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
471 "Can't create DMA map for data transfer\n"); in rtsx_dma_alloc()
474 error = bus_dmamap_load(sc->rtsx_data_dma_tag, /* DMA tag */ in rtsx_dma_alloc()
475 sc->rtsx_data_dmamap, /* DMA map */ in rtsx_dma_alloc()
476 sc->rtsx_data_dmamem, /* KVA pointer to be mapped */ in rtsx_dma_alloc()
479 &sc->rtsx_data_buffer, /* first arg of callback */ in rtsx_dma_alloc()
481 if (error || sc->rtsx_data_buffer == 0) { in rtsx_dma_alloc()
482 device_printf(sc->rtsx_dev, in rtsx_dma_alloc()
483 "Can't load DMA memory for data transfer\n"); in rtsx_dma_alloc()
490 bus_dmamem_free(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamem, sc->rtsx_data_dmamap); in rtsx_dma_alloc()
492 bus_dma_tag_destroy(sc->rtsx_data_dma_tag); in rtsx_dma_alloc()
494 bus_dmamap_unload(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap); in rtsx_dma_alloc()
496 bus_dmamem_free(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamem, sc->rtsx_cmd_dmamap); in rtsx_dma_alloc()
498 bus_dma_tag_destroy(sc->rtsx_cmd_dma_tag); in rtsx_dma_alloc()
516 if (sc->rtsx_cmd_dma_tag != NULL) { in rtsx_dma_free()
517 if (sc->rtsx_cmd_dmamap != NULL) in rtsx_dma_free()
518 bus_dmamap_unload(sc->rtsx_cmd_dma_tag, in rtsx_dma_free()
519 sc->rtsx_cmd_dmamap); in rtsx_dma_free()
520 if (sc->rtsx_cmd_dmamem != NULL) in rtsx_dma_free()
521 bus_dmamem_free(sc->rtsx_cmd_dma_tag, in rtsx_dma_free()
522 sc->rtsx_cmd_dmamem, in rtsx_dma_free()
523 sc->rtsx_cmd_dmamap); in rtsx_dma_free()
524 sc->rtsx_cmd_dmamap = NULL; in rtsx_dma_free()
525 sc->rtsx_cmd_dmamem = NULL; in rtsx_dma_free()
526 sc->rtsx_cmd_buffer = 0; in rtsx_dma_free()
527 bus_dma_tag_destroy(sc->rtsx_cmd_dma_tag); in rtsx_dma_free()
528 sc->rtsx_cmd_dma_tag = NULL; in rtsx_dma_free()
530 if (sc->rtsx_data_dma_tag != NULL) { in rtsx_dma_free()
531 if (sc->rtsx_data_dmamap != NULL) in rtsx_dma_free()
532 bus_dmamap_unload(sc->rtsx_data_dma_tag, in rtsx_dma_free()
533 sc->rtsx_data_dmamap); in rtsx_dma_free()
534 if (sc->rtsx_data_dmamem != NULL) in rtsx_dma_free()
535 bus_dmamem_free(sc->rtsx_data_dma_tag, in rtsx_dma_free()
536 sc->rtsx_data_dmamem, in rtsx_dma_free()
537 sc->rtsx_data_dmamap); in rtsx_dma_free()
538 sc->rtsx_data_dmamap = NULL; in rtsx_dma_free()
539 sc->rtsx_data_dmamem = NULL; in rtsx_dma_free()
540 sc->rtsx_data_buffer = 0; in rtsx_dma_free()
541 bus_dma_tag_destroy(sc->rtsx_data_dma_tag); in rtsx_dma_free()
542 sc->rtsx_data_dma_tag = NULL; in rtsx_dma_free()
555 enabled = sc->rtsx_intr_enabled; in rtsx_intr()
557 sc->rtsx_intr_status = status; in rtsx_intr()
559 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD) in rtsx_intr()
560 …device_printf(sc->rtsx_dev, "Interrupt handler - enabled: 0x%08x, status: 0x%08x\n", enabled, stat… in rtsx_intr()
566 …device_printf(sc->rtsx_dev, "Spurious interrupt - enabled: 0x%08x, status: 0x%08x\n", enabled, sta… in rtsx_intr()
573 sc->rtsx_read_only = 1; in rtsx_intr()
575 sc->rtsx_read_only = 0; in rtsx_intr()
579 device_printf(sc->rtsx_dev, "Interrupt card inserted/removed\n"); in rtsx_intr()
583 if (sc->rtsx_req == NULL) { in rtsx_intr()
589 sc->rtsx_req->cmd->error = MMC_ERR_NONE; in rtsx_intr()
590 if (sc->rtsx_intr_trans_ok != NULL) in rtsx_intr()
591 sc->rtsx_intr_trans_ok(sc); in rtsx_intr()
594 sc->rtsx_req->cmd->error = MMC_ERR_FAILED; in rtsx_intr()
597 device_printf(sc->rtsx_dev, "CRC error\n"); in rtsx_intr()
598 sc->rtsx_req->cmd->error = MMC_ERR_BADCRC; in rtsx_intr()
600 if (!sc->rtsx_tuning_mode) in rtsx_intr()
601 device_printf(sc->rtsx_dev, "Transfer fail - status: 0x%08x\n", status); in rtsx_intr()
603 if (sc->rtsx_intr_trans_ko != NULL) in rtsx_intr()
604 sc->rtsx_intr_trans_ko(sc); in rtsx_intr()
611 * Function called from the IRQ handler (from dwmmc.c).
620 was_present = sc->rtsx_cam_status; in rtsx_handle_card_present()
622 was_present = sc->rtsx_mmc_dev != NULL; in rtsx_handle_card_present()
626 device_printf(sc->rtsx_dev, "Card present\n"); in rtsx_handle_card_present()
628 device_printf(sc->rtsx_dev, "Card absent\n"); in rtsx_handle_card_present()
637 &sc->rtsx_card_insert_task, -hz); in rtsx_handle_card_present()
639 taskqueue_enqueue(taskqueue_swi_giant, &sc->rtsx_card_remove_task); in rtsx_handle_card_present()
644 * This function is called at startup.
652 sc->rtsx_flags |= RTSX_F_CARD_PRESENT;
655 if (sc->rtsx_cam_status == 0) {
657 if (sc->rtsx_mmc_dev == NULL) {
659 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
660 device_printf(sc->rtsx_dev, "Card inserted\n");
662 sc->rtsx_read_count = sc->rtsx_write_count = 0;
664 sc->rtsx_cam_status = 1;
665 mmc_cam_sim_discover(&sc->rtsx_mmc_sim);
668 sc->rtsx_mmc_dev = device_add_child(sc->rtsx_dev, "mmc", DEVICE_UNIT_ANY);
670 if (sc->rtsx_mmc_dev == NULL) {
671 device_printf(sc->rtsx_dev, "Adding MMC bus failed\n");
673 device_set_ivars(sc->rtsx_mmc_dev, sc);
674 device_probe_and_attach(sc->rtsx_mmc_dev);
679 sc->rtsx_flags &= ~RTSX_F_CARD_PRESENT;
682 if (sc->rtsx_cam_status != 0) {
684 if (sc->rtsx_mmc_dev != NULL) {
686 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
687 device_printf(sc->rtsx_dev, "Card removed\n");
689 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
690 device_printf(sc->rtsx_dev, "Read count: %" PRIu64 ", write count: %" PRIu64 "\n",
691 sc->rtsx_read_count, sc->rtsx_write_count);
693 sc->rtsx_cam_status = 0;
694 mmc_cam_sim_discover(&sc->rtsx_mmc_sim);
696 if (device_delete_child(sc->rtsx_dev, sc->rtsx_mmc_dev))
697 device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n");
698 sc->rtsx_mmc_dev = NULL;
710 if (sc->rtsx_inversion == 0)
723 sc->rtsx_host.host_ocr = RTSX_SUPPORTED_VOLTAGE;
724 sc->rtsx_host.f_min = RTSX_SDCLK_250KHZ;
725 sc->rtsx_host.f_max = RTSX_SDCLK_208MHZ;
726 sc->rtsx_host.caps = MMC_CAP_4_BIT_DATA | MMC_CAP_HSPEED |
729 sc->rtsx_host.caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104;
730 if (sc->rtsx_device_id == RTSX_RTS5209)
731 sc->rtsx_host.caps |= MMC_CAP_8_BIT_DATA;
732 pci_find_cap(sc->rtsx_dev, PCIY_EXPRESS, &(sc->rtsx_pcie_cap));
737 switch (sc->rtsx_device_id) {
742 sc->rtsx_flags |= RTSX_F_VERSION_C;
748 sc->rtsx_flags |= RTSX_F_VERSION_A;
754 sc->rtsx_flags |= RTSX_F_VERSION_A;
759 sc->rtsx_flags |= RTSX_F_8411B_QFN48;
772 sc->rtsx_card_drive_sel = RTSX_CARD_DRIVE_DEFAULT;
773 switch (sc->rtsx_device_id) {
778 sc->rtsx_card_drive_sel = RTSX_RTS5209_CARD_DRIVE_DEFAULT;
779 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
780 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
782 sc->rtsx_card_drive_sel = (reg >> 8) & 0x3F;
783 sc->rtsx_sd30_drive_sel_3v3 = reg & 0x07;
784 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
785 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
787 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
788 device_printf(sc->rtsx_dev, "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
789 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
793 sc->rtsx_sd30_drive_sel_3v3 = RTSX_CFG_DRIVER_TYPE_B;
794 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
796 sc->rtsx_card_drive_sel &= 0x3F;
797 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
798 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
799 sc->rtsx_sd30_drive_sel_3v3 = (reg >> 5) & 0x03;
801 sc->rtsx_flags |= RTSX_F_REVERSE_SOCKET;
802 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
803 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
805 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
806 device_printf(sc->rtsx_dev,
808 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3,
809 (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET) ? "true" : "false");
812 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
813 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
815 sc->rtsx_card_drive_sel &= 0x3F;
816 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
817 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
818 sc->rtsx_sd30_drive_sel_3v3 = rtsx_map_sd_drive((reg >> 5) & 0x03);
819 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
820 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
822 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
823 device_printf(sc->rtsx_dev, "card_drive_sel: 0x%02x, sd30_drive_sel_3v3: 0x%02x\n",
824 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
829 sc->rtsx_sd30_drive_sel_3v3 = RTSX_CFG_DRIVER_TYPE_B;
830 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
832 sc->rtsx_card_drive_sel &= 0x3F;
833 sc->rtsx_card_drive_sel |= ((reg >> 25) & 0x01) << 6;
834 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG2, 4);
835 sc->rtsx_sd30_drive_sel_3v3 = (reg >> 5) & 0x03;
837 sc->rtsx_flags |= RTSX_F_REVERSE_SOCKET;
838 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
839 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
841 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
842 device_printf(sc->rtsx_dev,
844 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3,
845 (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET) ? "true" : "false");
849 sc->rtsx_card_drive_sel = RTSX_RTL8411_CARD_DRIVE_DEFAULT;
850 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
851 reg1 = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
853 sc->rtsx_card_drive_sel &= 0x3F;
854 sc->rtsx_card_drive_sel |= ((reg1 >> 25) & 0x01) << 6;
855 reg3 = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG3, 1);
856 sc->rtsx_sd30_drive_sel_3v3 = (reg3 >> 5) & 0x07;
857 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
858 device_printf(sc->rtsx_dev, "pci_read_config() error - reg1: 0x%08x\n", reg1);
860 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
861 device_printf(sc->rtsx_dev,
863 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
866 sc->rtsx_card_drive_sel = RTSX_RTL8411_CARD_DRIVE_DEFAULT;
867 sc->rtsx_sd30_drive_sel_3v3 = RTSX_DRIVER_TYPE_D;
868 reg = pci_read_config(sc->rtsx_dev, RTSX_PCR_SETTING_REG1, 4);
870 sc->rtsx_sd30_drive_sel_3v3 = rtsx_map_sd_drive(reg & 0x03);
871 } else if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
872 device_printf(sc->rtsx_dev, "pci_read_config() error - reg: 0x%08x\n", reg);
874 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
875 device_printf(sc->rtsx_dev,
877 sc->rtsx_card_drive_sel, sc->rtsx_sd30_drive_sel_3v3);
881 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
882 device_printf(sc->rtsx_dev, "rtsx_init() rtsx_flags: 0x%04x\n", sc->rtsx_flags);
885 sc->rtsx_intr_enabled = RTSX_TRANS_OK_INT_EN | RTSX_TRANS_FAIL_INT_EN | RTSX_SD_INT_EN;
886 WRITE4(sc, RTSX_BIER, sc->rtsx_intr_enabled);
894 val = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL, 1);
895 pci_write_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL, val & 0xfc, 1);
900 switch (sc->rtsx_device_id) {
920 if (sc->rtsx_flags & RTSX_F_VERSION_A) {
943 if (sc->rtsx_flags & RTSX_F_VERSION_A)
1012 RTSX_WRITE(sc, RTSX_CARD_DRIVE_SEL, sc->rtsx_card_drive_sel);
1031 /* Enable interrupt write-clear (default is read-clear). */
1034 switch (sc->rtsx_device_id) {
1045 pci_write_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_LINK_CTL + 1, 1, 1);
1048 pci_write_config(sc->rtsx_dev, 0x70F, 0x5B, 1);
1053 switch (sc->rtsx_device_id) {
1065 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1078 cap = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_DEVICE_CTL2, 2);
1087 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1109 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1123 cap = pci_read_config(sc->rtsx_dev, sc->rtsx_pcie_cap + PCIER_DEVICE_CTL2, 2);
1132 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1163 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1170 if (sc->rtsx_flags & RTSX_F_VERSION_A) {
1202 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1226 if (sc->rtsx_flags & RTSX_F_REVERSE_SOCKET)
1241 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1246 if (sc->rtsx_flags & RTSX_F_8411B_QFN48)
1248 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, sc->rtsx_sd30_drive_sel_3v3);
1289 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1290 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1291 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1306 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1307 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1308 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1322 RTSX_WRITE(sc, RTSX_SD30_CLK_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][0]);
1323 RTSX_WRITE(sc, RTSX_SD30_CMD_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][1]);
1324 RTSX_WRITE(sc, RTSX_SD30_DAT_DRIVE_SEL, driving_3v3[sc->rtsx_sd30_drive_sel_3v3][2]);
1339 while (tries--) {
1349 device_printf(sc->rtsx_dev, "rtsx_read(0x%x) timeout\n", arg);
1364 while (tries--) {
1395 while (tries--) {
1399 device_printf(sc->rtsx_dev, "rtsx_write(0x%x) error reg=0x%x\n", arg, reg);
1405 device_printf(sc->rtsx_dev, "rtsx_write(0x%x) timeout\n", arg);
1419 while (tries--) {
1445 while (tries--) {
1462 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1463 device_printf(sc->rtsx_dev, "rtsx_bus_power_off()\n");
1472 switch (sc->rtsx_device_id) {
1512 switch (sc->rtsx_device_id) {
1525 if (sc->rtsx_flags & RTSX_F_VERSION_C)
1548 if (sc->rtsx_flags & RTSX_F_8411B_QFN48) {
1569 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1570 device_printf(sc->rtsx_dev, "rtsx_bus_power_on()\n");
1580 switch (sc->rtsx_device_id) {
1593 if (sc->rtsx_flags & RTSX_F_VERSION_C)
1616 if (sc->rtsx_flags & RTSX_F_8411B_QFN48) {
1635 switch (sc->rtsx_device_id) {
1767 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
1773 device_printf(sc->rtsx_dev, "Setting bus width to %s\n", busw[bus_width]);
1781 if (timing == bus_timing_hs && sc->rtsx_force_timing) {
1783 sc->rtsx_ios_timing = timing;
1786 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1787 device_printf(sc->rtsx_dev, "rtsx_set_sd_timing(%u)\n", timing);
1792 sc->rtsx_double_clk = false;
1793 sc->rtsx_vpclk = true;
1840 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1841 device_printf(sc->rtsx_dev, "rtsx_set_sd_clock(%u)\n", freq);
1848 sc->rtsx_ssc_depth = RTSX_SSC_DEPTH_500K;
1849 sc->rtsx_discovery_mode = (freq <= 1000000) ? true : false;
1851 if (sc->rtsx_discovery_mode) {
1861 if (sc->rtsx_discovery_mode || !sc->rtsx_double_clk)
1866 switch (sc->rtsx_device_id) {
1870 n = clk * 4 / 5 - 2;
1873 n = clk - 2;
1886 switch (sc->rtsx_device_id) {
1890 n = (((n + 2) * 5 / 4) * 2) * 4 / 5 - 2;
1893 n = (n + 2) * 2 - 2;
1899 if (sc->rtsx_double_clk && sc->rtsx_ssc_depth > 1)
1900 sc->rtsx_ssc_depth -= 1;
1903 if (sc->rtsx_ssc_depth > (div - 1))
1904 sc->rtsx_ssc_depth -= (div - 1);
1906 sc->rtsx_ssc_depth = RTSX_SSC_DEPTH_4M;
1927 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) {
1928 device_printf(sc->rtsx_dev, "rtsx_switch_sd_clock() - discovery-mode is %s, ssc_depth: %d\n",
1929 (sc->rtsx_discovery_mode) ? "true" : "false", sc->rtsx_ssc_depth);
1930 device_printf(sc->rtsx_dev, "rtsx_switch_sd_clock() - clk: %d, n: %d, div: %d, mcu: %d\n",
1937 RTSX_BITOP(sc, RTSX_SSC_CTL2, RTSX_SSC_DEPTH_MASK, sc->rtsx_ssc_depth);
1940 if (sc->rtsx_vpclk) {
1957 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
1958 device_printf(sc->rtsx_dev, "rtsx_sd_change_tx_phase() - sample_point: %d\n", sample_point);
1971 if (sc->rtsx_debug_mask & RTSX_DEBUG_TUNING)
1972 device_printf(sc->rtsx_dev, "rtsx_sd_change_rx_phase() - sample_point: %d\n", sample_point);
2011 sc->rtsx_req = &req;
2029 sc->rtsx_intr_trans_ok = rtsx_sd_tuning_rx_cmd_wakeup;
2030 sc->rtsx_intr_trans_ko = rtsx_sd_tuning_rx_cmd_wakeup;
2038 if (sc->rtsx_debug_mask & RTSX_DEBUG_TUNING)
2039 device_printf(sc->rtsx_dev, "rtsx_sd_tuning_rx_cmd() - error: %d\n", error);
2045 sc->rtsx_req = NULL;
2058 status = sc->rtsx_intr_status & mask;
2060 …if (msleep(&sc->rtsx_intr_status, &sc->rtsx_mtx, 0, "rtsxintr", sc->rtsx_timeout_cmd) == EWOULDBLO…
2061 cmd->error = MMC_ERR_TIMEOUT;
2064 status = sc->rtsx_intr_status & mask;
2066 return (cmd->error);
2072 wakeup(&sc->rtsx_intr_status);
2107 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2108 device_printf(sc->rtsx_dev,
2109 …"rtsx_sd_search_final_rx_phase() - phase_map: %x, start_final: %d, len_final: %d, final_phase: %d\…
2132 switch (sc->rtsx_device_id) {
2155 switch (sc->rtsx_device_id) {
2207 sc->rtsx_cmd_index = 0;
2209 0xff, RTSX_SD_CMD_START | cmd->opcode);
2211 0xff, cmd->arg >> 24);
2213 0xff, cmd->arg >> 16);
2215 0xff, cmd->arg >> 8);
2217 0xff, cmd->arg);
2227 KASSERT(sc->rtsx_cmd_index < RTSX_HOSTCMD_MAX,
2228 ("rtsx: Too many host commands (%d)\n", sc->rtsx_cmd_index));
2230 uint32_t *cmd_buffer = (uint32_t *)(sc->rtsx_cmd_dmamem);
2231 cmd_buffer[sc->rtsx_cmd_index++] =
2239 * Queue commands to configure data transfer size.
2260 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2261 device_printf(sc->rtsx_dev, "rtsx_send_cmd()\n");
2263 sc->rtsx_intr_status = 0;
2266 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_PREREAD);
2267 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_PREWRITE);
2270 WRITE4(sc, RTSX_HCBAR, (uint32_t)sc->rtsx_cmd_buffer);
2272 ((sc->rtsx_cmd_index * 4) & 0x00ffffff) | RTSX_START_CMD | RTSX_HW_AUTO_RSP);
2281 /* Stop command transfer. */
2284 /* Stop DMA transfer. */
2287 switch (sc->rtsx_device_id) {
2324 req = sc->rtsx_req;
2325 if (req->cmd->error == MMC_ERR_NONE) {
2326 if (req->cmd->opcode == MMC_READ_SINGLE_BLOCK ||
2327 req->cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
2328 sc->rtsx_read_count++;
2329 else if (req->cmd->opcode == MMC_WRITE_BLOCK ||
2330 req->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
2331 sc->rtsx_write_count++;
2335 callout_stop(&sc->rtsx_timeout_callout);
2336 sc->rtsx_req = NULL;
2338 ccb = sc->rtsx_ccb;
2339 sc->rtsx_ccb = NULL;
2340 ccb->ccb_h.status = (req->cmd->error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR);
2343 req->done(req);
2356 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2357 device_printf(sc->rtsx_dev, "rtsx_send_req() - CMD%d\n", cmd->opcode);
2360 rsp_type = rtsx_response_type(cmd->flags & MMC_RSP_MASK);
2362 device_printf(sc->rtsx_dev, "Unknown rsp_type: 0x%lx\n", (cmd->flags & MMC_RSP_MASK));
2363 cmd->error = MMC_ERR_INVALID;
2372 /* Use the ping-pong buffer (cmd buffer) for commands which do not transfer data. */
2376 /* Queue commands to perform SD transfer. */
2385 /* Read data from ping-pong buffer. */
2395 /* Set transfer OK function. */
2396 if (sc->rtsx_intr_trans_ok == NULL)
2397 sc->rtsx_intr_trans_ok = rtsx_ret_resp;
2406 * Return response of previous command (case cmd->data == NULL) and complete resquest.
2407 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2414 cmd = sc->rtsx_req->cmd;
2427 rsp_type = rtsx_response_type(cmd->flags & MMC_RSP_MASK);
2430 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2431 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2434 if (ISSET(cmd->flags, MMC_RSP_PRESENT)) {
2435 uint32_t *cmd_buffer = (uint32_t *)(sc->rtsx_cmd_dmamem);
2437 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD) {
2438 device_printf(sc->rtsx_dev, "cmd_buffer: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
2448 * The controller offloads the last byte {CRC-7, end bit 1}
2455 cmd->resp[i] = be32dec(ptr + 1 + i * 4);
2459 * one is the command op code -- we skip those.
2461 cmd->resp[0] =
2466 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2467 device_printf(sc->rtsx_dev, "cmd->resp: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2468 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
2473 * Use the ping-pong buffer (cmd buffer) for transfer <= 512 bytes.
2480 if (cmd->data == NULL || cmd->data->len == 0) {
2481 cmd->error = MMC_ERR_INVALID;
2484 cmd->data->xfer_len = (cmd->data->len > RTSX_MAX_DATA_BLKLEN) ?
2485 RTSX_MAX_DATA_BLKLEN : cmd->data->len;
2487 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2489 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2490 device_printf(sc->rtsx_dev, "rtsx_xfer_short() - %s xfer: %ld bytes with block size %ld\n",
2492 (unsigned long)cmd->data->len, (unsigned long)cmd->data->xfer_len);
2494 if (cmd->data->len > 512) {
2495 device_printf(sc->rtsx_dev, "rtsx_xfer_short() - length too large: %ld > 512\n",
2496 (unsigned long)cmd->data->len);
2497 cmd->error = MMC_ERR_INVALID;
2502 if (sc->rtsx_discovery_mode)
2507 /* Queue commands to configure data transfer size. */
2508 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2515 /* Use the ping-pong buffer (cmd buffer). */
2519 /* Queue commands to perform SD transfer. */
2525 /* Set transfer OK function. */
2526 sc->rtsx_intr_trans_ok = rtsx_ask_ppbuf_part1;
2531 /* Set transfer OK function. */
2532 sc->rtsx_intr_trans_ok = rtsx_put_ppbuf_part1;
2542 * Use the ping-pong buffer (cmd buffer) for the transfer - first part <= 256 bytes.
2543 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2553 cmd = sc->rtsx_req->cmd;
2554 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2556 sc->rtsx_cmd_index = 0;
2561 /* Set transfer OK function. */
2562 sc->rtsx_intr_trans_ok = rtsx_get_ppbuf_part1;
2569 * Get the data from the ping-pong buffer (cmd buffer) - first part <= 256 bytes.
2570 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2579 cmd = sc->rtsx_req->cmd;
2580 ptr = cmd->data->data;
2581 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2584 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2585 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2587 memcpy(ptr, sc->rtsx_cmd_dmamem, len);
2589 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? cmd->data->len - RTSX_HOSTCMD_MAX : 0;
2591 /* Use the ping-pong buffer (cmd buffer) for the transfer - second part > 256 bytes. */
2596 sc->rtsx_cmd_index = 0;
2601 /* Set transfer OK function. */
2602 sc->rtsx_intr_trans_ok = rtsx_get_ppbuf_part2;
2607 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD && cmd->opcode == ACMD_SEND_SCR) {
2608 uint8_t *ptr = cmd->data->data;
2609 device_printf(sc->rtsx_dev, "SCR: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2614 if (sc->rtsx_discovery_mode)
2622 * Get the data from the ping-pong buffer (cmd buffer) - second part > 256 bytes.
2623 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2632 cmd = sc->rtsx_req->cmd;
2633 ptr = cmd->data->data;
2635 len = cmd->data->len - RTSX_HOSTCMD_MAX;
2638 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTREAD);
2639 bus_dmamap_sync(sc->rtsx_cmd_dma_tag, sc->rtsx_cmd_dmamap, BUS_DMASYNC_POSTWRITE);
2641 memcpy(ptr, sc->rtsx_cmd_dmamem, len);
2643 if (sc->rtsx_discovery_mode)
2650 * Use the ping-pong buffer (cmd buffer) for transfer - first part <= 256 bytes.
2651 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2662 cmd = sc->rtsx_req->cmd;
2663 ptr = cmd->data->data;
2664 len = (cmd->data->len > RTSX_HOSTCMD_MAX) ? RTSX_HOSTCMD_MAX : cmd->data->len;
2668 sc->rtsx_cmd_index = 0;
2674 /* Set transfer OK function. */
2675 if (cmd->data->len > RTSX_HOSTCMD_MAX)
2676 sc->rtsx_intr_trans_ok = rtsx_put_ppbuf_part2;
2678 sc->rtsx_intr_trans_ok = rtsx_write_ppbuf;
2685 * Use the ping-pong buffer (cmd buffer) for transfer - second part > 256 bytes.
2686 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2697 cmd = sc->rtsx_req->cmd;
2698 ptr = cmd->data->data;
2700 len = cmd->data->len - RTSX_HOSTCMD_MAX;
2702 sc->rtsx_cmd_index = 0;
2708 /* Set transfer OK function. */
2709 sc->rtsx_intr_trans_ok = rtsx_write_ppbuf;
2716 * Write the data previously given via the ping-pong buffer on the card.
2717 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2724 cmd = sc->rtsx_req->cmd;
2726 sc->rtsx_cmd_index = 0;
2728 /* Queue commands to configure data transfer size. */
2729 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2741 /* Set transfer OK function. */
2742 sc->rtsx_intr_trans_ok = rtsx_req_done;
2749 * Use the data buffer for transfer > 512 bytes.
2754 int read = ISSET(cmd->data->flags, MMC_DATA_READ);
2756 cmd->data->xfer_len = (cmd->data->len > RTSX_MAX_DATA_BLKLEN) ?
2757 RTSX_MAX_DATA_BLKLEN : cmd->data->len;
2759 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2760 device_printf(sc->rtsx_dev, "rtsx_xfer() - %s xfer: %ld bytes with block size %ld\n",
2762 (unsigned long)cmd->data->len, (unsigned long)cmd->data->xfer_len);
2764 if (cmd->data->len > RTSX_DMA_DATA_BUFSIZE) {
2765 device_printf(sc->rtsx_dev, "rtsx_xfer() length too large: %ld > %ld\n",
2766 (unsigned long)cmd->data->len, RTSX_DMA_DATA_BUFSIZE);
2767 cmd->error = MMC_ERR_INVALID;
2772 /* Set transfer OK function. */
2773 sc->rtsx_intr_trans_ok = rtsx_xfer_begin;
2785 * Get request response and start dma data transfer (write command).
2786 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2793 cmd = sc->rtsx_req->cmd;
2795 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2796 device_printf(sc->rtsx_dev, "rtsx_xfer_begin() - CMD%d\n", cmd->opcode);
2803 * Start dma data transfer.
2814 cmd = sc->rtsx_req->cmd;
2815 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2817 /* Configure DMA transfer mode parameters. */
2818 if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK)
2825 * Use transfer mode AUTO_READ1, which assume we not
2836 * Use transfer mode AUTO_WRITE3, wich assumes we've already
2843 sc->rtsx_cmd_index = 0;
2846 /* Queue commands to configure data transfer size. */
2847 rtsx_set_cmd_data_len(sc, cmd->data->len / cmd->data->xfer_len, cmd->data->xfer_len);
2853 0xff, cmd->data->len >> 24);
2855 0xff, cmd->data->len >> 16);
2857 0xff, cmd->data->len >> 8);
2859 0xff, cmd->data->len);
2864 /* Use the DMA ring buffer for commands which transfer data. */
2871 /* Queue commands to perform SD transfer. */
2881 memcpy(sc->rtsx_data_dmamem, cmd->data->data, cmd->data->len);
2884 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREREAD);
2885 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREWRITE);
2887 /* Set transfer OK function. */
2888 sc->rtsx_intr_trans_ok = rtsx_xfer_finish;
2890 /* Tell the chip where the data buffer is and run the transfer. */
2891 WRITE4(sc, RTSX_HDBAR, sc->rtsx_data_buffer);
2893 (cmd->data->len & 0x00ffffff));
2897 * Finish dma data transfer.
2898 * This Function is called by the interrupt handler via sc->rtsx_intr_trans_ok.
2906 cmd = sc->rtsx_req->cmd;
2908 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
2909 device_printf(sc->rtsx_dev, "rtsx_xfer_finish() - CMD%d\n", cmd->opcode);
2911 read = ISSET(cmd->data->flags, MMC_DATA_READ);
2914 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTREAD);
2915 bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTWRITE);
2918 memcpy(cmd->data->data, sc->rtsx_data_dmamem, cmd->data->len);
2923 sc->rtsx_intr_trans_ok = NULL;
2924 rtsx_send_req(sc, sc->rtsx_req->stop);
2937 if (sc->rtsx_req != NULL) {
2938 device_printf(sc->rtsx_dev, "Controller timeout for CMD%u\n",
2939 sc->rtsx_req->cmd->opcode);
2940 sc->rtsx_req->cmd->error = MMC_ERR_TIMEOUT;
2944 device_printf(sc->rtsx_dev, "Controller timeout!\n");
2956 cts->host_ocr = sc->rtsx_host.host_ocr;
2957 cts->host_f_min = sc->rtsx_host.f_min;
2958 cts->host_f_max = sc->rtsx_host.f_max;
2959 cts->host_caps = sc->rtsx_host.caps;
2960 cts->host_max_data = RTSX_DMA_DATA_BUFSIZE / MMC_SECTOR_SIZE;
2961 memcpy(&cts->ios, &sc->rtsx_host.ios, sizeof(struct mmc_ios));
2978 ios = &sc->rtsx_host.ios;
2979 new_ios = &cts->ios;
2982 if (cts->ios_valid & MMC_CLK) {
2983 ios->clock = new_ios->clock;
2984 sc->rtsx_ios_clock = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
2985 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2986 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - clock: %u\n", ios->clock);
2988 if (cts->ios_valid & MMC_VDD) {
2989 ios->vdd = new_ios->vdd;
2990 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2991 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - vdd: %d\n", ios->vdd);
2993 if (cts->ios_valid & MMC_CS) {
2994 ios->chip_select = new_ios->chip_select;
2995 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
2996 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - chip_select: %d\n", ios->chip_select);
2998 if (cts->ios_valid & MMC_BW) {
2999 ios->bus_width = new_ios->bus_width;
3000 sc->rtsx_ios_bus_width = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
3001 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3002 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - bus width: %d\n", ios->bus_width);
3004 if (cts->ios_valid & MMC_PM) {
3005 ios->power_mode = new_ios->power_mode;
3006 sc->rtsx_ios_power_mode = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
3007 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3008 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - power mode: %d\n", ios->power_mode);
3010 if (cts->ios_valid & MMC_BT) {
3011 ios->timing = new_ios->timing;
3012 sc->rtsx_ios_timing = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
3013 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3014 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - timing: %d\n", ios->timing);
3016 if (cts->ios_valid & MMC_BM) {
3017 ios->bus_mode = new_ios->bus_mode;
3018 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3019 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - bus mode: %d\n", ios->bus_mode);
3022 if (cts->ios_valid & MMC_VCCQ) {
3023 ios->vccq = new_ios->vccq;
3024 sc->rtsx_ios_vccq = -1; /* To be updated by rtsx_mmcbr_update_ios(). */
3025 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3026 device_printf(sc->rtsx_dev, "rtsx_set_tran_settings() - vccq: %d\n", ios->vccq);
3029 if (rtsx_mmcbr_update_ios(sc->rtsx_dev, NULL) == 0)
3046 if (sc->rtsx_ccb != NULL) {
3050 sc->rtsx_ccb = ccb;
3051 sc->rtsx_cam_req.cmd = &ccb->mmcio.cmd;
3052 sc->rtsx_cam_req.stop = &ccb->mmcio.stop;
3055 rtsx_mmcbr_request(sc->rtsx_dev, NULL, &sc->rtsx_cam_req);
3067 case MMCBR_IVAR_BUS_MODE: /* ivar 0 - 1 = opendrain, 2 = pushpull */
3068 *result = sc->rtsx_host.ios.bus_mode;
3070 case MMCBR_IVAR_BUS_WIDTH: /* ivar 1 - 0 = 1b 2 = 4b, 3 = 8b */
3071 *result = sc->rtsx_host.ios.bus_width;
3073 case MMCBR_IVAR_CHIP_SELECT: /* ivar 2 - O = dontcare, 1 = cs_high, 2 = cs_low */
3074 *result = sc->rtsx_host.ios.chip_select;
3076 case MMCBR_IVAR_CLOCK: /* ivar 3 - clock in Hz */
3077 *result = sc->rtsx_host.ios.clock;
3080 *result = sc->rtsx_host.f_min;
3083 *result = sc->rtsx_host.f_max;
3085 case MMCBR_IVAR_HOST_OCR: /* ivar 6 - host operation conditions register */
3086 *result = sc->rtsx_host.host_ocr;
3088 case MMCBR_IVAR_MODE: /* ivar 7 - 0 = mode_mmc, 1 = mode_sd */
3089 *result = sc->rtsx_host.mode;
3091 case MMCBR_IVAR_OCR: /* ivar 8 - operation conditions register */
3092 *result = sc->rtsx_host.ocr;
3094 case MMCBR_IVAR_POWER_MODE: /* ivar 9 - 0 = off, 1 = up, 2 = on */
3095 *result = sc->rtsx_host.ios.power_mode;
3097 case MMCBR_IVAR_VDD: /* ivar 11 - voltage power pin */
3098 *result = sc->rtsx_host.ios.vdd;
3100 case MMCBR_IVAR_VCCQ: /* ivar 12 - signaling: 0 = 1.20V, 1 = 1.80V, 2 = 3.30V */
3101 *result = sc->rtsx_host.ios.vccq;
3104 *result = sc->rtsx_host.caps;
3106 case MMCBR_IVAR_TIMING: /* ivar 14 - 0 = normal, 1 = timing_hs, ... */
3107 *result = sc->rtsx_host.ios.timing;
3118 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3131 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3136 case MMCBR_IVAR_BUS_MODE: /* ivar 0 - 1 = opendrain, 2 = pushpull */
3137 sc->rtsx_host.ios.bus_mode = value;
3139 case MMCBR_IVAR_BUS_WIDTH: /* ivar 1 - 0 = 1b 2 = 4b, 3 = 8b */
3140 sc->rtsx_host.ios.bus_width = value;
3141 sc->rtsx_ios_bus_width = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3143 case MMCBR_IVAR_CHIP_SELECT: /* ivar 2 - O = dontcare, 1 = cs_high, 2 = cs_low */
3144 sc->rtsx_host.ios.chip_select = value;
3146 case MMCBR_IVAR_CLOCK: /* ivar 3 - clock in Hz */
3147 sc->rtsx_host.ios.clock = value;
3148 sc->rtsx_ios_clock = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3150 case MMCBR_IVAR_MODE: /* ivar 7 - 0 = mode_mmc, 1 = mode_sd */
3151 sc->rtsx_host.mode = value;
3153 case MMCBR_IVAR_OCR: /* ivar 8 - operation conditions register */
3154 sc->rtsx_host.ocr = value;
3156 case MMCBR_IVAR_POWER_MODE: /* ivar 9 - 0 = off, 1 = up, 2 = on */
3157 sc->rtsx_host.ios.power_mode = value;
3158 sc->rtsx_ios_power_mode = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3160 case MMCBR_IVAR_VDD: /* ivar 11 - voltage power pin */
3161 sc->rtsx_host.ios.vdd = value;
3163 case MMCBR_IVAR_VCCQ: /* ivar 12 - signaling: 0 = 1.20V, 1 = 1.80V, 2 = 3.30V */
3164 sc->rtsx_host.ios.vccq = value;
3165 sc->rtsx_ios_vccq = value; /* rtsx_mmcbr_switch_vccq() will be called by mmc.c (MMCCAM undef). */
3167 case MMCBR_IVAR_TIMING: /* ivar 14 - 0 = normal, 1 = timing_hs, ... */
3168 sc->rtsx_host.ios.timing = value;
3169 sc->rtsx_ios_timing = -1; /* To be updated on next rtsx_mmcbr_update_ios(). */
3171 /* These are read-only. */
3174 case MMCBR_IVAR_HOST_OCR: /* ivar 6 - host operation conditions register */
3194 ios = &sc->rtsx_host.ios;
3196 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3200 if (sc->rtsx_ios_bus_width < 0) {
3201 sc->rtsx_ios_bus_width = ios->bus_width;
3202 if ((error = rtsx_set_bus_width(sc, ios->bus_width)))
3207 if (sc->rtsx_ios_power_mode < 0) {
3208 sc->rtsx_ios_power_mode = ios->power_mode;
3209 switch (ios->power_mode) {
3225 sc->rtsx_double_clk = true;
3226 sc->rtsx_vpclk = false;
3229 if (sc->rtsx_ios_timing < 0) {
3230 sc->rtsx_ios_timing = ios->timing;
3231 if ((error = rtsx_set_sd_timing(sc, ios->timing)))
3236 if (sc->rtsx_ios_clock < 0) {
3237 sc->rtsx_ios_clock = ios->clock;
3238 if ((error = rtsx_set_sd_clock(sc, ios->clock)))
3243 if (sc->rtsx_ios_vccq < 0) {
3244 sc->rtsx_ios_vccq = ios->vccq;
3245 if ((error = rtsx_mmcbr_switch_vccq(sc->rtsx_dev, NULL)))
3264 switch (sc->rtsx_host.ios.vccq) {
3277 switch (sc->rtsx_device_id) {
3287 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3320 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3327 RTSX_BITOP(sc, RTSX_SD30_CMD_DRIVE_SEL, RTSX_SD30_DRIVE_SEL_MASK, sc->rtsx_sd30_drive_sel_3v3);
3336 if (sc->rtsx_debug_mask & (RTSX_DEBUG_BASIC | RTSX_TRACE_SD_CMD))
3337 device_printf(sc->rtsx_dev, "rtsx_mmcbr_switch_vccq(%d)\n", vccq);
3357 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3358 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - hs400 is %s\n",
3361 if (sc->rtsx_ios_timing != bus_timing_uhs_sdr50)
3364 sc->rtsx_tuning_mode = true;
3366 switch (sc->rtsx_device_id) {
3397 if (sc->rtsx_debug_mask & (RTSX_DEBUG_BASIC | RTSX_DEBUG_TUNING))
3398 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - RX raw_phase_map[%d]: 0x%08x\n",
3402 if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3403 device_printf(sc->rtsx_dev, "rtsx_mmcbr_tune() - RX phase_map: 0x%08x\n", phase_map);
3412 sc->rtsx_tuning_mode = false;
3424 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3425 device_printf(sc->rtsx_dev, "rtsx_mmcbr_retune()\n");
3442 if (sc->rtsx_req != NULL) {
3446 sc->rtsx_req = req;
3447 cmd = req->cmd;
3448 cmd->error = error = MMC_ERR_NONE;
3449 sc->rtsx_intr_status = 0;
3450 sc->rtsx_intr_trans_ok = NULL;
3451 sc->rtsx_intr_trans_ko = rtsx_req_done;
3453 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3454 device_printf(sc->rtsx_dev, "rtsx_mmcbr_request(CMD%u arg %#x, flags %#x, dlen %u, dflags %#x)\n",
3455 cmd->opcode, cmd->arg, cmd->flags,
3456 cmd->data != NULL ? (unsigned int)cmd->data->len : 0,
3457 cmd->data != NULL ? cmd->data->flags : 0);
3460 if (!ISSET(sc->rtsx_flags, RTSX_F_CARD_PRESENT)) {
3461 cmd->error = error = MMC_ERR_FAILED;
3466 if (cmd->opcode == IO_SEND_OP_COND &&
3467 !ISSET(sc->rtsx_flags, RTSX_F_SDIO_SUPPORT)) {
3468 cmd->error = error = MMC_ERR_INVALID;
3473 if (cmd->opcode == SD_IO_RW_DIRECT || cmd->opcode == IO_SEND_OP_COND) {
3474 cmd->error = error = MMC_ERR_TIMEOUT;
3482 if (cmd->data == NULL) {
3484 timeout = sc->rtsx_timeout_cmd;
3486 } else if (cmd->data->len <= 512) {
3487 timeout = sc->rtsx_timeout_io;
3490 timeout = sc->rtsx_timeout_io;
3495 callout_reset(&sc->rtsx_timeout_callout, timeout * hz, rtsx_timeout, sc);
3512 if (sc->rtsx_inversion == 0)
3513 return (sc->rtsx_read_only);
3515 return !(sc->rtsx_read_only);
3524 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3528 while (sc->rtsx_bus_busy)
3529 msleep(&sc->rtsx_bus_busy, &sc->rtsx_mtx, 0, "rtsxah", 0);
3530 sc->rtsx_bus_busy++;
3542 if (sc->rtsx_debug_mask & RTSX_TRACE_SD_CMD)
3546 sc->rtsx_bus_busy--;
3547 wakeup(&sc->rtsx_bus_busy);
3586 * Attach function is only called if the probe is successful.
3607 device_printf(dev, "Attach - Vendor ID: 0x%x - Device ID: 0x%x\n",
3610 sc->rtsx_dev = dev;
3611 sc->rtsx_device_id = device_id;
3612 sc->rtsx_req = NULL;
3613 sc->rtsx_timeout_cmd = 1;
3614 sc->rtsx_timeout_io = 10;
3615 sc->rtsx_read_only = 0;
3616 sc->rtsx_inversion = 0;
3617 sc->rtsx_force_timing = 0;
3618 sc->rtsx_debug_mask = 0;
3619 sc->rtsx_read_count = 0;
3620 sc->rtsx_write_count = 0;
3632 sc->rtsx_inversion = 1;
3642 &sc->rtsx_timeout_io, 0, "Request timeout for I/O commands in seconds");
3644 &sc->rtsx_timeout_cmd, 0, "Request timeout for setup commands in seconds");
3646 &sc->rtsx_read_only, 0, "Card is write protected");
3648 &sc->rtsx_inversion, 0, "Inversion of card detection and read only status");
3650 &sc->rtsx_force_timing, 0, "Force bus_timing_uhs_sdr50");
3652 &sc->rtsx_debug_mask, 0, "debugging mask, see rtsx(4)");
3654 &sc->rtsx_read_count, 0, "Count of read operations");
3656 &sc->rtsx_write_count, 0, "Count of write operations");
3658 if (bootverbose || sc->rtsx_debug_mask & RTSX_DEBUG_BASIC)
3659 device_printf(dev, "We are running with inversion: %d\n", sc->rtsx_inversion);
3662 sc->rtsx_irq_res_id = 0;
3664 sc->rtsx_irq_res_id = 1;
3665 sc->rtsx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rtsx_irq_res_id,
3666 RF_ACTIVE | (sc->rtsx_irq_res_id != 0 ? 0 : RF_SHAREABLE));
3667 if (sc->rtsx_irq_res == NULL) {
3668 device_printf(dev, "Can't allocate IRQ resources for %d\n", sc->rtsx_irq_res_id);
3673 callout_init_mtx(&sc->rtsx_timeout_callout, &sc->rtsx_mtx, 0);
3676 if (sc->rtsx_device_id == RTSX_RTS525A)
3677 sc->rtsx_mem_res_id = PCIR_BAR(1);
3679 sc->rtsx_mem_res_id = PCIR_BAR(0);
3680 sc->rtsx_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rtsx_mem_res_id, RF_ACTIVE);
3681 if (sc->rtsx_mem_res == NULL) {
3682 device_printf(dev, "Can't allocate memory resource for %d\n", sc->rtsx_mem_res_id);
3688 sc->rtsx_irq_res_id, sc->rtsx_mem_res_id);
3690 sc->rtsx_mem_btag = rman_get_bustag(sc->rtsx_mem_res);
3691 sc->rtsx_mem_bhandle = rman_get_bushandle(sc->rtsx_mem_res);
3693 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &sc->rtsx_card_insert_task, 0,
3695 TASK_INIT(&sc->rtsx_card_remove_task, 0, rtsx_card_task, sc);
3703 error = bus_setup_intr(dev, sc->rtsx_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
3704 NULL, rtsx_intr, sc, &sc->rtsx_irq_cookie);
3714 sc->rtsx_flags |= RTSX_F_SDIO_SUPPORT;
3718 sc->rtsx_ccb = NULL;
3719 sc->rtsx_cam_status = 0;
3722 &sc->rtsx_cam_status, 0, "driver cam card present");
3724 if (mmc_cam_sim_alloc(dev, "rtsx_mmc", &sc->rtsx_mmc_sim) != 0) {
3746 device_printf(sc->rtsx_dev, "A card is detected\n");
3748 device_printf(sc->rtsx_dev, "No card is detected\n");
3757 bus_teardown_intr(dev, sc->rtsx_irq_res, sc->rtsx_irq_cookie);
3759 bus_release_resource(dev, SYS_RES_MEMORY, sc->rtsx_mem_res_id,
3760 sc->rtsx_mem_res);
3763 callout_drain(&sc->rtsx_timeout_callout);
3764 bus_release_resource(dev, SYS_RES_IRQ, sc->rtsx_irq_res_id,
3765 sc->rtsx_irq_res);
3779 device_printf(dev, "Detach - Vendor ID: 0x%x - Device ID: 0x%x\n",
3780 pci_get_vendor(dev), sc->rtsx_device_id);
3783 sc->rtsx_intr_enabled = 0;
3784 WRITE4(sc, RTSX_BIER, sc->rtsx_intr_enabled);
3787 error = device_delete_children(sc->rtsx_dev);
3788 sc->rtsx_mmc_dev = NULL;
3792 taskqueue_drain_timeout(taskqueue_swi_giant, &sc->rtsx_card_insert_task);
3793 taskqueue_drain(taskqueue_swi_giant, &sc->rtsx_card_remove_task);
3797 if (sc->rtsx_mem_res != NULL)
3798 bus_release_resource(dev, SYS_RES_MEMORY, sc->rtsx_mem_res_id,
3799 sc->rtsx_mem_res);
3800 if (sc->rtsx_irq_cookie != NULL)
3801 bus_teardown_intr(dev, sc->rtsx_irq_res, sc->rtsx_irq_cookie);
3802 if (sc->rtsx_irq_res != NULL) {
3803 callout_drain(&sc->rtsx_timeout_callout);
3804 bus_release_resource(dev, SYS_RES_IRQ, sc->rtsx_irq_res_id,
3805 sc->rtsx_irq_res);
3810 mmc_cam_sim_free(&sc->rtsx_mmc_sim);
3836 if (sc->rtsx_ccb != NULL) {
3838 sc->rtsx_ccb->mmcio.cmd.opcode, sc->rtsx_intr_status);
3841 if (sc->rtsx_req != NULL) {
3843 sc->rtsx_req->cmd->opcode, sc->rtsx_intr_status);