1831f5dcfSAlexander Motin /*- 2831f5dcfSAlexander Motin * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org> 3831f5dcfSAlexander Motin * All rights reserved. 4831f5dcfSAlexander Motin * 5831f5dcfSAlexander Motin * Redistribution and use in source and binary forms, with or without 6831f5dcfSAlexander Motin * modification, are permitted provided that the following conditions 7831f5dcfSAlexander Motin * are met: 8831f5dcfSAlexander Motin * 1. Redistributions of source code must retain the above copyright 9831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer. 10831f5dcfSAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 11831f5dcfSAlexander Motin * notice, this list of conditions and the following disclaimer in the 12831f5dcfSAlexander Motin * documentation and/or other materials provided with the distribution. 13831f5dcfSAlexander Motin * 14831f5dcfSAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15831f5dcfSAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16831f5dcfSAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17831f5dcfSAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18831f5dcfSAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19831f5dcfSAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20831f5dcfSAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21831f5dcfSAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22831f5dcfSAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23831f5dcfSAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24831f5dcfSAlexander Motin */ 25831f5dcfSAlexander Motin 26831f5dcfSAlexander Motin #include <sys/cdefs.h> 27831f5dcfSAlexander Motin __FBSDID("$FreeBSD$"); 28831f5dcfSAlexander Motin 29831f5dcfSAlexander Motin #include <sys/param.h> 30831f5dcfSAlexander Motin #include <sys/systm.h> 31831f5dcfSAlexander Motin #include <sys/bus.h> 32e64f01a9SIan Lepore #include <sys/callout.h> 33831f5dcfSAlexander Motin #include <sys/conf.h> 34831f5dcfSAlexander Motin #include <sys/kernel.h> 35831f5dcfSAlexander Motin #include <sys/lock.h> 36831f5dcfSAlexander Motin #include <sys/module.h> 37831f5dcfSAlexander Motin #include <sys/mutex.h> 38831f5dcfSAlexander Motin #include <sys/resource.h> 39831f5dcfSAlexander Motin #include <sys/rman.h> 405b69a497SAlexander Motin #include <sys/sysctl.h> 41831f5dcfSAlexander Motin #include <sys/taskqueue.h> 42831f5dcfSAlexander Motin 43831f5dcfSAlexander Motin #include <machine/bus.h> 44831f5dcfSAlexander Motin #include <machine/resource.h> 45831f5dcfSAlexander Motin #include <machine/stdarg.h> 46831f5dcfSAlexander Motin 47831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 48831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 49831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 50831f5dcfSAlexander Motin 51831f5dcfSAlexander Motin #include "mmcbr_if.h" 52831f5dcfSAlexander Motin #include "sdhci.h" 53d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 54831f5dcfSAlexander Motin 55f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 56831f5dcfSAlexander Motin 57f0d2731dSMarius Strobl static int sdhci_debug; 587e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 597e6ccea3SMarius Strobl "Debug level"); 605b69a497SAlexander Motin 61d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 62d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 63d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 64d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 65d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 66831f5dcfSAlexander Motin 67d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 68d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 69d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 70d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 71d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 72831f5dcfSAlexander Motin 73831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 74831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 75831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 76831f5dcfSAlexander Motin 77639f59f0SIan Lepore static void sdhci_card_poll(void *); 78831f5dcfSAlexander Motin static void sdhci_card_task(void *, int); 79831f5dcfSAlexander Motin 80831f5dcfSAlexander Motin /* helper routines */ 81831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 82831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 83831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 84831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 85831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 86831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 87831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 88831f5dcfSAlexander Motin 8933aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 9033aad34dSOleksandr Tymoshenko 9157677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 9257677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 9357677a3aSOleksandr Tymoshenko 94639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 95639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 96639f59f0SIan Lepore 9793efdc63SAdrian Chadd /* 9893efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 9993efdc63SAdrian Chadd */ 1001bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1011bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1021bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1031bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 10493efdc63SAdrian Chadd 10593efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 10693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 10793efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 10893efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 10993efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 11093efdc63SAdrian Chadd 111831f5dcfSAlexander Motin static void 112831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 113831f5dcfSAlexander Motin { 1147e6ccea3SMarius Strobl 115831f5dcfSAlexander Motin if (error != 0) { 116831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 117831f5dcfSAlexander Motin return; 118831f5dcfSAlexander Motin } 119831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 120831f5dcfSAlexander Motin } 121831f5dcfSAlexander Motin 122d6b3aaf8SOleksandr Tymoshenko static int 123d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 124d6b3aaf8SOleksandr Tymoshenko { 125d6b3aaf8SOleksandr Tymoshenko va_list ap; 126d6b3aaf8SOleksandr Tymoshenko int retval; 127d6b3aaf8SOleksandr Tymoshenko 128d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 129d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 130d6b3aaf8SOleksandr Tymoshenko 131d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 132d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 133d6b3aaf8SOleksandr Tymoshenko va_end(ap); 134d6b3aaf8SOleksandr Tymoshenko return (retval); 135d6b3aaf8SOleksandr Tymoshenko } 136d6b3aaf8SOleksandr Tymoshenko 137831f5dcfSAlexander Motin static void 138831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 139831f5dcfSAlexander Motin { 1407e6ccea3SMarius Strobl 141831f5dcfSAlexander Motin slot_printf(slot, 142831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 143831f5dcfSAlexander Motin 144831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 145831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 146831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 147831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 148831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 149831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 150831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 151831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 152831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 153831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 154831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 155831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 156831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 157831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 158831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 159831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 160831f5dcfSAlexander Motin slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n", 161831f5dcfSAlexander Motin RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS)); 162831f5dcfSAlexander Motin slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n", 163831f5dcfSAlexander Motin RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT)); 164831f5dcfSAlexander Motin 165831f5dcfSAlexander Motin slot_printf(slot, 166831f5dcfSAlexander Motin "===========================================\n"); 167831f5dcfSAlexander Motin } 168831f5dcfSAlexander Motin 169831f5dcfSAlexander Motin static void 170831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 171831f5dcfSAlexander Motin { 172831f5dcfSAlexander Motin int timeout; 173*b440e965SMarius Strobl uint32_t clock; 174831f5dcfSAlexander Motin 175d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 1766e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 177831f5dcfSAlexander Motin return; 178831f5dcfSAlexander Motin } 179831f5dcfSAlexander Motin 180831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 181831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 182d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 183831f5dcfSAlexander Motin /* This is to force an update */ 184831f5dcfSAlexander Motin clock = slot->clock; 185831f5dcfSAlexander Motin slot->clock = 0; 186831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 187831f5dcfSAlexander Motin } 188831f5dcfSAlexander Motin 189d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 190831f5dcfSAlexander Motin slot->clock = 0; 191d8208d9eSAlexander Motin slot->power = 0; 192d8208d9eSAlexander Motin } 193831f5dcfSAlexander Motin 19461bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 19561bc42f7SIan Lepore 19661bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 19761bc42f7SIan Lepore /* 19861bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 19961bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 20061bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 20161bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 20261bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 20361bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 20461bc42f7SIan Lepore */ 20561bc42f7SIan Lepore timeout = 10000; 20661bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 20761bc42f7SIan Lepore if (timeout <= 0) 20861bc42f7SIan Lepore break; 20961bc42f7SIan Lepore timeout--; 21061bc42f7SIan Lepore DELAY(1); 21161bc42f7SIan Lepore } 21261bc42f7SIan Lepore } 21361bc42f7SIan Lepore 214831f5dcfSAlexander Motin /* Wait max 100 ms */ 21561bc42f7SIan Lepore timeout = 10000; 216831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 21761bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 21861bc42f7SIan Lepore if (timeout <= 0) { 21961bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 22061bc42f7SIan Lepore mask); 221831f5dcfSAlexander Motin sdhci_dumpregs(slot); 222831f5dcfSAlexander Motin return; 223831f5dcfSAlexander Motin } 224831f5dcfSAlexander Motin timeout--; 22561bc42f7SIan Lepore DELAY(10); 226831f5dcfSAlexander Motin } 227831f5dcfSAlexander Motin } 228831f5dcfSAlexander Motin 229831f5dcfSAlexander Motin static void 230831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 231831f5dcfSAlexander Motin { 232831f5dcfSAlexander Motin 233831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 234831f5dcfSAlexander Motin 235831f5dcfSAlexander Motin /* Enable interrupts. */ 236831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 237831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 238831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 239831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 240831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 241831f5dcfSAlexander Motin SDHCI_INT_ACMD12ERR; 242639f59f0SIan Lepore 243639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 244639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 245639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 246639f59f0SIan Lepore } 247639f59f0SIan Lepore 248831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 249831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 250831f5dcfSAlexander Motin } 251831f5dcfSAlexander Motin 252831f5dcfSAlexander Motin static void 253831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 254831f5dcfSAlexander Motin { 25593efdc63SAdrian Chadd uint32_t clk_base; 25693efdc63SAdrian Chadd uint32_t clk_sel; 257831f5dcfSAlexander Motin uint32_t res; 258831f5dcfSAlexander Motin uint16_t clk; 2598f3b7d56SOleksandr Tymoshenko uint16_t div; 260831f5dcfSAlexander Motin int timeout; 261831f5dcfSAlexander Motin 262831f5dcfSAlexander Motin if (clock == slot->clock) 263831f5dcfSAlexander Motin return; 264831f5dcfSAlexander Motin slot->clock = clock; 265831f5dcfSAlexander Motin 266831f5dcfSAlexander Motin /* Turn off the clock. */ 2674ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 2684ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 269*b440e965SMarius Strobl /* If no clock requested - leave it so. */ 270831f5dcfSAlexander Motin if (clock == 0) 271831f5dcfSAlexander Motin return; 272ceb9e9f7SIan Lepore 27393efdc63SAdrian Chadd /* Determine the clock base frequency */ 27493efdc63SAdrian Chadd clk_base = slot->max_clk; 27593efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 2761bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 2771bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 27893efdc63SAdrian Chadd 2791bacf3beSMarius Strobl /* 2801bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 2811bacf3beSMarius Strobl */ 28293efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 28393efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 2841bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 2851bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 28693efdc63SAdrian Chadd } else { 2871bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 2881bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 28993efdc63SAdrian Chadd } 29093efdc63SAdrian Chadd 29193efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 29293efdc63SAdrian Chadd } 29393efdc63SAdrian Chadd 294ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 295ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 296ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 297ceb9e9f7SIan Lepore 2988f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 299831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 30093efdc63SAdrian Chadd res = clk_base; 30157677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 302831f5dcfSAlexander Motin if (res <= clock) 303831f5dcfSAlexander Motin break; 304831f5dcfSAlexander Motin res >>= 1; 305831f5dcfSAlexander Motin } 306831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 3078f3b7d56SOleksandr Tymoshenko div >>= 1; 3088f3b7d56SOleksandr Tymoshenko } 3098f3b7d56SOleksandr Tymoshenko else { 3108f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023*2 */ 31193efdc63SAdrian Chadd if (clock >= clk_base) 31257677a3aSOleksandr Tymoshenko div = 0; 3138f3b7d56SOleksandr Tymoshenko else { 31457677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 31593efdc63SAdrian Chadd if ((clk_base / div) <= clock) 3168f3b7d56SOleksandr Tymoshenko break; 3178f3b7d56SOleksandr Tymoshenko } 3188f3b7d56SOleksandr Tymoshenko } 3198f3b7d56SOleksandr Tymoshenko div >>= 1; 3208f3b7d56SOleksandr Tymoshenko } 3218f3b7d56SOleksandr Tymoshenko 3228f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 32393efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 32493efdc63SAdrian Chadd div, clock, clk_base); 3258f3b7d56SOleksandr Tymoshenko 326831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 3278f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 3288f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 3298f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 3308f3b7d56SOleksandr Tymoshenko 331831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 332831f5dcfSAlexander Motin /* Enable clock. */ 333831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 334831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 335831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 336831f5dcfSAlexander Motin timeout = 10; 337831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 338831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 339831f5dcfSAlexander Motin if (timeout == 0) { 340831f5dcfSAlexander Motin slot_printf(slot, 341831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 342831f5dcfSAlexander Motin sdhci_dumpregs(slot); 343831f5dcfSAlexander Motin return; 344831f5dcfSAlexander Motin } 345831f5dcfSAlexander Motin timeout--; 346831f5dcfSAlexander Motin DELAY(1000); 347831f5dcfSAlexander Motin } 348831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 349831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 350831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 351831f5dcfSAlexander Motin } 352831f5dcfSAlexander Motin 353831f5dcfSAlexander Motin static void 354831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 355831f5dcfSAlexander Motin { 356831f5dcfSAlexander Motin uint8_t pwr; 357831f5dcfSAlexander Motin 358831f5dcfSAlexander Motin if (slot->power == power) 359831f5dcfSAlexander Motin return; 360d6b3aaf8SOleksandr Tymoshenko 361831f5dcfSAlexander Motin slot->power = power; 362831f5dcfSAlexander Motin 363831f5dcfSAlexander Motin /* Turn off the power. */ 364831f5dcfSAlexander Motin pwr = 0; 365831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 366*b440e965SMarius Strobl /* If power down requested - leave it so. */ 367831f5dcfSAlexander Motin if (power == 0) 368831f5dcfSAlexander Motin return; 369831f5dcfSAlexander Motin /* Set voltage. */ 370831f5dcfSAlexander Motin switch (1 << power) { 371831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 372831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 373831f5dcfSAlexander Motin break; 374831f5dcfSAlexander Motin case MMC_OCR_290_300: 375831f5dcfSAlexander Motin case MMC_OCR_300_310: 376831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 377831f5dcfSAlexander Motin break; 378831f5dcfSAlexander Motin case MMC_OCR_320_330: 379831f5dcfSAlexander Motin case MMC_OCR_330_340: 380831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 381831f5dcfSAlexander Motin break; 382831f5dcfSAlexander Motin } 383831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 384831f5dcfSAlexander Motin /* Turn on the power. */ 385831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 386831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 387a2832f9fSMarius Strobl 388a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 389a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 390a2832f9fSMarius Strobl DELAY(10); 391a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 392a2832f9fSMarius Strobl DELAY(300); 393a2832f9fSMarius Strobl } 394831f5dcfSAlexander Motin } 395831f5dcfSAlexander Motin 396831f5dcfSAlexander Motin static void 397831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 398831f5dcfSAlexander Motin { 399831f5dcfSAlexander Motin uint32_t data; 400831f5dcfSAlexander Motin char *buffer; 401831f5dcfSAlexander Motin size_t left; 402831f5dcfSAlexander Motin 403831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 404831f5dcfSAlexander Motin buffer += slot->offset; 405831f5dcfSAlexander Motin /* Transfer one block at a time. */ 406831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 407831f5dcfSAlexander Motin slot->offset += left; 408831f5dcfSAlexander Motin 409831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 410d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 411831f5dcfSAlexander Motin DELAY(10); 412ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 413831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 414831f5dcfSAlexander Motin while (left > 3) { 415831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 416831f5dcfSAlexander Motin buffer[0] = data; 417831f5dcfSAlexander Motin buffer[1] = (data >> 8); 418831f5dcfSAlexander Motin buffer[2] = (data >> 16); 419831f5dcfSAlexander Motin buffer[3] = (data >> 24); 420831f5dcfSAlexander Motin buffer += 4; 421831f5dcfSAlexander Motin left -= 4; 422831f5dcfSAlexander Motin } 423831f5dcfSAlexander Motin } else { 424d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 425831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 426831f5dcfSAlexander Motin left &= 3; 427831f5dcfSAlexander Motin } 428831f5dcfSAlexander Motin /* Handle uneven size case. */ 429831f5dcfSAlexander Motin if (left > 0) { 430831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 431831f5dcfSAlexander Motin while (left > 0) { 432831f5dcfSAlexander Motin *(buffer++) = data; 433831f5dcfSAlexander Motin data >>= 8; 434831f5dcfSAlexander Motin left--; 435831f5dcfSAlexander Motin } 436831f5dcfSAlexander Motin } 437831f5dcfSAlexander Motin } 438831f5dcfSAlexander Motin 439831f5dcfSAlexander Motin static void 440831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 441831f5dcfSAlexander Motin { 442831f5dcfSAlexander Motin uint32_t data = 0; 443831f5dcfSAlexander Motin char *buffer; 444831f5dcfSAlexander Motin size_t left; 445831f5dcfSAlexander Motin 446831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 447831f5dcfSAlexander Motin buffer += slot->offset; 448831f5dcfSAlexander Motin /* Transfer one block at a time. */ 449831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 450831f5dcfSAlexander Motin slot->offset += left; 451831f5dcfSAlexander Motin 452ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 453831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 454831f5dcfSAlexander Motin while (left > 3) { 455831f5dcfSAlexander Motin data = buffer[0] + 456831f5dcfSAlexander Motin (buffer[1] << 8) + 457831f5dcfSAlexander Motin (buffer[2] << 16) + 458831f5dcfSAlexander Motin (buffer[3] << 24); 459831f5dcfSAlexander Motin left -= 4; 460831f5dcfSAlexander Motin buffer += 4; 461831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 462831f5dcfSAlexander Motin } 463831f5dcfSAlexander Motin } else { 464d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 465831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 466831f5dcfSAlexander Motin left &= 3; 467831f5dcfSAlexander Motin } 468831f5dcfSAlexander Motin /* Handle uneven size case. */ 469831f5dcfSAlexander Motin if (left > 0) { 470831f5dcfSAlexander Motin while (left > 0) { 471831f5dcfSAlexander Motin data <<= 8; 472831f5dcfSAlexander Motin data += *(buffer++); 473831f5dcfSAlexander Motin left--; 474831f5dcfSAlexander Motin } 475831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 476831f5dcfSAlexander Motin } 477831f5dcfSAlexander Motin } 478831f5dcfSAlexander Motin 479831f5dcfSAlexander Motin static void 480831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 481831f5dcfSAlexander Motin { 482831f5dcfSAlexander Motin 483831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 484831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 485831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 486831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 487831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 488831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 489831f5dcfSAlexander Motin break; 490831f5dcfSAlexander Motin } 491831f5dcfSAlexander Motin } else { 492831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 493831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 494831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 495831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 496831f5dcfSAlexander Motin break; 497831f5dcfSAlexander Motin } 498831f5dcfSAlexander Motin } 499831f5dcfSAlexander Motin } 500831f5dcfSAlexander Motin 501831f5dcfSAlexander Motin static void 5027e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 503831f5dcfSAlexander Motin { 504831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 5057e6ccea3SMarius Strobl device_t d; 506831f5dcfSAlexander Motin 507831f5dcfSAlexander Motin SDHCI_LOCK(slot); 5086e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 509831f5dcfSAlexander Motin if (slot->dev == NULL) { 510831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 511639f59f0SIan Lepore if (bootverbose || sdhci_debug) 512639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 513d6b3aaf8SOleksandr Tymoshenko slot->dev = device_add_child(slot->bus, "mmc", -1); 514831f5dcfSAlexander Motin device_set_ivars(slot->dev, slot); 515831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 516831f5dcfSAlexander Motin device_probe_and_attach(slot->dev); 517831f5dcfSAlexander Motin } else 518831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 519831f5dcfSAlexander Motin } else { 520831f5dcfSAlexander Motin if (slot->dev != NULL) { 521831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 522639f59f0SIan Lepore if (bootverbose || sdhci_debug) 523639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 5247e6ccea3SMarius Strobl d = slot->dev; 525831f5dcfSAlexander Motin slot->dev = NULL; 526831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 527d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 528831f5dcfSAlexander Motin } else 529831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 530831f5dcfSAlexander Motin } 531831f5dcfSAlexander Motin } 532831f5dcfSAlexander Motin 533b8bf08b1SIan Lepore static void 534b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 535639f59f0SIan Lepore { 536639f59f0SIan Lepore bool was_present; 537639f59f0SIan Lepore 538639f59f0SIan Lepore /* 539639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 540639f59f0SIan Lepore * create the child device after a short delay. The delay is to 541639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 542639f59f0SIan Lepore * before the other pins have made good contact). 543639f59f0SIan Lepore * 544639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 545639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 546639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 547639f59f0SIan Lepore * that happens by deleting and recreating the child device. 548639f59f0SIan Lepore */ 549639f59f0SIan Lepore was_present = slot->dev != NULL; 550639f59f0SIan Lepore if (!was_present && is_present) { 551639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 552639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 553639f59f0SIan Lepore } else if (was_present && !is_present) { 554639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 555639f59f0SIan Lepore } 556b8bf08b1SIan Lepore } 557b8bf08b1SIan Lepore 558b8bf08b1SIan Lepore void 559b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 560b8bf08b1SIan Lepore { 561b8bf08b1SIan Lepore 562b8bf08b1SIan Lepore SDHCI_LOCK(slot); 563b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 564639f59f0SIan Lepore SDHCI_UNLOCK(slot); 565639f59f0SIan Lepore } 566639f59f0SIan Lepore 567639f59f0SIan Lepore static void 568639f59f0SIan Lepore sdhci_card_poll(void *arg) 569639f59f0SIan Lepore { 570639f59f0SIan Lepore struct sdhci_slot *slot = arg; 571639f59f0SIan Lepore 572639f59f0SIan Lepore sdhci_handle_card_present(slot, 573639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 574639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 575639f59f0SIan Lepore sdhci_card_poll, slot); 576639f59f0SIan Lepore } 577639f59f0SIan Lepore 578d6b3aaf8SOleksandr Tymoshenko int 579d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 580831f5dcfSAlexander Motin { 58187a6a871SIan Lepore uint32_t caps, freq; 582d6b3aaf8SOleksandr Tymoshenko int err; 583831f5dcfSAlexander Motin 584831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 585d6b3aaf8SOleksandr Tymoshenko slot->num = num; 586d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 587d6b3aaf8SOleksandr Tymoshenko 588831f5dcfSAlexander Motin /* Allocate DMA tag. */ 589831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 590831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 591831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 592831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 593831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 594831f5dcfSAlexander Motin &slot->dmatag); 595831f5dcfSAlexander Motin if (err != 0) { 596831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 597831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 598d6b3aaf8SOleksandr Tymoshenko return (err); 599831f5dcfSAlexander Motin } 600831f5dcfSAlexander Motin /* Allocate DMA memory. */ 601831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 602831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 603831f5dcfSAlexander Motin if (err != 0) { 604831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 605831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 606d6b3aaf8SOleksandr Tymoshenko return (err); 607831f5dcfSAlexander Motin } 608831f5dcfSAlexander Motin /* Map the memory. */ 609831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 610831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 611831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 612831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 613831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 614831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 615d6b3aaf8SOleksandr Tymoshenko if (err) 616d6b3aaf8SOleksandr Tymoshenko return (err); 617d6b3aaf8SOleksandr Tymoshenko else 618d6b3aaf8SOleksandr Tymoshenko return (EFAULT); 619831f5dcfSAlexander Motin } 620d6b3aaf8SOleksandr Tymoshenko 621831f5dcfSAlexander Motin /* Initialize slot. */ 622831f5dcfSAlexander Motin sdhci_init(slot); 623d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 624d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 6258f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) 6268f3b7d56SOleksandr Tymoshenko caps = slot->caps; 6278f3b7d56SOleksandr Tymoshenko else 628831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 629831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 63033aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 63187a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 63287a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 63333aad34dSOleksandr Tymoshenko else 63487a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 63587a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 63687a6a871SIan Lepore if (freq != 0) 63787a6a871SIan Lepore slot->max_clk = freq * 1000000; 63887a6a871SIan Lepore /* 63987a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 64087a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 64187a6a871SIan Lepore * with an assumption, so complain about it. 64287a6a871SIan Lepore */ 643831f5dcfSAlexander Motin if (slot->max_clk == 0) { 64487a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 645831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 6461bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 6471bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 648831f5dcfSAlexander Motin } 649a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 6508f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 6518f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 652a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 653a2832f9fSMarius Strobl slot->timeout_clk = 1000; 6548f3b7d56SOleksandr Tymoshenko } else { 6551bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 6561bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 6578f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 6588f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 6598f3b7d56SOleksandr Tymoshenko } 66087a6a871SIan Lepore /* 66187a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 66287a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 66387a6a871SIan Lepore * max timeout, but still mention it. 66487a6a871SIan Lepore */ 665831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 666831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 667ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 668ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 669831f5dcfSAlexander Motin } 670831f5dcfSAlexander Motin 67157677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 672831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 673831f5dcfSAlexander Motin slot->host.host_ocr = 0; 674831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 675831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 676831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 677831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 678831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_180) 679831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 680831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 681831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 682831f5dcfSAlexander Motin "support voltages.\n"); 683831f5dcfSAlexander Motin } 684831f5dcfSAlexander Motin slot->host.caps = MMC_CAP_4_BIT_DATA; 6852d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 6862d1731b8SIan Lepore slot->host.caps |= MMC_CAP_8_BIT_DATA; 687831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 688831f5dcfSAlexander Motin slot->host.caps |= MMC_CAP_HSPEED; 689831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 690831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 691831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 692d6b3aaf8SOleksandr Tymoshenko 693d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 694831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 695d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 696831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 697a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 698a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 699831f5dcfSAlexander Motin 700c3a0f75aSOleksandr Tymoshenko /* 701c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 702c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 703c3a0f75aSOleksandr Tymoshenko */ 704c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 705c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 706c3a0f75aSOleksandr Tymoshenko 7075b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 7082d1731b8SIan Lepore slot_printf(slot, "%uMHz%s %s%s%s%s %s\n", 709831f5dcfSAlexander Motin slot->max_clk / 1000000, 710831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 711a2832f9fSMarius Strobl (slot->host.caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 712a2832f9fSMarius Strobl ((slot->host.caps & MMC_CAP_4_BIT_DATA) ? "4bits" : 713a2832f9fSMarius Strobl "1bit"), 714831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 715831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 716831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 717831f5dcfSAlexander Motin (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 718831f5dcfSAlexander Motin sdhci_dumpregs(slot); 719831f5dcfSAlexander Motin } 720831f5dcfSAlexander Motin 721ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 722ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 723ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 724ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 725ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 726831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 727639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 728639f59f0SIan Lepore sdhci_card_task, slot); 729639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 730e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 731ba6fc1c7SLuiz Otavio O Souza 732639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 733639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 734639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 735639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 736639f59f0SIan Lepore } 737639f59f0SIan Lepore 738831f5dcfSAlexander Motin return (0); 739831f5dcfSAlexander Motin } 740831f5dcfSAlexander Motin 741d6b3aaf8SOleksandr Tymoshenko void 742d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 743831f5dcfSAlexander Motin { 7447e6ccea3SMarius Strobl 745d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 746d6b3aaf8SOleksandr Tymoshenko } 747831f5dcfSAlexander Motin 748d6b3aaf8SOleksandr Tymoshenko int 749d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 750d6b3aaf8SOleksandr Tymoshenko { 751831f5dcfSAlexander Motin device_t d; 752831f5dcfSAlexander Motin 753e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 754639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 755831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 756639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 757831f5dcfSAlexander Motin 758831f5dcfSAlexander Motin SDHCI_LOCK(slot); 759831f5dcfSAlexander Motin d = slot->dev; 760831f5dcfSAlexander Motin slot->dev = NULL; 761831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 762831f5dcfSAlexander Motin if (d != NULL) 763d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 764831f5dcfSAlexander Motin 765831f5dcfSAlexander Motin SDHCI_LOCK(slot); 766831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 767831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 768831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 769831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 770831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 771d6b3aaf8SOleksandr Tymoshenko 772831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 773d6b3aaf8SOleksandr Tymoshenko 774831f5dcfSAlexander Motin return (0); 775831f5dcfSAlexander Motin } 776831f5dcfSAlexander Motin 777d6b3aaf8SOleksandr Tymoshenko int 778d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 77992bf0e27SAlexander Motin { 7807e6ccea3SMarius Strobl 781d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 78292bf0e27SAlexander Motin 78392bf0e27SAlexander Motin return (0); 78492bf0e27SAlexander Motin } 78592bf0e27SAlexander Motin 786d6b3aaf8SOleksandr Tymoshenko int 787d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 78892bf0e27SAlexander Motin { 7897e6ccea3SMarius Strobl 790d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 79192bf0e27SAlexander Motin 792d6b3aaf8SOleksandr Tymoshenko return (0); 79392bf0e27SAlexander Motin } 79492bf0e27SAlexander Motin 79557677a3aSOleksandr Tymoshenko uint32_t 796*b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 79757677a3aSOleksandr Tymoshenko { 7987e6ccea3SMarius Strobl 79957677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 80057677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 80157677a3aSOleksandr Tymoshenko else 80257677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 80357677a3aSOleksandr Tymoshenko } 80457677a3aSOleksandr Tymoshenko 8056e37fb2bSIan Lepore bool 806*b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 8076e37fb2bSIan Lepore { 8086e37fb2bSIan Lepore 809639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 810639f59f0SIan Lepore return true; 811639f59f0SIan Lepore 8126e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 8136e37fb2bSIan Lepore } 8146e37fb2bSIan Lepore 815d6b3aaf8SOleksandr Tymoshenko int 816d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 817831f5dcfSAlexander Motin { 818831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 819831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 820831f5dcfSAlexander Motin 821831f5dcfSAlexander Motin SDHCI_LOCK(slot); 822831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 823831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 824831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 825831f5dcfSAlexander Motin sdhci_init(slot); 826831f5dcfSAlexander Motin } 827831f5dcfSAlexander Motin /* Configure the bus. */ 828831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 829831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 8302d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 8312d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 832831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 8332d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 8342d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 8352d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 8362d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 8372d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 8382d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 8392d1731b8SIan Lepore } else { 8402d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 8412d1731b8SIan Lepore } 842bba987dcSIan Lepore if (ios->timing == bus_timing_hs && 843bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 844831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 845831f5dcfSAlexander Motin else 846831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 847831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 848831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 849d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 850831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 851831f5dcfSAlexander Motin 852831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 853831f5dcfSAlexander Motin return (0); 854831f5dcfSAlexander Motin } 855831f5dcfSAlexander Motin 856831f5dcfSAlexander Motin static void 857e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 858e64f01a9SIan Lepore { 859e64f01a9SIan Lepore struct mmc_request *req; 860e64f01a9SIan Lepore 861e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 862e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 863e64f01a9SIan Lepore req = slot->req; 864e64f01a9SIan Lepore slot->req = NULL; 865e64f01a9SIan Lepore slot->curcmd = NULL; 866e64f01a9SIan Lepore req->done(req); 867e64f01a9SIan Lepore } 868e64f01a9SIan Lepore } 869e64f01a9SIan Lepore 870e64f01a9SIan Lepore static void 871e64f01a9SIan Lepore sdhci_timeout(void *arg) 872e64f01a9SIan Lepore { 873e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 874e64f01a9SIan Lepore 875e64f01a9SIan Lepore if (slot->curcmd != NULL) { 8767e586643SIan Lepore slot_printf(slot, " Controller timeout\n"); 8777e586643SIan Lepore sdhci_dumpregs(slot); 878a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 879e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 880e64f01a9SIan Lepore sdhci_req_done(slot); 8817e586643SIan Lepore } else { 8827e586643SIan Lepore slot_printf(slot, " Spurious timeout - no active command\n"); 883e64f01a9SIan Lepore } 884e64f01a9SIan Lepore } 885e64f01a9SIan Lepore 886e64f01a9SIan Lepore static void 887*b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data) 888831f5dcfSAlexander Motin { 889831f5dcfSAlexander Motin uint16_t mode; 890831f5dcfSAlexander Motin 891831f5dcfSAlexander Motin if (data == NULL) 892831f5dcfSAlexander Motin return; 893831f5dcfSAlexander Motin 894831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 895831f5dcfSAlexander Motin if (data->len > 512) 896831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 897831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 898831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 899831f5dcfSAlexander Motin if (slot->req->stop) 900831f5dcfSAlexander Motin mode |= SDHCI_TRNS_ACMD12; 901831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 902831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 903831f5dcfSAlexander Motin 904831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 905831f5dcfSAlexander Motin } 906831f5dcfSAlexander Motin 907831f5dcfSAlexander Motin static void 908831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 909831f5dcfSAlexander Motin { 910831f5dcfSAlexander Motin int flags, timeout; 91190993663SIan Lepore uint32_t mask; 912831f5dcfSAlexander Motin 913831f5dcfSAlexander Motin slot->curcmd = cmd; 914831f5dcfSAlexander Motin slot->cmd_done = 0; 915831f5dcfSAlexander Motin 916831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 917831f5dcfSAlexander Motin 918831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 919831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 920831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 921831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 922e64f01a9SIan Lepore sdhci_req_done(slot); 923831f5dcfSAlexander Motin return; 924831f5dcfSAlexander Motin } 925831f5dcfSAlexander Motin 926*b440e965SMarius Strobl /* 927*b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 928*b440e965SMarius Strobl * Controller will not detect timeout without clock active. 929*b440e965SMarius Strobl */ 9306e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 931d8208d9eSAlexander Motin slot->power == 0 || 932d8208d9eSAlexander Motin slot->clock == 0) { 933831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 934e64f01a9SIan Lepore sdhci_req_done(slot); 935831f5dcfSAlexander Motin return; 936831f5dcfSAlexander Motin } 937831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 938831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 939831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 940831f5dcfSAlexander Motin if (cmd->data || (cmd->flags & MMC_RSP_BUSY)) 941831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 942831f5dcfSAlexander Motin /* We shouldn't wait for DAT for stop commands. */ 943831f5dcfSAlexander Motin if (cmd == slot->req->stop) 944831f5dcfSAlexander Motin mask &= ~SDHCI_DAT_INHIBIT; 9458775ab45SIan Lepore /* 9468775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 9478775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 9488775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 9498775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 9508775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 9518775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 9528775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 9538775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 9548775ab45SIan Lepore */ 9558775ab45SIan Lepore timeout = 250; 95690993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 957831f5dcfSAlexander Motin if (timeout == 0) { 958831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 959831f5dcfSAlexander Motin "inhibit bit(s).\n"); 960831f5dcfSAlexander Motin sdhci_dumpregs(slot); 961831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 962e64f01a9SIan Lepore sdhci_req_done(slot); 963831f5dcfSAlexander Motin return; 964831f5dcfSAlexander Motin } 965831f5dcfSAlexander Motin timeout--; 966831f5dcfSAlexander Motin DELAY(1000); 967831f5dcfSAlexander Motin } 968831f5dcfSAlexander Motin 969831f5dcfSAlexander Motin /* Prepare command flags. */ 970831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 971831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 972831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 973831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 974831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 975831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 976831f5dcfSAlexander Motin else 977831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 978831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 979831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 980831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 981831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 982831f5dcfSAlexander Motin if (cmd->data) 983831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 984831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 985831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 986831f5dcfSAlexander Motin /* Prepare data. */ 987831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 988831f5dcfSAlexander Motin /* 989831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 990831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 991831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 992831f5dcfSAlexander Motin */ 993831f5dcfSAlexander Motin if (slot->data_done == 0) { 994831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 995831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 996831f5dcfSAlexander Motin } 997831f5dcfSAlexander Motin /* Set command argument. */ 998831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 999831f5dcfSAlexander Motin /* Set data transfer mode. */ 1000831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1001831f5dcfSAlexander Motin /* Start command. */ 1002d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1003a6873fd1SIan Lepore /* Start timeout callout. */ 1004ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1005ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1006831f5dcfSAlexander Motin } 1007831f5dcfSAlexander Motin 1008831f5dcfSAlexander Motin static void 1009831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1010831f5dcfSAlexander Motin { 1011831f5dcfSAlexander Motin int i; 10121bacf3beSMarius Strobl uint32_t val; 10131bacf3beSMarius Strobl uint8_t extra; 1014831f5dcfSAlexander Motin 1015831f5dcfSAlexander Motin slot->cmd_done = 1; 1016831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1017831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 1018831f5dcfSAlexander Motin * happened first. */ 1019831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 1020831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1021831f5dcfSAlexander Motin if (slot->curcmd->error) { 1022831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1023831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1024831f5dcfSAlexander Motin sdhci_start(slot); 1025831f5dcfSAlexander Motin return; 1026831f5dcfSAlexander Motin } 1027831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1028831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1029831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1030831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 10311bacf3beSMarius Strobl extra = 0; 1032831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 10331bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 10341bacf3beSMarius Strobl if (slot->quirks & 10351bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1036677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1037677ee494SIan Lepore else { 1038677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1039677ee494SIan Lepore (val << 8) | extra; 1040831f5dcfSAlexander Motin extra = val >> 24; 1041831f5dcfSAlexander Motin } 1042677ee494SIan Lepore } 1043831f5dcfSAlexander Motin } else 1044831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1045831f5dcfSAlexander Motin } 1046831f5dcfSAlexander Motin /* If data ready - finish. */ 1047831f5dcfSAlexander Motin if (slot->data_done) 1048831f5dcfSAlexander Motin sdhci_start(slot); 1049831f5dcfSAlexander Motin } 1050831f5dcfSAlexander Motin 1051831f5dcfSAlexander Motin static void 1052831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 1053831f5dcfSAlexander Motin { 1054831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 1055831f5dcfSAlexander Motin uint8_t div; 1056831f5dcfSAlexander Motin 1057831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1058831f5dcfSAlexander Motin slot->data_done = 1; 1059831f5dcfSAlexander Motin return; 1060831f5dcfSAlexander Motin } 1061831f5dcfSAlexander Motin 1062831f5dcfSAlexander Motin slot->data_done = 0; 1063831f5dcfSAlexander Motin 1064831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1065831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1066ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1067ceb9e9f7SIan Lepore div = 0xE; 1068ceb9e9f7SIan Lepore } else { 1069831f5dcfSAlexander Motin target_timeout = 1000000; 1070831f5dcfSAlexander Motin div = 0; 1071831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1072ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1073ceb9e9f7SIan Lepore ++div; 1074831f5dcfSAlexander Motin current_timeout <<= 1; 1075831f5dcfSAlexander Motin } 1076831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1077ceb9e9f7SIan Lepore if (div < 0xE && 1078ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1079ceb9e9f7SIan Lepore ++div; 1080831f5dcfSAlexander Motin } 1081ceb9e9f7SIan Lepore } 1082831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1083831f5dcfSAlexander Motin 1084831f5dcfSAlexander Motin if (data == NULL) 1085831f5dcfSAlexander Motin return; 1086831f5dcfSAlexander Motin 1087831f5dcfSAlexander Motin /* Use DMA if possible. */ 1088831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1089831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1090831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 1091d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1092831f5dcfSAlexander Motin (data->len <= 512)) 1093831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1094831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1095d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1096831f5dcfSAlexander Motin ((data->len) & 0x3)) 1097831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1098831f5dcfSAlexander Motin /* Load DMA buffer. */ 1099831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1100831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1101ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1102ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1103831f5dcfSAlexander Motin else { 1104831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 1105ecc2d997SRui Paulo (data->len < DMA_BLOCK_SIZE) ? 1106ecc2d997SRui Paulo data->len : DMA_BLOCK_SIZE); 1107ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1108ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1109831f5dcfSAlexander Motin } 1110831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1111831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1112831f5dcfSAlexander Motin * for the last page and unmask else. */ 1113831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 1114831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1115831f5dcfSAlexander Motin else 1116831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1117831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1118831f5dcfSAlexander Motin } 1119831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1120831f5dcfSAlexander Motin slot->offset = 0; 1121831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 11221bacf3beSMarius Strobl WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, 11231bacf3beSMarius Strobl (data->len < 512) ? data->len : 512)); 1124831f5dcfSAlexander Motin /* Set block count. */ 1125831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1126831f5dcfSAlexander Motin } 1127831f5dcfSAlexander Motin 1128c3a0f75aSOleksandr Tymoshenko void 1129831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1130831f5dcfSAlexander Motin { 1131831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 11327e6ccea3SMarius Strobl size_t left; 1133831f5dcfSAlexander Motin 1134831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1135ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1136831f5dcfSAlexander Motin * happened first. */ 1137831f5dcfSAlexander Motin if (!slot->cmd_done) { 1138831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1139831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1140831f5dcfSAlexander Motin } 1141831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1142a98788edSIan Lepore if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) { 1143831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 11447e6ccea3SMarius Strobl left = data->len - slot->offset; 1145ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1146ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1147831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1148831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1149831f5dcfSAlexander Motin } else 1150ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1151ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1152831f5dcfSAlexander Motin } 1153a98788edSIan Lepore slot->data_done = 1; 1154831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1155831f5dcfSAlexander Motin if (slot->curcmd->error) { 1156831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1157831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1158831f5dcfSAlexander Motin sdhci_start(slot); 1159831f5dcfSAlexander Motin return; 1160831f5dcfSAlexander Motin } 1161831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1162831f5dcfSAlexander Motin if (slot->cmd_done) 1163831f5dcfSAlexander Motin sdhci_start(slot); 1164831f5dcfSAlexander Motin } 1165831f5dcfSAlexander Motin 1166831f5dcfSAlexander Motin static void 1167831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1168831f5dcfSAlexander Motin { 1169831f5dcfSAlexander Motin struct mmc_request *req; 1170831f5dcfSAlexander Motin 1171831f5dcfSAlexander Motin req = slot->req; 1172831f5dcfSAlexander Motin if (req == NULL) 1173831f5dcfSAlexander Motin return; 1174831f5dcfSAlexander Motin 1175831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1176831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1177831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1178831f5dcfSAlexander Motin return; 1179831f5dcfSAlexander Motin } 1180831f5dcfSAlexander Motin /* We don't need this until using Auto-CMD12 feature 1181831f5dcfSAlexander Motin if (!(slot->flags & STOP_STARTED) && req->stop) { 1182831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1183831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1184831f5dcfSAlexander Motin return; 1185831f5dcfSAlexander Motin } 1186831f5dcfSAlexander Motin */ 11875b69a497SAlexander Motin if (sdhci_debug > 1) 11885b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 11895b69a497SAlexander Motin if (!req->cmd->error && 1190d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1191831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1192831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1193831f5dcfSAlexander Motin } 1194831f5dcfSAlexander Motin 1195e64f01a9SIan Lepore sdhci_req_done(slot); 1196831f5dcfSAlexander Motin } 1197831f5dcfSAlexander Motin 1198d6b3aaf8SOleksandr Tymoshenko int 1199*b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 1200*b440e965SMarius Strobl struct mmc_request *req) 1201831f5dcfSAlexander Motin { 1202831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1203831f5dcfSAlexander Motin 1204831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1205831f5dcfSAlexander Motin if (slot->req != NULL) { 1206831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1207831f5dcfSAlexander Motin return (EBUSY); 1208831f5dcfSAlexander Motin } 12095b69a497SAlexander Motin if (sdhci_debug > 1) { 12101bacf3beSMarius Strobl slot_printf(slot, 12111bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1212831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 12135b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 12145b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 12155b69a497SAlexander Motin } 1216831f5dcfSAlexander Motin slot->req = req; 1217831f5dcfSAlexander Motin slot->flags = 0; 1218831f5dcfSAlexander Motin sdhci_start(slot); 1219831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1220bea2dca2SAlexander Motin if (dumping) { 1221bea2dca2SAlexander Motin while (slot->req != NULL) { 1222d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 1223bea2dca2SAlexander Motin DELAY(10); 1224bea2dca2SAlexander Motin } 1225bea2dca2SAlexander Motin } 1226831f5dcfSAlexander Motin return (0); 1227831f5dcfSAlexander Motin } 1228831f5dcfSAlexander Motin 1229d6b3aaf8SOleksandr Tymoshenko int 1230*b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 1231831f5dcfSAlexander Motin { 1232831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1233831f5dcfSAlexander Motin uint32_t val; 1234831f5dcfSAlexander Motin 1235831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1236831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 1237831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1238831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 1239831f5dcfSAlexander Motin } 1240831f5dcfSAlexander Motin 1241d6b3aaf8SOleksandr Tymoshenko int 1242*b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 1243831f5dcfSAlexander Motin { 1244831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1245831f5dcfSAlexander Motin int err = 0; 1246831f5dcfSAlexander Motin 1247831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1248831f5dcfSAlexander Motin while (slot->bus_busy) 1249d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1250831f5dcfSAlexander Motin slot->bus_busy++; 1251831f5dcfSAlexander Motin /* Activate led. */ 1252831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1253831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1254831f5dcfSAlexander Motin return (err); 1255831f5dcfSAlexander Motin } 1256831f5dcfSAlexander Motin 1257d6b3aaf8SOleksandr Tymoshenko int 1258*b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 1259831f5dcfSAlexander Motin { 1260831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1261831f5dcfSAlexander Motin 1262831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1263831f5dcfSAlexander Motin /* Deactivate led. */ 1264831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1265831f5dcfSAlexander Motin slot->bus_busy--; 1266831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1267d493985aSAlexander Motin wakeup(slot); 1268831f5dcfSAlexander Motin return (0); 1269831f5dcfSAlexander Motin } 1270831f5dcfSAlexander Motin 1271831f5dcfSAlexander Motin static void 1272831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1273831f5dcfSAlexander Motin { 1274831f5dcfSAlexander Motin 1275831f5dcfSAlexander Motin if (!slot->curcmd) { 1276831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 1277831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1278831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1279831f5dcfSAlexander Motin return; 1280831f5dcfSAlexander Motin } 1281831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 1282831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1283831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 1284831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1285831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1286831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 1287831f5dcfSAlexander Motin 1288831f5dcfSAlexander Motin sdhci_finish_command(slot); 1289831f5dcfSAlexander Motin } 1290831f5dcfSAlexander Motin 1291831f5dcfSAlexander Motin static void 1292831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1293831f5dcfSAlexander Motin { 12941bacf3beSMarius Strobl struct mmc_data *data; 12951bacf3beSMarius Strobl size_t left; 1296831f5dcfSAlexander Motin 1297831f5dcfSAlexander Motin if (!slot->curcmd) { 1298831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1299831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1300831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1301831f5dcfSAlexander Motin return; 1302831f5dcfSAlexander Motin } 1303831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1304831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1305831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1306831f5dcfSAlexander Motin "there is no active data operation.\n", 1307831f5dcfSAlexander Motin intmask); 1308831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1309831f5dcfSAlexander Motin return; 1310831f5dcfSAlexander Motin } 1311831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 1312831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1313acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1314831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1315831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1316831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1317831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 1318831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1319831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 1320831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1321831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 1322831f5dcfSAlexander Motin } 1323831f5dcfSAlexander Motin if (slot->curcmd->error) { 1324831f5dcfSAlexander Motin /* No need to continue after any error. */ 1325a98788edSIan Lepore goto done; 1326831f5dcfSAlexander Motin } 1327831f5dcfSAlexander Motin 1328831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 1329c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 1330c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 1331c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 13321bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 13331bacf3beSMarius Strobl &intmask); 1334c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 1335c3a0f75aSOleksandr Tymoshenko } else 1336831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 1337c3a0f75aSOleksandr Tymoshenko } 1338831f5dcfSAlexander Motin /* Handle DMA border. */ 1339831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 13401bacf3beSMarius Strobl data = slot->curcmd->data; 1341831f5dcfSAlexander Motin 1342831f5dcfSAlexander Motin /* Unload DMA buffer... */ 1343831f5dcfSAlexander Motin left = data->len - slot->offset; 1344831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1345831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1346831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 1347831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1348831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1349831f5dcfSAlexander Motin } else { 1350831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1351831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 1352831f5dcfSAlexander Motin } 1353831f5dcfSAlexander Motin /* ... and reload it again. */ 1354831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 1355831f5dcfSAlexander Motin left = data->len - slot->offset; 1356831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1357831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1358831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 1359831f5dcfSAlexander Motin } else { 1360831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1361831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE); 1362831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1363831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 1364831f5dcfSAlexander Motin } 1365831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1366831f5dcfSAlexander Motin * for the last page. */ 1367831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 1368831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1369831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1370831f5dcfSAlexander Motin } 1371831f5dcfSAlexander Motin /* Restart DMA. */ 1372831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1373831f5dcfSAlexander Motin } 1374831f5dcfSAlexander Motin /* We have got all data. */ 1375c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 1376c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 1377c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 1378c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1379c3a0f75aSOleksandr Tymoshenko } else 1380831f5dcfSAlexander Motin sdhci_finish_data(slot); 1381831f5dcfSAlexander Motin } 1382a98788edSIan Lepore done: 1383a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 1384a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 1385a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 1386a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1387a98788edSIan Lepore } else 1388a98788edSIan Lepore sdhci_finish_data(slot); 1389a98788edSIan Lepore } 1390c3a0f75aSOleksandr Tymoshenko } 1391831f5dcfSAlexander Motin 1392831f5dcfSAlexander Motin static void 1393831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot) 1394831f5dcfSAlexander Motin { 1395831f5dcfSAlexander Motin uint16_t err; 1396831f5dcfSAlexander Motin 1397831f5dcfSAlexander Motin err = RD4(slot, SDHCI_ACMD12_ERR); 1398831f5dcfSAlexander Motin if (!slot->curcmd) { 1399831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1400831f5dcfSAlexander Motin "there is no active command.\n", err); 1401831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1402831f5dcfSAlexander Motin return; 1403831f5dcfSAlexander Motin } 1404831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1405831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1406831f5dcfSAlexander Motin } 1407831f5dcfSAlexander Motin 1408d6b3aaf8SOleksandr Tymoshenko void 1409d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 1410831f5dcfSAlexander Motin { 14112b96b955SJustin Hibbits uint32_t intmask, present; 1412831f5dcfSAlexander Motin 1413831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1414831f5dcfSAlexander Motin /* Read slot interrupt status. */ 1415831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 1416831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 1417831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1418d6b3aaf8SOleksandr Tymoshenko return; 1419831f5dcfSAlexander Motin } 14205b69a497SAlexander Motin if (sdhci_debug > 2) 14215b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 14225b69a497SAlexander Motin 1423831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 1424831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 1425639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 14262b96b955SJustin Hibbits slot->intmask &= 14272b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 14282b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 14292b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 14302b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 14312b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1432831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 1433831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1434b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 1435831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1436831f5dcfSAlexander Motin } 1437831f5dcfSAlexander Motin /* Handle command interrupts. */ 1438831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 1439831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1440831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1441831f5dcfSAlexander Motin } 1442831f5dcfSAlexander Motin /* Handle data interrupts. */ 1443831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 1444831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 14457e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 14467e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 1447831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1448831f5dcfSAlexander Motin } 1449831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 1450831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 1451831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1452831f5dcfSAlexander Motin sdhci_acmd_irq(slot); 1453831f5dcfSAlexander Motin } 1454831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1455831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ACMD12ERR; 1456831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ERROR; 1457831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 1458831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 1459831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1460831f5dcfSAlexander Motin slot_printf(slot, 1461831f5dcfSAlexander Motin "Card is consuming too much power!\n"); 1462831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_BUS_POWER; 1463831f5dcfSAlexander Motin } 1464831f5dcfSAlexander Motin /* The rest is unknown. */ 1465831f5dcfSAlexander Motin if (intmask) { 1466831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 1467831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1468831f5dcfSAlexander Motin intmask); 1469831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1470831f5dcfSAlexander Motin } 1471831f5dcfSAlexander Motin 1472831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1473831f5dcfSAlexander Motin } 1474831f5dcfSAlexander Motin 1475d6b3aaf8SOleksandr Tymoshenko int 14761bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 14771bacf3beSMarius Strobl uintptr_t *result) 1478831f5dcfSAlexander Motin { 1479831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1480831f5dcfSAlexander Motin 1481831f5dcfSAlexander Motin switch (which) { 1482831f5dcfSAlexander Motin default: 1483831f5dcfSAlexander Motin return (EINVAL); 1484831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1485bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 1486831f5dcfSAlexander Motin break; 1487831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1488bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 1489831f5dcfSAlexander Motin break; 1490831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1491bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 1492831f5dcfSAlexander Motin break; 1493831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1494bcd91d25SJayachandran C. *result = slot->host.ios.clock; 1495831f5dcfSAlexander Motin break; 1496831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1497bcd91d25SJayachandran C. *result = slot->host.f_min; 1498831f5dcfSAlexander Motin break; 1499831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 1500bcd91d25SJayachandran C. *result = slot->host.f_max; 1501831f5dcfSAlexander Motin break; 1502831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1503bcd91d25SJayachandran C. *result = slot->host.host_ocr; 1504831f5dcfSAlexander Motin break; 1505831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1506bcd91d25SJayachandran C. *result = slot->host.mode; 1507831f5dcfSAlexander Motin break; 1508831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1509bcd91d25SJayachandran C. *result = slot->host.ocr; 1510831f5dcfSAlexander Motin break; 1511831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1512bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 1513831f5dcfSAlexander Motin break; 1514831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1515bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 1516831f5dcfSAlexander Motin break; 1517831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1518bcd91d25SJayachandran C. *result = slot->host.caps; 1519831f5dcfSAlexander Motin break; 1520831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1521bcd91d25SJayachandran C. *result = slot->host.ios.timing; 1522831f5dcfSAlexander Motin break; 15233a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1524bcd91d25SJayachandran C. *result = 65535; 15253a4a2557SAlexander Motin break; 1526831f5dcfSAlexander Motin } 1527831f5dcfSAlexander Motin return (0); 1528831f5dcfSAlexander Motin } 1529831f5dcfSAlexander Motin 1530d6b3aaf8SOleksandr Tymoshenko int 15311bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 15321bacf3beSMarius Strobl uintptr_t value) 1533831f5dcfSAlexander Motin { 1534831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1535*b440e965SMarius Strobl uint32_t clock, max_clock; 1536*b440e965SMarius Strobl int i; 1537831f5dcfSAlexander Motin 1538831f5dcfSAlexander Motin switch (which) { 1539831f5dcfSAlexander Motin default: 1540831f5dcfSAlexander Motin return (EINVAL); 1541831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1542831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 1543831f5dcfSAlexander Motin break; 1544831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1545831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 1546831f5dcfSAlexander Motin break; 1547831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1548831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 1549831f5dcfSAlexander Motin break; 1550831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1551831f5dcfSAlexander Motin if (value > 0) { 155257677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 155357677a3aSOleksandr Tymoshenko clock = max_clock; 155457677a3aSOleksandr Tymoshenko 155557677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 155657677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 155757677a3aSOleksandr Tymoshenko i <<= 1) { 1558831f5dcfSAlexander Motin if (clock <= value) 1559831f5dcfSAlexander Motin break; 1560831f5dcfSAlexander Motin clock >>= 1; 1561831f5dcfSAlexander Motin } 1562*b440e965SMarius Strobl } else { 156357677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 156457677a3aSOleksandr Tymoshenko i += 2) { 156557677a3aSOleksandr Tymoshenko if (clock <= value) 156657677a3aSOleksandr Tymoshenko break; 156757677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 156857677a3aSOleksandr Tymoshenko } 156957677a3aSOleksandr Tymoshenko } 157057677a3aSOleksandr Tymoshenko 1571831f5dcfSAlexander Motin slot->host.ios.clock = clock; 1572831f5dcfSAlexander Motin } else 1573831f5dcfSAlexander Motin slot->host.ios.clock = 0; 1574831f5dcfSAlexander Motin break; 1575831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1576831f5dcfSAlexander Motin slot->host.mode = value; 1577831f5dcfSAlexander Motin break; 1578831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1579831f5dcfSAlexander Motin slot->host.ocr = value; 1580831f5dcfSAlexander Motin break; 1581831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1582831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 1583831f5dcfSAlexander Motin break; 1584831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1585831f5dcfSAlexander Motin slot->host.ios.vdd = value; 1586831f5dcfSAlexander Motin break; 1587831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1588831f5dcfSAlexander Motin slot->host.ios.timing = value; 1589831f5dcfSAlexander Motin break; 1590831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1591831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1592831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1593831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 15943a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1595831f5dcfSAlexander Motin return (EINVAL); 1596831f5dcfSAlexander Motin } 1597831f5dcfSAlexander Motin return (0); 1598831f5dcfSAlexander Motin } 1599831f5dcfSAlexander Motin 1600d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1); 1601