2 * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/callout.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/resource.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
47 #include <dev/mmc/bridge.h>
48 #include <dev/mmc/mmcreg.h>
49 #include <dev/mmc/mmcbrvar.h>
55 SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
57 static int sdhci_debug;
58 TUNABLE_INT("hw.sdhci.debug", &sdhci_debug);
59 SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, "Debug level");
61 #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off))
62 #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off))
63 #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off))
64 #define RD_MULTI_4(slot, off, ptr, count) \
65 SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
67 #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
68 #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
69 #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
70 #define WR_MULTI_4(slot, off, ptr, count) \
71 SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
73 static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
74 static void sdhci_start(struct sdhci_slot *slot);
75 static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
77 static void sdhci_card_poll(void *);
78 static void sdhci_card_task(void *, int);
81 #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx)
82 #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx)
83 #define SDHCI_LOCK_INIT(_slot) \
84 mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
85 #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx);
86 #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED);
87 #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED);
89 #define SDHCI_DEFAULT_MAX_FREQ 50
91 #define SDHCI_200_MAX_DIVIDER 256
92 #define SDHCI_300_MAX_DIVIDER 2046
94 #define SDHCI_CARD_PRESENT_TICKS (hz / 5)
95 #define SDHCI_INSERT_DELAY_TICKS (hz / 2)
98 * Broadcom BCM577xx Controller Constants
100 #define BCM577XX_DEFAULT_MAX_DIVIDER 256 /* Maximum divider supported by the default clock source. */
101 #define BCM577XX_ALT_CLOCK_BASE 63000000 /* Alternative clock's base frequency. */
103 #define BCM577XX_HOST_CONTROL 0x198
104 #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF
105 #define BCM577XX_CTRL_CLKSEL_SHIFT 12
106 #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0
107 #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3
111 sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
114 printf("getaddr: error %d\n", error);
117 *(bus_addr_t *)arg = segs[0].ds_addr;
121 slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
126 retval = printf("%s-slot%d: ",
127 device_get_nameunit(slot->bus), slot->num);
130 retval += vprintf(fmt, ap);
136 sdhci_dumpregs(struct sdhci_slot *slot)
139 "============== REGISTER DUMP ==============\n");
141 slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n",
142 RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
143 slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n",
144 RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
145 slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
146 RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
147 slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n",
148 RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
149 slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n",
150 RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
151 slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n",
152 RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
153 slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n",
154 RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
155 slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
156 RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
157 slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n",
158 RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS));
159 slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n",
160 RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT));
163 "===========================================\n");
167 sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
171 if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
172 if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
176 /* Some controllers need this kick or reset won't work. */
177 if ((mask & SDHCI_RESET_ALL) == 0 &&
178 (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
181 /* This is to force an update */
184 sdhci_set_clock(slot, clock);
187 if (mask & SDHCI_RESET_ALL) {
192 WR1(slot, SDHCI_SOFTWARE_RESET, mask);
194 if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
196 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
197 * specification. The reset bit has internal propagation delay,
198 * so a fast read after write returns 0 even if reset process is
199 * in progress. The workaround is to poll for 1 before polling
200 * for 0. In the worst case, if we miss seeing it asserted the
201 * time we spent waiting is enough to ensure the reset finishes.
204 while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
212 /* Wait max 100 ms */
214 /* Controller clears the bits when it's done */
215 while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
217 slot_printf(slot, "Reset 0x%x never completed.\n",
219 sdhci_dumpregs(slot);
228 sdhci_init(struct sdhci_slot *slot)
231 sdhci_reset(slot, SDHCI_RESET_ALL);
233 /* Enable interrupts. */
234 slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
235 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
236 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
237 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
238 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
241 if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
242 !(slot->opt & SDHCI_NON_REMOVABLE)) {
243 slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
246 WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
247 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
251 sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
260 if (clock == slot->clock)
264 /* Turn off the clock. */
265 clk = RD2(slot, SDHCI_CLOCK_CONTROL);
266 WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
267 /* If no clock requested - left it so. */
271 /* Determine the clock base frequency */
272 clk_base = slot->max_clk;
273 if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
274 clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & BCM577XX_CTRL_CLKSEL_MASK;
276 /* Select clock source appropriate for the requested frequency. */
277 if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
278 clk_base = BCM577XX_ALT_CLOCK_BASE;
279 clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << BCM577XX_CTRL_CLKSEL_SHIFT);
281 clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << BCM577XX_CTRL_CLKSEL_SHIFT);
284 WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
287 /* Recalculate timeout clock frequency based on the new sd clock. */
288 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
289 slot->timeout_clk = slot->clock / 1000;
291 if (slot->version < SDHCI_SPEC_300) {
292 /* Looking for highest freq <= clock. */
294 for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
299 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
303 /* Version 3.0 divisors are multiples of two up to 1023*2 */
304 if (clock >= clk_base)
307 for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
308 if ((clk_base / div) <= clock)
315 if (bootverbose || sdhci_debug)
316 slot_printf(slot, "Divider %d for freq %d (base %d)\n",
317 div, clock, clk_base);
319 /* Now we have got divider, set it. */
320 clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
321 clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
322 << SDHCI_DIVIDER_HI_SHIFT;
324 WR2(slot, SDHCI_CLOCK_CONTROL, clk);
326 clk |= SDHCI_CLOCK_INT_EN;
327 WR2(slot, SDHCI_CLOCK_CONTROL, clk);
328 /* Wait up to 10 ms until it stabilize. */
330 while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
331 & SDHCI_CLOCK_INT_STABLE)) {
334 "Internal clock never stabilised.\n");
335 sdhci_dumpregs(slot);
341 /* Pass clock signal to the bus. */
342 clk |= SDHCI_CLOCK_CARD_EN;
343 WR2(slot, SDHCI_CLOCK_CONTROL, clk);
347 sdhci_set_power(struct sdhci_slot *slot, u_char power)
351 if (slot->power == power)
356 /* Turn off the power. */
358 WR1(slot, SDHCI_POWER_CONTROL, pwr);
359 /* If power down requested - left it so. */
363 switch (1 << power) {
364 case MMC_OCR_LOW_VOLTAGE:
365 pwr |= SDHCI_POWER_180;
367 case MMC_OCR_290_300:
368 case MMC_OCR_300_310:
369 pwr |= SDHCI_POWER_300;
371 case MMC_OCR_320_330:
372 case MMC_OCR_330_340:
373 pwr |= SDHCI_POWER_330;
376 WR1(slot, SDHCI_POWER_CONTROL, pwr);
377 /* Turn on the power. */
378 pwr |= SDHCI_POWER_ON;
379 WR1(slot, SDHCI_POWER_CONTROL, pwr);
383 sdhci_read_block_pio(struct sdhci_slot *slot)
389 buffer = slot->curcmd->data->data;
390 buffer += slot->offset;
391 /* Transfer one block at a time. */
392 left = min(512, slot->curcmd->data->len - slot->offset);
393 slot->offset += left;
395 /* If we are too fast, broken controllers return zeroes. */
396 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
398 /* Handle unaligned and aligned buffer cases. */
399 if ((intptr_t)buffer & 3) {
401 data = RD4(slot, SDHCI_BUFFER);
403 buffer[1] = (data >> 8);
404 buffer[2] = (data >> 16);
405 buffer[3] = (data >> 24);
410 RD_MULTI_4(slot, SDHCI_BUFFER,
411 (uint32_t *)buffer, left >> 2);
414 /* Handle uneven size case. */
416 data = RD4(slot, SDHCI_BUFFER);
426 sdhci_write_block_pio(struct sdhci_slot *slot)
432 buffer = slot->curcmd->data->data;
433 buffer += slot->offset;
434 /* Transfer one block at a time. */
435 left = min(512, slot->curcmd->data->len - slot->offset);
436 slot->offset += left;
438 /* Handle unaligned and aligned buffer cases. */
439 if ((intptr_t)buffer & 3) {
447 WR4(slot, SDHCI_BUFFER, data);
450 WR_MULTI_4(slot, SDHCI_BUFFER,
451 (uint32_t *)buffer, left >> 2);
454 /* Handle uneven size case. */
461 WR4(slot, SDHCI_BUFFER, data);
466 sdhci_transfer_pio(struct sdhci_slot *slot)
469 /* Read as many blocks as possible. */
470 if (slot->curcmd->data->flags & MMC_DATA_READ) {
471 while (RD4(slot, SDHCI_PRESENT_STATE) &
472 SDHCI_DATA_AVAILABLE) {
473 sdhci_read_block_pio(slot);
474 if (slot->offset >= slot->curcmd->data->len)
478 while (RD4(slot, SDHCI_PRESENT_STATE) &
479 SDHCI_SPACE_AVAILABLE) {
480 sdhci_write_block_pio(slot);
481 if (slot->offset >= slot->curcmd->data->len)
488 sdhci_card_task(void *arg, int pending)
490 struct sdhci_slot *slot = arg;
493 if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
494 if (slot->dev == NULL) {
495 /* If card is present - attach mmc bus. */
496 if (bootverbose || sdhci_debug)
497 slot_printf(slot, "Card inserted\n");
498 slot->dev = device_add_child(slot->bus, "mmc", -1);
499 device_set_ivars(slot->dev, slot);
501 device_probe_and_attach(slot->dev);
505 if (slot->dev != NULL) {
506 /* If no card present - detach mmc bus. */
507 if (bootverbose || sdhci_debug)
508 slot_printf(slot, "Card removed\n");
509 device_t d = slot->dev;
512 device_delete_child(slot->bus, d);
519 sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
524 * If there was no card and now there is one, schedule the task to
525 * create the child device after a short delay. The delay is to
526 * debounce the card insert (sometimes the card detect pin stabilizes
527 * before the other pins have made good contact).
529 * If there was a card present and now it's gone, immediately schedule
530 * the task to delete the child device. No debouncing -- gone is gone,
531 * because once power is removed, a full card re-init is needed, and
532 * that happens by deleting and recreating the child device.
534 was_present = slot->dev != NULL;
535 if (!was_present && is_present) {
536 taskqueue_enqueue_timeout(taskqueue_swi_giant,
537 &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
538 } else if (was_present && !is_present) {
539 taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
544 sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
548 sdhci_handle_card_present_locked(slot, is_present);
553 sdhci_card_poll(void *arg)
555 struct sdhci_slot *slot = arg;
557 sdhci_handle_card_present(slot,
558 SDHCI_GET_CARD_PRESENT(slot->bus, slot));
559 callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
560 sdhci_card_poll, slot);
564 sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
569 SDHCI_LOCK_INIT(slot);
573 /* Allocate DMA tag. */
574 err = bus_dma_tag_create(bus_get_dma_tag(dev),
575 DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
576 BUS_SPACE_MAXADDR, NULL, NULL,
577 DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
578 BUS_DMA_ALLOCNOW, NULL, NULL,
581 device_printf(dev, "Can't create DMA tag\n");
582 SDHCI_LOCK_DESTROY(slot);
585 /* Allocate DMA memory. */
586 err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
587 BUS_DMA_NOWAIT, &slot->dmamap);
589 device_printf(dev, "Can't alloc DMA memory\n");
590 SDHCI_LOCK_DESTROY(slot);
593 /* Map the memory. */
594 err = bus_dmamap_load(slot->dmatag, slot->dmamap,
595 (void *)slot->dmamem, DMA_BLOCK_SIZE,
596 sdhci_getaddr, &slot->paddr, 0);
597 if (err != 0 || slot->paddr == 0) {
598 device_printf(dev, "Can't load DMA memory\n");
599 SDHCI_LOCK_DESTROY(slot);
606 /* Initialize slot. */
608 slot->version = (RD2(slot, SDHCI_HOST_VERSION)
609 >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
610 if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS)
613 caps = RD4(slot, SDHCI_CAPABILITIES);
614 /* Calculate base clock frequency. */
615 if (slot->version >= SDHCI_SPEC_300)
616 freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
617 SDHCI_CLOCK_BASE_SHIFT;
619 freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
620 SDHCI_CLOCK_BASE_SHIFT;
622 slot->max_clk = freq * 1000000;
624 * If the frequency wasn't in the capabilities and the hardware driver
625 * hasn't already set max_clk we're probably not going to work right
626 * with an assumption, so complain about it.
628 if (slot->max_clk == 0) {
629 slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
630 device_printf(dev, "Hardware doesn't specify base clock "
631 "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ);
633 /* Calculate timeout clock frequency. */
634 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
635 slot->timeout_clk = slot->max_clk / 1000;
638 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
639 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
640 slot->timeout_clk *= 1000;
643 * If the frequency wasn't in the capabilities and the hardware driver
644 * hasn't already set timeout_clk we'll probably work okay using the
645 * max timeout, but still mention it.
647 if (slot->timeout_clk == 0) {
648 device_printf(dev, "Hardware doesn't specify timeout clock "
649 "frequency, setting BROKEN_TIMEOUT quirk.\n");
650 slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
653 slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
654 slot->host.f_max = slot->max_clk;
655 slot->host.host_ocr = 0;
656 if (caps & SDHCI_CAN_VDD_330)
657 slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
658 if (caps & SDHCI_CAN_VDD_300)
659 slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
660 if (caps & SDHCI_CAN_VDD_180)
661 slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
662 if (slot->host.host_ocr == 0) {
663 device_printf(dev, "Hardware doesn't report any "
664 "support voltages.\n");
666 slot->host.caps = MMC_CAP_4_BIT_DATA;
667 if (caps & SDHCI_CAN_DO_8BITBUS)
668 slot->host.caps |= MMC_CAP_8_BIT_DATA;
669 if (caps & SDHCI_CAN_DO_HISPD)
670 slot->host.caps |= MMC_CAP_HSPEED;
671 /* Decide if we have usable DMA. */
672 if (caps & SDHCI_CAN_DO_DMA)
673 slot->opt |= SDHCI_HAVE_DMA;
675 if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
676 slot->opt &= ~SDHCI_HAVE_DMA;
677 if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
678 slot->opt |= SDHCI_HAVE_DMA;
681 * Use platform-provided transfer backend
682 * with PIO as a fallback mechanism
684 if (slot->opt & SDHCI_PLATFORM_TRANSFER)
685 slot->opt &= ~SDHCI_HAVE_DMA;
687 if (bootverbose || sdhci_debug) {
688 slot_printf(slot, "%uMHz%s %s%s%s%s %s\n",
689 slot->max_clk / 1000000,
690 (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
691 (caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
692 ((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
693 (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
694 (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
695 (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
696 (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
697 sdhci_dumpregs(slot);
701 SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
702 SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
703 "timeout", CTLFLAG_RW, &slot->timeout, 0,
704 "Maximum timeout for SDHCI transfers (in secs)");
705 TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
706 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
707 sdhci_card_task, slot);
708 callout_init(&slot->card_poll_callout, 1);
709 callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
711 if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
712 !(slot->opt & SDHCI_NON_REMOVABLE)) {
713 callout_reset(&slot->card_poll_callout,
714 SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
721 sdhci_start_slot(struct sdhci_slot *slot)
723 sdhci_card_task(slot, 0);
727 sdhci_cleanup_slot(struct sdhci_slot *slot)
731 callout_drain(&slot->timeout_callout);
732 callout_drain(&slot->card_poll_callout);
733 taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
734 taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
741 device_delete_child(slot->bus, d);
744 sdhci_reset(slot, SDHCI_RESET_ALL);
746 bus_dmamap_unload(slot->dmatag, slot->dmamap);
747 bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
748 bus_dma_tag_destroy(slot->dmatag);
750 SDHCI_LOCK_DESTROY(slot);
756 sdhci_generic_suspend(struct sdhci_slot *slot)
758 sdhci_reset(slot, SDHCI_RESET_ALL);
764 sdhci_generic_resume(struct sdhci_slot *slot)
772 sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot)
774 if (slot->version >= SDHCI_SPEC_300)
775 return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
777 return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
781 sdhci_generic_get_card_present(device_t brdev, struct sdhci_slot *slot)
784 if (slot->opt & SDHCI_NON_REMOVABLE)
787 return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
791 sdhci_generic_update_ios(device_t brdev, device_t reqdev)
793 struct sdhci_slot *slot = device_get_ivars(reqdev);
794 struct mmc_ios *ios = &slot->host.ios;
797 /* Do full reset on bus power down to clear from any state. */
798 if (ios->power_mode == power_off) {
799 WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
802 /* Configure the bus. */
803 sdhci_set_clock(slot, ios->clock);
804 sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
805 if (ios->bus_width == bus_width_8) {
806 slot->hostctrl |= SDHCI_CTRL_8BITBUS;
807 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
808 } else if (ios->bus_width == bus_width_4) {
809 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
810 slot->hostctrl |= SDHCI_CTRL_4BITBUS;
811 } else if (ios->bus_width == bus_width_1) {
812 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
813 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
815 panic("Invalid bus width: %d", ios->bus_width);
817 if (ios->timing == bus_timing_hs &&
818 !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
819 slot->hostctrl |= SDHCI_CTRL_HISPD;
821 slot->hostctrl &= ~SDHCI_CTRL_HISPD;
822 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
823 /* Some controllers like reset after bus changes. */
824 if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
825 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
832 sdhci_req_done(struct sdhci_slot *slot)
834 struct mmc_request *req;
836 if (slot->req != NULL && slot->curcmd != NULL) {
837 callout_stop(&slot->timeout_callout);
846 sdhci_timeout(void *arg)
848 struct sdhci_slot *slot = arg;
850 if (slot->curcmd != NULL) {
851 slot_printf(slot, " Controller timeout\n");
852 sdhci_dumpregs(slot);
853 sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
854 slot->curcmd->error = MMC_ERR_TIMEOUT;
855 sdhci_req_done(slot);
857 slot_printf(slot, " Spurious timeout - no active command\n");
862 sdhci_set_transfer_mode(struct sdhci_slot *slot,
863 struct mmc_data *data)
870 mode = SDHCI_TRNS_BLK_CNT_EN;
872 mode |= SDHCI_TRNS_MULTI;
873 if (data->flags & MMC_DATA_READ)
874 mode |= SDHCI_TRNS_READ;
876 mode |= SDHCI_TRNS_ACMD12;
877 if (slot->flags & SDHCI_USE_DMA)
878 mode |= SDHCI_TRNS_DMA;
880 WR2(slot, SDHCI_TRANSFER_MODE, mode);
884 sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
892 cmd->error = MMC_ERR_NONE;
894 /* This flags combination is not supported by controller. */
895 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
896 slot_printf(slot, "Unsupported response type!\n");
897 cmd->error = MMC_ERR_FAILED;
898 sdhci_req_done(slot);
902 /* Do not issue command if there is no card, clock or power.
903 * Controller will not detect timeout without clock active. */
904 if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
907 cmd->error = MMC_ERR_FAILED;
908 sdhci_req_done(slot);
911 /* Always wait for free CMD bus. */
912 mask = SDHCI_CMD_INHIBIT;
913 /* Wait for free DAT if we have data or busy signal. */
914 if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
915 mask |= SDHCI_DAT_INHIBIT;
916 /* We shouldn't wait for DAT for stop commands. */
917 if (cmd == slot->req->stop)
918 mask &= ~SDHCI_DAT_INHIBIT;
920 * Wait for bus no more then 250 ms. Typically there will be no wait
921 * here at all, but when writing a crash dump we may be bypassing the
922 * host platform's interrupt handler, and in some cases that handler
923 * may be working around hardware quirks such as not respecting r1b
924 * busy indications. In those cases, this wait-loop serves the purpose
925 * of waiting for the prior command and data transfers to be done, and
926 * SD cards are allowed to take up to 250ms for write and erase ops.
927 * (It's usually more like 20-30ms in the real world.)
930 while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
932 slot_printf(slot, "Controller never released "
933 "inhibit bit(s).\n");
934 sdhci_dumpregs(slot);
935 cmd->error = MMC_ERR_FAILED;
936 sdhci_req_done(slot);
943 /* Prepare command flags. */
944 if (!(cmd->flags & MMC_RSP_PRESENT))
945 flags = SDHCI_CMD_RESP_NONE;
946 else if (cmd->flags & MMC_RSP_136)
947 flags = SDHCI_CMD_RESP_LONG;
948 else if (cmd->flags & MMC_RSP_BUSY)
949 flags = SDHCI_CMD_RESP_SHORT_BUSY;
951 flags = SDHCI_CMD_RESP_SHORT;
952 if (cmd->flags & MMC_RSP_CRC)
953 flags |= SDHCI_CMD_CRC;
954 if (cmd->flags & MMC_RSP_OPCODE)
955 flags |= SDHCI_CMD_INDEX;
957 flags |= SDHCI_CMD_DATA;
958 if (cmd->opcode == MMC_STOP_TRANSMISSION)
959 flags |= SDHCI_CMD_TYPE_ABORT;
961 sdhci_start_data(slot, cmd->data);
963 * Interrupt aggregation: To reduce total number of interrupts
964 * group response interrupt with data interrupt when possible.
965 * If there going to be data interrupt, mask response one.
967 if (slot->data_done == 0) {
968 WR4(slot, SDHCI_SIGNAL_ENABLE,
969 slot->intmask &= ~SDHCI_INT_RESPONSE);
971 /* Set command argument. */
972 WR4(slot, SDHCI_ARGUMENT, cmd->arg);
973 /* Set data transfer mode. */
974 sdhci_set_transfer_mode(slot, cmd->data);
976 WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
977 /* Start timeout callout. */
978 callout_reset(&slot->timeout_callout, slot->timeout * hz,
979 sdhci_timeout, slot);
983 sdhci_finish_command(struct sdhci_slot *slot)
988 /* Interrupt aggregation: Restore command interrupt.
989 * Main restore point for the case when command interrupt
991 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
992 /* In case of error - reset host and return. */
993 if (slot->curcmd->error) {
994 sdhci_reset(slot, SDHCI_RESET_CMD);
995 sdhci_reset(slot, SDHCI_RESET_DATA);
999 /* If command has response - fetch it. */
1000 if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1001 if (slot->curcmd->flags & MMC_RSP_136) {
1002 /* CRC is stripped so we need one byte shift. */
1004 for (i = 0; i < 4; i++) {
1005 uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4);
1006 if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1007 slot->curcmd->resp[3 - i] = val;
1009 slot->curcmd->resp[3 - i] =
1015 slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1017 /* If data ready - finish. */
1018 if (slot->data_done)
1023 sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1025 uint32_t target_timeout, current_timeout;
1028 if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1029 slot->data_done = 1;
1033 slot->data_done = 0;
1035 /* Calculate and set data timeout.*/
1036 /* XXX: We should have this from mmc layer, now assume 1 sec. */
1037 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1040 target_timeout = 1000000;
1042 current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1043 while (current_timeout < target_timeout && div < 0xE) {
1045 current_timeout <<= 1;
1047 /* Compensate for an off-by-one error in the CaFe chip.*/
1049 (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1053 WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1058 /* Use DMA if possible. */
1059 if ((slot->opt & SDHCI_HAVE_DMA))
1060 slot->flags |= SDHCI_USE_DMA;
1061 /* If data is small, broken DMA may return zeroes instead of data, */
1062 if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1064 slot->flags &= ~SDHCI_USE_DMA;
1065 /* Some controllers require even block sizes. */
1066 if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1067 ((data->len) & 0x3))
1068 slot->flags &= ~SDHCI_USE_DMA;
1069 /* Load DMA buffer. */
1070 if (slot->flags & SDHCI_USE_DMA) {
1071 if (data->flags & MMC_DATA_READ)
1072 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1073 BUS_DMASYNC_PREREAD);
1075 memcpy(slot->dmamem, data->data,
1076 (data->len < DMA_BLOCK_SIZE) ?
1077 data->len : DMA_BLOCK_SIZE);
1078 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1079 BUS_DMASYNC_PREWRITE);
1081 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1082 /* Interrupt aggregation: Mask border interrupt
1083 * for the last page and unmask else. */
1084 if (data->len == DMA_BLOCK_SIZE)
1085 slot->intmask &= ~SDHCI_INT_DMA_END;
1087 slot->intmask |= SDHCI_INT_DMA_END;
1088 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1090 /* Current data offset for both PIO and DMA. */
1092 /* Set block size and request IRQ on 4K border. */
1093 WR2(slot, SDHCI_BLOCK_SIZE,
1094 SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512));
1095 /* Set block count. */
1096 WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1100 sdhci_finish_data(struct sdhci_slot *slot)
1102 struct mmc_data *data = slot->curcmd->data;
1104 /* Interrupt aggregation: Restore command interrupt.
1105 * Auxiliary restore point for the case when data interrupt
1106 * happened first. */
1107 if (!slot->cmd_done) {
1108 WR4(slot, SDHCI_SIGNAL_ENABLE,
1109 slot->intmask |= SDHCI_INT_RESPONSE);
1111 /* Unload rest of data from DMA buffer. */
1112 if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) {
1113 if (data->flags & MMC_DATA_READ) {
1114 size_t left = data->len - slot->offset;
1115 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1116 BUS_DMASYNC_POSTREAD);
1117 memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1118 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1120 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1121 BUS_DMASYNC_POSTWRITE);
1123 slot->data_done = 1;
1124 /* If there was error - reset the host. */
1125 if (slot->curcmd->error) {
1126 sdhci_reset(slot, SDHCI_RESET_CMD);
1127 sdhci_reset(slot, SDHCI_RESET_DATA);
1131 /* If we already have command response - finish. */
1137 sdhci_start(struct sdhci_slot *slot)
1139 struct mmc_request *req;
1145 if (!(slot->flags & CMD_STARTED)) {
1146 slot->flags |= CMD_STARTED;
1147 sdhci_start_command(slot, req->cmd);
1150 /* We don't need this until using Auto-CMD12 feature
1151 if (!(slot->flags & STOP_STARTED) && req->stop) {
1152 slot->flags |= STOP_STARTED;
1153 sdhci_start_command(slot, req->stop);
1157 if (sdhci_debug > 1)
1158 slot_printf(slot, "result: %d\n", req->cmd->error);
1159 if (!req->cmd->error &&
1160 (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1161 sdhci_reset(slot, SDHCI_RESET_CMD);
1162 sdhci_reset(slot, SDHCI_RESET_DATA);
1165 sdhci_req_done(slot);
1169 sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req)
1171 struct sdhci_slot *slot = device_get_ivars(reqdev);
1174 if (slot->req != NULL) {
1178 if (sdhci_debug > 1) {
1179 slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1180 req->cmd->opcode, req->cmd->arg, req->cmd->flags,
1181 (req->cmd->data)?(u_int)req->cmd->data->len:0,
1182 (req->cmd->data)?req->cmd->data->flags:0);
1189 while (slot->req != NULL) {
1190 sdhci_generic_intr(slot);
1198 sdhci_generic_get_ro(device_t brdev, device_t reqdev)
1200 struct sdhci_slot *slot = device_get_ivars(reqdev);
1204 val = RD4(slot, SDHCI_PRESENT_STATE);
1206 return (!(val & SDHCI_WRITE_PROTECT));
1210 sdhci_generic_acquire_host(device_t brdev, device_t reqdev)
1212 struct sdhci_slot *slot = device_get_ivars(reqdev);
1216 while (slot->bus_busy)
1217 msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1220 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1226 sdhci_generic_release_host(device_t brdev, device_t reqdev)
1228 struct sdhci_slot *slot = device_get_ivars(reqdev);
1231 /* Deactivate led. */
1232 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1240 sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1243 if (!slot->curcmd) {
1244 slot_printf(slot, "Got command interrupt 0x%08x, but "
1245 "there is no active command.\n", intmask);
1246 sdhci_dumpregs(slot);
1249 if (intmask & SDHCI_INT_TIMEOUT)
1250 slot->curcmd->error = MMC_ERR_TIMEOUT;
1251 else if (intmask & SDHCI_INT_CRC)
1252 slot->curcmd->error = MMC_ERR_BADCRC;
1253 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1254 slot->curcmd->error = MMC_ERR_FIFO;
1256 sdhci_finish_command(slot);
1260 sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1263 if (!slot->curcmd) {
1264 slot_printf(slot, "Got data interrupt 0x%08x, but "
1265 "there is no active command.\n", intmask);
1266 sdhci_dumpregs(slot);
1269 if (slot->curcmd->data == NULL &&
1270 (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1271 slot_printf(slot, "Got data interrupt 0x%08x, but "
1272 "there is no active data operation.\n",
1274 sdhci_dumpregs(slot);
1277 if (intmask & SDHCI_INT_DATA_TIMEOUT)
1278 slot->curcmd->error = MMC_ERR_TIMEOUT;
1279 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1280 slot->curcmd->error = MMC_ERR_BADCRC;
1281 if (slot->curcmd->data == NULL &&
1282 (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1283 SDHCI_INT_DMA_END))) {
1284 slot_printf(slot, "Got data interrupt 0x%08x, but "
1285 "there is busy-only command.\n", intmask);
1286 sdhci_dumpregs(slot);
1287 slot->curcmd->error = MMC_ERR_INVALID;
1289 if (slot->curcmd->error) {
1290 /* No need to continue after any error. */
1294 /* Handle PIO interrupt. */
1295 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1296 if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1297 SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
1298 SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask);
1299 slot->flags |= PLATFORM_DATA_STARTED;
1301 sdhci_transfer_pio(slot);
1303 /* Handle DMA border. */
1304 if (intmask & SDHCI_INT_DMA_END) {
1305 struct mmc_data *data = slot->curcmd->data;
1308 /* Unload DMA buffer... */
1309 left = data->len - slot->offset;
1310 if (data->flags & MMC_DATA_READ) {
1311 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1312 BUS_DMASYNC_POSTREAD);
1313 memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1314 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1316 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1317 BUS_DMASYNC_POSTWRITE);
1319 /* ... and reload it again. */
1320 slot->offset += DMA_BLOCK_SIZE;
1321 left = data->len - slot->offset;
1322 if (data->flags & MMC_DATA_READ) {
1323 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1324 BUS_DMASYNC_PREREAD);
1326 memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1327 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1328 bus_dmamap_sync(slot->dmatag, slot->dmamap,
1329 BUS_DMASYNC_PREWRITE);
1331 /* Interrupt aggregation: Mask border interrupt
1332 * for the last page. */
1333 if (left == DMA_BLOCK_SIZE) {
1334 slot->intmask &= ~SDHCI_INT_DMA_END;
1335 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1338 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1340 /* We have got all data. */
1341 if (intmask & SDHCI_INT_DATA_END) {
1342 if (slot->flags & PLATFORM_DATA_STARTED) {
1343 slot->flags &= ~PLATFORM_DATA_STARTED;
1344 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1346 sdhci_finish_data(slot);
1349 if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1350 if (slot->flags & PLATFORM_DATA_STARTED) {
1351 slot->flags &= ~PLATFORM_DATA_STARTED;
1352 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1354 sdhci_finish_data(slot);
1360 sdhci_acmd_irq(struct sdhci_slot *slot)
1364 err = RD4(slot, SDHCI_ACMD12_ERR);
1365 if (!slot->curcmd) {
1366 slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1367 "there is no active command.\n", err);
1368 sdhci_dumpregs(slot);
1371 slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1372 sdhci_reset(slot, SDHCI_RESET_CMD);
1376 sdhci_generic_intr(struct sdhci_slot *slot)
1378 uint32_t intmask, present;
1381 /* Read slot interrupt status. */
1382 intmask = RD4(slot, SDHCI_INT_STATUS);
1383 if (intmask == 0 || intmask == 0xffffffff) {
1387 if (sdhci_debug > 2)
1388 slot_printf(slot, "Interrupt %#x\n", intmask);
1390 /* Handle card presence interrupts. */
1391 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1392 present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
1394 ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1395 slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
1396 SDHCI_INT_CARD_INSERT;
1397 WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1398 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1399 WR4(slot, SDHCI_INT_STATUS, intmask &
1400 (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1401 sdhci_handle_card_present_locked(slot, present);
1402 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1404 /* Handle command interrupts. */
1405 if (intmask & SDHCI_INT_CMD_MASK) {
1406 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1407 sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1409 /* Handle data interrupts. */
1410 if (intmask & SDHCI_INT_DATA_MASK) {
1411 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
1412 /* Dont call data_irq in case of errored command */
1413 if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1414 sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1416 /* Handle AutoCMD12 error interrupt. */
1417 if (intmask & SDHCI_INT_ACMD12ERR) {
1418 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1419 sdhci_acmd_irq(slot);
1421 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1422 intmask &= ~SDHCI_INT_ACMD12ERR;
1423 intmask &= ~SDHCI_INT_ERROR;
1424 /* Handle bus power interrupt. */
1425 if (intmask & SDHCI_INT_BUS_POWER) {
1426 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1428 "Card is consuming too much power!\n");
1429 intmask &= ~SDHCI_INT_BUS_POWER;
1431 /* The rest is unknown. */
1433 WR4(slot, SDHCI_INT_STATUS, intmask);
1434 slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1436 sdhci_dumpregs(slot);
1443 sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1445 struct sdhci_slot *slot = device_get_ivars(child);
1450 case MMCBR_IVAR_BUS_MODE:
1451 *result = slot->host.ios.bus_mode;
1453 case MMCBR_IVAR_BUS_WIDTH:
1454 *result = slot->host.ios.bus_width;
1456 case MMCBR_IVAR_CHIP_SELECT:
1457 *result = slot->host.ios.chip_select;
1459 case MMCBR_IVAR_CLOCK:
1460 *result = slot->host.ios.clock;
1462 case MMCBR_IVAR_F_MIN:
1463 *result = slot->host.f_min;
1465 case MMCBR_IVAR_F_MAX:
1466 *result = slot->host.f_max;
1468 case MMCBR_IVAR_HOST_OCR:
1469 *result = slot->host.host_ocr;
1471 case MMCBR_IVAR_MODE:
1472 *result = slot->host.mode;
1474 case MMCBR_IVAR_OCR:
1475 *result = slot->host.ocr;
1477 case MMCBR_IVAR_POWER_MODE:
1478 *result = slot->host.ios.power_mode;
1480 case MMCBR_IVAR_VDD:
1481 *result = slot->host.ios.vdd;
1483 case MMCBR_IVAR_CAPS:
1484 *result = slot->host.caps;
1486 case MMCBR_IVAR_TIMING:
1487 *result = slot->host.ios.timing;
1489 case MMCBR_IVAR_MAX_DATA:
1497 sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1499 struct sdhci_slot *slot = device_get_ivars(child);
1504 case MMCBR_IVAR_BUS_MODE:
1505 slot->host.ios.bus_mode = value;
1507 case MMCBR_IVAR_BUS_WIDTH:
1508 slot->host.ios.bus_width = value;
1510 case MMCBR_IVAR_CHIP_SELECT:
1511 slot->host.ios.chip_select = value;
1513 case MMCBR_IVAR_CLOCK:
1519 max_clock = slot->max_clk;
1522 if (slot->version < SDHCI_SPEC_300) {
1523 for (i = 0; i < SDHCI_200_MAX_DIVIDER;
1531 for (i = 0; i < SDHCI_300_MAX_DIVIDER;
1535 clock = max_clock / (i + 2);
1539 slot->host.ios.clock = clock;
1541 slot->host.ios.clock = 0;
1543 case MMCBR_IVAR_MODE:
1544 slot->host.mode = value;
1546 case MMCBR_IVAR_OCR:
1547 slot->host.ocr = value;
1549 case MMCBR_IVAR_POWER_MODE:
1550 slot->host.ios.power_mode = value;
1552 case MMCBR_IVAR_VDD:
1553 slot->host.ios.vdd = value;
1555 case MMCBR_IVAR_TIMING:
1556 slot->host.ios.timing = value;
1558 case MMCBR_IVAR_CAPS:
1559 case MMCBR_IVAR_HOST_OCR:
1560 case MMCBR_IVAR_F_MIN:
1561 case MMCBR_IVAR_F_MAX:
1562 case MMCBR_IVAR_MAX_DATA:
1568 MODULE_VERSION(sdhci, 1);