1831f5dcfSAlexander Motin /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4831f5dcfSAlexander Motin * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org> 5aca38eabSMarius Strobl * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 6831f5dcfSAlexander Motin * All rights reserved. 7831f5dcfSAlexander Motin * 8831f5dcfSAlexander Motin * Redistribution and use in source and binary forms, with or without 9831f5dcfSAlexander Motin * modification, are permitted provided that the following conditions 10831f5dcfSAlexander Motin * are met: 11831f5dcfSAlexander Motin * 1. Redistributions of source code must retain the above copyright 12831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer. 13831f5dcfSAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 14831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer in the 15831f5dcfSAlexander Motin * documentation and/or other materials provided with the distribution. 16831f5dcfSAlexander Motin * 17831f5dcfSAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18831f5dcfSAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19831f5dcfSAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20831f5dcfSAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21831f5dcfSAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22831f5dcfSAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23831f5dcfSAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24831f5dcfSAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25831f5dcfSAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26831f5dcfSAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27831f5dcfSAlexander Motin */ 28831f5dcfSAlexander Motin 29831f5dcfSAlexander Motin #include <sys/param.h> 30831f5dcfSAlexander Motin #include <sys/systm.h> 31831f5dcfSAlexander Motin #include <sys/bus.h> 32e64f01a9SIan Lepore #include <sys/callout.h> 33831f5dcfSAlexander Motin #include <sys/conf.h> 34831f5dcfSAlexander Motin #include <sys/kernel.h> 35aca38eabSMarius Strobl #include <sys/kobj.h> 36ab00a509SMarius Strobl #include <sys/libkern.h> 37831f5dcfSAlexander Motin #include <sys/lock.h> 38aca38eabSMarius Strobl #include <sys/malloc.h> 39831f5dcfSAlexander Motin #include <sys/module.h> 40831f5dcfSAlexander Motin #include <sys/mutex.h> 41831f5dcfSAlexander Motin #include <sys/resource.h> 42831f5dcfSAlexander Motin #include <sys/rman.h> 435b69a497SAlexander Motin #include <sys/sysctl.h> 44831f5dcfSAlexander Motin #include <sys/taskqueue.h> 45d00c1f7fSBartlomiej Grzesik #include <sys/sbuf.h> 46831f5dcfSAlexander Motin 47831f5dcfSAlexander Motin #include <machine/bus.h> 48831f5dcfSAlexander Motin #include <machine/resource.h> 49831f5dcfSAlexander Motin #include <machine/stdarg.h> 50831f5dcfSAlexander Motin 51831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 52831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 53831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 54831f5dcfSAlexander Motin 55aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h> 56aca38eabSMarius Strobl 57a94a63f0SWarner Losh #include <cam/cam.h> 58a94a63f0SWarner Losh #include <cam/cam_ccb.h> 59a94a63f0SWarner Losh #include <cam/cam_debug.h> 60a94a63f0SWarner Losh #include <cam/cam_sim.h> 61a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 62a94a63f0SWarner Losh 63831f5dcfSAlexander Motin #include "mmcbr_if.h" 64d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 65831f5dcfSAlexander Motin 66a94a63f0SWarner Losh #include "opt_mmccam.h" 67a94a63f0SWarner Losh 687029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 697029da5cSPawel Biernacki "sdhci driver"); 70831f5dcfSAlexander Motin 71a94a63f0SWarner Losh static int sdhci_debug = 0; 727e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 737e6ccea3SMarius Strobl "Debug level"); 740f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 757b86593fSJustin Hibbits SYSCTL_UINT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 760f34084fSMarius Strobl 0, "Mask of quirks to clear"); 770f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 787b86593fSJustin Hibbits SYSCTL_UINT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 790f34084fSMarius Strobl "Mask of quirks to set"); 805b69a497SAlexander Motin 81d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 82d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 83d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 84d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 85d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 86831f5dcfSAlexander Motin 87d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 88d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 89d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 90d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 91d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 92831f5dcfSAlexander Motin 936dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err); 94aca38eabSMarius Strobl static void sdhci_card_poll(void *arg); 95aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending); 966dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); 976dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); 98aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 996dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, 1006dea80e6SMarius Strobl bool is_present); 1016dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot); 1026dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot); 1036dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot); 1046dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot); 105aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req); 106aca38eabSMarius Strobl static void sdhci_retune(void *arg); 107831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 1086dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power); 1096dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot, 110ab00a509SMarius Strobl const struct mmc_data *data); 111831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 1126dea80e6SMarius Strobl static void sdhci_timeout(void *arg); 1136dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot, 1146dea80e6SMarius Strobl struct mmc_command *cmd); 115ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot, 116ab00a509SMarius Strobl const struct mmc_data *data); 1176dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot); 1186dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot); 119831f5dcfSAlexander Motin 12015c440e1SWarner Losh #ifdef MMCCAM 121a94a63f0SWarner Losh /* CAM-related */ 122a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 123ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 1246dea80e6SMarius Strobl int proposed_clock); 125a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 1266dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 127a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 1286dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot); 12915c440e1SWarner Losh #endif 130a94a63f0SWarner Losh 131831f5dcfSAlexander Motin /* helper routines */ 132016f9657SMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot); 133ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot); 134d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps(struct sdhci_slot *slot); 135d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s); 1360f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 137d00c1f7fSBartlomiej Grzesik static void sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s); 138d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS); 139d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS); 1406dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, 1416dea80e6SMarius Strobl int error); 142ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 1430f34084fSMarius Strobl __printflike(2, 3); 144d00c1f7fSBartlomiej Grzesik static int slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s, 145d00c1f7fSBartlomiej Grzesik const char * fmt, ...) __printflike(3, 4); 146ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot); 1470f34084fSMarius Strobl 148831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 149831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 150831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 151831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 152831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 153831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 154831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 155831f5dcfSAlexander Motin 15633aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 15733aad34dSOleksandr Tymoshenko 15857677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 15957677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 16057677a3aSOleksandr Tymoshenko 161639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 162639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 163639f59f0SIan Lepore 16493efdc63SAdrian Chadd /* 16593efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 16693efdc63SAdrian Chadd */ 1671bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1681bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1691bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1701bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 17193efdc63SAdrian Chadd 17293efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 17393efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 17493efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 17593efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 17693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 17793efdc63SAdrian Chadd 178831f5dcfSAlexander Motin static void 179831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 180831f5dcfSAlexander Motin { 1817e6ccea3SMarius Strobl 182831f5dcfSAlexander Motin if (error != 0) { 183831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 184831f5dcfSAlexander Motin return; 185831f5dcfSAlexander Motin } 186831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 187831f5dcfSAlexander Motin } 188831f5dcfSAlexander Motin 189d6b3aaf8SOleksandr Tymoshenko static int 190ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 191d6b3aaf8SOleksandr Tymoshenko { 19227d72fe1SBjoern A. Zeeb char buf[128]; 193d6b3aaf8SOleksandr Tymoshenko va_list ap; 194d6b3aaf8SOleksandr Tymoshenko int retval; 195d6b3aaf8SOleksandr Tymoshenko 19627d72fe1SBjoern A. Zeeb /* 19727d72fe1SBjoern A. Zeeb * Make sure we print a single line all together rather than in two 19827d72fe1SBjoern A. Zeeb * halves to avoid console gibberish bingo. 19927d72fe1SBjoern A. Zeeb */ 200d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 20127d72fe1SBjoern A. Zeeb retval = vsnprintf(buf, sizeof(buf), fmt, ap); 202d6b3aaf8SOleksandr Tymoshenko va_end(ap); 20327d72fe1SBjoern A. Zeeb 20427d72fe1SBjoern A. Zeeb retval += printf("%s-slot%d: %s", 20527d72fe1SBjoern A. Zeeb device_get_nameunit(slot->bus), slot->num, buf); 206d6b3aaf8SOleksandr Tymoshenko return (retval); 207d6b3aaf8SOleksandr Tymoshenko } 208d6b3aaf8SOleksandr Tymoshenko 209d00c1f7fSBartlomiej Grzesik static int 210d00c1f7fSBartlomiej Grzesik slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s, 211d00c1f7fSBartlomiej Grzesik const char * fmt, ...) 212d00c1f7fSBartlomiej Grzesik { 213d00c1f7fSBartlomiej Grzesik va_list ap; 214d00c1f7fSBartlomiej Grzesik int retval; 215d00c1f7fSBartlomiej Grzesik 216d00c1f7fSBartlomiej Grzesik retval = sbuf_printf(s, "%s-slot%d: ", device_get_nameunit(slot->bus), slot->num); 217d00c1f7fSBartlomiej Grzesik 218d00c1f7fSBartlomiej Grzesik va_start(ap, fmt); 219d00c1f7fSBartlomiej Grzesik retval += sbuf_vprintf(s, fmt, ap); 220d00c1f7fSBartlomiej Grzesik va_end(ap); 221d00c1f7fSBartlomiej Grzesik 222d00c1f7fSBartlomiej Grzesik return (retval); 223d00c1f7fSBartlomiej Grzesik } 224d00c1f7fSBartlomiej Grzesik 225d00c1f7fSBartlomiej Grzesik static void 226d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s) 227d00c1f7fSBartlomiej Grzesik { 228d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "============== REGISTER DUMP ==============\n"); 229d00c1f7fSBartlomiej Grzesik 230d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Sys addr: 0x%08x | Version: 0x%08x\n", 231d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 232d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 233d00c1f7fSBartlomiej Grzesik RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 234d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Argument: 0x%08x | Trn mode: 0x%08x\n", 235d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 236d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Present: 0x%08x | Host ctl: 0x%08x\n", 237d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 238d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Power: 0x%08x | Blk gap: 0x%08x\n", 239d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 240d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Wake-up: 0x%08x | Clock: 0x%08x\n", 241d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 242d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Timeout: 0x%08x | Int stat: 0x%08x\n", 243d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 244d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 245d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 246d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 247d00c1f7fSBartlomiej Grzesik RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 248d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Caps: 0x%08x | Caps2: 0x%08x\n", 249d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 250d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 251d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 252d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 253d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 254d00c1f7fSBartlomiej Grzesik 255d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "===========================================\n"); 256d00c1f7fSBartlomiej Grzesik } 257d00c1f7fSBartlomiej Grzesik 258831f5dcfSAlexander Motin static void 259831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 260831f5dcfSAlexander Motin { 261d00c1f7fSBartlomiej Grzesik struct sbuf s; 2627e6ccea3SMarius Strobl 2633ac5012eSBartlomiej Grzesik if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) { 2643ac5012eSBartlomiej Grzesik slot_printf(slot, "sdhci_dumpregs: Failed to allocate memory for sbuf\n"); 2653ac5012eSBartlomiej Grzesik return; 2663ac5012eSBartlomiej Grzesik } 2673ac5012eSBartlomiej Grzesik 268d00c1f7fSBartlomiej Grzesik sbuf_set_drain(&s, &sbuf_printf_drain, NULL); 269d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(slot, &s); 270d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 271d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 272d00c1f7fSBartlomiej Grzesik } 273831f5dcfSAlexander Motin 274d00c1f7fSBartlomiej Grzesik static int 275d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS) 276d00c1f7fSBartlomiej Grzesik { 277d00c1f7fSBartlomiej Grzesik struct sdhci_slot *slot = arg1; 278d00c1f7fSBartlomiej Grzesik struct sbuf s; 279831f5dcfSAlexander Motin 280d00c1f7fSBartlomiej Grzesik sbuf_new_for_sysctl(&s, NULL, 1024, req); 281d00c1f7fSBartlomiej Grzesik sbuf_putc(&s, '\n'); 282d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(slot, &s); 283d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 284d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 285d00c1f7fSBartlomiej Grzesik 286d00c1f7fSBartlomiej Grzesik return (0); 287d00c1f7fSBartlomiej Grzesik } 288d00c1f7fSBartlomiej Grzesik 289d00c1f7fSBartlomiej Grzesik static void 290d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s) 291d00c1f7fSBartlomiej Grzesik { 292d00c1f7fSBartlomiej Grzesik int host_caps = slot->host.caps; 293d00c1f7fSBartlomiej Grzesik int caps = slot->caps; 294d00c1f7fSBartlomiej Grzesik 295d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, 296d00c1f7fSBartlomiej Grzesik "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 297d00c1f7fSBartlomiej Grzesik slot->max_clk / 1000000, 298d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 299d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 300d00c1f7fSBartlomiej Grzesik ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 301d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 302d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 303d00c1f7fSBartlomiej Grzesik ((caps & SDHCI_CAN_VDD_180) && 304d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 305d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 306d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 307d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 308d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 309d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 310d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 311d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 312d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 313d00c1f7fSBartlomiej Grzesik "removable"); 314d00c1f7fSBartlomiej Grzesik if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 315d00c1f7fSBartlomiej Grzesik MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 316d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "eMMC:%s%s%s%s\n", 317d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 318d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 319d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 320d00c1f7fSBartlomiej Grzesik ((host_caps & 321d00c1f7fSBartlomiej Grzesik (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 322d00c1f7fSBartlomiej Grzesik (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 323d00c1f7fSBartlomiej Grzesik " HS400ES" : ""); 324d00c1f7fSBartlomiej Grzesik if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 325d00c1f7fSBartlomiej Grzesik MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 326d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "UHS-I:%s%s%s%s%s\n", 327d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 328d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 329d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 330d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 331d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 332d00c1f7fSBartlomiej Grzesik if (slot->opt & SDHCI_TUNING_SUPPORTED) 333d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, 334d00c1f7fSBartlomiej Grzesik "Re-tuning count %d secs, mode %d\n", 335d00c1f7fSBartlomiej Grzesik slot->retune_count, slot->retune_mode + 1); 336d00c1f7fSBartlomiej Grzesik } 337d00c1f7fSBartlomiej Grzesik 338d00c1f7fSBartlomiej Grzesik static void 339d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(struct sdhci_slot *slot) 340d00c1f7fSBartlomiej Grzesik { 341d00c1f7fSBartlomiej Grzesik struct sbuf s; 342d00c1f7fSBartlomiej Grzesik 3433ac5012eSBartlomiej Grzesik if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) { 3443ac5012eSBartlomiej Grzesik slot_printf(slot, "sdhci_dumpcaps: Failed to allocate memory for sbuf\n"); 3453ac5012eSBartlomiej Grzesik return; 3463ac5012eSBartlomiej Grzesik } 3473ac5012eSBartlomiej Grzesik 348d00c1f7fSBartlomiej Grzesik sbuf_set_drain(&s, &sbuf_printf_drain, NULL); 349d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(slot, &s); 350d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 351d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 352d00c1f7fSBartlomiej Grzesik } 353d00c1f7fSBartlomiej Grzesik 354d00c1f7fSBartlomiej Grzesik static int 355d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS) 356d00c1f7fSBartlomiej Grzesik { 357d00c1f7fSBartlomiej Grzesik struct sdhci_slot *slot = arg1; 358d00c1f7fSBartlomiej Grzesik struct sbuf s; 359d00c1f7fSBartlomiej Grzesik 360d00c1f7fSBartlomiej Grzesik sbuf_new_for_sysctl(&s, NULL, 1024, req); 361d00c1f7fSBartlomiej Grzesik sbuf_putc(&s, '\n'); 362d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(slot, &s); 363d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 364d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 365d00c1f7fSBartlomiej Grzesik 366d00c1f7fSBartlomiej Grzesik return (0); 367831f5dcfSAlexander Motin } 368831f5dcfSAlexander Motin 369aca38eabSMarius Strobl static uint32_t 370ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot) 371aca38eabSMarius Strobl { 372aca38eabSMarius Strobl uint32_t intmask; 373aca38eabSMarius Strobl 374aca38eabSMarius Strobl intmask = 0; 37578f8baa8SMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 376aca38eabSMarius Strobl intmask |= SDHCI_INT_TUNEERR; 377aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 378aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_3) 379aca38eabSMarius Strobl intmask |= SDHCI_INT_RETUNE; 380aca38eabSMarius Strobl } 381aca38eabSMarius Strobl return (intmask); 382aca38eabSMarius Strobl } 383aca38eabSMarius Strobl 384831f5dcfSAlexander Motin static void 385831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 386831f5dcfSAlexander Motin { 387831f5dcfSAlexander Motin 388b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 389831f5dcfSAlexander Motin 390831f5dcfSAlexander Motin /* Enable interrupts. */ 391831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 392831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 393831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 394831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 395831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 3963685b398SWarner Losh SDHCI_INT_ACMD12ERR; 397639f59f0SIan Lepore 398639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 399639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 400639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 401639f59f0SIan Lepore } 402639f59f0SIan Lepore 403cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 404831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 405831f5dcfSAlexander Motin } 406831f5dcfSAlexander Motin 407831f5dcfSAlexander Motin static void 408831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 409831f5dcfSAlexander Motin { 41093efdc63SAdrian Chadd uint32_t clk_base; 41193efdc63SAdrian Chadd uint32_t clk_sel; 412831f5dcfSAlexander Motin uint32_t res; 413831f5dcfSAlexander Motin uint16_t clk; 4148f3b7d56SOleksandr Tymoshenko uint16_t div; 415831f5dcfSAlexander Motin int timeout; 416831f5dcfSAlexander Motin 417831f5dcfSAlexander Motin if (clock == slot->clock) 418831f5dcfSAlexander Motin return; 419e00774a9SSøren Schmidt clock = SDHCI_SET_CLOCK(slot->bus, slot, clock); 420831f5dcfSAlexander Motin slot->clock = clock; 421831f5dcfSAlexander Motin 422831f5dcfSAlexander Motin /* Turn off the clock. */ 4234ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 4244ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 425b440e965SMarius Strobl /* If no clock requested - leave it so. */ 426831f5dcfSAlexander Motin if (clock == 0) 427831f5dcfSAlexander Motin return; 428ceb9e9f7SIan Lepore 42993efdc63SAdrian Chadd /* Determine the clock base frequency */ 43093efdc63SAdrian Chadd clk_base = slot->max_clk; 43193efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 4321bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 4331bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 43493efdc63SAdrian Chadd 4351bacf3beSMarius Strobl /* 4361bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 4371bacf3beSMarius Strobl */ 43893efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 43993efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 4401bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 4411bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44293efdc63SAdrian Chadd } else { 4431bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 4441bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44593efdc63SAdrian Chadd } 44693efdc63SAdrian Chadd 44793efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 44893efdc63SAdrian Chadd } 44993efdc63SAdrian Chadd 450ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 451ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 452ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 453ceb9e9f7SIan Lepore 4548f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 455831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 45693efdc63SAdrian Chadd res = clk_base; 45757677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 458831f5dcfSAlexander Motin if (res <= clock) 459831f5dcfSAlexander Motin break; 460831f5dcfSAlexander Motin res >>= 1; 461831f5dcfSAlexander Motin } 462831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 4638f3b7d56SOleksandr Tymoshenko div >>= 1; 464c11bbc7dSMarius Strobl } else { 4658f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 46693efdc63SAdrian Chadd if (clock >= clk_base) 46757677a3aSOleksandr Tymoshenko div = 0; 4688f3b7d56SOleksandr Tymoshenko else { 46957677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 47093efdc63SAdrian Chadd if ((clk_base / div) <= clock) 4718f3b7d56SOleksandr Tymoshenko break; 4728f3b7d56SOleksandr Tymoshenko } 4738f3b7d56SOleksandr Tymoshenko } 4748f3b7d56SOleksandr Tymoshenko div >>= 1; 4758f3b7d56SOleksandr Tymoshenko } 4768f3b7d56SOleksandr Tymoshenko 4778f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 47893efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 47993efdc63SAdrian Chadd div, clock, clk_base); 4808f3b7d56SOleksandr Tymoshenko 481831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4828f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4838f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4848f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4858f3b7d56SOleksandr Tymoshenko 486831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 487831f5dcfSAlexander Motin /* Enable clock. */ 488831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 489831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 490831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 491831f5dcfSAlexander Motin timeout = 10; 492831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 493831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 494831f5dcfSAlexander Motin if (timeout == 0) { 495831f5dcfSAlexander Motin slot_printf(slot, 496831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 497831f5dcfSAlexander Motin sdhci_dumpregs(slot); 498831f5dcfSAlexander Motin return; 499831f5dcfSAlexander Motin } 500831f5dcfSAlexander Motin timeout--; 501831f5dcfSAlexander Motin DELAY(1000); 502831f5dcfSAlexander Motin } 503831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 504831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 505831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 506831f5dcfSAlexander Motin } 507831f5dcfSAlexander Motin 508831f5dcfSAlexander Motin static void 509831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 510831f5dcfSAlexander Motin { 51185083a80SMarius Strobl int i; 512831f5dcfSAlexander Motin uint8_t pwr; 513831f5dcfSAlexander Motin 514831f5dcfSAlexander Motin if (slot->power == power) 515831f5dcfSAlexander Motin return; 516d6b3aaf8SOleksandr Tymoshenko 517831f5dcfSAlexander Motin slot->power = power; 518831f5dcfSAlexander Motin 519831f5dcfSAlexander Motin /* Turn off the power. */ 520831f5dcfSAlexander Motin pwr = 0; 521831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 522b440e965SMarius Strobl /* If power down requested - leave it so. */ 523831f5dcfSAlexander Motin if (power == 0) 524831f5dcfSAlexander Motin return; 525831f5dcfSAlexander Motin /* Set voltage. */ 526831f5dcfSAlexander Motin switch (1 << power) { 527831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 528831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 529831f5dcfSAlexander Motin break; 530831f5dcfSAlexander Motin case MMC_OCR_290_300: 531831f5dcfSAlexander Motin case MMC_OCR_300_310: 532831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 533831f5dcfSAlexander Motin break; 534831f5dcfSAlexander Motin case MMC_OCR_320_330: 535831f5dcfSAlexander Motin case MMC_OCR_330_340: 536831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 537831f5dcfSAlexander Motin break; 538831f5dcfSAlexander Motin } 539831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 54085083a80SMarius Strobl /* 54185083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 54285083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 5438022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 54485083a80SMarius Strobl */ 545831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 54685083a80SMarius Strobl for (i = 0; i < 20; i++) { 547831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 54885083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 54985083a80SMarius Strobl break; 55085083a80SMarius Strobl DELAY(100); 55185083a80SMarius Strobl } 55285083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 5534d52f81dSIan Lepore slot_printf(slot, "Bus power failed to enable\n"); 554a2832f9fSMarius Strobl 555a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 556a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 557a2832f9fSMarius Strobl DELAY(10); 558a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 559a2832f9fSMarius Strobl DELAY(300); 560a2832f9fSMarius Strobl } 561831f5dcfSAlexander Motin } 562831f5dcfSAlexander Motin 563831f5dcfSAlexander Motin static void 564831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 565831f5dcfSAlexander Motin { 566831f5dcfSAlexander Motin uint32_t data; 567831f5dcfSAlexander Motin char *buffer; 568831f5dcfSAlexander Motin size_t left; 569831f5dcfSAlexander Motin 570831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 571831f5dcfSAlexander Motin buffer += slot->offset; 572831f5dcfSAlexander Motin /* Transfer one block at a time. */ 5735d5ae066SIlya Bakulin #ifdef MMCCAM 5745d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) 5755d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 5765d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 5775d5ae066SIlya Bakulin else 5785d5ae066SIlya Bakulin #endif 579831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 580831f5dcfSAlexander Motin slot->offset += left; 581831f5dcfSAlexander Motin 582831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 583d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 584831f5dcfSAlexander Motin DELAY(10); 585ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 586831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 587831f5dcfSAlexander Motin while (left > 3) { 588831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 589831f5dcfSAlexander Motin buffer[0] = data; 590831f5dcfSAlexander Motin buffer[1] = (data >> 8); 591831f5dcfSAlexander Motin buffer[2] = (data >> 16); 592831f5dcfSAlexander Motin buffer[3] = (data >> 24); 593831f5dcfSAlexander Motin buffer += 4; 594831f5dcfSAlexander Motin left -= 4; 595831f5dcfSAlexander Motin } 596831f5dcfSAlexander Motin } else { 597d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 598831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 599831f5dcfSAlexander Motin left &= 3; 600831f5dcfSAlexander Motin } 601831f5dcfSAlexander Motin /* Handle uneven size case. */ 602831f5dcfSAlexander Motin if (left > 0) { 603831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 604831f5dcfSAlexander Motin while (left > 0) { 605831f5dcfSAlexander Motin *(buffer++) = data; 606831f5dcfSAlexander Motin data >>= 8; 607831f5dcfSAlexander Motin left--; 608831f5dcfSAlexander Motin } 609831f5dcfSAlexander Motin } 610831f5dcfSAlexander Motin } 611831f5dcfSAlexander Motin 612831f5dcfSAlexander Motin static void 613831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 614831f5dcfSAlexander Motin { 615831f5dcfSAlexander Motin uint32_t data = 0; 616831f5dcfSAlexander Motin char *buffer; 617831f5dcfSAlexander Motin size_t left; 618831f5dcfSAlexander Motin 619831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 620831f5dcfSAlexander Motin buffer += slot->offset; 621831f5dcfSAlexander Motin /* Transfer one block at a time. */ 6225d5ae066SIlya Bakulin #ifdef MMCCAM 6235d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) { 6245d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 6255d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 6265d5ae066SIlya Bakulin } else 6275d5ae066SIlya Bakulin #endif 628831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 629831f5dcfSAlexander Motin slot->offset += left; 630831f5dcfSAlexander Motin 631ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 632831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 633831f5dcfSAlexander Motin while (left > 3) { 634831f5dcfSAlexander Motin data = buffer[0] + 635831f5dcfSAlexander Motin (buffer[1] << 8) + 636831f5dcfSAlexander Motin (buffer[2] << 16) + 637831f5dcfSAlexander Motin (buffer[3] << 24); 638831f5dcfSAlexander Motin left -= 4; 639831f5dcfSAlexander Motin buffer += 4; 640831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 641831f5dcfSAlexander Motin } 642831f5dcfSAlexander Motin } else { 643d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 644831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 645831f5dcfSAlexander Motin left &= 3; 646831f5dcfSAlexander Motin } 647831f5dcfSAlexander Motin /* Handle uneven size case. */ 648831f5dcfSAlexander Motin if (left > 0) { 649831f5dcfSAlexander Motin while (left > 0) { 650831f5dcfSAlexander Motin data <<= 8; 651831f5dcfSAlexander Motin data += *(buffer++); 652831f5dcfSAlexander Motin left--; 653831f5dcfSAlexander Motin } 654831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 655831f5dcfSAlexander Motin } 656831f5dcfSAlexander Motin } 657831f5dcfSAlexander Motin 658831f5dcfSAlexander Motin static void 659831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 660831f5dcfSAlexander Motin { 661831f5dcfSAlexander Motin 662831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 663831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 664831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 665831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 666831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 667831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 668831f5dcfSAlexander Motin break; 669831f5dcfSAlexander Motin } 670831f5dcfSAlexander Motin } else { 671831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 672831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 673831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 674831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 675831f5dcfSAlexander Motin break; 676831f5dcfSAlexander Motin } 677831f5dcfSAlexander Motin } 678831f5dcfSAlexander Motin } 679831f5dcfSAlexander Motin 680831f5dcfSAlexander Motin static void 6817e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 682831f5dcfSAlexander Motin { 683831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 68403d49ffcSJohn Baldwin #ifndef MMCCAM 6857e6ccea3SMarius Strobl device_t d; 68603d49ffcSJohn Baldwin #endif 687831f5dcfSAlexander Motin 688831f5dcfSAlexander Motin SDHCI_LOCK(slot); 6896e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 690a94a63f0SWarner Losh #ifdef MMCCAM 691a94a63f0SWarner Losh if (slot->card_present == 0) { 692a94a63f0SWarner Losh #else 693831f5dcfSAlexander Motin if (slot->dev == NULL) { 694a94a63f0SWarner Losh #endif 695831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 696639f59f0SIan Lepore if (bootverbose || sdhci_debug) 697639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 698a94a63f0SWarner Losh #ifdef MMCCAM 699a94a63f0SWarner Losh slot->card_present = 1; 700c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 701a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 702a94a63f0SWarner Losh #else 703831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 704*aaf0a730SJohn Baldwin bus_topo_lock(); 705*aaf0a730SJohn Baldwin d = slot->dev = device_add_child(slot->bus, "mmc", DEVICE_UNIT_ANY); 706aca38eabSMarius Strobl if (d) { 707aca38eabSMarius Strobl device_set_ivars(d, slot); 708aca38eabSMarius Strobl (void)device_probe_and_attach(d); 709aca38eabSMarius Strobl } 710*aaf0a730SJohn Baldwin bus_topo_unlock(); 711a94a63f0SWarner Losh #endif 712831f5dcfSAlexander Motin } else 713831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 714831f5dcfSAlexander Motin } else { 715a94a63f0SWarner Losh #ifdef MMCCAM 716a94a63f0SWarner Losh if (slot->card_present == 1) { 717a94a63f0SWarner Losh #else 718831f5dcfSAlexander Motin if (slot->dev != NULL) { 71903d49ffcSJohn Baldwin d = slot->dev; 720a94a63f0SWarner Losh #endif 721831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 722639f59f0SIan Lepore if (bootverbose || sdhci_debug) 723639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 724831f5dcfSAlexander Motin slot->dev = NULL; 725a94a63f0SWarner Losh #ifdef MMCCAM 726a94a63f0SWarner Losh slot->card_present = 0; 727c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 728a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 729a94a63f0SWarner Losh #else 730aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 731cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 732aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 733aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 734831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 735aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 736*aaf0a730SJohn Baldwin bus_topo_lock(); 737d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 738*aaf0a730SJohn Baldwin bus_topo_unlock(); 739a94a63f0SWarner Losh #endif 740831f5dcfSAlexander Motin } else 741831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 742831f5dcfSAlexander Motin } 743831f5dcfSAlexander Motin } 744831f5dcfSAlexander Motin 745b8bf08b1SIan Lepore static void 746b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 747639f59f0SIan Lepore { 748639f59f0SIan Lepore bool was_present; 749639f59f0SIan Lepore 750639f59f0SIan Lepore /* 751639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 752639f59f0SIan Lepore * create the child device after a short delay. The delay is to 753639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 754639f59f0SIan Lepore * before the other pins have made good contact). 755639f59f0SIan Lepore * 756639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 757639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 758639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 759639f59f0SIan Lepore * that happens by deleting and recreating the child device. 760639f59f0SIan Lepore */ 761a94a63f0SWarner Losh #ifdef MMCCAM 762a94a63f0SWarner Losh was_present = slot->card_present; 763a94a63f0SWarner Losh #else 764639f59f0SIan Lepore was_present = slot->dev != NULL; 765a94a63f0SWarner Losh #endif 766639f59f0SIan Lepore if (!was_present && is_present) { 767*aaf0a730SJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus, 768639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 769639f59f0SIan Lepore } else if (was_present && !is_present) { 770*aaf0a730SJohn Baldwin taskqueue_enqueue(taskqueue_bus, &slot->card_task); 771639f59f0SIan Lepore } 772b8bf08b1SIan Lepore } 773b8bf08b1SIan Lepore 774b8bf08b1SIan Lepore void 775b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 776b8bf08b1SIan Lepore { 777b8bf08b1SIan Lepore 778b8bf08b1SIan Lepore SDHCI_LOCK(slot); 779b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 780639f59f0SIan Lepore SDHCI_UNLOCK(slot); 781639f59f0SIan Lepore } 782639f59f0SIan Lepore 783639f59f0SIan Lepore static void 784639f59f0SIan Lepore sdhci_card_poll(void *arg) 785639f59f0SIan Lepore { 786639f59f0SIan Lepore struct sdhci_slot *slot = arg; 787639f59f0SIan Lepore 788639f59f0SIan Lepore sdhci_handle_card_present(slot, 789639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 790639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 791639f59f0SIan Lepore sdhci_card_poll, slot); 792639f59f0SIan Lepore } 793639f59f0SIan Lepore 794ab00a509SMarius Strobl static int 795016f9657SMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot) 796ab00a509SMarius Strobl { 797ab00a509SMarius Strobl int err; 798ab00a509SMarius Strobl 799ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 800cd853791SKonstantin Belousov if (maxphys <= 1024 * 4) 801ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 802cd853791SKonstantin Belousov else if (maxphys <= 1024 * 8) 803ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 804cd853791SKonstantin Belousov else if (maxphys <= 1024 * 16) 805ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 806cd853791SKonstantin Belousov else if (maxphys <= 1024 * 32) 807ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 808cd853791SKonstantin Belousov else if (maxphys <= 1024 * 64) 809ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 810cd853791SKonstantin Belousov else if (maxphys <= 1024 * 128) 811ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 812cd853791SKonstantin Belousov else if (maxphys <= 1024 * 256) 813ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 814ab00a509SMarius Strobl else 815ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 816ab00a509SMarius Strobl } 817ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 818ab00a509SMarius Strobl 819ab00a509SMarius Strobl /* 820ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 821ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 822ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 823ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 824ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 825ab00a509SMarius Strobl * be aligned to the SDMA boundary. 826ab00a509SMarius Strobl */ 827ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 828016f9657SMarcin Wojtas 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 829ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 830ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 831ab00a509SMarius Strobl if (err != 0) { 832ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 833ab00a509SMarius Strobl return (err); 834ab00a509SMarius Strobl } 835ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 836ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 837ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 838ab00a509SMarius Strobl if (err != 0) { 839ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 840ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 841ab00a509SMarius Strobl return (err); 842ab00a509SMarius Strobl } 843ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 844ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 845ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 846ab00a509SMarius Strobl &slot->paddr, 0); 847ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 848ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 849ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 850ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 851ab00a509SMarius Strobl if (err) 852ab00a509SMarius Strobl return (err); 853ab00a509SMarius Strobl else 854ab00a509SMarius Strobl return (EFAULT); 855ab00a509SMarius Strobl } 856ab00a509SMarius Strobl 857ab00a509SMarius Strobl return (0); 858ab00a509SMarius Strobl } 859ab00a509SMarius Strobl 860ab00a509SMarius Strobl static void 861ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 862ab00a509SMarius Strobl { 863ab00a509SMarius Strobl 864ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 865ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 866ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 867ab00a509SMarius Strobl } 868ab00a509SMarius Strobl 869d6b3aaf8SOleksandr Tymoshenko int 870d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 871831f5dcfSAlexander Motin { 872aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 873aca38eabSMarius Strobl kobj_method_t *kobj_method; 8740f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 875d6b3aaf8SOleksandr Tymoshenko int err; 876d00c1f7fSBartlomiej Grzesik char node_name[8]; 877d00c1f7fSBartlomiej Grzesik struct sysctl_oid *node_oid; 878831f5dcfSAlexander Motin 879831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 880a94a63f0SWarner Losh 881d6b3aaf8SOleksandr Tymoshenko slot->num = num; 882d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 883d6b3aaf8SOleksandr Tymoshenko 884d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 885d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8860f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8878f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8880f34084fSMarius Strobl caps2 = slot->caps2; 8890f34084fSMarius Strobl } else { 890831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8910f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8920f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8930f34084fSMarius Strobl else 8940f34084fSMarius Strobl caps2 = 0; 8950f34084fSMarius Strobl } 8967fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8977fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8987fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 899ab00a509SMarius Strobl slot_printf(slot, 9007fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 9017fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 9027fcf4780SMarius Strobl return (ENXIO); 9037fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 9047fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 9057fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 9067fcf4780SMarius Strobl } 9077fcf4780SMarius Strobl } 908831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 90933aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 91087a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 91187a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 91233aad34dSOleksandr Tymoshenko else 91387a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 91487a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 91587a6a871SIan Lepore if (freq != 0) 91687a6a871SIan Lepore slot->max_clk = freq * 1000000; 91787a6a871SIan Lepore /* 91887a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 91987a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 92087a6a871SIan Lepore * with an assumption, so complain about it. 92187a6a871SIan Lepore */ 922831f5dcfSAlexander Motin if (slot->max_clk == 0) { 92387a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 924ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 9251bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 9261bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 927831f5dcfSAlexander Motin } 928a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 9298f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 9308f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 931a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 932a2832f9fSMarius Strobl slot->timeout_clk = 1000; 9338f3b7d56SOleksandr Tymoshenko } else { 9341bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 9351bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 9368f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 9378f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 9388f3b7d56SOleksandr Tymoshenko } 93987a6a871SIan Lepore /* 94087a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 94187a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 94287a6a871SIan Lepore * max timeout, but still mention it. 94387a6a871SIan Lepore */ 944831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 945ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 946ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 947ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 948831f5dcfSAlexander Motin } 949831f5dcfSAlexander Motin 95057677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 951831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 952831f5dcfSAlexander Motin slot->host.host_ocr = 0; 953831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 954831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 955831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 956831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 95749dfdf63SIan Lepore /* 95849dfdf63SIan Lepore * 1.8V VDD is not supposed to be used for removable cards. Hardware 95949dfdf63SIan Lepore * prior to v3.0 had no way to indicate embedded slots, but did 96049dfdf63SIan Lepore * sometimes support 1.8v for non-removable devices. 96149dfdf63SIan Lepore */ 96249dfdf63SIan Lepore if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 || 96349dfdf63SIan Lepore (slot->opt & SDHCI_SLOT_EMBEDDED))) 964831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 965831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 966ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 967831f5dcfSAlexander Motin "support voltages.\n"); 968831f5dcfSAlexander Motin } 969aca38eabSMarius Strobl 9705652be30SMarcin Wojtas host_caps = slot->host.caps; 9715652be30SMarcin Wojtas host_caps |= MMC_CAP_4_BIT_DATA; 9722d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 9730f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 974831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 9750f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 97672dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9770f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 97872dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9790f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 980aca38eabSMarius Strobl 981aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9820f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9830f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9840f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9850f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9860f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9870f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9880f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9890f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9900f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9910f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9920f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9930f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9940f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9950f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9960f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9970f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 998835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 999835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 1000835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 1001aca38eabSMarius Strobl 1002aca38eabSMarius Strobl /* 1003aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 1004aca38eabSMarius Strobl * default NULL implementation. 1005aca38eabSMarius Strobl */ 1006aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 1007aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1008aca38eabSMarius Strobl kobj_desc); 1009aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1010aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1011aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 1012aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 1013aca38eabSMarius Strobl 1014aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 1015aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 1016aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 1017aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 1018aca38eabSMarius Strobl 1019aca38eabSMarius Strobl /* 1020aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 1021aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 1022aca38eabSMarius Strobl */ 1023aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 1024aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1025aca38eabSMarius Strobl kobj_desc); 1026aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1027aca38eabSMarius Strobl goto no_tuning; 1028aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 1029aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1030aca38eabSMarius Strobl kobj_desc); 1031aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 1032aca38eabSMarius Strobl no_tuning: 1033aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 1034aca38eabSMarius Strobl } 1035aca38eabSMarius Strobl 1036aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 1037aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 1038aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 1039aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 1040aca38eabSMarius Strobl M_WAITOK); 1041aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 1042aca38eabSMarius Strobl M_WAITOK); 1043aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 1044aca38eabSMarius Strobl M_WAITOK); 1045aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 1046aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 1047aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 1048aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 1049aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 1050aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 1051aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 1052aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 1053ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 1054aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 1055aca38eabSMarius Strobl slot->retune_count = 1; 1056aca38eabSMarius Strobl } else if (slot->retune_count != 0) 1057aca38eabSMarius Strobl slot->retune_count = 1058aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 1059aca38eabSMarius Strobl } 1060aca38eabSMarius Strobl } 1061aca38eabSMarius Strobl 1062aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 1063aca38eabSMarius Strobl 1064aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 10650f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 10660f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1067aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 10680f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 10690f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 1070aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 1071aca38eabSMarius Strobl 1072aca38eabSMarius Strobl /* 1073aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 1074aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 1075aca38eabSMarius Strobl * generic SDHCI implementation. 1076aca38eabSMarius Strobl */ 1077aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1078aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1079aca38eabSMarius Strobl kobj_desc); 1080aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1081aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1082aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1083aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1084aca38eabSMarius Strobl 1085aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1086f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10870f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1088f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10890f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1090f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10910f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10920f34084fSMarius Strobl slot->host.caps = host_caps; 10930f34084fSMarius Strobl 1094831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1095831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1096831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1097d6b3aaf8SOleksandr Tymoshenko 1098d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1099831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1100d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1101831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1102a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1103a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1104831f5dcfSAlexander Motin 1105c3a0f75aSOleksandr Tymoshenko /* 1106c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1107c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1108c3a0f75aSOleksandr Tymoshenko */ 1109c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1110c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1111c3a0f75aSOleksandr Tymoshenko 1112ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 1113016f9657SMarcin Wojtas err = sdhci_dma_alloc(slot); 1114ab00a509SMarius Strobl if (err != 0) { 1115ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1116ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1117ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1118ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1119ab00a509SMarius Strobl } 1120ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1121ab00a509SMarius Strobl return (err); 1122ab00a509SMarius Strobl } 1123ab00a509SMarius Strobl } 1124ab00a509SMarius Strobl 11255b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 1126d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(slot); 1127831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1128831f5dcfSAlexander Motin } 1129831f5dcfSAlexander Motin 1130ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1131ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1132ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 11334d52f81dSIan Lepore "timeout", CTLFLAG_RWTUN, &slot->timeout, 0, 1134ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1135831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1136*aaf0a730SJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &slot->card_delayed_task, 0, 1137639f59f0SIan Lepore sdhci_card_task, slot); 1138639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1139e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1140aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1141ba6fc1c7SLuiz Otavio O Souza 1142639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1143639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1144639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1145639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1146639f59f0SIan Lepore } 1147639f59f0SIan Lepore 1148aca38eabSMarius Strobl sdhci_init(slot); 1149aca38eabSMarius Strobl 1150d00c1f7fSBartlomiej Grzesik snprintf(node_name, sizeof(node_name), "slot%d", slot->num); 1151d00c1f7fSBartlomiej Grzesik 1152d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1153d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1154d00c1f7fSBartlomiej Grzesik OID_AUTO, node_name, CTLFLAG_RW, 0, "slot specific node"); 1155d00c1f7fSBartlomiej Grzesik 115621525fe0SJustin Hibbits SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 115721525fe0SJustin Hibbits OID_AUTO, "quirks", CTLFLAG_RD, &slot->quirks, 0, "Slot quirks"); 115821525fe0SJustin Hibbits 1159d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1160d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(node_oid), OID_AUTO, "debug", CTLFLAG_RW, 0, 1161d00c1f7fSBartlomiej Grzesik "Debugging node"); 1162d00c1f7fSBartlomiej Grzesik 1163d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1164d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpregs", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1165d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpregs, 1166d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI registers"); 1167d00c1f7fSBartlomiej Grzesik 1168d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1169d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpcaps", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1170d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpcaps, 1171d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI capabilites"); 1172d00c1f7fSBartlomiej Grzesik 1173831f5dcfSAlexander Motin return (0); 1174831f5dcfSAlexander Motin } 1175831f5dcfSAlexander Motin 1176d91f1a10SIlya Bakulin #ifndef MMCCAM 1177d6b3aaf8SOleksandr Tymoshenko void 1178d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1179831f5dcfSAlexander Motin { 11807e6ccea3SMarius Strobl 1181d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1182d6b3aaf8SOleksandr Tymoshenko } 1183d91f1a10SIlya Bakulin #endif 1184831f5dcfSAlexander Motin 1185d6b3aaf8SOleksandr Tymoshenko int 1186d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1187d6b3aaf8SOleksandr Tymoshenko { 1188831f5dcfSAlexander Motin device_t d; 1189831f5dcfSAlexander Motin 1190e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1191639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1192aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1193*aaf0a730SJohn Baldwin taskqueue_drain(taskqueue_bus, &slot->card_task); 1194*aaf0a730SJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &slot->card_delayed_task); 1195831f5dcfSAlexander Motin 1196831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1197831f5dcfSAlexander Motin d = slot->dev; 1198831f5dcfSAlexander Motin slot->dev = NULL; 1199831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1200831f5dcfSAlexander Motin if (d != NULL) 1201d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1202831f5dcfSAlexander Motin 1203831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1204b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1205831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1206ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1207ab00a509SMarius Strobl sdhci_dma_free(slot); 1208aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1209aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1210aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1211aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1212aca38eabSMarius Strobl } 1213d6b3aaf8SOleksandr Tymoshenko 1214831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1215d6b3aaf8SOleksandr Tymoshenko 1216831f5dcfSAlexander Motin return (0); 1217831f5dcfSAlexander Motin } 1218831f5dcfSAlexander Motin 1219d6b3aaf8SOleksandr Tymoshenko int 1220d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 122192bf0e27SAlexander Motin { 12227e6ccea3SMarius Strobl 1223aca38eabSMarius Strobl /* 1224aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1225aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1226aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1227aca38eabSMarius Strobl */ 1228aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1229aca38eabSMarius Strobl SDHCI_LOCK(slot); 1230aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1231b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1232aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 123392bf0e27SAlexander Motin 123492bf0e27SAlexander Motin return (0); 123592bf0e27SAlexander Motin } 123692bf0e27SAlexander Motin 1237d6b3aaf8SOleksandr Tymoshenko int 1238d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 123992bf0e27SAlexander Motin { 12407e6ccea3SMarius Strobl 1241aca38eabSMarius Strobl SDHCI_LOCK(slot); 1242d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1243aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 124492bf0e27SAlexander Motin 1245d6b3aaf8SOleksandr Tymoshenko return (0); 124692bf0e27SAlexander Motin } 124792bf0e27SAlexander Motin 1248b8f94506SArtur Rojek void 1249b8f94506SArtur Rojek sdhci_generic_reset(device_t brdev __unused, struct sdhci_slot *slot, 1250b8f94506SArtur Rojek uint8_t mask) 1251b8f94506SArtur Rojek { 1252b8f94506SArtur Rojek int timeout; 1253b8f94506SArtur Rojek uint32_t clock; 1254b8f94506SArtur Rojek 1255b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 1256b8f94506SArtur Rojek if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 1257b8f94506SArtur Rojek return; 1258b8f94506SArtur Rojek } 1259b8f94506SArtur Rojek 1260b8f94506SArtur Rojek /* Some controllers need this kick or reset won't work. */ 1261b8f94506SArtur Rojek if ((mask & SDHCI_RESET_ALL) == 0 && 1262b8f94506SArtur Rojek (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 1263b8f94506SArtur Rojek /* This is to force an update */ 1264b8f94506SArtur Rojek clock = slot->clock; 1265b8f94506SArtur Rojek slot->clock = 0; 1266b8f94506SArtur Rojek sdhci_set_clock(slot, clock); 1267b8f94506SArtur Rojek } 1268b8f94506SArtur Rojek 1269b8f94506SArtur Rojek if (mask & SDHCI_RESET_ALL) { 1270b8f94506SArtur Rojek slot->clock = 0; 1271b8f94506SArtur Rojek slot->power = 0; 1272b8f94506SArtur Rojek } 1273b8f94506SArtur Rojek 1274b8f94506SArtur Rojek WR1(slot, SDHCI_SOFTWARE_RESET, mask); 1275b8f94506SArtur Rojek 1276b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 1277b8f94506SArtur Rojek /* 1278b8f94506SArtur Rojek * Resets on TI OMAPs and AM335x are incompatible with SDHCI 1279b8f94506SArtur Rojek * specification. The reset bit has internal propagation delay, 1280b8f94506SArtur Rojek * so a fast read after write returns 0 even if reset process is 1281b8f94506SArtur Rojek * in progress. The workaround is to poll for 1 before polling 1282b8f94506SArtur Rojek * for 0. In the worst case, if we miss seeing it asserted the 1283b8f94506SArtur Rojek * time we spent waiting is enough to ensure the reset finishes. 1284b8f94506SArtur Rojek */ 1285b8f94506SArtur Rojek timeout = 10000; 1286b8f94506SArtur Rojek while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 1287b8f94506SArtur Rojek if (timeout <= 0) 1288b8f94506SArtur Rojek break; 1289b8f94506SArtur Rojek timeout--; 1290b8f94506SArtur Rojek DELAY(1); 1291b8f94506SArtur Rojek } 1292b8f94506SArtur Rojek } 1293b8f94506SArtur Rojek 1294b8f94506SArtur Rojek /* Wait max 100 ms */ 1295b8f94506SArtur Rojek timeout = 10000; 1296b8f94506SArtur Rojek /* Controller clears the bits when it's done */ 1297b8f94506SArtur Rojek while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 1298b8f94506SArtur Rojek if (timeout <= 0) { 1299b8f94506SArtur Rojek slot_printf(slot, "Reset 0x%x never completed.\n", 1300b8f94506SArtur Rojek mask); 1301b8f94506SArtur Rojek sdhci_dumpregs(slot); 1302b8f94506SArtur Rojek return; 1303b8f94506SArtur Rojek } 1304b8f94506SArtur Rojek timeout--; 1305b8f94506SArtur Rojek DELAY(10); 1306b8f94506SArtur Rojek } 1307b8f94506SArtur Rojek } 1308b8f94506SArtur Rojek 130957677a3aSOleksandr Tymoshenko uint32_t 1310b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 131157677a3aSOleksandr Tymoshenko { 13127e6ccea3SMarius Strobl 131357677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 131457677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 131557677a3aSOleksandr Tymoshenko else 131657677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 131757677a3aSOleksandr Tymoshenko } 131857677a3aSOleksandr Tymoshenko 13196e37fb2bSIan Lepore bool 1320b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 13216e37fb2bSIan Lepore { 13226e37fb2bSIan Lepore 1323639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1324639f59f0SIan Lepore return true; 1325639f59f0SIan Lepore 13266e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 13276e37fb2bSIan Lepore } 13286e37fb2bSIan Lepore 13290f34084fSMarius Strobl void 13300f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 13310f34084fSMarius Strobl { 1332ab00a509SMarius Strobl const struct mmc_ios *ios; 13330f34084fSMarius Strobl uint16_t hostctrl2; 13340f34084fSMarius Strobl 13350f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 13360f34084fSMarius Strobl return; 13370f34084fSMarius Strobl 1338aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 13390f34084fSMarius Strobl ios = &slot->host.ios; 13400f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13410f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13420f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1343aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 13440f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 13450f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 13460f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1347aca38eabSMarius Strobl else 13480f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1349aca38eabSMarius Strobl } 13500f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 13510f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 13520f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 13530f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 13540f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 13550f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 13560f34084fSMarius Strobl else 13570f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 13580f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 13590f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 13600f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13610f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 13620f34084fSMarius Strobl } 13630f34084fSMarius Strobl 1364d6b3aaf8SOleksandr Tymoshenko int 1365d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1366831f5dcfSAlexander Motin { 1367831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1368831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1369831f5dcfSAlexander Motin 1370831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1371831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1372831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1373831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1374831f5dcfSAlexander Motin sdhci_init(slot); 1375831f5dcfSAlexander Motin } 1376831f5dcfSAlexander Motin /* Configure the bus. */ 1377831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1378831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 13792d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 13802d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1381831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13822d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 13832d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13842d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 13852d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 13862d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13872d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13882d1731b8SIan Lepore } else { 13892d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 13902d1731b8SIan Lepore } 13910f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1392bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1393831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1394831f5dcfSAlexander Motin else 1395831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1396831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 13970f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1398831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1399d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1400b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1401b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1402831f5dcfSAlexander Motin 1403831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1404831f5dcfSAlexander Motin return (0); 1405831f5dcfSAlexander Motin } 1406831f5dcfSAlexander Motin 14070f34084fSMarius Strobl int 14080f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 14090f34084fSMarius Strobl { 14100f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 14110f34084fSMarius Strobl enum mmc_vccq vccq; 14120f34084fSMarius Strobl int err; 14130f34084fSMarius Strobl uint16_t hostctrl2; 14140f34084fSMarius Strobl 14150f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 14160f34084fSMarius Strobl return (0); 14170f34084fSMarius Strobl 14180f34084fSMarius Strobl err = 0; 14190f34084fSMarius Strobl vccq = slot->host.ios.vccq; 14200f34084fSMarius Strobl SDHCI_LOCK(slot); 14210f34084fSMarius Strobl sdhci_set_clock(slot, 0); 14220f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14230f34084fSMarius Strobl switch (vccq) { 14240f34084fSMarius Strobl case vccq_330: 14250f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14260f34084fSMarius Strobl goto done; 14270f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 14280f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14290f34084fSMarius Strobl DELAY(5000); 14300f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14310f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14320f34084fSMarius Strobl goto done; 14330f34084fSMarius Strobl err = EAGAIN; 14340f34084fSMarius Strobl break; 14350f34084fSMarius Strobl case vccq_180: 14360f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 14370f34084fSMarius Strobl err = EINVAL; 14380f34084fSMarius Strobl goto done; 14390f34084fSMarius Strobl } 14400f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14410f34084fSMarius Strobl goto done; 14420f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 14430f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14440f34084fSMarius Strobl DELAY(5000); 14450f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14460f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14470f34084fSMarius Strobl goto done; 14480f34084fSMarius Strobl err = EAGAIN; 14490f34084fSMarius Strobl break; 14500f34084fSMarius Strobl default: 14510f34084fSMarius Strobl slot_printf(slot, 14520f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 14530f34084fSMarius Strobl err = EINVAL; 14540f34084fSMarius Strobl break; 14550f34084fSMarius Strobl } 14560f34084fSMarius Strobl done: 14570f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 14580f34084fSMarius Strobl SDHCI_UNLOCK(slot); 14590f34084fSMarius Strobl return (err); 14600f34084fSMarius Strobl } 14610f34084fSMarius Strobl 1462aca38eabSMarius Strobl int 1463aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1464aca38eabSMarius Strobl { 1465aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1466ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1467aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1468aca38eabSMarius Strobl struct mmc_data *tune_data; 1469aca38eabSMarius Strobl uint32_t opcode; 1470aca38eabSMarius Strobl int err; 1471aca38eabSMarius Strobl 1472aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1473aca38eabSMarius Strobl return (0); 1474aca38eabSMarius Strobl 1475aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1476aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1477aca38eabSMarius Strobl SDHCI_LOCK(slot); 1478aca38eabSMarius Strobl switch (ios->timing) { 1479aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1480aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1481aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1482aca38eabSMarius Strobl return (EINVAL); 1483aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1484aca38eabSMarius Strobl /* 1485aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1486aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1487aca38eabSMarius Strobl * expected to be required. 1488aca38eabSMarius Strobl */ 1489aca38eabSMarius Strobl if (hs400) 1490aca38eabSMarius Strobl slot->retune_ticks = 0; 1491aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1492aca38eabSMarius Strobl break; 1493aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1494aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1495aca38eabSMarius Strobl break; 1496aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1497aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1498aca38eabSMarius Strobl break; 1499aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1500aca38eabSMarius Strobl return (0); 1501bd15d31cSMarius Strobl default: 1502bd15d31cSMarius Strobl slot_printf(slot, "Tuning requested but not required.\n"); 1503bd15d31cSMarius Strobl SDHCI_UNLOCK(slot); 1504bd15d31cSMarius Strobl return (EINVAL); 1505aca38eabSMarius Strobl } 1506aca38eabSMarius Strobl 1507aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1508aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1509aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1510aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1511aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1512aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1513aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1514aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1515aca38eabSMarius Strobl MMC_TUNING_LEN; 1516aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1517aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1518aca38eabSMarius Strobl 1519aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1520aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1521aca38eabSMarius Strobl if (err == 0) { 1522aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1523aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1524cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1525aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1526aca38eabSMarius Strobl if (slot->retune_ticks) { 1527aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1528aca38eabSMarius Strobl sdhci_retune, slot); 1529aca38eabSMarius Strobl } 1530aca38eabSMarius Strobl } 1531aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1532aca38eabSMarius Strobl return (err); 1533aca38eabSMarius Strobl } 1534aca38eabSMarius Strobl 1535aca38eabSMarius Strobl int 1536aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1537aca38eabSMarius Strobl { 1538aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1539aca38eabSMarius Strobl int err; 1540aca38eabSMarius Strobl 1541aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1542aca38eabSMarius Strobl return (0); 1543aca38eabSMarius Strobl 1544aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1545aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1546aca38eabSMarius Strobl return (EINVAL); 1547aca38eabSMarius Strobl 1548aca38eabSMarius Strobl SDHCI_LOCK(slot); 1549aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1550aca38eabSMarius Strobl /* 1551aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1552aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1553aca38eabSMarius Strobl * with the host properly acquired, and 1554aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1555aca38eabSMarius Strobl * 1556aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1557aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1558aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1559aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1560aca38eabSMarius Strobl */ 1561aca38eabSMarius Strobl if (slot->retune_ticks) { 1562aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1563aca38eabSMarius Strobl sdhci_retune, slot); 1564aca38eabSMarius Strobl } 1565aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1566aca38eabSMarius Strobl return (err); 1567aca38eabSMarius Strobl } 1568aca38eabSMarius Strobl 1569aca38eabSMarius Strobl static int 1570aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1571aca38eabSMarius Strobl { 1572aca38eabSMarius Strobl struct mmc_request *tune_req; 1573aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1574aca38eabSMarius Strobl int i; 1575aca38eabSMarius Strobl uint32_t intmask; 1576aca38eabSMarius Strobl uint16_t hostctrl2; 1577aca38eabSMarius Strobl u_char opt; 1578aca38eabSMarius Strobl 1579aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1580aca38eabSMarius Strobl if (slot->req != NULL) 1581aca38eabSMarius Strobl return (EBUSY); 1582aca38eabSMarius Strobl 1583aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1584aca38eabSMarius Strobl opt = slot->opt; 1585aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1586aca38eabSMarius Strobl 1587aca38eabSMarius Strobl /* 1588aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1589aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1590aca38eabSMarius Strobl */ 1591aca38eabSMarius Strobl intmask = slot->intmask; 1592aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1593cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1594aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1595aca38eabSMarius Strobl 1596aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1597aca38eabSMarius Strobl if (reset) 1598aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1599aca38eabSMarius Strobl else 1600aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1601aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1602aca38eabSMarius Strobl 1603aca38eabSMarius Strobl tune_req = slot->tune_req; 1604aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1605aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1606aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1607aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1608aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1609aca38eabSMarius Strobl tune_req->done_data = slot; 1610aca38eabSMarius Strobl slot->req = tune_req; 1611aca38eabSMarius Strobl slot->flags = 0; 1612aca38eabSMarius Strobl sdhci_start(slot); 1613aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1614aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1615aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1616aca38eabSMarius Strobl break; 1617aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1618aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1619aca38eabSMarius Strobl break; 1620aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1621aca38eabSMarius Strobl DELAY(1000); 1622aca38eabSMarius Strobl } 1623aca38eabSMarius Strobl 162478f8baa8SMarius Strobl /* 162578f8baa8SMarius Strobl * Restore DMA usage and interrupts. 162678f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 162778f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 162878f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 162978f8baa8SMarius Strobl * doesn't lose these. 163078f8baa8SMarius Strobl */ 1631aca38eabSMarius Strobl slot->opt = opt; 1632aca38eabSMarius Strobl slot->intmask = intmask; 163378f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 163478f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1635aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1636aca38eabSMarius Strobl 1637aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1638aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1639aca38eabSMarius Strobl slot->retune_req = 0; 1640aca38eabSMarius Strobl return (0); 1641aca38eabSMarius Strobl } 1642aca38eabSMarius Strobl 1643aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1644aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1645aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1646b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1647aca38eabSMarius Strobl return (EIO); 1648aca38eabSMarius Strobl } 1649aca38eabSMarius Strobl 1650aca38eabSMarius Strobl static void 1651aca38eabSMarius Strobl sdhci_retune(void *arg) 1652aca38eabSMarius Strobl { 1653aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1654aca38eabSMarius Strobl 1655aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1656aca38eabSMarius Strobl } 1657aca38eabSMarius Strobl 1658a94a63f0SWarner Losh #ifdef MMCCAM 1659a94a63f0SWarner Losh static void 1660a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1661a94a63f0SWarner Losh { 1662a94a63f0SWarner Losh union ccb *ccb; 166315c440e1SWarner Losh 1664aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 166515c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1666a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1667a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1668a94a63f0SWarner Losh ccb = slot->ccb; 1669a94a63f0SWarner Losh slot->ccb = NULL; 1670a94a63f0SWarner Losh slot->curcmd = NULL; 1671a94a63f0SWarner Losh 1672a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1673a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1674a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1675a94a63f0SWarner Losh 1676a94a63f0SWarner Losh ccb->ccb_h.status = 1677a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1678a94a63f0SWarner Losh xpt_done(ccb); 1679a94a63f0SWarner Losh } 1680a94a63f0SWarner Losh } 1681a94a63f0SWarner Losh #else 1682831f5dcfSAlexander Motin static void 1683e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1684e64f01a9SIan Lepore { 1685e64f01a9SIan Lepore struct mmc_request *req; 1686e64f01a9SIan Lepore 1687e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1688e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1689e64f01a9SIan Lepore req = slot->req; 1690e64f01a9SIan Lepore slot->req = NULL; 1691e64f01a9SIan Lepore slot->curcmd = NULL; 1692e64f01a9SIan Lepore req->done(req); 1693e64f01a9SIan Lepore } 1694e64f01a9SIan Lepore } 1695a94a63f0SWarner Losh #endif 1696e64f01a9SIan Lepore 1697e64f01a9SIan Lepore static void 1698aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1699aca38eabSMarius Strobl { 1700aca38eabSMarius Strobl 1701aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1702aca38eabSMarius Strobl wakeup(req); 1703aca38eabSMarius Strobl } 1704aca38eabSMarius Strobl 1705aca38eabSMarius Strobl static void 1706e64f01a9SIan Lepore sdhci_timeout(void *arg) 1707e64f01a9SIan Lepore { 1708e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1709e64f01a9SIan Lepore 1710e64f01a9SIan Lepore if (slot->curcmd != NULL) { 17117e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 17127e586643SIan Lepore sdhci_dumpregs(slot); 1713b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1714b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1715e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1716e64f01a9SIan Lepore sdhci_req_done(slot); 17177e586643SIan Lepore } else { 17187e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1719e64f01a9SIan Lepore } 1720e64f01a9SIan Lepore } 1721e64f01a9SIan Lepore 1722e64f01a9SIan Lepore static void 1723ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1724831f5dcfSAlexander Motin { 1725831f5dcfSAlexander Motin uint16_t mode; 1726831f5dcfSAlexander Motin 1727831f5dcfSAlexander Motin if (data == NULL) 1728831f5dcfSAlexander Motin return; 1729831f5dcfSAlexander Motin 1730831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 17315d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1732831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 17335d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 17346dea80e6SMarius Strobl #ifdef MMCCAM 17356dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 17366dea80e6SMarius Strobl #else 17370519c933SMarius Strobl slot->req->stop != NULL && 17386dea80e6SMarius Strobl #endif 17396dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 17406dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 17416dea80e6SMarius Strobl } 1742831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1743831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1744831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1745831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1746831f5dcfSAlexander Motin 1747831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1748831f5dcfSAlexander Motin } 1749831f5dcfSAlexander Motin 1750831f5dcfSAlexander Motin static void 1751831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1752831f5dcfSAlexander Motin { 1753831f5dcfSAlexander Motin int flags, timeout; 175490993663SIan Lepore uint32_t mask; 1755831f5dcfSAlexander Motin 1756831f5dcfSAlexander Motin slot->curcmd = cmd; 1757831f5dcfSAlexander Motin slot->cmd_done = 0; 1758831f5dcfSAlexander Motin 1759831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1760831f5dcfSAlexander Motin 1761831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1762831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1763831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1764831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1765e64f01a9SIan Lepore sdhci_req_done(slot); 1766831f5dcfSAlexander Motin return; 1767831f5dcfSAlexander Motin } 1768831f5dcfSAlexander Motin 1769b440e965SMarius Strobl /* 1770b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1771b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1772b440e965SMarius Strobl */ 17736e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1774d8208d9eSAlexander Motin slot->power == 0 || 1775d8208d9eSAlexander Motin slot->clock == 0) { 1776a94a63f0SWarner Losh slot_printf(slot, 1777440c645bSMitchell Horne "Cannot issue a command (power=%d clock=%d)\n", 1778a94a63f0SWarner Losh slot->power, slot->clock); 1779831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1780e64f01a9SIan Lepore sdhci_req_done(slot); 1781831f5dcfSAlexander Motin return; 1782831f5dcfSAlexander Motin } 1783831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1784831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1785831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1786a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1787831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1788aca38eabSMarius Strobl /* 1789aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1790aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1791aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1792aca38eabSMarius Strobl */ 1793a94a63f0SWarner Losh #ifdef MMCCAM 1794aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1795a94a63f0SWarner Losh #else 1796aca38eabSMarius Strobl if (cmd == slot->req->stop || 1797a94a63f0SWarner Losh #endif 1798aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1799aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1800aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 18018775ab45SIan Lepore /* 18028775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 18038775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 18048775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 18058775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 18068775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 18078775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 18088775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 18098775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 18108775ab45SIan Lepore */ 18118775ab45SIan Lepore timeout = 250; 181290993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1813831f5dcfSAlexander Motin if (timeout == 0) { 1814831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1815831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1816831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1817831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1818e64f01a9SIan Lepore sdhci_req_done(slot); 1819831f5dcfSAlexander Motin return; 1820831f5dcfSAlexander Motin } 1821831f5dcfSAlexander Motin timeout--; 1822831f5dcfSAlexander Motin DELAY(1000); 1823831f5dcfSAlexander Motin } 1824831f5dcfSAlexander Motin 1825831f5dcfSAlexander Motin /* Prepare command flags. */ 1826831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1827831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1828831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1829831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1830831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1831831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1832831f5dcfSAlexander Motin else 1833831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1834831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1835831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1836831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1837831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1838a94a63f0SWarner Losh if (cmd->data != NULL) 1839831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1840831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1841831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1842831f5dcfSAlexander Motin /* Prepare data. */ 1843831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1844831f5dcfSAlexander Motin /* 1845831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1846831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1847831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1848831f5dcfSAlexander Motin */ 1849831f5dcfSAlexander Motin if (slot->data_done == 0) { 1850831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1851831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1852831f5dcfSAlexander Motin } 1853831f5dcfSAlexander Motin /* Set command argument. */ 1854831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1855831f5dcfSAlexander Motin /* Set data transfer mode. */ 1856831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1857aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18588adf4202SBjoern A. Zeeb slot_printf(slot, "Starting command opcode %#04x flags %#04x\n", 18598adf4202SBjoern A. Zeeb cmd->opcode, flags); 18608adf4202SBjoern A. Zeeb 1861831f5dcfSAlexander Motin /* Start command. */ 1862d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1863a6873fd1SIan Lepore /* Start timeout callout. */ 1864ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1865ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1866831f5dcfSAlexander Motin } 1867831f5dcfSAlexander Motin 1868831f5dcfSAlexander Motin static void 1869831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1870831f5dcfSAlexander Motin { 1871831f5dcfSAlexander Motin int i; 18721bacf3beSMarius Strobl uint32_t val; 18731bacf3beSMarius Strobl uint8_t extra; 1874831f5dcfSAlexander Motin 1875aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18768adf4202SBjoern A. Zeeb slot_printf(slot, "%s: called, err %d flags %#04x\n", 1877a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1878831f5dcfSAlexander Motin slot->cmd_done = 1; 187972dec079SMarius Strobl /* 188072dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1881831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 188272dec079SMarius Strobl * happened first. 188372dec079SMarius Strobl */ 1884aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1885aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1886aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1887aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1888831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1889831f5dcfSAlexander Motin if (slot->curcmd->error) { 1890aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1891aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1892b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 1893b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 1894831f5dcfSAlexander Motin sdhci_start(slot); 1895831f5dcfSAlexander Motin return; 1896831f5dcfSAlexander Motin } 1897831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1898831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1899831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1900831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 19011bacf3beSMarius Strobl extra = 0; 1902831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 19031bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 19041bacf3beSMarius Strobl if (slot->quirks & 19051bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1906677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1907677ee494SIan Lepore else { 1908677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1909677ee494SIan Lepore (val << 8) | extra; 1910831f5dcfSAlexander Motin extra = val >> 24; 1911831f5dcfSAlexander Motin } 1912677ee494SIan Lepore } 1913831f5dcfSAlexander Motin } else 1914831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1915831f5dcfSAlexander Motin } 1916aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 19178adf4202SBjoern A. Zeeb slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n", 1918a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1919a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1920a94a63f0SWarner Losh 1921831f5dcfSAlexander Motin /* If data ready - finish. */ 1922831f5dcfSAlexander Motin if (slot->data_done) 1923831f5dcfSAlexander Motin sdhci_start(slot); 1924831f5dcfSAlexander Motin } 1925831f5dcfSAlexander Motin 1926831f5dcfSAlexander Motin static void 1927ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1928831f5dcfSAlexander Motin { 1929ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1930831f5dcfSAlexander Motin uint8_t div; 1931831f5dcfSAlexander Motin 1932831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1933831f5dcfSAlexander Motin slot->data_done = 1; 1934831f5dcfSAlexander Motin return; 1935831f5dcfSAlexander Motin } 1936831f5dcfSAlexander Motin 1937831f5dcfSAlexander Motin slot->data_done = 0; 1938831f5dcfSAlexander Motin 1939831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1940831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1941ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1942ceb9e9f7SIan Lepore div = 0xE; 1943ceb9e9f7SIan Lepore } else { 1944831f5dcfSAlexander Motin target_timeout = 1000000; 1945831f5dcfSAlexander Motin div = 0; 1946831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1947ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1948ceb9e9f7SIan Lepore ++div; 1949831f5dcfSAlexander Motin current_timeout <<= 1; 1950831f5dcfSAlexander Motin } 1951831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1952ceb9e9f7SIan Lepore if (div < 0xE && 1953ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1954ceb9e9f7SIan Lepore ++div; 1955831f5dcfSAlexander Motin } 1956ceb9e9f7SIan Lepore } 1957831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1958831f5dcfSAlexander Motin 1959831f5dcfSAlexander Motin if (data == NULL) 1960831f5dcfSAlexander Motin return; 1961831f5dcfSAlexander Motin 1962831f5dcfSAlexander Motin /* Use DMA if possible. */ 1963831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1964831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1965ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1966d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1967831f5dcfSAlexander Motin (data->len <= 512)) 1968831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1969831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1970d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1971831f5dcfSAlexander Motin ((data->len) & 0x3)) 1972831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1973831f5dcfSAlexander Motin /* Load DMA buffer. */ 1974831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1975ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1976831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1977ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1978ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1979831f5dcfSAlexander Motin else { 1980ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1981ab00a509SMarius Strobl sdma_bbufsz)); 1982ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1983ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1984831f5dcfSAlexander Motin } 1985831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1986ab00a509SMarius Strobl /* 1987ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1988ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1989ab00a509SMarius Strobl */ 1990ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1991831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1992831f5dcfSAlexander Motin else 1993831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1994831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1995831f5dcfSAlexander Motin } 1996831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1997831f5dcfSAlexander Motin slot->offset = 0; 19985d5ae066SIlya Bakulin #ifdef MMCCAM 19995d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 20005d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 20015d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 20025d5ae066SIlya Bakulin blkcnt = data->block_count; 20035d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 20045d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 20055d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 20065d5ae066SIlya Bakulin } else 20075d5ae066SIlya Bakulin #endif 20085d5ae066SIlya Bakulin { 2009ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 2010ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 2011ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 20125d5ae066SIlya Bakulin } 20135d5ae066SIlya Bakulin 20145d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 2015ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 2016aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2017ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 2018ab00a509SMarius Strobl blksz, blkcnt); 2019831f5dcfSAlexander Motin } 2020831f5dcfSAlexander Motin 2021c3a0f75aSOleksandr Tymoshenko void 2022831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 2023831f5dcfSAlexander Motin { 2024831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 20257e6ccea3SMarius Strobl size_t left; 2026831f5dcfSAlexander Motin 2027831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 2028ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 2029831f5dcfSAlexander Motin * happened first. */ 2030831f5dcfSAlexander Motin if (!slot->cmd_done) { 2031831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 2032831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 2033831f5dcfSAlexander Motin } 2034831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 2035915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 2036915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 2037831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 20387e6ccea3SMarius Strobl left = data->len - slot->offset; 2039ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2040ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 2041831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2042ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 2043831f5dcfSAlexander Motin } else 2044ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2045ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 2046831f5dcfSAlexander Motin } 2047a98788edSIan Lepore slot->data_done = 1; 2048831f5dcfSAlexander Motin /* If there was error - reset the host. */ 2049831f5dcfSAlexander Motin if (slot->curcmd->error) { 2050aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 2051aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2052b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2053b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2054831f5dcfSAlexander Motin sdhci_start(slot); 2055831f5dcfSAlexander Motin return; 2056831f5dcfSAlexander Motin } 2057831f5dcfSAlexander Motin /* If we already have command response - finish. */ 2058831f5dcfSAlexander Motin if (slot->cmd_done) 2059831f5dcfSAlexander Motin sdhci_start(slot); 2060831f5dcfSAlexander Motin } 2061831f5dcfSAlexander Motin 2062a94a63f0SWarner Losh #ifdef MMCCAM 2063a94a63f0SWarner Losh static void 2064a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 2065a94a63f0SWarner Losh { 2066a94a63f0SWarner Losh union ccb *ccb; 2067ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 2068a94a63f0SWarner Losh 2069a94a63f0SWarner Losh ccb = slot->ccb; 2070a94a63f0SWarner Losh if (ccb == NULL) 2071a94a63f0SWarner Losh return; 2072a94a63f0SWarner Losh 2073a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2074a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 2075a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 2076a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 2077a94a63f0SWarner Losh return; 2078a94a63f0SWarner Losh } 2079a94a63f0SWarner Losh 2080a94a63f0SWarner Losh /* 2081a94a63f0SWarner Losh * Old stack doesn't use this! 2082a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 2083a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 2084a94a63f0SWarner Losh * Not using this code does no harm... 2085a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 2086a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 2087a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 2088a94a63f0SWarner Losh return; 2089a94a63f0SWarner Losh } 2090a94a63f0SWarner Losh */ 2091aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2092a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 2093a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 2094a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 2095b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2096b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2097a94a63f0SWarner Losh } 2098a94a63f0SWarner Losh 2099a94a63f0SWarner Losh sdhci_req_done(slot); 2100a94a63f0SWarner Losh } 2101a94a63f0SWarner Losh #else 2102831f5dcfSAlexander Motin static void 2103831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 2104831f5dcfSAlexander Motin { 2105ab00a509SMarius Strobl const struct mmc_request *req; 2106831f5dcfSAlexander Motin 2107831f5dcfSAlexander Motin req = slot->req; 2108831f5dcfSAlexander Motin if (req == NULL) 2109831f5dcfSAlexander Motin return; 2110831f5dcfSAlexander Motin 2111831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 2112831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 2113831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 2114831f5dcfSAlexander Motin return; 2115831f5dcfSAlexander Motin } 2116915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 2117915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 2118831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 2119831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 2120831f5dcfSAlexander Motin return; 2121831f5dcfSAlexander Motin } 2122aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 21235b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 21245b69a497SAlexander Motin if (!req->cmd->error && 2125915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2126915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2127915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2128b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2129b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2130831f5dcfSAlexander Motin } 2131831f5dcfSAlexander Motin 2132e64f01a9SIan Lepore sdhci_req_done(slot); 2133831f5dcfSAlexander Motin } 2134a94a63f0SWarner Losh #endif 2135831f5dcfSAlexander Motin 2136d6b3aaf8SOleksandr Tymoshenko int 2137b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2138b440e965SMarius Strobl struct mmc_request *req) 2139831f5dcfSAlexander Motin { 2140831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2141831f5dcfSAlexander Motin 2142831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2143831f5dcfSAlexander Motin if (slot->req != NULL) { 2144831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2145831f5dcfSAlexander Motin return (EBUSY); 2146831f5dcfSAlexander Motin } 2147aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 21481bacf3beSMarius Strobl slot_printf(slot, 21491bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2150831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 21515b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 21525b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 21535b69a497SAlexander Motin } 2154831f5dcfSAlexander Motin slot->req = req; 2155831f5dcfSAlexander Motin slot->flags = 0; 2156831f5dcfSAlexander Motin sdhci_start(slot); 2157831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2158bea2dca2SAlexander Motin if (dumping) { 2159bea2dca2SAlexander Motin while (slot->req != NULL) { 2160d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2161bea2dca2SAlexander Motin DELAY(10); 2162bea2dca2SAlexander Motin } 2163bea2dca2SAlexander Motin } 2164831f5dcfSAlexander Motin return (0); 2165831f5dcfSAlexander Motin } 2166831f5dcfSAlexander Motin 2167d6b3aaf8SOleksandr Tymoshenko int 2168b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2169831f5dcfSAlexander Motin { 2170831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2171831f5dcfSAlexander Motin uint32_t val; 2172831f5dcfSAlexander Motin 2173831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2174831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2175831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2176831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2177831f5dcfSAlexander Motin } 2178831f5dcfSAlexander Motin 2179d6b3aaf8SOleksandr Tymoshenko int 2180b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2181831f5dcfSAlexander Motin { 2182831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2183831f5dcfSAlexander Motin int err = 0; 2184831f5dcfSAlexander Motin 2185831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2186831f5dcfSAlexander Motin while (slot->bus_busy) 2187d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2188831f5dcfSAlexander Motin slot->bus_busy++; 2189831f5dcfSAlexander Motin /* Activate led. */ 2190831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2191831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2192831f5dcfSAlexander Motin return (err); 2193831f5dcfSAlexander Motin } 2194831f5dcfSAlexander Motin 2195d6b3aaf8SOleksandr Tymoshenko int 2196b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2197831f5dcfSAlexander Motin { 2198831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2199831f5dcfSAlexander Motin 2200831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2201831f5dcfSAlexander Motin /* Deactivate led. */ 2202831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2203831f5dcfSAlexander Motin slot->bus_busy--; 2204d493985aSAlexander Motin wakeup(slot); 220535547df5SScott Long SDHCI_UNLOCK(slot); 2206831f5dcfSAlexander Motin return (0); 2207831f5dcfSAlexander Motin } 2208831f5dcfSAlexander Motin 2209831f5dcfSAlexander Motin static void 2210831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2211831f5dcfSAlexander Motin { 2212831f5dcfSAlexander Motin 2213831f5dcfSAlexander Motin if (!slot->curcmd) { 2214831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2215831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2216831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2217831f5dcfSAlexander Motin return; 2218831f5dcfSAlexander Motin } 2219831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2220831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2221831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2222831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2223831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2224831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2225831f5dcfSAlexander Motin 2226831f5dcfSAlexander Motin sdhci_finish_command(slot); 2227831f5dcfSAlexander Motin } 2228831f5dcfSAlexander Motin 2229831f5dcfSAlexander Motin static void 2230831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2231831f5dcfSAlexander Motin { 22321bacf3beSMarius Strobl struct mmc_data *data; 223315c440e1SWarner Losh size_t left; 2234ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2235831f5dcfSAlexander Motin 2236831f5dcfSAlexander Motin if (!slot->curcmd) { 2237831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2238831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2239831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2240831f5dcfSAlexander Motin return; 2241831f5dcfSAlexander Motin } 2242831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2243831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2244831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2245831f5dcfSAlexander Motin "there is no active data operation.\n", 2246831f5dcfSAlexander Motin intmask); 2247831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2248831f5dcfSAlexander Motin return; 2249831f5dcfSAlexander Motin } 2250831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2251831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2252acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2253831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2254831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2255831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2256831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2257831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2258831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2259831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2260831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2261831f5dcfSAlexander Motin } 2262831f5dcfSAlexander Motin if (slot->curcmd->error) { 2263831f5dcfSAlexander Motin /* No need to continue after any error. */ 2264a98788edSIan Lepore goto done; 2265831f5dcfSAlexander Motin } 2266831f5dcfSAlexander Motin 2267aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2268aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2269aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2270aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2271aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2272aca38eabSMarius Strobl sdhci_finish_command(slot); 2273aca38eabSMarius Strobl sdhci_finish_data(slot); 2274aca38eabSMarius Strobl return; 2275aca38eabSMarius Strobl } 2276831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2277c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2278c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2279c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 22801bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 22811bacf3beSMarius Strobl &intmask); 2282c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2283c3a0f75aSOleksandr Tymoshenko } else 2284831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2285c3a0f75aSOleksandr Tymoshenko } 2286831f5dcfSAlexander Motin /* Handle DMA border. */ 2287831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 22881bacf3beSMarius Strobl data = slot->curcmd->data; 2289ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2290831f5dcfSAlexander Motin 2291831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2292831f5dcfSAlexander Motin left = data->len - slot->offset; 2293831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2294831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2295831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2296831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2297ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2298831f5dcfSAlexander Motin } else { 2299831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2300831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2301831f5dcfSAlexander Motin } 2302831f5dcfSAlexander Motin /* ... and reload it again. */ 2303ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2304831f5dcfSAlexander Motin left = data->len - slot->offset; 2305831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2306831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2307831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2308831f5dcfSAlexander Motin } else { 2309831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2310ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2311831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2312831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2313831f5dcfSAlexander Motin } 2314ab00a509SMarius Strobl /* 2315ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2316ab00a509SMarius Strobl * bounce buffer. 2317ab00a509SMarius Strobl */ 2318ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2319831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2320831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2321831f5dcfSAlexander Motin } 2322831f5dcfSAlexander Motin /* Restart DMA. */ 2323831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2324831f5dcfSAlexander Motin } 2325831f5dcfSAlexander Motin /* We have got all data. */ 2326c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2327c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2328c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2329c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2330c3a0f75aSOleksandr Tymoshenko } else 2331831f5dcfSAlexander Motin sdhci_finish_data(slot); 2332831f5dcfSAlexander Motin } 2333a98788edSIan Lepore done: 2334a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2335a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2336a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2337a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2338a98788edSIan Lepore } else 2339a98788edSIan Lepore sdhci_finish_data(slot); 2340a98788edSIan Lepore } 2341c3a0f75aSOleksandr Tymoshenko } 2342831f5dcfSAlexander Motin 2343831f5dcfSAlexander Motin static void 23446dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2345831f5dcfSAlexander Motin { 2346831f5dcfSAlexander Motin 2347831f5dcfSAlexander Motin if (!slot->curcmd) { 2348831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 23496dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2350831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2351831f5dcfSAlexander Motin return; 2352831f5dcfSAlexander Motin } 23536dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2354b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2355831f5dcfSAlexander Motin } 2356831f5dcfSAlexander Motin 2357d6b3aaf8SOleksandr Tymoshenko void 2358d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2359831f5dcfSAlexander Motin { 23602b96b955SJustin Hibbits uint32_t intmask, present; 23616dea80e6SMarius Strobl uint16_t val16; 2362831f5dcfSAlexander Motin 2363831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2364831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2365831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2366831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2367831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2368d6b3aaf8SOleksandr Tymoshenko return; 2369831f5dcfSAlexander Motin } 2370aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 23715b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 23725b69a497SAlexander Motin 2373aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2374aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 23756dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2376aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2377aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2378aca38eabSMarius Strobl if (slot->curcmd) { 2379aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2380aca38eabSMarius Strobl sdhci_finish_command(slot); 2381aca38eabSMarius Strobl } 2382aca38eabSMarius Strobl } 2383aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2384aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2385aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2386831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2387831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2388639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 23892b96b955SJustin Hibbits slot->intmask &= 23902b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 23912b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 23922b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 23932b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 23942b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2395831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2396831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2397b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2398831f5dcfSAlexander Motin } 2399831f5dcfSAlexander Motin /* Handle command interrupts. */ 2400831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2401831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2402831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2403831f5dcfSAlexander Motin } 2404831f5dcfSAlexander Motin /* Handle data interrupts. */ 2405831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2406831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 24077e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 24087e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2409831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2410831f5dcfSAlexander Motin } 2411831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2412831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 24136dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 24146dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2415831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 24166dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2417831f5dcfSAlexander Motin } 2418831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2419831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2420831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2421aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2422831f5dcfSAlexander Motin } 2423aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2424aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2425aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2426831f5dcfSAlexander Motin /* The rest is unknown. */ 2427831f5dcfSAlexander Motin if (intmask) { 2428831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2429831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2430831f5dcfSAlexander Motin intmask); 2431831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2432831f5dcfSAlexander Motin } 2433831f5dcfSAlexander Motin 2434831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2435831f5dcfSAlexander Motin } 2436831f5dcfSAlexander Motin 2437d6b3aaf8SOleksandr Tymoshenko int 24381bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 24391bacf3beSMarius Strobl uintptr_t *result) 2440831f5dcfSAlexander Motin { 2441ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2442831f5dcfSAlexander Motin 2443831f5dcfSAlexander Motin switch (which) { 2444831f5dcfSAlexander Motin default: 2445831f5dcfSAlexander Motin return (EINVAL); 2446831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2447bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2448831f5dcfSAlexander Motin break; 2449831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2450bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2451831f5dcfSAlexander Motin break; 2452831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2453bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2454831f5dcfSAlexander Motin break; 2455831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2456bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2457831f5dcfSAlexander Motin break; 2458831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2459bcd91d25SJayachandran C. *result = slot->host.f_min; 2460831f5dcfSAlexander Motin break; 2461831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2462bcd91d25SJayachandran C. *result = slot->host.f_max; 2463831f5dcfSAlexander Motin break; 2464831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2465bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2466831f5dcfSAlexander Motin break; 2467831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2468bcd91d25SJayachandran C. *result = slot->host.mode; 2469831f5dcfSAlexander Motin break; 2470831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2471bcd91d25SJayachandran C. *result = slot->host.ocr; 2472831f5dcfSAlexander Motin break; 2473831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2474bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2475831f5dcfSAlexander Motin break; 2476831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2477bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2478831f5dcfSAlexander Motin break; 2479aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2480aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2481aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2482aca38eabSMarius Strobl *result = retune_req_reset; 2483aca38eabSMarius Strobl break; 2484aca38eabSMarius Strobl } 2485aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2486aca38eabSMarius Strobl *result = retune_req_normal; 2487aca38eabSMarius Strobl break; 2488aca38eabSMarius Strobl } 2489aca38eabSMarius Strobl } 2490aca38eabSMarius Strobl *result = retune_req_none; 2491aca38eabSMarius Strobl break; 24920f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24930f34084fSMarius Strobl *result = slot->host.ios.vccq; 24940f34084fSMarius Strobl break; 2495831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2496bcd91d25SJayachandran C. *result = slot->host.caps; 2497831f5dcfSAlexander Motin break; 2498831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2499bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2500831f5dcfSAlexander Motin break; 25013a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2502aca38eabSMarius Strobl /* 2503aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2504aca38eabSMarius Strobl * per read/write command to 4 MiB. 2505aca38eabSMarius Strobl */ 2506aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2507aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2508aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2509aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2510aca38eabSMarius Strobl break; 2511aca38eabSMarius Strobl } 2512bcd91d25SJayachandran C. *result = 65535; 25133a4a2557SAlexander Motin break; 251472dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 251572dec079SMarius Strobl /* 251672dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 251772dec079SMarius Strobl */ 251872dec079SMarius Strobl *result = 1000000; 251972dec079SMarius Strobl break; 2520831f5dcfSAlexander Motin } 2521831f5dcfSAlexander Motin return (0); 2522831f5dcfSAlexander Motin } 2523831f5dcfSAlexander Motin 2524d6b3aaf8SOleksandr Tymoshenko int 25251bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 25261bacf3beSMarius Strobl uintptr_t value) 2527831f5dcfSAlexander Motin { 2528831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2529b440e965SMarius Strobl uint32_t clock, max_clock; 2530b440e965SMarius Strobl int i; 2531831f5dcfSAlexander Motin 253215c440e1SWarner Losh if (sdhci_debug > 1) 253315c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2534831f5dcfSAlexander Motin switch (which) { 2535831f5dcfSAlexander Motin default: 2536831f5dcfSAlexander Motin return (EINVAL); 2537831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2538831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2539831f5dcfSAlexander Motin break; 2540831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2541831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2542831f5dcfSAlexander Motin break; 2543831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2544831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2545831f5dcfSAlexander Motin break; 2546831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2547831f5dcfSAlexander Motin if (value > 0) { 254857677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 254957677a3aSOleksandr Tymoshenko clock = max_clock; 255057677a3aSOleksandr Tymoshenko 255157677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 255257677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 255357677a3aSOleksandr Tymoshenko i <<= 1) { 2554831f5dcfSAlexander Motin if (clock <= value) 2555831f5dcfSAlexander Motin break; 2556831f5dcfSAlexander Motin clock >>= 1; 2557831f5dcfSAlexander Motin } 2558b440e965SMarius Strobl } else { 255957677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 256057677a3aSOleksandr Tymoshenko i += 2) { 256157677a3aSOleksandr Tymoshenko if (clock <= value) 256257677a3aSOleksandr Tymoshenko break; 256357677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 256457677a3aSOleksandr Tymoshenko } 256557677a3aSOleksandr Tymoshenko } 256657677a3aSOleksandr Tymoshenko 2567831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2568831f5dcfSAlexander Motin } else 2569831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2570831f5dcfSAlexander Motin break; 2571831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2572831f5dcfSAlexander Motin slot->host.mode = value; 2573831f5dcfSAlexander Motin break; 2574831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2575831f5dcfSAlexander Motin slot->host.ocr = value; 2576831f5dcfSAlexander Motin break; 2577831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2578831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2579831f5dcfSAlexander Motin break; 2580831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2581831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2582831f5dcfSAlexander Motin break; 25830f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 25840f34084fSMarius Strobl slot->host.ios.vccq = value; 25850f34084fSMarius Strobl break; 2586831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2587831f5dcfSAlexander Motin slot->host.ios.timing = value; 2588831f5dcfSAlexander Motin break; 2589831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2590831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2591831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2592831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 25933a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2594aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2595831f5dcfSAlexander Motin return (EINVAL); 2596831f5dcfSAlexander Motin } 2597831f5dcfSAlexander Motin return (0); 2598831f5dcfSAlexander Motin } 2599831f5dcfSAlexander Motin 260015c440e1SWarner Losh #ifdef MMCCAM 2601a94a63f0SWarner Losh void 2602d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2603a94a63f0SWarner Losh { 2604ab00a509SMarius Strobl 2605505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2606a94a63f0SWarner Losh goto fail; 2607a94a63f0SWarner Losh 2608a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2609aeb04e88SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2610da2f833fSBjoern A. Zeeb "sdhci_slot", slot, device_get_unit(slot->bus), 2611a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2612a94a63f0SWarner Losh 2613a94a63f0SWarner Losh if (slot->sim == NULL) { 2614a94a63f0SWarner Losh cam_simq_free(slot->devq); 2615a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2616a94a63f0SWarner Losh goto fail; 2617a94a63f0SWarner Losh } 2618a94a63f0SWarner Losh 2619a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2620a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2621505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2622a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2623a94a63f0SWarner Losh cam_simq_free(slot->devq); 2624a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2625a94a63f0SWarner Losh goto fail; 2626a94a63f0SWarner Losh } 2627a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2628505f6a0cSBjoern A. Zeeb 2629a94a63f0SWarner Losh /* End CAM-specific init */ 2630a94a63f0SWarner Losh slot->card_present = 0; 2631a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2632a94a63f0SWarner Losh return; 2633a94a63f0SWarner Losh 2634a94a63f0SWarner Losh fail: 2635a94a63f0SWarner Losh if (slot->sim != NULL) { 2636a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2637a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2638a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2639a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2640a94a63f0SWarner Losh } 2641a94a63f0SWarner Losh 2642a94a63f0SWarner Losh if (slot->devq != NULL) 2643a94a63f0SWarner Losh cam_simq_free(slot->devq); 2644a94a63f0SWarner Losh } 2645a94a63f0SWarner Losh 2646a94a63f0SWarner Losh void 2647a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2648a94a63f0SWarner Losh { 2649a94a63f0SWarner Losh struct sdhci_slot *slot; 2650a94a63f0SWarner Losh 2651a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2652a94a63f0SWarner Losh if (slot == NULL) { 2653a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2654a94a63f0SWarner Losh xpt_done(ccb); 2655a94a63f0SWarner Losh return; 2656a94a63f0SWarner Losh } 2657a94a63f0SWarner Losh 2658a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2659a94a63f0SWarner Losh 2660a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2661a94a63f0SWarner Losh case XPT_PATH_INQ: 2662cd853791SKonstantin Belousov mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys); 2663a94a63f0SWarner Losh break; 26648c7cd14aSWarner Losh 2665af2253f6SEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS: 2666a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2667a94a63f0SWarner Losh { 2668a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 26695d20e651SIlya Bakulin uint32_t max_data; 2670a94a63f0SWarner Losh 2671a94a63f0SWarner Losh if (sdhci_debug > 1) 2672a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2673a94a63f0SWarner Losh 2674a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2675a94a63f0SWarner Losh cts->protocol_version = 1; 2676a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2677a94a63f0SWarner Losh cts->transport_version = 1; 2678a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2679a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2680a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2681a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2682a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 26835d20e651SIlya Bakulin /* 26845d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 26855d20e651SIlya Bakulin * per read/write command to 4 MiB. 26865d20e651SIlya Bakulin */ 26875d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 26885d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 26895d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 26905d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 26915d20e651SIlya Bakulin } else { 26925d20e651SIlya Bakulin max_data = 65535; 26935d20e651SIlya Bakulin } 26945d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 26955d20e651SIlya Bakulin 2696a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2697a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2698a94a63f0SWarner Losh break; 2699a94a63f0SWarner Losh } 2700af2253f6SEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS: 2701a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2702a94a63f0SWarner Losh if (sdhci_debug > 1) 2703a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2704a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2705a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2706a94a63f0SWarner Losh break; 2707a94a63f0SWarner Losh case XPT_RESET_BUS: 2708a94a63f0SWarner Losh if (sdhci_debug > 1) 2709a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2710a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2711a94a63f0SWarner Losh break; 2712a94a63f0SWarner Losh case XPT_MMC_IO: 2713a94a63f0SWarner Losh /* 2714a94a63f0SWarner Losh * Here is the HW-dependent part of 2715a94a63f0SWarner Losh * sending the command to the underlying h/w 2716a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2717a94a63f0SWarner Losh * Then the request will be marked as completed. 2718a94a63f0SWarner Losh */ 2719aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2720a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2721a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2722a94a63f0SWarner Losh 2723160799c6SWarner Losh sdhci_cam_request(cam_sim_softc(sim), ccb); 2724a94a63f0SWarner Losh return; 2725a94a63f0SWarner Losh default: 2726a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2727a94a63f0SWarner Losh break; 2728a94a63f0SWarner Losh } 2729a94a63f0SWarner Losh xpt_done(ccb); 2730a94a63f0SWarner Losh return; 2731a94a63f0SWarner Losh } 2732a94a63f0SWarner Losh 2733a94a63f0SWarner Losh void 2734a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2735a94a63f0SWarner Losh { 273694ff1d9cSAndriy Gapon sdhci_generic_intr(cam_sim_softc(sim)); 2737a94a63f0SWarner Losh } 2738a94a63f0SWarner Losh 27396dea80e6SMarius Strobl static int 2740ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2741ab00a509SMarius Strobl int proposed_clock) 27426dea80e6SMarius Strobl { 2743a94a63f0SWarner Losh int max_clock, clock, i; 2744a94a63f0SWarner Losh 2745a94a63f0SWarner Losh if (proposed_clock == 0) 2746a94a63f0SWarner Losh return 0; 2747a94a63f0SWarner Losh max_clock = slot->max_clk; 2748a94a63f0SWarner Losh clock = max_clock; 2749a94a63f0SWarner Losh 2750a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2751505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2752a94a63f0SWarner Losh if (clock <= proposed_clock) 2753a94a63f0SWarner Losh break; 2754a94a63f0SWarner Losh clock >>= 1; 2755a94a63f0SWarner Losh } 2756a94a63f0SWarner Losh } else { 2757505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2758a94a63f0SWarner Losh if (clock <= proposed_clock) 2759a94a63f0SWarner Losh break; 2760a94a63f0SWarner Losh clock = max_clock / (i + 2); 2761a94a63f0SWarner Losh } 2762a94a63f0SWarner Losh } 2763a94a63f0SWarner Losh return clock; 2764a94a63f0SWarner Losh } 2765a94a63f0SWarner Losh 2766ab00a509SMarius Strobl static int 2767a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2768a94a63f0SWarner Losh { 2769a94a63f0SWarner Losh struct mmc_ios *ios; 2770ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2771ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2772a94a63f0SWarner Losh 2773a94a63f0SWarner Losh ios = &slot->host.ios; 2774a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2775a94a63f0SWarner Losh new_ios = &cts->ios; 2776a94a63f0SWarner Losh 2777a94a63f0SWarner Losh /* Update only requested fields */ 2778a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2779a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2780b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2781a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2782a94a63f0SWarner Losh } 2783a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2784a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2785b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2786a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2787a94a63f0SWarner Losh } 2788a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2789a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2790b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2791a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2792a94a63f0SWarner Losh } 2793a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2794a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2795b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2796a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2797a94a63f0SWarner Losh } 2798a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2799a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2800b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2801a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2802a94a63f0SWarner Losh } 2803a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2804a94a63f0SWarner Losh ios->timing = new_ios->timing; 2805b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2806a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2807a94a63f0SWarner Losh } 2808a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2809a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2810b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2811a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2812a94a63f0SWarner Losh } 28131a96c143SEmmanuel Vadot if (cts->ios_valid & MMC_VCCQ) { 28141a96c143SEmmanuel Vadot ios->vccq = new_ios->vccq; 28151a96c143SEmmanuel Vadot if (sdhci_debug > 1) 28161a96c143SEmmanuel Vadot slot_printf(slot, "VCCQ => %d\n", ios->vccq); 28171a96c143SEmmanuel Vadot } 2818a94a63f0SWarner Losh 2819a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2820a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2821a94a63f0SWarner Losh } 2822a94a63f0SWarner Losh 2823ab00a509SMarius Strobl static int 2824a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2825a94a63f0SWarner Losh { 2826a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2827a94a63f0SWarner Losh 2828b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2829a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2830a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2831a94a63f0SWarner Losh SDHCI_LOCK(slot); 2832a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2833a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2834a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2835a94a63f0SWarner Losh sdhci_init(slot); 2836a94a63f0SWarner Losh } 2837a94a63f0SWarner Losh /* Configure the bus. */ 2838a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2839a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2840a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2841a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2842a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2843a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2844a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2845a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2846a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2847a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2848a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2849a94a63f0SWarner Losh } else { 2850a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2851a94a63f0SWarner Losh } 2852a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2853a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2854a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2855a94a63f0SWarner Losh else 2856a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2857a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2858a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2859a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2860b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 2861b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2862a94a63f0SWarner Losh 2863a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2864a94a63f0SWarner Losh return (0); 2865a94a63f0SWarner Losh } 2866a94a63f0SWarner Losh 2867ab00a509SMarius Strobl static int 2868a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2869a94a63f0SWarner Losh { 2870ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2871a94a63f0SWarner Losh 2872a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2873a94a63f0SWarner Losh 2874a94a63f0SWarner Losh SDHCI_LOCK(slot); 2875a94a63f0SWarner Losh /* if (slot->req != NULL) { 2876a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2877a94a63f0SWarner Losh return (EBUSY); 2878a94a63f0SWarner Losh } 2879a94a63f0SWarner Losh */ 2880aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 28815d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 28825d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2883a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2884a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 28855d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 28865d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 28875d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2888a94a63f0SWarner Losh } 2889a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2890a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2891a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2892a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2893a94a63f0SWarner Losh } 2894a94a63f0SWarner Losh slot->ccb = ccb; 2895a94a63f0SWarner Losh slot->flags = 0; 2896a94a63f0SWarner Losh sdhci_start(slot); 2897a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2898a94a63f0SWarner Losh return (0); 2899a94a63f0SWarner Losh } 290015c440e1SWarner Losh #endif /* MMCCAM */ 2901a94a63f0SWarner Losh 2902ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2903