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; 58af3b2549SHans Petter Selasky SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, "Debug level"); 595b69a497SAlexander Motin 60d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 61d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 62d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 63d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 64d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 65831f5dcfSAlexander Motin 66d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 67d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 68d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 69d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 70d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 71831f5dcfSAlexander Motin 72831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 73831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 74831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 75831f5dcfSAlexander Motin 76831f5dcfSAlexander Motin static void sdhci_card_task(void *, int); 77831f5dcfSAlexander Motin 78831f5dcfSAlexander Motin /* helper routines */ 79831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 80831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 81831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 82831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 83831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 84831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 85831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 86831f5dcfSAlexander Motin 8733aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 8833aad34dSOleksandr Tymoshenko 8957677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 9057677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 9157677a3aSOleksandr Tymoshenko 9293efdc63SAdrian Chadd /* 9393efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 9493efdc63SAdrian Chadd */ 9593efdc63SAdrian Chadd #define BCM577XX_DEFAULT_MAX_DIVIDER 256 /* Maximum divider supported by the default clock source. */ 9693efdc63SAdrian Chadd #define BCM577XX_ALT_CLOCK_BASE 63000000 /* Alternative clock's base frequency. */ 9793efdc63SAdrian Chadd 9893efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 9993efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 10093efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 10193efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 10293efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 10393efdc63SAdrian Chadd 10493efdc63SAdrian Chadd 105831f5dcfSAlexander Motin static void 106831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 107831f5dcfSAlexander Motin { 108831f5dcfSAlexander Motin if (error != 0) { 109831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 110831f5dcfSAlexander Motin return; 111831f5dcfSAlexander Motin } 112831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 113831f5dcfSAlexander Motin } 114831f5dcfSAlexander Motin 115d6b3aaf8SOleksandr Tymoshenko static int 116d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 117d6b3aaf8SOleksandr Tymoshenko { 118d6b3aaf8SOleksandr Tymoshenko va_list ap; 119d6b3aaf8SOleksandr Tymoshenko int retval; 120d6b3aaf8SOleksandr Tymoshenko 121d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 122d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 123d6b3aaf8SOleksandr Tymoshenko 124d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 125d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 126d6b3aaf8SOleksandr Tymoshenko va_end(ap); 127d6b3aaf8SOleksandr Tymoshenko return (retval); 128d6b3aaf8SOleksandr Tymoshenko } 129d6b3aaf8SOleksandr Tymoshenko 130831f5dcfSAlexander Motin static void 131831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 132831f5dcfSAlexander Motin { 133831f5dcfSAlexander Motin slot_printf(slot, 134831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 135831f5dcfSAlexander Motin 136831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 137831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 138831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 139831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 140831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 141831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 142831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 143831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 144831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 145831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 146831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 147831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 148831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 149831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 150831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 151831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 152831f5dcfSAlexander Motin slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n", 153831f5dcfSAlexander Motin RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS)); 154831f5dcfSAlexander Motin slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n", 155831f5dcfSAlexander Motin RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT)); 156831f5dcfSAlexander Motin 157831f5dcfSAlexander Motin slot_printf(slot, 158831f5dcfSAlexander Motin "===========================================\n"); 159831f5dcfSAlexander Motin } 160831f5dcfSAlexander Motin 161831f5dcfSAlexander Motin static void 162831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 163831f5dcfSAlexander Motin { 164831f5dcfSAlexander Motin int timeout; 165831f5dcfSAlexander Motin 166d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 1676e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 168831f5dcfSAlexander Motin return; 169831f5dcfSAlexander Motin } 170831f5dcfSAlexander Motin 171831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 172831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 173d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 174831f5dcfSAlexander Motin uint32_t clock; 175831f5dcfSAlexander Motin 176831f5dcfSAlexander Motin /* This is to force an update */ 177831f5dcfSAlexander Motin clock = slot->clock; 178831f5dcfSAlexander Motin slot->clock = 0; 179831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 180831f5dcfSAlexander Motin } 181831f5dcfSAlexander Motin 182d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 183831f5dcfSAlexander Motin slot->clock = 0; 184d8208d9eSAlexander Motin slot->power = 0; 185d8208d9eSAlexander Motin } 186831f5dcfSAlexander Motin 18761bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 18861bc42f7SIan Lepore 18961bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 19061bc42f7SIan Lepore /* 19161bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 19261bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 19361bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 19461bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 19561bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 19661bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 19761bc42f7SIan Lepore */ 19861bc42f7SIan Lepore timeout = 10000; 19961bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 20061bc42f7SIan Lepore if (timeout <= 0) 20161bc42f7SIan Lepore break; 20261bc42f7SIan Lepore timeout--; 20361bc42f7SIan Lepore DELAY(1); 20461bc42f7SIan Lepore } 20561bc42f7SIan Lepore } 20661bc42f7SIan Lepore 207831f5dcfSAlexander Motin /* Wait max 100 ms */ 20861bc42f7SIan Lepore timeout = 10000; 209831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 21061bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 21161bc42f7SIan Lepore if (timeout <= 0) { 21261bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 21361bc42f7SIan Lepore mask); 214831f5dcfSAlexander Motin sdhci_dumpregs(slot); 215831f5dcfSAlexander Motin return; 216831f5dcfSAlexander Motin } 217831f5dcfSAlexander Motin timeout--; 21861bc42f7SIan Lepore DELAY(10); 219831f5dcfSAlexander Motin } 220831f5dcfSAlexander Motin } 221831f5dcfSAlexander Motin 222831f5dcfSAlexander Motin static void 223831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 224831f5dcfSAlexander Motin { 225831f5dcfSAlexander Motin 226831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 227831f5dcfSAlexander Motin 228831f5dcfSAlexander Motin /* Enable interrupts. */ 229831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 230831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 231831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 232831f5dcfSAlexander Motin SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | 233831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 234831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 235831f5dcfSAlexander Motin SDHCI_INT_ACMD12ERR; 236831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 237831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 238831f5dcfSAlexander Motin } 239831f5dcfSAlexander Motin 240831f5dcfSAlexander Motin static void 241831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 242831f5dcfSAlexander Motin { 24393efdc63SAdrian Chadd uint32_t clk_base; 24493efdc63SAdrian Chadd uint32_t clk_sel; 245831f5dcfSAlexander Motin uint32_t res; 246831f5dcfSAlexander Motin uint16_t clk; 2478f3b7d56SOleksandr Tymoshenko uint16_t div; 248831f5dcfSAlexander Motin int timeout; 249831f5dcfSAlexander Motin 250831f5dcfSAlexander Motin if (clock == slot->clock) 251831f5dcfSAlexander Motin return; 252831f5dcfSAlexander Motin slot->clock = clock; 253831f5dcfSAlexander Motin 254831f5dcfSAlexander Motin /* Turn off the clock. */ 2554ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 2564ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 257831f5dcfSAlexander Motin /* If no clock requested - left it so. */ 258831f5dcfSAlexander Motin if (clock == 0) 259831f5dcfSAlexander Motin return; 260ceb9e9f7SIan Lepore 26193efdc63SAdrian Chadd /* Determine the clock base frequency */ 26293efdc63SAdrian Chadd clk_base = slot->max_clk; 26393efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 26493efdc63SAdrian Chadd clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & BCM577XX_CTRL_CLKSEL_MASK; 26593efdc63SAdrian Chadd 26693efdc63SAdrian Chadd /* Select clock source appropriate for the requested frequency. */ 26793efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 26893efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 26993efdc63SAdrian Chadd clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << BCM577XX_CTRL_CLKSEL_SHIFT); 27093efdc63SAdrian Chadd } else { 27193efdc63SAdrian Chadd clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << BCM577XX_CTRL_CLKSEL_SHIFT); 27293efdc63SAdrian Chadd } 27393efdc63SAdrian Chadd 27493efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 27593efdc63SAdrian Chadd } 27693efdc63SAdrian Chadd 277ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 278ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 279ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 280ceb9e9f7SIan Lepore 2818f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 282831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 28393efdc63SAdrian Chadd res = clk_base; 28457677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 285831f5dcfSAlexander Motin if (res <= clock) 286831f5dcfSAlexander Motin break; 287831f5dcfSAlexander Motin res >>= 1; 288831f5dcfSAlexander Motin } 289831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 2908f3b7d56SOleksandr Tymoshenko div >>= 1; 2918f3b7d56SOleksandr Tymoshenko } 2928f3b7d56SOleksandr Tymoshenko else { 2938f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023*2 */ 29493efdc63SAdrian Chadd if (clock >= clk_base) 29557677a3aSOleksandr Tymoshenko div = 0; 2968f3b7d56SOleksandr Tymoshenko else { 29757677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 29893efdc63SAdrian Chadd if ((clk_base / div) <= clock) 2998f3b7d56SOleksandr Tymoshenko break; 3008f3b7d56SOleksandr Tymoshenko } 3018f3b7d56SOleksandr Tymoshenko } 3028f3b7d56SOleksandr Tymoshenko div >>= 1; 3038f3b7d56SOleksandr Tymoshenko } 3048f3b7d56SOleksandr Tymoshenko 3058f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 30693efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 30793efdc63SAdrian Chadd div, clock, clk_base); 3088f3b7d56SOleksandr Tymoshenko 309831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 3108f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 3118f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 3128f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 3138f3b7d56SOleksandr Tymoshenko 314831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 315831f5dcfSAlexander Motin /* Enable clock. */ 316831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 317831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 318831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 319831f5dcfSAlexander Motin timeout = 10; 320831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 321831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 322831f5dcfSAlexander Motin if (timeout == 0) { 323831f5dcfSAlexander Motin slot_printf(slot, 324831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 325831f5dcfSAlexander Motin sdhci_dumpregs(slot); 326831f5dcfSAlexander Motin return; 327831f5dcfSAlexander Motin } 328831f5dcfSAlexander Motin timeout--; 329831f5dcfSAlexander Motin DELAY(1000); 330831f5dcfSAlexander Motin } 331831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 332831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 333831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 334831f5dcfSAlexander Motin } 335831f5dcfSAlexander Motin 336831f5dcfSAlexander Motin static void 337831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 338831f5dcfSAlexander Motin { 339831f5dcfSAlexander Motin uint8_t pwr; 340831f5dcfSAlexander Motin 341831f5dcfSAlexander Motin if (slot->power == power) 342831f5dcfSAlexander Motin return; 343d6b3aaf8SOleksandr Tymoshenko 344831f5dcfSAlexander Motin slot->power = power; 345831f5dcfSAlexander Motin 346831f5dcfSAlexander Motin /* Turn off the power. */ 347831f5dcfSAlexander Motin pwr = 0; 348831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 349831f5dcfSAlexander Motin /* If power down requested - left it so. */ 350831f5dcfSAlexander Motin if (power == 0) 351831f5dcfSAlexander Motin return; 352831f5dcfSAlexander Motin /* Set voltage. */ 353831f5dcfSAlexander Motin switch (1 << power) { 354831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 355831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 356831f5dcfSAlexander Motin break; 357831f5dcfSAlexander Motin case MMC_OCR_290_300: 358831f5dcfSAlexander Motin case MMC_OCR_300_310: 359831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 360831f5dcfSAlexander Motin break; 361831f5dcfSAlexander Motin case MMC_OCR_320_330: 362831f5dcfSAlexander Motin case MMC_OCR_330_340: 363831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 364831f5dcfSAlexander Motin break; 365831f5dcfSAlexander Motin } 366831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 367831f5dcfSAlexander Motin /* Turn on the power. */ 368831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 369831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 370831f5dcfSAlexander Motin } 371831f5dcfSAlexander Motin 372831f5dcfSAlexander Motin static void 373831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 374831f5dcfSAlexander Motin { 375831f5dcfSAlexander Motin uint32_t data; 376831f5dcfSAlexander Motin char *buffer; 377831f5dcfSAlexander Motin size_t left; 378831f5dcfSAlexander Motin 379831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 380831f5dcfSAlexander Motin buffer += slot->offset; 381831f5dcfSAlexander Motin /* Transfer one block at a time. */ 382831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 383831f5dcfSAlexander Motin slot->offset += left; 384831f5dcfSAlexander Motin 385831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 386d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 387831f5dcfSAlexander Motin DELAY(10); 388ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 389831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 390831f5dcfSAlexander Motin while (left > 3) { 391831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 392831f5dcfSAlexander Motin buffer[0] = data; 393831f5dcfSAlexander Motin buffer[1] = (data >> 8); 394831f5dcfSAlexander Motin buffer[2] = (data >> 16); 395831f5dcfSAlexander Motin buffer[3] = (data >> 24); 396831f5dcfSAlexander Motin buffer += 4; 397831f5dcfSAlexander Motin left -= 4; 398831f5dcfSAlexander Motin } 399831f5dcfSAlexander Motin } else { 400d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 401831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 402831f5dcfSAlexander Motin left &= 3; 403831f5dcfSAlexander Motin } 404831f5dcfSAlexander Motin /* Handle uneven size case. */ 405831f5dcfSAlexander Motin if (left > 0) { 406831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 407831f5dcfSAlexander Motin while (left > 0) { 408831f5dcfSAlexander Motin *(buffer++) = data; 409831f5dcfSAlexander Motin data >>= 8; 410831f5dcfSAlexander Motin left--; 411831f5dcfSAlexander Motin } 412831f5dcfSAlexander Motin } 413831f5dcfSAlexander Motin } 414831f5dcfSAlexander Motin 415831f5dcfSAlexander Motin static void 416831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 417831f5dcfSAlexander Motin { 418831f5dcfSAlexander Motin uint32_t data = 0; 419831f5dcfSAlexander Motin char *buffer; 420831f5dcfSAlexander Motin size_t left; 421831f5dcfSAlexander Motin 422831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 423831f5dcfSAlexander Motin buffer += slot->offset; 424831f5dcfSAlexander Motin /* Transfer one block at a time. */ 425831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 426831f5dcfSAlexander Motin slot->offset += left; 427831f5dcfSAlexander Motin 428ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 429831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 430831f5dcfSAlexander Motin while (left > 3) { 431831f5dcfSAlexander Motin data = buffer[0] + 432831f5dcfSAlexander Motin (buffer[1] << 8) + 433831f5dcfSAlexander Motin (buffer[2] << 16) + 434831f5dcfSAlexander Motin (buffer[3] << 24); 435831f5dcfSAlexander Motin left -= 4; 436831f5dcfSAlexander Motin buffer += 4; 437831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 438831f5dcfSAlexander Motin } 439831f5dcfSAlexander Motin } else { 440d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 441831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 442831f5dcfSAlexander Motin left &= 3; 443831f5dcfSAlexander Motin } 444831f5dcfSAlexander Motin /* Handle uneven size case. */ 445831f5dcfSAlexander Motin if (left > 0) { 446831f5dcfSAlexander Motin while (left > 0) { 447831f5dcfSAlexander Motin data <<= 8; 448831f5dcfSAlexander Motin data += *(buffer++); 449831f5dcfSAlexander Motin left--; 450831f5dcfSAlexander Motin } 451831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 452831f5dcfSAlexander Motin } 453831f5dcfSAlexander Motin } 454831f5dcfSAlexander Motin 455831f5dcfSAlexander Motin static void 456831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 457831f5dcfSAlexander Motin { 458831f5dcfSAlexander Motin 459831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 460831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 461831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 462831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 463831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 464831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 465831f5dcfSAlexander Motin break; 466831f5dcfSAlexander Motin } 467831f5dcfSAlexander Motin } else { 468831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 469831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 470831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 471831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 472831f5dcfSAlexander Motin break; 473831f5dcfSAlexander Motin } 474831f5dcfSAlexander Motin } 475831f5dcfSAlexander Motin } 476831f5dcfSAlexander Motin 477831f5dcfSAlexander Motin static void 478831f5dcfSAlexander Motin sdhci_card_delay(void *arg) 479831f5dcfSAlexander Motin { 480831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 481831f5dcfSAlexander Motin 482831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 483831f5dcfSAlexander Motin } 484831f5dcfSAlexander Motin 485831f5dcfSAlexander Motin static void 486831f5dcfSAlexander Motin sdhci_card_task(void *arg, int pending) 487831f5dcfSAlexander Motin { 488831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 489831f5dcfSAlexander Motin 490831f5dcfSAlexander Motin SDHCI_LOCK(slot); 4916e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 492831f5dcfSAlexander Motin if (slot->dev == NULL) { 493831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 494d6b3aaf8SOleksandr Tymoshenko slot->dev = device_add_child(slot->bus, "mmc", -1); 495831f5dcfSAlexander Motin device_set_ivars(slot->dev, slot); 496831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 497831f5dcfSAlexander Motin device_probe_and_attach(slot->dev); 498831f5dcfSAlexander Motin } else 499831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 500831f5dcfSAlexander Motin } else { 501831f5dcfSAlexander Motin if (slot->dev != NULL) { 502831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 503831f5dcfSAlexander Motin device_t d = slot->dev; 504831f5dcfSAlexander Motin slot->dev = NULL; 505831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 506d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 507831f5dcfSAlexander Motin } else 508831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 509831f5dcfSAlexander Motin } 510831f5dcfSAlexander Motin } 511831f5dcfSAlexander Motin 512d6b3aaf8SOleksandr Tymoshenko int 513d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 514831f5dcfSAlexander Motin { 51587a6a871SIan Lepore uint32_t caps, freq; 516d6b3aaf8SOleksandr Tymoshenko int err; 517831f5dcfSAlexander Motin 518831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 519d6b3aaf8SOleksandr Tymoshenko slot->num = num; 520d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 521d6b3aaf8SOleksandr Tymoshenko 522831f5dcfSAlexander Motin /* Allocate DMA tag. */ 523831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 524831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 525831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 526831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 527831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 528831f5dcfSAlexander Motin &slot->dmatag); 529831f5dcfSAlexander Motin if (err != 0) { 530831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 531831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 532d6b3aaf8SOleksandr Tymoshenko return (err); 533831f5dcfSAlexander Motin } 534831f5dcfSAlexander Motin /* Allocate DMA memory. */ 535831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 536831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 537831f5dcfSAlexander Motin if (err != 0) { 538831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 539831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 540d6b3aaf8SOleksandr Tymoshenko return (err); 541831f5dcfSAlexander Motin } 542831f5dcfSAlexander Motin /* Map the memory. */ 543831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 544831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 545831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 546831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 547831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 548831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 549d6b3aaf8SOleksandr Tymoshenko if(err) 550d6b3aaf8SOleksandr Tymoshenko return (err); 551d6b3aaf8SOleksandr Tymoshenko else 552d6b3aaf8SOleksandr Tymoshenko return (EFAULT); 553831f5dcfSAlexander Motin } 554d6b3aaf8SOleksandr Tymoshenko 555831f5dcfSAlexander Motin /* Initialize slot. */ 556831f5dcfSAlexander Motin sdhci_init(slot); 557d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 558d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 5598f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) 5608f3b7d56SOleksandr Tymoshenko caps = slot->caps; 5618f3b7d56SOleksandr Tymoshenko else 562831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 563831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 56433aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 56587a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 56687a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 56733aad34dSOleksandr Tymoshenko else 56887a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 56987a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 57087a6a871SIan Lepore if (freq != 0) 57187a6a871SIan Lepore slot->max_clk = freq * 1000000; 57287a6a871SIan Lepore /* 57387a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 57487a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 57587a6a871SIan Lepore * with an assumption, so complain about it. 57687a6a871SIan Lepore */ 577831f5dcfSAlexander Motin if (slot->max_clk == 0) { 57887a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 579831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 58033aad34dSOleksandr Tymoshenko "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ); 581831f5dcfSAlexander Motin } 582831f5dcfSAlexander Motin /* Calculate timeout clock frequency. */ 5838f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 5848f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 5858f3b7d56SOleksandr Tymoshenko } else { 586831f5dcfSAlexander Motin slot->timeout_clk = 587831f5dcfSAlexander Motin (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; 5888f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 5898f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 5908f3b7d56SOleksandr Tymoshenko } 59187a6a871SIan Lepore /* 59287a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 59387a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 59487a6a871SIan Lepore * max timeout, but still mention it. 59587a6a871SIan Lepore */ 596831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 597831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 598ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 599ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 600831f5dcfSAlexander Motin } 601831f5dcfSAlexander Motin 60257677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 603831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 604831f5dcfSAlexander Motin slot->host.host_ocr = 0; 605831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 606831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 607831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 608831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 609831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_180) 610831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 611831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 612831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 613831f5dcfSAlexander Motin "support voltages.\n"); 614831f5dcfSAlexander Motin } 615831f5dcfSAlexander Motin slot->host.caps = MMC_CAP_4_BIT_DATA; 6162d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 6172d1731b8SIan Lepore slot->host.caps |= MMC_CAP_8_BIT_DATA; 618831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 619831f5dcfSAlexander Motin slot->host.caps |= MMC_CAP_HSPEED; 620831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 621831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 622831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 623d6b3aaf8SOleksandr Tymoshenko 624d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 625831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 626d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 627831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 628831f5dcfSAlexander Motin 629c3a0f75aSOleksandr Tymoshenko /* 630c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 631c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 632c3a0f75aSOleksandr Tymoshenko */ 633c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 634c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 635c3a0f75aSOleksandr Tymoshenko 6365b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 6372d1731b8SIan Lepore slot_printf(slot, "%uMHz%s %s%s%s%s %s\n", 638831f5dcfSAlexander Motin slot->max_clk / 1000000, 639831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 6402d1731b8SIan Lepore (caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 6412d1731b8SIan Lepore ((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 642831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 643831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 644831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 645831f5dcfSAlexander Motin (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 646831f5dcfSAlexander Motin sdhci_dumpregs(slot); 647831f5dcfSAlexander Motin } 648831f5dcfSAlexander Motin 649ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 650ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 651ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 652ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 653ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 654831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 655831f5dcfSAlexander Motin callout_init(&slot->card_callout, 1); 656e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 657ba6fc1c7SLuiz Otavio O Souza 658831f5dcfSAlexander Motin return (0); 659831f5dcfSAlexander Motin } 660831f5dcfSAlexander Motin 661d6b3aaf8SOleksandr Tymoshenko void 662d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 663831f5dcfSAlexander Motin { 664d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 665d6b3aaf8SOleksandr Tymoshenko } 666831f5dcfSAlexander Motin 667d6b3aaf8SOleksandr Tymoshenko int 668d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 669d6b3aaf8SOleksandr Tymoshenko { 670831f5dcfSAlexander Motin device_t d; 671831f5dcfSAlexander Motin 672e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 673831f5dcfSAlexander Motin callout_drain(&slot->card_callout); 674831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 675831f5dcfSAlexander Motin 676831f5dcfSAlexander Motin SDHCI_LOCK(slot); 677831f5dcfSAlexander Motin d = slot->dev; 678831f5dcfSAlexander Motin slot->dev = NULL; 679831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 680831f5dcfSAlexander Motin if (d != NULL) 681d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 682831f5dcfSAlexander Motin 683831f5dcfSAlexander Motin SDHCI_LOCK(slot); 684831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 685831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 686831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 687831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 688831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 689d6b3aaf8SOleksandr Tymoshenko 690831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 691d6b3aaf8SOleksandr Tymoshenko 692831f5dcfSAlexander Motin return (0); 693831f5dcfSAlexander Motin } 694831f5dcfSAlexander Motin 695d6b3aaf8SOleksandr Tymoshenko int 696d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 69792bf0e27SAlexander Motin { 698d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 69992bf0e27SAlexander Motin 70092bf0e27SAlexander Motin return (0); 70192bf0e27SAlexander Motin } 70292bf0e27SAlexander Motin 703d6b3aaf8SOleksandr Tymoshenko int 704d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 70592bf0e27SAlexander Motin { 706d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 70792bf0e27SAlexander Motin 708d6b3aaf8SOleksandr Tymoshenko return (0); 70992bf0e27SAlexander Motin } 71092bf0e27SAlexander Motin 71157677a3aSOleksandr Tymoshenko uint32_t 71257677a3aSOleksandr Tymoshenko sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot) 71357677a3aSOleksandr Tymoshenko { 71457677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 71557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 71657677a3aSOleksandr Tymoshenko else 71757677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 71857677a3aSOleksandr Tymoshenko } 71957677a3aSOleksandr Tymoshenko 7206e37fb2bSIan Lepore bool 7216e37fb2bSIan Lepore sdhci_generic_get_card_present(device_t brdev, struct sdhci_slot *slot) 7226e37fb2bSIan Lepore { 7236e37fb2bSIan Lepore 7246e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 7256e37fb2bSIan Lepore } 7266e37fb2bSIan Lepore 727d6b3aaf8SOleksandr Tymoshenko int 728d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 729831f5dcfSAlexander Motin { 730831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 731831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 732831f5dcfSAlexander Motin 733831f5dcfSAlexander Motin SDHCI_LOCK(slot); 734831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 735831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 736831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 737831f5dcfSAlexander Motin sdhci_init(slot); 738831f5dcfSAlexander Motin } 739831f5dcfSAlexander Motin /* Configure the bus. */ 740831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 741831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 7422d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 7432d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 744831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 7452d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 7462d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 7472d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 7482d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 7492d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 7502d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 7512d1731b8SIan Lepore } else { 7522d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 7532d1731b8SIan Lepore } 754bba987dcSIan Lepore if (ios->timing == bus_timing_hs && 755bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 756831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 757831f5dcfSAlexander Motin else 758831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 759831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 760831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 761d6b3aaf8SOleksandr Tymoshenko if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 762831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 763831f5dcfSAlexander Motin 764831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 765831f5dcfSAlexander Motin return (0); 766831f5dcfSAlexander Motin } 767831f5dcfSAlexander Motin 768831f5dcfSAlexander Motin static void 769e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 770e64f01a9SIan Lepore { 771e64f01a9SIan Lepore struct mmc_request *req; 772e64f01a9SIan Lepore 773e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 774e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 775e64f01a9SIan Lepore req = slot->req; 776e64f01a9SIan Lepore slot->req = NULL; 777e64f01a9SIan Lepore slot->curcmd = NULL; 778e64f01a9SIan Lepore req->done(req); 779e64f01a9SIan Lepore } 780e64f01a9SIan Lepore } 781e64f01a9SIan Lepore 782e64f01a9SIan Lepore static void 783e64f01a9SIan Lepore sdhci_timeout(void *arg) 784e64f01a9SIan Lepore { 785e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 786e64f01a9SIan Lepore 787e64f01a9SIan Lepore if (slot->curcmd != NULL) { 7887e586643SIan Lepore slot_printf(slot, " Controller timeout\n"); 7897e586643SIan Lepore sdhci_dumpregs(slot); 790a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA); 791e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 792e64f01a9SIan Lepore sdhci_req_done(slot); 7937e586643SIan Lepore } else { 7947e586643SIan Lepore slot_printf(slot, " Spurious timeout - no active command\n"); 795e64f01a9SIan Lepore } 796e64f01a9SIan Lepore } 797e64f01a9SIan Lepore 798e64f01a9SIan Lepore static void 799831f5dcfSAlexander Motin sdhci_set_transfer_mode(struct sdhci_slot *slot, 800831f5dcfSAlexander Motin struct mmc_data *data) 801831f5dcfSAlexander Motin { 802831f5dcfSAlexander Motin uint16_t mode; 803831f5dcfSAlexander Motin 804831f5dcfSAlexander Motin if (data == NULL) 805831f5dcfSAlexander Motin return; 806831f5dcfSAlexander Motin 807831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 808831f5dcfSAlexander Motin if (data->len > 512) 809831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 810831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 811831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 812831f5dcfSAlexander Motin if (slot->req->stop) 813831f5dcfSAlexander Motin mode |= SDHCI_TRNS_ACMD12; 814831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 815831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 816831f5dcfSAlexander Motin 817831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 818831f5dcfSAlexander Motin } 819831f5dcfSAlexander Motin 820831f5dcfSAlexander Motin static void 821831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 822831f5dcfSAlexander Motin { 823831f5dcfSAlexander Motin int flags, timeout; 824*90993663SIan Lepore uint32_t mask; 825831f5dcfSAlexander Motin 826831f5dcfSAlexander Motin slot->curcmd = cmd; 827831f5dcfSAlexander Motin slot->cmd_done = 0; 828831f5dcfSAlexander Motin 829831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 830831f5dcfSAlexander Motin 831831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 832831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 833831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 834831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 835e64f01a9SIan Lepore sdhci_req_done(slot); 836831f5dcfSAlexander Motin return; 837831f5dcfSAlexander Motin } 838831f5dcfSAlexander Motin 839d8208d9eSAlexander Motin /* Do not issue command if there is no card, clock or power. 840d8208d9eSAlexander Motin * Controller will not detect timeout without clock active. */ 8416e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 842d8208d9eSAlexander Motin slot->power == 0 || 843d8208d9eSAlexander Motin slot->clock == 0) { 844831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 845e64f01a9SIan Lepore sdhci_req_done(slot); 846831f5dcfSAlexander Motin return; 847831f5dcfSAlexander Motin } 848831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 849831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 850831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 851831f5dcfSAlexander Motin if (cmd->data || (cmd->flags & MMC_RSP_BUSY)) 852831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 853831f5dcfSAlexander Motin /* We shouldn't wait for DAT for stop commands. */ 854831f5dcfSAlexander Motin if (cmd == slot->req->stop) 855831f5dcfSAlexander Motin mask &= ~SDHCI_DAT_INHIBIT; 8568775ab45SIan Lepore /* 8578775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 8588775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 8598775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 8608775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 8618775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 8628775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 8638775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 8648775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 8658775ab45SIan Lepore */ 8668775ab45SIan Lepore timeout = 250; 867*90993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 868831f5dcfSAlexander Motin if (timeout == 0) { 869831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 870831f5dcfSAlexander Motin "inhibit bit(s).\n"); 871831f5dcfSAlexander Motin sdhci_dumpregs(slot); 872831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 873e64f01a9SIan Lepore sdhci_req_done(slot); 874831f5dcfSAlexander Motin return; 875831f5dcfSAlexander Motin } 876831f5dcfSAlexander Motin timeout--; 877831f5dcfSAlexander Motin DELAY(1000); 878831f5dcfSAlexander Motin } 879831f5dcfSAlexander Motin 880831f5dcfSAlexander Motin /* Prepare command flags. */ 881831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 882831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 883831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 884831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 885831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 886831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 887831f5dcfSAlexander Motin else 888831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 889831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 890831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 891831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 892831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 893831f5dcfSAlexander Motin if (cmd->data) 894831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 895831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 896831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 897831f5dcfSAlexander Motin /* Prepare data. */ 898831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 899831f5dcfSAlexander Motin /* 900831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 901831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 902831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 903831f5dcfSAlexander Motin */ 904831f5dcfSAlexander Motin if (slot->data_done == 0) { 905831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 906831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 907831f5dcfSAlexander Motin } 908831f5dcfSAlexander Motin /* Set command argument. */ 909831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 910831f5dcfSAlexander Motin /* Set data transfer mode. */ 911831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 912831f5dcfSAlexander Motin /* Start command. */ 913d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 914a6873fd1SIan Lepore /* Start timeout callout. */ 915ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 916ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 917831f5dcfSAlexander Motin } 918831f5dcfSAlexander Motin 919831f5dcfSAlexander Motin static void 920831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 921831f5dcfSAlexander Motin { 922831f5dcfSAlexander Motin int i; 923831f5dcfSAlexander Motin 924831f5dcfSAlexander Motin slot->cmd_done = 1; 925831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 926831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 927831f5dcfSAlexander Motin * happened first. */ 928831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 929831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 930831f5dcfSAlexander Motin if (slot->curcmd->error) { 931831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 932831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 933831f5dcfSAlexander Motin sdhci_start(slot); 934831f5dcfSAlexander Motin return; 935831f5dcfSAlexander Motin } 936831f5dcfSAlexander Motin /* If command has response - fetch it. */ 937831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 938831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 939831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 940831f5dcfSAlexander Motin uint8_t extra = 0; 941831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 942831f5dcfSAlexander Motin uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4); 943677ee494SIan Lepore if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 944677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 945677ee494SIan Lepore else { 946677ee494SIan Lepore slot->curcmd->resp[3 - i] = 947677ee494SIan Lepore (val << 8) | extra; 948831f5dcfSAlexander Motin extra = val >> 24; 949831f5dcfSAlexander Motin } 950677ee494SIan Lepore } 951831f5dcfSAlexander Motin } else 952831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 953831f5dcfSAlexander Motin } 954831f5dcfSAlexander Motin /* If data ready - finish. */ 955831f5dcfSAlexander Motin if (slot->data_done) 956831f5dcfSAlexander Motin sdhci_start(slot); 957831f5dcfSAlexander Motin } 958831f5dcfSAlexander Motin 959831f5dcfSAlexander Motin static void 960831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 961831f5dcfSAlexander Motin { 962831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 963831f5dcfSAlexander Motin uint8_t div; 964831f5dcfSAlexander Motin 965831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 966831f5dcfSAlexander Motin slot->data_done = 1; 967831f5dcfSAlexander Motin return; 968831f5dcfSAlexander Motin } 969831f5dcfSAlexander Motin 970831f5dcfSAlexander Motin slot->data_done = 0; 971831f5dcfSAlexander Motin 972831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 973831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 974ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 975ceb9e9f7SIan Lepore div = 0xE; 976ceb9e9f7SIan Lepore } else { 977831f5dcfSAlexander Motin target_timeout = 1000000; 978831f5dcfSAlexander Motin div = 0; 979831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 980ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 981ceb9e9f7SIan Lepore ++div; 982831f5dcfSAlexander Motin current_timeout <<= 1; 983831f5dcfSAlexander Motin } 984831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 985ceb9e9f7SIan Lepore if (div < 0xE && 986ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 987ceb9e9f7SIan Lepore ++div; 988831f5dcfSAlexander Motin } 989ceb9e9f7SIan Lepore } 990831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 991831f5dcfSAlexander Motin 992831f5dcfSAlexander Motin if (data == NULL) 993831f5dcfSAlexander Motin return; 994831f5dcfSAlexander Motin 995831f5dcfSAlexander Motin /* Use DMA if possible. */ 996831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 997831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 998831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 999d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1000831f5dcfSAlexander Motin (data->len <= 512)) 1001831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1002831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1003d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1004831f5dcfSAlexander Motin ((data->len) & 0x3)) 1005831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1006831f5dcfSAlexander Motin /* Load DMA buffer. */ 1007831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1008831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1009ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1010ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1011831f5dcfSAlexander Motin else { 1012831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 1013ecc2d997SRui Paulo (data->len < DMA_BLOCK_SIZE) ? 1014ecc2d997SRui Paulo data->len : DMA_BLOCK_SIZE); 1015ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1016ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1017831f5dcfSAlexander Motin } 1018831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1019831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1020831f5dcfSAlexander Motin * for the last page and unmask else. */ 1021831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 1022831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1023831f5dcfSAlexander Motin else 1024831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1025831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1026831f5dcfSAlexander Motin } 1027831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1028831f5dcfSAlexander Motin slot->offset = 0; 1029831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 1030831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_SIZE, 1031831f5dcfSAlexander Motin SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512)); 1032831f5dcfSAlexander Motin /* Set block count. */ 1033831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1034831f5dcfSAlexander Motin } 1035831f5dcfSAlexander Motin 1036c3a0f75aSOleksandr Tymoshenko void 1037831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1038831f5dcfSAlexander Motin { 1039831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1040831f5dcfSAlexander Motin 1041831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1042ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1043831f5dcfSAlexander Motin * happened first. */ 1044831f5dcfSAlexander Motin if (!slot->cmd_done) { 1045831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1046831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1047831f5dcfSAlexander Motin } 1048831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1049a98788edSIan Lepore if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) { 1050831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1051831f5dcfSAlexander Motin size_t left = data->len - slot->offset; 1052ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1053ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1054831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1055831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1056831f5dcfSAlexander Motin } else 1057ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1058ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1059831f5dcfSAlexander Motin } 1060a98788edSIan Lepore slot->data_done = 1; 1061831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1062831f5dcfSAlexander Motin if (slot->curcmd->error) { 1063831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1064831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1065831f5dcfSAlexander Motin sdhci_start(slot); 1066831f5dcfSAlexander Motin return; 1067831f5dcfSAlexander Motin } 1068831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1069831f5dcfSAlexander Motin if (slot->cmd_done) 1070831f5dcfSAlexander Motin sdhci_start(slot); 1071831f5dcfSAlexander Motin } 1072831f5dcfSAlexander Motin 1073831f5dcfSAlexander Motin static void 1074831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1075831f5dcfSAlexander Motin { 1076831f5dcfSAlexander Motin struct mmc_request *req; 1077831f5dcfSAlexander Motin 1078831f5dcfSAlexander Motin req = slot->req; 1079831f5dcfSAlexander Motin if (req == NULL) 1080831f5dcfSAlexander Motin return; 1081831f5dcfSAlexander Motin 1082831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1083831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1084831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1085831f5dcfSAlexander Motin return; 1086831f5dcfSAlexander Motin } 1087831f5dcfSAlexander Motin /* We don't need this until using Auto-CMD12 feature 1088831f5dcfSAlexander Motin if (!(slot->flags & STOP_STARTED) && req->stop) { 1089831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1090831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1091831f5dcfSAlexander Motin return; 1092831f5dcfSAlexander Motin } 1093831f5dcfSAlexander Motin */ 10945b69a497SAlexander Motin if (sdhci_debug > 1) 10955b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 10965b69a497SAlexander Motin if (!req->cmd->error && 1097d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1098831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1099831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1100831f5dcfSAlexander Motin } 1101831f5dcfSAlexander Motin 1102e64f01a9SIan Lepore sdhci_req_done(slot); 1103831f5dcfSAlexander Motin } 1104831f5dcfSAlexander Motin 1105d6b3aaf8SOleksandr Tymoshenko int 1106d6b3aaf8SOleksandr Tymoshenko sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req) 1107831f5dcfSAlexander Motin { 1108831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1109831f5dcfSAlexander Motin 1110831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1111831f5dcfSAlexander Motin if (slot->req != NULL) { 1112831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1113831f5dcfSAlexander Motin return (EBUSY); 1114831f5dcfSAlexander Motin } 11155b69a497SAlexander Motin if (sdhci_debug > 1) { 11165b69a497SAlexander Motin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1117831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 11185b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 11195b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 11205b69a497SAlexander Motin } 1121831f5dcfSAlexander Motin slot->req = req; 1122831f5dcfSAlexander Motin slot->flags = 0; 1123831f5dcfSAlexander Motin sdhci_start(slot); 1124831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1125bea2dca2SAlexander Motin if (dumping) { 1126bea2dca2SAlexander Motin while (slot->req != NULL) { 1127d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 1128bea2dca2SAlexander Motin DELAY(10); 1129bea2dca2SAlexander Motin } 1130bea2dca2SAlexander Motin } 1131831f5dcfSAlexander Motin return (0); 1132831f5dcfSAlexander Motin } 1133831f5dcfSAlexander Motin 1134d6b3aaf8SOleksandr Tymoshenko int 1135d6b3aaf8SOleksandr Tymoshenko sdhci_generic_get_ro(device_t brdev, device_t reqdev) 1136831f5dcfSAlexander Motin { 1137831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1138831f5dcfSAlexander Motin uint32_t val; 1139831f5dcfSAlexander Motin 1140831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1141831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 1142831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1143831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 1144831f5dcfSAlexander Motin } 1145831f5dcfSAlexander Motin 1146d6b3aaf8SOleksandr Tymoshenko int 1147d6b3aaf8SOleksandr Tymoshenko sdhci_generic_acquire_host(device_t brdev, device_t reqdev) 1148831f5dcfSAlexander Motin { 1149831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1150831f5dcfSAlexander Motin int err = 0; 1151831f5dcfSAlexander Motin 1152831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1153831f5dcfSAlexander Motin while (slot->bus_busy) 1154d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1155831f5dcfSAlexander Motin slot->bus_busy++; 1156831f5dcfSAlexander Motin /* Activate led. */ 1157831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1158831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1159831f5dcfSAlexander Motin return (err); 1160831f5dcfSAlexander Motin } 1161831f5dcfSAlexander Motin 1162d6b3aaf8SOleksandr Tymoshenko int 1163d6b3aaf8SOleksandr Tymoshenko sdhci_generic_release_host(device_t brdev, device_t reqdev) 1164831f5dcfSAlexander Motin { 1165831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1166831f5dcfSAlexander Motin 1167831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1168831f5dcfSAlexander Motin /* Deactivate led. */ 1169831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1170831f5dcfSAlexander Motin slot->bus_busy--; 1171831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1172d493985aSAlexander Motin wakeup(slot); 1173831f5dcfSAlexander Motin return (0); 1174831f5dcfSAlexander Motin } 1175831f5dcfSAlexander Motin 1176831f5dcfSAlexander Motin static void 1177831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1178831f5dcfSAlexander Motin { 1179831f5dcfSAlexander Motin 1180831f5dcfSAlexander Motin if (!slot->curcmd) { 1181831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 1182831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1183831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1184831f5dcfSAlexander Motin return; 1185831f5dcfSAlexander Motin } 1186831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 1187831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1188831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 1189831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1190831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1191831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 1192831f5dcfSAlexander Motin 1193831f5dcfSAlexander Motin sdhci_finish_command(slot); 1194831f5dcfSAlexander Motin } 1195831f5dcfSAlexander Motin 1196831f5dcfSAlexander Motin static void 1197831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1198831f5dcfSAlexander Motin { 1199831f5dcfSAlexander Motin 1200831f5dcfSAlexander Motin if (!slot->curcmd) { 1201831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1202831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1203831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1204831f5dcfSAlexander Motin return; 1205831f5dcfSAlexander Motin } 1206831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1207831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1208831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1209831f5dcfSAlexander Motin "there is no active data operation.\n", 1210831f5dcfSAlexander Motin intmask); 1211831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1212831f5dcfSAlexander Motin return; 1213831f5dcfSAlexander Motin } 1214831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 1215831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1216acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1217831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1218831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1219831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1220831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 1221831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1222831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 1223831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1224831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 1225831f5dcfSAlexander Motin } 1226831f5dcfSAlexander Motin if (slot->curcmd->error) { 1227831f5dcfSAlexander Motin /* No need to continue after any error. */ 1228a98788edSIan Lepore goto done; 1229831f5dcfSAlexander Motin } 1230831f5dcfSAlexander Motin 1231831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 1232c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 1233c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 1234c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 1235c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask); 1236c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 1237c3a0f75aSOleksandr Tymoshenko } else 1238831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 1239c3a0f75aSOleksandr Tymoshenko } 1240831f5dcfSAlexander Motin /* Handle DMA border. */ 1241831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 1242831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1243831f5dcfSAlexander Motin size_t left; 1244831f5dcfSAlexander Motin 1245831f5dcfSAlexander Motin /* Unload DMA buffer... */ 1246831f5dcfSAlexander Motin left = data->len - slot->offset; 1247831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1248831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1249831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 1250831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1251831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1252831f5dcfSAlexander Motin } else { 1253831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1254831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 1255831f5dcfSAlexander Motin } 1256831f5dcfSAlexander Motin /* ... and reload it again. */ 1257831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 1258831f5dcfSAlexander Motin left = data->len - slot->offset; 1259831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1260831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1261831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 1262831f5dcfSAlexander Motin } else { 1263831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1264831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1265831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1266831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 1267831f5dcfSAlexander Motin } 1268831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1269831f5dcfSAlexander Motin * for the last page. */ 1270831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 1271831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1272831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1273831f5dcfSAlexander Motin } 1274831f5dcfSAlexander Motin /* Restart DMA. */ 1275831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1276831f5dcfSAlexander Motin } 1277831f5dcfSAlexander Motin /* We have got all data. */ 1278c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 1279c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 1280c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 1281c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1282c3a0f75aSOleksandr Tymoshenko } else 1283831f5dcfSAlexander Motin sdhci_finish_data(slot); 1284831f5dcfSAlexander Motin } 1285a98788edSIan Lepore done: 1286a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 1287a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 1288a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 1289a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1290a98788edSIan Lepore } else 1291a98788edSIan Lepore sdhci_finish_data(slot); 1292a98788edSIan Lepore return; 1293a98788edSIan Lepore } 1294c3a0f75aSOleksandr Tymoshenko } 1295831f5dcfSAlexander Motin 1296831f5dcfSAlexander Motin static void 1297831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot) 1298831f5dcfSAlexander Motin { 1299831f5dcfSAlexander Motin uint16_t err; 1300831f5dcfSAlexander Motin 1301831f5dcfSAlexander Motin err = RD4(slot, SDHCI_ACMD12_ERR); 1302831f5dcfSAlexander Motin if (!slot->curcmd) { 1303831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1304831f5dcfSAlexander Motin "there is no active command.\n", err); 1305831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1306831f5dcfSAlexander Motin return; 1307831f5dcfSAlexander Motin } 1308831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1309831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1310831f5dcfSAlexander Motin } 1311831f5dcfSAlexander Motin 1312d6b3aaf8SOleksandr Tymoshenko void 1313d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 1314831f5dcfSAlexander Motin { 13152b96b955SJustin Hibbits uint32_t intmask, present; 1316831f5dcfSAlexander Motin 1317831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1318831f5dcfSAlexander Motin /* Read slot interrupt status. */ 1319831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 1320831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 1321831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1322d6b3aaf8SOleksandr Tymoshenko return; 1323831f5dcfSAlexander Motin } 13245b69a497SAlexander Motin if (sdhci_debug > 2) 13255b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 13265b69a497SAlexander Motin 1327831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 1328831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 13296e37fb2bSIan Lepore present = SDHCI_GET_CARD_PRESENT(slot->bus, slot); 13302b96b955SJustin Hibbits slot->intmask &= 13312b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 13322b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 13332b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 13342b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 13352b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1336831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 1337831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1338831f5dcfSAlexander Motin 1339831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_REMOVE) { 13405b69a497SAlexander Motin if (bootverbose || sdhci_debug) 1341831f5dcfSAlexander Motin slot_printf(slot, "Card removed\n"); 1342831f5dcfSAlexander Motin callout_stop(&slot->card_callout); 1343831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, 1344831f5dcfSAlexander Motin &slot->card_task); 1345831f5dcfSAlexander Motin } 1346831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_INSERT) { 13475b69a497SAlexander Motin if (bootverbose || sdhci_debug) 1348831f5dcfSAlexander Motin slot_printf(slot, "Card inserted\n"); 1349831f5dcfSAlexander Motin callout_reset(&slot->card_callout, hz / 2, 1350831f5dcfSAlexander Motin sdhci_card_delay, slot); 1351831f5dcfSAlexander Motin } 1352831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1353831f5dcfSAlexander Motin } 1354831f5dcfSAlexander Motin /* Handle command interrupts. */ 1355831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 1356831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1357831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1358831f5dcfSAlexander Motin } 1359831f5dcfSAlexander Motin /* Handle data interrupts. */ 1360831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 1361831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 13627e586643SIan Lepore /* Dont call data_irq in case of errored command */ 13637e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 1364831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1365831f5dcfSAlexander Motin } 1366831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 1367831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 1368831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1369831f5dcfSAlexander Motin sdhci_acmd_irq(slot); 1370831f5dcfSAlexander Motin } 1371831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1372831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ACMD12ERR; 1373831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ERROR; 1374831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 1375831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 1376831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1377831f5dcfSAlexander Motin slot_printf(slot, 1378831f5dcfSAlexander Motin "Card is consuming too much power!\n"); 1379831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_BUS_POWER; 1380831f5dcfSAlexander Motin } 1381831f5dcfSAlexander Motin /* The rest is unknown. */ 1382831f5dcfSAlexander Motin if (intmask) { 1383831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 1384831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1385831f5dcfSAlexander Motin intmask); 1386831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1387831f5dcfSAlexander Motin } 1388831f5dcfSAlexander Motin 1389831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1390831f5dcfSAlexander Motin } 1391831f5dcfSAlexander Motin 1392d6b3aaf8SOleksandr Tymoshenko int 1393d6b3aaf8SOleksandr Tymoshenko sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1394831f5dcfSAlexander Motin { 1395831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1396831f5dcfSAlexander Motin 1397831f5dcfSAlexander Motin switch (which) { 1398831f5dcfSAlexander Motin default: 1399831f5dcfSAlexander Motin return (EINVAL); 1400831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1401bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 1402831f5dcfSAlexander Motin break; 1403831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1404bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 1405831f5dcfSAlexander Motin break; 1406831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1407bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 1408831f5dcfSAlexander Motin break; 1409831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1410bcd91d25SJayachandran C. *result = slot->host.ios.clock; 1411831f5dcfSAlexander Motin break; 1412831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1413bcd91d25SJayachandran C. *result = slot->host.f_min; 1414831f5dcfSAlexander Motin break; 1415831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 1416bcd91d25SJayachandran C. *result = slot->host.f_max; 1417831f5dcfSAlexander Motin break; 1418831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1419bcd91d25SJayachandran C. *result = slot->host.host_ocr; 1420831f5dcfSAlexander Motin break; 1421831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1422bcd91d25SJayachandran C. *result = slot->host.mode; 1423831f5dcfSAlexander Motin break; 1424831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1425bcd91d25SJayachandran C. *result = slot->host.ocr; 1426831f5dcfSAlexander Motin break; 1427831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1428bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 1429831f5dcfSAlexander Motin break; 1430831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1431bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 1432831f5dcfSAlexander Motin break; 1433831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1434bcd91d25SJayachandran C. *result = slot->host.caps; 1435831f5dcfSAlexander Motin break; 1436831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1437bcd91d25SJayachandran C. *result = slot->host.ios.timing; 1438831f5dcfSAlexander Motin break; 14393a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1440bcd91d25SJayachandran C. *result = 65535; 14413a4a2557SAlexander Motin break; 1442831f5dcfSAlexander Motin } 1443831f5dcfSAlexander Motin return (0); 1444831f5dcfSAlexander Motin } 1445831f5dcfSAlexander Motin 1446d6b3aaf8SOleksandr Tymoshenko int 1447d6b3aaf8SOleksandr Tymoshenko sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1448831f5dcfSAlexander Motin { 1449831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1450831f5dcfSAlexander Motin 1451831f5dcfSAlexander Motin switch (which) { 1452831f5dcfSAlexander Motin default: 1453831f5dcfSAlexander Motin return (EINVAL); 1454831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1455831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 1456831f5dcfSAlexander Motin break; 1457831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1458831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 1459831f5dcfSAlexander Motin break; 1460831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1461831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 1462831f5dcfSAlexander Motin break; 1463831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1464831f5dcfSAlexander Motin if (value > 0) { 146557677a3aSOleksandr Tymoshenko uint32_t max_clock; 146657677a3aSOleksandr Tymoshenko uint32_t clock; 1467831f5dcfSAlexander Motin int i; 1468831f5dcfSAlexander Motin 146957677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 147057677a3aSOleksandr Tymoshenko clock = max_clock; 147157677a3aSOleksandr Tymoshenko 147257677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 147357677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 147457677a3aSOleksandr Tymoshenko i <<= 1) { 1475831f5dcfSAlexander Motin if (clock <= value) 1476831f5dcfSAlexander Motin break; 1477831f5dcfSAlexander Motin clock >>= 1; 1478831f5dcfSAlexander Motin } 147957677a3aSOleksandr Tymoshenko } 148057677a3aSOleksandr Tymoshenko else { 148157677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 148257677a3aSOleksandr Tymoshenko i += 2) { 148357677a3aSOleksandr Tymoshenko if (clock <= value) 148457677a3aSOleksandr Tymoshenko break; 148557677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 148657677a3aSOleksandr Tymoshenko } 148757677a3aSOleksandr Tymoshenko } 148857677a3aSOleksandr Tymoshenko 1489831f5dcfSAlexander Motin slot->host.ios.clock = clock; 1490831f5dcfSAlexander Motin } else 1491831f5dcfSAlexander Motin slot->host.ios.clock = 0; 1492831f5dcfSAlexander Motin break; 1493831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1494831f5dcfSAlexander Motin slot->host.mode = value; 1495831f5dcfSAlexander Motin break; 1496831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1497831f5dcfSAlexander Motin slot->host.ocr = value; 1498831f5dcfSAlexander Motin break; 1499831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1500831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 1501831f5dcfSAlexander Motin break; 1502831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1503831f5dcfSAlexander Motin slot->host.ios.vdd = value; 1504831f5dcfSAlexander Motin break; 1505831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1506831f5dcfSAlexander Motin slot->host.ios.timing = value; 1507831f5dcfSAlexander Motin break; 1508831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1509831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1510831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1511831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 15123a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1513831f5dcfSAlexander Motin return (EINVAL); 1514831f5dcfSAlexander Motin } 1515831f5dcfSAlexander Motin return (0); 1516831f5dcfSAlexander Motin } 1517831f5dcfSAlexander Motin 1518d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1); 1519