1831f5dcfSAlexander Motin /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 */ 1357d8700bcSMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps); 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; 422831f5dcfSAlexander Motin slot->clock = clock; 423831f5dcfSAlexander Motin 424831f5dcfSAlexander Motin /* Turn off the clock. */ 4254ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 4264ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 427b440e965SMarius Strobl /* If no clock requested - leave it so. */ 428831f5dcfSAlexander Motin if (clock == 0) 429831f5dcfSAlexander Motin return; 430ceb9e9f7SIan Lepore 43193efdc63SAdrian Chadd /* Determine the clock base frequency */ 43293efdc63SAdrian Chadd clk_base = slot->max_clk; 43393efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 4341bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 4351bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 43693efdc63SAdrian Chadd 4371bacf3beSMarius Strobl /* 4381bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 4391bacf3beSMarius Strobl */ 44093efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 44193efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 4421bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 4431bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44493efdc63SAdrian Chadd } else { 4451bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 4461bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 44793efdc63SAdrian Chadd } 44893efdc63SAdrian Chadd 44993efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 45093efdc63SAdrian Chadd } 45193efdc63SAdrian Chadd 452ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 453ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 454ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 455ceb9e9f7SIan Lepore 4568f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 457831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 45893efdc63SAdrian Chadd res = clk_base; 45957677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 460831f5dcfSAlexander Motin if (res <= clock) 461831f5dcfSAlexander Motin break; 462831f5dcfSAlexander Motin res >>= 1; 463831f5dcfSAlexander Motin } 464831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 4658f3b7d56SOleksandr Tymoshenko div >>= 1; 466c11bbc7dSMarius Strobl } else { 4678f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 46893efdc63SAdrian Chadd if (clock >= clk_base) 46957677a3aSOleksandr Tymoshenko div = 0; 4708f3b7d56SOleksandr Tymoshenko else { 47157677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 47293efdc63SAdrian Chadd if ((clk_base / div) <= clock) 4738f3b7d56SOleksandr Tymoshenko break; 4748f3b7d56SOleksandr Tymoshenko } 4758f3b7d56SOleksandr Tymoshenko } 4768f3b7d56SOleksandr Tymoshenko div >>= 1; 4778f3b7d56SOleksandr Tymoshenko } 4788f3b7d56SOleksandr Tymoshenko 4798f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 48093efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 48193efdc63SAdrian Chadd div, clock, clk_base); 4828f3b7d56SOleksandr Tymoshenko 483831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4848f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4858f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4868f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4878f3b7d56SOleksandr Tymoshenko 488831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 489831f5dcfSAlexander Motin /* Enable clock. */ 490831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 491831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 492831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 493831f5dcfSAlexander Motin timeout = 10; 494831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 495831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 496831f5dcfSAlexander Motin if (timeout == 0) { 497831f5dcfSAlexander Motin slot_printf(slot, 498831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 499831f5dcfSAlexander Motin sdhci_dumpregs(slot); 500831f5dcfSAlexander Motin return; 501831f5dcfSAlexander Motin } 502831f5dcfSAlexander Motin timeout--; 503831f5dcfSAlexander Motin DELAY(1000); 504831f5dcfSAlexander Motin } 505831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 506831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 507831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 508831f5dcfSAlexander Motin } 509831f5dcfSAlexander Motin 510831f5dcfSAlexander Motin static void 511831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 512831f5dcfSAlexander Motin { 51385083a80SMarius Strobl int i; 514831f5dcfSAlexander Motin uint8_t pwr; 515831f5dcfSAlexander Motin 516831f5dcfSAlexander Motin if (slot->power == power) 517831f5dcfSAlexander Motin return; 518d6b3aaf8SOleksandr Tymoshenko 519831f5dcfSAlexander Motin slot->power = power; 520831f5dcfSAlexander Motin 521831f5dcfSAlexander Motin /* Turn off the power. */ 522831f5dcfSAlexander Motin pwr = 0; 523831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 524b440e965SMarius Strobl /* If power down requested - leave it so. */ 525831f5dcfSAlexander Motin if (power == 0) 526831f5dcfSAlexander Motin return; 527831f5dcfSAlexander Motin /* Set voltage. */ 528831f5dcfSAlexander Motin switch (1 << power) { 529831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 530831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 531831f5dcfSAlexander Motin break; 532831f5dcfSAlexander Motin case MMC_OCR_290_300: 533831f5dcfSAlexander Motin case MMC_OCR_300_310: 534831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 535831f5dcfSAlexander Motin break; 536831f5dcfSAlexander Motin case MMC_OCR_320_330: 537831f5dcfSAlexander Motin case MMC_OCR_330_340: 538831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 539831f5dcfSAlexander Motin break; 540831f5dcfSAlexander Motin } 541831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 54285083a80SMarius Strobl /* 54385083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 54485083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 5458022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 54685083a80SMarius Strobl */ 547831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 54885083a80SMarius Strobl for (i = 0; i < 20; i++) { 549831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 55085083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 55185083a80SMarius Strobl break; 55285083a80SMarius Strobl DELAY(100); 55385083a80SMarius Strobl } 55485083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 5554d52f81dSIan Lepore slot_printf(slot, "Bus power failed to enable\n"); 556a2832f9fSMarius Strobl 557a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 558a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 559a2832f9fSMarius Strobl DELAY(10); 560a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 561a2832f9fSMarius Strobl DELAY(300); 562a2832f9fSMarius Strobl } 563831f5dcfSAlexander Motin } 564831f5dcfSAlexander Motin 565831f5dcfSAlexander Motin static void 566831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 567831f5dcfSAlexander Motin { 568831f5dcfSAlexander Motin uint32_t data; 569831f5dcfSAlexander Motin char *buffer; 570831f5dcfSAlexander Motin size_t left; 571831f5dcfSAlexander Motin 572831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 573831f5dcfSAlexander Motin buffer += slot->offset; 574831f5dcfSAlexander Motin /* Transfer one block at a time. */ 5755d5ae066SIlya Bakulin #ifdef MMCCAM 5765d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) 5775d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 5785d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 5795d5ae066SIlya Bakulin else 5805d5ae066SIlya Bakulin #endif 581831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 582831f5dcfSAlexander Motin slot->offset += left; 583831f5dcfSAlexander Motin 584831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 585d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 586831f5dcfSAlexander Motin DELAY(10); 587ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 588831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 589831f5dcfSAlexander Motin while (left > 3) { 590831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 591831f5dcfSAlexander Motin buffer[0] = data; 592831f5dcfSAlexander Motin buffer[1] = (data >> 8); 593831f5dcfSAlexander Motin buffer[2] = (data >> 16); 594831f5dcfSAlexander Motin buffer[3] = (data >> 24); 595831f5dcfSAlexander Motin buffer += 4; 596831f5dcfSAlexander Motin left -= 4; 597831f5dcfSAlexander Motin } 598831f5dcfSAlexander Motin } else { 599d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 600831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 601831f5dcfSAlexander Motin left &= 3; 602831f5dcfSAlexander Motin } 603831f5dcfSAlexander Motin /* Handle uneven size case. */ 604831f5dcfSAlexander Motin if (left > 0) { 605831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 606831f5dcfSAlexander Motin while (left > 0) { 607831f5dcfSAlexander Motin *(buffer++) = data; 608831f5dcfSAlexander Motin data >>= 8; 609831f5dcfSAlexander Motin left--; 610831f5dcfSAlexander Motin } 611831f5dcfSAlexander Motin } 612831f5dcfSAlexander Motin } 613831f5dcfSAlexander Motin 614831f5dcfSAlexander Motin static void 615831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 616831f5dcfSAlexander Motin { 617831f5dcfSAlexander Motin uint32_t data = 0; 618831f5dcfSAlexander Motin char *buffer; 619831f5dcfSAlexander Motin size_t left; 620831f5dcfSAlexander Motin 621831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 622831f5dcfSAlexander Motin buffer += slot->offset; 623831f5dcfSAlexander Motin /* Transfer one block at a time. */ 6245d5ae066SIlya Bakulin #ifdef MMCCAM 6255d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) { 6265d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 6275d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 6285d5ae066SIlya Bakulin } else 6295d5ae066SIlya Bakulin #endif 630831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 631831f5dcfSAlexander Motin slot->offset += left; 632831f5dcfSAlexander Motin 633ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 634831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 635831f5dcfSAlexander Motin while (left > 3) { 636831f5dcfSAlexander Motin data = buffer[0] + 637831f5dcfSAlexander Motin (buffer[1] << 8) + 638831f5dcfSAlexander Motin (buffer[2] << 16) + 639831f5dcfSAlexander Motin (buffer[3] << 24); 640831f5dcfSAlexander Motin left -= 4; 641831f5dcfSAlexander Motin buffer += 4; 642831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 643831f5dcfSAlexander Motin } 644831f5dcfSAlexander Motin } else { 645d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 646831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 647831f5dcfSAlexander Motin left &= 3; 648831f5dcfSAlexander Motin } 649831f5dcfSAlexander Motin /* Handle uneven size case. */ 650831f5dcfSAlexander Motin if (left > 0) { 651831f5dcfSAlexander Motin while (left > 0) { 652831f5dcfSAlexander Motin data <<= 8; 653831f5dcfSAlexander Motin data += *(buffer++); 654831f5dcfSAlexander Motin left--; 655831f5dcfSAlexander Motin } 656831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 657831f5dcfSAlexander Motin } 658831f5dcfSAlexander Motin } 659831f5dcfSAlexander Motin 660831f5dcfSAlexander Motin static void 661831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 662831f5dcfSAlexander Motin { 663831f5dcfSAlexander Motin 664831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 665831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 666831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 667831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 668831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 669831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 670831f5dcfSAlexander Motin break; 671831f5dcfSAlexander Motin } 672831f5dcfSAlexander Motin } else { 673831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 674831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 675831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 676831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 677831f5dcfSAlexander Motin break; 678831f5dcfSAlexander Motin } 679831f5dcfSAlexander Motin } 680831f5dcfSAlexander Motin } 681831f5dcfSAlexander Motin 682831f5dcfSAlexander Motin static void 6837e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 684831f5dcfSAlexander Motin { 685831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 6867e6ccea3SMarius Strobl device_t d; 687831f5dcfSAlexander Motin 688831f5dcfSAlexander Motin SDHCI_LOCK(slot); 6896e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 690a94a63f0SWarner Losh #ifdef MMCCAM 691a94a63f0SWarner Losh if (slot->card_present == 0) { 692a94a63f0SWarner Losh #else 693831f5dcfSAlexander Motin if (slot->dev == NULL) { 694a94a63f0SWarner Losh #endif 695831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 696639f59f0SIan Lepore if (bootverbose || sdhci_debug) 697639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 698a94a63f0SWarner Losh #ifdef MMCCAM 699a94a63f0SWarner Losh slot->card_present = 1; 700c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 701a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 702a94a63f0SWarner Losh #else 703aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 704831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 705aca38eabSMarius Strobl if (d) { 706aca38eabSMarius Strobl device_set_ivars(d, slot); 707aca38eabSMarius Strobl (void)device_probe_and_attach(d); 708aca38eabSMarius Strobl } 709a94a63f0SWarner Losh #endif 710831f5dcfSAlexander Motin } else 711831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 712831f5dcfSAlexander Motin } else { 713a94a63f0SWarner Losh #ifdef MMCCAM 714a94a63f0SWarner Losh if (slot->card_present == 1) { 715a94a63f0SWarner Losh #else 716831f5dcfSAlexander Motin if (slot->dev != NULL) { 717a94a63f0SWarner Losh #endif 718831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 719639f59f0SIan Lepore if (bootverbose || sdhci_debug) 720639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 7217e6ccea3SMarius Strobl d = slot->dev; 722831f5dcfSAlexander Motin slot->dev = NULL; 723a94a63f0SWarner Losh #ifdef MMCCAM 724a94a63f0SWarner Losh slot->card_present = 0; 725c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 726a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 727a94a63f0SWarner Losh #else 728aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 729cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 730aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 731aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 732831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 733aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 734d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 735a94a63f0SWarner Losh #endif 736831f5dcfSAlexander Motin } else 737831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 738831f5dcfSAlexander Motin } 739831f5dcfSAlexander Motin } 740831f5dcfSAlexander Motin 741b8bf08b1SIan Lepore static void 742b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 743639f59f0SIan Lepore { 744639f59f0SIan Lepore bool was_present; 745639f59f0SIan Lepore 746639f59f0SIan Lepore /* 747639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 748639f59f0SIan Lepore * create the child device after a short delay. The delay is to 749639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 750639f59f0SIan Lepore * before the other pins have made good contact). 751639f59f0SIan Lepore * 752639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 753639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 754639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 755639f59f0SIan Lepore * that happens by deleting and recreating the child device. 756639f59f0SIan Lepore */ 757a94a63f0SWarner Losh #ifdef MMCCAM 758a94a63f0SWarner Losh was_present = slot->card_present; 759a94a63f0SWarner Losh #else 760639f59f0SIan Lepore was_present = slot->dev != NULL; 761a94a63f0SWarner Losh #endif 762639f59f0SIan Lepore if (!was_present && is_present) { 763639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 764639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 765639f59f0SIan Lepore } else if (was_present && !is_present) { 766639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 767639f59f0SIan Lepore } 768b8bf08b1SIan Lepore } 769b8bf08b1SIan Lepore 770b8bf08b1SIan Lepore void 771b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 772b8bf08b1SIan Lepore { 773b8bf08b1SIan Lepore 774b8bf08b1SIan Lepore SDHCI_LOCK(slot); 775b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 776639f59f0SIan Lepore SDHCI_UNLOCK(slot); 777639f59f0SIan Lepore } 778639f59f0SIan Lepore 779639f59f0SIan Lepore static void 780639f59f0SIan Lepore sdhci_card_poll(void *arg) 781639f59f0SIan Lepore { 782639f59f0SIan Lepore struct sdhci_slot *slot = arg; 783639f59f0SIan Lepore 784639f59f0SIan Lepore sdhci_handle_card_present(slot, 785639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 786639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 787639f59f0SIan Lepore sdhci_card_poll, slot); 788639f59f0SIan Lepore } 789639f59f0SIan Lepore 790ab00a509SMarius Strobl static int 7917d8700bcSMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps) 792ab00a509SMarius Strobl { 793ab00a509SMarius Strobl int err; 794ab00a509SMarius Strobl 795ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 796cd853791SKonstantin Belousov if (maxphys <= 1024 * 4) 797ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 798cd853791SKonstantin Belousov else if (maxphys <= 1024 * 8) 799ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 800cd853791SKonstantin Belousov else if (maxphys <= 1024 * 16) 801ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 802cd853791SKonstantin Belousov else if (maxphys <= 1024 * 32) 803ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 804cd853791SKonstantin Belousov else if (maxphys <= 1024 * 64) 805ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 806cd853791SKonstantin Belousov else if (maxphys <= 1024 * 128) 807ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 808cd853791SKonstantin Belousov else if (maxphys <= 1024 * 256) 809ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 810ab00a509SMarius Strobl else 811ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 812ab00a509SMarius Strobl } 813ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 814ab00a509SMarius Strobl 815ab00a509SMarius Strobl /* 816ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 817ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 818ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 819ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 820ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 821ab00a509SMarius Strobl * be aligned to the SDMA boundary. 822ab00a509SMarius Strobl */ 823ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 8247d8700bcSMarcin Wojtas 0, (caps & SDHCI_CAN_DO_64BIT) ? BUS_SPACE_MAXADDR : 8257d8700bcSMarcin Wojtas BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 826ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 827ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 828ab00a509SMarius Strobl if (err != 0) { 829ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 830ab00a509SMarius Strobl return (err); 831ab00a509SMarius Strobl } 832ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 833ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 834ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 835ab00a509SMarius Strobl if (err != 0) { 836ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 837ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 838ab00a509SMarius Strobl return (err); 839ab00a509SMarius Strobl } 840ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 841ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 842ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 843ab00a509SMarius Strobl &slot->paddr, 0); 844ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 845ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 846ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 847ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 848ab00a509SMarius Strobl if (err) 849ab00a509SMarius Strobl return (err); 850ab00a509SMarius Strobl else 851ab00a509SMarius Strobl return (EFAULT); 852ab00a509SMarius Strobl } 853ab00a509SMarius Strobl 854ab00a509SMarius Strobl return (0); 855ab00a509SMarius Strobl } 856ab00a509SMarius Strobl 857ab00a509SMarius Strobl static void 858ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 859ab00a509SMarius Strobl { 860ab00a509SMarius Strobl 861ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 862ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 863ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 864ab00a509SMarius Strobl } 865ab00a509SMarius Strobl 866d6b3aaf8SOleksandr Tymoshenko int 867d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 868831f5dcfSAlexander Motin { 869aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 870aca38eabSMarius Strobl kobj_method_t *kobj_method; 8710f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 872d6b3aaf8SOleksandr Tymoshenko int err; 873d00c1f7fSBartlomiej Grzesik char node_name[8]; 874d00c1f7fSBartlomiej Grzesik struct sysctl_oid *node_oid; 875831f5dcfSAlexander Motin 876831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 877a94a63f0SWarner Losh 878d6b3aaf8SOleksandr Tymoshenko slot->num = num; 879d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 880d6b3aaf8SOleksandr Tymoshenko 881d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 882d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8830f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8848f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8850f34084fSMarius Strobl caps2 = slot->caps2; 8860f34084fSMarius Strobl } else { 887831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8880f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8890f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8900f34084fSMarius Strobl else 8910f34084fSMarius Strobl caps2 = 0; 8920f34084fSMarius Strobl } 8937fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8947fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8957fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 896ab00a509SMarius Strobl slot_printf(slot, 8977fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 8987fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 8997fcf4780SMarius Strobl return (ENXIO); 9007fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 9017fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 9027fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 9037fcf4780SMarius Strobl } 9047fcf4780SMarius Strobl } 905831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 90633aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 90787a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 90887a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 90933aad34dSOleksandr Tymoshenko else 91087a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 91187a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 91287a6a871SIan Lepore if (freq != 0) 91387a6a871SIan Lepore slot->max_clk = freq * 1000000; 91487a6a871SIan Lepore /* 91587a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 91687a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 91787a6a871SIan Lepore * with an assumption, so complain about it. 91887a6a871SIan Lepore */ 919831f5dcfSAlexander Motin if (slot->max_clk == 0) { 92087a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 921ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 9221bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 9231bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 924831f5dcfSAlexander Motin } 925a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 9268f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 9278f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 928a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 929a2832f9fSMarius Strobl slot->timeout_clk = 1000; 9308f3b7d56SOleksandr Tymoshenko } else { 9311bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 9321bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 9338f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 9348f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 9358f3b7d56SOleksandr Tymoshenko } 93687a6a871SIan Lepore /* 93787a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 93887a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 93987a6a871SIan Lepore * max timeout, but still mention it. 94087a6a871SIan Lepore */ 941831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 942ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 943ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 944ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 945831f5dcfSAlexander Motin } 946831f5dcfSAlexander Motin 94757677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 948831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 949831f5dcfSAlexander Motin slot->host.host_ocr = 0; 950831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 951831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 952831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 953831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 95449dfdf63SIan Lepore /* 95549dfdf63SIan Lepore * 1.8V VDD is not supposed to be used for removable cards. Hardware 95649dfdf63SIan Lepore * prior to v3.0 had no way to indicate embedded slots, but did 95749dfdf63SIan Lepore * sometimes support 1.8v for non-removable devices. 95849dfdf63SIan Lepore */ 95949dfdf63SIan Lepore if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 || 96049dfdf63SIan Lepore (slot->opt & SDHCI_SLOT_EMBEDDED))) 961831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 962831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 963ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 964831f5dcfSAlexander Motin "support voltages.\n"); 965831f5dcfSAlexander Motin } 966aca38eabSMarius Strobl 9675652be30SMarcin Wojtas host_caps = slot->host.caps; 9685652be30SMarcin Wojtas host_caps |= MMC_CAP_4_BIT_DATA; 9692d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 9700f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 971831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 9720f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 97372dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9740f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 97572dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9760f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 977aca38eabSMarius Strobl 978aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9790f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9800f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9810f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9820f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9830f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9840f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9850f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9860f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9870f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9880f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9890f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9900f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9910f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9920f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9930f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9940f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 995835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 996835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 997835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 998aca38eabSMarius Strobl 999aca38eabSMarius Strobl /* 1000aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 1001aca38eabSMarius Strobl * default NULL implementation. 1002aca38eabSMarius Strobl */ 1003aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 1004aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1005aca38eabSMarius Strobl kobj_desc); 1006aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1007aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1008aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 1009aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 1010aca38eabSMarius Strobl 1011aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 1012aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 1013aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 1014aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 1015aca38eabSMarius Strobl 1016aca38eabSMarius Strobl /* 1017aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 1018aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 1019aca38eabSMarius Strobl */ 1020aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 1021aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1022aca38eabSMarius Strobl kobj_desc); 1023aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1024aca38eabSMarius Strobl goto no_tuning; 1025aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 1026aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1027aca38eabSMarius Strobl kobj_desc); 1028aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 1029aca38eabSMarius Strobl no_tuning: 1030aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 1031aca38eabSMarius Strobl } 1032aca38eabSMarius Strobl 1033aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 1034aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 1035aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 1036aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 1037aca38eabSMarius Strobl M_WAITOK); 1038aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 1039aca38eabSMarius Strobl M_WAITOK); 1040aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 1041aca38eabSMarius Strobl M_WAITOK); 1042aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 1043aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 1044aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 1045aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 1046aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 1047aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 1048aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 1049aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 1050ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 1051aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 1052aca38eabSMarius Strobl slot->retune_count = 1; 1053aca38eabSMarius Strobl } else if (slot->retune_count != 0) 1054aca38eabSMarius Strobl slot->retune_count = 1055aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 1056aca38eabSMarius Strobl } 1057aca38eabSMarius Strobl } 1058aca38eabSMarius Strobl 1059aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 1060aca38eabSMarius Strobl 1061aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 10620f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 10630f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1064aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 10650f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 10660f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 1067aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 1068aca38eabSMarius Strobl 1069aca38eabSMarius Strobl /* 1070aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 1071aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 1072aca38eabSMarius Strobl * generic SDHCI implementation. 1073aca38eabSMarius Strobl */ 1074aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1075aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1076aca38eabSMarius Strobl kobj_desc); 1077aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1078aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1079aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1080aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1081aca38eabSMarius Strobl 1082aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1083f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10840f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1085f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10860f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1087f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10880f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10890f34084fSMarius Strobl slot->host.caps = host_caps; 10900f34084fSMarius Strobl 1091831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1092831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1093831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1094d6b3aaf8SOleksandr Tymoshenko 1095d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1096831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1097d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1098831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1099a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1100a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1101831f5dcfSAlexander Motin 1102c3a0f75aSOleksandr Tymoshenko /* 1103c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1104c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1105c3a0f75aSOleksandr Tymoshenko */ 1106c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1107c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1108c3a0f75aSOleksandr Tymoshenko 1109ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 11107d8700bcSMarcin Wojtas err = sdhci_dma_alloc(slot, caps); 1111ab00a509SMarius Strobl if (err != 0) { 1112ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1113ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1114ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1115ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1116ab00a509SMarius Strobl } 1117ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1118ab00a509SMarius Strobl return (err); 1119ab00a509SMarius Strobl } 1120ab00a509SMarius Strobl } 1121ab00a509SMarius Strobl 11225b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 1123d00c1f7fSBartlomiej Grzesik sdhci_dumpcaps(slot); 1124831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1125831f5dcfSAlexander Motin } 1126831f5dcfSAlexander Motin 1127ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1128ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1129ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 11304d52f81dSIan Lepore "timeout", CTLFLAG_RWTUN, &slot->timeout, 0, 1131ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1132831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1133639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1134639f59f0SIan Lepore sdhci_card_task, slot); 1135639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1136e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1137aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1138ba6fc1c7SLuiz Otavio O Souza 1139639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1140639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1141639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1142639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1143639f59f0SIan Lepore } 1144639f59f0SIan Lepore 1145aca38eabSMarius Strobl sdhci_init(slot); 1146aca38eabSMarius Strobl 1147d00c1f7fSBartlomiej Grzesik snprintf(node_name, sizeof(node_name), "slot%d", slot->num); 1148d00c1f7fSBartlomiej Grzesik 1149d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1150d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1151d00c1f7fSBartlomiej Grzesik OID_AUTO, node_name, CTLFLAG_RW, 0, "slot specific node"); 1152d00c1f7fSBartlomiej Grzesik 1153d00c1f7fSBartlomiej Grzesik node_oid = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 1154d00c1f7fSBartlomiej Grzesik SYSCTL_CHILDREN(node_oid), OID_AUTO, "debug", CTLFLAG_RW, 0, 1155d00c1f7fSBartlomiej Grzesik "Debugging node"); 1156d00c1f7fSBartlomiej Grzesik 1157d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1158d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpregs", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1159d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpregs, 1160d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI registers"); 1161d00c1f7fSBartlomiej Grzesik 1162d00c1f7fSBartlomiej Grzesik SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(node_oid), 1163d00c1f7fSBartlomiej Grzesik OID_AUTO, "dumpcaps", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 1164d00c1f7fSBartlomiej Grzesik slot, 0, &sdhci_syctl_dumpcaps, 1165d00c1f7fSBartlomiej Grzesik "A", "Dump SDHCI capabilites"); 1166d00c1f7fSBartlomiej Grzesik 1167831f5dcfSAlexander Motin return (0); 1168831f5dcfSAlexander Motin } 1169831f5dcfSAlexander Motin 1170d91f1a10SIlya Bakulin #ifndef MMCCAM 1171d6b3aaf8SOleksandr Tymoshenko void 1172d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1173831f5dcfSAlexander Motin { 11747e6ccea3SMarius Strobl 1175d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1176d6b3aaf8SOleksandr Tymoshenko } 1177d91f1a10SIlya Bakulin #endif 1178831f5dcfSAlexander Motin 1179d6b3aaf8SOleksandr Tymoshenko int 1180d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1181d6b3aaf8SOleksandr Tymoshenko { 1182831f5dcfSAlexander Motin device_t d; 1183831f5dcfSAlexander Motin 1184e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1185639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1186aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1187831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1188639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1189831f5dcfSAlexander Motin 1190831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1191831f5dcfSAlexander Motin d = slot->dev; 1192831f5dcfSAlexander Motin slot->dev = NULL; 1193831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1194831f5dcfSAlexander Motin if (d != NULL) 1195d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1196831f5dcfSAlexander Motin 1197831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1198b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1199831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1200ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1201ab00a509SMarius Strobl sdhci_dma_free(slot); 1202aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1203aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1204aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1205aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1206aca38eabSMarius Strobl } 1207d6b3aaf8SOleksandr Tymoshenko 1208831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1209d6b3aaf8SOleksandr Tymoshenko 1210831f5dcfSAlexander Motin return (0); 1211831f5dcfSAlexander Motin } 1212831f5dcfSAlexander Motin 1213d6b3aaf8SOleksandr Tymoshenko int 1214d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 121592bf0e27SAlexander Motin { 12167e6ccea3SMarius Strobl 1217aca38eabSMarius Strobl /* 1218aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1219aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1220aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1221aca38eabSMarius Strobl */ 1222aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1223aca38eabSMarius Strobl SDHCI_LOCK(slot); 1224aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1225b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_ALL); 1226aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 122792bf0e27SAlexander Motin 122892bf0e27SAlexander Motin return (0); 122992bf0e27SAlexander Motin } 123092bf0e27SAlexander Motin 1231d6b3aaf8SOleksandr Tymoshenko int 1232d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 123392bf0e27SAlexander Motin { 12347e6ccea3SMarius Strobl 1235aca38eabSMarius Strobl SDHCI_LOCK(slot); 1236d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1237aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 123892bf0e27SAlexander Motin 1239d6b3aaf8SOleksandr Tymoshenko return (0); 124092bf0e27SAlexander Motin } 124192bf0e27SAlexander Motin 1242b8f94506SArtur Rojek void 1243b8f94506SArtur Rojek sdhci_generic_reset(device_t brdev __unused, struct sdhci_slot *slot, 1244b8f94506SArtur Rojek uint8_t mask) 1245b8f94506SArtur Rojek { 1246b8f94506SArtur Rojek int timeout; 1247b8f94506SArtur Rojek uint32_t clock; 1248b8f94506SArtur Rojek 1249b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 1250b8f94506SArtur Rojek if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 1251b8f94506SArtur Rojek return; 1252b8f94506SArtur Rojek } 1253b8f94506SArtur Rojek 1254b8f94506SArtur Rojek /* Some controllers need this kick or reset won't work. */ 1255b8f94506SArtur Rojek if ((mask & SDHCI_RESET_ALL) == 0 && 1256b8f94506SArtur Rojek (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 1257b8f94506SArtur Rojek /* This is to force an update */ 1258b8f94506SArtur Rojek clock = slot->clock; 1259b8f94506SArtur Rojek slot->clock = 0; 1260b8f94506SArtur Rojek sdhci_set_clock(slot, clock); 1261b8f94506SArtur Rojek } 1262b8f94506SArtur Rojek 1263b8f94506SArtur Rojek if (mask & SDHCI_RESET_ALL) { 1264b8f94506SArtur Rojek slot->clock = 0; 1265b8f94506SArtur Rojek slot->power = 0; 1266b8f94506SArtur Rojek } 1267b8f94506SArtur Rojek 1268b8f94506SArtur Rojek WR1(slot, SDHCI_SOFTWARE_RESET, mask); 1269b8f94506SArtur Rojek 1270b8f94506SArtur Rojek if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 1271b8f94506SArtur Rojek /* 1272b8f94506SArtur Rojek * Resets on TI OMAPs and AM335x are incompatible with SDHCI 1273b8f94506SArtur Rojek * specification. The reset bit has internal propagation delay, 1274b8f94506SArtur Rojek * so a fast read after write returns 0 even if reset process is 1275b8f94506SArtur Rojek * in progress. The workaround is to poll for 1 before polling 1276b8f94506SArtur Rojek * for 0. In the worst case, if we miss seeing it asserted the 1277b8f94506SArtur Rojek * time we spent waiting is enough to ensure the reset finishes. 1278b8f94506SArtur Rojek */ 1279b8f94506SArtur Rojek timeout = 10000; 1280b8f94506SArtur Rojek while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 1281b8f94506SArtur Rojek if (timeout <= 0) 1282b8f94506SArtur Rojek break; 1283b8f94506SArtur Rojek timeout--; 1284b8f94506SArtur Rojek DELAY(1); 1285b8f94506SArtur Rojek } 1286b8f94506SArtur Rojek } 1287b8f94506SArtur Rojek 1288b8f94506SArtur Rojek /* Wait max 100 ms */ 1289b8f94506SArtur Rojek timeout = 10000; 1290b8f94506SArtur Rojek /* Controller clears the bits when it's done */ 1291b8f94506SArtur Rojek while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 1292b8f94506SArtur Rojek if (timeout <= 0) { 1293b8f94506SArtur Rojek slot_printf(slot, "Reset 0x%x never completed.\n", 1294b8f94506SArtur Rojek mask); 1295b8f94506SArtur Rojek sdhci_dumpregs(slot); 1296b8f94506SArtur Rojek return; 1297b8f94506SArtur Rojek } 1298b8f94506SArtur Rojek timeout--; 1299b8f94506SArtur Rojek DELAY(10); 1300b8f94506SArtur Rojek } 1301b8f94506SArtur Rojek } 1302b8f94506SArtur Rojek 130357677a3aSOleksandr Tymoshenko uint32_t 1304b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 130557677a3aSOleksandr Tymoshenko { 13067e6ccea3SMarius Strobl 130757677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 130857677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 130957677a3aSOleksandr Tymoshenko else 131057677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 131157677a3aSOleksandr Tymoshenko } 131257677a3aSOleksandr Tymoshenko 13136e37fb2bSIan Lepore bool 1314b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 13156e37fb2bSIan Lepore { 13166e37fb2bSIan Lepore 1317639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1318639f59f0SIan Lepore return true; 1319639f59f0SIan Lepore 13206e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 13216e37fb2bSIan Lepore } 13226e37fb2bSIan Lepore 13230f34084fSMarius Strobl void 13240f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 13250f34084fSMarius Strobl { 1326ab00a509SMarius Strobl const struct mmc_ios *ios; 13270f34084fSMarius Strobl uint16_t hostctrl2; 13280f34084fSMarius Strobl 13290f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 13300f34084fSMarius Strobl return; 13310f34084fSMarius Strobl 1332aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 13330f34084fSMarius Strobl ios = &slot->host.ios; 13340f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13350f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13360f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1337aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 13380f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 13390f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 13400f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1341aca38eabSMarius Strobl else 13420f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1343aca38eabSMarius Strobl } 13440f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 13450f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 13460f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 13470f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 13480f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 13490f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 13500f34084fSMarius Strobl else 13510f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 13520f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 13530f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 13540f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13550f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 13560f34084fSMarius Strobl } 13570f34084fSMarius Strobl 1358d6b3aaf8SOleksandr Tymoshenko int 1359d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1360831f5dcfSAlexander Motin { 1361831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1362831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1363831f5dcfSAlexander Motin 1364831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1365831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1366831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1367831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1368831f5dcfSAlexander Motin sdhci_init(slot); 1369831f5dcfSAlexander Motin } 1370831f5dcfSAlexander Motin /* Configure the bus. */ 1371831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1372831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 13732d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 13742d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1375831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13762d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 13772d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13782d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 13792d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 13802d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13812d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13822d1731b8SIan Lepore } else { 13832d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 13842d1731b8SIan Lepore } 13850f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1386bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1387831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1388831f5dcfSAlexander Motin else 1389831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1390831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 13910f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1392831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1393d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1394b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1395b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1396831f5dcfSAlexander Motin 1397831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1398831f5dcfSAlexander Motin return (0); 1399831f5dcfSAlexander Motin } 1400831f5dcfSAlexander Motin 14010f34084fSMarius Strobl int 14020f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 14030f34084fSMarius Strobl { 14040f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 14050f34084fSMarius Strobl enum mmc_vccq vccq; 14060f34084fSMarius Strobl int err; 14070f34084fSMarius Strobl uint16_t hostctrl2; 14080f34084fSMarius Strobl 14090f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 14100f34084fSMarius Strobl return (0); 14110f34084fSMarius Strobl 14120f34084fSMarius Strobl err = 0; 14130f34084fSMarius Strobl vccq = slot->host.ios.vccq; 14140f34084fSMarius Strobl SDHCI_LOCK(slot); 14150f34084fSMarius Strobl sdhci_set_clock(slot, 0); 14160f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14170f34084fSMarius Strobl switch (vccq) { 14180f34084fSMarius Strobl case vccq_330: 14190f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14200f34084fSMarius Strobl goto done; 14210f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 14220f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14230f34084fSMarius Strobl DELAY(5000); 14240f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14250f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 14260f34084fSMarius Strobl goto done; 14270f34084fSMarius Strobl err = EAGAIN; 14280f34084fSMarius Strobl break; 14290f34084fSMarius Strobl case vccq_180: 14300f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 14310f34084fSMarius Strobl err = EINVAL; 14320f34084fSMarius Strobl goto done; 14330f34084fSMarius Strobl } 14340f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14350f34084fSMarius Strobl goto done; 14360f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 14370f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 14380f34084fSMarius Strobl DELAY(5000); 14390f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 14400f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 14410f34084fSMarius Strobl goto done; 14420f34084fSMarius Strobl err = EAGAIN; 14430f34084fSMarius Strobl break; 14440f34084fSMarius Strobl default: 14450f34084fSMarius Strobl slot_printf(slot, 14460f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 14470f34084fSMarius Strobl err = EINVAL; 14480f34084fSMarius Strobl break; 14490f34084fSMarius Strobl } 14500f34084fSMarius Strobl done: 14510f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 14520f34084fSMarius Strobl SDHCI_UNLOCK(slot); 14530f34084fSMarius Strobl return (err); 14540f34084fSMarius Strobl } 14550f34084fSMarius Strobl 1456aca38eabSMarius Strobl int 1457aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1458aca38eabSMarius Strobl { 1459aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1460ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1461aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1462aca38eabSMarius Strobl struct mmc_data *tune_data; 1463aca38eabSMarius Strobl uint32_t opcode; 1464aca38eabSMarius Strobl int err; 1465aca38eabSMarius Strobl 1466aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1467aca38eabSMarius Strobl return (0); 1468aca38eabSMarius Strobl 1469aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1470aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1471aca38eabSMarius Strobl SDHCI_LOCK(slot); 1472aca38eabSMarius Strobl switch (ios->timing) { 1473aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1474aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1475aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1476aca38eabSMarius Strobl return (EINVAL); 1477aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1478aca38eabSMarius Strobl /* 1479aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1480aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1481aca38eabSMarius Strobl * expected to be required. 1482aca38eabSMarius Strobl */ 1483aca38eabSMarius Strobl if (hs400) 1484aca38eabSMarius Strobl slot->retune_ticks = 0; 1485aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1486aca38eabSMarius Strobl break; 1487aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1488aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1489aca38eabSMarius Strobl break; 1490aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1491aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1492aca38eabSMarius Strobl break; 1493aca38eabSMarius Strobl /* FALLTHROUGH */ 1494aca38eabSMarius Strobl default: 1495aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1496aca38eabSMarius Strobl return (0); 1497aca38eabSMarius Strobl } 1498aca38eabSMarius Strobl 1499aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1500aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1501aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1502aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1503aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1504aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1505aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1506aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1507aca38eabSMarius Strobl MMC_TUNING_LEN; 1508aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1509aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1510aca38eabSMarius Strobl 1511aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1512aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1513aca38eabSMarius Strobl if (err == 0) { 1514aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1515aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1516cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1517aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1518aca38eabSMarius Strobl if (slot->retune_ticks) { 1519aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1520aca38eabSMarius Strobl sdhci_retune, slot); 1521aca38eabSMarius Strobl } 1522aca38eabSMarius Strobl } 1523aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1524aca38eabSMarius Strobl return (err); 1525aca38eabSMarius Strobl } 1526aca38eabSMarius Strobl 1527aca38eabSMarius Strobl int 1528aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1529aca38eabSMarius Strobl { 1530aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1531aca38eabSMarius Strobl int err; 1532aca38eabSMarius Strobl 1533aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1534aca38eabSMarius Strobl return (0); 1535aca38eabSMarius Strobl 1536aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1537aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1538aca38eabSMarius Strobl return (EINVAL); 1539aca38eabSMarius Strobl 1540aca38eabSMarius Strobl SDHCI_LOCK(slot); 1541aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1542aca38eabSMarius Strobl /* 1543aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1544aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1545aca38eabSMarius Strobl * with the host properly acquired, and 1546aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1547aca38eabSMarius Strobl * 1548aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1549aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1550aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1551aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1552aca38eabSMarius Strobl */ 1553aca38eabSMarius Strobl if (slot->retune_ticks) { 1554aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1555aca38eabSMarius Strobl sdhci_retune, slot); 1556aca38eabSMarius Strobl } 1557aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1558aca38eabSMarius Strobl return (err); 1559aca38eabSMarius Strobl } 1560aca38eabSMarius Strobl 1561aca38eabSMarius Strobl static int 1562aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1563aca38eabSMarius Strobl { 1564aca38eabSMarius Strobl struct mmc_request *tune_req; 1565aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1566aca38eabSMarius Strobl int i; 1567aca38eabSMarius Strobl uint32_t intmask; 1568aca38eabSMarius Strobl uint16_t hostctrl2; 1569aca38eabSMarius Strobl u_char opt; 1570aca38eabSMarius Strobl 1571aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1572aca38eabSMarius Strobl if (slot->req != NULL) 1573aca38eabSMarius Strobl return (EBUSY); 1574aca38eabSMarius Strobl 1575aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1576aca38eabSMarius Strobl opt = slot->opt; 1577aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1578aca38eabSMarius Strobl 1579aca38eabSMarius Strobl /* 1580aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1581aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1582aca38eabSMarius Strobl */ 1583aca38eabSMarius Strobl intmask = slot->intmask; 1584aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1585cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1586aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1587aca38eabSMarius Strobl 1588aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1589aca38eabSMarius Strobl if (reset) 1590aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1591aca38eabSMarius Strobl else 1592aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1593aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1594aca38eabSMarius Strobl 1595aca38eabSMarius Strobl tune_req = slot->tune_req; 1596aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1597aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1598aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1599aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1600aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1601aca38eabSMarius Strobl tune_req->done_data = slot; 1602aca38eabSMarius Strobl slot->req = tune_req; 1603aca38eabSMarius Strobl slot->flags = 0; 1604aca38eabSMarius Strobl sdhci_start(slot); 1605aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1606aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1607aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1608aca38eabSMarius Strobl break; 1609aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1610aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1611aca38eabSMarius Strobl break; 1612aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1613aca38eabSMarius Strobl DELAY(1000); 1614aca38eabSMarius Strobl } 1615aca38eabSMarius Strobl 161678f8baa8SMarius Strobl /* 161778f8baa8SMarius Strobl * Restore DMA usage and interrupts. 161878f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 161978f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 162078f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 162178f8baa8SMarius Strobl * doesn't lose these. 162278f8baa8SMarius Strobl */ 1623aca38eabSMarius Strobl slot->opt = opt; 1624aca38eabSMarius Strobl slot->intmask = intmask; 162578f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 162678f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1627aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1628aca38eabSMarius Strobl 1629aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1630aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1631aca38eabSMarius Strobl slot->retune_req = 0; 1632aca38eabSMarius Strobl return (0); 1633aca38eabSMarius Strobl } 1634aca38eabSMarius Strobl 1635aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1636aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1637aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1638b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1639aca38eabSMarius Strobl return (EIO); 1640aca38eabSMarius Strobl } 1641aca38eabSMarius Strobl 1642aca38eabSMarius Strobl static void 1643aca38eabSMarius Strobl sdhci_retune(void *arg) 1644aca38eabSMarius Strobl { 1645aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1646aca38eabSMarius Strobl 1647aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1648aca38eabSMarius Strobl } 1649aca38eabSMarius Strobl 1650a94a63f0SWarner Losh #ifdef MMCCAM 1651a94a63f0SWarner Losh static void 1652a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1653a94a63f0SWarner Losh { 1654a94a63f0SWarner Losh union ccb *ccb; 165515c440e1SWarner Losh 1656aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 165715c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1658a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1659a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1660a94a63f0SWarner Losh ccb = slot->ccb; 1661a94a63f0SWarner Losh slot->ccb = NULL; 1662a94a63f0SWarner Losh slot->curcmd = NULL; 1663a94a63f0SWarner Losh 1664a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1665a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1666a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1667a94a63f0SWarner Losh 1668a94a63f0SWarner Losh ccb->ccb_h.status = 1669a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1670a94a63f0SWarner Losh xpt_done(ccb); 1671a94a63f0SWarner Losh } 1672a94a63f0SWarner Losh } 1673a94a63f0SWarner Losh #else 1674831f5dcfSAlexander Motin static void 1675e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1676e64f01a9SIan Lepore { 1677e64f01a9SIan Lepore struct mmc_request *req; 1678e64f01a9SIan Lepore 1679e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1680e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1681e64f01a9SIan Lepore req = slot->req; 1682e64f01a9SIan Lepore slot->req = NULL; 1683e64f01a9SIan Lepore slot->curcmd = NULL; 1684e64f01a9SIan Lepore req->done(req); 1685e64f01a9SIan Lepore } 1686e64f01a9SIan Lepore } 1687a94a63f0SWarner Losh #endif 1688e64f01a9SIan Lepore 1689e64f01a9SIan Lepore static void 1690aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1691aca38eabSMarius Strobl { 1692aca38eabSMarius Strobl 1693aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1694aca38eabSMarius Strobl wakeup(req); 1695aca38eabSMarius Strobl } 1696aca38eabSMarius Strobl 1697aca38eabSMarius Strobl static void 1698e64f01a9SIan Lepore sdhci_timeout(void *arg) 1699e64f01a9SIan Lepore { 1700e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1701e64f01a9SIan Lepore 1702e64f01a9SIan Lepore if (slot->curcmd != NULL) { 17037e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 17047e586643SIan Lepore sdhci_dumpregs(slot); 1705b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 1706b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1707e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1708e64f01a9SIan Lepore sdhci_req_done(slot); 17097e586643SIan Lepore } else { 17107e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1711e64f01a9SIan Lepore } 1712e64f01a9SIan Lepore } 1713e64f01a9SIan Lepore 1714e64f01a9SIan Lepore static void 1715ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1716831f5dcfSAlexander Motin { 1717831f5dcfSAlexander Motin uint16_t mode; 1718831f5dcfSAlexander Motin 1719831f5dcfSAlexander Motin if (data == NULL) 1720831f5dcfSAlexander Motin return; 1721831f5dcfSAlexander Motin 1722831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 17235d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1724831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 17255d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 17266dea80e6SMarius Strobl #ifdef MMCCAM 17276dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 17286dea80e6SMarius Strobl #else 17290519c933SMarius Strobl slot->req->stop != NULL && 17306dea80e6SMarius Strobl #endif 17316dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 17326dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 17336dea80e6SMarius Strobl } 1734831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1735831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1736831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1737831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1738831f5dcfSAlexander Motin 1739831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1740831f5dcfSAlexander Motin } 1741831f5dcfSAlexander Motin 1742831f5dcfSAlexander Motin static void 1743831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1744831f5dcfSAlexander Motin { 1745831f5dcfSAlexander Motin int flags, timeout; 174690993663SIan Lepore uint32_t mask; 1747831f5dcfSAlexander Motin 1748831f5dcfSAlexander Motin slot->curcmd = cmd; 1749831f5dcfSAlexander Motin slot->cmd_done = 0; 1750831f5dcfSAlexander Motin 1751831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1752831f5dcfSAlexander Motin 1753831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1754831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1755831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1756831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1757e64f01a9SIan Lepore sdhci_req_done(slot); 1758831f5dcfSAlexander Motin return; 1759831f5dcfSAlexander Motin } 1760831f5dcfSAlexander Motin 1761b440e965SMarius Strobl /* 1762b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1763b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1764b440e965SMarius Strobl */ 17656e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1766d8208d9eSAlexander Motin slot->power == 0 || 1767d8208d9eSAlexander Motin slot->clock == 0) { 1768a94a63f0SWarner Losh slot_printf(slot, 1769440c645bSMitchell Horne "Cannot issue a command (power=%d clock=%d)\n", 1770a94a63f0SWarner Losh slot->power, slot->clock); 1771831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1772e64f01a9SIan Lepore sdhci_req_done(slot); 1773831f5dcfSAlexander Motin return; 1774831f5dcfSAlexander Motin } 1775831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1776831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1777831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1778a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1779831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1780aca38eabSMarius Strobl /* 1781aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1782aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1783aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1784aca38eabSMarius Strobl */ 1785a94a63f0SWarner Losh #ifdef MMCCAM 1786aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1787a94a63f0SWarner Losh #else 1788aca38eabSMarius Strobl if (cmd == slot->req->stop || 1789a94a63f0SWarner Losh #endif 1790aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1791aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1792aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 17938775ab45SIan Lepore /* 17948775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 17958775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 17968775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 17978775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 17988775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 17998775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 18008775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 18018775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 18028775ab45SIan Lepore */ 18038775ab45SIan Lepore timeout = 250; 180490993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1805831f5dcfSAlexander Motin if (timeout == 0) { 1806831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1807831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1808831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1809831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1810e64f01a9SIan Lepore sdhci_req_done(slot); 1811831f5dcfSAlexander Motin return; 1812831f5dcfSAlexander Motin } 1813831f5dcfSAlexander Motin timeout--; 1814831f5dcfSAlexander Motin DELAY(1000); 1815831f5dcfSAlexander Motin } 1816831f5dcfSAlexander Motin 1817831f5dcfSAlexander Motin /* Prepare command flags. */ 1818831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1819831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1820831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1821831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1822831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1823831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1824831f5dcfSAlexander Motin else 1825831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1826831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1827831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1828831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1829831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1830a94a63f0SWarner Losh if (cmd->data != NULL) 1831831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1832831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1833831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1834831f5dcfSAlexander Motin /* Prepare data. */ 1835831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1836831f5dcfSAlexander Motin /* 1837831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1838831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1839831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1840831f5dcfSAlexander Motin */ 1841831f5dcfSAlexander Motin if (slot->data_done == 0) { 1842831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1843831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1844831f5dcfSAlexander Motin } 1845831f5dcfSAlexander Motin /* Set command argument. */ 1846831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1847831f5dcfSAlexander Motin /* Set data transfer mode. */ 1848831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1849aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18508adf4202SBjoern A. Zeeb slot_printf(slot, "Starting command opcode %#04x flags %#04x\n", 18518adf4202SBjoern A. Zeeb cmd->opcode, flags); 18528adf4202SBjoern A. Zeeb 1853831f5dcfSAlexander Motin /* Start command. */ 1854d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1855a6873fd1SIan Lepore /* Start timeout callout. */ 1856ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1857ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1858831f5dcfSAlexander Motin } 1859831f5dcfSAlexander Motin 1860831f5dcfSAlexander Motin static void 1861831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1862831f5dcfSAlexander Motin { 1863831f5dcfSAlexander Motin int i; 18641bacf3beSMarius Strobl uint32_t val; 18651bacf3beSMarius Strobl uint8_t extra; 1866831f5dcfSAlexander Motin 1867aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18688adf4202SBjoern A. Zeeb slot_printf(slot, "%s: called, err %d flags %#04x\n", 1869a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1870831f5dcfSAlexander Motin slot->cmd_done = 1; 187172dec079SMarius Strobl /* 187272dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1873831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 187472dec079SMarius Strobl * happened first. 187572dec079SMarius Strobl */ 1876aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1877aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1878aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1879aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1880831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1881831f5dcfSAlexander Motin if (slot->curcmd->error) { 1882aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1883aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1884b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 1885b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 1886831f5dcfSAlexander Motin sdhci_start(slot); 1887831f5dcfSAlexander Motin return; 1888831f5dcfSAlexander Motin } 1889831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1890831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1891831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1892831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 18931bacf3beSMarius Strobl extra = 0; 1894831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 18951bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 18961bacf3beSMarius Strobl if (slot->quirks & 18971bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1898677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1899677ee494SIan Lepore else { 1900677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1901677ee494SIan Lepore (val << 8) | extra; 1902831f5dcfSAlexander Motin extra = val >> 24; 1903831f5dcfSAlexander Motin } 1904677ee494SIan Lepore } 1905831f5dcfSAlexander Motin } else 1906831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1907831f5dcfSAlexander Motin } 1908aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 19098adf4202SBjoern A. Zeeb slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n", 1910a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1911a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1912a94a63f0SWarner Losh 1913831f5dcfSAlexander Motin /* If data ready - finish. */ 1914831f5dcfSAlexander Motin if (slot->data_done) 1915831f5dcfSAlexander Motin sdhci_start(slot); 1916831f5dcfSAlexander Motin } 1917831f5dcfSAlexander Motin 1918831f5dcfSAlexander Motin static void 1919ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1920831f5dcfSAlexander Motin { 1921ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1922831f5dcfSAlexander Motin uint8_t div; 1923831f5dcfSAlexander Motin 1924831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1925831f5dcfSAlexander Motin slot->data_done = 1; 1926831f5dcfSAlexander Motin return; 1927831f5dcfSAlexander Motin } 1928831f5dcfSAlexander Motin 1929831f5dcfSAlexander Motin slot->data_done = 0; 1930831f5dcfSAlexander Motin 1931831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1932831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1933ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1934ceb9e9f7SIan Lepore div = 0xE; 1935ceb9e9f7SIan Lepore } else { 1936831f5dcfSAlexander Motin target_timeout = 1000000; 1937831f5dcfSAlexander Motin div = 0; 1938831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1939ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1940ceb9e9f7SIan Lepore ++div; 1941831f5dcfSAlexander Motin current_timeout <<= 1; 1942831f5dcfSAlexander Motin } 1943831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1944ceb9e9f7SIan Lepore if (div < 0xE && 1945ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1946ceb9e9f7SIan Lepore ++div; 1947831f5dcfSAlexander Motin } 1948ceb9e9f7SIan Lepore } 1949831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1950831f5dcfSAlexander Motin 1951831f5dcfSAlexander Motin if (data == NULL) 1952831f5dcfSAlexander Motin return; 1953831f5dcfSAlexander Motin 1954831f5dcfSAlexander Motin /* Use DMA if possible. */ 1955831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1956831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1957ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1958d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1959831f5dcfSAlexander Motin (data->len <= 512)) 1960831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1961831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1962d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1963831f5dcfSAlexander Motin ((data->len) & 0x3)) 1964831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1965831f5dcfSAlexander Motin /* Load DMA buffer. */ 1966831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1967ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1968831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1969ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1970ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1971831f5dcfSAlexander Motin else { 1972ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1973ab00a509SMarius Strobl sdma_bbufsz)); 1974ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1975ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1976831f5dcfSAlexander Motin } 1977831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1978ab00a509SMarius Strobl /* 1979ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1980ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1981ab00a509SMarius Strobl */ 1982ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1983831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1984831f5dcfSAlexander Motin else 1985831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1986831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1987831f5dcfSAlexander Motin } 1988831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1989831f5dcfSAlexander Motin slot->offset = 0; 19905d5ae066SIlya Bakulin #ifdef MMCCAM 19915d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 19925d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 19935d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 19945d5ae066SIlya Bakulin blkcnt = data->block_count; 19955d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 19965d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 19975d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 19985d5ae066SIlya Bakulin } else 19995d5ae066SIlya Bakulin #endif 20005d5ae066SIlya Bakulin { 2001ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 2002ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 2003ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 20045d5ae066SIlya Bakulin } 20055d5ae066SIlya Bakulin 20065d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 2007ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 2008aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2009ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 2010ab00a509SMarius Strobl blksz, blkcnt); 2011831f5dcfSAlexander Motin } 2012831f5dcfSAlexander Motin 2013c3a0f75aSOleksandr Tymoshenko void 2014831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 2015831f5dcfSAlexander Motin { 2016831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 20177e6ccea3SMarius Strobl size_t left; 2018831f5dcfSAlexander Motin 2019831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 2020ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 2021831f5dcfSAlexander Motin * happened first. */ 2022831f5dcfSAlexander Motin if (!slot->cmd_done) { 2023831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 2024831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 2025831f5dcfSAlexander Motin } 2026831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 2027915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 2028915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 2029831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 20307e6ccea3SMarius Strobl left = data->len - slot->offset; 2031ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2032ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 2033831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2034ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 2035831f5dcfSAlexander Motin } else 2036ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 2037ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 2038831f5dcfSAlexander Motin } 2039a98788edSIan Lepore slot->data_done = 1; 2040831f5dcfSAlexander Motin /* If there was error - reset the host. */ 2041831f5dcfSAlexander Motin if (slot->curcmd->error) { 2042aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 2043aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2044b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2045b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2046831f5dcfSAlexander Motin sdhci_start(slot); 2047831f5dcfSAlexander Motin return; 2048831f5dcfSAlexander Motin } 2049831f5dcfSAlexander Motin /* If we already have command response - finish. */ 2050831f5dcfSAlexander Motin if (slot->cmd_done) 2051831f5dcfSAlexander Motin sdhci_start(slot); 2052831f5dcfSAlexander Motin } 2053831f5dcfSAlexander Motin 2054a94a63f0SWarner Losh #ifdef MMCCAM 2055a94a63f0SWarner Losh static void 2056a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 2057a94a63f0SWarner Losh { 2058a94a63f0SWarner Losh union ccb *ccb; 2059ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 2060a94a63f0SWarner Losh 2061a94a63f0SWarner Losh ccb = slot->ccb; 2062a94a63f0SWarner Losh if (ccb == NULL) 2063a94a63f0SWarner Losh return; 2064a94a63f0SWarner Losh 2065a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2066a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 2067a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 2068a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 2069a94a63f0SWarner Losh return; 2070a94a63f0SWarner Losh } 2071a94a63f0SWarner Losh 2072a94a63f0SWarner Losh /* 2073a94a63f0SWarner Losh * Old stack doesn't use this! 2074a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 2075a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 2076a94a63f0SWarner Losh * Not using this code does no harm... 2077a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 2078a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 2079a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 2080a94a63f0SWarner Losh return; 2081a94a63f0SWarner Losh } 2082a94a63f0SWarner Losh */ 2083aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2084a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 2085a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 2086a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 2087b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2088b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2089a94a63f0SWarner Losh } 2090a94a63f0SWarner Losh 2091a94a63f0SWarner Losh sdhci_req_done(slot); 2092a94a63f0SWarner Losh } 2093a94a63f0SWarner Losh #else 2094831f5dcfSAlexander Motin static void 2095831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 2096831f5dcfSAlexander Motin { 2097ab00a509SMarius Strobl const struct mmc_request *req; 2098831f5dcfSAlexander Motin 2099831f5dcfSAlexander Motin req = slot->req; 2100831f5dcfSAlexander Motin if (req == NULL) 2101831f5dcfSAlexander Motin return; 2102831f5dcfSAlexander Motin 2103831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 2104831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 2105831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 2106831f5dcfSAlexander Motin return; 2107831f5dcfSAlexander Motin } 2108915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 2109915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 2110831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 2111831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 2112831f5dcfSAlexander Motin return; 2113831f5dcfSAlexander Motin } 2114aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 21155b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 21165b69a497SAlexander Motin if (!req->cmd->error && 2117915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2118915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2119915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2120b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2121b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_DATA); 2122831f5dcfSAlexander Motin } 2123831f5dcfSAlexander Motin 2124e64f01a9SIan Lepore sdhci_req_done(slot); 2125831f5dcfSAlexander Motin } 2126a94a63f0SWarner Losh #endif 2127831f5dcfSAlexander Motin 2128d6b3aaf8SOleksandr Tymoshenko int 2129b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2130b440e965SMarius Strobl struct mmc_request *req) 2131831f5dcfSAlexander Motin { 2132831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2133831f5dcfSAlexander Motin 2134831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2135831f5dcfSAlexander Motin if (slot->req != NULL) { 2136831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2137831f5dcfSAlexander Motin return (EBUSY); 2138831f5dcfSAlexander Motin } 2139aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 21401bacf3beSMarius Strobl slot_printf(slot, 21411bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2142831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 21435b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 21445b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 21455b69a497SAlexander Motin } 2146831f5dcfSAlexander Motin slot->req = req; 2147831f5dcfSAlexander Motin slot->flags = 0; 2148831f5dcfSAlexander Motin sdhci_start(slot); 2149831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2150bea2dca2SAlexander Motin if (dumping) { 2151bea2dca2SAlexander Motin while (slot->req != NULL) { 2152d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2153bea2dca2SAlexander Motin DELAY(10); 2154bea2dca2SAlexander Motin } 2155bea2dca2SAlexander Motin } 2156831f5dcfSAlexander Motin return (0); 2157831f5dcfSAlexander Motin } 2158831f5dcfSAlexander Motin 2159d6b3aaf8SOleksandr Tymoshenko int 2160b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2161831f5dcfSAlexander Motin { 2162831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2163831f5dcfSAlexander Motin uint32_t val; 2164831f5dcfSAlexander Motin 2165831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2166831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2167831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2168831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2169831f5dcfSAlexander Motin } 2170831f5dcfSAlexander Motin 2171d6b3aaf8SOleksandr Tymoshenko int 2172b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2173831f5dcfSAlexander Motin { 2174831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2175831f5dcfSAlexander Motin int err = 0; 2176831f5dcfSAlexander Motin 2177831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2178831f5dcfSAlexander Motin while (slot->bus_busy) 2179d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2180831f5dcfSAlexander Motin slot->bus_busy++; 2181831f5dcfSAlexander Motin /* Activate led. */ 2182831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2183831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2184831f5dcfSAlexander Motin return (err); 2185831f5dcfSAlexander Motin } 2186831f5dcfSAlexander Motin 2187d6b3aaf8SOleksandr Tymoshenko int 2188b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2189831f5dcfSAlexander Motin { 2190831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2191831f5dcfSAlexander Motin 2192831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2193831f5dcfSAlexander Motin /* Deactivate led. */ 2194831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2195831f5dcfSAlexander Motin slot->bus_busy--; 2196d493985aSAlexander Motin wakeup(slot); 219735547df5SScott Long SDHCI_UNLOCK(slot); 2198831f5dcfSAlexander Motin return (0); 2199831f5dcfSAlexander Motin } 2200831f5dcfSAlexander Motin 2201831f5dcfSAlexander Motin static void 2202831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2203831f5dcfSAlexander Motin { 2204831f5dcfSAlexander Motin 2205831f5dcfSAlexander Motin if (!slot->curcmd) { 2206831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2207831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2208831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2209831f5dcfSAlexander Motin return; 2210831f5dcfSAlexander Motin } 2211831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2212831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2213831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2214831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2215831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2216831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2217831f5dcfSAlexander Motin 2218831f5dcfSAlexander Motin sdhci_finish_command(slot); 2219831f5dcfSAlexander Motin } 2220831f5dcfSAlexander Motin 2221831f5dcfSAlexander Motin static void 2222831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2223831f5dcfSAlexander Motin { 22241bacf3beSMarius Strobl struct mmc_data *data; 222515c440e1SWarner Losh size_t left; 2226ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2227831f5dcfSAlexander Motin 2228831f5dcfSAlexander Motin if (!slot->curcmd) { 2229831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2230831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2231831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2232831f5dcfSAlexander Motin return; 2233831f5dcfSAlexander Motin } 2234831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2235831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2236831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2237831f5dcfSAlexander Motin "there is no active data operation.\n", 2238831f5dcfSAlexander Motin intmask); 2239831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2240831f5dcfSAlexander Motin return; 2241831f5dcfSAlexander Motin } 2242831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2243831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2244acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2245831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2246831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2247831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2248831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2249831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2250831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2251831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2252831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2253831f5dcfSAlexander Motin } 2254831f5dcfSAlexander Motin if (slot->curcmd->error) { 2255831f5dcfSAlexander Motin /* No need to continue after any error. */ 2256a98788edSIan Lepore goto done; 2257831f5dcfSAlexander Motin } 2258831f5dcfSAlexander Motin 2259aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2260aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2261aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2262aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2263aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2264aca38eabSMarius Strobl sdhci_finish_command(slot); 2265aca38eabSMarius Strobl sdhci_finish_data(slot); 2266aca38eabSMarius Strobl return; 2267aca38eabSMarius Strobl } 2268831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2269c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2270c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2271c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 22721bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 22731bacf3beSMarius Strobl &intmask); 2274c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2275c3a0f75aSOleksandr Tymoshenko } else 2276831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2277c3a0f75aSOleksandr Tymoshenko } 2278831f5dcfSAlexander Motin /* Handle DMA border. */ 2279831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 22801bacf3beSMarius Strobl data = slot->curcmd->data; 2281ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2282831f5dcfSAlexander Motin 2283831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2284831f5dcfSAlexander Motin left = data->len - slot->offset; 2285831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2286831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2287831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2288831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2289ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2290831f5dcfSAlexander Motin } else { 2291831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2292831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2293831f5dcfSAlexander Motin } 2294831f5dcfSAlexander Motin /* ... and reload it again. */ 2295ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2296831f5dcfSAlexander Motin left = data->len - slot->offset; 2297831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2298831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2299831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2300831f5dcfSAlexander Motin } else { 2301831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2302ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2303831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2304831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2305831f5dcfSAlexander Motin } 2306ab00a509SMarius Strobl /* 2307ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2308ab00a509SMarius Strobl * bounce buffer. 2309ab00a509SMarius Strobl */ 2310ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2311831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2312831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2313831f5dcfSAlexander Motin } 2314831f5dcfSAlexander Motin /* Restart DMA. */ 2315831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2316831f5dcfSAlexander Motin } 2317831f5dcfSAlexander Motin /* We have got all data. */ 2318c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2319c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2320c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2321c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2322c3a0f75aSOleksandr Tymoshenko } else 2323831f5dcfSAlexander Motin sdhci_finish_data(slot); 2324831f5dcfSAlexander Motin } 2325a98788edSIan Lepore done: 2326a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2327a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2328a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2329a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2330a98788edSIan Lepore } else 2331a98788edSIan Lepore sdhci_finish_data(slot); 2332a98788edSIan Lepore } 2333c3a0f75aSOleksandr Tymoshenko } 2334831f5dcfSAlexander Motin 2335831f5dcfSAlexander Motin static void 23366dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2337831f5dcfSAlexander Motin { 2338831f5dcfSAlexander Motin 2339831f5dcfSAlexander Motin if (!slot->curcmd) { 2340831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 23416dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2342831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2343831f5dcfSAlexander Motin return; 2344831f5dcfSAlexander Motin } 23456dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2346b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, SDHCI_RESET_CMD); 2347831f5dcfSAlexander Motin } 2348831f5dcfSAlexander Motin 2349d6b3aaf8SOleksandr Tymoshenko void 2350d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2351831f5dcfSAlexander Motin { 23522b96b955SJustin Hibbits uint32_t intmask, present; 23536dea80e6SMarius Strobl uint16_t val16; 2354831f5dcfSAlexander Motin 2355831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2356831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2357831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2358831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2359831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2360d6b3aaf8SOleksandr Tymoshenko return; 2361831f5dcfSAlexander Motin } 2362aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 23635b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 23645b69a497SAlexander Motin 2365aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2366aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 23676dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2368aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2369aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2370aca38eabSMarius Strobl if (slot->curcmd) { 2371aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2372aca38eabSMarius Strobl sdhci_finish_command(slot); 2373aca38eabSMarius Strobl } 2374aca38eabSMarius Strobl } 2375aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2376aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2377aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2378831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2379831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2380639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 23812b96b955SJustin Hibbits slot->intmask &= 23822b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 23832b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 23842b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 23852b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 23862b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2387831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2388831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2389b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2390831f5dcfSAlexander Motin } 2391831f5dcfSAlexander Motin /* Handle command interrupts. */ 2392831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2393831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2394831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2395831f5dcfSAlexander Motin } 2396831f5dcfSAlexander Motin /* Handle data interrupts. */ 2397831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2398831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 23997e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 24007e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2401831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2402831f5dcfSAlexander Motin } 2403831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2404831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 24056dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 24066dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2407831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 24086dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2409831f5dcfSAlexander Motin } 2410831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2411831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2412831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2413aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2414831f5dcfSAlexander Motin } 2415aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2416aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2417aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2418831f5dcfSAlexander Motin /* The rest is unknown. */ 2419831f5dcfSAlexander Motin if (intmask) { 2420831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2421831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2422831f5dcfSAlexander Motin intmask); 2423831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2424831f5dcfSAlexander Motin } 2425831f5dcfSAlexander Motin 2426831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2427831f5dcfSAlexander Motin } 2428831f5dcfSAlexander Motin 2429d6b3aaf8SOleksandr Tymoshenko int 24301bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 24311bacf3beSMarius Strobl uintptr_t *result) 2432831f5dcfSAlexander Motin { 2433ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2434831f5dcfSAlexander Motin 2435831f5dcfSAlexander Motin switch (which) { 2436831f5dcfSAlexander Motin default: 2437831f5dcfSAlexander Motin return (EINVAL); 2438831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2439bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2440831f5dcfSAlexander Motin break; 2441831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2442bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2443831f5dcfSAlexander Motin break; 2444831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2445bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2446831f5dcfSAlexander Motin break; 2447831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2448bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2449831f5dcfSAlexander Motin break; 2450831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2451bcd91d25SJayachandran C. *result = slot->host.f_min; 2452831f5dcfSAlexander Motin break; 2453831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2454bcd91d25SJayachandran C. *result = slot->host.f_max; 2455831f5dcfSAlexander Motin break; 2456831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2457bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2458831f5dcfSAlexander Motin break; 2459831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2460bcd91d25SJayachandran C. *result = slot->host.mode; 2461831f5dcfSAlexander Motin break; 2462831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2463bcd91d25SJayachandran C. *result = slot->host.ocr; 2464831f5dcfSAlexander Motin break; 2465831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2466bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2467831f5dcfSAlexander Motin break; 2468831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2469bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2470831f5dcfSAlexander Motin break; 2471aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2472aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2473aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2474aca38eabSMarius Strobl *result = retune_req_reset; 2475aca38eabSMarius Strobl break; 2476aca38eabSMarius Strobl } 2477aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2478aca38eabSMarius Strobl *result = retune_req_normal; 2479aca38eabSMarius Strobl break; 2480aca38eabSMarius Strobl } 2481aca38eabSMarius Strobl } 2482aca38eabSMarius Strobl *result = retune_req_none; 2483aca38eabSMarius Strobl break; 24840f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24850f34084fSMarius Strobl *result = slot->host.ios.vccq; 24860f34084fSMarius Strobl break; 2487831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2488bcd91d25SJayachandran C. *result = slot->host.caps; 2489831f5dcfSAlexander Motin break; 2490831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2491bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2492831f5dcfSAlexander Motin break; 24933a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2494aca38eabSMarius Strobl /* 2495aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2496aca38eabSMarius Strobl * per read/write command to 4 MiB. 2497aca38eabSMarius Strobl */ 2498aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2499aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2500aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2501aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2502aca38eabSMarius Strobl break; 2503aca38eabSMarius Strobl } 2504bcd91d25SJayachandran C. *result = 65535; 25053a4a2557SAlexander Motin break; 250672dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 250772dec079SMarius Strobl /* 250872dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 250972dec079SMarius Strobl */ 251072dec079SMarius Strobl *result = 1000000; 251172dec079SMarius Strobl break; 2512831f5dcfSAlexander Motin } 2513831f5dcfSAlexander Motin return (0); 2514831f5dcfSAlexander Motin } 2515831f5dcfSAlexander Motin 2516d6b3aaf8SOleksandr Tymoshenko int 25171bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 25181bacf3beSMarius Strobl uintptr_t value) 2519831f5dcfSAlexander Motin { 2520831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2521b440e965SMarius Strobl uint32_t clock, max_clock; 2522b440e965SMarius Strobl int i; 2523831f5dcfSAlexander Motin 252415c440e1SWarner Losh if (sdhci_debug > 1) 252515c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2526831f5dcfSAlexander Motin switch (which) { 2527831f5dcfSAlexander Motin default: 2528831f5dcfSAlexander Motin return (EINVAL); 2529831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2530831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2531831f5dcfSAlexander Motin break; 2532831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2533831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2534831f5dcfSAlexander Motin break; 2535831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2536831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2537831f5dcfSAlexander Motin break; 2538831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2539831f5dcfSAlexander Motin if (value > 0) { 254057677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 254157677a3aSOleksandr Tymoshenko clock = max_clock; 254257677a3aSOleksandr Tymoshenko 254357677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 254457677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 254557677a3aSOleksandr Tymoshenko i <<= 1) { 2546831f5dcfSAlexander Motin if (clock <= value) 2547831f5dcfSAlexander Motin break; 2548831f5dcfSAlexander Motin clock >>= 1; 2549831f5dcfSAlexander Motin } 2550b440e965SMarius Strobl } else { 255157677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 255257677a3aSOleksandr Tymoshenko i += 2) { 255357677a3aSOleksandr Tymoshenko if (clock <= value) 255457677a3aSOleksandr Tymoshenko break; 255557677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 255657677a3aSOleksandr Tymoshenko } 255757677a3aSOleksandr Tymoshenko } 255857677a3aSOleksandr Tymoshenko 2559831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2560831f5dcfSAlexander Motin } else 2561831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2562831f5dcfSAlexander Motin break; 2563831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2564831f5dcfSAlexander Motin slot->host.mode = value; 2565831f5dcfSAlexander Motin break; 2566831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2567831f5dcfSAlexander Motin slot->host.ocr = value; 2568831f5dcfSAlexander Motin break; 2569831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2570831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2571831f5dcfSAlexander Motin break; 2572831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2573831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2574831f5dcfSAlexander Motin break; 25750f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 25760f34084fSMarius Strobl slot->host.ios.vccq = value; 25770f34084fSMarius Strobl break; 2578831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2579831f5dcfSAlexander Motin slot->host.ios.timing = value; 2580831f5dcfSAlexander Motin break; 2581831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2582831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2583831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2584831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 25853a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2586aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2587831f5dcfSAlexander Motin return (EINVAL); 2588831f5dcfSAlexander Motin } 2589831f5dcfSAlexander Motin return (0); 2590831f5dcfSAlexander Motin } 2591831f5dcfSAlexander Motin 259215c440e1SWarner Losh #ifdef MMCCAM 2593a94a63f0SWarner Losh void 2594d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2595a94a63f0SWarner Losh { 2596ab00a509SMarius Strobl 2597505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2598a94a63f0SWarner Losh goto fail; 2599a94a63f0SWarner Losh 2600a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2601aeb04e88SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2602da2f833fSBjoern A. Zeeb "sdhci_slot", slot, device_get_unit(slot->bus), 2603a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2604a94a63f0SWarner Losh 2605a94a63f0SWarner Losh if (slot->sim == NULL) { 2606a94a63f0SWarner Losh cam_simq_free(slot->devq); 2607a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2608a94a63f0SWarner Losh goto fail; 2609a94a63f0SWarner Losh } 2610a94a63f0SWarner Losh 2611a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2612a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2613505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2614a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2615a94a63f0SWarner Losh cam_simq_free(slot->devq); 2616a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2617a94a63f0SWarner Losh goto fail; 2618a94a63f0SWarner Losh } 2619a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2620505f6a0cSBjoern A. Zeeb 2621a94a63f0SWarner Losh /* End CAM-specific init */ 2622a94a63f0SWarner Losh slot->card_present = 0; 2623a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2624a94a63f0SWarner Losh return; 2625a94a63f0SWarner Losh 2626a94a63f0SWarner Losh fail: 2627a94a63f0SWarner Losh if (slot->sim != NULL) { 2628a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2629a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2630a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2631a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2632a94a63f0SWarner Losh } 2633a94a63f0SWarner Losh 2634a94a63f0SWarner Losh if (slot->devq != NULL) 2635a94a63f0SWarner Losh cam_simq_free(slot->devq); 2636a94a63f0SWarner Losh } 2637a94a63f0SWarner Losh 2638a94a63f0SWarner Losh void 2639a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2640a94a63f0SWarner Losh { 2641a94a63f0SWarner Losh struct sdhci_slot *slot; 2642a94a63f0SWarner Losh 2643a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2644a94a63f0SWarner Losh if (slot == NULL) { 2645a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2646a94a63f0SWarner Losh xpt_done(ccb); 2647a94a63f0SWarner Losh return; 2648a94a63f0SWarner Losh } 2649a94a63f0SWarner Losh 2650a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2651a94a63f0SWarner Losh 2652a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2653a94a63f0SWarner Losh case XPT_PATH_INQ: 2654cd853791SKonstantin Belousov mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys); 2655a94a63f0SWarner Losh break; 26568c7cd14aSWarner Losh 2657af2253f6SEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS: 2658a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2659a94a63f0SWarner Losh { 2660a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 26615d20e651SIlya Bakulin uint32_t max_data; 2662a94a63f0SWarner Losh 2663a94a63f0SWarner Losh if (sdhci_debug > 1) 2664a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2665a94a63f0SWarner Losh 2666a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2667a94a63f0SWarner Losh cts->protocol_version = 1; 2668a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2669a94a63f0SWarner Losh cts->transport_version = 1; 2670a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2671a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2672a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2673a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2674a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 26755d20e651SIlya Bakulin /* 26765d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 26775d20e651SIlya Bakulin * per read/write command to 4 MiB. 26785d20e651SIlya Bakulin */ 26795d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 26805d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 26815d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 26825d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 26835d20e651SIlya Bakulin } else { 26845d20e651SIlya Bakulin max_data = 65535; 26855d20e651SIlya Bakulin } 26865d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 26875d20e651SIlya Bakulin 2688a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2689a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2690a94a63f0SWarner Losh break; 2691a94a63f0SWarner Losh } 2692af2253f6SEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS: 2693a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2694a94a63f0SWarner Losh if (sdhci_debug > 1) 2695a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2696a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2697a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2698a94a63f0SWarner Losh break; 2699a94a63f0SWarner Losh case XPT_RESET_BUS: 2700a94a63f0SWarner Losh if (sdhci_debug > 1) 2701a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2702a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2703a94a63f0SWarner Losh break; 2704a94a63f0SWarner Losh case XPT_MMC_IO: 2705a94a63f0SWarner Losh /* 2706a94a63f0SWarner Losh * Here is the HW-dependent part of 2707a94a63f0SWarner Losh * sending the command to the underlying h/w 2708a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2709a94a63f0SWarner Losh * Then the request will be marked as completed. 2710a94a63f0SWarner Losh */ 2711aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2712a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2713a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2714a94a63f0SWarner Losh 2715160799c6SWarner Losh sdhci_cam_request(cam_sim_softc(sim), ccb); 2716a94a63f0SWarner Losh return; 2717a94a63f0SWarner Losh default: 2718a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2719a94a63f0SWarner Losh break; 2720a94a63f0SWarner Losh } 2721a94a63f0SWarner Losh xpt_done(ccb); 2722a94a63f0SWarner Losh return; 2723a94a63f0SWarner Losh } 2724a94a63f0SWarner Losh 2725a94a63f0SWarner Losh void 2726a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2727a94a63f0SWarner Losh { 2728*94ff1d9cSAndriy Gapon sdhci_generic_intr(cam_sim_softc(sim)); 2729a94a63f0SWarner Losh } 2730a94a63f0SWarner Losh 27316dea80e6SMarius Strobl static int 2732ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2733ab00a509SMarius Strobl int proposed_clock) 27346dea80e6SMarius Strobl { 2735a94a63f0SWarner Losh int max_clock, clock, i; 2736a94a63f0SWarner Losh 2737a94a63f0SWarner Losh if (proposed_clock == 0) 2738a94a63f0SWarner Losh return 0; 2739a94a63f0SWarner Losh max_clock = slot->max_clk; 2740a94a63f0SWarner Losh clock = max_clock; 2741a94a63f0SWarner Losh 2742a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2743505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2744a94a63f0SWarner Losh if (clock <= proposed_clock) 2745a94a63f0SWarner Losh break; 2746a94a63f0SWarner Losh clock >>= 1; 2747a94a63f0SWarner Losh } 2748a94a63f0SWarner Losh } else { 2749505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2750a94a63f0SWarner Losh if (clock <= proposed_clock) 2751a94a63f0SWarner Losh break; 2752a94a63f0SWarner Losh clock = max_clock / (i + 2); 2753a94a63f0SWarner Losh } 2754a94a63f0SWarner Losh } 2755a94a63f0SWarner Losh return clock; 2756a94a63f0SWarner Losh } 2757a94a63f0SWarner Losh 2758ab00a509SMarius Strobl static int 2759a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2760a94a63f0SWarner Losh { 2761a94a63f0SWarner Losh struct mmc_ios *ios; 2762ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2763ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2764a94a63f0SWarner Losh 2765a94a63f0SWarner Losh ios = &slot->host.ios; 2766a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2767a94a63f0SWarner Losh new_ios = &cts->ios; 2768a94a63f0SWarner Losh 2769a94a63f0SWarner Losh /* Update only requested fields */ 2770a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2771a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2772b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2773a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2774a94a63f0SWarner Losh } 2775a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2776a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2777b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2778a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2779a94a63f0SWarner Losh } 2780a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2781a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2782b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2783a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2784a94a63f0SWarner Losh } 2785a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2786a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2787b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2788a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2789a94a63f0SWarner Losh } 2790a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2791a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2792b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2793a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2794a94a63f0SWarner Losh } 2795a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2796a94a63f0SWarner Losh ios->timing = new_ios->timing; 2797b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2798a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2799a94a63f0SWarner Losh } 2800a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2801a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2802b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2803a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2804a94a63f0SWarner Losh } 28051a96c143SEmmanuel Vadot if (cts->ios_valid & MMC_VCCQ) { 28061a96c143SEmmanuel Vadot ios->vccq = new_ios->vccq; 28071a96c143SEmmanuel Vadot if (sdhci_debug > 1) 28081a96c143SEmmanuel Vadot slot_printf(slot, "VCCQ => %d\n", ios->vccq); 28091a96c143SEmmanuel Vadot } 2810a94a63f0SWarner Losh 2811a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2812a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2813a94a63f0SWarner Losh } 2814a94a63f0SWarner Losh 2815ab00a509SMarius Strobl static int 2816a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2817a94a63f0SWarner Losh { 2818a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2819a94a63f0SWarner Losh 2820b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2821a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2822a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2823a94a63f0SWarner Losh SDHCI_LOCK(slot); 2824a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2825a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2826a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2827a94a63f0SWarner Losh sdhci_init(slot); 2828a94a63f0SWarner Losh } 2829a94a63f0SWarner Losh /* Configure the bus. */ 2830a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2831a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2832a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2833a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2834a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2835a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2836a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2837a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2838a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2839a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2840a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2841a94a63f0SWarner Losh } else { 2842a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2843a94a63f0SWarner Losh } 2844a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2845a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2846a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2847a94a63f0SWarner Losh else 2848a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2849a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2850a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2851a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2852b8f94506SArtur Rojek SDHCI_RESET(slot->bus, slot, 2853b8f94506SArtur Rojek SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2854a94a63f0SWarner Losh 2855a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2856a94a63f0SWarner Losh return (0); 2857a94a63f0SWarner Losh } 2858a94a63f0SWarner Losh 2859ab00a509SMarius Strobl static int 2860a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2861a94a63f0SWarner Losh { 2862ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2863a94a63f0SWarner Losh 2864a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2865a94a63f0SWarner Losh 2866a94a63f0SWarner Losh SDHCI_LOCK(slot); 2867a94a63f0SWarner Losh /* if (slot->req != NULL) { 2868a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2869a94a63f0SWarner Losh return (EBUSY); 2870a94a63f0SWarner Losh } 2871a94a63f0SWarner Losh */ 2872aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 28735d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 28745d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2875a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2876a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 28775d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 28785d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 28795d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2880a94a63f0SWarner Losh } 2881a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2882a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2883a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2884a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2885a94a63f0SWarner Losh } 2886a94a63f0SWarner Losh slot->ccb = ccb; 2887a94a63f0SWarner Losh slot->flags = 0; 2888a94a63f0SWarner Losh sdhci_start(slot); 2889a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2890a94a63f0SWarner Losh return (0); 2891a94a63f0SWarner Losh } 289215c440e1SWarner Losh #endif /* MMCCAM */ 2893a94a63f0SWarner Losh 2894ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2895