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 70f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 71831f5dcfSAlexander Motin 72a94a63f0SWarner Losh static int sdhci_debug = 0; 737e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 747e6ccea3SMarius Strobl "Debug level"); 750f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 760f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 770f34084fSMarius Strobl 0, "Mask of quirks to clear"); 780f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 790f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 800f34084fSMarius Strobl "Mask of quirks to set"); 815b69a497SAlexander Motin 82d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 83d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 84d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 85d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 86d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 87831f5dcfSAlexander Motin 88d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 89d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 90d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 91d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 92d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 93831f5dcfSAlexander Motin 946dea80e6SMarius Strobl static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err); 95aca38eabSMarius Strobl static void sdhci_card_poll(void *arg); 96aca38eabSMarius Strobl static void sdhci_card_task(void *arg, int pending); 976dea80e6SMarius Strobl static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask); 986dea80e6SMarius Strobl static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask); 99aca38eabSMarius Strobl static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 1006dea80e6SMarius Strobl static void sdhci_handle_card_present_locked(struct sdhci_slot *slot, 1016dea80e6SMarius Strobl bool is_present); 1026dea80e6SMarius Strobl static void sdhci_finish_command(struct sdhci_slot *slot); 1036dea80e6SMarius Strobl static void sdhci_init(struct sdhci_slot *slot); 1046dea80e6SMarius Strobl static void sdhci_read_block_pio(struct sdhci_slot *slot); 1056dea80e6SMarius Strobl static void sdhci_req_done(struct sdhci_slot *slot); 106aca38eabSMarius Strobl static void sdhci_req_wakeup(struct mmc_request *req); 1076dea80e6SMarius Strobl static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask); 108aca38eabSMarius Strobl static void sdhci_retune(void *arg); 109831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 1106dea80e6SMarius Strobl static void sdhci_set_power(struct sdhci_slot *slot, u_char power); 1116dea80e6SMarius Strobl static void sdhci_set_transfer_mode(struct sdhci_slot *slot, 112ab00a509SMarius Strobl const struct mmc_data *data); 113831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 1146dea80e6SMarius Strobl static void sdhci_timeout(void *arg); 1156dea80e6SMarius Strobl static void sdhci_start_command(struct sdhci_slot *slot, 1166dea80e6SMarius Strobl struct mmc_command *cmd); 117ab00a509SMarius Strobl static void sdhci_start_data(struct sdhci_slot *slot, 118ab00a509SMarius Strobl const struct mmc_data *data); 1196dea80e6SMarius Strobl static void sdhci_write_block_pio(struct sdhci_slot *slot); 1206dea80e6SMarius Strobl static void sdhci_transfer_pio(struct sdhci_slot *slot); 121831f5dcfSAlexander Motin 12215c440e1SWarner Losh #ifdef MMCCAM 123a94a63f0SWarner Losh /* CAM-related */ 124a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 125ab00a509SMarius Strobl static int sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 1266dea80e6SMarius Strobl int proposed_clock); 1276dea80e6SMarius Strobl static void sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb); 128a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 1296dea80e6SMarius Strobl static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 130a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 1316dea80e6SMarius Strobl static int sdhci_cam_update_ios(struct sdhci_slot *slot); 13215c440e1SWarner Losh #endif 133a94a63f0SWarner Losh 134831f5dcfSAlexander Motin /* helper routines */ 135ab00a509SMarius Strobl static int sdhci_dma_alloc(struct sdhci_slot *slot); 136ab00a509SMarius Strobl static void sdhci_dma_free(struct sdhci_slot *slot); 1370f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 1386dea80e6SMarius Strobl static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, 1396dea80e6SMarius Strobl int error); 140ab00a509SMarius Strobl static int slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 1410f34084fSMarius Strobl __printflike(2, 3); 142ab00a509SMarius Strobl static uint32_t sdhci_tuning_intmask(const struct sdhci_slot *slot); 1430f34084fSMarius Strobl 144831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 145831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 146831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 147831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 148831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 149831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 150831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 151831f5dcfSAlexander Motin 15233aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 15333aad34dSOleksandr Tymoshenko 15457677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 15557677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 15657677a3aSOleksandr Tymoshenko 157639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 158639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 159639f59f0SIan Lepore 16093efdc63SAdrian Chadd /* 16193efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 16293efdc63SAdrian Chadd */ 1631bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1641bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1651bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1661bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 16793efdc63SAdrian Chadd 16893efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 16993efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 17093efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 17193efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 17293efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 17393efdc63SAdrian Chadd 174831f5dcfSAlexander Motin static void 175831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 176831f5dcfSAlexander Motin { 1777e6ccea3SMarius Strobl 178831f5dcfSAlexander Motin if (error != 0) { 179831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 180831f5dcfSAlexander Motin return; 181831f5dcfSAlexander Motin } 182831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 183831f5dcfSAlexander Motin } 184831f5dcfSAlexander Motin 185d6b3aaf8SOleksandr Tymoshenko static int 186ab00a509SMarius Strobl slot_printf(const struct sdhci_slot *slot, const char * fmt, ...) 187d6b3aaf8SOleksandr Tymoshenko { 188d6b3aaf8SOleksandr Tymoshenko va_list ap; 189d6b3aaf8SOleksandr Tymoshenko int retval; 190d6b3aaf8SOleksandr Tymoshenko 191d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 192d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 193d6b3aaf8SOleksandr Tymoshenko 194d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 195d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 196d6b3aaf8SOleksandr Tymoshenko va_end(ap); 197d6b3aaf8SOleksandr Tymoshenko return (retval); 198d6b3aaf8SOleksandr Tymoshenko } 199d6b3aaf8SOleksandr Tymoshenko 200831f5dcfSAlexander Motin static void 201831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 202831f5dcfSAlexander Motin { 2037e6ccea3SMarius Strobl 204831f5dcfSAlexander Motin slot_printf(slot, 205831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 206831f5dcfSAlexander Motin 207831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 208831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 209831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 210831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 211831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 212831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 213831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 214831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 215831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 216831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 217831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 218831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 219831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 220831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 221831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 222831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 2239dbf8c46SMarius Strobl slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 2249dbf8c46SMarius Strobl RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 2259dbf8c46SMarius Strobl slot_printf(slot, "Caps: 0x%08x | Caps2: 0x%08x\n", 2269dbf8c46SMarius Strobl RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 2279dbf8c46SMarius Strobl slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 2289dbf8c46SMarius Strobl RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 2299dbf8c46SMarius Strobl slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 2309dbf8c46SMarius Strobl RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 231831f5dcfSAlexander Motin 232831f5dcfSAlexander Motin slot_printf(slot, 233831f5dcfSAlexander Motin "===========================================\n"); 234831f5dcfSAlexander Motin } 235831f5dcfSAlexander Motin 236831f5dcfSAlexander Motin static void 237831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 238831f5dcfSAlexander Motin { 239831f5dcfSAlexander Motin int timeout; 240b440e965SMarius Strobl uint32_t clock; 241831f5dcfSAlexander Motin 242d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 2436e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 244831f5dcfSAlexander Motin return; 245831f5dcfSAlexander Motin } 246831f5dcfSAlexander Motin 247831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 248831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 249d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 250831f5dcfSAlexander Motin /* This is to force an update */ 251831f5dcfSAlexander Motin clock = slot->clock; 252831f5dcfSAlexander Motin slot->clock = 0; 253831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 254831f5dcfSAlexander Motin } 255831f5dcfSAlexander Motin 256d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 257831f5dcfSAlexander Motin slot->clock = 0; 258d8208d9eSAlexander Motin slot->power = 0; 259d8208d9eSAlexander Motin } 260831f5dcfSAlexander Motin 26161bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 26261bc42f7SIan Lepore 26361bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 26461bc42f7SIan Lepore /* 26561bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 26661bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 26761bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 26861bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 26961bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 27061bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 27161bc42f7SIan Lepore */ 27261bc42f7SIan Lepore timeout = 10000; 27361bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 27461bc42f7SIan Lepore if (timeout <= 0) 27561bc42f7SIan Lepore break; 27661bc42f7SIan Lepore timeout--; 27761bc42f7SIan Lepore DELAY(1); 27861bc42f7SIan Lepore } 27961bc42f7SIan Lepore } 28061bc42f7SIan Lepore 281831f5dcfSAlexander Motin /* Wait max 100 ms */ 28261bc42f7SIan Lepore timeout = 10000; 283831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 28461bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 28561bc42f7SIan Lepore if (timeout <= 0) { 28661bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 28761bc42f7SIan Lepore mask); 288831f5dcfSAlexander Motin sdhci_dumpregs(slot); 289831f5dcfSAlexander Motin return; 290831f5dcfSAlexander Motin } 291831f5dcfSAlexander Motin timeout--; 29261bc42f7SIan Lepore DELAY(10); 293831f5dcfSAlexander Motin } 294831f5dcfSAlexander Motin } 295831f5dcfSAlexander Motin 296aca38eabSMarius Strobl static uint32_t 297ab00a509SMarius Strobl sdhci_tuning_intmask(const struct sdhci_slot *slot) 298aca38eabSMarius Strobl { 299aca38eabSMarius Strobl uint32_t intmask; 300aca38eabSMarius Strobl 301aca38eabSMarius Strobl intmask = 0; 30278f8baa8SMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 303aca38eabSMarius Strobl intmask |= SDHCI_INT_TUNEERR; 304aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 305aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_3) 306aca38eabSMarius Strobl intmask |= SDHCI_INT_RETUNE; 307aca38eabSMarius Strobl } 308aca38eabSMarius Strobl return (intmask); 309aca38eabSMarius Strobl } 310aca38eabSMarius Strobl 311831f5dcfSAlexander Motin static void 312831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 313831f5dcfSAlexander Motin { 314831f5dcfSAlexander Motin 315831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 316831f5dcfSAlexander Motin 317831f5dcfSAlexander Motin /* Enable interrupts. */ 318831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 319831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 320831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 321831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 322831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 3233685b398SWarner Losh SDHCI_INT_ACMD12ERR; 324639f59f0SIan Lepore 325639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 326639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 327639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 328639f59f0SIan Lepore } 329639f59f0SIan Lepore 330cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 331831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 332831f5dcfSAlexander Motin } 333831f5dcfSAlexander Motin 334831f5dcfSAlexander Motin static void 335831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 336831f5dcfSAlexander Motin { 33793efdc63SAdrian Chadd uint32_t clk_base; 33893efdc63SAdrian Chadd uint32_t clk_sel; 339831f5dcfSAlexander Motin uint32_t res; 340831f5dcfSAlexander Motin uint16_t clk; 3418f3b7d56SOleksandr Tymoshenko uint16_t div; 342831f5dcfSAlexander Motin int timeout; 343831f5dcfSAlexander Motin 344831f5dcfSAlexander Motin if (clock == slot->clock) 345831f5dcfSAlexander Motin return; 346831f5dcfSAlexander Motin slot->clock = clock; 347831f5dcfSAlexander Motin 348831f5dcfSAlexander Motin /* Turn off the clock. */ 3494ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 3504ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 351b440e965SMarius Strobl /* If no clock requested - leave it so. */ 352831f5dcfSAlexander Motin if (clock == 0) 353831f5dcfSAlexander Motin return; 354ceb9e9f7SIan Lepore 35593efdc63SAdrian Chadd /* Determine the clock base frequency */ 35693efdc63SAdrian Chadd clk_base = slot->max_clk; 35793efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 3581bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 3591bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 36093efdc63SAdrian Chadd 3611bacf3beSMarius Strobl /* 3621bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 3631bacf3beSMarius Strobl */ 36493efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 36593efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 3661bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 3671bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 36893efdc63SAdrian Chadd } else { 3691bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 3701bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 37193efdc63SAdrian Chadd } 37293efdc63SAdrian Chadd 37393efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 37493efdc63SAdrian Chadd } 37593efdc63SAdrian Chadd 376ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 377ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 378ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 379ceb9e9f7SIan Lepore 3808f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 381831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 38293efdc63SAdrian Chadd res = clk_base; 38357677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 384831f5dcfSAlexander Motin if (res <= clock) 385831f5dcfSAlexander Motin break; 386831f5dcfSAlexander Motin res >>= 1; 387831f5dcfSAlexander Motin } 388831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 3898f3b7d56SOleksandr Tymoshenko div >>= 1; 390c11bbc7dSMarius Strobl } else { 3918f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 39293efdc63SAdrian Chadd if (clock >= clk_base) 39357677a3aSOleksandr Tymoshenko div = 0; 3948f3b7d56SOleksandr Tymoshenko else { 39557677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 39693efdc63SAdrian Chadd if ((clk_base / div) <= clock) 3978f3b7d56SOleksandr Tymoshenko break; 3988f3b7d56SOleksandr Tymoshenko } 3998f3b7d56SOleksandr Tymoshenko } 4008f3b7d56SOleksandr Tymoshenko div >>= 1; 4018f3b7d56SOleksandr Tymoshenko } 4028f3b7d56SOleksandr Tymoshenko 4038f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 40493efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 40593efdc63SAdrian Chadd div, clock, clk_base); 4068f3b7d56SOleksandr Tymoshenko 407831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 4088f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 4098f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 4108f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 4118f3b7d56SOleksandr Tymoshenko 412831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 413831f5dcfSAlexander Motin /* Enable clock. */ 414831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 415831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 416831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 417831f5dcfSAlexander Motin timeout = 10; 418831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 419831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 420831f5dcfSAlexander Motin if (timeout == 0) { 421831f5dcfSAlexander Motin slot_printf(slot, 422831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 423831f5dcfSAlexander Motin sdhci_dumpregs(slot); 424831f5dcfSAlexander Motin return; 425831f5dcfSAlexander Motin } 426831f5dcfSAlexander Motin timeout--; 427831f5dcfSAlexander Motin DELAY(1000); 428831f5dcfSAlexander Motin } 429831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 430831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 431831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 432831f5dcfSAlexander Motin } 433831f5dcfSAlexander Motin 434831f5dcfSAlexander Motin static void 435831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 436831f5dcfSAlexander Motin { 43785083a80SMarius Strobl int i; 438831f5dcfSAlexander Motin uint8_t pwr; 439831f5dcfSAlexander Motin 440831f5dcfSAlexander Motin if (slot->power == power) 441831f5dcfSAlexander Motin return; 442d6b3aaf8SOleksandr Tymoshenko 443831f5dcfSAlexander Motin slot->power = power; 444831f5dcfSAlexander Motin 445831f5dcfSAlexander Motin /* Turn off the power. */ 446831f5dcfSAlexander Motin pwr = 0; 447831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 448b440e965SMarius Strobl /* If power down requested - leave it so. */ 449831f5dcfSAlexander Motin if (power == 0) 450831f5dcfSAlexander Motin return; 451831f5dcfSAlexander Motin /* Set voltage. */ 452831f5dcfSAlexander Motin switch (1 << power) { 453831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 454831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 455831f5dcfSAlexander Motin break; 456831f5dcfSAlexander Motin case MMC_OCR_290_300: 457831f5dcfSAlexander Motin case MMC_OCR_300_310: 458831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 459831f5dcfSAlexander Motin break; 460831f5dcfSAlexander Motin case MMC_OCR_320_330: 461831f5dcfSAlexander Motin case MMC_OCR_330_340: 462831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 463831f5dcfSAlexander Motin break; 464831f5dcfSAlexander Motin } 465831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 46685083a80SMarius Strobl /* 46785083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 46885083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 4698022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 47085083a80SMarius Strobl */ 471831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 47285083a80SMarius Strobl for (i = 0; i < 20; i++) { 473831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 47485083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 47585083a80SMarius Strobl break; 47685083a80SMarius Strobl DELAY(100); 47785083a80SMarius Strobl } 47885083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 47985083a80SMarius Strobl slot_printf(slot, "Bus power failed to enable"); 480a2832f9fSMarius Strobl 481a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 482a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 483a2832f9fSMarius Strobl DELAY(10); 484a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 485a2832f9fSMarius Strobl DELAY(300); 486a2832f9fSMarius Strobl } 487831f5dcfSAlexander Motin } 488831f5dcfSAlexander Motin 489831f5dcfSAlexander Motin static void 490831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 491831f5dcfSAlexander Motin { 492831f5dcfSAlexander Motin uint32_t data; 493831f5dcfSAlexander Motin char *buffer; 494831f5dcfSAlexander Motin size_t left; 495831f5dcfSAlexander Motin 496831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 497831f5dcfSAlexander Motin buffer += slot->offset; 498831f5dcfSAlexander Motin /* Transfer one block at a time. */ 499*5d5ae066SIlya Bakulin #ifdef MMCCAM 500*5d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) 501*5d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 502*5d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 503*5d5ae066SIlya Bakulin else 504*5d5ae066SIlya Bakulin #endif 505831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 506831f5dcfSAlexander Motin slot->offset += left; 507831f5dcfSAlexander Motin 508831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 509d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 510831f5dcfSAlexander Motin DELAY(10); 511ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 512831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 513831f5dcfSAlexander Motin while (left > 3) { 514831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 515831f5dcfSAlexander Motin buffer[0] = data; 516831f5dcfSAlexander Motin buffer[1] = (data >> 8); 517831f5dcfSAlexander Motin buffer[2] = (data >> 16); 518831f5dcfSAlexander Motin buffer[3] = (data >> 24); 519831f5dcfSAlexander Motin buffer += 4; 520831f5dcfSAlexander Motin left -= 4; 521831f5dcfSAlexander Motin } 522831f5dcfSAlexander Motin } else { 523d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 524831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 525831f5dcfSAlexander Motin left &= 3; 526831f5dcfSAlexander Motin } 527831f5dcfSAlexander Motin /* Handle uneven size case. */ 528831f5dcfSAlexander Motin if (left > 0) { 529831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 530831f5dcfSAlexander Motin while (left > 0) { 531831f5dcfSAlexander Motin *(buffer++) = data; 532831f5dcfSAlexander Motin data >>= 8; 533831f5dcfSAlexander Motin left--; 534831f5dcfSAlexander Motin } 535831f5dcfSAlexander Motin } 536831f5dcfSAlexander Motin } 537831f5dcfSAlexander Motin 538831f5dcfSAlexander Motin static void 539831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 540831f5dcfSAlexander Motin { 541831f5dcfSAlexander Motin uint32_t data = 0; 542831f5dcfSAlexander Motin char *buffer; 543831f5dcfSAlexander Motin size_t left; 544831f5dcfSAlexander Motin 545831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 546831f5dcfSAlexander Motin buffer += slot->offset; 547831f5dcfSAlexander Motin /* Transfer one block at a time. */ 548*5d5ae066SIlya Bakulin #ifdef MMCCAM 549*5d5ae066SIlya Bakulin if (slot->curcmd->data->flags & MMC_DATA_BLOCK_SIZE) { 550*5d5ae066SIlya Bakulin left = min(slot->curcmd->data->block_size, 551*5d5ae066SIlya Bakulin slot->curcmd->data->len - slot->offset); 552*5d5ae066SIlya Bakulin } else 553*5d5ae066SIlya Bakulin #endif 554831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 555831f5dcfSAlexander Motin slot->offset += left; 556831f5dcfSAlexander Motin 557ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 558831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 559831f5dcfSAlexander Motin while (left > 3) { 560831f5dcfSAlexander Motin data = buffer[0] + 561831f5dcfSAlexander Motin (buffer[1] << 8) + 562831f5dcfSAlexander Motin (buffer[2] << 16) + 563831f5dcfSAlexander Motin (buffer[3] << 24); 564831f5dcfSAlexander Motin left -= 4; 565831f5dcfSAlexander Motin buffer += 4; 566831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 567831f5dcfSAlexander Motin } 568831f5dcfSAlexander Motin } else { 569d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 570831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 571831f5dcfSAlexander Motin left &= 3; 572831f5dcfSAlexander Motin } 573831f5dcfSAlexander Motin /* Handle uneven size case. */ 574831f5dcfSAlexander Motin if (left > 0) { 575831f5dcfSAlexander Motin while (left > 0) { 576831f5dcfSAlexander Motin data <<= 8; 577831f5dcfSAlexander Motin data += *(buffer++); 578831f5dcfSAlexander Motin left--; 579831f5dcfSAlexander Motin } 580831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 581831f5dcfSAlexander Motin } 582831f5dcfSAlexander Motin } 583831f5dcfSAlexander Motin 584831f5dcfSAlexander Motin static void 585831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 586831f5dcfSAlexander Motin { 587831f5dcfSAlexander Motin 588831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 589831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 590831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 591831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 592831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 593831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 594831f5dcfSAlexander Motin break; 595831f5dcfSAlexander Motin } 596831f5dcfSAlexander Motin } else { 597831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 598831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 599831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 600831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 601831f5dcfSAlexander Motin break; 602831f5dcfSAlexander Motin } 603831f5dcfSAlexander Motin } 604831f5dcfSAlexander Motin } 605831f5dcfSAlexander Motin 606831f5dcfSAlexander Motin static void 6077e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 608831f5dcfSAlexander Motin { 609831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 6107e6ccea3SMarius Strobl device_t d; 611831f5dcfSAlexander Motin 612831f5dcfSAlexander Motin SDHCI_LOCK(slot); 6136e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 614a94a63f0SWarner Losh #ifdef MMCCAM 615a94a63f0SWarner Losh if (slot->card_present == 0) { 616a94a63f0SWarner Losh #else 617831f5dcfSAlexander Motin if (slot->dev == NULL) { 618a94a63f0SWarner Losh #endif 619831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 620639f59f0SIan Lepore if (bootverbose || sdhci_debug) 621639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 622a94a63f0SWarner Losh #ifdef MMCCAM 623a94a63f0SWarner Losh slot->card_present = 1; 624a94a63f0SWarner Losh union ccb *ccb; 625a94a63f0SWarner Losh uint32_t pathid; 626a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 627a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 628a94a63f0SWarner Losh if (ccb == NULL) { 629a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 630a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 631a94a63f0SWarner Losh return; 632a94a63f0SWarner Losh } 633a94a63f0SWarner Losh 634a94a63f0SWarner Losh /* 635a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 636a94a63f0SWarner Losh * will be at lun 0. 637a94a63f0SWarner Losh */ 638a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 639a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 640a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 641a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 642a94a63f0SWarner Losh xpt_free_ccb(ccb); 643a94a63f0SWarner Losh return; 644a94a63f0SWarner Losh } 645a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 646a94a63f0SWarner Losh xpt_rescan(ccb); 647a94a63f0SWarner Losh #else 648aca38eabSMarius Strobl d = slot->dev = device_add_child(slot->bus, "mmc", -1); 649831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 650aca38eabSMarius Strobl if (d) { 651aca38eabSMarius Strobl device_set_ivars(d, slot); 652aca38eabSMarius Strobl (void)device_probe_and_attach(d); 653aca38eabSMarius Strobl } 654a94a63f0SWarner Losh #endif 655831f5dcfSAlexander Motin } else 656831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 657831f5dcfSAlexander Motin } else { 658a94a63f0SWarner Losh #ifdef MMCCAM 659a94a63f0SWarner Losh if (slot->card_present == 1) { 660a94a63f0SWarner Losh #else 661831f5dcfSAlexander Motin if (slot->dev != NULL) { 662a94a63f0SWarner Losh #endif 663831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 664639f59f0SIan Lepore if (bootverbose || sdhci_debug) 665639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 6667e6ccea3SMarius Strobl d = slot->dev; 667831f5dcfSAlexander Motin slot->dev = NULL; 668a94a63f0SWarner Losh #ifdef MMCCAM 669a94a63f0SWarner Losh slot->card_present = 0; 670a94a63f0SWarner Losh union ccb *ccb; 671a94a63f0SWarner Losh uint32_t pathid; 672a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 673a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 674a94a63f0SWarner Losh if (ccb == NULL) { 675a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 676a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 677a94a63f0SWarner Losh return; 678a94a63f0SWarner Losh } 679a94a63f0SWarner Losh 680a94a63f0SWarner Losh /* 681a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 682a94a63f0SWarner Losh * will be at lun 0. 683a94a63f0SWarner Losh */ 684a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 685a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 686a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 687a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 688a94a63f0SWarner Losh xpt_free_ccb(ccb); 689a94a63f0SWarner Losh return; 690a94a63f0SWarner Losh } 691a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 692a94a63f0SWarner Losh xpt_rescan(ccb); 693a94a63f0SWarner Losh #else 694aca38eabSMarius Strobl slot->intmask &= ~sdhci_tuning_intmask(slot); 695cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 696aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 697aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 698831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 699aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 700d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 701a94a63f0SWarner Losh #endif 702831f5dcfSAlexander Motin } else 703831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 704831f5dcfSAlexander Motin } 705831f5dcfSAlexander Motin } 706831f5dcfSAlexander Motin 707b8bf08b1SIan Lepore static void 708b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 709639f59f0SIan Lepore { 710639f59f0SIan Lepore bool was_present; 711639f59f0SIan Lepore 712639f59f0SIan Lepore /* 713639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 714639f59f0SIan Lepore * create the child device after a short delay. The delay is to 715639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 716639f59f0SIan Lepore * before the other pins have made good contact). 717639f59f0SIan Lepore * 718639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 719639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 720639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 721639f59f0SIan Lepore * that happens by deleting and recreating the child device. 722639f59f0SIan Lepore */ 723a94a63f0SWarner Losh #ifdef MMCCAM 724a94a63f0SWarner Losh was_present = slot->card_present; 725a94a63f0SWarner Losh #else 726639f59f0SIan Lepore was_present = slot->dev != NULL; 727a94a63f0SWarner Losh #endif 728639f59f0SIan Lepore if (!was_present && is_present) { 729639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 730639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 731639f59f0SIan Lepore } else if (was_present && !is_present) { 732639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 733639f59f0SIan Lepore } 734b8bf08b1SIan Lepore } 735b8bf08b1SIan Lepore 736b8bf08b1SIan Lepore void 737b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 738b8bf08b1SIan Lepore { 739b8bf08b1SIan Lepore 740b8bf08b1SIan Lepore SDHCI_LOCK(slot); 741b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 742639f59f0SIan Lepore SDHCI_UNLOCK(slot); 743639f59f0SIan Lepore } 744639f59f0SIan Lepore 745639f59f0SIan Lepore static void 746639f59f0SIan Lepore sdhci_card_poll(void *arg) 747639f59f0SIan Lepore { 748639f59f0SIan Lepore struct sdhci_slot *slot = arg; 749639f59f0SIan Lepore 750639f59f0SIan Lepore sdhci_handle_card_present(slot, 751639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 752639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 753639f59f0SIan Lepore sdhci_card_poll, slot); 754639f59f0SIan Lepore } 755639f59f0SIan Lepore 756ab00a509SMarius Strobl static int 757ab00a509SMarius Strobl sdhci_dma_alloc(struct sdhci_slot *slot) 758ab00a509SMarius Strobl { 759ab00a509SMarius Strobl int err; 760ab00a509SMarius Strobl 761ab00a509SMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_SDMA_BOUNDARY)) { 762ab00a509SMarius Strobl if (MAXPHYS <= 1024 * 4) 763ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_4K; 764ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 8) 765ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_8K; 766ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 16) 767ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_16K; 768ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 32) 769ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_32K; 770ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 64) 771ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_64K; 772ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 128) 773ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_128K; 774ab00a509SMarius Strobl else if (MAXPHYS <= 1024 * 256) 775ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_256K; 776ab00a509SMarius Strobl else 777ab00a509SMarius Strobl slot->sdma_boundary = SDHCI_BLKSZ_SDMA_BNDRY_512K; 778ab00a509SMarius Strobl } 779ab00a509SMarius Strobl slot->sdma_bbufsz = SDHCI_SDMA_BNDRY_TO_BBUFSZ(slot->sdma_boundary); 780ab00a509SMarius Strobl 781ab00a509SMarius Strobl /* 782ab00a509SMarius Strobl * Allocate the DMA tag for an SDMA bounce buffer. 783ab00a509SMarius Strobl * Note that the SDHCI specification doesn't state any alignment 784ab00a509SMarius Strobl * constraint for the SDMA system address. However, controllers 785ab00a509SMarius Strobl * typically ignore the SDMA boundary bits in SDHCI_DMA_ADDRESS when 786ab00a509SMarius Strobl * forming the actual address of data, requiring the SDMA buffer to 787ab00a509SMarius Strobl * be aligned to the SDMA boundary. 788ab00a509SMarius Strobl */ 789ab00a509SMarius Strobl err = bus_dma_tag_create(bus_get_dma_tag(slot->bus), slot->sdma_bbufsz, 790ab00a509SMarius Strobl 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 791ab00a509SMarius Strobl slot->sdma_bbufsz, 1, slot->sdma_bbufsz, BUS_DMA_ALLOCNOW, 792ab00a509SMarius Strobl NULL, NULL, &slot->dmatag); 793ab00a509SMarius Strobl if (err != 0) { 794ab00a509SMarius Strobl slot_printf(slot, "Can't create DMA tag for SDMA\n"); 795ab00a509SMarius Strobl return (err); 796ab00a509SMarius Strobl } 797ab00a509SMarius Strobl /* Allocate DMA memory for the SDMA bounce buffer. */ 798ab00a509SMarius Strobl err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 799ab00a509SMarius Strobl BUS_DMA_NOWAIT, &slot->dmamap); 800ab00a509SMarius Strobl if (err != 0) { 801ab00a509SMarius Strobl slot_printf(slot, "Can't alloc DMA memory for SDMA\n"); 802ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 803ab00a509SMarius Strobl return (err); 804ab00a509SMarius Strobl } 805ab00a509SMarius Strobl /* Map the memory of the SDMA bounce buffer. */ 806ab00a509SMarius Strobl err = bus_dmamap_load(slot->dmatag, slot->dmamap, 807ab00a509SMarius Strobl (void *)slot->dmamem, slot->sdma_bbufsz, sdhci_getaddr, 808ab00a509SMarius Strobl &slot->paddr, 0); 809ab00a509SMarius Strobl if (err != 0 || slot->paddr == 0) { 810ab00a509SMarius Strobl slot_printf(slot, "Can't load DMA memory for SDMA\n"); 811ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 812ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 813ab00a509SMarius Strobl if (err) 814ab00a509SMarius Strobl return (err); 815ab00a509SMarius Strobl else 816ab00a509SMarius Strobl return (EFAULT); 817ab00a509SMarius Strobl } 818ab00a509SMarius Strobl 819ab00a509SMarius Strobl return (0); 820ab00a509SMarius Strobl } 821ab00a509SMarius Strobl 822ab00a509SMarius Strobl static void 823ab00a509SMarius Strobl sdhci_dma_free(struct sdhci_slot *slot) 824ab00a509SMarius Strobl { 825ab00a509SMarius Strobl 826ab00a509SMarius Strobl bus_dmamap_unload(slot->dmatag, slot->dmamap); 827ab00a509SMarius Strobl bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 828ab00a509SMarius Strobl bus_dma_tag_destroy(slot->dmatag); 829ab00a509SMarius Strobl } 830ab00a509SMarius Strobl 831d6b3aaf8SOleksandr Tymoshenko int 832d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 833831f5dcfSAlexander Motin { 834aca38eabSMarius Strobl kobjop_desc_t kobj_desc; 835aca38eabSMarius Strobl kobj_method_t *kobj_method; 8360f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 837d6b3aaf8SOleksandr Tymoshenko int err; 838831f5dcfSAlexander Motin 839831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 840a94a63f0SWarner Losh 841d6b3aaf8SOleksandr Tymoshenko slot->num = num; 842d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 843d6b3aaf8SOleksandr Tymoshenko 844d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 845d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 8460f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 8478f3b7d56SOleksandr Tymoshenko caps = slot->caps; 8480f34084fSMarius Strobl caps2 = slot->caps2; 8490f34084fSMarius Strobl } else { 850831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 8510f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 8520f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 8530f34084fSMarius Strobl else 8540f34084fSMarius Strobl caps2 = 0; 8550f34084fSMarius Strobl } 8567fcf4780SMarius Strobl if (slot->version >= SDHCI_SPEC_300) { 8577fcf4780SMarius Strobl if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE && 8587fcf4780SMarius Strobl (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) { 859ab00a509SMarius Strobl slot_printf(slot, 8607fcf4780SMarius Strobl "Driver doesn't support shared bus slots\n"); 8617fcf4780SMarius Strobl SDHCI_LOCK_DESTROY(slot); 8627fcf4780SMarius Strobl return (ENXIO); 8637fcf4780SMarius Strobl } else if ((caps & SDHCI_SLOTTYPE_MASK) == 8647fcf4780SMarius Strobl SDHCI_SLOTTYPE_EMBEDDED) { 8657fcf4780SMarius Strobl slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE; 8667fcf4780SMarius Strobl } 8677fcf4780SMarius Strobl } 868831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 86933aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 87087a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 87187a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 87233aad34dSOleksandr Tymoshenko else 87387a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 87487a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 87587a6a871SIan Lepore if (freq != 0) 87687a6a871SIan Lepore slot->max_clk = freq * 1000000; 87787a6a871SIan Lepore /* 87887a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 87987a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 88087a6a871SIan Lepore * with an assumption, so complain about it. 88187a6a871SIan Lepore */ 882831f5dcfSAlexander Motin if (slot->max_clk == 0) { 88387a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 884ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify base clock " 8851bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 8861bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 887831f5dcfSAlexander Motin } 888a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 8898f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 8908f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 891a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 892a2832f9fSMarius Strobl slot->timeout_clk = 1000; 8938f3b7d56SOleksandr Tymoshenko } else { 8941bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 8951bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 8968f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 8978f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 8988f3b7d56SOleksandr Tymoshenko } 89987a6a871SIan Lepore /* 90087a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 90187a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 90287a6a871SIan Lepore * max timeout, but still mention it. 90387a6a871SIan Lepore */ 904831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 905ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't specify timeout clock " 906ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 907ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 908831f5dcfSAlexander Motin } 909831f5dcfSAlexander Motin 91057677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 911831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 912831f5dcfSAlexander Motin slot->host.host_ocr = 0; 913831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 914831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 915831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 916831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 9177fcf4780SMarius Strobl /* 1.8V VDD is not supposed to be used for removable cards. */ 9187fcf4780SMarius Strobl if ((caps & SDHCI_CAN_VDD_180) && (slot->opt & SDHCI_SLOT_EMBEDDED)) 919831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 920831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 921ab00a509SMarius Strobl slot_printf(slot, "Hardware doesn't report any " 922831f5dcfSAlexander Motin "support voltages.\n"); 923831f5dcfSAlexander Motin } 924aca38eabSMarius Strobl 9250f34084fSMarius Strobl host_caps = MMC_CAP_4_BIT_DATA; 9262d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 9270f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 928831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 9290f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 93072dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 9310f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 93272dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 9330f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 934aca38eabSMarius Strobl 935aca38eabSMarius Strobl /* Determine supported UHS-I and eMMC modes. */ 9360f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 9370f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 9380f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 9390f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 9400f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 9410f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 9420f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 9430f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 9440f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 9450f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 9460f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 9470f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 9480f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 9490f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 9500f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 9510f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 952835998c2SMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 && 953835998c2SMarius Strobl caps2 & SDHCI_CAN_SDR104) 954835998c2SMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 955aca38eabSMarius Strobl 956aca38eabSMarius Strobl /* 957aca38eabSMarius Strobl * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 958aca38eabSMarius Strobl * default NULL implementation. 959aca38eabSMarius Strobl */ 960aca38eabSMarius Strobl kobj_desc = &sdhci_set_uhs_timing_desc; 961aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 962aca38eabSMarius Strobl kobj_desc); 963aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 964aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 965aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 966aca38eabSMarius Strobl MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 967aca38eabSMarius Strobl 968aca38eabSMarius Strobl #define SDHCI_CAP_MODES_TUNING(caps2) \ 969aca38eabSMarius Strobl (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 970aca38eabSMarius Strobl MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 971aca38eabSMarius Strobl MMC_CAP_MMC_HS400) 972aca38eabSMarius Strobl 973aca38eabSMarius Strobl /* 974aca38eabSMarius Strobl * Disable UHS-I and eMMC modes that require (re-)tuning if either 975aca38eabSMarius Strobl * the tune or re-tune method is the default NULL implementation. 976aca38eabSMarius Strobl */ 977aca38eabSMarius Strobl kobj_desc = &mmcbr_tune_desc; 978aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 979aca38eabSMarius Strobl kobj_desc); 980aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 981aca38eabSMarius Strobl goto no_tuning; 982aca38eabSMarius Strobl kobj_desc = &mmcbr_retune_desc; 983aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 984aca38eabSMarius Strobl kobj_desc); 985aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 986aca38eabSMarius Strobl no_tuning: 987aca38eabSMarius Strobl host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 988aca38eabSMarius Strobl } 989aca38eabSMarius Strobl 990aca38eabSMarius Strobl /* Allocate tuning structures and determine tuning parameters. */ 991aca38eabSMarius Strobl if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 992aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_SUPPORTED; 993aca38eabSMarius Strobl slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 994aca38eabSMarius Strobl M_WAITOK); 995aca38eabSMarius Strobl slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 996aca38eabSMarius Strobl M_WAITOK); 997aca38eabSMarius Strobl slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 998aca38eabSMarius Strobl M_WAITOK); 999aca38eabSMarius Strobl if (caps2 & SDHCI_TUNE_SDR50) 1000aca38eabSMarius Strobl slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 1001aca38eabSMarius Strobl slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 1002aca38eabSMarius Strobl SDHCI_RETUNE_MODES_SHIFT; 1003aca38eabSMarius Strobl if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 1004aca38eabSMarius Strobl slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 1005aca38eabSMarius Strobl SDHCI_RETUNE_CNT_SHIFT; 1006aca38eabSMarius Strobl if (slot->retune_count > 0xb) { 1007ab00a509SMarius Strobl slot_printf(slot, "Unknown re-tuning count " 1008aca38eabSMarius Strobl "%x, using 1 sec\n", slot->retune_count); 1009aca38eabSMarius Strobl slot->retune_count = 1; 1010aca38eabSMarius Strobl } else if (slot->retune_count != 0) 1011aca38eabSMarius Strobl slot->retune_count = 1012aca38eabSMarius Strobl 1 << (slot->retune_count - 1); 1013aca38eabSMarius Strobl } 1014aca38eabSMarius Strobl } 1015aca38eabSMarius Strobl 1016aca38eabSMarius Strobl #undef SDHCI_CAP_MODES_TUNING 1017aca38eabSMarius Strobl 1018aca38eabSMarius Strobl /* Determine supported VCCQ signaling levels. */ 10190f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 10200f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 1021aca38eabSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 10220f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 10230f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 1024aca38eabSMarius Strobl host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 1025aca38eabSMarius Strobl 1026aca38eabSMarius Strobl /* 1027aca38eabSMarius Strobl * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 1028aca38eabSMarius Strobl * default NULL implementation. Disable 1.2 V support if it's the 1029aca38eabSMarius Strobl * generic SDHCI implementation. 1030aca38eabSMarius Strobl */ 1031aca38eabSMarius Strobl kobj_desc = &mmcbr_switch_vccq_desc; 1032aca38eabSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 1033aca38eabSMarius Strobl kobj_desc); 1034aca38eabSMarius Strobl if (kobj_method == &kobj_desc->deflt) 1035aca38eabSMarius Strobl host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 1036aca38eabSMarius Strobl else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 1037aca38eabSMarius Strobl host_caps &= ~MMC_CAP_SIGNALING_120; 1038aca38eabSMarius Strobl 1039aca38eabSMarius Strobl /* Determine supported driver types (type B is always mandatory). */ 1040f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 10410f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 1042f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 10430f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 1044f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 10450f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 10460f34084fSMarius Strobl slot->host.caps = host_caps; 10470f34084fSMarius Strobl 1048831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 1049831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 1050831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1051d6b3aaf8SOleksandr Tymoshenko 1052d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 1053831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 1054d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 1055831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 1056a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 1057a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 1058831f5dcfSAlexander Motin 1059c3a0f75aSOleksandr Tymoshenko /* 1060c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 1061c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 1062c3a0f75aSOleksandr Tymoshenko */ 1063c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 1064c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 1065c3a0f75aSOleksandr Tymoshenko 1066ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) { 1067ab00a509SMarius Strobl err = sdhci_dma_alloc(slot); 1068ab00a509SMarius Strobl if (err != 0) { 1069ab00a509SMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1070ab00a509SMarius Strobl free(slot->tune_req, M_DEVBUF); 1071ab00a509SMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1072ab00a509SMarius Strobl free(slot->tune_data, M_DEVBUF); 1073ab00a509SMarius Strobl } 1074ab00a509SMarius Strobl SDHCI_LOCK_DESTROY(slot); 1075ab00a509SMarius Strobl return (err); 1076ab00a509SMarius Strobl } 1077ab00a509SMarius Strobl } 1078ab00a509SMarius Strobl 10795b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 10800f34084fSMarius Strobl slot_printf(slot, 10817fcf4780SMarius Strobl "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n", 1082831f5dcfSAlexander Motin slot->max_clk / 1000000, 1083831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 10840f34084fSMarius Strobl (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 10850f34084fSMarius Strobl ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 1086831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 1087831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 10887fcf4780SMarius Strobl ((caps & SDHCI_CAN_VDD_180) && 10897fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "", 10900f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 10910f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 1092aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 1093aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 1094aca38eabSMarius Strobl (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 10957fcf4780SMarius Strobl (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO", 10967fcf4780SMarius Strobl (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" : 10977fcf4780SMarius Strobl (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" : 10987fcf4780SMarius Strobl "removable"); 10990f34084fSMarius Strobl if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 11000f34084fSMarius Strobl MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 11010f34084fSMarius Strobl slot_printf(slot, "eMMC:%s%s%s%s\n", 11020f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 11030f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 11040f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 11050f34084fSMarius Strobl ((host_caps & 11060f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 11070f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 11080f34084fSMarius Strobl " HS400ES" : ""); 11090f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 11100f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 11110f34084fSMarius Strobl slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 11120f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 11130f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 11140f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 11150f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 11160f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 1117aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) 1118aca38eabSMarius Strobl slot_printf(slot, "Re-tuning count %d secs, mode %d\n", 1119aca38eabSMarius Strobl slot->retune_count, slot->retune_mode + 1); 1120831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1121831f5dcfSAlexander Motin } 1122831f5dcfSAlexander Motin 1123ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 1124ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1125ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 1126ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 1127ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 1128831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1129639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1130639f59f0SIan Lepore sdhci_card_task, slot); 1131639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 1132e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1133aca38eabSMarius Strobl callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1134ba6fc1c7SLuiz Otavio O Souza 1135639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1136639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 1137639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 1138639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1139639f59f0SIan Lepore } 1140639f59f0SIan Lepore 1141aca38eabSMarius Strobl sdhci_init(slot); 1142aca38eabSMarius Strobl 1143831f5dcfSAlexander Motin return (0); 1144831f5dcfSAlexander Motin } 1145831f5dcfSAlexander Motin 1146d91f1a10SIlya Bakulin #ifndef MMCCAM 1147d6b3aaf8SOleksandr Tymoshenko void 1148d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 1149831f5dcfSAlexander Motin { 11507e6ccea3SMarius Strobl 1151d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 1152d6b3aaf8SOleksandr Tymoshenko } 1153d91f1a10SIlya Bakulin #endif 1154831f5dcfSAlexander Motin 1155d6b3aaf8SOleksandr Tymoshenko int 1156d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 1157d6b3aaf8SOleksandr Tymoshenko { 1158831f5dcfSAlexander Motin device_t d; 1159831f5dcfSAlexander Motin 1160e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 1161639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 1162aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1163831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1164639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1165831f5dcfSAlexander Motin 1166831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1167831f5dcfSAlexander Motin d = slot->dev; 1168831f5dcfSAlexander Motin slot->dev = NULL; 1169831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1170831f5dcfSAlexander Motin if (d != NULL) 1171d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 1172831f5dcfSAlexander Motin 1173831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1174831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 1175831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1176ab00a509SMarius Strobl if (slot->opt & SDHCI_HAVE_DMA) 1177ab00a509SMarius Strobl sdhci_dma_free(slot); 1178aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1179aca38eabSMarius Strobl free(slot->tune_req, M_DEVBUF); 1180aca38eabSMarius Strobl free(slot->tune_cmd, M_DEVBUF); 1181aca38eabSMarius Strobl free(slot->tune_data, M_DEVBUF); 1182aca38eabSMarius Strobl } 1183d6b3aaf8SOleksandr Tymoshenko 1184831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 1185d6b3aaf8SOleksandr Tymoshenko 1186831f5dcfSAlexander Motin return (0); 1187831f5dcfSAlexander Motin } 1188831f5dcfSAlexander Motin 1189d6b3aaf8SOleksandr Tymoshenko int 1190d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 119192bf0e27SAlexander Motin { 11927e6ccea3SMarius Strobl 1193aca38eabSMarius Strobl /* 1194aca38eabSMarius Strobl * We expect the MMC layer to issue initial tuning after resume. 1195aca38eabSMarius Strobl * Otherwise, we'd need to indicate re-tuning including circuit reset 1196aca38eabSMarius Strobl * being required at least for re-tuning modes 1 and 2 ourselves. 1197aca38eabSMarius Strobl */ 1198aca38eabSMarius Strobl callout_drain(&slot->retune_callout); 1199aca38eabSMarius Strobl SDHCI_LOCK(slot); 1200aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1201d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 1202aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 120392bf0e27SAlexander Motin 120492bf0e27SAlexander Motin return (0); 120592bf0e27SAlexander Motin } 120692bf0e27SAlexander Motin 1207d6b3aaf8SOleksandr Tymoshenko int 1208d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 120992bf0e27SAlexander Motin { 12107e6ccea3SMarius Strobl 1211aca38eabSMarius Strobl SDHCI_LOCK(slot); 1212d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 1213aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 121492bf0e27SAlexander Motin 1215d6b3aaf8SOleksandr Tymoshenko return (0); 121692bf0e27SAlexander Motin } 121792bf0e27SAlexander Motin 121857677a3aSOleksandr Tymoshenko uint32_t 1219b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 122057677a3aSOleksandr Tymoshenko { 12217e6ccea3SMarius Strobl 122257677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 122357677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 122457677a3aSOleksandr Tymoshenko else 122557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 122657677a3aSOleksandr Tymoshenko } 122757677a3aSOleksandr Tymoshenko 12286e37fb2bSIan Lepore bool 1229b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 12306e37fb2bSIan Lepore { 12316e37fb2bSIan Lepore 1232639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 1233639f59f0SIan Lepore return true; 1234639f59f0SIan Lepore 12356e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 12366e37fb2bSIan Lepore } 12376e37fb2bSIan Lepore 12380f34084fSMarius Strobl void 12390f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 12400f34084fSMarius Strobl { 1241ab00a509SMarius Strobl const struct mmc_ios *ios; 12420f34084fSMarius Strobl uint16_t hostctrl2; 12430f34084fSMarius Strobl 12440f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 12450f34084fSMarius Strobl return; 12460f34084fSMarius Strobl 1247aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 12480f34084fSMarius Strobl ios = &slot->host.ios; 12490f34084fSMarius Strobl sdhci_set_clock(slot, 0); 12500f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 12510f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1252aca38eabSMarius Strobl if (ios->clock > SD_SDR50_MAX) { 12530f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 12540f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 12550f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1256aca38eabSMarius Strobl else 12570f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1258aca38eabSMarius Strobl } 12590f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 12600f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 12610f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 12620f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 12630f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 12640f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 12650f34084fSMarius Strobl else 12660f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 12670f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 12680f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 12690f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 12700f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 12710f34084fSMarius Strobl } 12720f34084fSMarius Strobl 1273d6b3aaf8SOleksandr Tymoshenko int 1274d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1275831f5dcfSAlexander Motin { 1276831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1277831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1278831f5dcfSAlexander Motin 1279831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1280831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1281831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1282831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1283831f5dcfSAlexander Motin sdhci_init(slot); 1284831f5dcfSAlexander Motin } 1285831f5dcfSAlexander Motin /* Configure the bus. */ 1286831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1287831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 12882d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 12892d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1290831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12912d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 12922d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12932d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 12942d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 12952d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 12962d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 12972d1731b8SIan Lepore } else { 12982d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 12992d1731b8SIan Lepore } 13000f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1301bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1302831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1303831f5dcfSAlexander Motin else 1304831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1305831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 13060f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1307831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1308d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1309831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1310831f5dcfSAlexander Motin 1311831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1312831f5dcfSAlexander Motin return (0); 1313831f5dcfSAlexander Motin } 1314831f5dcfSAlexander Motin 13150f34084fSMarius Strobl int 13160f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 13170f34084fSMarius Strobl { 13180f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 13190f34084fSMarius Strobl enum mmc_vccq vccq; 13200f34084fSMarius Strobl int err; 13210f34084fSMarius Strobl uint16_t hostctrl2; 13220f34084fSMarius Strobl 13230f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 13240f34084fSMarius Strobl return (0); 13250f34084fSMarius Strobl 13260f34084fSMarius Strobl err = 0; 13270f34084fSMarius Strobl vccq = slot->host.ios.vccq; 13280f34084fSMarius Strobl SDHCI_LOCK(slot); 13290f34084fSMarius Strobl sdhci_set_clock(slot, 0); 13300f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13310f34084fSMarius Strobl switch (vccq) { 13320f34084fSMarius Strobl case vccq_330: 13330f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13340f34084fSMarius Strobl goto done; 13350f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 13360f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13370f34084fSMarius Strobl DELAY(5000); 13380f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13390f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 13400f34084fSMarius Strobl goto done; 13410f34084fSMarius Strobl err = EAGAIN; 13420f34084fSMarius Strobl break; 13430f34084fSMarius Strobl case vccq_180: 13440f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 13450f34084fSMarius Strobl err = EINVAL; 13460f34084fSMarius Strobl goto done; 13470f34084fSMarius Strobl } 13480f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13490f34084fSMarius Strobl goto done; 13500f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 13510f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 13520f34084fSMarius Strobl DELAY(5000); 13530f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 13540f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 13550f34084fSMarius Strobl goto done; 13560f34084fSMarius Strobl err = EAGAIN; 13570f34084fSMarius Strobl break; 13580f34084fSMarius Strobl default: 13590f34084fSMarius Strobl slot_printf(slot, 13600f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 13610f34084fSMarius Strobl err = EINVAL; 13620f34084fSMarius Strobl break; 13630f34084fSMarius Strobl } 13640f34084fSMarius Strobl done: 13650f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 13660f34084fSMarius Strobl SDHCI_UNLOCK(slot); 13670f34084fSMarius Strobl return (err); 13680f34084fSMarius Strobl } 13690f34084fSMarius Strobl 1370aca38eabSMarius Strobl int 1371aca38eabSMarius Strobl sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1372aca38eabSMarius Strobl { 1373aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1374ab00a509SMarius Strobl const struct mmc_ios *ios = &slot->host.ios; 1375aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1376aca38eabSMarius Strobl struct mmc_data *tune_data; 1377aca38eabSMarius Strobl uint32_t opcode; 1378aca38eabSMarius Strobl int err; 1379aca38eabSMarius Strobl 1380aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1381aca38eabSMarius Strobl return (0); 1382aca38eabSMarius Strobl 1383aca38eabSMarius Strobl slot->retune_ticks = slot->retune_count * hz; 1384aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK; 1385aca38eabSMarius Strobl SDHCI_LOCK(slot); 1386aca38eabSMarius Strobl switch (ios->timing) { 1387aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1388aca38eabSMarius Strobl slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1389aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1390aca38eabSMarius Strobl return (EINVAL); 1391aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1392aca38eabSMarius Strobl /* 1393aca38eabSMarius Strobl * In HS400 mode, controllers use the data strobe line to 1394aca38eabSMarius Strobl * latch data from the devices so periodic re-tuning isn't 1395aca38eabSMarius Strobl * expected to be required. 1396aca38eabSMarius Strobl */ 1397aca38eabSMarius Strobl if (hs400) 1398aca38eabSMarius Strobl slot->retune_ticks = 0; 1399aca38eabSMarius Strobl opcode = MMC_SEND_TUNING_BLOCK_HS200; 1400aca38eabSMarius Strobl break; 1401aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1402aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1403aca38eabSMarius Strobl break; 1404aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1405aca38eabSMarius Strobl if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1406aca38eabSMarius Strobl break; 1407aca38eabSMarius Strobl /* FALLTHROUGH */ 1408aca38eabSMarius Strobl default: 1409aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1410aca38eabSMarius Strobl return (0); 1411aca38eabSMarius Strobl } 1412aca38eabSMarius Strobl 1413aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1414aca38eabSMarius Strobl memset(tune_cmd, 0, sizeof(*tune_cmd)); 1415aca38eabSMarius Strobl tune_cmd->opcode = opcode; 1416aca38eabSMarius Strobl tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1417aca38eabSMarius Strobl tune_data = tune_cmd->data = slot->tune_data; 1418aca38eabSMarius Strobl memset(tune_data, 0, sizeof(*tune_data)); 1419aca38eabSMarius Strobl tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1420aca38eabSMarius Strobl ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1421aca38eabSMarius Strobl MMC_TUNING_LEN; 1422aca38eabSMarius Strobl tune_data->flags = MMC_DATA_READ; 1423aca38eabSMarius Strobl tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1424aca38eabSMarius Strobl 1425aca38eabSMarius Strobl slot->opt &= ~SDHCI_TUNING_ENABLED; 1426aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, true); 1427aca38eabSMarius Strobl if (err == 0) { 1428aca38eabSMarius Strobl slot->opt |= SDHCI_TUNING_ENABLED; 1429aca38eabSMarius Strobl slot->intmask |= sdhci_tuning_intmask(slot); 1430cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 1431aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1432aca38eabSMarius Strobl if (slot->retune_ticks) { 1433aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1434aca38eabSMarius Strobl sdhci_retune, slot); 1435aca38eabSMarius Strobl } 1436aca38eabSMarius Strobl } 1437aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1438aca38eabSMarius Strobl return (err); 1439aca38eabSMarius Strobl } 1440aca38eabSMarius Strobl 1441aca38eabSMarius Strobl int 1442aca38eabSMarius Strobl sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1443aca38eabSMarius Strobl { 1444aca38eabSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 1445aca38eabSMarius Strobl int err; 1446aca38eabSMarius Strobl 1447aca38eabSMarius Strobl if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1448aca38eabSMarius Strobl return (0); 1449aca38eabSMarius Strobl 1450aca38eabSMarius Strobl /* HS400 must be tuned in HS200 mode. */ 1451aca38eabSMarius Strobl if (slot->host.ios.timing == bus_timing_mmc_hs400) 1452aca38eabSMarius Strobl return (EINVAL); 1453aca38eabSMarius Strobl 1454aca38eabSMarius Strobl SDHCI_LOCK(slot); 1455aca38eabSMarius Strobl err = sdhci_exec_tuning(slot, reset); 1456aca38eabSMarius Strobl /* 1457aca38eabSMarius Strobl * There are two ways sdhci_exec_tuning() can fail: 1458aca38eabSMarius Strobl * EBUSY should not actually happen when requests are only issued 1459aca38eabSMarius Strobl * with the host properly acquired, and 1460aca38eabSMarius Strobl * EIO re-tuning failed (but it did work initially). 1461aca38eabSMarius Strobl * 1462aca38eabSMarius Strobl * In both cases, we should retry at later point if periodic re-tuning 1463aca38eabSMarius Strobl * is enabled. Note that due to slot->retune_req not being cleared in 1464aca38eabSMarius Strobl * these failure cases, the MMC layer should trigger another attempt at 1465aca38eabSMarius Strobl * re-tuning with the next request anyway, though. 1466aca38eabSMarius Strobl */ 1467aca38eabSMarius Strobl if (slot->retune_ticks) { 1468aca38eabSMarius Strobl callout_reset(&slot->retune_callout, slot->retune_ticks, 1469aca38eabSMarius Strobl sdhci_retune, slot); 1470aca38eabSMarius Strobl } 1471aca38eabSMarius Strobl SDHCI_UNLOCK(slot); 1472aca38eabSMarius Strobl return (err); 1473aca38eabSMarius Strobl } 1474aca38eabSMarius Strobl 1475aca38eabSMarius Strobl static int 1476aca38eabSMarius Strobl sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1477aca38eabSMarius Strobl { 1478aca38eabSMarius Strobl struct mmc_request *tune_req; 1479aca38eabSMarius Strobl struct mmc_command *tune_cmd; 1480aca38eabSMarius Strobl int i; 1481aca38eabSMarius Strobl uint32_t intmask; 1482aca38eabSMarius Strobl uint16_t hostctrl2; 1483aca38eabSMarius Strobl u_char opt; 1484aca38eabSMarius Strobl 1485aca38eabSMarius Strobl SDHCI_ASSERT_LOCKED(slot); 1486aca38eabSMarius Strobl if (slot->req != NULL) 1487aca38eabSMarius Strobl return (EBUSY); 1488aca38eabSMarius Strobl 1489aca38eabSMarius Strobl /* Tuning doesn't work with DMA enabled. */ 1490aca38eabSMarius Strobl opt = slot->opt; 1491aca38eabSMarius Strobl slot->opt = opt & ~SDHCI_HAVE_DMA; 1492aca38eabSMarius Strobl 1493aca38eabSMarius Strobl /* 1494aca38eabSMarius Strobl * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1495aca38eabSMarius Strobl * kind of interrupt we receive in response to a tuning request. 1496aca38eabSMarius Strobl */ 1497aca38eabSMarius Strobl intmask = slot->intmask; 1498aca38eabSMarius Strobl slot->intmask = SDHCI_INT_DATA_AVAIL; 1499cc22204bSMarius Strobl WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL); 1500aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1501aca38eabSMarius Strobl 1502aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1503aca38eabSMarius Strobl if (reset) 1504aca38eabSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1505aca38eabSMarius Strobl else 1506aca38eabSMarius Strobl hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1507aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1508aca38eabSMarius Strobl 1509aca38eabSMarius Strobl tune_req = slot->tune_req; 1510aca38eabSMarius Strobl tune_cmd = slot->tune_cmd; 1511aca38eabSMarius Strobl for (i = 0; i < MMC_TUNING_MAX; i++) { 1512aca38eabSMarius Strobl memset(tune_req, 0, sizeof(*tune_req)); 1513aca38eabSMarius Strobl tune_req->cmd = tune_cmd; 1514aca38eabSMarius Strobl tune_req->done = sdhci_req_wakeup; 1515aca38eabSMarius Strobl tune_req->done_data = slot; 1516aca38eabSMarius Strobl slot->req = tune_req; 1517aca38eabSMarius Strobl slot->flags = 0; 1518aca38eabSMarius Strobl sdhci_start(slot); 1519aca38eabSMarius Strobl while (!(tune_req->flags & MMC_REQ_DONE)) 1520aca38eabSMarius Strobl msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1521aca38eabSMarius Strobl if (!(tune_req->flags & MMC_TUNE_DONE)) 1522aca38eabSMarius Strobl break; 1523aca38eabSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1524aca38eabSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1525aca38eabSMarius Strobl break; 1526aca38eabSMarius Strobl if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1527aca38eabSMarius Strobl DELAY(1000); 1528aca38eabSMarius Strobl } 1529aca38eabSMarius Strobl 153078f8baa8SMarius Strobl /* 153178f8baa8SMarius Strobl * Restore DMA usage and interrupts. 153278f8baa8SMarius Strobl * Note that the interrupt aggregation code might have cleared 153378f8baa8SMarius Strobl * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask 153478f8baa8SMarius Strobl * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE 153578f8baa8SMarius Strobl * doesn't lose these. 153678f8baa8SMarius Strobl */ 1537aca38eabSMarius Strobl slot->opt = opt; 1538aca38eabSMarius Strobl slot->intmask = intmask; 153978f8baa8SMarius Strobl WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END | 154078f8baa8SMarius Strobl SDHCI_INT_RESPONSE); 1541aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1542aca38eabSMarius Strobl 1543aca38eabSMarius Strobl if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1544aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1545aca38eabSMarius Strobl slot->retune_req = 0; 1546aca38eabSMarius Strobl return (0); 1547aca38eabSMarius Strobl } 1548aca38eabSMarius Strobl 1549aca38eabSMarius Strobl slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1550aca38eabSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1551aca38eabSMarius Strobl SDHCI_CTRL2_SAMPLING_CLOCK)); 1552aca38eabSMarius Strobl sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1553aca38eabSMarius Strobl return (EIO); 1554aca38eabSMarius Strobl } 1555aca38eabSMarius Strobl 1556aca38eabSMarius Strobl static void 1557aca38eabSMarius Strobl sdhci_retune(void *arg) 1558aca38eabSMarius Strobl { 1559aca38eabSMarius Strobl struct sdhci_slot *slot = arg; 1560aca38eabSMarius Strobl 1561aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1562aca38eabSMarius Strobl } 1563aca38eabSMarius Strobl 1564a94a63f0SWarner Losh #ifdef MMCCAM 1565a94a63f0SWarner Losh static void 1566a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1567a94a63f0SWarner Losh { 1568a94a63f0SWarner Losh union ccb *ccb; 156915c440e1SWarner Losh 1570aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 157115c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1572a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1573a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1574a94a63f0SWarner Losh ccb = slot->ccb; 1575a94a63f0SWarner Losh slot->ccb = NULL; 1576a94a63f0SWarner Losh slot->curcmd = NULL; 1577a94a63f0SWarner Losh 1578a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1579a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1580a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1581a94a63f0SWarner Losh 1582a94a63f0SWarner Losh ccb->ccb_h.status = 1583a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1584a94a63f0SWarner Losh xpt_done(ccb); 1585a94a63f0SWarner Losh } 1586a94a63f0SWarner Losh } 1587a94a63f0SWarner Losh #else 1588831f5dcfSAlexander Motin static void 1589e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1590e64f01a9SIan Lepore { 1591e64f01a9SIan Lepore struct mmc_request *req; 1592e64f01a9SIan Lepore 1593e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1594e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1595e64f01a9SIan Lepore req = slot->req; 1596e64f01a9SIan Lepore slot->req = NULL; 1597e64f01a9SIan Lepore slot->curcmd = NULL; 1598e64f01a9SIan Lepore req->done(req); 1599e64f01a9SIan Lepore } 1600e64f01a9SIan Lepore } 1601a94a63f0SWarner Losh #endif 1602e64f01a9SIan Lepore 1603e64f01a9SIan Lepore static void 1604aca38eabSMarius Strobl sdhci_req_wakeup(struct mmc_request *req) 1605aca38eabSMarius Strobl { 1606aca38eabSMarius Strobl struct sdhci_slot *slot; 1607aca38eabSMarius Strobl 1608aca38eabSMarius Strobl slot = req->done_data; 1609aca38eabSMarius Strobl req->flags |= MMC_REQ_DONE; 1610aca38eabSMarius Strobl wakeup(req); 1611aca38eabSMarius Strobl } 1612aca38eabSMarius Strobl 1613aca38eabSMarius Strobl static void 1614e64f01a9SIan Lepore sdhci_timeout(void *arg) 1615e64f01a9SIan Lepore { 1616e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1617e64f01a9SIan Lepore 1618e64f01a9SIan Lepore if (slot->curcmd != NULL) { 16197e586643SIan Lepore slot_printf(slot, "Controller timeout\n"); 16207e586643SIan Lepore sdhci_dumpregs(slot); 1621a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1622e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1623e64f01a9SIan Lepore sdhci_req_done(slot); 16247e586643SIan Lepore } else { 16257e586643SIan Lepore slot_printf(slot, "Spurious timeout - no active command\n"); 1626e64f01a9SIan Lepore } 1627e64f01a9SIan Lepore } 1628e64f01a9SIan Lepore 1629e64f01a9SIan Lepore static void 1630ab00a509SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, const struct mmc_data *data) 1631831f5dcfSAlexander Motin { 1632831f5dcfSAlexander Motin uint16_t mode; 1633831f5dcfSAlexander Motin 1634831f5dcfSAlexander Motin if (data == NULL) 1635831f5dcfSAlexander Motin return; 1636831f5dcfSAlexander Motin 1637831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 1638*5d5ae066SIlya Bakulin if (data->len > 512 || data->block_count > 1) { 1639831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 1640*5d5ae066SIlya Bakulin if (data->block_count == 0 && __predict_true( 16416dea80e6SMarius Strobl #ifdef MMCCAM 16426dea80e6SMarius Strobl slot->ccb->mmcio.stop.opcode == MMC_STOP_TRANSMISSION && 16436dea80e6SMarius Strobl #else 16440519c933SMarius Strobl slot->req->stop != NULL && 16456dea80e6SMarius Strobl #endif 16466dea80e6SMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP))) 16476dea80e6SMarius Strobl mode |= SDHCI_TRNS_ACMD12; 16486dea80e6SMarius Strobl } 1649831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1650831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1651831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1652831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1653831f5dcfSAlexander Motin 1654831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1655831f5dcfSAlexander Motin } 1656831f5dcfSAlexander Motin 1657831f5dcfSAlexander Motin static void 1658831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1659831f5dcfSAlexander Motin { 1660831f5dcfSAlexander Motin int flags, timeout; 166190993663SIan Lepore uint32_t mask; 1662831f5dcfSAlexander Motin 1663831f5dcfSAlexander Motin slot->curcmd = cmd; 1664831f5dcfSAlexander Motin slot->cmd_done = 0; 1665831f5dcfSAlexander Motin 1666831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1667831f5dcfSAlexander Motin 1668831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1669831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1670831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1671831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1672e64f01a9SIan Lepore sdhci_req_done(slot); 1673831f5dcfSAlexander Motin return; 1674831f5dcfSAlexander Motin } 1675831f5dcfSAlexander Motin 1676b440e965SMarius Strobl /* 1677b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1678b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1679b440e965SMarius Strobl */ 16806e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1681d8208d9eSAlexander Motin slot->power == 0 || 1682d8208d9eSAlexander Motin slot->clock == 0) { 1683a94a63f0SWarner Losh slot_printf(slot, 1684a94a63f0SWarner Losh "Cannot issue a command (power=%d clock=%d)", 1685a94a63f0SWarner Losh slot->power, slot->clock); 1686831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1687e64f01a9SIan Lepore sdhci_req_done(slot); 1688831f5dcfSAlexander Motin return; 1689831f5dcfSAlexander Motin } 1690831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1691831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1692831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1693a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1694831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1695aca38eabSMarius Strobl /* 1696aca38eabSMarius Strobl * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1697aca38eabSMarius Strobl * that these latter are also special in that SDHCI_CMD_DATA should 1698aca38eabSMarius Strobl * be set below but no actual data is ever read from the controller. 1699aca38eabSMarius Strobl */ 1700a94a63f0SWarner Losh #ifdef MMCCAM 1701aca38eabSMarius Strobl if (cmd == &slot->ccb->mmcio.stop || 1702a94a63f0SWarner Losh #else 1703aca38eabSMarius Strobl if (cmd == slot->req->stop || 1704a94a63f0SWarner Losh #endif 1705aca38eabSMarius Strobl __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1706aca38eabSMarius Strobl cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1707aca38eabSMarius Strobl mask &= ~SDHCI_DAT_INHIBIT; 17088775ab45SIan Lepore /* 17098775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 17108775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 17118775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 17128775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 17138775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 17148775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 17158775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 17168775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 17178775ab45SIan Lepore */ 17188775ab45SIan Lepore timeout = 250; 171990993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1720831f5dcfSAlexander Motin if (timeout == 0) { 1721831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1722831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1723831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1724831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1725e64f01a9SIan Lepore sdhci_req_done(slot); 1726831f5dcfSAlexander Motin return; 1727831f5dcfSAlexander Motin } 1728831f5dcfSAlexander Motin timeout--; 1729831f5dcfSAlexander Motin DELAY(1000); 1730831f5dcfSAlexander Motin } 1731831f5dcfSAlexander Motin 1732831f5dcfSAlexander Motin /* Prepare command flags. */ 1733831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1734831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1735831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1736831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1737831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1738831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1739831f5dcfSAlexander Motin else 1740831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1741831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1742831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1743831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1744831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1745a94a63f0SWarner Losh if (cmd->data != NULL) 1746831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1747831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1748831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1749831f5dcfSAlexander Motin /* Prepare data. */ 1750831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1751831f5dcfSAlexander Motin /* 1752831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1753831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1754831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1755831f5dcfSAlexander Motin */ 1756831f5dcfSAlexander Motin if (slot->data_done == 0) { 1757831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1758831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1759831f5dcfSAlexander Motin } 1760831f5dcfSAlexander Motin /* Set command argument. */ 1761831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1762831f5dcfSAlexander Motin /* Set data transfer mode. */ 1763831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1764aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1765a94a63f0SWarner Losh slot_printf(slot, "Starting command!\n"); 1766831f5dcfSAlexander Motin /* Start command. */ 1767d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1768a6873fd1SIan Lepore /* Start timeout callout. */ 1769ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1770ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1771831f5dcfSAlexander Motin } 1772831f5dcfSAlexander Motin 1773831f5dcfSAlexander Motin static void 1774831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1775831f5dcfSAlexander Motin { 1776831f5dcfSAlexander Motin int i; 17771bacf3beSMarius Strobl uint32_t val; 17781bacf3beSMarius Strobl uint8_t extra; 1779831f5dcfSAlexander Motin 1780aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1781a94a63f0SWarner Losh slot_printf(slot, "%s: called, err %d flags %d\n", 1782a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1783831f5dcfSAlexander Motin slot->cmd_done = 1; 178472dec079SMarius Strobl /* 178572dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1786831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 178772dec079SMarius Strobl * happened first. 178872dec079SMarius Strobl */ 1789aca38eabSMarius Strobl if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1790aca38eabSMarius Strobl slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1791aca38eabSMarius Strobl WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1792aca38eabSMarius Strobl SDHCI_INT_RESPONSE); 1793831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1794831f5dcfSAlexander Motin if (slot->curcmd->error) { 1795aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1796aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1797831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1798831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1799831f5dcfSAlexander Motin sdhci_start(slot); 1800831f5dcfSAlexander Motin return; 1801831f5dcfSAlexander Motin } 1802831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1803831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1804831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1805831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 18061bacf3beSMarius Strobl extra = 0; 1807831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 18081bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 18091bacf3beSMarius Strobl if (slot->quirks & 18101bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1811677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1812677ee494SIan Lepore else { 1813677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1814677ee494SIan Lepore (val << 8) | extra; 1815831f5dcfSAlexander Motin extra = val >> 24; 1816831f5dcfSAlexander Motin } 1817677ee494SIan Lepore } 1818831f5dcfSAlexander Motin } else 1819831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1820831f5dcfSAlexander Motin } 1821aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1822a94a63f0SWarner Losh printf("Resp: %02x %02x %02x %02x\n", 1823a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1824a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1825a94a63f0SWarner Losh 1826831f5dcfSAlexander Motin /* If data ready - finish. */ 1827831f5dcfSAlexander Motin if (slot->data_done) 1828831f5dcfSAlexander Motin sdhci_start(slot); 1829831f5dcfSAlexander Motin } 1830831f5dcfSAlexander Motin 1831831f5dcfSAlexander Motin static void 1832ab00a509SMarius Strobl sdhci_start_data(struct sdhci_slot *slot, const struct mmc_data *data) 1833831f5dcfSAlexander Motin { 1834ab00a509SMarius Strobl uint32_t blkcnt, blksz, current_timeout, sdma_bbufsz, target_timeout; 1835831f5dcfSAlexander Motin uint8_t div; 1836831f5dcfSAlexander Motin 1837831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1838831f5dcfSAlexander Motin slot->data_done = 1; 1839831f5dcfSAlexander Motin return; 1840831f5dcfSAlexander Motin } 1841831f5dcfSAlexander Motin 1842831f5dcfSAlexander Motin slot->data_done = 0; 1843831f5dcfSAlexander Motin 1844831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1845831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1846ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1847ceb9e9f7SIan Lepore div = 0xE; 1848ceb9e9f7SIan Lepore } else { 1849831f5dcfSAlexander Motin target_timeout = 1000000; 1850831f5dcfSAlexander Motin div = 0; 1851831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1852ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1853ceb9e9f7SIan Lepore ++div; 1854831f5dcfSAlexander Motin current_timeout <<= 1; 1855831f5dcfSAlexander Motin } 1856831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1857ceb9e9f7SIan Lepore if (div < 0xE && 1858ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1859ceb9e9f7SIan Lepore ++div; 1860831f5dcfSAlexander Motin } 1861ceb9e9f7SIan Lepore } 1862831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1863831f5dcfSAlexander Motin 1864831f5dcfSAlexander Motin if (data == NULL) 1865831f5dcfSAlexander Motin return; 1866831f5dcfSAlexander Motin 1867831f5dcfSAlexander Motin /* Use DMA if possible. */ 1868831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1869831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1870ab00a509SMarius Strobl /* If data is small, broken DMA may return zeroes instead of data. */ 1871d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1872831f5dcfSAlexander Motin (data->len <= 512)) 1873831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1874831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1875d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1876831f5dcfSAlexander Motin ((data->len) & 0x3)) 1877831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1878831f5dcfSAlexander Motin /* Load DMA buffer. */ 1879831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1880ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 1881831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1882ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1883ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1884831f5dcfSAlexander Motin else { 1885ab00a509SMarius Strobl memcpy(slot->dmamem, data->data, ulmin(data->len, 1886ab00a509SMarius Strobl sdma_bbufsz)); 1887ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1888ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1889831f5dcfSAlexander Motin } 1890831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1891ab00a509SMarius Strobl /* 1892ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 1893ab00a509SMarius Strobl * bounce buffer and unmask otherwise. 1894ab00a509SMarius Strobl */ 1895ab00a509SMarius Strobl if (data->len == sdma_bbufsz) 1896831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1897831f5dcfSAlexander Motin else 1898831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1899831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1900831f5dcfSAlexander Motin } 1901831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1902831f5dcfSAlexander Motin slot->offset = 0; 1903*5d5ae066SIlya Bakulin #ifdef MMCCAM 1904*5d5ae066SIlya Bakulin if (data->flags & MMC_DATA_BLOCK_SIZE) { 1905*5d5ae066SIlya Bakulin /* Set block size and request border interrupts on the SDMA boundary. */ 1906*5d5ae066SIlya Bakulin blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, data->block_size); 1907*5d5ae066SIlya Bakulin blkcnt = data->block_count; 1908*5d5ae066SIlya Bakulin if (__predict_false(sdhci_debug > 0)) 1909*5d5ae066SIlya Bakulin slot_printf(slot, "SDIO Custom block params: blksz: " 1910*5d5ae066SIlya Bakulin "%#10x, blk cnt: %#10x\n", blksz, blkcnt); 1911*5d5ae066SIlya Bakulin } else 1912*5d5ae066SIlya Bakulin #endif 1913*5d5ae066SIlya Bakulin { 1914ab00a509SMarius Strobl /* Set block size and request border interrupts on the SDMA boundary. */ 1915ab00a509SMarius Strobl blksz = SDHCI_MAKE_BLKSZ(slot->sdma_boundary, ulmin(data->len, 512)); 1916ab00a509SMarius Strobl blkcnt = howmany(data->len, 512); 1917*5d5ae066SIlya Bakulin } 1918*5d5ae066SIlya Bakulin 1919*5d5ae066SIlya Bakulin WR2(slot, SDHCI_BLOCK_SIZE, blksz); 1920ab00a509SMarius Strobl WR2(slot, SDHCI_BLOCK_COUNT, blkcnt); 1921aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1922ab00a509SMarius Strobl slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 1923ab00a509SMarius Strobl blksz, blkcnt); 1924831f5dcfSAlexander Motin } 1925831f5dcfSAlexander Motin 1926c3a0f75aSOleksandr Tymoshenko void 1927831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1928831f5dcfSAlexander Motin { 1929831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 19307e6ccea3SMarius Strobl size_t left; 1931831f5dcfSAlexander Motin 1932831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1933ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1934831f5dcfSAlexander Motin * happened first. */ 1935831f5dcfSAlexander Motin if (!slot->cmd_done) { 1936831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1937831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1938831f5dcfSAlexander Motin } 1939831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1940915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1941915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 1942831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 19437e6ccea3SMarius Strobl left = data->len - slot->offset; 1944ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1945ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1946831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1947ab00a509SMarius Strobl ulmin(left, slot->sdma_bbufsz)); 1948831f5dcfSAlexander Motin } else 1949ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1950ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1951831f5dcfSAlexander Motin } 1952a98788edSIan Lepore slot->data_done = 1; 1953831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1954831f5dcfSAlexander Motin if (slot->curcmd->error) { 1955aca38eabSMarius Strobl if (slot->curcmd->error == MMC_ERR_BADCRC) 1956aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1957831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1958831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1959831f5dcfSAlexander Motin sdhci_start(slot); 1960831f5dcfSAlexander Motin return; 1961831f5dcfSAlexander Motin } 1962831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1963831f5dcfSAlexander Motin if (slot->cmd_done) 1964831f5dcfSAlexander Motin sdhci_start(slot); 1965831f5dcfSAlexander Motin } 1966831f5dcfSAlexander Motin 1967a94a63f0SWarner Losh #ifdef MMCCAM 1968a94a63f0SWarner Losh static void 1969a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 1970a94a63f0SWarner Losh { 1971a94a63f0SWarner Losh union ccb *ccb; 1972ab00a509SMarius Strobl struct ccb_mmcio *mmcio; 1973a94a63f0SWarner Losh 1974a94a63f0SWarner Losh ccb = slot->ccb; 1975a94a63f0SWarner Losh if (ccb == NULL) 1976a94a63f0SWarner Losh return; 1977a94a63f0SWarner Losh 1978a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1979a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 1980a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 1981a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 1982a94a63f0SWarner Losh return; 1983a94a63f0SWarner Losh } 1984a94a63f0SWarner Losh 1985a94a63f0SWarner Losh /* 1986a94a63f0SWarner Losh * Old stack doesn't use this! 1987a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 1988a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 1989a94a63f0SWarner Losh * Not using this code does no harm... 1990a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 1991a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 1992a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 1993a94a63f0SWarner Losh return; 1994a94a63f0SWarner Losh } 1995a94a63f0SWarner Losh */ 1996aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 1997a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 1998a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 1999a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 2000a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD); 2001a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_DATA); 2002a94a63f0SWarner Losh } 2003a94a63f0SWarner Losh 2004a94a63f0SWarner Losh sdhci_req_done(slot); 2005a94a63f0SWarner Losh } 2006a94a63f0SWarner Losh #else 2007831f5dcfSAlexander Motin static void 2008831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 2009831f5dcfSAlexander Motin { 2010ab00a509SMarius Strobl const struct mmc_request *req; 2011831f5dcfSAlexander Motin 2012831f5dcfSAlexander Motin req = slot->req; 2013831f5dcfSAlexander Motin if (req == NULL) 2014831f5dcfSAlexander Motin return; 2015831f5dcfSAlexander Motin 2016831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 2017831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 2018831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 2019831f5dcfSAlexander Motin return; 2020831f5dcfSAlexander Motin } 2021915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 2022915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 2023831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 2024831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 2025831f5dcfSAlexander Motin return; 2026831f5dcfSAlexander Motin } 2027aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 20285b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 20295b69a497SAlexander Motin if (!req->cmd->error && 2030915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 2031915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 2032915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 2033831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2034831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 2035831f5dcfSAlexander Motin } 2036831f5dcfSAlexander Motin 2037e64f01a9SIan Lepore sdhci_req_done(slot); 2038831f5dcfSAlexander Motin } 2039a94a63f0SWarner Losh #endif 2040831f5dcfSAlexander Motin 2041d6b3aaf8SOleksandr Tymoshenko int 2042b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 2043b440e965SMarius Strobl struct mmc_request *req) 2044831f5dcfSAlexander Motin { 2045831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2046831f5dcfSAlexander Motin 2047831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2048831f5dcfSAlexander Motin if (slot->req != NULL) { 2049831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2050831f5dcfSAlexander Motin return (EBUSY); 2051831f5dcfSAlexander Motin } 2052aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 20531bacf3beSMarius Strobl slot_printf(slot, 20541bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2055831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 20565b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 20575b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 20585b69a497SAlexander Motin } 2059831f5dcfSAlexander Motin slot->req = req; 2060831f5dcfSAlexander Motin slot->flags = 0; 2061831f5dcfSAlexander Motin sdhci_start(slot); 2062831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2063bea2dca2SAlexander Motin if (dumping) { 2064bea2dca2SAlexander Motin while (slot->req != NULL) { 2065d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 2066bea2dca2SAlexander Motin DELAY(10); 2067bea2dca2SAlexander Motin } 2068bea2dca2SAlexander Motin } 2069831f5dcfSAlexander Motin return (0); 2070831f5dcfSAlexander Motin } 2071831f5dcfSAlexander Motin 2072d6b3aaf8SOleksandr Tymoshenko int 2073b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 2074831f5dcfSAlexander Motin { 2075831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2076831f5dcfSAlexander Motin uint32_t val; 2077831f5dcfSAlexander Motin 2078831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2079831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 2080831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2081831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 2082831f5dcfSAlexander Motin } 2083831f5dcfSAlexander Motin 2084d6b3aaf8SOleksandr Tymoshenko int 2085b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 2086831f5dcfSAlexander Motin { 2087831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2088831f5dcfSAlexander Motin int err = 0; 2089831f5dcfSAlexander Motin 2090831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2091831f5dcfSAlexander Motin while (slot->bus_busy) 2092d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 2093831f5dcfSAlexander Motin slot->bus_busy++; 2094831f5dcfSAlexander Motin /* Activate led. */ 2095831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 2096831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2097831f5dcfSAlexander Motin return (err); 2098831f5dcfSAlexander Motin } 2099831f5dcfSAlexander Motin 2100d6b3aaf8SOleksandr Tymoshenko int 2101b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 2102831f5dcfSAlexander Motin { 2103831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 2104831f5dcfSAlexander Motin 2105831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2106831f5dcfSAlexander Motin /* Deactivate led. */ 2107831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 2108831f5dcfSAlexander Motin slot->bus_busy--; 2109831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2110d493985aSAlexander Motin wakeup(slot); 2111831f5dcfSAlexander Motin return (0); 2112831f5dcfSAlexander Motin } 2113831f5dcfSAlexander Motin 2114831f5dcfSAlexander Motin static void 2115831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 2116831f5dcfSAlexander Motin { 2117831f5dcfSAlexander Motin 2118831f5dcfSAlexander Motin if (!slot->curcmd) { 2119831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 2120831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2121831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2122831f5dcfSAlexander Motin return; 2123831f5dcfSAlexander Motin } 2124831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 2125831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2126831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 2127831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2128831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 2129831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 2130831f5dcfSAlexander Motin 2131831f5dcfSAlexander Motin sdhci_finish_command(slot); 2132831f5dcfSAlexander Motin } 2133831f5dcfSAlexander Motin 2134831f5dcfSAlexander Motin static void 2135831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 2136831f5dcfSAlexander Motin { 21371bacf3beSMarius Strobl struct mmc_data *data; 213815c440e1SWarner Losh size_t left; 2139ab00a509SMarius Strobl uint32_t sdma_bbufsz; 2140831f5dcfSAlexander Motin 2141831f5dcfSAlexander Motin if (!slot->curcmd) { 2142831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2143831f5dcfSAlexander Motin "there is no active command.\n", intmask); 2144831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2145831f5dcfSAlexander Motin return; 2146831f5dcfSAlexander Motin } 2147831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2148831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2149831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2150831f5dcfSAlexander Motin "there is no active data operation.\n", 2151831f5dcfSAlexander Motin intmask); 2152831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2153831f5dcfSAlexander Motin return; 2154831f5dcfSAlexander Motin } 2155831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 2156831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 2157acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2158831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 2159831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 2160831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2161831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 2162831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 2163831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 2164831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2165831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 2166831f5dcfSAlexander Motin } 2167831f5dcfSAlexander Motin if (slot->curcmd->error) { 2168831f5dcfSAlexander Motin /* No need to continue after any error. */ 2169a98788edSIan Lepore goto done; 2170831f5dcfSAlexander Motin } 2171831f5dcfSAlexander Motin 2172aca38eabSMarius Strobl /* Handle tuning completion interrupt. */ 2173aca38eabSMarius Strobl if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2174aca38eabSMarius Strobl (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2175aca38eabSMarius Strobl slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2176aca38eabSMarius Strobl slot->req->flags |= MMC_TUNE_DONE; 2177aca38eabSMarius Strobl sdhci_finish_command(slot); 2178aca38eabSMarius Strobl sdhci_finish_data(slot); 2179aca38eabSMarius Strobl return; 2180aca38eabSMarius Strobl } 2181831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 2182c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2183c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2184c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 21851bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 21861bacf3beSMarius Strobl &intmask); 2187c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 2188c3a0f75aSOleksandr Tymoshenko } else 2189831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 2190c3a0f75aSOleksandr Tymoshenko } 2191831f5dcfSAlexander Motin /* Handle DMA border. */ 2192831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 21931bacf3beSMarius Strobl data = slot->curcmd->data; 2194ab00a509SMarius Strobl sdma_bbufsz = slot->sdma_bbufsz; 2195831f5dcfSAlexander Motin 2196831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 2197831f5dcfSAlexander Motin left = data->len - slot->offset; 2198831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2199831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2200831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 2201831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2202ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2203831f5dcfSAlexander Motin } else { 2204831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2205831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 2206831f5dcfSAlexander Motin } 2207831f5dcfSAlexander Motin /* ... and reload it again. */ 2208ab00a509SMarius Strobl slot->offset += sdma_bbufsz; 2209831f5dcfSAlexander Motin left = data->len - slot->offset; 2210831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 2211831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2212831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 2213831f5dcfSAlexander Motin } else { 2214831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2215ab00a509SMarius Strobl ulmin(left, sdma_bbufsz)); 2216831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 2217831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 2218831f5dcfSAlexander Motin } 2219ab00a509SMarius Strobl /* 2220ab00a509SMarius Strobl * Interrupt aggregation: Mask border interrupt for the last 2221ab00a509SMarius Strobl * bounce buffer. 2222ab00a509SMarius Strobl */ 2223ab00a509SMarius Strobl if (left == sdma_bbufsz) { 2224831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 2225831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2226831f5dcfSAlexander Motin } 2227831f5dcfSAlexander Motin /* Restart DMA. */ 2228831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2229831f5dcfSAlexander Motin } 2230831f5dcfSAlexander Motin /* We have got all data. */ 2231c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 2232c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 2233c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 2234c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2235c3a0f75aSOleksandr Tymoshenko } else 2236831f5dcfSAlexander Motin sdhci_finish_data(slot); 2237831f5dcfSAlexander Motin } 2238a98788edSIan Lepore done: 2239a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2240a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 2241a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 2242a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2243a98788edSIan Lepore } else 2244a98788edSIan Lepore sdhci_finish_data(slot); 2245a98788edSIan Lepore } 2246c3a0f75aSOleksandr Tymoshenko } 2247831f5dcfSAlexander Motin 2248831f5dcfSAlexander Motin static void 22496dea80e6SMarius Strobl sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err) 2250831f5dcfSAlexander Motin { 2251831f5dcfSAlexander Motin 2252831f5dcfSAlexander Motin if (!slot->curcmd) { 2253831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 22546dea80e6SMarius Strobl "there is no active command.\n", acmd_err); 2255831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2256831f5dcfSAlexander Motin return; 2257831f5dcfSAlexander Motin } 22586dea80e6SMarius Strobl slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err); 2259831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 2260831f5dcfSAlexander Motin } 2261831f5dcfSAlexander Motin 2262d6b3aaf8SOleksandr Tymoshenko void 2263d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 2264831f5dcfSAlexander Motin { 22652b96b955SJustin Hibbits uint32_t intmask, present; 22666dea80e6SMarius Strobl uint16_t val16; 2267831f5dcfSAlexander Motin 2268831f5dcfSAlexander Motin SDHCI_LOCK(slot); 2269831f5dcfSAlexander Motin /* Read slot interrupt status. */ 2270831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 2271831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 2272831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2273d6b3aaf8SOleksandr Tymoshenko return; 2274831f5dcfSAlexander Motin } 2275aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 2)) 22765b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 22775b69a497SAlexander Motin 2278aca38eabSMarius Strobl /* Handle tuning error interrupt. */ 2279aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 22806dea80e6SMarius Strobl WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR); 2281aca38eabSMarius Strobl slot_printf(slot, "Tuning error indicated\n"); 2282aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2283aca38eabSMarius Strobl if (slot->curcmd) { 2284aca38eabSMarius Strobl slot->curcmd->error = MMC_ERR_BADCRC; 2285aca38eabSMarius Strobl sdhci_finish_command(slot); 2286aca38eabSMarius Strobl } 2287aca38eabSMarius Strobl } 2288aca38eabSMarius Strobl /* Handle re-tuning interrupt. */ 2289aca38eabSMarius Strobl if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2290aca38eabSMarius Strobl slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2291831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 2292831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2293639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 22942b96b955SJustin Hibbits slot->intmask &= 22952b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 22962b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 22972b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 22982b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 22992b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2300831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 2301831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2302b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 2303831f5dcfSAlexander Motin } 2304831f5dcfSAlexander Motin /* Handle command interrupts. */ 2305831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 2306831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2307831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2308831f5dcfSAlexander Motin } 2309831f5dcfSAlexander Motin /* Handle data interrupts. */ 2310831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 2311831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 23127e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 23137e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2314831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2315831f5dcfSAlexander Motin } 2316831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 2317831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 23186dea80e6SMarius Strobl /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */ 23196dea80e6SMarius Strobl val16 = RD2(slot, SDHCI_ACMD12_ERR); 2320831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 23216dea80e6SMarius Strobl sdhci_acmd_irq(slot, val16); 2322831f5dcfSAlexander Motin } 2323831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 2324831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 2325831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2326aca38eabSMarius Strobl slot_printf(slot, "Card is consuming too much power!\n"); 2327831f5dcfSAlexander Motin } 2328aca38eabSMarius Strobl intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2329aca38eabSMarius Strobl SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2330aca38eabSMarius Strobl SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2331831f5dcfSAlexander Motin /* The rest is unknown. */ 2332831f5dcfSAlexander Motin if (intmask) { 2333831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 2334831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2335831f5dcfSAlexander Motin intmask); 2336831f5dcfSAlexander Motin sdhci_dumpregs(slot); 2337831f5dcfSAlexander Motin } 2338831f5dcfSAlexander Motin 2339831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 2340831f5dcfSAlexander Motin } 2341831f5dcfSAlexander Motin 2342d6b3aaf8SOleksandr Tymoshenko int 23431bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 23441bacf3beSMarius Strobl uintptr_t *result) 2345831f5dcfSAlexander Motin { 2346ab00a509SMarius Strobl const struct sdhci_slot *slot = device_get_ivars(child); 2347831f5dcfSAlexander Motin 2348831f5dcfSAlexander Motin switch (which) { 2349831f5dcfSAlexander Motin default: 2350831f5dcfSAlexander Motin return (EINVAL); 2351831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2352bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 2353831f5dcfSAlexander Motin break; 2354831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2355bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 2356831f5dcfSAlexander Motin break; 2357831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2358bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 2359831f5dcfSAlexander Motin break; 2360831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2361bcd91d25SJayachandran C. *result = slot->host.ios.clock; 2362831f5dcfSAlexander Motin break; 2363831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2364bcd91d25SJayachandran C. *result = slot->host.f_min; 2365831f5dcfSAlexander Motin break; 2366831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 2367bcd91d25SJayachandran C. *result = slot->host.f_max; 2368831f5dcfSAlexander Motin break; 2369831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2370bcd91d25SJayachandran C. *result = slot->host.host_ocr; 2371831f5dcfSAlexander Motin break; 2372831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2373bcd91d25SJayachandran C. *result = slot->host.mode; 2374831f5dcfSAlexander Motin break; 2375831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2376bcd91d25SJayachandran C. *result = slot->host.ocr; 2377831f5dcfSAlexander Motin break; 2378831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2379bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 2380831f5dcfSAlexander Motin break; 2381831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2382bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 2383831f5dcfSAlexander Motin break; 2384aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2385aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED) { 2386aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2387aca38eabSMarius Strobl *result = retune_req_reset; 2388aca38eabSMarius Strobl break; 2389aca38eabSMarius Strobl } 2390aca38eabSMarius Strobl if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2391aca38eabSMarius Strobl *result = retune_req_normal; 2392aca38eabSMarius Strobl break; 2393aca38eabSMarius Strobl } 2394aca38eabSMarius Strobl } 2395aca38eabSMarius Strobl *result = retune_req_none; 2396aca38eabSMarius Strobl break; 23970f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 23980f34084fSMarius Strobl *result = slot->host.ios.vccq; 23990f34084fSMarius Strobl break; 2400831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2401bcd91d25SJayachandran C. *result = slot->host.caps; 2402831f5dcfSAlexander Motin break; 2403831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2404bcd91d25SJayachandran C. *result = slot->host.ios.timing; 2405831f5dcfSAlexander Motin break; 24063a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2407aca38eabSMarius Strobl /* 2408aca38eabSMarius Strobl * Re-tuning modes 1 and 2 restrict the maximum data length 2409aca38eabSMarius Strobl * per read/write command to 4 MiB. 2410aca38eabSMarius Strobl */ 2411aca38eabSMarius Strobl if (slot->opt & SDHCI_TUNING_ENABLED && 2412aca38eabSMarius Strobl (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2413aca38eabSMarius Strobl slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2414aca38eabSMarius Strobl *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2415aca38eabSMarius Strobl break; 2416aca38eabSMarius Strobl } 2417bcd91d25SJayachandran C. *result = 65535; 24183a4a2557SAlexander Motin break; 241972dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 242072dec079SMarius Strobl /* 242172dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 242272dec079SMarius Strobl */ 242372dec079SMarius Strobl *result = 1000000; 242472dec079SMarius Strobl break; 2425831f5dcfSAlexander Motin } 2426831f5dcfSAlexander Motin return (0); 2427831f5dcfSAlexander Motin } 2428831f5dcfSAlexander Motin 2429d6b3aaf8SOleksandr Tymoshenko int 24301bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 24311bacf3beSMarius Strobl uintptr_t value) 2432831f5dcfSAlexander Motin { 2433831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 2434b440e965SMarius Strobl uint32_t clock, max_clock; 2435b440e965SMarius Strobl int i; 2436831f5dcfSAlexander Motin 243715c440e1SWarner Losh if (sdhci_debug > 1) 243815c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 2439831f5dcfSAlexander Motin switch (which) { 2440831f5dcfSAlexander Motin default: 2441831f5dcfSAlexander Motin return (EINVAL); 2442831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 2443831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 2444831f5dcfSAlexander Motin break; 2445831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 2446831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 2447831f5dcfSAlexander Motin break; 2448831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 2449831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 2450831f5dcfSAlexander Motin break; 2451831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 2452831f5dcfSAlexander Motin if (value > 0) { 245357677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 245457677a3aSOleksandr Tymoshenko clock = max_clock; 245557677a3aSOleksandr Tymoshenko 245657677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 245757677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 245857677a3aSOleksandr Tymoshenko i <<= 1) { 2459831f5dcfSAlexander Motin if (clock <= value) 2460831f5dcfSAlexander Motin break; 2461831f5dcfSAlexander Motin clock >>= 1; 2462831f5dcfSAlexander Motin } 2463b440e965SMarius Strobl } else { 246457677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 246557677a3aSOleksandr Tymoshenko i += 2) { 246657677a3aSOleksandr Tymoshenko if (clock <= value) 246757677a3aSOleksandr Tymoshenko break; 246857677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 246957677a3aSOleksandr Tymoshenko } 247057677a3aSOleksandr Tymoshenko } 247157677a3aSOleksandr Tymoshenko 2472831f5dcfSAlexander Motin slot->host.ios.clock = clock; 2473831f5dcfSAlexander Motin } else 2474831f5dcfSAlexander Motin slot->host.ios.clock = 0; 2475831f5dcfSAlexander Motin break; 2476831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 2477831f5dcfSAlexander Motin slot->host.mode = value; 2478831f5dcfSAlexander Motin break; 2479831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 2480831f5dcfSAlexander Motin slot->host.ocr = value; 2481831f5dcfSAlexander Motin break; 2482831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 2483831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 2484831f5dcfSAlexander Motin break; 2485831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 2486831f5dcfSAlexander Motin slot->host.ios.vdd = value; 2487831f5dcfSAlexander Motin break; 24880f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 24890f34084fSMarius Strobl slot->host.ios.vccq = value; 24900f34084fSMarius Strobl break; 2491831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 2492831f5dcfSAlexander Motin slot->host.ios.timing = value; 2493831f5dcfSAlexander Motin break; 2494831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 2495831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 2496831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 2497831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 24983a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 2499aca38eabSMarius Strobl case MMCBR_IVAR_RETUNE_REQ: 2500831f5dcfSAlexander Motin return (EINVAL); 2501831f5dcfSAlexander Motin } 2502831f5dcfSAlexander Motin return (0); 2503831f5dcfSAlexander Motin } 2504831f5dcfSAlexander Motin 250515c440e1SWarner Losh #ifdef MMCCAM 2506a94a63f0SWarner Losh void 2507d91f1a10SIlya Bakulin sdhci_start_slot(struct sdhci_slot *slot) 2508a94a63f0SWarner Losh { 2509ab00a509SMarius Strobl 2510505f6a0cSBjoern A. Zeeb if ((slot->devq = cam_simq_alloc(1)) == NULL) 2511a94a63f0SWarner Losh goto fail; 2512a94a63f0SWarner Losh 2513a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2514a94a63f0SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2515a94a63f0SWarner Losh "sdhci_slot", slot, device_get_unit(slot->bus), 2516a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 2517a94a63f0SWarner Losh 2518a94a63f0SWarner Losh if (slot->sim == NULL) { 2519a94a63f0SWarner Losh cam_simq_free(slot->devq); 2520a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2521a94a63f0SWarner Losh goto fail; 2522a94a63f0SWarner Losh } 2523a94a63f0SWarner Losh 2524a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2525a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2526505f6a0cSBjoern A. Zeeb slot_printf(slot, "cannot register SCSI pass-through bus\n"); 2527a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2528a94a63f0SWarner Losh cam_simq_free(slot->devq); 2529a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2530a94a63f0SWarner Losh goto fail; 2531a94a63f0SWarner Losh } 2532a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2533505f6a0cSBjoern A. Zeeb 2534a94a63f0SWarner Losh /* End CAM-specific init */ 2535a94a63f0SWarner Losh slot->card_present = 0; 2536a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2537a94a63f0SWarner Losh return; 2538a94a63f0SWarner Losh 2539a94a63f0SWarner Losh fail: 2540a94a63f0SWarner Losh if (slot->sim != NULL) { 2541a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2542a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2543a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2544a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2545a94a63f0SWarner Losh } 2546a94a63f0SWarner Losh 2547a94a63f0SWarner Losh if (slot->devq != NULL) 2548a94a63f0SWarner Losh cam_simq_free(slot->devq); 2549a94a63f0SWarner Losh } 2550a94a63f0SWarner Losh 2551a94a63f0SWarner Losh static void 2552a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb) 2553a94a63f0SWarner Losh { 2554a94a63f0SWarner Losh struct sdhci_slot *slot; 2555a94a63f0SWarner Losh 2556a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2557a94a63f0SWarner Losh 2558a94a63f0SWarner Losh sdhci_cam_request(slot, ccb); 2559a94a63f0SWarner Losh } 2560a94a63f0SWarner Losh 2561a94a63f0SWarner Losh void 2562a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2563a94a63f0SWarner Losh { 2564a94a63f0SWarner Losh struct sdhci_slot *slot; 2565a94a63f0SWarner Losh 2566a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2567a94a63f0SWarner Losh if (slot == NULL) { 2568a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2569a94a63f0SWarner Losh xpt_done(ccb); 2570a94a63f0SWarner Losh return; 2571a94a63f0SWarner Losh } 2572a94a63f0SWarner Losh 2573a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2574a94a63f0SWarner Losh 2575a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2576a94a63f0SWarner Losh case XPT_PATH_INQ: 2577a94a63f0SWarner Losh { 2578a94a63f0SWarner Losh struct ccb_pathinq *cpi; 2579a94a63f0SWarner Losh 2580a94a63f0SWarner Losh cpi = &ccb->cpi; 2581a94a63f0SWarner Losh cpi->version_num = 1; 2582a94a63f0SWarner Losh cpi->hba_inquiry = 0; 2583a94a63f0SWarner Losh cpi->target_sprt = 0; 2584a94a63f0SWarner Losh cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 2585a94a63f0SWarner Losh cpi->hba_eng_cnt = 0; 2586a94a63f0SWarner Losh cpi->max_target = 0; 2587a94a63f0SWarner Losh cpi->max_lun = 0; 2588a94a63f0SWarner Losh cpi->initiator_id = 1; 2589a94a63f0SWarner Losh cpi->maxio = MAXPHYS; 2590a94a63f0SWarner Losh strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2591a94a63f0SWarner Losh strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN); 2592a94a63f0SWarner Losh strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2593a94a63f0SWarner Losh cpi->unit_number = cam_sim_unit(sim); 2594a94a63f0SWarner Losh cpi->bus_id = cam_sim_bus(sim); 2595a94a63f0SWarner Losh cpi->base_transfer_speed = 100; /* XXX WTF? */ 2596a94a63f0SWarner Losh cpi->protocol = PROTO_MMCSD; 2597a94a63f0SWarner Losh cpi->protocol_version = SCSI_REV_0; 2598a94a63f0SWarner Losh cpi->transport = XPORT_MMCSD; 2599a94a63f0SWarner Losh cpi->transport_version = 0; 2600a94a63f0SWarner Losh 2601a94a63f0SWarner Losh cpi->ccb_h.status = CAM_REQ_CMP; 2602a94a63f0SWarner Losh break; 2603a94a63f0SWarner Losh } 2604a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2605a94a63f0SWarner Losh { 2606a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 26075d20e651SIlya Bakulin uint32_t max_data; 2608a94a63f0SWarner Losh 2609a94a63f0SWarner Losh if (sdhci_debug > 1) 2610a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2611a94a63f0SWarner Losh 2612a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2613a94a63f0SWarner Losh cts->protocol_version = 1; 2614a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2615a94a63f0SWarner Losh cts->transport_version = 1; 2616a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2617a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2618a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2619a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2620a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 26215d20e651SIlya Bakulin /* 26225d20e651SIlya Bakulin * Re-tuning modes 1 and 2 restrict the maximum data length 26235d20e651SIlya Bakulin * per read/write command to 4 MiB. 26245d20e651SIlya Bakulin */ 26255d20e651SIlya Bakulin if (slot->opt & SDHCI_TUNING_ENABLED && 26265d20e651SIlya Bakulin (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 26275d20e651SIlya Bakulin slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 26285d20e651SIlya Bakulin max_data = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 26295d20e651SIlya Bakulin } else { 26305d20e651SIlya Bakulin max_data = 65535; 26315d20e651SIlya Bakulin } 26325d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = max_data; 26335d20e651SIlya Bakulin 2634a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2635a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2636a94a63f0SWarner Losh break; 2637a94a63f0SWarner Losh } 2638a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2639a94a63f0SWarner Losh { 2640a94a63f0SWarner Losh if (sdhci_debug > 1) 2641a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2642a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2643a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2644a94a63f0SWarner Losh break; 2645a94a63f0SWarner Losh } 2646a94a63f0SWarner Losh case XPT_RESET_BUS: 2647a94a63f0SWarner Losh if (sdhci_debug > 1) 2648a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2649a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2650a94a63f0SWarner Losh break; 2651a94a63f0SWarner Losh case XPT_MMC_IO: 2652a94a63f0SWarner Losh /* 2653a94a63f0SWarner Losh * Here is the HW-dependent part of 2654a94a63f0SWarner Losh * sending the command to the underlying h/w 2655a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2656a94a63f0SWarner Losh * Then the request will be marked as completed. 2657a94a63f0SWarner Losh */ 2658aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) 2659a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2660a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2661a94a63f0SWarner Losh 2662a94a63f0SWarner Losh sdhci_cam_handle_mmcio(sim, ccb); 2663a94a63f0SWarner Losh return; 2664a94a63f0SWarner Losh /* NOTREACHED */ 2665a94a63f0SWarner Losh break; 2666a94a63f0SWarner Losh default: 2667a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2668a94a63f0SWarner Losh break; 2669a94a63f0SWarner Losh } 2670a94a63f0SWarner Losh xpt_done(ccb); 2671a94a63f0SWarner Losh return; 2672a94a63f0SWarner Losh } 2673a94a63f0SWarner Losh 2674a94a63f0SWarner Losh void 2675a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2676a94a63f0SWarner Losh { 2677a94a63f0SWarner Losh return; 2678a94a63f0SWarner Losh } 2679a94a63f0SWarner Losh 26806dea80e6SMarius Strobl static int 2681ab00a509SMarius Strobl sdhci_cam_get_possible_host_clock(const struct sdhci_slot *slot, 2682ab00a509SMarius Strobl int proposed_clock) 26836dea80e6SMarius Strobl { 2684a94a63f0SWarner Losh int max_clock, clock, i; 2685a94a63f0SWarner Losh 2686a94a63f0SWarner Losh if (proposed_clock == 0) 2687a94a63f0SWarner Losh return 0; 2688a94a63f0SWarner Losh max_clock = slot->max_clk; 2689a94a63f0SWarner Losh clock = max_clock; 2690a94a63f0SWarner Losh 2691a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2692505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_200_MAX_DIVIDER; i <<= 1) { 2693a94a63f0SWarner Losh if (clock <= proposed_clock) 2694a94a63f0SWarner Losh break; 2695a94a63f0SWarner Losh clock >>= 1; 2696a94a63f0SWarner Losh } 2697a94a63f0SWarner Losh } else { 2698505f6a0cSBjoern A. Zeeb for (i = 0; i < SDHCI_300_MAX_DIVIDER; i += 2) { 2699a94a63f0SWarner Losh if (clock <= proposed_clock) 2700a94a63f0SWarner Losh break; 2701a94a63f0SWarner Losh clock = max_clock / (i + 2); 2702a94a63f0SWarner Losh } 2703a94a63f0SWarner Losh } 2704a94a63f0SWarner Losh return clock; 2705a94a63f0SWarner Losh } 2706a94a63f0SWarner Losh 2707ab00a509SMarius Strobl static int 2708a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2709a94a63f0SWarner Losh { 2710a94a63f0SWarner Losh struct mmc_ios *ios; 2711ab00a509SMarius Strobl const struct mmc_ios *new_ios; 2712ab00a509SMarius Strobl const struct ccb_trans_settings_mmc *cts; 2713a94a63f0SWarner Losh 2714a94a63f0SWarner Losh ios = &slot->host.ios; 2715a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2716a94a63f0SWarner Losh new_ios = &cts->ios; 2717a94a63f0SWarner Losh 2718a94a63f0SWarner Losh /* Update only requested fields */ 2719a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2720a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2721a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2722a94a63f0SWarner Losh } 2723a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2724a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2725a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2726a94a63f0SWarner Losh } 2727a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2728a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2729a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2730a94a63f0SWarner Losh } 2731a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2732a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2733a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2734a94a63f0SWarner Losh } 2735a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2736a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2737a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2738a94a63f0SWarner Losh } 2739a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2740a94a63f0SWarner Losh ios->timing = new_ios->timing; 2741a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2742a94a63f0SWarner Losh } 2743a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2744a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2745a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2746a94a63f0SWarner Losh } 2747a94a63f0SWarner Losh 2748a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2749a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2750a94a63f0SWarner Losh } 2751a94a63f0SWarner Losh 2752ab00a509SMarius Strobl static int 2753a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2754a94a63f0SWarner Losh { 2755a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2756a94a63f0SWarner Losh 2757a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2758a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2759a94a63f0SWarner Losh SDHCI_LOCK(slot); 2760a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2761a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2762a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2763a94a63f0SWarner Losh sdhci_init(slot); 2764a94a63f0SWarner Losh } 2765a94a63f0SWarner Losh /* Configure the bus. */ 2766a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2767a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2768a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2769a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2770a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2771a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2772a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2773a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2774a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2775a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2776a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2777a94a63f0SWarner Losh } else { 2778a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2779a94a63f0SWarner Losh } 2780a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2781a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2782a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2783a94a63f0SWarner Losh else 2784a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2785a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2786a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2787a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2788a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2789a94a63f0SWarner Losh 2790a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2791a94a63f0SWarner Losh return (0); 2792a94a63f0SWarner Losh } 2793a94a63f0SWarner Losh 2794ab00a509SMarius Strobl static int 2795a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2796a94a63f0SWarner Losh { 2797ab00a509SMarius Strobl const struct ccb_mmcio *mmcio; 2798a94a63f0SWarner Losh 2799a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2800a94a63f0SWarner Losh 2801a94a63f0SWarner Losh SDHCI_LOCK(slot); 2802a94a63f0SWarner Losh /* if (slot->req != NULL) { 2803a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2804a94a63f0SWarner Losh return (EBUSY); 2805a94a63f0SWarner Losh } 2806a94a63f0SWarner Losh */ 2807aca38eabSMarius Strobl if (__predict_false(sdhci_debug > 1)) { 2808*5d5ae066SIlya Bakulin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x " 2809*5d5ae066SIlya Bakulin "blksz=%zu blkcnt=%zu\n", 2810a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2811a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2812*5d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags : 0, 2813*5d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_size : 0, 2814*5d5ae066SIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->block_count : 0); 2815a94a63f0SWarner Losh } 2816a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2817a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2818a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2819a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2820a94a63f0SWarner Losh } 2821a94a63f0SWarner Losh slot->ccb = ccb; 2822a94a63f0SWarner Losh slot->flags = 0; 2823a94a63f0SWarner Losh sdhci_start(slot); 2824a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2825a94a63f0SWarner Losh if (dumping) { 2826a94a63f0SWarner Losh while (slot->ccb != NULL) { 2827a94a63f0SWarner Losh sdhci_generic_intr(slot); 2828a94a63f0SWarner Losh DELAY(10); 2829a94a63f0SWarner Losh } 2830a94a63f0SWarner Losh } 2831a94a63f0SWarner Losh return (0); 2832a94a63f0SWarner Losh } 283315c440e1SWarner Losh #endif /* MMCCAM */ 2834a94a63f0SWarner Losh 2835ab00a509SMarius Strobl MODULE_VERSION(sdhci, SDHCI_VERSION); 2836