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 707029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 717029da5cSPawel 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 */ 1357d8700bcSMarcin Wojtas static int sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps); 136ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot); 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; 629c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 630a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 631a94a63f0SWarner Losh #else 632aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 633831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 634aca38eabSMarius Strobl if (d) { 635aca38eabSMarius Strobl device_set_ivars(d, slot); 636aca38eabSMarius Strobl (void)device_probe_and_attach(d); 637aca38eabSMarius Strobl } 638a94a63f0SWarner Losh #endif 639831f5dcfSAlexander Motin } else 640831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 641831f5dcfSAlexander Motin } else { 642a94a63f0SWarner Losh #ifdef MMCCAM 643a94a63f0SWarner Losh if (slot->card_present == 1) { 644a94a63f0SWarner Losh #else 645831f5dcfSAlexander Motin if (slot->dev != NULL) { 646a94a63f0SWarner Losh #endif 647831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 648639f59f0SIan Lepore if (bootverbose || sdhci_debug) 649639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 6507e6ccea3SMarius Strobl d = slot->dev; 651831f5dcfSAlexander Motin slot->dev = NULL; 652a94a63f0SWarner Losh #ifdef MMCCAM 653a94a63f0SWarner Losh slot->card_present = 0; 654c7a49948SEmmanuel Vadot mmccam_start_discovery(slot->sim); 655a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 656a94a63f0SWarner Losh #else 657aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 658cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 659aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 660aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 661831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 662aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 663d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 664a94a63f0SWarner Losh #endif 665831f5dcfSAlexander Motin } else 666831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 667831f5dcfSAlexander Motin } 668831f5dcfSAlexander Motin } 669831f5dcfSAlexander Motin 670b8bf08b1SIan Lepore static void 671b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 672639f59f0SIan Lepore { 673639f59f0SIan Lepore bool was_present; 674639f59f0SIan Lepore 675639f59f0SIan Lepore /* 676639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 677639f59f0SIan Lepore * create the child device after a short delay. The delay is to 678639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 679639f59f0SIan Lepore * before the other pins have made good contact). 680639f59f0SIan Lepore * 681639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 682639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 683639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 684639f59f0SIan Lepore * that happens by deleting and recreating the child device. 685639f59f0SIan Lepore */ 686a94a63f0SWarner Losh #ifdef MMCCAM 687a94a63f0SWarner Losh was_present = slot->card_present; 688a94a63f0SWarner Losh #else 689639f59f0SIan Lepore was_present = slot->dev != NULL; 690a94a63f0SWarner Losh #endif 691639f59f0SIan Lepore if (!was_present && is_present) { 692639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 693639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 694639f59f0SIan Lepore } else if (was_present && !is_present) { 695639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 696639f59f0SIan Lepore } 697b8bf08b1SIan Lepore } 698b8bf08b1SIan Lepore 699b8bf08b1SIan Lepore void 700b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 701b8bf08b1SIan Lepore { 702b8bf08b1SIan Lepore 703b8bf08b1SIan Lepore SDHCI_LOCK(slot); 704b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 705639f59f0SIan Lepore SDHCI_UNLOCK(slot); 706639f59f0SIan Lepore } 707639f59f0SIan Lepore 708639f59f0SIan Lepore static void 709639f59f0SIan Lepore sdhci_card_poll(void *arg) 710639f59f0SIan Lepore { 711639f59f0SIan Lepore struct sdhci_slot *slot = arg; 712639f59f0SIan Lepore 713639f59f0SIan Lepore sdhci_handle_card_present(slot, 714639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 715639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 716639f59f0SIan Lepore sdhci_card_poll, slot); 717639f59f0SIan Lepore } 718639f59f0SIan Lepore 719ab00a509SMarius Strobl static int 7207d8700bcSMarcin Wojtas sdhci_dma_alloc(struct sdhci_slot *slot, uint32_t caps) 721ab00a509SMarius Strobl { 722ab00a509SMarius Strobl int err; 723ab00a509SMarius Strobl 724ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 725cd853791SKonstantin Belousov if (maxphys <= 1024 * 4) 726ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 727cd853791SKonstantin Belousov else if (maxphys <= 1024 * 8) 728ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 729cd853791SKonstantin Belousov else if (maxphys <= 1024 * 16) 730ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 731cd853791SKonstantin Belousov else if (maxphys <= 1024 * 32) 732ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 733cd853791SKonstantin Belousov else if (maxphys <= 1024 * 64) 734ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 735cd853791SKonstantin Belousov else if (maxphys <= 1024 * 128) 736ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 737cd853791SKonstantin Belousov else if (maxphys <= 1024 * 256) 738ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 739ab00a509SMarius Strobl else 740ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 741ab00a509SMarius Strobl } 742ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 743ab00a509SMarius Strobl 744ab00a509SMarius Strobl /* 745ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 746ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 747ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 748ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 749ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 750ab00a509SMarius Strobl * be aligned to the SDMA boundary. 751ab00a509SMarius Strobl */ 752ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 7537d8700bcSMarcin Wojtas 0, (caps & SDHCI_CAN_DO_64BIT) ? BUS_SPACE_MAXADDR : 7547d8700bcSMarcin Wojtas BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 755ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 756ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 757ab00a509SMarius Strobl if (err != 0) { 758ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 759ab00a509SMarius Strobl return (err); 760ab00a509SMarius Strobl } 761ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 762ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 763ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 764ab00a509SMarius Strobl if (err != 0) { 765ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 766ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 767ab00a509SMarius Strobl return (err); 768ab00a509SMarius Strobl } 769ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 770ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 771ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 772ab00a509SMarius Strobl &slot->paddr, 0); 773ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 774ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 775ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 776ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 777ab00a509SMarius Strobl if (err) 778ab00a509SMarius Strobl return (err); 779ab00a509SMarius Strobl else 780ab00a509SMarius Strobl return (EFAULT); 781ab00a509SMarius Strobl } 782ab00a509SMarius Strobl 783ab00a509SMarius Strobl return (0); 784ab00a509SMarius Strobl } 785ab00a509SMarius Strobl 786ab00a509SMarius Strobl static void 787ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 788ab00a509SMarius Strobl { 789ab00a509SMarius Strobl 790ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 791ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 792ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 793ab00a509SMarius Strobl } 794ab00a509SMarius Strobl 795d6b3aaf8SOleksandr Tymoshenko int 796d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 797831f5dcfSAlexander Motin { 798aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 799aca38eabSMarius Strobl kobj_method_t *kobj_method; 8000f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 801d6b3aaf8SOleksandr Tymoshenko int err; 802831f5dcfSAlexander Motin 803831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 804a94a63f0SWarner Losh 805d6b3aaf8SOleksandr Tymoshenko slot->num = num; 806d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 807d6b3aaf8SOleksandr Tymoshenko 808d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 809d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8100f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8118f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8120f34084fSMarius Strobl caps2 = slot->caps2; 8130f34084fSMarius Strobl } else { 814831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8150f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8160f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8170f34084fSMarius Strobl else 8180f34084fSMarius Strobl caps2 = 0; 8190f34084fSMarius Strobl } 8207fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8217fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8227fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 823ab00a509SMarius Strobl slot_printf(slot, 8247fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 8257fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 8267fcf4780SMarius Strobl return (ENXIO); 8277fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 8287fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 8297fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 8307fcf4780SMarius Strobl } 8317fcf4780SMarius Strobl } 832831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 83333aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 83487a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 83587a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 83633aad34dSOleksandr Tymoshenko else 83787a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 83887a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 83987a6a871SIan Lepore if (freq != 0) 84087a6a871SIan Lepore slot->max_clk = freq * 1000000; 84187a6a871SIan Lepore /* 84287a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 84387a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 84487a6a871SIan Lepore * with an assumption, so complain about it. 84587a6a871SIan Lepore */ 846831f5dcfSAlexander Motin if (slot->max_clk == 0) { 84787a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 848ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 8491bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 8501bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 851831f5dcfSAlexander Motin } 852a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 8538f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 8548f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 855a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 856a2832f9fSMarius Strobl slot->timeout_clk = 1000; 8578f3b7d56SOleksandr Tymoshenko } else { 8581bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 8591bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 8608f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 8618f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 8628f3b7d56SOleksandr Tymoshenko } 86387a6a871SIan Lepore /* 86487a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 86587a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 86687a6a871SIan Lepore * max timeout, but still mention it. 86787a6a871SIan Lepore */ 868831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 869ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 870ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 871ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 872831f5dcfSAlexander Motin } 873831f5dcfSAlexander Motin 87457677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 875831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 876831f5dcfSAlexander Motin slot->host.host_ocr = 0; 877831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 878831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 879831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 880831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 88149dfdf63SIan Lepore /* 88249dfdf63SIan Lepore * 1.8V VDD is not supposed to be used for removable cards. Hardware 88349dfdf63SIan Lepore * prior to v3.0 had no way to indicate embedded slots, but did 88449dfdf63SIan Lepore * sometimes support 1.8v for non-removable devices. 88549dfdf63SIan Lepore */ 88649dfdf63SIan Lepore if ((caps & SDHCI_CAN_VDD_180) && (slot->version < SDHCI_SPEC_300 || 88749dfdf63SIan Lepore (slot->opt & SDHCI_SLOT_EMBEDDED))) 888831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 889831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 890ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 891831f5dcfSAlexander Motin "support voltages.\n"); 892831f5dcfSAlexander Motin } 893aca38eabSMarius Strobl 8945652be30SMarcin Wojtas host_caps = slot->host.caps; 8955652be30SMarcin Wojtas host_caps |= MMC_CAP_4_BIT_DATA; 8962d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 8970f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 898831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 8990f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 90072dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9010f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 90272dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9030f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 904aca38eabSMarius Strobl 905aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9060f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9070f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9080f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9090f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9100f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9110f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9120f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9130f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9140f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9150f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9160f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9170f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9180f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9190f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9200f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9210f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 922835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 923835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 924835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 925aca38eabSMarius Strobl 926aca38eabSMarius Strobl /* 927aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 928aca38eabSMarius Strobl * default NULL implementation. 929aca38eabSMarius Strobl */ 930aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 931aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 932aca38eabSMarius Strobl kobj_desc); 933aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 934aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 935aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 936aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 937aca38eabSMarius Strobl 938aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 939aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 940aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 941aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 942aca38eabSMarius Strobl 943aca38eabSMarius Strobl /* 944aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 945aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 946aca38eabSMarius Strobl */ 947aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 948aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 949aca38eabSMarius Strobl kobj_desc); 950aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 951aca38eabSMarius Strobl goto no_tuning; 952aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 953aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 954aca38eabSMarius Strobl kobj_desc); 955aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 956aca38eabSMarius Strobl no_tuning: 957aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 958aca38eabSMarius Strobl } 959aca38eabSMarius Strobl 960aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 961aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 962aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 963aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 964aca38eabSMarius Strobl M_WAITOK); 965aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 966aca38eabSMarius Strobl M_WAITOK); 967aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 968aca38eabSMarius Strobl M_WAITOK); 969aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 970aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 971aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 972aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 973aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 974aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 975aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 976aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 977ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 978aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 979aca38eabSMarius Strobl slot->retune_count = 1; 980aca38eabSMarius Strobl } else if (slot->retune_count != 0) 981aca38eabSMarius Strobl slot->retune_count = 982aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 983aca38eabSMarius Strobl } 984aca38eabSMarius Strobl } 985aca38eabSMarius Strobl 986aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 987aca38eabSMarius Strobl 988aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 9890f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 9900f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 991aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 9920f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 9930f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 994aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 995aca38eabSMarius Strobl 996aca38eabSMarius Strobl /* 997aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 998aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 999aca38eabSMarius Strobl * generic SDHCI implementation. 1000aca38eabSMarius Strobl */ 1001aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1002aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1003aca38eabSMarius Strobl kobj_desc); 1004aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1005aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1006aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1007aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1008aca38eabSMarius Strobl 1009aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1010f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10110f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1012f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10130f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1014f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10150f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10160f34084fSMarius Strobl slot->host.caps = host_caps; 10170f34084fSMarius Strobl 1018831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1019831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1020831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1021d6b3aaf8SOleksandr Tymoshenko 1022d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1023831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1024d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1025831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1026a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1027a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1028831f5dcfSAlexander Motin 1029c3a0f75aSOleksandr Tymoshenko /* 1030c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1031c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1032c3a0f75aSOleksandr Tymoshenko */ 1033c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1034c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1035c3a0f75aSOleksandr Tymoshenko 1036ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 10377d8700bcSMarcin Wojtas err = sdhci_dma_alloc(slot, caps); 1038ab00a509SMarius Strobl if (err != 0) { 1039ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1040ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1041ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1042ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1043ab00a509SMarius Strobl } 1044ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1045ab00a509SMarius Strobl return (err); 1046ab00a509SMarius Strobl } 1047ab00a509SMarius Strobl } 1048ab00a509SMarius Strobl 10495b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 10500f34084fSMarius Strobl slot_printf(slot, 10517fcf4780SMarius Strobl "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 1052831f5dcfSAlexander Motin slot->max_clk / 1000000, 1053831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 10540f34084fSMarius Strobl (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 10550f34084fSMarius Strobl ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 1056831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 1057831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 10587fcf4780SMarius Strobl ((caps & SDHCI_CAN_VDD_180) && 10597fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 10600f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 10610f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 1062aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 1063aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 1064aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 10657fcf4780SMarius Strobl (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 10667fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 10677fcf4780SMarius Strobl (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 10687fcf4780SMarius Strobl "removable"); 10690f34084fSMarius Strobl if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 10700f34084fSMarius Strobl MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 10710f34084fSMarius Strobl slot_printf(slot, "eMMC:%s%s%s%s\n", 10720f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 10730f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 10740f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 10750f34084fSMarius Strobl ((host_caps & 10760f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 10770f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 10780f34084fSMarius Strobl " HS400ES" : ""); 10790f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 10800f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 10810f34084fSMarius Strobl slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 10820f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 10830f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 10840f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 10850f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 10860f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 1087aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) 1088aca38eabSMarius Strobl slot_printf(slot, "Re-tuning count %d secs, mode %d\n", 1089aca38eabSMarius Strobl slot->retune_count, slot->retune_mode + 1); 1090831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1091831f5dcfSAlexander Motin } 1092831f5dcfSAlexander Motin 1093ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1094ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1095ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 10964d52f81dSIan Lepore "timeout", CTLFLAG_RWTUN, &slot->timeout, 0, 1097ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1098831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1099639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1100639f59f0SIan Lepore sdhci_card_task, slot); 1101639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1102e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1103aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1104ba6fc1c7SLuiz Otavio O Souza 1105639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1106639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1107639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1108639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1109639f59f0SIan Lepore } 1110639f59f0SIan Lepore 1111aca38eabSMarius Strobl sdhci_init(slot); 1112aca38eabSMarius Strobl 1113831f5dcfSAlexander Motin return (0); 1114831f5dcfSAlexander Motin } 1115831f5dcfSAlexander Motin 1116d91f1a10SIlya Bakulin #ifndef MMCCAM 1117d6b3aaf8SOleksandr Tymoshenko void 1118d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1119831f5dcfSAlexander Motin { 11207e6ccea3SMarius Strobl 1121d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1122d6b3aaf8SOleksandr Tymoshenko } 1123d91f1a10SIlya Bakulin #endif 1124831f5dcfSAlexander Motin 1125d6b3aaf8SOleksandr Tymoshenko int 1126d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1127d6b3aaf8SOleksandr Tymoshenko { 1128831f5dcfSAlexander Motin device_t d; 1129831f5dcfSAlexander Motin 1130e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1131639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1132aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1133831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1134639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1135831f5dcfSAlexander Motin 1136831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1137831f5dcfSAlexander Motin d = slot->dev; 1138831f5dcfSAlexander Motin slot->dev = NULL; 1139831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1140831f5dcfSAlexander Motin if (d != NULL) 1141d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1142831f5dcfSAlexander Motin 1143831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1144831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 1145831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1146ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1147ab00a509SMarius Strobl sdhci_dma_free(slot); 1148aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1149aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1150aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1151aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1152aca38eabSMarius Strobl } 1153d6b3aaf8SOleksandr Tymoshenko 1154831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1155d6b3aaf8SOleksandr Tymoshenko 1156831f5dcfSAlexander Motin return (0); 1157831f5dcfSAlexander Motin } 1158831f5dcfSAlexander Motin 1159d6b3aaf8SOleksandr Tymoshenko int 1160d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 116192bf0e27SAlexander Motin { 11627e6ccea3SMarius Strobl 1163aca38eabSMarius Strobl /* 1164aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1165aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1166aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1167aca38eabSMarius Strobl */ 1168aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1169aca38eabSMarius Strobl SDHCI_LOCK(slot); 1170aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1171d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 1172aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 117392bf0e27SAlexander Motin 117492bf0e27SAlexander Motin return (0); 117592bf0e27SAlexander Motin } 117692bf0e27SAlexander Motin 1177d6b3aaf8SOleksandr Tymoshenko int 1178d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 117992bf0e27SAlexander Motin { 11807e6ccea3SMarius Strobl 1181aca38eabSMarius Strobl SDHCI_LOCK(slot); 1182d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1183aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 118492bf0e27SAlexander Motin 1185d6b3aaf8SOleksandr Tymoshenko return (0); 118692bf0e27SAlexander Motin } 118792bf0e27SAlexander Motin 118857677a3aSOleksandr Tymoshenko uint32_t 1189b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 119057677a3aSOleksandr Tymoshenko { 11917e6ccea3SMarius Strobl 119257677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 119357677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 119457677a3aSOleksandr Tymoshenko else 119557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 119657677a3aSOleksandr Tymoshenko } 119757677a3aSOleksandr Tymoshenko 11986e37fb2bSIan Lepore bool 1199b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 12006e37fb2bSIan Lepore { 12016e37fb2bSIan Lepore 1202639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1203639f59f0SIan Lepore return true; 1204639f59f0SIan Lepore 12056e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 12066e37fb2bSIan Lepore } 12076e37fb2bSIan Lepore 12080f34084fSMarius Strobl void 12090f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 12100f34084fSMarius Strobl { 1211ab00a509SMarius Strobl const struct mmc_ios *ios; 12120f34084fSMarius Strobl uint16_t hostctrl2; 12130f34084fSMarius Strobl 12140f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 12150f34084fSMarius Strobl return; 12160f34084fSMarius Strobl 1217aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 12180f34084fSMarius Strobl ios = &slot->host.ios; 12190f34084fSMarius Strobl sdhci_set_clock(slot, 0); 12200f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12210f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1222aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 12230f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 12240f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 12250f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1226aca38eabSMarius Strobl else 12270f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1228aca38eabSMarius Strobl } 12290f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 12300f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 12310f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 12320f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 12330f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 12340f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 12350f34084fSMarius Strobl else 12360f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 12370f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 12380f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 12390f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12400f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 12410f34084fSMarius Strobl } 12420f34084fSMarius Strobl 1243d6b3aaf8SOleksandr Tymoshenko int 1244d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1245831f5dcfSAlexander Motin { 1246831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1247831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1248831f5dcfSAlexander Motin 1249831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1250831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1251831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1252831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1253831f5dcfSAlexander Motin sdhci_init(slot); 1254831f5dcfSAlexander Motin } 1255831f5dcfSAlexander Motin /* Configure the bus. */ 1256831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1257831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 12582d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 12592d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1260831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12612d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 12622d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12632d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 12642d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 12652d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12662d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12672d1731b8SIan Lepore } else { 12682d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 12692d1731b8SIan Lepore } 12700f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1271bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1272831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1273831f5dcfSAlexander Motin else 1274831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1275831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 12760f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1277831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1278d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1279831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1280831f5dcfSAlexander Motin 1281831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1282831f5dcfSAlexander Motin return (0); 1283831f5dcfSAlexander Motin } 1284831f5dcfSAlexander Motin 12850f34084fSMarius Strobl int 12860f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 12870f34084fSMarius Strobl { 12880f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 12890f34084fSMarius Strobl enum mmc_vccq vccq; 12900f34084fSMarius Strobl int err; 12910f34084fSMarius Strobl uint16_t hostctrl2; 12920f34084fSMarius Strobl 12930f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 12940f34084fSMarius Strobl return (0); 12950f34084fSMarius Strobl 12960f34084fSMarius Strobl err = 0; 12970f34084fSMarius Strobl vccq = slot->host.ios.vccq; 12980f34084fSMarius Strobl SDHCI_LOCK(slot); 12990f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13000f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13010f34084fSMarius Strobl switch (vccq) { 13020f34084fSMarius Strobl case vccq_330: 13030f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13040f34084fSMarius Strobl goto done; 13050f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 13060f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13070f34084fSMarius Strobl DELAY(5000); 13080f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13090f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13100f34084fSMarius Strobl goto done; 13110f34084fSMarius Strobl err = EAGAIN; 13120f34084fSMarius Strobl break; 13130f34084fSMarius Strobl case vccq_180: 13140f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 13150f34084fSMarius Strobl err = EINVAL; 13160f34084fSMarius Strobl goto done; 13170f34084fSMarius Strobl } 13180f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13190f34084fSMarius Strobl goto done; 13200f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 13210f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13220f34084fSMarius Strobl DELAY(5000); 13230f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13240f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13250f34084fSMarius Strobl goto done; 13260f34084fSMarius Strobl err = EAGAIN; 13270f34084fSMarius Strobl break; 13280f34084fSMarius Strobl default: 13290f34084fSMarius Strobl slot_printf(slot, 13300f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 13310f34084fSMarius Strobl err = EINVAL; 13320f34084fSMarius Strobl break; 13330f34084fSMarius Strobl } 13340f34084fSMarius Strobl done: 13350f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 13360f34084fSMarius Strobl SDHCI_UNLOCK(slot); 13370f34084fSMarius Strobl return (err); 13380f34084fSMarius Strobl } 13390f34084fSMarius Strobl 1340aca38eabSMarius Strobl int 1341aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1342aca38eabSMarius Strobl { 1343aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1344ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1345aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1346aca38eabSMarius Strobl struct mmc_data *tune_data; 1347aca38eabSMarius Strobl uint32_t opcode; 1348aca38eabSMarius Strobl int err; 1349aca38eabSMarius Strobl 1350aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1351aca38eabSMarius Strobl return (0); 1352aca38eabSMarius Strobl 1353aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1354aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1355aca38eabSMarius Strobl SDHCI_LOCK(slot); 1356aca38eabSMarius Strobl switch (ios->timing) { 1357aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1358aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1359aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1360aca38eabSMarius Strobl return (EINVAL); 1361aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1362aca38eabSMarius Strobl /* 1363aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1364aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1365aca38eabSMarius Strobl * expected to be required. 1366aca38eabSMarius Strobl */ 1367aca38eabSMarius Strobl if (hs400) 1368aca38eabSMarius Strobl slot->retune_ticks = 0; 1369aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1370aca38eabSMarius Strobl break; 1371aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1372aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1373aca38eabSMarius Strobl break; 1374aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1375aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1376aca38eabSMarius Strobl break; 1377aca38eabSMarius Strobl /* FALLTHROUGH */ 1378aca38eabSMarius Strobl default: 1379aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1380aca38eabSMarius Strobl return (0); 1381aca38eabSMarius Strobl } 1382aca38eabSMarius Strobl 1383aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1384aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1385aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1386aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1387aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1388aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1389aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1390aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1391aca38eabSMarius Strobl MMC_TUNING_LEN; 1392aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1393aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1394aca38eabSMarius Strobl 1395aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1396aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1397aca38eabSMarius Strobl if (err == 0) { 1398aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1399aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1400cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1401aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1402aca38eabSMarius Strobl if (slot->retune_ticks) { 1403aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1404aca38eabSMarius Strobl sdhci_retune, slot); 1405aca38eabSMarius Strobl } 1406aca38eabSMarius Strobl } 1407aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1408aca38eabSMarius Strobl return (err); 1409aca38eabSMarius Strobl } 1410aca38eabSMarius Strobl 1411aca38eabSMarius Strobl int 1412aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1413aca38eabSMarius Strobl { 1414aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1415aca38eabSMarius Strobl int err; 1416aca38eabSMarius Strobl 1417aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1418aca38eabSMarius Strobl return (0); 1419aca38eabSMarius Strobl 1420aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1421aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1422aca38eabSMarius Strobl return (EINVAL); 1423aca38eabSMarius Strobl 1424aca38eabSMarius Strobl SDHCI_LOCK(slot); 1425aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1426aca38eabSMarius Strobl /* 1427aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1428aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1429aca38eabSMarius Strobl * with the host properly acquired, and 1430aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1431aca38eabSMarius Strobl * 1432aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1433aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1434aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1435aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1436aca38eabSMarius Strobl */ 1437aca38eabSMarius Strobl if (slot->retune_ticks) { 1438aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1439aca38eabSMarius Strobl sdhci_retune, slot); 1440aca38eabSMarius Strobl } 1441aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1442aca38eabSMarius Strobl return (err); 1443aca38eabSMarius Strobl } 1444aca38eabSMarius Strobl 1445aca38eabSMarius Strobl static int 1446aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1447aca38eabSMarius Strobl { 1448aca38eabSMarius Strobl struct mmc_request *tune_req; 1449aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1450aca38eabSMarius Strobl int i; 1451aca38eabSMarius Strobl uint32_t intmask; 1452aca38eabSMarius Strobl uint16_t hostctrl2; 1453aca38eabSMarius Strobl u_char opt; 1454aca38eabSMarius Strobl 1455aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1456aca38eabSMarius Strobl if (slot->req != NULL) 1457aca38eabSMarius Strobl return (EBUSY); 1458aca38eabSMarius Strobl 1459aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1460aca38eabSMarius Strobl opt = slot->opt; 1461aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1462aca38eabSMarius Strobl 1463aca38eabSMarius Strobl /* 1464aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1465aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1466aca38eabSMarius Strobl */ 1467aca38eabSMarius Strobl intmask = slot->intmask; 1468aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1469cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1470aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1471aca38eabSMarius Strobl 1472aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1473aca38eabSMarius Strobl if (reset) 1474aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1475aca38eabSMarius Strobl else 1476aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1477aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1478aca38eabSMarius Strobl 1479aca38eabSMarius Strobl tune_req = slot->tune_req; 1480aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1481aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1482aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1483aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1484aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1485aca38eabSMarius Strobl tune_req->done_data = slot; 1486aca38eabSMarius Strobl slot->req = tune_req; 1487aca38eabSMarius Strobl slot->flags = 0; 1488aca38eabSMarius Strobl sdhci_start(slot); 1489aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1490aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1491aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1492aca38eabSMarius Strobl break; 1493aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1494aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1495aca38eabSMarius Strobl break; 1496aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1497aca38eabSMarius Strobl DELAY(1000); 1498aca38eabSMarius Strobl } 1499aca38eabSMarius Strobl 150078f8baa8SMarius Strobl /* 150178f8baa8SMarius Strobl * Restore DMA usage and interrupts. 150278f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 150378f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 150478f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 150578f8baa8SMarius Strobl * doesn't lose these. 150678f8baa8SMarius Strobl */ 1507aca38eabSMarius Strobl slot->opt = opt; 1508aca38eabSMarius Strobl slot->intmask = intmask; 150978f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 151078f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1511aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1512aca38eabSMarius Strobl 1513aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1514aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1515aca38eabSMarius Strobl slot->retune_req = 0; 1516aca38eabSMarius Strobl return (0); 1517aca38eabSMarius Strobl } 1518aca38eabSMarius Strobl 1519aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1520aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1521aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1522aca38eabSMarius Strobl sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1523aca38eabSMarius Strobl return (EIO); 1524aca38eabSMarius Strobl } 1525aca38eabSMarius Strobl 1526aca38eabSMarius Strobl static void 1527aca38eabSMarius Strobl sdhci_retune(void *arg) 1528aca38eabSMarius Strobl { 1529aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1530aca38eabSMarius Strobl 1531aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1532aca38eabSMarius Strobl } 1533aca38eabSMarius Strobl 1534a94a63f0SWarner Losh #ifdef MMCCAM 1535a94a63f0SWarner Losh static void 1536a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1537a94a63f0SWarner Losh { 1538a94a63f0SWarner Losh union ccb *ccb; 153915c440e1SWarner Losh 1540aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 154115c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1542a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1543a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1544a94a63f0SWarner Losh ccb = slot->ccb; 1545a94a63f0SWarner Losh slot->ccb = NULL; 1546a94a63f0SWarner Losh slot->curcmd = NULL; 1547a94a63f0SWarner Losh 1548a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1549a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1550a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1551a94a63f0SWarner Losh 1552a94a63f0SWarner Losh ccb->ccb_h.status = 1553a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1554a94a63f0SWarner Losh xpt_done(ccb); 1555a94a63f0SWarner Losh } 1556a94a63f0SWarner Losh } 1557a94a63f0SWarner Losh #else 1558831f5dcfSAlexander Motin static void 1559e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1560e64f01a9SIan Lepore { 1561e64f01a9SIan Lepore struct mmc_request *req; 1562e64f01a9SIan Lepore 1563e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1564e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1565e64f01a9SIan Lepore req = slot->req; 1566e64f01a9SIan Lepore slot->req = NULL; 1567e64f01a9SIan Lepore slot->curcmd = NULL; 1568e64f01a9SIan Lepore req->done(req); 1569e64f01a9SIan Lepore } 1570e64f01a9SIan Lepore } 1571a94a63f0SWarner Losh #endif 1572e64f01a9SIan Lepore 1573e64f01a9SIan Lepore static void 1574aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1575aca38eabSMarius Strobl { 1576aca38eabSMarius Strobl struct sdhci_slot *slot; 1577aca38eabSMarius Strobl 1578aca38eabSMarius Strobl slot = req->done_data; 1579aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1580aca38eabSMarius Strobl wakeup(req); 1581aca38eabSMarius Strobl } 1582aca38eabSMarius Strobl 1583aca38eabSMarius Strobl static void 1584e64f01a9SIan Lepore sdhci_timeout(void *arg) 1585e64f01a9SIan Lepore { 1586e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1587e64f01a9SIan Lepore 1588e64f01a9SIan Lepore if (slot->curcmd != NULL) { 15897e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 15907e586643SIan Lepore sdhci_dumpregs(slot); 1591a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1592e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1593e64f01a9SIan Lepore sdhci_req_done(slot); 15947e586643SIan Lepore } else { 15957e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1596e64f01a9SIan Lepore } 1597e64f01a9SIan Lepore } 1598e64f01a9SIan Lepore 1599e64f01a9SIan Lepore static void 1600ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1601831f5dcfSAlexander Motin { 1602831f5dcfSAlexander Motin uint16_t mode; 1603831f5dcfSAlexander Motin 1604831f5dcfSAlexander Motin if (data == NULL) 1605831f5dcfSAlexander Motin return; 1606831f5dcfSAlexander Motin 1607831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 16085d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1609831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 16105d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 16116dea80e6SMarius Strobl #ifdef MMCCAM 16126dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 16136dea80e6SMarius Strobl #else 16140519c933SMarius Strobl slot->req->stop != NULL && 16156dea80e6SMarius Strobl #endif 16166dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 16176dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 16186dea80e6SMarius Strobl } 1619831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1620831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1621831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1622831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1623831f5dcfSAlexander Motin 1624831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1625831f5dcfSAlexander Motin } 1626831f5dcfSAlexander Motin 1627831f5dcfSAlexander Motin static void 1628831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1629831f5dcfSAlexander Motin { 1630831f5dcfSAlexander Motin int flags, timeout; 163190993663SIan Lepore uint32_t mask; 1632831f5dcfSAlexander Motin 1633831f5dcfSAlexander Motin slot->curcmd = cmd; 1634831f5dcfSAlexander Motin slot->cmd_done = 0; 1635831f5dcfSAlexander Motin 1636831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1637831f5dcfSAlexander Motin 1638831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1639831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1640831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1641831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1642e64f01a9SIan Lepore sdhci_req_done(slot); 1643831f5dcfSAlexander Motin return; 1644831f5dcfSAlexander Motin } 1645831f5dcfSAlexander Motin 1646b440e965SMarius Strobl /* 1647b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1648b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1649b440e965SMarius Strobl */ 16506e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1651d8208d9eSAlexander Motin slot->power == 0 || 1652d8208d9eSAlexander Motin slot->clock == 0) { 1653a94a63f0SWarner Losh slot_printf(slot, 1654a94a63f0SWarner Losh "Cannot issue a command (power=%d clock=%d)", 1655a94a63f0SWarner Losh slot->power, slot->clock); 1656831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1657e64f01a9SIan Lepore sdhci_req_done(slot); 1658831f5dcfSAlexander Motin return; 1659831f5dcfSAlexander Motin } 1660831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1661831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1662831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1663a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1664831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1665aca38eabSMarius Strobl /* 1666aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1667aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1668aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1669aca38eabSMarius Strobl */ 1670a94a63f0SWarner Losh #ifdef MMCCAM 1671aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1672a94a63f0SWarner Losh #else 1673aca38eabSMarius Strobl if (cmd == slot->req->stop || 1674a94a63f0SWarner Losh #endif 1675aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1676aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1677aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 16788775ab45SIan Lepore /* 16798775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 16808775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 16818775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 16828775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 16838775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 16848775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 16858775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 16868775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 16878775ab45SIan Lepore */ 16888775ab45SIan Lepore timeout = 250; 168990993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1690831f5dcfSAlexander Motin if (timeout == 0) { 1691831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1692831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1693831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1694831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1695e64f01a9SIan Lepore sdhci_req_done(slot); 1696831f5dcfSAlexander Motin return; 1697831f5dcfSAlexander Motin } 1698831f5dcfSAlexander Motin timeout--; 1699831f5dcfSAlexander Motin DELAY(1000); 1700831f5dcfSAlexander Motin } 1701831f5dcfSAlexander Motin 1702831f5dcfSAlexander Motin /* Prepare command flags. */ 1703831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1704831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1705831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1706831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1707831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1708831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1709831f5dcfSAlexander Motin else 1710831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1711831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1712831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1713831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1714831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1715a94a63f0SWarner Losh if (cmd->data != NULL) 1716831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1717831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1718831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1719831f5dcfSAlexander Motin /* Prepare data. */ 1720831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1721831f5dcfSAlexander Motin /* 1722831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1723831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1724831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1725831f5dcfSAlexander Motin */ 1726831f5dcfSAlexander Motin if (slot->data_done == 0) { 1727831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1728831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1729831f5dcfSAlexander Motin } 1730831f5dcfSAlexander Motin /* Set command argument. */ 1731831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1732831f5dcfSAlexander Motin /* Set data transfer mode. */ 1733831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1734aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 17358adf4202SBjoern A. Zeeb slot_printf(slot, "Starting command opcode %#04x flags %#04x\n", 17368adf4202SBjoern A. Zeeb cmd->opcode, flags); 17378adf4202SBjoern A. Zeeb 1738831f5dcfSAlexander Motin /* Start command. */ 1739d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1740a6873fd1SIan Lepore /* Start timeout callout. */ 1741ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1742ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1743831f5dcfSAlexander Motin } 1744831f5dcfSAlexander Motin 1745831f5dcfSAlexander Motin static void 1746831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1747831f5dcfSAlexander Motin { 1748831f5dcfSAlexander Motin int i; 17491bacf3beSMarius Strobl uint32_t val; 17501bacf3beSMarius Strobl uint8_t extra; 1751831f5dcfSAlexander Motin 1752aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 17538adf4202SBjoern A. Zeeb slot_printf(slot, "%s: called, err %d flags %#04x\n", 1754a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1755831f5dcfSAlexander Motin slot->cmd_done = 1; 175672dec079SMarius Strobl /* 175772dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1758831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 175972dec079SMarius Strobl * happened first. 176072dec079SMarius Strobl */ 1761aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1762aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1763aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1764aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1765831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1766831f5dcfSAlexander Motin if (slot->curcmd->error) { 1767aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1768aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1769831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1770831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1771831f5dcfSAlexander Motin sdhci_start(slot); 1772831f5dcfSAlexander Motin return; 1773831f5dcfSAlexander Motin } 1774831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1775831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1776831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1777831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 17781bacf3beSMarius Strobl extra = 0; 1779831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 17801bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 17811bacf3beSMarius Strobl if (slot->quirks & 17821bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1783677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1784677ee494SIan Lepore else { 1785677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1786677ee494SIan Lepore (val << 8) | extra; 1787831f5dcfSAlexander Motin extra = val >> 24; 1788831f5dcfSAlexander Motin } 1789677ee494SIan Lepore } 1790831f5dcfSAlexander Motin } else 1791831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1792831f5dcfSAlexander Motin } 1793aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 17948adf4202SBjoern A. Zeeb slot_printf(slot, "Resp: %#04x %#04x %#04x %#04x\n", 1795a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1796a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1797a94a63f0SWarner Losh 1798831f5dcfSAlexander Motin /* If data ready - finish. */ 1799831f5dcfSAlexander Motin if (slot->data_done) 1800831f5dcfSAlexander Motin sdhci_start(slot); 1801831f5dcfSAlexander Motin } 1802831f5dcfSAlexander Motin 1803831f5dcfSAlexander Motin static void 1804ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1805831f5dcfSAlexander Motin { 1806ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1807831f5dcfSAlexander Motin uint8_t div; 1808831f5dcfSAlexander Motin 1809831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1810831f5dcfSAlexander Motin slot->data_done = 1; 1811831f5dcfSAlexander Motin return; 1812831f5dcfSAlexander Motin } 1813831f5dcfSAlexander Motin 1814831f5dcfSAlexander Motin slot->data_done = 0; 1815831f5dcfSAlexander Motin 1816831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1817831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1818ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1819ceb9e9f7SIan Lepore div = 0xE; 1820ceb9e9f7SIan Lepore } else { 1821831f5dcfSAlexander Motin target_timeout = 1000000; 1822831f5dcfSAlexander Motin div = 0; 1823831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1824ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1825ceb9e9f7SIan Lepore ++div; 1826831f5dcfSAlexander Motin current_timeout <<= 1; 1827831f5dcfSAlexander Motin } 1828831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1829ceb9e9f7SIan Lepore if (div < 0xE && 1830ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1831ceb9e9f7SIan Lepore ++div; 1832831f5dcfSAlexander Motin } 1833ceb9e9f7SIan Lepore } 1834831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1835831f5dcfSAlexander Motin 1836831f5dcfSAlexander Motin if (data == NULL) 1837831f5dcfSAlexander Motin return; 1838831f5dcfSAlexander Motin 1839831f5dcfSAlexander Motin /* Use DMA if possible. */ 1840831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1841831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1842ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1843d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1844831f5dcfSAlexander Motin (data->len <= 512)) 1845831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1846831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1847d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1848831f5dcfSAlexander Motin ((data->len) & 0x3)) 1849831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1850831f5dcfSAlexander Motin /* Load DMA buffer. */ 1851831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1852ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1853831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1854ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1855ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1856831f5dcfSAlexander Motin else { 1857ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1858ab00a509SMarius Strobl sdma_bbufsz)); 1859ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1860ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1861831f5dcfSAlexander Motin } 1862831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1863ab00a509SMarius Strobl /* 1864ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1865ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1866ab00a509SMarius Strobl */ 1867ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1868831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1869831f5dcfSAlexander Motin else 1870831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1871831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1872831f5dcfSAlexander Motin } 1873831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1874831f5dcfSAlexander Motin slot->offset = 0; 18755d5ae066SIlya Bakulin #ifdef MMCCAM 18765d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 18775d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 18785d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 18795d5ae066SIlya Bakulin blkcnt = data->block_count; 18805d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 18815d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 18825d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 18835d5ae066SIlya Bakulin } else 18845d5ae066SIlya Bakulin #endif 18855d5ae066SIlya Bakulin { 1886ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 1887ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 1888ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 18895d5ae066SIlya Bakulin } 18905d5ae066SIlya Bakulin 18915d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 1892ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 1893aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1894ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 1895ab00a509SMarius Strobl blksz, blkcnt); 1896831f5dcfSAlexander Motin } 1897831f5dcfSAlexander Motin 1898c3a0f75aSOleksandr Tymoshenko void 1899831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1900831f5dcfSAlexander Motin { 1901831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 19027e6ccea3SMarius Strobl size_t left; 1903831f5dcfSAlexander Motin 1904831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1905ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1906831f5dcfSAlexander Motin * happened first. */ 1907831f5dcfSAlexander Motin if (!slot->cmd_done) { 1908831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1909831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1910831f5dcfSAlexander Motin } 1911831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1912915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1913915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 1914831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 19157e6ccea3SMarius Strobl left = data->len - slot->offset; 1916ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1917ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1918831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1919ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 1920831f5dcfSAlexander Motin } else 1921ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1922ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1923831f5dcfSAlexander Motin } 1924a98788edSIan Lepore slot->data_done = 1; 1925831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1926831f5dcfSAlexander Motin if (slot->curcmd->error) { 1927aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1928aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1929831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1930831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1931831f5dcfSAlexander Motin sdhci_start(slot); 1932831f5dcfSAlexander Motin return; 1933831f5dcfSAlexander Motin } 1934831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1935831f5dcfSAlexander Motin if (slot->cmd_done) 1936831f5dcfSAlexander Motin sdhci_start(slot); 1937831f5dcfSAlexander Motin } 1938831f5dcfSAlexander Motin 1939a94a63f0SWarner Losh #ifdef MMCCAM 1940a94a63f0SWarner Losh static void 1941a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 1942a94a63f0SWarner Losh { 1943a94a63f0SWarner Losh union ccb *ccb; 1944ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 1945a94a63f0SWarner Losh 1946a94a63f0SWarner Losh ccb = slot->ccb; 1947a94a63f0SWarner Losh if (ccb == NULL) 1948a94a63f0SWarner Losh return; 1949a94a63f0SWarner Losh 1950a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1951a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 1952a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 1953a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 1954a94a63f0SWarner Losh return; 1955a94a63f0SWarner Losh } 1956a94a63f0SWarner Losh 1957a94a63f0SWarner Losh /* 1958a94a63f0SWarner Losh * Old stack doesn't use this! 1959a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 1960a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 1961a94a63f0SWarner Losh * Not using this code does no harm... 1962a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 1963a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 1964a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 1965a94a63f0SWarner Losh return; 1966a94a63f0SWarner Losh } 1967a94a63f0SWarner Losh */ 1968aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1969a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 1970a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 1971a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1972a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD); 1973a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_DATA); 1974a94a63f0SWarner Losh } 1975a94a63f0SWarner Losh 1976a94a63f0SWarner Losh sdhci_req_done(slot); 1977a94a63f0SWarner Losh } 1978a94a63f0SWarner Losh #else 1979831f5dcfSAlexander Motin static void 1980831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1981831f5dcfSAlexander Motin { 1982ab00a509SMarius Strobl const struct mmc_request *req; 1983831f5dcfSAlexander Motin 1984831f5dcfSAlexander Motin req = slot->req; 1985831f5dcfSAlexander Motin if (req == NULL) 1986831f5dcfSAlexander Motin return; 1987831f5dcfSAlexander Motin 1988831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1989831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1990831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1991831f5dcfSAlexander Motin return; 1992831f5dcfSAlexander Motin } 1993915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 1994915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 1995831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1996831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1997831f5dcfSAlexander Motin return; 1998831f5dcfSAlexander Motin } 1999aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 20005b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 20015b69a497SAlexander Motin if (!req->cmd->error && 2002915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2003915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2004915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2005831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2006831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 2007831f5dcfSAlexander Motin } 2008831f5dcfSAlexander Motin 2009e64f01a9SIan Lepore sdhci_req_done(slot); 2010831f5dcfSAlexander Motin } 2011a94a63f0SWarner Losh #endif 2012831f5dcfSAlexander Motin 2013d6b3aaf8SOleksandr Tymoshenko int 2014b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2015b440e965SMarius Strobl struct mmc_request *req) 2016831f5dcfSAlexander Motin { 2017831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2018831f5dcfSAlexander Motin 2019831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2020831f5dcfSAlexander Motin if (slot->req != NULL) { 2021831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2022831f5dcfSAlexander Motin return (EBUSY); 2023831f5dcfSAlexander Motin } 2024aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 20251bacf3beSMarius Strobl slot_printf(slot, 20261bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2027831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 20285b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 20295b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 20305b69a497SAlexander Motin } 2031831f5dcfSAlexander Motin slot->req = req; 2032831f5dcfSAlexander Motin slot->flags = 0; 2033831f5dcfSAlexander Motin sdhci_start(slot); 2034831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2035bea2dca2SAlexander Motin if (dumping) { 2036bea2dca2SAlexander Motin while (slot->req != NULL) { 2037d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2038bea2dca2SAlexander Motin DELAY(10); 2039bea2dca2SAlexander Motin } 2040bea2dca2SAlexander Motin } 2041831f5dcfSAlexander Motin return (0); 2042831f5dcfSAlexander Motin } 2043831f5dcfSAlexander Motin 2044d6b3aaf8SOleksandr Tymoshenko int 2045b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2046831f5dcfSAlexander Motin { 2047831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2048831f5dcfSAlexander Motin uint32_t val; 2049831f5dcfSAlexander Motin 2050831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2051831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2052831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2053831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2054831f5dcfSAlexander Motin } 2055831f5dcfSAlexander Motin 2056d6b3aaf8SOleksandr Tymoshenko int 2057b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2058831f5dcfSAlexander Motin { 2059831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2060831f5dcfSAlexander Motin int err = 0; 2061831f5dcfSAlexander Motin 2062831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2063831f5dcfSAlexander Motin while (slot->bus_busy) 2064d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2065831f5dcfSAlexander Motin slot->bus_busy++; 2066831f5dcfSAlexander Motin /* Activate led. */ 2067831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2068831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2069831f5dcfSAlexander Motin return (err); 2070831f5dcfSAlexander Motin } 2071831f5dcfSAlexander Motin 2072d6b3aaf8SOleksandr Tymoshenko int 2073b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2074831f5dcfSAlexander Motin { 2075831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2076831f5dcfSAlexander Motin 2077831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2078831f5dcfSAlexander Motin /* Deactivate led. */ 2079831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2080831f5dcfSAlexander Motin slot->bus_busy--; 2081831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2082d493985aSAlexander Motin wakeup(slot); 2083831f5dcfSAlexander Motin return (0); 2084831f5dcfSAlexander Motin } 2085831f5dcfSAlexander Motin 2086831f5dcfSAlexander Motin static void 2087831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2088831f5dcfSAlexander Motin { 2089831f5dcfSAlexander Motin 2090831f5dcfSAlexander Motin if (!slot->curcmd) { 2091831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2092831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2093831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2094831f5dcfSAlexander Motin return; 2095831f5dcfSAlexander Motin } 2096831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2097831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2098831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2099831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2100831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2101831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2102831f5dcfSAlexander Motin 2103831f5dcfSAlexander Motin sdhci_finish_command(slot); 2104831f5dcfSAlexander Motin } 2105831f5dcfSAlexander Motin 2106831f5dcfSAlexander Motin static void 2107831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2108831f5dcfSAlexander Motin { 21091bacf3beSMarius Strobl struct mmc_data *data; 211015c440e1SWarner Losh size_t left; 2111ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2112831f5dcfSAlexander Motin 2113831f5dcfSAlexander Motin if (!slot->curcmd) { 2114831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2115831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2116831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2117831f5dcfSAlexander Motin return; 2118831f5dcfSAlexander Motin } 2119831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2120831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2121831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2122831f5dcfSAlexander Motin "there is no active data operation.\n", 2123831f5dcfSAlexander Motin intmask); 2124831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2125831f5dcfSAlexander Motin return; 2126831f5dcfSAlexander Motin } 2127831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2128831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2129acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2130831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2131831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2132831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2133831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2134831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2135831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2136831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2137831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2138831f5dcfSAlexander Motin } 2139831f5dcfSAlexander Motin if (slot->curcmd->error) { 2140831f5dcfSAlexander Motin /* No need to continue after any error. */ 2141a98788edSIan Lepore goto done; 2142831f5dcfSAlexander Motin } 2143831f5dcfSAlexander Motin 2144aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2145aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2146aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2147aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2148aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2149aca38eabSMarius Strobl sdhci_finish_command(slot); 2150aca38eabSMarius Strobl sdhci_finish_data(slot); 2151aca38eabSMarius Strobl return; 2152aca38eabSMarius Strobl } 2153831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2154c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2155c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2156c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 21571bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 21581bacf3beSMarius Strobl &intmask); 2159c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2160c3a0f75aSOleksandr Tymoshenko } else 2161831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2162c3a0f75aSOleksandr Tymoshenko } 2163831f5dcfSAlexander Motin /* Handle DMA border. */ 2164831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 21651bacf3beSMarius Strobl data = slot->curcmd->data; 2166ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2167831f5dcfSAlexander Motin 2168831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2169831f5dcfSAlexander Motin left = data->len - slot->offset; 2170831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2171831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2172831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2173831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2174ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2175831f5dcfSAlexander Motin } else { 2176831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2177831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2178831f5dcfSAlexander Motin } 2179831f5dcfSAlexander Motin /* ... and reload it again. */ 2180ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2181831f5dcfSAlexander Motin left = data->len - slot->offset; 2182831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2183831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2184831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2185831f5dcfSAlexander Motin } else { 2186831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2187ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2188831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2189831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2190831f5dcfSAlexander Motin } 2191ab00a509SMarius Strobl /* 2192ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2193ab00a509SMarius Strobl * bounce buffer. 2194ab00a509SMarius Strobl */ 2195ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2196831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2197831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2198831f5dcfSAlexander Motin } 2199831f5dcfSAlexander Motin /* Restart DMA. */ 2200831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2201831f5dcfSAlexander Motin } 2202831f5dcfSAlexander Motin /* We have got all data. */ 2203c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2204c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2205c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2206c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2207c3a0f75aSOleksandr Tymoshenko } else 2208831f5dcfSAlexander Motin sdhci_finish_data(slot); 2209831f5dcfSAlexander Motin } 2210a98788edSIan Lepore done: 2211a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2212a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2213a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2214a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2215a98788edSIan Lepore } else 2216a98788edSIan Lepore sdhci_finish_data(slot); 2217a98788edSIan Lepore } 2218c3a0f75aSOleksandr Tymoshenko } 2219831f5dcfSAlexander Motin 2220831f5dcfSAlexander Motin static void 22216dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2222831f5dcfSAlexander Motin { 2223831f5dcfSAlexander Motin 2224831f5dcfSAlexander Motin if (!slot->curcmd) { 2225831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 22266dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2227831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2228831f5dcfSAlexander Motin return; 2229831f5dcfSAlexander Motin } 22306dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2231831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2232831f5dcfSAlexander Motin } 2233831f5dcfSAlexander Motin 2234d6b3aaf8SOleksandr Tymoshenko void 2235d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2236831f5dcfSAlexander Motin { 22372b96b955SJustin Hibbits uint32_t intmask, present; 22386dea80e6SMarius Strobl uint16_t val16; 2239831f5dcfSAlexander Motin 2240831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2241831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2242831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2243831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2244831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2245d6b3aaf8SOleksandr Tymoshenko return; 2246831f5dcfSAlexander Motin } 2247aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 22485b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 22495b69a497SAlexander Motin 2250aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2251aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 22526dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2253aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2254aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2255aca38eabSMarius Strobl if (slot->curcmd) { 2256aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2257aca38eabSMarius Strobl sdhci_finish_command(slot); 2258aca38eabSMarius Strobl } 2259aca38eabSMarius Strobl } 2260aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2261aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2262aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2263831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2264831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2265639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 22662b96b955SJustin Hibbits slot->intmask &= 22672b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 22682b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 22692b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 22702b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 22712b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2272831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2273831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2274b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2275831f5dcfSAlexander Motin } 2276831f5dcfSAlexander Motin /* Handle command interrupts. */ 2277831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2278831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2279831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2280831f5dcfSAlexander Motin } 2281831f5dcfSAlexander Motin /* Handle data interrupts. */ 2282831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2283831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 22847e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 22857e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2286831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2287831f5dcfSAlexander Motin } 2288831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2289831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 22906dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 22916dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2292831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 22936dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2294831f5dcfSAlexander Motin } 2295831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2296831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2297831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2298aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2299831f5dcfSAlexander Motin } 2300aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2301aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2302aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2303831f5dcfSAlexander Motin /* The rest is unknown. */ 2304831f5dcfSAlexander Motin if (intmask) { 2305831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2306831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2307831f5dcfSAlexander Motin intmask); 2308831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2309831f5dcfSAlexander Motin } 2310831f5dcfSAlexander Motin 2311831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2312831f5dcfSAlexander Motin } 2313831f5dcfSAlexander Motin 2314d6b3aaf8SOleksandr Tymoshenko int 23151bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 23161bacf3beSMarius Strobl uintptr_t *result) 2317831f5dcfSAlexander Motin { 2318ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2319831f5dcfSAlexander Motin 2320831f5dcfSAlexander Motin switch (which) { 2321831f5dcfSAlexander Motin default: 2322831f5dcfSAlexander Motin return (EINVAL); 2323831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2324bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2325831f5dcfSAlexander Motin break; 2326831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2327bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2328831f5dcfSAlexander Motin break; 2329831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2330bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2331831f5dcfSAlexander Motin break; 2332831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2333bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2334831f5dcfSAlexander Motin break; 2335831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2336bcd91d25SJayachandran C. *result = slot->host.f_min; 2337831f5dcfSAlexander Motin break; 2338831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2339bcd91d25SJayachandran C. *result = slot->host.f_max; 2340831f5dcfSAlexander Motin break; 2341831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2342bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2343831f5dcfSAlexander Motin break; 2344831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2345bcd91d25SJayachandran C. *result = slot->host.mode; 2346831f5dcfSAlexander Motin break; 2347831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2348bcd91d25SJayachandran C. *result = slot->host.ocr; 2349831f5dcfSAlexander Motin break; 2350831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2351bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2352831f5dcfSAlexander Motin break; 2353831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2354bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2355831f5dcfSAlexander Motin break; 2356aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2357aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2358aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2359aca38eabSMarius Strobl *result = retune_req_reset; 2360aca38eabSMarius Strobl break; 2361aca38eabSMarius Strobl } 2362aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2363aca38eabSMarius Strobl *result = retune_req_normal; 2364aca38eabSMarius Strobl break; 2365aca38eabSMarius Strobl } 2366aca38eabSMarius Strobl } 2367aca38eabSMarius Strobl *result = retune_req_none; 2368aca38eabSMarius Strobl break; 23690f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 23700f34084fSMarius Strobl *result = slot->host.ios.vccq; 23710f34084fSMarius Strobl break; 2372831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2373bcd91d25SJayachandran C. *result = slot->host.caps; 2374831f5dcfSAlexander Motin break; 2375831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2376bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2377831f5dcfSAlexander Motin break; 23783a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2379aca38eabSMarius Strobl /* 2380aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2381aca38eabSMarius Strobl * per read/write command to 4 MiB. 2382aca38eabSMarius Strobl */ 2383aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2384aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2385aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2386aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2387aca38eabSMarius Strobl break; 2388aca38eabSMarius Strobl } 2389bcd91d25SJayachandran C. *result = 65535; 23903a4a2557SAlexander Motin break; 239172dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 239272dec079SMarius Strobl /* 239372dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 239472dec079SMarius Strobl */ 239572dec079SMarius Strobl *result = 1000000; 239672dec079SMarius Strobl break; 2397831f5dcfSAlexander Motin } 2398831f5dcfSAlexander Motin return (0); 2399831f5dcfSAlexander Motin } 2400831f5dcfSAlexander Motin 2401d6b3aaf8SOleksandr Tymoshenko int 24021bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 24031bacf3beSMarius Strobl uintptr_t value) 2404831f5dcfSAlexander Motin { 2405831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2406b440e965SMarius Strobl uint32_t clock, max_clock; 2407b440e965SMarius Strobl int i; 2408831f5dcfSAlexander Motin 240915c440e1SWarner Losh if (sdhci_debug > 1) 241015c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2411831f5dcfSAlexander Motin switch (which) { 2412831f5dcfSAlexander Motin default: 2413831f5dcfSAlexander Motin return (EINVAL); 2414831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2415831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2416831f5dcfSAlexander Motin break; 2417831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2418831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2419831f5dcfSAlexander Motin break; 2420831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2421831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2422831f5dcfSAlexander Motin break; 2423831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2424831f5dcfSAlexander Motin if (value > 0) { 242557677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 242657677a3aSOleksandr Tymoshenko clock = max_clock; 242757677a3aSOleksandr Tymoshenko 242857677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 242957677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 243057677a3aSOleksandr Tymoshenko i <<= 1) { 2431831f5dcfSAlexander Motin if (clock <= value) 2432831f5dcfSAlexander Motin break; 2433831f5dcfSAlexander Motin clock >>= 1; 2434831f5dcfSAlexander Motin } 2435b440e965SMarius Strobl } else { 243657677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 243757677a3aSOleksandr Tymoshenko i += 2) { 243857677a3aSOleksandr Tymoshenko if (clock <= value) 243957677a3aSOleksandr Tymoshenko break; 244057677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 244157677a3aSOleksandr Tymoshenko } 244257677a3aSOleksandr Tymoshenko } 244357677a3aSOleksandr Tymoshenko 2444831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2445831f5dcfSAlexander Motin } else 2446831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2447831f5dcfSAlexander Motin break; 2448831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2449831f5dcfSAlexander Motin slot->host.mode = value; 2450831f5dcfSAlexander Motin break; 2451831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2452831f5dcfSAlexander Motin slot->host.ocr = value; 2453831f5dcfSAlexander Motin break; 2454831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2455831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2456831f5dcfSAlexander Motin break; 2457831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2458831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2459831f5dcfSAlexander Motin break; 24600f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24610f34084fSMarius Strobl slot->host.ios.vccq = value; 24620f34084fSMarius Strobl break; 2463831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2464831f5dcfSAlexander Motin slot->host.ios.timing = value; 2465831f5dcfSAlexander Motin break; 2466831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2467831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2468831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2469831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 24703a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2471aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2472831f5dcfSAlexander Motin return (EINVAL); 2473831f5dcfSAlexander Motin } 2474831f5dcfSAlexander Motin return (0); 2475831f5dcfSAlexander Motin } 2476831f5dcfSAlexander Motin 247715c440e1SWarner Losh #ifdef MMCCAM 2478a94a63f0SWarner Losh void 2479d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2480a94a63f0SWarner Losh { 2481ab00a509SMarius Strobl 2482505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2483a94a63f0SWarner Losh goto fail; 2484a94a63f0SWarner Losh 2485a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2486*aeb04e88SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2487*aeb04e88SWarner Losh "sdhci_slot", slot, 2488a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2489a94a63f0SWarner Losh 2490a94a63f0SWarner Losh if (slot->sim == NULL) { 2491a94a63f0SWarner Losh cam_simq_free(slot->devq); 2492a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2493a94a63f0SWarner Losh goto fail; 2494a94a63f0SWarner Losh } 2495a94a63f0SWarner Losh 2496a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2497a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2498505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2499a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2500a94a63f0SWarner Losh cam_simq_free(slot->devq); 2501a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2502a94a63f0SWarner Losh goto fail; 2503a94a63f0SWarner Losh } 2504a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2505505f6a0cSBjoern A. Zeeb 2506a94a63f0SWarner Losh /* End CAM-specific init */ 2507a94a63f0SWarner Losh slot->card_present = 0; 2508a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2509a94a63f0SWarner Losh return; 2510a94a63f0SWarner Losh 2511a94a63f0SWarner Losh fail: 2512a94a63f0SWarner Losh if (slot->sim != NULL) { 2513a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2514a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2515a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2516a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2517a94a63f0SWarner Losh } 2518a94a63f0SWarner Losh 2519a94a63f0SWarner Losh if (slot->devq != NULL) 2520a94a63f0SWarner Losh cam_simq_free(slot->devq); 2521a94a63f0SWarner Losh } 2522a94a63f0SWarner Losh 2523a94a63f0SWarner Losh void 2524a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2525a94a63f0SWarner Losh { 2526a94a63f0SWarner Losh struct sdhci_slot *slot; 2527a94a63f0SWarner Losh 2528a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2529a94a63f0SWarner Losh if (slot == NULL) { 2530a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2531a94a63f0SWarner Losh xpt_done(ccb); 2532a94a63f0SWarner Losh return; 2533a94a63f0SWarner Losh } 2534a94a63f0SWarner Losh 2535a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2536a94a63f0SWarner Losh 2537a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2538a94a63f0SWarner Losh case XPT_PATH_INQ: 2539cd853791SKonstantin Belousov mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, maxphys); 2540a94a63f0SWarner Losh break; 25418c7cd14aSWarner Losh 2542af2253f6SEmmanuel Vadot case XPT_MMC_GET_TRAN_SETTINGS: 2543a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2544a94a63f0SWarner Losh { 2545a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 25465d20e651SIlya Bakulin uint32_t max_data; 2547a94a63f0SWarner Losh 2548a94a63f0SWarner Losh if (sdhci_debug > 1) 2549a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2550a94a63f0SWarner Losh 2551a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2552a94a63f0SWarner Losh cts->protocol_version = 1; 2553a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2554a94a63f0SWarner Losh cts->transport_version = 1; 2555a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2556a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2557a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2558a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2559a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 25605d20e651SIlya Bakulin /* 25615d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 25625d20e651SIlya Bakulin * per read/write command to 4 MiB. 25635d20e651SIlya Bakulin */ 25645d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 25655d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 25665d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 25675d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 25685d20e651SIlya Bakulin } else { 25695d20e651SIlya Bakulin max_data = 65535; 25705d20e651SIlya Bakulin } 25715d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 25725d20e651SIlya Bakulin 2573a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2574a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2575a94a63f0SWarner Losh break; 2576a94a63f0SWarner Losh } 2577af2253f6SEmmanuel Vadot case XPT_MMC_SET_TRAN_SETTINGS: 2578a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2579a94a63f0SWarner Losh if (sdhci_debug > 1) 2580a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2581a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2582a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2583a94a63f0SWarner Losh break; 2584a94a63f0SWarner Losh case XPT_RESET_BUS: 2585a94a63f0SWarner Losh if (sdhci_debug > 1) 2586a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2587a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2588a94a63f0SWarner Losh break; 2589a94a63f0SWarner Losh case XPT_MMC_IO: 2590a94a63f0SWarner Losh /* 2591a94a63f0SWarner Losh * Here is the HW-dependent part of 2592a94a63f0SWarner Losh * sending the command to the underlying h/w 2593a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2594a94a63f0SWarner Losh * Then the request will be marked as completed. 2595a94a63f0SWarner Losh */ 2596aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2597a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2598a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2599a94a63f0SWarner Losh 2600160799c6SWarner Losh sdhci_cam_request(cam_sim_softc(sim), ccb); 2601a94a63f0SWarner Losh return; 2602a94a63f0SWarner Losh default: 2603a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2604a94a63f0SWarner Losh break; 2605a94a63f0SWarner Losh } 2606a94a63f0SWarner Losh xpt_done(ccb); 2607a94a63f0SWarner Losh return; 2608a94a63f0SWarner Losh } 2609a94a63f0SWarner Losh 2610a94a63f0SWarner Losh void 2611a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2612a94a63f0SWarner Losh { 2613a94a63f0SWarner Losh return; 2614a94a63f0SWarner Losh } 2615a94a63f0SWarner Losh 26166dea80e6SMarius Strobl static int 2617ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2618ab00a509SMarius Strobl int proposed_clock) 26196dea80e6SMarius Strobl { 2620a94a63f0SWarner Losh int max_clock, clock, i; 2621a94a63f0SWarner Losh 2622a94a63f0SWarner Losh if (proposed_clock == 0) 2623a94a63f0SWarner Losh return 0; 2624a94a63f0SWarner Losh max_clock = slot->max_clk; 2625a94a63f0SWarner Losh clock = max_clock; 2626a94a63f0SWarner Losh 2627a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2628505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2629a94a63f0SWarner Losh if (clock <= proposed_clock) 2630a94a63f0SWarner Losh break; 2631a94a63f0SWarner Losh clock >>= 1; 2632a94a63f0SWarner Losh } 2633a94a63f0SWarner Losh } else { 2634505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2635a94a63f0SWarner Losh if (clock <= proposed_clock) 2636a94a63f0SWarner Losh break; 2637a94a63f0SWarner Losh clock = max_clock / (i + 2); 2638a94a63f0SWarner Losh } 2639a94a63f0SWarner Losh } 2640a94a63f0SWarner Losh return clock; 2641a94a63f0SWarner Losh } 2642a94a63f0SWarner Losh 2643ab00a509SMarius Strobl static int 2644a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2645a94a63f0SWarner Losh { 2646a94a63f0SWarner Losh struct mmc_ios *ios; 2647ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2648ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2649a94a63f0SWarner Losh 2650a94a63f0SWarner Losh ios = &slot->host.ios; 2651a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2652a94a63f0SWarner Losh new_ios = &cts->ios; 2653a94a63f0SWarner Losh 2654a94a63f0SWarner Losh /* Update only requested fields */ 2655a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2656a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2657b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2658a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2659a94a63f0SWarner Losh } 2660a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2661a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2662b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2663a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2664a94a63f0SWarner Losh } 2665a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2666a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2667b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2668a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2669a94a63f0SWarner Losh } 2670a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2671a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2672b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2673a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2674a94a63f0SWarner Losh } 2675a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2676a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2677b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2678a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2679a94a63f0SWarner Losh } 2680a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2681a94a63f0SWarner Losh ios->timing = new_ios->timing; 2682b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2683a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2684a94a63f0SWarner Losh } 2685a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2686a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2687b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2688a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2689a94a63f0SWarner Losh } 26901a96c143SEmmanuel Vadot if (cts->ios_valid & MMC_VCCQ) { 26911a96c143SEmmanuel Vadot ios->vccq = new_ios->vccq; 26921a96c143SEmmanuel Vadot if (sdhci_debug > 1) 26931a96c143SEmmanuel Vadot slot_printf(slot, "VCCQ => %d\n", ios->vccq); 26941a96c143SEmmanuel Vadot } 2695a94a63f0SWarner Losh 2696a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2697a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2698a94a63f0SWarner Losh } 2699a94a63f0SWarner Losh 2700ab00a509SMarius Strobl static int 2701a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2702a94a63f0SWarner Losh { 2703a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2704a94a63f0SWarner Losh 2705b18f2ef4SEmmanuel Vadot if (sdhci_debug > 1) 2706a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2707a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2708a94a63f0SWarner Losh SDHCI_LOCK(slot); 2709a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2710a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2711a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2712a94a63f0SWarner Losh sdhci_init(slot); 2713a94a63f0SWarner Losh } 2714a94a63f0SWarner Losh /* Configure the bus. */ 2715a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2716a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2717a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2718a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2719a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2720a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2721a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2722a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2723a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2724a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2725a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2726a94a63f0SWarner Losh } else { 2727a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2728a94a63f0SWarner Losh } 2729a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2730a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2731a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2732a94a63f0SWarner Losh else 2733a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2734a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2735a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2736a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2737a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2738a94a63f0SWarner Losh 2739a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2740a94a63f0SWarner Losh return (0); 2741a94a63f0SWarner Losh } 2742a94a63f0SWarner Losh 2743ab00a509SMarius Strobl static int 2744a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2745a94a63f0SWarner Losh { 2746ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2747a94a63f0SWarner Losh 2748a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2749a94a63f0SWarner Losh 2750a94a63f0SWarner Losh SDHCI_LOCK(slot); 2751a94a63f0SWarner Losh /* if (slot->req != NULL) { 2752a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2753a94a63f0SWarner Losh return (EBUSY); 2754a94a63f0SWarner Losh } 2755a94a63f0SWarner Losh */ 2756aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 27575d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 27585d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2759a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2760a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 27615d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 27625d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 27635d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2764a94a63f0SWarner Losh } 2765a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2766a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2767a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2768a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2769a94a63f0SWarner Losh } 2770a94a63f0SWarner Losh slot->ccb = ccb; 2771a94a63f0SWarner Losh slot->flags = 0; 2772a94a63f0SWarner Losh sdhci_start(slot); 2773a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2774a94a63f0SWarner Losh if (dumping) { 2775a94a63f0SWarner Losh while (slot->ccb != NULL) { 2776a94a63f0SWarner Losh sdhci_generic_intr(slot); 2777a94a63f0SWarner Losh DELAY(10); 2778a94a63f0SWarner Losh } 2779a94a63f0SWarner Losh } 2780a94a63f0SWarner Losh return (0); 2781a94a63f0SWarner Losh } 278215c440e1SWarner Losh #endif /* MMCCAM */ 2783a94a63f0SWarner Losh 2784ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2785