1831f5dcfSAlexander Motin /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 4831f5dcfSAlexander Motin * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org> 5aca38eabSMarius Strobl * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 6831f5dcfSAlexander Motin * All rights reserved. 7831f5dcfSAlexander Motin * 8831f5dcfSAlexander Motin * Redistribution and use in source and binary forms, with or without 9831f5dcfSAlexander Motin * modification, are permitted provided that the following conditions 10831f5dcfSAlexander Motin * are met: 11831f5dcfSAlexander Motin * 1. Redistributions of source code must retain the above copyright 12831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer. 13831f5dcfSAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 14831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer in the 15831f5dcfSAlexander Motin * documentation and/or other materials provided with the distribution. 16831f5dcfSAlexander Motin * 17831f5dcfSAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18831f5dcfSAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19831f5dcfSAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20831f5dcfSAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21831f5dcfSAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22831f5dcfSAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23831f5dcfSAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24831f5dcfSAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25831f5dcfSAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26831f5dcfSAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27831f5dcfSAlexander Motin */ 28831f5dcfSAlexander Motin 29831f5dcfSAlexander Motin #include <sys/cdefs.h> 30831f5dcfSAlexander Motin __FBSDID("$FreeBSD$"); 31831f5dcfSAlexander Motin 32831f5dcfSAlexander Motin #include <sys/param.h> 33831f5dcfSAlexander Motin #include <sys/systm.h> 34831f5dcfSAlexander Motin #include <sys/bus.h> 35e64f01a9SIan Lepore #include <sys/callout.h> 36831f5dcfSAlexander Motin #include <sys/conf.h> 37831f5dcfSAlexander Motin #include <sys/kernel.h> 38aca38eabSMarius Strobl #include <sys/kobj.h> 39ab00a509SMarius Strobl #include <sys/libkern.h> 40831f5dcfSAlexander Motin #include <sys/lock.h> 41aca38eabSMarius Strobl #include <sys/malloc.h> 42831f5dcfSAlexander Motin #include <sys/module.h> 43831f5dcfSAlexander Motin #include <sys/mutex.h> 44831f5dcfSAlexander Motin #include <sys/resource.h> 45831f5dcfSAlexander Motin #include <sys/rman.h> 465b69a497SAlexander Motin #include <sys/sysctl.h> 47831f5dcfSAlexander Motin #include <sys/taskqueue.h> 48831f5dcfSAlexander Motin 49831f5dcfSAlexander Motin #include <machine/bus.h> 50831f5dcfSAlexander Motin #include <machine/resource.h> 51831f5dcfSAlexander Motin #include <machine/stdarg.h> 52831f5dcfSAlexander Motin 53831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 54831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 55831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 56831f5dcfSAlexander Motin 57aca38eabSMarius Strobl #include <dev/sdhci/sdhci.h> 58aca38eabSMarius Strobl 59a94a63f0SWarner Losh #include <cam/cam.h> 60a94a63f0SWarner Losh #include <cam/cam_ccb.h> 61a94a63f0SWarner Losh #include <cam/cam_debug.h> 62a94a63f0SWarner Losh #include <cam/cam_sim.h> 63a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 64a94a63f0SWarner Losh 65831f5dcfSAlexander Motin #include "mmcbr_if.h" 66d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 67831f5dcfSAlexander Motin 68a94a63f0SWarner Losh #include "opt_mmccam.h" 69a94a63f0SWarner Losh 70*7029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 71*7029da5cSPawel Biernacki "sdhci driver"); 72831f5dcfSAlexander Motin 73a94a63f0SWarner Losh static int sdhci_debug = 0; 747e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 757e6ccea3SMarius Strobl "Debug level"); 760f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 770f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 780f34084fSMarius Strobl 0, "Mask of quirks to clear"); 790f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 800f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 810f34084fSMarius Strobl "Mask of quirks to set"); 825b69a497SAlexander Motin 83d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 84d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 85d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 86d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 87d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 88831f5dcfSAlexander Motin 89d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 90d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 91d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 92d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 93d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 94831f5dcfSAlexander Motin 956dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err); 96aca38eabSMarius Strobl static void sdhci_card_poll(void *arg); 97aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending); 986dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); 996dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); 100aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 1016dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, 1026dea80e6SMarius Strobl bool is_present); 1036dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot); 1046dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot); 1056dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot); 1066dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot); 107aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req); 1086dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask); 109aca38eabSMarius Strobl static void sdhci_retune(void *arg); 110831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 1116dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power); 1126dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot, 113ab00a509SMarius Strobl const struct mmc_data *data); 114831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 1156dea80e6SMarius Strobl static void sdhci_timeout(void *arg); 1166dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot, 1176dea80e6SMarius Strobl struct mmc_command *cmd); 118ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot, 119ab00a509SMarius Strobl const struct mmc_data *data); 1206dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot); 1216dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot); 122831f5dcfSAlexander Motin 12315c440e1SWarner Losh #ifdef MMCCAM 124a94a63f0SWarner Losh /* CAM-related */ 125a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 126ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 1276dea80e6SMarius Strobl int proposed_clock); 128a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 1296dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 130a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 1316dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot); 13215c440e1SWarner Losh #endif 133a94a63f0SWarner Losh 134831f5dcfSAlexander Motin /* helper routines */ 135ab00a509SMarius Strobl static int sdhci_dma_alloc(struct sdhci_slot *slot); 136ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot); 1370f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 1386dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, 1396dea80e6SMarius Strobl int error); 140ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 1410f34084fSMarius Strobl __printflike(2, 3); 142ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot); 1430f34084fSMarius Strobl 144831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 145831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 146831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 147831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 148831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 149831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 150831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 151831f5dcfSAlexander Motin 15233aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 15333aad34dSOleksandr Tymoshenko 15457677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 15557677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 15657677a3aSOleksandr Tymoshenko 157639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 158639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 159639f59f0SIan Lepore 16093efdc63SAdrian Chadd /* 16193efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 16293efdc63SAdrian Chadd */ 1631bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1641bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1651bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1661bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 16793efdc63SAdrian Chadd 16893efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 16993efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 17093efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 17193efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 17293efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 17393efdc63SAdrian Chadd 174831f5dcfSAlexander Motin static void 175831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 176831f5dcfSAlexander Motin { 1777e6ccea3SMarius Strobl 178831f5dcfSAlexander Motin if (error != 0) { 179831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 180831f5dcfSAlexander Motin return; 181831f5dcfSAlexander Motin } 182831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 183831f5dcfSAlexander Motin } 184831f5dcfSAlexander Motin 185d6b3aaf8SOleksandr Tymoshenko static int 186ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 187d6b3aaf8SOleksandr Tymoshenko { 18827d72fe1SBjoern A. Zeeb char buf[128]; 189d6b3aaf8SOleksandr Tymoshenko va_list ap; 190d6b3aaf8SOleksandr Tymoshenko int retval; 191d6b3aaf8SOleksandr Tymoshenko 19227d72fe1SBjoern A. Zeeb /* 19327d72fe1SBjoern A. Zeeb * Make sure we print a single line all together rather than in two 19427d72fe1SBjoern A. Zeeb * halves to avoid console gibberish bingo. 19527d72fe1SBjoern A. Zeeb */ 196d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 19727d72fe1SBjoern A. Zeeb retval = vsnprintf(buf, sizeof(buf), fmt, ap); 198d6b3aaf8SOleksandr Tymoshenko va_end(ap); 19927d72fe1SBjoern A. Zeeb 20027d72fe1SBjoern A. Zeeb retval += printf("%s-slot%d: %s", 20127d72fe1SBjoern A. Zeeb device_get_nameunit(slot->bus), slot->num, buf); 202d6b3aaf8SOleksandr Tymoshenko return (retval); 203d6b3aaf8SOleksandr Tymoshenko } 204d6b3aaf8SOleksandr Tymoshenko 205831f5dcfSAlexander Motin static void 206831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 207831f5dcfSAlexander Motin { 2087e6ccea3SMarius Strobl 209831f5dcfSAlexander Motin slot_printf(slot, 210831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 211831f5dcfSAlexander Motin 212831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 213831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 214831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 215831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 216831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 217831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 218831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 219831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 220831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 221831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 222831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 223831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 224831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 225831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 226831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 227831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 2289dbf8c46SMarius Strobl slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 2299dbf8c46SMarius Strobl RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 2309dbf8c46SMarius Strobl slot_printf(slot, "Caps: 0x%08x | Caps2: 0x%08x\n", 2319dbf8c46SMarius Strobl RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 2329dbf8c46SMarius Strobl slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 2339dbf8c46SMarius Strobl RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 2349dbf8c46SMarius Strobl slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 2359dbf8c46SMarius Strobl RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 236831f5dcfSAlexander Motin 237831f5dcfSAlexander Motin slot_printf(slot, 238831f5dcfSAlexander Motin "===========================================\n"); 239831f5dcfSAlexander Motin } 240831f5dcfSAlexander Motin 241831f5dcfSAlexander Motin static void 242831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 243831f5dcfSAlexander Motin { 244831f5dcfSAlexander Motin int timeout; 245b440e965SMarius Strobl uint32_t clock; 246831f5dcfSAlexander Motin 247d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 2486e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 249831f5dcfSAlexander Motin return; 250831f5dcfSAlexander Motin } 251831f5dcfSAlexander Motin 252831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 253831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 254d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 255831f5dcfSAlexander Motin /* This is to force an update */ 256831f5dcfSAlexander Motin clock = slot->clock; 257831f5dcfSAlexander Motin slot->clock = 0; 258831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 259831f5dcfSAlexander Motin } 260831f5dcfSAlexander Motin 261d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 262831f5dcfSAlexander Motin slot->clock = 0; 263d8208d9eSAlexander Motin slot->power = 0; 264d8208d9eSAlexander Motin } 265831f5dcfSAlexander Motin 26661bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 26761bc42f7SIan Lepore 26861bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 26961bc42f7SIan Lepore /* 27061bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 27161bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 27261bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 27361bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 27461bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 27561bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 27661bc42f7SIan Lepore */ 27761bc42f7SIan Lepore timeout = 10000; 27861bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 27961bc42f7SIan Lepore if (timeout <= 0) 28061bc42f7SIan Lepore break; 28161bc42f7SIan Lepore timeout--; 28261bc42f7SIan Lepore DELAY(1); 28361bc42f7SIan Lepore } 28461bc42f7SIan Lepore } 28561bc42f7SIan Lepore 286831f5dcfSAlexander Motin /* Wait max 100 ms */ 28761bc42f7SIan Lepore timeout = 10000; 288831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 28961bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 29061bc42f7SIan Lepore if (timeout <= 0) { 29161bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 29261bc42f7SIan Lepore mask); 293831f5dcfSAlexander Motin sdhci_dumpregs(slot); 294831f5dcfSAlexander Motin return; 295831f5dcfSAlexander Motin } 296831f5dcfSAlexander Motin timeout--; 29761bc42f7SIan Lepore DELAY(10); 298831f5dcfSAlexander Motin } 299831f5dcfSAlexander Motin } 300831f5dcfSAlexander Motin 301aca38eabSMarius Strobl static uint32_t 302ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot) 303aca38eabSMarius Strobl { 304aca38eabSMarius Strobl uint32_t intmask; 305aca38eabSMarius Strobl 306aca38eabSMarius Strobl intmask = 0; 30778f8baa8SMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 308aca38eabSMarius Strobl intmask |= SDHCI_INT_TUNEERR; 309aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 310aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_3) 311aca38eabSMarius Strobl intmask |= SDHCI_INT_RETUNE; 312aca38eabSMarius Strobl } 313aca38eabSMarius Strobl return (intmask); 314aca38eabSMarius Strobl } 315aca38eabSMarius Strobl 316831f5dcfSAlexander Motin static void 317831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 318831f5dcfSAlexander Motin { 319831f5dcfSAlexander Motin 320831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 321831f5dcfSAlexander Motin 322831f5dcfSAlexander Motin /* Enable interrupts. */ 323831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 324831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 325831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 326831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 327831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 3283685b398SWarner Losh SDHCI_INT_ACMD12ERR; 329639f59f0SIan Lepore 330639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 331639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 332639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 333639f59f0SIan Lepore } 334639f59f0SIan Lepore 335cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 336831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 337831f5dcfSAlexander Motin } 338831f5dcfSAlexander Motin 339831f5dcfSAlexander Motin static void 340831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 341831f5dcfSAlexander Motin { 34293efdc63SAdrian Chadd uint32_t clk_base; 34393efdc63SAdrian Chadd uint32_t clk_sel; 344831f5dcfSAlexander Motin uint32_t res; 345831f5dcfSAlexander Motin uint16_t clk; 3468f3b7d56SOleksandr Tymoshenko uint16_t div; 347831f5dcfSAlexander Motin int timeout; 348831f5dcfSAlexander Motin 349831f5dcfSAlexander Motin if (clock == slot->clock) 350831f5dcfSAlexander Motin return; 351831f5dcfSAlexander Motin slot->clock = clock; 352831f5dcfSAlexander Motin 353831f5dcfSAlexander Motin /* Turn off the clock. */ 3544ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 3554ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 356b440e965SMarius Strobl /* If no clock requested - leave it so. */ 357831f5dcfSAlexander Motin if (clock == 0) 358831f5dcfSAlexander Motin return; 359ceb9e9f7SIan Lepore 36093efdc63SAdrian Chadd /* Determine the clock base frequency */ 36193efdc63SAdrian Chadd clk_base = slot->max_clk; 36293efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 3631bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 3641bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 36593efdc63SAdrian Chadd 3661bacf3beSMarius Strobl /* 3671bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 3681bacf3beSMarius Strobl */ 36993efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 37093efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 3711bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 3721bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 37393efdc63SAdrian Chadd } else { 3741bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 3751bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 37693efdc63SAdrian Chadd } 37793efdc63SAdrian Chadd 37893efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 37993efdc63SAdrian Chadd } 38093efdc63SAdrian Chadd 381ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 382ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 383ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 384ceb9e9f7SIan Lepore 3858f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 386831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 38793efdc63SAdrian Chadd res = clk_base; 38857677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 389831f5dcfSAlexander Motin if (res <= clock) 390831f5dcfSAlexander Motin break; 391831f5dcfSAlexander Motin res >>= 1; 392831f5dcfSAlexander Motin } 393831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 3948f3b7d56SOleksandr Tymoshenko div >>= 1; 395c11bbc7dSMarius Strobl } else { 3968f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 39793efdc63SAdrian Chadd if (clock >= clk_base) 39857677a3aSOleksandr Tymoshenko div = 0; 3998f3b7d56SOleksandr Tymoshenko else { 40057677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 40193efdc63SAdrian Chadd if ((clk_base / div) <= clock) 4028f3b7d56SOleksandr Tymoshenko break; 4038f3b7d56SOleksandr Tymoshenko } 4048f3b7d56SOleksandr Tymoshenko } 4058f3b7d56SOleksandr Tymoshenko div >>= 1; 4068f3b7d56SOleksandr Tymoshenko } 4078f3b7d56SOleksandr Tymoshenko 4088f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 40993efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 41093efdc63SAdrian Chadd div, clock, clk_base); 4118f3b7d56SOleksandr Tymoshenko 412831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4138f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4148f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4158f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4168f3b7d56SOleksandr Tymoshenko 417831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 418831f5dcfSAlexander Motin /* Enable clock. */ 419831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 420831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 421831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 422831f5dcfSAlexander Motin timeout = 10; 423831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 424831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 425831f5dcfSAlexander Motin if (timeout == 0) { 426831f5dcfSAlexander Motin slot_printf(slot, 427831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 428831f5dcfSAlexander Motin sdhci_dumpregs(slot); 429831f5dcfSAlexander Motin return; 430831f5dcfSAlexander Motin } 431831f5dcfSAlexander Motin timeout--; 432831f5dcfSAlexander Motin DELAY(1000); 433831f5dcfSAlexander Motin } 434831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 435831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 436831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 437831f5dcfSAlexander Motin } 438831f5dcfSAlexander Motin 439831f5dcfSAlexander Motin static void 440831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 441831f5dcfSAlexander Motin { 44285083a80SMarius Strobl int i; 443831f5dcfSAlexander Motin uint8_t pwr; 444831f5dcfSAlexander Motin 445831f5dcfSAlexander Motin if (slot->power == power) 446831f5dcfSAlexander Motin return; 447d6b3aaf8SOleksandr Tymoshenko 448831f5dcfSAlexander Motin slot->power = power; 449831f5dcfSAlexander Motin 450831f5dcfSAlexander Motin /* Turn off the power. */ 451831f5dcfSAlexander Motin pwr = 0; 452831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 453b440e965SMarius Strobl /* If power down requested - leave it so. */ 454831f5dcfSAlexander Motin if (power == 0) 455831f5dcfSAlexander Motin return; 456831f5dcfSAlexander Motin /* Set voltage. */ 457831f5dcfSAlexander Motin switch (1 << power) { 458831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 459831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 460831f5dcfSAlexander Motin break; 461831f5dcfSAlexander Motin case MMC_OCR_290_300: 462831f5dcfSAlexander Motin case MMC_OCR_300_310: 463831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 464831f5dcfSAlexander Motin break; 465831f5dcfSAlexander Motin case MMC_OCR_320_330: 466831f5dcfSAlexander Motin case MMC_OCR_330_340: 467831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 468831f5dcfSAlexander Motin break; 469831f5dcfSAlexander Motin } 470831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 47185083a80SMarius Strobl /* 47285083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 47385083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 4748022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 47585083a80SMarius Strobl */ 476831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 47785083a80SMarius Strobl for (i = 0; i < 20; i++) { 478831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 47985083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 48085083a80SMarius Strobl break; 48185083a80SMarius Strobl DELAY(100); 48285083a80SMarius Strobl } 48385083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 4844d52f81dSIan Lepore slot_printf(slot, "Bus power failed to enable\n"); 485a2832f9fSMarius Strobl 486a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 487a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 488a2832f9fSMarius Strobl DELAY(10); 489a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 490a2832f9fSMarius Strobl DELAY(300); 491a2832f9fSMarius Strobl } 492831f5dcfSAlexander Motin } 493831f5dcfSAlexander Motin 494831f5dcfSAlexander Motin static void 495831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 496831f5dcfSAlexander Motin { 497831f5dcfSAlexander Motin uint32_t data; 498831f5dcfSAlexander Motin char *buffer; 499831f5dcfSAlexander Motin size_t left; 500831f5dcfSAlexander Motin 501831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 502831f5dcfSAlexander Motin buffer += slot->offset; 503831f5dcfSAlexander Motin /* Transfer one block at a time. */ 5045d5ae066SIlya Bakulin #ifdef MMCCAM 5055d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) 5065d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 5075d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 5085d5ae066SIlya Bakulin else 5095d5ae066SIlya Bakulin #endif 510831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 511831f5dcfSAlexander Motin slot->offset += left; 512831f5dcfSAlexander Motin 513831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 514d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 515831f5dcfSAlexander Motin DELAY(10); 516ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 517831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 518831f5dcfSAlexander Motin while (left > 3) { 519831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 520831f5dcfSAlexander Motin buffer[0] = data; 521831f5dcfSAlexander Motin buffer[1] = (data >> 8); 522831f5dcfSAlexander Motin buffer[2] = (data >> 16); 523831f5dcfSAlexander Motin buffer[3] = (data >> 24); 524831f5dcfSAlexander Motin buffer += 4; 525831f5dcfSAlexander Motin left -= 4; 526831f5dcfSAlexander Motin } 527831f5dcfSAlexander Motin } else { 528d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 529831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 530831f5dcfSAlexander Motin left &= 3; 531831f5dcfSAlexander Motin } 532831f5dcfSAlexander Motin /* Handle uneven size case. */ 533831f5dcfSAlexander Motin if (left > 0) { 534831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 535831f5dcfSAlexander Motin while (left > 0) { 536831f5dcfSAlexander Motin *(buffer++) = data; 537831f5dcfSAlexander Motin data >>= 8; 538831f5dcfSAlexander Motin left--; 539831f5dcfSAlexander Motin } 540831f5dcfSAlexander Motin } 541831f5dcfSAlexander Motin } 542831f5dcfSAlexander Motin 543831f5dcfSAlexander Motin static void 544831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 545831f5dcfSAlexander Motin { 546831f5dcfSAlexander Motin uint32_t data = 0; 547831f5dcfSAlexander Motin char *buffer; 548831f5dcfSAlexander Motin size_t left; 549831f5dcfSAlexander Motin 550831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 551831f5dcfSAlexander Motin buffer += slot->offset; 552831f5dcfSAlexander Motin /* Transfer one block at a time. */ 5535d5ae066SIlya Bakulin #ifdef MMCCAM 5545d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) { 5555d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 5565d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 5575d5ae066SIlya Bakulin } else 5585d5ae066SIlya Bakulin #endif 559831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 560831f5dcfSAlexander Motin slot->offset += left; 561831f5dcfSAlexander Motin 562ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 563831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 564831f5dcfSAlexander Motin while (left > 3) { 565831f5dcfSAlexander Motin data = buffer[0] + 566831f5dcfSAlexander Motin (buffer[1] << 8) + 567831f5dcfSAlexander Motin (buffer[2] << 16) + 568831f5dcfSAlexander Motin (buffer[3] << 24); 569831f5dcfSAlexander Motin left -= 4; 570831f5dcfSAlexander Motin buffer += 4; 571831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 572831f5dcfSAlexander Motin } 573831f5dcfSAlexander Motin } else { 574d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 575831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 576831f5dcfSAlexander Motin left &= 3; 577831f5dcfSAlexander Motin } 578831f5dcfSAlexander Motin /* Handle uneven size case. */ 579831f5dcfSAlexander Motin if (left > 0) { 580831f5dcfSAlexander Motin while (left > 0) { 581831f5dcfSAlexander Motin data <<= 8; 582831f5dcfSAlexander Motin data += *(buffer++); 583831f5dcfSAlexander Motin left--; 584831f5dcfSAlexander Motin } 585831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 586831f5dcfSAlexander Motin } 587831f5dcfSAlexander Motin } 588831f5dcfSAlexander Motin 589831f5dcfSAlexander Motin static void 590831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 591831f5dcfSAlexander Motin { 592831f5dcfSAlexander Motin 593831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 594831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 595831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 596831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 597831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 598831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 599831f5dcfSAlexander Motin break; 600831f5dcfSAlexander Motin } 601831f5dcfSAlexander Motin } else { 602831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 603831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 604831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 605831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 606831f5dcfSAlexander Motin break; 607831f5dcfSAlexander Motin } 608831f5dcfSAlexander Motin } 609831f5dcfSAlexander Motin } 610831f5dcfSAlexander Motin 611831f5dcfSAlexander Motin static void 6127e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 613831f5dcfSAlexander Motin { 614831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 6157e6ccea3SMarius Strobl device_t d; 616831f5dcfSAlexander Motin 617831f5dcfSAlexander Motin SDHCI_LOCK(slot); 6186e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 619a94a63f0SWarner Losh #ifdef MMCCAM 620a94a63f0SWarner Losh if (slot->card_present == 0) { 621a94a63f0SWarner Losh #else 622831f5dcfSAlexander Motin if (slot->dev == NULL) { 623a94a63f0SWarner Losh #endif 624831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 625639f59f0SIan Lepore if (bootverbose || sdhci_debug) 626639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 627a94a63f0SWarner Losh #ifdef MMCCAM 628a94a63f0SWarner Losh slot->card_present = 1; 629a94a63f0SWarner Losh union ccb *ccb; 630a94a63f0SWarner Losh uint32_t pathid; 631a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 632a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 633a94a63f0SWarner Losh if (ccb == NULL) { 634a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 635a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 636a94a63f0SWarner Losh return; 637a94a63f0SWarner Losh } 638a94a63f0SWarner Losh 639a94a63f0SWarner Losh /* 640a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 641a94a63f0SWarner Losh * will be at lun 0. 642a94a63f0SWarner Losh */ 643a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 644a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 645a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 646a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 647a94a63f0SWarner Losh xpt_free_ccb(ccb); 648a94a63f0SWarner Losh return; 649a94a63f0SWarner Losh } 650a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 651a94a63f0SWarner Losh xpt_rescan(ccb); 652a94a63f0SWarner Losh #else 653aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 654831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 655aca38eabSMarius Strobl if (d) { 656aca38eabSMarius Strobl device_set_ivars(d, slot); 657aca38eabSMarius Strobl (void)device_probe_and_attach(d); 658aca38eabSMarius Strobl } 659a94a63f0SWarner Losh #endif 660831f5dcfSAlexander Motin } else 661831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 662831f5dcfSAlexander Motin } else { 663a94a63f0SWarner Losh #ifdef MMCCAM 664a94a63f0SWarner Losh if (slot->card_present == 1) { 665a94a63f0SWarner Losh #else 666831f5dcfSAlexander Motin if (slot->dev != NULL) { 667a94a63f0SWarner Losh #endif 668831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 669639f59f0SIan Lepore if (bootverbose || sdhci_debug) 670639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 6717e6ccea3SMarius Strobl d = slot->dev; 672831f5dcfSAlexander Motin slot->dev = NULL; 673a94a63f0SWarner Losh #ifdef MMCCAM 674a94a63f0SWarner Losh slot->card_present = 0; 675a94a63f0SWarner Losh union ccb *ccb; 676a94a63f0SWarner Losh uint32_t pathid; 677a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 678a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 679a94a63f0SWarner Losh if (ccb == NULL) { 680a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 681a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 682a94a63f0SWarner Losh return; 683a94a63f0SWarner Losh } 684a94a63f0SWarner Losh 685a94a63f0SWarner Losh /* 686a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 687a94a63f0SWarner Losh * will be at lun 0. 688a94a63f0SWarner Losh */ 689a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 690a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 691a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 692a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 693a94a63f0SWarner Losh xpt_free_ccb(ccb); 694a94a63f0SWarner Losh return; 695a94a63f0SWarner Losh } 696a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 697a94a63f0SWarner Losh xpt_rescan(ccb); 698a94a63f0SWarner Losh #else 699aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 700cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 701aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 702aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 703831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 704aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 705d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 706a94a63f0SWarner Losh #endif 707831f5dcfSAlexander Motin } else 708831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 709831f5dcfSAlexander Motin } 710831f5dcfSAlexander Motin } 711831f5dcfSAlexander Motin 712b8bf08b1SIan Lepore static void 713b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 714639f59f0SIan Lepore { 715639f59f0SIan Lepore bool was_present; 716639f59f0SIan Lepore 717639f59f0SIan Lepore /* 718639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 719639f59f0SIan Lepore * create the child device after a short delay. The delay is to 720639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 721639f59f0SIan Lepore * before the other pins have made good contact). 722639f59f0SIan Lepore * 723639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 724639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 725639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 726639f59f0SIan Lepore * that happens by deleting and recreating the child device. 727639f59f0SIan Lepore */ 728a94a63f0SWarner Losh #ifdef MMCCAM 729a94a63f0SWarner Losh was_present = slot->card_present; 730a94a63f0SWarner Losh #else 731639f59f0SIan Lepore was_present = slot->dev != NULL; 732a94a63f0SWarner Losh #endif 733639f59f0SIan Lepore if (!was_present && is_present) { 734639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 735639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 736639f59f0SIan Lepore } else if (was_present && !is_present) { 737639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 738639f59f0SIan Lepore } 739b8bf08b1SIan Lepore } 740b8bf08b1SIan Lepore 741b8bf08b1SIan Lepore void 742b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 743b8bf08b1SIan Lepore { 744b8bf08b1SIan Lepore 745b8bf08b1SIan Lepore SDHCI_LOCK(slot); 746b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 747639f59f0SIan Lepore SDHCI_UNLOCK(slot); 748639f59f0SIan Lepore } 749639f59f0SIan Lepore 750639f59f0SIan Lepore static void 751639f59f0SIan Lepore sdhci_card_poll(void *arg) 752639f59f0SIan Lepore { 753639f59f0SIan Lepore struct sdhci_slot *slot = arg; 754639f59f0SIan Lepore 755639f59f0SIan Lepore sdhci_handle_card_present(slot, 756639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 757639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 758639f59f0SIan Lepore sdhci_card_poll, slot); 759639f59f0SIan Lepore } 760639f59f0SIan Lepore 761ab00a509SMarius Strobl static int 762ab00a509SMarius Strobl sdhci_dma_alloc(struct sdhci_slot *slot) 763ab00a509SMarius Strobl { 764ab00a509SMarius Strobl int err; 765ab00a509SMarius Strobl 766ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 767ab00a509SMarius Strobl if (MAXPHYS <= 1024 * 4) 768ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 769ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 8) 770ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 771ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 16) 772ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 773ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 32) 774ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 775ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 64) 776ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 777ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 128) 778ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 779ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 256) 780ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 781ab00a509SMarius Strobl else 782ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 783ab00a509SMarius Strobl } 784ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 785ab00a509SMarius Strobl 786ab00a509SMarius Strobl /* 787ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 788ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 789ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 790ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 791ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 792ab00a509SMarius Strobl * be aligned to the SDMA boundary. 793ab00a509SMarius Strobl */ 794ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 795ab00a509SMarius Strobl 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 796ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 797ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 798ab00a509SMarius Strobl if (err != 0) { 799ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 800ab00a509SMarius Strobl return (err); 801ab00a509SMarius Strobl } 802ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 803ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 804ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 805ab00a509SMarius Strobl if (err != 0) { 806ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 807ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 808ab00a509SMarius Strobl return (err); 809ab00a509SMarius Strobl } 810ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 811ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 812ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 813ab00a509SMarius Strobl &slot->paddr, 0); 814ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 815ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 816ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 817ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 818ab00a509SMarius Strobl if (err) 819ab00a509SMarius Strobl return (err); 820ab00a509SMarius Strobl else 821ab00a509SMarius Strobl return (EFAULT); 822ab00a509SMarius Strobl } 823ab00a509SMarius Strobl 824ab00a509SMarius Strobl return (0); 825ab00a509SMarius Strobl } 826ab00a509SMarius Strobl 827ab00a509SMarius Strobl static void 828ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 829ab00a509SMarius Strobl { 830ab00a509SMarius Strobl 831ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 832ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 833ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 834ab00a509SMarius Strobl } 835ab00a509SMarius Strobl 836d6b3aaf8SOleksandr Tymoshenko int 837d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 838831f5dcfSAlexander Motin { 839aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 840aca38eabSMarius Strobl kobj_method_t *kobj_method; 8410f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 842d6b3aaf8SOleksandr Tymoshenko int err; 843831f5dcfSAlexander Motin 844831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 845a94a63f0SWarner Losh 846d6b3aaf8SOleksandr Tymoshenko slot->num = num; 847d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 848d6b3aaf8SOleksandr Tymoshenko 849d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 850d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8510f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8528f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8530f34084fSMarius Strobl caps2 = slot->caps2; 8540f34084fSMarius Strobl } else { 855831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8560f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8570f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8580f34084fSMarius Strobl else 8590f34084fSMarius Strobl caps2 = 0; 8600f34084fSMarius Strobl } 8617fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8627fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8637fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 864ab00a509SMarius Strobl slot_printf(slot, 8657fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 8667fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 8677fcf4780SMarius Strobl return (ENXIO); 8687fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 8697fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 8707fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 8717fcf4780SMarius Strobl } 8727fcf4780SMarius Strobl } 873831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 87433aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 87587a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 87687a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 87733aad34dSOleksandr Tymoshenko else 87887a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 87987a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 88087a6a871SIan Lepore if (freq != 0) 88187a6a871SIan Lepore slot->max_clk = freq * 1000000; 88287a6a871SIan Lepore /* 88387a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 88487a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 88587a6a871SIan Lepore * with an assumption, so complain about it. 88687a6a871SIan Lepore */ 887831f5dcfSAlexander Motin if (slot->max_clk == 0) { 88887a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 889ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 8901bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 8911bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 892831f5dcfSAlexander Motin } 893a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 8948f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 8958f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 896a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 897a2832f9fSMarius Strobl slot->timeout_clk = 1000; 8988f3b7d56SOleksandr Tymoshenko } else { 8991bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 9001bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 9018f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 9028f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 9038f3b7d56SOleksandr Tymoshenko } 90487a6a871SIan Lepore /* 90587a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 90687a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 90787a6a871SIan Lepore * max timeout, but still mention it. 90887a6a871SIan Lepore */ 909831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 910ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 911ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 912ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 913831f5dcfSAlexander Motin } 914831f5dcfSAlexander Motin 91557677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 916831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 917831f5dcfSAlexander Motin slot->host.host_ocr = 0; 918831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 919831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 920831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 921831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 92249dfdf63SIan Lepore /* 92349dfdf63SIan Lepore * 1.8V VDD is not supposed to be used for removable cards. Hardware 92449dfdf63SIan Lepore * prior to v3.0 had no way to indicate embedded slots, but did 92549dfdf63SIan Lepore * sometimes support 1.8v for non-removable devices. 92649dfdf63SIan Lepore */ 92749dfdf63SIan Lepore if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 || 92849dfdf63SIan Lepore (slot->opt & SDHCI_SLOT_EMBEDDED))) 929831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 930831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 931ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 932831f5dcfSAlexander Motin "support voltages.\n"); 933831f5dcfSAlexander Motin } 934aca38eabSMarius Strobl 9350f34084fSMarius Strobl host_caps = MMC_CAP_4_BIT_DATA; 9362d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 9370f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 938831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 9390f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 94072dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9410f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 94272dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9430f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 944aca38eabSMarius Strobl 945aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9460f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9470f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9480f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9490f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9500f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9510f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9520f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9530f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9540f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9550f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9560f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9570f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9580f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9590f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9600f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9610f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 962835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 963835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 964835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 965aca38eabSMarius Strobl 966aca38eabSMarius Strobl /* 967aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 968aca38eabSMarius Strobl * default NULL implementation. 969aca38eabSMarius Strobl */ 970aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 971aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 972aca38eabSMarius Strobl kobj_desc); 973aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 974aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 975aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 976aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 977aca38eabSMarius Strobl 978aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 979aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 980aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 981aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 982aca38eabSMarius Strobl 983aca38eabSMarius Strobl /* 984aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 985aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 986aca38eabSMarius Strobl */ 987aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 988aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 989aca38eabSMarius Strobl kobj_desc); 990aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 991aca38eabSMarius Strobl goto no_tuning; 992aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 993aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 994aca38eabSMarius Strobl kobj_desc); 995aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 996aca38eabSMarius Strobl no_tuning: 997aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 998aca38eabSMarius Strobl } 999aca38eabSMarius Strobl 1000aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 1001aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 1002aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 1003aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 1004aca38eabSMarius Strobl M_WAITOK); 1005aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 1006aca38eabSMarius Strobl M_WAITOK); 1007aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 1008aca38eabSMarius Strobl M_WAITOK); 1009aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 1010aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 1011aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 1012aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 1013aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 1014aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 1015aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 1016aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 1017ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 1018aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 1019aca38eabSMarius Strobl slot->retune_count = 1; 1020aca38eabSMarius Strobl } else if (slot->retune_count != 0) 1021aca38eabSMarius Strobl slot->retune_count = 1022aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 1023aca38eabSMarius Strobl } 1024aca38eabSMarius Strobl } 1025aca38eabSMarius Strobl 1026aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 1027aca38eabSMarius Strobl 1028aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 10290f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 10300f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1031aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 10320f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 10330f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 1034aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 1035aca38eabSMarius Strobl 1036aca38eabSMarius Strobl /* 1037aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 1038aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 1039aca38eabSMarius Strobl * generic SDHCI implementation. 1040aca38eabSMarius Strobl */ 1041aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1042aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1043aca38eabSMarius Strobl kobj_desc); 1044aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1045aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1046aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1047aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1048aca38eabSMarius Strobl 1049aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1050f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10510f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1052f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10530f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1054f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10550f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10560f34084fSMarius Strobl slot->host.caps = host_caps; 10570f34084fSMarius Strobl 1058831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1059831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1060831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1061d6b3aaf8SOleksandr Tymoshenko 1062d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1063831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1064d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1065831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1066a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1067a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1068831f5dcfSAlexander Motin 1069c3a0f75aSOleksandr Tymoshenko /* 1070c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1071c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1072c3a0f75aSOleksandr Tymoshenko */ 1073c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1074c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1075c3a0f75aSOleksandr Tymoshenko 1076ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 1077ab00a509SMarius Strobl err = sdhci_dma_alloc(slot); 1078ab00a509SMarius Strobl if (err != 0) { 1079ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1080ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1081ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1082ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1083ab00a509SMarius Strobl } 1084ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1085ab00a509SMarius Strobl return (err); 1086ab00a509SMarius Strobl } 1087ab00a509SMarius Strobl } 1088ab00a509SMarius Strobl 10895b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 10900f34084fSMarius Strobl slot_printf(slot, 10917fcf4780SMarius Strobl "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 1092831f5dcfSAlexander Motin slot->max_clk / 1000000, 1093831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 10940f34084fSMarius Strobl (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 10950f34084fSMarius Strobl ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 1096831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 1097831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 10987fcf4780SMarius Strobl ((caps & SDHCI_CAN_VDD_180) && 10997fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 11000f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 11010f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 1102aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 1103aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 1104aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 11057fcf4780SMarius Strobl (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 11067fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 11077fcf4780SMarius Strobl (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 11087fcf4780SMarius Strobl "removable"); 11090f34084fSMarius Strobl if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 11100f34084fSMarius Strobl MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 11110f34084fSMarius Strobl slot_printf(slot, "eMMC:%s%s%s%s\n", 11120f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 11130f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 11140f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 11150f34084fSMarius Strobl ((host_caps & 11160f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 11170f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 11180f34084fSMarius Strobl " HS400ES" : ""); 11190f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 11200f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 11210f34084fSMarius Strobl slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 11220f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 11230f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 11240f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 11250f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 11260f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 1127aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) 1128aca38eabSMarius Strobl slot_printf(slot, "Re-tuning count %d secs, mode %d\n", 1129aca38eabSMarius Strobl slot->retune_count, slot->retune_mode + 1); 1130831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1131831f5dcfSAlexander Motin } 1132831f5dcfSAlexander Motin 1133ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1134ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1135ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 11364d52f81dSIan Lepore "timeout", CTLFLAG_RWTUN, &slot->timeout, 0, 1137ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1138831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1139639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1140639f59f0SIan Lepore sdhci_card_task, slot); 1141639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1142e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1143aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1144ba6fc1c7SLuiz Otavio O Souza 1145639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1146639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1147639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1148639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1149639f59f0SIan Lepore } 1150639f59f0SIan Lepore 1151aca38eabSMarius Strobl sdhci_init(slot); 1152aca38eabSMarius Strobl 1153831f5dcfSAlexander Motin return (0); 1154831f5dcfSAlexander Motin } 1155831f5dcfSAlexander Motin 1156d91f1a10SIlya Bakulin #ifndef MMCCAM 1157d6b3aaf8SOleksandr Tymoshenko void 1158d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1159831f5dcfSAlexander Motin { 11607e6ccea3SMarius Strobl 1161d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1162d6b3aaf8SOleksandr Tymoshenko } 1163d91f1a10SIlya Bakulin #endif 1164831f5dcfSAlexander Motin 1165d6b3aaf8SOleksandr Tymoshenko int 1166d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1167d6b3aaf8SOleksandr Tymoshenko { 1168831f5dcfSAlexander Motin device_t d; 1169831f5dcfSAlexander Motin 1170e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1171639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1172aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1173831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1174639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1175831f5dcfSAlexander Motin 1176831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1177831f5dcfSAlexander Motin d = slot->dev; 1178831f5dcfSAlexander Motin slot->dev = NULL; 1179831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1180831f5dcfSAlexander Motin if (d != NULL) 1181d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1182831f5dcfSAlexander Motin 1183831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1184831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 1185831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1186ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1187ab00a509SMarius Strobl sdhci_dma_free(slot); 1188aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1189aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1190aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1191aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1192aca38eabSMarius Strobl } 1193d6b3aaf8SOleksandr Tymoshenko 1194831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1195d6b3aaf8SOleksandr Tymoshenko 1196831f5dcfSAlexander Motin return (0); 1197831f5dcfSAlexander Motin } 1198831f5dcfSAlexander Motin 1199d6b3aaf8SOleksandr Tymoshenko int 1200d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 120192bf0e27SAlexander Motin { 12027e6ccea3SMarius Strobl 1203aca38eabSMarius Strobl /* 1204aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1205aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1206aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1207aca38eabSMarius Strobl */ 1208aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1209aca38eabSMarius Strobl SDHCI_LOCK(slot); 1210aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1211d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 1212aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 121392bf0e27SAlexander Motin 121492bf0e27SAlexander Motin return (0); 121592bf0e27SAlexander Motin } 121692bf0e27SAlexander Motin 1217d6b3aaf8SOleksandr Tymoshenko int 1218d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 121992bf0e27SAlexander Motin { 12207e6ccea3SMarius Strobl 1221aca38eabSMarius Strobl SDHCI_LOCK(slot); 1222d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1223aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 122492bf0e27SAlexander Motin 1225d6b3aaf8SOleksandr Tymoshenko return (0); 122692bf0e27SAlexander Motin } 122792bf0e27SAlexander Motin 122857677a3aSOleksandr Tymoshenko uint32_t 1229b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 123057677a3aSOleksandr Tymoshenko { 12317e6ccea3SMarius Strobl 123257677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 123357677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 123457677a3aSOleksandr Tymoshenko else 123557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 123657677a3aSOleksandr Tymoshenko } 123757677a3aSOleksandr Tymoshenko 12386e37fb2bSIan Lepore bool 1239b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 12406e37fb2bSIan Lepore { 12416e37fb2bSIan Lepore 1242639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1243639f59f0SIan Lepore return true; 1244639f59f0SIan Lepore 12456e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 12466e37fb2bSIan Lepore } 12476e37fb2bSIan Lepore 12480f34084fSMarius Strobl void 12490f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 12500f34084fSMarius Strobl { 1251ab00a509SMarius Strobl const struct mmc_ios *ios; 12520f34084fSMarius Strobl uint16_t hostctrl2; 12530f34084fSMarius Strobl 12540f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 12550f34084fSMarius Strobl return; 12560f34084fSMarius Strobl 1257aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 12580f34084fSMarius Strobl ios = &slot->host.ios; 12590f34084fSMarius Strobl sdhci_set_clock(slot, 0); 12600f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12610f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1262aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 12630f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 12640f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 12650f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1266aca38eabSMarius Strobl else 12670f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1268aca38eabSMarius Strobl } 12690f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 12700f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 12710f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 12720f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 12730f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 12740f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 12750f34084fSMarius Strobl else 12760f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 12770f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 12780f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 12790f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12800f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 12810f34084fSMarius Strobl } 12820f34084fSMarius Strobl 1283d6b3aaf8SOleksandr Tymoshenko int 1284d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1285831f5dcfSAlexander Motin { 1286831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1287831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1288831f5dcfSAlexander Motin 1289831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1290831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1291831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1292831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1293831f5dcfSAlexander Motin sdhci_init(slot); 1294831f5dcfSAlexander Motin } 1295831f5dcfSAlexander Motin /* Configure the bus. */ 1296831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1297831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 12982d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 12992d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1300831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13012d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 13022d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13032d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 13042d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 13052d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 13062d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 13072d1731b8SIan Lepore } else { 13082d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 13092d1731b8SIan Lepore } 13100f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1311bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1312831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1313831f5dcfSAlexander Motin else 1314831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1315831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 13160f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1317831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1318d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1319831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1320831f5dcfSAlexander Motin 1321831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1322831f5dcfSAlexander Motin return (0); 1323831f5dcfSAlexander Motin } 1324831f5dcfSAlexander Motin 13250f34084fSMarius Strobl int 13260f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 13270f34084fSMarius Strobl { 13280f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 13290f34084fSMarius Strobl enum mmc_vccq vccq; 13300f34084fSMarius Strobl int err; 13310f34084fSMarius Strobl uint16_t hostctrl2; 13320f34084fSMarius Strobl 13330f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 13340f34084fSMarius Strobl return (0); 13350f34084fSMarius Strobl 13360f34084fSMarius Strobl err = 0; 13370f34084fSMarius Strobl vccq = slot->host.ios.vccq; 13380f34084fSMarius Strobl SDHCI_LOCK(slot); 13390f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13400f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13410f34084fSMarius Strobl switch (vccq) { 13420f34084fSMarius Strobl case vccq_330: 13430f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13440f34084fSMarius Strobl goto done; 13450f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 13460f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13470f34084fSMarius Strobl DELAY(5000); 13480f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13490f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13500f34084fSMarius Strobl goto done; 13510f34084fSMarius Strobl err = EAGAIN; 13520f34084fSMarius Strobl break; 13530f34084fSMarius Strobl case vccq_180: 13540f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 13550f34084fSMarius Strobl err = EINVAL; 13560f34084fSMarius Strobl goto done; 13570f34084fSMarius Strobl } 13580f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13590f34084fSMarius Strobl goto done; 13600f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 13610f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13620f34084fSMarius Strobl DELAY(5000); 13630f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13640f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13650f34084fSMarius Strobl goto done; 13660f34084fSMarius Strobl err = EAGAIN; 13670f34084fSMarius Strobl break; 13680f34084fSMarius Strobl default: 13690f34084fSMarius Strobl slot_printf(slot, 13700f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 13710f34084fSMarius Strobl err = EINVAL; 13720f34084fSMarius Strobl break; 13730f34084fSMarius Strobl } 13740f34084fSMarius Strobl done: 13750f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 13760f34084fSMarius Strobl SDHCI_UNLOCK(slot); 13770f34084fSMarius Strobl return (err); 13780f34084fSMarius Strobl } 13790f34084fSMarius Strobl 1380aca38eabSMarius Strobl int 1381aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1382aca38eabSMarius Strobl { 1383aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1384ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1385aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1386aca38eabSMarius Strobl struct mmc_data *tune_data; 1387aca38eabSMarius Strobl uint32_t opcode; 1388aca38eabSMarius Strobl int err; 1389aca38eabSMarius Strobl 1390aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1391aca38eabSMarius Strobl return (0); 1392aca38eabSMarius Strobl 1393aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1394aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1395aca38eabSMarius Strobl SDHCI_LOCK(slot); 1396aca38eabSMarius Strobl switch (ios->timing) { 1397aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1398aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1399aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1400aca38eabSMarius Strobl return (EINVAL); 1401aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1402aca38eabSMarius Strobl /* 1403aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1404aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1405aca38eabSMarius Strobl * expected to be required. 1406aca38eabSMarius Strobl */ 1407aca38eabSMarius Strobl if (hs400) 1408aca38eabSMarius Strobl slot->retune_ticks = 0; 1409aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1410aca38eabSMarius Strobl break; 1411aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1412aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1413aca38eabSMarius Strobl break; 1414aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1415aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1416aca38eabSMarius Strobl break; 1417aca38eabSMarius Strobl /* FALLTHROUGH */ 1418aca38eabSMarius Strobl default: 1419aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1420aca38eabSMarius Strobl return (0); 1421aca38eabSMarius Strobl } 1422aca38eabSMarius Strobl 1423aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1424aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1425aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1426aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1427aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1428aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1429aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1430aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1431aca38eabSMarius Strobl MMC_TUNING_LEN; 1432aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1433aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1434aca38eabSMarius Strobl 1435aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1436aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1437aca38eabSMarius Strobl if (err == 0) { 1438aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1439aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1440cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1441aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1442aca38eabSMarius Strobl if (slot->retune_ticks) { 1443aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1444aca38eabSMarius Strobl sdhci_retune, slot); 1445aca38eabSMarius Strobl } 1446aca38eabSMarius Strobl } 1447aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1448aca38eabSMarius Strobl return (err); 1449aca38eabSMarius Strobl } 1450aca38eabSMarius Strobl 1451aca38eabSMarius Strobl int 1452aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1453aca38eabSMarius Strobl { 1454aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1455aca38eabSMarius Strobl int err; 1456aca38eabSMarius Strobl 1457aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1458aca38eabSMarius Strobl return (0); 1459aca38eabSMarius Strobl 1460aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1461aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1462aca38eabSMarius Strobl return (EINVAL); 1463aca38eabSMarius Strobl 1464aca38eabSMarius Strobl SDHCI_LOCK(slot); 1465aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1466aca38eabSMarius Strobl /* 1467aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1468aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1469aca38eabSMarius Strobl * with the host properly acquired, and 1470aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1471aca38eabSMarius Strobl * 1472aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1473aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1474aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1475aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1476aca38eabSMarius Strobl */ 1477aca38eabSMarius Strobl if (slot->retune_ticks) { 1478aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1479aca38eabSMarius Strobl sdhci_retune, slot); 1480aca38eabSMarius Strobl } 1481aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1482aca38eabSMarius Strobl return (err); 1483aca38eabSMarius Strobl } 1484aca38eabSMarius Strobl 1485aca38eabSMarius Strobl static int 1486aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1487aca38eabSMarius Strobl { 1488aca38eabSMarius Strobl struct mmc_request *tune_req; 1489aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1490aca38eabSMarius Strobl int i; 1491aca38eabSMarius Strobl uint32_t intmask; 1492aca38eabSMarius Strobl uint16_t hostctrl2; 1493aca38eabSMarius Strobl u_char opt; 1494aca38eabSMarius Strobl 1495aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1496aca38eabSMarius Strobl if (slot->req != NULL) 1497aca38eabSMarius Strobl return (EBUSY); 1498aca38eabSMarius Strobl 1499aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1500aca38eabSMarius Strobl opt = slot->opt; 1501aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1502aca38eabSMarius Strobl 1503aca38eabSMarius Strobl /* 1504aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1505aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1506aca38eabSMarius Strobl */ 1507aca38eabSMarius Strobl intmask = slot->intmask; 1508aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1509cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1510aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1511aca38eabSMarius Strobl 1512aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1513aca38eabSMarius Strobl if (reset) 1514aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1515aca38eabSMarius Strobl else 1516aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1517aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1518aca38eabSMarius Strobl 1519aca38eabSMarius Strobl tune_req = slot->tune_req; 1520aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1521aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1522aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1523aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1524aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1525aca38eabSMarius Strobl tune_req->done_data = slot; 1526aca38eabSMarius Strobl slot->req = tune_req; 1527aca38eabSMarius Strobl slot->flags = 0; 1528aca38eabSMarius Strobl sdhci_start(slot); 1529aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1530aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1531aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1532aca38eabSMarius Strobl break; 1533aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1534aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1535aca38eabSMarius Strobl break; 1536aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1537aca38eabSMarius Strobl DELAY(1000); 1538aca38eabSMarius Strobl } 1539aca38eabSMarius Strobl 154078f8baa8SMarius Strobl /* 154178f8baa8SMarius Strobl * Restore DMA usage and interrupts. 154278f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 154378f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 154478f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 154578f8baa8SMarius Strobl * doesn't lose these. 154678f8baa8SMarius Strobl */ 1547aca38eabSMarius Strobl slot->opt = opt; 1548aca38eabSMarius Strobl slot->intmask = intmask; 154978f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 155078f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1551aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1552aca38eabSMarius Strobl 1553aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1554aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1555aca38eabSMarius Strobl slot->retune_req = 0; 1556aca38eabSMarius Strobl return (0); 1557aca38eabSMarius Strobl } 1558aca38eabSMarius Strobl 1559aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1560aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1561aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1562aca38eabSMarius Strobl sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1563aca38eabSMarius Strobl return (EIO); 1564aca38eabSMarius Strobl } 1565aca38eabSMarius Strobl 1566aca38eabSMarius Strobl static void 1567aca38eabSMarius Strobl sdhci_retune(void *arg) 1568aca38eabSMarius Strobl { 1569aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1570aca38eabSMarius Strobl 1571aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1572aca38eabSMarius Strobl } 1573aca38eabSMarius Strobl 1574a94a63f0SWarner Losh #ifdef MMCCAM 1575a94a63f0SWarner Losh static void 1576a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1577a94a63f0SWarner Losh { 1578a94a63f0SWarner Losh union ccb *ccb; 157915c440e1SWarner Losh 1580aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 158115c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1582a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1583a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1584a94a63f0SWarner Losh ccb = slot->ccb; 1585a94a63f0SWarner Losh slot->ccb = NULL; 1586a94a63f0SWarner Losh slot->curcmd = NULL; 1587a94a63f0SWarner Losh 1588a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1589a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1590a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1591a94a63f0SWarner Losh 1592a94a63f0SWarner Losh ccb->ccb_h.status = 1593a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1594a94a63f0SWarner Losh xpt_done(ccb); 1595a94a63f0SWarner Losh } 1596a94a63f0SWarner Losh } 1597a94a63f0SWarner Losh #else 1598831f5dcfSAlexander Motin static void 1599e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1600e64f01a9SIan Lepore { 1601e64f01a9SIan Lepore struct mmc_request *req; 1602e64f01a9SIan Lepore 1603e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1604e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1605e64f01a9SIan Lepore req = slot->req; 1606e64f01a9SIan Lepore slot->req = NULL; 1607e64f01a9SIan Lepore slot->curcmd = NULL; 1608e64f01a9SIan Lepore req->done(req); 1609e64f01a9SIan Lepore } 1610e64f01a9SIan Lepore } 1611a94a63f0SWarner Losh #endif 1612e64f01a9SIan Lepore 1613e64f01a9SIan Lepore static void 1614aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1615aca38eabSMarius Strobl { 1616aca38eabSMarius Strobl struct sdhci_slot *slot; 1617aca38eabSMarius Strobl 1618aca38eabSMarius Strobl slot = req->done_data; 1619aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1620aca38eabSMarius Strobl wakeup(req); 1621aca38eabSMarius Strobl } 1622aca38eabSMarius Strobl 1623aca38eabSMarius Strobl static void 1624e64f01a9SIan Lepore sdhci_timeout(void *arg) 1625e64f01a9SIan Lepore { 1626e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1627e64f01a9SIan Lepore 1628e64f01a9SIan Lepore if (slot->curcmd != NULL) { 16297e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 16307e586643SIan Lepore sdhci_dumpregs(slot); 1631a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1632e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1633e64f01a9SIan Lepore sdhci_req_done(slot); 16347e586643SIan Lepore } else { 16357e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1636e64f01a9SIan Lepore } 1637e64f01a9SIan Lepore } 1638e64f01a9SIan Lepore 1639e64f01a9SIan Lepore static void 1640ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1641831f5dcfSAlexander Motin { 1642831f5dcfSAlexander Motin uint16_t mode; 1643831f5dcfSAlexander Motin 1644831f5dcfSAlexander Motin if (data == NULL) 1645831f5dcfSAlexander Motin return; 1646831f5dcfSAlexander Motin 1647831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 16485d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1649831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 16505d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 16516dea80e6SMarius Strobl #ifdef MMCCAM 16526dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 16536dea80e6SMarius Strobl #else 16540519c933SMarius Strobl slot->req->stop != NULL && 16556dea80e6SMarius Strobl #endif 16566dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 16576dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 16586dea80e6SMarius Strobl } 1659831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1660831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1661831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1662831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1663831f5dcfSAlexander Motin 1664831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1665831f5dcfSAlexander Motin } 1666831f5dcfSAlexander Motin 1667831f5dcfSAlexander Motin static void 1668831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1669831f5dcfSAlexander Motin { 1670831f5dcfSAlexander Motin int flags, timeout; 167190993663SIan Lepore uint32_t mask; 1672831f5dcfSAlexander Motin 1673831f5dcfSAlexander Motin slot->curcmd = cmd; 1674831f5dcfSAlexander Motin slot->cmd_done = 0; 1675831f5dcfSAlexander Motin 1676831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1677831f5dcfSAlexander Motin 1678831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1679831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1680831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1681831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1682e64f01a9SIan Lepore sdhci_req_done(slot); 1683831f5dcfSAlexander Motin return; 1684831f5dcfSAlexander Motin } 1685831f5dcfSAlexander Motin 1686b440e965SMarius Strobl /* 1687b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1688b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1689b440e965SMarius Strobl */ 16906e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1691d8208d9eSAlexander Motin slot->power == 0 || 1692d8208d9eSAlexander Motin slot->clock == 0) { 1693a94a63f0SWarner Losh slot_printf(slot, 1694a94a63f0SWarner Losh "Cannot issue a command (power=%d clock=%d)", 1695a94a63f0SWarner Losh slot->power, slot->clock); 1696831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1697e64f01a9SIan Lepore sdhci_req_done(slot); 1698831f5dcfSAlexander Motin return; 1699831f5dcfSAlexander Motin } 1700831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1701831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1702831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1703a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1704831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1705aca38eabSMarius Strobl /* 1706aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1707aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1708aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1709aca38eabSMarius Strobl */ 1710a94a63f0SWarner Losh #ifdef MMCCAM 1711aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1712a94a63f0SWarner Losh #else 1713aca38eabSMarius Strobl if (cmd == slot->req->stop || 1714a94a63f0SWarner Losh #endif 1715aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1716aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1717aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 17188775ab45SIan Lepore /* 17198775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 17208775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 17218775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 17228775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 17238775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 17248775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 17258775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 17268775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 17278775ab45SIan Lepore */ 17288775ab45SIan Lepore timeout = 250; 172990993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1730831f5dcfSAlexander Motin if (timeout == 0) { 1731831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1732831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1733831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1734831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1735e64f01a9SIan Lepore sdhci_req_done(slot); 1736831f5dcfSAlexander Motin return; 1737831f5dcfSAlexander Motin } 1738831f5dcfSAlexander Motin timeout--; 1739831f5dcfSAlexander Motin DELAY(1000); 1740831f5dcfSAlexander Motin } 1741831f5dcfSAlexander Motin 1742831f5dcfSAlexander Motin /* Prepare command flags. */ 1743831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1744831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1745831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1746831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1747831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1748831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1749831f5dcfSAlexander Motin else 1750831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1751831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1752831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1753831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1754831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1755a94a63f0SWarner Losh if (cmd->data != NULL) 1756831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1757831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1758831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1759831f5dcfSAlexander Motin /* Prepare data. */ 1760831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1761831f5dcfSAlexander Motin /* 1762831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1763831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1764831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1765831f5dcfSAlexander Motin */ 1766831f5dcfSAlexander Motin if (slot->data_done == 0) { 1767831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1768831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1769831f5dcfSAlexander Motin } 1770831f5dcfSAlexander Motin /* Set command argument. */ 1771831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1772831f5dcfSAlexander Motin /* Set data transfer mode. */ 1773831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1774aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 17758adf4202SBjoern A. Zeeb slot_printf(slot, "Starting command opcode %#04x flags %#04x\n", 17768adf4202SBjoern A. Zeeb cmd->opcode, flags); 17778adf4202SBjoern A. Zeeb 1778831f5dcfSAlexander Motin /* Start command. */ 1779d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1780a6873fd1SIan Lepore /* Start timeout callout. */ 1781ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1782ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1783831f5dcfSAlexander Motin } 1784831f5dcfSAlexander Motin 1785831f5dcfSAlexander Motin static void 1786831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1787831f5dcfSAlexander Motin { 1788831f5dcfSAlexander Motin int i; 17891bacf3beSMarius Strobl uint32_t val; 17901bacf3beSMarius Strobl uint8_t extra; 1791831f5dcfSAlexander Motin 1792aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 17938adf4202SBjoern A. Zeeb slot_printf(slot, "%s: called, err %d flags %#04x\n", 1794a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1795831f5dcfSAlexander Motin slot->cmd_done = 1; 179672dec079SMarius Strobl /* 179772dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1798831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 179972dec079SMarius Strobl * happened first. 180072dec079SMarius Strobl */ 1801aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1802aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1803aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1804aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1805831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1806831f5dcfSAlexander Motin if (slot->curcmd->error) { 1807aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1808aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1809831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1810831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1811831f5dcfSAlexander Motin sdhci_start(slot); 1812831f5dcfSAlexander Motin return; 1813831f5dcfSAlexander Motin } 1814831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1815831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1816831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1817831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 18181bacf3beSMarius Strobl extra = 0; 1819831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 18201bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 18211bacf3beSMarius Strobl if (slot->quirks & 18221bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1823677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1824677ee494SIan Lepore else { 1825677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1826677ee494SIan Lepore (val << 8) | extra; 1827831f5dcfSAlexander Motin extra = val >> 24; 1828831f5dcfSAlexander Motin } 1829677ee494SIan Lepore } 1830831f5dcfSAlexander Motin } else 1831831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1832831f5dcfSAlexander Motin } 1833aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 18348adf4202SBjoern A. Zeeb slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n", 1835a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1836a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1837a94a63f0SWarner Losh 1838831f5dcfSAlexander Motin /* If data ready - finish. */ 1839831f5dcfSAlexander Motin if (slot->data_done) 1840831f5dcfSAlexander Motin sdhci_start(slot); 1841831f5dcfSAlexander Motin } 1842831f5dcfSAlexander Motin 1843831f5dcfSAlexander Motin static void 1844ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1845831f5dcfSAlexander Motin { 1846ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1847831f5dcfSAlexander Motin uint8_t div; 1848831f5dcfSAlexander Motin 1849831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1850831f5dcfSAlexander Motin slot->data_done = 1; 1851831f5dcfSAlexander Motin return; 1852831f5dcfSAlexander Motin } 1853831f5dcfSAlexander Motin 1854831f5dcfSAlexander Motin slot->data_done = 0; 1855831f5dcfSAlexander Motin 1856831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1857831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1858ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1859ceb9e9f7SIan Lepore div = 0xE; 1860ceb9e9f7SIan Lepore } else { 1861831f5dcfSAlexander Motin target_timeout = 1000000; 1862831f5dcfSAlexander Motin div = 0; 1863831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1864ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1865ceb9e9f7SIan Lepore ++div; 1866831f5dcfSAlexander Motin current_timeout <<= 1; 1867831f5dcfSAlexander Motin } 1868831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1869ceb9e9f7SIan Lepore if (div < 0xE && 1870ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1871ceb9e9f7SIan Lepore ++div; 1872831f5dcfSAlexander Motin } 1873ceb9e9f7SIan Lepore } 1874831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1875831f5dcfSAlexander Motin 1876831f5dcfSAlexander Motin if (data == NULL) 1877831f5dcfSAlexander Motin return; 1878831f5dcfSAlexander Motin 1879831f5dcfSAlexander Motin /* Use DMA if possible. */ 1880831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1881831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1882ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1883d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1884831f5dcfSAlexander Motin (data->len <= 512)) 1885831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1886831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1887d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1888831f5dcfSAlexander Motin ((data->len) & 0x3)) 1889831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1890831f5dcfSAlexander Motin /* Load DMA buffer. */ 1891831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1892ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1893831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1894ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1895ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1896831f5dcfSAlexander Motin else { 1897ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1898ab00a509SMarius Strobl sdma_bbufsz)); 1899ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1900ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1901831f5dcfSAlexander Motin } 1902831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1903ab00a509SMarius Strobl /* 1904ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1905ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1906ab00a509SMarius Strobl */ 1907ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1908831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1909831f5dcfSAlexander Motin else 1910831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1911831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1912831f5dcfSAlexander Motin } 1913831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1914831f5dcfSAlexander Motin slot->offset = 0; 19155d5ae066SIlya Bakulin #ifdef MMCCAM 19165d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 19175d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 19185d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 19195d5ae066SIlya Bakulin blkcnt = data->block_count; 19205d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 19215d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 19225d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 19235d5ae066SIlya Bakulin } else 19245d5ae066SIlya Bakulin #endif 19255d5ae066SIlya Bakulin { 1926ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 1927ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 1928ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 19295d5ae066SIlya Bakulin } 19305d5ae066SIlya Bakulin 19315d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 1932ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 1933aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1934ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 1935ab00a509SMarius Strobl blksz, blkcnt); 1936831f5dcfSAlexander Motin } 1937831f5dcfSAlexander Motin 1938c3a0f75aSOleksandr Tymoshenko void 1939831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1940831f5dcfSAlexander Motin { 1941831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 19427e6ccea3SMarius Strobl size_t left; 1943831f5dcfSAlexander Motin 1944831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1945ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1946831f5dcfSAlexander Motin * happened first. */ 1947831f5dcfSAlexander Motin if (!slot->cmd_done) { 1948831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1949831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1950831f5dcfSAlexander Motin } 1951831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1952915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1953915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 1954831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 19557e6ccea3SMarius Strobl left = data->len - slot->offset; 1956ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1957ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1958831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1959ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 1960831f5dcfSAlexander Motin } else 1961ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1962ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1963831f5dcfSAlexander Motin } 1964a98788edSIan Lepore slot->data_done = 1; 1965831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1966831f5dcfSAlexander Motin if (slot->curcmd->error) { 1967aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1968aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1969831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1970831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1971831f5dcfSAlexander Motin sdhci_start(slot); 1972831f5dcfSAlexander Motin return; 1973831f5dcfSAlexander Motin } 1974831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1975831f5dcfSAlexander Motin if (slot->cmd_done) 1976831f5dcfSAlexander Motin sdhci_start(slot); 1977831f5dcfSAlexander Motin } 1978831f5dcfSAlexander Motin 1979a94a63f0SWarner Losh #ifdef MMCCAM 1980a94a63f0SWarner Losh static void 1981a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 1982a94a63f0SWarner Losh { 1983a94a63f0SWarner Losh union ccb *ccb; 1984ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 1985a94a63f0SWarner Losh 1986a94a63f0SWarner Losh ccb = slot->ccb; 1987a94a63f0SWarner Losh if (ccb == NULL) 1988a94a63f0SWarner Losh return; 1989a94a63f0SWarner Losh 1990a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1991a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 1992a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 1993a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 1994a94a63f0SWarner Losh return; 1995a94a63f0SWarner Losh } 1996a94a63f0SWarner Losh 1997a94a63f0SWarner Losh /* 1998a94a63f0SWarner Losh * Old stack doesn't use this! 1999a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 2000a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 2001a94a63f0SWarner Losh * Not using this code does no harm... 2002a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 2003a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 2004a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 2005a94a63f0SWarner Losh return; 2006a94a63f0SWarner Losh } 2007a94a63f0SWarner Losh */ 2008aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2009a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 2010a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 2011a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 2012a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD); 2013a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_DATA); 2014a94a63f0SWarner Losh } 2015a94a63f0SWarner Losh 2016a94a63f0SWarner Losh sdhci_req_done(slot); 2017a94a63f0SWarner Losh } 2018a94a63f0SWarner Losh #else 2019831f5dcfSAlexander Motin static void 2020831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 2021831f5dcfSAlexander Motin { 2022ab00a509SMarius Strobl const struct mmc_request *req; 2023831f5dcfSAlexander Motin 2024831f5dcfSAlexander Motin req = slot->req; 2025831f5dcfSAlexander Motin if (req == NULL) 2026831f5dcfSAlexander Motin return; 2027831f5dcfSAlexander Motin 2028831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 2029831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 2030831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 2031831f5dcfSAlexander Motin return; 2032831f5dcfSAlexander Motin } 2033915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 2034915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 2035831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 2036831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 2037831f5dcfSAlexander Motin return; 2038831f5dcfSAlexander Motin } 2039aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 20405b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 20415b69a497SAlexander Motin if (!req->cmd->error && 2042915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2043915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2044915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2045831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2046831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 2047831f5dcfSAlexander Motin } 2048831f5dcfSAlexander Motin 2049e64f01a9SIan Lepore sdhci_req_done(slot); 2050831f5dcfSAlexander Motin } 2051a94a63f0SWarner Losh #endif 2052831f5dcfSAlexander Motin 2053d6b3aaf8SOleksandr Tymoshenko int 2054b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2055b440e965SMarius Strobl struct mmc_request *req) 2056831f5dcfSAlexander Motin { 2057831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2058831f5dcfSAlexander Motin 2059831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2060831f5dcfSAlexander Motin if (slot->req != NULL) { 2061831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2062831f5dcfSAlexander Motin return (EBUSY); 2063831f5dcfSAlexander Motin } 2064aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 20651bacf3beSMarius Strobl slot_printf(slot, 20661bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2067831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 20685b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 20695b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 20705b69a497SAlexander Motin } 2071831f5dcfSAlexander Motin slot->req = req; 2072831f5dcfSAlexander Motin slot->flags = 0; 2073831f5dcfSAlexander Motin sdhci_start(slot); 2074831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2075bea2dca2SAlexander Motin if (dumping) { 2076bea2dca2SAlexander Motin while (slot->req != NULL) { 2077d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2078bea2dca2SAlexander Motin DELAY(10); 2079bea2dca2SAlexander Motin } 2080bea2dca2SAlexander Motin } 2081831f5dcfSAlexander Motin return (0); 2082831f5dcfSAlexander Motin } 2083831f5dcfSAlexander Motin 2084d6b3aaf8SOleksandr Tymoshenko int 2085b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2086831f5dcfSAlexander Motin { 2087831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2088831f5dcfSAlexander Motin uint32_t val; 2089831f5dcfSAlexander Motin 2090831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2091831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2092831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2093831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2094831f5dcfSAlexander Motin } 2095831f5dcfSAlexander Motin 2096d6b3aaf8SOleksandr Tymoshenko int 2097b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2098831f5dcfSAlexander Motin { 2099831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2100831f5dcfSAlexander Motin int err = 0; 2101831f5dcfSAlexander Motin 2102831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2103831f5dcfSAlexander Motin while (slot->bus_busy) 2104d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2105831f5dcfSAlexander Motin slot->bus_busy++; 2106831f5dcfSAlexander Motin /* Activate led. */ 2107831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2108831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2109831f5dcfSAlexander Motin return (err); 2110831f5dcfSAlexander Motin } 2111831f5dcfSAlexander Motin 2112d6b3aaf8SOleksandr Tymoshenko int 2113b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2114831f5dcfSAlexander Motin { 2115831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2116831f5dcfSAlexander Motin 2117831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2118831f5dcfSAlexander Motin /* Deactivate led. */ 2119831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2120831f5dcfSAlexander Motin slot->bus_busy--; 2121831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2122d493985aSAlexander Motin wakeup(slot); 2123831f5dcfSAlexander Motin return (0); 2124831f5dcfSAlexander Motin } 2125831f5dcfSAlexander Motin 2126831f5dcfSAlexander Motin static void 2127831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2128831f5dcfSAlexander Motin { 2129831f5dcfSAlexander Motin 2130831f5dcfSAlexander Motin if (!slot->curcmd) { 2131831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2132831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2133831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2134831f5dcfSAlexander Motin return; 2135831f5dcfSAlexander Motin } 2136831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2137831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2138831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2139831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2140831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2141831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2142831f5dcfSAlexander Motin 2143831f5dcfSAlexander Motin sdhci_finish_command(slot); 2144831f5dcfSAlexander Motin } 2145831f5dcfSAlexander Motin 2146831f5dcfSAlexander Motin static void 2147831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2148831f5dcfSAlexander Motin { 21491bacf3beSMarius Strobl struct mmc_data *data; 215015c440e1SWarner Losh size_t left; 2151ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2152831f5dcfSAlexander Motin 2153831f5dcfSAlexander Motin if (!slot->curcmd) { 2154831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2155831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2156831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2157831f5dcfSAlexander Motin return; 2158831f5dcfSAlexander Motin } 2159831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2160831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2161831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2162831f5dcfSAlexander Motin "there is no active data operation.\n", 2163831f5dcfSAlexander Motin intmask); 2164831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2165831f5dcfSAlexander Motin return; 2166831f5dcfSAlexander Motin } 2167831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2168831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2169acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2170831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2171831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2172831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2173831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2174831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2175831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2176831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2177831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2178831f5dcfSAlexander Motin } 2179831f5dcfSAlexander Motin if (slot->curcmd->error) { 2180831f5dcfSAlexander Motin /* No need to continue after any error. */ 2181a98788edSIan Lepore goto done; 2182831f5dcfSAlexander Motin } 2183831f5dcfSAlexander Motin 2184aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2185aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2186aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2187aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2188aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2189aca38eabSMarius Strobl sdhci_finish_command(slot); 2190aca38eabSMarius Strobl sdhci_finish_data(slot); 2191aca38eabSMarius Strobl return; 2192aca38eabSMarius Strobl } 2193831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2194c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2195c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2196c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 21971bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 21981bacf3beSMarius Strobl &intmask); 2199c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2200c3a0f75aSOleksandr Tymoshenko } else 2201831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2202c3a0f75aSOleksandr Tymoshenko } 2203831f5dcfSAlexander Motin /* Handle DMA border. */ 2204831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 22051bacf3beSMarius Strobl data = slot->curcmd->data; 2206ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2207831f5dcfSAlexander Motin 2208831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2209831f5dcfSAlexander Motin left = data->len - slot->offset; 2210831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2211831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2212831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2213831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2214ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2215831f5dcfSAlexander Motin } else { 2216831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2217831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2218831f5dcfSAlexander Motin } 2219831f5dcfSAlexander Motin /* ... and reload it again. */ 2220ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2221831f5dcfSAlexander Motin left = data->len - slot->offset; 2222831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2223831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2224831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2225831f5dcfSAlexander Motin } else { 2226831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2227ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2228831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2229831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2230831f5dcfSAlexander Motin } 2231ab00a509SMarius Strobl /* 2232ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2233ab00a509SMarius Strobl * bounce buffer. 2234ab00a509SMarius Strobl */ 2235ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2236831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2237831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2238831f5dcfSAlexander Motin } 2239831f5dcfSAlexander Motin /* Restart DMA. */ 2240831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2241831f5dcfSAlexander Motin } 2242831f5dcfSAlexander Motin /* We have got all data. */ 2243c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2244c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2245c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2246c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2247c3a0f75aSOleksandr Tymoshenko } else 2248831f5dcfSAlexander Motin sdhci_finish_data(slot); 2249831f5dcfSAlexander Motin } 2250a98788edSIan Lepore done: 2251a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2252a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2253a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2254a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2255a98788edSIan Lepore } else 2256a98788edSIan Lepore sdhci_finish_data(slot); 2257a98788edSIan Lepore } 2258c3a0f75aSOleksandr Tymoshenko } 2259831f5dcfSAlexander Motin 2260831f5dcfSAlexander Motin static void 22616dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2262831f5dcfSAlexander Motin { 2263831f5dcfSAlexander Motin 2264831f5dcfSAlexander Motin if (!slot->curcmd) { 2265831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 22666dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2267831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2268831f5dcfSAlexander Motin return; 2269831f5dcfSAlexander Motin } 22706dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2271831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2272831f5dcfSAlexander Motin } 2273831f5dcfSAlexander Motin 2274d6b3aaf8SOleksandr Tymoshenko void 2275d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2276831f5dcfSAlexander Motin { 22772b96b955SJustin Hibbits uint32_t intmask, present; 22786dea80e6SMarius Strobl uint16_t val16; 2279831f5dcfSAlexander Motin 2280831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2281831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2282831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2283831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2284831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2285d6b3aaf8SOleksandr Tymoshenko return; 2286831f5dcfSAlexander Motin } 2287aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 22885b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 22895b69a497SAlexander Motin 2290aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2291aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 22926dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2293aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2294aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2295aca38eabSMarius Strobl if (slot->curcmd) { 2296aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2297aca38eabSMarius Strobl sdhci_finish_command(slot); 2298aca38eabSMarius Strobl } 2299aca38eabSMarius Strobl } 2300aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2301aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2302aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2303831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2304831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2305639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 23062b96b955SJustin Hibbits slot->intmask &= 23072b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 23082b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 23092b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 23102b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 23112b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2312831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2313831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2314b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2315831f5dcfSAlexander Motin } 2316831f5dcfSAlexander Motin /* Handle command interrupts. */ 2317831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2318831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2319831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2320831f5dcfSAlexander Motin } 2321831f5dcfSAlexander Motin /* Handle data interrupts. */ 2322831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2323831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 23247e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 23257e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2326831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2327831f5dcfSAlexander Motin } 2328831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2329831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 23306dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 23316dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2332831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 23336dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2334831f5dcfSAlexander Motin } 2335831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2336831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2337831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2338aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2339831f5dcfSAlexander Motin } 2340aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2341aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2342aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2343831f5dcfSAlexander Motin /* The rest is unknown. */ 2344831f5dcfSAlexander Motin if (intmask) { 2345831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2346831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2347831f5dcfSAlexander Motin intmask); 2348831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2349831f5dcfSAlexander Motin } 2350831f5dcfSAlexander Motin 2351831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2352831f5dcfSAlexander Motin } 2353831f5dcfSAlexander Motin 2354d6b3aaf8SOleksandr Tymoshenko int 23551bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 23561bacf3beSMarius Strobl uintptr_t *result) 2357831f5dcfSAlexander Motin { 2358ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2359831f5dcfSAlexander Motin 2360831f5dcfSAlexander Motin switch (which) { 2361831f5dcfSAlexander Motin default: 2362831f5dcfSAlexander Motin return (EINVAL); 2363831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2364bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2365831f5dcfSAlexander Motin break; 2366831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2367bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2368831f5dcfSAlexander Motin break; 2369831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2370bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2371831f5dcfSAlexander Motin break; 2372831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2373bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2374831f5dcfSAlexander Motin break; 2375831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2376bcd91d25SJayachandran C. *result = slot->host.f_min; 2377831f5dcfSAlexander Motin break; 2378831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2379bcd91d25SJayachandran C. *result = slot->host.f_max; 2380831f5dcfSAlexander Motin break; 2381831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2382bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2383831f5dcfSAlexander Motin break; 2384831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2385bcd91d25SJayachandran C. *result = slot->host.mode; 2386831f5dcfSAlexander Motin break; 2387831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2388bcd91d25SJayachandran C. *result = slot->host.ocr; 2389831f5dcfSAlexander Motin break; 2390831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2391bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2392831f5dcfSAlexander Motin break; 2393831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2394bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2395831f5dcfSAlexander Motin break; 2396aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2397aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2398aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2399aca38eabSMarius Strobl *result = retune_req_reset; 2400aca38eabSMarius Strobl break; 2401aca38eabSMarius Strobl } 2402aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2403aca38eabSMarius Strobl *result = retune_req_normal; 2404aca38eabSMarius Strobl break; 2405aca38eabSMarius Strobl } 2406aca38eabSMarius Strobl } 2407aca38eabSMarius Strobl *result = retune_req_none; 2408aca38eabSMarius Strobl break; 24090f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24100f34084fSMarius Strobl *result = slot->host.ios.vccq; 24110f34084fSMarius Strobl break; 2412831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2413bcd91d25SJayachandran C. *result = slot->host.caps; 2414831f5dcfSAlexander Motin break; 2415831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2416bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2417831f5dcfSAlexander Motin break; 24183a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2419aca38eabSMarius Strobl /* 2420aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2421aca38eabSMarius Strobl * per read/write command to 4 MiB. 2422aca38eabSMarius Strobl */ 2423aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2424aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2425aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2426aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2427aca38eabSMarius Strobl break; 2428aca38eabSMarius Strobl } 2429bcd91d25SJayachandran C. *result = 65535; 24303a4a2557SAlexander Motin break; 243172dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 243272dec079SMarius Strobl /* 243372dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 243472dec079SMarius Strobl */ 243572dec079SMarius Strobl *result = 1000000; 243672dec079SMarius Strobl break; 2437831f5dcfSAlexander Motin } 2438831f5dcfSAlexander Motin return (0); 2439831f5dcfSAlexander Motin } 2440831f5dcfSAlexander Motin 2441d6b3aaf8SOleksandr Tymoshenko int 24421bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 24431bacf3beSMarius Strobl uintptr_t value) 2444831f5dcfSAlexander Motin { 2445831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2446b440e965SMarius Strobl uint32_t clock, max_clock; 2447b440e965SMarius Strobl int i; 2448831f5dcfSAlexander Motin 244915c440e1SWarner Losh if (sdhci_debug > 1) 245015c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2451831f5dcfSAlexander Motin switch (which) { 2452831f5dcfSAlexander Motin default: 2453831f5dcfSAlexander Motin return (EINVAL); 2454831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2455831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2456831f5dcfSAlexander Motin break; 2457831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2458831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2459831f5dcfSAlexander Motin break; 2460831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2461831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2462831f5dcfSAlexander Motin break; 2463831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2464831f5dcfSAlexander Motin if (value > 0) { 246557677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 246657677a3aSOleksandr Tymoshenko clock = max_clock; 246757677a3aSOleksandr Tymoshenko 246857677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 246957677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 247057677a3aSOleksandr Tymoshenko i <<= 1) { 2471831f5dcfSAlexander Motin if (clock <= value) 2472831f5dcfSAlexander Motin break; 2473831f5dcfSAlexander Motin clock >>= 1; 2474831f5dcfSAlexander Motin } 2475b440e965SMarius Strobl } else { 247657677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 247757677a3aSOleksandr Tymoshenko i += 2) { 247857677a3aSOleksandr Tymoshenko if (clock <= value) 247957677a3aSOleksandr Tymoshenko break; 248057677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 248157677a3aSOleksandr Tymoshenko } 248257677a3aSOleksandr Tymoshenko } 248357677a3aSOleksandr Tymoshenko 2484831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2485831f5dcfSAlexander Motin } else 2486831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2487831f5dcfSAlexander Motin break; 2488831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2489831f5dcfSAlexander Motin slot->host.mode = value; 2490831f5dcfSAlexander Motin break; 2491831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2492831f5dcfSAlexander Motin slot->host.ocr = value; 2493831f5dcfSAlexander Motin break; 2494831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2495831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2496831f5dcfSAlexander Motin break; 2497831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2498831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2499831f5dcfSAlexander Motin break; 25000f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 25010f34084fSMarius Strobl slot->host.ios.vccq = value; 25020f34084fSMarius Strobl break; 2503831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2504831f5dcfSAlexander Motin slot->host.ios.timing = value; 2505831f5dcfSAlexander Motin break; 2506831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2507831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2508831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2509831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 25103a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2511aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2512831f5dcfSAlexander Motin return (EINVAL); 2513831f5dcfSAlexander Motin } 2514831f5dcfSAlexander Motin return (0); 2515831f5dcfSAlexander Motin } 2516831f5dcfSAlexander Motin 251715c440e1SWarner Losh #ifdef MMCCAM 2518a94a63f0SWarner Losh void 2519d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2520a94a63f0SWarner Losh { 2521ab00a509SMarius Strobl 2522505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2523a94a63f0SWarner Losh goto fail; 2524a94a63f0SWarner Losh 2525a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 25266e40542aSBjoern A. Zeeb slot->sim = cam_sim_alloc_dev(sdhci_cam_action, sdhci_cam_poll, 25276e40542aSBjoern A. Zeeb "sdhci_slot", slot, slot->bus, 2528a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2529a94a63f0SWarner Losh 2530a94a63f0SWarner Losh if (slot->sim == NULL) { 2531a94a63f0SWarner Losh cam_simq_free(slot->devq); 2532a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2533a94a63f0SWarner Losh goto fail; 2534a94a63f0SWarner Losh } 2535a94a63f0SWarner Losh 2536a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2537a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2538505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2539a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2540a94a63f0SWarner Losh cam_simq_free(slot->devq); 2541a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2542a94a63f0SWarner Losh goto fail; 2543a94a63f0SWarner Losh } 2544a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2545505f6a0cSBjoern A. Zeeb 2546a94a63f0SWarner Losh /* End CAM-specific init */ 2547a94a63f0SWarner Losh slot->card_present = 0; 2548a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2549a94a63f0SWarner Losh return; 2550a94a63f0SWarner Losh 2551a94a63f0SWarner Losh fail: 2552a94a63f0SWarner Losh if (slot->sim != NULL) { 2553a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2554a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2555a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2556a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2557a94a63f0SWarner Losh } 2558a94a63f0SWarner Losh 2559a94a63f0SWarner Losh if (slot->devq != NULL) 2560a94a63f0SWarner Losh cam_simq_free(slot->devq); 2561a94a63f0SWarner Losh } 2562a94a63f0SWarner Losh 2563a94a63f0SWarner Losh void 2564a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2565a94a63f0SWarner Losh { 2566a94a63f0SWarner Losh struct sdhci_slot *slot; 2567a94a63f0SWarner Losh 2568a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2569a94a63f0SWarner Losh if (slot == NULL) { 2570a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2571a94a63f0SWarner Losh xpt_done(ccb); 2572a94a63f0SWarner Losh return; 2573a94a63f0SWarner Losh } 2574a94a63f0SWarner Losh 2575a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2576a94a63f0SWarner Losh 2577a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2578a94a63f0SWarner Losh case XPT_PATH_INQ: 25798c7cd14aSWarner Losh mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, MAXPHYS); 2580a94a63f0SWarner Losh break; 25818c7cd14aSWarner Losh 2582a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2583a94a63f0SWarner Losh { 2584a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 25855d20e651SIlya Bakulin uint32_t max_data; 2586a94a63f0SWarner Losh 2587a94a63f0SWarner Losh if (sdhci_debug > 1) 2588a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2589a94a63f0SWarner Losh 2590a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2591a94a63f0SWarner Losh cts->protocol_version = 1; 2592a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2593a94a63f0SWarner Losh cts->transport_version = 1; 2594a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2595a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2596a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2597a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2598a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 25995d20e651SIlya Bakulin /* 26005d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 26015d20e651SIlya Bakulin * per read/write command to 4 MiB. 26025d20e651SIlya Bakulin */ 26035d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 26045d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 26055d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 26065d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 26075d20e651SIlya Bakulin } else { 26085d20e651SIlya Bakulin max_data = 65535; 26095d20e651SIlya Bakulin } 26105d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 26115d20e651SIlya Bakulin 2612a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2613a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2614a94a63f0SWarner Losh break; 2615a94a63f0SWarner Losh } 2616a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2617a94a63f0SWarner Losh if (sdhci_debug > 1) 2618a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2619a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2620a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2621a94a63f0SWarner Losh break; 2622a94a63f0SWarner Losh case XPT_RESET_BUS: 2623a94a63f0SWarner Losh if (sdhci_debug > 1) 2624a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2625a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2626a94a63f0SWarner Losh break; 2627a94a63f0SWarner Losh case XPT_MMC_IO: 2628a94a63f0SWarner Losh /* 2629a94a63f0SWarner Losh * Here is the HW-dependent part of 2630a94a63f0SWarner Losh * sending the command to the underlying h/w 2631a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2632a94a63f0SWarner Losh * Then the request will be marked as completed. 2633a94a63f0SWarner Losh */ 2634aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2635a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2636a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2637a94a63f0SWarner Losh 2638160799c6SWarner Losh sdhci_cam_request(cam_sim_softc(sim), ccb); 2639a94a63f0SWarner Losh return; 2640a94a63f0SWarner Losh default: 2641a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2642a94a63f0SWarner Losh break; 2643a94a63f0SWarner Losh } 2644a94a63f0SWarner Losh xpt_done(ccb); 2645a94a63f0SWarner Losh return; 2646a94a63f0SWarner Losh } 2647a94a63f0SWarner Losh 2648a94a63f0SWarner Losh void 2649a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2650a94a63f0SWarner Losh { 2651a94a63f0SWarner Losh return; 2652a94a63f0SWarner Losh } 2653a94a63f0SWarner Losh 26546dea80e6SMarius Strobl static int 2655ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2656ab00a509SMarius Strobl int proposed_clock) 26576dea80e6SMarius Strobl { 2658a94a63f0SWarner Losh int max_clock, clock, i; 2659a94a63f0SWarner Losh 2660a94a63f0SWarner Losh if (proposed_clock == 0) 2661a94a63f0SWarner Losh return 0; 2662a94a63f0SWarner Losh max_clock = slot->max_clk; 2663a94a63f0SWarner Losh clock = max_clock; 2664a94a63f0SWarner Losh 2665a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2666505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2667a94a63f0SWarner Losh if (clock <= proposed_clock) 2668a94a63f0SWarner Losh break; 2669a94a63f0SWarner Losh clock >>= 1; 2670a94a63f0SWarner Losh } 2671a94a63f0SWarner Losh } else { 2672505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2673a94a63f0SWarner Losh if (clock <= proposed_clock) 2674a94a63f0SWarner Losh break; 2675a94a63f0SWarner Losh clock = max_clock / (i + 2); 2676a94a63f0SWarner Losh } 2677a94a63f0SWarner Losh } 2678a94a63f0SWarner Losh return clock; 2679a94a63f0SWarner Losh } 2680a94a63f0SWarner Losh 2681ab00a509SMarius Strobl static int 2682a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2683a94a63f0SWarner Losh { 2684a94a63f0SWarner Losh struct mmc_ios *ios; 2685ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2686ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2687a94a63f0SWarner Losh 2688a94a63f0SWarner Losh ios = &slot->host.ios; 2689a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2690a94a63f0SWarner Losh new_ios = &cts->ios; 2691a94a63f0SWarner Losh 2692a94a63f0SWarner Losh /* Update only requested fields */ 2693a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2694a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2695a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2696a94a63f0SWarner Losh } 2697a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2698a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2699a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2700a94a63f0SWarner Losh } 2701a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2702a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2703a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2704a94a63f0SWarner Losh } 2705a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2706a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2707a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2708a94a63f0SWarner Losh } 2709a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2710a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2711a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2712a94a63f0SWarner Losh } 2713a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2714a94a63f0SWarner Losh ios->timing = new_ios->timing; 2715a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2716a94a63f0SWarner Losh } 2717a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2718a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2719a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2720a94a63f0SWarner Losh } 2721a94a63f0SWarner Losh 2722a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2723a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2724a94a63f0SWarner Losh } 2725a94a63f0SWarner Losh 2726ab00a509SMarius Strobl static int 2727a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2728a94a63f0SWarner Losh { 2729a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2730a94a63f0SWarner Losh 2731a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2732a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2733a94a63f0SWarner Losh SDHCI_LOCK(slot); 2734a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2735a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2736a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2737a94a63f0SWarner Losh sdhci_init(slot); 2738a94a63f0SWarner Losh } 2739a94a63f0SWarner Losh /* Configure the bus. */ 2740a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2741a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2742a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2743a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2744a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2745a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2746a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2747a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2748a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2749a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2750a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2751a94a63f0SWarner Losh } else { 2752a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2753a94a63f0SWarner Losh } 2754a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2755a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2756a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2757a94a63f0SWarner Losh else 2758a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2759a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2760a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2761a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2762a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2763a94a63f0SWarner Losh 2764a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2765a94a63f0SWarner Losh return (0); 2766a94a63f0SWarner Losh } 2767a94a63f0SWarner Losh 2768ab00a509SMarius Strobl static int 2769a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2770a94a63f0SWarner Losh { 2771ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2772a94a63f0SWarner Losh 2773a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2774a94a63f0SWarner Losh 2775a94a63f0SWarner Losh SDHCI_LOCK(slot); 2776a94a63f0SWarner Losh /* if (slot->req != NULL) { 2777a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2778a94a63f0SWarner Losh return (EBUSY); 2779a94a63f0SWarner Losh } 2780a94a63f0SWarner Losh */ 2781aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 27825d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 27835d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2784a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2785a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 27865d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 27875d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 27885d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2789a94a63f0SWarner Losh } 2790a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2791a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2792a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2793a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2794a94a63f0SWarner Losh } 2795a94a63f0SWarner Losh slot->ccb = ccb; 2796a94a63f0SWarner Losh slot->flags = 0; 2797a94a63f0SWarner Losh sdhci_start(slot); 2798a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2799a94a63f0SWarner Losh if (dumping) { 2800a94a63f0SWarner Losh while (slot->ccb != NULL) { 2801a94a63f0SWarner Losh sdhci_generic_intr(slot); 2802a94a63f0SWarner Losh DELAY(10); 2803a94a63f0SWarner Losh } 2804a94a63f0SWarner Losh } 2805a94a63f0SWarner Losh return (0); 2806a94a63f0SWarner Losh } 280715c440e1SWarner Losh #endif /* MMCCAM */ 2808a94a63f0SWarner Losh 2809ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2810