1831f5dcfSAlexander Motin /*- 2*4d846d26SWarner 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/cdefs.h> 30831f5dcfSAlexander Motin __FBSDID("$FreeBSD$"); 31831f5dcfSAlexander Motin 32831f5dcfSAlexander Motin #include <sys/param.h> 33831f5dcfSAlexander Motin #include <sys/systm.h> 34831f5dcfSAlexander Motin #include <sys/bus.h> 35e64f01a9SIan Lepore #include <sys/callout.h> 36831f5dcfSAlexander Motin #include <sys/conf.h> 37831f5dcfSAlexander Motin #include <sys/kernel.h> 38aca38eabSMarius Strobl #include <sys/kobj.h> 39ab00a509SMarius Strobl #include <sys/libkern.h> 40831f5dcfSAlexander Motin #include <sys/lock.h> 41aca38eabSMarius Strobl #include <sys/malloc.h> 42831f5dcfSAlexander Motin #include <sys/module.h> 43831f5dcfSAlexander Motin #include <sys/mutex.h> 44831f5dcfSAlexander Motin #include <sys/resource.h> 45831f5dcfSAlexander Motin #include <sys/rman.h> 465b69a497SAlexander Motin #include <sys/sysctl.h> 47831f5dcfSAlexander Motin #include <sys/taskqueue.h> 48d00c1f7fSBartlomiej Grzesik #include <sys/sbuf.h> 49831f5dcfSAlexander Motin 50831f5dcfSAlexander Motin #include <machine/bus.h> 51831f5dcfSAlexander Motin #include <machine/resource.h> 52831f5dcfSAlexander Motin #include <machine/stdarg.h> 53831f5dcfSAlexander Motin 54831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 55831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 56831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 57831f5dcfSAlexander Motin 58aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h> 59aca38eabSMarius Strobl 60a94a63f0SWarner Losh #include <cam/cam.h> 61a94a63f0SWarner Losh #include <cam/cam_ccb.h> 62a94a63f0SWarner Losh #include <cam/cam_debug.h> 63a94a63f0SWarner Losh #include <cam/cam_sim.h> 64a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 65a94a63f0SWarner Losh 66831f5dcfSAlexander Motin #include "mmcbr_if.h" 67d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 68831f5dcfSAlexander Motin 69a94a63f0SWarner Losh #include "opt_mmccam.h" 70a94a63f0SWarner Losh 717029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 727029da5cSPawel Biernacki "sdhci driver"); 73831f5dcfSAlexander Motin 74a94a63f0SWarner Losh static int sdhci_debug = 0; 757e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 767e6ccea3SMarius Strobl "Debug level"); 770f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 780f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 790f34084fSMarius Strobl 0, "Mask of quirks to clear"); 800f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 810f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 820f34084fSMarius Strobl "Mask of quirks to set"); 835b69a497SAlexander Motin 84d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 85d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 86d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 87d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 88d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 89831f5dcfSAlexander Motin 90d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 91d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 92d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 93d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 94d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 95831f5dcfSAlexander Motin 966dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err); 97aca38eabSMarius Strobl static void sdhci_card_poll(void *arg); 98aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending); 996dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); 1006dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); 101aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 1026dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, 1036dea80e6SMarius Strobl bool is_present); 1046dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot); 1056dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot); 1066dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot); 1076dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot); 108aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req); 109aca38eabSMarius Strobl static void sdhci_retune(void *arg); 110831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 1116dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power); 1126dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot, 113ab00a509SMarius Strobl const struct mmc_data *data); 114831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 1156dea80e6SMarius Strobl static void sdhci_timeout(void *arg); 1166dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot, 1176dea80e6SMarius Strobl struct mmc_command *cmd); 118ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot, 119ab00a509SMarius Strobl const struct mmc_data *data); 1206dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot); 1216dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot); 122831f5dcfSAlexander Motin 12315c440e1SWarner Losh #ifdef MMCCAM 124a94a63f0SWarner Losh /* CAM-related */ 125a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 126ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 1276dea80e6SMarius Strobl int proposed_clock); 128a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 1296dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 130a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 1316dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot); 13215c440e1SWarner Losh #endif 133a94a63f0SWarner Losh 134831f5dcfSAlexander Motin /* helper routines */ 135016f9657SMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot); 136ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot); 137d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps(struct sdhci_slot *slot); 138d00c1f7fSBartlomiej Grzesik static void sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s); 1390f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 140d00c1f7fSBartlomiej Grzesik static void sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s); 141d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS); 142d00c1f7fSBartlomiej Grzesik static int sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS); 1436dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, 1446dea80e6SMarius Strobl int error); 145ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 1460f34084fSMarius Strobl __printflike(2, 3); 147d00c1f7fSBartlomiej Grzesik static int slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s, 148d00c1f7fSBartlomiej Grzesik const char * fmt, ...) __printflike(3, 4); 149ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot); 1500f34084fSMarius Strobl 151831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 152831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 153831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 154831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 155831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 156831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 157831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 158831f5dcfSAlexander Motin 15933aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 16033aad34dSOleksandr Tymoshenko 16157677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 16257677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 16357677a3aSOleksandr Tymoshenko 164639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 165639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 166639f59f0SIan Lepore 16793efdc63SAdrian Chadd /* 16893efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 16993efdc63SAdrian Chadd */ 1701bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1711bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1721bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1731bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 17493efdc63SAdrian Chadd 17593efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 17693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 17793efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 17893efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 17993efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 18093efdc63SAdrian Chadd 181831f5dcfSAlexander Motin static void 182831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 183831f5dcfSAlexander Motin { 1847e6ccea3SMarius Strobl 185831f5dcfSAlexander Motin if (error != 0) { 186831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 187831f5dcfSAlexander Motin return; 188831f5dcfSAlexander Motin } 189831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 190831f5dcfSAlexander Motin } 191831f5dcfSAlexander Motin 192d6b3aaf8SOleksandr Tymoshenko static int 193ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 194d6b3aaf8SOleksandr Tymoshenko { 19527d72fe1SBjoern A. Zeeb char buf[128]; 196d6b3aaf8SOleksandr Tymoshenko va_list ap; 197d6b3aaf8SOleksandr Tymoshenko int retval; 198d6b3aaf8SOleksandr Tymoshenko 19927d72fe1SBjoern A. Zeeb /* 20027d72fe1SBjoern A. Zeeb * Make sure we print a single line all together rather than in two 20127d72fe1SBjoern A. Zeeb * halves to avoid console gibberish bingo. 20227d72fe1SBjoern A. Zeeb */ 203d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 20427d72fe1SBjoern A. Zeeb retval = vsnprintf(buf, sizeof(buf), fmt, ap); 205d6b3aaf8SOleksandr Tymoshenko va_end(ap); 20627d72fe1SBjoern A. Zeeb 20727d72fe1SBjoern A. Zeeb retval += printf("%s-slot%d: %s", 20827d72fe1SBjoern A. Zeeb device_get_nameunit(slot->bus), slot->num, buf); 209d6b3aaf8SOleksandr Tymoshenko return (retval); 210d6b3aaf8SOleksandr Tymoshenko } 211d6b3aaf8SOleksandr Tymoshenko 212d00c1f7fSBartlomiej Grzesik static int 213d00c1f7fSBartlomiej Grzesik slot_sprintf(const struct sdhci_slot *slot, struct sbuf *s, 214d00c1f7fSBartlomiej Grzesik const char * fmt, ...) 215d00c1f7fSBartlomiej Grzesik { 216d00c1f7fSBartlomiej Grzesik va_list ap; 217d00c1f7fSBartlomiej Grzesik int retval; 218d00c1f7fSBartlomiej Grzesik 219d00c1f7fSBartlomiej Grzesik retval = sbuf_printf(s, "%s-slot%d: ", device_get_nameunit(slot->bus), slot->num); 220d00c1f7fSBartlomiej Grzesik 221d00c1f7fSBartlomiej Grzesik va_start(ap, fmt); 222d00c1f7fSBartlomiej Grzesik retval += sbuf_vprintf(s, fmt, ap); 223d00c1f7fSBartlomiej Grzesik va_end(ap); 224d00c1f7fSBartlomiej Grzesik 225d00c1f7fSBartlomiej Grzesik return (retval); 226d00c1f7fSBartlomiej Grzesik } 227d00c1f7fSBartlomiej Grzesik 228d00c1f7fSBartlomiej Grzesik static void 229d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(struct sdhci_slot *slot, struct sbuf *s) 230d00c1f7fSBartlomiej Grzesik { 231d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "============== REGISTER DUMP ==============\n"); 232d00c1f7fSBartlomiej Grzesik 233d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Sys addr: 0x%08x | Version: 0x%08x\n", 234d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 235d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 236d00c1f7fSBartlomiej Grzesik RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 237d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Argument: 0x%08x | Trn mode: 0x%08x\n", 238d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 239d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Present: 0x%08x | Host ctl: 0x%08x\n", 240d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 241d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Power: 0x%08x | Blk gap: 0x%08x\n", 242d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 243d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Wake-up: 0x%08x | Clock: 0x%08x\n", 244d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 245d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Timeout: 0x%08x | Int stat: 0x%08x\n", 246d00c1f7fSBartlomiej Grzesik RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 247d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 248d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 249d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 250d00c1f7fSBartlomiej Grzesik RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 251d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Caps: 0x%08x | Caps2: 0x%08x\n", 252d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 253d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 254d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 255d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 256d00c1f7fSBartlomiej Grzesik RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 257d00c1f7fSBartlomiej Grzesik 258d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "===========================================\n"); 259d00c1f7fSBartlomiej Grzesik } 260d00c1f7fSBartlomiej Grzesik 261831f5dcfSAlexander Motin static void 262831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 263831f5dcfSAlexander Motin { 264d00c1f7fSBartlomiej Grzesik struct sbuf s; 2657e6ccea3SMarius Strobl 2663ac5012eSBartlomiej Grzesik if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) { 2673ac5012eSBartlomiej Grzesik slot_printf(slot, "sdhci_dumpregs: Failed to allocate memory for sbuf\n"); 2683ac5012eSBartlomiej Grzesik return; 2693ac5012eSBartlomiej Grzesik } 2703ac5012eSBartlomiej Grzesik 271d00c1f7fSBartlomiej Grzesik sbuf_set_drain(&s, &sbuf_printf_drain, NULL); 272d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(slot, &s); 273d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 274d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 275d00c1f7fSBartlomiej Grzesik } 276831f5dcfSAlexander Motin 277d00c1f7fSBartlomiej Grzesik static int 278d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpregs(SYSCTL_HANDLER_ARGS) 279d00c1f7fSBartlomiej Grzesik { 280d00c1f7fSBartlomiej Grzesik struct sdhci_slot *slot = arg1; 281d00c1f7fSBartlomiej Grzesik struct sbuf s; 282831f5dcfSAlexander Motin 283d00c1f7fSBartlomiej Grzesik sbuf_new_for_sysctl(&s, NULL, 1024, req); 284d00c1f7fSBartlomiej Grzesik sbuf_putc(&s, '\n'); 285d00c1f7fSBartlomiej Grzesik sdhci_dumpregs_buf(slot, &s); 286d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 287d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 288d00c1f7fSBartlomiej Grzesik 289d00c1f7fSBartlomiej Grzesik return (0); 290d00c1f7fSBartlomiej Grzesik } 291d00c1f7fSBartlomiej Grzesik 292d00c1f7fSBartlomiej Grzesik static void 293d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(struct sdhci_slot *slot, struct sbuf *s) 294d00c1f7fSBartlomiej Grzesik { 295d00c1f7fSBartlomiej Grzesik int host_caps = slot->host.caps; 296d00c1f7fSBartlomiej Grzesik int caps = slot->caps; 297d00c1f7fSBartlomiej Grzesik 298d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, 299d00c1f7fSBartlomiej Grzesik "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 300d00c1f7fSBartlomiej Grzesik slot->max_clk / 1000000, 301d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 302d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 303d00c1f7fSBartlomiej Grzesik ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 304d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 305d00c1f7fSBartlomiej Grzesik (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 306d00c1f7fSBartlomiej Grzesik ((caps & SDHCI_CAN_VDD_180) && 307d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 308d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 309d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 310d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 311d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 312d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 313d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 314d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 315d00c1f7fSBartlomiej Grzesik (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 316d00c1f7fSBartlomiej Grzesik "removable"); 317d00c1f7fSBartlomiej Grzesik if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 318d00c1f7fSBartlomiej Grzesik MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 319d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "eMMC:%s%s%s%s\n", 320d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 321d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 322d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 323d00c1f7fSBartlomiej Grzesik ((host_caps & 324d00c1f7fSBartlomiej Grzesik (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 325d00c1f7fSBartlomiej Grzesik (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 326d00c1f7fSBartlomiej Grzesik " HS400ES" : ""); 327d00c1f7fSBartlomiej Grzesik if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 328d00c1f7fSBartlomiej Grzesik MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 329d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, "UHS-I:%s%s%s%s%s\n", 330d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 331d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 332d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 333d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 334d00c1f7fSBartlomiej Grzesik (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 335d00c1f7fSBartlomiej Grzesik if (slot->opt & SDHCI_TUNING_SUPPORTED) 336d00c1f7fSBartlomiej Grzesik slot_sprintf(slot, s, 337d00c1f7fSBartlomiej Grzesik "Re-tuning count %d secs, mode %d\n", 338d00c1f7fSBartlomiej Grzesik slot->retune_count, slot->retune_mode + 1); 339d00c1f7fSBartlomiej Grzesik } 340d00c1f7fSBartlomiej Grzesik 341d00c1f7fSBartlomiej Grzesik static void 342d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(struct sdhci_slot *slot) 343d00c1f7fSBartlomiej Grzesik { 344d00c1f7fSBartlomiej Grzesik struct sbuf s; 345d00c1f7fSBartlomiej Grzesik 3463ac5012eSBartlomiej Grzesik if (sbuf_new(&s, NULL, 1024, SBUF_NOWAIT | SBUF_AUTOEXTEND) == NULL) { 3473ac5012eSBartlomiej Grzesik slot_printf(slot, "sdhci_dumpcaps: Failed to allocate memory for sbuf\n"); 3483ac5012eSBartlomiej Grzesik return; 3493ac5012eSBartlomiej Grzesik } 3503ac5012eSBartlomiej Grzesik 351d00c1f7fSBartlomiej Grzesik sbuf_set_drain(&s, &sbuf_printf_drain, NULL); 352d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(slot, &s); 353d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 354d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 355d00c1f7fSBartlomiej Grzesik } 356d00c1f7fSBartlomiej Grzesik 357d00c1f7fSBartlomiej Grzesik static int 358d00c1f7fSBartlomiej Grzesik sdhci_syctl_dumpcaps(SYSCTL_HANDLER_ARGS) 359d00c1f7fSBartlomiej Grzesik { 360d00c1f7fSBartlomiej Grzesik struct sdhci_slot *slot = arg1; 361d00c1f7fSBartlomiej Grzesik struct sbuf s; 362d00c1f7fSBartlomiej Grzesik 363d00c1f7fSBartlomiej Grzesik sbuf_new_for_sysctl(&s, NULL, 1024, req); 364d00c1f7fSBartlomiej Grzesik sbuf_putc(&s, '\n'); 365d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps_buf(slot, &s); 366d00c1f7fSBartlomiej Grzesik sbuf_finish(&s); 367d00c1f7fSBartlomiej Grzesik sbuf_delete(&s); 368d00c1f7fSBartlomiej Grzesik 369d00c1f7fSBartlomiej Grzesik return (0); 370831f5dcfSAlexander Motin } 371831f5dcfSAlexander Motin 372aca38eabSMarius Strobl static uint32_t 373ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot) 374aca38eabSMarius Strobl { 375aca38eabSMarius Strobl uint32_t intmask; 376aca38eabSMarius Strobl 377aca38eabSMarius Strobl intmask = 0; 37878f8baa8SMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 379aca38eabSMarius Strobl intmask |= SDHCI_INT_TUNEERR; 380aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 381aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_3) 382aca38eabSMarius Strobl intmask |= SDHCI_INT_RETUNE; 383aca38eabSMarius Strobl } 384aca38eabSMarius Strobl return (intmask); 385aca38eabSMarius Strobl } 386aca38eabSMarius Strobl 387831f5dcfSAlexander Motin static void 388831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 389831f5dcfSAlexander Motin { 390831f5dcfSAlexander Motin 391b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 392831f5dcfSAlexander Motin 393831f5dcfSAlexander Motin /* Enable interrupts. */ 394831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 395831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 396831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 397831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 398831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 3993685b398SWarner Losh SDHCI_INT_ACMD12ERR; 400639f59f0SIan Lepore 401639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 402639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 403639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 404639f59f0SIan Lepore } 405639f59f0SIan Lepore 406cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 407831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 408831f5dcfSAlexander Motin } 409831f5dcfSAlexander Motin 410831f5dcfSAlexander Motin static void 411831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 412831f5dcfSAlexander Motin { 41393efdc63SAdrian Chadd uint32_t clk_base; 41493efdc63SAdrian Chadd uint32_t clk_sel; 415831f5dcfSAlexander Motin uint32_t res; 416831f5dcfSAlexander Motin uint16_t clk; 4178f3b7d56SOleksandr Tymoshenko uint16_t div; 418831f5dcfSAlexander Motin int timeout; 419831f5dcfSAlexander Motin 420831f5dcfSAlexander Motin if (clock == slot->clock) 421831f5dcfSAlexander Motin return; 422e00774a9SSøren Schmidt clock = SDHCI_SET_CLOCK(slot->bus, slot, clock); 423831f5dcfSAlexander Motin slot->clock = clock; 424831f5dcfSAlexander Motin 425831f5dcfSAlexander Motin /* Turn off the clock. */ 4264ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 4274ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 428b440e965SMarius Strobl /* If no clock requested - leave it so. */ 429831f5dcfSAlexander Motin if (clock == 0) 430831f5dcfSAlexander Motin return; 431ceb9e9f7SIan Lepore 43293efdc63SAdrian Chadd /* Determine the clock base frequency */ 43393efdc63SAdrian Chadd clk_base = slot->max_clk; 43493efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 4351bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 4361bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 43793efdc63SAdrian Chadd 4381bacf3beSMarius Strobl /* 4391bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 4401bacf3beSMarius Strobl */ 44193efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 44293efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 4431bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 4441bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44593efdc63SAdrian Chadd } else { 4461bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 4471bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44893efdc63SAdrian Chadd } 44993efdc63SAdrian Chadd 45093efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 45193efdc63SAdrian Chadd } 45293efdc63SAdrian Chadd 453ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 454ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 455ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 456ceb9e9f7SIan Lepore 4578f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 458831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 45993efdc63SAdrian Chadd res = clk_base; 46057677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 461831f5dcfSAlexander Motin if (res <= clock) 462831f5dcfSAlexander Motin break; 463831f5dcfSAlexander Motin res >>= 1; 464831f5dcfSAlexander Motin } 465831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 4668f3b7d56SOleksandr Tymoshenko div >>= 1; 467c11bbc7dSMarius Strobl } else { 4688f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 46993efdc63SAdrian Chadd if (clock >= clk_base) 47057677a3aSOleksandr Tymoshenko div = 0; 4718f3b7d56SOleksandr Tymoshenko else { 47257677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 47393efdc63SAdrian Chadd if ((clk_base / div) <= clock) 4748f3b7d56SOleksandr Tymoshenko break; 4758f3b7d56SOleksandr Tymoshenko } 4768f3b7d56SOleksandr Tymoshenko } 4778f3b7d56SOleksandr Tymoshenko div >>= 1; 4788f3b7d56SOleksandr Tymoshenko } 4798f3b7d56SOleksandr Tymoshenko 4808f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 48193efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 48293efdc63SAdrian Chadd div, clock, clk_base); 4838f3b7d56SOleksandr Tymoshenko 484831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4858f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4868f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4878f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4888f3b7d56SOleksandr Tymoshenko 489831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 490831f5dcfSAlexander Motin /* Enable clock. */ 491831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 492831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 493831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 494831f5dcfSAlexander Motin timeout = 10; 495831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 496831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 497831f5dcfSAlexander Motin if (timeout == 0) { 498831f5dcfSAlexander Motin slot_printf(slot, 499831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 500831f5dcfSAlexander Motin sdhci_dumpregs(slot); 501831f5dcfSAlexander Motin return; 502831f5dcfSAlexander Motin } 503831f5dcfSAlexander Motin timeout--; 504831f5dcfSAlexander Motin DELAY(1000); 505831f5dcfSAlexander Motin } 506831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 507831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 508831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 509831f5dcfSAlexander Motin } 510831f5dcfSAlexander Motin 511831f5dcfSAlexander Motin static void 512831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 513831f5dcfSAlexander Motin { 51485083a80SMarius Strobl int i; 515831f5dcfSAlexander Motin uint8_t pwr; 516831f5dcfSAlexander Motin 517831f5dcfSAlexander Motin if (slot->power == power) 518831f5dcfSAlexander Motin return; 519d6b3aaf8SOleksandr Tymoshenko 520831f5dcfSAlexander Motin slot->power = power; 521831f5dcfSAlexander Motin 522831f5dcfSAlexander Motin /* Turn off the power. */ 523831f5dcfSAlexander Motin pwr = 0; 524831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 525b440e965SMarius Strobl /* If power down requested - leave it so. */ 526831f5dcfSAlexander Motin if (power == 0) 527831f5dcfSAlexander Motin return; 528831f5dcfSAlexander Motin /* Set voltage. */ 529831f5dcfSAlexander Motin switch (1 << power) { 530831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 531831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 532831f5dcfSAlexander Motin break; 533831f5dcfSAlexander Motin case MMC_OCR_290_300: 534831f5dcfSAlexander Motin case MMC_OCR_300_310: 535831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 536831f5dcfSAlexander Motin break; 537831f5dcfSAlexander Motin case MMC_OCR_320_330: 538831f5dcfSAlexander Motin case MMC_OCR_330_340: 539831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 540831f5dcfSAlexander Motin break; 541831f5dcfSAlexander Motin } 542831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 54385083a80SMarius Strobl /* 54485083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 54585083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 5468022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 54785083a80SMarius Strobl */ 548831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 54985083a80SMarius Strobl for (i = 0; i < 20; i++) { 550831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 55185083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 55285083a80SMarius Strobl break; 55385083a80SMarius Strobl DELAY(100); 55485083a80SMarius Strobl } 55585083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 5564d52f81dSIan Lepore slot_printf(slot, "Bus power failed to enable\n"); 557a2832f9fSMarius Strobl 558a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 559a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 560a2832f9fSMarius Strobl DELAY(10); 561a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 562a2832f9fSMarius Strobl DELAY(300); 563a2832f9fSMarius Strobl } 564831f5dcfSAlexander Motin } 565831f5dcfSAlexander Motin 566831f5dcfSAlexander Motin static void 567831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 568831f5dcfSAlexander Motin { 569831f5dcfSAlexander Motin uint32_t data; 570831f5dcfSAlexander Motin char *buffer; 571831f5dcfSAlexander Motin size_t left; 572831f5dcfSAlexander Motin 573831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 574831f5dcfSAlexander Motin buffer += slot->offset; 575831f5dcfSAlexander Motin /* Transfer one block at a time. */ 5765d5ae066SIlya Bakulin #ifdef MMCCAM 5775d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) 5785d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 5795d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 5805d5ae066SIlya Bakulin else 5815d5ae066SIlya Bakulin #endif 582831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 583831f5dcfSAlexander Motin slot->offset += left; 584831f5dcfSAlexander Motin 585831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 586d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 587831f5dcfSAlexander Motin DELAY(10); 588ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 589831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 590831f5dcfSAlexander Motin while (left > 3) { 591831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 592831f5dcfSAlexander Motin buffer[0] = data; 593831f5dcfSAlexander Motin buffer[1] = (data >> 8); 594831f5dcfSAlexander Motin buffer[2] = (data >> 16); 595831f5dcfSAlexander Motin buffer[3] = (data >> 24); 596831f5dcfSAlexander Motin buffer += 4; 597831f5dcfSAlexander Motin left -= 4; 598831f5dcfSAlexander Motin } 599831f5dcfSAlexander Motin } else { 600d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 601831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 602831f5dcfSAlexander Motin left &= 3; 603831f5dcfSAlexander Motin } 604831f5dcfSAlexander Motin /* Handle uneven size case. */ 605831f5dcfSAlexander Motin if (left > 0) { 606831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 607831f5dcfSAlexander Motin while (left > 0) { 608831f5dcfSAlexander Motin *(buffer++) = data; 609831f5dcfSAlexander Motin data >>= 8; 610831f5dcfSAlexander Motin left--; 611831f5dcfSAlexander Motin } 612831f5dcfSAlexander Motin } 613831f5dcfSAlexander Motin } 614831f5dcfSAlexander Motin 615831f5dcfSAlexander Motin static void 616831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 617831f5dcfSAlexander Motin { 618831f5dcfSAlexander Motin uint32_t data = 0; 619831f5dcfSAlexander Motin char *buffer; 620831f5dcfSAlexander Motin size_t left; 621831f5dcfSAlexander Motin 622831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 623831f5dcfSAlexander Motin buffer += slot->offset; 624831f5dcfSAlexander Motin /* Transfer one block at a time. */ 6255d5ae066SIlya Bakulin #ifdef MMCCAM 6265d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) { 6275d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 6285d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 6295d5ae066SIlya Bakulin } else 6305d5ae066SIlya Bakulin #endif 631831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 632831f5dcfSAlexander Motin slot->offset += left; 633831f5dcfSAlexander Motin 634ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 635831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 636831f5dcfSAlexander Motin while (left > 3) { 637831f5dcfSAlexander Motin data = buffer[0] + 638831f5dcfSAlexander Motin (buffer[1] << 8) + 639831f5dcfSAlexander Motin (buffer[2] << 16) + 640831f5dcfSAlexander Motin (buffer[3] << 24); 641831f5dcfSAlexander Motin left -= 4; 642831f5dcfSAlexander Motin buffer += 4; 643831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 644831f5dcfSAlexander Motin } 645831f5dcfSAlexander Motin } else { 646d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 647831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 648831f5dcfSAlexander Motin left &= 3; 649831f5dcfSAlexander Motin } 650831f5dcfSAlexander Motin /* Handle uneven size case. */ 651831f5dcfSAlexander Motin if (left > 0) { 652831f5dcfSAlexander Motin while (left > 0) { 653831f5dcfSAlexander Motin data <<= 8; 654831f5dcfSAlexander Motin data += *(buffer++); 655831f5dcfSAlexander Motin left--; 656831f5dcfSAlexander Motin } 657831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 658831f5dcfSAlexander Motin } 659831f5dcfSAlexander Motin } 660831f5dcfSAlexander Motin 661831f5dcfSAlexander Motin static void 662831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 663831f5dcfSAlexander Motin { 664831f5dcfSAlexander Motin 665831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 666831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 667831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 668831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 669831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 670831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 671831f5dcfSAlexander Motin break; 672831f5dcfSAlexander Motin } 673831f5dcfSAlexander Motin } else { 674831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 675831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 676831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 677831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 678831f5dcfSAlexander Motin break; 679831f5dcfSAlexander Motin } 680831f5dcfSAlexander Motin } 681831f5dcfSAlexander Motin } 682831f5dcfSAlexander Motin 683831f5dcfSAlexander Motin static void 6847e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 685831f5dcfSAlexander Motin { 686831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 68703d49ffcSJohn Baldwin #ifndef MMCCAM 6887e6ccea3SMarius Strobl device_t d; 68903d49ffcSJohn Baldwin #endif 690831f5dcfSAlexander Motin 691831f5dcfSAlexander Motin SDHCI_LOCK(slot); 6926e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 693a94a63f0SWarner Losh #ifdef MMCCAM 694a94a63f0SWarner Losh if (slot->card_present == 0) { 695a94a63f0SWarner Losh #else 696831f5dcfSAlexander Motin if (slot->dev == NULL) { 697a94a63f0SWarner Losh #endif 698831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 699639f59f0SIan Lepore if (bootverbose || sdhci_debug) 700639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 701a94a63f0SWarner Losh #ifdef MMCCAM 702a94a63f0SWarner Losh slot->card_present = 1; 703c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 704a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 705a94a63f0SWarner Losh #else 706aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 707831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 708aca38eabSMarius Strobl if (d) { 709aca38eabSMarius Strobl device_set_ivars(d, slot); 710aca38eabSMarius Strobl (void)device_probe_and_attach(d); 711aca38eabSMarius Strobl } 712a94a63f0SWarner Losh #endif 713831f5dcfSAlexander Motin } else 714831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 715831f5dcfSAlexander Motin } else { 716a94a63f0SWarner Losh #ifdef MMCCAM 717a94a63f0SWarner Losh if (slot->card_present == 1) { 718a94a63f0SWarner Losh #else 719831f5dcfSAlexander Motin if (slot->dev != NULL) { 72003d49ffcSJohn Baldwin d = slot->dev; 721a94a63f0SWarner Losh #endif 722831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 723639f59f0SIan Lepore if (bootverbose || sdhci_debug) 724639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 725831f5dcfSAlexander Motin slot->dev = NULL; 726a94a63f0SWarner Losh #ifdef MMCCAM 727a94a63f0SWarner Losh slot->card_present = 0; 728c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 729a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 730a94a63f0SWarner Losh #else 731aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 732cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 733aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 734aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 735831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 736aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 737d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 738a94a63f0SWarner Losh #endif 739831f5dcfSAlexander Motin } else 740831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 741831f5dcfSAlexander Motin } 742831f5dcfSAlexander Motin } 743831f5dcfSAlexander Motin 744b8bf08b1SIan Lepore static void 745b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 746639f59f0SIan Lepore { 747639f59f0SIan Lepore bool was_present; 748639f59f0SIan Lepore 749639f59f0SIan Lepore /* 750639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 751639f59f0SIan Lepore * create the child device after a short delay. The delay is to 752639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 753639f59f0SIan Lepore * before the other pins have made good contact). 754639f59f0SIan Lepore * 755639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 756639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 757639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 758639f59f0SIan Lepore * that happens by deleting and recreating the child device. 759639f59f0SIan Lepore */ 760a94a63f0SWarner Losh #ifdef MMCCAM 761a94a63f0SWarner Losh was_present = slot->card_present; 762a94a63f0SWarner Losh #else 763639f59f0SIan Lepore was_present = slot->dev != NULL; 764a94a63f0SWarner Losh #endif 765639f59f0SIan Lepore if (!was_present && is_present) { 766639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 767639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 768639f59f0SIan Lepore } else if (was_present && !is_present) { 769639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 770639f59f0SIan Lepore } 771b8bf08b1SIan Lepore } 772b8bf08b1SIan Lepore 773b8bf08b1SIan Lepore void 774b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 775b8bf08b1SIan Lepore { 776b8bf08b1SIan Lepore 777b8bf08b1SIan Lepore SDHCI_LOCK(slot); 778b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 779639f59f0SIan Lepore SDHCI_UNLOCK(slot); 780639f59f0SIan Lepore } 781639f59f0SIan Lepore 782639f59f0SIan Lepore static void 783639f59f0SIan Lepore sdhci_card_poll(void *arg) 784639f59f0SIan Lepore { 785639f59f0SIan Lepore struct sdhci_slot *slot = arg; 786639f59f0SIan Lepore 787639f59f0SIan Lepore sdhci_handle_card_present(slot, 788639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 789639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 790639f59f0SIan Lepore sdhci_card_poll, slot); 791639f59f0SIan Lepore } 792639f59f0SIan Lepore 793ab00a509SMarius Strobl static int 794016f9657SMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot) 795ab00a509SMarius Strobl { 796ab00a509SMarius Strobl int err; 797ab00a509SMarius Strobl 798ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 799cd853791SKonstantin Belousov if (maxphys <= 1024 * 4) 800ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 801cd853791SKonstantin Belousov else if (maxphys <= 1024 * 8) 802ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 803cd853791SKonstantin Belousov else if (maxphys <= 1024 * 16) 804ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 805cd853791SKonstantin Belousov else if (maxphys <= 1024 * 32) 806ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 807cd853791SKonstantin Belousov else if (maxphys <= 1024 * 64) 808ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 809cd853791SKonstantin Belousov else if (maxphys <= 1024 * 128) 810ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 811cd853791SKonstantin Belousov else if (maxphys <= 1024 * 256) 812ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 813ab00a509SMarius Strobl else 814ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 815ab00a509SMarius Strobl } 816ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 817ab00a509SMarius Strobl 818ab00a509SMarius Strobl /* 819ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 820ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 821ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 822ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 823ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 824ab00a509SMarius Strobl * be aligned to the SDMA boundary. 825ab00a509SMarius Strobl */ 826ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 827016f9657SMarcin Wojtas 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 828ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 829ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 830ab00a509SMarius Strobl if (err != 0) { 831ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 832ab00a509SMarius Strobl return (err); 833ab00a509SMarius Strobl } 834ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 835ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 836ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 837ab00a509SMarius Strobl if (err != 0) { 838ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 839ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 840ab00a509SMarius Strobl return (err); 841ab00a509SMarius Strobl } 842ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 843ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 844ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 845ab00a509SMarius Strobl &slot->paddr, 0); 846ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 847ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 848ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 849ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 850ab00a509SMarius Strobl if (err) 851ab00a509SMarius Strobl return (err); 852ab00a509SMarius Strobl else 853ab00a509SMarius Strobl return (EFAULT); 854ab00a509SMarius Strobl } 855ab00a509SMarius Strobl 856ab00a509SMarius Strobl return (0); 857ab00a509SMarius Strobl } 858ab00a509SMarius Strobl 859ab00a509SMarius Strobl static void 860ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 861ab00a509SMarius Strobl { 862ab00a509SMarius Strobl 863ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 864ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 865ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 866ab00a509SMarius Strobl } 867ab00a509SMarius Strobl 868d6b3aaf8SOleksandr Tymoshenko int 869d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 870831f5dcfSAlexander Motin { 871aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 872aca38eabSMarius Strobl kobj_method_t *kobj_method; 8730f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 874d6b3aaf8SOleksandr Tymoshenko int err; 875d00c1f7fSBartlomiej Grzesik char node_name[8]; 876d00c1f7fSBartlomiej Grzesik struct sysctl_oid *node_oid; 877831f5dcfSAlexander Motin 878831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 879a94a63f0SWarner Losh 880d6b3aaf8SOleksandr Tymoshenko slot->num = num; 881d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 882d6b3aaf8SOleksandr Tymoshenko 883d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 884d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8850f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8868f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8870f34084fSMarius Strobl caps2 = slot->caps2; 8880f34084fSMarius Strobl } else { 889831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8900f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8910f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8920f34084fSMarius Strobl else 8930f34084fSMarius Strobl caps2 = 0; 8940f34084fSMarius Strobl } 8957fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8967fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8977fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 898ab00a509SMarius Strobl slot_printf(slot, 8997fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 9007fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 9017fcf4780SMarius Strobl return (ENXIO); 9027fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 9037fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 9047fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 9057fcf4780SMarius Strobl } 9067fcf4780SMarius Strobl } 907831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 90833aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 90987a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 91087a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 91133aad34dSOleksandr Tymoshenko else 91287a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 91387a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 91487a6a871SIan Lepore if (freq != 0) 91587a6a871SIan Lepore slot->max_clk = freq * 1000000; 91687a6a871SIan Lepore /* 91787a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 91887a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 91987a6a871SIan Lepore * with an assumption, so complain about it. 92087a6a871SIan Lepore */ 921831f5dcfSAlexander Motin if (slot->max_clk == 0) { 92287a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 923ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 9241bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 9251bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 926831f5dcfSAlexander Motin } 927a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 9288f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 9298f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 930a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 931a2832f9fSMarius Strobl slot->timeout_clk = 1000; 9328f3b7d56SOleksandr Tymoshenko } else { 9331bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 9341bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 9358f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 9368f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 9378f3b7d56SOleksandr Tymoshenko } 93887a6a871SIan Lepore /* 93987a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 94087a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 94187a6a871SIan Lepore * max timeout, but still mention it. 94287a6a871SIan Lepore */ 943831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 944ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 945ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 946ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 947831f5dcfSAlexander Motin } 948831f5dcfSAlexander Motin 94957677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 950831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 951831f5dcfSAlexander Motin slot->host.host_ocr = 0; 952831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 953831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 954831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 955831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 95649dfdf63SIan Lepore /* 95749dfdf63SIan Lepore * 1.8V VDD is not supposed to be used for removable cards. Hardware 95849dfdf63SIan Lepore * prior to v3.0 had no way to indicate embedded slots, but did 95949dfdf63SIan Lepore * sometimes support 1.8v for non-removable devices. 96049dfdf63SIan Lepore */ 96149dfdf63SIan Lepore if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 || 96249dfdf63SIan Lepore (slot->opt & SDHCI_SLOT_EMBEDDED))) 963831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 964831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 965ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 966831f5dcfSAlexander Motin "support voltages.\n"); 967831f5dcfSAlexander Motin } 968aca38eabSMarius Strobl 9695652be30SMarcin Wojtas host_caps = slot->host.caps; 9705652be30SMarcin Wojtas host_caps |= MMC_CAP_4_BIT_DATA; 9712d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 9720f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 973831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 9740f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 97572dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9760f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 97772dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9780f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 979aca38eabSMarius Strobl 980aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9810f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9820f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9830f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9840f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9850f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9860f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9870f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9880f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9890f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9900f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9910f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9920f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9930f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9940f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9950f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9960f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 997835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 998835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 999835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 1000aca38eabSMarius Strobl 1001aca38eabSMarius Strobl /* 1002aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 1003aca38eabSMarius Strobl * default NULL implementation. 1004aca38eabSMarius Strobl */ 1005aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 1006aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1007aca38eabSMarius Strobl kobj_desc); 1008aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1009aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1010aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 1011aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 1012aca38eabSMarius Strobl 1013aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 1014aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 1015aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 1016aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 1017aca38eabSMarius Strobl 1018aca38eabSMarius Strobl /* 1019aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 1020aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 1021aca38eabSMarius Strobl */ 1022aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 1023aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1024aca38eabSMarius Strobl kobj_desc); 1025aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1026aca38eabSMarius Strobl goto no_tuning; 1027aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 1028aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1029aca38eabSMarius Strobl kobj_desc); 1030aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 1031aca38eabSMarius Strobl no_tuning: 1032aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 1033aca38eabSMarius Strobl } 1034aca38eabSMarius Strobl 1035aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 1036aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 1037aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 1038aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 1039aca38eabSMarius Strobl M_WAITOK); 1040aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 1041aca38eabSMarius Strobl M_WAITOK); 1042aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 1043aca38eabSMarius Strobl M_WAITOK); 1044aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 1045aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 1046aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 1047aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 1048aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 1049aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 1050aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 1051aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 1052ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 1053aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 1054aca38eabSMarius Strobl slot->retune_count = 1; 1055aca38eabSMarius Strobl } else if (slot->retune_count != 0) 1056aca38eabSMarius Strobl slot->retune_count = 1057aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 1058aca38eabSMarius Strobl } 1059aca38eabSMarius Strobl } 1060aca38eabSMarius Strobl 1061aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 1062aca38eabSMarius Strobl 1063aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 10640f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 10650f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1066aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 10670f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 10680f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 1069aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 1070aca38eabSMarius Strobl 1071aca38eabSMarius Strobl /* 1072aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 1073aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 1074aca38eabSMarius Strobl * generic SDHCI implementation. 1075aca38eabSMarius Strobl */ 1076aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1077aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1078aca38eabSMarius Strobl kobj_desc); 1079aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1080aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1081aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1082aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1083aca38eabSMarius Strobl 1084aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1085f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10860f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1087f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10880f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1089f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10900f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10910f34084fSMarius Strobl slot->host.caps = host_caps; 10920f34084fSMarius Strobl 1093831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1094831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1095831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1096d6b3aaf8SOleksandr Tymoshenko 1097d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1098831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1099d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1100831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1101a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1102a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1103831f5dcfSAlexander Motin 1104c3a0f75aSOleksandr Tymoshenko /* 1105c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1106c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1107c3a0f75aSOleksandr Tymoshenko */ 1108c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1109c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1110c3a0f75aSOleksandr Tymoshenko 1111ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 1112016f9657SMarcin Wojtas err = sdhci_dma_alloc(slot); 1113ab00a509SMarius Strobl if (err != 0) { 1114ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1115ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1116ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1117ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1118ab00a509SMarius Strobl } 1119ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1120ab00a509SMarius Strobl return (err); 1121ab00a509SMarius Strobl } 1122ab00a509SMarius Strobl } 1123ab00a509SMarius Strobl 11245b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 1125d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(slot); 1126831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1127831f5dcfSAlexander Motin } 1128831f5dcfSAlexander Motin 1129ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1130ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1131ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 11324d52f81dSIan Lepore "timeout", CTLFLAG_RWTUN, &slot->timeout, 0, 1133ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1134831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1135639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1136639f59f0SIan Lepore sdhci_card_task, slot); 1137639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1138e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1139aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1140ba6fc1c7SLuiz Otavio O Souza 1141639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1142639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1143639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1144639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1145639f59f0SIan Lepore } 1146639f59f0SIan Lepore 1147aca38eabSMarius Strobl sdhci_init(slot); 1148aca38eabSMarius Strobl 1149d00c1f7fSBartlomiej Grzesik snprintf(node_name, sizeof(node_name), "slot%d", slot->num); 1150d00c1f7fSBartlomiej Grzesik 1151d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1152d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1153d00c1f7fSBartlomiej Grzesik OID_AUTO, node_name, CTLFLAG_RW, 0, "slot specific node"); 1154d00c1f7fSBartlomiej Grzesik 1155d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1156d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(node_oid), OID_AUTO, "debug", CTLFLAG_RW, 0, 1157d00c1f7fSBartlomiej Grzesik "Debugging node"); 1158d00c1f7fSBartlomiej Grzesik 1159d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1160d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpregs", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1161d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpregs, 1162d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI registers"); 1163d00c1f7fSBartlomiej Grzesik 1164d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1165d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpcaps", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1166d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpcaps, 1167d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI capabilites"); 1168d00c1f7fSBartlomiej Grzesik 1169831f5dcfSAlexander Motin return (0); 1170831f5dcfSAlexander Motin } 1171831f5dcfSAlexander Motin 1172d91f1a10SIlya Bakulin #ifndef MMCCAM 1173d6b3aaf8SOleksandr Tymoshenko void 1174d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1175831f5dcfSAlexander Motin { 11767e6ccea3SMarius Strobl 1177d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1178d6b3aaf8SOleksandr Tymoshenko } 1179d91f1a10SIlya Bakulin #endif 1180831f5dcfSAlexander Motin 1181d6b3aaf8SOleksandr Tymoshenko int 1182d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1183d6b3aaf8SOleksandr Tymoshenko { 1184831f5dcfSAlexander Motin device_t d; 1185831f5dcfSAlexander Motin 1186e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1187639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1188aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1189831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1190639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1191831f5dcfSAlexander Motin 1192831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1193831f5dcfSAlexander Motin d = slot->dev; 1194831f5dcfSAlexander Motin slot->dev = NULL; 1195831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1196831f5dcfSAlexander Motin if (d != NULL) 1197d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1198831f5dcfSAlexander Motin 1199831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1200b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1201831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1202ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1203ab00a509SMarius Strobl sdhci_dma_free(slot); 1204aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1205aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1206aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1207aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1208aca38eabSMarius Strobl } 1209d6b3aaf8SOleksandr Tymoshenko 1210831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1211d6b3aaf8SOleksandr Tymoshenko 1212831f5dcfSAlexander Motin return (0); 1213831f5dcfSAlexander Motin } 1214831f5dcfSAlexander Motin 1215d6b3aaf8SOleksandr Tymoshenko int 1216d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 121792bf0e27SAlexander Motin { 12187e6ccea3SMarius Strobl 1219aca38eabSMarius Strobl /* 1220aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1221aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1222aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1223aca38eabSMarius Strobl */ 1224aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1225aca38eabSMarius Strobl SDHCI_LOCK(slot); 1226aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1227b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1228aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 122992bf0e27SAlexander Motin 123092bf0e27SAlexander Motin return (0); 123192bf0e27SAlexander Motin } 123292bf0e27SAlexander Motin 1233d6b3aaf8SOleksandr Tymoshenko int 1234d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 123592bf0e27SAlexander Motin { 12367e6ccea3SMarius Strobl 1237aca38eabSMarius Strobl SDHCI_LOCK(slot); 1238d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1239aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 124092bf0e27SAlexander Motin 1241d6b3aaf8SOleksandr Tymoshenko return (0); 124292bf0e27SAlexander Motin } 124392bf0e27SAlexander Motin 1244b8f94506SArtur Rojek void 1245b8f94506SArtur Rojek sdhci_generic_reset(device_t brdev __unused, struct sdhci_slot *slot, 1246b8f94506SArtur Rojek uint8_t mask) 1247b8f94506SArtur Rojek { 1248b8f94506SArtur Rojek int timeout; 1249b8f94506SArtur Rojek uint32_t clock; 1250b8f94506SArtur Rojek 1251b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 1252b8f94506SArtur Rojek if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 1253b8f94506SArtur Rojek return; 1254b8f94506SArtur Rojek } 1255b8f94506SArtur Rojek 1256b8f94506SArtur Rojek /* Some controllers need this kick or reset won't work. */ 1257b8f94506SArtur Rojek if ((mask & SDHCI_RESET_ALL) == 0 && 1258b8f94506SArtur Rojek (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 1259b8f94506SArtur Rojek /* This is to force an update */ 1260b8f94506SArtur Rojek clock = slot->clock; 1261b8f94506SArtur Rojek slot->clock = 0; 1262b8f94506SArtur Rojek sdhci_set_clock(slot, clock); 1263b8f94506SArtur Rojek } 1264b8f94506SArtur Rojek 1265b8f94506SArtur Rojek if (mask & SDHCI_RESET_ALL) { 1266b8f94506SArtur Rojek slot->clock = 0; 1267b8f94506SArtur Rojek slot->power = 0; 1268b8f94506SArtur Rojek } 1269b8f94506SArtur Rojek 1270b8f94506SArtur Rojek WR1(slot, SDHCI_SOFTWARE_RESET, mask); 1271b8f94506SArtur Rojek 1272b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 1273b8f94506SArtur Rojek /* 1274b8f94506SArtur Rojek * Resets on TI OMAPs and AM335x are incompatible with SDHCI 1275b8f94506SArtur Rojek * specification. The reset bit has internal propagation delay, 1276b8f94506SArtur Rojek * so a fast read after write returns 0 even if reset process is 1277b8f94506SArtur Rojek * in progress. The workaround is to poll for 1 before polling 1278b8f94506SArtur Rojek * for 0. In the worst case, if we miss seeing it asserted the 1279b8f94506SArtur Rojek * time we spent waiting is enough to ensure the reset finishes. 1280b8f94506SArtur Rojek */ 1281b8f94506SArtur Rojek timeout = 10000; 1282b8f94506SArtur Rojek while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 1283b8f94506SArtur Rojek if (timeout <= 0) 1284b8f94506SArtur Rojek break; 1285b8f94506SArtur Rojek timeout--; 1286b8f94506SArtur Rojek DELAY(1); 1287b8f94506SArtur Rojek } 1288b8f94506SArtur Rojek } 1289b8f94506SArtur Rojek 1290b8f94506SArtur Rojek /* Wait max 100 ms */ 1291b8f94506SArtur Rojek timeout = 10000; 1292b8f94506SArtur Rojek /* Controller clears the bits when it's done */ 1293b8f94506SArtur Rojek while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 1294b8f94506SArtur Rojek if (timeout <= 0) { 1295b8f94506SArtur Rojek slot_printf(slot, "Reset 0x%x never completed.\n", 1296b8f94506SArtur Rojek mask); 1297b8f94506SArtur Rojek sdhci_dumpregs(slot); 1298b8f94506SArtur Rojek return; 1299b8f94506SArtur Rojek } 1300b8f94506SArtur Rojek timeout--; 1301b8f94506SArtur Rojek DELAY(10); 1302b8f94506SArtur Rojek } 1303b8f94506SArtur Rojek } 1304b8f94506SArtur Rojek 130557677a3aSOleksandr Tymoshenko uint32_t 1306b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 130757677a3aSOleksandr Tymoshenko { 13087e6ccea3SMarius Strobl 130957677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 131057677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 131157677a3aSOleksandr Tymoshenko else 131257677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 131357677a3aSOleksandr Tymoshenko } 131457677a3aSOleksandr Tymoshenko 13156e37fb2bSIan Lepore bool 1316b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 13176e37fb2bSIan Lepore { 13186e37fb2bSIan Lepore 1319639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1320639f59f0SIan Lepore return true; 1321639f59f0SIan Lepore 13226e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 13236e37fb2bSIan Lepore } 13246e37fb2bSIan Lepore 13250f34084fSMarius Strobl void 13260f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 13270f34084fSMarius Strobl { 1328ab00a509SMarius Strobl const struct mmc_ios *ios; 13290f34084fSMarius Strobl uint16_t hostctrl2; 13300f34084fSMarius Strobl 13310f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 13320f34084fSMarius Strobl return; 13330f34084fSMarius Strobl 1334aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 13350f34084fSMarius Strobl ios = &slot->host.ios; 13360f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13370f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13380f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1339aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 13400f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 13410f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 13420f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1343aca38eabSMarius Strobl else 13440f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1345aca38eabSMarius Strobl } 13460f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 13470f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 13480f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 13490f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 13500f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 13510f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 13520f34084fSMarius Strobl else 13530f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 13540f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 13550f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 13560f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13570f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 13580f34084fSMarius Strobl } 13590f34084fSMarius Strobl 1360d6b3aaf8SOleksandr Tymoshenko int 1361d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1362831f5dcfSAlexander Motin { 1363831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1364831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1365831f5dcfSAlexander Motin 1366831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1367831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1368831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1369831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1370831f5dcfSAlexander Motin sdhci_init(slot); 1371831f5dcfSAlexander Motin } 1372831f5dcfSAlexander Motin /* Configure the bus. */ 1373831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1374831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 13752d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 13762d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1377831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13782d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 13792d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13802d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 13812d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 13822d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13832d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13842d1731b8SIan Lepore } else { 13852d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 13862d1731b8SIan Lepore } 13870f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1388bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1389831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1390831f5dcfSAlexander Motin else 1391831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1392831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 13930f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1394831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1395d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1396b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1397b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1398831f5dcfSAlexander Motin 1399831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1400831f5dcfSAlexander Motin return (0); 1401831f5dcfSAlexander Motin } 1402831f5dcfSAlexander Motin 14030f34084fSMarius Strobl int 14040f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 14050f34084fSMarius Strobl { 14060f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 14070f34084fSMarius Strobl enum mmc_vccq vccq; 14080f34084fSMarius Strobl int err; 14090f34084fSMarius Strobl uint16_t hostctrl2; 14100f34084fSMarius Strobl 14110f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 14120f34084fSMarius Strobl return (0); 14130f34084fSMarius Strobl 14140f34084fSMarius Strobl err = 0; 14150f34084fSMarius Strobl vccq = slot->host.ios.vccq; 14160f34084fSMarius Strobl SDHCI_LOCK(slot); 14170f34084fSMarius Strobl sdhci_set_clock(slot, 0); 14180f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14190f34084fSMarius Strobl switch (vccq) { 14200f34084fSMarius Strobl case vccq_330: 14210f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14220f34084fSMarius Strobl goto done; 14230f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 14240f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14250f34084fSMarius Strobl DELAY(5000); 14260f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14270f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14280f34084fSMarius Strobl goto done; 14290f34084fSMarius Strobl err = EAGAIN; 14300f34084fSMarius Strobl break; 14310f34084fSMarius Strobl case vccq_180: 14320f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 14330f34084fSMarius Strobl err = EINVAL; 14340f34084fSMarius Strobl goto done; 14350f34084fSMarius Strobl } 14360f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14370f34084fSMarius Strobl goto done; 14380f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 14390f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14400f34084fSMarius Strobl DELAY(5000); 14410f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14420f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14430f34084fSMarius Strobl goto done; 14440f34084fSMarius Strobl err = EAGAIN; 14450f34084fSMarius Strobl break; 14460f34084fSMarius Strobl default: 14470f34084fSMarius Strobl slot_printf(slot, 14480f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 14490f34084fSMarius Strobl err = EINVAL; 14500f34084fSMarius Strobl break; 14510f34084fSMarius Strobl } 14520f34084fSMarius Strobl done: 14530f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 14540f34084fSMarius Strobl SDHCI_UNLOCK(slot); 14550f34084fSMarius Strobl return (err); 14560f34084fSMarius Strobl } 14570f34084fSMarius Strobl 1458aca38eabSMarius Strobl int 1459aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1460aca38eabSMarius Strobl { 1461aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1462ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1463aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1464aca38eabSMarius Strobl struct mmc_data *tune_data; 1465aca38eabSMarius Strobl uint32_t opcode; 1466aca38eabSMarius Strobl int err; 1467aca38eabSMarius Strobl 1468aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1469aca38eabSMarius Strobl return (0); 1470aca38eabSMarius Strobl 1471aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1472aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1473aca38eabSMarius Strobl SDHCI_LOCK(slot); 1474aca38eabSMarius Strobl switch (ios->timing) { 1475aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1476aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1477aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1478aca38eabSMarius Strobl return (EINVAL); 1479aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1480aca38eabSMarius Strobl /* 1481aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1482aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1483aca38eabSMarius Strobl * expected to be required. 1484aca38eabSMarius Strobl */ 1485aca38eabSMarius Strobl if (hs400) 1486aca38eabSMarius Strobl slot->retune_ticks = 0; 1487aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1488aca38eabSMarius Strobl break; 1489aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1490aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1491aca38eabSMarius Strobl break; 1492aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1493aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1494aca38eabSMarius Strobl break; 1495aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1496aca38eabSMarius Strobl return (0); 1497bd15d31cSMarius Strobl default: 1498bd15d31cSMarius Strobl slot_printf(slot, "Tuning requested but not required.\n"); 1499bd15d31cSMarius Strobl SDHCI_UNLOCK(slot); 1500bd15d31cSMarius Strobl return (EINVAL); 1501aca38eabSMarius Strobl } 1502aca38eabSMarius Strobl 1503aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1504aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1505aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1506aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1507aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1508aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1509aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1510aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1511aca38eabSMarius Strobl MMC_TUNING_LEN; 1512aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1513aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1514aca38eabSMarius Strobl 1515aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1516aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1517aca38eabSMarius Strobl if (err == 0) { 1518aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1519aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1520cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1521aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1522aca38eabSMarius Strobl if (slot->retune_ticks) { 1523aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1524aca38eabSMarius Strobl sdhci_retune, slot); 1525aca38eabSMarius Strobl } 1526aca38eabSMarius Strobl } 1527aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1528aca38eabSMarius Strobl return (err); 1529aca38eabSMarius Strobl } 1530aca38eabSMarius Strobl 1531aca38eabSMarius Strobl int 1532aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1533aca38eabSMarius Strobl { 1534aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1535aca38eabSMarius Strobl int err; 1536aca38eabSMarius Strobl 1537aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1538aca38eabSMarius Strobl return (0); 1539aca38eabSMarius Strobl 1540aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1541aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1542aca38eabSMarius Strobl return (EINVAL); 1543aca38eabSMarius Strobl 1544aca38eabSMarius Strobl SDHCI_LOCK(slot); 1545aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1546aca38eabSMarius Strobl /* 1547aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1548aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1549aca38eabSMarius Strobl * with the host properly acquired, and 1550aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1551aca38eabSMarius Strobl * 1552aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1553aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1554aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1555aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1556aca38eabSMarius Strobl */ 1557aca38eabSMarius Strobl if (slot->retune_ticks) { 1558aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1559aca38eabSMarius Strobl sdhci_retune, slot); 1560aca38eabSMarius Strobl } 1561aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1562aca38eabSMarius Strobl return (err); 1563aca38eabSMarius Strobl } 1564aca38eabSMarius Strobl 1565aca38eabSMarius Strobl static int 1566aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1567aca38eabSMarius Strobl { 1568aca38eabSMarius Strobl struct mmc_request *tune_req; 1569aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1570aca38eabSMarius Strobl int i; 1571aca38eabSMarius Strobl uint32_t intmask; 1572aca38eabSMarius Strobl uint16_t hostctrl2; 1573aca38eabSMarius Strobl u_char opt; 1574aca38eabSMarius Strobl 1575aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1576aca38eabSMarius Strobl if (slot->req != NULL) 1577aca38eabSMarius Strobl return (EBUSY); 1578aca38eabSMarius Strobl 1579aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1580aca38eabSMarius Strobl opt = slot->opt; 1581aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1582aca38eabSMarius Strobl 1583aca38eabSMarius Strobl /* 1584aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1585aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1586aca38eabSMarius Strobl */ 1587aca38eabSMarius Strobl intmask = slot->intmask; 1588aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1589cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1590aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1591aca38eabSMarius Strobl 1592aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1593aca38eabSMarius Strobl if (reset) 1594aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1595aca38eabSMarius Strobl else 1596aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1597aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1598aca38eabSMarius Strobl 1599aca38eabSMarius Strobl tune_req = slot->tune_req; 1600aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1601aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1602aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1603aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1604aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1605aca38eabSMarius Strobl tune_req->done_data = slot; 1606aca38eabSMarius Strobl slot->req = tune_req; 1607aca38eabSMarius Strobl slot->flags = 0; 1608aca38eabSMarius Strobl sdhci_start(slot); 1609aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1610aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1611aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1612aca38eabSMarius Strobl break; 1613aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1614aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1615aca38eabSMarius Strobl break; 1616aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1617aca38eabSMarius Strobl DELAY(1000); 1618aca38eabSMarius Strobl } 1619aca38eabSMarius Strobl 162078f8baa8SMarius Strobl /* 162178f8baa8SMarius Strobl * Restore DMA usage and interrupts. 162278f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 162378f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 162478f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 162578f8baa8SMarius Strobl * doesn't lose these. 162678f8baa8SMarius Strobl */ 1627aca38eabSMarius Strobl slot->opt = opt; 1628aca38eabSMarius Strobl slot->intmask = intmask; 162978f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 163078f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1631aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1632aca38eabSMarius Strobl 1633aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1634aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1635aca38eabSMarius Strobl slot->retune_req = 0; 1636aca38eabSMarius Strobl return (0); 1637aca38eabSMarius Strobl } 1638aca38eabSMarius Strobl 1639aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1640aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1641aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1642b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1643aca38eabSMarius Strobl return (EIO); 1644aca38eabSMarius Strobl } 1645aca38eabSMarius Strobl 1646aca38eabSMarius Strobl static void 1647aca38eabSMarius Strobl sdhci_retune(void *arg) 1648aca38eabSMarius Strobl { 1649aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1650aca38eabSMarius Strobl 1651aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1652aca38eabSMarius Strobl } 1653aca38eabSMarius Strobl 1654a94a63f0SWarner Losh #ifdef MMCCAM 1655a94a63f0SWarner Losh static void 1656a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1657a94a63f0SWarner Losh { 1658a94a63f0SWarner Losh union ccb *ccb; 165915c440e1SWarner Losh 1660aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 166115c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1662a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1663a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1664a94a63f0SWarner Losh ccb = slot->ccb; 1665a94a63f0SWarner Losh slot->ccb = NULL; 1666a94a63f0SWarner Losh slot->curcmd = NULL; 1667a94a63f0SWarner Losh 1668a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1669a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1670a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1671a94a63f0SWarner Losh 1672a94a63f0SWarner Losh ccb->ccb_h.status = 1673a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1674a94a63f0SWarner Losh xpt_done(ccb); 1675a94a63f0SWarner Losh } 1676a94a63f0SWarner Losh } 1677a94a63f0SWarner Losh #else 1678831f5dcfSAlexander Motin static void 1679e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1680e64f01a9SIan Lepore { 1681e64f01a9SIan Lepore struct mmc_request *req; 1682e64f01a9SIan Lepore 1683e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1684e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1685e64f01a9SIan Lepore req = slot->req; 1686e64f01a9SIan Lepore slot->req = NULL; 1687e64f01a9SIan Lepore slot->curcmd = NULL; 1688e64f01a9SIan Lepore req->done(req); 1689e64f01a9SIan Lepore } 1690e64f01a9SIan Lepore } 1691a94a63f0SWarner Losh #endif 1692e64f01a9SIan Lepore 1693e64f01a9SIan Lepore static void 1694aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1695aca38eabSMarius Strobl { 1696aca38eabSMarius Strobl 1697aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1698aca38eabSMarius Strobl wakeup(req); 1699aca38eabSMarius Strobl } 1700aca38eabSMarius Strobl 1701aca38eabSMarius Strobl static void 1702e64f01a9SIan Lepore sdhci_timeout(void *arg) 1703e64f01a9SIan Lepore { 1704e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1705e64f01a9SIan Lepore 1706e64f01a9SIan Lepore if (slot->curcmd != NULL) { 17077e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 17087e586643SIan Lepore sdhci_dumpregs(slot); 1709b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1710b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1711e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1712e64f01a9SIan Lepore sdhci_req_done(slot); 17137e586643SIan Lepore } else { 17147e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1715e64f01a9SIan Lepore } 1716e64f01a9SIan Lepore } 1717e64f01a9SIan Lepore 1718e64f01a9SIan Lepore static void 1719ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1720831f5dcfSAlexander Motin { 1721831f5dcfSAlexander Motin uint16_t mode; 1722831f5dcfSAlexander Motin 1723831f5dcfSAlexander Motin if (data == NULL) 1724831f5dcfSAlexander Motin return; 1725831f5dcfSAlexander Motin 1726831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 17275d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1728831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 17295d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 17306dea80e6SMarius Strobl #ifdef MMCCAM 17316dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 17326dea80e6SMarius Strobl #else 17330519c933SMarius Strobl slot->req->stop != NULL && 17346dea80e6SMarius Strobl #endif 17356dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 17366dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 17376dea80e6SMarius Strobl } 1738831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1739831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1740831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1741831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1742831f5dcfSAlexander Motin 1743831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1744831f5dcfSAlexander Motin } 1745831f5dcfSAlexander Motin 1746831f5dcfSAlexander Motin static void 1747831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1748831f5dcfSAlexander Motin { 1749831f5dcfSAlexander Motin int flags, timeout; 175090993663SIan Lepore uint32_t mask; 1751831f5dcfSAlexander Motin 1752831f5dcfSAlexander Motin slot->curcmd = cmd; 1753831f5dcfSAlexander Motin slot->cmd_done = 0; 1754831f5dcfSAlexander Motin 1755831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1756831f5dcfSAlexander Motin 1757831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1758831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1759831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1760831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1761e64f01a9SIan Lepore sdhci_req_done(slot); 1762831f5dcfSAlexander Motin return; 1763831f5dcfSAlexander Motin } 1764831f5dcfSAlexander Motin 1765b440e965SMarius Strobl /* 1766b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1767b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1768b440e965SMarius Strobl */ 17696e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1770d8208d9eSAlexander Motin slot->power == 0 || 1771d8208d9eSAlexander Motin slot->clock == 0) { 1772a94a63f0SWarner Losh slot_printf(slot, 1773440c645bSMitchell Horne "Cannot issue a command (power=%d clock=%d)\n", 1774a94a63f0SWarner Losh slot->power, slot->clock); 1775831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1776e64f01a9SIan Lepore sdhci_req_done(slot); 1777831f5dcfSAlexander Motin return; 1778831f5dcfSAlexander Motin } 1779831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1780831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1781831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1782a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1783831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1784aca38eabSMarius Strobl /* 1785aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1786aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1787aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1788aca38eabSMarius Strobl */ 1789a94a63f0SWarner Losh #ifdef MMCCAM 1790aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1791a94a63f0SWarner Losh #else 1792aca38eabSMarius Strobl if (cmd == slot->req->stop || 1793a94a63f0SWarner Losh #endif 1794aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1795aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1796aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 17978775ab45SIan Lepore /* 17988775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 17998775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 18008775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 18018775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 18028775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 18038775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 18048775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 18058775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 18068775ab45SIan Lepore */ 18078775ab45SIan Lepore timeout = 250; 180890993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1809831f5dcfSAlexander Motin if (timeout == 0) { 1810831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1811831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1812831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1813831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1814e64f01a9SIan Lepore sdhci_req_done(slot); 1815831f5dcfSAlexander Motin return; 1816831f5dcfSAlexander Motin } 1817831f5dcfSAlexander Motin timeout--; 1818831f5dcfSAlexander Motin DELAY(1000); 1819831f5dcfSAlexander Motin } 1820831f5dcfSAlexander Motin 1821831f5dcfSAlexander Motin /* Prepare command flags. */ 1822831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1823831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1824831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1825831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1826831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1827831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1828831f5dcfSAlexander Motin else 1829831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1830831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1831831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1832831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1833831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1834a94a63f0SWarner Losh if (cmd->data != NULL) 1835831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1836831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1837831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1838831f5dcfSAlexander Motin /* Prepare data. */ 1839831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1840831f5dcfSAlexander Motin /* 1841831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1842831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1843831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1844831f5dcfSAlexander Motin */ 1845831f5dcfSAlexander Motin if (slot->data_done == 0) { 1846831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1847831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1848831f5dcfSAlexander Motin } 1849831f5dcfSAlexander Motin /* Set command argument. */ 1850831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1851831f5dcfSAlexander Motin /* Set data transfer mode. */ 1852831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1853aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18548adf4202SBjoern A. Zeeb slot_printf(slot, "Starting command opcode %#04x flags %#04x\n", 18558adf4202SBjoern A. Zeeb cmd->opcode, flags); 18568adf4202SBjoern A. Zeeb 1857831f5dcfSAlexander Motin /* Start command. */ 1858d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1859a6873fd1SIan Lepore /* Start timeout callout. */ 1860ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1861ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1862831f5dcfSAlexander Motin } 1863831f5dcfSAlexander Motin 1864831f5dcfSAlexander Motin static void 1865831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1866831f5dcfSAlexander Motin { 1867831f5dcfSAlexander Motin int i; 18681bacf3beSMarius Strobl uint32_t val; 18691bacf3beSMarius Strobl uint8_t extra; 1870831f5dcfSAlexander Motin 1871aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18728adf4202SBjoern A. Zeeb slot_printf(slot, "%s: called, err %d flags %#04x\n", 1873a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1874831f5dcfSAlexander Motin slot->cmd_done = 1; 187572dec079SMarius Strobl /* 187672dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1877831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 187872dec079SMarius Strobl * happened first. 187972dec079SMarius Strobl */ 1880aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1881aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1882aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1883aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1884831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1885831f5dcfSAlexander Motin if (slot->curcmd->error) { 1886aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1887aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1888b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 1889b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 1890831f5dcfSAlexander Motin sdhci_start(slot); 1891831f5dcfSAlexander Motin return; 1892831f5dcfSAlexander Motin } 1893831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1894831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1895831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1896831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 18971bacf3beSMarius Strobl extra = 0; 1898831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 18991bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 19001bacf3beSMarius Strobl if (slot->quirks & 19011bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1902677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1903677ee494SIan Lepore else { 1904677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1905677ee494SIan Lepore (val << 8) | extra; 1906831f5dcfSAlexander Motin extra = val >> 24; 1907831f5dcfSAlexander Motin } 1908677ee494SIan Lepore } 1909831f5dcfSAlexander Motin } else 1910831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1911831f5dcfSAlexander Motin } 1912aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 19138adf4202SBjoern A. Zeeb slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n", 1914a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1915a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1916a94a63f0SWarner Losh 1917831f5dcfSAlexander Motin /* If data ready - finish. */ 1918831f5dcfSAlexander Motin if (slot->data_done) 1919831f5dcfSAlexander Motin sdhci_start(slot); 1920831f5dcfSAlexander Motin } 1921831f5dcfSAlexander Motin 1922831f5dcfSAlexander Motin static void 1923ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1924831f5dcfSAlexander Motin { 1925ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1926831f5dcfSAlexander Motin uint8_t div; 1927831f5dcfSAlexander Motin 1928831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1929831f5dcfSAlexander Motin slot->data_done = 1; 1930831f5dcfSAlexander Motin return; 1931831f5dcfSAlexander Motin } 1932831f5dcfSAlexander Motin 1933831f5dcfSAlexander Motin slot->data_done = 0; 1934831f5dcfSAlexander Motin 1935831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1936831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1937ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1938ceb9e9f7SIan Lepore div = 0xE; 1939ceb9e9f7SIan Lepore } else { 1940831f5dcfSAlexander Motin target_timeout = 1000000; 1941831f5dcfSAlexander Motin div = 0; 1942831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1943ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1944ceb9e9f7SIan Lepore ++div; 1945831f5dcfSAlexander Motin current_timeout <<= 1; 1946831f5dcfSAlexander Motin } 1947831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1948ceb9e9f7SIan Lepore if (div < 0xE && 1949ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1950ceb9e9f7SIan Lepore ++div; 1951831f5dcfSAlexander Motin } 1952ceb9e9f7SIan Lepore } 1953831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1954831f5dcfSAlexander Motin 1955831f5dcfSAlexander Motin if (data == NULL) 1956831f5dcfSAlexander Motin return; 1957831f5dcfSAlexander Motin 1958831f5dcfSAlexander Motin /* Use DMA if possible. */ 1959831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1960831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1961ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1962d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1963831f5dcfSAlexander Motin (data->len <= 512)) 1964831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1965831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1966d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1967831f5dcfSAlexander Motin ((data->len) & 0x3)) 1968831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1969831f5dcfSAlexander Motin /* Load DMA buffer. */ 1970831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1971ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1972831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1973ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1974ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1975831f5dcfSAlexander Motin else { 1976ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1977ab00a509SMarius Strobl sdma_bbufsz)); 1978ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1979ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1980831f5dcfSAlexander Motin } 1981831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1982ab00a509SMarius Strobl /* 1983ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1984ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1985ab00a509SMarius Strobl */ 1986ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1987831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1988831f5dcfSAlexander Motin else 1989831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1990831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1991831f5dcfSAlexander Motin } 1992831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1993831f5dcfSAlexander Motin slot->offset = 0; 19945d5ae066SIlya Bakulin #ifdef MMCCAM 19955d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 19965d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 19975d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 19985d5ae066SIlya Bakulin blkcnt = data->block_count; 19995d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 20005d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 20015d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 20025d5ae066SIlya Bakulin } else 20035d5ae066SIlya Bakulin #endif 20045d5ae066SIlya Bakulin { 2005ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 2006ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 2007ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 20085d5ae066SIlya Bakulin } 20095d5ae066SIlya Bakulin 20105d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 2011ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 2012aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2013ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 2014ab00a509SMarius Strobl blksz, blkcnt); 2015831f5dcfSAlexander Motin } 2016831f5dcfSAlexander Motin 2017c3a0f75aSOleksandr Tymoshenko void 2018831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 2019831f5dcfSAlexander Motin { 2020831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 20217e6ccea3SMarius Strobl size_t left; 2022831f5dcfSAlexander Motin 2023831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 2024ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 2025831f5dcfSAlexander Motin * happened first. */ 2026831f5dcfSAlexander Motin if (!slot->cmd_done) { 2027831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 2028831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 2029831f5dcfSAlexander Motin } 2030831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 2031915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 2032915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 2033831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 20347e6ccea3SMarius Strobl left = data->len - slot->offset; 2035ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2036ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 2037831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2038ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 2039831f5dcfSAlexander Motin } else 2040ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2041ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 2042831f5dcfSAlexander Motin } 2043a98788edSIan Lepore slot->data_done = 1; 2044831f5dcfSAlexander Motin /* If there was error - reset the host. */ 2045831f5dcfSAlexander Motin if (slot->curcmd->error) { 2046aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 2047aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2048b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2049b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2050831f5dcfSAlexander Motin sdhci_start(slot); 2051831f5dcfSAlexander Motin return; 2052831f5dcfSAlexander Motin } 2053831f5dcfSAlexander Motin /* If we already have command response - finish. */ 2054831f5dcfSAlexander Motin if (slot->cmd_done) 2055831f5dcfSAlexander Motin sdhci_start(slot); 2056831f5dcfSAlexander Motin } 2057831f5dcfSAlexander Motin 2058a94a63f0SWarner Losh #ifdef MMCCAM 2059a94a63f0SWarner Losh static void 2060a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 2061a94a63f0SWarner Losh { 2062a94a63f0SWarner Losh union ccb *ccb; 2063ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 2064a94a63f0SWarner Losh 2065a94a63f0SWarner Losh ccb = slot->ccb; 2066a94a63f0SWarner Losh if (ccb == NULL) 2067a94a63f0SWarner Losh return; 2068a94a63f0SWarner Losh 2069a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2070a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 2071a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 2072a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 2073a94a63f0SWarner Losh return; 2074a94a63f0SWarner Losh } 2075a94a63f0SWarner Losh 2076a94a63f0SWarner Losh /* 2077a94a63f0SWarner Losh * Old stack doesn't use this! 2078a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 2079a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 2080a94a63f0SWarner Losh * Not using this code does no harm... 2081a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 2082a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 2083a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 2084a94a63f0SWarner Losh return; 2085a94a63f0SWarner Losh } 2086a94a63f0SWarner Losh */ 2087aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2088a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 2089a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 2090a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 2091b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2092b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2093a94a63f0SWarner Losh } 2094a94a63f0SWarner Losh 2095a94a63f0SWarner Losh sdhci_req_done(slot); 2096a94a63f0SWarner Losh } 2097a94a63f0SWarner Losh #else 2098831f5dcfSAlexander Motin static void 2099831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 2100831f5dcfSAlexander Motin { 2101ab00a509SMarius Strobl const struct mmc_request *req; 2102831f5dcfSAlexander Motin 2103831f5dcfSAlexander Motin req = slot->req; 2104831f5dcfSAlexander Motin if (req == NULL) 2105831f5dcfSAlexander Motin return; 2106831f5dcfSAlexander Motin 2107831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 2108831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 2109831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 2110831f5dcfSAlexander Motin return; 2111831f5dcfSAlexander Motin } 2112915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 2113915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 2114831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 2115831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 2116831f5dcfSAlexander Motin return; 2117831f5dcfSAlexander Motin } 2118aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 21195b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 21205b69a497SAlexander Motin if (!req->cmd->error && 2121915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2122915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2123915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2124b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2125b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2126831f5dcfSAlexander Motin } 2127831f5dcfSAlexander Motin 2128e64f01a9SIan Lepore sdhci_req_done(slot); 2129831f5dcfSAlexander Motin } 2130a94a63f0SWarner Losh #endif 2131831f5dcfSAlexander Motin 2132d6b3aaf8SOleksandr Tymoshenko int 2133b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2134b440e965SMarius Strobl struct mmc_request *req) 2135831f5dcfSAlexander Motin { 2136831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2137831f5dcfSAlexander Motin 2138831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2139831f5dcfSAlexander Motin if (slot->req != NULL) { 2140831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2141831f5dcfSAlexander Motin return (EBUSY); 2142831f5dcfSAlexander Motin } 2143aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 21441bacf3beSMarius Strobl slot_printf(slot, 21451bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2146831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 21475b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 21485b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 21495b69a497SAlexander Motin } 2150831f5dcfSAlexander Motin slot->req = req; 2151831f5dcfSAlexander Motin slot->flags = 0; 2152831f5dcfSAlexander Motin sdhci_start(slot); 2153831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2154bea2dca2SAlexander Motin if (dumping) { 2155bea2dca2SAlexander Motin while (slot->req != NULL) { 2156d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2157bea2dca2SAlexander Motin DELAY(10); 2158bea2dca2SAlexander Motin } 2159bea2dca2SAlexander Motin } 2160831f5dcfSAlexander Motin return (0); 2161831f5dcfSAlexander Motin } 2162831f5dcfSAlexander Motin 2163d6b3aaf8SOleksandr Tymoshenko int 2164b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2165831f5dcfSAlexander Motin { 2166831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2167831f5dcfSAlexander Motin uint32_t val; 2168831f5dcfSAlexander Motin 2169831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2170831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2171831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2172831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2173831f5dcfSAlexander Motin } 2174831f5dcfSAlexander Motin 2175d6b3aaf8SOleksandr Tymoshenko int 2176b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2177831f5dcfSAlexander Motin { 2178831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2179831f5dcfSAlexander Motin int err = 0; 2180831f5dcfSAlexander Motin 2181831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2182831f5dcfSAlexander Motin while (slot->bus_busy) 2183d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2184831f5dcfSAlexander Motin slot->bus_busy++; 2185831f5dcfSAlexander Motin /* Activate led. */ 2186831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2187831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2188831f5dcfSAlexander Motin return (err); 2189831f5dcfSAlexander Motin } 2190831f5dcfSAlexander Motin 2191d6b3aaf8SOleksandr Tymoshenko int 2192b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2193831f5dcfSAlexander Motin { 2194831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2195831f5dcfSAlexander Motin 2196831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2197831f5dcfSAlexander Motin /* Deactivate led. */ 2198831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2199831f5dcfSAlexander Motin slot->bus_busy--; 2200d493985aSAlexander Motin wakeup(slot); 220135547df5SScott Long SDHCI_UNLOCK(slot); 2202831f5dcfSAlexander Motin return (0); 2203831f5dcfSAlexander Motin } 2204831f5dcfSAlexander Motin 2205831f5dcfSAlexander Motin static void 2206831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2207831f5dcfSAlexander Motin { 2208831f5dcfSAlexander Motin 2209831f5dcfSAlexander Motin if (!slot->curcmd) { 2210831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2211831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2212831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2213831f5dcfSAlexander Motin return; 2214831f5dcfSAlexander Motin } 2215831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2216831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2217831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2218831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2219831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2220831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2221831f5dcfSAlexander Motin 2222831f5dcfSAlexander Motin sdhci_finish_command(slot); 2223831f5dcfSAlexander Motin } 2224831f5dcfSAlexander Motin 2225831f5dcfSAlexander Motin static void 2226831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2227831f5dcfSAlexander Motin { 22281bacf3beSMarius Strobl struct mmc_data *data; 222915c440e1SWarner Losh size_t left; 2230ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2231831f5dcfSAlexander Motin 2232831f5dcfSAlexander Motin if (!slot->curcmd) { 2233831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2234831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2235831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2236831f5dcfSAlexander Motin return; 2237831f5dcfSAlexander Motin } 2238831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2239831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2240831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2241831f5dcfSAlexander Motin "there is no active data operation.\n", 2242831f5dcfSAlexander Motin intmask); 2243831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2244831f5dcfSAlexander Motin return; 2245831f5dcfSAlexander Motin } 2246831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2247831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2248acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2249831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2250831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2251831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2252831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2253831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2254831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2255831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2256831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2257831f5dcfSAlexander Motin } 2258831f5dcfSAlexander Motin if (slot->curcmd->error) { 2259831f5dcfSAlexander Motin /* No need to continue after any error. */ 2260a98788edSIan Lepore goto done; 2261831f5dcfSAlexander Motin } 2262831f5dcfSAlexander Motin 2263aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2264aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2265aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2266aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2267aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2268aca38eabSMarius Strobl sdhci_finish_command(slot); 2269aca38eabSMarius Strobl sdhci_finish_data(slot); 2270aca38eabSMarius Strobl return; 2271aca38eabSMarius Strobl } 2272831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2273c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2274c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2275c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 22761bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 22771bacf3beSMarius Strobl &intmask); 2278c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2279c3a0f75aSOleksandr Tymoshenko } else 2280831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2281c3a0f75aSOleksandr Tymoshenko } 2282831f5dcfSAlexander Motin /* Handle DMA border. */ 2283831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 22841bacf3beSMarius Strobl data = slot->curcmd->data; 2285ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2286831f5dcfSAlexander Motin 2287831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2288831f5dcfSAlexander Motin left = data->len - slot->offset; 2289831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2290831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2291831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2292831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2293ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2294831f5dcfSAlexander Motin } else { 2295831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2296831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2297831f5dcfSAlexander Motin } 2298831f5dcfSAlexander Motin /* ... and reload it again. */ 2299ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2300831f5dcfSAlexander Motin left = data->len - slot->offset; 2301831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2302831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2303831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2304831f5dcfSAlexander Motin } else { 2305831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2306ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2307831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2308831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2309831f5dcfSAlexander Motin } 2310ab00a509SMarius Strobl /* 2311ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2312ab00a509SMarius Strobl * bounce buffer. 2313ab00a509SMarius Strobl */ 2314ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2315831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2316831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2317831f5dcfSAlexander Motin } 2318831f5dcfSAlexander Motin /* Restart DMA. */ 2319831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2320831f5dcfSAlexander Motin } 2321831f5dcfSAlexander Motin /* We have got all data. */ 2322c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2323c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2324c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2325c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2326c3a0f75aSOleksandr Tymoshenko } else 2327831f5dcfSAlexander Motin sdhci_finish_data(slot); 2328831f5dcfSAlexander Motin } 2329a98788edSIan Lepore done: 2330a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2331a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2332a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2333a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2334a98788edSIan Lepore } else 2335a98788edSIan Lepore sdhci_finish_data(slot); 2336a98788edSIan Lepore } 2337c3a0f75aSOleksandr Tymoshenko } 2338831f5dcfSAlexander Motin 2339831f5dcfSAlexander Motin static void 23406dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2341831f5dcfSAlexander Motin { 2342831f5dcfSAlexander Motin 2343831f5dcfSAlexander Motin if (!slot->curcmd) { 2344831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 23456dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2346831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2347831f5dcfSAlexander Motin return; 2348831f5dcfSAlexander Motin } 23496dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2350b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2351831f5dcfSAlexander Motin } 2352831f5dcfSAlexander Motin 2353d6b3aaf8SOleksandr Tymoshenko void 2354d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2355831f5dcfSAlexander Motin { 23562b96b955SJustin Hibbits uint32_t intmask, present; 23576dea80e6SMarius Strobl uint16_t val16; 2358831f5dcfSAlexander Motin 2359831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2360831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2361831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2362831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2363831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2364d6b3aaf8SOleksandr Tymoshenko return; 2365831f5dcfSAlexander Motin } 2366aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 23675b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 23685b69a497SAlexander Motin 2369aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2370aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 23716dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2372aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2373aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2374aca38eabSMarius Strobl if (slot->curcmd) { 2375aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2376aca38eabSMarius Strobl sdhci_finish_command(slot); 2377aca38eabSMarius Strobl } 2378aca38eabSMarius Strobl } 2379aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2380aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2381aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2382831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2383831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2384639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 23852b96b955SJustin Hibbits slot->intmask &= 23862b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 23872b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 23882b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 23892b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 23902b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2391831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2392831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2393b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2394831f5dcfSAlexander Motin } 2395831f5dcfSAlexander Motin /* Handle command interrupts. */ 2396831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2397831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2398831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2399831f5dcfSAlexander Motin } 2400831f5dcfSAlexander Motin /* Handle data interrupts. */ 2401831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2402831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 24037e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 24047e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2405831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2406831f5dcfSAlexander Motin } 2407831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2408831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 24096dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 24106dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2411831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 24126dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2413831f5dcfSAlexander Motin } 2414831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2415831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2416831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2417aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2418831f5dcfSAlexander Motin } 2419aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2420aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2421aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2422831f5dcfSAlexander Motin /* The rest is unknown. */ 2423831f5dcfSAlexander Motin if (intmask) { 2424831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2425831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2426831f5dcfSAlexander Motin intmask); 2427831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2428831f5dcfSAlexander Motin } 2429831f5dcfSAlexander Motin 2430831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2431831f5dcfSAlexander Motin } 2432831f5dcfSAlexander Motin 2433d6b3aaf8SOleksandr Tymoshenko int 24341bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 24351bacf3beSMarius Strobl uintptr_t *result) 2436831f5dcfSAlexander Motin { 2437ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2438831f5dcfSAlexander Motin 2439831f5dcfSAlexander Motin switch (which) { 2440831f5dcfSAlexander Motin default: 2441831f5dcfSAlexander Motin return (EINVAL); 2442831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2443bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2444831f5dcfSAlexander Motin break; 2445831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2446bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2447831f5dcfSAlexander Motin break; 2448831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2449bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2450831f5dcfSAlexander Motin break; 2451831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2452bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2453831f5dcfSAlexander Motin break; 2454831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2455bcd91d25SJayachandran C. *result = slot->host.f_min; 2456831f5dcfSAlexander Motin break; 2457831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2458bcd91d25SJayachandran C. *result = slot->host.f_max; 2459831f5dcfSAlexander Motin break; 2460831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2461bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2462831f5dcfSAlexander Motin break; 2463831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2464bcd91d25SJayachandran C. *result = slot->host.mode; 2465831f5dcfSAlexander Motin break; 2466831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2467bcd91d25SJayachandran C. *result = slot->host.ocr; 2468831f5dcfSAlexander Motin break; 2469831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2470bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2471831f5dcfSAlexander Motin break; 2472831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2473bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2474831f5dcfSAlexander Motin break; 2475aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2476aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2477aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2478aca38eabSMarius Strobl *result = retune_req_reset; 2479aca38eabSMarius Strobl break; 2480aca38eabSMarius Strobl } 2481aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2482aca38eabSMarius Strobl *result = retune_req_normal; 2483aca38eabSMarius Strobl break; 2484aca38eabSMarius Strobl } 2485aca38eabSMarius Strobl } 2486aca38eabSMarius Strobl *result = retune_req_none; 2487aca38eabSMarius Strobl break; 24880f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24890f34084fSMarius Strobl *result = slot->host.ios.vccq; 24900f34084fSMarius Strobl break; 2491831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2492bcd91d25SJayachandran C. *result = slot->host.caps; 2493831f5dcfSAlexander Motin break; 2494831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2495bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2496831f5dcfSAlexander Motin break; 24973a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2498aca38eabSMarius Strobl /* 2499aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2500aca38eabSMarius Strobl * per read/write command to 4 MiB. 2501aca38eabSMarius Strobl */ 2502aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2503aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2504aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2505aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2506aca38eabSMarius Strobl break; 2507aca38eabSMarius Strobl } 2508bcd91d25SJayachandran C. *result = 65535; 25093a4a2557SAlexander Motin break; 251072dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 251172dec079SMarius Strobl /* 251272dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 251372dec079SMarius Strobl */ 251472dec079SMarius Strobl *result = 1000000; 251572dec079SMarius Strobl break; 2516831f5dcfSAlexander Motin } 2517831f5dcfSAlexander Motin return (0); 2518831f5dcfSAlexander Motin } 2519831f5dcfSAlexander Motin 2520d6b3aaf8SOleksandr Tymoshenko int 25211bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 25221bacf3beSMarius Strobl uintptr_t value) 2523831f5dcfSAlexander Motin { 2524831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2525b440e965SMarius Strobl uint32_t clock, max_clock; 2526b440e965SMarius Strobl int i; 2527831f5dcfSAlexander Motin 252815c440e1SWarner Losh if (sdhci_debug > 1) 252915c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2530831f5dcfSAlexander Motin switch (which) { 2531831f5dcfSAlexander Motin default: 2532831f5dcfSAlexander Motin return (EINVAL); 2533831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2534831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2535831f5dcfSAlexander Motin break; 2536831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2537831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2538831f5dcfSAlexander Motin break; 2539831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2540831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2541831f5dcfSAlexander Motin break; 2542831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2543831f5dcfSAlexander Motin if (value > 0) { 254457677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 254557677a3aSOleksandr Tymoshenko clock = max_clock; 254657677a3aSOleksandr Tymoshenko 254757677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 254857677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 254957677a3aSOleksandr Tymoshenko i <<= 1) { 2550831f5dcfSAlexander Motin if (clock <= value) 2551831f5dcfSAlexander Motin break; 2552831f5dcfSAlexander Motin clock >>= 1; 2553831f5dcfSAlexander Motin } 2554b440e965SMarius Strobl } else { 255557677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 255657677a3aSOleksandr Tymoshenko i += 2) { 255757677a3aSOleksandr Tymoshenko if (clock <= value) 255857677a3aSOleksandr Tymoshenko break; 255957677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 256057677a3aSOleksandr Tymoshenko } 256157677a3aSOleksandr Tymoshenko } 256257677a3aSOleksandr Tymoshenko 2563831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2564831f5dcfSAlexander Motin } else 2565831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2566831f5dcfSAlexander Motin break; 2567831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2568831f5dcfSAlexander Motin slot->host.mode = value; 2569831f5dcfSAlexander Motin break; 2570831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2571831f5dcfSAlexander Motin slot->host.ocr = value; 2572831f5dcfSAlexander Motin break; 2573831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2574831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2575831f5dcfSAlexander Motin break; 2576831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2577831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2578831f5dcfSAlexander Motin break; 25790f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 25800f34084fSMarius Strobl slot->host.ios.vccq = value; 25810f34084fSMarius Strobl break; 2582831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2583831f5dcfSAlexander Motin slot->host.ios.timing = value; 2584831f5dcfSAlexander Motin break; 2585831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2586831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2587831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2588831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 25893a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2590aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2591831f5dcfSAlexander Motin return (EINVAL); 2592831f5dcfSAlexander Motin } 2593831f5dcfSAlexander Motin return (0); 2594831f5dcfSAlexander Motin } 2595831f5dcfSAlexander Motin 259615c440e1SWarner Losh #ifdef MMCCAM 2597a94a63f0SWarner Losh void 2598d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2599a94a63f0SWarner Losh { 2600ab00a509SMarius Strobl 2601505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2602a94a63f0SWarner Losh goto fail; 2603a94a63f0SWarner Losh 2604a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2605aeb04e88SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2606da2f833fSBjoern A. Zeeb "sdhci_slot", slot, device_get_unit(slot->bus), 2607a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2608a94a63f0SWarner Losh 2609a94a63f0SWarner Losh if (slot->sim == NULL) { 2610a94a63f0SWarner Losh cam_simq_free(slot->devq); 2611a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2612a94a63f0SWarner Losh goto fail; 2613a94a63f0SWarner Losh } 2614a94a63f0SWarner Losh 2615a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2616a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2617505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2618a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2619a94a63f0SWarner Losh cam_simq_free(slot->devq); 2620a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2621a94a63f0SWarner Losh goto fail; 2622a94a63f0SWarner Losh } 2623a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2624505f6a0cSBjoern A. Zeeb 2625a94a63f0SWarner Losh /* End CAM-specific init */ 2626a94a63f0SWarner Losh slot->card_present = 0; 2627a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2628a94a63f0SWarner Losh return; 2629a94a63f0SWarner Losh 2630a94a63f0SWarner Losh fail: 2631a94a63f0SWarner Losh if (slot->sim != NULL) { 2632a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2633a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2634a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2635a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2636a94a63f0SWarner Losh } 2637a94a63f0SWarner Losh 2638a94a63f0SWarner Losh if (slot->devq != NULL) 2639a94a63f0SWarner Losh cam_simq_free(slot->devq); 2640a94a63f0SWarner Losh } 2641a94a63f0SWarner Losh 2642a94a63f0SWarner Losh void 2643a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2644a94a63f0SWarner Losh { 2645a94a63f0SWarner Losh struct sdhci_slot *slot; 2646a94a63f0SWarner Losh 2647a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2648a94a63f0SWarner Losh if (slot == NULL) { 2649a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2650a94a63f0SWarner Losh xpt_done(ccb); 2651a94a63f0SWarner Losh return; 2652a94a63f0SWarner Losh } 2653a94a63f0SWarner Losh 2654a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2655a94a63f0SWarner Losh 2656a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2657a94a63f0SWarner Losh case XPT_PATH_INQ: 2658cd853791SKonstantin Belousov mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys); 2659a94a63f0SWarner Losh break; 26608c7cd14aSWarner Losh 2661af2253f6SEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS: 2662a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2663a94a63f0SWarner Losh { 2664a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 26655d20e651SIlya Bakulin uint32_t max_data; 2666a94a63f0SWarner Losh 2667a94a63f0SWarner Losh if (sdhci_debug > 1) 2668a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2669a94a63f0SWarner Losh 2670a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2671a94a63f0SWarner Losh cts->protocol_version = 1; 2672a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2673a94a63f0SWarner Losh cts->transport_version = 1; 2674a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2675a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2676a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2677a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2678a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 26795d20e651SIlya Bakulin /* 26805d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 26815d20e651SIlya Bakulin * per read/write command to 4 MiB. 26825d20e651SIlya Bakulin */ 26835d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 26845d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 26855d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 26865d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 26875d20e651SIlya Bakulin } else { 26885d20e651SIlya Bakulin max_data = 65535; 26895d20e651SIlya Bakulin } 26905d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 26915d20e651SIlya Bakulin 2692a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2693a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2694a94a63f0SWarner Losh break; 2695a94a63f0SWarner Losh } 2696af2253f6SEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS: 2697a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2698a94a63f0SWarner Losh if (sdhci_debug > 1) 2699a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2700a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2701a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2702a94a63f0SWarner Losh break; 2703a94a63f0SWarner Losh case XPT_RESET_BUS: 2704a94a63f0SWarner Losh if (sdhci_debug > 1) 2705a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2706a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2707a94a63f0SWarner Losh break; 2708a94a63f0SWarner Losh case XPT_MMC_IO: 2709a94a63f0SWarner Losh /* 2710a94a63f0SWarner Losh * Here is the HW-dependent part of 2711a94a63f0SWarner Losh * sending the command to the underlying h/w 2712a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2713a94a63f0SWarner Losh * Then the request will be marked as completed. 2714a94a63f0SWarner Losh */ 2715aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2716a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2717a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2718a94a63f0SWarner Losh 2719160799c6SWarner Losh sdhci_cam_request(cam_sim_softc(sim), ccb); 2720a94a63f0SWarner Losh return; 2721a94a63f0SWarner Losh default: 2722a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2723a94a63f0SWarner Losh break; 2724a94a63f0SWarner Losh } 2725a94a63f0SWarner Losh xpt_done(ccb); 2726a94a63f0SWarner Losh return; 2727a94a63f0SWarner Losh } 2728a94a63f0SWarner Losh 2729a94a63f0SWarner Losh void 2730a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2731a94a63f0SWarner Losh { 273294ff1d9cSAndriy Gapon sdhci_generic_intr(cam_sim_softc(sim)); 2733a94a63f0SWarner Losh } 2734a94a63f0SWarner Losh 27356dea80e6SMarius Strobl static int 2736ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2737ab00a509SMarius Strobl int proposed_clock) 27386dea80e6SMarius Strobl { 2739a94a63f0SWarner Losh int max_clock, clock, i; 2740a94a63f0SWarner Losh 2741a94a63f0SWarner Losh if (proposed_clock == 0) 2742a94a63f0SWarner Losh return 0; 2743a94a63f0SWarner Losh max_clock = slot->max_clk; 2744a94a63f0SWarner Losh clock = max_clock; 2745a94a63f0SWarner Losh 2746a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2747505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2748a94a63f0SWarner Losh if (clock <= proposed_clock) 2749a94a63f0SWarner Losh break; 2750a94a63f0SWarner Losh clock >>= 1; 2751a94a63f0SWarner Losh } 2752a94a63f0SWarner Losh } else { 2753505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2754a94a63f0SWarner Losh if (clock <= proposed_clock) 2755a94a63f0SWarner Losh break; 2756a94a63f0SWarner Losh clock = max_clock / (i + 2); 2757a94a63f0SWarner Losh } 2758a94a63f0SWarner Losh } 2759a94a63f0SWarner Losh return clock; 2760a94a63f0SWarner Losh } 2761a94a63f0SWarner Losh 2762ab00a509SMarius Strobl static int 2763a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2764a94a63f0SWarner Losh { 2765a94a63f0SWarner Losh struct mmc_ios *ios; 2766ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2767ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2768a94a63f0SWarner Losh 2769a94a63f0SWarner Losh ios = &slot->host.ios; 2770a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2771a94a63f0SWarner Losh new_ios = &cts->ios; 2772a94a63f0SWarner Losh 2773a94a63f0SWarner Losh /* Update only requested fields */ 2774a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2775a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2776b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2777a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2778a94a63f0SWarner Losh } 2779a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2780a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2781b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2782a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2783a94a63f0SWarner Losh } 2784a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2785a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2786b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2787a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2788a94a63f0SWarner Losh } 2789a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2790a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2791b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2792a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2793a94a63f0SWarner Losh } 2794a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2795a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2796b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2797a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2798a94a63f0SWarner Losh } 2799a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2800a94a63f0SWarner Losh ios->timing = new_ios->timing; 2801b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2802a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2803a94a63f0SWarner Losh } 2804a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2805a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2806b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2807a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2808a94a63f0SWarner Losh } 28091a96c143SEmmanuel Vadot if (cts->ios_valid & MMC_VCCQ) { 28101a96c143SEmmanuel Vadot ios->vccq = new_ios->vccq; 28111a96c143SEmmanuel Vadot if (sdhci_debug > 1) 28121a96c143SEmmanuel Vadot slot_printf(slot, "VCCQ => %d\n", ios->vccq); 28131a96c143SEmmanuel Vadot } 2814a94a63f0SWarner Losh 2815a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2816a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2817a94a63f0SWarner Losh } 2818a94a63f0SWarner Losh 2819ab00a509SMarius Strobl static int 2820a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2821a94a63f0SWarner Losh { 2822a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2823a94a63f0SWarner Losh 2824b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2825a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2826a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2827a94a63f0SWarner Losh SDHCI_LOCK(slot); 2828a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2829a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2830a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2831a94a63f0SWarner Losh sdhci_init(slot); 2832a94a63f0SWarner Losh } 2833a94a63f0SWarner Losh /* Configure the bus. */ 2834a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2835a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2836a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2837a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2838a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2839a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2840a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2841a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2842a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2843a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2844a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2845a94a63f0SWarner Losh } else { 2846a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2847a94a63f0SWarner Losh } 2848a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2849a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2850a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2851a94a63f0SWarner Losh else 2852a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2853a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2854a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2855a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2856b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 2857b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2858a94a63f0SWarner Losh 2859a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2860a94a63f0SWarner Losh return (0); 2861a94a63f0SWarner Losh } 2862a94a63f0SWarner Losh 2863ab00a509SMarius Strobl static int 2864a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2865a94a63f0SWarner Losh { 2866ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2867a94a63f0SWarner Losh 2868a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2869a94a63f0SWarner Losh 2870a94a63f0SWarner Losh SDHCI_LOCK(slot); 2871a94a63f0SWarner Losh /* if (slot->req != NULL) { 2872a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2873a94a63f0SWarner Losh return (EBUSY); 2874a94a63f0SWarner Losh } 2875a94a63f0SWarner Losh */ 2876aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 28775d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 28785d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2879a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2880a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 28815d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 28825d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 28835d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2884a94a63f0SWarner Losh } 2885a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2886a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2887a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2888a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2889a94a63f0SWarner Losh } 2890a94a63f0SWarner Losh slot->ccb = ccb; 2891a94a63f0SWarner Losh slot->flags = 0; 2892a94a63f0SWarner Losh sdhci_start(slot); 2893a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2894a94a63f0SWarner Losh return (0); 2895a94a63f0SWarner Losh } 289615c440e1SWarner Losh #endif /* MMCCAM */ 2897a94a63f0SWarner Losh 2898ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2899