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 92831f5dcfSAlexander Motin static void 93831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 94831f5dcfSAlexander Motin { 95831f5dcfSAlexander Motin if (error != 0) { 96831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 97831f5dcfSAlexander Motin return; 98831f5dcfSAlexander Motin } 99831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 100831f5dcfSAlexander Motin } 101831f5dcfSAlexander Motin 102d6b3aaf8SOleksandr Tymoshenko static int 103d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 104d6b3aaf8SOleksandr Tymoshenko { 105d6b3aaf8SOleksandr Tymoshenko va_list ap; 106d6b3aaf8SOleksandr Tymoshenko int retval; 107d6b3aaf8SOleksandr Tymoshenko 108d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 109d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 110d6b3aaf8SOleksandr Tymoshenko 111d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 112d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 113d6b3aaf8SOleksandr Tymoshenko va_end(ap); 114d6b3aaf8SOleksandr Tymoshenko return (retval); 115d6b3aaf8SOleksandr Tymoshenko } 116d6b3aaf8SOleksandr Tymoshenko 117831f5dcfSAlexander Motin static void 118831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 119831f5dcfSAlexander Motin { 120831f5dcfSAlexander Motin slot_printf(slot, 121831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 122831f5dcfSAlexander Motin 123831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 124831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 125831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 126831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 127831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 128831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 129831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 130831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 131831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 132831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 133831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 134831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 135831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 136831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 137831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 138831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 139831f5dcfSAlexander Motin slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n", 140831f5dcfSAlexander Motin RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS)); 141831f5dcfSAlexander Motin slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n", 142831f5dcfSAlexander Motin RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT)); 143831f5dcfSAlexander Motin 144831f5dcfSAlexander Motin slot_printf(slot, 145831f5dcfSAlexander Motin "===========================================\n"); 146831f5dcfSAlexander Motin } 147831f5dcfSAlexander Motin 148831f5dcfSAlexander Motin static void 149831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 150831f5dcfSAlexander Motin { 151831f5dcfSAlexander Motin int timeout; 152831f5dcfSAlexander Motin 153d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 154831f5dcfSAlexander Motin if (!(RD4(slot, SDHCI_PRESENT_STATE) & 155831f5dcfSAlexander Motin SDHCI_CARD_PRESENT)) 156831f5dcfSAlexander Motin return; 157831f5dcfSAlexander Motin } 158831f5dcfSAlexander Motin 159831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 160831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 161d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 162831f5dcfSAlexander Motin uint32_t clock; 163831f5dcfSAlexander Motin 164831f5dcfSAlexander Motin /* This is to force an update */ 165831f5dcfSAlexander Motin clock = slot->clock; 166831f5dcfSAlexander Motin slot->clock = 0; 167831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 168831f5dcfSAlexander Motin } 169831f5dcfSAlexander Motin 170d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 171831f5dcfSAlexander Motin slot->clock = 0; 172d8208d9eSAlexander Motin slot->power = 0; 173d8208d9eSAlexander Motin } 174831f5dcfSAlexander Motin 17561bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 17661bc42f7SIan Lepore 17761bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 17861bc42f7SIan Lepore /* 17961bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 18061bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 18161bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 18261bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 18361bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 18461bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 18561bc42f7SIan Lepore */ 18661bc42f7SIan Lepore timeout = 10000; 18761bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 18861bc42f7SIan Lepore if (timeout <= 0) 18961bc42f7SIan Lepore break; 19061bc42f7SIan Lepore timeout--; 19161bc42f7SIan Lepore DELAY(1); 19261bc42f7SIan Lepore } 19361bc42f7SIan Lepore } 19461bc42f7SIan Lepore 195831f5dcfSAlexander Motin /* Wait max 100 ms */ 19661bc42f7SIan Lepore timeout = 10000; 197831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 19861bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 19961bc42f7SIan Lepore if (timeout <= 0) { 20061bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 20161bc42f7SIan Lepore mask); 202831f5dcfSAlexander Motin sdhci_dumpregs(slot); 203831f5dcfSAlexander Motin return; 204831f5dcfSAlexander Motin } 205831f5dcfSAlexander Motin timeout--; 20661bc42f7SIan Lepore DELAY(10); 207831f5dcfSAlexander Motin } 208831f5dcfSAlexander Motin } 209831f5dcfSAlexander Motin 210831f5dcfSAlexander Motin static void 211831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 212831f5dcfSAlexander Motin { 213831f5dcfSAlexander Motin 214831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 215831f5dcfSAlexander Motin 216831f5dcfSAlexander Motin /* Enable interrupts. */ 217831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 218831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 219831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 220831f5dcfSAlexander Motin SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | 221831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 222831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 223831f5dcfSAlexander Motin SDHCI_INT_ACMD12ERR; 224831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 225831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 226831f5dcfSAlexander Motin } 227831f5dcfSAlexander Motin 228831f5dcfSAlexander Motin static void 229831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 230831f5dcfSAlexander Motin { 231831f5dcfSAlexander Motin uint32_t res; 232831f5dcfSAlexander Motin uint16_t clk; 2338f3b7d56SOleksandr Tymoshenko uint16_t div; 234831f5dcfSAlexander Motin int timeout; 235831f5dcfSAlexander Motin 236831f5dcfSAlexander Motin if (clock == slot->clock) 237831f5dcfSAlexander Motin return; 238831f5dcfSAlexander Motin slot->clock = clock; 239831f5dcfSAlexander Motin 240831f5dcfSAlexander Motin /* Turn off the clock. */ 2414ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 2424ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 243831f5dcfSAlexander Motin /* If no clock requested - left it so. */ 244831f5dcfSAlexander Motin if (clock == 0) 245831f5dcfSAlexander Motin return; 246ceb9e9f7SIan Lepore 247ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 248ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 249ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 250ceb9e9f7SIan Lepore 2518f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 252831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 253831f5dcfSAlexander Motin res = slot->max_clk; 25457677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 255831f5dcfSAlexander Motin if (res <= clock) 256831f5dcfSAlexander Motin break; 257831f5dcfSAlexander Motin res >>= 1; 258831f5dcfSAlexander Motin } 259831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 2608f3b7d56SOleksandr Tymoshenko div >>= 1; 2618f3b7d56SOleksandr Tymoshenko } 2628f3b7d56SOleksandr Tymoshenko else { 2638f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023*2 */ 26457677a3aSOleksandr Tymoshenko if (clock >= slot->max_clk) 26557677a3aSOleksandr Tymoshenko div = 0; 2668f3b7d56SOleksandr Tymoshenko else { 26757677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 2688f3b7d56SOleksandr Tymoshenko if ((slot->max_clk / div) <= clock) 2698f3b7d56SOleksandr Tymoshenko break; 2708f3b7d56SOleksandr Tymoshenko } 2718f3b7d56SOleksandr Tymoshenko } 2728f3b7d56SOleksandr Tymoshenko div >>= 1; 2738f3b7d56SOleksandr Tymoshenko } 2748f3b7d56SOleksandr Tymoshenko 2758f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 2768f3b7d56SOleksandr Tymoshenko slot_printf(slot, "Divider %d for freq %d (max %d)\n", 2778f3b7d56SOleksandr Tymoshenko div, clock, slot->max_clk); 2788f3b7d56SOleksandr Tymoshenko 279831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 2808f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 2818f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 2828f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 2838f3b7d56SOleksandr Tymoshenko 284831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 285831f5dcfSAlexander Motin /* Enable clock. */ 286831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 287831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 288831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 289831f5dcfSAlexander Motin timeout = 10; 290831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 291831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 292831f5dcfSAlexander Motin if (timeout == 0) { 293831f5dcfSAlexander Motin slot_printf(slot, 294831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 295831f5dcfSAlexander Motin sdhci_dumpregs(slot); 296831f5dcfSAlexander Motin return; 297831f5dcfSAlexander Motin } 298831f5dcfSAlexander Motin timeout--; 299831f5dcfSAlexander Motin DELAY(1000); 300831f5dcfSAlexander Motin } 301831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 302831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 303831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 304831f5dcfSAlexander Motin } 305831f5dcfSAlexander Motin 306831f5dcfSAlexander Motin static void 307831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 308831f5dcfSAlexander Motin { 309831f5dcfSAlexander Motin uint8_t pwr; 310831f5dcfSAlexander Motin 311831f5dcfSAlexander Motin if (slot->power == power) 312831f5dcfSAlexander Motin return; 313d6b3aaf8SOleksandr Tymoshenko 314831f5dcfSAlexander Motin slot->power = power; 315831f5dcfSAlexander Motin 316831f5dcfSAlexander Motin /* Turn off the power. */ 317831f5dcfSAlexander Motin pwr = 0; 318831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 319831f5dcfSAlexander Motin /* If power down requested - left it so. */ 320831f5dcfSAlexander Motin if (power == 0) 321831f5dcfSAlexander Motin return; 322831f5dcfSAlexander Motin /* Set voltage. */ 323831f5dcfSAlexander Motin switch (1 << power) { 324831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 325831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 326831f5dcfSAlexander Motin break; 327831f5dcfSAlexander Motin case MMC_OCR_290_300: 328831f5dcfSAlexander Motin case MMC_OCR_300_310: 329831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 330831f5dcfSAlexander Motin break; 331831f5dcfSAlexander Motin case MMC_OCR_320_330: 332831f5dcfSAlexander Motin case MMC_OCR_330_340: 333831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 334831f5dcfSAlexander Motin break; 335831f5dcfSAlexander Motin } 336831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 337831f5dcfSAlexander Motin /* Turn on the power. */ 338831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 339831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 340831f5dcfSAlexander Motin } 341831f5dcfSAlexander Motin 342831f5dcfSAlexander Motin static void 343831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 344831f5dcfSAlexander Motin { 345831f5dcfSAlexander Motin uint32_t data; 346831f5dcfSAlexander Motin char *buffer; 347831f5dcfSAlexander Motin size_t left; 348831f5dcfSAlexander Motin 349831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 350831f5dcfSAlexander Motin buffer += slot->offset; 351831f5dcfSAlexander Motin /* Transfer one block at a time. */ 352831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 353831f5dcfSAlexander Motin slot->offset += left; 354831f5dcfSAlexander Motin 355831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 356d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 357831f5dcfSAlexander Motin DELAY(10); 358ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 359831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 360831f5dcfSAlexander Motin while (left > 3) { 361831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 362831f5dcfSAlexander Motin buffer[0] = data; 363831f5dcfSAlexander Motin buffer[1] = (data >> 8); 364831f5dcfSAlexander Motin buffer[2] = (data >> 16); 365831f5dcfSAlexander Motin buffer[3] = (data >> 24); 366831f5dcfSAlexander Motin buffer += 4; 367831f5dcfSAlexander Motin left -= 4; 368831f5dcfSAlexander Motin } 369831f5dcfSAlexander Motin } else { 370d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 371831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 372831f5dcfSAlexander Motin left &= 3; 373831f5dcfSAlexander Motin } 374831f5dcfSAlexander Motin /* Handle uneven size case. */ 375831f5dcfSAlexander Motin if (left > 0) { 376831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 377831f5dcfSAlexander Motin while (left > 0) { 378831f5dcfSAlexander Motin *(buffer++) = data; 379831f5dcfSAlexander Motin data >>= 8; 380831f5dcfSAlexander Motin left--; 381831f5dcfSAlexander Motin } 382831f5dcfSAlexander Motin } 383831f5dcfSAlexander Motin } 384831f5dcfSAlexander Motin 385831f5dcfSAlexander Motin static void 386831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 387831f5dcfSAlexander Motin { 388831f5dcfSAlexander Motin uint32_t data = 0; 389831f5dcfSAlexander Motin char *buffer; 390831f5dcfSAlexander Motin size_t left; 391831f5dcfSAlexander Motin 392831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 393831f5dcfSAlexander Motin buffer += slot->offset; 394831f5dcfSAlexander Motin /* Transfer one block at a time. */ 395831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 396831f5dcfSAlexander Motin slot->offset += left; 397831f5dcfSAlexander Motin 398ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 399831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 400831f5dcfSAlexander Motin while (left > 3) { 401831f5dcfSAlexander Motin data = buffer[0] + 402831f5dcfSAlexander Motin (buffer[1] << 8) + 403831f5dcfSAlexander Motin (buffer[2] << 16) + 404831f5dcfSAlexander Motin (buffer[3] << 24); 405831f5dcfSAlexander Motin left -= 4; 406831f5dcfSAlexander Motin buffer += 4; 407831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 408831f5dcfSAlexander Motin } 409831f5dcfSAlexander Motin } else { 410d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 411831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 412831f5dcfSAlexander Motin left &= 3; 413831f5dcfSAlexander Motin } 414831f5dcfSAlexander Motin /* Handle uneven size case. */ 415831f5dcfSAlexander Motin if (left > 0) { 416831f5dcfSAlexander Motin while (left > 0) { 417831f5dcfSAlexander Motin data <<= 8; 418831f5dcfSAlexander Motin data += *(buffer++); 419831f5dcfSAlexander Motin left--; 420831f5dcfSAlexander Motin } 421831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 422831f5dcfSAlexander Motin } 423831f5dcfSAlexander Motin } 424831f5dcfSAlexander Motin 425831f5dcfSAlexander Motin static void 426831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 427831f5dcfSAlexander Motin { 428831f5dcfSAlexander Motin 429831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 430831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 431831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 432831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 433831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 434831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 435831f5dcfSAlexander Motin break; 436831f5dcfSAlexander Motin } 437831f5dcfSAlexander Motin } else { 438831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 439831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 440831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 441831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 442831f5dcfSAlexander Motin break; 443831f5dcfSAlexander Motin } 444831f5dcfSAlexander Motin } 445831f5dcfSAlexander Motin } 446831f5dcfSAlexander Motin 447831f5dcfSAlexander Motin static void 448831f5dcfSAlexander Motin sdhci_card_delay(void *arg) 449831f5dcfSAlexander Motin { 450831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 451831f5dcfSAlexander Motin 452831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 453831f5dcfSAlexander Motin } 454831f5dcfSAlexander Motin 455831f5dcfSAlexander Motin static void 456831f5dcfSAlexander Motin sdhci_card_task(void *arg, int pending) 457831f5dcfSAlexander Motin { 458831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 459831f5dcfSAlexander Motin 460831f5dcfSAlexander Motin SDHCI_LOCK(slot); 461831f5dcfSAlexander Motin if (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) { 462831f5dcfSAlexander Motin if (slot->dev == NULL) { 463831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 464d6b3aaf8SOleksandr Tymoshenko slot->dev = device_add_child(slot->bus, "mmc", -1); 465831f5dcfSAlexander Motin device_set_ivars(slot->dev, slot); 466831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 467831f5dcfSAlexander Motin device_probe_and_attach(slot->dev); 468831f5dcfSAlexander Motin } else 469831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 470831f5dcfSAlexander Motin } else { 471831f5dcfSAlexander Motin if (slot->dev != NULL) { 472831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 473831f5dcfSAlexander Motin device_t d = slot->dev; 474831f5dcfSAlexander Motin slot->dev = NULL; 475831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 476d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 477831f5dcfSAlexander Motin } else 478831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 479831f5dcfSAlexander Motin } 480831f5dcfSAlexander Motin } 481831f5dcfSAlexander Motin 482d6b3aaf8SOleksandr Tymoshenko int 483d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 484831f5dcfSAlexander Motin { 48587a6a871SIan Lepore uint32_t caps, freq; 486d6b3aaf8SOleksandr Tymoshenko int err; 487831f5dcfSAlexander Motin 488831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 489d6b3aaf8SOleksandr Tymoshenko slot->num = num; 490d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 491d6b3aaf8SOleksandr Tymoshenko 492831f5dcfSAlexander Motin /* Allocate DMA tag. */ 493831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 494831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 495831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 496831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 497831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 498831f5dcfSAlexander Motin &slot->dmatag); 499831f5dcfSAlexander Motin if (err != 0) { 500831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 501831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 502d6b3aaf8SOleksandr Tymoshenko return (err); 503831f5dcfSAlexander Motin } 504831f5dcfSAlexander Motin /* Allocate DMA memory. */ 505831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 506831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 507831f5dcfSAlexander Motin if (err != 0) { 508831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 509831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 510d6b3aaf8SOleksandr Tymoshenko return (err); 511831f5dcfSAlexander Motin } 512831f5dcfSAlexander Motin /* Map the memory. */ 513831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 514831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 515831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 516831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 517831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 518831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 519d6b3aaf8SOleksandr Tymoshenko if(err) 520d6b3aaf8SOleksandr Tymoshenko return (err); 521d6b3aaf8SOleksandr Tymoshenko else 522d6b3aaf8SOleksandr Tymoshenko return (EFAULT); 523831f5dcfSAlexander Motin } 524d6b3aaf8SOleksandr Tymoshenko 525831f5dcfSAlexander Motin /* Initialize slot. */ 526831f5dcfSAlexander Motin sdhci_init(slot); 527d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 528d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 5298f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) 5308f3b7d56SOleksandr Tymoshenko caps = slot->caps; 5318f3b7d56SOleksandr Tymoshenko else 532831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 533831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 53433aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 53587a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 53687a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 53733aad34dSOleksandr Tymoshenko else 53887a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 53987a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 54087a6a871SIan Lepore if (freq != 0) 54187a6a871SIan Lepore slot->max_clk = freq * 1000000; 54287a6a871SIan Lepore /* 54387a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 54487a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 54587a6a871SIan Lepore * with an assumption, so complain about it. 54687a6a871SIan Lepore */ 547831f5dcfSAlexander Motin if (slot->max_clk == 0) { 54887a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 549831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 55033aad34dSOleksandr Tymoshenko "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ); 551831f5dcfSAlexander Motin } 552831f5dcfSAlexander Motin /* Calculate timeout clock frequency. */ 5538f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 5548f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 5558f3b7d56SOleksandr Tymoshenko } else { 556831f5dcfSAlexander Motin slot->timeout_clk = 557831f5dcfSAlexander Motin (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; 5588f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 5598f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 5608f3b7d56SOleksandr Tymoshenko } 56187a6a871SIan Lepore /* 56287a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 56387a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 56487a6a871SIan Lepore * max timeout, but still mention it. 56587a6a871SIan Lepore */ 566831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 567831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 568ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 569ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 570831f5dcfSAlexander Motin } 571831f5dcfSAlexander Motin 57257677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 573831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 574831f5dcfSAlexander Motin slot->host.host_ocr = 0; 575831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 576831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 577831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 578831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 579831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_180) 580831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 581831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 582831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 583831f5dcfSAlexander Motin "support voltages.\n"); 584831f5dcfSAlexander Motin } 585831f5dcfSAlexander Motin slot->host.caps = MMC_CAP_4_BIT_DATA; 5862d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 5872d1731b8SIan Lepore slot->host.caps |= MMC_CAP_8_BIT_DATA; 588831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 589831f5dcfSAlexander Motin slot->host.caps |= MMC_CAP_HSPEED; 590831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 591831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 592831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 593d6b3aaf8SOleksandr Tymoshenko 594d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 595831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 596d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 597831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 598831f5dcfSAlexander Motin 599c3a0f75aSOleksandr Tymoshenko /* 600c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 601c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 602c3a0f75aSOleksandr Tymoshenko */ 603c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 604c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 605c3a0f75aSOleksandr Tymoshenko 6065b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 6072d1731b8SIan Lepore slot_printf(slot, "%uMHz%s %s%s%s%s %s\n", 608831f5dcfSAlexander Motin slot->max_clk / 1000000, 609831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 6102d1731b8SIan Lepore (caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 6112d1731b8SIan Lepore ((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 612831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 613831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 614831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 615831f5dcfSAlexander Motin (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 616831f5dcfSAlexander Motin sdhci_dumpregs(slot); 617831f5dcfSAlexander Motin } 618831f5dcfSAlexander Motin 619*ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 620*ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 621*ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 622*ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 623*ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 624831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 625831f5dcfSAlexander Motin callout_init(&slot->card_callout, 1); 626e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 627*ba6fc1c7SLuiz Otavio O Souza 628831f5dcfSAlexander Motin return (0); 629831f5dcfSAlexander Motin } 630831f5dcfSAlexander Motin 631d6b3aaf8SOleksandr Tymoshenko void 632d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 633831f5dcfSAlexander Motin { 634d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 635d6b3aaf8SOleksandr Tymoshenko } 636831f5dcfSAlexander Motin 637d6b3aaf8SOleksandr Tymoshenko int 638d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 639d6b3aaf8SOleksandr Tymoshenko { 640831f5dcfSAlexander Motin device_t d; 641831f5dcfSAlexander Motin 642e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 643831f5dcfSAlexander Motin callout_drain(&slot->card_callout); 644831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 645831f5dcfSAlexander Motin 646831f5dcfSAlexander Motin SDHCI_LOCK(slot); 647831f5dcfSAlexander Motin d = slot->dev; 648831f5dcfSAlexander Motin slot->dev = NULL; 649831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 650831f5dcfSAlexander Motin if (d != NULL) 651d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 652831f5dcfSAlexander Motin 653831f5dcfSAlexander Motin SDHCI_LOCK(slot); 654831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 655831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 656831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 657831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 658831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 659d6b3aaf8SOleksandr Tymoshenko 660831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 661d6b3aaf8SOleksandr Tymoshenko 662831f5dcfSAlexander Motin return (0); 663831f5dcfSAlexander Motin } 664831f5dcfSAlexander Motin 665d6b3aaf8SOleksandr Tymoshenko int 666d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 66792bf0e27SAlexander Motin { 668d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 66992bf0e27SAlexander Motin 67092bf0e27SAlexander Motin return (0); 67192bf0e27SAlexander Motin } 67292bf0e27SAlexander Motin 673d6b3aaf8SOleksandr Tymoshenko int 674d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 67592bf0e27SAlexander Motin { 676d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 67792bf0e27SAlexander Motin 678d6b3aaf8SOleksandr Tymoshenko return (0); 67992bf0e27SAlexander Motin } 68092bf0e27SAlexander Motin 68157677a3aSOleksandr Tymoshenko uint32_t 68257677a3aSOleksandr Tymoshenko sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot) 68357677a3aSOleksandr Tymoshenko { 68457677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 68557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 68657677a3aSOleksandr Tymoshenko else 68757677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 68857677a3aSOleksandr Tymoshenko } 68957677a3aSOleksandr Tymoshenko 690d6b3aaf8SOleksandr Tymoshenko int 691d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 692831f5dcfSAlexander Motin { 693831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 694831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 695831f5dcfSAlexander Motin 696831f5dcfSAlexander Motin SDHCI_LOCK(slot); 697831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 698831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 699831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 700831f5dcfSAlexander Motin sdhci_init(slot); 701831f5dcfSAlexander Motin } 702831f5dcfSAlexander Motin /* Configure the bus. */ 703831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 704831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 7052d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 7062d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 707831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 7082d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 7092d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 7102d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 7112d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 7122d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 7132d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 7142d1731b8SIan Lepore } else { 7152d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 7162d1731b8SIan Lepore } 717bba987dcSIan Lepore if (ios->timing == bus_timing_hs && 718bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 719831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 720831f5dcfSAlexander Motin else 721831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 722831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 723831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 724d6b3aaf8SOleksandr Tymoshenko if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 725831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 726831f5dcfSAlexander Motin 727831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 728831f5dcfSAlexander Motin return (0); 729831f5dcfSAlexander Motin } 730831f5dcfSAlexander Motin 731831f5dcfSAlexander Motin static void 732e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 733e64f01a9SIan Lepore { 734e64f01a9SIan Lepore struct mmc_request *req; 735e64f01a9SIan Lepore 736e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 737e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 738e64f01a9SIan Lepore req = slot->req; 739e64f01a9SIan Lepore slot->req = NULL; 740e64f01a9SIan Lepore slot->curcmd = NULL; 741e64f01a9SIan Lepore req->done(req); 742e64f01a9SIan Lepore } 743e64f01a9SIan Lepore } 744e64f01a9SIan Lepore 745e64f01a9SIan Lepore static void 746e64f01a9SIan Lepore sdhci_timeout(void *arg) 747e64f01a9SIan Lepore { 748e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 749e64f01a9SIan Lepore 750e64f01a9SIan Lepore if (slot->curcmd != NULL) { 7517e586643SIan Lepore slot_printf(slot, " Controller timeout\n"); 7527e586643SIan Lepore sdhci_dumpregs(slot); 753a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA); 754e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 755e64f01a9SIan Lepore sdhci_req_done(slot); 7567e586643SIan Lepore } else { 7577e586643SIan Lepore slot_printf(slot, " Spurious timeout - no active command\n"); 758e64f01a9SIan Lepore } 759e64f01a9SIan Lepore } 760e64f01a9SIan Lepore 761e64f01a9SIan Lepore static void 762831f5dcfSAlexander Motin sdhci_set_transfer_mode(struct sdhci_slot *slot, 763831f5dcfSAlexander Motin struct mmc_data *data) 764831f5dcfSAlexander Motin { 765831f5dcfSAlexander Motin uint16_t mode; 766831f5dcfSAlexander Motin 767831f5dcfSAlexander Motin if (data == NULL) 768831f5dcfSAlexander Motin return; 769831f5dcfSAlexander Motin 770831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 771831f5dcfSAlexander Motin if (data->len > 512) 772831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 773831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 774831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 775831f5dcfSAlexander Motin if (slot->req->stop) 776831f5dcfSAlexander Motin mode |= SDHCI_TRNS_ACMD12; 777831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 778831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 779831f5dcfSAlexander Motin 780831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 781831f5dcfSAlexander Motin } 782831f5dcfSAlexander Motin 783831f5dcfSAlexander Motin static void 784831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 785831f5dcfSAlexander Motin { 786831f5dcfSAlexander Motin int flags, timeout; 787831f5dcfSAlexander Motin uint32_t mask, state; 788831f5dcfSAlexander Motin 789831f5dcfSAlexander Motin slot->curcmd = cmd; 790831f5dcfSAlexander Motin slot->cmd_done = 0; 791831f5dcfSAlexander Motin 792831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 793831f5dcfSAlexander Motin 794831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 795831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 796831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 797831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 798e64f01a9SIan Lepore sdhci_req_done(slot); 799831f5dcfSAlexander Motin return; 800831f5dcfSAlexander Motin } 801831f5dcfSAlexander Motin 802831f5dcfSAlexander Motin /* Read controller present state. */ 803831f5dcfSAlexander Motin state = RD4(slot, SDHCI_PRESENT_STATE); 804d8208d9eSAlexander Motin /* Do not issue command if there is no card, clock or power. 805d8208d9eSAlexander Motin * Controller will not detect timeout without clock active. */ 806d8208d9eSAlexander Motin if ((state & SDHCI_CARD_PRESENT) == 0 || 807d8208d9eSAlexander Motin slot->power == 0 || 808d8208d9eSAlexander Motin slot->clock == 0) { 809831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 810e64f01a9SIan Lepore sdhci_req_done(slot); 811831f5dcfSAlexander Motin return; 812831f5dcfSAlexander Motin } 813831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 814831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 815831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 816831f5dcfSAlexander Motin if (cmd->data || (cmd->flags & MMC_RSP_BUSY)) 817831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 818831f5dcfSAlexander Motin /* We shouldn't wait for DAT for stop commands. */ 819831f5dcfSAlexander Motin if (cmd == slot->req->stop) 820831f5dcfSAlexander Motin mask &= ~SDHCI_DAT_INHIBIT; 8218775ab45SIan Lepore /* 8228775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 8238775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 8248775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 8258775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 8268775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 8278775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 8288775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 8298775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 8308775ab45SIan Lepore */ 8318775ab45SIan Lepore timeout = 250; 832831f5dcfSAlexander Motin while (state & mask) { 833831f5dcfSAlexander Motin if (timeout == 0) { 834831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 835831f5dcfSAlexander Motin "inhibit bit(s).\n"); 836831f5dcfSAlexander Motin sdhci_dumpregs(slot); 837831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 838e64f01a9SIan Lepore sdhci_req_done(slot); 839831f5dcfSAlexander Motin return; 840831f5dcfSAlexander Motin } 841831f5dcfSAlexander Motin timeout--; 842831f5dcfSAlexander Motin DELAY(1000); 843831f5dcfSAlexander Motin state = RD4(slot, SDHCI_PRESENT_STATE); 844831f5dcfSAlexander Motin } 845831f5dcfSAlexander Motin 846831f5dcfSAlexander Motin /* Prepare command flags. */ 847831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 848831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 849831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 850831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 851831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 852831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 853831f5dcfSAlexander Motin else 854831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 855831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 856831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 857831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 858831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 859831f5dcfSAlexander Motin if (cmd->data) 860831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 861831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 862831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 863831f5dcfSAlexander Motin /* Prepare data. */ 864831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 865831f5dcfSAlexander Motin /* 866831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 867831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 868831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 869831f5dcfSAlexander Motin */ 870831f5dcfSAlexander Motin if (slot->data_done == 0) { 871831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 872831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 873831f5dcfSAlexander Motin } 874831f5dcfSAlexander Motin /* Set command argument. */ 875831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 876831f5dcfSAlexander Motin /* Set data transfer mode. */ 877831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 878831f5dcfSAlexander Motin /* Start command. */ 879d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 880a6873fd1SIan Lepore /* Start timeout callout. */ 881*ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 882*ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 883831f5dcfSAlexander Motin } 884831f5dcfSAlexander Motin 885831f5dcfSAlexander Motin static void 886831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 887831f5dcfSAlexander Motin { 888831f5dcfSAlexander Motin int i; 889831f5dcfSAlexander Motin 890831f5dcfSAlexander Motin slot->cmd_done = 1; 891831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 892831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 893831f5dcfSAlexander Motin * happened first. */ 894831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 895831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 896831f5dcfSAlexander Motin if (slot->curcmd->error) { 897831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 898831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 899831f5dcfSAlexander Motin sdhci_start(slot); 900831f5dcfSAlexander Motin return; 901831f5dcfSAlexander Motin } 902831f5dcfSAlexander Motin /* If command has response - fetch it. */ 903831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 904831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 905831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 906831f5dcfSAlexander Motin uint8_t extra = 0; 907831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 908831f5dcfSAlexander Motin uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4); 909677ee494SIan Lepore if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 910677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 911677ee494SIan Lepore else { 912677ee494SIan Lepore slot->curcmd->resp[3 - i] = 913677ee494SIan Lepore (val << 8) | extra; 914831f5dcfSAlexander Motin extra = val >> 24; 915831f5dcfSAlexander Motin } 916677ee494SIan Lepore } 917831f5dcfSAlexander Motin } else 918831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 919831f5dcfSAlexander Motin } 920831f5dcfSAlexander Motin /* If data ready - finish. */ 921831f5dcfSAlexander Motin if (slot->data_done) 922831f5dcfSAlexander Motin sdhci_start(slot); 923831f5dcfSAlexander Motin } 924831f5dcfSAlexander Motin 925831f5dcfSAlexander Motin static void 926831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 927831f5dcfSAlexander Motin { 928831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 929831f5dcfSAlexander Motin uint8_t div; 930831f5dcfSAlexander Motin 931831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 932831f5dcfSAlexander Motin slot->data_done = 1; 933831f5dcfSAlexander Motin return; 934831f5dcfSAlexander Motin } 935831f5dcfSAlexander Motin 936831f5dcfSAlexander Motin slot->data_done = 0; 937831f5dcfSAlexander Motin 938831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 939831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 940ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 941ceb9e9f7SIan Lepore div = 0xE; 942ceb9e9f7SIan Lepore } else { 943831f5dcfSAlexander Motin target_timeout = 1000000; 944831f5dcfSAlexander Motin div = 0; 945831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 946ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 947ceb9e9f7SIan Lepore ++div; 948831f5dcfSAlexander Motin current_timeout <<= 1; 949831f5dcfSAlexander Motin } 950831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 951ceb9e9f7SIan Lepore if (div < 0xE && 952ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 953ceb9e9f7SIan Lepore ++div; 954831f5dcfSAlexander Motin } 955ceb9e9f7SIan Lepore } 956831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 957831f5dcfSAlexander Motin 958831f5dcfSAlexander Motin if (data == NULL) 959831f5dcfSAlexander Motin return; 960831f5dcfSAlexander Motin 961831f5dcfSAlexander Motin /* Use DMA if possible. */ 962831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 963831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 964831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 965d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 966831f5dcfSAlexander Motin (data->len <= 512)) 967831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 968831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 969d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 970831f5dcfSAlexander Motin ((data->len) & 0x3)) 971831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 972831f5dcfSAlexander Motin /* Load DMA buffer. */ 973831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 974831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 975ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 976ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 977831f5dcfSAlexander Motin else { 978831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 979ecc2d997SRui Paulo (data->len < DMA_BLOCK_SIZE) ? 980ecc2d997SRui Paulo data->len : DMA_BLOCK_SIZE); 981ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 982ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 983831f5dcfSAlexander Motin } 984831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 985831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 986831f5dcfSAlexander Motin * for the last page and unmask else. */ 987831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 988831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 989831f5dcfSAlexander Motin else 990831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 991831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 992831f5dcfSAlexander Motin } 993831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 994831f5dcfSAlexander Motin slot->offset = 0; 995831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 996831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_SIZE, 997831f5dcfSAlexander Motin SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512)); 998831f5dcfSAlexander Motin /* Set block count. */ 999831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1000831f5dcfSAlexander Motin } 1001831f5dcfSAlexander Motin 1002c3a0f75aSOleksandr Tymoshenko void 1003831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1004831f5dcfSAlexander Motin { 1005831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1006831f5dcfSAlexander Motin 1007831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1008ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1009831f5dcfSAlexander Motin * happened first. */ 1010831f5dcfSAlexander Motin if (!slot->cmd_done) { 1011831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1012831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1013831f5dcfSAlexander Motin } 1014831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1015a98788edSIan Lepore if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) { 1016831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1017831f5dcfSAlexander Motin size_t left = data->len - slot->offset; 1018ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1019ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1020831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1021831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1022831f5dcfSAlexander Motin } else 1023ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1024ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1025831f5dcfSAlexander Motin } 1026a98788edSIan Lepore slot->data_done = 1; 1027831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1028831f5dcfSAlexander Motin if (slot->curcmd->error) { 1029831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1030831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1031831f5dcfSAlexander Motin sdhci_start(slot); 1032831f5dcfSAlexander Motin return; 1033831f5dcfSAlexander Motin } 1034831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1035831f5dcfSAlexander Motin if (slot->cmd_done) 1036831f5dcfSAlexander Motin sdhci_start(slot); 1037831f5dcfSAlexander Motin } 1038831f5dcfSAlexander Motin 1039831f5dcfSAlexander Motin static void 1040831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1041831f5dcfSAlexander Motin { 1042831f5dcfSAlexander Motin struct mmc_request *req; 1043831f5dcfSAlexander Motin 1044831f5dcfSAlexander Motin req = slot->req; 1045831f5dcfSAlexander Motin if (req == NULL) 1046831f5dcfSAlexander Motin return; 1047831f5dcfSAlexander Motin 1048831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1049831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1050831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1051831f5dcfSAlexander Motin return; 1052831f5dcfSAlexander Motin } 1053831f5dcfSAlexander Motin /* We don't need this until using Auto-CMD12 feature 1054831f5dcfSAlexander Motin if (!(slot->flags & STOP_STARTED) && req->stop) { 1055831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1056831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1057831f5dcfSAlexander Motin return; 1058831f5dcfSAlexander Motin } 1059831f5dcfSAlexander Motin */ 10605b69a497SAlexander Motin if (sdhci_debug > 1) 10615b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 10625b69a497SAlexander Motin if (!req->cmd->error && 1063d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1064831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1065831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1066831f5dcfSAlexander Motin } 1067831f5dcfSAlexander Motin 1068e64f01a9SIan Lepore sdhci_req_done(slot); 1069831f5dcfSAlexander Motin } 1070831f5dcfSAlexander Motin 1071d6b3aaf8SOleksandr Tymoshenko int 1072d6b3aaf8SOleksandr Tymoshenko sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req) 1073831f5dcfSAlexander Motin { 1074831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1075831f5dcfSAlexander Motin 1076831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1077831f5dcfSAlexander Motin if (slot->req != NULL) { 1078831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1079831f5dcfSAlexander Motin return (EBUSY); 1080831f5dcfSAlexander Motin } 10815b69a497SAlexander Motin if (sdhci_debug > 1) { 10825b69a497SAlexander Motin slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1083831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 10845b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 10855b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 10865b69a497SAlexander Motin } 1087831f5dcfSAlexander Motin slot->req = req; 1088831f5dcfSAlexander Motin slot->flags = 0; 1089831f5dcfSAlexander Motin sdhci_start(slot); 1090831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1091bea2dca2SAlexander Motin if (dumping) { 1092bea2dca2SAlexander Motin while (slot->req != NULL) { 1093d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 1094bea2dca2SAlexander Motin DELAY(10); 1095bea2dca2SAlexander Motin } 1096bea2dca2SAlexander Motin } 1097831f5dcfSAlexander Motin return (0); 1098831f5dcfSAlexander Motin } 1099831f5dcfSAlexander Motin 1100d6b3aaf8SOleksandr Tymoshenko int 1101d6b3aaf8SOleksandr Tymoshenko sdhci_generic_get_ro(device_t brdev, device_t reqdev) 1102831f5dcfSAlexander Motin { 1103831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1104831f5dcfSAlexander Motin uint32_t val; 1105831f5dcfSAlexander Motin 1106831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1107831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 1108831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1109831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 1110831f5dcfSAlexander Motin } 1111831f5dcfSAlexander Motin 1112d6b3aaf8SOleksandr Tymoshenko int 1113d6b3aaf8SOleksandr Tymoshenko sdhci_generic_acquire_host(device_t brdev, device_t reqdev) 1114831f5dcfSAlexander Motin { 1115831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1116831f5dcfSAlexander Motin int err = 0; 1117831f5dcfSAlexander Motin 1118831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1119831f5dcfSAlexander Motin while (slot->bus_busy) 1120d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1121831f5dcfSAlexander Motin slot->bus_busy++; 1122831f5dcfSAlexander Motin /* Activate led. */ 1123831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1124831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1125831f5dcfSAlexander Motin return (err); 1126831f5dcfSAlexander Motin } 1127831f5dcfSAlexander Motin 1128d6b3aaf8SOleksandr Tymoshenko int 1129d6b3aaf8SOleksandr Tymoshenko sdhci_generic_release_host(device_t brdev, device_t reqdev) 1130831f5dcfSAlexander Motin { 1131831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1132831f5dcfSAlexander Motin 1133831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1134831f5dcfSAlexander Motin /* Deactivate led. */ 1135831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1136831f5dcfSAlexander Motin slot->bus_busy--; 1137831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1138d493985aSAlexander Motin wakeup(slot); 1139831f5dcfSAlexander Motin return (0); 1140831f5dcfSAlexander Motin } 1141831f5dcfSAlexander Motin 1142831f5dcfSAlexander Motin static void 1143831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1144831f5dcfSAlexander Motin { 1145831f5dcfSAlexander Motin 1146831f5dcfSAlexander Motin if (!slot->curcmd) { 1147831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 1148831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1149831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1150831f5dcfSAlexander Motin return; 1151831f5dcfSAlexander Motin } 1152831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 1153831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1154831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 1155831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1156831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1157831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 1158831f5dcfSAlexander Motin 1159831f5dcfSAlexander Motin sdhci_finish_command(slot); 1160831f5dcfSAlexander Motin } 1161831f5dcfSAlexander Motin 1162831f5dcfSAlexander Motin static void 1163831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1164831f5dcfSAlexander Motin { 1165831f5dcfSAlexander Motin 1166831f5dcfSAlexander Motin if (!slot->curcmd) { 1167831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1168831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1169831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1170831f5dcfSAlexander Motin return; 1171831f5dcfSAlexander Motin } 1172831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1173831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1174831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1175831f5dcfSAlexander Motin "there is no active data operation.\n", 1176831f5dcfSAlexander Motin intmask); 1177831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1178831f5dcfSAlexander Motin return; 1179831f5dcfSAlexander Motin } 1180831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 1181831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1182acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1183831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1184831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1185831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1186831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 1187831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1188831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 1189831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1190831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 1191831f5dcfSAlexander Motin } 1192831f5dcfSAlexander Motin if (slot->curcmd->error) { 1193831f5dcfSAlexander Motin /* No need to continue after any error. */ 1194a98788edSIan Lepore goto done; 1195831f5dcfSAlexander Motin } 1196831f5dcfSAlexander Motin 1197831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 1198c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 1199c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 1200c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 1201c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask); 1202c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 1203c3a0f75aSOleksandr Tymoshenko } else 1204831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 1205c3a0f75aSOleksandr Tymoshenko } 1206831f5dcfSAlexander Motin /* Handle DMA border. */ 1207831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 1208831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1209831f5dcfSAlexander Motin size_t left; 1210831f5dcfSAlexander Motin 1211831f5dcfSAlexander Motin /* Unload DMA buffer... */ 1212831f5dcfSAlexander Motin left = data->len - slot->offset; 1213831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1214831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1215831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 1216831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1217831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1218831f5dcfSAlexander Motin } else { 1219831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1220831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 1221831f5dcfSAlexander Motin } 1222831f5dcfSAlexander Motin /* ... and reload it again. */ 1223831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 1224831f5dcfSAlexander Motin left = data->len - slot->offset; 1225831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1226831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1227831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 1228831f5dcfSAlexander Motin } else { 1229831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1230831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1231831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1232831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 1233831f5dcfSAlexander Motin } 1234831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1235831f5dcfSAlexander Motin * for the last page. */ 1236831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 1237831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1238831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1239831f5dcfSAlexander Motin } 1240831f5dcfSAlexander Motin /* Restart DMA. */ 1241831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1242831f5dcfSAlexander Motin } 1243831f5dcfSAlexander Motin /* We have got all data. */ 1244c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 1245c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 1246c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 1247c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1248c3a0f75aSOleksandr Tymoshenko } else 1249831f5dcfSAlexander Motin sdhci_finish_data(slot); 1250831f5dcfSAlexander Motin } 1251a98788edSIan Lepore done: 1252a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 1253a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 1254a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 1255a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1256a98788edSIan Lepore } else 1257a98788edSIan Lepore sdhci_finish_data(slot); 1258a98788edSIan Lepore return; 1259a98788edSIan Lepore } 1260c3a0f75aSOleksandr Tymoshenko } 1261831f5dcfSAlexander Motin 1262831f5dcfSAlexander Motin static void 1263831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot) 1264831f5dcfSAlexander Motin { 1265831f5dcfSAlexander Motin uint16_t err; 1266831f5dcfSAlexander Motin 1267831f5dcfSAlexander Motin err = RD4(slot, SDHCI_ACMD12_ERR); 1268831f5dcfSAlexander Motin if (!slot->curcmd) { 1269831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1270831f5dcfSAlexander Motin "there is no active command.\n", err); 1271831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1272831f5dcfSAlexander Motin return; 1273831f5dcfSAlexander Motin } 1274831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1275831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1276831f5dcfSAlexander Motin } 1277831f5dcfSAlexander Motin 1278d6b3aaf8SOleksandr Tymoshenko void 1279d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 1280831f5dcfSAlexander Motin { 1281831f5dcfSAlexander Motin uint32_t intmask; 1282831f5dcfSAlexander Motin 1283831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1284831f5dcfSAlexander Motin /* Read slot interrupt status. */ 1285831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 1286831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 1287831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1288d6b3aaf8SOleksandr Tymoshenko return; 1289831f5dcfSAlexander Motin } 12905b69a497SAlexander Motin if (sdhci_debug > 2) 12915b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 12925b69a497SAlexander Motin 1293831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 1294831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 1295831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 1296831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1297831f5dcfSAlexander Motin 1298831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_REMOVE) { 12995b69a497SAlexander Motin if (bootverbose || sdhci_debug) 1300831f5dcfSAlexander Motin slot_printf(slot, "Card removed\n"); 1301831f5dcfSAlexander Motin callout_stop(&slot->card_callout); 1302831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, 1303831f5dcfSAlexander Motin &slot->card_task); 1304831f5dcfSAlexander Motin } 1305831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_INSERT) { 13065b69a497SAlexander Motin if (bootverbose || sdhci_debug) 1307831f5dcfSAlexander Motin slot_printf(slot, "Card inserted\n"); 1308831f5dcfSAlexander Motin callout_reset(&slot->card_callout, hz / 2, 1309831f5dcfSAlexander Motin sdhci_card_delay, slot); 1310831f5dcfSAlexander Motin } 1311831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1312831f5dcfSAlexander Motin } 1313831f5dcfSAlexander Motin /* Handle command interrupts. */ 1314831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 1315831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1316831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1317831f5dcfSAlexander Motin } 1318831f5dcfSAlexander Motin /* Handle data interrupts. */ 1319831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 1320831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 13217e586643SIan Lepore /* Dont call data_irq in case of errored command */ 13227e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 1323831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1324831f5dcfSAlexander Motin } 1325831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 1326831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 1327831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1328831f5dcfSAlexander Motin sdhci_acmd_irq(slot); 1329831f5dcfSAlexander Motin } 1330831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1331831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ACMD12ERR; 1332831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ERROR; 1333831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 1334831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 1335831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1336831f5dcfSAlexander Motin slot_printf(slot, 1337831f5dcfSAlexander Motin "Card is consuming too much power!\n"); 1338831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_BUS_POWER; 1339831f5dcfSAlexander Motin } 1340831f5dcfSAlexander Motin /* The rest is unknown. */ 1341831f5dcfSAlexander Motin if (intmask) { 1342831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 1343831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1344831f5dcfSAlexander Motin intmask); 1345831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1346831f5dcfSAlexander Motin } 1347831f5dcfSAlexander Motin 1348831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1349831f5dcfSAlexander Motin } 1350831f5dcfSAlexander Motin 1351d6b3aaf8SOleksandr Tymoshenko int 1352d6b3aaf8SOleksandr Tymoshenko sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1353831f5dcfSAlexander Motin { 1354831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1355831f5dcfSAlexander Motin 1356831f5dcfSAlexander Motin switch (which) { 1357831f5dcfSAlexander Motin default: 1358831f5dcfSAlexander Motin return (EINVAL); 1359831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1360bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 1361831f5dcfSAlexander Motin break; 1362831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1363bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 1364831f5dcfSAlexander Motin break; 1365831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1366bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 1367831f5dcfSAlexander Motin break; 1368831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1369bcd91d25SJayachandran C. *result = slot->host.ios.clock; 1370831f5dcfSAlexander Motin break; 1371831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1372bcd91d25SJayachandran C. *result = slot->host.f_min; 1373831f5dcfSAlexander Motin break; 1374831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 1375bcd91d25SJayachandran C. *result = slot->host.f_max; 1376831f5dcfSAlexander Motin break; 1377831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1378bcd91d25SJayachandran C. *result = slot->host.host_ocr; 1379831f5dcfSAlexander Motin break; 1380831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1381bcd91d25SJayachandran C. *result = slot->host.mode; 1382831f5dcfSAlexander Motin break; 1383831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1384bcd91d25SJayachandran C. *result = slot->host.ocr; 1385831f5dcfSAlexander Motin break; 1386831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1387bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 1388831f5dcfSAlexander Motin break; 1389831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1390bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 1391831f5dcfSAlexander Motin break; 1392831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1393bcd91d25SJayachandran C. *result = slot->host.caps; 1394831f5dcfSAlexander Motin break; 1395831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1396bcd91d25SJayachandran C. *result = slot->host.ios.timing; 1397831f5dcfSAlexander Motin break; 13983a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1399bcd91d25SJayachandran C. *result = 65535; 14003a4a2557SAlexander Motin break; 1401831f5dcfSAlexander Motin } 1402831f5dcfSAlexander Motin return (0); 1403831f5dcfSAlexander Motin } 1404831f5dcfSAlexander Motin 1405d6b3aaf8SOleksandr Tymoshenko int 1406d6b3aaf8SOleksandr Tymoshenko sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1407831f5dcfSAlexander Motin { 1408831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1409831f5dcfSAlexander Motin 1410831f5dcfSAlexander Motin switch (which) { 1411831f5dcfSAlexander Motin default: 1412831f5dcfSAlexander Motin return (EINVAL); 1413831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1414831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 1415831f5dcfSAlexander Motin break; 1416831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1417831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 1418831f5dcfSAlexander Motin break; 1419831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1420831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 1421831f5dcfSAlexander Motin break; 1422831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1423831f5dcfSAlexander Motin if (value > 0) { 142457677a3aSOleksandr Tymoshenko uint32_t max_clock; 142557677a3aSOleksandr Tymoshenko uint32_t clock; 1426831f5dcfSAlexander Motin int i; 1427831f5dcfSAlexander Motin 142857677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 142957677a3aSOleksandr Tymoshenko clock = max_clock; 143057677a3aSOleksandr Tymoshenko 143157677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 143257677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 143357677a3aSOleksandr Tymoshenko i <<= 1) { 1434831f5dcfSAlexander Motin if (clock <= value) 1435831f5dcfSAlexander Motin break; 1436831f5dcfSAlexander Motin clock >>= 1; 1437831f5dcfSAlexander Motin } 143857677a3aSOleksandr Tymoshenko } 143957677a3aSOleksandr Tymoshenko else { 144057677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 144157677a3aSOleksandr Tymoshenko i += 2) { 144257677a3aSOleksandr Tymoshenko if (clock <= value) 144357677a3aSOleksandr Tymoshenko break; 144457677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 144557677a3aSOleksandr Tymoshenko } 144657677a3aSOleksandr Tymoshenko } 144757677a3aSOleksandr Tymoshenko 1448831f5dcfSAlexander Motin slot->host.ios.clock = clock; 1449831f5dcfSAlexander Motin } else 1450831f5dcfSAlexander Motin slot->host.ios.clock = 0; 1451831f5dcfSAlexander Motin break; 1452831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1453831f5dcfSAlexander Motin slot->host.mode = value; 1454831f5dcfSAlexander Motin break; 1455831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1456831f5dcfSAlexander Motin slot->host.ocr = value; 1457831f5dcfSAlexander Motin break; 1458831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1459831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 1460831f5dcfSAlexander Motin break; 1461831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1462831f5dcfSAlexander Motin slot->host.ios.vdd = value; 1463831f5dcfSAlexander Motin break; 1464831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1465831f5dcfSAlexander Motin slot->host.ios.timing = value; 1466831f5dcfSAlexander Motin break; 1467831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1468831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1469831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1470831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 14713a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1472831f5dcfSAlexander Motin return (EINVAL); 1473831f5dcfSAlexander Motin } 1474831f5dcfSAlexander Motin return (0); 1475831f5dcfSAlexander Motin } 1476831f5dcfSAlexander Motin 1477d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1); 1478