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 51a94a63f0SWarner Losh #include <cam/cam.h> 52a94a63f0SWarner Losh #include <cam/cam_ccb.h> 53a94a63f0SWarner Losh #include <cam/cam_debug.h> 54a94a63f0SWarner Losh #include <cam/cam_sim.h> 55a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 56a94a63f0SWarner Losh 57831f5dcfSAlexander Motin #include "mmcbr_if.h" 58831f5dcfSAlexander Motin #include "sdhci.h" 59d6b3aaf8SOleksandr Tymoshenko #include "sdhci_if.h" 60831f5dcfSAlexander Motin 61a94a63f0SWarner Losh #include "opt_mmccam.h" 62a94a63f0SWarner Losh 63f0d2731dSMarius Strobl SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 64831f5dcfSAlexander Motin 65a94a63f0SWarner Losh static int sdhci_debug = 0; 667e6ccea3SMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 677e6ccea3SMarius Strobl "Debug level"); 680f34084fSMarius Strobl u_int sdhci_quirk_clear = 0; 690f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 700f34084fSMarius Strobl 0, "Mask of quirks to clear"); 710f34084fSMarius Strobl u_int sdhci_quirk_set = 0; 720f34084fSMarius Strobl SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 730f34084fSMarius Strobl "Mask of quirks to set"); 745b69a497SAlexander Motin 75d6b3aaf8SOleksandr Tymoshenko #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 76d6b3aaf8SOleksandr Tymoshenko #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 77d6b3aaf8SOleksandr Tymoshenko #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 78d6b3aaf8SOleksandr Tymoshenko #define RD_MULTI_4(slot, off, ptr, count) \ 79d6b3aaf8SOleksandr Tymoshenko SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 80831f5dcfSAlexander Motin 81d6b3aaf8SOleksandr Tymoshenko #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 82d6b3aaf8SOleksandr Tymoshenko #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 83d6b3aaf8SOleksandr Tymoshenko #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 84d6b3aaf8SOleksandr Tymoshenko #define WR_MULTI_4(slot, off, ptr, count) \ 85d6b3aaf8SOleksandr Tymoshenko SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 86831f5dcfSAlexander Motin 87831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 88831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 89831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 90831f5dcfSAlexander Motin 91639f59f0SIan Lepore static void sdhci_card_poll(void *); 92831f5dcfSAlexander Motin static void sdhci_card_task(void *, int); 93831f5dcfSAlexander Motin 94*15c440e1SWarner Losh #ifdef MMCCAM 95a94a63f0SWarner Losh /* CAM-related */ 96a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock); 97a94a63f0SWarner Losh static int sdhci_cam_update_ios(struct sdhci_slot *slot); 98a94a63f0SWarner Losh static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 99a94a63f0SWarner Losh static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 100a94a63f0SWarner Losh static void sdhci_cam_poll(struct cam_sim *sim); 101a94a63f0SWarner Losh static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 102*15c440e1SWarner Losh #endif 103a94a63f0SWarner Losh 104831f5dcfSAlexander Motin /* helper routines */ 1050f34084fSMarius Strobl static void sdhci_dumpregs(struct sdhci_slot *slot); 1060f34084fSMarius Strobl static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 1070f34084fSMarius Strobl __printflike(2, 3); 1080f34084fSMarius Strobl 109831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 110831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 111831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 112831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 113831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 114831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 115831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 116831f5dcfSAlexander Motin 11733aad34dSOleksandr Tymoshenko #define SDHCI_DEFAULT_MAX_FREQ 50 11833aad34dSOleksandr Tymoshenko 11957677a3aSOleksandr Tymoshenko #define SDHCI_200_MAX_DIVIDER 256 12057677a3aSOleksandr Tymoshenko #define SDHCI_300_MAX_DIVIDER 2046 12157677a3aSOleksandr Tymoshenko 122639f59f0SIan Lepore #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 123639f59f0SIan Lepore #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 124639f59f0SIan Lepore 12593efdc63SAdrian Chadd /* 12693efdc63SAdrian Chadd * Broadcom BCM577xx Controller Constants 12793efdc63SAdrian Chadd */ 1281bacf3beSMarius Strobl /* Maximum divider supported by the default clock source. */ 1291bacf3beSMarius Strobl #define BCM577XX_DEFAULT_MAX_DIVIDER 256 1301bacf3beSMarius Strobl /* Alternative clock's base frequency. */ 1311bacf3beSMarius Strobl #define BCM577XX_ALT_CLOCK_BASE 63000000 13293efdc63SAdrian Chadd 13393efdc63SAdrian Chadd #define BCM577XX_HOST_CONTROL 0x198 13493efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 13593efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_SHIFT 12 13693efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 13793efdc63SAdrian Chadd #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 13893efdc63SAdrian Chadd 139831f5dcfSAlexander Motin static void 140831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 141831f5dcfSAlexander Motin { 1427e6ccea3SMarius Strobl 143831f5dcfSAlexander Motin if (error != 0) { 144831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 145831f5dcfSAlexander Motin return; 146831f5dcfSAlexander Motin } 147831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 148831f5dcfSAlexander Motin } 149831f5dcfSAlexander Motin 150d6b3aaf8SOleksandr Tymoshenko static int 151d6b3aaf8SOleksandr Tymoshenko slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 152d6b3aaf8SOleksandr Tymoshenko { 153d6b3aaf8SOleksandr Tymoshenko va_list ap; 154d6b3aaf8SOleksandr Tymoshenko int retval; 155d6b3aaf8SOleksandr Tymoshenko 156d6b3aaf8SOleksandr Tymoshenko retval = printf("%s-slot%d: ", 157d6b3aaf8SOleksandr Tymoshenko device_get_nameunit(slot->bus), slot->num); 158d6b3aaf8SOleksandr Tymoshenko 159d6b3aaf8SOleksandr Tymoshenko va_start(ap, fmt); 160d6b3aaf8SOleksandr Tymoshenko retval += vprintf(fmt, ap); 161d6b3aaf8SOleksandr Tymoshenko va_end(ap); 162d6b3aaf8SOleksandr Tymoshenko return (retval); 163d6b3aaf8SOleksandr Tymoshenko } 164d6b3aaf8SOleksandr Tymoshenko 165831f5dcfSAlexander Motin static void 166831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 167831f5dcfSAlexander Motin { 1687e6ccea3SMarius Strobl 169831f5dcfSAlexander Motin slot_printf(slot, 170831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 171831f5dcfSAlexander Motin 172831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 173831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 174831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 175831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 176831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 177831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 178831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 179831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 180831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 181831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 182831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 183831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 184831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 185831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 186831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 187831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 1889dbf8c46SMarius Strobl slot_printf(slot, "AC12 err: 0x%08x | Host ctl2: 0x%08x\n", 1899dbf8c46SMarius Strobl RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 1909dbf8c46SMarius Strobl slot_printf(slot, "Caps: 0x%08x | Caps2: 0x%08x\n", 1919dbf8c46SMarius Strobl RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 1929dbf8c46SMarius Strobl slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 1939dbf8c46SMarius Strobl RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 1949dbf8c46SMarius Strobl slot_printf(slot, "ADMA addr: 0x%08x | Slot int: 0x%08x\n", 1959dbf8c46SMarius Strobl RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 196831f5dcfSAlexander Motin 197831f5dcfSAlexander Motin slot_printf(slot, 198831f5dcfSAlexander Motin "===========================================\n"); 199831f5dcfSAlexander Motin } 200831f5dcfSAlexander Motin 201831f5dcfSAlexander Motin static void 202831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 203831f5dcfSAlexander Motin { 204831f5dcfSAlexander Motin int timeout; 205b440e965SMarius Strobl uint32_t clock; 206831f5dcfSAlexander Motin 207d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 2086e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 209831f5dcfSAlexander Motin return; 210831f5dcfSAlexander Motin } 211831f5dcfSAlexander Motin 212831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 213831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 214d6b3aaf8SOleksandr Tymoshenko (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 215831f5dcfSAlexander Motin /* This is to force an update */ 216831f5dcfSAlexander Motin clock = slot->clock; 217831f5dcfSAlexander Motin slot->clock = 0; 218831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 219831f5dcfSAlexander Motin } 220831f5dcfSAlexander Motin 221d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 222831f5dcfSAlexander Motin slot->clock = 0; 223d8208d9eSAlexander Motin slot->power = 0; 224d8208d9eSAlexander Motin } 225831f5dcfSAlexander Motin 22661bc42f7SIan Lepore WR1(slot, SDHCI_SOFTWARE_RESET, mask); 22761bc42f7SIan Lepore 22861bc42f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 22961bc42f7SIan Lepore /* 23061bc42f7SIan Lepore * Resets on TI OMAPs and AM335x are incompatible with SDHCI 23161bc42f7SIan Lepore * specification. The reset bit has internal propagation delay, 23261bc42f7SIan Lepore * so a fast read after write returns 0 even if reset process is 23361bc42f7SIan Lepore * in progress. The workaround is to poll for 1 before polling 23461bc42f7SIan Lepore * for 0. In the worst case, if we miss seeing it asserted the 23561bc42f7SIan Lepore * time we spent waiting is enough to ensure the reset finishes. 23661bc42f7SIan Lepore */ 23761bc42f7SIan Lepore timeout = 10000; 23861bc42f7SIan Lepore while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 23961bc42f7SIan Lepore if (timeout <= 0) 24061bc42f7SIan Lepore break; 24161bc42f7SIan Lepore timeout--; 24261bc42f7SIan Lepore DELAY(1); 24361bc42f7SIan Lepore } 24461bc42f7SIan Lepore } 24561bc42f7SIan Lepore 246831f5dcfSAlexander Motin /* Wait max 100 ms */ 24761bc42f7SIan Lepore timeout = 10000; 248831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 24961bc42f7SIan Lepore while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 25061bc42f7SIan Lepore if (timeout <= 0) { 25161bc42f7SIan Lepore slot_printf(slot, "Reset 0x%x never completed.\n", 25261bc42f7SIan Lepore mask); 253831f5dcfSAlexander Motin sdhci_dumpregs(slot); 254831f5dcfSAlexander Motin return; 255831f5dcfSAlexander Motin } 256831f5dcfSAlexander Motin timeout--; 25761bc42f7SIan Lepore DELAY(10); 258831f5dcfSAlexander Motin } 259831f5dcfSAlexander Motin } 260831f5dcfSAlexander Motin 261831f5dcfSAlexander Motin static void 262831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 263831f5dcfSAlexander Motin { 264831f5dcfSAlexander Motin 265831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 266831f5dcfSAlexander Motin 267831f5dcfSAlexander Motin /* Enable interrupts. */ 268831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 269831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 270831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 271831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 272831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 2733685b398SWarner Losh SDHCI_INT_ACMD12ERR; 274639f59f0SIan Lepore 275639f59f0SIan Lepore if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 276639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 277639f59f0SIan Lepore slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 278639f59f0SIan Lepore } 279639f59f0SIan Lepore 280831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 281831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 282831f5dcfSAlexander Motin } 283831f5dcfSAlexander Motin 284831f5dcfSAlexander Motin static void 285831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 286831f5dcfSAlexander Motin { 28793efdc63SAdrian Chadd uint32_t clk_base; 28893efdc63SAdrian Chadd uint32_t clk_sel; 289831f5dcfSAlexander Motin uint32_t res; 290831f5dcfSAlexander Motin uint16_t clk; 2918f3b7d56SOleksandr Tymoshenko uint16_t div; 292831f5dcfSAlexander Motin int timeout; 293831f5dcfSAlexander Motin 294831f5dcfSAlexander Motin if (clock == slot->clock) 295831f5dcfSAlexander Motin return; 296831f5dcfSAlexander Motin slot->clock = clock; 297831f5dcfSAlexander Motin 298831f5dcfSAlexander Motin /* Turn off the clock. */ 2994ddc0172SIan Lepore clk = RD2(slot, SDHCI_CLOCK_CONTROL); 3004ddc0172SIan Lepore WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 301b440e965SMarius Strobl /* If no clock requested - leave it so. */ 302831f5dcfSAlexander Motin if (clock == 0) 303831f5dcfSAlexander Motin return; 304ceb9e9f7SIan Lepore 30593efdc63SAdrian Chadd /* Determine the clock base frequency */ 30693efdc63SAdrian Chadd clk_base = slot->max_clk; 30793efdc63SAdrian Chadd if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 3081bacf3beSMarius Strobl clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 3091bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_MASK; 31093efdc63SAdrian Chadd 3111bacf3beSMarius Strobl /* 3121bacf3beSMarius Strobl * Select clock source appropriate for the requested frequency. 3131bacf3beSMarius Strobl */ 31493efdc63SAdrian Chadd if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 31593efdc63SAdrian Chadd clk_base = BCM577XX_ALT_CLOCK_BASE; 3161bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 3171bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 31893efdc63SAdrian Chadd } else { 3191bacf3beSMarius Strobl clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 3201bacf3beSMarius Strobl BCM577XX_CTRL_CLKSEL_SHIFT); 32193efdc63SAdrian Chadd } 32293efdc63SAdrian Chadd 32393efdc63SAdrian Chadd WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 32493efdc63SAdrian Chadd } 32593efdc63SAdrian Chadd 326ceb9e9f7SIan Lepore /* Recalculate timeout clock frequency based on the new sd clock. */ 327ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 328ceb9e9f7SIan Lepore slot->timeout_clk = slot->clock / 1000; 329ceb9e9f7SIan Lepore 3308f3b7d56SOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 331831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 33293efdc63SAdrian Chadd res = clk_base; 33357677a3aSOleksandr Tymoshenko for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 334831f5dcfSAlexander Motin if (res <= clock) 335831f5dcfSAlexander Motin break; 336831f5dcfSAlexander Motin res >>= 1; 337831f5dcfSAlexander Motin } 338831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 3398f3b7d56SOleksandr Tymoshenko div >>= 1; 340c11bbc7dSMarius Strobl } else { 3418f3b7d56SOleksandr Tymoshenko /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 34293efdc63SAdrian Chadd if (clock >= clk_base) 34357677a3aSOleksandr Tymoshenko div = 0; 3448f3b7d56SOleksandr Tymoshenko else { 34557677a3aSOleksandr Tymoshenko for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 34693efdc63SAdrian Chadd if ((clk_base / div) <= clock) 3478f3b7d56SOleksandr Tymoshenko break; 3488f3b7d56SOleksandr Tymoshenko } 3498f3b7d56SOleksandr Tymoshenko } 3508f3b7d56SOleksandr Tymoshenko div >>= 1; 3518f3b7d56SOleksandr Tymoshenko } 3528f3b7d56SOleksandr Tymoshenko 3538f3b7d56SOleksandr Tymoshenko if (bootverbose || sdhci_debug) 35493efdc63SAdrian Chadd slot_printf(slot, "Divider %d for freq %d (base %d)\n", 35593efdc63SAdrian Chadd div, clock, clk_base); 3568f3b7d56SOleksandr Tymoshenko 357831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 3588f3b7d56SOleksandr Tymoshenko clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 3598f3b7d56SOleksandr Tymoshenko clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 3608f3b7d56SOleksandr Tymoshenko << SDHCI_DIVIDER_HI_SHIFT; 3618f3b7d56SOleksandr Tymoshenko 362831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 363831f5dcfSAlexander Motin /* Enable clock. */ 364831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 365831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 366831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 367831f5dcfSAlexander Motin timeout = 10; 368831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 369831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 370831f5dcfSAlexander Motin if (timeout == 0) { 371831f5dcfSAlexander Motin slot_printf(slot, 372831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 373831f5dcfSAlexander Motin sdhci_dumpregs(slot); 374831f5dcfSAlexander Motin return; 375831f5dcfSAlexander Motin } 376831f5dcfSAlexander Motin timeout--; 377831f5dcfSAlexander Motin DELAY(1000); 378831f5dcfSAlexander Motin } 379831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 380831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 381831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 382831f5dcfSAlexander Motin } 383831f5dcfSAlexander Motin 384831f5dcfSAlexander Motin static void 385831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 386831f5dcfSAlexander Motin { 38785083a80SMarius Strobl int i; 388831f5dcfSAlexander Motin uint8_t pwr; 389831f5dcfSAlexander Motin 390831f5dcfSAlexander Motin if (slot->power == power) 391831f5dcfSAlexander Motin return; 392d6b3aaf8SOleksandr Tymoshenko 393831f5dcfSAlexander Motin slot->power = power; 394831f5dcfSAlexander Motin 395831f5dcfSAlexander Motin /* Turn off the power. */ 396831f5dcfSAlexander Motin pwr = 0; 397831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 398b440e965SMarius Strobl /* If power down requested - leave it so. */ 399831f5dcfSAlexander Motin if (power == 0) 400831f5dcfSAlexander Motin return; 401831f5dcfSAlexander Motin /* Set voltage. */ 402831f5dcfSAlexander Motin switch (1 << power) { 403831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 404831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 405831f5dcfSAlexander Motin break; 406831f5dcfSAlexander Motin case MMC_OCR_290_300: 407831f5dcfSAlexander Motin case MMC_OCR_300_310: 408831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 409831f5dcfSAlexander Motin break; 410831f5dcfSAlexander Motin case MMC_OCR_320_330: 411831f5dcfSAlexander Motin case MMC_OCR_330_340: 412831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 413831f5dcfSAlexander Motin break; 414831f5dcfSAlexander Motin } 415831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 41685083a80SMarius Strobl /* 41785083a80SMarius Strobl * Turn on VDD1 power. Note that at least some Intel controllers can 41885083a80SMarius Strobl * fail to enable bus power on the first try after transiting from D3 4198022c8ebSMarius Strobl * to D0, so we give them up to 2 ms. 42085083a80SMarius Strobl */ 421831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 42285083a80SMarius Strobl for (i = 0; i < 20; i++) { 423831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 42485083a80SMarius Strobl if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 42585083a80SMarius Strobl break; 42685083a80SMarius Strobl DELAY(100); 42785083a80SMarius Strobl } 42885083a80SMarius Strobl if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 42985083a80SMarius Strobl slot_printf(slot, "Bus power failed to enable"); 430a2832f9fSMarius Strobl 431a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 432a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 433a2832f9fSMarius Strobl DELAY(10); 434a2832f9fSMarius Strobl WR1(slot, SDHCI_POWER_CONTROL, pwr); 435a2832f9fSMarius Strobl DELAY(300); 436a2832f9fSMarius Strobl } 437831f5dcfSAlexander Motin } 438831f5dcfSAlexander Motin 439831f5dcfSAlexander Motin static void 440831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 441831f5dcfSAlexander Motin { 442831f5dcfSAlexander Motin uint32_t data; 443831f5dcfSAlexander Motin char *buffer; 444831f5dcfSAlexander Motin size_t left; 445831f5dcfSAlexander Motin 446831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 447831f5dcfSAlexander Motin buffer += slot->offset; 448831f5dcfSAlexander Motin /* Transfer one block at a time. */ 449831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 450831f5dcfSAlexander Motin slot->offset += left; 451831f5dcfSAlexander Motin 452831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 453d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 454831f5dcfSAlexander Motin DELAY(10); 455ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 456831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 457831f5dcfSAlexander Motin while (left > 3) { 458831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 459831f5dcfSAlexander Motin buffer[0] = data; 460831f5dcfSAlexander Motin buffer[1] = (data >> 8); 461831f5dcfSAlexander Motin buffer[2] = (data >> 16); 462831f5dcfSAlexander Motin buffer[3] = (data >> 24); 463831f5dcfSAlexander Motin buffer += 4; 464831f5dcfSAlexander Motin left -= 4; 465831f5dcfSAlexander Motin } 466831f5dcfSAlexander Motin } else { 467d6b3aaf8SOleksandr Tymoshenko RD_MULTI_4(slot, SDHCI_BUFFER, 468831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 469831f5dcfSAlexander Motin left &= 3; 470831f5dcfSAlexander Motin } 471831f5dcfSAlexander Motin /* Handle uneven size case. */ 472831f5dcfSAlexander Motin if (left > 0) { 473831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 474831f5dcfSAlexander Motin while (left > 0) { 475831f5dcfSAlexander Motin *(buffer++) = data; 476831f5dcfSAlexander Motin data >>= 8; 477831f5dcfSAlexander Motin left--; 478831f5dcfSAlexander Motin } 479831f5dcfSAlexander Motin } 480831f5dcfSAlexander Motin } 481831f5dcfSAlexander Motin 482831f5dcfSAlexander Motin static void 483831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 484831f5dcfSAlexander Motin { 485831f5dcfSAlexander Motin uint32_t data = 0; 486831f5dcfSAlexander Motin char *buffer; 487831f5dcfSAlexander Motin size_t left; 488831f5dcfSAlexander Motin 489831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 490831f5dcfSAlexander Motin buffer += slot->offset; 491831f5dcfSAlexander Motin /* Transfer one block at a time. */ 492831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 493831f5dcfSAlexander Motin slot->offset += left; 494831f5dcfSAlexander Motin 495ecc2d997SRui Paulo /* Handle unaligned and aligned buffer cases. */ 496831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 497831f5dcfSAlexander Motin while (left > 3) { 498831f5dcfSAlexander Motin data = buffer[0] + 499831f5dcfSAlexander Motin (buffer[1] << 8) + 500831f5dcfSAlexander Motin (buffer[2] << 16) + 501831f5dcfSAlexander Motin (buffer[3] << 24); 502831f5dcfSAlexander Motin left -= 4; 503831f5dcfSAlexander Motin buffer += 4; 504831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 505831f5dcfSAlexander Motin } 506831f5dcfSAlexander Motin } else { 507d6b3aaf8SOleksandr Tymoshenko WR_MULTI_4(slot, SDHCI_BUFFER, 508831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 509831f5dcfSAlexander Motin left &= 3; 510831f5dcfSAlexander Motin } 511831f5dcfSAlexander Motin /* Handle uneven size case. */ 512831f5dcfSAlexander Motin if (left > 0) { 513831f5dcfSAlexander Motin while (left > 0) { 514831f5dcfSAlexander Motin data <<= 8; 515831f5dcfSAlexander Motin data += *(buffer++); 516831f5dcfSAlexander Motin left--; 517831f5dcfSAlexander Motin } 518831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 519831f5dcfSAlexander Motin } 520831f5dcfSAlexander Motin } 521831f5dcfSAlexander Motin 522831f5dcfSAlexander Motin static void 523831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 524831f5dcfSAlexander Motin { 525831f5dcfSAlexander Motin 526831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 527831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 528831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 529831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 530831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 531831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 532831f5dcfSAlexander Motin break; 533831f5dcfSAlexander Motin } 534831f5dcfSAlexander Motin } else { 535831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 536831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 537831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 538831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 539831f5dcfSAlexander Motin break; 540831f5dcfSAlexander Motin } 541831f5dcfSAlexander Motin } 542831f5dcfSAlexander Motin } 543831f5dcfSAlexander Motin 544831f5dcfSAlexander Motin static void 5457e6ccea3SMarius Strobl sdhci_card_task(void *arg, int pending __unused) 546831f5dcfSAlexander Motin { 547831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 5487e6ccea3SMarius Strobl device_t d; 549831f5dcfSAlexander Motin 550831f5dcfSAlexander Motin SDHCI_LOCK(slot); 5516e37fb2bSIan Lepore if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 552a94a63f0SWarner Losh #ifdef MMCCAM 553a94a63f0SWarner Losh if (slot->card_present == 0) { 554a94a63f0SWarner Losh #else 555831f5dcfSAlexander Motin if (slot->dev == NULL) { 556a94a63f0SWarner Losh #endif 557831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 558639f59f0SIan Lepore if (bootverbose || sdhci_debug) 559639f59f0SIan Lepore slot_printf(slot, "Card inserted\n"); 560a94a63f0SWarner Losh #ifdef MMCCAM 561a94a63f0SWarner Losh slot->card_present = 1; 562a94a63f0SWarner Losh union ccb *ccb; 563a94a63f0SWarner Losh uint32_t pathid; 564a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 565a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 566a94a63f0SWarner Losh if (ccb == NULL) { 567a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 568a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 569a94a63f0SWarner Losh return; 570a94a63f0SWarner Losh } 571a94a63f0SWarner Losh 572a94a63f0SWarner Losh /* 573a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 574a94a63f0SWarner Losh * will be at lun 0. 575a94a63f0SWarner Losh */ 576a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 577a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 578a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 579a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 580a94a63f0SWarner Losh xpt_free_ccb(ccb); 581a94a63f0SWarner Losh return; 582a94a63f0SWarner Losh } 583a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 584a94a63f0SWarner Losh xpt_rescan(ccb); 585a94a63f0SWarner Losh #else 586d6b3aaf8SOleksandr Tymoshenko slot->dev = device_add_child(slot->bus, "mmc", -1); 587831f5dcfSAlexander Motin device_set_ivars(slot->dev, slot); 588831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 589831f5dcfSAlexander Motin device_probe_and_attach(slot->dev); 590a94a63f0SWarner Losh #endif 591831f5dcfSAlexander Motin } else 592831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 593831f5dcfSAlexander Motin } else { 594a94a63f0SWarner Losh #ifdef MMCCAM 595a94a63f0SWarner Losh if (slot->card_present == 1) { 596a94a63f0SWarner Losh #else 597831f5dcfSAlexander Motin if (slot->dev != NULL) { 598a94a63f0SWarner Losh #endif 599831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 600639f59f0SIan Lepore if (bootverbose || sdhci_debug) 601639f59f0SIan Lepore slot_printf(slot, "Card removed\n"); 6027e6ccea3SMarius Strobl d = slot->dev; 603831f5dcfSAlexander Motin slot->dev = NULL; 604a94a63f0SWarner Losh #ifdef MMCCAM 605a94a63f0SWarner Losh slot->card_present = 0; 606a94a63f0SWarner Losh union ccb *ccb; 607a94a63f0SWarner Losh uint32_t pathid; 608a94a63f0SWarner Losh pathid = cam_sim_path(slot->sim); 609a94a63f0SWarner Losh ccb = xpt_alloc_ccb_nowait(); 610a94a63f0SWarner Losh if (ccb == NULL) { 611a94a63f0SWarner Losh slot_printf(slot, "Unable to alloc CCB for rescan\n"); 612a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 613a94a63f0SWarner Losh return; 614a94a63f0SWarner Losh } 615a94a63f0SWarner Losh 616a94a63f0SWarner Losh /* 617a94a63f0SWarner Losh * We create a rescan request for BUS:0:0, since the card 618a94a63f0SWarner Losh * will be at lun 0. 619a94a63f0SWarner Losh */ 620a94a63f0SWarner Losh if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 621a94a63f0SWarner Losh /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 622a94a63f0SWarner Losh slot_printf(slot, "Unable to create path for rescan\n"); 623a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 624a94a63f0SWarner Losh xpt_free_ccb(ccb); 625a94a63f0SWarner Losh return; 626a94a63f0SWarner Losh } 627a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 628a94a63f0SWarner Losh xpt_rescan(ccb); 629a94a63f0SWarner Losh #else 630831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 631d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 632a94a63f0SWarner Losh #endif 633831f5dcfSAlexander Motin } else 634831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 635831f5dcfSAlexander Motin } 636831f5dcfSAlexander Motin } 637831f5dcfSAlexander Motin 638b8bf08b1SIan Lepore static void 639b8bf08b1SIan Lepore sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 640639f59f0SIan Lepore { 641639f59f0SIan Lepore bool was_present; 642639f59f0SIan Lepore 643639f59f0SIan Lepore /* 644639f59f0SIan Lepore * If there was no card and now there is one, schedule the task to 645639f59f0SIan Lepore * create the child device after a short delay. The delay is to 646639f59f0SIan Lepore * debounce the card insert (sometimes the card detect pin stabilizes 647639f59f0SIan Lepore * before the other pins have made good contact). 648639f59f0SIan Lepore * 649639f59f0SIan Lepore * If there was a card present and now it's gone, immediately schedule 650639f59f0SIan Lepore * the task to delete the child device. No debouncing -- gone is gone, 651639f59f0SIan Lepore * because once power is removed, a full card re-init is needed, and 652639f59f0SIan Lepore * that happens by deleting and recreating the child device. 653639f59f0SIan Lepore */ 654a94a63f0SWarner Losh #ifdef MMCCAM 655a94a63f0SWarner Losh was_present = slot->card_present; 656a94a63f0SWarner Losh #else 657639f59f0SIan Lepore was_present = slot->dev != NULL; 658a94a63f0SWarner Losh #endif 659639f59f0SIan Lepore if (!was_present && is_present) { 660639f59f0SIan Lepore taskqueue_enqueue_timeout(taskqueue_swi_giant, 661639f59f0SIan Lepore &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 662639f59f0SIan Lepore } else if (was_present && !is_present) { 663639f59f0SIan Lepore taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 664639f59f0SIan Lepore } 665b8bf08b1SIan Lepore } 666b8bf08b1SIan Lepore 667b8bf08b1SIan Lepore void 668b8bf08b1SIan Lepore sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 669b8bf08b1SIan Lepore { 670b8bf08b1SIan Lepore 671b8bf08b1SIan Lepore SDHCI_LOCK(slot); 672b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, is_present); 673639f59f0SIan Lepore SDHCI_UNLOCK(slot); 674639f59f0SIan Lepore } 675639f59f0SIan Lepore 676639f59f0SIan Lepore static void 677639f59f0SIan Lepore sdhci_card_poll(void *arg) 678639f59f0SIan Lepore { 679639f59f0SIan Lepore struct sdhci_slot *slot = arg; 680639f59f0SIan Lepore 681639f59f0SIan Lepore sdhci_handle_card_present(slot, 682639f59f0SIan Lepore SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 683639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 684639f59f0SIan Lepore sdhci_card_poll, slot); 685639f59f0SIan Lepore } 686639f59f0SIan Lepore 687d6b3aaf8SOleksandr Tymoshenko int 688d6b3aaf8SOleksandr Tymoshenko sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 689831f5dcfSAlexander Motin { 6900f34084fSMarius Strobl uint32_t caps, caps2, freq, host_caps; 691d6b3aaf8SOleksandr Tymoshenko int err; 692831f5dcfSAlexander Motin 693831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 694a94a63f0SWarner Losh 695d6b3aaf8SOleksandr Tymoshenko slot->num = num; 696d6b3aaf8SOleksandr Tymoshenko slot->bus = dev; 697d6b3aaf8SOleksandr Tymoshenko 698831f5dcfSAlexander Motin /* Allocate DMA tag. */ 699831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 700831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 701831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 702831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 703831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 704831f5dcfSAlexander Motin &slot->dmatag); 705831f5dcfSAlexander Motin if (err != 0) { 706831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 707831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 708d6b3aaf8SOleksandr Tymoshenko return (err); 709831f5dcfSAlexander Motin } 710831f5dcfSAlexander Motin /* Allocate DMA memory. */ 711831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 712831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 713831f5dcfSAlexander Motin if (err != 0) { 714831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 715831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 716d6b3aaf8SOleksandr Tymoshenko return (err); 717831f5dcfSAlexander Motin } 718831f5dcfSAlexander Motin /* Map the memory. */ 719831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 720831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 721831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 722831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 723831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 724831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 725d6b3aaf8SOleksandr Tymoshenko if (err) 726d6b3aaf8SOleksandr Tymoshenko return (err); 727d6b3aaf8SOleksandr Tymoshenko else 728d6b3aaf8SOleksandr Tymoshenko return (EFAULT); 729831f5dcfSAlexander Motin } 730d6b3aaf8SOleksandr Tymoshenko 731831f5dcfSAlexander Motin /* Initialize slot. */ 732831f5dcfSAlexander Motin sdhci_init(slot); 733d6b3aaf8SOleksandr Tymoshenko slot->version = (RD2(slot, SDHCI_HOST_VERSION) 734d6b3aaf8SOleksandr Tymoshenko >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 7350f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 7368f3b7d56SOleksandr Tymoshenko caps = slot->caps; 7370f34084fSMarius Strobl caps2 = slot->caps2; 7380f34084fSMarius Strobl } else { 739831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 7400f34084fSMarius Strobl if (slot->version >= SDHCI_SPEC_300) 7410f34084fSMarius Strobl caps2 = RD4(slot, SDHCI_CAPABILITIES2); 7420f34084fSMarius Strobl else 7430f34084fSMarius Strobl caps2 = 0; 7440f34084fSMarius Strobl } 745831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 74633aad34dSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 74787a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 74887a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 74933aad34dSOleksandr Tymoshenko else 75087a6a871SIan Lepore freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 75187a6a871SIan Lepore SDHCI_CLOCK_BASE_SHIFT; 75287a6a871SIan Lepore if (freq != 0) 75387a6a871SIan Lepore slot->max_clk = freq * 1000000; 75487a6a871SIan Lepore /* 75587a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 75687a6a871SIan Lepore * hasn't already set max_clk we're probably not going to work right 75787a6a871SIan Lepore * with an assumption, so complain about it. 75887a6a871SIan Lepore */ 759831f5dcfSAlexander Motin if (slot->max_clk == 0) { 76087a6a871SIan Lepore slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 761831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 7621bacf3beSMarius Strobl "frequency, using %dMHz as default.\n", 7631bacf3beSMarius Strobl SDHCI_DEFAULT_MAX_FREQ); 764831f5dcfSAlexander Motin } 765a2832f9fSMarius Strobl /* Calculate/set timeout clock frequency. */ 7668f3b7d56SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 7678f3b7d56SOleksandr Tymoshenko slot->timeout_clk = slot->max_clk / 1000; 768a2832f9fSMarius Strobl } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 769a2832f9fSMarius Strobl slot->timeout_clk = 1000; 7708f3b7d56SOleksandr Tymoshenko } else { 7711bacf3beSMarius Strobl slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 7721bacf3beSMarius Strobl SDHCI_TIMEOUT_CLK_SHIFT; 7738f3b7d56SOleksandr Tymoshenko if (caps & SDHCI_TIMEOUT_CLK_UNIT) 7748f3b7d56SOleksandr Tymoshenko slot->timeout_clk *= 1000; 7758f3b7d56SOleksandr Tymoshenko } 77687a6a871SIan Lepore /* 77787a6a871SIan Lepore * If the frequency wasn't in the capabilities and the hardware driver 77887a6a871SIan Lepore * hasn't already set timeout_clk we'll probably work okay using the 77987a6a871SIan Lepore * max timeout, but still mention it. 78087a6a871SIan Lepore */ 781831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 782831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 783ceb9e9f7SIan Lepore "frequency, setting BROKEN_TIMEOUT quirk.\n"); 784ceb9e9f7SIan Lepore slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 785831f5dcfSAlexander Motin } 786831f5dcfSAlexander Motin 78757677a3aSOleksandr Tymoshenko slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 788831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 789831f5dcfSAlexander Motin slot->host.host_ocr = 0; 790831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 791831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 792831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 793831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 794831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_180) 795831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 796831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 797831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 798831f5dcfSAlexander Motin "support voltages.\n"); 799831f5dcfSAlexander Motin } 8000f34084fSMarius Strobl host_caps = MMC_CAP_4_BIT_DATA; 8012d1731b8SIan Lepore if (caps & SDHCI_CAN_DO_8BITBUS) 8020f34084fSMarius Strobl host_caps |= MMC_CAP_8_BIT_DATA; 803831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 8040f34084fSMarius Strobl host_caps |= MMC_CAP_HSPEED; 80572dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 8060f34084fSMarius Strobl host_caps |= MMC_CAP_BOOT_NOACC; 80772dec079SMarius Strobl if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 8080f34084fSMarius Strobl host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 8090f34084fSMarius Strobl if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 8100f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 8110f34084fSMarius Strobl if (caps2 & SDHCI_CAN_SDR104) { 8120f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 8130f34084fSMarius Strobl if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 8140f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS200; 8150f34084fSMarius Strobl } else if (caps2 & SDHCI_CAN_SDR50) 8160f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_SDR50; 8170f34084fSMarius Strobl if (caps2 & SDHCI_CAN_DDR50 && 8180f34084fSMarius Strobl !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 8190f34084fSMarius Strobl host_caps |= MMC_CAP_UHS_DDR50; 8200f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 8210f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_DDR52; 8220f34084fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 8230f34084fSMarius Strobl caps2 & SDHCI_CAN_MMC_HS400) 8240f34084fSMarius Strobl host_caps |= MMC_CAP_MMC_HS400; 8250f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_330; 8260f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 8270f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50 | 8280f34084fSMarius Strobl MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 8290f34084fSMarius Strobl MMC_CAP_MMC_HS400_180)) 8300f34084fSMarius Strobl host_caps |= MMC_CAP_SIGNALING_180; 831f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 8320f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_A; 833f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 8340f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_C; 835f8b883c1SImre Vadász if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 8360f34084fSMarius Strobl host_caps |= MMC_CAP_DRIVER_TYPE_D; 8370f34084fSMarius Strobl slot->host.caps = host_caps; 8380f34084fSMarius Strobl 839831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 840831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 841831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 842d6b3aaf8SOleksandr Tymoshenko 843d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 844831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 845d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 846831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 847a2832f9fSMarius Strobl if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 848a2832f9fSMarius Strobl slot->opt |= SDHCI_NON_REMOVABLE; 849831f5dcfSAlexander Motin 850c3a0f75aSOleksandr Tymoshenko /* 851c3a0f75aSOleksandr Tymoshenko * Use platform-provided transfer backend 852c3a0f75aSOleksandr Tymoshenko * with PIO as a fallback mechanism 853c3a0f75aSOleksandr Tymoshenko */ 854c3a0f75aSOleksandr Tymoshenko if (slot->opt & SDHCI_PLATFORM_TRANSFER) 855c3a0f75aSOleksandr Tymoshenko slot->opt &= ~SDHCI_HAVE_DMA; 856c3a0f75aSOleksandr Tymoshenko 8575b69a497SAlexander Motin if (bootverbose || sdhci_debug) { 8580f34084fSMarius Strobl slot_printf(slot, 8590f34084fSMarius Strobl "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n", 860831f5dcfSAlexander Motin slot->max_clk / 1000000, 861831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 8620f34084fSMarius Strobl (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 8630f34084fSMarius Strobl ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 864831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 865831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 866831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 8670f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 8680f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 869f8b883c1SImre Vadász (caps2 & SDHCI_CAN_DRIVE_TYPE_A) ? "A" : "", 870f8b883c1SImre Vadász (caps2 & SDHCI_CAN_DRIVE_TYPE_C) ? "C" : "", 871f8b883c1SImre Vadász (caps2 & SDHCI_CAN_DRIVE_TYPE_D) ? "D" : "", 872831f5dcfSAlexander Motin (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 8730f34084fSMarius Strobl if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 8740f34084fSMarius Strobl MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 8750f34084fSMarius Strobl slot_printf(slot, "eMMC:%s%s%s%s\n", 8760f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 8770f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 8780f34084fSMarius Strobl (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 8790f34084fSMarius Strobl ((host_caps & 8800f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 8810f34084fSMarius Strobl (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 8820f34084fSMarius Strobl " HS400ES" : ""); 8830f34084fSMarius Strobl if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 8840f34084fSMarius Strobl MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 8850f34084fSMarius Strobl slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 8860f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 8870f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 8880f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 8890f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 8900f34084fSMarius Strobl (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 891831f5dcfSAlexander Motin sdhci_dumpregs(slot); 892831f5dcfSAlexander Motin } 893831f5dcfSAlexander Motin 894ba6fc1c7SLuiz Otavio O Souza slot->timeout = 10; 895ba6fc1c7SLuiz Otavio O Souza SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 896ba6fc1c7SLuiz Otavio O Souza SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 897ba6fc1c7SLuiz Otavio O Souza "timeout", CTLFLAG_RW, &slot->timeout, 0, 898ba6fc1c7SLuiz Otavio O Souza "Maximum timeout for SDHCI transfers (in secs)"); 899831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 900639f59f0SIan Lepore TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 901639f59f0SIan Lepore sdhci_card_task, slot); 902639f59f0SIan Lepore callout_init(&slot->card_poll_callout, 1); 903e64f01a9SIan Lepore callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 904ba6fc1c7SLuiz Otavio O Souza 905639f59f0SIan Lepore if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 906639f59f0SIan Lepore !(slot->opt & SDHCI_NON_REMOVABLE)) { 907639f59f0SIan Lepore callout_reset(&slot->card_poll_callout, 908639f59f0SIan Lepore SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 909639f59f0SIan Lepore } 910639f59f0SIan Lepore 911831f5dcfSAlexander Motin return (0); 912831f5dcfSAlexander Motin } 913831f5dcfSAlexander Motin 914d6b3aaf8SOleksandr Tymoshenko void 915d6b3aaf8SOleksandr Tymoshenko sdhci_start_slot(struct sdhci_slot *slot) 916831f5dcfSAlexander Motin { 9177e6ccea3SMarius Strobl 918d6b3aaf8SOleksandr Tymoshenko sdhci_card_task(slot, 0); 919d6b3aaf8SOleksandr Tymoshenko } 920831f5dcfSAlexander Motin 921d6b3aaf8SOleksandr Tymoshenko int 922d6b3aaf8SOleksandr Tymoshenko sdhci_cleanup_slot(struct sdhci_slot *slot) 923d6b3aaf8SOleksandr Tymoshenko { 924831f5dcfSAlexander Motin device_t d; 925831f5dcfSAlexander Motin 926e64f01a9SIan Lepore callout_drain(&slot->timeout_callout); 927639f59f0SIan Lepore callout_drain(&slot->card_poll_callout); 928831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 929639f59f0SIan Lepore taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 930831f5dcfSAlexander Motin 931831f5dcfSAlexander Motin SDHCI_LOCK(slot); 932831f5dcfSAlexander Motin d = slot->dev; 933831f5dcfSAlexander Motin slot->dev = NULL; 934831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 935831f5dcfSAlexander Motin if (d != NULL) 936d6b3aaf8SOleksandr Tymoshenko device_delete_child(slot->bus, d); 937831f5dcfSAlexander Motin 938831f5dcfSAlexander Motin SDHCI_LOCK(slot); 939831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 940831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 941831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 942831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 943831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 944d6b3aaf8SOleksandr Tymoshenko 945831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 946d6b3aaf8SOleksandr Tymoshenko 947831f5dcfSAlexander Motin return (0); 948831f5dcfSAlexander Motin } 949831f5dcfSAlexander Motin 950d6b3aaf8SOleksandr Tymoshenko int 951d6b3aaf8SOleksandr Tymoshenko sdhci_generic_suspend(struct sdhci_slot *slot) 95292bf0e27SAlexander Motin { 9537e6ccea3SMarius Strobl 954d6b3aaf8SOleksandr Tymoshenko sdhci_reset(slot, SDHCI_RESET_ALL); 95592bf0e27SAlexander Motin 95692bf0e27SAlexander Motin return (0); 95792bf0e27SAlexander Motin } 95892bf0e27SAlexander Motin 959d6b3aaf8SOleksandr Tymoshenko int 960d6b3aaf8SOleksandr Tymoshenko sdhci_generic_resume(struct sdhci_slot *slot) 96192bf0e27SAlexander Motin { 9627e6ccea3SMarius Strobl 963d6b3aaf8SOleksandr Tymoshenko sdhci_init(slot); 96492bf0e27SAlexander Motin 965d6b3aaf8SOleksandr Tymoshenko return (0); 96692bf0e27SAlexander Motin } 96792bf0e27SAlexander Motin 96857677a3aSOleksandr Tymoshenko uint32_t 969b440e965SMarius Strobl sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 97057677a3aSOleksandr Tymoshenko { 9717e6ccea3SMarius Strobl 97257677a3aSOleksandr Tymoshenko if (slot->version >= SDHCI_SPEC_300) 97357677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 97457677a3aSOleksandr Tymoshenko else 97557677a3aSOleksandr Tymoshenko return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 97657677a3aSOleksandr Tymoshenko } 97757677a3aSOleksandr Tymoshenko 9786e37fb2bSIan Lepore bool 979b440e965SMarius Strobl sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 9806e37fb2bSIan Lepore { 9816e37fb2bSIan Lepore 982639f59f0SIan Lepore if (slot->opt & SDHCI_NON_REMOVABLE) 983639f59f0SIan Lepore return true; 984639f59f0SIan Lepore 9856e37fb2bSIan Lepore return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 9866e37fb2bSIan Lepore } 9876e37fb2bSIan Lepore 9880f34084fSMarius Strobl void 9890f34084fSMarius Strobl sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 9900f34084fSMarius Strobl { 9910f34084fSMarius Strobl struct mmc_ios *ios; 9920f34084fSMarius Strobl uint16_t hostctrl2; 9930f34084fSMarius Strobl 9940f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 9950f34084fSMarius Strobl return; 9960f34084fSMarius Strobl 9970f34084fSMarius Strobl ios = &slot->host.ios; 9980f34084fSMarius Strobl sdhci_set_clock(slot, 0); 9990f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 10000f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 10010f34084fSMarius Strobl if (ios->timing == bus_timing_mmc_hs400 || 10020f34084fSMarius Strobl ios->timing == bus_timing_mmc_hs400es) 10030f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 10040f34084fSMarius Strobl else if (ios->clock > SD_SDR50_MAX) 10050f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 10060f34084fSMarius Strobl else if (ios->clock > SD_SDR25_MAX) 10070f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 10080f34084fSMarius Strobl else if (ios->clock > SD_SDR12_MAX) { 10090f34084fSMarius Strobl if (ios->timing == bus_timing_uhs_ddr50 || 10100f34084fSMarius Strobl ios->timing == bus_timing_mmc_ddr52) 10110f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 10120f34084fSMarius Strobl else 10130f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 10140f34084fSMarius Strobl } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 10150f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 10160f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 10170f34084fSMarius Strobl sdhci_set_clock(slot, ios->clock); 10180f34084fSMarius Strobl } 10190f34084fSMarius Strobl 1020d6b3aaf8SOleksandr Tymoshenko int 1021d6b3aaf8SOleksandr Tymoshenko sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1022831f5dcfSAlexander Motin { 1023831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1024831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 1025831f5dcfSAlexander Motin 1026831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1027831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 1028831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 1029831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1030831f5dcfSAlexander Motin sdhci_init(slot); 1031831f5dcfSAlexander Motin } 1032831f5dcfSAlexander Motin /* Configure the bus. */ 1033831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 1034831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 10352d1731b8SIan Lepore if (ios->bus_width == bus_width_8) { 10362d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1037831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 10382d1731b8SIan Lepore } else if (ios->bus_width == bus_width_4) { 10392d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 10402d1731b8SIan Lepore slot->hostctrl |= SDHCI_CTRL_4BITBUS; 10412d1731b8SIan Lepore } else if (ios->bus_width == bus_width_1) { 10422d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 10432d1731b8SIan Lepore slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 10442d1731b8SIan Lepore } else { 10452d1731b8SIan Lepore panic("Invalid bus width: %d", ios->bus_width); 10462d1731b8SIan Lepore } 10470f34084fSMarius Strobl if (ios->clock > SD_SDR12_MAX && 1048bba987dcSIan Lepore !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1049831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 1050831f5dcfSAlexander Motin else 1051831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1052831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 10530f34084fSMarius Strobl SDHCI_SET_UHS_TIMING(brdev, slot); 1054831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 1055d6b3aaf8SOleksandr Tymoshenko if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1056831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1057831f5dcfSAlexander Motin 1058831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1059831f5dcfSAlexander Motin return (0); 1060831f5dcfSAlexander Motin } 1061831f5dcfSAlexander Motin 10620f34084fSMarius Strobl int 10630f34084fSMarius Strobl sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 10640f34084fSMarius Strobl { 10650f34084fSMarius Strobl struct sdhci_slot *slot = device_get_ivars(reqdev); 10660f34084fSMarius Strobl enum mmc_vccq vccq; 10670f34084fSMarius Strobl int err; 10680f34084fSMarius Strobl uint16_t hostctrl2; 10690f34084fSMarius Strobl 10700f34084fSMarius Strobl if (slot->version < SDHCI_SPEC_300) 10710f34084fSMarius Strobl return (0); 10720f34084fSMarius Strobl 10730f34084fSMarius Strobl err = 0; 10740f34084fSMarius Strobl vccq = slot->host.ios.vccq; 10750f34084fSMarius Strobl SDHCI_LOCK(slot); 10760f34084fSMarius Strobl sdhci_set_clock(slot, 0); 10770f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 10780f34084fSMarius Strobl switch (vccq) { 10790f34084fSMarius Strobl case vccq_330: 10800f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 10810f34084fSMarius Strobl goto done; 10820f34084fSMarius Strobl hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 10830f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 10840f34084fSMarius Strobl DELAY(5000); 10850f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 10860f34084fSMarius Strobl if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 10870f34084fSMarius Strobl goto done; 10880f34084fSMarius Strobl err = EAGAIN; 10890f34084fSMarius Strobl break; 10900f34084fSMarius Strobl case vccq_180: 10910f34084fSMarius Strobl if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 10920f34084fSMarius Strobl err = EINVAL; 10930f34084fSMarius Strobl goto done; 10940f34084fSMarius Strobl } 10950f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 10960f34084fSMarius Strobl goto done; 10970f34084fSMarius Strobl hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 10980f34084fSMarius Strobl WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 10990f34084fSMarius Strobl DELAY(5000); 11000f34084fSMarius Strobl hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 11010f34084fSMarius Strobl if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 11020f34084fSMarius Strobl goto done; 11030f34084fSMarius Strobl err = EAGAIN; 11040f34084fSMarius Strobl break; 11050f34084fSMarius Strobl default: 11060f34084fSMarius Strobl slot_printf(slot, 11070f34084fSMarius Strobl "Attempt to set unsupported signaling voltage\n"); 11080f34084fSMarius Strobl err = EINVAL; 11090f34084fSMarius Strobl break; 11100f34084fSMarius Strobl } 11110f34084fSMarius Strobl done: 11120f34084fSMarius Strobl sdhci_set_clock(slot, slot->host.ios.clock); 11130f34084fSMarius Strobl SDHCI_UNLOCK(slot); 11140f34084fSMarius Strobl return (err); 11150f34084fSMarius Strobl } 11160f34084fSMarius Strobl 1117a94a63f0SWarner Losh #ifdef MMCCAM 1118a94a63f0SWarner Losh static void 1119a94a63f0SWarner Losh sdhci_req_done(struct sdhci_slot *slot) 1120a94a63f0SWarner Losh { 1121a94a63f0SWarner Losh union ccb *ccb; 1122*15c440e1SWarner Losh 1123a94a63f0SWarner Losh if (sdhci_debug > 1) 1124*15c440e1SWarner Losh slot_printf(slot, "%s\n", __func__); 1125a94a63f0SWarner Losh if (slot->ccb != NULL && slot->curcmd != NULL) { 1126a94a63f0SWarner Losh callout_stop(&slot->timeout_callout); 1127a94a63f0SWarner Losh ccb = slot->ccb; 1128a94a63f0SWarner Losh slot->ccb = NULL; 1129a94a63f0SWarner Losh slot->curcmd = NULL; 1130a94a63f0SWarner Losh 1131a94a63f0SWarner Losh /* Tell CAM the request is finished */ 1132a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1133a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1134a94a63f0SWarner Losh 1135a94a63f0SWarner Losh ccb->ccb_h.status = 1136a94a63f0SWarner Losh (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1137a94a63f0SWarner Losh xpt_done(ccb); 1138a94a63f0SWarner Losh } 1139a94a63f0SWarner Losh } 1140a94a63f0SWarner Losh #else 1141831f5dcfSAlexander Motin static void 1142e64f01a9SIan Lepore sdhci_req_done(struct sdhci_slot *slot) 1143e64f01a9SIan Lepore { 1144e64f01a9SIan Lepore struct mmc_request *req; 1145e64f01a9SIan Lepore 1146e64f01a9SIan Lepore if (slot->req != NULL && slot->curcmd != NULL) { 1147e64f01a9SIan Lepore callout_stop(&slot->timeout_callout); 1148e64f01a9SIan Lepore req = slot->req; 1149e64f01a9SIan Lepore slot->req = NULL; 1150e64f01a9SIan Lepore slot->curcmd = NULL; 1151e64f01a9SIan Lepore req->done(req); 1152e64f01a9SIan Lepore } 1153e64f01a9SIan Lepore } 1154a94a63f0SWarner Losh #endif 1155e64f01a9SIan Lepore 1156e64f01a9SIan Lepore static void 1157e64f01a9SIan Lepore sdhci_timeout(void *arg) 1158e64f01a9SIan Lepore { 1159e64f01a9SIan Lepore struct sdhci_slot *slot = arg; 1160e64f01a9SIan Lepore 1161e64f01a9SIan Lepore if (slot->curcmd != NULL) { 11627e586643SIan Lepore slot_printf(slot, " Controller timeout\n"); 11637e586643SIan Lepore sdhci_dumpregs(slot); 1164a6873fd1SIan Lepore sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1165e64f01a9SIan Lepore slot->curcmd->error = MMC_ERR_TIMEOUT; 1166e64f01a9SIan Lepore sdhci_req_done(slot); 11677e586643SIan Lepore } else { 11687e586643SIan Lepore slot_printf(slot, " Spurious timeout - no active command\n"); 1169e64f01a9SIan Lepore } 1170e64f01a9SIan Lepore } 1171e64f01a9SIan Lepore 1172e64f01a9SIan Lepore static void 1173b440e965SMarius Strobl sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data) 1174831f5dcfSAlexander Motin { 1175831f5dcfSAlexander Motin uint16_t mode; 1176831f5dcfSAlexander Motin 1177831f5dcfSAlexander Motin if (data == NULL) 1178831f5dcfSAlexander Motin return; 1179831f5dcfSAlexander Motin 1180831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 1181831f5dcfSAlexander Motin if (data->len > 512) 1182831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 1183831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1184831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 1185a94a63f0SWarner Losh #ifdef MMCCAM 1186a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1187a94a63f0SWarner Losh mmcio = &slot->ccb->mmcio; 1188a94a63f0SWarner Losh if (mmcio->stop.opcode == MMC_STOP_TRANSMISSION 1189a94a63f0SWarner Losh && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) 1190a94a63f0SWarner Losh mode |= SDHCI_TRNS_ACMD12; 1191a94a63f0SWarner Losh #else 1192915780d7SLuiz Otavio O Souza if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) 1193831f5dcfSAlexander Motin mode |= SDHCI_TRNS_ACMD12; 1194a94a63f0SWarner Losh #endif 1195831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 1196831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 1197831f5dcfSAlexander Motin 1198831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 1199831f5dcfSAlexander Motin } 1200831f5dcfSAlexander Motin 1201831f5dcfSAlexander Motin static void 1202831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1203831f5dcfSAlexander Motin { 1204831f5dcfSAlexander Motin int flags, timeout; 120590993663SIan Lepore uint32_t mask; 1206831f5dcfSAlexander Motin 1207831f5dcfSAlexander Motin slot->curcmd = cmd; 1208831f5dcfSAlexander Motin slot->cmd_done = 0; 1209831f5dcfSAlexander Motin 1210831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 1211831f5dcfSAlexander Motin 1212831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 1213831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1214831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 1215831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1216e64f01a9SIan Lepore sdhci_req_done(slot); 1217831f5dcfSAlexander Motin return; 1218831f5dcfSAlexander Motin } 1219831f5dcfSAlexander Motin 1220b440e965SMarius Strobl /* 1221b440e965SMarius Strobl * Do not issue command if there is no card, clock or power. 1222b440e965SMarius Strobl * Controller will not detect timeout without clock active. 1223b440e965SMarius Strobl */ 12246e37fb2bSIan Lepore if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1225d8208d9eSAlexander Motin slot->power == 0 || 1226d8208d9eSAlexander Motin slot->clock == 0) { 1227a94a63f0SWarner Losh slot_printf(slot, 1228a94a63f0SWarner Losh "Cannot issue a command (power=%d clock=%d)", 1229a94a63f0SWarner Losh slot->power, slot->clock); 1230831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1231e64f01a9SIan Lepore sdhci_req_done(slot); 1232831f5dcfSAlexander Motin return; 1233831f5dcfSAlexander Motin } 1234831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 1235831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 1236831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 1237a94a63f0SWarner Losh if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1238831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 1239831f5dcfSAlexander Motin /* We shouldn't wait for DAT for stop commands. */ 1240a94a63f0SWarner Losh #ifdef MMCCAM 1241a94a63f0SWarner Losh struct ccb_mmcio *mmcio = &slot->ccb->mmcio; 1242a94a63f0SWarner Losh if (cmd == &mmcio->stop) 1243a94a63f0SWarner Losh mask &= ~SDHCI_DAT_INHIBIT; 1244a94a63f0SWarner Losh #else 1245831f5dcfSAlexander Motin if (cmd == slot->req->stop) 1246831f5dcfSAlexander Motin mask &= ~SDHCI_DAT_INHIBIT; 1247a94a63f0SWarner Losh #endif 12488775ab45SIan Lepore /* 12498775ab45SIan Lepore * Wait for bus no more then 250 ms. Typically there will be no wait 12508775ab45SIan Lepore * here at all, but when writing a crash dump we may be bypassing the 12518775ab45SIan Lepore * host platform's interrupt handler, and in some cases that handler 12528775ab45SIan Lepore * may be working around hardware quirks such as not respecting r1b 12538775ab45SIan Lepore * busy indications. In those cases, this wait-loop serves the purpose 12548775ab45SIan Lepore * of waiting for the prior command and data transfers to be done, and 12558775ab45SIan Lepore * SD cards are allowed to take up to 250ms for write and erase ops. 12568775ab45SIan Lepore * (It's usually more like 20-30ms in the real world.) 12578775ab45SIan Lepore */ 12588775ab45SIan Lepore timeout = 250; 125990993663SIan Lepore while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1260831f5dcfSAlexander Motin if (timeout == 0) { 1261831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 1262831f5dcfSAlexander Motin "inhibit bit(s).\n"); 1263831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1264831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 1265e64f01a9SIan Lepore sdhci_req_done(slot); 1266831f5dcfSAlexander Motin return; 1267831f5dcfSAlexander Motin } 1268831f5dcfSAlexander Motin timeout--; 1269831f5dcfSAlexander Motin DELAY(1000); 1270831f5dcfSAlexander Motin } 1271831f5dcfSAlexander Motin 1272831f5dcfSAlexander Motin /* Prepare command flags. */ 1273831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 1274831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 1275831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 1276831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 1277831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 1278831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 1279831f5dcfSAlexander Motin else 1280831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 1281831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 1282831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 1283831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 1284831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 1285a94a63f0SWarner Losh if (cmd->data != NULL) 1286831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 1287831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 1288831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 1289831f5dcfSAlexander Motin /* Prepare data. */ 1290831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 1291831f5dcfSAlexander Motin /* 1292831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 1293831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 1294831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 1295831f5dcfSAlexander Motin */ 1296831f5dcfSAlexander Motin if (slot->data_done == 0) { 1297831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1298831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 1299831f5dcfSAlexander Motin } 1300831f5dcfSAlexander Motin /* Set command argument. */ 1301831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1302831f5dcfSAlexander Motin /* Set data transfer mode. */ 1303831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 1304a94a63f0SWarner Losh if (sdhci_debug > 1) 1305a94a63f0SWarner Losh slot_printf(slot, "Starting command!\n"); 1306831f5dcfSAlexander Motin /* Start command. */ 1307d6b3aaf8SOleksandr Tymoshenko WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1308a6873fd1SIan Lepore /* Start timeout callout. */ 1309ba6fc1c7SLuiz Otavio O Souza callout_reset(&slot->timeout_callout, slot->timeout * hz, 1310ba6fc1c7SLuiz Otavio O Souza sdhci_timeout, slot); 1311831f5dcfSAlexander Motin } 1312831f5dcfSAlexander Motin 1313831f5dcfSAlexander Motin static void 1314831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 1315831f5dcfSAlexander Motin { 1316831f5dcfSAlexander Motin int i; 13171bacf3beSMarius Strobl uint32_t val; 13181bacf3beSMarius Strobl uint8_t extra; 1319831f5dcfSAlexander Motin 1320a94a63f0SWarner Losh if (sdhci_debug > 1) 1321a94a63f0SWarner Losh slot_printf(slot, "%s: called, err %d flags %d\n", 1322a94a63f0SWarner Losh __func__, slot->curcmd->error, slot->curcmd->flags); 1323831f5dcfSAlexander Motin slot->cmd_done = 1; 132472dec079SMarius Strobl /* 132572dec079SMarius Strobl * Interrupt aggregation: Restore command interrupt. 1326831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 132772dec079SMarius Strobl * happened first. 132872dec079SMarius Strobl */ 1329831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 1330831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 1331831f5dcfSAlexander Motin if (slot->curcmd->error) { 1332831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1333831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1334831f5dcfSAlexander Motin sdhci_start(slot); 1335831f5dcfSAlexander Motin return; 1336831f5dcfSAlexander Motin } 1337831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1338831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1339831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1340831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 13411bacf3beSMarius Strobl extra = 0; 1342831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 13431bacf3beSMarius Strobl val = RD4(slot, SDHCI_RESPONSE + i * 4); 13441bacf3beSMarius Strobl if (slot->quirks & 13451bacf3beSMarius Strobl SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1346677ee494SIan Lepore slot->curcmd->resp[3 - i] = val; 1347677ee494SIan Lepore else { 1348677ee494SIan Lepore slot->curcmd->resp[3 - i] = 1349677ee494SIan Lepore (val << 8) | extra; 1350831f5dcfSAlexander Motin extra = val >> 24; 1351831f5dcfSAlexander Motin } 1352677ee494SIan Lepore } 1353831f5dcfSAlexander Motin } else 1354831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1355831f5dcfSAlexander Motin } 1356a94a63f0SWarner Losh if (sdhci_debug > 1) 1357a94a63f0SWarner Losh printf("Resp: %02x %02x %02x %02x\n", 1358a94a63f0SWarner Losh slot->curcmd->resp[0], slot->curcmd->resp[1], 1359a94a63f0SWarner Losh slot->curcmd->resp[2], slot->curcmd->resp[3]); 1360a94a63f0SWarner Losh 1361831f5dcfSAlexander Motin /* If data ready - finish. */ 1362831f5dcfSAlexander Motin if (slot->data_done) 1363831f5dcfSAlexander Motin sdhci_start(slot); 1364831f5dcfSAlexander Motin } 1365831f5dcfSAlexander Motin 1366831f5dcfSAlexander Motin static void 1367831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 1368831f5dcfSAlexander Motin { 1369831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 1370831f5dcfSAlexander Motin uint8_t div; 1371831f5dcfSAlexander Motin 1372831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1373831f5dcfSAlexander Motin slot->data_done = 1; 1374831f5dcfSAlexander Motin return; 1375831f5dcfSAlexander Motin } 1376831f5dcfSAlexander Motin 1377831f5dcfSAlexander Motin slot->data_done = 0; 1378831f5dcfSAlexander Motin 1379831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1380831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1381ceb9e9f7SIan Lepore if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1382ceb9e9f7SIan Lepore div = 0xE; 1383ceb9e9f7SIan Lepore } else { 1384831f5dcfSAlexander Motin target_timeout = 1000000; 1385831f5dcfSAlexander Motin div = 0; 1386831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1387ceb9e9f7SIan Lepore while (current_timeout < target_timeout && div < 0xE) { 1388ceb9e9f7SIan Lepore ++div; 1389831f5dcfSAlexander Motin current_timeout <<= 1; 1390831f5dcfSAlexander Motin } 1391831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1392ceb9e9f7SIan Lepore if (div < 0xE && 1393ceb9e9f7SIan Lepore (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1394ceb9e9f7SIan Lepore ++div; 1395831f5dcfSAlexander Motin } 1396ceb9e9f7SIan Lepore } 1397831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1398831f5dcfSAlexander Motin 1399831f5dcfSAlexander Motin if (data == NULL) 1400831f5dcfSAlexander Motin return; 1401831f5dcfSAlexander Motin 1402831f5dcfSAlexander Motin /* Use DMA if possible. */ 1403831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1404831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1405831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 1406d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1407831f5dcfSAlexander Motin (data->len <= 512)) 1408831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1409831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1410d6b3aaf8SOleksandr Tymoshenko if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1411831f5dcfSAlexander Motin ((data->len) & 0x3)) 1412831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1413831f5dcfSAlexander Motin /* Load DMA buffer. */ 1414831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1415831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1416ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1417ecc2d997SRui Paulo BUS_DMASYNC_PREREAD); 1418831f5dcfSAlexander Motin else { 1419831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 1420ecc2d997SRui Paulo (data->len < DMA_BLOCK_SIZE) ? 1421ecc2d997SRui Paulo data->len : DMA_BLOCK_SIZE); 1422ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1423ecc2d997SRui Paulo BUS_DMASYNC_PREWRITE); 1424831f5dcfSAlexander Motin } 1425831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1426831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1427831f5dcfSAlexander Motin * for the last page and unmask else. */ 1428831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 1429831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1430831f5dcfSAlexander Motin else 1431831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1432831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1433831f5dcfSAlexander Motin } 1434831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1435831f5dcfSAlexander Motin slot->offset = 0; 1436831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 14371bacf3beSMarius Strobl WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, 14381bacf3beSMarius Strobl (data->len < 512) ? data->len : 512)); 1439831f5dcfSAlexander Motin /* Set block count. */ 1440831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1441a94a63f0SWarner Losh 1442a94a63f0SWarner Losh if (sdhci_debug > 1) 1443*15c440e1SWarner Losh slot_printf(slot, "Block size: %02x, count %lu\n", 1444*15c440e1SWarner Losh (unsigned int)SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512) ? data->len : 512), 1445a94a63f0SWarner Losh (unsigned long)(data->len + 511) / 512); 1446831f5dcfSAlexander Motin } 1447831f5dcfSAlexander Motin 1448c3a0f75aSOleksandr Tymoshenko void 1449831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1450831f5dcfSAlexander Motin { 1451831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 14527e6ccea3SMarius Strobl size_t left; 1453831f5dcfSAlexander Motin 1454831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1455ecc2d997SRui Paulo * Auxiliary restore point for the case when data interrupt 1456831f5dcfSAlexander Motin * happened first. */ 1457831f5dcfSAlexander Motin if (!slot->cmd_done) { 1458831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1459831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1460831f5dcfSAlexander Motin } 1461831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1462915780d7SLuiz Otavio O Souza if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1463915780d7SLuiz Otavio O Souza slot->curcmd->data != NULL) { 1464831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 14657e6ccea3SMarius Strobl left = data->len - slot->offset; 1466ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1467ecc2d997SRui Paulo BUS_DMASYNC_POSTREAD); 1468831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1469831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1470831f5dcfSAlexander Motin } else 1471ecc2d997SRui Paulo bus_dmamap_sync(slot->dmatag, slot->dmamap, 1472ecc2d997SRui Paulo BUS_DMASYNC_POSTWRITE); 1473831f5dcfSAlexander Motin } 1474a98788edSIan Lepore slot->data_done = 1; 1475831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1476831f5dcfSAlexander Motin if (slot->curcmd->error) { 1477831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1478831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1479831f5dcfSAlexander Motin sdhci_start(slot); 1480831f5dcfSAlexander Motin return; 1481831f5dcfSAlexander Motin } 1482831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1483831f5dcfSAlexander Motin if (slot->cmd_done) 1484831f5dcfSAlexander Motin sdhci_start(slot); 1485831f5dcfSAlexander Motin } 1486831f5dcfSAlexander Motin 1487a94a63f0SWarner Losh #ifdef MMCCAM 1488a94a63f0SWarner Losh static void 1489a94a63f0SWarner Losh sdhci_start(struct sdhci_slot *slot) 1490a94a63f0SWarner Losh { 1491a94a63f0SWarner Losh union ccb *ccb; 1492a94a63f0SWarner Losh 1493a94a63f0SWarner Losh ccb = slot->ccb; 1494a94a63f0SWarner Losh if (ccb == NULL) 1495a94a63f0SWarner Losh return; 1496a94a63f0SWarner Losh 1497a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 1498a94a63f0SWarner Losh mmcio = &ccb->mmcio; 1499a94a63f0SWarner Losh 1500a94a63f0SWarner Losh if (!(slot->flags & CMD_STARTED)) { 1501a94a63f0SWarner Losh slot->flags |= CMD_STARTED; 1502a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->cmd); 1503a94a63f0SWarner Losh return; 1504a94a63f0SWarner Losh } 1505a94a63f0SWarner Losh 1506a94a63f0SWarner Losh /* 1507a94a63f0SWarner Losh * Old stack doesn't use this! 1508a94a63f0SWarner Losh * Enabling this code causes significant performance degradation 1509a94a63f0SWarner Losh * and IRQ storms on BBB, Wandboard behaves fine. 1510a94a63f0SWarner Losh * Not using this code does no harm... 1511a94a63f0SWarner Losh if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 1512a94a63f0SWarner Losh slot->flags |= STOP_STARTED; 1513a94a63f0SWarner Losh sdhci_start_command(slot, &mmcio->stop); 1514a94a63f0SWarner Losh return; 1515a94a63f0SWarner Losh } 1516a94a63f0SWarner Losh */ 1517a94a63f0SWarner Losh if (sdhci_debug > 1) 1518a94a63f0SWarner Losh slot_printf(slot, "result: %d\n", mmcio->cmd.error); 1519a94a63f0SWarner Losh if (mmcio->cmd.error == 0 && 1520a94a63f0SWarner Losh (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1521a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD); 1522a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_DATA); 1523a94a63f0SWarner Losh } 1524a94a63f0SWarner Losh 1525a94a63f0SWarner Losh sdhci_req_done(slot); 1526a94a63f0SWarner Losh } 1527a94a63f0SWarner Losh #else 1528831f5dcfSAlexander Motin static void 1529831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1530831f5dcfSAlexander Motin { 1531831f5dcfSAlexander Motin struct mmc_request *req; 1532831f5dcfSAlexander Motin 1533831f5dcfSAlexander Motin req = slot->req; 1534831f5dcfSAlexander Motin if (req == NULL) 1535831f5dcfSAlexander Motin return; 1536831f5dcfSAlexander Motin 1537831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1538831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1539831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1540831f5dcfSAlexander Motin return; 1541831f5dcfSAlexander Motin } 1542915780d7SLuiz Otavio O Souza if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 1543915780d7SLuiz Otavio O Souza !(slot->flags & STOP_STARTED) && req->stop) { 1544831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1545831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1546831f5dcfSAlexander Motin return; 1547831f5dcfSAlexander Motin } 15485b69a497SAlexander Motin if (sdhci_debug > 1) 15495b69a497SAlexander Motin slot_printf(slot, "result: %d\n", req->cmd->error); 15505b69a497SAlexander Motin if (!req->cmd->error && 1551915780d7SLuiz Otavio O Souza ((slot->curcmd == req->stop && 1552915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 1553915780d7SLuiz Otavio O Souza (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 1554831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1555831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1556831f5dcfSAlexander Motin } 1557831f5dcfSAlexander Motin 1558e64f01a9SIan Lepore sdhci_req_done(slot); 1559831f5dcfSAlexander Motin } 1560a94a63f0SWarner Losh #endif 1561831f5dcfSAlexander Motin 1562d6b3aaf8SOleksandr Tymoshenko int 1563b440e965SMarius Strobl sdhci_generic_request(device_t brdev __unused, device_t reqdev, 1564b440e965SMarius Strobl struct mmc_request *req) 1565831f5dcfSAlexander Motin { 1566831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1567831f5dcfSAlexander Motin 1568831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1569831f5dcfSAlexander Motin if (slot->req != NULL) { 1570831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1571831f5dcfSAlexander Motin return (EBUSY); 1572831f5dcfSAlexander Motin } 15735b69a497SAlexander Motin if (sdhci_debug > 1) { 15741bacf3beSMarius Strobl slot_printf(slot, 15751bacf3beSMarius Strobl "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1576831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 15775b69a497SAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 15785b69a497SAlexander Motin (req->cmd->data)?req->cmd->data->flags:0); 15795b69a497SAlexander Motin } 1580831f5dcfSAlexander Motin slot->req = req; 1581831f5dcfSAlexander Motin slot->flags = 0; 1582831f5dcfSAlexander Motin sdhci_start(slot); 1583831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1584bea2dca2SAlexander Motin if (dumping) { 1585bea2dca2SAlexander Motin while (slot->req != NULL) { 1586d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(slot); 1587bea2dca2SAlexander Motin DELAY(10); 1588bea2dca2SAlexander Motin } 1589bea2dca2SAlexander Motin } 1590831f5dcfSAlexander Motin return (0); 1591831f5dcfSAlexander Motin } 1592831f5dcfSAlexander Motin 1593d6b3aaf8SOleksandr Tymoshenko int 1594b440e965SMarius Strobl sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 1595831f5dcfSAlexander Motin { 1596831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1597831f5dcfSAlexander Motin uint32_t val; 1598831f5dcfSAlexander Motin 1599831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1600831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 1601831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1602831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 1603831f5dcfSAlexander Motin } 1604831f5dcfSAlexander Motin 1605d6b3aaf8SOleksandr Tymoshenko int 1606b440e965SMarius Strobl sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 1607831f5dcfSAlexander Motin { 1608831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1609831f5dcfSAlexander Motin int err = 0; 1610831f5dcfSAlexander Motin 1611831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1612831f5dcfSAlexander Motin while (slot->bus_busy) 1613d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1614831f5dcfSAlexander Motin slot->bus_busy++; 1615831f5dcfSAlexander Motin /* Activate led. */ 1616831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1617831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1618831f5dcfSAlexander Motin return (err); 1619831f5dcfSAlexander Motin } 1620831f5dcfSAlexander Motin 1621d6b3aaf8SOleksandr Tymoshenko int 1622b440e965SMarius Strobl sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 1623831f5dcfSAlexander Motin { 1624831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1625831f5dcfSAlexander Motin 1626831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1627831f5dcfSAlexander Motin /* Deactivate led. */ 1628831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1629831f5dcfSAlexander Motin slot->bus_busy--; 1630831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1631d493985aSAlexander Motin wakeup(slot); 1632831f5dcfSAlexander Motin return (0); 1633831f5dcfSAlexander Motin } 1634831f5dcfSAlexander Motin 1635831f5dcfSAlexander Motin static void 1636831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1637831f5dcfSAlexander Motin { 1638831f5dcfSAlexander Motin 1639831f5dcfSAlexander Motin if (!slot->curcmd) { 1640831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 1641831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1642831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1643831f5dcfSAlexander Motin return; 1644831f5dcfSAlexander Motin } 1645831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 1646831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1647831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 1648831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1649831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1650831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 1651831f5dcfSAlexander Motin 1652831f5dcfSAlexander Motin sdhci_finish_command(slot); 1653831f5dcfSAlexander Motin } 1654831f5dcfSAlexander Motin 1655831f5dcfSAlexander Motin static void 1656831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1657831f5dcfSAlexander Motin { 16581bacf3beSMarius Strobl struct mmc_data *data; 1659*15c440e1SWarner Losh size_t left; 1660831f5dcfSAlexander Motin 1661831f5dcfSAlexander Motin if (!slot->curcmd) { 1662831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1663831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1664831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1665831f5dcfSAlexander Motin return; 1666831f5dcfSAlexander Motin } 1667831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1668831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1669831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1670831f5dcfSAlexander Motin "there is no active data operation.\n", 1671831f5dcfSAlexander Motin intmask); 1672831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1673831f5dcfSAlexander Motin return; 1674831f5dcfSAlexander Motin } 1675831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 1676831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1677acbaa69fSOleksandr Tymoshenko else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1678831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1679831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1680831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1681831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 1682831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1683831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 1684831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1685831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 1686831f5dcfSAlexander Motin } 1687831f5dcfSAlexander Motin if (slot->curcmd->error) { 1688831f5dcfSAlexander Motin /* No need to continue after any error. */ 1689a98788edSIan Lepore goto done; 1690831f5dcfSAlexander Motin } 1691831f5dcfSAlexander Motin 1692831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 1693c3a0f75aSOleksandr Tymoshenko if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 1694c3a0f75aSOleksandr Tymoshenko if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 1695c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 16961bacf3beSMarius Strobl SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 16971bacf3beSMarius Strobl &intmask); 1698c3a0f75aSOleksandr Tymoshenko slot->flags |= PLATFORM_DATA_STARTED; 1699c3a0f75aSOleksandr Tymoshenko } else 1700831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 1701c3a0f75aSOleksandr Tymoshenko } 1702831f5dcfSAlexander Motin /* Handle DMA border. */ 1703831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 17041bacf3beSMarius Strobl data = slot->curcmd->data; 1705831f5dcfSAlexander Motin 1706831f5dcfSAlexander Motin /* Unload DMA buffer ... */ 1707831f5dcfSAlexander Motin left = data->len - slot->offset; 1708831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1709831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1710831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 1711831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1712831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1713831f5dcfSAlexander Motin } else { 1714831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1715831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 1716831f5dcfSAlexander Motin } 1717831f5dcfSAlexander Motin /* ... and reload it again. */ 1718831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 1719831f5dcfSAlexander Motin left = data->len - slot->offset; 1720831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1721831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1722831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 1723831f5dcfSAlexander Motin } else { 1724831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1725831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE); 1726831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1727831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 1728831f5dcfSAlexander Motin } 1729831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1730831f5dcfSAlexander Motin * for the last page. */ 1731831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 1732831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1733831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1734831f5dcfSAlexander Motin } 1735831f5dcfSAlexander Motin /* Restart DMA. */ 1736831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1737831f5dcfSAlexander Motin } 1738831f5dcfSAlexander Motin /* We have got all data. */ 1739c3a0f75aSOleksandr Tymoshenko if (intmask & SDHCI_INT_DATA_END) { 1740c3a0f75aSOleksandr Tymoshenko if (slot->flags & PLATFORM_DATA_STARTED) { 1741c3a0f75aSOleksandr Tymoshenko slot->flags &= ~PLATFORM_DATA_STARTED; 1742c3a0f75aSOleksandr Tymoshenko SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1743c3a0f75aSOleksandr Tymoshenko } else 1744831f5dcfSAlexander Motin sdhci_finish_data(slot); 1745831f5dcfSAlexander Motin } 1746a98788edSIan Lepore done: 1747a98788edSIan Lepore if (slot->curcmd != NULL && slot->curcmd->error != 0) { 1748a98788edSIan Lepore if (slot->flags & PLATFORM_DATA_STARTED) { 1749a98788edSIan Lepore slot->flags &= ~PLATFORM_DATA_STARTED; 1750a98788edSIan Lepore SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1751a98788edSIan Lepore } else 1752a98788edSIan Lepore sdhci_finish_data(slot); 1753a98788edSIan Lepore } 1754c3a0f75aSOleksandr Tymoshenko } 1755831f5dcfSAlexander Motin 1756831f5dcfSAlexander Motin static void 1757831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot) 1758831f5dcfSAlexander Motin { 1759831f5dcfSAlexander Motin uint16_t err; 1760831f5dcfSAlexander Motin 1761831f5dcfSAlexander Motin err = RD4(slot, SDHCI_ACMD12_ERR); 1762831f5dcfSAlexander Motin if (!slot->curcmd) { 1763831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1764831f5dcfSAlexander Motin "there is no active command.\n", err); 1765831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1766831f5dcfSAlexander Motin return; 1767831f5dcfSAlexander Motin } 1768831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1769831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1770831f5dcfSAlexander Motin } 1771831f5dcfSAlexander Motin 1772d6b3aaf8SOleksandr Tymoshenko void 1773d6b3aaf8SOleksandr Tymoshenko sdhci_generic_intr(struct sdhci_slot *slot) 1774831f5dcfSAlexander Motin { 17752b96b955SJustin Hibbits uint32_t intmask, present; 1776831f5dcfSAlexander Motin 1777831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1778831f5dcfSAlexander Motin /* Read slot interrupt status. */ 1779831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 1780831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 1781831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1782d6b3aaf8SOleksandr Tymoshenko return; 1783831f5dcfSAlexander Motin } 17845b69a497SAlexander Motin if (sdhci_debug > 2) 17855b69a497SAlexander Motin slot_printf(slot, "Interrupt %#x\n", intmask); 17865b69a497SAlexander Motin 1787831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 1788831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 1789639f59f0SIan Lepore present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 17902b96b955SJustin Hibbits slot->intmask &= 17912b96b955SJustin Hibbits ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 17922b96b955SJustin Hibbits slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 17932b96b955SJustin Hibbits SDHCI_INT_CARD_INSERT; 17942b96b955SJustin Hibbits WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 17952b96b955SJustin Hibbits WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1796831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 1797831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1798b8bf08b1SIan Lepore sdhci_handle_card_present_locked(slot, present); 1799831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1800831f5dcfSAlexander Motin } 1801831f5dcfSAlexander Motin /* Handle command interrupts. */ 1802831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 1803831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1804831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1805831f5dcfSAlexander Motin } 1806831f5dcfSAlexander Motin /* Handle data interrupts. */ 1807831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 1808831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 18097e6ccea3SMarius Strobl /* Don't call data_irq in case of errored command. */ 18107e586643SIan Lepore if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 1811831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1812831f5dcfSAlexander Motin } 1813831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 1814831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 1815831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1816831f5dcfSAlexander Motin sdhci_acmd_irq(slot); 1817831f5dcfSAlexander Motin } 1818831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1819831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ACMD12ERR; 1820831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ERROR; 1821831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 1822831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 1823831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1824831f5dcfSAlexander Motin slot_printf(slot, 1825831f5dcfSAlexander Motin "Card is consuming too much power!\n"); 1826831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_BUS_POWER; 1827831f5dcfSAlexander Motin } 1828a94a63f0SWarner Losh 1829831f5dcfSAlexander Motin /* The rest is unknown. */ 1830831f5dcfSAlexander Motin if (intmask) { 1831831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 1832831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1833831f5dcfSAlexander Motin intmask); 1834831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1835831f5dcfSAlexander Motin } 1836831f5dcfSAlexander Motin 1837831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1838831f5dcfSAlexander Motin } 1839831f5dcfSAlexander Motin 1840d6b3aaf8SOleksandr Tymoshenko int 18411bacf3beSMarius Strobl sdhci_generic_read_ivar(device_t bus, device_t child, int which, 18421bacf3beSMarius Strobl uintptr_t *result) 1843831f5dcfSAlexander Motin { 1844831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1845831f5dcfSAlexander Motin 1846831f5dcfSAlexander Motin switch (which) { 1847831f5dcfSAlexander Motin default: 1848831f5dcfSAlexander Motin return (EINVAL); 1849831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1850bcd91d25SJayachandran C. *result = slot->host.ios.bus_mode; 1851831f5dcfSAlexander Motin break; 1852831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1853bcd91d25SJayachandran C. *result = slot->host.ios.bus_width; 1854831f5dcfSAlexander Motin break; 1855831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1856bcd91d25SJayachandran C. *result = slot->host.ios.chip_select; 1857831f5dcfSAlexander Motin break; 1858831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1859bcd91d25SJayachandran C. *result = slot->host.ios.clock; 1860831f5dcfSAlexander Motin break; 1861831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1862bcd91d25SJayachandran C. *result = slot->host.f_min; 1863831f5dcfSAlexander Motin break; 1864831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 1865bcd91d25SJayachandran C. *result = slot->host.f_max; 1866831f5dcfSAlexander Motin break; 1867831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1868bcd91d25SJayachandran C. *result = slot->host.host_ocr; 1869831f5dcfSAlexander Motin break; 1870831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1871bcd91d25SJayachandran C. *result = slot->host.mode; 1872831f5dcfSAlexander Motin break; 1873831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1874bcd91d25SJayachandran C. *result = slot->host.ocr; 1875831f5dcfSAlexander Motin break; 1876831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1877bcd91d25SJayachandran C. *result = slot->host.ios.power_mode; 1878831f5dcfSAlexander Motin break; 1879831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1880bcd91d25SJayachandran C. *result = slot->host.ios.vdd; 1881831f5dcfSAlexander Motin break; 18820f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 18830f34084fSMarius Strobl *result = slot->host.ios.vccq; 18840f34084fSMarius Strobl break; 1885831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1886bcd91d25SJayachandran C. *result = slot->host.caps; 1887831f5dcfSAlexander Motin break; 1888831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1889bcd91d25SJayachandran C. *result = slot->host.ios.timing; 1890831f5dcfSAlexander Motin break; 18913a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1892bcd91d25SJayachandran C. *result = 65535; 18933a4a2557SAlexander Motin break; 189472dec079SMarius Strobl case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 189572dec079SMarius Strobl /* 189672dec079SMarius Strobl * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 189772dec079SMarius Strobl */ 189872dec079SMarius Strobl *result = 1000000; 189972dec079SMarius Strobl break; 1900831f5dcfSAlexander Motin } 1901831f5dcfSAlexander Motin return (0); 1902831f5dcfSAlexander Motin } 1903831f5dcfSAlexander Motin 1904d6b3aaf8SOleksandr Tymoshenko int 19051bacf3beSMarius Strobl sdhci_generic_write_ivar(device_t bus, device_t child, int which, 19061bacf3beSMarius Strobl uintptr_t value) 1907831f5dcfSAlexander Motin { 1908831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1909b440e965SMarius Strobl uint32_t clock, max_clock; 1910b440e965SMarius Strobl int i; 1911831f5dcfSAlexander Motin 1912*15c440e1SWarner Losh if (sdhci_debug > 1) 1913*15c440e1SWarner Losh slot_printf(slot, "%s: var=%d\n", __func__, which); 1914831f5dcfSAlexander Motin switch (which) { 1915831f5dcfSAlexander Motin default: 1916831f5dcfSAlexander Motin return (EINVAL); 1917831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1918831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 1919831f5dcfSAlexander Motin break; 1920831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1921831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 1922831f5dcfSAlexander Motin break; 1923831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1924831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 1925831f5dcfSAlexander Motin break; 1926831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1927831f5dcfSAlexander Motin if (value > 0) { 192857677a3aSOleksandr Tymoshenko max_clock = slot->max_clk; 192957677a3aSOleksandr Tymoshenko clock = max_clock; 193057677a3aSOleksandr Tymoshenko 193157677a3aSOleksandr Tymoshenko if (slot->version < SDHCI_SPEC_300) { 193257677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_200_MAX_DIVIDER; 193357677a3aSOleksandr Tymoshenko i <<= 1) { 1934831f5dcfSAlexander Motin if (clock <= value) 1935831f5dcfSAlexander Motin break; 1936831f5dcfSAlexander Motin clock >>= 1; 1937831f5dcfSAlexander Motin } 1938b440e965SMarius Strobl } else { 193957677a3aSOleksandr Tymoshenko for (i = 0; i < SDHCI_300_MAX_DIVIDER; 194057677a3aSOleksandr Tymoshenko i += 2) { 194157677a3aSOleksandr Tymoshenko if (clock <= value) 194257677a3aSOleksandr Tymoshenko break; 194357677a3aSOleksandr Tymoshenko clock = max_clock / (i + 2); 194457677a3aSOleksandr Tymoshenko } 194557677a3aSOleksandr Tymoshenko } 194657677a3aSOleksandr Tymoshenko 1947831f5dcfSAlexander Motin slot->host.ios.clock = clock; 1948831f5dcfSAlexander Motin } else 1949831f5dcfSAlexander Motin slot->host.ios.clock = 0; 1950831f5dcfSAlexander Motin break; 1951831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1952831f5dcfSAlexander Motin slot->host.mode = value; 1953831f5dcfSAlexander Motin break; 1954831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1955831f5dcfSAlexander Motin slot->host.ocr = value; 1956831f5dcfSAlexander Motin break; 1957831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1958831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 1959831f5dcfSAlexander Motin break; 1960831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1961831f5dcfSAlexander Motin slot->host.ios.vdd = value; 1962831f5dcfSAlexander Motin break; 19630f34084fSMarius Strobl case MMCBR_IVAR_VCCQ: 19640f34084fSMarius Strobl slot->host.ios.vccq = value; 19650f34084fSMarius Strobl break; 1966831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1967831f5dcfSAlexander Motin slot->host.ios.timing = value; 1968831f5dcfSAlexander Motin break; 1969831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1970831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1971831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1972831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 19733a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1974831f5dcfSAlexander Motin return (EINVAL); 1975831f5dcfSAlexander Motin } 1976831f5dcfSAlexander Motin return (0); 1977831f5dcfSAlexander Motin } 1978831f5dcfSAlexander Motin 1979*15c440e1SWarner Losh #ifdef MMCCAM 1980a94a63f0SWarner Losh /* CAM-related functions */ 1981a94a63f0SWarner Losh #include <cam/cam.h> 1982a94a63f0SWarner Losh #include <cam/cam_ccb.h> 1983a94a63f0SWarner Losh #include <cam/cam_debug.h> 1984a94a63f0SWarner Losh #include <cam/cam_sim.h> 1985a94a63f0SWarner Losh #include <cam/cam_xpt_sim.h> 1986a94a63f0SWarner Losh 1987a94a63f0SWarner Losh void 1988a94a63f0SWarner Losh sdhci_cam_start_slot(struct sdhci_slot *slot) 1989a94a63f0SWarner Losh { 1990a94a63f0SWarner Losh if ((slot->devq = cam_simq_alloc(1)) == NULL) { 1991a94a63f0SWarner Losh goto fail; 1992a94a63f0SWarner Losh } 1993a94a63f0SWarner Losh 1994a94a63f0SWarner Losh mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 1995a94a63f0SWarner Losh slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 1996a94a63f0SWarner Losh "sdhci_slot", slot, device_get_unit(slot->bus), 1997a94a63f0SWarner Losh &slot->sim_mtx, 1, 1, slot->devq); 1998a94a63f0SWarner Losh 1999a94a63f0SWarner Losh if (slot->sim == NULL) { 2000a94a63f0SWarner Losh cam_simq_free(slot->devq); 2001a94a63f0SWarner Losh slot_printf(slot, "cannot allocate CAM SIM\n"); 2002a94a63f0SWarner Losh goto fail; 2003a94a63f0SWarner Losh } 2004a94a63f0SWarner Losh 2005a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2006a94a63f0SWarner Losh if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2007a94a63f0SWarner Losh slot_printf(slot, 2008a94a63f0SWarner Losh "cannot register SCSI pass-through bus\n"); 2009a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2010a94a63f0SWarner Losh cam_simq_free(slot->devq); 2011a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2012a94a63f0SWarner Losh goto fail; 2013a94a63f0SWarner Losh } 2014a94a63f0SWarner Losh 2015a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2016a94a63f0SWarner Losh /* End CAM-specific init */ 2017a94a63f0SWarner Losh slot->card_present = 0; 2018a94a63f0SWarner Losh sdhci_card_task(slot, 0); 2019a94a63f0SWarner Losh return; 2020a94a63f0SWarner Losh 2021a94a63f0SWarner Losh fail: 2022a94a63f0SWarner Losh if (slot->sim != NULL) { 2023a94a63f0SWarner Losh mtx_lock(&slot->sim_mtx); 2024a94a63f0SWarner Losh xpt_bus_deregister(cam_sim_path(slot->sim)); 2025a94a63f0SWarner Losh cam_sim_free(slot->sim, FALSE); 2026a94a63f0SWarner Losh mtx_unlock(&slot->sim_mtx); 2027a94a63f0SWarner Losh } 2028a94a63f0SWarner Losh 2029a94a63f0SWarner Losh if (slot->devq != NULL) 2030a94a63f0SWarner Losh cam_simq_free(slot->devq); 2031a94a63f0SWarner Losh } 2032a94a63f0SWarner Losh 2033a94a63f0SWarner Losh static void 2034a94a63f0SWarner Losh sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb) 2035a94a63f0SWarner Losh { 2036a94a63f0SWarner Losh struct sdhci_slot *slot; 2037a94a63f0SWarner Losh 2038a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2039a94a63f0SWarner Losh 2040a94a63f0SWarner Losh sdhci_cam_request(slot, ccb); 2041a94a63f0SWarner Losh } 2042a94a63f0SWarner Losh 2043a94a63f0SWarner Losh void 2044a94a63f0SWarner Losh sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2045a94a63f0SWarner Losh { 2046a94a63f0SWarner Losh struct sdhci_slot *slot; 2047a94a63f0SWarner Losh 2048a94a63f0SWarner Losh slot = cam_sim_softc(sim); 2049a94a63f0SWarner Losh if (slot == NULL) { 2050a94a63f0SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2051a94a63f0SWarner Losh xpt_done(ccb); 2052a94a63f0SWarner Losh return; 2053a94a63f0SWarner Losh } 2054a94a63f0SWarner Losh 2055a94a63f0SWarner Losh mtx_assert(&slot->sim_mtx, MA_OWNED); 2056a94a63f0SWarner Losh 2057a94a63f0SWarner Losh switch (ccb->ccb_h.func_code) { 2058a94a63f0SWarner Losh case XPT_PATH_INQ: 2059a94a63f0SWarner Losh { 2060a94a63f0SWarner Losh struct ccb_pathinq *cpi; 2061a94a63f0SWarner Losh 2062a94a63f0SWarner Losh cpi = &ccb->cpi; 2063a94a63f0SWarner Losh cpi->version_num = 1; 2064a94a63f0SWarner Losh cpi->hba_inquiry = 0; 2065a94a63f0SWarner Losh cpi->target_sprt = 0; 2066a94a63f0SWarner Losh cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 2067a94a63f0SWarner Losh cpi->hba_eng_cnt = 0; 2068a94a63f0SWarner Losh cpi->max_target = 0; 2069a94a63f0SWarner Losh cpi->max_lun = 0; 2070a94a63f0SWarner Losh cpi->initiator_id = 1; 2071a94a63f0SWarner Losh cpi->maxio = MAXPHYS; 2072a94a63f0SWarner Losh strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2073a94a63f0SWarner Losh strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN); 2074a94a63f0SWarner Losh strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2075a94a63f0SWarner Losh cpi->unit_number = cam_sim_unit(sim); 2076a94a63f0SWarner Losh cpi->bus_id = cam_sim_bus(sim); 2077a94a63f0SWarner Losh cpi->base_transfer_speed = 100; /* XXX WTF? */ 2078a94a63f0SWarner Losh cpi->protocol = PROTO_MMCSD; 2079a94a63f0SWarner Losh cpi->protocol_version = SCSI_REV_0; 2080a94a63f0SWarner Losh cpi->transport = XPORT_MMCSD; 2081a94a63f0SWarner Losh cpi->transport_version = 0; 2082a94a63f0SWarner Losh 2083a94a63f0SWarner Losh cpi->ccb_h.status = CAM_REQ_CMP; 2084a94a63f0SWarner Losh break; 2085a94a63f0SWarner Losh } 2086a94a63f0SWarner Losh case XPT_GET_TRAN_SETTINGS: 2087a94a63f0SWarner Losh { 2088a94a63f0SWarner Losh struct ccb_trans_settings *cts = &ccb->cts; 2089a94a63f0SWarner Losh 2090a94a63f0SWarner Losh if (sdhci_debug > 1) 2091a94a63f0SWarner Losh slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2092a94a63f0SWarner Losh 2093a94a63f0SWarner Losh cts->protocol = PROTO_MMCSD; 2094a94a63f0SWarner Losh cts->protocol_version = 1; 2095a94a63f0SWarner Losh cts->transport = XPORT_MMCSD; 2096a94a63f0SWarner Losh cts->transport_version = 1; 2097a94a63f0SWarner Losh cts->xport_specific.valid = 0; 2098a94a63f0SWarner Losh cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2099a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2100a94a63f0SWarner Losh cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2101a94a63f0SWarner Losh cts->proto_specific.mmc.host_caps = slot->host.caps; 2102a94a63f0SWarner Losh memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2103a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2104a94a63f0SWarner Losh break; 2105a94a63f0SWarner Losh } 2106a94a63f0SWarner Losh case XPT_SET_TRAN_SETTINGS: 2107a94a63f0SWarner Losh { 2108a94a63f0SWarner Losh if (sdhci_debug > 1) 2109a94a63f0SWarner Losh slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2110a94a63f0SWarner Losh sdhci_cam_settran_settings(slot, ccb); 2111a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2112a94a63f0SWarner Losh break; 2113a94a63f0SWarner Losh } 2114a94a63f0SWarner Losh case XPT_RESET_BUS: 2115a94a63f0SWarner Losh if (sdhci_debug > 1) 2116a94a63f0SWarner Losh slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2117a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_CMP; 2118a94a63f0SWarner Losh break; 2119a94a63f0SWarner Losh case XPT_MMC_IO: 2120a94a63f0SWarner Losh /* 2121a94a63f0SWarner Losh * Here is the HW-dependent part of 2122a94a63f0SWarner Losh * sending the command to the underlying h/w 2123a94a63f0SWarner Losh * At some point in the future an interrupt comes. 2124a94a63f0SWarner Losh * Then the request will be marked as completed. 2125a94a63f0SWarner Losh */ 2126a94a63f0SWarner Losh if (sdhci_debug > 1) 2127a94a63f0SWarner Losh slot_printf(slot, "Got XPT_MMC_IO\n"); 2128a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INPROG; 2129a94a63f0SWarner Losh 2130a94a63f0SWarner Losh sdhci_cam_handle_mmcio(sim, ccb); 2131a94a63f0SWarner Losh return; 2132a94a63f0SWarner Losh /* NOTREACHED */ 2133a94a63f0SWarner Losh break; 2134a94a63f0SWarner Losh default: 2135a94a63f0SWarner Losh ccb->ccb_h.status = CAM_REQ_INVALID; 2136a94a63f0SWarner Losh break; 2137a94a63f0SWarner Losh } 2138a94a63f0SWarner Losh xpt_done(ccb); 2139a94a63f0SWarner Losh return; 2140a94a63f0SWarner Losh } 2141a94a63f0SWarner Losh 2142a94a63f0SWarner Losh void 2143a94a63f0SWarner Losh sdhci_cam_poll(struct cam_sim *sim) 2144a94a63f0SWarner Losh { 2145a94a63f0SWarner Losh return; 2146a94a63f0SWarner Losh } 2147a94a63f0SWarner Losh 2148a94a63f0SWarner Losh int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) { 2149a94a63f0SWarner Losh int max_clock, clock, i; 2150a94a63f0SWarner Losh 2151a94a63f0SWarner Losh if (proposed_clock == 0) 2152a94a63f0SWarner Losh return 0; 2153a94a63f0SWarner Losh max_clock = slot->max_clk; 2154a94a63f0SWarner Losh clock = max_clock; 2155a94a63f0SWarner Losh 2156a94a63f0SWarner Losh if (slot->version < SDHCI_SPEC_300) { 2157a94a63f0SWarner Losh for (i = 0; i < SDHCI_200_MAX_DIVIDER; 2158a94a63f0SWarner Losh i <<= 1) { 2159a94a63f0SWarner Losh if (clock <= proposed_clock) 2160a94a63f0SWarner Losh break; 2161a94a63f0SWarner Losh clock >>= 1; 2162a94a63f0SWarner Losh } 2163a94a63f0SWarner Losh } else { 2164a94a63f0SWarner Losh for (i = 0; i < SDHCI_300_MAX_DIVIDER; 2165a94a63f0SWarner Losh i += 2) { 2166a94a63f0SWarner Losh if (clock <= proposed_clock) 2167a94a63f0SWarner Losh break; 2168a94a63f0SWarner Losh clock = max_clock / (i + 2); 2169a94a63f0SWarner Losh } 2170a94a63f0SWarner Losh } 2171a94a63f0SWarner Losh return clock; 2172a94a63f0SWarner Losh } 2173a94a63f0SWarner Losh 2174a94a63f0SWarner Losh int 2175a94a63f0SWarner Losh sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2176a94a63f0SWarner Losh { 2177a94a63f0SWarner Losh struct mmc_ios *ios; 2178a94a63f0SWarner Losh struct mmc_ios *new_ios; 2179a94a63f0SWarner Losh struct ccb_trans_settings_mmc *cts; 2180a94a63f0SWarner Losh 2181a94a63f0SWarner Losh ios = &slot->host.ios; 2182a94a63f0SWarner Losh 2183a94a63f0SWarner Losh cts = &ccb->cts.proto_specific.mmc; 2184a94a63f0SWarner Losh new_ios = &cts->ios; 2185a94a63f0SWarner Losh 2186a94a63f0SWarner Losh /* Update only requested fields */ 2187a94a63f0SWarner Losh if (cts->ios_valid & MMC_CLK) { 2188a94a63f0SWarner Losh ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2189a94a63f0SWarner Losh slot_printf(slot, "Clock => %d\n", ios->clock); 2190a94a63f0SWarner Losh } 2191a94a63f0SWarner Losh if (cts->ios_valid & MMC_VDD) { 2192a94a63f0SWarner Losh ios->vdd = new_ios->vdd; 2193a94a63f0SWarner Losh slot_printf(slot, "VDD => %d\n", ios->vdd); 2194a94a63f0SWarner Losh } 2195a94a63f0SWarner Losh if (cts->ios_valid & MMC_CS) { 2196a94a63f0SWarner Losh ios->chip_select = new_ios->chip_select; 2197a94a63f0SWarner Losh slot_printf(slot, "CS => %d\n", ios->chip_select); 2198a94a63f0SWarner Losh } 2199a94a63f0SWarner Losh if (cts->ios_valid & MMC_BW) { 2200a94a63f0SWarner Losh ios->bus_width = new_ios->bus_width; 2201a94a63f0SWarner Losh slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2202a94a63f0SWarner Losh } 2203a94a63f0SWarner Losh if (cts->ios_valid & MMC_PM) { 2204a94a63f0SWarner Losh ios->power_mode = new_ios->power_mode; 2205a94a63f0SWarner Losh slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2206a94a63f0SWarner Losh } 2207a94a63f0SWarner Losh if (cts->ios_valid & MMC_BT) { 2208a94a63f0SWarner Losh ios->timing = new_ios->timing; 2209a94a63f0SWarner Losh slot_printf(slot, "Timing => %d\n", ios->timing); 2210a94a63f0SWarner Losh } 2211a94a63f0SWarner Losh if (cts->ios_valid & MMC_BM) { 2212a94a63f0SWarner Losh ios->bus_mode = new_ios->bus_mode; 2213a94a63f0SWarner Losh slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2214a94a63f0SWarner Losh } 2215a94a63f0SWarner Losh 2216a94a63f0SWarner Losh /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2217a94a63f0SWarner Losh return (sdhci_cam_update_ios(slot)); 2218a94a63f0SWarner Losh } 2219a94a63f0SWarner Losh 2220a94a63f0SWarner Losh int 2221a94a63f0SWarner Losh sdhci_cam_update_ios(struct sdhci_slot *slot) 2222a94a63f0SWarner Losh { 2223a94a63f0SWarner Losh struct mmc_ios *ios = &slot->host.ios; 2224a94a63f0SWarner Losh 2225a94a63f0SWarner Losh slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2226a94a63f0SWarner Losh __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2227a94a63f0SWarner Losh SDHCI_LOCK(slot); 2228a94a63f0SWarner Losh /* Do full reset on bus power down to clear from any state. */ 2229a94a63f0SWarner Losh if (ios->power_mode == power_off) { 2230a94a63f0SWarner Losh WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2231a94a63f0SWarner Losh sdhci_init(slot); 2232a94a63f0SWarner Losh } 2233a94a63f0SWarner Losh /* Configure the bus. */ 2234a94a63f0SWarner Losh sdhci_set_clock(slot, ios->clock); 2235a94a63f0SWarner Losh sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2236a94a63f0SWarner Losh if (ios->bus_width == bus_width_8) { 2237a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2238a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2239a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_4) { 2240a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2241a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2242a94a63f0SWarner Losh } else if (ios->bus_width == bus_width_1) { 2243a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2244a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2245a94a63f0SWarner Losh } else { 2246a94a63f0SWarner Losh panic("Invalid bus width: %d", ios->bus_width); 2247a94a63f0SWarner Losh } 2248a94a63f0SWarner Losh if (ios->timing == bus_timing_hs && 2249a94a63f0SWarner Losh !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2250a94a63f0SWarner Losh slot->hostctrl |= SDHCI_CTRL_HISPD; 2251a94a63f0SWarner Losh else 2252a94a63f0SWarner Losh slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2253a94a63f0SWarner Losh WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2254a94a63f0SWarner Losh /* Some controllers like reset after bus changes. */ 2255a94a63f0SWarner Losh if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2256a94a63f0SWarner Losh sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2257a94a63f0SWarner Losh 2258a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2259a94a63f0SWarner Losh return (0); 2260a94a63f0SWarner Losh } 2261a94a63f0SWarner Losh 2262a94a63f0SWarner Losh int 2263a94a63f0SWarner Losh sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2264a94a63f0SWarner Losh { 2265a94a63f0SWarner Losh struct ccb_mmcio *mmcio; 2266a94a63f0SWarner Losh 2267a94a63f0SWarner Losh mmcio = &ccb->mmcio; 2268a94a63f0SWarner Losh 2269a94a63f0SWarner Losh SDHCI_LOCK(slot); 2270a94a63f0SWarner Losh /* if (slot->req != NULL) { 2271a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2272a94a63f0SWarner Losh return (EBUSY); 2273a94a63f0SWarner Losh } 2274a94a63f0SWarner Losh */ 2275a94a63f0SWarner Losh if (sdhci_debug > 1) { 2276a94a63f0SWarner Losh slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2277a94a63f0SWarner Losh mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2278a94a63f0SWarner Losh mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2279a94a63f0SWarner Losh mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 2280a94a63f0SWarner Losh } 2281a94a63f0SWarner Losh if (mmcio->cmd.data != NULL) { 2282a94a63f0SWarner Losh if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2283a94a63f0SWarner Losh panic("data->len = %d, data->flags = %d -- something is b0rked", 2284a94a63f0SWarner Losh (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2285a94a63f0SWarner Losh } 2286a94a63f0SWarner Losh slot->ccb = ccb; 2287a94a63f0SWarner Losh slot->flags = 0; 2288a94a63f0SWarner Losh sdhci_start(slot); 2289a94a63f0SWarner Losh SDHCI_UNLOCK(slot); 2290a94a63f0SWarner Losh if (dumping) { 2291a94a63f0SWarner Losh while (slot->ccb != NULL) { 2292a94a63f0SWarner Losh sdhci_generic_intr(slot); 2293a94a63f0SWarner Losh DELAY(10); 2294a94a63f0SWarner Losh } 2295a94a63f0SWarner Losh } 2296a94a63f0SWarner Losh return (0); 2297a94a63f0SWarner Losh } 2298*15c440e1SWarner Losh #endif /* MMCCAM */ 2299a94a63f0SWarner Losh 2300d6b3aaf8SOleksandr Tymoshenko MODULE_VERSION(sdhci, 1); 2301