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> 39831f5dcfSAlexander Motin #include <sys/lock.h> 40aca38eabSMarius Strobl #include <sys/malloc.h> 41831f5dcfSAlexander Motin #include <sys/module.h> 42831f5dcfSAlexander Motin #include <sys/mutex.h> 43831f5dcfSAlexander Motin #include <sys/resource.h> 44831f5dcfSAlexander Motin #include <sys/rman.h> 455b69a497SAlexander Motin #include <sys/sysctl.h> 46831f5dcfSAlexander Motin #include <sys/taskqueue.h> 47831f5dcfSAlexander Motin 48831f5dcfSAlexander Motin #include <machine/bus.h> 49831f5dcfSAlexander Motin #include <machine/resource.h> 50831f5dcfSAlexander Motin #include <machine/stdarg.h> 51831f5dcfSAlexander Motin 52831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 53831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 54831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 55831f5dcfSAlexander Motin 56aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h> 57aca38eabSMarius Strobl 58a94a63f0SWarner Losh #include <cam/cam.h> 59a94a63f0SWarner Losh #include <cam/cam_ccb.h> 60a94a63f0SWarner Losh #include <cam/cam_debug.h> 61a94a63f0SWarner Losh #include <cam/cam_sim.h> 62a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 63a94a63f0SWarner Losh 64831f5dcfSAlexander Motin #include "mmcbr_if.h" 65d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 66831f5dcfSAlexander Motin 67a94a63f0SWarner Losh #include "opt_mmccam.h" 68a94a63f0SWarner Losh 69f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 70831f5dcfSAlexander Motin 71a94a63f0SWarner Losh static int sdhci_debug = 0; 727e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 737e6ccea3SMarius Strobl "Debug level"); 740f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 750f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 760f34084fSMarius Strobl 0, "Mask of quirks to clear"); 770f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 780f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 790f34084fSMarius Strobl "Mask of quirks to set"); 805b69a497SAlexander Motin 81d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 82d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 83d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 84d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 85d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 86831f5dcfSAlexander Motin 87d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 88d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 89d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 90d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 91d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 92831f5dcfSAlexander Motin 936dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err); 94aca38eabSMarius Strobl static void sdhci_card_poll(void *arg); 95aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending); 966dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); 976dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); 98aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 996dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, 1006dea80e6SMarius Strobl bool is_present); 1016dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot); 1026dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot); 1036dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot); 1046dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot); 105aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req); 1066dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask); 107aca38eabSMarius Strobl static void sdhci_retune(void *arg); 108831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 1096dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power); 1106dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot, 1116dea80e6SMarius Strobl struct mmc_data *data); 112831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 1136dea80e6SMarius Strobl static void sdhci_timeout(void *arg); 1146dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot, 1156dea80e6SMarius Strobl struct mmc_command *cmd); 116831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 1176dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot); 1186dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot); 119831f5dcfSAlexander Motin 12015c440e1SWarner Losh #ifdef MMCCAM 121a94a63f0SWarner Losh /* CAM-related */ 122a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 1236dea80e6SMarius Strobl static int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, 1246dea80e6SMarius Strobl int proposed_clock); 1256dea80e6SMarius Strobl static void sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb); 126a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 1276dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 128a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 1296dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot); 13015c440e1SWarner Losh #endif 131a94a63f0SWarner Losh 132831f5dcfSAlexander Motin /* helper routines */ 1330f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 1346dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, 1356dea80e6SMarius Strobl int error); 1360f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 1370f34084fSMarius Strobl __printflike(2, 3); 138aca38eabSMarius Strobl static uint32_t sdhci_tuning_intmask(struct sdhci_slot *slot); 1390f34084fSMarius Strobl 140831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 141831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 142831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 143831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 144831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 145831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 146831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 147831f5dcfSAlexander Motin 14833aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 14933aad34dSOleksandr Tymoshenko 15057677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 15157677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 15257677a3aSOleksandr Tymoshenko 153639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 154639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 155639f59f0SIan Lepore 15693efdc63SAdrian Chadd /* 15793efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 15893efdc63SAdrian Chadd */ 1591bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1601bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1611bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1621bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 16393efdc63SAdrian Chadd 16493efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 16593efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 16693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 16793efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 16893efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 16993efdc63SAdrian Chadd 170831f5dcfSAlexander Motin static void 171831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 172831f5dcfSAlexander Motin { 1737e6ccea3SMarius Strobl 174831f5dcfSAlexander Motin if (error != 0) { 175831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 176831f5dcfSAlexander Motin return; 177831f5dcfSAlexander Motin } 178831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 179831f5dcfSAlexander Motin } 180831f5dcfSAlexander Motin 181d6b3aaf8SOleksandr Tymoshenko static int 182d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 183d6b3aaf8SOleksandr Tymoshenko { 184d6b3aaf8SOleksandr Tymoshenko va_list ap; 185d6b3aaf8SOleksandr Tymoshenko int retval; 186d6b3aaf8SOleksandr Tymoshenko 187d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 188d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 189d6b3aaf8SOleksandr Tymoshenko 190d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 191d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 192d6b3aaf8SOleksandr Tymoshenko va_end(ap); 193d6b3aaf8SOleksandr Tymoshenko return (retval); 194d6b3aaf8SOleksandr Tymoshenko } 195d6b3aaf8SOleksandr Tymoshenko 196831f5dcfSAlexander Motin static void 197831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 198831f5dcfSAlexander Motin { 1997e6ccea3SMarius Strobl 200831f5dcfSAlexander Motin slot_printf(slot, 201831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 202831f5dcfSAlexander Motin 203831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 204831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 205831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 206831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 207831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 208831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 209831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 210831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 211831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 212831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 213831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 214831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 215831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 216831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 217831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 218831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 2199dbf8c46SMarius Strobl slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 2209dbf8c46SMarius Strobl RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 2219dbf8c46SMarius Strobl slot_printf(slot, "Caps: 0x%08x | Caps2: 0x%08x\n", 2229dbf8c46SMarius Strobl RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 2239dbf8c46SMarius Strobl slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 2249dbf8c46SMarius Strobl RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 2259dbf8c46SMarius Strobl slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 2269dbf8c46SMarius Strobl RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 227831f5dcfSAlexander Motin 228831f5dcfSAlexander Motin slot_printf(slot, 229831f5dcfSAlexander Motin "===========================================\n"); 230831f5dcfSAlexander Motin } 231831f5dcfSAlexander Motin 232831f5dcfSAlexander Motin static void 233831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 234831f5dcfSAlexander Motin { 235831f5dcfSAlexander Motin int timeout; 236b440e965SMarius Strobl uint32_t clock; 237831f5dcfSAlexander Motin 238d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 2396e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 240831f5dcfSAlexander Motin return; 241831f5dcfSAlexander Motin } 242831f5dcfSAlexander Motin 243831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 244831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 245d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 246831f5dcfSAlexander Motin /* This is to force an update */ 247831f5dcfSAlexander Motin clock = slot->clock; 248831f5dcfSAlexander Motin slot->clock = 0; 249831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 250831f5dcfSAlexander Motin } 251831f5dcfSAlexander Motin 252d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 253831f5dcfSAlexander Motin slot->clock = 0; 254d8208d9eSAlexander Motin slot->power = 0; 255d8208d9eSAlexander Motin } 256831f5dcfSAlexander Motin 25761bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 25861bc42f7SIan Lepore 25961bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 26061bc42f7SIan Lepore /* 26161bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 26261bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 26361bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 26461bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 26561bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 26661bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 26761bc42f7SIan Lepore */ 26861bc42f7SIan Lepore timeout = 10000; 26961bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 27061bc42f7SIan Lepore if (timeout <= 0) 27161bc42f7SIan Lepore break; 27261bc42f7SIan Lepore timeout--; 27361bc42f7SIan Lepore DELAY(1); 27461bc42f7SIan Lepore } 27561bc42f7SIan Lepore } 27661bc42f7SIan Lepore 277831f5dcfSAlexander Motin /* Wait max 100 ms */ 27861bc42f7SIan Lepore timeout = 10000; 279831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 28061bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 28161bc42f7SIan Lepore if (timeout <= 0) { 28261bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 28361bc42f7SIan Lepore mask); 284831f5dcfSAlexander Motin sdhci_dumpregs(slot); 285831f5dcfSAlexander Motin return; 286831f5dcfSAlexander Motin } 287831f5dcfSAlexander Motin timeout--; 28861bc42f7SIan Lepore DELAY(10); 289831f5dcfSAlexander Motin } 290831f5dcfSAlexander Motin } 291831f5dcfSAlexander Motin 292aca38eabSMarius Strobl static uint32_t 293aca38eabSMarius Strobl sdhci_tuning_intmask(struct sdhci_slot *slot) 294aca38eabSMarius Strobl { 295aca38eabSMarius Strobl uint32_t intmask; 296aca38eabSMarius Strobl 297aca38eabSMarius Strobl intmask = 0; 29878f8baa8SMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 299aca38eabSMarius Strobl intmask |= SDHCI_INT_TUNEERR; 300aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 301aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_3) 302aca38eabSMarius Strobl intmask |= SDHCI_INT_RETUNE; 303aca38eabSMarius Strobl } 304aca38eabSMarius Strobl return (intmask); 305aca38eabSMarius Strobl } 306aca38eabSMarius Strobl 307831f5dcfSAlexander Motin static void 308831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 309831f5dcfSAlexander Motin { 310831f5dcfSAlexander Motin 311831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 312831f5dcfSAlexander Motin 313831f5dcfSAlexander Motin /* Enable interrupts. */ 314831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 315831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 316831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 317831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 318831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 3193685b398SWarner Losh SDHCI_INT_ACMD12ERR; 320639f59f0SIan Lepore 321639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 322639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 323639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 324639f59f0SIan Lepore } 325639f59f0SIan Lepore 326cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 327831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 328831f5dcfSAlexander Motin } 329831f5dcfSAlexander Motin 330831f5dcfSAlexander Motin static void 331831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 332831f5dcfSAlexander Motin { 33393efdc63SAdrian Chadd uint32_t clk_base; 33493efdc63SAdrian Chadd uint32_t clk_sel; 335831f5dcfSAlexander Motin uint32_t res; 336831f5dcfSAlexander Motin uint16_t clk; 3378f3b7d56SOleksandr Tymoshenko uint16_t div; 338831f5dcfSAlexander Motin int timeout; 339831f5dcfSAlexander Motin 340831f5dcfSAlexander Motin if (clock == slot->clock) 341831f5dcfSAlexander Motin return; 342831f5dcfSAlexander Motin slot->clock = clock; 343831f5dcfSAlexander Motin 344831f5dcfSAlexander Motin /* Turn off the clock. */ 3454ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 3464ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 347b440e965SMarius Strobl /* If no clock requested - leave it so. */ 348831f5dcfSAlexander Motin if (clock == 0) 349831f5dcfSAlexander Motin return; 350ceb9e9f7SIan Lepore 35193efdc63SAdrian Chadd /* Determine the clock base frequency */ 35293efdc63SAdrian Chadd clk_base = slot->max_clk; 35393efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 3541bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 3551bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 35693efdc63SAdrian Chadd 3571bacf3beSMarius Strobl /* 3581bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 3591bacf3beSMarius Strobl */ 36093efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 36193efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 3621bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 3631bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 36493efdc63SAdrian Chadd } else { 3651bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 3661bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 36793efdc63SAdrian Chadd } 36893efdc63SAdrian Chadd 36993efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 37093efdc63SAdrian Chadd } 37193efdc63SAdrian Chadd 372ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 373ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 374ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 375ceb9e9f7SIan Lepore 3768f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 377831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 37893efdc63SAdrian Chadd res = clk_base; 37957677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 380831f5dcfSAlexander Motin if (res <= clock) 381831f5dcfSAlexander Motin break; 382831f5dcfSAlexander Motin res >>= 1; 383831f5dcfSAlexander Motin } 384831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 3858f3b7d56SOleksandr Tymoshenko div >>= 1; 386c11bbc7dSMarius Strobl } else { 3878f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 38893efdc63SAdrian Chadd if (clock >= clk_base) 38957677a3aSOleksandr Tymoshenko div = 0; 3908f3b7d56SOleksandr Tymoshenko else { 39157677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 39293efdc63SAdrian Chadd if ((clk_base / div) <= clock) 3938f3b7d56SOleksandr Tymoshenko break; 3948f3b7d56SOleksandr Tymoshenko } 3958f3b7d56SOleksandr Tymoshenko } 3968f3b7d56SOleksandr Tymoshenko div >>= 1; 3978f3b7d56SOleksandr Tymoshenko } 3988f3b7d56SOleksandr Tymoshenko 3998f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 40093efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 40193efdc63SAdrian Chadd div, clock, clk_base); 4028f3b7d56SOleksandr Tymoshenko 403831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4048f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4058f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4068f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4078f3b7d56SOleksandr Tymoshenko 408831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 409831f5dcfSAlexander Motin /* Enable clock. */ 410831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 411831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 412831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 413831f5dcfSAlexander Motin timeout = 10; 414831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 415831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 416831f5dcfSAlexander Motin if (timeout == 0) { 417831f5dcfSAlexander Motin slot_printf(slot, 418831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 419831f5dcfSAlexander Motin sdhci_dumpregs(slot); 420831f5dcfSAlexander Motin return; 421831f5dcfSAlexander Motin } 422831f5dcfSAlexander Motin timeout--; 423831f5dcfSAlexander Motin DELAY(1000); 424831f5dcfSAlexander Motin } 425831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 426831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 427831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 428831f5dcfSAlexander Motin } 429831f5dcfSAlexander Motin 430831f5dcfSAlexander Motin static void 431831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 432831f5dcfSAlexander Motin { 43385083a80SMarius Strobl int i; 434831f5dcfSAlexander Motin uint8_t pwr; 435831f5dcfSAlexander Motin 436831f5dcfSAlexander Motin if (slot->power == power) 437831f5dcfSAlexander Motin return; 438d6b3aaf8SOleksandr Tymoshenko 439831f5dcfSAlexander Motin slot->power = power; 440831f5dcfSAlexander Motin 441831f5dcfSAlexander Motin /* Turn off the power. */ 442831f5dcfSAlexander Motin pwr = 0; 443831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 444b440e965SMarius Strobl /* If power down requested - leave it so. */ 445831f5dcfSAlexander Motin if (power == 0) 446831f5dcfSAlexander Motin return; 447831f5dcfSAlexander Motin /* Set voltage. */ 448831f5dcfSAlexander Motin switch (1 << power) { 449831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 450831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 451831f5dcfSAlexander Motin break; 452831f5dcfSAlexander Motin case MMC_OCR_290_300: 453831f5dcfSAlexander Motin case MMC_OCR_300_310: 454831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 455831f5dcfSAlexander Motin break; 456831f5dcfSAlexander Motin case MMC_OCR_320_330: 457831f5dcfSAlexander Motin case MMC_OCR_330_340: 458831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 459831f5dcfSAlexander Motin break; 460831f5dcfSAlexander Motin } 461831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 46285083a80SMarius Strobl /* 46385083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 46485083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 4658022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 46685083a80SMarius Strobl */ 467831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 46885083a80SMarius Strobl for (i = 0; i < 20; i++) { 469831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 47085083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 47185083a80SMarius Strobl break; 47285083a80SMarius Strobl DELAY(100); 47385083a80SMarius Strobl } 47485083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 47585083a80SMarius Strobl slot_printf(slot, "Bus power failed to enable"); 476a2832f9fSMarius Strobl 477a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 478a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 479a2832f9fSMarius Strobl DELAY(10); 480a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 481a2832f9fSMarius Strobl DELAY(300); 482a2832f9fSMarius Strobl } 483831f5dcfSAlexander Motin } 484831f5dcfSAlexander Motin 485831f5dcfSAlexander Motin static void 486831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 487831f5dcfSAlexander Motin { 488831f5dcfSAlexander Motin uint32_t data; 489831f5dcfSAlexander Motin char *buffer; 490831f5dcfSAlexander Motin size_t left; 491831f5dcfSAlexander Motin 492831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 493831f5dcfSAlexander Motin buffer += slot->offset; 494831f5dcfSAlexander Motin /* Transfer one block at a time. */ 495831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 496831f5dcfSAlexander Motin slot->offset += left; 497831f5dcfSAlexander Motin 498831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 499d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 500831f5dcfSAlexander Motin DELAY(10); 501ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 502831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 503831f5dcfSAlexander Motin while (left > 3) { 504831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 505831f5dcfSAlexander Motin buffer[0] = data; 506831f5dcfSAlexander Motin buffer[1] = (data >> 8); 507831f5dcfSAlexander Motin buffer[2] = (data >> 16); 508831f5dcfSAlexander Motin buffer[3] = (data >> 24); 509831f5dcfSAlexander Motin buffer += 4; 510831f5dcfSAlexander Motin left -= 4; 511831f5dcfSAlexander Motin } 512831f5dcfSAlexander Motin } else { 513d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 514831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 515831f5dcfSAlexander Motin left &= 3; 516831f5dcfSAlexander Motin } 517831f5dcfSAlexander Motin /* Handle uneven size case. */ 518831f5dcfSAlexander Motin if (left > 0) { 519831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 520831f5dcfSAlexander Motin while (left > 0) { 521831f5dcfSAlexander Motin *(buffer++) = data; 522831f5dcfSAlexander Motin data >>= 8; 523831f5dcfSAlexander Motin left--; 524831f5dcfSAlexander Motin } 525831f5dcfSAlexander Motin } 526831f5dcfSAlexander Motin } 527831f5dcfSAlexander Motin 528831f5dcfSAlexander Motin static void 529831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 530831f5dcfSAlexander Motin { 531831f5dcfSAlexander Motin uint32_t data = 0; 532831f5dcfSAlexander Motin char *buffer; 533831f5dcfSAlexander Motin size_t left; 534831f5dcfSAlexander Motin 535831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 536831f5dcfSAlexander Motin buffer += slot->offset; 537831f5dcfSAlexander Motin /* Transfer one block at a time. */ 538831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 539831f5dcfSAlexander Motin slot->offset += left; 540831f5dcfSAlexander Motin 541ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 542831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 543831f5dcfSAlexander Motin while (left > 3) { 544831f5dcfSAlexander Motin data = buffer[0] + 545831f5dcfSAlexander Motin (buffer[1] << 8) + 546831f5dcfSAlexander Motin (buffer[2] << 16) + 547831f5dcfSAlexander Motin (buffer[3] << 24); 548831f5dcfSAlexander Motin left -= 4; 549831f5dcfSAlexander Motin buffer += 4; 550831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 551831f5dcfSAlexander Motin } 552831f5dcfSAlexander Motin } else { 553d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 554831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 555831f5dcfSAlexander Motin left &= 3; 556831f5dcfSAlexander Motin } 557831f5dcfSAlexander Motin /* Handle uneven size case. */ 558831f5dcfSAlexander Motin if (left > 0) { 559831f5dcfSAlexander Motin while (left > 0) { 560831f5dcfSAlexander Motin data <<= 8; 561831f5dcfSAlexander Motin data += *(buffer++); 562831f5dcfSAlexander Motin left--; 563831f5dcfSAlexander Motin } 564831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 565831f5dcfSAlexander Motin } 566831f5dcfSAlexander Motin } 567831f5dcfSAlexander Motin 568831f5dcfSAlexander Motin static void 569831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 570831f5dcfSAlexander Motin { 571831f5dcfSAlexander Motin 572831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 573831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 574831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 575831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 576831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 577831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 578831f5dcfSAlexander Motin break; 579831f5dcfSAlexander Motin } 580831f5dcfSAlexander Motin } else { 581831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 582831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 583831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 584831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 585831f5dcfSAlexander Motin break; 586831f5dcfSAlexander Motin } 587831f5dcfSAlexander Motin } 588831f5dcfSAlexander Motin } 589831f5dcfSAlexander Motin 590831f5dcfSAlexander Motin static void 5917e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 592831f5dcfSAlexander Motin { 593831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 5947e6ccea3SMarius Strobl device_t d; 595831f5dcfSAlexander Motin 596831f5dcfSAlexander Motin SDHCI_LOCK(slot); 5976e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 598a94a63f0SWarner Losh #ifdef MMCCAM 599a94a63f0SWarner Losh if (slot->card_present == 0) { 600a94a63f0SWarner Losh #else 601831f5dcfSAlexander Motin if (slot->dev == NULL) { 602a94a63f0SWarner Losh #endif 603831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 604639f59f0SIan Lepore if (bootverbose || sdhci_debug) 605639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 606a94a63f0SWarner Losh #ifdef MMCCAM 607a94a63f0SWarner Losh slot->card_present = 1; 608a94a63f0SWarner Losh union ccb *ccb; 609a94a63f0SWarner Losh uint32_t pathid; 610a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 611a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 612a94a63f0SWarner Losh if (ccb == NULL) { 613a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 614a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 615a94a63f0SWarner Losh return; 616a94a63f0SWarner Losh } 617a94a63f0SWarner Losh 618a94a63f0SWarner Losh /* 619a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 620a94a63f0SWarner Losh * will be at lun 0. 621a94a63f0SWarner Losh */ 622a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 623a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 624a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 625a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 626a94a63f0SWarner Losh xpt_free_ccb(ccb); 627a94a63f0SWarner Losh return; 628a94a63f0SWarner Losh } 629a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 630a94a63f0SWarner Losh xpt_rescan(ccb); 631a94a63f0SWarner Losh #else 632aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 633831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 634aca38eabSMarius Strobl if (d) { 635aca38eabSMarius Strobl device_set_ivars(d, slot); 636aca38eabSMarius Strobl (void)device_probe_and_attach(d); 637aca38eabSMarius Strobl } 638a94a63f0SWarner Losh #endif 639831f5dcfSAlexander Motin } else 640831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 641831f5dcfSAlexander Motin } else { 642a94a63f0SWarner Losh #ifdef MMCCAM 643a94a63f0SWarner Losh if (slot->card_present == 1) { 644a94a63f0SWarner Losh #else 645831f5dcfSAlexander Motin if (slot->dev != NULL) { 646a94a63f0SWarner Losh #endif 647831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 648639f59f0SIan Lepore if (bootverbose || sdhci_debug) 649639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 6507e6ccea3SMarius Strobl d = slot->dev; 651831f5dcfSAlexander Motin slot->dev = NULL; 652a94a63f0SWarner Losh #ifdef MMCCAM 653a94a63f0SWarner Losh slot->card_present = 0; 654a94a63f0SWarner Losh union ccb *ccb; 655a94a63f0SWarner Losh uint32_t pathid; 656a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 657a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 658a94a63f0SWarner Losh if (ccb == NULL) { 659a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 660a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 661a94a63f0SWarner Losh return; 662a94a63f0SWarner Losh } 663a94a63f0SWarner Losh 664a94a63f0SWarner Losh /* 665a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 666a94a63f0SWarner Losh * will be at lun 0. 667a94a63f0SWarner Losh */ 668a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 669a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 670a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 671a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 672a94a63f0SWarner Losh xpt_free_ccb(ccb); 673a94a63f0SWarner Losh return; 674a94a63f0SWarner Losh } 675a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 676a94a63f0SWarner Losh xpt_rescan(ccb); 677a94a63f0SWarner Losh #else 678aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 679cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 680aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 681aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 682831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 683aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 684d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 685a94a63f0SWarner Losh #endif 686831f5dcfSAlexander Motin } else 687831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 688831f5dcfSAlexander Motin } 689831f5dcfSAlexander Motin } 690831f5dcfSAlexander Motin 691b8bf08b1SIan Lepore static void 692b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 693639f59f0SIan Lepore { 694639f59f0SIan Lepore bool was_present; 695639f59f0SIan Lepore 696639f59f0SIan Lepore /* 697639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 698639f59f0SIan Lepore * create the child device after a short delay. The delay is to 699639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 700639f59f0SIan Lepore * before the other pins have made good contact). 701639f59f0SIan Lepore * 702639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 703639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 704639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 705639f59f0SIan Lepore * that happens by deleting and recreating the child device. 706639f59f0SIan Lepore */ 707a94a63f0SWarner Losh #ifdef MMCCAM 708a94a63f0SWarner Losh was_present = slot->card_present; 709a94a63f0SWarner Losh #else 710639f59f0SIan Lepore was_present = slot->dev != NULL; 711a94a63f0SWarner Losh #endif 712639f59f0SIan Lepore if (!was_present && is_present) { 713639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 714639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 715639f59f0SIan Lepore } else if (was_present && !is_present) { 716639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 717639f59f0SIan Lepore } 718b8bf08b1SIan Lepore } 719b8bf08b1SIan Lepore 720b8bf08b1SIan Lepore void 721b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 722b8bf08b1SIan Lepore { 723b8bf08b1SIan Lepore 724b8bf08b1SIan Lepore SDHCI_LOCK(slot); 725b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 726639f59f0SIan Lepore SDHCI_UNLOCK(slot); 727639f59f0SIan Lepore } 728639f59f0SIan Lepore 729639f59f0SIan Lepore static void 730639f59f0SIan Lepore sdhci_card_poll(void *arg) 731639f59f0SIan Lepore { 732639f59f0SIan Lepore struct sdhci_slot *slot = arg; 733639f59f0SIan Lepore 734639f59f0SIan Lepore sdhci_handle_card_present(slot, 735639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 736639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 737639f59f0SIan Lepore sdhci_card_poll, slot); 738639f59f0SIan Lepore } 739639f59f0SIan Lepore 740d6b3aaf8SOleksandr Tymoshenko int 741d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 742831f5dcfSAlexander Motin { 743aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 744aca38eabSMarius Strobl kobj_method_t *kobj_method; 7450f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 746d6b3aaf8SOleksandr Tymoshenko int err; 747831f5dcfSAlexander Motin 748831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 749a94a63f0SWarner Losh 750d6b3aaf8SOleksandr Tymoshenko slot->num = num; 751d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 752d6b3aaf8SOleksandr Tymoshenko 753831f5dcfSAlexander Motin /* Allocate DMA tag. */ 754831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 755831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 756831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 757831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 758831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 759831f5dcfSAlexander Motin &slot->dmatag); 760831f5dcfSAlexander Motin if (err != 0) { 761831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 762831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 763d6b3aaf8SOleksandr Tymoshenko return (err); 764831f5dcfSAlexander Motin } 765831f5dcfSAlexander Motin /* Allocate DMA memory. */ 766831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 767831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 768831f5dcfSAlexander Motin if (err != 0) { 769831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 7707fcf4780SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 771831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 772d6b3aaf8SOleksandr Tymoshenko return (err); 773831f5dcfSAlexander Motin } 774831f5dcfSAlexander Motin /* Map the memory. */ 775831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 776831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 777831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 778831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 779831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 7807fcf4780SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 7817fcf4780SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 782831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 783d6b3aaf8SOleksandr Tymoshenko if (err) 784d6b3aaf8SOleksandr Tymoshenko return (err); 785d6b3aaf8SOleksandr Tymoshenko else 786d6b3aaf8SOleksandr Tymoshenko return (EFAULT); 787831f5dcfSAlexander Motin } 788d6b3aaf8SOleksandr Tymoshenko 789d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 790d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 7910f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 7928f3b7d56SOleksandr Tymoshenko caps = slot->caps; 7930f34084fSMarius Strobl caps2 = slot->caps2; 7940f34084fSMarius Strobl } else { 795831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 7960f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 7970f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 7980f34084fSMarius Strobl else 7990f34084fSMarius Strobl caps2 = 0; 8000f34084fSMarius Strobl } 8017fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8027fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8037fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 8047fcf4780SMarius Strobl device_printf(dev, 8057fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 8067fcf4780SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 8077fcf4780SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, 8087fcf4780SMarius Strobl slot->dmamap); 8097fcf4780SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 8107fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 8117fcf4780SMarius Strobl return (ENXIO); 8127fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 8137fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 8147fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 8157fcf4780SMarius Strobl } 8167fcf4780SMarius Strobl } 817831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 81833aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 81987a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 82087a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 82133aad34dSOleksandr Tymoshenko else 82287a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 82387a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 82487a6a871SIan Lepore if (freq != 0) 82587a6a871SIan Lepore slot->max_clk = freq * 1000000; 82687a6a871SIan Lepore /* 82787a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 82887a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 82987a6a871SIan Lepore * with an assumption, so complain about it. 83087a6a871SIan Lepore */ 831831f5dcfSAlexander Motin if (slot->max_clk == 0) { 83287a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 833831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 8341bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 8351bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 836831f5dcfSAlexander Motin } 837a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 8388f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 8398f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 840a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 841a2832f9fSMarius Strobl slot->timeout_clk = 1000; 8428f3b7d56SOleksandr Tymoshenko } else { 8431bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 8441bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 8458f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 8468f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 8478f3b7d56SOleksandr Tymoshenko } 84887a6a871SIan Lepore /* 84987a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 85087a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 85187a6a871SIan Lepore * max timeout, but still mention it. 85287a6a871SIan Lepore */ 853831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 854831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 855ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 856ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 857831f5dcfSAlexander Motin } 858831f5dcfSAlexander Motin 85957677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 860831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 861831f5dcfSAlexander Motin slot->host.host_ocr = 0; 862831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 863831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 864831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 865831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 8667fcf4780SMarius Strobl /* 1.8V VDD is not supposed to be used for removable cards. */ 8677fcf4780SMarius Strobl if ((caps & SDHCI_CAN_VDD_180) && (slot->opt & SDHCI_SLOT_EMBEDDED)) 868831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 869831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 870831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 871831f5dcfSAlexander Motin "support voltages.\n"); 872831f5dcfSAlexander Motin } 873aca38eabSMarius Strobl 8740f34084fSMarius Strobl host_caps = MMC_CAP_4_BIT_DATA; 8752d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 8760f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 877831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 8780f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 87972dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 8800f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 88172dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 8820f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 883aca38eabSMarius Strobl 884aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 8850f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 8860f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 8870f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 8880f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 8890f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 8900f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 8910f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 8920f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 8930f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 8940f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 8950f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 8960f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 8970f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 8980f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 8990f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9000f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 901*835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 902*835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 903*835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 904aca38eabSMarius Strobl 905aca38eabSMarius Strobl /* 906aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 907aca38eabSMarius Strobl * default NULL implementation. 908aca38eabSMarius Strobl */ 909aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 910aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 911aca38eabSMarius Strobl kobj_desc); 912aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 913aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 914aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 915aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 916aca38eabSMarius Strobl 917aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 918aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 919aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 920aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 921aca38eabSMarius Strobl 922aca38eabSMarius Strobl /* 923aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 924aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 925aca38eabSMarius Strobl */ 926aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 927aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 928aca38eabSMarius Strobl kobj_desc); 929aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 930aca38eabSMarius Strobl goto no_tuning; 931aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 932aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 933aca38eabSMarius Strobl kobj_desc); 934aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 935aca38eabSMarius Strobl no_tuning: 936aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 937aca38eabSMarius Strobl } 938aca38eabSMarius Strobl 939aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 940aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 941aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 942aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 943aca38eabSMarius Strobl M_WAITOK); 944aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 945aca38eabSMarius Strobl M_WAITOK); 946aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 947aca38eabSMarius Strobl M_WAITOK); 948aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 949aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 950aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 951aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 952aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 953aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 954aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 955aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 956aca38eabSMarius Strobl device_printf(dev, "Unknown re-tuning count " 957aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 958aca38eabSMarius Strobl slot->retune_count = 1; 959aca38eabSMarius Strobl } else if (slot->retune_count != 0) 960aca38eabSMarius Strobl slot->retune_count = 961aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 962aca38eabSMarius Strobl } 963aca38eabSMarius Strobl } 964aca38eabSMarius Strobl 965aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 966aca38eabSMarius Strobl 967aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 9680f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 9690f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 970aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 9710f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 9720f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 973aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 974aca38eabSMarius Strobl 975aca38eabSMarius Strobl /* 976aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 977aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 978aca38eabSMarius Strobl * generic SDHCI implementation. 979aca38eabSMarius Strobl */ 980aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 981aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 982aca38eabSMarius Strobl kobj_desc); 983aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 984aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 985aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 986aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 987aca38eabSMarius Strobl 988aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 989f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 9900f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 991f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 9920f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 993f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 9940f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 9950f34084fSMarius Strobl slot->host.caps = host_caps; 9960f34084fSMarius Strobl 997831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 998831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 999831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1000d6b3aaf8SOleksandr Tymoshenko 1001d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1002831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1003d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1004831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1005a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1006a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1007831f5dcfSAlexander Motin 1008c3a0f75aSOleksandr Tymoshenko /* 1009c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1010c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1011c3a0f75aSOleksandr Tymoshenko */ 1012c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1013c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1014c3a0f75aSOleksandr Tymoshenko 10155b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 10160f34084fSMarius Strobl slot_printf(slot, 10177fcf4780SMarius Strobl "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 1018831f5dcfSAlexander Motin slot->max_clk / 1000000, 1019831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 10200f34084fSMarius Strobl (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 10210f34084fSMarius Strobl ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 1022831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 1023831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 10247fcf4780SMarius Strobl ((caps & SDHCI_CAN_VDD_180) && 10257fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 10260f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 10270f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 1028aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 1029aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 1030aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 10317fcf4780SMarius Strobl (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 10327fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 10337fcf4780SMarius Strobl (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 10347fcf4780SMarius Strobl "removable"); 10350f34084fSMarius Strobl if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 10360f34084fSMarius Strobl MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 10370f34084fSMarius Strobl slot_printf(slot, "eMMC:%s%s%s%s\n", 10380f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 10390f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 10400f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 10410f34084fSMarius Strobl ((host_caps & 10420f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 10430f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 10440f34084fSMarius Strobl " HS400ES" : ""); 10450f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 10460f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 10470f34084fSMarius Strobl slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 10480f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 10490f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 10500f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 10510f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 10520f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 1053aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) 1054aca38eabSMarius Strobl slot_printf(slot, "Re-tuning count %d secs, mode %d\n", 1055aca38eabSMarius Strobl slot->retune_count, slot->retune_mode + 1); 1056831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1057831f5dcfSAlexander Motin } 1058831f5dcfSAlexander Motin 1059ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1060ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1061ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 1062ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 1063ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1064831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1065639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1066639f59f0SIan Lepore sdhci_card_task, slot); 1067639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1068e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1069aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1070ba6fc1c7SLuiz Otavio O Souza 1071639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1072639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1073639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1074639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1075639f59f0SIan Lepore } 1076639f59f0SIan Lepore 1077aca38eabSMarius Strobl sdhci_init(slot); 1078aca38eabSMarius Strobl 1079831f5dcfSAlexander Motin return (0); 1080831f5dcfSAlexander Motin } 1081831f5dcfSAlexander Motin 1082d91f1a10SIlya Bakulin #ifndef MMCCAM 1083d6b3aaf8SOleksandr Tymoshenko void 1084d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1085831f5dcfSAlexander Motin { 10867e6ccea3SMarius Strobl 1087d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1088d6b3aaf8SOleksandr Tymoshenko } 1089d91f1a10SIlya Bakulin #endif 1090831f5dcfSAlexander Motin 1091d6b3aaf8SOleksandr Tymoshenko int 1092d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1093d6b3aaf8SOleksandr Tymoshenko { 1094831f5dcfSAlexander Motin device_t d; 1095831f5dcfSAlexander Motin 1096e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1097639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1098aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1099831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1100639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1101831f5dcfSAlexander Motin 1102831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1103831f5dcfSAlexander Motin d = slot->dev; 1104831f5dcfSAlexander Motin slot->dev = NULL; 1105831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1106831f5dcfSAlexander Motin if (d != NULL) 1107d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1108831f5dcfSAlexander Motin 1109831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1110831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 1111831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1112831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 1113831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 1114831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 1115aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1116aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1117aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1118aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1119aca38eabSMarius Strobl } 1120d6b3aaf8SOleksandr Tymoshenko 1121831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1122d6b3aaf8SOleksandr Tymoshenko 1123831f5dcfSAlexander Motin return (0); 1124831f5dcfSAlexander Motin } 1125831f5dcfSAlexander Motin 1126d6b3aaf8SOleksandr Tymoshenko int 1127d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 112892bf0e27SAlexander Motin { 11297e6ccea3SMarius Strobl 1130aca38eabSMarius Strobl /* 1131aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1132aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1133aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1134aca38eabSMarius Strobl */ 1135aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1136aca38eabSMarius Strobl SDHCI_LOCK(slot); 1137aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1138d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 1139aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 114092bf0e27SAlexander Motin 114192bf0e27SAlexander Motin return (0); 114292bf0e27SAlexander Motin } 114392bf0e27SAlexander Motin 1144d6b3aaf8SOleksandr Tymoshenko int 1145d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 114692bf0e27SAlexander Motin { 11477e6ccea3SMarius Strobl 1148aca38eabSMarius Strobl SDHCI_LOCK(slot); 1149d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1150aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 115192bf0e27SAlexander Motin 1152d6b3aaf8SOleksandr Tymoshenko return (0); 115392bf0e27SAlexander Motin } 115492bf0e27SAlexander Motin 115557677a3aSOleksandr Tymoshenko uint32_t 1156b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 115757677a3aSOleksandr Tymoshenko { 11587e6ccea3SMarius Strobl 115957677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 116057677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 116157677a3aSOleksandr Tymoshenko else 116257677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 116357677a3aSOleksandr Tymoshenko } 116457677a3aSOleksandr Tymoshenko 11656e37fb2bSIan Lepore bool 1166b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 11676e37fb2bSIan Lepore { 11686e37fb2bSIan Lepore 1169639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1170639f59f0SIan Lepore return true; 1171639f59f0SIan Lepore 11726e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 11736e37fb2bSIan Lepore } 11746e37fb2bSIan Lepore 11750f34084fSMarius Strobl void 11760f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 11770f34084fSMarius Strobl { 11780f34084fSMarius Strobl struct mmc_ios *ios; 11790f34084fSMarius Strobl uint16_t hostctrl2; 11800f34084fSMarius Strobl 11810f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 11820f34084fSMarius Strobl return; 11830f34084fSMarius Strobl 1184aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 11850f34084fSMarius Strobl ios = &slot->host.ios; 11860f34084fSMarius Strobl sdhci_set_clock(slot, 0); 11870f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 11880f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1189aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 11900f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 11910f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 11920f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1193aca38eabSMarius Strobl else 11940f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1195aca38eabSMarius Strobl } 11960f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 11970f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 11980f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 11990f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 12000f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 12010f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 12020f34084fSMarius Strobl else 12030f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 12040f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 12050f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 12060f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12070f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 12080f34084fSMarius Strobl } 12090f34084fSMarius Strobl 1210d6b3aaf8SOleksandr Tymoshenko int 1211d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1212831f5dcfSAlexander Motin { 1213831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1214831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1215831f5dcfSAlexander Motin 1216831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1217831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1218831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1219831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1220831f5dcfSAlexander Motin sdhci_init(slot); 1221831f5dcfSAlexander Motin } 1222831f5dcfSAlexander Motin /* Configure the bus. */ 1223831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1224831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 12252d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 12262d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1227831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12282d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 12292d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12302d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 12312d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 12322d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12332d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12342d1731b8SIan Lepore } else { 12352d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 12362d1731b8SIan Lepore } 12370f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1238bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1239831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1240831f5dcfSAlexander Motin else 1241831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1242831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 12430f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1244831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1245d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1246831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1247831f5dcfSAlexander Motin 1248831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1249831f5dcfSAlexander Motin return (0); 1250831f5dcfSAlexander Motin } 1251831f5dcfSAlexander Motin 12520f34084fSMarius Strobl int 12530f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 12540f34084fSMarius Strobl { 12550f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 12560f34084fSMarius Strobl enum mmc_vccq vccq; 12570f34084fSMarius Strobl int err; 12580f34084fSMarius Strobl uint16_t hostctrl2; 12590f34084fSMarius Strobl 12600f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 12610f34084fSMarius Strobl return (0); 12620f34084fSMarius Strobl 12630f34084fSMarius Strobl err = 0; 12640f34084fSMarius Strobl vccq = slot->host.ios.vccq; 12650f34084fSMarius Strobl SDHCI_LOCK(slot); 12660f34084fSMarius Strobl sdhci_set_clock(slot, 0); 12670f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12680f34084fSMarius Strobl switch (vccq) { 12690f34084fSMarius Strobl case vccq_330: 12700f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 12710f34084fSMarius Strobl goto done; 12720f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 12730f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12740f34084fSMarius Strobl DELAY(5000); 12750f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12760f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 12770f34084fSMarius Strobl goto done; 12780f34084fSMarius Strobl err = EAGAIN; 12790f34084fSMarius Strobl break; 12800f34084fSMarius Strobl case vccq_180: 12810f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 12820f34084fSMarius Strobl err = EINVAL; 12830f34084fSMarius Strobl goto done; 12840f34084fSMarius Strobl } 12850f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 12860f34084fSMarius Strobl goto done; 12870f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 12880f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12890f34084fSMarius Strobl DELAY(5000); 12900f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12910f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 12920f34084fSMarius Strobl goto done; 12930f34084fSMarius Strobl err = EAGAIN; 12940f34084fSMarius Strobl break; 12950f34084fSMarius Strobl default: 12960f34084fSMarius Strobl slot_printf(slot, 12970f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 12980f34084fSMarius Strobl err = EINVAL; 12990f34084fSMarius Strobl break; 13000f34084fSMarius Strobl } 13010f34084fSMarius Strobl done: 13020f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 13030f34084fSMarius Strobl SDHCI_UNLOCK(slot); 13040f34084fSMarius Strobl return (err); 13050f34084fSMarius Strobl } 13060f34084fSMarius Strobl 1307aca38eabSMarius Strobl int 1308aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1309aca38eabSMarius Strobl { 1310aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1311aca38eabSMarius Strobl struct mmc_ios *ios = &slot->host.ios; 1312aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1313aca38eabSMarius Strobl struct mmc_data *tune_data; 1314aca38eabSMarius Strobl uint32_t opcode; 1315aca38eabSMarius Strobl int err; 1316aca38eabSMarius Strobl 1317aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1318aca38eabSMarius Strobl return (0); 1319aca38eabSMarius Strobl 1320aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1321aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1322aca38eabSMarius Strobl SDHCI_LOCK(slot); 1323aca38eabSMarius Strobl switch (ios->timing) { 1324aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1325aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1326aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1327aca38eabSMarius Strobl return (EINVAL); 1328aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1329aca38eabSMarius Strobl /* 1330aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1331aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1332aca38eabSMarius Strobl * expected to be required. 1333aca38eabSMarius Strobl */ 1334aca38eabSMarius Strobl if (hs400) 1335aca38eabSMarius Strobl slot->retune_ticks = 0; 1336aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1337aca38eabSMarius Strobl break; 1338aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1339aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1340aca38eabSMarius Strobl break; 1341aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1342aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1343aca38eabSMarius Strobl break; 1344aca38eabSMarius Strobl /* FALLTHROUGH */ 1345aca38eabSMarius Strobl default: 1346aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1347aca38eabSMarius Strobl return (0); 1348aca38eabSMarius Strobl } 1349aca38eabSMarius Strobl 1350aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1351aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1352aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1353aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1354aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1355aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1356aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1357aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1358aca38eabSMarius Strobl MMC_TUNING_LEN; 1359aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1360aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1361aca38eabSMarius Strobl 1362aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1363aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1364aca38eabSMarius Strobl if (err == 0) { 1365aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1366aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1367cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1368aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1369aca38eabSMarius Strobl if (slot->retune_ticks) { 1370aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1371aca38eabSMarius Strobl sdhci_retune, slot); 1372aca38eabSMarius Strobl } 1373aca38eabSMarius Strobl } 1374aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1375aca38eabSMarius Strobl return (err); 1376aca38eabSMarius Strobl } 1377aca38eabSMarius Strobl 1378aca38eabSMarius Strobl int 1379aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1380aca38eabSMarius Strobl { 1381aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1382aca38eabSMarius Strobl int err; 1383aca38eabSMarius Strobl 1384aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1385aca38eabSMarius Strobl return (0); 1386aca38eabSMarius Strobl 1387aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1388aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1389aca38eabSMarius Strobl return (EINVAL); 1390aca38eabSMarius Strobl 1391aca38eabSMarius Strobl SDHCI_LOCK(slot); 1392aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1393aca38eabSMarius Strobl /* 1394aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1395aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1396aca38eabSMarius Strobl * with the host properly acquired, and 1397aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1398aca38eabSMarius Strobl * 1399aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1400aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1401aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1402aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1403aca38eabSMarius Strobl */ 1404aca38eabSMarius Strobl if (slot->retune_ticks) { 1405aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1406aca38eabSMarius Strobl sdhci_retune, slot); 1407aca38eabSMarius Strobl } 1408aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1409aca38eabSMarius Strobl return (err); 1410aca38eabSMarius Strobl } 1411aca38eabSMarius Strobl 1412aca38eabSMarius Strobl static int 1413aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1414aca38eabSMarius Strobl { 1415aca38eabSMarius Strobl struct mmc_request *tune_req; 1416aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1417aca38eabSMarius Strobl int i; 1418aca38eabSMarius Strobl uint32_t intmask; 1419aca38eabSMarius Strobl uint16_t hostctrl2; 1420aca38eabSMarius Strobl u_char opt; 1421aca38eabSMarius Strobl 1422aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1423aca38eabSMarius Strobl if (slot->req != NULL) 1424aca38eabSMarius Strobl return (EBUSY); 1425aca38eabSMarius Strobl 1426aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1427aca38eabSMarius Strobl opt = slot->opt; 1428aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1429aca38eabSMarius Strobl 1430aca38eabSMarius Strobl /* 1431aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1432aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1433aca38eabSMarius Strobl */ 1434aca38eabSMarius Strobl intmask = slot->intmask; 1435aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1436cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1437aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1438aca38eabSMarius Strobl 1439aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1440aca38eabSMarius Strobl if (reset) 1441aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1442aca38eabSMarius Strobl else 1443aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1444aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1445aca38eabSMarius Strobl 1446aca38eabSMarius Strobl tune_req = slot->tune_req; 1447aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1448aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1449aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1450aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1451aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1452aca38eabSMarius Strobl tune_req->done_data = slot; 1453aca38eabSMarius Strobl slot->req = tune_req; 1454aca38eabSMarius Strobl slot->flags = 0; 1455aca38eabSMarius Strobl sdhci_start(slot); 1456aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1457aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1458aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1459aca38eabSMarius Strobl break; 1460aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1461aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1462aca38eabSMarius Strobl break; 1463aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1464aca38eabSMarius Strobl DELAY(1000); 1465aca38eabSMarius Strobl } 1466aca38eabSMarius Strobl 146778f8baa8SMarius Strobl /* 146878f8baa8SMarius Strobl * Restore DMA usage and interrupts. 146978f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 147078f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 147178f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 147278f8baa8SMarius Strobl * doesn't lose these. 147378f8baa8SMarius Strobl */ 1474aca38eabSMarius Strobl slot->opt = opt; 1475aca38eabSMarius Strobl slot->intmask = intmask; 147678f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 147778f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1478aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1479aca38eabSMarius Strobl 1480aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1481aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1482aca38eabSMarius Strobl slot->retune_req = 0; 1483aca38eabSMarius Strobl return (0); 1484aca38eabSMarius Strobl } 1485aca38eabSMarius Strobl 1486aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1487aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1488aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1489aca38eabSMarius Strobl sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1490aca38eabSMarius Strobl return (EIO); 1491aca38eabSMarius Strobl } 1492aca38eabSMarius Strobl 1493aca38eabSMarius Strobl static void 1494aca38eabSMarius Strobl sdhci_retune(void *arg) 1495aca38eabSMarius Strobl { 1496aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1497aca38eabSMarius Strobl 1498aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1499aca38eabSMarius Strobl } 1500aca38eabSMarius Strobl 1501a94a63f0SWarner Losh #ifdef MMCCAM 1502a94a63f0SWarner Losh static void 1503a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1504a94a63f0SWarner Losh { 1505a94a63f0SWarner Losh union ccb *ccb; 150615c440e1SWarner Losh 1507aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 150815c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1509a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1510a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1511a94a63f0SWarner Losh ccb = slot->ccb; 1512a94a63f0SWarner Losh slot->ccb = NULL; 1513a94a63f0SWarner Losh slot->curcmd = NULL; 1514a94a63f0SWarner Losh 1515a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1516a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1517a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1518a94a63f0SWarner Losh 1519a94a63f0SWarner Losh ccb->ccb_h.status = 1520a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1521a94a63f0SWarner Losh xpt_done(ccb); 1522a94a63f0SWarner Losh } 1523a94a63f0SWarner Losh } 1524a94a63f0SWarner Losh #else 1525831f5dcfSAlexander Motin static void 1526e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1527e64f01a9SIan Lepore { 1528e64f01a9SIan Lepore struct mmc_request *req; 1529e64f01a9SIan Lepore 1530e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1531e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1532e64f01a9SIan Lepore req = slot->req; 1533e64f01a9SIan Lepore slot->req = NULL; 1534e64f01a9SIan Lepore slot->curcmd = NULL; 1535e64f01a9SIan Lepore req->done(req); 1536e64f01a9SIan Lepore } 1537e64f01a9SIan Lepore } 1538a94a63f0SWarner Losh #endif 1539e64f01a9SIan Lepore 1540e64f01a9SIan Lepore static void 1541aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1542aca38eabSMarius Strobl { 1543aca38eabSMarius Strobl struct sdhci_slot *slot; 1544aca38eabSMarius Strobl 1545aca38eabSMarius Strobl slot = req->done_data; 1546aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1547aca38eabSMarius Strobl wakeup(req); 1548aca38eabSMarius Strobl } 1549aca38eabSMarius Strobl 1550aca38eabSMarius Strobl static void 1551e64f01a9SIan Lepore sdhci_timeout(void *arg) 1552e64f01a9SIan Lepore { 1553e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1554e64f01a9SIan Lepore 1555e64f01a9SIan Lepore if (slot->curcmd != NULL) { 15567e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 15577e586643SIan Lepore sdhci_dumpregs(slot); 1558a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1559e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1560e64f01a9SIan Lepore sdhci_req_done(slot); 15617e586643SIan Lepore } else { 15627e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1563e64f01a9SIan Lepore } 1564e64f01a9SIan Lepore } 1565e64f01a9SIan Lepore 1566e64f01a9SIan Lepore static void 1567b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data) 1568831f5dcfSAlexander Motin { 1569831f5dcfSAlexander Motin uint16_t mode; 1570831f5dcfSAlexander Motin 1571831f5dcfSAlexander Motin if (data == NULL) 1572831f5dcfSAlexander Motin return; 1573831f5dcfSAlexander Motin 1574831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 15756dea80e6SMarius Strobl if (data->len > 512) { 1576831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 15776dea80e6SMarius Strobl if (__predict_true( 15786dea80e6SMarius Strobl #ifdef MMCCAM 15796dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 15806dea80e6SMarius Strobl #else 15810519c933SMarius Strobl slot->req->stop != NULL && 15826dea80e6SMarius Strobl #endif 15836dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 15846dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 15856dea80e6SMarius Strobl } 1586831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1587831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1588831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1589831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1590831f5dcfSAlexander Motin 1591831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1592831f5dcfSAlexander Motin } 1593831f5dcfSAlexander Motin 1594831f5dcfSAlexander Motin static void 1595831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1596831f5dcfSAlexander Motin { 1597831f5dcfSAlexander Motin int flags, timeout; 159890993663SIan Lepore uint32_t mask; 1599831f5dcfSAlexander Motin 1600831f5dcfSAlexander Motin slot->curcmd = cmd; 1601831f5dcfSAlexander Motin slot->cmd_done = 0; 1602831f5dcfSAlexander Motin 1603831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1604831f5dcfSAlexander Motin 1605831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1606831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1607831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1608831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1609e64f01a9SIan Lepore sdhci_req_done(slot); 1610831f5dcfSAlexander Motin return; 1611831f5dcfSAlexander Motin } 1612831f5dcfSAlexander Motin 1613b440e965SMarius Strobl /* 1614b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1615b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1616b440e965SMarius Strobl */ 16176e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1618d8208d9eSAlexander Motin slot->power == 0 || 1619d8208d9eSAlexander Motin slot->clock == 0) { 1620a94a63f0SWarner Losh slot_printf(slot, 1621a94a63f0SWarner Losh "Cannot issue a command (power=%d clock=%d)", 1622a94a63f0SWarner Losh slot->power, slot->clock); 1623831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1624e64f01a9SIan Lepore sdhci_req_done(slot); 1625831f5dcfSAlexander Motin return; 1626831f5dcfSAlexander Motin } 1627831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1628831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1629831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1630a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1631831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1632aca38eabSMarius Strobl /* 1633aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1634aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1635aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1636aca38eabSMarius Strobl */ 1637a94a63f0SWarner Losh #ifdef MMCCAM 1638aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1639a94a63f0SWarner Losh #else 1640aca38eabSMarius Strobl if (cmd == slot->req->stop || 1641a94a63f0SWarner Losh #endif 1642aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1643aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1644aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 16458775ab45SIan Lepore /* 16468775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 16478775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 16488775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 16498775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 16508775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 16518775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 16528775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 16538775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 16548775ab45SIan Lepore */ 16558775ab45SIan Lepore timeout = 250; 165690993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1657831f5dcfSAlexander Motin if (timeout == 0) { 1658831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1659831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1660831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1661831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1662e64f01a9SIan Lepore sdhci_req_done(slot); 1663831f5dcfSAlexander Motin return; 1664831f5dcfSAlexander Motin } 1665831f5dcfSAlexander Motin timeout--; 1666831f5dcfSAlexander Motin DELAY(1000); 1667831f5dcfSAlexander Motin } 1668831f5dcfSAlexander Motin 1669831f5dcfSAlexander Motin /* Prepare command flags. */ 1670831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1671831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1672831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1673831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1674831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1675831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1676831f5dcfSAlexander Motin else 1677831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1678831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1679831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1680831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1681831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1682a94a63f0SWarner Losh if (cmd->data != NULL) 1683831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1684831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1685831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1686831f5dcfSAlexander Motin /* Prepare data. */ 1687831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1688831f5dcfSAlexander Motin /* 1689831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1690831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1691831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1692831f5dcfSAlexander Motin */ 1693831f5dcfSAlexander Motin if (slot->data_done == 0) { 1694831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1695831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1696831f5dcfSAlexander Motin } 1697831f5dcfSAlexander Motin /* Set command argument. */ 1698831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1699831f5dcfSAlexander Motin /* Set data transfer mode. */ 1700831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1701aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1702a94a63f0SWarner Losh slot_printf(slot, "Starting command!\n"); 1703831f5dcfSAlexander Motin /* Start command. */ 1704d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1705a6873fd1SIan Lepore /* Start timeout callout. */ 1706ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1707ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1708831f5dcfSAlexander Motin } 1709831f5dcfSAlexander Motin 1710831f5dcfSAlexander Motin static void 1711831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1712831f5dcfSAlexander Motin { 1713831f5dcfSAlexander Motin int i; 17141bacf3beSMarius Strobl uint32_t val; 17151bacf3beSMarius Strobl uint8_t extra; 1716831f5dcfSAlexander Motin 1717aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1718a94a63f0SWarner Losh slot_printf(slot, "%s: called, err %d flags %d\n", 1719a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1720831f5dcfSAlexander Motin slot->cmd_done = 1; 172172dec079SMarius Strobl /* 172272dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1723831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 172472dec079SMarius Strobl * happened first. 172572dec079SMarius Strobl */ 1726aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1727aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1728aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1729aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1730831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1731831f5dcfSAlexander Motin if (slot->curcmd->error) { 1732aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1733aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1734831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1735831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1736831f5dcfSAlexander Motin sdhci_start(slot); 1737831f5dcfSAlexander Motin return; 1738831f5dcfSAlexander Motin } 1739831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1740831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1741831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1742831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 17431bacf3beSMarius Strobl extra = 0; 1744831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 17451bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 17461bacf3beSMarius Strobl if (slot->quirks & 17471bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1748677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1749677ee494SIan Lepore else { 1750677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1751677ee494SIan Lepore (val << 8) | extra; 1752831f5dcfSAlexander Motin extra = val >> 24; 1753831f5dcfSAlexander Motin } 1754677ee494SIan Lepore } 1755831f5dcfSAlexander Motin } else 1756831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1757831f5dcfSAlexander Motin } 1758aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1759a94a63f0SWarner Losh printf("Resp: %02x %02x %02x %02x\n", 1760a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1761a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1762a94a63f0SWarner Losh 1763831f5dcfSAlexander Motin /* If data ready - finish. */ 1764831f5dcfSAlexander Motin if (slot->data_done) 1765831f5dcfSAlexander Motin sdhci_start(slot); 1766831f5dcfSAlexander Motin } 1767831f5dcfSAlexander Motin 1768831f5dcfSAlexander Motin static void 1769831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 1770831f5dcfSAlexander Motin { 1771831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 1772831f5dcfSAlexander Motin uint8_t div; 1773831f5dcfSAlexander Motin 1774831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1775831f5dcfSAlexander Motin slot->data_done = 1; 1776831f5dcfSAlexander Motin return; 1777831f5dcfSAlexander Motin } 1778831f5dcfSAlexander Motin 1779831f5dcfSAlexander Motin slot->data_done = 0; 1780831f5dcfSAlexander Motin 1781831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1782831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1783ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1784ceb9e9f7SIan Lepore div = 0xE; 1785ceb9e9f7SIan Lepore } else { 1786831f5dcfSAlexander Motin target_timeout = 1000000; 1787831f5dcfSAlexander Motin div = 0; 1788831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1789ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1790ceb9e9f7SIan Lepore ++div; 1791831f5dcfSAlexander Motin current_timeout <<= 1; 1792831f5dcfSAlexander Motin } 1793831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1794ceb9e9f7SIan Lepore if (div < 0xE && 1795ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1796ceb9e9f7SIan Lepore ++div; 1797831f5dcfSAlexander Motin } 1798ceb9e9f7SIan Lepore } 1799831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1800831f5dcfSAlexander Motin 1801831f5dcfSAlexander Motin if (data == NULL) 1802831f5dcfSAlexander Motin return; 1803831f5dcfSAlexander Motin 1804831f5dcfSAlexander Motin /* Use DMA if possible. */ 1805831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1806831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1807831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 1808d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1809831f5dcfSAlexander Motin (data->len <= 512)) 1810831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1811831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1812d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1813831f5dcfSAlexander Motin ((data->len) & 0x3)) 1814831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1815831f5dcfSAlexander Motin /* Load DMA buffer. */ 1816831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1817831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1818ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1819ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1820831f5dcfSAlexander Motin else { 1821831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 1822ecc2d997SRui Paulo (data->len < DMA_BLOCK_SIZE) ? 1823ecc2d997SRui Paulo data->len : DMA_BLOCK_SIZE); 1824ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1825ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1826831f5dcfSAlexander Motin } 1827831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1828831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1829831f5dcfSAlexander Motin * for the last page and unmask else. */ 1830831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 1831831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1832831f5dcfSAlexander Motin else 1833831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1834831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1835831f5dcfSAlexander Motin } 1836831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1837831f5dcfSAlexander Motin slot->offset = 0; 1838831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 18391bacf3beSMarius Strobl WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, 18401bacf3beSMarius Strobl (data->len < 512) ? data->len : 512)); 1841831f5dcfSAlexander Motin /* Set block count. */ 1842831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1843a94a63f0SWarner Losh 1844aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 184515c440e1SWarner Losh slot_printf(slot, "Block size: %02x, count %lu\n", 184615c440e1SWarner Losh (unsigned int)SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512) ? data->len : 512), 1847a94a63f0SWarner Losh (unsigned long)(data->len + 511) / 512); 1848831f5dcfSAlexander Motin } 1849831f5dcfSAlexander Motin 1850c3a0f75aSOleksandr Tymoshenko void 1851831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1852831f5dcfSAlexander Motin { 1853831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 18547e6ccea3SMarius Strobl size_t left; 1855831f5dcfSAlexander Motin 1856831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1857ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1858831f5dcfSAlexander Motin * happened first. */ 1859831f5dcfSAlexander Motin if (!slot->cmd_done) { 1860831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1861831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1862831f5dcfSAlexander Motin } 1863831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1864915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1865915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 1866831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 18677e6ccea3SMarius Strobl left = data->len - slot->offset; 1868ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1869ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1870831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1871831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1872831f5dcfSAlexander Motin } else 1873ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1874ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1875831f5dcfSAlexander Motin } 1876a98788edSIan Lepore slot->data_done = 1; 1877831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1878831f5dcfSAlexander Motin if (slot->curcmd->error) { 1879aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1880aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1881831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1882831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1883831f5dcfSAlexander Motin sdhci_start(slot); 1884831f5dcfSAlexander Motin return; 1885831f5dcfSAlexander Motin } 1886831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1887831f5dcfSAlexander Motin if (slot->cmd_done) 1888831f5dcfSAlexander Motin sdhci_start(slot); 1889831f5dcfSAlexander Motin } 1890831f5dcfSAlexander Motin 1891a94a63f0SWarner Losh #ifdef MMCCAM 1892a94a63f0SWarner Losh static void 1893a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 1894a94a63f0SWarner Losh { 1895a94a63f0SWarner Losh union ccb *ccb; 1896a94a63f0SWarner Losh 1897a94a63f0SWarner Losh ccb = slot->ccb; 1898a94a63f0SWarner Losh if (ccb == NULL) 1899a94a63f0SWarner Losh return; 1900a94a63f0SWarner Losh 1901a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1902a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1903a94a63f0SWarner Losh 1904a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 1905a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 1906a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 1907a94a63f0SWarner Losh return; 1908a94a63f0SWarner Losh } 1909a94a63f0SWarner Losh 1910a94a63f0SWarner Losh /* 1911a94a63f0SWarner Losh * Old stack doesn't use this! 1912a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 1913a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 1914a94a63f0SWarner Losh * Not using this code does no harm... 1915a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 1916a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 1917a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 1918a94a63f0SWarner Losh return; 1919a94a63f0SWarner Losh } 1920a94a63f0SWarner Losh */ 1921aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1922a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 1923a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 1924a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1925a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD); 1926a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_DATA); 1927a94a63f0SWarner Losh } 1928a94a63f0SWarner Losh 1929a94a63f0SWarner Losh sdhci_req_done(slot); 1930a94a63f0SWarner Losh } 1931a94a63f0SWarner Losh #else 1932831f5dcfSAlexander Motin static void 1933831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1934831f5dcfSAlexander Motin { 1935831f5dcfSAlexander Motin struct mmc_request *req; 1936831f5dcfSAlexander Motin 1937831f5dcfSAlexander Motin req = slot->req; 1938831f5dcfSAlexander Motin if (req == NULL) 1939831f5dcfSAlexander Motin return; 1940831f5dcfSAlexander Motin 1941831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1942831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1943831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1944831f5dcfSAlexander Motin return; 1945831f5dcfSAlexander Motin } 1946915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 1947915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 1948831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1949831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1950831f5dcfSAlexander Motin return; 1951831f5dcfSAlexander Motin } 1952aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 19535b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 19545b69a497SAlexander Motin if (!req->cmd->error && 1955915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 1956915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 1957915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 1958831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1959831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1960831f5dcfSAlexander Motin } 1961831f5dcfSAlexander Motin 1962e64f01a9SIan Lepore sdhci_req_done(slot); 1963831f5dcfSAlexander Motin } 1964a94a63f0SWarner Losh #endif 1965831f5dcfSAlexander Motin 1966d6b3aaf8SOleksandr Tymoshenko int 1967b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 1968b440e965SMarius Strobl struct mmc_request *req) 1969831f5dcfSAlexander Motin { 1970831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1971831f5dcfSAlexander Motin 1972831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1973831f5dcfSAlexander Motin if (slot->req != NULL) { 1974831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1975831f5dcfSAlexander Motin return (EBUSY); 1976831f5dcfSAlexander Motin } 1977aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 19781bacf3beSMarius Strobl slot_printf(slot, 19791bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1980831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 19815b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 19825b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 19835b69a497SAlexander Motin } 1984831f5dcfSAlexander Motin slot->req = req; 1985831f5dcfSAlexander Motin slot->flags = 0; 1986831f5dcfSAlexander Motin sdhci_start(slot); 1987831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1988bea2dca2SAlexander Motin if (dumping) { 1989bea2dca2SAlexander Motin while (slot->req != NULL) { 1990d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 1991bea2dca2SAlexander Motin DELAY(10); 1992bea2dca2SAlexander Motin } 1993bea2dca2SAlexander Motin } 1994831f5dcfSAlexander Motin return (0); 1995831f5dcfSAlexander Motin } 1996831f5dcfSAlexander Motin 1997d6b3aaf8SOleksandr Tymoshenko int 1998b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 1999831f5dcfSAlexander Motin { 2000831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2001831f5dcfSAlexander Motin uint32_t val; 2002831f5dcfSAlexander Motin 2003831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2004831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2005831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2006831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2007831f5dcfSAlexander Motin } 2008831f5dcfSAlexander Motin 2009d6b3aaf8SOleksandr Tymoshenko int 2010b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2011831f5dcfSAlexander Motin { 2012831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2013831f5dcfSAlexander Motin int err = 0; 2014831f5dcfSAlexander Motin 2015831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2016831f5dcfSAlexander Motin while (slot->bus_busy) 2017d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2018831f5dcfSAlexander Motin slot->bus_busy++; 2019831f5dcfSAlexander Motin /* Activate led. */ 2020831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2021831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2022831f5dcfSAlexander Motin return (err); 2023831f5dcfSAlexander Motin } 2024831f5dcfSAlexander Motin 2025d6b3aaf8SOleksandr Tymoshenko int 2026b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2027831f5dcfSAlexander Motin { 2028831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2029831f5dcfSAlexander Motin 2030831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2031831f5dcfSAlexander Motin /* Deactivate led. */ 2032831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2033831f5dcfSAlexander Motin slot->bus_busy--; 2034831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2035d493985aSAlexander Motin wakeup(slot); 2036831f5dcfSAlexander Motin return (0); 2037831f5dcfSAlexander Motin } 2038831f5dcfSAlexander Motin 2039831f5dcfSAlexander Motin static void 2040831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2041831f5dcfSAlexander Motin { 2042831f5dcfSAlexander Motin 2043831f5dcfSAlexander Motin if (!slot->curcmd) { 2044831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2045831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2046831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2047831f5dcfSAlexander Motin return; 2048831f5dcfSAlexander Motin } 2049831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2050831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2051831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2052831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2053831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2054831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2055831f5dcfSAlexander Motin 2056831f5dcfSAlexander Motin sdhci_finish_command(slot); 2057831f5dcfSAlexander Motin } 2058831f5dcfSAlexander Motin 2059831f5dcfSAlexander Motin static void 2060831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2061831f5dcfSAlexander Motin { 20621bacf3beSMarius Strobl struct mmc_data *data; 206315c440e1SWarner Losh size_t left; 2064831f5dcfSAlexander Motin 2065831f5dcfSAlexander Motin if (!slot->curcmd) { 2066831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2067831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2068831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2069831f5dcfSAlexander Motin return; 2070831f5dcfSAlexander Motin } 2071831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2072831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2073831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2074831f5dcfSAlexander Motin "there is no active data operation.\n", 2075831f5dcfSAlexander Motin intmask); 2076831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2077831f5dcfSAlexander Motin return; 2078831f5dcfSAlexander Motin } 2079831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2080831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2081acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2082831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2083831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2084831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2085831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2086831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2087831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2088831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2089831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2090831f5dcfSAlexander Motin } 2091831f5dcfSAlexander Motin if (slot->curcmd->error) { 2092831f5dcfSAlexander Motin /* No need to continue after any error. */ 2093a98788edSIan Lepore goto done; 2094831f5dcfSAlexander Motin } 2095831f5dcfSAlexander Motin 2096aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2097aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2098aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2099aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2100aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2101aca38eabSMarius Strobl sdhci_finish_command(slot); 2102aca38eabSMarius Strobl sdhci_finish_data(slot); 2103aca38eabSMarius Strobl return; 2104aca38eabSMarius Strobl } 2105831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2106c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2107c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2108c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 21091bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 21101bacf3beSMarius Strobl &intmask); 2111c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2112c3a0f75aSOleksandr Tymoshenko } else 2113831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2114c3a0f75aSOleksandr Tymoshenko } 2115831f5dcfSAlexander Motin /* Handle DMA border. */ 2116831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 21171bacf3beSMarius Strobl data = slot->curcmd->data; 2118831f5dcfSAlexander Motin 2119831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2120831f5dcfSAlexander Motin left = data->len - slot->offset; 2121831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2122831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2123831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2124831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2125831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 2126831f5dcfSAlexander Motin } else { 2127831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2128831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2129831f5dcfSAlexander Motin } 2130831f5dcfSAlexander Motin /* ... and reload it again. */ 2131831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 2132831f5dcfSAlexander Motin left = data->len - slot->offset; 2133831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2134831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2135831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2136831f5dcfSAlexander Motin } else { 2137831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2138831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE); 2139831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2140831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2141831f5dcfSAlexander Motin } 2142831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 2143831f5dcfSAlexander Motin * for the last page. */ 2144831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 2145831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2146831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2147831f5dcfSAlexander Motin } 2148831f5dcfSAlexander Motin /* Restart DMA. */ 2149831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2150831f5dcfSAlexander Motin } 2151831f5dcfSAlexander Motin /* We have got all data. */ 2152c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2153c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2154c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2155c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2156c3a0f75aSOleksandr Tymoshenko } else 2157831f5dcfSAlexander Motin sdhci_finish_data(slot); 2158831f5dcfSAlexander Motin } 2159a98788edSIan Lepore done: 2160a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2161a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2162a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2163a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2164a98788edSIan Lepore } else 2165a98788edSIan Lepore sdhci_finish_data(slot); 2166a98788edSIan Lepore } 2167c3a0f75aSOleksandr Tymoshenko } 2168831f5dcfSAlexander Motin 2169831f5dcfSAlexander Motin static void 21706dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2171831f5dcfSAlexander Motin { 2172831f5dcfSAlexander Motin 2173831f5dcfSAlexander Motin if (!slot->curcmd) { 2174831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 21756dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2176831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2177831f5dcfSAlexander Motin return; 2178831f5dcfSAlexander Motin } 21796dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2180831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2181831f5dcfSAlexander Motin } 2182831f5dcfSAlexander Motin 2183d6b3aaf8SOleksandr Tymoshenko void 2184d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2185831f5dcfSAlexander Motin { 21862b96b955SJustin Hibbits uint32_t intmask, present; 21876dea80e6SMarius Strobl uint16_t val16; 2188831f5dcfSAlexander Motin 2189831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2190831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2191831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2192831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2193831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2194d6b3aaf8SOleksandr Tymoshenko return; 2195831f5dcfSAlexander Motin } 2196aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 21975b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 21985b69a497SAlexander Motin 2199aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2200aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 22016dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2202aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2203aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2204aca38eabSMarius Strobl if (slot->curcmd) { 2205aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2206aca38eabSMarius Strobl sdhci_finish_command(slot); 2207aca38eabSMarius Strobl } 2208aca38eabSMarius Strobl } 2209aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2210aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2211aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2212831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2213831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2214639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 22152b96b955SJustin Hibbits slot->intmask &= 22162b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 22172b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 22182b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 22192b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 22202b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2221831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2222831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2223b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2224831f5dcfSAlexander Motin } 2225831f5dcfSAlexander Motin /* Handle command interrupts. */ 2226831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2227831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2228831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2229831f5dcfSAlexander Motin } 2230831f5dcfSAlexander Motin /* Handle data interrupts. */ 2231831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2232831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 22337e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 22347e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2235831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2236831f5dcfSAlexander Motin } 2237831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2238831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 22396dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 22406dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2241831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 22426dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2243831f5dcfSAlexander Motin } 2244831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2245831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2246831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2247aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2248831f5dcfSAlexander Motin } 2249aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2250aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2251aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2252831f5dcfSAlexander Motin /* The rest is unknown. */ 2253831f5dcfSAlexander Motin if (intmask) { 2254831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2255831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2256831f5dcfSAlexander Motin intmask); 2257831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2258831f5dcfSAlexander Motin } 2259831f5dcfSAlexander Motin 2260831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2261831f5dcfSAlexander Motin } 2262831f5dcfSAlexander Motin 2263d6b3aaf8SOleksandr Tymoshenko int 22641bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 22651bacf3beSMarius Strobl uintptr_t *result) 2266831f5dcfSAlexander Motin { 2267831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2268831f5dcfSAlexander Motin 2269831f5dcfSAlexander Motin switch (which) { 2270831f5dcfSAlexander Motin default: 2271831f5dcfSAlexander Motin return (EINVAL); 2272831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2273bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2274831f5dcfSAlexander Motin break; 2275831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2276bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2277831f5dcfSAlexander Motin break; 2278831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2279bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2280831f5dcfSAlexander Motin break; 2281831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2282bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2283831f5dcfSAlexander Motin break; 2284831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2285bcd91d25SJayachandran C. *result = slot->host.f_min; 2286831f5dcfSAlexander Motin break; 2287831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2288bcd91d25SJayachandran C. *result = slot->host.f_max; 2289831f5dcfSAlexander Motin break; 2290831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2291bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2292831f5dcfSAlexander Motin break; 2293831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2294bcd91d25SJayachandran C. *result = slot->host.mode; 2295831f5dcfSAlexander Motin break; 2296831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2297bcd91d25SJayachandran C. *result = slot->host.ocr; 2298831f5dcfSAlexander Motin break; 2299831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2300bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2301831f5dcfSAlexander Motin break; 2302831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2303bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2304831f5dcfSAlexander Motin break; 2305aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2306aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2307aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2308aca38eabSMarius Strobl *result = retune_req_reset; 2309aca38eabSMarius Strobl break; 2310aca38eabSMarius Strobl } 2311aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2312aca38eabSMarius Strobl *result = retune_req_normal; 2313aca38eabSMarius Strobl break; 2314aca38eabSMarius Strobl } 2315aca38eabSMarius Strobl } 2316aca38eabSMarius Strobl *result = retune_req_none; 2317aca38eabSMarius Strobl break; 23180f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 23190f34084fSMarius Strobl *result = slot->host.ios.vccq; 23200f34084fSMarius Strobl break; 2321831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2322bcd91d25SJayachandran C. *result = slot->host.caps; 2323831f5dcfSAlexander Motin break; 2324831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2325bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2326831f5dcfSAlexander Motin break; 23273a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2328aca38eabSMarius Strobl /* 2329aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2330aca38eabSMarius Strobl * per read/write command to 4 MiB. 2331aca38eabSMarius Strobl */ 2332aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2333aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2334aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2335aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2336aca38eabSMarius Strobl break; 2337aca38eabSMarius Strobl } 2338bcd91d25SJayachandran C. *result = 65535; 23393a4a2557SAlexander Motin break; 234072dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 234172dec079SMarius Strobl /* 234272dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 234372dec079SMarius Strobl */ 234472dec079SMarius Strobl *result = 1000000; 234572dec079SMarius Strobl break; 2346831f5dcfSAlexander Motin } 2347831f5dcfSAlexander Motin return (0); 2348831f5dcfSAlexander Motin } 2349831f5dcfSAlexander Motin 2350d6b3aaf8SOleksandr Tymoshenko int 23511bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 23521bacf3beSMarius Strobl uintptr_t value) 2353831f5dcfSAlexander Motin { 2354831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2355b440e965SMarius Strobl uint32_t clock, max_clock; 2356b440e965SMarius Strobl int i; 2357831f5dcfSAlexander Motin 235815c440e1SWarner Losh if (sdhci_debug > 1) 235915c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2360831f5dcfSAlexander Motin switch (which) { 2361831f5dcfSAlexander Motin default: 2362831f5dcfSAlexander Motin return (EINVAL); 2363831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2364831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2365831f5dcfSAlexander Motin break; 2366831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2367831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2368831f5dcfSAlexander Motin break; 2369831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2370831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2371831f5dcfSAlexander Motin break; 2372831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2373831f5dcfSAlexander Motin if (value > 0) { 237457677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 237557677a3aSOleksandr Tymoshenko clock = max_clock; 237657677a3aSOleksandr Tymoshenko 237757677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 237857677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 237957677a3aSOleksandr Tymoshenko i <<= 1) { 2380831f5dcfSAlexander Motin if (clock <= value) 2381831f5dcfSAlexander Motin break; 2382831f5dcfSAlexander Motin clock >>= 1; 2383831f5dcfSAlexander Motin } 2384b440e965SMarius Strobl } else { 238557677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 238657677a3aSOleksandr Tymoshenko i += 2) { 238757677a3aSOleksandr Tymoshenko if (clock <= value) 238857677a3aSOleksandr Tymoshenko break; 238957677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 239057677a3aSOleksandr Tymoshenko } 239157677a3aSOleksandr Tymoshenko } 239257677a3aSOleksandr Tymoshenko 2393831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2394831f5dcfSAlexander Motin } else 2395831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2396831f5dcfSAlexander Motin break; 2397831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2398831f5dcfSAlexander Motin slot->host.mode = value; 2399831f5dcfSAlexander Motin break; 2400831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2401831f5dcfSAlexander Motin slot->host.ocr = value; 2402831f5dcfSAlexander Motin break; 2403831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2404831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2405831f5dcfSAlexander Motin break; 2406831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2407831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2408831f5dcfSAlexander Motin break; 24090f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24100f34084fSMarius Strobl slot->host.ios.vccq = value; 24110f34084fSMarius Strobl break; 2412831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2413831f5dcfSAlexander Motin slot->host.ios.timing = value; 2414831f5dcfSAlexander Motin break; 2415831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2416831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2417831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2418831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 24193a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2420aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2421831f5dcfSAlexander Motin return (EINVAL); 2422831f5dcfSAlexander Motin } 2423831f5dcfSAlexander Motin return (0); 2424831f5dcfSAlexander Motin } 2425831f5dcfSAlexander Motin 242615c440e1SWarner Losh #ifdef MMCCAM 2427a94a63f0SWarner Losh void 2428d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2429a94a63f0SWarner Losh { 2430a94a63f0SWarner Losh if ((slot->devq = cam_simq_alloc(1)) == NULL) { 2431a94a63f0SWarner Losh goto fail; 2432a94a63f0SWarner Losh } 2433a94a63f0SWarner Losh 2434a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2435a94a63f0SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2436a94a63f0SWarner Losh "sdhci_slot", slot, device_get_unit(slot->bus), 2437a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2438a94a63f0SWarner Losh 2439a94a63f0SWarner Losh if (slot->sim == NULL) { 2440a94a63f0SWarner Losh cam_simq_free(slot->devq); 2441a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2442a94a63f0SWarner Losh goto fail; 2443a94a63f0SWarner Losh } 2444a94a63f0SWarner Losh 2445a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2446a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2447a94a63f0SWarner Losh slot_printf(slot, 2448a94a63f0SWarner Losh "cannot register SCSI pass-through bus\n"); 2449a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2450a94a63f0SWarner Losh cam_simq_free(slot->devq); 2451a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2452a94a63f0SWarner Losh goto fail; 2453a94a63f0SWarner Losh } 2454a94a63f0SWarner Losh 2455a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2456a94a63f0SWarner Losh /* End CAM-specific init */ 2457a94a63f0SWarner Losh slot->card_present = 0; 2458a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2459a94a63f0SWarner Losh return; 2460a94a63f0SWarner Losh 2461a94a63f0SWarner Losh fail: 2462a94a63f0SWarner Losh if (slot->sim != NULL) { 2463a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2464a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2465a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2466a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2467a94a63f0SWarner Losh } 2468a94a63f0SWarner Losh 2469a94a63f0SWarner Losh if (slot->devq != NULL) 2470a94a63f0SWarner Losh cam_simq_free(slot->devq); 2471a94a63f0SWarner Losh } 2472a94a63f0SWarner Losh 2473a94a63f0SWarner Losh static void 2474a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb) 2475a94a63f0SWarner Losh { 2476a94a63f0SWarner Losh struct sdhci_slot *slot; 2477a94a63f0SWarner Losh 2478a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2479a94a63f0SWarner Losh 2480a94a63f0SWarner Losh sdhci_cam_request(slot, ccb); 2481a94a63f0SWarner Losh } 2482a94a63f0SWarner Losh 2483a94a63f0SWarner Losh void 2484a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2485a94a63f0SWarner Losh { 2486a94a63f0SWarner Losh struct sdhci_slot *slot; 2487a94a63f0SWarner Losh 2488a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2489a94a63f0SWarner Losh if (slot == NULL) { 2490a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2491a94a63f0SWarner Losh xpt_done(ccb); 2492a94a63f0SWarner Losh return; 2493a94a63f0SWarner Losh } 2494a94a63f0SWarner Losh 2495a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2496a94a63f0SWarner Losh 2497a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2498a94a63f0SWarner Losh case XPT_PATH_INQ: 2499a94a63f0SWarner Losh { 2500a94a63f0SWarner Losh struct ccb_pathinq *cpi; 2501a94a63f0SWarner Losh 2502a94a63f0SWarner Losh cpi = &ccb->cpi; 2503a94a63f0SWarner Losh cpi->version_num = 1; 2504a94a63f0SWarner Losh cpi->hba_inquiry = 0; 2505a94a63f0SWarner Losh cpi->target_sprt = 0; 2506a94a63f0SWarner Losh cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 2507a94a63f0SWarner Losh cpi->hba_eng_cnt = 0; 2508a94a63f0SWarner Losh cpi->max_target = 0; 2509a94a63f0SWarner Losh cpi->max_lun = 0; 2510a94a63f0SWarner Losh cpi->initiator_id = 1; 2511a94a63f0SWarner Losh cpi->maxio = MAXPHYS; 2512a94a63f0SWarner Losh strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2513a94a63f0SWarner Losh strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN); 2514a94a63f0SWarner Losh strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2515a94a63f0SWarner Losh cpi->unit_number = cam_sim_unit(sim); 2516a94a63f0SWarner Losh cpi->bus_id = cam_sim_bus(sim); 2517a94a63f0SWarner Losh cpi->base_transfer_speed = 100; /* XXX WTF? */ 2518a94a63f0SWarner Losh cpi->protocol = PROTO_MMCSD; 2519a94a63f0SWarner Losh cpi->protocol_version = SCSI_REV_0; 2520a94a63f0SWarner Losh cpi->transport = XPORT_MMCSD; 2521a94a63f0SWarner Losh cpi->transport_version = 0; 2522a94a63f0SWarner Losh 2523a94a63f0SWarner Losh cpi->ccb_h.status = CAM_REQ_CMP; 2524a94a63f0SWarner Losh break; 2525a94a63f0SWarner Losh } 2526a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2527a94a63f0SWarner Losh { 2528a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 2529a94a63f0SWarner Losh 2530a94a63f0SWarner Losh if (sdhci_debug > 1) 2531a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2532a94a63f0SWarner Losh 2533a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2534a94a63f0SWarner Losh cts->protocol_version = 1; 2535a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2536a94a63f0SWarner Losh cts->transport_version = 1; 2537a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2538a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2539a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2540a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2541a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 2542a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2543a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2544a94a63f0SWarner Losh break; 2545a94a63f0SWarner Losh } 2546a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2547a94a63f0SWarner Losh { 2548a94a63f0SWarner Losh if (sdhci_debug > 1) 2549a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2550a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2551a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2552a94a63f0SWarner Losh break; 2553a94a63f0SWarner Losh } 2554a94a63f0SWarner Losh case XPT_RESET_BUS: 2555a94a63f0SWarner Losh if (sdhci_debug > 1) 2556a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2557a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2558a94a63f0SWarner Losh break; 2559a94a63f0SWarner Losh case XPT_MMC_IO: 2560a94a63f0SWarner Losh /* 2561a94a63f0SWarner Losh * Here is the HW-dependent part of 2562a94a63f0SWarner Losh * sending the command to the underlying h/w 2563a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2564a94a63f0SWarner Losh * Then the request will be marked as completed. 2565a94a63f0SWarner Losh */ 2566aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2567a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2568a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2569a94a63f0SWarner Losh 2570a94a63f0SWarner Losh sdhci_cam_handle_mmcio(sim, ccb); 2571a94a63f0SWarner Losh return; 2572a94a63f0SWarner Losh /* NOTREACHED */ 2573a94a63f0SWarner Losh break; 2574a94a63f0SWarner Losh default: 2575a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2576a94a63f0SWarner Losh break; 2577a94a63f0SWarner Losh } 2578a94a63f0SWarner Losh xpt_done(ccb); 2579a94a63f0SWarner Losh return; 2580a94a63f0SWarner Losh } 2581a94a63f0SWarner Losh 2582a94a63f0SWarner Losh void 2583a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2584a94a63f0SWarner Losh { 2585a94a63f0SWarner Losh return; 2586a94a63f0SWarner Losh } 2587a94a63f0SWarner Losh 25886dea80e6SMarius Strobl static int 25896dea80e6SMarius Strobl sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) 25906dea80e6SMarius Strobl { 2591a94a63f0SWarner Losh int max_clock, clock, i; 2592a94a63f0SWarner Losh 2593a94a63f0SWarner Losh if (proposed_clock == 0) 2594a94a63f0SWarner Losh return 0; 2595a94a63f0SWarner Losh max_clock = slot->max_clk; 2596a94a63f0SWarner Losh clock = max_clock; 2597a94a63f0SWarner Losh 2598a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2599a94a63f0SWarner Losh for (i = 0; i < SDHCI_200_MAX_DIVIDER; 2600a94a63f0SWarner Losh i <<= 1) { 2601a94a63f0SWarner Losh if (clock <= proposed_clock) 2602a94a63f0SWarner Losh break; 2603a94a63f0SWarner Losh clock >>= 1; 2604a94a63f0SWarner Losh } 2605a94a63f0SWarner Losh } else { 2606a94a63f0SWarner Losh for (i = 0; i < SDHCI_300_MAX_DIVIDER; 2607a94a63f0SWarner Losh i += 2) { 2608a94a63f0SWarner Losh if (clock <= proposed_clock) 2609a94a63f0SWarner Losh break; 2610a94a63f0SWarner Losh clock = max_clock / (i + 2); 2611a94a63f0SWarner Losh } 2612a94a63f0SWarner Losh } 2613a94a63f0SWarner Losh return clock; 2614a94a63f0SWarner Losh } 2615a94a63f0SWarner Losh 2616a94a63f0SWarner Losh int 2617a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2618a94a63f0SWarner Losh { 2619a94a63f0SWarner Losh struct mmc_ios *ios; 2620a94a63f0SWarner Losh struct mmc_ios *new_ios; 2621a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts; 2622a94a63f0SWarner Losh 2623a94a63f0SWarner Losh ios = &slot->host.ios; 2624a94a63f0SWarner Losh 2625a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2626a94a63f0SWarner Losh new_ios = &cts->ios; 2627a94a63f0SWarner Losh 2628a94a63f0SWarner Losh /* Update only requested fields */ 2629a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2630a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2631a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2632a94a63f0SWarner Losh } 2633a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2634a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2635a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2636a94a63f0SWarner Losh } 2637a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2638a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2639a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2640a94a63f0SWarner Losh } 2641a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2642a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2643a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2644a94a63f0SWarner Losh } 2645a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2646a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2647a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2648a94a63f0SWarner Losh } 2649a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2650a94a63f0SWarner Losh ios->timing = new_ios->timing; 2651a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2652a94a63f0SWarner Losh } 2653a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2654a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2655a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2656a94a63f0SWarner Losh } 2657a94a63f0SWarner Losh 2658a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2659a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2660a94a63f0SWarner Losh } 2661a94a63f0SWarner Losh 2662a94a63f0SWarner Losh int 2663a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2664a94a63f0SWarner Losh { 2665a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2666a94a63f0SWarner Losh 2667a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2668a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2669a94a63f0SWarner Losh SDHCI_LOCK(slot); 2670a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2671a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2672a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2673a94a63f0SWarner Losh sdhci_init(slot); 2674a94a63f0SWarner Losh } 2675a94a63f0SWarner Losh /* Configure the bus. */ 2676a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2677a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2678a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2679a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2680a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2681a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2682a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2683a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2684a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2685a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2686a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2687a94a63f0SWarner Losh } else { 2688a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2689a94a63f0SWarner Losh } 2690a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2691a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2692a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2693a94a63f0SWarner Losh else 2694a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2695a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2696a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2697a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2698a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2699a94a63f0SWarner Losh 2700a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2701a94a63f0SWarner Losh return (0); 2702a94a63f0SWarner Losh } 2703a94a63f0SWarner Losh 2704a94a63f0SWarner Losh int 2705a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2706a94a63f0SWarner Losh { 2707a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 2708a94a63f0SWarner Losh 2709a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2710a94a63f0SWarner Losh 2711a94a63f0SWarner Losh SDHCI_LOCK(slot); 2712a94a63f0SWarner Losh /* if (slot->req != NULL) { 2713a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2714a94a63f0SWarner Losh return (EBUSY); 2715a94a63f0SWarner Losh } 2716a94a63f0SWarner Losh */ 2717aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 2718a94a63f0SWarner Losh slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2719a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2720a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2721a94a63f0SWarner Losh mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 2722a94a63f0SWarner Losh } 2723a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2724a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2725a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2726a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2727a94a63f0SWarner Losh } 2728a94a63f0SWarner Losh slot->ccb = ccb; 2729a94a63f0SWarner Losh slot->flags = 0; 2730a94a63f0SWarner Losh sdhci_start(slot); 2731a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2732a94a63f0SWarner Losh if (dumping) { 2733a94a63f0SWarner Losh while (slot->ccb != NULL) { 2734a94a63f0SWarner Losh sdhci_generic_intr(slot); 2735a94a63f0SWarner Losh DELAY(10); 2736a94a63f0SWarner Losh } 2737a94a63f0SWarner Losh } 2738a94a63f0SWarner Losh return (0); 2739a94a63f0SWarner Losh } 274015c440e1SWarner Losh #endif /* MMCCAM */ 2741a94a63f0SWarner Losh 2742d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1); 2743