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> 32831f5dcfSAlexander Motin #include <sys/conf.h> 33831f5dcfSAlexander Motin #include <sys/kernel.h> 34831f5dcfSAlexander Motin #include <sys/lock.h> 35831f5dcfSAlexander Motin #include <sys/module.h> 36831f5dcfSAlexander Motin #include <sys/mutex.h> 37831f5dcfSAlexander Motin #include <sys/resource.h> 38831f5dcfSAlexander Motin #include <sys/rman.h> 39831f5dcfSAlexander Motin #include <sys/taskqueue.h> 40831f5dcfSAlexander Motin 41831f5dcfSAlexander Motin #include <dev/pci/pcireg.h> 42831f5dcfSAlexander Motin #include <dev/pci/pcivar.h> 43831f5dcfSAlexander Motin 44831f5dcfSAlexander Motin #include <machine/bus.h> 45831f5dcfSAlexander Motin #include <machine/resource.h> 46831f5dcfSAlexander Motin #include <machine/stdarg.h> 47831f5dcfSAlexander Motin 48831f5dcfSAlexander Motin #include <dev/mmc/bridge.h> 49831f5dcfSAlexander Motin #include <dev/mmc/mmcreg.h> 50831f5dcfSAlexander Motin #include <dev/mmc/mmcbrvar.h> 51831f5dcfSAlexander Motin 52831f5dcfSAlexander Motin #include "mmcbr_if.h" 53831f5dcfSAlexander Motin #include "sdhci.h" 54831f5dcfSAlexander Motin 55831f5dcfSAlexander Motin #define DMA_BLOCK_SIZE 4096 56831f5dcfSAlexander Motin #define DMA_BOUNDARY 0 /* DMA reload every 4K */ 57831f5dcfSAlexander Motin 58831f5dcfSAlexander Motin /* Controller doesn't honor resets unless we touch the clock register */ 59831f5dcfSAlexander Motin #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0) 60831f5dcfSAlexander Motin /* Controller really supports DMA */ 61831f5dcfSAlexander Motin #define SDHCI_QUIRK_FORCE_DMA (1<<1) 62831f5dcfSAlexander Motin /* Controller has unusable DMA engine */ 63831f5dcfSAlexander Motin #define SDHCI_QUIRK_BROKEN_DMA (1<<2) 64831f5dcfSAlexander Motin /* Controller doesn't like to be reset when there is no card inserted. */ 65831f5dcfSAlexander Motin #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<3) 66831f5dcfSAlexander Motin /* Controller has flaky internal state so reset it on each ios change */ 67831f5dcfSAlexander Motin #define SDHCI_QUIRK_RESET_ON_IOS (1<<4) 68831f5dcfSAlexander Motin /* Controller can only DMA chunk sizes that are a multiple of 32 bits */ 69831f5dcfSAlexander Motin #define SDHCI_QUIRK_32BIT_DMA_SIZE (1<<5) 70831f5dcfSAlexander Motin /* Controller needs to be reset after each request to stay stable */ 71831f5dcfSAlexander Motin #define SDHCI_QUIRK_RESET_AFTER_REQUEST (1<<6) 72831f5dcfSAlexander Motin /* Controller has an off-by-one issue with timeout value */ 73831f5dcfSAlexander Motin #define SDHCI_QUIRK_INCR_TIMEOUT_CONTROL (1<<7) 74831f5dcfSAlexander Motin /* Controller has broken read timings */ 75831f5dcfSAlexander Motin #define SDHCI_QUIRK_BROKEN_TIMINGS (1<<8) 76831f5dcfSAlexander Motin 77831f5dcfSAlexander Motin static const struct sdhci_device { 78831f5dcfSAlexander Motin uint32_t model; 79831f5dcfSAlexander Motin uint16_t subvendor; 80831f5dcfSAlexander Motin char *desc; 81831f5dcfSAlexander Motin u_int quirks; 82831f5dcfSAlexander Motin } sdhci_devices[] = { 83831f5dcfSAlexander Motin { 0x08221180, 0xffff, "RICOH R5C822 SD", 84831f5dcfSAlexander Motin SDHCI_QUIRK_FORCE_DMA }, 85831f5dcfSAlexander Motin { 0x8034104c, 0xffff, "TI XX21/XX11 SD", 86831f5dcfSAlexander Motin SDHCI_QUIRK_FORCE_DMA }, 87831f5dcfSAlexander Motin { 0x05501524, 0xffff, "ENE CB712 SD", 88831f5dcfSAlexander Motin SDHCI_QUIRK_BROKEN_TIMINGS }, 89831f5dcfSAlexander Motin { 0x05511524, 0xffff, "ENE CB712 SD 2", 90831f5dcfSAlexander Motin SDHCI_QUIRK_BROKEN_TIMINGS }, 91831f5dcfSAlexander Motin { 0x07501524, 0xffff, "ENE CB714 SD", 92831f5dcfSAlexander Motin SDHCI_QUIRK_RESET_ON_IOS | 93831f5dcfSAlexander Motin SDHCI_QUIRK_BROKEN_TIMINGS }, 94831f5dcfSAlexander Motin { 0x07511524, 0xffff, "ENE CB714 SD 2", 95831f5dcfSAlexander Motin SDHCI_QUIRK_RESET_ON_IOS | 96831f5dcfSAlexander Motin SDHCI_QUIRK_BROKEN_TIMINGS }, 97831f5dcfSAlexander Motin { 0x410111ab, 0xffff, "Marvell CaFe SD", 98831f5dcfSAlexander Motin SDHCI_QUIRK_INCR_TIMEOUT_CONTROL }, 99831f5dcfSAlexander Motin { 0x2381197B, 0xffff, "JMicron JMB38X SD", 100831f5dcfSAlexander Motin SDHCI_QUIRK_32BIT_DMA_SIZE | 101831f5dcfSAlexander Motin SDHCI_QUIRK_RESET_AFTER_REQUEST }, 102831f5dcfSAlexander Motin { 0, 0xffff, NULL, 103831f5dcfSAlexander Motin 0 } 104831f5dcfSAlexander Motin }; 105831f5dcfSAlexander Motin 106831f5dcfSAlexander Motin struct sdhci_softc; 107831f5dcfSAlexander Motin 108831f5dcfSAlexander Motin struct sdhci_slot { 109831f5dcfSAlexander Motin struct sdhci_softc *sc; 110831f5dcfSAlexander Motin device_t dev; /* Slot device */ 111831f5dcfSAlexander Motin u_char num; /* Slot number */ 112831f5dcfSAlexander Motin u_char opt; /* Slot options */ 113831f5dcfSAlexander Motin #define SDHCI_HAVE_DMA 1 114831f5dcfSAlexander Motin uint32_t max_clk; /* Max possible freq */ 115831f5dcfSAlexander Motin uint32_t timeout_clk; /* Timeout freq */ 116831f5dcfSAlexander Motin struct resource *mem_res; /* Memory resource */ 117831f5dcfSAlexander Motin int mem_rid; 118831f5dcfSAlexander Motin bus_dma_tag_t dmatag; 119831f5dcfSAlexander Motin bus_dmamap_t dmamap; 120831f5dcfSAlexander Motin u_char *dmamem; 121831f5dcfSAlexander Motin bus_addr_t paddr; /* DMA buffer address */ 122831f5dcfSAlexander Motin struct task card_task; /* Card presence check task */ 123831f5dcfSAlexander Motin struct callout card_callout; /* Card insert delay callout */ 124831f5dcfSAlexander Motin struct mmc_host host; /* Host parameters */ 125831f5dcfSAlexander Motin struct mmc_request *req; /* Current request */ 126831f5dcfSAlexander Motin struct mmc_command *curcmd; /* Current command of current request */ 127831f5dcfSAlexander Motin 128831f5dcfSAlexander Motin uint32_t intmask; /* Current interrupt mask */ 129831f5dcfSAlexander Motin uint32_t clock; /* Current clock freq. */ 130831f5dcfSAlexander Motin size_t offset; /* Data buffer offset */ 131831f5dcfSAlexander Motin uint8_t hostctrl; /* Current host control register */ 132831f5dcfSAlexander Motin u_char power; /* Current power */ 133831f5dcfSAlexander Motin u_char bus_busy; /* Bus busy status */ 134831f5dcfSAlexander Motin u_char cmd_done; /* CMD command part done flag */ 135831f5dcfSAlexander Motin u_char data_done; /* DAT command part done flag */ 136831f5dcfSAlexander Motin u_char flags; /* Request execution flags */ 137831f5dcfSAlexander Motin #define CMD_STARTED 1 138831f5dcfSAlexander Motin #define STOP_STARTED 2 139831f5dcfSAlexander Motin #define SDHCI_USE_DMA 4 /* Use DMA for this req. */ 140831f5dcfSAlexander Motin struct mtx mtx; /* Slot mutex */ 141831f5dcfSAlexander Motin }; 142831f5dcfSAlexander Motin 143831f5dcfSAlexander Motin struct sdhci_softc { 144831f5dcfSAlexander Motin device_t dev; /* Controller device */ 145831f5dcfSAlexander Motin u_int quirks; /* Chip specific quirks */ 146831f5dcfSAlexander Motin struct resource *irq_res; /* IRQ resource */ 147831f5dcfSAlexander Motin int irq_rid; 148831f5dcfSAlexander Motin void *intrhand; /* Interrupt handle */ 149831f5dcfSAlexander Motin 150831f5dcfSAlexander Motin int num_slots; /* Number of slots on this controller */ 151831f5dcfSAlexander Motin struct sdhci_slot slots[6]; 152831f5dcfSAlexander Motin }; 153831f5dcfSAlexander Motin 154831f5dcfSAlexander Motin static inline uint8_t 155831f5dcfSAlexander Motin RD1(struct sdhci_slot *slot, bus_size_t off) 156831f5dcfSAlexander Motin { 157831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 158831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 159831f5dcfSAlexander Motin return bus_read_1(slot->mem_res, off); 160831f5dcfSAlexander Motin } 161831f5dcfSAlexander Motin 162831f5dcfSAlexander Motin static inline void 163831f5dcfSAlexander Motin WR1(struct sdhci_slot *slot, bus_size_t off, uint8_t val) 164831f5dcfSAlexander Motin { 165831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 166831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 167831f5dcfSAlexander Motin bus_write_1(slot->mem_res, off, val); 168831f5dcfSAlexander Motin } 169831f5dcfSAlexander Motin 170831f5dcfSAlexander Motin static inline uint16_t 171831f5dcfSAlexander Motin RD2(struct sdhci_slot *slot, bus_size_t off) 172831f5dcfSAlexander Motin { 173831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 174831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 175831f5dcfSAlexander Motin return bus_read_2(slot->mem_res, off); 176831f5dcfSAlexander Motin } 177831f5dcfSAlexander Motin 178831f5dcfSAlexander Motin static inline void 179831f5dcfSAlexander Motin WR2(struct sdhci_slot *slot, bus_size_t off, uint16_t val) 180831f5dcfSAlexander Motin { 181831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 182831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 183831f5dcfSAlexander Motin bus_write_2(slot->mem_res, off, val); 184831f5dcfSAlexander Motin } 185831f5dcfSAlexander Motin 186831f5dcfSAlexander Motin static inline uint32_t 187831f5dcfSAlexander Motin RD4(struct sdhci_slot *slot, bus_size_t off) 188831f5dcfSAlexander Motin { 189831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 190831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 191831f5dcfSAlexander Motin return bus_read_4(slot->mem_res, off); 192831f5dcfSAlexander Motin } 193831f5dcfSAlexander Motin 194831f5dcfSAlexander Motin static inline void 195831f5dcfSAlexander Motin WR4(struct sdhci_slot *slot, bus_size_t off, uint32_t val) 196831f5dcfSAlexander Motin { 197831f5dcfSAlexander Motin bus_barrier(slot->mem_res, 0, 0xFF, 198831f5dcfSAlexander Motin BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 199831f5dcfSAlexander Motin bus_write_4(slot->mem_res, off, val); 200831f5dcfSAlexander Motin } 201831f5dcfSAlexander Motin 202831f5dcfSAlexander Motin /* bus entry points */ 203831f5dcfSAlexander Motin static int sdhci_probe(device_t dev); 204831f5dcfSAlexander Motin static int sdhci_attach(device_t dev); 205831f5dcfSAlexander Motin static int sdhci_detach(device_t dev); 206831f5dcfSAlexander Motin static void sdhci_intr(void *); 207831f5dcfSAlexander Motin 208831f5dcfSAlexander Motin static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 209831f5dcfSAlexander Motin static void sdhci_start(struct sdhci_slot *slot); 210831f5dcfSAlexander Motin static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 211831f5dcfSAlexander Motin 212831f5dcfSAlexander Motin static void sdhci_card_task(void *, int); 213831f5dcfSAlexander Motin 214831f5dcfSAlexander Motin /* helper routines */ 215831f5dcfSAlexander Motin #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 216831f5dcfSAlexander Motin #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 217831f5dcfSAlexander Motin #define SDHCI_LOCK_INIT(_slot) \ 218831f5dcfSAlexander Motin mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 219831f5dcfSAlexander Motin #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 220831f5dcfSAlexander Motin #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 221831f5dcfSAlexander Motin #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 222831f5dcfSAlexander Motin 223831f5dcfSAlexander Motin static int 224831f5dcfSAlexander Motin slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 225831f5dcfSAlexander Motin { 226831f5dcfSAlexander Motin va_list ap; 227831f5dcfSAlexander Motin int retval; 228831f5dcfSAlexander Motin 229831f5dcfSAlexander Motin retval = printf("%s-slot%d: ", 230831f5dcfSAlexander Motin device_get_nameunit(slot->sc->dev), slot->num); 231831f5dcfSAlexander Motin 232831f5dcfSAlexander Motin va_start(ap, fmt); 233831f5dcfSAlexander Motin retval += vprintf(fmt, ap); 234831f5dcfSAlexander Motin va_end(ap); 235831f5dcfSAlexander Motin return (retval); 236831f5dcfSAlexander Motin } 237831f5dcfSAlexander Motin 238831f5dcfSAlexander Motin static void 239831f5dcfSAlexander Motin sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 240831f5dcfSAlexander Motin { 241831f5dcfSAlexander Motin if (error != 0) { 242831f5dcfSAlexander Motin printf("getaddr: error %d\n", error); 243831f5dcfSAlexander Motin return; 244831f5dcfSAlexander Motin } 245831f5dcfSAlexander Motin *(bus_addr_t *)arg = segs[0].ds_addr; 246831f5dcfSAlexander Motin } 247831f5dcfSAlexander Motin 248831f5dcfSAlexander Motin static void 249831f5dcfSAlexander Motin sdhci_dumpregs(struct sdhci_slot *slot) 250831f5dcfSAlexander Motin { 251831f5dcfSAlexander Motin slot_printf(slot, 252831f5dcfSAlexander Motin "============== REGISTER DUMP ==============\n"); 253831f5dcfSAlexander Motin 254831f5dcfSAlexander Motin slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 255831f5dcfSAlexander Motin RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 256831f5dcfSAlexander Motin slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 257831f5dcfSAlexander Motin RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 258831f5dcfSAlexander Motin slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 259831f5dcfSAlexander Motin RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 260831f5dcfSAlexander Motin slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 261831f5dcfSAlexander Motin RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 262831f5dcfSAlexander Motin slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 263831f5dcfSAlexander Motin RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 264831f5dcfSAlexander Motin slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 265831f5dcfSAlexander Motin RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 266831f5dcfSAlexander Motin slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 267831f5dcfSAlexander Motin RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 268831f5dcfSAlexander Motin slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 269831f5dcfSAlexander Motin RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 270831f5dcfSAlexander Motin slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n", 271831f5dcfSAlexander Motin RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS)); 272831f5dcfSAlexander Motin slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n", 273831f5dcfSAlexander Motin RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT)); 274831f5dcfSAlexander Motin 275831f5dcfSAlexander Motin slot_printf(slot, 276831f5dcfSAlexander Motin "===========================================\n"); 277831f5dcfSAlexander Motin } 278831f5dcfSAlexander Motin 279831f5dcfSAlexander Motin static void 280831f5dcfSAlexander Motin sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 281831f5dcfSAlexander Motin { 282831f5dcfSAlexander Motin int timeout; 283831f5dcfSAlexander Motin uint8_t res; 284831f5dcfSAlexander Motin 285831f5dcfSAlexander Motin if (slot->sc->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 286831f5dcfSAlexander Motin if (!(RD4(slot, SDHCI_PRESENT_STATE) & 287831f5dcfSAlexander Motin SDHCI_CARD_PRESENT)) 288831f5dcfSAlexander Motin return; 289831f5dcfSAlexander Motin } 290831f5dcfSAlexander Motin 291831f5dcfSAlexander Motin /* Some controllers need this kick or reset won't work. */ 292831f5dcfSAlexander Motin if ((mask & SDHCI_RESET_ALL) == 0 && 293831f5dcfSAlexander Motin (slot->sc->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 294831f5dcfSAlexander Motin uint32_t clock; 295831f5dcfSAlexander Motin 296831f5dcfSAlexander Motin /* This is to force an update */ 297831f5dcfSAlexander Motin clock = slot->clock; 298831f5dcfSAlexander Motin slot->clock = 0; 299831f5dcfSAlexander Motin sdhci_set_clock(slot, clock); 300831f5dcfSAlexander Motin } 301831f5dcfSAlexander Motin 302831f5dcfSAlexander Motin WR1(slot, SDHCI_SOFTWARE_RESET, mask); 303831f5dcfSAlexander Motin 304d8208d9eSAlexander Motin if (mask & SDHCI_RESET_ALL) { 305831f5dcfSAlexander Motin slot->clock = 0; 306d8208d9eSAlexander Motin slot->power = 0; 307d8208d9eSAlexander Motin } 308831f5dcfSAlexander Motin 309831f5dcfSAlexander Motin /* Wait max 100 ms */ 310831f5dcfSAlexander Motin timeout = 100; 311831f5dcfSAlexander Motin /* Controller clears the bits when it's done */ 312831f5dcfSAlexander Motin while ((res = RD1(slot, SDHCI_SOFTWARE_RESET)) & mask) { 313831f5dcfSAlexander Motin if (timeout == 0) { 314831f5dcfSAlexander Motin slot_printf(slot, 315831f5dcfSAlexander Motin "Reset 0x%x never completed - 0x%x.\n", 316831f5dcfSAlexander Motin (int)mask, (int)res); 317831f5dcfSAlexander Motin sdhci_dumpregs(slot); 318831f5dcfSAlexander Motin return; 319831f5dcfSAlexander Motin } 320831f5dcfSAlexander Motin timeout--; 321831f5dcfSAlexander Motin DELAY(1000); 322831f5dcfSAlexander Motin } 323831f5dcfSAlexander Motin } 324831f5dcfSAlexander Motin 325831f5dcfSAlexander Motin static void 326831f5dcfSAlexander Motin sdhci_init(struct sdhci_slot *slot) 327831f5dcfSAlexander Motin { 328831f5dcfSAlexander Motin 329831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 330831f5dcfSAlexander Motin 331831f5dcfSAlexander Motin /* Enable interrupts. */ 332831f5dcfSAlexander Motin slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 333831f5dcfSAlexander Motin SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 334831f5dcfSAlexander Motin SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 335831f5dcfSAlexander Motin SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | 336831f5dcfSAlexander Motin SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 337831f5dcfSAlexander Motin SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 338831f5dcfSAlexander Motin SDHCI_INT_ACMD12ERR; 339831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 340831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 341831f5dcfSAlexander Motin } 342831f5dcfSAlexander Motin 343831f5dcfSAlexander Motin static void 344831f5dcfSAlexander Motin sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 345831f5dcfSAlexander Motin { 346831f5dcfSAlexander Motin uint32_t res; 347831f5dcfSAlexander Motin uint16_t clk; 348831f5dcfSAlexander Motin int timeout; 349831f5dcfSAlexander Motin 350831f5dcfSAlexander Motin if (clock == slot->clock) 351831f5dcfSAlexander Motin return; 352831f5dcfSAlexander Motin slot->clock = clock; 353831f5dcfSAlexander Motin 354831f5dcfSAlexander Motin /* Turn off the clock. */ 355831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, 0); 356831f5dcfSAlexander Motin /* If no clock requested - left it so. */ 357831f5dcfSAlexander Motin if (clock == 0) 358831f5dcfSAlexander Motin return; 359831f5dcfSAlexander Motin /* Looking for highest freq <= clock. */ 360831f5dcfSAlexander Motin res = slot->max_clk; 361831f5dcfSAlexander Motin for (clk = 1; clk < 256; clk <<= 1) { 362831f5dcfSAlexander Motin if (res <= clock) 363831f5dcfSAlexander Motin break; 364831f5dcfSAlexander Motin res >>= 1; 365831f5dcfSAlexander Motin } 366831f5dcfSAlexander Motin /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 367831f5dcfSAlexander Motin clk >>= 1; 368831f5dcfSAlexander Motin /* Now we have got divider, set it. */ 369831f5dcfSAlexander Motin clk <<= SDHCI_DIVIDER_SHIFT; 370831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 371831f5dcfSAlexander Motin /* Enable clock. */ 372831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_INT_EN; 373831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 374831f5dcfSAlexander Motin /* Wait up to 10 ms until it stabilize. */ 375831f5dcfSAlexander Motin timeout = 10; 376831f5dcfSAlexander Motin while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 377831f5dcfSAlexander Motin & SDHCI_CLOCK_INT_STABLE)) { 378831f5dcfSAlexander Motin if (timeout == 0) { 379831f5dcfSAlexander Motin slot_printf(slot, 380831f5dcfSAlexander Motin "Internal clock never stabilised.\n"); 381831f5dcfSAlexander Motin sdhci_dumpregs(slot); 382831f5dcfSAlexander Motin return; 383831f5dcfSAlexander Motin } 384831f5dcfSAlexander Motin timeout--; 385831f5dcfSAlexander Motin DELAY(1000); 386831f5dcfSAlexander Motin } 387831f5dcfSAlexander Motin /* Pass clock signal to the bus. */ 388831f5dcfSAlexander Motin clk |= SDHCI_CLOCK_CARD_EN; 389831f5dcfSAlexander Motin WR2(slot, SDHCI_CLOCK_CONTROL, clk); 390831f5dcfSAlexander Motin } 391831f5dcfSAlexander Motin 392831f5dcfSAlexander Motin static void 393831f5dcfSAlexander Motin sdhci_set_power(struct sdhci_slot *slot, u_char power) 394831f5dcfSAlexander Motin { 395831f5dcfSAlexander Motin uint8_t pwr; 396831f5dcfSAlexander Motin 397831f5dcfSAlexander Motin if (slot->power == power) 398831f5dcfSAlexander Motin return; 399831f5dcfSAlexander Motin slot->power = power; 400831f5dcfSAlexander Motin 401831f5dcfSAlexander Motin /* Turn off the power. */ 402831f5dcfSAlexander Motin pwr = 0; 403831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 404831f5dcfSAlexander Motin /* If power down requested - left it so. */ 405831f5dcfSAlexander Motin if (power == 0) 406831f5dcfSAlexander Motin return; 407831f5dcfSAlexander Motin /* Set voltage. */ 408831f5dcfSAlexander Motin switch (1 << power) { 409831f5dcfSAlexander Motin case MMC_OCR_LOW_VOLTAGE: 410831f5dcfSAlexander Motin pwr |= SDHCI_POWER_180; 411831f5dcfSAlexander Motin break; 412831f5dcfSAlexander Motin case MMC_OCR_290_300: 413831f5dcfSAlexander Motin case MMC_OCR_300_310: 414831f5dcfSAlexander Motin pwr |= SDHCI_POWER_300; 415831f5dcfSAlexander Motin break; 416831f5dcfSAlexander Motin case MMC_OCR_320_330: 417831f5dcfSAlexander Motin case MMC_OCR_330_340: 418831f5dcfSAlexander Motin pwr |= SDHCI_POWER_330; 419831f5dcfSAlexander Motin break; 420831f5dcfSAlexander Motin } 421831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 422831f5dcfSAlexander Motin /* Turn on the power. */ 423831f5dcfSAlexander Motin pwr |= SDHCI_POWER_ON; 424831f5dcfSAlexander Motin WR1(slot, SDHCI_POWER_CONTROL, pwr); 425831f5dcfSAlexander Motin } 426831f5dcfSAlexander Motin 427831f5dcfSAlexander Motin static void 428831f5dcfSAlexander Motin sdhci_read_block_pio(struct sdhci_slot *slot) 429831f5dcfSAlexander Motin { 430831f5dcfSAlexander Motin uint32_t data; 431831f5dcfSAlexander Motin char *buffer; 432831f5dcfSAlexander Motin size_t left; 433831f5dcfSAlexander Motin 434831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 435831f5dcfSAlexander Motin buffer += slot->offset; 436831f5dcfSAlexander Motin /* Transfer one block at a time. */ 437831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 438831f5dcfSAlexander Motin slot->offset += left; 439831f5dcfSAlexander Motin 440831f5dcfSAlexander Motin /* If we are too fast, broken controllers return zeroes. */ 441831f5dcfSAlexander Motin if (slot->sc->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 442831f5dcfSAlexander Motin DELAY(10); 443831f5dcfSAlexander Motin /* Handle unalligned and alligned buffer cases. */ 444831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 445831f5dcfSAlexander Motin while (left > 3) { 446831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 447831f5dcfSAlexander Motin buffer[0] = data; 448831f5dcfSAlexander Motin buffer[1] = (data >> 8); 449831f5dcfSAlexander Motin buffer[2] = (data >> 16); 450831f5dcfSAlexander Motin buffer[3] = (data >> 24); 451831f5dcfSAlexander Motin buffer += 4; 452831f5dcfSAlexander Motin left -= 4; 453831f5dcfSAlexander Motin } 454831f5dcfSAlexander Motin } else { 455831f5dcfSAlexander Motin bus_read_multi_stream_4(slot->mem_res, SDHCI_BUFFER, 456831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 457831f5dcfSAlexander Motin left &= 3; 458831f5dcfSAlexander Motin } 459831f5dcfSAlexander Motin /* Handle uneven size case. */ 460831f5dcfSAlexander Motin if (left > 0) { 461831f5dcfSAlexander Motin data = RD4(slot, SDHCI_BUFFER); 462831f5dcfSAlexander Motin while (left > 0) { 463831f5dcfSAlexander Motin *(buffer++) = data; 464831f5dcfSAlexander Motin data >>= 8; 465831f5dcfSAlexander Motin left--; 466831f5dcfSAlexander Motin } 467831f5dcfSAlexander Motin } 468831f5dcfSAlexander Motin } 469831f5dcfSAlexander Motin 470831f5dcfSAlexander Motin static void 471831f5dcfSAlexander Motin sdhci_write_block_pio(struct sdhci_slot *slot) 472831f5dcfSAlexander Motin { 473831f5dcfSAlexander Motin uint32_t data = 0; 474831f5dcfSAlexander Motin char *buffer; 475831f5dcfSAlexander Motin size_t left; 476831f5dcfSAlexander Motin 477831f5dcfSAlexander Motin buffer = slot->curcmd->data->data; 478831f5dcfSAlexander Motin buffer += slot->offset; 479831f5dcfSAlexander Motin /* Transfer one block at a time. */ 480831f5dcfSAlexander Motin left = min(512, slot->curcmd->data->len - slot->offset); 481831f5dcfSAlexander Motin slot->offset += left; 482831f5dcfSAlexander Motin 483831f5dcfSAlexander Motin /* Handle unalligned and alligned buffer cases. */ 484831f5dcfSAlexander Motin if ((intptr_t)buffer & 3) { 485831f5dcfSAlexander Motin while (left > 3) { 486831f5dcfSAlexander Motin data = buffer[0] + 487831f5dcfSAlexander Motin (buffer[1] << 8) + 488831f5dcfSAlexander Motin (buffer[2] << 16) + 489831f5dcfSAlexander Motin (buffer[3] << 24); 490831f5dcfSAlexander Motin left -= 4; 491831f5dcfSAlexander Motin buffer += 4; 492831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 493831f5dcfSAlexander Motin } 494831f5dcfSAlexander Motin } else { 495831f5dcfSAlexander Motin bus_write_multi_stream_4(slot->mem_res, SDHCI_BUFFER, 496831f5dcfSAlexander Motin (uint32_t *)buffer, left >> 2); 497831f5dcfSAlexander Motin left &= 3; 498831f5dcfSAlexander Motin } 499831f5dcfSAlexander Motin /* Handle uneven size case. */ 500831f5dcfSAlexander Motin if (left > 0) { 501831f5dcfSAlexander Motin while (left > 0) { 502831f5dcfSAlexander Motin data <<= 8; 503831f5dcfSAlexander Motin data += *(buffer++); 504831f5dcfSAlexander Motin left--; 505831f5dcfSAlexander Motin } 506831f5dcfSAlexander Motin WR4(slot, SDHCI_BUFFER, data); 507831f5dcfSAlexander Motin } 508831f5dcfSAlexander Motin } 509831f5dcfSAlexander Motin 510831f5dcfSAlexander Motin static void 511831f5dcfSAlexander Motin sdhci_transfer_pio(struct sdhci_slot *slot) 512831f5dcfSAlexander Motin { 513831f5dcfSAlexander Motin 514831f5dcfSAlexander Motin /* Read as many blocks as possible. */ 515831f5dcfSAlexander Motin if (slot->curcmd->data->flags & MMC_DATA_READ) { 516831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 517831f5dcfSAlexander Motin SDHCI_DATA_AVAILABLE) { 518831f5dcfSAlexander Motin sdhci_read_block_pio(slot); 519831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 520831f5dcfSAlexander Motin break; 521831f5dcfSAlexander Motin } 522831f5dcfSAlexander Motin } else { 523831f5dcfSAlexander Motin while (RD4(slot, SDHCI_PRESENT_STATE) & 524831f5dcfSAlexander Motin SDHCI_SPACE_AVAILABLE) { 525831f5dcfSAlexander Motin sdhci_write_block_pio(slot); 526831f5dcfSAlexander Motin if (slot->offset >= slot->curcmd->data->len) 527831f5dcfSAlexander Motin break; 528831f5dcfSAlexander Motin } 529831f5dcfSAlexander Motin } 530831f5dcfSAlexander Motin } 531831f5dcfSAlexander Motin 532831f5dcfSAlexander Motin static void 533831f5dcfSAlexander Motin sdhci_card_delay(void *arg) 534831f5dcfSAlexander Motin { 535831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 536831f5dcfSAlexander Motin 537831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 538831f5dcfSAlexander Motin } 539831f5dcfSAlexander Motin 540831f5dcfSAlexander Motin static void 541831f5dcfSAlexander Motin sdhci_card_task(void *arg, int pending) 542831f5dcfSAlexander Motin { 543831f5dcfSAlexander Motin struct sdhci_slot *slot = arg; 544831f5dcfSAlexander Motin 545831f5dcfSAlexander Motin SDHCI_LOCK(slot); 546831f5dcfSAlexander Motin if (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) { 547831f5dcfSAlexander Motin if (slot->dev == NULL) { 548831f5dcfSAlexander Motin /* If card is present - attach mmc bus. */ 549831f5dcfSAlexander Motin slot->dev = device_add_child(slot->sc->dev, "mmc", -1); 550831f5dcfSAlexander Motin device_set_ivars(slot->dev, slot); 551831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 552831f5dcfSAlexander Motin device_probe_and_attach(slot->dev); 553831f5dcfSAlexander Motin } else 554831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 555831f5dcfSAlexander Motin } else { 556831f5dcfSAlexander Motin if (slot->dev != NULL) { 557831f5dcfSAlexander Motin /* If no card present - detach mmc bus. */ 558831f5dcfSAlexander Motin device_t d = slot->dev; 559831f5dcfSAlexander Motin slot->dev = NULL; 560831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 561831f5dcfSAlexander Motin device_delete_child(slot->sc->dev, d); 562831f5dcfSAlexander Motin } else 563831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 564831f5dcfSAlexander Motin } 565831f5dcfSAlexander Motin } 566831f5dcfSAlexander Motin 567831f5dcfSAlexander Motin static int 568831f5dcfSAlexander Motin sdhci_probe(device_t dev) 569831f5dcfSAlexander Motin { 570831f5dcfSAlexander Motin uint32_t model; 571831f5dcfSAlexander Motin uint16_t subvendor; 572831f5dcfSAlexander Motin uint8_t class, subclass; 573831f5dcfSAlexander Motin int i, result; 574831f5dcfSAlexander Motin 575831f5dcfSAlexander Motin model = (uint32_t)pci_get_device(dev) << 16; 576831f5dcfSAlexander Motin model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff; 577831f5dcfSAlexander Motin subvendor = pci_get_subvendor(dev); 578831f5dcfSAlexander Motin class = pci_get_class(dev); 579831f5dcfSAlexander Motin subclass = pci_get_subclass(dev); 580831f5dcfSAlexander Motin 581831f5dcfSAlexander Motin result = ENXIO; 582831f5dcfSAlexander Motin for (i = 0; sdhci_devices[i].model != 0; i++) { 583831f5dcfSAlexander Motin if (sdhci_devices[i].model == model && 584831f5dcfSAlexander Motin (sdhci_devices[i].subvendor == 0xffff || 585831f5dcfSAlexander Motin sdhci_devices[i].subvendor == subvendor)) { 586831f5dcfSAlexander Motin device_set_desc(dev, sdhci_devices[i].desc); 587831f5dcfSAlexander Motin result = BUS_PROBE_DEFAULT; 588831f5dcfSAlexander Motin break; 589831f5dcfSAlexander Motin } 590831f5dcfSAlexander Motin } 591831f5dcfSAlexander Motin if (result == ENXIO && class == PCIC_BASEPERIPH && 592831f5dcfSAlexander Motin subclass == PCIS_BASEPERIPH_SDHC) { 593831f5dcfSAlexander Motin device_set_desc(dev, "Generic SD HCI"); 594831f5dcfSAlexander Motin result = BUS_PROBE_GENERIC; 595831f5dcfSAlexander Motin } 596831f5dcfSAlexander Motin 597831f5dcfSAlexander Motin return (result); 598831f5dcfSAlexander Motin } 599831f5dcfSAlexander Motin 600831f5dcfSAlexander Motin static int 601831f5dcfSAlexander Motin sdhci_attach(device_t dev) 602831f5dcfSAlexander Motin { 603831f5dcfSAlexander Motin struct sdhci_softc *sc = device_get_softc(dev); 604831f5dcfSAlexander Motin uint32_t model; 605831f5dcfSAlexander Motin uint16_t subvendor; 606831f5dcfSAlexander Motin uint8_t class, subclass, progif; 607831f5dcfSAlexander Motin int err, slots, bar, i; 608831f5dcfSAlexander Motin 609831f5dcfSAlexander Motin sc->dev = dev; 610831f5dcfSAlexander Motin model = (uint32_t)pci_get_device(dev) << 16; 611831f5dcfSAlexander Motin model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff; 612831f5dcfSAlexander Motin subvendor = pci_get_subvendor(dev); 613831f5dcfSAlexander Motin class = pci_get_class(dev); 614831f5dcfSAlexander Motin subclass = pci_get_subclass(dev); 615831f5dcfSAlexander Motin progif = pci_get_progif(dev); 616831f5dcfSAlexander Motin /* Apply chip specific quirks. */ 617831f5dcfSAlexander Motin for (i = 0; sdhci_devices[i].model != 0; i++) { 618831f5dcfSAlexander Motin if (sdhci_devices[i].model == model && 619831f5dcfSAlexander Motin (sdhci_devices[i].subvendor == 0xffff || 620831f5dcfSAlexander Motin sdhci_devices[i].subvendor == subvendor)) { 621831f5dcfSAlexander Motin sc->quirks = sdhci_devices[i].quirks; 622831f5dcfSAlexander Motin break; 623831f5dcfSAlexander Motin } 624831f5dcfSAlexander Motin } 625831f5dcfSAlexander Motin /* Read slots info from PCI registers. */ 626831f5dcfSAlexander Motin slots = pci_read_config(dev, PCI_SLOT_INFO, 1); 627831f5dcfSAlexander Motin bar = PCI_SLOT_INFO_FIRST_BAR(slots); 628831f5dcfSAlexander Motin slots = PCI_SLOT_INFO_SLOTS(slots); 629831f5dcfSAlexander Motin if (slots > 6 || bar > 5) { 630831f5dcfSAlexander Motin device_printf(dev, "Incorrect slots information (%d, %d).\n", 631831f5dcfSAlexander Motin slots, bar); 632831f5dcfSAlexander Motin return (EINVAL); 633831f5dcfSAlexander Motin } 634831f5dcfSAlexander Motin /* Allocate IRQ. */ 635831f5dcfSAlexander Motin sc->irq_rid = 0; 636831f5dcfSAlexander Motin sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 637831f5dcfSAlexander Motin RF_SHAREABLE | RF_ACTIVE); 638831f5dcfSAlexander Motin if (sc->irq_res == NULL) { 639831f5dcfSAlexander Motin device_printf(dev, "Can't allocate IRQ\n"); 640831f5dcfSAlexander Motin return (ENOMEM); 641831f5dcfSAlexander Motin } 642831f5dcfSAlexander Motin /* Scan all slots. */ 643831f5dcfSAlexander Motin for (i = 0; i < slots; i++) { 644831f5dcfSAlexander Motin struct sdhci_slot *slot = &sc->slots[sc->num_slots]; 645831f5dcfSAlexander Motin uint32_t caps; 646831f5dcfSAlexander Motin 647831f5dcfSAlexander Motin SDHCI_LOCK_INIT(slot); 648831f5dcfSAlexander Motin slot->sc = sc; 649831f5dcfSAlexander Motin slot->num = sc->num_slots; 650831f5dcfSAlexander Motin /* Allocate memory. */ 651831f5dcfSAlexander Motin slot->mem_rid = PCIR_BAR(bar + i); 652831f5dcfSAlexander Motin slot->mem_res = bus_alloc_resource(dev, 653831f5dcfSAlexander Motin SYS_RES_MEMORY, &slot->mem_rid, 0ul, ~0ul, 0x100, RF_ACTIVE); 654831f5dcfSAlexander Motin if (slot->mem_res == NULL) { 655831f5dcfSAlexander Motin device_printf(dev, "Can't allocate memory\n"); 656831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 657831f5dcfSAlexander Motin continue; 658831f5dcfSAlexander Motin } 659831f5dcfSAlexander Motin /* Allocate DMA tag. */ 660831f5dcfSAlexander Motin err = bus_dma_tag_create(bus_get_dma_tag(dev), 661831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 662831f5dcfSAlexander Motin BUS_SPACE_MAXADDR, NULL, NULL, 663831f5dcfSAlexander Motin DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 664831f5dcfSAlexander Motin BUS_DMA_ALLOCNOW, NULL, NULL, 665831f5dcfSAlexander Motin &slot->dmatag); 666831f5dcfSAlexander Motin if (err != 0) { 667831f5dcfSAlexander Motin device_printf(dev, "Can't create DMA tag\n"); 668831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 669831f5dcfSAlexander Motin continue; 670831f5dcfSAlexander Motin } 671831f5dcfSAlexander Motin /* Allocate DMA memory. */ 672831f5dcfSAlexander Motin err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 673831f5dcfSAlexander Motin BUS_DMA_NOWAIT, &slot->dmamap); 674831f5dcfSAlexander Motin if (err != 0) { 675831f5dcfSAlexander Motin device_printf(dev, "Can't alloc DMA memory\n"); 676831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 677831f5dcfSAlexander Motin continue; 678831f5dcfSAlexander Motin } 679831f5dcfSAlexander Motin /* Map the memory. */ 680831f5dcfSAlexander Motin err = bus_dmamap_load(slot->dmatag, slot->dmamap, 681831f5dcfSAlexander Motin (void *)slot->dmamem, DMA_BLOCK_SIZE, 682831f5dcfSAlexander Motin sdhci_getaddr, &slot->paddr, 0); 683831f5dcfSAlexander Motin if (err != 0 || slot->paddr == 0) { 684831f5dcfSAlexander Motin device_printf(dev, "Can't load DMA memory\n"); 685831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 686831f5dcfSAlexander Motin continue; 687831f5dcfSAlexander Motin } 688831f5dcfSAlexander Motin /* Initialize slot. */ 689831f5dcfSAlexander Motin sdhci_init(slot); 690831f5dcfSAlexander Motin caps = RD4(slot, SDHCI_CAPABILITIES); 691831f5dcfSAlexander Motin /* Calculate base clock frequency. */ 692831f5dcfSAlexander Motin slot->max_clk = 693831f5dcfSAlexander Motin (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; 694831f5dcfSAlexander Motin if (slot->max_clk == 0) { 695831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify base clock " 696831f5dcfSAlexander Motin "frequency.\n"); 697831f5dcfSAlexander Motin } 698831f5dcfSAlexander Motin slot->max_clk *= 1000000; 699831f5dcfSAlexander Motin /* Calculate timeout clock frequency. */ 700831f5dcfSAlexander Motin slot->timeout_clk = 701831f5dcfSAlexander Motin (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; 702831f5dcfSAlexander Motin if (slot->timeout_clk == 0) { 703831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't specify timeout clock " 704831f5dcfSAlexander Motin "frequency.\n"); 705831f5dcfSAlexander Motin } 706831f5dcfSAlexander Motin if (caps & SDHCI_TIMEOUT_CLK_UNIT) 707831f5dcfSAlexander Motin slot->timeout_clk *= 1000; 708831f5dcfSAlexander Motin 709831f5dcfSAlexander Motin slot->host.f_min = slot->max_clk / 256; 710831f5dcfSAlexander Motin slot->host.f_max = slot->max_clk; 711831f5dcfSAlexander Motin slot->host.host_ocr = 0; 712831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_330) 713831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 714831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_300) 715831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 716831f5dcfSAlexander Motin if (caps & SDHCI_CAN_VDD_180) 717831f5dcfSAlexander Motin slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 718831f5dcfSAlexander Motin if (slot->host.host_ocr == 0) { 719831f5dcfSAlexander Motin device_printf(dev, "Hardware doesn't report any " 720831f5dcfSAlexander Motin "support voltages.\n"); 721831f5dcfSAlexander Motin } 722831f5dcfSAlexander Motin slot->host.caps = MMC_CAP_4_BIT_DATA; 723831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_HISPD) 724831f5dcfSAlexander Motin slot->host.caps |= MMC_CAP_HSPEED; 725831f5dcfSAlexander Motin /* Decide if we have usable DMA. */ 726831f5dcfSAlexander Motin if (caps & SDHCI_CAN_DO_DMA) 727831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 728831f5dcfSAlexander Motin if (class == PCIC_BASEPERIPH && 729831f5dcfSAlexander Motin subclass == PCIS_BASEPERIPH_SDHC && 730831f5dcfSAlexander Motin progif != PCI_SDHCI_IFDMA) 731831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 732831f5dcfSAlexander Motin if (sc->quirks & SDHCI_QUIRK_BROKEN_DMA) 733831f5dcfSAlexander Motin slot->opt &= ~SDHCI_HAVE_DMA; 734831f5dcfSAlexander Motin if (sc->quirks & SDHCI_QUIRK_FORCE_DMA) 735831f5dcfSAlexander Motin slot->opt |= SDHCI_HAVE_DMA; 736831f5dcfSAlexander Motin 737831f5dcfSAlexander Motin if (bootverbose) { 738831f5dcfSAlexander Motin slot_printf(slot, "%uMHz%s 4bits%s%s%s %s\n", 739831f5dcfSAlexander Motin slot->max_clk / 1000000, 740831f5dcfSAlexander Motin (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 741831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 742831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 743831f5dcfSAlexander Motin (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 744831f5dcfSAlexander Motin (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 745831f5dcfSAlexander Motin sdhci_dumpregs(slot); 746831f5dcfSAlexander Motin } 747831f5dcfSAlexander Motin 748831f5dcfSAlexander Motin TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 749831f5dcfSAlexander Motin callout_init(&slot->card_callout, 1); 750831f5dcfSAlexander Motin sc->num_slots++; 751831f5dcfSAlexander Motin } 752831f5dcfSAlexander Motin device_printf(dev, "%d slot(s) allocated\n", sc->num_slots); 753831f5dcfSAlexander Motin /* Activate the interrupt */ 754831f5dcfSAlexander Motin err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 755831f5dcfSAlexander Motin NULL, sdhci_intr, sc, &sc->intrhand); 756831f5dcfSAlexander Motin if (err) 757831f5dcfSAlexander Motin device_printf(dev, "Can't setup IRQ\n"); 758831f5dcfSAlexander Motin pci_enable_busmaster(dev); 759831f5dcfSAlexander Motin /* Process cards detection. */ 760831f5dcfSAlexander Motin for (i = 0; i < sc->num_slots; i++) { 761831f5dcfSAlexander Motin struct sdhci_slot *slot = &sc->slots[i]; 762831f5dcfSAlexander Motin 763831f5dcfSAlexander Motin sdhci_card_task(slot, 0); 764831f5dcfSAlexander Motin } 765831f5dcfSAlexander Motin 766831f5dcfSAlexander Motin return (0); 767831f5dcfSAlexander Motin } 768831f5dcfSAlexander Motin 769831f5dcfSAlexander Motin static int 770831f5dcfSAlexander Motin sdhci_detach(device_t dev) 771831f5dcfSAlexander Motin { 772831f5dcfSAlexander Motin struct sdhci_softc *sc = device_get_softc(dev); 773831f5dcfSAlexander Motin int i; 774831f5dcfSAlexander Motin 775831f5dcfSAlexander Motin bus_teardown_intr(dev, sc->irq_res, sc->intrhand); 776831f5dcfSAlexander Motin bus_release_resource(dev, SYS_RES_IRQ, 777831f5dcfSAlexander Motin sc->irq_rid, sc->irq_res); 778831f5dcfSAlexander Motin 779831f5dcfSAlexander Motin for (i = 0; i < sc->num_slots; i++) { 780831f5dcfSAlexander Motin struct sdhci_slot *slot = &sc->slots[i]; 781831f5dcfSAlexander Motin device_t d; 782831f5dcfSAlexander Motin 783831f5dcfSAlexander Motin callout_drain(&slot->card_callout); 784831f5dcfSAlexander Motin taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 785831f5dcfSAlexander Motin 786831f5dcfSAlexander Motin SDHCI_LOCK(slot); 787831f5dcfSAlexander Motin d = slot->dev; 788831f5dcfSAlexander Motin slot->dev = NULL; 789831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 790831f5dcfSAlexander Motin if (d != NULL) 791831f5dcfSAlexander Motin device_delete_child(dev, d); 792831f5dcfSAlexander Motin 793831f5dcfSAlexander Motin SDHCI_LOCK(slot); 794831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_ALL); 795831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 796831f5dcfSAlexander Motin bus_dmamap_unload(slot->dmatag, slot->dmamap); 797831f5dcfSAlexander Motin bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 798831f5dcfSAlexander Motin bus_dma_tag_destroy(slot->dmatag); 799831f5dcfSAlexander Motin bus_release_resource(dev, SYS_RES_MEMORY, 800831f5dcfSAlexander Motin slot->mem_rid, slot->mem_res); 801831f5dcfSAlexander Motin SDHCI_LOCK_DESTROY(slot); 802831f5dcfSAlexander Motin } 803831f5dcfSAlexander Motin return (0); 804831f5dcfSAlexander Motin } 805831f5dcfSAlexander Motin 806831f5dcfSAlexander Motin static int 80792bf0e27SAlexander Motin sdhci_suspend(device_t dev) 80892bf0e27SAlexander Motin { 80992bf0e27SAlexander Motin struct sdhci_softc *sc = device_get_softc(dev); 81092bf0e27SAlexander Motin int i, err; 81192bf0e27SAlexander Motin 81292bf0e27SAlexander Motin err = bus_generic_suspend(dev); 81392bf0e27SAlexander Motin if (err) 81492bf0e27SAlexander Motin return (err); 81592bf0e27SAlexander Motin for (i = 0; i < sc->num_slots; i++) 81692bf0e27SAlexander Motin sdhci_reset(&sc->slots[i], SDHCI_RESET_ALL); 81792bf0e27SAlexander Motin return (0); 81892bf0e27SAlexander Motin } 81992bf0e27SAlexander Motin 82092bf0e27SAlexander Motin static int 82192bf0e27SAlexander Motin sdhci_resume(device_t dev) 82292bf0e27SAlexander Motin { 82392bf0e27SAlexander Motin struct sdhci_softc *sc = device_get_softc(dev); 82492bf0e27SAlexander Motin int i; 82592bf0e27SAlexander Motin 82692bf0e27SAlexander Motin for (i = 0; i < sc->num_slots; i++) 82792bf0e27SAlexander Motin sdhci_init(&sc->slots[i]); 82892bf0e27SAlexander Motin return (bus_generic_resume(dev)); 82992bf0e27SAlexander Motin } 83092bf0e27SAlexander Motin 83192bf0e27SAlexander Motin static int 832831f5dcfSAlexander Motin sdhci_update_ios(device_t brdev, device_t reqdev) 833831f5dcfSAlexander Motin { 834831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 835831f5dcfSAlexander Motin struct mmc_ios *ios = &slot->host.ios; 836831f5dcfSAlexander Motin 837831f5dcfSAlexander Motin SDHCI_LOCK(slot); 838831f5dcfSAlexander Motin /* Do full reset on bus power down to clear from any state. */ 839831f5dcfSAlexander Motin if (ios->power_mode == power_off) { 840831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 841831f5dcfSAlexander Motin sdhci_init(slot); 842831f5dcfSAlexander Motin } 843831f5dcfSAlexander Motin /* Configure the bus. */ 844831f5dcfSAlexander Motin sdhci_set_clock(slot, ios->clock); 845831f5dcfSAlexander Motin sdhci_set_power(slot, (ios->power_mode == power_off)?0:ios->vdd); 846831f5dcfSAlexander Motin if (ios->bus_width == bus_width_4) 847831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_4BITBUS; 848831f5dcfSAlexander Motin else 849831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 850831f5dcfSAlexander Motin if (ios->timing == bus_timing_hs) 851831f5dcfSAlexander Motin slot->hostctrl |= SDHCI_CTRL_HISPD; 852831f5dcfSAlexander Motin else 853831f5dcfSAlexander Motin slot->hostctrl &= ~SDHCI_CTRL_HISPD; 854831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 855831f5dcfSAlexander Motin /* Some controllers like reset after bus changes. */ 856831f5dcfSAlexander Motin if(slot->sc->quirks & SDHCI_QUIRK_RESET_ON_IOS) 857831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 858831f5dcfSAlexander Motin 859831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 860831f5dcfSAlexander Motin return (0); 861831f5dcfSAlexander Motin } 862831f5dcfSAlexander Motin 863831f5dcfSAlexander Motin static void 864831f5dcfSAlexander Motin sdhci_set_transfer_mode(struct sdhci_slot *slot, 865831f5dcfSAlexander Motin struct mmc_data *data) 866831f5dcfSAlexander Motin { 867831f5dcfSAlexander Motin uint16_t mode; 868831f5dcfSAlexander Motin 869831f5dcfSAlexander Motin if (data == NULL) 870831f5dcfSAlexander Motin return; 871831f5dcfSAlexander Motin 872831f5dcfSAlexander Motin mode = SDHCI_TRNS_BLK_CNT_EN; 873831f5dcfSAlexander Motin if (data->len > 512) 874831f5dcfSAlexander Motin mode |= SDHCI_TRNS_MULTI; 875831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 876831f5dcfSAlexander Motin mode |= SDHCI_TRNS_READ; 877831f5dcfSAlexander Motin if (slot->req->stop) 878831f5dcfSAlexander Motin mode |= SDHCI_TRNS_ACMD12; 879831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) 880831f5dcfSAlexander Motin mode |= SDHCI_TRNS_DMA; 881831f5dcfSAlexander Motin 882831f5dcfSAlexander Motin WR2(slot, SDHCI_TRANSFER_MODE, mode); 883831f5dcfSAlexander Motin } 884831f5dcfSAlexander Motin 885831f5dcfSAlexander Motin static void 886831f5dcfSAlexander Motin sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 887831f5dcfSAlexander Motin { 888831f5dcfSAlexander Motin struct mmc_request *req = slot->req; 889831f5dcfSAlexander Motin int flags, timeout; 890831f5dcfSAlexander Motin uint32_t mask, state; 891831f5dcfSAlexander Motin 892831f5dcfSAlexander Motin slot->curcmd = cmd; 893831f5dcfSAlexander Motin slot->cmd_done = 0; 894831f5dcfSAlexander Motin 895831f5dcfSAlexander Motin cmd->error = MMC_ERR_NONE; 896831f5dcfSAlexander Motin 897831f5dcfSAlexander Motin /* This flags combination is not supported by controller. */ 898831f5dcfSAlexander Motin if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 899831f5dcfSAlexander Motin slot_printf(slot, "Unsupported response type!\n"); 900831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 901831f5dcfSAlexander Motin slot->req = NULL; 902831f5dcfSAlexander Motin slot->curcmd = NULL; 903831f5dcfSAlexander Motin req->done(req); 904831f5dcfSAlexander Motin return; 905831f5dcfSAlexander Motin } 906831f5dcfSAlexander Motin 907831f5dcfSAlexander Motin /* Read controller present state. */ 908831f5dcfSAlexander Motin state = RD4(slot, SDHCI_PRESENT_STATE); 909d8208d9eSAlexander Motin /* Do not issue command if there is no card, clock or power. 910d8208d9eSAlexander Motin * Controller will not detect timeout without clock active. */ 911d8208d9eSAlexander Motin if ((state & SDHCI_CARD_PRESENT) == 0 || 912d8208d9eSAlexander Motin slot->power == 0 || 913d8208d9eSAlexander Motin slot->clock == 0) { 914831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 915831f5dcfSAlexander Motin slot->req = NULL; 916831f5dcfSAlexander Motin slot->curcmd = NULL; 917831f5dcfSAlexander Motin req->done(req); 918831f5dcfSAlexander Motin return; 919831f5dcfSAlexander Motin } 920831f5dcfSAlexander Motin /* Always wait for free CMD bus. */ 921831f5dcfSAlexander Motin mask = SDHCI_CMD_INHIBIT; 922831f5dcfSAlexander Motin /* Wait for free DAT if we have data or busy signal. */ 923831f5dcfSAlexander Motin if (cmd->data || (cmd->flags & MMC_RSP_BUSY)) 924831f5dcfSAlexander Motin mask |= SDHCI_DAT_INHIBIT; 925831f5dcfSAlexander Motin /* We shouldn't wait for DAT for stop commands. */ 926831f5dcfSAlexander Motin if (cmd == slot->req->stop) 927831f5dcfSAlexander Motin mask &= ~SDHCI_DAT_INHIBIT; 928831f5dcfSAlexander Motin /* Wait for bus no more then 10 ms. */ 929831f5dcfSAlexander Motin timeout = 10; 930831f5dcfSAlexander Motin while (state & mask) { 931831f5dcfSAlexander Motin if (timeout == 0) { 932831f5dcfSAlexander Motin slot_printf(slot, "Controller never released " 933831f5dcfSAlexander Motin "inhibit bit(s).\n"); 934831f5dcfSAlexander Motin sdhci_dumpregs(slot); 935831f5dcfSAlexander Motin cmd->error = MMC_ERR_FAILED; 936831f5dcfSAlexander Motin slot->req = NULL; 937831f5dcfSAlexander Motin slot->curcmd = NULL; 938831f5dcfSAlexander Motin req->done(req); 939831f5dcfSAlexander Motin return; 940831f5dcfSAlexander Motin } 941831f5dcfSAlexander Motin timeout--; 942831f5dcfSAlexander Motin DELAY(1000); 943831f5dcfSAlexander Motin state = RD4(slot, SDHCI_PRESENT_STATE); 944831f5dcfSAlexander Motin } 945831f5dcfSAlexander Motin 946831f5dcfSAlexander Motin /* Prepare command flags. */ 947831f5dcfSAlexander Motin if (!(cmd->flags & MMC_RSP_PRESENT)) 948831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_NONE; 949831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_136) 950831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_LONG; 951831f5dcfSAlexander Motin else if (cmd->flags & MMC_RSP_BUSY) 952831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT_BUSY; 953831f5dcfSAlexander Motin else 954831f5dcfSAlexander Motin flags = SDHCI_CMD_RESP_SHORT; 955831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_CRC) 956831f5dcfSAlexander Motin flags |= SDHCI_CMD_CRC; 957831f5dcfSAlexander Motin if (cmd->flags & MMC_RSP_OPCODE) 958831f5dcfSAlexander Motin flags |= SDHCI_CMD_INDEX; 959831f5dcfSAlexander Motin if (cmd->data) 960831f5dcfSAlexander Motin flags |= SDHCI_CMD_DATA; 961831f5dcfSAlexander Motin if (cmd->opcode == MMC_STOP_TRANSMISSION) 962831f5dcfSAlexander Motin flags |= SDHCI_CMD_TYPE_ABORT; 963831f5dcfSAlexander Motin /* Prepare data. */ 964831f5dcfSAlexander Motin sdhci_start_data(slot, cmd->data); 965831f5dcfSAlexander Motin /* 966831f5dcfSAlexander Motin * Interrupt aggregation: To reduce total number of interrupts 967831f5dcfSAlexander Motin * group response interrupt with data interrupt when possible. 968831f5dcfSAlexander Motin * If there going to be data interrupt, mask response one. 969831f5dcfSAlexander Motin */ 970831f5dcfSAlexander Motin if (slot->data_done == 0) { 971831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 972831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_RESPONSE); 973831f5dcfSAlexander Motin } 974831f5dcfSAlexander Motin /* Set command argument. */ 975831f5dcfSAlexander Motin WR4(slot, SDHCI_ARGUMENT, cmd->arg); 976831f5dcfSAlexander Motin /* Set data transfer mode. */ 977831f5dcfSAlexander Motin sdhci_set_transfer_mode(slot, cmd->data); 978831f5dcfSAlexander Motin /* Set command flags. */ 979831f5dcfSAlexander Motin WR1(slot, SDHCI_COMMAND_FLAGS, flags); 980831f5dcfSAlexander Motin /* Start command. */ 981831f5dcfSAlexander Motin WR1(slot, SDHCI_COMMAND, cmd->opcode); 982831f5dcfSAlexander Motin } 983831f5dcfSAlexander Motin 984831f5dcfSAlexander Motin static void 985831f5dcfSAlexander Motin sdhci_finish_command(struct sdhci_slot *slot) 986831f5dcfSAlexander Motin { 987831f5dcfSAlexander Motin int i; 988831f5dcfSAlexander Motin 989831f5dcfSAlexander Motin slot->cmd_done = 1; 990831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 991831f5dcfSAlexander Motin * Main restore point for the case when command interrupt 992831f5dcfSAlexander Motin * happened first. */ 993831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 994831f5dcfSAlexander Motin /* In case of error - reset host and return. */ 995831f5dcfSAlexander Motin if (slot->curcmd->error) { 996831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 997831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 998831f5dcfSAlexander Motin sdhci_start(slot); 999831f5dcfSAlexander Motin return; 1000831f5dcfSAlexander Motin } 1001831f5dcfSAlexander Motin /* If command has response - fetch it. */ 1002831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1003831f5dcfSAlexander Motin if (slot->curcmd->flags & MMC_RSP_136) { 1004831f5dcfSAlexander Motin /* CRC is stripped so we need one byte shift. */ 1005831f5dcfSAlexander Motin uint8_t extra = 0; 1006831f5dcfSAlexander Motin for (i = 0; i < 4; i++) { 1007831f5dcfSAlexander Motin uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4); 1008831f5dcfSAlexander Motin slot->curcmd->resp[3 - i] = (val << 8) + extra; 1009831f5dcfSAlexander Motin extra = val >> 24; 1010831f5dcfSAlexander Motin } 1011831f5dcfSAlexander Motin } else 1012831f5dcfSAlexander Motin slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1013831f5dcfSAlexander Motin } 1014831f5dcfSAlexander Motin /* If data ready - finish. */ 1015831f5dcfSAlexander Motin if (slot->data_done) 1016831f5dcfSAlexander Motin sdhci_start(slot); 1017831f5dcfSAlexander Motin } 1018831f5dcfSAlexander Motin 1019831f5dcfSAlexander Motin static void 1020831f5dcfSAlexander Motin sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 1021831f5dcfSAlexander Motin { 1022831f5dcfSAlexander Motin uint32_t target_timeout, current_timeout; 1023831f5dcfSAlexander Motin uint8_t div; 1024831f5dcfSAlexander Motin 1025831f5dcfSAlexander Motin if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1026831f5dcfSAlexander Motin slot->data_done = 1; 1027831f5dcfSAlexander Motin return; 1028831f5dcfSAlexander Motin } 1029831f5dcfSAlexander Motin 1030831f5dcfSAlexander Motin slot->data_done = 0; 1031831f5dcfSAlexander Motin 1032831f5dcfSAlexander Motin /* Calculate and set data timeout.*/ 1033831f5dcfSAlexander Motin /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1034831f5dcfSAlexander Motin target_timeout = 1000000; 1035831f5dcfSAlexander Motin div = 0; 1036831f5dcfSAlexander Motin current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1037831f5dcfSAlexander Motin while (current_timeout < target_timeout) { 1038831f5dcfSAlexander Motin div++; 1039831f5dcfSAlexander Motin current_timeout <<= 1; 1040831f5dcfSAlexander Motin if (div >= 0xF) 1041831f5dcfSAlexander Motin break; 1042831f5dcfSAlexander Motin } 1043831f5dcfSAlexander Motin /* Compensate for an off-by-one error in the CaFe chip.*/ 1044831f5dcfSAlexander Motin if (slot->sc->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL) 1045831f5dcfSAlexander Motin div++; 1046831f5dcfSAlexander Motin if (div >= 0xF) { 1047831f5dcfSAlexander Motin slot_printf(slot, "Timeout too large!\n"); 1048831f5dcfSAlexander Motin div = 0xE; 1049831f5dcfSAlexander Motin } 1050831f5dcfSAlexander Motin WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1051831f5dcfSAlexander Motin 1052831f5dcfSAlexander Motin if (data == NULL) 1053831f5dcfSAlexander Motin return; 1054831f5dcfSAlexander Motin 1055831f5dcfSAlexander Motin /* Use DMA if possible. */ 1056831f5dcfSAlexander Motin if ((slot->opt & SDHCI_HAVE_DMA)) 1057831f5dcfSAlexander Motin slot->flags |= SDHCI_USE_DMA; 1058831f5dcfSAlexander Motin /* If data is small, broken DMA may return zeroes instead of data, */ 1059831f5dcfSAlexander Motin if ((slot->sc->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1060831f5dcfSAlexander Motin (data->len <= 512)) 1061831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1062831f5dcfSAlexander Motin /* Some controllers require even block sizes. */ 1063831f5dcfSAlexander Motin if ((slot->sc->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1064831f5dcfSAlexander Motin ((data->len) & 0x3)) 1065831f5dcfSAlexander Motin slot->flags &= ~SDHCI_USE_DMA; 1066831f5dcfSAlexander Motin /* Load DMA buffer. */ 1067831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1068831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) 1069831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_PREREAD); 1070831f5dcfSAlexander Motin else { 1071831f5dcfSAlexander Motin memcpy(slot->dmamem, data->data, 1072831f5dcfSAlexander Motin (data->len < DMA_BLOCK_SIZE)?data->len:DMA_BLOCK_SIZE); 1073831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_PREWRITE); 1074831f5dcfSAlexander Motin } 1075831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1076831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1077831f5dcfSAlexander Motin * for the last page and unmask else. */ 1078831f5dcfSAlexander Motin if (data->len == DMA_BLOCK_SIZE) 1079831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1080831f5dcfSAlexander Motin else 1081831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_DMA_END; 1082831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1083831f5dcfSAlexander Motin } 1084831f5dcfSAlexander Motin /* Current data offset for both PIO and DMA. */ 1085831f5dcfSAlexander Motin slot->offset = 0; 1086831f5dcfSAlexander Motin /* Set block size and request IRQ on 4K border. */ 1087831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_SIZE, 1088831f5dcfSAlexander Motin SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512)); 1089831f5dcfSAlexander Motin /* Set block count. */ 1090831f5dcfSAlexander Motin WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1091831f5dcfSAlexander Motin } 1092831f5dcfSAlexander Motin 1093831f5dcfSAlexander Motin static void 1094831f5dcfSAlexander Motin sdhci_finish_data(struct sdhci_slot *slot) 1095831f5dcfSAlexander Motin { 1096831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1097831f5dcfSAlexander Motin 1098831f5dcfSAlexander Motin slot->data_done = 1; 1099831f5dcfSAlexander Motin /* Interrupt aggregation: Restore command interrupt. 1100831f5dcfSAlexander Motin * Auxillary restore point for the case when data interrupt 1101831f5dcfSAlexander Motin * happened first. */ 1102831f5dcfSAlexander Motin if (!slot->cmd_done) { 1103831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, 1104831f5dcfSAlexander Motin slot->intmask |= SDHCI_INT_RESPONSE); 1105831f5dcfSAlexander Motin } 1106831f5dcfSAlexander Motin /* Unload rest of data from DMA buffer. */ 1107831f5dcfSAlexander Motin if (slot->flags & SDHCI_USE_DMA) { 1108831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1109831f5dcfSAlexander Motin size_t left = data->len - slot->offset; 1110831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_POSTREAD); 1111831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1112831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1113831f5dcfSAlexander Motin } else 1114831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, BUS_DMASYNC_POSTWRITE); 1115831f5dcfSAlexander Motin } 1116831f5dcfSAlexander Motin /* If there was error - reset the host. */ 1117831f5dcfSAlexander Motin if (slot->curcmd->error) { 1118831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1119831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1120831f5dcfSAlexander Motin sdhci_start(slot); 1121831f5dcfSAlexander Motin return; 1122831f5dcfSAlexander Motin } 1123831f5dcfSAlexander Motin /* If we already have command response - finish. */ 1124831f5dcfSAlexander Motin if (slot->cmd_done) 1125831f5dcfSAlexander Motin sdhci_start(slot); 1126831f5dcfSAlexander Motin } 1127831f5dcfSAlexander Motin 1128831f5dcfSAlexander Motin static void 1129831f5dcfSAlexander Motin sdhci_start(struct sdhci_slot *slot) 1130831f5dcfSAlexander Motin { 1131831f5dcfSAlexander Motin struct mmc_request *req; 1132831f5dcfSAlexander Motin 1133831f5dcfSAlexander Motin req = slot->req; 1134831f5dcfSAlexander Motin if (req == NULL) 1135831f5dcfSAlexander Motin return; 1136831f5dcfSAlexander Motin 1137831f5dcfSAlexander Motin if (!(slot->flags & CMD_STARTED)) { 1138831f5dcfSAlexander Motin slot->flags |= CMD_STARTED; 1139831f5dcfSAlexander Motin sdhci_start_command(slot, req->cmd); 1140831f5dcfSAlexander Motin return; 1141831f5dcfSAlexander Motin } 1142831f5dcfSAlexander Motin /* We don't need this until using Auto-CMD12 feature 1143831f5dcfSAlexander Motin if (!(slot->flags & STOP_STARTED) && req->stop) { 1144831f5dcfSAlexander Motin slot->flags |= STOP_STARTED; 1145831f5dcfSAlexander Motin sdhci_start_command(slot, req->stop); 1146831f5dcfSAlexander Motin return; 1147831f5dcfSAlexander Motin } 1148831f5dcfSAlexander Motin */ 1149831f5dcfSAlexander Motin if (req->cmd->error) { 1150831f5dcfSAlexander Motin if (bootverbose) { 1151831f5dcfSAlexander Motin slot_printf(slot, 1152831f5dcfSAlexander Motin "Command error %d (opcode %u arg %u flags %u " 1153831f5dcfSAlexander Motin "dlen %u dflags %u)\n", 1154831f5dcfSAlexander Motin req->cmd->error, req->cmd->opcode, req->cmd->arg, 1155831f5dcfSAlexander Motin req->cmd->flags, 1156831f5dcfSAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->len:0, 1157831f5dcfSAlexander Motin (req->cmd->data)?(u_int)req->cmd->data->flags:0); 1158831f5dcfSAlexander Motin } 1159831f5dcfSAlexander Motin } else if (slot->sc->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST) { 1160831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1161831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_DATA); 1162831f5dcfSAlexander Motin } 1163831f5dcfSAlexander Motin 1164831f5dcfSAlexander Motin /* We must be done -- bad idea to do this while locked? */ 1165831f5dcfSAlexander Motin slot->req = NULL; 1166831f5dcfSAlexander Motin slot->curcmd = NULL; 1167831f5dcfSAlexander Motin req->done(req); 1168831f5dcfSAlexander Motin } 1169831f5dcfSAlexander Motin 1170831f5dcfSAlexander Motin static int 1171831f5dcfSAlexander Motin sdhci_request(device_t brdev, device_t reqdev, struct mmc_request *req) 1172831f5dcfSAlexander Motin { 1173831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1174831f5dcfSAlexander Motin 1175831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1176831f5dcfSAlexander Motin if (slot->req != NULL) { 1177831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1178831f5dcfSAlexander Motin return (EBUSY); 1179831f5dcfSAlexander Motin } 1180831f5dcfSAlexander Motin /* printf("%s cmd op %u arg %u flags %u data %ju\n", __func__, 1181831f5dcfSAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags, 1182831f5dcfSAlexander Motin (req->cmd->data)?req->cmd->data->len:0); */ 1183831f5dcfSAlexander Motin slot->req = req; 1184831f5dcfSAlexander Motin slot->flags = 0; 1185831f5dcfSAlexander Motin sdhci_start(slot); 1186831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1187831f5dcfSAlexander Motin return (0); 1188831f5dcfSAlexander Motin } 1189831f5dcfSAlexander Motin 1190831f5dcfSAlexander Motin static int 1191831f5dcfSAlexander Motin sdhci_get_ro(device_t brdev, device_t reqdev) 1192831f5dcfSAlexander Motin { 1193831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1194831f5dcfSAlexander Motin uint32_t val; 1195831f5dcfSAlexander Motin 1196831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1197831f5dcfSAlexander Motin val = RD4(slot, SDHCI_PRESENT_STATE); 1198831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1199831f5dcfSAlexander Motin return (!(val & SDHCI_WRITE_PROTECT)); 1200831f5dcfSAlexander Motin } 1201831f5dcfSAlexander Motin 1202831f5dcfSAlexander Motin static int 1203831f5dcfSAlexander Motin sdhci_acquire_host(device_t brdev, device_t reqdev) 1204831f5dcfSAlexander Motin { 1205831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1206831f5dcfSAlexander Motin int err = 0; 1207831f5dcfSAlexander Motin 1208831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1209831f5dcfSAlexander Motin while (slot->bus_busy) 1210d493985aSAlexander Motin msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1211831f5dcfSAlexander Motin slot->bus_busy++; 1212831f5dcfSAlexander Motin /* Activate led. */ 1213831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1214831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1215831f5dcfSAlexander Motin return (err); 1216831f5dcfSAlexander Motin } 1217831f5dcfSAlexander Motin 1218831f5dcfSAlexander Motin static int 1219831f5dcfSAlexander Motin sdhci_release_host(device_t brdev, device_t reqdev) 1220831f5dcfSAlexander Motin { 1221831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(reqdev); 1222831f5dcfSAlexander Motin 1223831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1224831f5dcfSAlexander Motin /* Deactivate led. */ 1225831f5dcfSAlexander Motin WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1226831f5dcfSAlexander Motin slot->bus_busy--; 1227831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1228d493985aSAlexander Motin wakeup(slot); 1229831f5dcfSAlexander Motin return (0); 1230831f5dcfSAlexander Motin } 1231831f5dcfSAlexander Motin 1232831f5dcfSAlexander Motin static void 1233831f5dcfSAlexander Motin sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1234831f5dcfSAlexander Motin { 1235831f5dcfSAlexander Motin 1236831f5dcfSAlexander Motin if (!slot->curcmd) { 1237831f5dcfSAlexander Motin slot_printf(slot, "Got command interrupt 0x%08x, but " 1238831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1239831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1240831f5dcfSAlexander Motin return; 1241831f5dcfSAlexander Motin } 1242831f5dcfSAlexander Motin if (intmask & SDHCI_INT_TIMEOUT) 1243831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1244831f5dcfSAlexander Motin else if (intmask & SDHCI_INT_CRC) 1245831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1246831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1247831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_FIFO; 1248831f5dcfSAlexander Motin 1249831f5dcfSAlexander Motin sdhci_finish_command(slot); 1250831f5dcfSAlexander Motin } 1251831f5dcfSAlexander Motin 1252831f5dcfSAlexander Motin static void 1253831f5dcfSAlexander Motin sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1254831f5dcfSAlexander Motin { 1255831f5dcfSAlexander Motin 1256831f5dcfSAlexander Motin if (!slot->curcmd) { 1257831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1258831f5dcfSAlexander Motin "there is no active command.\n", intmask); 1259831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1260831f5dcfSAlexander Motin return; 1261831f5dcfSAlexander Motin } 1262831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1263831f5dcfSAlexander Motin (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1264831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1265831f5dcfSAlexander Motin "there is no active data operation.\n", 1266831f5dcfSAlexander Motin intmask); 1267831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1268831f5dcfSAlexander Motin return; 1269831f5dcfSAlexander Motin } 1270831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_TIMEOUT) 1271831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_TIMEOUT; 1272831f5dcfSAlexander Motin else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1273831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_BADCRC; 1274831f5dcfSAlexander Motin if (slot->curcmd->data == NULL && 1275831f5dcfSAlexander Motin (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1276831f5dcfSAlexander Motin SDHCI_INT_DMA_END))) { 1277831f5dcfSAlexander Motin slot_printf(slot, "Got data interrupt 0x%08x, but " 1278831f5dcfSAlexander Motin "there is busy-only command.\n", intmask); 1279831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1280831f5dcfSAlexander Motin slot->curcmd->error = MMC_ERR_INVALID; 1281831f5dcfSAlexander Motin } 1282831f5dcfSAlexander Motin if (slot->curcmd->error) { 1283831f5dcfSAlexander Motin /* No need to continue after any error. */ 1284831f5dcfSAlexander Motin sdhci_finish_data(slot); 1285831f5dcfSAlexander Motin return; 1286831f5dcfSAlexander Motin } 1287831f5dcfSAlexander Motin 1288831f5dcfSAlexander Motin /* Handle PIO interrupt. */ 1289831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) 1290831f5dcfSAlexander Motin sdhci_transfer_pio(slot); 1291831f5dcfSAlexander Motin /* Handle DMA border. */ 1292831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DMA_END) { 1293831f5dcfSAlexander Motin struct mmc_data *data = slot->curcmd->data; 1294831f5dcfSAlexander Motin size_t left; 1295831f5dcfSAlexander Motin 1296831f5dcfSAlexander Motin /* Unload DMA buffer... */ 1297831f5dcfSAlexander Motin left = data->len - slot->offset; 1298831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1299831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1300831f5dcfSAlexander Motin BUS_DMASYNC_POSTREAD); 1301831f5dcfSAlexander Motin memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1302831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1303831f5dcfSAlexander Motin } else { 1304831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1305831f5dcfSAlexander Motin BUS_DMASYNC_POSTWRITE); 1306831f5dcfSAlexander Motin } 1307831f5dcfSAlexander Motin /* ... and reload it again. */ 1308831f5dcfSAlexander Motin slot->offset += DMA_BLOCK_SIZE; 1309831f5dcfSAlexander Motin left = data->len - slot->offset; 1310831f5dcfSAlexander Motin if (data->flags & MMC_DATA_READ) { 1311831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1312831f5dcfSAlexander Motin BUS_DMASYNC_PREREAD); 1313831f5dcfSAlexander Motin } else { 1314831f5dcfSAlexander Motin memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1315831f5dcfSAlexander Motin (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1316831f5dcfSAlexander Motin bus_dmamap_sync(slot->dmatag, slot->dmamap, 1317831f5dcfSAlexander Motin BUS_DMASYNC_PREWRITE); 1318831f5dcfSAlexander Motin } 1319831f5dcfSAlexander Motin /* Interrupt aggregation: Mask border interrupt 1320831f5dcfSAlexander Motin * for the last page. */ 1321831f5dcfSAlexander Motin if (left == DMA_BLOCK_SIZE) { 1322831f5dcfSAlexander Motin slot->intmask &= ~SDHCI_INT_DMA_END; 1323831f5dcfSAlexander Motin WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1324831f5dcfSAlexander Motin } 1325831f5dcfSAlexander Motin /* Restart DMA. */ 1326831f5dcfSAlexander Motin WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1327831f5dcfSAlexander Motin } 1328831f5dcfSAlexander Motin /* We have got all data. */ 1329831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_END) 1330831f5dcfSAlexander Motin sdhci_finish_data(slot); 1331831f5dcfSAlexander Motin } 1332831f5dcfSAlexander Motin 1333831f5dcfSAlexander Motin static void 1334831f5dcfSAlexander Motin sdhci_acmd_irq(struct sdhci_slot *slot) 1335831f5dcfSAlexander Motin { 1336831f5dcfSAlexander Motin uint16_t err; 1337831f5dcfSAlexander Motin 1338831f5dcfSAlexander Motin err = RD4(slot, SDHCI_ACMD12_ERR); 1339831f5dcfSAlexander Motin if (!slot->curcmd) { 1340831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1341831f5dcfSAlexander Motin "there is no active command.\n", err); 1342831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1343831f5dcfSAlexander Motin return; 1344831f5dcfSAlexander Motin } 1345831f5dcfSAlexander Motin slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1346831f5dcfSAlexander Motin sdhci_reset(slot, SDHCI_RESET_CMD); 1347831f5dcfSAlexander Motin } 1348831f5dcfSAlexander Motin 1349831f5dcfSAlexander Motin static void 1350831f5dcfSAlexander Motin sdhci_intr(void *arg) 1351831f5dcfSAlexander Motin { 1352831f5dcfSAlexander Motin struct sdhci_softc *sc = (struct sdhci_softc *)arg; 1353831f5dcfSAlexander Motin int i; 1354831f5dcfSAlexander Motin 1355831f5dcfSAlexander Motin for (i = 0; i < sc->num_slots; i++) { 1356831f5dcfSAlexander Motin struct sdhci_slot *slot = &sc->slots[i]; 1357831f5dcfSAlexander Motin uint32_t intmask; 1358831f5dcfSAlexander Motin 1359831f5dcfSAlexander Motin SDHCI_LOCK(slot); 1360831f5dcfSAlexander Motin /* Read slot interrupt status. */ 1361831f5dcfSAlexander Motin intmask = RD4(slot, SDHCI_INT_STATUS); 1362831f5dcfSAlexander Motin if (intmask == 0 || intmask == 0xffffffff) { 1363831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1364831f5dcfSAlexander Motin continue; 1365831f5dcfSAlexander Motin } 1366831f5dcfSAlexander Motin /* 1367831f5dcfSAlexander Motin slot_printf(slot, "got interrupt %x\n", intmask); 1368831f5dcfSAlexander Motin */ 1369831f5dcfSAlexander Motin /* Handle card presence interrupts. */ 1370831f5dcfSAlexander Motin if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 1371831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & 1372831f5dcfSAlexander Motin (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1373831f5dcfSAlexander Motin 1374831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_REMOVE) { 1375831f5dcfSAlexander Motin if (bootverbose) 1376831f5dcfSAlexander Motin slot_printf(slot, "Card removed\n"); 1377831f5dcfSAlexander Motin callout_stop(&slot->card_callout); 1378831f5dcfSAlexander Motin taskqueue_enqueue(taskqueue_swi_giant, 1379831f5dcfSAlexander Motin &slot->card_task); 1380831f5dcfSAlexander Motin } 1381831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CARD_INSERT) { 1382831f5dcfSAlexander Motin if (bootverbose) 1383831f5dcfSAlexander Motin slot_printf(slot, "Card inserted\n"); 1384831f5dcfSAlexander Motin callout_reset(&slot->card_callout, hz / 2, 1385831f5dcfSAlexander Motin sdhci_card_delay, slot); 1386831f5dcfSAlexander Motin } 1387831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1388831f5dcfSAlexander Motin } 1389831f5dcfSAlexander Motin /* Handle command interrupts. */ 1390831f5dcfSAlexander Motin if (intmask & SDHCI_INT_CMD_MASK) { 1391831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1392831f5dcfSAlexander Motin sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1393831f5dcfSAlexander Motin } 1394831f5dcfSAlexander Motin /* Handle data interrupts. */ 1395831f5dcfSAlexander Motin if (intmask & SDHCI_INT_DATA_MASK) { 1396831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 1397831f5dcfSAlexander Motin sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1398831f5dcfSAlexander Motin } 1399831f5dcfSAlexander Motin /* Handle AutoCMD12 error interrupt. */ 1400831f5dcfSAlexander Motin if (intmask & SDHCI_INT_ACMD12ERR) { 1401831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1402831f5dcfSAlexander Motin sdhci_acmd_irq(slot); 1403831f5dcfSAlexander Motin } 1404831f5dcfSAlexander Motin intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1405831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ACMD12ERR; 1406831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_ERROR; 1407831f5dcfSAlexander Motin /* Handle bus power interrupt. */ 1408831f5dcfSAlexander Motin if (intmask & SDHCI_INT_BUS_POWER) { 1409831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1410831f5dcfSAlexander Motin slot_printf(slot, 1411831f5dcfSAlexander Motin "Card is consuming too much power!\n"); 1412831f5dcfSAlexander Motin intmask &= ~SDHCI_INT_BUS_POWER; 1413831f5dcfSAlexander Motin } 1414831f5dcfSAlexander Motin /* The rest is unknown. */ 1415831f5dcfSAlexander Motin if (intmask) { 1416831f5dcfSAlexander Motin WR4(slot, SDHCI_INT_STATUS, intmask); 1417831f5dcfSAlexander Motin slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1418831f5dcfSAlexander Motin intmask); 1419831f5dcfSAlexander Motin sdhci_dumpregs(slot); 1420831f5dcfSAlexander Motin } 1421831f5dcfSAlexander Motin 1422831f5dcfSAlexander Motin SDHCI_UNLOCK(slot); 1423831f5dcfSAlexander Motin } 1424831f5dcfSAlexander Motin } 1425831f5dcfSAlexander Motin 1426831f5dcfSAlexander Motin static int 1427831f5dcfSAlexander Motin sdhci_read_ivar(device_t bus, device_t child, int which, u_char *result) 1428831f5dcfSAlexander Motin { 1429831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1430831f5dcfSAlexander Motin 1431831f5dcfSAlexander Motin switch (which) { 1432831f5dcfSAlexander Motin default: 1433831f5dcfSAlexander Motin return (EINVAL); 1434831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1435831f5dcfSAlexander Motin *(int *)result = slot->host.ios.bus_mode; 1436831f5dcfSAlexander Motin break; 1437831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1438831f5dcfSAlexander Motin *(int *)result = slot->host.ios.bus_width; 1439831f5dcfSAlexander Motin break; 1440831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1441831f5dcfSAlexander Motin *(int *)result = slot->host.ios.chip_select; 1442831f5dcfSAlexander Motin break; 1443831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1444831f5dcfSAlexander Motin *(int *)result = slot->host.ios.clock; 1445831f5dcfSAlexander Motin break; 1446831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1447831f5dcfSAlexander Motin *(int *)result = slot->host.f_min; 1448831f5dcfSAlexander Motin break; 1449831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 1450831f5dcfSAlexander Motin *(int *)result = slot->host.f_max; 1451831f5dcfSAlexander Motin break; 1452831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1453831f5dcfSAlexander Motin *(int *)result = slot->host.host_ocr; 1454831f5dcfSAlexander Motin break; 1455831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1456831f5dcfSAlexander Motin *(int *)result = slot->host.mode; 1457831f5dcfSAlexander Motin break; 1458831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1459831f5dcfSAlexander Motin *(int *)result = slot->host.ocr; 1460831f5dcfSAlexander Motin break; 1461831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1462831f5dcfSAlexander Motin *(int *)result = slot->host.ios.power_mode; 1463831f5dcfSAlexander Motin break; 1464831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1465831f5dcfSAlexander Motin *(int *)result = slot->host.ios.vdd; 1466831f5dcfSAlexander Motin break; 1467831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1468831f5dcfSAlexander Motin *(int *)result = slot->host.caps; 1469831f5dcfSAlexander Motin break; 1470831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1471831f5dcfSAlexander Motin *(int *)result = slot->host.ios.timing; 1472831f5dcfSAlexander Motin break; 14733a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 14743a4a2557SAlexander Motin *(int *)result = 65535; 14753a4a2557SAlexander Motin break; 1476831f5dcfSAlexander Motin } 1477831f5dcfSAlexander Motin return (0); 1478831f5dcfSAlexander Motin } 1479831f5dcfSAlexander Motin 1480831f5dcfSAlexander Motin static int 1481831f5dcfSAlexander Motin sdhci_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1482831f5dcfSAlexander Motin { 1483831f5dcfSAlexander Motin struct sdhci_slot *slot = device_get_ivars(child); 1484831f5dcfSAlexander Motin 1485831f5dcfSAlexander Motin switch (which) { 1486831f5dcfSAlexander Motin default: 1487831f5dcfSAlexander Motin return (EINVAL); 1488831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_MODE: 1489831f5dcfSAlexander Motin slot->host.ios.bus_mode = value; 1490831f5dcfSAlexander Motin break; 1491831f5dcfSAlexander Motin case MMCBR_IVAR_BUS_WIDTH: 1492831f5dcfSAlexander Motin slot->host.ios.bus_width = value; 1493831f5dcfSAlexander Motin break; 1494831f5dcfSAlexander Motin case MMCBR_IVAR_CHIP_SELECT: 1495831f5dcfSAlexander Motin slot->host.ios.chip_select = value; 1496831f5dcfSAlexander Motin break; 1497831f5dcfSAlexander Motin case MMCBR_IVAR_CLOCK: 1498831f5dcfSAlexander Motin if (value > 0) { 1499831f5dcfSAlexander Motin uint32_t clock = slot->max_clk; 1500831f5dcfSAlexander Motin int i; 1501831f5dcfSAlexander Motin 1502831f5dcfSAlexander Motin for (i = 0; i < 8; i++) { 1503831f5dcfSAlexander Motin if (clock <= value) 1504831f5dcfSAlexander Motin break; 1505831f5dcfSAlexander Motin clock >>= 1; 1506831f5dcfSAlexander Motin } 1507831f5dcfSAlexander Motin slot->host.ios.clock = clock; 1508831f5dcfSAlexander Motin } else 1509831f5dcfSAlexander Motin slot->host.ios.clock = 0; 1510831f5dcfSAlexander Motin break; 1511831f5dcfSAlexander Motin case MMCBR_IVAR_MODE: 1512831f5dcfSAlexander Motin slot->host.mode = value; 1513831f5dcfSAlexander Motin break; 1514831f5dcfSAlexander Motin case MMCBR_IVAR_OCR: 1515831f5dcfSAlexander Motin slot->host.ocr = value; 1516831f5dcfSAlexander Motin break; 1517831f5dcfSAlexander Motin case MMCBR_IVAR_POWER_MODE: 1518831f5dcfSAlexander Motin slot->host.ios.power_mode = value; 1519831f5dcfSAlexander Motin break; 1520831f5dcfSAlexander Motin case MMCBR_IVAR_VDD: 1521831f5dcfSAlexander Motin slot->host.ios.vdd = value; 1522831f5dcfSAlexander Motin break; 1523831f5dcfSAlexander Motin case MMCBR_IVAR_TIMING: 1524831f5dcfSAlexander Motin slot->host.ios.timing = value; 1525831f5dcfSAlexander Motin break; 1526831f5dcfSAlexander Motin case MMCBR_IVAR_CAPS: 1527831f5dcfSAlexander Motin case MMCBR_IVAR_HOST_OCR: 1528831f5dcfSAlexander Motin case MMCBR_IVAR_F_MIN: 1529831f5dcfSAlexander Motin case MMCBR_IVAR_F_MAX: 15303a4a2557SAlexander Motin case MMCBR_IVAR_MAX_DATA: 1531831f5dcfSAlexander Motin return (EINVAL); 1532831f5dcfSAlexander Motin } 1533831f5dcfSAlexander Motin return (0); 1534831f5dcfSAlexander Motin } 1535831f5dcfSAlexander Motin 1536831f5dcfSAlexander Motin static device_method_t sdhci_methods[] = { 1537831f5dcfSAlexander Motin /* device_if */ 1538831f5dcfSAlexander Motin DEVMETHOD(device_probe, sdhci_probe), 1539831f5dcfSAlexander Motin DEVMETHOD(device_attach, sdhci_attach), 1540831f5dcfSAlexander Motin DEVMETHOD(device_detach, sdhci_detach), 154192bf0e27SAlexander Motin DEVMETHOD(device_suspend, sdhci_suspend), 154292bf0e27SAlexander Motin DEVMETHOD(device_resume, sdhci_resume), 1543831f5dcfSAlexander Motin 1544831f5dcfSAlexander Motin /* Bus interface */ 1545831f5dcfSAlexander Motin DEVMETHOD(bus_read_ivar, sdhci_read_ivar), 1546831f5dcfSAlexander Motin DEVMETHOD(bus_write_ivar, sdhci_write_ivar), 1547831f5dcfSAlexander Motin 1548831f5dcfSAlexander Motin /* mmcbr_if */ 1549831f5dcfSAlexander Motin DEVMETHOD(mmcbr_update_ios, sdhci_update_ios), 1550831f5dcfSAlexander Motin DEVMETHOD(mmcbr_request, sdhci_request), 1551831f5dcfSAlexander Motin DEVMETHOD(mmcbr_get_ro, sdhci_get_ro), 1552831f5dcfSAlexander Motin DEVMETHOD(mmcbr_acquire_host, sdhci_acquire_host), 1553831f5dcfSAlexander Motin DEVMETHOD(mmcbr_release_host, sdhci_release_host), 1554831f5dcfSAlexander Motin 1555831f5dcfSAlexander Motin {0, 0}, 1556831f5dcfSAlexander Motin }; 1557831f5dcfSAlexander Motin 1558831f5dcfSAlexander Motin static driver_t sdhci_driver = { 1559831f5dcfSAlexander Motin "sdhci", 1560831f5dcfSAlexander Motin sdhci_methods, 1561831f5dcfSAlexander Motin sizeof(struct sdhci_softc), 1562831f5dcfSAlexander Motin }; 1563831f5dcfSAlexander Motin static devclass_t sdhci_devclass; 1564831f5dcfSAlexander Motin 1565831f5dcfSAlexander Motin 1566831f5dcfSAlexander Motin DRIVER_MODULE(sdhci, pci, sdhci_driver, sdhci_devclass, 0, 0); 1567